From 6afce56c197ee83520994a2c94a82c2ca2bce9fa Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Wed, 1 Nov 2023 09:56:07 -0300 Subject: nptl: Decorate thread stack on pthread_create 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 --- nptl/allocatestack.c | 40 ++++++++++++++++++++++++++++++++++++++++ nptl/pthread_create.c | 6 ++++++ 2 files changed, 46 insertions(+) (limited to 'nptl') 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. -- cgit 1.4.1