1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (C) Michael Kerrisk, 2004
4 .\" using some material drawn from earlier man pages
5 .\" written by Thomas Kuhn, Copyright 1996
7 .\" This is free documentation; you can redistribute it and/or
8 .\" modify it under the terms of the GNU General Public License as
9 .\" published by the Free Software Foundation; either version 2 of
10 .\" the License, or (at your option) any later version.
12 .\" The GNU General Public License's references to "object code"
13 .\" and "executables" are to be interpreted as the output of any
14 .\" document formatting or typesetting system, including
15 .\" intermediate and printed output.
17 .\" This manual is distributed in the hope that it will be useful,
18 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
19 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 .\" GNU General Public License for more details.
22 .\" You should have received a copy of the GNU General Public
23 .\" License along with this manual; if not, write to the Free
24 .\" Software Foundation, Inc., 59 Temple Place, Suite 330,
25 .\" Boston, MA 02111, USA.
27 .TH MLOCK 2 2006-02-04 "Linux 2.6.15" "Linux Programmer's Manual"
29 mlock, munlock, mlockall, munlockall \- lock and unlock memory
32 .B #include <sys/mman.h>
34 \fBint mlock(const void *\fIaddr\fB, size_t \fIlen\fB);
36 \fBint munlock(const void *\fIaddr\fB, size_t \fIlen\fB);
38 \fBint mlockall(int \fIflags\fB);
40 \fBint munlockall(void);
46 respectively lock part or all of the calling process's virtual address
47 space into RAM, preventing that memory from being paged to the
52 perform the converse operation,
53 respectively unlocking part or all of the calling process's virtual
54 address space, so that pages in the specified virtual address range may
55 once more to be swapped out if required by the kernel memory manager.
56 Memory locking and unlocking are performed in units of whole pages.
57 .SS "mlock() and munlock()"
59 locks pages in the address range starting at
64 All pages that contain a part of the specified address range are
65 guaranteed to be resident in RAM when the call returns successfully;
66 the pages are guaranteed to stay in RAM until later unlocked.
69 unlocks pages in the address range starting at
74 After this call, all pages that contain a part of the specified
75 memory range can be moved to external swap space again by the kernel.
76 .SS "mlockall() and munlockall()"
78 locks all pages mapped into the address space of the
80 This includes the pages of the code, data and stack
81 segment, as well as shared libraries, user space kernel data, shared
82 memory, and memory\-mapped files.
83 All mapped pages are guaranteed
84 to be resident in RAM when the call returns successfully;
85 the pages are guaranteed to stay in RAM until later unlocked.
89 argument is constructed as the bitwise OR of one or more of the
93 Lock all pages which are currently mapped into the address space of
97 Lock all pages which will become mapped into the address space of the
98 process in the future.
99 These could be for instance new pages required
100 by a growing heap and stack as well as new memory mapped files or
101 shared memory regions.
105 has been specified, then a later system call (e.g.,
109 may fail if it would cause the number of locked bytes to exceed
110 the permitted maximum (see below).
111 In the same circumstances, stack growth may likewise fail:
112 the kernel will deny stack expansion and deliver a
114 signal to the process.
117 unlocks all pages mapped into the address space of the
120 On success these system calls return 0.
121 On error, \-1 is returned,
123 is set appropriately, and no changes are made to any locks in the
124 address space of the process.
128 (Linux 2.6.9 and later) the caller had a non-zero
130 soft resource limit, but tried to lock more memory than the limit
132 This limit is not enforced if the process is privileged
133 .RB ( CAP_IPC_LOCK ).
136 (Linux 2.4 and earlier) the calling process tried to lock more than
138 .\" In the case of mlock(), this check is somewhat buggy: it doesn't
139 .\" take into account whether the to-be-locked range overlaps with
140 .\" already locked pages. Thus, suppose we allocate
141 .\" (num_physpages / 4 + 1) of memory, and lock those pages once using
142 .\" mlock(), and then lock the *same* page range a second time.
143 .\" In the case, the second mlock() call will fail, since the check
144 .\" calculates that the process is trying to lock (num_physpages / 2 + 2)
145 .\" pages, which of course is not true. (MTK, Nov 04, kernel 2.4.28)
148 (Linux 2.6.9 and later) the caller was not privileged
152 soft resource limit was 0.
155 (Linux 2.6.8 and earlier)
156 The calling process has insufficient privilege to call
160 capability is required.
161 .\"SVr4 documents an additional EAGAIN error code.
175 was not a multiple of the page size.
178 Some of the specified address range does not correspond to mapped
179 pages in the address space of the process.
185 Unknown \fIflags\fP were specified.
191 (Linux 2.6.8 and earlier) The caller was not privileged
192 .RB ( CAP_IPC_LOCK ).
196 On POSIX systems on which
201 .B _POSIX_MEMLOCK_RANGE
202 is defined in <unistd.h> and the number of bytes in a page
203 can be determined from the constant
205 (if defined) in <limits.h> or by calling
206 .IR sysconf(_SC_PAGESIZE) .
208 On POSIX systems on which
214 is defined in <unistd.h> to a value greater than 0. (See also
216 .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
217 .\" -1: unavailable, 0: ask using sysconf().
218 .\" glibc defines it to 1.
220 Memory locking has two main applications: real-time algorithms and
221 high-security data processing.
222 Real-time applications require
223 deterministic timing, and, like scheduling, paging is one major cause
224 of unexpected program execution delays.
225 Real-time applications will
226 usually also switch to a real-time scheduler with
227 .BR sched_setscheduler (2).
228 Cryptographic security software often handles critical bytes like
229 passwords or secret keys as data structures.
230 As a result of paging,
231 these secrets could be transferred onto a persistent swap store medium,
232 where they might be accessible to the enemy long after the security
233 software has erased the secrets in RAM and terminated.
234 (But be aware that the suspend mode on laptops and some desktop
235 computers will save a copy of the system's RAM to disk, regardless
238 Real-time processes that are using
240 to prevent delays on page faults should reserve enough
241 locked stack pages before entering the time-critical section,
242 so that no page fault can be caused by function calls.
243 This can be achieved by calling a function that allocates a
244 sufficiently large automatic variable (an array) and writes to the
245 memory occupied by this array in order to touch these stack pages.
246 This way, enough pages will be mapped for the stack and can be
248 The dummy writes ensure that not even copy-on-write
249 page faults can occur in the critical section.
251 Memory locks are not inherited by a child created via
253 and are automatically removed (unlocked) during an
255 or when the process terminates.
257 The memory lock on an address range is automatically removed
258 if the address range is unmapped via
261 Memory locks do not stack, i.e., pages which have been locked several times
266 will be unlocked by a single call to
268 for the corresponding range or by
270 Pages which are mapped to several locations or by several processes stay
271 locked into RAM as long as they are locked at least at one location or by
272 at least one process.
280 down to the nearest page boundary.
281 However, POSIX.1-2001 allows an implementation to require that
283 is page aligned, so portable applications should ensure this.
284 .SS "Limits and permissions"
285 In Linux 2.6.8 and earlier,
286 a process must be privileged
288 in order to lock memory and the
290 soft resource limit defines a limit on how much memory the process may lock.
292 Since Linux 2.6.9, no limits are placed on the amount of memory
293 that a privileged process can lock and the
295 soft resource limit instead defines a limit on how much memory an
296 unprivileged process may lock.
298 In the 2.4 series Linux kernels up to and including 2.4.17,
302 flag to be inherited across a
304 This was rectified in kernel 2.4.18.
306 Since kernel 2.6.9, if a privileged process calls
307 .I mlockall(MCL_FUTURE)
308 and later drops privileges (loses the
310 capability by, for example,
311 setting its effective UID to a non-zero value),
312 then subsequent memory allocations (e.g.,
317 resource limit is encountered.
318 .\" See the following LKML thread:
319 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=113801392825023&w=2
320 .\" "Rationale for RLIMIT_MEMLOCK"