1MLOCK(2)                   Linux Programmer's Manual                  MLOCK(2)
2
3
4

NAME

6       mlock, munlock, mlockall, munlockall - lock and unlock memory
7

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

73       ENOMEM (Linux 2.6.9 and later) the caller had a nonzero  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  The caller is not privileged, but needs privilege (CAP_IPC_LOCK)
82              to perform the requested operation.
83
84       For mlock() and munlock():
85
86       EAGAIN Some or all of the specified address range could not be locked.
87
88       EINVAL The  result of the addition start+len was less than start (e.g.,
89              the addition may have resulted in an overflow).
90
91       EINVAL (Not on Linux) addr was not a multiple of the page size.
92
93       ENOMEM Some of the specified  address  range  does  not  correspond  to
94              mapped pages in the address space of the process.
95
96       For mlockall():
97
98       EINVAL Unknown flags were specified.
99
100       For munlockall():
101
102       EPERM  (Linux   2.6.8  and  earlier)  The  caller  was  not  privileged
103              (CAP_IPC_LOCK).
104

CONFORMING TO

106       POSIX.1-2001, SVr4.
107

AVAILABILITY

109       On  POSIX  systems  on  which  mlock()  and  munlock()  are  available,
110       _POSIX_MEMLOCK_RANGE  is  defined in <unistd.h> and the number of bytes
111       in a page can be determined from the constant PAGESIZE (if defined)  in
112       <limits.h> or by calling sysconf(_SC_PAGESIZE).
113
114       On  POSIX  systems  on which mlockall() and munlockall() are available,
115       _POSIX_MEMLOCK is defined in <unistd.h> to  a  value  greater  than  0.
116       (See also sysconf(3).)
117

NOTES

119       Memory  locking  has  two  main  applications: real-time algorithms and
120       high-security data processing.  Real-time applications  require  deter‐
121       ministic  timing,  and,  like  scheduling, paging is one major cause of
122       unexpected program execution delays.  Real-time applications will  usu‐
123       ally  also  switch to a real-time scheduler with sched_setscheduler(2).
124       Cryptographic security software often handles critical bytes like pass‐
125       words  or secret keys as data structures.  As a result of paging, these
126       secrets could be transferred onto a persistent swap store medium, where
127       they  might be accessible to the enemy long after the security software
128       has erased the secrets in RAM and terminated.  (But be aware  that  the
129       suspend  mode on laptops and some desktop computers will save a copy of
130       the system's RAM to disk, regardless of memory locks.)
131
132       Real-time processes that are using mlockall() to prevent delays on page
133       faults  should  reserve  enough  locked stack pages before entering the
134       time-critical section, so that no page fault can be caused by  function
135       calls.   This  can  be  achieved by calling a function that allocates a
136       sufficiently large automatic variable (an array) and writes to the mem‐
137       ory  occupied  by this array in order to touch these stack pages.  This
138       way, enough pages will be mapped for the stack and can be  locked  into
139       RAM.   The  dummy writes ensure that not even copy-on-write page faults
140       can occur in the critical section.
141
142       Memory locks are not inherited by a child created via fork(2)  and  are
143       automatically  removed  (unlocked)  during  an  execve(2)  or  when the
144       process terminates.
145
146       The memory lock on an address range is  automatically  removed  if  the
147       address range is unmapped via munmap(2).
148
149       Memory  locks  do not stack, that is, pages which have been locked sev‐
150       eral times by calls to mlock() or mlockall() will be unlocked by a sin‐
151       gle  call  to munlock() for the corresponding range or by munlockall().
152       Pages which are mapped to several locations  or  by  several  processes
153       stay  locked  into RAM as long as they are locked at least at one loca‐
154       tion or by at least one process.
155
156   Linux notes
157       Under Linux, mlock() and munlock() automatically round addr down to the
158       nearest  page boundary.  However, POSIX.1-2001 allows an implementation
159       to require that addr is page aligned, so portable  applications  should
160       ensure this.
161
162       The  VmLck  field of the Linux-specific /proc/PID/status file shows how
163       many kilobytes of memory the process  with  ID  PID  has  locked  using
164       mlock(), mlockall(), and mmap(2) MAP_LOCKED.
165
166   Limits and permissions
167       In Linux 2.6.8 and earlier, a process must be privileged (CAP_IPC_LOCK)
168       in order to lock memory and  the  RLIMIT_MEMLOCK  soft  resource  limit
169       defines a limit on how much memory the process may lock.
170
171       Since  Linux 2.6.9, no limits are placed on the amount of memory that a
172       privileged process can lock and the RLIMIT_MEMLOCK soft resource  limit
173       instead  defines a limit on how much memory an unprivileged process may
174       lock.
175

BUGS

177       In the 2.4 series Linux kernels up  to  and  including  2.4.17,  a  bug
178       caused the mlockall() MCL_FUTURE flag to be inherited across a fork(2).
179       This was rectified in kernel 2.4.18.
180
181       Since kernel 2.6.9, if a privileged process calls  mlockall(MCL_FUTURE)
182       and  later  drops privileges (loses the CAP_IPC_LOCK capability by, for
183       example, setting its effective UID to a nonzero value), then subsequent
184       memory allocations (e.g., mmap(2), brk(2)) will fail if the RLIMIT_MEM‐
185       LOCK resource limit is encountered.
186

SEE ALSO

188       mmap(2), setrlimit(2), shmctl(2), sysconf(3), proc(5), capabilities(7)
189

COLOPHON

191       This page is part of release 3.53 of the Linux  man-pages  project.   A
192       description  of  the project, and information about reporting bugs, can
193       be found at http://www.kernel.org/doc/man-pages/.
194
195
196
197Linux                             2011-09-14                          MLOCK(2)
Impressum