xref: /freebsd/sys/vm/uma.h (revision 0095a784195d45d2ffdaa43fef5a5499a7007464)
160727d8bSWarner Losh /*-
208ecce74SRobert Watson  * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org>
308ecce74SRobert Watson  * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
408ecce74SRobert Watson  * All rights reserved.
58355f576SJeff Roberson  *
68355f576SJeff Roberson  * Redistribution and use in source and binary forms, with or without
78355f576SJeff Roberson  * modification, are permitted provided that the following conditions
88355f576SJeff Roberson  * are met:
98355f576SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
108355f576SJeff Roberson  *    notice unmodified, this list of conditions, and the following
118355f576SJeff Roberson  *    disclaimer.
128355f576SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
138355f576SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
148355f576SJeff Roberson  *    documentation and/or other materials provided with the distribution.
158355f576SJeff Roberson  *
168355f576SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
178355f576SJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
188355f576SJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198355f576SJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
208355f576SJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
218355f576SJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
228355f576SJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
238355f576SJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248355f576SJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
258355f576SJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
268355f576SJeff Roberson  *
278355f576SJeff Roberson  * $FreeBSD$
288355f576SJeff Roberson  *
298355f576SJeff Roberson  */
308355f576SJeff Roberson 
318355f576SJeff Roberson /*
328355f576SJeff Roberson  * uma.h - External definitions for the Universal Memory Allocator
338355f576SJeff Roberson  *
348355f576SJeff Roberson */
358355f576SJeff Roberson 
368355f576SJeff Roberson #ifndef VM_UMA_H
378355f576SJeff Roberson #define VM_UMA_H
388355f576SJeff Roberson 
398355f576SJeff Roberson #include <sys/param.h>		/* For NULL */
408355f576SJeff Roberson #include <sys/malloc.h>		/* For M_* */
418355f576SJeff Roberson 
422db63c5eSGiorgos Keramidas /* User visible parameters */
438355f576SJeff Roberson #define UMA_SMALLEST_UNIT       (PAGE_SIZE / 256) /* Smallest item allocated */
448355f576SJeff Roberson 
458355f576SJeff Roberson /* Types and type defs */
468355f576SJeff Roberson 
478355f576SJeff Roberson struct uma_zone;
488355f576SJeff Roberson /* Opaque type used as a handle to the zone */
498355f576SJeff Roberson typedef struct uma_zone * uma_zone_t;
508355f576SJeff Roberson 
516c125b8dSMohan Srinivasan void zone_drain(uma_zone_t);
526c125b8dSMohan Srinivasan 
538355f576SJeff Roberson /*
548355f576SJeff Roberson  * Item constructor
558355f576SJeff Roberson  *
568355f576SJeff Roberson  * Arguments:
578355f576SJeff Roberson  *	item  A pointer to the memory which has been allocated.
588355f576SJeff Roberson  *	arg   The arg field passed to uma_zalloc_arg
598355f576SJeff Roberson  *	size  The size of the allocated item
60b23f72e9SBrian Feldman  *	flags See zalloc flags
618355f576SJeff Roberson  *
628355f576SJeff Roberson  * Returns:
63b23f72e9SBrian Feldman  *	0      on success
64b23f72e9SBrian Feldman  *      errno  on failure
658355f576SJeff Roberson  *
668355f576SJeff Roberson  * Discussion:
678355f576SJeff Roberson  *	The constructor is called just before the memory is returned
6829b4d526SSheldon Hearn  *	to the user. It may block if necessary.
698355f576SJeff Roberson  */
70b23f72e9SBrian Feldman typedef int (*uma_ctor)(void *mem, int size, void *arg, int flags);
718355f576SJeff Roberson 
728355f576SJeff Roberson /*
738355f576SJeff Roberson  * Item destructor
748355f576SJeff Roberson  *
758355f576SJeff Roberson  * Arguments:
768355f576SJeff Roberson  *	item  A pointer to the memory which has been allocated.
778355f576SJeff Roberson  *	size  The size of the item being destructed.
788355f576SJeff Roberson  *	arg   Argument passed through uma_zfree_arg
798355f576SJeff Roberson  *
808355f576SJeff Roberson  * Returns:
818355f576SJeff Roberson  *	Nothing
828355f576SJeff Roberson  *
838355f576SJeff Roberson  * Discussion:
848355f576SJeff Roberson  *	The destructor may perform operations that differ from those performed
858355f576SJeff Roberson  *	by the initializer, but it must leave the object in the same state.
868355f576SJeff Roberson  *	This IS type stable storage.  This is called after EVERY zfree call.
878355f576SJeff Roberson  */
888355f576SJeff Roberson typedef void (*uma_dtor)(void *mem, int size, void *arg);
898355f576SJeff Roberson 
908355f576SJeff Roberson /*
918355f576SJeff Roberson  * Item initializer
928355f576SJeff Roberson  *
938355f576SJeff Roberson  * Arguments:
948355f576SJeff Roberson  *	item  A pointer to the memory which has been allocated.
958355f576SJeff Roberson  *	size  The size of the item being initialized.
96b23f72e9SBrian Feldman  *	flags See zalloc flags
978355f576SJeff Roberson  *
988355f576SJeff Roberson  * Returns:
99b23f72e9SBrian Feldman  *	0      on success
100b23f72e9SBrian Feldman  *      errno  on failure
1018355f576SJeff Roberson  *
1028355f576SJeff Roberson  * Discussion:
1038355f576SJeff Roberson  *	The initializer is called when the memory is cached in the uma zone.
1042db63c5eSGiorgos Keramidas  *	The initializer and the destructor should leave the object in the same
1052db63c5eSGiorgos Keramidas  *	state.
1068355f576SJeff Roberson  */
107b23f72e9SBrian Feldman typedef int (*uma_init)(void *mem, int size, int flags);
1088355f576SJeff Roberson 
1098355f576SJeff Roberson /*
1108355f576SJeff Roberson  * Item discard function
1118355f576SJeff Roberson  *
1128355f576SJeff Roberson  * Arguments:
1138355f576SJeff Roberson  *	item  A pointer to memory which has been 'freed' but has not left the
1148355f576SJeff Roberson  *	      zone's cache.
1158355f576SJeff Roberson  *	size  The size of the item being discarded.
1168355f576SJeff Roberson  *
1178355f576SJeff Roberson  * Returns:
1188355f576SJeff Roberson  *	Nothing
1198355f576SJeff Roberson  *
1208355f576SJeff Roberson  * Discussion:
1218355f576SJeff Roberson  *	This routine is called when memory leaves a zone and is returned to the
1222db63c5eSGiorgos Keramidas  *	system for other uses.  It is the counter-part to the init function.
1238355f576SJeff Roberson  */
1248355f576SJeff Roberson typedef void (*uma_fini)(void *mem, int size);
1258355f576SJeff Roberson 
1268355f576SJeff Roberson /*
127*0095a784SJeff Roberson  * Import new memory into a cache zone.
128*0095a784SJeff Roberson  */
129*0095a784SJeff Roberson typedef int (*uma_import)(void *arg, void **store, int count, int flags);
130*0095a784SJeff Roberson 
131*0095a784SJeff Roberson /*
132*0095a784SJeff Roberson  * Free memory from a cache zone.
133*0095a784SJeff Roberson  */
134*0095a784SJeff Roberson typedef void (*uma_release)(void *arg, void **store, int count);
135*0095a784SJeff Roberson 
136*0095a784SJeff Roberson /*
1378355f576SJeff Roberson  * What's the difference between initializing and constructing?
1388355f576SJeff Roberson  *
1398355f576SJeff Roberson  * The item is initialized when it is cached, and this is the state that the
1408355f576SJeff Roberson  * object should be in when returned to the allocator. The purpose of this is
1418355f576SJeff Roberson  * to remove some code which would otherwise be called on each allocation by
1428355f576SJeff Roberson  * utilizing a known, stable state.  This differs from the constructor which
1438355f576SJeff Roberson  * will be called on EVERY allocation.
1448355f576SJeff Roberson  *
1452db63c5eSGiorgos Keramidas  * For example, in the initializer you may want to initialize embedded locks,
1468355f576SJeff Roberson  * NULL list pointers, set up initial states, magic numbers, etc.  This way if
14729b4d526SSheldon Hearn  * the object is held in the allocator and re-used it won't be necessary to
1488355f576SJeff Roberson  * re-initialize it.
1498355f576SJeff Roberson  *
1508355f576SJeff Roberson  * The constructor may be used to lock a data structure, link it on to lists,
1518355f576SJeff Roberson  * bump reference counts or total counts of outstanding structures, etc.
1528355f576SJeff Roberson  *
1538355f576SJeff Roberson  */
1548355f576SJeff Roberson 
1558355f576SJeff Roberson 
1568355f576SJeff Roberson /* Function proto types */
1578355f576SJeff Roberson 
1588355f576SJeff Roberson /*
1598355f576SJeff Roberson  * Create a new uma zone
1608355f576SJeff Roberson  *
1618355f576SJeff Roberson  * Arguments:
1622db63c5eSGiorgos Keramidas  *	name  The text name of the zone for debugging and stats. This memory
1638355f576SJeff Roberson  *		should not be freed until the zone has been deallocated.
1648355f576SJeff Roberson  *	size  The size of the object that is being created.
1652db63c5eSGiorgos Keramidas  *	ctor  The constructor that is called when the object is allocated.
1668355f576SJeff Roberson  *	dtor  The destructor that is called when the object is freed.
1678355f576SJeff Roberson  *	init  An initializer that sets up the initial state of the memory.
1688355f576SJeff Roberson  *	fini  A discard function that undoes initialization done by init.
1698355f576SJeff Roberson  *		ctor/dtor/init/fini may all be null, see notes above.
1702db63c5eSGiorgos Keramidas  *	align A bitmask that corresponds to the requested alignment
1718355f576SJeff Roberson  *		eg 4 would be 0x3
1722db63c5eSGiorgos Keramidas  *	flags A set of parameters that control the behavior of the zone.
1738355f576SJeff Roberson  *
1748355f576SJeff Roberson  * Returns:
1758355f576SJeff Roberson  *	A pointer to a structure which is intended to be opaque to users of
1768355f576SJeff Roberson  *	the interface.  The value may be null if the wait flag is not set.
1778355f576SJeff Roberson  */
178bb196eb4SMatthew D Fleming uma_zone_t uma_zcreate(const char *name, size_t size, uma_ctor ctor,
179bb196eb4SMatthew D Fleming 		    uma_dtor dtor, uma_init uminit, uma_fini fini,
18085dcf349SGleb Smirnoff 		    int align, uint32_t flags);
1818355f576SJeff Roberson 
182b60f5b79SJeff Roberson /*
183099a0e58SBosko Milekic  * Create a secondary uma zone
184099a0e58SBosko Milekic  *
185099a0e58SBosko Milekic  * Arguments:
1862db63c5eSGiorgos Keramidas  *	name  The text name of the zone for debugging and stats. This memory
187099a0e58SBosko Milekic  *		should not be freed until the zone has been deallocated.
1882db63c5eSGiorgos Keramidas  *	ctor  The constructor that is called when the object is allocated.
189099a0e58SBosko Milekic  *	dtor  The destructor that is called when the object is freed.
190099a0e58SBosko Milekic  *	zinit  An initializer that sets up the initial state of the memory
191099a0e58SBosko Milekic  *		as the object passes from the Keg's slab to the Zone's cache.
192099a0e58SBosko Milekic  *	zfini  A discard function that undoes initialization done by init
193099a0e58SBosko Milekic  *		as the object passes from the Zone's cache to the Keg's slab.
194099a0e58SBosko Milekic  *
195099a0e58SBosko Milekic  *		ctor/dtor/zinit/zfini may all be null, see notes above.
196099a0e58SBosko Milekic  *		Note that the zinit and zfini specified here are NOT
197099a0e58SBosko Milekic  *		exactly the same as the init/fini specified to uma_zcreate()
198099a0e58SBosko Milekic  *		when creating a master zone.  These zinit/zfini are called
199099a0e58SBosko Milekic  *		on the TRANSITION from keg to zone (and vice-versa). Once
200099a0e58SBosko Milekic  *		these are set, the primary zone may alter its init/fini
201099a0e58SBosko Milekic  *		(which are called when the object passes from VM to keg)
202099a0e58SBosko Milekic  *		using uma_zone_set_init/fini()) as well as its own
203099a0e58SBosko Milekic  *		zinit/zfini (unset by default for master zone) with
204099a0e58SBosko Milekic  *		uma_zone_set_zinit/zfini() (note subtle 'z' prefix).
205099a0e58SBosko Milekic  *
206b83e441bSBosko Milekic  *	master  A reference to this zone's Master Zone (Primary Zone),
207b83e441bSBosko Milekic  *		which contains the backing Keg for the Secondary Zone
208b83e441bSBosko Milekic  *		being added.
209099a0e58SBosko Milekic  *
210099a0e58SBosko Milekic  * Returns:
211099a0e58SBosko Milekic  *	A pointer to a structure which is intended to be opaque to users of
212099a0e58SBosko Milekic  *	the interface.  The value may be null if the wait flag is not set.
213099a0e58SBosko Milekic  */
214099a0e58SBosko Milekic uma_zone_t uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
215099a0e58SBosko Milekic 		    uma_init zinit, uma_fini zfini, uma_zone_t master);
216099a0e58SBosko Milekic 
217099a0e58SBosko Milekic /*
218e20a199fSJeff Roberson  * Add a second master to a secondary zone.  This provides multiple data
219e20a199fSJeff Roberson  * backends for objects with the same size.  Both masters must have
220e20a199fSJeff Roberson  * compatible allocation flags.  Presently, UMA_ZONE_MALLOC type zones are
221e20a199fSJeff Roberson  * the only supported.
222e20a199fSJeff Roberson  *
223e20a199fSJeff Roberson  * Returns:
224e20a199fSJeff Roberson  *	Error on failure, 0 on success.
225e20a199fSJeff Roberson  */
226e20a199fSJeff Roberson int uma_zsecond_add(uma_zone_t zone, uma_zone_t master);
227e20a199fSJeff Roberson 
228e20a199fSJeff Roberson /*
229*0095a784SJeff Roberson  * Create cache-only zones.
230*0095a784SJeff Roberson  *
231*0095a784SJeff Roberson  * This allows uma's per-cpu cache facilities to handle arbitrary
232*0095a784SJeff Roberson  * pointers.  Consumers must specify the import and release functions to
233*0095a784SJeff Roberson  * fill and destroy caches.  UMA does not allocate any memory for these
234*0095a784SJeff Roberson  * zones.  The 'arg' parameter is passed to import/release and is caller
235*0095a784SJeff Roberson  * specific.
236*0095a784SJeff Roberson  */
237*0095a784SJeff Roberson uma_zone_t uma_zcache_create(char *name, uma_ctor ctor, uma_dtor dtor,
238*0095a784SJeff Roberson 		    uma_init zinit, uma_fini zfini, uma_import zimport,
239*0095a784SJeff Roberson 		    uma_release zrelease, void *arg, int flags);
240*0095a784SJeff Roberson 
241*0095a784SJeff Roberson /*
242b60f5b79SJeff Roberson  * Definitions for uma_zcreate flags
243b60f5b79SJeff Roberson  *
244b60f5b79SJeff Roberson  * These flags share space with UMA_ZFLAGs in uma_int.h.  Be careful not to
2452018f30cSMike Silbersack  * overlap when adding new features.  0xf0000000 is in use by uma_int.h.
246b60f5b79SJeff Roberson  */
2478355f576SJeff Roberson #define UMA_ZONE_PAGEABLE	0x0001	/* Return items not fully backed by
2488355f576SJeff Roberson 					   physical memory XXX Not yet */
2498355f576SJeff Roberson #define UMA_ZONE_ZINIT		0x0002	/* Initialize with zeros */
2502db63c5eSGiorgos Keramidas #define UMA_ZONE_STATIC		0x0004	/* Statically sized zone */
2518355f576SJeff Roberson #define UMA_ZONE_OFFPAGE	0x0008	/* Force the slab structure allocation
2528355f576SJeff Roberson 					   off of the real memory */
2538355f576SJeff Roberson #define UMA_ZONE_MALLOC		0x0010	/* For use by malloc(9) only! */
2548355f576SJeff Roberson #define UMA_ZONE_NOFREE		0x0020	/* Do not free slabs of this type! */
25528bc4419SJeff Roberson #define UMA_ZONE_MTXCLASS	0x0040	/* Create a new lock class */
25699571dc3SJeff Roberson #define	UMA_ZONE_VM		0x0080	/*
25799571dc3SJeff Roberson 					 * Used for internal vm datastructures
25899571dc3SJeff Roberson 					 * only.
25999571dc3SJeff Roberson 					 */
26099571dc3SJeff Roberson #define	UMA_ZONE_HASH		0x0100	/*
26199571dc3SJeff Roberson 					 * Use a hash table instead of caching
26299571dc3SJeff Roberson 					 * information in the vm_page.
26399571dc3SJeff Roberson 					 */
264099a0e58SBosko Milekic #define	UMA_ZONE_SECONDARY	0x0200	/* Zone is a Secondary Zone */
265099a0e58SBosko Milekic #define	UMA_ZONE_REFCNT		0x0400	/* Allocate refcnts in slabs */
266099a0e58SBosko Milekic #define	UMA_ZONE_MAXBUCKET	0x0800	/* Use largest buckets */
267e20a199fSJeff Roberson #define	UMA_ZONE_CACHESPREAD	0x1000	/*
268e20a199fSJeff Roberson 					 * Spread memory start locations across
269e20a199fSJeff Roberson 					 * all possible cache lines.  May
270e20a199fSJeff Roberson 					 * require many virtually contiguous
271e20a199fSJeff Roberson 					 * backend pages and can fail early.
272e20a199fSJeff Roberson 					 */
273e20a199fSJeff Roberson #define	UMA_ZONE_VTOSLAB	0x2000	/* Zone uses vtoslab for lookup. */
274263811f7SKip Macy #define	UMA_ZONE_NODUMP		0x4000	/*
275263811f7SKip Macy 					 * Zone's pages will not be included in
276263811f7SKip Macy 					 * mini-dumps.
277263811f7SKip Macy 					 */
278ad97af7eSGleb Smirnoff #define	UMA_ZONE_PCPU		0x8000	/*
279ad97af7eSGleb Smirnoff 					 * Allocates mp_ncpus slabs sized to
280ad97af7eSGleb Smirnoff 					 * sizeof(struct pcpu).
281ad97af7eSGleb Smirnoff 					 */
282e20a199fSJeff Roberson 
283e20a199fSJeff Roberson /*
284e20a199fSJeff Roberson  * These flags are shared between the keg and zone.  In zones wishing to add
285e20a199fSJeff Roberson  * new kegs these flags must be compatible.  Some are determined based on
286e20a199fSJeff Roberson  * physical parameters of the request and may not be provided by the consumer.
287e20a199fSJeff Roberson  */
288e20a199fSJeff Roberson #define	UMA_ZONE_INHERIT						\
2898d689e04SGleb Smirnoff     (UMA_ZONE_OFFPAGE | UMA_ZONE_MALLOC | UMA_ZONE_NOFREE |		\
290ad97af7eSGleb Smirnoff     UMA_ZONE_HASH | UMA_ZONE_REFCNT | UMA_ZONE_VTOSLAB | UMA_ZONE_PCPU)
2918355f576SJeff Roberson 
2928355f576SJeff Roberson /* Definitions for align */
2938355f576SJeff Roberson #define UMA_ALIGN_PTR	(sizeof(void *) - 1)	/* Alignment fit for ptr */
2948355f576SJeff Roberson #define UMA_ALIGN_LONG	(sizeof(long) - 1)	/* "" long */
2958355f576SJeff Roberson #define UMA_ALIGN_INT	(sizeof(int) - 1)	/* "" int */
2968355f576SJeff Roberson #define UMA_ALIGN_SHORT	(sizeof(short) - 1)	/* "" short */
2978355f576SJeff Roberson #define UMA_ALIGN_CHAR	(sizeof(char) - 1)	/* "" char */
2981e319f6dSRobert Watson #define UMA_ALIGN_CACHE	(0 - 1)			/* Cache line size align */
2998355f576SJeff Roberson 
3008355f576SJeff Roberson /*
3019c2cd7e5SJeff Roberson  * Destroys an empty uma zone.  If the zone is not empty uma complains loudly.
3028355f576SJeff Roberson  *
3038355f576SJeff Roberson  * Arguments:
3048355f576SJeff Roberson  *	zone  The zone we want to destroy.
3058355f576SJeff Roberson  *
3068355f576SJeff Roberson  */
3079c2cd7e5SJeff Roberson void uma_zdestroy(uma_zone_t zone);
3088355f576SJeff Roberson 
3098355f576SJeff Roberson /*
3108355f576SJeff Roberson  * Allocates an item out of a zone
3118355f576SJeff Roberson  *
3128355f576SJeff Roberson  * Arguments:
3138355f576SJeff Roberson  *	zone  The zone we are allocating from
3148355f576SJeff Roberson  *	arg   This data is passed to the ctor function
3152cc35ff9SJeff Roberson  *	flags See sys/malloc.h for available flags.
3168355f576SJeff Roberson  *
3178355f576SJeff Roberson  * Returns:
3182db63c5eSGiorgos Keramidas  *	A non-null pointer to an initialized element from the zone is
3192db63c5eSGiorgos Keramidas  *	guaranteed if the wait flag is M_WAITOK.  Otherwise a null pointer
3202db63c5eSGiorgos Keramidas  *	may be returned if the zone is empty or the ctor failed.
3218355f576SJeff Roberson  */
3228355f576SJeff Roberson 
3232cc35ff9SJeff Roberson void *uma_zalloc_arg(uma_zone_t zone, void *arg, int flags);
3248355f576SJeff Roberson 
3258355f576SJeff Roberson /*
3268355f576SJeff Roberson  * Allocates an item out of a zone without supplying an argument
3278355f576SJeff Roberson  *
3288355f576SJeff Roberson  * This is just a wrapper for uma_zalloc_arg for convenience.
3298355f576SJeff Roberson  *
3308355f576SJeff Roberson  */
3312cc35ff9SJeff Roberson static __inline void *uma_zalloc(uma_zone_t zone, int flags);
3328355f576SJeff Roberson 
3338355f576SJeff Roberson static __inline void *
3342cc35ff9SJeff Roberson uma_zalloc(uma_zone_t zone, int flags)
3358355f576SJeff Roberson {
3362cc35ff9SJeff Roberson 	return uma_zalloc_arg(zone, NULL, flags);
3378355f576SJeff Roberson }
3388355f576SJeff Roberson 
3398355f576SJeff Roberson /*
3408355f576SJeff Roberson  * Frees an item back into the specified zone.
3418355f576SJeff Roberson  *
3428355f576SJeff Roberson  * Arguments:
3438355f576SJeff Roberson  *	zone  The zone the item was originally allocated out of.
3448355f576SJeff Roberson  *	item  The memory to be freed.
3458355f576SJeff Roberson  *	arg   Argument passed to the destructor
3468355f576SJeff Roberson  *
3478355f576SJeff Roberson  * Returns:
3488355f576SJeff Roberson  *	Nothing.
3498355f576SJeff Roberson  */
3508355f576SJeff Roberson 
3518355f576SJeff Roberson void uma_zfree_arg(uma_zone_t zone, void *item, void *arg);
3528355f576SJeff Roberson 
3538355f576SJeff Roberson /*
3548355f576SJeff Roberson  * Frees an item back to a zone without supplying an argument
3558355f576SJeff Roberson  *
3568355f576SJeff Roberson  * This is just a wrapper for uma_zfree_arg for convenience.
3578355f576SJeff Roberson  *
3588355f576SJeff Roberson  */
3598355f576SJeff Roberson static __inline void uma_zfree(uma_zone_t zone, void *item);
3608355f576SJeff Roberson 
3618355f576SJeff Roberson static __inline void
3628355f576SJeff Roberson uma_zfree(uma_zone_t zone, void *item)
3638355f576SJeff Roberson {
364f6e34b82SMark Murray 	uma_zfree_arg(zone, item, NULL);
3658355f576SJeff Roberson }
3668355f576SJeff Roberson 
3678355f576SJeff Roberson /*
3688355f576SJeff Roberson  * XXX The rest of the prototypes in this header are h0h0 magic for the VM.
3698355f576SJeff Roberson  * If you think you need to use it for a normal zone you're probably incorrect.
3708355f576SJeff Roberson  */
3718355f576SJeff Roberson 
3728355f576SJeff Roberson /*
3738355f576SJeff Roberson  * Backend page supplier routines
3748355f576SJeff Roberson  *
3758355f576SJeff Roberson  * Arguments:
3762db63c5eSGiorgos Keramidas  *	zone  The zone that is requesting pages.
3772db63c5eSGiorgos Keramidas  *	size  The number of bytes being requested.
3788355f576SJeff Roberson  *	pflag Flags for these memory pages, see below.
3798355f576SJeff Roberson  *	wait  Indicates our willingness to block.
3808355f576SJeff Roberson  *
3818355f576SJeff Roberson  * Returns:
3822db63c5eSGiorgos Keramidas  *	A pointer to the allocated memory or NULL on failure.
3838355f576SJeff Roberson  */
3848355f576SJeff Roberson 
38585dcf349SGleb Smirnoff typedef void *(*uma_alloc)(uma_zone_t zone, int size, uint8_t *pflag, int wait);
3868355f576SJeff Roberson 
3878355f576SJeff Roberson /*
3888355f576SJeff Roberson  * Backend page free routines
3898355f576SJeff Roberson  *
3908355f576SJeff Roberson  * Arguments:
3912db63c5eSGiorgos Keramidas  *	item  A pointer to the previously allocated pages.
3922db63c5eSGiorgos Keramidas  *	size  The original size of the allocation.
3932db63c5eSGiorgos Keramidas  *	pflag The flags for the slab.  See UMA_SLAB_* below.
3948355f576SJeff Roberson  *
3958355f576SJeff Roberson  * Returns:
3968355f576SJeff Roberson  *	None
3978355f576SJeff Roberson  */
39885dcf349SGleb Smirnoff typedef void (*uma_free)(void *item, int size, uint8_t pflag);
3998355f576SJeff Roberson 
4008355f576SJeff Roberson 
4018355f576SJeff Roberson 
4028355f576SJeff Roberson /*
4038355f576SJeff Roberson  * Sets up the uma allocator. (Called by vm_mem_init)
4048355f576SJeff Roberson  *
4058355f576SJeff Roberson  * Arguments:
4068355f576SJeff Roberson  *	bootmem  A pointer to memory used to bootstrap the system.
4078355f576SJeff Roberson  *
4088355f576SJeff Roberson  * Returns:
4098355f576SJeff Roberson  *	Nothing
4108355f576SJeff Roberson  *
4118355f576SJeff Roberson  * Discussion:
4128355f576SJeff Roberson  *	This memory is used for zones which allocate things before the
4138355f576SJeff Roberson  *	backend page supplier can give us pages.  It should be
4143803b26bSDag-Erling Smørgrav  *	UMA_SLAB_SIZE * boot_pages bytes. (see uma_int.h)
4158355f576SJeff Roberson  *
4168355f576SJeff Roberson  */
4178355f576SJeff Roberson 
4183803b26bSDag-Erling Smørgrav void uma_startup(void *bootmem, int boot_pages);
4198355f576SJeff Roberson 
4208355f576SJeff Roberson /*
4218355f576SJeff Roberson  * Finishes starting up the allocator.  This should
4228355f576SJeff Roberson  * be called when kva is ready for normal allocs.
4238355f576SJeff Roberson  *
4248355f576SJeff Roberson  * Arguments:
42599571dc3SJeff Roberson  *	None
4268355f576SJeff Roberson  *
4278355f576SJeff Roberson  * Returns:
4288355f576SJeff Roberson  *	Nothing
4298355f576SJeff Roberson  *
4308355f576SJeff Roberson  * Discussion:
43199571dc3SJeff Roberson  *	uma_startup2 is called by kmeminit() to enable us of uma for malloc.
4328355f576SJeff Roberson  */
4338355f576SJeff Roberson 
43499571dc3SJeff Roberson void uma_startup2(void);
4358355f576SJeff Roberson 
4368355f576SJeff Roberson /*
4378355f576SJeff Roberson  * Reclaims unused memory for all zones
4388355f576SJeff Roberson  *
4398355f576SJeff Roberson  * Arguments:
4408355f576SJeff Roberson  *	None
4418355f576SJeff Roberson  * Returns:
4428355f576SJeff Roberson  *	None
4438355f576SJeff Roberson  *
4448355f576SJeff Roberson  * This should only be called by the page out daemon.
4458355f576SJeff Roberson  */
4468355f576SJeff Roberson 
4478355f576SJeff Roberson void uma_reclaim(void);
4488355f576SJeff Roberson 
4498355f576SJeff Roberson /*
4501e319f6dSRobert Watson  * Sets the alignment mask to be used for all zones requesting cache
4511e319f6dSRobert Watson  * alignment.  Should be called by MD boot code prior to starting VM/UMA.
4521e319f6dSRobert Watson  *
4531e319f6dSRobert Watson  * Arguments:
4541e319f6dSRobert Watson  *	align The alignment mask
4551e319f6dSRobert Watson  *
4561e319f6dSRobert Watson  * Returns:
4571e319f6dSRobert Watson  *	Nothing
4581e319f6dSRobert Watson  */
4591e319f6dSRobert Watson void uma_set_align(int align);
4601e319f6dSRobert Watson 
4611e319f6dSRobert Watson /*
462a4915c21SAttilio Rao  * Reserves the maximum KVA space required by the zone and configures the zone
463a4915c21SAttilio Rao  * to use a VM_ALLOC_NOOBJ-based backend allocator.
4648355f576SJeff Roberson  *
4658355f576SJeff Roberson  * Arguments:
4662db63c5eSGiorgos Keramidas  *	zone  The zone to update.
467a4915c21SAttilio Rao  *	nitems  The upper limit on the number of items that can be allocated.
4688355f576SJeff Roberson  *
4698355f576SJeff Roberson  * Returns:
470a4915c21SAttilio Rao  *	0  if KVA space can not be allocated
4718355f576SJeff Roberson  *	1  if successful
4728355f576SJeff Roberson  *
4738355f576SJeff Roberson  * Discussion:
474a4915c21SAttilio Rao  *	When the machine supports a direct map and the zone's items are smaller
475a4915c21SAttilio Rao  *	than a page, the zone will use the direct map instead of allocating KVA
476a4915c21SAttilio Rao  *	space.
4778355f576SJeff Roberson  */
478a4915c21SAttilio Rao int uma_zone_reserve_kva(uma_zone_t zone, int nitems);
4798355f576SJeff Roberson 
480736ee590SJeff Roberson /*
481736ee590SJeff Roberson  * Sets a high limit on the number of items allowed in a zone
482736ee590SJeff Roberson  *
483736ee590SJeff Roberson  * Arguments:
484736ee590SJeff Roberson  *	zone  The zone to limit
4851c6cae97SLawrence Stewart  *	nitems  The requested upper limit on the number of items allowed
486736ee590SJeff Roberson  *
487736ee590SJeff Roberson  * Returns:
4881c6cae97SLawrence Stewart  *	int  The effective value of nitems after rounding up based on page size
489736ee590SJeff Roberson  */
4901c6cae97SLawrence Stewart int uma_zone_set_max(uma_zone_t zone, int nitems);
4918355f576SJeff Roberson 
4928355f576SJeff Roberson /*
493e49471b0SAndre Oppermann  * Obtains the effective limit on the number of items in a zone
494e49471b0SAndre Oppermann  *
495e49471b0SAndre Oppermann  * Arguments:
496e49471b0SAndre Oppermann  *	zone  The zone to obtain the effective limit from
497e49471b0SAndre Oppermann  *
498e49471b0SAndre Oppermann  * Return:
499e49471b0SAndre Oppermann  *	0  No limit
500e49471b0SAndre Oppermann  *	int  The effective limit of the zone
501e49471b0SAndre Oppermann  */
502e49471b0SAndre Oppermann int uma_zone_get_max(uma_zone_t zone);
503e49471b0SAndre Oppermann 
504e49471b0SAndre Oppermann /*
5052f891cd5SPawel Jakub Dawidek  * Sets a warning to be printed when limit is reached
5062f891cd5SPawel Jakub Dawidek  *
5072f891cd5SPawel Jakub Dawidek  * Arguments:
5082f891cd5SPawel Jakub Dawidek  *	zone  The zone we will warn about
5092f891cd5SPawel Jakub Dawidek  *	warning  Warning content
5102f891cd5SPawel Jakub Dawidek  *
5112f891cd5SPawel Jakub Dawidek  * Returns:
5122f891cd5SPawel Jakub Dawidek  *	Nothing
5132f891cd5SPawel Jakub Dawidek  */
5142f891cd5SPawel Jakub Dawidek void uma_zone_set_warning(uma_zone_t zone, const char *warning);
5152f891cd5SPawel Jakub Dawidek 
5162f891cd5SPawel Jakub Dawidek /*
517c4ae7908SLawrence Stewart  * Obtains the approximate current number of items allocated from a zone
518c4ae7908SLawrence Stewart  *
519c4ae7908SLawrence Stewart  * Arguments:
520c4ae7908SLawrence Stewart  *	zone  The zone to obtain the current allocation count from
521c4ae7908SLawrence Stewart  *
522c4ae7908SLawrence Stewart  * Return:
523c4ae7908SLawrence Stewart  *	int  The approximate current number of items allocated from the zone
524c4ae7908SLawrence Stewart  */
525c4ae7908SLawrence Stewart int uma_zone_get_cur(uma_zone_t zone);
526c4ae7908SLawrence Stewart 
527c4ae7908SLawrence Stewart /*
528099a0e58SBosko Milekic  * The following two routines (uma_zone_set_init/fini)
529099a0e58SBosko Milekic  * are used to set the backend init/fini pair which acts on an
530099a0e58SBosko Milekic  * object as it becomes allocated and is placed in a slab within
531099a0e58SBosko Milekic  * the specified zone's backing keg.  These should probably not
5322db63c5eSGiorgos Keramidas  * be changed once allocations have already begun, but only be set
533099a0e58SBosko Milekic  * immediately upon zone creation.
534099a0e58SBosko Milekic  */
535099a0e58SBosko Milekic void uma_zone_set_init(uma_zone_t zone, uma_init uminit);
536099a0e58SBosko Milekic void uma_zone_set_fini(uma_zone_t zone, uma_fini fini);
537099a0e58SBosko Milekic 
538099a0e58SBosko Milekic /*
539099a0e58SBosko Milekic  * The following two routines (uma_zone_set_zinit/zfini) are
540099a0e58SBosko Milekic  * used to set the zinit/zfini pair which acts on an object as
541099a0e58SBosko Milekic  * it passes from the backing Keg's slab cache to the
542099a0e58SBosko Milekic  * specified Zone's bucket cache.  These should probably not
5432db63c5eSGiorgos Keramidas  * be changed once allocations have already begun, but only be set
5442db63c5eSGiorgos Keramidas  * immediately upon zone creation.
545099a0e58SBosko Milekic  */
546099a0e58SBosko Milekic void uma_zone_set_zinit(uma_zone_t zone, uma_init zinit);
547099a0e58SBosko Milekic void uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini);
548099a0e58SBosko Milekic 
549099a0e58SBosko Milekic /*
550a4915c21SAttilio Rao  * Replaces the standard backend allocator for this zone.
5518355f576SJeff Roberson  *
5528355f576SJeff Roberson  * Arguments:
5532db63c5eSGiorgos Keramidas  *	zone   The zone whose backend allocator is being changed.
5548355f576SJeff Roberson  *	allocf A pointer to the allocation function
5558355f576SJeff Roberson  *
5568355f576SJeff Roberson  * Returns:
5578355f576SJeff Roberson  *	Nothing
5588355f576SJeff Roberson  *
5598355f576SJeff Roberson  * Discussion:
5608355f576SJeff Roberson  *	This could be used to implement pageable allocation, or perhaps
5618355f576SJeff Roberson  *	even DMA allocators if used in conjunction with the OFFPAGE
5628355f576SJeff Roberson  *	zone flag.
5638355f576SJeff Roberson  */
5648355f576SJeff Roberson 
5658355f576SJeff Roberson void uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf);
5668355f576SJeff Roberson 
5678355f576SJeff Roberson /*
5688355f576SJeff Roberson  * Used for freeing memory provided by the allocf above
5698355f576SJeff Roberson  *
5708355f576SJeff Roberson  * Arguments:
5718355f576SJeff Roberson  *	zone  The zone that intends to use this free routine.
5728355f576SJeff Roberson  *	freef The page freeing routine.
5738355f576SJeff Roberson  *
5748355f576SJeff Roberson  * Returns:
5758355f576SJeff Roberson  *	Nothing
5768355f576SJeff Roberson  */
5778355f576SJeff Roberson 
5788355f576SJeff Roberson void uma_zone_set_freef(uma_zone_t zone, uma_free freef);
5798355f576SJeff Roberson 
5808355f576SJeff Roberson /*
5812db63c5eSGiorgos Keramidas  * These flags are setable in the allocf and visible in the freef.
5828355f576SJeff Roberson  */
5838355f576SJeff Roberson #define UMA_SLAB_BOOT	0x01		/* Slab alloced from boot pages */
5848355f576SJeff Roberson #define UMA_SLAB_KMEM	0x02		/* Slab alloced from kmem_map */
5857630c265SAlan Cox #define UMA_SLAB_KERNEL	0x04		/* Slab alloced from kernel_map */
5868355f576SJeff Roberson #define UMA_SLAB_PRIV	0x08		/* Slab alloced from priv allocator */
587c235bfa5SJeff Roberson #define UMA_SLAB_OFFP	0x10		/* Slab is managed separately  */
5888355f576SJeff Roberson #define UMA_SLAB_MALLOC	0x20		/* Slab is a large malloc slab */
5898355f576SJeff Roberson /* 0x40 and 0x80 are available */
5908355f576SJeff Roberson 
5918355f576SJeff Roberson /*
5928355f576SJeff Roberson  * Used to pre-fill a zone with some number of items
5938355f576SJeff Roberson  *
5948355f576SJeff Roberson  * Arguments:
5958355f576SJeff Roberson  *	zone    The zone to fill
5968355f576SJeff Roberson  *	itemcnt The number of items to reserve
5978355f576SJeff Roberson  *
5988355f576SJeff Roberson  * Returns:
5998355f576SJeff Roberson  *	Nothing
6008355f576SJeff Roberson  *
6018355f576SJeff Roberson  * NOTE: This is blocking and should only be done at startup
6028355f576SJeff Roberson  */
6038355f576SJeff Roberson void uma_prealloc(uma_zone_t zone, int itemcnt);
6048355f576SJeff Roberson 
605099a0e58SBosko Milekic /*
606099a0e58SBosko Milekic  * Used to lookup the reference counter allocated for an item
607099a0e58SBosko Milekic  * from a UMA_ZONE_REFCNT zone.  For UMA_ZONE_REFCNT zones,
608099a0e58SBosko Milekic  * reference counters are allocated for items and stored in
609099a0e58SBosko Milekic  * the underlying slab header.
610099a0e58SBosko Milekic  *
611099a0e58SBosko Milekic  * Arguments:
612099a0e58SBosko Milekic  *	zone  The UMA_ZONE_REFCNT zone to which the item belongs.
613099a0e58SBosko Milekic  *	item  The address of the item for which we want a refcnt.
614099a0e58SBosko Milekic  *
615099a0e58SBosko Milekic  * Returns:
61685dcf349SGleb Smirnoff  *	A pointer to a uint32_t reference counter.
617099a0e58SBosko Milekic  */
61885dcf349SGleb Smirnoff uint32_t *uma_find_refcnt(uma_zone_t zone, void *item);
6198355f576SJeff Roberson 
6207a52a97eSRobert Watson /*
621663b416fSJohn Baldwin  * Used to determine if a fixed-size zone is exhausted.
622663b416fSJohn Baldwin  *
623663b416fSJohn Baldwin  * Arguments:
624663b416fSJohn Baldwin  *	zone    The zone to check
625663b416fSJohn Baldwin  *
626663b416fSJohn Baldwin  * Returns:
627663b416fSJohn Baldwin  *	Non-zero if zone is exhausted.
628663b416fSJohn Baldwin  */
629663b416fSJohn Baldwin int uma_zone_exhausted(uma_zone_t zone);
6306c125b8dSMohan Srinivasan int uma_zone_exhausted_nolock(uma_zone_t zone);
631663b416fSJohn Baldwin 
632663b416fSJohn Baldwin /*
6337a52a97eSRobert Watson  * Exported statistics structures to be used by user space monitoring tools.
6342db63c5eSGiorgos Keramidas  * Statistics stream consists of a uma_stream_header, followed by a series of
6352db63c5eSGiorgos Keramidas  * alternative uma_type_header and uma_type_stat structures.
6367a52a97eSRobert Watson  */
6377a52a97eSRobert Watson #define	UMA_STREAM_VERSION	0x00000001
6387a52a97eSRobert Watson struct uma_stream_header {
63985dcf349SGleb Smirnoff 	uint32_t	ush_version;	/* Stream format version. */
64085dcf349SGleb Smirnoff 	uint32_t	ush_maxcpus;	/* Value of MAXCPU for stream. */
64185dcf349SGleb Smirnoff 	uint32_t	ush_count;	/* Number of records. */
64285dcf349SGleb Smirnoff 	uint32_t	_ush_pad;	/* Pad/reserved field. */
6437a52a97eSRobert Watson };
6447a52a97eSRobert Watson 
645cbbb4a00SRobert Watson #define	UTH_MAX_NAME	32
646cbbb4a00SRobert Watson #define	UTH_ZONE_SECONDARY	0x00000001
6477a52a97eSRobert Watson struct uma_type_header {
6487a52a97eSRobert Watson 	/*
6497a52a97eSRobert Watson 	 * Static per-zone data, some extracted from the supporting keg.
6507a52a97eSRobert Watson 	 */
651cbbb4a00SRobert Watson 	char		uth_name[UTH_MAX_NAME];
65285dcf349SGleb Smirnoff 	uint32_t	uth_align;	/* Keg: alignment. */
65385dcf349SGleb Smirnoff 	uint32_t	uth_size;	/* Keg: requested size of item. */
65485dcf349SGleb Smirnoff 	uint32_t	uth_rsize;	/* Keg: real size of item. */
65585dcf349SGleb Smirnoff 	uint32_t	uth_maxpages;	/* Keg: maximum number of pages. */
65685dcf349SGleb Smirnoff 	uint32_t	uth_limit;	/* Keg: max items to allocate. */
6577a52a97eSRobert Watson 
6587a52a97eSRobert Watson 	/*
6597a52a97eSRobert Watson 	 * Current dynamic zone/keg-derived statistics.
6607a52a97eSRobert Watson 	 */
66185dcf349SGleb Smirnoff 	uint32_t	uth_pages;	/* Keg: pages allocated. */
66285dcf349SGleb Smirnoff 	uint32_t	uth_keg_free;	/* Keg: items free. */
66385dcf349SGleb Smirnoff 	uint32_t	uth_zone_free;	/* Zone: items free. */
66485dcf349SGleb Smirnoff 	uint32_t	uth_bucketsize;	/* Zone: desired bucket size. */
66585dcf349SGleb Smirnoff 	uint32_t	uth_zone_flags;	/* Zone: flags. */
66685dcf349SGleb Smirnoff 	uint64_t	uth_allocs;	/* Zone: number of allocations. */
66785dcf349SGleb Smirnoff 	uint64_t	uth_frees;	/* Zone: number of frees. */
66885dcf349SGleb Smirnoff 	uint64_t	uth_fails;	/* Zone: number of alloc failures. */
66985dcf349SGleb Smirnoff 	uint64_t	uth_sleeps;	/* Zone: number of alloc sleeps. */
67085dcf349SGleb Smirnoff 	uint64_t	_uth_reserved1[2];	/* Reserved. */
6717a52a97eSRobert Watson };
6727a52a97eSRobert Watson 
6737a52a97eSRobert Watson struct uma_percpu_stat {
67485dcf349SGleb Smirnoff 	uint64_t	ups_allocs;	/* Cache: number of allocations. */
67585dcf349SGleb Smirnoff 	uint64_t	ups_frees;	/* Cache: number of frees. */
67685dcf349SGleb Smirnoff 	uint64_t	ups_cache_free;	/* Cache: free items in cache. */
67785dcf349SGleb Smirnoff 	uint64_t	_ups_reserved[5];	/* Reserved. */
6787a52a97eSRobert Watson };
6797a52a97eSRobert Watson 
6808355f576SJeff Roberson #endif
681