1*8269e767SBrooks Davis.\" Copyright (c) 1993 2*8269e767SBrooks Davis.\" The Regents of the University of California. All rights reserved. 3*8269e767SBrooks Davis.\" 4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 6*8269e767SBrooks Davis.\" are met: 7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 8*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 10*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 11*8269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors 13*8269e767SBrooks Davis.\" may be used to endorse or promote products derived from this software 14*8269e767SBrooks Davis.\" without specific prior written permission. 15*8269e767SBrooks Davis.\" 16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*8269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*8269e767SBrooks Davis.\" SUCH DAMAGE. 27*8269e767SBrooks Davis.\" 28*8269e767SBrooks Davis.Dd May 13, 2019 29*8269e767SBrooks Davis.Dt MLOCK 2 30*8269e767SBrooks Davis.Os 31*8269e767SBrooks Davis.Sh NAME 32*8269e767SBrooks Davis.Nm mlock , 33*8269e767SBrooks Davis.Nm munlock 34*8269e767SBrooks Davis.Nd lock (unlock) physical pages in memory 35*8269e767SBrooks Davis.Sh LIBRARY 36*8269e767SBrooks Davis.Lb libc 37*8269e767SBrooks Davis.Sh SYNOPSIS 38*8269e767SBrooks Davis.In sys/mman.h 39*8269e767SBrooks Davis.Ft int 40*8269e767SBrooks Davis.Fn mlock "const void *addr" "size_t len" 41*8269e767SBrooks Davis.Ft int 42*8269e767SBrooks Davis.Fn munlock "const void *addr" "size_t len" 43*8269e767SBrooks Davis.Sh DESCRIPTION 44*8269e767SBrooks DavisThe 45*8269e767SBrooks Davis.Fn mlock 46*8269e767SBrooks Davissystem call 47*8269e767SBrooks Davislocks into memory the physical pages associated with the virtual address 48*8269e767SBrooks Davisrange starting at 49*8269e767SBrooks Davis.Fa addr 50*8269e767SBrooks Davisfor 51*8269e767SBrooks Davis.Fa len 52*8269e767SBrooks Davisbytes. 53*8269e767SBrooks DavisThe 54*8269e767SBrooks Davis.Fn munlock 55*8269e767SBrooks Davissystem call unlocks pages previously locked by one or more 56*8269e767SBrooks Davis.Fn mlock 57*8269e767SBrooks Daviscalls. 58*8269e767SBrooks DavisFor both, the 59*8269e767SBrooks Davis.Fa addr 60*8269e767SBrooks Davisargument should be aligned to a multiple of the page size. 61*8269e767SBrooks DavisIf the 62*8269e767SBrooks Davis.Fa len 63*8269e767SBrooks Davisargument is not a multiple of the page size, it will be rounded up 64*8269e767SBrooks Davisto be so. 65*8269e767SBrooks DavisThe entire range must be allocated. 66*8269e767SBrooks Davis.Pp 67*8269e767SBrooks DavisAfter an 68*8269e767SBrooks Davis.Fn mlock 69*8269e767SBrooks Davissystem call, the indicated pages will cause neither a non-resident page 70*8269e767SBrooks Davisnor address-translation fault until they are unlocked. 71*8269e767SBrooks DavisThey may still cause protection-violation faults or TLB-miss faults on 72*8269e767SBrooks Davisarchitectures with software-managed TLBs. 73*8269e767SBrooks DavisThe physical pages remain in memory until all locked mappings for the pages 74*8269e767SBrooks Davisare removed. 75*8269e767SBrooks DavisMultiple processes may have the same physical pages locked via their own 76*8269e767SBrooks Davisvirtual address mappings. 77*8269e767SBrooks DavisA single process may likewise have pages multiply-locked via different virtual 78*8269e767SBrooks Davismappings of the same physical pages. 79*8269e767SBrooks DavisUnlocking is performed explicitly by 80*8269e767SBrooks Davis.Fn munlock 81*8269e767SBrooks Davisor implicitly by a call to 82*8269e767SBrooks Davis.Fn munmap 83*8269e767SBrooks Daviswhich deallocates the unmapped address range. 84*8269e767SBrooks DavisLocked mappings are not inherited by the child process after a 85*8269e767SBrooks Davis.Xr fork 2 . 86*8269e767SBrooks Davis.Pp 87*8269e767SBrooks DavisSince physical memory is a potentially scarce resource, processes are 88*8269e767SBrooks Davislimited in how much they can lock down. 89*8269e767SBrooks DavisThe amount of memory that a single process can 90*8269e767SBrooks Davis.Fn mlock 91*8269e767SBrooks Davisis limited by both the per-process 92*8269e767SBrooks Davis.Dv RLIMIT_MEMLOCK 93*8269e767SBrooks Davisresource limit and the 94*8269e767SBrooks Davissystem-wide 95*8269e767SBrooks Davis.Dq wired pages 96*8269e767SBrooks Davislimit 97*8269e767SBrooks Davis.Va vm.max_user_wired . 98*8269e767SBrooks Davis.Va vm.max_user_wired 99*8269e767SBrooks Davisapplies to the system as a whole, so the amount available to a single 100*8269e767SBrooks Davisprocess at any given time is the difference between 101*8269e767SBrooks Davis.Va vm.max_user_wired 102*8269e767SBrooks Davisand 103*8269e767SBrooks Davis.Va vm.stats.vm.v_user_wire_count . 104*8269e767SBrooks Davis.Pp 105*8269e767SBrooks DavisIf 106*8269e767SBrooks Davis.Va security.bsd.unprivileged_mlock 107*8269e767SBrooks Davisis set to 0 these calls are only available to the super-user. 108*8269e767SBrooks Davis.Sh RETURN VALUES 109*8269e767SBrooks Davis.Rv -std 110*8269e767SBrooks Davis.Pp 111*8269e767SBrooks DavisIf the call succeeds, all pages in the range become locked (unlocked); 112*8269e767SBrooks Davisotherwise the locked status of all pages in the range remains unchanged. 113*8269e767SBrooks Davis.Sh ERRORS 114*8269e767SBrooks DavisThe 115*8269e767SBrooks Davis.Fn mlock 116*8269e767SBrooks Davissystem call 117*8269e767SBrooks Daviswill fail if: 118*8269e767SBrooks Davis.Bl -tag -width Er 119*8269e767SBrooks Davis.It Bq Er EPERM 120*8269e767SBrooks Davis.Va security.bsd.unprivileged_mlock 121*8269e767SBrooks Davisis set to 0 and the caller is not the super-user. 122*8269e767SBrooks Davis.It Bq Er EINVAL 123*8269e767SBrooks DavisThe address range given wraps around zero. 124*8269e767SBrooks Davis.It Bq Er ENOMEM 125*8269e767SBrooks DavisSome portion of the indicated address range is not allocated. 126*8269e767SBrooks DavisThere was an error faulting/mapping a page. 127*8269e767SBrooks DavisLocking the indicated range would exceed the per-process or system-wide limits 128*8269e767SBrooks Davisfor locked memory. 129*8269e767SBrooks Davis.El 130*8269e767SBrooks DavisThe 131*8269e767SBrooks Davis.Fn munlock 132*8269e767SBrooks Davissystem call 133*8269e767SBrooks Daviswill fail if: 134*8269e767SBrooks Davis.Bl -tag -width Er 135*8269e767SBrooks Davis.It Bq Er EPERM 136*8269e767SBrooks Davis.Va security.bsd.unprivileged_mlock 137*8269e767SBrooks Davisis set to 0 and the caller is not the super-user. 138*8269e767SBrooks Davis.It Bq Er EINVAL 139*8269e767SBrooks DavisThe address range given wraps around zero. 140*8269e767SBrooks Davis.It Bq Er ENOMEM 141*8269e767SBrooks DavisSome or all of the address range specified by the addr and len 142*8269e767SBrooks Davisarguments does not correspond to valid mapped pages in the address space 143*8269e767SBrooks Davisof the process. 144*8269e767SBrooks Davis.It Bq Er ENOMEM 145*8269e767SBrooks DavisLocking the pages mapped by the specified range would exceed a limit on 146*8269e767SBrooks Davisthe amount of memory that the process may lock. 147*8269e767SBrooks Davis.El 148*8269e767SBrooks Davis.Sh "SEE ALSO" 149*8269e767SBrooks Davis.Xr fork 2 , 150*8269e767SBrooks Davis.Xr mincore 2 , 151*8269e767SBrooks Davis.Xr minherit 2 , 152*8269e767SBrooks Davis.Xr mlockall 2 , 153*8269e767SBrooks Davis.Xr mmap 2 , 154*8269e767SBrooks Davis.Xr munlockall 2 , 155*8269e767SBrooks Davis.Xr munmap 2 , 156*8269e767SBrooks Davis.Xr setrlimit 2 , 157*8269e767SBrooks Davis.Xr getpagesize 3 158*8269e767SBrooks Davis.Sh HISTORY 159*8269e767SBrooks DavisThe 160*8269e767SBrooks Davis.Fn mlock 161*8269e767SBrooks Davisand 162*8269e767SBrooks Davis.Fn munlock 163*8269e767SBrooks Davissystem calls first appeared in 164*8269e767SBrooks Davis.Bx 4.4 . 165*8269e767SBrooks Davis.Sh BUGS 166*8269e767SBrooks DavisAllocating too much wired memory can lead to a memory-allocation deadlock 167*8269e767SBrooks Daviswhich requires a reboot to recover from. 168*8269e767SBrooks Davis.Pp 169*8269e767SBrooks DavisThe per-process and system-wide resource limits of locked memory apply 170*8269e767SBrooks Davisto the amount of virtual memory locked, not the amount of locked physical 171*8269e767SBrooks Davispages. 172*8269e767SBrooks DavisHence two distinct locked mappings of the same physical page counts as 173*8269e767SBrooks Davis2 pages aginst the system limit, and also against the per-process limit 174*8269e767SBrooks Davisif both mappings belong to the same physical map. 175*8269e767SBrooks Davis.Pp 176*8269e767SBrooks DavisThe per-process resource limit is not currently supported. 177