zyfeier opened a new pull request, #22053:
URL: https://github.com/apache/nuttx/pull/22053
## 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, and not on `LIBC_ATOMIC_ARCH`. It is not offered with
`MM_KASAN` and the hardware spinlock backend, because the
`hwspin_lock_irqsave()` helpers are plain inline functions and could be
emitted as instrumented copies.
## 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 917fc520477, 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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]