This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new f57187b6090 libc/sched: add user-space fast path for sched_getcpu()
f57187b6090 is described below
commit f57187b6090bc1760cdd4be50af742be1c6aab69
Author: anjiahao <[email protected]>
AuthorDate: Thu Nov 26 23:39:15 2026 +0800
libc/sched: add user-space fast path for sched_getcpu()
When CONFIG_ARCH_SCHED_GETCPU_USER_ACCESS is selected, provide
sched_getcpu() in libc that returns up_cpu_index() directly instead of
going through the syscall. The kernel sched_getcpu() and the syscall
table entry are compiled out in that case. The option depends on
BUILD_FLAT because the fast path calls into the arch code directly.
Signed-off-by: anjiahao <[email protected]>
---
arch/Kconfig | 8 ++++++++
libs/libc/sched/CMakeLists.txt | 3 ++-
libs/libc/sched/Make.defs | 2 +-
{sched => libs/libc}/sched/sched_getcpu.c | 6 ++++--
sched/sched/sched_getcpu.c | 4 ++++
syscall/syscall.csv | 2 +-
6 files changed, 20 insertions(+), 5 deletions(-)
diff --git a/arch/Kconfig b/arch/Kconfig
index 9a90b204d92..840e2bf80d6 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -717,6 +717,14 @@ config ARCH_HAVE_PERF_EVENTS_USER_ACCESS
---help---
Select if hardware allows userspace perf counter access.
+config ARCH_SCHED_GETCPU_USER_ACCESS
+ bool "Enable userspace sched_getcpu access"
+ default n
+ depends on ARCH_HAVE_MULTICPU
+ depends on BUILD_FLAT
+ ---help---
+ Enable userspace sched_getcpu access.
+
config ARCH_PERF_EVENTS
bool "Configure hardware performance counting"
default y if SCHED_CRITMONITOR || SCHED_IRQMONITOR || RPMSG_PING ||
SEGGER_SYSVIEW
diff --git a/libs/libc/sched/CMakeLists.txt b/libs/libc/sched/CMakeLists.txt
index 9fb42c09fe2..ab47705ded9 100644
--- a/libs/libc/sched/CMakeLists.txt
+++ b/libs/libc/sched/CMakeLists.txt
@@ -29,7 +29,8 @@ set(SRCS
task_setcanceltype.c
task_testcancel.c
task_gettid.c
- task_getpid.c)
+ task_getpid.c
+ sched_getcpu.c)
if(CONFIG_SMP)
list(APPEND SRCS sched_cpucount.c)
diff --git a/libs/libc/sched/Make.defs b/libs/libc/sched/Make.defs
index f3ffae413c2..31b9431dbd4 100644
--- a/libs/libc/sched/Make.defs
+++ b/libs/libc/sched/Make.defs
@@ -24,7 +24,7 @@
CSRCS += sched_getprioritymax.c sched_getprioritymin.c
CSRCS += task_cancelpt.c task_setcancelstate.c task_setcanceltype.c
-CSRCS += task_testcancel.c task_gettid.c task_getpid.c
+CSRCS += task_testcancel.c task_gettid.c task_getpid.c sched_getcpu.c
CSRCS += clock_getcpuclockid.c
ifeq ($(CONFIG_SMP),y)
diff --git a/sched/sched/sched_getcpu.c b/libs/libc/sched/sched_getcpu.c
similarity index 95%
copy from sched/sched/sched_getcpu.c
copy to libs/libc/sched/sched_getcpu.c
index 6d6b241186f..a3862aa52d2 100644
--- a/sched/sched/sched_getcpu.c
+++ b/libs/libc/sched/sched_getcpu.c
@@ -1,5 +1,5 @@
/****************************************************************************
- * sched/sched/sched_getcpu.c
+ * libs/libc/sched/sched_getcpu.c
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -29,7 +29,7 @@
#include <sched.h>
#include <nuttx/arch.h>
-#include "sched/sched.h"
+#ifdef CONFIG_ARCH_SCHED_GETCPU_USER_ACCESS
/****************************************************************************
* Public Functions
@@ -65,3 +65,5 @@ int sched_getcpu(void)
{
return up_cpu_index(); /* Does not fail */
}
+
+#endif /* CONFIG_ARCH_SCHED_GETCPU_USER_ACCESS */
diff --git a/sched/sched/sched_getcpu.c b/sched/sched/sched_getcpu.c
index 6d6b241186f..a64c6556df1 100644
--- a/sched/sched/sched_getcpu.c
+++ b/sched/sched/sched_getcpu.c
@@ -31,6 +31,8 @@
#include "sched/sched.h"
+#ifndef CONFIG_ARCH_SCHED_GETCPU_USER_ACCESS
+
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -65,3 +67,5 @@ int sched_getcpu(void)
{
return up_cpu_index(); /* Does not fail */
}
+
+#endif /* CONFIG_ARCH_SCHED_GETCPU_USER_ACCESS */
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index 6bd67e635ca..904bae3691b 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -150,7 +150,7 @@
"rmmod","nuttx/module.h","defined(CONFIG_MODULE)","int","FAR void *"
"sched_backtrace","sched.h","defined(CONFIG_SCHED_BACKTRACE)","int","pid_t","FAR
void **","int","int"
"sched_getaffinity","sched.h","defined(CONFIG_SMP)","int","pid_t","size_t","FAR
cpu_set_t *"
-"sched_getcpu","sched.h","","int"
+"sched_getcpu","sched.h","!defined(CONFIG_ARCH_SCHED_GETCPU_USER_ACCESS)","int"
"sched_getparam","sched.h","","int","pid_t","FAR struct sched_param *"
"sched_getscheduler","sched.h","","int","pid_t"
"sched_lock","sched.h","","void"