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
13       int munlock(const void *addr, size_t len);
14
15       int mlockall(int flags);
16
17       int munlockall(void);
18

DESCRIPTION

20       mlock()  and  mlockall()  respectively  lock part or all of the calling
21       process's virtual address space into RAM, preventing that  memory  from
22       being  paged  to the swap area.  munlock() and munlockall() perform the
23       converse operation, respectively unlocking part or all of  the  calling
24       process's virtual address space, so that pages in the specified virtual
25       address range may once more to be swapped out if required by the kernel
26       memory manager.  Memory locking and unlocking are performed in units of
27       whole pages.
28
29   mlock() and munlock()
30       mlock() locks pages in the address range starting at addr and  continu‐
31       ing  for  len  bytes.   All  pages that contain a part of the specified
32       address range are guaranteed to  be  resident  in  RAM  when  the  call
33       returns  successfully;  the  pages  are guaranteed to stay in RAM until
34       later unlocked.
35
36       munlock() unlocks pages in the address range starting at addr and  con‐
37       tinuing  for len bytes.  After this call, all pages that contain a part
38       of the specified memory range can be moved to external swap space again
39       by the kernel.
40
41   mlockall() and munlockall()
42       mlockall() locks all pages mapped into the address space of the calling
43       process. This includes the pages of the code, data and  stack  segment,
44       as well as shared libraries, user space kernel data, shared memory, and
45       memory-mapped files. All mapped pages are guaranteed to be resident  in
46       RAM  when  the  call  returns successfully; the pages are guaranteed to
47       stay in RAM until later unlocked.
48
49       The flags argument is constructed as the bitwise OR of one or  more  of
50       the following constants:
51
52       MCL_CURRENT Lock  all pages which are currently mapped into the address
53                   space of the process.
54
55       MCL_FUTURE  Lock all pages which will become mapped  into  the  address
56                   space  of  the  process  in  the future. These could be for
57                   instance new pages required by a growing heap and stack  as
58                   well as new memory mapped files or shared memory regions.
59
60       If  MCL_FUTURE  has  been  specified,  then  a later system call (e.g.,
61       mmap(2), sbrk(2), malloc(3)), may fail if it would cause the number  of
62       locked  bytes to exceed the permitted maximum (see below).  In the same
63       circumstances, stack growth may likewise fail:  the  kernel  will  deny
64       stack expansion and deliver a SIGSEGV signal to the process.
65
66       munlockall()  unlocks  all  pages  mapped into the address space of the
67       calling process.
68

NOTES

70       Memory locking has two  main  applications:  real-time  algorithms  and
71       high-security data processing. Real-time applications require determin‐
72       istic timing, and, like scheduling, paging is one major cause of  unex‐
73       pected  program  execution  delays. Real-time applications will usually
74       also switch to a real-time scheduler with sched_setscheduler(2).  Cryp‐
75       tographic security software often handles critical bytes like passwords
76       or secret keys as data structures. As a result of paging, these secrets
77       could  be  transferred  onto a persistent swap store medium, where they
78       might be accessible to the enemy long after the security  software  has
79       erased  the secrets in RAM and terminated.  (But be aware that the sus‐
80       pend mode on laptops and some desktop computers will save a copy of the
81       system's RAM to disk, regardless of memory locks.)
82
83       Real-time processes that are using mlockall() to prevent delays on page
84       faults should reserve enough locked stack  pages  before  entering  the
85       time-critical  section, so that no page fault can be caused by function
86       calls.  This can be achieved by calling a  function  that  allocates  a
87       sufficiently large automatic variable (an array) and writes to the mem‐
88       ory occupied by this array in order to touch these stack  pages.   This
89       way,  enough  pages will be mapped for the stack and can be locked into
90       RAM. The dummy writes ensure that not even  copy-on-write  page  faults
91       can occur in the critical section.
92
93       Memory  locks  are not inherited by a child created via fork(2) and are
94       automatically removed  (unlocked)  during  an  execve(2)  or  when  the
95       process terminates.
96
97       The  memory  lock  on  an address range is automatically removed if the
98       address range is unmapped via munmap(2).
99
100       Memory locks do not stack, i.e., pages which have been  locked  several
101       times  by  calls  to mlock() or mlockall() will be unlocked by a single
102       call to munlock() for  the  corresponding  range  or  by  munlockall().
103       Pages  which  are  mapped  to several locations or by several processes
104       stay locked into RAM as long as they are locked at least at  one  loca‐
105       tion or by at least one process.
106

LINUX NOTES

108       Under Linux, mlock() and munlock() automatically round addr down to the
109       nearest page boundary.  However, POSIX.1-2001 allows an  implementation
110       to  require  that addr is page aligned, so portable applications should
111       ensure this.
112
113   Limits and permissions
114       In Linux 2.6.8 and earlier, a process must be privileged (CAP_IPC_LOCK)
115       in  order  to  lock  memory  and the RLIMIT_MEMLOCK soft resource limit
116       defines a limit on how much memory the process may lock.
117
118       Since Linux 2.6.9, no limits are placed on the amount of memory that  a
119       privileged  process can lock and the RLIMIT_MEMLOCK soft resource limit
120       instead defines a limit on how much memory an unprivileged process  may
121       lock.
122

RETURN VALUE

124       On  success  these  system  calls  return 0.  On error, -1 is returned,
125       errno is set appropriately, and no changes are made to any locks in the
126       address space of the process.
127

ERRORS

129       ENOMEM (Linux 2.6.9 and later) the caller had a non-zero RLIMIT_MEMLOCK
130              soft resource limit, but tried to  lock  more  memory  than  the
131              limit  permitted.   This limit is not enforced if the process is
132              privileged (CAP_IPC_LOCK).
133
134       ENOMEM (Linux 2.4 and earlier) the calling process tried to  lock  more
135              than half of RAM.
136
137       EPERM  (Linux   2.6.9   and   later)  the  caller  was  not  privileged
138              (CAP_IPC_LOCK) and its RLIMIT_MEMLOCK soft resource limit was 0.
139
140       EPERM  (Linux 2.6.8 and earlier) The calling process  has  insufficient
141              privilege  to  call  munlockall().  Under Linux the CAP_IPC_LOCK
142              capability is required.
143
144       For mlock() and munlock():
145
146       EINVAL len was negative.
147
148       EINVAL (Not on Linux) addr was not a multiple of the page size.
149
150       ENOMEM Some of the specified  address  range  does  not  correspond  to
151              mapped pages in the address space of the process.
152
153       For mlockall():
154
155       EINVAL Unknown flags were specified.
156
157       For munlockall():
158
159       EPERM  (Linux   2.6.8  and  earlier)  The  caller  was  not  privileged
160              (CAP_IPC_LOCK).
161

BUGS

163       In the 2.4 series Linux kernels up  to  and  including  2.4.17,  a  bug
164       caused the mlockall() MCL_FUTURE flag to be inherited across a fork(2).
165       This was rectified in kernel 2.4.18.
166
167       Since kernel 2.6.9, if a privileged process calls  mlockall(MCL_FUTURE)
168       and  later  drops privileges (loses the CAP_IPC_LOCK capability by, for
169       example, setting its effective UID to a non-zero  value),  then  subse‐
170       quent  memory  allocations  (e.g.,  mmap(2),  brk(2))  will fail if the
171       RLIMIT_MEMLOCK resource limit is encountered.
172

AVAILABILITY

174       On  POSIX  systems  on  which  mlock()  and  munlock()  are  available,
175       _POSIX_MEMLOCK_RANGE  is  defined in <unistd.h> and the number of bytes
176       in a page can be determined from the constant PAGESIZE (if defined)  in
177       <limits.h> or by calling sysconf(_SC_PAGESIZE).
178
179       On  POSIX  systems  on which mlockall() and munlockall() are available,
180       _POSIX_MEMLOCK is defined in <unistd.h> to a value greater than 0. (See
181       also sysconf(3).)
182

CONFORMING TO

184       POSIX.1-2001, SVr4
185

SEE ALSO

187       mmap(2), shmctl(2), setrlimit(2), sysconf(3), capabilities(7)
188
189
190
191Linux 2.6.15                      2006-02-04                          MLOCK(2)
Impressum