xref: /dflybsd-src/lib/libc/sys/mlockall.2 (revision c936cb6fb98d3c40c4f9174ee19179e918a2b544)
1c9b0e5eaSVenkatesh Srinivas.\"     $OpenBSD: mlockall.2,v 1.5 2008/06/26 05:42:05 ray Exp $
2c9b0e5eaSVenkatesh Srinivas.\"     $NetBSD: mlockall.2,v 1.6 2000/06/26 17:00:02 kleink Exp $
3c9b0e5eaSVenkatesh Srinivas.\"
4c9b0e5eaSVenkatesh Srinivas.\" Copyright (c) 1999 The NetBSD Foundation, Inc.
5c9b0e5eaSVenkatesh Srinivas.\" All rights reserved.
6c9b0e5eaSVenkatesh Srinivas.\"
7c9b0e5eaSVenkatesh Srinivas.\" This code is derived from software contributed to The NetBSD Foundation
8c9b0e5eaSVenkatesh Srinivas.\" by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9c9b0e5eaSVenkatesh Srinivas.\" NASA Ames Research Center.
10c9b0e5eaSVenkatesh Srinivas.\"
11c9b0e5eaSVenkatesh Srinivas.\" Redistribution and use in source and binary forms, with or without
12c9b0e5eaSVenkatesh Srinivas.\" modification, are permitted provided that the following conditions
13c9b0e5eaSVenkatesh Srinivas.\" are met:
14c9b0e5eaSVenkatesh Srinivas.\" 1. Redistributions of source code must retain the above copyright
15c9b0e5eaSVenkatesh Srinivas.\"    notice, this list of conditions and the following disclaimer.
16c9b0e5eaSVenkatesh Srinivas.\" 2. Redistributions in binary form must reproduce the above copyright
17c9b0e5eaSVenkatesh Srinivas.\"    notice, this list of conditions and the following disclaimer in the
18c9b0e5eaSVenkatesh Srinivas.\"    documentation and/or other materials provided with the distribution.
19c9b0e5eaSVenkatesh Srinivas.\" 3. All advertising materials mentioning features or use of this software
20c9b0e5eaSVenkatesh Srinivas.\"    must display the following acknowledgement:
21c9b0e5eaSVenkatesh Srinivas.\"        This product includes software developed by the NetBSD
22c9b0e5eaSVenkatesh Srinivas.\"        Foundation, Inc. and its contributors.
23c9b0e5eaSVenkatesh Srinivas.\" 4. Neither the name of The NetBSD Foundation nor the names of its
24c9b0e5eaSVenkatesh Srinivas.\"    contributors may be used to endorse or promote products derived
25c9b0e5eaSVenkatesh Srinivas.\"    from this software without specific prior written permission.
26c9b0e5eaSVenkatesh Srinivas.\"
27c9b0e5eaSVenkatesh Srinivas.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28c9b0e5eaSVenkatesh Srinivas.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29c9b0e5eaSVenkatesh Srinivas.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30c9b0e5eaSVenkatesh Srinivas.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31c9b0e5eaSVenkatesh Srinivas.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32c9b0e5eaSVenkatesh Srinivas.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33c9b0e5eaSVenkatesh Srinivas.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34c9b0e5eaSVenkatesh Srinivas.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35c9b0e5eaSVenkatesh Srinivas.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36c9b0e5eaSVenkatesh Srinivas.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37c9b0e5eaSVenkatesh Srinivas.\" POSSIBILITY OF SUCH DAMAGE.
38c9b0e5eaSVenkatesh Srinivas.\"
39c52cfb36SAntonio Huete Jimenez.Dd May 16, 2022
40c9b0e5eaSVenkatesh Srinivas.Dt MLOCKALL 2
41c9b0e5eaSVenkatesh Srinivas.Os
42c9b0e5eaSVenkatesh Srinivas.Sh NAME
43c9b0e5eaSVenkatesh Srinivas.Nm mlockall ,
44c9b0e5eaSVenkatesh Srinivas.Nm munlockall
45c9b0e5eaSVenkatesh Srinivas.Nd lock (unlock) the address space of a process
46c9b0e5eaSVenkatesh Srinivas.Sh LIBRARY
47c9b0e5eaSVenkatesh Srinivas.Lb libc
48c9b0e5eaSVenkatesh Srinivas.Sh SYNOPSIS
49c9b0e5eaSVenkatesh Srinivas.In sys/types.h
50c9b0e5eaSVenkatesh Srinivas.In sys/mman.h
51c9b0e5eaSVenkatesh Srinivas.Ft int
52c9b0e5eaSVenkatesh Srinivas.Fn mlockall "int flags"
53c9b0e5eaSVenkatesh Srinivas.Ft int
54c9b0e5eaSVenkatesh Srinivas.Fn munlockall "void"
55c9b0e5eaSVenkatesh Srinivas.Sh DESCRIPTION
56c9b0e5eaSVenkatesh SrinivasThe
57c9b0e5eaSVenkatesh Srinivas.Fn mlockall
58c9b0e5eaSVenkatesh Srinivassystem call locks into memory the physical pages associated with the
59c9b0e5eaSVenkatesh Srinivasaddress space of a process until the address space is unlocked, the
60*c936cb6fSMatthew Dillonprocess exits, fork()s, or execs another program image.  Any pages which
61*c936cb6fSMatthew Dillonare copy-on-write at the time of the function call will be force faulted.
62*c936cb6fSMatthew DillonLocked pages will not be paged to swap backing store.
63c9b0e5eaSVenkatesh Srinivas.Pp
64c9b0e5eaSVenkatesh SrinivasThe following flags affect the behavior of
65c9b0e5eaSVenkatesh Srinivas.Fn mlockall :
66c9b0e5eaSVenkatesh Srinivas.Bl -tag -width ".Dv MCL_CURRENT"
67c9b0e5eaSVenkatesh Srinivas.It Dv MCL_CURRENT
68c9b0e5eaSVenkatesh SrinivasLock all pages currently mapped into the process's address space.
69c9b0e5eaSVenkatesh Srinivas.It Dv MCL_FUTURE
70c9b0e5eaSVenkatesh SrinivasLock all pages mapped into the process's address space in the future,
71c9b0e5eaSVenkatesh Srinivasat the time the mapping is established.
72c9b0e5eaSVenkatesh SrinivasNote that this may cause future mappings to fail if those mappings
73c9b0e5eaSVenkatesh Srinivascause resource limits to be exceeded.
74c9b0e5eaSVenkatesh Srinivas.El
75c9b0e5eaSVenkatesh Srinivas.Pp
76c9b0e5eaSVenkatesh SrinivasSince physical memory is a potentially scarce resource, processes are
77c9b0e5eaSVenkatesh Srinivaslimited in how much they can lock down.
78c9b0e5eaSVenkatesh SrinivasA single process can lock the minimum of a system-wide
79c9b0e5eaSVenkatesh Srinivas.Dq wired pages
80c9b0e5eaSVenkatesh Srinivaslimit and the per-process
81c9b0e5eaSVenkatesh Srinivas.Dv RLIMIT_MEMLOCK
82c9b0e5eaSVenkatesh Srinivasresource limit.
83c9b0e5eaSVenkatesh Srinivas.Pp
84*c936cb6fSMatthew Dillon.Fn mlockall
85*c936cb6fSMatthew Dillonhas two limitations, necessary to keep it functional on modern systems.
86*c936cb6fSMatthew DillonThe first is that writable file-backed MAP_PRIVATE pages (such as shared
87*c936cb6fSMatthew Dillonlibrary mappings) which have not yet been write-faulted will retain their
88*c936cb6fSMatthew Dillonread-only mapping to the file backing store and not be force-copied.
89*c936cb6fSMatthew DillonIf we were to force copy these pages, it would cause immense unnecessary
90*c936cb6fSMatthew Dillonoverheads for the program.
91*c936cb6fSMatthew DillonSo any unmodified but writable pages which are currently in
92*c936cb6fSMatthew Dillonthe pmap read-only will still take a COW fault if written to.
93*c936cb6fSMatthew Dillon.Pp
94*c936cb6fSMatthew DillonThe second limitation is that when a fork() is issued, all writable pages
95*c936cb6fSMatthew Dillonwill be made copy-on-write (COW) in both the parent and the child.  The child
96*c936cb6fSMatthew Dillonof course does not inherit the locked memory state, but this action will
97*c936cb6fSMatthew Dilloncause any locked pages in the parent to become copy-on-write and they will
98*c936cb6fSMatthew Dillonbe faulted if written to.  So they will not be quite as locked as might have
99*c936cb6fSMatthew Dillonbeen intended in this situation.
100*c936cb6fSMatthew Dillon.Pp
101c9b0e5eaSVenkatesh SrinivasThe
102c9b0e5eaSVenkatesh Srinivas.Fn munlockall
103c9b0e5eaSVenkatesh Srinivascall unlocks any locked memory regions in the process address space.
104c9b0e5eaSVenkatesh SrinivasAny regions mapped after an
105c9b0e5eaSVenkatesh Srinivas.Fn munlockall
106c9b0e5eaSVenkatesh Srinivascall will not be locked.
107c9b0e5eaSVenkatesh Srinivas.Sh RETURN VALUES
108c9b0e5eaSVenkatesh SrinivasA return value of 0 indicates that the call
109c9b0e5eaSVenkatesh Srinivassucceeded and all pages in the range have either been locked or unlocked.
110c9b0e5eaSVenkatesh SrinivasA return value of \-1 indicates an error occurred and the locked
111c9b0e5eaSVenkatesh Srinivasstatus of all pages in the range remains unchanged.
112c9b0e5eaSVenkatesh SrinivasIn this case, the global location
113c9b0e5eaSVenkatesh Srinivas.Va errno
114c9b0e5eaSVenkatesh Srinivasis set to indicate the error.
115c9b0e5eaSVenkatesh Srinivas.Sh ERRORS
116c9b0e5eaSVenkatesh Srinivas.Fn mlockall
117c9b0e5eaSVenkatesh Srinivaswill fail if:
118c9b0e5eaSVenkatesh Srinivas.Bl -tag -width Er
119c9b0e5eaSVenkatesh Srinivas.It Bq Er EINVAL
120c9b0e5eaSVenkatesh SrinivasThe
121c9b0e5eaSVenkatesh Srinivas.Fa flags
122c9b0e5eaSVenkatesh Srinivasargument is zero, or includes unimplemented flags.
123c9b0e5eaSVenkatesh Srinivas.It Bq Er ENOMEM
124c9b0e5eaSVenkatesh SrinivasLocking the indicated range would exceed either the system or per-process
125c9b0e5eaSVenkatesh Srinivaslimit for locked memory.
126c9b0e5eaSVenkatesh Srinivas.It Bq Er EPERM
127c9b0e5eaSVenkatesh SrinivasThe calling process does not have the appropriate privilege to perform
128c9b0e5eaSVenkatesh Srinivasthe requested operation.
129c9b0e5eaSVenkatesh Srinivas.El
130c9b0e5eaSVenkatesh Srinivas.Sh SEE ALSO
131c9b0e5eaSVenkatesh Srinivas.Xr mincore 2 ,
132c9b0e5eaSVenkatesh Srinivas.Xr mlock 2 ,
133c9b0e5eaSVenkatesh Srinivas.Xr setrlimit 2
134c9b0e5eaSVenkatesh Srinivas.Sh STANDARDS
135c9b0e5eaSVenkatesh SrinivasThe
136c9b0e5eaSVenkatesh Srinivas.Fn mlockall
137c9b0e5eaSVenkatesh Srinivasand
138c9b0e5eaSVenkatesh Srinivas.Fn munlockall
139c9b0e5eaSVenkatesh Srinivasfunctions are believed to conform to
140c9b0e5eaSVenkatesh Srinivas.St -p1003.1-2001 .
141c9b0e5eaSVenkatesh Srinivas.Sh HISTORY
142c9b0e5eaSVenkatesh SrinivasThe
143c9b0e5eaSVenkatesh Srinivas.Fn mlockall
144c9b0e5eaSVenkatesh Srinivasand
145c9b0e5eaSVenkatesh Srinivas.Fn munlockall
146c9b0e5eaSVenkatesh Srinivasfunctions first appeared in
147c9b0e5eaSVenkatesh Srinivas.Dx 2.9 .
148c9b0e5eaSVenkatesh Srinivas.Sh BUGS
149*c936cb6fSMatthew DillonHow could there be any bugs?  This is soooo simple...
150*c936cb6fSMatthew Dillon.Pp
151*c936cb6fSMatthew DillonThese system calls are not recommended for general use.  They are obviously
152*c936cb6fSMatthew Dillonnot thread-safe, and the larger application context from which they are
153*c936cb6fSMatthew Dilloncalled might be hostile to such actions due to non-deterministic resource
154*c936cb6fSMatthew Dillonlimits in the system.  In a modern system, even semi-realtime and interactive
155*c936cb6fSMatthew Dillonprocesses are already detected and handled by the system schedule.
156*c936cb6fSMatthew DillonPlease use
157*c936cb6fSMatthew Dillon.Fn mlock
158*c936cb6fSMatthew Dillonand
159*c936cb6fSMatthew Dillon.Fn munlock
160*c936cb6fSMatthew Dillonto lock specific address ranges instead of locking the entire address space.
161