Skip to content

Commit 0d18407

Browse files
committed
kernel/smp: add a Kconfig to disable validation of _current_cpu
The assert validation was added in eefd3da to ensure that `_current_cpu` is used safely. However, since `_current_cpu` is used frequently in the kernel/scheduler, especially when SMP is enabled, the assertion has a negative impact on the performance. This commit introduces a Kconfig to disable just the assert validation of the `_current_cpu`, without having to disable `CONFIG_ASSERT` entirely. Signed-off-by: Yong Cong Sin <[email protected]>
1 parent c3466b1 commit 0d18407

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

include/zephyr/kernel_structs.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,11 @@ extern atomic_t _cpus_active;
258258
*/
259259
bool z_smp_cpu_mobile(void);
260260

261-
#define _current_cpu ({ __ASSERT_NO_MSG(!z_smp_cpu_mobile()); \
262-
arch_curr_cpu(); })
261+
#define _current_cpu \
262+
({ \
263+
IF_ENABLED(CONFIG_CURRENT_CPU_VALIDATE, (__ASSERT_NO_MSG(!z_smp_cpu_mobile()))); \
264+
arch_curr_cpu(); \
265+
})
263266
#define _current k_sched_current_thread_query()
264267

265268
#else

kernel/Kconfig.smp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,12 @@ config TICKET_SPINLOCKS
126126
which resolves such unfairness issue at the cost of slightly
127127
increased memory footprint.
128128

129+
config CURRENT_CPU_VALIDATE
130+
bool "Validate usage of _current_cpu"
131+
depends on SMP && ASSERT
132+
default y if MP_MAX_NUM_CPUS > 1
133+
help
134+
The `_current_cpu` pointer should only ever be used in non-preemptible
135+
contexts. When assertions are enabled in SMP systems, this option guarantees
136+
correct `_current_cpu` usage with runtime checks.
129137
endmenu

0 commit comments

Comments
 (0)