]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/mlock.2
mlock.2: srcfix
[thirdparty/man-pages.git] / man2 / mlock.2
1 .\" Copyright (C) Michael Kerrisk, 2004
2 .\" using some material drawn from earlier man pages
3 .\" written by Thomas Kuhn, Copyright 1996
4 .\"
5 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
10 .\"
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
15 .\"
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 .\" GNU General Public License for more details.
20 .\"
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, see
23 .\" <http://www.gnu.org/licenses/>.
24 .\" %%%LICENSE_END
25 .\"
26 .TH MLOCK 2 2015-08-28 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 mlock, mlock2, munlock, mlockall, munlockall \- lock and unlock memory
29 .SH SYNOPSIS
30 .nf
31 .B #include <sys/mman.h>
32 .sp
33 .BI "int mlock(const void *" addr ", size_t " len );
34 .BI "int mlock2(const void *" addr ", size_t " len ", int " flags );
35 .BI "int munlock(const void *" addr ", size_t " len );
36 .sp
37 .BI "int mlockall(int " flags );
38 .B int munlockall(void);
39 .fi
40 .SH DESCRIPTION
41 .BR mlock (),
42 .BR mlock2 (),
43 and
44 .BR mlockall ()
45 respectively lock part or all of the calling process's virtual address
46 space into RAM, preventing that memory from being paged to the
47 swap area.
48 .BR munlock ()
49 and
50 .BR munlockall ()
51 perform the converse operation,
52 respectively unlocking part or all of the calling process's virtual
53 address space, so that pages in the specified virtual address range may
54 once more to be swapped out if required by the kernel memory manager.
55 Memory locking and unlocking are performed in units of whole pages.
56 .SS mlock(), mlock2(), and munlock()
57 .BR mlock ()
58 locks pages in the address range starting at
59 .I addr
60 and continuing for
61 .I len
62 bytes.
63 All pages that contain a part of the specified address range are
64 guaranteed to be resident in RAM when the call returns successfully;
65 the pages are guaranteed to stay in RAM until later unlocked.
66
67 .BR mlock2 ()
68 also locks pages in the specified range starting at
69 .I addr
70 and continuing for
71 .I len
72 bytes.
73 However, the state of the pages contained in that range after the call
74 returns successfully will depend on the value in the
75 .I flags
76 argument.
77
78 The
79 .I flags
80 argument can be either 0 or the following constant:
81 .TP 1.2i
82 .B MLOCK_ONFAULT
83 Lock pages that are currently resident and mark the entire range to have
84 pages locked when they are populated by the page fault.
85 .PP
86
87 If
88 .I flags
89 is 0,
90 .BR mlock2 ()
91 will function exactly as
92 .BR mlock ()
93 would.
94
95 Note: Currently, there is not a glibc wrapper for
96 .BR mlock2 ()
97 so it will need to be invoked using
98 .BR syscall (2)
99
100 .BR munlock ()
101 unlocks pages in the address range starting at
102 .I addr
103 and continuing for
104 .I len
105 bytes.
106 After this call, all pages that contain a part of the specified
107 memory range can be moved to external swap space again by the kernel.
108 .SS mlockall() and munlockall()
109 .BR mlockall ()
110 locks all pages mapped into the address space of the
111 calling process.
112 This includes the pages of the code, data and stack
113 segment, as well as shared libraries, user space kernel data, shared
114 memory, and memory-mapped files.
115 All mapped pages are guaranteed
116 to be resident in RAM when the call returns successfully;
117 the pages are guaranteed to stay in RAM until later unlocked.
118
119 The
120 .I flags
121 argument is constructed as the bitwise OR of one or more of the
122 following constants:
123 .TP 1.2i
124 .B MCL_CURRENT
125 Lock all pages which are currently mapped into the address space of
126 the process.
127 .TP
128 .B MCL_FUTURE
129 Lock all pages which will become mapped into the address space of the
130 process in the future.
131 These could be, for instance, new pages required
132 by a growing heap and stack as well as new memory-mapped files or
133 shared memory regions.
134 .TP
135 .BR MCL_ONFAULT " (since Linux 4.4)"
136 Used together with
137 .BR MCL_CURRENT ,
138 .BR MCL_FUTURE ,
139 or both.
140 Mark all current (with
141 .BR MCL_CURRENT )
142 or future (with
143 .BR MCL_FUTURE )
144 mappings to lock pages when they are faulted in.
145 When used with
146 .BR MCL_CURRENT ,
147 all present pages are locked, but
148 .BR mlockall ()
149 will not fault in non-present pages.
150 When used with
151 .BR MCL_FUTURE ,
152 all future mappings will be marked to lock pages when they are faulted
153 in, but they will not be populated by the lock when the mapping is
154 created.
155 .B MCL_ONFAULT
156 must be used with either
157 .B MCL_CURRENT
158 or
159 .B MCL_FUTURE
160 or both.
161 .PP
162 If
163 .B MCL_FUTURE
164 has been specified, then a later system call (e.g.,
165 .BR mmap (2),
166 .BR sbrk (2),
167 .BR malloc (3)),
168 may fail if it would cause the number of locked bytes to exceed
169 the permitted maximum (see below).
170 In the same circumstances, stack growth may likewise fail:
171 the kernel will deny stack expansion and deliver a
172 .B SIGSEGV
173 signal to the process.
174
175 .BR munlockall ()
176 unlocks all pages mapped into the address space of the
177 calling process.
178 .SH RETURN VALUE
179 On success, these system calls return 0.
180 On error, \-1 is returned,
181 .I errno
182 is set appropriately, and no changes are made to any locks in the
183 address space of the process.
184 .SH ERRORS
185 .TP
186 .B ENOMEM
187 (Linux 2.6.9 and later) the caller had a nonzero
188 .B RLIMIT_MEMLOCK
189 soft resource limit, but tried to lock more memory than the limit
190 permitted.
191 This limit is not enforced if the process is privileged
192 .RB ( CAP_IPC_LOCK ).
193 .TP
194 .B ENOMEM
195 (Linux 2.4 and earlier) the calling process tried to lock more than
196 half of RAM.
197 .\" In the case of mlock(), this check is somewhat buggy: it doesn't
198 .\" take into account whether the to-be-locked range overlaps with
199 .\" already locked pages. Thus, suppose we allocate
200 .\" (num_physpages / 4 + 1) of memory, and lock those pages once using
201 .\" mlock(), and then lock the *same* page range a second time.
202 .\" In the case, the second mlock() call will fail, since the check
203 .\" calculates that the process is trying to lock (num_physpages / 2 + 2)
204 .\" pages, which of course is not true. (MTK, Nov 04, kernel 2.4.28)
205 .TP
206 .B EPERM
207 The caller is not privileged, but needs privilege
208 .RB ( CAP_IPC_LOCK )
209 to perform the requested operation.
210 .\"SVr4 documents an additional EAGAIN error code.
211 .LP
212 For
213 .BR mlock (),
214 .BR mlock2 (),
215 and
216 .BR munlock ():
217 .TP
218 .B EAGAIN
219 Some or all of the specified address range could not be locked.
220 .TP
221 .B EINVAL
222 The result of the addition
223 .IR addr + len
224 was less than
225 .IR addr
226 (e.g., the addition may have resulted in an overflow).
227 .TP
228 .B EINVAL
229 (Not on Linux)
230 .I addr
231 was not a multiple of the page size.
232 .TP
233 .B ENOMEM
234 Some of the specified address range does not correspond to mapped
235 pages in the address space of the process.
236 .TP
237 .B ENOMEM
238 Locking or unlocking a region would result in the total number of
239 mappings with distinct attributes (e.g., locked versus unlocked)
240 exceeding the allowed maximum.
241 .\" I.e., the number of VMAs would exceed the 64kB maximum
242 (For example, unlocking a range in the middle of a currently locked
243 mapping would result in three mappings:
244 two locked mappings at each end and an unlocked mapping in the middle.)
245 .LP
246 For
247 .BR mlock2 ():
248 .TP
249 .B EINVAL
250 Unknown \fIflags\fP were specified.
251 .LP
252 For
253 .BR mlockall ():
254 .TP
255 .B EINVAL
256 Unknown \fIflags\fP were specified or
257 .B MCL_ONFAULT
258 was specified without either
259 .B MCL_FUTURE
260 or
261 .BR MCL_CURRENT .
262 .LP
263 For
264 .BR munlockall ():
265 .TP
266 .B EPERM
267 (Linux 2.6.8 and earlier) The caller was not privileged
268 .RB ( CAP_IPC_LOCK ).
269 .SH CONFORMING TO
270 POSIX.1-2001, POSIX.1-2008, SVr4.
271 .SH AVAILABILITY
272 On POSIX systems on which
273 .BR mlock ()
274 and
275 .BR munlock ()
276 are available,
277 .B _POSIX_MEMLOCK_RANGE
278 is defined in \fI<unistd.h>\fP and the number of bytes in a page
279 can be determined from the constant
280 .B PAGESIZE
281 (if defined) in \fI<limits.h>\fP or by calling
282 .IR sysconf(_SC_PAGESIZE) .
283
284 On POSIX systems on which
285 .BR mlockall ()
286 and
287 .BR munlockall ()
288 are available,
289 .B _POSIX_MEMLOCK
290 is defined in \fI<unistd.h>\fP to a value greater than 0.
291 (See also
292 .BR sysconf (3).)
293 .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
294 .\" -1: unavailable, 0: ask using sysconf().
295 .\" glibc defines it to 1.
296 .SH NOTES
297 Memory locking has two main applications: real-time algorithms and
298 high-security data processing.
299 Real-time applications require
300 deterministic timing, and, like scheduling, paging is one major cause
301 of unexpected program execution delays.
302 Real-time applications will
303 usually also switch to a real-time scheduler with
304 .BR sched_setscheduler (2).
305 Cryptographic security software often handles critical bytes like
306 passwords or secret keys as data structures.
307 As a result of paging,
308 these secrets could be transferred onto a persistent swap store medium,
309 where they might be accessible to the enemy long after the security
310 software has erased the secrets in RAM and terminated.
311 (But be aware that the suspend mode on laptops and some desktop
312 computers will save a copy of the system's RAM to disk, regardless
313 of memory locks.)
314
315 Real-time processes that are using
316 .BR mlockall ()
317 to prevent delays on page faults should reserve enough
318 locked stack pages before entering the time-critical section,
319 so that no page fault can be caused by function calls.
320 This can be achieved by calling a function that allocates a
321 sufficiently large automatic variable (an array) and writes to the
322 memory occupied by this array in order to touch these stack pages.
323 This way, enough pages will be mapped for the stack and can be
324 locked into RAM.
325 The dummy writes ensure that not even copy-on-write
326 page faults can occur in the critical section.
327
328 Memory locks are not inherited by a child created via
329 .BR fork (2)
330 and are automatically removed (unlocked) during an
331 .BR execve (2)
332 or when the process terminates.
333 The
334 .BR mlockall ()
335 .B MCL_FUTURE
336 and
337 .B MCL_FUTURE | MCL_ONFAULT
338 settings are not inherited by a child created via
339 .BR fork (2)
340 and are cleared during an
341 .BR execve (2).
342
343 The memory lock on an address range is automatically removed
344 if the address range is unmapped via
345 .BR munmap (2).
346
347 Memory locks do not stack, that is, pages which have been locked several times
348 by calls to
349 .BR mlock (),
350 .BR mlock2 (),
351 or
352 .BR mlockall ()
353 will be unlocked by a single call to
354 .BR munlock ()
355 for the corresponding range or by
356 .BR munlockall ().
357 Pages which are mapped to several locations or by several processes stay
358 locked into RAM as long as they are locked at least at one location or by
359 at least one process.
360
361 If a call to
362 .BR mlockall ()
363 which uses the
364 .B MCL_FUTURE
365 flag is followed by another call that does not specify this flag, the
366 changes made by the
367 .B MCL_FUTURE
368 call will be lost.
369 .SS Linux notes
370 Under Linux,
371 .BR mlock (),
372 .BR mlock2 (),
373 and
374 .BR munlock ()
375 automatically round
376 .I addr
377 down to the nearest page boundary.
378 However, POSIX.1 allows an implementation to require that
379 .I addr
380 is page aligned, so portable applications should ensure this.
381
382 The
383 .I VmLck
384 field of the Linux-specific
385 .I /proc/PID/status
386 file shows how many kilobytes of memory the process with ID
387 .I PID
388 has locked using
389 .BR mlock (),
390 .BR mlock2 (),
391 .BR mlockall (),
392 and
393 .BR mmap (2)
394 .BR MAP_LOCKED .
395 .SS Limits and permissions
396 In Linux 2.6.8 and earlier,
397 a process must be privileged
398 .RB ( CAP_IPC_LOCK )
399 in order to lock memory and the
400 .B RLIMIT_MEMLOCK
401 soft resource limit defines a limit on how much memory the process may lock.
402
403 Since Linux 2.6.9, no limits are placed on the amount of memory
404 that a privileged process can lock and the
405 .B RLIMIT_MEMLOCK
406 soft resource limit instead defines a limit on how much memory an
407 unprivileged process may lock.
408 .SH BUGS
409 In the 2.4 series Linux kernels up to and including 2.4.17,
410 a bug caused the
411 .BR mlockall ()
412 .B MCL_FUTURE
413 flag to be inherited across a
414 .BR fork (2).
415 This was rectified in kernel 2.4.18.
416
417 Since kernel 2.6.9, if a privileged process calls
418 .I mlockall(MCL_FUTURE)
419 and later drops privileges (loses the
420 .B CAP_IPC_LOCK
421 capability by, for example,
422 setting its effective UID to a nonzero value),
423 then subsequent memory allocations (e.g.,
424 .BR mmap (2),
425 .BR brk (2))
426 will fail if the
427 .B RLIMIT_MEMLOCK
428 resource limit is encountered.
429 .\" See the following LKML thread:
430 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=113801392825023&w=2
431 .\" "Rationale for RLIMIT_MEMLOCK"
432 .\" 23 Jan 2006
433 .SH VERSIONS
434 .BR mlock2 (2)
435 is available since Linux 4.4.
436 .SH SEE ALSO
437 .BR mmap (2),
438 .BR setrlimit (2),
439 .BR shmctl (2),
440 .BR sysconf (3),
441 .BR proc (5),
442 .BR capabilities (7)