[PATCH v2 1/1] nptl: namespace-safe pthread keys implementation

Florian Weimer fweimer@redhat.com
Wed Nov 11 18:32:40 GMT 2026


* DJ Delorie:

> The root data initially points at a structure in ld.so but if it's
> NULL a backup copy in libc.so is used; this happens when statically
> linked.

Static linking with dlopen is not fixed by this patch.  Not enough is
shared for that.  It would requiring sharing the lock (and perhaps
future data) as well.  I would prefer if you could move in that
direction and set up the pointer with __rtld_static_init.

> The nptl_db library parts, I ended up just commenting out as:
>
> 1. The internals are not documented
> 2. There are no test cases for it
> 3. I could find nothing that used those functions (not even gdb)
> 4. An AI-generated test case didn't even work with the old logic.

This should go into a separate patch.  Please just delete the code with
a comment and an e ror return.

> So with no users and no way to know if the code I was writing was
> correct, I just left it empty.

The x86_64-gnu (Hurd) build fails:

dl-pthread-keys.c:9:34: error: array type has incomplete element type ‘struct pthread_key_struct’
    9 | static struct pthread_key_struct key_bucket_zero[32] =
      |                                  ^~~~~~~~~~~~~~~


I also see a build failure on sh4eb-linux-gnu, which seems related:

…/sh4eb-linux-gnu/sh4eb-glibc-linux-gnu/bin/ld: …/sh4eb-linux-gnu/glibc/elf/librtld.os: in function `___pthread_key_create':
/home/bmg/src/glibc/nptl/pthread_key_create.c:101:(.text+0x1b488): undefined reference to `abort'
…/sh4eb-linux-gnu/sh4eb-glibc-linux-gnu/bin/ld:
…/nptl/pthread_key_create.c:101:(.text+0x1b48c): undefined reference to
`abort'

Also happens on or1k-linux-gnu-soft.

There is a check-localplt failure on powerpc-linux-gnu-soft:

Extra PLT reference: libc.so: __fixunssfsi
Extra PLT reference: libc.so: __ltsf2
Extra PLT reference: libc.so: __lesf2
Extra PLT reference: libc.so: __gesf2

I don't know if that is a regression.


Regarding the Hurd build failure, it may be necessary to consolidate the
two implementations.  I don't know if it makes sense to do this in a
separate patch.  It likely involves making PTHREAD_KEY_MEMBERS a shared
macro that is used in the appropriate places.  I would prefer if we
could it define in such a way that the size of struct pthread on Linux
does not grow (so that backporting does not become complicated).  Some
discrepancy will remain because the size of pthread_key_t is different
on Hurd and Linux (void * vs unsigned int).

> diff --git a/elf/dl-pthread-keys.c b/elf/dl-pthread-keys.c
> new file mode 100644
> index 0000000000..ceb2173975
> --- /dev/null
> +++ b/elf/dl-pthread-keys.c
> @@ -0,0 +1,17 @@
> +#include <unistd.h>
> +#include <ldsodefs.h>
> +#include <list.h>
> +#include <libc-lock.h>
> +#include "pthreadP.h"
> +
> +/* This struct must match the global definition in pthreadP.h so that
> +   the slot counts match.  */
> +static struct pthread_key_struct key_bucket_zero[32] =
> +  {
> +    { 32, 0 },
> +    [1 ... 31] = { 0, 0 }
> +  };
> +
> +/* Table of the key information.  */
> +struct pthread_key_struct *_dl_pthread_key_buckets_ldso[32] =
> +  { (struct pthread_key_struct *)&key_bucket_zero };

Sorry, I find this comment somewhat confusing.  Should this use
PTHREAD_KEY_2NDLEVEL_SIZE?

> diff --git a/nptl/descr.h b/nptl/descr.h
> index 627cc3980f..c08ca4bc7f 100644
> --- a/nptl/descr.h
> +++ b/nptl/descr.h
> @@ -57,9 +57,6 @@
>    ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
>     / PTHREAD_KEY_2NDLEVEL_SIZE)
>  
> -
> -
> -
>  /* Internal version of the buffer to store cancellation handler
>     information.  */

Extra whitespace change.

> diff --git a/nptl/nptl_deallocate_tsd.c b/nptl/nptl_deallocate_tsd.c
> index 4c3960b516..57b685a394 100644
> --- a/nptl/nptl_deallocate_tsd.c
> +++ b/nptl/nptl_deallocate_tsd.c
> @@ -15,48 +15,55 @@
>     License along with the GNU C Library; if not, see
>     <https://www.gnu.org/licenses/>.  */
>  
> +#include <ldsodefs.h>
>  #include <pthreadP.h>
>  
> +static size_t page_size = 0;
> +
> +static size_t
> +get_page_size(void)
> +{
> +  page_size = EXEC_PAGESIZE;
> +  return page_size;
> +}

Why not use GLRO(dl_pagesize)?

> +
>  /* Deallocate POSIX thread-local-storage.  */
>  void
>  __nptl_deallocate_tsd (void)
>  {
> -  struct pthread *self = THREAD_SELF;
> +  struct pthread_key_struct **global1;
> +  struct pthread_key_struct *global2;
> +  struct pthread_key_data *level2;
>  
>    /* Maybe no data was ever allocated.  This happens often so we have
>       a flag for this.  */
> -  if (THREAD_GETMEM (self, specific_used))
> +  if (THREAD_GETMEM (THREAD_SELF, specific_used))
>      {
> -      size_t round;
> -      size_t cnt;
> -
> -      round = 0;

This seems to have a fair amount of unrelated changes (THREAD_SELF to
self, whitespace)?  The convention with multiple uses is to cache
THREAD_SELF.  It could be a special register that is somewhat costly to
move to a general-purpose register.  Probably a leftover from the
original generation counter removal?

Same for pthread_getspecific and pthread_key_create, I think.

> diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
> index 15c4659853..f4a77de132 100644
> --- a/sysdeps/generic/ldsodefs.h
> +++ b/sysdeps/generic/ldsodefs.h
> @@ -467,6 +467,13 @@ struct rtld_global
>  
>    /* Mutex protecting the stack lists.  */
>    EXTERN int _dl_stack_cache_lock;
> +
> +  /* Pthread keys global destructor tables.  Actually a pointer to
> +     pthread_key_bucket[32].  */
> +  EXTERN void *_dl_pthread_keys_data;
> +
> +  /* Mutex protecting the pthread keys global data.  */
> +  EXTERN int _dl_pthread_keys_lock;

See above, move that behind the pointer.  There is no need to use void *
here, you can add a forward declaration like

struct rtld_pthread_key_cb;

near the start of the file (outside of this struct).

It makes sense to keep the pointer even if ld.so allocates the backing
storage.  This way, GL layout does not change if we tweak the key
implementation.  And once you ld.so supplies storage, the pointer can be
in GLRO, I think.

> diff --git a/sysdeps/nptl/pthreadP.h b/sysdeps/nptl/pthreadP.h
> index de432d4032..d1fb233b5e 100644
> --- a/sysdeps/nptl/pthreadP.h
> +++ b/sysdeps/nptl/pthreadP.h
> @@ -185,8 +185,23 @@ extern int __attr_list_lock attribute_hidden;
>  extern int __concurrency_level attribute_hidden;
>  
>  /* Thread-local data key handling.  */
> -extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
> -libc_hidden_proto (__pthread_keys)
> +#define PTHREAD_KEY_BUCKET2MLEN(b) \
> +  (((page_size != 0) ? page_size : get_page_size()) << (b-1))
> +#define PTHREAD_KEY_BUCKET2SLOTS(b) \
> +  (PTHREAD_KEY_BUCKET2MLEN(b) / sizeof (struct pthread_key_data))
> +#define PTHREAD_KEY_ENCODE(b,i) ((pthread_key_t) (b | (i<<5)))
> +#define PTHREAD_KEY_DECODE_BUCKET(k) (((size_t)k) & 31)
> +#define PTHREAD_KEY_DECODE_SLOT(k) (((size_t)k) >> 5)
> +#if IS_IN(rtld)
> +#define PTHREAD_KEY_LOCK
> +#define PTHREAD_KEY_UNLOCK
> +#else
> +#define PTHREAD_KEY_LOCK lll_lock (GL(dl_pthread_keys_lock), LLL_PRIVATE)
> +#define PTHREAD_KEY_UNLOCK lll_unlock (GL(dl_pthread_keys_lock), LLL_PRIVATE)
> +#endif

Please add comments to the new macros.

Thanks,
Florian



More information about the Libc-alpha mailing list