From patchwork Sat Oct 28 19:55:53 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: 78656 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8661D3864821 for ; Sat, 28 Oct 2023 19:56:25 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by sourceware.org (Postfix) with ESMTPS id 92F84385F01C for ; Sat, 28 Oct 2023 19:56:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 92F84385F01C 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 92F84385F01C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::22d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522973; cv=none; b=pvmyQE8iLua7355ikJbqnIu4I9b4FD5GNp6uqdDRC9gRLjjDYnvqJnlyK3w7XgD+5EkjaSDJ28ia7PCtEKDmzew1hs1LCCcOzciNMj+JKLz+7D24qQHnJQngDqF5zVIW7X+FnMo3W5PqNKGL8em0C6UlbhRQL9NxibAj43exJvQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522973; c=relaxed/simple; bh=fr/vRCe4Tc6by2qRCfck9INftlmRVY3JVUSPKkDgCFE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=ZlV6IAkTzDBwZDXavuns39E/XEMRZb9ZfsQ2/lOdBAg8JWEbyryLtKZ5rXAjkaot3j3dMOMaWC3rNEqAzF4s9WxkudUPRDEiE5QHjWnqdgvUioe+DavZg2quzhbRKdpt/UCPEaegCQfbbUlz2iU1ge40RZLhFfr7l89Hiy81ZgA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x22d.google.com with SMTP id 5614622812f47-3b2f507c03cso1841911b6e.2 for ; Sat, 28 Oct 2023 12:56:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698522970; x=1699127770; 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=I4RpB7qGSb35rdpy7VxSRUxRllXP+KRj7Dspu0xYtZ0=; b=A0HHeIpSjuEryfnRB8K1LVJ6VH7ucMvJI4uk7kcrRAIf8qAqJZyKGe8plGwLW279YN Vnq3c9wbyWpwyo9UyPcV+n5/3CPoh3/7fGOVgTleDqJePMWjNmF8qsW206s4qhi1l2IC 076CXETORjnkjy7ev8ON9yZWCwdoSwB7YSqd5Z5YztfpUuxT8Zei92rwBIwHb0muw5yB ZATrWIzcN2vJkAgEuRtc1K7j0DdG+e8WYnyBx+76HZnnQa6+UX6T2E3UgDO/UJKRm8/5 ofHr+eski6eNPrEApI6FzZRdFm+VwmoruM04jNvwc7bxZdx4RnffyUN6OVGAvf1yo8ZA Omeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698522970; x=1699127770; 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=I4RpB7qGSb35rdpy7VxSRUxRllXP+KRj7Dspu0xYtZ0=; b=QIUdNAOHo7jD8V++DlI1ZnZijatTFf2jbiSlrQj9p7A6r+/Xt54l4C22LrMr8q3hMx x/EOcdwFpuWW4igC+uy4p1LTreAX7gDBPrwyNeDmNg+6+WyhXPH0C7KBGKDpmqVrXCJm Gcclhe0vp8L0pY+t6BsJtwl/FCX5BG18ncMBEG723suTaskWlYqH2kiN4DMywcUNPRly mtACUJP5mbbVBYcVrvaOsN1523yESyFH+oQGV84vqSa1/bre7Y7xHtVjFr2ouCfeYbYj mxB69bx9qP0t9XcfoVD8G1KzU/tOTA6x+Ij1ZtD3KSGMq8bMzc1Cin36Zx7t2rf7DWYI 6TyA== X-Gm-Message-State: AOJu0YxDSaiNLMUhtluwGGc2omf+4T0gBxD1Idi5EDKwSFX+tRtZ6yMi g2zoaV/eG8N2HNX7Bb7E268UXnKS+odqa8vjwxbiVmvb X-Google-Smtp-Source: AGHT+IG5sW8kO989zNyDWUtmUqOm2vYk5YJxCZU4B8l76dvexanWnx69EucRPETdQJDcYI0pIhLKFw== X-Received: by 2002:a05:6808:1482:b0:3ae:4cb1:74e8 with SMTP id e2-20020a056808148200b003ae4cb174e8mr7423443oiw.11.1698522970294; Sat, 28 Oct 2023 12:56:10 -0700 (PDT) Received: from localhost.localdomain ([139.178.84.207]) by smtp.gmail.com with ESMTPSA id w20-20020a056830411400b006b8c87551e8sm763181ott.35.2023.10.28.12.56.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 12:56:08 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Ian Rogers , Francesco Nigro , Carlos O'Donell , Siddhesh Poyarekar Subject: [PATCH v2 1/7] linux: Add PR_SET_VMA_ANON_NAME support Date: Sat, 28 Oct 2023 19:55:53 +0000 Message-Id: <20231028195559.390407-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231028195559.390407-1-adhemerval.zanella@linaro.org> References: <20231028195559.390407-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 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+patchwork=sourceware.org@sourceware.org Linux 5.17 added support to naming anonymous virtual memory areas through the prctl syscall. The __set_vma_name is a wrapper to avoid optimizing the prctl call if the kernel does not support it. If the kernel does not support PR_SET_VMA_ANON_NAME, prctl returns EINVAL. And it also returns the same error for an invalid argument. Since it is an internal-only API, it assumes well-formatted input: aligned START, with (START, START+LEN) being a valid memory range, and NAME with a limit of 80 characters without an invalid one ("\\`$[]"). Reviewed-by: DJ Delorie --- include/sys/prctl.h | 5 ++++ sysdeps/generic/setvmaname.h | 27 +++++++++++++++++ sysdeps/unix/sysv/linux/Makefile | 1 + sysdeps/unix/sysv/linux/setvmaname.c | 44 ++++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/setvmaname.h | 36 +++++++++++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 sysdeps/generic/setvmaname.h create mode 100644 sysdeps/unix/sysv/linux/setvmaname.c create mode 100644 sysdeps/unix/sysv/linux/setvmaname.h diff --git a/include/sys/prctl.h b/include/sys/prctl.h index d33f3a290e..8e8e05b07c 100644 --- a/include/sys/prctl.h +++ b/include/sys/prctl.h @@ -3,6 +3,11 @@ # ifndef _ISOMAC +# ifndef PR_SET_VMA +# define PR_SET_VMA 0x53564d41 +# define PR_SET_VMA_ANON_NAME 0 +# endif + extern int __prctl (int __option, ...); libc_hidden_proto (__prctl) diff --git a/sysdeps/generic/setvmaname.h b/sysdeps/generic/setvmaname.h new file mode 100644 index 0000000000..2824587e9f --- /dev/null +++ b/sysdeps/generic/setvmaname.h @@ -0,0 +1,27 @@ +/* Utilities functions to name memory mappings. + Copyright (C) 2023 Free Software Foundation, Inc. + 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 + . */ + +#ifndef __SETVMANAME_H +#define __SETVMANAME_H + +static inline +void __set_vma_name (void *start, size_t len, const char *name) +{ +} + +#endif diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 063719bae6..250df6f455 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -97,6 +97,7 @@ sysdep_routines += \ readahead \ setfsgid \ setfsuid \ + setvmaname \ signalfd \ splice \ sysctl \ diff --git a/sysdeps/unix/sysv/linux/setvmaname.c b/sysdeps/unix/sysv/linux/setvmaname.c new file mode 100644 index 0000000000..9960ab5917 --- /dev/null +++ b/sysdeps/unix/sysv/linux/setvmaname.c @@ -0,0 +1,44 @@ +/* Utilities functions to name memory mappings. + Copyright (C) 2023 Free Software Foundation, Inc. + 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 + . */ + +#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. + Since it is an internal-only API, it assumes well formatted input: + aligned START, with (START, START+LEN) being a valid memory range, + and NAME with a limit of 80 characters without invalid one ("\\`$[]"). */ + +void +__set_vma_name (void *start, size_t len, const char *name) +{ + static int prctl_supported = 1; + 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; + + atomic_store_relaxed (&prctl_supported, 0); + return; +} diff --git a/sysdeps/unix/sysv/linux/setvmaname.h b/sysdeps/unix/sysv/linux/setvmaname.h new file mode 100644 index 0000000000..eaa7a7a32e --- /dev/null +++ b/sysdeps/unix/sysv/linux/setvmaname.h @@ -0,0 +1,36 @@ +/* Utilities functions to name memory mappings. + Copyright (C) 2023 Free Software Foundation, Inc. + 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 + . */ + +#ifndef __SETVMANAME_H +#define __SETVMANAME_H + +/* Set the NAME to the anonymous memory map START with size of LEN. + It assumes well-formatted input. */ +#if IS_IN(libc) || IS_IN(rtld) +void __set_vma_name (void *start, size_t len, const char *name) + attribute_hidden; +#else +#include + +static inline void __set_vma_name (void *start, size_t len, const char *name) +{ + prctl (PR_SET_VMA, PR_SET_VMA_ANON_NAME, start, len, name); +} +#endif + +#endif From patchwork Sat Oct 28 19:55:54 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: 78657 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 81636386D603 for ; Sat, 28 Oct 2023 19:56:34 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22b.google.com (mail-oi1-x22b.google.com [IPv6:2607:f8b0:4864:20::22b]) by sourceware.org (Postfix) with ESMTPS id 2B5AE385F02B for ; Sat, 28 Oct 2023 19:56:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2B5AE385F02B 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 2B5AE385F02B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::22b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522974; cv=none; b=sk4dtVpYbUaL+ph6xmXC3YRyEODFYMjPYhSDPcAXunJ12puoXwvMRx/fuRDNFjr98V2kD00oxLvc2P/5Df/Fyc+TJi8yPmGoGmn0FuiBB8NyumfT5usviuJf3+rWwB17Lx/aNlqJQ8hjUcQBBs8GSvIj0zivWfF5Bpt9XgAnoUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522974; c=relaxed/simple; bh=8RRL5kABKc1J1lzV6b7gDsG4ZHPjL9tbGRf2dOdaMtU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=T2ETlhLRnD3llbJ3rF6K/LoftlNpcO/ixQ1ENY/czXRh/rG5572EzSAjylttjBeLIIllvi7G1nFaVeTiYirAv5tRbetXGN6xCP6RE+OfWWInCVN/eRP9npeJ6Yg5AuKuX1TXPF1nzZBBKz1mPsJLXIs4Wxp7ig2Y7wWtrYzucUY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x22b.google.com with SMTP id 5614622812f47-3af609c4dfeso2081584b6e.1 for ; Sat, 28 Oct 2023 12:56:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698522972; x=1699127772; 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=s8RVhP5i91O39KSQjCyRzBdQQD/6GnedOsUOtoDrF/Y=; b=mdRGuARZw2oRzR7okgocJlpq4IuPdQV0vasNl1z7eacsGou4JLa6ZLL9ey/2Mtvzn+ X7XUkwjAO9v41r5d93b5MgJ3f4EsXrDyFxbM1Hnvn6lGDSKpu4MQrsbolrsh/UNP6vqO BjzNLRz9FRWFceaf9yX/lKXSCn0K56vU1d/RcAR4HXAcr3VG2PfQzQbRwflarN/ftExM ebCl+u63jTYOZ+UP8YRBwi6IkBCFvzQ+jHHf6kACWBZwNBtOa6r8RlWBSxoa/vAR+F+M Wlbr1lRD44xoe0zsi9G7sZtTrV9CS8cmUHJb2nrGNQTHgKt1+MEIG8QRcThg/WqZziuM /iQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698522972; x=1699127772; 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=s8RVhP5i91O39KSQjCyRzBdQQD/6GnedOsUOtoDrF/Y=; b=qeTcEPtrilgnnwDZx9qv1oiusLP/58qyLFGj+kZEhPKYQwSJy9iA+NgvRplK1MsCaP O2FfBMk77MXO8ACqGCYKb2+uruuZgJi7ZBIaW70KfxTTC6H8vxzKn4r8s/R/j3xo5a0w R+XEoqLz21Lh6acIfLxrcVbKoNEmnzaCcIg3RCbsgOFB0oZjGLvlm5Id+r/qZOkuMQA8 LDeeP1Yd4BqXh3ewlMcgaIJPw6LL0a7z/4L+AOixFcJbePYfKdfU8QRDH89ykgmb0Fxj fAxmNBACOmgx1JBgf0WwDofUnXYeUbiaW6MU6zBgvnI3fvEH9es4KQ70XJRlefsHqr5Z akgA== X-Gm-Message-State: AOJu0Yw2dxyPiNiN7w3qX/mJksp8gxGGEH7kbQP6wJTI5TqrLFBtNLya JJAC5dgE+m57voiWWrgFpmpkoeZi+K3MecSRTgbfx7GP X-Google-Smtp-Source: AGHT+IG8u8ABcydLkkRpUzTk8eBDtcrsfjD5BHNFSLCIcXsviJQw3zvFxsgTrvEn81sB4ElYt1DSzQ== X-Received: by 2002:a05:6808:2981:b0:3ad:fc0f:e852 with SMTP id ex1-20020a056808298100b003adfc0fe852mr6731076oib.23.1698522971814; Sat, 28 Oct 2023 12:56:11 -0700 (PDT) Received: from localhost.localdomain ([139.178.84.207]) by smtp.gmail.com with ESMTPSA id w20-20020a056830411400b006b8c87551e8sm763181ott.35.2023.10.28.12.56.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 12:56:10 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Ian Rogers , Francesco Nigro , Carlos O'Donell , Siddhesh Poyarekar Subject: [PATCH v2 2/7] support: Add support_set_vma_name Date: Sat, 28 Oct 2023 19:55:54 +0000 Message-Id: <20231028195559.390407-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231028195559.390407-1-adhemerval.zanella@linaro.org> References: <20231028195559.390407-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 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+patchwork=sourceware.org@sourceware.org Check if kernel supports prctl (PR_SET_VMA, PR_SET_VMA_ANON_NAME, ...). --- support/Makefile | 1 + support/support.h | 4 +++ support/support_set_vma_name.c | 49 ++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 support/support_set_vma_name.c diff --git a/support/Makefile b/support/Makefile index 917a858bd1..54a8cc30ac 100644 --- a/support/Makefile +++ b/support/Makefile @@ -80,6 +80,7 @@ libsupport-routines = \ support_select_modifies_timeout \ support_select_normalizes_timeout \ support_set_small_thread_stack_size \ + support_set_vma_name \ support_shared_allocate \ support_small_stack_thread_attribute \ support_socket_so_timestamp_time64 \ diff --git a/support/support.h b/support/support.h index b7f76bf080..659ef25b06 100644 --- a/support/support.h +++ b/support/support.h @@ -235,6 +235,10 @@ void support_stack_free (struct support_stack *stack); The returned value is the lowest file descriptor number. */ int support_open_dev_null_range (int num, int flags, mode_t mode); + +/* Check if kernel supports set VMA range name. */ +extern bool support_set_vma_name (void); + __END_DECLS #endif /* SUPPORT_H */ diff --git a/support/support_set_vma_name.c b/support/support_set_vma_name.c new file mode 100644 index 0000000000..41f5dea409 --- /dev/null +++ b/support/support_set_vma_name.c @@ -0,0 +1,49 @@ +/* Check if kernel supports set VMA range name. + Copyright (C) 2023 Free Software Foundation, Inc. + 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 + . */ + +#include +#include +#include +#include +#ifdef __linux__ +# include +#endif + +bool +support_set_vma_name (void) +{ +#ifdef __linux__ + size_t size = sysconf (_SC_PAGESIZE); + if (size == -1) + FAIL_EXIT1 ("sysconf (_SC_PAGESIZE): %m\n"); + + void *vma = xmmap (0, + size, + PROT_NONE, + MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, + -1); + + int r = prctl (PR_SET_VMA, PR_SET_VMA_ANON_NAME, vma, size, "vmaname"); + + xmunmap (vma, size); + + return r == 0; +#else + return false; +#endif +} From patchwork Sat Oct 28 19:55:55 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: 78660 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 15B95386480A for ; Sat, 28 Oct 2023 19:56:58 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id 1A6FD38618E5 for ; Sat, 28 Oct 2023 19:56:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1A6FD38618E5 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 1A6FD38618E5 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::331 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522978; cv=none; b=xQQIzacnD6eO5efvQVPgRGHVgnVVPZJjWnqX5NWQlnJhQ9ryn/WUA9rJKRNeKx1STrQCpLMU+m1NbsE7A5cuSd4xdpNCeC2jj1IOcUpHSKLGGxU+ozh2vjqbOga+snlOe9UCJlcchMy+2yi8RnVQ3im5lvDoyOz52JDpJcO0lug= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522978; c=relaxed/simple; bh=jTwbbxK6coyBuUNDI/1hQ7nwGzS5N7M/TZQfOn2Dvtc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Zyr/Wc0oaaLelCcX6c+t3lcToG08DUWeUjHKZYyG60J3qP8LTSmJ1h/9C17JwNd1TmIF6gJCQHhJ3slaFic6JmB4ymwr7WefiDjfLOLc0LE2NS5SCDp5l7WrB4Tl6DIWXa9zq0hEzeNEiZjid0ET1GPft05aAmiY6BMOBXZmxUs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x331.google.com with SMTP id 46e09a7af769-6ce2988d62eso2121090a34.1 for ; Sat, 28 Oct 2023 12:56:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698522974; x=1699127774; 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=utSWdvQl98iM9LBNL1svLmWLEXYk5MyyPql06V1kCt4=; b=Z/mWilEOvJgjQaYqesm1KqpjHOs2WUFny1HPTGyFkIR4jBQjrc2ebMYwDxf6yEhcWd 6ZOcxnIxdL5aQm17wvCkvr1eVbmRomgPS9Y7ZtCE6l3LlOi9+6lWZ1+3eNXUSv+5kceQ P+ZwTsC2qdBAM5PyeVdZfE6cif097wI/DwOedq5eHSH+Ka/Q3aKNOLcUGhGEFKWcuMNp JCSRlz1yYRv7upISgaQMh/momEQv+mjkz/daad/bS+wiO1NQPqoRPaiZ2kRRqrtCv/yn QWhzsUF3J62eZDKufU4tb1cIfYQj+bEsXogZfmWfGgv0ucGDSINnm6Aap+JIAzUCBSFy JkBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698522974; x=1699127774; 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=utSWdvQl98iM9LBNL1svLmWLEXYk5MyyPql06V1kCt4=; b=QIucnYOVxixX4plLxm9+gYN4xEjh/Prl0aBtQYGzDCy2EFkqFEuCrvp4q+RJFqxSy7 Xh7+fBFQUR4LsLkKXyRGFyhxdJaX4d1im8ORh60a8H1nsSRh+cpldD9zjn3miDcqCrhy irlDmk7H685+f4YH+SJqhzqqQ2KQvKA1NnBpzRiX3JeP5RibPFH9IMnY44SFCdN3J/JN STB//3FMYWF6/BphCN/tzHspivBRvWuukNKyNRx9XH2r+tYql4qdctW9DSn+FqKQV+6P YWyji3h/aRXFPYySdaxnHl+nbPFlv/iGOA+L0Uwz8R185uu/7fTujwZxJDHIktSQsKBU iUTg== X-Gm-Message-State: AOJu0YzJlSmCu7SqN79NWB6cCtqBMnDMD9f9yyHvlsi5ysWYpwqIb1So Ki7a3mm+UYK9jtysVL78drr23qK+xfFKqrzbBjjZiorB X-Google-Smtp-Source: AGHT+IEZSCK6DaLHr5YzllA6ecnvKAveseoq3vaA5roQcqNAQXkOFPr6lAwcSdqu0whJ+Q1m3tXmoA== X-Received: by 2002:a9d:7499:0:b0:6b9:1917:b2f3 with SMTP id t25-20020a9d7499000000b006b91917b2f3mr6407618otk.33.1698522974638; Sat, 28 Oct 2023 12:56:14 -0700 (PDT) Received: from localhost.localdomain ([139.178.84.207]) by smtp.gmail.com with ESMTPSA id w20-20020a056830411400b006b8c87551e8sm763181ott.35.2023.10.28.12.56.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 12:56:13 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Ian Rogers , Francesco Nigro , Carlos O'Donell , Siddhesh Poyarekar Subject: [PATCH v2 3/7] nptl: Decorate thread stack on pthread_create Date: Sat, 28 Oct 2023 19:55:55 +0000 Message-Id: <20231028195559.390407-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231028195559.390407-1-adhemerval.zanella@linaro.org> References: <20231028195559.390407-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 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+patchwork=sourceware.org@sourceware.org Linux 4.5 removed thread stack annotations due to the complexity of computing them [1], and Linux added PR_SET_VMA_ANON_NAME on 5.17 as a way to name anonymous virtual memory areas. This patch adds decoration on the stack created and used by pthread_create, for glibc crated thread stack the /proc/self/maps will now show: [anon: glibc: pthread stack: ] And for user-provided stacks: [anon: glibc: pthread user stack: ] The guard page is not decorated, and the mapping name is cleared when the thread finishes its execution (so the cached stack does not have any name associated). Co-authored-by: Ian Rogers Checked on x86_64-linux-gnu aarch64 aarch64-linux-gnu. [1] https://github.com/torvalds/linux/commit/65376df582174ffcec9e6471bf5b0dd79ba05e4a Reviewed-by: DJ Delorie --- elf/Makefile | 3 + elf/tst-decorate-maps.c | 160 ++++++++++++++++++++++++++++++++++++++++ nptl/allocatestack.c | 40 ++++++++++ nptl/pthread_create.c | 6 ++ 4 files changed, 209 insertions(+) create mode 100644 elf/tst-decorate-maps.c diff --git a/elf/Makefile b/elf/Makefile index 9176cbf1e3..a82590703c 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -303,6 +303,7 @@ tests := \ tst-array4 \ tst-array5 \ tst-auxv \ + tst-decorate-maps \ tst-dl-hash \ tst-leaks1 \ tst-stringtable \ @@ -3018,3 +3019,5 @@ LDFLAGS-tst-dlclose-lazy-mod1.so = -Wl,-z,lazy,--no-as-needed $(objpfx)tst-dlclose-lazy-mod1.so: $(objpfx)tst-dlclose-lazy-mod2.so $(objpfx)tst-dlclose-lazy.out: \ $(objpfx)tst-dlclose-lazy-mod1.so $(objpfx)tst-dlclose-lazy-mod2.so + +$(objpfx)tst-decorate-maps: $(shared-thread-library) diff --git a/elf/tst-decorate-maps.c b/elf/tst-decorate-maps.c new file mode 100644 index 0000000000..bbb7972094 --- /dev/null +++ b/elf/tst-decorate-maps.c @@ -0,0 +1,160 @@ +/* Check the VMA name decoration. + Copyright (C) 2023 Free Software Foundation, Inc. + 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef MAP_STACK +# define MAP_STACK 0 +#endif + +static pthread_barrier_t b; + +static void * +tf (void *closure) +{ + /* 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); + + return NULL; +} + +struct proc_maps_t +{ + int n_def_threads; + int n_user_threads; +}; + +static struct proc_maps_t +read_proc_maps (void) +{ + if (test_verbose) + printf ("=== print process %jd memory mapping ===\n", + (intmax_t) getpid ()); + struct proc_maps_t r = { 0 }; + + FILE *f = xfopen ("/proc/self/maps", "r"); + char *line = NULL; + size_t line_len = 0; + while (xgetline (&line, &line_len, f)) + { + if (test_verbose) + printf ("%s", line); + if (strstr (line, "[anon: glibc: pthread stack:") != NULL) + r.n_def_threads++; + else if (strstr (line, "[anon: glibc: pthread user stack:") != NULL) + r.n_user_threads++; + } + free (line); + xfclose (f); + + if (test_verbose) + printf ("===\n"); + return r; +} + +static void +do_test_threads (bool set_guard) +{ + enum + { + num_def_threads = 8, + num_user_threads = 2, + num_threads = num_def_threads + num_user_threads, + }; + + xpthread_barrier_init (&b, NULL, num_threads + 1); + + pthread_t thr[num_threads]; + { + int i = 0; + for (; i < num_threads - num_user_threads; i++) + { + pthread_attr_t attr; + xpthread_attr_init (&attr); + /* The guard page is not annotated. */ + if (!set_guard) + xpthread_attr_setguardsize (&attr, 0); + thr[i] = xpthread_create (&attr, tf, NULL); + xpthread_attr_destroy (&attr); + } + for (; i < num_threads; i++) + { + pthread_attr_t attr; + xpthread_attr_init (&attr); + size_t stacksize = support_small_thread_stack_size (); + void *stack = xmmap (0, + stacksize, + PROT_READ | PROT_WRITE, + MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, + -1); + xpthread_attr_setstack (&attr, stack, stacksize); + if (!set_guard) + xpthread_attr_setguardsize (&attr, 0); + thr[i] = xpthread_create (&attr, tf, NULL); + xpthread_attr_destroy (&attr); + } + } + + /* Wait all threads to finshed statup and stack allocation. */ + xpthread_barrier_wait (&b); + + { + 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); + } + + /* Let the threads finish. */ + xpthread_barrier_wait (&b); + + for (int i = 0; i < num_threads; i++) + xpthread_join (thr[i]); + + { + struct proc_maps_t r = read_proc_maps (); + TEST_COMPARE (r.n_def_threads, 0); + TEST_COMPARE (r.n_user_threads, 0); + } +} + +static int +do_test (void) +{ + support_need_proc ("Reads /proc/self/maps to get stack names."); + + if (!support_set_vma_name ()) + FAIL_UNSUPPORTED ("kernel does not support PR_SET_VMA_ANON_NAME"); + + do_test_threads (false); + do_test_threads (true); + + return 0; +} + +#include diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c index f9d8cdfd08..97d0efec10 100644 --- a/nptl/allocatestack.c +++ b/nptl/allocatestack.c @@ -33,6 +33,8 @@ #include #include #include +#include +#include /* Default alignment of stack. */ #ifndef STACK_ALIGN @@ -577,3 +579,41 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp, return 0; } + +/* Maximum supported name from initial kernel support, not exported + by user API. */ +#define ANON_VMA_NAME_MAX_LEN 80 + +#define SET_STACK_NAME(__prefix, __stack, __stacksize, __tid) \ + ({ \ + char __stack_name[sizeof (__prefix) + \ + INT_BUFSIZE_BOUND (unsigned int)]; \ + _Static_assert (sizeof __stack_name <= ANON_VMA_NAME_MAX_LEN, \ + "VMA name size larger than maximum supported"); \ + __snprintf (__stack_name, sizeof (__stack_name), __prefix "%u", \ + (unsigned int) __tid); \ + __set_vma_name (__stack, __stacksize, __stack_name); \ + }) + +/* Add or remove an associated name to the PD VMA stack. */ +static void +name_stack_maps (struct pthread *pd, bool set) +{ +#if _STACK_GROWS_DOWN && !defined(NEED_SEPARATE_REGISTER_STACK) + void *stack = pd->stackblock + pd->guardsize; +#else + void *stack = pd->stackblock; +#endif + size_t stacksize = pd->stackblock_size - pd->guardsize; + + if (!set) + __set_vma_name (stack, stacksize, NULL); + else + { + unsigned int tid = pd->tid; + if (pd->user_stack) + SET_STACK_NAME (" glibc: pthread user stack: ", stack, stacksize, tid); + else + SET_STACK_NAME (" glibc: pthread stack: ", stack, stacksize, tid); + } +} diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index 6a41d50109..63cb684f04 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -369,6 +369,9 @@ start_thread (void *arg) /* Initialize pointers to locale data. */ __ctype_init (); + /* Name the thread stack if kernel supports it. */ + name_stack_maps (pd, true); + /* Register rseq TLS to the kernel. */ { bool do_rseq = THREAD_GETMEM (pd, flags) & ATTR_FLAG_DO_RSEQ; @@ -571,6 +574,9 @@ start_thread (void *arg) /* Free the TCB. */ __nptl_free_tcb (pd); + /* Remove the associated name from the thread stack. */ + name_stack_maps (pd, false); + out: /* We cannot call '_exit' here. '_exit' will terminate the process. From patchwork Sat Oct 28 19:55:56 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: 78658 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A74D2386D616 for ; Sat, 28 Oct 2023 19:56:48 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by sourceware.org (Postfix) with ESMTPS id 907C638618D2 for ; Sat, 28 Oct 2023 19:56:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 907C638618D2 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 907C638618D2 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::32c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522979; cv=none; b=VpBvUTAProAuxACSXYdKXZb0MOOjaZQNpf5aIrskBhinRHawkdKAwykkWobH7/sp1gXrlJAkVI4Y2agK9OQekMx770qpPMHgTZVufGrQVVa/yathZKKBAuGxGG7mtOXBQu/KZudwf50817Asvw6h7Wgr/81fFsXCnSozU+I5rvo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522979; c=relaxed/simple; bh=Tmelg8G4qrvZMsEEzhIEKtkWU/WwkAQIHBhTdfwfQZw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=LbFQmYmhPMrNWjByvGyeaEuSbF7ZhXvqe30fotaIXV/Kp+HCI1x5t5goDK+L2UR4y3oXbnq76hLoK3HihlDSDUpqNAOlEY1ddVOH0eLxi7bJO0ImXbB4auiwi8KAd4kHp0dEg9+7UFWWKO88i0XWqnN/pyM7PY/YkoDmY42W1t4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x32c.google.com with SMTP id 46e09a7af769-6ce2add34c9so2094615a34.1 for ; Sat, 28 Oct 2023 12:56:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698522976; x=1699127776; 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=ywSBRkURYEseda3clNl6LU+acfWrILy5o5PtEufvb+k=; b=BeSmy+8JZRS4GRLnrUdQb464I8xjusKvNk9mF7oQeGUkPjeti9ASfWyD5ONDKfE8D6 aCN72ZTBpPHk1iFEyrIRo9FVdixn8M93WxPm4SOgtuMml5Ueh88uBvPOD+Vs9HhOv8kV IZns1tIxU46hKB22YvPOLumjdb+a+6Xa4JdhYgPpjt1TzwMLhIw3Znkglrw7j3pvNiHY iCxf0L3H+G6OCZ/KW2c/0JFg7hu81dGcObWz1QD+XJW8QOW5Z6X/IUBRYAZNY1Rcqg71 AesbPqHtnIDcYFFppFhaU47xXhuUO2V3Ag/DHICH9lQCYZ9gQ37/TWDpr58ISIZW85Ae l+nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698522976; x=1699127776; 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=ywSBRkURYEseda3clNl6LU+acfWrILy5o5PtEufvb+k=; b=lbdhpsR8/xN81Hn2RSfS3yUqkD3nEE0sV86PMVyY6fh/30/Ae80+OBfFzFUhE2Sep9 BvemhzDcoNvNBHH/CAbJ/7dqA9feVSfLANncUy2qvexQdeP8gAJHKByP5gM3zAYtFweR gRS7ZJOoTfceytgm6z8Lf1OX9uCitteWTPb3s0VCC9XsZLJO6foDzjj8lRHCiH5USRZs UsCf/l4VBBdeJA6UjNcAzBS5BKFk72KMzxjKMcUU6r4LNw5CLuOoqsUdRtuxDXxAsVVC q9EeLsjv9VTEKiOJleHpB4CNe7Va22r+EBz8VN8e+ttWOg06e2EaccMX0QPcMpps9O3/ 4NCA== X-Gm-Message-State: AOJu0YzysRgILtpv/V/I+d4PLa35FCyBerqnJqVP+wO0ctrtwylNe/+V 3Waj/KUsbLSF34SRC67CWES6uH5kH3ZJ8Fps8pErrdxo X-Google-Smtp-Source: AGHT+IFCH7lv5cLQ1/Ni/LnS1nkhjhNDBO3tsuya/v5GA9Lp2QZeL/IeUtoOlfYimwG5OGfReoLE7A== X-Received: by 2002:a05:6830:3494:b0:6d2:eb63:e47b with SMTP id c20-20020a056830349400b006d2eb63e47bmr2619572otu.2.1698522976367; Sat, 28 Oct 2023 12:56:16 -0700 (PDT) Received: from localhost.localdomain ([139.178.84.207]) by smtp.gmail.com with ESMTPSA id w20-20020a056830411400b006b8c87551e8sm763181ott.35.2023.10.28.12.56.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 12:56:14 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Ian Rogers , Francesco Nigro , Carlos O'Donell , Siddhesh Poyarekar Subject: [PATCH v2 4/7] malloc: Decorate malloc maps Date: Sat, 28 Oct 2023 19:55:56 +0000 Message-Id: <20231028195559.390407-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231028195559.390407-1-adhemerval.zanella@linaro.org> References: <20231028195559.390407-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 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+patchwork=sourceware.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 | 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 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)),) From patchwork Sat Oct 28 19:55:57 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: 78659 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2B70D384601B for ; Sat, 28 Oct 2023 19:56:57 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x330.google.com (mail-ot1-x330.google.com [IPv6:2607:f8b0:4864:20::330]) by sourceware.org (Postfix) with ESMTPS id 1BAA038618FE for ; Sat, 28 Oct 2023 19:56:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1BAA038618FE 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 1BAA038618FE Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::330 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522980; cv=none; b=AMPEyZCLZjOAWNZsArQhrRCPSXO7fA+S1DbS6hzX6Rmpu5ejQ5xhu+p+jxQqWp2B52xSdM096MnVlbb818pjTyW2gnJ6VH2DDqcxw1DpKkeoSbj+QL0Vu227gUl1q6MjJC6mv2RgH8EnQfpGLcT4Xo6b2G8kh1W+4Sga7jgJXrs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522980; c=relaxed/simple; bh=7IU+K49IxS1kn/ocnc54WSjSZ3Q/mkS/5hQqscvWxMA=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=DitkTvereUPge3aQs8F0t5+8+U1GL50iz5Icu90xnPh0BWhPD+etsuG0j38hHoKRAd42vwPCli7c1tx44k6KinNJV0W1keevdihpMDIVPU2+dd7bebnPo7bRA5RE3Jn3yud03ircdlGyTNo2bmqWIUS6nvLy3oXsXqa6GzNmesA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x330.google.com with SMTP id 46e09a7af769-6ce37683cf6so2158548a34.3 for ; Sat, 28 Oct 2023 12:56:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698522978; x=1699127778; 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=xLM6XTFrju2h0P2Jp6D87i92a5MTlqzEcCSkMAXExHA=; b=hwx16VM/fmtrBYX/Kp95GcQsHdMzdKqcjnpAi/NMbuuasaJueHd4GzEjr1dxOQuT44 NKtvaiz7eLmNhwh4JrYyYinCTbywAjjlRzfRPBnysrhmTYvPmpfv0IpjK2YJHbMtc+0H HtXV53pbBfo/1lgGia56+ObyyWPfnubTMEHZ0kDm2BY/jDxZ0iBRvE8ziQ39LeQhiq9+ LXEp4fAuLbqt2ZBc0MzvZSs7ZioscbfeVtociAJUvyxmbntF+vRKQRHhOERX3m2Ziouh sKarKkEAO/X4nwPia/zKONogXXoUwS5DvOUZ68U4sR5inTb0PoKJMCnTU01BSo1ppeIx uLJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698522978; x=1699127778; 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=xLM6XTFrju2h0P2Jp6D87i92a5MTlqzEcCSkMAXExHA=; b=rB+QcrlKzPWGewjJo3Y6j3y9q+xRtKDwg/IPjWZIdrBWwmSOmSVq32DdYs5k/a8KEW 9jgMD+SHNOOePk7mrxm2vGhrradLQJjYZtZ5oSDm7Zsx3WUsVMZWxgeEQOGBcwP09HKu wxel2l5QD8q8x2fdul+KwYps4VzZiYySRvC7sZ3nnmAsvruEj7dAtYxU1d8lqH0jSRDc AVdPBiRvABva3/DWG16a+kVcIMyKmwGDWL31aR4aysnYR64sjXlqOr8HGR2TKjcIfEx2 xP1BHAn+hVyH8//BvGUCGRJooq10qNAcbueZqBr1E5Txtxf+9CMHuhMn2Y13DzAVml6L 8S7g== X-Gm-Message-State: AOJu0YxvNCKAAWOjcKS4tan79NyHHSJ715x8MqMd77Am++eS7cCCnHvL IFbBIGGU+73XQtKhRLhxyCoCCUq2TYszMaK/vgH3j9KD X-Google-Smtp-Source: AGHT+IHos1vogaiKtbZJETGF2CGO4xcSDfqesSCgKANe+Y1FpDFRelrXIf7s4SzkWmClN0iEmkGykA== X-Received: by 2002:a05:6830:7195:b0:6c6:50fb:cd0b with SMTP id el21-20020a056830719500b006c650fbcd0bmr8250100otb.6.1698522977750; Sat, 28 Oct 2023 12:56:17 -0700 (PDT) Received: from localhost.localdomain ([139.178.84.207]) by smtp.gmail.com with ESMTPSA id w20-20020a056830411400b006b8c87551e8sm763181ott.35.2023.10.28.12.56.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 12:56:16 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Ian Rogers , Francesco Nigro , Carlos O'Donell , Siddhesh Poyarekar Subject: [PATCH v2 5/7] assert: Decorate error message buffer Date: Sat, 28 Oct 2023 19:55:57 +0000 Message-Id: <20231028195559.390407-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231028195559.390407-1-adhemerval.zanella@linaro.org> References: <20231028195559.390407-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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+patchwork=sourceware.org@sourceware.org --- assert/assert.c | 2 ++ 1 file changed, 2 insertions(+) Reviewed-by: DJ Delorie diff --git a/assert/assert.c b/assert/assert.c index b7c7a4a1ba..bf0f4a69f5 100644 --- a/assert/assert.c +++ b/assert/assert.c @@ -24,6 +24,7 @@ #include #include #include +#include extern const char *__progname; @@ -71,6 +72,7 @@ __assert_fail_base (const char *fmt, const char *assertion, const char *file, { buf->size = total; strcpy (buf->msg, str); + __set_vma_name (buf, total, " glibc: assert"); /* We have to free the old buffer since the application might catch the SIGABRT signal. */ From patchwork Sat Oct 28 19:55:58 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: 78661 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6069F386546F for ; Sat, 28 Oct 2023 19:57:04 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2b.google.com (mail-oa1-x2b.google.com [IPv6:2001:4860:4864:20::2b]) by sourceware.org (Postfix) with ESMTPS id 0C01538618DA for ; Sat, 28 Oct 2023 19:56:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0C01538618DA 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 0C01538618DA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:4860:4864:20::2b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522982; cv=none; b=UKNm7G3BU82/BDhdNzqOS25K/drznWUFJlDF8VUY2z1DNwRalb8ioSPQEskXi6Vi6txiaAsICoGtyG2Q+kKcpOW9uTx9s2yOKsCadW61AzRLLS73uHtqvlcopeUDjiUOOC2y2Sqg2naklE4HPaYbsB1R7l7dCafZYzT5ROVvu3c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522982; c=relaxed/simple; bh=ImCn8D9L4MIrZ9fskXjKjheSDp3oRb8wVhEnozuDrjc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=lbBTckSdOAbZzWhltM+D8jL1iabN9CgHLrA3R9eXJwMp5I1qtNPhm7twk9NMeQVHv9d1buctDAMQ1OIbNraEhPbKjSbVo6lI24PaXW4Sv6gWAfisDTDyhtS+irHMEMm2hlr7Fsml+M5W6qUSQEoX0AYUixbEGW+XuIrqMtMtq4o= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oa1-x2b.google.com with SMTP id 586e51a60fabf-1e58a522e41so2119785fac.2 for ; Sat, 28 Oct 2023 12:56:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698522979; x=1699127779; 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=xH2cPYi2ud62JL2Pj9p045VIjHtNXt5o/PabtWc7sl0=; b=RocCQFBYvs81IcHIptSqGn+9qMAO0lexBnk4QeiLDdgOJxl2twg9ZBo7+RFSxht7G+ 95K4qnYKuhpcJ8F2fE7e+d/Ic8oaaI+PZULM3xc9t7oyc2EPkVb3X8a7jcBM/IjRedC6 vmv102aiHhhV2CwcGop1umLnkDK7oEbTdjQzO8CLbyMgnD6mu/44bPMfzYmLV54k6AxP YnIC3GyNKF19OD095BNoDpZj7/DzUnUHer5dkqh9cZrB0B58j3GWD8suvT80lVvV5o7i FO4jJBDuXtFAnsG+tMdS/CT6ad8EN25PxAXC7Yq4UTyf/DnorzrdZR7MnTdVvQnnpdw7 tQBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698522979; x=1699127779; 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=xH2cPYi2ud62JL2Pj9p045VIjHtNXt5o/PabtWc7sl0=; b=sHuo/5OSWHX8a0Z1799Inumcvjis2W3z6BIuHEupBihmd6ntBMjyU1MKOlsF+sp8U8 CLMLzo7wj5/SdSGETQhCLZZINkpkSmjdmRr21qSH8i56Jjv4gVRf/u/cUaz65+ntPMz5 lBbooM+LSeEY0KilYaORvk5NTBrTxVXMKI7gys3SZrYBfmP8oZQ6e9gPpcKytqU5fyHQ izr/xhbPS2L7u5w/tg++TEF9+RXKBp1MRCNNsON9ETJbNH9xrhvN33FA32F566qrnDsJ iOkvyvFqgQ3sIOadTpn97zpOOwic6K5MAnKGAjtSealtdnA4WYc39oPINiD6UJpzxjR4 BlKQ== X-Gm-Message-State: AOJu0Yw5e3ztU6uwZYjO/UO0hlTqibXJX0RERyWcFSnriAUcGcCjyTRb xVGSJiySRkj2CIGcyfjxURUOuzoRlZrfTbg35LnG0Gfs X-Google-Smtp-Source: AGHT+IFkMxal+NgPI+mnGukojmLxqPtQjhCsas4+/RqQNm0AmRM++kTfS2ir58Vumx6M6eseuFSMtw== X-Received: by 2002:a05:6870:8188:b0:1e9:b2fe:9ea7 with SMTP id k8-20020a056870818800b001e9b2fe9ea7mr7880782oae.43.1698522979425; Sat, 28 Oct 2023 12:56:19 -0700 (PDT) Received: from localhost.localdomain ([139.178.84.207]) by smtp.gmail.com with ESMTPSA id w20-20020a056830411400b006b8c87551e8sm763181ott.35.2023.10.28.12.56.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 12:56:17 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Ian Rogers , Francesco Nigro , Carlos O'Donell , Siddhesh Poyarekar Subject: [PATCH v2 6/7] linux: Decorate __libc_fatal error buffer Date: Sat, 28 Oct 2023 19:55:58 +0000 Message-Id: <20231028195559.390407-7-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231028195559.390407-1-adhemerval.zanella@linaro.org> References: <20231028195559.390407-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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+patchwork=sourceware.org@sourceware.org --- sysdeps/posix/libc_fatal.c | 3 +++ 1 file changed, 3 insertions(+) Reviewed-by: DJ Delorie diff --git a/sysdeps/posix/libc_fatal.c b/sysdeps/posix/libc_fatal.c index cf28387ee6..f564d232bf 100644 --- a/sysdeps/posix/libc_fatal.c +++ b/sysdeps/posix/libc_fatal.c @@ -31,6 +31,7 @@ #include #include #include +#include #ifdef FATAL_PREPARE_INCLUDE #include FATAL_PREPARE_INCLUDE @@ -116,6 +117,8 @@ __libc_message_impl (const char *fmt, ...) wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len); *wp = '\0'; + __set_vma_name (buf, total, " glibc: fatal"); + /* We have to free the old buffer since the application might catch the SIGABRT signal. */ struct abort_msg_s *old = atomic_exchange_acquire (&__abort_msg, From patchwork Sat Oct 28 19:55:59 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: 78662 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 573823861885 for ; Sat, 28 Oct 2023 19:57:20 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id 8BD1138618E1 for ; Sat, 28 Oct 2023 19:56:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8BD1138618E1 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 8BD1138618E1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::32d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522984; cv=none; b=M5UcDjMlwUsYuoIS6IftfIe6klnodMGkw2H73CaO6u1qwsUmz3qupVMnnbJo3VrJpTmvlPDF34dHOd7Wv9r2yTZlpllWQd5ycTy53LZGxIR2Y8IE5WsttPwhkKAH9F8nSOAaSD8Di1ebnDBFE/+eSz47AxGmiadsUy1QYbHICNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522984; c=relaxed/simple; bh=54XFVv6yTgUxeZ6DeSgmQxu0PKDIKjfP/KDiGrUfBLM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=jdCXj+KcPA6elAa4ynQkR2SFtBnOV79eVcn536ckgOnASWWAZBPb5XTk+DbZEWmL9zKWFeHvsM+XDeG2jphRp+kqjx5IqWaXNlEQMmAMZFCt0nz2x7aq174NPSoeBMzc7fa3Y4TFgKNprwCN2Egu6L2uJW9lsVVKt5Szp5OR0TI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x32d.google.com with SMTP id 46e09a7af769-6ce327458a6so1631300a34.1 for ; Sat, 28 Oct 2023 12:56:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698522981; x=1699127781; 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=WlH95tVNYVT/bq+I1M60U0Pco+sIKS/SJrorYGSac1k=; b=ExAGM1HWrfikSiQClfCF9Wg+Skt1niDyKbM55mnKCPiPWweMCNAMY9n8lJqGxsX4yr I6qagUZ4v1V2em4CL0hI/uNFdkGKrhPtjaWD1Lc9vvTZTQgx4JLhDZB2ZADRpcjiPoBQ rGlkAFI7UDgIOuqS4sFH3uQVuL1frBsz7PTYqbJYRaFHFCXIwogCADVA3qrFG7fwG7AO P1knIdh85uP16OYuKPWaU0igiJ38/9J+PX8mC8CwffDndB218C7fpCmN59gJwlbvmUBL zlTY7A5u2p7P/c/9urUmBxKL2WmmYYn98QNZJiL+Uh5WZ4T0c3ORnrFaP+3/Dfgnxhhe dBEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698522981; x=1699127781; 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=WlH95tVNYVT/bq+I1M60U0Pco+sIKS/SJrorYGSac1k=; b=u1hG3TwfASTBi/amlCDG2peDmcjB8GluNoCNsRqP2nnb6fi3hJePq1XXlCKX3hzrRF nUUv2MJ6TgBJzo9VnujCtSrWuYsyEU5Yzo4iemhdsyCDyGsnWhbnGqGw0lEe3jghaP2K KmD5+BA4FMWbGybXLEQoNt9ds6gPwRWV71SBWStSWMiZZaerPG7QJQFQ7fuqalls1mCE KWusetj4bvCm0rtokNC2ql80SRVYbyGutIdbD6VXiIZT9dko5emDLxJS1YcXVT77nygx 2vVlOXgnF8cILxCjtzbtmkC3hN0aVOkce/eZSgeob2TtPQcRAZVDwaURhQVdLu5AOeG6 Hq+w== X-Gm-Message-State: AOJu0YxXOD5OG5nV+u2cDEzgDTOLf9H4W95IY/9Awl19eXV0bdlnfoNC wqbs4kSz6vxF6A9fRcNUQz8vqM+9Sz1VrHWWrqzG7JlQ X-Google-Smtp-Source: AGHT+IEEdhuKHIvNQlr4Qbner39U4Wir7hIfCNhW89e/9M1FkK+HHlQFLdkBdagmg0DQsNdQv4im5Q== X-Received: by 2002:a05:6830:1550:b0:6b4:4ee1:b525 with SMTP id l16-20020a056830155000b006b44ee1b525mr3231663otp.1.1698522981219; Sat, 28 Oct 2023 12:56:21 -0700 (PDT) Received: from localhost.localdomain ([139.178.84.207]) by smtp.gmail.com with ESMTPSA id w20-20020a056830411400b006b8c87551e8sm763181ott.35.2023.10.28.12.56.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 12:56:19 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Ian Rogers , Francesco Nigro , Carlos O'Donell , Siddhesh Poyarekar Subject: [PATCH v2 7/7] elf: Add glibc.mem.decorate_maps tunable Date: Sat, 28 Oct 2023 19:55:59 +0000 Message-Id: <20231028195559.390407-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231028195559.390407-1-adhemerval.zanella@linaro.org> References: <20231028195559.390407-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 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+patchwork=sourceware.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 | 12 ++++++++++++ sysdeps/unix/sysv/linux/setvmaname.c | 4 ++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 9432564444..67f6e63b14 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 c3cf63a443..2603b90961 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -3023,5 +3023,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..2a5ad73e9d 100644 --- a/manual/tunables.texi +++ b/manual/tunables.texi @@ -653,6 +653,18 @@ 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), 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}). + +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..62237fcf7c 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. @@ -30,6 +31,9 @@ void __set_vma_name (void *start, size_t len, const char *name) { + if (TUNABLE_GET (glibc, mem, decorate_maps, int32_t, NULL) == 0) + return; + static int prctl_supported = 1; if (atomic_load_relaxed (&prctl_supported) == 0) return;