1MLOCK(2) Linux Programmer's Manual MLOCK(2)
2
3
4
6 mlock, munlock, mlockall, munlockall - lock and unlock memory
7
9 #include <sys/mman.h>
10
11 int mlock(const void *addr, size_t len);
12 int munlock(const void *addr, size_t len);
13
14 int mlockall(int flags);
15 int munlockall(void);
16
18 mlock() and mlockall() respectively lock part or all of the calling
19 process's virtual address space into RAM, preventing that memory from
20 being paged to the swap area. munlock() and munlockall() perform the
21 converse operation, respectively unlocking part or all of the calling
22 process's virtual address space, so that pages in the specified virtual
23 address range may once more to be swapped out if required by the kernel
24 memory manager. Memory locking and unlocking are performed in units of
25 whole pages.
26
27 mlock() and munlock()
28 mlock() locks pages in the address range starting at addr and continu‐
29 ing for len bytes. All pages that contain a part of the specified
30 address range are guaranteed to be resident in RAM when the call
31 returns successfully; the pages are guaranteed to stay in RAM until
32 later unlocked.
33
34 munlock() unlocks pages in the address range starting at addr and con‐
35 tinuing for len bytes. After this call, all pages that contain a part
36 of the specified memory range can be moved to external swap space again
37 by the kernel.
38
39 mlockall() and munlockall()
40 mlockall() locks all pages mapped into the address space of the calling
41 process. This includes the pages of the code, data and stack segment,
42 as well as shared libraries, user space kernel data, shared memory, and
43 memory-mapped files. All mapped pages are guaranteed to be resident in
44 RAM when the call returns successfully; the pages are guaranteed to
45 stay in RAM until later unlocked.
46
47 The flags argument is constructed as the bitwise OR of one or more of
48 the following constants:
49
50 MCL_CURRENT Lock all pages which are currently mapped into the address
51 space of the process.
52
53 MCL_FUTURE Lock all pages which will become mapped into the address
54 space of the process in the future. These could be for
55 instance new pages required by a growing heap and stack as
56 well as new memory mapped files or shared memory regions.
57
58 If MCL_FUTURE has been specified, then a later system call (e.g.,
59 mmap(2), sbrk(2), malloc(3)), may fail if it would cause the number of
60 locked bytes to exceed the permitted maximum (see below). In the same
61 circumstances, stack growth may likewise fail: the kernel will deny
62 stack expansion and deliver a SIGSEGV signal to the process.
63
64 munlockall() unlocks all pages mapped into the address space of the
65 calling process.
66
68 On success these system calls return 0. On error, -1 is returned,
69 errno is set appropriately, and no changes are made to any locks in the
70 address space of the process.
71
73 ENOMEM (Linux 2.6.9 and later) the caller had a non-zero RLIMIT_MEMLOCK
74 soft resource limit, but tried to lock more memory than the
75 limit permitted. This limit is not enforced if the process is
76 privileged (CAP_IPC_LOCK).
77
78 ENOMEM (Linux 2.4 and earlier) the calling process tried to lock more
79 than half of RAM.
80
81 EPERM (Linux 2.6.9 and later) the caller was not privileged
82 (CAP_IPC_LOCK) and its RLIMIT_MEMLOCK soft resource limit was 0.
83
84 EPERM (Linux 2.6.8 and earlier) The calling process has insufficient
85 privilege to call munlockall(). Under Linux the CAP_IPC_LOCK
86 capability is required.
87
88 For mlock() and munlock():
89
90 EAGAIN Some or all of the specified address range could not be locked.
91
92 EINVAL len was negative.
93
94 EINVAL (Not on Linux) addr was not a multiple of the page size.
95
96 ENOMEM Some of the specified address range does not correspond to
97 mapped pages in the address space of the process.
98
99 For mlockall():
100
101 EINVAL Unknown flags were specified.
102
103 For munlockall():
104
105 EPERM (Linux 2.6.8 and earlier) The caller was not privileged
106 (CAP_IPC_LOCK).
107
109 POSIX.1-2001, SVr4.
110
112 On POSIX systems on which mlock() and munlock() are available,
113 _POSIX_MEMLOCK_RANGE is defined in <unistd.h> and the number of bytes
114 in a page can be determined from the constant PAGESIZE (if defined) in
115 <limits.h> or by calling sysconf(_SC_PAGESIZE).
116
117 On POSIX systems on which mlockall() and munlockall() are available,
118 _POSIX_MEMLOCK is defined in <unistd.h> to a value greater than 0.
119 (See also sysconf(3).)
120
122 Memory locking has two main applications: real-time algorithms and
123 high-security data processing. Real-time applications require deter‐
124 ministic timing, and, like scheduling, paging is one major cause of
125 unexpected program execution delays. Real-time applications will usu‐
126 ally also switch to a real-time scheduler with sched_setscheduler(2).
127 Cryptographic security software often handles critical bytes like pass‐
128 words or secret keys as data structures. As a result of paging, these
129 secrets could be transferred onto a persistent swap store medium, where
130 they might be accessible to the enemy long after the security software
131 has erased the secrets in RAM and terminated. (But be aware that the
132 suspend mode on laptops and some desktop computers will save a copy of
133 the system's RAM to disk, regardless of memory locks.)
134
135 Real-time processes that are using mlockall() to prevent delays on page
136 faults should reserve enough locked stack pages before entering the
137 time-critical section, so that no page fault can be caused by function
138 calls. This can be achieved by calling a function that allocates a
139 sufficiently large automatic variable (an array) and writes to the mem‐
140 ory occupied by this array in order to touch these stack pages. This
141 way, enough pages will be mapped for the stack and can be locked into
142 RAM. The dummy writes ensure that not even copy-on-write page faults
143 can occur in the critical section.
144
145 Memory locks are not inherited by a child created via fork(2) and are
146 automatically removed (unlocked) during an execve(2) or when the
147 process terminates.
148
149 The memory lock on an address range is automatically removed if the
150 address range is unmapped via munmap(2).
151
152 Memory locks do not stack, that is, pages which have been locked sev‐
153 eral times by calls to mlock() or mlockall() will be unlocked by a sin‐
154 gle call to munlock() for the corresponding range or by munlockall().
155 Pages which are mapped to several locations or by several processes
156 stay locked into RAM as long as they are locked at least at one loca‐
157 tion or by at least one process.
158
159 Linux Notes
160 Under Linux, mlock() and munlock() automatically round addr down to the
161 nearest page boundary. However, POSIX.1-2001 allows an implementation
162 to require that addr is page aligned, so portable applications should
163 ensure this.
164
165 Limits and permissions
166 In Linux 2.6.8 and earlier, a process must be privileged (CAP_IPC_LOCK)
167 in order to lock memory and the RLIMIT_MEMLOCK soft resource limit
168 defines a limit on how much memory the process may lock.
169
170 Since Linux 2.6.9, no limits are placed on the amount of memory that a
171 privileged process can lock and the RLIMIT_MEMLOCK soft resource limit
172 instead defines a limit on how much memory an unprivileged process may
173 lock.
174
176 In the 2.4 series Linux kernels up to and including 2.4.17, a bug
177 caused the mlockall() MCL_FUTURE flag to be inherited across a fork(2).
178 This was rectified in kernel 2.4.18.
179
180 Since kernel 2.6.9, if a privileged process calls mlockall(MCL_FUTURE)
181 and later drops privileges (loses the CAP_IPC_LOCK capability by, for
182 example, setting its effective UID to a non-zero value), then subse‐
183 quent memory allocations (e.g., mmap(2), brk(2)) will fail if the
184 RLIMIT_MEMLOCK resource limit is encountered.
185
187 mmap(2), setrlimit(2), shmctl(2), sysconf(3), capabilities(7)
188
190 This page is part of release 3.22 of the Linux man-pages project. A
191 description of the project, and information about reporting bugs, can
192 be found at http://www.kernel.org/doc/man-pages/.
193
194
195
196Linux 2008-09-25 MLOCK(2)