xref: /f-stack/tools/compat/include/vm/uma_int.h (revision d4a07e70)
11eaf0ac3Slogwang /*-
2*d4a07e70Sfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d4a07e70Sfengbojiang  *
4*d4a07e70Sfengbojiang  * Copyright (c) 2002-2019 Jeffrey Roberson <[email protected]>
51eaf0ac3Slogwang  * Copyright (c) 2004, 2005 Bosko Milekic <[email protected]>
61eaf0ac3Slogwang  * All rights reserved.
71eaf0ac3Slogwang  *
81eaf0ac3Slogwang  * Redistribution and use in source and binary forms, with or without
91eaf0ac3Slogwang  * modification, are permitted provided that the following conditions
101eaf0ac3Slogwang  * are met:
111eaf0ac3Slogwang  * 1. Redistributions of source code must retain the above copyright
121eaf0ac3Slogwang  *    notice unmodified, this list of conditions, and the following
131eaf0ac3Slogwang  *    disclaimer.
141eaf0ac3Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
151eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer in the
161eaf0ac3Slogwang  *    documentation and/or other materials provided with the distribution.
171eaf0ac3Slogwang  *
181eaf0ac3Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
191eaf0ac3Slogwang  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201eaf0ac3Slogwang  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
211eaf0ac3Slogwang  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
221eaf0ac3Slogwang  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
231eaf0ac3Slogwang  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241eaf0ac3Slogwang  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251eaf0ac3Slogwang  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261eaf0ac3Slogwang  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
271eaf0ac3Slogwang  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281eaf0ac3Slogwang  *
291eaf0ac3Slogwang  * $FreeBSD$
301eaf0ac3Slogwang  *
311eaf0ac3Slogwang  */
321eaf0ac3Slogwang 
33*d4a07e70Sfengbojiang #include <sys/counter.h>
34*d4a07e70Sfengbojiang #include <sys/_bitset.h>
35*d4a07e70Sfengbojiang #include <sys/_domainset.h>
361eaf0ac3Slogwang #include <sys/_task.h>
371eaf0ac3Slogwang 
381eaf0ac3Slogwang /*
391eaf0ac3Slogwang  * This file includes definitions, structures, prototypes, and inlines that
401eaf0ac3Slogwang  * should not be used outside of the actual implementation of UMA.
411eaf0ac3Slogwang  */
421eaf0ac3Slogwang 
431eaf0ac3Slogwang /*
44*d4a07e70Sfengbojiang  * The brief summary;  Zones describe unique allocation types.  Zones are
45*d4a07e70Sfengbojiang  * organized into per-CPU caches which are filled by buckets.  Buckets are
46*d4a07e70Sfengbojiang  * organized according to memory domains.  Buckets are filled from kegs which
47*d4a07e70Sfengbojiang  * are also organized according to memory domains.  Kegs describe a unique
48*d4a07e70Sfengbojiang  * allocation type, backend memory provider, and layout.  Kegs are associated
49*d4a07e70Sfengbojiang  * with one or more zones and zones reference one or more kegs.  Kegs provide
50*d4a07e70Sfengbojiang  * slabs which are virtually contiguous collections of pages.  Each slab is
51*d4a07e70Sfengbojiang  * broken down int one or more items that will satisfy an individual allocation.
52*d4a07e70Sfengbojiang  *
53*d4a07e70Sfengbojiang  * Allocation is satisfied in the following order:
54*d4a07e70Sfengbojiang  * 1) Per-CPU cache
55*d4a07e70Sfengbojiang  * 2) Per-domain cache of buckets
56*d4a07e70Sfengbojiang  * 3) Slab from any of N kegs
57*d4a07e70Sfengbojiang  * 4) Backend page provider
58*d4a07e70Sfengbojiang  *
59*d4a07e70Sfengbojiang  * More detail on individual objects is contained below:
601eaf0ac3Slogwang  *
611eaf0ac3Slogwang  * Kegs contain lists of slabs which are stored in either the full bin, empty
621eaf0ac3Slogwang  * bin, or partially allocated bin, to reduce fragmentation.  They also contain
631eaf0ac3Slogwang  * the user supplied value for size, which is adjusted for alignment purposes
641eaf0ac3Slogwang  * and rsize is the result of that.  The Keg also stores information for
651eaf0ac3Slogwang  * managing a hash of page addresses that maps pages to uma_slab_t structures
661eaf0ac3Slogwang  * for pages that don't have embedded uma_slab_t's.
671eaf0ac3Slogwang  *
68*d4a07e70Sfengbojiang  * Keg slab lists are organized by memory domain to support NUMA allocation
69*d4a07e70Sfengbojiang  * policies.  By default allocations are spread across domains to reduce the
70*d4a07e70Sfengbojiang  * potential for hotspots.  Special keg creation flags may be specified to
71*d4a07e70Sfengbojiang  * prefer location allocation.  However there is no strict enforcement as frees
72*d4a07e70Sfengbojiang  * may happen on any CPU and these are returned to the CPU-local cache
73*d4a07e70Sfengbojiang  * regardless of the originating domain.
74*d4a07e70Sfengbojiang  *
751eaf0ac3Slogwang  * The uma_slab_t may be embedded in a UMA_SLAB_SIZE chunk of memory or it may
761eaf0ac3Slogwang  * be allocated off the page from a special slab zone.  The free list within a
771eaf0ac3Slogwang  * slab is managed with a bitmask.  For item sizes that would yield more than
781eaf0ac3Slogwang  * 10% memory waste we potentially allocate a separate uma_slab_t if this will
791eaf0ac3Slogwang  * improve the number of items per slab that will fit.
801eaf0ac3Slogwang  *
811eaf0ac3Slogwang  * The only really gross cases, with regards to memory waste, are for those
821eaf0ac3Slogwang  * items that are just over half the page size.   You can get nearly 50% waste,
831eaf0ac3Slogwang  * so you fall back to the memory footprint of the power of two allocator. I
841eaf0ac3Slogwang  * have looked at memory allocation sizes on many of the machines available to
851eaf0ac3Slogwang  * me, and there does not seem to be an abundance of allocations at this range
861eaf0ac3Slogwang  * so at this time it may not make sense to optimize for it.  This can, of
871eaf0ac3Slogwang  * course, be solved with dynamic slab sizes.
881eaf0ac3Slogwang  *
891eaf0ac3Slogwang  * Kegs may serve multiple Zones but by far most of the time they only serve
901eaf0ac3Slogwang  * one.  When a Zone is created, a Keg is allocated and setup for it.  While
911eaf0ac3Slogwang  * the backing Keg stores slabs, the Zone caches Buckets of items allocated
921eaf0ac3Slogwang  * from the slabs.  Each Zone is equipped with an init/fini and ctor/dtor
931eaf0ac3Slogwang  * pair, as well as with its own set of small per-CPU caches, layered above
941eaf0ac3Slogwang  * the Zone's general Bucket cache.
951eaf0ac3Slogwang  *
961eaf0ac3Slogwang  * The PCPU caches are protected by critical sections, and may be accessed
971eaf0ac3Slogwang  * safely only from their associated CPU, while the Zones backed by the same
981eaf0ac3Slogwang  * Keg all share a common Keg lock (to coalesce contention on the backing
991eaf0ac3Slogwang  * slabs).  The backing Keg typically only serves one Zone but in the case of
100*d4a07e70Sfengbojiang  * multiple Zones, one of the Zones is considered the Primary Zone and all
101*d4a07e70Sfengbojiang  * Zone-related stats from the Keg are done in the Primary Zone.  For an
1021eaf0ac3Slogwang  * example of a Multi-Zone setup, refer to the Mbuf allocation code.
1031eaf0ac3Slogwang  */
1041eaf0ac3Slogwang 
1051eaf0ac3Slogwang /*
1061eaf0ac3Slogwang  *	This is the representation for normal (Non OFFPAGE slab)
1071eaf0ac3Slogwang  *
1081eaf0ac3Slogwang  *	i == item
1091eaf0ac3Slogwang  *	s == slab pointer
1101eaf0ac3Slogwang  *
1111eaf0ac3Slogwang  *	<----------------  Page (UMA_SLAB_SIZE) ------------------>
1121eaf0ac3Slogwang  *	___________________________________________________________
1131eaf0ac3Slogwang  *     | _  _  _  _  _  _  _  _  _  _  _  _  _  _  _   ___________ |
1141eaf0ac3Slogwang  *     ||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i| |slab header||
1151eaf0ac3Slogwang  *     ||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_| |___________||
1161eaf0ac3Slogwang  *     |___________________________________________________________|
1171eaf0ac3Slogwang  *
1181eaf0ac3Slogwang  *
1191eaf0ac3Slogwang  *	This is an OFFPAGE slab. These can be larger than UMA_SLAB_SIZE.
1201eaf0ac3Slogwang  *
1211eaf0ac3Slogwang  *	___________________________________________________________
1221eaf0ac3Slogwang  *     | _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _   |
1231eaf0ac3Slogwang  *     ||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i|  |
1241eaf0ac3Slogwang  *     ||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_|  |
1251eaf0ac3Slogwang  *     |___________________________________________________________|
1261eaf0ac3Slogwang  *       ___________    ^
1271eaf0ac3Slogwang  *	|slab header|   |
1281eaf0ac3Slogwang  *	|___________|---*
1291eaf0ac3Slogwang  *
1301eaf0ac3Slogwang  */
1311eaf0ac3Slogwang 
1321eaf0ac3Slogwang #ifndef VM_UMA_INT_H
1331eaf0ac3Slogwang #define VM_UMA_INT_H
1341eaf0ac3Slogwang 
1351eaf0ac3Slogwang #define UMA_SLAB_SIZE	PAGE_SIZE	/* How big are our slabs? */
1361eaf0ac3Slogwang #define UMA_SLAB_MASK	(PAGE_SIZE - 1)	/* Mask to get back to the page */
1371eaf0ac3Slogwang #define UMA_SLAB_SHIFT	PAGE_SHIFT	/* Number of bits PAGE_MASK */
1381eaf0ac3Slogwang 
1391eaf0ac3Slogwang /* Max waste percentage before going to off page slab management */
1401eaf0ac3Slogwang #define UMA_MAX_WASTE	10
1411eaf0ac3Slogwang 
142*d4a07e70Sfengbojiang /* Max size of a CACHESPREAD slab. */
143*d4a07e70Sfengbojiang #define	UMA_CACHESPREAD_MAX_SIZE	(128 * 1024)
1441eaf0ac3Slogwang 
1451eaf0ac3Slogwang /*
146*d4a07e70Sfengbojiang  * These flags must not overlap with the UMA_ZONE flags specified in uma.h.
1471eaf0ac3Slogwang  */
148*d4a07e70Sfengbojiang #define	UMA_ZFLAG_OFFPAGE	0x00200000	/*
149*d4a07e70Sfengbojiang 						 * Force the slab structure
150*d4a07e70Sfengbojiang 						 * allocation off of the real
151*d4a07e70Sfengbojiang 						 * memory.
152*d4a07e70Sfengbojiang 						 */
153*d4a07e70Sfengbojiang #define	UMA_ZFLAG_HASH		0x00400000	/*
154*d4a07e70Sfengbojiang 						 * Use a hash table instead of
155*d4a07e70Sfengbojiang 						 * caching information in the
156*d4a07e70Sfengbojiang 						 * vm_page.
157*d4a07e70Sfengbojiang 						 */
158*d4a07e70Sfengbojiang #define	UMA_ZFLAG_VTOSLAB	0x00800000	/*
159*d4a07e70Sfengbojiang 						 * Zone uses vtoslab for
160*d4a07e70Sfengbojiang 						 * lookup.
161*d4a07e70Sfengbojiang 						 */
162*d4a07e70Sfengbojiang #define	UMA_ZFLAG_CTORDTOR	0x01000000	/* Zone has ctor/dtor set. */
163*d4a07e70Sfengbojiang #define	UMA_ZFLAG_LIMIT		0x02000000	/* Zone has limit set. */
164*d4a07e70Sfengbojiang #define	UMA_ZFLAG_CACHE		0x04000000	/* uma_zcache_create()d it */
165*d4a07e70Sfengbojiang #define	UMA_ZFLAG_RECLAIMING	0x08000000	/* Running zone_reclaim(). */
166*d4a07e70Sfengbojiang #define	UMA_ZFLAG_BUCKET	0x10000000	/* Bucket zone. */
167*d4a07e70Sfengbojiang #define	UMA_ZFLAG_INTERNAL	0x20000000	/* No offpage no PCPU. */
168*d4a07e70Sfengbojiang #define	UMA_ZFLAG_TRASH		0x40000000	/* Add trash ctor/dtor. */
169*d4a07e70Sfengbojiang 
170*d4a07e70Sfengbojiang #define	UMA_ZFLAG_INHERIT						\
171*d4a07e70Sfengbojiang     (UMA_ZFLAG_OFFPAGE | UMA_ZFLAG_HASH | UMA_ZFLAG_VTOSLAB |		\
172*d4a07e70Sfengbojiang      UMA_ZFLAG_BUCKET | UMA_ZFLAG_INTERNAL)
173*d4a07e70Sfengbojiang 
174*d4a07e70Sfengbojiang #define	PRINT_UMA_ZFLAGS	"\20"	\
175*d4a07e70Sfengbojiang     "\37TRASH"				\
176*d4a07e70Sfengbojiang     "\36INTERNAL"			\
177*d4a07e70Sfengbojiang     "\35BUCKET"				\
178*d4a07e70Sfengbojiang     "\34RECLAIMING"			\
179*d4a07e70Sfengbojiang     "\33CACHE"				\
180*d4a07e70Sfengbojiang     "\32LIMIT"				\
181*d4a07e70Sfengbojiang     "\31CTORDTOR"			\
182*d4a07e70Sfengbojiang     "\30VTOSLAB"			\
183*d4a07e70Sfengbojiang     "\27HASH"				\
184*d4a07e70Sfengbojiang     "\26OFFPAGE"			\
185*d4a07e70Sfengbojiang     "\23SMR"				\
186*d4a07e70Sfengbojiang     "\22ROUNDROBIN"			\
187*d4a07e70Sfengbojiang     "\21FIRSTTOUCH"			\
188*d4a07e70Sfengbojiang     "\20PCPU"				\
189*d4a07e70Sfengbojiang     "\17NODUMP"				\
190*d4a07e70Sfengbojiang     "\16CACHESPREAD"			\
191*d4a07e70Sfengbojiang     "\14MAXBUCKET"			\
192*d4a07e70Sfengbojiang     "\13NOBUCKET"			\
193*d4a07e70Sfengbojiang     "\12SECONDARY"			\
194*d4a07e70Sfengbojiang     "\11NOTPAGE"			\
195*d4a07e70Sfengbojiang     "\10VM"				\
196*d4a07e70Sfengbojiang     "\7MTXCLASS"			\
197*d4a07e70Sfengbojiang     "\6NOFREE"				\
198*d4a07e70Sfengbojiang     "\5MALLOC"				\
199*d4a07e70Sfengbojiang     "\4NOTOUCH"				\
200*d4a07e70Sfengbojiang     "\3CONTIG"				\
201*d4a07e70Sfengbojiang     "\2ZINIT"
202*d4a07e70Sfengbojiang 
203*d4a07e70Sfengbojiang /*
204*d4a07e70Sfengbojiang  * Hash table for freed address -> slab translation.
205*d4a07e70Sfengbojiang  *
206*d4a07e70Sfengbojiang  * Only zones with memory not touchable by the allocator use the
207*d4a07e70Sfengbojiang  * hash table.  Otherwise slabs are found with vtoslab().
208*d4a07e70Sfengbojiang  */
209*d4a07e70Sfengbojiang #define UMA_HASH_SIZE_INIT	32
2101eaf0ac3Slogwang 
2111eaf0ac3Slogwang #define UMA_HASH(h, s) ((((uintptr_t)s) >> UMA_SLAB_SHIFT) & (h)->uh_hashmask)
2121eaf0ac3Slogwang 
2131eaf0ac3Slogwang #define UMA_HASH_INSERT(h, s, mem)					\
214*d4a07e70Sfengbojiang 	LIST_INSERT_HEAD(&(h)->uh_slab_hash[UMA_HASH((h),		\
215*d4a07e70Sfengbojiang 	    (mem))], slab_tohashslab(s), uhs_hlink)
2161eaf0ac3Slogwang 
217*d4a07e70Sfengbojiang #define UMA_HASH_REMOVE(h, s)						\
218*d4a07e70Sfengbojiang 	LIST_REMOVE(slab_tohashslab(s), uhs_hlink)
2191eaf0ac3Slogwang 
220*d4a07e70Sfengbojiang LIST_HEAD(slabhashhead, uma_hash_slab);
2211eaf0ac3Slogwang 
2221eaf0ac3Slogwang struct uma_hash {
223*d4a07e70Sfengbojiang 	struct slabhashhead	*uh_slab_hash;	/* Hash table for slabs */
224*d4a07e70Sfengbojiang 	u_int		uh_hashsize;	/* Current size of the hash table */
225*d4a07e70Sfengbojiang 	u_int		uh_hashmask;	/* Mask used during hashing */
2261eaf0ac3Slogwang };
2271eaf0ac3Slogwang 
2281eaf0ac3Slogwang /*
229*d4a07e70Sfengbojiang  * Align field or structure to cache 'sector' in intel terminology.  This
230*d4a07e70Sfengbojiang  * is more efficient with adjacent line prefetch.
2311eaf0ac3Slogwang  */
232*d4a07e70Sfengbojiang #define	CACHE_LINE_SHIFT	6
233*d4a07e70Sfengbojiang #define	CACHE_LINE_SIZE		(1 << CACHE_LINE_SHIFT)
234*d4a07e70Sfengbojiang 
235*d4a07e70Sfengbojiang #if defined(__amd64__) || defined(__powerpc64__)
236*d4a07e70Sfengbojiang #define UMA_SUPER_ALIGN	(CACHE_LINE_SIZE * 2)
237*d4a07e70Sfengbojiang #else
238*d4a07e70Sfengbojiang #define UMA_SUPER_ALIGN	CACHE_LINE_SIZE
239*d4a07e70Sfengbojiang #endif
240*d4a07e70Sfengbojiang 
241*d4a07e70Sfengbojiang #define UMA_ALIGN __attribute__((__aligned__(UMA_SUPER_ALIGN)))
2421eaf0ac3Slogwang 
2431eaf0ac3Slogwang /*
244*d4a07e70Sfengbojiang  * The uma_bucket structure is used to queue and manage buckets divorced
245*d4a07e70Sfengbojiang  * from per-cpu caches.  They are loaded into uma_cache_bucket structures
246*d4a07e70Sfengbojiang  * for use.
2471eaf0ac3Slogwang  */
2481eaf0ac3Slogwang struct uma_bucket {
249*d4a07e70Sfengbojiang 	STAILQ_ENTRY(uma_bucket)	ub_link; /* Link into the zone */
250*d4a07e70Sfengbojiang 	int16_t		ub_cnt;			/* Count of items in bucket. */
2511eaf0ac3Slogwang 	int16_t		ub_entries;		/* Max items. */
252*d4a07e70Sfengbojiang 	smr_seq_t	ub_seq;			/* SMR sequence number. */
2531eaf0ac3Slogwang 	void		*ub_bucket[];		/* actual allocation storage */
2541eaf0ac3Slogwang };
2551eaf0ac3Slogwang 
2561eaf0ac3Slogwang typedef struct uma_bucket * uma_bucket_t;
2571eaf0ac3Slogwang 
258*d4a07e70Sfengbojiang /*
259*d4a07e70Sfengbojiang  * The uma_cache_bucket structure is statically allocated on each per-cpu
260*d4a07e70Sfengbojiang  * cache.  Its use reduces branches and cache misses in the fast path.
261*d4a07e70Sfengbojiang  */
262*d4a07e70Sfengbojiang struct uma_cache_bucket {
263*d4a07e70Sfengbojiang 	uma_bucket_t	ucb_bucket;
264*d4a07e70Sfengbojiang 	int16_t		ucb_cnt;
265*d4a07e70Sfengbojiang 	int16_t		ucb_entries;
266*d4a07e70Sfengbojiang 	uint32_t	ucb_spare;
267*d4a07e70Sfengbojiang };
268*d4a07e70Sfengbojiang 
269*d4a07e70Sfengbojiang typedef struct uma_cache_bucket * uma_cache_bucket_t;
270*d4a07e70Sfengbojiang 
271*d4a07e70Sfengbojiang /*
272*d4a07e70Sfengbojiang  * The uma_cache structure is allocated for each cpu for every zone
273*d4a07e70Sfengbojiang  * type.  This optimizes synchronization out of the allocator fast path.
274*d4a07e70Sfengbojiang  */
2751eaf0ac3Slogwang struct uma_cache {
276*d4a07e70Sfengbojiang 	struct uma_cache_bucket	uc_freebucket;	/* Bucket we're freeing to */
277*d4a07e70Sfengbojiang 	struct uma_cache_bucket	uc_allocbucket;	/* Bucket to allocate from */
278*d4a07e70Sfengbojiang 	struct uma_cache_bucket	uc_crossbucket;	/* cross domain bucket */
2791eaf0ac3Slogwang 	uint64_t		uc_allocs;	/* Count of allocations */
2801eaf0ac3Slogwang 	uint64_t		uc_frees;	/* Count of frees */
2811eaf0ac3Slogwang } UMA_ALIGN;
2821eaf0ac3Slogwang 
2831eaf0ac3Slogwang typedef struct uma_cache * uma_cache_t;
2841eaf0ac3Slogwang 
285*d4a07e70Sfengbojiang LIST_HEAD(slabhead, uma_slab);
286*d4a07e70Sfengbojiang 
287*d4a07e70Sfengbojiang /*
288*d4a07e70Sfengbojiang  * The cache structure pads perfectly into 64 bytes so we use spare
289*d4a07e70Sfengbojiang  * bits from the embedded cache buckets to store information from the zone
290*d4a07e70Sfengbojiang  * and keep all fast-path allocations accessing a single per-cpu line.
291*d4a07e70Sfengbojiang  */
292*d4a07e70Sfengbojiang static inline void
cache_set_uz_flags(uma_cache_t cache,uint32_t flags)293*d4a07e70Sfengbojiang cache_set_uz_flags(uma_cache_t cache, uint32_t flags)
294*d4a07e70Sfengbojiang {
295*d4a07e70Sfengbojiang 
296*d4a07e70Sfengbojiang 	cache->uc_freebucket.ucb_spare = flags;
297*d4a07e70Sfengbojiang }
298*d4a07e70Sfengbojiang 
299*d4a07e70Sfengbojiang static inline void
cache_set_uz_size(uma_cache_t cache,uint32_t size)300*d4a07e70Sfengbojiang cache_set_uz_size(uma_cache_t cache, uint32_t size)
301*d4a07e70Sfengbojiang {
302*d4a07e70Sfengbojiang 
303*d4a07e70Sfengbojiang 	cache->uc_allocbucket.ucb_spare = size;
304*d4a07e70Sfengbojiang }
305*d4a07e70Sfengbojiang 
306*d4a07e70Sfengbojiang static inline uint32_t
cache_uz_flags(uma_cache_t cache)307*d4a07e70Sfengbojiang cache_uz_flags(uma_cache_t cache)
308*d4a07e70Sfengbojiang {
309*d4a07e70Sfengbojiang 
310*d4a07e70Sfengbojiang 	return (cache->uc_freebucket.ucb_spare);
311*d4a07e70Sfengbojiang }
312*d4a07e70Sfengbojiang 
313*d4a07e70Sfengbojiang static inline uint32_t
cache_uz_size(uma_cache_t cache)314*d4a07e70Sfengbojiang cache_uz_size(uma_cache_t cache)
315*d4a07e70Sfengbojiang {
316*d4a07e70Sfengbojiang 
317*d4a07e70Sfengbojiang 	return (cache->uc_allocbucket.ucb_spare);
318*d4a07e70Sfengbojiang }
319*d4a07e70Sfengbojiang 
320*d4a07e70Sfengbojiang /*
321*d4a07e70Sfengbojiang  * Per-domain slab lists.  Embedded in the kegs.
322*d4a07e70Sfengbojiang  */
323*d4a07e70Sfengbojiang struct uma_domain {
324*d4a07e70Sfengbojiang 	struct mtx_padalign ud_lock;	/* Lock for the domain lists. */
325*d4a07e70Sfengbojiang 	struct slabhead	ud_part_slab;	/* partially allocated slabs */
326*d4a07e70Sfengbojiang 	struct slabhead	ud_free_slab;	/* completely unallocated slabs */
327*d4a07e70Sfengbojiang 	struct slabhead ud_full_slab;	/* fully allocated slabs */
328*d4a07e70Sfengbojiang 	uint32_t	ud_pages;	/* Total page count */
329*d4a07e70Sfengbojiang 	uint32_t	ud_free_items;	/* Count of items free in all slabs */
330*d4a07e70Sfengbojiang 	uint32_t	ud_free_slabs;	/* Count of free slabs */
331*d4a07e70Sfengbojiang } __attribute__((__aligned__(CACHE_LINE_SIZE)));
332*d4a07e70Sfengbojiang 
333*d4a07e70Sfengbojiang typedef struct uma_domain * uma_domain_t;
334*d4a07e70Sfengbojiang 
3351eaf0ac3Slogwang /*
3361eaf0ac3Slogwang  * Keg management structure
3371eaf0ac3Slogwang  *
3381eaf0ac3Slogwang  * TODO: Optimize for cache line size
3391eaf0ac3Slogwang  *
3401eaf0ac3Slogwang  */
3411eaf0ac3Slogwang struct uma_keg {
3421eaf0ac3Slogwang 	struct uma_hash	uk_hash;
3431eaf0ac3Slogwang 	LIST_HEAD(,uma_zone)	uk_zones;	/* Keg's zones */
3441eaf0ac3Slogwang 
345*d4a07e70Sfengbojiang 	struct domainset_ref uk_dr;	/* Domain selection policy. */
3461eaf0ac3Slogwang 	uint32_t	uk_align;	/* Alignment mask */
3471eaf0ac3Slogwang 	uint32_t	uk_reserve;	/* Number of reserved items. */
3481eaf0ac3Slogwang 	uint32_t	uk_size;	/* Requested size of each item */
3491eaf0ac3Slogwang 	uint32_t	uk_rsize;	/* Real size of each item */
3501eaf0ac3Slogwang 
3511eaf0ac3Slogwang 	uma_init	uk_init;	/* Keg's init routine */
3521eaf0ac3Slogwang 	uma_fini	uk_fini;	/* Keg's fini routine */
3531eaf0ac3Slogwang 	uma_alloc	uk_allocf;	/* Allocation function */
3541eaf0ac3Slogwang 	uma_free	uk_freef;	/* Free routine */
3551eaf0ac3Slogwang 
3561eaf0ac3Slogwang 	u_long		uk_offset;	/* Next free offset from base KVA */
3571eaf0ac3Slogwang 	vm_offset_t	uk_kva;		/* Zone base KVA */
3581eaf0ac3Slogwang 
359*d4a07e70Sfengbojiang 	uint32_t	uk_pgoff;	/* Offset to uma_slab struct */
3601eaf0ac3Slogwang 	uint16_t	uk_ppera;	/* pages per allocation from backend */
3611eaf0ac3Slogwang 	uint16_t	uk_ipers;	/* Items per slab */
3621eaf0ac3Slogwang 	uint32_t	uk_flags;	/* Internal flags */
3631eaf0ac3Slogwang 
3641eaf0ac3Slogwang 	/* Least used fields go to the last cache line. */
3651eaf0ac3Slogwang 	const char	*uk_name;		/* Name of creating zone. */
3661eaf0ac3Slogwang 	LIST_ENTRY(uma_keg)	uk_link;	/* List of all kegs */
367*d4a07e70Sfengbojiang 
368*d4a07e70Sfengbojiang 	/* Must be last, variable sized. */
369*d4a07e70Sfengbojiang 	struct uma_domain	uk_domain[];	/* Keg's slab lists. */
3701eaf0ac3Slogwang };
3711eaf0ac3Slogwang typedef struct uma_keg	* uma_keg_t;
3721eaf0ac3Slogwang 
3731eaf0ac3Slogwang /*
3741eaf0ac3Slogwang  * Free bits per-slab.
3751eaf0ac3Slogwang  */
376*d4a07e70Sfengbojiang #define	SLAB_MAX_SETSIZE	(PAGE_SIZE / UMA_SMALLEST_UNIT)
377*d4a07e70Sfengbojiang #define	SLAB_MIN_SETSIZE	_BITSET_BITS
378*d4a07e70Sfengbojiang BITSET_DEFINE(noslabbits, 0);
3791eaf0ac3Slogwang 
3801eaf0ac3Slogwang /*
3811eaf0ac3Slogwang  * The slab structure manages a single contiguous allocation from backing
3821eaf0ac3Slogwang  * store and subdivides it into individually allocatable items.
3831eaf0ac3Slogwang  */
3841eaf0ac3Slogwang struct uma_slab {
385*d4a07e70Sfengbojiang 	LIST_ENTRY(uma_slab)	us_link;	/* slabs in zone */
3861eaf0ac3Slogwang 	uint16_t	us_freecount;		/* How many are free? */
3871eaf0ac3Slogwang 	uint8_t		us_flags;		/* Page flags see uma.h */
388*d4a07e70Sfengbojiang 	uint8_t		us_domain;		/* Backing NUMA domain. */
389*d4a07e70Sfengbojiang 	struct noslabbits us_free;		/* Free bitmask, flexible. */
3901eaf0ac3Slogwang };
3911eaf0ac3Slogwang 
3921eaf0ac3Slogwang typedef struct uma_slab * uma_slab_t;
3931eaf0ac3Slogwang 
3941eaf0ac3Slogwang /*
395*d4a07e70Sfengbojiang  * Slab structure with a full sized bitset and hash link for both
396*d4a07e70Sfengbojiang  * HASH and OFFPAGE zones.
397*d4a07e70Sfengbojiang  */
398*d4a07e70Sfengbojiang struct uma_hash_slab {
399*d4a07e70Sfengbojiang 	LIST_ENTRY(uma_hash_slab) uhs_hlink;	/* Link for hash table */
400*d4a07e70Sfengbojiang 	uint8_t			*uhs_data;	/* First item */
401*d4a07e70Sfengbojiang 	struct uma_slab		uhs_slab;	/* Must be last. */
402*d4a07e70Sfengbojiang };
403*d4a07e70Sfengbojiang 
404*d4a07e70Sfengbojiang typedef struct uma_hash_slab * uma_hash_slab_t;
405*d4a07e70Sfengbojiang 
406*d4a07e70Sfengbojiang static inline uma_hash_slab_t
slab_tohashslab(uma_slab_t slab)407*d4a07e70Sfengbojiang slab_tohashslab(uma_slab_t slab)
408*d4a07e70Sfengbojiang {
409*d4a07e70Sfengbojiang 
410*d4a07e70Sfengbojiang 	return NULL;
411*d4a07e70Sfengbojiang }
412*d4a07e70Sfengbojiang 
413*d4a07e70Sfengbojiang static inline void *
slab_data(uma_slab_t slab,uma_keg_t keg)414*d4a07e70Sfengbojiang slab_data(uma_slab_t slab, uma_keg_t keg)
415*d4a07e70Sfengbojiang {
416*d4a07e70Sfengbojiang 
417*d4a07e70Sfengbojiang 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) == 0)
418*d4a07e70Sfengbojiang 		return ((void *)((uintptr_t)slab - keg->uk_pgoff));
419*d4a07e70Sfengbojiang 	else
420*d4a07e70Sfengbojiang 		return (slab_tohashslab(slab)->uhs_data);
421*d4a07e70Sfengbojiang }
422*d4a07e70Sfengbojiang 
423*d4a07e70Sfengbojiang static inline void *
slab_item(uma_slab_t slab,uma_keg_t keg,int index)424*d4a07e70Sfengbojiang slab_item(uma_slab_t slab, uma_keg_t keg, int index)
425*d4a07e70Sfengbojiang {
426*d4a07e70Sfengbojiang 	uintptr_t data;
427*d4a07e70Sfengbojiang 
428*d4a07e70Sfengbojiang 	data = (uintptr_t)slab_data(slab, keg);
429*d4a07e70Sfengbojiang 	return ((void *)(data + keg->uk_rsize * index));
430*d4a07e70Sfengbojiang }
431*d4a07e70Sfengbojiang 
432*d4a07e70Sfengbojiang static inline int
slab_item_index(uma_slab_t slab,uma_keg_t keg,void * item)433*d4a07e70Sfengbojiang slab_item_index(uma_slab_t slab, uma_keg_t keg, void *item)
434*d4a07e70Sfengbojiang {
435*d4a07e70Sfengbojiang 	uintptr_t data;
436*d4a07e70Sfengbojiang 
437*d4a07e70Sfengbojiang 	data = (uintptr_t)slab_data(slab, keg);
438*d4a07e70Sfengbojiang 	return (((uintptr_t)item - data) / keg->uk_rsize);
439*d4a07e70Sfengbojiang }
440*d4a07e70Sfengbojiang 
441*d4a07e70Sfengbojiang STAILQ_HEAD(uma_bucketlist, uma_bucket);
442*d4a07e70Sfengbojiang 
443*d4a07e70Sfengbojiang struct uma_zone_domain {
444*d4a07e70Sfengbojiang 	struct uma_bucketlist uzd_buckets; /* full buckets */
445*d4a07e70Sfengbojiang 	uma_bucket_t	uzd_cross;	/* Fills from cross buckets. */
446*d4a07e70Sfengbojiang 	long		uzd_nitems;	/* total item count */
447*d4a07e70Sfengbojiang 	long		uzd_imax;	/* maximum item count this period */
448*d4a07e70Sfengbojiang 	long		uzd_imin;	/* minimum item count this period */
449*d4a07e70Sfengbojiang 	long		uzd_wss;	/* working set size estimate */
450*d4a07e70Sfengbojiang 	smr_seq_t	uzd_seq;	/* Lowest queued seq. */
451*d4a07e70Sfengbojiang 	struct mtx	uzd_lock;	/* Lock for the domain */
452*d4a07e70Sfengbojiang } __attribute__((__aligned__(CACHE_LINE_SIZE)));
453*d4a07e70Sfengbojiang 
454*d4a07e70Sfengbojiang typedef struct uma_zone_domain * uma_zone_domain_t;
455*d4a07e70Sfengbojiang 
456*d4a07e70Sfengbojiang /*
457*d4a07e70Sfengbojiang  * Zone structure - per memory type.
4581eaf0ac3Slogwang  */
4591eaf0ac3Slogwang struct uma_zone {
460*d4a07e70Sfengbojiang 	/* Offset 0, used in alloc/free fast/medium fast path and const. */
461*d4a07e70Sfengbojiang 	uint32_t	uz_flags;	/* Flags inherited from kegs */
462*d4a07e70Sfengbojiang 	uint32_t	uz_size;	/* Size inherited from kegs */
4631eaf0ac3Slogwang 	uma_ctor	uz_ctor;	/* Constructor for each allocation */
4641eaf0ac3Slogwang 	uma_dtor	uz_dtor;	/* Destructor */
465*d4a07e70Sfengbojiang 	smr_t		uz_smr;		/* Safe memory reclaim context. */
466*d4a07e70Sfengbojiang 	uint64_t	uz_max_items;	/* Maximum number of items to alloc */
467*d4a07e70Sfengbojiang 	uint64_t	uz_bucket_max;	/* Maximum bucket cache size */
468*d4a07e70Sfengbojiang 	uint16_t	uz_bucket_size;	/* Number of items in full bucket */
469*d4a07e70Sfengbojiang 	uint16_t	uz_bucket_size_max; /* Maximum number of bucket items */
470*d4a07e70Sfengbojiang 	uint32_t	uz_sleepers;	/* Threads sleeping on limit */
471*d4a07e70Sfengbojiang 	counter_u64_t	uz_xdomain;	/* Total number of cross-domain frees */
472*d4a07e70Sfengbojiang 
473*d4a07e70Sfengbojiang 	/* Offset 64, used in bucket replenish. */
474*d4a07e70Sfengbojiang 	uma_keg_t	uz_keg;		/* This zone's keg if !CACHE */
4751eaf0ac3Slogwang 	uma_import	uz_import;	/* Import new memory to cache. */
4761eaf0ac3Slogwang 	uma_release	uz_release;	/* Release memory from cache. */
4771eaf0ac3Slogwang 	void		*uz_arg;	/* Import/release argument. */
478*d4a07e70Sfengbojiang 	uma_init	uz_init;	/* Initializer for each item */
479*d4a07e70Sfengbojiang 	uma_fini	uz_fini;	/* Finalizer for each item. */
480*d4a07e70Sfengbojiang 	volatile uint64_t uz_items;	/* Total items count & sleepers */
4811eaf0ac3Slogwang 	uint64_t	uz_sleeps;	/* Total number of alloc sleeps */
4821eaf0ac3Slogwang 
483*d4a07e70Sfengbojiang 	/* Offset 128 Rare stats, misc read-only. */
484*d4a07e70Sfengbojiang 	LIST_ENTRY(uma_zone) uz_link;	/* List of all zones in keg */
485*d4a07e70Sfengbojiang 	counter_u64_t	uz_allocs;	/* Total number of allocations */
486*d4a07e70Sfengbojiang 	counter_u64_t	uz_frees;	/* Total number of frees */
487*d4a07e70Sfengbojiang 	counter_u64_t	uz_fails;	/* Total number of alloc failures */
488*d4a07e70Sfengbojiang 	const char	*uz_name;	/* Text name of the zone */
489*d4a07e70Sfengbojiang 	char		*uz_ctlname;	/* sysctl safe name string. */
490*d4a07e70Sfengbojiang 	int		uz_namecnt;	/* duplicate name count. */
491*d4a07e70Sfengbojiang 	uint16_t	uz_bucket_size_min; /* Min number of items in bucket */
492*d4a07e70Sfengbojiang 	uint16_t	uz_pad0;
493*d4a07e70Sfengbojiang 
494*d4a07e70Sfengbojiang 	/* Offset 192, rare read-only. */
495*d4a07e70Sfengbojiang 	struct sysctl_oid *uz_oid;	/* sysctl oid pointer. */
4961eaf0ac3Slogwang 	const char	*uz_warning;	/* Warning to print on failure */
4971eaf0ac3Slogwang 	struct timeval	uz_ratecheck;	/* Warnings rate-limiting */
4981eaf0ac3Slogwang 	struct task	uz_maxaction;	/* Task to run when at limit */
4991eaf0ac3Slogwang 
500*d4a07e70Sfengbojiang 	/* Offset 256. */
501*d4a07e70Sfengbojiang 	struct mtx	uz_cross_lock;	/* Cross domain free lock */
502*d4a07e70Sfengbojiang 
5031eaf0ac3Slogwang 	/*
5041eaf0ac3Slogwang 	 * This HAS to be the last item because we adjust the zone size
5051eaf0ac3Slogwang 	 * based on NCPU and then allocate the space for the zones.
5061eaf0ac3Slogwang 	 */
507*d4a07e70Sfengbojiang 	struct uma_cache	uz_cpu[]; /* Per cpu caches */
508*d4a07e70Sfengbojiang 
509*d4a07e70Sfengbojiang 	/* domains follow here. */
5101eaf0ac3Slogwang };
5111eaf0ac3Slogwang 
5121eaf0ac3Slogwang /*
513*d4a07e70Sfengbojiang  * Macros for interpreting the uz_items field.  20 bits of sleeper count
514*d4a07e70Sfengbojiang  * and 44 bit of item count.
5151eaf0ac3Slogwang  */
516*d4a07e70Sfengbojiang #define	UZ_ITEMS_SLEEPER_SHIFT	44LL
517*d4a07e70Sfengbojiang #define	UZ_ITEMS_SLEEPERS_MAX	((1 << (64 - UZ_ITEMS_SLEEPER_SHIFT)) - 1)
518*d4a07e70Sfengbojiang #define	UZ_ITEMS_COUNT_MASK	((1LL << UZ_ITEMS_SLEEPER_SHIFT) - 1)
519*d4a07e70Sfengbojiang #define	UZ_ITEMS_COUNT(x)	((x) & UZ_ITEMS_COUNT_MASK)
520*d4a07e70Sfengbojiang #define	UZ_ITEMS_SLEEPERS(x)	((x) >> UZ_ITEMS_SLEEPER_SHIFT)
521*d4a07e70Sfengbojiang #define	UZ_ITEMS_SLEEPER	(1LL << UZ_ITEMS_SLEEPER_SHIFT)
5221eaf0ac3Slogwang 
523*d4a07e70Sfengbojiang #define	ZONE_ASSERT_COLD(z)						\
524*d4a07e70Sfengbojiang 	KASSERT(uma_zone_get_allocs((z)) == 0,				\
525*d4a07e70Sfengbojiang 	    ("zone %s initialization after use.", (z)->uz_name))
5261eaf0ac3Slogwang 
527*d4a07e70Sfengbojiang /* Domains are contiguous after the last CPU */
528*d4a07e70Sfengbojiang #define	ZDOM_GET(z, n)							\
529*d4a07e70Sfengbojiang 	(&((uma_zone_domain_t)&(z)->uz_cpu[mp_maxid + 1])[n])
5301eaf0ac3Slogwang 
5311eaf0ac3Slogwang #undef	UMA_ALIGN
5321eaf0ac3Slogwang 
533*d4a07e70Sfengbojiang #ifdef _KERNEL
534*d4a07e70Sfengbojiang /* Internal prototypes */
535*d4a07e70Sfengbojiang static __inline uma_slab_t hash_sfind(struct uma_hash *hash, uint8_t *data);
536*d4a07e70Sfengbojiang 
537*d4a07e70Sfengbojiang /* Lock Macros */
538*d4a07e70Sfengbojiang 
539*d4a07e70Sfengbojiang #define	KEG_LOCKPTR(k, d)	(struct mtx *)&(k)->uk_domain[(d)].ud_lock
540*d4a07e70Sfengbojiang #define	KEG_LOCK_INIT(k, d, lc)						\
541*d4a07e70Sfengbojiang 	do {								\
542*d4a07e70Sfengbojiang 		if ((lc))						\
543*d4a07e70Sfengbojiang 			mtx_init(KEG_LOCKPTR(k, d), (k)->uk_name,	\
544*d4a07e70Sfengbojiang 			    (k)->uk_name, MTX_DEF | MTX_DUPOK);		\
545*d4a07e70Sfengbojiang 		else							\
546*d4a07e70Sfengbojiang 			mtx_init(KEG_LOCKPTR(k, d), (k)->uk_name,	\
547*d4a07e70Sfengbojiang 			    "UMA zone", MTX_DEF | MTX_DUPOK);		\
548*d4a07e70Sfengbojiang 	} while (0)
549*d4a07e70Sfengbojiang 
550*d4a07e70Sfengbojiang #define	KEG_LOCK_FINI(k, d)	mtx_destroy(KEG_LOCKPTR(k, d))
551*d4a07e70Sfengbojiang #define	KEG_LOCK(k, d)							\
552*d4a07e70Sfengbojiang 	({ mtx_lock(KEG_LOCKPTR(k, d)); KEG_LOCKPTR(k, d); })
553*d4a07e70Sfengbojiang #define	KEG_UNLOCK(k, d)	mtx_unlock(KEG_LOCKPTR(k, d))
554*d4a07e70Sfengbojiang #define	KEG_LOCK_ASSERT(k, d)	mtx_assert(KEG_LOCKPTR(k, d), MA_OWNED)
555*d4a07e70Sfengbojiang 
556*d4a07e70Sfengbojiang #define	KEG_GET(zone, keg) do {					\
557*d4a07e70Sfengbojiang 	(keg) = (zone)->uz_keg;					\
558*d4a07e70Sfengbojiang 	KASSERT((void *)(keg) != NULL,				\
559*d4a07e70Sfengbojiang 	    ("%s: Invalid zone %p type", __func__, (zone)));	\
560*d4a07e70Sfengbojiang 	} while (0)
561*d4a07e70Sfengbojiang 
562*d4a07e70Sfengbojiang #define	KEG_ASSERT_COLD(k)						\
563*d4a07e70Sfengbojiang 	KASSERT(uma_keg_get_allocs((k)) == 0,				\
564*d4a07e70Sfengbojiang 	    ("keg %s initialization after use.", (k)->uk_name))
565*d4a07e70Sfengbojiang 
566*d4a07e70Sfengbojiang #define	ZDOM_LOCK_INIT(z, zdom, lc)					\
567*d4a07e70Sfengbojiang 	do {								\
568*d4a07e70Sfengbojiang 		if ((lc))						\
569*d4a07e70Sfengbojiang 			mtx_init(&(zdom)->uzd_lock, (z)->uz_name,	\
570*d4a07e70Sfengbojiang 			    (z)->uz_name, MTX_DEF | MTX_DUPOK);		\
571*d4a07e70Sfengbojiang 		else							\
572*d4a07e70Sfengbojiang 			mtx_init(&(zdom)->uzd_lock, (z)->uz_name,	\
573*d4a07e70Sfengbojiang 			    "UMA zone", MTX_DEF | MTX_DUPOK);		\
574*d4a07e70Sfengbojiang 	} while (0)
575*d4a07e70Sfengbojiang #define	ZDOM_LOCK_FINI(z)	mtx_destroy(&(z)->uzd_lock)
576*d4a07e70Sfengbojiang #define	ZDOM_LOCK_ASSERT(z)	mtx_assert(&(z)->uzd_lock, MA_OWNED)
577*d4a07e70Sfengbojiang 
578*d4a07e70Sfengbojiang #define	ZDOM_LOCK(z)	mtx_lock(&(z)->uzd_lock)
579*d4a07e70Sfengbojiang #define	ZDOM_OWNED(z)	(mtx_owner(&(z)->uzd_lock) != NULL)
580*d4a07e70Sfengbojiang #define	ZDOM_UNLOCK(z)	mtx_unlock(&(z)->uzd_lock)
581*d4a07e70Sfengbojiang 
582*d4a07e70Sfengbojiang #define	ZONE_LOCK(z)	ZDOM_LOCK(ZDOM_GET((z), 0))
583*d4a07e70Sfengbojiang #define	ZONE_UNLOCK(z)	ZDOM_UNLOCK(ZDOM_GET((z), 0))
584*d4a07e70Sfengbojiang 
585*d4a07e70Sfengbojiang #define	ZONE_CROSS_LOCK_INIT(z)					\
586*d4a07e70Sfengbojiang 	mtx_init(&(z)->uz_cross_lock, "UMA Cross", NULL, MTX_DEF)
587*d4a07e70Sfengbojiang #define	ZONE_CROSS_LOCK(z)	mtx_lock(&(z)->uz_cross_lock)
588*d4a07e70Sfengbojiang #define	ZONE_CROSS_UNLOCK(z)	mtx_unlock(&(z)->uz_cross_lock)
589*d4a07e70Sfengbojiang #define	ZONE_CROSS_LOCK_FINI(z)	mtx_destroy(&(z)->uz_cross_lock)
590*d4a07e70Sfengbojiang 
591*d4a07e70Sfengbojiang /*
592*d4a07e70Sfengbojiang  * Find a slab within a hash table.  This is used for OFFPAGE zones to lookup
593*d4a07e70Sfengbojiang  * the slab structure.
594*d4a07e70Sfengbojiang  *
595*d4a07e70Sfengbojiang  * Arguments:
596*d4a07e70Sfengbojiang  *	hash  The hash table to search.
597*d4a07e70Sfengbojiang  *	data  The base page of the item.
598*d4a07e70Sfengbojiang  *
599*d4a07e70Sfengbojiang  * Returns:
600*d4a07e70Sfengbojiang  *	A pointer to a slab if successful, else NULL.
601*d4a07e70Sfengbojiang  */
602*d4a07e70Sfengbojiang static __inline uma_slab_t
hash_sfind(struct uma_hash * hash,uint8_t * data)603*d4a07e70Sfengbojiang hash_sfind(struct uma_hash *hash, uint8_t *data)
604*d4a07e70Sfengbojiang {
605*d4a07e70Sfengbojiang         uma_hash_slab_t slab;
606*d4a07e70Sfengbojiang         u_int hval;
607*d4a07e70Sfengbojiang 
608*d4a07e70Sfengbojiang         hval = UMA_HASH(hash, data);
609*d4a07e70Sfengbojiang 
610*d4a07e70Sfengbojiang         LIST_FOREACH(slab, &hash->uh_slab_hash[hval], uhs_hlink) {
611*d4a07e70Sfengbojiang                 if ((uint8_t *)slab->uhs_data == data)
612*d4a07e70Sfengbojiang                         return (&slab->uhs_slab);
613*d4a07e70Sfengbojiang         }
614*d4a07e70Sfengbojiang         return (NULL);
615*d4a07e70Sfengbojiang }
616*d4a07e70Sfengbojiang 
617*d4a07e70Sfengbojiang static __inline uma_slab_t
vtoslab(vm_offset_t va)618*d4a07e70Sfengbojiang vtoslab(vm_offset_t va)
619*d4a07e70Sfengbojiang {
620*d4a07e70Sfengbojiang 	vm_page_t p;
621*d4a07e70Sfengbojiang 
622*d4a07e70Sfengbojiang 	p = PHYS_TO_VM_PAGE(pmap_kextract(va));
623*d4a07e70Sfengbojiang 	return (p->plinks.uma.slab);
624*d4a07e70Sfengbojiang }
625*d4a07e70Sfengbojiang 
626*d4a07e70Sfengbojiang static __inline void
vtozoneslab(vm_offset_t va,uma_zone_t * zone,uma_slab_t * slab)627*d4a07e70Sfengbojiang vtozoneslab(vm_offset_t va, uma_zone_t *zone, uma_slab_t *slab)
628*d4a07e70Sfengbojiang {
629*d4a07e70Sfengbojiang 	vm_page_t p;
630*d4a07e70Sfengbojiang 
631*d4a07e70Sfengbojiang 	p = PHYS_TO_VM_PAGE(pmap_kextract(va));
632*d4a07e70Sfengbojiang 	*slab = p->plinks.uma.slab;
633*d4a07e70Sfengbojiang 	*zone = p->plinks.uma.zone;
634*d4a07e70Sfengbojiang }
635*d4a07e70Sfengbojiang 
636*d4a07e70Sfengbojiang static __inline void
vsetzoneslab(vm_offset_t va,uma_zone_t zone,uma_slab_t slab)637*d4a07e70Sfengbojiang vsetzoneslab(vm_offset_t va, uma_zone_t zone, uma_slab_t slab)
638*d4a07e70Sfengbojiang {
639*d4a07e70Sfengbojiang 	vm_page_t p;
640*d4a07e70Sfengbojiang 
641*d4a07e70Sfengbojiang 	p = PHYS_TO_VM_PAGE(pmap_kextract(va));
642*d4a07e70Sfengbojiang 	p->plinks.uma.slab = slab;
643*d4a07e70Sfengbojiang 	p->plinks.uma.zone = zone;
644*d4a07e70Sfengbojiang }
645*d4a07e70Sfengbojiang 
646*d4a07e70Sfengbojiang extern unsigned long uma_kmem_limit;
647*d4a07e70Sfengbojiang extern unsigned long uma_kmem_total;
648*d4a07e70Sfengbojiang 
649*d4a07e70Sfengbojiang /* Adjust bytes under management by UMA. */
650*d4a07e70Sfengbojiang static inline void
uma_total_dec(unsigned long size)651*d4a07e70Sfengbojiang uma_total_dec(unsigned long size)
652*d4a07e70Sfengbojiang {
653*d4a07e70Sfengbojiang 
654*d4a07e70Sfengbojiang 	atomic_subtract_long(&uma_kmem_total, size);
655*d4a07e70Sfengbojiang }
656*d4a07e70Sfengbojiang 
657*d4a07e70Sfengbojiang static inline void
uma_total_inc(unsigned long size)658*d4a07e70Sfengbojiang uma_total_inc(unsigned long size)
659*d4a07e70Sfengbojiang {
660*d4a07e70Sfengbojiang 
661*d4a07e70Sfengbojiang 	if (atomic_fetchadd_long(&uma_kmem_total, size) > uma_kmem_limit)
662*d4a07e70Sfengbojiang 		uma_reclaim_wakeup();
663*d4a07e70Sfengbojiang }
664*d4a07e70Sfengbojiang 
665*d4a07e70Sfengbojiang /*
666*d4a07e70Sfengbojiang  * The following two functions may be defined by architecture specific code
667*d4a07e70Sfengbojiang  * if they can provide more efficient allocation functions.  This is useful
668*d4a07e70Sfengbojiang  * for using direct mapped addresses.
669*d4a07e70Sfengbojiang  */
670*d4a07e70Sfengbojiang void *uma_small_alloc(uma_zone_t zone, vm_size_t bytes, int domain,
671*d4a07e70Sfengbojiang     uint8_t *pflag, int wait);
672*d4a07e70Sfengbojiang void uma_small_free(void *mem, vm_size_t size, uint8_t flags);
673*d4a07e70Sfengbojiang 
674*d4a07e70Sfengbojiang /* Set a global soft limit on UMA managed memory. */
675*d4a07e70Sfengbojiang void uma_set_limit(unsigned long limit);
676*d4a07e70Sfengbojiang 
677*d4a07e70Sfengbojiang #endif /* _KERNEL */
6781eaf0ac3Slogwang 
6791eaf0ac3Slogwang #endif /* VM_UMA_INT_H */
680