]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libc/sys/mlock.2
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 .\" 4. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)mlock.2     8.2 (Berkeley) 12/11/93
29 .\" $FreeBSD$
30 .\"
31 .Dd March 18, 2013
32 .Dt MLOCK 2
33 .Os
34 .Sh NAME
35 .Nm mlock ,
36 .Nm munlock
37 .Nd lock (unlock) physical pages in memory
38 .Sh LIBRARY
39 .Lb libc
40 .Sh SYNOPSIS
41 .In sys/mman.h
42 .Ft int
43 .Fn mlock "const void *addr" "size_t len"
44 .Ft int
45 .Fn munlock "const void *addr" "size_t len"
46 .Sh DESCRIPTION
47 The
48 .Fn mlock
49 system call
50 locks into memory the physical pages associated with the virtual address
51 range starting at
52 .Fa addr
53 for
54 .Fa len
55 bytes.
56 The
57 .Fn munlock
58 system call unlocks pages previously locked by one or more
59 .Fn mlock
60 calls.
61 For both, the
62 .Fa addr
63 argument should be aligned to a multiple of the page size.
64 If the
65 .Fa len
66 argument is not a multiple of the page size, it will be rounded up
67 to be so.
68 The entire range must be allocated.
69 .Pp
70 After an
71 .Fn mlock
72 system call, the indicated pages will cause neither a non-resident page
73 nor address-translation fault until they are unlocked.
74 They may still cause protection-violation faults or TLB-miss faults on
75 architectures with software-managed TLBs.
76 The physical pages remain in memory until all locked mappings for the pages
77 are removed.
78 Multiple processes may have the same physical pages locked via their own
79 virtual address mappings.
80 A single process may likewise have pages multiply-locked via different virtual
81 mappings of the same pages or via nested
82 .Fn mlock
83 calls on the same address range.
84 Unlocking is performed explicitly by
85 .Fn munlock
86 or implicitly by a call to
87 .Fn munmap
88 which deallocates the unmapped address range.
89 Locked mappings are not inherited by the child process after a
90 .Xr fork 2 .
91 .Pp
92 Since physical memory is a potentially scarce resource, processes are
93 limited in how much they can lock down.
94 A single process can
95 .Fn mlock
96 the minimum of
97 a system-wide ``wired pages'' limit
98 .Va vm.max_wired
99 and the per-process
100 .Li RLIMIT_MEMLOCK
101 resource limit.
102 .Pp
103 If
104 .Va security.bsd.unprivileged_mlock
105 is set to 0 these calls are only available to the super-user.
106 .Sh RETURN VALUES
107 .Rv -std
108 .Pp
109 If the call succeeds, all pages in the range become locked (unlocked);
110 otherwise the locked status of all pages in the range remains unchanged.
111 .Sh ERRORS
112 The
113 .Fn mlock
114 system call
115 will fail if:
116 .Bl -tag -width Er
117 .It Bq Er EPERM
118 .Va security.bsd.unprivileged_mlock
119 is set to 0 and the caller is not the super-user.
120 .It Bq Er EINVAL
121 The address given is not page aligned or the length is negative.
122 .It Bq Er EAGAIN
123 Locking the indicated range would exceed the system 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 Locking the indicated range would exceed the per-process limit for locked
128 memory.
129 .El
130 The
131 .Fn munlock
132 system call
133 will fail if:
134 .Bl -tag -width Er
135 .It Bq Er EPERM
136 .Va security.bsd.unprivileged_mlock
137 is set to 0 and the caller is not the super-user.
138 .It Bq Er EINVAL
139 The address given is not page aligned or the length is negative.
140 .It Bq Er ENOMEM
141 Some or all of the address range specified by the addr and len
142 arguments does not correspond to valid mapped pages in the address space
143 of the process.
144 .It Bq Er ENOMEM
145 Locking the pages mapped by the specified range would exceed a limit on
146 the amount of memory that the process may lock.
147 .El
148 .Sh "SEE ALSO"
149 .Xr fork 2 ,
150 .Xr mincore 2 ,
151 .Xr minherit 2 ,
152 .Xr mlockall 2 ,
153 .Xr mmap 2 ,
154 .Xr munlockall 2 ,
155 .Xr munmap 2 ,
156 .Xr setrlimit 2 ,
157 .Xr getpagesize 3
158 .Sh HISTORY
159 The
160 .Fn mlock
161 and
162 .Fn munlock
163 system calls first appeared in
164 .Bx 4.4 .
165 .Sh BUGS
166 Allocating too much wired memory can lead to a memory-allocation deadlock
167 which requires a reboot to recover from.
168 .Pp
169 The per-process resource limit is a limit on the amount of virtual
170 memory locked, while the system-wide limit is for the number of locked
171 physical pages.
172 Hence a process with two distinct locked mappings of the same physical page
173 counts as 2 pages against the per-process limit and as only a single page
174 in the system limit.
175 .Pp
176 The per-process resource limit is not currently supported.