This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit b3b1858b4735017e782cef163bca5ea9a27309fd
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Oct 19 11:52:59 2026 +0200

    libs/libc: Free the __thread copies of a module when it is unloaded.
    
    A module or shared library with __thread variables took a new emutls
    index each time it was loaded, and the copies of its variables stayed
    in each thread until the thread exited.  A module that is loaded and
    unloaded again and again made the arrays of all threads grow.
    
    libc now keeps the control object of each index and a list of the
    arrays of all threads.  When a module goes, libelf_uninit() calls
    lib_emutls_release() with the data range of the module, where all its
    control objects are.  It calls it through a pointer that libc sets when
    it gives the first index, so the loader does not link the code.  libc
    frees the copies of those variables in every thread and gives their
    indexes back; a new variable takes the first free index.  One lock
    covers the index table, the list, and the slow path of
    __emutls_get_address(); a copy that exists is still read with no lock.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/elf/elf_remove.c  |  10 ++++
 libs/libc/libc.h            |   6 +-
 libs/libc/tls/tls_emutls.c  | 131 +++++++++++++++++++++++++++++++++++++++-----
 libs/libc/tls/tls_getinfo.c |   5 +-
 4 files changed, 134 insertions(+), 18 deletions(-)

diff --git a/libs/libc/elf/elf_remove.c b/libs/libc/elf/elf_remove.c
index d9be2e288f4..3402f1c20a2 100644
--- a/libs/libc/elf/elf_remove.c
+++ b/libs/libc/elf/elf_remove.c
@@ -33,6 +33,7 @@
 #include <nuttx/lib/lib.h>
 #include <nuttx/lib/elf.h>
 
+#include "libc.h"
 #include "elf/elf.h"
 
 /****************************************************************************
@@ -112,6 +113,15 @@ int libelf_uninit(FAR struct module_s *modp)
   libelf_pinrelease(&modp->pinfile);
 #endif
 
+#if defined(CONFIG_TLS_EMUTLS) && !defined(__KERNEL__)
+  /* Free the copies of the __thread variables of the module */
+
+  if (g_tls_emutls_release != NULL)
+    {
+      g_tls_emutls_release(modp->dataalloc, modp->datasize);
+    }
+#endif
+
   /* Release resources held by the module */
 
   if (modp->textalloc != NULL || modp->dataalloc != NULL)
diff --git a/libs/libc/libc.h b/libs/libc/libc.h
index 1e289c06260..a71e2aa405e 100644
--- a/libs/libc/libc.h
+++ b/libs/libc/libc.h
@@ -328,12 +328,14 @@ ssize_t lib_parse_hostfile(FAR FILE *stream, FAR struct 
hostent *host,
 int lib_restoredir(void);
 #endif
 
-/* Defined in tls/tls_getinfo.c.  tls/tls_emutls.c sets it, and exit()
- * and pthread_exit() call it.
+/* Defined in tls/tls_getinfo.c.  tls/tls_emutls.c sets them, and exit(),
+ * pthread_exit() and the loader call them.
  */
 
 #ifdef CONFIG_TLS_EMUTLS
 extern CODE void (*g_tls_emutls_destruct)(void);
+extern CODE void (*g_tls_emutls_release)(FAR const void *start,
+                                         size_t size);
 #endif
 
 /* Defined in lib_cxx_initialize.c */
diff --git a/libs/libc/tls/tls_emutls.c b/libs/libc/tls/tls_emutls.c
index cafbeb6007c..1a59d2ee933 100644
--- a/libs/libc/tls/tls_emutls.c
+++ b/libs/libc/tls/tls_emutls.c
@@ -31,6 +31,7 @@
 #include <string.h>
 
 #include <nuttx/atomic.h>
+#include <nuttx/list.h>
 #include <nuttx/mutex.h>
 #include <nuttx/tls.h>
 
@@ -62,6 +63,7 @@ struct __emutls_object
 
 struct emutls_array_s
 {
+  struct list_node node;  /* In g_emutls_arrays */
   uintptr_t size;
   FAR void *data[1];
 };
@@ -71,16 +73,97 @@ struct emutls_array_s
  ****************************************************************************/
 
 /* The index of a variable is global: in a flat build all task groups share
- * this data, but each variable has its own index.
+ * this data, but each variable has its own index.  g_emutls_objs[] gives
+ * the variable of each index (NULL if free), and g_emutls_arrays lists the
+ * arrays of all threads, so that the variables of an unloaded module can
+ * be freed in every thread.
  */
 
 static mutex_t g_emutls_lock = NXMUTEX_INITIALIZER;
+static struct list_node g_emutls_arrays =
+  LIST_INITIAL_VALUE(g_emutls_arrays);
+static FAR struct __emutls_object **g_emutls_objs;
 static uintptr_t g_emutls_count;
 
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: lib_emutls_release
+ *
+ * Description:
+ *   Free the copies, in every thread, of the variables whose control
+ *   objects are in [start, start + size), and free their indexes.  The
+ *   loader calls it before it frees the data of a module.
+ *
+ ****************************************************************************/
+
+static void lib_emutls_release(FAR const void *start, size_t size)
+{
+  FAR struct emutls_array_s *array;
+  uintptr_t i;
+
+  nxmutex_lock(&g_emutls_lock);
+  for (i = 0; i < g_emutls_count; i++)
+    {
+      if (g_emutls_objs[i] == NULL ||
+          (uintptr_t)g_emutls_objs[i] - (uintptr_t)start >= size)
+        {
+          continue;
+        }
+
+      list_for_every_entry(&g_emutls_arrays, array, struct emutls_array_s,
+                           node)
+        {
+          if (i < array->size)
+            {
+              lib_free(array->data[i]);
+              array->data[i] = NULL;
+            }
+        }
+
+      g_emutls_objs[i] = NULL;
+    }
+
+  nxmutex_unlock(&g_emutls_lock);
+}
+
+/****************************************************************************
+ * Name: emutls_newindex
+ *
+ * Description:
+ *   Give a variable the first free index.  The caller holds the lock.
+ *
+ ****************************************************************************/
+
+static uintptr_t emutls_newindex(FAR struct __emutls_object *obj)
+{
+  FAR struct __emutls_object **objs;
+  uintptr_t i;
+
+  for (i = 0; i < g_emutls_count && g_emutls_objs[i] != NULL; i++)
+    {
+    }
+
+  if (i == g_emutls_count)
+    {
+      objs = lib_realloc(g_emutls_objs, (i + 16) * sizeof(*objs));
+      if (objs == NULL)
+        {
+          abort();
+        }
+
+      memset(&objs[i], 0, 16 * sizeof(*objs));
+      g_emutls_objs = objs;
+      g_emutls_count = i + 16;
+    }
+
+  g_emutls_objs[i] = obj;
+  g_tls_emutls_release = lib_emutls_release;
+  return i;
+}
+
 /****************************************************************************
  * Name: emutls_alloc
  *
@@ -133,7 +216,11 @@ static void tls_emutls_destruct(void)
       return;
     }
 
+  nxmutex_lock(&g_emutls_lock);
+  list_delete(&array->node);
   tls->tl_emutls = NULL;
+  nxmutex_unlock(&g_emutls_lock);
+
   for (i = 0; i < array->size; i++)
     {
       lib_free(array->data[i]);
@@ -146,7 +233,8 @@ static void tls_emutls_destruct(void)
  * Name: emutls_getslot
  *
  * Description:
- *   Return the entry for index in the array of the calling thread.
+ *   Return the entry for index in the array of the calling thread.  The
+ *   caller holds the lock.
  *
  ****************************************************************************/
 
@@ -163,6 +251,11 @@ static FAR void **emutls_getslot(FAR struct tls_info_s 
*tls,
       return &array->data[index];
     }
 
+  if (array != NULL)
+    {
+      list_delete(&array->node);
+    }
+
   newsize = index + 16;
   newarray = lib_realloc(array, sizeof(struct emutls_array_s) +
                          (newsize - 1) * sizeof(FAR void *));
@@ -174,6 +267,7 @@ static FAR void **emutls_getslot(FAR struct tls_info_s *tls,
   memset(&newarray->data[oldsize], 0,
          (newsize - oldsize) * sizeof(FAR void *));
   newarray->size = newsize;
+  list_add_tail(&g_emutls_arrays, &newarray->node);
   tls->tl_emutls = newarray;
   g_tls_emutls_destruct = tls_emutls_destruct;
   return &newarray->data[index];
@@ -196,33 +290,40 @@ static FAR void **emutls_getslot(FAR struct tls_info_s 
*tls,
 FAR void *__wrap___emutls_get_address(FAR void *arg)
 {
   FAR struct __emutls_object *obj = arg;
+  FAR struct tls_info_s *tls = tls_get_info();
+  FAR struct emutls_array_s *array = tls->tl_emutls;
   FAR void **slot;
   uintptr_t index;
+  FAR void *ptr;
 
-  /* loc.offset is the index plus one, or 0 before the first use */
+  /* loc.offset is the index plus one, or 0 before the first use.  Only the
+   * thread changes its array: no lock for a copy that exists.
+   */
 
-  index = atomic_ptr_read_acquire(&obj->loc.offset);
-  if (index == 0)
+  index = atomic_ptr_read_acquire(&obj->loc.offset) - 1;
+  if (array != NULL && index < array->size && array->data[index] != NULL)
     {
-      nxmutex_lock(&g_emutls_lock);
-      index = obj->loc.offset;
-      if (index == 0)
-        {
-          index = ++g_emutls_count;
-          atomic_ptr_set_release(&obj->loc.offset, index);
-        }
+      return array->data[index];
+    }
 
-      nxmutex_unlock(&g_emutls_lock);
+  nxmutex_lock(&g_emutls_lock);
+  index = obj->loc.offset;
+  if (index == 0)
+    {
+      index = emutls_newindex(obj) + 1;
+      atomic_ptr_set_release(&obj->loc.offset, index);
     }
 
   index--;
-  slot = emutls_getslot(tls_get_info(), index);
+  slot = emutls_getslot(tls, index);
   if (*slot == NULL)
     {
       *slot = emutls_alloc(obj);
     }
 
-  return *slot;
+  ptr = *slot;
+  nxmutex_unlock(&g_emutls_lock);
+  return ptr;
 }
 
 /****************************************************************************
diff --git a/libs/libc/tls/tls_getinfo.c b/libs/libc/tls/tls_getinfo.c
index 759c40bf96a..578f80929cd 100644
--- a/libs/libc/tls/tls_getinfo.c
+++ b/libs/libc/tls/tls_getinfo.c
@@ -38,10 +38,13 @@
  * Public Data
  ****************************************************************************/
 
-/* tls_emutls.c sets it on its first use, so exit() does not link it */
+/* tls_emutls.c sets them on its first use, so exit() and the loader do not
+ * link it.
+ */
 
 #ifdef CONFIG_TLS_EMUTLS
 CODE void (*g_tls_emutls_destruct)(void);
+CODE void (*g_tls_emutls_release)(FAR const void *start, size_t size);
 #endif
 
 /****************************************************************************

Reply via email to