Repository navigation
libc/machine: add an option to not inline the atomic functions - #22053
Merged
Merged
Conversation
|
With LIBC_ATOMIC_IRQ or LIBC_ATOMIC_HWSPINLOCK, each atomic function is an always-inline function that disables the interrupts (or takes the hardware spinlock) around the access. Each use expands to this sequence, also atomic_read() and atomic_set(). On armv6-m that is about 14 bytes at each use, against a few bytes for a call. Add LIBC_ATOMIC_NOINLINE. With it, atomic.h only declares the atomic functions, and arch_atomic.c builds them once in libc. KASAN calls the atomic functions itself, so they are built without KASAN and function instrumentation. The option is off by default, so the existing configurations keep the inline functions. Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com>
zyfeier
force-pushed
the
libc-atomic-noinline
branch
from
November 19, 2026 12:47
f61c0a2 to
bcb2bb4
Compare
zyfeier
marked this pull request as ready for review
November 19, 2026 12:48
zyfeier
requested review from
masayuki2009,
tmedicci and
xiaoxiang781216
as code owners
November 19, 2026 12:48
GUIDINGLI
approved these changes
Nov 19, 2026
xiaoxiang781216
approved these changes
Nov 19, 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.
Summary
Why. With
LIBC_ATOMIC_IRQorLIBC_ATOMIC_HWSPINLOCK, each atomicfunction in
include/nuttx/lib/arch_atomic.his an always-inline functionthat locks around the access (disables the interrupts, takes the hardware
spinlock, or calls
sched_lock()in the user-space libc). Every use expandsto that sequence, including
atomic_read()andatomic_set(). On armv6-mthis is about 14 bytes at each use, against a few bytes for a call.
Small-flash boards pay this at every atomic use, and the number of uses grows
as more kernel state becomes atomic.
What. Add
LIBC_ATOMIC_NOINLINE, which builds the atomic functions oncein libc and calls them instead of inlining them.
How.
include/nuttx/lib/arch_atomic.h: with the option,ARCH_ATOMIC_SPECIFIERmakes the functions external, and the
ARCH_ATOMIC_DEFINE/ARCH_SYNC_DEFINEbodies are only emitted in the file that definesARCH_ATOMIC_NOINLINE_DEFINE.libs/libc/machine/arch_atomic.c: definesARCH_ATOMIC_NOINLINE_DEFINEbefore including
<nuttx/atomic.h>, so it holds the only copy.include/nuttx/atomic.h: with the option, declares the functions with theexisting
ARCH_ATOMIC_DECLARE/ARCH_SYNC_DECLARE.built with
nosanitize_addressandnoinstrument_function.libs/libc/machine/Kconfig: the option depends on the IRQ or hardwarespinlock backend, the only backends that use
arch_atomic.h. With thetoolchain backend,
compiler.hdefines the atomic functions as macros, sothe prototypes cannot be declared.
Impact
it is enabled.
of an inlined lock sequence. Code that runs from RAM while the flash cannot
be read must also place
arch_atomic.oin RAM.the symbol table when the option is on.
few uses, the out-of-line copies can cost more than they save, which is why
the option is off by default.
In
arch_atomic.cthe definitions come before the prototypes inatomic.h, so-Wmissing-prototypeswould warn there. No NuttX buildenables that flag.
Testing
Host: Ubuntu x86_64, base master 917fc52, apps 6e4684b611f.
ostest_main: Exiting with status 0(1065 s)Exiting with status 0LIBC_ATOMIC_IRQforced,LIBC_ATOMIC_NOINLINE=y, KASANatomic_*functions in the image;ostest_main: Exiting with status 0(1068 s), no KASAN reportFlash size, inline ->
LIBC_ATOMIC_NOINLINE=y:LTO_NONEand the option links without undefinedor multiple definitions (it only overflows the flash, as it does without LTO
anyway).
build.
arch_atomic.cis built unconditionally by both Make.defs andCMakeLists.txt. With the hardware spinlock backend and KASAN, the
hwspin_lock_irqsave()helpers are plain inline functions and may beemitted as instrumented copies in
arch_atomic.o; that combination isnot tested.