1*0a6a1f1dSLionel Sambuc.\" $NetBSD: mlock.2,v 1.22 2015/02/08 14:10:28 wiz Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 82fe8fb19SBen Gras.\" are met: 92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 102fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 122fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 132fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 152fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 162fe8fb19SBen Gras.\" without specific prior written permission. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 212fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 282fe8fb19SBen Gras.\" SUCH DAMAGE. 292fe8fb19SBen Gras.\" 302fe8fb19SBen Gras.\" @(#)mlock.2 8.2 (Berkeley) 12/11/93 312fe8fb19SBen Gras.\" 32*0a6a1f1dSLionel Sambuc.Dd February 8, 2015 332fe8fb19SBen Gras.Dt MLOCK 2 342fe8fb19SBen Gras.Os 352fe8fb19SBen Gras.Sh NAME 362fe8fb19SBen Gras.Nm mlock , 372fe8fb19SBen Gras.Nm munlock 382fe8fb19SBen Gras.Nd lock (unlock) physical pages in memory 392fe8fb19SBen Gras.Sh LIBRARY 402fe8fb19SBen Gras.Lb libc 412fe8fb19SBen Gras.Sh SYNOPSIS 422fe8fb19SBen Gras.In sys/mman.h 432fe8fb19SBen Gras.Ft int 442fe8fb19SBen Gras.Fn mlock "void *addr" "size_t len" 452fe8fb19SBen Gras.Ft int 462fe8fb19SBen Gras.Fn munlock "void *addr" "size_t len" 472fe8fb19SBen Gras.Sh DESCRIPTION 482fe8fb19SBen GrasThe 492fe8fb19SBen Gras.Nm mlock 502fe8fb19SBen Grassystem call 512fe8fb19SBen Graslocks into memory the physical pages associated with the virtual address 522fe8fb19SBen Grasrange starting at 532fe8fb19SBen Gras.Fa addr 542fe8fb19SBen Grasfor 552fe8fb19SBen Gras.Fa len 562fe8fb19SBen Grasbytes. 572fe8fb19SBen GrasThe 582fe8fb19SBen Gras.Nm munlock 592fe8fb19SBen Grascall unlocks pages previously locked by one or more 602fe8fb19SBen Gras.Nm mlock 612fe8fb19SBen Grascalls. 62f14fb602SLionel SambucThe entire range of memory must be allocated. 632fe8fb19SBen Gras.Pp 642fe8fb19SBen GrasAfter an 652fe8fb19SBen Gras.Nm mlock 662fe8fb19SBen Grascall, the indicated pages will cause neither a non-resident page 672fe8fb19SBen Grasnor address-translation fault until they are unlocked. 682fe8fb19SBen GrasThey may still cause protection-violation faults or TLB-miss faults on 692fe8fb19SBen Grasarchitectures with software-managed TLBs. 702fe8fb19SBen GrasThe physical pages remain in memory until all locked mappings for the pages 712fe8fb19SBen Grasare removed. 722fe8fb19SBen GrasMultiple processes may have the same physical pages locked via their own 732fe8fb19SBen Grasvirtual address mappings. 742fe8fb19SBen GrasA single process may likewise have pages multiply-locked via different virtual 752fe8fb19SBen Grasmappings of the same pages or via nested 762fe8fb19SBen Gras.Nm mlock 772fe8fb19SBen Grascalls on the same address range. 782fe8fb19SBen GrasUnlocking is performed explicitly by 792fe8fb19SBen Gras.Nm munlock 802fe8fb19SBen Grasor implicitly by a call to 812fe8fb19SBen Gras.Nm munmap 822fe8fb19SBen Graswhich deallocates the unmapped address range. 832fe8fb19SBen GrasLocked mappings are not inherited by the child process after a 842fe8fb19SBen Gras.Xr fork 2 . 852fe8fb19SBen Gras.Pp 862fe8fb19SBen GrasSince physical memory is a potentially scarce resource, processes are 872fe8fb19SBen Graslimited in how much they can lock down. 882fe8fb19SBen GrasA single process can 892fe8fb19SBen Gras.Nm mlock 902fe8fb19SBen Grasthe minimum of 912fe8fb19SBen Grasa system-wide ``wired pages'' limit and 922fe8fb19SBen Grasthe per-process 932fe8fb19SBen Gras.Li RLIMIT_MEMLOCK 942fe8fb19SBen Grasresource limit. 95f14fb602SLionel Sambuc.Pp 96f14fb602SLionel SambucPortable code should ensure that the 97f14fb602SLionel Sambuc.Fa addr 98f14fb602SLionel Sambucand 99f14fb602SLionel Sambuc.Fa len 100f14fb602SLionel Sambucparameters are aligned to a multiple of the page size, even though the 101f14fb602SLionel Sambuc.Nx 102f14fb602SLionel Sambucimplementation will round as necessary. 1032fe8fb19SBen Gras.Sh RETURN VALUES 1042fe8fb19SBen GrasA return value of 0 indicates that the call 1052fe8fb19SBen Grassucceeded and all pages in the range have either been locked or unlocked. 1062fe8fb19SBen GrasA return value of \-1 indicates an error occurred and the locked 1072fe8fb19SBen Grasstatus of all pages in the range remains unchanged. 1082fe8fb19SBen GrasIn this case, the global location 1092fe8fb19SBen Gras.Va errno 1102fe8fb19SBen Grasis set to indicate the error. 1112fe8fb19SBen Gras.Sh ERRORS 1122fe8fb19SBen Gras.Fn mlock 1132fe8fb19SBen Graswill fail if: 1142fe8fb19SBen Gras.Bl -tag -width Er 1152fe8fb19SBen Gras.It Bq Er EAGAIN 1162fe8fb19SBen GrasLocking the indicated range would exceed either the system or per-process 1172fe8fb19SBen Graslimit for locked memory. 118f14fb602SLionel Sambuc.It Bq Er EINVAL 119*0a6a1f1dSLionel SambucThe address or length given is not page aligned and the implementation does 120*0a6a1f1dSLionel Sambucnot round. 1212fe8fb19SBen Gras.It Bq Er ENOMEM 1222fe8fb19SBen GrasSome portion of the indicated address range is not allocated. 1232fe8fb19SBen GrasThere was an error faulting/mapping a page. 1242fe8fb19SBen Gras.It Bq Er EPERM 1252fe8fb19SBen Gras.Fn mlock 1262fe8fb19SBen Graswas called by non-root on an architecture where locked page accounting 1272fe8fb19SBen Grasis not implemented. 1282fe8fb19SBen Gras.Pp 1292fe8fb19SBen Gras.El 1302fe8fb19SBen Gras.Fn munlock 1312fe8fb19SBen Graswill fail if: 1322fe8fb19SBen Gras.Bl -tag -width Er 1332fe8fb19SBen Gras.It Bq Er EINVAL 134*0a6a1f1dSLionel SambucThe address or length given is not page aligned and the implementation does 135*0a6a1f1dSLionel Sambucnot round. 1362fe8fb19SBen Gras.It Bq Er ENOMEM 1372fe8fb19SBen GrasSome portion of the indicated address range is not allocated. 1382fe8fb19SBen GrasSome portion of the indicated address range is not locked. 1392fe8fb19SBen Gras.El 1402fe8fb19SBen Gras.Sh SEE ALSO 1412fe8fb19SBen Gras.Xr fork 2 , 1422fe8fb19SBen Gras.Xr mincore 2 , 1432fe8fb19SBen Gras.Xr mmap 2 , 1442fe8fb19SBen Gras.Xr munmap 2 , 1452fe8fb19SBen Gras.Xr setrlimit 2 , 1462fe8fb19SBen Gras.Xr getpagesize 3 1472fe8fb19SBen Gras.Sh STANDARDS 1482fe8fb19SBen GrasThe 1492fe8fb19SBen Gras.Fn mlock 1502fe8fb19SBen Grasand 1512fe8fb19SBen Gras.Fn munlock 1522fe8fb19SBen Grasfunctions conform to 1532fe8fb19SBen Gras.St -p1003.1b-93 . 1542fe8fb19SBen Gras.Sh HISTORY 1552fe8fb19SBen GrasThe 1562fe8fb19SBen Gras.Fn mlock 1572fe8fb19SBen Grasand 1582fe8fb19SBen Gras.Fn munlock 1592fe8fb19SBen Grasfunctions first appeared in 1602fe8fb19SBen Gras.Bx 4.4 . 1612fe8fb19SBen Gras.Sh BUGS 1622fe8fb19SBen GrasThe per-process resource limit is a limit on the amount of virtual 1632fe8fb19SBen Grasmemory locked, while the system-wide limit is for the number of locked 1642fe8fb19SBen Grasphysical pages. 1652fe8fb19SBen GrasHence a process with two distinct locked mappings of the same physical page 1662fe8fb19SBen Grascounts as 2 pages against the per-process limit and as only a single page 1672fe8fb19SBen Grasin the system limit. 168