From patchwork Wed Nov 1 12:56:05 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: 1857946 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=zVAiMoha; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4SL6TY1RXlz1yQm for ; Wed, 1 Nov 2023 23:56:49 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 24D5A3857017 for ; Wed, 1 Nov 2023 12:56:47 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by sourceware.org (Postfix) with ESMTPS id B82E23858C2B for ; Wed, 1 Nov 2023 12:56:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B82E23858C2B 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 B82E23858C2B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b33 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843391; cv=none; b=IZJYjNdawT+0EgNxawHZkirloHHLsHzu48dH/IhRHOU3Nbtnh743ylagCXsmvvHP7gPRXriKbHAdvC1OCXXiHE3Q0NoV3xY0c7WytGphhDmKMtBJ6GVDlzSQ3iRHXS/QEfcYZS+tNd+CGv0F395U/Mpg+3DZWBV0N+kL0oXKGmw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843391; c=relaxed/simple; bh=ISkbMjwVd7JsLOQkuM+rxcrTwsHkdMD8jRVD1FBUMjI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=lfr5w19LiH72OpkWjdV5ippa+xP20ALmHh5BwvQPNAzbkqAffuAJrA0RB1DAYfaMckHYS0Cs7eL5dStlaGD+bwdeuodyiwK7VbJewezaL6+1sAgVZ0rNkhkBh8YPbUiDRdVsunDgQjRb80aaiA9dML/6NnptE9l3kmH6llsaI8M= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb33.google.com with SMTP id 3f1490d57ef6-da2b9234a9fso4426885276.3 for ; Wed, 01 Nov 2023 05:56:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698843379; x=1699448179; 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=1F+2KIbgsk1fyfgMGRYp2xAq37K8oAJmUhZ6k/9SOHk=; b=zVAiMoha5wiVzT1aR+qb97tPHTvyBPs7/PfhI4tqzBFpWOpGCDB17kSPgGw60bLmf6 9/IBMPCPti0ChfIJS0hB79C8nTczwgueH+4I89ICb+8QSulylAZIciZR0lgixfgHtOK/ eYJmafB1RnqUMH4FT+l0+IbQR2BoWSGktNy4yPN8/y+DwXBnTvloBSgdEZ+nhg9tiDl+ Pida0aNcm7Tw44nUNqWFUrotOhk8xIqE+3P6iSYtBo12Hntn8noasqX0kX0anrweH0Mz lTYtfcyWj1PMac799nX/L9DAstVnYWxjBF04fFWTJJNLIIUWO/UiIdW28jt2PTB66R40 zhIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698843379; x=1699448179; 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=1F+2KIbgsk1fyfgMGRYp2xAq37K8oAJmUhZ6k/9SOHk=; b=F3HDJBTEObBPeVqUKXMxFWZ98jtIO4jwjgGYvesYfcoKxvNy0lAL2oWtrgiFuj2xxX 7QqDcM5Vu6Jp3yq6KHcO1u7xG+vsFUidMsMw0800wL7IW9akufK0v0gqcxBNS5fBkzlp ueVq42FyryAer8oMWpGpVDKqSov6kutKnyZf4chyAXjS+A4kmijJSMj8dMOUl/bdEL/W TcpfCO+PTyb4UGnah/12hy1TceMfa5tqpFCCJDkW2p9qkOtk22gTnqwSNfMu8e3d/SP8 lt1yfwW547B9qBZtED7l//SoDDx0ojlbDzyF6wroL7IxFSZoio/Mw/ChplS/+YlUqRSv IUcw== X-Gm-Message-State: AOJu0Yxrz3Q81Zp5UAFhTTTCc26uUGWFCPLmswOU4o2MnXbsrGUPriii /awmwMTp7s1hUL6Y3+Lmr3xf7wgqHd9XoHt8DIewkg== X-Google-Smtp-Source: AGHT+IELs3qKxTK3PVFhv+ZMWwNjwnRsK9GX2qeO7VgOM/zgxYBBiycKtZGdYR9NUlUcqAEyHk1ILA== X-Received: by 2002:a25:aa8b:0:b0:d9b:dae4:63fa with SMTP id t11-20020a25aa8b000000b00d9bdae463famr15834741ybi.34.1698843379397; Wed, 01 Nov 2023 05:56:19 -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.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 05:56:18 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, DJ Delorie , Ian Rogers , Francesco Nigro , Carlos O'Donell Subject: [PATCH v3 1/7] linux: Add PR_SET_VMA_ANON_NAME support Date: Wed, 1 Nov 2023 09:56:05 -0300 Message-Id: <20231101125611.208544-2-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.3 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+incoming=patchwork.ozlabs.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 Wed Nov 1 12:56:06 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: 1857945 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=B8w5a/px; 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 4SL6TN5clmz1yQm for ; Wed, 1 Nov 2023 23:56:40 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D10F43857806 for ; Wed, 1 Nov 2023 12:56:38 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb36.google.com (mail-yb1-xb36.google.com [IPv6:2607:f8b0:4864:20::b36]) by sourceware.org (Postfix) with ESMTPS id F02F3385840F for ; Wed, 1 Nov 2023 12:56:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F02F3385840F 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 F02F3385840F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b36 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843391; cv=none; b=tKcNHSYq0F+ugpNtR7CSQlXJ3ZayWi87ZhI0pbtcFcC6LsMgMhBDQtzxJkj9XJZ/UOyE1YIhv3DQocQR6OTURzCP6+wulne1xEjS+cahd0Vi99xT/B/UAgSdBvhc8xbOXKRAejiyBPotL995PqjvVZkVLE5UbH8L9q2NlY9M/Rw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843391; c=relaxed/simple; bh=WG53seEKpuXvBhS6R/4bsY7sK1gYeZpD7QzufqQ+Lj4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=fiv5oEBfEsNiW/oimwxp/bG3nKpdasrDBE4PoGoLSLg6aBME9MBsTn3dh2JpDM05lLnxIGs+KfiJA7lvZeyHCUWB+0bwKmaOdJmUkLNDh8Em+W558GEpLfDlX4WbOyOzCJJJe20POPkj4hrJO5p4AurwAnuLCLVeop1BW4NZENI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb36.google.com with SMTP id 3f1490d57ef6-d865854ef96so6198113276.2 for ; Wed, 01 Nov 2023 05:56:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698843382; x=1699448182; 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=3a7I+lue/0gnA5BTyHrSEIRxoZIlLTWh+LPA0Yf1gsg=; b=B8w5a/pxfWiwRwDRo0lCr0Z8IEPhBksQdOvDlwY1uCPBiQOWmXMzSM0hAGiUl6I5ps /VZdFgsweXyGdbc7O2C8e81anybGvCzHeJ6nrPsP35Xl0hGZtXMS0WguDO7fqg2lK7q3 OHprOq8vXizm8KxDShjjTgo8Txnd90pfrzhJaR62wVPKswxbcl9MAq4hCdNisKUpFUf9 DTNZSFMz5DZkHr2Tei/rb4ZraLiej4BWtsQGdjzHxzM3CDv/cbJPu5ehdcm3vwuyxjuj Thwvrnzg+asjiYtqpo8iXQXY5pRXG4cHsCnwpLT4CRWlysJqPuNx9U6veGh8vOlfFwqd ALKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698843382; x=1699448182; 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=3a7I+lue/0gnA5BTyHrSEIRxoZIlLTWh+LPA0Yf1gsg=; b=TgibRKO8EjEJEz3jArOKjW5uK5XZ4kbJ1W0UXJm0y6Bskol87u8OrUHgsDrvMcbb8R xDmgMUzyMKSgxxf9f5Sgww4b0hHzhuFnanoELKecQIw9xBLwIQMMBKvGq9GcVvkVPyr3 afxTQ5Z4X34VG69VRPfwhGYr35eZYJpK4QURbIOvZ7dm/8pmnsaoJRiGpMf0gebrfBeR b7LeeCIoqXOIi0fvRDLeeKKKfspskTmnAATH6Pcvor8PckaLrBrw3Gb8kO6JXrxADskk z0N30edK0ht/OK7ODPuv9kulL0pDfVVT6KQnOqZkeqjKM4WdpAcXa9f2jF8MFHrRRPaF rHKQ== X-Gm-Message-State: AOJu0YyZWsThItoWqw6IBH2VP2BTAc/LsXMkJDlYVSKZVdWgmcZ1sw+D 06NkFvRmXDeuIOtmdnvjdN4oRg92kQYBDa8cgPp9Tw== X-Google-Smtp-Source: AGHT+IG/667P7Twed3Fl1OluNs+RqfDGxU7HvKJIAhbtxEH3jGkOUXun3EZ2zo/R6QiKmYW/a3TF9g== X-Received: by 2002:a25:804f:0:b0:d9a:3d70:db36 with SMTP id a15-20020a25804f000000b00d9a3d70db36mr13380088ybn.42.1698843381801; Wed, 01 Nov 2023 05:56:21 -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.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 05:56:21 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, DJ Delorie , Ian Rogers , Francesco Nigro , Carlos O'Donell Subject: [PATCH v3 2/7] support: Add support_set_vma_name Date: Wed, 1 Nov 2023 09:56:06 -0300 Message-Id: <20231101125611.208544-3-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.0 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+incoming=patchwork.ozlabs.org@sourceware.org Check if kernel supports prctl (PR_SET_VMA, PR_SET_VMA_ANON_NAME, ...). Reviewed-by: DJ Delorie --- support/Makefile | 1 + support/support.h | 4 ++ support/support_set_vma_name_supported.c | 49 ++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 support/support_set_vma_name_supported.c diff --git a/support/Makefile b/support/Makefile index 917a858bd1..9aa7f23a6e 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_supported \ 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..4a068d3aee 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_supported (void); + __END_DECLS #endif /* SUPPORT_H */ diff --git a/support/support_set_vma_name_supported.c b/support/support_set_vma_name_supported.c new file mode 100644 index 0000000000..712caa9b9b --- /dev/null +++ b/support/support_set_vma_name_supported.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_supported (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 Wed Nov 1 12:56:07 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: 1857947 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=GXBPkv6d; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4SL6Tl4QNTz1yQm for ; Wed, 1 Nov 2023 23:56:59 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E2FB63856965 for ; Wed, 1 Nov 2023 12:56:56 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb29.google.com (mail-yb1-xb29.google.com [IPv6:2607:f8b0:4864:20::b29]) by sourceware.org (Postfix) with ESMTPS id 83D083858412 for ; Wed, 1 Nov 2023 12:56:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 83D083858412 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 83D083858412 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843395; cv=none; b=E/FjlSwco8HLf607nkZjTSBi7XhBtWs4TjhCAFLF/rnhD5nRlfrzQX0NKtVyFqC9NEnd6ffhB8+Hx5UhIcUHlBFiNaFahpFcQ/jlyGbM2klFaRKVqDd3RR8tAn8BKHB9NVXUbFcFI44uBTaLbvp+SYxWXuebT17Poq+eZSgDHdk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843395; c=relaxed/simple; bh=FO3ADlkLBQb15irFLg8Ohq9np5D2yI2q0MMZkaDWk7Y=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=eQVFfkgzptE2Czc1j7YdfulgBJ2/bi2/LFZgQ7rrpkmROJGYv/r3aMfl03jNYug4OLhmOBOccqrwLfEAW5bhLH8IVCWSWVRH2D+UZMONQkBCpFCu9KnVt5dFbY1xgJztydUJ7FkM2pq2qb4s5NXhfUxjTGyZaI60hs20b8PEyYU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb29.google.com with SMTP id 3f1490d57ef6-d9ad90e1038so6310173276.3 for ; Wed, 01 Nov 2023 05:56:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698843384; x=1699448184; 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=bKGTvDrs3EZm8QuGc3+yl+qhCuH5QjsUhkgltvYjiTs=; b=GXBPkv6dVVZtiQFIlUnTVBQyaUfPGRVdbjVLeD2vlyKc7Pbt39nXOGrVomfEGQR6q5 vbuZwq4XgLYIR+NfN26AaGrY3WJTNiYXAkbXDJckHvOSxbHJimtfXEFJ+fVVsO9DDjYW WmnO0WkrxjzG3QpChlwJ45GV3J7MmOEBg30b2FeDQspLq5XVJkA1E30sQ59qlkOUvfgp rS/mjSEAW4QOJsQV2FqwN6D0MwBufS/Drkb1/a2MKDFHVf3CJF/63eYrfd7tnNk/497b n9eYE8xBGYTSuX18wq/7TbRAqile1xdK47/mALRvxwwhdJGFrdtfjmTt9yK70vhUR/M5 fViw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698843384; x=1699448184; 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=bKGTvDrs3EZm8QuGc3+yl+qhCuH5QjsUhkgltvYjiTs=; b=hfyQEL0GQJ/jumnvGXfu+YtKVfMFe8301jYIMSdCpadYPkzmIyXRXtSGxBMX1DVbSV G+G8i9n9beYCTTrjz0RLncAJrAMhvQPFIbbu1Y3ZLTZbeEwHBzekvRb1CNIId1xMaSj2 wTJHpNbP3K9zDT3/wD8tqw3JUyx18+kqzVWWV8Y4Ji/fqpknqoX3axWAfMRCLVz9yC8M jQyhvi462zCsQdl8PdsjNxAxAcAN09+Z2FRu2eX8e3AIXQyQ+SuQkTAtvdGtPNXywrlG Xl1oruqeP147SI8LH1aOowmq1/0DN7hRawuHFRXia1lLvME19aRTfPvWljsK+twDThQZ epYg== X-Gm-Message-State: AOJu0Yxk4IPx1+dnd3euu0Ztd8lrzTh+d8rziiKVS9x5ciYm2/TsddOS Ak9r6CcWKvI1V8WQgLUIOkVfFLywm7SxgZw7utwaTg== X-Google-Smtp-Source: AGHT+IGDsOwUdQ4UkGsjdBXGDlu376cBocnzH032kTn6sOuVU2NXunRpzeWfjqAVwPewoIva0XC/MQ== X-Received: by 2002:a25:9805:0:b0:da0:29e2:d29 with SMTP id a5-20020a259805000000b00da029e20d29mr13213069ybo.42.1698843384204; Wed, 01 Nov 2023 05:56:24 -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.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 05:56:23 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, DJ Delorie , Ian Rogers , Francesco Nigro , Carlos O'Donell Subject: [PATCH v3 3/7] nptl: Decorate thread stack on pthread_create Date: Wed, 1 Nov 2023 09:56:07 -0300 Message-Id: <20231101125611.208544-4-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.4 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+incoming=patchwork.ozlabs.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). Checked on x86_64-linux-gnu aarch64 aarch64-linux-gnu. [1] https://github.com/torvalds/linux/commit/65376df582174ffcec9e6471bf5b0dd79ba05e4a Co-authored-by: Ian Rogers 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 b46dee1c16..33f88bae96 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 \ @@ -2980,3 +2981,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..b40a0e9b49 --- /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 mapping. */ + 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_supported ()) + 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 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: 1857948 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=EGR3oH7J; 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 4SL6V00YW7z1yQm for ; Wed, 1 Nov 2023 23:57:12 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1918D3857355 for ; Wed, 1 Nov 2023 12:57:10 +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-Google-Smtp-Source: AGHT+IFhcx9a//Ze0sq5CnBZj3fiXVaoB/whBaHftTeClAsKtB3t2DGnCfqDF3+N7qCuuosUFYFeFg== 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+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. 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)),) From patchwork Wed Nov 1 12:56:09 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: 1857949 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=mBhXRWNY; 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 4SL6VG5RPxz1yQm for ; Wed, 1 Nov 2023 23:57:26 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CCEC73857006 for ; Wed, 1 Nov 2023 12:57:24 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yw1-x112d.google.com (mail-yw1-x112d.google.com [IPv6:2607:f8b0:4864:20::112d]) by sourceware.org (Postfix) with ESMTPS id 213A33857C44 for ; Wed, 1 Nov 2023 12:56:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 213A33857C44 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 213A33857C44 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::112d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843400; cv=none; b=HaSHfXcUUSbHRNnMo1DmFsMr1g9NyoYrWWekeWwosUbmqAM8VB4t/2Cc17jfOOrRG8uifUmb9x9mG6UCsmMBQnsq1+Vx/PTGWYQQNpUVLBPKPtsB3+cHGQBg3/1YdwZOnfxK+ZGq2XEmoezcqonq9bpBRmUcKH4BV5l77bEG9Gk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843400; c=relaxed/simple; bh=7IU+K49IxS1kn/ocnc54WSjSZ3Q/mkS/5hQqscvWxMA=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=KY4t1i0yaxadKnQ9RTXpNtnADuKx0iIT2ygTKpGH78ZOTWbn6X+uM+Mw0hyj8TFSGnAlmDczdcWgfxdv7CbLXyer51PuPWsKopvipdahyK1rk/cS6bD/4X9wTNTPKXjwoXihzNgO0QG8muTAqJJ6iX7jyU3W/w7gO6SviTdbT+w= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-5afa5dbc378so52536957b3.0 for ; Wed, 01 Nov 2023 05:56:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698843389; x=1699448189; 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=mBhXRWNYIzTwI5KeY7fNin+XHqQpVLpF8PES5Sr/cIWn3lx8JKFAfimHS8SGESFKhh Kjk+B+dfl8yXKTjwm4E4Coje8SFjUWIUdAb+QHazYP1v5DCv58Ua33kA1CmrKqLuDAmW EgI+YFMVSn1jjx624Ev+Un/ID6KcPMKLt/TmWJ82uhbHCM9HROpogZSgkH8LCtDT9yhx 1lAzRe6EE14NGPH+zmiUlibuIjtarNYo8T5AB5AT5E6Llk12a7CfXeIFEPie5m+toWiE cFOKGk/D8oTxWisl2YYmeGTpmUwoaJKXBo2W4zAi/V9xxdN02CKvnZT0Vm7Vh0YAmtTy 6S8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698843389; x=1699448189; 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=sKqh0Jp42b3DuVXX+7AvtKN19b62tZyTpJckFvBbG4ScnQt0fGaTiz6qNpNYUF+BEg wd1iIwgnLVKoPNSA/4VpAoY9Pb9MZWZi1MtvHgfuKfNNoATggX0m337O81GKt3OwdppK j5IrWb/rfLoOr5BEtPQ7NrXdsFcrSvjH1+pNs41RERLZ9ifJ87A56DABVJBCc/Lh7jYf 3Y5OrGVzBaA2l6xy7qjoDRdqjBejw6lRX9mFbrfLDsCa4MbwI8pZVod4sRDiGpewrx7+ g5kgzX4fcYzWxdDW26IwTMShRpfXnqid5R8BkXnOoQeFnLihp5wvLGtMU6UJBgUzEMBB 2T3Q== X-Gm-Message-State: AOJu0YwbTcPKxgHRV8wr6kkdrMQn1U+jpUzCRonO0ALTQrNob7+BBlAA EYD7deXjr180hvVgGIvLDR96rLhg/VhSwNRn/giLaw== X-Google-Smtp-Source: AGHT+IE95LuhEI+Y13QEKXgnLApPvaXOzwU2dxsuyxhyz/ZIZAUIQtpFfGeuDItvUBmT3q8HWHsgPg== X-Received: by 2002:a25:40c5:0:b0:da0:c887:36cb with SMTP id n188-20020a2540c5000000b00da0c88736cbmr12731553yba.45.1698843388811; Wed, 01 Nov 2023 05:56:28 -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.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 05:56:28 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, DJ Delorie , Ian Rogers , Francesco Nigro , Carlos O'Donell Subject: [PATCH v3 5/7] assert: Decorate error message buffer Date: Wed, 1 Nov 2023 09:56:09 -0300 Message-Id: <20231101125611.208544-6-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.6 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, 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+incoming=patchwork.ozlabs.org@sourceware.org --- assert/assert.c | 2 ++ 1 file changed, 2 insertions(+) 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 Wed Nov 1 12:56:10 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: 1857950 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=XHRCtxXk; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4SL6VS1x0vz1yQm for ; Wed, 1 Nov 2023 23:57:36 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 355333858C60 for ; Wed, 1 Nov 2023 12:57:34 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb32.google.com (mail-yb1-xb32.google.com [IPv6:2607:f8b0:4864:20::b32]) by sourceware.org (Postfix) with ESMTPS id 4029138582BE for ; Wed, 1 Nov 2023 12:56:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4029138582BE 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 4029138582BE Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b32 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843400; cv=none; b=NP2dLUCfhKCdlTWAoCLaCgMblvpmf9X96kWZ/9WtDj77WSoYHVg9jormdSXr/rY+FI+3E48isOPFa4ZlITLs/ijR0Yl0ieFbY5xV9zs79XLYdCa6JkGuyBbuAdSMwYHHa0qAcXGlxcsfKM6P1KF2Z+HY9LubJJ2+2m6qy+kdI2M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843400; c=relaxed/simple; bh=ImCn8D9L4MIrZ9fskXjKjheSDp3oRb8wVhEnozuDrjc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=KqBVJL1oW/BLqU7bbiNo2bLgoDzNNpp7hfdYwC85279rFpFDbPMhDFIMjtekTkmG5iiqGKMfN/xNYH2zGaRgBdoiAKoivQEmQckfj0243uc3a7GqKO9wOXJhIMpPU0IBzbi0f9kcx9v147cR+NEauVZHxg7LNcdVJfLVCfBmQ+M= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb32.google.com with SMTP id 3f1490d57ef6-da041ffef81so6257002276.0 for ; Wed, 01 Nov 2023 05:56:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698843391; x=1699448191; 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=XHRCtxXk2KVwgAznFeEKiH7kVIWRcrET+hvFnYRgU3oc0UGAC7BlXBOntQQ5uNAAiC mOadHpJXLY8pCSrp3hB0gdkFo1YrQy32FlJyBwLFwfyMzwIfpBAzWRxibSQ5cooTA93U TalFvAc4BYZC/UXRmt6TNVMuf61Q0dcpvwb6qcVW2Q9rlxFBw3fh+hPPjFIhzggqOxh8 XKXGTRfMU5UswW71IXVkNkXaLiD3iak02jnNpCvCxZd8pmXYUQAgbA2YobCsXAlsqFxu XJltL1konQXgnCPsqxuEwPxtozMYMRuR5+aK045tg7XR3Ee10M9bnghs2KB96qFDBkdY 167Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698843391; x=1699448191; 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=ocoZGOOWSFduHticIzJo49PFnzVJbQuAIFy3hMorLgO1gGo9KT9xPc19hbzqFbP3ma VZbnryLjPKxNPwuVYKj9S0d3PHQZEjJaaqmveY9XwrPDLbrWKXZbgf23Wc9Qmz6H1eIr 6dP+TIst0Xi3HXHz76gO+NvSZpPotxlNdhz5dxlO8IDKnl23k6NM+jDnBQQ6xz+oPE2G qGcz5i6TOsnVuSQmeH25YG2usB24iv5LgIUj+6BKKWmla9EWxNKiBg683Je1lVgOXGyl knf3UtCrBc9P/SqEdgI8EIdnTKWshq7By6jtcoHyYhRcuV2MM9MIKsMKpPP78KwE4FvZ Rr2g== X-Gm-Message-State: AOJu0YzeFrHpgIbNsk/q5J94A7p8jtSzyiiLeRo7DYZ7w6EYTrmJoqan p6tdAR+FYyN0H7qlDkX4LXOZxGioWmtLHMHhS/Xk/w== X-Google-Smtp-Source: AGHT+IGzl/ZhAdWus9zIY9+kQHmIScJ1/HITn17t4v0kMI0/EL7HIU5YrfwVd0tJj5i4g6IlkLZIkw== X-Received: by 2002:a25:301:0:b0:d9a:c7af:bb4d with SMTP id 1-20020a250301000000b00d9ac7afbb4dmr14410318ybd.37.1698843391206; Wed, 01 Nov 2023 05:56:31 -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.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 05:56:30 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, DJ Delorie , Ian Rogers , Francesco Nigro , Carlos O'Donell Subject: [PATCH v3 6/7] linux: Decorate __libc_fatal error buffer Date: Wed, 1 Nov 2023 09:56:10 -0300 Message-Id: <20231101125611.208544-7-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.6 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, 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+incoming=patchwork.ozlabs.org@sourceware.org --- sysdeps/posix/libc_fatal.c | 3 +++ 1 file changed, 3 insertions(+) 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 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: 1857951 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=HTAfZhhl; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4SL6VV1xY3z1yQm for ; Wed, 1 Nov 2023 23:57:38 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2F4EC3857026 for ; Wed, 1 Nov 2023 12:57:36 +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-Google-Smtp-Source: AGHT+IEsFpfHBcDRxV863Neuf7J5VNtAG02sFnvcXU6tvcGrTPp2ZsSNZgSnZDrDPnmUa0nBpWT1mQ== 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+incoming=patchwork.ozlabs.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; }