10aa028ffSDag-Erling Smørgrav.\"- 2e738085bSDag-Erling Smørgrav.\" Copyright (c) 2001 Dag-Erling Smørgrav 30aa028ffSDag-Erling Smørgrav.\" All rights reserved. 40aa028ffSDag-Erling Smørgrav.\" 50aa028ffSDag-Erling Smørgrav.\" Redistribution and use in source and binary forms, with or without 60aa028ffSDag-Erling Smørgrav.\" modification, are permitted provided that the following conditions 70aa028ffSDag-Erling Smørgrav.\" are met: 80aa028ffSDag-Erling Smørgrav.\" 1. Redistributions of source code must retain the above copyright 90aa028ffSDag-Erling Smørgrav.\" notice, this list of conditions and the following disclaimer. 100aa028ffSDag-Erling Smørgrav.\" 2. Redistributions in binary form must reproduce the above copyright 110aa028ffSDag-Erling Smørgrav.\" notice, this list of conditions and the following disclaimer in the 120aa028ffSDag-Erling Smørgrav.\" documentation and/or other materials provided with the distribution. 130aa028ffSDag-Erling Smørgrav.\" 140aa028ffSDag-Erling Smørgrav.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 150aa028ffSDag-Erling Smørgrav.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 160aa028ffSDag-Erling Smørgrav.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 170aa028ffSDag-Erling Smørgrav.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 180aa028ffSDag-Erling Smørgrav.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 190aa028ffSDag-Erling Smørgrav.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 200aa028ffSDag-Erling Smørgrav.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 210aa028ffSDag-Erling Smørgrav.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 220aa028ffSDag-Erling Smørgrav.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 230aa028ffSDag-Erling Smørgrav.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 240aa028ffSDag-Erling Smørgrav.\" SUCH DAMAGE. 250aa028ffSDag-Erling Smørgrav.\" 260de03c30SMark Johnston.Dd January 16, 2023 27d794b3a3SMark Johnston.Dt UMA 9 280aa028ffSDag-Erling Smørgrav.Os 290aa028ffSDag-Erling Smørgrav.Sh NAME 30d794b3a3SMark Johnston.Nm UMA 31d794b3a3SMark Johnston.Nd general-purpose kernel object allocator 320aa028ffSDag-Erling Smørgrav.Sh SYNOPSIS 3332eef9aeSRuslan Ermilov.In sys/param.h 34f16b3c0dSChad David.In sys/queue.h 353a347a6eSJeroen Ruigrok van der Werven.In vm/uma.h 36d794b3a3SMark Johnston.Bd -literal 37d794b3a3SMark Johnstontypedef int (*uma_ctor)(void *mem, int size, void *arg, int flags); 38d794b3a3SMark Johnstontypedef void (*uma_dtor)(void *mem, int size, void *arg); 39d794b3a3SMark Johnstontypedef int (*uma_init)(void *mem, int size, int flags); 40d794b3a3SMark Johnstontypedef void (*uma_fini)(void *mem, int size); 41d794b3a3SMark Johnstontypedef int (*uma_import)(void *arg, void **store, int count, int domain, 42d794b3a3SMark Johnston int flags); 43d794b3a3SMark Johnstontypedef void (*uma_release)(void *arg, void **store, int count); 44d794b3a3SMark Johnstontypedef void *(*uma_alloc)(uma_zone_t zone, vm_size_t size, int domain, 45d794b3a3SMark Johnston uint8_t *pflag, int wait); 46d794b3a3SMark Johnstontypedef void (*uma_free)(void *item, vm_size_t size, uint8_t pflag); 47d794b3a3SMark Johnston 48d794b3a3SMark Johnston.Ed 49382682b2SJeroen Ruigrok van der Werven.Ft uma_zone_t 509be1948fSRuslan Ermilov.Fo uma_zcreate 511e902c9bSGleb Smirnoff.Fa "char *name" "size_t size" 52d794b3a3SMark Johnston.Fa "uma_ctor ctor" "uma_dtor dtor" "uma_init zinit" "uma_fini zfini" 536b99842aSEd Schouten.Fa "int align" "uint16_t flags" 549be1948fSRuslan Ermilov.Fc 55d794b3a3SMark Johnston.Ft uma_zone_t 56d794b3a3SMark Johnston.Fo uma_zcache_create 57d794b3a3SMark Johnston.Fa "char *name" "int size" 58d794b3a3SMark Johnston.Fa "uma_ctor ctor" "uma_dtor dtor" "uma_init zinit" "uma_fini zfini" 59d794b3a3SMark Johnston.Fa "uma_import zimport" "uma_release zrelease" 60d794b3a3SMark Johnston.Fa "void *arg" "int flags" 61d794b3a3SMark Johnston.Fc 62d794b3a3SMark Johnston.Ft uma_zone_t 63d794b3a3SMark Johnston.Fo uma_zsecond_create 64d794b3a3SMark Johnston.Fa "char *name" 65d794b3a3SMark Johnston.Fa "uma_ctor ctor" "uma_dtor dtor" "uma_init zinit" "uma_fini zfini" 66d794b3a3SMark Johnston.Fa "uma_zone_t master" 67d794b3a3SMark Johnston.Fc 68d794b3a3SMark Johnston.Ft void 69d794b3a3SMark Johnston.Fn uma_zdestroy "uma_zone_t zone" 709be1948fSRuslan Ermilov.Ft "void *" 719be1948fSRuslan Ermilov.Fn uma_zalloc "uma_zone_t zone" "int flags" 726a4c41d1SRemko Lodder.Ft "void *" 736a4c41d1SRemko Lodder.Fn uma_zalloc_arg "uma_zone_t zone" "void *arg" "int flags" 7493f31533SJeff Roberson.Ft "void *" 7593f31533SJeff Roberson.Fn uma_zalloc_domain "uma_zone_t zone" "void *arg" "int domain" "int flags" 76d794b3a3SMark Johnston.Ft "void *" 77d794b3a3SMark Johnston.Fn uma_zalloc_pcpu "uma_zone_t zone" "int flags" 78d794b3a3SMark Johnston.Ft "void *" 79d794b3a3SMark Johnston.Fn uma_zalloc_pcpu_arg "uma_zone_t zone" "void *arg" "int flags" 800de03c30SMark Johnston.Ft "void *" 810de03c30SMark Johnston.Fn uma_zalloc_smr "uma_zone_t zone" "int flags" 820aa028ffSDag-Erling Smørgrav.Ft void 83f1de92e6SJeroen Ruigrok van der Werven.Fn uma_zfree "uma_zone_t zone" "void *item" 845ca0c84eSThomas Moestl.Ft void 856a4c41d1SRemko Lodder.Fn uma_zfree_arg "uma_zone_t zone" "void *item" "void *arg" 866a4c41d1SRemko Lodder.Ft void 87d794b3a3SMark Johnston.Fn uma_zfree_pcpu "uma_zone_t zone" "void *item" 88d794b3a3SMark Johnston.Ft void 89d794b3a3SMark Johnston.Fn uma_zfree_pcpu_arg "uma_zone_t zone" "void *item" "void *arg" 90d794b3a3SMark Johnston.Ft void 910de03c30SMark Johnston.Fn uma_zfree_smr "uma_zone_t zone" "void *item" 920de03c30SMark Johnston.Ft void 93d794b3a3SMark Johnston.Fn uma_prealloc "uma_zone_t zone" "int nitems" 94d794b3a3SMark Johnston.Ft void 95d794b3a3SMark Johnston.Fn uma_zone_reserve "uma_zone_t zone" "int nitems" 96d794b3a3SMark Johnston.Ft void 97d794b3a3SMark Johnston.Fn uma_zone_reserve_kva "uma_zone_t zone" "int nitems" 98d794b3a3SMark Johnston.Ft void 9908cfa56eSMark Johnston.Fn uma_reclaim "int req" 10008cfa56eSMark Johnston.Ft void 101aabe13f1SMark Johnston.Fn uma_reclaim_domain "int req" "int domain" 102aabe13f1SMark Johnston.Ft void 10308cfa56eSMark Johnston.Fn uma_zone_reclaim "uma_zone_t zone" "int req" 10408cfa56eSMark Johnston.Ft void 105aabe13f1SMark Johnston.Fn uma_zone_reclaim_domain "uma_zone_t zone" "int req" "int domain" 106aabe13f1SMark Johnston.Ft void 107d794b3a3SMark Johnston.Fn uma_zone_set_allocf "uma_zone_t zone" "uma_alloc allocf" 108d794b3a3SMark Johnston.Ft void 109d794b3a3SMark Johnston.Fn uma_zone_set_freef "uma_zone_t zone" "uma_free freef" 1101c6cae97SLawrence Stewart.Ft int 111d7bbb21aSHartmut Brandt.Fn uma_zone_set_max "uma_zone_t zone" "int nitems" 112003cf08bSMark Johnston.Ft void 113d794b3a3SMark Johnston.Fn uma_zone_set_maxcache "uma_zone_t zone" "int nitems" 114d794b3a3SMark Johnston.Ft int 115c4ae7908SLawrence Stewart.Fn uma_zone_get_max "uma_zone_t zone" 116c4ae7908SLawrence Stewart.Ft int 117c4ae7908SLawrence Stewart.Fn uma_zone_get_cur "uma_zone_t zone" 1182f891cd5SPawel Jakub Dawidek.Ft void 1192f891cd5SPawel Jakub Dawidek.Fn uma_zone_set_warning "uma_zone_t zone" "const char *warning" 12054503a13SJonathan T. Looney.Ft void 12154503a13SJonathan T. Looney.Fn uma_zone_set_maxaction "uma_zone_t zone" "void (*maxaction)(uma_zone_t)" 1220de03c30SMark Johnston.Ft smr_t 1230de03c30SMark Johnston.Fn uma_zone_get_smr "uma_zone_t zone" 1240de03c30SMark Johnston.Ft void 1250de03c30SMark Johnston.Fn uma_zone_set_smr "uma_zone_t zone" "smr_t smr" 1260a5a3ccbSGleb Smirnoff.In sys/sysctl.h 1270a5a3ccbSGleb Smirnoff.Fn SYSCTL_UMA_MAX parent nbr name access zone descr 1280a5a3ccbSGleb Smirnoff.Fn SYSCTL_ADD_UMA_MAX ctx parent nbr name access zone descr 1290a5a3ccbSGleb Smirnoff.Fn SYSCTL_UMA_CUR parent nbr name access zone descr 1300a5a3ccbSGleb Smirnoff.Fn SYSCTL_ADD_UMA_CUR ctx parent nbr name access zone descr 1310aa028ffSDag-Erling Smørgrav.Sh DESCRIPTION 132d794b3a3SMark JohnstonUMA (Universal Memory Allocator) provides an efficient interface for managing 133d794b3a3SMark Johnstondynamically-sized collections of items of identical size, referred to as zones. 134d794b3a3SMark JohnstonZones keep track of which items are in use and which 135d794b3a3SMark Johnstonare not, and UMA provides functions for allocating items from a zone and 136d794b3a3SMark Johnstonfor releasing them back, making them available for subsequent allocation requests. 137d794b3a3SMark JohnstonZones maintain per-CPU caches with linear scalability on SMP 13893f31533SJeff Robersonsystems as well as round-robin and first-touch policies for NUMA 13993f31533SJeff Robersonsystems. 140d794b3a3SMark JohnstonThe number of items cached per CPU is bounded, and each zone additionally 141d794b3a3SMark Johnstonmaintains an unbounded cache of items that is used to quickly satisfy 142d794b3a3SMark Johnstonper-CPU cache allocation misses. 1430aa028ffSDag-Erling Smørgrav.Pp 144d794b3a3SMark JohnstonTwo types of zones exist: regular zones and cache zones. 145d794b3a3SMark JohnstonIn a regular zone, items are allocated from a slab, which is one or more 146d794b3a3SMark Johnstonvirtually contiguous memory pages that have been allocated from the kernel's 147d794b3a3SMark Johnstonpage allocator. 148d794b3a3SMark JohnstonInternally, slabs are managed by a UMA keg, which is responsible for allocating 149d794b3a3SMark Johnstonslabs and keeping track of their usage by one or more zones. 150d794b3a3SMark JohnstonIn typical usage, there is one keg per zone, so slabs are not shared among 151d794b3a3SMark Johnstonmultiple zones. 1520aa028ffSDag-Erling Smørgrav.Pp 153d794b3a3SMark JohnstonNormal zones import items from a keg, and release items back to that keg if 154d794b3a3SMark Johnstonrequested. 155d794b3a3SMark JohnstonCache zones do not have a keg, and instead use custom import and release 156d794b3a3SMark Johnstonmethods. 157d794b3a3SMark JohnstonFor example, some collections of kernel objects are statically allocated 158d794b3a3SMark Johnstonat boot-time, and the size of the collection does not change. 159d794b3a3SMark JohnstonA cache zone can be used to implement an efficient allocator for the objects in 160d794b3a3SMark Johnstonsuch a collection. 16190d83886SJulian Elischer.Pp 162382682b2SJeroen Ruigrok van der WervenThe 163382682b2SJeroen Ruigrok van der Werven.Fn uma_zcreate 164d794b3a3SMark Johnstonand 165d794b3a3SMark Johnston.Fn uma_zcache_create 166d794b3a3SMark Johnstonfunctions create a new regular zone and cache zone, respectively. 167d794b3a3SMark JohnstonThe 168d794b3a3SMark Johnston.Fn uma_zsecond_create 169d794b3a3SMark Johnstonfunction creates a regular zone which shares the keg of the zone 170d794b3a3SMark Johnstonspecified by the 171d794b3a3SMark Johnston.Fa master 172d794b3a3SMark Johnstonargument. 173382682b2SJeroen Ruigrok van der WervenThe 174382682b2SJeroen Ruigrok van der Werven.Fa name 175382682b2SJeroen Ruigrok van der Wervenargument is a text name of the zone for debugging and stats; this memory 176382682b2SJeroen Ruigrok van der Wervenshould not be freed until the zone has been deallocated. 177382682b2SJeroen Ruigrok van der Werven.Pp 1784919e8cbSAlfred PerlsteinThe 1794919e8cbSAlfred Perlstein.Fa ctor 1804919e8cbSAlfred Perlsteinand 1814919e8cbSAlfred Perlstein.Fa dtor 1824919e8cbSAlfred Perlsteinarguments are callback functions that are called by 183d794b3a3SMark Johnstonthe UMA subsystem at the time of the call to 1844919e8cbSAlfred Perlstein.Fn uma_zalloc 1854919e8cbSAlfred Perlsteinand 1864919e8cbSAlfred Perlstein.Fn uma_zfree 187d962d52aSRuslan Ermilovrespectively. 188d962d52aSRuslan ErmilovTheir purpose is to provide hooks for initializing or 1894919e8cbSAlfred Perlsteindestroying things that need to be done at the time of the allocation 190d962d52aSRuslan Ermilovor release of a resource. 191e07f1d5fSHiten PandyaA good usage for the 1924919e8cbSAlfred Perlstein.Fa ctor 1934919e8cbSAlfred Perlsteinand 1944919e8cbSAlfred Perlstein.Fa dtor 195d794b3a3SMark Johnstoncallbacks might be to initialize a data structure embedded in the item, 196d794b3a3SMark Johnstonsuch as a 197d794b3a3SMark Johnston.Xr queue 3 198d794b3a3SMark Johnstonhead. 1994919e8cbSAlfred Perlstein.Pp 2004919e8cbSAlfred PerlsteinThe 201d794b3a3SMark Johnston.Fa zinit 2024919e8cbSAlfred Perlsteinand 203d794b3a3SMark Johnston.Fa zfini 204d794b3a3SMark Johnstonarguments are used to optimize the allocation of items from the zone. 205d794b3a3SMark JohnstonThey are called by the UMA subsystem whenever 206d794b3a3SMark Johnstonit needs to allocate or free items to satisfy requests or memory pressure. 207d962d52aSRuslan ErmilovA good use for the 208d794b3a3SMark Johnston.Fa zinit 209d962d52aSRuslan Ermilovand 210d794b3a3SMark Johnston.Fa zfini 211d962d52aSRuslan Ermilovcallbacks might be to 212d794b3a3SMark Johnstoninitialize and destroy a mutex contained within an item. 213d794b3a3SMark JohnstonThis would allow one to avoid destroying and re-initializing the mutex 214d794b3a3SMark Johnstoneach time the item is freed and re-allocated. 215d962d52aSRuslan ErmilovThey are not called on each call to 2164919e8cbSAlfred Perlstein.Fn uma_zalloc 2174919e8cbSAlfred Perlsteinand 2184919e8cbSAlfred Perlstein.Fn uma_zfree 219d794b3a3SMark Johnstonbut rather when an item is imported into a zone's cache, and when a zone 220d794b3a3SMark Johnstonreleases an item to the slab allocator, typically as a response to memory 221d794b3a3SMark Johnstonpressure. 222d794b3a3SMark Johnston.Pp 223d794b3a3SMark JohnstonFor 224d794b3a3SMark Johnston.Fn uma_zcache_create , 225d794b3a3SMark Johnstonthe 226d794b3a3SMark Johnston.Fa zimport 227d794b3a3SMark Johnstonand 228d794b3a3SMark Johnston.Fa zrelease 229d794b3a3SMark Johnstonfunctions are called to import items into the zone and to release items 230d794b3a3SMark Johnstonfrom the zone, respectively. 231d794b3a3SMark JohnstonThe 232d794b3a3SMark Johnston.Fa zimport 233d794b3a3SMark Johnstonfunction should store pointers to items in the 234d794b3a3SMark Johnston.Fa store 235d794b3a3SMark Johnstonarray, which contains a maximum of 236d794b3a3SMark Johnston.Fa count 237d794b3a3SMark Johnstonentries. 238d794b3a3SMark JohnstonThe function must return the number of imported items, which may be less than 239d794b3a3SMark Johnstonthe maximum. 240d794b3a3SMark JohnstonSimilarly, the 241d794b3a3SMark Johnston.Fa store 242d794b3a3SMark Johnstonparameter to the 243d794b3a3SMark Johnston.Fa zrelease 244d794b3a3SMark Johnstonfunction contains an array of 245d794b3a3SMark Johnston.Fa count 246d794b3a3SMark Johnstonpointers to items. 247d794b3a3SMark JohnstonThe 248d794b3a3SMark Johnston.Fa arg 249d794b3a3SMark Johnstonparameter passed to 250d794b3a3SMark Johnston.Fn uma_zcache_create 251d794b3a3SMark Johnstonis provided to the import and release functions. 252d794b3a3SMark JohnstonThe 253d794b3a3SMark Johnston.Fa domain 254d794b3a3SMark Johnstonparameter to 255d794b3a3SMark Johnston.Fa zimport 256d794b3a3SMark Johnstonspecifies the requested 257d794b3a3SMark Johnston.Xr numa 4 258d794b3a3SMark Johnstondomain for the allocation. 259d794b3a3SMark JohnstonIt is either a NUMA domain number or the special value 260d794b3a3SMark Johnston.Dv UMA_ANYDOMAIN . 2614919e8cbSAlfred Perlstein.Pp 262b7e2b86cSGleb SmirnoffThe 263b7e2b86cSGleb Smirnoff.Fa flags 264d794b3a3SMark Johnstonargument of 265b7e2b86cSGleb Smirnoff.Fn uma_zcreate 266d794b3a3SMark Johnstonand 267d794b3a3SMark Johnston.Fn uma_zcache_create 268b7e2b86cSGleb Smirnoffis a subset of the following flags: 269b7e2b86cSGleb Smirnoff.Bl -tag -width "foo" 270b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_NOFREE 271d794b3a3SMark JohnstonSlabs allocated to the zone's keg are never freed. 272b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_NODUMP 273d794b3a3SMark JohnstonPages belonging to the zone will not be included in minidumps. 274ad97af7eSGleb Smirnoff.It Dv UMA_ZONE_PCPU 275ad97af7eSGleb SmirnoffAn allocation from zone would have 276ad97af7eSGleb Smirnoff.Va mp_ncpu 277ad97af7eSGleb Smirnoffshadow copies, that are privately assigned to CPUs. 278d794b3a3SMark JohnstonA CPU can address its private copy using base the allocation address plus 279d794b3a3SMark Johnstona multiple of the current CPU ID and 280ad97af7eSGleb Smirnoff.Fn sizeof "struct pcpu" : 281ad97af7eSGleb Smirnoff.Bd -literal -offset indent 282ad97af7eSGleb Smirnofffoo_zone = uma_zcreate(..., UMA_ZONE_PCPU); 283ad97af7eSGleb Smirnoff ... 284ad97af7eSGleb Smirnofffoo_base = uma_zalloc(foo_zone, ...); 285ad97af7eSGleb Smirnoff ... 286ad97af7eSGleb Smirnoffcritical_enter(); 287ad97af7eSGleb Smirnofffoo_pcpu = (foo_t *)zpcpu_get(foo_base); 288ad97af7eSGleb Smirnoff/* do something with foo_pcpu */ 289ad97af7eSGleb Smirnoffcritical_exit(); 290d794b3a3SMark Johnston 291ad97af7eSGleb Smirnoff.Ed 292d794b3a3SMark JohnstonNote that 293d794b3a3SMark Johnston.Dv M_ZERO 294d794b3a3SMark Johnstoncannot be used when allocating items from a PCPU zone. 295d794b3a3SMark JohnstonTo obtain zeroed memory from a PCPU zone, use the 296d794b3a3SMark Johnston.Fn uma_zalloc_pcpu 297d794b3a3SMark Johnstonfunction and its variants instead, and pass 298d794b3a3SMark Johnston.Dv M_ZERO . 29954c5ae80SRyan Libby.It Dv UMA_ZONE_NOTOUCH 30054c5ae80SRyan LibbyThe UMA subsystem may not directly touch (i.e. read or write) the slab memory. 30154c5ae80SRyan LibbyOtherwise, by default, book-keeping of items within a slab may be done in the 30254c5ae80SRyan Libbyslab page itself, and 30354c5ae80SRyan Libby.Dv INVARIANTS 30454c5ae80SRyan Libbykernels may also do use-after-free checking by accessing the slab memory. 305b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_ZINIT 306b7e2b86cSGleb SmirnoffThe zone will have its 307b7e2b86cSGleb Smirnoff.Ft uma_init 308b7e2b86cSGleb Smirnoffmethod set to internal method that initializes a new allocated slab 309b7e2b86cSGleb Smirnoffto all zeros. 310b1edef17SJoel DahlDo not mistake 311b7e2b86cSGleb Smirnoff.Ft uma_init 312b7e2b86cSGleb Smirnoffmethod with 313b7e2b86cSGleb Smirnoff.Ft uma_ctor . 314b7e2b86cSGleb SmirnoffA zone with 315b7e2b86cSGleb Smirnoff.Dv UMA_ZONE_ZINIT 316b7e2b86cSGleb Smirnoffflag would not return zeroed memory on every 317b7e2b86cSGleb Smirnoff.Fn uma_zalloc . 31854c5ae80SRyan Libby.It Dv UMA_ZONE_NOTPAGE 31954c5ae80SRyan LibbyAn allocator function will be supplied with 32054c5ae80SRyan Libby.Fn uma_zone_set_allocf 32154c5ae80SRyan Libbyand the memory that it returns may not be kernel virtual memory backed by VM 32254c5ae80SRyan Libbypages in the page array. 323b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_MALLOC 324b7e2b86cSGleb SmirnoffThe zone is for the 325b7e2b86cSGleb Smirnoff.Xr malloc 9 326b7e2b86cSGleb Smirnoffsubsystem. 327b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_VM 328b7e2b86cSGleb SmirnoffThe zone is for the VM subsystem. 329ec0d8280SRyan Libby.It Dv UMA_ZONE_CONTIG 330ec0d8280SRyan LibbyItems in this zone must be contiguous in physical address space. 331ec0d8280SRyan LibbyItems will follow normal alignment constraints and may span page boundaries 332ec0d8280SRyan Libbybetween pages with contiguous physical addresses. 333389a3fa6SMark Johnston.It Dv UMA_ZONE_UNMANAGED 334389a3fa6SMark JohnstonBy default, UMA zone caches are shrunk to help resolve free page shortages. 335389a3fa6SMark JohnstonCached items that have not been used for a long period may also be freed from 336389a3fa6SMark Johnstonzone. 337389a3fa6SMark JohnstonWhen this flag is set, the system will not reclaim memory from the zone's 338389a3fa6SMark Johnstoncaches. 3390de03c30SMark Johnston.It Dv UMA_ZONE_SMR 3400de03c30SMark JohnstonCreate a zone whose items will be synchronized using the 3410de03c30SMark Johnston.Xr smr 9 3420de03c30SMark Johnstonmechanism. 3430de03c30SMark JohnstonUpon creation the zone will have an associated 344*8b92fa1cSGraham Percival.Ft smr_t 3450de03c30SMark Johnstonstructure which can be fetched using 3460de03c30SMark Johnston.Fn uma_zone_get_smr . 347b7e2b86cSGleb Smirnoff.El 348b7e2b86cSGleb Smirnoff.Pp 349d794b3a3SMark JohnstonZones can be destroyed using 350d794b3a3SMark Johnston.Fn uma_zdestroy , 351d794b3a3SMark Johnstonfreeing all memory that is cached in the zone. 352d794b3a3SMark JohnstonAll items allocated from the zone must be freed to the zone before the zone 353d794b3a3SMark Johnstonmay be safely destroyed. 354d794b3a3SMark Johnston.Pp 3550aa028ffSDag-Erling SmørgravTo allocate an item from a zone, simply call 3567c32182eSJeroen Ruigrok van der Werven.Fn uma_zalloc 357d794b3a3SMark Johnstonwith a pointer to that zone and set the 358acff84fbSJeroen Ruigrok van der Werven.Fa flags 359acff84fbSJeroen Ruigrok van der Wervenargument to selected flags as documented in 360acff84fbSJeroen Ruigrok van der Werven.Xr malloc 9 . 361d794b3a3SMark JohnstonIt will return a pointer to an item if successful, or 3620aa028ffSDag-Erling Smørgrav.Dv NULL 3630aa028ffSDag-Erling Smørgravin the rare case where all items in the zone are in use and the 364d794b3a3SMark Johnstonallocator is unable to grow the zone and 3651437c79aSJeroen Ruigrok van der Werven.Dv M_NOWAIT 3661437c79aSJeroen Ruigrok van der Wervenis specified. 3670aa028ffSDag-Erling Smørgrav.Pp 3680aa028ffSDag-Erling SmørgravItems are released back to the zone from which they were allocated by 3690aa028ffSDag-Erling Smørgravcalling 3707c32182eSJeroen Ruigrok van der Werven.Fn uma_zfree 3710aa028ffSDag-Erling Smørgravwith a pointer to the zone and a pointer to the item. 37220ed0cb0SMatthew D FlemingIf 37320ed0cb0SMatthew D Fleming.Fa item 37420ed0cb0SMatthew D Flemingis 37520ed0cb0SMatthew D Fleming.Dv NULL , 37620ed0cb0SMatthew D Flemingthen 37720ed0cb0SMatthew D Fleming.Fn uma_zfree 37820ed0cb0SMatthew D Flemingdoes nothing. 3795ca0c84eSThomas Moestl.Pp 380d794b3a3SMark JohnstonThe variants 3816a4c41d1SRemko Lodder.Fn uma_zalloc_arg 3826a4c41d1SRemko Lodderand 3836a4c41d1SRemko Lodder.Fn uma_zfree_arg 38493f31533SJeff Robersonallow callers to 3856a4c41d1SRemko Lodderspecify an argument for the 3866a4c41d1SRemko Lodder.Dv ctor 3876a4c41d1SRemko Lodderand 3886a4c41d1SRemko Lodder.Dv dtor 389d794b3a3SMark Johnstonfunctions of the zone, respectively. 39051dc8e7fSKristof ProvostThe variants 39151dc8e7fSKristof Provost.Fn uma_zalloc_pcpu 39251dc8e7fSKristof Provostand 39351dc8e7fSKristof Provost.Fn uma_zfree_pcpu 39451dc8e7fSKristof Provostallocate and free 39551dc8e7fSKristof Provost.Va mp_ncpu 39651dc8e7fSKristof Provostshadow copies as described for 39751dc8e7fSKristof Provost.Dv UMA_ZONE_PCPU . 39851dc8e7fSKristof ProvostIf 39951dc8e7fSKristof Provost.Fa item 40051dc8e7fSKristof Provostis 40151dc8e7fSKristof Provost.Dv NULL , 40251dc8e7fSKristof Provostthen 40351dc8e7fSKristof Provost.Fn uma_zfree_pcpu 40451dc8e7fSKristof Provostdoes nothing. 40551dc8e7fSKristof Provost.Pp 40693f31533SJeff RobersonThe 4070de03c30SMark Johnston.Fn uma_zalloc_smr 4080de03c30SMark Johnstonand 4090de03c30SMark Johnston.Fn uma_zfree_smr 4100de03c30SMark Johnstonfunctions allocate and free items from an SMR-enabled zone, that is, 4110de03c30SMark Johnstona zone created with 4120de03c30SMark Johnston.Dv UMA_ZONE_SMR 4130de03c30SMark Johnstonor a zone that has had 4140de03c30SMark Johnston.Fn uma_zone_set_smr 4150de03c30SMark Johnstoncalled. 4160de03c30SMark Johnston.Pp 4170de03c30SMark JohnstonThe 41893f31533SJeff Roberson.Fn uma_zalloc_domain 41993f31533SJeff Robersonfunction allows callers to specify a fixed 420d794b3a3SMark Johnston.Xr numa 4 421d794b3a3SMark Johnstondomain to allocate from. 422d794b3a3SMark JohnstonThis uses a guaranteed but slow path in the allocator which reduces 423d794b3a3SMark Johnstonconcurrency. 4246a4c41d1SRemko Lodder.Pp 425d794b3a3SMark JohnstonThe 426a418d47bSWarner Losh.Fn uma_prealloc 427d794b3a3SMark Johnstonfunction allocates slabs for the requested number of items, typically following 428d794b3a3SMark Johnstonthe initial creation of a zone. 429d794b3a3SMark JohnstonSubsequent allocations from the zone will be satisfied using the pre-allocated 430d794b3a3SMark Johnstonslabs. 431d794b3a3SMark JohnstonNote that slab allocation is performed with the 432d794b3a3SMark Johnston.Dv M_WAITOK 433d794b3a3SMark Johnstonflag, so 434a418d47bSWarner Losh.Fn uma_prealloc 435d794b3a3SMark Johnstonmay sleep. 436d794b3a3SMark Johnston.Pp 437d794b3a3SMark JohnstonThe 438d794b3a3SMark Johnston.Fn uma_zone_reserve 439d794b3a3SMark Johnstonfunction sets the number of reserved items for the zone. 4407c32182eSJeroen Ruigrok van der Werven.Fn uma_zalloc 441d794b3a3SMark Johnstonand variants will ensure that the zone contains at least the reserved number 442d794b3a3SMark Johnstonof free items. 443d794b3a3SMark JohnstonReserved items may be allocated by specifying 444d794b3a3SMark Johnston.Dv M_USE_RESERVE 445d794b3a3SMark Johnstonin the allocation request flags. 446d794b3a3SMark Johnston.Fn uma_zone_reserve 447d794b3a3SMark Johnstondoes not perform any pre-allocation by itself. 448d794b3a3SMark Johnston.Pp 449d794b3a3SMark JohnstonThe 450d794b3a3SMark Johnston.Fn uma_zone_reserve_kva 451d794b3a3SMark Johnstonfunction pre-allocates kernel virtual address space for the requested 452d794b3a3SMark Johnstonnumber of items. 453d794b3a3SMark JohnstonSubsequent allocations from the zone will be satisfied using the pre-allocated 454d794b3a3SMark Johnstonaddress space. 455d794b3a3SMark JohnstonNote that unlike 456d794b3a3SMark Johnston.Fn uma_zone_reserve , 457d794b3a3SMark Johnston.Fn uma_zone_reserve_kva 458d794b3a3SMark Johnstondoes not restrict the use of the pre-allocation to 459d794b3a3SMark Johnston.Dv M_USE_RESERVE 460d794b3a3SMark Johnstonrequests. 461d794b3a3SMark Johnston.Pp 462d794b3a3SMark JohnstonThe 46308cfa56eSMark Johnston.Fn uma_reclaim 46408cfa56eSMark Johnstonand 46508cfa56eSMark Johnston.Fn uma_zone_reclaim 46608cfa56eSMark Johnstonfunctions reclaim cached items from UMA zones, releasing unused memory. 46708cfa56eSMark JohnstonThe 46808cfa56eSMark Johnston.Fn uma_reclaim 46908cfa56eSMark Johnstonfunction reclaims items from all regular zones, while 47008cfa56eSMark Johnston.Fn uma_zone_reclaim 47108cfa56eSMark Johnstonreclaims items only from the specified zone. 47208cfa56eSMark JohnstonThe 47308cfa56eSMark Johnston.Fa req 47408cfa56eSMark Johnstonparameter must be one of three values which specify how aggressively 47508cfa56eSMark Johnstonitems are to be reclaimed: 47608cfa56eSMark Johnston.Bl -tag -width indent 47708cfa56eSMark Johnston.It Dv UMA_RECLAIM_TRIM 47808cfa56eSMark JohnstonReclaim items only in excess of the zone's estimated working set size. 47908cfa56eSMark JohnstonThe working set size is periodically updated and tracks the recent history 48008cfa56eSMark Johnstonof the zone's usage. 48108cfa56eSMark Johnston.It Dv UMA_RECLAIM_DRAIN 48208cfa56eSMark JohnstonReclaim all items from the unbounded cache. 48308cfa56eSMark JohnstonFree items in the per-CPU caches are left alone. 48408cfa56eSMark Johnston.It Dv UMA_RECLAIM_DRAIN_CPU 48508cfa56eSMark JohnstonReclaim all cached items. 48608cfa56eSMark Johnston.El 487aabe13f1SMark JohnstonThe 488aabe13f1SMark Johnston.Fn uma_reclaim_domain 489aabe13f1SMark Johnstonand 490aabe13f1SMark Johnston.Fn uma_zone_reclaim_domain 491aabe13f1SMark Johnstonfunctions apply only to items allocated from the specified domain. 492aabe13f1SMark JohnstonIn the case of domains using a round-robin NUMA policy, cached items from all 493aabe13f1SMark Johnstondomains are freed to the keg, but only slabs from the specific domain will 494aabe13f1SMark Johnstonbe freed. 49508cfa56eSMark Johnston.Pp 49608cfa56eSMark JohnstonThe 497d794b3a3SMark Johnston.Fn uma_zone_set_allocf 498d794b3a3SMark Johnstonand 499d794b3a3SMark Johnston.Fn uma_zone_set_freef 500d794b3a3SMark Johnstonfunctions allow a zone's default slab allocation and free functions to be 501d794b3a3SMark Johnstonoverridden. 502ec0d8280SRyan LibbyThis is useful if memory with special constraints such as attributes, 503ec0d8280SRyan Libbyalignment, or address ranges must be used. 504d7bbb21aSHartmut Brandt.Pp 505c4ae7908SLawrence StewartThe 506d7bbb21aSHartmut Brandt.Fn uma_zone_set_max 507c4ae7908SLawrence Stewartfunction limits the number of items 508c4ae7908SLawrence Stewart.Pq and therefore memory 509c4ae7908SLawrence Stewartthat can be allocated to 510c4ae7908SLawrence Stewart.Fa zone . 511d7bbb21aSHartmut BrandtThe 512d7bbb21aSHartmut Brandt.Fa nitems 513c4ae7908SLawrence Stewartargument specifies the requested upper limit number of items. 5141c6cae97SLawrence StewartThe effective limit is returned to the caller, as it may end up being higher 5151c6cae97SLawrence Stewartthan requested due to the implementation rounding up to ensure all memory pages 5161c6cae97SLawrence Stewartallocated to the zone are utilised to capacity. 517c4ae7908SLawrence StewartThe limit applies to the total number of items in the zone, which includes 518d7bbb21aSHartmut Brandtallocated items, free items and free items in the per-cpu caches. 519d7bbb21aSHartmut BrandtOn systems with more than one CPU it may not be possible to allocate 520d7bbb21aSHartmut Brandtthe specified number of items even when there is no shortage of memory, 521d7bbb21aSHartmut Brandtbecause all of the remaining free items may be in the caches of the 522d7bbb21aSHartmut Brandtother CPUs when the limit is hit. 523c4ae7908SLawrence Stewart.Pp 524c4ae7908SLawrence StewartThe 525d794b3a3SMark Johnston.Fn uma_zone_set_maxcache 526003cf08bSMark Johnstonfunction limits the number of free items which may be cached in the zone. 527003cf08bSMark JohnstonThis limit applies to both the per-CPU caches and the cache of free buckets. 528d794b3a3SMark Johnston.Pp 529d794b3a3SMark JohnstonThe 530c4ae7908SLawrence Stewart.Fn uma_zone_get_max 531c4ae7908SLawrence Stewartfunction returns the effective upper limit number of items for a zone. 532c4ae7908SLawrence Stewart.Pp 533c4ae7908SLawrence StewartThe 534c4ae7908SLawrence Stewart.Fn uma_zone_get_cur 535d794b3a3SMark Johnstonfunction returns an approximation of the number of items currently allocated 536d794b3a3SMark Johnstonfrom the zone. 537c4ae7908SLawrence StewartThe returned value is approximate because appropriate synchronisation to 5383102cfe2SGlen Barberdetermine an exact value is not performed by the implementation. 539c4ae7908SLawrence StewartThis ensures low overhead at the expense of potentially stale data being used 540c4ae7908SLawrence Stewartin the calculation. 5412f891cd5SPawel Jakub Dawidek.Pp 5422f891cd5SPawel Jakub DawidekThe 5432f891cd5SPawel Jakub Dawidek.Fn uma_zone_set_warning 5442f891cd5SPawel Jakub Dawidekfunction sets a warning that will be printed on the system console when the 5452f891cd5SPawel Jakub Dawidekgiven zone becomes full and fails to allocate an item. 54654503a13SJonathan T. LooneyThe warning will be printed no more often than every five minutes. 5472f891cd5SPawel Jakub DawidekWarnings can be turned off globally by setting the 5482f891cd5SPawel Jakub Dawidek.Va vm.zone_warnings 5492f891cd5SPawel Jakub Dawideksysctl tunable to 5502f891cd5SPawel Jakub Dawidek.Va 0 . 5510a5a3ccbSGleb Smirnoff.Pp 5520a5a3ccbSGleb SmirnoffThe 55354503a13SJonathan T. Looney.Fn uma_zone_set_maxaction 55454503a13SJonathan T. Looneyfunction sets a function that will be called when the given zone becomes full 55554503a13SJonathan T. Looneyand fails to allocate an item. 5567851d429SEdward Tomasz NapieralaThe function will be called with the zone locked. 5577851d429SEdward Tomasz NapieralaAlso, the function 5587851d429SEdward Tomasz Napieralathat called the allocation function may have held additional locks. 5597851d429SEdward Tomasz NapieralaTherefore, 56054503a13SJonathan T. Looneythis function should do very little work (similar to a signal handler). 56154503a13SJonathan T. Looney.Pp 56254503a13SJonathan T. LooneyThe 5630de03c30SMark Johnston.Fn uma_zone_set_smr 5640de03c30SMark Johnstonfunction associates an existing 5650de03c30SMark Johnston.Xr smr 9 5660de03c30SMark Johnstonstructure with a UMA zone. 5670de03c30SMark JohnstonThe effect is similar to creating a zone with the 5680de03c30SMark Johnston.Dv UMA_ZONE_SMR 5690de03c30SMark Johnstonflag, except that a new SMR structure is not created. 5700de03c30SMark JohnstonThis function must be called before any allocations from the zone are performed. 5710de03c30SMark Johnston.Pp 5720de03c30SMark JohnstonThe 5730a5a3ccbSGleb Smirnoff.Fn SYSCTL_UMA_MAX parent nbr name access zone descr 5740a5a3ccbSGleb Smirnoffmacro declares a static 575d794b3a3SMark Johnston.Xr sysctl 9 5760a5a3ccbSGleb Smirnoffoid that exports the effective upper limit number of items for a zone. 5770a5a3ccbSGleb SmirnoffThe 5780a5a3ccbSGleb Smirnoff.Fa zone 5790a5a3ccbSGleb Smirnoffargument should be a pointer to 5800a5a3ccbSGleb Smirnoff.Vt uma_zone_t . 5810a5a3ccbSGleb SmirnoffA read of the oid returns value obtained through 5820a5a3ccbSGleb Smirnoff.Fn uma_zone_get_max . 5830a5a3ccbSGleb SmirnoffA write to the oid sets new value via 5840a5a3ccbSGleb Smirnoff.Fn uma_zone_set_max . 5850a5a3ccbSGleb SmirnoffThe 5860a5a3ccbSGleb Smirnoff.Fn SYSCTL_ADD_UMA_MAX ctx parent nbr name access zone descr 5870a5a3ccbSGleb Smirnoffmacro is provided to create this type of oid dynamically. 5880a5a3ccbSGleb Smirnoff.Pp 5890a5a3ccbSGleb SmirnoffThe 5900a5a3ccbSGleb Smirnoff.Fn SYSCTL_UMA_CUR parent nbr name access zone descr 59162cb31dcSWarner Loshmacro declares a static read-only 592d794b3a3SMark Johnston.Xr sysctl 9 5930a5a3ccbSGleb Smirnoffoid that exports the approximate current occupancy of the zone. 5940a5a3ccbSGleb SmirnoffThe 5950a5a3ccbSGleb Smirnoff.Fa zone 5960a5a3ccbSGleb Smirnoffargument should be a pointer to 5970a5a3ccbSGleb Smirnoff.Vt uma_zone_t . 5980a5a3ccbSGleb SmirnoffA read of the oid returns value obtained through 5990a5a3ccbSGleb Smirnoff.Fn uma_zone_get_cur . 6000a5a3ccbSGleb SmirnoffThe 6010a5a3ccbSGleb Smirnoff.Fn SYSCTL_ADD_UMA_CUR ctx parent nbr name zone descr 6020a5a3ccbSGleb Smirnoffmacro is provided to create this type of oid dynamically. 6030766f278SJonathan T. Looney.Sh IMPLEMENTATION NOTES 6040766f278SJonathan T. LooneyThe memory that these allocation calls return is not executable. 6050766f278SJonathan T. LooneyThe 6060766f278SJonathan T. Looney.Fn uma_zalloc 6070766f278SJonathan T. Looneyfunction does not support the 6080766f278SJonathan T. Looney.Dv M_EXEC 6090766f278SJonathan T. Looneyflag to allocate executable memory. 6100766f278SJonathan T. LooneyNot all platforms enforce a distinction between executable and 6110766f278SJonathan T. Looneynon-executable memory. 6120aa028ffSDag-Erling Smørgrav.Sh SEE ALSO 613d794b3a3SMark Johnston.Xr numa 4 , 614d794b3a3SMark Johnston.Xr vmstat 8 , 6150de03c30SMark Johnston.Xr malloc 9 , 6160de03c30SMark Johnston.Xr smr 9 617d794b3a3SMark Johnston.Rs 618d794b3a3SMark Johnston.%A Jeff Bonwick 619d794b3a3SMark Johnston.%T "The Slab Allocator: An Object-Caching Kernel Memory Allocator" 620d794b3a3SMark Johnston.%D 1994 621d794b3a3SMark Johnston.Re 6220aa028ffSDag-Erling Smørgrav.Sh HISTORY 6230aa028ffSDag-Erling SmørgravThe zone allocator first appeared in 6240aa028ffSDag-Erling Smørgrav.Fx 3.0 . 6259a795583SJeroen Ruigrok van der WervenIt was radically changed in 6269a795583SJeroen Ruigrok van der Werven.Fx 5.0 6279a795583SJeroen Ruigrok van der Wervento function as a slab allocator. 6280aa028ffSDag-Erling Smørgrav.Sh AUTHORS 6290aa028ffSDag-Erling Smørgrav.An -nosplit 6300aa028ffSDag-Erling SmørgravThe zone allocator was written by 6310aa028ffSDag-Erling Smørgrav.An John S. Dyson . 6329a795583SJeroen Ruigrok van der WervenThe zone allocator was rewritten in large parts by 6338a7314fcSBaptiste Daroussin.An Jeff Roberson Aq Mt jeff@FreeBSD.org 6349a795583SJeroen Ruigrok van der Wervento function as a slab allocator. 6350aa028ffSDag-Erling Smørgrav.Pp 6360aa028ffSDag-Erling SmørgravThis manual page was written by 6378a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . 6389a795583SJeroen Ruigrok van der WervenChanges for UMA by 6398a7314fcSBaptiste Daroussin.An Jeroen Ruigrok van der Werven Aq Mt asmodai@FreeBSD.org . 640