1*07f64461Sriastradh.\" $NetBSD: memoryallocators.9,v 1.8 2017/10/29 03:48:17 riastradh Exp $ 2190f747fSelad.\" 3190f747fSelad.\" Copyright (c) 2006 Elad Efrat <elad@NetBSD.org> 4190f747fSelad.\" All rights reserved. 5190f747fSelad.\" 6190f747fSelad.\" Redistribution and use in source and binary forms, with or without 7190f747fSelad.\" modification, are permitted provided that the following conditions 8190f747fSelad.\" are met: 9190f747fSelad.\" 1. Redistributions of source code must retain the above copyright 10190f747fSelad.\" notice, this list of conditions and the following disclaimer. 11190f747fSelad.\" 2. Redistributions in binary form must reproduce the above copyright 12190f747fSelad.\" notice, this list of conditions and the following disclaimer in the 13190f747fSelad.\" documentation and/or other materials provided with the distribution. 14d2e4f716Selad.\" 3. The name of the author may not be used to endorse or promote products 15190f747fSelad.\" derived from this software without specific prior written permission. 16190f747fSelad.\" 17190f747fSelad.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18190f747fSelad.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19190f747fSelad.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20190f747fSelad.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21190f747fSelad.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22190f747fSelad.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23190f747fSelad.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24190f747fSelad.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25190f747fSelad.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26190f747fSelad.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27190f747fSelad.\" 28b6b63286Sriastradh.Dd October 28, 2017 29190f747fSelad.Dt MEMORYALLOCATORS 9 30190f747fSelad.Os 31190f747fSelad.Sh NAME 32190f747fSelad.Nm memoryallocators 33190f747fSelad.Nd introduction to kernel memory allocators 34190f747fSelad.Sh DESCRIPTION 35190f747fSeladThe 36190f747fSelad.Nx 37190f747fSeladkernel provides several memory allocators, each with different characteristics 38190f747fSeladand purpose. 39190f747fSeladThis document summarizes the main differences between them. 40190f747fSelad.Pp 41b6b63286SriastradhYou should use the 42c783b6b3Srmind.Xr kmem 9 43b6b63286Sriastradhallocator for all allocations unless you have special needs that it 44b6b63286Sriastradhdoes not provide, such as: 45b6b63286Sriastradh.Bl -bullet -compact 46b6b63286Sriastradh.It 47b6b63286Sriastradhuse from interrupt handlers 48b6b63286Sriastradh.It 49b6b63286Sriastradha minimum reserved number of allocations 50b6b63286Sriastradh.It 51b6b63286Sriastradha maximum usable number of allocations 52b6b63286Sriastradh.It 53b6b63286Sriastradhcostly object initialization that can be reused 54b6b63286Sriastradh.It 55*07f64461Sriastradhallocating resources other than pageable RAM-backed kernel virtual 56b6b63286Sriastradhaddress space 57b6b63286Sriastradh.El 58b6b63286Sriastradh.Ss The Kmem Allocator 59b6b63286SriastradhThe 60b6b63286Sriastradh.Xr kmem 9 61b6b63286Sriastradhallocator is main general purpose allocator in the kernel. 62b6b63286SriastradhIt was modelled after an interface of the same name implemented 63b6b63286Sriastradhin Solaris. 64b6b63286Sriastradh.Pp 65b6b63286Sriastradh.Xr kmem 9 66b6b63286Sriastradhis fast and requires no setup. 67b6b63286SriastradhIt cannot be used from interrupt context. 68b6b63286Sriastradh.Pp 69b6b63286SriastradhInternally, 70b6b63286Sriastradh.Xr kmem 9 71b6b63286Sriastradhis implemented using a collection of pool caches for common small 72b6b63286Sriastradhallocation sizes, so there is no performance benefit to using a pool 73b6b63286Sriastradhcache if you have no other needs. 74190f747fSelad.Ss The Pool Allocator 75190f747fSeladThe 76190f747fSelad.Xr pool 9 77b6b63286Sriastradhallocator is a fixed-size memory allocator which requires setup to 78b6b63286Sriastradhinitialize a shared pool. 79190f747fSelad.Pp 80b6b63286SriastradhA pool can be configured with a low-water mark to reserve a minimum 81b6b63286Sriastradhnumber of objects available, a high-water mark to bound the maximum number of 82b6b63286Sriastradhobjects in reserve, and a hard limit to bound on the maximum number of 83b6b63286Sriastradhobjects in use. 84b6b63286Sriastradh.Pp 85b6b63286Sriastradh.Xr pool_get 9 86b6b63286Sriastradhcan be used to allocate memory in interrupt context for objects that 87b6b63286Sriastradhhave been reserved in advance, with the possibility of failure if there 88b6b63286Sriastradhare none. 89b6b63286Sriastradh.Pp 90b6b63286SriastradhBy default, 91b6b63286Sriastradh.Xr pool 9 92b6b63286Sriastradhallocates pageable RAM-backed kernel virtual address space from the 93b6b63286Sriastradhsame backing store as 94b6b63286Sriastradh.Xr kmem 9 , 95b6b63286Sriastradhbut it can be configured to allocate any kind of resource with a custom 96b6b63286Sriastradhallocator. 97b6b63286Sriastradh.\".Pp 98190f747fSelad.\" On some architectures (foo, bar) the 99190f747fSelad.\" .Xr pool 9 100190f747fSelad.\" allocator will use direct-mapped segments rather than normal page 101190f747fSelad.\" mappings, which can reduce TLB contentions. 102190f747fSelad.Ss The Pool Cache Allocator 103b6b63286SriastradhThe pool cache allocator is a per-CPU cache on top of 104190f747fSelad.Xr pool 9 105b6b63286Sriastradhfor fixed-size memory allocations that may occur in interrupt context 106b6b63286Sriastradhrequiring setup beforehand. 107190f747fSelad.Pp 108b6b63286SriastradhThe per-CPU cache makes allocation much cheaper \(em no interprocessor 109b6b63286Sriastradhsynchronization in the fast case \(em at the cost of potentially 110b6b63286Sriastradhcaching some extra resources on one CPU that cannot be used by another. 111190f747fSelad.Pp 112b6b63286SriastradhIn addition to all the features of a pool like a custom backing 113b6b63286Sriastradhallocator, a pool cache also supports a constructor and destructor 114b6b63286Sriastradhroutine for when objects are drawn from the shared pool in case the 115b6b63286Sriastradhper-CPU cache is empty, or returned to it when the cache is full. 116b6b63286SriastradhThis can reduce the cost of reusable initialization and finalization, 117b6b63286Sriastradhor associate objects with CPU-local resources. 118190f747fSelad.Ss The UVM Kernel Memory Allocator 119b6b63286SriastradhThe 120b6b63286Sriastradh.Xr uvm_km 9 121b6b63286SriastradhAPI is a low-level memory allocator for page-aligned kernel virtual 122b6b63286Sriastradhaddress space in multiples of 123190f747fSelad.Dv PAGE_SIZE , 124b6b63286Sriastradhwith wired RAM backing, pageable RAM backing, or backing to be supplied 125b6b63286Sriastradhby the caller with 126b6b63286Sriastradh.Xr pmap 9 . 127b6b63286Sriastradh.Ss The VMEM Allocator API 128b6b63286SriastradhThe 129b6b63286Sriastradh.Xr vmem 9 130b6b63286SriastradhAPI is a general address space allocator. 131b6b63286SriastradhIt is used internally by 132b6b63286Sriastradh.Xr kmem 9 , 133b6b63286Sriastradh.Xr pool 9 , 134b6b63286Sriastradh.Xr uvm 9 , 135b6b63286Sriastradhand other kernel subsystems and device drivers to allocate regions of 136b6b63286Sriastradhvarious kinds of address spaces. 137b6b63286SriastradhInternally, it allocates large chunks of the address space and uses a 138b6b63286Sriastradh.Xr pool_cache 9 139b6b63286Sriastradhto draw small allocations out of them. 140b6b63286Sriastradh.Ss The Extent Manager 141b6b63286SriastradhThe 142b6b63286Sriastradh.Xr extent 9 143b6b63286SriastradhAPI manages and allocates constrained regions of an address space. 144b6b63286SriastradhThe extent manager is optimized for simplicity, not speed, and is 145b6b63286Sriastradhavailable early at boot. 146b6b63286Sriastradh.Nx 147b6b63286Sriastradhuses 148b6b63286Sriastradh.Xr extent 9 149b6b63286Sriastradhto reserve regions of I/O port and memory spaces to prevent drivers 150b6b63286Sriastradhfrom using the same device registers or bus memory. 151190f747fSelad.Sh SEE ALSO 152b6b63286Sriastradh.Xr bus_space 9 , 153b6b63286Sriastradh.Xr extent 9 , 154751fa51eSwiz.Xr intro 9 , 155c783b6b3Srmind.Xr kmem 9 , 156190f747fSelad.Xr pool 9 , 157190f747fSelad.Xr pool_cache 9 , 158190f747fSelad.Xr uvm 9 , 159b6b63286Sriastradh.Xr uvm_km 9 , 160190f747fSelad.Xr vmem 9 161190f747fSelad.Sh AUTHORS 162a5684d07Swiz.An Elad Efrat Aq Mt elad@NetBSD.org 163a5684d07Swiz.An YAMAMOTO Takashi Aq Mt yamt@NetBSD.org 164b6b63286Sriastradh.An Taylor R Campbell Aq Mt riastradh@NetBSD.org 165