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 X-Patchwork-Id: 740061 Delivered-To: patch@linaro.org Received: by 2002:a5d:538f:0:b0:32d:baff:b0ca with SMTP id d15csp267411wrv; Wed, 1 Nov 2023 05:56:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHG2lk87MksmE+D3kn3fTgcpv8jfuG2c/hILpuXd/wAqISnandJVuqNrjtlvEF+myxArsaV X-Received: by 2002:a05:620a:2846:b0:779:deb9:72c6 with SMTP id h6-20020a05620a284600b00779deb972c6mr17354195qkp.14.1698843404392; Wed, 01 Nov 2023 05:56:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698843404; cv=pass; d=google.com; s=arc-20160816; b=n8igER1eWYH7BGBBpizeJEkYnLMbNU9ypRONWigqAAXVhfsFJmxREX8tPPnDzZ1zoB TOKzojsNkTMDPZ5jWtcaZbwYzqJxfJ1ZclEr5jGu8PhqVhwbozovcccc0s7NqcoA7Nhg PrBouWJ3cpyVQhOU4tTTWSu+jDKUPQkudEDrnUqsUUr5ErPm2iEzaTNGZxaOVAxgu/is zR2UWiKV8bb5pItC222dowsKvwMIZdnoTqsTh5N9bh+TySCB2kfiQSdMYmLH8sH0wOpO LYRqTJ4Jp5t1iNIM4YBWs+Og+QILQKmQC2fMJNyYSun0eWum0FtsTZ3aXgp2DG5WQ1Zb qcrA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=1F+2KIbgsk1fyfgMGRYp2xAq37K8oAJmUhZ6k/9SOHk=; fh=wOialSeEyZrL0Zmx/XQW8gMhKHOwjxYz8NEgiPSksGI=; b=wV+Q9EJ+XQfCBdiNZsrcXUPweMHc4suk23CydEf/4CgY40kK5UfMn0/peeUrJjWsTG Dg46+9MI8xMi9BSye66YCKTzMu2ALEpKy8YJR947LAjtqfzglF6llzhLu/9aS5SNUmsP BJZsIBoSTOldU4Zwe+LVz+qbvUlCPClF9qnVlWqBY4/aOl1NEhP+pJwz0qOJkYqMfFoI gGwHM8/jWPvUWxhRedFsr5tNNKbBkpRq1GqQu4uPMZhf+icTcQdCoKFk8EizVgMs29O7 clgsCyXHIvlzq8kbLOOlbiyQMBTafCgm2aUA1vMD46xJyiSYSTEuHsURgpCC9jxH5ntY ztXA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zVAiMoha; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id p17-20020a05620a113100b00773d3c0bcdbsi2552284qkk.427.2023.11.01.05.56.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 05:56:44 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zVAiMoha; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E81D33857730 for ; Wed, 1 Nov 2023 12:56:43 +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-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+patch=linaro.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