xref: /freebsd/sys/vm/uma_int.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
160727d8bSWarner Losh /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4584061b4SJeff Roberson  * Copyright (c) 2002-2019 Jeffrey Roberson <jeff@FreeBSD.org>
508ecce74SRobert Watson  * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
608ecce74SRobert Watson  * All rights reserved.
78355f576SJeff Roberson  *
88355f576SJeff Roberson  * Redistribution and use in source and binary forms, with or without
98355f576SJeff Roberson  * modification, are permitted provided that the following conditions
108355f576SJeff Roberson  * are met:
118355f576SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
128355f576SJeff Roberson  *    notice unmodified, this list of conditions, and the following
138355f576SJeff Roberson  *    disclaimer.
148355f576SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
158355f576SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
168355f576SJeff Roberson  *    documentation and/or other materials provided with the distribution.
178355f576SJeff Roberson  *
188355f576SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198355f576SJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208355f576SJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218355f576SJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228355f576SJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
238355f576SJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248355f576SJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258355f576SJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268355f576SJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278355f576SJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288355f576SJeff Roberson  *
298355f576SJeff Roberson  */
308355f576SJeff Roberson 
3139669415SGleb Smirnoff #include <sys/counter.h>
32e04223bfSMark Johnston #include <sys/_bitset.h>
337571e249SMark Johnston #include <sys/_domainset.h>
34b28cc462SGleb Smirnoff #include <sys/_task.h>
35b28cc462SGleb Smirnoff 
368355f576SJeff Roberson /*
378355f576SJeff Roberson  * This file includes definitions, structures, prototypes, and inlines that
388355f576SJeff Roberson  * should not be used outside of the actual implementation of UMA.
398355f576SJeff Roberson  */
408355f576SJeff Roberson 
418355f576SJeff Roberson /*
42ab3185d1SJeff Roberson  * The brief summary;  Zones describe unique allocation types.  Zones are
43ab3185d1SJeff Roberson  * organized into per-CPU caches which are filled by buckets.  Buckets are
44ab3185d1SJeff Roberson  * organized according to memory domains.  Buckets are filled from kegs which
45ab3185d1SJeff Roberson  * are also organized according to memory domains.  Kegs describe a unique
46ab3185d1SJeff Roberson  * allocation type, backend memory provider, and layout.  Kegs are associated
47ab3185d1SJeff Roberson  * with one or more zones and zones reference one or more kegs.  Kegs provide
48ab3185d1SJeff Roberson  * slabs which are virtually contiguous collections of pages.  Each slab is
49ab3185d1SJeff Roberson  * broken down int one or more items that will satisfy an individual allocation.
50ab3185d1SJeff Roberson  *
51ab3185d1SJeff Roberson  * Allocation is satisfied in the following order:
52ab3185d1SJeff Roberson  * 1) Per-CPU cache
53ab3185d1SJeff Roberson  * 2) Per-domain cache of buckets
54ab3185d1SJeff Roberson  * 3) Slab from any of N kegs
55ab3185d1SJeff Roberson  * 4) Backend page provider
56ab3185d1SJeff Roberson  *
57ab3185d1SJeff Roberson  * More detail on individual objects is contained below:
588355f576SJeff Roberson  *
59099a0e58SBosko Milekic  * Kegs contain lists of slabs which are stored in either the full bin, empty
608355f576SJeff Roberson  * bin, or partially allocated bin, to reduce fragmentation.  They also contain
618355f576SJeff Roberson  * the user supplied value for size, which is adjusted for alignment purposes
62099a0e58SBosko Milekic  * and rsize is the result of that.  The Keg also stores information for
638355f576SJeff Roberson  * managing a hash of page addresses that maps pages to uma_slab_t structures
648355f576SJeff Roberson  * for pages that don't have embedded uma_slab_t's.
658355f576SJeff Roberson  *
66ab3185d1SJeff Roberson  * Keg slab lists are organized by memory domain to support NUMA allocation
67ab3185d1SJeff Roberson  * policies.  By default allocations are spread across domains to reduce the
68ab3185d1SJeff Roberson  * potential for hotspots.  Special keg creation flags may be specified to
69ab3185d1SJeff Roberson  * prefer location allocation.  However there is no strict enforcement as frees
70ab3185d1SJeff Roberson  * may happen on any CPU and these are returned to the CPU-local cache
71ab3185d1SJeff Roberson  * regardless of the originating domain.
72ab3185d1SJeff Roberson  *
738355f576SJeff Roberson  * The uma_slab_t may be embedded in a UMA_SLAB_SIZE chunk of memory or it may
748355f576SJeff Roberson  * be allocated off the page from a special slab zone.  The free list within a
75ef72505eSJeff Roberson  * slab is managed with a bitmask.  For item sizes that would yield more than
76ef72505eSJeff Roberson  * 10% memory waste we potentially allocate a separate uma_slab_t if this will
77ef72505eSJeff Roberson  * improve the number of items per slab that will fit.
788355f576SJeff Roberson  *
798355f576SJeff Roberson  * The only really gross cases, with regards to memory waste, are for those
808355f576SJeff Roberson  * items that are just over half the page size.   You can get nearly 50% waste,
818355f576SJeff Roberson  * so you fall back to the memory footprint of the power of two allocator. I
828355f576SJeff Roberson  * have looked at memory allocation sizes on many of the machines available to
838355f576SJeff Roberson  * me, and there does not seem to be an abundance of allocations at this range
848355f576SJeff Roberson  * so at this time it may not make sense to optimize for it.  This can, of
858355f576SJeff Roberson  * course, be solved with dynamic slab sizes.
868355f576SJeff Roberson  *
87099a0e58SBosko Milekic  * Kegs may serve multiple Zones but by far most of the time they only serve
88099a0e58SBosko Milekic  * one.  When a Zone is created, a Keg is allocated and setup for it.  While
89099a0e58SBosko Milekic  * the backing Keg stores slabs, the Zone caches Buckets of items allocated
90099a0e58SBosko Milekic  * from the slabs.  Each Zone is equipped with an init/fini and ctor/dtor
91099a0e58SBosko Milekic  * pair, as well as with its own set of small per-CPU caches, layered above
92099a0e58SBosko Milekic  * the Zone's general Bucket cache.
93099a0e58SBosko Milekic  *
946ab3b958SRobert Watson  * The PCPU caches are protected by critical sections, and may be accessed
956ab3b958SRobert Watson  * safely only from their associated CPU, while the Zones backed by the same
966ab3b958SRobert Watson  * Keg all share a common Keg lock (to coalesce contention on the backing
976ab3b958SRobert Watson  * slabs).  The backing Keg typically only serves one Zone but in the case of
98c8b0a88bSJeff Roberson  * multiple Zones, one of the Zones is considered the Primary Zone and all
99c8b0a88bSJeff Roberson  * Zone-related stats from the Keg are done in the Primary Zone.  For an
1006ab3b958SRobert Watson  * example of a Multi-Zone setup, refer to the Mbuf allocation code.
1018355f576SJeff Roberson  */
1028355f576SJeff Roberson 
1038355f576SJeff Roberson /*
1048355f576SJeff Roberson  *	This is the representation for normal (Non OFFPAGE slab)
1058355f576SJeff Roberson  *
1068355f576SJeff Roberson  *	i == item
1078355f576SJeff Roberson  *	s == slab pointer
1088355f576SJeff Roberson  *
1098355f576SJeff Roberson  *	<----------------  Page (UMA_SLAB_SIZE) ------------------>
1108355f576SJeff Roberson  *	___________________________________________________________
1118355f576SJeff Roberson  *     | _  _  _  _  _  _  _  _  _  _  _  _  _  _  _   ___________ |
1128355f576SJeff Roberson  *     ||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i| |slab header||
1138355f576SJeff Roberson  *     ||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_| |___________||
1148355f576SJeff Roberson  *     |___________________________________________________________|
1158355f576SJeff Roberson  *
1168355f576SJeff Roberson  *
1178355f576SJeff Roberson  *	This is an OFFPAGE slab. These can be larger than UMA_SLAB_SIZE.
1188355f576SJeff Roberson  *
1198355f576SJeff Roberson  *	___________________________________________________________
1208355f576SJeff Roberson  *     | _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _   |
1218355f576SJeff Roberson  *     ||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i|  |
1228355f576SJeff Roberson  *     ||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_|  |
1238355f576SJeff Roberson  *     |___________________________________________________________|
1248355f576SJeff Roberson  *       ___________    ^
1258355f576SJeff Roberson  *	|slab header|   |
1268355f576SJeff Roberson  *	|___________|---*
1278355f576SJeff Roberson  *
1288355f576SJeff Roberson  */
1298355f576SJeff Roberson 
1308355f576SJeff Roberson #ifndef VM_UMA_INT_H
1318355f576SJeff Roberson #define VM_UMA_INT_H
1328355f576SJeff Roberson 
1338355f576SJeff Roberson #define UMA_SLAB_SIZE	PAGE_SIZE	/* How big are our slabs? */
1348355f576SJeff Roberson #define UMA_SLAB_MASK	(PAGE_SIZE - 1)	/* Mask to get back to the page */
1358355f576SJeff Roberson #define UMA_SLAB_SHIFT	PAGE_SHIFT	/* Number of bits PAGE_MASK */
1368355f576SJeff Roberson 
137ad97af7eSGleb Smirnoff /* Max waste percentage before going to off page slab management */
138ad97af7eSGleb Smirnoff #define UMA_MAX_WASTE	10
1398355f576SJeff Roberson 
1404a8b575cSRyan Libby /* Max size of a CACHESPREAD slab. */
1414a8b575cSRyan Libby #define	UMA_CACHESPREAD_MAX_SIZE	(128 * 1024)
1424a8b575cSRyan Libby 
14354c5ae80SRyan Libby /*
14454c5ae80SRyan Libby  * These flags must not overlap with the UMA_ZONE flags specified in uma.h.
14554c5ae80SRyan Libby  */
14654c5ae80SRyan Libby #define	UMA_ZFLAG_OFFPAGE	0x00200000	/*
14754c5ae80SRyan Libby 						 * Force the slab structure
14854c5ae80SRyan Libby 						 * allocation off of the real
14954c5ae80SRyan Libby 						 * memory.
15054c5ae80SRyan Libby 						 */
15154c5ae80SRyan Libby #define	UMA_ZFLAG_HASH		0x00400000	/*
15254c5ae80SRyan Libby 						 * Use a hash table instead of
15354c5ae80SRyan Libby 						 * caching information in the
15454c5ae80SRyan Libby 						 * vm_page.
15554c5ae80SRyan Libby 						 */
15654c5ae80SRyan Libby #define	UMA_ZFLAG_VTOSLAB	0x00800000	/*
15754c5ae80SRyan Libby 						 * Zone uses vtoslab for
15854c5ae80SRyan Libby 						 * lookup.
15954c5ae80SRyan Libby 						 */
16054c5ae80SRyan Libby #define	UMA_ZFLAG_CTORDTOR	0x01000000	/* Zone has ctor/dtor set. */
16154c5ae80SRyan Libby #define	UMA_ZFLAG_LIMIT		0x02000000	/* Zone has limit set. */
16254c5ae80SRyan Libby #define	UMA_ZFLAG_CACHE		0x04000000	/* uma_zcache_create()d it */
16354c5ae80SRyan Libby #define	UMA_ZFLAG_BUCKET	0x10000000	/* Bucket zone. */
16454c5ae80SRyan Libby #define	UMA_ZFLAG_INTERNAL	0x20000000	/* No offpage no PCPU. */
16554c5ae80SRyan Libby #define	UMA_ZFLAG_TRASH		0x40000000	/* Add trash ctor/dtor. */
16654c5ae80SRyan Libby 
16754c5ae80SRyan Libby #define	UMA_ZFLAG_INHERIT						\
16854c5ae80SRyan Libby     (UMA_ZFLAG_OFFPAGE | UMA_ZFLAG_HASH | UMA_ZFLAG_VTOSLAB |		\
169bae55c4aSRyan Libby      UMA_ZFLAG_BUCKET | UMA_ZFLAG_INTERNAL)
17054c5ae80SRyan Libby 
17154c5ae80SRyan Libby #define	PRINT_UMA_ZFLAGS	"\20"	\
17254c5ae80SRyan Libby     "\37TRASH"				\
17354c5ae80SRyan Libby     "\36INTERNAL"			\
17454c5ae80SRyan Libby     "\35BUCKET"				\
17554c5ae80SRyan Libby     "\33CACHE"				\
17654c5ae80SRyan Libby     "\32LIMIT"				\
17754c5ae80SRyan Libby     "\31CTORDTOR"			\
17854c5ae80SRyan Libby     "\30VTOSLAB"			\
17954c5ae80SRyan Libby     "\27HASH"				\
18054c5ae80SRyan Libby     "\26OFFPAGE"			\
181d4665eaaSJeff Roberson     "\23SMR"				\
18254c5ae80SRyan Libby     "\22ROUNDROBIN"			\
18354c5ae80SRyan Libby     "\21FIRSTTOUCH"			\
18454c5ae80SRyan Libby     "\20PCPU"				\
18554c5ae80SRyan Libby     "\17NODUMP"				\
18654c5ae80SRyan Libby     "\16CACHESPREAD"			\
18754c5ae80SRyan Libby     "\14MAXBUCKET"			\
18854c5ae80SRyan Libby     "\13NOBUCKET"			\
18954c5ae80SRyan Libby     "\12SECONDARY"			\
19054c5ae80SRyan Libby     "\11NOTPAGE"			\
19154c5ae80SRyan Libby     "\10VM"				\
19254c5ae80SRyan Libby     "\7MTXCLASS"			\
19354c5ae80SRyan Libby     "\6NOFREE"				\
19454c5ae80SRyan Libby     "\5MALLOC"				\
19554c5ae80SRyan Libby     "\4NOTOUCH"				\
196ec0d8280SRyan Libby     "\3CONTIG"				\
19754c5ae80SRyan Libby     "\2ZINIT"
1988355f576SJeff Roberson 
1998355f576SJeff Roberson /*
2001e0701e1SJeff Roberson  * Hash table for freed address -> slab translation.
2011e0701e1SJeff Roberson  *
2021e0701e1SJeff Roberson  * Only zones with memory not touchable by the allocator use the
2031e0701e1SJeff Roberson  * hash table.  Otherwise slabs are found with vtoslab().
2048355f576SJeff Roberson  */
2051e0701e1SJeff Roberson #define UMA_HASH_SIZE_INIT	32
2068355f576SJeff Roberson 
207ef72505eSJeff Roberson #define UMA_HASH(h, s) ((((uintptr_t)s) >> UMA_SLAB_SHIFT) & (h)->uh_hashmask)
2088355f576SJeff Roberson 
2098355f576SJeff Roberson #define UMA_HASH_INSERT(h, s, mem)					\
2101e0701e1SJeff Roberson 	LIST_INSERT_HEAD(&(h)->uh_slab_hash[UMA_HASH((h),		\
2119b8db4d0SRyan Libby 	    (mem))], slab_tohashslab(s), uhs_hlink)
2128355f576SJeff Roberson 
2131e0701e1SJeff Roberson #define UMA_HASH_REMOVE(h, s)						\
2149b8db4d0SRyan Libby 	LIST_REMOVE(slab_tohashslab(s), uhs_hlink)
2158355f576SJeff Roberson 
2161e0701e1SJeff Roberson LIST_HEAD(slabhashhead, uma_hash_slab);
2178355f576SJeff Roberson 
2188355f576SJeff Roberson struct uma_hash {
2191e0701e1SJeff Roberson 	struct slabhashhead	*uh_slab_hash;	/* Hash table for slabs */
2206929b7d1SPedro F. Giffuni 	u_int		uh_hashsize;	/* Current size of the hash table */
2216929b7d1SPedro F. Giffuni 	u_int		uh_hashmask;	/* Mask used during hashing */
2228355f576SJeff Roberson };
2238355f576SJeff Roberson 
2248355f576SJeff Roberson /*
22579c9f942SJeff Roberson  * Align field or structure to cache 'sector' in intel terminology.  This
22679c9f942SJeff Roberson  * is more efficient with adjacent line prefetch.
2275e4bb93cSKip Macy  */
22812f69195SJustin Hibbits #if defined(__amd64__) || defined(__powerpc64__)
22979c9f942SJeff Roberson #define UMA_SUPER_ALIGN	(CACHE_LINE_SIZE * 2)
2301a23373cSKip Macy #else
23179c9f942SJeff Roberson #define UMA_SUPER_ALIGN	CACHE_LINE_SIZE
2321a23373cSKip Macy #endif
2335e4bb93cSKip Macy 
23479c9f942SJeff Roberson #define	UMA_ALIGN	__aligned(UMA_SUPER_ALIGN)
23579c9f942SJeff Roberson 
2365e4bb93cSKip Macy /*
237376b1ba3SJeff Roberson  * The uma_bucket structure is used to queue and manage buckets divorced
238376b1ba3SJeff Roberson  * from per-cpu caches.  They are loaded into uma_cache_bucket structures
239376b1ba3SJeff Roberson  * for use.
2408355f576SJeff Roberson  */
2418355f576SJeff Roberson struct uma_bucket {
242dc3915c8SJeff Roberson 	STAILQ_ENTRY(uma_bucket)	ub_link; /* Link into the zone */
243306abf0fSGleb Smirnoff 	int16_t		ub_cnt;			/* Count of items in bucket. */
244cae33c14SJeff Roberson 	int16_t		ub_entries;		/* Max items. */
245d4665eaaSJeff Roberson 	smr_seq_t	ub_seq;			/* SMR sequence number. */
246cae33c14SJeff Roberson 	void		*ub_bucket[];		/* actual allocation storage */
2471a23373cSKip Macy };
2488355f576SJeff Roberson 
2498355f576SJeff Roberson typedef struct uma_bucket * uma_bucket_t;
2508355f576SJeff Roberson 
251376b1ba3SJeff Roberson /*
252376b1ba3SJeff Roberson  * The uma_cache_bucket structure is statically allocated on each per-cpu
253376b1ba3SJeff Roberson  * cache.  Its use reduces branches and cache misses in the fast path.
254376b1ba3SJeff Roberson  */
255376b1ba3SJeff Roberson struct uma_cache_bucket {
256376b1ba3SJeff Roberson 	uma_bucket_t	ucb_bucket;
257376b1ba3SJeff Roberson 	int16_t		ucb_cnt;
258376b1ba3SJeff Roberson 	int16_t		ucb_entries;
259376b1ba3SJeff Roberson 	uint32_t	ucb_spare;
260376b1ba3SJeff Roberson };
261376b1ba3SJeff Roberson 
262376b1ba3SJeff Roberson typedef struct uma_cache_bucket * uma_cache_bucket_t;
263376b1ba3SJeff Roberson 
264376b1ba3SJeff Roberson /*
265376b1ba3SJeff Roberson  * The uma_cache structure is allocated for each cpu for every zone
266376b1ba3SJeff Roberson  * type.  This optimizes synchronization out of the allocator fast path.
267376b1ba3SJeff Roberson  */
2688355f576SJeff Roberson struct uma_cache {
269376b1ba3SJeff Roberson 	struct uma_cache_bucket	uc_freebucket;	/* Bucket we're freeing to */
270376b1ba3SJeff Roberson 	struct uma_cache_bucket	uc_allocbucket;	/* Bucket to allocate from */
271376b1ba3SJeff Roberson 	struct uma_cache_bucket	uc_crossbucket;	/* cross domain bucket */
27285dcf349SGleb Smirnoff 	uint64_t		uc_allocs;	/* Count of allocations */
27385dcf349SGleb Smirnoff 	uint64_t		uc_frees;	/* Count of frees */
2745e4bb93cSKip Macy } UMA_ALIGN;
2758355f576SJeff Roberson 
2768355f576SJeff Roberson typedef struct uma_cache * uma_cache_t;
2778355f576SJeff Roberson 
2781e0701e1SJeff Roberson LIST_HEAD(slabhead, uma_slab);
2791e0701e1SJeff Roberson 
2808355f576SJeff Roberson /*
281cc7ce83aSJeff Roberson  * The cache structure pads perfectly into 64 bytes so we use spare
282cc7ce83aSJeff Roberson  * bits from the embedded cache buckets to store information from the zone
283cc7ce83aSJeff Roberson  * and keep all fast-path allocations accessing a single per-cpu line.
284cc7ce83aSJeff Roberson  */
285cc7ce83aSJeff Roberson static inline void
cache_set_uz_flags(uma_cache_t cache,uint32_t flags)286cc7ce83aSJeff Roberson cache_set_uz_flags(uma_cache_t cache, uint32_t flags)
287cc7ce83aSJeff Roberson {
288cc7ce83aSJeff Roberson 
289cc7ce83aSJeff Roberson 	cache->uc_freebucket.ucb_spare = flags;
290cc7ce83aSJeff Roberson }
291cc7ce83aSJeff Roberson 
292cc7ce83aSJeff Roberson static inline void
cache_set_uz_size(uma_cache_t cache,uint32_t size)293cc7ce83aSJeff Roberson cache_set_uz_size(uma_cache_t cache, uint32_t size)
294cc7ce83aSJeff Roberson {
295cc7ce83aSJeff Roberson 
296cc7ce83aSJeff Roberson 	cache->uc_allocbucket.ucb_spare = size;
297cc7ce83aSJeff Roberson }
298cc7ce83aSJeff Roberson 
299cc7ce83aSJeff Roberson static inline uint32_t
cache_uz_flags(uma_cache_t cache)300cc7ce83aSJeff Roberson cache_uz_flags(uma_cache_t cache)
301cc7ce83aSJeff Roberson {
302cc7ce83aSJeff Roberson 
303cc7ce83aSJeff Roberson 	return (cache->uc_freebucket.ucb_spare);
304cc7ce83aSJeff Roberson }
305cc7ce83aSJeff Roberson 
306cc7ce83aSJeff Roberson static inline uint32_t
cache_uz_size(uma_cache_t cache)307cc7ce83aSJeff Roberson cache_uz_size(uma_cache_t cache)
308cc7ce83aSJeff Roberson {
309cc7ce83aSJeff Roberson 
310cc7ce83aSJeff Roberson 	return (cache->uc_allocbucket.ucb_spare);
311cc7ce83aSJeff Roberson }
312cc7ce83aSJeff Roberson 
313cc7ce83aSJeff Roberson /*
314376b1ba3SJeff Roberson  * Per-domain slab lists.  Embedded in the kegs.
315ab3185d1SJeff Roberson  */
316ab3185d1SJeff Roberson struct uma_domain {
3178b987a77SJeff Roberson 	struct mtx_padalign ud_lock;	/* Lock for the domain lists. */
3181e0701e1SJeff Roberson 	struct slabhead	ud_part_slab;	/* partially allocated slabs */
3191e0701e1SJeff Roberson 	struct slabhead	ud_free_slab;	/* completely unallocated slabs */
3201e0701e1SJeff Roberson 	struct slabhead ud_full_slab;	/* fully allocated slabs */
3218b987a77SJeff Roberson 	uint32_t	ud_pages;	/* Total page count */
3224ab3aee8SMark Johnston 	uint32_t	ud_free_items;	/* Count of items free in all slabs */
3234ab3aee8SMark Johnston 	uint32_t	ud_free_slabs;	/* Count of free slabs */
324727c6918SJeff Roberson } __aligned(CACHE_LINE_SIZE);
325ab3185d1SJeff Roberson 
326ab3185d1SJeff Roberson typedef struct uma_domain * uma_domain_t;
327ab3185d1SJeff Roberson 
328ab3185d1SJeff Roberson /*
329099a0e58SBosko Milekic  * Keg management structure
330099a0e58SBosko Milekic  *
331099a0e58SBosko Milekic  * TODO: Optimize for cache line size
332099a0e58SBosko Milekic  *
333099a0e58SBosko Milekic  */
334099a0e58SBosko Milekic struct uma_keg {
335099a0e58SBosko Milekic 	struct uma_hash	uk_hash;
336099a0e58SBosko Milekic 	LIST_HEAD(,uma_zone)	uk_zones;	/* Keg's zones */
337099a0e58SBosko Milekic 
338194a979eSMark Johnston 	struct domainset_ref uk_dr;	/* Domain selection policy. */
33985dcf349SGleb Smirnoff 	uint32_t	uk_align;	/* Alignment mask */
3406fd34d6fSJeff Roberson 	uint32_t	uk_reserve;	/* Number of reserved items. */
34185dcf349SGleb Smirnoff 	uint32_t	uk_size;	/* Requested size of each item */
34285dcf349SGleb Smirnoff 	uint32_t	uk_rsize;	/* Real size of each item */
343099a0e58SBosko Milekic 
344099a0e58SBosko Milekic 	uma_init	uk_init;	/* Keg's init routine */
345099a0e58SBosko Milekic 	uma_fini	uk_fini;	/* Keg's fini routine */
346099a0e58SBosko Milekic 	uma_alloc	uk_allocf;	/* Allocation function */
347099a0e58SBosko Milekic 	uma_free	uk_freef;	/* Free routine */
348099a0e58SBosko Milekic 
349a4915c21SAttilio Rao 	u_long		uk_offset;	/* Next free offset from base KVA */
350a4915c21SAttilio Rao 	vm_offset_t	uk_kva;		/* Zone base KVA */
351099a0e58SBosko Milekic 
3522d54d4bbSMark Johnston 	uint32_t	uk_pgoff;	/* Offset to uma_slab struct */
35385dcf349SGleb Smirnoff 	uint16_t	uk_ppera;	/* pages per allocation from backend */
35485dcf349SGleb Smirnoff 	uint16_t	uk_ipers;	/* Items per slab */
35585dcf349SGleb Smirnoff 	uint32_t	uk_flags;	/* Internal flags */
356ad97af7eSGleb Smirnoff 
357ad97af7eSGleb Smirnoff 	/* Least used fields go to the last cache line. */
358ad97af7eSGleb Smirnoff 	const char	*uk_name;		/* Name of creating zone. */
359ad97af7eSGleb Smirnoff 	LIST_ENTRY(uma_keg)	uk_link;	/* List of all kegs */
360ab3185d1SJeff Roberson 
361ab3185d1SJeff Roberson 	/* Must be last, variable sized. */
362ab3185d1SJeff Roberson 	struct uma_domain	uk_domain[];	/* Keg's slab lists. */
363099a0e58SBosko Milekic };
364099a0e58SBosko Milekic typedef struct uma_keg	* uma_keg_t;
365099a0e58SBosko Milekic 
366ef72505eSJeff Roberson /*
367ef72505eSJeff Roberson  * Free bits per-slab.
368ef72505eSJeff Roberson  */
3699b78b1f4SJeff Roberson #define	SLAB_MAX_SETSIZE	(PAGE_SIZE / UMA_SMALLEST_UNIT)
3709b78b1f4SJeff Roberson #define	SLAB_MIN_SETSIZE	_BITSET_BITS
3719b78b1f4SJeff Roberson BITSET_DEFINE(noslabbits, 0);
372099a0e58SBosko Milekic 
373ef72505eSJeff Roberson /*
374ef72505eSJeff Roberson  * The slab structure manages a single contiguous allocation from backing
375ef72505eSJeff Roberson  * store and subdivides it into individually allocatable items.
376ef72505eSJeff Roberson  */
377ef72505eSJeff Roberson struct uma_slab {
3786d6a03d7SJeff Roberson 	LIST_ENTRY(uma_slab)	us_link;	/* slabs in zone */
37985dcf349SGleb Smirnoff 	uint16_t	us_freecount;		/* How many are free? */
38085dcf349SGleb Smirnoff 	uint8_t		us_flags;		/* Page flags see uma.h */
381ab3185d1SJeff Roberson 	uint8_t		us_domain;		/* Backing NUMA domain. */
382815db204SRyan Libby 	struct noslabbits us_free;		/* Free bitmask, flexible. */
383099a0e58SBosko Milekic };
38454007ce8SMark Johnston _Static_assert(sizeof(struct uma_slab) == __offsetof(struct uma_slab, us_free),
385815db204SRyan Libby     "us_free field must be last");
38654007ce8SMark Johnston _Static_assert(MAXMEMDOM < 255,
38754007ce8SMark Johnston     "us_domain field is not wide enough");
388ab3185d1SJeff Roberson 
389099a0e58SBosko Milekic typedef struct uma_slab * uma_slab_t;
390e20a199fSJeff Roberson 
391815db204SRyan Libby /*
3921e0701e1SJeff Roberson  * Slab structure with a full sized bitset and hash link for both
3931e0701e1SJeff Roberson  * HASH and OFFPAGE zones.
3941e0701e1SJeff Roberson  */
3951e0701e1SJeff Roberson struct uma_hash_slab {
3961e0701e1SJeff Roberson 	LIST_ENTRY(uma_hash_slab) uhs_hlink;	/* Link for hash table */
3971e0701e1SJeff Roberson 	uint8_t			*uhs_data;	/* First item */
3989b8db4d0SRyan Libby 	struct uma_slab		uhs_slab;	/* Must be last. */
3991e0701e1SJeff Roberson };
4001e0701e1SJeff Roberson 
4011e0701e1SJeff Roberson typedef struct uma_hash_slab * uma_hash_slab_t;
4021e0701e1SJeff Roberson 
4039b8db4d0SRyan Libby static inline uma_hash_slab_t
slab_tohashslab(uma_slab_t slab)4049b8db4d0SRyan Libby slab_tohashslab(uma_slab_t slab)
4059b8db4d0SRyan Libby {
4069b8db4d0SRyan Libby 
4079b8db4d0SRyan Libby 	return (__containerof(slab, struct uma_hash_slab, uhs_slab));
4089b8db4d0SRyan Libby }
4099b8db4d0SRyan Libby 
4101e0701e1SJeff Roberson static inline void *
slab_data(uma_slab_t slab,uma_keg_t keg)4111e0701e1SJeff Roberson slab_data(uma_slab_t slab, uma_keg_t keg)
4121e0701e1SJeff Roberson {
4131e0701e1SJeff Roberson 
41454c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) == 0)
4151e0701e1SJeff Roberson 		return ((void *)((uintptr_t)slab - keg->uk_pgoff));
4161e0701e1SJeff Roberson 	else
4179b8db4d0SRyan Libby 		return (slab_tohashslab(slab)->uhs_data);
4181e0701e1SJeff Roberson }
4191e0701e1SJeff Roberson 
4201e0701e1SJeff Roberson static inline void *
slab_item(uma_slab_t slab,uma_keg_t keg,int index)4211e0701e1SJeff Roberson slab_item(uma_slab_t slab, uma_keg_t keg, int index)
4221e0701e1SJeff Roberson {
4231e0701e1SJeff Roberson 	uintptr_t data;
4241e0701e1SJeff Roberson 
4251e0701e1SJeff Roberson 	data = (uintptr_t)slab_data(slab, keg);
4261e0701e1SJeff Roberson 	return ((void *)(data + keg->uk_rsize * index));
4271e0701e1SJeff Roberson }
4281e0701e1SJeff Roberson 
4291e0701e1SJeff Roberson static inline int
slab_item_index(uma_slab_t slab,uma_keg_t keg,void * item)4301e0701e1SJeff Roberson slab_item_index(uma_slab_t slab, uma_keg_t keg, void *item)
4311e0701e1SJeff Roberson {
4321e0701e1SJeff Roberson 	uintptr_t data;
4331e0701e1SJeff Roberson 
4341e0701e1SJeff Roberson 	data = (uintptr_t)slab_data(slab, keg);
4351e0701e1SJeff Roberson 	return (((uintptr_t)item - data) / keg->uk_rsize);
4361e0701e1SJeff Roberson }
4371e0701e1SJeff Roberson 
438dc3915c8SJeff Roberson STAILQ_HEAD(uma_bucketlist, uma_bucket);
43908cfa56eSMark Johnston 
440ab3185d1SJeff Roberson struct uma_zone_domain {
44108cfa56eSMark Johnston 	struct uma_bucketlist uzd_buckets; /* full buckets */
44291d947bfSJeff Roberson 	uma_bucket_t	uzd_cross;	/* Fills from cross buckets. */
4430f9b7bf3SMark Johnston 	long		uzd_nitems;	/* total item count */
4440f9b7bf3SMark Johnston 	long		uzd_imax;	/* maximum item count this period */
4450f9b7bf3SMark Johnston 	long		uzd_imin;	/* minimum item count this period */
4462760658bSAlexander Motin 	long		uzd_bimin;	/* Minimum item count this batch. */
4470f9b7bf3SMark Johnston 	long		uzd_wss;	/* working set size estimate */
4482760658bSAlexander Motin 	long		uzd_limin;	/* Longtime minimum item count. */
4492760658bSAlexander Motin 	u_int		uzd_timin;	/* Time since uzd_limin == 0. */
450c6fd3e23SJeff Roberson 	smr_seq_t	uzd_seq;	/* Lowest queued seq. */
451c6fd3e23SJeff Roberson 	struct mtx	uzd_lock;	/* Lock for the domain */
452727c6918SJeff Roberson } __aligned(CACHE_LINE_SIZE);
453ab3185d1SJeff Roberson 
454ab3185d1SJeff Roberson typedef struct uma_zone_domain * uma_zone_domain_t;
455ab3185d1SJeff Roberson 
456244f4554SBosko Milekic /*
4574bd61e19SJeff Roberson  * Zone structure - per memory type.
4588355f576SJeff Roberson  */
4598355f576SJeff Roberson struct uma_zone {
46063b5557bSJeff Roberson 	/* Offset 0, used in alloc/free fast/medium fast path and const. */
46163b5557bSJeff Roberson 	uint32_t	uz_flags;	/* Flags inherited from kegs */
46263b5557bSJeff Roberson 	uint32_t	uz_size;	/* Size inherited from kegs */
4638355f576SJeff Roberson 	uma_ctor	uz_ctor;	/* Constructor for each allocation */
4648355f576SJeff Roberson 	uma_dtor	uz_dtor;	/* Destructor */
465d4665eaaSJeff Roberson 	smr_t		uz_smr;		/* Safe memory reclaim context. */
466bb15d1c7SGleb Smirnoff 	uint64_t	uz_max_items;	/* Maximum number of items to alloc */
467c6fd3e23SJeff Roberson 	uint64_t	uz_bucket_max;	/* Maximum bucket cache size */
46820a4e154SJeff Roberson 	uint16_t	uz_bucket_size;	/* Number of items in full bucket */
46920a4e154SJeff Roberson 	uint16_t	uz_bucket_size_max; /* Maximum number of bucket items */
470c6fd3e23SJeff Roberson 	uint32_t	uz_sleepers;	/* Threads sleeping on limit */
471c6fd3e23SJeff Roberson 	counter_u64_t	uz_xdomain;	/* Total number of cross-domain frees */
47263b5557bSJeff Roberson 
47363b5557bSJeff Roberson 	/* Offset 64, used in bucket replenish. */
474c6fd3e23SJeff Roberson 	uma_keg_t	uz_keg;		/* This zone's keg if !CACHE */
4750095a784SJeff Roberson 	uma_import	uz_import;	/* Import new memory to cache. */
4760095a784SJeff Roberson 	uma_release	uz_release;	/* Release memory from cache. */
4770095a784SJeff Roberson 	void		*uz_arg;	/* Import/release argument. */
478bb15d1c7SGleb Smirnoff 	uma_init	uz_init;	/* Initializer for each item */
479bb15d1c7SGleb Smirnoff 	uma_fini	uz_fini;	/* Finalizer for each item. */
480c6fd3e23SJeff Roberson 	volatile uint64_t uz_items;	/* Total items count & sleepers */
481c6fd3e23SJeff Roberson 	uint64_t	uz_sleeps;	/* Total number of alloc sleeps */
482099a0e58SBosko Milekic 
483c6fd3e23SJeff Roberson 	/* Offset 128 Rare stats, misc read-only. */
484bb15d1c7SGleb Smirnoff 	LIST_ENTRY(uma_zone) uz_link;	/* List of all zones in keg */
4852efcc8cbSGleb Smirnoff 	counter_u64_t	uz_allocs;	/* Total number of allocations */
4862efcc8cbSGleb Smirnoff 	counter_u64_t	uz_frees;	/* Total number of frees */
4872efcc8cbSGleb Smirnoff 	counter_u64_t	uz_fails;	/* Total number of alloc failures */
488c6fd3e23SJeff Roberson 	const char	*uz_name;	/* Text name of the zone */
48920a4e154SJeff Roberson 	char		*uz_ctlname;	/* sysctl safe name string. */
49020a4e154SJeff Roberson 	int		uz_namecnt;	/* duplicate name count. */
491c6fd3e23SJeff Roberson 	uint16_t	uz_bucket_size_min; /* Min number of items in bucket */
492aabe13f1SMark Johnston 	uint16_t	uz_reclaimers;	/* pending reclaim operations. */
493c6fd3e23SJeff Roberson 
494c6fd3e23SJeff Roberson 	/* Offset 192, rare read-only. */
495c6fd3e23SJeff Roberson 	struct sysctl_oid *uz_oid;	/* sysctl oid pointer. */
496c6fd3e23SJeff Roberson 	const char	*uz_warning;	/* Warning to print on failure */
497c6fd3e23SJeff Roberson 	struct timeval	uz_ratecheck;	/* Warnings rate-limiting */
498c6fd3e23SJeff Roberson 	struct task	uz_maxaction;	/* Task to run when at limit */
499c6fd3e23SJeff Roberson 
500c6fd3e23SJeff Roberson 	/* Offset 256. */
501c6fd3e23SJeff Roberson 	struct mtx	uz_cross_lock;	/* Cross domain free lock */
50254503a13SJonathan T. Looney 
5038355f576SJeff Roberson 	/*
5048355f576SJeff Roberson 	 * This HAS to be the last item because we adjust the zone size
5058355f576SJeff Roberson 	 * based on NCPU and then allocate the space for the zones.
5068355f576SJeff Roberson 	 */
507ab3185d1SJeff Roberson 	struct uma_cache	uz_cpu[]; /* Per cpu caches */
508ab3185d1SJeff Roberson 
509c6fd3e23SJeff Roberson 	/* domains follow here. */
5108355f576SJeff Roberson };
5118355f576SJeff Roberson 
512b60f5b79SJeff Roberson /*
5134bd61e19SJeff Roberson  * Macros for interpreting the uz_items field.  20 bits of sleeper count
5144bd61e19SJeff Roberson  * and 44 bit of item count.
5154bd61e19SJeff Roberson  */
5164bd61e19SJeff Roberson #define	UZ_ITEMS_SLEEPER_SHIFT	44LL
5174bd61e19SJeff Roberson #define	UZ_ITEMS_SLEEPERS_MAX	((1 << (64 - UZ_ITEMS_SLEEPER_SHIFT)) - 1)
5184bd61e19SJeff Roberson #define	UZ_ITEMS_COUNT_MASK	((1LL << UZ_ITEMS_SLEEPER_SHIFT) - 1)
5194bd61e19SJeff Roberson #define	UZ_ITEMS_COUNT(x)	((x) & UZ_ITEMS_COUNT_MASK)
5204bd61e19SJeff Roberson #define	UZ_ITEMS_SLEEPERS(x)	((x) >> UZ_ITEMS_SLEEPER_SHIFT)
5214bd61e19SJeff Roberson #define	UZ_ITEMS_SLEEPER	(1LL << UZ_ITEMS_SLEEPER_SHIFT)
5224bd61e19SJeff Roberson 
523727c6918SJeff Roberson #define	ZONE_ASSERT_COLD(z)						\
52431c251a0SJeff Roberson 	KASSERT(uma_zone_get_allocs((z)) == 0,				\
525727c6918SJeff Roberson 	    ("zone %s initialization after use.", (z)->uz_name))
526727c6918SJeff Roberson 
527a2e19465SEric van Gyzen /* Domains are contiguous after the last CPU */
528a2e19465SEric van Gyzen #define	ZDOM_GET(z, n)							\
529a2e19465SEric van Gyzen 	(&((uma_zone_domain_t)&(z)->uz_cpu[mp_maxid + 1])[n])
530a2e19465SEric van Gyzen 
5315e4bb93cSKip Macy #undef	UMA_ALIGN
5325e4bb93cSKip Macy 
533af17e9a9SRobert Watson #ifdef _KERNEL
5348355f576SJeff Roberson /* Internal prototypes */
53585dcf349SGleb Smirnoff static __inline uma_slab_t hash_sfind(struct uma_hash *hash, uint8_t *data);
5368355f576SJeff Roberson 
5378355f576SJeff Roberson /* Lock Macros */
5388355f576SJeff Roberson 
5398b987a77SJeff Roberson #define	KEG_LOCKPTR(k, d)	(struct mtx *)&(k)->uk_domain[(d)].ud_lock
5408b987a77SJeff Roberson #define	KEG_LOCK_INIT(k, d, lc)						\
54128bc4419SJeff Roberson 	do {								\
54228bc4419SJeff Roberson 		if ((lc))						\
5438b987a77SJeff Roberson 			mtx_init(KEG_LOCKPTR(k, d), (k)->uk_name,	\
544e20a199fSJeff Roberson 			    (k)->uk_name, MTX_DEF | MTX_DUPOK);		\
54528bc4419SJeff Roberson 		else							\
5468b987a77SJeff Roberson 			mtx_init(KEG_LOCKPTR(k, d), (k)->uk_name,	\
54728bc4419SJeff Roberson 			    "UMA zone", MTX_DEF | MTX_DUPOK);		\
54828bc4419SJeff Roberson 	} while (0)
54928bc4419SJeff Roberson 
5508b987a77SJeff Roberson #define	KEG_LOCK_FINI(k, d)	mtx_destroy(KEG_LOCKPTR(k, d))
5518b987a77SJeff Roberson #define	KEG_LOCK(k, d)							\
5528b987a77SJeff Roberson 	({ mtx_lock(KEG_LOCKPTR(k, d)); KEG_LOCKPTR(k, d); })
5538b987a77SJeff Roberson #define	KEG_UNLOCK(k, d)	mtx_unlock(KEG_LOCKPTR(k, d))
5548b987a77SJeff Roberson #define	KEG_LOCK_ASSERT(k, d)	mtx_assert(KEG_LOCKPTR(k, d), MA_OWNED)
555bb15d1c7SGleb Smirnoff 
556bb15d1c7SGleb Smirnoff #define	KEG_GET(zone, keg) do {					\
557bb15d1c7SGleb Smirnoff 	(keg) = (zone)->uz_keg;					\
558c6fd3e23SJeff Roberson 	KASSERT((void *)(keg) != NULL,				\
559bb15d1c7SGleb Smirnoff 	    ("%s: Invalid zone %p type", __func__, (zone)));	\
560bb15d1c7SGleb Smirnoff 	} while (0)
561af526374SJeff Roberson 
56254007ce8SMark Johnston #define	KEG_ASSERT_COLD(k)						\
56354007ce8SMark Johnston 	KASSERT(uma_keg_get_allocs((k)) == 0,				\
56454007ce8SMark Johnston 	    ("keg %s initialization after use.", (k)->uk_name))
56554007ce8SMark Johnston 
566c6fd3e23SJeff Roberson #define	ZDOM_LOCK_INIT(z, zdom, lc)					\
567af526374SJeff Roberson 	do {								\
568af526374SJeff Roberson 		if ((lc))						\
569c6fd3e23SJeff Roberson 			mtx_init(&(zdom)->uzd_lock, (z)->uz_name,	\
570af526374SJeff Roberson 			    (z)->uz_name, MTX_DEF | MTX_DUPOK);		\
571af526374SJeff Roberson 		else							\
572c6fd3e23SJeff Roberson 			mtx_init(&(zdom)->uzd_lock, (z)->uz_name,	\
573af526374SJeff Roberson 			    "UMA zone", MTX_DEF | MTX_DUPOK);		\
574af526374SJeff Roberson 	} while (0)
575c6fd3e23SJeff Roberson #define	ZDOM_LOCK_FINI(z)	mtx_destroy(&(z)->uzd_lock)
576c6fd3e23SJeff Roberson #define	ZDOM_LOCK_ASSERT(z)	mtx_assert(&(z)->uzd_lock, MA_OWNED)
577af526374SJeff Roberson 
578c6fd3e23SJeff Roberson #define	ZDOM_LOCK(z)	mtx_lock(&(z)->uzd_lock)
579c6fd3e23SJeff Roberson #define	ZDOM_OWNED(z)	(mtx_owner(&(z)->uzd_lock) != NULL)
580c6fd3e23SJeff Roberson #define	ZDOM_UNLOCK(z)	mtx_unlock(&(z)->uzd_lock)
581c6fd3e23SJeff Roberson 
582c6fd3e23SJeff Roberson #define	ZONE_LOCK(z)	ZDOM_LOCK(ZDOM_GET((z), 0))
583c6fd3e23SJeff Roberson #define	ZONE_UNLOCK(z)	ZDOM_UNLOCK(ZDOM_GET((z), 0))
584aabe13f1SMark Johnston #define	ZONE_LOCKPTR(z)	(&ZDOM_GET((z), 0)->uzd_lock)
5858355f576SJeff Roberson 
58691d947bfSJeff Roberson #define	ZONE_CROSS_LOCK_INIT(z)					\
58791d947bfSJeff Roberson 	mtx_init(&(z)->uz_cross_lock, "UMA Cross", NULL, MTX_DEF)
58891d947bfSJeff Roberson #define	ZONE_CROSS_LOCK(z)	mtx_lock(&(z)->uz_cross_lock)
58991d947bfSJeff Roberson #define	ZONE_CROSS_UNLOCK(z)	mtx_unlock(&(z)->uz_cross_lock)
59091d947bfSJeff Roberson #define	ZONE_CROSS_LOCK_FINI(z)	mtx_destroy(&(z)->uz_cross_lock)
59191d947bfSJeff Roberson 
5928355f576SJeff Roberson /*
5938355f576SJeff Roberson  * Find a slab within a hash table.  This is used for OFFPAGE zones to lookup
5948355f576SJeff Roberson  * the slab structure.
5958355f576SJeff Roberson  *
5968355f576SJeff Roberson  * Arguments:
5978355f576SJeff Roberson  *	hash  The hash table to search.
5988355f576SJeff Roberson  *	data  The base page of the item.
5998355f576SJeff Roberson  *
6008355f576SJeff Roberson  * Returns:
6018355f576SJeff Roberson  *	A pointer to a slab if successful, else NULL.
6028355f576SJeff Roberson  */
6038355f576SJeff Roberson static __inline uma_slab_t
hash_sfind(struct uma_hash * hash,uint8_t * data)60485dcf349SGleb Smirnoff hash_sfind(struct uma_hash *hash, uint8_t *data)
6058355f576SJeff Roberson {
6061e0701e1SJeff Roberson         uma_hash_slab_t slab;
6076929b7d1SPedro F. Giffuni         u_int hval;
6088355f576SJeff Roberson 
6098355f576SJeff Roberson         hval = UMA_HASH(hash, data);
6108355f576SJeff Roberson 
6111e0701e1SJeff Roberson         LIST_FOREACH(slab, &hash->uh_slab_hash[hval], uhs_hlink) {
6121e0701e1SJeff Roberson                 if ((uint8_t *)slab->uhs_data == data)
6131e0701e1SJeff Roberson                         return (&slab->uhs_slab);
6148355f576SJeff Roberson         }
6158355f576SJeff Roberson         return (NULL);
6168355f576SJeff Roberson }
6178355f576SJeff Roberson 
61899571dc3SJeff Roberson static __inline uma_slab_t
vtoslab(vm_offset_t va)61999571dc3SJeff Roberson vtoslab(vm_offset_t va)
62099571dc3SJeff Roberson {
62199571dc3SJeff Roberson 	vm_page_t p;
62299571dc3SJeff Roberson 
62399571dc3SJeff Roberson 	p = PHYS_TO_VM_PAGE(pmap_kextract(va));
624584061b4SJeff Roberson 	return (p->plinks.uma.slab);
62599571dc3SJeff Roberson }
62699571dc3SJeff Roberson 
62799571dc3SJeff Roberson static __inline void
vtozoneslab(vm_offset_t va,uma_zone_t * zone,uma_slab_t * slab)628584061b4SJeff Roberson vtozoneslab(vm_offset_t va, uma_zone_t *zone, uma_slab_t *slab)
62999571dc3SJeff Roberson {
63099571dc3SJeff Roberson 	vm_page_t p;
63199571dc3SJeff Roberson 
6326fc96493SOlivier Houchard 	p = PHYS_TO_VM_PAGE(pmap_kextract(va));
633584061b4SJeff Roberson 	*slab = p->plinks.uma.slab;
634584061b4SJeff Roberson 	*zone = p->plinks.uma.zone;
635584061b4SJeff Roberson }
636584061b4SJeff Roberson 
637584061b4SJeff Roberson static __inline void
vsetzoneslab(vm_offset_t va,uma_zone_t zone,uma_slab_t slab)638584061b4SJeff Roberson vsetzoneslab(vm_offset_t va, uma_zone_t zone, uma_slab_t slab)
639584061b4SJeff Roberson {
640584061b4SJeff Roberson 	vm_page_t p;
641584061b4SJeff Roberson 
642584061b4SJeff Roberson 	p = PHYS_TO_VM_PAGE(pmap_kextract(va));
643584061b4SJeff Roberson 	p->plinks.uma.slab = slab;
644584061b4SJeff Roberson 	p->plinks.uma.zone = zone;
64599571dc3SJeff Roberson }
64699571dc3SJeff Roberson 
6476d6a03d7SJeff Roberson extern unsigned long uma_kmem_limit;
6486d6a03d7SJeff Roberson extern unsigned long uma_kmem_total;
6496d6a03d7SJeff Roberson 
6506d6a03d7SJeff Roberson /* Adjust bytes under management by UMA. */
6516d6a03d7SJeff Roberson static inline void
uma_total_dec(unsigned long size)6526d6a03d7SJeff Roberson uma_total_dec(unsigned long size)
6536d6a03d7SJeff Roberson {
6546d6a03d7SJeff Roberson 
6556d6a03d7SJeff Roberson 	atomic_subtract_long(&uma_kmem_total, size);
6566d6a03d7SJeff Roberson }
6576d6a03d7SJeff Roberson 
6586d6a03d7SJeff Roberson static inline void
uma_total_inc(unsigned long size)6596d6a03d7SJeff Roberson uma_total_inc(unsigned long size)
6606d6a03d7SJeff Roberson {
6616d6a03d7SJeff Roberson 
6626d6a03d7SJeff Roberson 	if (atomic_fetchadd_long(&uma_kmem_total, size) > uma_kmem_limit)
6636d6a03d7SJeff Roberson 		uma_reclaim_wakeup();
6646d6a03d7SJeff Roberson }
6656d6a03d7SJeff Roberson 
66648eea375SJeff Roberson /*
66748eea375SJeff Roberson  * The following two functions may be defined by architecture specific code
668763df3ecSPedro F. Giffuni  * if they can provide more efficient allocation functions.  This is useful
66948eea375SJeff Roberson  * for using direct mapped addresses.
67048eea375SJeff Roberson  */
671ab3185d1SJeff Roberson void *uma_small_alloc(uma_zone_t zone, vm_size_t bytes, int domain,
672ab3185d1SJeff Roberson     uint8_t *pflag, int wait);
673f2c2231eSRyan Stone void uma_small_free(void *mem, vm_size_t size, uint8_t flags);
6742e47807cSJeff Roberson 
6752e47807cSJeff Roberson /* Set a global soft limit on UMA managed memory. */
6762e47807cSJeff Roberson void uma_set_limit(unsigned long limit);
677c6fd3e23SJeff Roberson 
678af17e9a9SRobert Watson #endif /* _KERNEL */
67948eea375SJeff Roberson 
6808355f576SJeff Roberson #endif /* VM_UMA_INT_H */
681