[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for msync (PR #243784)
Connector Switch via libc-commits
libc-commits at lists.llvm.org
Wed Nov 18 02:26:50 PST 2026
https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/243784
This PR move msync behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.
>From 0bf0286633bbd81cc4201731e472f500fb1c3d41 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 18 Nov 2026 18:25:06 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for msync
---
.../linux/syscall_wrappers/CMakeLists.txt | 13 +++++++
.../OSUtil/linux/syscall_wrappers/msync.h | 34 +++++++++++++++++++
libc/src/sys/mman/linux/CMakeLists.txt | 8 +++--
libc/src/sys/mman/linux/msync.cpp | 20 +++++++----
.../OSUtil/linux/syscall_wrappers/BUILD.bazel | 16 +++++++++
.../libc/src/sys/mman/BUILD.bazel | 3 +-
6 files changed, 82 insertions(+), 12 deletions(-)
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/msync.h
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index cd2f92ce59e718..c332cf2cb65826 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1137,6 +1137,19 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ msync
+ HDRS
+ msync.h
+ DEPENDS
+ libc.hdr.types.size_t
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
add_header_library(
munmap
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/msync.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/msync.h
new file mode 100644
index 00000000000000..3a53a2367abb61
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/msync.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 msync.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MSYNC_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MSYNC_H
+
+#include "hdr/types/size_t.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 {
+
+LIBC_INLINE ErrorOr<int> msync(void *addr, size_t len, int flags) {
+ return syscall_checked<int>(SYS_msync, addr, len, flags);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MSYNC_H
diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt
index bfdfa3ee6c55d7..4180ba24ce7aaf 100644
--- a/libc/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/src/sys/mman/linux/CMakeLists.txt
@@ -192,9 +192,11 @@ add_entrypoint_object(
HDRS
../msync.h
DEPENDS
- libc.include.sys_mman
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
+ libc.hdr.types.size_t
+ libc.src.__support.OSUtil.linux.syscall_wrappers.msync
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
libc.src.errno.errno
)
diff --git a/libc/src/sys/mman/linux/msync.cpp b/libc/src/sys/mman/linux/msync.cpp
index 650678bcb36e0f..465f7273c86dfe 100644
--- a/libc/src/sys/mman/linux/msync.cpp
+++ b/libc/src/sys/mman/linux/msync.cpp
@@ -1,26 +1,32 @@
-//===---------- Linux implementation of the msync function ----------------===//
+//===----------------------------------------------------------------------===//
//
// 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
+/// Linux implementation of msync.
+///
+//===----------------------------------------------------------------------===//
#include "src/sys/mman/msync.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
-
+#include "src/__support/OSUtil/linux/syscall_wrappers/msync.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, msync, (void *addr, size_t len, int flags)) {
- long ret = syscall_impl(SYS_msync, cpp::bit_cast<long>(addr), len, flags);
- if (ret < 0) {
- libc_errno = static_cast<int>(-ret);
+ auto result = linux_syscalls::msync(addr, len, flags);
+ if (!result) {
+ libc_errno = result.error();
return -1;
}
return 0;
}
+
} // namespace LIBC_NAMESPACE_DECL
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
index 5e1c3cd4fb86d3..b9cb62558e1291 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
@@ -1610,6 +1610,22 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "msync",
+ hdrs = ["msync.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ "//libc/hdr/types:size_t",
+ "//libc/src/__support:common",
+ "//libc/src/__support:error_or",
+ "//libc/src/__support/OSUtil:syscall",
+ "//libc/src/__support/macros:config",
+ ],
+)
+
libc_support_library(
name = "munmap",
hdrs = ["munmap.h"],
diff --git a/utils/bazel/llvm-project-overlay/libc/src/sys/mman/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/sys/mman/BUILD.bazel
index 63475f9e27e553..4fa25b630ed9bf 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/sys/mman/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/sys/mman/BUILD.bazel
@@ -140,11 +140,10 @@ libc_function(
srcs = ["linux/msync.cpp"],
hdrs = ["msync.h"],
deps = [
- "//libc/hdr:sys_mman_macros",
"//libc/hdr/types:size_t",
"//libc/src/__support:common",
"//libc/src/__support:libc_errno",
- "//libc/src/__support/OSUtil:syscall",
+ "//libc/src/__support/OSUtil/linux/syscall_wrappers:msync",
"//libc/src/__support/macros:config",
"//libc/src/errno",
],
More information about the libc-commits
mailing list