1 .\" Copyright (C) Michael Kerrisk, 2004
2 .\" using some material drawn from earlier man pages
3 .\" written by Thomas Kuhn, Copyright 1996
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.
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.
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.
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/>.
26 .TH MLOCK 2 2015-08-28 "Linux" "Linux Programmer's Manual"
28 mlock, mlock2, munlock, mlockall, munlockall \- lock and unlock memory
31 .B #include <sys/mman.h>
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 );
37 .BI "int mlockall(int " flags );
38 .B int munlockall(void);
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
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()
58 locks pages in the address range starting at
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.
68 also locks pages in the specified range starting at
73 However, the state of the pages contained in that range after the call
74 returns successfully will depend on the value in the
80 argument can be either 0 or the following constant:
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.
91 will function exactly as
95 Note: Currently, there is not a glibc wrapper for
97 so it will need to be invoked using
101 unlocks pages in the address range starting at
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()
110 locks all pages mapped into the address space of the
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.
121 argument is constructed as the bitwise OR of one or more of the
125 Lock all pages which are currently mapped into the address space of
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.
135 .BR MCL_ONFAULT " (since Linux 4.4)"
140 Mark all current (with
144 mappings to lock pages when they are faulted in.
147 all present pages are locked, but
149 will not fault in non-present pages.
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
156 must be used with either
164 has been specified, then a later system call (e.g.,
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
173 signal to the process.
176 unlocks all pages mapped into the address space of the
179 On success, these system calls return 0.
180 On error, \-1 is returned,
182 is set appropriately, and no changes are made to any locks in the
183 address space of the process.
187 (Linux 2.6.9 and later) the caller had a nonzero
189 soft resource limit, but tried to lock more memory than the limit
191 This limit is not enforced if the process is privileged
192 .RB ( CAP_IPC_LOCK ).
195 (Linux 2.4 and earlier) the calling process tried to lock more than
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)
207 The caller is not privileged, but needs privilege
209 to perform the requested operation.
210 .\"SVr4 documents an additional EAGAIN error code.
219 Some or all of the specified address range could not be locked.
222 The result of the addition
226 (e.g., the addition may have resulted in an overflow).
231 was not a multiple of the page size.
234 Some of the specified address range does not correspond to mapped
235 pages in the address space of the process.
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.)
250 Unknown \fIflags\fP were specified.
256 Unknown \fIflags\fP were specified or
258 was specified without either
267 (Linux 2.6.8 and earlier) The caller was not privileged
268 .RB ( CAP_IPC_LOCK ).
270 POSIX.1-2001, POSIX.1-2008, SVr4.
272 On POSIX systems on which
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
281 (if defined) in \fI<limits.h>\fP or by calling
282 .IR sysconf(_SC_PAGESIZE) .
284 On POSIX systems on which
290 is defined in \fI<unistd.h>\fP to a value greater than 0.
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.
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
315 Real-time processes that are using
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
325 The dummy writes ensure that not even copy-on-write
326 page faults can occur in the critical section.
328 Memory locks are not inherited by a child created via
330 and are automatically removed (unlocked) during an
332 or when the process terminates.
337 .B MCL_FUTURE | MCL_ONFAULT
338 settings are not inherited by a child created via
340 and are cleared during an
343 The memory lock on an address range is automatically removed
344 if the address range is unmapped via
347 Memory locks do not stack, that is, pages which have been locked several times
353 will be unlocked by a single call to
355 for the corresponding range or by
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.
365 flag is followed by another call that does not specify this flag, the
377 down to the nearest page boundary.
378 However, POSIX.1 allows an implementation to require that
380 is page aligned, so portable applications should ensure this.
384 field of the Linux-specific
386 file shows how many kilobytes of memory the process with ID
395 .SS Limits and permissions
396 In Linux 2.6.8 and earlier,
397 a process must be privileged
399 in order to lock memory and the
401 soft resource limit defines a limit on how much memory the process may lock.
403 Since Linux 2.6.9, no limits are placed on the amount of memory
404 that a privileged process can lock and the
406 soft resource limit instead defines a limit on how much memory an
407 unprivileged process may lock.
409 In the 2.4 series Linux kernels up to and including 2.4.17,
413 flag to be inherited across a
415 This was rectified in kernel 2.4.18.
417 Since kernel 2.6.9, if a privileged process calls
418 .I mlockall(MCL_FUTURE)
419 and later drops privileges (loses the
421 capability by, for example,
422 setting its effective UID to a nonzero value),
423 then subsequent memory allocations (e.g.,
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"
435 is available since Linux 4.4.