[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for `sched_getcpu` (PR #233011)
via libc-commits
libc-commits at lists.llvm.org
Fri Oct 16 21:18:40 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Connector Switch (c8ef)
<details>
<summary>Changes</summary>
This PR move `sched_getcpu` behind the centralized syscall wrappers, following `libc/docs/dev/syscall_wrapper_refactor.md`.
---
Full diff: https://github.com/llvm/llvm-project/pull/233011.diff
5 Files Affected:
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+12)
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/getcpu.h (+38)
- (modified) libc/src/sched/linux/CMakeLists.txt (+4-1)
- (modified) libc/src/sched/linux/sched_getcpu.cpp (+4-7)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+28)
``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 3f2ed1852acee2..e0f613ef32623b 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -38,6 +38,18 @@ add_header_library(
libc.include.sys_syscall
)
+add_header_library(
+ getcpu
+ HDRS
+ getcpu.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.include.sys_syscall
+)
+
add_header_library(
sched_getaffinity
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/getcpu.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/getcpu.h
new file mode 100644
index 00000000000000..0eb83a3d9d1125
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/getcpu.h
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for getcpu.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_GETCPU_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_GETCPU_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+// The third argument of the raw syscall (the obsolete tcache pointer) is left
+// out: per getcpu(2), it is unused since Linux 2.6.24 and should be specified
+// as NULL when invoking the system call directly.
+// https://man7.org/linux/man-pages/man2/getcpu.2.html
+LIBC_INLINE ErrorOr<int> getcpu(unsigned int *cpu, unsigned int *node) {
+ return syscall_checked<int>(SYS_getcpu, cpu, node, nullptr);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_GETCPU_H
diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt
index 81c29f42469173..b579d727eda8cf 100644
--- a/libc/src/sched/linux/CMakeLists.txt
+++ b/libc/src/sched/linux/CMakeLists.txt
@@ -50,7 +50,10 @@ add_entrypoint_object(
HDRS
../sched_getcpu.h
DEPENDS
- libc.src.__support.OSUtil.osutil
+ libc.src.__support.OSUtil.linux.syscall_wrappers.getcpu
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
libc.src.errno.errno
)
diff --git a/libc/src/sched/linux/sched_getcpu.cpp b/libc/src/sched/linux/sched_getcpu.cpp
index 2463f9e053d95c..7c1174d64c79f0 100644
--- a/libc/src/sched/linux/sched_getcpu.cpp
+++ b/libc/src/sched/linux/sched_getcpu.cpp
@@ -8,21 +8,18 @@
#include "src/sched/sched_getcpu.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/getcpu.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
-#include <sys/syscall.h> // For syscall numbers.
-
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, sched_getcpu, ()) {
unsigned int cpu = 0;
- int ret =
- LIBC_NAMESPACE::syscall_impl<int>(SYS_getcpu, &cpu, nullptr, nullptr);
- if (ret < 0) {
- libc_errno = -ret;
+ auto result = linux_syscalls::getcpu(&cpu, nullptr);
+ if (!result) {
+ libc_errno = result.error();
return -1;
}
return cpu;
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index f2df1ef9466183..6f2cce08d6c50d 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2712,6 +2712,21 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_osutil_linux_syscall_wrappers_getcpu",
+ hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/getcpu.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ ":__support_common",
+ ":__support_error_or",
+ ":__support_macros_config",
+ ":__support_osutil_syscall",
+ ],
+)
+
libc_support_library(
name = "__support_osutil_linux_syscall_wrappers_sched_getparam",
hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/sched_getparam.h"],
@@ -17047,6 +17062,19 @@ libc_function(
],
)
+libc_function(
+ name = "sched_getcpu",
+ srcs = ["src/sched/linux/sched_getcpu.cpp"],
+ hdrs = ["src/sched/sched_getcpu.h"],
+ deps = [
+ ":__support_common",
+ ":__support_libc_errno",
+ ":__support_macros_config",
+ ":__support_osutil_linux_syscall_wrappers_getcpu",
+ ":errno",
+ ],
+)
+
libc_function(
name = "sched_getparam",
srcs = ["src/sched/linux/sched_getparam.cpp"],
``````````
</details>
https://github.com/llvm/llvm-project/pull/233011
More information about the libc-commits
mailing list