1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) Markus Kuhn, 1996
.\"
.\" This is free documentation; you can redistribute it and/or
.\" modify it under the terms of the GNU General Public License as
.\" published by the Free Software Foundation; either version 2 of
.\" the License, or (at your option) any later version.
.\"
.\" The GNU General Public License's references to "object code"
.\" and "executables" are to be interpreted as the output of any
.\" document formatting or typesetting system, including
.\" intermediate and printed output.
.\"
.\" This manual is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public
.\" License along with this manual; if not, write to the Free
.\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
.\" USA.
.\"
.\" 1995-11-26 Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
.\" First version written
.\"
.TH MLOCK 2 "1995-11-26" "Linux 1.3.43" "Linux Programmer's Manual"
.SH NAME
mlock \- disable paging for some parts of memory
.SH SYNOPSIS
.nf
.B #include <sys/mman.h>
.sp
\fBint mlock(const void *\fIaddr\fB, size_t \fIlen\fB);
.fi
.SH DESCRIPTION
.B mlock
disables paging for the memory in the range starting at
.I addr
with length
.I len
bytes. All pages which contain a part of the specified memory range
are guaranteed be resident in RAM when the
.B mlock
system call returns successfully and they are guaranteed to stay in RAM
until the pages are unlocked by
.B munlock
or
.BR munlockall ,
or until the process terminates or starts another program with
.BR exec .
Child processes do not inherit page locks across a
.BR fork .
Memory locking has two main applications: real-time algorithms and
high-security data processing. Real-time applications require
deterministic timing, and, like scheduling, paging is one major cause
of unexpected program execution delays. Real-time applications will
usually also switch to a real-time scheduler with
.BR sched_setscheduler .
Cryptographic security software often handles critical bytes like
passwords or secret keys as data structures. As a result of paging,
these secrets could be transfered onto a persistent swap store medium,
where they might be accessible to the enemy long after the security
software has erased the secrets in RAM and terminated.
Memory locks do not stack, i.e., pages which have been locked several times
by calls to
.B mlock
or
.B mlockall
will be unlocked by a single call to
.B munlock
for the corresponding range or by
.BR munlockall .
Pages which are mapped to several locations or by several processes stay
locked into RAM as long as they are locked at least at one location or by
at least one process.
On POSIX systems on which
.B mlock
and
.B munlock
are available,
.B _POSIX_MEMLOCK_RANGE
is defined in <unistd.h> and the value
.B PAGESIZE
from <limits.h> indicates the number of bytes per page.
.SH RETURN VALUE
On success,
.B mlock
returns zero. On error, \-1 is returned,
.I errno
is set appropriately, and no changes are made to any locks in the
address space of the process.
.SH ERRORS
.TP
.B ENOMEM
Some of the specified address range does not correspond to mapped
pages in the address space of the process or the process tried to
exceed the maximum number of allowed locked pages.
.TP
.B EPERM
The calling process does not have appropriate privileges. Only root
processes are allowed to lock pages.
.TP
.B EINVAL
.I len
was not a positive number.
.SH CONFORMING TO
POSIX.1b, SVr4. SVr4 documents an additional EAGAIN error code.
.SH SEE ALSO
.BR munlock (2),
.BR mlockall (2),
.BR munlockall (2)
|