From patchwork Wed Sep 27 20:38:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 1840442 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=google.com header.i=@google.com header.a=rsa-sha256 header.s=20230601 header.b=YB7YLlDu; 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 4RwpNV3hFvz1yp0 for ; Thu, 28 Sep 2023 06:38:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B1B3385828D for ; Wed, 27 Sep 2023 20:38:32 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by sourceware.org (Postfix) with ESMTPS id A7B65385828D for ; Wed, 27 Sep 2023 20:38:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A7B65385828D Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d814634fe4bso18716620276.1 for ; Wed, 27 Sep 2023 13:38:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695847102; x=1696451902; darn=sourceware.org; h=cc:to:from:subject:mime-version:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=l7kHN3H8KvRD6wnDxxtX2kYTNySRUj0EWh1TRzxD5kw=; b=YB7YLlDutQQ0lyBaXfOeJAS28Xbtg9biA1+rDDYtzA7EP479ulCiiJSFHJf4lAKK2G ExUC9Ex5S2JXzd8PnvJ7SFyZvJzgha2Kf49WHBYl6uZARnuorpLkrRNa+961PQBntCk8 ZaDK/YrUo6SZKOjLjrbqo8QyejSJ72lUcQ07iHaxi0n6RFZUQw1Tnw6UuroAENzeTIjk diS0ZhQesug7c/G64dMSioSsOKb5tsIOwfcuKz7JyHkKWkzIGLjr5JrKR0iFDfwmEitu V2WAHxzeek+M70gH3HEWDQSMarP0Pnoua7xGPVSSRIwKhZFSCn91+jCNDldgnc4rWz+F SnoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695847102; x=1696451902; h=cc:to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=l7kHN3H8KvRD6wnDxxtX2kYTNySRUj0EWh1TRzxD5kw=; b=JwmhpupyR514wQZ9N5eiz8YwbU9W4R4qTWBBQRais0+U06CKt9pSBd/snJ4yiBtVlW zm8pSN7FNhoJKYuFyAUW4CJ9UVJ0z4CYndMbyOKSgKwD8+NVVlRJif02u9Wsg029z5Uy NB3PBYjl6ySPXZGwmp4ymBpIMJ/BAA6dSzpKpIQASP2EPCARRvXfjlyhXc4rx39poery +vjoFlI6Gi9Kh/80/ra9UNXEYaZNLvup6hOvVRBJFvyO0gT0JOPgbELQjm4+mc78P9YB 535T43w/jOl4LYGlZmA7tall5hUKw2ilb1Uuyu4+Sg+jPMOwvr5Aot0pPXmcqfDDMB5i VPWA== X-Gm-Message-State: AOJu0Yy4702H/7H7r65EMJinORacnYARlBVYK1xkTA6qfyxJAwgWPAqj udSimdcVTn/HVE5Zdhr+YawRWOk20ZGMjbFVnXGuTLtul9HYUUVsRx4ZX/QOyTTWyF7gV4ieYHn p1IQFlKtf9n40uHDfAGIaMbn7ZPs85HHK6zImk5X8+x9bOzHTqMWe7mKpDMrbhpfN5opt X-Google-Smtp-Source: AGHT+IGzWlYqHxyz5FpdhGR5hdI/uCDIbvSg1iE8NbzV+DxS68wc0L73TJFhF8aAOJA0X2WoW0HRZalj5UgH X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:247a:7324:7470:3619]) (user=irogers job=sendgmr) by 2002:a25:d40f:0:b0:d85:ad61:1591 with SMTP id m15-20020a25d40f000000b00d85ad611591mr49014ybf.11.1695847101960; Wed, 27 Sep 2023 13:38:21 -0700 (PDT) Date: Wed, 27 Sep 2023 13:38:15 -0700 Message-Id: <20230927203815.1843924-1-irogers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Subject: [RFC PATCH] pthread_create: Name stacks in /proc//maps From: Ian Rogers To: libc-alpha@sourceware.org, Adhemerval Zanella Netto Cc: Ian Rogers X-Spam-Status: No, score=-21.4 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL 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: https://github.com/torvalds/linux/commit/65376df582174ffcec9e6471bf5b0dd79ba05e4a This untested (other than building) RFC patch uses PR_SET_VMA_ANON_NAME to name stacks again as part of thread creation. PR_SET_VMA_ANON_NAME was added in Linux 5.17: https://github.com/torvalds/linux/commit/9a10064f5625d5572c3626c1516e0bebc6c9fe9b The patch is intended to create discussion and possibly serve as an implementation. When I try to test it current I get unrelated failures and so guidance would be appreciated. The naming of stacks can be useful in situations like debugging and profiling, for example, to differentiate stack from heap memory accesses. --- include/sys/prctl.h | 5 +++++ nptl/allocatestack.c | 24 ++++++++++++++++++++++++ nptl/pthread_create.c | 5 +++++ 3 files changed, 34 insertions(+) diff --git a/include/sys/prctl.h b/include/sys/prctl.h index d33f3a290e..8968a632d3 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/nptl/allocatestack.c b/nptl/allocatestack.c index f9d8cdfd08..404a222c24 100644 --- a/nptl/allocatestack.c +++ b/nptl/allocatestack.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -33,6 +34,7 @@ #include #include #include +#include "intprops.h" /* Default alignment of stack. */ #ifndef STACK_ALIGN @@ -577,3 +579,25 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp, return 0; } + +/* Use PR_SET_VMA_ANON_NAME to name the thread's stack stack: */ +static void +name_stack_maps (struct pthread *pd, bool set) +{ + void *stack = THREAD_GETMEM (pd, stackblock); + size_t stacksize = THREAD_GETMEM (pd, stackblock_size); + + if (!set) + __prctl (PR_SET_VMA, PR_SET_VMA_ANON_NAME, stack, stacksize, NULL); + else + { +#define STACK_MAPS_PREFIX "stack:" + char stack_name[sizeof (STACK_MAPS_PREFIX) + + INT_BUFSIZE_BOUND (unsigned int)]; + + __snprintf (stack_name, sizeof (stack_name), STACK_MAPS_PREFIX "%u", + (unsigned int) THREAD_GETMEM (pd, tid)); + __prctl (PR_SET_VMA, PR_SET_VMA_ANON_NAME, stack, stacksize, stack_name); +#undef STACK_MAPS_PREFIX + } +} diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index 6a41d50109..7249513a80 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 (); + /* Set up /proc//maps entry for stack to stack:tid. */ + 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,8 @@ start_thread (void *arg) /* Free the TCB. */ __nptl_free_tcb (pd); + /* Release name for /proc//maps. */ + name_stack_maps (pd, false); out: /* We cannot call '_exit' here. '_exit' will terminate the process.