xref: /netbsd-src/lib/libc/sys/mlock.2 (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1.\"	$NetBSD: mlock.2,v 1.11 2000/06/26 17:00:02 kleink Exp $
2.\"
3.\" Copyright (c) 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"	@(#)mlock.2	8.2 (Berkeley) 12/11/93
35.\"
36.Dd June 2, 1993
37.Dt MLOCK 2
38.Os
39.Sh NAME
40.Nm mlock ,
41.Nm munlock
42.Nd lock (unlock) physical pages in memory
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.Fd #include <sys/mman.h>
47.Ft int
48.Fn mlock "void *addr" "size_t len"
49.Ft int
50.Fn munlock "void *addr" "size_t len"
51.Sh DESCRIPTION
52The
53.Nm mlock
54system call
55locks into memory the physical pages associated with the virtual address
56range starting at
57.Fa addr
58for
59.Fa len
60bytes.
61The
62.Nm munlock
63call unlocks pages previously locked by one or more
64.Nm mlock
65calls.
66For both, the
67.Fa addr
68parameter should be aligned to a multiple of the page size.
69If the
70.Fa len
71parameter is not a multiple of the page size, it will be rounded up
72to be so.
73The entire range must be allocated.
74.Pp
75After an
76.Nm mlock
77call, the indicated pages will cause neither a non-resident page
78nor address-translation fault until they are unlocked.
79They may still cause protection-violation faults or TLB-miss faults on
80architectures with software-managed TLBs.
81The physical pages remain in memory until all locked mappings for the pages
82are removed.
83Multiple processes may have the same physical pages locked via their own
84virtual address mappings.
85A single process may likewise have pages multiply-locked via different virtual
86mappings of the same pages or via nested
87.Nm mlock
88calls on the same address range.
89Unlocking is performed explicitly by
90.Nm munlock
91or implicitly by a call to
92.Nm munmap
93which deallocates the unmapped address range.
94Locked mappings are not inherited by the child process after a
95.Xr fork 2 .
96.Pp
97Since physical memory is a potentially scarce resource, processes are
98limited in how much they can lock down.
99A single process can
100.Nm mlock
101the minimum of
102a system-wide ``wired pages'' limit and
103the per-process
104.Li RLIMIT_MEMLOCK
105resource limit.
106.Sh RETURN VALUES
107A return value of 0 indicates that the call
108succeeded and all pages in the range have either been locked or unlocked.
109A return value of -1 indicates an error occurred and the locked
110status of all pages in the range remains unchanged.
111In this case, the global location
112.Va errno
113is set to indicate the error.
114.Sh ERRORS
115.Fn mlock
116will fail if:
117.Bl -tag -width Er
118.It Bq Er EINVAL
119The address given is not page aligned or the length is negative.
120.It Bq Er EAGAIN
121Locking the indicated range would exceed either the system or per-process
122limit for locked memory.
123.It Bq Er ENOMEM
124Some portion of the indicated address range is not allocated.
125There was an error faulting/mapping a page.
126.El
127.Fn munlock
128will fail if:
129.Bl -tag -width Er
130.It Bq Er EINVAL
131The address given is not page aligned or the length is negative.
132.It Bq Er ENOMEM
133Some portion of the indicated address range is not allocated.
134Some portion of the indicated address range is not locked.
135.El
136.Sh SEE ALSO
137.Xr fork 2 ,
138.Xr mincore 2 ,
139.Xr mmap 2 ,
140.Xr munmap 2 ,
141.Xr setrlimit 2 ,
142.Xr getpagesize 3
143.Sh STANDARDS
144The
145.Fn mlock
146and
147.Fn munlock
148functions conform to
149.St -p1003.1b-93 .
150.Sh HISTORY
151The
152.Fn mlock
153and
154.Fn munlock
155functions first appeared in
156.Bx 4.4 .
157.Sh BUGS
158The per-process resource limit is a limit on the amount of virtual
159memory locked, while the system-wide limit is for the number of locked
160physical pages.
161Hence a process with two distinct locked mappings of the same physical page
162counts as 2 pages against the per-process limit and as only a single page
163in the system limit.
164