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