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

Gabriel Ravier gabravier@gmail.com
Fri Nov 13 00:43:17 GMT 2026


On 5/27/26 6:49 PM, DJ Delorie wrote:
> This patch makes a couple of key changes to the pthread
> thread-specific-data (tsd) key logic:
> 
> * Root data is in ld.so and not libc.so
> * Malloc is not used for allocation
> * No practical key limit
> 
> This primarily addresses the problems when dlmopen namespaces are used
> and multiple copies of libc.so are loaded; previously there would be
> two copies of the root data and thus keys could not be shared across
> the namespace boundary.
> 
> The mechanism of the change is as follows:
> 
> (based onhttps://conf.gnu-tools-cauldron.org/opo25/talk/LQTU3G/)
> 
> The old method supported a fixed 1024 keys; the root data was a single
> 1024-length array and the thread data was [32][32] array with the
> second level allocated with malloc.  The new method uses a [32] array
> for the first level like the old method, but the second level is
> allocated with mmap, and the zeroth entry contains the number of
> entries.  A key's 5 LSBs index the first level, and the remaining bits
> index the second level.  The first second level (i.e. [0][]) points to
> a small fixed array in the per-thread area, like the old method.  The
> second second layer (i.e. [1][]) points to a single mmap'd page and
> can hold 255 keys.  Each additional second layer is double the size of
> the previous, up to about a quarter trillion keys in the 31st second
> layer.
> 
> 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.
> 
> 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.
> 
> So with no users and no way to know if the code I was writing was
> correct, I just left it empty.
> 
> ---
> changes in v2:
> - switched to ld.so lock and lll_lock/lll_unlock
> - don't use THREAD_GETMEM in pthread_key_create's thread loop
> - Fix destructing keys with NULL destructors
> - locked stack_cache in pthread_key_create
> - reverted to seq/data pair logic
> - renamed bucket2* macros and made them more global
> 
> ---
>   elf/Makefile                                  |   1 +
>   elf/dl-pthread-keys.c                         |  17 +++
>   elf/dl-support.c                              |   4 +
>   elf/rtld.c                                    |   4 +
>   nptl/Makefile                                 |   5 +-
>   nptl/allocatestack.c                          |   5 +-
>   nptl/descr.h                                  |   5 +-
>   nptl/nptl_deallocate_tsd.c                    | 131 +++++++++---------
>   nptl/pthread_getspecific.c                    |  62 +++++----
>   nptl/pthread_key_create.c                     | 126 +++++++++++++++--
>   nptl/pthread_key_delete.c                     |  31 +++--
>   nptl/pthread_setspecific.c                    |  98 ++++++-------
>   nptl/tst-pthread-keys-ns.c                    |  70 ++++++++++
>   ...{pthread_keys.c => tst-pthread-keys-ns1.c} |  18 ++-
>   nptl_db/structs.def                           |   2 +-
>   nptl_db/td_ta_tsd_iter.c                      |   4 +
>   nptl_db/td_thr_tsd.c                          |   4 +
>   sysdeps/generic/ldsodefs.h                    |   8 ++
>   sysdeps/nptl/dl-tls_init_tp.c                 |   2 +-
>   sysdeps/nptl/fork.h                           |  28 ++--
>   sysdeps/nptl/pthreadP.h                       |  19 ++-
>   21 files changed, 447 insertions(+), 197 deletions(-)
>   create mode 100644 elf/dl-pthread-keys.c
>   create mode 100644 nptl/tst-pthread-keys-ns.c
>   rename nptl/{pthread_keys.c => tst-pthread-keys-ns1.c} (65%)
> 
> [...]
> 
> diff --git a/nptl/pthread_key_delete.c b/nptl/pthread_key_delete.c
> index 278e523ab1..95664729bb 100644
> --- a/nptl/pthread_key_delete.c
> +++ b/nptl/pthread_key_delete.c
> @@ -16,27 +16,38 @@
>      <https://www.gnu.org/licenses/>.  */
>   
>   #include <errno.h>
> -#include "pthreadP.h"
>   #include <atomic.h>
>   #include <shlib-compat.h>
>   
> +#include "ldsodefs.h"
> +#include "pthreadP.h"
> +
> +extern int ___ldso_pthread_key_delete (pthread_key_t key)
> +  weak_function;
> +
>   int
>   ___pthread_key_delete (pthread_key_t key)
>   {
> -  int result = EINVAL;
> +  int b = PTHREAD_KEY_DECODE_BUCKET(key);
> +  int i = PTHREAD_KEY_DECODE_SLOT(key);
> +  struct pthread_key_struct **global1 = GL(dl_pthread_keys_data);
> +
> +  if (global1[b] == NULL)
> +    return EINVAL;
> +
> +  struct pthread_key_struct *global2 = global1[b];
> +  if (i < 1 || i >= global2[0].seq)
> +    return EINVAL;
>   
> -  if (__glibc_likely (key < PTHREAD_KEYS_MAX))
> -    {
> -      unsigned int seq = __pthread_keys[key].seq;
> +  int seq = global2[i].seq;

Can't check manually myself right now but I am close to certain that 
this program:

#include <pthread.h>
#include <stdio.h>

int
main (void)
{
   pthread_key_t k;
   for (long i = 0; i < 1L << 30; i++)
     {
       pthread_key_create (&k, NULL);
       pthread_key_delete (k);
     }

   int x;
   pthread_key_create (&k, NULL);
   pthread_setspecific (k, &x);
   void *p = pthread_getspecific (k);
   printf ("set %p, got %p\n", (void *) &x, p);
   return p != &x;
}

...behaves incorrectly and outputs nil under this patch due to declaring 
seq as an int. I assume it should be uintptr_t? (and also should be 
uintptr_t in pthread_setspecific, I think).
>   
> -      if (__builtin_expect (! KEY_UNUSED (seq), 1)
> -	  && ! atomic_compare_and_exchange_bool_acq (&__pthread_keys[key].seq,
> +  if (__builtin_expect (! KEY_UNUSED (seq), 1)
> +	  && ! atomic_compare_and_exchange_bool_acq (&global2[i].seq,
>   						     seq + 1, seq))
>   	/* We deleted a valid key.  */
> -	result = 0;
> -    }
> +    return 0;
>   
> -  return result;
> +  return EINVAL;
>   }
>   versioned_symbol (libc, ___pthread_key_delete, pthread_key_delete,
>   		  GLIBC_2_34);
> diff --git a/nptl/pthread_setspecific.c b/nptl/pthread_setspecific.c
> index 3cf07a020c..0dbba74082 100644
> --- a/nptl/pthread_setspecific.c
> +++ b/nptl/pthread_setspecific.c
> @@ -17,75 +17,77 @@
>   
>   #include <errno.h>
>   #include <stdlib.h>
> -#include "pthreadP.h"
>   #include <shlib-compat.h>
>   
> +#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;
> +}
> +
> +
>   int
>   ___pthread_setspecific (pthread_key_t key, const void *value)
>   {
>     struct pthread *self;
> -  unsigned int idx1st;
> -  unsigned int idx2nd;
> -  struct pthread_key_data *level2;
>     unsigned int seq;

(from above: seems to me like it ought to be uintptr_t)

> +  struct pthread_key_data *level2;
> +  int b = PTHREAD_KEY_DECODE_BUCKET(key);
> +  int i = PTHREAD_KEY_DECODE_SLOT(key);
>   
>     self = THREAD_SELF;
>   
> -  /* Special case access to the first 2nd-level block.  This is the
> -     usual case.  */
> -  if (__glibc_likely (key < PTHREAD_KEY_2NDLEVEL_SIZE))
> -    {
> -      /* Verify the key is sane.  */
> -      if (KEY_UNUSED ((seq = __pthread_keys[key].seq)))
> -	/* Not valid.  */
> -	return EINVAL;
> +  /* The bucket number can only be 0..31, and all are valid.  */
> +  struct pthread_key_struct **global1 = GL(dl_pthread_keys_data);
> +  struct pthread_key_struct *global2 = global1[b];
> +  if (global2 == NULL)
> +    return EINVAL;
> +  if (i < 0 || i >= global2[0].seq)
> +    return EINVAL;
>   
> -      level2 = &self->specific_1stblock[key];
> +  seq = global2[i].seq;
> +  if (KEY_UNUSED (seq))
> +    return EINVAL;
>   
> -      /* Remember that we stored at least one set of data.  */
> -      if (value != NULL)
> -	THREAD_SETMEM (self, specific_used, true);
> -    }
> -  else
> -    {
> -      if (key >= PTHREAD_KEYS_MAX
> -	  || KEY_UNUSED ((seq = __pthread_keys[key].seq)))
> -	/* Not valid.  */
> -	return EINVAL;
> +  /* If we need to mmap a bucket, do so now.  */
>   
> -      idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
> -      idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
> +  level2 = THREAD_GETMEM_NC (self, specific, b);
> +
> +  if (level2 == NULL)
> +    {
> +      size_t mlen = PTHREAD_KEY_BUCKET2MLEN (b);
> +      size_t slots = PTHREAD_KEY_BUCKET2SLOTS (b);
> +      struct pthread_key_data *m;
>   
> -      /* This is the second level array.  Allocate it if necessary.  */
> -      level2 = THREAD_GETMEM_NC (self, specific, idx1st);
> -      if (level2 == NULL)
> +      if (b == 0)
>   	{
> -	  if (value == NULL)
> -	    /* We don't have to do anything.  The value would in any case
> -	       be NULL.  We can save the memory allocation.  */
> -	    return 0;
> -
> -	  level2
> -	    = (struct pthread_key_data *) calloc (PTHREAD_KEY_2NDLEVEL_SIZE,
> -						  sizeof (*level2));
> -	  if (level2 == NULL)
> +	  m = (struct pthread_key_data *) & self->specific_1stblock;
> +	  slots = PTHREAD_KEY_2NDLEVEL_SIZE;
> +	}
> +      else
> +	{
> +	  m = __mmap (0, mlen, PROT_READ|PROT_WRITE,
> +		    MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
> +	  if (m == MAP_FAILED)
>   	    return ENOMEM;
> -
> -	  THREAD_SETMEM_NC (self, specific, idx1st, level2);
>   	}
>   
> -      /* Pointer to the right array element.  */
> -      level2 = &level2[idx2nd];
> +      memset (m, 0, slots * sizeof (struct pthread_key_data));
> +      m[0].seq = slots;
>   
> -      /* Remember that we stored at least one set of data.  */
> -      THREAD_SETMEM (self, specific_used, true);
> +      level2 = m;
> +      THREAD_SETMEM_NC (self, specific, b, m);
>       }
>   
> -  /* Store the data and the sequence number so that we can recognize
> -     stale data.  */
> -  level2->seq = seq;
> -  level2->data = (void *) value;
> -
> +  THREAD_SETMEM (self, specific_used, 1);
> +  level2[i].seq = seq;
> +  level2[i].data = (void *) value;
>     return 0;
>   }
>   versioned_symbol (libc, ___pthread_setspecific, pthread_setspecific,
> 
> [...]
> 
> -- 2.47.3
> 



More information about the Libc-alpha mailing list