Skip to content

Commit bcb2bb4

Browse files
committed
libc/machine: add an option to not inline the atomic functions
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>
1 parent 917fc52 commit bcb2bb4

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎include/nuttx/atomic.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,16 @@ ARCH_SYNC_DECLARE(sync, uint16_t, 2)
406406
ARCH_ATOMIC_DECLARE(atomic, int64_t, 8)
407407
ARCH_SYNC_DECLARE(sync, uint64_t, 8)
408408
# endif
409+
#elif defined(CONFIG_LIBC_ATOMIC_NOINLINE)
410+
ARCH_ATOMIC_DECLARE(atomic, uint8_t, 1)
411+
ARCH_ATOMIC_DECLARE(atomic, uint16_t, 2)
412+
ARCH_ATOMIC_DECLARE(atomic, int32_t, 4)
413+
ARCH_ATOMIC_DECLARE(atomic, int64_t, 8)
414+
415+
ARCH_SYNC_DECLARE(sync, uint8_t, 1)
416+
ARCH_SYNC_DECLARE(sync, uint16_t, 2)
417+
ARCH_SYNC_DECLARE(sync, uint32_t, 4)
418+
ARCH_SYNC_DECLARE(sync, uint64_t, 8)
409419
#endif
410420

411421
#if defined(__cplusplus)

‎include/nuttx/lib/arch_atomic.h‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@
4545
****************************************************************************/
4646

4747
#ifndef ARCH_ATOMIC_SPECIFIER
48-
# define ARCH_ATOMIC_SPECIFIER static always_inline_function
48+
# ifdef CONFIG_LIBC_ATOMIC_NOINLINE
49+
50+
/* KASAN calls the atomic functions, so they must not be instrumented */
51+
52+
# define ARCH_ATOMIC_SPECIFIER \
53+
noinline_function nosanitize_address noinstrument_function
54+
# else
55+
# define ARCH_ATOMIC_SPECIFIER static always_inline_function
56+
# endif
4957
#endif
5058

5159
#if !defined(CONFIG_BUILD_FLAT) && !defined(__KERNEL__)
@@ -276,6 +284,8 @@ extern struct hwspinlock_dev_s g_atomic_hwspinlock;
276284
* Inline Functions
277285
****************************************************************************/
278286

287+
# if !defined(CONFIG_LIBC_ATOMIC_NOINLINE) || \
288+
defined(ARCH_ATOMIC_NOINLINE_DEFINE)
279289
ARCH_ATOMIC_DEFINE(atomic, uint8_t, 1)
280290
ARCH_ATOMIC_DEFINE(atomic, uint16_t, 2)
281291
ARCH_ATOMIC_DEFINE(atomic, int32_t, 4)
@@ -285,6 +295,7 @@ ARCH_SYNC_DEFINE(sync, uint8_t, 1)
285295
ARCH_SYNC_DEFINE(sync, uint16_t, 2)
286296
ARCH_SYNC_DEFINE(sync, uint32_t, 4)
287297
ARCH_SYNC_DEFINE(sync, uint64_t, 8)
298+
# endif
288299
#endif
289300

290301
#endif /* __INCLUDE_NUTTX_LIB_ARCH_ATOMIC_H */

‎libs/libc/machine/Kconfig‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ config LIBC_ATOMIC_IRQ
4848
---help---
4949
atomic function by irq disable/enable
5050

51+
config LIBC_ATOMIC_NOINLINE
52+
bool "Do not inline the atomic functions"
53+
default n
54+
depends on LIBC_ATOMIC_IRQ || LIBC_ATOMIC_HWSPINLOCK
55+
---help---
56+
Build the atomic functions of the IRQ and hardware spinlock
57+
backends once in libc and call them, instead of inlining them at
58+
each use. This reduces the code size when the atomic functions
59+
are used at many places, at the cost of a call for each atomic
60+
operation. Code that runs from RAM while the flash cannot be
61+
read must also place arch_atomic.o in RAM, and loadable modules
62+
that use the atomic functions need them in the symbol table.
63+
5164
config LIBC_ATOMIC_TOOLCHAIN
5265
bool
5366
default y if !LIBC_ATOMIC_ARCH && !LIBC_ATOMIC_HWSPINLOCK && !LIBC_ATOMIC_IRQ

‎libs/libc/machine/arch_atomic.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <nuttx/config.h>
2828

29+
#define ARCH_ATOMIC_NOINLINE_DEFINE
2930
#include <nuttx/atomic.h>
3031

3132
#undef ARCH_ATOMIC_SPECIFIER

0 commit comments

Comments
 (0)