diff --git a/include/zephyr/kernel_structs.h b/include/zephyr/kernel_structs.h index cf7daff9a6cf7..1fc5dd7f2b4c0 100644 --- a/include/zephyr/kernel_structs.h +++ b/include/zephyr/kernel_structs.h @@ -258,8 +258,11 @@ extern atomic_t _cpus_active; */ bool z_smp_cpu_mobile(void); -#define _current_cpu ({ __ASSERT_NO_MSG(!z_smp_cpu_mobile()); \ - arch_curr_cpu(); }) +#define _current_cpu \ + ({ \ + IF_ENABLED(CONFIG_CURRENT_CPU_VALIDATE, (__ASSERT_NO_MSG(!z_smp_cpu_mobile()))); \ + arch_curr_cpu(); \ + }) #define _current k_sched_current_thread_query() #else diff --git a/kernel/Kconfig.smp b/kernel/Kconfig.smp index 33bc58a311ac3..80d15c40458ed 100644 --- a/kernel/Kconfig.smp +++ b/kernel/Kconfig.smp @@ -126,4 +126,12 @@ config TICKET_SPINLOCKS which resolves such unfairness issue at the cost of slightly increased memory footprint. +config CURRENT_CPU_VALIDATE + bool "Validate usage of _current_cpu" + depends on SMP && ASSERT + default y if MP_MAX_NUM_CPUS > 1 + help + The `_current_cpu` pointer should only ever be used in non-preemptible + contexts. When assertions are enabled in SMP systems, this option guarantees + correct `_current_cpu` usage with runtime checks. endmenu