11eaf0ac3Slogwang /*-
2*d4a07e70Sfengbojiang * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d4a07e70Sfengbojiang *
41eaf0ac3Slogwang * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <[email protected]>
51eaf0ac3Slogwang * Copyright (c) 2004, 2005 Bosko Milekic <[email protected]>
61eaf0ac3Slogwang * All rights reserved.
71eaf0ac3Slogwang *
81eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without
91eaf0ac3Slogwang * modification, are permitted provided that the following conditions
101eaf0ac3Slogwang * are met:
111eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright
121eaf0ac3Slogwang * notice unmodified, this list of conditions, and the following
131eaf0ac3Slogwang * disclaimer.
141eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright
151eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the
161eaf0ac3Slogwang * documentation and/or other materials provided with the distribution.
171eaf0ac3Slogwang *
181eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
191eaf0ac3Slogwang * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201eaf0ac3Slogwang * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
211eaf0ac3Slogwang * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
221eaf0ac3Slogwang * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
231eaf0ac3Slogwang * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241eaf0ac3Slogwang * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251eaf0ac3Slogwang * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261eaf0ac3Slogwang * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
271eaf0ac3Slogwang * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281eaf0ac3Slogwang *
291eaf0ac3Slogwang * $FreeBSD$
301eaf0ac3Slogwang *
311eaf0ac3Slogwang */
321eaf0ac3Slogwang
331eaf0ac3Slogwang /*
341eaf0ac3Slogwang * uma.h - External definitions for the Universal Memory Allocator
351eaf0ac3Slogwang *
361eaf0ac3Slogwang */
371eaf0ac3Slogwang
381eaf0ac3Slogwang #ifndef _VM_UMA_H_
391eaf0ac3Slogwang #define _VM_UMA_H_
401eaf0ac3Slogwang
411eaf0ac3Slogwang #include <sys/param.h> /* For NULL */
421eaf0ac3Slogwang #include <sys/malloc.h> /* For M_* */
43*d4a07e70Sfengbojiang #include <sys/_smr.h>
441eaf0ac3Slogwang
451eaf0ac3Slogwang /* User visible parameters */
46*d4a07e70Sfengbojiang #define UMA_SMALLEST_UNIT 8 /* Smallest item allocated */
471eaf0ac3Slogwang
481eaf0ac3Slogwang /* Types and type defs */
491eaf0ac3Slogwang
501eaf0ac3Slogwang struct uma_zone;
511eaf0ac3Slogwang /* Opaque type used as a handle to the zone */
521eaf0ac3Slogwang typedef struct uma_zone * uma_zone_t;
531eaf0ac3Slogwang
541eaf0ac3Slogwang /*
551eaf0ac3Slogwang * Item constructor
561eaf0ac3Slogwang *
571eaf0ac3Slogwang * Arguments:
581eaf0ac3Slogwang * item A pointer to the memory which has been allocated.
591eaf0ac3Slogwang * arg The arg field passed to uma_zalloc_arg
601eaf0ac3Slogwang * size The size of the allocated item
611eaf0ac3Slogwang * flags See zalloc flags
621eaf0ac3Slogwang *
631eaf0ac3Slogwang * Returns:
641eaf0ac3Slogwang * 0 on success
651eaf0ac3Slogwang * errno on failure
661eaf0ac3Slogwang *
671eaf0ac3Slogwang * Discussion:
681eaf0ac3Slogwang * The constructor is called just before the memory is returned
691eaf0ac3Slogwang * to the user. It may block if necessary.
701eaf0ac3Slogwang */
711eaf0ac3Slogwang typedef int (*uma_ctor)(void *mem, int size, void *arg, int flags);
721eaf0ac3Slogwang
731eaf0ac3Slogwang /*
741eaf0ac3Slogwang * Item destructor
751eaf0ac3Slogwang *
761eaf0ac3Slogwang * Arguments:
771eaf0ac3Slogwang * item A pointer to the memory which has been allocated.
781eaf0ac3Slogwang * size The size of the item being destructed.
791eaf0ac3Slogwang * arg Argument passed through uma_zfree_arg
801eaf0ac3Slogwang *
811eaf0ac3Slogwang * Returns:
821eaf0ac3Slogwang * Nothing
831eaf0ac3Slogwang *
841eaf0ac3Slogwang * Discussion:
851eaf0ac3Slogwang * The destructor may perform operations that differ from those performed
861eaf0ac3Slogwang * by the initializer, but it must leave the object in the same state.
871eaf0ac3Slogwang * This IS type stable storage. This is called after EVERY zfree call.
881eaf0ac3Slogwang */
891eaf0ac3Slogwang typedef void (*uma_dtor)(void *mem, int size, void *arg);
901eaf0ac3Slogwang
911eaf0ac3Slogwang /*
921eaf0ac3Slogwang * Item initializer
931eaf0ac3Slogwang *
941eaf0ac3Slogwang * Arguments:
951eaf0ac3Slogwang * item A pointer to the memory which has been allocated.
961eaf0ac3Slogwang * size The size of the item being initialized.
971eaf0ac3Slogwang * flags See zalloc flags
981eaf0ac3Slogwang *
991eaf0ac3Slogwang * Returns:
1001eaf0ac3Slogwang * 0 on success
1011eaf0ac3Slogwang * errno on failure
1021eaf0ac3Slogwang *
1031eaf0ac3Slogwang * Discussion:
1041eaf0ac3Slogwang * The initializer is called when the memory is cached in the uma zone.
1051eaf0ac3Slogwang * The initializer and the destructor should leave the object in the same
1061eaf0ac3Slogwang * state.
1071eaf0ac3Slogwang */
1081eaf0ac3Slogwang typedef int (*uma_init)(void *mem, int size, int flags);
1091eaf0ac3Slogwang
1101eaf0ac3Slogwang /*
1111eaf0ac3Slogwang * Item discard function
1121eaf0ac3Slogwang *
1131eaf0ac3Slogwang * Arguments:
1141eaf0ac3Slogwang * item A pointer to memory which has been 'freed' but has not left the
1151eaf0ac3Slogwang * zone's cache.
1161eaf0ac3Slogwang * size The size of the item being discarded.
1171eaf0ac3Slogwang *
1181eaf0ac3Slogwang * Returns:
1191eaf0ac3Slogwang * Nothing
1201eaf0ac3Slogwang *
1211eaf0ac3Slogwang * Discussion:
1221eaf0ac3Slogwang * This routine is called when memory leaves a zone and is returned to the
1231eaf0ac3Slogwang * system for other uses. It is the counter-part to the init function.
1241eaf0ac3Slogwang */
1251eaf0ac3Slogwang typedef void (*uma_fini)(void *mem, int size);
1261eaf0ac3Slogwang
1271eaf0ac3Slogwang /*
1281eaf0ac3Slogwang * Import new memory into a cache zone.
1291eaf0ac3Slogwang */
130*d4a07e70Sfengbojiang typedef int (*uma_import)(void *arg, void **store, int count, int domain,
131*d4a07e70Sfengbojiang int flags);
1321eaf0ac3Slogwang
1331eaf0ac3Slogwang /*
1341eaf0ac3Slogwang * Free memory from a cache zone.
1351eaf0ac3Slogwang */
1361eaf0ac3Slogwang typedef void (*uma_release)(void *arg, void **store, int count);
1371eaf0ac3Slogwang
1381eaf0ac3Slogwang /*
1391eaf0ac3Slogwang * What's the difference between initializing and constructing?
1401eaf0ac3Slogwang *
1411eaf0ac3Slogwang * The item is initialized when it is cached, and this is the state that the
1421eaf0ac3Slogwang * object should be in when returned to the allocator. The purpose of this is
1431eaf0ac3Slogwang * to remove some code which would otherwise be called on each allocation by
1441eaf0ac3Slogwang * utilizing a known, stable state. This differs from the constructor which
1451eaf0ac3Slogwang * will be called on EVERY allocation.
1461eaf0ac3Slogwang *
1471eaf0ac3Slogwang * For example, in the initializer you may want to initialize embedded locks,
1481eaf0ac3Slogwang * NULL list pointers, set up initial states, magic numbers, etc. This way if
1491eaf0ac3Slogwang * the object is held in the allocator and re-used it won't be necessary to
1501eaf0ac3Slogwang * re-initialize it.
1511eaf0ac3Slogwang *
1521eaf0ac3Slogwang * The constructor may be used to lock a data structure, link it on to lists,
1531eaf0ac3Slogwang * bump reference counts or total counts of outstanding structures, etc.
1541eaf0ac3Slogwang *
1551eaf0ac3Slogwang */
1561eaf0ac3Slogwang
1571eaf0ac3Slogwang /* Function proto types */
1581eaf0ac3Slogwang
1591eaf0ac3Slogwang /*
1601eaf0ac3Slogwang * Create a new uma zone
1611eaf0ac3Slogwang *
1621eaf0ac3Slogwang * Arguments:
1631eaf0ac3Slogwang * name The text name of the zone for debugging and stats. This memory
1641eaf0ac3Slogwang * should not be freed until the zone has been deallocated.
1651eaf0ac3Slogwang * size The size of the object that is being created.
1661eaf0ac3Slogwang * ctor The constructor that is called when the object is allocated.
1671eaf0ac3Slogwang * dtor The destructor that is called when the object is freed.
1681eaf0ac3Slogwang * init An initializer that sets up the initial state of the memory.
1691eaf0ac3Slogwang * fini A discard function that undoes initialization done by init.
1701eaf0ac3Slogwang * ctor/dtor/init/fini may all be null, see notes above.
1711eaf0ac3Slogwang * align A bitmask that corresponds to the requested alignment
1721eaf0ac3Slogwang * eg 4 would be 0x3
1731eaf0ac3Slogwang * flags A set of parameters that control the behavior of the zone.
1741eaf0ac3Slogwang *
1751eaf0ac3Slogwang * Returns:
1761eaf0ac3Slogwang * A pointer to a structure which is intended to be opaque to users of
1771eaf0ac3Slogwang * the interface. The value may be null if the wait flag is not set.
1781eaf0ac3Slogwang */
1791eaf0ac3Slogwang uma_zone_t uma_zcreate(const char *name, size_t size, uma_ctor ctor,
1801eaf0ac3Slogwang uma_dtor dtor, uma_init uminit, uma_fini fini,
1811eaf0ac3Slogwang int align, uint32_t flags);
1821eaf0ac3Slogwang
1831eaf0ac3Slogwang /*
1841eaf0ac3Slogwang * Create a secondary uma zone
1851eaf0ac3Slogwang *
1861eaf0ac3Slogwang * Arguments:
1871eaf0ac3Slogwang * name The text name of the zone for debugging and stats. This memory
1881eaf0ac3Slogwang * should not be freed until the zone has been deallocated.
1891eaf0ac3Slogwang * ctor The constructor that is called when the object is allocated.
1901eaf0ac3Slogwang * dtor The destructor that is called when the object is freed.
1911eaf0ac3Slogwang * zinit An initializer that sets up the initial state of the memory
1921eaf0ac3Slogwang * as the object passes from the Keg's slab to the Zone's cache.
1931eaf0ac3Slogwang * zfini A discard function that undoes initialization done by init
1941eaf0ac3Slogwang * as the object passes from the Zone's cache to the Keg's slab.
1951eaf0ac3Slogwang *
1961eaf0ac3Slogwang * ctor/dtor/zinit/zfini may all be null, see notes above.
1971eaf0ac3Slogwang * Note that the zinit and zfini specified here are NOT
1981eaf0ac3Slogwang * exactly the same as the init/fini specified to uma_zcreate()
199*d4a07e70Sfengbojiang * when creating a primary zone. These zinit/zfini are called
2001eaf0ac3Slogwang * on the TRANSITION from keg to zone (and vice-versa). Once
2011eaf0ac3Slogwang * these are set, the primary zone may alter its init/fini
2021eaf0ac3Slogwang * (which are called when the object passes from VM to keg)
2031eaf0ac3Slogwang * using uma_zone_set_init/fini()) as well as its own
204*d4a07e70Sfengbojiang * zinit/zfini (unset by default for primary zone) with
2051eaf0ac3Slogwang * uma_zone_set_zinit/zfini() (note subtle 'z' prefix).
2061eaf0ac3Slogwang *
207*d4a07e70Sfengbojiang * primary A reference to this zone's Primary Zone which contains the
208*d4a07e70Sfengbojiang * backing Keg for the Secondary Zone being added.
2091eaf0ac3Slogwang *
2101eaf0ac3Slogwang * Returns:
2111eaf0ac3Slogwang * A pointer to a structure which is intended to be opaque to users of
2121eaf0ac3Slogwang * the interface. The value may be null if the wait flag is not set.
2131eaf0ac3Slogwang */
214*d4a07e70Sfengbojiang uma_zone_t uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor,
215*d4a07e70Sfengbojiang uma_init zinit, uma_fini zfini, uma_zone_t primary);
2161eaf0ac3Slogwang
2171eaf0ac3Slogwang /*
2181eaf0ac3Slogwang * Create cache-only zones.
2191eaf0ac3Slogwang *
2201eaf0ac3Slogwang * This allows uma's per-cpu cache facilities to handle arbitrary
2211eaf0ac3Slogwang * pointers. Consumers must specify the import and release functions to
2221eaf0ac3Slogwang * fill and destroy caches. UMA does not allocate any memory for these
2231eaf0ac3Slogwang * zones. The 'arg' parameter is passed to import/release and is caller
2241eaf0ac3Slogwang * specific.
2251eaf0ac3Slogwang */
226*d4a07e70Sfengbojiang uma_zone_t uma_zcache_create(const char *name, int size, uma_ctor ctor,
227*d4a07e70Sfengbojiang uma_dtor dtor, uma_init zinit, uma_fini zfini, uma_import zimport,
2281eaf0ac3Slogwang uma_release zrelease, void *arg, int flags);
2291eaf0ac3Slogwang
2301eaf0ac3Slogwang /*
2311eaf0ac3Slogwang * Definitions for uma_zcreate flags
2321eaf0ac3Slogwang *
2331eaf0ac3Slogwang * These flags share space with UMA_ZFLAGs in uma_int.h. Be careful not to
234*d4a07e70Sfengbojiang * overlap when adding new features.
2351eaf0ac3Slogwang */
2361eaf0ac3Slogwang #define UMA_ZONE_ZINIT 0x0002 /* Initialize with zeros */
237*d4a07e70Sfengbojiang #define UMA_ZONE_CONTIG 0x0004 /*
238*d4a07e70Sfengbojiang * Physical memory underlying an object
239*d4a07e70Sfengbojiang * must be contiguous.
240*d4a07e70Sfengbojiang */
241*d4a07e70Sfengbojiang #define UMA_ZONE_NOTOUCH 0x0008 /* UMA may not access the memory */
2421eaf0ac3Slogwang #define UMA_ZONE_MALLOC 0x0010 /* For use by malloc(9) only! */
2431eaf0ac3Slogwang #define UMA_ZONE_NOFREE 0x0020 /* Do not free slabs of this type! */
2441eaf0ac3Slogwang #define UMA_ZONE_MTXCLASS 0x0040 /* Create a new lock class */
2451eaf0ac3Slogwang #define UMA_ZONE_VM 0x0080 /*
2461eaf0ac3Slogwang * Used for internal vm datastructures
2471eaf0ac3Slogwang * only.
2481eaf0ac3Slogwang */
249*d4a07e70Sfengbojiang #define UMA_ZONE_NOTPAGE 0x0100 /* allocf memory not vm pages */
2501eaf0ac3Slogwang #define UMA_ZONE_SECONDARY 0x0200 /* Zone is a Secondary Zone */
251*d4a07e70Sfengbojiang #define UMA_ZONE_NOBUCKET 0x0400 /* Do not use buckets. */
252*d4a07e70Sfengbojiang #define UMA_ZONE_MAXBUCKET 0x0800 /* Use largest buckets. */
253*d4a07e70Sfengbojiang #define UMA_ZONE_CACHESPREAD 0x2000 /*
2541eaf0ac3Slogwang * Spread memory start locations across
2551eaf0ac3Slogwang * all possible cache lines. May
2561eaf0ac3Slogwang * require many virtually contiguous
2571eaf0ac3Slogwang * backend pages and can fail early.
2581eaf0ac3Slogwang */
2591eaf0ac3Slogwang #define UMA_ZONE_NODUMP 0x4000 /*
2601eaf0ac3Slogwang * Zone's pages will not be included in
2611eaf0ac3Slogwang * mini-dumps.
2621eaf0ac3Slogwang */
2631eaf0ac3Slogwang #define UMA_ZONE_PCPU 0x8000 /*
264*d4a07e70Sfengbojiang * Allocates mp_maxid + 1 slabs of
265*d4a07e70Sfengbojiang * PAGE_SIZE
2661eaf0ac3Slogwang */
267*d4a07e70Sfengbojiang #define UMA_ZONE_FIRSTTOUCH 0x10000 /* First touch NUMA policy */
268*d4a07e70Sfengbojiang #define UMA_ZONE_ROUNDROBIN 0x20000 /* Round-robin NUMA policy. */
269*d4a07e70Sfengbojiang #define UMA_ZONE_SMR 0x40000 /*
270*d4a07e70Sfengbojiang * Safe memory reclamation defers
271*d4a07e70Sfengbojiang * frees until all read sections
272*d4a07e70Sfengbojiang * have exited. This flag creates
273*d4a07e70Sfengbojiang * a unique SMR context for this
274*d4a07e70Sfengbojiang * zone. To share contexts see
275*d4a07e70Sfengbojiang * uma_zone_set_smr() below.
276*d4a07e70Sfengbojiang *
277*d4a07e70Sfengbojiang * See sys/smr.h for more details.
278*d4a07e70Sfengbojiang */
279*d4a07e70Sfengbojiang /* In use by UMA_ZFLAGs: 0xffe00000 */
2801eaf0ac3Slogwang
2811eaf0ac3Slogwang /*
282*d4a07e70Sfengbojiang * These flags are shared between the keg and zone. Some are determined
283*d4a07e70Sfengbojiang * based on physical parameters of the request and may not be provided by
284*d4a07e70Sfengbojiang * the consumer.
2851eaf0ac3Slogwang */
2861eaf0ac3Slogwang #define UMA_ZONE_INHERIT \
287*d4a07e70Sfengbojiang (UMA_ZONE_NOTOUCH | UMA_ZONE_MALLOC | UMA_ZONE_NOFREE | \
288*d4a07e70Sfengbojiang UMA_ZONE_VM | UMA_ZONE_NOTPAGE | UMA_ZONE_PCPU | \
289*d4a07e70Sfengbojiang UMA_ZONE_FIRSTTOUCH | UMA_ZONE_ROUNDROBIN)
2901eaf0ac3Slogwang
2911eaf0ac3Slogwang /* Definitions for align */
2921eaf0ac3Slogwang #define UMA_ALIGN_PTR (sizeof(void *) - 1) /* Alignment fit for ptr */
2931eaf0ac3Slogwang #define UMA_ALIGN_LONG (sizeof(long) - 1) /* "" long */
2941eaf0ac3Slogwang #define UMA_ALIGN_INT (sizeof(int) - 1) /* "" int */
2951eaf0ac3Slogwang #define UMA_ALIGN_SHORT (sizeof(short) - 1) /* "" short */
2961eaf0ac3Slogwang #define UMA_ALIGN_CHAR (sizeof(char) - 1) /* "" char */
2971eaf0ac3Slogwang #define UMA_ALIGN_CACHE (0 - 1) /* Cache line size align */
298*d4a07e70Sfengbojiang #define UMA_ALIGNOF(type) (_Alignof(type) - 1) /* Alignment fit for 'type' */
299*d4a07e70Sfengbojiang
300*d4a07e70Sfengbojiang #define UMA_ANYDOMAIN -1 /* Special value for domain search. */
3011eaf0ac3Slogwang
3021eaf0ac3Slogwang /*
3031eaf0ac3Slogwang * Destroys an empty uma zone. If the zone is not empty uma complains loudly.
3041eaf0ac3Slogwang *
3051eaf0ac3Slogwang * Arguments:
3061eaf0ac3Slogwang * zone The zone we want to destroy.
3071eaf0ac3Slogwang *
3081eaf0ac3Slogwang */
3091eaf0ac3Slogwang void uma_zdestroy(uma_zone_t zone);
3101eaf0ac3Slogwang
3111eaf0ac3Slogwang /*
3121eaf0ac3Slogwang * Allocates an item out of a zone
3131eaf0ac3Slogwang *
3141eaf0ac3Slogwang * Arguments:
3151eaf0ac3Slogwang * zone The zone we are allocating from
3161eaf0ac3Slogwang * arg This data is passed to the ctor function
3171eaf0ac3Slogwang * flags See sys/malloc.h for available flags.
3181eaf0ac3Slogwang *
3191eaf0ac3Slogwang * Returns:
3201eaf0ac3Slogwang * A non-null pointer to an initialized element from the zone is
3211eaf0ac3Slogwang * guaranteed if the wait flag is M_WAITOK. Otherwise a null pointer
3221eaf0ac3Slogwang * may be returned if the zone is empty or the ctor failed.
3231eaf0ac3Slogwang */
3241eaf0ac3Slogwang
3251eaf0ac3Slogwang void *uma_zalloc_arg(uma_zone_t zone, void *arg, int flags);
3261eaf0ac3Slogwang
327*d4a07e70Sfengbojiang /* Allocate per-cpu data. Access the correct data with zpcpu_get(). */
328*d4a07e70Sfengbojiang void *uma_zalloc_pcpu_arg(uma_zone_t zone, void *arg, int flags);
329*d4a07e70Sfengbojiang
330*d4a07e70Sfengbojiang /* Use with SMR zones. */
331*d4a07e70Sfengbojiang void *uma_zalloc_smr(uma_zone_t zone, int flags);
332*d4a07e70Sfengbojiang
333*d4a07e70Sfengbojiang /*
334*d4a07e70Sfengbojiang * Allocate an item from a specific NUMA domain. This uses a slow path in
335*d4a07e70Sfengbojiang * the allocator but is guaranteed to allocate memory from the requested
336*d4a07e70Sfengbojiang * domain if M_WAITOK is set.
337*d4a07e70Sfengbojiang *
338*d4a07e70Sfengbojiang * Arguments:
339*d4a07e70Sfengbojiang * zone The zone we are allocating from
340*d4a07e70Sfengbojiang * arg This data is passed to the ctor function
341*d4a07e70Sfengbojiang * domain The domain to allocate from.
342*d4a07e70Sfengbojiang * flags See sys/malloc.h for available flags.
343*d4a07e70Sfengbojiang */
344*d4a07e70Sfengbojiang void *uma_zalloc_domain(uma_zone_t zone, void *arg, int domain, int flags);
345*d4a07e70Sfengbojiang
3461eaf0ac3Slogwang /*
3471eaf0ac3Slogwang * Allocates an item out of a zone without supplying an argument
3481eaf0ac3Slogwang *
3491eaf0ac3Slogwang * This is just a wrapper for uma_zalloc_arg for convenience.
3501eaf0ac3Slogwang *
3511eaf0ac3Slogwang */
3521eaf0ac3Slogwang static __inline void *uma_zalloc(uma_zone_t zone, int flags);
353*d4a07e70Sfengbojiang static __inline void *uma_zalloc_pcpu(uma_zone_t zone, int flags);
3541eaf0ac3Slogwang
3551eaf0ac3Slogwang static __inline void *
uma_zalloc(uma_zone_t zone,int flags)3561eaf0ac3Slogwang uma_zalloc(uma_zone_t zone, int flags)
3571eaf0ac3Slogwang {
3581eaf0ac3Slogwang return uma_zalloc_arg(zone, NULL, flags);
3591eaf0ac3Slogwang }
3601eaf0ac3Slogwang
361*d4a07e70Sfengbojiang static __inline void *
uma_zalloc_pcpu(uma_zone_t zone,int flags)362*d4a07e70Sfengbojiang uma_zalloc_pcpu(uma_zone_t zone, int flags)
363*d4a07e70Sfengbojiang {
364*d4a07e70Sfengbojiang return uma_zalloc_pcpu_arg(zone, NULL, flags);
365*d4a07e70Sfengbojiang }
366*d4a07e70Sfengbojiang
3671eaf0ac3Slogwang /*
3681eaf0ac3Slogwang * Frees an item back into the specified zone.
3691eaf0ac3Slogwang *
3701eaf0ac3Slogwang * Arguments:
3711eaf0ac3Slogwang * zone The zone the item was originally allocated out of.
3721eaf0ac3Slogwang * item The memory to be freed.
3731eaf0ac3Slogwang * arg Argument passed to the destructor
3741eaf0ac3Slogwang *
3751eaf0ac3Slogwang * Returns:
3761eaf0ac3Slogwang * Nothing.
3771eaf0ac3Slogwang */
3781eaf0ac3Slogwang
3791eaf0ac3Slogwang void uma_zfree_arg(uma_zone_t zone, void *item, void *arg);
3801eaf0ac3Slogwang
381*d4a07e70Sfengbojiang /* Use with PCPU zones. */
382*d4a07e70Sfengbojiang void uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *arg);
383*d4a07e70Sfengbojiang
384*d4a07e70Sfengbojiang /* Use with SMR zones. */
385*d4a07e70Sfengbojiang void uma_zfree_smr(uma_zone_t zone, void *item);
386*d4a07e70Sfengbojiang
3871eaf0ac3Slogwang /*
3881eaf0ac3Slogwang * Frees an item back to a zone without supplying an argument
3891eaf0ac3Slogwang *
3901eaf0ac3Slogwang * This is just a wrapper for uma_zfree_arg for convenience.
3911eaf0ac3Slogwang *
3921eaf0ac3Slogwang */
3931eaf0ac3Slogwang static __inline void uma_zfree(uma_zone_t zone, void *item);
394*d4a07e70Sfengbojiang static __inline void uma_zfree_pcpu(uma_zone_t zone, void *item);
3951eaf0ac3Slogwang
3961eaf0ac3Slogwang static __inline void
uma_zfree(uma_zone_t zone,void * item)3971eaf0ac3Slogwang uma_zfree(uma_zone_t zone, void *item)
3981eaf0ac3Slogwang {
3991eaf0ac3Slogwang uma_zfree_arg(zone, item, NULL);
4001eaf0ac3Slogwang }
4011eaf0ac3Slogwang
402*d4a07e70Sfengbojiang static __inline void
uma_zfree_pcpu(uma_zone_t zone,void * item)403*d4a07e70Sfengbojiang uma_zfree_pcpu(uma_zone_t zone, void *item)
404*d4a07e70Sfengbojiang {
405*d4a07e70Sfengbojiang uma_zfree_pcpu_arg(zone, item, NULL);
406*d4a07e70Sfengbojiang }
407*d4a07e70Sfengbojiang
4081eaf0ac3Slogwang /*
409*d4a07e70Sfengbojiang * Wait until the specified zone can allocate an item.
4101eaf0ac3Slogwang */
411*d4a07e70Sfengbojiang void uma_zwait(uma_zone_t zone);
4121eaf0ac3Slogwang
4131eaf0ac3Slogwang /*
4141eaf0ac3Slogwang * Backend page supplier routines
4151eaf0ac3Slogwang *
4161eaf0ac3Slogwang * Arguments:
4171eaf0ac3Slogwang * zone The zone that is requesting pages.
4181eaf0ac3Slogwang * size The number of bytes being requested.
4191eaf0ac3Slogwang * pflag Flags for these memory pages, see below.
420*d4a07e70Sfengbojiang * domain The NUMA domain that we prefer for this allocation.
4211eaf0ac3Slogwang * wait Indicates our willingness to block.
4221eaf0ac3Slogwang *
4231eaf0ac3Slogwang * Returns:
4241eaf0ac3Slogwang * A pointer to the allocated memory or NULL on failure.
4251eaf0ac3Slogwang */
4261eaf0ac3Slogwang
427*d4a07e70Sfengbojiang typedef void *(*uma_alloc)(uma_zone_t zone, vm_size_t size, int domain,
428*d4a07e70Sfengbojiang uint8_t *pflag, int wait);
4291eaf0ac3Slogwang
4301eaf0ac3Slogwang /*
4311eaf0ac3Slogwang * Backend page free routines
4321eaf0ac3Slogwang *
4331eaf0ac3Slogwang * Arguments:
4341eaf0ac3Slogwang * item A pointer to the previously allocated pages.
4351eaf0ac3Slogwang * size The original size of the allocation.
4361eaf0ac3Slogwang * pflag The flags for the slab. See UMA_SLAB_* below.
4371eaf0ac3Slogwang *
4381eaf0ac3Slogwang * Returns:
4391eaf0ac3Slogwang * None
4401eaf0ac3Slogwang */
4411eaf0ac3Slogwang typedef void (*uma_free)(void *item, vm_size_t size, uint8_t pflag);
4421eaf0ac3Slogwang
4431eaf0ac3Slogwang /*
444*d4a07e70Sfengbojiang * Reclaims unused memory
4451eaf0ac3Slogwang *
4461eaf0ac3Slogwang * Arguments:
447*d4a07e70Sfengbojiang * req Reclamation request type.
4481eaf0ac3Slogwang * Returns:
4491eaf0ac3Slogwang * None
4501eaf0ac3Slogwang */
451*d4a07e70Sfengbojiang #define UMA_RECLAIM_DRAIN 1 /* release bucket cache */
452*d4a07e70Sfengbojiang #define UMA_RECLAIM_DRAIN_CPU 2 /* release bucket and per-CPU caches */
453*d4a07e70Sfengbojiang #define UMA_RECLAIM_TRIM 3 /* trim bucket cache to WSS */
454*d4a07e70Sfengbojiang void uma_reclaim(int req);
455*d4a07e70Sfengbojiang void uma_zone_reclaim(uma_zone_t, int req);
4561eaf0ac3Slogwang
4571eaf0ac3Slogwang /*
4581eaf0ac3Slogwang * Sets the alignment mask to be used for all zones requesting cache
4591eaf0ac3Slogwang * alignment. Should be called by MD boot code prior to starting VM/UMA.
4601eaf0ac3Slogwang *
4611eaf0ac3Slogwang * Arguments:
4621eaf0ac3Slogwang * align The alignment mask
4631eaf0ac3Slogwang *
4641eaf0ac3Slogwang * Returns:
4651eaf0ac3Slogwang * Nothing
4661eaf0ac3Slogwang */
4671eaf0ac3Slogwang void uma_set_align(int align);
4681eaf0ac3Slogwang
4691eaf0ac3Slogwang /*
4701eaf0ac3Slogwang * Set a reserved number of items to hold for M_USE_RESERVE allocations. All
4711eaf0ac3Slogwang * other requests must allocate new backing pages.
4721eaf0ac3Slogwang */
4731eaf0ac3Slogwang void uma_zone_reserve(uma_zone_t zone, int nitems);
4741eaf0ac3Slogwang
4751eaf0ac3Slogwang /*
4761eaf0ac3Slogwang * Reserves the maximum KVA space required by the zone and configures the zone
4771eaf0ac3Slogwang * to use a VM_ALLOC_NOOBJ-based backend allocator.
4781eaf0ac3Slogwang *
4791eaf0ac3Slogwang * Arguments:
4801eaf0ac3Slogwang * zone The zone to update.
4811eaf0ac3Slogwang * nitems The upper limit on the number of items that can be allocated.
4821eaf0ac3Slogwang *
4831eaf0ac3Slogwang * Returns:
4841eaf0ac3Slogwang * 0 if KVA space can not be allocated
4851eaf0ac3Slogwang * 1 if successful
4861eaf0ac3Slogwang *
4871eaf0ac3Slogwang * Discussion:
4881eaf0ac3Slogwang * When the machine supports a direct map and the zone's items are smaller
4891eaf0ac3Slogwang * than a page, the zone will use the direct map instead of allocating KVA
4901eaf0ac3Slogwang * space.
4911eaf0ac3Slogwang */
4921eaf0ac3Slogwang int uma_zone_reserve_kva(uma_zone_t zone, int nitems);
4931eaf0ac3Slogwang
4941eaf0ac3Slogwang /*
495*d4a07e70Sfengbojiang * Sets an upper limit on the number of items allocated from a zone
4961eaf0ac3Slogwang *
4971eaf0ac3Slogwang * Arguments:
4981eaf0ac3Slogwang * zone The zone to limit
4991eaf0ac3Slogwang * nitems The requested upper limit on the number of items allowed
5001eaf0ac3Slogwang *
5011eaf0ac3Slogwang * Returns:
502*d4a07e70Sfengbojiang * int The effective value of nitems
5031eaf0ac3Slogwang */
5041eaf0ac3Slogwang int uma_zone_set_max(uma_zone_t zone, int nitems);
5051eaf0ac3Slogwang
5061eaf0ac3Slogwang /*
507*d4a07e70Sfengbojiang * Sets an upper limit on the number of items allowed in zone's caches
508*d4a07e70Sfengbojiang *
509*d4a07e70Sfengbojiang * Arguments:
510*d4a07e70Sfengbojiang * zone The zone to limit
511*d4a07e70Sfengbojiang * nitems The requested upper limit on the number of items allowed
512*d4a07e70Sfengbojiang */
513*d4a07e70Sfengbojiang void uma_zone_set_maxcache(uma_zone_t zone, int nitems);
514*d4a07e70Sfengbojiang
515*d4a07e70Sfengbojiang /*
5161eaf0ac3Slogwang * Obtains the effective limit on the number of items in a zone
5171eaf0ac3Slogwang *
5181eaf0ac3Slogwang * Arguments:
5191eaf0ac3Slogwang * zone The zone to obtain the effective limit from
5201eaf0ac3Slogwang *
5211eaf0ac3Slogwang * Return:
5221eaf0ac3Slogwang * 0 No limit
5231eaf0ac3Slogwang * int The effective limit of the zone
5241eaf0ac3Slogwang */
5251eaf0ac3Slogwang int uma_zone_get_max(uma_zone_t zone);
5261eaf0ac3Slogwang
5271eaf0ac3Slogwang /*
5281eaf0ac3Slogwang * Sets a warning to be printed when limit is reached
5291eaf0ac3Slogwang *
5301eaf0ac3Slogwang * Arguments:
5311eaf0ac3Slogwang * zone The zone we will warn about
5321eaf0ac3Slogwang * warning Warning content
5331eaf0ac3Slogwang *
5341eaf0ac3Slogwang * Returns:
5351eaf0ac3Slogwang * Nothing
5361eaf0ac3Slogwang */
5371eaf0ac3Slogwang void uma_zone_set_warning(uma_zone_t zone, const char *warning);
5381eaf0ac3Slogwang
5391eaf0ac3Slogwang /*
5401eaf0ac3Slogwang * Sets a function to run when limit is reached
5411eaf0ac3Slogwang *
5421eaf0ac3Slogwang * Arguments:
5431eaf0ac3Slogwang * zone The zone to which this applies
5441eaf0ac3Slogwang * fx The function ro run
5451eaf0ac3Slogwang *
5461eaf0ac3Slogwang * Returns:
5471eaf0ac3Slogwang * Nothing
5481eaf0ac3Slogwang */
5491eaf0ac3Slogwang typedef void (*uma_maxaction_t)(uma_zone_t, int);
5501eaf0ac3Slogwang void uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t);
5511eaf0ac3Slogwang
5521eaf0ac3Slogwang /*
5531eaf0ac3Slogwang * Obtains the approximate current number of items allocated from a zone
5541eaf0ac3Slogwang *
5551eaf0ac3Slogwang * Arguments:
5561eaf0ac3Slogwang * zone The zone to obtain the current allocation count from
5571eaf0ac3Slogwang *
5581eaf0ac3Slogwang * Return:
5591eaf0ac3Slogwang * int The approximate current number of items allocated from the zone
5601eaf0ac3Slogwang */
5611eaf0ac3Slogwang int uma_zone_get_cur(uma_zone_t zone);
5621eaf0ac3Slogwang
5631eaf0ac3Slogwang /*
5641eaf0ac3Slogwang * The following two routines (uma_zone_set_init/fini)
5651eaf0ac3Slogwang * are used to set the backend init/fini pair which acts on an
5661eaf0ac3Slogwang * object as it becomes allocated and is placed in a slab within
5671eaf0ac3Slogwang * the specified zone's backing keg. These should probably not
5681eaf0ac3Slogwang * be changed once allocations have already begun, but only be set
5691eaf0ac3Slogwang * immediately upon zone creation.
5701eaf0ac3Slogwang */
5711eaf0ac3Slogwang void uma_zone_set_init(uma_zone_t zone, uma_init uminit);
5721eaf0ac3Slogwang void uma_zone_set_fini(uma_zone_t zone, uma_fini fini);
5731eaf0ac3Slogwang
5741eaf0ac3Slogwang /*
5751eaf0ac3Slogwang * The following two routines (uma_zone_set_zinit/zfini) are
5761eaf0ac3Slogwang * used to set the zinit/zfini pair which acts on an object as
5771eaf0ac3Slogwang * it passes from the backing Keg's slab cache to the
5781eaf0ac3Slogwang * specified Zone's bucket cache. These should probably not
5791eaf0ac3Slogwang * be changed once allocations have already begun, but only be set
5801eaf0ac3Slogwang * immediately upon zone creation.
5811eaf0ac3Slogwang */
5821eaf0ac3Slogwang void uma_zone_set_zinit(uma_zone_t zone, uma_init zinit);
5831eaf0ac3Slogwang void uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini);
5841eaf0ac3Slogwang
5851eaf0ac3Slogwang /*
5861eaf0ac3Slogwang * Replaces the standard backend allocator for this zone.
5871eaf0ac3Slogwang *
5881eaf0ac3Slogwang * Arguments:
5891eaf0ac3Slogwang * zone The zone whose backend allocator is being changed.
5901eaf0ac3Slogwang * allocf A pointer to the allocation function
5911eaf0ac3Slogwang *
5921eaf0ac3Slogwang * Returns:
5931eaf0ac3Slogwang * Nothing
5941eaf0ac3Slogwang *
5951eaf0ac3Slogwang * Discussion:
5961eaf0ac3Slogwang * This could be used to implement pageable allocation, or perhaps
5971eaf0ac3Slogwang * even DMA allocators if used in conjunction with the OFFPAGE
5981eaf0ac3Slogwang * zone flag.
5991eaf0ac3Slogwang */
6001eaf0ac3Slogwang
6011eaf0ac3Slogwang void uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf);
6021eaf0ac3Slogwang
6031eaf0ac3Slogwang /*
6041eaf0ac3Slogwang * Used for freeing memory provided by the allocf above
6051eaf0ac3Slogwang *
6061eaf0ac3Slogwang * Arguments:
6071eaf0ac3Slogwang * zone The zone that intends to use this free routine.
6081eaf0ac3Slogwang * freef The page freeing routine.
6091eaf0ac3Slogwang *
6101eaf0ac3Slogwang * Returns:
6111eaf0ac3Slogwang * Nothing
6121eaf0ac3Slogwang */
6131eaf0ac3Slogwang
6141eaf0ac3Slogwang void uma_zone_set_freef(uma_zone_t zone, uma_free freef);
6151eaf0ac3Slogwang
6161eaf0ac3Slogwang /*
617*d4a07e70Sfengbojiang * Associate a zone with a smr context that is allocated after creation
618*d4a07e70Sfengbojiang * so that multiple zones may share the same context.
619*d4a07e70Sfengbojiang */
620*d4a07e70Sfengbojiang void uma_zone_set_smr(uma_zone_t zone, smr_t smr);
621*d4a07e70Sfengbojiang
622*d4a07e70Sfengbojiang /*
623*d4a07e70Sfengbojiang * Fetch the smr context that was set or made in uma_zcreate().
624*d4a07e70Sfengbojiang */
625*d4a07e70Sfengbojiang smr_t uma_zone_get_smr(uma_zone_t zone);
626*d4a07e70Sfengbojiang
627*d4a07e70Sfengbojiang /*
6281eaf0ac3Slogwang * These flags are setable in the allocf and visible in the freef.
6291eaf0ac3Slogwang */
6301eaf0ac3Slogwang #define UMA_SLAB_BOOT 0x01 /* Slab alloced from boot pages */
631*d4a07e70Sfengbojiang #define UMA_SLAB_KERNEL 0x04 /* Slab alloced from kmem */
6321eaf0ac3Slogwang #define UMA_SLAB_PRIV 0x08 /* Slab alloced from priv allocator */
633*d4a07e70Sfengbojiang /* 0x02, 0x10, 0x40, and 0x80 are available */
6341eaf0ac3Slogwang
6351eaf0ac3Slogwang /*
6361eaf0ac3Slogwang * Used to pre-fill a zone with some number of items
6371eaf0ac3Slogwang *
6381eaf0ac3Slogwang * Arguments:
6391eaf0ac3Slogwang * zone The zone to fill
6401eaf0ac3Slogwang * itemcnt The number of items to reserve
6411eaf0ac3Slogwang *
6421eaf0ac3Slogwang * Returns:
6431eaf0ac3Slogwang * Nothing
6441eaf0ac3Slogwang *
6451eaf0ac3Slogwang * NOTE: This is blocking and should only be done at startup
6461eaf0ac3Slogwang */
6471eaf0ac3Slogwang void uma_prealloc(uma_zone_t zone, int itemcnt);
6481eaf0ac3Slogwang
6491eaf0ac3Slogwang /*
6501eaf0ac3Slogwang * Used to determine if a fixed-size zone is exhausted.
6511eaf0ac3Slogwang *
6521eaf0ac3Slogwang * Arguments:
6531eaf0ac3Slogwang * zone The zone to check
6541eaf0ac3Slogwang *
6551eaf0ac3Slogwang * Returns:
6561eaf0ac3Slogwang * Non-zero if zone is exhausted.
6571eaf0ac3Slogwang */
6581eaf0ac3Slogwang int uma_zone_exhausted(uma_zone_t zone);
659*d4a07e70Sfengbojiang
660*d4a07e70Sfengbojiang /*
661*d4a07e70Sfengbojiang * Returns the bytes of memory consumed by the zone.
662*d4a07e70Sfengbojiang */
663*d4a07e70Sfengbojiang size_t uma_zone_memory(uma_zone_t zone);
6641eaf0ac3Slogwang
6651eaf0ac3Slogwang /*
6661eaf0ac3Slogwang * Common UMA_ZONE_PCPU zones.
6671eaf0ac3Slogwang */
668*d4a07e70Sfengbojiang extern uma_zone_t pcpu_zone_4;
669*d4a07e70Sfengbojiang extern uma_zone_t pcpu_zone_8;
670*d4a07e70Sfengbojiang extern uma_zone_t pcpu_zone_16;
671*d4a07e70Sfengbojiang extern uma_zone_t pcpu_zone_32;
6721eaf0ac3Slogwang extern uma_zone_t pcpu_zone_64;
6731eaf0ac3Slogwang
6741eaf0ac3Slogwang /*
6751eaf0ac3Slogwang * Exported statistics structures to be used by user space monitoring tools.
6761eaf0ac3Slogwang * Statistics stream consists of a uma_stream_header, followed by a series of
6771eaf0ac3Slogwang * alternative uma_type_header and uma_type_stat structures.
6781eaf0ac3Slogwang */
6791eaf0ac3Slogwang #define UMA_STREAM_VERSION 0x00000001
6801eaf0ac3Slogwang struct uma_stream_header {
6811eaf0ac3Slogwang uint32_t ush_version; /* Stream format version. */
6821eaf0ac3Slogwang uint32_t ush_maxcpus; /* Value of MAXCPU for stream. */
6831eaf0ac3Slogwang uint32_t ush_count; /* Number of records. */
6841eaf0ac3Slogwang uint32_t _ush_pad; /* Pad/reserved field. */
6851eaf0ac3Slogwang };
6861eaf0ac3Slogwang
6871eaf0ac3Slogwang #define UTH_MAX_NAME 32
6881eaf0ac3Slogwang #define UTH_ZONE_SECONDARY 0x00000001
6891eaf0ac3Slogwang struct uma_type_header {
6901eaf0ac3Slogwang /*
6911eaf0ac3Slogwang * Static per-zone data, some extracted from the supporting keg.
6921eaf0ac3Slogwang */
6931eaf0ac3Slogwang char uth_name[UTH_MAX_NAME];
6941eaf0ac3Slogwang uint32_t uth_align; /* Keg: alignment. */
6951eaf0ac3Slogwang uint32_t uth_size; /* Keg: requested size of item. */
6961eaf0ac3Slogwang uint32_t uth_rsize; /* Keg: real size of item. */
6971eaf0ac3Slogwang uint32_t uth_maxpages; /* Keg: maximum number of pages. */
6981eaf0ac3Slogwang uint32_t uth_limit; /* Keg: max items to allocate. */
6991eaf0ac3Slogwang
7001eaf0ac3Slogwang /*
7011eaf0ac3Slogwang * Current dynamic zone/keg-derived statistics.
7021eaf0ac3Slogwang */
7031eaf0ac3Slogwang uint32_t uth_pages; /* Keg: pages allocated. */
7041eaf0ac3Slogwang uint32_t uth_keg_free; /* Keg: items free. */
7051eaf0ac3Slogwang uint32_t uth_zone_free; /* Zone: items free. */
7061eaf0ac3Slogwang uint32_t uth_bucketsize; /* Zone: desired bucket size. */
7071eaf0ac3Slogwang uint32_t uth_zone_flags; /* Zone: flags. */
7081eaf0ac3Slogwang uint64_t uth_allocs; /* Zone: number of allocations. */
7091eaf0ac3Slogwang uint64_t uth_frees; /* Zone: number of frees. */
7101eaf0ac3Slogwang uint64_t uth_fails; /* Zone: number of alloc failures. */
7111eaf0ac3Slogwang uint64_t uth_sleeps; /* Zone: number of alloc sleeps. */
712*d4a07e70Sfengbojiang uint64_t uth_xdomain; /* Zone: Number of cross domain frees. */
713*d4a07e70Sfengbojiang uint64_t _uth_reserved1[1]; /* Reserved. */
7141eaf0ac3Slogwang };
7151eaf0ac3Slogwang
7161eaf0ac3Slogwang struct uma_percpu_stat {
7171eaf0ac3Slogwang uint64_t ups_allocs; /* Cache: number of allocations. */
7181eaf0ac3Slogwang uint64_t ups_frees; /* Cache: number of frees. */
7191eaf0ac3Slogwang uint64_t ups_cache_free; /* Cache: free items in cache. */
7201eaf0ac3Slogwang uint64_t _ups_reserved[5]; /* Reserved. */
7211eaf0ac3Slogwang };
7221eaf0ac3Slogwang
7231eaf0ac3Slogwang void uma_reclaim_wakeup(void);
7241eaf0ac3Slogwang void uma_reclaim_worker(void *);
7251eaf0ac3Slogwang
726*d4a07e70Sfengbojiang unsigned long uma_limit(void);
727*d4a07e70Sfengbojiang
728*d4a07e70Sfengbojiang /* Return the amount of memory managed by UMA. */
729*d4a07e70Sfengbojiang unsigned long uma_size(void);
730*d4a07e70Sfengbojiang
731*d4a07e70Sfengbojiang /* Return the amount of memory remaining. May be negative. */
732*d4a07e70Sfengbojiang long uma_avail(void);
733*d4a07e70Sfengbojiang
7341eaf0ac3Slogwang #endif /* _VM_UMA_H_ */
735