15a342f14SRichard Lowe.\" 25a342f14SRichard Lowe.\" CDDL HEADER START 35a342f14SRichard Lowe.\" 45a342f14SRichard Lowe.\" The contents of this file are subject to the terms of the 55a342f14SRichard Lowe.\" Common Development and Distribution License (the "License"). 65a342f14SRichard Lowe.\" You may not use this file except in compliance with the License. 75a342f14SRichard Lowe.\" 85a342f14SRichard Lowe.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95a342f14SRichard Lowe.\" or http://www.opensolaris.org/os/licensing. 105a342f14SRichard Lowe.\" See the License for the specific language governing permissions 115a342f14SRichard Lowe.\" and limitations under the License. 125a342f14SRichard Lowe.\" 135a342f14SRichard Lowe.\" When distributing Covered Code, include this CDDL HEADER in each 145a342f14SRichard Lowe.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155a342f14SRichard Lowe.\" If applicable, add the following below this CDDL HEADER, with the 165a342f14SRichard Lowe.\" fields enclosed by brackets "[]" replaced with your own identifying 175a342f14SRichard Lowe.\" information: Portions Copyright [yyyy] [name of copyright owner] 185a342f14SRichard Lowe.\" 195a342f14SRichard Lowe.\" CDDL HEADER END 205a342f14SRichard Lowe.\" 215a342f14SRichard Lowe.\" 225a342f14SRichard Lowe.\" Copyright 2010 Sun Microsystems, Inc. All rights reserved. 235a342f14SRichard Lowe.\" Use is subject to license terms. 245a342f14SRichard Lowe.\" 255a342f14SRichard Lowe.\" Copyright (c) 2012, 2015 by Delphix. All rights reserved. 265a342f14SRichard Lowe.\" Copyright (c) 2012, Joyent, Inc. All rights reserved. 275a342f14SRichard Lowe.\" 285a342f14SRichard Lowe.\" The text of this is derived from section 1 of the big theory statement in 295a342f14SRichard Lowe.\" usr/src/uts/common/os/vmem.c, the traditional location of this text. They 305a342f14SRichard Lowe.\" should largely be updated in tandem. 315a342f14SRichard Lowe.Dd Jan 18, 2017 325a342f14SRichard Lowe.Dt VMEM 9 335a342f14SRichard Lowe.Os 345a342f14SRichard Lowe.Sh NAME 355a342f14SRichard Lowe.Nm vmem 365a342f14SRichard Lowe.Nd virtual memory allocator 375a342f14SRichard Lowe.Sh DESCRIPTION 385a342f14SRichard Lowe.Ss Overview 395a342f14SRichard LoweAn address space is divided into a number of logically distinct pieces, or 405a342f14SRichard Lowe.Em arenas : 415a342f14SRichard Lowetext, data, heap, stack, and so on. 425a342f14SRichard LoweWithin these 435a342f14SRichard Lowearenas we often subdivide further; for example, we use heap addresses 445a342f14SRichard Lowenot only for the kernel heap 455a342f14SRichard Lowe.Po 465a342f14SRichard Lowe.Fn kmem_alloc 475a342f14SRichard Lowespace 485a342f14SRichard Lowe.Pc , 495a342f14SRichard Lowebut also for DVMA, 505a342f14SRichard Lowe.Fn bp_mapin , 515a342f14SRichard Lowe.Pa /dev/kmem , 525a342f14SRichard Loweand even some device mappings. 535a342f14SRichard Lowe.Pp 545a342f14SRichard LoweThe kernel address space, therefore, is most accurately described as 555a342f14SRichard Lowea tree of arenas in which each node of the tree 565a342f14SRichard Lowe.Em imports 575a342f14SRichard Lowesome subset of its parent. 585a342f14SRichard LoweThe virtual memory allocator manages these arenas 595a342f14SRichard Loweand supports their natural hierarchical structure. 605a342f14SRichard Lowe.Ss Arenas 61*72d3dbb9SYuri PankovAn arena is nothing more than a set of integers. 62*72d3dbb9SYuri PankovThese integers most commonly represent virtual addresses, but in fact they can 63*72d3dbb9SYuri Pankovrepresent anything at all. 64*72d3dbb9SYuri PankovFor example, we could use an arena containing the integers minpid through maxpid 65*72d3dbb9SYuri Pankovto allocate process IDs. 66*72d3dbb9SYuri PankovFor uses of this nature, prefer 675a342f14SRichard Lowe.Xr id_space 9F 685a342f14SRichard Loweinstead. 695a342f14SRichard Lowe.Pp 705a342f14SRichard Lowe.Fn vmem_create 715a342f14SRichard Loweand 725a342f14SRichard Lowe.Fn vmem_destroy 73*72d3dbb9SYuri Pankovcreate and destroy vmem arenas. 74*72d3dbb9SYuri PankovIn order to differentiate between arenas used for addresses and arenas used for 75*72d3dbb9SYuri Pankovidentifiers, the 765a342f14SRichard Lowe.Dv VMC_IDENTIFIER 775a342f14SRichard Loweflag is passed to 785a342f14SRichard Lowe.Fn vmem_create . 795a342f14SRichard LoweThis prevents identifier exhaustion from being diagnosed as general memory 805a342f14SRichard Lowefailure. 815a342f14SRichard Lowe.Ss Spans 825a342f14SRichard LoweWe represent the integers in an arena as a collection of 835a342f14SRichard Lowe.Em spans , 84*72d3dbb9SYuri Pankovor contiguous ranges of integers. 85*72d3dbb9SYuri PankovFor example, the kernel heap consists of just one span: 865a342f14SRichard Lowe.Li "[kernelheap, ekernelheap)" . 875a342f14SRichard LoweSpans can be added to an arena in two ways: explicitly, by 885a342f14SRichard Lowe.Fn vmem_add ; 895a342f14SRichard Loweor implicitly, by importing, as described in 905a342f14SRichard Lowe.Sx Imported Memory 915a342f14SRichard Lowebelow. 925a342f14SRichard Lowe.Ss Segments 935a342f14SRichard LoweSpans are subdivided into 945a342f14SRichard Lowe.Em segments , 95*72d3dbb9SYuri Pankoveach of which is either allocated or free. 96*72d3dbb9SYuri PankovA segment, like a span, is a contiguous range of integers. 97*72d3dbb9SYuri PankovEach allocated segment 985a342f14SRichard Lowe.Li "[addr, addr + size)" 995a342f14SRichard Lowerepresents exactly one 1005a342f14SRichard Lowe.Li "vmem_alloc(size)" 1015a342f14SRichard Lowethat returned 1025a342f14SRichard Lowe.Sy addr . 103*72d3dbb9SYuri PankovFree segments represent the space between allocated segments. 104*72d3dbb9SYuri PankovIf two free segments are adjacent, we coalesce them into one larger segment; 105*72d3dbb9SYuri Pankovthat is, if segments 1065a342f14SRichard Lowe.Li "[a, b)" 1075a342f14SRichard Loweand 1085a342f14SRichard Lowe.Li "[b, c)" 1095a342f14SRichard Loweare both free, we merge them into a single segment 1105a342f14SRichard Lowe.Li "[a, c)" . 1115a342f14SRichard LoweThe segments within a span are linked together in increasing\-address 1125a342f14SRichard Loweorder so we can easily determine whether coalescing is possible. 1135a342f14SRichard Lowe.Pp 114*72d3dbb9SYuri PankovSegments never cross span boundaries. 115*72d3dbb9SYuri PankovWhen all segments within an imported span become free, we return the span to its 116*72d3dbb9SYuri Pankovsource. 1175a342f14SRichard Lowe.Ss Imported Memory 118*72d3dbb9SYuri PankovAs mentioned in the overview, some arenas are logical subsets of other arenas. 119*72d3dbb9SYuri PankovFor example, 1205a342f14SRichard Lowe.Sy kmem_va_arena 1215a342f14SRichard Lowe(a virtual address cache 1225a342f14SRichard Lowethat satisfies most 1235a342f14SRichard Lowe.Fn kmem_slab_create 1245a342f14SRichard Lowerequests) is just a subset of 1255a342f14SRichard Lowe.Sy heap_arena 126*72d3dbb9SYuri Pankov(the kernel heap) that provides caching for the most common slab sizes. 127*72d3dbb9SYuri PankovWhen 1285a342f14SRichard Lowe.Sy kmem_va_arena 1295a342f14SRichard Loweruns out of virtual memory, it 1305a342f14SRichard Lowe.Em imports 1315a342f14SRichard Lowemore from the heap; we say that 1325a342f14SRichard Lowe.Sy heap_arena 1335a342f14SRichard Loweis the 1345a342f14SRichard Lowe.Em "vmem source" 1355a342f14SRichard Lowefor 1365a342f14SRichard Lowe.Sy kmem_va_arena. 1375a342f14SRichard Lowe.Fn vmem_create 138*72d3dbb9SYuri Pankovallows you to specify any existing vmem arena as the source for your new arena. 139*72d3dbb9SYuri PankovTopologically, since every arena is a child of at most one source, the set of 140*72d3dbb9SYuri Pankovall arenas forms a collection of trees. 1415a342f14SRichard Lowe.Ss Constrained Allocations 1425a342f14SRichard LoweSome vmem clients are quite picky about the kind of address they want. 1435a342f14SRichard LoweFor example, the DVMA code may need an address that is at a particular 1445a342f14SRichard Lowephase with respect to some alignment (to get good cache coloring), or 1455a342f14SRichard Lowethat lies within certain limits (the addressable range of a device), 1465a342f14SRichard Loweor that doesn't cross some boundary (a DMA counter restriction) \(em 1475a342f14SRichard Loweor all of the above. 1485a342f14SRichard Lowe.Fn vmem_xalloc 1495a342f14SRichard Loweallows the client to specify any or all of these constraints. 1505a342f14SRichard Lowe.Ss The Vmem Quantum 1515a342f14SRichard LoweEvery arena has a notion of 1525a342f14SRichard Lowe.Sq quantum , 1535a342f14SRichard Lowespecified at 1545a342f14SRichard Lowe.Fn vmem_create 155*72d3dbb9SYuri Pankovtime, that defines the arena's minimum unit of currency. 156*72d3dbb9SYuri PankovMost commonly the quantum is either 1 or 1575a342f14SRichard Lowe.Dv PAGESIZE , 158*72d3dbb9SYuri Pankovbut any power of 2 is legal. 159*72d3dbb9SYuri PankovAll vmem allocations are guaranteed to be quantum\-aligned. 1605a342f14SRichard Lowe.Ss Relationship to the Kernel Memory Allocator 161*72d3dbb9SYuri PankovEvery kmem cache has a vmem arena as its slab supplier. 162*72d3dbb9SYuri PankovThe kernel memory allocator uses 1635a342f14SRichard Lowe.Fn vmem_alloc 1645a342f14SRichard Loweand 1655a342f14SRichard Lowe.Fn vmem_free 1665a342f14SRichard Loweto create and destroy slabs. 1675a342f14SRichard Lowe.Sh SEE ALSO 1685a342f14SRichard Lowe.Xr id_space 9F , 1695a342f14SRichard Lowe.Xr vmem_add 9F , 1705a342f14SRichard Lowe.Xr vmem_alloc 9F , 1715a342f14SRichard Lowe.Xr vmem_contains 9F , 1725a342f14SRichard Lowe.Xr vmem_create 9F , 1735a342f14SRichard Lowe.Xr vmem_walk 9F 1745a342f14SRichard Lowe.Pp 1755a342f14SRichard Lowe.Rs 1765a342f14SRichard Lowe.%A Jeff Bonwick 1775a342f14SRichard Lowe.%A Jonathan Adams 1785a342f14SRichard Lowe.%T Magazines and vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resources. 1795a342f14SRichard Lowe.%J Proceedings of the 2001 Usenix Conference 1805a342f14SRichard Lowe.%U http://www.usenix.org/event/usenix01/bonwick.html 1815a342f14SRichard Lowe.Re 182