xref: /freebsd/sys/vm/uma_core.c (revision d6e77cda9be1509ea170142cca3ff0d3b9f12e35)
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>
6109c8cb71SMark Johnston #include <sys/asan.h>
62ef72505eSJeff Roberson #include <sys/bitset.h>
63194a979eSMark Johnston #include <sys/domainset.h>
649b43bc27SAndriy Gapon #include <sys/eventhandler.h>
658355f576SJeff Roberson #include <sys/kernel.h>
668355f576SJeff Roberson #include <sys/types.h>
67ad5b0f5bSJeff Roberson #include <sys/limits.h>
688355f576SJeff Roberson #include <sys/queue.h>
698355f576SJeff Roberson #include <sys/malloc.h>
703659f747SRobert Watson #include <sys/ktr.h>
718355f576SJeff Roberson #include <sys/lock.h>
7210094910SMark Johnston #include <sys/msan.h>
738355f576SJeff Roberson #include <sys/mutex.h>
744c1cc01cSJohn Baldwin #include <sys/proc.h>
7510cb2424SMark Murray #include <sys/random.h>
7689f6b863SAttilio Rao #include <sys/rwlock.h>
777a52a97eSRobert Watson #include <sys/sbuf.h>
78a2de44abSAlexander Motin #include <sys/sched.h>
794bd61e19SJeff Roberson #include <sys/sleepqueue.h>
808355f576SJeff Roberson #include <sys/smp.h>
81d4665eaaSJeff Roberson #include <sys/smr.h>
8210094910SMark Johnston #include <sys/sysctl.h>
83e60b2fcbSGleb Smirnoff #include <sys/taskqueue.h>
8486bbae32SJeff Roberson #include <sys/vmmeter.h>
8586bbae32SJeff Roberson 
868355f576SJeff Roberson #include <vm/vm.h>
876f3b523cSKonstantin Belousov #include <vm/vm_param.h>
88194a979eSMark Johnston #include <vm/vm_domainset.h>
898355f576SJeff Roberson #include <vm/vm_object.h>
908355f576SJeff Roberson #include <vm/vm_page.h>
91a4915c21SAttilio Rao #include <vm/vm_pageout.h>
92ab3185d1SJeff Roberson #include <vm/vm_phys.h>
9330c5525bSAndrew Gallatin #include <vm/vm_pagequeue.h>
948355f576SJeff Roberson #include <vm/vm_map.h>
958355f576SJeff Roberson #include <vm/vm_kern.h>
968355f576SJeff Roberson #include <vm/vm_extern.h>
976f3b523cSKonstantin Belousov #include <vm/vm_dumpset.h>
988355f576SJeff Roberson #include <vm/uma.h>
998355f576SJeff Roberson #include <vm/uma_int.h>
100639c9550SJeff Roberson #include <vm/uma_dbg.h>
1018355f576SJeff Roberson 
10248c5777eSRobert Watson #include <ddb/ddb.h>
10348c5777eSRobert Watson 
1048d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
1058d689e04SGleb Smirnoff #include <vm/memguard.h>
1068d689e04SGleb Smirnoff #endif
1078d689e04SGleb Smirnoff 
108a81c400eSJeff Roberson #include <machine/md_var.h>
109a81c400eSJeff Roberson 
110d4665eaaSJeff Roberson #ifdef INVARIANTS
111d4665eaaSJeff Roberson #define	UMA_ALWAYS_CTORDTOR	1
112d4665eaaSJeff Roberson #else
113d4665eaaSJeff Roberson #define	UMA_ALWAYS_CTORDTOR	0
114d4665eaaSJeff Roberson #endif
115d4665eaaSJeff Roberson 
1168355f576SJeff Roberson /*
117ab3185d1SJeff Roberson  * This is the zone and keg from which all zones are spawned.
1188355f576SJeff Roberson  */
119ab3185d1SJeff Roberson static uma_zone_t kegs;
120ab3185d1SJeff Roberson static uma_zone_t zones;
1218355f576SJeff Roberson 
1229b8db4d0SRyan Libby /*
12354007ce8SMark Johnston  * On INVARIANTS builds, the slab contains a second bitset of the same size,
12454007ce8SMark Johnston  * "dbg_bits", which is laid out immediately after us_free.
12554007ce8SMark Johnston  */
12654007ce8SMark Johnston #ifdef INVARIANTS
12754007ce8SMark Johnston #define	SLAB_BITSETS	2
12854007ce8SMark Johnston #else
12954007ce8SMark Johnston #define	SLAB_BITSETS	1
13054007ce8SMark Johnston #endif
13154007ce8SMark Johnston 
13254007ce8SMark Johnston /*
1339b8db4d0SRyan Libby  * These are the two zones from which all offpage uma_slab_ts are allocated.
1349b8db4d0SRyan Libby  *
1359b8db4d0SRyan Libby  * One zone is for slab headers that can represent a larger number of items,
1369b8db4d0SRyan Libby  * making the slabs themselves more efficient, and the other zone is for
1379b8db4d0SRyan Libby  * headers that are smaller and represent fewer items, making the headers more
1389b8db4d0SRyan Libby  * efficient.
1399b8db4d0SRyan Libby  */
1409b8db4d0SRyan Libby #define	SLABZONE_SIZE(setsize)					\
1419b8db4d0SRyan Libby     (sizeof(struct uma_hash_slab) + BITSET_SIZE(setsize) * SLAB_BITSETS)
1429b8db4d0SRyan Libby #define	SLABZONE0_SETSIZE	(PAGE_SIZE / 16)
1439b8db4d0SRyan Libby #define	SLABZONE1_SETSIZE	SLAB_MAX_SETSIZE
1449b8db4d0SRyan Libby #define	SLABZONE0_SIZE	SLABZONE_SIZE(SLABZONE0_SETSIZE)
1459b8db4d0SRyan Libby #define	SLABZONE1_SIZE	SLABZONE_SIZE(SLABZONE1_SETSIZE)
1469b8db4d0SRyan Libby static uma_zone_t slabzones[2];
1478355f576SJeff Roberson 
1488355f576SJeff Roberson /*
1498355f576SJeff Roberson  * The initial hash tables come out of this zone so they can be allocated
1508355f576SJeff Roberson  * prior to malloc coming up.
1518355f576SJeff Roberson  */
1528355f576SJeff Roberson static uma_zone_t hashzone;
1538355f576SJeff Roberson 
1541e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */
155e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1;
1561e319f6dSRobert Watson 
157961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
15820a4e154SJeff Roberson static MALLOC_DEFINE(M_UMA, "UMA", "UMA Misc");
159961647dfSJeff Roberson 
1608355f576SJeff Roberson /*
16186bbae32SJeff Roberson  * Are we allowed to allocate buckets?
16286bbae32SJeff Roberson  */
16386bbae32SJeff Roberson static int bucketdisable = 1;
16486bbae32SJeff Roberson 
165099a0e58SBosko Milekic /* Linked list of all kegs in the system */
16613e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
1678355f576SJeff Roberson 
16803175483SAlexander Motin /* Linked list of all cache-only zones in the system */
16903175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones =
17003175483SAlexander Motin     LIST_HEAD_INITIALIZER(uma_cachezones);
17103175483SAlexander Motin 
172aabe13f1SMark Johnston /*
173aabe13f1SMark Johnston  * Mutex for global lists: uma_kegs, uma_cachezones, and the per-keg list of
174aabe13f1SMark Johnston  * zones.
175aabe13f1SMark Johnston  */
176fe933c1dSMateusz Guzik static struct rwlock_padalign __exclusive_cache_line uma_rwlock;
1778355f576SJeff Roberson 
178aabe13f1SMark Johnston static struct sx uma_reclaim_lock;
179aabe13f1SMark Johnston 
180ac0a6fd0SGleb Smirnoff /*
181a81c400eSJeff Roberson  * First available virual address for boot time allocations.
182ac0a6fd0SGleb Smirnoff  */
183a81c400eSJeff Roberson static vm_offset_t bootstart;
184a81c400eSJeff Roberson static vm_offset_t bootmem;
1858355f576SJeff Roberson 
186fbd95859SMark Johnston /*
187fbd95859SMark Johnston  * kmem soft limit, initialized by uma_set_limit().  Ensure that early
188fbd95859SMark Johnston  * allocations don't trigger a wakeup of the reclaim thread.
189fbd95859SMark Johnston  */
1906d6a03d7SJeff Roberson unsigned long uma_kmem_limit = LONG_MAX;
191fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_limit, CTLFLAG_RD, &uma_kmem_limit, 0,
192fbd95859SMark Johnston     "UMA kernel memory soft limit");
1936d6a03d7SJeff Roberson unsigned long uma_kmem_total;
194fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_total, CTLFLAG_RD, &uma_kmem_total, 0,
195fbd95859SMark Johnston     "UMA kernel memory usage");
1962e47807cSJeff Roberson 
1978355f576SJeff Roberson /* Is the VM done starting up? */
198860bb7a0SMark Johnston static enum {
199860bb7a0SMark Johnston 	BOOT_COLD,
200a81c400eSJeff Roberson 	BOOT_KVA,
201dc2b3205SMark Johnston 	BOOT_PCPU,
202860bb7a0SMark Johnston 	BOOT_RUNNING,
203860bb7a0SMark Johnston 	BOOT_SHUTDOWN,
204860bb7a0SMark Johnston } booted = BOOT_COLD;
2058355f576SJeff Roberson 
206ef72505eSJeff Roberson /*
2079643769aSJeff Roberson  * This is the handle used to schedule events that need to happen
2089643769aSJeff Roberson  * outside of the allocation fast path.
2099643769aSJeff Roberson  */
2108355f576SJeff Roberson static struct callout uma_callout;
2119643769aSJeff Roberson #define	UMA_TIMEOUT	20		/* Seconds for callout interval. */
2128355f576SJeff Roberson 
2138355f576SJeff Roberson /*
2148355f576SJeff Roberson  * This structure is passed as the zone ctor arg so that I don't have to create
2158355f576SJeff Roberson  * a special allocation function just for zones.
2168355f576SJeff Roberson  */
2178355f576SJeff Roberson struct uma_zctor_args {
218bb196eb4SMatthew D Fleming 	const char *name;
219c3bdc05fSAndrew R. Reiter 	size_t size;
2208355f576SJeff Roberson 	uma_ctor ctor;
2218355f576SJeff Roberson 	uma_dtor dtor;
2228355f576SJeff Roberson 	uma_init uminit;
2238355f576SJeff Roberson 	uma_fini fini;
2240095a784SJeff Roberson 	uma_import import;
2250095a784SJeff Roberson 	uma_release release;
2260095a784SJeff Roberson 	void *arg;
227099a0e58SBosko Milekic 	uma_keg_t keg;
228099a0e58SBosko Milekic 	int align;
22985dcf349SGleb Smirnoff 	uint32_t flags;
230099a0e58SBosko Milekic };
231099a0e58SBosko Milekic 
232099a0e58SBosko Milekic struct uma_kctor_args {
233099a0e58SBosko Milekic 	uma_zone_t zone;
234099a0e58SBosko Milekic 	size_t size;
235099a0e58SBosko Milekic 	uma_init uminit;
236099a0e58SBosko Milekic 	uma_fini fini;
2378355f576SJeff Roberson 	int align;
23885dcf349SGleb Smirnoff 	uint32_t flags;
2398355f576SJeff Roberson };
2408355f576SJeff Roberson 
241cae33c14SJeff Roberson struct uma_bucket_zone {
242cae33c14SJeff Roberson 	uma_zone_t	ubz_zone;
243eaa17d42SRyan Libby 	const char	*ubz_name;
244fc03d22bSJeff Roberson 	int		ubz_entries;	/* Number of items it can hold. */
245fc03d22bSJeff Roberson 	int		ubz_maxsize;	/* Maximum allocation size per-item. */
246cae33c14SJeff Roberson };
247cae33c14SJeff Roberson 
248f9d27e75SRobert Watson /*
249fc03d22bSJeff Roberson  * Compute the actual number of bucket entries to pack them in power
250fc03d22bSJeff Roberson  * of two sizes for more efficient space utilization.
251f9d27e75SRobert Watson  */
252fc03d22bSJeff Roberson #define	BUCKET_SIZE(n)						\
253fc03d22bSJeff Roberson     (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *))
254fc03d22bSJeff Roberson 
2551aa6c758SAlexander Motin #define	BUCKET_MAX	BUCKET_SIZE(256)
256fc03d22bSJeff Roberson 
257fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = {
258e84130a0SJeff Roberson 	/* Literal bucket sizes. */
259e84130a0SJeff Roberson 	{ NULL, "2 Bucket", 2, 4096 },
260e84130a0SJeff Roberson 	{ NULL, "4 Bucket", 4, 3072 },
261e84130a0SJeff Roberson 	{ NULL, "8 Bucket", 8, 2048 },
262e84130a0SJeff Roberson 	{ NULL, "16 Bucket", 16, 1024 },
263e84130a0SJeff Roberson 	/* Rounded down power of 2 sizes for efficiency. */
264fc03d22bSJeff Roberson 	{ NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
265fc03d22bSJeff Roberson 	{ NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
266fc03d22bSJeff Roberson 	{ NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
2671aa6c758SAlexander Motin 	{ NULL, "256 Bucket", BUCKET_SIZE(256), 64 },
268fc03d22bSJeff Roberson 	{ NULL, NULL, 0}
269fc03d22bSJeff Roberson };
270cae33c14SJeff Roberson 
2712019094aSRobert Watson /*
2722019094aSRobert Watson  * Flags and enumerations to be passed to internal functions.
2732019094aSRobert Watson  */
274bb15d1c7SGleb Smirnoff enum zfreeskip {
275bb15d1c7SGleb Smirnoff 	SKIP_NONE =	0,
276bb15d1c7SGleb Smirnoff 	SKIP_CNT =	0x00000001,
277bb15d1c7SGleb Smirnoff 	SKIP_DTOR =	0x00010000,
278bb15d1c7SGleb Smirnoff 	SKIP_FINI =	0x00020000,
279bb15d1c7SGleb Smirnoff };
280b23f72e9SBrian Feldman 
2818355f576SJeff Roberson /* Prototypes.. */
2828355f576SJeff Roberson 
283a81c400eSJeff Roberson void	uma_startup1(vm_offset_t);
284f4bef67cSGleb Smirnoff void	uma_startup2(void);
285f4bef67cSGleb Smirnoff 
286ab3185d1SJeff Roberson static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
287ab3185d1SJeff Roberson static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
288ab3059a8SMatt Macy static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
289ab3185d1SJeff Roberson static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
290ec0d8280SRyan Libby static void *contig_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
291f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t);
292ab3059a8SMatt Macy static void pcpu_page_free(void *, vm_size_t, uint8_t);
29386220393SMark Johnston static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int);
2949643769aSJeff Roberson static void cache_drain(uma_zone_t);
2958355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t);
296aabe13f1SMark Johnston static void bucket_cache_reclaim(uma_zone_t zone, bool, int);
2972760658bSAlexander Motin static bool bucket_cache_reclaim_domain(uma_zone_t, bool, bool, int);
298b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int);
299099a0e58SBosko Milekic static void keg_dtor(void *, int, void *);
3002760658bSAlexander Motin static void keg_drain(uma_keg_t keg, int domain);
301b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int);
3029c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *);
303d4665eaaSJeff Roberson static inline void item_dtor(uma_zone_t zone, void *item, int size,
304d4665eaaSJeff Roberson     void *udata, enum zfreeskip skip);
305b23f72e9SBrian Feldman static int zero_init(void *, int, int);
306c6fd3e23SJeff Roberson static void zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata,
307c6fd3e23SJeff Roberson     int itemdomain, bool ws);
30820a4e154SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t, void *), void *);
309a81c400eSJeff Roberson static void zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *), void *);
31020a4e154SJeff Roberson static void zone_timeout(uma_zone_t zone, void *);
3113b2f2cb8SAlexander Motin static int hash_alloc(struct uma_hash *, u_int);
3120aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *);
3130aef6126SJeff Roberson static void hash_free(struct uma_hash *hash);
3148355f576SJeff Roberson static void uma_timeout(void *);
315860bb7a0SMark Johnston static void uma_shutdown(void);
316ab3185d1SJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int, int);
3170095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
3184bd61e19SJeff Roberson static int zone_alloc_limit(uma_zone_t zone, int count, int flags);
3194bd61e19SJeff Roberson static void zone_free_limit(uma_zone_t zone, int count);
32086bbae32SJeff Roberson static void bucket_enable(void);
321cae33c14SJeff Roberson static void bucket_init(void);
3226fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int);
3236fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *);
324aabe13f1SMark Johnston static void bucket_zone_drain(int domain);
325beb8beefSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int);
3260095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
327bb15d1c7SGleb Smirnoff static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item);
32809c8cb71SMark Johnston static size_t slab_sizeof(int nitems);
329e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
33085dcf349SGleb Smirnoff     uma_fini fini, int align, uint32_t flags);
331b75c4efcSAndrew Turner static int zone_import(void *, void **, int, int, int);
332b75c4efcSAndrew Turner static void zone_release(void *, void **, int);
333beb8beefSJeff Roberson static bool cache_alloc(uma_zone_t, uma_cache_t, void *, int);
3340a81b439SJeff Roberson static bool cache_free(uma_zone_t, uma_cache_t, void *, void *, int);
335bbee39c6SJeff Roberson 
3367a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
3377a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
33820a4e154SJeff Roberson static int sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS);
33920a4e154SJeff Roberson static int sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS);
3406d204a6aSRyan Libby static int sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS);
341f7af5015SRyan Libby static int sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS);
3424bd61e19SJeff Roberson static int sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS);
3438355f576SJeff Roberson 
34431c251a0SJeff Roberson static uint64_t uma_zone_get_allocs(uma_zone_t zone);
34531c251a0SJeff Roberson 
3467029da5cSPawel Biernacki static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
34733e5a1eaSRyan Libby     "Memory allocation debugging");
34833e5a1eaSRyan Libby 
3499542ea7bSGleb Smirnoff #ifdef INVARIANTS
35031c251a0SJeff Roberson static uint64_t uma_keg_get_allocs(uma_keg_t zone);
351815db204SRyan Libby static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg);
352815db204SRyan Libby 
353c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem);
354c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem);
3559542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item);
3569542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item);
357c5deaf04SGleb Smirnoff 
358c5deaf04SGleb Smirnoff static u_int dbg_divisor = 1;
359c5deaf04SGleb Smirnoff SYSCTL_UINT(_vm_debug, OID_AUTO, divisor,
360c5deaf04SGleb Smirnoff     CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0,
361c5deaf04SGleb Smirnoff     "Debug & thrash every this item in memory allocator");
362c5deaf04SGleb Smirnoff 
363c5deaf04SGleb Smirnoff static counter_u64_t uma_dbg_cnt = EARLY_COUNTER;
364c5deaf04SGleb Smirnoff static counter_u64_t uma_skip_cnt = EARLY_COUNTER;
365c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD,
366c5deaf04SGleb Smirnoff     &uma_dbg_cnt, "memory items debugged");
367c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD,
368c5deaf04SGleb Smirnoff     &uma_skip_cnt, "memory items skipped, not debugged");
3699542ea7bSGleb Smirnoff #endif
3709542ea7bSGleb Smirnoff 
3717029da5cSPawel Biernacki SYSCTL_NODE(_vm, OID_AUTO, uma, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3727029da5cSPawel Biernacki     "Universal Memory Allocator");
37335ec24f3SRyan Libby 
374a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_INT,
3757a52a97eSRobert Watson     0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
3767a52a97eSRobert Watson 
377a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_STRUCT,
3787a52a97eSRobert Watson     0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
3797a52a97eSRobert Watson 
3802f891cd5SPawel Jakub Dawidek static int zone_warnings = 1;
381af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0,
3822f891cd5SPawel Jakub Dawidek     "Warn when UMA zones becomes full");
3832f891cd5SPawel Jakub Dawidek 
38433e5a1eaSRyan Libby static int multipage_slabs = 1;
38533e5a1eaSRyan Libby TUNABLE_INT("vm.debug.uma_multipage_slabs", &multipage_slabs);
38633e5a1eaSRyan Libby SYSCTL_INT(_vm_debug, OID_AUTO, uma_multipage_slabs,
38733e5a1eaSRyan Libby     CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &multipage_slabs, 0,
38833e5a1eaSRyan Libby     "UMA may choose larger slab sizes for better efficiency");
38933e5a1eaSRyan Libby 
39086bbae32SJeff Roberson /*
3919b8db4d0SRyan Libby  * Select the slab zone for an offpage slab with the given maximum item count.
3929b8db4d0SRyan Libby  */
3939b8db4d0SRyan Libby static inline uma_zone_t
3949b8db4d0SRyan Libby slabzone(int ipers)
3959b8db4d0SRyan Libby {
3969b8db4d0SRyan Libby 
3979b8db4d0SRyan Libby 	return (slabzones[ipers > SLABZONE0_SETSIZE]);
3989b8db4d0SRyan Libby }
3999b8db4d0SRyan Libby 
4009b8db4d0SRyan Libby /*
40186bbae32SJeff Roberson  * This routine checks to see whether or not it's safe to enable buckets.
40286bbae32SJeff Roberson  */
40386bbae32SJeff Roberson static void
40486bbae32SJeff Roberson bucket_enable(void)
40586bbae32SJeff Roberson {
4063182660aSRyan Libby 
407a81c400eSJeff Roberson 	KASSERT(booted >= BOOT_KVA, ("Bucket enable before init"));
408251386b4SMaksim Yevmenkin 	bucketdisable = vm_page_count_min();
40986bbae32SJeff Roberson }
41086bbae32SJeff Roberson 
411dc2c7965SRobert Watson /*
412dc2c7965SRobert Watson  * Initialize bucket_zones, the array of zones of buckets of various sizes.
413dc2c7965SRobert Watson  *
414dc2c7965SRobert Watson  * For each zone, calculate the memory required for each bucket, consisting
415fc03d22bSJeff Roberson  * of the header and an array of pointers.
416dc2c7965SRobert Watson  */
417cae33c14SJeff Roberson static void
418cae33c14SJeff Roberson bucket_init(void)
419cae33c14SJeff Roberson {
420cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
421cae33c14SJeff Roberson 	int size;
422cae33c14SJeff Roberson 
423d74e6a1dSAlan Cox 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
424cae33c14SJeff Roberson 		size = roundup(sizeof(struct uma_bucket), sizeof(void *));
425cae33c14SJeff Roberson 		size += sizeof(void *) * ubz->ubz_entries;
426cae33c14SJeff Roberson 		ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
427e20a199fSJeff Roberson 		    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
428dfe13344SJeff Roberson 		    UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET |
429dfe13344SJeff Roberson 		    UMA_ZONE_FIRSTTOUCH);
430cae33c14SJeff Roberson 	}
431cae33c14SJeff Roberson }
432cae33c14SJeff Roberson 
433dc2c7965SRobert Watson /*
434dc2c7965SRobert Watson  * Given a desired number of entries for a bucket, return the zone from which
435dc2c7965SRobert Watson  * to allocate the bucket.
436dc2c7965SRobert Watson  */
437dc2c7965SRobert Watson static struct uma_bucket_zone *
438dc2c7965SRobert Watson bucket_zone_lookup(int entries)
439dc2c7965SRobert Watson {
440fc03d22bSJeff Roberson 	struct uma_bucket_zone *ubz;
441dc2c7965SRobert Watson 
442fc03d22bSJeff Roberson 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
443fc03d22bSJeff Roberson 		if (ubz->ubz_entries >= entries)
444fc03d22bSJeff Roberson 			return (ubz);
445fc03d22bSJeff Roberson 	ubz--;
446fc03d22bSJeff Roberson 	return (ubz);
447fc03d22bSJeff Roberson }
448fc03d22bSJeff Roberson 
449fc03d22bSJeff Roberson static int
450fc03d22bSJeff Roberson bucket_select(int size)
451fc03d22bSJeff Roberson {
452fc03d22bSJeff Roberson 	struct uma_bucket_zone *ubz;
453fc03d22bSJeff Roberson 
454fc03d22bSJeff Roberson 	ubz = &bucket_zones[0];
455fc03d22bSJeff Roberson 	if (size > ubz->ubz_maxsize)
456fc03d22bSJeff Roberson 		return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
457fc03d22bSJeff Roberson 
458fc03d22bSJeff Roberson 	for (; ubz->ubz_entries != 0; ubz++)
459fc03d22bSJeff Roberson 		if (ubz->ubz_maxsize < size)
460fc03d22bSJeff Roberson 			break;
461fc03d22bSJeff Roberson 	ubz--;
462fc03d22bSJeff Roberson 	return (ubz->ubz_entries);
463dc2c7965SRobert Watson }
464dc2c7965SRobert Watson 
465cae33c14SJeff Roberson static uma_bucket_t
4666fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags)
467cae33c14SJeff Roberson {
468cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
469cae33c14SJeff Roberson 	uma_bucket_t bucket;
470cae33c14SJeff Roberson 
471cae33c14SJeff Roberson 	/*
472d4665eaaSJeff Roberson 	 * Don't allocate buckets early in boot.
473cae33c14SJeff Roberson 	 */
474d4665eaaSJeff Roberson 	if (__predict_false(booted < BOOT_KVA))
475cae33c14SJeff Roberson 		return (NULL);
476a81c400eSJeff Roberson 
4776fd34d6fSJeff Roberson 	/*
4786fd34d6fSJeff Roberson 	 * To limit bucket recursion we store the original zone flags
4796fd34d6fSJeff Roberson 	 * in a cookie passed via zalloc_arg/zfree_arg.  This allows the
4806fd34d6fSJeff Roberson 	 * NOVM flag to persist even through deep recursions.  We also
4816fd34d6fSJeff Roberson 	 * store ZFLAG_BUCKET once we have recursed attempting to allocate
4826fd34d6fSJeff Roberson 	 * a bucket for a bucket zone so we do not allow infinite bucket
4836fd34d6fSJeff Roberson 	 * recursion.  This cookie will even persist to frees of unused
4846fd34d6fSJeff Roberson 	 * buckets via the allocation path or bucket allocations in the
4856fd34d6fSJeff Roberson 	 * free path.
4866fd34d6fSJeff Roberson 	 */
4876fd34d6fSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
4886fd34d6fSJeff Roberson 		udata = (void *)(uintptr_t)zone->uz_flags;
489e8a720feSAlexander Motin 	else {
490e8a720feSAlexander Motin 		if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
491e8a720feSAlexander Motin 			return (NULL);
4926fd34d6fSJeff Roberson 		udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
493e8a720feSAlexander Motin 	}
494bae55c4aSRyan Libby 	if (((uintptr_t)udata & UMA_ZONE_VM) != 0)
495af526374SJeff Roberson 		flags |= M_NOVM;
496f8b6c515SMark Johnston 	ubz = bucket_zone_lookup(atomic_load_16(&zone->uz_bucket_size));
49720d3ab87SAlexander Motin 	if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0)
49820d3ab87SAlexander Motin 		ubz++;
4996fd34d6fSJeff Roberson 	bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
500cae33c14SJeff Roberson 	if (bucket) {
501cae33c14SJeff Roberson #ifdef INVARIANTS
502cae33c14SJeff Roberson 		bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
503cae33c14SJeff Roberson #endif
504cae33c14SJeff Roberson 		bucket->ub_cnt = 0;
505f8b6c515SMark Johnston 		bucket->ub_entries = min(ubz->ubz_entries,
506f8b6c515SMark Johnston 		    zone->uz_bucket_size_max);
507d4665eaaSJeff Roberson 		bucket->ub_seq = SMR_SEQ_INVALID;
508d4665eaaSJeff Roberson 		CTR3(KTR_UMA, "bucket_alloc: zone %s(%p) allocated bucket %p",
509d4665eaaSJeff Roberson 		    zone->uz_name, zone, bucket);
510cae33c14SJeff Roberson 	}
511cae33c14SJeff Roberson 
512cae33c14SJeff Roberson 	return (bucket);
513cae33c14SJeff Roberson }
514cae33c14SJeff Roberson 
515cae33c14SJeff Roberson static void
5166fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
517cae33c14SJeff Roberson {
518cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
519cae33c14SJeff Roberson 
520c6fd3e23SJeff Roberson 	if (bucket->ub_cnt != 0)
521c6fd3e23SJeff Roberson 		bucket_drain(zone, bucket);
522c6fd3e23SJeff Roberson 
523fc03d22bSJeff Roberson 	KASSERT(bucket->ub_cnt == 0,
524fc03d22bSJeff Roberson 	    ("bucket_free: Freeing a non free bucket."));
525d4665eaaSJeff Roberson 	KASSERT(bucket->ub_seq == SMR_SEQ_INVALID,
526d4665eaaSJeff Roberson 	    ("bucket_free: Freeing an SMR bucket."));
5276fd34d6fSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
5286fd34d6fSJeff Roberson 		udata = (void *)(uintptr_t)zone->uz_flags;
529dc2c7965SRobert Watson 	ubz = bucket_zone_lookup(bucket->ub_entries);
5306fd34d6fSJeff Roberson 	uma_zfree_arg(ubz->ubz_zone, bucket, udata);
531cae33c14SJeff Roberson }
532cae33c14SJeff Roberson 
533cae33c14SJeff Roberson static void
534aabe13f1SMark Johnston bucket_zone_drain(int domain)
535cae33c14SJeff Roberson {
536cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
537cae33c14SJeff Roberson 
538cae33c14SJeff Roberson 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
539aabe13f1SMark Johnston 		uma_zone_reclaim_domain(ubz->ubz_zone, UMA_RECLAIM_DRAIN,
540aabe13f1SMark Johnston 		    domain);
541cae33c14SJeff Roberson }
542cae33c14SJeff Roberson 
54309c8cb71SMark Johnston #ifdef KASAN
5449a7c2de3SMark Johnston _Static_assert(UMA_SMALLEST_UNIT % KASAN_SHADOW_SCALE == 0,
5459a7c2de3SMark Johnston     "Base UMA allocation size not a multiple of the KASAN scale factor");
5469a7c2de3SMark Johnston 
54709c8cb71SMark Johnston static void
54809c8cb71SMark Johnston kasan_mark_item_valid(uma_zone_t zone, void *item)
54909c8cb71SMark Johnston {
55009c8cb71SMark Johnston 	void *pcpu_item;
55109c8cb71SMark Johnston 	size_t sz, rsz;
55209c8cb71SMark Johnston 	int i;
55309c8cb71SMark Johnston 
55409c8cb71SMark Johnston 	if ((zone->uz_flags & UMA_ZONE_NOKASAN) != 0)
55509c8cb71SMark Johnston 		return;
55609c8cb71SMark Johnston 
55709c8cb71SMark Johnston 	sz = zone->uz_size;
55809c8cb71SMark Johnston 	rsz = roundup2(sz, KASAN_SHADOW_SCALE);
55909c8cb71SMark Johnston 	if ((zone->uz_flags & UMA_ZONE_PCPU) == 0) {
560b0dfc486SMark Johnston 		kasan_mark(item, sz, rsz, KASAN_GENERIC_REDZONE);
56109c8cb71SMark Johnston 	} else {
56209c8cb71SMark Johnston 		pcpu_item = zpcpu_base_to_offset(item);
56309c8cb71SMark Johnston 		for (i = 0; i <= mp_maxid; i++)
564b0dfc486SMark Johnston 			kasan_mark(zpcpu_get_cpu(pcpu_item, i), sz, rsz,
565b0dfc486SMark Johnston 			    KASAN_GENERIC_REDZONE);
56609c8cb71SMark Johnston 	}
56709c8cb71SMark Johnston }
56809c8cb71SMark Johnston 
56909c8cb71SMark Johnston static void
57009c8cb71SMark Johnston kasan_mark_item_invalid(uma_zone_t zone, void *item)
57109c8cb71SMark Johnston {
57209c8cb71SMark Johnston 	void *pcpu_item;
57309c8cb71SMark Johnston 	size_t sz;
57409c8cb71SMark Johnston 	int i;
57509c8cb71SMark Johnston 
57609c8cb71SMark Johnston 	if ((zone->uz_flags & UMA_ZONE_NOKASAN) != 0)
57709c8cb71SMark Johnston 		return;
57809c8cb71SMark Johnston 
57909c8cb71SMark Johnston 	sz = roundup2(zone->uz_size, KASAN_SHADOW_SCALE);
58009c8cb71SMark Johnston 	if ((zone->uz_flags & UMA_ZONE_PCPU) == 0) {
58109c8cb71SMark Johnston 		kasan_mark(item, 0, sz, KASAN_UMA_FREED);
58209c8cb71SMark Johnston 	} else {
58309c8cb71SMark Johnston 		pcpu_item = zpcpu_base_to_offset(item);
58409c8cb71SMark Johnston 		for (i = 0; i <= mp_maxid; i++)
585b0dfc486SMark Johnston 			kasan_mark(zpcpu_get_cpu(pcpu_item, i), 0, sz,
586b0dfc486SMark Johnston 			    KASAN_UMA_FREED);
58709c8cb71SMark Johnston 	}
58809c8cb71SMark Johnston }
58909c8cb71SMark Johnston 
59009c8cb71SMark Johnston static void
59109c8cb71SMark Johnston kasan_mark_slab_valid(uma_keg_t keg, void *mem)
59209c8cb71SMark Johnston {
59309c8cb71SMark Johnston 	size_t sz;
59409c8cb71SMark Johnston 
59509c8cb71SMark Johnston 	if ((keg->uk_flags & UMA_ZONE_NOKASAN) == 0) {
59609c8cb71SMark Johnston 		sz = keg->uk_ppera * PAGE_SIZE;
59709c8cb71SMark Johnston 		kasan_mark(mem, sz, sz, 0);
59809c8cb71SMark Johnston 	}
59909c8cb71SMark Johnston }
60009c8cb71SMark Johnston 
60109c8cb71SMark Johnston static void
60209c8cb71SMark Johnston kasan_mark_slab_invalid(uma_keg_t keg, void *mem)
60309c8cb71SMark Johnston {
60409c8cb71SMark Johnston 	size_t sz;
60509c8cb71SMark Johnston 
60609c8cb71SMark Johnston 	if ((keg->uk_flags & UMA_ZONE_NOKASAN) == 0) {
60709c8cb71SMark Johnston 		if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0)
60809c8cb71SMark Johnston 			sz = keg->uk_ppera * PAGE_SIZE;
60909c8cb71SMark Johnston 		else
61009c8cb71SMark Johnston 			sz = keg->uk_pgoff;
61109c8cb71SMark Johnston 		kasan_mark(mem, 0, sz, KASAN_UMA_FREED);
61209c8cb71SMark Johnston 	}
61309c8cb71SMark Johnston }
61409c8cb71SMark Johnston #else /* !KASAN */
61509c8cb71SMark Johnston static void
61609c8cb71SMark Johnston kasan_mark_item_valid(uma_zone_t zone __unused, void *item __unused)
61709c8cb71SMark Johnston {
61809c8cb71SMark Johnston }
61909c8cb71SMark Johnston 
62009c8cb71SMark Johnston static void
62109c8cb71SMark Johnston kasan_mark_item_invalid(uma_zone_t zone __unused, void *item __unused)
62209c8cb71SMark Johnston {
62309c8cb71SMark Johnston }
62409c8cb71SMark Johnston 
62509c8cb71SMark Johnston static void
62609c8cb71SMark Johnston kasan_mark_slab_valid(uma_keg_t keg __unused, void *mem __unused)
62709c8cb71SMark Johnston {
62809c8cb71SMark Johnston }
62909c8cb71SMark Johnston 
63009c8cb71SMark Johnston static void
63109c8cb71SMark Johnston kasan_mark_slab_invalid(uma_keg_t keg __unused, void *mem __unused)
63209c8cb71SMark Johnston {
63309c8cb71SMark Johnston }
63409c8cb71SMark Johnston #endif /* KASAN */
63509c8cb71SMark Johnston 
63610094910SMark Johnston #ifdef KMSAN
63710094910SMark Johnston static inline void
63810094910SMark Johnston kmsan_mark_item_uninitialized(uma_zone_t zone, void *item)
63910094910SMark Johnston {
64010094910SMark Johnston 	void *pcpu_item;
64110094910SMark Johnston 	size_t sz;
64210094910SMark Johnston 	int i;
64310094910SMark Johnston 
64410094910SMark Johnston 	if ((zone->uz_flags &
64510094910SMark Johnston 	    (UMA_ZFLAG_CACHE | UMA_ZONE_SECONDARY | UMA_ZONE_MALLOC)) != 0) {
64610094910SMark Johnston 		/*
64710094910SMark Johnston 		 * Cache zones should not be instrumented by default, as UMA
64810094910SMark Johnston 		 * does not have enough information to do so correctly.
64910094910SMark Johnston 		 * Consumers can mark items themselves if it makes sense to do
65010094910SMark Johnston 		 * so.
65110094910SMark Johnston 		 *
65210094910SMark Johnston 		 * Items from secondary zones are initialized by the parent
65310094910SMark Johnston 		 * zone and thus cannot safely be marked by UMA.
65410094910SMark Johnston 		 *
65510094910SMark Johnston 		 * malloc zones are handled directly by malloc(9) and friends,
65610094910SMark Johnston 		 * since they can provide more precise origin tracking.
65710094910SMark Johnston 		 */
65810094910SMark Johnston 		return;
65910094910SMark Johnston 	}
66010094910SMark Johnston 	if (zone->uz_keg->uk_init != NULL) {
66110094910SMark Johnston 		/*
66210094910SMark Johnston 		 * By definition, initialized items cannot be marked.  The
66310094910SMark Johnston 		 * best we can do is mark items from these zones after they
66410094910SMark Johnston 		 * are freed to the keg.
66510094910SMark Johnston 		 */
66610094910SMark Johnston 		return;
66710094910SMark Johnston 	}
66810094910SMark Johnston 
66910094910SMark Johnston 	sz = zone->uz_size;
67010094910SMark Johnston 	if ((zone->uz_flags & UMA_ZONE_PCPU) == 0) {
67110094910SMark Johnston 		kmsan_orig(item, sz, KMSAN_TYPE_UMA, KMSAN_RET_ADDR);
67210094910SMark Johnston 		kmsan_mark(item, sz, KMSAN_STATE_UNINIT);
67310094910SMark Johnston 	} else {
67410094910SMark Johnston 		pcpu_item = zpcpu_base_to_offset(item);
67510094910SMark Johnston 		for (i = 0; i <= mp_maxid; i++) {
67610094910SMark Johnston 			kmsan_orig(zpcpu_get_cpu(pcpu_item, i), sz,
67710094910SMark Johnston 			    KMSAN_TYPE_UMA, KMSAN_RET_ADDR);
67810094910SMark Johnston 			kmsan_mark(zpcpu_get_cpu(pcpu_item, i), sz,
67910094910SMark Johnston 			    KMSAN_STATE_INITED);
68010094910SMark Johnston 		}
68110094910SMark Johnston 	}
68210094910SMark Johnston }
68310094910SMark Johnston #else /* !KMSAN */
68410094910SMark Johnston static inline void
68510094910SMark Johnston kmsan_mark_item_uninitialized(uma_zone_t zone __unused, void *item __unused)
68610094910SMark Johnston {
68710094910SMark Johnston }
68810094910SMark Johnston #endif /* KMSAN */
68910094910SMark Johnston 
69008cfa56eSMark Johnston /*
691c6fd3e23SJeff Roberson  * Acquire the domain lock and record contention.
692c6fd3e23SJeff Roberson  */
693c6fd3e23SJeff Roberson static uma_zone_domain_t
694c6fd3e23SJeff Roberson zone_domain_lock(uma_zone_t zone, int domain)
695c6fd3e23SJeff Roberson {
696c6fd3e23SJeff Roberson 	uma_zone_domain_t zdom;
697c6fd3e23SJeff Roberson 	bool lockfail;
698c6fd3e23SJeff Roberson 
699c6fd3e23SJeff Roberson 	zdom = ZDOM_GET(zone, domain);
700c6fd3e23SJeff Roberson 	lockfail = false;
701c6fd3e23SJeff Roberson 	if (ZDOM_OWNED(zdom))
702c6fd3e23SJeff Roberson 		lockfail = true;
703c6fd3e23SJeff Roberson 	ZDOM_LOCK(zdom);
704c6fd3e23SJeff Roberson 	/* This is unsynchronized.  The counter does not need to be precise. */
705c6fd3e23SJeff Roberson 	if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max)
706c6fd3e23SJeff Roberson 		zone->uz_bucket_size++;
707c6fd3e23SJeff Roberson 	return (zdom);
708c6fd3e23SJeff Roberson }
709c6fd3e23SJeff Roberson 
710c6fd3e23SJeff Roberson /*
711fe835cbfSJeff Roberson  * Search for the domain with the least cached items and return it if it
712fe835cbfSJeff Roberson  * is out of balance with the preferred domain.
713c6fd3e23SJeff Roberson  */
714c6fd3e23SJeff Roberson static __noinline int
715c6fd3e23SJeff Roberson zone_domain_lowest(uma_zone_t zone, int pref)
716c6fd3e23SJeff Roberson {
717fe835cbfSJeff Roberson 	long least, nitems, prefitems;
718c6fd3e23SJeff Roberson 	int domain;
719c6fd3e23SJeff Roberson 	int i;
720c6fd3e23SJeff Roberson 
721fe835cbfSJeff Roberson 	prefitems = least = LONG_MAX;
722c6fd3e23SJeff Roberson 	domain = 0;
723c6fd3e23SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
724c6fd3e23SJeff Roberson 		nitems = ZDOM_GET(zone, i)->uzd_nitems;
725c6fd3e23SJeff Roberson 		if (nitems < least) {
726c6fd3e23SJeff Roberson 			domain = i;
727c6fd3e23SJeff Roberson 			least = nitems;
728c6fd3e23SJeff Roberson 		}
729fe835cbfSJeff Roberson 		if (domain == pref)
730fe835cbfSJeff Roberson 			prefitems = nitems;
731fe835cbfSJeff Roberson 	}
732fe835cbfSJeff Roberson 	if (prefitems < least * 2)
733fe835cbfSJeff Roberson 		return (pref);
734c6fd3e23SJeff Roberson 
735c6fd3e23SJeff Roberson 	return (domain);
736c6fd3e23SJeff Roberson }
737c6fd3e23SJeff Roberson 
738c6fd3e23SJeff Roberson /*
739c6fd3e23SJeff Roberson  * Search for the domain with the most cached items and return it or the
740c6fd3e23SJeff Roberson  * preferred domain if it has enough to proceed.
741c6fd3e23SJeff Roberson  */
742c6fd3e23SJeff Roberson static __noinline int
743c6fd3e23SJeff Roberson zone_domain_highest(uma_zone_t zone, int pref)
744c6fd3e23SJeff Roberson {
745c6fd3e23SJeff Roberson 	long most, nitems;
746c6fd3e23SJeff Roberson 	int domain;
747c6fd3e23SJeff Roberson 	int i;
748c6fd3e23SJeff Roberson 
749c6fd3e23SJeff Roberson 	if (ZDOM_GET(zone, pref)->uzd_nitems > BUCKET_MAX)
750c6fd3e23SJeff Roberson 		return (pref);
751c6fd3e23SJeff Roberson 
752c6fd3e23SJeff Roberson 	most = 0;
753c6fd3e23SJeff Roberson 	domain = 0;
754c6fd3e23SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
755c6fd3e23SJeff Roberson 		nitems = ZDOM_GET(zone, i)->uzd_nitems;
756c6fd3e23SJeff Roberson 		if (nitems > most) {
757c6fd3e23SJeff Roberson 			domain = i;
758c6fd3e23SJeff Roberson 			most = nitems;
759c6fd3e23SJeff Roberson 		}
760c6fd3e23SJeff Roberson 	}
761c6fd3e23SJeff Roberson 
762c6fd3e23SJeff Roberson 	return (domain);
763c6fd3e23SJeff Roberson }
764c6fd3e23SJeff Roberson 
765c6fd3e23SJeff Roberson /*
766c6fd3e23SJeff Roberson  * Set the maximum imax value.
767c6fd3e23SJeff Roberson  */
768c6fd3e23SJeff Roberson static void
769c6fd3e23SJeff Roberson zone_domain_imax_set(uma_zone_domain_t zdom, int nitems)
770c6fd3e23SJeff Roberson {
771c6fd3e23SJeff Roberson 	long old;
772c6fd3e23SJeff Roberson 
773c6fd3e23SJeff Roberson 	old = zdom->uzd_imax;
774c6fd3e23SJeff Roberson 	do {
775c6fd3e23SJeff Roberson 		if (old >= nitems)
7762760658bSAlexander Motin 			return;
777c6fd3e23SJeff Roberson 	} while (atomic_fcmpset_long(&zdom->uzd_imax, &old, nitems) == 0);
7782760658bSAlexander Motin 
7792760658bSAlexander Motin 	/*
7802760658bSAlexander Motin 	 * We are at new maximum, so do the last WSS update for the old
7812760658bSAlexander Motin 	 * bimin and prepare to measure next allocation batch.
7822760658bSAlexander Motin 	 */
7832760658bSAlexander Motin 	if (zdom->uzd_wss < old - zdom->uzd_bimin)
7842760658bSAlexander Motin 		zdom->uzd_wss = old - zdom->uzd_bimin;
7852760658bSAlexander Motin 	zdom->uzd_bimin = nitems;
786c6fd3e23SJeff Roberson }
787c6fd3e23SJeff Roberson 
788c6fd3e23SJeff Roberson /*
78908cfa56eSMark Johnston  * Attempt to satisfy an allocation by retrieving a full bucket from one of the
790d4665eaaSJeff Roberson  * zone's caches.  If a bucket is found the zone is not locked on return.
79108cfa56eSMark Johnston  */
7920f9b7bf3SMark Johnston static uma_bucket_t
793c6fd3e23SJeff Roberson zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom, bool reclaim)
7940f9b7bf3SMark Johnston {
7950f9b7bf3SMark Johnston 	uma_bucket_t bucket;
7962760658bSAlexander Motin 	long cnt;
797d4665eaaSJeff Roberson 	int i;
798d4665eaaSJeff Roberson 	bool dtor = false;
7990f9b7bf3SMark Johnston 
800c6fd3e23SJeff Roberson 	ZDOM_LOCK_ASSERT(zdom);
8010f9b7bf3SMark Johnston 
802dc3915c8SJeff Roberson 	if ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) == NULL)
803d4665eaaSJeff Roberson 		return (NULL);
804d4665eaaSJeff Roberson 
805543117beSJeff Roberson 	/* SMR Buckets can not be re-used until readers expire. */
806d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0 &&
807d4665eaaSJeff Roberson 	    bucket->ub_seq != SMR_SEQ_INVALID) {
808d4665eaaSJeff Roberson 		if (!smr_poll(zone->uz_smr, bucket->ub_seq, false))
809d4665eaaSJeff Roberson 			return (NULL);
810d4665eaaSJeff Roberson 		bucket->ub_seq = SMR_SEQ_INVALID;
811543117beSJeff Roberson 		dtor = (zone->uz_dtor != NULL) || UMA_ALWAYS_CTORDTOR;
812c6fd3e23SJeff Roberson 		if (STAILQ_NEXT(bucket, ub_link) != NULL)
813c6fd3e23SJeff Roberson 			zdom->uzd_seq = STAILQ_NEXT(bucket, ub_link)->ub_seq;
814d4665eaaSJeff Roberson 	}
815dc3915c8SJeff Roberson 	STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link);
81606d8bdcbSMark Johnston 
81706d8bdcbSMark Johnston 	KASSERT(zdom->uzd_nitems >= bucket->ub_cnt,
81806d8bdcbSMark Johnston 	    ("%s: item count underflow (%ld, %d)",
81906d8bdcbSMark Johnston 	    __func__, zdom->uzd_nitems, bucket->ub_cnt));
82006d8bdcbSMark Johnston 	KASSERT(bucket->ub_cnt > 0,
82106d8bdcbSMark Johnston 	    ("%s: empty bucket in bucket cache", __func__));
8220f9b7bf3SMark Johnston 	zdom->uzd_nitems -= bucket->ub_cnt;
823c6fd3e23SJeff Roberson 
8242760658bSAlexander Motin 	if (reclaim) {
825c6fd3e23SJeff Roberson 		/*
826c6fd3e23SJeff Roberson 		 * Shift the bounds of the current WSS interval to avoid
8272760658bSAlexander Motin 		 * perturbing the estimates.
828c6fd3e23SJeff Roberson 		 */
8292760658bSAlexander Motin 		cnt = lmin(zdom->uzd_bimin, bucket->ub_cnt);
8302760658bSAlexander Motin 		atomic_subtract_long(&zdom->uzd_imax, cnt);
8312760658bSAlexander Motin 		zdom->uzd_bimin -= cnt;
832c6fd3e23SJeff Roberson 		zdom->uzd_imin -= lmin(zdom->uzd_imin, bucket->ub_cnt);
8332760658bSAlexander Motin 		if (zdom->uzd_limin >= bucket->ub_cnt) {
8342760658bSAlexander Motin 			zdom->uzd_limin -= bucket->ub_cnt;
8352760658bSAlexander Motin 		} else {
8362760658bSAlexander Motin 			zdom->uzd_limin = 0;
8372760658bSAlexander Motin 			zdom->uzd_timin = 0;
8382760658bSAlexander Motin 		}
8392760658bSAlexander Motin 	} else if (zdom->uzd_bimin > zdom->uzd_nitems) {
8402760658bSAlexander Motin 		zdom->uzd_bimin = zdom->uzd_nitems;
8412760658bSAlexander Motin 		if (zdom->uzd_imin > zdom->uzd_nitems)
8420f9b7bf3SMark Johnston 			zdom->uzd_imin = zdom->uzd_nitems;
8432760658bSAlexander Motin 	}
844c6fd3e23SJeff Roberson 
845c6fd3e23SJeff Roberson 	ZDOM_UNLOCK(zdom);
846d4665eaaSJeff Roberson 	if (dtor)
847d4665eaaSJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
848d4665eaaSJeff Roberson 			item_dtor(zone, bucket->ub_bucket[i], zone->uz_size,
849d4665eaaSJeff Roberson 			    NULL, SKIP_NONE);
850d4665eaaSJeff Roberson 
8510f9b7bf3SMark Johnston 	return (bucket);
8520f9b7bf3SMark Johnston }
8530f9b7bf3SMark Johnston 
85408cfa56eSMark Johnston /*
85508cfa56eSMark Johnston  * Insert a full bucket into the specified cache.  The "ws" parameter indicates
85608cfa56eSMark Johnston  * whether the bucket's contents should be counted as part of the zone's working
857c6fd3e23SJeff Roberson  * set.  The bucket may be freed if it exceeds the bucket limit.
85808cfa56eSMark Johnston  */
8590f9b7bf3SMark Johnston static void
860c6fd3e23SJeff Roberson zone_put_bucket(uma_zone_t zone, int domain, uma_bucket_t bucket, void *udata,
8610f9b7bf3SMark Johnston     const bool ws)
8620f9b7bf3SMark Johnston {
863c6fd3e23SJeff Roberson 	uma_zone_domain_t zdom;
8640f9b7bf3SMark Johnston 
865c6fd3e23SJeff Roberson 	/* We don't cache empty buckets.  This can happen after a reclaim. */
866c6fd3e23SJeff Roberson 	if (bucket->ub_cnt == 0)
867c6fd3e23SJeff Roberson 		goto out;
868c6fd3e23SJeff Roberson 	zdom = zone_domain_lock(zone, domain);
869c6fd3e23SJeff Roberson 
870c6fd3e23SJeff Roberson 	/*
871c6fd3e23SJeff Roberson 	 * Conditionally set the maximum number of items.
872c6fd3e23SJeff Roberson 	 */
8730f9b7bf3SMark Johnston 	zdom->uzd_nitems += bucket->ub_cnt;
874c6fd3e23SJeff Roberson 	if (__predict_true(zdom->uzd_nitems < zone->uz_bucket_max)) {
8752760658bSAlexander Motin 		if (ws) {
876c6fd3e23SJeff Roberson 			zone_domain_imax_set(zdom, zdom->uzd_nitems);
8772760658bSAlexander Motin 		} else {
8782760658bSAlexander Motin 			/*
8792760658bSAlexander Motin 			 * Shift the bounds of the current WSS interval to
8802760658bSAlexander Motin 			 * avoid perturbing the estimates.
8812760658bSAlexander Motin 			 */
8822760658bSAlexander Motin 			atomic_add_long(&zdom->uzd_imax, bucket->ub_cnt);
8832760658bSAlexander Motin 			zdom->uzd_imin += bucket->ub_cnt;
8842760658bSAlexander Motin 			zdom->uzd_bimin += bucket->ub_cnt;
8852760658bSAlexander Motin 			zdom->uzd_limin += bucket->ub_cnt;
8862760658bSAlexander Motin 		}
887c6fd3e23SJeff Roberson 		if (STAILQ_EMPTY(&zdom->uzd_buckets))
888c6fd3e23SJeff Roberson 			zdom->uzd_seq = bucket->ub_seq;
8895afdf5c1SMark Johnston 
8905afdf5c1SMark Johnston 		/*
8915afdf5c1SMark Johnston 		 * Try to promote reuse of recently used items.  For items
8925afdf5c1SMark Johnston 		 * protected by SMR, try to defer reuse to minimize polling.
8935afdf5c1SMark Johnston 		 */
8945afdf5c1SMark Johnston 		if (bucket->ub_seq == SMR_SEQ_INVALID)
8955afdf5c1SMark Johnston 			STAILQ_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link);
8965afdf5c1SMark Johnston 		else
897c6fd3e23SJeff Roberson 			STAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link);
898c6fd3e23SJeff Roberson 		ZDOM_UNLOCK(zdom);
899c6fd3e23SJeff Roberson 		return;
900c6fd3e23SJeff Roberson 	}
901c6fd3e23SJeff Roberson 	zdom->uzd_nitems -= bucket->ub_cnt;
902c6fd3e23SJeff Roberson 	ZDOM_UNLOCK(zdom);
903c6fd3e23SJeff Roberson out:
904c6fd3e23SJeff Roberson 	bucket_free(zone, bucket, udata);
9050f9b7bf3SMark Johnston }
9060f9b7bf3SMark Johnston 
907376b1ba3SJeff Roberson /* Pops an item out of a per-cpu cache bucket. */
908376b1ba3SJeff Roberson static inline void *
909376b1ba3SJeff Roberson cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket)
910376b1ba3SJeff Roberson {
911376b1ba3SJeff Roberson 	void *item;
912376b1ba3SJeff Roberson 
913376b1ba3SJeff Roberson 	CRITICAL_ASSERT(curthread);
914376b1ba3SJeff Roberson 
915376b1ba3SJeff Roberson 	bucket->ucb_cnt--;
916376b1ba3SJeff Roberson 	item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt];
917376b1ba3SJeff Roberson #ifdef INVARIANTS
918376b1ba3SJeff Roberson 	bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL;
919376b1ba3SJeff Roberson 	KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
920376b1ba3SJeff Roberson #endif
921376b1ba3SJeff Roberson 	cache->uc_allocs++;
922376b1ba3SJeff Roberson 
923376b1ba3SJeff Roberson 	return (item);
924376b1ba3SJeff Roberson }
925376b1ba3SJeff Roberson 
926376b1ba3SJeff Roberson /* Pushes an item into a per-cpu cache bucket. */
927376b1ba3SJeff Roberson static inline void
928376b1ba3SJeff Roberson cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item)
929376b1ba3SJeff Roberson {
930376b1ba3SJeff Roberson 
931376b1ba3SJeff Roberson 	CRITICAL_ASSERT(curthread);
932376b1ba3SJeff Roberson 	KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL,
933376b1ba3SJeff Roberson 	    ("uma_zfree: Freeing to non free bucket index."));
934376b1ba3SJeff Roberson 
935376b1ba3SJeff Roberson 	bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item;
936376b1ba3SJeff Roberson 	bucket->ucb_cnt++;
937376b1ba3SJeff Roberson 	cache->uc_frees++;
938376b1ba3SJeff Roberson }
939376b1ba3SJeff Roberson 
940376b1ba3SJeff Roberson /*
941376b1ba3SJeff Roberson  * Unload a UMA bucket from a per-cpu cache.
942376b1ba3SJeff Roberson  */
943376b1ba3SJeff Roberson static inline uma_bucket_t
944376b1ba3SJeff Roberson cache_bucket_unload(uma_cache_bucket_t bucket)
945376b1ba3SJeff Roberson {
946376b1ba3SJeff Roberson 	uma_bucket_t b;
947376b1ba3SJeff Roberson 
948376b1ba3SJeff Roberson 	b = bucket->ucb_bucket;
949376b1ba3SJeff Roberson 	if (b != NULL) {
950376b1ba3SJeff Roberson 		MPASS(b->ub_entries == bucket->ucb_entries);
951376b1ba3SJeff Roberson 		b->ub_cnt = bucket->ucb_cnt;
952376b1ba3SJeff Roberson 		bucket->ucb_bucket = NULL;
953376b1ba3SJeff Roberson 		bucket->ucb_entries = bucket->ucb_cnt = 0;
954376b1ba3SJeff Roberson 	}
955376b1ba3SJeff Roberson 
956376b1ba3SJeff Roberson 	return (b);
957376b1ba3SJeff Roberson }
958376b1ba3SJeff Roberson 
959376b1ba3SJeff Roberson static inline uma_bucket_t
960376b1ba3SJeff Roberson cache_bucket_unload_alloc(uma_cache_t cache)
961376b1ba3SJeff Roberson {
962376b1ba3SJeff Roberson 
963376b1ba3SJeff Roberson 	return (cache_bucket_unload(&cache->uc_allocbucket));
964376b1ba3SJeff Roberson }
965376b1ba3SJeff Roberson 
966376b1ba3SJeff Roberson static inline uma_bucket_t
967376b1ba3SJeff Roberson cache_bucket_unload_free(uma_cache_t cache)
968376b1ba3SJeff Roberson {
969376b1ba3SJeff Roberson 
970376b1ba3SJeff Roberson 	return (cache_bucket_unload(&cache->uc_freebucket));
971376b1ba3SJeff Roberson }
972376b1ba3SJeff Roberson 
973376b1ba3SJeff Roberson static inline uma_bucket_t
974376b1ba3SJeff Roberson cache_bucket_unload_cross(uma_cache_t cache)
975376b1ba3SJeff Roberson {
976376b1ba3SJeff Roberson 
977376b1ba3SJeff Roberson 	return (cache_bucket_unload(&cache->uc_crossbucket));
978376b1ba3SJeff Roberson }
979376b1ba3SJeff Roberson 
980376b1ba3SJeff Roberson /*
981376b1ba3SJeff Roberson  * Load a bucket into a per-cpu cache bucket.
982376b1ba3SJeff Roberson  */
983376b1ba3SJeff Roberson static inline void
984376b1ba3SJeff Roberson cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b)
985376b1ba3SJeff Roberson {
986376b1ba3SJeff Roberson 
987376b1ba3SJeff Roberson 	CRITICAL_ASSERT(curthread);
988376b1ba3SJeff Roberson 	MPASS(bucket->ucb_bucket == NULL);
989543117beSJeff Roberson 	MPASS(b->ub_seq == SMR_SEQ_INVALID);
990376b1ba3SJeff Roberson 
991376b1ba3SJeff Roberson 	bucket->ucb_bucket = b;
992376b1ba3SJeff Roberson 	bucket->ucb_cnt = b->ub_cnt;
993376b1ba3SJeff Roberson 	bucket->ucb_entries = b->ub_entries;
994376b1ba3SJeff Roberson }
995376b1ba3SJeff Roberson 
996376b1ba3SJeff Roberson static inline void
997376b1ba3SJeff Roberson cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b)
998376b1ba3SJeff Roberson {
999376b1ba3SJeff Roberson 
1000376b1ba3SJeff Roberson 	cache_bucket_load(&cache->uc_allocbucket, b);
1001376b1ba3SJeff Roberson }
1002376b1ba3SJeff Roberson 
1003376b1ba3SJeff Roberson static inline void
1004376b1ba3SJeff Roberson cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b)
1005376b1ba3SJeff Roberson {
1006376b1ba3SJeff Roberson 
1007376b1ba3SJeff Roberson 	cache_bucket_load(&cache->uc_freebucket, b);
1008376b1ba3SJeff Roberson }
1009376b1ba3SJeff Roberson 
1010dfe13344SJeff Roberson #ifdef NUMA
1011376b1ba3SJeff Roberson static inline void
1012376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b)
1013376b1ba3SJeff Roberson {
1014376b1ba3SJeff Roberson 
1015376b1ba3SJeff Roberson 	cache_bucket_load(&cache->uc_crossbucket, b);
1016376b1ba3SJeff Roberson }
1017376b1ba3SJeff Roberson #endif
1018376b1ba3SJeff Roberson 
1019376b1ba3SJeff Roberson /*
1020376b1ba3SJeff Roberson  * Copy and preserve ucb_spare.
1021376b1ba3SJeff Roberson  */
1022376b1ba3SJeff Roberson static inline void
1023376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2)
1024376b1ba3SJeff Roberson {
1025376b1ba3SJeff Roberson 
1026376b1ba3SJeff Roberson 	b1->ucb_bucket = b2->ucb_bucket;
1027376b1ba3SJeff Roberson 	b1->ucb_entries = b2->ucb_entries;
1028376b1ba3SJeff Roberson 	b1->ucb_cnt = b2->ucb_cnt;
1029376b1ba3SJeff Roberson }
1030376b1ba3SJeff Roberson 
1031376b1ba3SJeff Roberson /*
1032376b1ba3SJeff Roberson  * Swap two cache buckets.
1033376b1ba3SJeff Roberson  */
1034376b1ba3SJeff Roberson static inline void
1035376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2)
1036376b1ba3SJeff Roberson {
1037376b1ba3SJeff Roberson 	struct uma_cache_bucket b3;
1038376b1ba3SJeff Roberson 
1039376b1ba3SJeff Roberson 	CRITICAL_ASSERT(curthread);
1040376b1ba3SJeff Roberson 
1041376b1ba3SJeff Roberson 	cache_bucket_copy(&b3, b1);
1042376b1ba3SJeff Roberson 	cache_bucket_copy(b1, b2);
1043376b1ba3SJeff Roberson 	cache_bucket_copy(b2, &b3);
1044376b1ba3SJeff Roberson }
1045376b1ba3SJeff Roberson 
1046c6fd3e23SJeff Roberson /*
1047c6fd3e23SJeff Roberson  * Attempt to fetch a bucket from a zone on behalf of the current cpu cache.
1048c6fd3e23SJeff Roberson  */
1049c6fd3e23SJeff Roberson static uma_bucket_t
1050c6fd3e23SJeff Roberson cache_fetch_bucket(uma_zone_t zone, uma_cache_t cache, int domain)
1051c6fd3e23SJeff Roberson {
1052c6fd3e23SJeff Roberson 	uma_zone_domain_t zdom;
1053c6fd3e23SJeff Roberson 	uma_bucket_t bucket;
1054c6fd3e23SJeff Roberson 
1055c6fd3e23SJeff Roberson 	/*
1056c6fd3e23SJeff Roberson 	 * Avoid the lock if possible.
1057c6fd3e23SJeff Roberson 	 */
1058c6fd3e23SJeff Roberson 	zdom = ZDOM_GET(zone, domain);
1059c6fd3e23SJeff Roberson 	if (zdom->uzd_nitems == 0)
1060c6fd3e23SJeff Roberson 		return (NULL);
1061c6fd3e23SJeff Roberson 
1062c6fd3e23SJeff Roberson 	if ((cache_uz_flags(cache) & UMA_ZONE_SMR) != 0 &&
1063c6fd3e23SJeff Roberson 	    !smr_poll(zone->uz_smr, zdom->uzd_seq, false))
1064c6fd3e23SJeff Roberson 		return (NULL);
1065c6fd3e23SJeff Roberson 
1066c6fd3e23SJeff Roberson 	/*
1067c6fd3e23SJeff Roberson 	 * Check the zone's cache of buckets.
1068c6fd3e23SJeff Roberson 	 */
1069c6fd3e23SJeff Roberson 	zdom = zone_domain_lock(zone, domain);
107006d8bdcbSMark Johnston 	if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL)
1071c6fd3e23SJeff Roberson 		return (bucket);
1072c6fd3e23SJeff Roberson 	ZDOM_UNLOCK(zdom);
1073c6fd3e23SJeff Roberson 
1074c6fd3e23SJeff Roberson 	return (NULL);
1075c6fd3e23SJeff Roberson }
1076c6fd3e23SJeff Roberson 
10772f891cd5SPawel Jakub Dawidek static void
10782f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone)
10792f891cd5SPawel Jakub Dawidek {
10802f891cd5SPawel Jakub Dawidek 	static const struct timeval warninterval = { 300, 0 };
10812f891cd5SPawel Jakub Dawidek 
10822f891cd5SPawel Jakub Dawidek 	if (!zone_warnings || zone->uz_warning == NULL)
10832f891cd5SPawel Jakub Dawidek 		return;
10842f891cd5SPawel Jakub Dawidek 
10852f891cd5SPawel Jakub Dawidek 	if (ratecheck(&zone->uz_ratecheck, &warninterval))
10862f891cd5SPawel Jakub Dawidek 		printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
10872f891cd5SPawel Jakub Dawidek }
10882f891cd5SPawel Jakub Dawidek 
108954503a13SJonathan T. Looney static inline void
109054503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone)
109154503a13SJonathan T. Looney {
1092e60b2fcbSGleb Smirnoff 
1093e60b2fcbSGleb Smirnoff 	if (zone->uz_maxaction.ta_func != NULL)
1094e60b2fcbSGleb Smirnoff 		taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction);
109554503a13SJonathan T. Looney }
109654503a13SJonathan T. Looney 
10978355f576SJeff Roberson /*
10988355f576SJeff Roberson  * Routine called by timeout which is used to fire off some time interval
10999643769aSJeff Roberson  * based calculations.  (stats, hash size, etc.)
11008355f576SJeff Roberson  *
11018355f576SJeff Roberson  * Arguments:
11028355f576SJeff Roberson  *	arg   Unused
11038355f576SJeff Roberson  *
11048355f576SJeff Roberson  * Returns:
11058355f576SJeff Roberson  *	Nothing
11068355f576SJeff Roberson  */
11078355f576SJeff Roberson static void
11088355f576SJeff Roberson uma_timeout(void *unused)
11098355f576SJeff Roberson {
111086bbae32SJeff Roberson 	bucket_enable();
111120a4e154SJeff Roberson 	zone_foreach(zone_timeout, NULL);
11128355f576SJeff Roberson 
11138355f576SJeff Roberson 	/* Reschedule this event */
11149643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
11158355f576SJeff Roberson }
11168355f576SJeff Roberson 
11178355f576SJeff Roberson /*
11182760658bSAlexander Motin  * Update the working set size estimates for the zone's bucket cache.
11192760658bSAlexander Motin  * The constants chosen here are somewhat arbitrary.
11200f9b7bf3SMark Johnston  */
11210f9b7bf3SMark Johnston static void
11220f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom)
11230f9b7bf3SMark Johnston {
11242760658bSAlexander Motin 	long m;
11250f9b7bf3SMark Johnston 
11262760658bSAlexander Motin 	ZDOM_LOCK_ASSERT(zdom);
11272760658bSAlexander Motin 	MPASS(zdom->uzd_imax >= zdom->uzd_nitems);
11282760658bSAlexander Motin 	MPASS(zdom->uzd_nitems >= zdom->uzd_bimin);
11292760658bSAlexander Motin 	MPASS(zdom->uzd_bimin >= zdom->uzd_imin);
11302760658bSAlexander Motin 
11312760658bSAlexander Motin 	/*
11322760658bSAlexander Motin 	 * Estimate WSS as modified moving average of biggest allocation
11332760658bSAlexander Motin 	 * batches for each period over few minutes (UMA_TIMEOUT of 20s).
11342760658bSAlexander Motin 	 */
11352760658bSAlexander Motin 	zdom->uzd_wss = lmax(zdom->uzd_wss * 3 / 4,
11362760658bSAlexander Motin 	    zdom->uzd_imax - zdom->uzd_bimin);
11372760658bSAlexander Motin 
11382760658bSAlexander Motin 	/*
11392760658bSAlexander Motin 	 * Estimate longtime minimum item count as a combination of recent
11402760658bSAlexander Motin 	 * minimum item count, adjusted by WSS for safety, and the modified
11412760658bSAlexander Motin 	 * moving average over the last several hours (UMA_TIMEOUT of 20s).
11422760658bSAlexander Motin 	 * timin measures time since limin tried to go negative, that means
11432760658bSAlexander Motin 	 * we were dangerously close to or got out of cache.
11442760658bSAlexander Motin 	 */
11452760658bSAlexander Motin 	m = zdom->uzd_imin - zdom->uzd_wss;
11462760658bSAlexander Motin 	if (m >= 0) {
11472760658bSAlexander Motin 		if (zdom->uzd_limin >= m)
11482760658bSAlexander Motin 			zdom->uzd_limin = m;
11492760658bSAlexander Motin 		else
11502760658bSAlexander Motin 			zdom->uzd_limin = (m + zdom->uzd_limin * 255) / 256;
11512760658bSAlexander Motin 		zdom->uzd_timin++;
11522760658bSAlexander Motin 	} else {
11532760658bSAlexander Motin 		zdom->uzd_limin = 0;
11542760658bSAlexander Motin 		zdom->uzd_timin = 0;
11552760658bSAlexander Motin 	}
11562760658bSAlexander Motin 
11572760658bSAlexander Motin 	/* To reduce period edge effects on WSS keep half of the imax. */
11582760658bSAlexander Motin 	atomic_subtract_long(&zdom->uzd_imax,
11592760658bSAlexander Motin 	    (zdom->uzd_imax - zdom->uzd_nitems + 1) / 2);
11602760658bSAlexander Motin 	zdom->uzd_imin = zdom->uzd_bimin = zdom->uzd_nitems;
11610f9b7bf3SMark Johnston }
11620f9b7bf3SMark Johnston 
11630f9b7bf3SMark Johnston /*
11649643769aSJeff Roberson  * Routine to perform timeout driven calculations.  This expands the
11659643769aSJeff Roberson  * hashes and does per cpu statistics aggregation.
11668355f576SJeff Roberson  *
1167e20a199fSJeff Roberson  *  Returns nothing.
11688355f576SJeff Roberson  */
11698355f576SJeff Roberson static void
117020a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused)
11718355f576SJeff Roberson {
117208034d10SKonstantin Belousov 	uma_keg_t keg;
11738b987a77SJeff Roberson 	u_int slabs, pages;
11748355f576SJeff Roberson 
117554c5ae80SRyan Libby 	if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0)
11762760658bSAlexander Motin 		goto trim;
117708034d10SKonstantin Belousov 
117808034d10SKonstantin Belousov 	keg = zone->uz_keg;
11798b987a77SJeff Roberson 
11808b987a77SJeff Roberson 	/*
11818b987a77SJeff Roberson 	 * Hash zones are non-numa by definition so the first domain
11828b987a77SJeff Roberson 	 * is the only one present.
11838b987a77SJeff Roberson 	 */
11848b987a77SJeff Roberson 	KEG_LOCK(keg, 0);
11858b987a77SJeff Roberson 	pages = keg->uk_domain[0].ud_pages;
11868b987a77SJeff Roberson 
11878355f576SJeff Roberson 	/*
1188e20a199fSJeff Roberson 	 * Expand the keg hash table.
11898355f576SJeff Roberson 	 *
11908355f576SJeff Roberson 	 * This is done if the number of slabs is larger than the hash size.
11918355f576SJeff Roberson 	 * What I'm trying to do here is completely reduce collisions.  This
11928355f576SJeff Roberson 	 * may be a little aggressive.  Should I allow for two collisions max?
11938355f576SJeff Roberson 	 */
11948b987a77SJeff Roberson 	if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) {
11950aef6126SJeff Roberson 		struct uma_hash newhash;
11960aef6126SJeff Roberson 		struct uma_hash oldhash;
11970aef6126SJeff Roberson 		int ret;
11985300d9ddSJeff Roberson 
11990aef6126SJeff Roberson 		/*
12000aef6126SJeff Roberson 		 * This is so involved because allocating and freeing
1201e20a199fSJeff Roberson 		 * while the keg lock is held will lead to deadlock.
12020aef6126SJeff Roberson 		 * I have to do everything in stages and check for
12030aef6126SJeff Roberson 		 * races.
12040aef6126SJeff Roberson 		 */
12058b987a77SJeff Roberson 		KEG_UNLOCK(keg, 0);
12063b2f2cb8SAlexander Motin 		ret = hash_alloc(&newhash, 1 << fls(slabs));
12078b987a77SJeff Roberson 		KEG_LOCK(keg, 0);
12080aef6126SJeff Roberson 		if (ret) {
1209099a0e58SBosko Milekic 			if (hash_expand(&keg->uk_hash, &newhash)) {
1210099a0e58SBosko Milekic 				oldhash = keg->uk_hash;
1211099a0e58SBosko Milekic 				keg->uk_hash = newhash;
12120aef6126SJeff Roberson 			} else
12130aef6126SJeff Roberson 				oldhash = newhash;
12140aef6126SJeff Roberson 
12158b987a77SJeff Roberson 			KEG_UNLOCK(keg, 0);
12160aef6126SJeff Roberson 			hash_free(&oldhash);
12172760658bSAlexander Motin 			goto trim;
12180aef6126SJeff Roberson 		}
12195300d9ddSJeff Roberson 	}
12208b987a77SJeff Roberson 	KEG_UNLOCK(keg, 0);
1221e20a199fSJeff Roberson 
12222760658bSAlexander Motin trim:
12232760658bSAlexander Motin 	/* Trim caches not used for a long time. */
12242760658bSAlexander Motin 	for (int i = 0; i < vm_ndomains; i++) {
12252760658bSAlexander Motin 		if (bucket_cache_reclaim_domain(zone, false, false, i) &&
12262760658bSAlexander Motin 		    (zone->uz_flags & UMA_ZFLAG_CACHE) == 0)
12272760658bSAlexander Motin 			keg_drain(zone->uz_keg, i);
12282760658bSAlexander Motin 	}
12298355f576SJeff Roberson }
12308355f576SJeff Roberson 
12318355f576SJeff Roberson /*
12325300d9ddSJeff Roberson  * Allocate and zero fill the next sized hash table from the appropriate
12335300d9ddSJeff Roberson  * backing store.
12345300d9ddSJeff Roberson  *
12355300d9ddSJeff Roberson  * Arguments:
12360aef6126SJeff Roberson  *	hash  A new hash structure with the old hash size in uh_hashsize
12375300d9ddSJeff Roberson  *
12385300d9ddSJeff Roberson  * Returns:
1239763df3ecSPedro F. Giffuni  *	1 on success and 0 on failure.
12405300d9ddSJeff Roberson  */
124137c84183SPoul-Henning Kamp static int
12423b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size)
12435300d9ddSJeff Roberson {
124459568a0eSAlexander Motin 	size_t alloc;
12455300d9ddSJeff Roberson 
12463b2f2cb8SAlexander Motin 	KASSERT(powerof2(size), ("hash size must be power of 2"));
12473b2f2cb8SAlexander Motin 	if (size > UMA_HASH_SIZE_INIT)  {
12483b2f2cb8SAlexander Motin 		hash->uh_hashsize = size;
12490aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
12501e0701e1SJeff Roberson 		hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT);
12515300d9ddSJeff Roberson 	} else {
12520aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
1253e20a199fSJeff Roberson 		hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
1254ab3185d1SJeff Roberson 		    UMA_ANYDOMAIN, M_WAITOK);
12550aef6126SJeff Roberson 		hash->uh_hashsize = UMA_HASH_SIZE_INIT;
12565300d9ddSJeff Roberson 	}
12570aef6126SJeff Roberson 	if (hash->uh_slab_hash) {
12580aef6126SJeff Roberson 		bzero(hash->uh_slab_hash, alloc);
12590aef6126SJeff Roberson 		hash->uh_hashmask = hash->uh_hashsize - 1;
12600aef6126SJeff Roberson 		return (1);
12610aef6126SJeff Roberson 	}
12625300d9ddSJeff Roberson 
12630aef6126SJeff Roberson 	return (0);
12645300d9ddSJeff Roberson }
12655300d9ddSJeff Roberson 
12665300d9ddSJeff Roberson /*
126764f051e9SJeff Roberson  * Expands the hash table for HASH zones.  This is done from zone_timeout
126864f051e9SJeff Roberson  * to reduce collisions.  This must not be done in the regular allocation
126964f051e9SJeff Roberson  * path, otherwise, we can recurse on the vm while allocating pages.
12708355f576SJeff Roberson  *
12718355f576SJeff Roberson  * Arguments:
12720aef6126SJeff Roberson  *	oldhash  The hash you want to expand
12730aef6126SJeff Roberson  *	newhash  The hash structure for the new table
12748355f576SJeff Roberson  *
12758355f576SJeff Roberson  * Returns:
12768355f576SJeff Roberson  *	Nothing
12778355f576SJeff Roberson  *
12788355f576SJeff Roberson  * Discussion:
12798355f576SJeff Roberson  */
12800aef6126SJeff Roberson static int
12810aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
12828355f576SJeff Roberson {
12831e0701e1SJeff Roberson 	uma_hash_slab_t slab;
12846929b7d1SPedro F. Giffuni 	u_int hval;
12856929b7d1SPedro F. Giffuni 	u_int idx;
12868355f576SJeff Roberson 
12870aef6126SJeff Roberson 	if (!newhash->uh_slab_hash)
12880aef6126SJeff Roberson 		return (0);
12898355f576SJeff Roberson 
12900aef6126SJeff Roberson 	if (oldhash->uh_hashsize >= newhash->uh_hashsize)
12910aef6126SJeff Roberson 		return (0);
12928355f576SJeff Roberson 
12938355f576SJeff Roberson 	/*
12948355f576SJeff Roberson 	 * I need to investigate hash algorithms for resizing without a
12958355f576SJeff Roberson 	 * full rehash.
12968355f576SJeff Roberson 	 */
12978355f576SJeff Roberson 
12986929b7d1SPedro F. Giffuni 	for (idx = 0; idx < oldhash->uh_hashsize; idx++)
12991e0701e1SJeff Roberson 		while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) {
13001e0701e1SJeff Roberson 			slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]);
13011e0701e1SJeff Roberson 			LIST_REMOVE(slab, uhs_hlink);
13021e0701e1SJeff Roberson 			hval = UMA_HASH(newhash, slab->uhs_data);
13031e0701e1SJeff Roberson 			LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
13041e0701e1SJeff Roberson 			    slab, uhs_hlink);
13058355f576SJeff Roberson 		}
13068355f576SJeff Roberson 
13070aef6126SJeff Roberson 	return (1);
13089c2cd7e5SJeff Roberson }
13099c2cd7e5SJeff Roberson 
13105300d9ddSJeff Roberson /*
13115300d9ddSJeff Roberson  * Free the hash bucket to the appropriate backing store.
13125300d9ddSJeff Roberson  *
13135300d9ddSJeff Roberson  * Arguments:
13145300d9ddSJeff Roberson  *	slab_hash  The hash bucket we're freeing
13155300d9ddSJeff Roberson  *	hashsize   The number of entries in that hash bucket
13165300d9ddSJeff Roberson  *
13175300d9ddSJeff Roberson  * Returns:
13185300d9ddSJeff Roberson  *	Nothing
13195300d9ddSJeff Roberson  */
13209c2cd7e5SJeff Roberson static void
13210aef6126SJeff Roberson hash_free(struct uma_hash *hash)
13229c2cd7e5SJeff Roberson {
13230aef6126SJeff Roberson 	if (hash->uh_slab_hash == NULL)
13240aef6126SJeff Roberson 		return;
13250aef6126SJeff Roberson 	if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
13260095a784SJeff Roberson 		zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
13278355f576SJeff Roberson 	else
1328961647dfSJeff Roberson 		free(hash->uh_slab_hash, M_UMAHASH);
13298355f576SJeff Roberson }
13308355f576SJeff Roberson 
13318355f576SJeff Roberson /*
13328355f576SJeff Roberson  * Frees all outstanding items in a bucket
13338355f576SJeff Roberson  *
13348355f576SJeff Roberson  * Arguments:
13358355f576SJeff Roberson  *	zone   The zone to free to, must be unlocked.
13364bd61e19SJeff Roberson  *	bucket The free/alloc bucket with items.
13378355f576SJeff Roberson  *
13388355f576SJeff Roberson  * Returns:
13398355f576SJeff Roberson  *	Nothing
13408355f576SJeff Roberson  */
13418355f576SJeff Roberson static void
13428355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
13438355f576SJeff Roberson {
13440095a784SJeff Roberson 	int i;
13458355f576SJeff Roberson 
1346c6fd3e23SJeff Roberson 	if (bucket->ub_cnt == 0)
13478355f576SJeff Roberson 		return;
13488355f576SJeff Roberson 
1349d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0 &&
1350d4665eaaSJeff Roberson 	    bucket->ub_seq != SMR_SEQ_INVALID) {
1351d4665eaaSJeff Roberson 		smr_wait(zone->uz_smr, bucket->ub_seq);
1352543117beSJeff Roberson 		bucket->ub_seq = SMR_SEQ_INVALID;
1353d4665eaaSJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
1354d4665eaaSJeff Roberson 			item_dtor(zone, bucket->ub_bucket[i],
1355d4665eaaSJeff Roberson 			    zone->uz_size, NULL, SKIP_NONE);
1356d4665eaaSJeff Roberson 	}
13570095a784SJeff Roberson 	if (zone->uz_fini)
135809c8cb71SMark Johnston 		for (i = 0; i < bucket->ub_cnt; i++) {
135909c8cb71SMark Johnston 			kasan_mark_item_valid(zone, bucket->ub_bucket[i]);
13600095a784SJeff Roberson 			zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
136109c8cb71SMark Johnston 			kasan_mark_item_invalid(zone, bucket->ub_bucket[i]);
136209c8cb71SMark Johnston 		}
13630095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
13644bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
13654bd61e19SJeff Roberson 		zone_free_limit(zone, bucket->ub_cnt);
1366d4665eaaSJeff Roberson #ifdef INVARIANTS
1367d4665eaaSJeff Roberson 	bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt);
1368d4665eaaSJeff Roberson #endif
13690095a784SJeff Roberson 	bucket->ub_cnt = 0;
13708355f576SJeff Roberson }
13718355f576SJeff Roberson 
13728355f576SJeff Roberson /*
13738355f576SJeff Roberson  * Drains the per cpu caches for a zone.
13748355f576SJeff Roberson  *
1375727c6918SJeff Roberson  * NOTE: This may only be called while the zone is being torn down, and not
13765d1ae027SRobert Watson  * during normal operation.  This is necessary in order that we do not have
13775d1ae027SRobert Watson  * to migrate CPUs to drain the per-CPU caches.
13785d1ae027SRobert Watson  *
13798355f576SJeff Roberson  * Arguments:
13808355f576SJeff Roberson  *	zone     The zone to drain, must be unlocked.
13818355f576SJeff Roberson  *
13828355f576SJeff Roberson  * Returns:
13838355f576SJeff Roberson  *	Nothing
13848355f576SJeff Roberson  */
13858355f576SJeff Roberson static void
13869643769aSJeff Roberson cache_drain(uma_zone_t zone)
13878355f576SJeff Roberson {
13888355f576SJeff Roberson 	uma_cache_t cache;
1389376b1ba3SJeff Roberson 	uma_bucket_t bucket;
1390543117beSJeff Roberson 	smr_seq_t seq;
13918355f576SJeff Roberson 	int cpu;
13928355f576SJeff Roberson 
13938355f576SJeff Roberson 	/*
13945d1ae027SRobert Watson 	 * XXX: It is safe to not lock the per-CPU caches, because we're
13955d1ae027SRobert Watson 	 * tearing down the zone anyway.  I.e., there will be no further use
13965d1ae027SRobert Watson 	 * of the caches at this point.
13975d1ae027SRobert Watson 	 *
13985d1ae027SRobert Watson 	 * XXX: It would good to be able to assert that the zone is being
13995d1ae027SRobert Watson 	 * torn down to prevent improper use of cache_drain().
14008355f576SJeff Roberson 	 */
1401543117beSJeff Roberson 	seq = SMR_SEQ_INVALID;
1402543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
1403226dd6dbSJeff Roberson 		seq = smr_advance(zone->uz_smr);
14043aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
14058355f576SJeff Roberson 		cache = &zone->uz_cpu[cpu];
1406376b1ba3SJeff Roberson 		bucket = cache_bucket_unload_alloc(cache);
1407c6fd3e23SJeff Roberson 		if (bucket != NULL)
1408376b1ba3SJeff Roberson 			bucket_free(zone, bucket, NULL);
1409376b1ba3SJeff Roberson 		bucket = cache_bucket_unload_free(cache);
1410376b1ba3SJeff Roberson 		if (bucket != NULL) {
1411543117beSJeff Roberson 			bucket->ub_seq = seq;
1412376b1ba3SJeff Roberson 			bucket_free(zone, bucket, NULL);
1413376b1ba3SJeff Roberson 		}
1414376b1ba3SJeff Roberson 		bucket = cache_bucket_unload_cross(cache);
1415376b1ba3SJeff Roberson 		if (bucket != NULL) {
1416543117beSJeff Roberson 			bucket->ub_seq = seq;
1417376b1ba3SJeff Roberson 			bucket_free(zone, bucket, NULL);
1418376b1ba3SJeff Roberson 		}
1419d56368d7SBosko Milekic 	}
1420aabe13f1SMark Johnston 	bucket_cache_reclaim(zone, true, UMA_ANYDOMAIN);
1421aaa8bb16SJeff Roberson }
1422aaa8bb16SJeff Roberson 
1423a2de44abSAlexander Motin static void
142420a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused)
1425a2de44abSAlexander Motin {
1426a2de44abSAlexander Motin 
1427a2de44abSAlexander Motin 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
1428a2de44abSAlexander Motin 		return;
1429a2de44abSAlexander Motin 
1430aabe13f1SMark Johnston 	ZONE_LOCK(zone);
143120a4e154SJeff Roberson 	zone->uz_bucket_size =
143220a4e154SJeff Roberson 	    (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2;
1433aabe13f1SMark Johnston 	ZONE_UNLOCK(zone);
1434a2de44abSAlexander Motin }
1435a2de44abSAlexander Motin 
1436a2de44abSAlexander Motin static void
143720a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused)
1438a2de44abSAlexander Motin {
1439a2de44abSAlexander Motin 	uma_cache_t cache;
1440c1685086SJeff Roberson 	uma_bucket_t b1, b2, b3;
1441ab3185d1SJeff Roberson 	int domain;
1442a2de44abSAlexander Motin 
1443a2de44abSAlexander Motin 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
1444a2de44abSAlexander Motin 		return;
1445a2de44abSAlexander Motin 
1446c1685086SJeff Roberson 	b1 = b2 = b3 = NULL;
1447a2de44abSAlexander Motin 	critical_enter();
1448a2de44abSAlexander Motin 	cache = &zone->uz_cpu[curcpu];
1449c6fd3e23SJeff Roberson 	domain = PCPU_GET(domain);
1450376b1ba3SJeff Roberson 	b1 = cache_bucket_unload_alloc(cache);
1451d4665eaaSJeff Roberson 
1452d4665eaaSJeff Roberson 	/*
1453d4665eaaSJeff Roberson 	 * Don't flush SMR zone buckets.  This leaves the zone without a
1454d4665eaaSJeff Roberson 	 * bucket and forces every free to synchronize().
1455d4665eaaSJeff Roberson 	 */
1456543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0) {
1457376b1ba3SJeff Roberson 		b2 = cache_bucket_unload_free(cache);
1458543117beSJeff Roberson 		b3 = cache_bucket_unload_cross(cache);
1459543117beSJeff Roberson 	}
1460543117beSJeff Roberson 	critical_exit();
1461543117beSJeff Roberson 
1462543117beSJeff Roberson 	if (b1 != NULL)
1463c6fd3e23SJeff Roberson 		zone_free_bucket(zone, b1, NULL, domain, false);
1464543117beSJeff Roberson 	if (b2 != NULL)
1465c6fd3e23SJeff Roberson 		zone_free_bucket(zone, b2, NULL, domain, false);
1466543117beSJeff Roberson 	if (b3 != NULL) {
1467c6fd3e23SJeff Roberson 		/* Adjust the domain so it goes to zone_free_cross. */
1468c6fd3e23SJeff Roberson 		domain = (domain + 1) % vm_ndomains;
1469c6fd3e23SJeff Roberson 		zone_free_bucket(zone, b3, NULL, domain, false);
1470c1685086SJeff Roberson 	}
1471a2de44abSAlexander Motin }
1472a2de44abSAlexander Motin 
1473a2de44abSAlexander Motin /*
1474a2de44abSAlexander Motin  * Safely drain per-CPU caches of a zone(s) to alloc bucket.
1475a2de44abSAlexander Motin  * This is an expensive call because it needs to bind to all CPUs
1476a2de44abSAlexander Motin  * one by one and enter a critical section on each of them in order
1477a2de44abSAlexander Motin  * to safely access their cache buckets.
1478a2de44abSAlexander Motin  * Zone lock must not be held on call this function.
1479a2de44abSAlexander Motin  */
1480a2de44abSAlexander Motin static void
148108cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone)
1482a2de44abSAlexander Motin {
1483a2de44abSAlexander Motin 	int cpu;
1484a2de44abSAlexander Motin 
1485a2de44abSAlexander Motin 	/*
1486727c6918SJeff Roberson 	 * Polite bucket sizes shrinking was not enough, shrink aggressively.
1487a2de44abSAlexander Motin 	 */
1488a2de44abSAlexander Motin 	if (zone)
148920a4e154SJeff Roberson 		cache_shrink(zone, NULL);
1490a2de44abSAlexander Motin 	else
149120a4e154SJeff Roberson 		zone_foreach(cache_shrink, NULL);
1492a2de44abSAlexander Motin 
1493a2de44abSAlexander Motin 	CPU_FOREACH(cpu) {
1494a2de44abSAlexander Motin 		thread_lock(curthread);
1495a2de44abSAlexander Motin 		sched_bind(curthread, cpu);
1496a2de44abSAlexander Motin 		thread_unlock(curthread);
1497a2de44abSAlexander Motin 
1498a2de44abSAlexander Motin 		if (zone)
149920a4e154SJeff Roberson 			cache_drain_safe_cpu(zone, NULL);
1500a2de44abSAlexander Motin 		else
150120a4e154SJeff Roberson 			zone_foreach(cache_drain_safe_cpu, NULL);
1502a2de44abSAlexander Motin 	}
1503a2de44abSAlexander Motin 	thread_lock(curthread);
1504a2de44abSAlexander Motin 	sched_unbind(curthread);
1505a2de44abSAlexander Motin 	thread_unlock(curthread);
1506a2de44abSAlexander Motin }
1507a2de44abSAlexander Motin 
1508aaa8bb16SJeff Roberson /*
150908cfa56eSMark Johnston  * Reclaim cached buckets from a zone.  All buckets are reclaimed if the caller
151008cfa56eSMark Johnston  * requested a drain, otherwise the per-domain caches are trimmed to either
151108cfa56eSMark Johnston  * estimated working set size.
1512aaa8bb16SJeff Roberson  */
15132760658bSAlexander Motin static bool
15142760658bSAlexander Motin bucket_cache_reclaim_domain(uma_zone_t zone, bool drain, bool trim, int domain)
1515aaa8bb16SJeff Roberson {
1516ab3185d1SJeff Roberson 	uma_zone_domain_t zdom;
1517aaa8bb16SJeff Roberson 	uma_bucket_t bucket;
1518c6fd3e23SJeff Roberson 	long target;
15192760658bSAlexander Motin 	bool done = false;
15208355f576SJeff Roberson 
1521c6fd3e23SJeff Roberson 	/*
152291d947bfSJeff Roberson 	 * The cross bucket is partially filled and not part of
152391d947bfSJeff Roberson 	 * the item count.  Reclaim it individually here.
152491d947bfSJeff Roberson 	 */
152554f421f9SMark Johnston 	zdom = ZDOM_GET(zone, domain);
1526226dd6dbSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 || drain) {
152791d947bfSJeff Roberson 		ZONE_CROSS_LOCK(zone);
152891d947bfSJeff Roberson 		bucket = zdom->uzd_cross;
152991d947bfSJeff Roberson 		zdom->uzd_cross = NULL;
153091d947bfSJeff Roberson 		ZONE_CROSS_UNLOCK(zone);
1531c6fd3e23SJeff Roberson 		if (bucket != NULL)
153291d947bfSJeff Roberson 			bucket_free(zone, bucket, NULL);
153391d947bfSJeff Roberson 	}
153491d947bfSJeff Roberson 
153591d947bfSJeff Roberson 	/*
153608cfa56eSMark Johnston 	 * If we were asked to drain the zone, we are done only once
15372760658bSAlexander Motin 	 * this bucket cache is empty.  If trim, we reclaim items in
15382760658bSAlexander Motin 	 * excess of the zone's estimated working set size.  Multiple
15392760658bSAlexander Motin 	 * consecutive calls will shrink the WSS and so reclaim more.
15402760658bSAlexander Motin 	 * If neither drain nor trim, then voluntarily reclaim 1/4
15412760658bSAlexander Motin 	 * (to reduce first spike) of items not used for a long time.
154208cfa56eSMark Johnston 	 */
1543c6fd3e23SJeff Roberson 	ZDOM_LOCK(zdom);
15442760658bSAlexander Motin 	zone_domain_update_wss(zdom);
15452760658bSAlexander Motin 	if (drain)
15462760658bSAlexander Motin 		target = 0;
15472760658bSAlexander Motin 	else if (trim)
15482760658bSAlexander Motin 		target = zdom->uzd_wss;
15492760658bSAlexander Motin 	else if (zdom->uzd_timin > 900 / UMA_TIMEOUT)
15502760658bSAlexander Motin 		target = zdom->uzd_nitems - zdom->uzd_limin / 4;
15512760658bSAlexander Motin 	else {
15522760658bSAlexander Motin 		ZDOM_UNLOCK(zdom);
15532760658bSAlexander Motin 		return (done);
15542760658bSAlexander Motin 	}
15552760658bSAlexander Motin 	while ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) != NULL &&
15562760658bSAlexander Motin 	    zdom->uzd_nitems >= target + bucket->ub_cnt) {
1557c6fd3e23SJeff Roberson 		bucket = zone_fetch_bucket(zone, zdom, true);
155808cfa56eSMark Johnston 		if (bucket == NULL)
155908cfa56eSMark Johnston 			break;
15606fd34d6fSJeff Roberson 		bucket_free(zone, bucket, NULL);
15612760658bSAlexander Motin 		done = true;
1562c6fd3e23SJeff Roberson 		ZDOM_LOCK(zdom);
15638355f576SJeff Roberson 	}
1564c6fd3e23SJeff Roberson 	ZDOM_UNLOCK(zdom);
15652760658bSAlexander Motin 	return (done);
1566ab3185d1SJeff Roberson }
156754f421f9SMark Johnston 
156854f421f9SMark Johnston static void
1569aabe13f1SMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain, int domain)
157054f421f9SMark Johnston {
157154f421f9SMark Johnston 	int i;
157254f421f9SMark Johnston 
157354f421f9SMark Johnston 	/*
157454f421f9SMark Johnston 	 * Shrink the zone bucket size to ensure that the per-CPU caches
157554f421f9SMark Johnston 	 * don't grow too large.
157654f421f9SMark Johnston 	 */
157754f421f9SMark Johnston 	if (zone->uz_bucket_size > zone->uz_bucket_size_min)
157854f421f9SMark Johnston 		zone->uz_bucket_size--;
157954f421f9SMark Johnston 
1580aabe13f1SMark Johnston 	if (domain != UMA_ANYDOMAIN &&
1581aabe13f1SMark Johnston 	    (zone->uz_flags & UMA_ZONE_ROUNDROBIN) == 0) {
15822760658bSAlexander Motin 		bucket_cache_reclaim_domain(zone, drain, true, domain);
1583aabe13f1SMark Johnston 	} else {
158454f421f9SMark Johnston 		for (i = 0; i < vm_ndomains; i++)
15852760658bSAlexander Motin 			bucket_cache_reclaim_domain(zone, drain, true, i);
15868355f576SJeff Roberson 	}
1587aabe13f1SMark Johnston }
1588fc03d22bSJeff Roberson 
1589fc03d22bSJeff Roberson static void
1590fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
1591fc03d22bSJeff Roberson {
1592fc03d22bSJeff Roberson 	uint8_t *mem;
159309c8cb71SMark Johnston 	size_t size;
1594fc03d22bSJeff Roberson 	int i;
1595fc03d22bSJeff Roberson 	uint8_t flags;
1596fc03d22bSJeff Roberson 
15971431a748SGleb Smirnoff 	CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes",
15981431a748SGleb Smirnoff 	    keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera);
15991431a748SGleb Smirnoff 
16001e0701e1SJeff Roberson 	mem = slab_data(slab, keg);
160109c8cb71SMark Johnston 	size = PAGE_SIZE * keg->uk_ppera;
160209c8cb71SMark Johnston 
160309c8cb71SMark Johnston 	kasan_mark_slab_valid(keg, mem);
1604fc03d22bSJeff Roberson 	if (keg->uk_fini != NULL) {
160509c8cb71SMark Johnston 		for (i = start - 1; i > -1; i--)
1606c5deaf04SGleb Smirnoff #ifdef INVARIANTS
1607c5deaf04SGleb Smirnoff 		/*
1608c5deaf04SGleb Smirnoff 		 * trash_fini implies that dtor was trash_dtor. trash_fini
1609c5deaf04SGleb Smirnoff 		 * would check that memory hasn't been modified since free,
1610c5deaf04SGleb Smirnoff 		 * which executed trash_dtor.
1611c5deaf04SGleb Smirnoff 		 * That's why we need to run uma_dbg_kskip() check here,
1612c5deaf04SGleb Smirnoff 		 * albeit we don't make skip check for other init/fini
1613c5deaf04SGleb Smirnoff 		 * invocations.
1614c5deaf04SGleb Smirnoff 		 */
16151e0701e1SJeff Roberson 		if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) ||
1616c5deaf04SGleb Smirnoff 		    keg->uk_fini != trash_fini)
1617c5deaf04SGleb Smirnoff #endif
16181e0701e1SJeff Roberson 			keg->uk_fini(slab_item(slab, keg, i), keg->uk_size);
1619fc03d22bSJeff Roberson 	}
162009c8cb71SMark Johnston 	flags = slab->us_flags;
162109c8cb71SMark Johnston 	if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) {
16229b8db4d0SRyan Libby 		zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab),
16239b8db4d0SRyan Libby 		    NULL, SKIP_NONE);
162409c8cb71SMark Johnston 	}
162509c8cb71SMark Johnston 	keg->uk_freef(mem, size, flags);
162609c8cb71SMark Johnston 	uma_total_dec(size);
16278355f576SJeff Roberson }
16288355f576SJeff Roberson 
1629f09cbea3SMark Johnston static void
1630f09cbea3SMark Johnston keg_drain_domain(uma_keg_t keg, int domain)
1631f09cbea3SMark Johnston {
1632f09cbea3SMark Johnston 	struct slabhead freeslabs;
1633f09cbea3SMark Johnston 	uma_domain_t dom;
1634f09cbea3SMark Johnston 	uma_slab_t slab, tmp;
1635f09cbea3SMark Johnston 	uint32_t i, stofree, stokeep, partial;
1636f09cbea3SMark Johnston 
1637f09cbea3SMark Johnston 	dom = &keg->uk_domain[domain];
1638f09cbea3SMark Johnston 	LIST_INIT(&freeslabs);
1639f09cbea3SMark Johnston 
1640f09cbea3SMark Johnston 	CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u",
1641575a4437SEd Maste 	    keg->uk_name, keg, domain, dom->ud_free_items);
1642f09cbea3SMark Johnston 
1643f09cbea3SMark Johnston 	KEG_LOCK(keg, domain);
1644f09cbea3SMark Johnston 
1645f09cbea3SMark Johnston 	/*
1646f09cbea3SMark Johnston 	 * Are the free items in partially allocated slabs sufficient to meet
1647f09cbea3SMark Johnston 	 * the reserve? If not, compute the number of fully free slabs that must
1648f09cbea3SMark Johnston 	 * be kept.
1649f09cbea3SMark Johnston 	 */
1650f09cbea3SMark Johnston 	partial = dom->ud_free_items - dom->ud_free_slabs * keg->uk_ipers;
1651f09cbea3SMark Johnston 	if (partial < keg->uk_reserve) {
1652f09cbea3SMark Johnston 		stokeep = min(dom->ud_free_slabs,
1653f09cbea3SMark Johnston 		    howmany(keg->uk_reserve - partial, keg->uk_ipers));
1654f09cbea3SMark Johnston 	} else {
1655f09cbea3SMark Johnston 		stokeep = 0;
1656f09cbea3SMark Johnston 	}
1657f09cbea3SMark Johnston 	stofree = dom->ud_free_slabs - stokeep;
1658f09cbea3SMark Johnston 
1659f09cbea3SMark Johnston 	/*
1660f09cbea3SMark Johnston 	 * Partition the free slabs into two sets: those that must be kept in
1661f09cbea3SMark Johnston 	 * order to maintain the reserve, and those that may be released back to
1662f09cbea3SMark Johnston 	 * the system.  Since one set may be much larger than the other,
1663f09cbea3SMark Johnston 	 * populate the smaller of the two sets and swap them if necessary.
1664f09cbea3SMark Johnston 	 */
1665f09cbea3SMark Johnston 	for (i = min(stofree, stokeep); i > 0; i--) {
1666f09cbea3SMark Johnston 		slab = LIST_FIRST(&dom->ud_free_slab);
1667f09cbea3SMark Johnston 		LIST_REMOVE(slab, us_link);
1668f09cbea3SMark Johnston 		LIST_INSERT_HEAD(&freeslabs, slab, us_link);
1669f09cbea3SMark Johnston 	}
1670f09cbea3SMark Johnston 	if (stofree > stokeep)
1671f09cbea3SMark Johnston 		LIST_SWAP(&freeslabs, &dom->ud_free_slab, uma_slab, us_link);
1672f09cbea3SMark Johnston 
1673f09cbea3SMark Johnston 	if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) {
1674f09cbea3SMark Johnston 		LIST_FOREACH(slab, &freeslabs, us_link)
1675f09cbea3SMark Johnston 			UMA_HASH_REMOVE(&keg->uk_hash, slab);
1676f09cbea3SMark Johnston 	}
1677f09cbea3SMark Johnston 	dom->ud_free_items -= stofree * keg->uk_ipers;
1678f09cbea3SMark Johnston 	dom->ud_free_slabs -= stofree;
1679f09cbea3SMark Johnston 	dom->ud_pages -= stofree * keg->uk_ppera;
1680f09cbea3SMark Johnston 	KEG_UNLOCK(keg, domain);
1681f09cbea3SMark Johnston 
1682f09cbea3SMark Johnston 	LIST_FOREACH_SAFE(slab, &freeslabs, us_link, tmp)
1683f09cbea3SMark Johnston 		keg_free_slab(keg, slab, keg->uk_ipers);
1684f09cbea3SMark Johnston }
1685f09cbea3SMark Johnston 
16868355f576SJeff Roberson /*
1687e20a199fSJeff Roberson  * Frees pages from a keg back to the system.  This is done on demand from
16888355f576SJeff Roberson  * the pageout daemon.
16898355f576SJeff Roberson  *
1690e20a199fSJeff Roberson  * Returns nothing.
16918355f576SJeff Roberson  */
1692e20a199fSJeff Roberson static void
1693aabe13f1SMark Johnston keg_drain(uma_keg_t keg, int domain)
16948355f576SJeff Roberson {
1695f09cbea3SMark Johnston 	int i;
16968355f576SJeff Roberson 
1697f09cbea3SMark Johnston 	if ((keg->uk_flags & UMA_ZONE_NOFREE) != 0)
16988355f576SJeff Roberson 		return;
1699aabe13f1SMark Johnston 	if (domain != UMA_ANYDOMAIN) {
1700aabe13f1SMark Johnston 		keg_drain_domain(keg, domain);
1701aabe13f1SMark Johnston 	} else {
1702f09cbea3SMark Johnston 		for (i = 0; i < vm_ndomains; i++)
1703f09cbea3SMark Johnston 			keg_drain_domain(keg, i);
17048355f576SJeff Roberson 	}
1705aabe13f1SMark Johnston }
17068355f576SJeff Roberson 
1707e20a199fSJeff Roberson static void
1708aabe13f1SMark Johnston zone_reclaim(uma_zone_t zone, int domain, int waitok, bool drain)
1709e20a199fSJeff Roberson {
17108355f576SJeff Roberson 	/*
1711aabe13f1SMark Johnston 	 * Count active reclaim operations in order to interlock with
1712aabe13f1SMark Johnston 	 * zone_dtor(), which removes the zone from global lists before
1713aabe13f1SMark Johnston 	 * attempting to reclaim items itself.
1714aabe13f1SMark Johnston 	 *
1715aabe13f1SMark Johnston 	 * The zone may be destroyed while sleeping, so only zone_dtor() should
1716aabe13f1SMark Johnston 	 * specify M_WAITOK.
1717e20a199fSJeff Roberson 	 */
1718e20a199fSJeff Roberson 	ZONE_LOCK(zone);
1719aabe13f1SMark Johnston 	if (waitok == M_WAITOK) {
1720aabe13f1SMark Johnston 		while (zone->uz_reclaimers > 0)
1721aabe13f1SMark Johnston 			msleep(zone, ZONE_LOCKPTR(zone), PVM, "zonedrain", 1);
1722e20a199fSJeff Roberson 	}
1723aabe13f1SMark Johnston 	zone->uz_reclaimers++;
1724e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
1725aabe13f1SMark Johnston 	bucket_cache_reclaim(zone, drain, domain);
172608cfa56eSMark Johnston 
172708034d10SKonstantin Belousov 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0)
1728aabe13f1SMark Johnston 		keg_drain(zone->uz_keg, domain);
1729e20a199fSJeff Roberson 	ZONE_LOCK(zone);
1730aabe13f1SMark Johnston 	zone->uz_reclaimers--;
1731aabe13f1SMark Johnston 	if (zone->uz_reclaimers == 0)
1732e20a199fSJeff Roberson 		wakeup(zone);
1733e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
1734e20a199fSJeff Roberson }
1735e20a199fSJeff Roberson 
173608cfa56eSMark Johnston static void
1737aabe13f1SMark Johnston zone_drain(uma_zone_t zone, void *arg)
1738e20a199fSJeff Roberson {
1739aabe13f1SMark Johnston 	int domain;
1740e20a199fSJeff Roberson 
1741aabe13f1SMark Johnston 	domain = (int)(uintptr_t)arg;
1742aabe13f1SMark Johnston 	zone_reclaim(zone, domain, M_NOWAIT, true);
174308cfa56eSMark Johnston }
174408cfa56eSMark Johnston 
174508cfa56eSMark Johnston static void
1746aabe13f1SMark Johnston zone_trim(uma_zone_t zone, void *arg)
174708cfa56eSMark Johnston {
1748aabe13f1SMark Johnston 	int domain;
174908cfa56eSMark Johnston 
1750aabe13f1SMark Johnston 	domain = (int)(uintptr_t)arg;
1751aabe13f1SMark Johnston 	zone_reclaim(zone, domain, M_NOWAIT, false);
1752e20a199fSJeff Roberson }
1753e20a199fSJeff Roberson 
1754e20a199fSJeff Roberson /*
17558b987a77SJeff Roberson  * Allocate a new slab for a keg and inserts it into the partial slab list.
17568b987a77SJeff Roberson  * The keg should be unlocked on entry.  If the allocation succeeds it will
17578b987a77SJeff Roberson  * be locked on return.
17588355f576SJeff Roberson  *
17598355f576SJeff Roberson  * Arguments:
176086220393SMark Johnston  *	flags   Wait flags for the item initialization routine
176186220393SMark Johnston  *	aflags  Wait flags for the slab allocation
17628355f576SJeff Roberson  *
17638355f576SJeff Roberson  * Returns:
17648355f576SJeff Roberson  *	The slab that was allocated or NULL if there is no memory and the
17658355f576SJeff Roberson  *	caller specified M_NOWAIT.
17668355f576SJeff Roberson  */
17678355f576SJeff Roberson static uma_slab_t
176886220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags,
176986220393SMark Johnston     int aflags)
17708355f576SJeff Roberson {
17718b987a77SJeff Roberson 	uma_domain_t dom;
1772099a0e58SBosko Milekic 	uma_slab_t slab;
17732e47807cSJeff Roberson 	unsigned long size;
177485dcf349SGleb Smirnoff 	uint8_t *mem;
177586220393SMark Johnston 	uint8_t sflags;
17768355f576SJeff Roberson 	int i;
17778355f576SJeff Roberson 
1778ab3185d1SJeff Roberson 	KASSERT(domain >= 0 && domain < vm_ndomains,
1779ab3185d1SJeff Roberson 	    ("keg_alloc_slab: domain %d out of range", domain));
1780a553d4b8SJeff Roberson 
1781194a979eSMark Johnston 	slab = NULL;
1782194a979eSMark Johnston 	mem = NULL;
178354c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) {
17849b8db4d0SRyan Libby 		uma_hash_slab_t hslab;
17859b8db4d0SRyan Libby 		hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL,
17869b8db4d0SRyan Libby 		    domain, aflags);
17879b8db4d0SRyan Libby 		if (hslab == NULL)
1788727c6918SJeff Roberson 			goto fail;
17899b8db4d0SRyan Libby 		slab = &hslab->uhs_slab;
1790a553d4b8SJeff Roberson 	}
1791a553d4b8SJeff Roberson 
17923370c5bfSJeff Roberson 	/*
17933370c5bfSJeff Roberson 	 * This reproduces the old vm_zone behavior of zero filling pages the
17943370c5bfSJeff Roberson 	 * first time they are added to a zone.
17953370c5bfSJeff Roberson 	 *
17963370c5bfSJeff Roberson 	 * Malloced items are zeroed in uma_zalloc.
17973370c5bfSJeff Roberson 	 */
17983370c5bfSJeff Roberson 
1799099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
180086220393SMark Johnston 		aflags |= M_ZERO;
18013370c5bfSJeff Roberson 	else
180286220393SMark Johnston 		aflags &= ~M_ZERO;
18033370c5bfSJeff Roberson 
1804263811f7SKip Macy 	if (keg->uk_flags & UMA_ZONE_NODUMP)
180586220393SMark Johnston 		aflags |= M_NODUMP;
1806263811f7SKip Macy 
1807e20a199fSJeff Roberson 	/* zone is passed for legacy reasons. */
1808194a979eSMark Johnston 	size = keg->uk_ppera * PAGE_SIZE;
180909c8cb71SMark Johnston 	mem = keg->uk_allocf(zone, size, domain, &sflags, aflags);
1810a553d4b8SJeff Roberson 	if (mem == NULL) {
181154c5ae80SRyan Libby 		if (keg->uk_flags & UMA_ZFLAG_OFFPAGE)
18129b8db4d0SRyan Libby 			zone_free_item(slabzone(keg->uk_ipers),
18139b8db4d0SRyan Libby 			    slab_tohashslab(slab), NULL, SKIP_NONE);
1814727c6918SJeff Roberson 		goto fail;
1815a553d4b8SJeff Roberson 	}
18162e47807cSJeff Roberson 	uma_total_inc(size);
18178355f576SJeff Roberson 
18188b987a77SJeff Roberson 	/* For HASH zones all pages go to the same uma_domain. */
181954c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0)
18208b987a77SJeff Roberson 		domain = 0;
18218b987a77SJeff Roberson 
18225c0e403bSJeff Roberson 	/* Point the slab into the allocated memory */
182354c5ae80SRyan Libby 	if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE))
1824099a0e58SBosko Milekic 		slab = (uma_slab_t)(mem + keg->uk_pgoff);
18251e0701e1SJeff Roberson 	else
18269b8db4d0SRyan Libby 		slab_tohashslab(slab)->uhs_data = mem;
18275c0e403bSJeff Roberson 
182854c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_VTOSLAB)
1829099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ppera; i++)
1830584061b4SJeff Roberson 			vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE),
1831584061b4SJeff Roberson 			    zone, slab);
18328355f576SJeff Roberson 
1833099a0e58SBosko Milekic 	slab->us_freecount = keg->uk_ipers;
183486220393SMark Johnston 	slab->us_flags = sflags;
1835ab3185d1SJeff Roberson 	slab->us_domain = domain;
18368b987a77SJeff Roberson 
18379b78b1f4SJeff Roberson 	BIT_FILL(keg->uk_ipers, &slab->us_free);
1838ef72505eSJeff Roberson #ifdef INVARIANTS
1839815db204SRyan Libby 	BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg));
1840ef72505eSJeff Roberson #endif
1841099a0e58SBosko Milekic 
1842b23f72e9SBrian Feldman 	if (keg->uk_init != NULL) {
1843099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ipers; i++)
18441e0701e1SJeff Roberson 			if (keg->uk_init(slab_item(slab, keg, i),
184586220393SMark Johnston 			    keg->uk_size, flags) != 0)
1846b23f72e9SBrian Feldman 				break;
1847b23f72e9SBrian Feldman 		if (i != keg->uk_ipers) {
1848fc03d22bSJeff Roberson 			keg_free_slab(keg, slab, i);
1849727c6918SJeff Roberson 			goto fail;
1850b23f72e9SBrian Feldman 		}
1851b23f72e9SBrian Feldman 	}
185209c8cb71SMark Johnston 	kasan_mark_slab_invalid(keg, mem);
18538b987a77SJeff Roberson 	KEG_LOCK(keg, domain);
18545c0e403bSJeff Roberson 
18551431a748SGleb Smirnoff 	CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)",
18561431a748SGleb Smirnoff 	    slab, keg->uk_name, keg);
18571431a748SGleb Smirnoff 
185854c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_HASH)
1859099a0e58SBosko Milekic 		UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
18608355f576SJeff Roberson 
18618b987a77SJeff Roberson 	/*
18628b987a77SJeff Roberson 	 * If we got a slab here it's safe to mark it partially used
18638b987a77SJeff Roberson 	 * and return.  We assume that the caller is going to remove
18648b987a77SJeff Roberson 	 * at least one item.
18658b987a77SJeff Roberson 	 */
18668b987a77SJeff Roberson 	dom = &keg->uk_domain[domain];
18678b987a77SJeff Roberson 	LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
18688b987a77SJeff Roberson 	dom->ud_pages += keg->uk_ppera;
18694ab3aee8SMark Johnston 	dom->ud_free_items += keg->uk_ipers;
18708355f576SJeff Roberson 
18718355f576SJeff Roberson 	return (slab);
1872727c6918SJeff Roberson 
1873727c6918SJeff Roberson fail:
1874727c6918SJeff Roberson 	return (NULL);
18758355f576SJeff Roberson }
18768355f576SJeff Roberson 
18778355f576SJeff Roberson /*
1878537f92cdSMark Johnston  * This function is intended to be used early on in place of page_alloc().  It
1879537f92cdSMark Johnston  * performs contiguous physical memory allocations and uses a bump allocator for
1880537f92cdSMark Johnston  * KVA, so is usable before the kernel map is initialized.
1881009b6fcbSJeff Roberson  */
1882009b6fcbSJeff Roberson static void *
1883ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1884ab3185d1SJeff Roberson     int wait)
1885009b6fcbSJeff Roberson {
1886a81c400eSJeff Roberson 	vm_paddr_t pa;
1887a81c400eSJeff Roberson 	vm_page_t m;
1888ac0a6fd0SGleb Smirnoff 	void *mem;
1889ac0a6fd0SGleb Smirnoff 	int pages;
1890a81c400eSJeff Roberson 	int i;
1891099a0e58SBosko Milekic 
1892f7d35785SGleb Smirnoff 	pages = howmany(bytes, PAGE_SIZE);
1893f7d35785SGleb Smirnoff 	KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__));
1894a81c400eSJeff Roberson 
1895f7d35785SGleb Smirnoff 	*pflag = UMA_SLAB_BOOT;
1896a81c400eSJeff Roberson 	m = vm_page_alloc_contig_domain(NULL, 0, domain,
1897a81c400eSJeff Roberson 	    malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages,
1898a81c400eSJeff Roberson 	    (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT);
1899a81c400eSJeff Roberson 	if (m == NULL)
1900a81c400eSJeff Roberson 		return (NULL);
1901a81c400eSJeff Roberson 
1902a81c400eSJeff Roberson 	pa = VM_PAGE_TO_PHYS(m);
1903a81c400eSJeff Roberson 	for (i = 0; i < pages; i++, pa += PAGE_SIZE) {
1904a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
1905a81c400eSJeff Roberson     defined(__riscv) || defined(__powerpc64__)
1906a81c400eSJeff Roberson 		if ((wait & M_NODUMP) == 0)
1907a81c400eSJeff Roberson 			dump_add_page(pa);
1908a81c400eSJeff Roberson #endif
1909a81c400eSJeff Roberson 	}
1910a81c400eSJeff Roberson 	/* Allocate KVA and indirectly advance bootmem. */
1911a81c400eSJeff Roberson 	mem = (void *)pmap_map(&bootmem, m->phys_addr,
1912a81c400eSJeff Roberson 	    m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE);
1913a81c400eSJeff Roberson         if ((wait & M_ZERO) != 0)
1914a81c400eSJeff Roberson                 bzero(mem, pages * PAGE_SIZE);
1915f7d35785SGleb Smirnoff 
1916f7d35785SGleb Smirnoff         return (mem);
1917f7d35785SGleb Smirnoff }
1918f7d35785SGleb Smirnoff 
1919a81c400eSJeff Roberson static void
1920a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes)
1921a81c400eSJeff Roberson {
1922a81c400eSJeff Roberson 	vm_offset_t va;
1923a81c400eSJeff Roberson 	vm_page_t m;
1924a81c400eSJeff Roberson 
1925a81c400eSJeff Roberson 	va = (vm_offset_t)mem;
1926a81c400eSJeff Roberson 	m = PHYS_TO_VM_PAGE(pmap_kextract(va));
1927663de81fSMark Johnston 
1928663de81fSMark Johnston 	/*
1929663de81fSMark Johnston 	 * startup_alloc() returns direct-mapped slabs on some platforms.  Avoid
1930663de81fSMark Johnston 	 * unmapping ranges of the direct map.
1931663de81fSMark Johnston 	 */
1932663de81fSMark Johnston 	if (va >= bootstart && va + bytes <= bootmem)
1933a81c400eSJeff Roberson 		pmap_remove(kernel_pmap, va, va + bytes);
1934a81c400eSJeff Roberson 	for (; bytes != 0; bytes -= PAGE_SIZE, m++) {
1935a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
1936a81c400eSJeff Roberson     defined(__riscv) || defined(__powerpc64__)
1937a81c400eSJeff Roberson 		dump_drop_page(VM_PAGE_TO_PHYS(m));
1938a81c400eSJeff Roberson #endif
1939a81c400eSJeff Roberson 		vm_page_unwire_noq(m);
1940a81c400eSJeff Roberson 		vm_page_free(m);
1941a81c400eSJeff Roberson 	}
1942a81c400eSJeff Roberson }
1943a81c400eSJeff Roberson 
1944f7d35785SGleb Smirnoff /*
19458355f576SJeff Roberson  * Allocates a number of pages from the system
19468355f576SJeff Roberson  *
19478355f576SJeff Roberson  * Arguments:
19488355f576SJeff Roberson  *	bytes  The number of bytes requested
19498355f576SJeff Roberson  *	wait  Shall we wait?
19508355f576SJeff Roberson  *
19518355f576SJeff Roberson  * Returns:
19528355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
19538355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
19548355f576SJeff Roberson  */
19558355f576SJeff Roberson static void *
1956ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1957ab3185d1SJeff Roberson     int wait)
19588355f576SJeff Roberson {
19598355f576SJeff Roberson 	void *p;	/* Returned page */
19608355f576SJeff Roberson 
19612e47807cSJeff Roberson 	*pflag = UMA_SLAB_KERNEL;
19629978bd99SMark Johnston 	p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait);
19638355f576SJeff Roberson 
19648355f576SJeff Roberson 	return (p);
19658355f576SJeff Roberson }
19668355f576SJeff Roberson 
1967ab3059a8SMatt Macy static void *
1968ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1969ab3059a8SMatt Macy     int wait)
1970ab3059a8SMatt Macy {
1971ab3059a8SMatt Macy 	struct pglist alloctail;
1972ab3059a8SMatt Macy 	vm_offset_t addr, zkva;
1973ab3059a8SMatt Macy 	int cpu, flags;
1974ab3059a8SMatt Macy 	vm_page_t p, p_next;
1975ab3059a8SMatt Macy #ifdef NUMA
1976ab3059a8SMatt Macy 	struct pcpu *pc;
1977ab3059a8SMatt Macy #endif
1978ab3059a8SMatt Macy 
1979ab3059a8SMatt Macy 	MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE);
1980ab3059a8SMatt Macy 
1981013072f0SMark Johnston 	TAILQ_INIT(&alloctail);
1982ab3059a8SMatt Macy 	flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1983013072f0SMark Johnston 	    malloc2vm_flags(wait);
1984013072f0SMark Johnston 	*pflag = UMA_SLAB_KERNEL;
1985ab3059a8SMatt Macy 	for (cpu = 0; cpu <= mp_maxid; cpu++) {
1986ab3059a8SMatt Macy 		if (CPU_ABSENT(cpu)) {
1987ab3059a8SMatt Macy 			p = vm_page_alloc(NULL, 0, flags);
1988ab3059a8SMatt Macy 		} else {
1989ab3059a8SMatt Macy #ifndef NUMA
1990ab3059a8SMatt Macy 			p = vm_page_alloc(NULL, 0, flags);
1991ab3059a8SMatt Macy #else
1992ab3059a8SMatt Macy 			pc = pcpu_find(cpu);
199320526802SAndrew Gallatin 			if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain)))
199420526802SAndrew Gallatin 				p = NULL;
199520526802SAndrew Gallatin 			else
199620526802SAndrew Gallatin 				p = vm_page_alloc_domain(NULL, 0,
199720526802SAndrew Gallatin 				    pc->pc_domain, flags);
1998ab3059a8SMatt Macy 			if (__predict_false(p == NULL))
1999ab3059a8SMatt Macy 				p = vm_page_alloc(NULL, 0, flags);
2000ab3059a8SMatt Macy #endif
2001ab3059a8SMatt Macy 		}
2002ab3059a8SMatt Macy 		if (__predict_false(p == NULL))
2003ab3059a8SMatt Macy 			goto fail;
2004ab3059a8SMatt Macy 		TAILQ_INSERT_TAIL(&alloctail, p, listq);
2005ab3059a8SMatt Macy 	}
2006ab3059a8SMatt Macy 	if ((addr = kva_alloc(bytes)) == 0)
2007ab3059a8SMatt Macy 		goto fail;
2008ab3059a8SMatt Macy 	zkva = addr;
2009ab3059a8SMatt Macy 	TAILQ_FOREACH(p, &alloctail, listq) {
2010ab3059a8SMatt Macy 		pmap_qenter(zkva, &p, 1);
2011ab3059a8SMatt Macy 		zkva += PAGE_SIZE;
2012ab3059a8SMatt Macy 	}
2013ab3059a8SMatt Macy 	return ((void*)addr);
2014ab3059a8SMatt Macy fail:
2015ab3059a8SMatt Macy 	TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
201688ea538aSMark Johnston 		vm_page_unwire_noq(p);
2017ab3059a8SMatt Macy 		vm_page_free(p);
2018ab3059a8SMatt Macy 	}
2019ab3059a8SMatt Macy 	return (NULL);
2020ab3059a8SMatt Macy }
2021ab3059a8SMatt Macy 
20228355f576SJeff Roberson /*
20238355f576SJeff Roberson  * Allocates a number of pages from within an object
20248355f576SJeff Roberson  *
20258355f576SJeff Roberson  * Arguments:
20268355f576SJeff Roberson  *	bytes  The number of bytes requested
20278355f576SJeff Roberson  *	wait   Shall we wait?
20288355f576SJeff Roberson  *
20298355f576SJeff Roberson  * Returns:
20308355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
20318355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
20328355f576SJeff Roberson  */
20338355f576SJeff Roberson static void *
2034ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
2035ab3185d1SJeff Roberson     int wait)
20368355f576SJeff Roberson {
2037a4915c21SAttilio Rao 	TAILQ_HEAD(, vm_page) alloctail;
2038a4915c21SAttilio Rao 	u_long npages;
2039b245ac95SAlan Cox 	vm_offset_t retkva, zkva;
2040a4915c21SAttilio Rao 	vm_page_t p, p_next;
2041e20a199fSJeff Roberson 	uma_keg_t keg;
20428355f576SJeff Roberson 
2043a4915c21SAttilio Rao 	TAILQ_INIT(&alloctail);
2044bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
2045a4915c21SAttilio Rao 
2046a4915c21SAttilio Rao 	npages = howmany(bytes, PAGE_SIZE);
2047a4915c21SAttilio Rao 	while (npages > 0) {
2048ab3185d1SJeff Roberson 		p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT |
20498d6fbbb8SJeff Roberson 		    VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
2050772c8b67SKonstantin Belousov 		    ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK :
2051772c8b67SKonstantin Belousov 		    VM_ALLOC_NOWAIT));
2052a4915c21SAttilio Rao 		if (p != NULL) {
2053a4915c21SAttilio Rao 			/*
2054a4915c21SAttilio Rao 			 * Since the page does not belong to an object, its
2055a4915c21SAttilio Rao 			 * listq is unused.
2056a4915c21SAttilio Rao 			 */
2057a4915c21SAttilio Rao 			TAILQ_INSERT_TAIL(&alloctail, p, listq);
2058a4915c21SAttilio Rao 			npages--;
2059a4915c21SAttilio Rao 			continue;
2060a4915c21SAttilio Rao 		}
20618355f576SJeff Roberson 		/*
2062a4915c21SAttilio Rao 		 * Page allocation failed, free intermediate pages and
2063a4915c21SAttilio Rao 		 * exit.
20648355f576SJeff Roberson 		 */
2065a4915c21SAttilio Rao 		TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
206688ea538aSMark Johnston 			vm_page_unwire_noq(p);
2067b245ac95SAlan Cox 			vm_page_free(p);
2068b245ac95SAlan Cox 		}
2069a4915c21SAttilio Rao 		return (NULL);
2070b245ac95SAlan Cox 	}
20718355f576SJeff Roberson 	*flags = UMA_SLAB_PRIV;
2072a4915c21SAttilio Rao 	zkva = keg->uk_kva +
2073a4915c21SAttilio Rao 	    atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
2074a4915c21SAttilio Rao 	retkva = zkva;
2075a4915c21SAttilio Rao 	TAILQ_FOREACH(p, &alloctail, listq) {
2076a4915c21SAttilio Rao 		pmap_qenter(zkva, &p, 1);
2077a4915c21SAttilio Rao 		zkva += PAGE_SIZE;
2078a4915c21SAttilio Rao 	}
20798355f576SJeff Roberson 
20808355f576SJeff Roberson 	return ((void *)retkva);
20818355f576SJeff Roberson }
20828355f576SJeff Roberson 
20838355f576SJeff Roberson /*
2084ec0d8280SRyan Libby  * Allocate physically contiguous pages.
2085ec0d8280SRyan Libby  */
2086ec0d8280SRyan Libby static void *
2087ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
2088ec0d8280SRyan Libby     int wait)
2089ec0d8280SRyan Libby {
2090ec0d8280SRyan Libby 
2091ec0d8280SRyan Libby 	*pflag = UMA_SLAB_KERNEL;
2092ec0d8280SRyan Libby 	return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain),
2093ec0d8280SRyan Libby 	    bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT));
2094ec0d8280SRyan Libby }
2095ec0d8280SRyan Libby 
2096ec0d8280SRyan Libby /*
20978355f576SJeff Roberson  * Frees a number of pages to the system
20988355f576SJeff Roberson  *
20998355f576SJeff Roberson  * Arguments:
21008355f576SJeff Roberson  *	mem   A pointer to the memory to be freed
21018355f576SJeff Roberson  *	size  The size of the memory being freed
21028355f576SJeff Roberson  *	flags The original p->us_flags field
21038355f576SJeff Roberson  *
21048355f576SJeff Roberson  * Returns:
21058355f576SJeff Roberson  *	Nothing
21068355f576SJeff Roberson  */
21078355f576SJeff Roberson static void
2108f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags)
21098355f576SJeff Roberson {
21103370c5bfSJeff Roberson 
2111a81c400eSJeff Roberson 	if ((flags & UMA_SLAB_BOOT) != 0) {
2112a81c400eSJeff Roberson 		startup_free(mem, size);
2113a81c400eSJeff Roberson 		return;
2114a81c400eSJeff Roberson 	}
2115a81c400eSJeff Roberson 
2116ec0d8280SRyan Libby 	KASSERT((flags & UMA_SLAB_KERNEL) != 0,
2117ec0d8280SRyan Libby 	    ("UMA: page_free used with invalid flags %x", flags));
21188355f576SJeff Roberson 
211949bfa624SAlan Cox 	kmem_free((vm_offset_t)mem, size);
21208355f576SJeff Roberson }
21218355f576SJeff Roberson 
21228355f576SJeff Roberson /*
2123ab3059a8SMatt Macy  * Frees pcpu zone allocations
2124ab3059a8SMatt Macy  *
2125ab3059a8SMatt Macy  * Arguments:
2126ab3059a8SMatt Macy  *	mem   A pointer to the memory to be freed
2127ab3059a8SMatt Macy  *	size  The size of the memory being freed
2128ab3059a8SMatt Macy  *	flags The original p->us_flags field
2129ab3059a8SMatt Macy  *
2130ab3059a8SMatt Macy  * Returns:
2131ab3059a8SMatt Macy  *	Nothing
2132ab3059a8SMatt Macy  */
2133ab3059a8SMatt Macy static void
2134ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags)
2135ab3059a8SMatt Macy {
2136ab3059a8SMatt Macy 	vm_offset_t sva, curva;
2137ab3059a8SMatt Macy 	vm_paddr_t paddr;
2138ab3059a8SMatt Macy 	vm_page_t m;
2139ab3059a8SMatt Macy 
2140ab3059a8SMatt Macy 	MPASS(size == (mp_maxid+1)*PAGE_SIZE);
21415ba16cf3SRyan Libby 
21425ba16cf3SRyan Libby 	if ((flags & UMA_SLAB_BOOT) != 0) {
21435ba16cf3SRyan Libby 		startup_free(mem, size);
21445ba16cf3SRyan Libby 		return;
21455ba16cf3SRyan Libby 	}
21465ba16cf3SRyan Libby 
2147ab3059a8SMatt Macy 	sva = (vm_offset_t)mem;
2148ab3059a8SMatt Macy 	for (curva = sva; curva < sva + size; curva += PAGE_SIZE) {
2149ab3059a8SMatt Macy 		paddr = pmap_kextract(curva);
2150ab3059a8SMatt Macy 		m = PHYS_TO_VM_PAGE(paddr);
215188ea538aSMark Johnston 		vm_page_unwire_noq(m);
2152ab3059a8SMatt Macy 		vm_page_free(m);
2153ab3059a8SMatt Macy 	}
2154ab3059a8SMatt Macy 	pmap_qremove(sva, size >> PAGE_SHIFT);
2155ab3059a8SMatt Macy 	kva_free(sva, size);
2156ab3059a8SMatt Macy }
2157ab3059a8SMatt Macy 
2158ab3059a8SMatt Macy /*
21598355f576SJeff Roberson  * Zero fill initializer
21608355f576SJeff Roberson  *
21618355f576SJeff Roberson  * Arguments/Returns follow uma_init specifications
21628355f576SJeff Roberson  */
2163b23f72e9SBrian Feldman static int
2164b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags)
21658355f576SJeff Roberson {
21668355f576SJeff Roberson 	bzero(mem, size);
2167b23f72e9SBrian Feldman 	return (0);
21688355f576SJeff Roberson }
21698355f576SJeff Roberson 
2170815db204SRyan Libby #ifdef INVARIANTS
217154007ce8SMark Johnston static struct noslabbits *
2172815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg)
2173815db204SRyan Libby {
2174815db204SRyan Libby 
2175815db204SRyan Libby 	return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers)));
2176815db204SRyan Libby }
2177815db204SRyan Libby #endif
2178815db204SRyan Libby 
21798355f576SJeff Roberson /*
21809b78b1f4SJeff Roberson  * Actual size of embedded struct slab (!OFFPAGE).
21819b78b1f4SJeff Roberson  */
218254007ce8SMark Johnston static size_t
21839b78b1f4SJeff Roberson slab_sizeof(int nitems)
21849b78b1f4SJeff Roberson {
21859b78b1f4SJeff Roberson 	size_t s;
21869b78b1f4SJeff Roberson 
2187815db204SRyan Libby 	s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS;
21889b78b1f4SJeff Roberson 	return (roundup(s, UMA_ALIGN_PTR + 1));
21899b78b1f4SJeff Roberson }
21909b78b1f4SJeff Roberson 
21914a8b575cSRyan Libby #define	UMA_FIXPT_SHIFT	31
21924a8b575cSRyan Libby #define	UMA_FRAC_FIXPT(n, d)						\
21934a8b575cSRyan Libby 	((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d)))
21944a8b575cSRyan Libby #define	UMA_FIXPT_PCT(f)						\
21954a8b575cSRyan Libby 	((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT))
21964a8b575cSRyan Libby #define	UMA_PCT_FIXPT(pct)	UMA_FRAC_FIXPT((pct), 100)
21974a8b575cSRyan Libby #define	UMA_MIN_EFF	UMA_PCT_FIXPT(100 - UMA_MAX_WASTE)
21984a8b575cSRyan Libby 
21999b78b1f4SJeff Roberson /*
22004a8b575cSRyan Libby  * Compute the number of items that will fit in a slab.  If hdr is true, the
22014a8b575cSRyan Libby  * item count may be limited to provide space in the slab for an inline slab
22024a8b575cSRyan Libby  * header.  Otherwise, all slab space will be provided for item storage.
22034a8b575cSRyan Libby  */
22044a8b575cSRyan Libby static u_int
22054a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr)
22064a8b575cSRyan Libby {
22074a8b575cSRyan Libby 	u_int ipers;
22084a8b575cSRyan Libby 	u_int padpi;
22094a8b575cSRyan Libby 
22104a8b575cSRyan Libby 	/* The padding between items is not needed after the last item. */
22114a8b575cSRyan Libby 	padpi = rsize - size;
22124a8b575cSRyan Libby 
22134a8b575cSRyan Libby 	if (hdr) {
22144a8b575cSRyan Libby 		/*
22154a8b575cSRyan Libby 		 * Start with the maximum item count and remove items until
22164a8b575cSRyan Libby 		 * the slab header first alongside the allocatable memory.
22174a8b575cSRyan Libby 		 */
22184a8b575cSRyan Libby 		for (ipers = MIN(SLAB_MAX_SETSIZE,
22194a8b575cSRyan Libby 		    (slabsize + padpi - slab_sizeof(1)) / rsize);
22204a8b575cSRyan Libby 		    ipers > 0 &&
22214a8b575cSRyan Libby 		    ipers * rsize - padpi + slab_sizeof(ipers) > slabsize;
22224a8b575cSRyan Libby 		    ipers--)
22234a8b575cSRyan Libby 			continue;
22244a8b575cSRyan Libby 	} else {
22254a8b575cSRyan Libby 		ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE);
22264a8b575cSRyan Libby 	}
22274a8b575cSRyan Libby 
22284a8b575cSRyan Libby 	return (ipers);
22294a8b575cSRyan Libby }
22304a8b575cSRyan Libby 
223127ca37acSRyan Libby struct keg_layout_result {
223227ca37acSRyan Libby 	u_int format;
223327ca37acSRyan Libby 	u_int slabsize;
223427ca37acSRyan Libby 	u_int ipers;
223527ca37acSRyan Libby 	u_int eff;
223627ca37acSRyan Libby };
223727ca37acSRyan Libby 
223827ca37acSRyan Libby static void
223927ca37acSRyan Libby keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt,
224027ca37acSRyan Libby     struct keg_layout_result *kl)
224127ca37acSRyan Libby {
224227ca37acSRyan Libby 	u_int total;
224327ca37acSRyan Libby 
224427ca37acSRyan Libby 	kl->format = fmt;
224527ca37acSRyan Libby 	kl->slabsize = slabsize;
224627ca37acSRyan Libby 
224727ca37acSRyan Libby 	/* Handle INTERNAL as inline with an extra page. */
224827ca37acSRyan Libby 	if ((fmt & UMA_ZFLAG_INTERNAL) != 0) {
224927ca37acSRyan Libby 		kl->format &= ~UMA_ZFLAG_INTERNAL;
225027ca37acSRyan Libby 		kl->slabsize += PAGE_SIZE;
225127ca37acSRyan Libby 	}
225227ca37acSRyan Libby 
225327ca37acSRyan Libby 	kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize,
225427ca37acSRyan Libby 	    (fmt & UMA_ZFLAG_OFFPAGE) == 0);
225527ca37acSRyan Libby 
225627ca37acSRyan Libby 	/* Account for memory used by an offpage slab header. */
225727ca37acSRyan Libby 	total = kl->slabsize;
225827ca37acSRyan Libby 	if ((fmt & UMA_ZFLAG_OFFPAGE) != 0)
225927ca37acSRyan Libby 		total += slabzone(kl->ipers)->uz_keg->uk_rsize;
226027ca37acSRyan Libby 
226127ca37acSRyan Libby 	kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total);
226227ca37acSRyan Libby }
226327ca37acSRyan Libby 
22649b78b1f4SJeff Roberson /*
22654a8b575cSRyan Libby  * Determine the format of a uma keg.  This determines where the slab header
22664a8b575cSRyan Libby  * will be placed (inline or offpage) and calculates ipers, rsize, and ppera.
22678355f576SJeff Roberson  *
22688355f576SJeff Roberson  * Arguments
2269e20a199fSJeff Roberson  *	keg  The zone we should initialize
22708355f576SJeff Roberson  *
22718355f576SJeff Roberson  * Returns
22728355f576SJeff Roberson  *	Nothing
22738355f576SJeff Roberson  */
22748355f576SJeff Roberson static void
22754a8b575cSRyan Libby keg_layout(uma_keg_t keg)
22768355f576SJeff Roberson {
227727ca37acSRyan Libby 	struct keg_layout_result kl = {}, kl_tmp;
227827ca37acSRyan Libby 	u_int fmts[2];
22794a8b575cSRyan Libby 	u_int alignsize;
228027ca37acSRyan Libby 	u_int nfmt;
22814a8b575cSRyan Libby 	u_int pages;
2282244f4554SBosko Milekic 	u_int rsize;
2283a55ebb7cSAndriy Gapon 	u_int slabsize;
228427ca37acSRyan Libby 	u_int i, j;
22858355f576SJeff Roberson 
22864a8b575cSRyan Libby 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
22874a8b575cSRyan Libby 	    (keg->uk_size <= UMA_PCPU_ALLOC_SIZE &&
22884a8b575cSRyan Libby 	     (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0),
22894a8b575cSRyan Libby 	    ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b",
22904a8b575cSRyan Libby 	     __func__, keg->uk_name, keg->uk_size, keg->uk_flags,
22914a8b575cSRyan Libby 	     PRINT_UMA_ZFLAGS));
2292bae55c4aSRyan Libby 	KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 ||
22934a8b575cSRyan Libby 	    (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0,
22944a8b575cSRyan Libby 	    ("%s: incompatible flags 0x%b", __func__, keg->uk_flags,
22954a8b575cSRyan Libby 	     PRINT_UMA_ZFLAGS));
2296e28a647dSGleb Smirnoff 
22974a8b575cSRyan Libby 	alignsize = keg->uk_align + 1;
2298b0dfc486SMark Johnston #ifdef KASAN
2299b0dfc486SMark Johnston 	/*
2300b0dfc486SMark Johnston 	 * ASAN requires that each allocation be aligned to the shadow map
2301b0dfc486SMark Johnston 	 * scale factor.
2302b0dfc486SMark Johnston 	 */
2303b0dfc486SMark Johnston 	if (alignsize < KASAN_SHADOW_SCALE)
2304b0dfc486SMark Johnston 		alignsize = KASAN_SHADOW_SCALE;
2305b0dfc486SMark Johnston #endif
2306ad97af7eSGleb Smirnoff 
2307ef72505eSJeff Roberson 	/*
2308ef72505eSJeff Roberson 	 * Calculate the size of each allocation (rsize) according to
2309ef72505eSJeff Roberson 	 * alignment.  If the requested size is smaller than we have
2310ef72505eSJeff Roberson 	 * allocation bits for we round it up.
2311ef72505eSJeff Roberson 	 */
23129b8db4d0SRyan Libby 	rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT);
23134a8b575cSRyan Libby 	rsize = roundup2(rsize, alignsize);
2314ad97af7eSGleb Smirnoff 
231527ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) {
23169b78b1f4SJeff Roberson 		/*
23174a8b575cSRyan Libby 		 * We want one item to start on every align boundary in a page.
23184a8b575cSRyan Libby 		 * To do this we will span pages.  We will also extend the item
23194a8b575cSRyan Libby 		 * by the size of align if it is an even multiple of align.
23204a8b575cSRyan Libby 		 * Otherwise, it would fall on the same boundary every time.
23219b78b1f4SJeff Roberson 		 */
23224a8b575cSRyan Libby 		if ((rsize & alignsize) == 0)
23234a8b575cSRyan Libby 			rsize += alignsize;
23244a8b575cSRyan Libby 		slabsize = rsize * (PAGE_SIZE / alignsize);
23254a8b575cSRyan Libby 		slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE);
23264a8b575cSRyan Libby 		slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE);
232727ca37acSRyan Libby 		slabsize = round_page(slabsize);
23284a8b575cSRyan Libby 	} else {
23294a8b575cSRyan Libby 		/*
233027ca37acSRyan Libby 		 * Start with a slab size of as many pages as it takes to
233127ca37acSRyan Libby 		 * represent a single item.  We will try to fit as many
233227ca37acSRyan Libby 		 * additional items into the slab as possible.
23334a8b575cSRyan Libby 		 */
233427ca37acSRyan Libby 		slabsize = round_page(keg->uk_size);
23351ca6ed45SGleb Smirnoff 	}
2336ad97af7eSGleb Smirnoff 
233727ca37acSRyan Libby 	/* Build a list of all of the available formats for this keg. */
233827ca37acSRyan Libby 	nfmt = 0;
233927ca37acSRyan Libby 
23404a8b575cSRyan Libby 	/* Evaluate an inline slab layout. */
23414a8b575cSRyan Libby 	if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0)
234227ca37acSRyan Libby 		fmts[nfmt++] = 0;
23434a8b575cSRyan Libby 
23444a8b575cSRyan Libby 	/* TODO: vm_page-embedded slab. */
2345244f4554SBosko Milekic 
234620e8e865SBosko Milekic 	/*
2347244f4554SBosko Milekic 	 * We can't do OFFPAGE if we're internal or if we've been
234820e8e865SBosko Milekic 	 * asked to not go to the VM for buckets.  If we do this we
2349bae55c4aSRyan Libby 	 * may end up going to the VM for slabs which we do not want
2350bae55c4aSRyan Libby 	 * to do if we're UMA_ZONE_VM, which clearly forbids it.
2351bae55c4aSRyan Libby 	 * In those cases, evaluate a pseudo-format called INTERNAL
2352bae55c4aSRyan Libby 	 * which has an inline slab header and one extra page to
2353bae55c4aSRyan Libby 	 * guarantee that it fits.
235427ca37acSRyan Libby 	 *
235527ca37acSRyan Libby 	 * Otherwise, see if using an OFFPAGE slab will improve our
235627ca37acSRyan Libby 	 * efficiency.
235720e8e865SBosko Milekic 	 */
2358bae55c4aSRyan Libby 	if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0)
235927ca37acSRyan Libby 		fmts[nfmt++] = UMA_ZFLAG_INTERNAL;
236027ca37acSRyan Libby 	else
236127ca37acSRyan Libby 		fmts[nfmt++] = UMA_ZFLAG_OFFPAGE;
2362244f4554SBosko Milekic 
2363ef72505eSJeff Roberson 	/*
236427ca37acSRyan Libby 	 * Choose a slab size and format which satisfy the minimum efficiency.
236527ca37acSRyan Libby 	 * Prefer the smallest slab size that meets the constraints.
2366ef72505eSJeff Roberson 	 *
236727ca37acSRyan Libby 	 * Start with a minimum slab size, to accommodate CACHESPREAD.  Then,
236827ca37acSRyan Libby 	 * for small items (up to PAGE_SIZE), the iteration increment is one
236927ca37acSRyan Libby 	 * page; and for large items, the increment is one item.
2370ef72505eSJeff Roberson 	 */
237127ca37acSRyan Libby 	i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize);
237227ca37acSRyan Libby 	KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u",
237327ca37acSRyan Libby 	    keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize,
237427ca37acSRyan Libby 	    rsize, i));
237527ca37acSRyan Libby 	for ( ; ; i++) {
237627ca37acSRyan Libby 		slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) :
237727ca37acSRyan Libby 		    round_page(rsize * (i - 1) + keg->uk_size);
237827ca37acSRyan Libby 
237927ca37acSRyan Libby 		for (j = 0; j < nfmt; j++) {
238027ca37acSRyan Libby 			/* Only if we have no viable format yet. */
238127ca37acSRyan Libby 			if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 &&
238227ca37acSRyan Libby 			    kl.ipers > 0)
238327ca37acSRyan Libby 				continue;
238427ca37acSRyan Libby 
238527ca37acSRyan Libby 			keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp);
238627ca37acSRyan Libby 			if (kl_tmp.eff <= kl.eff)
238727ca37acSRyan Libby 				continue;
238827ca37acSRyan Libby 
238927ca37acSRyan Libby 			kl = kl_tmp;
239027ca37acSRyan Libby 
239127ca37acSRyan Libby 			CTR6(KTR_UMA, "keg %s layout: format %#x "
239227ca37acSRyan Libby 			    "(ipers %u * rsize %u) / slabsize %#x = %u%% eff",
239327ca37acSRyan Libby 			    keg->uk_name, kl.format, kl.ipers, rsize,
239427ca37acSRyan Libby 			    kl.slabsize, UMA_FIXPT_PCT(kl.eff));
239527ca37acSRyan Libby 
239627ca37acSRyan Libby 			/* Stop when we reach the minimum efficiency. */
239727ca37acSRyan Libby 			if (kl.eff >= UMA_MIN_EFF)
239827ca37acSRyan Libby 				break;
23998355f576SJeff Roberson 		}
2400ad97af7eSGleb Smirnoff 
240133e5a1eaSRyan Libby 		if (kl.eff >= UMA_MIN_EFF || !multipage_slabs ||
240227ca37acSRyan Libby 		    slabsize >= SLAB_MAX_SETSIZE * rsize ||
240327ca37acSRyan Libby 		    (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0)
240427ca37acSRyan Libby 			break;
240527ca37acSRyan Libby 	}
240627ca37acSRyan Libby 
240727ca37acSRyan Libby 	pages = atop(kl.slabsize);
240827ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
240927ca37acSRyan Libby 		pages *= mp_maxid + 1;
241027ca37acSRyan Libby 
241127ca37acSRyan Libby 	keg->uk_rsize = rsize;
241227ca37acSRyan Libby 	keg->uk_ipers = kl.ipers;
241327ca37acSRyan Libby 	keg->uk_ppera = pages;
241427ca37acSRyan Libby 	keg->uk_flags |= kl.format;
241527ca37acSRyan Libby 
24164a8b575cSRyan Libby 	/*
24174a8b575cSRyan Libby 	 * How do we find the slab header if it is offpage or if not all item
24184a8b575cSRyan Libby 	 * start addresses are in the same page?  We could solve the latter
24194a8b575cSRyan Libby 	 * case with vaddr alignment, but we don't.
24204a8b575cSRyan Libby 	 */
242127ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 ||
242227ca37acSRyan Libby 	    (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) {
242354c5ae80SRyan Libby 		if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0)
242427ca37acSRyan Libby 			keg->uk_flags |= UMA_ZFLAG_HASH;
242554c5ae80SRyan Libby 		else
242627ca37acSRyan Libby 			keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
242754c5ae80SRyan Libby 	}
242827ca37acSRyan Libby 
2429e63a1c2fSRyan Libby 	CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u",
243027ca37acSRyan Libby 	    __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers,
243127ca37acSRyan Libby 	    pages);
24324a8b575cSRyan Libby 	KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE,
24334a8b575cSRyan Libby 	    ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__,
243427ca37acSRyan Libby 	     keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize,
243527ca37acSRyan Libby 	     keg->uk_ipers, pages));
2436e20a199fSJeff Roberson }
2437e20a199fSJeff Roberson 
24388355f576SJeff Roberson /*
2439099a0e58SBosko Milekic  * Keg header ctor.  This initializes all fields, locks, etc.  And inserts
2440099a0e58SBosko Milekic  * the keg onto the global keg list.
24418355f576SJeff Roberson  *
24428355f576SJeff Roberson  * Arguments/Returns follow uma_ctor specifications
2443099a0e58SBosko Milekic  *	udata  Actually uma_kctor_args
2444099a0e58SBosko Milekic  */
2445b23f72e9SBrian Feldman static int
2446b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags)
2447099a0e58SBosko Milekic {
2448099a0e58SBosko Milekic 	struct uma_kctor_args *arg = udata;
2449099a0e58SBosko Milekic 	uma_keg_t keg = mem;
2450099a0e58SBosko Milekic 	uma_zone_t zone;
24518b987a77SJeff Roberson 	int i;
2452099a0e58SBosko Milekic 
2453099a0e58SBosko Milekic 	bzero(keg, size);
2454099a0e58SBosko Milekic 	keg->uk_size = arg->size;
2455099a0e58SBosko Milekic 	keg->uk_init = arg->uminit;
2456099a0e58SBosko Milekic 	keg->uk_fini = arg->fini;
2457099a0e58SBosko Milekic 	keg->uk_align = arg->align;
24586fd34d6fSJeff Roberson 	keg->uk_reserve = 0;
2459099a0e58SBosko Milekic 	keg->uk_flags = arg->flags;
2460099a0e58SBosko Milekic 
2461099a0e58SBosko Milekic 	/*
2462194a979eSMark Johnston 	 * We use a global round-robin policy by default.  Zones with
2463dfe13344SJeff Roberson 	 * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which
2464dfe13344SJeff Roberson 	 * case the iterator is never run.
2465194a979eSMark Johnston 	 */
2466194a979eSMark Johnston 	keg->uk_dr.dr_policy = DOMAINSET_RR();
2467194a979eSMark Johnston 	keg->uk_dr.dr_iter = 0;
2468194a979eSMark Johnston 
2469194a979eSMark Johnston 	/*
2470c8b0a88bSJeff Roberson 	 * The primary zone is passed to us at keg-creation time.
2471099a0e58SBosko Milekic 	 */
2472099a0e58SBosko Milekic 	zone = arg->zone;
2473e20a199fSJeff Roberson 	keg->uk_name = zone->uz_name;
2474099a0e58SBosko Milekic 
2475099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_ZINIT)
2476099a0e58SBosko Milekic 		keg->uk_init = zero_init;
2477099a0e58SBosko Milekic 
2478cfcae3f8SGleb Smirnoff 	if (arg->flags & UMA_ZONE_MALLOC)
247954c5ae80SRyan Libby 		keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
2480e20a199fSJeff Roberson 
248154c5ae80SRyan Libby #ifndef SMP
2482ad97af7eSGleb Smirnoff 	keg->uk_flags &= ~UMA_ZONE_PCPU;
2483ad97af7eSGleb Smirnoff #endif
2484ad97af7eSGleb Smirnoff 
24854a8b575cSRyan Libby 	keg_layout(keg);
2486099a0e58SBosko Milekic 
24878b987a77SJeff Roberson 	/*
2488c6fd3e23SJeff Roberson 	 * Use a first-touch NUMA policy for kegs that pmap_extract() will
2489c6fd3e23SJeff Roberson 	 * work on.  Use round-robin for everything else.
2490dfe13344SJeff Roberson 	 *
2491dfe13344SJeff Roberson 	 * Zones may override the default by specifying either.
24928b987a77SJeff Roberson 	 */
2493dfe13344SJeff Roberson #ifdef NUMA
2494dfe13344SJeff Roberson 	if ((keg->uk_flags &
2495c6fd3e23SJeff Roberson 	    (UMA_ZONE_ROUNDROBIN | UMA_ZFLAG_CACHE | UMA_ZONE_NOTPAGE)) == 0)
2496dfe13344SJeff Roberson 		keg->uk_flags |= UMA_ZONE_FIRSTTOUCH;
2497dfe13344SJeff Roberson 	else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0)
2498dfe13344SJeff Roberson 		keg->uk_flags |= UMA_ZONE_ROUNDROBIN;
24998b987a77SJeff Roberson #endif
25008b987a77SJeff Roberson 
2501099a0e58SBosko Milekic 	/*
2502099a0e58SBosko Milekic 	 * If we haven't booted yet we need allocations to go through the
2503099a0e58SBosko Milekic 	 * startup cache until the vm is ready.
2504099a0e58SBosko Milekic 	 */
250577e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC
2506a81c400eSJeff Roberson 	if (keg->uk_ppera == 1)
250777e19437SGleb Smirnoff 		keg->uk_allocf = uma_small_alloc;
2508a81c400eSJeff Roberson 	else
25098cd02d00SAlan Cox #endif
2510a81c400eSJeff Roberson 	if (booted < BOOT_KVA)
2511a81c400eSJeff Roberson 		keg->uk_allocf = startup_alloc;
2512ab3059a8SMatt Macy 	else if (keg->uk_flags & UMA_ZONE_PCPU)
2513ab3059a8SMatt Macy 		keg->uk_allocf = pcpu_page_alloc;
2514ec0d8280SRyan Libby 	else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1)
2515ec0d8280SRyan Libby 		keg->uk_allocf = contig_alloc;
251677e19437SGleb Smirnoff 	else
251777e19437SGleb Smirnoff 		keg->uk_allocf = page_alloc;
251877e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC
251977e19437SGleb Smirnoff 	if (keg->uk_ppera == 1)
252077e19437SGleb Smirnoff 		keg->uk_freef = uma_small_free;
252177e19437SGleb Smirnoff 	else
252277e19437SGleb Smirnoff #endif
2523ab3059a8SMatt Macy 	if (keg->uk_flags & UMA_ZONE_PCPU)
2524ab3059a8SMatt Macy 		keg->uk_freef = pcpu_page_free;
2525ab3059a8SMatt Macy 	else
252677e19437SGleb Smirnoff 		keg->uk_freef = page_free;
2527099a0e58SBosko Milekic 
2528099a0e58SBosko Milekic 	/*
25298b987a77SJeff Roberson 	 * Initialize keg's locks.
2530099a0e58SBosko Milekic 	 */
25318b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
25328b987a77SJeff Roberson 		KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS));
2533099a0e58SBosko Milekic 
2534099a0e58SBosko Milekic 	/*
2535099a0e58SBosko Milekic 	 * If we're putting the slab header in the actual page we need to
25369b78b1f4SJeff Roberson 	 * figure out where in each page it goes.  See slab_sizeof
25379b78b1f4SJeff Roberson 	 * definition.
2538099a0e58SBosko Milekic 	 */
253954c5ae80SRyan Libby 	if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) {
25409b78b1f4SJeff Roberson 		size_t shsize;
25419b78b1f4SJeff Roberson 
25429b78b1f4SJeff Roberson 		shsize = slab_sizeof(keg->uk_ipers);
25439b78b1f4SJeff Roberson 		keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize;
2544244f4554SBosko Milekic 		/*
2545244f4554SBosko Milekic 		 * The only way the following is possible is if with our
2546244f4554SBosko Milekic 		 * UMA_ALIGN_PTR adjustments we are now bigger than
2547244f4554SBosko Milekic 		 * UMA_SLAB_SIZE.  I haven't checked whether this is
2548244f4554SBosko Milekic 		 * mathematically possible for all cases, so we make
2549244f4554SBosko Milekic 		 * sure here anyway.
2550244f4554SBosko Milekic 		 */
25519b78b1f4SJeff Roberson 		KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera,
25523d5e3df7SGleb Smirnoff 		    ("zone %s ipers %d rsize %d size %d slab won't fit",
25533d5e3df7SGleb Smirnoff 		    zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size));
2554099a0e58SBosko Milekic 	}
2555099a0e58SBosko Milekic 
255654c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_HASH)
25573b2f2cb8SAlexander Motin 		hash_alloc(&keg->uk_hash, 0);
2558099a0e58SBosko Milekic 
2559e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone);
2560099a0e58SBosko Milekic 
2561099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
2562099a0e58SBosko Milekic 
2563111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
2564099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
2565111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
2566b23f72e9SBrian Feldman 	return (0);
2567099a0e58SBosko Milekic }
2568099a0e58SBosko Milekic 
25692efcc8cbSGleb Smirnoff static void
2570a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused)
2571a81c400eSJeff Roberson {
2572a81c400eSJeff Roberson 	uma_keg_t keg;
2573a81c400eSJeff Roberson 
2574a81c400eSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
2575a81c400eSJeff Roberson 		return;
2576a81c400eSJeff Roberson 	KEG_GET(zone, keg);
2577ec0d8280SRyan Libby 
2578ec0d8280SRyan Libby 	if (keg->uk_allocf == startup_alloc) {
2579ec0d8280SRyan Libby 		/* Switch to the real allocator. */
2580f96d4157SJeff Roberson 		if (keg->uk_flags & UMA_ZONE_PCPU)
2581f96d4157SJeff Roberson 			keg->uk_allocf = pcpu_page_alloc;
2582ec0d8280SRyan Libby 		else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 &&
2583ec0d8280SRyan Libby 		    keg->uk_ppera > 1)
2584ec0d8280SRyan Libby 			keg->uk_allocf = contig_alloc;
2585ec0d8280SRyan Libby 		else
2586a81c400eSJeff Roberson 			keg->uk_allocf = page_alloc;
2587a81c400eSJeff Roberson 	}
2588ec0d8280SRyan Libby }
2589a81c400eSJeff Roberson 
2590a81c400eSJeff Roberson static void
259120a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused)
25922efcc8cbSGleb Smirnoff {
25932efcc8cbSGleb Smirnoff 
25942efcc8cbSGleb Smirnoff 	zone->uz_allocs = counter_u64_alloc(M_WAITOK);
25952efcc8cbSGleb Smirnoff 	zone->uz_frees = counter_u64_alloc(M_WAITOK);
25962efcc8cbSGleb Smirnoff 	zone->uz_fails = counter_u64_alloc(M_WAITOK);
2597c6fd3e23SJeff Roberson 	zone->uz_xdomain = counter_u64_alloc(M_WAITOK);
25982efcc8cbSGleb Smirnoff }
25992efcc8cbSGleb Smirnoff 
260020a4e154SJeff Roberson static void
260120a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused)
260220a4e154SJeff Roberson {
260320a4e154SJeff Roberson 	uma_zone_domain_t zdom;
26048b987a77SJeff Roberson 	uma_domain_t dom;
260520a4e154SJeff Roberson 	uma_keg_t keg;
260620a4e154SJeff Roberson 	struct sysctl_oid *oid, *domainoid;
26073b490537SJeff Roberson 	int domains, i, cnt;
260820a4e154SJeff Roberson 	static const char *nokeg = "cache zone";
260920a4e154SJeff Roberson 	char *c;
261020a4e154SJeff Roberson 
261120a4e154SJeff Roberson 	/*
261220a4e154SJeff Roberson 	 * Make a sysctl safe copy of the zone name by removing
261320a4e154SJeff Roberson 	 * any special characters and handling dups by appending
261420a4e154SJeff Roberson 	 * an index.
261520a4e154SJeff Roberson 	 */
261620a4e154SJeff Roberson 	if (zone->uz_namecnt != 0) {
26173b490537SJeff Roberson 		/* Count the number of decimal digits and '_' separator. */
26183b490537SJeff Roberson 		for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++)
26193b490537SJeff Roberson 			cnt /= 10;
26203b490537SJeff Roberson 		zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1,
26213b490537SJeff Roberson 		    M_UMA, M_WAITOK);
262220a4e154SJeff Roberson 		sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name,
262320a4e154SJeff Roberson 		    zone->uz_namecnt);
262420a4e154SJeff Roberson 	} else
262520a4e154SJeff Roberson 		zone->uz_ctlname = strdup(zone->uz_name, M_UMA);
262620a4e154SJeff Roberson 	for (c = zone->uz_ctlname; *c != '\0'; c++)
262720a4e154SJeff Roberson 		if (strchr("./\\ -", *c) != NULL)
262820a4e154SJeff Roberson 			*c = '_';
262920a4e154SJeff Roberson 
263020a4e154SJeff Roberson 	/*
263120a4e154SJeff Roberson 	 * Basic parameters at the root.
263220a4e154SJeff Roberson 	 */
263320a4e154SJeff Roberson 	zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma),
26347029da5cSPawel Biernacki 	    OID_AUTO, zone->uz_ctlname, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
263520a4e154SJeff Roberson 	oid = zone->uz_oid;
263620a4e154SJeff Roberson 	SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
263720a4e154SJeff Roberson 	    "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size");
26386d204a6aSRyan Libby 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
26396d204a6aSRyan Libby 	    "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE,
26406d204a6aSRyan Libby 	    zone, 0, sysctl_handle_uma_zone_flags, "A",
264120a4e154SJeff Roberson 	    "Allocator configuration flags");
264220a4e154SJeff Roberson 	SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
264320a4e154SJeff Roberson 	    "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0,
264420a4e154SJeff Roberson 	    "Desired per-cpu cache size");
264520a4e154SJeff Roberson 	SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
264620a4e154SJeff Roberson 	    "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0,
264720a4e154SJeff Roberson 	    "Maximum allowed per-cpu cache size");
264820a4e154SJeff Roberson 
264920a4e154SJeff Roberson 	/*
265020a4e154SJeff Roberson 	 * keg if present.
265120a4e154SJeff Roberson 	 */
265254c5ae80SRyan Libby 	if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0)
26538b987a77SJeff Roberson 		domains = vm_ndomains;
26548b987a77SJeff Roberson 	else
26558b987a77SJeff Roberson 		domains = 1;
265620a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
26577029da5cSPawel Biernacki 	    "keg", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
265820a4e154SJeff Roberson 	keg = zone->uz_keg;
26593b490537SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) {
266020a4e154SJeff Roberson 		SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
266120a4e154SJeff Roberson 		    "name", CTLFLAG_RD, keg->uk_name, "Keg name");
266220a4e154SJeff Roberson 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
266320a4e154SJeff Roberson 		    "rsize", CTLFLAG_RD, &keg->uk_rsize, 0,
266420a4e154SJeff Roberson 		    "Real object size with alignment");
266520a4e154SJeff Roberson 		SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
266620a4e154SJeff Roberson 		    "ppera", CTLFLAG_RD, &keg->uk_ppera, 0,
266720a4e154SJeff Roberson 		    "pages per-slab allocation");
266820a4e154SJeff Roberson 		SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
266920a4e154SJeff Roberson 		    "ipers", CTLFLAG_RD, &keg->uk_ipers, 0,
267020a4e154SJeff Roberson 		    "items available per-slab");
267120a4e154SJeff Roberson 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
267220a4e154SJeff Roberson 		    "align", CTLFLAG_RD, &keg->uk_align, 0,
267320a4e154SJeff Roberson 		    "item alignment mask");
2674f09cbea3SMark Johnston 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2675f09cbea3SMark Johnston 		    "reserve", CTLFLAG_RD, &keg->uk_reserve, 0,
2676f09cbea3SMark Johnston 		    "number of reserved items");
2677f7af5015SRyan Libby 		SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2678f7af5015SRyan Libby 		    "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
2679f7af5015SRyan Libby 		    keg, 0, sysctl_handle_uma_slab_efficiency, "I",
2680f7af5015SRyan Libby 		    "Slab utilization (100 - internal fragmentation %)");
26818b987a77SJeff Roberson 		domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid),
26827029da5cSPawel Biernacki 		    OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
26838b987a77SJeff Roberson 		for (i = 0; i < domains; i++) {
26848b987a77SJeff Roberson 			dom = &keg->uk_domain[i];
26858b987a77SJeff Roberson 			oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
26867029da5cSPawel Biernacki 			    OID_AUTO, VM_DOMAIN(i)->vmd_name,
26877029da5cSPawel Biernacki 			    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
26888b987a77SJeff Roberson 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
26898b987a77SJeff Roberson 			    "pages", CTLFLAG_RD, &dom->ud_pages, 0,
26908b987a77SJeff Roberson 			    "Total pages currently allocated from VM");
26918b987a77SJeff Roberson 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
26924ab3aee8SMark Johnston 			    "free_items", CTLFLAG_RD, &dom->ud_free_items, 0,
2693*d6e77cdaSMark Johnston 			    "Items free in the slab layer");
2694*d6e77cdaSMark Johnston 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2695*d6e77cdaSMark Johnston 			    "free_slabs", CTLFLAG_RD, &dom->ud_free_slabs, 0,
2696*d6e77cdaSMark Johnston 			    "Unused slabs");
26978b987a77SJeff Roberson 		}
269820a4e154SJeff Roberson 	} else
269920a4e154SJeff Roberson 		SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
270020a4e154SJeff Roberson 		    "name", CTLFLAG_RD, nokeg, "Keg name");
270120a4e154SJeff Roberson 
270220a4e154SJeff Roberson 	/*
270320a4e154SJeff Roberson 	 * Information about zone limits.
270420a4e154SJeff Roberson 	 */
270520a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
27067029da5cSPawel Biernacki 	    "limit", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
27074bd61e19SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
27084bd61e19SJeff Roberson 	    "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
27094bd61e19SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_items, "QU",
2710e574d407SMark Johnston 	    "Current number of allocated items if limit is set");
271120a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
271220a4e154SJeff Roberson 	    "max_items", CTLFLAG_RD, &zone->uz_max_items, 0,
2713e574d407SMark Johnston 	    "Maximum number of allocated and cached items");
271420a4e154SJeff Roberson 	SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
271520a4e154SJeff Roberson 	    "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0,
271620a4e154SJeff Roberson 	    "Number of threads sleeping at limit");
271720a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
271820a4e154SJeff Roberson 	    "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0,
271920a4e154SJeff Roberson 	    "Total zone limit sleeps");
27204bd61e19SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2721c6fd3e23SJeff Roberson 	    "bucket_max", CTLFLAG_RD, &zone->uz_bucket_max, 0,
2722c6fd3e23SJeff Roberson 	    "Maximum number of items in each domain's bucket cache");
272320a4e154SJeff Roberson 
272420a4e154SJeff Roberson 	/*
27258b987a77SJeff Roberson 	 * Per-domain zone information.
272620a4e154SJeff Roberson 	 */
272720a4e154SJeff Roberson 	domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid),
27287029da5cSPawel Biernacki 	    OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
272920a4e154SJeff Roberson 	for (i = 0; i < domains; i++) {
2730c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(zone, i);
273120a4e154SJeff Roberson 		oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
27327029da5cSPawel Biernacki 		    OID_AUTO, VM_DOMAIN(i)->vmd_name,
27337029da5cSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
273420a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
273520a4e154SJeff Roberson 		    "nitems", CTLFLAG_RD, &zdom->uzd_nitems,
273620a4e154SJeff Roberson 		    "number of items in this domain");
273720a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
273820a4e154SJeff Roberson 		    "imax", CTLFLAG_RD, &zdom->uzd_imax,
273920a4e154SJeff Roberson 		    "maximum item count in this period");
274020a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
274120a4e154SJeff Roberson 		    "imin", CTLFLAG_RD, &zdom->uzd_imin,
274220a4e154SJeff Roberson 		    "minimum item count in this period");
274320a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
27442760658bSAlexander Motin 		    "bimin", CTLFLAG_RD, &zdom->uzd_bimin,
27452760658bSAlexander Motin 		    "Minimum item count in this batch");
27462760658bSAlexander Motin 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
274720a4e154SJeff Roberson 		    "wss", CTLFLAG_RD, &zdom->uzd_wss,
274820a4e154SJeff Roberson 		    "Working set size");
27492760658bSAlexander Motin 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
27502760658bSAlexander Motin 		    "limin", CTLFLAG_RD, &zdom->uzd_limin,
27512760658bSAlexander Motin 		    "Long time minimum item count");
27522760658bSAlexander Motin 		SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
27532760658bSAlexander Motin 		    "timin", CTLFLAG_RD, &zdom->uzd_timin, 0,
27542760658bSAlexander Motin 		    "Time since zero long time minimum item count");
275520a4e154SJeff Roberson 	}
275620a4e154SJeff Roberson 
275720a4e154SJeff Roberson 	/*
275820a4e154SJeff Roberson 	 * General statistics.
275920a4e154SJeff Roberson 	 */
276020a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
27617029da5cSPawel Biernacki 	    "stats", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
276220a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
276320a4e154SJeff Roberson 	    "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
276420a4e154SJeff Roberson 	    zone, 1, sysctl_handle_uma_zone_cur, "I",
276520a4e154SJeff Roberson 	    "Current number of allocated items");
276620a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
276720a4e154SJeff Roberson 	    "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
276820a4e154SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_allocs, "QU",
276920a4e154SJeff Roberson 	    "Total allocation calls");
277020a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
277120a4e154SJeff Roberson 	    "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
277220a4e154SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_frees, "QU",
277320a4e154SJeff Roberson 	    "Total free calls");
277420a4e154SJeff Roberson 	SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
277520a4e154SJeff Roberson 	    "fails", CTLFLAG_RD, &zone->uz_fails,
277620a4e154SJeff Roberson 	    "Number of allocation failures");
2777c6fd3e23SJeff Roberson 	SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2778c6fd3e23SJeff Roberson 	    "xdomain", CTLFLAG_RD, &zone->uz_xdomain,
277920a4e154SJeff Roberson 	    "Free calls from the wrong domain");
278020a4e154SJeff Roberson }
278120a4e154SJeff Roberson 
278220a4e154SJeff Roberson struct uma_zone_count {
278320a4e154SJeff Roberson 	const char	*name;
278420a4e154SJeff Roberson 	int		count;
278520a4e154SJeff Roberson };
278620a4e154SJeff Roberson 
278720a4e154SJeff Roberson static void
278820a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg)
278920a4e154SJeff Roberson {
279020a4e154SJeff Roberson 	struct uma_zone_count *cnt;
279120a4e154SJeff Roberson 
279220a4e154SJeff Roberson 	cnt = arg;
27933b490537SJeff Roberson 	/*
27943b490537SJeff Roberson 	 * Some zones are rapidly created with identical names and
27953b490537SJeff Roberson 	 * destroyed out of order.  This can lead to gaps in the count.
27963b490537SJeff Roberson 	 * Use one greater than the maximum observed for this name.
27973b490537SJeff Roberson 	 */
279820a4e154SJeff Roberson 	if (strcmp(zone->uz_name, cnt->name) == 0)
27993b490537SJeff Roberson 		cnt->count = MAX(cnt->count,
28003b490537SJeff Roberson 		    zone->uz_namecnt + 1);
280120a4e154SJeff Roberson }
280220a4e154SJeff Roberson 
2803cc7ce83aSJeff Roberson static void
2804cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone)
2805cc7ce83aSJeff Roberson {
2806cc7ce83aSJeff Roberson 	int i;
2807cc7ce83aSJeff Roberson 
2808cc7ce83aSJeff Roberson 	for (i = 0; i <= mp_maxid; i++) {
2809cc7ce83aSJeff Roberson 		cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size);
2810cc7ce83aSJeff Roberson 		cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags);
2811cc7ce83aSJeff Roberson 	}
2812cc7ce83aSJeff Roberson }
2813cc7ce83aSJeff Roberson 
2814099a0e58SBosko Milekic /*
2815099a0e58SBosko Milekic  * Zone header ctor.  This initializes all fields, locks, etc.
2816099a0e58SBosko Milekic  *
2817099a0e58SBosko Milekic  * Arguments/Returns follow uma_ctor specifications
2818099a0e58SBosko Milekic  *	udata  Actually uma_zctor_args
28198355f576SJeff Roberson  */
2820b23f72e9SBrian Feldman static int
2821b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags)
28228355f576SJeff Roberson {
282320a4e154SJeff Roberson 	struct uma_zone_count cnt;
28248355f576SJeff Roberson 	struct uma_zctor_args *arg = udata;
2825c6fd3e23SJeff Roberson 	uma_zone_domain_t zdom;
28268355f576SJeff Roberson 	uma_zone_t zone = mem;
2827099a0e58SBosko Milekic 	uma_zone_t z;
2828099a0e58SBosko Milekic 	uma_keg_t keg;
282908cfa56eSMark Johnston 	int i;
28308355f576SJeff Roberson 
28318355f576SJeff Roberson 	bzero(zone, size);
28328355f576SJeff Roberson 	zone->uz_name = arg->name;
28338355f576SJeff Roberson 	zone->uz_ctor = arg->ctor;
28348355f576SJeff Roberson 	zone->uz_dtor = arg->dtor;
2835099a0e58SBosko Milekic 	zone->uz_init = NULL;
2836099a0e58SBosko Milekic 	zone->uz_fini = NULL;
2837bf965959SSean Bruno 	zone->uz_sleeps = 0;
283820a4e154SJeff Roberson 	zone->uz_bucket_size = 0;
283920a4e154SJeff Roberson 	zone->uz_bucket_size_min = 0;
284020a4e154SJeff Roberson 	zone->uz_bucket_size_max = BUCKET_MAX;
2841d4665eaaSJeff Roberson 	zone->uz_flags = (arg->flags & UMA_ZONE_SMR);
28422f891cd5SPawel Jakub Dawidek 	zone->uz_warning = NULL;
2843ab3185d1SJeff Roberson 	/* The domain structures follow the cpu structures. */
2844c6fd3e23SJeff Roberson 	zone->uz_bucket_max = ULONG_MAX;
28452f891cd5SPawel Jakub Dawidek 	timevalclear(&zone->uz_ratecheck);
2846af526374SJeff Roberson 
284720a4e154SJeff Roberson 	/* Count the number of duplicate names. */
284820a4e154SJeff Roberson 	cnt.name = arg->name;
284920a4e154SJeff Roberson 	cnt.count = 0;
285020a4e154SJeff Roberson 	zone_foreach(zone_count, &cnt);
285120a4e154SJeff Roberson 	zone->uz_namecnt = cnt.count;
285291d947bfSJeff Roberson 	ZONE_CROSS_LOCK_INIT(zone);
28532efcc8cbSGleb Smirnoff 
2854c6fd3e23SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
2855c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(zone, i);
2856c6fd3e23SJeff Roberson 		ZDOM_LOCK_INIT(zone, zdom, (arg->flags & UMA_ZONE_MTXCLASS));
2857c6fd3e23SJeff Roberson 		STAILQ_INIT(&zdom->uzd_buckets);
2858c6fd3e23SJeff Roberson 	}
285908cfa56eSMark Johnston 
286010094910SMark Johnston #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN)
2861ca293436SRyan Libby 	if (arg->uminit == trash_init && arg->fini == trash_fini)
2862cc7ce83aSJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR;
286309c8cb71SMark Johnston #elif defined(KASAN)
286409c8cb71SMark Johnston 	if ((arg->flags & (UMA_ZONE_NOFREE | UMA_ZFLAG_CACHE)) != 0)
286509c8cb71SMark Johnston 		arg->flags |= UMA_ZONE_NOKASAN;
2866ca293436SRyan Libby #endif
2867ca293436SRyan Libby 
28680095a784SJeff Roberson 	/*
28690095a784SJeff Roberson 	 * This is a pure cache zone, no kegs.
28700095a784SJeff Roberson 	 */
28710095a784SJeff Roberson 	if (arg->import) {
2872727c6918SJeff Roberson 		KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0,
2873727c6918SJeff Roberson 		    ("zone_ctor: Import specified for non-cache zone."));
28746fd34d6fSJeff Roberson 		zone->uz_flags = arg->flags;
2875af526374SJeff Roberson 		zone->uz_size = arg->size;
28760095a784SJeff Roberson 		zone->uz_import = arg->import;
28770095a784SJeff Roberson 		zone->uz_release = arg->release;
28780095a784SJeff Roberson 		zone->uz_arg = arg->arg;
2879c6fd3e23SJeff Roberson #ifdef NUMA
2880c6fd3e23SJeff Roberson 		/*
2881c6fd3e23SJeff Roberson 		 * Cache zones are round-robin unless a policy is
2882c6fd3e23SJeff Roberson 		 * specified because they may have incompatible
2883c6fd3e23SJeff Roberson 		 * constraints.
2884c6fd3e23SJeff Roberson 		 */
2885c6fd3e23SJeff Roberson 		if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0)
2886c6fd3e23SJeff Roberson 			zone->uz_flags |= UMA_ZONE_ROUNDROBIN;
2887c6fd3e23SJeff Roberson #endif
2888111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
288903175483SAlexander Motin 		LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
2890111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
2891af526374SJeff Roberson 		goto out;
28920095a784SJeff Roberson 	}
28930095a784SJeff Roberson 
28940095a784SJeff Roberson 	/*
28950095a784SJeff Roberson 	 * Use the regular zone/keg/slab allocator.
28960095a784SJeff Roberson 	 */
2897b75c4efcSAndrew Turner 	zone->uz_import = zone_import;
2898b75c4efcSAndrew Turner 	zone->uz_release = zone_release;
28990095a784SJeff Roberson 	zone->uz_arg = zone;
2900bb15d1c7SGleb Smirnoff 	keg = arg->keg;
29010095a784SJeff Roberson 
2902099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_SECONDARY) {
290320a4e154SJeff Roberson 		KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
290420a4e154SJeff Roberson 		    ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
2905099a0e58SBosko Milekic 		KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
29068355f576SJeff Roberson 		zone->uz_init = arg->uminit;
2907e221e841SJeff Roberson 		zone->uz_fini = arg->fini;
2908e20a199fSJeff Roberson 		zone->uz_flags |= UMA_ZONE_SECONDARY;
2909111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
2910099a0e58SBosko Milekic 		ZONE_LOCK(zone);
2911099a0e58SBosko Milekic 		LIST_FOREACH(z, &keg->uk_zones, uz_link) {
2912099a0e58SBosko Milekic 			if (LIST_NEXT(z, uz_link) == NULL) {
2913099a0e58SBosko Milekic 				LIST_INSERT_AFTER(z, zone, uz_link);
2914099a0e58SBosko Milekic 				break;
2915099a0e58SBosko Milekic 			}
2916099a0e58SBosko Milekic 		}
2917099a0e58SBosko Milekic 		ZONE_UNLOCK(zone);
2918111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
2919e20a199fSJeff Roberson 	} else if (keg == NULL) {
2920e20a199fSJeff Roberson 		if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
2921e20a199fSJeff Roberson 		    arg->align, arg->flags)) == NULL)
2922b23f72e9SBrian Feldman 			return (ENOMEM);
2923099a0e58SBosko Milekic 	} else {
2924099a0e58SBosko Milekic 		struct uma_kctor_args karg;
2925b23f72e9SBrian Feldman 		int error;
2926099a0e58SBosko Milekic 
2927099a0e58SBosko Milekic 		/* We should only be here from uma_startup() */
2928099a0e58SBosko Milekic 		karg.size = arg->size;
2929099a0e58SBosko Milekic 		karg.uminit = arg->uminit;
2930099a0e58SBosko Milekic 		karg.fini = arg->fini;
2931099a0e58SBosko Milekic 		karg.align = arg->align;
2932d4665eaaSJeff Roberson 		karg.flags = (arg->flags & ~UMA_ZONE_SMR);
2933099a0e58SBosko Milekic 		karg.zone = zone;
2934b23f72e9SBrian Feldman 		error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
2935b23f72e9SBrian Feldman 		    flags);
2936b23f72e9SBrian Feldman 		if (error)
2937b23f72e9SBrian Feldman 			return (error);
2938099a0e58SBosko Milekic 	}
29390095a784SJeff Roberson 
294020a4e154SJeff Roberson 	/* Inherit properties from the keg. */
2941bb15d1c7SGleb Smirnoff 	zone->uz_keg = keg;
2942e20a199fSJeff Roberson 	zone->uz_size = keg->uk_size;
2943e20a199fSJeff Roberson 	zone->uz_flags |= (keg->uk_flags &
2944e20a199fSJeff Roberson 	    (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
29458355f576SJeff Roberson 
294620a4e154SJeff Roberson out:
2947dc2b3205SMark Johnston 	if (booted >= BOOT_PCPU) {
294820a4e154SJeff Roberson 		zone_alloc_counters(zone, NULL);
2949dc2b3205SMark Johnston 		if (booted >= BOOT_RUNNING)
295020a4e154SJeff Roberson 			zone_alloc_sysctl(zone, NULL);
295120a4e154SJeff Roberson 	} else {
295220a4e154SJeff Roberson 		zone->uz_allocs = EARLY_COUNTER;
295320a4e154SJeff Roberson 		zone->uz_frees = EARLY_COUNTER;
295420a4e154SJeff Roberson 		zone->uz_fails = EARLY_COUNTER;
2955099a0e58SBosko Milekic 	}
29568355f576SJeff Roberson 
2957d4665eaaSJeff Roberson 	/* Caller requests a private SMR context. */
2958d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
2959226dd6dbSJeff Roberson 		zone->uz_smr = smr_create(zone->uz_name, 0, 0);
2960d4665eaaSJeff Roberson 
29617e28037aSMark Johnston 	KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) !=
29627e28037aSMark Johnston 	    (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET),
29637e28037aSMark Johnston 	    ("Invalid zone flag combination"));
296420a4e154SJeff Roberson 	if (arg->flags & UMA_ZFLAG_INTERNAL)
296520a4e154SJeff Roberson 		zone->uz_bucket_size_max = zone->uz_bucket_size = 0;
296620a4e154SJeff Roberson 	if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0)
296720a4e154SJeff Roberson 		zone->uz_bucket_size = BUCKET_MAX;
296820a4e154SJeff Roberson 	else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0)
296920a4e154SJeff Roberson 		zone->uz_bucket_size = 0;
29707e28037aSMark Johnston 	else
297120a4e154SJeff Roberson 		zone->uz_bucket_size = bucket_select(zone->uz_size);
297220a4e154SJeff Roberson 	zone->uz_bucket_size_min = zone->uz_bucket_size;
2973cc7ce83aSJeff Roberson 	if (zone->uz_dtor != NULL || zone->uz_ctor != NULL)
2974cc7ce83aSJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_CTORDTOR;
2975cc7ce83aSJeff Roberson 	zone_update_caches(zone);
2976fc03d22bSJeff Roberson 
2977b23f72e9SBrian Feldman 	return (0);
29788355f576SJeff Roberson }
29798355f576SJeff Roberson 
29808355f576SJeff Roberson /*
2981099a0e58SBosko Milekic  * Keg header dtor.  This frees all data, destroys locks, frees the hash
2982099a0e58SBosko Milekic  * table and removes the keg from the global list.
29839c2cd7e5SJeff Roberson  *
29849c2cd7e5SJeff Roberson  * Arguments/Returns follow uma_dtor specifications
29859c2cd7e5SJeff Roberson  *	udata  unused
29869c2cd7e5SJeff Roberson  */
2987099a0e58SBosko Milekic static void
2988099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata)
2989099a0e58SBosko Milekic {
2990099a0e58SBosko Milekic 	uma_keg_t keg;
29918b987a77SJeff Roberson 	uint32_t free, pages;
29928b987a77SJeff Roberson 	int i;
29939c2cd7e5SJeff Roberson 
2994099a0e58SBosko Milekic 	keg = (uma_keg_t)arg;
29958b987a77SJeff Roberson 	free = pages = 0;
29968b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
29974ab3aee8SMark Johnston 		free += keg->uk_domain[i].ud_free_items;
29988b987a77SJeff Roberson 		pages += keg->uk_domain[i].ud_pages;
29998b987a77SJeff Roberson 		KEG_LOCK_FINI(keg, i);
3000099a0e58SBosko Milekic 	}
30017e240677SRyan Libby 	if (pages != 0)
30028b987a77SJeff Roberson 		printf("Freed UMA keg (%s) was not empty (%u items). "
30038b987a77SJeff Roberson 		    " Lost %u pages of memory.\n",
30048b987a77SJeff Roberson 		    keg->uk_name ? keg->uk_name : "",
30057e240677SRyan Libby 		    pages / keg->uk_ppera * keg->uk_ipers - free, pages);
3006099a0e58SBosko Milekic 
3007099a0e58SBosko Milekic 	hash_free(&keg->uk_hash);
3008099a0e58SBosko Milekic }
3009099a0e58SBosko Milekic 
3010099a0e58SBosko Milekic /*
3011099a0e58SBosko Milekic  * Zone header dtor.
3012099a0e58SBosko Milekic  *
3013099a0e58SBosko Milekic  * Arguments/Returns follow uma_dtor specifications
3014099a0e58SBosko Milekic  *	udata  unused
3015099a0e58SBosko Milekic  */
30169c2cd7e5SJeff Roberson static void
30179c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata)
30189c2cd7e5SJeff Roberson {
30199c2cd7e5SJeff Roberson 	uma_zone_t zone;
3020099a0e58SBosko Milekic 	uma_keg_t keg;
3021c6fd3e23SJeff Roberson 	int i;
30229c2cd7e5SJeff Roberson 
30239c2cd7e5SJeff Roberson 	zone = (uma_zone_t)arg;
30249643769aSJeff Roberson 
302520a4e154SJeff Roberson 	sysctl_remove_oid(zone->uz_oid, 1, 1);
302620a4e154SJeff Roberson 
3027e20a199fSJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
30289643769aSJeff Roberson 		cache_drain(zone);
3029099a0e58SBosko Milekic 
3030111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
3031099a0e58SBosko Milekic 	LIST_REMOVE(zone, uz_link);
3032111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
30337b516613SJonathan T. Looney 	if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) {
30347b516613SJonathan T. Looney 		keg = zone->uz_keg;
30357b516613SJonathan T. Looney 		keg->uk_reserve = 0;
30367b516613SJonathan T. Looney 	}
3037aabe13f1SMark Johnston 	zone_reclaim(zone, UMA_ANYDOMAIN, M_WAITOK, true);
3038c6fd3e23SJeff Roberson 
3039e20a199fSJeff Roberson 	/*
3040323ad386STycho Nightingale 	 * We only destroy kegs from non secondary/non cache zones.
3041e20a199fSJeff Roberson 	 */
3042323ad386STycho Nightingale 	if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) {
3043323ad386STycho Nightingale 		keg = zone->uz_keg;
3044111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
3045099a0e58SBosko Milekic 		LIST_REMOVE(keg, uk_link);
3046111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
30470095a784SJeff Roberson 		zone_free_item(kegs, keg, NULL, SKIP_NONE);
30489c2cd7e5SJeff Roberson 	}
30492efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_allocs);
30502efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_frees);
30512efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_fails);
3052c6fd3e23SJeff Roberson 	counter_u64_free(zone->uz_xdomain);
305320a4e154SJeff Roberson 	free(zone->uz_ctlname, M_UMA);
3054c6fd3e23SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
3055c6fd3e23SJeff Roberson 		ZDOM_LOCK_FINI(ZDOM_GET(zone, i));
305691d947bfSJeff Roberson 	ZONE_CROSS_LOCK_FINI(zone);
3057099a0e58SBosko Milekic }
3058099a0e58SBosko Milekic 
3059a81c400eSJeff Roberson static void
3060a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg)
3061a81c400eSJeff Roberson {
3062a81c400eSJeff Roberson 	uma_keg_t keg;
3063a81c400eSJeff Roberson 	uma_zone_t zone;
3064a81c400eSJeff Roberson 
3065a81c400eSJeff Roberson 	LIST_FOREACH(keg, &uma_kegs, uk_link) {
3066a81c400eSJeff Roberson 		LIST_FOREACH(zone, &keg->uk_zones, uz_link)
3067a81c400eSJeff Roberson 			zfunc(zone, arg);
3068a81c400eSJeff Roberson 	}
3069a81c400eSJeff Roberson 	LIST_FOREACH(zone, &uma_cachezones, uz_link)
3070a81c400eSJeff Roberson 		zfunc(zone, arg);
3071a81c400eSJeff Roberson }
3072a81c400eSJeff Roberson 
30739c2cd7e5SJeff Roberson /*
30748355f576SJeff Roberson  * Traverses every zone in the system and calls a callback
30758355f576SJeff Roberson  *
30768355f576SJeff Roberson  * Arguments:
30778355f576SJeff Roberson  *	zfunc  A pointer to a function which accepts a zone
30788355f576SJeff Roberson  *		as an argument.
30798355f576SJeff Roberson  *
30808355f576SJeff Roberson  * Returns:
30818355f576SJeff Roberson  *	Nothing
30828355f576SJeff Roberson  */
30838355f576SJeff Roberson static void
308420a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg)
30858355f576SJeff Roberson {
30868355f576SJeff Roberson 
3087111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
3088a81c400eSJeff Roberson 	zone_foreach_unlocked(zfunc, arg);
3089111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
30908355f576SJeff Roberson }
30918355f576SJeff Roberson 
3092f4bef67cSGleb Smirnoff /*
3093a81c400eSJeff Roberson  * Initialize the kernel memory allocator.  This is done after pages can be
3094a81c400eSJeff Roberson  * allocated but before general KVA is available.
3095f4bef67cSGleb Smirnoff  */
3096a81c400eSJeff Roberson void
3097a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail)
3098f4bef67cSGleb Smirnoff {
3099a81c400eSJeff Roberson 	struct uma_zctor_args args;
3100a81c400eSJeff Roberson 	size_t ksize, zsize, size;
3101c8b0a88bSJeff Roberson 	uma_keg_t primarykeg;
3102a81c400eSJeff Roberson 	uintptr_t m;
310381302f1dSMark Johnston 	int domain;
3104a81c400eSJeff Roberson 	uint8_t pflag;
3105a81c400eSJeff Roberson 
3106a81c400eSJeff Roberson 	bootstart = bootmem = virtual_avail;
3107a81c400eSJeff Roberson 
3108a81c400eSJeff Roberson 	rw_init(&uma_rwlock, "UMA lock");
3109a81c400eSJeff Roberson 	sx_init(&uma_reclaim_lock, "umareclaim");
3110f4bef67cSGleb Smirnoff 
3111f4bef67cSGleb Smirnoff 	ksize = sizeof(struct uma_keg) +
3112f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_domain) * vm_ndomains);
311379c9f942SJeff Roberson 	ksize = roundup(ksize, UMA_SUPER_ALIGN);
3114f4bef67cSGleb Smirnoff 	zsize = sizeof(struct uma_zone) +
3115f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_cache) * (mp_maxid + 1)) +
3116f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_zone_domain) * vm_ndomains);
311779c9f942SJeff Roberson 	zsize = roundup(zsize, UMA_SUPER_ALIGN);
3118f4bef67cSGleb Smirnoff 
3119a81c400eSJeff Roberson 	/* Allocate the zone of zones, zone of kegs, and zone of zones keg. */
3120a81c400eSJeff Roberson 	size = (zsize * 2) + ksize;
312181302f1dSMark Johnston 	for (domain = 0; domain < vm_ndomains; domain++) {
312281302f1dSMark Johnston 		m = (uintptr_t)startup_alloc(NULL, size, domain, &pflag,
312381302f1dSMark Johnston 		    M_NOWAIT | M_ZERO);
312481302f1dSMark Johnston 		if (m != 0)
312581302f1dSMark Johnston 			break;
312681302f1dSMark Johnston 	}
3127ab3185d1SJeff Roberson 	zones = (uma_zone_t)m;
312879c9f942SJeff Roberson 	m += zsize;
3129ab3185d1SJeff Roberson 	kegs = (uma_zone_t)m;
313079c9f942SJeff Roberson 	m += zsize;
3131c8b0a88bSJeff Roberson 	primarykeg = (uma_keg_t)m;
3132ab3185d1SJeff Roberson 
3133099a0e58SBosko Milekic 	/* "manually" create the initial zone */
31340095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
3135099a0e58SBosko Milekic 	args.name = "UMA Kegs";
3136ab3185d1SJeff Roberson 	args.size = ksize;
3137099a0e58SBosko Milekic 	args.ctor = keg_ctor;
3138099a0e58SBosko Milekic 	args.dtor = keg_dtor;
31398355f576SJeff Roberson 	args.uminit = zero_init;
31408355f576SJeff Roberson 	args.fini = NULL;
3141c8b0a88bSJeff Roberson 	args.keg = primarykeg;
314279c9f942SJeff Roberson 	args.align = UMA_SUPER_ALIGN - 1;
3143b60f5b79SJeff Roberson 	args.flags = UMA_ZFLAG_INTERNAL;
3144ab3185d1SJeff Roberson 	zone_ctor(kegs, zsize, &args, M_WAITOK);
31458355f576SJeff Roberson 
3146099a0e58SBosko Milekic 	args.name = "UMA Zones";
3147f4bef67cSGleb Smirnoff 	args.size = zsize;
3148099a0e58SBosko Milekic 	args.ctor = zone_ctor;
3149099a0e58SBosko Milekic 	args.dtor = zone_dtor;
3150099a0e58SBosko Milekic 	args.uminit = zero_init;
3151099a0e58SBosko Milekic 	args.fini = NULL;
3152099a0e58SBosko Milekic 	args.keg = NULL;
315379c9f942SJeff Roberson 	args.align = UMA_SUPER_ALIGN - 1;
3154099a0e58SBosko Milekic 	args.flags = UMA_ZFLAG_INTERNAL;
3155ab3185d1SJeff Roberson 	zone_ctor(zones, zsize, &args, M_WAITOK);
3156099a0e58SBosko Milekic 
31579b8db4d0SRyan Libby 	/* Now make zones for slab headers */
31589b8db4d0SRyan Libby 	slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE,
31599b8db4d0SRyan Libby 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
31609b8db4d0SRyan Libby 	slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE,
31611e0701e1SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
31628355f576SJeff Roberson 
31638355f576SJeff Roberson 	hashzone = uma_zcreate("UMA Hash",
31648355f576SJeff Roberson 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
31651e0701e1SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
31668355f576SJeff Roberson 
3167a81c400eSJeff Roberson 	bucket_init();
3168d4665eaaSJeff Roberson 	smr_init();
31698355f576SJeff Roberson }
31708355f576SJeff Roberson 
3171a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC
3172a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void);
3173f4bef67cSGleb Smirnoff #endif
3174f4bef67cSGleb Smirnoff 
3175a81c400eSJeff Roberson /*
3176a81c400eSJeff Roberson  * Advertise the availability of normal kva allocations and switch to
3177a81c400eSJeff Roberson  * the default back-end allocator.  Marks the KVA we consumed on startup
3178a81c400eSJeff Roberson  * as used in the map.
3179a81c400eSJeff Roberson  */
31808355f576SJeff Roberson void
318199571dc3SJeff Roberson uma_startup2(void)
31828355f576SJeff Roberson {
3183f4bef67cSGleb Smirnoff 
3184530cc6a2SJeff Roberson 	if (bootstart != bootmem) {
3185a81c400eSJeff Roberson 		vm_map_lock(kernel_map);
3186a81c400eSJeff Roberson 		(void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem,
3187a81c400eSJeff Roberson 		    VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
3188a81c400eSJeff Roberson 		vm_map_unlock(kernel_map);
3189a81c400eSJeff Roberson 	}
3190a81c400eSJeff Roberson 
3191a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC
3192a81c400eSJeff Roberson 	/* Set up radix zone to use noobj_alloc. */
3193a81c400eSJeff Roberson 	vm_radix_reserve_kva();
3194f7d35785SGleb Smirnoff #endif
3195a81c400eSJeff Roberson 
3196a81c400eSJeff Roberson 	booted = BOOT_KVA;
3197a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_kva_available, NULL);
3198f4bef67cSGleb Smirnoff 	bucket_enable();
31998355f576SJeff Roberson }
32008355f576SJeff Roberson 
3201a81c400eSJeff Roberson /*
3202dc2b3205SMark Johnston  * Allocate counters as early as possible so that boot-time allocations are
3203dc2b3205SMark Johnston  * accounted more precisely.
3204dc2b3205SMark Johnston  */
3205dc2b3205SMark Johnston static void
3206dc2b3205SMark Johnston uma_startup_pcpu(void *arg __unused)
3207dc2b3205SMark Johnston {
3208dc2b3205SMark Johnston 
3209dc2b3205SMark Johnston 	zone_foreach_unlocked(zone_alloc_counters, NULL);
3210dc2b3205SMark Johnston 	booted = BOOT_PCPU;
3211dc2b3205SMark Johnston }
3212dc2b3205SMark Johnston SYSINIT(uma_startup_pcpu, SI_SUB_COUNTER, SI_ORDER_ANY, uma_startup_pcpu, NULL);
3213dc2b3205SMark Johnston 
3214dc2b3205SMark Johnston /*
3215a81c400eSJeff Roberson  * Finish our initialization steps.
3216a81c400eSJeff Roberson  */
32178355f576SJeff Roberson static void
3218dc2b3205SMark Johnston uma_startup3(void *arg __unused)
32198355f576SJeff Roberson {
32201431a748SGleb Smirnoff 
3221c5deaf04SGleb Smirnoff #ifdef INVARIANTS
3222c5deaf04SGleb Smirnoff 	TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor);
3223c5deaf04SGleb Smirnoff 	uma_dbg_cnt = counter_u64_alloc(M_WAITOK);
3224c5deaf04SGleb Smirnoff 	uma_skip_cnt = counter_u64_alloc(M_WAITOK);
3225c5deaf04SGleb Smirnoff #endif
3226a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_alloc_sysctl, NULL);
3227fd90e2edSJung-uk Kim 	callout_init(&uma_callout, 1);
32289643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
3229c5deaf04SGleb Smirnoff 	booted = BOOT_RUNNING;
3230860bb7a0SMark Johnston 
3231860bb7a0SMark Johnston 	EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL,
3232860bb7a0SMark Johnston 	    EVENTHANDLER_PRI_FIRST);
3233860bb7a0SMark Johnston }
3234dc2b3205SMark Johnston SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
3235860bb7a0SMark Johnston 
3236860bb7a0SMark Johnston static void
3237860bb7a0SMark Johnston uma_shutdown(void)
3238860bb7a0SMark Johnston {
3239860bb7a0SMark Johnston 
3240860bb7a0SMark Johnston 	booted = BOOT_SHUTDOWN;
32418355f576SJeff Roberson }
32428355f576SJeff Roberson 
3243e20a199fSJeff Roberson static uma_keg_t
3244099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
324585dcf349SGleb Smirnoff 		int align, uint32_t flags)
3246099a0e58SBosko Milekic {
3247099a0e58SBosko Milekic 	struct uma_kctor_args args;
3248099a0e58SBosko Milekic 
3249099a0e58SBosko Milekic 	args.size = size;
3250099a0e58SBosko Milekic 	args.uminit = uminit;
3251099a0e58SBosko Milekic 	args.fini = fini;
32521e319f6dSRobert Watson 	args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
3253099a0e58SBosko Milekic 	args.flags = flags;
3254099a0e58SBosko Milekic 	args.zone = zone;
3255ab3185d1SJeff Roberson 	return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK));
3256099a0e58SBosko Milekic }
3257099a0e58SBosko Milekic 
3258f4bef67cSGleb Smirnoff /* Public functions */
32598355f576SJeff Roberson /* See uma.h */
32601e319f6dSRobert Watson void
32611e319f6dSRobert Watson uma_set_align(int align)
32621e319f6dSRobert Watson {
32631e319f6dSRobert Watson 
32641e319f6dSRobert Watson 	if (align != UMA_ALIGN_CACHE)
32651e319f6dSRobert Watson 		uma_align_cache = align;
32661e319f6dSRobert Watson }
32671e319f6dSRobert Watson 
32681e319f6dSRobert Watson /* See uma.h */
32698355f576SJeff Roberson uma_zone_t
3270bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
327185dcf349SGleb Smirnoff 		uma_init uminit, uma_fini fini, int align, uint32_t flags)
32728355f576SJeff Roberson 
32738355f576SJeff Roberson {
32748355f576SJeff Roberson 	struct uma_zctor_args args;
327595c4bf75SKonstantin Belousov 	uma_zone_t res;
32768355f576SJeff Roberson 
3277a5a35578SJohn Baldwin 	KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
3278a5a35578SJohn Baldwin 	    align, name));
3279a5a35578SJohn Baldwin 
32808355f576SJeff Roberson 	/* This stuff is essential for the zone ctor */
32810095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
32828355f576SJeff Roberson 	args.name = name;
32838355f576SJeff Roberson 	args.size = size;
32848355f576SJeff Roberson 	args.ctor = ctor;
32858355f576SJeff Roberson 	args.dtor = dtor;
32868355f576SJeff Roberson 	args.uminit = uminit;
32878355f576SJeff Roberson 	args.fini = fini;
328810094910SMark Johnston #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN)
3289afc6dc36SJohn-Mark Gurney 	/*
3290ca293436SRyan Libby 	 * Inject procedures which check for memory use after free if we are
3291ca293436SRyan Libby 	 * allowed to scramble the memory while it is not allocated.  This
3292ca293436SRyan Libby 	 * requires that: UMA is actually able to access the memory, no init
3293ca293436SRyan Libby 	 * or fini procedures, no dependency on the initial value of the
3294ca293436SRyan Libby 	 * memory, and no (legitimate) use of the memory after free.  Note,
3295ca293436SRyan Libby 	 * the ctor and dtor do not need to be empty.
3296afc6dc36SJohn-Mark Gurney 	 */
329754c5ae80SRyan Libby 	if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH |
329854c5ae80SRyan Libby 	    UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) {
3299afc6dc36SJohn-Mark Gurney 		args.uminit = trash_init;
3300afc6dc36SJohn-Mark Gurney 		args.fini = trash_fini;
3301afc6dc36SJohn-Mark Gurney 	}
3302afc6dc36SJohn-Mark Gurney #endif
33038355f576SJeff Roberson 	args.align = align;
33048355f576SJeff Roberson 	args.flags = flags;
3305099a0e58SBosko Milekic 	args.keg = NULL;
3306099a0e58SBosko Milekic 
3307aabe13f1SMark Johnston 	sx_xlock(&uma_reclaim_lock);
3308ab3185d1SJeff Roberson 	res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
3309aabe13f1SMark Johnston 	sx_xunlock(&uma_reclaim_lock);
3310a81c400eSJeff Roberson 
331195c4bf75SKonstantin Belousov 	return (res);
3312099a0e58SBosko Milekic }
3313099a0e58SBosko Milekic 
3314099a0e58SBosko Milekic /* See uma.h */
3315099a0e58SBosko Milekic uma_zone_t
33160464f16eSMark Johnston uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor,
3317c8b0a88bSJeff Roberson     uma_init zinit, uma_fini zfini, uma_zone_t primary)
3318099a0e58SBosko Milekic {
3319099a0e58SBosko Milekic 	struct uma_zctor_args args;
3320e20a199fSJeff Roberson 	uma_keg_t keg;
332195c4bf75SKonstantin Belousov 	uma_zone_t res;
3322099a0e58SBosko Milekic 
3323c8b0a88bSJeff Roberson 	keg = primary->uz_keg;
33240095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
3325099a0e58SBosko Milekic 	args.name = name;
3326e20a199fSJeff Roberson 	args.size = keg->uk_size;
3327099a0e58SBosko Milekic 	args.ctor = ctor;
3328099a0e58SBosko Milekic 	args.dtor = dtor;
3329099a0e58SBosko Milekic 	args.uminit = zinit;
3330099a0e58SBosko Milekic 	args.fini = zfini;
3331e20a199fSJeff Roberson 	args.align = keg->uk_align;
3332e20a199fSJeff Roberson 	args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
3333e20a199fSJeff Roberson 	args.keg = keg;
33348355f576SJeff Roberson 
3335aabe13f1SMark Johnston 	sx_xlock(&uma_reclaim_lock);
3336ab3185d1SJeff Roberson 	res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
3337aabe13f1SMark Johnston 	sx_xunlock(&uma_reclaim_lock);
3338a81c400eSJeff Roberson 
333995c4bf75SKonstantin Belousov 	return (res);
33408355f576SJeff Roberson }
33418355f576SJeff Roberson 
33420095a784SJeff Roberson /* See uma.h */
33430095a784SJeff Roberson uma_zone_t
33440464f16eSMark Johnston uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor,
33450464f16eSMark Johnston     uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease,
33460464f16eSMark Johnston     void *arg, int flags)
33470095a784SJeff Roberson {
33480095a784SJeff Roberson 	struct uma_zctor_args args;
33490095a784SJeff Roberson 
33500095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
33510095a784SJeff Roberson 	args.name = name;
3352af526374SJeff Roberson 	args.size = size;
33530095a784SJeff Roberson 	args.ctor = ctor;
33540095a784SJeff Roberson 	args.dtor = dtor;
33550095a784SJeff Roberson 	args.uminit = zinit;
33560095a784SJeff Roberson 	args.fini = zfini;
33570095a784SJeff Roberson 	args.import = zimport;
33580095a784SJeff Roberson 	args.release = zrelease;
33590095a784SJeff Roberson 	args.arg = arg;
33600095a784SJeff Roberson 	args.align = 0;
3361bb15d1c7SGleb Smirnoff 	args.flags = flags | UMA_ZFLAG_CACHE;
33620095a784SJeff Roberson 
3363ab3185d1SJeff Roberson 	return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK));
33640095a784SJeff Roberson }
33650095a784SJeff Roberson 
33668355f576SJeff Roberson /* See uma.h */
33679c2cd7e5SJeff Roberson void
33689c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone)
33699c2cd7e5SJeff Roberson {
3370f4ff923bSRobert Watson 
3371860bb7a0SMark Johnston 	/*
3372860bb7a0SMark Johnston 	 * Large slabs are expensive to reclaim, so don't bother doing
3373860bb7a0SMark Johnston 	 * unnecessary work if we're shutting down.
3374860bb7a0SMark Johnston 	 */
3375860bb7a0SMark Johnston 	if (booted == BOOT_SHUTDOWN &&
3376860bb7a0SMark Johnston 	    zone->uz_fini == NULL && zone->uz_release == zone_release)
3377860bb7a0SMark Johnston 		return;
3378aabe13f1SMark Johnston 	sx_xlock(&uma_reclaim_lock);
33790095a784SJeff Roberson 	zone_free_item(zones, zone, NULL, SKIP_NONE);
3380aabe13f1SMark Johnston 	sx_xunlock(&uma_reclaim_lock);
33819c2cd7e5SJeff Roberson }
33829c2cd7e5SJeff Roberson 
33838d6fbbb8SJeff Roberson void
33848d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone)
33858d6fbbb8SJeff Roberson {
33868d6fbbb8SJeff Roberson 
338770260874SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
338870260874SJeff Roberson 		uma_zfree_smr(zone, uma_zalloc_smr(zone, M_WAITOK));
338970260874SJeff Roberson 	else if ((zone->uz_flags & UMA_ZONE_PCPU) != 0)
339070260874SJeff Roberson 		uma_zfree_pcpu(zone, uma_zalloc_pcpu(zone, M_WAITOK));
339170260874SJeff Roberson 	else
339270260874SJeff Roberson 		uma_zfree(zone, uma_zalloc(zone, M_WAITOK));
33938d6fbbb8SJeff Roberson }
33948d6fbbb8SJeff Roberson 
33954e180881SMateusz Guzik void *
33964e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags)
33974e180881SMateusz Guzik {
33983acb6572SMateusz Guzik 	void *item, *pcpu_item;
3399b4799947SRuslan Bukin #ifdef SMP
34004e180881SMateusz Guzik 	int i;
34014e180881SMateusz Guzik 
34024e180881SMateusz Guzik 	MPASS(zone->uz_flags & UMA_ZONE_PCPU);
3403b4799947SRuslan Bukin #endif
34044e180881SMateusz Guzik 	item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO);
34053acb6572SMateusz Guzik 	if (item == NULL)
34063acb6572SMateusz Guzik 		return (NULL);
34073acb6572SMateusz Guzik 	pcpu_item = zpcpu_base_to_offset(item);
34083acb6572SMateusz Guzik 	if (flags & M_ZERO) {
3409b4799947SRuslan Bukin #ifdef SMP
3410013072f0SMark Johnston 		for (i = 0; i <= mp_maxid; i++)
34113acb6572SMateusz Guzik 			bzero(zpcpu_get_cpu(pcpu_item, i), zone->uz_size);
3412b4799947SRuslan Bukin #else
3413b4799947SRuslan Bukin 		bzero(item, zone->uz_size);
3414b4799947SRuslan Bukin #endif
34154e180881SMateusz Guzik 	}
34163acb6572SMateusz Guzik 	return (pcpu_item);
34174e180881SMateusz Guzik }
34184e180881SMateusz Guzik 
34194e180881SMateusz Guzik /*
34204e180881SMateusz Guzik  * A stub while both regular and pcpu cases are identical.
34214e180881SMateusz Guzik  */
34224e180881SMateusz Guzik void
34233acb6572SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *pcpu_item, void *udata)
34244e180881SMateusz Guzik {
34253acb6572SMateusz Guzik 	void *item;
34264e180881SMateusz Guzik 
3427c5b7751fSIan Lepore #ifdef SMP
34284e180881SMateusz Guzik 	MPASS(zone->uz_flags & UMA_ZONE_PCPU);
3429c5b7751fSIan Lepore #endif
3430b8f7267dSKristof Provost 
3431b8f7267dSKristof Provost         /* uma_zfree_pcu_*(..., NULL) does nothing, to match free(9). */
3432b8f7267dSKristof Provost         if (pcpu_item == NULL)
3433b8f7267dSKristof Provost                 return;
3434b8f7267dSKristof Provost 
34353acb6572SMateusz Guzik 	item = zpcpu_offset_to_base(pcpu_item);
34364e180881SMateusz Guzik 	uma_zfree_arg(zone, item, udata);
34374e180881SMateusz Guzik }
34384e180881SMateusz Guzik 
3439d4665eaaSJeff Roberson static inline void *
3440d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags,
3441d4665eaaSJeff Roberson     void *item)
3442beb8beefSJeff Roberson {
3443beb8beefSJeff Roberson #ifdef INVARIANTS
3444ca293436SRyan Libby 	bool skipdbg;
344509c8cb71SMark Johnston #endif
3446beb8beefSJeff Roberson 
344709c8cb71SMark Johnston 	kasan_mark_item_valid(zone, item);
344810094910SMark Johnston 	kmsan_mark_item_uninitialized(zone, item);
344909c8cb71SMark Johnston 
345009c8cb71SMark Johnston #ifdef INVARIANTS
3451beb8beefSJeff Roberson 	skipdbg = uma_dbg_zskip(zone, item);
345209c8cb71SMark Johnston 	if (!skipdbg && (uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3453ca293436SRyan Libby 	    zone->uz_ctor != trash_ctor)
3454cc7ce83aSJeff Roberson 		trash_ctor(item, size, udata, flags);
3455beb8beefSJeff Roberson #endif
345609c8cb71SMark Johnston 
3457d4665eaaSJeff Roberson 	/* Check flags before loading ctor pointer. */
3458d4665eaaSJeff Roberson 	if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) &&
3459d4665eaaSJeff Roberson 	    __predict_false(zone->uz_ctor != NULL) &&
3460cc7ce83aSJeff Roberson 	    zone->uz_ctor(item, size, udata, flags) != 0) {
3461beb8beefSJeff Roberson 		counter_u64_add(zone->uz_fails, 1);
3462beb8beefSJeff Roberson 		zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT);
3463beb8beefSJeff Roberson 		return (NULL);
3464beb8beefSJeff Roberson 	}
3465beb8beefSJeff Roberson #ifdef INVARIANTS
3466beb8beefSJeff Roberson 	if (!skipdbg)
3467beb8beefSJeff Roberson 		uma_dbg_alloc(zone, NULL, item);
3468beb8beefSJeff Roberson #endif
34696d88d784SJeff Roberson 	if (__predict_false(flags & M_ZERO))
34706d88d784SJeff Roberson 		return (memset(item, 0, size));
3471beb8beefSJeff Roberson 
3472beb8beefSJeff Roberson 	return (item);
3473beb8beefSJeff Roberson }
3474beb8beefSJeff Roberson 
3475ca293436SRyan Libby static inline void
3476cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata,
3477cc7ce83aSJeff Roberson     enum zfreeskip skip)
3478ca293436SRyan Libby {
3479ca293436SRyan Libby #ifdef INVARIANTS
3480ca293436SRyan Libby 	bool skipdbg;
3481ca293436SRyan Libby 
3482ca293436SRyan Libby 	skipdbg = uma_dbg_zskip(zone, item);
3483ca293436SRyan Libby 	if (skip == SKIP_NONE && !skipdbg) {
3484ca293436SRyan Libby 		if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0)
3485ca293436SRyan Libby 			uma_dbg_free(zone, udata, item);
3486ca293436SRyan Libby 		else
3487ca293436SRyan Libby 			uma_dbg_free(zone, NULL, item);
3488ca293436SRyan Libby 	}
3489ca293436SRyan Libby #endif
3490cc7ce83aSJeff Roberson 	if (__predict_true(skip < SKIP_DTOR)) {
3491ca293436SRyan Libby 		if (zone->uz_dtor != NULL)
3492cc7ce83aSJeff Roberson 			zone->uz_dtor(item, size, udata);
3493ca293436SRyan Libby #ifdef INVARIANTS
3494ca293436SRyan Libby 		if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3495ca293436SRyan Libby 		    zone->uz_dtor != trash_dtor)
3496cc7ce83aSJeff Roberson 			trash_dtor(item, size, udata);
3497ca293436SRyan Libby #endif
3498ca293436SRyan Libby 	}
349909c8cb71SMark Johnston 	kasan_mark_item_invalid(zone, item);
3500ca293436SRyan Libby }
3501ca293436SRyan Libby 
35021c58c09fSMateusz Guzik #ifdef NUMA
350381302f1dSMark Johnston static int
350481302f1dSMark Johnston item_domain(void *item)
350581302f1dSMark Johnston {
350681302f1dSMark Johnston 	int domain;
350781302f1dSMark Johnston 
3508431fb8abSMark Johnston 	domain = vm_phys_domain(vtophys(item));
350981302f1dSMark Johnston 	KASSERT(domain >= 0 && domain < vm_ndomains,
351081302f1dSMark Johnston 	    ("%s: unknown domain for item %p", __func__, item));
351181302f1dSMark Johnston 	return (domain);
351281302f1dSMark Johnston }
35131c58c09fSMateusz Guzik #endif
351481302f1dSMark Johnston 
3515d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS)
3516d4665eaaSJeff Roberson #define	UMA_ZALLOC_DEBUG
3517d4665eaaSJeff Roberson static int
3518d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags)
3519d4665eaaSJeff Roberson {
3520d4665eaaSJeff Roberson 	int error;
3521d4665eaaSJeff Roberson 
3522d4665eaaSJeff Roberson 	error = 0;
3523d4665eaaSJeff Roberson #ifdef WITNESS
3524d4665eaaSJeff Roberson 	if (flags & M_WAITOK) {
3525d4665eaaSJeff Roberson 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
3526d4665eaaSJeff Roberson 		    "uma_zalloc_debug: zone \"%s\"", zone->uz_name);
3527d4665eaaSJeff Roberson 	}
3528d4665eaaSJeff Roberson #endif
3529d4665eaaSJeff Roberson 
3530d4665eaaSJeff Roberson #ifdef INVARIANTS
3531d4665eaaSJeff Roberson 	KASSERT((flags & M_EXEC) == 0,
3532d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: called with M_EXEC"));
3533d4665eaaSJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3534d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: called within spinlock or critical section"));
3535d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0,
3536d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO"));
3537d4665eaaSJeff Roberson #endif
3538d4665eaaSJeff Roberson 
3539d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD
35409e47b341SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) {
3541d4665eaaSJeff Roberson 		void *item;
3542d4665eaaSJeff Roberson 		item = memguard_alloc(zone->uz_size, flags);
3543d4665eaaSJeff Roberson 		if (item != NULL) {
3544d4665eaaSJeff Roberson 			error = EJUSTRETURN;
3545d4665eaaSJeff Roberson 			if (zone->uz_init != NULL &&
3546d4665eaaSJeff Roberson 			    zone->uz_init(item, zone->uz_size, flags) != 0) {
3547d4665eaaSJeff Roberson 				*itemp = NULL;
3548d4665eaaSJeff Roberson 				return (error);
3549d4665eaaSJeff Roberson 			}
3550d4665eaaSJeff Roberson 			if (zone->uz_ctor != NULL &&
3551d4665eaaSJeff Roberson 			    zone->uz_ctor(item, zone->uz_size, udata,
3552d4665eaaSJeff Roberson 			    flags) != 0) {
3553d4665eaaSJeff Roberson 				counter_u64_add(zone->uz_fails, 1);
3554d4665eaaSJeff Roberson 			    	zone->uz_fini(item, zone->uz_size);
3555d4665eaaSJeff Roberson 				*itemp = NULL;
3556d4665eaaSJeff Roberson 				return (error);
3557d4665eaaSJeff Roberson 			}
3558d4665eaaSJeff Roberson 			*itemp = item;
3559d4665eaaSJeff Roberson 			return (error);
3560d4665eaaSJeff Roberson 		}
3561d4665eaaSJeff Roberson 		/* This is unfortunate but should not be fatal. */
3562d4665eaaSJeff Roberson 	}
3563d4665eaaSJeff Roberson #endif
3564d4665eaaSJeff Roberson 	return (error);
3565d4665eaaSJeff Roberson }
3566d4665eaaSJeff Roberson 
3567d4665eaaSJeff Roberson static int
3568d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata)
3569d4665eaaSJeff Roberson {
3570d4665eaaSJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3571d4665eaaSJeff Roberson 	    ("uma_zfree_debug: called with spinlock or critical section held"));
3572d4665eaaSJeff Roberson 
3573d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD
35749e47b341SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) {
3575d4665eaaSJeff Roberson 		if (zone->uz_dtor != NULL)
3576d4665eaaSJeff Roberson 			zone->uz_dtor(item, zone->uz_size, udata);
3577d4665eaaSJeff Roberson 		if (zone->uz_fini != NULL)
3578d4665eaaSJeff Roberson 			zone->uz_fini(item, zone->uz_size);
3579d4665eaaSJeff Roberson 		memguard_free(item);
3580d4665eaaSJeff Roberson 		return (EJUSTRETURN);
3581d4665eaaSJeff Roberson 	}
3582d4665eaaSJeff Roberson #endif
3583d4665eaaSJeff Roberson 	return (0);
3584d4665eaaSJeff Roberson }
3585d4665eaaSJeff Roberson #endif
3586d4665eaaSJeff Roberson 
35876d88d784SJeff Roberson static inline void *
35886d88d784SJeff Roberson cache_alloc_item(uma_zone_t zone, uma_cache_t cache, uma_cache_bucket_t bucket,
35896d88d784SJeff Roberson     void *udata, int flags)
3590d4665eaaSJeff Roberson {
35916d88d784SJeff Roberson 	void *item;
35926d88d784SJeff Roberson 	int size, uz_flags;
35936d88d784SJeff Roberson 
35946d88d784SJeff Roberson 	item = cache_bucket_pop(cache, bucket);
35956d88d784SJeff Roberson 	size = cache_uz_size(cache);
35966d88d784SJeff Roberson 	uz_flags = cache_uz_flags(cache);
35976d88d784SJeff Roberson 	critical_exit();
35986d88d784SJeff Roberson 	return (item_ctor(zone, uz_flags, size, udata, flags, item));
35996d88d784SJeff Roberson }
36006d88d784SJeff Roberson 
36016d88d784SJeff Roberson static __noinline void *
36026d88d784SJeff Roberson cache_alloc_retry(uma_zone_t zone, uma_cache_t cache, void *udata, int flags)
36036d88d784SJeff Roberson {
36046d88d784SJeff Roberson 	uma_cache_bucket_t bucket;
3605d4665eaaSJeff Roberson 	int domain;
3606d4665eaaSJeff Roberson 
36076d88d784SJeff Roberson 	while (cache_alloc(zone, cache, udata, flags)) {
36086d88d784SJeff Roberson 		cache = &zone->uz_cpu[curcpu];
36096d88d784SJeff Roberson 		bucket = &cache->uc_allocbucket;
36106d88d784SJeff Roberson 		if (__predict_false(bucket->ucb_cnt == 0))
36116d88d784SJeff Roberson 			continue;
36126d88d784SJeff Roberson 		return (cache_alloc_item(zone, cache, bucket, udata, flags));
36136d88d784SJeff Roberson 	}
36146d88d784SJeff Roberson 	critical_exit();
36156d88d784SJeff Roberson 
3616d4665eaaSJeff Roberson 	/*
3617d4665eaaSJeff Roberson 	 * We can not get a bucket so try to return a single item.
3618d4665eaaSJeff Roberson 	 */
3619d4665eaaSJeff Roberson 	if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH)
3620d4665eaaSJeff Roberson 		domain = PCPU_GET(domain);
3621d4665eaaSJeff Roberson 	else
3622d4665eaaSJeff Roberson 		domain = UMA_ANYDOMAIN;
3623d4665eaaSJeff Roberson 	return (zone_alloc_item(zone, udata, domain, flags));
3624d4665eaaSJeff Roberson }
3625d4665eaaSJeff Roberson 
3626d4665eaaSJeff Roberson /* See uma.h */
3627d4665eaaSJeff Roberson void *
3628d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags)
3629d4665eaaSJeff Roberson {
3630d4665eaaSJeff Roberson 	uma_cache_bucket_t bucket;
3631d4665eaaSJeff Roberson 	uma_cache_t cache;
3632d4665eaaSJeff Roberson 
3633d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
36346d88d784SJeff Roberson 	void *item;
36356d88d784SJeff Roberson 
3636d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
3637952c8964SMark Johnston 	    ("uma_zalloc_arg: called with non-SMR zone."));
3638d4665eaaSJeff Roberson 	if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN)
3639d4665eaaSJeff Roberson 		return (item);
3640d4665eaaSJeff Roberson #endif
3641d4665eaaSJeff Roberson 
3642d4665eaaSJeff Roberson 	critical_enter();
3643d4665eaaSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3644d4665eaaSJeff Roberson 	bucket = &cache->uc_allocbucket;
36456d88d784SJeff Roberson 	if (__predict_false(bucket->ucb_cnt == 0))
36466d88d784SJeff Roberson 		return (cache_alloc_retry(zone, cache, NULL, flags));
36476d88d784SJeff Roberson 	return (cache_alloc_item(zone, cache, bucket, NULL, flags));
3648d4665eaaSJeff Roberson }
3649d4665eaaSJeff Roberson 
36509c2cd7e5SJeff Roberson /* See uma.h */
36518355f576SJeff Roberson void *
36522cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
36538355f576SJeff Roberson {
3654376b1ba3SJeff Roberson 	uma_cache_bucket_t bucket;
3655ab3185d1SJeff Roberson 	uma_cache_t cache;
36568355f576SJeff Roberson 
3657e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
365819fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
365910cb2424SMark Murray 
36608355f576SJeff Roberson 	/* This is the fast path allocation */
3661e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name,
3662e63a1c2fSRyan Libby 	    zone, flags);
3663a553d4b8SJeff Roberson 
3664d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
36656d88d784SJeff Roberson 	void *item;
36666d88d784SJeff Roberson 
3667d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
3668952c8964SMark Johnston 	    ("uma_zalloc_arg: called with SMR zone."));
3669d4665eaaSJeff Roberson 	if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN)
36708d689e04SGleb Smirnoff 		return (item);
36718d689e04SGleb Smirnoff #endif
3672d4665eaaSJeff Roberson 
36735d1ae027SRobert Watson 	/*
36745d1ae027SRobert Watson 	 * If possible, allocate from the per-CPU cache.  There are two
36755d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
36765d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
36775d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
36785d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
36795d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
36805d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to allocate from
36815d1ae027SRobert Watson 	 * the current cache; when we re-acquire the critical section, we
36825d1ae027SRobert Watson 	 * must detect and handle migration if it has occurred.
36835d1ae027SRobert Watson 	 */
36845d1ae027SRobert Watson 	critical_enter();
3685cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3686376b1ba3SJeff Roberson 	bucket = &cache->uc_allocbucket;
36876d88d784SJeff Roberson 	if (__predict_false(bucket->ucb_cnt == 0))
36886d88d784SJeff Roberson 		return (cache_alloc_retry(zone, cache, udata, flags));
36896d88d784SJeff Roberson 	return (cache_alloc_item(zone, cache, bucket, udata, flags));
3690fc03d22bSJeff Roberson }
3691fc03d22bSJeff Roberson 
36928355f576SJeff Roberson /*
3693beb8beefSJeff Roberson  * Replenish an alloc bucket and possibly restore an old one.  Called in
3694beb8beefSJeff Roberson  * a critical section.  Returns in a critical section.
3695beb8beefSJeff Roberson  *
36964bd61e19SJeff Roberson  * A false return value indicates an allocation failure.
36974bd61e19SJeff Roberson  * A true return value indicates success and the caller should retry.
3698beb8beefSJeff Roberson  */
3699beb8beefSJeff Roberson static __noinline bool
3700beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags)
3701beb8beefSJeff Roberson {
3702beb8beefSJeff Roberson 	uma_bucket_t bucket;
37038c277118SMark Johnston 	int curdomain, domain;
3704c6fd3e23SJeff Roberson 	bool new;
3705beb8beefSJeff Roberson 
3706beb8beefSJeff Roberson 	CRITICAL_ASSERT(curthread);
3707beb8beefSJeff Roberson 
3708beb8beefSJeff Roberson 	/*
3709beb8beefSJeff Roberson 	 * If we have run out of items in our alloc bucket see
3710beb8beefSJeff Roberson 	 * if we can switch with the free bucket.
3711d4665eaaSJeff Roberson 	 *
3712d4665eaaSJeff Roberson 	 * SMR Zones can't re-use the free bucket until the sequence has
3713d4665eaaSJeff Roberson 	 * expired.
37148355f576SJeff Roberson 	 */
3715c6fd3e23SJeff Roberson 	if ((cache_uz_flags(cache) & UMA_ZONE_SMR) == 0 &&
3716d4665eaaSJeff Roberson 	    cache->uc_freebucket.ucb_cnt != 0) {
3717d4665eaaSJeff Roberson 		cache_bucket_swap(&cache->uc_freebucket,
3718d4665eaaSJeff Roberson 		    &cache->uc_allocbucket);
3719beb8beefSJeff Roberson 		return (true);
37208355f576SJeff Roberson 	}
3721fc03d22bSJeff Roberson 
3722fc03d22bSJeff Roberson 	/*
3723fc03d22bSJeff Roberson 	 * Discard any empty allocation bucket while we hold no locks.
3724fc03d22bSJeff Roberson 	 */
3725376b1ba3SJeff Roberson 	bucket = cache_bucket_unload_alloc(cache);
3726fc03d22bSJeff Roberson 	critical_exit();
3727c6fd3e23SJeff Roberson 
3728c6fd3e23SJeff Roberson 	if (bucket != NULL) {
3729c6fd3e23SJeff Roberson 		KASSERT(bucket->ub_cnt == 0,
3730c6fd3e23SJeff Roberson 		    ("cache_alloc: Entered with non-empty alloc bucket."));
37316fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
3732c6fd3e23SJeff Roberson 	}
3733fc03d22bSJeff Roberson 
37345d1ae027SRobert Watson 	/*
37355d1ae027SRobert Watson 	 * Attempt to retrieve the item from the per-CPU cache has failed, so
3736c6fd3e23SJeff Roberson 	 * we must go back to the zone.  This requires the zdom lock, so we
37375d1ae027SRobert Watson 	 * must drop the critical section, then re-acquire it when we go back
37385d1ae027SRobert Watson 	 * to the cache.  Since the critical section is released, we may be
37395d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
37405d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
37415d1ae027SRobert Watson 	 * the critical section.
37425d1ae027SRobert Watson 	 */
3743c1685086SJeff Roberson 	domain = PCPU_GET(domain);
37448c277118SMark Johnston 	if ((cache_uz_flags(cache) & UMA_ZONE_ROUNDROBIN) != 0 ||
37458c277118SMark Johnston 	    VM_DOMAIN_EMPTY(domain))
3746c6fd3e23SJeff Roberson 		domain = zone_domain_highest(zone, domain);
3747c6fd3e23SJeff Roberson 	bucket = cache_fetch_bucket(zone, cache, domain);
3748af32cefdSMark Johnston 	if (bucket == NULL && zone->uz_bucket_size != 0 && !bucketdisable) {
3749beb8beefSJeff Roberson 		bucket = zone_alloc_bucket(zone, udata, domain, flags);
3750c6fd3e23SJeff Roberson 		new = true;
3751af32cefdSMark Johnston 	} else {
3752c6fd3e23SJeff Roberson 		new = false;
3753af32cefdSMark Johnston 	}
3754c6fd3e23SJeff Roberson 
37551431a748SGleb Smirnoff 	CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
37561431a748SGleb Smirnoff 	    zone->uz_name, zone, bucket);
37574bd61e19SJeff Roberson 	if (bucket == NULL) {
3758fc03d22bSJeff Roberson 		critical_enter();
3759beb8beefSJeff Roberson 		return (false);
37604bd61e19SJeff Roberson 	}
37610f9b7bf3SMark Johnston 
3762fc03d22bSJeff Roberson 	/*
3763fc03d22bSJeff Roberson 	 * See if we lost the race or were migrated.  Cache the
3764fc03d22bSJeff Roberson 	 * initialized bucket to make this less likely or claim
3765fc03d22bSJeff Roberson 	 * the memory directly.
3766fc03d22bSJeff Roberson 	 */
37674bd61e19SJeff Roberson 	critical_enter();
3768cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3769376b1ba3SJeff Roberson 	if (cache->uc_allocbucket.ucb_bucket == NULL &&
3770c6fd3e23SJeff Roberson 	    ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) == 0 ||
37718c277118SMark Johnston 	    (curdomain = PCPU_GET(domain)) == domain ||
37728c277118SMark Johnston 	    VM_DOMAIN_EMPTY(curdomain))) {
3773c6fd3e23SJeff Roberson 		if (new)
3774c6fd3e23SJeff Roberson 			atomic_add_long(&ZDOM_GET(zone, domain)->uzd_imax,
3775c6fd3e23SJeff Roberson 			    bucket->ub_cnt);
3776376b1ba3SJeff Roberson 		cache_bucket_load_alloc(cache, bucket);
3777beb8beefSJeff Roberson 		return (true);
3778c6fd3e23SJeff Roberson 	}
3779c6fd3e23SJeff Roberson 
3780c6fd3e23SJeff Roberson 	/*
3781c6fd3e23SJeff Roberson 	 * We lost the race, release this bucket and start over.
3782c6fd3e23SJeff Roberson 	 */
3783c6fd3e23SJeff Roberson 	critical_exit();
37842760658bSAlexander Motin 	zone_put_bucket(zone, domain, bucket, udata, !new);
3785c6fd3e23SJeff Roberson 	critical_enter();
3786c6fd3e23SJeff Roberson 
3787beb8beefSJeff Roberson 	return (true);
3788bbee39c6SJeff Roberson }
3789bbee39c6SJeff Roberson 
3790ab3185d1SJeff Roberson void *
3791ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
3792bbee39c6SJeff Roberson {
379306d8bdcbSMark Johnston #ifdef NUMA
379406d8bdcbSMark Johnston 	uma_bucket_t bucket;
379506d8bdcbSMark Johnston 	uma_zone_domain_t zdom;
379606d8bdcbSMark Johnston 	void *item;
379706d8bdcbSMark Johnston #endif
3798ab3185d1SJeff Roberson 
3799ab3185d1SJeff Roberson 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
380019fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3801ab3185d1SJeff Roberson 
3802ab3185d1SJeff Roberson 	/* This is the fast path allocation */
3803e63a1c2fSRyan Libby 	CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d",
3804e63a1c2fSRyan Libby 	    zone->uz_name, zone, domain, flags);
3805ab3185d1SJeff Roberson 
3806ab3185d1SJeff Roberson 	if (flags & M_WAITOK) {
3807ab3185d1SJeff Roberson 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
3808ab3185d1SJeff Roberson 		    "uma_zalloc_domain: zone \"%s\"", zone->uz_name);
3809ab3185d1SJeff Roberson 	}
3810ab3185d1SJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3811ab3185d1SJeff Roberson 	    ("uma_zalloc_domain: called with spinlock or critical section held"));
381206d8bdcbSMark Johnston 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
381306d8bdcbSMark Johnston 	    ("uma_zalloc_domain: called with SMR zone."));
381406d8bdcbSMark Johnston #ifdef NUMA
381506d8bdcbSMark Johnston 	KASSERT((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0,
381606d8bdcbSMark Johnston 	    ("uma_zalloc_domain: called with non-FIRSTTOUCH zone."));
3817ab3185d1SJeff Roberson 
381806d8bdcbSMark Johnston 	if (vm_ndomains == 1)
381906d8bdcbSMark Johnston 		return (uma_zalloc_arg(zone, udata, flags));
382006d8bdcbSMark Johnston 
382106d8bdcbSMark Johnston 	/*
382206d8bdcbSMark Johnston 	 * Try to allocate from the bucket cache before falling back to the keg.
382306d8bdcbSMark Johnston 	 * We could try harder and attempt to allocate from per-CPU caches or
382406d8bdcbSMark Johnston 	 * the per-domain cross-domain buckets, but the complexity is probably
382506d8bdcbSMark Johnston 	 * not worth it.  It is more important that frees of previous
382606d8bdcbSMark Johnston 	 * cross-domain allocations do not blow up the cache.
382706d8bdcbSMark Johnston 	 */
382806d8bdcbSMark Johnston 	zdom = zone_domain_lock(zone, domain);
382906d8bdcbSMark Johnston 	if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) {
383006d8bdcbSMark Johnston 		item = bucket->ub_bucket[bucket->ub_cnt - 1];
383106d8bdcbSMark Johnston #ifdef INVARIANTS
383206d8bdcbSMark Johnston 		bucket->ub_bucket[bucket->ub_cnt - 1] = NULL;
383306d8bdcbSMark Johnston #endif
383406d8bdcbSMark Johnston 		bucket->ub_cnt--;
383506d8bdcbSMark Johnston 		zone_put_bucket(zone, domain, bucket, udata, true);
383606d8bdcbSMark Johnston 		item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata,
383706d8bdcbSMark Johnston 		    flags, item);
383806d8bdcbSMark Johnston 		if (item != NULL) {
383906d8bdcbSMark Johnston 			KASSERT(item_domain(item) == domain,
384006d8bdcbSMark Johnston 			    ("%s: bucket cache item %p from wrong domain",
384106d8bdcbSMark Johnston 			    __func__, item));
384206d8bdcbSMark Johnston 			counter_u64_add(zone->uz_allocs, 1);
384306d8bdcbSMark Johnston 		}
384406d8bdcbSMark Johnston 		return (item);
384506d8bdcbSMark Johnston 	}
384606d8bdcbSMark Johnston 	ZDOM_UNLOCK(zdom);
3847ab3185d1SJeff Roberson 	return (zone_alloc_item(zone, udata, domain, flags));
384806d8bdcbSMark Johnston #else
384906d8bdcbSMark Johnston 	return (uma_zalloc_arg(zone, udata, flags));
385006d8bdcbSMark Johnston #endif
3851ab3185d1SJeff Roberson }
3852ab3185d1SJeff Roberson 
3853ab3185d1SJeff Roberson /*
3854ab3185d1SJeff Roberson  * Find a slab with some space.  Prefer slabs that are partially used over those
3855ab3185d1SJeff Roberson  * that are totally full.  This helps to reduce fragmentation.
3856ab3185d1SJeff Roberson  *
3857ab3185d1SJeff Roberson  * If 'rr' is 1, search all domains starting from 'domain'.  Otherwise check
3858ab3185d1SJeff Roberson  * only 'domain'.
3859ab3185d1SJeff Roberson  */
3860ab3185d1SJeff Roberson static uma_slab_t
3861194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr)
3862ab3185d1SJeff Roberson {
3863ab3185d1SJeff Roberson 	uma_domain_t dom;
3864bbee39c6SJeff Roberson 	uma_slab_t slab;
3865ab3185d1SJeff Roberson 	int start;
3866ab3185d1SJeff Roberson 
3867ab3185d1SJeff Roberson 	KASSERT(domain >= 0 && domain < vm_ndomains,
3868ab3185d1SJeff Roberson 	    ("keg_first_slab: domain %d out of range", domain));
38698b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, domain);
3870ab3185d1SJeff Roberson 
3871ab3185d1SJeff Roberson 	slab = NULL;
3872ab3185d1SJeff Roberson 	start = domain;
3873ab3185d1SJeff Roberson 	do {
3874ab3185d1SJeff Roberson 		dom = &keg->uk_domain[domain];
38754ab3aee8SMark Johnston 		if ((slab = LIST_FIRST(&dom->ud_part_slab)) != NULL)
38764ab3aee8SMark Johnston 			return (slab);
38774ab3aee8SMark Johnston 		if ((slab = LIST_FIRST(&dom->ud_free_slab)) != NULL) {
3878ab3185d1SJeff Roberson 			LIST_REMOVE(slab, us_link);
38794ab3aee8SMark Johnston 			dom->ud_free_slabs--;
3880ab3185d1SJeff Roberson 			LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
3881ab3185d1SJeff Roberson 			return (slab);
3882ab3185d1SJeff Roberson 		}
3883ab3185d1SJeff Roberson 		if (rr)
3884ab3185d1SJeff Roberson 			domain = (domain + 1) % vm_ndomains;
3885ab3185d1SJeff Roberson 	} while (domain != start);
3886ab3185d1SJeff Roberson 
3887ab3185d1SJeff Roberson 	return (NULL);
3888ab3185d1SJeff Roberson }
3889ab3185d1SJeff Roberson 
38908b987a77SJeff Roberson /*
38918b987a77SJeff Roberson  * Fetch an existing slab from a free or partial list.  Returns with the
38928b987a77SJeff Roberson  * keg domain lock held if a slab was found or unlocked if not.
38938b987a77SJeff Roberson  */
3894ab3185d1SJeff Roberson static uma_slab_t
3895194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags)
3896ab3185d1SJeff Roberson {
38978b987a77SJeff Roberson 	uma_slab_t slab;
3898194a979eSMark Johnston 	uint32_t reserve;
3899099a0e58SBosko Milekic 
39008b987a77SJeff Roberson 	/* HASH has a single free list. */
390154c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0)
39028b987a77SJeff Roberson 		domain = 0;
3903194a979eSMark Johnston 
39048b987a77SJeff Roberson 	KEG_LOCK(keg, domain);
3905194a979eSMark Johnston 	reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve;
39064ab3aee8SMark Johnston 	if (keg->uk_domain[domain].ud_free_items <= reserve ||
39078b987a77SJeff Roberson 	    (slab = keg_first_slab(keg, domain, rr)) == NULL) {
39088b987a77SJeff Roberson 		KEG_UNLOCK(keg, domain);
3909194a979eSMark Johnston 		return (NULL);
39108b987a77SJeff Roberson 	}
39118b987a77SJeff Roberson 	return (slab);
3912194a979eSMark Johnston }
3913194a979eSMark Johnston 
3914194a979eSMark Johnston static uma_slab_t
3915194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags)
3916194a979eSMark Johnston {
3917194a979eSMark Johnston 	struct vm_domainset_iter di;
3918194a979eSMark Johnston 	uma_slab_t slab;
3919194a979eSMark Johnston 	int aflags, domain;
3920194a979eSMark Johnston 	bool rr;
3921194a979eSMark Johnston 
3922194a979eSMark Johnston restart:
3923bbee39c6SJeff Roberson 	/*
3924194a979eSMark Johnston 	 * Use the keg's policy if upper layers haven't already specified a
3925194a979eSMark Johnston 	 * domain (as happens with first-touch zones).
3926194a979eSMark Johnston 	 *
3927194a979eSMark Johnston 	 * To avoid races we run the iterator with the keg lock held, but that
3928194a979eSMark Johnston 	 * means that we cannot allow the vm_domainset layer to sleep.  Thus,
3929194a979eSMark Johnston 	 * clear M_WAITOK and handle low memory conditions locally.
3930bbee39c6SJeff Roberson 	 */
3931ab3185d1SJeff Roberson 	rr = rdomain == UMA_ANYDOMAIN;
3932ab3185d1SJeff Roberson 	if (rr) {
3933194a979eSMark Johnston 		aflags = (flags & ~M_WAITOK) | M_NOWAIT;
3934194a979eSMark Johnston 		vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
3935194a979eSMark Johnston 		    &aflags);
3936194a979eSMark Johnston 	} else {
3937194a979eSMark Johnston 		aflags = flags;
3938194a979eSMark Johnston 		domain = rdomain;
3939194a979eSMark Johnston 	}
3940ab3185d1SJeff Roberson 
3941194a979eSMark Johnston 	for (;;) {
3942194a979eSMark Johnston 		slab = keg_fetch_free_slab(keg, domain, rr, flags);
3943584061b4SJeff Roberson 		if (slab != NULL)
3944bbee39c6SJeff Roberson 			return (slab);
3945bbee39c6SJeff Roberson 
3946bbee39c6SJeff Roberson 		/*
3947bbee39c6SJeff Roberson 		 * M_NOVM means don't ask at all!
3948bbee39c6SJeff Roberson 		 */
3949bbee39c6SJeff Roberson 		if (flags & M_NOVM)
3950bbee39c6SJeff Roberson 			break;
3951bbee39c6SJeff Roberson 
395286220393SMark Johnston 		slab = keg_alloc_slab(keg, zone, domain, flags, aflags);
39538b987a77SJeff Roberson 		if (slab != NULL)
3954bbee39c6SJeff Roberson 			return (slab);
39553639ac42SJeff Roberson 		if (!rr && (flags & M_WAITOK) == 0)
39563639ac42SJeff Roberson 			break;
3957194a979eSMark Johnston 		if (rr && vm_domainset_iter_policy(&di, &domain) != 0) {
3958194a979eSMark Johnston 			if ((flags & M_WAITOK) != 0) {
395989d2fb14SKonstantin Belousov 				vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0);
3960194a979eSMark Johnston 				goto restart;
396130c5525bSAndrew Gallatin 			}
3962194a979eSMark Johnston 			break;
3963194a979eSMark Johnston 		}
3964ab3185d1SJeff Roberson 	}
3965ab3185d1SJeff Roberson 
3966bbee39c6SJeff Roberson 	/*
3967bbee39c6SJeff Roberson 	 * We might not have been able to get a slab but another cpu
3968bbee39c6SJeff Roberson 	 * could have while we were unlocked.  Check again before we
3969bbee39c6SJeff Roberson 	 * fail.
3970bbee39c6SJeff Roberson 	 */
39718b987a77SJeff Roberson 	if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL)
3972bbee39c6SJeff Roberson 		return (slab);
39738b987a77SJeff Roberson 
3974ab3185d1SJeff Roberson 	return (NULL);
3975ab3185d1SJeff Roberson }
3976bbee39c6SJeff Roberson 
3977d56368d7SBosko Milekic static void *
39780095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
3979bbee39c6SJeff Roberson {
3980ab3185d1SJeff Roberson 	uma_domain_t dom;
3981bbee39c6SJeff Roberson 	void *item;
39829b8db4d0SRyan Libby 	int freei;
3983bbee39c6SJeff Roberson 
39848b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, slab->us_domain);
3985099a0e58SBosko Milekic 
39868b987a77SJeff Roberson 	dom = &keg->uk_domain[slab->us_domain];
39879b78b1f4SJeff Roberson 	freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1;
39889b78b1f4SJeff Roberson 	BIT_CLR(keg->uk_ipers, freei, &slab->us_free);
39891e0701e1SJeff Roberson 	item = slab_item(slab, keg, freei);
3990bbee39c6SJeff Roberson 	slab->us_freecount--;
39914ab3aee8SMark Johnston 	dom->ud_free_items--;
3992ef72505eSJeff Roberson 
39934ab3aee8SMark Johnston 	/*
39944ab3aee8SMark Johnston 	 * Move this slab to the full list.  It must be on the partial list, so
39954ab3aee8SMark Johnston 	 * we do not need to update the free slab count.  In particular,
39964ab3aee8SMark Johnston 	 * keg_fetch_slab() always returns slabs on the partial list.
39974ab3aee8SMark Johnston 	 */
3998bbee39c6SJeff Roberson 	if (slab->us_freecount == 0) {
3999bbee39c6SJeff Roberson 		LIST_REMOVE(slab, us_link);
4000ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link);
4001bbee39c6SJeff Roberson 	}
4002bbee39c6SJeff Roberson 
4003bbee39c6SJeff Roberson 	return (item);
4004bbee39c6SJeff Roberson }
4005bbee39c6SJeff Roberson 
4006bbee39c6SJeff Roberson static int
4007b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags)
40080095a784SJeff Roberson {
40098b987a77SJeff Roberson 	uma_domain_t dom;
4010b75c4efcSAndrew Turner 	uma_zone_t zone;
40110095a784SJeff Roberson 	uma_slab_t slab;
40120095a784SJeff Roberson 	uma_keg_t keg;
4013a03af342SSean Bruno #ifdef NUMA
4014ab3185d1SJeff Roberson 	int stripe;
4015a03af342SSean Bruno #endif
40160095a784SJeff Roberson 	int i;
40170095a784SJeff Roberson 
4018b75c4efcSAndrew Turner 	zone = arg;
40190095a784SJeff Roberson 	slab = NULL;
4020584061b4SJeff Roberson 	keg = zone->uz_keg;
4021af526374SJeff Roberson 	/* Try to keep the buckets totally full */
40220095a784SJeff Roberson 	for (i = 0; i < max; ) {
4023584061b4SJeff Roberson 		if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL)
40240095a784SJeff Roberson 			break;
4025a03af342SSean Bruno #ifdef NUMA
4026ab3185d1SJeff Roberson 		stripe = howmany(max, vm_ndomains);
4027a03af342SSean Bruno #endif
40288b987a77SJeff Roberson 		dom = &keg->uk_domain[slab->us_domain];
40291b2dcc8cSMark Johnston 		do {
40300095a784SJeff Roberson 			bucket[i++] = slab_alloc_item(keg, slab);
40311b2dcc8cSMark Johnston 			if (dom->ud_free_items <= keg->uk_reserve) {
40321b2dcc8cSMark Johnston 				/*
40331b2dcc8cSMark Johnston 				 * Avoid depleting the reserve after a
40341b2dcc8cSMark Johnston 				 * successful item allocation, even if
40351b2dcc8cSMark Johnston 				 * M_USE_RESERVE is specified.
40361b2dcc8cSMark Johnston 				 */
40371b2dcc8cSMark Johnston 				KEG_UNLOCK(keg, slab->us_domain);
40381b2dcc8cSMark Johnston 				goto out;
40391b2dcc8cSMark Johnston 			}
4040b6715dabSJeff Roberson #ifdef NUMA
4041ab3185d1SJeff Roberson 			/*
4042ab3185d1SJeff Roberson 			 * If the zone is striped we pick a new slab for every
4043ab3185d1SJeff Roberson 			 * N allocations.  Eliminating this conditional will
4044ab3185d1SJeff Roberson 			 * instead pick a new domain for each bucket rather
4045ab3185d1SJeff Roberson 			 * than stripe within each bucket.  The current option
4046ab3185d1SJeff Roberson 			 * produces more fragmentation and requires more cpu
4047ab3185d1SJeff Roberson 			 * time but yields better distribution.
4048ab3185d1SJeff Roberson 			 */
4049dfe13344SJeff Roberson 			if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 &&
4050ab3185d1SJeff Roberson 			    vm_ndomains > 1 && --stripe == 0)
4051ab3185d1SJeff Roberson 				break;
4052ab3185d1SJeff Roberson #endif
40531b2dcc8cSMark Johnston 		} while (slab->us_freecount != 0 && i < max);
40548b987a77SJeff Roberson 		KEG_UNLOCK(keg, slab->us_domain);
40551b2dcc8cSMark Johnston 
4056ab3185d1SJeff Roberson 		/* Don't block if we allocated any successfully. */
40570095a784SJeff Roberson 		flags &= ~M_WAITOK;
40580095a784SJeff Roberson 		flags |= M_NOWAIT;
40590095a784SJeff Roberson 	}
40601b2dcc8cSMark Johnston out:
40610095a784SJeff Roberson 	return i;
40620095a784SJeff Roberson }
40630095a784SJeff Roberson 
40644bd61e19SJeff Roberson static int
40654bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags)
40664bd61e19SJeff Roberson {
40674bd61e19SJeff Roberson 	uint64_t old, new, total, max;
40684bd61e19SJeff Roberson 
40694bd61e19SJeff Roberson 	/*
40704bd61e19SJeff Roberson 	 * The hard case.  We're going to sleep because there were existing
40714bd61e19SJeff Roberson 	 * sleepers or because we ran out of items.  This routine enforces
40724bd61e19SJeff Roberson 	 * fairness by keeping fifo order.
40734bd61e19SJeff Roberson 	 *
40744bd61e19SJeff Roberson 	 * First release our ill gotten gains and make some noise.
40754bd61e19SJeff Roberson 	 */
40764bd61e19SJeff Roberson 	for (;;) {
40774bd61e19SJeff Roberson 		zone_free_limit(zone, count);
40784bd61e19SJeff Roberson 		zone_log_warning(zone);
40794bd61e19SJeff Roberson 		zone_maxaction(zone);
40804bd61e19SJeff Roberson 		if (flags & M_NOWAIT)
40814bd61e19SJeff Roberson 			return (0);
40824bd61e19SJeff Roberson 
40834bd61e19SJeff Roberson 		/*
40844bd61e19SJeff Roberson 		 * We need to allocate an item or set ourself as a sleeper
40854bd61e19SJeff Roberson 		 * while the sleepq lock is held to avoid wakeup races.  This
40864bd61e19SJeff Roberson 		 * is essentially a home rolled semaphore.
40874bd61e19SJeff Roberson 		 */
40884bd61e19SJeff Roberson 		sleepq_lock(&zone->uz_max_items);
40894bd61e19SJeff Roberson 		old = zone->uz_items;
40904bd61e19SJeff Roberson 		do {
40914bd61e19SJeff Roberson 			MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX);
40924bd61e19SJeff Roberson 			/* Cache the max since we will evaluate twice. */
40934bd61e19SJeff Roberson 			max = zone->uz_max_items;
40944bd61e19SJeff Roberson 			if (UZ_ITEMS_SLEEPERS(old) != 0 ||
40954bd61e19SJeff Roberson 			    UZ_ITEMS_COUNT(old) >= max)
40964bd61e19SJeff Roberson 				new = old + UZ_ITEMS_SLEEPER;
40974bd61e19SJeff Roberson 			else
40984bd61e19SJeff Roberson 				new = old + MIN(count, max - old);
40994bd61e19SJeff Roberson 		} while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0);
41004bd61e19SJeff Roberson 
41014bd61e19SJeff Roberson 		/* We may have successfully allocated under the sleepq lock. */
41024bd61e19SJeff Roberson 		if (UZ_ITEMS_SLEEPERS(new) == 0) {
41034bd61e19SJeff Roberson 			sleepq_release(&zone->uz_max_items);
41044bd61e19SJeff Roberson 			return (new - old);
41054bd61e19SJeff Roberson 		}
41064bd61e19SJeff Roberson 
41074bd61e19SJeff Roberson 		/*
41084bd61e19SJeff Roberson 		 * This is in a different cacheline from uz_items so that we
41094bd61e19SJeff Roberson 		 * don't constantly invalidate the fastpath cacheline when we
41104bd61e19SJeff Roberson 		 * adjust item counts.  This could be limited to toggling on
41114bd61e19SJeff Roberson 		 * transitions.
41124bd61e19SJeff Roberson 		 */
41134bd61e19SJeff Roberson 		atomic_add_32(&zone->uz_sleepers, 1);
41144bd61e19SJeff Roberson 		atomic_add_64(&zone->uz_sleeps, 1);
41154bd61e19SJeff Roberson 
41164bd61e19SJeff Roberson 		/*
41174bd61e19SJeff Roberson 		 * We have added ourselves as a sleeper.  The sleepq lock
41184bd61e19SJeff Roberson 		 * protects us from wakeup races.  Sleep now and then retry.
41194bd61e19SJeff Roberson 		 */
41204bd61e19SJeff Roberson 		sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0);
41214bd61e19SJeff Roberson 		sleepq_wait(&zone->uz_max_items, PVM);
41224bd61e19SJeff Roberson 
41234bd61e19SJeff Roberson 		/*
41244bd61e19SJeff Roberson 		 * After wakeup, remove ourselves as a sleeper and try
41254bd61e19SJeff Roberson 		 * again.  We no longer have the sleepq lock for protection.
41264bd61e19SJeff Roberson 		 *
41274bd61e19SJeff Roberson 		 * Subract ourselves as a sleeper while attempting to add
41284bd61e19SJeff Roberson 		 * our count.
41294bd61e19SJeff Roberson 		 */
41304bd61e19SJeff Roberson 		atomic_subtract_32(&zone->uz_sleepers, 1);
41314bd61e19SJeff Roberson 		old = atomic_fetchadd_64(&zone->uz_items,
41324bd61e19SJeff Roberson 		    -(UZ_ITEMS_SLEEPER - count));
41334bd61e19SJeff Roberson 		/* We're no longer a sleeper. */
41344bd61e19SJeff Roberson 		old -= UZ_ITEMS_SLEEPER;
41354bd61e19SJeff Roberson 
41364bd61e19SJeff Roberson 		/*
41374bd61e19SJeff Roberson 		 * If we're still at the limit, restart.  Notably do not
41384bd61e19SJeff Roberson 		 * block on other sleepers.  Cache the max value to protect
41394bd61e19SJeff Roberson 		 * against changes via sysctl.
41404bd61e19SJeff Roberson 		 */
41414bd61e19SJeff Roberson 		total = UZ_ITEMS_COUNT(old);
41424bd61e19SJeff Roberson 		max = zone->uz_max_items;
41434bd61e19SJeff Roberson 		if (total >= max)
41444bd61e19SJeff Roberson 			continue;
41454bd61e19SJeff Roberson 		/* Truncate if necessary, otherwise wake other sleepers. */
41464bd61e19SJeff Roberson 		if (total + count > max) {
41474bd61e19SJeff Roberson 			zone_free_limit(zone, total + count - max);
41484bd61e19SJeff Roberson 			count = max - total;
41494bd61e19SJeff Roberson 		} else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0)
41504bd61e19SJeff Roberson 			wakeup_one(&zone->uz_max_items);
41514bd61e19SJeff Roberson 
41524bd61e19SJeff Roberson 		return (count);
41534bd61e19SJeff Roberson 	}
41544bd61e19SJeff Roberson }
41554bd61e19SJeff Roberson 
41564bd61e19SJeff Roberson /*
41574bd61e19SJeff Roberson  * Allocate 'count' items from our max_items limit.  Returns the number
41584bd61e19SJeff Roberson  * available.  If M_NOWAIT is not specified it will sleep until at least
41594bd61e19SJeff Roberson  * one item can be allocated.
41604bd61e19SJeff Roberson  */
41614bd61e19SJeff Roberson static int
41624bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags)
41634bd61e19SJeff Roberson {
41644bd61e19SJeff Roberson 	uint64_t old;
41654bd61e19SJeff Roberson 	uint64_t max;
41664bd61e19SJeff Roberson 
41674bd61e19SJeff Roberson 	max = zone->uz_max_items;
41684bd61e19SJeff Roberson 	MPASS(max > 0);
41694bd61e19SJeff Roberson 
41704bd61e19SJeff Roberson 	/*
41714bd61e19SJeff Roberson 	 * We expect normal allocations to succeed with a simple
41724bd61e19SJeff Roberson 	 * fetchadd.
41734bd61e19SJeff Roberson 	 */
41744bd61e19SJeff Roberson 	old = atomic_fetchadd_64(&zone->uz_items, count);
41754bd61e19SJeff Roberson 	if (__predict_true(old + count <= max))
41764bd61e19SJeff Roberson 		return (count);
41774bd61e19SJeff Roberson 
41784bd61e19SJeff Roberson 	/*
41794bd61e19SJeff Roberson 	 * If we had some items and no sleepers just return the
41804bd61e19SJeff Roberson 	 * truncated value.  We have to release the excess space
41814bd61e19SJeff Roberson 	 * though because that may wake sleepers who weren't woken
41824bd61e19SJeff Roberson 	 * because we were temporarily over the limit.
41834bd61e19SJeff Roberson 	 */
41844bd61e19SJeff Roberson 	if (old < max) {
41854bd61e19SJeff Roberson 		zone_free_limit(zone, (old + count) - max);
41864bd61e19SJeff Roberson 		return (max - old);
41874bd61e19SJeff Roberson 	}
41884bd61e19SJeff Roberson 	return (zone_alloc_limit_hard(zone, count, flags));
41894bd61e19SJeff Roberson }
41904bd61e19SJeff Roberson 
41914bd61e19SJeff Roberson /*
41924bd61e19SJeff Roberson  * Free a number of items back to the limit.
41934bd61e19SJeff Roberson  */
41944bd61e19SJeff Roberson static void
41954bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count)
41964bd61e19SJeff Roberson {
41974bd61e19SJeff Roberson 	uint64_t old;
41984bd61e19SJeff Roberson 
41994bd61e19SJeff Roberson 	MPASS(count > 0);
42004bd61e19SJeff Roberson 
42014bd61e19SJeff Roberson 	/*
42024bd61e19SJeff Roberson 	 * In the common case we either have no sleepers or
42034bd61e19SJeff Roberson 	 * are still over the limit and can just return.
42044bd61e19SJeff Roberson 	 */
42054bd61e19SJeff Roberson 	old = atomic_fetchadd_64(&zone->uz_items, -count);
42064bd61e19SJeff Roberson 	if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 ||
42074bd61e19SJeff Roberson 	   UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items))
42084bd61e19SJeff Roberson 		return;
42094bd61e19SJeff Roberson 
42104bd61e19SJeff Roberson 	/*
42114bd61e19SJeff Roberson 	 * Moderate the rate of wakeups.  Sleepers will continue
42124bd61e19SJeff Roberson 	 * to generate wakeups if necessary.
42134bd61e19SJeff Roberson 	 */
42144bd61e19SJeff Roberson 	wakeup_one(&zone->uz_max_items);
42154bd61e19SJeff Roberson }
42164bd61e19SJeff Roberson 
4217fc03d22bSJeff Roberson static uma_bucket_t
4218beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags)
4219bbee39c6SJeff Roberson {
4220bbee39c6SJeff Roberson 	uma_bucket_t bucket;
422109c8cb71SMark Johnston 	int error, maxbucket, cnt;
4222bbee39c6SJeff Roberson 
4223e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name,
4224e63a1c2fSRyan Libby 	    zone, domain);
422530c5525bSAndrew Gallatin 
4226c1685086SJeff Roberson 	/* Avoid allocs targeting empty domains. */
4227c1685086SJeff Roberson 	if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
4228c1685086SJeff Roberson 		domain = UMA_ANYDOMAIN;
42298c277118SMark Johnston 	else if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0)
4230c6fd3e23SJeff Roberson 		domain = UMA_ANYDOMAIN;
4231c1685086SJeff Roberson 
42324bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
42334bd61e19SJeff Roberson 		maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size,
42344bd61e19SJeff Roberson 		    M_NOWAIT);
42354bd61e19SJeff Roberson 	else
423620a4e154SJeff Roberson 		maxbucket = zone->uz_bucket_size;
42374bd61e19SJeff Roberson 	if (maxbucket == 0)
42384bd61e19SJeff Roberson 		return (false);
4239beb8beefSJeff Roberson 
42406fd34d6fSJeff Roberson 	/* Don't wait for buckets, preserve caller's NOVM setting. */
42416fd34d6fSJeff Roberson 	bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
4242beb8beefSJeff Roberson 	if (bucket == NULL) {
4243beb8beefSJeff Roberson 		cnt = 0;
4244beb8beefSJeff Roberson 		goto out;
4245beb8beefSJeff Roberson 	}
42460095a784SJeff Roberson 
42470095a784SJeff Roberson 	bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
4248beb8beefSJeff Roberson 	    MIN(maxbucket, bucket->ub_entries), domain, flags);
42490095a784SJeff Roberson 
42500095a784SJeff Roberson 	/*
42510095a784SJeff Roberson 	 * Initialize the memory if necessary.
42520095a784SJeff Roberson 	 */
42530095a784SJeff Roberson 	if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
4254099a0e58SBosko Milekic 		int i;
4255bbee39c6SJeff Roberson 
425609c8cb71SMark Johnston 		for (i = 0; i < bucket->ub_cnt; i++) {
425709c8cb71SMark Johnston 			kasan_mark_item_valid(zone, bucket->ub_bucket[i]);
425809c8cb71SMark Johnston 			error = zone->uz_init(bucket->ub_bucket[i],
425909c8cb71SMark Johnston 			    zone->uz_size, flags);
426009c8cb71SMark Johnston 			kasan_mark_item_invalid(zone, bucket->ub_bucket[i]);
426109c8cb71SMark Johnston 			if (error != 0)
4262b23f72e9SBrian Feldman 				break;
426309c8cb71SMark Johnston 		}
426409c8cb71SMark Johnston 
4265b23f72e9SBrian Feldman 		/*
4266b23f72e9SBrian Feldman 		 * If we couldn't initialize the whole bucket, put the
4267b23f72e9SBrian Feldman 		 * rest back onto the freelist.
4268b23f72e9SBrian Feldman 		 */
4269b23f72e9SBrian Feldman 		if (i != bucket->ub_cnt) {
4270af526374SJeff Roberson 			zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
42710095a784SJeff Roberson 			    bucket->ub_cnt - i);
4272a5a262c6SBosko Milekic #ifdef INVARIANTS
42730095a784SJeff Roberson 			bzero(&bucket->ub_bucket[i],
42740095a784SJeff Roberson 			    sizeof(void *) * (bucket->ub_cnt - i));
4275a5a262c6SBosko Milekic #endif
4276b23f72e9SBrian Feldman 			bucket->ub_cnt = i;
4277b23f72e9SBrian Feldman 		}
4278099a0e58SBosko Milekic 	}
4279099a0e58SBosko Milekic 
4280beb8beefSJeff Roberson 	cnt = bucket->ub_cnt;
4281f7104ccdSAlexander Motin 	if (bucket->ub_cnt == 0) {
42826fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
42832efcc8cbSGleb Smirnoff 		counter_u64_add(zone->uz_fails, 1);
4284beb8beefSJeff Roberson 		bucket = NULL;
4285beb8beefSJeff Roberson 	}
4286beb8beefSJeff Roberson out:
42874bd61e19SJeff Roberson 	if (zone->uz_max_items > 0 && cnt < maxbucket)
42884bd61e19SJeff Roberson 		zone_free_limit(zone, maxbucket - cnt);
4289fc03d22bSJeff Roberson 
4290fc03d22bSJeff Roberson 	return (bucket);
4291fc03d22bSJeff Roberson }
4292fc03d22bSJeff Roberson 
42938355f576SJeff Roberson /*
42940095a784SJeff Roberson  * Allocates a single item from a zone.
42958355f576SJeff Roberson  *
42968355f576SJeff Roberson  * Arguments
42978355f576SJeff Roberson  *	zone   The zone to alloc for.
42988355f576SJeff Roberson  *	udata  The data to be passed to the constructor.
4299ab3185d1SJeff Roberson  *	domain The domain to allocate from or UMA_ANYDOMAIN.
4300a163d034SWarner Losh  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
43018355f576SJeff Roberson  *
43028355f576SJeff Roberson  * Returns
43038355f576SJeff Roberson  *	NULL if there is no memory and M_NOWAIT is set
4304bbee39c6SJeff Roberson  *	An item if successful
43058355f576SJeff Roberson  */
43068355f576SJeff Roberson 
43078355f576SJeff Roberson static void *
4308ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags)
43098355f576SJeff Roberson {
43108355f576SJeff Roberson 	void *item;
43118355f576SJeff Roberson 
4312791dda87SAndrew Gallatin 	if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) {
4313791dda87SAndrew Gallatin 		counter_u64_add(zone->uz_fails, 1);
4314bb15d1c7SGleb Smirnoff 		return (NULL);
4315791dda87SAndrew Gallatin 	}
43168355f576SJeff Roberson 
4317c1685086SJeff Roberson 	/* Avoid allocs targeting empty domains. */
4318c1685086SJeff Roberson 	if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
431930c5525bSAndrew Gallatin 		domain = UMA_ANYDOMAIN;
4320c1685086SJeff Roberson 
4321ab3185d1SJeff Roberson 	if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1)
4322beb8beefSJeff Roberson 		goto fail_cnt;
43238355f576SJeff Roberson 
4324099a0e58SBosko Milekic 	/*
4325099a0e58SBosko Milekic 	 * We have to call both the zone's init (not the keg's init)
4326099a0e58SBosko Milekic 	 * and the zone's ctor.  This is because the item is going from
4327099a0e58SBosko Milekic 	 * a keg slab directly to the user, and the user is expecting it
4328099a0e58SBosko Milekic 	 * to be both zone-init'd as well as zone-ctor'd.
4329099a0e58SBosko Milekic 	 */
4330b23f72e9SBrian Feldman 	if (zone->uz_init != NULL) {
433109c8cb71SMark Johnston 		int error;
433209c8cb71SMark Johnston 
433309c8cb71SMark Johnston 		kasan_mark_item_valid(zone, item);
433409c8cb71SMark Johnston 		error = zone->uz_init(item, zone->uz_size, flags);
433509c8cb71SMark Johnston 		kasan_mark_item_invalid(zone, item);
433609c8cb71SMark Johnston 		if (error != 0) {
4337bb15d1c7SGleb Smirnoff 			zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT);
4338beb8beefSJeff Roberson 			goto fail_cnt;
4339beb8beefSJeff Roberson 		}
4340beb8beefSJeff Roberson 	}
4341d4665eaaSJeff Roberson 	item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags,
4342d4665eaaSJeff Roberson 	    item);
4343beb8beefSJeff Roberson 	if (item == NULL)
43440095a784SJeff Roberson 		goto fail;
43458355f576SJeff Roberson 
43462efcc8cbSGleb Smirnoff 	counter_u64_add(zone->uz_allocs, 1);
43471431a748SGleb Smirnoff 	CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
43481431a748SGleb Smirnoff 	    zone->uz_name, zone);
43491431a748SGleb Smirnoff 
43508355f576SJeff Roberson 	return (item);
43510095a784SJeff Roberson 
4352beb8beefSJeff Roberson fail_cnt:
4353beb8beefSJeff Roberson 	counter_u64_add(zone->uz_fails, 1);
43540095a784SJeff Roberson fail:
43554bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
43564bd61e19SJeff Roberson 		zone_free_limit(zone, 1);
43571431a748SGleb Smirnoff 	CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
43581431a748SGleb Smirnoff 	    zone->uz_name, zone);
43594bd61e19SJeff Roberson 
43600095a784SJeff Roberson 	return (NULL);
43618355f576SJeff Roberson }
43628355f576SJeff Roberson 
43638355f576SJeff Roberson /* See uma.h */
43648355f576SJeff Roberson void
4365d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item)
4366d4665eaaSJeff Roberson {
4367d4665eaaSJeff Roberson 	uma_cache_t cache;
4368d4665eaaSJeff Roberson 	uma_cache_bucket_t bucket;
4369c6fd3e23SJeff Roberson 	int itemdomain, uz_flags;
4370d4665eaaSJeff Roberson 
4371d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
4372d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
4373952c8964SMark Johnston 	    ("uma_zfree_smr: called with non-SMR zone."));
4374d4665eaaSJeff Roberson 	KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer."));
4375c6fd3e23SJeff Roberson 	SMR_ASSERT_NOT_ENTERED(zone->uz_smr);
4376d4665eaaSJeff Roberson 	if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN)
4377d4665eaaSJeff Roberson 		return;
4378d4665eaaSJeff Roberson #endif
4379d4665eaaSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4380d4665eaaSJeff Roberson 	uz_flags = cache_uz_flags(cache);
4381c6fd3e23SJeff Roberson 	itemdomain = 0;
4382d4665eaaSJeff Roberson #ifdef NUMA
4383d4665eaaSJeff Roberson 	if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
438481302f1dSMark Johnston 		itemdomain = item_domain(item);
4385d4665eaaSJeff Roberson #endif
4386d4665eaaSJeff Roberson 	critical_enter();
4387d4665eaaSJeff Roberson 	do {
4388d4665eaaSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
4389d4665eaaSJeff Roberson 		/* SMR Zones must free to the free bucket. */
4390d4665eaaSJeff Roberson 		bucket = &cache->uc_freebucket;
4391d4665eaaSJeff Roberson #ifdef NUMA
4392d4665eaaSJeff Roberson 		if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4393c6fd3e23SJeff Roberson 		    PCPU_GET(domain) != itemdomain) {
4394d4665eaaSJeff Roberson 			bucket = &cache->uc_crossbucket;
4395d4665eaaSJeff Roberson 		}
4396d4665eaaSJeff Roberson #endif
4397d4665eaaSJeff Roberson 		if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
4398d4665eaaSJeff Roberson 			cache_bucket_push(cache, bucket, item);
4399d4665eaaSJeff Roberson 			critical_exit();
4400d4665eaaSJeff Roberson 			return;
4401d4665eaaSJeff Roberson 		}
4402d4665eaaSJeff Roberson 	} while (cache_free(zone, cache, NULL, item, itemdomain));
4403d4665eaaSJeff Roberson 	critical_exit();
4404d4665eaaSJeff Roberson 
4405d4665eaaSJeff Roberson 	/*
4406d4665eaaSJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
4407d4665eaaSJeff Roberson 	 */
4408d4665eaaSJeff Roberson 	zone_free_item(zone, item, NULL, SKIP_NONE);
4409d4665eaaSJeff Roberson }
4410d4665eaaSJeff Roberson 
4411d4665eaaSJeff Roberson /* See uma.h */
4412d4665eaaSJeff Roberson void
44138355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
44148355f576SJeff Roberson {
44158355f576SJeff Roberson 	uma_cache_t cache;
4416376b1ba3SJeff Roberson 	uma_cache_bucket_t bucket;
4417c6fd3e23SJeff Roberson 	int itemdomain, uz_flags;
44188355f576SJeff Roberson 
4419e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
442019fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
442110cb2424SMark Murray 
4422e63a1c2fSRyan Libby 	CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone);
44233659f747SRobert Watson 
4424d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
4425d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
4426952c8964SMark Johnston 	    ("uma_zfree_arg: called with SMR zone."));
4427d4665eaaSJeff Roberson 	if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN)
4428d4665eaaSJeff Roberson 		return;
4429d4665eaaSJeff Roberson #endif
443020ed0cb0SMatthew D Fleming         /* uma_zfree(..., NULL) does nothing, to match free(9). */
443120ed0cb0SMatthew D Fleming         if (item == NULL)
443220ed0cb0SMatthew D Fleming                 return;
4433cc7ce83aSJeff Roberson 
4434cc7ce83aSJeff Roberson 	/*
4435cc7ce83aSJeff Roberson 	 * We are accessing the per-cpu cache without a critical section to
4436cc7ce83aSJeff Roberson 	 * fetch size and flags.  This is acceptable, if we are preempted we
4437cc7ce83aSJeff Roberson 	 * will simply read another cpu's line.
4438cc7ce83aSJeff Roberson 	 */
4439cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4440cc7ce83aSJeff Roberson 	uz_flags = cache_uz_flags(cache);
4441d4665eaaSJeff Roberson 	if (UMA_ALWAYS_CTORDTOR ||
4442d4665eaaSJeff Roberson 	    __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0))
4443cc7ce83aSJeff Roberson 		item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE);
4444ef72505eSJeff Roberson 
4445af7f9b97SJeff Roberson 	/*
4446af7f9b97SJeff Roberson 	 * The race here is acceptable.  If we miss it we'll just have to wait
4447af7f9b97SJeff Roberson 	 * a little longer for the limits to be reset.
4448af7f9b97SJeff Roberson 	 */
4449cc7ce83aSJeff Roberson 	if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) {
44508a6776caSMark Johnston 		if (atomic_load_32(&zone->uz_sleepers) > 0)
4451fc03d22bSJeff Roberson 			goto zfree_item;
4452cc7ce83aSJeff Roberson 	}
4453af7f9b97SJeff Roberson 
44545d1ae027SRobert Watson 	/*
44555d1ae027SRobert Watson 	 * If possible, free to the per-CPU cache.  There are two
44565d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
44575d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
44585d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
44595d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
44605d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
44615d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to free to the
44625d1ae027SRobert Watson 	 * current cache; when we re-acquire the critical section, we must
44635d1ae027SRobert Watson 	 * detect and handle migration if it has occurred.
44645d1ae027SRobert Watson 	 */
4465c6fd3e23SJeff Roberson 	itemdomain = 0;
4466dfe13344SJeff Roberson #ifdef NUMA
4467dfe13344SJeff Roberson 	if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
446881302f1dSMark Johnston 		itemdomain = item_domain(item);
4469dfe13344SJeff Roberson #endif
44705d1ae027SRobert Watson 	critical_enter();
44710a81b439SJeff Roberson 	do {
4472cc7ce83aSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
4473a553d4b8SJeff Roberson 		/*
4474dfe13344SJeff Roberson 		 * Try to free into the allocbucket first to give LIFO
4475dfe13344SJeff Roberson 		 * ordering for cache-hot datastructures.  Spill over
4476dfe13344SJeff Roberson 		 * into the freebucket if necessary.  Alloc will swap
4477dfe13344SJeff Roberson 		 * them if one runs dry.
4478a553d4b8SJeff Roberson 		 */
4479dfe13344SJeff Roberson 		bucket = &cache->uc_allocbucket;
4480d4665eaaSJeff Roberson #ifdef NUMA
4481d4665eaaSJeff Roberson 		if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4482c6fd3e23SJeff Roberson 		    PCPU_GET(domain) != itemdomain) {
4483d4665eaaSJeff Roberson 			bucket = &cache->uc_crossbucket;
4484d4665eaaSJeff Roberson 		} else
4485d4665eaaSJeff Roberson #endif
4486fe835cbfSJeff Roberson 		if (bucket->ucb_cnt == bucket->ucb_entries &&
4487fe835cbfSJeff Roberson 		   cache->uc_freebucket.ucb_cnt <
4488fe835cbfSJeff Roberson 		   cache->uc_freebucket.ucb_entries)
4489fe835cbfSJeff Roberson 			cache_bucket_swap(&cache->uc_freebucket,
4490fe835cbfSJeff Roberson 			    &cache->uc_allocbucket);
4491376b1ba3SJeff Roberson 		if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
4492376b1ba3SJeff Roberson 			cache_bucket_push(cache, bucket, item);
44935d1ae027SRobert Watson 			critical_exit();
44948355f576SJeff Roberson 			return;
4495fc03d22bSJeff Roberson 		}
44960a81b439SJeff Roberson 	} while (cache_free(zone, cache, udata, item, itemdomain));
44970a81b439SJeff Roberson 	critical_exit();
4498fc03d22bSJeff Roberson 
44998355f576SJeff Roberson 	/*
45000a81b439SJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
45018355f576SJeff Roberson 	 */
45020a81b439SJeff Roberson zfree_item:
45030a81b439SJeff Roberson 	zone_free_item(zone, item, udata, SKIP_DTOR);
45040a81b439SJeff Roberson }
4505fc03d22bSJeff Roberson 
4506dfe13344SJeff Roberson #ifdef NUMA
450791d947bfSJeff Roberson /*
450891d947bfSJeff Roberson  * sort crossdomain free buckets to domain correct buckets and cache
450991d947bfSJeff Roberson  * them.
451091d947bfSJeff Roberson  */
451191d947bfSJeff Roberson static void
451291d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata)
451391d947bfSJeff Roberson {
4514991f23efSMark Johnston 	struct uma_bucketlist emptybuckets, fullbuckets;
451591d947bfSJeff Roberson 	uma_zone_domain_t zdom;
451691d947bfSJeff Roberson 	uma_bucket_t b;
4517543117beSJeff Roberson 	smr_seq_t seq;
451891d947bfSJeff Roberson 	void *item;
451991d947bfSJeff Roberson 	int domain;
452091d947bfSJeff Roberson 
452191d947bfSJeff Roberson 	CTR3(KTR_UMA,
452291d947bfSJeff Roberson 	    "uma_zfree: zone %s(%p) draining cross bucket %p",
452391d947bfSJeff Roberson 	    zone->uz_name, zone, bucket);
452491d947bfSJeff Roberson 
4525543117beSJeff Roberson 	/*
4526543117beSJeff Roberson 	 * It is possible for buckets to arrive here out of order so we fetch
4527543117beSJeff Roberson 	 * the current smr seq rather than accepting the bucket's.
4528543117beSJeff Roberson 	 */
4529543117beSJeff Roberson 	seq = SMR_SEQ_INVALID;
4530543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
4531226dd6dbSJeff Roberson 		seq = smr_advance(zone->uz_smr);
4532226dd6dbSJeff Roberson 
4533226dd6dbSJeff Roberson 	/*
4534226dd6dbSJeff Roberson 	 * To avoid having ndomain * ndomain buckets for sorting we have a
4535226dd6dbSJeff Roberson 	 * lock on the current crossfree bucket.  A full matrix with
4536226dd6dbSJeff Roberson 	 * per-domain locking could be used if necessary.
4537226dd6dbSJeff Roberson 	 */
4538991f23efSMark Johnston 	STAILQ_INIT(&emptybuckets);
4539226dd6dbSJeff Roberson 	STAILQ_INIT(&fullbuckets);
4540226dd6dbSJeff Roberson 	ZONE_CROSS_LOCK(zone);
4541991f23efSMark Johnston 	for (; bucket->ub_cnt > 0; bucket->ub_cnt--) {
454291d947bfSJeff Roberson 		item = bucket->ub_bucket[bucket->ub_cnt - 1];
454381302f1dSMark Johnston 		domain = item_domain(item);
4544c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(zone, domain);
454591d947bfSJeff Roberson 		if (zdom->uzd_cross == NULL) {
4546991f23efSMark Johnston 			if ((b = STAILQ_FIRST(&emptybuckets)) != NULL) {
4547991f23efSMark Johnston 				STAILQ_REMOVE_HEAD(&emptybuckets, ub_link);
4548991f23efSMark Johnston 				zdom->uzd_cross = b;
4549991f23efSMark Johnston 			} else {
4550991f23efSMark Johnston 				/*
4551991f23efSMark Johnston 				 * Avoid allocating a bucket with the cross lock
4552991f23efSMark Johnston 				 * held, since allocation can trigger a
4553991f23efSMark Johnston 				 * cross-domain free and bucket zones may
4554991f23efSMark Johnston 				 * allocate from each other.
4555991f23efSMark Johnston 				 */
4556991f23efSMark Johnston 				ZONE_CROSS_UNLOCK(zone);
4557991f23efSMark Johnston 				b = bucket_alloc(zone, udata, M_NOWAIT);
4558991f23efSMark Johnston 				if (b == NULL)
4559991f23efSMark Johnston 					goto out;
4560991f23efSMark Johnston 				ZONE_CROSS_LOCK(zone);
4561991f23efSMark Johnston 				if (zdom->uzd_cross != NULL) {
4562991f23efSMark Johnston 					STAILQ_INSERT_HEAD(&emptybuckets, b,
4563991f23efSMark Johnston 					    ub_link);
4564991f23efSMark Johnston 				} else {
4565991f23efSMark Johnston 					zdom->uzd_cross = b;
4566991f23efSMark Johnston 				}
4567991f23efSMark Johnston 			}
456891d947bfSJeff Roberson 		}
4569543117beSJeff Roberson 		b = zdom->uzd_cross;
4570543117beSJeff Roberson 		b->ub_bucket[b->ub_cnt++] = item;
4571543117beSJeff Roberson 		b->ub_seq = seq;
4572543117beSJeff Roberson 		if (b->ub_cnt == b->ub_entries) {
4573543117beSJeff Roberson 			STAILQ_INSERT_HEAD(&fullbuckets, b, ub_link);
4574991f23efSMark Johnston 			if ((b = STAILQ_FIRST(&emptybuckets)) != NULL)
4575991f23efSMark Johnston 				STAILQ_REMOVE_HEAD(&emptybuckets, ub_link);
4576991f23efSMark Johnston 			zdom->uzd_cross = b;
457791d947bfSJeff Roberson 		}
457891d947bfSJeff Roberson 	}
457991d947bfSJeff Roberson 	ZONE_CROSS_UNLOCK(zone);
4580991f23efSMark Johnston out:
4581c6fd3e23SJeff Roberson 	if (bucket->ub_cnt == 0)
4582d4665eaaSJeff Roberson 		bucket->ub_seq = SMR_SEQ_INVALID;
458391d947bfSJeff Roberson 	bucket_free(zone, bucket, udata);
4584c6fd3e23SJeff Roberson 
4585991f23efSMark Johnston 	while ((b = STAILQ_FIRST(&emptybuckets)) != NULL) {
4586991f23efSMark Johnston 		STAILQ_REMOVE_HEAD(&emptybuckets, ub_link);
4587991f23efSMark Johnston 		bucket_free(zone, b, udata);
4588991f23efSMark Johnston 	}
4589c6fd3e23SJeff Roberson 	while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) {
4590c6fd3e23SJeff Roberson 		STAILQ_REMOVE_HEAD(&fullbuckets, ub_link);
459181302f1dSMark Johnston 		domain = item_domain(b->ub_bucket[0]);
4592c6fd3e23SJeff Roberson 		zone_put_bucket(zone, domain, b, udata, true);
4593c6fd3e23SJeff Roberson 	}
459491d947bfSJeff Roberson }
459591d947bfSJeff Roberson #endif
459691d947bfSJeff Roberson 
45970a81b439SJeff Roberson static void
45980a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata,
4599c6fd3e23SJeff Roberson     int itemdomain, bool ws)
46000a81b439SJeff Roberson {
46010a81b439SJeff Roberson 
4602dfe13344SJeff Roberson #ifdef NUMA
46030a81b439SJeff Roberson 	/*
46040a81b439SJeff Roberson 	 * Buckets coming from the wrong domain will be entirely for the
46050a81b439SJeff Roberson 	 * only other domain on two domain systems.  In this case we can
46060a81b439SJeff Roberson 	 * simply cache them.  Otherwise we need to sort them back to
460791d947bfSJeff Roberson 	 * correct domains.
46080a81b439SJeff Roberson 	 */
4609c6fd3e23SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4610c6fd3e23SJeff Roberson 	    vm_ndomains > 2 && PCPU_GET(domain) != itemdomain) {
461191d947bfSJeff Roberson 		zone_free_cross(zone, bucket, udata);
46120a81b439SJeff Roberson 		return;
46130a81b439SJeff Roberson 	}
46140a81b439SJeff Roberson #endif
461591d947bfSJeff Roberson 
46160a81b439SJeff Roberson 	/*
46170a81b439SJeff Roberson 	 * Attempt to save the bucket in the zone's domain bucket cache.
46180a81b439SJeff Roberson 	 */
46190a81b439SJeff Roberson 	CTR3(KTR_UMA,
46200a81b439SJeff Roberson 	    "uma_zfree: zone %s(%p) putting bucket %p on free list",
46210a81b439SJeff Roberson 	    zone->uz_name, zone, bucket);
46220a81b439SJeff Roberson 	/* ub_cnt is pointing to the last free item */
4623c6fd3e23SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0)
4624c6fd3e23SJeff Roberson 		itemdomain = zone_domain_lowest(zone, itemdomain);
4625c6fd3e23SJeff Roberson 	zone_put_bucket(zone, itemdomain, bucket, udata, ws);
46268355f576SJeff Roberson }
4627fc03d22bSJeff Roberson 
46284d104ba0SAlexander Motin /*
46290a81b439SJeff Roberson  * Populate a free or cross bucket for the current cpu cache.  Free any
46300a81b439SJeff Roberson  * existing full bucket either to the zone cache or back to the slab layer.
46310a81b439SJeff Roberson  *
46320a81b439SJeff Roberson  * Enters and returns in a critical section.  false return indicates that
46330a81b439SJeff Roberson  * we can not satisfy this free in the cache layer.  true indicates that
46340a81b439SJeff Roberson  * the caller should retry.
46354d104ba0SAlexander Motin  */
46360a81b439SJeff Roberson static __noinline bool
46370a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item,
46380a81b439SJeff Roberson     int itemdomain)
46390a81b439SJeff Roberson {
4640dfe13344SJeff Roberson 	uma_cache_bucket_t cbucket;
4641d4665eaaSJeff Roberson 	uma_bucket_t newbucket, bucket;
46420a81b439SJeff Roberson 
46430a81b439SJeff Roberson 	CRITICAL_ASSERT(curthread);
46440a81b439SJeff Roberson 
4645d4665eaaSJeff Roberson 	if (zone->uz_bucket_size == 0)
46460a81b439SJeff Roberson 		return false;
46470a81b439SJeff Roberson 
4648cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4649d4665eaaSJeff Roberson 	newbucket = NULL;
46500a81b439SJeff Roberson 
46510a81b439SJeff Roberson 	/*
4652dfe13344SJeff Roberson 	 * FIRSTTOUCH domains need to free to the correct zdom.  When
4653dfe13344SJeff Roberson 	 * enabled this is the zdom of the item.   The bucket is the
4654dfe13344SJeff Roberson 	 * cross bucket if the current domain and itemdomain do not match.
46550a81b439SJeff Roberson 	 */
4656dfe13344SJeff Roberson 	cbucket = &cache->uc_freebucket;
4657dfe13344SJeff Roberson #ifdef NUMA
4658c6fd3e23SJeff Roberson 	if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) {
4659c6fd3e23SJeff Roberson 		if (PCPU_GET(domain) != itemdomain) {
4660dfe13344SJeff Roberson 			cbucket = &cache->uc_crossbucket;
4661dfe13344SJeff Roberson 			if (cbucket->ucb_cnt != 0)
4662c6fd3e23SJeff Roberson 				counter_u64_add(zone->uz_xdomain,
4663dfe13344SJeff Roberson 				    cbucket->ucb_cnt);
4664dfe13344SJeff Roberson 		}
4665c6fd3e23SJeff Roberson 	}
46660a81b439SJeff Roberson #endif
4667dfe13344SJeff Roberson 	bucket = cache_bucket_unload(cbucket);
4668c6fd3e23SJeff Roberson 	KASSERT(bucket == NULL || bucket->ub_cnt == bucket->ub_entries,
4669c6fd3e23SJeff Roberson 	    ("cache_free: Entered with non-full free bucket."));
46700a81b439SJeff Roberson 
46710a81b439SJeff Roberson 	/* We are no longer associated with this CPU. */
46720a81b439SJeff Roberson 	critical_exit();
46730a81b439SJeff Roberson 
4674d4665eaaSJeff Roberson 	/*
4675d4665eaaSJeff Roberson 	 * Don't let SMR zones operate without a free bucket.  Force
4676d4665eaaSJeff Roberson 	 * a synchronize and re-use this one.  We will only degrade
4677d4665eaaSJeff Roberson 	 * to a synchronize every bucket_size items rather than every
4678d4665eaaSJeff Roberson 	 * item if we fail to allocate a bucket.
4679d4665eaaSJeff Roberson 	 */
4680d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0) {
4681d4665eaaSJeff Roberson 		if (bucket != NULL)
4682d4665eaaSJeff Roberson 			bucket->ub_seq = smr_advance(zone->uz_smr);
4683d4665eaaSJeff Roberson 		newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4684d4665eaaSJeff Roberson 		if (newbucket == NULL && bucket != NULL) {
4685d4665eaaSJeff Roberson 			bucket_drain(zone, bucket);
4686d4665eaaSJeff Roberson 			newbucket = bucket;
4687d4665eaaSJeff Roberson 			bucket = NULL;
4688d4665eaaSJeff Roberson 		}
4689d4665eaaSJeff Roberson 	} else if (!bucketdisable)
4690d4665eaaSJeff Roberson 		newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4691d4665eaaSJeff Roberson 
46920a81b439SJeff Roberson 	if (bucket != NULL)
4693c6fd3e23SJeff Roberson 		zone_free_bucket(zone, bucket, udata, itemdomain, true);
4694a553d4b8SJeff Roberson 
4695fc03d22bSJeff Roberson 	critical_enter();
4696d4665eaaSJeff Roberson 	if ((bucket = newbucket) == NULL)
46970a81b439SJeff Roberson 		return (false);
4698cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4699dfe13344SJeff Roberson #ifdef NUMA
4700fc03d22bSJeff Roberson 	/*
47010a81b439SJeff Roberson 	 * Check to see if we should be populating the cross bucket.  If it
47020a81b439SJeff Roberson 	 * is already populated we will fall through and attempt to populate
47030a81b439SJeff Roberson 	 * the free bucket.
4704fc03d22bSJeff Roberson 	 */
4705c6fd3e23SJeff Roberson 	if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) {
4706c6fd3e23SJeff Roberson 		if (PCPU_GET(domain) != itemdomain &&
4707376b1ba3SJeff Roberson 		    cache->uc_crossbucket.ucb_bucket == NULL) {
4708376b1ba3SJeff Roberson 			cache_bucket_load_cross(cache, bucket);
47090a81b439SJeff Roberson 			return (true);
47100a81b439SJeff Roberson 		}
47110a81b439SJeff Roberson 	}
47120a81b439SJeff Roberson #endif
47130a81b439SJeff Roberson 	/*
47140a81b439SJeff Roberson 	 * We may have lost the race to fill the bucket or switched CPUs.
47150a81b439SJeff Roberson 	 */
4716376b1ba3SJeff Roberson 	if (cache->uc_freebucket.ucb_bucket != NULL) {
4717fc03d22bSJeff Roberson 		critical_exit();
47186fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
47190a81b439SJeff Roberson 		critical_enter();
47200a81b439SJeff Roberson 	} else
4721376b1ba3SJeff Roberson 		cache_bucket_load_free(cache, bucket);
47228355f576SJeff Roberson 
47230a81b439SJeff Roberson 	return (true);
47248355f576SJeff Roberson }
47258355f576SJeff Roberson 
47268355f576SJeff Roberson static void
4727bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item)
47288355f576SJeff Roberson {
4729bb15d1c7SGleb Smirnoff 	uma_keg_t keg;
4730ab3185d1SJeff Roberson 	uma_domain_t dom;
47319b8db4d0SRyan Libby 	int freei;
4732099a0e58SBosko Milekic 
4733bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
47348b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, slab->us_domain);
4735ab3185d1SJeff Roberson 
47368355f576SJeff Roberson 	/* Do we need to remove from any lists? */
47378b987a77SJeff Roberson 	dom = &keg->uk_domain[slab->us_domain];
4738099a0e58SBosko Milekic 	if (slab->us_freecount + 1 == keg->uk_ipers) {
47398355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
4740ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
47414ab3aee8SMark Johnston 		dom->ud_free_slabs++;
47428355f576SJeff Roberson 	} else if (slab->us_freecount == 0) {
47438355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
4744ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
47458355f576SJeff Roberson 	}
47468355f576SJeff Roberson 
4747ef72505eSJeff Roberson 	/* Slab management. */
47481e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
47499b78b1f4SJeff Roberson 	BIT_SET(keg->uk_ipers, freei, &slab->us_free);
47508355f576SJeff Roberson 	slab->us_freecount++;
47518355f576SJeff Roberson 
4752ef72505eSJeff Roberson 	/* Keg statistics. */
47534ab3aee8SMark Johnston 	dom->ud_free_items++;
47540095a784SJeff Roberson }
47550095a784SJeff Roberson 
47560095a784SJeff Roberson static void
4757b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt)
47580095a784SJeff Roberson {
47598b987a77SJeff Roberson 	struct mtx *lock;
4760b75c4efcSAndrew Turner 	uma_zone_t zone;
47610095a784SJeff Roberson 	uma_slab_t slab;
47620095a784SJeff Roberson 	uma_keg_t keg;
47630095a784SJeff Roberson 	uint8_t *mem;
47648b987a77SJeff Roberson 	void *item;
47650095a784SJeff Roberson 	int i;
47668355f576SJeff Roberson 
4767b75c4efcSAndrew Turner 	zone = arg;
4768bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
47698b987a77SJeff Roberson 	lock = NULL;
477054c5ae80SRyan Libby 	if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0))
47718b987a77SJeff Roberson 		lock = KEG_LOCK(keg, 0);
47720095a784SJeff Roberson 	for (i = 0; i < cnt; i++) {
47730095a784SJeff Roberson 		item = bucket[i];
477454c5ae80SRyan Libby 		if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) {
47750095a784SJeff Roberson 			slab = vtoslab((vm_offset_t)item);
47768b987a77SJeff Roberson 		} else {
47778b987a77SJeff Roberson 			mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
477854c5ae80SRyan Libby 			if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0)
47798b987a77SJeff Roberson 				slab = hash_sfind(&keg->uk_hash, mem);
47808b987a77SJeff Roberson 			else
47818b987a77SJeff Roberson 				slab = (uma_slab_t)(mem + keg->uk_pgoff);
47828b987a77SJeff Roberson 		}
47838b987a77SJeff Roberson 		if (lock != KEG_LOCKPTR(keg, slab->us_domain)) {
47848b987a77SJeff Roberson 			if (lock != NULL)
47858b987a77SJeff Roberson 				mtx_unlock(lock);
47868b987a77SJeff Roberson 			lock = KEG_LOCK(keg, slab->us_domain);
47878b987a77SJeff Roberson 		}
4788bb15d1c7SGleb Smirnoff 		slab_free_item(zone, slab, item);
47890095a784SJeff Roberson 	}
47908b987a77SJeff Roberson 	if (lock != NULL)
47918b987a77SJeff Roberson 		mtx_unlock(lock);
47928355f576SJeff Roberson }
47938355f576SJeff Roberson 
47940095a784SJeff Roberson /*
47950095a784SJeff Roberson  * Frees a single item to any zone.
47960095a784SJeff Roberson  *
47970095a784SJeff Roberson  * Arguments:
47980095a784SJeff Roberson  *	zone   The zone to free to
47990095a784SJeff Roberson  *	item   The item we're freeing
48000095a784SJeff Roberson  *	udata  User supplied data for the dtor
48010095a784SJeff Roberson  *	skip   Skip dtors and finis
48020095a784SJeff Roberson  */
48036d88d784SJeff Roberson static __noinline void
48040095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
48050095a784SJeff Roberson {
4806c5deaf04SGleb Smirnoff 
4807d4665eaaSJeff Roberson 	/*
4808d4665eaaSJeff Roberson 	 * If a free is sent directly to an SMR zone we have to
4809d4665eaaSJeff Roberson 	 * synchronize immediately because the item can instantly
4810d4665eaaSJeff Roberson 	 * be reallocated. This should only happen in degenerate
4811d4665eaaSJeff Roberson 	 * cases when no memory is available for per-cpu caches.
4812d4665eaaSJeff Roberson 	 */
4813d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE)
4814d4665eaaSJeff Roberson 		smr_synchronize(zone->uz_smr);
4815d4665eaaSJeff Roberson 
4816cc7ce83aSJeff Roberson 	item_dtor(zone, item, zone->uz_size, udata, skip);
48170095a784SJeff Roberson 
481809c8cb71SMark Johnston 	if (skip < SKIP_FINI && zone->uz_fini) {
481909c8cb71SMark Johnston 		kasan_mark_item_valid(zone, item);
48200095a784SJeff Roberson 		zone->uz_fini(item, zone->uz_size);
482109c8cb71SMark Johnston 		kasan_mark_item_invalid(zone, item);
482209c8cb71SMark Johnston 	}
48230095a784SJeff Roberson 
48240095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, &item, 1);
4825bb15d1c7SGleb Smirnoff 
4826bb15d1c7SGleb Smirnoff 	if (skip & SKIP_CNT)
4827bb15d1c7SGleb Smirnoff 		return;
4828bb15d1c7SGleb Smirnoff 
48292efcc8cbSGleb Smirnoff 	counter_u64_add(zone->uz_frees, 1);
48302efcc8cbSGleb Smirnoff 
48314bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
48324bd61e19SJeff Roberson 		zone_free_limit(zone, 1);
4833bb45b411SGleb Smirnoff }
48340095a784SJeff Roberson 
48358355f576SJeff Roberson /* See uma.h */
48361c6cae97SLawrence Stewart int
4837736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems)
4838736ee590SJeff Roberson {
4839e574d407SMark Johnston 
4840e574d407SMark Johnston 	/*
4841e574d407SMark Johnston 	 * If the limit is small, we may need to constrain the maximum per-CPU
4842e574d407SMark Johnston 	 * cache size, or disable caching entirely.
4843e574d407SMark Johnston 	 */
4844e574d407SMark Johnston 	uma_zone_set_maxcache(zone, nitems);
4845bb15d1c7SGleb Smirnoff 
48464bd61e19SJeff Roberson 	/*
48474bd61e19SJeff Roberson 	 * XXX This can misbehave if the zone has any allocations with
48484bd61e19SJeff Roberson 	 * no limit and a limit is imposed.  There is currently no
48494bd61e19SJeff Roberson 	 * way to clear a limit.
48504bd61e19SJeff Roberson 	 */
4851bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4852bb15d1c7SGleb Smirnoff 	zone->uz_max_items = nitems;
4853cc7ce83aSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_LIMIT;
4854cc7ce83aSJeff Roberson 	zone_update_caches(zone);
48554bd61e19SJeff Roberson 	/* We may need to wake waiters. */
48564bd61e19SJeff Roberson 	wakeup(&zone->uz_max_items);
4857bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4858bb15d1c7SGleb Smirnoff 
4859bb15d1c7SGleb Smirnoff 	return (nitems);
4860bb15d1c7SGleb Smirnoff }
4861bb15d1c7SGleb Smirnoff 
4862bb15d1c7SGleb Smirnoff /* See uma.h */
4863003cf08bSMark Johnston void
4864bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems)
4865bb15d1c7SGleb Smirnoff {
4866e574d407SMark Johnston 	int bpcpu, bpdom, bsize, nb;
4867bb15d1c7SGleb Smirnoff 
4868bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4869e574d407SMark Johnston 
4870e574d407SMark Johnston 	/*
4871e574d407SMark Johnston 	 * Compute a lower bound on the number of items that may be cached in
4872e574d407SMark Johnston 	 * the zone.  Each CPU gets at least two buckets, and for cross-domain
4873e574d407SMark Johnston 	 * frees we use an additional bucket per CPU and per domain.  Select the
4874e574d407SMark Johnston 	 * largest bucket size that does not exceed half of the requested limit,
4875e574d407SMark Johnston 	 * with the left over space given to the full bucket cache.
4876e574d407SMark Johnston 	 */
4877e574d407SMark Johnston 	bpdom = 0;
4878003cf08bSMark Johnston 	bpcpu = 2;
4879e574d407SMark Johnston #ifdef NUMA
4880e574d407SMark Johnston 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && vm_ndomains > 1) {
4881003cf08bSMark Johnston 		bpcpu++;
4882e574d407SMark Johnston 		bpdom++;
4883003cf08bSMark Johnston 	}
4884e574d407SMark Johnston #endif
4885e574d407SMark Johnston 	nb = bpcpu * mp_ncpus + bpdom * vm_ndomains;
4886e574d407SMark Johnston 	bsize = nitems / nb / 2;
4887e574d407SMark Johnston 	if (bsize > BUCKET_MAX)
4888e574d407SMark Johnston 		bsize = BUCKET_MAX;
4889e574d407SMark Johnston 	else if (bsize == 0 && nitems / nb > 0)
4890e574d407SMark Johnston 		bsize = 1;
4891e574d407SMark Johnston 	zone->uz_bucket_size_max = zone->uz_bucket_size = bsize;
489220a4e154SJeff Roberson 	if (zone->uz_bucket_size_min > zone->uz_bucket_size_max)
489320a4e154SJeff Roberson 		zone->uz_bucket_size_min = zone->uz_bucket_size_max;
4894e574d407SMark Johnston 	zone->uz_bucket_max = nitems - nb * bsize;
4895bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4896736ee590SJeff Roberson }
4897736ee590SJeff Roberson 
4898736ee590SJeff Roberson /* See uma.h */
4899e49471b0SAndre Oppermann int
4900e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone)
4901e49471b0SAndre Oppermann {
4902e49471b0SAndre Oppermann 	int nitems;
4903e49471b0SAndre Oppermann 
4904727c6918SJeff Roberson 	nitems = atomic_load_64(&zone->uz_max_items);
4905e49471b0SAndre Oppermann 
4906e49471b0SAndre Oppermann 	return (nitems);
4907e49471b0SAndre Oppermann }
4908e49471b0SAndre Oppermann 
4909e49471b0SAndre Oppermann /* See uma.h */
49102f891cd5SPawel Jakub Dawidek void
49112f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning)
49122f891cd5SPawel Jakub Dawidek {
49132f891cd5SPawel Jakub Dawidek 
4914727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
49152f891cd5SPawel Jakub Dawidek 	zone->uz_warning = warning;
49162f891cd5SPawel Jakub Dawidek }
49172f891cd5SPawel Jakub Dawidek 
49182f891cd5SPawel Jakub Dawidek /* See uma.h */
491954503a13SJonathan T. Looney void
492054503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
492154503a13SJonathan T. Looney {
492254503a13SJonathan T. Looney 
4923727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
4924e60b2fcbSGleb Smirnoff 	TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
492554503a13SJonathan T. Looney }
492654503a13SJonathan T. Looney 
492754503a13SJonathan T. Looney /* See uma.h */
4928c4ae7908SLawrence Stewart int
4929c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone)
4930c4ae7908SLawrence Stewart {
4931c4ae7908SLawrence Stewart 	int64_t nitems;
4932c4ae7908SLawrence Stewart 	u_int i;
4933c4ae7908SLawrence Stewart 
4934bfb6b7a1SJeff Roberson 	nitems = 0;
4935bfb6b7a1SJeff Roberson 	if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER)
49362efcc8cbSGleb Smirnoff 		nitems = counter_u64_fetch(zone->uz_allocs) -
49372efcc8cbSGleb Smirnoff 		    counter_u64_fetch(zone->uz_frees);
4938727c6918SJeff Roberson 	CPU_FOREACH(i)
4939727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) -
4940727c6918SJeff Roberson 		    atomic_load_64(&zone->uz_cpu[i].uc_frees);
4941c4ae7908SLawrence Stewart 
4942c4ae7908SLawrence Stewart 	return (nitems < 0 ? 0 : nitems);
4943c4ae7908SLawrence Stewart }
4944c4ae7908SLawrence Stewart 
494520a4e154SJeff Roberson static uint64_t
494620a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone)
494720a4e154SJeff Roberson {
494820a4e154SJeff Roberson 	uint64_t nitems;
494920a4e154SJeff Roberson 	u_int i;
495020a4e154SJeff Roberson 
4951bfb6b7a1SJeff Roberson 	nitems = 0;
4952bfb6b7a1SJeff Roberson 	if (zone->uz_allocs != EARLY_COUNTER)
495320a4e154SJeff Roberson 		nitems = counter_u64_fetch(zone->uz_allocs);
4954727c6918SJeff Roberson 	CPU_FOREACH(i)
4955727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs);
495620a4e154SJeff Roberson 
495720a4e154SJeff Roberson 	return (nitems);
495820a4e154SJeff Roberson }
495920a4e154SJeff Roberson 
496020a4e154SJeff Roberson static uint64_t
496120a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone)
496220a4e154SJeff Roberson {
496320a4e154SJeff Roberson 	uint64_t nitems;
496420a4e154SJeff Roberson 	u_int i;
496520a4e154SJeff Roberson 
4966bfb6b7a1SJeff Roberson 	nitems = 0;
4967bfb6b7a1SJeff Roberson 	if (zone->uz_frees != EARLY_COUNTER)
496820a4e154SJeff Roberson 		nitems = counter_u64_fetch(zone->uz_frees);
4969727c6918SJeff Roberson 	CPU_FOREACH(i)
4970727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees);
497120a4e154SJeff Roberson 
497220a4e154SJeff Roberson 	return (nitems);
497320a4e154SJeff Roberson }
497420a4e154SJeff Roberson 
497531c251a0SJeff Roberson #ifdef INVARIANTS
497631c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */
497731c251a0SJeff Roberson static uint64_t
497831c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg)
497931c251a0SJeff Roberson {
498031c251a0SJeff Roberson 	uma_zone_t z;
498131c251a0SJeff Roberson 	uint64_t nitems;
498231c251a0SJeff Roberson 
498331c251a0SJeff Roberson 	nitems = 0;
498431c251a0SJeff Roberson 	LIST_FOREACH(z, &keg->uk_zones, uz_link)
498531c251a0SJeff Roberson 		nitems += uma_zone_get_allocs(z);
498631c251a0SJeff Roberson 
498731c251a0SJeff Roberson 	return (nitems);
498831c251a0SJeff Roberson }
498931c251a0SJeff Roberson #endif
499031c251a0SJeff Roberson 
4991c4ae7908SLawrence Stewart /* See uma.h */
4992736ee590SJeff Roberson void
4993099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit)
4994099a0e58SBosko Milekic {
4995e20a199fSJeff Roberson 	uma_keg_t keg;
4996e20a199fSJeff Roberson 
4997bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
4998727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
4999e20a199fSJeff Roberson 	keg->uk_init = uminit;
5000099a0e58SBosko Milekic }
5001099a0e58SBosko Milekic 
5002099a0e58SBosko Milekic /* See uma.h */
5003099a0e58SBosko Milekic void
5004099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
5005099a0e58SBosko Milekic {
5006e20a199fSJeff Roberson 	uma_keg_t keg;
5007e20a199fSJeff Roberson 
5008bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5009727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
5010e20a199fSJeff Roberson 	keg->uk_fini = fini;
5011099a0e58SBosko Milekic }
5012099a0e58SBosko Milekic 
5013099a0e58SBosko Milekic /* See uma.h */
5014099a0e58SBosko Milekic void
5015099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
5016099a0e58SBosko Milekic {
5017af526374SJeff Roberson 
5018727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
5019099a0e58SBosko Milekic 	zone->uz_init = zinit;
5020099a0e58SBosko Milekic }
5021099a0e58SBosko Milekic 
5022099a0e58SBosko Milekic /* See uma.h */
5023099a0e58SBosko Milekic void
5024099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
5025099a0e58SBosko Milekic {
5026af526374SJeff Roberson 
5027727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
5028099a0e58SBosko Milekic 	zone->uz_fini = zfini;
5029099a0e58SBosko Milekic }
5030099a0e58SBosko Milekic 
5031099a0e58SBosko Milekic /* See uma.h */
5032099a0e58SBosko Milekic void
50338355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef)
50348355f576SJeff Roberson {
50350095a784SJeff Roberson 	uma_keg_t keg;
5036e20a199fSJeff Roberson 
5037bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5038727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
50390095a784SJeff Roberson 	keg->uk_freef = freef;
50408355f576SJeff Roberson }
50418355f576SJeff Roberson 
50428355f576SJeff Roberson /* See uma.h */
50438355f576SJeff Roberson void
50448355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
50458355f576SJeff Roberson {
5046e20a199fSJeff Roberson 	uma_keg_t keg;
5047e20a199fSJeff Roberson 
5048bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5049727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
5050e20a199fSJeff Roberson 	keg->uk_allocf = allocf;
50518355f576SJeff Roberson }
50528355f576SJeff Roberson 
50538355f576SJeff Roberson /* See uma.h */
50546fd34d6fSJeff Roberson void
5055d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr)
5056d4665eaaSJeff Roberson {
5057d4665eaaSJeff Roberson 
5058d4665eaaSJeff Roberson 	ZONE_ASSERT_COLD(zone);
5059d4665eaaSJeff Roberson 
50607f746c9fSMateusz Guzik 	KASSERT(smr != NULL, ("Got NULL smr"));
50617f746c9fSMateusz Guzik 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
50627f746c9fSMateusz Guzik 	    ("zone %p (%s) already uses SMR", zone, zone->uz_name));
5063d4665eaaSJeff Roberson 	zone->uz_flags |= UMA_ZONE_SMR;
5064d4665eaaSJeff Roberson 	zone->uz_smr = smr;
5065d4665eaaSJeff Roberson 	zone_update_caches(zone);
5066d4665eaaSJeff Roberson }
5067d4665eaaSJeff Roberson 
5068d4665eaaSJeff Roberson smr_t
5069d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone)
5070d4665eaaSJeff Roberson {
5071d4665eaaSJeff Roberson 
5072d4665eaaSJeff Roberson 	return (zone->uz_smr);
5073d4665eaaSJeff Roberson }
5074d4665eaaSJeff Roberson 
5075d4665eaaSJeff Roberson /* See uma.h */
5076d4665eaaSJeff Roberson void
50776fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items)
50786fd34d6fSJeff Roberson {
50796fd34d6fSJeff Roberson 	uma_keg_t keg;
50806fd34d6fSJeff Roberson 
5081bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5082727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
50836fd34d6fSJeff Roberson 	keg->uk_reserve = items;
50846fd34d6fSJeff Roberson }
50856fd34d6fSJeff Roberson 
50866fd34d6fSJeff Roberson /* See uma.h */
50878355f576SJeff Roberson int
5088a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count)
50898355f576SJeff Roberson {
5090099a0e58SBosko Milekic 	uma_keg_t keg;
50918355f576SJeff Roberson 	vm_offset_t kva;
50929ba30bcbSZbigniew Bodek 	u_int pages;
50938355f576SJeff Roberson 
5094bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5095727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
5096727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
50978355f576SJeff Roberson 
509879c9f942SJeff Roberson 	pages = howmany(count, keg->uk_ipers) * keg->uk_ppera;
5099a553d4b8SJeff Roberson 
5100a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
5101a4915c21SAttilio Rao 	if (keg->uk_ppera > 1) {
5102a4915c21SAttilio Rao #else
5103a4915c21SAttilio Rao 	if (1) {
5104a4915c21SAttilio Rao #endif
510557223e99SAndriy Gapon 		kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
5106d1f42ac2SAlan Cox 		if (kva == 0)
51078355f576SJeff Roberson 			return (0);
5108a4915c21SAttilio Rao 	} else
5109a4915c21SAttilio Rao 		kva = 0;
5110bb15d1c7SGleb Smirnoff 
5111bb15d1c7SGleb Smirnoff 	MPASS(keg->uk_kva == 0);
5112099a0e58SBosko Milekic 	keg->uk_kva = kva;
5113a4915c21SAttilio Rao 	keg->uk_offset = 0;
5114bb15d1c7SGleb Smirnoff 	zone->uz_max_items = pages * keg->uk_ipers;
5115a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
5116a4915c21SAttilio Rao 	keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
5117a4915c21SAttilio Rao #else
5118a4915c21SAttilio Rao 	keg->uk_allocf = noobj_alloc;
5119a4915c21SAttilio Rao #endif
5120cc7ce83aSJeff Roberson 	keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
5121cc7ce83aSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
5122cc7ce83aSJeff Roberson 	zone_update_caches(zone);
5123af526374SJeff Roberson 
51248355f576SJeff Roberson 	return (1);
51258355f576SJeff Roberson }
51268355f576SJeff Roberson 
51278355f576SJeff Roberson /* See uma.h */
51288355f576SJeff Roberson void
51298355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items)
51308355f576SJeff Roberson {
5131920239efSMark Johnston 	struct vm_domainset_iter di;
5132ab3185d1SJeff Roberson 	uma_domain_t dom;
51338355f576SJeff Roberson 	uma_slab_t slab;
5134099a0e58SBosko Milekic 	uma_keg_t keg;
513586220393SMark Johnston 	int aflags, domain, slabs;
51368355f576SJeff Roberson 
5137bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
513879c9f942SJeff Roberson 	slabs = howmany(items, keg->uk_ipers);
5139194a979eSMark Johnston 	while (slabs-- > 0) {
514086220393SMark Johnston 		aflags = M_NOWAIT;
514186220393SMark Johnston 		vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
514286220393SMark Johnston 		    &aflags);
514386220393SMark Johnston 		for (;;) {
514486220393SMark Johnston 			slab = keg_alloc_slab(keg, zone, domain, M_WAITOK,
514586220393SMark Johnston 			    aflags);
514686220393SMark Johnston 			if (slab != NULL) {
5147ab3185d1SJeff Roberson 				dom = &keg->uk_domain[slab->us_domain];
51484ab3aee8SMark Johnston 				/*
51494ab3aee8SMark Johnston 				 * keg_alloc_slab() always returns a slab on the
51504ab3aee8SMark Johnston 				 * partial list.
51514ab3aee8SMark Johnston 				 */
51528b987a77SJeff Roberson 				LIST_REMOVE(slab, us_link);
515386220393SMark Johnston 				LIST_INSERT_HEAD(&dom->ud_free_slab, slab,
515486220393SMark Johnston 				    us_link);
51554ab3aee8SMark Johnston 				dom->ud_free_slabs++;
51568b987a77SJeff Roberson 				KEG_UNLOCK(keg, slab->us_domain);
5157920239efSMark Johnston 				break;
51588355f576SJeff Roberson 			}
51598b987a77SJeff Roberson 			if (vm_domainset_iter_policy(&di, &domain) != 0)
516089d2fb14SKonstantin Belousov 				vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0);
516186220393SMark Johnston 		}
516286220393SMark Johnston 	}
516386220393SMark Johnston }
51648355f576SJeff Roberson 
5165ed581bf6SJeff Roberson /*
5166ed581bf6SJeff Roberson  * Returns a snapshot of memory consumption in bytes.
5167ed581bf6SJeff Roberson  */
5168ed581bf6SJeff Roberson size_t
5169ed581bf6SJeff Roberson uma_zone_memory(uma_zone_t zone)
5170ed581bf6SJeff Roberson {
5171ed581bf6SJeff Roberson 	size_t sz;
5172ed581bf6SJeff Roberson 	int i;
5173ed581bf6SJeff Roberson 
5174ed581bf6SJeff Roberson 	sz = 0;
5175ed581bf6SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_CACHE) {
5176ed581bf6SJeff Roberson 		for (i = 0; i < vm_ndomains; i++)
5177c6fd3e23SJeff Roberson 			sz += ZDOM_GET(zone, i)->uzd_nitems;
5178ed581bf6SJeff Roberson 		return (sz * zone->uz_size);
5179ed581bf6SJeff Roberson 	}
5180ed581bf6SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
5181ed581bf6SJeff Roberson 		sz += zone->uz_keg->uk_domain[i].ud_pages;
5182ed581bf6SJeff Roberson 
5183ed581bf6SJeff Roberson 	return (sz * PAGE_SIZE);
5184ed581bf6SJeff Roberson }
5185ed581bf6SJeff Roberson 
51868355f576SJeff Roberson /* See uma.h */
518708cfa56eSMark Johnston void
518808cfa56eSMark Johnston uma_reclaim(int req)
51898355f576SJeff Roberson {
5190aabe13f1SMark Johnston 	uma_reclaim_domain(req, UMA_ANYDOMAIN);
5191aabe13f1SMark Johnston }
519244ec2b63SKonstantin Belousov 
5193aabe13f1SMark Johnston void
5194aabe13f1SMark Johnston uma_reclaim_domain(int req, int domain)
5195aabe13f1SMark Johnston {
5196aabe13f1SMark Johnston 	void *arg;
5197aabe13f1SMark Johnston 
519886bbae32SJeff Roberson 	bucket_enable();
519908cfa56eSMark Johnston 
5200aabe13f1SMark Johnston 	arg = (void *)(uintptr_t)domain;
5201aabe13f1SMark Johnston 	sx_slock(&uma_reclaim_lock);
520208cfa56eSMark Johnston 	switch (req) {
520308cfa56eSMark Johnston 	case UMA_RECLAIM_TRIM:
5204aabe13f1SMark Johnston 		zone_foreach(zone_trim, arg);
520508cfa56eSMark Johnston 		break;
520608cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN:
5207aabe13f1SMark Johnston 		zone_foreach(zone_drain, arg);
5208aabe13f1SMark Johnston 		break;
520908cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN_CPU:
5210aabe13f1SMark Johnston 		zone_foreach(zone_drain, arg);
521108cfa56eSMark Johnston 		pcpu_cache_drain_safe(NULL);
5212aabe13f1SMark Johnston 		zone_foreach(zone_drain, arg);
521308cfa56eSMark Johnston 		break;
521408cfa56eSMark Johnston 	default:
521508cfa56eSMark Johnston 		panic("unhandled reclamation request %d", req);
521608cfa56eSMark Johnston 	}
52170f9b7bf3SMark Johnston 
52188355f576SJeff Roberson 	/*
52198355f576SJeff Roberson 	 * Some slabs may have been freed but this zone will be visited early
52208355f576SJeff Roberson 	 * we visit again so that we can free pages that are empty once other
52218355f576SJeff Roberson 	 * zones are drained.  We have to do the same for buckets.
52228355f576SJeff Roberson 	 */
5223aabe13f1SMark Johnston 	zone_drain(slabzones[0], arg);
5224aabe13f1SMark Johnston 	zone_drain(slabzones[1], arg);
5225aabe13f1SMark Johnston 	bucket_zone_drain(domain);
5226aabe13f1SMark Johnston 	sx_sunlock(&uma_reclaim_lock);
52278355f576SJeff Roberson }
52288355f576SJeff Roberson 
52292e47807cSJeff Roberson static volatile int uma_reclaim_needed;
523044ec2b63SKonstantin Belousov 
523144ec2b63SKonstantin Belousov void
523244ec2b63SKonstantin Belousov uma_reclaim_wakeup(void)
523344ec2b63SKonstantin Belousov {
523444ec2b63SKonstantin Belousov 
52352e47807cSJeff Roberson 	if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
52362e47807cSJeff Roberson 		wakeup(uma_reclaim);
523744ec2b63SKonstantin Belousov }
523844ec2b63SKonstantin Belousov 
523944ec2b63SKonstantin Belousov void
524044ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused)
524144ec2b63SKonstantin Belousov {
524244ec2b63SKonstantin Belousov 
524344ec2b63SKonstantin Belousov 	for (;;) {
524408cfa56eSMark Johnston 		sx_xlock(&uma_reclaim_lock);
5245200f8117SKonstantin Belousov 		while (atomic_load_int(&uma_reclaim_needed) == 0)
524608cfa56eSMark Johnston 			sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl",
52472e47807cSJeff Roberson 			    hz);
524808cfa56eSMark Johnston 		sx_xunlock(&uma_reclaim_lock);
52499b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
525008cfa56eSMark Johnston 		uma_reclaim(UMA_RECLAIM_DRAIN_CPU);
5251200f8117SKonstantin Belousov 		atomic_store_int(&uma_reclaim_needed, 0);
52522e47807cSJeff Roberson 		/* Don't fire more than once per-second. */
52532e47807cSJeff Roberson 		pause("umarclslp", hz);
525444ec2b63SKonstantin Belousov 	}
525544ec2b63SKonstantin Belousov }
525644ec2b63SKonstantin Belousov 
5257663b416fSJohn Baldwin /* See uma.h */
525808cfa56eSMark Johnston void
525908cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req)
526008cfa56eSMark Johnston {
5261aabe13f1SMark Johnston 	uma_zone_reclaim_domain(zone, req, UMA_ANYDOMAIN);
5262aabe13f1SMark Johnston }
526308cfa56eSMark Johnston 
5264aabe13f1SMark Johnston void
5265aabe13f1SMark Johnston uma_zone_reclaim_domain(uma_zone_t zone, int req, int domain)
5266aabe13f1SMark Johnston {
5267aabe13f1SMark Johnston 	void *arg;
5268aabe13f1SMark Johnston 
5269aabe13f1SMark Johnston 	arg = (void *)(uintptr_t)domain;
527008cfa56eSMark Johnston 	switch (req) {
527108cfa56eSMark Johnston 	case UMA_RECLAIM_TRIM:
5272aabe13f1SMark Johnston 		zone_trim(zone, arg);
527308cfa56eSMark Johnston 		break;
527408cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN:
5275aabe13f1SMark Johnston 		zone_drain(zone, arg);
527608cfa56eSMark Johnston 		break;
527708cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN_CPU:
527808cfa56eSMark Johnston 		pcpu_cache_drain_safe(zone);
5279aabe13f1SMark Johnston 		zone_drain(zone, arg);
528008cfa56eSMark Johnston 		break;
528108cfa56eSMark Johnston 	default:
528208cfa56eSMark Johnston 		panic("unhandled reclamation request %d", req);
528308cfa56eSMark Johnston 	}
528408cfa56eSMark Johnston }
528508cfa56eSMark Johnston 
528608cfa56eSMark Johnston /* See uma.h */
5287663b416fSJohn Baldwin int
5288663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone)
5289663b416fSJohn Baldwin {
5290663b416fSJohn Baldwin 
5291727c6918SJeff Roberson 	return (atomic_load_32(&zone->uz_sleepers) > 0);
52926c125b8dSMohan Srinivasan }
52936c125b8dSMohan Srinivasan 
52942e47807cSJeff Roberson unsigned long
52952e47807cSJeff Roberson uma_limit(void)
52962e47807cSJeff Roberson {
52972e47807cSJeff Roberson 
52982e47807cSJeff Roberson 	return (uma_kmem_limit);
52992e47807cSJeff Roberson }
53002e47807cSJeff Roberson 
53012e47807cSJeff Roberson void
53022e47807cSJeff Roberson uma_set_limit(unsigned long limit)
53032e47807cSJeff Roberson {
53042e47807cSJeff Roberson 
53052e47807cSJeff Roberson 	uma_kmem_limit = limit;
53062e47807cSJeff Roberson }
53072e47807cSJeff Roberson 
53082e47807cSJeff Roberson unsigned long
53092e47807cSJeff Roberson uma_size(void)
53102e47807cSJeff Roberson {
53112e47807cSJeff Roberson 
5312058f0f74SMark Johnston 	return (atomic_load_long(&uma_kmem_total));
5313ad5b0f5bSJeff Roberson }
5314ad5b0f5bSJeff Roberson 
5315ad5b0f5bSJeff Roberson long
5316ad5b0f5bSJeff Roberson uma_avail(void)
5317ad5b0f5bSJeff Roberson {
5318ad5b0f5bSJeff Roberson 
5319058f0f74SMark Johnston 	return (uma_kmem_limit - uma_size());
53202e47807cSJeff Roberson }
53212e47807cSJeff Roberson 
5322a0d4b0aeSRobert Watson #ifdef DDB
53238355f576SJeff Roberson /*
53247a52a97eSRobert Watson  * Generate statistics across both the zone and its per-cpu cache's.  Return
53257a52a97eSRobert Watson  * desired statistics if the pointer is non-NULL for that statistic.
53267a52a97eSRobert Watson  *
53277a52a97eSRobert Watson  * Note: does not update the zone statistics, as it can't safely clear the
53287a52a97eSRobert Watson  * per-CPU cache statistic.
53297a52a97eSRobert Watson  *
53307a52a97eSRobert Watson  */
53317a52a97eSRobert Watson static void
53320f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp,
5333c1685086SJeff Roberson     uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp)
53347a52a97eSRobert Watson {
53357a52a97eSRobert Watson 	uma_cache_t cache;
5336c1685086SJeff Roberson 	uint64_t allocs, frees, sleeps, xdomain;
53377a52a97eSRobert Watson 	int cachefree, cpu;
53387a52a97eSRobert Watson 
5339c1685086SJeff Roberson 	allocs = frees = sleeps = xdomain = 0;
53407a52a97eSRobert Watson 	cachefree = 0;
53413aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
53427a52a97eSRobert Watson 		cache = &z->uz_cpu[cpu];
5343376b1ba3SJeff Roberson 		cachefree += cache->uc_allocbucket.ucb_cnt;
5344376b1ba3SJeff Roberson 		cachefree += cache->uc_freebucket.ucb_cnt;
5345376b1ba3SJeff Roberson 		xdomain += cache->uc_crossbucket.ucb_cnt;
5346376b1ba3SJeff Roberson 		cachefree += cache->uc_crossbucket.ucb_cnt;
53477a52a97eSRobert Watson 		allocs += cache->uc_allocs;
53487a52a97eSRobert Watson 		frees += cache->uc_frees;
53497a52a97eSRobert Watson 	}
53502efcc8cbSGleb Smirnoff 	allocs += counter_u64_fetch(z->uz_allocs);
53512efcc8cbSGleb Smirnoff 	frees += counter_u64_fetch(z->uz_frees);
5352c6fd3e23SJeff Roberson 	xdomain += counter_u64_fetch(z->uz_xdomain);
5353bf965959SSean Bruno 	sleeps += z->uz_sleeps;
53547a52a97eSRobert Watson 	if (cachefreep != NULL)
53557a52a97eSRobert Watson 		*cachefreep = cachefree;
53567a52a97eSRobert Watson 	if (allocsp != NULL)
53577a52a97eSRobert Watson 		*allocsp = allocs;
53587a52a97eSRobert Watson 	if (freesp != NULL)
53597a52a97eSRobert Watson 		*freesp = frees;
5360bf965959SSean Bruno 	if (sleepsp != NULL)
5361bf965959SSean Bruno 		*sleepsp = sleeps;
5362c1685086SJeff Roberson 	if (xdomainp != NULL)
5363c1685086SJeff Roberson 		*xdomainp = xdomain;
53647a52a97eSRobert Watson }
5365a0d4b0aeSRobert Watson #endif /* DDB */
53667a52a97eSRobert Watson 
53677a52a97eSRobert Watson static int
53687a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
53697a52a97eSRobert Watson {
53707a52a97eSRobert Watson 	uma_keg_t kz;
53717a52a97eSRobert Watson 	uma_zone_t z;
53727a52a97eSRobert Watson 	int count;
53737a52a97eSRobert Watson 
53747a52a97eSRobert Watson 	count = 0;
5375111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
53767a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
53777a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
53787a52a97eSRobert Watson 			count++;
53797a52a97eSRobert Watson 	}
5380b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link)
5381b47acb0aSGleb Smirnoff 		count++;
5382b47acb0aSGleb Smirnoff 
5383111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
53847a52a97eSRobert Watson 	return (sysctl_handle_int(oidp, &count, 0, req));
53857a52a97eSRobert Watson }
53867a52a97eSRobert Watson 
5387b47acb0aSGleb Smirnoff static void
5388b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf,
5389b47acb0aSGleb Smirnoff     struct uma_percpu_stat *ups, bool internal)
5390b47acb0aSGleb Smirnoff {
5391b47acb0aSGleb Smirnoff 	uma_zone_domain_t zdom;
5392b47acb0aSGleb Smirnoff 	uma_cache_t cache;
5393b47acb0aSGleb Smirnoff 	int i;
5394b47acb0aSGleb Smirnoff 
5395b47acb0aSGleb Smirnoff 	for (i = 0; i < vm_ndomains; i++) {
5396c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(z, i);
5397b47acb0aSGleb Smirnoff 		uth->uth_zone_free += zdom->uzd_nitems;
5398b47acb0aSGleb Smirnoff 	}
5399b47acb0aSGleb Smirnoff 	uth->uth_allocs = counter_u64_fetch(z->uz_allocs);
5400b47acb0aSGleb Smirnoff 	uth->uth_frees = counter_u64_fetch(z->uz_frees);
5401b47acb0aSGleb Smirnoff 	uth->uth_fails = counter_u64_fetch(z->uz_fails);
5402c6fd3e23SJeff Roberson 	uth->uth_xdomain = counter_u64_fetch(z->uz_xdomain);
5403b47acb0aSGleb Smirnoff 	uth->uth_sleeps = z->uz_sleeps;
54041de9724eSMark Johnston 
5405b47acb0aSGleb Smirnoff 	for (i = 0; i < mp_maxid + 1; i++) {
5406b47acb0aSGleb Smirnoff 		bzero(&ups[i], sizeof(*ups));
5407b47acb0aSGleb Smirnoff 		if (internal || CPU_ABSENT(i))
5408b47acb0aSGleb Smirnoff 			continue;
5409b47acb0aSGleb Smirnoff 		cache = &z->uz_cpu[i];
5410376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt;
5411376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt;
5412376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt;
5413b47acb0aSGleb Smirnoff 		ups[i].ups_allocs = cache->uc_allocs;
5414b47acb0aSGleb Smirnoff 		ups[i].ups_frees = cache->uc_frees;
5415b47acb0aSGleb Smirnoff 	}
5416b47acb0aSGleb Smirnoff }
5417b47acb0aSGleb Smirnoff 
54187a52a97eSRobert Watson static int
54197a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
54207a52a97eSRobert Watson {
54217a52a97eSRobert Watson 	struct uma_stream_header ush;
54227a52a97eSRobert Watson 	struct uma_type_header uth;
542363b5d112SKonstantin Belousov 	struct uma_percpu_stat *ups;
54247a52a97eSRobert Watson 	struct sbuf sbuf;
54257a52a97eSRobert Watson 	uma_keg_t kz;
54267a52a97eSRobert Watson 	uma_zone_t z;
54274bd61e19SJeff Roberson 	uint64_t items;
54288b987a77SJeff Roberson 	uint32_t kfree, pages;
54294e657159SMatthew D Fleming 	int count, error, i;
54307a52a97eSRobert Watson 
543100f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
543200f0e671SMatthew D Fleming 	if (error != 0)
543300f0e671SMatthew D Fleming 		return (error);
54344e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
54351eafc078SIan Lepore 	sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
543663b5d112SKonstantin Belousov 	ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK);
54374e657159SMatthew D Fleming 
5438404a593eSMatthew D Fleming 	count = 0;
5439111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
54407a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
54417a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
54427a52a97eSRobert Watson 			count++;
54437a52a97eSRobert Watson 	}
54447a52a97eSRobert Watson 
5445b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link)
5446b47acb0aSGleb Smirnoff 		count++;
5447b47acb0aSGleb Smirnoff 
54487a52a97eSRobert Watson 	/*
54497a52a97eSRobert Watson 	 * Insert stream header.
54507a52a97eSRobert Watson 	 */
54517a52a97eSRobert Watson 	bzero(&ush, sizeof(ush));
54527a52a97eSRobert Watson 	ush.ush_version = UMA_STREAM_VERSION;
5453ab3a57c0SRobert Watson 	ush.ush_maxcpus = (mp_maxid + 1);
54547a52a97eSRobert Watson 	ush.ush_count = count;
54554e657159SMatthew D Fleming 	(void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
54567a52a97eSRobert Watson 
54577a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
54588b987a77SJeff Roberson 		kfree = pages = 0;
54598b987a77SJeff Roberson 		for (i = 0; i < vm_ndomains; i++) {
54604ab3aee8SMark Johnston 			kfree += kz->uk_domain[i].ud_free_items;
54618b987a77SJeff Roberson 			pages += kz->uk_domain[i].ud_pages;
54628b987a77SJeff Roberson 		}
54637a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
54647a52a97eSRobert Watson 			bzero(&uth, sizeof(uth));
5465cbbb4a00SRobert Watson 			strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
54667a52a97eSRobert Watson 			uth.uth_align = kz->uk_align;
54677a52a97eSRobert Watson 			uth.uth_size = kz->uk_size;
54687a52a97eSRobert Watson 			uth.uth_rsize = kz->uk_rsize;
54694bd61e19SJeff Roberson 			if (z->uz_max_items > 0) {
54704bd61e19SJeff Roberson 				items = UZ_ITEMS_COUNT(z->uz_items);
54714bd61e19SJeff Roberson 				uth.uth_pages = (items / kz->uk_ipers) *
5472bb15d1c7SGleb Smirnoff 					kz->uk_ppera;
54734bd61e19SJeff Roberson 			} else
54748b987a77SJeff Roberson 				uth.uth_pages = pages;
5475f8c86a5fSGleb Smirnoff 			uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) *
5476bb15d1c7SGleb Smirnoff 			    kz->uk_ppera;
5477bb15d1c7SGleb Smirnoff 			uth.uth_limit = z->uz_max_items;
54788b987a77SJeff Roberson 			uth.uth_keg_free = kfree;
5479cbbb4a00SRobert Watson 
5480cbbb4a00SRobert Watson 			/*
5481cbbb4a00SRobert Watson 			 * A zone is secondary is it is not the first entry
5482cbbb4a00SRobert Watson 			 * on the keg's zone list.
5483cbbb4a00SRobert Watson 			 */
5484e20a199fSJeff Roberson 			if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
5485cbbb4a00SRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z))
5486cbbb4a00SRobert Watson 				uth.uth_zone_flags = UTH_ZONE_SECONDARY;
5487b47acb0aSGleb Smirnoff 			uma_vm_zone_stats(&uth, z, &sbuf, ups,
5488b47acb0aSGleb Smirnoff 			    kz->uk_flags & UMA_ZFLAG_INTERNAL);
548963b5d112SKonstantin Belousov 			(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
549063b5d112SKonstantin Belousov 			for (i = 0; i < mp_maxid + 1; i++)
549163b5d112SKonstantin Belousov 				(void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
54927a52a97eSRobert Watson 		}
54937a52a97eSRobert Watson 	}
5494b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
5495b47acb0aSGleb Smirnoff 		bzero(&uth, sizeof(uth));
5496b47acb0aSGleb Smirnoff 		strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
5497b47acb0aSGleb Smirnoff 		uth.uth_size = z->uz_size;
5498b47acb0aSGleb Smirnoff 		uma_vm_zone_stats(&uth, z, &sbuf, ups, false);
5499b47acb0aSGleb Smirnoff 		(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
5500b47acb0aSGleb Smirnoff 		for (i = 0; i < mp_maxid + 1; i++)
5501b47acb0aSGleb Smirnoff 			(void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
5502b47acb0aSGleb Smirnoff 	}
5503b47acb0aSGleb Smirnoff 
5504111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
55054e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
55064e657159SMatthew D Fleming 	sbuf_delete(&sbuf);
550763b5d112SKonstantin Belousov 	free(ups, M_TEMP);
55087a52a97eSRobert Watson 	return (error);
55097a52a97eSRobert Watson }
551048c5777eSRobert Watson 
55110a5a3ccbSGleb Smirnoff int
55120a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
55130a5a3ccbSGleb Smirnoff {
55140a5a3ccbSGleb Smirnoff 	uma_zone_t zone = *(uma_zone_t *)arg1;
551516be9f54SGleb Smirnoff 	int error, max;
55160a5a3ccbSGleb Smirnoff 
551716be9f54SGleb Smirnoff 	max = uma_zone_get_max(zone);
55180a5a3ccbSGleb Smirnoff 	error = sysctl_handle_int(oidp, &max, 0, req);
55190a5a3ccbSGleb Smirnoff 	if (error || !req->newptr)
55200a5a3ccbSGleb Smirnoff 		return (error);
55210a5a3ccbSGleb Smirnoff 
55220a5a3ccbSGleb Smirnoff 	uma_zone_set_max(zone, max);
55230a5a3ccbSGleb Smirnoff 
55240a5a3ccbSGleb Smirnoff 	return (0);
55250a5a3ccbSGleb Smirnoff }
55260a5a3ccbSGleb Smirnoff 
55270a5a3ccbSGleb Smirnoff int
55280a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
55290a5a3ccbSGleb Smirnoff {
553020a4e154SJeff Roberson 	uma_zone_t zone;
55310a5a3ccbSGleb Smirnoff 	int cur;
55320a5a3ccbSGleb Smirnoff 
553320a4e154SJeff Roberson 	/*
553420a4e154SJeff Roberson 	 * Some callers want to add sysctls for global zones that
553520a4e154SJeff Roberson 	 * may not yet exist so they pass a pointer to a pointer.
553620a4e154SJeff Roberson 	 */
553720a4e154SJeff Roberson 	if (arg2 == 0)
553820a4e154SJeff Roberson 		zone = *(uma_zone_t *)arg1;
553920a4e154SJeff Roberson 	else
554020a4e154SJeff Roberson 		zone = arg1;
55410a5a3ccbSGleb Smirnoff 	cur = uma_zone_get_cur(zone);
55420a5a3ccbSGleb Smirnoff 	return (sysctl_handle_int(oidp, &cur, 0, req));
55430a5a3ccbSGleb Smirnoff }
55440a5a3ccbSGleb Smirnoff 
554520a4e154SJeff Roberson static int
554620a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS)
554720a4e154SJeff Roberson {
554820a4e154SJeff Roberson 	uma_zone_t zone = arg1;
554920a4e154SJeff Roberson 	uint64_t cur;
555020a4e154SJeff Roberson 
555120a4e154SJeff Roberson 	cur = uma_zone_get_allocs(zone);
555220a4e154SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
555320a4e154SJeff Roberson }
555420a4e154SJeff Roberson 
555520a4e154SJeff Roberson static int
555620a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS)
555720a4e154SJeff Roberson {
555820a4e154SJeff Roberson 	uma_zone_t zone = arg1;
555920a4e154SJeff Roberson 	uint64_t cur;
556020a4e154SJeff Roberson 
556120a4e154SJeff Roberson 	cur = uma_zone_get_frees(zone);
556220a4e154SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
556320a4e154SJeff Roberson }
556420a4e154SJeff Roberson 
55656d204a6aSRyan Libby static int
55666d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS)
55676d204a6aSRyan Libby {
55686d204a6aSRyan Libby 	struct sbuf sbuf;
55696d204a6aSRyan Libby 	uma_zone_t zone = arg1;
55706d204a6aSRyan Libby 	int error;
55716d204a6aSRyan Libby 
55726d204a6aSRyan Libby 	sbuf_new_for_sysctl(&sbuf, NULL, 0, req);
55736d204a6aSRyan Libby 	if (zone->uz_flags != 0)
55746d204a6aSRyan Libby 		sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS);
55756d204a6aSRyan Libby 	else
55766d204a6aSRyan Libby 		sbuf_printf(&sbuf, "0");
55776d204a6aSRyan Libby 	error = sbuf_finish(&sbuf);
55786d204a6aSRyan Libby 	sbuf_delete(&sbuf);
55796d204a6aSRyan Libby 
55806d204a6aSRyan Libby 	return (error);
55816d204a6aSRyan Libby }
55826d204a6aSRyan Libby 
5583f7af5015SRyan Libby static int
5584f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS)
5585f7af5015SRyan Libby {
5586f7af5015SRyan Libby 	uma_keg_t keg = arg1;
5587f7af5015SRyan Libby 	int avail, effpct, total;
5588f7af5015SRyan Libby 
5589f7af5015SRyan Libby 	total = keg->uk_ppera * PAGE_SIZE;
559054c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0)
55919b8db4d0SRyan Libby 		total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize;
5592f7af5015SRyan Libby 	/*
5593f7af5015SRyan Libby 	 * We consider the client's requested size and alignment here, not the
5594f7af5015SRyan Libby 	 * real size determination uk_rsize, because we also adjust the real
5595f7af5015SRyan Libby 	 * size for internal implementation reasons (max bitset size).
5596f7af5015SRyan Libby 	 */
5597f7af5015SRyan Libby 	avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1);
5598f7af5015SRyan Libby 	if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
5599f7af5015SRyan Libby 		avail *= mp_maxid + 1;
5600f7af5015SRyan Libby 	effpct = 100 * avail / total;
5601f7af5015SRyan Libby 	return (sysctl_handle_int(oidp, &effpct, 0, req));
5602f7af5015SRyan Libby }
5603f7af5015SRyan Libby 
56044bd61e19SJeff Roberson static int
56054bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS)
56064bd61e19SJeff Roberson {
56074bd61e19SJeff Roberson 	uma_zone_t zone = arg1;
56084bd61e19SJeff Roberson 	uint64_t cur;
56094bd61e19SJeff Roberson 
56104bd61e19SJeff Roberson 	cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items));
56114bd61e19SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
56124bd61e19SJeff Roberson }
56134bd61e19SJeff Roberson 
56149542ea7bSGleb Smirnoff #ifdef INVARIANTS
56159542ea7bSGleb Smirnoff static uma_slab_t
56169542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item)
56179542ea7bSGleb Smirnoff {
56189542ea7bSGleb Smirnoff 	uma_slab_t slab;
56199542ea7bSGleb Smirnoff 	uma_keg_t keg;
56209542ea7bSGleb Smirnoff 	uint8_t *mem;
56219542ea7bSGleb Smirnoff 
56229542ea7bSGleb Smirnoff 	/*
56239542ea7bSGleb Smirnoff 	 * It is safe to return the slab here even though the
56249542ea7bSGleb Smirnoff 	 * zone is unlocked because the item's allocation state
56259542ea7bSGleb Smirnoff 	 * essentially holds a reference.
56269542ea7bSGleb Smirnoff 	 */
5627727c6918SJeff Roberson 	mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
5628727c6918SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5629bb15d1c7SGleb Smirnoff 		return (NULL);
563054c5ae80SRyan Libby 	if (zone->uz_flags & UMA_ZFLAG_VTOSLAB)
5631727c6918SJeff Roberson 		return (vtoslab((vm_offset_t)mem));
5632bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
563354c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0)
5634727c6918SJeff Roberson 		return ((uma_slab_t)(mem + keg->uk_pgoff));
56358b987a77SJeff Roberson 	KEG_LOCK(keg, 0);
56369542ea7bSGleb Smirnoff 	slab = hash_sfind(&keg->uk_hash, mem);
56378b987a77SJeff Roberson 	KEG_UNLOCK(keg, 0);
56389542ea7bSGleb Smirnoff 
56399542ea7bSGleb Smirnoff 	return (slab);
56409542ea7bSGleb Smirnoff }
56419542ea7bSGleb Smirnoff 
5642c5deaf04SGleb Smirnoff static bool
5643c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem)
5644c5deaf04SGleb Smirnoff {
5645c5deaf04SGleb Smirnoff 
5646727c6918SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5647c5deaf04SGleb Smirnoff 		return (true);
5648c5deaf04SGleb Smirnoff 
5649bb15d1c7SGleb Smirnoff 	return (uma_dbg_kskip(zone->uz_keg, mem));
5650c5deaf04SGleb Smirnoff }
5651c5deaf04SGleb Smirnoff 
5652c5deaf04SGleb Smirnoff static bool
5653c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem)
5654c5deaf04SGleb Smirnoff {
5655c5deaf04SGleb Smirnoff 	uintptr_t idx;
5656c5deaf04SGleb Smirnoff 
5657c5deaf04SGleb Smirnoff 	if (dbg_divisor == 0)
5658c5deaf04SGleb Smirnoff 		return (true);
5659c5deaf04SGleb Smirnoff 
5660c5deaf04SGleb Smirnoff 	if (dbg_divisor == 1)
5661c5deaf04SGleb Smirnoff 		return (false);
5662c5deaf04SGleb Smirnoff 
5663c5deaf04SGleb Smirnoff 	idx = (uintptr_t)mem >> PAGE_SHIFT;
5664c5deaf04SGleb Smirnoff 	if (keg->uk_ipers > 1) {
5665c5deaf04SGleb Smirnoff 		idx *= keg->uk_ipers;
5666c5deaf04SGleb Smirnoff 		idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize;
5667c5deaf04SGleb Smirnoff 	}
5668c5deaf04SGleb Smirnoff 
5669c5deaf04SGleb Smirnoff 	if ((idx / dbg_divisor) * dbg_divisor != idx) {
5670c5deaf04SGleb Smirnoff 		counter_u64_add(uma_skip_cnt, 1);
5671c5deaf04SGleb Smirnoff 		return (true);
5672c5deaf04SGleb Smirnoff 	}
5673c5deaf04SGleb Smirnoff 	counter_u64_add(uma_dbg_cnt, 1);
5674c5deaf04SGleb Smirnoff 
5675c5deaf04SGleb Smirnoff 	return (false);
5676c5deaf04SGleb Smirnoff }
5677c5deaf04SGleb Smirnoff 
56789542ea7bSGleb Smirnoff /*
56799542ea7bSGleb Smirnoff  * Set up the slab's freei data such that uma_dbg_free can function.
56809542ea7bSGleb Smirnoff  *
56819542ea7bSGleb Smirnoff  */
56829542ea7bSGleb Smirnoff static void
56839542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
56849542ea7bSGleb Smirnoff {
56859542ea7bSGleb Smirnoff 	uma_keg_t keg;
56869542ea7bSGleb Smirnoff 	int freei;
56879542ea7bSGleb Smirnoff 
56889542ea7bSGleb Smirnoff 	if (slab == NULL) {
56899542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
56909542ea7bSGleb Smirnoff 		if (slab == NULL)
5691952c8964SMark Johnston 			panic("uma: item %p did not belong to zone %s",
56929542ea7bSGleb Smirnoff 			    item, zone->uz_name);
56939542ea7bSGleb Smirnoff 	}
5694584061b4SJeff Roberson 	keg = zone->uz_keg;
56951e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
56969542ea7bSGleb Smirnoff 
5697942951baSRyan Libby 	if (BIT_TEST_SET_ATOMIC(keg->uk_ipers, freei,
5698942951baSRyan Libby 	    slab_dbg_bits(slab, keg)))
5699952c8964SMark Johnston 		panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)",
57009542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
57019542ea7bSGleb Smirnoff }
57029542ea7bSGleb Smirnoff 
57039542ea7bSGleb Smirnoff /*
57049542ea7bSGleb Smirnoff  * Verifies freed addresses.  Checks for alignment, valid slab membership
57059542ea7bSGleb Smirnoff  * and duplicate frees.
57069542ea7bSGleb Smirnoff  *
57079542ea7bSGleb Smirnoff  */
57089542ea7bSGleb Smirnoff static void
57099542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
57109542ea7bSGleb Smirnoff {
57119542ea7bSGleb Smirnoff 	uma_keg_t keg;
57129542ea7bSGleb Smirnoff 	int freei;
57139542ea7bSGleb Smirnoff 
57149542ea7bSGleb Smirnoff 	if (slab == NULL) {
57159542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
57169542ea7bSGleb Smirnoff 		if (slab == NULL)
5717952c8964SMark Johnston 			panic("uma: Freed item %p did not belong to zone %s",
57189542ea7bSGleb Smirnoff 			    item, zone->uz_name);
57199542ea7bSGleb Smirnoff 	}
5720584061b4SJeff Roberson 	keg = zone->uz_keg;
57211e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
57229542ea7bSGleb Smirnoff 
57239542ea7bSGleb Smirnoff 	if (freei >= keg->uk_ipers)
5724952c8964SMark Johnston 		panic("Invalid free of %p from zone %p(%s) slab %p(%d)",
57259542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
57269542ea7bSGleb Smirnoff 
57271e0701e1SJeff Roberson 	if (slab_item(slab, keg, freei) != item)
5728952c8964SMark Johnston 		panic("Unaligned free of %p from zone %p(%s) slab %p(%d)",
57299542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
57309542ea7bSGleb Smirnoff 
5731942951baSRyan Libby 	if (!BIT_TEST_CLR_ATOMIC(keg->uk_ipers, freei,
5732942951baSRyan Libby 	    slab_dbg_bits(slab, keg)))
5733952c8964SMark Johnston 		panic("Duplicate free of %p from zone %p(%s) slab %p(%d)",
57349542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
57359542ea7bSGleb Smirnoff }
57369542ea7bSGleb Smirnoff #endif /* INVARIANTS */
57379542ea7bSGleb Smirnoff 
573848c5777eSRobert Watson #ifdef DDB
573946d70077SConrad Meyer static int64_t
574046d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used,
57410223790fSConrad Meyer     uint64_t *sleeps, long *cachefree, uint64_t *xdomain)
574248c5777eSRobert Watson {
574346d70077SConrad Meyer 	uint64_t frees;
57440f9b7bf3SMark Johnston 	int i;
574548c5777eSRobert Watson 
574648c5777eSRobert Watson 	if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
574746d70077SConrad Meyer 		*allocs = counter_u64_fetch(z->uz_allocs);
57482efcc8cbSGleb Smirnoff 		frees = counter_u64_fetch(z->uz_frees);
574946d70077SConrad Meyer 		*sleeps = z->uz_sleeps;
575046d70077SConrad Meyer 		*cachefree = 0;
575146d70077SConrad Meyer 		*xdomain = 0;
575248c5777eSRobert Watson 	} else
575346d70077SConrad Meyer 		uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps,
575446d70077SConrad Meyer 		    xdomain);
57558b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
5756c6fd3e23SJeff Roberson 		*cachefree += ZDOM_GET(z, i)->uzd_nitems;
5757e20a199fSJeff Roberson 		if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
575848c5777eSRobert Watson 		    (LIST_FIRST(&kz->uk_zones) != z)))
57594ab3aee8SMark Johnston 			*cachefree += kz->uk_domain[i].ud_free_items;
57608b987a77SJeff Roberson 	}
576146d70077SConrad Meyer 	*used = *allocs - frees;
576246d70077SConrad Meyer 	return (((int64_t)*used + *cachefree) * kz->uk_size);
576346d70077SConrad Meyer }
57640f9b7bf3SMark Johnston 
576546d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma)
576646d70077SConrad Meyer {
576746d70077SConrad Meyer 	const char *fmt_hdr, *fmt_entry;
576846d70077SConrad Meyer 	uma_keg_t kz;
576946d70077SConrad Meyer 	uma_zone_t z;
577046d70077SConrad Meyer 	uint64_t allocs, used, sleeps, xdomain;
577146d70077SConrad Meyer 	long cachefree;
577246d70077SConrad Meyer 	/* variables for sorting */
577346d70077SConrad Meyer 	uma_keg_t cur_keg;
577446d70077SConrad Meyer 	uma_zone_t cur_zone, last_zone;
577546d70077SConrad Meyer 	int64_t cur_size, last_size, size;
577646d70077SConrad Meyer 	int ties;
577746d70077SConrad Meyer 
577846d70077SConrad Meyer 	/* /i option produces machine-parseable CSV output */
577946d70077SConrad Meyer 	if (modif[0] == 'i') {
578046d70077SConrad Meyer 		fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n";
578146d70077SConrad Meyer 		fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n";
578246d70077SConrad Meyer 	} else {
578346d70077SConrad Meyer 		fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n";
578446d70077SConrad Meyer 		fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n";
578546d70077SConrad Meyer 	}
578646d70077SConrad Meyer 
578746d70077SConrad Meyer 	db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests",
578846d70077SConrad Meyer 	    "Sleeps", "Bucket", "Total Mem", "XFree");
578946d70077SConrad Meyer 
579046d70077SConrad Meyer 	/* Sort the zones with largest size first. */
579146d70077SConrad Meyer 	last_zone = NULL;
579246d70077SConrad Meyer 	last_size = INT64_MAX;
579346d70077SConrad Meyer 	for (;;) {
579446d70077SConrad Meyer 		cur_zone = NULL;
579546d70077SConrad Meyer 		cur_size = -1;
579646d70077SConrad Meyer 		ties = 0;
579746d70077SConrad Meyer 		LIST_FOREACH(kz, &uma_kegs, uk_link) {
579846d70077SConrad Meyer 			LIST_FOREACH(z, &kz->uk_zones, uz_link) {
579946d70077SConrad Meyer 				/*
580046d70077SConrad Meyer 				 * In the case of size ties, print out zones
580146d70077SConrad Meyer 				 * in the order they are encountered.  That is,
580246d70077SConrad Meyer 				 * when we encounter the most recently output
580346d70077SConrad Meyer 				 * zone, we have already printed all preceding
580446d70077SConrad Meyer 				 * ties, and we must print all following ties.
580546d70077SConrad Meyer 				 */
580646d70077SConrad Meyer 				if (z == last_zone) {
580746d70077SConrad Meyer 					ties = 1;
580846d70077SConrad Meyer 					continue;
580946d70077SConrad Meyer 				}
581046d70077SConrad Meyer 				size = get_uma_stats(kz, z, &allocs, &used,
581146d70077SConrad Meyer 				    &sleeps, &cachefree, &xdomain);
581246d70077SConrad Meyer 				if (size > cur_size && size < last_size + ties)
581346d70077SConrad Meyer 				{
581446d70077SConrad Meyer 					cur_size = size;
581546d70077SConrad Meyer 					cur_zone = z;
581646d70077SConrad Meyer 					cur_keg = kz;
581746d70077SConrad Meyer 				}
581846d70077SConrad Meyer 			}
581946d70077SConrad Meyer 		}
582046d70077SConrad Meyer 		if (cur_zone == NULL)
582146d70077SConrad Meyer 			break;
582246d70077SConrad Meyer 
582346d70077SConrad Meyer 		size = get_uma_stats(cur_keg, cur_zone, &allocs, &used,
582446d70077SConrad Meyer 		    &sleeps, &cachefree, &xdomain);
582546d70077SConrad Meyer 		db_printf(fmt_entry, cur_zone->uz_name,
582646d70077SConrad Meyer 		    (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree,
582746d70077SConrad Meyer 		    (uintmax_t)allocs, (uintmax_t)sleeps,
582820a4e154SJeff Roberson 		    (unsigned)cur_zone->uz_bucket_size, (intmax_t)size,
582920a4e154SJeff Roberson 		    xdomain);
583046d70077SConrad Meyer 
5831687c94aaSJohn Baldwin 		if (db_pager_quit)
5832687c94aaSJohn Baldwin 			return;
583346d70077SConrad Meyer 		last_zone = cur_zone;
583446d70077SConrad Meyer 		last_size = cur_size;
583548c5777eSRobert Watson 	}
583648c5777eSRobert Watson }
583703175483SAlexander Motin 
583803175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache)
583903175483SAlexander Motin {
584003175483SAlexander Motin 	uma_zone_t z;
5841ab3185d1SJeff Roberson 	uint64_t allocs, frees;
58420f9b7bf3SMark Johnston 	long cachefree;
58430f9b7bf3SMark Johnston 	int i;
584403175483SAlexander Motin 
584503175483SAlexander Motin 	db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
584603175483SAlexander Motin 	    "Requests", "Bucket");
584703175483SAlexander Motin 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
5848c1685086SJeff Roberson 		uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL);
58490f9b7bf3SMark Johnston 		for (i = 0; i < vm_ndomains; i++)
5850c6fd3e23SJeff Roberson 			cachefree += ZDOM_GET(z, i)->uzd_nitems;
58510f9b7bf3SMark Johnston 		db_printf("%18s %8ju %8jd %8ld %12ju %8u\n",
585203175483SAlexander Motin 		    z->uz_name, (uintmax_t)z->uz_size,
585303175483SAlexander Motin 		    (intmax_t)(allocs - frees), cachefree,
585420a4e154SJeff Roberson 		    (uintmax_t)allocs, z->uz_bucket_size);
585503175483SAlexander Motin 		if (db_pager_quit)
585603175483SAlexander Motin 			return;
585703175483SAlexander Motin 	}
585803175483SAlexander Motin }
58599542ea7bSGleb Smirnoff #endif	/* DDB */
5860