xref: /netbsd-src/lib/libc/sys/mlock.2 (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1.\"	$NetBSD: mlock.2,v 1.12 2000/09/28 09:46:17 is 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.It Bq Er EPERM
127.Fn mlock
128was called by non-root on an architecture were locked page accounting
129is not implemented.
130.Pp
131.El
132.Fn munlock
133will fail if:
134.Bl -tag -width Er
135.It Bq Er EINVAL
136The address given is not page aligned or the length is negative.
137.It Bq Er ENOMEM
138Some portion of the indicated address range is not allocated.
139Some portion of the indicated address range is not locked.
140.El
141.Sh SEE ALSO
142.Xr fork 2 ,
143.Xr mincore 2 ,
144.Xr mmap 2 ,
145.Xr munmap 2 ,
146.Xr setrlimit 2 ,
147.Xr getpagesize 3
148.Sh STANDARDS
149The
150.Fn mlock
151and
152.Fn munlock
153functions conform to
154.St -p1003.1b-93 .
155.Sh HISTORY
156The
157.Fn mlock
158and
159.Fn munlock
160functions first appeared in
161.Bx 4.4 .
162.Sh BUGS
163The per-process resource limit is a limit on the amount of virtual
164memory locked, while the system-wide limit is for the number of locked
165physical pages.
166Hence a process with two distinct locked mappings of the same physical page
167counts as 2 pages against the per-process limit and as only a single page
168in the system limit.
169