.\" <http://www.gnu.org/licenses/>.
.\" %%%LICENSE_END
.\"
-.TH MLOCK 2 2015-12-28 "Linux" "Linux Programmer's Manual"
+.TH MLOCK 2 2020-04-11 "Linux" "Linux Programmer's Manual"
.SH NAME
mlock, mlock2, munlock, mlockall, munlockall \- lock and unlock memory
.SH SYNOPSIS
.nf
.B #include <sys/mman.h>
-.sp
+.PP
.BI "int mlock(const void *" addr ", size_t " len );
.BI "int mlock2(const void *" addr ", size_t " len ", int " flags );
.BI "int munlock(const void *" addr ", size_t " len );
-.sp
+.PP
.BI "int mlockall(int " flags );
.B int munlockall(void);
.fi
lock part or all of the calling process's virtual address
space into RAM, preventing that memory from being paged to the
swap area.
-
+.PP
.BR munlock ()
and
.BR munlockall ()
unlocking part or all of the calling process's virtual
address space, so that pages in the specified virtual address range may
once more to be swapped out if required by the kernel memory manager.
-
+.PP
Memory locking and unlocking are performed in units of whole pages.
.SS mlock(), mlock2(), and munlock()
.BR mlock ()
All pages that contain a part of the specified address range are
guaranteed to be resident in RAM when the call returns successfully;
the pages are guaranteed to stay in RAM until later unlocked.
-
+.PP
.BR mlock2 ()
.\" commit a8ca5d0ecbdde5cc3d7accacbd69968b0c98764e
.\" commit de60f5f10c58d4f34b68622442c0e04180367f3f
returns successfully will depend on the value in the
.I flags
argument.
-
+.PP
The
.I flags
argument can be either 0 or the following constant:
.TP
.B MLOCK_ONFAULT
-Lock pages that are currently resident and mark the entire range to have
-pages locked when they are populated by the page fault.
+Lock pages that are currently resident and mark the entire range so
+that the remaining nonresident pages are locked when they are populated
+by a page fault.
+.PP
.PP
-
If
.I flags
is 0,
.BR mlock2 ()
behaves exactly the same as
.BR mlock ().
-
-Note: currently, there is not a glibc wrapper for
-.BR mlock2 (),
-so it will need to be invoked using
-.BR syscall (2).
-
+.PP
.BR munlock ()
unlocks pages in the address range starting at
.I addr
All mapped pages are guaranteed
to be resident in RAM when the call returns successfully;
the pages are guaranteed to stay in RAM until later unlocked.
-
+.PP
The
.I flags
argument is constructed as the bitwise OR of one or more of the
following constants:
-.TP 1.2i
+.TP
.B MCL_CURRENT
Lock all pages which are currently mapped into the address space of
the process.
the kernel will deny stack expansion and deliver a
.B SIGSEGV
signal to the process.
-
+.PP
.BR munlockall ()
unlocks all pages mapped into the address space of the
calling process.
.RB ( CAP_IPC_LOCK )
to perform the requested operation.
.\"SVr4 documents an additional EAGAIN error code.
-.LP
+.PP
For
.BR mlock (),
.BR mlock2 (),
(For example, unlocking a range in the middle of a currently locked
mapping would result in three mappings:
two locked mappings at each end and an unlocked mapping in the middle.)
-.LP
+.PP
For
.BR mlock2 ():
.TP
.B EINVAL
Unknown \fIflags\fP were specified.
-.LP
+.PP
For
.BR mlockall ():
.TP
.B MCL_FUTURE
or
.BR MCL_CURRENT .
-.LP
+.PP
For
.BR munlockall ():
.TP
(Linux 2.6.8 and earlier) The caller was not privileged
.RB ( CAP_IPC_LOCK ).
.SH VERSIONS
-.BR mlock2 (2)
-is available since Linux 4.4.
+.BR mlock2 ()
+is available since Linux 4.4;
+glibc support was added in version 2.27.
.SH CONFORMING TO
POSIX.1-2001, POSIX.1-2008, SVr4.
-
-mlock2 ()
+.PP
+.BR mlock2 ()
is Linux specific.
.SH AVAILABILITY
On POSIX systems on which
.B PAGESIZE
(if defined) in \fI<limits.h>\fP or by calling
.IR sysconf(_SC_PAGESIZE) .
-
+.PP
On POSIX systems on which
.BR mlockall ()
and
(But be aware that the suspend mode on laptops and some desktop
computers will save a copy of the system's RAM to disk, regardless
of memory locks.)
-
+.PP
Real-time processes that are using
.BR mlockall ()
to prevent delays on page faults should reserve enough
locked into RAM.
The dummy writes ensure that not even copy-on-write
page faults can occur in the critical section.
-
+.PP
Memory locks are not inherited by a child created via
.BR fork (2)
and are automatically removed (unlocked) during an
.BR fork (2)
and are cleared during an
.BR execve (2).
-
+.PP
Note that
.BR fork (2)
will prepare the address space for a copy-on-write operation.
.BR mlock ()
operation\(emnot even from a thread which runs at a low priority within
a process which also has a thread running at elevated priority.
-
+.PP
The memory lock on an address range is automatically removed
if the address range is unmapped via
.BR munmap (2).
-
+.PP
Memory locks do not stack, that is, pages which have been locked several times
by calls to
.BR mlock (),
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.
-
+.PP
If a call to
.BR mlockall ()
which uses the
changes made by the
.B MCL_FUTURE
call will be lost.
-
+.PP
The
.BR mlock2 ()
.B MLOCK_ONFAULT
allows an implementation to require that
.I addr
is page aligned, so portable applications should ensure this.
-
+.PP
The
.I VmLck
field of the Linux-specific
-.I /proc/PID/status
+.I /proc/[pid]/status
file shows how many kilobytes of memory the process with ID
.I PID
has locked using
in order to lock memory and the
.B RLIMIT_MEMLOCK
soft resource limit defines a limit on how much memory the process may lock.
-
+.PP
Since Linux 2.6.9, no limits are placed on the amount of memory
that a privileged process can lock and the
.B RLIMIT_MEMLOCK
soft resource limit instead defines a limit on how much memory an
unprivileged process may lock.
.SH BUGS
+In Linux 4.8 and earlier,
+a bug in the kernel's accounting of locked memory for unprivileged processes
+(i.e., without
+.BR CAP_IPC_LOCK )
+meant that if the region specified by
+.I addr
+and
+.I len
+overlapped an existing lock,
+then the already locked bytes in the overlapping region were counted twice
+when checking against the limit.
+Such double accounting could incorrectly calculate a "total locked memory"
+value for the process that exceeded the
+.BR RLIMIT_MEMLOCK
+limit, with the result that
+.BR mlock ()
+and
+.BR mlock2 ()
+would fail on requests that should have succeeded.
+This bug was fixed
+.\" commit 0cf2f6f6dc605e587d2c1120f295934c77e810e8
+in Linux 4.9
+.PP
In the 2.4 series Linux kernels up to and including 2.4.17,
a bug caused the
.BR mlockall ()
flag to be inherited across a
.BR fork (2).
This was rectified in kernel 2.4.18.
-
+.PP
Since kernel 2.6.9, if a privileged process calls
.I mlockall(MCL_FUTURE)
and later drops privileges (loses the
.\" "Rationale for RLIMIT_MEMLOCK"
.\" 23 Jan 2006
.SH SEE ALSO
+.BR mincore (2),
.BR mmap (2),
.BR setrlimit (2),
.BR shmctl (2),