xref: /freebsd/share/man/man9/vm_page_alloc.9 (revision 31cc65708c664c2a0257c26503d39ebc506f674e)
16484c361SAlfred Perlstein.\"
26484c361SAlfred Perlstein.\" Copyright (C) 2001 Chad David <davidc@acns.ab.ca>. All rights reserved.
36d3c78d9SMark Johnston.\" Copyright (c) 2021 The FreeBSD Foundation
46d3c78d9SMark Johnston.\"
56d3c78d9SMark Johnston.\" Portions of this documentation were written by Mark Johnston under
66d3c78d9SMark Johnston.\" sponsorship from the FreeBSD Foundation.
76484c361SAlfred Perlstein.\"
86484c361SAlfred Perlstein.\" Redistribution and use in source and binary forms, with or without
96484c361SAlfred Perlstein.\" modification, are permitted provided that the following conditions
106484c361SAlfred Perlstein.\" are met:
116484c361SAlfred Perlstein.\" 1. Redistributions of source code must retain the above copyright
126484c361SAlfred Perlstein.\"    notice(s), this list of conditions and the following disclaimer as
136484c361SAlfred Perlstein.\"    the first lines of this file unmodified other than the possible
146484c361SAlfred Perlstein.\"    addition of one or more copyright notices.
156484c361SAlfred Perlstein.\" 2. Redistributions in binary form must reproduce the above copyright
166484c361SAlfred Perlstein.\"    notice(s), this list of conditions and the following disclaimer in the
176484c361SAlfred Perlstein.\"    documentation and/or other materials provided with the distribution.
186484c361SAlfred Perlstein.\"
196484c361SAlfred Perlstein.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
206484c361SAlfred Perlstein.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
216484c361SAlfred Perlstein.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
226484c361SAlfred Perlstein.\" DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
236484c361SAlfred Perlstein.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
246484c361SAlfred Perlstein.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
256484c361SAlfred Perlstein.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
266484c361SAlfred Perlstein.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
276484c361SAlfred Perlstein.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
286484c361SAlfred Perlstein.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
296484c361SAlfred Perlstein.\" DAMAGE.
306484c361SAlfred Perlstein.\"
31*31cc6570SBojan Novković.Dd August 4, 2024
326484c361SAlfred Perlstein.Dt VM_PAGE_ALLOC 9
336484c361SAlfred Perlstein.Os
346484c361SAlfred Perlstein.Sh NAME
356484c361SAlfred Perlstein.Nm vm_page_alloc
366d3c78d9SMark Johnston.Nd "allocate a page of memory"
376484c361SAlfred Perlstein.Sh SYNOPSIS
386484c361SAlfred Perlstein.In sys/param.h
396484c361SAlfred Perlstein.In vm/vm.h
406484c361SAlfred Perlstein.In vm/vm_page.h
416484c361SAlfred Perlstein.Ft vm_page_t
42b64400a0SAlan Cox.Fn vm_page_alloc "vm_object_t object" "vm_pindex_t pindex" "int req"
436d3c78d9SMark Johnston.Ft vm_page_t
446d3c78d9SMark Johnston.Fo vm_page_alloc_after
456d3c78d9SMark Johnston.Fa "vm_object_t object"
466d3c78d9SMark Johnston.Fa "vm_pindex_t pindex"
476d3c78d9SMark Johnston.Fa "int req"
486d3c78d9SMark Johnston.Fa "vm_page_t mpred"
496d3c78d9SMark Johnston.Fc
506d3c78d9SMark Johnston.Ft vm_page_t
516d3c78d9SMark Johnston.Fo vm_page_alloc_contig
526d3c78d9SMark Johnston.Fa "vm_object_t object"
536d3c78d9SMark Johnston.Fa "vm_pindex_t pindex"
546d3c78d9SMark Johnston.Fa "int req"
556d3c78d9SMark Johnston.Fa "u_long npages"
566d3c78d9SMark Johnston.Fa "vm_paddr_t low"
576d3c78d9SMark Johnston.Fa "vm_paddr_t high"
586d3c78d9SMark Johnston.Fa "u_long alignment"
596d3c78d9SMark Johnston.Fa "vm_paddr_t boundary"
606d3c78d9SMark Johnston.Fa "vm_memattr_t memattr"
616d3c78d9SMark Johnston.Fc
626d3c78d9SMark Johnston.Ft vm_page_t
636d3c78d9SMark Johnston.Fo vm_page_alloc_contig_domain
646d3c78d9SMark Johnston.Fa "vm_object_t object"
656d3c78d9SMark Johnston.Fa "vm_pindex_t pindex"
666d3c78d9SMark Johnston.Fa "int req"
676d3c78d9SMark Johnston.Fa "u_long npages"
686d3c78d9SMark Johnston.Fa "vm_paddr_t low"
696d3c78d9SMark Johnston.Fa "vm_paddr_t high"
706d3c78d9SMark Johnston.Fa "u_long alignment"
716d3c78d9SMark Johnston.Fa "vm_paddr_t boundary"
726d3c78d9SMark Johnston.Fa "vm_memattr_t memattr"
736d3c78d9SMark Johnston.Fc
746d3c78d9SMark Johnston.Ft vm_page_t
756d3c78d9SMark Johnston.Fo vm_page_alloc_domain
766d3c78d9SMark Johnston.Fa "vm_object_t object"
776d3c78d9SMark Johnston.Fa "vm_pindex_t pindex"
786d3c78d9SMark Johnston.Fa "int domain"
796d3c78d9SMark Johnston.Fa "int req"
806d3c78d9SMark Johnston.Fc
816d3c78d9SMark Johnston.Ft vm_page_t
826d3c78d9SMark Johnston.Fo vm_page_alloc_domain_after
836d3c78d9SMark Johnston.Fa "vm_object_t object"
846d3c78d9SMark Johnston.Fa "vm_pindex_t pindex"
856d3c78d9SMark Johnston.Fa "int domain"
866d3c78d9SMark Johnston.Fa "int req"
876d3c78d9SMark Johnston.Fa "vm_page_t mpred"
886d3c78d9SMark Johnston.Fc
896d3c78d9SMark Johnston.Ft vm_page_t
906d3c78d9SMark Johnston.Fo vm_page_alloc_noobj
916d3c78d9SMark Johnston.Fa "int req"
926d3c78d9SMark Johnston.Fc
936d3c78d9SMark Johnston.Ft vm_page_t
946d3c78d9SMark Johnston.Fo vm_page_alloc_noobj_contig
956d3c78d9SMark Johnston.Fa "int req"
966d3c78d9SMark Johnston.Fa "u_long npages"
976d3c78d9SMark Johnston.Fa "vm_paddr_t low"
986d3c78d9SMark Johnston.Fa "vm_paddr_t high"
996d3c78d9SMark Johnston.Fa "u_long alignment"
1006d3c78d9SMark Johnston.Fa "vm_paddr_t boundary"
1016d3c78d9SMark Johnston.Fa "vm_memattr_t memattr"
1026d3c78d9SMark Johnston.Fc
1036d3c78d9SMark Johnston.Ft vm_page_t
1046d3c78d9SMark Johnston.Fo vm_page_alloc_noobj_contig_domain
1056d3c78d9SMark Johnston.Fa "int domain"
1066d3c78d9SMark Johnston.Fa "int req"
1076d3c78d9SMark Johnston.Fa "u_long npages"
1086d3c78d9SMark Johnston.Fa "vm_paddr_t low"
1096d3c78d9SMark Johnston.Fa "vm_paddr_t high"
1106d3c78d9SMark Johnston.Fa "u_long alignment"
1116d3c78d9SMark Johnston.Fa "vm_paddr_t boundary"
1126d3c78d9SMark Johnston.Fa "vm_memattr_t memattr"
1136d3c78d9SMark Johnston.Fc
1146d3c78d9SMark Johnston.Ft vm_page_t
1156d3c78d9SMark Johnston.Fo vm_page_alloc_noobj_domain
1166d3c78d9SMark Johnston.Fa "int domain"
1176d3c78d9SMark Johnston.Fa "int req"
1186d3c78d9SMark Johnston.Fc
1196484c361SAlfred Perlstein.Sh DESCRIPTION
1206484c361SAlfred PerlsteinThe
1216484c361SAlfred Perlstein.Fn vm_page_alloc
1226d3c78d9SMark Johnstonfamily of functions allocate one or more pages of physical memory.
1236d3c78d9SMark JohnstonMost kernel code should not call these functions directly but should instead
1246d3c78d9SMark Johnstonuse a kernel memory allocator such as
1256d3c78d9SMark Johnston.Xr malloc 9
1266d3c78d9SMark Johnstonor
1276d3c78d9SMark Johnston.Xr uma 9 ,
1286d3c78d9SMark Johnstonor should use a higher-level interface to the page cache, such as
1296d3c78d9SMark Johnston.Xr vm_page_grab 9 .
1306d3c78d9SMark Johnston.Pp
1316d3c78d9SMark JohnstonAll of the functions take a
1326d3c78d9SMark Johnston.Fa req
1336d3c78d9SMark Johnstonparameter which encodes the allocation priority and optional modifier flags,
1346d3c78d9SMark Johnstondescribed below.
1356d3c78d9SMark JohnstonThe functions whose names do not include
1366d3c78d9SMark Johnston.Dq noobj
1376d3c78d9SMark Johnstonadditionally insert the pages starting at index
1386484c361SAlfred Perlstein.Fa pindex
1396d3c78d9SMark Johnstonin the
1406d3c78d9SMark JohnstonVM object
1416484c361SAlfred Perlstein.Fa object .
1426d3c78d9SMark JohnstonThe object must be write-locked and not have a page already resident at the
1436d3c78d9SMark Johnstonspecified index.
1446d3c78d9SMark JohnstonThe functions whose names include
1456d3c78d9SMark Johnston.Dq domain
1466d3c78d9SMark Johnstonsupport NUMA-aware allocation by returning pages from the
1476d3c78d9SMark Johnston.Xr numa 4
1486d3c78d9SMark Johnstondomain specified by
1496d3c78d9SMark Johnston.Fa domain .
1506484c361SAlfred Perlstein.Pp
151c46221d7SKonstantin BelousovThe
1526d3c78d9SMark Johnston.Fn vm_page_alloc_after
1536d3c78d9SMark Johnstonand
1546d3c78d9SMark Johnston.Fn vm_page_alloc_domain_after
1556d3c78d9SMark Johnstonfunctions behave identically to
1566d3c78d9SMark Johnston.Fn vm_page_alloc
1576d3c78d9SMark Johnstonand
1586d3c78d9SMark Johnston.Fn vm_page_alloc_domain ,
1596d3c78d9SMark Johnstonrespectively, except that they take an additional parameter
1606d3c78d9SMark Johnston.Fa mpred
1616d3c78d9SMark Johnstonwhich must be the page resident in
162c46221d7SKonstantin Belousov.Fa object
1636d3c78d9SMark Johnstonwith largest index smaller than
1646d3c78d9SMark Johnston.Fa pindex ,
1656d3c78d9SMark Johnstonor
1666d3c78d9SMark Johnston.Dv NULL
1676d3c78d9SMark Johnstonif no such page exists.
1686d3c78d9SMark JohnstonThese functions exist to optimize the common case of loops that allocate
1696d3c78d9SMark Johnstonmultiple pages at successive indices within an object.
170b64400a0SAlan Cox.Pp
1716d3c78d9SMark JohnstonThe
1726d3c78d9SMark Johnston.Fn vm_page_alloc_contig
1736d3c78d9SMark Johnstonand
1746d3c78d9SMark Johnston.Fn vm_page_alloc_noobj_contig
1756d3c78d9SMark Johnstonfunctions and their NUMA-aware variants allocate a physically contiguous run of
1766d3c78d9SMark Johnston.Fa npages
1776d3c78d9SMark Johnstonpages which satisfies the specified constraints.
1786d3c78d9SMark JohnstonThe
1796d3c78d9SMark Johnston.Fa low
1806d3c78d9SMark Johnstonand
1816d3c78d9SMark Johnston.Fa high
1826d3c78d9SMark Johnstonparameters specify a physical address range from which the run is to
1836d3c78d9SMark Johnstonbe allocated.
1846d3c78d9SMark JohnstonThe
1856d3c78d9SMark Johnston.Fa alignment
1866d3c78d9SMark Johnstonparameter specifies the requested alignment of the first page in the run
1876d3c78d9SMark Johnstonand must be a power of two.
1886d3c78d9SMark JohnstonIf the
1896d3c78d9SMark Johnston.Fa boundary
1906d3c78d9SMark Johnstonparameter is non-zero, the pages constituting the run will not cross a
1916d3c78d9SMark Johnstonphysical address that is a multiple of the parameter value, which must be a
1926d3c78d9SMark Johnstonpower of two.
1936d3c78d9SMark JohnstonIf
1946d3c78d9SMark Johnston.Fa memattr
1956d3c78d9SMark Johnstonis not equal to
1966d3c78d9SMark Johnston.Dv VM_MEMATTR_DEFAULT ,
1976d3c78d9SMark Johnstonthen mappings of the returned pages created by, e.g.,
1986d3c78d9SMark Johnston.Xr pmap_enter 9
1996d3c78d9SMark Johnstonor
2006d3c78d9SMark Johnston.Xr pmap_qenter 9 ,
2016d3c78d9SMark Johnstonwill carry the machine-dependent encoding of the memory attribute.
2026d3c78d9SMark JohnstonAdditionally, the direct mapping of the page, if any, will be updated to
2036d3c78d9SMark Johnstonreflect the requested memory attribute.
2046d3c78d9SMark Johnston.Sh REQUEST FLAGS
2056d3c78d9SMark JohnstonAll page allocator functions accept a
2066d3c78d9SMark Johnston.Fa req
2076d3c78d9SMark Johnstonparameter that governs certain aspects of the function's behavior.
2086d3c78d9SMark Johnston.Pp
2096d3c78d9SMark JohnstonThe
2106d3c78d9SMark Johnston.Dv VM_ALLOC_WAITOK ,
2116d3c78d9SMark Johnston.Dv VM_ALLOC_WAITFAIL ,
2126d3c78d9SMark Johnstonand
2136d3c78d9SMark Johnston.Dv VM_ALLOC_NOWAIT
2146d3c78d9SMark Johnstonflags specify the behavior of the allocator if free pages could not be
2156d3c78d9SMark Johnstonimmediately allocated.
2166d3c78d9SMark JohnstonThe
2176d3c78d9SMark Johnston.Dv VM_ALLOC_WAITOK
2186d3c78d9SMark Johnstonflag can only be used with the
2196d3c78d9SMark Johnston.Dq noobj
2206d3c78d9SMark Johnstonvariants.
2216d3c78d9SMark JohnstonIf
2226d3c78d9SMark Johnston.Dv VM_ALLOC_NOWAIT
2236d3c78d9SMark Johnstonis specified, then the allocator gives up and returns
2246d3c78d9SMark Johnston.Dv NULL .
2256d3c78d9SMark Johnston.Dv VM_ALLOC_NOWAIT
2266d3c78d9SMark Johnstonis specified implicitly if none of the flags are present in the request.
2276d3c78d9SMark JohnstonIf either
2286d3c78d9SMark Johnston.Dv VM_ALLOC_WAITOK
2296d3c78d9SMark Johnstonor
2306d3c78d9SMark Johnston.Dv VM_ALLOC_WAITFAIL
2316d3c78d9SMark Johnstonis specified, the allocator will put the calling thread to sleep until
2326d3c78d9SMark Johnstonsufficient free pages become available.
2336d3c78d9SMark JohnstonAt this point, if
2346d3c78d9SMark Johnston.Dv VM_ALLOC_WAITFAIL
2356d3c78d9SMark Johnstonis specified the allocator will return
2366d3c78d9SMark Johnston.Dv NULL ,
2376d3c78d9SMark Johnstonand if
2386d3c78d9SMark Johnston.Dv VM_ALLOC_WAITOK
2396d3c78d9SMark Johnstonis specified the allocator will retry the allocation.
2406d3c78d9SMark JohnstonAfter a failed
2416d3c78d9SMark Johnston.Dv VM_ALLOC_WAITFAIL
2426d3c78d9SMark Johnstonallocation returns, the VM object, if any, will have been unlocked while the
2436d3c78d9SMark Johnstonthread was sleeping.
2446d3c78d9SMark JohnstonIn this case the VM object write lock will be re-acquired before the function
2456d3c78d9SMark Johnstoncall returns.
2466d3c78d9SMark Johnston.Pp
2476d3c78d9SMark Johnston.Fa req
2486d3c78d9SMark Johnstonalso encodes the allocation request priority.
2496d3c78d9SMark JohnstonBy default the page(s) are allocated with no special treatment.
2506d3c78d9SMark JohnstonIf the number of available free pages is below a certain watermark, the
2516d3c78d9SMark Johnstonallocation will fail or the allocating thread will sleep, depending on
2526d3c78d9SMark Johnstonthe specified wait flag.
2536d3c78d9SMark JohnstonThe watermark is computed at boot time and corresponds to a small (less than
2546d3c78d9SMark Johnstonone percent) fraction of the system's total physical memory.
2556d3c78d9SMark JohnstonTo allocate memory more aggressively, one of following flags may be specified.
2566484c361SAlfred Perlstein.Bl -tag -width ".Dv VM_ALLOC_INTERRUPT"
2576484c361SAlfred Perlstein.It Dv VM_ALLOC_SYSTEM
2586d3c78d9SMark JohnstonThe page can be allocated if the free page count is above the interrupt
2596d3c78d9SMark Johnstonreserved water mark.
2602e693d19SChad DavidThis flag should be used only when the system really needs the page.
2616484c361SAlfred Perlstein.It Dv VM_ALLOC_INTERRUPT
2626d3c78d9SMark JohnstonThe allocation will fail only if zero free pages are available.
2636d3c78d9SMark JohnstonThis flag should be used only if the consequences of an allocation failure
2646d3c78d9SMark Johnstonare worse than leaving the system without free memory.
2656d3c78d9SMark JohnstonFor example, this flag is used when allocating kernel page table pages, where
2666d3c78d9SMark Johnstonallocation failures trigger a kernel panic.
267b64400a0SAlan Cox.El
268b64400a0SAlan Cox.Pp
2696d3c78d9SMark JohnstonThe following optional flags can further modify allocator behavior:
2704fab83ffSAlan Cox.Bl -tag -width ".Dv VM_ALLOC_NOBUSY"
2716d3c78d9SMark Johnston.It Dv VM_ALLOC_SBUSY
2726d3c78d9SMark JohnstonThe returned page will be shared-busy.
2736d3c78d9SMark JohnstonThis flag may only be specified when allocating pages in a VM object.
274e013216eSKip Macy.It Dv VM_ALLOC_NOBUSY
2756d3c78d9SMark JohnstonThe returned page will not be busy.
2766d3c78d9SMark JohnstonThis flag is implicit when allocating pages without a VM object.
2776d3c78d9SMark JohnstonWhen allocating pages in a VM object, and neither
2786d3c78d9SMark Johnston.Dv VM_ALLOC_SBUSY
2796d3c78d9SMark Johnstonnor
2806d3c78d9SMark Johnston.Dv VM_ALLOC_NOBUSY
2816d3c78d9SMark Johnstonare specified, the returned pages will be exclusively busied.
282e013216eSKip Macy.It Dv VM_ALLOC_NODUMP
283e013216eSKip MacyThe returned page will not be included in any kernel core dumps
284e013216eSKip Macyregardless of whether or not it is mapped in to KVA.
285e013216eSKip Macy.It Dv VM_ALLOC_WIRED
286e013216eSKip MacyThe returned page will be wired.
287e013216eSKip Macy.It Dv VM_ALLOC_ZERO
2886d3c78d9SMark JohnstonIf this flag is specified, the
2896d3c78d9SMark Johnston.Dq noobj
2906d3c78d9SMark Johnstonvariants will return zeroed pages.
2916d3c78d9SMark JohnstonThe other allocator interfaces ignore this flag.
292811d0544SMark Johnston.It Dv VM_ALLOC_NORECLAIM
293811d0544SMark JohnstonIf this flag is specified and the request can not be immediately satisfied,
294811d0544SMark Johnstonthe allocator will not attempt to break superpage reservations to satisfy the
295811d0544SMark Johnstonallocation.
296811d0544SMark JohnstonThis may be useful when the overhead of scanning the reservation queue
297811d0544SMark Johnstonoutweighs the cost of a failed allocation.
298811d0544SMark JohnstonThis flag may be used only with the
299811d0544SMark Johnston.Dq contig
300811d0544SMark Johnstonvariants, and must not be specified in combination with
301811d0544SMark Johnston.Dv VM_ALLOC_WAITOK .
3026d3c78d9SMark Johnston.It Dv VM_ALLOC_COUNT(n)
3036d3c78d9SMark JohnstonHint that at least
3046d3c78d9SMark Johnston.Fa n
3056d3c78d9SMark Johnstonpages will be allocated by the caller in the near future.
3066d3c78d9SMark Johnston.Fa n
3076d3c78d9SMark Johnstonmust be no larger than 65535.
3086d3c78d9SMark JohnstonIf the system is short of free pages, this hint may cause the kernel
3096d3c78d9SMark Johnstonto reclaim memory more aggressively than it would otherwise.
310*31cc6570SBojan Novković.It Dv VM_ALLOC_NOFREE
311*31cc6570SBojan NovkovićThe caller asserts that the returned page will never be released.
312*31cc6570SBojan NovkovićIf this flag is specified, the allocator will try to fetch a page from a
313*31cc6570SBojan Novkovićspecial per-domain arena in order to curb long-term physical memory fragmentation.
314bf7f20c2SRuslan Ermilov.El
3156484c361SAlfred Perlstein.Sh RETURN VALUES
3166d3c78d9SMark JohnstonIf the allocation was successful, a pointer to the
3176d3c78d9SMark Johnston.Vt struct vm_page
3186d3c78d9SMark Johnstoncorresponding to the allocated page is returned.
3196d3c78d9SMark JohnstonIf the allocation request specified multiple pages, the returned
3206d3c78d9SMark Johnstonpointer points to an array of
3216d3c78d9SMark Johnston.Vt struct vm_page
3226d3c78d9SMark Johnstonconstituting the run.
3236d3c78d9SMark JohnstonUpon failure,
3246484c361SAlfred Perlstein.Dv NULL
3256484c361SAlfred Perlsteinis returned.
3266d3c78d9SMark JohnstonRegardless of whether the allocation succeeds or fails, the VM
3276d3c78d9SMark Johnstonobject
3286d3c78d9SMark Johnston.Fa object
3296d3c78d9SMark Johnstonwill be write-locked upon return.
3306d3c78d9SMark Johnston.Sh SEE ALSO
3316d3c78d9SMark Johnston.Xr numa 4 ,
3326d3c78d9SMark Johnston.Xr malloc 9 ,
3336d3c78d9SMark Johnston.Xr uma 9 ,
3346d3c78d9SMark Johnston.Xr vm_page_grab 9 ,
3356d3c78d9SMark Johnston.Xr vm_page_sbusy 9
3366484c361SAlfred Perlstein.Sh AUTHORS
337571dba6eSHiten PandyaThis manual page was written by
3388a7314fcSBaptiste Daroussin.An Chad David Aq Mt davidc@acns.ab.ca .
339