1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (C) Markus Kuhn, 1996
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
10 .\" The GNU General Public License's references to "object code"
11 .\" and "executables" are to be interpreted as the output of any
12 .\" document formatting or typesetting system, including
13 .\" intermediate and printed output.
15 .\" This manual is distributed in the hope that it will be useful,
16 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 .\" GNU General Public License for more details.
20 .\" You should have received a copy of the GNU General Public
21 .\" License along with this manual; if not, write to the Free
22 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
25 .\" 1995-11-26 Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
26 .\" First version written
27 .\" 2003-07-09 Michael Kerrisk <mtk16@ext.canterbury.ac.nz>
28 .\" Added note on suspend mode on laptops
30 .\" Modified, 27 May 2004, Michael Kerrisk <mtk16@ext.canterbury.ac.nz>
31 .\" Added notes on capability requirements
33 .TH MLOCK 2 2004-05-27 "Linux 2.6.6" "Linux Programmer's Manual"
35 mlock \- disable paging for some parts of memory
38 .B #include <sys/mman.h>
40 \fBint mlock(const void *\fIaddr\fB, size_t \fIlen\fB);
44 disables paging for the memory in the range starting at
48 bytes. All pages which contain a part of the specified memory range
49 are guaranteed be resident in RAM when the
51 system call returns successfully and they are guaranteed to stay in RAM
52 until the pages are unlocked by
56 until the pages are unmapped via
58 or until the process terminates or starts another program with
60 Child processes do not inherit page locks across a
63 Memory locking has two main applications: real-time algorithms and
64 high-security data processing. Real-time applications require
65 deterministic timing, and, like scheduling, paging is one major cause
66 of unexpected program execution delays. Real-time applications will
67 usually also switch to a real-time scheduler with
68 .BR sched_setscheduler .
69 Cryptographic security software often handles critical bytes like
70 passwords or secret keys as data structures. As a result of paging,
71 these secrets could be transferred onto a persistent swap store medium,
72 where they might be accessible to the enemy long after the security
73 software has erased the secrets in RAM and terminated.
74 (But be aware that the suspend mode on laptops and some desktop
75 computers will save a copy of the system's RAM to disk, regardless
78 Memory locks do not stack, i.e., pages which have been locked several times
83 will be unlocked by a single call to
85 for the corresponding range or by
87 Pages which are mapped to several locations or by several processes stay
88 locked into RAM as long as they are locked at least at one location or by
91 On POSIX systems on which
96 .B _POSIX_MEMLOCK_RANGE
97 is defined in <unistd.h> and the value
99 from <limits.h> indicates the number of bytes per page.
101 With the Linux system call,
103 is automatically rounded down to the nearest page boundary.
104 However, POSIX 1003.1-2001 allows an implementation to require that
106 is page aligned, so portable applications should ensure this.
110 returns zero. On error, \-1 is returned,
112 is set appropriately, and no changes are made to any locks in the
113 address space of the process.
119 was not a multiple of the page size.
122 Some of the specified address range does not correspond to mapped
123 pages in the address space of the process or the process tried to
124 exceed the maximum number of allowed locked pages.
127 The calling process has insufficient privilege to call
131 capability is required.
139 POSIX.1b, SVr4. SVr4 documents an additional EAGAIN error code.