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 X-Patchwork-Id: 738981 Delivered-To: patch@linaro.org Received: by 2002:a5d:4c47:0:b0:32d:baff:b0ca with SMTP id n7csp482775wrt; Sat, 28 Oct 2023 12:56:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFeMvPSau1TmFhqfeOGA0fFMS4AVZU55CgoGDQy82zvrC5hkfx+2y4mnnVrPtMuMCdO0RhB X-Received: by 2002:a05:620a:c4a:b0:774:2c5c:557e with SMTP id u10-20020a05620a0c4a00b007742c5c557emr6273773qki.1.1698522978949; Sat, 28 Oct 2023 12:56:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698522978; cv=pass; d=google.com; s=arc-20160816; b=tNCjGjV1g3cjncdaEgTY4StTt3EH2ambXQ2jvxH3CDITKQPsCHdAnbI2AG4Qjn0R74 J6PCmcUYOj1aFDrakPfBWWbyIBEzaeaiJwggEqnohS63NN5x0LI1tun/+wIoAwoMRKZ4 tIavpDHfY6vWVAJwBBjXi2b+irXae+A8xJnQNmFLqX4/V/LqvxxA1OGIU9F6NMrLCCbB dtoAommRWrrRFLGed6bbHLxXnucvsbTH9hT4hYFmXOjQcx0AHPlkZG/hhiIXsPdauPk0 WmF6LJp9UoGe2Gv5LaGnEg6kH1rAPSCYPMNYeSXHgnLOHjHRjCza9M05FpaTeP5e0Lhu 65Qw== 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=I4RpB7qGSb35rdpy7VxSRUxRllXP+KRj7Dspu0xYtZ0=; fh=Lbqp+rs2mRBTG5C9ik7cirOzlOF2+U9yLxjtEQVqExY=; b=r8p3cIpN/b/HWlfGMkvtkyMehsffiuvkP3M0tmJubKapMywublbLG3H+VrnCnB+4cN tR6uwUxt+lAiOgVyG5FmYjzZXJ8kCo2Kkr4C8rUHpABe/TNrMjT448DB6GlyhaNY0BVO Hj9MG1lt9ahedskyt5qFwtcCAI+kl/BmF9Ql8Tl6cMnwHFqKKLk2ndAfoKbSsaI+VWEd 8Z4vtGWnPc88XSHGnU91+6r9ojSd+Ck4xYY/ocJRHzAAgQX+rXreziw2cuqnZgZ6nd0+ PshD8mk83CKNIfIdG5SGVTfKGfXAyMvwMGgv+t4EDcmPEsYQ0ZPkwleyG84HKsKoVIxj /Uhg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=A0HHeIpS; 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 j18-20020a05620a411200b007789c2585f0si2914043qko.418.2023.10.28.12.56.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 12:56:18 -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=A0HHeIpS; 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 9285538618EF for ; Sat, 28 Oct 2023 19:56:18 +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-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+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