Skip to content

libc/machine: add an option to not inline the atomic functions - #22053

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
zyfeier:libc-atomic-noinline
Nov 19, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
zyfeier:libc-atomic-noinline

Conversation

@zyfeier

@zyfeier zyfeier commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Why. With LIBC_ATOMIC_IRQ or LIBC_ATOMIC_HWSPINLOCK, each atomic
function in include/nuttx/lib/arch_atomic.h is an always-inline function
that locks around the access (disables the interrupts, takes the hardware
spinlock, or calls sched_lock() in the user-space libc). Every use expands
to that sequence, including atomic_read() and atomic_set(). On armv6-m
this 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 once
in libc and calls them instead of inlining them.

How.

  • include/nuttx/lib/arch_atomic.h: with the option, ARCH_ATOMIC_SPECIFIER
    makes the functions external, and the ARCH_ATOMIC_DEFINE /
    ARCH_SYNC_DEFINE bodies are only emitted in the file that defines
    ARCH_ATOMIC_NOINLINE_DEFINE.
  • libs/libc/machine/arch_atomic.c: defines ARCH_ATOMIC_NOINLINE_DEFINE
    before including <nuttx/atomic.h>, so it holds the only copy.
  • include/nuttx/atomic.h: with the option, declares the functions with the
    existing ARCH_ATOMIC_DECLARE / ARCH_SYNC_DECLARE.
  • KASAN calls the atomic functions itself, so the out-of-line functions are
    built with nosanitize_address and noinstrument_function.
  • libs/libc/machine/Kconfig: the option depends on the IRQ or hardware
    spinlock backend, the only backends that use arch_atomic.h. With the
    toolchain backend, compiler.h defines the atomic functions as macros, so
    the prototypes cannot be declared.

Impact

  • Users / API: none. Same functions and signatures.
  • Build: new Kconfig option, off by default. No configuration changes unless
    it is enabled.
  • Hardware: with the option, one function call per atomic operation instead
    of an inlined lock sequence. Code that runs from RAM while the flash cannot
    be read must also place arch_atomic.o in RAM.
  • Documentation: Kconfig help.
  • Security: none.
  • Compatibility: loadable modules that use the atomic functions need them in
    the symbol table when the option is on.
  • Size: the saving grows with the number of atomic uses (see Testing). With
    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.c the definitions come before the prototypes in
atomic.h, so -Wmissing-prototypes would warn there. No NuttX build
enables that flag.

Testing

Host: Ubuntu x86_64, base master 917fc52, apps 6e4684b611f.

Config Result
sim:nsh build OK, 0 warnings
sim:ostest ostest_main: Exiting with status 0 (1065 s)
sim:smp build OK
qemu-armv8a:nsh_smp (4 CPUs) ostest Exiting with status 0
smartl-c906:knsh build OK
stm32l476vg-disco:knsh build OK, ksram 8136 / 8192 B
sim:ostest, LIBC_ATOMIC_IRQ forced, LIBC_ATOMIC_NOINLINE=y, KASAN build OK, 0 warnings, 7 atomic_* functions in the image; ostest_main: Exiting with status 0 (1068 s), no KASAN report
checkpatch All checks pass
$ ./nuttx            # sim:ostest, LIBC_ATOMIC_IRQ + LIBC_ATOMIC_NOINLINE + MM_KASAN
...
End of test memory usage:
user_main: Exiting
ostest_main: Exiting with status 0

Flash size, inline -> LIBC_ATOMIC_NOINLINE=y:

tree stm32l0538-disco:nsh (armv6-m, LTO) ea3131:pgnsh (no LTO)
master 64480 -> 64208 (-272) 45972 -> 46096 (+124)
+ #21804 (atomic tcb->flags) 65416 -> 64676 (-740) 46644 -> 46520 (-124)
+ #21804 + #21774 (TCB refcount) 66388 -> 65464 (-924) 47796 -> 47540 (-256)
  • stm32l0538-disco:nsh with LTO_NONE and the option links without undefined
    or multiple definitions (it only overflows the flash, as it does without LTO
    anyway).
  • nucleo-g431rb:nsh (toolchain atomics): 32428 B, unchanged.
  • Not tested: the hardware spinlock backend (no target at hand) and the CMake
    build. arch_atomic.c is built unconditionally by both Make.defs and
    CMakeLists.txt. With the hardware spinlock backend and KASAN, the
    hwspin_lock_irqsave() helpers are plain inline functions and may be
    emitted as instrumented copies in arch_atomic.o; that combination is
    not tested.

@github-actions github-actions Bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Nov 19, 2026
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


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

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
zyfeier force-pushed the libc-atomic-noinline branch from f61c0a2 to bcb2bb4 Compare November 19, 2026 12:47
@zyfeier
zyfeier marked this pull request as ready for review November 19, 2026 12:48
@xiaoxiang781216
xiaoxiang781216 merged commit 84abb47 into apache:master Nov 19, 2026
62 of 63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants