1139825Simp/*-
2148078Srwatson * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org>
3148078Srwatson * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
4148078Srwatson * All rights reserved.
592654Sjeff *
692654Sjeff * Redistribution and use in source and binary forms, with or without
792654Sjeff * modification, are permitted provided that the following conditions
892654Sjeff * are met:
992654Sjeff * 1. Redistributions of source code must retain the above copyright
1092654Sjeff *    notice unmodified, this list of conditions, and the following
1192654Sjeff *    disclaimer.
1292654Sjeff * 2. Redistributions in binary form must reproduce the above copyright
1392654Sjeff *    notice, this list of conditions and the following disclaimer in the
1492654Sjeff *    documentation and/or other materials provided with the distribution.
1592654Sjeff *
1692654Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1792654Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1892654Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1992654Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2092654Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2192654Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2292654Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2392654Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2492654Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2592654Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2692654Sjeff *
2792654Sjeff * $FreeBSD: stable/10/sys/vm/uma.h 324602 2017-10-13 17:11:08Z jhb $
2892654Sjeff *
2992654Sjeff */
3092654Sjeff
3192654Sjeff/*
3292654Sjeff * uma.h - External definitions for the Universal Memory Allocator
3392654Sjeff *
3492654Sjeff*/
3592654Sjeff
36262739Sglebius#ifndef _VM_UMA_H_
37262739Sglebius#define _VM_UMA_H_
3892654Sjeff
3992654Sjeff#include <sys/param.h>		/* For NULL */
4092654Sjeff#include <sys/malloc.h>		/* For M_* */
4192654Sjeff
42184546Skeramida/* User visible parameters */
4392654Sjeff#define UMA_SMALLEST_UNIT       (PAGE_SIZE / 256) /* Smallest item allocated */
4492654Sjeff
4592654Sjeff/* Types and type defs */
4692654Sjeff
47129906Sbmilekicstruct uma_zone;
4892654Sjeff/* Opaque type used as a handle to the zone */
4992654Sjefftypedef struct uma_zone * uma_zone_t;
5092654Sjeff
51166213Smohansvoid zone_drain(uma_zone_t);
52166213Smohans
53244024Spjd/*
5492654Sjeff * Item constructor
5592654Sjeff *
5692654Sjeff * Arguments:
5792654Sjeff *	item  A pointer to the memory which has been allocated.
5892654Sjeff *	arg   The arg field passed to uma_zalloc_arg
5992654Sjeff *	size  The size of the allocated item
60132987Sgreen *	flags See zalloc flags
61244024Spjd *
6292654Sjeff * Returns:
63132987Sgreen *	0      on success
64132987Sgreen *      errno  on failure
6592654Sjeff *
6692654Sjeff * Discussion:
6792654Sjeff *	The constructor is called just before the memory is returned
68105689Ssheldonh *	to the user. It may block if necessary.
6992654Sjeff */
70132987Sgreentypedef int (*uma_ctor)(void *mem, int size, void *arg, int flags);
7192654Sjeff
7292654Sjeff/*
7392654Sjeff * Item destructor
7492654Sjeff *
7592654Sjeff * Arguments:
7692654Sjeff *	item  A pointer to the memory which has been allocated.
7792654Sjeff *	size  The size of the item being destructed.
7892654Sjeff *	arg   Argument passed through uma_zfree_arg
79244024Spjd *
8092654Sjeff * Returns:
8192654Sjeff *	Nothing
8292654Sjeff *
8392654Sjeff * Discussion:
8492654Sjeff *	The destructor may perform operations that differ from those performed
8592654Sjeff *	by the initializer, but it must leave the object in the same state.
8692654Sjeff *	This IS type stable storage.  This is called after EVERY zfree call.
8792654Sjeff */
8892654Sjefftypedef void (*uma_dtor)(void *mem, int size, void *arg);
8992654Sjeff
90244024Spjd/*
9192654Sjeff * Item initializer
9292654Sjeff *
9392654Sjeff * Arguments:
9492654Sjeff *	item  A pointer to the memory which has been allocated.
9592654Sjeff *	size  The size of the item being initialized.
96132987Sgreen *	flags See zalloc flags
97244024Spjd *
9892654Sjeff * Returns:
99132987Sgreen *	0      on success
100132987Sgreen *      errno  on failure
10192654Sjeff *
10292654Sjeff * Discussion:
103244024Spjd *	The initializer is called when the memory is cached in the uma zone.
104184546Skeramida *	The initializer and the destructor should leave the object in the same
105184546Skeramida *	state.
10692654Sjeff */
107132987Sgreentypedef int (*uma_init)(void *mem, int size, int flags);
10892654Sjeff
10992654Sjeff/*
11092654Sjeff * Item discard function
11192654Sjeff *
11292654Sjeff * Arguments:
113244024Spjd *	item  A pointer to memory which has been 'freed' but has not left the
11492654Sjeff *	      zone's cache.
11592654Sjeff *	size  The size of the item being discarded.
11692654Sjeff *
11792654Sjeff * Returns:
11892654Sjeff *	Nothing
11992654Sjeff *
12092654Sjeff * Discussion:
12192654Sjeff *	This routine is called when memory leaves a zone and is returned to the
122184546Skeramida *	system for other uses.  It is the counter-part to the init function.
12392654Sjeff */
12492654Sjefftypedef void (*uma_fini)(void *mem, int size);
12592654Sjeff
12692654Sjeff/*
127251826Sjeff * Import new memory into a cache zone.
128251826Sjeff */
129251826Sjefftypedef int (*uma_import)(void *arg, void **store, int count, int flags);
130251826Sjeff
131251826Sjeff/*
132251826Sjeff * Free memory from a cache zone.
133251826Sjeff */
134251826Sjefftypedef void (*uma_release)(void *arg, void **store, int count);
135251826Sjeff
136251826Sjeff/*
13792654Sjeff * What's the difference between initializing and constructing?
13892654Sjeff *
139244024Spjd * The item is initialized when it is cached, and this is the state that the
14092654Sjeff * object should be in when returned to the allocator. The purpose of this is
14192654Sjeff * to remove some code which would otherwise be called on each allocation by
14292654Sjeff * utilizing a known, stable state.  This differs from the constructor which
14392654Sjeff * will be called on EVERY allocation.
14492654Sjeff *
145184546Skeramida * For example, in the initializer you may want to initialize embedded locks,
14692654Sjeff * NULL list pointers, set up initial states, magic numbers, etc.  This way if
147105689Ssheldonh * the object is held in the allocator and re-used it won't be necessary to
14892654Sjeff * re-initialize it.
14992654Sjeff *
15092654Sjeff * The constructor may be used to lock a data structure, link it on to lists,
15192654Sjeff * bump reference counts or total counts of outstanding structures, etc.
15292654Sjeff *
15392654Sjeff */
15492654Sjeff
15592654Sjeff
15692654Sjeff/* Function proto types */
15792654Sjeff
15892654Sjeff/*
15992654Sjeff * Create a new uma zone
16092654Sjeff *
16192654Sjeff * Arguments:
162184546Skeramida *	name  The text name of the zone for debugging and stats. This memory
16392654Sjeff *		should not be freed until the zone has been deallocated.
16492654Sjeff *	size  The size of the object that is being created.
165184546Skeramida *	ctor  The constructor that is called when the object is allocated.
16692654Sjeff *	dtor  The destructor that is called when the object is freed.
16792654Sjeff *	init  An initializer that sets up the initial state of the memory.
16892654Sjeff *	fini  A discard function that undoes initialization done by init.
16992654Sjeff *		ctor/dtor/init/fini may all be null, see notes above.
170184546Skeramida *	align A bitmask that corresponds to the requested alignment
17192654Sjeff *		eg 4 would be 0x3
172184546Skeramida *	flags A set of parameters that control the behavior of the zone.
17392654Sjeff *
17492654Sjeff * Returns:
17592654Sjeff *	A pointer to a structure which is intended to be opaque to users of
17692654Sjeff *	the interface.  The value may be null if the wait flag is not set.
17792654Sjeff */
178242152Smdfuma_zone_t uma_zcreate(const char *name, size_t size, uma_ctor ctor,
179242152Smdf		    uma_dtor dtor, uma_init uminit, uma_fini fini,
180249313Sglebius		    int align, uint32_t flags);
18192654Sjeff
182120223Sjeff/*
183129906Sbmilekic * Create a secondary uma zone
184129906Sbmilekic *
185129906Sbmilekic * Arguments:
186184546Skeramida *	name  The text name of the zone for debugging and stats. This memory
187129906Sbmilekic *		should not be freed until the zone has been deallocated.
188184546Skeramida *	ctor  The constructor that is called when the object is allocated.
189129906Sbmilekic *	dtor  The destructor that is called when the object is freed.
190129906Sbmilekic *	zinit  An initializer that sets up the initial state of the memory
191129906Sbmilekic *		as the object passes from the Keg's slab to the Zone's cache.
192129906Sbmilekic *	zfini  A discard function that undoes initialization done by init
193129906Sbmilekic *		as the object passes from the Zone's cache to the Keg's slab.
194129906Sbmilekic *
195129906Sbmilekic *		ctor/dtor/zinit/zfini may all be null, see notes above.
196129906Sbmilekic *		Note that the zinit and zfini specified here are NOT
197129906Sbmilekic *		exactly the same as the init/fini specified to uma_zcreate()
198129906Sbmilekic *		when creating a master zone.  These zinit/zfini are called
199129906Sbmilekic *		on the TRANSITION from keg to zone (and vice-versa). Once
200129906Sbmilekic *		these are set, the primary zone may alter its init/fini
201129906Sbmilekic *		(which are called when the object passes from VM to keg)
202129906Sbmilekic *		using uma_zone_set_init/fini()) as well as its own
203129906Sbmilekic *		zinit/zfini (unset by default for master zone) with
204129906Sbmilekic *		uma_zone_set_zinit/zfini() (note subtle 'z' prefix).
205129906Sbmilekic *
206129913Sbmilekic *	master  A reference to this zone's Master Zone (Primary Zone),
207129913Sbmilekic *		which contains the backing Keg for the Secondary Zone
208129913Sbmilekic *		being added.
209129906Sbmilekic *
210129906Sbmilekic * Returns:
211129906Sbmilekic *	A pointer to a structure which is intended to be opaque to users of
212129906Sbmilekic *	the interface.  The value may be null if the wait flag is not set.
213129906Sbmilekic */
214129906Sbmilekicuma_zone_t uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
215129906Sbmilekic		    uma_init zinit, uma_fini zfini, uma_zone_t master);
216129906Sbmilekic
217129906Sbmilekic/*
218187681Sjeff * Add a second master to a secondary zone.  This provides multiple data
219187681Sjeff * backends for objects with the same size.  Both masters must have
220187681Sjeff * compatible allocation flags.  Presently, UMA_ZONE_MALLOC type zones are
221187681Sjeff * the only supported.
222187681Sjeff *
223187681Sjeff * Returns:
224244024Spjd *	Error on failure, 0 on success.
225187681Sjeff */
226187681Sjeffint uma_zsecond_add(uma_zone_t zone, uma_zone_t master);
227187681Sjeff
228187681Sjeff/*
229251826Sjeff * Create cache-only zones.
230251826Sjeff *
231251826Sjeff * This allows uma's per-cpu cache facilities to handle arbitrary
232251826Sjeff * pointers.  Consumers must specify the import and release functions to
233251826Sjeff * fill and destroy caches.  UMA does not allocate any memory for these
234251826Sjeff * zones.  The 'arg' parameter is passed to import/release and is caller
235251826Sjeff * specific.
236251826Sjeff */
237252040Sjeffuma_zone_t uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
238251826Sjeff		    uma_init zinit, uma_fini zfini, uma_import zimport,
239251826Sjeff		    uma_release zrelease, void *arg, int flags);
240251826Sjeff
241251826Sjeff/*
242120223Sjeff * Definitions for uma_zcreate flags
243120223Sjeff *
244120223Sjeff * These flags share space with UMA_ZFLAGs in uma_int.h.  Be careful not to
245148072Ssilby * overlap when adding new features.  0xf0000000 is in use by uma_int.h.
246120223Sjeff */
24792654Sjeff#define UMA_ZONE_PAGEABLE	0x0001	/* Return items not fully backed by
24892654Sjeff					   physical memory XXX Not yet */
24992654Sjeff#define UMA_ZONE_ZINIT		0x0002	/* Initialize with zeros */
250184546Skeramida#define UMA_ZONE_STATIC		0x0004	/* Statically sized zone */
25192654Sjeff#define UMA_ZONE_OFFPAGE	0x0008	/* Force the slab structure allocation
25292654Sjeff					   off of the real memory */
25392654Sjeff#define UMA_ZONE_MALLOC		0x0010	/* For use by malloc(9) only! */
25492654Sjeff#define UMA_ZONE_NOFREE		0x0020	/* Do not free slabs of this type! */
25595758Sjeff#define UMA_ZONE_MTXCLASS	0x0040	/* Create a new lock class */
256103531Sjeff#define	UMA_ZONE_VM		0x0080	/*
257103531Sjeff					 * Used for internal vm datastructures
258103531Sjeff					 * only.
259103531Sjeff					 */
260103531Sjeff#define	UMA_ZONE_HASH		0x0100	/*
261103531Sjeff					 * Use a hash table instead of caching
262103531Sjeff					 * information in the vm_page.
263103531Sjeff					 */
264129906Sbmilekic#define	UMA_ZONE_SECONDARY	0x0200	/* Zone is a Secondary Zone */
265129906Sbmilekic#define	UMA_ZONE_REFCNT		0x0400	/* Allocate refcnts in slabs */
266129906Sbmilekic#define	UMA_ZONE_MAXBUCKET	0x0800	/* Use largest buckets */
267187681Sjeff#define	UMA_ZONE_CACHESPREAD	0x1000	/*
268187681Sjeff					 * Spread memory start locations across
269187681Sjeff					 * all possible cache lines.  May
270187681Sjeff					 * require many virtually contiguous
271187681Sjeff					 * backend pages and can fail early.
272187681Sjeff					 */
273187681Sjeff#define	UMA_ZONE_VTOSLAB	0x2000	/* Zone uses vtoslab for lookup. */
274230623Skmacy#define	UMA_ZONE_NODUMP		0x4000	/*
275230623Skmacy					 * Zone's pages will not be included in
276230623Skmacy					 * mini-dumps.
277230623Skmacy					 */
278249264Sglebius#define	UMA_ZONE_PCPU		0x8000	/*
279249264Sglebius					 * Allocates mp_ncpus slabs sized to
280249264Sglebius					 * sizeof(struct pcpu).
281249264Sglebius					 */
28292654Sjeff
283187681Sjeff/*
284187681Sjeff * These flags are shared between the keg and zone.  In zones wishing to add
285187681Sjeff * new kegs these flags must be compatible.  Some are determined based on
286187681Sjeff * physical parameters of the request and may not be provided by the consumer.
287187681Sjeff */
288187681Sjeff#define	UMA_ZONE_INHERIT						\
289226313Sglebius    (UMA_ZONE_OFFPAGE | UMA_ZONE_MALLOC | UMA_ZONE_NOFREE |		\
290249264Sglebius    UMA_ZONE_HASH | UMA_ZONE_REFCNT | UMA_ZONE_VTOSLAB | UMA_ZONE_PCPU)
291187681Sjeff
29292654Sjeff/* Definitions for align */
29392654Sjeff#define UMA_ALIGN_PTR	(sizeof(void *) - 1)	/* Alignment fit for ptr */
29492654Sjeff#define UMA_ALIGN_LONG	(sizeof(long) - 1)	/* "" long */
29592654Sjeff#define UMA_ALIGN_INT	(sizeof(int) - 1)	/* "" int */
29692654Sjeff#define UMA_ALIGN_SHORT	(sizeof(short) - 1)	/* "" short */
29792654Sjeff#define UMA_ALIGN_CHAR	(sizeof(char) - 1)	/* "" char */
298166654Srwatson#define UMA_ALIGN_CACHE	(0 - 1)			/* Cache line size align */
299324602Sjhb#define	UMA_ALIGNOF(type) (_Alignof(type) - 1)	/* Alignment fit for 'type' */
30092654Sjeff
30192654Sjeff/*
30294161Sjeff * Destroys an empty uma zone.  If the zone is not empty uma complains loudly.
30392654Sjeff *
30492654Sjeff * Arguments:
30592654Sjeff *	zone  The zone we want to destroy.
30692654Sjeff *
30792654Sjeff */
30894161Sjeffvoid uma_zdestroy(uma_zone_t zone);
30992654Sjeff
31092654Sjeff/*
31192654Sjeff * Allocates an item out of a zone
31292654Sjeff *
31392654Sjeff * Arguments:
31492654Sjeff *	zone  The zone we are allocating from
31592654Sjeff *	arg   This data is passed to the ctor function
31695766Sjeff *	flags See sys/malloc.h for available flags.
31792654Sjeff *
31892654Sjeff * Returns:
319184546Skeramida *	A non-null pointer to an initialized element from the zone is
320184546Skeramida *	guaranteed if the wait flag is M_WAITOK.  Otherwise a null pointer
321184546Skeramida *	may be returned if the zone is empty or the ctor failed.
32292654Sjeff */
32392654Sjeff
32495766Sjeffvoid *uma_zalloc_arg(uma_zone_t zone, void *arg, int flags);
32592654Sjeff
32692654Sjeff/*
32792654Sjeff * Allocates an item out of a zone without supplying an argument
32892654Sjeff *
32992654Sjeff * This is just a wrapper for uma_zalloc_arg for convenience.
33092654Sjeff *
33192654Sjeff */
33295766Sjeffstatic __inline void *uma_zalloc(uma_zone_t zone, int flags);
33392654Sjeff
33492654Sjeffstatic __inline void *
33595766Sjeffuma_zalloc(uma_zone_t zone, int flags)
33692654Sjeff{
33795766Sjeff	return uma_zalloc_arg(zone, NULL, flags);
33892654Sjeff}
33992654Sjeff
34092654Sjeff/*
34192654Sjeff * Frees an item back into the specified zone.
34292654Sjeff *
34392654Sjeff * Arguments:
34492654Sjeff *	zone  The zone the item was originally allocated out of.
34592654Sjeff *	item  The memory to be freed.
34692654Sjeff *	arg   Argument passed to the destructor
34792654Sjeff *
34892654Sjeff * Returns:
34992654Sjeff *	Nothing.
35092654Sjeff */
35192654Sjeff
35292654Sjeffvoid uma_zfree_arg(uma_zone_t zone, void *item, void *arg);
35392654Sjeff
35492654Sjeff/*
35592654Sjeff * Frees an item back to a zone without supplying an argument
35692654Sjeff *
35792654Sjeff * This is just a wrapper for uma_zfree_arg for convenience.
35892654Sjeff *
35992654Sjeff */
36092654Sjeffstatic __inline void uma_zfree(uma_zone_t zone, void *item);
36192654Sjeff
36292654Sjeffstatic __inline void
36392654Sjeffuma_zfree(uma_zone_t zone, void *item)
36492654Sjeff{
365100326Smarkm	uma_zfree_arg(zone, item, NULL);
36692654Sjeff}
36792654Sjeff
36892654Sjeff/*
36992654Sjeff * XXX The rest of the prototypes in this header are h0h0 magic for the VM.
37092654Sjeff * If you think you need to use it for a normal zone you're probably incorrect.
37192654Sjeff */
37292654Sjeff
37392654Sjeff/*
37492654Sjeff * Backend page supplier routines
37592654Sjeff *
37692654Sjeff * Arguments:
377184546Skeramida *	zone  The zone that is requesting pages.
378184546Skeramida *	size  The number of bytes being requested.
37992654Sjeff *	pflag Flags for these memory pages, see below.
38092654Sjeff *	wait  Indicates our willingness to block.
38192654Sjeff *
38292654Sjeff * Returns:
383184546Skeramida *	A pointer to the allocated memory or NULL on failure.
38492654Sjeff */
38592654Sjeff
386287945Srstonetypedef void *(*uma_alloc)(uma_zone_t zone, vm_size_t size, uint8_t *pflag,
387287945Srstone    int wait);
38892654Sjeff
38992654Sjeff/*
39092654Sjeff * Backend page free routines
39192654Sjeff *
39292654Sjeff * Arguments:
393184546Skeramida *	item  A pointer to the previously allocated pages.
394184546Skeramida *	size  The original size of the allocation.
395184546Skeramida *	pflag The flags for the slab.  See UMA_SLAB_* below.
39692654Sjeff *
39792654Sjeff * Returns:
39892654Sjeff *	None
39992654Sjeff */
400287945Srstonetypedef void (*uma_free)(void *item, vm_size_t size, uint8_t pflag);
40192654Sjeff
40292654Sjeff
40392654Sjeff
40492654Sjeff/*
40592654Sjeff * Sets up the uma allocator. (Called by vm_mem_init)
40692654Sjeff *
40792654Sjeff * Arguments:
40892654Sjeff *	bootmem  A pointer to memory used to bootstrap the system.
40992654Sjeff *
41092654Sjeff * Returns:
41192654Sjeff *	Nothing
41292654Sjeff *
41392654Sjeff * Discussion:
41492654Sjeff *	This memory is used for zones which allocate things before the
41592654Sjeff *	backend page supplier can give us pages.  It should be
416151104Sdes *	UMA_SLAB_SIZE * boot_pages bytes. (see uma_int.h)
41792654Sjeff *
41892654Sjeff */
41992654Sjeff
420151104Sdesvoid uma_startup(void *bootmem, int boot_pages);
42192654Sjeff
42292654Sjeff/*
42392654Sjeff * Finishes starting up the allocator.  This should
42492654Sjeff * be called when kva is ready for normal allocs.
42592654Sjeff *
42692654Sjeff * Arguments:
427103531Sjeff *	None
42892654Sjeff *
42992654Sjeff * Returns:
43092654Sjeff *	Nothing
43192654Sjeff *
43292654Sjeff * Discussion:
433103531Sjeff *	uma_startup2 is called by kmeminit() to enable us of uma for malloc.
43492654Sjeff */
435244024Spjd
436103531Sjeffvoid uma_startup2(void);
43792654Sjeff
43892654Sjeff/*
43992654Sjeff * Reclaims unused memory for all zones
44092654Sjeff *
44192654Sjeff * Arguments:
44292654Sjeff *	None
44392654Sjeff * Returns:
44492654Sjeff *	None
44592654Sjeff *
44692654Sjeff * This should only be called by the page out daemon.
44792654Sjeff */
44892654Sjeff
44992654Sjeffvoid uma_reclaim(void);
45092654Sjeff
45192654Sjeff/*
452166654Srwatson * Sets the alignment mask to be used for all zones requesting cache
453166654Srwatson * alignment.  Should be called by MD boot code prior to starting VM/UMA.
454166654Srwatson *
455166654Srwatson * Arguments:
456166654Srwatson *	align The alignment mask
457166654Srwatson *
458166654Srwatson * Returns:
459166654Srwatson *	Nothing
460166654Srwatson */
461166654Srwatsonvoid uma_set_align(int align);
462166654Srwatson
463166654Srwatson/*
464252226Sjeff * Set a reserved number of items to hold for M_USE_RESERVE allocations.  All
465252226Sjeff * other requests must allocate new backing pages.
466252226Sjeff */
467252226Sjeffvoid uma_zone_reserve(uma_zone_t zone, int nitems);
468252226Sjeff
469252226Sjeff/*
470247360Sattilio * Reserves the maximum KVA space required by the zone and configures the zone
471247360Sattilio * to use a VM_ALLOC_NOOBJ-based backend allocator.
47292654Sjeff *
47392654Sjeff * Arguments:
474184546Skeramida *	zone  The zone to update.
475247360Sattilio *	nitems  The upper limit on the number of items that can be allocated.
47692654Sjeff *
47792654Sjeff * Returns:
478247360Sattilio *	0  if KVA space can not be allocated
47992654Sjeff *	1  if successful
48092654Sjeff *
48192654Sjeff * Discussion:
482247360Sattilio *	When the machine supports a direct map and the zone's items are smaller
483247360Sattilio *	than a page, the zone will use the direct map instead of allocating KVA
484247360Sattilio *	space.
48592654Sjeff */
486247360Sattilioint uma_zone_reserve_kva(uma_zone_t zone, int nitems);
48792654Sjeff
48892758Sjeff/*
48992758Sjeff * Sets a high limit on the number of items allowed in a zone
49092758Sjeff *
49192758Sjeff * Arguments:
49292758Sjeff *	zone  The zone to limit
493213911Slstewart *	nitems  The requested upper limit on the number of items allowed
49492758Sjeff *
49592758Sjeff * Returns:
496213911Slstewart *	int  The effective value of nitems after rounding up based on page size
49792758Sjeff */
498213911Slstewartint uma_zone_set_max(uma_zone_t zone, int nitems);
49992654Sjeff
50092654Sjeff/*
501211396Sandre * Obtains the effective limit on the number of items in a zone
502211396Sandre *
503211396Sandre * Arguments:
504211396Sandre *	zone  The zone to obtain the effective limit from
505211396Sandre *
506211396Sandre * Return:
507211396Sandre *	0  No limit
508211396Sandre *	int  The effective limit of the zone
509211396Sandre */
510211396Sandreint uma_zone_get_max(uma_zone_t zone);
511211396Sandre
512211396Sandre/*
513243998Spjd * Sets a warning to be printed when limit is reached
514243998Spjd *
515243998Spjd * Arguments:
516243998Spjd *	zone  The zone we will warn about
517243998Spjd *	warning  Warning content
518243998Spjd *
519243998Spjd * Returns:
520243998Spjd *	Nothing
521243998Spjd */
522243998Spjdvoid uma_zone_set_warning(uma_zone_t zone, const char *warning);
523243998Spjd
524243998Spjd/*
525213910Slstewart * Obtains the approximate current number of items allocated from a zone
526213910Slstewart *
527213910Slstewart * Arguments:
528213910Slstewart *	zone  The zone to obtain the current allocation count from
529213910Slstewart *
530213910Slstewart * Return:
531213910Slstewart *	int  The approximate current number of items allocated from the zone
532213910Slstewart */
533213910Slstewartint uma_zone_get_cur(uma_zone_t zone);
534213910Slstewart
535213910Slstewart/*
536129906Sbmilekic * The following two routines (uma_zone_set_init/fini)
537129906Sbmilekic * are used to set the backend init/fini pair which acts on an
538129906Sbmilekic * object as it becomes allocated and is placed in a slab within
539129906Sbmilekic * the specified zone's backing keg.  These should probably not
540184546Skeramida * be changed once allocations have already begun, but only be set
541129906Sbmilekic * immediately upon zone creation.
542129906Sbmilekic */
543129906Sbmilekicvoid uma_zone_set_init(uma_zone_t zone, uma_init uminit);
544129906Sbmilekicvoid uma_zone_set_fini(uma_zone_t zone, uma_fini fini);
545129906Sbmilekic
546129906Sbmilekic/*
547129906Sbmilekic * The following two routines (uma_zone_set_zinit/zfini) are
548129906Sbmilekic * used to set the zinit/zfini pair which acts on an object as
549129906Sbmilekic * it passes from the backing Keg's slab cache to the
550129906Sbmilekic * specified Zone's bucket cache.  These should probably not
551184546Skeramida * be changed once allocations have already begun, but only be set
552184546Skeramida * immediately upon zone creation.
553129906Sbmilekic */
554129906Sbmilekicvoid uma_zone_set_zinit(uma_zone_t zone, uma_init zinit);
555129906Sbmilekicvoid uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini);
556129906Sbmilekic
557129906Sbmilekic/*
558247360Sattilio * Replaces the standard backend allocator for this zone.
55992654Sjeff *
56092654Sjeff * Arguments:
561184546Skeramida *	zone   The zone whose backend allocator is being changed.
56292654Sjeff *	allocf A pointer to the allocation function
56392654Sjeff *
56492654Sjeff * Returns:
56592654Sjeff *	Nothing
56692654Sjeff *
56792654Sjeff * Discussion:
56892654Sjeff *	This could be used to implement pageable allocation, or perhaps
56992654Sjeff *	even DMA allocators if used in conjunction with the OFFPAGE
57092654Sjeff *	zone flag.
57192654Sjeff */
57292654Sjeff
57392654Sjeffvoid uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf);
57492654Sjeff
57592654Sjeff/*
57692654Sjeff * Used for freeing memory provided by the allocf above
57792654Sjeff *
57892654Sjeff * Arguments:
57992654Sjeff *	zone  The zone that intends to use this free routine.
58092654Sjeff *	freef The page freeing routine.
58192654Sjeff *
58292654Sjeff * Returns:
58392654Sjeff *	Nothing
58492654Sjeff */
58592654Sjeff
58692654Sjeffvoid uma_zone_set_freef(uma_zone_t zone, uma_free freef);
58792654Sjeff
58892654Sjeff/*
589184546Skeramida * These flags are setable in the allocf and visible in the freef.
59092654Sjeff */
59192654Sjeff#define UMA_SLAB_BOOT	0x01		/* Slab alloced from boot pages */
59292654Sjeff#define UMA_SLAB_KMEM	0x02		/* Slab alloced from kmem_map */
593177921Salc#define UMA_SLAB_KERNEL	0x04		/* Slab alloced from kernel_map */
59492654Sjeff#define UMA_SLAB_PRIV	0x08		/* Slab alloced from priv allocator */
59594157Sjeff#define UMA_SLAB_OFFP	0x10		/* Slab is managed separately  */
59692654Sjeff#define UMA_SLAB_MALLOC	0x20		/* Slab is a large malloc slab */
59792654Sjeff/* 0x40 and 0x80 are available */
59892654Sjeff
59992654Sjeff/*
60092654Sjeff * Used to pre-fill a zone with some number of items
60192654Sjeff *
60292654Sjeff * Arguments:
60392654Sjeff *	zone    The zone to fill
60492654Sjeff *	itemcnt The number of items to reserve
60592654Sjeff *
60692654Sjeff * Returns:
60792654Sjeff *	Nothing
60892654Sjeff *
60992654Sjeff * NOTE: This is blocking and should only be done at startup
61092654Sjeff */
61192654Sjeffvoid uma_prealloc(uma_zone_t zone, int itemcnt);
61292654Sjeff
613129906Sbmilekic/*
614129906Sbmilekic * Used to lookup the reference counter allocated for an item
615129906Sbmilekic * from a UMA_ZONE_REFCNT zone.  For UMA_ZONE_REFCNT zones,
616129906Sbmilekic * reference counters are allocated for items and stored in
617129906Sbmilekic * the underlying slab header.
618129906Sbmilekic *
619129906Sbmilekic * Arguments:
620244024Spjd *	zone  The UMA_ZONE_REFCNT zone to which the item belongs.
621129906Sbmilekic *	item  The address of the item for which we want a refcnt.
622129906Sbmilekic *
623129906Sbmilekic * Returns:
624249313Sglebius *	A pointer to a uint32_t reference counter.
625129906Sbmilekic */
626249313Sglebiusuint32_t *uma_find_refcnt(uma_zone_t zone, void *item);
62792654Sjeff
628147996Srwatson/*
629165809Sjhb * Used to determine if a fixed-size zone is exhausted.
630165809Sjhb *
631165809Sjhb * Arguments:
632165809Sjhb *	zone    The zone to check
633165809Sjhb *
634165809Sjhb * Returns:
635244024Spjd *	Non-zero if zone is exhausted.
636165809Sjhb */
637165809Sjhbint uma_zone_exhausted(uma_zone_t zone);
638166213Smohansint uma_zone_exhausted_nolock(uma_zone_t zone);
639165809Sjhb
640165809Sjhb/*
641262739Sglebius * Common UMA_ZONE_PCPU zones.
642262739Sglebius */
643262739Sglebiusextern uma_zone_t pcpu_zone_64;
644262739Sglebiusextern uma_zone_t pcpu_zone_ptr;
645262739Sglebius
646262739Sglebius/*
647147996Srwatson * Exported statistics structures to be used by user space monitoring tools.
648184546Skeramida * Statistics stream consists of a uma_stream_header, followed by a series of
649184546Skeramida * alternative uma_type_header and uma_type_stat structures.
650147996Srwatson */
651147996Srwatson#define	UMA_STREAM_VERSION	0x00000001
652147996Srwatsonstruct uma_stream_header {
653249313Sglebius	uint32_t	ush_version;	/* Stream format version. */
654249313Sglebius	uint32_t	ush_maxcpus;	/* Value of MAXCPU for stream. */
655249313Sglebius	uint32_t	ush_count;	/* Number of records. */
656249313Sglebius	uint32_t	_ush_pad;	/* Pad/reserved field. */
657147996Srwatson};
658147996Srwatson
659148371Srwatson#define	UTH_MAX_NAME	32
660148371Srwatson#define	UTH_ZONE_SECONDARY	0x00000001
661147996Srwatsonstruct uma_type_header {
662147996Srwatson	/*
663147996Srwatson	 * Static per-zone data, some extracted from the supporting keg.
664147996Srwatson	 */
665148371Srwatson	char		uth_name[UTH_MAX_NAME];
666249313Sglebius	uint32_t	uth_align;	/* Keg: alignment. */
667249313Sglebius	uint32_t	uth_size;	/* Keg: requested size of item. */
668249313Sglebius	uint32_t	uth_rsize;	/* Keg: real size of item. */
669249313Sglebius	uint32_t	uth_maxpages;	/* Keg: maximum number of pages. */
670249313Sglebius	uint32_t	uth_limit;	/* Keg: max items to allocate. */
671147996Srwatson
672147996Srwatson	/*
673147996Srwatson	 * Current dynamic zone/keg-derived statistics.
674147996Srwatson	 */
675249313Sglebius	uint32_t	uth_pages;	/* Keg: pages allocated. */
676249313Sglebius	uint32_t	uth_keg_free;	/* Keg: items free. */
677249313Sglebius	uint32_t	uth_zone_free;	/* Zone: items free. */
678249313Sglebius	uint32_t	uth_bucketsize;	/* Zone: desired bucket size. */
679249313Sglebius	uint32_t	uth_zone_flags;	/* Zone: flags. */
680249313Sglebius	uint64_t	uth_allocs;	/* Zone: number of allocations. */
681249313Sglebius	uint64_t	uth_frees;	/* Zone: number of frees. */
682249313Sglebius	uint64_t	uth_fails;	/* Zone: number of alloc failures. */
683249313Sglebius	uint64_t	uth_sleeps;	/* Zone: number of alloc sleeps. */
684249313Sglebius	uint64_t	_uth_reserved1[2];	/* Reserved. */
685147996Srwatson};
686147996Srwatson
687147996Srwatsonstruct uma_percpu_stat {
688249313Sglebius	uint64_t	ups_allocs;	/* Cache: number of allocations. */
689249313Sglebius	uint64_t	ups_frees;	/* Cache: number of frees. */
690249313Sglebius	uint64_t	ups_cache_free;	/* Cache: free items in cache. */
691249313Sglebius	uint64_t	_ups_reserved[5];	/* Reserved. */
692147996Srwatson};
693147996Srwatson
694283310Skibvoid uma_reclaim_wakeup(void);
695283310Skibvoid uma_reclaim_worker(void *);
696283310Skib
697262739Sglebius#endif	/* _VM_UMA_H_ */
698