Skip to content

libs/libc: Free the __thread copies of a module when it is unloaded. - #21470

Open
casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:tls-emutls-module-free-up
Open

casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:tls-emutls-module-free-up

Conversation

@casaroli

@casaroli casaroli commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

#20723 is merged. This PR has one commit (plus its documentation commit), on master: it changes the libc emutls code that #20723 added.

Summary

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 the static lib_emutls_release() of tls_emutls.c with the data range of the module. libc frees the copies of those variables in every thread and gives their indexes back; a new variable takes the first free index. libc calls it through a pointer that it sets when it gives the first index, so the loader does not link the code.

The documentation commit adds one sentence to the TLS guide: a copy is also freed, in every thread, when the module that defines the variable is unloaded.

Impact

  • Builds with CONFIG_TLS_EMUTLS and module loading: the copies of an unloaded module's __thread variables are freed, and their indexes reused. Before, each load took a new index and the old copies stayed until the threads exited.
  • A program that never unloads a module links nothing new (the loader calls the code through a pointer libc sets on the first index).
  • No ABI change. Not a breaking change.

Testing

Test program tlsload.c: a thread loads and unloads a module with a __thread variable 300 times, with dlopen(), and checks the value of the variable after each load. Two threads then do the same 300 times each, and the main thread 300 times more. mallinfo() gives the heap in use after each step. The test passes when the value is right and the heap in use after the last step has not grown by more than 1 KiB.

qemu-armv8a:nsh (flat, HVF), CONFIG_TLS_NELEM 4, master 6b9b216:

Heap in use (bytes) #20723 #20723 and this PR
after the first 300 loads 89888 77856
after 2 x 300 loads in two threads 89888 77856
after 300 more loads 106720 77856
result FAIL PASS

The value of the variable was right in all loads in both columns. The test does not cover exec() of a module, or a build with more than one CPU.

qemu-armv8a:knsh (HVF), with #20723: tlsgroup.c and tlsmany.c PASS, ostest exits with status 0.

Again after the review of 2026-11-14, on master 43d9de6 (with #21843) and #20723: tlsload.c PASS on qemu-armv8a:nsh (heap in use 78024 bytes after each of the three steps); on qemu-armv8a:knsh tlsgroup.c, tlsmany.c, tlsstack.c and atexittls.c PASS; ostest exits with status 0 on knsh and nsh.

Builds with EXTRAFLAGS="-Wno-cpp -Werror", each OK and with tls_emutls.c compiled: qemu-armv8a:knsh, qemu-armv8a:nsh, mps2-an500:knsh.

tools/checkpatch.sh -c -u -m -g passes.

@github-actions github-actions Bot added Area: Build system Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: avr Issues related to all AVR(8-bit or 32-bit) architectures Arch: mips Issues related to the MIPS architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: simulator Issues related to the SIMulator Arch: sparc Issues related to the SPARC architecture Arch: x86_64 Issues related to the x86_64 architecture Arch: xtensa Issues related to the Xtensa architecture Size: L The size of the change in this PR is large Board: arm Board: simulator labels Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

s698pm-dkit

  • Code: .text +64 B (+0.0%, 397,616 B)
  • Data: .bss +16 B (+0.1%, 12,139 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@casaroli
casaroli force-pushed the tls-emutls-module-free-up branch from ea94f3d to 5a36763 Compare November 9, 2026 08:19
@github-actions github-actions Bot removed the Arch: avr Issues related to all AVR(8-bit or 32-bit) architectures label Nov 9, 2026
@casaroli
casaroli force-pushed the tls-emutls-module-free-up branch 2 times, most recently from 7399305 to 04df9d1 Compare November 14, 2026 15:02
Comment thread libs/libc/tls/tls_emutls.c Resolved
@casaroli
casaroli force-pushed the tls-emutls-module-free-up branch from 04df9d1 to d893d03 Compare November 15, 2026 00:01
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 <marco.casaroli@gmail.com>
The TLS guide says that a copy is freed when its thread exits.  It now
also says that the copies are freed in every thread when the module or
shared library that defines the variable is unloaded.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli
casaroli force-pushed the tls-emutls-module-free-up branch from 1999e6e to 3672aa3 Compare November 17, 2026 07:43
@casaroli
casaroli marked this pull request as ready for review November 17, 2026 07:43
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium and removed Area: Build system Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: mips Issues related to the MIPS architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: simulator Issues related to the SIMulator Arch: sparc Issues related to the SPARC architecture Arch: x86_64 Issues related to the x86_64 architecture Arch: xtensa Issues related to the Xtensa architecture Size: L The size of the change in this PR is large Board: arm Board: simulator labels Nov 17, 2026
@casaroli
casaroli marked this pull request as draft November 17, 2026 13:04
@casaroli
casaroli marked this pull request as ready for review November 17, 2026 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants