xref: /freebsd/sys/vm/uma_core.c (revision 543117bed857cfb85e3fa4d9b16df7bcba55cc52)
160727d8bSWarner Losh /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
6ae4e9636SRobert Watson  * Copyright (c) 2004-2006 Robert N. M. Watson
708ecce74SRobert Watson  * All rights reserved.
88355f576SJeff Roberson  *
98355f576SJeff Roberson  * Redistribution and use in source and binary forms, with or without
108355f576SJeff Roberson  * modification, are permitted provided that the following conditions
118355f576SJeff Roberson  * are met:
128355f576SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
138355f576SJeff Roberson  *    notice unmodified, this list of conditions, and the following
148355f576SJeff Roberson  *    disclaimer.
158355f576SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
168355f576SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
178355f576SJeff Roberson  *    documentation and/or other materials provided with the distribution.
188355f576SJeff Roberson  *
198355f576SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
208355f576SJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
218355f576SJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
228355f576SJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
238355f576SJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
248355f576SJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
258355f576SJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
268355f576SJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
278355f576SJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
288355f576SJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
298355f576SJeff Roberson  */
308355f576SJeff Roberson 
318355f576SJeff Roberson /*
328355f576SJeff Roberson  * uma_core.c  Implementation of the Universal Memory allocator
338355f576SJeff Roberson  *
348355f576SJeff Roberson  * This allocator is intended to replace the multitude of similar object caches
358355f576SJeff Roberson  * in the standard FreeBSD kernel.  The intent is to be flexible as well as
36763df3ecSPedro F. Giffuni  * efficient.  A primary design goal is to return unused memory to the rest of
378355f576SJeff Roberson  * the system.  This will make the system as a whole more flexible due to the
388355f576SJeff Roberson  * ability to move memory to subsystems which most need it instead of leaving
398355f576SJeff Roberson  * pools of reserved memory unused.
408355f576SJeff Roberson  *
418355f576SJeff Roberson  * The basic ideas stem from similar slab/zone based allocators whose algorithms
428355f576SJeff Roberson  * are well known.
438355f576SJeff Roberson  *
448355f576SJeff Roberson  */
458355f576SJeff Roberson 
468355f576SJeff Roberson /*
478355f576SJeff Roberson  * TODO:
488355f576SJeff Roberson  *	- Improve memory usage for large allocations
498355f576SJeff Roberson  *	- Investigate cache size adjustments
508355f576SJeff Roberson  */
518355f576SJeff Roberson 
52874651b1SDavid E. O'Brien #include <sys/cdefs.h>
53874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
54874651b1SDavid E. O'Brien 
5548c5777eSRobert Watson #include "opt_ddb.h"
568355f576SJeff Roberson #include "opt_param.h"
578d689e04SGleb Smirnoff #include "opt_vm.h"
5848c5777eSRobert Watson 
598355f576SJeff Roberson #include <sys/param.h>
608355f576SJeff Roberson #include <sys/systm.h>
61ef72505eSJeff Roberson #include <sys/bitset.h>
62194a979eSMark Johnston #include <sys/domainset.h>
639b43bc27SAndriy Gapon #include <sys/eventhandler.h>
648355f576SJeff Roberson #include <sys/kernel.h>
658355f576SJeff Roberson #include <sys/types.h>
66ad5b0f5bSJeff Roberson #include <sys/limits.h>
678355f576SJeff Roberson #include <sys/queue.h>
688355f576SJeff Roberson #include <sys/malloc.h>
693659f747SRobert Watson #include <sys/ktr.h>
708355f576SJeff Roberson #include <sys/lock.h>
718355f576SJeff Roberson #include <sys/sysctl.h>
728355f576SJeff Roberson #include <sys/mutex.h>
734c1cc01cSJohn Baldwin #include <sys/proc.h>
7410cb2424SMark Murray #include <sys/random.h>
7589f6b863SAttilio Rao #include <sys/rwlock.h>
767a52a97eSRobert Watson #include <sys/sbuf.h>
77a2de44abSAlexander Motin #include <sys/sched.h>
784bd61e19SJeff Roberson #include <sys/sleepqueue.h>
798355f576SJeff Roberson #include <sys/smp.h>
80d4665eaaSJeff Roberson #include <sys/smr.h>
81e60b2fcbSGleb Smirnoff #include <sys/taskqueue.h>
8286bbae32SJeff Roberson #include <sys/vmmeter.h>
8386bbae32SJeff Roberson 
848355f576SJeff Roberson #include <vm/vm.h>
85194a979eSMark Johnston #include <vm/vm_domainset.h>
868355f576SJeff Roberson #include <vm/vm_object.h>
878355f576SJeff Roberson #include <vm/vm_page.h>
88a4915c21SAttilio Rao #include <vm/vm_pageout.h>
898355f576SJeff Roberson #include <vm/vm_param.h>
90ab3185d1SJeff Roberson #include <vm/vm_phys.h>
9130c5525bSAndrew Gallatin #include <vm/vm_pagequeue.h>
928355f576SJeff Roberson #include <vm/vm_map.h>
938355f576SJeff Roberson #include <vm/vm_kern.h>
948355f576SJeff Roberson #include <vm/vm_extern.h>
958355f576SJeff Roberson #include <vm/uma.h>
968355f576SJeff Roberson #include <vm/uma_int.h>
97639c9550SJeff Roberson #include <vm/uma_dbg.h>
988355f576SJeff Roberson 
9948c5777eSRobert Watson #include <ddb/ddb.h>
10048c5777eSRobert Watson 
1018d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
1028d689e04SGleb Smirnoff #include <vm/memguard.h>
1038d689e04SGleb Smirnoff #endif
1048d689e04SGleb Smirnoff 
105a81c400eSJeff Roberson #include <machine/md_var.h>
106a81c400eSJeff Roberson 
107d4665eaaSJeff Roberson #ifdef INVARIANTS
108d4665eaaSJeff Roberson #define	UMA_ALWAYS_CTORDTOR	1
109d4665eaaSJeff Roberson #else
110d4665eaaSJeff Roberson #define	UMA_ALWAYS_CTORDTOR	0
111d4665eaaSJeff Roberson #endif
112d4665eaaSJeff Roberson 
1138355f576SJeff Roberson /*
114ab3185d1SJeff Roberson  * This is the zone and keg from which all zones are spawned.
1158355f576SJeff Roberson  */
116ab3185d1SJeff Roberson static uma_zone_t kegs;
117ab3185d1SJeff Roberson static uma_zone_t zones;
1188355f576SJeff Roberson 
1199b8db4d0SRyan Libby /*
1209b8db4d0SRyan Libby  * These are the two zones from which all offpage uma_slab_ts are allocated.
1219b8db4d0SRyan Libby  *
1229b8db4d0SRyan Libby  * One zone is for slab headers that can represent a larger number of items,
1239b8db4d0SRyan Libby  * making the slabs themselves more efficient, and the other zone is for
1249b8db4d0SRyan Libby  * headers that are smaller and represent fewer items, making the headers more
1259b8db4d0SRyan Libby  * efficient.
1269b8db4d0SRyan Libby  */
1279b8db4d0SRyan Libby #define	SLABZONE_SIZE(setsize)					\
1289b8db4d0SRyan Libby     (sizeof(struct uma_hash_slab) + BITSET_SIZE(setsize) * SLAB_BITSETS)
1299b8db4d0SRyan Libby #define	SLABZONE0_SETSIZE	(PAGE_SIZE / 16)
1309b8db4d0SRyan Libby #define	SLABZONE1_SETSIZE	SLAB_MAX_SETSIZE
1319b8db4d0SRyan Libby #define	SLABZONE0_SIZE	SLABZONE_SIZE(SLABZONE0_SETSIZE)
1329b8db4d0SRyan Libby #define	SLABZONE1_SIZE	SLABZONE_SIZE(SLABZONE1_SETSIZE)
1339b8db4d0SRyan Libby static uma_zone_t slabzones[2];
1348355f576SJeff Roberson 
1358355f576SJeff Roberson /*
1368355f576SJeff Roberson  * The initial hash tables come out of this zone so they can be allocated
1378355f576SJeff Roberson  * prior to malloc coming up.
1388355f576SJeff Roberson  */
1398355f576SJeff Roberson static uma_zone_t hashzone;
1408355f576SJeff Roberson 
1411e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */
142e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1;
1431e319f6dSRobert Watson 
144961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
14520a4e154SJeff Roberson static MALLOC_DEFINE(M_UMA, "UMA", "UMA Misc");
146961647dfSJeff Roberson 
1478355f576SJeff Roberson /*
14886bbae32SJeff Roberson  * Are we allowed to allocate buckets?
14986bbae32SJeff Roberson  */
15086bbae32SJeff Roberson static int bucketdisable = 1;
15186bbae32SJeff Roberson 
152099a0e58SBosko Milekic /* Linked list of all kegs in the system */
15313e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
1548355f576SJeff Roberson 
15503175483SAlexander Motin /* Linked list of all cache-only zones in the system */
15603175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones =
15703175483SAlexander Motin     LIST_HEAD_INITIALIZER(uma_cachezones);
15803175483SAlexander Motin 
159111fbcd5SBryan Venteicher /* This RW lock protects the keg list */
160fe933c1dSMateusz Guzik static struct rwlock_padalign __exclusive_cache_line uma_rwlock;
1618355f576SJeff Roberson 
162ac0a6fd0SGleb Smirnoff /*
163a81c400eSJeff Roberson  * First available virual address for boot time allocations.
164ac0a6fd0SGleb Smirnoff  */
165a81c400eSJeff Roberson static vm_offset_t bootstart;
166a81c400eSJeff Roberson static vm_offset_t bootmem;
1678355f576SJeff Roberson 
16808cfa56eSMark Johnston static struct sx uma_reclaim_lock;
16995c4bf75SKonstantin Belousov 
170fbd95859SMark Johnston /*
171fbd95859SMark Johnston  * kmem soft limit, initialized by uma_set_limit().  Ensure that early
172fbd95859SMark Johnston  * allocations don't trigger a wakeup of the reclaim thread.
173fbd95859SMark Johnston  */
1746d6a03d7SJeff Roberson unsigned long uma_kmem_limit = LONG_MAX;
175fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_limit, CTLFLAG_RD, &uma_kmem_limit, 0,
176fbd95859SMark Johnston     "UMA kernel memory soft limit");
1776d6a03d7SJeff Roberson unsigned long uma_kmem_total;
178fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_total, CTLFLAG_RD, &uma_kmem_total, 0,
179fbd95859SMark Johnston     "UMA kernel memory usage");
1802e47807cSJeff Roberson 
1818355f576SJeff Roberson /* Is the VM done starting up? */
182860bb7a0SMark Johnston static enum {
183860bb7a0SMark Johnston 	BOOT_COLD,
184a81c400eSJeff Roberson 	BOOT_KVA,
185860bb7a0SMark Johnston 	BOOT_RUNNING,
186860bb7a0SMark Johnston 	BOOT_SHUTDOWN,
187860bb7a0SMark Johnston } booted = BOOT_COLD;
1888355f576SJeff Roberson 
189ef72505eSJeff Roberson /*
1909643769aSJeff Roberson  * This is the handle used to schedule events that need to happen
1919643769aSJeff Roberson  * outside of the allocation fast path.
1929643769aSJeff Roberson  */
1938355f576SJeff Roberson static struct callout uma_callout;
1949643769aSJeff Roberson #define	UMA_TIMEOUT	20		/* Seconds for callout interval. */
1958355f576SJeff Roberson 
1968355f576SJeff Roberson /*
1978355f576SJeff Roberson  * This structure is passed as the zone ctor arg so that I don't have to create
1988355f576SJeff Roberson  * a special allocation function just for zones.
1998355f576SJeff Roberson  */
2008355f576SJeff Roberson struct uma_zctor_args {
201bb196eb4SMatthew D Fleming 	const char *name;
202c3bdc05fSAndrew R. Reiter 	size_t size;
2038355f576SJeff Roberson 	uma_ctor ctor;
2048355f576SJeff Roberson 	uma_dtor dtor;
2058355f576SJeff Roberson 	uma_init uminit;
2068355f576SJeff Roberson 	uma_fini fini;
2070095a784SJeff Roberson 	uma_import import;
2080095a784SJeff Roberson 	uma_release release;
2090095a784SJeff Roberson 	void *arg;
210099a0e58SBosko Milekic 	uma_keg_t keg;
211099a0e58SBosko Milekic 	int align;
21285dcf349SGleb Smirnoff 	uint32_t flags;
213099a0e58SBosko Milekic };
214099a0e58SBosko Milekic 
215099a0e58SBosko Milekic struct uma_kctor_args {
216099a0e58SBosko Milekic 	uma_zone_t zone;
217099a0e58SBosko Milekic 	size_t size;
218099a0e58SBosko Milekic 	uma_init uminit;
219099a0e58SBosko Milekic 	uma_fini fini;
2208355f576SJeff Roberson 	int align;
22185dcf349SGleb Smirnoff 	uint32_t flags;
2228355f576SJeff Roberson };
2238355f576SJeff Roberson 
224cae33c14SJeff Roberson struct uma_bucket_zone {
225cae33c14SJeff Roberson 	uma_zone_t	ubz_zone;
226cae33c14SJeff Roberson 	char		*ubz_name;
227fc03d22bSJeff Roberson 	int		ubz_entries;	/* Number of items it can hold. */
228fc03d22bSJeff Roberson 	int		ubz_maxsize;	/* Maximum allocation size per-item. */
229cae33c14SJeff Roberson };
230cae33c14SJeff Roberson 
231f9d27e75SRobert Watson /*
232fc03d22bSJeff Roberson  * Compute the actual number of bucket entries to pack them in power
233fc03d22bSJeff Roberson  * of two sizes for more efficient space utilization.
234f9d27e75SRobert Watson  */
235fc03d22bSJeff Roberson #define	BUCKET_SIZE(n)						\
236fc03d22bSJeff Roberson     (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *))
237fc03d22bSJeff Roberson 
2381aa6c758SAlexander Motin #define	BUCKET_MAX	BUCKET_SIZE(256)
239e84130a0SJeff Roberson #define	BUCKET_MIN	2
240fc03d22bSJeff Roberson 
241fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = {
242e84130a0SJeff Roberson 	/* Literal bucket sizes. */
243e84130a0SJeff Roberson 	{ NULL, "2 Bucket", 2, 4096 },
244e84130a0SJeff Roberson 	{ NULL, "4 Bucket", 4, 3072 },
245e84130a0SJeff Roberson 	{ NULL, "8 Bucket", 8, 2048 },
246e84130a0SJeff Roberson 	{ NULL, "16 Bucket", 16, 1024 },
247e84130a0SJeff Roberson 	/* Rounded down power of 2 sizes for efficiency. */
248fc03d22bSJeff Roberson 	{ NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
249fc03d22bSJeff Roberson 	{ NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
250fc03d22bSJeff Roberson 	{ NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
2511aa6c758SAlexander Motin 	{ NULL, "256 Bucket", BUCKET_SIZE(256), 64 },
252fc03d22bSJeff Roberson 	{ NULL, NULL, 0}
253fc03d22bSJeff Roberson };
254cae33c14SJeff Roberson 
2552019094aSRobert Watson /*
2562019094aSRobert Watson  * Flags and enumerations to be passed to internal functions.
2572019094aSRobert Watson  */
258bb15d1c7SGleb Smirnoff enum zfreeskip {
259bb15d1c7SGleb Smirnoff 	SKIP_NONE =	0,
260bb15d1c7SGleb Smirnoff 	SKIP_CNT =	0x00000001,
261bb15d1c7SGleb Smirnoff 	SKIP_DTOR =	0x00010000,
262bb15d1c7SGleb Smirnoff 	SKIP_FINI =	0x00020000,
263bb15d1c7SGleb Smirnoff };
264b23f72e9SBrian Feldman 
2658355f576SJeff Roberson /* Prototypes.. */
2668355f576SJeff Roberson 
267a81c400eSJeff Roberson void	uma_startup1(vm_offset_t);
268f4bef67cSGleb Smirnoff void	uma_startup2(void);
269f4bef67cSGleb Smirnoff 
270ab3185d1SJeff Roberson static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
271ab3185d1SJeff Roberson static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
272ab3059a8SMatt Macy static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
273ab3185d1SJeff Roberson static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
274ec0d8280SRyan Libby static void *contig_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
275f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t);
276ab3059a8SMatt Macy static void pcpu_page_free(void *, vm_size_t, uint8_t);
27786220393SMark Johnston static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int);
2789643769aSJeff Roberson static void cache_drain(uma_zone_t);
2798355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t);
28008cfa56eSMark Johnston static void bucket_cache_reclaim(uma_zone_t zone, bool);
281b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int);
282099a0e58SBosko Milekic static void keg_dtor(void *, int, void *);
283b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int);
2849c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *);
285d4665eaaSJeff Roberson static inline void item_dtor(uma_zone_t zone, void *item, int size,
286d4665eaaSJeff Roberson     void *udata, enum zfreeskip skip);
287b23f72e9SBrian Feldman static int zero_init(void *, int, int);
28820a4e154SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t, void *), void *);
289a81c400eSJeff Roberson static void zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *), void *);
29020a4e154SJeff Roberson static void zone_timeout(uma_zone_t zone, void *);
2913b2f2cb8SAlexander Motin static int hash_alloc(struct uma_hash *, u_int);
2920aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *);
2930aef6126SJeff Roberson static void hash_free(struct uma_hash *hash);
2948355f576SJeff Roberson static void uma_timeout(void *);
2958355f576SJeff Roberson static void uma_startup3(void);
296860bb7a0SMark Johnston static void uma_shutdown(void);
297ab3185d1SJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int, int);
2980095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
2994bd61e19SJeff Roberson static int zone_alloc_limit(uma_zone_t zone, int count, int flags);
3004bd61e19SJeff Roberson static void zone_free_limit(uma_zone_t zone, int count);
30186bbae32SJeff Roberson static void bucket_enable(void);
302cae33c14SJeff Roberson static void bucket_init(void);
3036fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int);
3046fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *);
305cae33c14SJeff Roberson static void bucket_zone_drain(void);
306beb8beefSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int);
3070095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
308bb15d1c7SGleb Smirnoff static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item);
309e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
31085dcf349SGleb Smirnoff     uma_fini fini, int align, uint32_t flags);
311b75c4efcSAndrew Turner static int zone_import(void *, void **, int, int, int);
312b75c4efcSAndrew Turner static void zone_release(void *, void **, int);
313beb8beefSJeff Roberson static bool cache_alloc(uma_zone_t, uma_cache_t, void *, int);
3140a81b439SJeff Roberson static bool cache_free(uma_zone_t, uma_cache_t, void *, void *, int);
315bbee39c6SJeff Roberson 
3167a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
3177a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
31820a4e154SJeff Roberson static int sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS);
31920a4e154SJeff Roberson static int sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS);
3206d204a6aSRyan Libby static int sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS);
321f7af5015SRyan Libby static int sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS);
3224bd61e19SJeff Roberson static int sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS);
3238355f576SJeff Roberson 
32431c251a0SJeff Roberson static uint64_t uma_zone_get_allocs(uma_zone_t zone);
32531c251a0SJeff Roberson 
32633e5a1eaSRyan Libby static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0,
32733e5a1eaSRyan Libby     "Memory allocation debugging");
32833e5a1eaSRyan Libby 
3299542ea7bSGleb Smirnoff #ifdef INVARIANTS
33031c251a0SJeff Roberson static uint64_t uma_keg_get_allocs(uma_keg_t zone);
331815db204SRyan Libby static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg);
332815db204SRyan Libby 
333c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem);
334c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem);
3359542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item);
3369542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item);
337c5deaf04SGleb Smirnoff 
338c5deaf04SGleb Smirnoff static u_int dbg_divisor = 1;
339c5deaf04SGleb Smirnoff SYSCTL_UINT(_vm_debug, OID_AUTO, divisor,
340c5deaf04SGleb Smirnoff     CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0,
341c5deaf04SGleb Smirnoff     "Debug & thrash every this item in memory allocator");
342c5deaf04SGleb Smirnoff 
343c5deaf04SGleb Smirnoff static counter_u64_t uma_dbg_cnt = EARLY_COUNTER;
344c5deaf04SGleb Smirnoff static counter_u64_t uma_skip_cnt = EARLY_COUNTER;
345c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD,
346c5deaf04SGleb Smirnoff     &uma_dbg_cnt, "memory items debugged");
347c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD,
348c5deaf04SGleb Smirnoff     &uma_skip_cnt, "memory items skipped, not debugged");
3499542ea7bSGleb Smirnoff #endif
3509542ea7bSGleb Smirnoff 
3518355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
3528355f576SJeff Roberson 
35335ec24f3SRyan Libby SYSCTL_NODE(_vm, OID_AUTO, uma, CTLFLAG_RW, 0, "Universal Memory Allocator");
35435ec24f3SRyan Libby 
355a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_INT,
3567a52a97eSRobert Watson     0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
3577a52a97eSRobert Watson 
358a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_STRUCT,
3597a52a97eSRobert Watson     0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
3607a52a97eSRobert Watson 
3612f891cd5SPawel Jakub Dawidek static int zone_warnings = 1;
362af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0,
3632f891cd5SPawel Jakub Dawidek     "Warn when UMA zones becomes full");
3642f891cd5SPawel Jakub Dawidek 
36533e5a1eaSRyan Libby static int multipage_slabs = 1;
36633e5a1eaSRyan Libby TUNABLE_INT("vm.debug.uma_multipage_slabs", &multipage_slabs);
36733e5a1eaSRyan Libby SYSCTL_INT(_vm_debug, OID_AUTO, uma_multipage_slabs,
36833e5a1eaSRyan Libby     CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &multipage_slabs, 0,
36933e5a1eaSRyan Libby     "UMA may choose larger slab sizes for better efficiency");
37033e5a1eaSRyan Libby 
37186bbae32SJeff Roberson /*
3729b8db4d0SRyan Libby  * Select the slab zone for an offpage slab with the given maximum item count.
3739b8db4d0SRyan Libby  */
3749b8db4d0SRyan Libby static inline uma_zone_t
3759b8db4d0SRyan Libby slabzone(int ipers)
3769b8db4d0SRyan Libby {
3779b8db4d0SRyan Libby 
3789b8db4d0SRyan Libby 	return (slabzones[ipers > SLABZONE0_SETSIZE]);
3799b8db4d0SRyan Libby }
3809b8db4d0SRyan Libby 
3819b8db4d0SRyan Libby /*
38286bbae32SJeff Roberson  * This routine checks to see whether or not it's safe to enable buckets.
38386bbae32SJeff Roberson  */
38486bbae32SJeff Roberson static void
38586bbae32SJeff Roberson bucket_enable(void)
38686bbae32SJeff Roberson {
3873182660aSRyan Libby 
388a81c400eSJeff Roberson 	KASSERT(booted >= BOOT_KVA, ("Bucket enable before init"));
389251386b4SMaksim Yevmenkin 	bucketdisable = vm_page_count_min();
39086bbae32SJeff Roberson }
39186bbae32SJeff Roberson 
392dc2c7965SRobert Watson /*
393dc2c7965SRobert Watson  * Initialize bucket_zones, the array of zones of buckets of various sizes.
394dc2c7965SRobert Watson  *
395dc2c7965SRobert Watson  * For each zone, calculate the memory required for each bucket, consisting
396fc03d22bSJeff Roberson  * of the header and an array of pointers.
397dc2c7965SRobert Watson  */
398cae33c14SJeff Roberson static void
399cae33c14SJeff Roberson bucket_init(void)
400cae33c14SJeff Roberson {
401cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
402cae33c14SJeff Roberson 	int size;
403cae33c14SJeff Roberson 
404d74e6a1dSAlan Cox 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
405cae33c14SJeff Roberson 		size = roundup(sizeof(struct uma_bucket), sizeof(void *));
406cae33c14SJeff Roberson 		size += sizeof(void *) * ubz->ubz_entries;
407cae33c14SJeff Roberson 		ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
408e20a199fSJeff Roberson 		    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
409dfe13344SJeff Roberson 		    UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET |
410dfe13344SJeff Roberson 		    UMA_ZONE_FIRSTTOUCH);
411cae33c14SJeff Roberson 	}
412cae33c14SJeff Roberson }
413cae33c14SJeff Roberson 
414dc2c7965SRobert Watson /*
415dc2c7965SRobert Watson  * Given a desired number of entries for a bucket, return the zone from which
416dc2c7965SRobert Watson  * to allocate the bucket.
417dc2c7965SRobert Watson  */
418dc2c7965SRobert Watson static struct uma_bucket_zone *
419dc2c7965SRobert Watson bucket_zone_lookup(int entries)
420dc2c7965SRobert Watson {
421fc03d22bSJeff Roberson 	struct uma_bucket_zone *ubz;
422dc2c7965SRobert Watson 
423fc03d22bSJeff Roberson 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
424fc03d22bSJeff Roberson 		if (ubz->ubz_entries >= entries)
425fc03d22bSJeff Roberson 			return (ubz);
426fc03d22bSJeff Roberson 	ubz--;
427fc03d22bSJeff Roberson 	return (ubz);
428fc03d22bSJeff Roberson }
429fc03d22bSJeff Roberson 
430003cf08bSMark Johnston static struct uma_bucket_zone *
431003cf08bSMark Johnston bucket_zone_max(uma_zone_t zone, int nitems)
432003cf08bSMark Johnston {
433003cf08bSMark Johnston 	struct uma_bucket_zone *ubz;
434003cf08bSMark Johnston 	int bpcpu;
435003cf08bSMark Johnston 
436003cf08bSMark Johnston 	bpcpu = 2;
437dfe13344SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
438003cf08bSMark Johnston 		/* Count the cross-domain bucket. */
439003cf08bSMark Johnston 		bpcpu++;
440003cf08bSMark Johnston 
441003cf08bSMark Johnston 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
442003cf08bSMark Johnston 		if (ubz->ubz_entries * bpcpu * mp_ncpus > nitems)
443003cf08bSMark Johnston 			break;
444003cf08bSMark Johnston 	if (ubz == &bucket_zones[0])
445003cf08bSMark Johnston 		ubz = NULL;
446003cf08bSMark Johnston 	else
447003cf08bSMark Johnston 		ubz--;
448003cf08bSMark Johnston 	return (ubz);
449003cf08bSMark Johnston }
450003cf08bSMark Johnston 
451fc03d22bSJeff Roberson static int
452fc03d22bSJeff Roberson bucket_select(int size)
453fc03d22bSJeff Roberson {
454fc03d22bSJeff Roberson 	struct uma_bucket_zone *ubz;
455fc03d22bSJeff Roberson 
456fc03d22bSJeff Roberson 	ubz = &bucket_zones[0];
457fc03d22bSJeff Roberson 	if (size > ubz->ubz_maxsize)
458fc03d22bSJeff Roberson 		return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
459fc03d22bSJeff Roberson 
460fc03d22bSJeff Roberson 	for (; ubz->ubz_entries != 0; ubz++)
461fc03d22bSJeff Roberson 		if (ubz->ubz_maxsize < size)
462fc03d22bSJeff Roberson 			break;
463fc03d22bSJeff Roberson 	ubz--;
464fc03d22bSJeff Roberson 	return (ubz->ubz_entries);
465dc2c7965SRobert Watson }
466dc2c7965SRobert Watson 
467cae33c14SJeff Roberson static uma_bucket_t
4686fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags)
469cae33c14SJeff Roberson {
470cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
471cae33c14SJeff Roberson 	uma_bucket_t bucket;
472cae33c14SJeff Roberson 
473cae33c14SJeff Roberson 	/*
474d4665eaaSJeff Roberson 	 * Don't allocate buckets early in boot.
475cae33c14SJeff Roberson 	 */
476d4665eaaSJeff Roberson 	if (__predict_false(booted < BOOT_KVA))
477cae33c14SJeff Roberson 		return (NULL);
478a81c400eSJeff Roberson 
4796fd34d6fSJeff Roberson 	/*
4806fd34d6fSJeff Roberson 	 * To limit bucket recursion we store the original zone flags
4816fd34d6fSJeff Roberson 	 * in a cookie passed via zalloc_arg/zfree_arg.  This allows the
4826fd34d6fSJeff Roberson 	 * NOVM flag to persist even through deep recursions.  We also
4836fd34d6fSJeff Roberson 	 * store ZFLAG_BUCKET once we have recursed attempting to allocate
4846fd34d6fSJeff Roberson 	 * a bucket for a bucket zone so we do not allow infinite bucket
4856fd34d6fSJeff Roberson 	 * recursion.  This cookie will even persist to frees of unused
4866fd34d6fSJeff Roberson 	 * buckets via the allocation path or bucket allocations in the
4876fd34d6fSJeff Roberson 	 * free path.
4886fd34d6fSJeff Roberson 	 */
4896fd34d6fSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
4906fd34d6fSJeff Roberson 		udata = (void *)(uintptr_t)zone->uz_flags;
491e8a720feSAlexander Motin 	else {
492e8a720feSAlexander Motin 		if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
493e8a720feSAlexander Motin 			return (NULL);
4946fd34d6fSJeff Roberson 		udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
495e8a720feSAlexander Motin 	}
496bae55c4aSRyan Libby 	if (((uintptr_t)udata & UMA_ZONE_VM) != 0)
497af526374SJeff Roberson 		flags |= M_NOVM;
49820a4e154SJeff Roberson 	ubz = bucket_zone_lookup(zone->uz_bucket_size);
49920d3ab87SAlexander Motin 	if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0)
50020d3ab87SAlexander Motin 		ubz++;
5016fd34d6fSJeff Roberson 	bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
502cae33c14SJeff Roberson 	if (bucket) {
503cae33c14SJeff Roberson #ifdef INVARIANTS
504cae33c14SJeff Roberson 		bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
505cae33c14SJeff Roberson #endif
506cae33c14SJeff Roberson 		bucket->ub_cnt = 0;
507cae33c14SJeff Roberson 		bucket->ub_entries = ubz->ubz_entries;
508d4665eaaSJeff Roberson 		bucket->ub_seq = SMR_SEQ_INVALID;
509d4665eaaSJeff Roberson 		CTR3(KTR_UMA, "bucket_alloc: zone %s(%p) allocated bucket %p",
510d4665eaaSJeff Roberson 		    zone->uz_name, zone, bucket);
511cae33c14SJeff Roberson 	}
512cae33c14SJeff Roberson 
513cae33c14SJeff Roberson 	return (bucket);
514cae33c14SJeff Roberson }
515cae33c14SJeff Roberson 
516cae33c14SJeff Roberson static void
5176fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
518cae33c14SJeff Roberson {
519cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
520cae33c14SJeff Roberson 
521fc03d22bSJeff Roberson 	KASSERT(bucket->ub_cnt == 0,
522fc03d22bSJeff Roberson 	    ("bucket_free: Freeing a non free bucket."));
523d4665eaaSJeff Roberson 	KASSERT(bucket->ub_seq == SMR_SEQ_INVALID,
524d4665eaaSJeff Roberson 	    ("bucket_free: Freeing an SMR bucket."));
5256fd34d6fSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
5266fd34d6fSJeff Roberson 		udata = (void *)(uintptr_t)zone->uz_flags;
527dc2c7965SRobert Watson 	ubz = bucket_zone_lookup(bucket->ub_entries);
5286fd34d6fSJeff Roberson 	uma_zfree_arg(ubz->ubz_zone, bucket, udata);
529cae33c14SJeff Roberson }
530cae33c14SJeff Roberson 
531cae33c14SJeff Roberson static void
532cae33c14SJeff Roberson bucket_zone_drain(void)
533cae33c14SJeff Roberson {
534cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
535cae33c14SJeff Roberson 
536cae33c14SJeff Roberson 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
53708cfa56eSMark Johnston 		uma_zone_reclaim(ubz->ubz_zone, UMA_RECLAIM_DRAIN);
538cae33c14SJeff Roberson }
539cae33c14SJeff Roberson 
54008cfa56eSMark Johnston /*
54108cfa56eSMark Johnston  * Attempt to satisfy an allocation by retrieving a full bucket from one of the
542d4665eaaSJeff Roberson  * zone's caches.  If a bucket is found the zone is not locked on return.
54308cfa56eSMark Johnston  */
5440f9b7bf3SMark Johnston static uma_bucket_t
54508cfa56eSMark Johnston zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom)
5460f9b7bf3SMark Johnston {
5470f9b7bf3SMark Johnston 	uma_bucket_t bucket;
548d4665eaaSJeff Roberson 	int i;
549d4665eaaSJeff Roberson 	bool dtor = false;
5500f9b7bf3SMark Johnston 
5510f9b7bf3SMark Johnston 	ZONE_LOCK_ASSERT(zone);
5520f9b7bf3SMark Johnston 
553dc3915c8SJeff Roberson 	if ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) == NULL)
554d4665eaaSJeff Roberson 		return (NULL);
555d4665eaaSJeff Roberson 
556*543117beSJeff Roberson 	/* SMR Buckets can not be re-used until readers expire. */
557d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0 &&
558d4665eaaSJeff Roberson 	    bucket->ub_seq != SMR_SEQ_INVALID) {
559d4665eaaSJeff Roberson 		if (!smr_poll(zone->uz_smr, bucket->ub_seq, false))
560d4665eaaSJeff Roberson 			return (NULL);
561d4665eaaSJeff Roberson 		bucket->ub_seq = SMR_SEQ_INVALID;
562*543117beSJeff Roberson 		dtor = (zone->uz_dtor != NULL) || UMA_ALWAYS_CTORDTOR;
563d4665eaaSJeff Roberson 	}
5640f9b7bf3SMark Johnston 	MPASS(zdom->uzd_nitems >= bucket->ub_cnt);
565dc3915c8SJeff Roberson 	STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link);
5660f9b7bf3SMark Johnston 	zdom->uzd_nitems -= bucket->ub_cnt;
56708cfa56eSMark Johnston 	if (zdom->uzd_imin > zdom->uzd_nitems)
5680f9b7bf3SMark Johnston 		zdom->uzd_imin = zdom->uzd_nitems;
569bb15d1c7SGleb Smirnoff 	zone->uz_bkt_count -= bucket->ub_cnt;
570d4665eaaSJeff Roberson 	ZONE_UNLOCK(zone);
571d4665eaaSJeff Roberson 	if (dtor)
572d4665eaaSJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
573d4665eaaSJeff Roberson 			item_dtor(zone, bucket->ub_bucket[i], zone->uz_size,
574d4665eaaSJeff Roberson 			    NULL, SKIP_NONE);
575d4665eaaSJeff Roberson 
5760f9b7bf3SMark Johnston 	return (bucket);
5770f9b7bf3SMark Johnston }
5780f9b7bf3SMark Johnston 
57908cfa56eSMark Johnston /*
58008cfa56eSMark Johnston  * Insert a full bucket into the specified cache.  The "ws" parameter indicates
58108cfa56eSMark Johnston  * whether the bucket's contents should be counted as part of the zone's working
58208cfa56eSMark Johnston  * set.
58308cfa56eSMark Johnston  */
5840f9b7bf3SMark Johnston static void
5850f9b7bf3SMark Johnston zone_put_bucket(uma_zone_t zone, uma_zone_domain_t zdom, uma_bucket_t bucket,
5860f9b7bf3SMark Johnston     const bool ws)
5870f9b7bf3SMark Johnston {
5880f9b7bf3SMark Johnston 
5890f9b7bf3SMark Johnston 	ZONE_LOCK_ASSERT(zone);
59008034d10SKonstantin Belousov 	KASSERT(!ws || zone->uz_bkt_count < zone->uz_bkt_max,
59108034d10SKonstantin Belousov 	    ("%s: zone %p overflow", __func__, zone));
5920f9b7bf3SMark Johnston 
593dc3915c8SJeff Roberson 	STAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link);
5940f9b7bf3SMark Johnston 	zdom->uzd_nitems += bucket->ub_cnt;
5950f9b7bf3SMark Johnston 	if (ws && zdom->uzd_imax < zdom->uzd_nitems)
5960f9b7bf3SMark Johnston 		zdom->uzd_imax = zdom->uzd_nitems;
597bb15d1c7SGleb Smirnoff 	zone->uz_bkt_count += bucket->ub_cnt;
5980f9b7bf3SMark Johnston }
5990f9b7bf3SMark Johnston 
600376b1ba3SJeff Roberson /* Pops an item out of a per-cpu cache bucket. */
601376b1ba3SJeff Roberson static inline void *
602376b1ba3SJeff Roberson cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket)
603376b1ba3SJeff Roberson {
604376b1ba3SJeff Roberson 	void *item;
605376b1ba3SJeff Roberson 
606376b1ba3SJeff Roberson 	CRITICAL_ASSERT(curthread);
607376b1ba3SJeff Roberson 
608376b1ba3SJeff Roberson 	bucket->ucb_cnt--;
609376b1ba3SJeff Roberson 	item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt];
610376b1ba3SJeff Roberson #ifdef INVARIANTS
611376b1ba3SJeff Roberson 	bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL;
612376b1ba3SJeff Roberson 	KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
613376b1ba3SJeff Roberson #endif
614376b1ba3SJeff Roberson 	cache->uc_allocs++;
615376b1ba3SJeff Roberson 
616376b1ba3SJeff Roberson 	return (item);
617376b1ba3SJeff Roberson }
618376b1ba3SJeff Roberson 
619376b1ba3SJeff Roberson /* Pushes an item into a per-cpu cache bucket. */
620376b1ba3SJeff Roberson static inline void
621376b1ba3SJeff Roberson cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item)
622376b1ba3SJeff Roberson {
623376b1ba3SJeff Roberson 
624376b1ba3SJeff Roberson 	CRITICAL_ASSERT(curthread);
625376b1ba3SJeff Roberson 	KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL,
626376b1ba3SJeff Roberson 	    ("uma_zfree: Freeing to non free bucket index."));
627376b1ba3SJeff Roberson 
628376b1ba3SJeff Roberson 	bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item;
629376b1ba3SJeff Roberson 	bucket->ucb_cnt++;
630376b1ba3SJeff Roberson 	cache->uc_frees++;
631376b1ba3SJeff Roberson }
632376b1ba3SJeff Roberson 
633376b1ba3SJeff Roberson /*
634376b1ba3SJeff Roberson  * Unload a UMA bucket from a per-cpu cache.
635376b1ba3SJeff Roberson  */
636376b1ba3SJeff Roberson static inline uma_bucket_t
637376b1ba3SJeff Roberson cache_bucket_unload(uma_cache_bucket_t bucket)
638376b1ba3SJeff Roberson {
639376b1ba3SJeff Roberson 	uma_bucket_t b;
640376b1ba3SJeff Roberson 
641376b1ba3SJeff Roberson 	b = bucket->ucb_bucket;
642376b1ba3SJeff Roberson 	if (b != NULL) {
643376b1ba3SJeff Roberson 		MPASS(b->ub_entries == bucket->ucb_entries);
644376b1ba3SJeff Roberson 		b->ub_cnt = bucket->ucb_cnt;
645376b1ba3SJeff Roberson 		bucket->ucb_bucket = NULL;
646376b1ba3SJeff Roberson 		bucket->ucb_entries = bucket->ucb_cnt = 0;
647376b1ba3SJeff Roberson 	}
648376b1ba3SJeff Roberson 
649376b1ba3SJeff Roberson 	return (b);
650376b1ba3SJeff Roberson }
651376b1ba3SJeff Roberson 
652376b1ba3SJeff Roberson static inline uma_bucket_t
653376b1ba3SJeff Roberson cache_bucket_unload_alloc(uma_cache_t cache)
654376b1ba3SJeff Roberson {
655376b1ba3SJeff Roberson 
656376b1ba3SJeff Roberson 	return (cache_bucket_unload(&cache->uc_allocbucket));
657376b1ba3SJeff Roberson }
658376b1ba3SJeff Roberson 
659376b1ba3SJeff Roberson static inline uma_bucket_t
660376b1ba3SJeff Roberson cache_bucket_unload_free(uma_cache_t cache)
661376b1ba3SJeff Roberson {
662376b1ba3SJeff Roberson 
663376b1ba3SJeff Roberson 	return (cache_bucket_unload(&cache->uc_freebucket));
664376b1ba3SJeff Roberson }
665376b1ba3SJeff Roberson 
666376b1ba3SJeff Roberson static inline uma_bucket_t
667376b1ba3SJeff Roberson cache_bucket_unload_cross(uma_cache_t cache)
668376b1ba3SJeff Roberson {
669376b1ba3SJeff Roberson 
670376b1ba3SJeff Roberson 	return (cache_bucket_unload(&cache->uc_crossbucket));
671376b1ba3SJeff Roberson }
672376b1ba3SJeff Roberson 
673376b1ba3SJeff Roberson /*
674376b1ba3SJeff Roberson  * Load a bucket into a per-cpu cache bucket.
675376b1ba3SJeff Roberson  */
676376b1ba3SJeff Roberson static inline void
677376b1ba3SJeff Roberson cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b)
678376b1ba3SJeff Roberson {
679376b1ba3SJeff Roberson 
680376b1ba3SJeff Roberson 	CRITICAL_ASSERT(curthread);
681376b1ba3SJeff Roberson 	MPASS(bucket->ucb_bucket == NULL);
682*543117beSJeff Roberson 	MPASS(b->ub_seq == SMR_SEQ_INVALID);
683376b1ba3SJeff Roberson 
684376b1ba3SJeff Roberson 	bucket->ucb_bucket = b;
685376b1ba3SJeff Roberson 	bucket->ucb_cnt = b->ub_cnt;
686376b1ba3SJeff Roberson 	bucket->ucb_entries = b->ub_entries;
687376b1ba3SJeff Roberson }
688376b1ba3SJeff Roberson 
689376b1ba3SJeff Roberson static inline void
690376b1ba3SJeff Roberson cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b)
691376b1ba3SJeff Roberson {
692376b1ba3SJeff Roberson 
693376b1ba3SJeff Roberson 	cache_bucket_load(&cache->uc_allocbucket, b);
694376b1ba3SJeff Roberson }
695376b1ba3SJeff Roberson 
696376b1ba3SJeff Roberson static inline void
697376b1ba3SJeff Roberson cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b)
698376b1ba3SJeff Roberson {
699376b1ba3SJeff Roberson 
700376b1ba3SJeff Roberson 	cache_bucket_load(&cache->uc_freebucket, b);
701376b1ba3SJeff Roberson }
702376b1ba3SJeff Roberson 
703dfe13344SJeff Roberson #ifdef NUMA
704376b1ba3SJeff Roberson static inline void
705376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b)
706376b1ba3SJeff Roberson {
707376b1ba3SJeff Roberson 
708376b1ba3SJeff Roberson 	cache_bucket_load(&cache->uc_crossbucket, b);
709376b1ba3SJeff Roberson }
710376b1ba3SJeff Roberson #endif
711376b1ba3SJeff Roberson 
712376b1ba3SJeff Roberson /*
713376b1ba3SJeff Roberson  * Copy and preserve ucb_spare.
714376b1ba3SJeff Roberson  */
715376b1ba3SJeff Roberson static inline void
716376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2)
717376b1ba3SJeff Roberson {
718376b1ba3SJeff Roberson 
719376b1ba3SJeff Roberson 	b1->ucb_bucket = b2->ucb_bucket;
720376b1ba3SJeff Roberson 	b1->ucb_entries = b2->ucb_entries;
721376b1ba3SJeff Roberson 	b1->ucb_cnt = b2->ucb_cnt;
722376b1ba3SJeff Roberson }
723376b1ba3SJeff Roberson 
724376b1ba3SJeff Roberson /*
725376b1ba3SJeff Roberson  * Swap two cache buckets.
726376b1ba3SJeff Roberson  */
727376b1ba3SJeff Roberson static inline void
728376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2)
729376b1ba3SJeff Roberson {
730376b1ba3SJeff Roberson 	struct uma_cache_bucket b3;
731376b1ba3SJeff Roberson 
732376b1ba3SJeff Roberson 	CRITICAL_ASSERT(curthread);
733376b1ba3SJeff Roberson 
734376b1ba3SJeff Roberson 	cache_bucket_copy(&b3, b1);
735376b1ba3SJeff Roberson 	cache_bucket_copy(b1, b2);
736376b1ba3SJeff Roberson 	cache_bucket_copy(b2, &b3);
737376b1ba3SJeff Roberson }
738376b1ba3SJeff Roberson 
7392f891cd5SPawel Jakub Dawidek static void
7402f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone)
7412f891cd5SPawel Jakub Dawidek {
7422f891cd5SPawel Jakub Dawidek 	static const struct timeval warninterval = { 300, 0 };
7432f891cd5SPawel Jakub Dawidek 
7442f891cd5SPawel Jakub Dawidek 	if (!zone_warnings || zone->uz_warning == NULL)
7452f891cd5SPawel Jakub Dawidek 		return;
7462f891cd5SPawel Jakub Dawidek 
7472f891cd5SPawel Jakub Dawidek 	if (ratecheck(&zone->uz_ratecheck, &warninterval))
7482f891cd5SPawel Jakub Dawidek 		printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
7492f891cd5SPawel Jakub Dawidek }
7502f891cd5SPawel Jakub Dawidek 
75154503a13SJonathan T. Looney static inline void
75254503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone)
75354503a13SJonathan T. Looney {
754e60b2fcbSGleb Smirnoff 
755e60b2fcbSGleb Smirnoff 	if (zone->uz_maxaction.ta_func != NULL)
756e60b2fcbSGleb Smirnoff 		taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction);
75754503a13SJonathan T. Looney }
75854503a13SJonathan T. Looney 
7598355f576SJeff Roberson /*
7608355f576SJeff Roberson  * Routine called by timeout which is used to fire off some time interval
7619643769aSJeff Roberson  * based calculations.  (stats, hash size, etc.)
7628355f576SJeff Roberson  *
7638355f576SJeff Roberson  * Arguments:
7648355f576SJeff Roberson  *	arg   Unused
7658355f576SJeff Roberson  *
7668355f576SJeff Roberson  * Returns:
7678355f576SJeff Roberson  *	Nothing
7688355f576SJeff Roberson  */
7698355f576SJeff Roberson static void
7708355f576SJeff Roberson uma_timeout(void *unused)
7718355f576SJeff Roberson {
77286bbae32SJeff Roberson 	bucket_enable();
77320a4e154SJeff Roberson 	zone_foreach(zone_timeout, NULL);
7748355f576SJeff Roberson 
7758355f576SJeff Roberson 	/* Reschedule this event */
7769643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
7778355f576SJeff Roberson }
7788355f576SJeff Roberson 
7798355f576SJeff Roberson /*
7800f9b7bf3SMark Johnston  * Update the working set size estimate for the zone's bucket cache.
7810f9b7bf3SMark Johnston  * The constants chosen here are somewhat arbitrary.  With an update period of
7820f9b7bf3SMark Johnston  * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the
7830f9b7bf3SMark Johnston  * last 100s.
7840f9b7bf3SMark Johnston  */
7850f9b7bf3SMark Johnston static void
7860f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom)
7870f9b7bf3SMark Johnston {
7880f9b7bf3SMark Johnston 	long wss;
7890f9b7bf3SMark Johnston 
7900f9b7bf3SMark Johnston 	MPASS(zdom->uzd_imax >= zdom->uzd_imin);
7910f9b7bf3SMark Johnston 	wss = zdom->uzd_imax - zdom->uzd_imin;
7920f9b7bf3SMark Johnston 	zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems;
79308cfa56eSMark Johnston 	zdom->uzd_wss = (4 * wss + zdom->uzd_wss) / 5;
7940f9b7bf3SMark Johnston }
7950f9b7bf3SMark Johnston 
7960f9b7bf3SMark Johnston /*
7979643769aSJeff Roberson  * Routine to perform timeout driven calculations.  This expands the
7989643769aSJeff Roberson  * hashes and does per cpu statistics aggregation.
7998355f576SJeff Roberson  *
800e20a199fSJeff Roberson  *  Returns nothing.
8018355f576SJeff Roberson  */
8028355f576SJeff Roberson static void
80320a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused)
8048355f576SJeff Roberson {
80508034d10SKonstantin Belousov 	uma_keg_t keg;
8068b987a77SJeff Roberson 	u_int slabs, pages;
8078355f576SJeff Roberson 
80854c5ae80SRyan Libby 	if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0)
80908034d10SKonstantin Belousov 		goto update_wss;
81008034d10SKonstantin Belousov 
81108034d10SKonstantin Belousov 	keg = zone->uz_keg;
8128b987a77SJeff Roberson 
8138b987a77SJeff Roberson 	/*
8148b987a77SJeff Roberson 	 * Hash zones are non-numa by definition so the first domain
8158b987a77SJeff Roberson 	 * is the only one present.
8168b987a77SJeff Roberson 	 */
8178b987a77SJeff Roberson 	KEG_LOCK(keg, 0);
8188b987a77SJeff Roberson 	pages = keg->uk_domain[0].ud_pages;
8198b987a77SJeff Roberson 
8208355f576SJeff Roberson 	/*
821e20a199fSJeff Roberson 	 * Expand the keg hash table.
8228355f576SJeff Roberson 	 *
8238355f576SJeff Roberson 	 * This is done if the number of slabs is larger than the hash size.
8248355f576SJeff Roberson 	 * What I'm trying to do here is completely reduce collisions.  This
8258355f576SJeff Roberson 	 * may be a little aggressive.  Should I allow for two collisions max?
8268355f576SJeff Roberson 	 */
8278b987a77SJeff Roberson 	if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) {
8280aef6126SJeff Roberson 		struct uma_hash newhash;
8290aef6126SJeff Roberson 		struct uma_hash oldhash;
8300aef6126SJeff Roberson 		int ret;
8315300d9ddSJeff Roberson 
8320aef6126SJeff Roberson 		/*
8330aef6126SJeff Roberson 		 * This is so involved because allocating and freeing
834e20a199fSJeff Roberson 		 * while the keg lock is held will lead to deadlock.
8350aef6126SJeff Roberson 		 * I have to do everything in stages and check for
8360aef6126SJeff Roberson 		 * races.
8370aef6126SJeff Roberson 		 */
8388b987a77SJeff Roberson 		KEG_UNLOCK(keg, 0);
8393b2f2cb8SAlexander Motin 		ret = hash_alloc(&newhash, 1 << fls(slabs));
8408b987a77SJeff Roberson 		KEG_LOCK(keg, 0);
8410aef6126SJeff Roberson 		if (ret) {
842099a0e58SBosko Milekic 			if (hash_expand(&keg->uk_hash, &newhash)) {
843099a0e58SBosko Milekic 				oldhash = keg->uk_hash;
844099a0e58SBosko Milekic 				keg->uk_hash = newhash;
8450aef6126SJeff Roberson 			} else
8460aef6126SJeff Roberson 				oldhash = newhash;
8470aef6126SJeff Roberson 
8488b987a77SJeff Roberson 			KEG_UNLOCK(keg, 0);
8490aef6126SJeff Roberson 			hash_free(&oldhash);
8508b987a77SJeff Roberson 			goto update_wss;
8510aef6126SJeff Roberson 		}
8525300d9ddSJeff Roberson 	}
8538b987a77SJeff Roberson 	KEG_UNLOCK(keg, 0);
854e20a199fSJeff Roberson 
85508034d10SKonstantin Belousov update_wss:
85608cfa56eSMark Johnston 	ZONE_LOCK(zone);
857bb15d1c7SGleb Smirnoff 	for (int i = 0; i < vm_ndomains; i++)
8580f9b7bf3SMark Johnston 		zone_domain_update_wss(&zone->uz_domain[i]);
85908cfa56eSMark Johnston 	ZONE_UNLOCK(zone);
8608355f576SJeff Roberson }
8618355f576SJeff Roberson 
8628355f576SJeff Roberson /*
8635300d9ddSJeff Roberson  * Allocate and zero fill the next sized hash table from the appropriate
8645300d9ddSJeff Roberson  * backing store.
8655300d9ddSJeff Roberson  *
8665300d9ddSJeff Roberson  * Arguments:
8670aef6126SJeff Roberson  *	hash  A new hash structure with the old hash size in uh_hashsize
8685300d9ddSJeff Roberson  *
8695300d9ddSJeff Roberson  * Returns:
870763df3ecSPedro F. Giffuni  *	1 on success and 0 on failure.
8715300d9ddSJeff Roberson  */
87237c84183SPoul-Henning Kamp static int
8733b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size)
8745300d9ddSJeff Roberson {
87559568a0eSAlexander Motin 	size_t alloc;
8765300d9ddSJeff Roberson 
8773b2f2cb8SAlexander Motin 	KASSERT(powerof2(size), ("hash size must be power of 2"));
8783b2f2cb8SAlexander Motin 	if (size > UMA_HASH_SIZE_INIT)  {
8793b2f2cb8SAlexander Motin 		hash->uh_hashsize = size;
8800aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
8811e0701e1SJeff Roberson 		hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT);
8825300d9ddSJeff Roberson 	} else {
8830aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
884e20a199fSJeff Roberson 		hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
885ab3185d1SJeff Roberson 		    UMA_ANYDOMAIN, M_WAITOK);
8860aef6126SJeff Roberson 		hash->uh_hashsize = UMA_HASH_SIZE_INIT;
8875300d9ddSJeff Roberson 	}
8880aef6126SJeff Roberson 	if (hash->uh_slab_hash) {
8890aef6126SJeff Roberson 		bzero(hash->uh_slab_hash, alloc);
8900aef6126SJeff Roberson 		hash->uh_hashmask = hash->uh_hashsize - 1;
8910aef6126SJeff Roberson 		return (1);
8920aef6126SJeff Roberson 	}
8935300d9ddSJeff Roberson 
8940aef6126SJeff Roberson 	return (0);
8955300d9ddSJeff Roberson }
8965300d9ddSJeff Roberson 
8975300d9ddSJeff Roberson /*
89864f051e9SJeff Roberson  * Expands the hash table for HASH zones.  This is done from zone_timeout
89964f051e9SJeff Roberson  * to reduce collisions.  This must not be done in the regular allocation
90064f051e9SJeff Roberson  * path, otherwise, we can recurse on the vm while allocating pages.
9018355f576SJeff Roberson  *
9028355f576SJeff Roberson  * Arguments:
9030aef6126SJeff Roberson  *	oldhash  The hash you want to expand
9040aef6126SJeff Roberson  *	newhash  The hash structure for the new table
9058355f576SJeff Roberson  *
9068355f576SJeff Roberson  * Returns:
9078355f576SJeff Roberson  *	Nothing
9088355f576SJeff Roberson  *
9098355f576SJeff Roberson  * Discussion:
9108355f576SJeff Roberson  */
9110aef6126SJeff Roberson static int
9120aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
9138355f576SJeff Roberson {
9141e0701e1SJeff Roberson 	uma_hash_slab_t slab;
9156929b7d1SPedro F. Giffuni 	u_int hval;
9166929b7d1SPedro F. Giffuni 	u_int idx;
9178355f576SJeff Roberson 
9180aef6126SJeff Roberson 	if (!newhash->uh_slab_hash)
9190aef6126SJeff Roberson 		return (0);
9208355f576SJeff Roberson 
9210aef6126SJeff Roberson 	if (oldhash->uh_hashsize >= newhash->uh_hashsize)
9220aef6126SJeff Roberson 		return (0);
9238355f576SJeff Roberson 
9248355f576SJeff Roberson 	/*
9258355f576SJeff Roberson 	 * I need to investigate hash algorithms for resizing without a
9268355f576SJeff Roberson 	 * full rehash.
9278355f576SJeff Roberson 	 */
9288355f576SJeff Roberson 
9296929b7d1SPedro F. Giffuni 	for (idx = 0; idx < oldhash->uh_hashsize; idx++)
9301e0701e1SJeff Roberson 		while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) {
9311e0701e1SJeff Roberson 			slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]);
9321e0701e1SJeff Roberson 			LIST_REMOVE(slab, uhs_hlink);
9331e0701e1SJeff Roberson 			hval = UMA_HASH(newhash, slab->uhs_data);
9341e0701e1SJeff Roberson 			LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
9351e0701e1SJeff Roberson 			    slab, uhs_hlink);
9368355f576SJeff Roberson 		}
9378355f576SJeff Roberson 
9380aef6126SJeff Roberson 	return (1);
9399c2cd7e5SJeff Roberson }
9409c2cd7e5SJeff Roberson 
9415300d9ddSJeff Roberson /*
9425300d9ddSJeff Roberson  * Free the hash bucket to the appropriate backing store.
9435300d9ddSJeff Roberson  *
9445300d9ddSJeff Roberson  * Arguments:
9455300d9ddSJeff Roberson  *	slab_hash  The hash bucket we're freeing
9465300d9ddSJeff Roberson  *	hashsize   The number of entries in that hash bucket
9475300d9ddSJeff Roberson  *
9485300d9ddSJeff Roberson  * Returns:
9495300d9ddSJeff Roberson  *	Nothing
9505300d9ddSJeff Roberson  */
9519c2cd7e5SJeff Roberson static void
9520aef6126SJeff Roberson hash_free(struct uma_hash *hash)
9539c2cd7e5SJeff Roberson {
9540aef6126SJeff Roberson 	if (hash->uh_slab_hash == NULL)
9550aef6126SJeff Roberson 		return;
9560aef6126SJeff Roberson 	if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
9570095a784SJeff Roberson 		zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
9588355f576SJeff Roberson 	else
959961647dfSJeff Roberson 		free(hash->uh_slab_hash, M_UMAHASH);
9608355f576SJeff Roberson }
9618355f576SJeff Roberson 
9628355f576SJeff Roberson /*
9638355f576SJeff Roberson  * Frees all outstanding items in a bucket
9648355f576SJeff Roberson  *
9658355f576SJeff Roberson  * Arguments:
9668355f576SJeff Roberson  *	zone   The zone to free to, must be unlocked.
9674bd61e19SJeff Roberson  *	bucket The free/alloc bucket with items.
9688355f576SJeff Roberson  *
9698355f576SJeff Roberson  * Returns:
9708355f576SJeff Roberson  *	Nothing
9718355f576SJeff Roberson  */
9728355f576SJeff Roberson 
9738355f576SJeff Roberson static void
9748355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
9758355f576SJeff Roberson {
9760095a784SJeff Roberson 	int i;
9778355f576SJeff Roberson 
9784bd61e19SJeff Roberson 	if (bucket == NULL || bucket->ub_cnt == 0)
9798355f576SJeff Roberson 		return;
9808355f576SJeff Roberson 
981d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0 &&
982d4665eaaSJeff Roberson 	    bucket->ub_seq != SMR_SEQ_INVALID) {
983d4665eaaSJeff Roberson 		smr_wait(zone->uz_smr, bucket->ub_seq);
984*543117beSJeff Roberson 		bucket->ub_seq = SMR_SEQ_INVALID;
985d4665eaaSJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
986d4665eaaSJeff Roberson 			item_dtor(zone, bucket->ub_bucket[i],
987d4665eaaSJeff Roberson 			    zone->uz_size, NULL, SKIP_NONE);
988d4665eaaSJeff Roberson 	}
9890095a784SJeff Roberson 	if (zone->uz_fini)
9900095a784SJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
9910095a784SJeff Roberson 			zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
9920095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
9934bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
9944bd61e19SJeff Roberson 		zone_free_limit(zone, bucket->ub_cnt);
995d4665eaaSJeff Roberson #ifdef INVARIANTS
996d4665eaaSJeff Roberson 	bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt);
997d4665eaaSJeff Roberson #endif
9980095a784SJeff Roberson 	bucket->ub_cnt = 0;
9998355f576SJeff Roberson }
10008355f576SJeff Roberson 
10018355f576SJeff Roberson /*
10028355f576SJeff Roberson  * Drains the per cpu caches for a zone.
10038355f576SJeff Roberson  *
1004727c6918SJeff Roberson  * NOTE: This may only be called while the zone is being torn down, and not
10055d1ae027SRobert Watson  * during normal operation.  This is necessary in order that we do not have
10065d1ae027SRobert Watson  * to migrate CPUs to drain the per-CPU caches.
10075d1ae027SRobert Watson  *
10088355f576SJeff Roberson  * Arguments:
10098355f576SJeff Roberson  *	zone     The zone to drain, must be unlocked.
10108355f576SJeff Roberson  *
10118355f576SJeff Roberson  * Returns:
10128355f576SJeff Roberson  *	Nothing
10138355f576SJeff Roberson  */
10148355f576SJeff Roberson static void
10159643769aSJeff Roberson cache_drain(uma_zone_t zone)
10168355f576SJeff Roberson {
10178355f576SJeff Roberson 	uma_cache_t cache;
1018376b1ba3SJeff Roberson 	uma_bucket_t bucket;
1019*543117beSJeff Roberson 	smr_seq_t seq;
10208355f576SJeff Roberson 	int cpu;
10218355f576SJeff Roberson 
10228355f576SJeff Roberson 	/*
10235d1ae027SRobert Watson 	 * XXX: It is safe to not lock the per-CPU caches, because we're
10245d1ae027SRobert Watson 	 * tearing down the zone anyway.  I.e., there will be no further use
10255d1ae027SRobert Watson 	 * of the caches at this point.
10265d1ae027SRobert Watson 	 *
10275d1ae027SRobert Watson 	 * XXX: It would good to be able to assert that the zone is being
10285d1ae027SRobert Watson 	 * torn down to prevent improper use of cache_drain().
10298355f576SJeff Roberson 	 */
1030*543117beSJeff Roberson 	seq = SMR_SEQ_INVALID;
1031*543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
1032*543117beSJeff Roberson 		seq = smr_current(zone->uz_smr);
10333aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
10348355f576SJeff Roberson 		cache = &zone->uz_cpu[cpu];
1035376b1ba3SJeff Roberson 		bucket = cache_bucket_unload_alloc(cache);
1036376b1ba3SJeff Roberson 		if (bucket != NULL) {
1037376b1ba3SJeff Roberson 			bucket_drain(zone, bucket);
1038376b1ba3SJeff Roberson 			bucket_free(zone, bucket, NULL);
1039376b1ba3SJeff Roberson 		}
1040376b1ba3SJeff Roberson 		bucket = cache_bucket_unload_free(cache);
1041376b1ba3SJeff Roberson 		if (bucket != NULL) {
1042*543117beSJeff Roberson 			bucket->ub_seq = seq;
1043376b1ba3SJeff Roberson 			bucket_drain(zone, bucket);
1044376b1ba3SJeff Roberson 			bucket_free(zone, bucket, NULL);
1045376b1ba3SJeff Roberson 		}
1046376b1ba3SJeff Roberson 		bucket = cache_bucket_unload_cross(cache);
1047376b1ba3SJeff Roberson 		if (bucket != NULL) {
1048*543117beSJeff Roberson 			bucket->ub_seq = seq;
1049376b1ba3SJeff Roberson 			bucket_drain(zone, bucket);
1050376b1ba3SJeff Roberson 			bucket_free(zone, bucket, NULL);
1051376b1ba3SJeff Roberson 		}
1052d56368d7SBosko Milekic 	}
105308cfa56eSMark Johnston 	bucket_cache_reclaim(zone, true);
1054aaa8bb16SJeff Roberson }
1055aaa8bb16SJeff Roberson 
1056a2de44abSAlexander Motin static void
105720a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused)
1058a2de44abSAlexander Motin {
1059a2de44abSAlexander Motin 
1060a2de44abSAlexander Motin 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
1061a2de44abSAlexander Motin 		return;
1062a2de44abSAlexander Motin 
1063a2de44abSAlexander Motin 	ZONE_LOCK(zone);
106420a4e154SJeff Roberson 	zone->uz_bucket_size =
106520a4e154SJeff Roberson 	    (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2;
1066a2de44abSAlexander Motin 	ZONE_UNLOCK(zone);
1067a2de44abSAlexander Motin }
1068a2de44abSAlexander Motin 
1069a2de44abSAlexander Motin static void
107020a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused)
1071a2de44abSAlexander Motin {
1072a2de44abSAlexander Motin 	uma_cache_t cache;
1073c1685086SJeff Roberson 	uma_bucket_t b1, b2, b3;
1074ab3185d1SJeff Roberson 	int domain;
1075a2de44abSAlexander Motin 
1076a2de44abSAlexander Motin 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
1077a2de44abSAlexander Motin 		return;
1078a2de44abSAlexander Motin 
1079c1685086SJeff Roberson 	b1 = b2 = b3 = NULL;
1080a2de44abSAlexander Motin 	critical_enter();
1081dfe13344SJeff Roberson 	if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH)
1082ab3185d1SJeff Roberson 		domain = PCPU_GET(domain);
1083ab3185d1SJeff Roberson 	else
1084ab3185d1SJeff Roberson 		domain = 0;
1085a2de44abSAlexander Motin 	cache = &zone->uz_cpu[curcpu];
1086376b1ba3SJeff Roberson 	b1 = cache_bucket_unload_alloc(cache);
1087d4665eaaSJeff Roberson 
1088d4665eaaSJeff Roberson 	/*
1089d4665eaaSJeff Roberson 	 * Don't flush SMR zone buckets.  This leaves the zone without a
1090d4665eaaSJeff Roberson 	 * bucket and forces every free to synchronize().
1091d4665eaaSJeff Roberson 	 */
1092*543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0) {
1093376b1ba3SJeff Roberson 		b2 = cache_bucket_unload_free(cache);
1094*543117beSJeff Roberson 		b3 = cache_bucket_unload_cross(cache);
1095*543117beSJeff Roberson 	}
1096*543117beSJeff Roberson 	critical_exit();
1097*543117beSJeff Roberson 
1098*543117beSJeff Roberson 	ZONE_LOCK(zone);
1099*543117beSJeff Roberson 	if (b1 != NULL && b1->ub_cnt != 0) {
1100*543117beSJeff Roberson 		zone_put_bucket(zone, &zone->uz_domain[domain], b1, false);
1101*543117beSJeff Roberson 		b1 = NULL;
1102*543117beSJeff Roberson 	}
1103376b1ba3SJeff Roberson 	if (b2 != NULL && b2->ub_cnt != 0) {
1104376b1ba3SJeff Roberson 		zone_put_bucket(zone, &zone->uz_domain[domain], b2, false);
1105376b1ba3SJeff Roberson 		b2 = NULL;
1106a2de44abSAlexander Motin 	}
1107a2de44abSAlexander Motin 	ZONE_UNLOCK(zone);
1108*543117beSJeff Roberson 
1109*543117beSJeff Roberson 	if (b1 != NULL)
11108a8d9d14SAlexander Motin 		bucket_free(zone, b1, NULL);
1111*543117beSJeff Roberson 	if (b2 != NULL)
11128a8d9d14SAlexander Motin 		bucket_free(zone, b2, NULL);
1113*543117beSJeff Roberson 	if (b3 != NULL) {
1114c1685086SJeff Roberson 		bucket_drain(zone, b3);
1115c1685086SJeff Roberson 		bucket_free(zone, b3, NULL);
1116c1685086SJeff Roberson 	}
1117a2de44abSAlexander Motin }
1118a2de44abSAlexander Motin 
1119a2de44abSAlexander Motin /*
1120a2de44abSAlexander Motin  * Safely drain per-CPU caches of a zone(s) to alloc bucket.
1121a2de44abSAlexander Motin  * This is an expensive call because it needs to bind to all CPUs
1122a2de44abSAlexander Motin  * one by one and enter a critical section on each of them in order
1123a2de44abSAlexander Motin  * to safely access their cache buckets.
1124a2de44abSAlexander Motin  * Zone lock must not be held on call this function.
1125a2de44abSAlexander Motin  */
1126a2de44abSAlexander Motin static void
112708cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone)
1128a2de44abSAlexander Motin {
1129a2de44abSAlexander Motin 	int cpu;
1130a2de44abSAlexander Motin 
1131a2de44abSAlexander Motin 	/*
1132727c6918SJeff Roberson 	 * Polite bucket sizes shrinking was not enough, shrink aggressively.
1133a2de44abSAlexander Motin 	 */
1134a2de44abSAlexander Motin 	if (zone)
113520a4e154SJeff Roberson 		cache_shrink(zone, NULL);
1136a2de44abSAlexander Motin 	else
113720a4e154SJeff Roberson 		zone_foreach(cache_shrink, NULL);
1138a2de44abSAlexander Motin 
1139a2de44abSAlexander Motin 	CPU_FOREACH(cpu) {
1140a2de44abSAlexander Motin 		thread_lock(curthread);
1141a2de44abSAlexander Motin 		sched_bind(curthread, cpu);
1142a2de44abSAlexander Motin 		thread_unlock(curthread);
1143a2de44abSAlexander Motin 
1144a2de44abSAlexander Motin 		if (zone)
114520a4e154SJeff Roberson 			cache_drain_safe_cpu(zone, NULL);
1146a2de44abSAlexander Motin 		else
114720a4e154SJeff Roberson 			zone_foreach(cache_drain_safe_cpu, NULL);
1148a2de44abSAlexander Motin 	}
1149a2de44abSAlexander Motin 	thread_lock(curthread);
1150a2de44abSAlexander Motin 	sched_unbind(curthread);
1151a2de44abSAlexander Motin 	thread_unlock(curthread);
1152a2de44abSAlexander Motin }
1153a2de44abSAlexander Motin 
1154aaa8bb16SJeff Roberson /*
115508cfa56eSMark Johnston  * Reclaim cached buckets from a zone.  All buckets are reclaimed if the caller
115608cfa56eSMark Johnston  * requested a drain, otherwise the per-domain caches are trimmed to either
115708cfa56eSMark Johnston  * estimated working set size.
1158aaa8bb16SJeff Roberson  */
1159aaa8bb16SJeff Roberson static void
116008cfa56eSMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain)
1161aaa8bb16SJeff Roberson {
1162ab3185d1SJeff Roberson 	uma_zone_domain_t zdom;
1163aaa8bb16SJeff Roberson 	uma_bucket_t bucket;
116408cfa56eSMark Johnston 	long target, tofree;
1165ab3185d1SJeff Roberson 	int i;
11668355f576SJeff Roberson 
1167ab3185d1SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
116891d947bfSJeff Roberson 		/*
116991d947bfSJeff Roberson 		 * The cross bucket is partially filled and not part of
117091d947bfSJeff Roberson 		 * the item count.  Reclaim it individually here.
117191d947bfSJeff Roberson 		 */
1172ab3185d1SJeff Roberson 		zdom = &zone->uz_domain[i];
117391d947bfSJeff Roberson 		ZONE_CROSS_LOCK(zone);
117491d947bfSJeff Roberson 		bucket = zdom->uzd_cross;
117591d947bfSJeff Roberson 		zdom->uzd_cross = NULL;
117691d947bfSJeff Roberson 		ZONE_CROSS_UNLOCK(zone);
117791d947bfSJeff Roberson 		if (bucket != NULL) {
117891d947bfSJeff Roberson 			bucket_drain(zone, bucket);
117991d947bfSJeff Roberson 			bucket_free(zone, bucket, NULL);
118091d947bfSJeff Roberson 		}
118191d947bfSJeff Roberson 
118291d947bfSJeff Roberson 		/*
118391d947bfSJeff Roberson 		 * Shrink the zone bucket size to ensure that the per-CPU caches
118491d947bfSJeff Roberson 		 * don't grow too large.
118591d947bfSJeff Roberson 		 */
118691d947bfSJeff Roberson 		ZONE_LOCK(zone);
118791d947bfSJeff Roberson 		if (i == 0 && zone->uz_bucket_size > zone->uz_bucket_size_min)
118891d947bfSJeff Roberson 			zone->uz_bucket_size--;
118908cfa56eSMark Johnston 
119008cfa56eSMark Johnston 		/*
119108cfa56eSMark Johnston 		 * If we were asked to drain the zone, we are done only once
119208cfa56eSMark Johnston 		 * this bucket cache is empty.  Otherwise, we reclaim items in
119308cfa56eSMark Johnston 		 * excess of the zone's estimated working set size.  If the
119408cfa56eSMark Johnston 		 * difference nitems - imin is larger than the WSS estimate,
119508cfa56eSMark Johnston 		 * then the estimate will grow at the end of this interval and
119608cfa56eSMark Johnston 		 * we ignore the historical average.
119708cfa56eSMark Johnston 		 */
119808cfa56eSMark Johnston 		target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems -
119908cfa56eSMark Johnston 		    zdom->uzd_imin);
120008cfa56eSMark Johnston 		while (zdom->uzd_nitems > target) {
1201dc3915c8SJeff Roberson 			bucket = STAILQ_FIRST(&zdom->uzd_buckets);
120208cfa56eSMark Johnston 			if (bucket == NULL)
120308cfa56eSMark Johnston 				break;
120408cfa56eSMark Johnston 			tofree = bucket->ub_cnt;
1205dc3915c8SJeff Roberson 			STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link);
120608cfa56eSMark Johnston 			zdom->uzd_nitems -= tofree;
120708cfa56eSMark Johnston 
120808cfa56eSMark Johnston 			/*
120908cfa56eSMark Johnston 			 * Shift the bounds of the current WSS interval to avoid
121008cfa56eSMark Johnston 			 * perturbing the estimate.
121108cfa56eSMark Johnston 			 */
121208cfa56eSMark Johnston 			zdom->uzd_imax -= lmin(zdom->uzd_imax, tofree);
121308cfa56eSMark Johnston 			zdom->uzd_imin -= lmin(zdom->uzd_imin, tofree);
121408cfa56eSMark Johnston 
12158355f576SJeff Roberson 			ZONE_UNLOCK(zone);
12168355f576SJeff Roberson 			bucket_drain(zone, bucket);
12176fd34d6fSJeff Roberson 			bucket_free(zone, bucket, NULL);
12188355f576SJeff Roberson 			ZONE_LOCK(zone);
12198355f576SJeff Roberson 		}
122091d947bfSJeff Roberson 		ZONE_UNLOCK(zone);
1221ab3185d1SJeff Roberson 	}
12228355f576SJeff Roberson }
1223fc03d22bSJeff Roberson 
1224fc03d22bSJeff Roberson static void
1225fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
1226fc03d22bSJeff Roberson {
1227fc03d22bSJeff Roberson 	uint8_t *mem;
1228fc03d22bSJeff Roberson 	int i;
1229fc03d22bSJeff Roberson 	uint8_t flags;
1230fc03d22bSJeff Roberson 
12311431a748SGleb Smirnoff 	CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes",
12321431a748SGleb Smirnoff 	    keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera);
12331431a748SGleb Smirnoff 
12341e0701e1SJeff Roberson 	mem = slab_data(slab, keg);
1235fc03d22bSJeff Roberson 	flags = slab->us_flags;
1236fc03d22bSJeff Roberson 	i = start;
1237fc03d22bSJeff Roberson 	if (keg->uk_fini != NULL) {
1238fc03d22bSJeff Roberson 		for (i--; i > -1; i--)
1239c5deaf04SGleb Smirnoff #ifdef INVARIANTS
1240c5deaf04SGleb Smirnoff 		/*
1241c5deaf04SGleb Smirnoff 		 * trash_fini implies that dtor was trash_dtor. trash_fini
1242c5deaf04SGleb Smirnoff 		 * would check that memory hasn't been modified since free,
1243c5deaf04SGleb Smirnoff 		 * which executed trash_dtor.
1244c5deaf04SGleb Smirnoff 		 * That's why we need to run uma_dbg_kskip() check here,
1245c5deaf04SGleb Smirnoff 		 * albeit we don't make skip check for other init/fini
1246c5deaf04SGleb Smirnoff 		 * invocations.
1247c5deaf04SGleb Smirnoff 		 */
12481e0701e1SJeff Roberson 		if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) ||
1249c5deaf04SGleb Smirnoff 		    keg->uk_fini != trash_fini)
1250c5deaf04SGleb Smirnoff #endif
12511e0701e1SJeff Roberson 			keg->uk_fini(slab_item(slab, keg, i), keg->uk_size);
1252fc03d22bSJeff Roberson 	}
125354c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_OFFPAGE)
12549b8db4d0SRyan Libby 		zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab),
12559b8db4d0SRyan Libby 		    NULL, SKIP_NONE);
1256fc03d22bSJeff Roberson 	keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
12572e47807cSJeff Roberson 	uma_total_dec(PAGE_SIZE * keg->uk_ppera);
12588355f576SJeff Roberson }
12598355f576SJeff Roberson 
12608355f576SJeff Roberson /*
1261e20a199fSJeff Roberson  * Frees pages from a keg back to the system.  This is done on demand from
12628355f576SJeff Roberson  * the pageout daemon.
12638355f576SJeff Roberson  *
1264e20a199fSJeff Roberson  * Returns nothing.
12658355f576SJeff Roberson  */
1266e20a199fSJeff Roberson static void
1267e20a199fSJeff Roberson keg_drain(uma_keg_t keg)
12688355f576SJeff Roberson {
12694ab3aee8SMark Johnston 	struct slabhead freeslabs;
1270ab3185d1SJeff Roberson 	uma_domain_t dom;
1271829be516SMark Johnston 	uma_slab_t slab, tmp;
12728b987a77SJeff Roberson 	int i, n;
12738355f576SJeff Roberson 
1274099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
12758355f576SJeff Roberson 		return;
12768355f576SJeff Roberson 
1277ab3185d1SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
12788b987a77SJeff Roberson 		CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u",
12794ab3aee8SMark Johnston 		    keg->uk_name, keg, i, dom->ud_free_items);
1280ab3185d1SJeff Roberson 		dom = &keg->uk_domain[i];
12814ab3aee8SMark Johnston 		LIST_INIT(&freeslabs);
1282ab3185d1SJeff Roberson 
12834ab3aee8SMark Johnston 		KEG_LOCK(keg, i);
12844ab3aee8SMark Johnston 		if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) {
12854ab3aee8SMark Johnston 			LIST_FOREACH(slab, &dom->ud_free_slab, us_link)
12864ab3aee8SMark Johnston 				UMA_HASH_REMOVE(&keg->uk_hash, slab);
12874ab3aee8SMark Johnston 		}
12884ab3aee8SMark Johnston 		n = dom->ud_free_slabs;
12894ab3aee8SMark Johnston 		LIST_SWAP(&freeslabs, &dom->ud_free_slab, uma_slab, us_link);
12904ab3aee8SMark Johnston 		dom->ud_free_slabs = 0;
12914ab3aee8SMark Johnston 		dom->ud_free_items -= n * keg->uk_ipers;
12924ab3aee8SMark Johnston 		dom->ud_pages -= n * keg->uk_ppera;
12934ab3aee8SMark Johnston 		KEG_UNLOCK(keg, i);
12944ab3aee8SMark Johnston 
12954ab3aee8SMark Johnston 		LIST_FOREACH_SAFE(slab, &freeslabs, us_link, tmp)
12961645995bSKirk McKusick 			keg_free_slab(keg, slab, keg->uk_ipers);
12978355f576SJeff Roberson 	}
12988355f576SJeff Roberson }
12998355f576SJeff Roberson 
1300e20a199fSJeff Roberson static void
130108cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain)
1302e20a199fSJeff Roberson {
1303e20a199fSJeff Roberson 
13048355f576SJeff Roberson 	/*
1305e20a199fSJeff Roberson 	 * Set draining to interlock with zone_dtor() so we can release our
1306e20a199fSJeff Roberson 	 * locks as we go.  Only dtor() should do a WAITOK call since it
1307e20a199fSJeff Roberson 	 * is the only call that knows the structure will still be available
1308e20a199fSJeff Roberson 	 * when it wakes up.
1309e20a199fSJeff Roberson 	 */
1310e20a199fSJeff Roberson 	ZONE_LOCK(zone);
131108cfa56eSMark Johnston 	while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) {
1312e20a199fSJeff Roberson 		if (waitok == M_NOWAIT)
1313e20a199fSJeff Roberson 			goto out;
1314727c6918SJeff Roberson 		msleep(zone, &zone->uz_lock, PVM, "zonedrain", 1);
1315e20a199fSJeff Roberson 	}
131608cfa56eSMark Johnston 	zone->uz_flags |= UMA_ZFLAG_RECLAIMING;
1317e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
131891d947bfSJeff Roberson 	bucket_cache_reclaim(zone, drain);
131908cfa56eSMark Johnston 
1320e20a199fSJeff Roberson 	/*
1321e20a199fSJeff Roberson 	 * The DRAINING flag protects us from being freed while
1322111fbcd5SBryan Venteicher 	 * we're running.  Normally the uma_rwlock would protect us but we
1323e20a199fSJeff Roberson 	 * must be able to release and acquire the right lock for each keg.
1324e20a199fSJeff Roberson 	 */
132508034d10SKonstantin Belousov 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0)
1326bb15d1c7SGleb Smirnoff 		keg_drain(zone->uz_keg);
1327e20a199fSJeff Roberson 	ZONE_LOCK(zone);
132808cfa56eSMark Johnston 	zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING;
1329e20a199fSJeff Roberson 	wakeup(zone);
1330e20a199fSJeff Roberson out:
1331e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
1332e20a199fSJeff Roberson }
1333e20a199fSJeff Roberson 
133408cfa56eSMark Johnston static void
133520a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused)
1336e20a199fSJeff Roberson {
1337e20a199fSJeff Roberson 
133808cfa56eSMark Johnston 	zone_reclaim(zone, M_NOWAIT, true);
133908cfa56eSMark Johnston }
134008cfa56eSMark Johnston 
134108cfa56eSMark Johnston static void
134220a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused)
134308cfa56eSMark Johnston {
134408cfa56eSMark Johnston 
134508cfa56eSMark Johnston 	zone_reclaim(zone, M_NOWAIT, false);
1346e20a199fSJeff Roberson }
1347e20a199fSJeff Roberson 
1348e20a199fSJeff Roberson /*
13498b987a77SJeff Roberson  * Allocate a new slab for a keg and inserts it into the partial slab list.
13508b987a77SJeff Roberson  * The keg should be unlocked on entry.  If the allocation succeeds it will
13518b987a77SJeff Roberson  * be locked on return.
13528355f576SJeff Roberson  *
13538355f576SJeff Roberson  * Arguments:
135486220393SMark Johnston  *	flags   Wait flags for the item initialization routine
135586220393SMark Johnston  *	aflags  Wait flags for the slab allocation
13568355f576SJeff Roberson  *
13578355f576SJeff Roberson  * Returns:
13588355f576SJeff Roberson  *	The slab that was allocated or NULL if there is no memory and the
13598355f576SJeff Roberson  *	caller specified M_NOWAIT.
13608355f576SJeff Roberson  */
13618355f576SJeff Roberson static uma_slab_t
136286220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags,
136386220393SMark Johnston     int aflags)
13648355f576SJeff Roberson {
13658b987a77SJeff Roberson 	uma_domain_t dom;
1366e20a199fSJeff Roberson 	uma_alloc allocf;
1367099a0e58SBosko Milekic 	uma_slab_t slab;
13682e47807cSJeff Roberson 	unsigned long size;
136985dcf349SGleb Smirnoff 	uint8_t *mem;
137086220393SMark Johnston 	uint8_t sflags;
13718355f576SJeff Roberson 	int i;
13728355f576SJeff Roberson 
1373ab3185d1SJeff Roberson 	KASSERT(domain >= 0 && domain < vm_ndomains,
1374ab3185d1SJeff Roberson 	    ("keg_alloc_slab: domain %d out of range", domain));
1375a553d4b8SJeff Roberson 
13768b987a77SJeff Roberson 	allocf = keg->uk_allocf;
1377194a979eSMark Johnston 	slab = NULL;
1378194a979eSMark Johnston 	mem = NULL;
137954c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) {
13809b8db4d0SRyan Libby 		uma_hash_slab_t hslab;
13819b8db4d0SRyan Libby 		hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL,
13829b8db4d0SRyan Libby 		    domain, aflags);
13839b8db4d0SRyan Libby 		if (hslab == NULL)
1384727c6918SJeff Roberson 			goto fail;
13859b8db4d0SRyan Libby 		slab = &hslab->uhs_slab;
1386a553d4b8SJeff Roberson 	}
1387a553d4b8SJeff Roberson 
13883370c5bfSJeff Roberson 	/*
13893370c5bfSJeff Roberson 	 * This reproduces the old vm_zone behavior of zero filling pages the
13903370c5bfSJeff Roberson 	 * first time they are added to a zone.
13913370c5bfSJeff Roberson 	 *
13923370c5bfSJeff Roberson 	 * Malloced items are zeroed in uma_zalloc.
13933370c5bfSJeff Roberson 	 */
13943370c5bfSJeff Roberson 
1395099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
139686220393SMark Johnston 		aflags |= M_ZERO;
13973370c5bfSJeff Roberson 	else
139886220393SMark Johnston 		aflags &= ~M_ZERO;
13993370c5bfSJeff Roberson 
1400263811f7SKip Macy 	if (keg->uk_flags & UMA_ZONE_NODUMP)
140186220393SMark Johnston 		aflags |= M_NODUMP;
1402263811f7SKip Macy 
1403e20a199fSJeff Roberson 	/* zone is passed for legacy reasons. */
1404194a979eSMark Johnston 	size = keg->uk_ppera * PAGE_SIZE;
140586220393SMark Johnston 	mem = allocf(zone, size, domain, &sflags, aflags);
1406a553d4b8SJeff Roberson 	if (mem == NULL) {
140754c5ae80SRyan Libby 		if (keg->uk_flags & UMA_ZFLAG_OFFPAGE)
14089b8db4d0SRyan Libby 			zone_free_item(slabzone(keg->uk_ipers),
14099b8db4d0SRyan Libby 			    slab_tohashslab(slab), NULL, SKIP_NONE);
1410727c6918SJeff Roberson 		goto fail;
1411a553d4b8SJeff Roberson 	}
14122e47807cSJeff Roberson 	uma_total_inc(size);
14138355f576SJeff Roberson 
14148b987a77SJeff Roberson 	/* For HASH zones all pages go to the same uma_domain. */
141554c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0)
14168b987a77SJeff Roberson 		domain = 0;
14178b987a77SJeff Roberson 
14185c0e403bSJeff Roberson 	/* Point the slab into the allocated memory */
141954c5ae80SRyan Libby 	if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE))
1420099a0e58SBosko Milekic 		slab = (uma_slab_t )(mem + keg->uk_pgoff);
14211e0701e1SJeff Roberson 	else
14229b8db4d0SRyan Libby 		slab_tohashslab(slab)->uhs_data = mem;
14235c0e403bSJeff Roberson 
142454c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_VTOSLAB)
1425099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ppera; i++)
1426584061b4SJeff Roberson 			vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE),
1427584061b4SJeff Roberson 			    zone, slab);
14288355f576SJeff Roberson 
1429099a0e58SBosko Milekic 	slab->us_freecount = keg->uk_ipers;
143086220393SMark Johnston 	slab->us_flags = sflags;
1431ab3185d1SJeff Roberson 	slab->us_domain = domain;
14328b987a77SJeff Roberson 
14339b78b1f4SJeff Roberson 	BIT_FILL(keg->uk_ipers, &slab->us_free);
1434ef72505eSJeff Roberson #ifdef INVARIANTS
1435815db204SRyan Libby 	BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg));
1436ef72505eSJeff Roberson #endif
1437099a0e58SBosko Milekic 
1438b23f72e9SBrian Feldman 	if (keg->uk_init != NULL) {
1439099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ipers; i++)
14401e0701e1SJeff Roberson 			if (keg->uk_init(slab_item(slab, keg, i),
144186220393SMark Johnston 			    keg->uk_size, flags) != 0)
1442b23f72e9SBrian Feldman 				break;
1443b23f72e9SBrian Feldman 		if (i != keg->uk_ipers) {
1444fc03d22bSJeff Roberson 			keg_free_slab(keg, slab, i);
1445727c6918SJeff Roberson 			goto fail;
1446b23f72e9SBrian Feldman 		}
1447b23f72e9SBrian Feldman 	}
14488b987a77SJeff Roberson 	KEG_LOCK(keg, domain);
14495c0e403bSJeff Roberson 
14501431a748SGleb Smirnoff 	CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)",
14511431a748SGleb Smirnoff 	    slab, keg->uk_name, keg);
14521431a748SGleb Smirnoff 
145354c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_HASH)
1454099a0e58SBosko Milekic 		UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
14558355f576SJeff Roberson 
14568b987a77SJeff Roberson 	/*
14578b987a77SJeff Roberson 	 * If we got a slab here it's safe to mark it partially used
14588b987a77SJeff Roberson 	 * and return.  We assume that the caller is going to remove
14598b987a77SJeff Roberson 	 * at least one item.
14608b987a77SJeff Roberson 	 */
14618b987a77SJeff Roberson 	dom = &keg->uk_domain[domain];
14628b987a77SJeff Roberson 	LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
14638b987a77SJeff Roberson 	dom->ud_pages += keg->uk_ppera;
14644ab3aee8SMark Johnston 	dom->ud_free_items += keg->uk_ipers;
14658355f576SJeff Roberson 
14668355f576SJeff Roberson 	return (slab);
1467727c6918SJeff Roberson 
1468727c6918SJeff Roberson fail:
1469727c6918SJeff Roberson 	return (NULL);
14708355f576SJeff Roberson }
14718355f576SJeff Roberson 
14728355f576SJeff Roberson /*
1473009b6fcbSJeff Roberson  * This function is intended to be used early on in place of page_alloc() so
1474009b6fcbSJeff Roberson  * that we may use the boot time page cache to satisfy allocations before
1475009b6fcbSJeff Roberson  * the VM is ready.
1476009b6fcbSJeff Roberson  */
1477009b6fcbSJeff Roberson static void *
1478ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1479ab3185d1SJeff Roberson     int wait)
1480009b6fcbSJeff Roberson {
1481a81c400eSJeff Roberson 	vm_paddr_t pa;
1482a81c400eSJeff Roberson 	vm_page_t m;
1483ac0a6fd0SGleb Smirnoff 	void *mem;
1484ac0a6fd0SGleb Smirnoff 	int pages;
1485a81c400eSJeff Roberson 	int i;
1486099a0e58SBosko Milekic 
1487f7d35785SGleb Smirnoff 	pages = howmany(bytes, PAGE_SIZE);
1488f7d35785SGleb Smirnoff 	KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__));
1489a81c400eSJeff Roberson 
1490f7d35785SGleb Smirnoff 	*pflag = UMA_SLAB_BOOT;
1491a81c400eSJeff Roberson 	m = vm_page_alloc_contig_domain(NULL, 0, domain,
1492a81c400eSJeff Roberson 	    malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages,
1493a81c400eSJeff Roberson 	    (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT);
1494a81c400eSJeff Roberson 	if (m == NULL)
1495a81c400eSJeff Roberson 		return (NULL);
1496a81c400eSJeff Roberson 
1497a81c400eSJeff Roberson 	pa = VM_PAGE_TO_PHYS(m);
1498a81c400eSJeff Roberson 	for (i = 0; i < pages; i++, pa += PAGE_SIZE) {
1499a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
1500a81c400eSJeff Roberson     defined(__riscv) || defined(__powerpc64__)
1501a81c400eSJeff Roberson 		if ((wait & M_NODUMP) == 0)
1502a81c400eSJeff Roberson 			dump_add_page(pa);
1503a81c400eSJeff Roberson #endif
1504a81c400eSJeff Roberson 	}
1505a81c400eSJeff Roberson 	/* Allocate KVA and indirectly advance bootmem. */
1506a81c400eSJeff Roberson 	mem = (void *)pmap_map(&bootmem, m->phys_addr,
1507a81c400eSJeff Roberson 	    m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE);
1508a81c400eSJeff Roberson         if ((wait & M_ZERO) != 0)
1509a81c400eSJeff Roberson                 bzero(mem, pages * PAGE_SIZE);
1510f7d35785SGleb Smirnoff 
1511f7d35785SGleb Smirnoff         return (mem);
1512f7d35785SGleb Smirnoff }
1513f7d35785SGleb Smirnoff 
1514a81c400eSJeff Roberson static void
1515a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes)
1516a81c400eSJeff Roberson {
1517a81c400eSJeff Roberson 	vm_offset_t va;
1518a81c400eSJeff Roberson 	vm_page_t m;
1519a81c400eSJeff Roberson 
1520a81c400eSJeff Roberson 	va = (vm_offset_t)mem;
1521a81c400eSJeff Roberson 	m = PHYS_TO_VM_PAGE(pmap_kextract(va));
1522a81c400eSJeff Roberson 	pmap_remove(kernel_pmap, va, va + bytes);
1523a81c400eSJeff Roberson 	for (; bytes != 0; bytes -= PAGE_SIZE, m++) {
1524a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
1525a81c400eSJeff Roberson     defined(__riscv) || defined(__powerpc64__)
1526a81c400eSJeff Roberson 		dump_drop_page(VM_PAGE_TO_PHYS(m));
1527a81c400eSJeff Roberson #endif
1528a81c400eSJeff Roberson 		vm_page_unwire_noq(m);
1529a81c400eSJeff Roberson 		vm_page_free(m);
1530a81c400eSJeff Roberson 	}
1531a81c400eSJeff Roberson }
1532a81c400eSJeff Roberson 
1533f7d35785SGleb Smirnoff /*
15348355f576SJeff Roberson  * Allocates a number of pages from the system
15358355f576SJeff Roberson  *
15368355f576SJeff Roberson  * Arguments:
15378355f576SJeff Roberson  *	bytes  The number of bytes requested
15388355f576SJeff Roberson  *	wait  Shall we wait?
15398355f576SJeff Roberson  *
15408355f576SJeff Roberson  * Returns:
15418355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
15428355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
15438355f576SJeff Roberson  */
15448355f576SJeff Roberson static void *
1545ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1546ab3185d1SJeff Roberson     int wait)
15478355f576SJeff Roberson {
15488355f576SJeff Roberson 	void *p;	/* Returned page */
15498355f576SJeff Roberson 
15502e47807cSJeff Roberson 	*pflag = UMA_SLAB_KERNEL;
15519978bd99SMark Johnston 	p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait);
15528355f576SJeff Roberson 
15538355f576SJeff Roberson 	return (p);
15548355f576SJeff Roberson }
15558355f576SJeff Roberson 
1556ab3059a8SMatt Macy static void *
1557ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1558ab3059a8SMatt Macy     int wait)
1559ab3059a8SMatt Macy {
1560ab3059a8SMatt Macy 	struct pglist alloctail;
1561ab3059a8SMatt Macy 	vm_offset_t addr, zkva;
1562ab3059a8SMatt Macy 	int cpu, flags;
1563ab3059a8SMatt Macy 	vm_page_t p, p_next;
1564ab3059a8SMatt Macy #ifdef NUMA
1565ab3059a8SMatt Macy 	struct pcpu *pc;
1566ab3059a8SMatt Macy #endif
1567ab3059a8SMatt Macy 
1568ab3059a8SMatt Macy 	MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE);
1569ab3059a8SMatt Macy 
1570013072f0SMark Johnston 	TAILQ_INIT(&alloctail);
1571ab3059a8SMatt Macy 	flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1572013072f0SMark Johnston 	    malloc2vm_flags(wait);
1573013072f0SMark Johnston 	*pflag = UMA_SLAB_KERNEL;
1574ab3059a8SMatt Macy 	for (cpu = 0; cpu <= mp_maxid; cpu++) {
1575ab3059a8SMatt Macy 		if (CPU_ABSENT(cpu)) {
1576ab3059a8SMatt Macy 			p = vm_page_alloc(NULL, 0, flags);
1577ab3059a8SMatt Macy 		} else {
1578ab3059a8SMatt Macy #ifndef NUMA
1579ab3059a8SMatt Macy 			p = vm_page_alloc(NULL, 0, flags);
1580ab3059a8SMatt Macy #else
1581ab3059a8SMatt Macy 			pc = pcpu_find(cpu);
158220526802SAndrew Gallatin 			if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain)))
158320526802SAndrew Gallatin 				p = NULL;
158420526802SAndrew Gallatin 			else
158520526802SAndrew Gallatin 				p = vm_page_alloc_domain(NULL, 0,
158620526802SAndrew Gallatin 				    pc->pc_domain, flags);
1587ab3059a8SMatt Macy 			if (__predict_false(p == NULL))
1588ab3059a8SMatt Macy 				p = vm_page_alloc(NULL, 0, flags);
1589ab3059a8SMatt Macy #endif
1590ab3059a8SMatt Macy 		}
1591ab3059a8SMatt Macy 		if (__predict_false(p == NULL))
1592ab3059a8SMatt Macy 			goto fail;
1593ab3059a8SMatt Macy 		TAILQ_INSERT_TAIL(&alloctail, p, listq);
1594ab3059a8SMatt Macy 	}
1595ab3059a8SMatt Macy 	if ((addr = kva_alloc(bytes)) == 0)
1596ab3059a8SMatt Macy 		goto fail;
1597ab3059a8SMatt Macy 	zkva = addr;
1598ab3059a8SMatt Macy 	TAILQ_FOREACH(p, &alloctail, listq) {
1599ab3059a8SMatt Macy 		pmap_qenter(zkva, &p, 1);
1600ab3059a8SMatt Macy 		zkva += PAGE_SIZE;
1601ab3059a8SMatt Macy 	}
1602ab3059a8SMatt Macy 	return ((void*)addr);
1603ab3059a8SMatt Macy fail:
1604ab3059a8SMatt Macy 	TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
160588ea538aSMark Johnston 		vm_page_unwire_noq(p);
1606ab3059a8SMatt Macy 		vm_page_free(p);
1607ab3059a8SMatt Macy 	}
1608ab3059a8SMatt Macy 	return (NULL);
1609ab3059a8SMatt Macy }
1610ab3059a8SMatt Macy 
16118355f576SJeff Roberson /*
16128355f576SJeff Roberson  * Allocates a number of pages from within an object
16138355f576SJeff Roberson  *
16148355f576SJeff Roberson  * Arguments:
16158355f576SJeff Roberson  *	bytes  The number of bytes requested
16168355f576SJeff Roberson  *	wait   Shall we wait?
16178355f576SJeff Roberson  *
16188355f576SJeff Roberson  * Returns:
16198355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
16208355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
16218355f576SJeff Roberson  */
16228355f576SJeff Roberson static void *
1623ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
1624ab3185d1SJeff Roberson     int wait)
16258355f576SJeff Roberson {
1626a4915c21SAttilio Rao 	TAILQ_HEAD(, vm_page) alloctail;
1627a4915c21SAttilio Rao 	u_long npages;
1628b245ac95SAlan Cox 	vm_offset_t retkva, zkva;
1629a4915c21SAttilio Rao 	vm_page_t p, p_next;
1630e20a199fSJeff Roberson 	uma_keg_t keg;
16318355f576SJeff Roberson 
1632a4915c21SAttilio Rao 	TAILQ_INIT(&alloctail);
1633bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
1634a4915c21SAttilio Rao 
1635a4915c21SAttilio Rao 	npages = howmany(bytes, PAGE_SIZE);
1636a4915c21SAttilio Rao 	while (npages > 0) {
1637ab3185d1SJeff Roberson 		p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT |
16388d6fbbb8SJeff Roberson 		    VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1639772c8b67SKonstantin Belousov 		    ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK :
1640772c8b67SKonstantin Belousov 		    VM_ALLOC_NOWAIT));
1641a4915c21SAttilio Rao 		if (p != NULL) {
1642a4915c21SAttilio Rao 			/*
1643a4915c21SAttilio Rao 			 * Since the page does not belong to an object, its
1644a4915c21SAttilio Rao 			 * listq is unused.
1645a4915c21SAttilio Rao 			 */
1646a4915c21SAttilio Rao 			TAILQ_INSERT_TAIL(&alloctail, p, listq);
1647a4915c21SAttilio Rao 			npages--;
1648a4915c21SAttilio Rao 			continue;
1649a4915c21SAttilio Rao 		}
16508355f576SJeff Roberson 		/*
1651a4915c21SAttilio Rao 		 * Page allocation failed, free intermediate pages and
1652a4915c21SAttilio Rao 		 * exit.
16538355f576SJeff Roberson 		 */
1654a4915c21SAttilio Rao 		TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
165588ea538aSMark Johnston 			vm_page_unwire_noq(p);
1656b245ac95SAlan Cox 			vm_page_free(p);
1657b245ac95SAlan Cox 		}
1658a4915c21SAttilio Rao 		return (NULL);
1659b245ac95SAlan Cox 	}
16608355f576SJeff Roberson 	*flags = UMA_SLAB_PRIV;
1661a4915c21SAttilio Rao 	zkva = keg->uk_kva +
1662a4915c21SAttilio Rao 	    atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1663a4915c21SAttilio Rao 	retkva = zkva;
1664a4915c21SAttilio Rao 	TAILQ_FOREACH(p, &alloctail, listq) {
1665a4915c21SAttilio Rao 		pmap_qenter(zkva, &p, 1);
1666a4915c21SAttilio Rao 		zkva += PAGE_SIZE;
1667a4915c21SAttilio Rao 	}
16688355f576SJeff Roberson 
16698355f576SJeff Roberson 	return ((void *)retkva);
16708355f576SJeff Roberson }
16718355f576SJeff Roberson 
16728355f576SJeff Roberson /*
1673ec0d8280SRyan Libby  * Allocate physically contiguous pages.
1674ec0d8280SRyan Libby  */
1675ec0d8280SRyan Libby static void *
1676ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1677ec0d8280SRyan Libby     int wait)
1678ec0d8280SRyan Libby {
1679ec0d8280SRyan Libby 
1680ec0d8280SRyan Libby 	*pflag = UMA_SLAB_KERNEL;
1681ec0d8280SRyan Libby 	return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain),
1682ec0d8280SRyan Libby 	    bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT));
1683ec0d8280SRyan Libby }
1684ec0d8280SRyan Libby 
1685ec0d8280SRyan Libby /*
16868355f576SJeff Roberson  * Frees a number of pages to the system
16878355f576SJeff Roberson  *
16888355f576SJeff Roberson  * Arguments:
16898355f576SJeff Roberson  *	mem   A pointer to the memory to be freed
16908355f576SJeff Roberson  *	size  The size of the memory being freed
16918355f576SJeff Roberson  *	flags The original p->us_flags field
16928355f576SJeff Roberson  *
16938355f576SJeff Roberson  * Returns:
16948355f576SJeff Roberson  *	Nothing
16958355f576SJeff Roberson  */
16968355f576SJeff Roberson static void
1697f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags)
16988355f576SJeff Roberson {
16993370c5bfSJeff Roberson 
1700a81c400eSJeff Roberson 	if ((flags & UMA_SLAB_BOOT) != 0) {
1701a81c400eSJeff Roberson 		startup_free(mem, size);
1702a81c400eSJeff Roberson 		return;
1703a81c400eSJeff Roberson 	}
1704a81c400eSJeff Roberson 
1705ec0d8280SRyan Libby 	KASSERT((flags & UMA_SLAB_KERNEL) != 0,
1706ec0d8280SRyan Libby 	    ("UMA: page_free used with invalid flags %x", flags));
17078355f576SJeff Roberson 
170849bfa624SAlan Cox 	kmem_free((vm_offset_t)mem, size);
17098355f576SJeff Roberson }
17108355f576SJeff Roberson 
17118355f576SJeff Roberson /*
1712ab3059a8SMatt Macy  * Frees pcpu zone allocations
1713ab3059a8SMatt Macy  *
1714ab3059a8SMatt Macy  * Arguments:
1715ab3059a8SMatt Macy  *	mem   A pointer to the memory to be freed
1716ab3059a8SMatt Macy  *	size  The size of the memory being freed
1717ab3059a8SMatt Macy  *	flags The original p->us_flags field
1718ab3059a8SMatt Macy  *
1719ab3059a8SMatt Macy  * Returns:
1720ab3059a8SMatt Macy  *	Nothing
1721ab3059a8SMatt Macy  */
1722ab3059a8SMatt Macy static void
1723ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags)
1724ab3059a8SMatt Macy {
1725ab3059a8SMatt Macy 	vm_offset_t sva, curva;
1726ab3059a8SMatt Macy 	vm_paddr_t paddr;
1727ab3059a8SMatt Macy 	vm_page_t m;
1728ab3059a8SMatt Macy 
1729ab3059a8SMatt Macy 	MPASS(size == (mp_maxid+1)*PAGE_SIZE);
17305ba16cf3SRyan Libby 
17315ba16cf3SRyan Libby 	if ((flags & UMA_SLAB_BOOT) != 0) {
17325ba16cf3SRyan Libby 		startup_free(mem, size);
17335ba16cf3SRyan Libby 		return;
17345ba16cf3SRyan Libby 	}
17355ba16cf3SRyan Libby 
1736ab3059a8SMatt Macy 	sva = (vm_offset_t)mem;
1737ab3059a8SMatt Macy 	for (curva = sva; curva < sva + size; curva += PAGE_SIZE) {
1738ab3059a8SMatt Macy 		paddr = pmap_kextract(curva);
1739ab3059a8SMatt Macy 		m = PHYS_TO_VM_PAGE(paddr);
174088ea538aSMark Johnston 		vm_page_unwire_noq(m);
1741ab3059a8SMatt Macy 		vm_page_free(m);
1742ab3059a8SMatt Macy 	}
1743ab3059a8SMatt Macy 	pmap_qremove(sva, size >> PAGE_SHIFT);
1744ab3059a8SMatt Macy 	kva_free(sva, size);
1745ab3059a8SMatt Macy }
1746ab3059a8SMatt Macy 
1747ab3059a8SMatt Macy 
1748ab3059a8SMatt Macy /*
17498355f576SJeff Roberson  * Zero fill initializer
17508355f576SJeff Roberson  *
17518355f576SJeff Roberson  * Arguments/Returns follow uma_init specifications
17528355f576SJeff Roberson  */
1753b23f72e9SBrian Feldman static int
1754b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags)
17558355f576SJeff Roberson {
17568355f576SJeff Roberson 	bzero(mem, size);
1757b23f72e9SBrian Feldman 	return (0);
17588355f576SJeff Roberson }
17598355f576SJeff Roberson 
1760815db204SRyan Libby #ifdef INVARIANTS
1761815db204SRyan Libby struct noslabbits *
1762815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg)
1763815db204SRyan Libby {
1764815db204SRyan Libby 
1765815db204SRyan Libby 	return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers)));
1766815db204SRyan Libby }
1767815db204SRyan Libby #endif
1768815db204SRyan Libby 
17698355f576SJeff Roberson /*
17709b78b1f4SJeff Roberson  * Actual size of embedded struct slab (!OFFPAGE).
17719b78b1f4SJeff Roberson  */
17729b78b1f4SJeff Roberson size_t
17739b78b1f4SJeff Roberson slab_sizeof(int nitems)
17749b78b1f4SJeff Roberson {
17759b78b1f4SJeff Roberson 	size_t s;
17769b78b1f4SJeff Roberson 
1777815db204SRyan Libby 	s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS;
17789b78b1f4SJeff Roberson 	return (roundup(s, UMA_ALIGN_PTR + 1));
17799b78b1f4SJeff Roberson }
17809b78b1f4SJeff Roberson 
17819b78b1f4SJeff Roberson /*
17829b78b1f4SJeff Roberson  * Size of memory for embedded slabs (!OFFPAGE).
17839b78b1f4SJeff Roberson  */
17849b78b1f4SJeff Roberson size_t
17859b78b1f4SJeff Roberson slab_space(int nitems)
17869b78b1f4SJeff Roberson {
17879b78b1f4SJeff Roberson 	return (UMA_SLAB_SIZE - slab_sizeof(nitems));
17889b78b1f4SJeff Roberson }
17899b78b1f4SJeff Roberson 
17904a8b575cSRyan Libby #define	UMA_FIXPT_SHIFT	31
17914a8b575cSRyan Libby #define	UMA_FRAC_FIXPT(n, d)						\
17924a8b575cSRyan Libby 	((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d)))
17934a8b575cSRyan Libby #define	UMA_FIXPT_PCT(f)						\
17944a8b575cSRyan Libby 	((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT))
17954a8b575cSRyan Libby #define	UMA_PCT_FIXPT(pct)	UMA_FRAC_FIXPT((pct), 100)
17964a8b575cSRyan Libby #define	UMA_MIN_EFF	UMA_PCT_FIXPT(100 - UMA_MAX_WASTE)
17974a8b575cSRyan Libby 
17989b78b1f4SJeff Roberson /*
17994a8b575cSRyan Libby  * Compute the number of items that will fit in a slab.  If hdr is true, the
18004a8b575cSRyan Libby  * item count may be limited to provide space in the slab for an inline slab
18014a8b575cSRyan Libby  * header.  Otherwise, all slab space will be provided for item storage.
18024a8b575cSRyan Libby  */
18034a8b575cSRyan Libby static u_int
18044a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr)
18054a8b575cSRyan Libby {
18064a8b575cSRyan Libby 	u_int ipers;
18074a8b575cSRyan Libby 	u_int padpi;
18084a8b575cSRyan Libby 
18094a8b575cSRyan Libby 	/* The padding between items is not needed after the last item. */
18104a8b575cSRyan Libby 	padpi = rsize - size;
18114a8b575cSRyan Libby 
18124a8b575cSRyan Libby 	if (hdr) {
18134a8b575cSRyan Libby 		/*
18144a8b575cSRyan Libby 		 * Start with the maximum item count and remove items until
18154a8b575cSRyan Libby 		 * the slab header first alongside the allocatable memory.
18164a8b575cSRyan Libby 		 */
18174a8b575cSRyan Libby 		for (ipers = MIN(SLAB_MAX_SETSIZE,
18184a8b575cSRyan Libby 		    (slabsize + padpi - slab_sizeof(1)) / rsize);
18194a8b575cSRyan Libby 		    ipers > 0 &&
18204a8b575cSRyan Libby 		    ipers * rsize - padpi + slab_sizeof(ipers) > slabsize;
18214a8b575cSRyan Libby 		    ipers--)
18224a8b575cSRyan Libby 			continue;
18234a8b575cSRyan Libby 	} else {
18244a8b575cSRyan Libby 		ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE);
18254a8b575cSRyan Libby 	}
18264a8b575cSRyan Libby 
18274a8b575cSRyan Libby 	return (ipers);
18284a8b575cSRyan Libby }
18294a8b575cSRyan Libby 
18304a8b575cSRyan Libby /*
18314a8b575cSRyan Libby  * Compute the number of items that will fit in a slab for a startup zone.
18329b78b1f4SJeff Roberson  */
18339b78b1f4SJeff Roberson int
18349b78b1f4SJeff Roberson slab_ipers(size_t size, int align)
18359b78b1f4SJeff Roberson {
18369b78b1f4SJeff Roberson 	int rsize;
18379b78b1f4SJeff Roberson 
18384a8b575cSRyan Libby 	rsize = roundup(size, align + 1); /* Assume no CACHESPREAD */
18394a8b575cSRyan Libby 	return (slab_ipers_hdr(size, rsize, UMA_SLAB_SIZE, true));
18409b78b1f4SJeff Roberson }
18419b78b1f4SJeff Roberson 
184227ca37acSRyan Libby struct keg_layout_result {
184327ca37acSRyan Libby 	u_int format;
184427ca37acSRyan Libby 	u_int slabsize;
184527ca37acSRyan Libby 	u_int ipers;
184627ca37acSRyan Libby 	u_int eff;
184727ca37acSRyan Libby };
184827ca37acSRyan Libby 
184927ca37acSRyan Libby static void
185027ca37acSRyan Libby keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt,
185127ca37acSRyan Libby     struct keg_layout_result *kl)
185227ca37acSRyan Libby {
185327ca37acSRyan Libby 	u_int total;
185427ca37acSRyan Libby 
185527ca37acSRyan Libby 	kl->format = fmt;
185627ca37acSRyan Libby 	kl->slabsize = slabsize;
185727ca37acSRyan Libby 
185827ca37acSRyan Libby 	/* Handle INTERNAL as inline with an extra page. */
185927ca37acSRyan Libby 	if ((fmt & UMA_ZFLAG_INTERNAL) != 0) {
186027ca37acSRyan Libby 		kl->format &= ~UMA_ZFLAG_INTERNAL;
186127ca37acSRyan Libby 		kl->slabsize += PAGE_SIZE;
186227ca37acSRyan Libby 	}
186327ca37acSRyan Libby 
186427ca37acSRyan Libby 	kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize,
186527ca37acSRyan Libby 	    (fmt & UMA_ZFLAG_OFFPAGE) == 0);
186627ca37acSRyan Libby 
186727ca37acSRyan Libby 	/* Account for memory used by an offpage slab header. */
186827ca37acSRyan Libby 	total = kl->slabsize;
186927ca37acSRyan Libby 	if ((fmt & UMA_ZFLAG_OFFPAGE) != 0)
187027ca37acSRyan Libby 		total += slabzone(kl->ipers)->uz_keg->uk_rsize;
187127ca37acSRyan Libby 
187227ca37acSRyan Libby 	kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total);
187327ca37acSRyan Libby }
187427ca37acSRyan Libby 
18759b78b1f4SJeff Roberson /*
18764a8b575cSRyan Libby  * Determine the format of a uma keg.  This determines where the slab header
18774a8b575cSRyan Libby  * will be placed (inline or offpage) and calculates ipers, rsize, and ppera.
18788355f576SJeff Roberson  *
18798355f576SJeff Roberson  * Arguments
1880e20a199fSJeff Roberson  *	keg  The zone we should initialize
18818355f576SJeff Roberson  *
18828355f576SJeff Roberson  * Returns
18838355f576SJeff Roberson  *	Nothing
18848355f576SJeff Roberson  */
18858355f576SJeff Roberson static void
18864a8b575cSRyan Libby keg_layout(uma_keg_t keg)
18878355f576SJeff Roberson {
188827ca37acSRyan Libby 	struct keg_layout_result kl = {}, kl_tmp;
188927ca37acSRyan Libby 	u_int fmts[2];
18904a8b575cSRyan Libby 	u_int alignsize;
189127ca37acSRyan Libby 	u_int nfmt;
18924a8b575cSRyan Libby 	u_int pages;
1893244f4554SBosko Milekic 	u_int rsize;
1894a55ebb7cSAndriy Gapon 	u_int slabsize;
189527ca37acSRyan Libby 	u_int i, j;
18968355f576SJeff Roberson 
18974a8b575cSRyan Libby 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
18984a8b575cSRyan Libby 	    (keg->uk_size <= UMA_PCPU_ALLOC_SIZE &&
18994a8b575cSRyan Libby 	     (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0),
19004a8b575cSRyan Libby 	    ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b",
19014a8b575cSRyan Libby 	     __func__, keg->uk_name, keg->uk_size, keg->uk_flags,
19024a8b575cSRyan Libby 	     PRINT_UMA_ZFLAGS));
1903bae55c4aSRyan Libby 	KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 ||
19044a8b575cSRyan Libby 	    (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0,
19054a8b575cSRyan Libby 	    ("%s: incompatible flags 0x%b", __func__, keg->uk_flags,
19064a8b575cSRyan Libby 	     PRINT_UMA_ZFLAGS));
1907e28a647dSGleb Smirnoff 
19084a8b575cSRyan Libby 	alignsize = keg->uk_align + 1;
1909ad97af7eSGleb Smirnoff 
1910ef72505eSJeff Roberson 	/*
1911ef72505eSJeff Roberson 	 * Calculate the size of each allocation (rsize) according to
1912ef72505eSJeff Roberson 	 * alignment.  If the requested size is smaller than we have
1913ef72505eSJeff Roberson 	 * allocation bits for we round it up.
1914ef72505eSJeff Roberson 	 */
19159b8db4d0SRyan Libby 	rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT);
19164a8b575cSRyan Libby 	rsize = roundup2(rsize, alignsize);
1917ad97af7eSGleb Smirnoff 
191827ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) {
19199b78b1f4SJeff Roberson 		/*
19204a8b575cSRyan Libby 		 * We want one item to start on every align boundary in a page.
19214a8b575cSRyan Libby 		 * To do this we will span pages.  We will also extend the item
19224a8b575cSRyan Libby 		 * by the size of align if it is an even multiple of align.
19234a8b575cSRyan Libby 		 * Otherwise, it would fall on the same boundary every time.
19249b78b1f4SJeff Roberson 		 */
19254a8b575cSRyan Libby 		if ((rsize & alignsize) == 0)
19264a8b575cSRyan Libby 			rsize += alignsize;
19274a8b575cSRyan Libby 		slabsize = rsize * (PAGE_SIZE / alignsize);
19284a8b575cSRyan Libby 		slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE);
19294a8b575cSRyan Libby 		slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE);
193027ca37acSRyan Libby 		slabsize = round_page(slabsize);
19314a8b575cSRyan Libby 	} else {
19324a8b575cSRyan Libby 		/*
193327ca37acSRyan Libby 		 * Start with a slab size of as many pages as it takes to
193427ca37acSRyan Libby 		 * represent a single item.  We will try to fit as many
193527ca37acSRyan Libby 		 * additional items into the slab as possible.
19364a8b575cSRyan Libby 		 */
193727ca37acSRyan Libby 		slabsize = round_page(keg->uk_size);
19381ca6ed45SGleb Smirnoff 	}
1939ad97af7eSGleb Smirnoff 
194027ca37acSRyan Libby 	/* Build a list of all of the available formats for this keg. */
194127ca37acSRyan Libby 	nfmt = 0;
194227ca37acSRyan Libby 
19434a8b575cSRyan Libby 	/* Evaluate an inline slab layout. */
19444a8b575cSRyan Libby 	if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0)
194527ca37acSRyan Libby 		fmts[nfmt++] = 0;
19464a8b575cSRyan Libby 
19474a8b575cSRyan Libby 	/* TODO: vm_page-embedded slab. */
1948244f4554SBosko Milekic 
194920e8e865SBosko Milekic 	/*
1950244f4554SBosko Milekic 	 * We can't do OFFPAGE if we're internal or if we've been
195120e8e865SBosko Milekic 	 * asked to not go to the VM for buckets.  If we do this we
1952bae55c4aSRyan Libby 	 * may end up going to the VM for slabs which we do not want
1953bae55c4aSRyan Libby 	 * to do if we're UMA_ZONE_VM, which clearly forbids it.
1954bae55c4aSRyan Libby 	 * In those cases, evaluate a pseudo-format called INTERNAL
1955bae55c4aSRyan Libby 	 * which has an inline slab header and one extra page to
1956bae55c4aSRyan Libby 	 * guarantee that it fits.
195727ca37acSRyan Libby 	 *
195827ca37acSRyan Libby 	 * Otherwise, see if using an OFFPAGE slab will improve our
195927ca37acSRyan Libby 	 * efficiency.
196020e8e865SBosko Milekic 	 */
1961bae55c4aSRyan Libby 	if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0)
196227ca37acSRyan Libby 		fmts[nfmt++] = UMA_ZFLAG_INTERNAL;
196327ca37acSRyan Libby 	else
196427ca37acSRyan Libby 		fmts[nfmt++] = UMA_ZFLAG_OFFPAGE;
1965244f4554SBosko Milekic 
1966ef72505eSJeff Roberson 	/*
196727ca37acSRyan Libby 	 * Choose a slab size and format which satisfy the minimum efficiency.
196827ca37acSRyan Libby 	 * Prefer the smallest slab size that meets the constraints.
1969ef72505eSJeff Roberson 	 *
197027ca37acSRyan Libby 	 * Start with a minimum slab size, to accommodate CACHESPREAD.  Then,
197127ca37acSRyan Libby 	 * for small items (up to PAGE_SIZE), the iteration increment is one
197227ca37acSRyan Libby 	 * page; and for large items, the increment is one item.
1973ef72505eSJeff Roberson 	 */
197427ca37acSRyan Libby 	i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize);
197527ca37acSRyan Libby 	KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u",
197627ca37acSRyan Libby 	    keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize,
197727ca37acSRyan Libby 	    rsize, i));
197827ca37acSRyan Libby 	for ( ; ; i++) {
197927ca37acSRyan Libby 		slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) :
198027ca37acSRyan Libby 		    round_page(rsize * (i - 1) + keg->uk_size);
198127ca37acSRyan Libby 
198227ca37acSRyan Libby 		for (j = 0; j < nfmt; j++) {
198327ca37acSRyan Libby 			/* Only if we have no viable format yet. */
198427ca37acSRyan Libby 			if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 &&
198527ca37acSRyan Libby 			    kl.ipers > 0)
198627ca37acSRyan Libby 				continue;
198727ca37acSRyan Libby 
198827ca37acSRyan Libby 			keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp);
198927ca37acSRyan Libby 			if (kl_tmp.eff <= kl.eff)
199027ca37acSRyan Libby 				continue;
199127ca37acSRyan Libby 
199227ca37acSRyan Libby 			kl = kl_tmp;
199327ca37acSRyan Libby 
199427ca37acSRyan Libby 			CTR6(KTR_UMA, "keg %s layout: format %#x "
199527ca37acSRyan Libby 			    "(ipers %u * rsize %u) / slabsize %#x = %u%% eff",
199627ca37acSRyan Libby 			    keg->uk_name, kl.format, kl.ipers, rsize,
199727ca37acSRyan Libby 			    kl.slabsize, UMA_FIXPT_PCT(kl.eff));
199827ca37acSRyan Libby 
199927ca37acSRyan Libby 			/* Stop when we reach the minimum efficiency. */
200027ca37acSRyan Libby 			if (kl.eff >= UMA_MIN_EFF)
200127ca37acSRyan Libby 				break;
20028355f576SJeff Roberson 		}
2003ad97af7eSGleb Smirnoff 
200433e5a1eaSRyan Libby 		if (kl.eff >= UMA_MIN_EFF || !multipage_slabs ||
200527ca37acSRyan Libby 		    slabsize >= SLAB_MAX_SETSIZE * rsize ||
200627ca37acSRyan Libby 		    (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0)
200727ca37acSRyan Libby 			break;
200827ca37acSRyan Libby 	}
200927ca37acSRyan Libby 
201027ca37acSRyan Libby 	pages = atop(kl.slabsize);
201127ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
201227ca37acSRyan Libby 		pages *= mp_maxid + 1;
201327ca37acSRyan Libby 
201427ca37acSRyan Libby 	keg->uk_rsize = rsize;
201527ca37acSRyan Libby 	keg->uk_ipers = kl.ipers;
201627ca37acSRyan Libby 	keg->uk_ppera = pages;
201727ca37acSRyan Libby 	keg->uk_flags |= kl.format;
201827ca37acSRyan Libby 
20194a8b575cSRyan Libby 	/*
20204a8b575cSRyan Libby 	 * How do we find the slab header if it is offpage or if not all item
20214a8b575cSRyan Libby 	 * start addresses are in the same page?  We could solve the latter
20224a8b575cSRyan Libby 	 * case with vaddr alignment, but we don't.
20234a8b575cSRyan Libby 	 */
202427ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 ||
202527ca37acSRyan Libby 	    (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) {
202654c5ae80SRyan Libby 		if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0)
202727ca37acSRyan Libby 			keg->uk_flags |= UMA_ZFLAG_HASH;
202854c5ae80SRyan Libby 		else
202927ca37acSRyan Libby 			keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
203054c5ae80SRyan Libby 	}
203127ca37acSRyan Libby 
2032e63a1c2fSRyan Libby 	CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u",
203327ca37acSRyan Libby 	    __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers,
203427ca37acSRyan Libby 	    pages);
20354a8b575cSRyan Libby 	KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE,
20364a8b575cSRyan Libby 	    ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__,
203727ca37acSRyan Libby 	     keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize,
203827ca37acSRyan Libby 	     keg->uk_ipers, pages));
2039e20a199fSJeff Roberson }
2040e20a199fSJeff Roberson 
20418355f576SJeff Roberson /*
2042099a0e58SBosko Milekic  * Keg header ctor.  This initializes all fields, locks, etc.  And inserts
2043099a0e58SBosko Milekic  * the keg onto the global keg list.
20448355f576SJeff Roberson  *
20458355f576SJeff Roberson  * Arguments/Returns follow uma_ctor specifications
2046099a0e58SBosko Milekic  *	udata  Actually uma_kctor_args
2047099a0e58SBosko Milekic  */
2048b23f72e9SBrian Feldman static int
2049b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags)
2050099a0e58SBosko Milekic {
2051099a0e58SBosko Milekic 	struct uma_kctor_args *arg = udata;
2052099a0e58SBosko Milekic 	uma_keg_t keg = mem;
2053099a0e58SBosko Milekic 	uma_zone_t zone;
20548b987a77SJeff Roberson 	int i;
2055099a0e58SBosko Milekic 
2056099a0e58SBosko Milekic 	bzero(keg, size);
2057099a0e58SBosko Milekic 	keg->uk_size = arg->size;
2058099a0e58SBosko Milekic 	keg->uk_init = arg->uminit;
2059099a0e58SBosko Milekic 	keg->uk_fini = arg->fini;
2060099a0e58SBosko Milekic 	keg->uk_align = arg->align;
20616fd34d6fSJeff Roberson 	keg->uk_reserve = 0;
2062099a0e58SBosko Milekic 	keg->uk_flags = arg->flags;
2063099a0e58SBosko Milekic 
2064099a0e58SBosko Milekic 	/*
2065194a979eSMark Johnston 	 * We use a global round-robin policy by default.  Zones with
2066dfe13344SJeff Roberson 	 * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which
2067dfe13344SJeff Roberson 	 * case the iterator is never run.
2068194a979eSMark Johnston 	 */
2069194a979eSMark Johnston 	keg->uk_dr.dr_policy = DOMAINSET_RR();
2070194a979eSMark Johnston 	keg->uk_dr.dr_iter = 0;
2071194a979eSMark Johnston 
2072194a979eSMark Johnston 	/*
2073099a0e58SBosko Milekic 	 * The master zone is passed to us at keg-creation time.
2074099a0e58SBosko Milekic 	 */
2075099a0e58SBosko Milekic 	zone = arg->zone;
2076e20a199fSJeff Roberson 	keg->uk_name = zone->uz_name;
2077099a0e58SBosko Milekic 
2078099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_ZINIT)
2079099a0e58SBosko Milekic 		keg->uk_init = zero_init;
2080099a0e58SBosko Milekic 
2081cfcae3f8SGleb Smirnoff 	if (arg->flags & UMA_ZONE_MALLOC)
208254c5ae80SRyan Libby 		keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
2083e20a199fSJeff Roberson 
208454c5ae80SRyan Libby #ifndef SMP
2085ad97af7eSGleb Smirnoff 	keg->uk_flags &= ~UMA_ZONE_PCPU;
2086ad97af7eSGleb Smirnoff #endif
2087ad97af7eSGleb Smirnoff 
20884a8b575cSRyan Libby 	keg_layout(keg);
2089099a0e58SBosko Milekic 
20908b987a77SJeff Roberson 	/*
2091dfe13344SJeff Roberson 	 * Use a first-touch NUMA policy for all kegs that pmap_extract()
2092dfe13344SJeff Roberson 	 * will work on with the exception of critical VM structures
2093dfe13344SJeff Roberson 	 * necessary for paging.
2094dfe13344SJeff Roberson 	 *
2095dfe13344SJeff Roberson 	 * Zones may override the default by specifying either.
20968b987a77SJeff Roberson 	 */
2097dfe13344SJeff Roberson #ifdef NUMA
2098dfe13344SJeff Roberson 	if ((keg->uk_flags &
209954c5ae80SRyan Libby 	    (UMA_ZFLAG_HASH | UMA_ZONE_VM | UMA_ZONE_ROUNDROBIN)) == 0)
2100dfe13344SJeff Roberson 		keg->uk_flags |= UMA_ZONE_FIRSTTOUCH;
2101dfe13344SJeff Roberson 	else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0)
2102dfe13344SJeff Roberson 		keg->uk_flags |= UMA_ZONE_ROUNDROBIN;
21038b987a77SJeff Roberson #endif
21048b987a77SJeff Roberson 
2105099a0e58SBosko Milekic 	/*
2106099a0e58SBosko Milekic 	 * If we haven't booted yet we need allocations to go through the
2107099a0e58SBosko Milekic 	 * startup cache until the vm is ready.
2108099a0e58SBosko Milekic 	 */
210977e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC
2110a81c400eSJeff Roberson 	if (keg->uk_ppera == 1)
211177e19437SGleb Smirnoff 		keg->uk_allocf = uma_small_alloc;
2112a81c400eSJeff Roberson 	else
21138cd02d00SAlan Cox #endif
2114a81c400eSJeff Roberson 	if (booted < BOOT_KVA)
2115a81c400eSJeff Roberson 		keg->uk_allocf = startup_alloc;
2116ab3059a8SMatt Macy 	else if (keg->uk_flags & UMA_ZONE_PCPU)
2117ab3059a8SMatt Macy 		keg->uk_allocf = pcpu_page_alloc;
2118ec0d8280SRyan Libby 	else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1)
2119ec0d8280SRyan Libby 		keg->uk_allocf = contig_alloc;
212077e19437SGleb Smirnoff 	else
212177e19437SGleb Smirnoff 		keg->uk_allocf = page_alloc;
212277e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC
212377e19437SGleb Smirnoff 	if (keg->uk_ppera == 1)
212477e19437SGleb Smirnoff 		keg->uk_freef = uma_small_free;
212577e19437SGleb Smirnoff 	else
212677e19437SGleb Smirnoff #endif
2127ab3059a8SMatt Macy 	if (keg->uk_flags & UMA_ZONE_PCPU)
2128ab3059a8SMatt Macy 		keg->uk_freef = pcpu_page_free;
2129ab3059a8SMatt Macy 	else
213077e19437SGleb Smirnoff 		keg->uk_freef = page_free;
2131099a0e58SBosko Milekic 
2132099a0e58SBosko Milekic 	/*
21338b987a77SJeff Roberson 	 * Initialize keg's locks.
2134099a0e58SBosko Milekic 	 */
21358b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
21368b987a77SJeff Roberson 		KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS));
2137099a0e58SBosko Milekic 
2138099a0e58SBosko Milekic 	/*
2139099a0e58SBosko Milekic 	 * If we're putting the slab header in the actual page we need to
21409b78b1f4SJeff Roberson 	 * figure out where in each page it goes.  See slab_sizeof
21419b78b1f4SJeff Roberson 	 * definition.
2142099a0e58SBosko Milekic 	 */
214354c5ae80SRyan Libby 	if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) {
21449b78b1f4SJeff Roberson 		size_t shsize;
21459b78b1f4SJeff Roberson 
21469b78b1f4SJeff Roberson 		shsize = slab_sizeof(keg->uk_ipers);
21479b78b1f4SJeff Roberson 		keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize;
2148244f4554SBosko Milekic 		/*
2149244f4554SBosko Milekic 		 * The only way the following is possible is if with our
2150244f4554SBosko Milekic 		 * UMA_ALIGN_PTR adjustments we are now bigger than
2151244f4554SBosko Milekic 		 * UMA_SLAB_SIZE.  I haven't checked whether this is
2152244f4554SBosko Milekic 		 * mathematically possible for all cases, so we make
2153244f4554SBosko Milekic 		 * sure here anyway.
2154244f4554SBosko Milekic 		 */
21559b78b1f4SJeff Roberson 		KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera,
21563d5e3df7SGleb Smirnoff 		    ("zone %s ipers %d rsize %d size %d slab won't fit",
21573d5e3df7SGleb Smirnoff 		    zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size));
2158099a0e58SBosko Milekic 	}
2159099a0e58SBosko Milekic 
216054c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_HASH)
21613b2f2cb8SAlexander Motin 		hash_alloc(&keg->uk_hash, 0);
2162099a0e58SBosko Milekic 
2163e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone);
2164099a0e58SBosko Milekic 
2165099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
2166099a0e58SBosko Milekic 
2167111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
2168099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
2169111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
2170b23f72e9SBrian Feldman 	return (0);
2171099a0e58SBosko Milekic }
2172099a0e58SBosko Milekic 
21732efcc8cbSGleb Smirnoff static void
2174a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused)
2175a81c400eSJeff Roberson {
2176a81c400eSJeff Roberson 	uma_keg_t keg;
2177a81c400eSJeff Roberson 
2178a81c400eSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
2179a81c400eSJeff Roberson 		return;
2180a81c400eSJeff Roberson 	KEG_GET(zone, keg);
2181ec0d8280SRyan Libby 
2182ec0d8280SRyan Libby 	if (keg->uk_allocf == startup_alloc) {
2183ec0d8280SRyan Libby 		/* Switch to the real allocator. */
2184f96d4157SJeff Roberson 		if (keg->uk_flags & UMA_ZONE_PCPU)
2185f96d4157SJeff Roberson 			keg->uk_allocf = pcpu_page_alloc;
2186ec0d8280SRyan Libby 		else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 &&
2187ec0d8280SRyan Libby 		    keg->uk_ppera > 1)
2188ec0d8280SRyan Libby 			keg->uk_allocf = contig_alloc;
2189ec0d8280SRyan Libby 		else
2190a81c400eSJeff Roberson 			keg->uk_allocf = page_alloc;
2191a81c400eSJeff Roberson 	}
2192ec0d8280SRyan Libby }
2193a81c400eSJeff Roberson 
2194a81c400eSJeff Roberson static void
219520a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused)
21962efcc8cbSGleb Smirnoff {
21972efcc8cbSGleb Smirnoff 
21982efcc8cbSGleb Smirnoff 	zone->uz_allocs = counter_u64_alloc(M_WAITOK);
21992efcc8cbSGleb Smirnoff 	zone->uz_frees = counter_u64_alloc(M_WAITOK);
22002efcc8cbSGleb Smirnoff 	zone->uz_fails = counter_u64_alloc(M_WAITOK);
22012efcc8cbSGleb Smirnoff }
22022efcc8cbSGleb Smirnoff 
220320a4e154SJeff Roberson static void
220420a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused)
220520a4e154SJeff Roberson {
220620a4e154SJeff Roberson 	uma_zone_domain_t zdom;
22078b987a77SJeff Roberson 	uma_domain_t dom;
220820a4e154SJeff Roberson 	uma_keg_t keg;
220920a4e154SJeff Roberson 	struct sysctl_oid *oid, *domainoid;
22103b490537SJeff Roberson 	int domains, i, cnt;
221120a4e154SJeff Roberson 	static const char *nokeg = "cache zone";
221220a4e154SJeff Roberson 	char *c;
221320a4e154SJeff Roberson 
221420a4e154SJeff Roberson 	/*
221520a4e154SJeff Roberson 	 * Make a sysctl safe copy of the zone name by removing
221620a4e154SJeff Roberson 	 * any special characters and handling dups by appending
221720a4e154SJeff Roberson 	 * an index.
221820a4e154SJeff Roberson 	 */
221920a4e154SJeff Roberson 	if (zone->uz_namecnt != 0) {
22203b490537SJeff Roberson 		/* Count the number of decimal digits and '_' separator. */
22213b490537SJeff Roberson 		for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++)
22223b490537SJeff Roberson 			cnt /= 10;
22233b490537SJeff Roberson 		zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1,
22243b490537SJeff Roberson 		    M_UMA, M_WAITOK);
222520a4e154SJeff Roberson 		sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name,
222620a4e154SJeff Roberson 		    zone->uz_namecnt);
222720a4e154SJeff Roberson 	} else
222820a4e154SJeff Roberson 		zone->uz_ctlname = strdup(zone->uz_name, M_UMA);
222920a4e154SJeff Roberson 	for (c = zone->uz_ctlname; *c != '\0'; c++)
223020a4e154SJeff Roberson 		if (strchr("./\\ -", *c) != NULL)
223120a4e154SJeff Roberson 			*c = '_';
223220a4e154SJeff Roberson 
223320a4e154SJeff Roberson 	/*
223420a4e154SJeff Roberson 	 * Basic parameters at the root.
223520a4e154SJeff Roberson 	 */
223620a4e154SJeff Roberson 	zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma),
223720a4e154SJeff Roberson 	    OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, "");
223820a4e154SJeff Roberson 	oid = zone->uz_oid;
223920a4e154SJeff Roberson 	SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
224020a4e154SJeff Roberson 	    "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size");
22416d204a6aSRyan Libby 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
22426d204a6aSRyan Libby 	    "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE,
22436d204a6aSRyan Libby 	    zone, 0, sysctl_handle_uma_zone_flags, "A",
224420a4e154SJeff Roberson 	    "Allocator configuration flags");
224520a4e154SJeff Roberson 	SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
224620a4e154SJeff Roberson 	    "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0,
224720a4e154SJeff Roberson 	    "Desired per-cpu cache size");
224820a4e154SJeff Roberson 	SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
224920a4e154SJeff Roberson 	    "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0,
225020a4e154SJeff Roberson 	    "Maximum allowed per-cpu cache size");
225120a4e154SJeff Roberson 
225220a4e154SJeff Roberson 	/*
225320a4e154SJeff Roberson 	 * keg if present.
225420a4e154SJeff Roberson 	 */
225554c5ae80SRyan Libby 	if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0)
22568b987a77SJeff Roberson 		domains = vm_ndomains;
22578b987a77SJeff Roberson 	else
22588b987a77SJeff Roberson 		domains = 1;
225920a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
226020a4e154SJeff Roberson 	    "keg", CTLFLAG_RD, NULL, "");
226120a4e154SJeff Roberson 	keg = zone->uz_keg;
22623b490537SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) {
226320a4e154SJeff Roberson 		SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
226420a4e154SJeff Roberson 		    "name", CTLFLAG_RD, keg->uk_name, "Keg name");
226520a4e154SJeff Roberson 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
226620a4e154SJeff Roberson 		    "rsize", CTLFLAG_RD, &keg->uk_rsize, 0,
226720a4e154SJeff Roberson 		    "Real object size with alignment");
226820a4e154SJeff Roberson 		SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
226920a4e154SJeff Roberson 		    "ppera", CTLFLAG_RD, &keg->uk_ppera, 0,
227020a4e154SJeff Roberson 		    "pages per-slab allocation");
227120a4e154SJeff Roberson 		SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
227220a4e154SJeff Roberson 		    "ipers", CTLFLAG_RD, &keg->uk_ipers, 0,
227320a4e154SJeff Roberson 		    "items available per-slab");
227420a4e154SJeff Roberson 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
227520a4e154SJeff Roberson 		    "align", CTLFLAG_RD, &keg->uk_align, 0,
227620a4e154SJeff Roberson 		    "item alignment mask");
2277f7af5015SRyan Libby 		SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2278f7af5015SRyan Libby 		    "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
2279f7af5015SRyan Libby 		    keg, 0, sysctl_handle_uma_slab_efficiency, "I",
2280f7af5015SRyan Libby 		    "Slab utilization (100 - internal fragmentation %)");
22818b987a77SJeff Roberson 		domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid),
22828b987a77SJeff Roberson 		    OID_AUTO, "domain", CTLFLAG_RD, NULL, "");
22838b987a77SJeff Roberson 		for (i = 0; i < domains; i++) {
22848b987a77SJeff Roberson 			dom = &keg->uk_domain[i];
22858b987a77SJeff Roberson 			oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
22868b987a77SJeff Roberson 			    OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD,
22878b987a77SJeff Roberson 			    NULL, "");
22888b987a77SJeff Roberson 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
22898b987a77SJeff Roberson 			    "pages", CTLFLAG_RD, &dom->ud_pages, 0,
22908b987a77SJeff Roberson 			    "Total pages currently allocated from VM");
22918b987a77SJeff Roberson 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
22924ab3aee8SMark Johnston 			    "free_items", CTLFLAG_RD, &dom->ud_free_items, 0,
22938b987a77SJeff Roberson 			    "items free in the slab layer");
22948b987a77SJeff Roberson 		}
229520a4e154SJeff Roberson 	} else
229620a4e154SJeff Roberson 		SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
229720a4e154SJeff Roberson 		    "name", CTLFLAG_RD, nokeg, "Keg name");
229820a4e154SJeff Roberson 
229920a4e154SJeff Roberson 	/*
230020a4e154SJeff Roberson 	 * Information about zone limits.
230120a4e154SJeff Roberson 	 */
230220a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
230320a4e154SJeff Roberson 	    "limit", CTLFLAG_RD, NULL, "");
23044bd61e19SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
23054bd61e19SJeff Roberson 	    "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
23064bd61e19SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_items, "QU",
23074bd61e19SJeff Roberson 	    "current number of allocated items if limit is set");
230820a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
230920a4e154SJeff Roberson 	    "max_items", CTLFLAG_RD, &zone->uz_max_items, 0,
231020a4e154SJeff Roberson 	    "Maximum number of cached items");
231120a4e154SJeff Roberson 	SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
231220a4e154SJeff Roberson 	    "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0,
231320a4e154SJeff Roberson 	    "Number of threads sleeping at limit");
231420a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
231520a4e154SJeff Roberson 	    "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0,
231620a4e154SJeff Roberson 	    "Total zone limit sleeps");
23174bd61e19SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
23184bd61e19SJeff Roberson 	    "bucket_max", CTLFLAG_RD, &zone->uz_bkt_max, 0,
23194bd61e19SJeff Roberson 	    "Maximum number of items in the bucket cache");
23204bd61e19SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
23214bd61e19SJeff Roberson 	    "bucket_cnt", CTLFLAG_RD, &zone->uz_bkt_count, 0,
23224bd61e19SJeff Roberson 	    "Number of items in the bucket cache");
232320a4e154SJeff Roberson 
232420a4e154SJeff Roberson 	/*
23258b987a77SJeff Roberson 	 * Per-domain zone information.
232620a4e154SJeff Roberson 	 */
232720a4e154SJeff Roberson 	domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid),
232820a4e154SJeff Roberson 	    OID_AUTO, "domain", CTLFLAG_RD, NULL, "");
2329dfe13344SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0)
23308b987a77SJeff Roberson 		domains = 1;
233120a4e154SJeff Roberson 	for (i = 0; i < domains; i++) {
233220a4e154SJeff Roberson 		zdom = &zone->uz_domain[i];
233320a4e154SJeff Roberson 		oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
233420a4e154SJeff Roberson 		    OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, "");
233520a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
233620a4e154SJeff Roberson 		    "nitems", CTLFLAG_RD, &zdom->uzd_nitems,
233720a4e154SJeff Roberson 		    "number of items in this domain");
233820a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
233920a4e154SJeff Roberson 		    "imax", CTLFLAG_RD, &zdom->uzd_imax,
234020a4e154SJeff Roberson 		    "maximum item count in this period");
234120a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
234220a4e154SJeff Roberson 		    "imin", CTLFLAG_RD, &zdom->uzd_imin,
234320a4e154SJeff Roberson 		    "minimum item count in this period");
234420a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
234520a4e154SJeff Roberson 		    "wss", CTLFLAG_RD, &zdom->uzd_wss,
234620a4e154SJeff Roberson 		    "Working set size");
234720a4e154SJeff Roberson 	}
234820a4e154SJeff Roberson 
234920a4e154SJeff Roberson 	/*
235020a4e154SJeff Roberson 	 * General statistics.
235120a4e154SJeff Roberson 	 */
235220a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
235320a4e154SJeff Roberson 	    "stats", CTLFLAG_RD, NULL, "");
235420a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
235520a4e154SJeff Roberson 	    "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
235620a4e154SJeff Roberson 	    zone, 1, sysctl_handle_uma_zone_cur, "I",
235720a4e154SJeff Roberson 	    "Current number of allocated items");
235820a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
235920a4e154SJeff Roberson 	    "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
236020a4e154SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_allocs, "QU",
236120a4e154SJeff Roberson 	    "Total allocation calls");
236220a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
236320a4e154SJeff Roberson 	    "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
236420a4e154SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_frees, "QU",
236520a4e154SJeff Roberson 	    "Total free calls");
236620a4e154SJeff Roberson 	SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
236720a4e154SJeff Roberson 	    "fails", CTLFLAG_RD, &zone->uz_fails,
236820a4e154SJeff Roberson 	    "Number of allocation failures");
236920a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
237020a4e154SJeff Roberson 	    "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 0,
237120a4e154SJeff Roberson 	    "Free calls from the wrong domain");
237220a4e154SJeff Roberson }
237320a4e154SJeff Roberson 
237420a4e154SJeff Roberson struct uma_zone_count {
237520a4e154SJeff Roberson 	const char	*name;
237620a4e154SJeff Roberson 	int		count;
237720a4e154SJeff Roberson };
237820a4e154SJeff Roberson 
237920a4e154SJeff Roberson static void
238020a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg)
238120a4e154SJeff Roberson {
238220a4e154SJeff Roberson 	struct uma_zone_count *cnt;
238320a4e154SJeff Roberson 
238420a4e154SJeff Roberson 	cnt = arg;
23853b490537SJeff Roberson 	/*
23863b490537SJeff Roberson 	 * Some zones are rapidly created with identical names and
23873b490537SJeff Roberson 	 * destroyed out of order.  This can lead to gaps in the count.
23883b490537SJeff Roberson 	 * Use one greater than the maximum observed for this name.
23893b490537SJeff Roberson 	 */
239020a4e154SJeff Roberson 	if (strcmp(zone->uz_name, cnt->name) == 0)
23913b490537SJeff Roberson 		cnt->count = MAX(cnt->count,
23923b490537SJeff Roberson 		    zone->uz_namecnt + 1);
239320a4e154SJeff Roberson }
239420a4e154SJeff Roberson 
2395cc7ce83aSJeff Roberson static void
2396cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone)
2397cc7ce83aSJeff Roberson {
2398cc7ce83aSJeff Roberson 	int i;
2399cc7ce83aSJeff Roberson 
2400cc7ce83aSJeff Roberson 	for (i = 0; i <= mp_maxid; i++) {
2401cc7ce83aSJeff Roberson 		cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size);
2402cc7ce83aSJeff Roberson 		cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags);
2403cc7ce83aSJeff Roberson 	}
2404cc7ce83aSJeff Roberson }
2405cc7ce83aSJeff Roberson 
2406099a0e58SBosko Milekic /*
2407099a0e58SBosko Milekic  * Zone header ctor.  This initializes all fields, locks, etc.
2408099a0e58SBosko Milekic  *
2409099a0e58SBosko Milekic  * Arguments/Returns follow uma_ctor specifications
2410099a0e58SBosko Milekic  *	udata  Actually uma_zctor_args
24118355f576SJeff Roberson  */
2412b23f72e9SBrian Feldman static int
2413b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags)
24148355f576SJeff Roberson {
241520a4e154SJeff Roberson 	struct uma_zone_count cnt;
24168355f576SJeff Roberson 	struct uma_zctor_args *arg = udata;
24178355f576SJeff Roberson 	uma_zone_t zone = mem;
2418099a0e58SBosko Milekic 	uma_zone_t z;
2419099a0e58SBosko Milekic 	uma_keg_t keg;
242008cfa56eSMark Johnston 	int i;
24218355f576SJeff Roberson 
24228355f576SJeff Roberson 	bzero(zone, size);
24238355f576SJeff Roberson 	zone->uz_name = arg->name;
24248355f576SJeff Roberson 	zone->uz_ctor = arg->ctor;
24258355f576SJeff Roberson 	zone->uz_dtor = arg->dtor;
2426099a0e58SBosko Milekic 	zone->uz_init = NULL;
2427099a0e58SBosko Milekic 	zone->uz_fini = NULL;
2428bf965959SSean Bruno 	zone->uz_sleeps = 0;
2429c1685086SJeff Roberson 	zone->uz_xdomain = 0;
243020a4e154SJeff Roberson 	zone->uz_bucket_size = 0;
243120a4e154SJeff Roberson 	zone->uz_bucket_size_min = 0;
243220a4e154SJeff Roberson 	zone->uz_bucket_size_max = BUCKET_MAX;
2433d4665eaaSJeff Roberson 	zone->uz_flags = (arg->flags & UMA_ZONE_SMR);
24342f891cd5SPawel Jakub Dawidek 	zone->uz_warning = NULL;
2435ab3185d1SJeff Roberson 	/* The domain structures follow the cpu structures. */
24368d1c459aSRyan Libby 	zone->uz_domain =
24378d1c459aSRyan Libby 	    (struct uma_zone_domain *)&zone->uz_cpu[mp_maxid + 1];
2438bb15d1c7SGleb Smirnoff 	zone->uz_bkt_max = ULONG_MAX;
24392f891cd5SPawel Jakub Dawidek 	timevalclear(&zone->uz_ratecheck);
2440af526374SJeff Roberson 
244120a4e154SJeff Roberson 	/* Count the number of duplicate names. */
244220a4e154SJeff Roberson 	cnt.name = arg->name;
244320a4e154SJeff Roberson 	cnt.count = 0;
244420a4e154SJeff Roberson 	zone_foreach(zone_count, &cnt);
244520a4e154SJeff Roberson 	zone->uz_namecnt = cnt.count;
2446727c6918SJeff Roberson 	ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
244791d947bfSJeff Roberson 	ZONE_CROSS_LOCK_INIT(zone);
24482efcc8cbSGleb Smirnoff 
244908cfa56eSMark Johnston 	for (i = 0; i < vm_ndomains; i++)
2450dc3915c8SJeff Roberson 		STAILQ_INIT(&zone->uz_domain[i].uzd_buckets);
245108cfa56eSMark Johnston 
2452ca293436SRyan Libby #ifdef INVARIANTS
2453ca293436SRyan Libby 	if (arg->uminit == trash_init && arg->fini == trash_fini)
2454cc7ce83aSJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR;
2455ca293436SRyan Libby #endif
2456ca293436SRyan Libby 
24570095a784SJeff Roberson 	/*
24580095a784SJeff Roberson 	 * This is a pure cache zone, no kegs.
24590095a784SJeff Roberson 	 */
24600095a784SJeff Roberson 	if (arg->import) {
2461727c6918SJeff Roberson 		KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0,
2462727c6918SJeff Roberson 		    ("zone_ctor: Import specified for non-cache zone."));
24636fd34d6fSJeff Roberson 		zone->uz_flags = arg->flags;
2464af526374SJeff Roberson 		zone->uz_size = arg->size;
24650095a784SJeff Roberson 		zone->uz_import = arg->import;
24660095a784SJeff Roberson 		zone->uz_release = arg->release;
24670095a784SJeff Roberson 		zone->uz_arg = arg->arg;
2468111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
246903175483SAlexander Motin 		LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
2470111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
2471af526374SJeff Roberson 		goto out;
24720095a784SJeff Roberson 	}
24730095a784SJeff Roberson 
24740095a784SJeff Roberson 	/*
24750095a784SJeff Roberson 	 * Use the regular zone/keg/slab allocator.
24760095a784SJeff Roberson 	 */
2477b75c4efcSAndrew Turner 	zone->uz_import = zone_import;
2478b75c4efcSAndrew Turner 	zone->uz_release = zone_release;
24790095a784SJeff Roberson 	zone->uz_arg = zone;
2480bb15d1c7SGleb Smirnoff 	keg = arg->keg;
24810095a784SJeff Roberson 
2482099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_SECONDARY) {
248320a4e154SJeff Roberson 		KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
248420a4e154SJeff Roberson 		    ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
2485099a0e58SBosko Milekic 		KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
24868355f576SJeff Roberson 		zone->uz_init = arg->uminit;
2487e221e841SJeff Roberson 		zone->uz_fini = arg->fini;
2488e20a199fSJeff Roberson 		zone->uz_flags |= UMA_ZONE_SECONDARY;
2489111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
2490099a0e58SBosko Milekic 		ZONE_LOCK(zone);
2491099a0e58SBosko Milekic 		LIST_FOREACH(z, &keg->uk_zones, uz_link) {
2492099a0e58SBosko Milekic 			if (LIST_NEXT(z, uz_link) == NULL) {
2493099a0e58SBosko Milekic 				LIST_INSERT_AFTER(z, zone, uz_link);
2494099a0e58SBosko Milekic 				break;
2495099a0e58SBosko Milekic 			}
2496099a0e58SBosko Milekic 		}
2497099a0e58SBosko Milekic 		ZONE_UNLOCK(zone);
2498111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
2499e20a199fSJeff Roberson 	} else if (keg == NULL) {
2500e20a199fSJeff Roberson 		if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
2501e20a199fSJeff Roberson 		    arg->align, arg->flags)) == NULL)
2502b23f72e9SBrian Feldman 			return (ENOMEM);
2503099a0e58SBosko Milekic 	} else {
2504099a0e58SBosko Milekic 		struct uma_kctor_args karg;
2505b23f72e9SBrian Feldman 		int error;
2506099a0e58SBosko Milekic 
2507099a0e58SBosko Milekic 		/* We should only be here from uma_startup() */
2508099a0e58SBosko Milekic 		karg.size = arg->size;
2509099a0e58SBosko Milekic 		karg.uminit = arg->uminit;
2510099a0e58SBosko Milekic 		karg.fini = arg->fini;
2511099a0e58SBosko Milekic 		karg.align = arg->align;
2512d4665eaaSJeff Roberson 		karg.flags = (arg->flags & ~UMA_ZONE_SMR);
2513099a0e58SBosko Milekic 		karg.zone = zone;
2514b23f72e9SBrian Feldman 		error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
2515b23f72e9SBrian Feldman 		    flags);
2516b23f72e9SBrian Feldman 		if (error)
2517b23f72e9SBrian Feldman 			return (error);
2518099a0e58SBosko Milekic 	}
25190095a784SJeff Roberson 
252020a4e154SJeff Roberson 	/* Inherit properties from the keg. */
2521bb15d1c7SGleb Smirnoff 	zone->uz_keg = keg;
2522e20a199fSJeff Roberson 	zone->uz_size = keg->uk_size;
2523e20a199fSJeff Roberson 	zone->uz_flags |= (keg->uk_flags &
2524e20a199fSJeff Roberson 	    (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
25258355f576SJeff Roberson 
252620a4e154SJeff Roberson out:
2527860bb7a0SMark Johnston 	if (__predict_true(booted >= BOOT_RUNNING)) {
252820a4e154SJeff Roberson 		zone_alloc_counters(zone, NULL);
252920a4e154SJeff Roberson 		zone_alloc_sysctl(zone, NULL);
253020a4e154SJeff Roberson 	} else {
253120a4e154SJeff Roberson 		zone->uz_allocs = EARLY_COUNTER;
253220a4e154SJeff Roberson 		zone->uz_frees = EARLY_COUNTER;
253320a4e154SJeff Roberson 		zone->uz_fails = EARLY_COUNTER;
2534099a0e58SBosko Milekic 	}
25358355f576SJeff Roberson 
2536d4665eaaSJeff Roberson 	/* Caller requests a private SMR context. */
2537d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
2538d4665eaaSJeff Roberson 		zone->uz_smr = smr_create(zone->uz_name);
2539d4665eaaSJeff Roberson 
25407e28037aSMark Johnston 	KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) !=
25417e28037aSMark Johnston 	    (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET),
25427e28037aSMark Johnston 	    ("Invalid zone flag combination"));
254320a4e154SJeff Roberson 	if (arg->flags & UMA_ZFLAG_INTERNAL)
254420a4e154SJeff Roberson 		zone->uz_bucket_size_max = zone->uz_bucket_size = 0;
254520a4e154SJeff Roberson 	if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0)
254620a4e154SJeff Roberson 		zone->uz_bucket_size = BUCKET_MAX;
254720a4e154SJeff Roberson 	else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0)
254820a4e154SJeff Roberson 		zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN;
254920a4e154SJeff Roberson 	else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0)
255020a4e154SJeff Roberson 		zone->uz_bucket_size = 0;
25517e28037aSMark Johnston 	else
255220a4e154SJeff Roberson 		zone->uz_bucket_size = bucket_select(zone->uz_size);
255320a4e154SJeff Roberson 	zone->uz_bucket_size_min = zone->uz_bucket_size;
2554cc7ce83aSJeff Roberson 	if (zone->uz_dtor != NULL || zone->uz_ctor != NULL)
2555cc7ce83aSJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_CTORDTOR;
2556cc7ce83aSJeff Roberson 	zone_update_caches(zone);
2557fc03d22bSJeff Roberson 
2558b23f72e9SBrian Feldman 	return (0);
25598355f576SJeff Roberson }
25608355f576SJeff Roberson 
25618355f576SJeff Roberson /*
2562099a0e58SBosko Milekic  * Keg header dtor.  This frees all data, destroys locks, frees the hash
2563099a0e58SBosko Milekic  * table and removes the keg from the global list.
25649c2cd7e5SJeff Roberson  *
25659c2cd7e5SJeff Roberson  * Arguments/Returns follow uma_dtor specifications
25669c2cd7e5SJeff Roberson  *	udata  unused
25679c2cd7e5SJeff Roberson  */
2568099a0e58SBosko Milekic static void
2569099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata)
2570099a0e58SBosko Milekic {
2571099a0e58SBosko Milekic 	uma_keg_t keg;
25728b987a77SJeff Roberson 	uint32_t free, pages;
25738b987a77SJeff Roberson 	int i;
25749c2cd7e5SJeff Roberson 
2575099a0e58SBosko Milekic 	keg = (uma_keg_t)arg;
25768b987a77SJeff Roberson 	free = pages = 0;
25778b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
25784ab3aee8SMark Johnston 		free += keg->uk_domain[i].ud_free_items;
25798b987a77SJeff Roberson 		pages += keg->uk_domain[i].ud_pages;
25808b987a77SJeff Roberson 		KEG_LOCK_FINI(keg, i);
2581099a0e58SBosko Milekic 	}
25827e240677SRyan Libby 	if (pages != 0)
25838b987a77SJeff Roberson 		printf("Freed UMA keg (%s) was not empty (%u items). "
25848b987a77SJeff Roberson 		    " Lost %u pages of memory.\n",
25858b987a77SJeff Roberson 		    keg->uk_name ? keg->uk_name : "",
25867e240677SRyan Libby 		    pages / keg->uk_ppera * keg->uk_ipers - free, pages);
2587099a0e58SBosko Milekic 
2588099a0e58SBosko Milekic 	hash_free(&keg->uk_hash);
2589099a0e58SBosko Milekic }
2590099a0e58SBosko Milekic 
2591099a0e58SBosko Milekic /*
2592099a0e58SBosko Milekic  * Zone header dtor.
2593099a0e58SBosko Milekic  *
2594099a0e58SBosko Milekic  * Arguments/Returns follow uma_dtor specifications
2595099a0e58SBosko Milekic  *	udata  unused
2596099a0e58SBosko Milekic  */
25979c2cd7e5SJeff Roberson static void
25989c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata)
25999c2cd7e5SJeff Roberson {
26009c2cd7e5SJeff Roberson 	uma_zone_t zone;
2601099a0e58SBosko Milekic 	uma_keg_t keg;
26029c2cd7e5SJeff Roberson 
26039c2cd7e5SJeff Roberson 	zone = (uma_zone_t)arg;
26049643769aSJeff Roberson 
260520a4e154SJeff Roberson 	sysctl_remove_oid(zone->uz_oid, 1, 1);
260620a4e154SJeff Roberson 
2607e20a199fSJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
26089643769aSJeff Roberson 		cache_drain(zone);
2609099a0e58SBosko Milekic 
2610111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
2611099a0e58SBosko Milekic 	LIST_REMOVE(zone, uz_link);
2612111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
2613099a0e58SBosko Milekic 	/*
2614099a0e58SBosko Milekic 	 * XXX there are some races here where
2615099a0e58SBosko Milekic 	 * the zone can be drained but zone lock
2616099a0e58SBosko Milekic 	 * released and then refilled before we
2617099a0e58SBosko Milekic 	 * remove it... we dont care for now
2618099a0e58SBosko Milekic 	 */
261908cfa56eSMark Johnston 	zone_reclaim(zone, M_WAITOK, true);
2620e20a199fSJeff Roberson 	/*
2621323ad386STycho Nightingale 	 * We only destroy kegs from non secondary/non cache zones.
2622e20a199fSJeff Roberson 	 */
2623323ad386STycho Nightingale 	if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) {
2624323ad386STycho Nightingale 		keg = zone->uz_keg;
2625111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
2626099a0e58SBosko Milekic 		LIST_REMOVE(keg, uk_link);
2627111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
26280095a784SJeff Roberson 		zone_free_item(kegs, keg, NULL, SKIP_NONE);
26299c2cd7e5SJeff Roberson 	}
26302efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_allocs);
26312efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_frees);
26322efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_fails);
263320a4e154SJeff Roberson 	free(zone->uz_ctlname, M_UMA);
2634af526374SJeff Roberson 	ZONE_LOCK_FINI(zone);
263591d947bfSJeff Roberson 	ZONE_CROSS_LOCK_FINI(zone);
2636099a0e58SBosko Milekic }
2637099a0e58SBosko Milekic 
2638a81c400eSJeff Roberson static void
2639a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg)
2640a81c400eSJeff Roberson {
2641a81c400eSJeff Roberson 	uma_keg_t keg;
2642a81c400eSJeff Roberson 	uma_zone_t zone;
2643a81c400eSJeff Roberson 
2644a81c400eSJeff Roberson 	LIST_FOREACH(keg, &uma_kegs, uk_link) {
2645a81c400eSJeff Roberson 		LIST_FOREACH(zone, &keg->uk_zones, uz_link)
2646a81c400eSJeff Roberson 			zfunc(zone, arg);
2647a81c400eSJeff Roberson 	}
2648a81c400eSJeff Roberson 	LIST_FOREACH(zone, &uma_cachezones, uz_link)
2649a81c400eSJeff Roberson 		zfunc(zone, arg);
2650a81c400eSJeff Roberson }
2651a81c400eSJeff Roberson 
26529c2cd7e5SJeff Roberson /*
26538355f576SJeff Roberson  * Traverses every zone in the system and calls a callback
26548355f576SJeff Roberson  *
26558355f576SJeff Roberson  * Arguments:
26568355f576SJeff Roberson  *	zfunc  A pointer to a function which accepts a zone
26578355f576SJeff Roberson  *		as an argument.
26588355f576SJeff Roberson  *
26598355f576SJeff Roberson  * Returns:
26608355f576SJeff Roberson  *	Nothing
26618355f576SJeff Roberson  */
26628355f576SJeff Roberson static void
266320a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg)
26648355f576SJeff Roberson {
26658355f576SJeff Roberson 
2666111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
2667a81c400eSJeff Roberson 	zone_foreach_unlocked(zfunc, arg);
2668111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
26698355f576SJeff Roberson }
26708355f576SJeff Roberson 
2671f4bef67cSGleb Smirnoff /*
2672a81c400eSJeff Roberson  * Initialize the kernel memory allocator.  This is done after pages can be
2673a81c400eSJeff Roberson  * allocated but before general KVA is available.
2674f4bef67cSGleb Smirnoff  */
2675a81c400eSJeff Roberson void
2676a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail)
2677f4bef67cSGleb Smirnoff {
2678a81c400eSJeff Roberson 	struct uma_zctor_args args;
2679a81c400eSJeff Roberson 	size_t ksize, zsize, size;
2680a81c400eSJeff Roberson 	uma_keg_t masterkeg;
2681a81c400eSJeff Roberson 	uintptr_t m;
2682a81c400eSJeff Roberson 	uint8_t pflag;
2683a81c400eSJeff Roberson 
2684a81c400eSJeff Roberson 	bootstart = bootmem = virtual_avail;
2685a81c400eSJeff Roberson 
2686a81c400eSJeff Roberson 	rw_init(&uma_rwlock, "UMA lock");
2687a81c400eSJeff Roberson 	sx_init(&uma_reclaim_lock, "umareclaim");
2688f4bef67cSGleb Smirnoff 
2689f4bef67cSGleb Smirnoff 	ksize = sizeof(struct uma_keg) +
2690f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_domain) * vm_ndomains);
269179c9f942SJeff Roberson 	ksize = roundup(ksize, UMA_SUPER_ALIGN);
2692f4bef67cSGleb Smirnoff 	zsize = sizeof(struct uma_zone) +
2693f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_cache) * (mp_maxid + 1)) +
2694f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_zone_domain) * vm_ndomains);
269579c9f942SJeff Roberson 	zsize = roundup(zsize, UMA_SUPER_ALIGN);
2696f4bef67cSGleb Smirnoff 
2697a81c400eSJeff Roberson 	/* Allocate the zone of zones, zone of kegs, and zone of zones keg. */
2698a81c400eSJeff Roberson 	size = (zsize * 2) + ksize;
2699a81c400eSJeff Roberson 	m = (uintptr_t)startup_alloc(NULL, size, 0, &pflag, M_NOWAIT | M_ZERO);
2700ab3185d1SJeff Roberson 	zones = (uma_zone_t)m;
270179c9f942SJeff Roberson 	m += zsize;
2702ab3185d1SJeff Roberson 	kegs = (uma_zone_t)m;
270379c9f942SJeff Roberson 	m += zsize;
2704ab3185d1SJeff Roberson 	masterkeg = (uma_keg_t)m;
2705ab3185d1SJeff Roberson 
2706099a0e58SBosko Milekic 	/* "manually" create the initial zone */
27070095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
2708099a0e58SBosko Milekic 	args.name = "UMA Kegs";
2709ab3185d1SJeff Roberson 	args.size = ksize;
2710099a0e58SBosko Milekic 	args.ctor = keg_ctor;
2711099a0e58SBosko Milekic 	args.dtor = keg_dtor;
27128355f576SJeff Roberson 	args.uminit = zero_init;
27138355f576SJeff Roberson 	args.fini = NULL;
2714ab3185d1SJeff Roberson 	args.keg = masterkeg;
271579c9f942SJeff Roberson 	args.align = UMA_SUPER_ALIGN - 1;
2716b60f5b79SJeff Roberson 	args.flags = UMA_ZFLAG_INTERNAL;
2717ab3185d1SJeff Roberson 	zone_ctor(kegs, zsize, &args, M_WAITOK);
27188355f576SJeff Roberson 
2719099a0e58SBosko Milekic 	args.name = "UMA Zones";
2720f4bef67cSGleb Smirnoff 	args.size = zsize;
2721099a0e58SBosko Milekic 	args.ctor = zone_ctor;
2722099a0e58SBosko Milekic 	args.dtor = zone_dtor;
2723099a0e58SBosko Milekic 	args.uminit = zero_init;
2724099a0e58SBosko Milekic 	args.fini = NULL;
2725099a0e58SBosko Milekic 	args.keg = NULL;
272679c9f942SJeff Roberson 	args.align = UMA_SUPER_ALIGN - 1;
2727099a0e58SBosko Milekic 	args.flags = UMA_ZFLAG_INTERNAL;
2728ab3185d1SJeff Roberson 	zone_ctor(zones, zsize, &args, M_WAITOK);
2729099a0e58SBosko Milekic 
27309b8db4d0SRyan Libby 	/* Now make zones for slab headers */
27319b8db4d0SRyan Libby 	slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE,
27329b8db4d0SRyan Libby 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
27339b8db4d0SRyan Libby 	slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE,
27341e0701e1SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
27358355f576SJeff Roberson 
27368355f576SJeff Roberson 	hashzone = uma_zcreate("UMA Hash",
27378355f576SJeff Roberson 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
27381e0701e1SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
27398355f576SJeff Roberson 
2740a81c400eSJeff Roberson 	bucket_init();
2741d4665eaaSJeff Roberson 	smr_init();
27428355f576SJeff Roberson }
27438355f576SJeff Roberson 
2744a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC
2745a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void);
2746f4bef67cSGleb Smirnoff #endif
2747f4bef67cSGleb Smirnoff 
2748a81c400eSJeff Roberson /*
2749a81c400eSJeff Roberson  * Advertise the availability of normal kva allocations and switch to
2750a81c400eSJeff Roberson  * the default back-end allocator.  Marks the KVA we consumed on startup
2751a81c400eSJeff Roberson  * as used in the map.
2752a81c400eSJeff Roberson  */
27538355f576SJeff Roberson void
275499571dc3SJeff Roberson uma_startup2(void)
27558355f576SJeff Roberson {
2756f4bef67cSGleb Smirnoff 
2757530cc6a2SJeff Roberson 	if (bootstart != bootmem) {
2758a81c400eSJeff Roberson 		vm_map_lock(kernel_map);
2759a81c400eSJeff Roberson 		(void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem,
2760a81c400eSJeff Roberson 		    VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
2761a81c400eSJeff Roberson 		vm_map_unlock(kernel_map);
2762a81c400eSJeff Roberson 	}
2763a81c400eSJeff Roberson 
2764a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC
2765a81c400eSJeff Roberson 	/* Set up radix zone to use noobj_alloc. */
2766a81c400eSJeff Roberson 	vm_radix_reserve_kva();
2767f7d35785SGleb Smirnoff #endif
2768a81c400eSJeff Roberson 
2769a81c400eSJeff Roberson 	booted = BOOT_KVA;
2770a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_kva_available, NULL);
2771f4bef67cSGleb Smirnoff 	bucket_enable();
27728355f576SJeff Roberson }
27738355f576SJeff Roberson 
2774a81c400eSJeff Roberson /*
2775a81c400eSJeff Roberson  * Finish our initialization steps.
2776a81c400eSJeff Roberson  */
27778355f576SJeff Roberson static void
27788355f576SJeff Roberson uma_startup3(void)
27798355f576SJeff Roberson {
27801431a748SGleb Smirnoff 
2781c5deaf04SGleb Smirnoff #ifdef INVARIANTS
2782c5deaf04SGleb Smirnoff 	TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor);
2783c5deaf04SGleb Smirnoff 	uma_dbg_cnt = counter_u64_alloc(M_WAITOK);
2784c5deaf04SGleb Smirnoff 	uma_skip_cnt = counter_u64_alloc(M_WAITOK);
2785c5deaf04SGleb Smirnoff #endif
2786a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_alloc_counters, NULL);
2787a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_alloc_sysctl, NULL);
2788fd90e2edSJung-uk Kim 	callout_init(&uma_callout, 1);
27899643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
2790c5deaf04SGleb Smirnoff 	booted = BOOT_RUNNING;
2791860bb7a0SMark Johnston 
2792860bb7a0SMark Johnston 	EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL,
2793860bb7a0SMark Johnston 	    EVENTHANDLER_PRI_FIRST);
2794860bb7a0SMark Johnston }
2795860bb7a0SMark Johnston 
2796860bb7a0SMark Johnston static void
2797860bb7a0SMark Johnston uma_shutdown(void)
2798860bb7a0SMark Johnston {
2799860bb7a0SMark Johnston 
2800860bb7a0SMark Johnston 	booted = BOOT_SHUTDOWN;
28018355f576SJeff Roberson }
28028355f576SJeff Roberson 
2803e20a199fSJeff Roberson static uma_keg_t
2804099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
280585dcf349SGleb Smirnoff 		int align, uint32_t flags)
2806099a0e58SBosko Milekic {
2807099a0e58SBosko Milekic 	struct uma_kctor_args args;
2808099a0e58SBosko Milekic 
2809099a0e58SBosko Milekic 	args.size = size;
2810099a0e58SBosko Milekic 	args.uminit = uminit;
2811099a0e58SBosko Milekic 	args.fini = fini;
28121e319f6dSRobert Watson 	args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
2813099a0e58SBosko Milekic 	args.flags = flags;
2814099a0e58SBosko Milekic 	args.zone = zone;
2815ab3185d1SJeff Roberson 	return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK));
2816099a0e58SBosko Milekic }
2817099a0e58SBosko Milekic 
2818f4bef67cSGleb Smirnoff /* Public functions */
28198355f576SJeff Roberson /* See uma.h */
28201e319f6dSRobert Watson void
28211e319f6dSRobert Watson uma_set_align(int align)
28221e319f6dSRobert Watson {
28231e319f6dSRobert Watson 
28241e319f6dSRobert Watson 	if (align != UMA_ALIGN_CACHE)
28251e319f6dSRobert Watson 		uma_align_cache = align;
28261e319f6dSRobert Watson }
28271e319f6dSRobert Watson 
28281e319f6dSRobert Watson /* See uma.h */
28298355f576SJeff Roberson uma_zone_t
2830bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
283185dcf349SGleb Smirnoff 		uma_init uminit, uma_fini fini, int align, uint32_t flags)
28328355f576SJeff Roberson 
28338355f576SJeff Roberson {
28348355f576SJeff Roberson 	struct uma_zctor_args args;
283595c4bf75SKonstantin Belousov 	uma_zone_t res;
28368355f576SJeff Roberson 
2837a5a35578SJohn Baldwin 	KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
2838a5a35578SJohn Baldwin 	    align, name));
2839a5a35578SJohn Baldwin 
28408355f576SJeff Roberson 	/* This stuff is essential for the zone ctor */
28410095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
28428355f576SJeff Roberson 	args.name = name;
28438355f576SJeff Roberson 	args.size = size;
28448355f576SJeff Roberson 	args.ctor = ctor;
28458355f576SJeff Roberson 	args.dtor = dtor;
28468355f576SJeff Roberson 	args.uminit = uminit;
28478355f576SJeff Roberson 	args.fini = fini;
2848afc6dc36SJohn-Mark Gurney #ifdef  INVARIANTS
2849afc6dc36SJohn-Mark Gurney 	/*
2850ca293436SRyan Libby 	 * Inject procedures which check for memory use after free if we are
2851ca293436SRyan Libby 	 * allowed to scramble the memory while it is not allocated.  This
2852ca293436SRyan Libby 	 * requires that: UMA is actually able to access the memory, no init
2853ca293436SRyan Libby 	 * or fini procedures, no dependency on the initial value of the
2854ca293436SRyan Libby 	 * memory, and no (legitimate) use of the memory after free.  Note,
2855ca293436SRyan Libby 	 * the ctor and dtor do not need to be empty.
2856afc6dc36SJohn-Mark Gurney 	 */
285754c5ae80SRyan Libby 	if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH |
285854c5ae80SRyan Libby 	    UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) {
2859afc6dc36SJohn-Mark Gurney 		args.uminit = trash_init;
2860afc6dc36SJohn-Mark Gurney 		args.fini = trash_fini;
2861afc6dc36SJohn-Mark Gurney 	}
2862afc6dc36SJohn-Mark Gurney #endif
28638355f576SJeff Roberson 	args.align = align;
28648355f576SJeff Roberson 	args.flags = flags;
2865099a0e58SBosko Milekic 	args.keg = NULL;
2866099a0e58SBosko Milekic 
286708cfa56eSMark Johnston 	sx_slock(&uma_reclaim_lock);
2868ab3185d1SJeff Roberson 	res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
286908cfa56eSMark Johnston 	sx_sunlock(&uma_reclaim_lock);
2870a81c400eSJeff Roberson 
287195c4bf75SKonstantin Belousov 	return (res);
2872099a0e58SBosko Milekic }
2873099a0e58SBosko Milekic 
2874099a0e58SBosko Milekic /* See uma.h */
2875099a0e58SBosko Milekic uma_zone_t
2876099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
2877099a0e58SBosko Milekic 		    uma_init zinit, uma_fini zfini, uma_zone_t master)
2878099a0e58SBosko Milekic {
2879099a0e58SBosko Milekic 	struct uma_zctor_args args;
2880e20a199fSJeff Roberson 	uma_keg_t keg;
288195c4bf75SKonstantin Belousov 	uma_zone_t res;
2882099a0e58SBosko Milekic 
2883bb15d1c7SGleb Smirnoff 	keg = master->uz_keg;
28840095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
2885099a0e58SBosko Milekic 	args.name = name;
2886e20a199fSJeff Roberson 	args.size = keg->uk_size;
2887099a0e58SBosko Milekic 	args.ctor = ctor;
2888099a0e58SBosko Milekic 	args.dtor = dtor;
2889099a0e58SBosko Milekic 	args.uminit = zinit;
2890099a0e58SBosko Milekic 	args.fini = zfini;
2891e20a199fSJeff Roberson 	args.align = keg->uk_align;
2892e20a199fSJeff Roberson 	args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
2893e20a199fSJeff Roberson 	args.keg = keg;
28948355f576SJeff Roberson 
289508cfa56eSMark Johnston 	sx_slock(&uma_reclaim_lock);
2896ab3185d1SJeff Roberson 	res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
289708cfa56eSMark Johnston 	sx_sunlock(&uma_reclaim_lock);
2898a81c400eSJeff Roberson 
289995c4bf75SKonstantin Belousov 	return (res);
29008355f576SJeff Roberson }
29018355f576SJeff Roberson 
29020095a784SJeff Roberson /* See uma.h */
29030095a784SJeff Roberson uma_zone_t
2904af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
2905af526374SJeff Roberson 		    uma_init zinit, uma_fini zfini, uma_import zimport,
2906af526374SJeff Roberson 		    uma_release zrelease, void *arg, int flags)
29070095a784SJeff Roberson {
29080095a784SJeff Roberson 	struct uma_zctor_args args;
29090095a784SJeff Roberson 
29100095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
29110095a784SJeff Roberson 	args.name = name;
2912af526374SJeff Roberson 	args.size = size;
29130095a784SJeff Roberson 	args.ctor = ctor;
29140095a784SJeff Roberson 	args.dtor = dtor;
29150095a784SJeff Roberson 	args.uminit = zinit;
29160095a784SJeff Roberson 	args.fini = zfini;
29170095a784SJeff Roberson 	args.import = zimport;
29180095a784SJeff Roberson 	args.release = zrelease;
29190095a784SJeff Roberson 	args.arg = arg;
29200095a784SJeff Roberson 	args.align = 0;
2921bb15d1c7SGleb Smirnoff 	args.flags = flags | UMA_ZFLAG_CACHE;
29220095a784SJeff Roberson 
2923ab3185d1SJeff Roberson 	return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK));
29240095a784SJeff Roberson }
29250095a784SJeff Roberson 
29268355f576SJeff Roberson /* See uma.h */
29279c2cd7e5SJeff Roberson void
29289c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone)
29299c2cd7e5SJeff Roberson {
2930f4ff923bSRobert Watson 
2931860bb7a0SMark Johnston 	/*
2932860bb7a0SMark Johnston 	 * Large slabs are expensive to reclaim, so don't bother doing
2933860bb7a0SMark Johnston 	 * unnecessary work if we're shutting down.
2934860bb7a0SMark Johnston 	 */
2935860bb7a0SMark Johnston 	if (booted == BOOT_SHUTDOWN &&
2936860bb7a0SMark Johnston 	    zone->uz_fini == NULL && zone->uz_release == zone_release)
2937860bb7a0SMark Johnston 		return;
293808cfa56eSMark Johnston 	sx_slock(&uma_reclaim_lock);
29390095a784SJeff Roberson 	zone_free_item(zones, zone, NULL, SKIP_NONE);
294008cfa56eSMark Johnston 	sx_sunlock(&uma_reclaim_lock);
29419c2cd7e5SJeff Roberson }
29429c2cd7e5SJeff Roberson 
29438d6fbbb8SJeff Roberson void
29448d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone)
29458d6fbbb8SJeff Roberson {
29468d6fbbb8SJeff Roberson 	void *item;
29478d6fbbb8SJeff Roberson 
29488d6fbbb8SJeff Roberson 	item = uma_zalloc_arg(zone, NULL, M_WAITOK);
29498d6fbbb8SJeff Roberson 	uma_zfree(zone, item);
29508d6fbbb8SJeff Roberson }
29518d6fbbb8SJeff Roberson 
29524e180881SMateusz Guzik void *
29534e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags)
29544e180881SMateusz Guzik {
29553acb6572SMateusz Guzik 	void *item, *pcpu_item;
2956b4799947SRuslan Bukin #ifdef SMP
29574e180881SMateusz Guzik 	int i;
29584e180881SMateusz Guzik 
29594e180881SMateusz Guzik 	MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2960b4799947SRuslan Bukin #endif
29614e180881SMateusz Guzik 	item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO);
29623acb6572SMateusz Guzik 	if (item == NULL)
29633acb6572SMateusz Guzik 		return (NULL);
29643acb6572SMateusz Guzik 	pcpu_item = zpcpu_base_to_offset(item);
29653acb6572SMateusz Guzik 	if (flags & M_ZERO) {
2966b4799947SRuslan Bukin #ifdef SMP
2967013072f0SMark Johnston 		for (i = 0; i <= mp_maxid; i++)
29683acb6572SMateusz Guzik 			bzero(zpcpu_get_cpu(pcpu_item, i), zone->uz_size);
2969b4799947SRuslan Bukin #else
2970b4799947SRuslan Bukin 		bzero(item, zone->uz_size);
2971b4799947SRuslan Bukin #endif
29724e180881SMateusz Guzik 	}
29733acb6572SMateusz Guzik 	return (pcpu_item);
29744e180881SMateusz Guzik }
29754e180881SMateusz Guzik 
29764e180881SMateusz Guzik /*
29774e180881SMateusz Guzik  * A stub while both regular and pcpu cases are identical.
29784e180881SMateusz Guzik  */
29794e180881SMateusz Guzik void
29803acb6572SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *pcpu_item, void *udata)
29814e180881SMateusz Guzik {
29823acb6572SMateusz Guzik 	void *item;
29834e180881SMateusz Guzik 
2984c5b7751fSIan Lepore #ifdef SMP
29854e180881SMateusz Guzik 	MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2986c5b7751fSIan Lepore #endif
29873acb6572SMateusz Guzik 	item = zpcpu_offset_to_base(pcpu_item);
29884e180881SMateusz Guzik 	uma_zfree_arg(zone, item, udata);
29894e180881SMateusz Guzik }
29904e180881SMateusz Guzik 
2991d4665eaaSJeff Roberson static inline void *
2992d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags,
2993d4665eaaSJeff Roberson     void *item)
2994beb8beefSJeff Roberson {
2995beb8beefSJeff Roberson #ifdef INVARIANTS
2996ca293436SRyan Libby 	bool skipdbg;
2997beb8beefSJeff Roberson 
2998beb8beefSJeff Roberson 	skipdbg = uma_dbg_zskip(zone, item);
2999ca293436SRyan Libby 	if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3000ca293436SRyan Libby 	    zone->uz_ctor != trash_ctor)
3001cc7ce83aSJeff Roberson 		trash_ctor(item, size, udata, flags);
3002beb8beefSJeff Roberson #endif
3003d4665eaaSJeff Roberson 	/* Check flags before loading ctor pointer. */
3004d4665eaaSJeff Roberson 	if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) &&
3005d4665eaaSJeff Roberson 	    __predict_false(zone->uz_ctor != NULL) &&
3006cc7ce83aSJeff Roberson 	    zone->uz_ctor(item, size, udata, flags) != 0) {
3007beb8beefSJeff Roberson 		counter_u64_add(zone->uz_fails, 1);
3008beb8beefSJeff Roberson 		zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT);
3009beb8beefSJeff Roberson 		return (NULL);
3010beb8beefSJeff Roberson 	}
3011beb8beefSJeff Roberson #ifdef INVARIANTS
3012beb8beefSJeff Roberson 	if (!skipdbg)
3013beb8beefSJeff Roberson 		uma_dbg_alloc(zone, NULL, item);
3014beb8beefSJeff Roberson #endif
3015beb8beefSJeff Roberson 	if (flags & M_ZERO)
3016cc7ce83aSJeff Roberson 		bzero(item, size);
3017beb8beefSJeff Roberson 
3018beb8beefSJeff Roberson 	return (item);
3019beb8beefSJeff Roberson }
3020beb8beefSJeff Roberson 
3021ca293436SRyan Libby static inline void
3022cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata,
3023cc7ce83aSJeff Roberson     enum zfreeskip skip)
3024ca293436SRyan Libby {
3025ca293436SRyan Libby #ifdef INVARIANTS
3026ca293436SRyan Libby 	bool skipdbg;
3027ca293436SRyan Libby 
3028ca293436SRyan Libby 	skipdbg = uma_dbg_zskip(zone, item);
3029ca293436SRyan Libby 	if (skip == SKIP_NONE && !skipdbg) {
3030ca293436SRyan Libby 		if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0)
3031ca293436SRyan Libby 			uma_dbg_free(zone, udata, item);
3032ca293436SRyan Libby 		else
3033ca293436SRyan Libby 			uma_dbg_free(zone, NULL, item);
3034ca293436SRyan Libby 	}
3035ca293436SRyan Libby #endif
3036cc7ce83aSJeff Roberson 	if (__predict_true(skip < SKIP_DTOR)) {
3037ca293436SRyan Libby 		if (zone->uz_dtor != NULL)
3038cc7ce83aSJeff Roberson 			zone->uz_dtor(item, size, udata);
3039ca293436SRyan Libby #ifdef INVARIANTS
3040ca293436SRyan Libby 		if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3041ca293436SRyan Libby 		    zone->uz_dtor != trash_dtor)
3042cc7ce83aSJeff Roberson 			trash_dtor(item, size, udata);
3043ca293436SRyan Libby #endif
3044ca293436SRyan Libby 	}
3045ca293436SRyan Libby }
3046ca293436SRyan Libby 
3047d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS)
3048d4665eaaSJeff Roberson #define	UMA_ZALLOC_DEBUG
3049d4665eaaSJeff Roberson static int
3050d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags)
3051d4665eaaSJeff Roberson {
3052d4665eaaSJeff Roberson 	int error;
3053d4665eaaSJeff Roberson 
3054d4665eaaSJeff Roberson 	error = 0;
3055d4665eaaSJeff Roberson #ifdef WITNESS
3056d4665eaaSJeff Roberson 	if (flags & M_WAITOK) {
3057d4665eaaSJeff Roberson 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
3058d4665eaaSJeff Roberson 		    "uma_zalloc_debug: zone \"%s\"", zone->uz_name);
3059d4665eaaSJeff Roberson 	}
3060d4665eaaSJeff Roberson #endif
3061d4665eaaSJeff Roberson 
3062d4665eaaSJeff Roberson #ifdef INVARIANTS
3063d4665eaaSJeff Roberson 	KASSERT((flags & M_EXEC) == 0,
3064d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: called with M_EXEC"));
3065d4665eaaSJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3066d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: called within spinlock or critical section"));
3067d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0,
3068d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO"));
3069d4665eaaSJeff Roberson #endif
3070d4665eaaSJeff Roberson 
3071d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD
30729e47b341SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) {
3073d4665eaaSJeff Roberson 		void *item;
3074d4665eaaSJeff Roberson 		item = memguard_alloc(zone->uz_size, flags);
3075d4665eaaSJeff Roberson 		if (item != NULL) {
3076d4665eaaSJeff Roberson 			error = EJUSTRETURN;
3077d4665eaaSJeff Roberson 			if (zone->uz_init != NULL &&
3078d4665eaaSJeff Roberson 			    zone->uz_init(item, zone->uz_size, flags) != 0) {
3079d4665eaaSJeff Roberson 				*itemp = NULL;
3080d4665eaaSJeff Roberson 				return (error);
3081d4665eaaSJeff Roberson 			}
3082d4665eaaSJeff Roberson 			if (zone->uz_ctor != NULL &&
3083d4665eaaSJeff Roberson 			    zone->uz_ctor(item, zone->uz_size, udata,
3084d4665eaaSJeff Roberson 			    flags) != 0) {
3085d4665eaaSJeff Roberson 				counter_u64_add(zone->uz_fails, 1);
3086d4665eaaSJeff Roberson 			    	zone->uz_fini(item, zone->uz_size);
3087d4665eaaSJeff Roberson 				*itemp = NULL;
3088d4665eaaSJeff Roberson 				return (error);
3089d4665eaaSJeff Roberson 			}
3090d4665eaaSJeff Roberson 			*itemp = item;
3091d4665eaaSJeff Roberson 			return (error);
3092d4665eaaSJeff Roberson 		}
3093d4665eaaSJeff Roberson 		/* This is unfortunate but should not be fatal. */
3094d4665eaaSJeff Roberson 	}
3095d4665eaaSJeff Roberson #endif
3096d4665eaaSJeff Roberson 	return (error);
3097d4665eaaSJeff Roberson }
3098d4665eaaSJeff Roberson 
3099d4665eaaSJeff Roberson static int
3100d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata)
3101d4665eaaSJeff Roberson {
3102d4665eaaSJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3103d4665eaaSJeff Roberson 	    ("uma_zfree_debug: called with spinlock or critical section held"));
3104d4665eaaSJeff Roberson 
3105d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD
31069e47b341SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) {
3107d4665eaaSJeff Roberson 		if (zone->uz_dtor != NULL)
3108d4665eaaSJeff Roberson 			zone->uz_dtor(item, zone->uz_size, udata);
3109d4665eaaSJeff Roberson 		if (zone->uz_fini != NULL)
3110d4665eaaSJeff Roberson 			zone->uz_fini(item, zone->uz_size);
3111d4665eaaSJeff Roberson 		memguard_free(item);
3112d4665eaaSJeff Roberson 		return (EJUSTRETURN);
3113d4665eaaSJeff Roberson 	}
3114d4665eaaSJeff Roberson #endif
3115d4665eaaSJeff Roberson 	return (0);
3116d4665eaaSJeff Roberson }
3117d4665eaaSJeff Roberson #endif
3118d4665eaaSJeff Roberson 
3119d4665eaaSJeff Roberson static __noinline void *
3120d4665eaaSJeff Roberson uma_zalloc_single(uma_zone_t zone, void *udata, int flags)
3121d4665eaaSJeff Roberson {
3122d4665eaaSJeff Roberson 	int domain;
3123d4665eaaSJeff Roberson 
3124d4665eaaSJeff Roberson 	/*
3125d4665eaaSJeff Roberson 	 * We can not get a bucket so try to return a single item.
3126d4665eaaSJeff Roberson 	 */
3127d4665eaaSJeff Roberson 	if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH)
3128d4665eaaSJeff Roberson 		domain = PCPU_GET(domain);
3129d4665eaaSJeff Roberson 	else
3130d4665eaaSJeff Roberson 		domain = UMA_ANYDOMAIN;
3131d4665eaaSJeff Roberson 	return (zone_alloc_item(zone, udata, domain, flags));
3132d4665eaaSJeff Roberson }
3133d4665eaaSJeff Roberson 
3134d4665eaaSJeff Roberson /* See uma.h */
3135d4665eaaSJeff Roberson void *
3136d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags)
3137d4665eaaSJeff Roberson {
3138d4665eaaSJeff Roberson 	uma_cache_bucket_t bucket;
3139d4665eaaSJeff Roberson 	uma_cache_t cache;
3140d4665eaaSJeff Roberson 	void *item;
3141d4665eaaSJeff Roberson 	int size, uz_flags;
3142d4665eaaSJeff Roberson 
3143d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
3144d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
3145d4665eaaSJeff Roberson 	    ("uma_zalloc_arg: called with non-SMR zone.\n"));
3146d4665eaaSJeff Roberson 	if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN)
3147d4665eaaSJeff Roberson 		return (item);
3148d4665eaaSJeff Roberson #endif
3149d4665eaaSJeff Roberson 
3150d4665eaaSJeff Roberson 	critical_enter();
3151d4665eaaSJeff Roberson 	do {
3152d4665eaaSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
3153d4665eaaSJeff Roberson 		bucket = &cache->uc_allocbucket;
3154d4665eaaSJeff Roberson 		size = cache_uz_size(cache);
3155d4665eaaSJeff Roberson 		uz_flags = cache_uz_flags(cache);
3156d4665eaaSJeff Roberson 		if (__predict_true(bucket->ucb_cnt != 0)) {
3157d4665eaaSJeff Roberson 			item = cache_bucket_pop(cache, bucket);
3158d4665eaaSJeff Roberson 			critical_exit();
3159d4665eaaSJeff Roberson 			return (item_ctor(zone, uz_flags, size, NULL, flags,
3160d4665eaaSJeff Roberson 			    item));
3161d4665eaaSJeff Roberson 		}
3162d4665eaaSJeff Roberson 	} while (cache_alloc(zone, cache, NULL, flags));
3163d4665eaaSJeff Roberson 	critical_exit();
3164d4665eaaSJeff Roberson 
3165d4665eaaSJeff Roberson 	return (uma_zalloc_single(zone, NULL, flags));
3166d4665eaaSJeff Roberson }
3167d4665eaaSJeff Roberson 
31689c2cd7e5SJeff Roberson /* See uma.h */
31698355f576SJeff Roberson void *
31702cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
31718355f576SJeff Roberson {
3172376b1ba3SJeff Roberson 	uma_cache_bucket_t bucket;
3173ab3185d1SJeff Roberson 	uma_cache_t cache;
3174ab3185d1SJeff Roberson 	void *item;
3175d4665eaaSJeff Roberson 	int size, uz_flags;
31768355f576SJeff Roberson 
3177e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
317819fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
317910cb2424SMark Murray 
31808355f576SJeff Roberson 	/* This is the fast path allocation */
3181e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name,
3182e63a1c2fSRyan Libby 	    zone, flags);
3183a553d4b8SJeff Roberson 
3184d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
3185d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
3186d4665eaaSJeff Roberson 	    ("uma_zalloc_arg: called with SMR zone.\n"));
3187d4665eaaSJeff Roberson 	if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN)
31888d689e04SGleb Smirnoff 		return (item);
31898d689e04SGleb Smirnoff #endif
3190d4665eaaSJeff Roberson 
31915d1ae027SRobert Watson 	/*
31925d1ae027SRobert Watson 	 * If possible, allocate from the per-CPU cache.  There are two
31935d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
31945d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
31955d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
31965d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
31975d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
31985d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to allocate from
31995d1ae027SRobert Watson 	 * the current cache; when we re-acquire the critical section, we
32005d1ae027SRobert Watson 	 * must detect and handle migration if it has occurred.
32015d1ae027SRobert Watson 	 */
32025d1ae027SRobert Watson 	critical_enter();
3203beb8beefSJeff Roberson 	do {
3204cc7ce83aSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
3205376b1ba3SJeff Roberson 		bucket = &cache->uc_allocbucket;
3206cc7ce83aSJeff Roberson 		size = cache_uz_size(cache);
3207cc7ce83aSJeff Roberson 		uz_flags = cache_uz_flags(cache);
3208376b1ba3SJeff Roberson 		if (__predict_true(bucket->ucb_cnt != 0)) {
3209376b1ba3SJeff Roberson 			item = cache_bucket_pop(cache, bucket);
32105d1ae027SRobert Watson 			critical_exit();
3211d4665eaaSJeff Roberson 			return (item_ctor(zone, uz_flags, size, udata, flags,
3212d4665eaaSJeff Roberson 			    item));
3213b23f72e9SBrian Feldman 		}
3214beb8beefSJeff Roberson 	} while (cache_alloc(zone, cache, udata, flags));
3215beb8beefSJeff Roberson 	critical_exit();
3216beb8beefSJeff Roberson 
3217d4665eaaSJeff Roberson 	return (uma_zalloc_single(zone, udata, flags));
3218fc03d22bSJeff Roberson }
3219fc03d22bSJeff Roberson 
32208355f576SJeff Roberson /*
3221beb8beefSJeff Roberson  * Replenish an alloc bucket and possibly restore an old one.  Called in
3222beb8beefSJeff Roberson  * a critical section.  Returns in a critical section.
3223beb8beefSJeff Roberson  *
32244bd61e19SJeff Roberson  * A false return value indicates an allocation failure.
32254bd61e19SJeff Roberson  * A true return value indicates success and the caller should retry.
3226beb8beefSJeff Roberson  */
3227beb8beefSJeff Roberson static __noinline bool
3228beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags)
3229beb8beefSJeff Roberson {
3230beb8beefSJeff Roberson 	uma_zone_domain_t zdom;
3231beb8beefSJeff Roberson 	uma_bucket_t bucket;
3232cc7ce83aSJeff Roberson 	int domain;
3233beb8beefSJeff Roberson 	bool lockfail;
3234beb8beefSJeff Roberson 
3235beb8beefSJeff Roberson 	CRITICAL_ASSERT(curthread);
3236beb8beefSJeff Roberson 
3237beb8beefSJeff Roberson 	/*
3238beb8beefSJeff Roberson 	 * If we have run out of items in our alloc bucket see
3239beb8beefSJeff Roberson 	 * if we can switch with the free bucket.
3240d4665eaaSJeff Roberson 	 *
3241d4665eaaSJeff Roberson 	 * SMR Zones can't re-use the free bucket until the sequence has
3242d4665eaaSJeff Roberson 	 * expired.
32438355f576SJeff Roberson 	 */
3244d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 &&
3245d4665eaaSJeff Roberson 	    cache->uc_freebucket.ucb_cnt != 0) {
3246d4665eaaSJeff Roberson 		cache_bucket_swap(&cache->uc_freebucket,
3247d4665eaaSJeff Roberson 		    &cache->uc_allocbucket);
3248beb8beefSJeff Roberson 		return (true);
32498355f576SJeff Roberson 	}
3250fc03d22bSJeff Roberson 
3251fc03d22bSJeff Roberson 	/*
3252fc03d22bSJeff Roberson 	 * Discard any empty allocation bucket while we hold no locks.
3253fc03d22bSJeff Roberson 	 */
3254376b1ba3SJeff Roberson 	bucket = cache_bucket_unload_alloc(cache);
3255fc03d22bSJeff Roberson 	critical_exit();
3256fc03d22bSJeff Roberson 	if (bucket != NULL)
32576fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
3258fc03d22bSJeff Roberson 
32594bd61e19SJeff Roberson 	/* Short-circuit for zones without buckets and low memory. */
32604bd61e19SJeff Roberson 	if (zone->uz_bucket_size == 0 || bucketdisable) {
32614bd61e19SJeff Roberson 		critical_enter();
32624bd61e19SJeff Roberson 		return (false);
32634bd61e19SJeff Roberson 	}
32644bd61e19SJeff Roberson 
32655d1ae027SRobert Watson 	/*
32665d1ae027SRobert Watson 	 * Attempt to retrieve the item from the per-CPU cache has failed, so
32675d1ae027SRobert Watson 	 * we must go back to the zone.  This requires the zone lock, so we
32685d1ae027SRobert Watson 	 * must drop the critical section, then re-acquire it when we go back
32695d1ae027SRobert Watson 	 * to the cache.  Since the critical section is released, we may be
32705d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
32715d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
32725d1ae027SRobert Watson 	 * the critical section.
32735d1ae027SRobert Watson 	 */
3274fc03d22bSJeff Roberson 	lockfail = 0;
3275fc03d22bSJeff Roberson 	if (ZONE_TRYLOCK(zone) == 0) {
3276fc03d22bSJeff Roberson 		/* Record contention to size the buckets. */
3277a553d4b8SJeff Roberson 		ZONE_LOCK(zone);
3278fc03d22bSJeff Roberson 		lockfail = 1;
3279fc03d22bSJeff Roberson 	}
3280beb8beefSJeff Roberson 
3281fc03d22bSJeff Roberson 	/* See if we lost the race to fill the cache. */
32824bd61e19SJeff Roberson 	critical_enter();
32834bd61e19SJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3284376b1ba3SJeff Roberson 	if (cache->uc_allocbucket.ucb_bucket != NULL) {
3285fc03d22bSJeff Roberson 		ZONE_UNLOCK(zone);
3286beb8beefSJeff Roberson 		return (true);
3287a553d4b8SJeff Roberson 	}
32888355f576SJeff Roberson 
3289fc03d22bSJeff Roberson 	/*
3290fc03d22bSJeff Roberson 	 * Check the zone's cache of buckets.
3291fc03d22bSJeff Roberson 	 */
3292dfe13344SJeff Roberson 	if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) {
3293c1685086SJeff Roberson 		domain = PCPU_GET(domain);
3294ab3185d1SJeff Roberson 		zdom = &zone->uz_domain[domain];
3295c1685086SJeff Roberson 	} else {
3296c1685086SJeff Roberson 		domain = UMA_ANYDOMAIN;
3297c1685086SJeff Roberson 		zdom = &zone->uz_domain[0];
3298c1685086SJeff Roberson 	}
3299c1685086SJeff Roberson 
330008cfa56eSMark Johnston 	if ((bucket = zone_fetch_bucket(zone, zdom)) != NULL) {
3301cae33c14SJeff Roberson 		KASSERT(bucket->ub_cnt != 0,
3302a553d4b8SJeff Roberson 		    ("uma_zalloc_arg: Returning an empty bucket."));
3303376b1ba3SJeff Roberson 		cache_bucket_load_alloc(cache, bucket);
3304beb8beefSJeff Roberson 		return (true);
3305a553d4b8SJeff Roberson 	}
33065d1ae027SRobert Watson 	/* We are no longer associated with this CPU. */
33075d1ae027SRobert Watson 	critical_exit();
3308bbee39c6SJeff Roberson 
3309fc03d22bSJeff Roberson 	/*
3310fc03d22bSJeff Roberson 	 * We bump the uz count when the cache size is insufficient to
3311fc03d22bSJeff Roberson 	 * handle the working set.
3312fc03d22bSJeff Roberson 	 */
331320a4e154SJeff Roberson 	if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max)
331420a4e154SJeff Roberson 		zone->uz_bucket_size++;
33154bd61e19SJeff Roberson 	ZONE_UNLOCK(zone);
3316bb15d1c7SGleb Smirnoff 
33178355f576SJeff Roberson 	/*
3318beb8beefSJeff Roberson 	 * Fill a bucket and attempt to use it as the alloc bucket.
3319bbee39c6SJeff Roberson 	 */
3320beb8beefSJeff Roberson 	bucket = zone_alloc_bucket(zone, udata, domain, flags);
33211431a748SGleb Smirnoff 	CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
33221431a748SGleb Smirnoff 	    zone->uz_name, zone, bucket);
33234bd61e19SJeff Roberson 	if (bucket == NULL) {
3324fc03d22bSJeff Roberson 		critical_enter();
3325beb8beefSJeff Roberson 		return (false);
33264bd61e19SJeff Roberson 	}
33270f9b7bf3SMark Johnston 
3328fc03d22bSJeff Roberson 	/*
3329fc03d22bSJeff Roberson 	 * See if we lost the race or were migrated.  Cache the
3330fc03d22bSJeff Roberson 	 * initialized bucket to make this less likely or claim
3331fc03d22bSJeff Roberson 	 * the memory directly.
3332fc03d22bSJeff Roberson 	 */
33334bd61e19SJeff Roberson 	ZONE_LOCK(zone);
33344bd61e19SJeff Roberson 	critical_enter();
3335cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3336376b1ba3SJeff Roberson 	if (cache->uc_allocbucket.ucb_bucket == NULL &&
3337dfe13344SJeff Roberson 	    ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0 ||
333881c0d72cSGleb Smirnoff 	    domain == PCPU_GET(domain))) {
3339376b1ba3SJeff Roberson 		cache_bucket_load_alloc(cache, bucket);
33400f9b7bf3SMark Johnston 		zdom->uzd_imax += bucket->ub_cnt;
3341bb15d1c7SGleb Smirnoff 	} else if (zone->uz_bkt_count >= zone->uz_bkt_max) {
334281c0d72cSGleb Smirnoff 		critical_exit();
334381c0d72cSGleb Smirnoff 		ZONE_UNLOCK(zone);
334481c0d72cSGleb Smirnoff 		bucket_drain(zone, bucket);
334581c0d72cSGleb Smirnoff 		bucket_free(zone, bucket, udata);
3346beb8beefSJeff Roberson 		critical_enter();
3347beb8beefSJeff Roberson 		return (true);
334881c0d72cSGleb Smirnoff 	} else
33490f9b7bf3SMark Johnston 		zone_put_bucket(zone, zdom, bucket, false);
3350bbee39c6SJeff Roberson 	ZONE_UNLOCK(zone);
3351beb8beefSJeff Roberson 	return (true);
3352bbee39c6SJeff Roberson }
3353bbee39c6SJeff Roberson 
3354ab3185d1SJeff Roberson void *
3355ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
3356bbee39c6SJeff Roberson {
3357ab3185d1SJeff Roberson 
3358ab3185d1SJeff Roberson 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
335919fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3360ab3185d1SJeff Roberson 
3361ab3185d1SJeff Roberson 	/* This is the fast path allocation */
3362e63a1c2fSRyan Libby 	CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d",
3363e63a1c2fSRyan Libby 	    zone->uz_name, zone, domain, flags);
3364ab3185d1SJeff Roberson 
3365ab3185d1SJeff Roberson 	if (flags & M_WAITOK) {
3366ab3185d1SJeff Roberson 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
3367ab3185d1SJeff Roberson 		    "uma_zalloc_domain: zone \"%s\"", zone->uz_name);
3368ab3185d1SJeff Roberson 	}
3369ab3185d1SJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3370ab3185d1SJeff Roberson 	    ("uma_zalloc_domain: called with spinlock or critical section held"));
3371ab3185d1SJeff Roberson 
3372ab3185d1SJeff Roberson 	return (zone_alloc_item(zone, udata, domain, flags));
3373ab3185d1SJeff Roberson }
3374ab3185d1SJeff Roberson 
3375ab3185d1SJeff Roberson /*
3376ab3185d1SJeff Roberson  * Find a slab with some space.  Prefer slabs that are partially used over those
3377ab3185d1SJeff Roberson  * that are totally full.  This helps to reduce fragmentation.
3378ab3185d1SJeff Roberson  *
3379ab3185d1SJeff Roberson  * If 'rr' is 1, search all domains starting from 'domain'.  Otherwise check
3380ab3185d1SJeff Roberson  * only 'domain'.
3381ab3185d1SJeff Roberson  */
3382ab3185d1SJeff Roberson static uma_slab_t
3383194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr)
3384ab3185d1SJeff Roberson {
3385ab3185d1SJeff Roberson 	uma_domain_t dom;
3386bbee39c6SJeff Roberson 	uma_slab_t slab;
3387ab3185d1SJeff Roberson 	int start;
3388ab3185d1SJeff Roberson 
3389ab3185d1SJeff Roberson 	KASSERT(domain >= 0 && domain < vm_ndomains,
3390ab3185d1SJeff Roberson 	    ("keg_first_slab: domain %d out of range", domain));
33918b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, domain);
3392ab3185d1SJeff Roberson 
3393ab3185d1SJeff Roberson 	slab = NULL;
3394ab3185d1SJeff Roberson 	start = domain;
3395ab3185d1SJeff Roberson 	do {
3396ab3185d1SJeff Roberson 		dom = &keg->uk_domain[domain];
33974ab3aee8SMark Johnston 		if ((slab = LIST_FIRST(&dom->ud_part_slab)) != NULL)
33984ab3aee8SMark Johnston 			return (slab);
33994ab3aee8SMark Johnston 		if ((slab = LIST_FIRST(&dom->ud_free_slab)) != NULL) {
3400ab3185d1SJeff Roberson 			LIST_REMOVE(slab, us_link);
34014ab3aee8SMark Johnston 			dom->ud_free_slabs--;
3402ab3185d1SJeff Roberson 			LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
3403ab3185d1SJeff Roberson 			return (slab);
3404ab3185d1SJeff Roberson 		}
3405ab3185d1SJeff Roberson 		if (rr)
3406ab3185d1SJeff Roberson 			domain = (domain + 1) % vm_ndomains;
3407ab3185d1SJeff Roberson 	} while (domain != start);
3408ab3185d1SJeff Roberson 
3409ab3185d1SJeff Roberson 	return (NULL);
3410ab3185d1SJeff Roberson }
3411ab3185d1SJeff Roberson 
34128b987a77SJeff Roberson /*
34138b987a77SJeff Roberson  * Fetch an existing slab from a free or partial list.  Returns with the
34148b987a77SJeff Roberson  * keg domain lock held if a slab was found or unlocked if not.
34158b987a77SJeff Roberson  */
3416ab3185d1SJeff Roberson static uma_slab_t
3417194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags)
3418ab3185d1SJeff Roberson {
34198b987a77SJeff Roberson 	uma_slab_t slab;
3420194a979eSMark Johnston 	uint32_t reserve;
3421099a0e58SBosko Milekic 
34228b987a77SJeff Roberson 	/* HASH has a single free list. */
342354c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0)
34248b987a77SJeff Roberson 		domain = 0;
3425194a979eSMark Johnston 
34268b987a77SJeff Roberson 	KEG_LOCK(keg, domain);
3427194a979eSMark Johnston 	reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve;
34284ab3aee8SMark Johnston 	if (keg->uk_domain[domain].ud_free_items <= reserve ||
34298b987a77SJeff Roberson 	    (slab = keg_first_slab(keg, domain, rr)) == NULL) {
34308b987a77SJeff Roberson 		KEG_UNLOCK(keg, domain);
3431194a979eSMark Johnston 		return (NULL);
34328b987a77SJeff Roberson 	}
34338b987a77SJeff Roberson 	return (slab);
3434194a979eSMark Johnston }
3435194a979eSMark Johnston 
3436194a979eSMark Johnston static uma_slab_t
3437194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags)
3438194a979eSMark Johnston {
3439194a979eSMark Johnston 	struct vm_domainset_iter di;
3440194a979eSMark Johnston 	uma_slab_t slab;
3441194a979eSMark Johnston 	int aflags, domain;
3442194a979eSMark Johnston 	bool rr;
3443194a979eSMark Johnston 
3444194a979eSMark Johnston restart:
3445bbee39c6SJeff Roberson 	/*
3446194a979eSMark Johnston 	 * Use the keg's policy if upper layers haven't already specified a
3447194a979eSMark Johnston 	 * domain (as happens with first-touch zones).
3448194a979eSMark Johnston 	 *
3449194a979eSMark Johnston 	 * To avoid races we run the iterator with the keg lock held, but that
3450194a979eSMark Johnston 	 * means that we cannot allow the vm_domainset layer to sleep.  Thus,
3451194a979eSMark Johnston 	 * clear M_WAITOK and handle low memory conditions locally.
3452bbee39c6SJeff Roberson 	 */
3453ab3185d1SJeff Roberson 	rr = rdomain == UMA_ANYDOMAIN;
3454ab3185d1SJeff Roberson 	if (rr) {
3455194a979eSMark Johnston 		aflags = (flags & ~M_WAITOK) | M_NOWAIT;
3456194a979eSMark Johnston 		vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
3457194a979eSMark Johnston 		    &aflags);
3458194a979eSMark Johnston 	} else {
3459194a979eSMark Johnston 		aflags = flags;
3460194a979eSMark Johnston 		domain = rdomain;
3461194a979eSMark Johnston 	}
3462ab3185d1SJeff Roberson 
3463194a979eSMark Johnston 	for (;;) {
3464194a979eSMark Johnston 		slab = keg_fetch_free_slab(keg, domain, rr, flags);
3465584061b4SJeff Roberson 		if (slab != NULL)
3466bbee39c6SJeff Roberson 			return (slab);
3467bbee39c6SJeff Roberson 
3468bbee39c6SJeff Roberson 		/*
3469bbee39c6SJeff Roberson 		 * M_NOVM means don't ask at all!
3470bbee39c6SJeff Roberson 		 */
3471bbee39c6SJeff Roberson 		if (flags & M_NOVM)
3472bbee39c6SJeff Roberson 			break;
3473bbee39c6SJeff Roberson 
347486220393SMark Johnston 		slab = keg_alloc_slab(keg, zone, domain, flags, aflags);
34758b987a77SJeff Roberson 		if (slab != NULL)
3476bbee39c6SJeff Roberson 			return (slab);
34773639ac42SJeff Roberson 		if (!rr && (flags & M_WAITOK) == 0)
34783639ac42SJeff Roberson 			break;
3479194a979eSMark Johnston 		if (rr && vm_domainset_iter_policy(&di, &domain) != 0) {
3480194a979eSMark Johnston 			if ((flags & M_WAITOK) != 0) {
3481194a979eSMark Johnston 				vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
3482194a979eSMark Johnston 				goto restart;
348330c5525bSAndrew Gallatin 			}
3484194a979eSMark Johnston 			break;
3485194a979eSMark Johnston 		}
3486ab3185d1SJeff Roberson 	}
3487ab3185d1SJeff Roberson 
3488bbee39c6SJeff Roberson 	/*
3489bbee39c6SJeff Roberson 	 * We might not have been able to get a slab but another cpu
3490bbee39c6SJeff Roberson 	 * could have while we were unlocked.  Check again before we
3491bbee39c6SJeff Roberson 	 * fail.
3492bbee39c6SJeff Roberson 	 */
34938b987a77SJeff Roberson 	if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL)
3494bbee39c6SJeff Roberson 		return (slab);
34958b987a77SJeff Roberson 
3496ab3185d1SJeff Roberson 	return (NULL);
3497ab3185d1SJeff Roberson }
3498bbee39c6SJeff Roberson 
3499d56368d7SBosko Milekic static void *
35000095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
3501bbee39c6SJeff Roberson {
3502ab3185d1SJeff Roberson 	uma_domain_t dom;
3503bbee39c6SJeff Roberson 	void *item;
35049b8db4d0SRyan Libby 	int freei;
3505bbee39c6SJeff Roberson 
35068b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, slab->us_domain);
3507099a0e58SBosko Milekic 
35088b987a77SJeff Roberson 	dom = &keg->uk_domain[slab->us_domain];
35099b78b1f4SJeff Roberson 	freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1;
35109b78b1f4SJeff Roberson 	BIT_CLR(keg->uk_ipers, freei, &slab->us_free);
35111e0701e1SJeff Roberson 	item = slab_item(slab, keg, freei);
3512bbee39c6SJeff Roberson 	slab->us_freecount--;
35134ab3aee8SMark Johnston 	dom->ud_free_items--;
3514ef72505eSJeff Roberson 
35154ab3aee8SMark Johnston 	/*
35164ab3aee8SMark Johnston 	 * Move this slab to the full list.  It must be on the partial list, so
35174ab3aee8SMark Johnston 	 * we do not need to update the free slab count.  In particular,
35184ab3aee8SMark Johnston 	 * keg_fetch_slab() always returns slabs on the partial list.
35194ab3aee8SMark Johnston 	 */
3520bbee39c6SJeff Roberson 	if (slab->us_freecount == 0) {
3521bbee39c6SJeff Roberson 		LIST_REMOVE(slab, us_link);
3522ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link);
3523bbee39c6SJeff Roberson 	}
3524bbee39c6SJeff Roberson 
3525bbee39c6SJeff Roberson 	return (item);
3526bbee39c6SJeff Roberson }
3527bbee39c6SJeff Roberson 
3528bbee39c6SJeff Roberson static int
3529b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags)
35300095a784SJeff Roberson {
35318b987a77SJeff Roberson 	uma_domain_t dom;
3532b75c4efcSAndrew Turner 	uma_zone_t zone;
35330095a784SJeff Roberson 	uma_slab_t slab;
35340095a784SJeff Roberson 	uma_keg_t keg;
3535a03af342SSean Bruno #ifdef NUMA
3536ab3185d1SJeff Roberson 	int stripe;
3537a03af342SSean Bruno #endif
35380095a784SJeff Roberson 	int i;
35390095a784SJeff Roberson 
3540b75c4efcSAndrew Turner 	zone = arg;
35410095a784SJeff Roberson 	slab = NULL;
3542584061b4SJeff Roberson 	keg = zone->uz_keg;
3543af526374SJeff Roberson 	/* Try to keep the buckets totally full */
35440095a784SJeff Roberson 	for (i = 0; i < max; ) {
3545584061b4SJeff Roberson 		if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL)
35460095a784SJeff Roberson 			break;
3547a03af342SSean Bruno #ifdef NUMA
3548ab3185d1SJeff Roberson 		stripe = howmany(max, vm_ndomains);
3549a03af342SSean Bruno #endif
35508b987a77SJeff Roberson 		dom = &keg->uk_domain[slab->us_domain];
35516fd34d6fSJeff Roberson 		while (slab->us_freecount && i < max) {
35520095a784SJeff Roberson 			bucket[i++] = slab_alloc_item(keg, slab);
35534ab3aee8SMark Johnston 			if (dom->ud_free_items <= keg->uk_reserve)
35546fd34d6fSJeff Roberson 				break;
3555b6715dabSJeff Roberson #ifdef NUMA
3556ab3185d1SJeff Roberson 			/*
3557ab3185d1SJeff Roberson 			 * If the zone is striped we pick a new slab for every
3558ab3185d1SJeff Roberson 			 * N allocations.  Eliminating this conditional will
3559ab3185d1SJeff Roberson 			 * instead pick a new domain for each bucket rather
3560ab3185d1SJeff Roberson 			 * than stripe within each bucket.  The current option
3561ab3185d1SJeff Roberson 			 * produces more fragmentation and requires more cpu
3562ab3185d1SJeff Roberson 			 * time but yields better distribution.
3563ab3185d1SJeff Roberson 			 */
3564dfe13344SJeff Roberson 			if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 &&
3565ab3185d1SJeff Roberson 			    vm_ndomains > 1 && --stripe == 0)
3566ab3185d1SJeff Roberson 				break;
3567ab3185d1SJeff Roberson #endif
35686fd34d6fSJeff Roberson 		}
35698b987a77SJeff Roberson 		KEG_UNLOCK(keg, slab->us_domain);
3570ab3185d1SJeff Roberson 		/* Don't block if we allocated any successfully. */
35710095a784SJeff Roberson 		flags &= ~M_WAITOK;
35720095a784SJeff Roberson 		flags |= M_NOWAIT;
35730095a784SJeff Roberson 	}
35740095a784SJeff Roberson 
35750095a784SJeff Roberson 	return i;
35760095a784SJeff Roberson }
35770095a784SJeff Roberson 
35784bd61e19SJeff Roberson static int
35794bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags)
35804bd61e19SJeff Roberson {
35814bd61e19SJeff Roberson 	uint64_t old, new, total, max;
35824bd61e19SJeff Roberson 
35834bd61e19SJeff Roberson 	/*
35844bd61e19SJeff Roberson 	 * The hard case.  We're going to sleep because there were existing
35854bd61e19SJeff Roberson 	 * sleepers or because we ran out of items.  This routine enforces
35864bd61e19SJeff Roberson 	 * fairness by keeping fifo order.
35874bd61e19SJeff Roberson 	 *
35884bd61e19SJeff Roberson 	 * First release our ill gotten gains and make some noise.
35894bd61e19SJeff Roberson 	 */
35904bd61e19SJeff Roberson 	for (;;) {
35914bd61e19SJeff Roberson 		zone_free_limit(zone, count);
35924bd61e19SJeff Roberson 		zone_log_warning(zone);
35934bd61e19SJeff Roberson 		zone_maxaction(zone);
35944bd61e19SJeff Roberson 		if (flags & M_NOWAIT)
35954bd61e19SJeff Roberson 			return (0);
35964bd61e19SJeff Roberson 
35974bd61e19SJeff Roberson 		/*
35984bd61e19SJeff Roberson 		 * We need to allocate an item or set ourself as a sleeper
35994bd61e19SJeff Roberson 		 * while the sleepq lock is held to avoid wakeup races.  This
36004bd61e19SJeff Roberson 		 * is essentially a home rolled semaphore.
36014bd61e19SJeff Roberson 		 */
36024bd61e19SJeff Roberson 		sleepq_lock(&zone->uz_max_items);
36034bd61e19SJeff Roberson 		old = zone->uz_items;
36044bd61e19SJeff Roberson 		do {
36054bd61e19SJeff Roberson 			MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX);
36064bd61e19SJeff Roberson 			/* Cache the max since we will evaluate twice. */
36074bd61e19SJeff Roberson 			max = zone->uz_max_items;
36084bd61e19SJeff Roberson 			if (UZ_ITEMS_SLEEPERS(old) != 0 ||
36094bd61e19SJeff Roberson 			    UZ_ITEMS_COUNT(old) >= max)
36104bd61e19SJeff Roberson 				new = old + UZ_ITEMS_SLEEPER;
36114bd61e19SJeff Roberson 			else
36124bd61e19SJeff Roberson 				new = old + MIN(count, max - old);
36134bd61e19SJeff Roberson 		} while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0);
36144bd61e19SJeff Roberson 
36154bd61e19SJeff Roberson 		/* We may have successfully allocated under the sleepq lock. */
36164bd61e19SJeff Roberson 		if (UZ_ITEMS_SLEEPERS(new) == 0) {
36174bd61e19SJeff Roberson 			sleepq_release(&zone->uz_max_items);
36184bd61e19SJeff Roberson 			return (new - old);
36194bd61e19SJeff Roberson 		}
36204bd61e19SJeff Roberson 
36214bd61e19SJeff Roberson 		/*
36224bd61e19SJeff Roberson 		 * This is in a different cacheline from uz_items so that we
36234bd61e19SJeff Roberson 		 * don't constantly invalidate the fastpath cacheline when we
36244bd61e19SJeff Roberson 		 * adjust item counts.  This could be limited to toggling on
36254bd61e19SJeff Roberson 		 * transitions.
36264bd61e19SJeff Roberson 		 */
36274bd61e19SJeff Roberson 		atomic_add_32(&zone->uz_sleepers, 1);
36284bd61e19SJeff Roberson 		atomic_add_64(&zone->uz_sleeps, 1);
36294bd61e19SJeff Roberson 
36304bd61e19SJeff Roberson 		/*
36314bd61e19SJeff Roberson 		 * We have added ourselves as a sleeper.  The sleepq lock
36324bd61e19SJeff Roberson 		 * protects us from wakeup races.  Sleep now and then retry.
36334bd61e19SJeff Roberson 		 */
36344bd61e19SJeff Roberson 		sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0);
36354bd61e19SJeff Roberson 		sleepq_wait(&zone->uz_max_items, PVM);
36364bd61e19SJeff Roberson 
36374bd61e19SJeff Roberson 		/*
36384bd61e19SJeff Roberson 		 * After wakeup, remove ourselves as a sleeper and try
36394bd61e19SJeff Roberson 		 * again.  We no longer have the sleepq lock for protection.
36404bd61e19SJeff Roberson 		 *
36414bd61e19SJeff Roberson 		 * Subract ourselves as a sleeper while attempting to add
36424bd61e19SJeff Roberson 		 * our count.
36434bd61e19SJeff Roberson 		 */
36444bd61e19SJeff Roberson 		atomic_subtract_32(&zone->uz_sleepers, 1);
36454bd61e19SJeff Roberson 		old = atomic_fetchadd_64(&zone->uz_items,
36464bd61e19SJeff Roberson 		    -(UZ_ITEMS_SLEEPER - count));
36474bd61e19SJeff Roberson 		/* We're no longer a sleeper. */
36484bd61e19SJeff Roberson 		old -= UZ_ITEMS_SLEEPER;
36494bd61e19SJeff Roberson 
36504bd61e19SJeff Roberson 		/*
36514bd61e19SJeff Roberson 		 * If we're still at the limit, restart.  Notably do not
36524bd61e19SJeff Roberson 		 * block on other sleepers.  Cache the max value to protect
36534bd61e19SJeff Roberson 		 * against changes via sysctl.
36544bd61e19SJeff Roberson 		 */
36554bd61e19SJeff Roberson 		total = UZ_ITEMS_COUNT(old);
36564bd61e19SJeff Roberson 		max = zone->uz_max_items;
36574bd61e19SJeff Roberson 		if (total >= max)
36584bd61e19SJeff Roberson 			continue;
36594bd61e19SJeff Roberson 		/* Truncate if necessary, otherwise wake other sleepers. */
36604bd61e19SJeff Roberson 		if (total + count > max) {
36614bd61e19SJeff Roberson 			zone_free_limit(zone, total + count - max);
36624bd61e19SJeff Roberson 			count = max - total;
36634bd61e19SJeff Roberson 		} else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0)
36644bd61e19SJeff Roberson 			wakeup_one(&zone->uz_max_items);
36654bd61e19SJeff Roberson 
36664bd61e19SJeff Roberson 		return (count);
36674bd61e19SJeff Roberson 	}
36684bd61e19SJeff Roberson }
36694bd61e19SJeff Roberson 
36704bd61e19SJeff Roberson /*
36714bd61e19SJeff Roberson  * Allocate 'count' items from our max_items limit.  Returns the number
36724bd61e19SJeff Roberson  * available.  If M_NOWAIT is not specified it will sleep until at least
36734bd61e19SJeff Roberson  * one item can be allocated.
36744bd61e19SJeff Roberson  */
36754bd61e19SJeff Roberson static int
36764bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags)
36774bd61e19SJeff Roberson {
36784bd61e19SJeff Roberson 	uint64_t old;
36794bd61e19SJeff Roberson 	uint64_t max;
36804bd61e19SJeff Roberson 
36814bd61e19SJeff Roberson 	max = zone->uz_max_items;
36824bd61e19SJeff Roberson 	MPASS(max > 0);
36834bd61e19SJeff Roberson 
36844bd61e19SJeff Roberson 	/*
36854bd61e19SJeff Roberson 	 * We expect normal allocations to succeed with a simple
36864bd61e19SJeff Roberson 	 * fetchadd.
36874bd61e19SJeff Roberson 	 */
36884bd61e19SJeff Roberson 	old = atomic_fetchadd_64(&zone->uz_items, count);
36894bd61e19SJeff Roberson 	if (__predict_true(old + count <= max))
36904bd61e19SJeff Roberson 		return (count);
36914bd61e19SJeff Roberson 
36924bd61e19SJeff Roberson 	/*
36934bd61e19SJeff Roberson 	 * If we had some items and no sleepers just return the
36944bd61e19SJeff Roberson 	 * truncated value.  We have to release the excess space
36954bd61e19SJeff Roberson 	 * though because that may wake sleepers who weren't woken
36964bd61e19SJeff Roberson 	 * because we were temporarily over the limit.
36974bd61e19SJeff Roberson 	 */
36984bd61e19SJeff Roberson 	if (old < max) {
36994bd61e19SJeff Roberson 		zone_free_limit(zone, (old + count) - max);
37004bd61e19SJeff Roberson 		return (max - old);
37014bd61e19SJeff Roberson 	}
37024bd61e19SJeff Roberson 	return (zone_alloc_limit_hard(zone, count, flags));
37034bd61e19SJeff Roberson }
37044bd61e19SJeff Roberson 
37054bd61e19SJeff Roberson /*
37064bd61e19SJeff Roberson  * Free a number of items back to the limit.
37074bd61e19SJeff Roberson  */
37084bd61e19SJeff Roberson static void
37094bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count)
37104bd61e19SJeff Roberson {
37114bd61e19SJeff Roberson 	uint64_t old;
37124bd61e19SJeff Roberson 
37134bd61e19SJeff Roberson 	MPASS(count > 0);
37144bd61e19SJeff Roberson 
37154bd61e19SJeff Roberson 	/*
37164bd61e19SJeff Roberson 	 * In the common case we either have no sleepers or
37174bd61e19SJeff Roberson 	 * are still over the limit and can just return.
37184bd61e19SJeff Roberson 	 */
37194bd61e19SJeff Roberson 	old = atomic_fetchadd_64(&zone->uz_items, -count);
37204bd61e19SJeff Roberson 	if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 ||
37214bd61e19SJeff Roberson 	   UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items))
37224bd61e19SJeff Roberson 		return;
37234bd61e19SJeff Roberson 
37244bd61e19SJeff Roberson 	/*
37254bd61e19SJeff Roberson 	 * Moderate the rate of wakeups.  Sleepers will continue
37264bd61e19SJeff Roberson 	 * to generate wakeups if necessary.
37274bd61e19SJeff Roberson 	 */
37284bd61e19SJeff Roberson 	wakeup_one(&zone->uz_max_items);
37294bd61e19SJeff Roberson }
37304bd61e19SJeff Roberson 
3731fc03d22bSJeff Roberson static uma_bucket_t
3732beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags)
3733bbee39c6SJeff Roberson {
3734bbee39c6SJeff Roberson 	uma_bucket_t bucket;
3735beb8beefSJeff Roberson 	int maxbucket, cnt;
3736bbee39c6SJeff Roberson 
3737e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name,
3738e63a1c2fSRyan Libby 	    zone, domain);
373930c5525bSAndrew Gallatin 
3740c1685086SJeff Roberson 	/* Avoid allocs targeting empty domains. */
3741c1685086SJeff Roberson 	if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
3742c1685086SJeff Roberson 		domain = UMA_ANYDOMAIN;
3743c1685086SJeff Roberson 
37444bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
37454bd61e19SJeff Roberson 		maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size,
37464bd61e19SJeff Roberson 		    M_NOWAIT);
37474bd61e19SJeff Roberson 	else
374820a4e154SJeff Roberson 		maxbucket = zone->uz_bucket_size;
37494bd61e19SJeff Roberson 	if (maxbucket == 0)
37504bd61e19SJeff Roberson 		return (false);
3751beb8beefSJeff Roberson 
37526fd34d6fSJeff Roberson 	/* Don't wait for buckets, preserve caller's NOVM setting. */
37536fd34d6fSJeff Roberson 	bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
3754beb8beefSJeff Roberson 	if (bucket == NULL) {
3755beb8beefSJeff Roberson 		cnt = 0;
3756beb8beefSJeff Roberson 		goto out;
3757beb8beefSJeff Roberson 	}
37580095a784SJeff Roberson 
37590095a784SJeff Roberson 	bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
3760beb8beefSJeff Roberson 	    MIN(maxbucket, bucket->ub_entries), domain, flags);
37610095a784SJeff Roberson 
37620095a784SJeff Roberson 	/*
37630095a784SJeff Roberson 	 * Initialize the memory if necessary.
37640095a784SJeff Roberson 	 */
37650095a784SJeff Roberson 	if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
3766099a0e58SBosko Milekic 		int i;
3767bbee39c6SJeff Roberson 
37680095a784SJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
3769e20a199fSJeff Roberson 			if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
37700095a784SJeff Roberson 			    flags) != 0)
3771b23f72e9SBrian Feldman 				break;
3772b23f72e9SBrian Feldman 		/*
3773b23f72e9SBrian Feldman 		 * If we couldn't initialize the whole bucket, put the
3774b23f72e9SBrian Feldman 		 * rest back onto the freelist.
3775b23f72e9SBrian Feldman 		 */
3776b23f72e9SBrian Feldman 		if (i != bucket->ub_cnt) {
3777af526374SJeff Roberson 			zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
37780095a784SJeff Roberson 			    bucket->ub_cnt - i);
3779a5a262c6SBosko Milekic #ifdef INVARIANTS
37800095a784SJeff Roberson 			bzero(&bucket->ub_bucket[i],
37810095a784SJeff Roberson 			    sizeof(void *) * (bucket->ub_cnt - i));
3782a5a262c6SBosko Milekic #endif
3783b23f72e9SBrian Feldman 			bucket->ub_cnt = i;
3784b23f72e9SBrian Feldman 		}
3785099a0e58SBosko Milekic 	}
3786099a0e58SBosko Milekic 
3787beb8beefSJeff Roberson 	cnt = bucket->ub_cnt;
3788f7104ccdSAlexander Motin 	if (bucket->ub_cnt == 0) {
37896fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
37902efcc8cbSGleb Smirnoff 		counter_u64_add(zone->uz_fails, 1);
3791beb8beefSJeff Roberson 		bucket = NULL;
3792beb8beefSJeff Roberson 	}
3793beb8beefSJeff Roberson out:
37944bd61e19SJeff Roberson 	if (zone->uz_max_items > 0 && cnt < maxbucket)
37954bd61e19SJeff Roberson 		zone_free_limit(zone, maxbucket - cnt);
3796fc03d22bSJeff Roberson 
3797fc03d22bSJeff Roberson 	return (bucket);
3798fc03d22bSJeff Roberson }
3799fc03d22bSJeff Roberson 
38008355f576SJeff Roberson /*
38010095a784SJeff Roberson  * Allocates a single item from a zone.
38028355f576SJeff Roberson  *
38038355f576SJeff Roberson  * Arguments
38048355f576SJeff Roberson  *	zone   The zone to alloc for.
38058355f576SJeff Roberson  *	udata  The data to be passed to the constructor.
3806ab3185d1SJeff Roberson  *	domain The domain to allocate from or UMA_ANYDOMAIN.
3807a163d034SWarner Losh  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
38088355f576SJeff Roberson  *
38098355f576SJeff Roberson  * Returns
38108355f576SJeff Roberson  *	NULL if there is no memory and M_NOWAIT is set
3811bbee39c6SJeff Roberson  *	An item if successful
38128355f576SJeff Roberson  */
38138355f576SJeff Roberson 
38148355f576SJeff Roberson static void *
3815ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags)
38168355f576SJeff Roberson {
38178355f576SJeff Roberson 	void *item;
38188355f576SJeff Roberson 
38194bd61e19SJeff Roberson 	if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0)
3820bb15d1c7SGleb Smirnoff 		return (NULL);
38218355f576SJeff Roberson 
3822c1685086SJeff Roberson 	/* Avoid allocs targeting empty domains. */
3823c1685086SJeff Roberson 	if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
382430c5525bSAndrew Gallatin 		domain = UMA_ANYDOMAIN;
3825c1685086SJeff Roberson 
3826ab3185d1SJeff Roberson 	if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1)
3827beb8beefSJeff Roberson 		goto fail_cnt;
38288355f576SJeff Roberson 
3829099a0e58SBosko Milekic 	/*
3830099a0e58SBosko Milekic 	 * We have to call both the zone's init (not the keg's init)
3831099a0e58SBosko Milekic 	 * and the zone's ctor.  This is because the item is going from
3832099a0e58SBosko Milekic 	 * a keg slab directly to the user, and the user is expecting it
3833099a0e58SBosko Milekic 	 * to be both zone-init'd as well as zone-ctor'd.
3834099a0e58SBosko Milekic 	 */
3835b23f72e9SBrian Feldman 	if (zone->uz_init != NULL) {
3836e20a199fSJeff Roberson 		if (zone->uz_init(item, zone->uz_size, flags) != 0) {
3837bb15d1c7SGleb Smirnoff 			zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT);
3838beb8beefSJeff Roberson 			goto fail_cnt;
3839beb8beefSJeff Roberson 		}
3840beb8beefSJeff Roberson 	}
3841d4665eaaSJeff Roberson 	item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags,
3842d4665eaaSJeff Roberson 	    item);
3843beb8beefSJeff Roberson 	if (item == NULL)
38440095a784SJeff Roberson 		goto fail;
38458355f576SJeff Roberson 
38462efcc8cbSGleb Smirnoff 	counter_u64_add(zone->uz_allocs, 1);
38471431a748SGleb Smirnoff 	CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
38481431a748SGleb Smirnoff 	    zone->uz_name, zone);
38491431a748SGleb Smirnoff 
38508355f576SJeff Roberson 	return (item);
38510095a784SJeff Roberson 
3852beb8beefSJeff Roberson fail_cnt:
3853beb8beefSJeff Roberson 	counter_u64_add(zone->uz_fails, 1);
38540095a784SJeff Roberson fail:
38554bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
38564bd61e19SJeff Roberson 		zone_free_limit(zone, 1);
38571431a748SGleb Smirnoff 	CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
38581431a748SGleb Smirnoff 	    zone->uz_name, zone);
38594bd61e19SJeff Roberson 
38600095a784SJeff Roberson 	return (NULL);
38618355f576SJeff Roberson }
38628355f576SJeff Roberson 
38638355f576SJeff Roberson /* See uma.h */
38648355f576SJeff Roberson void
3865d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item)
3866d4665eaaSJeff Roberson {
3867d4665eaaSJeff Roberson 	uma_cache_t cache;
3868d4665eaaSJeff Roberson 	uma_cache_bucket_t bucket;
3869d4665eaaSJeff Roberson 	int domain, itemdomain, uz_flags;
3870d4665eaaSJeff Roberson 
3871d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
3872d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
3873d4665eaaSJeff Roberson 	    ("uma_zfree_smr: called with non-SMR zone.\n"));
3874d4665eaaSJeff Roberson 	KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer."));
3875d4665eaaSJeff Roberson 	if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN)
3876d4665eaaSJeff Roberson 		return;
3877d4665eaaSJeff Roberson #endif
3878d4665eaaSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3879d4665eaaSJeff Roberson 	uz_flags = cache_uz_flags(cache);
3880d4665eaaSJeff Roberson 	domain = itemdomain = 0;
3881d4665eaaSJeff Roberson #ifdef NUMA
3882d4665eaaSJeff Roberson 	if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
3883d4665eaaSJeff Roberson 		itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item));
3884d4665eaaSJeff Roberson #endif
3885d4665eaaSJeff Roberson 	critical_enter();
3886d4665eaaSJeff Roberson 	do {
3887d4665eaaSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
3888d4665eaaSJeff Roberson 		/* SMR Zones must free to the free bucket. */
3889d4665eaaSJeff Roberson 		bucket = &cache->uc_freebucket;
3890d4665eaaSJeff Roberson #ifdef NUMA
3891d4665eaaSJeff Roberson 		domain = PCPU_GET(domain);
3892d4665eaaSJeff Roberson 		if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
3893d4665eaaSJeff Roberson 		    domain != itemdomain) {
3894d4665eaaSJeff Roberson 			bucket = &cache->uc_crossbucket;
3895d4665eaaSJeff Roberson 		}
3896d4665eaaSJeff Roberson #endif
3897d4665eaaSJeff Roberson 		if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
3898d4665eaaSJeff Roberson 			cache_bucket_push(cache, bucket, item);
3899d4665eaaSJeff Roberson 			critical_exit();
3900d4665eaaSJeff Roberson 			return;
3901d4665eaaSJeff Roberson 		}
3902d4665eaaSJeff Roberson 	} while (cache_free(zone, cache, NULL, item, itemdomain));
3903d4665eaaSJeff Roberson 	critical_exit();
3904d4665eaaSJeff Roberson 
3905d4665eaaSJeff Roberson 	/*
3906d4665eaaSJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
3907d4665eaaSJeff Roberson 	 */
3908d4665eaaSJeff Roberson 	zone_free_item(zone, item, NULL, SKIP_NONE);
3909d4665eaaSJeff Roberson }
3910d4665eaaSJeff Roberson 
3911d4665eaaSJeff Roberson /* See uma.h */
3912d4665eaaSJeff Roberson void
39138355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
39148355f576SJeff Roberson {
39158355f576SJeff Roberson 	uma_cache_t cache;
3916376b1ba3SJeff Roberson 	uma_cache_bucket_t bucket;
3917cc7ce83aSJeff Roberson 	int domain, itemdomain, uz_flags;
39188355f576SJeff Roberson 
3919e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
392019fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
392110cb2424SMark Murray 
3922e63a1c2fSRyan Libby 	CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone);
39233659f747SRobert Watson 
3924d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
3925d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
3926d4665eaaSJeff Roberson 	    ("uma_zfree_arg: called with SMR zone.\n"));
3927d4665eaaSJeff Roberson 	if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN)
3928d4665eaaSJeff Roberson 		return;
3929d4665eaaSJeff Roberson #endif
393020ed0cb0SMatthew D Fleming         /* uma_zfree(..., NULL) does nothing, to match free(9). */
393120ed0cb0SMatthew D Fleming         if (item == NULL)
393220ed0cb0SMatthew D Fleming                 return;
3933cc7ce83aSJeff Roberson 
3934cc7ce83aSJeff Roberson 	/*
3935cc7ce83aSJeff Roberson 	 * We are accessing the per-cpu cache without a critical section to
3936cc7ce83aSJeff Roberson 	 * fetch size and flags.  This is acceptable, if we are preempted we
3937cc7ce83aSJeff Roberson 	 * will simply read another cpu's line.
3938cc7ce83aSJeff Roberson 	 */
3939cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3940cc7ce83aSJeff Roberson 	uz_flags = cache_uz_flags(cache);
3941d4665eaaSJeff Roberson 	if (UMA_ALWAYS_CTORDTOR ||
3942d4665eaaSJeff Roberson 	    __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0))
3943cc7ce83aSJeff Roberson 		item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE);
3944ef72505eSJeff Roberson 
3945af7f9b97SJeff Roberson 	/*
3946af7f9b97SJeff Roberson 	 * The race here is acceptable.  If we miss it we'll just have to wait
3947af7f9b97SJeff Roberson 	 * a little longer for the limits to be reset.
3948af7f9b97SJeff Roberson 	 */
3949cc7ce83aSJeff Roberson 	if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) {
3950bb15d1c7SGleb Smirnoff 		if (zone->uz_sleepers > 0)
3951fc03d22bSJeff Roberson 			goto zfree_item;
3952cc7ce83aSJeff Roberson 	}
3953af7f9b97SJeff Roberson 
39545d1ae027SRobert Watson 	/*
39555d1ae027SRobert Watson 	 * If possible, free to the per-CPU cache.  There are two
39565d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
39575d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
39585d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
39595d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
39605d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
39615d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to free to the
39625d1ae027SRobert Watson 	 * current cache; when we re-acquire the critical section, we must
39635d1ae027SRobert Watson 	 * detect and handle migration if it has occurred.
39645d1ae027SRobert Watson 	 */
39650a81b439SJeff Roberson 	domain = itemdomain = 0;
3966dfe13344SJeff Roberson #ifdef NUMA
3967dfe13344SJeff Roberson 	if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
3968dfe13344SJeff Roberson 		itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item));
3969dfe13344SJeff Roberson #endif
39705d1ae027SRobert Watson 	critical_enter();
39710a81b439SJeff Roberson 	do {
3972cc7ce83aSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
3973a553d4b8SJeff Roberson 		/*
3974dfe13344SJeff Roberson 		 * Try to free into the allocbucket first to give LIFO
3975dfe13344SJeff Roberson 		 * ordering for cache-hot datastructures.  Spill over
3976dfe13344SJeff Roberson 		 * into the freebucket if necessary.  Alloc will swap
3977dfe13344SJeff Roberson 		 * them if one runs dry.
3978a553d4b8SJeff Roberson 		 */
3979dfe13344SJeff Roberson 		bucket = &cache->uc_allocbucket;
3980d4665eaaSJeff Roberson #ifdef NUMA
3981d4665eaaSJeff Roberson 		domain = PCPU_GET(domain);
3982d4665eaaSJeff Roberson 		if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
3983d4665eaaSJeff Roberson 		    domain != itemdomain) {
3984d4665eaaSJeff Roberson 			bucket = &cache->uc_crossbucket;
3985d4665eaaSJeff Roberson 		} else
3986d4665eaaSJeff Roberson #endif
3987d4665eaaSJeff Roberson 		if (bucket->ucb_cnt >= bucket->ucb_entries)
3988376b1ba3SJeff Roberson 			bucket = &cache->uc_freebucket;
3989376b1ba3SJeff Roberson 		if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
3990376b1ba3SJeff Roberson 			cache_bucket_push(cache, bucket, item);
39915d1ae027SRobert Watson 			critical_exit();
39928355f576SJeff Roberson 			return;
3993fc03d22bSJeff Roberson 		}
39940a81b439SJeff Roberson 	} while (cache_free(zone, cache, udata, item, itemdomain));
39950a81b439SJeff Roberson 	critical_exit();
3996fc03d22bSJeff Roberson 
39978355f576SJeff Roberson 	/*
39980a81b439SJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
39998355f576SJeff Roberson 	 */
40000a81b439SJeff Roberson zfree_item:
40010a81b439SJeff Roberson 	zone_free_item(zone, item, udata, SKIP_DTOR);
40020a81b439SJeff Roberson }
4003fc03d22bSJeff Roberson 
4004dfe13344SJeff Roberson #ifdef NUMA
400591d947bfSJeff Roberson /*
400691d947bfSJeff Roberson  * sort crossdomain free buckets to domain correct buckets and cache
400791d947bfSJeff Roberson  * them.
400891d947bfSJeff Roberson  */
400991d947bfSJeff Roberson static void
401091d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata)
401191d947bfSJeff Roberson {
401291d947bfSJeff Roberson 	struct uma_bucketlist fullbuckets;
401391d947bfSJeff Roberson 	uma_zone_domain_t zdom;
401491d947bfSJeff Roberson 	uma_bucket_t b;
4015*543117beSJeff Roberson 	smr_seq_t seq;
401691d947bfSJeff Roberson 	void *item;
401791d947bfSJeff Roberson 	int domain;
401891d947bfSJeff Roberson 
401991d947bfSJeff Roberson 	CTR3(KTR_UMA,
402091d947bfSJeff Roberson 	    "uma_zfree: zone %s(%p) draining cross bucket %p",
402191d947bfSJeff Roberson 	    zone->uz_name, zone, bucket);
402291d947bfSJeff Roberson 
4023dc3915c8SJeff Roberson 	STAILQ_INIT(&fullbuckets);
402491d947bfSJeff Roberson 
402591d947bfSJeff Roberson 	/*
402691d947bfSJeff Roberson 	 * To avoid having ndomain * ndomain buckets for sorting we have a
402791d947bfSJeff Roberson 	 * lock on the current crossfree bucket.  A full matrix with
402891d947bfSJeff Roberson 	 * per-domain locking could be used if necessary.
402991d947bfSJeff Roberson 	 */
403091d947bfSJeff Roberson 	ZONE_CROSS_LOCK(zone);
4031*543117beSJeff Roberson 
4032*543117beSJeff Roberson 	/*
4033*543117beSJeff Roberson 	 * It is possible for buckets to arrive here out of order so we fetch
4034*543117beSJeff Roberson 	 * the current smr seq rather than accepting the bucket's.
4035*543117beSJeff Roberson 	 */
4036*543117beSJeff Roberson 	seq = SMR_SEQ_INVALID;
4037*543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
4038*543117beSJeff Roberson 		seq = smr_current(zone->uz_smr);
403991d947bfSJeff Roberson 	while (bucket->ub_cnt > 0) {
404091d947bfSJeff Roberson 		item = bucket->ub_bucket[bucket->ub_cnt - 1];
404191d947bfSJeff Roberson 		domain = _vm_phys_domain(pmap_kextract((vm_offset_t)item));
404291d947bfSJeff Roberson 		zdom = &zone->uz_domain[domain];
404391d947bfSJeff Roberson 		if (zdom->uzd_cross == NULL) {
404491d947bfSJeff Roberson 			zdom->uzd_cross = bucket_alloc(zone, udata, M_NOWAIT);
404591d947bfSJeff Roberson 			if (zdom->uzd_cross == NULL)
404691d947bfSJeff Roberson 				break;
404791d947bfSJeff Roberson 		}
4048*543117beSJeff Roberson 		b = zdom->uzd_cross;
4049*543117beSJeff Roberson 		b->ub_bucket[b->ub_cnt++] = item;
4050*543117beSJeff Roberson 		b->ub_seq = seq;
4051*543117beSJeff Roberson 		if (b->ub_cnt == b->ub_entries) {
4052*543117beSJeff Roberson 			STAILQ_INSERT_HEAD(&fullbuckets, b, ub_link);
405391d947bfSJeff Roberson 			zdom->uzd_cross = NULL;
405491d947bfSJeff Roberson 		}
405591d947bfSJeff Roberson 		bucket->ub_cnt--;
405691d947bfSJeff Roberson 	}
405791d947bfSJeff Roberson 	ZONE_CROSS_UNLOCK(zone);
4058dc3915c8SJeff Roberson 	if (!STAILQ_EMPTY(&fullbuckets)) {
405991d947bfSJeff Roberson 		ZONE_LOCK(zone);
4060dc3915c8SJeff Roberson 		while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) {
4061dc3915c8SJeff Roberson 			STAILQ_REMOVE_HEAD(&fullbuckets, ub_link);
406291d947bfSJeff Roberson 			if (zone->uz_bkt_count >= zone->uz_bkt_max) {
406391d947bfSJeff Roberson 				ZONE_UNLOCK(zone);
406491d947bfSJeff Roberson 				bucket_drain(zone, b);
406591d947bfSJeff Roberson 				bucket_free(zone, b, udata);
406691d947bfSJeff Roberson 				ZONE_LOCK(zone);
406791d947bfSJeff Roberson 			} else {
406891d947bfSJeff Roberson 				domain = _vm_phys_domain(
406991d947bfSJeff Roberson 				    pmap_kextract(
407091d947bfSJeff Roberson 				    (vm_offset_t)b->ub_bucket[0]));
407191d947bfSJeff Roberson 				zdom = &zone->uz_domain[domain];
407291d947bfSJeff Roberson 				zone_put_bucket(zone, zdom, b, true);
407391d947bfSJeff Roberson 			}
407491d947bfSJeff Roberson 		}
407591d947bfSJeff Roberson 		ZONE_UNLOCK(zone);
407691d947bfSJeff Roberson 	}
407791d947bfSJeff Roberson 	if (bucket->ub_cnt != 0)
407891d947bfSJeff Roberson 		bucket_drain(zone, bucket);
4079d4665eaaSJeff Roberson 	bucket->ub_seq = SMR_SEQ_INVALID;
408091d947bfSJeff Roberson 	bucket_free(zone, bucket, udata);
408191d947bfSJeff Roberson }
408291d947bfSJeff Roberson #endif
408391d947bfSJeff Roberson 
40840a81b439SJeff Roberson static void
40850a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata,
40860a81b439SJeff Roberson     int domain, int itemdomain)
40870a81b439SJeff Roberson {
40880a81b439SJeff Roberson 	uma_zone_domain_t zdom;
40890a81b439SJeff Roberson 
4090dfe13344SJeff Roberson #ifdef NUMA
40910a81b439SJeff Roberson 	/*
40920a81b439SJeff Roberson 	 * Buckets coming from the wrong domain will be entirely for the
40930a81b439SJeff Roberson 	 * only other domain on two domain systems.  In this case we can
40940a81b439SJeff Roberson 	 * simply cache them.  Otherwise we need to sort them back to
409591d947bfSJeff Roberson 	 * correct domains.
40960a81b439SJeff Roberson 	 */
40970a81b439SJeff Roberson 	if (domain != itemdomain && vm_ndomains > 2) {
409891d947bfSJeff Roberson 		zone_free_cross(zone, bucket, udata);
40990a81b439SJeff Roberson 		return;
41000a81b439SJeff Roberson 	}
41010a81b439SJeff Roberson #endif
410291d947bfSJeff Roberson 
41030a81b439SJeff Roberson 	/*
41040a81b439SJeff Roberson 	 * Attempt to save the bucket in the zone's domain bucket cache.
41050a81b439SJeff Roberson 	 *
41060a81b439SJeff Roberson 	 * We bump the uz count when the cache size is insufficient to
41070a81b439SJeff Roberson 	 * handle the working set.
41080a81b439SJeff Roberson 	 */
41094d104ba0SAlexander Motin 	if (ZONE_TRYLOCK(zone) == 0) {
41104d104ba0SAlexander Motin 		/* Record contention to size the buckets. */
41118355f576SJeff Roberson 		ZONE_LOCK(zone);
411220a4e154SJeff Roberson 		if (zone->uz_bucket_size < zone->uz_bucket_size_max)
411320a4e154SJeff Roberson 			zone->uz_bucket_size++;
41144d104ba0SAlexander Motin 	}
41158355f576SJeff Roberson 
41160a81b439SJeff Roberson 	CTR3(KTR_UMA,
41170a81b439SJeff Roberson 	    "uma_zfree: zone %s(%p) putting bucket %p on free list",
41180a81b439SJeff Roberson 	    zone->uz_name, zone, bucket);
41190a81b439SJeff Roberson 	/* ub_cnt is pointing to the last free item */
41200a81b439SJeff Roberson 	KASSERT(bucket->ub_cnt == bucket->ub_entries,
41210a81b439SJeff Roberson 	    ("uma_zfree: Attempting to insert partial  bucket onto the full list.\n"));
41220a81b439SJeff Roberson 	if (zone->uz_bkt_count >= zone->uz_bkt_max) {
4123c1685086SJeff Roberson 		ZONE_UNLOCK(zone);
4124c1685086SJeff Roberson 		bucket_drain(zone, bucket);
4125c1685086SJeff Roberson 		bucket_free(zone, bucket, udata);
4126c1685086SJeff Roberson 	} else {
4127c1685086SJeff Roberson 		zdom = &zone->uz_domain[itemdomain];
4128c1685086SJeff Roberson 		zone_put_bucket(zone, zdom, bucket, true);
4129c1685086SJeff Roberson 		ZONE_UNLOCK(zone);
4130c1685086SJeff Roberson 	}
41318355f576SJeff Roberson }
4132fc03d22bSJeff Roberson 
41334d104ba0SAlexander Motin /*
41340a81b439SJeff Roberson  * Populate a free or cross bucket for the current cpu cache.  Free any
41350a81b439SJeff Roberson  * existing full bucket either to the zone cache or back to the slab layer.
41360a81b439SJeff Roberson  *
41370a81b439SJeff Roberson  * Enters and returns in a critical section.  false return indicates that
41380a81b439SJeff Roberson  * we can not satisfy this free in the cache layer.  true indicates that
41390a81b439SJeff Roberson  * the caller should retry.
41404d104ba0SAlexander Motin  */
41410a81b439SJeff Roberson static __noinline bool
41420a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item,
41430a81b439SJeff Roberson     int itemdomain)
41440a81b439SJeff Roberson {
4145dfe13344SJeff Roberson 	uma_cache_bucket_t cbucket;
4146d4665eaaSJeff Roberson 	uma_bucket_t newbucket, bucket;
4147cc7ce83aSJeff Roberson 	int domain;
41480a81b439SJeff Roberson 
41490a81b439SJeff Roberson 	CRITICAL_ASSERT(curthread);
41500a81b439SJeff Roberson 
4151d4665eaaSJeff Roberson 	if (zone->uz_bucket_size == 0)
41520a81b439SJeff Roberson 		return false;
41530a81b439SJeff Roberson 
4154cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4155d4665eaaSJeff Roberson 	newbucket = NULL;
41560a81b439SJeff Roberson 
41570a81b439SJeff Roberson 	/*
4158dfe13344SJeff Roberson 	 * FIRSTTOUCH domains need to free to the correct zdom.  When
4159dfe13344SJeff Roberson 	 * enabled this is the zdom of the item.   The bucket is the
4160dfe13344SJeff Roberson 	 * cross bucket if the current domain and itemdomain do not match.
41610a81b439SJeff Roberson 	 */
4162dfe13344SJeff Roberson 	cbucket = &cache->uc_freebucket;
4163dfe13344SJeff Roberson #ifdef NUMA
4164dfe13344SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) {
41650a81b439SJeff Roberson 		domain = PCPU_GET(domain);
41660a81b439SJeff Roberson 		if (domain != itemdomain) {
4167dfe13344SJeff Roberson 			cbucket = &cache->uc_crossbucket;
4168dfe13344SJeff Roberson 			if (cbucket->ucb_cnt != 0)
4169dfe13344SJeff Roberson 				atomic_add_64(&zone->uz_xdomain,
4170dfe13344SJeff Roberson 				    cbucket->ucb_cnt);
4171dfe13344SJeff Roberson 		}
41720a81b439SJeff Roberson 	} else
41730a81b439SJeff Roberson #endif
4174dfe13344SJeff Roberson 		itemdomain = domain = 0;
4175dfe13344SJeff Roberson 	bucket = cache_bucket_unload(cbucket);
41760a81b439SJeff Roberson 
41770a81b439SJeff Roberson 	/* We are no longer associated with this CPU. */
41780a81b439SJeff Roberson 	critical_exit();
41790a81b439SJeff Roberson 
4180d4665eaaSJeff Roberson 	/*
4181d4665eaaSJeff Roberson 	 * Don't let SMR zones operate without a free bucket.  Force
4182d4665eaaSJeff Roberson 	 * a synchronize and re-use this one.  We will only degrade
4183d4665eaaSJeff Roberson 	 * to a synchronize every bucket_size items rather than every
4184d4665eaaSJeff Roberson 	 * item if we fail to allocate a bucket.
4185d4665eaaSJeff Roberson 	 */
4186d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0) {
4187d4665eaaSJeff Roberson 		if (bucket != NULL)
4188d4665eaaSJeff Roberson 			bucket->ub_seq = smr_advance(zone->uz_smr);
4189d4665eaaSJeff Roberson 		newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4190d4665eaaSJeff Roberson 		if (newbucket == NULL && bucket != NULL) {
4191d4665eaaSJeff Roberson 			bucket_drain(zone, bucket);
4192d4665eaaSJeff Roberson 			newbucket = bucket;
4193d4665eaaSJeff Roberson 			bucket = NULL;
4194d4665eaaSJeff Roberson 		}
4195d4665eaaSJeff Roberson 	} else if (!bucketdisable)
4196d4665eaaSJeff Roberson 		newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4197d4665eaaSJeff Roberson 
41980a81b439SJeff Roberson 	if (bucket != NULL)
41990a81b439SJeff Roberson 		zone_free_bucket(zone, bucket, udata, domain, itemdomain);
4200a553d4b8SJeff Roberson 
4201fc03d22bSJeff Roberson 	critical_enter();
4202d4665eaaSJeff Roberson 	if ((bucket = newbucket) == NULL)
42030a81b439SJeff Roberson 		return (false);
4204cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4205dfe13344SJeff Roberson #ifdef NUMA
4206fc03d22bSJeff Roberson 	/*
42070a81b439SJeff Roberson 	 * Check to see if we should be populating the cross bucket.  If it
42080a81b439SJeff Roberson 	 * is already populated we will fall through and attempt to populate
42090a81b439SJeff Roberson 	 * the free bucket.
4210fc03d22bSJeff Roberson 	 */
4211dfe13344SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) {
42120a81b439SJeff Roberson 		domain = PCPU_GET(domain);
4213376b1ba3SJeff Roberson 		if (domain != itemdomain &&
4214376b1ba3SJeff Roberson 		    cache->uc_crossbucket.ucb_bucket == NULL) {
4215376b1ba3SJeff Roberson 			cache_bucket_load_cross(cache, bucket);
42160a81b439SJeff Roberson 			return (true);
42170a81b439SJeff Roberson 		}
42180a81b439SJeff Roberson 	}
42190a81b439SJeff Roberson #endif
42200a81b439SJeff Roberson 	/*
42210a81b439SJeff Roberson 	 * We may have lost the race to fill the bucket or switched CPUs.
42220a81b439SJeff Roberson 	 */
4223376b1ba3SJeff Roberson 	if (cache->uc_freebucket.ucb_bucket != NULL) {
4224fc03d22bSJeff Roberson 		critical_exit();
42256fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
42260a81b439SJeff Roberson 		critical_enter();
42270a81b439SJeff Roberson 	} else
4228376b1ba3SJeff Roberson 		cache_bucket_load_free(cache, bucket);
42298355f576SJeff Roberson 
42300a81b439SJeff Roberson 	return (true);
42318355f576SJeff Roberson }
42328355f576SJeff Roberson 
4233ab3185d1SJeff Roberson void
4234ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata)
4235ab3185d1SJeff Roberson {
4236ab3185d1SJeff Roberson 
4237ab3185d1SJeff Roberson 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
423819fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
4239ab3185d1SJeff Roberson 
4240e63a1c2fSRyan Libby 	CTR2(KTR_UMA, "uma_zfree_domain zone %s(%p)", zone->uz_name, zone);
4241ab3185d1SJeff Roberson 
4242ab3185d1SJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
4243ab3185d1SJeff Roberson 	    ("uma_zfree_domain: called with spinlock or critical section held"));
4244ab3185d1SJeff Roberson 
4245ab3185d1SJeff Roberson         /* uma_zfree(..., NULL) does nothing, to match free(9). */
4246ab3185d1SJeff Roberson         if (item == NULL)
4247ab3185d1SJeff Roberson                 return;
4248ab3185d1SJeff Roberson 	zone_free_item(zone, item, udata, SKIP_NONE);
4249ab3185d1SJeff Roberson }
4250ab3185d1SJeff Roberson 
42518355f576SJeff Roberson static void
4252bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item)
42538355f576SJeff Roberson {
4254bb15d1c7SGleb Smirnoff 	uma_keg_t keg;
4255ab3185d1SJeff Roberson 	uma_domain_t dom;
42569b8db4d0SRyan Libby 	int freei;
4257099a0e58SBosko Milekic 
4258bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
42598b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, slab->us_domain);
4260ab3185d1SJeff Roberson 
42618355f576SJeff Roberson 	/* Do we need to remove from any lists? */
42628b987a77SJeff Roberson 	dom = &keg->uk_domain[slab->us_domain];
4263099a0e58SBosko Milekic 	if (slab->us_freecount + 1 == keg->uk_ipers) {
42648355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
4265ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
42664ab3aee8SMark Johnston 		dom->ud_free_slabs++;
42678355f576SJeff Roberson 	} else if (slab->us_freecount == 0) {
42688355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
4269ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
42708355f576SJeff Roberson 	}
42718355f576SJeff Roberson 
4272ef72505eSJeff Roberson 	/* Slab management. */
42731e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
42749b78b1f4SJeff Roberson 	BIT_SET(keg->uk_ipers, freei, &slab->us_free);
42758355f576SJeff Roberson 	slab->us_freecount++;
42768355f576SJeff Roberson 
4277ef72505eSJeff Roberson 	/* Keg statistics. */
42784ab3aee8SMark Johnston 	dom->ud_free_items++;
42790095a784SJeff Roberson }
42800095a784SJeff Roberson 
42810095a784SJeff Roberson static void
4282b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt)
42830095a784SJeff Roberson {
42848b987a77SJeff Roberson 	struct mtx *lock;
4285b75c4efcSAndrew Turner 	uma_zone_t zone;
42860095a784SJeff Roberson 	uma_slab_t slab;
42870095a784SJeff Roberson 	uma_keg_t keg;
42880095a784SJeff Roberson 	uint8_t *mem;
42898b987a77SJeff Roberson 	void *item;
42900095a784SJeff Roberson 	int i;
42918355f576SJeff Roberson 
4292b75c4efcSAndrew Turner 	zone = arg;
4293bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
42948b987a77SJeff Roberson 	lock = NULL;
429554c5ae80SRyan Libby 	if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0))
42968b987a77SJeff Roberson 		lock = KEG_LOCK(keg, 0);
42970095a784SJeff Roberson 	for (i = 0; i < cnt; i++) {
42980095a784SJeff Roberson 		item = bucket[i];
429954c5ae80SRyan Libby 		if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) {
43000095a784SJeff Roberson 			slab = vtoslab((vm_offset_t)item);
43018b987a77SJeff Roberson 		} else {
43028b987a77SJeff Roberson 			mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
430354c5ae80SRyan Libby 			if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0)
43048b987a77SJeff Roberson 				slab = hash_sfind(&keg->uk_hash, mem);
43058b987a77SJeff Roberson 			else
43068b987a77SJeff Roberson 				slab = (uma_slab_t)(mem + keg->uk_pgoff);
43078b987a77SJeff Roberson 		}
43088b987a77SJeff Roberson 		if (lock != KEG_LOCKPTR(keg, slab->us_domain)) {
43098b987a77SJeff Roberson 			if (lock != NULL)
43108b987a77SJeff Roberson 				mtx_unlock(lock);
43118b987a77SJeff Roberson 			lock = KEG_LOCK(keg, slab->us_domain);
43128b987a77SJeff Roberson 		}
4313bb15d1c7SGleb Smirnoff 		slab_free_item(zone, slab, item);
43140095a784SJeff Roberson 	}
43158b987a77SJeff Roberson 	if (lock != NULL)
43168b987a77SJeff Roberson 		mtx_unlock(lock);
43178355f576SJeff Roberson }
43188355f576SJeff Roberson 
43190095a784SJeff Roberson /*
43200095a784SJeff Roberson  * Frees a single item to any zone.
43210095a784SJeff Roberson  *
43220095a784SJeff Roberson  * Arguments:
43230095a784SJeff Roberson  *	zone   The zone to free to
43240095a784SJeff Roberson  *	item   The item we're freeing
43250095a784SJeff Roberson  *	udata  User supplied data for the dtor
43260095a784SJeff Roberson  *	skip   Skip dtors and finis
43270095a784SJeff Roberson  */
43280095a784SJeff Roberson static void
43290095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
43300095a784SJeff Roberson {
4331c5deaf04SGleb Smirnoff 
4332d4665eaaSJeff Roberson 	/*
4333d4665eaaSJeff Roberson 	 * If a free is sent directly to an SMR zone we have to
4334d4665eaaSJeff Roberson 	 * synchronize immediately because the item can instantly
4335d4665eaaSJeff Roberson 	 * be reallocated. This should only happen in degenerate
4336d4665eaaSJeff Roberson 	 * cases when no memory is available for per-cpu caches.
4337d4665eaaSJeff Roberson 	 */
4338d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE)
4339d4665eaaSJeff Roberson 		smr_synchronize(zone->uz_smr);
4340d4665eaaSJeff Roberson 
4341cc7ce83aSJeff Roberson 	item_dtor(zone, item, zone->uz_size, udata, skip);
43420095a784SJeff Roberson 
43430095a784SJeff Roberson 	if (skip < SKIP_FINI && zone->uz_fini)
43440095a784SJeff Roberson 		zone->uz_fini(item, zone->uz_size);
43450095a784SJeff Roberson 
43460095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, &item, 1);
4347bb15d1c7SGleb Smirnoff 
4348bb15d1c7SGleb Smirnoff 	if (skip & SKIP_CNT)
4349bb15d1c7SGleb Smirnoff 		return;
4350bb15d1c7SGleb Smirnoff 
43512efcc8cbSGleb Smirnoff 	counter_u64_add(zone->uz_frees, 1);
43522efcc8cbSGleb Smirnoff 
43534bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
43544bd61e19SJeff Roberson 		zone_free_limit(zone, 1);
4355bb45b411SGleb Smirnoff }
43560095a784SJeff Roberson 
43578355f576SJeff Roberson /* See uma.h */
43581c6cae97SLawrence Stewart int
4359736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems)
4360736ee590SJeff Roberson {
4361bb15d1c7SGleb Smirnoff 	struct uma_bucket_zone *ubz;
4362003cf08bSMark Johnston 	int count;
4363bb15d1c7SGleb Smirnoff 
43644bd61e19SJeff Roberson 	/*
43654bd61e19SJeff Roberson 	 * XXX This can misbehave if the zone has any allocations with
43664bd61e19SJeff Roberson 	 * no limit and a limit is imposed.  There is currently no
43674bd61e19SJeff Roberson 	 * way to clear a limit.
43684bd61e19SJeff Roberson 	 */
4369bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4370003cf08bSMark Johnston 	ubz = bucket_zone_max(zone, nitems);
4371003cf08bSMark Johnston 	count = ubz != NULL ? ubz->ubz_entries : 0;
437220a4e154SJeff Roberson 	zone->uz_bucket_size_max = zone->uz_bucket_size = count;
437320a4e154SJeff Roberson 	if (zone->uz_bucket_size_min > zone->uz_bucket_size_max)
437420a4e154SJeff Roberson 		zone->uz_bucket_size_min = zone->uz_bucket_size_max;
4375bb15d1c7SGleb Smirnoff 	zone->uz_max_items = nitems;
4376cc7ce83aSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_LIMIT;
4377cc7ce83aSJeff Roberson 	zone_update_caches(zone);
43784bd61e19SJeff Roberson 	/* We may need to wake waiters. */
43794bd61e19SJeff Roberson 	wakeup(&zone->uz_max_items);
4380bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4381bb15d1c7SGleb Smirnoff 
4382bb15d1c7SGleb Smirnoff 	return (nitems);
4383bb15d1c7SGleb Smirnoff }
4384bb15d1c7SGleb Smirnoff 
4385bb15d1c7SGleb Smirnoff /* See uma.h */
4386003cf08bSMark Johnston void
4387bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems)
4388bb15d1c7SGleb Smirnoff {
4389003cf08bSMark Johnston 	struct uma_bucket_zone *ubz;
4390003cf08bSMark Johnston 	int bpcpu;
4391bb15d1c7SGleb Smirnoff 
4392bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4393003cf08bSMark Johnston 	ubz = bucket_zone_max(zone, nitems);
4394003cf08bSMark Johnston 	if (ubz != NULL) {
4395003cf08bSMark Johnston 		bpcpu = 2;
4396dfe13344SJeff Roberson 		if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
4397003cf08bSMark Johnston 			/* Count the cross-domain bucket. */
4398003cf08bSMark Johnston 			bpcpu++;
4399003cf08bSMark Johnston 		nitems -= ubz->ubz_entries * bpcpu * mp_ncpus;
440020a4e154SJeff Roberson 		zone->uz_bucket_size_max = ubz->ubz_entries;
4401003cf08bSMark Johnston 	} else {
440220a4e154SJeff Roberson 		zone->uz_bucket_size_max = zone->uz_bucket_size = 0;
4403003cf08bSMark Johnston 	}
440420a4e154SJeff Roberson 	if (zone->uz_bucket_size_min > zone->uz_bucket_size_max)
440520a4e154SJeff Roberson 		zone->uz_bucket_size_min = zone->uz_bucket_size_max;
4406bb15d1c7SGleb Smirnoff 	zone->uz_bkt_max = nitems;
4407bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4408736ee590SJeff Roberson }
4409736ee590SJeff Roberson 
4410736ee590SJeff Roberson /* See uma.h */
4411e49471b0SAndre Oppermann int
4412e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone)
4413e49471b0SAndre Oppermann {
4414e49471b0SAndre Oppermann 	int nitems;
4415e49471b0SAndre Oppermann 
4416727c6918SJeff Roberson 	nitems = atomic_load_64(&zone->uz_max_items);
4417e49471b0SAndre Oppermann 
4418e49471b0SAndre Oppermann 	return (nitems);
4419e49471b0SAndre Oppermann }
4420e49471b0SAndre Oppermann 
4421e49471b0SAndre Oppermann /* See uma.h */
44222f891cd5SPawel Jakub Dawidek void
44232f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning)
44242f891cd5SPawel Jakub Dawidek {
44252f891cd5SPawel Jakub Dawidek 
4426727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
44272f891cd5SPawel Jakub Dawidek 	zone->uz_warning = warning;
44282f891cd5SPawel Jakub Dawidek }
44292f891cd5SPawel Jakub Dawidek 
44302f891cd5SPawel Jakub Dawidek /* See uma.h */
443154503a13SJonathan T. Looney void
443254503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
443354503a13SJonathan T. Looney {
443454503a13SJonathan T. Looney 
4435727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
4436e60b2fcbSGleb Smirnoff 	TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
443754503a13SJonathan T. Looney }
443854503a13SJonathan T. Looney 
443954503a13SJonathan T. Looney /* See uma.h */
4440c4ae7908SLawrence Stewart int
4441c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone)
4442c4ae7908SLawrence Stewart {
4443c4ae7908SLawrence Stewart 	int64_t nitems;
4444c4ae7908SLawrence Stewart 	u_int i;
4445c4ae7908SLawrence Stewart 
4446bfb6b7a1SJeff Roberson 	nitems = 0;
4447bfb6b7a1SJeff Roberson 	if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER)
44482efcc8cbSGleb Smirnoff 		nitems = counter_u64_fetch(zone->uz_allocs) -
44492efcc8cbSGleb Smirnoff 		    counter_u64_fetch(zone->uz_frees);
4450727c6918SJeff Roberson 	CPU_FOREACH(i)
4451727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) -
4452727c6918SJeff Roberson 		    atomic_load_64(&zone->uz_cpu[i].uc_frees);
4453c4ae7908SLawrence Stewart 
4454c4ae7908SLawrence Stewart 	return (nitems < 0 ? 0 : nitems);
4455c4ae7908SLawrence Stewart }
4456c4ae7908SLawrence Stewart 
445720a4e154SJeff Roberson static uint64_t
445820a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone)
445920a4e154SJeff Roberson {
446020a4e154SJeff Roberson 	uint64_t nitems;
446120a4e154SJeff Roberson 	u_int i;
446220a4e154SJeff Roberson 
4463bfb6b7a1SJeff Roberson 	nitems = 0;
4464bfb6b7a1SJeff Roberson 	if (zone->uz_allocs != EARLY_COUNTER)
446520a4e154SJeff Roberson 		nitems = counter_u64_fetch(zone->uz_allocs);
4466727c6918SJeff Roberson 	CPU_FOREACH(i)
4467727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs);
446820a4e154SJeff Roberson 
446920a4e154SJeff Roberson 	return (nitems);
447020a4e154SJeff Roberson }
447120a4e154SJeff Roberson 
447220a4e154SJeff Roberson static uint64_t
447320a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone)
447420a4e154SJeff Roberson {
447520a4e154SJeff Roberson 	uint64_t nitems;
447620a4e154SJeff Roberson 	u_int i;
447720a4e154SJeff Roberson 
4478bfb6b7a1SJeff Roberson 	nitems = 0;
4479bfb6b7a1SJeff Roberson 	if (zone->uz_frees != EARLY_COUNTER)
448020a4e154SJeff Roberson 		nitems = counter_u64_fetch(zone->uz_frees);
4481727c6918SJeff Roberson 	CPU_FOREACH(i)
4482727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees);
448320a4e154SJeff Roberson 
448420a4e154SJeff Roberson 	return (nitems);
448520a4e154SJeff Roberson }
448620a4e154SJeff Roberson 
448731c251a0SJeff Roberson #ifdef INVARIANTS
448831c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */
448931c251a0SJeff Roberson static uint64_t
449031c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg)
449131c251a0SJeff Roberson {
449231c251a0SJeff Roberson 	uma_zone_t z;
449331c251a0SJeff Roberson 	uint64_t nitems;
449431c251a0SJeff Roberson 
449531c251a0SJeff Roberson 	nitems = 0;
449631c251a0SJeff Roberson 	LIST_FOREACH(z, &keg->uk_zones, uz_link)
449731c251a0SJeff Roberson 		nitems += uma_zone_get_allocs(z);
449831c251a0SJeff Roberson 
449931c251a0SJeff Roberson 	return (nitems);
450031c251a0SJeff Roberson }
450131c251a0SJeff Roberson #endif
450231c251a0SJeff Roberson 
4503c4ae7908SLawrence Stewart /* See uma.h */
4504736ee590SJeff Roberson void
4505099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit)
4506099a0e58SBosko Milekic {
4507e20a199fSJeff Roberson 	uma_keg_t keg;
4508e20a199fSJeff Roberson 
4509bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4510727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
4511e20a199fSJeff Roberson 	keg->uk_init = uminit;
4512099a0e58SBosko Milekic }
4513099a0e58SBosko Milekic 
4514099a0e58SBosko Milekic /* See uma.h */
4515099a0e58SBosko Milekic void
4516099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
4517099a0e58SBosko Milekic {
4518e20a199fSJeff Roberson 	uma_keg_t keg;
4519e20a199fSJeff Roberson 
4520bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4521727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
4522e20a199fSJeff Roberson 	keg->uk_fini = fini;
4523099a0e58SBosko Milekic }
4524099a0e58SBosko Milekic 
4525099a0e58SBosko Milekic /* See uma.h */
4526099a0e58SBosko Milekic void
4527099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
4528099a0e58SBosko Milekic {
4529af526374SJeff Roberson 
4530727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
4531099a0e58SBosko Milekic 	zone->uz_init = zinit;
4532099a0e58SBosko Milekic }
4533099a0e58SBosko Milekic 
4534099a0e58SBosko Milekic /* See uma.h */
4535099a0e58SBosko Milekic void
4536099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
4537099a0e58SBosko Milekic {
4538af526374SJeff Roberson 
4539727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
4540099a0e58SBosko Milekic 	zone->uz_fini = zfini;
4541099a0e58SBosko Milekic }
4542099a0e58SBosko Milekic 
4543099a0e58SBosko Milekic /* See uma.h */
4544099a0e58SBosko Milekic void
45458355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef)
45468355f576SJeff Roberson {
45470095a784SJeff Roberson 	uma_keg_t keg;
4548e20a199fSJeff Roberson 
4549bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4550727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
45510095a784SJeff Roberson 	keg->uk_freef = freef;
45528355f576SJeff Roberson }
45538355f576SJeff Roberson 
45548355f576SJeff Roberson /* See uma.h */
45558355f576SJeff Roberson void
45568355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
45578355f576SJeff Roberson {
4558e20a199fSJeff Roberson 	uma_keg_t keg;
4559e20a199fSJeff Roberson 
4560bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4561727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
4562e20a199fSJeff Roberson 	keg->uk_allocf = allocf;
45638355f576SJeff Roberson }
45648355f576SJeff Roberson 
45658355f576SJeff Roberson /* See uma.h */
45666fd34d6fSJeff Roberson void
4567d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr)
4568d4665eaaSJeff Roberson {
4569d4665eaaSJeff Roberson 
4570d4665eaaSJeff Roberson 	ZONE_ASSERT_COLD(zone);
4571d4665eaaSJeff Roberson 
4572d4665eaaSJeff Roberson 	zone->uz_flags |= UMA_ZONE_SMR;
4573d4665eaaSJeff Roberson 	zone->uz_smr = smr;
4574d4665eaaSJeff Roberson 	zone_update_caches(zone);
4575d4665eaaSJeff Roberson }
4576d4665eaaSJeff Roberson 
4577d4665eaaSJeff Roberson smr_t
4578d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone)
4579d4665eaaSJeff Roberson {
4580d4665eaaSJeff Roberson 
4581d4665eaaSJeff Roberson 	return (zone->uz_smr);
4582d4665eaaSJeff Roberson }
4583d4665eaaSJeff Roberson 
4584d4665eaaSJeff Roberson /* See uma.h */
4585d4665eaaSJeff Roberson void
45866fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items)
45876fd34d6fSJeff Roberson {
45886fd34d6fSJeff Roberson 	uma_keg_t keg;
45896fd34d6fSJeff Roberson 
4590bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4591727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
45926fd34d6fSJeff Roberson 	keg->uk_reserve = items;
45936fd34d6fSJeff Roberson }
45946fd34d6fSJeff Roberson 
45956fd34d6fSJeff Roberson /* See uma.h */
45968355f576SJeff Roberson int
4597a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count)
45988355f576SJeff Roberson {
4599099a0e58SBosko Milekic 	uma_keg_t keg;
46008355f576SJeff Roberson 	vm_offset_t kva;
46019ba30bcbSZbigniew Bodek 	u_int pages;
46028355f576SJeff Roberson 
4603bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4604727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
4605727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
46068355f576SJeff Roberson 
460779c9f942SJeff Roberson 	pages = howmany(count, keg->uk_ipers) * keg->uk_ppera;
4608a553d4b8SJeff Roberson 
4609a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
4610a4915c21SAttilio Rao 	if (keg->uk_ppera > 1) {
4611a4915c21SAttilio Rao #else
4612a4915c21SAttilio Rao 	if (1) {
4613a4915c21SAttilio Rao #endif
461457223e99SAndriy Gapon 		kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
4615d1f42ac2SAlan Cox 		if (kva == 0)
46168355f576SJeff Roberson 			return (0);
4617a4915c21SAttilio Rao 	} else
4618a4915c21SAttilio Rao 		kva = 0;
4619bb15d1c7SGleb Smirnoff 
4620bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4621bb15d1c7SGleb Smirnoff 	MPASS(keg->uk_kva == 0);
4622099a0e58SBosko Milekic 	keg->uk_kva = kva;
4623a4915c21SAttilio Rao 	keg->uk_offset = 0;
4624bb15d1c7SGleb Smirnoff 	zone->uz_max_items = pages * keg->uk_ipers;
4625a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
4626a4915c21SAttilio Rao 	keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
4627a4915c21SAttilio Rao #else
4628a4915c21SAttilio Rao 	keg->uk_allocf = noobj_alloc;
4629a4915c21SAttilio Rao #endif
4630cc7ce83aSJeff Roberson 	keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
4631cc7ce83aSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
4632cc7ce83aSJeff Roberson 	zone_update_caches(zone);
4633bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4634af526374SJeff Roberson 
46358355f576SJeff Roberson 	return (1);
46368355f576SJeff Roberson }
46378355f576SJeff Roberson 
46388355f576SJeff Roberson /* See uma.h */
46398355f576SJeff Roberson void
46408355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items)
46418355f576SJeff Roberson {
4642920239efSMark Johnston 	struct vm_domainset_iter di;
4643ab3185d1SJeff Roberson 	uma_domain_t dom;
46448355f576SJeff Roberson 	uma_slab_t slab;
4645099a0e58SBosko Milekic 	uma_keg_t keg;
464686220393SMark Johnston 	int aflags, domain, slabs;
46478355f576SJeff Roberson 
4648bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
464979c9f942SJeff Roberson 	slabs = howmany(items, keg->uk_ipers);
4650194a979eSMark Johnston 	while (slabs-- > 0) {
465186220393SMark Johnston 		aflags = M_NOWAIT;
465286220393SMark Johnston 		vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
465386220393SMark Johnston 		    &aflags);
465486220393SMark Johnston 		for (;;) {
465586220393SMark Johnston 			slab = keg_alloc_slab(keg, zone, domain, M_WAITOK,
465686220393SMark Johnston 			    aflags);
465786220393SMark Johnston 			if (slab != NULL) {
4658ab3185d1SJeff Roberson 				dom = &keg->uk_domain[slab->us_domain];
46594ab3aee8SMark Johnston 				/*
46604ab3aee8SMark Johnston 				 * keg_alloc_slab() always returns a slab on the
46614ab3aee8SMark Johnston 				 * partial list.
46624ab3aee8SMark Johnston 				 */
46638b987a77SJeff Roberson 				LIST_REMOVE(slab, us_link);
466486220393SMark Johnston 				LIST_INSERT_HEAD(&dom->ud_free_slab, slab,
466586220393SMark Johnston 				    us_link);
46664ab3aee8SMark Johnston 				dom->ud_free_slabs++;
46678b987a77SJeff Roberson 				KEG_UNLOCK(keg, slab->us_domain);
4668920239efSMark Johnston 				break;
46698355f576SJeff Roberson 			}
46708b987a77SJeff Roberson 			if (vm_domainset_iter_policy(&di, &domain) != 0)
467186220393SMark Johnston 				vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
467286220393SMark Johnston 		}
467386220393SMark Johnston 	}
467486220393SMark Johnston }
46758355f576SJeff Roberson 
46768355f576SJeff Roberson /* See uma.h */
467708cfa56eSMark Johnston void
467808cfa56eSMark Johnston uma_reclaim(int req)
46798355f576SJeff Roberson {
468044ec2b63SKonstantin Belousov 
46811431a748SGleb Smirnoff 	CTR0(KTR_UMA, "UMA: vm asked us to release pages!");
468208cfa56eSMark Johnston 	sx_xlock(&uma_reclaim_lock);
468386bbae32SJeff Roberson 	bucket_enable();
468408cfa56eSMark Johnston 
468508cfa56eSMark Johnston 	switch (req) {
468608cfa56eSMark Johnston 	case UMA_RECLAIM_TRIM:
468720a4e154SJeff Roberson 		zone_foreach(zone_trim, NULL);
468808cfa56eSMark Johnston 		break;
468908cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN:
469008cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN_CPU:
469120a4e154SJeff Roberson 		zone_foreach(zone_drain, NULL);
469208cfa56eSMark Johnston 		if (req == UMA_RECLAIM_DRAIN_CPU) {
469308cfa56eSMark Johnston 			pcpu_cache_drain_safe(NULL);
469420a4e154SJeff Roberson 			zone_foreach(zone_drain, NULL);
4695a2de44abSAlexander Motin 		}
469608cfa56eSMark Johnston 		break;
469708cfa56eSMark Johnston 	default:
469808cfa56eSMark Johnston 		panic("unhandled reclamation request %d", req);
469908cfa56eSMark Johnston 	}
47000f9b7bf3SMark Johnston 
47018355f576SJeff Roberson 	/*
47028355f576SJeff Roberson 	 * Some slabs may have been freed but this zone will be visited early
47038355f576SJeff Roberson 	 * we visit again so that we can free pages that are empty once other
47048355f576SJeff Roberson 	 * zones are drained.  We have to do the same for buckets.
47058355f576SJeff Roberson 	 */
47069b8db4d0SRyan Libby 	zone_drain(slabzones[0], NULL);
47079b8db4d0SRyan Libby 	zone_drain(slabzones[1], NULL);
4708cae33c14SJeff Roberson 	bucket_zone_drain();
470908cfa56eSMark Johnston 	sx_xunlock(&uma_reclaim_lock);
47108355f576SJeff Roberson }
47118355f576SJeff Roberson 
47122e47807cSJeff Roberson static volatile int uma_reclaim_needed;
471344ec2b63SKonstantin Belousov 
471444ec2b63SKonstantin Belousov void
471544ec2b63SKonstantin Belousov uma_reclaim_wakeup(void)
471644ec2b63SKonstantin Belousov {
471744ec2b63SKonstantin Belousov 
47182e47807cSJeff Roberson 	if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
47192e47807cSJeff Roberson 		wakeup(uma_reclaim);
472044ec2b63SKonstantin Belousov }
472144ec2b63SKonstantin Belousov 
472244ec2b63SKonstantin Belousov void
472344ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused)
472444ec2b63SKonstantin Belousov {
472544ec2b63SKonstantin Belousov 
472644ec2b63SKonstantin Belousov 	for (;;) {
472708cfa56eSMark Johnston 		sx_xlock(&uma_reclaim_lock);
4728200f8117SKonstantin Belousov 		while (atomic_load_int(&uma_reclaim_needed) == 0)
472908cfa56eSMark Johnston 			sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl",
47302e47807cSJeff Roberson 			    hz);
473108cfa56eSMark Johnston 		sx_xunlock(&uma_reclaim_lock);
47329b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
473308cfa56eSMark Johnston 		uma_reclaim(UMA_RECLAIM_DRAIN_CPU);
4734200f8117SKonstantin Belousov 		atomic_store_int(&uma_reclaim_needed, 0);
47352e47807cSJeff Roberson 		/* Don't fire more than once per-second. */
47362e47807cSJeff Roberson 		pause("umarclslp", hz);
473744ec2b63SKonstantin Belousov 	}
473844ec2b63SKonstantin Belousov }
473944ec2b63SKonstantin Belousov 
4740663b416fSJohn Baldwin /* See uma.h */
474108cfa56eSMark Johnston void
474208cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req)
474308cfa56eSMark Johnston {
474408cfa56eSMark Johnston 
474508cfa56eSMark Johnston 	switch (req) {
474608cfa56eSMark Johnston 	case UMA_RECLAIM_TRIM:
474720a4e154SJeff Roberson 		zone_trim(zone, NULL);
474808cfa56eSMark Johnston 		break;
474908cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN:
475020a4e154SJeff Roberson 		zone_drain(zone, NULL);
475108cfa56eSMark Johnston 		break;
475208cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN_CPU:
475308cfa56eSMark Johnston 		pcpu_cache_drain_safe(zone);
475420a4e154SJeff Roberson 		zone_drain(zone, NULL);
475508cfa56eSMark Johnston 		break;
475608cfa56eSMark Johnston 	default:
475708cfa56eSMark Johnston 		panic("unhandled reclamation request %d", req);
475808cfa56eSMark Johnston 	}
475908cfa56eSMark Johnston }
476008cfa56eSMark Johnston 
476108cfa56eSMark Johnston /* See uma.h */
4762663b416fSJohn Baldwin int
4763663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone)
4764663b416fSJohn Baldwin {
4765663b416fSJohn Baldwin 
4766727c6918SJeff Roberson 	return (atomic_load_32(&zone->uz_sleepers) > 0);
47676c125b8dSMohan Srinivasan }
47686c125b8dSMohan Srinivasan 
47692e47807cSJeff Roberson unsigned long
47702e47807cSJeff Roberson uma_limit(void)
47712e47807cSJeff Roberson {
47722e47807cSJeff Roberson 
47732e47807cSJeff Roberson 	return (uma_kmem_limit);
47742e47807cSJeff Roberson }
47752e47807cSJeff Roberson 
47762e47807cSJeff Roberson void
47772e47807cSJeff Roberson uma_set_limit(unsigned long limit)
47782e47807cSJeff Roberson {
47792e47807cSJeff Roberson 
47802e47807cSJeff Roberson 	uma_kmem_limit = limit;
47812e47807cSJeff Roberson }
47822e47807cSJeff Roberson 
47832e47807cSJeff Roberson unsigned long
47842e47807cSJeff Roberson uma_size(void)
47852e47807cSJeff Roberson {
47862e47807cSJeff Roberson 
4787058f0f74SMark Johnston 	return (atomic_load_long(&uma_kmem_total));
4788ad5b0f5bSJeff Roberson }
4789ad5b0f5bSJeff Roberson 
4790ad5b0f5bSJeff Roberson long
4791ad5b0f5bSJeff Roberson uma_avail(void)
4792ad5b0f5bSJeff Roberson {
4793ad5b0f5bSJeff Roberson 
4794058f0f74SMark Johnston 	return (uma_kmem_limit - uma_size());
47952e47807cSJeff Roberson }
47962e47807cSJeff Roberson 
4797a0d4b0aeSRobert Watson #ifdef DDB
47988355f576SJeff Roberson /*
47997a52a97eSRobert Watson  * Generate statistics across both the zone and its per-cpu cache's.  Return
48007a52a97eSRobert Watson  * desired statistics if the pointer is non-NULL for that statistic.
48017a52a97eSRobert Watson  *
48027a52a97eSRobert Watson  * Note: does not update the zone statistics, as it can't safely clear the
48037a52a97eSRobert Watson  * per-CPU cache statistic.
48047a52a97eSRobert Watson  *
48057a52a97eSRobert Watson  */
48067a52a97eSRobert Watson static void
48070f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp,
4808c1685086SJeff Roberson     uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp)
48097a52a97eSRobert Watson {
48107a52a97eSRobert Watson 	uma_cache_t cache;
4811c1685086SJeff Roberson 	uint64_t allocs, frees, sleeps, xdomain;
48127a52a97eSRobert Watson 	int cachefree, cpu;
48137a52a97eSRobert Watson 
4814c1685086SJeff Roberson 	allocs = frees = sleeps = xdomain = 0;
48157a52a97eSRobert Watson 	cachefree = 0;
48163aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
48177a52a97eSRobert Watson 		cache = &z->uz_cpu[cpu];
4818376b1ba3SJeff Roberson 		cachefree += cache->uc_allocbucket.ucb_cnt;
4819376b1ba3SJeff Roberson 		cachefree += cache->uc_freebucket.ucb_cnt;
4820376b1ba3SJeff Roberson 		xdomain += cache->uc_crossbucket.ucb_cnt;
4821376b1ba3SJeff Roberson 		cachefree += cache->uc_crossbucket.ucb_cnt;
48227a52a97eSRobert Watson 		allocs += cache->uc_allocs;
48237a52a97eSRobert Watson 		frees += cache->uc_frees;
48247a52a97eSRobert Watson 	}
48252efcc8cbSGleb Smirnoff 	allocs += counter_u64_fetch(z->uz_allocs);
48262efcc8cbSGleb Smirnoff 	frees += counter_u64_fetch(z->uz_frees);
4827bf965959SSean Bruno 	sleeps += z->uz_sleeps;
4828c1685086SJeff Roberson 	xdomain += z->uz_xdomain;
48297a52a97eSRobert Watson 	if (cachefreep != NULL)
48307a52a97eSRobert Watson 		*cachefreep = cachefree;
48317a52a97eSRobert Watson 	if (allocsp != NULL)
48327a52a97eSRobert Watson 		*allocsp = allocs;
48337a52a97eSRobert Watson 	if (freesp != NULL)
48347a52a97eSRobert Watson 		*freesp = frees;
4835bf965959SSean Bruno 	if (sleepsp != NULL)
4836bf965959SSean Bruno 		*sleepsp = sleeps;
4837c1685086SJeff Roberson 	if (xdomainp != NULL)
4838c1685086SJeff Roberson 		*xdomainp = xdomain;
48397a52a97eSRobert Watson }
4840a0d4b0aeSRobert Watson #endif /* DDB */
48417a52a97eSRobert Watson 
48427a52a97eSRobert Watson static int
48437a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
48447a52a97eSRobert Watson {
48457a52a97eSRobert Watson 	uma_keg_t kz;
48467a52a97eSRobert Watson 	uma_zone_t z;
48477a52a97eSRobert Watson 	int count;
48487a52a97eSRobert Watson 
48497a52a97eSRobert Watson 	count = 0;
4850111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
48517a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
48527a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
48537a52a97eSRobert Watson 			count++;
48547a52a97eSRobert Watson 	}
4855b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link)
4856b47acb0aSGleb Smirnoff 		count++;
4857b47acb0aSGleb Smirnoff 
4858111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
48597a52a97eSRobert Watson 	return (sysctl_handle_int(oidp, &count, 0, req));
48607a52a97eSRobert Watson }
48617a52a97eSRobert Watson 
4862b47acb0aSGleb Smirnoff static void
4863b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf,
4864b47acb0aSGleb Smirnoff     struct uma_percpu_stat *ups, bool internal)
4865b47acb0aSGleb Smirnoff {
4866b47acb0aSGleb Smirnoff 	uma_zone_domain_t zdom;
4867b47acb0aSGleb Smirnoff 	uma_cache_t cache;
4868b47acb0aSGleb Smirnoff 	int i;
4869b47acb0aSGleb Smirnoff 
4870b47acb0aSGleb Smirnoff 
4871b47acb0aSGleb Smirnoff 	for (i = 0; i < vm_ndomains; i++) {
4872b47acb0aSGleb Smirnoff 		zdom = &z->uz_domain[i];
4873b47acb0aSGleb Smirnoff 		uth->uth_zone_free += zdom->uzd_nitems;
4874b47acb0aSGleb Smirnoff 	}
4875b47acb0aSGleb Smirnoff 	uth->uth_allocs = counter_u64_fetch(z->uz_allocs);
4876b47acb0aSGleb Smirnoff 	uth->uth_frees = counter_u64_fetch(z->uz_frees);
4877b47acb0aSGleb Smirnoff 	uth->uth_fails = counter_u64_fetch(z->uz_fails);
4878b47acb0aSGleb Smirnoff 	uth->uth_sleeps = z->uz_sleeps;
4879c1685086SJeff Roberson 	uth->uth_xdomain = z->uz_xdomain;
48801de9724eSMark Johnston 
4881b47acb0aSGleb Smirnoff 	/*
48821de9724eSMark Johnston 	 * While it is not normally safe to access the cache bucket pointers
48831de9724eSMark Johnston 	 * while not on the CPU that owns the cache, we only allow the pointers
48841de9724eSMark Johnston 	 * to be exchanged without the zone lock held, not invalidated, so
48851de9724eSMark Johnston 	 * accept the possible race associated with bucket exchange during
48861de9724eSMark Johnston 	 * monitoring.  Use atomic_load_ptr() to ensure that the bucket pointers
48871de9724eSMark Johnston 	 * are loaded only once.
4888b47acb0aSGleb Smirnoff 	 */
4889b47acb0aSGleb Smirnoff 	for (i = 0; i < mp_maxid + 1; i++) {
4890b47acb0aSGleb Smirnoff 		bzero(&ups[i], sizeof(*ups));
4891b47acb0aSGleb Smirnoff 		if (internal || CPU_ABSENT(i))
4892b47acb0aSGleb Smirnoff 			continue;
4893b47acb0aSGleb Smirnoff 		cache = &z->uz_cpu[i];
4894376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt;
4895376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt;
4896376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt;
4897b47acb0aSGleb Smirnoff 		ups[i].ups_allocs = cache->uc_allocs;
4898b47acb0aSGleb Smirnoff 		ups[i].ups_frees = cache->uc_frees;
4899b47acb0aSGleb Smirnoff 	}
4900b47acb0aSGleb Smirnoff }
4901b47acb0aSGleb Smirnoff 
49027a52a97eSRobert Watson static int
49037a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
49047a52a97eSRobert Watson {
49057a52a97eSRobert Watson 	struct uma_stream_header ush;
49067a52a97eSRobert Watson 	struct uma_type_header uth;
490763b5d112SKonstantin Belousov 	struct uma_percpu_stat *ups;
49087a52a97eSRobert Watson 	struct sbuf sbuf;
49097a52a97eSRobert Watson 	uma_keg_t kz;
49107a52a97eSRobert Watson 	uma_zone_t z;
49114bd61e19SJeff Roberson 	uint64_t items;
49128b987a77SJeff Roberson 	uint32_t kfree, pages;
49134e657159SMatthew D Fleming 	int count, error, i;
49147a52a97eSRobert Watson 
491500f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
491600f0e671SMatthew D Fleming 	if (error != 0)
491700f0e671SMatthew D Fleming 		return (error);
49184e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
49191eafc078SIan Lepore 	sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
492063b5d112SKonstantin Belousov 	ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK);
49214e657159SMatthew D Fleming 
4922404a593eSMatthew D Fleming 	count = 0;
4923111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
49247a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
49257a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
49267a52a97eSRobert Watson 			count++;
49277a52a97eSRobert Watson 	}
49287a52a97eSRobert Watson 
4929b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link)
4930b47acb0aSGleb Smirnoff 		count++;
4931b47acb0aSGleb Smirnoff 
49327a52a97eSRobert Watson 	/*
49337a52a97eSRobert Watson 	 * Insert stream header.
49347a52a97eSRobert Watson 	 */
49357a52a97eSRobert Watson 	bzero(&ush, sizeof(ush));
49367a52a97eSRobert Watson 	ush.ush_version = UMA_STREAM_VERSION;
4937ab3a57c0SRobert Watson 	ush.ush_maxcpus = (mp_maxid + 1);
49387a52a97eSRobert Watson 	ush.ush_count = count;
49394e657159SMatthew D Fleming 	(void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
49407a52a97eSRobert Watson 
49417a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
49428b987a77SJeff Roberson 		kfree = pages = 0;
49438b987a77SJeff Roberson 		for (i = 0; i < vm_ndomains; i++) {
49444ab3aee8SMark Johnston 			kfree += kz->uk_domain[i].ud_free_items;
49458b987a77SJeff Roberson 			pages += kz->uk_domain[i].ud_pages;
49468b987a77SJeff Roberson 		}
49477a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
49487a52a97eSRobert Watson 			bzero(&uth, sizeof(uth));
49497a52a97eSRobert Watson 			ZONE_LOCK(z);
4950cbbb4a00SRobert Watson 			strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
49517a52a97eSRobert Watson 			uth.uth_align = kz->uk_align;
49527a52a97eSRobert Watson 			uth.uth_size = kz->uk_size;
49537a52a97eSRobert Watson 			uth.uth_rsize = kz->uk_rsize;
49544bd61e19SJeff Roberson 			if (z->uz_max_items > 0) {
49554bd61e19SJeff Roberson 				items = UZ_ITEMS_COUNT(z->uz_items);
49564bd61e19SJeff Roberson 				uth.uth_pages = (items / kz->uk_ipers) *
4957bb15d1c7SGleb Smirnoff 					kz->uk_ppera;
49584bd61e19SJeff Roberson 			} else
49598b987a77SJeff Roberson 				uth.uth_pages = pages;
4960f8c86a5fSGleb Smirnoff 			uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) *
4961bb15d1c7SGleb Smirnoff 			    kz->uk_ppera;
4962bb15d1c7SGleb Smirnoff 			uth.uth_limit = z->uz_max_items;
49638b987a77SJeff Roberson 			uth.uth_keg_free = kfree;
4964cbbb4a00SRobert Watson 
4965cbbb4a00SRobert Watson 			/*
4966cbbb4a00SRobert Watson 			 * A zone is secondary is it is not the first entry
4967cbbb4a00SRobert Watson 			 * on the keg's zone list.
4968cbbb4a00SRobert Watson 			 */
4969e20a199fSJeff Roberson 			if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
4970cbbb4a00SRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z))
4971cbbb4a00SRobert Watson 				uth.uth_zone_flags = UTH_ZONE_SECONDARY;
4972b47acb0aSGleb Smirnoff 			uma_vm_zone_stats(&uth, z, &sbuf, ups,
4973b47acb0aSGleb Smirnoff 			    kz->uk_flags & UMA_ZFLAG_INTERNAL);
49742450bbb8SRobert Watson 			ZONE_UNLOCK(z);
497563b5d112SKonstantin Belousov 			(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
497663b5d112SKonstantin Belousov 			for (i = 0; i < mp_maxid + 1; i++)
497763b5d112SKonstantin Belousov 				(void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
49787a52a97eSRobert Watson 		}
49797a52a97eSRobert Watson 	}
4980b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
4981b47acb0aSGleb Smirnoff 		bzero(&uth, sizeof(uth));
4982b47acb0aSGleb Smirnoff 		ZONE_LOCK(z);
4983b47acb0aSGleb Smirnoff 		strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
4984b47acb0aSGleb Smirnoff 		uth.uth_size = z->uz_size;
4985b47acb0aSGleb Smirnoff 		uma_vm_zone_stats(&uth, z, &sbuf, ups, false);
4986b47acb0aSGleb Smirnoff 		ZONE_UNLOCK(z);
4987b47acb0aSGleb Smirnoff 		(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
4988b47acb0aSGleb Smirnoff 		for (i = 0; i < mp_maxid + 1; i++)
4989b47acb0aSGleb Smirnoff 			(void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
4990b47acb0aSGleb Smirnoff 	}
4991b47acb0aSGleb Smirnoff 
4992111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
49934e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
49944e657159SMatthew D Fleming 	sbuf_delete(&sbuf);
499563b5d112SKonstantin Belousov 	free(ups, M_TEMP);
49967a52a97eSRobert Watson 	return (error);
49977a52a97eSRobert Watson }
499848c5777eSRobert Watson 
49990a5a3ccbSGleb Smirnoff int
50000a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
50010a5a3ccbSGleb Smirnoff {
50020a5a3ccbSGleb Smirnoff 	uma_zone_t zone = *(uma_zone_t *)arg1;
500316be9f54SGleb Smirnoff 	int error, max;
50040a5a3ccbSGleb Smirnoff 
500516be9f54SGleb Smirnoff 	max = uma_zone_get_max(zone);
50060a5a3ccbSGleb Smirnoff 	error = sysctl_handle_int(oidp, &max, 0, req);
50070a5a3ccbSGleb Smirnoff 	if (error || !req->newptr)
50080a5a3ccbSGleb Smirnoff 		return (error);
50090a5a3ccbSGleb Smirnoff 
50100a5a3ccbSGleb Smirnoff 	uma_zone_set_max(zone, max);
50110a5a3ccbSGleb Smirnoff 
50120a5a3ccbSGleb Smirnoff 	return (0);
50130a5a3ccbSGleb Smirnoff }
50140a5a3ccbSGleb Smirnoff 
50150a5a3ccbSGleb Smirnoff int
50160a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
50170a5a3ccbSGleb Smirnoff {
501820a4e154SJeff Roberson 	uma_zone_t zone;
50190a5a3ccbSGleb Smirnoff 	int cur;
50200a5a3ccbSGleb Smirnoff 
502120a4e154SJeff Roberson 	/*
502220a4e154SJeff Roberson 	 * Some callers want to add sysctls for global zones that
502320a4e154SJeff Roberson 	 * may not yet exist so they pass a pointer to a pointer.
502420a4e154SJeff Roberson 	 */
502520a4e154SJeff Roberson 	if (arg2 == 0)
502620a4e154SJeff Roberson 		zone = *(uma_zone_t *)arg1;
502720a4e154SJeff Roberson 	else
502820a4e154SJeff Roberson 		zone = arg1;
50290a5a3ccbSGleb Smirnoff 	cur = uma_zone_get_cur(zone);
50300a5a3ccbSGleb Smirnoff 	return (sysctl_handle_int(oidp, &cur, 0, req));
50310a5a3ccbSGleb Smirnoff }
50320a5a3ccbSGleb Smirnoff 
503320a4e154SJeff Roberson static int
503420a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS)
503520a4e154SJeff Roberson {
503620a4e154SJeff Roberson 	uma_zone_t zone = arg1;
503720a4e154SJeff Roberson 	uint64_t cur;
503820a4e154SJeff Roberson 
503920a4e154SJeff Roberson 	cur = uma_zone_get_allocs(zone);
504020a4e154SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
504120a4e154SJeff Roberson }
504220a4e154SJeff Roberson 
504320a4e154SJeff Roberson static int
504420a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS)
504520a4e154SJeff Roberson {
504620a4e154SJeff Roberson 	uma_zone_t zone = arg1;
504720a4e154SJeff Roberson 	uint64_t cur;
504820a4e154SJeff Roberson 
504920a4e154SJeff Roberson 	cur = uma_zone_get_frees(zone);
505020a4e154SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
505120a4e154SJeff Roberson }
505220a4e154SJeff Roberson 
50536d204a6aSRyan Libby static int
50546d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS)
50556d204a6aSRyan Libby {
50566d204a6aSRyan Libby 	struct sbuf sbuf;
50576d204a6aSRyan Libby 	uma_zone_t zone = arg1;
50586d204a6aSRyan Libby 	int error;
50596d204a6aSRyan Libby 
50606d204a6aSRyan Libby 	sbuf_new_for_sysctl(&sbuf, NULL, 0, req);
50616d204a6aSRyan Libby 	if (zone->uz_flags != 0)
50626d204a6aSRyan Libby 		sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS);
50636d204a6aSRyan Libby 	else
50646d204a6aSRyan Libby 		sbuf_printf(&sbuf, "0");
50656d204a6aSRyan Libby 	error = sbuf_finish(&sbuf);
50666d204a6aSRyan Libby 	sbuf_delete(&sbuf);
50676d204a6aSRyan Libby 
50686d204a6aSRyan Libby 	return (error);
50696d204a6aSRyan Libby }
50706d204a6aSRyan Libby 
5071f7af5015SRyan Libby static int
5072f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS)
5073f7af5015SRyan Libby {
5074f7af5015SRyan Libby 	uma_keg_t keg = arg1;
5075f7af5015SRyan Libby 	int avail, effpct, total;
5076f7af5015SRyan Libby 
5077f7af5015SRyan Libby 	total = keg->uk_ppera * PAGE_SIZE;
507854c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0)
50799b8db4d0SRyan Libby 		total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize;
5080f7af5015SRyan Libby 	/*
5081f7af5015SRyan Libby 	 * We consider the client's requested size and alignment here, not the
5082f7af5015SRyan Libby 	 * real size determination uk_rsize, because we also adjust the real
5083f7af5015SRyan Libby 	 * size for internal implementation reasons (max bitset size).
5084f7af5015SRyan Libby 	 */
5085f7af5015SRyan Libby 	avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1);
5086f7af5015SRyan Libby 	if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
5087f7af5015SRyan Libby 		avail *= mp_maxid + 1;
5088f7af5015SRyan Libby 	effpct = 100 * avail / total;
5089f7af5015SRyan Libby 	return (sysctl_handle_int(oidp, &effpct, 0, req));
5090f7af5015SRyan Libby }
5091f7af5015SRyan Libby 
50924bd61e19SJeff Roberson static int
50934bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS)
50944bd61e19SJeff Roberson {
50954bd61e19SJeff Roberson 	uma_zone_t zone = arg1;
50964bd61e19SJeff Roberson 	uint64_t cur;
50974bd61e19SJeff Roberson 
50984bd61e19SJeff Roberson 	cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items));
50994bd61e19SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
51004bd61e19SJeff Roberson }
51014bd61e19SJeff Roberson 
51029542ea7bSGleb Smirnoff #ifdef INVARIANTS
51039542ea7bSGleb Smirnoff static uma_slab_t
51049542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item)
51059542ea7bSGleb Smirnoff {
51069542ea7bSGleb Smirnoff 	uma_slab_t slab;
51079542ea7bSGleb Smirnoff 	uma_keg_t keg;
51089542ea7bSGleb Smirnoff 	uint8_t *mem;
51099542ea7bSGleb Smirnoff 
51109542ea7bSGleb Smirnoff 	/*
51119542ea7bSGleb Smirnoff 	 * It is safe to return the slab here even though the
51129542ea7bSGleb Smirnoff 	 * zone is unlocked because the item's allocation state
51139542ea7bSGleb Smirnoff 	 * essentially holds a reference.
51149542ea7bSGleb Smirnoff 	 */
5115727c6918SJeff Roberson 	mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
5116727c6918SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5117bb15d1c7SGleb Smirnoff 		return (NULL);
511854c5ae80SRyan Libby 	if (zone->uz_flags & UMA_ZFLAG_VTOSLAB)
5119727c6918SJeff Roberson 		return (vtoslab((vm_offset_t)mem));
5120bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
512154c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0)
5122727c6918SJeff Roberson 		return ((uma_slab_t)(mem + keg->uk_pgoff));
51238b987a77SJeff Roberson 	KEG_LOCK(keg, 0);
51249542ea7bSGleb Smirnoff 	slab = hash_sfind(&keg->uk_hash, mem);
51258b987a77SJeff Roberson 	KEG_UNLOCK(keg, 0);
51269542ea7bSGleb Smirnoff 
51279542ea7bSGleb Smirnoff 	return (slab);
51289542ea7bSGleb Smirnoff }
51299542ea7bSGleb Smirnoff 
5130c5deaf04SGleb Smirnoff static bool
5131c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem)
5132c5deaf04SGleb Smirnoff {
5133c5deaf04SGleb Smirnoff 
5134727c6918SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5135c5deaf04SGleb Smirnoff 		return (true);
5136c5deaf04SGleb Smirnoff 
5137bb15d1c7SGleb Smirnoff 	return (uma_dbg_kskip(zone->uz_keg, mem));
5138c5deaf04SGleb Smirnoff }
5139c5deaf04SGleb Smirnoff 
5140c5deaf04SGleb Smirnoff static bool
5141c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem)
5142c5deaf04SGleb Smirnoff {
5143c5deaf04SGleb Smirnoff 	uintptr_t idx;
5144c5deaf04SGleb Smirnoff 
5145c5deaf04SGleb Smirnoff 	if (dbg_divisor == 0)
5146c5deaf04SGleb Smirnoff 		return (true);
5147c5deaf04SGleb Smirnoff 
5148c5deaf04SGleb Smirnoff 	if (dbg_divisor == 1)
5149c5deaf04SGleb Smirnoff 		return (false);
5150c5deaf04SGleb Smirnoff 
5151c5deaf04SGleb Smirnoff 	idx = (uintptr_t)mem >> PAGE_SHIFT;
5152c5deaf04SGleb Smirnoff 	if (keg->uk_ipers > 1) {
5153c5deaf04SGleb Smirnoff 		idx *= keg->uk_ipers;
5154c5deaf04SGleb Smirnoff 		idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize;
5155c5deaf04SGleb Smirnoff 	}
5156c5deaf04SGleb Smirnoff 
5157c5deaf04SGleb Smirnoff 	if ((idx / dbg_divisor) * dbg_divisor != idx) {
5158c5deaf04SGleb Smirnoff 		counter_u64_add(uma_skip_cnt, 1);
5159c5deaf04SGleb Smirnoff 		return (true);
5160c5deaf04SGleb Smirnoff 	}
5161c5deaf04SGleb Smirnoff 	counter_u64_add(uma_dbg_cnt, 1);
5162c5deaf04SGleb Smirnoff 
5163c5deaf04SGleb Smirnoff 	return (false);
5164c5deaf04SGleb Smirnoff }
5165c5deaf04SGleb Smirnoff 
51669542ea7bSGleb Smirnoff /*
51679542ea7bSGleb Smirnoff  * Set up the slab's freei data such that uma_dbg_free can function.
51689542ea7bSGleb Smirnoff  *
51699542ea7bSGleb Smirnoff  */
51709542ea7bSGleb Smirnoff static void
51719542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
51729542ea7bSGleb Smirnoff {
51739542ea7bSGleb Smirnoff 	uma_keg_t keg;
51749542ea7bSGleb Smirnoff 	int freei;
51759542ea7bSGleb Smirnoff 
51769542ea7bSGleb Smirnoff 	if (slab == NULL) {
51779542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
51789542ea7bSGleb Smirnoff 		if (slab == NULL)
51799542ea7bSGleb Smirnoff 			panic("uma: item %p did not belong to zone %s\n",
51809542ea7bSGleb Smirnoff 			    item, zone->uz_name);
51819542ea7bSGleb Smirnoff 	}
5182584061b4SJeff Roberson 	keg = zone->uz_keg;
51831e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
51849542ea7bSGleb Smirnoff 
5185815db204SRyan Libby 	if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)))
51869542ea7bSGleb Smirnoff 		panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
51879542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
5188815db204SRyan Libby 	BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg));
51899542ea7bSGleb Smirnoff }
51909542ea7bSGleb Smirnoff 
51919542ea7bSGleb Smirnoff /*
51929542ea7bSGleb Smirnoff  * Verifies freed addresses.  Checks for alignment, valid slab membership
51939542ea7bSGleb Smirnoff  * and duplicate frees.
51949542ea7bSGleb Smirnoff  *
51959542ea7bSGleb Smirnoff  */
51969542ea7bSGleb Smirnoff static void
51979542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
51989542ea7bSGleb Smirnoff {
51999542ea7bSGleb Smirnoff 	uma_keg_t keg;
52009542ea7bSGleb Smirnoff 	int freei;
52019542ea7bSGleb Smirnoff 
52029542ea7bSGleb Smirnoff 	if (slab == NULL) {
52039542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
52049542ea7bSGleb Smirnoff 		if (slab == NULL)
52059542ea7bSGleb Smirnoff 			panic("uma: Freed item %p did not belong to zone %s\n",
52069542ea7bSGleb Smirnoff 			    item, zone->uz_name);
52079542ea7bSGleb Smirnoff 	}
5208584061b4SJeff Roberson 	keg = zone->uz_keg;
52091e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
52109542ea7bSGleb Smirnoff 
52119542ea7bSGleb Smirnoff 	if (freei >= keg->uk_ipers)
52129542ea7bSGleb Smirnoff 		panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
52139542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
52149542ea7bSGleb Smirnoff 
52151e0701e1SJeff Roberson 	if (slab_item(slab, keg, freei) != item)
52169542ea7bSGleb Smirnoff 		panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
52179542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
52189542ea7bSGleb Smirnoff 
5219815db204SRyan Libby 	if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)))
52209542ea7bSGleb Smirnoff 		panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
52219542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
52229542ea7bSGleb Smirnoff 
5223815db204SRyan Libby 	BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg));
52249542ea7bSGleb Smirnoff }
52259542ea7bSGleb Smirnoff #endif /* INVARIANTS */
52269542ea7bSGleb Smirnoff 
522748c5777eSRobert Watson #ifdef DDB
522846d70077SConrad Meyer static int64_t
522946d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used,
52300223790fSConrad Meyer     uint64_t *sleeps, long *cachefree, uint64_t *xdomain)
523148c5777eSRobert Watson {
523246d70077SConrad Meyer 	uint64_t frees;
52330f9b7bf3SMark Johnston 	int i;
523448c5777eSRobert Watson 
523548c5777eSRobert Watson 	if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
523646d70077SConrad Meyer 		*allocs = counter_u64_fetch(z->uz_allocs);
52372efcc8cbSGleb Smirnoff 		frees = counter_u64_fetch(z->uz_frees);
523846d70077SConrad Meyer 		*sleeps = z->uz_sleeps;
523946d70077SConrad Meyer 		*cachefree = 0;
524046d70077SConrad Meyer 		*xdomain = 0;
524148c5777eSRobert Watson 	} else
524246d70077SConrad Meyer 		uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps,
524346d70077SConrad Meyer 		    xdomain);
52448b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
52458b987a77SJeff Roberson 		*cachefree += z->uz_domain[i].uzd_nitems;
5246e20a199fSJeff Roberson 		if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
524748c5777eSRobert Watson 		    (LIST_FIRST(&kz->uk_zones) != z)))
52484ab3aee8SMark Johnston 			*cachefree += kz->uk_domain[i].ud_free_items;
52498b987a77SJeff Roberson 	}
525046d70077SConrad Meyer 	*used = *allocs - frees;
525146d70077SConrad Meyer 	return (((int64_t)*used + *cachefree) * kz->uk_size);
525246d70077SConrad Meyer }
52530f9b7bf3SMark Johnston 
525446d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma)
525546d70077SConrad Meyer {
525646d70077SConrad Meyer 	const char *fmt_hdr, *fmt_entry;
525746d70077SConrad Meyer 	uma_keg_t kz;
525846d70077SConrad Meyer 	uma_zone_t z;
525946d70077SConrad Meyer 	uint64_t allocs, used, sleeps, xdomain;
526046d70077SConrad Meyer 	long cachefree;
526146d70077SConrad Meyer 	/* variables for sorting */
526246d70077SConrad Meyer 	uma_keg_t cur_keg;
526346d70077SConrad Meyer 	uma_zone_t cur_zone, last_zone;
526446d70077SConrad Meyer 	int64_t cur_size, last_size, size;
526546d70077SConrad Meyer 	int ties;
526646d70077SConrad Meyer 
526746d70077SConrad Meyer 	/* /i option produces machine-parseable CSV output */
526846d70077SConrad Meyer 	if (modif[0] == 'i') {
526946d70077SConrad Meyer 		fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n";
527046d70077SConrad Meyer 		fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n";
527146d70077SConrad Meyer 	} else {
527246d70077SConrad Meyer 		fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n";
527346d70077SConrad Meyer 		fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n";
527446d70077SConrad Meyer 	}
527546d70077SConrad Meyer 
527646d70077SConrad Meyer 	db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests",
527746d70077SConrad Meyer 	    "Sleeps", "Bucket", "Total Mem", "XFree");
527846d70077SConrad Meyer 
527946d70077SConrad Meyer 	/* Sort the zones with largest size first. */
528046d70077SConrad Meyer 	last_zone = NULL;
528146d70077SConrad Meyer 	last_size = INT64_MAX;
528246d70077SConrad Meyer 	for (;;) {
528346d70077SConrad Meyer 		cur_zone = NULL;
528446d70077SConrad Meyer 		cur_size = -1;
528546d70077SConrad Meyer 		ties = 0;
528646d70077SConrad Meyer 		LIST_FOREACH(kz, &uma_kegs, uk_link) {
528746d70077SConrad Meyer 			LIST_FOREACH(z, &kz->uk_zones, uz_link) {
528846d70077SConrad Meyer 				/*
528946d70077SConrad Meyer 				 * In the case of size ties, print out zones
529046d70077SConrad Meyer 				 * in the order they are encountered.  That is,
529146d70077SConrad Meyer 				 * when we encounter the most recently output
529246d70077SConrad Meyer 				 * zone, we have already printed all preceding
529346d70077SConrad Meyer 				 * ties, and we must print all following ties.
529446d70077SConrad Meyer 				 */
529546d70077SConrad Meyer 				if (z == last_zone) {
529646d70077SConrad Meyer 					ties = 1;
529746d70077SConrad Meyer 					continue;
529846d70077SConrad Meyer 				}
529946d70077SConrad Meyer 				size = get_uma_stats(kz, z, &allocs, &used,
530046d70077SConrad Meyer 				    &sleeps, &cachefree, &xdomain);
530146d70077SConrad Meyer 				if (size > cur_size && size < last_size + ties)
530246d70077SConrad Meyer 				{
530346d70077SConrad Meyer 					cur_size = size;
530446d70077SConrad Meyer 					cur_zone = z;
530546d70077SConrad Meyer 					cur_keg = kz;
530646d70077SConrad Meyer 				}
530746d70077SConrad Meyer 			}
530846d70077SConrad Meyer 		}
530946d70077SConrad Meyer 		if (cur_zone == NULL)
531046d70077SConrad Meyer 			break;
531146d70077SConrad Meyer 
531246d70077SConrad Meyer 		size = get_uma_stats(cur_keg, cur_zone, &allocs, &used,
531346d70077SConrad Meyer 		    &sleeps, &cachefree, &xdomain);
531446d70077SConrad Meyer 		db_printf(fmt_entry, cur_zone->uz_name,
531546d70077SConrad Meyer 		    (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree,
531646d70077SConrad Meyer 		    (uintmax_t)allocs, (uintmax_t)sleeps,
531720a4e154SJeff Roberson 		    (unsigned)cur_zone->uz_bucket_size, (intmax_t)size,
531820a4e154SJeff Roberson 		    xdomain);
531946d70077SConrad Meyer 
5320687c94aaSJohn Baldwin 		if (db_pager_quit)
5321687c94aaSJohn Baldwin 			return;
532246d70077SConrad Meyer 		last_zone = cur_zone;
532346d70077SConrad Meyer 		last_size = cur_size;
532448c5777eSRobert Watson 	}
532548c5777eSRobert Watson }
532603175483SAlexander Motin 
532703175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache)
532803175483SAlexander Motin {
532903175483SAlexander Motin 	uma_zone_t z;
5330ab3185d1SJeff Roberson 	uint64_t allocs, frees;
53310f9b7bf3SMark Johnston 	long cachefree;
53320f9b7bf3SMark Johnston 	int i;
533303175483SAlexander Motin 
533403175483SAlexander Motin 	db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
533503175483SAlexander Motin 	    "Requests", "Bucket");
533603175483SAlexander Motin 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
5337c1685086SJeff Roberson 		uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL);
53380f9b7bf3SMark Johnston 		for (i = 0; i < vm_ndomains; i++)
53390f9b7bf3SMark Johnston 			cachefree += z->uz_domain[i].uzd_nitems;
53400f9b7bf3SMark Johnston 		db_printf("%18s %8ju %8jd %8ld %12ju %8u\n",
534103175483SAlexander Motin 		    z->uz_name, (uintmax_t)z->uz_size,
534203175483SAlexander Motin 		    (intmax_t)(allocs - frees), cachefree,
534320a4e154SJeff Roberson 		    (uintmax_t)allocs, z->uz_bucket_size);
534403175483SAlexander Motin 		if (db_pager_quit)
534503175483SAlexander Motin 			return;
534603175483SAlexander Motin 	}
534703175483SAlexander Motin }
53489542ea7bSGleb Smirnoff #endif	/* DDB */
5349