Repository navigation
libs/libc/tls: Keep a copy of each __thread variable per thread. - #20723
Conversation
|
6e64782 to
e79c99e
Compare
e79c99e to
4600c89
Compare
a7a9768 to
8df6bd7
Compare
|
This PR is split to make the review easier. Review order:
The code is the same as before the split. |
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s): |
|
@casaroli need rebase the patch to resolve the ci break. |
|
Done. |
…gets. atomic_ptr_t was atomic_t, 32 bits, also where a pointer is 16 bits, as on AVR. atomic_ptr_read() of a variable as wide as a pointer then read two more bytes, and atomic_ptr_set() wrote them. When UINTPTR_MAX <= UINT16_MAX, atomic_ptr_t is now a 16-bit atomic, and the atomic_ptr_*() operations are the GCC __atomic builtins on 2 bytes. Where the CPU has no atomic instructions, as on AVR, GCC calls the __atomic_*_2 functions that libs/libc/machine/arch_atomic.c already provides. Nothing uses atomic_ptr_t yet, so no user changes. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
A toolchain without native TLS, such as the Arm GNU toolchain for aarch64-none-elf, compiles __thread variables into calls to __emutls_get_address(). That toolchain's libgcc is built with the single thread model, so its __emutls_get_address() keeps one copy of each variable for the whole program: all threads read and write the same copy. Add __wrap___emutls_get_address() and __wrap___emutls_register_common() to libc, and link the image with "--wrap=__emutls_get_address --wrap=__emutls_register_common" (tools/Config.mk and libs/libc/tls/CMakeLists.txt; CMake also wraps the user image of a protected build). The calls then go to libc whatever the order of the libraries, and the linker never takes libgcc's copy. With LTO the linker sees these calls only in the code made after it has chosen the archive members, so an LTO link also gets "-u" for the two __wrap_ names. EMUTLS_LDFLAGS gives the same options in the form for $(LD). The Toolchain.defs of each architecture that can have CONFIG_TLS_EMUTLS, and the sim Make.defs, add it to LDELFFLAGS and LDMODULEFLAGS, and the export keeps it, so the links of modules and programs get it too. The system symbol table (libc.csv) exports the __wrap_ names, which a module linked with --wrap refers to. The real names are weak aliases for a link without --wrap, such as the user image of a protected build or a program built outside NuttX: libc comes before libgcc there, and in a --whole-archive link libgcc's strong definitions win without a conflict. CONFIG_TLS_EMUTLS (default y) builds this for the GCC and clang toolchains when libgcc is the builtin library (BUILTIN_TOOLCHAIN; compiler-rt has its own copy per thread), but not for armclang or GHS. The sim has it with GCC and the GNU linker (Linux, and MinGW, whose GCC uses emulated TLS): the sim links NuttX into nuttx.rel with --wrap, and nuttx-names.in renames the weak real names, so the host program keeps its own libgcc copy. The macOS and MSVC linkers have no --wrap, so the sim there does not have it. The control object and the parameters use the types of libgcc's emutls.c, as GCC declares its built-in __emutls_register_common(): a machine word for the size and the alignment and an integer as wide as a pointer for the index, which is read and set with the atomic_ptr_t operations (16 bits on AVR, with the previous commit). Each variable gets a global index on its first use. Each thread keeps its copies in an array that its TLS info points to; a copy is made on the first use from the variable's template, or zeroed. exit(), quick_exit() and pthread_exit() free the copies after the exit functions and the TLS destructors, which may still use them. They call the code through a pointer that it sets on its first use, so a program without __thread variables does not link it. Without CONFIG_TLS_EMUTLS there is no pointer and no call. A thread that gets a new copy of the TLS info of its parent (vfork()) starts with no copies. Nothing depends on pthread keys or CONFIG_TLS_NELEM. With LTO every program links this code (about 800 bytes on Cortex-M; see the -u above), so the option has a prompt: nucleo-g431rb:nsh, the one LTO configuration without room for it, disables it. The --wrap options for ELF programs and modules (the sim Make.defs part of this commit) come to the CMake build in a separate change. [skip-cmake-parity] Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
libc now provides its own __emutls_get_address(), so a __thread variable has one copy per thread also with a single-thread libgcc. The TLS guide said that libgcc's emutls implements the keyword. It now also says when the copies are freed, that the links use --wrap, and that code loaded at run time finds the libc version in the system symbol table. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
|
@casaroli @xiaoxiang781216 @GUIDINGLI This change costs every LTO image ~1.5 KB of flash, even when nothing in the image uses Measured on
That is +1456 B (+6.7%) of flash for a feature the image doesn't use. The smallest image I have (no console, no files) goes from 8.8 KB to 10.1 KB. Without LTO the difference is only 12 B, so the cost comes only from the forced The PR description mentions ~800 B and one board that overflowed, so the cost was known. But the option defaults to Can we at least make it: Even better would be to link this code only when |
@casaroli please take a look |
Summary
A toolchain without native TLS, such as the Arm GNU toolchain for
aarch64-none-elf, compiles__threadvariables into calls to__emutls_get_address(). That toolchain's libgcc keeps one copy of each variable for the whole program: all threads read and write the same copy.This PR adds
__wrap___emutls_get_address()and__wrap___emutls_register_common()to libc, and links the image with--wrap=__emutls_get_address --wrap=__emutls_register_common(Make and CMake). The calls then go to libc whatever the order of the libraries, and the linker never takes libgcc's copy. With LTO the linker sees these calls only in the code made after it has chosen the archive members, so an LTO link also gets-ufor the two__wrap_names. TheToolchain.defsof each architecture that can haveCONFIG_TLS_EMUTLS, and the simMake.defs, add it toLDELFFLAGSandLDMODULEFLAGS, and the export keeps it. Each thread keeps its copies in an array that its TLS info points to;exit(),quick_exit()andpthread_exit()free them after the exit functions and the TLS destructors. It does not use pthread keys and does not needCONFIG_TLS_NELEM.CONFIG_TLS_EMUTLS(default y, with a prompt for a board that has no space for it) builds this for the GCC and clang toolchains when libgcc is the builtin library (CONFIG_BUILTIN_TOOLCHAIN; compiler-rt has its own copy per thread), but not for armclang or GHS. The index is read and set with theatomic_ptr_toperations of #21843. The sim has it with GCC and the GNU linker (Linux, MinGW); the macOS and MSVC linkers have no--wrap, so the sim there does not have it.The first commit makes
atomic_ptr_t16 bits where a pointer is 16 bits, as on AVR. It wasatomic_t, 32 bits, soatomic_ptr_read()of the 16-bit index of GCC's control object also read two more bytes. On such a target theatomic_ptr_*()operations are now the GCC__atomicbuiltins on 2 bytes; on AVR they call the__atomic_*_2functions thatarch_atomic.calready has. Nothing else usesatomic_ptr_tyet. With it, AVR gets the per-thread copies too.The documentation commit updates
thread_local_storage.rst: libc's per-thread emutls, when to disableCONFIG_TLS_EMUTLS, and that the links use--wrap.The 5-board libgcc order fix is in #21469. This PR needs it: without it, the user image of a protected build of
lm3s6965-ek:qemu-protectedandfvp-armv8r:pnshlinks libgcc'semutls.oand not the version in libc. Seen in the symbols of both images and in thelm3s6965-eklink map.Impact
__threadvariables are per thread in flat, protected and kernel builds, with any value ofCONFIG_TLS_NELEM, on every architecture whose GCC has no native TLS. Without LTO, an image without__threadvariables links only two pointers and their checks. With LTO it links the emutls code (about 800 bytes on Cortex-M):nucleo-g431rb:nsh, the one LTO configuration without room for it, disablesCONFIG_TLS_EMUTLSand keeps the behaviour of master. No ABI change. Not a breaking change.Testing
Test programs
tlsgroup.c(two threads, a copy per thread),tlsmany.c(twelve__threadvariables withCONFIG_TLS_NELEM4) andtlsstack.c.qemu-armv8a:knsh (kernel) and qemu-armv8a:nsh (flat), under HVF, master 6b9b216:
CONFIG_TLS_ALIGNED, tlsstackostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh with this PR.
Again after the review of 2026-11-14, on master 43d9de6 (with #21843):
tlsgroup.cas a loadable module (it calls__wrap___emutls_get_address): PASS. qemu-armv8a:knsh,tlsgroup.cas a program built from the export (LDELFFLAGSandLDMODULEFLAGShave the--wrapoptions): PASS.tlsgroup.c,tlsmany.c,tlsstack.candatexittls.cPASS; ostest exits with status 0 on knsh and nsh.CONFIG_TLS_EMUTLS=yon qemu-armv8a:nsh and mps2-an500:knsh (CONFIG_BUILTIN_TOOLCHAIN); not set on mps3-an547:clang (CONFIG_BUILTIN_COMPILER_RT).nucleo-g431rb:nshhas 219 bytes free on master and overflowed by 580 bytes with this code, so it disablesCONFIG_TLS_EMUTLS(text+data then the same as master;savedefconfiggives the same defconfig; Make and CMake). CMakenucleo-f302r8:cansock(LTO) links with the-uoptions and the__wrap_functions.Again after the review comments on AVR and the stm32g4 defconfigs, on master 43d9de6:
EXTRAFLAGS="-Wno-cpp -Werror": the 12 AVR configurations that build on master build withCONFIG_TLS_EMUTLS=yand 0 warnings (amber:hello, arduino-mega2560:hello/nsh, elegoo-mega2560r3:nsh, mega1284p-xplained:hello/nsh, moteino-mega:hello/nsh, micropendous3:hello, teensy-2.0:hello/nsh/usbmsc). Without__threadvariables, the 10 images measured grow by 28 bytes of text (the exit hook). breadxavr:nsh/ws2812 do not build on master or with this PR (the image has no device specs for avr128da28). The AVR32 configurations (avr32dev1:nsh/ostest, mizar32a:nsh) do not get the option (their toolchain choices do not selectARCH_TOOLCHAIN_GNU), and their images are the same as on master.hello_main.creplaced by a two-thread__threadtest: the image calls__wrap___emutls_get_address()of libc, and libgcc'semutls.ois not linked;__atomic_load_2()and__atomic_store_2()come fromarch_atomic.c. Not run: NuttX does not start on qemu-system-avr (master also waits innxsem_wait()with no interrupt), and no AVR board was available.b-g431b-esc1:nshandnucleo-g431kb:nshnow keepCONFIG_TLS_EMUTLS: with LTO they link with 128821 and 128789 bytes of text+data in 128 KiB of flash.nucleo-g431rb:nshkeeps it disabled. On master e1eca35 the CMake build ofnucleo-g431rb:nshoverflows by 20 bytes without this PR.tlsload.c(libs/libc: Free the __thread copies of a module when it is unloaded. #21470) and qemu-armv8a:knshtlsgroup.c,tlsmany.c,tlsstack.c,atexittls.cPASS; ostest exits with status 0 on knsh and nsh; mps2-an500:knsh with-Werror: 0 warnings.Builds with
EXTRAFLAGS="-Wno-cpp -Werror", each OK: qemu-armv8a:knsh, qemu-armv8a:nsh, mps2-an500:knsh, rv-virt:knsh64, rv-virt:pnsh64, lm3s6965-ek:qemu-protected, and withtls_emutls.ccompiled in each of these; nucleo-g431rb:nsh and sim:nsh (macOS), which do not compile it.The second commit has
[skip-cmake-parity]: the--wrapoptions for ELF programs and modules (the simMake.defspart) come to CMake in #20812.tools/checkpatch.sh -c -u -m -gpasses.