]> git.ipfire.org Git - thirdparty/man-pages.git/blobdiff - man2/mlock.2
move_pages.2: Minor tweaks to Yang Shi's patch
[thirdparty/man-pages.git] / man2 / mlock.2
index 08387b0bbebb8f0426a608a9bc21b314a4b2ca50..9eacfcfae2571b25aff7af48ac11d2e6a7e2f8ab 100644 (file)
 .\" <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
@@ -45,7 +45,7 @@ and
 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 ()
@@ -53,7 +53,7 @@ perform the converse operation,
 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 ()
@@ -65,7 +65,7 @@ bytes.
 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
@@ -79,28 +79,24 @@ However, the state of the pages contained in that range after the call
 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
@@ -119,12 +115,12 @@ memory, and memory-mapped files.
 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.
@@ -175,7 +171,7 @@ In the same circumstances, stack growth may likewise fail:
 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.
@@ -212,7 +208,7 @@ The caller is not privileged, but needs privilege
 .RB ( CAP_IPC_LOCK )
 to perform the requested operation.
 .\"SVr4 documents an additional EAGAIN error code.
-.LP
+.PP
 For
 .BR mlock (),
 .BR mlock2 (),
@@ -246,13 +242,13 @@ exceeding the allowed maximum.
 (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
@@ -263,7 +259,7 @@ was specified without either
 .B MCL_FUTURE
 or
 .BR MCL_CURRENT .
-.LP
+.PP
 For
 .BR munlockall ():
 .TP
@@ -271,12 +267,13 @@ For
 (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
@@ -290,7 +287,7 @@ can be determined from the constant
 .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
@@ -321,7 +318,7 @@ software has erased the secrets in RAM and terminated.
 (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
@@ -334,7 +331,7 @@ This way, enough pages will be mapped for the stack and can be
 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
@@ -349,7 +346,7 @@ settings are not inherited by a child created via
 .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.
@@ -363,11 +360,11 @@ or
 .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 (),
@@ -381,7 +378,7 @@ for the corresponding range or by
 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
@@ -390,7 +387,7 @@ flag is followed by another call that does not specify this flag, the
 changes made by the
 .B MCL_FUTURE
 call will be lost.
-
+.PP
 The
 .BR mlock2 ()
 .B MLOCK_ONFAULT
@@ -417,11 +414,11 @@ and
 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
@@ -438,13 +435,36 @@ a process must be privileged
 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 ()
@@ -452,7 +472,7 @@ a bug caused the
 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
@@ -470,6 +490,7 @@ resource limit is encountered.
 .\" "Rationale for RLIMIT_MEMLOCK"
 .\" 23 Jan 2006
 .SH SEE ALSO
+.BR mincore (2),
 .BR mmap (2),
 .BR setrlimit (2),
 .BR shmctl (2),