From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id 8BD1138618E1 for ; Sat, 28 Oct 2023 19:56:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8BD1138618E1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8BD1138618E1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::32d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522984; cv=none; b=M5UcDjMlwUsYuoIS6IftfIe6klnodMGkw2H73CaO6u1qwsUmz3qupVMnnbJo3VrJpTmvlPDF34dHOd7Wv9r2yTZlpllWQd5ycTy53LZGxIR2Y8IE5WsttPwhkKAH9F8nSOAaSD8Di1ebnDBFE/+eSz47AxGmiadsUy1QYbHICNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698522984; c=relaxed/simple; bh=54XFVv6yTgUxeZ6DeSgmQxu0PKDIKjfP/KDiGrUfBLM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=jdCXj+KcPA6elAa4ynQkR2SFtBnOV79eVcn536ckgOnASWWAZBPb5XTk+DbZEWmL9zKWFeHvsM+XDeG2jphRp+kqjx5IqWaXNlEQMmAMZFCt0nz2x7aq174NPSoeBMzc7fa3Y4TFgKNprwCN2Egu6L2uJW9lsVVKt5Szp5OR0TI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x32d.google.com with SMTP id 46e09a7af769-6ce327458a6so1631300a34.1 for ; Sat, 28 Oct 2023 12:56:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698522981; x=1699127781; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=WlH95tVNYVT/bq+I1M60U0Pco+sIKS/SJrorYGSac1k=; b=ExAGM1HWrfikSiQClfCF9Wg+Skt1niDyKbM55mnKCPiPWweMCNAMY9n8lJqGxsX4yr I6qagUZ4v1V2em4CL0hI/uNFdkGKrhPtjaWD1Lc9vvTZTQgx4JLhDZB2ZADRpcjiPoBQ rGlkAFI7UDgIOuqS4sFH3uQVuL1frBsz7PTYqbJYRaFHFCXIwogCADVA3qrFG7fwG7AO P1knIdh85uP16OYuKPWaU0igiJ38/9J+PX8mC8CwffDndB218C7fpCmN59gJwlbvmUBL zlTY7A5u2p7P/c/9urUmBxKL2WmmYYn98QNZJiL+Uh5WZ4T0c3ORnrFaP+3/Dfgnxhhe dBEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698522981; x=1699127781; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WlH95tVNYVT/bq+I1M60U0Pco+sIKS/SJrorYGSac1k=; b=u1hG3TwfASTBi/amlCDG2peDmcjB8GluNoCNsRqP2nnb6fi3hJePq1XXlCKX3hzrRF nUUv2MJ6TgBJzo9VnujCtSrWuYsyEU5Yzo4iemhdsyCDyGsnWhbnGqGw0lEe3jghaP2K KmD5+BA4FMWbGybXLEQoNt9ds6gPwRWV71SBWStSWMiZZaerPG7QJQFQ7fuqalls1mCE KWusetj4bvCm0rtokNC2ql80SRVYbyGutIdbD6VXiIZT9dko5emDLxJS1YcXVT77nygx 2vVlOXgnF8cILxCjtzbtmkC3hN0aVOkce/eZSgeob2TtPQcRAZVDwaURhQVdLu5AOeG6 Hq+w== X-Gm-Message-State: AOJu0YxXOD5OG5nV+u2cDEzgDTOLf9H4W95IY/9Awl19eXV0bdlnfoNC wqbs4kSz6vxF6A9fRcNUQz8vqM+9Sz1VrHWWrqzG7JlQ X-Google-Smtp-Source: AGHT+IEEdhuKHIvNQlr4Qbner39U4Wir7hIfCNhW89e/9M1FkK+HHlQFLdkBdagmg0DQsNdQv4im5Q== X-Received: by 2002:a05:6830:1550:b0:6b4:4ee1:b525 with SMTP id l16-20020a056830155000b006b44ee1b525mr3231663otp.1.1698522981219; Sat, 28 Oct 2023 12:56:21 -0700 (PDT) Received: from localhost.localdomain ([139.178.84.207]) by smtp.gmail.com with ESMTPSA id w20-20020a056830411400b006b8c87551e8sm763181ott.35.2023.10.28.12.56.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 12:56:19 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Ian Rogers , Francesco Nigro , Carlos O'Donell , Siddhesh Poyarekar Subject: [PATCH v2 7/7] elf: Add glibc.mem.decorate_maps tunable Date: Sat, 28 Oct 2023 19:55:59 +0000 Message-Id: <20231028195559.390407-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231028195559.390407-1-adhemerval.zanella@linaro.org> References: <20231028195559.390407-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 List-Id: 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. --- NEWS | 5 +++++ elf/Makefile | 2 +- elf/dl-tunables.list | 5 +++++ manual/tunables.texi | 12 ++++++++++++ sysdeps/unix/sysv/linux/setvmaname.c | 4 ++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 9432564444..67f6e63b14 100644 --- a/NEWS +++ b/NEWS @@ -38,6 +38,11 @@ Major new features: and the wfN format length modifiers for arguments pointing to types int_fastN_t or uint_fastN_t, as specified in draft ISO C2X. +* A new tunable, glibc.mem.decorate_maps, can be used to add additional + information on underlying memory allocated by the glibc (for instance, + on thread stack created by pthread_create or memory allocated by + malloc). + Deprecated and removed features, and other changes affecting compatibility: * The ldconfig program now skips file names containing ';' or ending in diff --git a/elf/Makefile b/elf/Makefile index c3cf63a443..2603b90961 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -3023,5 +3023,5 @@ $(objpfx)tst-dlclose-lazy.out: \ $(objpfx)tst-decorate-maps: $(shared-thread-library) tst-decorate-maps-ENV = \ - GLIBC_TUNABLES=glibc.malloc.arena_max=8:glibc.malloc.mmap_threshold=1024 + GLIBC_TUNABLES=glibc.malloc.arena_max=8:glibc.malloc.mmap_threshold=1024:glibc.mem.decorate_maps=1 tst-decorate-maps-ARGS = 8 diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list index 695ba7192e..888d2ede04 100644 --- a/elf/dl-tunables.list +++ b/elf/dl-tunables.list @@ -160,6 +160,11 @@ glibc { maxval: 255 security_level: SXID_IGNORE } + decorate_maps { + type: INT_32 + minval: 0 + maxval: 1 + } } rtld { diff --git a/manual/tunables.texi b/manual/tunables.texi index 776fd93fd9..2a5ad73e9d 100644 --- a/manual/tunables.texi +++ b/manual/tunables.texi @@ -653,6 +653,18 @@ support in the kernel if this tunable has any non-zero value. The default value is @samp{0}, which disables all memory tagging. @end deftp +@deftp Tunable glibc.mem.decorate_maps +If the kernel supports naming anonymous virtual memory areas (since +Linux version 5.17), this tunable can be used to control whether +@theglibc{} decorates the underlying memory obtained from operating +system with a string describing its usage (for instance, on the thread +stack created by @code{ptthread_create} or memory allocated by +@code{malloc}). + +This tunable takes a value of 0 and 1, where 1 enables the feature. +The default value is @samp{0}, which disables the decoration. +@end deftp + @node gmon Tunables @section gmon Tunables @cindex gmon tunables diff --git a/sysdeps/unix/sysv/linux/setvmaname.c b/sysdeps/unix/sysv/linux/setvmaname.c index 9960ab5917..62237fcf7c 100644 --- a/sysdeps/unix/sysv/linux/setvmaname.c +++ b/sysdeps/unix/sysv/linux/setvmaname.c @@ -20,6 +20,7 @@ #include #include #include +#include /* If PR_SET_VMA_ANON_NAME is not supported by the kernel, prctl returns EINVAL. However, it also returns the same error for invalid argument. @@ -30,6 +31,9 @@ void __set_vma_name (void *start, size_t len, const char *name) { + if (TUNABLE_GET (glibc, mem, decorate_maps, int32_t, NULL) == 0) + return; + static int prctl_supported = 1; if (atomic_load_relaxed (&prctl_supported) == 0) return; -- 2.34.1