xref: /freebsd/share/man/man9/zone.9 (revision 003cf08ba98387092792bd4dd880807620ea5bbf)
10aa028ffSDag-Erling Smørgrav.\"-
26e18fca1SUlrich Spörlein.\" Copyright (c) 2001 Dag-Erling Coïdan 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.\"
260aa028ffSDag-Erling Smørgrav.\" $FreeBSD$
270aa028ffSDag-Erling Smørgrav.\"
28*003cf08bSMark Johnston.Dd November 22, 2019
29d794b3a3SMark Johnston.Dt UMA 9
300aa028ffSDag-Erling Smørgrav.Os
310aa028ffSDag-Erling Smørgrav.Sh NAME
32d794b3a3SMark Johnston.Nm UMA
33d794b3a3SMark Johnston.Nd general-purpose kernel object allocator
340aa028ffSDag-Erling Smørgrav.Sh SYNOPSIS
3532eef9aeSRuslan Ermilov.In sys/param.h
36f16b3c0dSChad David.In sys/queue.h
373a347a6eSJeroen Ruigrok van der Werven.In vm/uma.h
38d794b3a3SMark Johnston.Cd "options UMA_FIRSTTOUCH"
39d794b3a3SMark Johnston.Cd "options UMA_XDOMAIN"
40d794b3a3SMark Johnston.Bd -literal
41d794b3a3SMark Johnstontypedef int (*uma_ctor)(void *mem, int size, void *arg, int flags);
42d794b3a3SMark Johnstontypedef void (*uma_dtor)(void *mem, int size, void *arg);
43d794b3a3SMark Johnstontypedef int (*uma_init)(void *mem, int size, int flags);
44d794b3a3SMark Johnstontypedef void (*uma_fini)(void *mem, int size);
45d794b3a3SMark Johnstontypedef int (*uma_import)(void *arg, void **store, int count, int domain,
46d794b3a3SMark Johnston    int flags);
47d794b3a3SMark Johnstontypedef void (*uma_release)(void *arg, void **store, int count);
48d794b3a3SMark Johnstontypedef void *(*uma_alloc)(uma_zone_t zone, vm_size_t size, int domain,
49d794b3a3SMark Johnston    uint8_t *pflag, int wait);
50d794b3a3SMark Johnstontypedef void (*uma_free)(void *item, vm_size_t size, uint8_t pflag);
51d794b3a3SMark Johnston
52d794b3a3SMark Johnston.Ed
53382682b2SJeroen Ruigrok van der Werven.Ft uma_zone_t
549be1948fSRuslan Ermilov.Fo uma_zcreate
559be1948fSRuslan Ermilov.Fa "char *name" "int size"
56d794b3a3SMark Johnston.Fa "uma_ctor ctor" "uma_dtor dtor" "uma_init zinit" "uma_fini zfini"
576b99842aSEd Schouten.Fa "int align" "uint16_t flags"
589be1948fSRuslan Ermilov.Fc
59d794b3a3SMark Johnston.Ft uma_zone_t
60d794b3a3SMark Johnston.Fo uma_zcache_create
61d794b3a3SMark Johnston.Fa "char *name" "int size"
62d794b3a3SMark Johnston.Fa "uma_ctor ctor" "uma_dtor dtor" "uma_init zinit" "uma_fini zfini"
63d794b3a3SMark Johnston.Fa "uma_import zimport" "uma_release zrelease"
64d794b3a3SMark Johnston.Fa "void *arg" "int flags"
65d794b3a3SMark Johnston.Fc
66d794b3a3SMark Johnston.Ft uma_zone_t
67d794b3a3SMark Johnston.Fo uma_zsecond_create
68d794b3a3SMark Johnston.Fa "char *name"
69d794b3a3SMark Johnston.Fa "uma_ctor ctor" "uma_dtor dtor" "uma_init zinit" "uma_fini zfini"
70d794b3a3SMark Johnston.Fa "uma_zone_t master"
71d794b3a3SMark Johnston.Fc
72d794b3a3SMark Johnston.Ft void
73d794b3a3SMark Johnston.Fn uma_zdestroy "uma_zone_t zone"
749be1948fSRuslan Ermilov.Ft "void *"
759be1948fSRuslan Ermilov.Fn uma_zalloc "uma_zone_t zone" "int flags"
766a4c41d1SRemko Lodder.Ft "void *"
776a4c41d1SRemko Lodder.Fn uma_zalloc_arg "uma_zone_t zone" "void *arg" "int flags"
7893f31533SJeff Roberson.Ft "void *"
7993f31533SJeff Roberson.Fn uma_zalloc_domain "uma_zone_t zone" "void *arg" "int domain" "int flags"
80d794b3a3SMark Johnston.Ft "void *"
81d794b3a3SMark Johnston.Fn uma_zalloc_pcpu "uma_zone_t zone" "int flags"
82d794b3a3SMark Johnston.Ft "void *"
83d794b3a3SMark Johnston.Fn uma_zalloc_pcpu_arg "uma_zone_t zone" "void *arg" "int flags"
840aa028ffSDag-Erling Smørgrav.Ft void
85f1de92e6SJeroen Ruigrok van der Werven.Fn uma_zfree "uma_zone_t zone" "void *item"
865ca0c84eSThomas Moestl.Ft void
876a4c41d1SRemko Lodder.Fn uma_zfree_arg "uma_zone_t zone" "void *item" "void *arg"
886a4c41d1SRemko Lodder.Ft void
8993f31533SJeff Roberson.Fn uma_zfree_domain "uma_zone_t zone" "void *item" "void *arg"
9093f31533SJeff Roberson.Ft void
91d794b3a3SMark Johnston.Fn uma_zfree_pcpu "uma_zone_t zone" "void *item"
92d794b3a3SMark Johnston.Ft void
93d794b3a3SMark Johnston.Fn uma_zfree_pcpu_arg "uma_zone_t zone" "void *item" "void *arg"
94d794b3a3SMark Johnston.Ft void
95d794b3a3SMark Johnston.Fn uma_prealloc "uma_zone_t zone" "int nitems"
96d794b3a3SMark Johnston.Ft void
97d794b3a3SMark Johnston.Fn uma_zone_reserve "uma_zone_t zone" "int nitems"
98d794b3a3SMark Johnston.Ft void
99d794b3a3SMark Johnston.Fn uma_zone_reserve_kva "uma_zone_t zone" "int nitems"
100d794b3a3SMark Johnston.Ft void
10108cfa56eSMark Johnston.Fn uma_reclaim "int req"
10208cfa56eSMark Johnston.Ft void
10308cfa56eSMark Johnston.Fn uma_zone_reclaim "uma_zone_t zone" "int req"
10408cfa56eSMark Johnston.Ft void
105d794b3a3SMark Johnston.Fn uma_zone_set_allocf "uma_zone_t zone" "uma_alloc allocf"
106d794b3a3SMark Johnston.Ft void
107d794b3a3SMark Johnston.Fn uma_zone_set_freef "uma_zone_t zone" "uma_free freef"
1081c6cae97SLawrence Stewart.Ft int
109d7bbb21aSHartmut Brandt.Fn uma_zone_set_max "uma_zone_t zone" "int nitems"
110*003cf08bSMark Johnston.Ft void
111d794b3a3SMark Johnston.Fn uma_zone_set_maxcache "uma_zone_t zone" "int nitems"
112d794b3a3SMark Johnston.Ft int
113c4ae7908SLawrence Stewart.Fn uma_zone_get_max "uma_zone_t zone"
114c4ae7908SLawrence Stewart.Ft int
115c4ae7908SLawrence Stewart.Fn uma_zone_get_cur "uma_zone_t zone"
1162f891cd5SPawel Jakub Dawidek.Ft void
1172f891cd5SPawel Jakub Dawidek.Fn uma_zone_set_warning "uma_zone_t zone" "const char *warning"
11854503a13SJonathan T. Looney.Ft void
11954503a13SJonathan T. Looney.Fn uma_zone_set_maxaction "uma_zone_t zone" "void (*maxaction)(uma_zone_t)"
120d794b3a3SMark Johnston.Ft void
121d794b3a3SMark Johnston.Fn uma_reclaim
1220a5a3ccbSGleb Smirnoff.In sys/sysctl.h
1230a5a3ccbSGleb Smirnoff.Fn SYSCTL_UMA_MAX parent nbr name access zone descr
1240a5a3ccbSGleb Smirnoff.Fn SYSCTL_ADD_UMA_MAX ctx parent nbr name access zone descr
1250a5a3ccbSGleb Smirnoff.Fn SYSCTL_UMA_CUR parent nbr name access zone descr
1260a5a3ccbSGleb Smirnoff.Fn SYSCTL_ADD_UMA_CUR ctx parent nbr name access zone descr
1270aa028ffSDag-Erling Smørgrav.Sh DESCRIPTION
128d794b3a3SMark JohnstonUMA (Universal Memory Allocator) provides an efficient interface for managing
129d794b3a3SMark Johnstondynamically-sized collections of items of identical size, referred to as zones.
130d794b3a3SMark JohnstonZones keep track of which items are in use and which
131d794b3a3SMark Johnstonare not, and UMA provides functions for allocating items from a zone and
132d794b3a3SMark Johnstonfor releasing them back, making them available for subsequent allocation requests.
133d794b3a3SMark JohnstonZones maintain per-CPU caches with linear scalability on SMP
13493f31533SJeff Robersonsystems as well as round-robin and first-touch policies for NUMA
13593f31533SJeff Robersonsystems.
136d794b3a3SMark JohnstonThe number of items cached per CPU is bounded, and each zone additionally
137d794b3a3SMark Johnstonmaintains an unbounded cache of items that is used to quickly satisfy
138d794b3a3SMark Johnstonper-CPU cache allocation misses.
1390aa028ffSDag-Erling Smørgrav.Pp
140d794b3a3SMark JohnstonTwo types of zones exist: regular zones and cache zones.
141d794b3a3SMark JohnstonIn a regular zone, items are allocated from a slab, which is one or more
142d794b3a3SMark Johnstonvirtually contiguous memory pages that have been allocated from the kernel's
143d794b3a3SMark Johnstonpage allocator.
144d794b3a3SMark JohnstonInternally, slabs are managed by a UMA keg, which is responsible for allocating
145d794b3a3SMark Johnstonslabs and keeping track of their usage by one or more zones.
146d794b3a3SMark JohnstonIn typical usage, there is one keg per zone, so slabs are not shared among
147d794b3a3SMark Johnstonmultiple zones.
1480aa028ffSDag-Erling Smørgrav.Pp
149d794b3a3SMark JohnstonNormal zones import items from a keg, and release items back to that keg if
150d794b3a3SMark Johnstonrequested.
151d794b3a3SMark JohnstonCache zones do not have a keg, and instead use custom import and release
152d794b3a3SMark Johnstonmethods.
153d794b3a3SMark JohnstonFor example, some collections of kernel objects are statically allocated
154d794b3a3SMark Johnstonat boot-time, and the size of the collection does not change.
155d794b3a3SMark JohnstonA cache zone can be used to implement an efficient allocator for the objects in
156d794b3a3SMark Johnstonsuch a collection.
15790d83886SJulian Elischer.Pp
158382682b2SJeroen Ruigrok van der WervenThe
159382682b2SJeroen Ruigrok van der Werven.Fn uma_zcreate
160d794b3a3SMark Johnstonand
161d794b3a3SMark Johnston.Fn uma_zcache_create
162d794b3a3SMark Johnstonfunctions create a new regular zone and cache zone, respectively.
163d794b3a3SMark JohnstonThe
164d794b3a3SMark Johnston.Fn uma_zsecond_create
165d794b3a3SMark Johnstonfunction creates a regular zone which shares the keg of the zone
166d794b3a3SMark Johnstonspecified by the
167d794b3a3SMark Johnston.Fa master
168d794b3a3SMark Johnstonargument.
169382682b2SJeroen Ruigrok van der WervenThe
170382682b2SJeroen Ruigrok van der Werven.Fa name
171382682b2SJeroen Ruigrok van der Wervenargument is a text name of the zone for debugging and stats; this memory
172382682b2SJeroen Ruigrok van der Wervenshould not be freed until the zone has been deallocated.
173382682b2SJeroen Ruigrok van der Werven.Pp
1744919e8cbSAlfred PerlsteinThe
1754919e8cbSAlfred Perlstein.Fa ctor
1764919e8cbSAlfred Perlsteinand
1774919e8cbSAlfred Perlstein.Fa dtor
1784919e8cbSAlfred Perlsteinarguments are callback functions that are called by
179d794b3a3SMark Johnstonthe UMA subsystem at the time of the call to
1804919e8cbSAlfred Perlstein.Fn uma_zalloc
1814919e8cbSAlfred Perlsteinand
1824919e8cbSAlfred Perlstein.Fn uma_zfree
183d962d52aSRuslan Ermilovrespectively.
184d962d52aSRuslan ErmilovTheir purpose is to provide hooks for initializing or
1854919e8cbSAlfred Perlsteindestroying things that need to be done at the time of the allocation
186d962d52aSRuslan Ermilovor release of a resource.
187e07f1d5fSHiten PandyaA good usage for the
1884919e8cbSAlfred Perlstein.Fa ctor
1894919e8cbSAlfred Perlsteinand
1904919e8cbSAlfred Perlstein.Fa dtor
191d794b3a3SMark Johnstoncallbacks might be to initialize a data structure embedded in the item,
192d794b3a3SMark Johnstonsuch as a
193d794b3a3SMark Johnston.Xr queue 3
194d794b3a3SMark Johnstonhead.
1954919e8cbSAlfred Perlstein.Pp
1964919e8cbSAlfred PerlsteinThe
197d794b3a3SMark Johnston.Fa zinit
1984919e8cbSAlfred Perlsteinand
199d794b3a3SMark Johnston.Fa zfini
200d794b3a3SMark Johnstonarguments are used to optimize the allocation of items from the zone.
201d794b3a3SMark JohnstonThey are called by the UMA subsystem whenever
202d794b3a3SMark Johnstonit needs to allocate or free items to satisfy requests or memory pressure.
203d962d52aSRuslan ErmilovA good use for the
204d794b3a3SMark Johnston.Fa zinit
205d962d52aSRuslan Ermilovand
206d794b3a3SMark Johnston.Fa zfini
207d962d52aSRuslan Ermilovcallbacks might be to
208d794b3a3SMark Johnstoninitialize and destroy a mutex contained within an item.
209d794b3a3SMark JohnstonThis would allow one to avoid destroying and re-initializing the mutex
210d794b3a3SMark Johnstoneach time the item is freed and re-allocated.
211d962d52aSRuslan ErmilovThey are not called on each call to
2124919e8cbSAlfred Perlstein.Fn uma_zalloc
2134919e8cbSAlfred Perlsteinand
2144919e8cbSAlfred Perlstein.Fn uma_zfree
215d794b3a3SMark Johnstonbut rather when an item is imported into a zone's cache, and when a zone
216d794b3a3SMark Johnstonreleases an item to the slab allocator, typically as a response to memory
217d794b3a3SMark Johnstonpressure.
218d794b3a3SMark Johnston.Pp
219d794b3a3SMark JohnstonFor
220d794b3a3SMark Johnston.Fn uma_zcache_create ,
221d794b3a3SMark Johnstonthe
222d794b3a3SMark Johnston.Fa zimport
223d794b3a3SMark Johnstonand
224d794b3a3SMark Johnston.Fa zrelease
225d794b3a3SMark Johnstonfunctions are called to import items into the zone and to release items
226d794b3a3SMark Johnstonfrom the zone, respectively.
227d794b3a3SMark JohnstonThe
228d794b3a3SMark Johnston.Fa zimport
229d794b3a3SMark Johnstonfunction should store pointers to items in the
230d794b3a3SMark Johnston.Fa store
231d794b3a3SMark Johnstonarray, which contains a maximum of
232d794b3a3SMark Johnston.Fa count
233d794b3a3SMark Johnstonentries.
234d794b3a3SMark JohnstonThe function must return the number of imported items, which may be less than
235d794b3a3SMark Johnstonthe maximum.
236d794b3a3SMark JohnstonSimilarly, the
237d794b3a3SMark Johnston.Fa store
238d794b3a3SMark Johnstonparameter to the
239d794b3a3SMark Johnston.Fa zrelease
240d794b3a3SMark Johnstonfunction contains an array of
241d794b3a3SMark Johnston.Fa count
242d794b3a3SMark Johnstonpointers to items.
243d794b3a3SMark JohnstonThe
244d794b3a3SMark Johnston.Fa arg
245d794b3a3SMark Johnstonparameter passed to
246d794b3a3SMark Johnston.Fn uma_zcache_create
247d794b3a3SMark Johnstonis provided to the import and release functions.
248d794b3a3SMark JohnstonThe
249d794b3a3SMark Johnston.Fa domain
250d794b3a3SMark Johnstonparameter to
251d794b3a3SMark Johnston.Fa zimport
252d794b3a3SMark Johnstonspecifies the requested
253d794b3a3SMark Johnston.Xr numa 4
254d794b3a3SMark Johnstondomain for the allocation.
255d794b3a3SMark JohnstonIt is either a NUMA domain number or the special value
256d794b3a3SMark Johnston.Dv UMA_ANYDOMAIN .
2574919e8cbSAlfred Perlstein.Pp
258b7e2b86cSGleb SmirnoffThe
259b7e2b86cSGleb Smirnoff.Fa flags
260d794b3a3SMark Johnstonargument of
261b7e2b86cSGleb Smirnoff.Fn uma_zcreate
262d794b3a3SMark Johnstonand
263d794b3a3SMark Johnston.Fn uma_zcache_create
264b7e2b86cSGleb Smirnoffis a subset of the following flags:
265b7e2b86cSGleb Smirnoff.Bl -tag -width "foo"
266b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_NOFREE
267d794b3a3SMark JohnstonSlabs allocated to the zone's keg are never freed.
268b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_NODUMP
269d794b3a3SMark JohnstonPages belonging to the zone will not be included in minidumps.
270ad97af7eSGleb Smirnoff.It Dv UMA_ZONE_PCPU
271ad97af7eSGleb SmirnoffAn allocation from zone would have
272ad97af7eSGleb Smirnoff.Va mp_ncpu
273ad97af7eSGleb Smirnoffshadow copies, that are privately assigned to CPUs.
274d794b3a3SMark JohnstonA CPU can address its private copy using base the allocation address plus
275d794b3a3SMark Johnstona multiple of the current CPU ID and
276ad97af7eSGleb Smirnoff.Fn sizeof "struct pcpu" :
277ad97af7eSGleb Smirnoff.Bd -literal -offset indent
278ad97af7eSGleb Smirnofffoo_zone = uma_zcreate(..., UMA_ZONE_PCPU);
279ad97af7eSGleb Smirnoff ...
280ad97af7eSGleb Smirnofffoo_base = uma_zalloc(foo_zone, ...);
281ad97af7eSGleb Smirnoff ...
282ad97af7eSGleb Smirnoffcritical_enter();
283ad97af7eSGleb Smirnofffoo_pcpu = (foo_t *)zpcpu_get(foo_base);
284ad97af7eSGleb Smirnoff/* do something with foo_pcpu */
285ad97af7eSGleb Smirnoffcritical_exit();
286d794b3a3SMark Johnston
287ad97af7eSGleb Smirnoff.Ed
288d794b3a3SMark JohnstonNote that
289d794b3a3SMark Johnston.Dv M_ZERO
290d794b3a3SMark Johnstoncannot be used when allocating items from a PCPU zone.
291d794b3a3SMark JohnstonTo obtain zeroed memory from a PCPU zone, use the
292d794b3a3SMark Johnston.Fn uma_zalloc_pcpu
293d794b3a3SMark Johnstonfunction and its variants instead, and pass
294d794b3a3SMark Johnston.Dv M_ZERO .
295b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_OFFPAGE
296b7e2b86cSGleb SmirnoffBy default book-keeping of items within a slab is done in the slab page itself.
297b7e2b86cSGleb SmirnoffThis flag explicitly tells subsystem that book-keeping structure should be
298b7e2b86cSGleb Smirnoffallocated separately from special internal zone.
299b7e2b86cSGleb SmirnoffThis flag requires either
300b7e2b86cSGleb Smirnoff.Dv UMA_ZONE_VTOSLAB
301b7e2b86cSGleb Smirnoffor
302b7e2b86cSGleb Smirnoff.Dv UMA_ZONE_HASH ,
303b7e2b86cSGleb Smirnoffsince subsystem requires a mechanism to find a book-keeping structure
3047c64ddd5SWarren Blockto an item being freed.
305b7e2b86cSGleb SmirnoffThe subsystem may choose to prefer offpage book-keeping for certain zones
306b7e2b86cSGleb Smirnoffimplicitly.
307b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_ZINIT
308b7e2b86cSGleb SmirnoffThe zone will have its
309b7e2b86cSGleb Smirnoff.Ft uma_init
310b7e2b86cSGleb Smirnoffmethod set to internal method that initializes a new allocated slab
311b7e2b86cSGleb Smirnoffto all zeros.
312b1edef17SJoel DahlDo not mistake
313b7e2b86cSGleb Smirnoff.Ft uma_init
314b7e2b86cSGleb Smirnoffmethod with
315b7e2b86cSGleb Smirnoff.Ft uma_ctor .
316b7e2b86cSGleb SmirnoffA zone with
317b7e2b86cSGleb Smirnoff.Dv UMA_ZONE_ZINIT
318b7e2b86cSGleb Smirnoffflag would not return zeroed memory on every
319b7e2b86cSGleb Smirnoff.Fn uma_zalloc .
320b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_HASH
321b7e2b86cSGleb SmirnoffThe zone should use an internal hash table to find slab book-keeping
322b7e2b86cSGleb Smirnoffstructure where an allocation being freed belongs to.
323b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_VTOSLAB
324b7e2b86cSGleb SmirnoffThe zone should use special field of
325b7e2b86cSGleb Smirnoff.Vt vm_page_t
326b7e2b86cSGleb Smirnoffto find slab book-keeping structure where an allocation being freed belongs to.
327b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_MALLOC
328b7e2b86cSGleb SmirnoffThe zone is for the
329b7e2b86cSGleb Smirnoff.Xr malloc 9
330b7e2b86cSGleb Smirnoffsubsystem.
331b7e2b86cSGleb Smirnoff.It Dv UMA_ZONE_VM
332b7e2b86cSGleb SmirnoffThe zone is for the VM subsystem.
33393f31533SJeff Roberson.It Dv UMA_ZONE_NUMA
33493f31533SJeff RobersonThe zone should use a first-touch NUMA policy rather than the round-robin
335d794b3a3SMark Johnstondefault.
336d794b3a3SMark JohnstonIf the
337d794b3a3SMark Johnston.Dv UMA_FIRSTTOUCH
338d794b3a3SMark Johnstonkernel option is configured, all zones implicitly use a first-touch policy,
339d794b3a3SMark Johnstonand the
340d794b3a3SMark Johnston.Dv UMA_ZONE_NUMA
341d794b3a3SMark Johnstonflag has no effect.
342d794b3a3SMark JohnstonThe
343d794b3a3SMark Johnston.Dv UMA_XDOMAIN
344d794b3a3SMark Johnstonkernel option, when configured, causes UMA to do the extra tracking to ensure
345d794b3a3SMark Johnstonthat allocations from first-touch zones are always local.
346d794b3a3SMark JohnstonOtherwise, consumers that do not free memory on the same domain from which it
347d794b3a3SMark Johnstonwas allocated will cause mixing in per-CPU caches.
348d794b3a3SMark JohnstonSee
349d794b3a3SMark Johnston.Xr numa 4
350d794b3a3SMark Johnstonfor more details.
351b7e2b86cSGleb Smirnoff.El
352b7e2b86cSGleb Smirnoff.Pp
353d794b3a3SMark JohnstonZones can be destroyed using
354d794b3a3SMark Johnston.Fn uma_zdestroy ,
355d794b3a3SMark Johnstonfreeing all memory that is cached in the zone.
356d794b3a3SMark JohnstonAll items allocated from the zone must be freed to the zone before the zone
357d794b3a3SMark Johnstonmay be safely destroyed.
358d794b3a3SMark Johnston.Pp
3590aa028ffSDag-Erling SmørgravTo allocate an item from a zone, simply call
3607c32182eSJeroen Ruigrok van der Werven.Fn uma_zalloc
361d794b3a3SMark Johnstonwith a pointer to that zone and set the
362acff84fbSJeroen Ruigrok van der Werven.Fa flags
363acff84fbSJeroen Ruigrok van der Wervenargument to selected flags as documented in
364acff84fbSJeroen Ruigrok van der Werven.Xr malloc 9 .
365d794b3a3SMark JohnstonIt will return a pointer to an item if successful, or
3660aa028ffSDag-Erling Smørgrav.Dv NULL
3670aa028ffSDag-Erling Smørgravin the rare case where all items in the zone are in use and the
368d794b3a3SMark Johnstonallocator is unable to grow the zone and
3691437c79aSJeroen Ruigrok van der Werven.Dv M_NOWAIT
3701437c79aSJeroen Ruigrok van der Wervenis specified.
3710aa028ffSDag-Erling Smørgrav.Pp
3720aa028ffSDag-Erling SmørgravItems are released back to the zone from which they were allocated by
3730aa028ffSDag-Erling Smørgravcalling
3747c32182eSJeroen Ruigrok van der Werven.Fn uma_zfree
3750aa028ffSDag-Erling Smørgravwith a pointer to the zone and a pointer to the item.
37620ed0cb0SMatthew D FlemingIf
37720ed0cb0SMatthew D Fleming.Fa item
37820ed0cb0SMatthew D Flemingis
37920ed0cb0SMatthew D Fleming.Dv NULL ,
38020ed0cb0SMatthew D Flemingthen
38120ed0cb0SMatthew D Fleming.Fn uma_zfree
38220ed0cb0SMatthew D Flemingdoes nothing.
3835ca0c84eSThomas Moestl.Pp
384d794b3a3SMark JohnstonThe variants
3856a4c41d1SRemko Lodder.Fn uma_zalloc_arg
3866a4c41d1SRemko Lodderand
3876a4c41d1SRemko Lodder.Fn uma_zfree_arg
38893f31533SJeff Robersonallow callers to
3896a4c41d1SRemko Lodderspecify an argument for the
3906a4c41d1SRemko Lodder.Dv ctor
3916a4c41d1SRemko Lodderand
3926a4c41d1SRemko Lodder.Dv dtor
393d794b3a3SMark Johnstonfunctions of the zone, respectively.
39493f31533SJeff RobersonThe
39593f31533SJeff Roberson.Fn uma_zalloc_domain
39693f31533SJeff Robersonfunction allows callers to specify a fixed
397d794b3a3SMark Johnston.Xr numa 4
398d794b3a3SMark Johnstondomain to allocate from.
399d794b3a3SMark JohnstonThis uses a guaranteed but slow path in the allocator which reduces
400d794b3a3SMark Johnstonconcurrency.
401d794b3a3SMark JohnstonThe
40293f31533SJeff Roberson.Fn uma_zfree_domain
403d794b3a3SMark Johnstonfunction should be used to return memory allocated in this fashion.
404d794b3a3SMark JohnstonThis function infers the domain from the pointer and does not require it as an
40593f31533SJeff Robersonargument.
4066a4c41d1SRemko Lodder.Pp
407d794b3a3SMark JohnstonThe
408d794b3a3SMark Johnston.Fn uma_zone_prealloc
409d794b3a3SMark Johnstonfunction allocates slabs for the requested number of items, typically following
410d794b3a3SMark Johnstonthe initial creation of a zone.
411d794b3a3SMark JohnstonSubsequent allocations from the zone will be satisfied using the pre-allocated
412d794b3a3SMark Johnstonslabs.
413d794b3a3SMark JohnstonNote that slab allocation is performed with the
414d794b3a3SMark Johnston.Dv M_WAITOK
415d794b3a3SMark Johnstonflag, so
416d794b3a3SMark Johnston.Fn uma_zone_prealloc
417d794b3a3SMark Johnstonmay sleep.
418d794b3a3SMark Johnston.Pp
419d794b3a3SMark JohnstonThe
420d794b3a3SMark Johnston.Fn uma_zone_reserve
421d794b3a3SMark Johnstonfunction sets the number of reserved items for the zone.
4227c32182eSJeroen Ruigrok van der Werven.Fn uma_zalloc
423d794b3a3SMark Johnstonand variants will ensure that the zone contains at least the reserved number
424d794b3a3SMark Johnstonof free items.
425d794b3a3SMark JohnstonReserved items may be allocated by specifying
426d794b3a3SMark Johnston.Dv M_USE_RESERVE
427d794b3a3SMark Johnstonin the allocation request flags.
428d794b3a3SMark Johnston.Fn uma_zone_reserve
429d794b3a3SMark Johnstondoes not perform any pre-allocation by itself.
430d794b3a3SMark Johnston.Pp
431d794b3a3SMark JohnstonThe
432d794b3a3SMark Johnston.Fn uma_zone_reserve_kva
433d794b3a3SMark Johnstonfunction pre-allocates kernel virtual address space for the requested
434d794b3a3SMark Johnstonnumber of items.
435d794b3a3SMark JohnstonSubsequent allocations from the zone will be satisfied using the pre-allocated
436d794b3a3SMark Johnstonaddress space.
437d794b3a3SMark JohnstonNote that unlike
438d794b3a3SMark Johnston.Fn uma_zone_reserve ,
439d794b3a3SMark Johnston.Fn uma_zone_reserve_kva
440d794b3a3SMark Johnstondoes not restrict the use of the pre-allocation to
441d794b3a3SMark Johnston.Dv M_USE_RESERVE
442d794b3a3SMark Johnstonrequests.
443d794b3a3SMark Johnston.Pp
444d794b3a3SMark JohnstonThe
44508cfa56eSMark Johnston.Fn uma_reclaim
44608cfa56eSMark Johnstonand
44708cfa56eSMark Johnston.Fn uma_zone_reclaim
44808cfa56eSMark Johnstonfunctions reclaim cached items from UMA zones, releasing unused memory.
44908cfa56eSMark JohnstonThe
45008cfa56eSMark Johnston.Fn uma_reclaim
45108cfa56eSMark Johnstonfunction reclaims items from all regular zones, while
45208cfa56eSMark Johnston.Fn uma_zone_reclaim
45308cfa56eSMark Johnstonreclaims items only from the specified zone.
45408cfa56eSMark JohnstonThe
45508cfa56eSMark Johnston.Fa req
45608cfa56eSMark Johnstonparameter must be one of three values which specify how aggressively
45708cfa56eSMark Johnstonitems are to be reclaimed:
45808cfa56eSMark Johnston.Bl -tag -width indent
45908cfa56eSMark Johnston.It Dv UMA_RECLAIM_TRIM
46008cfa56eSMark JohnstonReclaim items only in excess of the zone's estimated working set size.
46108cfa56eSMark JohnstonThe working set size is periodically updated and tracks the recent history
46208cfa56eSMark Johnstonof the zone's usage.
46308cfa56eSMark Johnston.It Dv UMA_RECLAIM_DRAIN
46408cfa56eSMark JohnstonReclaim all items from the unbounded cache.
46508cfa56eSMark JohnstonFree items in the per-CPU caches are left alone.
46608cfa56eSMark Johnston.It Dv UMA_RECLAIM_DRAIN_CPU
46708cfa56eSMark JohnstonReclaim all cached items.
46808cfa56eSMark Johnston.El
46908cfa56eSMark Johnston.Pp
47008cfa56eSMark JohnstonThe
471d794b3a3SMark Johnston.Fn uma_zone_set_allocf
472d794b3a3SMark Johnstonand
473d794b3a3SMark Johnston.Fn uma_zone_set_freef
474d794b3a3SMark Johnstonfunctions allow a zone's default slab allocation and free functions to be
475d794b3a3SMark Johnstonoverridden.
476d794b3a3SMark JohnstonThis is useful if the zone's items have special memory allocation constraints.
477d794b3a3SMark JohnstonFor example, if multi-page objects are required to be physically contiguous,
478d794b3a3SMark Johnstonan
479d794b3a3SMark Johnston.Fa allocf
480d794b3a3SMark Johnstonfunction which requests contiguous memory from the kernel's page allocator
481d794b3a3SMark Johnstonmay be used.
482d7bbb21aSHartmut Brandt.Pp
483c4ae7908SLawrence StewartThe
484d7bbb21aSHartmut Brandt.Fn uma_zone_set_max
485c4ae7908SLawrence Stewartfunction limits the number of items
486c4ae7908SLawrence Stewart.Pq and therefore memory
487c4ae7908SLawrence Stewartthat can be allocated to
488c4ae7908SLawrence Stewart.Fa zone .
489d7bbb21aSHartmut BrandtThe
490d7bbb21aSHartmut Brandt.Fa nitems
491c4ae7908SLawrence Stewartargument specifies the requested upper limit number of items.
4921c6cae97SLawrence StewartThe effective limit is returned to the caller, as it may end up being higher
4931c6cae97SLawrence Stewartthan requested due to the implementation rounding up to ensure all memory pages
4941c6cae97SLawrence Stewartallocated to the zone are utilised to capacity.
495c4ae7908SLawrence StewartThe limit applies to the total number of items in the zone, which includes
496d7bbb21aSHartmut Brandtallocated items, free items and free items in the per-cpu caches.
497d7bbb21aSHartmut BrandtOn systems with more than one CPU it may not be possible to allocate
498d7bbb21aSHartmut Brandtthe specified number of items even when there is no shortage of memory,
499d7bbb21aSHartmut Brandtbecause all of the remaining free items may be in the caches of the
500d7bbb21aSHartmut Brandtother CPUs when the limit is hit.
501c4ae7908SLawrence Stewart.Pp
502c4ae7908SLawrence StewartThe
503d794b3a3SMark Johnston.Fn uma_zone_set_maxcache
504*003cf08bSMark Johnstonfunction limits the number of free items which may be cached in the zone.
505*003cf08bSMark JohnstonThis limit applies to both the per-CPU caches and the cache of free buckets.
506d794b3a3SMark Johnston.Pp
507d794b3a3SMark JohnstonThe
508c4ae7908SLawrence Stewart.Fn uma_zone_get_max
509c4ae7908SLawrence Stewartfunction returns the effective upper limit number of items for a zone.
510c4ae7908SLawrence Stewart.Pp
511c4ae7908SLawrence StewartThe
512c4ae7908SLawrence Stewart.Fn uma_zone_get_cur
513d794b3a3SMark Johnstonfunction returns an approximation of the number of items currently allocated
514d794b3a3SMark Johnstonfrom the zone.
515c4ae7908SLawrence StewartThe returned value is approximate because appropriate synchronisation to
5163102cfe2SGlen Barberdetermine an exact value is not performed by the implementation.
517c4ae7908SLawrence StewartThis ensures low overhead at the expense of potentially stale data being used
518c4ae7908SLawrence Stewartin the calculation.
5192f891cd5SPawel Jakub Dawidek.Pp
5202f891cd5SPawel Jakub DawidekThe
5212f891cd5SPawel Jakub Dawidek.Fn uma_zone_set_warning
5222f891cd5SPawel Jakub Dawidekfunction sets a warning that will be printed on the system console when the
5232f891cd5SPawel Jakub Dawidekgiven zone becomes full and fails to allocate an item.
52454503a13SJonathan T. LooneyThe warning will be printed no more often than every five minutes.
5252f891cd5SPawel Jakub DawidekWarnings can be turned off globally by setting the
5262f891cd5SPawel Jakub Dawidek.Va vm.zone_warnings
5272f891cd5SPawel Jakub Dawideksysctl tunable to
5282f891cd5SPawel Jakub Dawidek.Va 0 .
5290a5a3ccbSGleb Smirnoff.Pp
5300a5a3ccbSGleb SmirnoffThe
53154503a13SJonathan T. Looney.Fn uma_zone_set_maxaction
53254503a13SJonathan T. Looneyfunction sets a function that will be called when the given zone becomes full
53354503a13SJonathan T. Looneyand fails to allocate an item.
5347851d429SEdward Tomasz NapieralaThe function will be called with the zone locked.
5357851d429SEdward Tomasz NapieralaAlso, the function
5367851d429SEdward Tomasz Napieralathat called the allocation function may have held additional locks.
5377851d429SEdward Tomasz NapieralaTherefore,
53854503a13SJonathan T. Looneythis function should do very little work (similar to a signal handler).
53954503a13SJonathan T. Looney.Pp
54054503a13SJonathan T. LooneyThe
5410a5a3ccbSGleb Smirnoff.Fn SYSCTL_UMA_MAX parent nbr name access zone descr
5420a5a3ccbSGleb Smirnoffmacro declares a static
543d794b3a3SMark Johnston.Xr sysctl 9
5440a5a3ccbSGleb Smirnoffoid that exports the effective upper limit number of items for a zone.
5450a5a3ccbSGleb SmirnoffThe
5460a5a3ccbSGleb Smirnoff.Fa zone
5470a5a3ccbSGleb Smirnoffargument should be a pointer to
5480a5a3ccbSGleb Smirnoff.Vt uma_zone_t .
5490a5a3ccbSGleb SmirnoffA read of the oid returns value obtained through
5500a5a3ccbSGleb Smirnoff.Fn uma_zone_get_max .
5510a5a3ccbSGleb SmirnoffA write to the oid sets new value via
5520a5a3ccbSGleb Smirnoff.Fn uma_zone_set_max .
5530a5a3ccbSGleb SmirnoffThe
5540a5a3ccbSGleb Smirnoff.Fn SYSCTL_ADD_UMA_MAX ctx parent nbr name access zone descr
5550a5a3ccbSGleb Smirnoffmacro is provided to create this type of oid dynamically.
5560a5a3ccbSGleb Smirnoff.Pp
5570a5a3ccbSGleb SmirnoffThe
5580a5a3ccbSGleb Smirnoff.Fn SYSCTL_UMA_CUR parent nbr name access zone descr
55962cb31dcSWarner Loshmacro declares a static read-only
560d794b3a3SMark Johnston.Xr sysctl 9
5610a5a3ccbSGleb Smirnoffoid that exports the approximate current occupancy of the zone.
5620a5a3ccbSGleb SmirnoffThe
5630a5a3ccbSGleb Smirnoff.Fa zone
5640a5a3ccbSGleb Smirnoffargument should be a pointer to
5650a5a3ccbSGleb Smirnoff.Vt uma_zone_t .
5660a5a3ccbSGleb SmirnoffA read of the oid returns value obtained through
5670a5a3ccbSGleb Smirnoff.Fn uma_zone_get_cur .
5680a5a3ccbSGleb SmirnoffThe
5690a5a3ccbSGleb Smirnoff.Fn SYSCTL_ADD_UMA_CUR ctx parent nbr name zone descr
5700a5a3ccbSGleb Smirnoffmacro is provided to create this type of oid dynamically.
5710766f278SJonathan T. Looney.Sh IMPLEMENTATION NOTES
5720766f278SJonathan T. LooneyThe memory that these allocation calls return is not executable.
5730766f278SJonathan T. LooneyThe
5740766f278SJonathan T. Looney.Fn uma_zalloc
5750766f278SJonathan T. Looneyfunction does not support the
5760766f278SJonathan T. Looney.Dv M_EXEC
5770766f278SJonathan T. Looneyflag to allocate executable memory.
5780766f278SJonathan T. LooneyNot all platforms enforce a distinction between executable and
5790766f278SJonathan T. Looneynon-executable memory.
5800aa028ffSDag-Erling Smørgrav.Sh SEE ALSO
581d794b3a3SMark Johnston.Xr numa 4 ,
582d794b3a3SMark Johnston.Xr vmstat 8 ,
5830aa028ffSDag-Erling Smørgrav.Xr malloc 9
584d794b3a3SMark Johnston.Rs
585d794b3a3SMark Johnston.%A Jeff Bonwick
586d794b3a3SMark Johnston.%T "The Slab Allocator: An Object-Caching Kernel Memory Allocator"
587d794b3a3SMark Johnston.%D 1994
588d794b3a3SMark Johnston.Re
5890aa028ffSDag-Erling Smørgrav.Sh HISTORY
5900aa028ffSDag-Erling SmørgravThe zone allocator first appeared in
5910aa028ffSDag-Erling Smørgrav.Fx 3.0 .
5929a795583SJeroen Ruigrok van der WervenIt was radically changed in
5939a795583SJeroen Ruigrok van der Werven.Fx 5.0
5949a795583SJeroen Ruigrok van der Wervento function as a slab allocator.
5950aa028ffSDag-Erling Smørgrav.Sh AUTHORS
5960aa028ffSDag-Erling Smørgrav.An -nosplit
5970aa028ffSDag-Erling SmørgravThe zone allocator was written by
5980aa028ffSDag-Erling Smørgrav.An John S. Dyson .
5999a795583SJeroen Ruigrok van der WervenThe zone allocator was rewritten in large parts by
6008a7314fcSBaptiste Daroussin.An Jeff Roberson Aq Mt jeff@FreeBSD.org
6019a795583SJeroen Ruigrok van der Wervento function as a slab allocator.
6020aa028ffSDag-Erling Smørgrav.Pp
6030aa028ffSDag-Erling SmørgravThis manual page was written by
6048a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
6059a795583SJeroen Ruigrok van der WervenChanges for UMA by
6068a7314fcSBaptiste Daroussin.An Jeroen Ruigrok van der Werven Aq Mt asmodai@FreeBSD.org .
607