1 VMEM(9) Device Driver Interfaces VMEM(9) 2 3 NAME 4 vmem - virtual memory allocator 5 6 DESCRIPTION 7 Overview 8 An address space is divided into a number of logically distinct pieces, 9 or arenas: text, data, heap, stack, and so on. Within these arenas we 10 often subdivide further; for example, we use heap addresses not only for 11 the kernel heap (kmem_alloc() space), but also for DVMA, bp_mapin(), 12 /dev/kmem, and even some device mappings. 13 14 The kernel address space, therefore, is most accurately described as a 15 tree of arenas in which each node of the tree imports some subset of its 16 parent. The virtual memory allocator manages these arenas and supports 17 their natural hierarchical structure. 18 19 Arenas 20 An arena is nothing more than a set of integers. These integers most 21 commonly represent virtual addresses, but in fact they can represent 22 anything at all. For example, we could use an arena containing the 23 integers minpid through maxpid to allocate process IDs. For uses of this 24 nature, prefer id_space(9F) instead. 25 26 vmem_create() and vmem_destroy() create and destroy vmem arenas. In 27 order to differentiate between arenas used for adresses and arenas used 28 for identifiers, the VMC_IDENTIFIER flag is passed to vmem_create(). 29 This prevents identifier exhaustion from being diagnosed as general 30 memory failure. 31 32 Spans 33 We represent the integers in an arena as a collection of spans, or 34 contiguous ranges of integers. For example, the kernel heap consists of 35 just one span: [kernelheap, ekernelheap). Spans can be added to an arena 36 in two ways: explicitly, by vmem_add(), or implicitly, by importing, as 37 described in Imported Memory below. 38 39 Segments 40 Spans are subdivided into segments, each of which is either allocated or 41 free. A segment, like a span, is a contiguous range of integers. Each 42 allocated segment [addr, addr + size) represents exactly one 43 vmem_alloc(size) that returned addr. Free segments represent the space 44 between allocated segments. If two free segments are adjacent, we 45 coalesce them into one larger segment; that is, if segments [a, b) and 46 [b, c) are both free, we merge them into a single segment [a, c). The 47 segments within a span are linked together in increasing-address order so 48 we can easily determine whether coalescing is possible. 49 50 Segments never cross span boundaries. When all segments within an 51 imported span become free, we return the span to its source. 52 53 Imported Memory 54 As mentioned in the overview, some arenas are logical subsets of other 55 arenas. For example, kmem_va_arena (a virtual address cache that 56 satisfies most kmem_slab_create() requests) is just a subset of 57 heap_arena (the kernel heap) that provides caching for the most common 58 slab sizes. When kmem_va_arena runs out of virtual memory, it imports 59 more from the heap; we say that heap_arena is the vmem source for 60 kmem_va_arena. vmem_create() allows you to specify any existing vmem 61 arena as the source for your new arena. Topologically, since every arena 62 is a child of at most one source, the set of all arenas forms a 63 collection of trees. 64 65 Constrained Allocations 66 Some vmem clients are quite picky about the kind of address they want. 67 For example, the DVMA code may need an address that is at a particular 68 phase with respect to some alignment (to get good cache coloring), or 69 that lies within certain limits (the addressable range of a device), or 70 that doesn't cross some boundary (a DMA counter restriction) -- or all of 71 the above. vmem_xalloc() allows the client to specify any or all of 72 these constraints. 73 74 The Vmem Quantum 75 Every arena has a notion of `quantum', specified at vmem_create() time, 76 that defines the arena's minimum unit of currency. Most commonly the 77 quantum is either 1 or PAGESIZE, but any power of 2 is legal. All vmem 78 allocations are guaranteed to be quantum-aligned. 79 80 Relationship to the Kernel Memory Allocator 81 Every kmem cache has a vmem arena as its slab supplier. The kernel 82 memory allocator uses vmem_alloc() and vmem_free() to create and destroy 83 slabs. 84 85 SEE ALSO 86 id_space(9F), vmem_add(9F), vmem_alloc(9F), vmem_contains(9F), 87 vmem_create(9F), vmem_walk(9F) 88 89 90 Jeff Bonwick and Jonathan Adams, "Magazines and vmem: Extending the Slab 91 Allocator to Many CPUs and Arbitrary Resources.", Proceedings of the 2001 92 Usenix Conference, http://www.usenix.org/event/usenix01/bonwick.html. 93 94 illumos January 18, 2017 illumos