2 .\" Copyright (C) 2001 Chad David <davidc@acns.ab.ca>. All rights reserved.
3 .\" Copyright (c) 2021 The FreeBSD Foundation
5 .\" Portions of this documentation were written by Mark Johnston under
6 .\" sponsorship from the FreeBSD Foundation.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice(s), this list of conditions and the following disclaimer as
13 .\" the first lines of this file unmodified other than the possible
14 .\" addition of one or more copyright notices.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\" notice(s), this list of conditions and the following disclaimer in the
17 .\" documentation and/or other materials provided with the distribution.
19 .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
20 .\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 .\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
23 .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 .\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 .\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38 .Nd "allocate a page of memory"
44 .Fn vm_page_alloc "vm_object_t object" "vm_pindex_t pindex" "int req"
46 .Fo vm_page_alloc_after
47 .Fa "vm_object_t object"
48 .Fa "vm_pindex_t pindex"
53 .Fo vm_page_alloc_contig
54 .Fa "vm_object_t object"
55 .Fa "vm_pindex_t pindex"
60 .Fa "u_long alignment"
61 .Fa "vm_paddr_t boundary"
62 .Fa "vm_memattr_t memattr"
65 .Fo vm_page_alloc_contig_domain
66 .Fa "vm_object_t object"
67 .Fa "vm_pindex_t pindex"
72 .Fa "u_long alignment"
73 .Fa "vm_paddr_t boundary"
74 .Fa "vm_memattr_t memattr"
77 .Fo vm_page_alloc_domain
78 .Fa "vm_object_t object"
79 .Fa "vm_pindex_t pindex"
84 .Fo vm_page_alloc_domain_after
85 .Fa "vm_object_t object"
86 .Fa "vm_pindex_t pindex"
92 .Fo vm_page_alloc_freelist
97 .Fo vm_page_alloc_freelist_domain
103 .Fo vm_page_alloc_noobj
107 .Fo vm_page_alloc_noobj_contig
111 .Fa "vm_paddr_t high"
112 .Fa "u_long alignment"
113 .Fa "vm_paddr_t boundary"
114 .Fa "vm_memattr_t memattr"
117 .Fo vm_page_alloc_noobj_contig_domain
122 .Fa "vm_paddr_t high"
123 .Fa "u_long alignment"
124 .Fa "vm_paddr_t boundary"
125 .Fa "vm_memattr_t memattr"
128 .Fo vm_page_alloc_noobj_domain
135 family of functions allocate one or more pages of physical memory.
136 Most kernel code should not call these functions directly but should instead
137 use a kernel memory allocator such as
141 or should use a higher-level interface to the page cache, such as
144 All of the functions take a
146 parameter which encodes the allocation priority and optional modifier flags,
148 The functions whose names do not include
150 additionally insert the pages starting at index
155 The object must be write-locked and not have a page already resident at the
157 The functions whose names include
159 support NUMA-aware allocation by returning pages from the
165 .Fn vm_page_alloc_after
167 .Fn vm_page_alloc_domain_after
168 functions behave identically to
171 .Fn vm_page_alloc_domain ,
172 respectively, except that they take an additional parameter
174 which must be the page resident in
176 with largest index smaller than
180 if no such page exists.
181 These functions exist to optimize the common case of loops that allocate
182 multiple pages at successive indices within an object.
185 .Fn vm_page_alloc_contig
187 .Fn vm_page_alloc_noobj_contig
188 functions and their NUMA-aware variants allocate a physically contiguous run of
190 pages which satisfies the specified constraints.
195 parameters specify a physical address range from which the run is to
199 parameter specifies the requested alignment of the first page in the run
200 and must be a power of two.
203 parameter is non-zero, the pages constituting the run will not cross a
204 physical address that is a multiple of the parameter value, which must be a
209 .Dv VM_MEMATTR_DEFAULT ,
210 then mappings of the returned pages created by, e.g.,
214 will carry the machine-dependent encoding of the memory attribute.
215 Additionally, the direct mapping of the page, if any, will be updated to
216 reflect the requested memory attribute.
219 .Fn vm_page_alloc_freelist
221 .Fn vm_page_alloc_freelist_domain
222 functions behave identically to
223 .Fn vm_page_alloc_noobj
225 .Fn vm_page_alloc_noobj_domain ,
226 respectively, except that a successful allocation will return a page from the
227 specified physical memory freelist.
228 These functions are not intended for use outside of the virtual memory
229 subsystem and exist only to support the requirements of certain platforms.
231 All page allocator functions accept a
233 parameter that governs certain aspects of the function's behavior.
236 .Dv VM_ALLOC_WAITOK ,
237 .Dv VM_ALLOC_WAITFAIL ,
240 flags specify the behavior of the allocator if free pages could not be
241 immediately allocated.
244 flag can only be used with the
249 is specified, then the allocator gives up and returns
252 is specified implicitly if none of the flags are present in the request.
256 .Dv VM_ALLOC_WAITFAIL
257 is specified, the allocator will put the calling thread to sleep until
258 sufficient free pages become available.
260 .Dv VM_ALLOC_WAITFAIL
261 is specified the allocator will return
265 is specified the allocator will retry the allocation.
267 .Dv VM_ALLOC_WAITFAIL
268 allocation returns, the VM object, if any, will have been unlocked while the
270 In this case the VM object write lock will be re-acquired before the function
274 also encodes the allocation request priority.
275 By default the page(s) are allocated with no special treatment.
276 If the number of available free pages is below a certain watermark, the
277 allocation will fail or the allocating thread will sleep, depending on
278 the specified wait flag.
279 The watermark is computed at boot time and corresponds to a small (less than
280 one percent) fraction of the system's total physical memory.
281 To allocate memory more aggressively, one of following flags may be specified.
282 .Bl -tag -width ".Dv VM_ALLOC_INTERRUPT"
283 .It Dv VM_ALLOC_SYSTEM
284 The page can be allocated if the free page count is above the interrupt
286 This flag should be used only when the system really needs the page.
287 .It Dv VM_ALLOC_INTERRUPT
288 The allocation will fail only if zero free pages are available.
289 This flag should be used only if the consequences of an allocation failure
290 are worse than leaving the system without free memory.
291 For example, this flag is used when allocating kernel page table pages, where
292 allocation failures trigger a kernel panic.
295 The following optional flags can further modify allocator behavior:
296 .Bl -tag -width ".Dv VM_ALLOC_NOBUSY"
297 .It Dv VM_ALLOC_SBUSY
298 The returned page will be shared-busy.
299 This flag may only be specified when allocating pages in a VM object.
300 .It Dv VM_ALLOC_NOBUSY
301 The returned page will not be busy.
302 This flag is implicit when allocating pages without a VM object.
303 When allocating pages in a VM object, and neither
307 are specified, the returned pages will be exclusively busied.
308 .It Dv VM_ALLOC_NODUMP
309 The returned page will not be included in any kernel core dumps
310 regardless of whether or not it is mapped in to KVA.
311 .It Dv VM_ALLOC_WIRED
312 The returned page will be wired.
314 If this flag is specified, the
316 variants will return zeroed pages.
317 The other allocator interfaces ignore this flag.
318 .It Dv VM_ALLOC_NORECLAIM
319 If this flag is specified and the request can not be immediately satisfied,
320 the allocator will not attempt to break superpage reservations to satisfy the
322 This may be useful when the overhead of scanning the reservation queue
323 outweighs the cost of a failed allocation.
324 This flag may be used only with the
326 variants, and must not be specified in combination with
327 .Dv VM_ALLOC_WAITOK .
328 .It Dv VM_ALLOC_COUNT(n)
331 pages will be allocated by the caller in the near future.
333 must be no larger than 65535.
334 If the system is short of free pages, this hint may cause the kernel
335 to reclaim memory more aggressively than it would otherwise.
338 If the allocation was successful, a pointer to the
340 corresponding to the allocated page is returned.
341 If the allocation request specified multiple pages, the returned
342 pointer points to an array of
344 constituting the run.
348 Regardless of whether the allocation succeeds or fails, the VM
351 will be write-locked upon return.
359 This manual page was written by
360 .An Chad David Aq Mt davidc@acns.ab.ca .