Skip to content

Commit 2492321

Browse files
[libc] implement mlock/mlock2/munlock/mlockall/munlockall (#79645)
fixes #79336 Co-authored-by: Sirui Mu <[email protected]>
1 parent 404af14 commit 2492321

File tree

20 files changed

+607
-7
lines changed

20 files changed

+607
-7
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ set(TARGET_LIBC_ENTRYPOINTS
144144
libc.src.sys.mman.munmap
145145
libc.src.sys.mman.posix_madvise
146146
libc.src.sys.mman.mincore
147+
libc.src.sys.mman.mlock
148+
libc.src.sys.mman.mlock2
149+
libc.src.sys.mman.munlock
150+
libc.src.sys.mman.mlockall
151+
libc.src.sys.mman.munlockall
147152

148153
# sys/random.h entrypoints
149154
libc.src.sys.random.getrandom

libc/config/linux/riscv/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ set(TARGET_LIBC_ENTRYPOINTS
150150
libc.src.sys.mman.munmap
151151
libc.src.sys.mman.posix_madvise
152152
libc.src.sys.mman.mincore
153+
libc.src.sys.mman.mlock
154+
libc.src.sys.mman.mlock2
155+
libc.src.sys.mman.munlock
156+
libc.src.sys.mman.mlockall
157+
libc.src.sys.mman.munlockall
153158

154159
# sys/random.h entrypoints
155160
libc.src.sys.random.getrandom

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ set(TARGET_LIBC_ENTRYPOINTS
157157
libc.src.sys.mman.munmap
158158
libc.src.sys.mman.posix_madvise
159159
libc.src.sys.mman.mincore
160+
libc.src.sys.mman.mlock
161+
libc.src.sys.mman.mlock2
162+
libc.src.sys.mman.munlock
163+
libc.src.sys.mman.mlockall
164+
libc.src.sys.mman.munlockall
160165

161166
# sys/random.h entrypoints
162167
libc.src.sys.random.getrandom

libc/spec/linux.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ def Linux : StandardSpec<"Linux"> {
9494
ArgSpec<UnsignedCharPtr>,
9595
]
9696
>,
97+
FunctionSpec<
98+
"mlock2",
99+
RetValSpec<IntType>,
100+
[
101+
ArgSpec<VoidPtr>,
102+
ArgSpec<SizeTType>,
103+
ArgSpec<UnsignedIntType>,
104+
]
105+
>,
97106
] // Functions
98107
>;
99108

libc/spec/posix.td

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,26 @@ def POSIX : StandardSpec<"POSIX"> {
289289
ArgSpec<SizeTType>,
290290
ArgSpec<IntType>]
291291
>,
292+
FunctionSpec<
293+
"mlock",
294+
RetValSpec<IntType>,
295+
[ArgSpec<VoidPtr>, ArgSpec<SizeTType>]
296+
>,
297+
FunctionSpec<
298+
"munlock",
299+
RetValSpec<IntType>,
300+
[ArgSpec<VoidPtr>, ArgSpec<SizeTType>]
301+
>,
302+
FunctionSpec<
303+
"mlockall",
304+
RetValSpec<IntType>,
305+
[ArgSpec<IntType>]
306+
>,
307+
FunctionSpec<
308+
"munlockall",
309+
RetValSpec<IntType>,
310+
[ArgSpec<VoidType>]
311+
>,
292312
]
293313
>;
294314

libc/src/sys/mman/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,38 @@ add_entrypoint_object(
4343
DEPENDS
4444
.${LIBC_TARGET_OS}.mincore
4545
)
46+
47+
add_entrypoint_object(
48+
mlock
49+
ALIAS
50+
DEPENDS
51+
.${LIBC_TARGET_OS}.mlock
52+
)
53+
54+
add_entrypoint_object(
55+
mlock2
56+
ALIAS
57+
DEPENDS
58+
.${LIBC_TARGET_OS}.mlock2
59+
)
60+
61+
add_entrypoint_object(
62+
munlock
63+
ALIAS
64+
DEPENDS
65+
.${LIBC_TARGET_OS}.munlock
66+
)
67+
68+
add_entrypoint_object(
69+
mlockall
70+
ALIAS
71+
DEPENDS
72+
.${LIBC_TARGET_OS}.mlockall
73+
)
74+
75+
add_entrypoint_object(
76+
munlockall
77+
ALIAS
78+
DEPENDS
79+
.${LIBC_TARGET_OS}.munlockall
80+
)

libc/src/sys/mman/linux/CMakeLists.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,68 @@ add_entrypoint_object(
7474
libc.src.__support.OSUtil.osutil
7575
libc.src.errno.errno
7676
)
77+
78+
add_entrypoint_object(
79+
mlock
80+
SRCS
81+
mlock.cpp
82+
HDRS
83+
../mlock.h
84+
DEPENDS
85+
libc.include.sys_mman
86+
libc.include.sys_syscall
87+
libc.src.__support.OSUtil.osutil
88+
libc.src.errno.errno
89+
)
90+
91+
add_entrypoint_object(
92+
mlock2
93+
SRCS
94+
mlock2.cpp
95+
HDRS
96+
../mlock2.h
97+
DEPENDS
98+
libc.include.sys_mman
99+
libc.include.sys_syscall
100+
libc.src.__support.OSUtil.osutil
101+
libc.src.errno.errno
102+
)
103+
104+
add_entrypoint_object(
105+
munlock
106+
SRCS
107+
munlock.cpp
108+
HDRS
109+
../munlock.h
110+
DEPENDS
111+
libc.include.sys_mman
112+
libc.include.sys_syscall
113+
libc.src.__support.OSUtil.osutil
114+
libc.src.errno.errno
115+
)
116+
117+
add_entrypoint_object(
118+
mlockall
119+
SRCS
120+
mlockall.cpp
121+
HDRS
122+
../mlockall.h
123+
DEPENDS
124+
libc.include.sys_mman
125+
libc.include.sys_syscall
126+
libc.src.__support.OSUtil.osutil
127+
libc.src.errno.errno
128+
)
129+
130+
add_entrypoint_object(
131+
munlockall
132+
SRCS
133+
munlockall.cpp
134+
HDRS
135+
../munlockall.h
136+
DEPENDS
137+
libc.include.sys_mman
138+
libc.include.sys_syscall
139+
libc.src.__support.OSUtil.osutil
140+
libc.src.errno.errno
141+
)

libc/src/sys/mman/linux/mlock.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===---------- Linux implementation of the mlock function ----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/sys/mman/mlock.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
13+
#include "src/errno/libc_errno.h"
14+
#include <sys/syscall.h> // For syscall numbers.
15+
16+
namespace LIBC_NAMESPACE {
17+
18+
LLVM_LIBC_FUNCTION(int, mlock, (const void *addr, size_t len)) {
19+
long ret = syscall_impl(SYS_mlock, cpp::bit_cast<long>(addr), len);
20+
if (ret < 0) {
21+
libc_errno = static_cast<int>(-ret);
22+
return -1;
23+
}
24+
return 0;
25+
}
26+
} // namespace LIBC_NAMESPACE

libc/src/sys/mman/linux/mlock2.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===---------- Linux implementation of the mlock function ----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/sys/mman/mlock2.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
13+
#include "src/errno/libc_errno.h"
14+
#include <sys/syscall.h> // For syscall numbers.
15+
16+
namespace LIBC_NAMESPACE {
17+
#ifdef SYS_mlock2
18+
LLVM_LIBC_FUNCTION(int, mlock2, (const void *addr, size_t len, int flags)) {
19+
long ret = syscall_impl(SYS_mlock2, cpp::bit_cast<long>(addr), len, flags);
20+
if (ret < 0) {
21+
libc_errno = static_cast<int>(-ret);
22+
return -1;
23+
}
24+
return 0;
25+
}
26+
#endif
27+
} // namespace LIBC_NAMESPACE

libc/src/sys/mman/linux/mlockall.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===---------- Linux implementation of the mlockall function -------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/sys/mman/mlockall.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
13+
#include "src/errno/libc_errno.h"
14+
#include <sys/syscall.h> // For syscall numbers.
15+
16+
namespace LIBC_NAMESPACE {
17+
18+
LLVM_LIBC_FUNCTION(int, mlockall, (int flags)) {
19+
long ret = syscall_impl(SYS_mlockall, flags);
20+
if (ret < 0) {
21+
libc_errno = static_cast<int>(-ret);
22+
return -1;
23+
}
24+
return 0;
25+
}
26+
27+
} // namespace LIBC_NAMESPACE

libc/src/sys/mman/linux/munlock.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===---------- Linux implementation of the munlock function --------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/sys/mman/munlock.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
13+
#include "src/errno/libc_errno.h"
14+
#include <sys/syscall.h> // For syscall numbers.
15+
16+
namespace LIBC_NAMESPACE {
17+
18+
LLVM_LIBC_FUNCTION(int, munlock, (const void *addr, size_t len)) {
19+
long ret = syscall_impl(SYS_munlock, cpp::bit_cast<long>(addr), len);
20+
if (ret < 0) {
21+
libc_errno = static_cast<int>(-ret);
22+
return -1;
23+
}
24+
return 0;
25+
}
26+
27+
} // namespace LIBC_NAMESPACE
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===---------- Linux implementation of the munlockall function -----------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/sys/mman/munlockall.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
13+
#include "src/errno/libc_errno.h"
14+
#include <sys/syscall.h> // For syscall numbers.
15+
16+
namespace LIBC_NAMESPACE {
17+
18+
LLVM_LIBC_FUNCTION(int, munlockall, (void)) {
19+
long ret = syscall_impl(SYS_munlockall);
20+
if (ret < 0) {
21+
libc_errno = static_cast<int>(-ret);
22+
return -1;
23+
}
24+
return 0;
25+
}
26+
27+
} // namespace LIBC_NAMESPACE

libc/src/sys/mman/mlock.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for mlock function ----------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SYS_MMAN_MLOCK_H
10+
#define LLVM_LIBC_SRC_SYS_MMAN_MLOCK_H
11+
12+
#include <sys/mman.h>
13+
#include <sys/syscall.h>
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
int mlock(const void *addr, size_t len);
18+
19+
} // namespace LIBC_NAMESPACE
20+
21+
#endif // LLVM_LIBC_SRC_SYS_MMAN_MLOCK_H

libc/src/sys/mman/mlock2.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation header for mlock2 function ---------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SYS_MMAN_MLOCK2_H
10+
#define LLVM_LIBC_SRC_SYS_MMAN_MLOCK2_H
11+
12+
#include <sys/mman.h>
13+
#include <sys/syscall.h>
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
#ifdef SYS_mlock2
18+
int mlock2(const void *addr, size_t len, int flags);
19+
#endif
20+
21+
} // namespace LIBC_NAMESPACE
22+
23+
#endif // LLVM_LIBC_SRC_SYS_MMAN_MLOCK2_H

libc/src/sys/mman/mlockall.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for mlockall function -------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SYS_MMAN_MLOCKALL_H
10+
#define LLVM_LIBC_SRC_SYS_MMAN_MLOCKALL_H
11+
12+
#include <sys/mman.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int mlockall(int flags);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_SYS_MMAN_MLOCKALL_H

0 commit comments

Comments
 (0)