xref: /openbsd-src/lib/libc/sys/mlock.2 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: mlock.2,v 1.11 2001/03/09 15:15:20 art Exp $
2.\"	$NetBSD: mlock.2,v 1.3 1995/06/24 10:42:03 cgd Exp $
3.\"
4.\" Copyright (c) 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"	@(#)mlock.2	8.2 (Berkeley) 12/11/93
36.\"
37.Dd June 2, 1993
38.Dt MLOCK 2
39.Os
40.Sh NAME
41.Nm mlock ,
42.Nm munlock
43.Nd lock (unlock) physical pages in memory
44.Sh SYNOPSIS
45.Fd #include <sys/types.h>
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 minherit 2 ,
140.Xr mlockall 2 ,
141.Xr mmap 2 ,
142.Xr munmap 2 ,
143.Xr setrlimit 2 ,
144.Xr getpagesize 3
145.Sh BUGS
146Unlike The Sun implementation, multiple
147.Nm mlock
148calls on the same address range require the corresponding number of
149.Nm munlock
150calls to actually unlock the pages, i.e.,
151.Nm mlock
152nests.
153This should be considered a consequence of the implementation
154and not a feature.
155.Pp
156The per-process resource limit is a limit on the amount of virtual
157memory locked, while the system-wide limit is for the number of locked
158physical pages.
159Hence a process with two distinct locked mappings of the same physical page
160counts as 2 pages against the per-process limit and as only a single page
161in the system limit.
162.Sh HISTORY
163The
164.Fn mlock
165and
166.Fn munlock
167functions first appeared in
168.Bx 4.4 .
169