xref: /freebsd/sys/vm/uma_int.h (revision e20a199fd56de5996c0a3f101b0569c0722816b0)
160727d8bSWarner Losh /*-
2e20a199fSJeff Roberson  * Copyright (c) 2002-2005, 2009 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  * This file includes definitions, structures, prototypes, and inlines that
338355f576SJeff Roberson  * should not be used outside of the actual implementation of UMA.
348355f576SJeff Roberson  */
358355f576SJeff Roberson 
368355f576SJeff Roberson /*
378355f576SJeff Roberson  * Here's a quick description of the relationship between the objects:
388355f576SJeff Roberson  *
39099a0e58SBosko Milekic  * Kegs contain lists of slabs which are stored in either the full bin, empty
408355f576SJeff Roberson  * bin, or partially allocated bin, to reduce fragmentation.  They also contain
418355f576SJeff Roberson  * the user supplied value for size, which is adjusted for alignment purposes
42099a0e58SBosko Milekic  * and rsize is the result of that.  The Keg also stores information for
438355f576SJeff Roberson  * managing a hash of page addresses that maps pages to uma_slab_t structures
448355f576SJeff Roberson  * for pages that don't have embedded uma_slab_t's.
458355f576SJeff Roberson  *
468355f576SJeff Roberson  * The uma_slab_t may be embedded in a UMA_SLAB_SIZE chunk of memory or it may
478355f576SJeff Roberson  * be allocated off the page from a special slab zone.  The free list within a
488355f576SJeff Roberson  * slab is managed with a linked list of indexes, which are 8 bit values.  If
498355f576SJeff Roberson  * UMA_SLAB_SIZE is defined to be too large I will have to switch to 16bit
508355f576SJeff Roberson  * values.  Currently on alpha you can get 250 or so 32 byte items and on x86
518355f576SJeff Roberson  * you can get 250 or so 16byte items.  For item sizes that would yield more
52c235bfa5SJeff Roberson  * than 10% memory waste we potentially allocate a separate uma_slab_t if this
538355f576SJeff Roberson  * will improve the number of items per slab that will fit.
548355f576SJeff Roberson  *
558355f576SJeff Roberson  * Other potential space optimizations are storing the 8bit of linkage in space
568355f576SJeff Roberson  * wasted between items due to alignment problems.  This may yield a much better
578355f576SJeff Roberson  * memory footprint for certain sizes of objects.  Another alternative is to
588355f576SJeff Roberson  * increase the UMA_SLAB_SIZE, or allow for dynamic slab sizes.  I prefer
598355f576SJeff Roberson  * dynamic slab sizes because we could stick with 8 bit indexes and only use
608355f576SJeff Roberson  * large slab sizes for zones with a lot of waste per slab.  This may create
618355f576SJeff Roberson  * ineffeciencies in the vm subsystem due to fragmentation in the address space.
628355f576SJeff Roberson  *
638355f576SJeff Roberson  * The only really gross cases, with regards to memory waste, are for those
648355f576SJeff Roberson  * items that are just over half the page size.   You can get nearly 50% waste,
658355f576SJeff Roberson  * so you fall back to the memory footprint of the power of two allocator. I
668355f576SJeff Roberson  * have looked at memory allocation sizes on many of the machines available to
678355f576SJeff Roberson  * me, and there does not seem to be an abundance of allocations at this range
688355f576SJeff Roberson  * so at this time it may not make sense to optimize for it.  This can, of
698355f576SJeff Roberson  * course, be solved with dynamic slab sizes.
708355f576SJeff Roberson  *
71099a0e58SBosko Milekic  * Kegs may serve multiple Zones but by far most of the time they only serve
72099a0e58SBosko Milekic  * one.  When a Zone is created, a Keg is allocated and setup for it.  While
73099a0e58SBosko Milekic  * the backing Keg stores slabs, the Zone caches Buckets of items allocated
74099a0e58SBosko Milekic  * from the slabs.  Each Zone is equipped with an init/fini and ctor/dtor
75099a0e58SBosko Milekic  * pair, as well as with its own set of small per-CPU caches, layered above
76099a0e58SBosko Milekic  * the Zone's general Bucket cache.
77099a0e58SBosko Milekic  *
786ab3b958SRobert Watson  * The PCPU caches are protected by critical sections, and may be accessed
796ab3b958SRobert Watson  * safely only from their associated CPU, while the Zones backed by the same
806ab3b958SRobert Watson  * Keg all share a common Keg lock (to coalesce contention on the backing
816ab3b958SRobert Watson  * slabs).  The backing Keg typically only serves one Zone but in the case of
826ab3b958SRobert Watson  * multiple Zones, one of the Zones is considered the Master Zone and all
836ab3b958SRobert Watson  * Zone-related stats from the Keg are done in the Master Zone.  For an
846ab3b958SRobert Watson  * example of a Multi-Zone setup, refer to the Mbuf allocation code.
858355f576SJeff Roberson  */
868355f576SJeff Roberson 
878355f576SJeff Roberson /*
888355f576SJeff Roberson  *	This is the representation for normal (Non OFFPAGE slab)
898355f576SJeff Roberson  *
908355f576SJeff Roberson  *	i == item
918355f576SJeff Roberson  *	s == slab pointer
928355f576SJeff Roberson  *
938355f576SJeff Roberson  *	<----------------  Page (UMA_SLAB_SIZE) ------------------>
948355f576SJeff Roberson  *	___________________________________________________________
958355f576SJeff Roberson  *     | _  _  _  _  _  _  _  _  _  _  _  _  _  _  _   ___________ |
968355f576SJeff Roberson  *     ||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i| |slab header||
978355f576SJeff Roberson  *     ||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_| |___________||
988355f576SJeff Roberson  *     |___________________________________________________________|
998355f576SJeff Roberson  *
1008355f576SJeff Roberson  *
1018355f576SJeff Roberson  *	This is an OFFPAGE slab. These can be larger than UMA_SLAB_SIZE.
1028355f576SJeff Roberson  *
1038355f576SJeff Roberson  *	___________________________________________________________
1048355f576SJeff Roberson  *     | _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _   |
1058355f576SJeff Roberson  *     ||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i|  |
1068355f576SJeff Roberson  *     ||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_|  |
1078355f576SJeff Roberson  *     |___________________________________________________________|
1088355f576SJeff Roberson  *       ___________    ^
1098355f576SJeff Roberson  *	|slab header|   |
1108355f576SJeff Roberson  *	|___________|---*
1118355f576SJeff Roberson  *
1128355f576SJeff Roberson  */
1138355f576SJeff Roberson 
1148355f576SJeff Roberson #ifndef VM_UMA_INT_H
1158355f576SJeff Roberson #define VM_UMA_INT_H
1168355f576SJeff Roberson 
1178355f576SJeff Roberson #define UMA_SLAB_SIZE	PAGE_SIZE	/* How big are our slabs? */
1188355f576SJeff Roberson #define UMA_SLAB_MASK	(PAGE_SIZE - 1)	/* Mask to get back to the page */
1198355f576SJeff Roberson #define UMA_SLAB_SHIFT	PAGE_SHIFT	/* Number of bits PAGE_MASK */
1208355f576SJeff Roberson 
121eafc7b54SAlan Cox #define UMA_BOOT_PAGES		48	/* Pages allocated for startup */
1228355f576SJeff Roberson 
1238355f576SJeff Roberson /* Max waste before going to off page slab management */
1248355f576SJeff Roberson #define UMA_MAX_WASTE	(UMA_SLAB_SIZE / 10)
1258355f576SJeff Roberson 
1268355f576SJeff Roberson /*
1278355f576SJeff Roberson  * I doubt there will be many cases where this is exceeded. This is the initial
1288355f576SJeff Roberson  * size of the hash table for uma_slabs that are managed off page. This hash
1298355f576SJeff Roberson  * does expand by powers of two.  Currently it doesn't get smaller.
1308355f576SJeff Roberson  */
1318355f576SJeff Roberson #define UMA_HASH_SIZE_INIT	32
1328355f576SJeff Roberson 
1338355f576SJeff Roberson /*
1348355f576SJeff Roberson  * I should investigate other hashing algorithms.  This should yield a low
1358355f576SJeff Roberson  * number of collisions if the pages are relatively contiguous.
1368355f576SJeff Roberson  *
1378355f576SJeff Roberson  * This is the same algorithm that most processor caches use.
1388355f576SJeff Roberson  *
1398355f576SJeff Roberson  * I'm shifting and masking instead of % because it should be faster.
1408355f576SJeff Roberson  */
1418355f576SJeff Roberson 
1428355f576SJeff Roberson #define UMA_HASH(h, s) ((((unsigned long)s) >> UMA_SLAB_SHIFT) &	\
1438355f576SJeff Roberson     (h)->uh_hashmask)
1448355f576SJeff Roberson 
1458355f576SJeff Roberson #define UMA_HASH_INSERT(h, s, mem)					\
1468355f576SJeff Roberson 		SLIST_INSERT_HEAD(&(h)->uh_slab_hash[UMA_HASH((h),	\
1478355f576SJeff Roberson 		    (mem))], (s), us_hlink);
1488355f576SJeff Roberson #define UMA_HASH_REMOVE(h, s, mem)					\
1498355f576SJeff Roberson 		SLIST_REMOVE(&(h)->uh_slab_hash[UMA_HASH((h),		\
1508355f576SJeff Roberson 		    (mem))], (s), uma_slab, us_hlink);
1518355f576SJeff Roberson 
1528355f576SJeff Roberson /* Hash table for freed address -> slab translation */
1538355f576SJeff Roberson 
1548355f576SJeff Roberson SLIST_HEAD(slabhead, uma_slab);
1558355f576SJeff Roberson 
1568355f576SJeff Roberson struct uma_hash {
1578355f576SJeff Roberson 	struct slabhead	*uh_slab_hash;	/* Hash table for slabs */
1588355f576SJeff Roberson 	int		uh_hashsize;	/* Current size of the hash table */
1598355f576SJeff Roberson 	int		uh_hashmask;	/* Mask used during hashing */
1608355f576SJeff Roberson };
1618355f576SJeff Roberson 
1628355f576SJeff Roberson /*
1638355f576SJeff Roberson  * Structures for per cpu queues.
1648355f576SJeff Roberson  */
1658355f576SJeff Roberson 
1668355f576SJeff Roberson struct uma_bucket {
1678355f576SJeff Roberson 	LIST_ENTRY(uma_bucket)	ub_link;	/* Link into the zone */
168cae33c14SJeff Roberson 	int16_t	ub_cnt;				/* Count of free items. */
169cae33c14SJeff Roberson 	int16_t	ub_entries;			/* Max items. */
170cae33c14SJeff Roberson 	void	*ub_bucket[];			/* actual allocation storage */
1718355f576SJeff Roberson };
1728355f576SJeff Roberson 
1738355f576SJeff Roberson typedef struct uma_bucket * uma_bucket_t;
1748355f576SJeff Roberson 
1758355f576SJeff Roberson struct uma_cache {
1768355f576SJeff Roberson 	uma_bucket_t	uc_freebucket;	/* Bucket we're freeing to */
1778355f576SJeff Roberson 	uma_bucket_t	uc_allocbucket;	/* Bucket to allocate from */
1788355f576SJeff Roberson 	u_int64_t	uc_allocs;	/* Count of allocations */
179773df9abSRobert Watson 	u_int64_t	uc_frees;	/* Count of frees */
1808355f576SJeff Roberson };
1818355f576SJeff Roberson 
1828355f576SJeff Roberson typedef struct uma_cache * uma_cache_t;
1838355f576SJeff Roberson 
1848355f576SJeff Roberson /*
185099a0e58SBosko Milekic  * Keg management structure
186099a0e58SBosko Milekic  *
187099a0e58SBosko Milekic  * TODO: Optimize for cache line size
188099a0e58SBosko Milekic  *
189099a0e58SBosko Milekic  */
190099a0e58SBosko Milekic struct uma_keg {
191099a0e58SBosko Milekic 	LIST_ENTRY(uma_keg)	uk_link;	/* List of all kegs */
192099a0e58SBosko Milekic 
193099a0e58SBosko Milekic 	struct mtx	uk_lock;	/* Lock for the keg */
194099a0e58SBosko Milekic 	struct uma_hash	uk_hash;
195099a0e58SBosko Milekic 
196e20a199fSJeff Roberson 	char		*uk_name;		/* Name of creating zone. */
197099a0e58SBosko Milekic 	LIST_HEAD(,uma_zone)	uk_zones;	/* Keg's zones */
198099a0e58SBosko Milekic 	LIST_HEAD(,uma_slab)	uk_part_slab;	/* partially allocated slabs */
199099a0e58SBosko Milekic 	LIST_HEAD(,uma_slab)	uk_free_slab;	/* empty slab list */
200099a0e58SBosko Milekic 	LIST_HEAD(,uma_slab)	uk_full_slab;	/* full slabs */
201099a0e58SBosko Milekic 
202099a0e58SBosko Milekic 	u_int32_t	uk_recurse;	/* Allocation recursion count */
203099a0e58SBosko Milekic 	u_int32_t	uk_align;	/* Alignment mask */
204099a0e58SBosko Milekic 	u_int32_t	uk_pages;	/* Total page count */
205099a0e58SBosko Milekic 	u_int32_t	uk_free;	/* Count of items free in slabs */
206099a0e58SBosko Milekic 	u_int32_t	uk_size;	/* Requested size of each item */
207099a0e58SBosko Milekic 	u_int32_t	uk_rsize;	/* Real size of each item */
208099a0e58SBosko Milekic 	u_int32_t	uk_maxpages;	/* Maximum number of pages to alloc */
209099a0e58SBosko Milekic 
210099a0e58SBosko Milekic 	uma_init	uk_init;	/* Keg's init routine */
211099a0e58SBosko Milekic 	uma_fini	uk_fini;	/* Keg's fini routine */
212099a0e58SBosko Milekic 	uma_alloc	uk_allocf;	/* Allocation function */
213099a0e58SBosko Milekic 	uma_free	uk_freef;	/* Free routine */
214099a0e58SBosko Milekic 
215099a0e58SBosko Milekic 	struct vm_object	*uk_obj;	/* Zone specific object */
216099a0e58SBosko Milekic 	vm_offset_t	uk_kva;		/* Base kva for zones with objs */
217099a0e58SBosko Milekic 	uma_zone_t	uk_slabzone;	/* Slab zone backing us, if OFFPAGE */
218099a0e58SBosko Milekic 
219099a0e58SBosko Milekic 	u_int16_t	uk_pgoff;	/* Offset to uma_slab struct */
220099a0e58SBosko Milekic 	u_int16_t	uk_ppera;	/* pages per allocation from backend */
221099a0e58SBosko Milekic 	u_int16_t	uk_ipers;	/* Items per slab */
2222018f30cSMike Silbersack 	u_int32_t	uk_flags;	/* Internal flags */
223099a0e58SBosko Milekic };
224099a0e58SBosko Milekic typedef struct uma_keg	* uma_keg_t;
225099a0e58SBosko Milekic 
226099a0e58SBosko Milekic /* Page management structure */
227099a0e58SBosko Milekic 
228099a0e58SBosko Milekic /* Sorry for the union, but space efficiency is important */
229099a0e58SBosko Milekic struct uma_slab_head {
230099a0e58SBosko Milekic 	uma_keg_t	us_keg;			/* Keg we live in */
231099a0e58SBosko Milekic 	union {
232099a0e58SBosko Milekic 		LIST_ENTRY(uma_slab)	_us_link;	/* slabs in zone */
233099a0e58SBosko Milekic 		unsigned long	_us_size;	/* Size of allocation */
234099a0e58SBosko Milekic 	} us_type;
235099a0e58SBosko Milekic 	SLIST_ENTRY(uma_slab)	us_hlink;	/* Link for hash table */
236099a0e58SBosko Milekic 	u_int8_t	*us_data;		/* First item */
237099a0e58SBosko Milekic 	u_int8_t	us_flags;		/* Page flags see uma.h */
238099a0e58SBosko Milekic 	u_int8_t	us_freecount;	/* How many are free? */
239099a0e58SBosko Milekic 	u_int8_t	us_firstfree;	/* First free item index */
240099a0e58SBosko Milekic };
241099a0e58SBosko Milekic 
242099a0e58SBosko Milekic /* The standard slab structure */
243099a0e58SBosko Milekic struct uma_slab {
244099a0e58SBosko Milekic 	struct uma_slab_head	us_head;	/* slab header data */
245099a0e58SBosko Milekic 	struct {
246099a0e58SBosko Milekic 		u_int8_t	us_item;
247099a0e58SBosko Milekic 	} us_freelist[1];			/* actual number bigger */
248099a0e58SBosko Milekic };
249099a0e58SBosko Milekic 
250099a0e58SBosko Milekic /*
251099a0e58SBosko Milekic  * The slab structure for UMA_ZONE_REFCNT zones for whose items we
252099a0e58SBosko Milekic  * maintain reference counters in the slab for.
253099a0e58SBosko Milekic  */
254099a0e58SBosko Milekic struct uma_slab_refcnt {
255099a0e58SBosko Milekic 	struct uma_slab_head	us_head;	/* slab header data */
256099a0e58SBosko Milekic 	struct {
257099a0e58SBosko Milekic 		u_int8_t	us_item;
258099a0e58SBosko Milekic 		u_int32_t	us_refcnt;
259099a0e58SBosko Milekic 	} us_freelist[1];			/* actual number bigger */
260099a0e58SBosko Milekic };
261099a0e58SBosko Milekic 
262099a0e58SBosko Milekic #define	us_keg		us_head.us_keg
263099a0e58SBosko Milekic #define	us_link		us_head.us_type._us_link
264099a0e58SBosko Milekic #define	us_size		us_head.us_type._us_size
265099a0e58SBosko Milekic #define	us_hlink	us_head.us_hlink
266099a0e58SBosko Milekic #define	us_data		us_head.us_data
267099a0e58SBosko Milekic #define	us_flags	us_head.us_flags
268099a0e58SBosko Milekic #define	us_freecount	us_head.us_freecount
269099a0e58SBosko Milekic #define	us_firstfree	us_head.us_firstfree
270099a0e58SBosko Milekic 
271099a0e58SBosko Milekic typedef struct uma_slab * uma_slab_t;
272099a0e58SBosko Milekic typedef struct uma_slab_refcnt * uma_slabrefcnt_t;
273e20a199fSJeff Roberson typedef uma_slab_t (*uma_slaballoc)(uma_zone_t, uma_keg_t, int);
274e20a199fSJeff Roberson 
275099a0e58SBosko Milekic 
276099a0e58SBosko Milekic /*
277244f4554SBosko Milekic  * These give us the size of one free item reference within our corresponding
278244f4554SBosko Milekic  * uma_slab structures, so that our calculations during zone setup are correct
279244f4554SBosko Milekic  * regardless of what the compiler decides to do with padding the structure
280244f4554SBosko Milekic  * arrays within uma_slab.
281244f4554SBosko Milekic  */
282244f4554SBosko Milekic #define	UMA_FRITM_SZ	(sizeof(struct uma_slab) - sizeof(struct uma_slab_head))
283244f4554SBosko Milekic #define	UMA_FRITMREF_SZ	(sizeof(struct uma_slab_refcnt) -	\
284244f4554SBosko Milekic     sizeof(struct uma_slab_head))
285244f4554SBosko Milekic 
286e20a199fSJeff Roberson struct uma_klink {
287e20a199fSJeff Roberson 	LIST_ENTRY(uma_klink)	kl_link;
288e20a199fSJeff Roberson 	uma_keg_t		kl_keg;
289e20a199fSJeff Roberson };
290e20a199fSJeff Roberson typedef struct uma_klink *uma_klink_t;
291e20a199fSJeff Roberson 
292244f4554SBosko Milekic /*
2938355f576SJeff Roberson  * Zone management structure
2948355f576SJeff Roberson  *
2958355f576SJeff Roberson  * TODO: Optimize for cache line size
2968355f576SJeff Roberson  *
2978355f576SJeff Roberson  */
2988355f576SJeff Roberson struct uma_zone {
2998355f576SJeff Roberson 	char		*uz_name;	/* Text name of the zone */
300099a0e58SBosko Milekic 	struct mtx	*uz_lock;	/* Lock for the zone (keg's lock) */
3018355f576SJeff Roberson 
302099a0e58SBosko Milekic 	LIST_ENTRY(uma_zone)	uz_link;	/* List of all zones in keg */
3038355f576SJeff Roberson 	LIST_HEAD(,uma_bucket)	uz_full_bucket;	/* full buckets */
3048355f576SJeff Roberson 	LIST_HEAD(,uma_bucket)	uz_free_bucket;	/* Buckets for frees */
3058355f576SJeff Roberson 
306e20a199fSJeff Roberson 	LIST_HEAD(,uma_klink)	uz_kegs;	/* List of kegs. */
307e20a199fSJeff Roberson 	struct uma_klink	uz_klink;	/* klink for first keg. */
308e20a199fSJeff Roberson 
309e20a199fSJeff Roberson 	uma_slaballoc	uz_slab;	/* Allocate a slab from the backend. */
3108355f576SJeff Roberson 	uma_ctor	uz_ctor;	/* Constructor for each allocation */
3118355f576SJeff Roberson 	uma_dtor	uz_dtor;	/* Destructor */
3128355f576SJeff Roberson 	uma_init	uz_init;	/* Initializer for each item */
3138355f576SJeff Roberson 	uma_fini	uz_fini;	/* Discards memory */
314099a0e58SBosko Milekic 
315099a0e58SBosko Milekic 	u_int64_t	uz_allocs;	/* Total number of allocations */
3167a52a97eSRobert Watson 	u_int64_t	uz_frees;	/* Total number of frees */
3172019094aSRobert Watson 	u_int64_t	uz_fails;	/* Total number of alloc failures */
318e20a199fSJeff Roberson 	u_int32_t	uz_flags;	/* Flags inherited from kegs */
319e20a199fSJeff Roberson 	u_int32_t	uz_size;	/* Size inherited from kegs */
320a553d4b8SJeff Roberson 	uint16_t	uz_fills;	/* Outstanding bucket fills */
321a553d4b8SJeff Roberson 	uint16_t	uz_count;	/* Highest value ub_ptr can have */
322099a0e58SBosko Milekic 
3238355f576SJeff Roberson 	/*
3248355f576SJeff Roberson 	 * This HAS to be the last item because we adjust the zone size
3258355f576SJeff Roberson 	 * based on NCPU and then allocate the space for the zones.
3268355f576SJeff Roberson 	 */
3278355f576SJeff Roberson 	struct uma_cache	uz_cpu[1];	/* Per cpu caches */
3288355f576SJeff Roberson };
3298355f576SJeff Roberson 
330b60f5b79SJeff Roberson /*
331b60f5b79SJeff Roberson  * These flags must not overlap with the UMA_ZONE flags specified in uma.h.
332b60f5b79SJeff Roberson  */
333e20a199fSJeff Roberson #define	UMA_ZFLAG_BUCKET	0x02000000	/* Bucket zone. */
334e20a199fSJeff Roberson #define	UMA_ZFLAG_MULTI		0x04000000	/* Multiple kegs in the zone. */
335e20a199fSJeff Roberson #define	UMA_ZFLAG_DRAINING	0x08000000	/* Running zone_drain. */
3362018f30cSMike Silbersack #define UMA_ZFLAG_PRIVALLOC	0x10000000	/* Use uz_allocf. */
3372018f30cSMike Silbersack #define UMA_ZFLAG_INTERNAL	0x20000000	/* No offpage no PCPU. */
3382018f30cSMike Silbersack #define UMA_ZFLAG_FULL		0x40000000	/* Reached uz_maxpages */
3392018f30cSMike Silbersack #define UMA_ZFLAG_CACHEONLY	0x80000000	/* Don't ask VM for buckets. */
3408355f576SJeff Roberson 
341e20a199fSJeff Roberson #define	UMA_ZFLAG_INHERIT	(UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY | \
342e20a199fSJeff Roberson 				    UMA_ZFLAG_BUCKET)
343e20a199fSJeff Roberson 
344af17e9a9SRobert Watson #ifdef _KERNEL
3458355f576SJeff Roberson /* Internal prototypes */
3468355f576SJeff Roberson static __inline uma_slab_t hash_sfind(struct uma_hash *hash, u_int8_t *data);
3478355f576SJeff Roberson void *uma_large_malloc(int size, int wait);
3488355f576SJeff Roberson void uma_large_free(uma_slab_t slab);
3498355f576SJeff Roberson 
3508355f576SJeff Roberson /* Lock Macros */
3518355f576SJeff Roberson 
352e20a199fSJeff Roberson #define	KEG_LOCK_INIT(k, lc)					\
35328bc4419SJeff Roberson 	do {							\
35428bc4419SJeff Roberson 		if ((lc))					\
355e20a199fSJeff Roberson 			mtx_init(&(k)->uk_lock, (k)->uk_name,	\
356e20a199fSJeff Roberson 			    (k)->uk_name, MTX_DEF | MTX_DUPOK);	\
35728bc4419SJeff Roberson 		else						\
358e20a199fSJeff Roberson 			mtx_init(&(k)->uk_lock, (k)->uk_name,	\
35928bc4419SJeff Roberson 			    "UMA zone", MTX_DEF | MTX_DUPOK);	\
36028bc4419SJeff Roberson 	} while (0)
36128bc4419SJeff Roberson 
362e20a199fSJeff Roberson #define	KEG_LOCK_FINI(k)	mtx_destroy(&(k)->uk_lock)
363e20a199fSJeff Roberson #define	KEG_LOCK(k)	mtx_lock(&(k)->uk_lock)
364e20a199fSJeff Roberson #define	KEG_UNLOCK(k)	mtx_unlock(&(k)->uk_lock)
365099a0e58SBosko Milekic #define	ZONE_LOCK(z)	mtx_lock((z)->uz_lock)
366099a0e58SBosko Milekic #define ZONE_UNLOCK(z)	mtx_unlock((z)->uz_lock)
3678355f576SJeff Roberson 
3688355f576SJeff Roberson /*
3698355f576SJeff Roberson  * Find a slab within a hash table.  This is used for OFFPAGE zones to lookup
3708355f576SJeff Roberson  * the slab structure.
3718355f576SJeff Roberson  *
3728355f576SJeff Roberson  * Arguments:
3738355f576SJeff Roberson  *	hash  The hash table to search.
3748355f576SJeff Roberson  *	data  The base page of the item.
3758355f576SJeff Roberson  *
3768355f576SJeff Roberson  * Returns:
3778355f576SJeff Roberson  *	A pointer to a slab if successful, else NULL.
3788355f576SJeff Roberson  */
3798355f576SJeff Roberson static __inline uma_slab_t
3808355f576SJeff Roberson hash_sfind(struct uma_hash *hash, u_int8_t *data)
3818355f576SJeff Roberson {
3828355f576SJeff Roberson         uma_slab_t slab;
3838355f576SJeff Roberson         int hval;
3848355f576SJeff Roberson 
3858355f576SJeff Roberson         hval = UMA_HASH(hash, data);
3868355f576SJeff Roberson 
3878355f576SJeff Roberson         SLIST_FOREACH(slab, &hash->uh_slab_hash[hval], us_hlink) {
3888355f576SJeff Roberson                 if ((u_int8_t *)slab->us_data == data)
3898355f576SJeff Roberson                         return (slab);
3908355f576SJeff Roberson         }
3918355f576SJeff Roberson         return (NULL);
3928355f576SJeff Roberson }
3938355f576SJeff Roberson 
39499571dc3SJeff Roberson static __inline uma_slab_t
39599571dc3SJeff Roberson vtoslab(vm_offset_t va)
39699571dc3SJeff Roberson {
39799571dc3SJeff Roberson 	vm_page_t p;
39899571dc3SJeff Roberson 	uma_slab_t slab;
39999571dc3SJeff Roberson 
40099571dc3SJeff Roberson 	p = PHYS_TO_VM_PAGE(pmap_kextract(va));
40199571dc3SJeff Roberson 	slab = (uma_slab_t )p->object;
40299571dc3SJeff Roberson 
40399571dc3SJeff Roberson 	if (p->flags & PG_SLAB)
40499571dc3SJeff Roberson 		return (slab);
40599571dc3SJeff Roberson 	else
40699571dc3SJeff Roberson 		return (NULL);
40799571dc3SJeff Roberson }
40899571dc3SJeff Roberson 
40999571dc3SJeff Roberson static __inline void
41099571dc3SJeff Roberson vsetslab(vm_offset_t va, uma_slab_t slab)
41199571dc3SJeff Roberson {
41299571dc3SJeff Roberson 	vm_page_t p;
41399571dc3SJeff Roberson 
4146fc96493SOlivier Houchard 	p = PHYS_TO_VM_PAGE(pmap_kextract(va));
41599571dc3SJeff Roberson 	p->object = (vm_object_t)slab;
41699571dc3SJeff Roberson 	p->flags |= PG_SLAB;
41799571dc3SJeff Roberson }
41899571dc3SJeff Roberson 
41999571dc3SJeff Roberson static __inline void
42099571dc3SJeff Roberson vsetobj(vm_offset_t va, vm_object_t obj)
42199571dc3SJeff Roberson {
42299571dc3SJeff Roberson 	vm_page_t p;
42399571dc3SJeff Roberson 
4246fc96493SOlivier Houchard 	p = PHYS_TO_VM_PAGE(pmap_kextract(va));
42599571dc3SJeff Roberson 	p->object = obj;
42699571dc3SJeff Roberson 	p->flags &= ~PG_SLAB;
42799571dc3SJeff Roberson }
4288355f576SJeff Roberson 
42948eea375SJeff Roberson /*
43048eea375SJeff Roberson  * The following two functions may be defined by architecture specific code
43148eea375SJeff Roberson  * if they can provide more effecient allocation functions.  This is useful
43248eea375SJeff Roberson  * for using direct mapped addresses.
43348eea375SJeff Roberson  */
43448eea375SJeff Roberson void *uma_small_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait);
43548eea375SJeff Roberson void uma_small_free(void *mem, int size, u_int8_t flags);
436af17e9a9SRobert Watson #endif /* _KERNEL */
43748eea375SJeff Roberson 
4388355f576SJeff Roberson #endif /* VM_UMA_INT_H */
439