xref: /freebsd/sys/vm/uma_core.c (revision ed581bf68f8b81bc1ac6815257bc529c09bfe775)
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 
556543117beSJeff 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;
562543117beSJeff 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);
682543117beSJeff 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);
984543117beSJeff 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;
1019543117beSJeff 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 	 */
1030543117beSJeff Roberson 	seq = SMR_SEQ_INVALID;
1031543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
1032543117beSJeff 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) {
1042543117beSJeff 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) {
1048543117beSJeff 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 	 */
1092543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0) {
1093376b1ba3SJeff Roberson 		b2 = cache_bucket_unload_free(cache);
1094543117beSJeff Roberson 		b3 = cache_bucket_unload_cross(cache);
1095543117beSJeff Roberson 	}
1096543117beSJeff Roberson 	critical_exit();
1097543117beSJeff Roberson 
1098543117beSJeff Roberson 	ZONE_LOCK(zone);
1099543117beSJeff Roberson 	if (b1 != NULL && b1->ub_cnt != 0) {
1100543117beSJeff Roberson 		zone_put_bucket(zone, &zone->uz_domain[domain], b1, false);
1101543117beSJeff Roberson 		b1 = NULL;
1102543117beSJeff 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);
1108543117beSJeff Roberson 
1109543117beSJeff Roberson 	if (b1 != NULL)
11108a8d9d14SAlexander Motin 		bucket_free(zone, b1, NULL);
1111543117beSJeff Roberson 	if (b2 != NULL)
11128a8d9d14SAlexander Motin 		bucket_free(zone, b2, NULL);
1113543117beSJeff 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;
1207cefc92e1SMark Johnston 			zone->uz_bkt_count -= tofree;
120808cfa56eSMark Johnston 
120908cfa56eSMark Johnston 			/*
121008cfa56eSMark Johnston 			 * Shift the bounds of the current WSS interval to avoid
121108cfa56eSMark Johnston 			 * perturbing the estimate.
121208cfa56eSMark Johnston 			 */
121308cfa56eSMark Johnston 			zdom->uzd_imax -= lmin(zdom->uzd_imax, tofree);
121408cfa56eSMark Johnston 			zdom->uzd_imin -= lmin(zdom->uzd_imin, tofree);
121508cfa56eSMark Johnston 
12168355f576SJeff Roberson 			ZONE_UNLOCK(zone);
12178355f576SJeff Roberson 			bucket_drain(zone, bucket);
12186fd34d6fSJeff Roberson 			bucket_free(zone, bucket, NULL);
12198355f576SJeff Roberson 			ZONE_LOCK(zone);
12208355f576SJeff Roberson 		}
122191d947bfSJeff Roberson 		ZONE_UNLOCK(zone);
1222ab3185d1SJeff Roberson 	}
12238355f576SJeff Roberson }
1224fc03d22bSJeff Roberson 
1225fc03d22bSJeff Roberson static void
1226fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
1227fc03d22bSJeff Roberson {
1228fc03d22bSJeff Roberson 	uint8_t *mem;
1229fc03d22bSJeff Roberson 	int i;
1230fc03d22bSJeff Roberson 	uint8_t flags;
1231fc03d22bSJeff Roberson 
12321431a748SGleb Smirnoff 	CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes",
12331431a748SGleb Smirnoff 	    keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera);
12341431a748SGleb Smirnoff 
12351e0701e1SJeff Roberson 	mem = slab_data(slab, keg);
1236fc03d22bSJeff Roberson 	flags = slab->us_flags;
1237fc03d22bSJeff Roberson 	i = start;
1238fc03d22bSJeff Roberson 	if (keg->uk_fini != NULL) {
1239fc03d22bSJeff Roberson 		for (i--; i > -1; i--)
1240c5deaf04SGleb Smirnoff #ifdef INVARIANTS
1241c5deaf04SGleb Smirnoff 		/*
1242c5deaf04SGleb Smirnoff 		 * trash_fini implies that dtor was trash_dtor. trash_fini
1243c5deaf04SGleb Smirnoff 		 * would check that memory hasn't been modified since free,
1244c5deaf04SGleb Smirnoff 		 * which executed trash_dtor.
1245c5deaf04SGleb Smirnoff 		 * That's why we need to run uma_dbg_kskip() check here,
1246c5deaf04SGleb Smirnoff 		 * albeit we don't make skip check for other init/fini
1247c5deaf04SGleb Smirnoff 		 * invocations.
1248c5deaf04SGleb Smirnoff 		 */
12491e0701e1SJeff Roberson 		if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) ||
1250c5deaf04SGleb Smirnoff 		    keg->uk_fini != trash_fini)
1251c5deaf04SGleb Smirnoff #endif
12521e0701e1SJeff Roberson 			keg->uk_fini(slab_item(slab, keg, i), keg->uk_size);
1253fc03d22bSJeff Roberson 	}
125454c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_OFFPAGE)
12559b8db4d0SRyan Libby 		zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab),
12569b8db4d0SRyan Libby 		    NULL, SKIP_NONE);
1257fc03d22bSJeff Roberson 	keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
12582e47807cSJeff Roberson 	uma_total_dec(PAGE_SIZE * keg->uk_ppera);
12598355f576SJeff Roberson }
12608355f576SJeff Roberson 
12618355f576SJeff Roberson /*
1262e20a199fSJeff Roberson  * Frees pages from a keg back to the system.  This is done on demand from
12638355f576SJeff Roberson  * the pageout daemon.
12648355f576SJeff Roberson  *
1265e20a199fSJeff Roberson  * Returns nothing.
12668355f576SJeff Roberson  */
1267e20a199fSJeff Roberson static void
1268e20a199fSJeff Roberson keg_drain(uma_keg_t keg)
12698355f576SJeff Roberson {
12704ab3aee8SMark Johnston 	struct slabhead freeslabs;
1271ab3185d1SJeff Roberson 	uma_domain_t dom;
1272829be516SMark Johnston 	uma_slab_t slab, tmp;
12738b987a77SJeff Roberson 	int i, n;
12748355f576SJeff Roberson 
1275099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
12768355f576SJeff Roberson 		return;
12778355f576SJeff Roberson 
1278ab3185d1SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
12798b987a77SJeff Roberson 		CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u",
12804ab3aee8SMark Johnston 		    keg->uk_name, keg, i, dom->ud_free_items);
1281ab3185d1SJeff Roberson 		dom = &keg->uk_domain[i];
12824ab3aee8SMark Johnston 		LIST_INIT(&freeslabs);
1283ab3185d1SJeff Roberson 
12844ab3aee8SMark Johnston 		KEG_LOCK(keg, i);
12854ab3aee8SMark Johnston 		if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) {
12864ab3aee8SMark Johnston 			LIST_FOREACH(slab, &dom->ud_free_slab, us_link)
12874ab3aee8SMark Johnston 				UMA_HASH_REMOVE(&keg->uk_hash, slab);
12884ab3aee8SMark Johnston 		}
12894ab3aee8SMark Johnston 		n = dom->ud_free_slabs;
12904ab3aee8SMark Johnston 		LIST_SWAP(&freeslabs, &dom->ud_free_slab, uma_slab, us_link);
12914ab3aee8SMark Johnston 		dom->ud_free_slabs = 0;
12924ab3aee8SMark Johnston 		dom->ud_free_items -= n * keg->uk_ipers;
12934ab3aee8SMark Johnston 		dom->ud_pages -= n * keg->uk_ppera;
12944ab3aee8SMark Johnston 		KEG_UNLOCK(keg, i);
12954ab3aee8SMark Johnston 
12964ab3aee8SMark Johnston 		LIST_FOREACH_SAFE(slab, &freeslabs, us_link, tmp)
12971645995bSKirk McKusick 			keg_free_slab(keg, slab, keg->uk_ipers);
12988355f576SJeff Roberson 	}
12998355f576SJeff Roberson }
13008355f576SJeff Roberson 
1301e20a199fSJeff Roberson static void
130208cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain)
1303e20a199fSJeff Roberson {
1304e20a199fSJeff Roberson 
13058355f576SJeff Roberson 	/*
1306e20a199fSJeff Roberson 	 * Set draining to interlock with zone_dtor() so we can release our
1307e20a199fSJeff Roberson 	 * locks as we go.  Only dtor() should do a WAITOK call since it
1308e20a199fSJeff Roberson 	 * is the only call that knows the structure will still be available
1309e20a199fSJeff Roberson 	 * when it wakes up.
1310e20a199fSJeff Roberson 	 */
1311e20a199fSJeff Roberson 	ZONE_LOCK(zone);
131208cfa56eSMark Johnston 	while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) {
1313e20a199fSJeff Roberson 		if (waitok == M_NOWAIT)
1314e20a199fSJeff Roberson 			goto out;
1315727c6918SJeff Roberson 		msleep(zone, &zone->uz_lock, PVM, "zonedrain", 1);
1316e20a199fSJeff Roberson 	}
131708cfa56eSMark Johnston 	zone->uz_flags |= UMA_ZFLAG_RECLAIMING;
1318e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
131991d947bfSJeff Roberson 	bucket_cache_reclaim(zone, drain);
132008cfa56eSMark Johnston 
1321e20a199fSJeff Roberson 	/*
1322e20a199fSJeff Roberson 	 * The DRAINING flag protects us from being freed while
1323111fbcd5SBryan Venteicher 	 * we're running.  Normally the uma_rwlock would protect us but we
1324e20a199fSJeff Roberson 	 * must be able to release and acquire the right lock for each keg.
1325e20a199fSJeff Roberson 	 */
132608034d10SKonstantin Belousov 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0)
1327bb15d1c7SGleb Smirnoff 		keg_drain(zone->uz_keg);
1328e20a199fSJeff Roberson 	ZONE_LOCK(zone);
132908cfa56eSMark Johnston 	zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING;
1330e20a199fSJeff Roberson 	wakeup(zone);
1331e20a199fSJeff Roberson out:
1332e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
1333e20a199fSJeff Roberson }
1334e20a199fSJeff Roberson 
133508cfa56eSMark Johnston static void
133620a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused)
1337e20a199fSJeff Roberson {
1338e20a199fSJeff Roberson 
133908cfa56eSMark Johnston 	zone_reclaim(zone, M_NOWAIT, true);
134008cfa56eSMark Johnston }
134108cfa56eSMark Johnston 
134208cfa56eSMark Johnston static void
134320a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused)
134408cfa56eSMark Johnston {
134508cfa56eSMark Johnston 
134608cfa56eSMark Johnston 	zone_reclaim(zone, M_NOWAIT, false);
1347e20a199fSJeff Roberson }
1348e20a199fSJeff Roberson 
1349e20a199fSJeff Roberson /*
13508b987a77SJeff Roberson  * Allocate a new slab for a keg and inserts it into the partial slab list.
13518b987a77SJeff Roberson  * The keg should be unlocked on entry.  If the allocation succeeds it will
13528b987a77SJeff Roberson  * be locked on return.
13538355f576SJeff Roberson  *
13548355f576SJeff Roberson  * Arguments:
135586220393SMark Johnston  *	flags   Wait flags for the item initialization routine
135686220393SMark Johnston  *	aflags  Wait flags for the slab allocation
13578355f576SJeff Roberson  *
13588355f576SJeff Roberson  * Returns:
13598355f576SJeff Roberson  *	The slab that was allocated or NULL if there is no memory and the
13608355f576SJeff Roberson  *	caller specified M_NOWAIT.
13618355f576SJeff Roberson  */
13628355f576SJeff Roberson static uma_slab_t
136386220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags,
136486220393SMark Johnston     int aflags)
13658355f576SJeff Roberson {
13668b987a77SJeff Roberson 	uma_domain_t dom;
1367e20a199fSJeff Roberson 	uma_alloc allocf;
1368099a0e58SBosko Milekic 	uma_slab_t slab;
13692e47807cSJeff Roberson 	unsigned long size;
137085dcf349SGleb Smirnoff 	uint8_t *mem;
137186220393SMark Johnston 	uint8_t sflags;
13728355f576SJeff Roberson 	int i;
13738355f576SJeff Roberson 
1374ab3185d1SJeff Roberson 	KASSERT(domain >= 0 && domain < vm_ndomains,
1375ab3185d1SJeff Roberson 	    ("keg_alloc_slab: domain %d out of range", domain));
1376a553d4b8SJeff Roberson 
13778b987a77SJeff Roberson 	allocf = keg->uk_allocf;
1378194a979eSMark Johnston 	slab = NULL;
1379194a979eSMark Johnston 	mem = NULL;
138054c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) {
13819b8db4d0SRyan Libby 		uma_hash_slab_t hslab;
13829b8db4d0SRyan Libby 		hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL,
13839b8db4d0SRyan Libby 		    domain, aflags);
13849b8db4d0SRyan Libby 		if (hslab == NULL)
1385727c6918SJeff Roberson 			goto fail;
13869b8db4d0SRyan Libby 		slab = &hslab->uhs_slab;
1387a553d4b8SJeff Roberson 	}
1388a553d4b8SJeff Roberson 
13893370c5bfSJeff Roberson 	/*
13903370c5bfSJeff Roberson 	 * This reproduces the old vm_zone behavior of zero filling pages the
13913370c5bfSJeff Roberson 	 * first time they are added to a zone.
13923370c5bfSJeff Roberson 	 *
13933370c5bfSJeff Roberson 	 * Malloced items are zeroed in uma_zalloc.
13943370c5bfSJeff Roberson 	 */
13953370c5bfSJeff Roberson 
1396099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
139786220393SMark Johnston 		aflags |= M_ZERO;
13983370c5bfSJeff Roberson 	else
139986220393SMark Johnston 		aflags &= ~M_ZERO;
14003370c5bfSJeff Roberson 
1401263811f7SKip Macy 	if (keg->uk_flags & UMA_ZONE_NODUMP)
140286220393SMark Johnston 		aflags |= M_NODUMP;
1403263811f7SKip Macy 
1404e20a199fSJeff Roberson 	/* zone is passed for legacy reasons. */
1405194a979eSMark Johnston 	size = keg->uk_ppera * PAGE_SIZE;
140686220393SMark Johnston 	mem = allocf(zone, size, domain, &sflags, aflags);
1407a553d4b8SJeff Roberson 	if (mem == NULL) {
140854c5ae80SRyan Libby 		if (keg->uk_flags & UMA_ZFLAG_OFFPAGE)
14099b8db4d0SRyan Libby 			zone_free_item(slabzone(keg->uk_ipers),
14109b8db4d0SRyan Libby 			    slab_tohashslab(slab), NULL, SKIP_NONE);
1411727c6918SJeff Roberson 		goto fail;
1412a553d4b8SJeff Roberson 	}
14132e47807cSJeff Roberson 	uma_total_inc(size);
14148355f576SJeff Roberson 
14158b987a77SJeff Roberson 	/* For HASH zones all pages go to the same uma_domain. */
141654c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0)
14178b987a77SJeff Roberson 		domain = 0;
14188b987a77SJeff Roberson 
14195c0e403bSJeff Roberson 	/* Point the slab into the allocated memory */
142054c5ae80SRyan Libby 	if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE))
1421099a0e58SBosko Milekic 		slab = (uma_slab_t )(mem + keg->uk_pgoff);
14221e0701e1SJeff Roberson 	else
14239b8db4d0SRyan Libby 		slab_tohashslab(slab)->uhs_data = mem;
14245c0e403bSJeff Roberson 
142554c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_VTOSLAB)
1426099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ppera; i++)
1427584061b4SJeff Roberson 			vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE),
1428584061b4SJeff Roberson 			    zone, slab);
14298355f576SJeff Roberson 
1430099a0e58SBosko Milekic 	slab->us_freecount = keg->uk_ipers;
143186220393SMark Johnston 	slab->us_flags = sflags;
1432ab3185d1SJeff Roberson 	slab->us_domain = domain;
14338b987a77SJeff Roberson 
14349b78b1f4SJeff Roberson 	BIT_FILL(keg->uk_ipers, &slab->us_free);
1435ef72505eSJeff Roberson #ifdef INVARIANTS
1436815db204SRyan Libby 	BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg));
1437ef72505eSJeff Roberson #endif
1438099a0e58SBosko Milekic 
1439b23f72e9SBrian Feldman 	if (keg->uk_init != NULL) {
1440099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ipers; i++)
14411e0701e1SJeff Roberson 			if (keg->uk_init(slab_item(slab, keg, i),
144286220393SMark Johnston 			    keg->uk_size, flags) != 0)
1443b23f72e9SBrian Feldman 				break;
1444b23f72e9SBrian Feldman 		if (i != keg->uk_ipers) {
1445fc03d22bSJeff Roberson 			keg_free_slab(keg, slab, i);
1446727c6918SJeff Roberson 			goto fail;
1447b23f72e9SBrian Feldman 		}
1448b23f72e9SBrian Feldman 	}
14498b987a77SJeff Roberson 	KEG_LOCK(keg, domain);
14505c0e403bSJeff Roberson 
14511431a748SGleb Smirnoff 	CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)",
14521431a748SGleb Smirnoff 	    slab, keg->uk_name, keg);
14531431a748SGleb Smirnoff 
145454c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_HASH)
1455099a0e58SBosko Milekic 		UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
14568355f576SJeff Roberson 
14578b987a77SJeff Roberson 	/*
14588b987a77SJeff Roberson 	 * If we got a slab here it's safe to mark it partially used
14598b987a77SJeff Roberson 	 * and return.  We assume that the caller is going to remove
14608b987a77SJeff Roberson 	 * at least one item.
14618b987a77SJeff Roberson 	 */
14628b987a77SJeff Roberson 	dom = &keg->uk_domain[domain];
14638b987a77SJeff Roberson 	LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
14648b987a77SJeff Roberson 	dom->ud_pages += keg->uk_ppera;
14654ab3aee8SMark Johnston 	dom->ud_free_items += keg->uk_ipers;
14668355f576SJeff Roberson 
14678355f576SJeff Roberson 	return (slab);
1468727c6918SJeff Roberson 
1469727c6918SJeff Roberson fail:
1470727c6918SJeff Roberson 	return (NULL);
14718355f576SJeff Roberson }
14728355f576SJeff Roberson 
14738355f576SJeff Roberson /*
1474009b6fcbSJeff Roberson  * This function is intended to be used early on in place of page_alloc() so
1475009b6fcbSJeff Roberson  * that we may use the boot time page cache to satisfy allocations before
1476009b6fcbSJeff Roberson  * the VM is ready.
1477009b6fcbSJeff Roberson  */
1478009b6fcbSJeff Roberson static void *
1479ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1480ab3185d1SJeff Roberson     int wait)
1481009b6fcbSJeff Roberson {
1482a81c400eSJeff Roberson 	vm_paddr_t pa;
1483a81c400eSJeff Roberson 	vm_page_t m;
1484ac0a6fd0SGleb Smirnoff 	void *mem;
1485ac0a6fd0SGleb Smirnoff 	int pages;
1486a81c400eSJeff Roberson 	int i;
1487099a0e58SBosko Milekic 
1488f7d35785SGleb Smirnoff 	pages = howmany(bytes, PAGE_SIZE);
1489f7d35785SGleb Smirnoff 	KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__));
1490a81c400eSJeff Roberson 
1491f7d35785SGleb Smirnoff 	*pflag = UMA_SLAB_BOOT;
1492a81c400eSJeff Roberson 	m = vm_page_alloc_contig_domain(NULL, 0, domain,
1493a81c400eSJeff Roberson 	    malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages,
1494a81c400eSJeff Roberson 	    (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT);
1495a81c400eSJeff Roberson 	if (m == NULL)
1496a81c400eSJeff Roberson 		return (NULL);
1497a81c400eSJeff Roberson 
1498a81c400eSJeff Roberson 	pa = VM_PAGE_TO_PHYS(m);
1499a81c400eSJeff Roberson 	for (i = 0; i < pages; i++, pa += PAGE_SIZE) {
1500a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
1501a81c400eSJeff Roberson     defined(__riscv) || defined(__powerpc64__)
1502a81c400eSJeff Roberson 		if ((wait & M_NODUMP) == 0)
1503a81c400eSJeff Roberson 			dump_add_page(pa);
1504a81c400eSJeff Roberson #endif
1505a81c400eSJeff Roberson 	}
1506a81c400eSJeff Roberson 	/* Allocate KVA and indirectly advance bootmem. */
1507a81c400eSJeff Roberson 	mem = (void *)pmap_map(&bootmem, m->phys_addr,
1508a81c400eSJeff Roberson 	    m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE);
1509a81c400eSJeff Roberson         if ((wait & M_ZERO) != 0)
1510a81c400eSJeff Roberson                 bzero(mem, pages * PAGE_SIZE);
1511f7d35785SGleb Smirnoff 
1512f7d35785SGleb Smirnoff         return (mem);
1513f7d35785SGleb Smirnoff }
1514f7d35785SGleb Smirnoff 
1515a81c400eSJeff Roberson static void
1516a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes)
1517a81c400eSJeff Roberson {
1518a81c400eSJeff Roberson 	vm_offset_t va;
1519a81c400eSJeff Roberson 	vm_page_t m;
1520a81c400eSJeff Roberson 
1521a81c400eSJeff Roberson 	va = (vm_offset_t)mem;
1522a81c400eSJeff Roberson 	m = PHYS_TO_VM_PAGE(pmap_kextract(va));
1523a81c400eSJeff Roberson 	pmap_remove(kernel_pmap, va, va + bytes);
1524a81c400eSJeff Roberson 	for (; bytes != 0; bytes -= PAGE_SIZE, m++) {
1525a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
1526a81c400eSJeff Roberson     defined(__riscv) || defined(__powerpc64__)
1527a81c400eSJeff Roberson 		dump_drop_page(VM_PAGE_TO_PHYS(m));
1528a81c400eSJeff Roberson #endif
1529a81c400eSJeff Roberson 		vm_page_unwire_noq(m);
1530a81c400eSJeff Roberson 		vm_page_free(m);
1531a81c400eSJeff Roberson 	}
1532a81c400eSJeff Roberson }
1533a81c400eSJeff Roberson 
1534f7d35785SGleb Smirnoff /*
15358355f576SJeff Roberson  * Allocates a number of pages from the system
15368355f576SJeff Roberson  *
15378355f576SJeff Roberson  * Arguments:
15388355f576SJeff Roberson  *	bytes  The number of bytes requested
15398355f576SJeff Roberson  *	wait  Shall we wait?
15408355f576SJeff Roberson  *
15418355f576SJeff Roberson  * Returns:
15428355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
15438355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
15448355f576SJeff Roberson  */
15458355f576SJeff Roberson static void *
1546ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1547ab3185d1SJeff Roberson     int wait)
15488355f576SJeff Roberson {
15498355f576SJeff Roberson 	void *p;	/* Returned page */
15508355f576SJeff Roberson 
15512e47807cSJeff Roberson 	*pflag = UMA_SLAB_KERNEL;
15529978bd99SMark Johnston 	p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait);
15538355f576SJeff Roberson 
15548355f576SJeff Roberson 	return (p);
15558355f576SJeff Roberson }
15568355f576SJeff Roberson 
1557ab3059a8SMatt Macy static void *
1558ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1559ab3059a8SMatt Macy     int wait)
1560ab3059a8SMatt Macy {
1561ab3059a8SMatt Macy 	struct pglist alloctail;
1562ab3059a8SMatt Macy 	vm_offset_t addr, zkva;
1563ab3059a8SMatt Macy 	int cpu, flags;
1564ab3059a8SMatt Macy 	vm_page_t p, p_next;
1565ab3059a8SMatt Macy #ifdef NUMA
1566ab3059a8SMatt Macy 	struct pcpu *pc;
1567ab3059a8SMatt Macy #endif
1568ab3059a8SMatt Macy 
1569ab3059a8SMatt Macy 	MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE);
1570ab3059a8SMatt Macy 
1571013072f0SMark Johnston 	TAILQ_INIT(&alloctail);
1572ab3059a8SMatt Macy 	flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1573013072f0SMark Johnston 	    malloc2vm_flags(wait);
1574013072f0SMark Johnston 	*pflag = UMA_SLAB_KERNEL;
1575ab3059a8SMatt Macy 	for (cpu = 0; cpu <= mp_maxid; cpu++) {
1576ab3059a8SMatt Macy 		if (CPU_ABSENT(cpu)) {
1577ab3059a8SMatt Macy 			p = vm_page_alloc(NULL, 0, flags);
1578ab3059a8SMatt Macy 		} else {
1579ab3059a8SMatt Macy #ifndef NUMA
1580ab3059a8SMatt Macy 			p = vm_page_alloc(NULL, 0, flags);
1581ab3059a8SMatt Macy #else
1582ab3059a8SMatt Macy 			pc = pcpu_find(cpu);
158320526802SAndrew Gallatin 			if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain)))
158420526802SAndrew Gallatin 				p = NULL;
158520526802SAndrew Gallatin 			else
158620526802SAndrew Gallatin 				p = vm_page_alloc_domain(NULL, 0,
158720526802SAndrew Gallatin 				    pc->pc_domain, flags);
1588ab3059a8SMatt Macy 			if (__predict_false(p == NULL))
1589ab3059a8SMatt Macy 				p = vm_page_alloc(NULL, 0, flags);
1590ab3059a8SMatt Macy #endif
1591ab3059a8SMatt Macy 		}
1592ab3059a8SMatt Macy 		if (__predict_false(p == NULL))
1593ab3059a8SMatt Macy 			goto fail;
1594ab3059a8SMatt Macy 		TAILQ_INSERT_TAIL(&alloctail, p, listq);
1595ab3059a8SMatt Macy 	}
1596ab3059a8SMatt Macy 	if ((addr = kva_alloc(bytes)) == 0)
1597ab3059a8SMatt Macy 		goto fail;
1598ab3059a8SMatt Macy 	zkva = addr;
1599ab3059a8SMatt Macy 	TAILQ_FOREACH(p, &alloctail, listq) {
1600ab3059a8SMatt Macy 		pmap_qenter(zkva, &p, 1);
1601ab3059a8SMatt Macy 		zkva += PAGE_SIZE;
1602ab3059a8SMatt Macy 	}
1603ab3059a8SMatt Macy 	return ((void*)addr);
1604ab3059a8SMatt Macy fail:
1605ab3059a8SMatt Macy 	TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
160688ea538aSMark Johnston 		vm_page_unwire_noq(p);
1607ab3059a8SMatt Macy 		vm_page_free(p);
1608ab3059a8SMatt Macy 	}
1609ab3059a8SMatt Macy 	return (NULL);
1610ab3059a8SMatt Macy }
1611ab3059a8SMatt Macy 
16128355f576SJeff Roberson /*
16138355f576SJeff Roberson  * Allocates a number of pages from within an object
16148355f576SJeff Roberson  *
16158355f576SJeff Roberson  * Arguments:
16168355f576SJeff Roberson  *	bytes  The number of bytes requested
16178355f576SJeff Roberson  *	wait   Shall we wait?
16188355f576SJeff Roberson  *
16198355f576SJeff Roberson  * Returns:
16208355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
16218355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
16228355f576SJeff Roberson  */
16238355f576SJeff Roberson static void *
1624ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
1625ab3185d1SJeff Roberson     int wait)
16268355f576SJeff Roberson {
1627a4915c21SAttilio Rao 	TAILQ_HEAD(, vm_page) alloctail;
1628a4915c21SAttilio Rao 	u_long npages;
1629b245ac95SAlan Cox 	vm_offset_t retkva, zkva;
1630a4915c21SAttilio Rao 	vm_page_t p, p_next;
1631e20a199fSJeff Roberson 	uma_keg_t keg;
16328355f576SJeff Roberson 
1633a4915c21SAttilio Rao 	TAILQ_INIT(&alloctail);
1634bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
1635a4915c21SAttilio Rao 
1636a4915c21SAttilio Rao 	npages = howmany(bytes, PAGE_SIZE);
1637a4915c21SAttilio Rao 	while (npages > 0) {
1638ab3185d1SJeff Roberson 		p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT |
16398d6fbbb8SJeff Roberson 		    VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1640772c8b67SKonstantin Belousov 		    ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK :
1641772c8b67SKonstantin Belousov 		    VM_ALLOC_NOWAIT));
1642a4915c21SAttilio Rao 		if (p != NULL) {
1643a4915c21SAttilio Rao 			/*
1644a4915c21SAttilio Rao 			 * Since the page does not belong to an object, its
1645a4915c21SAttilio Rao 			 * listq is unused.
1646a4915c21SAttilio Rao 			 */
1647a4915c21SAttilio Rao 			TAILQ_INSERT_TAIL(&alloctail, p, listq);
1648a4915c21SAttilio Rao 			npages--;
1649a4915c21SAttilio Rao 			continue;
1650a4915c21SAttilio Rao 		}
16518355f576SJeff Roberson 		/*
1652a4915c21SAttilio Rao 		 * Page allocation failed, free intermediate pages and
1653a4915c21SAttilio Rao 		 * exit.
16548355f576SJeff Roberson 		 */
1655a4915c21SAttilio Rao 		TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
165688ea538aSMark Johnston 			vm_page_unwire_noq(p);
1657b245ac95SAlan Cox 			vm_page_free(p);
1658b245ac95SAlan Cox 		}
1659a4915c21SAttilio Rao 		return (NULL);
1660b245ac95SAlan Cox 	}
16618355f576SJeff Roberson 	*flags = UMA_SLAB_PRIV;
1662a4915c21SAttilio Rao 	zkva = keg->uk_kva +
1663a4915c21SAttilio Rao 	    atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1664a4915c21SAttilio Rao 	retkva = zkva;
1665a4915c21SAttilio Rao 	TAILQ_FOREACH(p, &alloctail, listq) {
1666a4915c21SAttilio Rao 		pmap_qenter(zkva, &p, 1);
1667a4915c21SAttilio Rao 		zkva += PAGE_SIZE;
1668a4915c21SAttilio Rao 	}
16698355f576SJeff Roberson 
16708355f576SJeff Roberson 	return ((void *)retkva);
16718355f576SJeff Roberson }
16728355f576SJeff Roberson 
16738355f576SJeff Roberson /*
1674ec0d8280SRyan Libby  * Allocate physically contiguous pages.
1675ec0d8280SRyan Libby  */
1676ec0d8280SRyan Libby static void *
1677ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1678ec0d8280SRyan Libby     int wait)
1679ec0d8280SRyan Libby {
1680ec0d8280SRyan Libby 
1681ec0d8280SRyan Libby 	*pflag = UMA_SLAB_KERNEL;
1682ec0d8280SRyan Libby 	return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain),
1683ec0d8280SRyan Libby 	    bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT));
1684ec0d8280SRyan Libby }
1685ec0d8280SRyan Libby 
1686ec0d8280SRyan Libby /*
16878355f576SJeff Roberson  * Frees a number of pages to the system
16888355f576SJeff Roberson  *
16898355f576SJeff Roberson  * Arguments:
16908355f576SJeff Roberson  *	mem   A pointer to the memory to be freed
16918355f576SJeff Roberson  *	size  The size of the memory being freed
16928355f576SJeff Roberson  *	flags The original p->us_flags field
16938355f576SJeff Roberson  *
16948355f576SJeff Roberson  * Returns:
16958355f576SJeff Roberson  *	Nothing
16968355f576SJeff Roberson  */
16978355f576SJeff Roberson static void
1698f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags)
16998355f576SJeff Roberson {
17003370c5bfSJeff Roberson 
1701a81c400eSJeff Roberson 	if ((flags & UMA_SLAB_BOOT) != 0) {
1702a81c400eSJeff Roberson 		startup_free(mem, size);
1703a81c400eSJeff Roberson 		return;
1704a81c400eSJeff Roberson 	}
1705a81c400eSJeff Roberson 
1706ec0d8280SRyan Libby 	KASSERT((flags & UMA_SLAB_KERNEL) != 0,
1707ec0d8280SRyan Libby 	    ("UMA: page_free used with invalid flags %x", flags));
17088355f576SJeff Roberson 
170949bfa624SAlan Cox 	kmem_free((vm_offset_t)mem, size);
17108355f576SJeff Roberson }
17118355f576SJeff Roberson 
17128355f576SJeff Roberson /*
1713ab3059a8SMatt Macy  * Frees pcpu zone allocations
1714ab3059a8SMatt Macy  *
1715ab3059a8SMatt Macy  * Arguments:
1716ab3059a8SMatt Macy  *	mem   A pointer to the memory to be freed
1717ab3059a8SMatt Macy  *	size  The size of the memory being freed
1718ab3059a8SMatt Macy  *	flags The original p->us_flags field
1719ab3059a8SMatt Macy  *
1720ab3059a8SMatt Macy  * Returns:
1721ab3059a8SMatt Macy  *	Nothing
1722ab3059a8SMatt Macy  */
1723ab3059a8SMatt Macy static void
1724ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags)
1725ab3059a8SMatt Macy {
1726ab3059a8SMatt Macy 	vm_offset_t sva, curva;
1727ab3059a8SMatt Macy 	vm_paddr_t paddr;
1728ab3059a8SMatt Macy 	vm_page_t m;
1729ab3059a8SMatt Macy 
1730ab3059a8SMatt Macy 	MPASS(size == (mp_maxid+1)*PAGE_SIZE);
17315ba16cf3SRyan Libby 
17325ba16cf3SRyan Libby 	if ((flags & UMA_SLAB_BOOT) != 0) {
17335ba16cf3SRyan Libby 		startup_free(mem, size);
17345ba16cf3SRyan Libby 		return;
17355ba16cf3SRyan Libby 	}
17365ba16cf3SRyan Libby 
1737ab3059a8SMatt Macy 	sva = (vm_offset_t)mem;
1738ab3059a8SMatt Macy 	for (curva = sva; curva < sva + size; curva += PAGE_SIZE) {
1739ab3059a8SMatt Macy 		paddr = pmap_kextract(curva);
1740ab3059a8SMatt Macy 		m = PHYS_TO_VM_PAGE(paddr);
174188ea538aSMark Johnston 		vm_page_unwire_noq(m);
1742ab3059a8SMatt Macy 		vm_page_free(m);
1743ab3059a8SMatt Macy 	}
1744ab3059a8SMatt Macy 	pmap_qremove(sva, size >> PAGE_SHIFT);
1745ab3059a8SMatt Macy 	kva_free(sva, size);
1746ab3059a8SMatt Macy }
1747ab3059a8SMatt Macy 
1748ab3059a8SMatt Macy 
1749ab3059a8SMatt Macy /*
17508355f576SJeff Roberson  * Zero fill initializer
17518355f576SJeff Roberson  *
17528355f576SJeff Roberson  * Arguments/Returns follow uma_init specifications
17538355f576SJeff Roberson  */
1754b23f72e9SBrian Feldman static int
1755b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags)
17568355f576SJeff Roberson {
17578355f576SJeff Roberson 	bzero(mem, size);
1758b23f72e9SBrian Feldman 	return (0);
17598355f576SJeff Roberson }
17608355f576SJeff Roberson 
1761815db204SRyan Libby #ifdef INVARIANTS
1762815db204SRyan Libby struct noslabbits *
1763815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg)
1764815db204SRyan Libby {
1765815db204SRyan Libby 
1766815db204SRyan Libby 	return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers)));
1767815db204SRyan Libby }
1768815db204SRyan Libby #endif
1769815db204SRyan Libby 
17708355f576SJeff Roberson /*
17719b78b1f4SJeff Roberson  * Actual size of embedded struct slab (!OFFPAGE).
17729b78b1f4SJeff Roberson  */
17739b78b1f4SJeff Roberson size_t
17749b78b1f4SJeff Roberson slab_sizeof(int nitems)
17759b78b1f4SJeff Roberson {
17769b78b1f4SJeff Roberson 	size_t s;
17779b78b1f4SJeff Roberson 
1778815db204SRyan Libby 	s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS;
17799b78b1f4SJeff Roberson 	return (roundup(s, UMA_ALIGN_PTR + 1));
17809b78b1f4SJeff Roberson }
17819b78b1f4SJeff Roberson 
17829b78b1f4SJeff Roberson /*
17839b78b1f4SJeff Roberson  * Size of memory for embedded slabs (!OFFPAGE).
17849b78b1f4SJeff Roberson  */
17859b78b1f4SJeff Roberson size_t
17869b78b1f4SJeff Roberson slab_space(int nitems)
17879b78b1f4SJeff Roberson {
17889b78b1f4SJeff Roberson 	return (UMA_SLAB_SIZE - slab_sizeof(nitems));
17899b78b1f4SJeff Roberson }
17909b78b1f4SJeff Roberson 
17914a8b575cSRyan Libby #define	UMA_FIXPT_SHIFT	31
17924a8b575cSRyan Libby #define	UMA_FRAC_FIXPT(n, d)						\
17934a8b575cSRyan Libby 	((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d)))
17944a8b575cSRyan Libby #define	UMA_FIXPT_PCT(f)						\
17954a8b575cSRyan Libby 	((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT))
17964a8b575cSRyan Libby #define	UMA_PCT_FIXPT(pct)	UMA_FRAC_FIXPT((pct), 100)
17974a8b575cSRyan Libby #define	UMA_MIN_EFF	UMA_PCT_FIXPT(100 - UMA_MAX_WASTE)
17984a8b575cSRyan Libby 
17999b78b1f4SJeff Roberson /*
18004a8b575cSRyan Libby  * Compute the number of items that will fit in a slab.  If hdr is true, the
18014a8b575cSRyan Libby  * item count may be limited to provide space in the slab for an inline slab
18024a8b575cSRyan Libby  * header.  Otherwise, all slab space will be provided for item storage.
18034a8b575cSRyan Libby  */
18044a8b575cSRyan Libby static u_int
18054a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr)
18064a8b575cSRyan Libby {
18074a8b575cSRyan Libby 	u_int ipers;
18084a8b575cSRyan Libby 	u_int padpi;
18094a8b575cSRyan Libby 
18104a8b575cSRyan Libby 	/* The padding between items is not needed after the last item. */
18114a8b575cSRyan Libby 	padpi = rsize - size;
18124a8b575cSRyan Libby 
18134a8b575cSRyan Libby 	if (hdr) {
18144a8b575cSRyan Libby 		/*
18154a8b575cSRyan Libby 		 * Start with the maximum item count and remove items until
18164a8b575cSRyan Libby 		 * the slab header first alongside the allocatable memory.
18174a8b575cSRyan Libby 		 */
18184a8b575cSRyan Libby 		for (ipers = MIN(SLAB_MAX_SETSIZE,
18194a8b575cSRyan Libby 		    (slabsize + padpi - slab_sizeof(1)) / rsize);
18204a8b575cSRyan Libby 		    ipers > 0 &&
18214a8b575cSRyan Libby 		    ipers * rsize - padpi + slab_sizeof(ipers) > slabsize;
18224a8b575cSRyan Libby 		    ipers--)
18234a8b575cSRyan Libby 			continue;
18244a8b575cSRyan Libby 	} else {
18254a8b575cSRyan Libby 		ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE);
18264a8b575cSRyan Libby 	}
18274a8b575cSRyan Libby 
18284a8b575cSRyan Libby 	return (ipers);
18294a8b575cSRyan Libby }
18304a8b575cSRyan Libby 
18314a8b575cSRyan Libby /*
18324a8b575cSRyan Libby  * Compute the number of items that will fit in a slab for a startup zone.
18339b78b1f4SJeff Roberson  */
18349b78b1f4SJeff Roberson int
18359b78b1f4SJeff Roberson slab_ipers(size_t size, int align)
18369b78b1f4SJeff Roberson {
18379b78b1f4SJeff Roberson 	int rsize;
18389b78b1f4SJeff Roberson 
18394a8b575cSRyan Libby 	rsize = roundup(size, align + 1); /* Assume no CACHESPREAD */
18404a8b575cSRyan Libby 	return (slab_ipers_hdr(size, rsize, UMA_SLAB_SIZE, true));
18419b78b1f4SJeff Roberson }
18429b78b1f4SJeff Roberson 
184327ca37acSRyan Libby struct keg_layout_result {
184427ca37acSRyan Libby 	u_int format;
184527ca37acSRyan Libby 	u_int slabsize;
184627ca37acSRyan Libby 	u_int ipers;
184727ca37acSRyan Libby 	u_int eff;
184827ca37acSRyan Libby };
184927ca37acSRyan Libby 
185027ca37acSRyan Libby static void
185127ca37acSRyan Libby keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt,
185227ca37acSRyan Libby     struct keg_layout_result *kl)
185327ca37acSRyan Libby {
185427ca37acSRyan Libby 	u_int total;
185527ca37acSRyan Libby 
185627ca37acSRyan Libby 	kl->format = fmt;
185727ca37acSRyan Libby 	kl->slabsize = slabsize;
185827ca37acSRyan Libby 
185927ca37acSRyan Libby 	/* Handle INTERNAL as inline with an extra page. */
186027ca37acSRyan Libby 	if ((fmt & UMA_ZFLAG_INTERNAL) != 0) {
186127ca37acSRyan Libby 		kl->format &= ~UMA_ZFLAG_INTERNAL;
186227ca37acSRyan Libby 		kl->slabsize += PAGE_SIZE;
186327ca37acSRyan Libby 	}
186427ca37acSRyan Libby 
186527ca37acSRyan Libby 	kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize,
186627ca37acSRyan Libby 	    (fmt & UMA_ZFLAG_OFFPAGE) == 0);
186727ca37acSRyan Libby 
186827ca37acSRyan Libby 	/* Account for memory used by an offpage slab header. */
186927ca37acSRyan Libby 	total = kl->slabsize;
187027ca37acSRyan Libby 	if ((fmt & UMA_ZFLAG_OFFPAGE) != 0)
187127ca37acSRyan Libby 		total += slabzone(kl->ipers)->uz_keg->uk_rsize;
187227ca37acSRyan Libby 
187327ca37acSRyan Libby 	kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total);
187427ca37acSRyan Libby }
187527ca37acSRyan Libby 
18769b78b1f4SJeff Roberson /*
18774a8b575cSRyan Libby  * Determine the format of a uma keg.  This determines where the slab header
18784a8b575cSRyan Libby  * will be placed (inline or offpage) and calculates ipers, rsize, and ppera.
18798355f576SJeff Roberson  *
18808355f576SJeff Roberson  * Arguments
1881e20a199fSJeff Roberson  *	keg  The zone we should initialize
18828355f576SJeff Roberson  *
18838355f576SJeff Roberson  * Returns
18848355f576SJeff Roberson  *	Nothing
18858355f576SJeff Roberson  */
18868355f576SJeff Roberson static void
18874a8b575cSRyan Libby keg_layout(uma_keg_t keg)
18888355f576SJeff Roberson {
188927ca37acSRyan Libby 	struct keg_layout_result kl = {}, kl_tmp;
189027ca37acSRyan Libby 	u_int fmts[2];
18914a8b575cSRyan Libby 	u_int alignsize;
189227ca37acSRyan Libby 	u_int nfmt;
18934a8b575cSRyan Libby 	u_int pages;
1894244f4554SBosko Milekic 	u_int rsize;
1895a55ebb7cSAndriy Gapon 	u_int slabsize;
189627ca37acSRyan Libby 	u_int i, j;
18978355f576SJeff Roberson 
18984a8b575cSRyan Libby 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
18994a8b575cSRyan Libby 	    (keg->uk_size <= UMA_PCPU_ALLOC_SIZE &&
19004a8b575cSRyan Libby 	     (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0),
19014a8b575cSRyan Libby 	    ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b",
19024a8b575cSRyan Libby 	     __func__, keg->uk_name, keg->uk_size, keg->uk_flags,
19034a8b575cSRyan Libby 	     PRINT_UMA_ZFLAGS));
1904bae55c4aSRyan Libby 	KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 ||
19054a8b575cSRyan Libby 	    (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0,
19064a8b575cSRyan Libby 	    ("%s: incompatible flags 0x%b", __func__, keg->uk_flags,
19074a8b575cSRyan Libby 	     PRINT_UMA_ZFLAGS));
1908e28a647dSGleb Smirnoff 
19094a8b575cSRyan Libby 	alignsize = keg->uk_align + 1;
1910ad97af7eSGleb Smirnoff 
1911ef72505eSJeff Roberson 	/*
1912ef72505eSJeff Roberson 	 * Calculate the size of each allocation (rsize) according to
1913ef72505eSJeff Roberson 	 * alignment.  If the requested size is smaller than we have
1914ef72505eSJeff Roberson 	 * allocation bits for we round it up.
1915ef72505eSJeff Roberson 	 */
19169b8db4d0SRyan Libby 	rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT);
19174a8b575cSRyan Libby 	rsize = roundup2(rsize, alignsize);
1918ad97af7eSGleb Smirnoff 
191927ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) {
19209b78b1f4SJeff Roberson 		/*
19214a8b575cSRyan Libby 		 * We want one item to start on every align boundary in a page.
19224a8b575cSRyan Libby 		 * To do this we will span pages.  We will also extend the item
19234a8b575cSRyan Libby 		 * by the size of align if it is an even multiple of align.
19244a8b575cSRyan Libby 		 * Otherwise, it would fall on the same boundary every time.
19259b78b1f4SJeff Roberson 		 */
19264a8b575cSRyan Libby 		if ((rsize & alignsize) == 0)
19274a8b575cSRyan Libby 			rsize += alignsize;
19284a8b575cSRyan Libby 		slabsize = rsize * (PAGE_SIZE / alignsize);
19294a8b575cSRyan Libby 		slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE);
19304a8b575cSRyan Libby 		slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE);
193127ca37acSRyan Libby 		slabsize = round_page(slabsize);
19324a8b575cSRyan Libby 	} else {
19334a8b575cSRyan Libby 		/*
193427ca37acSRyan Libby 		 * Start with a slab size of as many pages as it takes to
193527ca37acSRyan Libby 		 * represent a single item.  We will try to fit as many
193627ca37acSRyan Libby 		 * additional items into the slab as possible.
19374a8b575cSRyan Libby 		 */
193827ca37acSRyan Libby 		slabsize = round_page(keg->uk_size);
19391ca6ed45SGleb Smirnoff 	}
1940ad97af7eSGleb Smirnoff 
194127ca37acSRyan Libby 	/* Build a list of all of the available formats for this keg. */
194227ca37acSRyan Libby 	nfmt = 0;
194327ca37acSRyan Libby 
19444a8b575cSRyan Libby 	/* Evaluate an inline slab layout. */
19454a8b575cSRyan Libby 	if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0)
194627ca37acSRyan Libby 		fmts[nfmt++] = 0;
19474a8b575cSRyan Libby 
19484a8b575cSRyan Libby 	/* TODO: vm_page-embedded slab. */
1949244f4554SBosko Milekic 
195020e8e865SBosko Milekic 	/*
1951244f4554SBosko Milekic 	 * We can't do OFFPAGE if we're internal or if we've been
195220e8e865SBosko Milekic 	 * asked to not go to the VM for buckets.  If we do this we
1953bae55c4aSRyan Libby 	 * may end up going to the VM for slabs which we do not want
1954bae55c4aSRyan Libby 	 * to do if we're UMA_ZONE_VM, which clearly forbids it.
1955bae55c4aSRyan Libby 	 * In those cases, evaluate a pseudo-format called INTERNAL
1956bae55c4aSRyan Libby 	 * which has an inline slab header and one extra page to
1957bae55c4aSRyan Libby 	 * guarantee that it fits.
195827ca37acSRyan Libby 	 *
195927ca37acSRyan Libby 	 * Otherwise, see if using an OFFPAGE slab will improve our
196027ca37acSRyan Libby 	 * efficiency.
196120e8e865SBosko Milekic 	 */
1962bae55c4aSRyan Libby 	if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0)
196327ca37acSRyan Libby 		fmts[nfmt++] = UMA_ZFLAG_INTERNAL;
196427ca37acSRyan Libby 	else
196527ca37acSRyan Libby 		fmts[nfmt++] = UMA_ZFLAG_OFFPAGE;
1966244f4554SBosko Milekic 
1967ef72505eSJeff Roberson 	/*
196827ca37acSRyan Libby 	 * Choose a slab size and format which satisfy the minimum efficiency.
196927ca37acSRyan Libby 	 * Prefer the smallest slab size that meets the constraints.
1970ef72505eSJeff Roberson 	 *
197127ca37acSRyan Libby 	 * Start with a minimum slab size, to accommodate CACHESPREAD.  Then,
197227ca37acSRyan Libby 	 * for small items (up to PAGE_SIZE), the iteration increment is one
197327ca37acSRyan Libby 	 * page; and for large items, the increment is one item.
1974ef72505eSJeff Roberson 	 */
197527ca37acSRyan Libby 	i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize);
197627ca37acSRyan Libby 	KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u",
197727ca37acSRyan Libby 	    keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize,
197827ca37acSRyan Libby 	    rsize, i));
197927ca37acSRyan Libby 	for ( ; ; i++) {
198027ca37acSRyan Libby 		slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) :
198127ca37acSRyan Libby 		    round_page(rsize * (i - 1) + keg->uk_size);
198227ca37acSRyan Libby 
198327ca37acSRyan Libby 		for (j = 0; j < nfmt; j++) {
198427ca37acSRyan Libby 			/* Only if we have no viable format yet. */
198527ca37acSRyan Libby 			if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 &&
198627ca37acSRyan Libby 			    kl.ipers > 0)
198727ca37acSRyan Libby 				continue;
198827ca37acSRyan Libby 
198927ca37acSRyan Libby 			keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp);
199027ca37acSRyan Libby 			if (kl_tmp.eff <= kl.eff)
199127ca37acSRyan Libby 				continue;
199227ca37acSRyan Libby 
199327ca37acSRyan Libby 			kl = kl_tmp;
199427ca37acSRyan Libby 
199527ca37acSRyan Libby 			CTR6(KTR_UMA, "keg %s layout: format %#x "
199627ca37acSRyan Libby 			    "(ipers %u * rsize %u) / slabsize %#x = %u%% eff",
199727ca37acSRyan Libby 			    keg->uk_name, kl.format, kl.ipers, rsize,
199827ca37acSRyan Libby 			    kl.slabsize, UMA_FIXPT_PCT(kl.eff));
199927ca37acSRyan Libby 
200027ca37acSRyan Libby 			/* Stop when we reach the minimum efficiency. */
200127ca37acSRyan Libby 			if (kl.eff >= UMA_MIN_EFF)
200227ca37acSRyan Libby 				break;
20038355f576SJeff Roberson 		}
2004ad97af7eSGleb Smirnoff 
200533e5a1eaSRyan Libby 		if (kl.eff >= UMA_MIN_EFF || !multipage_slabs ||
200627ca37acSRyan Libby 		    slabsize >= SLAB_MAX_SETSIZE * rsize ||
200727ca37acSRyan Libby 		    (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0)
200827ca37acSRyan Libby 			break;
200927ca37acSRyan Libby 	}
201027ca37acSRyan Libby 
201127ca37acSRyan Libby 	pages = atop(kl.slabsize);
201227ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
201327ca37acSRyan Libby 		pages *= mp_maxid + 1;
201427ca37acSRyan Libby 
201527ca37acSRyan Libby 	keg->uk_rsize = rsize;
201627ca37acSRyan Libby 	keg->uk_ipers = kl.ipers;
201727ca37acSRyan Libby 	keg->uk_ppera = pages;
201827ca37acSRyan Libby 	keg->uk_flags |= kl.format;
201927ca37acSRyan Libby 
20204a8b575cSRyan Libby 	/*
20214a8b575cSRyan Libby 	 * How do we find the slab header if it is offpage or if not all item
20224a8b575cSRyan Libby 	 * start addresses are in the same page?  We could solve the latter
20234a8b575cSRyan Libby 	 * case with vaddr alignment, but we don't.
20244a8b575cSRyan Libby 	 */
202527ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 ||
202627ca37acSRyan Libby 	    (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) {
202754c5ae80SRyan Libby 		if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0)
202827ca37acSRyan Libby 			keg->uk_flags |= UMA_ZFLAG_HASH;
202954c5ae80SRyan Libby 		else
203027ca37acSRyan Libby 			keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
203154c5ae80SRyan Libby 	}
203227ca37acSRyan Libby 
2033e63a1c2fSRyan Libby 	CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u",
203427ca37acSRyan Libby 	    __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers,
203527ca37acSRyan Libby 	    pages);
20364a8b575cSRyan Libby 	KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE,
20374a8b575cSRyan Libby 	    ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__,
203827ca37acSRyan Libby 	     keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize,
203927ca37acSRyan Libby 	     keg->uk_ipers, pages));
2040e20a199fSJeff Roberson }
2041e20a199fSJeff Roberson 
20428355f576SJeff Roberson /*
2043099a0e58SBosko Milekic  * Keg header ctor.  This initializes all fields, locks, etc.  And inserts
2044099a0e58SBosko Milekic  * the keg onto the global keg list.
20458355f576SJeff Roberson  *
20468355f576SJeff Roberson  * Arguments/Returns follow uma_ctor specifications
2047099a0e58SBosko Milekic  *	udata  Actually uma_kctor_args
2048099a0e58SBosko Milekic  */
2049b23f72e9SBrian Feldman static int
2050b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags)
2051099a0e58SBosko Milekic {
2052099a0e58SBosko Milekic 	struct uma_kctor_args *arg = udata;
2053099a0e58SBosko Milekic 	uma_keg_t keg = mem;
2054099a0e58SBosko Milekic 	uma_zone_t zone;
20558b987a77SJeff Roberson 	int i;
2056099a0e58SBosko Milekic 
2057099a0e58SBosko Milekic 	bzero(keg, size);
2058099a0e58SBosko Milekic 	keg->uk_size = arg->size;
2059099a0e58SBosko Milekic 	keg->uk_init = arg->uminit;
2060099a0e58SBosko Milekic 	keg->uk_fini = arg->fini;
2061099a0e58SBosko Milekic 	keg->uk_align = arg->align;
20626fd34d6fSJeff Roberson 	keg->uk_reserve = 0;
2063099a0e58SBosko Milekic 	keg->uk_flags = arg->flags;
2064099a0e58SBosko Milekic 
2065099a0e58SBosko Milekic 	/*
2066194a979eSMark Johnston 	 * We use a global round-robin policy by default.  Zones with
2067dfe13344SJeff Roberson 	 * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which
2068dfe13344SJeff Roberson 	 * case the iterator is never run.
2069194a979eSMark Johnston 	 */
2070194a979eSMark Johnston 	keg->uk_dr.dr_policy = DOMAINSET_RR();
2071194a979eSMark Johnston 	keg->uk_dr.dr_iter = 0;
2072194a979eSMark Johnston 
2073194a979eSMark Johnston 	/*
2074099a0e58SBosko Milekic 	 * The master zone is passed to us at keg-creation time.
2075099a0e58SBosko Milekic 	 */
2076099a0e58SBosko Milekic 	zone = arg->zone;
2077e20a199fSJeff Roberson 	keg->uk_name = zone->uz_name;
2078099a0e58SBosko Milekic 
2079099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_ZINIT)
2080099a0e58SBosko Milekic 		keg->uk_init = zero_init;
2081099a0e58SBosko Milekic 
2082cfcae3f8SGleb Smirnoff 	if (arg->flags & UMA_ZONE_MALLOC)
208354c5ae80SRyan Libby 		keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
2084e20a199fSJeff Roberson 
208554c5ae80SRyan Libby #ifndef SMP
2086ad97af7eSGleb Smirnoff 	keg->uk_flags &= ~UMA_ZONE_PCPU;
2087ad97af7eSGleb Smirnoff #endif
2088ad97af7eSGleb Smirnoff 
20894a8b575cSRyan Libby 	keg_layout(keg);
2090099a0e58SBosko Milekic 
20918b987a77SJeff Roberson 	/*
2092dfe13344SJeff Roberson 	 * Use a first-touch NUMA policy for all kegs that pmap_extract()
2093dfe13344SJeff Roberson 	 * will work on with the exception of critical VM structures
2094dfe13344SJeff Roberson 	 * necessary for paging.
2095dfe13344SJeff Roberson 	 *
2096dfe13344SJeff Roberson 	 * Zones may override the default by specifying either.
20978b987a77SJeff Roberson 	 */
2098dfe13344SJeff Roberson #ifdef NUMA
2099dfe13344SJeff Roberson 	if ((keg->uk_flags &
210054c5ae80SRyan Libby 	    (UMA_ZFLAG_HASH | UMA_ZONE_VM | UMA_ZONE_ROUNDROBIN)) == 0)
2101dfe13344SJeff Roberson 		keg->uk_flags |= UMA_ZONE_FIRSTTOUCH;
2102dfe13344SJeff Roberson 	else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0)
2103dfe13344SJeff Roberson 		keg->uk_flags |= UMA_ZONE_ROUNDROBIN;
21048b987a77SJeff Roberson #endif
21058b987a77SJeff Roberson 
2106099a0e58SBosko Milekic 	/*
2107099a0e58SBosko Milekic 	 * If we haven't booted yet we need allocations to go through the
2108099a0e58SBosko Milekic 	 * startup cache until the vm is ready.
2109099a0e58SBosko Milekic 	 */
211077e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC
2111a81c400eSJeff Roberson 	if (keg->uk_ppera == 1)
211277e19437SGleb Smirnoff 		keg->uk_allocf = uma_small_alloc;
2113a81c400eSJeff Roberson 	else
21148cd02d00SAlan Cox #endif
2115a81c400eSJeff Roberson 	if (booted < BOOT_KVA)
2116a81c400eSJeff Roberson 		keg->uk_allocf = startup_alloc;
2117ab3059a8SMatt Macy 	else if (keg->uk_flags & UMA_ZONE_PCPU)
2118ab3059a8SMatt Macy 		keg->uk_allocf = pcpu_page_alloc;
2119ec0d8280SRyan Libby 	else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1)
2120ec0d8280SRyan Libby 		keg->uk_allocf = contig_alloc;
212177e19437SGleb Smirnoff 	else
212277e19437SGleb Smirnoff 		keg->uk_allocf = page_alloc;
212377e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC
212477e19437SGleb Smirnoff 	if (keg->uk_ppera == 1)
212577e19437SGleb Smirnoff 		keg->uk_freef = uma_small_free;
212677e19437SGleb Smirnoff 	else
212777e19437SGleb Smirnoff #endif
2128ab3059a8SMatt Macy 	if (keg->uk_flags & UMA_ZONE_PCPU)
2129ab3059a8SMatt Macy 		keg->uk_freef = pcpu_page_free;
2130ab3059a8SMatt Macy 	else
213177e19437SGleb Smirnoff 		keg->uk_freef = page_free;
2132099a0e58SBosko Milekic 
2133099a0e58SBosko Milekic 	/*
21348b987a77SJeff Roberson 	 * Initialize keg's locks.
2135099a0e58SBosko Milekic 	 */
21368b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
21378b987a77SJeff Roberson 		KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS));
2138099a0e58SBosko Milekic 
2139099a0e58SBosko Milekic 	/*
2140099a0e58SBosko Milekic 	 * If we're putting the slab header in the actual page we need to
21419b78b1f4SJeff Roberson 	 * figure out where in each page it goes.  See slab_sizeof
21429b78b1f4SJeff Roberson 	 * definition.
2143099a0e58SBosko Milekic 	 */
214454c5ae80SRyan Libby 	if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) {
21459b78b1f4SJeff Roberson 		size_t shsize;
21469b78b1f4SJeff Roberson 
21479b78b1f4SJeff Roberson 		shsize = slab_sizeof(keg->uk_ipers);
21489b78b1f4SJeff Roberson 		keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize;
2149244f4554SBosko Milekic 		/*
2150244f4554SBosko Milekic 		 * The only way the following is possible is if with our
2151244f4554SBosko Milekic 		 * UMA_ALIGN_PTR adjustments we are now bigger than
2152244f4554SBosko Milekic 		 * UMA_SLAB_SIZE.  I haven't checked whether this is
2153244f4554SBosko Milekic 		 * mathematically possible for all cases, so we make
2154244f4554SBosko Milekic 		 * sure here anyway.
2155244f4554SBosko Milekic 		 */
21569b78b1f4SJeff Roberson 		KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera,
21573d5e3df7SGleb Smirnoff 		    ("zone %s ipers %d rsize %d size %d slab won't fit",
21583d5e3df7SGleb Smirnoff 		    zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size));
2159099a0e58SBosko Milekic 	}
2160099a0e58SBosko Milekic 
216154c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_HASH)
21623b2f2cb8SAlexander Motin 		hash_alloc(&keg->uk_hash, 0);
2163099a0e58SBosko Milekic 
2164e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone);
2165099a0e58SBosko Milekic 
2166099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
2167099a0e58SBosko Milekic 
2168111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
2169099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
2170111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
2171b23f72e9SBrian Feldman 	return (0);
2172099a0e58SBosko Milekic }
2173099a0e58SBosko Milekic 
21742efcc8cbSGleb Smirnoff static void
2175a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused)
2176a81c400eSJeff Roberson {
2177a81c400eSJeff Roberson 	uma_keg_t keg;
2178a81c400eSJeff Roberson 
2179a81c400eSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
2180a81c400eSJeff Roberson 		return;
2181a81c400eSJeff Roberson 	KEG_GET(zone, keg);
2182ec0d8280SRyan Libby 
2183ec0d8280SRyan Libby 	if (keg->uk_allocf == startup_alloc) {
2184ec0d8280SRyan Libby 		/* Switch to the real allocator. */
2185f96d4157SJeff Roberson 		if (keg->uk_flags & UMA_ZONE_PCPU)
2186f96d4157SJeff Roberson 			keg->uk_allocf = pcpu_page_alloc;
2187ec0d8280SRyan Libby 		else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 &&
2188ec0d8280SRyan Libby 		    keg->uk_ppera > 1)
2189ec0d8280SRyan Libby 			keg->uk_allocf = contig_alloc;
2190ec0d8280SRyan Libby 		else
2191a81c400eSJeff Roberson 			keg->uk_allocf = page_alloc;
2192a81c400eSJeff Roberson 	}
2193ec0d8280SRyan Libby }
2194a81c400eSJeff Roberson 
2195a81c400eSJeff Roberson static void
219620a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused)
21972efcc8cbSGleb Smirnoff {
21982efcc8cbSGleb Smirnoff 
21992efcc8cbSGleb Smirnoff 	zone->uz_allocs = counter_u64_alloc(M_WAITOK);
22002efcc8cbSGleb Smirnoff 	zone->uz_frees = counter_u64_alloc(M_WAITOK);
22012efcc8cbSGleb Smirnoff 	zone->uz_fails = counter_u64_alloc(M_WAITOK);
22022efcc8cbSGleb Smirnoff }
22032efcc8cbSGleb Smirnoff 
220420a4e154SJeff Roberson static void
220520a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused)
220620a4e154SJeff Roberson {
220720a4e154SJeff Roberson 	uma_zone_domain_t zdom;
22088b987a77SJeff Roberson 	uma_domain_t dom;
220920a4e154SJeff Roberson 	uma_keg_t keg;
221020a4e154SJeff Roberson 	struct sysctl_oid *oid, *domainoid;
22113b490537SJeff Roberson 	int domains, i, cnt;
221220a4e154SJeff Roberson 	static const char *nokeg = "cache zone";
221320a4e154SJeff Roberson 	char *c;
221420a4e154SJeff Roberson 
221520a4e154SJeff Roberson 	/*
221620a4e154SJeff Roberson 	 * Make a sysctl safe copy of the zone name by removing
221720a4e154SJeff Roberson 	 * any special characters and handling dups by appending
221820a4e154SJeff Roberson 	 * an index.
221920a4e154SJeff Roberson 	 */
222020a4e154SJeff Roberson 	if (zone->uz_namecnt != 0) {
22213b490537SJeff Roberson 		/* Count the number of decimal digits and '_' separator. */
22223b490537SJeff Roberson 		for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++)
22233b490537SJeff Roberson 			cnt /= 10;
22243b490537SJeff Roberson 		zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1,
22253b490537SJeff Roberson 		    M_UMA, M_WAITOK);
222620a4e154SJeff Roberson 		sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name,
222720a4e154SJeff Roberson 		    zone->uz_namecnt);
222820a4e154SJeff Roberson 	} else
222920a4e154SJeff Roberson 		zone->uz_ctlname = strdup(zone->uz_name, M_UMA);
223020a4e154SJeff Roberson 	for (c = zone->uz_ctlname; *c != '\0'; c++)
223120a4e154SJeff Roberson 		if (strchr("./\\ -", *c) != NULL)
223220a4e154SJeff Roberson 			*c = '_';
223320a4e154SJeff Roberson 
223420a4e154SJeff Roberson 	/*
223520a4e154SJeff Roberson 	 * Basic parameters at the root.
223620a4e154SJeff Roberson 	 */
223720a4e154SJeff Roberson 	zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma),
223820a4e154SJeff Roberson 	    OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, "");
223920a4e154SJeff Roberson 	oid = zone->uz_oid;
224020a4e154SJeff Roberson 	SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
224120a4e154SJeff Roberson 	    "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size");
22426d204a6aSRyan Libby 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
22436d204a6aSRyan Libby 	    "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE,
22446d204a6aSRyan Libby 	    zone, 0, sysctl_handle_uma_zone_flags, "A",
224520a4e154SJeff Roberson 	    "Allocator configuration flags");
224620a4e154SJeff Roberson 	SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
224720a4e154SJeff Roberson 	    "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0,
224820a4e154SJeff Roberson 	    "Desired per-cpu cache size");
224920a4e154SJeff Roberson 	SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
225020a4e154SJeff Roberson 	    "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0,
225120a4e154SJeff Roberson 	    "Maximum allowed per-cpu cache size");
225220a4e154SJeff Roberson 
225320a4e154SJeff Roberson 	/*
225420a4e154SJeff Roberson 	 * keg if present.
225520a4e154SJeff Roberson 	 */
225654c5ae80SRyan Libby 	if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0)
22578b987a77SJeff Roberson 		domains = vm_ndomains;
22588b987a77SJeff Roberson 	else
22598b987a77SJeff Roberson 		domains = 1;
226020a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
226120a4e154SJeff Roberson 	    "keg", CTLFLAG_RD, NULL, "");
226220a4e154SJeff Roberson 	keg = zone->uz_keg;
22633b490537SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) {
226420a4e154SJeff Roberson 		SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
226520a4e154SJeff Roberson 		    "name", CTLFLAG_RD, keg->uk_name, "Keg name");
226620a4e154SJeff Roberson 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
226720a4e154SJeff Roberson 		    "rsize", CTLFLAG_RD, &keg->uk_rsize, 0,
226820a4e154SJeff Roberson 		    "Real object size with alignment");
226920a4e154SJeff Roberson 		SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
227020a4e154SJeff Roberson 		    "ppera", CTLFLAG_RD, &keg->uk_ppera, 0,
227120a4e154SJeff Roberson 		    "pages per-slab allocation");
227220a4e154SJeff Roberson 		SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
227320a4e154SJeff Roberson 		    "ipers", CTLFLAG_RD, &keg->uk_ipers, 0,
227420a4e154SJeff Roberson 		    "items available per-slab");
227520a4e154SJeff Roberson 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
227620a4e154SJeff Roberson 		    "align", CTLFLAG_RD, &keg->uk_align, 0,
227720a4e154SJeff Roberson 		    "item alignment mask");
2278f7af5015SRyan Libby 		SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2279f7af5015SRyan Libby 		    "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
2280f7af5015SRyan Libby 		    keg, 0, sysctl_handle_uma_slab_efficiency, "I",
2281f7af5015SRyan Libby 		    "Slab utilization (100 - internal fragmentation %)");
22828b987a77SJeff Roberson 		domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid),
22838b987a77SJeff Roberson 		    OID_AUTO, "domain", CTLFLAG_RD, NULL, "");
22848b987a77SJeff Roberson 		for (i = 0; i < domains; i++) {
22858b987a77SJeff Roberson 			dom = &keg->uk_domain[i];
22868b987a77SJeff Roberson 			oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
22878b987a77SJeff Roberson 			    OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD,
22888b987a77SJeff Roberson 			    NULL, "");
22898b987a77SJeff Roberson 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
22908b987a77SJeff Roberson 			    "pages", CTLFLAG_RD, &dom->ud_pages, 0,
22918b987a77SJeff Roberson 			    "Total pages currently allocated from VM");
22928b987a77SJeff Roberson 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
22934ab3aee8SMark Johnston 			    "free_items", CTLFLAG_RD, &dom->ud_free_items, 0,
22948b987a77SJeff Roberson 			    "items free in the slab layer");
22958b987a77SJeff Roberson 		}
229620a4e154SJeff Roberson 	} else
229720a4e154SJeff Roberson 		SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
229820a4e154SJeff Roberson 		    "name", CTLFLAG_RD, nokeg, "Keg name");
229920a4e154SJeff Roberson 
230020a4e154SJeff Roberson 	/*
230120a4e154SJeff Roberson 	 * Information about zone limits.
230220a4e154SJeff Roberson 	 */
230320a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
230420a4e154SJeff Roberson 	    "limit", CTLFLAG_RD, NULL, "");
23054bd61e19SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
23064bd61e19SJeff Roberson 	    "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
23074bd61e19SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_items, "QU",
23084bd61e19SJeff Roberson 	    "current number of allocated items if limit is set");
230920a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
231020a4e154SJeff Roberson 	    "max_items", CTLFLAG_RD, &zone->uz_max_items, 0,
231120a4e154SJeff Roberson 	    "Maximum number of cached items");
231220a4e154SJeff Roberson 	SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
231320a4e154SJeff Roberson 	    "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0,
231420a4e154SJeff Roberson 	    "Number of threads sleeping at limit");
231520a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
231620a4e154SJeff Roberson 	    "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0,
231720a4e154SJeff Roberson 	    "Total zone limit sleeps");
23184bd61e19SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
23194bd61e19SJeff Roberson 	    "bucket_max", CTLFLAG_RD, &zone->uz_bkt_max, 0,
23204bd61e19SJeff Roberson 	    "Maximum number of items in the bucket cache");
23214bd61e19SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
23224bd61e19SJeff Roberson 	    "bucket_cnt", CTLFLAG_RD, &zone->uz_bkt_count, 0,
23234bd61e19SJeff Roberson 	    "Number of items in the bucket cache");
232420a4e154SJeff Roberson 
232520a4e154SJeff Roberson 	/*
23268b987a77SJeff Roberson 	 * Per-domain zone information.
232720a4e154SJeff Roberson 	 */
232820a4e154SJeff Roberson 	domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid),
232920a4e154SJeff Roberson 	    OID_AUTO, "domain", CTLFLAG_RD, NULL, "");
2330dfe13344SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0)
23318b987a77SJeff Roberson 		domains = 1;
233220a4e154SJeff Roberson 	for (i = 0; i < domains; i++) {
233320a4e154SJeff Roberson 		zdom = &zone->uz_domain[i];
233420a4e154SJeff Roberson 		oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
233520a4e154SJeff Roberson 		    OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, "");
233620a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
233720a4e154SJeff Roberson 		    "nitems", CTLFLAG_RD, &zdom->uzd_nitems,
233820a4e154SJeff Roberson 		    "number of items in this domain");
233920a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
234020a4e154SJeff Roberson 		    "imax", CTLFLAG_RD, &zdom->uzd_imax,
234120a4e154SJeff Roberson 		    "maximum item count in this period");
234220a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
234320a4e154SJeff Roberson 		    "imin", CTLFLAG_RD, &zdom->uzd_imin,
234420a4e154SJeff Roberson 		    "minimum item count in this period");
234520a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
234620a4e154SJeff Roberson 		    "wss", CTLFLAG_RD, &zdom->uzd_wss,
234720a4e154SJeff Roberson 		    "Working set size");
234820a4e154SJeff Roberson 	}
234920a4e154SJeff Roberson 
235020a4e154SJeff Roberson 	/*
235120a4e154SJeff Roberson 	 * General statistics.
235220a4e154SJeff Roberson 	 */
235320a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
235420a4e154SJeff Roberson 	    "stats", CTLFLAG_RD, NULL, "");
235520a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
235620a4e154SJeff Roberson 	    "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
235720a4e154SJeff Roberson 	    zone, 1, sysctl_handle_uma_zone_cur, "I",
235820a4e154SJeff Roberson 	    "Current number of allocated items");
235920a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
236020a4e154SJeff Roberson 	    "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
236120a4e154SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_allocs, "QU",
236220a4e154SJeff Roberson 	    "Total allocation calls");
236320a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
236420a4e154SJeff Roberson 	    "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
236520a4e154SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_frees, "QU",
236620a4e154SJeff Roberson 	    "Total free calls");
236720a4e154SJeff Roberson 	SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
236820a4e154SJeff Roberson 	    "fails", CTLFLAG_RD, &zone->uz_fails,
236920a4e154SJeff Roberson 	    "Number of allocation failures");
237020a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
237120a4e154SJeff Roberson 	    "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 0,
237220a4e154SJeff Roberson 	    "Free calls from the wrong domain");
237320a4e154SJeff Roberson }
237420a4e154SJeff Roberson 
237520a4e154SJeff Roberson struct uma_zone_count {
237620a4e154SJeff Roberson 	const char	*name;
237720a4e154SJeff Roberson 	int		count;
237820a4e154SJeff Roberson };
237920a4e154SJeff Roberson 
238020a4e154SJeff Roberson static void
238120a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg)
238220a4e154SJeff Roberson {
238320a4e154SJeff Roberson 	struct uma_zone_count *cnt;
238420a4e154SJeff Roberson 
238520a4e154SJeff Roberson 	cnt = arg;
23863b490537SJeff Roberson 	/*
23873b490537SJeff Roberson 	 * Some zones are rapidly created with identical names and
23883b490537SJeff Roberson 	 * destroyed out of order.  This can lead to gaps in the count.
23893b490537SJeff Roberson 	 * Use one greater than the maximum observed for this name.
23903b490537SJeff Roberson 	 */
239120a4e154SJeff Roberson 	if (strcmp(zone->uz_name, cnt->name) == 0)
23923b490537SJeff Roberson 		cnt->count = MAX(cnt->count,
23933b490537SJeff Roberson 		    zone->uz_namecnt + 1);
239420a4e154SJeff Roberson }
239520a4e154SJeff Roberson 
2396cc7ce83aSJeff Roberson static void
2397cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone)
2398cc7ce83aSJeff Roberson {
2399cc7ce83aSJeff Roberson 	int i;
2400cc7ce83aSJeff Roberson 
2401cc7ce83aSJeff Roberson 	for (i = 0; i <= mp_maxid; i++) {
2402cc7ce83aSJeff Roberson 		cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size);
2403cc7ce83aSJeff Roberson 		cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags);
2404cc7ce83aSJeff Roberson 	}
2405cc7ce83aSJeff Roberson }
2406cc7ce83aSJeff Roberson 
2407099a0e58SBosko Milekic /*
2408099a0e58SBosko Milekic  * Zone header ctor.  This initializes all fields, locks, etc.
2409099a0e58SBosko Milekic  *
2410099a0e58SBosko Milekic  * Arguments/Returns follow uma_ctor specifications
2411099a0e58SBosko Milekic  *	udata  Actually uma_zctor_args
24128355f576SJeff Roberson  */
2413b23f72e9SBrian Feldman static int
2414b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags)
24158355f576SJeff Roberson {
241620a4e154SJeff Roberson 	struct uma_zone_count cnt;
24178355f576SJeff Roberson 	struct uma_zctor_args *arg = udata;
24188355f576SJeff Roberson 	uma_zone_t zone = mem;
2419099a0e58SBosko Milekic 	uma_zone_t z;
2420099a0e58SBosko Milekic 	uma_keg_t keg;
242108cfa56eSMark Johnston 	int i;
24228355f576SJeff Roberson 
24238355f576SJeff Roberson 	bzero(zone, size);
24248355f576SJeff Roberson 	zone->uz_name = arg->name;
24258355f576SJeff Roberson 	zone->uz_ctor = arg->ctor;
24268355f576SJeff Roberson 	zone->uz_dtor = arg->dtor;
2427099a0e58SBosko Milekic 	zone->uz_init = NULL;
2428099a0e58SBosko Milekic 	zone->uz_fini = NULL;
2429bf965959SSean Bruno 	zone->uz_sleeps = 0;
2430c1685086SJeff Roberson 	zone->uz_xdomain = 0;
243120a4e154SJeff Roberson 	zone->uz_bucket_size = 0;
243220a4e154SJeff Roberson 	zone->uz_bucket_size_min = 0;
243320a4e154SJeff Roberson 	zone->uz_bucket_size_max = BUCKET_MAX;
2434d4665eaaSJeff Roberson 	zone->uz_flags = (arg->flags & UMA_ZONE_SMR);
24352f891cd5SPawel Jakub Dawidek 	zone->uz_warning = NULL;
2436ab3185d1SJeff Roberson 	/* The domain structures follow the cpu structures. */
24378d1c459aSRyan Libby 	zone->uz_domain =
24388d1c459aSRyan Libby 	    (struct uma_zone_domain *)&zone->uz_cpu[mp_maxid + 1];
2439bb15d1c7SGleb Smirnoff 	zone->uz_bkt_max = ULONG_MAX;
24402f891cd5SPawel Jakub Dawidek 	timevalclear(&zone->uz_ratecheck);
2441af526374SJeff Roberson 
244220a4e154SJeff Roberson 	/* Count the number of duplicate names. */
244320a4e154SJeff Roberson 	cnt.name = arg->name;
244420a4e154SJeff Roberson 	cnt.count = 0;
244520a4e154SJeff Roberson 	zone_foreach(zone_count, &cnt);
244620a4e154SJeff Roberson 	zone->uz_namecnt = cnt.count;
2447727c6918SJeff Roberson 	ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
244891d947bfSJeff Roberson 	ZONE_CROSS_LOCK_INIT(zone);
24492efcc8cbSGleb Smirnoff 
245008cfa56eSMark Johnston 	for (i = 0; i < vm_ndomains; i++)
2451dc3915c8SJeff Roberson 		STAILQ_INIT(&zone->uz_domain[i].uzd_buckets);
245208cfa56eSMark Johnston 
2453ca293436SRyan Libby #ifdef INVARIANTS
2454ca293436SRyan Libby 	if (arg->uminit == trash_init && arg->fini == trash_fini)
2455cc7ce83aSJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR;
2456ca293436SRyan Libby #endif
2457ca293436SRyan Libby 
24580095a784SJeff Roberson 	/*
24590095a784SJeff Roberson 	 * This is a pure cache zone, no kegs.
24600095a784SJeff Roberson 	 */
24610095a784SJeff Roberson 	if (arg->import) {
2462727c6918SJeff Roberson 		KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0,
2463727c6918SJeff Roberson 		    ("zone_ctor: Import specified for non-cache zone."));
24646fd34d6fSJeff Roberson 		zone->uz_flags = arg->flags;
2465af526374SJeff Roberson 		zone->uz_size = arg->size;
24660095a784SJeff Roberson 		zone->uz_import = arg->import;
24670095a784SJeff Roberson 		zone->uz_release = arg->release;
24680095a784SJeff Roberson 		zone->uz_arg = arg->arg;
2469111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
247003175483SAlexander Motin 		LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
2471111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
2472af526374SJeff Roberson 		goto out;
24730095a784SJeff Roberson 	}
24740095a784SJeff Roberson 
24750095a784SJeff Roberson 	/*
24760095a784SJeff Roberson 	 * Use the regular zone/keg/slab allocator.
24770095a784SJeff Roberson 	 */
2478b75c4efcSAndrew Turner 	zone->uz_import = zone_import;
2479b75c4efcSAndrew Turner 	zone->uz_release = zone_release;
24800095a784SJeff Roberson 	zone->uz_arg = zone;
2481bb15d1c7SGleb Smirnoff 	keg = arg->keg;
24820095a784SJeff Roberson 
2483099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_SECONDARY) {
248420a4e154SJeff Roberson 		KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
248520a4e154SJeff Roberson 		    ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
2486099a0e58SBosko Milekic 		KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
24878355f576SJeff Roberson 		zone->uz_init = arg->uminit;
2488e221e841SJeff Roberson 		zone->uz_fini = arg->fini;
2489e20a199fSJeff Roberson 		zone->uz_flags |= UMA_ZONE_SECONDARY;
2490111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
2491099a0e58SBosko Milekic 		ZONE_LOCK(zone);
2492099a0e58SBosko Milekic 		LIST_FOREACH(z, &keg->uk_zones, uz_link) {
2493099a0e58SBosko Milekic 			if (LIST_NEXT(z, uz_link) == NULL) {
2494099a0e58SBosko Milekic 				LIST_INSERT_AFTER(z, zone, uz_link);
2495099a0e58SBosko Milekic 				break;
2496099a0e58SBosko Milekic 			}
2497099a0e58SBosko Milekic 		}
2498099a0e58SBosko Milekic 		ZONE_UNLOCK(zone);
2499111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
2500e20a199fSJeff Roberson 	} else if (keg == NULL) {
2501e20a199fSJeff Roberson 		if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
2502e20a199fSJeff Roberson 		    arg->align, arg->flags)) == NULL)
2503b23f72e9SBrian Feldman 			return (ENOMEM);
2504099a0e58SBosko Milekic 	} else {
2505099a0e58SBosko Milekic 		struct uma_kctor_args karg;
2506b23f72e9SBrian Feldman 		int error;
2507099a0e58SBosko Milekic 
2508099a0e58SBosko Milekic 		/* We should only be here from uma_startup() */
2509099a0e58SBosko Milekic 		karg.size = arg->size;
2510099a0e58SBosko Milekic 		karg.uminit = arg->uminit;
2511099a0e58SBosko Milekic 		karg.fini = arg->fini;
2512099a0e58SBosko Milekic 		karg.align = arg->align;
2513d4665eaaSJeff Roberson 		karg.flags = (arg->flags & ~UMA_ZONE_SMR);
2514099a0e58SBosko Milekic 		karg.zone = zone;
2515b23f72e9SBrian Feldman 		error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
2516b23f72e9SBrian Feldman 		    flags);
2517b23f72e9SBrian Feldman 		if (error)
2518b23f72e9SBrian Feldman 			return (error);
2519099a0e58SBosko Milekic 	}
25200095a784SJeff Roberson 
252120a4e154SJeff Roberson 	/* Inherit properties from the keg. */
2522bb15d1c7SGleb Smirnoff 	zone->uz_keg = keg;
2523e20a199fSJeff Roberson 	zone->uz_size = keg->uk_size;
2524e20a199fSJeff Roberson 	zone->uz_flags |= (keg->uk_flags &
2525e20a199fSJeff Roberson 	    (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
25268355f576SJeff Roberson 
252720a4e154SJeff Roberson out:
2528860bb7a0SMark Johnston 	if (__predict_true(booted >= BOOT_RUNNING)) {
252920a4e154SJeff Roberson 		zone_alloc_counters(zone, NULL);
253020a4e154SJeff Roberson 		zone_alloc_sysctl(zone, NULL);
253120a4e154SJeff Roberson 	} else {
253220a4e154SJeff Roberson 		zone->uz_allocs = EARLY_COUNTER;
253320a4e154SJeff Roberson 		zone->uz_frees = EARLY_COUNTER;
253420a4e154SJeff Roberson 		zone->uz_fails = EARLY_COUNTER;
2535099a0e58SBosko Milekic 	}
25368355f576SJeff Roberson 
2537d4665eaaSJeff Roberson 	/* Caller requests a private SMR context. */
2538d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
2539d4665eaaSJeff Roberson 		zone->uz_smr = smr_create(zone->uz_name);
2540d4665eaaSJeff Roberson 
25417e28037aSMark Johnston 	KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) !=
25427e28037aSMark Johnston 	    (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET),
25437e28037aSMark Johnston 	    ("Invalid zone flag combination"));
254420a4e154SJeff Roberson 	if (arg->flags & UMA_ZFLAG_INTERNAL)
254520a4e154SJeff Roberson 		zone->uz_bucket_size_max = zone->uz_bucket_size = 0;
254620a4e154SJeff Roberson 	if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0)
254720a4e154SJeff Roberson 		zone->uz_bucket_size = BUCKET_MAX;
254820a4e154SJeff Roberson 	else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0)
254920a4e154SJeff Roberson 		zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN;
255020a4e154SJeff Roberson 	else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0)
255120a4e154SJeff Roberson 		zone->uz_bucket_size = 0;
25527e28037aSMark Johnston 	else
255320a4e154SJeff Roberson 		zone->uz_bucket_size = bucket_select(zone->uz_size);
255420a4e154SJeff Roberson 	zone->uz_bucket_size_min = zone->uz_bucket_size;
2555cc7ce83aSJeff Roberson 	if (zone->uz_dtor != NULL || zone->uz_ctor != NULL)
2556cc7ce83aSJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_CTORDTOR;
2557cc7ce83aSJeff Roberson 	zone_update_caches(zone);
2558fc03d22bSJeff Roberson 
2559b23f72e9SBrian Feldman 	return (0);
25608355f576SJeff Roberson }
25618355f576SJeff Roberson 
25628355f576SJeff Roberson /*
2563099a0e58SBosko Milekic  * Keg header dtor.  This frees all data, destroys locks, frees the hash
2564099a0e58SBosko Milekic  * table and removes the keg from the global list.
25659c2cd7e5SJeff Roberson  *
25669c2cd7e5SJeff Roberson  * Arguments/Returns follow uma_dtor specifications
25679c2cd7e5SJeff Roberson  *	udata  unused
25689c2cd7e5SJeff Roberson  */
2569099a0e58SBosko Milekic static void
2570099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata)
2571099a0e58SBosko Milekic {
2572099a0e58SBosko Milekic 	uma_keg_t keg;
25738b987a77SJeff Roberson 	uint32_t free, pages;
25748b987a77SJeff Roberson 	int i;
25759c2cd7e5SJeff Roberson 
2576099a0e58SBosko Milekic 	keg = (uma_keg_t)arg;
25778b987a77SJeff Roberson 	free = pages = 0;
25788b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
25794ab3aee8SMark Johnston 		free += keg->uk_domain[i].ud_free_items;
25808b987a77SJeff Roberson 		pages += keg->uk_domain[i].ud_pages;
25818b987a77SJeff Roberson 		KEG_LOCK_FINI(keg, i);
2582099a0e58SBosko Milekic 	}
25837e240677SRyan Libby 	if (pages != 0)
25848b987a77SJeff Roberson 		printf("Freed UMA keg (%s) was not empty (%u items). "
25858b987a77SJeff Roberson 		    " Lost %u pages of memory.\n",
25868b987a77SJeff Roberson 		    keg->uk_name ? keg->uk_name : "",
25877e240677SRyan Libby 		    pages / keg->uk_ppera * keg->uk_ipers - free, pages);
2588099a0e58SBosko Milekic 
2589099a0e58SBosko Milekic 	hash_free(&keg->uk_hash);
2590099a0e58SBosko Milekic }
2591099a0e58SBosko Milekic 
2592099a0e58SBosko Milekic /*
2593099a0e58SBosko Milekic  * Zone header dtor.
2594099a0e58SBosko Milekic  *
2595099a0e58SBosko Milekic  * Arguments/Returns follow uma_dtor specifications
2596099a0e58SBosko Milekic  *	udata  unused
2597099a0e58SBosko Milekic  */
25989c2cd7e5SJeff Roberson static void
25999c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata)
26009c2cd7e5SJeff Roberson {
26019c2cd7e5SJeff Roberson 	uma_zone_t zone;
2602099a0e58SBosko Milekic 	uma_keg_t keg;
26039c2cd7e5SJeff Roberson 
26049c2cd7e5SJeff Roberson 	zone = (uma_zone_t)arg;
26059643769aSJeff Roberson 
260620a4e154SJeff Roberson 	sysctl_remove_oid(zone->uz_oid, 1, 1);
260720a4e154SJeff Roberson 
2608e20a199fSJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
26099643769aSJeff Roberson 		cache_drain(zone);
2610099a0e58SBosko Milekic 
2611111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
2612099a0e58SBosko Milekic 	LIST_REMOVE(zone, uz_link);
2613111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
2614099a0e58SBosko Milekic 	/*
2615099a0e58SBosko Milekic 	 * XXX there are some races here where
2616099a0e58SBosko Milekic 	 * the zone can be drained but zone lock
2617099a0e58SBosko Milekic 	 * released and then refilled before we
2618099a0e58SBosko Milekic 	 * remove it... we dont care for now
2619099a0e58SBosko Milekic 	 */
262008cfa56eSMark Johnston 	zone_reclaim(zone, M_WAITOK, true);
2621e20a199fSJeff Roberson 	/*
2622323ad386STycho Nightingale 	 * We only destroy kegs from non secondary/non cache zones.
2623e20a199fSJeff Roberson 	 */
2624323ad386STycho Nightingale 	if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) {
2625323ad386STycho Nightingale 		keg = zone->uz_keg;
2626111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
2627099a0e58SBosko Milekic 		LIST_REMOVE(keg, uk_link);
2628111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
26290095a784SJeff Roberson 		zone_free_item(kegs, keg, NULL, SKIP_NONE);
26309c2cd7e5SJeff Roberson 	}
26312efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_allocs);
26322efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_frees);
26332efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_fails);
263420a4e154SJeff Roberson 	free(zone->uz_ctlname, M_UMA);
2635af526374SJeff Roberson 	ZONE_LOCK_FINI(zone);
263691d947bfSJeff Roberson 	ZONE_CROSS_LOCK_FINI(zone);
2637099a0e58SBosko Milekic }
2638099a0e58SBosko Milekic 
2639a81c400eSJeff Roberson static void
2640a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg)
2641a81c400eSJeff Roberson {
2642a81c400eSJeff Roberson 	uma_keg_t keg;
2643a81c400eSJeff Roberson 	uma_zone_t zone;
2644a81c400eSJeff Roberson 
2645a81c400eSJeff Roberson 	LIST_FOREACH(keg, &uma_kegs, uk_link) {
2646a81c400eSJeff Roberson 		LIST_FOREACH(zone, &keg->uk_zones, uz_link)
2647a81c400eSJeff Roberson 			zfunc(zone, arg);
2648a81c400eSJeff Roberson 	}
2649a81c400eSJeff Roberson 	LIST_FOREACH(zone, &uma_cachezones, uz_link)
2650a81c400eSJeff Roberson 		zfunc(zone, arg);
2651a81c400eSJeff Roberson }
2652a81c400eSJeff Roberson 
26539c2cd7e5SJeff Roberson /*
26548355f576SJeff Roberson  * Traverses every zone in the system and calls a callback
26558355f576SJeff Roberson  *
26568355f576SJeff Roberson  * Arguments:
26578355f576SJeff Roberson  *	zfunc  A pointer to a function which accepts a zone
26588355f576SJeff Roberson  *		as an argument.
26598355f576SJeff Roberson  *
26608355f576SJeff Roberson  * Returns:
26618355f576SJeff Roberson  *	Nothing
26628355f576SJeff Roberson  */
26638355f576SJeff Roberson static void
266420a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg)
26658355f576SJeff Roberson {
26668355f576SJeff Roberson 
2667111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
2668a81c400eSJeff Roberson 	zone_foreach_unlocked(zfunc, arg);
2669111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
26708355f576SJeff Roberson }
26718355f576SJeff Roberson 
2672f4bef67cSGleb Smirnoff /*
2673a81c400eSJeff Roberson  * Initialize the kernel memory allocator.  This is done after pages can be
2674a81c400eSJeff Roberson  * allocated but before general KVA is available.
2675f4bef67cSGleb Smirnoff  */
2676a81c400eSJeff Roberson void
2677a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail)
2678f4bef67cSGleb Smirnoff {
2679a81c400eSJeff Roberson 	struct uma_zctor_args args;
2680a81c400eSJeff Roberson 	size_t ksize, zsize, size;
2681a81c400eSJeff Roberson 	uma_keg_t masterkeg;
2682a81c400eSJeff Roberson 	uintptr_t m;
2683a81c400eSJeff Roberson 	uint8_t pflag;
2684a81c400eSJeff Roberson 
2685a81c400eSJeff Roberson 	bootstart = bootmem = virtual_avail;
2686a81c400eSJeff Roberson 
2687a81c400eSJeff Roberson 	rw_init(&uma_rwlock, "UMA lock");
2688a81c400eSJeff Roberson 	sx_init(&uma_reclaim_lock, "umareclaim");
2689f4bef67cSGleb Smirnoff 
2690f4bef67cSGleb Smirnoff 	ksize = sizeof(struct uma_keg) +
2691f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_domain) * vm_ndomains);
269279c9f942SJeff Roberson 	ksize = roundup(ksize, UMA_SUPER_ALIGN);
2693f4bef67cSGleb Smirnoff 	zsize = sizeof(struct uma_zone) +
2694f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_cache) * (mp_maxid + 1)) +
2695f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_zone_domain) * vm_ndomains);
269679c9f942SJeff Roberson 	zsize = roundup(zsize, UMA_SUPER_ALIGN);
2697f4bef67cSGleb Smirnoff 
2698a81c400eSJeff Roberson 	/* Allocate the zone of zones, zone of kegs, and zone of zones keg. */
2699a81c400eSJeff Roberson 	size = (zsize * 2) + ksize;
2700a81c400eSJeff Roberson 	m = (uintptr_t)startup_alloc(NULL, size, 0, &pflag, M_NOWAIT | M_ZERO);
2701ab3185d1SJeff Roberson 	zones = (uma_zone_t)m;
270279c9f942SJeff Roberson 	m += zsize;
2703ab3185d1SJeff Roberson 	kegs = (uma_zone_t)m;
270479c9f942SJeff Roberson 	m += zsize;
2705ab3185d1SJeff Roberson 	masterkeg = (uma_keg_t)m;
2706ab3185d1SJeff Roberson 
2707099a0e58SBosko Milekic 	/* "manually" create the initial zone */
27080095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
2709099a0e58SBosko Milekic 	args.name = "UMA Kegs";
2710ab3185d1SJeff Roberson 	args.size = ksize;
2711099a0e58SBosko Milekic 	args.ctor = keg_ctor;
2712099a0e58SBosko Milekic 	args.dtor = keg_dtor;
27138355f576SJeff Roberson 	args.uminit = zero_init;
27148355f576SJeff Roberson 	args.fini = NULL;
2715ab3185d1SJeff Roberson 	args.keg = masterkeg;
271679c9f942SJeff Roberson 	args.align = UMA_SUPER_ALIGN - 1;
2717b60f5b79SJeff Roberson 	args.flags = UMA_ZFLAG_INTERNAL;
2718ab3185d1SJeff Roberson 	zone_ctor(kegs, zsize, &args, M_WAITOK);
27198355f576SJeff Roberson 
2720099a0e58SBosko Milekic 	args.name = "UMA Zones";
2721f4bef67cSGleb Smirnoff 	args.size = zsize;
2722099a0e58SBosko Milekic 	args.ctor = zone_ctor;
2723099a0e58SBosko Milekic 	args.dtor = zone_dtor;
2724099a0e58SBosko Milekic 	args.uminit = zero_init;
2725099a0e58SBosko Milekic 	args.fini = NULL;
2726099a0e58SBosko Milekic 	args.keg = NULL;
272779c9f942SJeff Roberson 	args.align = UMA_SUPER_ALIGN - 1;
2728099a0e58SBosko Milekic 	args.flags = UMA_ZFLAG_INTERNAL;
2729ab3185d1SJeff Roberson 	zone_ctor(zones, zsize, &args, M_WAITOK);
2730099a0e58SBosko Milekic 
27319b8db4d0SRyan Libby 	/* Now make zones for slab headers */
27329b8db4d0SRyan Libby 	slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE,
27339b8db4d0SRyan Libby 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
27349b8db4d0SRyan Libby 	slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE,
27351e0701e1SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
27368355f576SJeff Roberson 
27378355f576SJeff Roberson 	hashzone = uma_zcreate("UMA Hash",
27388355f576SJeff Roberson 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
27391e0701e1SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
27408355f576SJeff Roberson 
2741a81c400eSJeff Roberson 	bucket_init();
2742d4665eaaSJeff Roberson 	smr_init();
27438355f576SJeff Roberson }
27448355f576SJeff Roberson 
2745a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC
2746a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void);
2747f4bef67cSGleb Smirnoff #endif
2748f4bef67cSGleb Smirnoff 
2749a81c400eSJeff Roberson /*
2750a81c400eSJeff Roberson  * Advertise the availability of normal kva allocations and switch to
2751a81c400eSJeff Roberson  * the default back-end allocator.  Marks the KVA we consumed on startup
2752a81c400eSJeff Roberson  * as used in the map.
2753a81c400eSJeff Roberson  */
27548355f576SJeff Roberson void
275599571dc3SJeff Roberson uma_startup2(void)
27568355f576SJeff Roberson {
2757f4bef67cSGleb Smirnoff 
2758530cc6a2SJeff Roberson 	if (bootstart != bootmem) {
2759a81c400eSJeff Roberson 		vm_map_lock(kernel_map);
2760a81c400eSJeff Roberson 		(void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem,
2761a81c400eSJeff Roberson 		    VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
2762a81c400eSJeff Roberson 		vm_map_unlock(kernel_map);
2763a81c400eSJeff Roberson 	}
2764a81c400eSJeff Roberson 
2765a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC
2766a81c400eSJeff Roberson 	/* Set up radix zone to use noobj_alloc. */
2767a81c400eSJeff Roberson 	vm_radix_reserve_kva();
2768f7d35785SGleb Smirnoff #endif
2769a81c400eSJeff Roberson 
2770a81c400eSJeff Roberson 	booted = BOOT_KVA;
2771a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_kva_available, NULL);
2772f4bef67cSGleb Smirnoff 	bucket_enable();
27738355f576SJeff Roberson }
27748355f576SJeff Roberson 
2775a81c400eSJeff Roberson /*
2776a81c400eSJeff Roberson  * Finish our initialization steps.
2777a81c400eSJeff Roberson  */
27788355f576SJeff Roberson static void
27798355f576SJeff Roberson uma_startup3(void)
27808355f576SJeff Roberson {
27811431a748SGleb Smirnoff 
2782c5deaf04SGleb Smirnoff #ifdef INVARIANTS
2783c5deaf04SGleb Smirnoff 	TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor);
2784c5deaf04SGleb Smirnoff 	uma_dbg_cnt = counter_u64_alloc(M_WAITOK);
2785c5deaf04SGleb Smirnoff 	uma_skip_cnt = counter_u64_alloc(M_WAITOK);
2786c5deaf04SGleb Smirnoff #endif
2787a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_alloc_counters, NULL);
2788a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_alloc_sysctl, NULL);
2789fd90e2edSJung-uk Kim 	callout_init(&uma_callout, 1);
27909643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
2791c5deaf04SGleb Smirnoff 	booted = BOOT_RUNNING;
2792860bb7a0SMark Johnston 
2793860bb7a0SMark Johnston 	EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL,
2794860bb7a0SMark Johnston 	    EVENTHANDLER_PRI_FIRST);
2795860bb7a0SMark Johnston }
2796860bb7a0SMark Johnston 
2797860bb7a0SMark Johnston static void
2798860bb7a0SMark Johnston uma_shutdown(void)
2799860bb7a0SMark Johnston {
2800860bb7a0SMark Johnston 
2801860bb7a0SMark Johnston 	booted = BOOT_SHUTDOWN;
28028355f576SJeff Roberson }
28038355f576SJeff Roberson 
2804e20a199fSJeff Roberson static uma_keg_t
2805099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
280685dcf349SGleb Smirnoff 		int align, uint32_t flags)
2807099a0e58SBosko Milekic {
2808099a0e58SBosko Milekic 	struct uma_kctor_args args;
2809099a0e58SBosko Milekic 
2810099a0e58SBosko Milekic 	args.size = size;
2811099a0e58SBosko Milekic 	args.uminit = uminit;
2812099a0e58SBosko Milekic 	args.fini = fini;
28131e319f6dSRobert Watson 	args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
2814099a0e58SBosko Milekic 	args.flags = flags;
2815099a0e58SBosko Milekic 	args.zone = zone;
2816ab3185d1SJeff Roberson 	return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK));
2817099a0e58SBosko Milekic }
2818099a0e58SBosko Milekic 
2819f4bef67cSGleb Smirnoff /* Public functions */
28208355f576SJeff Roberson /* See uma.h */
28211e319f6dSRobert Watson void
28221e319f6dSRobert Watson uma_set_align(int align)
28231e319f6dSRobert Watson {
28241e319f6dSRobert Watson 
28251e319f6dSRobert Watson 	if (align != UMA_ALIGN_CACHE)
28261e319f6dSRobert Watson 		uma_align_cache = align;
28271e319f6dSRobert Watson }
28281e319f6dSRobert Watson 
28291e319f6dSRobert Watson /* See uma.h */
28308355f576SJeff Roberson uma_zone_t
2831bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
283285dcf349SGleb Smirnoff 		uma_init uminit, uma_fini fini, int align, uint32_t flags)
28338355f576SJeff Roberson 
28348355f576SJeff Roberson {
28358355f576SJeff Roberson 	struct uma_zctor_args args;
283695c4bf75SKonstantin Belousov 	uma_zone_t res;
28378355f576SJeff Roberson 
2838a5a35578SJohn Baldwin 	KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
2839a5a35578SJohn Baldwin 	    align, name));
2840a5a35578SJohn Baldwin 
28418355f576SJeff Roberson 	/* This stuff is essential for the zone ctor */
28420095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
28438355f576SJeff Roberson 	args.name = name;
28448355f576SJeff Roberson 	args.size = size;
28458355f576SJeff Roberson 	args.ctor = ctor;
28468355f576SJeff Roberson 	args.dtor = dtor;
28478355f576SJeff Roberson 	args.uminit = uminit;
28488355f576SJeff Roberson 	args.fini = fini;
2849afc6dc36SJohn-Mark Gurney #ifdef  INVARIANTS
2850afc6dc36SJohn-Mark Gurney 	/*
2851ca293436SRyan Libby 	 * Inject procedures which check for memory use after free if we are
2852ca293436SRyan Libby 	 * allowed to scramble the memory while it is not allocated.  This
2853ca293436SRyan Libby 	 * requires that: UMA is actually able to access the memory, no init
2854ca293436SRyan Libby 	 * or fini procedures, no dependency on the initial value of the
2855ca293436SRyan Libby 	 * memory, and no (legitimate) use of the memory after free.  Note,
2856ca293436SRyan Libby 	 * the ctor and dtor do not need to be empty.
2857afc6dc36SJohn-Mark Gurney 	 */
285854c5ae80SRyan Libby 	if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH |
285954c5ae80SRyan Libby 	    UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) {
2860afc6dc36SJohn-Mark Gurney 		args.uminit = trash_init;
2861afc6dc36SJohn-Mark Gurney 		args.fini = trash_fini;
2862afc6dc36SJohn-Mark Gurney 	}
2863afc6dc36SJohn-Mark Gurney #endif
28648355f576SJeff Roberson 	args.align = align;
28658355f576SJeff Roberson 	args.flags = flags;
2866099a0e58SBosko Milekic 	args.keg = NULL;
2867099a0e58SBosko Milekic 
286808cfa56eSMark Johnston 	sx_slock(&uma_reclaim_lock);
2869ab3185d1SJeff Roberson 	res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
287008cfa56eSMark Johnston 	sx_sunlock(&uma_reclaim_lock);
2871a81c400eSJeff Roberson 
287295c4bf75SKonstantin Belousov 	return (res);
2873099a0e58SBosko Milekic }
2874099a0e58SBosko Milekic 
2875099a0e58SBosko Milekic /* See uma.h */
2876099a0e58SBosko Milekic uma_zone_t
2877099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
2878099a0e58SBosko Milekic 		    uma_init zinit, uma_fini zfini, uma_zone_t master)
2879099a0e58SBosko Milekic {
2880099a0e58SBosko Milekic 	struct uma_zctor_args args;
2881e20a199fSJeff Roberson 	uma_keg_t keg;
288295c4bf75SKonstantin Belousov 	uma_zone_t res;
2883099a0e58SBosko Milekic 
2884bb15d1c7SGleb Smirnoff 	keg = master->uz_keg;
28850095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
2886099a0e58SBosko Milekic 	args.name = name;
2887e20a199fSJeff Roberson 	args.size = keg->uk_size;
2888099a0e58SBosko Milekic 	args.ctor = ctor;
2889099a0e58SBosko Milekic 	args.dtor = dtor;
2890099a0e58SBosko Milekic 	args.uminit = zinit;
2891099a0e58SBosko Milekic 	args.fini = zfini;
2892e20a199fSJeff Roberson 	args.align = keg->uk_align;
2893e20a199fSJeff Roberson 	args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
2894e20a199fSJeff Roberson 	args.keg = keg;
28958355f576SJeff Roberson 
289608cfa56eSMark Johnston 	sx_slock(&uma_reclaim_lock);
2897ab3185d1SJeff Roberson 	res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
289808cfa56eSMark Johnston 	sx_sunlock(&uma_reclaim_lock);
2899a81c400eSJeff Roberson 
290095c4bf75SKonstantin Belousov 	return (res);
29018355f576SJeff Roberson }
29028355f576SJeff Roberson 
29030095a784SJeff Roberson /* See uma.h */
29040095a784SJeff Roberson uma_zone_t
2905af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
2906af526374SJeff Roberson 		    uma_init zinit, uma_fini zfini, uma_import zimport,
2907af526374SJeff Roberson 		    uma_release zrelease, void *arg, int flags)
29080095a784SJeff Roberson {
29090095a784SJeff Roberson 	struct uma_zctor_args args;
29100095a784SJeff Roberson 
29110095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
29120095a784SJeff Roberson 	args.name = name;
2913af526374SJeff Roberson 	args.size = size;
29140095a784SJeff Roberson 	args.ctor = ctor;
29150095a784SJeff Roberson 	args.dtor = dtor;
29160095a784SJeff Roberson 	args.uminit = zinit;
29170095a784SJeff Roberson 	args.fini = zfini;
29180095a784SJeff Roberson 	args.import = zimport;
29190095a784SJeff Roberson 	args.release = zrelease;
29200095a784SJeff Roberson 	args.arg = arg;
29210095a784SJeff Roberson 	args.align = 0;
2922bb15d1c7SGleb Smirnoff 	args.flags = flags | UMA_ZFLAG_CACHE;
29230095a784SJeff Roberson 
2924ab3185d1SJeff Roberson 	return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK));
29250095a784SJeff Roberson }
29260095a784SJeff Roberson 
29278355f576SJeff Roberson /* See uma.h */
29289c2cd7e5SJeff Roberson void
29299c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone)
29309c2cd7e5SJeff Roberson {
2931f4ff923bSRobert Watson 
2932860bb7a0SMark Johnston 	/*
2933860bb7a0SMark Johnston 	 * Large slabs are expensive to reclaim, so don't bother doing
2934860bb7a0SMark Johnston 	 * unnecessary work if we're shutting down.
2935860bb7a0SMark Johnston 	 */
2936860bb7a0SMark Johnston 	if (booted == BOOT_SHUTDOWN &&
2937860bb7a0SMark Johnston 	    zone->uz_fini == NULL && zone->uz_release == zone_release)
2938860bb7a0SMark Johnston 		return;
293908cfa56eSMark Johnston 	sx_slock(&uma_reclaim_lock);
29400095a784SJeff Roberson 	zone_free_item(zones, zone, NULL, SKIP_NONE);
294108cfa56eSMark Johnston 	sx_sunlock(&uma_reclaim_lock);
29429c2cd7e5SJeff Roberson }
29439c2cd7e5SJeff Roberson 
29448d6fbbb8SJeff Roberson void
29458d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone)
29468d6fbbb8SJeff Roberson {
29478d6fbbb8SJeff Roberson 
294870260874SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
294970260874SJeff Roberson 		uma_zfree_smr(zone, uma_zalloc_smr(zone, M_WAITOK));
295070260874SJeff Roberson 	else if ((zone->uz_flags & UMA_ZONE_PCPU) != 0)
295170260874SJeff Roberson 		uma_zfree_pcpu(zone, uma_zalloc_pcpu(zone, M_WAITOK));
295270260874SJeff Roberson 	else
295370260874SJeff Roberson 		uma_zfree(zone, uma_zalloc(zone, M_WAITOK));
29548d6fbbb8SJeff Roberson }
29558d6fbbb8SJeff Roberson 
29564e180881SMateusz Guzik void *
29574e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags)
29584e180881SMateusz Guzik {
29593acb6572SMateusz Guzik 	void *item, *pcpu_item;
2960b4799947SRuslan Bukin #ifdef SMP
29614e180881SMateusz Guzik 	int i;
29624e180881SMateusz Guzik 
29634e180881SMateusz Guzik 	MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2964b4799947SRuslan Bukin #endif
29654e180881SMateusz Guzik 	item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO);
29663acb6572SMateusz Guzik 	if (item == NULL)
29673acb6572SMateusz Guzik 		return (NULL);
29683acb6572SMateusz Guzik 	pcpu_item = zpcpu_base_to_offset(item);
29693acb6572SMateusz Guzik 	if (flags & M_ZERO) {
2970b4799947SRuslan Bukin #ifdef SMP
2971013072f0SMark Johnston 		for (i = 0; i <= mp_maxid; i++)
29723acb6572SMateusz Guzik 			bzero(zpcpu_get_cpu(pcpu_item, i), zone->uz_size);
2973b4799947SRuslan Bukin #else
2974b4799947SRuslan Bukin 		bzero(item, zone->uz_size);
2975b4799947SRuslan Bukin #endif
29764e180881SMateusz Guzik 	}
29773acb6572SMateusz Guzik 	return (pcpu_item);
29784e180881SMateusz Guzik }
29794e180881SMateusz Guzik 
29804e180881SMateusz Guzik /*
29814e180881SMateusz Guzik  * A stub while both regular and pcpu cases are identical.
29824e180881SMateusz Guzik  */
29834e180881SMateusz Guzik void
29843acb6572SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *pcpu_item, void *udata)
29854e180881SMateusz Guzik {
29863acb6572SMateusz Guzik 	void *item;
29874e180881SMateusz Guzik 
2988c5b7751fSIan Lepore #ifdef SMP
29894e180881SMateusz Guzik 	MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2990c5b7751fSIan Lepore #endif
29913acb6572SMateusz Guzik 	item = zpcpu_offset_to_base(pcpu_item);
29924e180881SMateusz Guzik 	uma_zfree_arg(zone, item, udata);
29934e180881SMateusz Guzik }
29944e180881SMateusz Guzik 
2995d4665eaaSJeff Roberson static inline void *
2996d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags,
2997d4665eaaSJeff Roberson     void *item)
2998beb8beefSJeff Roberson {
2999beb8beefSJeff Roberson #ifdef INVARIANTS
3000ca293436SRyan Libby 	bool skipdbg;
3001beb8beefSJeff Roberson 
3002beb8beefSJeff Roberson 	skipdbg = uma_dbg_zskip(zone, item);
3003ca293436SRyan Libby 	if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3004ca293436SRyan Libby 	    zone->uz_ctor != trash_ctor)
3005cc7ce83aSJeff Roberson 		trash_ctor(item, size, udata, flags);
3006beb8beefSJeff Roberson #endif
3007d4665eaaSJeff Roberson 	/* Check flags before loading ctor pointer. */
3008d4665eaaSJeff Roberson 	if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) &&
3009d4665eaaSJeff Roberson 	    __predict_false(zone->uz_ctor != NULL) &&
3010cc7ce83aSJeff Roberson 	    zone->uz_ctor(item, size, udata, flags) != 0) {
3011beb8beefSJeff Roberson 		counter_u64_add(zone->uz_fails, 1);
3012beb8beefSJeff Roberson 		zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT);
3013beb8beefSJeff Roberson 		return (NULL);
3014beb8beefSJeff Roberson 	}
3015beb8beefSJeff Roberson #ifdef INVARIANTS
3016beb8beefSJeff Roberson 	if (!skipdbg)
3017beb8beefSJeff Roberson 		uma_dbg_alloc(zone, NULL, item);
3018beb8beefSJeff Roberson #endif
30196d88d784SJeff Roberson 	if (__predict_false(flags & M_ZERO))
30206d88d784SJeff Roberson 		return (memset(item, 0, size));
3021beb8beefSJeff Roberson 
3022beb8beefSJeff Roberson 	return (item);
3023beb8beefSJeff Roberson }
3024beb8beefSJeff Roberson 
3025ca293436SRyan Libby static inline void
3026cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata,
3027cc7ce83aSJeff Roberson     enum zfreeskip skip)
3028ca293436SRyan Libby {
3029ca293436SRyan Libby #ifdef INVARIANTS
3030ca293436SRyan Libby 	bool skipdbg;
3031ca293436SRyan Libby 
3032ca293436SRyan Libby 	skipdbg = uma_dbg_zskip(zone, item);
3033ca293436SRyan Libby 	if (skip == SKIP_NONE && !skipdbg) {
3034ca293436SRyan Libby 		if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0)
3035ca293436SRyan Libby 			uma_dbg_free(zone, udata, item);
3036ca293436SRyan Libby 		else
3037ca293436SRyan Libby 			uma_dbg_free(zone, NULL, item);
3038ca293436SRyan Libby 	}
3039ca293436SRyan Libby #endif
3040cc7ce83aSJeff Roberson 	if (__predict_true(skip < SKIP_DTOR)) {
3041ca293436SRyan Libby 		if (zone->uz_dtor != NULL)
3042cc7ce83aSJeff Roberson 			zone->uz_dtor(item, size, udata);
3043ca293436SRyan Libby #ifdef INVARIANTS
3044ca293436SRyan Libby 		if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3045ca293436SRyan Libby 		    zone->uz_dtor != trash_dtor)
3046cc7ce83aSJeff Roberson 			trash_dtor(item, size, udata);
3047ca293436SRyan Libby #endif
3048ca293436SRyan Libby 	}
3049ca293436SRyan Libby }
3050ca293436SRyan Libby 
3051d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS)
3052d4665eaaSJeff Roberson #define	UMA_ZALLOC_DEBUG
3053d4665eaaSJeff Roberson static int
3054d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags)
3055d4665eaaSJeff Roberson {
3056d4665eaaSJeff Roberson 	int error;
3057d4665eaaSJeff Roberson 
3058d4665eaaSJeff Roberson 	error = 0;
3059d4665eaaSJeff Roberson #ifdef WITNESS
3060d4665eaaSJeff Roberson 	if (flags & M_WAITOK) {
3061d4665eaaSJeff Roberson 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
3062d4665eaaSJeff Roberson 		    "uma_zalloc_debug: zone \"%s\"", zone->uz_name);
3063d4665eaaSJeff Roberson 	}
3064d4665eaaSJeff Roberson #endif
3065d4665eaaSJeff Roberson 
3066d4665eaaSJeff Roberson #ifdef INVARIANTS
3067d4665eaaSJeff Roberson 	KASSERT((flags & M_EXEC) == 0,
3068d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: called with M_EXEC"));
3069d4665eaaSJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3070d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: called within spinlock or critical section"));
3071d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0,
3072d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO"));
3073d4665eaaSJeff Roberson #endif
3074d4665eaaSJeff Roberson 
3075d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD
30769e47b341SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) {
3077d4665eaaSJeff Roberson 		void *item;
3078d4665eaaSJeff Roberson 		item = memguard_alloc(zone->uz_size, flags);
3079d4665eaaSJeff Roberson 		if (item != NULL) {
3080d4665eaaSJeff Roberson 			error = EJUSTRETURN;
3081d4665eaaSJeff Roberson 			if (zone->uz_init != NULL &&
3082d4665eaaSJeff Roberson 			    zone->uz_init(item, zone->uz_size, flags) != 0) {
3083d4665eaaSJeff Roberson 				*itemp = NULL;
3084d4665eaaSJeff Roberson 				return (error);
3085d4665eaaSJeff Roberson 			}
3086d4665eaaSJeff Roberson 			if (zone->uz_ctor != NULL &&
3087d4665eaaSJeff Roberson 			    zone->uz_ctor(item, zone->uz_size, udata,
3088d4665eaaSJeff Roberson 			    flags) != 0) {
3089d4665eaaSJeff Roberson 				counter_u64_add(zone->uz_fails, 1);
3090d4665eaaSJeff Roberson 			    	zone->uz_fini(item, zone->uz_size);
3091d4665eaaSJeff Roberson 				*itemp = NULL;
3092d4665eaaSJeff Roberson 				return (error);
3093d4665eaaSJeff Roberson 			}
3094d4665eaaSJeff Roberson 			*itemp = item;
3095d4665eaaSJeff Roberson 			return (error);
3096d4665eaaSJeff Roberson 		}
3097d4665eaaSJeff Roberson 		/* This is unfortunate but should not be fatal. */
3098d4665eaaSJeff Roberson 	}
3099d4665eaaSJeff Roberson #endif
3100d4665eaaSJeff Roberson 	return (error);
3101d4665eaaSJeff Roberson }
3102d4665eaaSJeff Roberson 
3103d4665eaaSJeff Roberson static int
3104d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata)
3105d4665eaaSJeff Roberson {
3106d4665eaaSJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3107d4665eaaSJeff Roberson 	    ("uma_zfree_debug: called with spinlock or critical section held"));
3108d4665eaaSJeff Roberson 
3109d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD
31109e47b341SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) {
3111d4665eaaSJeff Roberson 		if (zone->uz_dtor != NULL)
3112d4665eaaSJeff Roberson 			zone->uz_dtor(item, zone->uz_size, udata);
3113d4665eaaSJeff Roberson 		if (zone->uz_fini != NULL)
3114d4665eaaSJeff Roberson 			zone->uz_fini(item, zone->uz_size);
3115d4665eaaSJeff Roberson 		memguard_free(item);
3116d4665eaaSJeff Roberson 		return (EJUSTRETURN);
3117d4665eaaSJeff Roberson 	}
3118d4665eaaSJeff Roberson #endif
3119d4665eaaSJeff Roberson 	return (0);
3120d4665eaaSJeff Roberson }
3121d4665eaaSJeff Roberson #endif
3122d4665eaaSJeff Roberson 
31236d88d784SJeff Roberson static inline void *
31246d88d784SJeff Roberson cache_alloc_item(uma_zone_t zone, uma_cache_t cache, uma_cache_bucket_t bucket,
31256d88d784SJeff Roberson     void *udata, int flags)
3126d4665eaaSJeff Roberson {
31276d88d784SJeff Roberson 	void *item;
31286d88d784SJeff Roberson 	int size, uz_flags;
31296d88d784SJeff Roberson 
31306d88d784SJeff Roberson 	item = cache_bucket_pop(cache, bucket);
31316d88d784SJeff Roberson 	size = cache_uz_size(cache);
31326d88d784SJeff Roberson 	uz_flags = cache_uz_flags(cache);
31336d88d784SJeff Roberson 	critical_exit();
31346d88d784SJeff Roberson 	return (item_ctor(zone, uz_flags, size, udata, flags, item));
31356d88d784SJeff Roberson }
31366d88d784SJeff Roberson 
31376d88d784SJeff Roberson static __noinline void *
31386d88d784SJeff Roberson cache_alloc_retry(uma_zone_t zone, uma_cache_t cache, void *udata, int flags)
31396d88d784SJeff Roberson {
31406d88d784SJeff Roberson 	uma_cache_bucket_t bucket;
3141d4665eaaSJeff Roberson 	int domain;
3142d4665eaaSJeff Roberson 
31436d88d784SJeff Roberson 	while (cache_alloc(zone, cache, udata, flags)) {
31446d88d784SJeff Roberson 		cache = &zone->uz_cpu[curcpu];
31456d88d784SJeff Roberson 		bucket = &cache->uc_allocbucket;
31466d88d784SJeff Roberson 		if (__predict_false(bucket->ucb_cnt == 0))
31476d88d784SJeff Roberson 			continue;
31486d88d784SJeff Roberson 		return (cache_alloc_item(zone, cache, bucket, udata, flags));
31496d88d784SJeff Roberson 	}
31506d88d784SJeff Roberson 	critical_exit();
31516d88d784SJeff Roberson 
3152d4665eaaSJeff Roberson 	/*
3153d4665eaaSJeff Roberson 	 * We can not get a bucket so try to return a single item.
3154d4665eaaSJeff Roberson 	 */
3155d4665eaaSJeff Roberson 	if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH)
3156d4665eaaSJeff Roberson 		domain = PCPU_GET(domain);
3157d4665eaaSJeff Roberson 	else
3158d4665eaaSJeff Roberson 		domain = UMA_ANYDOMAIN;
3159d4665eaaSJeff Roberson 	return (zone_alloc_item(zone, udata, domain, flags));
3160d4665eaaSJeff Roberson }
3161d4665eaaSJeff Roberson 
3162d4665eaaSJeff Roberson /* See uma.h */
3163d4665eaaSJeff Roberson void *
3164d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags)
3165d4665eaaSJeff Roberson {
3166d4665eaaSJeff Roberson 	uma_cache_bucket_t bucket;
3167d4665eaaSJeff Roberson 	uma_cache_t cache;
3168d4665eaaSJeff Roberson 
3169d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
31706d88d784SJeff Roberson 	void *item;
31716d88d784SJeff Roberson 
3172d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
3173d4665eaaSJeff Roberson 	    ("uma_zalloc_arg: called with non-SMR zone.\n"));
3174d4665eaaSJeff Roberson 	if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN)
3175d4665eaaSJeff Roberson 		return (item);
3176d4665eaaSJeff Roberson #endif
3177d4665eaaSJeff Roberson 
3178d4665eaaSJeff Roberson 	critical_enter();
3179d4665eaaSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3180d4665eaaSJeff Roberson 	bucket = &cache->uc_allocbucket;
31816d88d784SJeff Roberson 	if (__predict_false(bucket->ucb_cnt == 0))
31826d88d784SJeff Roberson 		return (cache_alloc_retry(zone, cache, NULL, flags));
31836d88d784SJeff Roberson 	return (cache_alloc_item(zone, cache, bucket, NULL, flags));
3184d4665eaaSJeff Roberson }
3185d4665eaaSJeff Roberson 
31869c2cd7e5SJeff Roberson /* See uma.h */
31878355f576SJeff Roberson void *
31882cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
31898355f576SJeff Roberson {
3190376b1ba3SJeff Roberson 	uma_cache_bucket_t bucket;
3191ab3185d1SJeff Roberson 	uma_cache_t cache;
31928355f576SJeff Roberson 
3193e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
319419fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
319510cb2424SMark Murray 
31968355f576SJeff Roberson 	/* This is the fast path allocation */
3197e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name,
3198e63a1c2fSRyan Libby 	    zone, flags);
3199a553d4b8SJeff Roberson 
3200d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
32016d88d784SJeff Roberson 	void *item;
32026d88d784SJeff Roberson 
3203d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
3204d4665eaaSJeff Roberson 	    ("uma_zalloc_arg: called with SMR zone.\n"));
3205d4665eaaSJeff Roberson 	if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN)
32068d689e04SGleb Smirnoff 		return (item);
32078d689e04SGleb Smirnoff #endif
3208d4665eaaSJeff Roberson 
32095d1ae027SRobert Watson 	/*
32105d1ae027SRobert Watson 	 * If possible, allocate from the per-CPU cache.  There are two
32115d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
32125d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
32135d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
32145d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
32155d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
32165d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to allocate from
32175d1ae027SRobert Watson 	 * the current cache; when we re-acquire the critical section, we
32185d1ae027SRobert Watson 	 * must detect and handle migration if it has occurred.
32195d1ae027SRobert Watson 	 */
32205d1ae027SRobert Watson 	critical_enter();
3221cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3222376b1ba3SJeff Roberson 	bucket = &cache->uc_allocbucket;
32236d88d784SJeff Roberson 	if (__predict_false(bucket->ucb_cnt == 0))
32246d88d784SJeff Roberson 		return (cache_alloc_retry(zone, cache, udata, flags));
32256d88d784SJeff Roberson 	return (cache_alloc_item(zone, cache, bucket, udata, flags));
3226fc03d22bSJeff Roberson }
3227fc03d22bSJeff Roberson 
32288355f576SJeff Roberson /*
3229beb8beefSJeff Roberson  * Replenish an alloc bucket and possibly restore an old one.  Called in
3230beb8beefSJeff Roberson  * a critical section.  Returns in a critical section.
3231beb8beefSJeff Roberson  *
32324bd61e19SJeff Roberson  * A false return value indicates an allocation failure.
32334bd61e19SJeff Roberson  * A true return value indicates success and the caller should retry.
3234beb8beefSJeff Roberson  */
3235beb8beefSJeff Roberson static __noinline bool
3236beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags)
3237beb8beefSJeff Roberson {
3238beb8beefSJeff Roberson 	uma_zone_domain_t zdom;
3239beb8beefSJeff Roberson 	uma_bucket_t bucket;
3240cc7ce83aSJeff Roberson 	int domain;
3241beb8beefSJeff Roberson 	bool lockfail;
3242beb8beefSJeff Roberson 
3243beb8beefSJeff Roberson 	CRITICAL_ASSERT(curthread);
3244beb8beefSJeff Roberson 
3245beb8beefSJeff Roberson 	/*
3246beb8beefSJeff Roberson 	 * If we have run out of items in our alloc bucket see
3247beb8beefSJeff Roberson 	 * if we can switch with the free bucket.
3248d4665eaaSJeff Roberson 	 *
3249d4665eaaSJeff Roberson 	 * SMR Zones can't re-use the free bucket until the sequence has
3250d4665eaaSJeff Roberson 	 * expired.
32518355f576SJeff Roberson 	 */
3252d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 &&
3253d4665eaaSJeff Roberson 	    cache->uc_freebucket.ucb_cnt != 0) {
3254d4665eaaSJeff Roberson 		cache_bucket_swap(&cache->uc_freebucket,
3255d4665eaaSJeff Roberson 		    &cache->uc_allocbucket);
3256beb8beefSJeff Roberson 		return (true);
32578355f576SJeff Roberson 	}
3258fc03d22bSJeff Roberson 
3259fc03d22bSJeff Roberson 	/*
3260fc03d22bSJeff Roberson 	 * Discard any empty allocation bucket while we hold no locks.
3261fc03d22bSJeff Roberson 	 */
3262376b1ba3SJeff Roberson 	bucket = cache_bucket_unload_alloc(cache);
3263fc03d22bSJeff Roberson 	critical_exit();
3264fc03d22bSJeff Roberson 	if (bucket != NULL)
32656fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
3266fc03d22bSJeff Roberson 
32674bd61e19SJeff Roberson 	/* Short-circuit for zones without buckets and low memory. */
32684bd61e19SJeff Roberson 	if (zone->uz_bucket_size == 0 || bucketdisable) {
32694bd61e19SJeff Roberson 		critical_enter();
32704bd61e19SJeff Roberson 		return (false);
32714bd61e19SJeff Roberson 	}
32724bd61e19SJeff Roberson 
32735d1ae027SRobert Watson 	/*
32745d1ae027SRobert Watson 	 * Attempt to retrieve the item from the per-CPU cache has failed, so
32755d1ae027SRobert Watson 	 * we must go back to the zone.  This requires the zone lock, so we
32765d1ae027SRobert Watson 	 * must drop the critical section, then re-acquire it when we go back
32775d1ae027SRobert Watson 	 * to the cache.  Since the critical section is released, we may be
32785d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
32795d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
32805d1ae027SRobert Watson 	 * the critical section.
32815d1ae027SRobert Watson 	 */
3282fc03d22bSJeff Roberson 	lockfail = 0;
3283fc03d22bSJeff Roberson 	if (ZONE_TRYLOCK(zone) == 0) {
3284fc03d22bSJeff Roberson 		/* Record contention to size the buckets. */
3285a553d4b8SJeff Roberson 		ZONE_LOCK(zone);
3286fc03d22bSJeff Roberson 		lockfail = 1;
3287fc03d22bSJeff Roberson 	}
3288beb8beefSJeff Roberson 
3289fc03d22bSJeff Roberson 	/* See if we lost the race to fill the cache. */
32904bd61e19SJeff Roberson 	critical_enter();
32914bd61e19SJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3292376b1ba3SJeff Roberson 	if (cache->uc_allocbucket.ucb_bucket != NULL) {
3293fc03d22bSJeff Roberson 		ZONE_UNLOCK(zone);
3294beb8beefSJeff Roberson 		return (true);
3295a553d4b8SJeff Roberson 	}
32968355f576SJeff Roberson 
3297fc03d22bSJeff Roberson 	/*
3298fc03d22bSJeff Roberson 	 * Check the zone's cache of buckets.
3299fc03d22bSJeff Roberson 	 */
3300dfe13344SJeff Roberson 	if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) {
3301c1685086SJeff Roberson 		domain = PCPU_GET(domain);
3302ab3185d1SJeff Roberson 		zdom = &zone->uz_domain[domain];
3303c1685086SJeff Roberson 	} else {
3304c1685086SJeff Roberson 		domain = UMA_ANYDOMAIN;
3305c1685086SJeff Roberson 		zdom = &zone->uz_domain[0];
3306c1685086SJeff Roberson 	}
3307c1685086SJeff Roberson 
330808cfa56eSMark Johnston 	if ((bucket = zone_fetch_bucket(zone, zdom)) != NULL) {
3309cae33c14SJeff Roberson 		KASSERT(bucket->ub_cnt != 0,
3310a553d4b8SJeff Roberson 		    ("uma_zalloc_arg: Returning an empty bucket."));
3311376b1ba3SJeff Roberson 		cache_bucket_load_alloc(cache, bucket);
3312beb8beefSJeff Roberson 		return (true);
3313a553d4b8SJeff Roberson 	}
33145d1ae027SRobert Watson 	/* We are no longer associated with this CPU. */
33155d1ae027SRobert Watson 	critical_exit();
3316bbee39c6SJeff Roberson 
3317fc03d22bSJeff Roberson 	/*
3318fc03d22bSJeff Roberson 	 * We bump the uz count when the cache size is insufficient to
3319fc03d22bSJeff Roberson 	 * handle the working set.
3320fc03d22bSJeff Roberson 	 */
332120a4e154SJeff Roberson 	if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max)
332220a4e154SJeff Roberson 		zone->uz_bucket_size++;
33234bd61e19SJeff Roberson 	ZONE_UNLOCK(zone);
3324bb15d1c7SGleb Smirnoff 
33258355f576SJeff Roberson 	/*
3326beb8beefSJeff Roberson 	 * Fill a bucket and attempt to use it as the alloc bucket.
3327bbee39c6SJeff Roberson 	 */
3328beb8beefSJeff Roberson 	bucket = zone_alloc_bucket(zone, udata, domain, flags);
33291431a748SGleb Smirnoff 	CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
33301431a748SGleb Smirnoff 	    zone->uz_name, zone, bucket);
33314bd61e19SJeff Roberson 	if (bucket == NULL) {
3332fc03d22bSJeff Roberson 		critical_enter();
3333beb8beefSJeff Roberson 		return (false);
33344bd61e19SJeff Roberson 	}
33350f9b7bf3SMark Johnston 
3336fc03d22bSJeff Roberson 	/*
3337fc03d22bSJeff Roberson 	 * See if we lost the race or were migrated.  Cache the
3338fc03d22bSJeff Roberson 	 * initialized bucket to make this less likely or claim
3339fc03d22bSJeff Roberson 	 * the memory directly.
3340fc03d22bSJeff Roberson 	 */
33414bd61e19SJeff Roberson 	ZONE_LOCK(zone);
33424bd61e19SJeff Roberson 	critical_enter();
3343cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3344376b1ba3SJeff Roberson 	if (cache->uc_allocbucket.ucb_bucket == NULL &&
3345dfe13344SJeff Roberson 	    ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0 ||
334681c0d72cSGleb Smirnoff 	    domain == PCPU_GET(domain))) {
3347376b1ba3SJeff Roberson 		cache_bucket_load_alloc(cache, bucket);
33480f9b7bf3SMark Johnston 		zdom->uzd_imax += bucket->ub_cnt;
3349bb15d1c7SGleb Smirnoff 	} else if (zone->uz_bkt_count >= zone->uz_bkt_max) {
335081c0d72cSGleb Smirnoff 		critical_exit();
335181c0d72cSGleb Smirnoff 		ZONE_UNLOCK(zone);
335281c0d72cSGleb Smirnoff 		bucket_drain(zone, bucket);
335381c0d72cSGleb Smirnoff 		bucket_free(zone, bucket, udata);
3354beb8beefSJeff Roberson 		critical_enter();
3355beb8beefSJeff Roberson 		return (true);
335681c0d72cSGleb Smirnoff 	} else
33570f9b7bf3SMark Johnston 		zone_put_bucket(zone, zdom, bucket, false);
3358bbee39c6SJeff Roberson 	ZONE_UNLOCK(zone);
3359beb8beefSJeff Roberson 	return (true);
3360bbee39c6SJeff Roberson }
3361bbee39c6SJeff Roberson 
3362ab3185d1SJeff Roberson void *
3363ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
3364bbee39c6SJeff Roberson {
3365ab3185d1SJeff Roberson 
3366ab3185d1SJeff Roberson 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
336719fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3368ab3185d1SJeff Roberson 
3369ab3185d1SJeff Roberson 	/* This is the fast path allocation */
3370e63a1c2fSRyan Libby 	CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d",
3371e63a1c2fSRyan Libby 	    zone->uz_name, zone, domain, flags);
3372ab3185d1SJeff Roberson 
3373ab3185d1SJeff Roberson 	if (flags & M_WAITOK) {
3374ab3185d1SJeff Roberson 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
3375ab3185d1SJeff Roberson 		    "uma_zalloc_domain: zone \"%s\"", zone->uz_name);
3376ab3185d1SJeff Roberson 	}
3377ab3185d1SJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3378ab3185d1SJeff Roberson 	    ("uma_zalloc_domain: called with spinlock or critical section held"));
3379ab3185d1SJeff Roberson 
3380ab3185d1SJeff Roberson 	return (zone_alloc_item(zone, udata, domain, flags));
3381ab3185d1SJeff Roberson }
3382ab3185d1SJeff Roberson 
3383ab3185d1SJeff Roberson /*
3384ab3185d1SJeff Roberson  * Find a slab with some space.  Prefer slabs that are partially used over those
3385ab3185d1SJeff Roberson  * that are totally full.  This helps to reduce fragmentation.
3386ab3185d1SJeff Roberson  *
3387ab3185d1SJeff Roberson  * If 'rr' is 1, search all domains starting from 'domain'.  Otherwise check
3388ab3185d1SJeff Roberson  * only 'domain'.
3389ab3185d1SJeff Roberson  */
3390ab3185d1SJeff Roberson static uma_slab_t
3391194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr)
3392ab3185d1SJeff Roberson {
3393ab3185d1SJeff Roberson 	uma_domain_t dom;
3394bbee39c6SJeff Roberson 	uma_slab_t slab;
3395ab3185d1SJeff Roberson 	int start;
3396ab3185d1SJeff Roberson 
3397ab3185d1SJeff Roberson 	KASSERT(domain >= 0 && domain < vm_ndomains,
3398ab3185d1SJeff Roberson 	    ("keg_first_slab: domain %d out of range", domain));
33998b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, domain);
3400ab3185d1SJeff Roberson 
3401ab3185d1SJeff Roberson 	slab = NULL;
3402ab3185d1SJeff Roberson 	start = domain;
3403ab3185d1SJeff Roberson 	do {
3404ab3185d1SJeff Roberson 		dom = &keg->uk_domain[domain];
34054ab3aee8SMark Johnston 		if ((slab = LIST_FIRST(&dom->ud_part_slab)) != NULL)
34064ab3aee8SMark Johnston 			return (slab);
34074ab3aee8SMark Johnston 		if ((slab = LIST_FIRST(&dom->ud_free_slab)) != NULL) {
3408ab3185d1SJeff Roberson 			LIST_REMOVE(slab, us_link);
34094ab3aee8SMark Johnston 			dom->ud_free_slabs--;
3410ab3185d1SJeff Roberson 			LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
3411ab3185d1SJeff Roberson 			return (slab);
3412ab3185d1SJeff Roberson 		}
3413ab3185d1SJeff Roberson 		if (rr)
3414ab3185d1SJeff Roberson 			domain = (domain + 1) % vm_ndomains;
3415ab3185d1SJeff Roberson 	} while (domain != start);
3416ab3185d1SJeff Roberson 
3417ab3185d1SJeff Roberson 	return (NULL);
3418ab3185d1SJeff Roberson }
3419ab3185d1SJeff Roberson 
34208b987a77SJeff Roberson /*
34218b987a77SJeff Roberson  * Fetch an existing slab from a free or partial list.  Returns with the
34228b987a77SJeff Roberson  * keg domain lock held if a slab was found or unlocked if not.
34238b987a77SJeff Roberson  */
3424ab3185d1SJeff Roberson static uma_slab_t
3425194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags)
3426ab3185d1SJeff Roberson {
34278b987a77SJeff Roberson 	uma_slab_t slab;
3428194a979eSMark Johnston 	uint32_t reserve;
3429099a0e58SBosko Milekic 
34308b987a77SJeff Roberson 	/* HASH has a single free list. */
343154c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0)
34328b987a77SJeff Roberson 		domain = 0;
3433194a979eSMark Johnston 
34348b987a77SJeff Roberson 	KEG_LOCK(keg, domain);
3435194a979eSMark Johnston 	reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve;
34364ab3aee8SMark Johnston 	if (keg->uk_domain[domain].ud_free_items <= reserve ||
34378b987a77SJeff Roberson 	    (slab = keg_first_slab(keg, domain, rr)) == NULL) {
34388b987a77SJeff Roberson 		KEG_UNLOCK(keg, domain);
3439194a979eSMark Johnston 		return (NULL);
34408b987a77SJeff Roberson 	}
34418b987a77SJeff Roberson 	return (slab);
3442194a979eSMark Johnston }
3443194a979eSMark Johnston 
3444194a979eSMark Johnston static uma_slab_t
3445194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags)
3446194a979eSMark Johnston {
3447194a979eSMark Johnston 	struct vm_domainset_iter di;
3448194a979eSMark Johnston 	uma_slab_t slab;
3449194a979eSMark Johnston 	int aflags, domain;
3450194a979eSMark Johnston 	bool rr;
3451194a979eSMark Johnston 
3452194a979eSMark Johnston restart:
3453bbee39c6SJeff Roberson 	/*
3454194a979eSMark Johnston 	 * Use the keg's policy if upper layers haven't already specified a
3455194a979eSMark Johnston 	 * domain (as happens with first-touch zones).
3456194a979eSMark Johnston 	 *
3457194a979eSMark Johnston 	 * To avoid races we run the iterator with the keg lock held, but that
3458194a979eSMark Johnston 	 * means that we cannot allow the vm_domainset layer to sleep.  Thus,
3459194a979eSMark Johnston 	 * clear M_WAITOK and handle low memory conditions locally.
3460bbee39c6SJeff Roberson 	 */
3461ab3185d1SJeff Roberson 	rr = rdomain == UMA_ANYDOMAIN;
3462ab3185d1SJeff Roberson 	if (rr) {
3463194a979eSMark Johnston 		aflags = (flags & ~M_WAITOK) | M_NOWAIT;
3464194a979eSMark Johnston 		vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
3465194a979eSMark Johnston 		    &aflags);
3466194a979eSMark Johnston 	} else {
3467194a979eSMark Johnston 		aflags = flags;
3468194a979eSMark Johnston 		domain = rdomain;
3469194a979eSMark Johnston 	}
3470ab3185d1SJeff Roberson 
3471194a979eSMark Johnston 	for (;;) {
3472194a979eSMark Johnston 		slab = keg_fetch_free_slab(keg, domain, rr, flags);
3473584061b4SJeff Roberson 		if (slab != NULL)
3474bbee39c6SJeff Roberson 			return (slab);
3475bbee39c6SJeff Roberson 
3476bbee39c6SJeff Roberson 		/*
3477bbee39c6SJeff Roberson 		 * M_NOVM means don't ask at all!
3478bbee39c6SJeff Roberson 		 */
3479bbee39c6SJeff Roberson 		if (flags & M_NOVM)
3480bbee39c6SJeff Roberson 			break;
3481bbee39c6SJeff Roberson 
348286220393SMark Johnston 		slab = keg_alloc_slab(keg, zone, domain, flags, aflags);
34838b987a77SJeff Roberson 		if (slab != NULL)
3484bbee39c6SJeff Roberson 			return (slab);
34853639ac42SJeff Roberson 		if (!rr && (flags & M_WAITOK) == 0)
34863639ac42SJeff Roberson 			break;
3487194a979eSMark Johnston 		if (rr && vm_domainset_iter_policy(&di, &domain) != 0) {
3488194a979eSMark Johnston 			if ((flags & M_WAITOK) != 0) {
3489194a979eSMark Johnston 				vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
3490194a979eSMark Johnston 				goto restart;
349130c5525bSAndrew Gallatin 			}
3492194a979eSMark Johnston 			break;
3493194a979eSMark Johnston 		}
3494ab3185d1SJeff Roberson 	}
3495ab3185d1SJeff Roberson 
3496bbee39c6SJeff Roberson 	/*
3497bbee39c6SJeff Roberson 	 * We might not have been able to get a slab but another cpu
3498bbee39c6SJeff Roberson 	 * could have while we were unlocked.  Check again before we
3499bbee39c6SJeff Roberson 	 * fail.
3500bbee39c6SJeff Roberson 	 */
35018b987a77SJeff Roberson 	if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL)
3502bbee39c6SJeff Roberson 		return (slab);
35038b987a77SJeff Roberson 
3504ab3185d1SJeff Roberson 	return (NULL);
3505ab3185d1SJeff Roberson }
3506bbee39c6SJeff Roberson 
3507d56368d7SBosko Milekic static void *
35080095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
3509bbee39c6SJeff Roberson {
3510ab3185d1SJeff Roberson 	uma_domain_t dom;
3511bbee39c6SJeff Roberson 	void *item;
35129b8db4d0SRyan Libby 	int freei;
3513bbee39c6SJeff Roberson 
35148b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, slab->us_domain);
3515099a0e58SBosko Milekic 
35168b987a77SJeff Roberson 	dom = &keg->uk_domain[slab->us_domain];
35179b78b1f4SJeff Roberson 	freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1;
35189b78b1f4SJeff Roberson 	BIT_CLR(keg->uk_ipers, freei, &slab->us_free);
35191e0701e1SJeff Roberson 	item = slab_item(slab, keg, freei);
3520bbee39c6SJeff Roberson 	slab->us_freecount--;
35214ab3aee8SMark Johnston 	dom->ud_free_items--;
3522ef72505eSJeff Roberson 
35234ab3aee8SMark Johnston 	/*
35244ab3aee8SMark Johnston 	 * Move this slab to the full list.  It must be on the partial list, so
35254ab3aee8SMark Johnston 	 * we do not need to update the free slab count.  In particular,
35264ab3aee8SMark Johnston 	 * keg_fetch_slab() always returns slabs on the partial list.
35274ab3aee8SMark Johnston 	 */
3528bbee39c6SJeff Roberson 	if (slab->us_freecount == 0) {
3529bbee39c6SJeff Roberson 		LIST_REMOVE(slab, us_link);
3530ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link);
3531bbee39c6SJeff Roberson 	}
3532bbee39c6SJeff Roberson 
3533bbee39c6SJeff Roberson 	return (item);
3534bbee39c6SJeff Roberson }
3535bbee39c6SJeff Roberson 
3536bbee39c6SJeff Roberson static int
3537b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags)
35380095a784SJeff Roberson {
35398b987a77SJeff Roberson 	uma_domain_t dom;
3540b75c4efcSAndrew Turner 	uma_zone_t zone;
35410095a784SJeff Roberson 	uma_slab_t slab;
35420095a784SJeff Roberson 	uma_keg_t keg;
3543a03af342SSean Bruno #ifdef NUMA
3544ab3185d1SJeff Roberson 	int stripe;
3545a03af342SSean Bruno #endif
35460095a784SJeff Roberson 	int i;
35470095a784SJeff Roberson 
3548b75c4efcSAndrew Turner 	zone = arg;
35490095a784SJeff Roberson 	slab = NULL;
3550584061b4SJeff Roberson 	keg = zone->uz_keg;
3551af526374SJeff Roberson 	/* Try to keep the buckets totally full */
35520095a784SJeff Roberson 	for (i = 0; i < max; ) {
3553584061b4SJeff Roberson 		if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL)
35540095a784SJeff Roberson 			break;
3555a03af342SSean Bruno #ifdef NUMA
3556ab3185d1SJeff Roberson 		stripe = howmany(max, vm_ndomains);
3557a03af342SSean Bruno #endif
35588b987a77SJeff Roberson 		dom = &keg->uk_domain[slab->us_domain];
35596fd34d6fSJeff Roberson 		while (slab->us_freecount && i < max) {
35600095a784SJeff Roberson 			bucket[i++] = slab_alloc_item(keg, slab);
35614ab3aee8SMark Johnston 			if (dom->ud_free_items <= keg->uk_reserve)
35626fd34d6fSJeff Roberson 				break;
3563b6715dabSJeff Roberson #ifdef NUMA
3564ab3185d1SJeff Roberson 			/*
3565ab3185d1SJeff Roberson 			 * If the zone is striped we pick a new slab for every
3566ab3185d1SJeff Roberson 			 * N allocations.  Eliminating this conditional will
3567ab3185d1SJeff Roberson 			 * instead pick a new domain for each bucket rather
3568ab3185d1SJeff Roberson 			 * than stripe within each bucket.  The current option
3569ab3185d1SJeff Roberson 			 * produces more fragmentation and requires more cpu
3570ab3185d1SJeff Roberson 			 * time but yields better distribution.
3571ab3185d1SJeff Roberson 			 */
3572dfe13344SJeff Roberson 			if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 &&
3573ab3185d1SJeff Roberson 			    vm_ndomains > 1 && --stripe == 0)
3574ab3185d1SJeff Roberson 				break;
3575ab3185d1SJeff Roberson #endif
35766fd34d6fSJeff Roberson 		}
35778b987a77SJeff Roberson 		KEG_UNLOCK(keg, slab->us_domain);
3578ab3185d1SJeff Roberson 		/* Don't block if we allocated any successfully. */
35790095a784SJeff Roberson 		flags &= ~M_WAITOK;
35800095a784SJeff Roberson 		flags |= M_NOWAIT;
35810095a784SJeff Roberson 	}
35820095a784SJeff Roberson 
35830095a784SJeff Roberson 	return i;
35840095a784SJeff Roberson }
35850095a784SJeff Roberson 
35864bd61e19SJeff Roberson static int
35874bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags)
35884bd61e19SJeff Roberson {
35894bd61e19SJeff Roberson 	uint64_t old, new, total, max;
35904bd61e19SJeff Roberson 
35914bd61e19SJeff Roberson 	/*
35924bd61e19SJeff Roberson 	 * The hard case.  We're going to sleep because there were existing
35934bd61e19SJeff Roberson 	 * sleepers or because we ran out of items.  This routine enforces
35944bd61e19SJeff Roberson 	 * fairness by keeping fifo order.
35954bd61e19SJeff Roberson 	 *
35964bd61e19SJeff Roberson 	 * First release our ill gotten gains and make some noise.
35974bd61e19SJeff Roberson 	 */
35984bd61e19SJeff Roberson 	for (;;) {
35994bd61e19SJeff Roberson 		zone_free_limit(zone, count);
36004bd61e19SJeff Roberson 		zone_log_warning(zone);
36014bd61e19SJeff Roberson 		zone_maxaction(zone);
36024bd61e19SJeff Roberson 		if (flags & M_NOWAIT)
36034bd61e19SJeff Roberson 			return (0);
36044bd61e19SJeff Roberson 
36054bd61e19SJeff Roberson 		/*
36064bd61e19SJeff Roberson 		 * We need to allocate an item or set ourself as a sleeper
36074bd61e19SJeff Roberson 		 * while the sleepq lock is held to avoid wakeup races.  This
36084bd61e19SJeff Roberson 		 * is essentially a home rolled semaphore.
36094bd61e19SJeff Roberson 		 */
36104bd61e19SJeff Roberson 		sleepq_lock(&zone->uz_max_items);
36114bd61e19SJeff Roberson 		old = zone->uz_items;
36124bd61e19SJeff Roberson 		do {
36134bd61e19SJeff Roberson 			MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX);
36144bd61e19SJeff Roberson 			/* Cache the max since we will evaluate twice. */
36154bd61e19SJeff Roberson 			max = zone->uz_max_items;
36164bd61e19SJeff Roberson 			if (UZ_ITEMS_SLEEPERS(old) != 0 ||
36174bd61e19SJeff Roberson 			    UZ_ITEMS_COUNT(old) >= max)
36184bd61e19SJeff Roberson 				new = old + UZ_ITEMS_SLEEPER;
36194bd61e19SJeff Roberson 			else
36204bd61e19SJeff Roberson 				new = old + MIN(count, max - old);
36214bd61e19SJeff Roberson 		} while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0);
36224bd61e19SJeff Roberson 
36234bd61e19SJeff Roberson 		/* We may have successfully allocated under the sleepq lock. */
36244bd61e19SJeff Roberson 		if (UZ_ITEMS_SLEEPERS(new) == 0) {
36254bd61e19SJeff Roberson 			sleepq_release(&zone->uz_max_items);
36264bd61e19SJeff Roberson 			return (new - old);
36274bd61e19SJeff Roberson 		}
36284bd61e19SJeff Roberson 
36294bd61e19SJeff Roberson 		/*
36304bd61e19SJeff Roberson 		 * This is in a different cacheline from uz_items so that we
36314bd61e19SJeff Roberson 		 * don't constantly invalidate the fastpath cacheline when we
36324bd61e19SJeff Roberson 		 * adjust item counts.  This could be limited to toggling on
36334bd61e19SJeff Roberson 		 * transitions.
36344bd61e19SJeff Roberson 		 */
36354bd61e19SJeff Roberson 		atomic_add_32(&zone->uz_sleepers, 1);
36364bd61e19SJeff Roberson 		atomic_add_64(&zone->uz_sleeps, 1);
36374bd61e19SJeff Roberson 
36384bd61e19SJeff Roberson 		/*
36394bd61e19SJeff Roberson 		 * We have added ourselves as a sleeper.  The sleepq lock
36404bd61e19SJeff Roberson 		 * protects us from wakeup races.  Sleep now and then retry.
36414bd61e19SJeff Roberson 		 */
36424bd61e19SJeff Roberson 		sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0);
36434bd61e19SJeff Roberson 		sleepq_wait(&zone->uz_max_items, PVM);
36444bd61e19SJeff Roberson 
36454bd61e19SJeff Roberson 		/*
36464bd61e19SJeff Roberson 		 * After wakeup, remove ourselves as a sleeper and try
36474bd61e19SJeff Roberson 		 * again.  We no longer have the sleepq lock for protection.
36484bd61e19SJeff Roberson 		 *
36494bd61e19SJeff Roberson 		 * Subract ourselves as a sleeper while attempting to add
36504bd61e19SJeff Roberson 		 * our count.
36514bd61e19SJeff Roberson 		 */
36524bd61e19SJeff Roberson 		atomic_subtract_32(&zone->uz_sleepers, 1);
36534bd61e19SJeff Roberson 		old = atomic_fetchadd_64(&zone->uz_items,
36544bd61e19SJeff Roberson 		    -(UZ_ITEMS_SLEEPER - count));
36554bd61e19SJeff Roberson 		/* We're no longer a sleeper. */
36564bd61e19SJeff Roberson 		old -= UZ_ITEMS_SLEEPER;
36574bd61e19SJeff Roberson 
36584bd61e19SJeff Roberson 		/*
36594bd61e19SJeff Roberson 		 * If we're still at the limit, restart.  Notably do not
36604bd61e19SJeff Roberson 		 * block on other sleepers.  Cache the max value to protect
36614bd61e19SJeff Roberson 		 * against changes via sysctl.
36624bd61e19SJeff Roberson 		 */
36634bd61e19SJeff Roberson 		total = UZ_ITEMS_COUNT(old);
36644bd61e19SJeff Roberson 		max = zone->uz_max_items;
36654bd61e19SJeff Roberson 		if (total >= max)
36664bd61e19SJeff Roberson 			continue;
36674bd61e19SJeff Roberson 		/* Truncate if necessary, otherwise wake other sleepers. */
36684bd61e19SJeff Roberson 		if (total + count > max) {
36694bd61e19SJeff Roberson 			zone_free_limit(zone, total + count - max);
36704bd61e19SJeff Roberson 			count = max - total;
36714bd61e19SJeff Roberson 		} else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0)
36724bd61e19SJeff Roberson 			wakeup_one(&zone->uz_max_items);
36734bd61e19SJeff Roberson 
36744bd61e19SJeff Roberson 		return (count);
36754bd61e19SJeff Roberson 	}
36764bd61e19SJeff Roberson }
36774bd61e19SJeff Roberson 
36784bd61e19SJeff Roberson /*
36794bd61e19SJeff Roberson  * Allocate 'count' items from our max_items limit.  Returns the number
36804bd61e19SJeff Roberson  * available.  If M_NOWAIT is not specified it will sleep until at least
36814bd61e19SJeff Roberson  * one item can be allocated.
36824bd61e19SJeff Roberson  */
36834bd61e19SJeff Roberson static int
36844bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags)
36854bd61e19SJeff Roberson {
36864bd61e19SJeff Roberson 	uint64_t old;
36874bd61e19SJeff Roberson 	uint64_t max;
36884bd61e19SJeff Roberson 
36894bd61e19SJeff Roberson 	max = zone->uz_max_items;
36904bd61e19SJeff Roberson 	MPASS(max > 0);
36914bd61e19SJeff Roberson 
36924bd61e19SJeff Roberson 	/*
36934bd61e19SJeff Roberson 	 * We expect normal allocations to succeed with a simple
36944bd61e19SJeff Roberson 	 * fetchadd.
36954bd61e19SJeff Roberson 	 */
36964bd61e19SJeff Roberson 	old = atomic_fetchadd_64(&zone->uz_items, count);
36974bd61e19SJeff Roberson 	if (__predict_true(old + count <= max))
36984bd61e19SJeff Roberson 		return (count);
36994bd61e19SJeff Roberson 
37004bd61e19SJeff Roberson 	/*
37014bd61e19SJeff Roberson 	 * If we had some items and no sleepers just return the
37024bd61e19SJeff Roberson 	 * truncated value.  We have to release the excess space
37034bd61e19SJeff Roberson 	 * though because that may wake sleepers who weren't woken
37044bd61e19SJeff Roberson 	 * because we were temporarily over the limit.
37054bd61e19SJeff Roberson 	 */
37064bd61e19SJeff Roberson 	if (old < max) {
37074bd61e19SJeff Roberson 		zone_free_limit(zone, (old + count) - max);
37084bd61e19SJeff Roberson 		return (max - old);
37094bd61e19SJeff Roberson 	}
37104bd61e19SJeff Roberson 	return (zone_alloc_limit_hard(zone, count, flags));
37114bd61e19SJeff Roberson }
37124bd61e19SJeff Roberson 
37134bd61e19SJeff Roberson /*
37144bd61e19SJeff Roberson  * Free a number of items back to the limit.
37154bd61e19SJeff Roberson  */
37164bd61e19SJeff Roberson static void
37174bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count)
37184bd61e19SJeff Roberson {
37194bd61e19SJeff Roberson 	uint64_t old;
37204bd61e19SJeff Roberson 
37214bd61e19SJeff Roberson 	MPASS(count > 0);
37224bd61e19SJeff Roberson 
37234bd61e19SJeff Roberson 	/*
37244bd61e19SJeff Roberson 	 * In the common case we either have no sleepers or
37254bd61e19SJeff Roberson 	 * are still over the limit and can just return.
37264bd61e19SJeff Roberson 	 */
37274bd61e19SJeff Roberson 	old = atomic_fetchadd_64(&zone->uz_items, -count);
37284bd61e19SJeff Roberson 	if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 ||
37294bd61e19SJeff Roberson 	   UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items))
37304bd61e19SJeff Roberson 		return;
37314bd61e19SJeff Roberson 
37324bd61e19SJeff Roberson 	/*
37334bd61e19SJeff Roberson 	 * Moderate the rate of wakeups.  Sleepers will continue
37344bd61e19SJeff Roberson 	 * to generate wakeups if necessary.
37354bd61e19SJeff Roberson 	 */
37364bd61e19SJeff Roberson 	wakeup_one(&zone->uz_max_items);
37374bd61e19SJeff Roberson }
37384bd61e19SJeff Roberson 
3739fc03d22bSJeff Roberson static uma_bucket_t
3740beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags)
3741bbee39c6SJeff Roberson {
3742bbee39c6SJeff Roberson 	uma_bucket_t bucket;
3743beb8beefSJeff Roberson 	int maxbucket, cnt;
3744bbee39c6SJeff Roberson 
3745e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name,
3746e63a1c2fSRyan Libby 	    zone, domain);
374730c5525bSAndrew Gallatin 
3748c1685086SJeff Roberson 	/* Avoid allocs targeting empty domains. */
3749c1685086SJeff Roberson 	if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
3750c1685086SJeff Roberson 		domain = UMA_ANYDOMAIN;
3751c1685086SJeff Roberson 
37524bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
37534bd61e19SJeff Roberson 		maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size,
37544bd61e19SJeff Roberson 		    M_NOWAIT);
37554bd61e19SJeff Roberson 	else
375620a4e154SJeff Roberson 		maxbucket = zone->uz_bucket_size;
37574bd61e19SJeff Roberson 	if (maxbucket == 0)
37584bd61e19SJeff Roberson 		return (false);
3759beb8beefSJeff Roberson 
37606fd34d6fSJeff Roberson 	/* Don't wait for buckets, preserve caller's NOVM setting. */
37616fd34d6fSJeff Roberson 	bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
3762beb8beefSJeff Roberson 	if (bucket == NULL) {
3763beb8beefSJeff Roberson 		cnt = 0;
3764beb8beefSJeff Roberson 		goto out;
3765beb8beefSJeff Roberson 	}
37660095a784SJeff Roberson 
37670095a784SJeff Roberson 	bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
3768beb8beefSJeff Roberson 	    MIN(maxbucket, bucket->ub_entries), domain, flags);
37690095a784SJeff Roberson 
37700095a784SJeff Roberson 	/*
37710095a784SJeff Roberson 	 * Initialize the memory if necessary.
37720095a784SJeff Roberson 	 */
37730095a784SJeff Roberson 	if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
3774099a0e58SBosko Milekic 		int i;
3775bbee39c6SJeff Roberson 
37760095a784SJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
3777e20a199fSJeff Roberson 			if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
37780095a784SJeff Roberson 			    flags) != 0)
3779b23f72e9SBrian Feldman 				break;
3780b23f72e9SBrian Feldman 		/*
3781b23f72e9SBrian Feldman 		 * If we couldn't initialize the whole bucket, put the
3782b23f72e9SBrian Feldman 		 * rest back onto the freelist.
3783b23f72e9SBrian Feldman 		 */
3784b23f72e9SBrian Feldman 		if (i != bucket->ub_cnt) {
3785af526374SJeff Roberson 			zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
37860095a784SJeff Roberson 			    bucket->ub_cnt - i);
3787a5a262c6SBosko Milekic #ifdef INVARIANTS
37880095a784SJeff Roberson 			bzero(&bucket->ub_bucket[i],
37890095a784SJeff Roberson 			    sizeof(void *) * (bucket->ub_cnt - i));
3790a5a262c6SBosko Milekic #endif
3791b23f72e9SBrian Feldman 			bucket->ub_cnt = i;
3792b23f72e9SBrian Feldman 		}
3793099a0e58SBosko Milekic 	}
3794099a0e58SBosko Milekic 
3795beb8beefSJeff Roberson 	cnt = bucket->ub_cnt;
3796f7104ccdSAlexander Motin 	if (bucket->ub_cnt == 0) {
37976fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
37982efcc8cbSGleb Smirnoff 		counter_u64_add(zone->uz_fails, 1);
3799beb8beefSJeff Roberson 		bucket = NULL;
3800beb8beefSJeff Roberson 	}
3801beb8beefSJeff Roberson out:
38024bd61e19SJeff Roberson 	if (zone->uz_max_items > 0 && cnt < maxbucket)
38034bd61e19SJeff Roberson 		zone_free_limit(zone, maxbucket - cnt);
3804fc03d22bSJeff Roberson 
3805fc03d22bSJeff Roberson 	return (bucket);
3806fc03d22bSJeff Roberson }
3807fc03d22bSJeff Roberson 
38088355f576SJeff Roberson /*
38090095a784SJeff Roberson  * Allocates a single item from a zone.
38108355f576SJeff Roberson  *
38118355f576SJeff Roberson  * Arguments
38128355f576SJeff Roberson  *	zone   The zone to alloc for.
38138355f576SJeff Roberson  *	udata  The data to be passed to the constructor.
3814ab3185d1SJeff Roberson  *	domain The domain to allocate from or UMA_ANYDOMAIN.
3815a163d034SWarner Losh  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
38168355f576SJeff Roberson  *
38178355f576SJeff Roberson  * Returns
38188355f576SJeff Roberson  *	NULL if there is no memory and M_NOWAIT is set
3819bbee39c6SJeff Roberson  *	An item if successful
38208355f576SJeff Roberson  */
38218355f576SJeff Roberson 
38228355f576SJeff Roberson static void *
3823ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags)
38248355f576SJeff Roberson {
38258355f576SJeff Roberson 	void *item;
38268355f576SJeff Roberson 
38274bd61e19SJeff Roberson 	if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0)
3828bb15d1c7SGleb Smirnoff 		return (NULL);
38298355f576SJeff Roberson 
3830c1685086SJeff Roberson 	/* Avoid allocs targeting empty domains. */
3831c1685086SJeff Roberson 	if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
383230c5525bSAndrew Gallatin 		domain = UMA_ANYDOMAIN;
3833c1685086SJeff Roberson 
3834ab3185d1SJeff Roberson 	if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1)
3835beb8beefSJeff Roberson 		goto fail_cnt;
38368355f576SJeff Roberson 
3837099a0e58SBosko Milekic 	/*
3838099a0e58SBosko Milekic 	 * We have to call both the zone's init (not the keg's init)
3839099a0e58SBosko Milekic 	 * and the zone's ctor.  This is because the item is going from
3840099a0e58SBosko Milekic 	 * a keg slab directly to the user, and the user is expecting it
3841099a0e58SBosko Milekic 	 * to be both zone-init'd as well as zone-ctor'd.
3842099a0e58SBosko Milekic 	 */
3843b23f72e9SBrian Feldman 	if (zone->uz_init != NULL) {
3844e20a199fSJeff Roberson 		if (zone->uz_init(item, zone->uz_size, flags) != 0) {
3845bb15d1c7SGleb Smirnoff 			zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT);
3846beb8beefSJeff Roberson 			goto fail_cnt;
3847beb8beefSJeff Roberson 		}
3848beb8beefSJeff Roberson 	}
3849d4665eaaSJeff Roberson 	item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags,
3850d4665eaaSJeff Roberson 	    item);
3851beb8beefSJeff Roberson 	if (item == NULL)
38520095a784SJeff Roberson 		goto fail;
38538355f576SJeff Roberson 
38542efcc8cbSGleb Smirnoff 	counter_u64_add(zone->uz_allocs, 1);
38551431a748SGleb Smirnoff 	CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
38561431a748SGleb Smirnoff 	    zone->uz_name, zone);
38571431a748SGleb Smirnoff 
38588355f576SJeff Roberson 	return (item);
38590095a784SJeff Roberson 
3860beb8beefSJeff Roberson fail_cnt:
3861beb8beefSJeff Roberson 	counter_u64_add(zone->uz_fails, 1);
38620095a784SJeff Roberson fail:
38634bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
38644bd61e19SJeff Roberson 		zone_free_limit(zone, 1);
38651431a748SGleb Smirnoff 	CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
38661431a748SGleb Smirnoff 	    zone->uz_name, zone);
38674bd61e19SJeff Roberson 
38680095a784SJeff Roberson 	return (NULL);
38698355f576SJeff Roberson }
38708355f576SJeff Roberson 
38718355f576SJeff Roberson /* See uma.h */
38728355f576SJeff Roberson void
3873d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item)
3874d4665eaaSJeff Roberson {
3875d4665eaaSJeff Roberson 	uma_cache_t cache;
3876d4665eaaSJeff Roberson 	uma_cache_bucket_t bucket;
3877d4665eaaSJeff Roberson 	int domain, itemdomain, uz_flags;
3878d4665eaaSJeff Roberson 
3879d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
3880d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
3881d4665eaaSJeff Roberson 	    ("uma_zfree_smr: called with non-SMR zone.\n"));
3882d4665eaaSJeff Roberson 	KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer."));
3883d4665eaaSJeff Roberson 	if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN)
3884d4665eaaSJeff Roberson 		return;
3885d4665eaaSJeff Roberson #endif
3886d4665eaaSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3887d4665eaaSJeff Roberson 	uz_flags = cache_uz_flags(cache);
3888d4665eaaSJeff Roberson 	domain = itemdomain = 0;
3889d4665eaaSJeff Roberson #ifdef NUMA
3890d4665eaaSJeff Roberson 	if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
3891d4665eaaSJeff Roberson 		itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item));
3892d4665eaaSJeff Roberson #endif
3893d4665eaaSJeff Roberson 	critical_enter();
3894d4665eaaSJeff Roberson 	do {
3895d4665eaaSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
3896d4665eaaSJeff Roberson 		/* SMR Zones must free to the free bucket. */
3897d4665eaaSJeff Roberson 		bucket = &cache->uc_freebucket;
3898d4665eaaSJeff Roberson #ifdef NUMA
3899d4665eaaSJeff Roberson 		domain = PCPU_GET(domain);
3900d4665eaaSJeff Roberson 		if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
3901d4665eaaSJeff Roberson 		    domain != itemdomain) {
3902d4665eaaSJeff Roberson 			bucket = &cache->uc_crossbucket;
3903d4665eaaSJeff Roberson 		}
3904d4665eaaSJeff Roberson #endif
3905d4665eaaSJeff Roberson 		if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
3906d4665eaaSJeff Roberson 			cache_bucket_push(cache, bucket, item);
3907d4665eaaSJeff Roberson 			critical_exit();
3908d4665eaaSJeff Roberson 			return;
3909d4665eaaSJeff Roberson 		}
3910d4665eaaSJeff Roberson 	} while (cache_free(zone, cache, NULL, item, itemdomain));
3911d4665eaaSJeff Roberson 	critical_exit();
3912d4665eaaSJeff Roberson 
3913d4665eaaSJeff Roberson 	/*
3914d4665eaaSJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
3915d4665eaaSJeff Roberson 	 */
3916d4665eaaSJeff Roberson 	zone_free_item(zone, item, NULL, SKIP_NONE);
3917d4665eaaSJeff Roberson }
3918d4665eaaSJeff Roberson 
3919d4665eaaSJeff Roberson /* See uma.h */
3920d4665eaaSJeff Roberson void
39218355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
39228355f576SJeff Roberson {
39238355f576SJeff Roberson 	uma_cache_t cache;
3924376b1ba3SJeff Roberson 	uma_cache_bucket_t bucket;
3925cc7ce83aSJeff Roberson 	int domain, itemdomain, uz_flags;
39268355f576SJeff Roberson 
3927e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
392819fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
392910cb2424SMark Murray 
3930e63a1c2fSRyan Libby 	CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone);
39313659f747SRobert Watson 
3932d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
3933d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
3934d4665eaaSJeff Roberson 	    ("uma_zfree_arg: called with SMR zone.\n"));
3935d4665eaaSJeff Roberson 	if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN)
3936d4665eaaSJeff Roberson 		return;
3937d4665eaaSJeff Roberson #endif
393820ed0cb0SMatthew D Fleming         /* uma_zfree(..., NULL) does nothing, to match free(9). */
393920ed0cb0SMatthew D Fleming         if (item == NULL)
394020ed0cb0SMatthew D Fleming                 return;
3941cc7ce83aSJeff Roberson 
3942cc7ce83aSJeff Roberson 	/*
3943cc7ce83aSJeff Roberson 	 * We are accessing the per-cpu cache without a critical section to
3944cc7ce83aSJeff Roberson 	 * fetch size and flags.  This is acceptable, if we are preempted we
3945cc7ce83aSJeff Roberson 	 * will simply read another cpu's line.
3946cc7ce83aSJeff Roberson 	 */
3947cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3948cc7ce83aSJeff Roberson 	uz_flags = cache_uz_flags(cache);
3949d4665eaaSJeff Roberson 	if (UMA_ALWAYS_CTORDTOR ||
3950d4665eaaSJeff Roberson 	    __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0))
3951cc7ce83aSJeff Roberson 		item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE);
3952ef72505eSJeff Roberson 
3953af7f9b97SJeff Roberson 	/*
3954af7f9b97SJeff Roberson 	 * The race here is acceptable.  If we miss it we'll just have to wait
3955af7f9b97SJeff Roberson 	 * a little longer for the limits to be reset.
3956af7f9b97SJeff Roberson 	 */
3957cc7ce83aSJeff Roberson 	if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) {
3958bb15d1c7SGleb Smirnoff 		if (zone->uz_sleepers > 0)
3959fc03d22bSJeff Roberson 			goto zfree_item;
3960cc7ce83aSJeff Roberson 	}
3961af7f9b97SJeff Roberson 
39625d1ae027SRobert Watson 	/*
39635d1ae027SRobert Watson 	 * If possible, free to the per-CPU cache.  There are two
39645d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
39655d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
39665d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
39675d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
39685d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
39695d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to free to the
39705d1ae027SRobert Watson 	 * current cache; when we re-acquire the critical section, we must
39715d1ae027SRobert Watson 	 * detect and handle migration if it has occurred.
39725d1ae027SRobert Watson 	 */
39730a81b439SJeff Roberson 	domain = itemdomain = 0;
3974dfe13344SJeff Roberson #ifdef NUMA
3975dfe13344SJeff Roberson 	if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
3976dfe13344SJeff Roberson 		itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item));
3977dfe13344SJeff Roberson #endif
39785d1ae027SRobert Watson 	critical_enter();
39790a81b439SJeff Roberson 	do {
3980cc7ce83aSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
3981a553d4b8SJeff Roberson 		/*
3982dfe13344SJeff Roberson 		 * Try to free into the allocbucket first to give LIFO
3983dfe13344SJeff Roberson 		 * ordering for cache-hot datastructures.  Spill over
3984dfe13344SJeff Roberson 		 * into the freebucket if necessary.  Alloc will swap
3985dfe13344SJeff Roberson 		 * them if one runs dry.
3986a553d4b8SJeff Roberson 		 */
3987dfe13344SJeff Roberson 		bucket = &cache->uc_allocbucket;
3988d4665eaaSJeff Roberson #ifdef NUMA
3989d4665eaaSJeff Roberson 		domain = PCPU_GET(domain);
3990d4665eaaSJeff Roberson 		if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
3991d4665eaaSJeff Roberson 		    domain != itemdomain) {
3992d4665eaaSJeff Roberson 			bucket = &cache->uc_crossbucket;
3993d4665eaaSJeff Roberson 		} else
3994d4665eaaSJeff Roberson #endif
3995d4665eaaSJeff Roberson 		if (bucket->ucb_cnt >= bucket->ucb_entries)
3996376b1ba3SJeff Roberson 			bucket = &cache->uc_freebucket;
3997376b1ba3SJeff Roberson 		if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
3998376b1ba3SJeff Roberson 			cache_bucket_push(cache, bucket, item);
39995d1ae027SRobert Watson 			critical_exit();
40008355f576SJeff Roberson 			return;
4001fc03d22bSJeff Roberson 		}
40020a81b439SJeff Roberson 	} while (cache_free(zone, cache, udata, item, itemdomain));
40030a81b439SJeff Roberson 	critical_exit();
4004fc03d22bSJeff Roberson 
40058355f576SJeff Roberson 	/*
40060a81b439SJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
40078355f576SJeff Roberson 	 */
40080a81b439SJeff Roberson zfree_item:
40090a81b439SJeff Roberson 	zone_free_item(zone, item, udata, SKIP_DTOR);
40100a81b439SJeff Roberson }
4011fc03d22bSJeff Roberson 
4012dfe13344SJeff Roberson #ifdef NUMA
401391d947bfSJeff Roberson /*
401491d947bfSJeff Roberson  * sort crossdomain free buckets to domain correct buckets and cache
401591d947bfSJeff Roberson  * them.
401691d947bfSJeff Roberson  */
401791d947bfSJeff Roberson static void
401891d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata)
401991d947bfSJeff Roberson {
402091d947bfSJeff Roberson 	struct uma_bucketlist fullbuckets;
402191d947bfSJeff Roberson 	uma_zone_domain_t zdom;
402291d947bfSJeff Roberson 	uma_bucket_t b;
4023543117beSJeff Roberson 	smr_seq_t seq;
402491d947bfSJeff Roberson 	void *item;
402591d947bfSJeff Roberson 	int domain;
402691d947bfSJeff Roberson 
402791d947bfSJeff Roberson 	CTR3(KTR_UMA,
402891d947bfSJeff Roberson 	    "uma_zfree: zone %s(%p) draining cross bucket %p",
402991d947bfSJeff Roberson 	    zone->uz_name, zone, bucket);
403091d947bfSJeff Roberson 
4031dc3915c8SJeff Roberson 	STAILQ_INIT(&fullbuckets);
403291d947bfSJeff Roberson 
403391d947bfSJeff Roberson 	/*
403491d947bfSJeff Roberson 	 * To avoid having ndomain * ndomain buckets for sorting we have a
403591d947bfSJeff Roberson 	 * lock on the current crossfree bucket.  A full matrix with
403691d947bfSJeff Roberson 	 * per-domain locking could be used if necessary.
403791d947bfSJeff Roberson 	 */
403891d947bfSJeff Roberson 	ZONE_CROSS_LOCK(zone);
4039543117beSJeff Roberson 
4040543117beSJeff Roberson 	/*
4041543117beSJeff Roberson 	 * It is possible for buckets to arrive here out of order so we fetch
4042543117beSJeff Roberson 	 * the current smr seq rather than accepting the bucket's.
4043543117beSJeff Roberson 	 */
4044543117beSJeff Roberson 	seq = SMR_SEQ_INVALID;
4045543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
4046543117beSJeff Roberson 		seq = smr_current(zone->uz_smr);
404791d947bfSJeff Roberson 	while (bucket->ub_cnt > 0) {
404891d947bfSJeff Roberson 		item = bucket->ub_bucket[bucket->ub_cnt - 1];
404991d947bfSJeff Roberson 		domain = _vm_phys_domain(pmap_kextract((vm_offset_t)item));
405091d947bfSJeff Roberson 		zdom = &zone->uz_domain[domain];
405191d947bfSJeff Roberson 		if (zdom->uzd_cross == NULL) {
405291d947bfSJeff Roberson 			zdom->uzd_cross = bucket_alloc(zone, udata, M_NOWAIT);
405391d947bfSJeff Roberson 			if (zdom->uzd_cross == NULL)
405491d947bfSJeff Roberson 				break;
405591d947bfSJeff Roberson 		}
4056543117beSJeff Roberson 		b = zdom->uzd_cross;
4057543117beSJeff Roberson 		b->ub_bucket[b->ub_cnt++] = item;
4058543117beSJeff Roberson 		b->ub_seq = seq;
4059543117beSJeff Roberson 		if (b->ub_cnt == b->ub_entries) {
4060543117beSJeff Roberson 			STAILQ_INSERT_HEAD(&fullbuckets, b, ub_link);
406191d947bfSJeff Roberson 			zdom->uzd_cross = NULL;
406291d947bfSJeff Roberson 		}
406391d947bfSJeff Roberson 		bucket->ub_cnt--;
406491d947bfSJeff Roberson 	}
406591d947bfSJeff Roberson 	ZONE_CROSS_UNLOCK(zone);
4066dc3915c8SJeff Roberson 	if (!STAILQ_EMPTY(&fullbuckets)) {
406791d947bfSJeff Roberson 		ZONE_LOCK(zone);
4068dc3915c8SJeff Roberson 		while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) {
4069dc3915c8SJeff Roberson 			STAILQ_REMOVE_HEAD(&fullbuckets, ub_link);
407091d947bfSJeff Roberson 			if (zone->uz_bkt_count >= zone->uz_bkt_max) {
407191d947bfSJeff Roberson 				ZONE_UNLOCK(zone);
407291d947bfSJeff Roberson 				bucket_drain(zone, b);
407391d947bfSJeff Roberson 				bucket_free(zone, b, udata);
407491d947bfSJeff Roberson 				ZONE_LOCK(zone);
407591d947bfSJeff Roberson 			} else {
407691d947bfSJeff Roberson 				domain = _vm_phys_domain(
407791d947bfSJeff Roberson 				    pmap_kextract(
407891d947bfSJeff Roberson 				    (vm_offset_t)b->ub_bucket[0]));
407991d947bfSJeff Roberson 				zdom = &zone->uz_domain[domain];
408091d947bfSJeff Roberson 				zone_put_bucket(zone, zdom, b, true);
408191d947bfSJeff Roberson 			}
408291d947bfSJeff Roberson 		}
408391d947bfSJeff Roberson 		ZONE_UNLOCK(zone);
408491d947bfSJeff Roberson 	}
408591d947bfSJeff Roberson 	if (bucket->ub_cnt != 0)
408691d947bfSJeff Roberson 		bucket_drain(zone, bucket);
4087d4665eaaSJeff Roberson 	bucket->ub_seq = SMR_SEQ_INVALID;
408891d947bfSJeff Roberson 	bucket_free(zone, bucket, udata);
408991d947bfSJeff Roberson }
409091d947bfSJeff Roberson #endif
409191d947bfSJeff Roberson 
40920a81b439SJeff Roberson static void
40930a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata,
40940a81b439SJeff Roberson     int domain, int itemdomain)
40950a81b439SJeff Roberson {
40960a81b439SJeff Roberson 	uma_zone_domain_t zdom;
40970a81b439SJeff Roberson 
4098dfe13344SJeff Roberson #ifdef NUMA
40990a81b439SJeff Roberson 	/*
41000a81b439SJeff Roberson 	 * Buckets coming from the wrong domain will be entirely for the
41010a81b439SJeff Roberson 	 * only other domain on two domain systems.  In this case we can
41020a81b439SJeff Roberson 	 * simply cache them.  Otherwise we need to sort them back to
410391d947bfSJeff Roberson 	 * correct domains.
41040a81b439SJeff Roberson 	 */
41050a81b439SJeff Roberson 	if (domain != itemdomain && vm_ndomains > 2) {
410691d947bfSJeff Roberson 		zone_free_cross(zone, bucket, udata);
41070a81b439SJeff Roberson 		return;
41080a81b439SJeff Roberson 	}
41090a81b439SJeff Roberson #endif
411091d947bfSJeff Roberson 
41110a81b439SJeff Roberson 	/*
41120a81b439SJeff Roberson 	 * Attempt to save the bucket in the zone's domain bucket cache.
41130a81b439SJeff Roberson 	 *
41140a81b439SJeff Roberson 	 * We bump the uz count when the cache size is insufficient to
41150a81b439SJeff Roberson 	 * handle the working set.
41160a81b439SJeff Roberson 	 */
41174d104ba0SAlexander Motin 	if (ZONE_TRYLOCK(zone) == 0) {
41184d104ba0SAlexander Motin 		/* Record contention to size the buckets. */
41198355f576SJeff Roberson 		ZONE_LOCK(zone);
412020a4e154SJeff Roberson 		if (zone->uz_bucket_size < zone->uz_bucket_size_max)
412120a4e154SJeff Roberson 			zone->uz_bucket_size++;
41224d104ba0SAlexander Motin 	}
41238355f576SJeff Roberson 
41240a81b439SJeff Roberson 	CTR3(KTR_UMA,
41250a81b439SJeff Roberson 	    "uma_zfree: zone %s(%p) putting bucket %p on free list",
41260a81b439SJeff Roberson 	    zone->uz_name, zone, bucket);
41270a81b439SJeff Roberson 	/* ub_cnt is pointing to the last free item */
41280a81b439SJeff Roberson 	KASSERT(bucket->ub_cnt == bucket->ub_entries,
41290a81b439SJeff Roberson 	    ("uma_zfree: Attempting to insert partial  bucket onto the full list.\n"));
41300a81b439SJeff Roberson 	if (zone->uz_bkt_count >= zone->uz_bkt_max) {
4131c1685086SJeff Roberson 		ZONE_UNLOCK(zone);
4132c1685086SJeff Roberson 		bucket_drain(zone, bucket);
4133c1685086SJeff Roberson 		bucket_free(zone, bucket, udata);
4134c1685086SJeff Roberson 	} else {
4135c1685086SJeff Roberson 		zdom = &zone->uz_domain[itemdomain];
4136c1685086SJeff Roberson 		zone_put_bucket(zone, zdom, bucket, true);
4137c1685086SJeff Roberson 		ZONE_UNLOCK(zone);
4138c1685086SJeff Roberson 	}
41398355f576SJeff Roberson }
4140fc03d22bSJeff Roberson 
41414d104ba0SAlexander Motin /*
41420a81b439SJeff Roberson  * Populate a free or cross bucket for the current cpu cache.  Free any
41430a81b439SJeff Roberson  * existing full bucket either to the zone cache or back to the slab layer.
41440a81b439SJeff Roberson  *
41450a81b439SJeff Roberson  * Enters and returns in a critical section.  false return indicates that
41460a81b439SJeff Roberson  * we can not satisfy this free in the cache layer.  true indicates that
41470a81b439SJeff Roberson  * the caller should retry.
41484d104ba0SAlexander Motin  */
41490a81b439SJeff Roberson static __noinline bool
41500a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item,
41510a81b439SJeff Roberson     int itemdomain)
41520a81b439SJeff Roberson {
4153dfe13344SJeff Roberson 	uma_cache_bucket_t cbucket;
4154d4665eaaSJeff Roberson 	uma_bucket_t newbucket, bucket;
4155cc7ce83aSJeff Roberson 	int domain;
41560a81b439SJeff Roberson 
41570a81b439SJeff Roberson 	CRITICAL_ASSERT(curthread);
41580a81b439SJeff Roberson 
4159d4665eaaSJeff Roberson 	if (zone->uz_bucket_size == 0)
41600a81b439SJeff Roberson 		return false;
41610a81b439SJeff Roberson 
4162cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4163d4665eaaSJeff Roberson 	newbucket = NULL;
41640a81b439SJeff Roberson 
41650a81b439SJeff Roberson 	/*
4166dfe13344SJeff Roberson 	 * FIRSTTOUCH domains need to free to the correct zdom.  When
4167dfe13344SJeff Roberson 	 * enabled this is the zdom of the item.   The bucket is the
4168dfe13344SJeff Roberson 	 * cross bucket if the current domain and itemdomain do not match.
41690a81b439SJeff Roberson 	 */
4170dfe13344SJeff Roberson 	cbucket = &cache->uc_freebucket;
4171dfe13344SJeff Roberson #ifdef NUMA
4172dfe13344SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) {
41730a81b439SJeff Roberson 		domain = PCPU_GET(domain);
41740a81b439SJeff Roberson 		if (domain != itemdomain) {
4175dfe13344SJeff Roberson 			cbucket = &cache->uc_crossbucket;
4176dfe13344SJeff Roberson 			if (cbucket->ucb_cnt != 0)
4177dfe13344SJeff Roberson 				atomic_add_64(&zone->uz_xdomain,
4178dfe13344SJeff Roberson 				    cbucket->ucb_cnt);
4179dfe13344SJeff Roberson 		}
41800a81b439SJeff Roberson 	} else
41810a81b439SJeff Roberson #endif
4182dfe13344SJeff Roberson 		itemdomain = domain = 0;
4183dfe13344SJeff Roberson 	bucket = cache_bucket_unload(cbucket);
41840a81b439SJeff Roberson 
41850a81b439SJeff Roberson 	/* We are no longer associated with this CPU. */
41860a81b439SJeff Roberson 	critical_exit();
41870a81b439SJeff Roberson 
4188d4665eaaSJeff Roberson 	/*
4189d4665eaaSJeff Roberson 	 * Don't let SMR zones operate without a free bucket.  Force
4190d4665eaaSJeff Roberson 	 * a synchronize and re-use this one.  We will only degrade
4191d4665eaaSJeff Roberson 	 * to a synchronize every bucket_size items rather than every
4192d4665eaaSJeff Roberson 	 * item if we fail to allocate a bucket.
4193d4665eaaSJeff Roberson 	 */
4194d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0) {
4195d4665eaaSJeff Roberson 		if (bucket != NULL)
4196d4665eaaSJeff Roberson 			bucket->ub_seq = smr_advance(zone->uz_smr);
4197d4665eaaSJeff Roberson 		newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4198d4665eaaSJeff Roberson 		if (newbucket == NULL && bucket != NULL) {
4199d4665eaaSJeff Roberson 			bucket_drain(zone, bucket);
4200d4665eaaSJeff Roberson 			newbucket = bucket;
4201d4665eaaSJeff Roberson 			bucket = NULL;
4202d4665eaaSJeff Roberson 		}
4203d4665eaaSJeff Roberson 	} else if (!bucketdisable)
4204d4665eaaSJeff Roberson 		newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4205d4665eaaSJeff Roberson 
42060a81b439SJeff Roberson 	if (bucket != NULL)
42070a81b439SJeff Roberson 		zone_free_bucket(zone, bucket, udata, domain, itemdomain);
4208a553d4b8SJeff Roberson 
4209fc03d22bSJeff Roberson 	critical_enter();
4210d4665eaaSJeff Roberson 	if ((bucket = newbucket) == NULL)
42110a81b439SJeff Roberson 		return (false);
4212cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4213dfe13344SJeff Roberson #ifdef NUMA
4214fc03d22bSJeff Roberson 	/*
42150a81b439SJeff Roberson 	 * Check to see if we should be populating the cross bucket.  If it
42160a81b439SJeff Roberson 	 * is already populated we will fall through and attempt to populate
42170a81b439SJeff Roberson 	 * the free bucket.
4218fc03d22bSJeff Roberson 	 */
4219dfe13344SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) {
42200a81b439SJeff Roberson 		domain = PCPU_GET(domain);
4221376b1ba3SJeff Roberson 		if (domain != itemdomain &&
4222376b1ba3SJeff Roberson 		    cache->uc_crossbucket.ucb_bucket == NULL) {
4223376b1ba3SJeff Roberson 			cache_bucket_load_cross(cache, bucket);
42240a81b439SJeff Roberson 			return (true);
42250a81b439SJeff Roberson 		}
42260a81b439SJeff Roberson 	}
42270a81b439SJeff Roberson #endif
42280a81b439SJeff Roberson 	/*
42290a81b439SJeff Roberson 	 * We may have lost the race to fill the bucket or switched CPUs.
42300a81b439SJeff Roberson 	 */
4231376b1ba3SJeff Roberson 	if (cache->uc_freebucket.ucb_bucket != NULL) {
4232fc03d22bSJeff Roberson 		critical_exit();
42336fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
42340a81b439SJeff Roberson 		critical_enter();
42350a81b439SJeff Roberson 	} else
4236376b1ba3SJeff Roberson 		cache_bucket_load_free(cache, bucket);
42378355f576SJeff Roberson 
42380a81b439SJeff Roberson 	return (true);
42398355f576SJeff Roberson }
42408355f576SJeff Roberson 
4241ab3185d1SJeff Roberson void
4242ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata)
4243ab3185d1SJeff Roberson {
4244ab3185d1SJeff Roberson 
4245ab3185d1SJeff Roberson 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
424619fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
4247ab3185d1SJeff Roberson 
4248e63a1c2fSRyan Libby 	CTR2(KTR_UMA, "uma_zfree_domain zone %s(%p)", zone->uz_name, zone);
4249ab3185d1SJeff Roberson 
4250ab3185d1SJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
4251ab3185d1SJeff Roberson 	    ("uma_zfree_domain: called with spinlock or critical section held"));
4252ab3185d1SJeff Roberson 
4253ab3185d1SJeff Roberson         /* uma_zfree(..., NULL) does nothing, to match free(9). */
4254ab3185d1SJeff Roberson         if (item == NULL)
4255ab3185d1SJeff Roberson                 return;
4256ab3185d1SJeff Roberson 	zone_free_item(zone, item, udata, SKIP_NONE);
4257ab3185d1SJeff Roberson }
4258ab3185d1SJeff Roberson 
42598355f576SJeff Roberson static void
4260bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item)
42618355f576SJeff Roberson {
4262bb15d1c7SGleb Smirnoff 	uma_keg_t keg;
4263ab3185d1SJeff Roberson 	uma_domain_t dom;
42649b8db4d0SRyan Libby 	int freei;
4265099a0e58SBosko Milekic 
4266bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
42678b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, slab->us_domain);
4268ab3185d1SJeff Roberson 
42698355f576SJeff Roberson 	/* Do we need to remove from any lists? */
42708b987a77SJeff Roberson 	dom = &keg->uk_domain[slab->us_domain];
4271099a0e58SBosko Milekic 	if (slab->us_freecount + 1 == keg->uk_ipers) {
42728355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
4273ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
42744ab3aee8SMark Johnston 		dom->ud_free_slabs++;
42758355f576SJeff Roberson 	} else if (slab->us_freecount == 0) {
42768355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
4277ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
42788355f576SJeff Roberson 	}
42798355f576SJeff Roberson 
4280ef72505eSJeff Roberson 	/* Slab management. */
42811e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
42829b78b1f4SJeff Roberson 	BIT_SET(keg->uk_ipers, freei, &slab->us_free);
42838355f576SJeff Roberson 	slab->us_freecount++;
42848355f576SJeff Roberson 
4285ef72505eSJeff Roberson 	/* Keg statistics. */
42864ab3aee8SMark Johnston 	dom->ud_free_items++;
42870095a784SJeff Roberson }
42880095a784SJeff Roberson 
42890095a784SJeff Roberson static void
4290b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt)
42910095a784SJeff Roberson {
42928b987a77SJeff Roberson 	struct mtx *lock;
4293b75c4efcSAndrew Turner 	uma_zone_t zone;
42940095a784SJeff Roberson 	uma_slab_t slab;
42950095a784SJeff Roberson 	uma_keg_t keg;
42960095a784SJeff Roberson 	uint8_t *mem;
42978b987a77SJeff Roberson 	void *item;
42980095a784SJeff Roberson 	int i;
42998355f576SJeff Roberson 
4300b75c4efcSAndrew Turner 	zone = arg;
4301bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
43028b987a77SJeff Roberson 	lock = NULL;
430354c5ae80SRyan Libby 	if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0))
43048b987a77SJeff Roberson 		lock = KEG_LOCK(keg, 0);
43050095a784SJeff Roberson 	for (i = 0; i < cnt; i++) {
43060095a784SJeff Roberson 		item = bucket[i];
430754c5ae80SRyan Libby 		if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) {
43080095a784SJeff Roberson 			slab = vtoslab((vm_offset_t)item);
43098b987a77SJeff Roberson 		} else {
43108b987a77SJeff Roberson 			mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
431154c5ae80SRyan Libby 			if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0)
43128b987a77SJeff Roberson 				slab = hash_sfind(&keg->uk_hash, mem);
43138b987a77SJeff Roberson 			else
43148b987a77SJeff Roberson 				slab = (uma_slab_t)(mem + keg->uk_pgoff);
43158b987a77SJeff Roberson 		}
43168b987a77SJeff Roberson 		if (lock != KEG_LOCKPTR(keg, slab->us_domain)) {
43178b987a77SJeff Roberson 			if (lock != NULL)
43188b987a77SJeff Roberson 				mtx_unlock(lock);
43198b987a77SJeff Roberson 			lock = KEG_LOCK(keg, slab->us_domain);
43208b987a77SJeff Roberson 		}
4321bb15d1c7SGleb Smirnoff 		slab_free_item(zone, slab, item);
43220095a784SJeff Roberson 	}
43238b987a77SJeff Roberson 	if (lock != NULL)
43248b987a77SJeff Roberson 		mtx_unlock(lock);
43258355f576SJeff Roberson }
43268355f576SJeff Roberson 
43270095a784SJeff Roberson /*
43280095a784SJeff Roberson  * Frees a single item to any zone.
43290095a784SJeff Roberson  *
43300095a784SJeff Roberson  * Arguments:
43310095a784SJeff Roberson  *	zone   The zone to free to
43320095a784SJeff Roberson  *	item   The item we're freeing
43330095a784SJeff Roberson  *	udata  User supplied data for the dtor
43340095a784SJeff Roberson  *	skip   Skip dtors and finis
43350095a784SJeff Roberson  */
43366d88d784SJeff Roberson static __noinline void
43370095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
43380095a784SJeff Roberson {
4339c5deaf04SGleb Smirnoff 
4340d4665eaaSJeff Roberson 	/*
4341d4665eaaSJeff Roberson 	 * If a free is sent directly to an SMR zone we have to
4342d4665eaaSJeff Roberson 	 * synchronize immediately because the item can instantly
4343d4665eaaSJeff Roberson 	 * be reallocated. This should only happen in degenerate
4344d4665eaaSJeff Roberson 	 * cases when no memory is available for per-cpu caches.
4345d4665eaaSJeff Roberson 	 */
4346d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE)
4347d4665eaaSJeff Roberson 		smr_synchronize(zone->uz_smr);
4348d4665eaaSJeff Roberson 
4349cc7ce83aSJeff Roberson 	item_dtor(zone, item, zone->uz_size, udata, skip);
43500095a784SJeff Roberson 
43510095a784SJeff Roberson 	if (skip < SKIP_FINI && zone->uz_fini)
43520095a784SJeff Roberson 		zone->uz_fini(item, zone->uz_size);
43530095a784SJeff Roberson 
43540095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, &item, 1);
4355bb15d1c7SGleb Smirnoff 
4356bb15d1c7SGleb Smirnoff 	if (skip & SKIP_CNT)
4357bb15d1c7SGleb Smirnoff 		return;
4358bb15d1c7SGleb Smirnoff 
43592efcc8cbSGleb Smirnoff 	counter_u64_add(zone->uz_frees, 1);
43602efcc8cbSGleb Smirnoff 
43614bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
43624bd61e19SJeff Roberson 		zone_free_limit(zone, 1);
4363bb45b411SGleb Smirnoff }
43640095a784SJeff Roberson 
43658355f576SJeff Roberson /* See uma.h */
43661c6cae97SLawrence Stewart int
4367736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems)
4368736ee590SJeff Roberson {
4369bb15d1c7SGleb Smirnoff 	struct uma_bucket_zone *ubz;
4370003cf08bSMark Johnston 	int count;
4371bb15d1c7SGleb Smirnoff 
43724bd61e19SJeff Roberson 	/*
43734bd61e19SJeff Roberson 	 * XXX This can misbehave if the zone has any allocations with
43744bd61e19SJeff Roberson 	 * no limit and a limit is imposed.  There is currently no
43754bd61e19SJeff Roberson 	 * way to clear a limit.
43764bd61e19SJeff Roberson 	 */
4377bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4378003cf08bSMark Johnston 	ubz = bucket_zone_max(zone, nitems);
4379003cf08bSMark Johnston 	count = ubz != NULL ? ubz->ubz_entries : 0;
438020a4e154SJeff Roberson 	zone->uz_bucket_size_max = zone->uz_bucket_size = count;
438120a4e154SJeff Roberson 	if (zone->uz_bucket_size_min > zone->uz_bucket_size_max)
438220a4e154SJeff Roberson 		zone->uz_bucket_size_min = zone->uz_bucket_size_max;
4383bb15d1c7SGleb Smirnoff 	zone->uz_max_items = nitems;
4384cc7ce83aSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_LIMIT;
4385cc7ce83aSJeff Roberson 	zone_update_caches(zone);
43864bd61e19SJeff Roberson 	/* We may need to wake waiters. */
43874bd61e19SJeff Roberson 	wakeup(&zone->uz_max_items);
4388bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4389bb15d1c7SGleb Smirnoff 
4390bb15d1c7SGleb Smirnoff 	return (nitems);
4391bb15d1c7SGleb Smirnoff }
4392bb15d1c7SGleb Smirnoff 
4393bb15d1c7SGleb Smirnoff /* See uma.h */
4394003cf08bSMark Johnston void
4395bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems)
4396bb15d1c7SGleb Smirnoff {
4397003cf08bSMark Johnston 	struct uma_bucket_zone *ubz;
4398003cf08bSMark Johnston 	int bpcpu;
4399bb15d1c7SGleb Smirnoff 
4400bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4401003cf08bSMark Johnston 	ubz = bucket_zone_max(zone, nitems);
4402003cf08bSMark Johnston 	if (ubz != NULL) {
4403003cf08bSMark Johnston 		bpcpu = 2;
4404dfe13344SJeff Roberson 		if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
4405003cf08bSMark Johnston 			/* Count the cross-domain bucket. */
4406003cf08bSMark Johnston 			bpcpu++;
4407003cf08bSMark Johnston 		nitems -= ubz->ubz_entries * bpcpu * mp_ncpus;
440820a4e154SJeff Roberson 		zone->uz_bucket_size_max = ubz->ubz_entries;
4409003cf08bSMark Johnston 	} else {
441020a4e154SJeff Roberson 		zone->uz_bucket_size_max = zone->uz_bucket_size = 0;
4411003cf08bSMark Johnston 	}
441220a4e154SJeff Roberson 	if (zone->uz_bucket_size_min > zone->uz_bucket_size_max)
441320a4e154SJeff Roberson 		zone->uz_bucket_size_min = zone->uz_bucket_size_max;
4414bb15d1c7SGleb Smirnoff 	zone->uz_bkt_max = nitems;
4415bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4416736ee590SJeff Roberson }
4417736ee590SJeff Roberson 
4418736ee590SJeff Roberson /* See uma.h */
4419e49471b0SAndre Oppermann int
4420e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone)
4421e49471b0SAndre Oppermann {
4422e49471b0SAndre Oppermann 	int nitems;
4423e49471b0SAndre Oppermann 
4424727c6918SJeff Roberson 	nitems = atomic_load_64(&zone->uz_max_items);
4425e49471b0SAndre Oppermann 
4426e49471b0SAndre Oppermann 	return (nitems);
4427e49471b0SAndre Oppermann }
4428e49471b0SAndre Oppermann 
4429e49471b0SAndre Oppermann /* See uma.h */
44302f891cd5SPawel Jakub Dawidek void
44312f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning)
44322f891cd5SPawel Jakub Dawidek {
44332f891cd5SPawel Jakub Dawidek 
4434727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
44352f891cd5SPawel Jakub Dawidek 	zone->uz_warning = warning;
44362f891cd5SPawel Jakub Dawidek }
44372f891cd5SPawel Jakub Dawidek 
44382f891cd5SPawel Jakub Dawidek /* See uma.h */
443954503a13SJonathan T. Looney void
444054503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
444154503a13SJonathan T. Looney {
444254503a13SJonathan T. Looney 
4443727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
4444e60b2fcbSGleb Smirnoff 	TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
444554503a13SJonathan T. Looney }
444654503a13SJonathan T. Looney 
444754503a13SJonathan T. Looney /* See uma.h */
4448c4ae7908SLawrence Stewart int
4449c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone)
4450c4ae7908SLawrence Stewart {
4451c4ae7908SLawrence Stewart 	int64_t nitems;
4452c4ae7908SLawrence Stewart 	u_int i;
4453c4ae7908SLawrence Stewart 
4454bfb6b7a1SJeff Roberson 	nitems = 0;
4455bfb6b7a1SJeff Roberson 	if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER)
44562efcc8cbSGleb Smirnoff 		nitems = counter_u64_fetch(zone->uz_allocs) -
44572efcc8cbSGleb Smirnoff 		    counter_u64_fetch(zone->uz_frees);
4458727c6918SJeff Roberson 	CPU_FOREACH(i)
4459727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) -
4460727c6918SJeff Roberson 		    atomic_load_64(&zone->uz_cpu[i].uc_frees);
4461c4ae7908SLawrence Stewart 
4462c4ae7908SLawrence Stewart 	return (nitems < 0 ? 0 : nitems);
4463c4ae7908SLawrence Stewart }
4464c4ae7908SLawrence Stewart 
446520a4e154SJeff Roberson static uint64_t
446620a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone)
446720a4e154SJeff Roberson {
446820a4e154SJeff Roberson 	uint64_t nitems;
446920a4e154SJeff Roberson 	u_int i;
447020a4e154SJeff Roberson 
4471bfb6b7a1SJeff Roberson 	nitems = 0;
4472bfb6b7a1SJeff Roberson 	if (zone->uz_allocs != EARLY_COUNTER)
447320a4e154SJeff Roberson 		nitems = counter_u64_fetch(zone->uz_allocs);
4474727c6918SJeff Roberson 	CPU_FOREACH(i)
4475727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs);
447620a4e154SJeff Roberson 
447720a4e154SJeff Roberson 	return (nitems);
447820a4e154SJeff Roberson }
447920a4e154SJeff Roberson 
448020a4e154SJeff Roberson static uint64_t
448120a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone)
448220a4e154SJeff Roberson {
448320a4e154SJeff Roberson 	uint64_t nitems;
448420a4e154SJeff Roberson 	u_int i;
448520a4e154SJeff Roberson 
4486bfb6b7a1SJeff Roberson 	nitems = 0;
4487bfb6b7a1SJeff Roberson 	if (zone->uz_frees != EARLY_COUNTER)
448820a4e154SJeff Roberson 		nitems = counter_u64_fetch(zone->uz_frees);
4489727c6918SJeff Roberson 	CPU_FOREACH(i)
4490727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees);
449120a4e154SJeff Roberson 
449220a4e154SJeff Roberson 	return (nitems);
449320a4e154SJeff Roberson }
449420a4e154SJeff Roberson 
449531c251a0SJeff Roberson #ifdef INVARIANTS
449631c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */
449731c251a0SJeff Roberson static uint64_t
449831c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg)
449931c251a0SJeff Roberson {
450031c251a0SJeff Roberson 	uma_zone_t z;
450131c251a0SJeff Roberson 	uint64_t nitems;
450231c251a0SJeff Roberson 
450331c251a0SJeff Roberson 	nitems = 0;
450431c251a0SJeff Roberson 	LIST_FOREACH(z, &keg->uk_zones, uz_link)
450531c251a0SJeff Roberson 		nitems += uma_zone_get_allocs(z);
450631c251a0SJeff Roberson 
450731c251a0SJeff Roberson 	return (nitems);
450831c251a0SJeff Roberson }
450931c251a0SJeff Roberson #endif
451031c251a0SJeff Roberson 
4511c4ae7908SLawrence Stewart /* See uma.h */
4512736ee590SJeff Roberson void
4513099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit)
4514099a0e58SBosko Milekic {
4515e20a199fSJeff Roberson 	uma_keg_t keg;
4516e20a199fSJeff Roberson 
4517bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4518727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
4519e20a199fSJeff Roberson 	keg->uk_init = uminit;
4520099a0e58SBosko Milekic }
4521099a0e58SBosko Milekic 
4522099a0e58SBosko Milekic /* See uma.h */
4523099a0e58SBosko Milekic void
4524099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
4525099a0e58SBosko Milekic {
4526e20a199fSJeff Roberson 	uma_keg_t keg;
4527e20a199fSJeff Roberson 
4528bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4529727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
4530e20a199fSJeff Roberson 	keg->uk_fini = fini;
4531099a0e58SBosko Milekic }
4532099a0e58SBosko Milekic 
4533099a0e58SBosko Milekic /* See uma.h */
4534099a0e58SBosko Milekic void
4535099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
4536099a0e58SBosko Milekic {
4537af526374SJeff Roberson 
4538727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
4539099a0e58SBosko Milekic 	zone->uz_init = zinit;
4540099a0e58SBosko Milekic }
4541099a0e58SBosko Milekic 
4542099a0e58SBosko Milekic /* See uma.h */
4543099a0e58SBosko Milekic void
4544099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
4545099a0e58SBosko Milekic {
4546af526374SJeff Roberson 
4547727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
4548099a0e58SBosko Milekic 	zone->uz_fini = zfini;
4549099a0e58SBosko Milekic }
4550099a0e58SBosko Milekic 
4551099a0e58SBosko Milekic /* See uma.h */
4552099a0e58SBosko Milekic void
45538355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef)
45548355f576SJeff Roberson {
45550095a784SJeff Roberson 	uma_keg_t keg;
4556e20a199fSJeff Roberson 
4557bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4558727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
45590095a784SJeff Roberson 	keg->uk_freef = freef;
45608355f576SJeff Roberson }
45618355f576SJeff Roberson 
45628355f576SJeff Roberson /* See uma.h */
45638355f576SJeff Roberson void
45648355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
45658355f576SJeff Roberson {
4566e20a199fSJeff Roberson 	uma_keg_t keg;
4567e20a199fSJeff Roberson 
4568bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4569727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
4570e20a199fSJeff Roberson 	keg->uk_allocf = allocf;
45718355f576SJeff Roberson }
45728355f576SJeff Roberson 
45738355f576SJeff Roberson /* See uma.h */
45746fd34d6fSJeff Roberson void
4575d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr)
4576d4665eaaSJeff Roberson {
4577d4665eaaSJeff Roberson 
4578d4665eaaSJeff Roberson 	ZONE_ASSERT_COLD(zone);
4579d4665eaaSJeff Roberson 
4580d4665eaaSJeff Roberson 	zone->uz_flags |= UMA_ZONE_SMR;
4581d4665eaaSJeff Roberson 	zone->uz_smr = smr;
4582d4665eaaSJeff Roberson 	zone_update_caches(zone);
4583d4665eaaSJeff Roberson }
4584d4665eaaSJeff Roberson 
4585d4665eaaSJeff Roberson smr_t
4586d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone)
4587d4665eaaSJeff Roberson {
4588d4665eaaSJeff Roberson 
4589d4665eaaSJeff Roberson 	return (zone->uz_smr);
4590d4665eaaSJeff Roberson }
4591d4665eaaSJeff Roberson 
4592d4665eaaSJeff Roberson /* See uma.h */
4593d4665eaaSJeff Roberson void
45946fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items)
45956fd34d6fSJeff Roberson {
45966fd34d6fSJeff Roberson 	uma_keg_t keg;
45976fd34d6fSJeff Roberson 
4598bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4599727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
46006fd34d6fSJeff Roberson 	keg->uk_reserve = items;
46016fd34d6fSJeff Roberson }
46026fd34d6fSJeff Roberson 
46036fd34d6fSJeff Roberson /* See uma.h */
46048355f576SJeff Roberson int
4605a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count)
46068355f576SJeff Roberson {
4607099a0e58SBosko Milekic 	uma_keg_t keg;
46088355f576SJeff Roberson 	vm_offset_t kva;
46099ba30bcbSZbigniew Bodek 	u_int pages;
46108355f576SJeff Roberson 
4611bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4612727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
4613727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
46148355f576SJeff Roberson 
461579c9f942SJeff Roberson 	pages = howmany(count, keg->uk_ipers) * keg->uk_ppera;
4616a553d4b8SJeff Roberson 
4617a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
4618a4915c21SAttilio Rao 	if (keg->uk_ppera > 1) {
4619a4915c21SAttilio Rao #else
4620a4915c21SAttilio Rao 	if (1) {
4621a4915c21SAttilio Rao #endif
462257223e99SAndriy Gapon 		kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
4623d1f42ac2SAlan Cox 		if (kva == 0)
46248355f576SJeff Roberson 			return (0);
4625a4915c21SAttilio Rao 	} else
4626a4915c21SAttilio Rao 		kva = 0;
4627bb15d1c7SGleb Smirnoff 
4628bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4629bb15d1c7SGleb Smirnoff 	MPASS(keg->uk_kva == 0);
4630099a0e58SBosko Milekic 	keg->uk_kva = kva;
4631a4915c21SAttilio Rao 	keg->uk_offset = 0;
4632bb15d1c7SGleb Smirnoff 	zone->uz_max_items = pages * keg->uk_ipers;
4633a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
4634a4915c21SAttilio Rao 	keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
4635a4915c21SAttilio Rao #else
4636a4915c21SAttilio Rao 	keg->uk_allocf = noobj_alloc;
4637a4915c21SAttilio Rao #endif
4638cc7ce83aSJeff Roberson 	keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
4639cc7ce83aSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
4640cc7ce83aSJeff Roberson 	zone_update_caches(zone);
4641bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4642af526374SJeff Roberson 
46438355f576SJeff Roberson 	return (1);
46448355f576SJeff Roberson }
46458355f576SJeff Roberson 
46468355f576SJeff Roberson /* See uma.h */
46478355f576SJeff Roberson void
46488355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items)
46498355f576SJeff Roberson {
4650920239efSMark Johnston 	struct vm_domainset_iter di;
4651ab3185d1SJeff Roberson 	uma_domain_t dom;
46528355f576SJeff Roberson 	uma_slab_t slab;
4653099a0e58SBosko Milekic 	uma_keg_t keg;
465486220393SMark Johnston 	int aflags, domain, slabs;
46558355f576SJeff Roberson 
4656bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
465779c9f942SJeff Roberson 	slabs = howmany(items, keg->uk_ipers);
4658194a979eSMark Johnston 	while (slabs-- > 0) {
465986220393SMark Johnston 		aflags = M_NOWAIT;
466086220393SMark Johnston 		vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
466186220393SMark Johnston 		    &aflags);
466286220393SMark Johnston 		for (;;) {
466386220393SMark Johnston 			slab = keg_alloc_slab(keg, zone, domain, M_WAITOK,
466486220393SMark Johnston 			    aflags);
466586220393SMark Johnston 			if (slab != NULL) {
4666ab3185d1SJeff Roberson 				dom = &keg->uk_domain[slab->us_domain];
46674ab3aee8SMark Johnston 				/*
46684ab3aee8SMark Johnston 				 * keg_alloc_slab() always returns a slab on the
46694ab3aee8SMark Johnston 				 * partial list.
46704ab3aee8SMark Johnston 				 */
46718b987a77SJeff Roberson 				LIST_REMOVE(slab, us_link);
467286220393SMark Johnston 				LIST_INSERT_HEAD(&dom->ud_free_slab, slab,
467386220393SMark Johnston 				    us_link);
46744ab3aee8SMark Johnston 				dom->ud_free_slabs++;
46758b987a77SJeff Roberson 				KEG_UNLOCK(keg, slab->us_domain);
4676920239efSMark Johnston 				break;
46778355f576SJeff Roberson 			}
46788b987a77SJeff Roberson 			if (vm_domainset_iter_policy(&di, &domain) != 0)
467986220393SMark Johnston 				vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
468086220393SMark Johnston 		}
468186220393SMark Johnston 	}
468286220393SMark Johnston }
46838355f576SJeff Roberson 
4684*ed581bf6SJeff Roberson /*
4685*ed581bf6SJeff Roberson  * Returns a snapshot of memory consumption in bytes.
4686*ed581bf6SJeff Roberson  */
4687*ed581bf6SJeff Roberson size_t
4688*ed581bf6SJeff Roberson uma_zone_memory(uma_zone_t zone)
4689*ed581bf6SJeff Roberson {
4690*ed581bf6SJeff Roberson 	size_t sz;
4691*ed581bf6SJeff Roberson 	int i;
4692*ed581bf6SJeff Roberson 
4693*ed581bf6SJeff Roberson 	sz = 0;
4694*ed581bf6SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_CACHE) {
4695*ed581bf6SJeff Roberson 		for (i = 0; i < vm_ndomains; i++)
4696*ed581bf6SJeff Roberson 			sz += zone->uz_domain[i].uzd_nitems;
4697*ed581bf6SJeff Roberson 		return (sz * zone->uz_size);
4698*ed581bf6SJeff Roberson 	}
4699*ed581bf6SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
4700*ed581bf6SJeff Roberson 		sz += zone->uz_keg->uk_domain[i].ud_pages;
4701*ed581bf6SJeff Roberson 
4702*ed581bf6SJeff Roberson 	return (sz * PAGE_SIZE);
4703*ed581bf6SJeff Roberson }
4704*ed581bf6SJeff Roberson 
47058355f576SJeff Roberson /* See uma.h */
470608cfa56eSMark Johnston void
470708cfa56eSMark Johnston uma_reclaim(int req)
47088355f576SJeff Roberson {
470944ec2b63SKonstantin Belousov 
47101431a748SGleb Smirnoff 	CTR0(KTR_UMA, "UMA: vm asked us to release pages!");
471108cfa56eSMark Johnston 	sx_xlock(&uma_reclaim_lock);
471286bbae32SJeff Roberson 	bucket_enable();
471308cfa56eSMark Johnston 
471408cfa56eSMark Johnston 	switch (req) {
471508cfa56eSMark Johnston 	case UMA_RECLAIM_TRIM:
471620a4e154SJeff Roberson 		zone_foreach(zone_trim, NULL);
471708cfa56eSMark Johnston 		break;
471808cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN:
471908cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN_CPU:
472020a4e154SJeff Roberson 		zone_foreach(zone_drain, NULL);
472108cfa56eSMark Johnston 		if (req == UMA_RECLAIM_DRAIN_CPU) {
472208cfa56eSMark Johnston 			pcpu_cache_drain_safe(NULL);
472320a4e154SJeff Roberson 			zone_foreach(zone_drain, NULL);
4724a2de44abSAlexander Motin 		}
472508cfa56eSMark Johnston 		break;
472608cfa56eSMark Johnston 	default:
472708cfa56eSMark Johnston 		panic("unhandled reclamation request %d", req);
472808cfa56eSMark Johnston 	}
47290f9b7bf3SMark Johnston 
47308355f576SJeff Roberson 	/*
47318355f576SJeff Roberson 	 * Some slabs may have been freed but this zone will be visited early
47328355f576SJeff Roberson 	 * we visit again so that we can free pages that are empty once other
47338355f576SJeff Roberson 	 * zones are drained.  We have to do the same for buckets.
47348355f576SJeff Roberson 	 */
47359b8db4d0SRyan Libby 	zone_drain(slabzones[0], NULL);
47369b8db4d0SRyan Libby 	zone_drain(slabzones[1], NULL);
4737cae33c14SJeff Roberson 	bucket_zone_drain();
473808cfa56eSMark Johnston 	sx_xunlock(&uma_reclaim_lock);
47398355f576SJeff Roberson }
47408355f576SJeff Roberson 
47412e47807cSJeff Roberson static volatile int uma_reclaim_needed;
474244ec2b63SKonstantin Belousov 
474344ec2b63SKonstantin Belousov void
474444ec2b63SKonstantin Belousov uma_reclaim_wakeup(void)
474544ec2b63SKonstantin Belousov {
474644ec2b63SKonstantin Belousov 
47472e47807cSJeff Roberson 	if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
47482e47807cSJeff Roberson 		wakeup(uma_reclaim);
474944ec2b63SKonstantin Belousov }
475044ec2b63SKonstantin Belousov 
475144ec2b63SKonstantin Belousov void
475244ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused)
475344ec2b63SKonstantin Belousov {
475444ec2b63SKonstantin Belousov 
475544ec2b63SKonstantin Belousov 	for (;;) {
475608cfa56eSMark Johnston 		sx_xlock(&uma_reclaim_lock);
4757200f8117SKonstantin Belousov 		while (atomic_load_int(&uma_reclaim_needed) == 0)
475808cfa56eSMark Johnston 			sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl",
47592e47807cSJeff Roberson 			    hz);
476008cfa56eSMark Johnston 		sx_xunlock(&uma_reclaim_lock);
47619b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
476208cfa56eSMark Johnston 		uma_reclaim(UMA_RECLAIM_DRAIN_CPU);
4763200f8117SKonstantin Belousov 		atomic_store_int(&uma_reclaim_needed, 0);
47642e47807cSJeff Roberson 		/* Don't fire more than once per-second. */
47652e47807cSJeff Roberson 		pause("umarclslp", hz);
476644ec2b63SKonstantin Belousov 	}
476744ec2b63SKonstantin Belousov }
476844ec2b63SKonstantin Belousov 
4769663b416fSJohn Baldwin /* See uma.h */
477008cfa56eSMark Johnston void
477108cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req)
477208cfa56eSMark Johnston {
477308cfa56eSMark Johnston 
477408cfa56eSMark Johnston 	switch (req) {
477508cfa56eSMark Johnston 	case UMA_RECLAIM_TRIM:
477620a4e154SJeff Roberson 		zone_trim(zone, NULL);
477708cfa56eSMark Johnston 		break;
477808cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN:
477920a4e154SJeff Roberson 		zone_drain(zone, NULL);
478008cfa56eSMark Johnston 		break;
478108cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN_CPU:
478208cfa56eSMark Johnston 		pcpu_cache_drain_safe(zone);
478320a4e154SJeff Roberson 		zone_drain(zone, NULL);
478408cfa56eSMark Johnston 		break;
478508cfa56eSMark Johnston 	default:
478608cfa56eSMark Johnston 		panic("unhandled reclamation request %d", req);
478708cfa56eSMark Johnston 	}
478808cfa56eSMark Johnston }
478908cfa56eSMark Johnston 
479008cfa56eSMark Johnston /* See uma.h */
4791663b416fSJohn Baldwin int
4792663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone)
4793663b416fSJohn Baldwin {
4794663b416fSJohn Baldwin 
4795727c6918SJeff Roberson 	return (atomic_load_32(&zone->uz_sleepers) > 0);
47966c125b8dSMohan Srinivasan }
47976c125b8dSMohan Srinivasan 
47982e47807cSJeff Roberson unsigned long
47992e47807cSJeff Roberson uma_limit(void)
48002e47807cSJeff Roberson {
48012e47807cSJeff Roberson 
48022e47807cSJeff Roberson 	return (uma_kmem_limit);
48032e47807cSJeff Roberson }
48042e47807cSJeff Roberson 
48052e47807cSJeff Roberson void
48062e47807cSJeff Roberson uma_set_limit(unsigned long limit)
48072e47807cSJeff Roberson {
48082e47807cSJeff Roberson 
48092e47807cSJeff Roberson 	uma_kmem_limit = limit;
48102e47807cSJeff Roberson }
48112e47807cSJeff Roberson 
48122e47807cSJeff Roberson unsigned long
48132e47807cSJeff Roberson uma_size(void)
48142e47807cSJeff Roberson {
48152e47807cSJeff Roberson 
4816058f0f74SMark Johnston 	return (atomic_load_long(&uma_kmem_total));
4817ad5b0f5bSJeff Roberson }
4818ad5b0f5bSJeff Roberson 
4819ad5b0f5bSJeff Roberson long
4820ad5b0f5bSJeff Roberson uma_avail(void)
4821ad5b0f5bSJeff Roberson {
4822ad5b0f5bSJeff Roberson 
4823058f0f74SMark Johnston 	return (uma_kmem_limit - uma_size());
48242e47807cSJeff Roberson }
48252e47807cSJeff Roberson 
4826a0d4b0aeSRobert Watson #ifdef DDB
48278355f576SJeff Roberson /*
48287a52a97eSRobert Watson  * Generate statistics across both the zone and its per-cpu cache's.  Return
48297a52a97eSRobert Watson  * desired statistics if the pointer is non-NULL for that statistic.
48307a52a97eSRobert Watson  *
48317a52a97eSRobert Watson  * Note: does not update the zone statistics, as it can't safely clear the
48327a52a97eSRobert Watson  * per-CPU cache statistic.
48337a52a97eSRobert Watson  *
48347a52a97eSRobert Watson  */
48357a52a97eSRobert Watson static void
48360f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp,
4837c1685086SJeff Roberson     uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp)
48387a52a97eSRobert Watson {
48397a52a97eSRobert Watson 	uma_cache_t cache;
4840c1685086SJeff Roberson 	uint64_t allocs, frees, sleeps, xdomain;
48417a52a97eSRobert Watson 	int cachefree, cpu;
48427a52a97eSRobert Watson 
4843c1685086SJeff Roberson 	allocs = frees = sleeps = xdomain = 0;
48447a52a97eSRobert Watson 	cachefree = 0;
48453aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
48467a52a97eSRobert Watson 		cache = &z->uz_cpu[cpu];
4847376b1ba3SJeff Roberson 		cachefree += cache->uc_allocbucket.ucb_cnt;
4848376b1ba3SJeff Roberson 		cachefree += cache->uc_freebucket.ucb_cnt;
4849376b1ba3SJeff Roberson 		xdomain += cache->uc_crossbucket.ucb_cnt;
4850376b1ba3SJeff Roberson 		cachefree += cache->uc_crossbucket.ucb_cnt;
48517a52a97eSRobert Watson 		allocs += cache->uc_allocs;
48527a52a97eSRobert Watson 		frees += cache->uc_frees;
48537a52a97eSRobert Watson 	}
48542efcc8cbSGleb Smirnoff 	allocs += counter_u64_fetch(z->uz_allocs);
48552efcc8cbSGleb Smirnoff 	frees += counter_u64_fetch(z->uz_frees);
4856bf965959SSean Bruno 	sleeps += z->uz_sleeps;
4857c1685086SJeff Roberson 	xdomain += z->uz_xdomain;
48587a52a97eSRobert Watson 	if (cachefreep != NULL)
48597a52a97eSRobert Watson 		*cachefreep = cachefree;
48607a52a97eSRobert Watson 	if (allocsp != NULL)
48617a52a97eSRobert Watson 		*allocsp = allocs;
48627a52a97eSRobert Watson 	if (freesp != NULL)
48637a52a97eSRobert Watson 		*freesp = frees;
4864bf965959SSean Bruno 	if (sleepsp != NULL)
4865bf965959SSean Bruno 		*sleepsp = sleeps;
4866c1685086SJeff Roberson 	if (xdomainp != NULL)
4867c1685086SJeff Roberson 		*xdomainp = xdomain;
48687a52a97eSRobert Watson }
4869a0d4b0aeSRobert Watson #endif /* DDB */
48707a52a97eSRobert Watson 
48717a52a97eSRobert Watson static int
48727a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
48737a52a97eSRobert Watson {
48747a52a97eSRobert Watson 	uma_keg_t kz;
48757a52a97eSRobert Watson 	uma_zone_t z;
48767a52a97eSRobert Watson 	int count;
48777a52a97eSRobert Watson 
48787a52a97eSRobert Watson 	count = 0;
4879111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
48807a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
48817a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
48827a52a97eSRobert Watson 			count++;
48837a52a97eSRobert Watson 	}
4884b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link)
4885b47acb0aSGleb Smirnoff 		count++;
4886b47acb0aSGleb Smirnoff 
4887111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
48887a52a97eSRobert Watson 	return (sysctl_handle_int(oidp, &count, 0, req));
48897a52a97eSRobert Watson }
48907a52a97eSRobert Watson 
4891b47acb0aSGleb Smirnoff static void
4892b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf,
4893b47acb0aSGleb Smirnoff     struct uma_percpu_stat *ups, bool internal)
4894b47acb0aSGleb Smirnoff {
4895b47acb0aSGleb Smirnoff 	uma_zone_domain_t zdom;
4896b47acb0aSGleb Smirnoff 	uma_cache_t cache;
4897b47acb0aSGleb Smirnoff 	int i;
4898b47acb0aSGleb Smirnoff 
4899b47acb0aSGleb Smirnoff 
4900b47acb0aSGleb Smirnoff 	for (i = 0; i < vm_ndomains; i++) {
4901b47acb0aSGleb Smirnoff 		zdom = &z->uz_domain[i];
4902b47acb0aSGleb Smirnoff 		uth->uth_zone_free += zdom->uzd_nitems;
4903b47acb0aSGleb Smirnoff 	}
4904b47acb0aSGleb Smirnoff 	uth->uth_allocs = counter_u64_fetch(z->uz_allocs);
4905b47acb0aSGleb Smirnoff 	uth->uth_frees = counter_u64_fetch(z->uz_frees);
4906b47acb0aSGleb Smirnoff 	uth->uth_fails = counter_u64_fetch(z->uz_fails);
4907b47acb0aSGleb Smirnoff 	uth->uth_sleeps = z->uz_sleeps;
4908c1685086SJeff Roberson 	uth->uth_xdomain = z->uz_xdomain;
49091de9724eSMark Johnston 
4910b47acb0aSGleb Smirnoff 	/*
49111de9724eSMark Johnston 	 * While it is not normally safe to access the cache bucket pointers
49121de9724eSMark Johnston 	 * while not on the CPU that owns the cache, we only allow the pointers
49131de9724eSMark Johnston 	 * to be exchanged without the zone lock held, not invalidated, so
49141de9724eSMark Johnston 	 * accept the possible race associated with bucket exchange during
49151de9724eSMark Johnston 	 * monitoring.  Use atomic_load_ptr() to ensure that the bucket pointers
49161de9724eSMark Johnston 	 * are loaded only once.
4917b47acb0aSGleb Smirnoff 	 */
4918b47acb0aSGleb Smirnoff 	for (i = 0; i < mp_maxid + 1; i++) {
4919b47acb0aSGleb Smirnoff 		bzero(&ups[i], sizeof(*ups));
4920b47acb0aSGleb Smirnoff 		if (internal || CPU_ABSENT(i))
4921b47acb0aSGleb Smirnoff 			continue;
4922b47acb0aSGleb Smirnoff 		cache = &z->uz_cpu[i];
4923376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt;
4924376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt;
4925376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt;
4926b47acb0aSGleb Smirnoff 		ups[i].ups_allocs = cache->uc_allocs;
4927b47acb0aSGleb Smirnoff 		ups[i].ups_frees = cache->uc_frees;
4928b47acb0aSGleb Smirnoff 	}
4929b47acb0aSGleb Smirnoff }
4930b47acb0aSGleb Smirnoff 
49317a52a97eSRobert Watson static int
49327a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
49337a52a97eSRobert Watson {
49347a52a97eSRobert Watson 	struct uma_stream_header ush;
49357a52a97eSRobert Watson 	struct uma_type_header uth;
493663b5d112SKonstantin Belousov 	struct uma_percpu_stat *ups;
49377a52a97eSRobert Watson 	struct sbuf sbuf;
49387a52a97eSRobert Watson 	uma_keg_t kz;
49397a52a97eSRobert Watson 	uma_zone_t z;
49404bd61e19SJeff Roberson 	uint64_t items;
49418b987a77SJeff Roberson 	uint32_t kfree, pages;
49424e657159SMatthew D Fleming 	int count, error, i;
49437a52a97eSRobert Watson 
494400f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
494500f0e671SMatthew D Fleming 	if (error != 0)
494600f0e671SMatthew D Fleming 		return (error);
49474e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
49481eafc078SIan Lepore 	sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
494963b5d112SKonstantin Belousov 	ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK);
49504e657159SMatthew D Fleming 
4951404a593eSMatthew D Fleming 	count = 0;
4952111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
49537a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
49547a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
49557a52a97eSRobert Watson 			count++;
49567a52a97eSRobert Watson 	}
49577a52a97eSRobert Watson 
4958b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link)
4959b47acb0aSGleb Smirnoff 		count++;
4960b47acb0aSGleb Smirnoff 
49617a52a97eSRobert Watson 	/*
49627a52a97eSRobert Watson 	 * Insert stream header.
49637a52a97eSRobert Watson 	 */
49647a52a97eSRobert Watson 	bzero(&ush, sizeof(ush));
49657a52a97eSRobert Watson 	ush.ush_version = UMA_STREAM_VERSION;
4966ab3a57c0SRobert Watson 	ush.ush_maxcpus = (mp_maxid + 1);
49677a52a97eSRobert Watson 	ush.ush_count = count;
49684e657159SMatthew D Fleming 	(void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
49697a52a97eSRobert Watson 
49707a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
49718b987a77SJeff Roberson 		kfree = pages = 0;
49728b987a77SJeff Roberson 		for (i = 0; i < vm_ndomains; i++) {
49734ab3aee8SMark Johnston 			kfree += kz->uk_domain[i].ud_free_items;
49748b987a77SJeff Roberson 			pages += kz->uk_domain[i].ud_pages;
49758b987a77SJeff Roberson 		}
49767a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
49777a52a97eSRobert Watson 			bzero(&uth, sizeof(uth));
49787a52a97eSRobert Watson 			ZONE_LOCK(z);
4979cbbb4a00SRobert Watson 			strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
49807a52a97eSRobert Watson 			uth.uth_align = kz->uk_align;
49817a52a97eSRobert Watson 			uth.uth_size = kz->uk_size;
49827a52a97eSRobert Watson 			uth.uth_rsize = kz->uk_rsize;
49834bd61e19SJeff Roberson 			if (z->uz_max_items > 0) {
49844bd61e19SJeff Roberson 				items = UZ_ITEMS_COUNT(z->uz_items);
49854bd61e19SJeff Roberson 				uth.uth_pages = (items / kz->uk_ipers) *
4986bb15d1c7SGleb Smirnoff 					kz->uk_ppera;
49874bd61e19SJeff Roberson 			} else
49888b987a77SJeff Roberson 				uth.uth_pages = pages;
4989f8c86a5fSGleb Smirnoff 			uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) *
4990bb15d1c7SGleb Smirnoff 			    kz->uk_ppera;
4991bb15d1c7SGleb Smirnoff 			uth.uth_limit = z->uz_max_items;
49928b987a77SJeff Roberson 			uth.uth_keg_free = kfree;
4993cbbb4a00SRobert Watson 
4994cbbb4a00SRobert Watson 			/*
4995cbbb4a00SRobert Watson 			 * A zone is secondary is it is not the first entry
4996cbbb4a00SRobert Watson 			 * on the keg's zone list.
4997cbbb4a00SRobert Watson 			 */
4998e20a199fSJeff Roberson 			if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
4999cbbb4a00SRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z))
5000cbbb4a00SRobert Watson 				uth.uth_zone_flags = UTH_ZONE_SECONDARY;
5001b47acb0aSGleb Smirnoff 			uma_vm_zone_stats(&uth, z, &sbuf, ups,
5002b47acb0aSGleb Smirnoff 			    kz->uk_flags & UMA_ZFLAG_INTERNAL);
50032450bbb8SRobert Watson 			ZONE_UNLOCK(z);
500463b5d112SKonstantin Belousov 			(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
500563b5d112SKonstantin Belousov 			for (i = 0; i < mp_maxid + 1; i++)
500663b5d112SKonstantin Belousov 				(void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
50077a52a97eSRobert Watson 		}
50087a52a97eSRobert Watson 	}
5009b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
5010b47acb0aSGleb Smirnoff 		bzero(&uth, sizeof(uth));
5011b47acb0aSGleb Smirnoff 		ZONE_LOCK(z);
5012b47acb0aSGleb Smirnoff 		strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
5013b47acb0aSGleb Smirnoff 		uth.uth_size = z->uz_size;
5014b47acb0aSGleb Smirnoff 		uma_vm_zone_stats(&uth, z, &sbuf, ups, false);
5015b47acb0aSGleb Smirnoff 		ZONE_UNLOCK(z);
5016b47acb0aSGleb Smirnoff 		(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
5017b47acb0aSGleb Smirnoff 		for (i = 0; i < mp_maxid + 1; i++)
5018b47acb0aSGleb Smirnoff 			(void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
5019b47acb0aSGleb Smirnoff 	}
5020b47acb0aSGleb Smirnoff 
5021111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
50224e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
50234e657159SMatthew D Fleming 	sbuf_delete(&sbuf);
502463b5d112SKonstantin Belousov 	free(ups, M_TEMP);
50257a52a97eSRobert Watson 	return (error);
50267a52a97eSRobert Watson }
502748c5777eSRobert Watson 
50280a5a3ccbSGleb Smirnoff int
50290a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
50300a5a3ccbSGleb Smirnoff {
50310a5a3ccbSGleb Smirnoff 	uma_zone_t zone = *(uma_zone_t *)arg1;
503216be9f54SGleb Smirnoff 	int error, max;
50330a5a3ccbSGleb Smirnoff 
503416be9f54SGleb Smirnoff 	max = uma_zone_get_max(zone);
50350a5a3ccbSGleb Smirnoff 	error = sysctl_handle_int(oidp, &max, 0, req);
50360a5a3ccbSGleb Smirnoff 	if (error || !req->newptr)
50370a5a3ccbSGleb Smirnoff 		return (error);
50380a5a3ccbSGleb Smirnoff 
50390a5a3ccbSGleb Smirnoff 	uma_zone_set_max(zone, max);
50400a5a3ccbSGleb Smirnoff 
50410a5a3ccbSGleb Smirnoff 	return (0);
50420a5a3ccbSGleb Smirnoff }
50430a5a3ccbSGleb Smirnoff 
50440a5a3ccbSGleb Smirnoff int
50450a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
50460a5a3ccbSGleb Smirnoff {
504720a4e154SJeff Roberson 	uma_zone_t zone;
50480a5a3ccbSGleb Smirnoff 	int cur;
50490a5a3ccbSGleb Smirnoff 
505020a4e154SJeff Roberson 	/*
505120a4e154SJeff Roberson 	 * Some callers want to add sysctls for global zones that
505220a4e154SJeff Roberson 	 * may not yet exist so they pass a pointer to a pointer.
505320a4e154SJeff Roberson 	 */
505420a4e154SJeff Roberson 	if (arg2 == 0)
505520a4e154SJeff Roberson 		zone = *(uma_zone_t *)arg1;
505620a4e154SJeff Roberson 	else
505720a4e154SJeff Roberson 		zone = arg1;
50580a5a3ccbSGleb Smirnoff 	cur = uma_zone_get_cur(zone);
50590a5a3ccbSGleb Smirnoff 	return (sysctl_handle_int(oidp, &cur, 0, req));
50600a5a3ccbSGleb Smirnoff }
50610a5a3ccbSGleb Smirnoff 
506220a4e154SJeff Roberson static int
506320a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS)
506420a4e154SJeff Roberson {
506520a4e154SJeff Roberson 	uma_zone_t zone = arg1;
506620a4e154SJeff Roberson 	uint64_t cur;
506720a4e154SJeff Roberson 
506820a4e154SJeff Roberson 	cur = uma_zone_get_allocs(zone);
506920a4e154SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
507020a4e154SJeff Roberson }
507120a4e154SJeff Roberson 
507220a4e154SJeff Roberson static int
507320a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS)
507420a4e154SJeff Roberson {
507520a4e154SJeff Roberson 	uma_zone_t zone = arg1;
507620a4e154SJeff Roberson 	uint64_t cur;
507720a4e154SJeff Roberson 
507820a4e154SJeff Roberson 	cur = uma_zone_get_frees(zone);
507920a4e154SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
508020a4e154SJeff Roberson }
508120a4e154SJeff Roberson 
50826d204a6aSRyan Libby static int
50836d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS)
50846d204a6aSRyan Libby {
50856d204a6aSRyan Libby 	struct sbuf sbuf;
50866d204a6aSRyan Libby 	uma_zone_t zone = arg1;
50876d204a6aSRyan Libby 	int error;
50886d204a6aSRyan Libby 
50896d204a6aSRyan Libby 	sbuf_new_for_sysctl(&sbuf, NULL, 0, req);
50906d204a6aSRyan Libby 	if (zone->uz_flags != 0)
50916d204a6aSRyan Libby 		sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS);
50926d204a6aSRyan Libby 	else
50936d204a6aSRyan Libby 		sbuf_printf(&sbuf, "0");
50946d204a6aSRyan Libby 	error = sbuf_finish(&sbuf);
50956d204a6aSRyan Libby 	sbuf_delete(&sbuf);
50966d204a6aSRyan Libby 
50976d204a6aSRyan Libby 	return (error);
50986d204a6aSRyan Libby }
50996d204a6aSRyan Libby 
5100f7af5015SRyan Libby static int
5101f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS)
5102f7af5015SRyan Libby {
5103f7af5015SRyan Libby 	uma_keg_t keg = arg1;
5104f7af5015SRyan Libby 	int avail, effpct, total;
5105f7af5015SRyan Libby 
5106f7af5015SRyan Libby 	total = keg->uk_ppera * PAGE_SIZE;
510754c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0)
51089b8db4d0SRyan Libby 		total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize;
5109f7af5015SRyan Libby 	/*
5110f7af5015SRyan Libby 	 * We consider the client's requested size and alignment here, not the
5111f7af5015SRyan Libby 	 * real size determination uk_rsize, because we also adjust the real
5112f7af5015SRyan Libby 	 * size for internal implementation reasons (max bitset size).
5113f7af5015SRyan Libby 	 */
5114f7af5015SRyan Libby 	avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1);
5115f7af5015SRyan Libby 	if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
5116f7af5015SRyan Libby 		avail *= mp_maxid + 1;
5117f7af5015SRyan Libby 	effpct = 100 * avail / total;
5118f7af5015SRyan Libby 	return (sysctl_handle_int(oidp, &effpct, 0, req));
5119f7af5015SRyan Libby }
5120f7af5015SRyan Libby 
51214bd61e19SJeff Roberson static int
51224bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS)
51234bd61e19SJeff Roberson {
51244bd61e19SJeff Roberson 	uma_zone_t zone = arg1;
51254bd61e19SJeff Roberson 	uint64_t cur;
51264bd61e19SJeff Roberson 
51274bd61e19SJeff Roberson 	cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items));
51284bd61e19SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
51294bd61e19SJeff Roberson }
51304bd61e19SJeff Roberson 
51319542ea7bSGleb Smirnoff #ifdef INVARIANTS
51329542ea7bSGleb Smirnoff static uma_slab_t
51339542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item)
51349542ea7bSGleb Smirnoff {
51359542ea7bSGleb Smirnoff 	uma_slab_t slab;
51369542ea7bSGleb Smirnoff 	uma_keg_t keg;
51379542ea7bSGleb Smirnoff 	uint8_t *mem;
51389542ea7bSGleb Smirnoff 
51399542ea7bSGleb Smirnoff 	/*
51409542ea7bSGleb Smirnoff 	 * It is safe to return the slab here even though the
51419542ea7bSGleb Smirnoff 	 * zone is unlocked because the item's allocation state
51429542ea7bSGleb Smirnoff 	 * essentially holds a reference.
51439542ea7bSGleb Smirnoff 	 */
5144727c6918SJeff Roberson 	mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
5145727c6918SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5146bb15d1c7SGleb Smirnoff 		return (NULL);
514754c5ae80SRyan Libby 	if (zone->uz_flags & UMA_ZFLAG_VTOSLAB)
5148727c6918SJeff Roberson 		return (vtoslab((vm_offset_t)mem));
5149bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
515054c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0)
5151727c6918SJeff Roberson 		return ((uma_slab_t)(mem + keg->uk_pgoff));
51528b987a77SJeff Roberson 	KEG_LOCK(keg, 0);
51539542ea7bSGleb Smirnoff 	slab = hash_sfind(&keg->uk_hash, mem);
51548b987a77SJeff Roberson 	KEG_UNLOCK(keg, 0);
51559542ea7bSGleb Smirnoff 
51569542ea7bSGleb Smirnoff 	return (slab);
51579542ea7bSGleb Smirnoff }
51589542ea7bSGleb Smirnoff 
5159c5deaf04SGleb Smirnoff static bool
5160c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem)
5161c5deaf04SGleb Smirnoff {
5162c5deaf04SGleb Smirnoff 
5163727c6918SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5164c5deaf04SGleb Smirnoff 		return (true);
5165c5deaf04SGleb Smirnoff 
5166bb15d1c7SGleb Smirnoff 	return (uma_dbg_kskip(zone->uz_keg, mem));
5167c5deaf04SGleb Smirnoff }
5168c5deaf04SGleb Smirnoff 
5169c5deaf04SGleb Smirnoff static bool
5170c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem)
5171c5deaf04SGleb Smirnoff {
5172c5deaf04SGleb Smirnoff 	uintptr_t idx;
5173c5deaf04SGleb Smirnoff 
5174c5deaf04SGleb Smirnoff 	if (dbg_divisor == 0)
5175c5deaf04SGleb Smirnoff 		return (true);
5176c5deaf04SGleb Smirnoff 
5177c5deaf04SGleb Smirnoff 	if (dbg_divisor == 1)
5178c5deaf04SGleb Smirnoff 		return (false);
5179c5deaf04SGleb Smirnoff 
5180c5deaf04SGleb Smirnoff 	idx = (uintptr_t)mem >> PAGE_SHIFT;
5181c5deaf04SGleb Smirnoff 	if (keg->uk_ipers > 1) {
5182c5deaf04SGleb Smirnoff 		idx *= keg->uk_ipers;
5183c5deaf04SGleb Smirnoff 		idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize;
5184c5deaf04SGleb Smirnoff 	}
5185c5deaf04SGleb Smirnoff 
5186c5deaf04SGleb Smirnoff 	if ((idx / dbg_divisor) * dbg_divisor != idx) {
5187c5deaf04SGleb Smirnoff 		counter_u64_add(uma_skip_cnt, 1);
5188c5deaf04SGleb Smirnoff 		return (true);
5189c5deaf04SGleb Smirnoff 	}
5190c5deaf04SGleb Smirnoff 	counter_u64_add(uma_dbg_cnt, 1);
5191c5deaf04SGleb Smirnoff 
5192c5deaf04SGleb Smirnoff 	return (false);
5193c5deaf04SGleb Smirnoff }
5194c5deaf04SGleb Smirnoff 
51959542ea7bSGleb Smirnoff /*
51969542ea7bSGleb Smirnoff  * Set up the slab's freei data such that uma_dbg_free can function.
51979542ea7bSGleb Smirnoff  *
51989542ea7bSGleb Smirnoff  */
51999542ea7bSGleb Smirnoff static void
52009542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
52019542ea7bSGleb Smirnoff {
52029542ea7bSGleb Smirnoff 	uma_keg_t keg;
52039542ea7bSGleb Smirnoff 	int freei;
52049542ea7bSGleb Smirnoff 
52059542ea7bSGleb Smirnoff 	if (slab == NULL) {
52069542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
52079542ea7bSGleb Smirnoff 		if (slab == NULL)
52089542ea7bSGleb Smirnoff 			panic("uma: item %p did not belong to zone %s\n",
52099542ea7bSGleb Smirnoff 			    item, zone->uz_name);
52109542ea7bSGleb Smirnoff 	}
5211584061b4SJeff Roberson 	keg = zone->uz_keg;
52121e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
52139542ea7bSGleb Smirnoff 
5214815db204SRyan Libby 	if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)))
52159542ea7bSGleb Smirnoff 		panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
52169542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
5217815db204SRyan Libby 	BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg));
52189542ea7bSGleb Smirnoff }
52199542ea7bSGleb Smirnoff 
52209542ea7bSGleb Smirnoff /*
52219542ea7bSGleb Smirnoff  * Verifies freed addresses.  Checks for alignment, valid slab membership
52229542ea7bSGleb Smirnoff  * and duplicate frees.
52239542ea7bSGleb Smirnoff  *
52249542ea7bSGleb Smirnoff  */
52259542ea7bSGleb Smirnoff static void
52269542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
52279542ea7bSGleb Smirnoff {
52289542ea7bSGleb Smirnoff 	uma_keg_t keg;
52299542ea7bSGleb Smirnoff 	int freei;
52309542ea7bSGleb Smirnoff 
52319542ea7bSGleb Smirnoff 	if (slab == NULL) {
52329542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
52339542ea7bSGleb Smirnoff 		if (slab == NULL)
52349542ea7bSGleb Smirnoff 			panic("uma: Freed item %p did not belong to zone %s\n",
52359542ea7bSGleb Smirnoff 			    item, zone->uz_name);
52369542ea7bSGleb Smirnoff 	}
5237584061b4SJeff Roberson 	keg = zone->uz_keg;
52381e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
52399542ea7bSGleb Smirnoff 
52409542ea7bSGleb Smirnoff 	if (freei >= keg->uk_ipers)
52419542ea7bSGleb Smirnoff 		panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
52429542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
52439542ea7bSGleb Smirnoff 
52441e0701e1SJeff Roberson 	if (slab_item(slab, keg, freei) != item)
52459542ea7bSGleb Smirnoff 		panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
52469542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
52479542ea7bSGleb Smirnoff 
5248815db204SRyan Libby 	if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)))
52499542ea7bSGleb Smirnoff 		panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
52509542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
52519542ea7bSGleb Smirnoff 
5252815db204SRyan Libby 	BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg));
52539542ea7bSGleb Smirnoff }
52549542ea7bSGleb Smirnoff #endif /* INVARIANTS */
52559542ea7bSGleb Smirnoff 
525648c5777eSRobert Watson #ifdef DDB
525746d70077SConrad Meyer static int64_t
525846d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used,
52590223790fSConrad Meyer     uint64_t *sleeps, long *cachefree, uint64_t *xdomain)
526048c5777eSRobert Watson {
526146d70077SConrad Meyer 	uint64_t frees;
52620f9b7bf3SMark Johnston 	int i;
526348c5777eSRobert Watson 
526448c5777eSRobert Watson 	if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
526546d70077SConrad Meyer 		*allocs = counter_u64_fetch(z->uz_allocs);
52662efcc8cbSGleb Smirnoff 		frees = counter_u64_fetch(z->uz_frees);
526746d70077SConrad Meyer 		*sleeps = z->uz_sleeps;
526846d70077SConrad Meyer 		*cachefree = 0;
526946d70077SConrad Meyer 		*xdomain = 0;
527048c5777eSRobert Watson 	} else
527146d70077SConrad Meyer 		uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps,
527246d70077SConrad Meyer 		    xdomain);
52738b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
52748b987a77SJeff Roberson 		*cachefree += z->uz_domain[i].uzd_nitems;
5275e20a199fSJeff Roberson 		if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
527648c5777eSRobert Watson 		    (LIST_FIRST(&kz->uk_zones) != z)))
52774ab3aee8SMark Johnston 			*cachefree += kz->uk_domain[i].ud_free_items;
52788b987a77SJeff Roberson 	}
527946d70077SConrad Meyer 	*used = *allocs - frees;
528046d70077SConrad Meyer 	return (((int64_t)*used + *cachefree) * kz->uk_size);
528146d70077SConrad Meyer }
52820f9b7bf3SMark Johnston 
528346d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma)
528446d70077SConrad Meyer {
528546d70077SConrad Meyer 	const char *fmt_hdr, *fmt_entry;
528646d70077SConrad Meyer 	uma_keg_t kz;
528746d70077SConrad Meyer 	uma_zone_t z;
528846d70077SConrad Meyer 	uint64_t allocs, used, sleeps, xdomain;
528946d70077SConrad Meyer 	long cachefree;
529046d70077SConrad Meyer 	/* variables for sorting */
529146d70077SConrad Meyer 	uma_keg_t cur_keg;
529246d70077SConrad Meyer 	uma_zone_t cur_zone, last_zone;
529346d70077SConrad Meyer 	int64_t cur_size, last_size, size;
529446d70077SConrad Meyer 	int ties;
529546d70077SConrad Meyer 
529646d70077SConrad Meyer 	/* /i option produces machine-parseable CSV output */
529746d70077SConrad Meyer 	if (modif[0] == 'i') {
529846d70077SConrad Meyer 		fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n";
529946d70077SConrad Meyer 		fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n";
530046d70077SConrad Meyer 	} else {
530146d70077SConrad Meyer 		fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n";
530246d70077SConrad Meyer 		fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n";
530346d70077SConrad Meyer 	}
530446d70077SConrad Meyer 
530546d70077SConrad Meyer 	db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests",
530646d70077SConrad Meyer 	    "Sleeps", "Bucket", "Total Mem", "XFree");
530746d70077SConrad Meyer 
530846d70077SConrad Meyer 	/* Sort the zones with largest size first. */
530946d70077SConrad Meyer 	last_zone = NULL;
531046d70077SConrad Meyer 	last_size = INT64_MAX;
531146d70077SConrad Meyer 	for (;;) {
531246d70077SConrad Meyer 		cur_zone = NULL;
531346d70077SConrad Meyer 		cur_size = -1;
531446d70077SConrad Meyer 		ties = 0;
531546d70077SConrad Meyer 		LIST_FOREACH(kz, &uma_kegs, uk_link) {
531646d70077SConrad Meyer 			LIST_FOREACH(z, &kz->uk_zones, uz_link) {
531746d70077SConrad Meyer 				/*
531846d70077SConrad Meyer 				 * In the case of size ties, print out zones
531946d70077SConrad Meyer 				 * in the order they are encountered.  That is,
532046d70077SConrad Meyer 				 * when we encounter the most recently output
532146d70077SConrad Meyer 				 * zone, we have already printed all preceding
532246d70077SConrad Meyer 				 * ties, and we must print all following ties.
532346d70077SConrad Meyer 				 */
532446d70077SConrad Meyer 				if (z == last_zone) {
532546d70077SConrad Meyer 					ties = 1;
532646d70077SConrad Meyer 					continue;
532746d70077SConrad Meyer 				}
532846d70077SConrad Meyer 				size = get_uma_stats(kz, z, &allocs, &used,
532946d70077SConrad Meyer 				    &sleeps, &cachefree, &xdomain);
533046d70077SConrad Meyer 				if (size > cur_size && size < last_size + ties)
533146d70077SConrad Meyer 				{
533246d70077SConrad Meyer 					cur_size = size;
533346d70077SConrad Meyer 					cur_zone = z;
533446d70077SConrad Meyer 					cur_keg = kz;
533546d70077SConrad Meyer 				}
533646d70077SConrad Meyer 			}
533746d70077SConrad Meyer 		}
533846d70077SConrad Meyer 		if (cur_zone == NULL)
533946d70077SConrad Meyer 			break;
534046d70077SConrad Meyer 
534146d70077SConrad Meyer 		size = get_uma_stats(cur_keg, cur_zone, &allocs, &used,
534246d70077SConrad Meyer 		    &sleeps, &cachefree, &xdomain);
534346d70077SConrad Meyer 		db_printf(fmt_entry, cur_zone->uz_name,
534446d70077SConrad Meyer 		    (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree,
534546d70077SConrad Meyer 		    (uintmax_t)allocs, (uintmax_t)sleeps,
534620a4e154SJeff Roberson 		    (unsigned)cur_zone->uz_bucket_size, (intmax_t)size,
534720a4e154SJeff Roberson 		    xdomain);
534846d70077SConrad Meyer 
5349687c94aaSJohn Baldwin 		if (db_pager_quit)
5350687c94aaSJohn Baldwin 			return;
535146d70077SConrad Meyer 		last_zone = cur_zone;
535246d70077SConrad Meyer 		last_size = cur_size;
535348c5777eSRobert Watson 	}
535448c5777eSRobert Watson }
535503175483SAlexander Motin 
535603175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache)
535703175483SAlexander Motin {
535803175483SAlexander Motin 	uma_zone_t z;
5359ab3185d1SJeff Roberson 	uint64_t allocs, frees;
53600f9b7bf3SMark Johnston 	long cachefree;
53610f9b7bf3SMark Johnston 	int i;
536203175483SAlexander Motin 
536303175483SAlexander Motin 	db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
536403175483SAlexander Motin 	    "Requests", "Bucket");
536503175483SAlexander Motin 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
5366c1685086SJeff Roberson 		uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL);
53670f9b7bf3SMark Johnston 		for (i = 0; i < vm_ndomains; i++)
53680f9b7bf3SMark Johnston 			cachefree += z->uz_domain[i].uzd_nitems;
53690f9b7bf3SMark Johnston 		db_printf("%18s %8ju %8jd %8ld %12ju %8u\n",
537003175483SAlexander Motin 		    z->uz_name, (uintmax_t)z->uz_size,
537103175483SAlexander Motin 		    (intmax_t)(allocs - frees), cachefree,
537220a4e154SJeff Roberson 		    (uintmax_t)allocs, z->uz_bucket_size);
537303175483SAlexander Motin 		if (db_pager_quit)
537403175483SAlexander Motin 			return;
537503175483SAlexander Motin 	}
537603175483SAlexander Motin }
53779542ea7bSGleb Smirnoff #endif	/* DDB */
5378