[llvm-branch-commits] [libc] [llvm] [libc] Use a syscall wrapper for mlock2 (PR #243413)

Connector Switch via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Nov 17 02:24:37 PST 2026


https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/243413

This PR move mlock2 behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.


>From 06d75cc8820b44257b96e76c4e0783893f175e52 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Tue, 17 Nov 2026 18:22:52 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for mlock2

---
 .../linux/syscall_wrappers/CMakeLists.txt     | 13 +++++++
 .../OSUtil/linux/syscall_wrappers/mlock2.h    | 37 +++++++++++++++++++
 libc/src/sys/mman/linux/CMakeLists.txt        |  7 +++-
 libc/src/sys/mman/linux/mlock2.cpp            | 20 ++++++----
 .../OSUtil/linux/syscall_wrappers/BUILD.bazel | 16 ++++++++
 .../libc/src/sys/mman/BUILD.bazel             |  3 +-
 6 files changed, 85 insertions(+), 11 deletions(-)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/mlock2.h

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 0b2191bd49dae9..c2c9a35542d12d 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1085,6 +1085,19 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  mlock2
+  HDRS
+    mlock2.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(
   mmap
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/mlock2.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/mlock2.h
new file mode 100644
index 00000000000000..dc5537287e367f
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/mlock2.h
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 mlock2.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MLOCK2_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MLOCK2_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 {
+
+#ifdef SYS_mlock2
+LIBC_INLINE ErrorOr<int> mlock2(const void *addr, size_t len,
+                                unsigned int flags) {
+  return syscall_checked<int>(SYS_mlock2, addr, len, flags);
+}
+#endif
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MLOCK2_H
diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt
index 11c2a9e581545d..f0ac9dd5bd6be3 100644
--- a/libc/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/src/sys/mman/linux/CMakeLists.txt
@@ -136,9 +136,12 @@ add_entrypoint_object(
   HDRS
     ../mlock2.h
   DEPENDS
-    libc.include.sys_mman
+    libc.hdr.types.size_t
     libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
+    libc.src.__support.OSUtil.linux.syscall_wrappers.mlock2
+    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/mlock2.cpp b/libc/src/sys/mman/linux/mlock2.cpp
index ca7c0df9b0e788..1107bb2939eca4 100644
--- a/libc/src/sys/mman/linux/mlock2.cpp
+++ b/libc/src/sys/mman/linux/mlock2.cpp
@@ -1,29 +1,35 @@
-//===---------- Linux implementation of the mlock 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 mlock2.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/sys/mman/mlock2.h"
 
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
-
+#include "src/__support/OSUtil/linux/syscall_wrappers/mlock2.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 {
+
 #ifdef SYS_mlock2
 LLVM_LIBC_FUNCTION(int, mlock2,
                    (const void *addr, size_t len, unsigned int flags)) {
-  long ret = syscall_impl(SYS_mlock2, cpp::bit_cast<long>(addr), len, flags);
-  if (ret < 0) {
-    libc_errno = static_cast<int>(-ret);
+  auto result = linux_syscalls::mlock2(addr, len, flags);
+  if (!result) {
+    libc_errno = result.error();
     return -1;
   }
   return 0;
 }
 #endif
+
 } // 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 b3051452c2878f..5bb78ec9f9fa14 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
@@ -1531,6 +1531,22 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "mlock2",
+    hdrs = ["mlock2.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 = "mmap",
     hdrs = ["mmap.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 50def90a41d9df..eb2fb51e6a9d4b 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
@@ -64,11 +64,10 @@ libc_function(
     srcs = ["linux/mlock2.cpp"],
     hdrs = ["mlock2.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:mlock2",
         "//libc/src/__support/macros:config",
         "//libc/src/errno",
     ],



More information about the llvm-branch-commits mailing list