Repository navigation
Conversation
|
casaroli
force-pushed
the
tls-emutls-module-free-up
branch
from
November 9, 2026 08:19
ea94f3d to
5a36763
Compare
casaroli
force-pushed
the
tls-emutls-module-free-up
branch
2 times, most recently
from
November 14, 2026 15:02
7399305 to
04df9d1
Compare
casaroli
force-pushed
the
tls-emutls-module-free-up
branch
from
November 15, 2026 00:01
04df9d1 to
d893d03
Compare
xiaoxiang781216
previously approved these changes
Nov 15, 2026
casaroli
force-pushed
the
tls-emutls-module-free-up
branch
from
November 15, 2026 07:56
d893d03 to
1999e6e
Compare
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
force-pushed
the
tls-emutls-module-free-up
branch
from
November 17, 2026 07:43
1999e6e to
3672aa3
Compare
casaroli
marked this pull request as ready for review
November 17, 2026 07:43
casaroli
marked this pull request as draft
November 17, 2026 13:04
casaroli
marked this pull request as ready for review
November 17, 2026 13:04
xiaoxiang781216
approved these changes
Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#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
__threadvariables 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 staticlib_emutls_release()oftls_emutls.cwith 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
CONFIG_TLS_EMUTLSand module loading: the copies of an unloaded module's__threadvariables are freed, and their indexes reused. Before, each load took a new index and the old copies stayed until the threads exited.Testing
Test program
tlsload.c: a thread loads and unloads a module with a__threadvariable 300 times, withdlopen(), 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_NELEM4, master 6b9b216: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.candtlsmany.cPASS, ostest exits with status 0.Again after the review of 2026-11-14, on master 43d9de6 (with #21843) and #20723:
tlsload.cPASS on qemu-armv8a:nsh (heap in use 78024 bytes after each of the three steps); on qemu-armv8a:knshtlsgroup.c,tlsmany.c,tlsstack.candatexittls.cPASS; ostest exits with status 0 on knsh and nsh.Builds with
EXTRAFLAGS="-Wno-cpp -Werror", each OK and withtls_emutls.ccompiled: qemu-armv8a:knsh, qemu-armv8a:nsh, mps2-an500:knsh.tools/checkpatch.sh -c -u -m -gpasses.