xref: /freebsd/sys/vm/uma_core.c (revision 841e0a87576b745dfcdd1c43d1f1cb440e73b646)
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);
3342cb67bd7SGleb Smirnoff static bool cache_free(uma_zone_t, uma_cache_t, 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;
188884c39222SMark Johnston 	int i, pages;
1889099a0e58SBosko Milekic 
1890f7d35785SGleb Smirnoff 	pages = howmany(bytes, PAGE_SIZE);
1891f7d35785SGleb Smirnoff 	KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__));
1892a81c400eSJeff Roberson 
1893f7d35785SGleb Smirnoff 	*pflag = UMA_SLAB_BOOT;
189484c39222SMark Johnston 	m = vm_page_alloc_noobj_contig_domain(domain, malloc2vm_flags(wait) |
189584c39222SMark Johnston 	    VM_ALLOC_WIRED, pages, (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0,
189684c39222SMark Johnston 	    VM_MEMATTR_DEFAULT);
1897a81c400eSJeff Roberson 	if (m == NULL)
1898a81c400eSJeff Roberson 		return (NULL);
1899a81c400eSJeff Roberson 
1900a81c400eSJeff Roberson 	pa = VM_PAGE_TO_PHYS(m);
1901a81c400eSJeff Roberson 	for (i = 0; i < pages; i++, pa += PAGE_SIZE) {
1902a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
1903a81c400eSJeff Roberson     defined(__riscv) || defined(__powerpc64__)
1904a81c400eSJeff Roberson 		if ((wait & M_NODUMP) == 0)
1905a81c400eSJeff Roberson 			dump_add_page(pa);
1906a81c400eSJeff Roberson #endif
1907a81c400eSJeff Roberson 	}
1908f7d35785SGleb Smirnoff 
190984c39222SMark Johnston 	/* Allocate KVA and indirectly advance bootmem. */
191084c39222SMark Johnston 	return ((void *)pmap_map(&bootmem, m->phys_addr,
191184c39222SMark Johnston 	    m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE));
1912f7d35785SGleb Smirnoff }
1913f7d35785SGleb Smirnoff 
1914a81c400eSJeff Roberson static void
1915a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes)
1916a81c400eSJeff Roberson {
1917a81c400eSJeff Roberson 	vm_offset_t va;
1918a81c400eSJeff Roberson 	vm_page_t m;
1919a81c400eSJeff Roberson 
1920a81c400eSJeff Roberson 	va = (vm_offset_t)mem;
1921a81c400eSJeff Roberson 	m = PHYS_TO_VM_PAGE(pmap_kextract(va));
1922663de81fSMark Johnston 
1923663de81fSMark Johnston 	/*
1924663de81fSMark Johnston 	 * startup_alloc() returns direct-mapped slabs on some platforms.  Avoid
1925663de81fSMark Johnston 	 * unmapping ranges of the direct map.
1926663de81fSMark Johnston 	 */
1927663de81fSMark Johnston 	if (va >= bootstart && va + bytes <= bootmem)
1928a81c400eSJeff Roberson 		pmap_remove(kernel_pmap, va, va + bytes);
1929a81c400eSJeff Roberson 	for (; bytes != 0; bytes -= PAGE_SIZE, m++) {
1930a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
1931a81c400eSJeff Roberson     defined(__riscv) || defined(__powerpc64__)
1932a81c400eSJeff Roberson 		dump_drop_page(VM_PAGE_TO_PHYS(m));
1933a81c400eSJeff Roberson #endif
1934a81c400eSJeff Roberson 		vm_page_unwire_noq(m);
1935a81c400eSJeff Roberson 		vm_page_free(m);
1936a81c400eSJeff Roberson 	}
1937a81c400eSJeff Roberson }
1938a81c400eSJeff Roberson 
1939f7d35785SGleb Smirnoff /*
19408355f576SJeff Roberson  * Allocates a number of pages from the system
19418355f576SJeff Roberson  *
19428355f576SJeff Roberson  * Arguments:
19438355f576SJeff Roberson  *	bytes  The number of bytes requested
19448355f576SJeff Roberson  *	wait  Shall we wait?
19458355f576SJeff Roberson  *
19468355f576SJeff Roberson  * Returns:
19478355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
19488355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
19498355f576SJeff Roberson  */
19508355f576SJeff Roberson static void *
1951ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1952ab3185d1SJeff Roberson     int wait)
19538355f576SJeff Roberson {
19548355f576SJeff Roberson 	void *p;	/* Returned page */
19558355f576SJeff Roberson 
19562e47807cSJeff Roberson 	*pflag = UMA_SLAB_KERNEL;
19579978bd99SMark Johnston 	p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait);
19588355f576SJeff Roberson 
19598355f576SJeff Roberson 	return (p);
19608355f576SJeff Roberson }
19618355f576SJeff Roberson 
1962ab3059a8SMatt Macy static void *
1963ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1964ab3059a8SMatt Macy     int wait)
1965ab3059a8SMatt Macy {
1966ab3059a8SMatt Macy 	struct pglist alloctail;
1967ab3059a8SMatt Macy 	vm_offset_t addr, zkva;
1968ab3059a8SMatt Macy 	int cpu, flags;
1969ab3059a8SMatt Macy 	vm_page_t p, p_next;
1970ab3059a8SMatt Macy #ifdef NUMA
1971ab3059a8SMatt Macy 	struct pcpu *pc;
1972ab3059a8SMatt Macy #endif
1973ab3059a8SMatt Macy 
1974ab3059a8SMatt Macy 	MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE);
1975ab3059a8SMatt Macy 
1976013072f0SMark Johnston 	TAILQ_INIT(&alloctail);
1977a4667e09SMark Johnston 	flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | malloc2vm_flags(wait);
1978013072f0SMark Johnston 	*pflag = UMA_SLAB_KERNEL;
1979ab3059a8SMatt Macy 	for (cpu = 0; cpu <= mp_maxid; cpu++) {
1980ab3059a8SMatt Macy 		if (CPU_ABSENT(cpu)) {
1981a4667e09SMark Johnston 			p = vm_page_alloc_noobj(flags);
1982ab3059a8SMatt Macy 		} else {
1983ab3059a8SMatt Macy #ifndef NUMA
1984a4667e09SMark Johnston 			p = vm_page_alloc_noobj(flags);
1985ab3059a8SMatt Macy #else
1986ab3059a8SMatt Macy 			pc = pcpu_find(cpu);
198720526802SAndrew Gallatin 			if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain)))
198820526802SAndrew Gallatin 				p = NULL;
198920526802SAndrew Gallatin 			else
1990a4667e09SMark Johnston 				p = vm_page_alloc_noobj_domain(pc->pc_domain,
1991a4667e09SMark Johnston 				    flags);
1992ab3059a8SMatt Macy 			if (__predict_false(p == NULL))
1993a4667e09SMark Johnston 				p = vm_page_alloc_noobj(flags);
1994ab3059a8SMatt Macy #endif
1995ab3059a8SMatt Macy 		}
1996ab3059a8SMatt Macy 		if (__predict_false(p == NULL))
1997ab3059a8SMatt Macy 			goto fail;
1998ab3059a8SMatt Macy 		TAILQ_INSERT_TAIL(&alloctail, p, listq);
1999ab3059a8SMatt Macy 	}
2000ab3059a8SMatt Macy 	if ((addr = kva_alloc(bytes)) == 0)
2001ab3059a8SMatt Macy 		goto fail;
2002ab3059a8SMatt Macy 	zkva = addr;
2003ab3059a8SMatt Macy 	TAILQ_FOREACH(p, &alloctail, listq) {
2004ab3059a8SMatt Macy 		pmap_qenter(zkva, &p, 1);
2005ab3059a8SMatt Macy 		zkva += PAGE_SIZE;
2006ab3059a8SMatt Macy 	}
2007ab3059a8SMatt Macy 	return ((void*)addr);
2008ab3059a8SMatt Macy fail:
2009ab3059a8SMatt Macy 	TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
201088ea538aSMark Johnston 		vm_page_unwire_noq(p);
2011ab3059a8SMatt Macy 		vm_page_free(p);
2012ab3059a8SMatt Macy 	}
2013ab3059a8SMatt Macy 	return (NULL);
2014ab3059a8SMatt Macy }
2015ab3059a8SMatt Macy 
20168355f576SJeff Roberson /*
2017a9d6f1feSMark Johnston  * Allocates a number of pages not belonging to a VM object
20188355f576SJeff Roberson  *
20198355f576SJeff Roberson  * Arguments:
20208355f576SJeff Roberson  *	bytes  The number of bytes requested
20218355f576SJeff Roberson  *	wait   Shall we wait?
20228355f576SJeff Roberson  *
20238355f576SJeff Roberson  * Returns:
20248355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
20258355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
20268355f576SJeff Roberson  */
20278355f576SJeff Roberson static void *
2028ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
2029ab3185d1SJeff Roberson     int wait)
20308355f576SJeff Roberson {
2031a4915c21SAttilio Rao 	TAILQ_HEAD(, vm_page) alloctail;
2032a4915c21SAttilio Rao 	u_long npages;
2033b245ac95SAlan Cox 	vm_offset_t retkva, zkva;
2034a4915c21SAttilio Rao 	vm_page_t p, p_next;
2035e20a199fSJeff Roberson 	uma_keg_t keg;
2036a4667e09SMark Johnston 	int req;
20378355f576SJeff Roberson 
2038a4915c21SAttilio Rao 	TAILQ_INIT(&alloctail);
2039bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
2040a4667e09SMark Johnston 	req = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED;
2041a4667e09SMark Johnston 	if ((wait & M_WAITOK) != 0)
2042a4667e09SMark Johnston 		req |= VM_ALLOC_WAITOK;
2043a4915c21SAttilio Rao 
2044a4915c21SAttilio Rao 	npages = howmany(bytes, PAGE_SIZE);
2045a4915c21SAttilio Rao 	while (npages > 0) {
2046a4667e09SMark Johnston 		p = vm_page_alloc_noobj_domain(domain, req);
2047a4915c21SAttilio Rao 		if (p != NULL) {
2048a4915c21SAttilio Rao 			/*
2049a4915c21SAttilio Rao 			 * Since the page does not belong to an object, its
2050a4915c21SAttilio Rao 			 * listq is unused.
2051a4915c21SAttilio Rao 			 */
2052a4915c21SAttilio Rao 			TAILQ_INSERT_TAIL(&alloctail, p, listq);
2053a4915c21SAttilio Rao 			npages--;
2054a4915c21SAttilio Rao 			continue;
2055a4915c21SAttilio Rao 		}
20568355f576SJeff Roberson 		/*
2057a4915c21SAttilio Rao 		 * Page allocation failed, free intermediate pages and
2058a4915c21SAttilio Rao 		 * exit.
20598355f576SJeff Roberson 		 */
2060a4915c21SAttilio Rao 		TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
206188ea538aSMark Johnston 			vm_page_unwire_noq(p);
2062b245ac95SAlan Cox 			vm_page_free(p);
2063b245ac95SAlan Cox 		}
2064a4915c21SAttilio Rao 		return (NULL);
2065b245ac95SAlan Cox 	}
20668355f576SJeff Roberson 	*flags = UMA_SLAB_PRIV;
2067a4915c21SAttilio Rao 	zkva = keg->uk_kva +
2068a4915c21SAttilio Rao 	    atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
2069a4915c21SAttilio Rao 	retkva = zkva;
2070a4915c21SAttilio Rao 	TAILQ_FOREACH(p, &alloctail, listq) {
2071a4915c21SAttilio Rao 		pmap_qenter(zkva, &p, 1);
2072a4915c21SAttilio Rao 		zkva += PAGE_SIZE;
2073a4915c21SAttilio Rao 	}
20748355f576SJeff Roberson 
20758355f576SJeff Roberson 	return ((void *)retkva);
20768355f576SJeff Roberson }
20778355f576SJeff Roberson 
20788355f576SJeff Roberson /*
2079ec0d8280SRyan Libby  * Allocate physically contiguous pages.
2080ec0d8280SRyan Libby  */
2081ec0d8280SRyan Libby static void *
2082ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
2083ec0d8280SRyan Libby     int wait)
2084ec0d8280SRyan Libby {
2085ec0d8280SRyan Libby 
2086ec0d8280SRyan Libby 	*pflag = UMA_SLAB_KERNEL;
2087ec0d8280SRyan Libby 	return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain),
2088ec0d8280SRyan Libby 	    bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT));
2089ec0d8280SRyan Libby }
2090ec0d8280SRyan Libby 
2091ec0d8280SRyan Libby /*
20928355f576SJeff Roberson  * Frees a number of pages to the system
20938355f576SJeff Roberson  *
20948355f576SJeff Roberson  * Arguments:
20958355f576SJeff Roberson  *	mem   A pointer to the memory to be freed
20968355f576SJeff Roberson  *	size  The size of the memory being freed
20978355f576SJeff Roberson  *	flags The original p->us_flags field
20988355f576SJeff Roberson  *
20998355f576SJeff Roberson  * Returns:
21008355f576SJeff Roberson  *	Nothing
21018355f576SJeff Roberson  */
21028355f576SJeff Roberson static void
2103f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags)
21048355f576SJeff Roberson {
21053370c5bfSJeff Roberson 
2106a81c400eSJeff Roberson 	if ((flags & UMA_SLAB_BOOT) != 0) {
2107a81c400eSJeff Roberson 		startup_free(mem, size);
2108a81c400eSJeff Roberson 		return;
2109a81c400eSJeff Roberson 	}
2110a81c400eSJeff Roberson 
2111ec0d8280SRyan Libby 	KASSERT((flags & UMA_SLAB_KERNEL) != 0,
2112ec0d8280SRyan Libby 	    ("UMA: page_free used with invalid flags %x", flags));
21138355f576SJeff Roberson 
211449bfa624SAlan Cox 	kmem_free((vm_offset_t)mem, size);
21158355f576SJeff Roberson }
21168355f576SJeff Roberson 
21178355f576SJeff Roberson /*
2118ab3059a8SMatt Macy  * Frees pcpu zone allocations
2119ab3059a8SMatt Macy  *
2120ab3059a8SMatt Macy  * Arguments:
2121ab3059a8SMatt Macy  *	mem   A pointer to the memory to be freed
2122ab3059a8SMatt Macy  *	size  The size of the memory being freed
2123ab3059a8SMatt Macy  *	flags The original p->us_flags field
2124ab3059a8SMatt Macy  *
2125ab3059a8SMatt Macy  * Returns:
2126ab3059a8SMatt Macy  *	Nothing
2127ab3059a8SMatt Macy  */
2128ab3059a8SMatt Macy static void
2129ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags)
2130ab3059a8SMatt Macy {
2131ab3059a8SMatt Macy 	vm_offset_t sva, curva;
2132ab3059a8SMatt Macy 	vm_paddr_t paddr;
2133ab3059a8SMatt Macy 	vm_page_t m;
2134ab3059a8SMatt Macy 
2135ab3059a8SMatt Macy 	MPASS(size == (mp_maxid+1)*PAGE_SIZE);
21365ba16cf3SRyan Libby 
21375ba16cf3SRyan Libby 	if ((flags & UMA_SLAB_BOOT) != 0) {
21385ba16cf3SRyan Libby 		startup_free(mem, size);
21395ba16cf3SRyan Libby 		return;
21405ba16cf3SRyan Libby 	}
21415ba16cf3SRyan Libby 
2142ab3059a8SMatt Macy 	sva = (vm_offset_t)mem;
2143ab3059a8SMatt Macy 	for (curva = sva; curva < sva + size; curva += PAGE_SIZE) {
2144ab3059a8SMatt Macy 		paddr = pmap_kextract(curva);
2145ab3059a8SMatt Macy 		m = PHYS_TO_VM_PAGE(paddr);
214688ea538aSMark Johnston 		vm_page_unwire_noq(m);
2147ab3059a8SMatt Macy 		vm_page_free(m);
2148ab3059a8SMatt Macy 	}
2149ab3059a8SMatt Macy 	pmap_qremove(sva, size >> PAGE_SHIFT);
2150ab3059a8SMatt Macy 	kva_free(sva, size);
2151ab3059a8SMatt Macy }
2152ab3059a8SMatt Macy 
2153ab3059a8SMatt Macy /*
21548355f576SJeff Roberson  * Zero fill initializer
21558355f576SJeff Roberson  *
21568355f576SJeff Roberson  * Arguments/Returns follow uma_init specifications
21578355f576SJeff Roberson  */
2158b23f72e9SBrian Feldman static int
2159b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags)
21608355f576SJeff Roberson {
21618355f576SJeff Roberson 	bzero(mem, size);
2162b23f72e9SBrian Feldman 	return (0);
21638355f576SJeff Roberson }
21648355f576SJeff Roberson 
2165815db204SRyan Libby #ifdef INVARIANTS
216654007ce8SMark Johnston static struct noslabbits *
2167815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg)
2168815db204SRyan Libby {
2169815db204SRyan Libby 
2170815db204SRyan Libby 	return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers)));
2171815db204SRyan Libby }
2172815db204SRyan Libby #endif
2173815db204SRyan Libby 
21748355f576SJeff Roberson /*
21759b78b1f4SJeff Roberson  * Actual size of embedded struct slab (!OFFPAGE).
21769b78b1f4SJeff Roberson  */
217754007ce8SMark Johnston static size_t
21789b78b1f4SJeff Roberson slab_sizeof(int nitems)
21799b78b1f4SJeff Roberson {
21809b78b1f4SJeff Roberson 	size_t s;
21819b78b1f4SJeff Roberson 
2182815db204SRyan Libby 	s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS;
21839b78b1f4SJeff Roberson 	return (roundup(s, UMA_ALIGN_PTR + 1));
21849b78b1f4SJeff Roberson }
21859b78b1f4SJeff Roberson 
21864a8b575cSRyan Libby #define	UMA_FIXPT_SHIFT	31
21874a8b575cSRyan Libby #define	UMA_FRAC_FIXPT(n, d)						\
21884a8b575cSRyan Libby 	((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d)))
21894a8b575cSRyan Libby #define	UMA_FIXPT_PCT(f)						\
21904a8b575cSRyan Libby 	((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT))
21914a8b575cSRyan Libby #define	UMA_PCT_FIXPT(pct)	UMA_FRAC_FIXPT((pct), 100)
21924a8b575cSRyan Libby #define	UMA_MIN_EFF	UMA_PCT_FIXPT(100 - UMA_MAX_WASTE)
21934a8b575cSRyan Libby 
21949b78b1f4SJeff Roberson /*
21954a8b575cSRyan Libby  * Compute the number of items that will fit in a slab.  If hdr is true, the
21964a8b575cSRyan Libby  * item count may be limited to provide space in the slab for an inline slab
21974a8b575cSRyan Libby  * header.  Otherwise, all slab space will be provided for item storage.
21984a8b575cSRyan Libby  */
21994a8b575cSRyan Libby static u_int
22004a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr)
22014a8b575cSRyan Libby {
22024a8b575cSRyan Libby 	u_int ipers;
22034a8b575cSRyan Libby 	u_int padpi;
22044a8b575cSRyan Libby 
22054a8b575cSRyan Libby 	/* The padding between items is not needed after the last item. */
22064a8b575cSRyan Libby 	padpi = rsize - size;
22074a8b575cSRyan Libby 
22084a8b575cSRyan Libby 	if (hdr) {
22094a8b575cSRyan Libby 		/*
22104a8b575cSRyan Libby 		 * Start with the maximum item count and remove items until
22114a8b575cSRyan Libby 		 * the slab header first alongside the allocatable memory.
22124a8b575cSRyan Libby 		 */
22134a8b575cSRyan Libby 		for (ipers = MIN(SLAB_MAX_SETSIZE,
22144a8b575cSRyan Libby 		    (slabsize + padpi - slab_sizeof(1)) / rsize);
22154a8b575cSRyan Libby 		    ipers > 0 &&
22164a8b575cSRyan Libby 		    ipers * rsize - padpi + slab_sizeof(ipers) > slabsize;
22174a8b575cSRyan Libby 		    ipers--)
22184a8b575cSRyan Libby 			continue;
22194a8b575cSRyan Libby 	} else {
22204a8b575cSRyan Libby 		ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE);
22214a8b575cSRyan Libby 	}
22224a8b575cSRyan Libby 
22234a8b575cSRyan Libby 	return (ipers);
22244a8b575cSRyan Libby }
22254a8b575cSRyan Libby 
222627ca37acSRyan Libby struct keg_layout_result {
222727ca37acSRyan Libby 	u_int format;
222827ca37acSRyan Libby 	u_int slabsize;
222927ca37acSRyan Libby 	u_int ipers;
223027ca37acSRyan Libby 	u_int eff;
223127ca37acSRyan Libby };
223227ca37acSRyan Libby 
223327ca37acSRyan Libby static void
223427ca37acSRyan Libby keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt,
223527ca37acSRyan Libby     struct keg_layout_result *kl)
223627ca37acSRyan Libby {
223727ca37acSRyan Libby 	u_int total;
223827ca37acSRyan Libby 
223927ca37acSRyan Libby 	kl->format = fmt;
224027ca37acSRyan Libby 	kl->slabsize = slabsize;
224127ca37acSRyan Libby 
224227ca37acSRyan Libby 	/* Handle INTERNAL as inline with an extra page. */
224327ca37acSRyan Libby 	if ((fmt & UMA_ZFLAG_INTERNAL) != 0) {
224427ca37acSRyan Libby 		kl->format &= ~UMA_ZFLAG_INTERNAL;
224527ca37acSRyan Libby 		kl->slabsize += PAGE_SIZE;
224627ca37acSRyan Libby 	}
224727ca37acSRyan Libby 
224827ca37acSRyan Libby 	kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize,
224927ca37acSRyan Libby 	    (fmt & UMA_ZFLAG_OFFPAGE) == 0);
225027ca37acSRyan Libby 
225127ca37acSRyan Libby 	/* Account for memory used by an offpage slab header. */
225227ca37acSRyan Libby 	total = kl->slabsize;
225327ca37acSRyan Libby 	if ((fmt & UMA_ZFLAG_OFFPAGE) != 0)
225427ca37acSRyan Libby 		total += slabzone(kl->ipers)->uz_keg->uk_rsize;
225527ca37acSRyan Libby 
225627ca37acSRyan Libby 	kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total);
225727ca37acSRyan Libby }
225827ca37acSRyan Libby 
22599b78b1f4SJeff Roberson /*
22604a8b575cSRyan Libby  * Determine the format of a uma keg.  This determines where the slab header
22614a8b575cSRyan Libby  * will be placed (inline or offpage) and calculates ipers, rsize, and ppera.
22628355f576SJeff Roberson  *
22638355f576SJeff Roberson  * Arguments
2264e20a199fSJeff Roberson  *	keg  The zone we should initialize
22658355f576SJeff Roberson  *
22668355f576SJeff Roberson  * Returns
22678355f576SJeff Roberson  *	Nothing
22688355f576SJeff Roberson  */
22698355f576SJeff Roberson static void
22704a8b575cSRyan Libby keg_layout(uma_keg_t keg)
22718355f576SJeff Roberson {
227227ca37acSRyan Libby 	struct keg_layout_result kl = {}, kl_tmp;
227327ca37acSRyan Libby 	u_int fmts[2];
22744a8b575cSRyan Libby 	u_int alignsize;
227527ca37acSRyan Libby 	u_int nfmt;
22764a8b575cSRyan Libby 	u_int pages;
2277244f4554SBosko Milekic 	u_int rsize;
2278a55ebb7cSAndriy Gapon 	u_int slabsize;
227927ca37acSRyan Libby 	u_int i, j;
22808355f576SJeff Roberson 
22814a8b575cSRyan Libby 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
22824a8b575cSRyan Libby 	    (keg->uk_size <= UMA_PCPU_ALLOC_SIZE &&
22834a8b575cSRyan Libby 	     (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0),
22844a8b575cSRyan Libby 	    ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b",
22854a8b575cSRyan Libby 	     __func__, keg->uk_name, keg->uk_size, keg->uk_flags,
22864a8b575cSRyan Libby 	     PRINT_UMA_ZFLAGS));
2287bae55c4aSRyan Libby 	KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 ||
22884a8b575cSRyan Libby 	    (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0,
22894a8b575cSRyan Libby 	    ("%s: incompatible flags 0x%b", __func__, keg->uk_flags,
22904a8b575cSRyan Libby 	     PRINT_UMA_ZFLAGS));
2291e28a647dSGleb Smirnoff 
22924a8b575cSRyan Libby 	alignsize = keg->uk_align + 1;
2293b0dfc486SMark Johnston #ifdef KASAN
2294b0dfc486SMark Johnston 	/*
2295b0dfc486SMark Johnston 	 * ASAN requires that each allocation be aligned to the shadow map
2296b0dfc486SMark Johnston 	 * scale factor.
2297b0dfc486SMark Johnston 	 */
2298b0dfc486SMark Johnston 	if (alignsize < KASAN_SHADOW_SCALE)
2299b0dfc486SMark Johnston 		alignsize = KASAN_SHADOW_SCALE;
2300b0dfc486SMark Johnston #endif
2301ad97af7eSGleb Smirnoff 
2302ef72505eSJeff Roberson 	/*
2303ef72505eSJeff Roberson 	 * Calculate the size of each allocation (rsize) according to
2304ef72505eSJeff Roberson 	 * alignment.  If the requested size is smaller than we have
2305ef72505eSJeff Roberson 	 * allocation bits for we round it up.
2306ef72505eSJeff Roberson 	 */
23079b8db4d0SRyan Libby 	rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT);
23084a8b575cSRyan Libby 	rsize = roundup2(rsize, alignsize);
2309ad97af7eSGleb Smirnoff 
231027ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) {
23119b78b1f4SJeff Roberson 		/*
23124a8b575cSRyan Libby 		 * We want one item to start on every align boundary in a page.
23134a8b575cSRyan Libby 		 * To do this we will span pages.  We will also extend the item
23144a8b575cSRyan Libby 		 * by the size of align if it is an even multiple of align.
23154a8b575cSRyan Libby 		 * Otherwise, it would fall on the same boundary every time.
23169b78b1f4SJeff Roberson 		 */
23174a8b575cSRyan Libby 		if ((rsize & alignsize) == 0)
23184a8b575cSRyan Libby 			rsize += alignsize;
23194a8b575cSRyan Libby 		slabsize = rsize * (PAGE_SIZE / alignsize);
23204a8b575cSRyan Libby 		slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE);
23214a8b575cSRyan Libby 		slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE);
232227ca37acSRyan Libby 		slabsize = round_page(slabsize);
23234a8b575cSRyan Libby 	} else {
23244a8b575cSRyan Libby 		/*
232527ca37acSRyan Libby 		 * Start with a slab size of as many pages as it takes to
232627ca37acSRyan Libby 		 * represent a single item.  We will try to fit as many
232727ca37acSRyan Libby 		 * additional items into the slab as possible.
23284a8b575cSRyan Libby 		 */
232927ca37acSRyan Libby 		slabsize = round_page(keg->uk_size);
23301ca6ed45SGleb Smirnoff 	}
2331ad97af7eSGleb Smirnoff 
233227ca37acSRyan Libby 	/* Build a list of all of the available formats for this keg. */
233327ca37acSRyan Libby 	nfmt = 0;
233427ca37acSRyan Libby 
23354a8b575cSRyan Libby 	/* Evaluate an inline slab layout. */
23364a8b575cSRyan Libby 	if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0)
233727ca37acSRyan Libby 		fmts[nfmt++] = 0;
23384a8b575cSRyan Libby 
23394a8b575cSRyan Libby 	/* TODO: vm_page-embedded slab. */
2340244f4554SBosko Milekic 
234120e8e865SBosko Milekic 	/*
2342244f4554SBosko Milekic 	 * We can't do OFFPAGE if we're internal or if we've been
234320e8e865SBosko Milekic 	 * asked to not go to the VM for buckets.  If we do this we
2344bae55c4aSRyan Libby 	 * may end up going to the VM for slabs which we do not want
2345bae55c4aSRyan Libby 	 * to do if we're UMA_ZONE_VM, which clearly forbids it.
2346bae55c4aSRyan Libby 	 * In those cases, evaluate a pseudo-format called INTERNAL
2347bae55c4aSRyan Libby 	 * which has an inline slab header and one extra page to
2348bae55c4aSRyan Libby 	 * guarantee that it fits.
234927ca37acSRyan Libby 	 *
235027ca37acSRyan Libby 	 * Otherwise, see if using an OFFPAGE slab will improve our
235127ca37acSRyan Libby 	 * efficiency.
235220e8e865SBosko Milekic 	 */
2353bae55c4aSRyan Libby 	if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0)
235427ca37acSRyan Libby 		fmts[nfmt++] = UMA_ZFLAG_INTERNAL;
235527ca37acSRyan Libby 	else
235627ca37acSRyan Libby 		fmts[nfmt++] = UMA_ZFLAG_OFFPAGE;
2357244f4554SBosko Milekic 
2358ef72505eSJeff Roberson 	/*
235927ca37acSRyan Libby 	 * Choose a slab size and format which satisfy the minimum efficiency.
236027ca37acSRyan Libby 	 * Prefer the smallest slab size that meets the constraints.
2361ef72505eSJeff Roberson 	 *
236227ca37acSRyan Libby 	 * Start with a minimum slab size, to accommodate CACHESPREAD.  Then,
236327ca37acSRyan Libby 	 * for small items (up to PAGE_SIZE), the iteration increment is one
236427ca37acSRyan Libby 	 * page; and for large items, the increment is one item.
2365ef72505eSJeff Roberson 	 */
236627ca37acSRyan Libby 	i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize);
236727ca37acSRyan Libby 	KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u",
236827ca37acSRyan Libby 	    keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize,
236927ca37acSRyan Libby 	    rsize, i));
237027ca37acSRyan Libby 	for ( ; ; i++) {
237127ca37acSRyan Libby 		slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) :
237227ca37acSRyan Libby 		    round_page(rsize * (i - 1) + keg->uk_size);
237327ca37acSRyan Libby 
237427ca37acSRyan Libby 		for (j = 0; j < nfmt; j++) {
237527ca37acSRyan Libby 			/* Only if we have no viable format yet. */
237627ca37acSRyan Libby 			if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 &&
237727ca37acSRyan Libby 			    kl.ipers > 0)
237827ca37acSRyan Libby 				continue;
237927ca37acSRyan Libby 
238027ca37acSRyan Libby 			keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp);
238127ca37acSRyan Libby 			if (kl_tmp.eff <= kl.eff)
238227ca37acSRyan Libby 				continue;
238327ca37acSRyan Libby 
238427ca37acSRyan Libby 			kl = kl_tmp;
238527ca37acSRyan Libby 
238627ca37acSRyan Libby 			CTR6(KTR_UMA, "keg %s layout: format %#x "
238727ca37acSRyan Libby 			    "(ipers %u * rsize %u) / slabsize %#x = %u%% eff",
238827ca37acSRyan Libby 			    keg->uk_name, kl.format, kl.ipers, rsize,
238927ca37acSRyan Libby 			    kl.slabsize, UMA_FIXPT_PCT(kl.eff));
239027ca37acSRyan Libby 
239127ca37acSRyan Libby 			/* Stop when we reach the minimum efficiency. */
239227ca37acSRyan Libby 			if (kl.eff >= UMA_MIN_EFF)
239327ca37acSRyan Libby 				break;
23948355f576SJeff Roberson 		}
2395ad97af7eSGleb Smirnoff 
239633e5a1eaSRyan Libby 		if (kl.eff >= UMA_MIN_EFF || !multipage_slabs ||
239727ca37acSRyan Libby 		    slabsize >= SLAB_MAX_SETSIZE * rsize ||
239827ca37acSRyan Libby 		    (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0)
239927ca37acSRyan Libby 			break;
240027ca37acSRyan Libby 	}
240127ca37acSRyan Libby 
240227ca37acSRyan Libby 	pages = atop(kl.slabsize);
240327ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
240427ca37acSRyan Libby 		pages *= mp_maxid + 1;
240527ca37acSRyan Libby 
240627ca37acSRyan Libby 	keg->uk_rsize = rsize;
240727ca37acSRyan Libby 	keg->uk_ipers = kl.ipers;
240827ca37acSRyan Libby 	keg->uk_ppera = pages;
240927ca37acSRyan Libby 	keg->uk_flags |= kl.format;
241027ca37acSRyan Libby 
24114a8b575cSRyan Libby 	/*
24124a8b575cSRyan Libby 	 * How do we find the slab header if it is offpage or if not all item
24134a8b575cSRyan Libby 	 * start addresses are in the same page?  We could solve the latter
24144a8b575cSRyan Libby 	 * case with vaddr alignment, but we don't.
24154a8b575cSRyan Libby 	 */
241627ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 ||
241727ca37acSRyan Libby 	    (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) {
241854c5ae80SRyan Libby 		if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0)
241927ca37acSRyan Libby 			keg->uk_flags |= UMA_ZFLAG_HASH;
242054c5ae80SRyan Libby 		else
242127ca37acSRyan Libby 			keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
242254c5ae80SRyan Libby 	}
242327ca37acSRyan Libby 
2424e63a1c2fSRyan Libby 	CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u",
242527ca37acSRyan Libby 	    __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers,
242627ca37acSRyan Libby 	    pages);
24274a8b575cSRyan Libby 	KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE,
24284a8b575cSRyan Libby 	    ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__,
242927ca37acSRyan Libby 	     keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize,
243027ca37acSRyan Libby 	     keg->uk_ipers, pages));
2431e20a199fSJeff Roberson }
2432e20a199fSJeff Roberson 
24338355f576SJeff Roberson /*
2434099a0e58SBosko Milekic  * Keg header ctor.  This initializes all fields, locks, etc.  And inserts
2435099a0e58SBosko Milekic  * the keg onto the global keg list.
24368355f576SJeff Roberson  *
24378355f576SJeff Roberson  * Arguments/Returns follow uma_ctor specifications
2438099a0e58SBosko Milekic  *	udata  Actually uma_kctor_args
2439099a0e58SBosko Milekic  */
2440b23f72e9SBrian Feldman static int
2441b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags)
2442099a0e58SBosko Milekic {
2443099a0e58SBosko Milekic 	struct uma_kctor_args *arg = udata;
2444099a0e58SBosko Milekic 	uma_keg_t keg = mem;
2445099a0e58SBosko Milekic 	uma_zone_t zone;
24468b987a77SJeff Roberson 	int i;
2447099a0e58SBosko Milekic 
2448099a0e58SBosko Milekic 	bzero(keg, size);
2449099a0e58SBosko Milekic 	keg->uk_size = arg->size;
2450099a0e58SBosko Milekic 	keg->uk_init = arg->uminit;
2451099a0e58SBosko Milekic 	keg->uk_fini = arg->fini;
2452099a0e58SBosko Milekic 	keg->uk_align = arg->align;
24536fd34d6fSJeff Roberson 	keg->uk_reserve = 0;
2454099a0e58SBosko Milekic 	keg->uk_flags = arg->flags;
2455099a0e58SBosko Milekic 
2456099a0e58SBosko Milekic 	/*
2457194a979eSMark Johnston 	 * We use a global round-robin policy by default.  Zones with
2458dfe13344SJeff Roberson 	 * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which
2459dfe13344SJeff Roberson 	 * case the iterator is never run.
2460194a979eSMark Johnston 	 */
2461194a979eSMark Johnston 	keg->uk_dr.dr_policy = DOMAINSET_RR();
2462194a979eSMark Johnston 	keg->uk_dr.dr_iter = 0;
2463194a979eSMark Johnston 
2464194a979eSMark Johnston 	/*
2465c8b0a88bSJeff Roberson 	 * The primary zone is passed to us at keg-creation time.
2466099a0e58SBosko Milekic 	 */
2467099a0e58SBosko Milekic 	zone = arg->zone;
2468e20a199fSJeff Roberson 	keg->uk_name = zone->uz_name;
2469099a0e58SBosko Milekic 
2470099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_ZINIT)
2471099a0e58SBosko Milekic 		keg->uk_init = zero_init;
2472099a0e58SBosko Milekic 
2473cfcae3f8SGleb Smirnoff 	if (arg->flags & UMA_ZONE_MALLOC)
247454c5ae80SRyan Libby 		keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
2475e20a199fSJeff Roberson 
247654c5ae80SRyan Libby #ifndef SMP
2477ad97af7eSGleb Smirnoff 	keg->uk_flags &= ~UMA_ZONE_PCPU;
2478ad97af7eSGleb Smirnoff #endif
2479ad97af7eSGleb Smirnoff 
24804a8b575cSRyan Libby 	keg_layout(keg);
2481099a0e58SBosko Milekic 
24828b987a77SJeff Roberson 	/*
2483c6fd3e23SJeff Roberson 	 * Use a first-touch NUMA policy for kegs that pmap_extract() will
2484c6fd3e23SJeff Roberson 	 * work on.  Use round-robin for everything else.
2485dfe13344SJeff Roberson 	 *
2486dfe13344SJeff Roberson 	 * Zones may override the default by specifying either.
24878b987a77SJeff Roberson 	 */
2488dfe13344SJeff Roberson #ifdef NUMA
2489dfe13344SJeff Roberson 	if ((keg->uk_flags &
2490c6fd3e23SJeff Roberson 	    (UMA_ZONE_ROUNDROBIN | UMA_ZFLAG_CACHE | UMA_ZONE_NOTPAGE)) == 0)
2491dfe13344SJeff Roberson 		keg->uk_flags |= UMA_ZONE_FIRSTTOUCH;
2492dfe13344SJeff Roberson 	else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0)
2493dfe13344SJeff Roberson 		keg->uk_flags |= UMA_ZONE_ROUNDROBIN;
24948b987a77SJeff Roberson #endif
24958b987a77SJeff Roberson 
2496099a0e58SBosko Milekic 	/*
2497099a0e58SBosko Milekic 	 * If we haven't booted yet we need allocations to go through the
2498099a0e58SBosko Milekic 	 * startup cache until the vm is ready.
2499099a0e58SBosko Milekic 	 */
250077e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC
2501a81c400eSJeff Roberson 	if (keg->uk_ppera == 1)
250277e19437SGleb Smirnoff 		keg->uk_allocf = uma_small_alloc;
2503a81c400eSJeff Roberson 	else
25048cd02d00SAlan Cox #endif
2505a81c400eSJeff Roberson 	if (booted < BOOT_KVA)
2506a81c400eSJeff Roberson 		keg->uk_allocf = startup_alloc;
2507ab3059a8SMatt Macy 	else if (keg->uk_flags & UMA_ZONE_PCPU)
2508ab3059a8SMatt Macy 		keg->uk_allocf = pcpu_page_alloc;
2509ec0d8280SRyan Libby 	else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1)
2510ec0d8280SRyan Libby 		keg->uk_allocf = contig_alloc;
251177e19437SGleb Smirnoff 	else
251277e19437SGleb Smirnoff 		keg->uk_allocf = page_alloc;
251377e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC
251477e19437SGleb Smirnoff 	if (keg->uk_ppera == 1)
251577e19437SGleb Smirnoff 		keg->uk_freef = uma_small_free;
251677e19437SGleb Smirnoff 	else
251777e19437SGleb Smirnoff #endif
2518ab3059a8SMatt Macy 	if (keg->uk_flags & UMA_ZONE_PCPU)
2519ab3059a8SMatt Macy 		keg->uk_freef = pcpu_page_free;
2520ab3059a8SMatt Macy 	else
252177e19437SGleb Smirnoff 		keg->uk_freef = page_free;
2522099a0e58SBosko Milekic 
2523099a0e58SBosko Milekic 	/*
25248b987a77SJeff Roberson 	 * Initialize keg's locks.
2525099a0e58SBosko Milekic 	 */
25268b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
25278b987a77SJeff Roberson 		KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS));
2528099a0e58SBosko Milekic 
2529099a0e58SBosko Milekic 	/*
2530099a0e58SBosko Milekic 	 * If we're putting the slab header in the actual page we need to
25319b78b1f4SJeff Roberson 	 * figure out where in each page it goes.  See slab_sizeof
25329b78b1f4SJeff Roberson 	 * definition.
2533099a0e58SBosko Milekic 	 */
253454c5ae80SRyan Libby 	if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) {
25359b78b1f4SJeff Roberson 		size_t shsize;
25369b78b1f4SJeff Roberson 
25379b78b1f4SJeff Roberson 		shsize = slab_sizeof(keg->uk_ipers);
25389b78b1f4SJeff Roberson 		keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize;
2539244f4554SBosko Milekic 		/*
2540244f4554SBosko Milekic 		 * The only way the following is possible is if with our
2541244f4554SBosko Milekic 		 * UMA_ALIGN_PTR adjustments we are now bigger than
2542244f4554SBosko Milekic 		 * UMA_SLAB_SIZE.  I haven't checked whether this is
2543244f4554SBosko Milekic 		 * mathematically possible for all cases, so we make
2544244f4554SBosko Milekic 		 * sure here anyway.
2545244f4554SBosko Milekic 		 */
25469b78b1f4SJeff Roberson 		KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera,
25473d5e3df7SGleb Smirnoff 		    ("zone %s ipers %d rsize %d size %d slab won't fit",
25483d5e3df7SGleb Smirnoff 		    zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size));
2549099a0e58SBosko Milekic 	}
2550099a0e58SBosko Milekic 
255154c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_HASH)
25523b2f2cb8SAlexander Motin 		hash_alloc(&keg->uk_hash, 0);
2553099a0e58SBosko Milekic 
2554e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone);
2555099a0e58SBosko Milekic 
2556099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
2557099a0e58SBosko Milekic 
2558111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
2559099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
2560111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
2561b23f72e9SBrian Feldman 	return (0);
2562099a0e58SBosko Milekic }
2563099a0e58SBosko Milekic 
25642efcc8cbSGleb Smirnoff static void
2565a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused)
2566a81c400eSJeff Roberson {
2567a81c400eSJeff Roberson 	uma_keg_t keg;
2568a81c400eSJeff Roberson 
2569a81c400eSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
2570a81c400eSJeff Roberson 		return;
2571a81c400eSJeff Roberson 	KEG_GET(zone, keg);
2572ec0d8280SRyan Libby 
2573ec0d8280SRyan Libby 	if (keg->uk_allocf == startup_alloc) {
2574ec0d8280SRyan Libby 		/* Switch to the real allocator. */
2575f96d4157SJeff Roberson 		if (keg->uk_flags & UMA_ZONE_PCPU)
2576f96d4157SJeff Roberson 			keg->uk_allocf = pcpu_page_alloc;
2577ec0d8280SRyan Libby 		else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 &&
2578ec0d8280SRyan Libby 		    keg->uk_ppera > 1)
2579ec0d8280SRyan Libby 			keg->uk_allocf = contig_alloc;
2580ec0d8280SRyan Libby 		else
2581a81c400eSJeff Roberson 			keg->uk_allocf = page_alloc;
2582a81c400eSJeff Roberson 	}
2583ec0d8280SRyan Libby }
2584a81c400eSJeff Roberson 
2585a81c400eSJeff Roberson static void
258620a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused)
25872efcc8cbSGleb Smirnoff {
25882efcc8cbSGleb Smirnoff 
25892efcc8cbSGleb Smirnoff 	zone->uz_allocs = counter_u64_alloc(M_WAITOK);
25902efcc8cbSGleb Smirnoff 	zone->uz_frees = counter_u64_alloc(M_WAITOK);
25912efcc8cbSGleb Smirnoff 	zone->uz_fails = counter_u64_alloc(M_WAITOK);
2592c6fd3e23SJeff Roberson 	zone->uz_xdomain = counter_u64_alloc(M_WAITOK);
25932efcc8cbSGleb Smirnoff }
25942efcc8cbSGleb Smirnoff 
259520a4e154SJeff Roberson static void
259620a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused)
259720a4e154SJeff Roberson {
259820a4e154SJeff Roberson 	uma_zone_domain_t zdom;
25998b987a77SJeff Roberson 	uma_domain_t dom;
260020a4e154SJeff Roberson 	uma_keg_t keg;
260120a4e154SJeff Roberson 	struct sysctl_oid *oid, *domainoid;
26023b490537SJeff Roberson 	int domains, i, cnt;
260320a4e154SJeff Roberson 	static const char *nokeg = "cache zone";
260420a4e154SJeff Roberson 	char *c;
260520a4e154SJeff Roberson 
260620a4e154SJeff Roberson 	/*
260720a4e154SJeff Roberson 	 * Make a sysctl safe copy of the zone name by removing
260820a4e154SJeff Roberson 	 * any special characters and handling dups by appending
260920a4e154SJeff Roberson 	 * an index.
261020a4e154SJeff Roberson 	 */
261120a4e154SJeff Roberson 	if (zone->uz_namecnt != 0) {
26123b490537SJeff Roberson 		/* Count the number of decimal digits and '_' separator. */
26133b490537SJeff Roberson 		for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++)
26143b490537SJeff Roberson 			cnt /= 10;
26153b490537SJeff Roberson 		zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1,
26163b490537SJeff Roberson 		    M_UMA, M_WAITOK);
261720a4e154SJeff Roberson 		sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name,
261820a4e154SJeff Roberson 		    zone->uz_namecnt);
261920a4e154SJeff Roberson 	} else
262020a4e154SJeff Roberson 		zone->uz_ctlname = strdup(zone->uz_name, M_UMA);
262120a4e154SJeff Roberson 	for (c = zone->uz_ctlname; *c != '\0'; c++)
262220a4e154SJeff Roberson 		if (strchr("./\\ -", *c) != NULL)
262320a4e154SJeff Roberson 			*c = '_';
262420a4e154SJeff Roberson 
262520a4e154SJeff Roberson 	/*
262620a4e154SJeff Roberson 	 * Basic parameters at the root.
262720a4e154SJeff Roberson 	 */
262820a4e154SJeff Roberson 	zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma),
26297029da5cSPawel Biernacki 	    OID_AUTO, zone->uz_ctlname, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
263020a4e154SJeff Roberson 	oid = zone->uz_oid;
263120a4e154SJeff Roberson 	SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
263220a4e154SJeff Roberson 	    "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size");
26336d204a6aSRyan Libby 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
26346d204a6aSRyan Libby 	    "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE,
26356d204a6aSRyan Libby 	    zone, 0, sysctl_handle_uma_zone_flags, "A",
263620a4e154SJeff Roberson 	    "Allocator configuration flags");
263720a4e154SJeff Roberson 	SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
263820a4e154SJeff Roberson 	    "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0,
263920a4e154SJeff Roberson 	    "Desired per-cpu cache size");
264020a4e154SJeff Roberson 	SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
264120a4e154SJeff Roberson 	    "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0,
264220a4e154SJeff Roberson 	    "Maximum allowed per-cpu cache size");
264320a4e154SJeff Roberson 
264420a4e154SJeff Roberson 	/*
264520a4e154SJeff Roberson 	 * keg if present.
264620a4e154SJeff Roberson 	 */
264754c5ae80SRyan Libby 	if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0)
26488b987a77SJeff Roberson 		domains = vm_ndomains;
26498b987a77SJeff Roberson 	else
26508b987a77SJeff Roberson 		domains = 1;
265120a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
26527029da5cSPawel Biernacki 	    "keg", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
265320a4e154SJeff Roberson 	keg = zone->uz_keg;
26543b490537SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) {
265520a4e154SJeff Roberson 		SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
265620a4e154SJeff Roberson 		    "name", CTLFLAG_RD, keg->uk_name, "Keg name");
265720a4e154SJeff Roberson 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
265820a4e154SJeff Roberson 		    "rsize", CTLFLAG_RD, &keg->uk_rsize, 0,
265920a4e154SJeff Roberson 		    "Real object size with alignment");
266020a4e154SJeff Roberson 		SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
266120a4e154SJeff Roberson 		    "ppera", CTLFLAG_RD, &keg->uk_ppera, 0,
266220a4e154SJeff Roberson 		    "pages per-slab allocation");
266320a4e154SJeff Roberson 		SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
266420a4e154SJeff Roberson 		    "ipers", CTLFLAG_RD, &keg->uk_ipers, 0,
266520a4e154SJeff Roberson 		    "items available per-slab");
266620a4e154SJeff Roberson 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
266720a4e154SJeff Roberson 		    "align", CTLFLAG_RD, &keg->uk_align, 0,
266820a4e154SJeff Roberson 		    "item alignment mask");
2669f09cbea3SMark Johnston 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2670f09cbea3SMark Johnston 		    "reserve", CTLFLAG_RD, &keg->uk_reserve, 0,
2671f09cbea3SMark Johnston 		    "number of reserved items");
2672f7af5015SRyan Libby 		SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2673f7af5015SRyan Libby 		    "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
2674f7af5015SRyan Libby 		    keg, 0, sysctl_handle_uma_slab_efficiency, "I",
2675f7af5015SRyan Libby 		    "Slab utilization (100 - internal fragmentation %)");
26768b987a77SJeff Roberson 		domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid),
26777029da5cSPawel Biernacki 		    OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
26788b987a77SJeff Roberson 		for (i = 0; i < domains; i++) {
26798b987a77SJeff Roberson 			dom = &keg->uk_domain[i];
26808b987a77SJeff Roberson 			oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
26817029da5cSPawel Biernacki 			    OID_AUTO, VM_DOMAIN(i)->vmd_name,
26827029da5cSPawel Biernacki 			    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
26838b987a77SJeff Roberson 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
26848b987a77SJeff Roberson 			    "pages", CTLFLAG_RD, &dom->ud_pages, 0,
26858b987a77SJeff Roberson 			    "Total pages currently allocated from VM");
26868b987a77SJeff Roberson 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
26874ab3aee8SMark Johnston 			    "free_items", CTLFLAG_RD, &dom->ud_free_items, 0,
2688d6e77cdaSMark Johnston 			    "Items free in the slab layer");
2689d6e77cdaSMark Johnston 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2690d6e77cdaSMark Johnston 			    "free_slabs", CTLFLAG_RD, &dom->ud_free_slabs, 0,
2691d6e77cdaSMark Johnston 			    "Unused slabs");
26928b987a77SJeff Roberson 		}
269320a4e154SJeff Roberson 	} else
269420a4e154SJeff Roberson 		SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
269520a4e154SJeff Roberson 		    "name", CTLFLAG_RD, nokeg, "Keg name");
269620a4e154SJeff Roberson 
269720a4e154SJeff Roberson 	/*
269820a4e154SJeff Roberson 	 * Information about zone limits.
269920a4e154SJeff Roberson 	 */
270020a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
27017029da5cSPawel Biernacki 	    "limit", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
27024bd61e19SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
27034bd61e19SJeff Roberson 	    "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
27044bd61e19SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_items, "QU",
2705e574d407SMark Johnston 	    "Current number of allocated items if limit is set");
270620a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
270720a4e154SJeff Roberson 	    "max_items", CTLFLAG_RD, &zone->uz_max_items, 0,
2708e574d407SMark Johnston 	    "Maximum number of allocated and cached items");
270920a4e154SJeff Roberson 	SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
271020a4e154SJeff Roberson 	    "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0,
271120a4e154SJeff Roberson 	    "Number of threads sleeping at limit");
271220a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
271320a4e154SJeff Roberson 	    "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0,
271420a4e154SJeff Roberson 	    "Total zone limit sleeps");
27154bd61e19SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2716c6fd3e23SJeff Roberson 	    "bucket_max", CTLFLAG_RD, &zone->uz_bucket_max, 0,
2717c6fd3e23SJeff Roberson 	    "Maximum number of items in each domain's bucket cache");
271820a4e154SJeff Roberson 
271920a4e154SJeff Roberson 	/*
27208b987a77SJeff Roberson 	 * Per-domain zone information.
272120a4e154SJeff Roberson 	 */
272220a4e154SJeff Roberson 	domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid),
27237029da5cSPawel Biernacki 	    OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
272420a4e154SJeff Roberson 	for (i = 0; i < domains; i++) {
2725c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(zone, i);
272620a4e154SJeff Roberson 		oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
27277029da5cSPawel Biernacki 		    OID_AUTO, VM_DOMAIN(i)->vmd_name,
27287029da5cSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
272920a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
273020a4e154SJeff Roberson 		    "nitems", CTLFLAG_RD, &zdom->uzd_nitems,
273120a4e154SJeff Roberson 		    "number of items in this domain");
273220a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
273320a4e154SJeff Roberson 		    "imax", CTLFLAG_RD, &zdom->uzd_imax,
273420a4e154SJeff Roberson 		    "maximum item count in this period");
273520a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
273620a4e154SJeff Roberson 		    "imin", CTLFLAG_RD, &zdom->uzd_imin,
273720a4e154SJeff Roberson 		    "minimum item count in this period");
273820a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
27392760658bSAlexander Motin 		    "bimin", CTLFLAG_RD, &zdom->uzd_bimin,
27402760658bSAlexander Motin 		    "Minimum item count in this batch");
27412760658bSAlexander Motin 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
274220a4e154SJeff Roberson 		    "wss", CTLFLAG_RD, &zdom->uzd_wss,
274320a4e154SJeff Roberson 		    "Working set size");
27442760658bSAlexander Motin 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
27452760658bSAlexander Motin 		    "limin", CTLFLAG_RD, &zdom->uzd_limin,
27462760658bSAlexander Motin 		    "Long time minimum item count");
27472760658bSAlexander Motin 		SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
27482760658bSAlexander Motin 		    "timin", CTLFLAG_RD, &zdom->uzd_timin, 0,
27492760658bSAlexander Motin 		    "Time since zero long time minimum item count");
275020a4e154SJeff Roberson 	}
275120a4e154SJeff Roberson 
275220a4e154SJeff Roberson 	/*
275320a4e154SJeff Roberson 	 * General statistics.
275420a4e154SJeff Roberson 	 */
275520a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
27567029da5cSPawel Biernacki 	    "stats", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
275720a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
275820a4e154SJeff Roberson 	    "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
275920a4e154SJeff Roberson 	    zone, 1, sysctl_handle_uma_zone_cur, "I",
276020a4e154SJeff Roberson 	    "Current number of allocated items");
276120a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
276220a4e154SJeff Roberson 	    "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
276320a4e154SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_allocs, "QU",
276420a4e154SJeff Roberson 	    "Total allocation calls");
276520a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
276620a4e154SJeff Roberson 	    "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
276720a4e154SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_frees, "QU",
276820a4e154SJeff Roberson 	    "Total free calls");
276920a4e154SJeff Roberson 	SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
277020a4e154SJeff Roberson 	    "fails", CTLFLAG_RD, &zone->uz_fails,
277120a4e154SJeff Roberson 	    "Number of allocation failures");
2772c6fd3e23SJeff Roberson 	SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2773c6fd3e23SJeff Roberson 	    "xdomain", CTLFLAG_RD, &zone->uz_xdomain,
277420a4e154SJeff Roberson 	    "Free calls from the wrong domain");
277520a4e154SJeff Roberson }
277620a4e154SJeff Roberson 
277720a4e154SJeff Roberson struct uma_zone_count {
277820a4e154SJeff Roberson 	const char	*name;
277920a4e154SJeff Roberson 	int		count;
278020a4e154SJeff Roberson };
278120a4e154SJeff Roberson 
278220a4e154SJeff Roberson static void
278320a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg)
278420a4e154SJeff Roberson {
278520a4e154SJeff Roberson 	struct uma_zone_count *cnt;
278620a4e154SJeff Roberson 
278720a4e154SJeff Roberson 	cnt = arg;
27883b490537SJeff Roberson 	/*
27893b490537SJeff Roberson 	 * Some zones are rapidly created with identical names and
27903b490537SJeff Roberson 	 * destroyed out of order.  This can lead to gaps in the count.
27913b490537SJeff Roberson 	 * Use one greater than the maximum observed for this name.
27923b490537SJeff Roberson 	 */
279320a4e154SJeff Roberson 	if (strcmp(zone->uz_name, cnt->name) == 0)
27943b490537SJeff Roberson 		cnt->count = MAX(cnt->count,
27953b490537SJeff Roberson 		    zone->uz_namecnt + 1);
279620a4e154SJeff Roberson }
279720a4e154SJeff Roberson 
2798cc7ce83aSJeff Roberson static void
2799cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone)
2800cc7ce83aSJeff Roberson {
2801cc7ce83aSJeff Roberson 	int i;
2802cc7ce83aSJeff Roberson 
2803cc7ce83aSJeff Roberson 	for (i = 0; i <= mp_maxid; i++) {
2804cc7ce83aSJeff Roberson 		cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size);
2805cc7ce83aSJeff Roberson 		cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags);
2806cc7ce83aSJeff Roberson 	}
2807cc7ce83aSJeff Roberson }
2808cc7ce83aSJeff Roberson 
2809099a0e58SBosko Milekic /*
2810099a0e58SBosko Milekic  * Zone header ctor.  This initializes all fields, locks, etc.
2811099a0e58SBosko Milekic  *
2812099a0e58SBosko Milekic  * Arguments/Returns follow uma_ctor specifications
2813099a0e58SBosko Milekic  *	udata  Actually uma_zctor_args
28148355f576SJeff Roberson  */
2815b23f72e9SBrian Feldman static int
2816b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags)
28178355f576SJeff Roberson {
281820a4e154SJeff Roberson 	struct uma_zone_count cnt;
28198355f576SJeff Roberson 	struct uma_zctor_args *arg = udata;
2820c6fd3e23SJeff Roberson 	uma_zone_domain_t zdom;
28218355f576SJeff Roberson 	uma_zone_t zone = mem;
2822099a0e58SBosko Milekic 	uma_zone_t z;
2823099a0e58SBosko Milekic 	uma_keg_t keg;
282408cfa56eSMark Johnston 	int i;
28258355f576SJeff Roberson 
28268355f576SJeff Roberson 	bzero(zone, size);
28278355f576SJeff Roberson 	zone->uz_name = arg->name;
28288355f576SJeff Roberson 	zone->uz_ctor = arg->ctor;
28298355f576SJeff Roberson 	zone->uz_dtor = arg->dtor;
2830099a0e58SBosko Milekic 	zone->uz_init = NULL;
2831099a0e58SBosko Milekic 	zone->uz_fini = NULL;
2832bf965959SSean Bruno 	zone->uz_sleeps = 0;
283320a4e154SJeff Roberson 	zone->uz_bucket_size = 0;
283420a4e154SJeff Roberson 	zone->uz_bucket_size_min = 0;
283520a4e154SJeff Roberson 	zone->uz_bucket_size_max = BUCKET_MAX;
2836d4665eaaSJeff Roberson 	zone->uz_flags = (arg->flags & UMA_ZONE_SMR);
28372f891cd5SPawel Jakub Dawidek 	zone->uz_warning = NULL;
2838ab3185d1SJeff Roberson 	/* The domain structures follow the cpu structures. */
2839c6fd3e23SJeff Roberson 	zone->uz_bucket_max = ULONG_MAX;
28402f891cd5SPawel Jakub Dawidek 	timevalclear(&zone->uz_ratecheck);
2841af526374SJeff Roberson 
284220a4e154SJeff Roberson 	/* Count the number of duplicate names. */
284320a4e154SJeff Roberson 	cnt.name = arg->name;
284420a4e154SJeff Roberson 	cnt.count = 0;
284520a4e154SJeff Roberson 	zone_foreach(zone_count, &cnt);
284620a4e154SJeff Roberson 	zone->uz_namecnt = cnt.count;
284791d947bfSJeff Roberson 	ZONE_CROSS_LOCK_INIT(zone);
28482efcc8cbSGleb Smirnoff 
2849c6fd3e23SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
2850c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(zone, i);
2851c6fd3e23SJeff Roberson 		ZDOM_LOCK_INIT(zone, zdom, (arg->flags & UMA_ZONE_MTXCLASS));
2852c6fd3e23SJeff Roberson 		STAILQ_INIT(&zdom->uzd_buckets);
2853c6fd3e23SJeff Roberson 	}
285408cfa56eSMark Johnston 
285510094910SMark Johnston #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN)
2856ca293436SRyan Libby 	if (arg->uminit == trash_init && arg->fini == trash_fini)
2857cc7ce83aSJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR;
285809c8cb71SMark Johnston #elif defined(KASAN)
285909c8cb71SMark Johnston 	if ((arg->flags & (UMA_ZONE_NOFREE | UMA_ZFLAG_CACHE)) != 0)
286009c8cb71SMark Johnston 		arg->flags |= UMA_ZONE_NOKASAN;
2861ca293436SRyan Libby #endif
2862ca293436SRyan Libby 
28630095a784SJeff Roberson 	/*
28640095a784SJeff Roberson 	 * This is a pure cache zone, no kegs.
28650095a784SJeff Roberson 	 */
28660095a784SJeff Roberson 	if (arg->import) {
2867727c6918SJeff Roberson 		KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0,
2868727c6918SJeff Roberson 		    ("zone_ctor: Import specified for non-cache zone."));
28696fd34d6fSJeff Roberson 		zone->uz_flags = arg->flags;
2870af526374SJeff Roberson 		zone->uz_size = arg->size;
28710095a784SJeff Roberson 		zone->uz_import = arg->import;
28720095a784SJeff Roberson 		zone->uz_release = arg->release;
28730095a784SJeff Roberson 		zone->uz_arg = arg->arg;
2874c6fd3e23SJeff Roberson #ifdef NUMA
2875c6fd3e23SJeff Roberson 		/*
2876c6fd3e23SJeff Roberson 		 * Cache zones are round-robin unless a policy is
2877c6fd3e23SJeff Roberson 		 * specified because they may have incompatible
2878c6fd3e23SJeff Roberson 		 * constraints.
2879c6fd3e23SJeff Roberson 		 */
2880c6fd3e23SJeff Roberson 		if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0)
2881c6fd3e23SJeff Roberson 			zone->uz_flags |= UMA_ZONE_ROUNDROBIN;
2882c6fd3e23SJeff Roberson #endif
2883111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
288403175483SAlexander Motin 		LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
2885111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
2886af526374SJeff Roberson 		goto out;
28870095a784SJeff Roberson 	}
28880095a784SJeff Roberson 
28890095a784SJeff Roberson 	/*
28900095a784SJeff Roberson 	 * Use the regular zone/keg/slab allocator.
28910095a784SJeff Roberson 	 */
2892b75c4efcSAndrew Turner 	zone->uz_import = zone_import;
2893b75c4efcSAndrew Turner 	zone->uz_release = zone_release;
28940095a784SJeff Roberson 	zone->uz_arg = zone;
2895bb15d1c7SGleb Smirnoff 	keg = arg->keg;
28960095a784SJeff Roberson 
2897099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_SECONDARY) {
289820a4e154SJeff Roberson 		KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
289920a4e154SJeff Roberson 		    ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
2900099a0e58SBosko Milekic 		KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
29018355f576SJeff Roberson 		zone->uz_init = arg->uminit;
2902e221e841SJeff Roberson 		zone->uz_fini = arg->fini;
2903e20a199fSJeff Roberson 		zone->uz_flags |= UMA_ZONE_SECONDARY;
2904111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
2905099a0e58SBosko Milekic 		ZONE_LOCK(zone);
2906099a0e58SBosko Milekic 		LIST_FOREACH(z, &keg->uk_zones, uz_link) {
2907099a0e58SBosko Milekic 			if (LIST_NEXT(z, uz_link) == NULL) {
2908099a0e58SBosko Milekic 				LIST_INSERT_AFTER(z, zone, uz_link);
2909099a0e58SBosko Milekic 				break;
2910099a0e58SBosko Milekic 			}
2911099a0e58SBosko Milekic 		}
2912099a0e58SBosko Milekic 		ZONE_UNLOCK(zone);
2913111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
2914e20a199fSJeff Roberson 	} else if (keg == NULL) {
2915e20a199fSJeff Roberson 		if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
2916e20a199fSJeff Roberson 		    arg->align, arg->flags)) == NULL)
2917b23f72e9SBrian Feldman 			return (ENOMEM);
2918099a0e58SBosko Milekic 	} else {
2919099a0e58SBosko Milekic 		struct uma_kctor_args karg;
2920b23f72e9SBrian Feldman 		int error;
2921099a0e58SBosko Milekic 
2922099a0e58SBosko Milekic 		/* We should only be here from uma_startup() */
2923099a0e58SBosko Milekic 		karg.size = arg->size;
2924099a0e58SBosko Milekic 		karg.uminit = arg->uminit;
2925099a0e58SBosko Milekic 		karg.fini = arg->fini;
2926099a0e58SBosko Milekic 		karg.align = arg->align;
2927d4665eaaSJeff Roberson 		karg.flags = (arg->flags & ~UMA_ZONE_SMR);
2928099a0e58SBosko Milekic 		karg.zone = zone;
2929b23f72e9SBrian Feldman 		error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
2930b23f72e9SBrian Feldman 		    flags);
2931b23f72e9SBrian Feldman 		if (error)
2932b23f72e9SBrian Feldman 			return (error);
2933099a0e58SBosko Milekic 	}
29340095a784SJeff Roberson 
293520a4e154SJeff Roberson 	/* Inherit properties from the keg. */
2936bb15d1c7SGleb Smirnoff 	zone->uz_keg = keg;
2937e20a199fSJeff Roberson 	zone->uz_size = keg->uk_size;
2938e20a199fSJeff Roberson 	zone->uz_flags |= (keg->uk_flags &
2939e20a199fSJeff Roberson 	    (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
29408355f576SJeff Roberson 
294120a4e154SJeff Roberson out:
2942dc2b3205SMark Johnston 	if (booted >= BOOT_PCPU) {
294320a4e154SJeff Roberson 		zone_alloc_counters(zone, NULL);
2944dc2b3205SMark Johnston 		if (booted >= BOOT_RUNNING)
294520a4e154SJeff Roberson 			zone_alloc_sysctl(zone, NULL);
294620a4e154SJeff Roberson 	} else {
294720a4e154SJeff Roberson 		zone->uz_allocs = EARLY_COUNTER;
294820a4e154SJeff Roberson 		zone->uz_frees = EARLY_COUNTER;
294920a4e154SJeff Roberson 		zone->uz_fails = EARLY_COUNTER;
2950099a0e58SBosko Milekic 	}
29518355f576SJeff Roberson 
2952d4665eaaSJeff Roberson 	/* Caller requests a private SMR context. */
2953d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
2954226dd6dbSJeff Roberson 		zone->uz_smr = smr_create(zone->uz_name, 0, 0);
2955d4665eaaSJeff Roberson 
29567e28037aSMark Johnston 	KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) !=
29577e28037aSMark Johnston 	    (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET),
29587e28037aSMark Johnston 	    ("Invalid zone flag combination"));
295920a4e154SJeff Roberson 	if (arg->flags & UMA_ZFLAG_INTERNAL)
296020a4e154SJeff Roberson 		zone->uz_bucket_size_max = zone->uz_bucket_size = 0;
296120a4e154SJeff Roberson 	if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0)
296220a4e154SJeff Roberson 		zone->uz_bucket_size = BUCKET_MAX;
296320a4e154SJeff Roberson 	else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0)
296420a4e154SJeff Roberson 		zone->uz_bucket_size = 0;
29657e28037aSMark Johnston 	else
296620a4e154SJeff Roberson 		zone->uz_bucket_size = bucket_select(zone->uz_size);
296720a4e154SJeff Roberson 	zone->uz_bucket_size_min = zone->uz_bucket_size;
2968cc7ce83aSJeff Roberson 	if (zone->uz_dtor != NULL || zone->uz_ctor != NULL)
2969cc7ce83aSJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_CTORDTOR;
2970cc7ce83aSJeff Roberson 	zone_update_caches(zone);
2971fc03d22bSJeff Roberson 
2972b23f72e9SBrian Feldman 	return (0);
29738355f576SJeff Roberson }
29748355f576SJeff Roberson 
29758355f576SJeff Roberson /*
2976099a0e58SBosko Milekic  * Keg header dtor.  This frees all data, destroys locks, frees the hash
2977099a0e58SBosko Milekic  * table and removes the keg from the global list.
29789c2cd7e5SJeff Roberson  *
29799c2cd7e5SJeff Roberson  * Arguments/Returns follow uma_dtor specifications
29809c2cd7e5SJeff Roberson  *	udata  unused
29819c2cd7e5SJeff Roberson  */
2982099a0e58SBosko Milekic static void
2983099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata)
2984099a0e58SBosko Milekic {
2985099a0e58SBosko Milekic 	uma_keg_t keg;
29868b987a77SJeff Roberson 	uint32_t free, pages;
29878b987a77SJeff Roberson 	int i;
29889c2cd7e5SJeff Roberson 
2989099a0e58SBosko Milekic 	keg = (uma_keg_t)arg;
29908b987a77SJeff Roberson 	free = pages = 0;
29918b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
29924ab3aee8SMark Johnston 		free += keg->uk_domain[i].ud_free_items;
29938b987a77SJeff Roberson 		pages += keg->uk_domain[i].ud_pages;
29948b987a77SJeff Roberson 		KEG_LOCK_FINI(keg, i);
2995099a0e58SBosko Milekic 	}
29967e240677SRyan Libby 	if (pages != 0)
29978b987a77SJeff Roberson 		printf("Freed UMA keg (%s) was not empty (%u items). "
29988b987a77SJeff Roberson 		    " Lost %u pages of memory.\n",
29998b987a77SJeff Roberson 		    keg->uk_name ? keg->uk_name : "",
30007e240677SRyan Libby 		    pages / keg->uk_ppera * keg->uk_ipers - free, pages);
3001099a0e58SBosko Milekic 
3002099a0e58SBosko Milekic 	hash_free(&keg->uk_hash);
3003099a0e58SBosko Milekic }
3004099a0e58SBosko Milekic 
3005099a0e58SBosko Milekic /*
3006099a0e58SBosko Milekic  * Zone header dtor.
3007099a0e58SBosko Milekic  *
3008099a0e58SBosko Milekic  * Arguments/Returns follow uma_dtor specifications
3009099a0e58SBosko Milekic  *	udata  unused
3010099a0e58SBosko Milekic  */
30119c2cd7e5SJeff Roberson static void
30129c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata)
30139c2cd7e5SJeff Roberson {
30149c2cd7e5SJeff Roberson 	uma_zone_t zone;
3015099a0e58SBosko Milekic 	uma_keg_t keg;
3016c6fd3e23SJeff Roberson 	int i;
30179c2cd7e5SJeff Roberson 
30189c2cd7e5SJeff Roberson 	zone = (uma_zone_t)arg;
30199643769aSJeff Roberson 
302020a4e154SJeff Roberson 	sysctl_remove_oid(zone->uz_oid, 1, 1);
302120a4e154SJeff Roberson 
3022e20a199fSJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
30239643769aSJeff Roberson 		cache_drain(zone);
3024099a0e58SBosko Milekic 
3025111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
3026099a0e58SBosko Milekic 	LIST_REMOVE(zone, uz_link);
3027111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
30287b516613SJonathan T. Looney 	if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) {
30297b516613SJonathan T. Looney 		keg = zone->uz_keg;
30307b516613SJonathan T. Looney 		keg->uk_reserve = 0;
30317b516613SJonathan T. Looney 	}
3032aabe13f1SMark Johnston 	zone_reclaim(zone, UMA_ANYDOMAIN, M_WAITOK, true);
3033c6fd3e23SJeff Roberson 
3034e20a199fSJeff Roberson 	/*
3035323ad386STycho Nightingale 	 * We only destroy kegs from non secondary/non cache zones.
3036e20a199fSJeff Roberson 	 */
3037323ad386STycho Nightingale 	if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) {
3038323ad386STycho Nightingale 		keg = zone->uz_keg;
3039111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
3040099a0e58SBosko Milekic 		LIST_REMOVE(keg, uk_link);
3041111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
30420095a784SJeff Roberson 		zone_free_item(kegs, keg, NULL, SKIP_NONE);
30439c2cd7e5SJeff Roberson 	}
30442efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_allocs);
30452efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_frees);
30462efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_fails);
3047c6fd3e23SJeff Roberson 	counter_u64_free(zone->uz_xdomain);
304820a4e154SJeff Roberson 	free(zone->uz_ctlname, M_UMA);
3049c6fd3e23SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
3050c6fd3e23SJeff Roberson 		ZDOM_LOCK_FINI(ZDOM_GET(zone, i));
305191d947bfSJeff Roberson 	ZONE_CROSS_LOCK_FINI(zone);
3052099a0e58SBosko Milekic }
3053099a0e58SBosko Milekic 
3054a81c400eSJeff Roberson static void
3055a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg)
3056a81c400eSJeff Roberson {
3057a81c400eSJeff Roberson 	uma_keg_t keg;
3058a81c400eSJeff Roberson 	uma_zone_t zone;
3059a81c400eSJeff Roberson 
3060a81c400eSJeff Roberson 	LIST_FOREACH(keg, &uma_kegs, uk_link) {
3061a81c400eSJeff Roberson 		LIST_FOREACH(zone, &keg->uk_zones, uz_link)
3062a81c400eSJeff Roberson 			zfunc(zone, arg);
3063a81c400eSJeff Roberson 	}
3064a81c400eSJeff Roberson 	LIST_FOREACH(zone, &uma_cachezones, uz_link)
3065a81c400eSJeff Roberson 		zfunc(zone, arg);
3066a81c400eSJeff Roberson }
3067a81c400eSJeff Roberson 
30689c2cd7e5SJeff Roberson /*
30698355f576SJeff Roberson  * Traverses every zone in the system and calls a callback
30708355f576SJeff Roberson  *
30718355f576SJeff Roberson  * Arguments:
30728355f576SJeff Roberson  *	zfunc  A pointer to a function which accepts a zone
30738355f576SJeff Roberson  *		as an argument.
30748355f576SJeff Roberson  *
30758355f576SJeff Roberson  * Returns:
30768355f576SJeff Roberson  *	Nothing
30778355f576SJeff Roberson  */
30788355f576SJeff Roberson static void
307920a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg)
30808355f576SJeff Roberson {
30818355f576SJeff Roberson 
3082111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
3083a81c400eSJeff Roberson 	zone_foreach_unlocked(zfunc, arg);
3084111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
30858355f576SJeff Roberson }
30868355f576SJeff Roberson 
3087f4bef67cSGleb Smirnoff /*
3088a81c400eSJeff Roberson  * Initialize the kernel memory allocator.  This is done after pages can be
3089a81c400eSJeff Roberson  * allocated but before general KVA is available.
3090f4bef67cSGleb Smirnoff  */
3091a81c400eSJeff Roberson void
3092a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail)
3093f4bef67cSGleb Smirnoff {
3094a81c400eSJeff Roberson 	struct uma_zctor_args args;
3095a81c400eSJeff Roberson 	size_t ksize, zsize, size;
3096c8b0a88bSJeff Roberson 	uma_keg_t primarykeg;
3097a81c400eSJeff Roberson 	uintptr_t m;
309881302f1dSMark Johnston 	int domain;
3099a81c400eSJeff Roberson 	uint8_t pflag;
3100a81c400eSJeff Roberson 
3101a81c400eSJeff Roberson 	bootstart = bootmem = virtual_avail;
3102a81c400eSJeff Roberson 
3103a81c400eSJeff Roberson 	rw_init(&uma_rwlock, "UMA lock");
3104a81c400eSJeff Roberson 	sx_init(&uma_reclaim_lock, "umareclaim");
3105f4bef67cSGleb Smirnoff 
3106f4bef67cSGleb Smirnoff 	ksize = sizeof(struct uma_keg) +
3107f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_domain) * vm_ndomains);
310879c9f942SJeff Roberson 	ksize = roundup(ksize, UMA_SUPER_ALIGN);
3109f4bef67cSGleb Smirnoff 	zsize = sizeof(struct uma_zone) +
3110f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_cache) * (mp_maxid + 1)) +
3111f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_zone_domain) * vm_ndomains);
311279c9f942SJeff Roberson 	zsize = roundup(zsize, UMA_SUPER_ALIGN);
3113f4bef67cSGleb Smirnoff 
3114a81c400eSJeff Roberson 	/* Allocate the zone of zones, zone of kegs, and zone of zones keg. */
3115a81c400eSJeff Roberson 	size = (zsize * 2) + ksize;
311681302f1dSMark Johnston 	for (domain = 0; domain < vm_ndomains; domain++) {
311781302f1dSMark Johnston 		m = (uintptr_t)startup_alloc(NULL, size, domain, &pflag,
311881302f1dSMark Johnston 		    M_NOWAIT | M_ZERO);
311981302f1dSMark Johnston 		if (m != 0)
312081302f1dSMark Johnston 			break;
312181302f1dSMark Johnston 	}
3122ab3185d1SJeff Roberson 	zones = (uma_zone_t)m;
312379c9f942SJeff Roberson 	m += zsize;
3124ab3185d1SJeff Roberson 	kegs = (uma_zone_t)m;
312579c9f942SJeff Roberson 	m += zsize;
3126c8b0a88bSJeff Roberson 	primarykeg = (uma_keg_t)m;
3127ab3185d1SJeff Roberson 
3128099a0e58SBosko Milekic 	/* "manually" create the initial zone */
31290095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
3130099a0e58SBosko Milekic 	args.name = "UMA Kegs";
3131ab3185d1SJeff Roberson 	args.size = ksize;
3132099a0e58SBosko Milekic 	args.ctor = keg_ctor;
3133099a0e58SBosko Milekic 	args.dtor = keg_dtor;
31348355f576SJeff Roberson 	args.uminit = zero_init;
31358355f576SJeff Roberson 	args.fini = NULL;
3136c8b0a88bSJeff Roberson 	args.keg = primarykeg;
313779c9f942SJeff Roberson 	args.align = UMA_SUPER_ALIGN - 1;
3138b60f5b79SJeff Roberson 	args.flags = UMA_ZFLAG_INTERNAL;
3139ab3185d1SJeff Roberson 	zone_ctor(kegs, zsize, &args, M_WAITOK);
31408355f576SJeff Roberson 
3141099a0e58SBosko Milekic 	args.name = "UMA Zones";
3142f4bef67cSGleb Smirnoff 	args.size = zsize;
3143099a0e58SBosko Milekic 	args.ctor = zone_ctor;
3144099a0e58SBosko Milekic 	args.dtor = zone_dtor;
3145099a0e58SBosko Milekic 	args.uminit = zero_init;
3146099a0e58SBosko Milekic 	args.fini = NULL;
3147099a0e58SBosko Milekic 	args.keg = NULL;
314879c9f942SJeff Roberson 	args.align = UMA_SUPER_ALIGN - 1;
3149099a0e58SBosko Milekic 	args.flags = UMA_ZFLAG_INTERNAL;
3150ab3185d1SJeff Roberson 	zone_ctor(zones, zsize, &args, M_WAITOK);
3151099a0e58SBosko Milekic 
31529b8db4d0SRyan Libby 	/* Now make zones for slab headers */
31539b8db4d0SRyan Libby 	slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE,
31549b8db4d0SRyan Libby 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
31559b8db4d0SRyan Libby 	slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE,
31561e0701e1SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
31578355f576SJeff Roberson 
31588355f576SJeff Roberson 	hashzone = uma_zcreate("UMA Hash",
31598355f576SJeff Roberson 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
31601e0701e1SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
31618355f576SJeff Roberson 
3162a81c400eSJeff Roberson 	bucket_init();
3163d4665eaaSJeff Roberson 	smr_init();
31648355f576SJeff Roberson }
31658355f576SJeff Roberson 
3166a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC
3167a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void);
3168f4bef67cSGleb Smirnoff #endif
3169f4bef67cSGleb Smirnoff 
3170a81c400eSJeff Roberson /*
3171a81c400eSJeff Roberson  * Advertise the availability of normal kva allocations and switch to
3172a81c400eSJeff Roberson  * the default back-end allocator.  Marks the KVA we consumed on startup
3173a81c400eSJeff Roberson  * as used in the map.
3174a81c400eSJeff Roberson  */
31758355f576SJeff Roberson void
317699571dc3SJeff Roberson uma_startup2(void)
31778355f576SJeff Roberson {
3178f4bef67cSGleb Smirnoff 
3179530cc6a2SJeff Roberson 	if (bootstart != bootmem) {
3180a81c400eSJeff Roberson 		vm_map_lock(kernel_map);
3181a81c400eSJeff Roberson 		(void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem,
3182a81c400eSJeff Roberson 		    VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
3183a81c400eSJeff Roberson 		vm_map_unlock(kernel_map);
3184a81c400eSJeff Roberson 	}
3185a81c400eSJeff Roberson 
3186a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC
3187a81c400eSJeff Roberson 	/* Set up radix zone to use noobj_alloc. */
3188a81c400eSJeff Roberson 	vm_radix_reserve_kva();
3189f7d35785SGleb Smirnoff #endif
3190a81c400eSJeff Roberson 
3191a81c400eSJeff Roberson 	booted = BOOT_KVA;
3192a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_kva_available, NULL);
3193f4bef67cSGleb Smirnoff 	bucket_enable();
31948355f576SJeff Roberson }
31958355f576SJeff Roberson 
3196a81c400eSJeff Roberson /*
3197dc2b3205SMark Johnston  * Allocate counters as early as possible so that boot-time allocations are
3198dc2b3205SMark Johnston  * accounted more precisely.
3199dc2b3205SMark Johnston  */
3200dc2b3205SMark Johnston static void
3201dc2b3205SMark Johnston uma_startup_pcpu(void *arg __unused)
3202dc2b3205SMark Johnston {
3203dc2b3205SMark Johnston 
3204dc2b3205SMark Johnston 	zone_foreach_unlocked(zone_alloc_counters, NULL);
3205dc2b3205SMark Johnston 	booted = BOOT_PCPU;
3206dc2b3205SMark Johnston }
3207dc2b3205SMark Johnston SYSINIT(uma_startup_pcpu, SI_SUB_COUNTER, SI_ORDER_ANY, uma_startup_pcpu, NULL);
3208dc2b3205SMark Johnston 
3209dc2b3205SMark Johnston /*
3210a81c400eSJeff Roberson  * Finish our initialization steps.
3211a81c400eSJeff Roberson  */
32128355f576SJeff Roberson static void
3213dc2b3205SMark Johnston uma_startup3(void *arg __unused)
32148355f576SJeff Roberson {
32151431a748SGleb Smirnoff 
3216c5deaf04SGleb Smirnoff #ifdef INVARIANTS
3217c5deaf04SGleb Smirnoff 	TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor);
3218c5deaf04SGleb Smirnoff 	uma_dbg_cnt = counter_u64_alloc(M_WAITOK);
3219c5deaf04SGleb Smirnoff 	uma_skip_cnt = counter_u64_alloc(M_WAITOK);
3220c5deaf04SGleb Smirnoff #endif
3221a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_alloc_sysctl, NULL);
3222fd90e2edSJung-uk Kim 	callout_init(&uma_callout, 1);
32239643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
3224c5deaf04SGleb Smirnoff 	booted = BOOT_RUNNING;
3225860bb7a0SMark Johnston 
3226860bb7a0SMark Johnston 	EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL,
3227860bb7a0SMark Johnston 	    EVENTHANDLER_PRI_FIRST);
3228860bb7a0SMark Johnston }
3229dc2b3205SMark Johnston SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
3230860bb7a0SMark Johnston 
3231860bb7a0SMark Johnston static void
3232860bb7a0SMark Johnston uma_shutdown(void)
3233860bb7a0SMark Johnston {
3234860bb7a0SMark Johnston 
3235860bb7a0SMark Johnston 	booted = BOOT_SHUTDOWN;
32368355f576SJeff Roberson }
32378355f576SJeff Roberson 
3238e20a199fSJeff Roberson static uma_keg_t
3239099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
324085dcf349SGleb Smirnoff 		int align, uint32_t flags)
3241099a0e58SBosko Milekic {
3242099a0e58SBosko Milekic 	struct uma_kctor_args args;
3243099a0e58SBosko Milekic 
3244099a0e58SBosko Milekic 	args.size = size;
3245099a0e58SBosko Milekic 	args.uminit = uminit;
3246099a0e58SBosko Milekic 	args.fini = fini;
32471e319f6dSRobert Watson 	args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
3248099a0e58SBosko Milekic 	args.flags = flags;
3249099a0e58SBosko Milekic 	args.zone = zone;
3250ab3185d1SJeff Roberson 	return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK));
3251099a0e58SBosko Milekic }
3252099a0e58SBosko Milekic 
3253f4bef67cSGleb Smirnoff /* Public functions */
32548355f576SJeff Roberson /* See uma.h */
32551e319f6dSRobert Watson void
32561e319f6dSRobert Watson uma_set_align(int align)
32571e319f6dSRobert Watson {
32581e319f6dSRobert Watson 
32591e319f6dSRobert Watson 	if (align != UMA_ALIGN_CACHE)
32601e319f6dSRobert Watson 		uma_align_cache = align;
32611e319f6dSRobert Watson }
32621e319f6dSRobert Watson 
32631e319f6dSRobert Watson /* See uma.h */
32648355f576SJeff Roberson uma_zone_t
3265bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
326685dcf349SGleb Smirnoff 		uma_init uminit, uma_fini fini, int align, uint32_t flags)
32678355f576SJeff Roberson 
32688355f576SJeff Roberson {
32698355f576SJeff Roberson 	struct uma_zctor_args args;
327095c4bf75SKonstantin Belousov 	uma_zone_t res;
32718355f576SJeff Roberson 
3272a5a35578SJohn Baldwin 	KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
3273a5a35578SJohn Baldwin 	    align, name));
3274a5a35578SJohn Baldwin 
32758355f576SJeff Roberson 	/* This stuff is essential for the zone ctor */
32760095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
32778355f576SJeff Roberson 	args.name = name;
32788355f576SJeff Roberson 	args.size = size;
32798355f576SJeff Roberson 	args.ctor = ctor;
32808355f576SJeff Roberson 	args.dtor = dtor;
32818355f576SJeff Roberson 	args.uminit = uminit;
32828355f576SJeff Roberson 	args.fini = fini;
328310094910SMark Johnston #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN)
3284afc6dc36SJohn-Mark Gurney 	/*
3285ca293436SRyan Libby 	 * Inject procedures which check for memory use after free if we are
3286ca293436SRyan Libby 	 * allowed to scramble the memory while it is not allocated.  This
3287ca293436SRyan Libby 	 * requires that: UMA is actually able to access the memory, no init
3288ca293436SRyan Libby 	 * or fini procedures, no dependency on the initial value of the
3289ca293436SRyan Libby 	 * memory, and no (legitimate) use of the memory after free.  Note,
3290ca293436SRyan Libby 	 * the ctor and dtor do not need to be empty.
3291afc6dc36SJohn-Mark Gurney 	 */
329254c5ae80SRyan Libby 	if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH |
329354c5ae80SRyan Libby 	    UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) {
3294afc6dc36SJohn-Mark Gurney 		args.uminit = trash_init;
3295afc6dc36SJohn-Mark Gurney 		args.fini = trash_fini;
3296afc6dc36SJohn-Mark Gurney 	}
3297afc6dc36SJohn-Mark Gurney #endif
32988355f576SJeff Roberson 	args.align = align;
32998355f576SJeff Roberson 	args.flags = flags;
3300099a0e58SBosko Milekic 	args.keg = NULL;
3301099a0e58SBosko Milekic 
3302aabe13f1SMark Johnston 	sx_xlock(&uma_reclaim_lock);
3303ab3185d1SJeff Roberson 	res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
3304aabe13f1SMark Johnston 	sx_xunlock(&uma_reclaim_lock);
3305a81c400eSJeff Roberson 
330695c4bf75SKonstantin Belousov 	return (res);
3307099a0e58SBosko Milekic }
3308099a0e58SBosko Milekic 
3309099a0e58SBosko Milekic /* See uma.h */
3310099a0e58SBosko Milekic uma_zone_t
33110464f16eSMark Johnston uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor,
3312c8b0a88bSJeff Roberson     uma_init zinit, uma_fini zfini, uma_zone_t primary)
3313099a0e58SBosko Milekic {
3314099a0e58SBosko Milekic 	struct uma_zctor_args args;
3315e20a199fSJeff Roberson 	uma_keg_t keg;
331695c4bf75SKonstantin Belousov 	uma_zone_t res;
3317099a0e58SBosko Milekic 
3318c8b0a88bSJeff Roberson 	keg = primary->uz_keg;
33190095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
3320099a0e58SBosko Milekic 	args.name = name;
3321e20a199fSJeff Roberson 	args.size = keg->uk_size;
3322099a0e58SBosko Milekic 	args.ctor = ctor;
3323099a0e58SBosko Milekic 	args.dtor = dtor;
3324099a0e58SBosko Milekic 	args.uminit = zinit;
3325099a0e58SBosko Milekic 	args.fini = zfini;
3326e20a199fSJeff Roberson 	args.align = keg->uk_align;
3327e20a199fSJeff Roberson 	args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
3328e20a199fSJeff Roberson 	args.keg = keg;
33298355f576SJeff Roberson 
3330aabe13f1SMark Johnston 	sx_xlock(&uma_reclaim_lock);
3331ab3185d1SJeff Roberson 	res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
3332aabe13f1SMark Johnston 	sx_xunlock(&uma_reclaim_lock);
3333a81c400eSJeff Roberson 
333495c4bf75SKonstantin Belousov 	return (res);
33358355f576SJeff Roberson }
33368355f576SJeff Roberson 
33370095a784SJeff Roberson /* See uma.h */
33380095a784SJeff Roberson uma_zone_t
33390464f16eSMark Johnston uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor,
33400464f16eSMark Johnston     uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease,
33410464f16eSMark Johnston     void *arg, int flags)
33420095a784SJeff Roberson {
33430095a784SJeff Roberson 	struct uma_zctor_args args;
33440095a784SJeff Roberson 
33450095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
33460095a784SJeff Roberson 	args.name = name;
3347af526374SJeff Roberson 	args.size = size;
33480095a784SJeff Roberson 	args.ctor = ctor;
33490095a784SJeff Roberson 	args.dtor = dtor;
33500095a784SJeff Roberson 	args.uminit = zinit;
33510095a784SJeff Roberson 	args.fini = zfini;
33520095a784SJeff Roberson 	args.import = zimport;
33530095a784SJeff Roberson 	args.release = zrelease;
33540095a784SJeff Roberson 	args.arg = arg;
33550095a784SJeff Roberson 	args.align = 0;
3356bb15d1c7SGleb Smirnoff 	args.flags = flags | UMA_ZFLAG_CACHE;
33570095a784SJeff Roberson 
3358ab3185d1SJeff Roberson 	return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK));
33590095a784SJeff Roberson }
33600095a784SJeff Roberson 
33618355f576SJeff Roberson /* See uma.h */
33629c2cd7e5SJeff Roberson void
33639c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone)
33649c2cd7e5SJeff Roberson {
3365f4ff923bSRobert Watson 
3366860bb7a0SMark Johnston 	/*
3367860bb7a0SMark Johnston 	 * Large slabs are expensive to reclaim, so don't bother doing
3368860bb7a0SMark Johnston 	 * unnecessary work if we're shutting down.
3369860bb7a0SMark Johnston 	 */
3370860bb7a0SMark Johnston 	if (booted == BOOT_SHUTDOWN &&
3371860bb7a0SMark Johnston 	    zone->uz_fini == NULL && zone->uz_release == zone_release)
3372860bb7a0SMark Johnston 		return;
3373aabe13f1SMark Johnston 	sx_xlock(&uma_reclaim_lock);
33740095a784SJeff Roberson 	zone_free_item(zones, zone, NULL, SKIP_NONE);
3375aabe13f1SMark Johnston 	sx_xunlock(&uma_reclaim_lock);
33769c2cd7e5SJeff Roberson }
33779c2cd7e5SJeff Roberson 
33788d6fbbb8SJeff Roberson void
33798d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone)
33808d6fbbb8SJeff Roberson {
33818d6fbbb8SJeff Roberson 
338270260874SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
338370260874SJeff Roberson 		uma_zfree_smr(zone, uma_zalloc_smr(zone, M_WAITOK));
338470260874SJeff Roberson 	else if ((zone->uz_flags & UMA_ZONE_PCPU) != 0)
338570260874SJeff Roberson 		uma_zfree_pcpu(zone, uma_zalloc_pcpu(zone, M_WAITOK));
338670260874SJeff Roberson 	else
338770260874SJeff Roberson 		uma_zfree(zone, uma_zalloc(zone, M_WAITOK));
33888d6fbbb8SJeff Roberson }
33898d6fbbb8SJeff Roberson 
33904e180881SMateusz Guzik void *
33914e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags)
33924e180881SMateusz Guzik {
33933acb6572SMateusz Guzik 	void *item, *pcpu_item;
3394b4799947SRuslan Bukin #ifdef SMP
33954e180881SMateusz Guzik 	int i;
33964e180881SMateusz Guzik 
33974e180881SMateusz Guzik 	MPASS(zone->uz_flags & UMA_ZONE_PCPU);
3398b4799947SRuslan Bukin #endif
33994e180881SMateusz Guzik 	item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO);
34003acb6572SMateusz Guzik 	if (item == NULL)
34013acb6572SMateusz Guzik 		return (NULL);
34023acb6572SMateusz Guzik 	pcpu_item = zpcpu_base_to_offset(item);
34033acb6572SMateusz Guzik 	if (flags & M_ZERO) {
3404b4799947SRuslan Bukin #ifdef SMP
3405013072f0SMark Johnston 		for (i = 0; i <= mp_maxid; i++)
34063acb6572SMateusz Guzik 			bzero(zpcpu_get_cpu(pcpu_item, i), zone->uz_size);
3407b4799947SRuslan Bukin #else
3408b4799947SRuslan Bukin 		bzero(item, zone->uz_size);
3409b4799947SRuslan Bukin #endif
34104e180881SMateusz Guzik 	}
34113acb6572SMateusz Guzik 	return (pcpu_item);
34124e180881SMateusz Guzik }
34134e180881SMateusz Guzik 
34144e180881SMateusz Guzik /*
34154e180881SMateusz Guzik  * A stub while both regular and pcpu cases are identical.
34164e180881SMateusz Guzik  */
34174e180881SMateusz Guzik void
34183acb6572SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *pcpu_item, void *udata)
34194e180881SMateusz Guzik {
34203acb6572SMateusz Guzik 	void *item;
34214e180881SMateusz Guzik 
3422c5b7751fSIan Lepore #ifdef SMP
34234e180881SMateusz Guzik 	MPASS(zone->uz_flags & UMA_ZONE_PCPU);
3424c5b7751fSIan Lepore #endif
3425b8f7267dSKristof Provost 
3426b8f7267dSKristof Provost         /* uma_zfree_pcu_*(..., NULL) does nothing, to match free(9). */
3427b8f7267dSKristof Provost         if (pcpu_item == NULL)
3428b8f7267dSKristof Provost                 return;
3429b8f7267dSKristof Provost 
34303acb6572SMateusz Guzik 	item = zpcpu_offset_to_base(pcpu_item);
34314e180881SMateusz Guzik 	uma_zfree_arg(zone, item, udata);
34324e180881SMateusz Guzik }
34334e180881SMateusz Guzik 
3434d4665eaaSJeff Roberson static inline void *
3435d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags,
3436d4665eaaSJeff Roberson     void *item)
3437beb8beefSJeff Roberson {
3438beb8beefSJeff Roberson #ifdef INVARIANTS
3439ca293436SRyan Libby 	bool skipdbg;
344009c8cb71SMark Johnston #endif
3441beb8beefSJeff Roberson 
344209c8cb71SMark Johnston 	kasan_mark_item_valid(zone, item);
344310094910SMark Johnston 	kmsan_mark_item_uninitialized(zone, item);
344409c8cb71SMark Johnston 
344509c8cb71SMark Johnston #ifdef INVARIANTS
3446beb8beefSJeff Roberson 	skipdbg = uma_dbg_zskip(zone, item);
344709c8cb71SMark Johnston 	if (!skipdbg && (uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3448ca293436SRyan Libby 	    zone->uz_ctor != trash_ctor)
3449cc7ce83aSJeff Roberson 		trash_ctor(item, size, udata, flags);
3450beb8beefSJeff Roberson #endif
345109c8cb71SMark Johnston 
3452d4665eaaSJeff Roberson 	/* Check flags before loading ctor pointer. */
3453d4665eaaSJeff Roberson 	if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) &&
3454d4665eaaSJeff Roberson 	    __predict_false(zone->uz_ctor != NULL) &&
3455cc7ce83aSJeff Roberson 	    zone->uz_ctor(item, size, udata, flags) != 0) {
3456beb8beefSJeff Roberson 		counter_u64_add(zone->uz_fails, 1);
3457beb8beefSJeff Roberson 		zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT);
3458beb8beefSJeff Roberson 		return (NULL);
3459beb8beefSJeff Roberson 	}
3460beb8beefSJeff Roberson #ifdef INVARIANTS
3461beb8beefSJeff Roberson 	if (!skipdbg)
3462beb8beefSJeff Roberson 		uma_dbg_alloc(zone, NULL, item);
3463beb8beefSJeff Roberson #endif
34646d88d784SJeff Roberson 	if (__predict_false(flags & M_ZERO))
34656d88d784SJeff Roberson 		return (memset(item, 0, size));
3466beb8beefSJeff Roberson 
3467beb8beefSJeff Roberson 	return (item);
3468beb8beefSJeff Roberson }
3469beb8beefSJeff Roberson 
3470ca293436SRyan Libby static inline void
3471cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata,
3472cc7ce83aSJeff Roberson     enum zfreeskip skip)
3473ca293436SRyan Libby {
3474ca293436SRyan Libby #ifdef INVARIANTS
3475ca293436SRyan Libby 	bool skipdbg;
3476ca293436SRyan Libby 
3477ca293436SRyan Libby 	skipdbg = uma_dbg_zskip(zone, item);
3478ca293436SRyan Libby 	if (skip == SKIP_NONE && !skipdbg) {
3479ca293436SRyan Libby 		if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0)
3480ca293436SRyan Libby 			uma_dbg_free(zone, udata, item);
3481ca293436SRyan Libby 		else
3482ca293436SRyan Libby 			uma_dbg_free(zone, NULL, item);
3483ca293436SRyan Libby 	}
3484ca293436SRyan Libby #endif
3485cc7ce83aSJeff Roberson 	if (__predict_true(skip < SKIP_DTOR)) {
3486ca293436SRyan Libby 		if (zone->uz_dtor != NULL)
3487cc7ce83aSJeff Roberson 			zone->uz_dtor(item, size, udata);
3488ca293436SRyan Libby #ifdef INVARIANTS
3489ca293436SRyan Libby 		if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3490ca293436SRyan Libby 		    zone->uz_dtor != trash_dtor)
3491cc7ce83aSJeff Roberson 			trash_dtor(item, size, udata);
3492ca293436SRyan Libby #endif
3493ca293436SRyan Libby 	}
349409c8cb71SMark Johnston 	kasan_mark_item_invalid(zone, item);
3495ca293436SRyan Libby }
3496ca293436SRyan Libby 
34971c58c09fSMateusz Guzik #ifdef NUMA
349881302f1dSMark Johnston static int
349981302f1dSMark Johnston item_domain(void *item)
350081302f1dSMark Johnston {
350181302f1dSMark Johnston 	int domain;
350281302f1dSMark Johnston 
3503431fb8abSMark Johnston 	domain = vm_phys_domain(vtophys(item));
350481302f1dSMark Johnston 	KASSERT(domain >= 0 && domain < vm_ndomains,
350581302f1dSMark Johnston 	    ("%s: unknown domain for item %p", __func__, item));
350681302f1dSMark Johnston 	return (domain);
350781302f1dSMark Johnston }
35081c58c09fSMateusz Guzik #endif
350981302f1dSMark Johnston 
3510d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS)
3511d4665eaaSJeff Roberson #define	UMA_ZALLOC_DEBUG
3512d4665eaaSJeff Roberson static int
3513d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags)
3514d4665eaaSJeff Roberson {
3515d4665eaaSJeff Roberson 	int error;
3516d4665eaaSJeff Roberson 
3517d4665eaaSJeff Roberson 	error = 0;
3518d4665eaaSJeff Roberson #ifdef WITNESS
3519d4665eaaSJeff Roberson 	if (flags & M_WAITOK) {
3520d4665eaaSJeff Roberson 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
3521d4665eaaSJeff Roberson 		    "uma_zalloc_debug: zone \"%s\"", zone->uz_name);
3522d4665eaaSJeff Roberson 	}
3523d4665eaaSJeff Roberson #endif
3524d4665eaaSJeff Roberson 
3525d4665eaaSJeff Roberson #ifdef INVARIANTS
3526d4665eaaSJeff Roberson 	KASSERT((flags & M_EXEC) == 0,
3527d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: called with M_EXEC"));
3528d4665eaaSJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3529d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: called within spinlock or critical section"));
3530d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0,
3531d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO"));
3532d4665eaaSJeff Roberson #endif
3533d4665eaaSJeff Roberson 
3534d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD
35359e47b341SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) {
3536d4665eaaSJeff Roberson 		void *item;
3537d4665eaaSJeff Roberson 		item = memguard_alloc(zone->uz_size, flags);
3538d4665eaaSJeff Roberson 		if (item != NULL) {
3539d4665eaaSJeff Roberson 			error = EJUSTRETURN;
3540d4665eaaSJeff Roberson 			if (zone->uz_init != NULL &&
3541d4665eaaSJeff Roberson 			    zone->uz_init(item, zone->uz_size, flags) != 0) {
3542d4665eaaSJeff Roberson 				*itemp = NULL;
3543d4665eaaSJeff Roberson 				return (error);
3544d4665eaaSJeff Roberson 			}
3545d4665eaaSJeff Roberson 			if (zone->uz_ctor != NULL &&
3546d4665eaaSJeff Roberson 			    zone->uz_ctor(item, zone->uz_size, udata,
3547d4665eaaSJeff Roberson 			    flags) != 0) {
3548d4665eaaSJeff Roberson 				counter_u64_add(zone->uz_fails, 1);
3549d4665eaaSJeff Roberson 			    	zone->uz_fini(item, zone->uz_size);
3550d4665eaaSJeff Roberson 				*itemp = NULL;
3551d4665eaaSJeff Roberson 				return (error);
3552d4665eaaSJeff Roberson 			}
3553d4665eaaSJeff Roberson 			*itemp = item;
3554d4665eaaSJeff Roberson 			return (error);
3555d4665eaaSJeff Roberson 		}
3556d4665eaaSJeff Roberson 		/* This is unfortunate but should not be fatal. */
3557d4665eaaSJeff Roberson 	}
3558d4665eaaSJeff Roberson #endif
3559d4665eaaSJeff Roberson 	return (error);
3560d4665eaaSJeff Roberson }
3561d4665eaaSJeff Roberson 
3562d4665eaaSJeff Roberson static int
3563d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata)
3564d4665eaaSJeff Roberson {
3565d4665eaaSJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3566d4665eaaSJeff Roberson 	    ("uma_zfree_debug: called with spinlock or critical section held"));
3567d4665eaaSJeff Roberson 
3568d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD
35699e47b341SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) {
3570d4665eaaSJeff Roberson 		if (zone->uz_dtor != NULL)
3571d4665eaaSJeff Roberson 			zone->uz_dtor(item, zone->uz_size, udata);
3572d4665eaaSJeff Roberson 		if (zone->uz_fini != NULL)
3573d4665eaaSJeff Roberson 			zone->uz_fini(item, zone->uz_size);
3574d4665eaaSJeff Roberson 		memguard_free(item);
3575d4665eaaSJeff Roberson 		return (EJUSTRETURN);
3576d4665eaaSJeff Roberson 	}
3577d4665eaaSJeff Roberson #endif
3578d4665eaaSJeff Roberson 	return (0);
3579d4665eaaSJeff Roberson }
3580d4665eaaSJeff Roberson #endif
3581d4665eaaSJeff Roberson 
35826d88d784SJeff Roberson static inline void *
35836d88d784SJeff Roberson cache_alloc_item(uma_zone_t zone, uma_cache_t cache, uma_cache_bucket_t bucket,
35846d88d784SJeff Roberson     void *udata, int flags)
3585d4665eaaSJeff Roberson {
35866d88d784SJeff Roberson 	void *item;
35876d88d784SJeff Roberson 	int size, uz_flags;
35886d88d784SJeff Roberson 
35896d88d784SJeff Roberson 	item = cache_bucket_pop(cache, bucket);
35906d88d784SJeff Roberson 	size = cache_uz_size(cache);
35916d88d784SJeff Roberson 	uz_flags = cache_uz_flags(cache);
35926d88d784SJeff Roberson 	critical_exit();
35936d88d784SJeff Roberson 	return (item_ctor(zone, uz_flags, size, udata, flags, item));
35946d88d784SJeff Roberson }
35956d88d784SJeff Roberson 
35966d88d784SJeff Roberson static __noinline void *
35976d88d784SJeff Roberson cache_alloc_retry(uma_zone_t zone, uma_cache_t cache, void *udata, int flags)
35986d88d784SJeff Roberson {
35996d88d784SJeff Roberson 	uma_cache_bucket_t bucket;
3600d4665eaaSJeff Roberson 	int domain;
3601d4665eaaSJeff Roberson 
36026d88d784SJeff Roberson 	while (cache_alloc(zone, cache, udata, flags)) {
36036d88d784SJeff Roberson 		cache = &zone->uz_cpu[curcpu];
36046d88d784SJeff Roberson 		bucket = &cache->uc_allocbucket;
36056d88d784SJeff Roberson 		if (__predict_false(bucket->ucb_cnt == 0))
36066d88d784SJeff Roberson 			continue;
36076d88d784SJeff Roberson 		return (cache_alloc_item(zone, cache, bucket, udata, flags));
36086d88d784SJeff Roberson 	}
36096d88d784SJeff Roberson 	critical_exit();
36106d88d784SJeff Roberson 
3611d4665eaaSJeff Roberson 	/*
3612d4665eaaSJeff Roberson 	 * We can not get a bucket so try to return a single item.
3613d4665eaaSJeff Roberson 	 */
3614d4665eaaSJeff Roberson 	if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH)
3615d4665eaaSJeff Roberson 		domain = PCPU_GET(domain);
3616d4665eaaSJeff Roberson 	else
3617d4665eaaSJeff Roberson 		domain = UMA_ANYDOMAIN;
3618d4665eaaSJeff Roberson 	return (zone_alloc_item(zone, udata, domain, flags));
3619d4665eaaSJeff Roberson }
3620d4665eaaSJeff Roberson 
3621d4665eaaSJeff Roberson /* See uma.h */
3622d4665eaaSJeff Roberson void *
3623d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags)
3624d4665eaaSJeff Roberson {
3625d4665eaaSJeff Roberson 	uma_cache_bucket_t bucket;
3626d4665eaaSJeff Roberson 	uma_cache_t cache;
3627d4665eaaSJeff Roberson 
3628*841e0a87SGleb Smirnoff 	CTR3(KTR_UMA, "uma_zalloc_smr zone %s(%p) flags %d", zone->uz_name,
3629*841e0a87SGleb Smirnoff 	    zone, flags);
3630*841e0a87SGleb Smirnoff 
3631d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
36326d88d784SJeff Roberson 	void *item;
36336d88d784SJeff Roberson 
3634d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
3635952c8964SMark Johnston 	    ("uma_zalloc_arg: called with non-SMR zone."));
3636d4665eaaSJeff Roberson 	if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN)
3637d4665eaaSJeff Roberson 		return (item);
3638d4665eaaSJeff Roberson #endif
3639d4665eaaSJeff Roberson 
3640d4665eaaSJeff Roberson 	critical_enter();
3641d4665eaaSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3642d4665eaaSJeff Roberson 	bucket = &cache->uc_allocbucket;
36436d88d784SJeff Roberson 	if (__predict_false(bucket->ucb_cnt == 0))
36446d88d784SJeff Roberson 		return (cache_alloc_retry(zone, cache, NULL, flags));
36456d88d784SJeff Roberson 	return (cache_alloc_item(zone, cache, bucket, NULL, flags));
3646d4665eaaSJeff Roberson }
3647d4665eaaSJeff Roberson 
36489c2cd7e5SJeff Roberson /* See uma.h */
36498355f576SJeff Roberson void *
36502cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
36518355f576SJeff Roberson {
3652376b1ba3SJeff Roberson 	uma_cache_bucket_t bucket;
3653ab3185d1SJeff Roberson 	uma_cache_t cache;
36548355f576SJeff Roberson 
3655e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
365619fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
365710cb2424SMark Murray 
36588355f576SJeff Roberson 	/* This is the fast path allocation */
3659e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name,
3660e63a1c2fSRyan Libby 	    zone, flags);
3661a553d4b8SJeff Roberson 
3662d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
36636d88d784SJeff Roberson 	void *item;
36646d88d784SJeff Roberson 
3665d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
3666952c8964SMark Johnston 	    ("uma_zalloc_arg: called with SMR zone."));
3667d4665eaaSJeff Roberson 	if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN)
36688d689e04SGleb Smirnoff 		return (item);
36698d689e04SGleb Smirnoff #endif
3670d4665eaaSJeff Roberson 
36715d1ae027SRobert Watson 	/*
36725d1ae027SRobert Watson 	 * If possible, allocate from the per-CPU cache.  There are two
36735d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
36745d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
36755d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
36765d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
36775d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
36785d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to allocate from
36795d1ae027SRobert Watson 	 * the current cache; when we re-acquire the critical section, we
36805d1ae027SRobert Watson 	 * must detect and handle migration if it has occurred.
36815d1ae027SRobert Watson 	 */
36825d1ae027SRobert Watson 	critical_enter();
3683cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3684376b1ba3SJeff Roberson 	bucket = &cache->uc_allocbucket;
36856d88d784SJeff Roberson 	if (__predict_false(bucket->ucb_cnt == 0))
36866d88d784SJeff Roberson 		return (cache_alloc_retry(zone, cache, udata, flags));
36876d88d784SJeff Roberson 	return (cache_alloc_item(zone, cache, bucket, udata, flags));
3688fc03d22bSJeff Roberson }
3689fc03d22bSJeff Roberson 
36908355f576SJeff Roberson /*
3691beb8beefSJeff Roberson  * Replenish an alloc bucket and possibly restore an old one.  Called in
3692beb8beefSJeff Roberson  * a critical section.  Returns in a critical section.
3693beb8beefSJeff Roberson  *
36944bd61e19SJeff Roberson  * A false return value indicates an allocation failure.
36954bd61e19SJeff Roberson  * A true return value indicates success and the caller should retry.
3696beb8beefSJeff Roberson  */
3697beb8beefSJeff Roberson static __noinline bool
3698beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags)
3699beb8beefSJeff Roberson {
3700beb8beefSJeff Roberson 	uma_bucket_t bucket;
37018c277118SMark Johnston 	int curdomain, domain;
3702c6fd3e23SJeff Roberson 	bool new;
3703beb8beefSJeff Roberson 
3704beb8beefSJeff Roberson 	CRITICAL_ASSERT(curthread);
3705beb8beefSJeff Roberson 
3706beb8beefSJeff Roberson 	/*
3707beb8beefSJeff Roberson 	 * If we have run out of items in our alloc bucket see
3708beb8beefSJeff Roberson 	 * if we can switch with the free bucket.
3709d4665eaaSJeff Roberson 	 *
3710d4665eaaSJeff Roberson 	 * SMR Zones can't re-use the free bucket until the sequence has
3711d4665eaaSJeff Roberson 	 * expired.
37128355f576SJeff Roberson 	 */
3713c6fd3e23SJeff Roberson 	if ((cache_uz_flags(cache) & UMA_ZONE_SMR) == 0 &&
3714d4665eaaSJeff Roberson 	    cache->uc_freebucket.ucb_cnt != 0) {
3715d4665eaaSJeff Roberson 		cache_bucket_swap(&cache->uc_freebucket,
3716d4665eaaSJeff Roberson 		    &cache->uc_allocbucket);
3717beb8beefSJeff Roberson 		return (true);
37188355f576SJeff Roberson 	}
3719fc03d22bSJeff Roberson 
3720fc03d22bSJeff Roberson 	/*
3721fc03d22bSJeff Roberson 	 * Discard any empty allocation bucket while we hold no locks.
3722fc03d22bSJeff Roberson 	 */
3723376b1ba3SJeff Roberson 	bucket = cache_bucket_unload_alloc(cache);
3724fc03d22bSJeff Roberson 	critical_exit();
3725c6fd3e23SJeff Roberson 
3726c6fd3e23SJeff Roberson 	if (bucket != NULL) {
3727c6fd3e23SJeff Roberson 		KASSERT(bucket->ub_cnt == 0,
3728c6fd3e23SJeff Roberson 		    ("cache_alloc: Entered with non-empty alloc bucket."));
37296fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
3730c6fd3e23SJeff Roberson 	}
3731fc03d22bSJeff Roberson 
37325d1ae027SRobert Watson 	/*
37335d1ae027SRobert Watson 	 * Attempt to retrieve the item from the per-CPU cache has failed, so
3734c6fd3e23SJeff Roberson 	 * we must go back to the zone.  This requires the zdom lock, so we
37355d1ae027SRobert Watson 	 * must drop the critical section, then re-acquire it when we go back
37365d1ae027SRobert Watson 	 * to the cache.  Since the critical section is released, we may be
37375d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
37385d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
37395d1ae027SRobert Watson 	 * the critical section.
37405d1ae027SRobert Watson 	 */
3741c1685086SJeff Roberson 	domain = PCPU_GET(domain);
37428c277118SMark Johnston 	if ((cache_uz_flags(cache) & UMA_ZONE_ROUNDROBIN) != 0 ||
37438c277118SMark Johnston 	    VM_DOMAIN_EMPTY(domain))
3744c6fd3e23SJeff Roberson 		domain = zone_domain_highest(zone, domain);
3745c6fd3e23SJeff Roberson 	bucket = cache_fetch_bucket(zone, cache, domain);
3746af32cefdSMark Johnston 	if (bucket == NULL && zone->uz_bucket_size != 0 && !bucketdisable) {
3747beb8beefSJeff Roberson 		bucket = zone_alloc_bucket(zone, udata, domain, flags);
3748c6fd3e23SJeff Roberson 		new = true;
3749af32cefdSMark Johnston 	} else {
3750c6fd3e23SJeff Roberson 		new = false;
3751af32cefdSMark Johnston 	}
3752c6fd3e23SJeff Roberson 
37531431a748SGleb Smirnoff 	CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
37541431a748SGleb Smirnoff 	    zone->uz_name, zone, bucket);
37554bd61e19SJeff Roberson 	if (bucket == NULL) {
3756fc03d22bSJeff Roberson 		critical_enter();
3757beb8beefSJeff Roberson 		return (false);
37584bd61e19SJeff Roberson 	}
37590f9b7bf3SMark Johnston 
3760fc03d22bSJeff Roberson 	/*
3761fc03d22bSJeff Roberson 	 * See if we lost the race or were migrated.  Cache the
3762fc03d22bSJeff Roberson 	 * initialized bucket to make this less likely or claim
3763fc03d22bSJeff Roberson 	 * the memory directly.
3764fc03d22bSJeff Roberson 	 */
37654bd61e19SJeff Roberson 	critical_enter();
3766cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3767376b1ba3SJeff Roberson 	if (cache->uc_allocbucket.ucb_bucket == NULL &&
3768c6fd3e23SJeff Roberson 	    ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) == 0 ||
37698c277118SMark Johnston 	    (curdomain = PCPU_GET(domain)) == domain ||
37708c277118SMark Johnston 	    VM_DOMAIN_EMPTY(curdomain))) {
3771c6fd3e23SJeff Roberson 		if (new)
3772c6fd3e23SJeff Roberson 			atomic_add_long(&ZDOM_GET(zone, domain)->uzd_imax,
3773c6fd3e23SJeff Roberson 			    bucket->ub_cnt);
3774376b1ba3SJeff Roberson 		cache_bucket_load_alloc(cache, bucket);
3775beb8beefSJeff Roberson 		return (true);
3776c6fd3e23SJeff Roberson 	}
3777c6fd3e23SJeff Roberson 
3778c6fd3e23SJeff Roberson 	/*
3779c6fd3e23SJeff Roberson 	 * We lost the race, release this bucket and start over.
3780c6fd3e23SJeff Roberson 	 */
3781c6fd3e23SJeff Roberson 	critical_exit();
37822760658bSAlexander Motin 	zone_put_bucket(zone, domain, bucket, udata, !new);
3783c6fd3e23SJeff Roberson 	critical_enter();
3784c6fd3e23SJeff Roberson 
3785beb8beefSJeff Roberson 	return (true);
3786bbee39c6SJeff Roberson }
3787bbee39c6SJeff Roberson 
3788ab3185d1SJeff Roberson void *
3789ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
3790bbee39c6SJeff Roberson {
379106d8bdcbSMark Johnston #ifdef NUMA
379206d8bdcbSMark Johnston 	uma_bucket_t bucket;
379306d8bdcbSMark Johnston 	uma_zone_domain_t zdom;
379406d8bdcbSMark Johnston 	void *item;
379506d8bdcbSMark Johnston #endif
3796ab3185d1SJeff Roberson 
3797ab3185d1SJeff Roberson 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
379819fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3799ab3185d1SJeff Roberson 
3800ab3185d1SJeff Roberson 	/* This is the fast path allocation */
3801e63a1c2fSRyan Libby 	CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d",
3802e63a1c2fSRyan Libby 	    zone->uz_name, zone, domain, flags);
3803ab3185d1SJeff Roberson 
3804ab3185d1SJeff Roberson 	if (flags & M_WAITOK) {
3805ab3185d1SJeff Roberson 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
3806ab3185d1SJeff Roberson 		    "uma_zalloc_domain: zone \"%s\"", zone->uz_name);
3807ab3185d1SJeff Roberson 	}
3808ab3185d1SJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3809ab3185d1SJeff Roberson 	    ("uma_zalloc_domain: called with spinlock or critical section held"));
381006d8bdcbSMark Johnston 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
381106d8bdcbSMark Johnston 	    ("uma_zalloc_domain: called with SMR zone."));
381206d8bdcbSMark Johnston #ifdef NUMA
381306d8bdcbSMark Johnston 	KASSERT((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0,
381406d8bdcbSMark Johnston 	    ("uma_zalloc_domain: called with non-FIRSTTOUCH zone."));
3815ab3185d1SJeff Roberson 
381606d8bdcbSMark Johnston 	if (vm_ndomains == 1)
381706d8bdcbSMark Johnston 		return (uma_zalloc_arg(zone, udata, flags));
381806d8bdcbSMark Johnston 
381906d8bdcbSMark Johnston 	/*
382006d8bdcbSMark Johnston 	 * Try to allocate from the bucket cache before falling back to the keg.
382106d8bdcbSMark Johnston 	 * We could try harder and attempt to allocate from per-CPU caches or
382206d8bdcbSMark Johnston 	 * the per-domain cross-domain buckets, but the complexity is probably
382306d8bdcbSMark Johnston 	 * not worth it.  It is more important that frees of previous
382406d8bdcbSMark Johnston 	 * cross-domain allocations do not blow up the cache.
382506d8bdcbSMark Johnston 	 */
382606d8bdcbSMark Johnston 	zdom = zone_domain_lock(zone, domain);
382706d8bdcbSMark Johnston 	if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) {
382806d8bdcbSMark Johnston 		item = bucket->ub_bucket[bucket->ub_cnt - 1];
382906d8bdcbSMark Johnston #ifdef INVARIANTS
383006d8bdcbSMark Johnston 		bucket->ub_bucket[bucket->ub_cnt - 1] = NULL;
383106d8bdcbSMark Johnston #endif
383206d8bdcbSMark Johnston 		bucket->ub_cnt--;
383306d8bdcbSMark Johnston 		zone_put_bucket(zone, domain, bucket, udata, true);
383406d8bdcbSMark Johnston 		item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata,
383506d8bdcbSMark Johnston 		    flags, item);
383606d8bdcbSMark Johnston 		if (item != NULL) {
383706d8bdcbSMark Johnston 			KASSERT(item_domain(item) == domain,
383806d8bdcbSMark Johnston 			    ("%s: bucket cache item %p from wrong domain",
383906d8bdcbSMark Johnston 			    __func__, item));
384006d8bdcbSMark Johnston 			counter_u64_add(zone->uz_allocs, 1);
384106d8bdcbSMark Johnston 		}
384206d8bdcbSMark Johnston 		return (item);
384306d8bdcbSMark Johnston 	}
384406d8bdcbSMark Johnston 	ZDOM_UNLOCK(zdom);
3845ab3185d1SJeff Roberson 	return (zone_alloc_item(zone, udata, domain, flags));
384606d8bdcbSMark Johnston #else
384706d8bdcbSMark Johnston 	return (uma_zalloc_arg(zone, udata, flags));
384806d8bdcbSMark Johnston #endif
3849ab3185d1SJeff Roberson }
3850ab3185d1SJeff Roberson 
3851ab3185d1SJeff Roberson /*
3852ab3185d1SJeff Roberson  * Find a slab with some space.  Prefer slabs that are partially used over those
3853ab3185d1SJeff Roberson  * that are totally full.  This helps to reduce fragmentation.
3854ab3185d1SJeff Roberson  *
3855ab3185d1SJeff Roberson  * If 'rr' is 1, search all domains starting from 'domain'.  Otherwise check
3856ab3185d1SJeff Roberson  * only 'domain'.
3857ab3185d1SJeff Roberson  */
3858ab3185d1SJeff Roberson static uma_slab_t
3859194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr)
3860ab3185d1SJeff Roberson {
3861ab3185d1SJeff Roberson 	uma_domain_t dom;
3862bbee39c6SJeff Roberson 	uma_slab_t slab;
3863ab3185d1SJeff Roberson 	int start;
3864ab3185d1SJeff Roberson 
3865ab3185d1SJeff Roberson 	KASSERT(domain >= 0 && domain < vm_ndomains,
3866ab3185d1SJeff Roberson 	    ("keg_first_slab: domain %d out of range", domain));
38678b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, domain);
3868ab3185d1SJeff Roberson 
3869ab3185d1SJeff Roberson 	slab = NULL;
3870ab3185d1SJeff Roberson 	start = domain;
3871ab3185d1SJeff Roberson 	do {
3872ab3185d1SJeff Roberson 		dom = &keg->uk_domain[domain];
38734ab3aee8SMark Johnston 		if ((slab = LIST_FIRST(&dom->ud_part_slab)) != NULL)
38744ab3aee8SMark Johnston 			return (slab);
38754ab3aee8SMark Johnston 		if ((slab = LIST_FIRST(&dom->ud_free_slab)) != NULL) {
3876ab3185d1SJeff Roberson 			LIST_REMOVE(slab, us_link);
38774ab3aee8SMark Johnston 			dom->ud_free_slabs--;
3878ab3185d1SJeff Roberson 			LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
3879ab3185d1SJeff Roberson 			return (slab);
3880ab3185d1SJeff Roberson 		}
3881ab3185d1SJeff Roberson 		if (rr)
3882ab3185d1SJeff Roberson 			domain = (domain + 1) % vm_ndomains;
3883ab3185d1SJeff Roberson 	} while (domain != start);
3884ab3185d1SJeff Roberson 
3885ab3185d1SJeff Roberson 	return (NULL);
3886ab3185d1SJeff Roberson }
3887ab3185d1SJeff Roberson 
38888b987a77SJeff Roberson /*
38898b987a77SJeff Roberson  * Fetch an existing slab from a free or partial list.  Returns with the
38908b987a77SJeff Roberson  * keg domain lock held if a slab was found or unlocked if not.
38918b987a77SJeff Roberson  */
3892ab3185d1SJeff Roberson static uma_slab_t
3893194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags)
3894ab3185d1SJeff Roberson {
38958b987a77SJeff Roberson 	uma_slab_t slab;
3896194a979eSMark Johnston 	uint32_t reserve;
3897099a0e58SBosko Milekic 
38988b987a77SJeff Roberson 	/* HASH has a single free list. */
389954c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0)
39008b987a77SJeff Roberson 		domain = 0;
3901194a979eSMark Johnston 
39028b987a77SJeff Roberson 	KEG_LOCK(keg, domain);
3903194a979eSMark Johnston 	reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve;
39044ab3aee8SMark Johnston 	if (keg->uk_domain[domain].ud_free_items <= reserve ||
39058b987a77SJeff Roberson 	    (slab = keg_first_slab(keg, domain, rr)) == NULL) {
39068b987a77SJeff Roberson 		KEG_UNLOCK(keg, domain);
3907194a979eSMark Johnston 		return (NULL);
39088b987a77SJeff Roberson 	}
39098b987a77SJeff Roberson 	return (slab);
3910194a979eSMark Johnston }
3911194a979eSMark Johnston 
3912194a979eSMark Johnston static uma_slab_t
3913194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags)
3914194a979eSMark Johnston {
3915194a979eSMark Johnston 	struct vm_domainset_iter di;
3916194a979eSMark Johnston 	uma_slab_t slab;
3917194a979eSMark Johnston 	int aflags, domain;
3918194a979eSMark Johnston 	bool rr;
3919194a979eSMark Johnston 
3920fab343a7SMark Johnston 	KASSERT((flags & (M_WAITOK | M_NOVM)) != (M_WAITOK | M_NOVM),
3921fab343a7SMark Johnston 	    ("%s: invalid flags %#x", __func__, flags));
3922fab343a7SMark Johnston 
3923194a979eSMark Johnston restart:
3924bbee39c6SJeff Roberson 	/*
3925194a979eSMark Johnston 	 * Use the keg's policy if upper layers haven't already specified a
3926194a979eSMark Johnston 	 * domain (as happens with first-touch zones).
3927194a979eSMark Johnston 	 *
3928194a979eSMark Johnston 	 * To avoid races we run the iterator with the keg lock held, but that
3929194a979eSMark Johnston 	 * means that we cannot allow the vm_domainset layer to sleep.  Thus,
3930194a979eSMark Johnston 	 * clear M_WAITOK and handle low memory conditions locally.
3931bbee39c6SJeff Roberson 	 */
3932ab3185d1SJeff Roberson 	rr = rdomain == UMA_ANYDOMAIN;
3933ab3185d1SJeff Roberson 	if (rr) {
3934194a979eSMark Johnston 		aflags = (flags & ~M_WAITOK) | M_NOWAIT;
3935194a979eSMark Johnston 		vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
3936194a979eSMark Johnston 		    &aflags);
3937194a979eSMark Johnston 	} else {
3938194a979eSMark Johnston 		aflags = flags;
3939194a979eSMark Johnston 		domain = rdomain;
3940194a979eSMark Johnston 	}
3941ab3185d1SJeff Roberson 
3942194a979eSMark Johnston 	for (;;) {
3943194a979eSMark Johnston 		slab = keg_fetch_free_slab(keg, domain, rr, flags);
3944584061b4SJeff Roberson 		if (slab != NULL)
3945bbee39c6SJeff Roberson 			return (slab);
3946bbee39c6SJeff Roberson 
3947bbee39c6SJeff Roberson 		/*
3948fab343a7SMark Johnston 		 * M_NOVM is used to break the recursion that can otherwise
3949fab343a7SMark Johnston 		 * occur if low-level memory management routines use UMA.
3950bbee39c6SJeff Roberson 		 */
3951fab343a7SMark Johnston 		if ((flags & M_NOVM) == 0) {
395286220393SMark Johnston 			slab = keg_alloc_slab(keg, zone, domain, flags, aflags);
39538b987a77SJeff Roberson 			if (slab != NULL)
3954bbee39c6SJeff Roberson 				return (slab);
3955fab343a7SMark Johnston 		}
3956fab343a7SMark Johnston 
3957fab343a7SMark Johnston 		if (!rr) {
3958fab343a7SMark Johnston 			if ((flags & M_USE_RESERVE) != 0) {
3959fab343a7SMark Johnston 				/*
3960fab343a7SMark Johnston 				 * Drain reserves from other domains before
3961fab343a7SMark Johnston 				 * giving up or sleeping.  It may be useful to
3962fab343a7SMark Johnston 				 * support per-domain reserves eventually.
3963fab343a7SMark Johnston 				 */
3964fab343a7SMark Johnston 				rdomain = UMA_ANYDOMAIN;
3965fab343a7SMark Johnston 				goto restart;
3966fab343a7SMark Johnston 			}
3967fab343a7SMark Johnston 			if ((flags & M_WAITOK) == 0)
39683639ac42SJeff Roberson 				break;
3969fab343a7SMark Johnston 			vm_wait_domain(domain);
3970fab343a7SMark Johnston 		} else if (vm_domainset_iter_policy(&di, &domain) != 0) {
3971194a979eSMark Johnston 			if ((flags & M_WAITOK) != 0) {
397289d2fb14SKonstantin Belousov 				vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0);
3973194a979eSMark Johnston 				goto restart;
397430c5525bSAndrew Gallatin 			}
3975194a979eSMark Johnston 			break;
3976194a979eSMark Johnston 		}
3977ab3185d1SJeff Roberson 	}
3978ab3185d1SJeff Roberson 
3979bbee39c6SJeff Roberson 	/*
3980bbee39c6SJeff Roberson 	 * We might not have been able to get a slab but another cpu
3981bbee39c6SJeff Roberson 	 * could have while we were unlocked.  Check again before we
3982bbee39c6SJeff Roberson 	 * fail.
3983bbee39c6SJeff Roberson 	 */
39848b987a77SJeff Roberson 	if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL)
3985bbee39c6SJeff Roberson 		return (slab);
39868b987a77SJeff Roberson 
3987ab3185d1SJeff Roberson 	return (NULL);
3988ab3185d1SJeff Roberson }
3989bbee39c6SJeff Roberson 
3990d56368d7SBosko Milekic static void *
39910095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
3992bbee39c6SJeff Roberson {
3993ab3185d1SJeff Roberson 	uma_domain_t dom;
3994bbee39c6SJeff Roberson 	void *item;
39959b8db4d0SRyan Libby 	int freei;
3996bbee39c6SJeff Roberson 
39978b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, slab->us_domain);
3998099a0e58SBosko Milekic 
39998b987a77SJeff Roberson 	dom = &keg->uk_domain[slab->us_domain];
40009b78b1f4SJeff Roberson 	freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1;
40019b78b1f4SJeff Roberson 	BIT_CLR(keg->uk_ipers, freei, &slab->us_free);
40021e0701e1SJeff Roberson 	item = slab_item(slab, keg, freei);
4003bbee39c6SJeff Roberson 	slab->us_freecount--;
40044ab3aee8SMark Johnston 	dom->ud_free_items--;
4005ef72505eSJeff Roberson 
40064ab3aee8SMark Johnston 	/*
40074ab3aee8SMark Johnston 	 * Move this slab to the full list.  It must be on the partial list, so
40084ab3aee8SMark Johnston 	 * we do not need to update the free slab count.  In particular,
40094ab3aee8SMark Johnston 	 * keg_fetch_slab() always returns slabs on the partial list.
40104ab3aee8SMark Johnston 	 */
4011bbee39c6SJeff Roberson 	if (slab->us_freecount == 0) {
4012bbee39c6SJeff Roberson 		LIST_REMOVE(slab, us_link);
4013ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link);
4014bbee39c6SJeff Roberson 	}
4015bbee39c6SJeff Roberson 
4016bbee39c6SJeff Roberson 	return (item);
4017bbee39c6SJeff Roberson }
4018bbee39c6SJeff Roberson 
4019bbee39c6SJeff Roberson static int
4020b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags)
40210095a784SJeff Roberson {
40228b987a77SJeff Roberson 	uma_domain_t dom;
4023b75c4efcSAndrew Turner 	uma_zone_t zone;
40240095a784SJeff Roberson 	uma_slab_t slab;
40250095a784SJeff Roberson 	uma_keg_t keg;
4026a03af342SSean Bruno #ifdef NUMA
4027ab3185d1SJeff Roberson 	int stripe;
4028a03af342SSean Bruno #endif
40290095a784SJeff Roberson 	int i;
40300095a784SJeff Roberson 
4031b75c4efcSAndrew Turner 	zone = arg;
40320095a784SJeff Roberson 	slab = NULL;
4033584061b4SJeff Roberson 	keg = zone->uz_keg;
4034af526374SJeff Roberson 	/* Try to keep the buckets totally full */
40350095a784SJeff Roberson 	for (i = 0; i < max; ) {
4036584061b4SJeff Roberson 		if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL)
40370095a784SJeff Roberson 			break;
4038a03af342SSean Bruno #ifdef NUMA
4039ab3185d1SJeff Roberson 		stripe = howmany(max, vm_ndomains);
4040a03af342SSean Bruno #endif
40418b987a77SJeff Roberson 		dom = &keg->uk_domain[slab->us_domain];
40421b2dcc8cSMark Johnston 		do {
40430095a784SJeff Roberson 			bucket[i++] = slab_alloc_item(keg, slab);
40447585c5dbSMark Johnston 			if (keg->uk_reserve > 0 &&
40457585c5dbSMark Johnston 			    dom->ud_free_items <= keg->uk_reserve) {
40461b2dcc8cSMark Johnston 				/*
40471b2dcc8cSMark Johnston 				 * Avoid depleting the reserve after a
40481b2dcc8cSMark Johnston 				 * successful item allocation, even if
40491b2dcc8cSMark Johnston 				 * M_USE_RESERVE is specified.
40501b2dcc8cSMark Johnston 				 */
40511b2dcc8cSMark Johnston 				KEG_UNLOCK(keg, slab->us_domain);
40521b2dcc8cSMark Johnston 				goto out;
40531b2dcc8cSMark Johnston 			}
4054b6715dabSJeff Roberson #ifdef NUMA
4055ab3185d1SJeff Roberson 			/*
4056ab3185d1SJeff Roberson 			 * If the zone is striped we pick a new slab for every
4057ab3185d1SJeff Roberson 			 * N allocations.  Eliminating this conditional will
4058ab3185d1SJeff Roberson 			 * instead pick a new domain for each bucket rather
4059ab3185d1SJeff Roberson 			 * than stripe within each bucket.  The current option
4060ab3185d1SJeff Roberson 			 * produces more fragmentation and requires more cpu
4061ab3185d1SJeff Roberson 			 * time but yields better distribution.
4062ab3185d1SJeff Roberson 			 */
4063dfe13344SJeff Roberson 			if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 &&
4064ab3185d1SJeff Roberson 			    vm_ndomains > 1 && --stripe == 0)
4065ab3185d1SJeff Roberson 				break;
4066ab3185d1SJeff Roberson #endif
40671b2dcc8cSMark Johnston 		} while (slab->us_freecount != 0 && i < max);
40688b987a77SJeff Roberson 		KEG_UNLOCK(keg, slab->us_domain);
40691b2dcc8cSMark Johnston 
4070ab3185d1SJeff Roberson 		/* Don't block if we allocated any successfully. */
40710095a784SJeff Roberson 		flags &= ~M_WAITOK;
40720095a784SJeff Roberson 		flags |= M_NOWAIT;
40730095a784SJeff Roberson 	}
40741b2dcc8cSMark Johnston out:
40750095a784SJeff Roberson 	return i;
40760095a784SJeff Roberson }
40770095a784SJeff Roberson 
40784bd61e19SJeff Roberson static int
40794bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags)
40804bd61e19SJeff Roberson {
40814bd61e19SJeff Roberson 	uint64_t old, new, total, max;
40824bd61e19SJeff Roberson 
40834bd61e19SJeff Roberson 	/*
40844bd61e19SJeff Roberson 	 * The hard case.  We're going to sleep because there were existing
40854bd61e19SJeff Roberson 	 * sleepers or because we ran out of items.  This routine enforces
40864bd61e19SJeff Roberson 	 * fairness by keeping fifo order.
40874bd61e19SJeff Roberson 	 *
40884bd61e19SJeff Roberson 	 * First release our ill gotten gains and make some noise.
40894bd61e19SJeff Roberson 	 */
40904bd61e19SJeff Roberson 	for (;;) {
40914bd61e19SJeff Roberson 		zone_free_limit(zone, count);
40924bd61e19SJeff Roberson 		zone_log_warning(zone);
40934bd61e19SJeff Roberson 		zone_maxaction(zone);
40944bd61e19SJeff Roberson 		if (flags & M_NOWAIT)
40954bd61e19SJeff Roberson 			return (0);
40964bd61e19SJeff Roberson 
40974bd61e19SJeff Roberson 		/*
40984bd61e19SJeff Roberson 		 * We need to allocate an item or set ourself as a sleeper
40994bd61e19SJeff Roberson 		 * while the sleepq lock is held to avoid wakeup races.  This
41004bd61e19SJeff Roberson 		 * is essentially a home rolled semaphore.
41014bd61e19SJeff Roberson 		 */
41024bd61e19SJeff Roberson 		sleepq_lock(&zone->uz_max_items);
41034bd61e19SJeff Roberson 		old = zone->uz_items;
41044bd61e19SJeff Roberson 		do {
41054bd61e19SJeff Roberson 			MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX);
41064bd61e19SJeff Roberson 			/* Cache the max since we will evaluate twice. */
41074bd61e19SJeff Roberson 			max = zone->uz_max_items;
41084bd61e19SJeff Roberson 			if (UZ_ITEMS_SLEEPERS(old) != 0 ||
41094bd61e19SJeff Roberson 			    UZ_ITEMS_COUNT(old) >= max)
41104bd61e19SJeff Roberson 				new = old + UZ_ITEMS_SLEEPER;
41114bd61e19SJeff Roberson 			else
41124bd61e19SJeff Roberson 				new = old + MIN(count, max - old);
41134bd61e19SJeff Roberson 		} while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0);
41144bd61e19SJeff Roberson 
41154bd61e19SJeff Roberson 		/* We may have successfully allocated under the sleepq lock. */
41164bd61e19SJeff Roberson 		if (UZ_ITEMS_SLEEPERS(new) == 0) {
41174bd61e19SJeff Roberson 			sleepq_release(&zone->uz_max_items);
41184bd61e19SJeff Roberson 			return (new - old);
41194bd61e19SJeff Roberson 		}
41204bd61e19SJeff Roberson 
41214bd61e19SJeff Roberson 		/*
41224bd61e19SJeff Roberson 		 * This is in a different cacheline from uz_items so that we
41234bd61e19SJeff Roberson 		 * don't constantly invalidate the fastpath cacheline when we
41244bd61e19SJeff Roberson 		 * adjust item counts.  This could be limited to toggling on
41254bd61e19SJeff Roberson 		 * transitions.
41264bd61e19SJeff Roberson 		 */
41274bd61e19SJeff Roberson 		atomic_add_32(&zone->uz_sleepers, 1);
41284bd61e19SJeff Roberson 		atomic_add_64(&zone->uz_sleeps, 1);
41294bd61e19SJeff Roberson 
41304bd61e19SJeff Roberson 		/*
41314bd61e19SJeff Roberson 		 * We have added ourselves as a sleeper.  The sleepq lock
41324bd61e19SJeff Roberson 		 * protects us from wakeup races.  Sleep now and then retry.
41334bd61e19SJeff Roberson 		 */
41344bd61e19SJeff Roberson 		sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0);
41354bd61e19SJeff Roberson 		sleepq_wait(&zone->uz_max_items, PVM);
41364bd61e19SJeff Roberson 
41374bd61e19SJeff Roberson 		/*
41384bd61e19SJeff Roberson 		 * After wakeup, remove ourselves as a sleeper and try
41394bd61e19SJeff Roberson 		 * again.  We no longer have the sleepq lock for protection.
41404bd61e19SJeff Roberson 		 *
41414bd61e19SJeff Roberson 		 * Subract ourselves as a sleeper while attempting to add
41424bd61e19SJeff Roberson 		 * our count.
41434bd61e19SJeff Roberson 		 */
41444bd61e19SJeff Roberson 		atomic_subtract_32(&zone->uz_sleepers, 1);
41454bd61e19SJeff Roberson 		old = atomic_fetchadd_64(&zone->uz_items,
41464bd61e19SJeff Roberson 		    -(UZ_ITEMS_SLEEPER - count));
41474bd61e19SJeff Roberson 		/* We're no longer a sleeper. */
41484bd61e19SJeff Roberson 		old -= UZ_ITEMS_SLEEPER;
41494bd61e19SJeff Roberson 
41504bd61e19SJeff Roberson 		/*
41514bd61e19SJeff Roberson 		 * If we're still at the limit, restart.  Notably do not
41524bd61e19SJeff Roberson 		 * block on other sleepers.  Cache the max value to protect
41534bd61e19SJeff Roberson 		 * against changes via sysctl.
41544bd61e19SJeff Roberson 		 */
41554bd61e19SJeff Roberson 		total = UZ_ITEMS_COUNT(old);
41564bd61e19SJeff Roberson 		max = zone->uz_max_items;
41574bd61e19SJeff Roberson 		if (total >= max)
41584bd61e19SJeff Roberson 			continue;
41594bd61e19SJeff Roberson 		/* Truncate if necessary, otherwise wake other sleepers. */
41604bd61e19SJeff Roberson 		if (total + count > max) {
41614bd61e19SJeff Roberson 			zone_free_limit(zone, total + count - max);
41624bd61e19SJeff Roberson 			count = max - total;
41634bd61e19SJeff Roberson 		} else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0)
41644bd61e19SJeff Roberson 			wakeup_one(&zone->uz_max_items);
41654bd61e19SJeff Roberson 
41664bd61e19SJeff Roberson 		return (count);
41674bd61e19SJeff Roberson 	}
41684bd61e19SJeff Roberson }
41694bd61e19SJeff Roberson 
41704bd61e19SJeff Roberson /*
41714bd61e19SJeff Roberson  * Allocate 'count' items from our max_items limit.  Returns the number
41724bd61e19SJeff Roberson  * available.  If M_NOWAIT is not specified it will sleep until at least
41734bd61e19SJeff Roberson  * one item can be allocated.
41744bd61e19SJeff Roberson  */
41754bd61e19SJeff Roberson static int
41764bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags)
41774bd61e19SJeff Roberson {
41784bd61e19SJeff Roberson 	uint64_t old;
41794bd61e19SJeff Roberson 	uint64_t max;
41804bd61e19SJeff Roberson 
41814bd61e19SJeff Roberson 	max = zone->uz_max_items;
41824bd61e19SJeff Roberson 	MPASS(max > 0);
41834bd61e19SJeff Roberson 
41844bd61e19SJeff Roberson 	/*
41854bd61e19SJeff Roberson 	 * We expect normal allocations to succeed with a simple
41864bd61e19SJeff Roberson 	 * fetchadd.
41874bd61e19SJeff Roberson 	 */
41884bd61e19SJeff Roberson 	old = atomic_fetchadd_64(&zone->uz_items, count);
41894bd61e19SJeff Roberson 	if (__predict_true(old + count <= max))
41904bd61e19SJeff Roberson 		return (count);
41914bd61e19SJeff Roberson 
41924bd61e19SJeff Roberson 	/*
41934bd61e19SJeff Roberson 	 * If we had some items and no sleepers just return the
41944bd61e19SJeff Roberson 	 * truncated value.  We have to release the excess space
41954bd61e19SJeff Roberson 	 * though because that may wake sleepers who weren't woken
41964bd61e19SJeff Roberson 	 * because we were temporarily over the limit.
41974bd61e19SJeff Roberson 	 */
41984bd61e19SJeff Roberson 	if (old < max) {
41994bd61e19SJeff Roberson 		zone_free_limit(zone, (old + count) - max);
42004bd61e19SJeff Roberson 		return (max - old);
42014bd61e19SJeff Roberson 	}
42024bd61e19SJeff Roberson 	return (zone_alloc_limit_hard(zone, count, flags));
42034bd61e19SJeff Roberson }
42044bd61e19SJeff Roberson 
42054bd61e19SJeff Roberson /*
42064bd61e19SJeff Roberson  * Free a number of items back to the limit.
42074bd61e19SJeff Roberson  */
42084bd61e19SJeff Roberson static void
42094bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count)
42104bd61e19SJeff Roberson {
42114bd61e19SJeff Roberson 	uint64_t old;
42124bd61e19SJeff Roberson 
42134bd61e19SJeff Roberson 	MPASS(count > 0);
42144bd61e19SJeff Roberson 
42154bd61e19SJeff Roberson 	/*
42164bd61e19SJeff Roberson 	 * In the common case we either have no sleepers or
42174bd61e19SJeff Roberson 	 * are still over the limit and can just return.
42184bd61e19SJeff Roberson 	 */
42194bd61e19SJeff Roberson 	old = atomic_fetchadd_64(&zone->uz_items, -count);
42204bd61e19SJeff Roberson 	if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 ||
42214bd61e19SJeff Roberson 	   UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items))
42224bd61e19SJeff Roberson 		return;
42234bd61e19SJeff Roberson 
42244bd61e19SJeff Roberson 	/*
42254bd61e19SJeff Roberson 	 * Moderate the rate of wakeups.  Sleepers will continue
42264bd61e19SJeff Roberson 	 * to generate wakeups if necessary.
42274bd61e19SJeff Roberson 	 */
42284bd61e19SJeff Roberson 	wakeup_one(&zone->uz_max_items);
42294bd61e19SJeff Roberson }
42304bd61e19SJeff Roberson 
4231fc03d22bSJeff Roberson static uma_bucket_t
4232beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags)
4233bbee39c6SJeff Roberson {
4234bbee39c6SJeff Roberson 	uma_bucket_t bucket;
423509c8cb71SMark Johnston 	int error, maxbucket, cnt;
4236bbee39c6SJeff Roberson 
4237e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name,
4238e63a1c2fSRyan Libby 	    zone, domain);
423930c5525bSAndrew Gallatin 
4240c1685086SJeff Roberson 	/* Avoid allocs targeting empty domains. */
4241c1685086SJeff Roberson 	if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
4242c1685086SJeff Roberson 		domain = UMA_ANYDOMAIN;
42438c277118SMark Johnston 	else if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0)
4244c6fd3e23SJeff Roberson 		domain = UMA_ANYDOMAIN;
4245c1685086SJeff Roberson 
42464bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
42474bd61e19SJeff Roberson 		maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size,
42484bd61e19SJeff Roberson 		    M_NOWAIT);
42494bd61e19SJeff Roberson 	else
425020a4e154SJeff Roberson 		maxbucket = zone->uz_bucket_size;
42514bd61e19SJeff Roberson 	if (maxbucket == 0)
42524bd61e19SJeff Roberson 		return (false);
4253beb8beefSJeff Roberson 
42546fd34d6fSJeff Roberson 	/* Don't wait for buckets, preserve caller's NOVM setting. */
42556fd34d6fSJeff Roberson 	bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
4256beb8beefSJeff Roberson 	if (bucket == NULL) {
4257beb8beefSJeff Roberson 		cnt = 0;
4258beb8beefSJeff Roberson 		goto out;
4259beb8beefSJeff Roberson 	}
42600095a784SJeff Roberson 
42610095a784SJeff Roberson 	bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
4262beb8beefSJeff Roberson 	    MIN(maxbucket, bucket->ub_entries), domain, flags);
42630095a784SJeff Roberson 
42640095a784SJeff Roberson 	/*
42650095a784SJeff Roberson 	 * Initialize the memory if necessary.
42660095a784SJeff Roberson 	 */
42670095a784SJeff Roberson 	if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
4268099a0e58SBosko Milekic 		int i;
4269bbee39c6SJeff Roberson 
427009c8cb71SMark Johnston 		for (i = 0; i < bucket->ub_cnt; i++) {
427109c8cb71SMark Johnston 			kasan_mark_item_valid(zone, bucket->ub_bucket[i]);
427209c8cb71SMark Johnston 			error = zone->uz_init(bucket->ub_bucket[i],
427309c8cb71SMark Johnston 			    zone->uz_size, flags);
427409c8cb71SMark Johnston 			kasan_mark_item_invalid(zone, bucket->ub_bucket[i]);
427509c8cb71SMark Johnston 			if (error != 0)
4276b23f72e9SBrian Feldman 				break;
427709c8cb71SMark Johnston 		}
427809c8cb71SMark Johnston 
4279b23f72e9SBrian Feldman 		/*
4280b23f72e9SBrian Feldman 		 * If we couldn't initialize the whole bucket, put the
4281b23f72e9SBrian Feldman 		 * rest back onto the freelist.
4282b23f72e9SBrian Feldman 		 */
4283b23f72e9SBrian Feldman 		if (i != bucket->ub_cnt) {
4284af526374SJeff Roberson 			zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
42850095a784SJeff Roberson 			    bucket->ub_cnt - i);
4286a5a262c6SBosko Milekic #ifdef INVARIANTS
42870095a784SJeff Roberson 			bzero(&bucket->ub_bucket[i],
42880095a784SJeff Roberson 			    sizeof(void *) * (bucket->ub_cnt - i));
4289a5a262c6SBosko Milekic #endif
4290b23f72e9SBrian Feldman 			bucket->ub_cnt = i;
4291b23f72e9SBrian Feldman 		}
4292099a0e58SBosko Milekic 	}
4293099a0e58SBosko Milekic 
4294beb8beefSJeff Roberson 	cnt = bucket->ub_cnt;
4295f7104ccdSAlexander Motin 	if (bucket->ub_cnt == 0) {
42966fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
42972efcc8cbSGleb Smirnoff 		counter_u64_add(zone->uz_fails, 1);
4298beb8beefSJeff Roberson 		bucket = NULL;
4299beb8beefSJeff Roberson 	}
4300beb8beefSJeff Roberson out:
43014bd61e19SJeff Roberson 	if (zone->uz_max_items > 0 && cnt < maxbucket)
43024bd61e19SJeff Roberson 		zone_free_limit(zone, maxbucket - cnt);
4303fc03d22bSJeff Roberson 
4304fc03d22bSJeff Roberson 	return (bucket);
4305fc03d22bSJeff Roberson }
4306fc03d22bSJeff Roberson 
43078355f576SJeff Roberson /*
43080095a784SJeff Roberson  * Allocates a single item from a zone.
43098355f576SJeff Roberson  *
43108355f576SJeff Roberson  * Arguments
43118355f576SJeff Roberson  *	zone   The zone to alloc for.
43128355f576SJeff Roberson  *	udata  The data to be passed to the constructor.
4313ab3185d1SJeff Roberson  *	domain The domain to allocate from or UMA_ANYDOMAIN.
4314a163d034SWarner Losh  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
43158355f576SJeff Roberson  *
43168355f576SJeff Roberson  * Returns
43178355f576SJeff Roberson  *	NULL if there is no memory and M_NOWAIT is set
4318bbee39c6SJeff Roberson  *	An item if successful
43198355f576SJeff Roberson  */
43208355f576SJeff Roberson 
43218355f576SJeff Roberson static void *
4322ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags)
43238355f576SJeff Roberson {
43248355f576SJeff Roberson 	void *item;
43258355f576SJeff Roberson 
4326791dda87SAndrew Gallatin 	if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) {
4327791dda87SAndrew Gallatin 		counter_u64_add(zone->uz_fails, 1);
4328bb15d1c7SGleb Smirnoff 		return (NULL);
4329791dda87SAndrew Gallatin 	}
43308355f576SJeff Roberson 
4331c1685086SJeff Roberson 	/* Avoid allocs targeting empty domains. */
4332c1685086SJeff Roberson 	if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
433330c5525bSAndrew Gallatin 		domain = UMA_ANYDOMAIN;
4334c1685086SJeff Roberson 
4335ab3185d1SJeff Roberson 	if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1)
4336beb8beefSJeff Roberson 		goto fail_cnt;
43378355f576SJeff Roberson 
4338099a0e58SBosko Milekic 	/*
4339099a0e58SBosko Milekic 	 * We have to call both the zone's init (not the keg's init)
4340099a0e58SBosko Milekic 	 * and the zone's ctor.  This is because the item is going from
4341099a0e58SBosko Milekic 	 * a keg slab directly to the user, and the user is expecting it
4342099a0e58SBosko Milekic 	 * to be both zone-init'd as well as zone-ctor'd.
4343099a0e58SBosko Milekic 	 */
4344b23f72e9SBrian Feldman 	if (zone->uz_init != NULL) {
434509c8cb71SMark Johnston 		int error;
434609c8cb71SMark Johnston 
434709c8cb71SMark Johnston 		kasan_mark_item_valid(zone, item);
434809c8cb71SMark Johnston 		error = zone->uz_init(item, zone->uz_size, flags);
434909c8cb71SMark Johnston 		kasan_mark_item_invalid(zone, item);
435009c8cb71SMark Johnston 		if (error != 0) {
4351bb15d1c7SGleb Smirnoff 			zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT);
4352beb8beefSJeff Roberson 			goto fail_cnt;
4353beb8beefSJeff Roberson 		}
4354beb8beefSJeff Roberson 	}
4355d4665eaaSJeff Roberson 	item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags,
4356d4665eaaSJeff Roberson 	    item);
4357beb8beefSJeff Roberson 	if (item == NULL)
43580095a784SJeff Roberson 		goto fail;
43598355f576SJeff Roberson 
43602efcc8cbSGleb Smirnoff 	counter_u64_add(zone->uz_allocs, 1);
43611431a748SGleb Smirnoff 	CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
43621431a748SGleb Smirnoff 	    zone->uz_name, zone);
43631431a748SGleb Smirnoff 
43648355f576SJeff Roberson 	return (item);
43650095a784SJeff Roberson 
4366beb8beefSJeff Roberson fail_cnt:
4367beb8beefSJeff Roberson 	counter_u64_add(zone->uz_fails, 1);
43680095a784SJeff Roberson fail:
43694bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
43704bd61e19SJeff Roberson 		zone_free_limit(zone, 1);
43711431a748SGleb Smirnoff 	CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
43721431a748SGleb Smirnoff 	    zone->uz_name, zone);
43734bd61e19SJeff Roberson 
43740095a784SJeff Roberson 	return (NULL);
43758355f576SJeff Roberson }
43768355f576SJeff Roberson 
43778355f576SJeff Roberson /* See uma.h */
43788355f576SJeff Roberson void
4379d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item)
4380d4665eaaSJeff Roberson {
4381d4665eaaSJeff Roberson 	uma_cache_t cache;
4382d4665eaaSJeff Roberson 	uma_cache_bucket_t bucket;
4383c6fd3e23SJeff Roberson 	int itemdomain, uz_flags;
4384d4665eaaSJeff Roberson 
4385*841e0a87SGleb Smirnoff 	CTR3(KTR_UMA, "uma_zfree_smr zone %s(%p) item %p",
4386*841e0a87SGleb Smirnoff 	    zone->uz_name, zone, item);
4387*841e0a87SGleb Smirnoff 
4388d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
4389d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
4390952c8964SMark Johnston 	    ("uma_zfree_smr: called with non-SMR zone."));
4391d4665eaaSJeff Roberson 	KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer."));
4392c6fd3e23SJeff Roberson 	SMR_ASSERT_NOT_ENTERED(zone->uz_smr);
4393d4665eaaSJeff Roberson 	if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN)
4394d4665eaaSJeff Roberson 		return;
4395d4665eaaSJeff Roberson #endif
4396d4665eaaSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4397d4665eaaSJeff Roberson 	uz_flags = cache_uz_flags(cache);
4398c6fd3e23SJeff Roberson 	itemdomain = 0;
4399d4665eaaSJeff Roberson #ifdef NUMA
4400d4665eaaSJeff Roberson 	if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
440181302f1dSMark Johnston 		itemdomain = item_domain(item);
4402d4665eaaSJeff Roberson #endif
4403d4665eaaSJeff Roberson 	critical_enter();
4404d4665eaaSJeff Roberson 	do {
4405d4665eaaSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
4406d4665eaaSJeff Roberson 		/* SMR Zones must free to the free bucket. */
4407d4665eaaSJeff Roberson 		bucket = &cache->uc_freebucket;
4408d4665eaaSJeff Roberson #ifdef NUMA
4409d4665eaaSJeff Roberson 		if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4410c6fd3e23SJeff Roberson 		    PCPU_GET(domain) != itemdomain) {
4411d4665eaaSJeff Roberson 			bucket = &cache->uc_crossbucket;
4412d4665eaaSJeff Roberson 		}
4413d4665eaaSJeff Roberson #endif
4414d4665eaaSJeff Roberson 		if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
4415d4665eaaSJeff Roberson 			cache_bucket_push(cache, bucket, item);
4416d4665eaaSJeff Roberson 			critical_exit();
4417d4665eaaSJeff Roberson 			return;
4418d4665eaaSJeff Roberson 		}
44192cb67bd7SGleb Smirnoff 	} while (cache_free(zone, cache, NULL, itemdomain));
4420d4665eaaSJeff Roberson 	critical_exit();
4421d4665eaaSJeff Roberson 
4422d4665eaaSJeff Roberson 	/*
4423d4665eaaSJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
4424d4665eaaSJeff Roberson 	 */
4425d4665eaaSJeff Roberson 	zone_free_item(zone, item, NULL, SKIP_NONE);
4426d4665eaaSJeff Roberson }
4427d4665eaaSJeff Roberson 
4428d4665eaaSJeff Roberson /* See uma.h */
4429d4665eaaSJeff Roberson void
44308355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
44318355f576SJeff Roberson {
44328355f576SJeff Roberson 	uma_cache_t cache;
4433376b1ba3SJeff Roberson 	uma_cache_bucket_t bucket;
4434c6fd3e23SJeff Roberson 	int itemdomain, uz_flags;
44358355f576SJeff Roberson 
4436e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
443719fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
443810cb2424SMark Murray 
443928782f73SGleb Smirnoff 	CTR3(KTR_UMA, "uma_zfree_arg zone %s(%p) item %p",
444028782f73SGleb Smirnoff 	    zone->uz_name, zone, item);
44413659f747SRobert Watson 
4442d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
4443d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
4444952c8964SMark Johnston 	    ("uma_zfree_arg: called with SMR zone."));
4445d4665eaaSJeff Roberson 	if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN)
4446d4665eaaSJeff Roberson 		return;
4447d4665eaaSJeff Roberson #endif
444820ed0cb0SMatthew D Fleming         /* uma_zfree(..., NULL) does nothing, to match free(9). */
444920ed0cb0SMatthew D Fleming         if (item == NULL)
445020ed0cb0SMatthew D Fleming                 return;
4451cc7ce83aSJeff Roberson 
4452cc7ce83aSJeff Roberson 	/*
4453cc7ce83aSJeff Roberson 	 * We are accessing the per-cpu cache without a critical section to
4454cc7ce83aSJeff Roberson 	 * fetch size and flags.  This is acceptable, if we are preempted we
4455cc7ce83aSJeff Roberson 	 * will simply read another cpu's line.
4456cc7ce83aSJeff Roberson 	 */
4457cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4458cc7ce83aSJeff Roberson 	uz_flags = cache_uz_flags(cache);
4459d4665eaaSJeff Roberson 	if (UMA_ALWAYS_CTORDTOR ||
4460d4665eaaSJeff Roberson 	    __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0))
4461cc7ce83aSJeff Roberson 		item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE);
4462ef72505eSJeff Roberson 
4463af7f9b97SJeff Roberson 	/*
4464af7f9b97SJeff Roberson 	 * The race here is acceptable.  If we miss it we'll just have to wait
4465af7f9b97SJeff Roberson 	 * a little longer for the limits to be reset.
4466af7f9b97SJeff Roberson 	 */
4467cc7ce83aSJeff Roberson 	if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) {
44688a6776caSMark Johnston 		if (atomic_load_32(&zone->uz_sleepers) > 0)
4469fc03d22bSJeff Roberson 			goto zfree_item;
4470cc7ce83aSJeff Roberson 	}
4471af7f9b97SJeff Roberson 
44725d1ae027SRobert Watson 	/*
44735d1ae027SRobert Watson 	 * If possible, free to the per-CPU cache.  There are two
44745d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
44755d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
44765d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
44775d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
44785d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
44795d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to free to the
44805d1ae027SRobert Watson 	 * current cache; when we re-acquire the critical section, we must
44815d1ae027SRobert Watson 	 * detect and handle migration if it has occurred.
44825d1ae027SRobert Watson 	 */
4483c6fd3e23SJeff Roberson 	itemdomain = 0;
4484dfe13344SJeff Roberson #ifdef NUMA
4485dfe13344SJeff Roberson 	if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
448681302f1dSMark Johnston 		itemdomain = item_domain(item);
4487dfe13344SJeff Roberson #endif
44885d1ae027SRobert Watson 	critical_enter();
44890a81b439SJeff Roberson 	do {
4490cc7ce83aSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
4491a553d4b8SJeff Roberson 		/*
4492dfe13344SJeff Roberson 		 * Try to free into the allocbucket first to give LIFO
4493dfe13344SJeff Roberson 		 * ordering for cache-hot datastructures.  Spill over
4494dfe13344SJeff Roberson 		 * into the freebucket if necessary.  Alloc will swap
4495dfe13344SJeff Roberson 		 * them if one runs dry.
4496a553d4b8SJeff Roberson 		 */
4497dfe13344SJeff Roberson 		bucket = &cache->uc_allocbucket;
4498d4665eaaSJeff Roberson #ifdef NUMA
4499d4665eaaSJeff Roberson 		if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4500c6fd3e23SJeff Roberson 		    PCPU_GET(domain) != itemdomain) {
4501d4665eaaSJeff Roberson 			bucket = &cache->uc_crossbucket;
4502d4665eaaSJeff Roberson 		} else
4503d4665eaaSJeff Roberson #endif
4504fe835cbfSJeff Roberson 		if (bucket->ucb_cnt == bucket->ucb_entries &&
4505fe835cbfSJeff Roberson 		   cache->uc_freebucket.ucb_cnt <
4506fe835cbfSJeff Roberson 		   cache->uc_freebucket.ucb_entries)
4507fe835cbfSJeff Roberson 			cache_bucket_swap(&cache->uc_freebucket,
4508fe835cbfSJeff Roberson 			    &cache->uc_allocbucket);
4509376b1ba3SJeff Roberson 		if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
4510376b1ba3SJeff Roberson 			cache_bucket_push(cache, bucket, item);
45115d1ae027SRobert Watson 			critical_exit();
45128355f576SJeff Roberson 			return;
4513fc03d22bSJeff Roberson 		}
45142cb67bd7SGleb Smirnoff 	} while (cache_free(zone, cache, udata, itemdomain));
45150a81b439SJeff Roberson 	critical_exit();
4516fc03d22bSJeff Roberson 
45178355f576SJeff Roberson 	/*
45180a81b439SJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
45198355f576SJeff Roberson 	 */
45200a81b439SJeff Roberson zfree_item:
45210a81b439SJeff Roberson 	zone_free_item(zone, item, udata, SKIP_DTOR);
45220a81b439SJeff Roberson }
4523fc03d22bSJeff Roberson 
4524dfe13344SJeff Roberson #ifdef NUMA
452591d947bfSJeff Roberson /*
452691d947bfSJeff Roberson  * sort crossdomain free buckets to domain correct buckets and cache
452791d947bfSJeff Roberson  * them.
452891d947bfSJeff Roberson  */
452991d947bfSJeff Roberson static void
453091d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata)
453191d947bfSJeff Roberson {
4532991f23efSMark Johnston 	struct uma_bucketlist emptybuckets, fullbuckets;
453391d947bfSJeff Roberson 	uma_zone_domain_t zdom;
453491d947bfSJeff Roberson 	uma_bucket_t b;
4535543117beSJeff Roberson 	smr_seq_t seq;
453691d947bfSJeff Roberson 	void *item;
453791d947bfSJeff Roberson 	int domain;
453891d947bfSJeff Roberson 
453991d947bfSJeff Roberson 	CTR3(KTR_UMA,
454091d947bfSJeff Roberson 	    "uma_zfree: zone %s(%p) draining cross bucket %p",
454191d947bfSJeff Roberson 	    zone->uz_name, zone, bucket);
454291d947bfSJeff Roberson 
4543543117beSJeff Roberson 	/*
4544543117beSJeff Roberson 	 * It is possible for buckets to arrive here out of order so we fetch
4545543117beSJeff Roberson 	 * the current smr seq rather than accepting the bucket's.
4546543117beSJeff Roberson 	 */
4547543117beSJeff Roberson 	seq = SMR_SEQ_INVALID;
4548543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
4549226dd6dbSJeff Roberson 		seq = smr_advance(zone->uz_smr);
4550226dd6dbSJeff Roberson 
4551226dd6dbSJeff Roberson 	/*
4552226dd6dbSJeff Roberson 	 * To avoid having ndomain * ndomain buckets for sorting we have a
4553226dd6dbSJeff Roberson 	 * lock on the current crossfree bucket.  A full matrix with
4554226dd6dbSJeff Roberson 	 * per-domain locking could be used if necessary.
4555226dd6dbSJeff Roberson 	 */
4556991f23efSMark Johnston 	STAILQ_INIT(&emptybuckets);
4557226dd6dbSJeff Roberson 	STAILQ_INIT(&fullbuckets);
4558226dd6dbSJeff Roberson 	ZONE_CROSS_LOCK(zone);
4559991f23efSMark Johnston 	for (; bucket->ub_cnt > 0; bucket->ub_cnt--) {
456091d947bfSJeff Roberson 		item = bucket->ub_bucket[bucket->ub_cnt - 1];
456181302f1dSMark Johnston 		domain = item_domain(item);
4562c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(zone, domain);
456391d947bfSJeff Roberson 		if (zdom->uzd_cross == NULL) {
4564991f23efSMark Johnston 			if ((b = STAILQ_FIRST(&emptybuckets)) != NULL) {
4565991f23efSMark Johnston 				STAILQ_REMOVE_HEAD(&emptybuckets, ub_link);
4566991f23efSMark Johnston 				zdom->uzd_cross = b;
4567991f23efSMark Johnston 			} else {
4568991f23efSMark Johnston 				/*
4569991f23efSMark Johnston 				 * Avoid allocating a bucket with the cross lock
4570991f23efSMark Johnston 				 * held, since allocation can trigger a
4571991f23efSMark Johnston 				 * cross-domain free and bucket zones may
4572991f23efSMark Johnston 				 * allocate from each other.
4573991f23efSMark Johnston 				 */
4574991f23efSMark Johnston 				ZONE_CROSS_UNLOCK(zone);
4575991f23efSMark Johnston 				b = bucket_alloc(zone, udata, M_NOWAIT);
4576991f23efSMark Johnston 				if (b == NULL)
4577991f23efSMark Johnston 					goto out;
4578991f23efSMark Johnston 				ZONE_CROSS_LOCK(zone);
4579991f23efSMark Johnston 				if (zdom->uzd_cross != NULL) {
4580991f23efSMark Johnston 					STAILQ_INSERT_HEAD(&emptybuckets, b,
4581991f23efSMark Johnston 					    ub_link);
4582991f23efSMark Johnston 				} else {
4583991f23efSMark Johnston 					zdom->uzd_cross = b;
4584991f23efSMark Johnston 				}
4585991f23efSMark Johnston 			}
458691d947bfSJeff Roberson 		}
4587543117beSJeff Roberson 		b = zdom->uzd_cross;
4588543117beSJeff Roberson 		b->ub_bucket[b->ub_cnt++] = item;
4589543117beSJeff Roberson 		b->ub_seq = seq;
4590543117beSJeff Roberson 		if (b->ub_cnt == b->ub_entries) {
4591543117beSJeff Roberson 			STAILQ_INSERT_HEAD(&fullbuckets, b, ub_link);
4592991f23efSMark Johnston 			if ((b = STAILQ_FIRST(&emptybuckets)) != NULL)
4593991f23efSMark Johnston 				STAILQ_REMOVE_HEAD(&emptybuckets, ub_link);
4594991f23efSMark Johnston 			zdom->uzd_cross = b;
459591d947bfSJeff Roberson 		}
459691d947bfSJeff Roberson 	}
459791d947bfSJeff Roberson 	ZONE_CROSS_UNLOCK(zone);
4598991f23efSMark Johnston out:
4599c6fd3e23SJeff Roberson 	if (bucket->ub_cnt == 0)
4600d4665eaaSJeff Roberson 		bucket->ub_seq = SMR_SEQ_INVALID;
460191d947bfSJeff Roberson 	bucket_free(zone, bucket, udata);
4602c6fd3e23SJeff Roberson 
4603991f23efSMark Johnston 	while ((b = STAILQ_FIRST(&emptybuckets)) != NULL) {
4604991f23efSMark Johnston 		STAILQ_REMOVE_HEAD(&emptybuckets, ub_link);
4605991f23efSMark Johnston 		bucket_free(zone, b, udata);
4606991f23efSMark Johnston 	}
4607c6fd3e23SJeff Roberson 	while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) {
4608c6fd3e23SJeff Roberson 		STAILQ_REMOVE_HEAD(&fullbuckets, ub_link);
460981302f1dSMark Johnston 		domain = item_domain(b->ub_bucket[0]);
4610c6fd3e23SJeff Roberson 		zone_put_bucket(zone, domain, b, udata, true);
4611c6fd3e23SJeff Roberson 	}
461291d947bfSJeff Roberson }
461391d947bfSJeff Roberson #endif
461491d947bfSJeff Roberson 
46150a81b439SJeff Roberson static void
46160a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata,
4617c6fd3e23SJeff Roberson     int itemdomain, bool ws)
46180a81b439SJeff Roberson {
46190a81b439SJeff Roberson 
4620dfe13344SJeff Roberson #ifdef NUMA
46210a81b439SJeff Roberson 	/*
46220a81b439SJeff Roberson 	 * Buckets coming from the wrong domain will be entirely for the
46230a81b439SJeff Roberson 	 * only other domain on two domain systems.  In this case we can
46240a81b439SJeff Roberson 	 * simply cache them.  Otherwise we need to sort them back to
462591d947bfSJeff Roberson 	 * correct domains.
46260a81b439SJeff Roberson 	 */
4627c6fd3e23SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4628c6fd3e23SJeff Roberson 	    vm_ndomains > 2 && PCPU_GET(domain) != itemdomain) {
462991d947bfSJeff Roberson 		zone_free_cross(zone, bucket, udata);
46300a81b439SJeff Roberson 		return;
46310a81b439SJeff Roberson 	}
46320a81b439SJeff Roberson #endif
463391d947bfSJeff Roberson 
46340a81b439SJeff Roberson 	/*
46350a81b439SJeff Roberson 	 * Attempt to save the bucket in the zone's domain bucket cache.
46360a81b439SJeff Roberson 	 */
46370a81b439SJeff Roberson 	CTR3(KTR_UMA,
46380a81b439SJeff Roberson 	    "uma_zfree: zone %s(%p) putting bucket %p on free list",
46390a81b439SJeff Roberson 	    zone->uz_name, zone, bucket);
46400a81b439SJeff Roberson 	/* ub_cnt is pointing to the last free item */
4641c6fd3e23SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0)
4642c6fd3e23SJeff Roberson 		itemdomain = zone_domain_lowest(zone, itemdomain);
4643c6fd3e23SJeff Roberson 	zone_put_bucket(zone, itemdomain, bucket, udata, ws);
46448355f576SJeff Roberson }
4645fc03d22bSJeff Roberson 
46464d104ba0SAlexander Motin /*
46470a81b439SJeff Roberson  * Populate a free or cross bucket for the current cpu cache.  Free any
46480a81b439SJeff Roberson  * existing full bucket either to the zone cache or back to the slab layer.
46490a81b439SJeff Roberson  *
46500a81b439SJeff Roberson  * Enters and returns in a critical section.  false return indicates that
46510a81b439SJeff Roberson  * we can not satisfy this free in the cache layer.  true indicates that
46520a81b439SJeff Roberson  * the caller should retry.
46534d104ba0SAlexander Motin  */
46540a81b439SJeff Roberson static __noinline bool
46552cb67bd7SGleb Smirnoff cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, int itemdomain)
46560a81b439SJeff Roberson {
4657dfe13344SJeff Roberson 	uma_cache_bucket_t cbucket;
4658d4665eaaSJeff Roberson 	uma_bucket_t newbucket, bucket;
46590a81b439SJeff Roberson 
46600a81b439SJeff Roberson 	CRITICAL_ASSERT(curthread);
46610a81b439SJeff Roberson 
4662d4665eaaSJeff Roberson 	if (zone->uz_bucket_size == 0)
46630a81b439SJeff Roberson 		return false;
46640a81b439SJeff Roberson 
4665cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4666d4665eaaSJeff Roberson 	newbucket = NULL;
46670a81b439SJeff Roberson 
46680a81b439SJeff Roberson 	/*
4669dfe13344SJeff Roberson 	 * FIRSTTOUCH domains need to free to the correct zdom.  When
4670dfe13344SJeff Roberson 	 * enabled this is the zdom of the item.   The bucket is the
4671dfe13344SJeff Roberson 	 * cross bucket if the current domain and itemdomain do not match.
46720a81b439SJeff Roberson 	 */
4673dfe13344SJeff Roberson 	cbucket = &cache->uc_freebucket;
4674dfe13344SJeff Roberson #ifdef NUMA
4675c6fd3e23SJeff Roberson 	if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) {
4676c6fd3e23SJeff Roberson 		if (PCPU_GET(domain) != itemdomain) {
4677dfe13344SJeff Roberson 			cbucket = &cache->uc_crossbucket;
4678dfe13344SJeff Roberson 			if (cbucket->ucb_cnt != 0)
4679c6fd3e23SJeff Roberson 				counter_u64_add(zone->uz_xdomain,
4680dfe13344SJeff Roberson 				    cbucket->ucb_cnt);
4681dfe13344SJeff Roberson 		}
4682c6fd3e23SJeff Roberson 	}
46830a81b439SJeff Roberson #endif
4684dfe13344SJeff Roberson 	bucket = cache_bucket_unload(cbucket);
4685c6fd3e23SJeff Roberson 	KASSERT(bucket == NULL || bucket->ub_cnt == bucket->ub_entries,
4686c6fd3e23SJeff Roberson 	    ("cache_free: Entered with non-full free bucket."));
46870a81b439SJeff Roberson 
46880a81b439SJeff Roberson 	/* We are no longer associated with this CPU. */
46890a81b439SJeff Roberson 	critical_exit();
46900a81b439SJeff Roberson 
4691d4665eaaSJeff Roberson 	/*
4692d4665eaaSJeff Roberson 	 * Don't let SMR zones operate without a free bucket.  Force
4693d4665eaaSJeff Roberson 	 * a synchronize and re-use this one.  We will only degrade
4694d4665eaaSJeff Roberson 	 * to a synchronize every bucket_size items rather than every
4695d4665eaaSJeff Roberson 	 * item if we fail to allocate a bucket.
4696d4665eaaSJeff Roberson 	 */
4697d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0) {
4698d4665eaaSJeff Roberson 		if (bucket != NULL)
4699d4665eaaSJeff Roberson 			bucket->ub_seq = smr_advance(zone->uz_smr);
4700d4665eaaSJeff Roberson 		newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4701d4665eaaSJeff Roberson 		if (newbucket == NULL && bucket != NULL) {
4702d4665eaaSJeff Roberson 			bucket_drain(zone, bucket);
4703d4665eaaSJeff Roberson 			newbucket = bucket;
4704d4665eaaSJeff Roberson 			bucket = NULL;
4705d4665eaaSJeff Roberson 		}
4706d4665eaaSJeff Roberson 	} else if (!bucketdisable)
4707d4665eaaSJeff Roberson 		newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4708d4665eaaSJeff Roberson 
47090a81b439SJeff Roberson 	if (bucket != NULL)
4710c6fd3e23SJeff Roberson 		zone_free_bucket(zone, bucket, udata, itemdomain, true);
4711a553d4b8SJeff Roberson 
4712fc03d22bSJeff Roberson 	critical_enter();
4713d4665eaaSJeff Roberson 	if ((bucket = newbucket) == NULL)
47140a81b439SJeff Roberson 		return (false);
4715cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4716dfe13344SJeff Roberson #ifdef NUMA
4717fc03d22bSJeff Roberson 	/*
47180a81b439SJeff Roberson 	 * Check to see if we should be populating the cross bucket.  If it
47190a81b439SJeff Roberson 	 * is already populated we will fall through and attempt to populate
47200a81b439SJeff Roberson 	 * the free bucket.
4721fc03d22bSJeff Roberson 	 */
4722c6fd3e23SJeff Roberson 	if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) {
4723c6fd3e23SJeff Roberson 		if (PCPU_GET(domain) != itemdomain &&
4724376b1ba3SJeff Roberson 		    cache->uc_crossbucket.ucb_bucket == NULL) {
4725376b1ba3SJeff Roberson 			cache_bucket_load_cross(cache, bucket);
47260a81b439SJeff Roberson 			return (true);
47270a81b439SJeff Roberson 		}
47280a81b439SJeff Roberson 	}
47290a81b439SJeff Roberson #endif
47300a81b439SJeff Roberson 	/*
47310a81b439SJeff Roberson 	 * We may have lost the race to fill the bucket or switched CPUs.
47320a81b439SJeff Roberson 	 */
4733376b1ba3SJeff Roberson 	if (cache->uc_freebucket.ucb_bucket != NULL) {
4734fc03d22bSJeff Roberson 		critical_exit();
47356fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
47360a81b439SJeff Roberson 		critical_enter();
47370a81b439SJeff Roberson 	} else
4738376b1ba3SJeff Roberson 		cache_bucket_load_free(cache, bucket);
47398355f576SJeff Roberson 
47400a81b439SJeff Roberson 	return (true);
47418355f576SJeff Roberson }
47428355f576SJeff Roberson 
47438355f576SJeff Roberson static void
4744bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item)
47458355f576SJeff Roberson {
4746bb15d1c7SGleb Smirnoff 	uma_keg_t keg;
4747ab3185d1SJeff Roberson 	uma_domain_t dom;
47489b8db4d0SRyan Libby 	int freei;
4749099a0e58SBosko Milekic 
4750bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
47518b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, slab->us_domain);
4752ab3185d1SJeff Roberson 
47538355f576SJeff Roberson 	/* Do we need to remove from any lists? */
47548b987a77SJeff Roberson 	dom = &keg->uk_domain[slab->us_domain];
4755099a0e58SBosko Milekic 	if (slab->us_freecount + 1 == keg->uk_ipers) {
47568355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
4757ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
47584ab3aee8SMark Johnston 		dom->ud_free_slabs++;
47598355f576SJeff Roberson 	} else if (slab->us_freecount == 0) {
47608355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
4761ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
47628355f576SJeff Roberson 	}
47638355f576SJeff Roberson 
4764ef72505eSJeff Roberson 	/* Slab management. */
47651e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
47669b78b1f4SJeff Roberson 	BIT_SET(keg->uk_ipers, freei, &slab->us_free);
47678355f576SJeff Roberson 	slab->us_freecount++;
47688355f576SJeff Roberson 
4769ef72505eSJeff Roberson 	/* Keg statistics. */
47704ab3aee8SMark Johnston 	dom->ud_free_items++;
47710095a784SJeff Roberson }
47720095a784SJeff Roberson 
47730095a784SJeff Roberson static void
4774b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt)
47750095a784SJeff Roberson {
47768b987a77SJeff Roberson 	struct mtx *lock;
4777b75c4efcSAndrew Turner 	uma_zone_t zone;
47780095a784SJeff Roberson 	uma_slab_t slab;
47790095a784SJeff Roberson 	uma_keg_t keg;
47800095a784SJeff Roberson 	uint8_t *mem;
47818b987a77SJeff Roberson 	void *item;
47820095a784SJeff Roberson 	int i;
47838355f576SJeff Roberson 
4784b75c4efcSAndrew Turner 	zone = arg;
4785bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
47868b987a77SJeff Roberson 	lock = NULL;
478754c5ae80SRyan Libby 	if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0))
47888b987a77SJeff Roberson 		lock = KEG_LOCK(keg, 0);
47890095a784SJeff Roberson 	for (i = 0; i < cnt; i++) {
47900095a784SJeff Roberson 		item = bucket[i];
479154c5ae80SRyan Libby 		if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) {
47920095a784SJeff Roberson 			slab = vtoslab((vm_offset_t)item);
47938b987a77SJeff Roberson 		} else {
47948b987a77SJeff Roberson 			mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
479554c5ae80SRyan Libby 			if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0)
47968b987a77SJeff Roberson 				slab = hash_sfind(&keg->uk_hash, mem);
47978b987a77SJeff Roberson 			else
47988b987a77SJeff Roberson 				slab = (uma_slab_t)(mem + keg->uk_pgoff);
47998b987a77SJeff Roberson 		}
48008b987a77SJeff Roberson 		if (lock != KEG_LOCKPTR(keg, slab->us_domain)) {
48018b987a77SJeff Roberson 			if (lock != NULL)
48028b987a77SJeff Roberson 				mtx_unlock(lock);
48038b987a77SJeff Roberson 			lock = KEG_LOCK(keg, slab->us_domain);
48048b987a77SJeff Roberson 		}
4805bb15d1c7SGleb Smirnoff 		slab_free_item(zone, slab, item);
48060095a784SJeff Roberson 	}
48078b987a77SJeff Roberson 	if (lock != NULL)
48088b987a77SJeff Roberson 		mtx_unlock(lock);
48098355f576SJeff Roberson }
48108355f576SJeff Roberson 
48110095a784SJeff Roberson /*
48120095a784SJeff Roberson  * Frees a single item to any zone.
48130095a784SJeff Roberson  *
48140095a784SJeff Roberson  * Arguments:
48150095a784SJeff Roberson  *	zone   The zone to free to
48160095a784SJeff Roberson  *	item   The item we're freeing
48170095a784SJeff Roberson  *	udata  User supplied data for the dtor
48180095a784SJeff Roberson  *	skip   Skip dtors and finis
48190095a784SJeff Roberson  */
48206d88d784SJeff Roberson static __noinline void
48210095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
48220095a784SJeff Roberson {
4823c5deaf04SGleb Smirnoff 
4824d4665eaaSJeff Roberson 	/*
4825d4665eaaSJeff Roberson 	 * If a free is sent directly to an SMR zone we have to
4826d4665eaaSJeff Roberson 	 * synchronize immediately because the item can instantly
4827d4665eaaSJeff Roberson 	 * be reallocated. This should only happen in degenerate
4828d4665eaaSJeff Roberson 	 * cases when no memory is available for per-cpu caches.
4829d4665eaaSJeff Roberson 	 */
4830d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE)
4831d4665eaaSJeff Roberson 		smr_synchronize(zone->uz_smr);
4832d4665eaaSJeff Roberson 
4833cc7ce83aSJeff Roberson 	item_dtor(zone, item, zone->uz_size, udata, skip);
48340095a784SJeff Roberson 
483509c8cb71SMark Johnston 	if (skip < SKIP_FINI && zone->uz_fini) {
483609c8cb71SMark Johnston 		kasan_mark_item_valid(zone, item);
48370095a784SJeff Roberson 		zone->uz_fini(item, zone->uz_size);
483809c8cb71SMark Johnston 		kasan_mark_item_invalid(zone, item);
483909c8cb71SMark Johnston 	}
48400095a784SJeff Roberson 
48410095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, &item, 1);
4842bb15d1c7SGleb Smirnoff 
4843bb15d1c7SGleb Smirnoff 	if (skip & SKIP_CNT)
4844bb15d1c7SGleb Smirnoff 		return;
4845bb15d1c7SGleb Smirnoff 
48462efcc8cbSGleb Smirnoff 	counter_u64_add(zone->uz_frees, 1);
48472efcc8cbSGleb Smirnoff 
48484bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
48494bd61e19SJeff Roberson 		zone_free_limit(zone, 1);
4850bb45b411SGleb Smirnoff }
48510095a784SJeff Roberson 
48528355f576SJeff Roberson /* See uma.h */
48531c6cae97SLawrence Stewart int
4854736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems)
4855736ee590SJeff Roberson {
4856e574d407SMark Johnston 
4857e574d407SMark Johnston 	/*
4858e574d407SMark Johnston 	 * If the limit is small, we may need to constrain the maximum per-CPU
4859e574d407SMark Johnston 	 * cache size, or disable caching entirely.
4860e574d407SMark Johnston 	 */
4861e574d407SMark Johnston 	uma_zone_set_maxcache(zone, nitems);
4862bb15d1c7SGleb Smirnoff 
48634bd61e19SJeff Roberson 	/*
48644bd61e19SJeff Roberson 	 * XXX This can misbehave if the zone has any allocations with
48654bd61e19SJeff Roberson 	 * no limit and a limit is imposed.  There is currently no
48664bd61e19SJeff Roberson 	 * way to clear a limit.
48674bd61e19SJeff Roberson 	 */
4868bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4869bb15d1c7SGleb Smirnoff 	zone->uz_max_items = nitems;
4870cc7ce83aSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_LIMIT;
4871cc7ce83aSJeff Roberson 	zone_update_caches(zone);
48724bd61e19SJeff Roberson 	/* We may need to wake waiters. */
48734bd61e19SJeff Roberson 	wakeup(&zone->uz_max_items);
4874bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4875bb15d1c7SGleb Smirnoff 
4876bb15d1c7SGleb Smirnoff 	return (nitems);
4877bb15d1c7SGleb Smirnoff }
4878bb15d1c7SGleb Smirnoff 
4879bb15d1c7SGleb Smirnoff /* See uma.h */
4880003cf08bSMark Johnston void
4881bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems)
4882bb15d1c7SGleb Smirnoff {
4883e574d407SMark Johnston 	int bpcpu, bpdom, bsize, nb;
4884bb15d1c7SGleb Smirnoff 
4885bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4886e574d407SMark Johnston 
4887e574d407SMark Johnston 	/*
4888e574d407SMark Johnston 	 * Compute a lower bound on the number of items that may be cached in
4889e574d407SMark Johnston 	 * the zone.  Each CPU gets at least two buckets, and for cross-domain
4890e574d407SMark Johnston 	 * frees we use an additional bucket per CPU and per domain.  Select the
4891e574d407SMark Johnston 	 * largest bucket size that does not exceed half of the requested limit,
4892e574d407SMark Johnston 	 * with the left over space given to the full bucket cache.
4893e574d407SMark Johnston 	 */
4894e574d407SMark Johnston 	bpdom = 0;
4895003cf08bSMark Johnston 	bpcpu = 2;
4896e574d407SMark Johnston #ifdef NUMA
4897e574d407SMark Johnston 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && vm_ndomains > 1) {
4898003cf08bSMark Johnston 		bpcpu++;
4899e574d407SMark Johnston 		bpdom++;
4900003cf08bSMark Johnston 	}
4901e574d407SMark Johnston #endif
4902e574d407SMark Johnston 	nb = bpcpu * mp_ncpus + bpdom * vm_ndomains;
4903e574d407SMark Johnston 	bsize = nitems / nb / 2;
4904e574d407SMark Johnston 	if (bsize > BUCKET_MAX)
4905e574d407SMark Johnston 		bsize = BUCKET_MAX;
4906e574d407SMark Johnston 	else if (bsize == 0 && nitems / nb > 0)
4907e574d407SMark Johnston 		bsize = 1;
4908e574d407SMark Johnston 	zone->uz_bucket_size_max = zone->uz_bucket_size = bsize;
490920a4e154SJeff Roberson 	if (zone->uz_bucket_size_min > zone->uz_bucket_size_max)
491020a4e154SJeff Roberson 		zone->uz_bucket_size_min = zone->uz_bucket_size_max;
4911e574d407SMark Johnston 	zone->uz_bucket_max = nitems - nb * bsize;
4912bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4913736ee590SJeff Roberson }
4914736ee590SJeff Roberson 
4915736ee590SJeff Roberson /* See uma.h */
4916e49471b0SAndre Oppermann int
4917e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone)
4918e49471b0SAndre Oppermann {
4919e49471b0SAndre Oppermann 	int nitems;
4920e49471b0SAndre Oppermann 
4921727c6918SJeff Roberson 	nitems = atomic_load_64(&zone->uz_max_items);
4922e49471b0SAndre Oppermann 
4923e49471b0SAndre Oppermann 	return (nitems);
4924e49471b0SAndre Oppermann }
4925e49471b0SAndre Oppermann 
4926e49471b0SAndre Oppermann /* See uma.h */
49272f891cd5SPawel Jakub Dawidek void
49282f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning)
49292f891cd5SPawel Jakub Dawidek {
49302f891cd5SPawel Jakub Dawidek 
4931727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
49322f891cd5SPawel Jakub Dawidek 	zone->uz_warning = warning;
49332f891cd5SPawel Jakub Dawidek }
49342f891cd5SPawel Jakub Dawidek 
49352f891cd5SPawel Jakub Dawidek /* See uma.h */
493654503a13SJonathan T. Looney void
493754503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
493854503a13SJonathan T. Looney {
493954503a13SJonathan T. Looney 
4940727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
4941e60b2fcbSGleb Smirnoff 	TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
494254503a13SJonathan T. Looney }
494354503a13SJonathan T. Looney 
494454503a13SJonathan T. Looney /* See uma.h */
4945c4ae7908SLawrence Stewart int
4946c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone)
4947c4ae7908SLawrence Stewart {
4948c4ae7908SLawrence Stewart 	int64_t nitems;
4949c4ae7908SLawrence Stewart 	u_int i;
4950c4ae7908SLawrence Stewart 
4951bfb6b7a1SJeff Roberson 	nitems = 0;
4952bfb6b7a1SJeff Roberson 	if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER)
49532efcc8cbSGleb Smirnoff 		nitems = counter_u64_fetch(zone->uz_allocs) -
49542efcc8cbSGleb Smirnoff 		    counter_u64_fetch(zone->uz_frees);
4955727c6918SJeff Roberson 	CPU_FOREACH(i)
4956727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) -
4957727c6918SJeff Roberson 		    atomic_load_64(&zone->uz_cpu[i].uc_frees);
4958c4ae7908SLawrence Stewart 
4959c4ae7908SLawrence Stewart 	return (nitems < 0 ? 0 : nitems);
4960c4ae7908SLawrence Stewart }
4961c4ae7908SLawrence Stewart 
496220a4e154SJeff Roberson static uint64_t
496320a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone)
496420a4e154SJeff Roberson {
496520a4e154SJeff Roberson 	uint64_t nitems;
496620a4e154SJeff Roberson 	u_int i;
496720a4e154SJeff Roberson 
4968bfb6b7a1SJeff Roberson 	nitems = 0;
4969bfb6b7a1SJeff Roberson 	if (zone->uz_allocs != EARLY_COUNTER)
497020a4e154SJeff Roberson 		nitems = counter_u64_fetch(zone->uz_allocs);
4971727c6918SJeff Roberson 	CPU_FOREACH(i)
4972727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs);
497320a4e154SJeff Roberson 
497420a4e154SJeff Roberson 	return (nitems);
497520a4e154SJeff Roberson }
497620a4e154SJeff Roberson 
497720a4e154SJeff Roberson static uint64_t
497820a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone)
497920a4e154SJeff Roberson {
498020a4e154SJeff Roberson 	uint64_t nitems;
498120a4e154SJeff Roberson 	u_int i;
498220a4e154SJeff Roberson 
4983bfb6b7a1SJeff Roberson 	nitems = 0;
4984bfb6b7a1SJeff Roberson 	if (zone->uz_frees != EARLY_COUNTER)
498520a4e154SJeff Roberson 		nitems = counter_u64_fetch(zone->uz_frees);
4986727c6918SJeff Roberson 	CPU_FOREACH(i)
4987727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees);
498820a4e154SJeff Roberson 
498920a4e154SJeff Roberson 	return (nitems);
499020a4e154SJeff Roberson }
499120a4e154SJeff Roberson 
499231c251a0SJeff Roberson #ifdef INVARIANTS
499331c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */
499431c251a0SJeff Roberson static uint64_t
499531c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg)
499631c251a0SJeff Roberson {
499731c251a0SJeff Roberson 	uma_zone_t z;
499831c251a0SJeff Roberson 	uint64_t nitems;
499931c251a0SJeff Roberson 
500031c251a0SJeff Roberson 	nitems = 0;
500131c251a0SJeff Roberson 	LIST_FOREACH(z, &keg->uk_zones, uz_link)
500231c251a0SJeff Roberson 		nitems += uma_zone_get_allocs(z);
500331c251a0SJeff Roberson 
500431c251a0SJeff Roberson 	return (nitems);
500531c251a0SJeff Roberson }
500631c251a0SJeff Roberson #endif
500731c251a0SJeff Roberson 
5008c4ae7908SLawrence Stewart /* See uma.h */
5009736ee590SJeff Roberson void
5010099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit)
5011099a0e58SBosko Milekic {
5012e20a199fSJeff Roberson 	uma_keg_t keg;
5013e20a199fSJeff Roberson 
5014bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5015727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
5016e20a199fSJeff Roberson 	keg->uk_init = uminit;
5017099a0e58SBosko Milekic }
5018099a0e58SBosko Milekic 
5019099a0e58SBosko Milekic /* See uma.h */
5020099a0e58SBosko Milekic void
5021099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
5022099a0e58SBosko Milekic {
5023e20a199fSJeff Roberson 	uma_keg_t keg;
5024e20a199fSJeff Roberson 
5025bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5026727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
5027e20a199fSJeff Roberson 	keg->uk_fini = fini;
5028099a0e58SBosko Milekic }
5029099a0e58SBosko Milekic 
5030099a0e58SBosko Milekic /* See uma.h */
5031099a0e58SBosko Milekic void
5032099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
5033099a0e58SBosko Milekic {
5034af526374SJeff Roberson 
5035727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
5036099a0e58SBosko Milekic 	zone->uz_init = zinit;
5037099a0e58SBosko Milekic }
5038099a0e58SBosko Milekic 
5039099a0e58SBosko Milekic /* See uma.h */
5040099a0e58SBosko Milekic void
5041099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
5042099a0e58SBosko Milekic {
5043af526374SJeff Roberson 
5044727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
5045099a0e58SBosko Milekic 	zone->uz_fini = zfini;
5046099a0e58SBosko Milekic }
5047099a0e58SBosko Milekic 
5048099a0e58SBosko Milekic /* See uma.h */
5049099a0e58SBosko Milekic void
50508355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef)
50518355f576SJeff Roberson {
50520095a784SJeff Roberson 	uma_keg_t keg;
5053e20a199fSJeff Roberson 
5054bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5055727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
50560095a784SJeff Roberson 	keg->uk_freef = freef;
50578355f576SJeff Roberson }
50588355f576SJeff Roberson 
50598355f576SJeff Roberson /* See uma.h */
50608355f576SJeff Roberson void
50618355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
50628355f576SJeff Roberson {
5063e20a199fSJeff Roberson 	uma_keg_t keg;
5064e20a199fSJeff Roberson 
5065bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5066727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
5067e20a199fSJeff Roberson 	keg->uk_allocf = allocf;
50688355f576SJeff Roberson }
50698355f576SJeff Roberson 
50708355f576SJeff Roberson /* See uma.h */
50716fd34d6fSJeff Roberson void
5072d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr)
5073d4665eaaSJeff Roberson {
5074d4665eaaSJeff Roberson 
5075d4665eaaSJeff Roberson 	ZONE_ASSERT_COLD(zone);
5076d4665eaaSJeff Roberson 
50777f746c9fSMateusz Guzik 	KASSERT(smr != NULL, ("Got NULL smr"));
50787f746c9fSMateusz Guzik 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
50797f746c9fSMateusz Guzik 	    ("zone %p (%s) already uses SMR", zone, zone->uz_name));
5080d4665eaaSJeff Roberson 	zone->uz_flags |= UMA_ZONE_SMR;
5081d4665eaaSJeff Roberson 	zone->uz_smr = smr;
5082d4665eaaSJeff Roberson 	zone_update_caches(zone);
5083d4665eaaSJeff Roberson }
5084d4665eaaSJeff Roberson 
5085d4665eaaSJeff Roberson smr_t
5086d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone)
5087d4665eaaSJeff Roberson {
5088d4665eaaSJeff Roberson 
5089d4665eaaSJeff Roberson 	return (zone->uz_smr);
5090d4665eaaSJeff Roberson }
5091d4665eaaSJeff Roberson 
5092d4665eaaSJeff Roberson /* See uma.h */
5093d4665eaaSJeff Roberson void
50946fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items)
50956fd34d6fSJeff Roberson {
50966fd34d6fSJeff Roberson 	uma_keg_t keg;
50976fd34d6fSJeff Roberson 
5098bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5099727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
51006fd34d6fSJeff Roberson 	keg->uk_reserve = items;
51016fd34d6fSJeff Roberson }
51026fd34d6fSJeff Roberson 
51036fd34d6fSJeff Roberson /* See uma.h */
51048355f576SJeff Roberson int
5105a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count)
51068355f576SJeff Roberson {
5107099a0e58SBosko Milekic 	uma_keg_t keg;
51088355f576SJeff Roberson 	vm_offset_t kva;
51099ba30bcbSZbigniew Bodek 	u_int pages;
51108355f576SJeff Roberson 
5111bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5112727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
5113727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
51148355f576SJeff Roberson 
511579c9f942SJeff Roberson 	pages = howmany(count, keg->uk_ipers) * keg->uk_ppera;
5116a553d4b8SJeff Roberson 
5117a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
5118a4915c21SAttilio Rao 	if (keg->uk_ppera > 1) {
5119a4915c21SAttilio Rao #else
5120a4915c21SAttilio Rao 	if (1) {
5121a4915c21SAttilio Rao #endif
512257223e99SAndriy Gapon 		kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
5123d1f42ac2SAlan Cox 		if (kva == 0)
51248355f576SJeff Roberson 			return (0);
5125a4915c21SAttilio Rao 	} else
5126a4915c21SAttilio Rao 		kva = 0;
5127bb15d1c7SGleb Smirnoff 
5128bb15d1c7SGleb Smirnoff 	MPASS(keg->uk_kva == 0);
5129099a0e58SBosko Milekic 	keg->uk_kva = kva;
5130a4915c21SAttilio Rao 	keg->uk_offset = 0;
5131bb15d1c7SGleb Smirnoff 	zone->uz_max_items = pages * keg->uk_ipers;
5132a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
5133a4915c21SAttilio Rao 	keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
5134a4915c21SAttilio Rao #else
5135a4915c21SAttilio Rao 	keg->uk_allocf = noobj_alloc;
5136a4915c21SAttilio Rao #endif
5137cc7ce83aSJeff Roberson 	keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
5138cc7ce83aSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
5139cc7ce83aSJeff Roberson 	zone_update_caches(zone);
5140af526374SJeff Roberson 
51418355f576SJeff Roberson 	return (1);
51428355f576SJeff Roberson }
51438355f576SJeff Roberson 
51448355f576SJeff Roberson /* See uma.h */
51458355f576SJeff Roberson void
51468355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items)
51478355f576SJeff Roberson {
5148920239efSMark Johnston 	struct vm_domainset_iter di;
5149ab3185d1SJeff Roberson 	uma_domain_t dom;
51508355f576SJeff Roberson 	uma_slab_t slab;
5151099a0e58SBosko Milekic 	uma_keg_t keg;
515286220393SMark Johnston 	int aflags, domain, slabs;
51538355f576SJeff Roberson 
5154bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
515579c9f942SJeff Roberson 	slabs = howmany(items, keg->uk_ipers);
5156194a979eSMark Johnston 	while (slabs-- > 0) {
515786220393SMark Johnston 		aflags = M_NOWAIT;
515886220393SMark Johnston 		vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
515986220393SMark Johnston 		    &aflags);
516086220393SMark Johnston 		for (;;) {
516186220393SMark Johnston 			slab = keg_alloc_slab(keg, zone, domain, M_WAITOK,
516286220393SMark Johnston 			    aflags);
516386220393SMark Johnston 			if (slab != NULL) {
5164ab3185d1SJeff Roberson 				dom = &keg->uk_domain[slab->us_domain];
51654ab3aee8SMark Johnston 				/*
51664ab3aee8SMark Johnston 				 * keg_alloc_slab() always returns a slab on the
51674ab3aee8SMark Johnston 				 * partial list.
51684ab3aee8SMark Johnston 				 */
51698b987a77SJeff Roberson 				LIST_REMOVE(slab, us_link);
517086220393SMark Johnston 				LIST_INSERT_HEAD(&dom->ud_free_slab, slab,
517186220393SMark Johnston 				    us_link);
51724ab3aee8SMark Johnston 				dom->ud_free_slabs++;
51738b987a77SJeff Roberson 				KEG_UNLOCK(keg, slab->us_domain);
5174920239efSMark Johnston 				break;
51758355f576SJeff Roberson 			}
51768b987a77SJeff Roberson 			if (vm_domainset_iter_policy(&di, &domain) != 0)
517789d2fb14SKonstantin Belousov 				vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0);
517886220393SMark Johnston 		}
517986220393SMark Johnston 	}
518086220393SMark Johnston }
51818355f576SJeff Roberson 
5182ed581bf6SJeff Roberson /*
5183ed581bf6SJeff Roberson  * Returns a snapshot of memory consumption in bytes.
5184ed581bf6SJeff Roberson  */
5185ed581bf6SJeff Roberson size_t
5186ed581bf6SJeff Roberson uma_zone_memory(uma_zone_t zone)
5187ed581bf6SJeff Roberson {
5188ed581bf6SJeff Roberson 	size_t sz;
5189ed581bf6SJeff Roberson 	int i;
5190ed581bf6SJeff Roberson 
5191ed581bf6SJeff Roberson 	sz = 0;
5192ed581bf6SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_CACHE) {
5193ed581bf6SJeff Roberson 		for (i = 0; i < vm_ndomains; i++)
5194c6fd3e23SJeff Roberson 			sz += ZDOM_GET(zone, i)->uzd_nitems;
5195ed581bf6SJeff Roberson 		return (sz * zone->uz_size);
5196ed581bf6SJeff Roberson 	}
5197ed581bf6SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
5198ed581bf6SJeff Roberson 		sz += zone->uz_keg->uk_domain[i].ud_pages;
5199ed581bf6SJeff Roberson 
5200ed581bf6SJeff Roberson 	return (sz * PAGE_SIZE);
5201ed581bf6SJeff Roberson }
5202ed581bf6SJeff Roberson 
52038355f576SJeff Roberson /* See uma.h */
520408cfa56eSMark Johnston void
520508cfa56eSMark Johnston uma_reclaim(int req)
52068355f576SJeff Roberson {
5207aabe13f1SMark Johnston 	uma_reclaim_domain(req, UMA_ANYDOMAIN);
5208aabe13f1SMark Johnston }
520944ec2b63SKonstantin Belousov 
5210aabe13f1SMark Johnston void
5211aabe13f1SMark Johnston uma_reclaim_domain(int req, int domain)
5212aabe13f1SMark Johnston {
5213aabe13f1SMark Johnston 	void *arg;
5214aabe13f1SMark Johnston 
521586bbae32SJeff Roberson 	bucket_enable();
521608cfa56eSMark Johnston 
5217aabe13f1SMark Johnston 	arg = (void *)(uintptr_t)domain;
5218aabe13f1SMark Johnston 	sx_slock(&uma_reclaim_lock);
521908cfa56eSMark Johnston 	switch (req) {
522008cfa56eSMark Johnston 	case UMA_RECLAIM_TRIM:
5221aabe13f1SMark Johnston 		zone_foreach(zone_trim, arg);
522208cfa56eSMark Johnston 		break;
522308cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN:
5224aabe13f1SMark Johnston 		zone_foreach(zone_drain, arg);
5225aabe13f1SMark Johnston 		break;
522608cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN_CPU:
5227aabe13f1SMark Johnston 		zone_foreach(zone_drain, arg);
522808cfa56eSMark Johnston 		pcpu_cache_drain_safe(NULL);
5229aabe13f1SMark Johnston 		zone_foreach(zone_drain, arg);
523008cfa56eSMark Johnston 		break;
523108cfa56eSMark Johnston 	default:
523208cfa56eSMark Johnston 		panic("unhandled reclamation request %d", req);
523308cfa56eSMark Johnston 	}
52340f9b7bf3SMark Johnston 
52358355f576SJeff Roberson 	/*
52368355f576SJeff Roberson 	 * Some slabs may have been freed but this zone will be visited early
52378355f576SJeff Roberson 	 * we visit again so that we can free pages that are empty once other
52388355f576SJeff Roberson 	 * zones are drained.  We have to do the same for buckets.
52398355f576SJeff Roberson 	 */
5240aabe13f1SMark Johnston 	zone_drain(slabzones[0], arg);
5241aabe13f1SMark Johnston 	zone_drain(slabzones[1], arg);
5242aabe13f1SMark Johnston 	bucket_zone_drain(domain);
5243aabe13f1SMark Johnston 	sx_sunlock(&uma_reclaim_lock);
52448355f576SJeff Roberson }
52458355f576SJeff Roberson 
52462e47807cSJeff Roberson static volatile int uma_reclaim_needed;
524744ec2b63SKonstantin Belousov 
524844ec2b63SKonstantin Belousov void
524944ec2b63SKonstantin Belousov uma_reclaim_wakeup(void)
525044ec2b63SKonstantin Belousov {
525144ec2b63SKonstantin Belousov 
52522e47807cSJeff Roberson 	if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
52532e47807cSJeff Roberson 		wakeup(uma_reclaim);
525444ec2b63SKonstantin Belousov }
525544ec2b63SKonstantin Belousov 
525644ec2b63SKonstantin Belousov void
525744ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused)
525844ec2b63SKonstantin Belousov {
525944ec2b63SKonstantin Belousov 
526044ec2b63SKonstantin Belousov 	for (;;) {
526108cfa56eSMark Johnston 		sx_xlock(&uma_reclaim_lock);
5262200f8117SKonstantin Belousov 		while (atomic_load_int(&uma_reclaim_needed) == 0)
526308cfa56eSMark Johnston 			sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl",
52642e47807cSJeff Roberson 			    hz);
526508cfa56eSMark Johnston 		sx_xunlock(&uma_reclaim_lock);
52669b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
526708cfa56eSMark Johnston 		uma_reclaim(UMA_RECLAIM_DRAIN_CPU);
5268200f8117SKonstantin Belousov 		atomic_store_int(&uma_reclaim_needed, 0);
52692e47807cSJeff Roberson 		/* Don't fire more than once per-second. */
52702e47807cSJeff Roberson 		pause("umarclslp", hz);
527144ec2b63SKonstantin Belousov 	}
527244ec2b63SKonstantin Belousov }
527344ec2b63SKonstantin Belousov 
5274663b416fSJohn Baldwin /* See uma.h */
527508cfa56eSMark Johnston void
527608cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req)
527708cfa56eSMark Johnston {
5278aabe13f1SMark Johnston 	uma_zone_reclaim_domain(zone, req, UMA_ANYDOMAIN);
5279aabe13f1SMark Johnston }
528008cfa56eSMark Johnston 
5281aabe13f1SMark Johnston void
5282aabe13f1SMark Johnston uma_zone_reclaim_domain(uma_zone_t zone, int req, int domain)
5283aabe13f1SMark Johnston {
5284aabe13f1SMark Johnston 	void *arg;
5285aabe13f1SMark Johnston 
5286aabe13f1SMark Johnston 	arg = (void *)(uintptr_t)domain;
528708cfa56eSMark Johnston 	switch (req) {
528808cfa56eSMark Johnston 	case UMA_RECLAIM_TRIM:
5289aabe13f1SMark Johnston 		zone_trim(zone, arg);
529008cfa56eSMark Johnston 		break;
529108cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN:
5292aabe13f1SMark Johnston 		zone_drain(zone, arg);
529308cfa56eSMark Johnston 		break;
529408cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN_CPU:
529508cfa56eSMark Johnston 		pcpu_cache_drain_safe(zone);
5296aabe13f1SMark Johnston 		zone_drain(zone, arg);
529708cfa56eSMark Johnston 		break;
529808cfa56eSMark Johnston 	default:
529908cfa56eSMark Johnston 		panic("unhandled reclamation request %d", req);
530008cfa56eSMark Johnston 	}
530108cfa56eSMark Johnston }
530208cfa56eSMark Johnston 
530308cfa56eSMark Johnston /* See uma.h */
5304663b416fSJohn Baldwin int
5305663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone)
5306663b416fSJohn Baldwin {
5307663b416fSJohn Baldwin 
5308727c6918SJeff Roberson 	return (atomic_load_32(&zone->uz_sleepers) > 0);
53096c125b8dSMohan Srinivasan }
53106c125b8dSMohan Srinivasan 
53112e47807cSJeff Roberson unsigned long
53122e47807cSJeff Roberson uma_limit(void)
53132e47807cSJeff Roberson {
53142e47807cSJeff Roberson 
53152e47807cSJeff Roberson 	return (uma_kmem_limit);
53162e47807cSJeff Roberson }
53172e47807cSJeff Roberson 
53182e47807cSJeff Roberson void
53192e47807cSJeff Roberson uma_set_limit(unsigned long limit)
53202e47807cSJeff Roberson {
53212e47807cSJeff Roberson 
53222e47807cSJeff Roberson 	uma_kmem_limit = limit;
53232e47807cSJeff Roberson }
53242e47807cSJeff Roberson 
53252e47807cSJeff Roberson unsigned long
53262e47807cSJeff Roberson uma_size(void)
53272e47807cSJeff Roberson {
53282e47807cSJeff Roberson 
5329058f0f74SMark Johnston 	return (atomic_load_long(&uma_kmem_total));
5330ad5b0f5bSJeff Roberson }
5331ad5b0f5bSJeff Roberson 
5332ad5b0f5bSJeff Roberson long
5333ad5b0f5bSJeff Roberson uma_avail(void)
5334ad5b0f5bSJeff Roberson {
5335ad5b0f5bSJeff Roberson 
5336058f0f74SMark Johnston 	return (uma_kmem_limit - uma_size());
53372e47807cSJeff Roberson }
53382e47807cSJeff Roberson 
5339a0d4b0aeSRobert Watson #ifdef DDB
53408355f576SJeff Roberson /*
53417a52a97eSRobert Watson  * Generate statistics across both the zone and its per-cpu cache's.  Return
53427a52a97eSRobert Watson  * desired statistics if the pointer is non-NULL for that statistic.
53437a52a97eSRobert Watson  *
53447a52a97eSRobert Watson  * Note: does not update the zone statistics, as it can't safely clear the
53457a52a97eSRobert Watson  * per-CPU cache statistic.
53467a52a97eSRobert Watson  *
53477a52a97eSRobert Watson  */
53487a52a97eSRobert Watson static void
53490f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp,
5350c1685086SJeff Roberson     uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp)
53517a52a97eSRobert Watson {
53527a52a97eSRobert Watson 	uma_cache_t cache;
5353c1685086SJeff Roberson 	uint64_t allocs, frees, sleeps, xdomain;
53547a52a97eSRobert Watson 	int cachefree, cpu;
53557a52a97eSRobert Watson 
5356c1685086SJeff Roberson 	allocs = frees = sleeps = xdomain = 0;
53577a52a97eSRobert Watson 	cachefree = 0;
53583aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
53597a52a97eSRobert Watson 		cache = &z->uz_cpu[cpu];
5360376b1ba3SJeff Roberson 		cachefree += cache->uc_allocbucket.ucb_cnt;
5361376b1ba3SJeff Roberson 		cachefree += cache->uc_freebucket.ucb_cnt;
5362376b1ba3SJeff Roberson 		xdomain += cache->uc_crossbucket.ucb_cnt;
5363376b1ba3SJeff Roberson 		cachefree += cache->uc_crossbucket.ucb_cnt;
53647a52a97eSRobert Watson 		allocs += cache->uc_allocs;
53657a52a97eSRobert Watson 		frees += cache->uc_frees;
53667a52a97eSRobert Watson 	}
53672efcc8cbSGleb Smirnoff 	allocs += counter_u64_fetch(z->uz_allocs);
53682efcc8cbSGleb Smirnoff 	frees += counter_u64_fetch(z->uz_frees);
5369c6fd3e23SJeff Roberson 	xdomain += counter_u64_fetch(z->uz_xdomain);
5370bf965959SSean Bruno 	sleeps += z->uz_sleeps;
53717a52a97eSRobert Watson 	if (cachefreep != NULL)
53727a52a97eSRobert Watson 		*cachefreep = cachefree;
53737a52a97eSRobert Watson 	if (allocsp != NULL)
53747a52a97eSRobert Watson 		*allocsp = allocs;
53757a52a97eSRobert Watson 	if (freesp != NULL)
53767a52a97eSRobert Watson 		*freesp = frees;
5377bf965959SSean Bruno 	if (sleepsp != NULL)
5378bf965959SSean Bruno 		*sleepsp = sleeps;
5379c1685086SJeff Roberson 	if (xdomainp != NULL)
5380c1685086SJeff Roberson 		*xdomainp = xdomain;
53817a52a97eSRobert Watson }
5382a0d4b0aeSRobert Watson #endif /* DDB */
53837a52a97eSRobert Watson 
53847a52a97eSRobert Watson static int
53857a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
53867a52a97eSRobert Watson {
53877a52a97eSRobert Watson 	uma_keg_t kz;
53887a52a97eSRobert Watson 	uma_zone_t z;
53897a52a97eSRobert Watson 	int count;
53907a52a97eSRobert Watson 
53917a52a97eSRobert Watson 	count = 0;
5392111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
53937a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
53947a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
53957a52a97eSRobert Watson 			count++;
53967a52a97eSRobert Watson 	}
5397b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link)
5398b47acb0aSGleb Smirnoff 		count++;
5399b47acb0aSGleb Smirnoff 
5400111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
54017a52a97eSRobert Watson 	return (sysctl_handle_int(oidp, &count, 0, req));
54027a52a97eSRobert Watson }
54037a52a97eSRobert Watson 
5404b47acb0aSGleb Smirnoff static void
5405b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf,
5406b47acb0aSGleb Smirnoff     struct uma_percpu_stat *ups, bool internal)
5407b47acb0aSGleb Smirnoff {
5408b47acb0aSGleb Smirnoff 	uma_zone_domain_t zdom;
5409b47acb0aSGleb Smirnoff 	uma_cache_t cache;
5410b47acb0aSGleb Smirnoff 	int i;
5411b47acb0aSGleb Smirnoff 
5412b47acb0aSGleb Smirnoff 	for (i = 0; i < vm_ndomains; i++) {
5413c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(z, i);
5414b47acb0aSGleb Smirnoff 		uth->uth_zone_free += zdom->uzd_nitems;
5415b47acb0aSGleb Smirnoff 	}
5416b47acb0aSGleb Smirnoff 	uth->uth_allocs = counter_u64_fetch(z->uz_allocs);
5417b47acb0aSGleb Smirnoff 	uth->uth_frees = counter_u64_fetch(z->uz_frees);
5418b47acb0aSGleb Smirnoff 	uth->uth_fails = counter_u64_fetch(z->uz_fails);
5419c6fd3e23SJeff Roberson 	uth->uth_xdomain = counter_u64_fetch(z->uz_xdomain);
5420b47acb0aSGleb Smirnoff 	uth->uth_sleeps = z->uz_sleeps;
54211de9724eSMark Johnston 
5422b47acb0aSGleb Smirnoff 	for (i = 0; i < mp_maxid + 1; i++) {
5423b47acb0aSGleb Smirnoff 		bzero(&ups[i], sizeof(*ups));
5424b47acb0aSGleb Smirnoff 		if (internal || CPU_ABSENT(i))
5425b47acb0aSGleb Smirnoff 			continue;
5426b47acb0aSGleb Smirnoff 		cache = &z->uz_cpu[i];
5427376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt;
5428376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt;
5429376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt;
5430b47acb0aSGleb Smirnoff 		ups[i].ups_allocs = cache->uc_allocs;
5431b47acb0aSGleb Smirnoff 		ups[i].ups_frees = cache->uc_frees;
5432b47acb0aSGleb Smirnoff 	}
5433b47acb0aSGleb Smirnoff }
5434b47acb0aSGleb Smirnoff 
54357a52a97eSRobert Watson static int
54367a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
54377a52a97eSRobert Watson {
54387a52a97eSRobert Watson 	struct uma_stream_header ush;
54397a52a97eSRobert Watson 	struct uma_type_header uth;
544063b5d112SKonstantin Belousov 	struct uma_percpu_stat *ups;
54417a52a97eSRobert Watson 	struct sbuf sbuf;
54427a52a97eSRobert Watson 	uma_keg_t kz;
54437a52a97eSRobert Watson 	uma_zone_t z;
54444bd61e19SJeff Roberson 	uint64_t items;
54458b987a77SJeff Roberson 	uint32_t kfree, pages;
54464e657159SMatthew D Fleming 	int count, error, i;
54477a52a97eSRobert Watson 
544800f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
544900f0e671SMatthew D Fleming 	if (error != 0)
545000f0e671SMatthew D Fleming 		return (error);
54514e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
54521eafc078SIan Lepore 	sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
545363b5d112SKonstantin Belousov 	ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK);
54544e657159SMatthew D Fleming 
5455404a593eSMatthew D Fleming 	count = 0;
5456111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
54577a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
54587a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
54597a52a97eSRobert Watson 			count++;
54607a52a97eSRobert Watson 	}
54617a52a97eSRobert Watson 
5462b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link)
5463b47acb0aSGleb Smirnoff 		count++;
5464b47acb0aSGleb Smirnoff 
54657a52a97eSRobert Watson 	/*
54667a52a97eSRobert Watson 	 * Insert stream header.
54677a52a97eSRobert Watson 	 */
54687a52a97eSRobert Watson 	bzero(&ush, sizeof(ush));
54697a52a97eSRobert Watson 	ush.ush_version = UMA_STREAM_VERSION;
5470ab3a57c0SRobert Watson 	ush.ush_maxcpus = (mp_maxid + 1);
54717a52a97eSRobert Watson 	ush.ush_count = count;
54724e657159SMatthew D Fleming 	(void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
54737a52a97eSRobert Watson 
54747a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
54758b987a77SJeff Roberson 		kfree = pages = 0;
54768b987a77SJeff Roberson 		for (i = 0; i < vm_ndomains; i++) {
54774ab3aee8SMark Johnston 			kfree += kz->uk_domain[i].ud_free_items;
54788b987a77SJeff Roberson 			pages += kz->uk_domain[i].ud_pages;
54798b987a77SJeff Roberson 		}
54807a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
54817a52a97eSRobert Watson 			bzero(&uth, sizeof(uth));
5482cbbb4a00SRobert Watson 			strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
54837a52a97eSRobert Watson 			uth.uth_align = kz->uk_align;
54847a52a97eSRobert Watson 			uth.uth_size = kz->uk_size;
54857a52a97eSRobert Watson 			uth.uth_rsize = kz->uk_rsize;
54864bd61e19SJeff Roberson 			if (z->uz_max_items > 0) {
54874bd61e19SJeff Roberson 				items = UZ_ITEMS_COUNT(z->uz_items);
54884bd61e19SJeff Roberson 				uth.uth_pages = (items / kz->uk_ipers) *
5489bb15d1c7SGleb Smirnoff 					kz->uk_ppera;
54904bd61e19SJeff Roberson 			} else
54918b987a77SJeff Roberson 				uth.uth_pages = pages;
5492f8c86a5fSGleb Smirnoff 			uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) *
5493bb15d1c7SGleb Smirnoff 			    kz->uk_ppera;
5494bb15d1c7SGleb Smirnoff 			uth.uth_limit = z->uz_max_items;
54958b987a77SJeff Roberson 			uth.uth_keg_free = kfree;
5496cbbb4a00SRobert Watson 
5497cbbb4a00SRobert Watson 			/*
5498cbbb4a00SRobert Watson 			 * A zone is secondary is it is not the first entry
5499cbbb4a00SRobert Watson 			 * on the keg's zone list.
5500cbbb4a00SRobert Watson 			 */
5501e20a199fSJeff Roberson 			if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
5502cbbb4a00SRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z))
5503cbbb4a00SRobert Watson 				uth.uth_zone_flags = UTH_ZONE_SECONDARY;
5504b47acb0aSGleb Smirnoff 			uma_vm_zone_stats(&uth, z, &sbuf, ups,
5505b47acb0aSGleb Smirnoff 			    kz->uk_flags & UMA_ZFLAG_INTERNAL);
550663b5d112SKonstantin Belousov 			(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
550763b5d112SKonstantin Belousov 			for (i = 0; i < mp_maxid + 1; i++)
550863b5d112SKonstantin Belousov 				(void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
55097a52a97eSRobert Watson 		}
55107a52a97eSRobert Watson 	}
5511b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
5512b47acb0aSGleb Smirnoff 		bzero(&uth, sizeof(uth));
5513b47acb0aSGleb Smirnoff 		strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
5514b47acb0aSGleb Smirnoff 		uth.uth_size = z->uz_size;
5515b47acb0aSGleb Smirnoff 		uma_vm_zone_stats(&uth, z, &sbuf, ups, false);
5516b47acb0aSGleb Smirnoff 		(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
5517b47acb0aSGleb Smirnoff 		for (i = 0; i < mp_maxid + 1; i++)
5518b47acb0aSGleb Smirnoff 			(void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
5519b47acb0aSGleb Smirnoff 	}
5520b47acb0aSGleb Smirnoff 
5521111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
55224e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
55234e657159SMatthew D Fleming 	sbuf_delete(&sbuf);
552463b5d112SKonstantin Belousov 	free(ups, M_TEMP);
55257a52a97eSRobert Watson 	return (error);
55267a52a97eSRobert Watson }
552748c5777eSRobert Watson 
55280a5a3ccbSGleb Smirnoff int
55290a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
55300a5a3ccbSGleb Smirnoff {
55310a5a3ccbSGleb Smirnoff 	uma_zone_t zone = *(uma_zone_t *)arg1;
553216be9f54SGleb Smirnoff 	int error, max;
55330a5a3ccbSGleb Smirnoff 
553416be9f54SGleb Smirnoff 	max = uma_zone_get_max(zone);
55350a5a3ccbSGleb Smirnoff 	error = sysctl_handle_int(oidp, &max, 0, req);
55360a5a3ccbSGleb Smirnoff 	if (error || !req->newptr)
55370a5a3ccbSGleb Smirnoff 		return (error);
55380a5a3ccbSGleb Smirnoff 
55390a5a3ccbSGleb Smirnoff 	uma_zone_set_max(zone, max);
55400a5a3ccbSGleb Smirnoff 
55410a5a3ccbSGleb Smirnoff 	return (0);
55420a5a3ccbSGleb Smirnoff }
55430a5a3ccbSGleb Smirnoff 
55440a5a3ccbSGleb Smirnoff int
55450a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
55460a5a3ccbSGleb Smirnoff {
554720a4e154SJeff Roberson 	uma_zone_t zone;
55480a5a3ccbSGleb Smirnoff 	int cur;
55490a5a3ccbSGleb Smirnoff 
555020a4e154SJeff Roberson 	/*
555120a4e154SJeff Roberson 	 * Some callers want to add sysctls for global zones that
555220a4e154SJeff Roberson 	 * may not yet exist so they pass a pointer to a pointer.
555320a4e154SJeff Roberson 	 */
555420a4e154SJeff Roberson 	if (arg2 == 0)
555520a4e154SJeff Roberson 		zone = *(uma_zone_t *)arg1;
555620a4e154SJeff Roberson 	else
555720a4e154SJeff Roberson 		zone = arg1;
55580a5a3ccbSGleb Smirnoff 	cur = uma_zone_get_cur(zone);
55590a5a3ccbSGleb Smirnoff 	return (sysctl_handle_int(oidp, &cur, 0, req));
55600a5a3ccbSGleb Smirnoff }
55610a5a3ccbSGleb Smirnoff 
556220a4e154SJeff Roberson static int
556320a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS)
556420a4e154SJeff Roberson {
556520a4e154SJeff Roberson 	uma_zone_t zone = arg1;
556620a4e154SJeff Roberson 	uint64_t cur;
556720a4e154SJeff Roberson 
556820a4e154SJeff Roberson 	cur = uma_zone_get_allocs(zone);
556920a4e154SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
557020a4e154SJeff Roberson }
557120a4e154SJeff Roberson 
557220a4e154SJeff Roberson static int
557320a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS)
557420a4e154SJeff Roberson {
557520a4e154SJeff Roberson 	uma_zone_t zone = arg1;
557620a4e154SJeff Roberson 	uint64_t cur;
557720a4e154SJeff Roberson 
557820a4e154SJeff Roberson 	cur = uma_zone_get_frees(zone);
557920a4e154SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
558020a4e154SJeff Roberson }
558120a4e154SJeff Roberson 
55826d204a6aSRyan Libby static int
55836d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS)
55846d204a6aSRyan Libby {
55856d204a6aSRyan Libby 	struct sbuf sbuf;
55866d204a6aSRyan Libby 	uma_zone_t zone = arg1;
55876d204a6aSRyan Libby 	int error;
55886d204a6aSRyan Libby 
55896d204a6aSRyan Libby 	sbuf_new_for_sysctl(&sbuf, NULL, 0, req);
55906d204a6aSRyan Libby 	if (zone->uz_flags != 0)
55916d204a6aSRyan Libby 		sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS);
55926d204a6aSRyan Libby 	else
55936d204a6aSRyan Libby 		sbuf_printf(&sbuf, "0");
55946d204a6aSRyan Libby 	error = sbuf_finish(&sbuf);
55956d204a6aSRyan Libby 	sbuf_delete(&sbuf);
55966d204a6aSRyan Libby 
55976d204a6aSRyan Libby 	return (error);
55986d204a6aSRyan Libby }
55996d204a6aSRyan Libby 
5600f7af5015SRyan Libby static int
5601f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS)
5602f7af5015SRyan Libby {
5603f7af5015SRyan Libby 	uma_keg_t keg = arg1;
5604f7af5015SRyan Libby 	int avail, effpct, total;
5605f7af5015SRyan Libby 
5606f7af5015SRyan Libby 	total = keg->uk_ppera * PAGE_SIZE;
560754c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0)
56089b8db4d0SRyan Libby 		total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize;
5609f7af5015SRyan Libby 	/*
5610f7af5015SRyan Libby 	 * We consider the client's requested size and alignment here, not the
5611f7af5015SRyan Libby 	 * real size determination uk_rsize, because we also adjust the real
5612f7af5015SRyan Libby 	 * size for internal implementation reasons (max bitset size).
5613f7af5015SRyan Libby 	 */
5614f7af5015SRyan Libby 	avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1);
5615f7af5015SRyan Libby 	if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
5616f7af5015SRyan Libby 		avail *= mp_maxid + 1;
5617f7af5015SRyan Libby 	effpct = 100 * avail / total;
5618f7af5015SRyan Libby 	return (sysctl_handle_int(oidp, &effpct, 0, req));
5619f7af5015SRyan Libby }
5620f7af5015SRyan Libby 
56214bd61e19SJeff Roberson static int
56224bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS)
56234bd61e19SJeff Roberson {
56244bd61e19SJeff Roberson 	uma_zone_t zone = arg1;
56254bd61e19SJeff Roberson 	uint64_t cur;
56264bd61e19SJeff Roberson 
56274bd61e19SJeff Roberson 	cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items));
56284bd61e19SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
56294bd61e19SJeff Roberson }
56304bd61e19SJeff Roberson 
56319542ea7bSGleb Smirnoff #ifdef INVARIANTS
56329542ea7bSGleb Smirnoff static uma_slab_t
56339542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item)
56349542ea7bSGleb Smirnoff {
56359542ea7bSGleb Smirnoff 	uma_slab_t slab;
56369542ea7bSGleb Smirnoff 	uma_keg_t keg;
56379542ea7bSGleb Smirnoff 	uint8_t *mem;
56389542ea7bSGleb Smirnoff 
56399542ea7bSGleb Smirnoff 	/*
56409542ea7bSGleb Smirnoff 	 * It is safe to return the slab here even though the
56419542ea7bSGleb Smirnoff 	 * zone is unlocked because the item's allocation state
56429542ea7bSGleb Smirnoff 	 * essentially holds a reference.
56439542ea7bSGleb Smirnoff 	 */
5644727c6918SJeff Roberson 	mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
5645727c6918SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5646bb15d1c7SGleb Smirnoff 		return (NULL);
564754c5ae80SRyan Libby 	if (zone->uz_flags & UMA_ZFLAG_VTOSLAB)
5648727c6918SJeff Roberson 		return (vtoslab((vm_offset_t)mem));
5649bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
565054c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0)
5651727c6918SJeff Roberson 		return ((uma_slab_t)(mem + keg->uk_pgoff));
56528b987a77SJeff Roberson 	KEG_LOCK(keg, 0);
56539542ea7bSGleb Smirnoff 	slab = hash_sfind(&keg->uk_hash, mem);
56548b987a77SJeff Roberson 	KEG_UNLOCK(keg, 0);
56559542ea7bSGleb Smirnoff 
56569542ea7bSGleb Smirnoff 	return (slab);
56579542ea7bSGleb Smirnoff }
56589542ea7bSGleb Smirnoff 
5659c5deaf04SGleb Smirnoff static bool
5660c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem)
5661c5deaf04SGleb Smirnoff {
5662c5deaf04SGleb Smirnoff 
5663727c6918SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5664c5deaf04SGleb Smirnoff 		return (true);
5665c5deaf04SGleb Smirnoff 
5666bb15d1c7SGleb Smirnoff 	return (uma_dbg_kskip(zone->uz_keg, mem));
5667c5deaf04SGleb Smirnoff }
5668c5deaf04SGleb Smirnoff 
5669c5deaf04SGleb Smirnoff static bool
5670c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem)
5671c5deaf04SGleb Smirnoff {
5672c5deaf04SGleb Smirnoff 	uintptr_t idx;
5673c5deaf04SGleb Smirnoff 
5674c5deaf04SGleb Smirnoff 	if (dbg_divisor == 0)
5675c5deaf04SGleb Smirnoff 		return (true);
5676c5deaf04SGleb Smirnoff 
5677c5deaf04SGleb Smirnoff 	if (dbg_divisor == 1)
5678c5deaf04SGleb Smirnoff 		return (false);
5679c5deaf04SGleb Smirnoff 
5680c5deaf04SGleb Smirnoff 	idx = (uintptr_t)mem >> PAGE_SHIFT;
5681c5deaf04SGleb Smirnoff 	if (keg->uk_ipers > 1) {
5682c5deaf04SGleb Smirnoff 		idx *= keg->uk_ipers;
5683c5deaf04SGleb Smirnoff 		idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize;
5684c5deaf04SGleb Smirnoff 	}
5685c5deaf04SGleb Smirnoff 
5686c5deaf04SGleb Smirnoff 	if ((idx / dbg_divisor) * dbg_divisor != idx) {
5687c5deaf04SGleb Smirnoff 		counter_u64_add(uma_skip_cnt, 1);
5688c5deaf04SGleb Smirnoff 		return (true);
5689c5deaf04SGleb Smirnoff 	}
5690c5deaf04SGleb Smirnoff 	counter_u64_add(uma_dbg_cnt, 1);
5691c5deaf04SGleb Smirnoff 
5692c5deaf04SGleb Smirnoff 	return (false);
5693c5deaf04SGleb Smirnoff }
5694c5deaf04SGleb Smirnoff 
56959542ea7bSGleb Smirnoff /*
56969542ea7bSGleb Smirnoff  * Set up the slab's freei data such that uma_dbg_free can function.
56979542ea7bSGleb Smirnoff  *
56989542ea7bSGleb Smirnoff  */
56999542ea7bSGleb Smirnoff static void
57009542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
57019542ea7bSGleb Smirnoff {
57029542ea7bSGleb Smirnoff 	uma_keg_t keg;
57039542ea7bSGleb Smirnoff 	int freei;
57049542ea7bSGleb Smirnoff 
57059542ea7bSGleb Smirnoff 	if (slab == NULL) {
57069542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
57079542ea7bSGleb Smirnoff 		if (slab == NULL)
5708952c8964SMark Johnston 			panic("uma: item %p did not belong to zone %s",
57099542ea7bSGleb Smirnoff 			    item, zone->uz_name);
57109542ea7bSGleb Smirnoff 	}
5711584061b4SJeff Roberson 	keg = zone->uz_keg;
57121e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
57139542ea7bSGleb Smirnoff 
5714942951baSRyan Libby 	if (BIT_TEST_SET_ATOMIC(keg->uk_ipers, freei,
5715942951baSRyan Libby 	    slab_dbg_bits(slab, keg)))
5716952c8964SMark Johnston 		panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)",
57179542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
57189542ea7bSGleb Smirnoff }
57199542ea7bSGleb Smirnoff 
57209542ea7bSGleb Smirnoff /*
57219542ea7bSGleb Smirnoff  * Verifies freed addresses.  Checks for alignment, valid slab membership
57229542ea7bSGleb Smirnoff  * and duplicate frees.
57239542ea7bSGleb Smirnoff  *
57249542ea7bSGleb Smirnoff  */
57259542ea7bSGleb Smirnoff static void
57269542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
57279542ea7bSGleb Smirnoff {
57289542ea7bSGleb Smirnoff 	uma_keg_t keg;
57299542ea7bSGleb Smirnoff 	int freei;
57309542ea7bSGleb Smirnoff 
57319542ea7bSGleb Smirnoff 	if (slab == NULL) {
57329542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
57339542ea7bSGleb Smirnoff 		if (slab == NULL)
5734952c8964SMark Johnston 			panic("uma: Freed item %p did not belong to zone %s",
57359542ea7bSGleb Smirnoff 			    item, zone->uz_name);
57369542ea7bSGleb Smirnoff 	}
5737584061b4SJeff Roberson 	keg = zone->uz_keg;
57381e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
57399542ea7bSGleb Smirnoff 
57409542ea7bSGleb Smirnoff 	if (freei >= keg->uk_ipers)
5741952c8964SMark Johnston 		panic("Invalid free of %p from zone %p(%s) slab %p(%d)",
57429542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
57439542ea7bSGleb Smirnoff 
57441e0701e1SJeff Roberson 	if (slab_item(slab, keg, freei) != item)
5745952c8964SMark Johnston 		panic("Unaligned free of %p from zone %p(%s) slab %p(%d)",
57469542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
57479542ea7bSGleb Smirnoff 
5748942951baSRyan Libby 	if (!BIT_TEST_CLR_ATOMIC(keg->uk_ipers, freei,
5749942951baSRyan Libby 	    slab_dbg_bits(slab, keg)))
5750952c8964SMark Johnston 		panic("Duplicate free of %p from zone %p(%s) slab %p(%d)",
57519542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
57529542ea7bSGleb Smirnoff }
57539542ea7bSGleb Smirnoff #endif /* INVARIANTS */
57549542ea7bSGleb Smirnoff 
575548c5777eSRobert Watson #ifdef DDB
575646d70077SConrad Meyer static int64_t
575746d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used,
57580223790fSConrad Meyer     uint64_t *sleeps, long *cachefree, uint64_t *xdomain)
575948c5777eSRobert Watson {
576046d70077SConrad Meyer 	uint64_t frees;
57610f9b7bf3SMark Johnston 	int i;
576248c5777eSRobert Watson 
576348c5777eSRobert Watson 	if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
576446d70077SConrad Meyer 		*allocs = counter_u64_fetch(z->uz_allocs);
57652efcc8cbSGleb Smirnoff 		frees = counter_u64_fetch(z->uz_frees);
576646d70077SConrad Meyer 		*sleeps = z->uz_sleeps;
576746d70077SConrad Meyer 		*cachefree = 0;
576846d70077SConrad Meyer 		*xdomain = 0;
576948c5777eSRobert Watson 	} else
577046d70077SConrad Meyer 		uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps,
577146d70077SConrad Meyer 		    xdomain);
57728b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
5773c6fd3e23SJeff Roberson 		*cachefree += ZDOM_GET(z, i)->uzd_nitems;
5774e20a199fSJeff Roberson 		if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
577548c5777eSRobert Watson 		    (LIST_FIRST(&kz->uk_zones) != z)))
57764ab3aee8SMark Johnston 			*cachefree += kz->uk_domain[i].ud_free_items;
57778b987a77SJeff Roberson 	}
577846d70077SConrad Meyer 	*used = *allocs - frees;
577946d70077SConrad Meyer 	return (((int64_t)*used + *cachefree) * kz->uk_size);
578046d70077SConrad Meyer }
57810f9b7bf3SMark Johnston 
578246d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma)
578346d70077SConrad Meyer {
578446d70077SConrad Meyer 	const char *fmt_hdr, *fmt_entry;
578546d70077SConrad Meyer 	uma_keg_t kz;
578646d70077SConrad Meyer 	uma_zone_t z;
578746d70077SConrad Meyer 	uint64_t allocs, used, sleeps, xdomain;
578846d70077SConrad Meyer 	long cachefree;
578946d70077SConrad Meyer 	/* variables for sorting */
579046d70077SConrad Meyer 	uma_keg_t cur_keg;
579146d70077SConrad Meyer 	uma_zone_t cur_zone, last_zone;
579246d70077SConrad Meyer 	int64_t cur_size, last_size, size;
579346d70077SConrad Meyer 	int ties;
579446d70077SConrad Meyer 
579546d70077SConrad Meyer 	/* /i option produces machine-parseable CSV output */
579646d70077SConrad Meyer 	if (modif[0] == 'i') {
579746d70077SConrad Meyer 		fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n";
579846d70077SConrad Meyer 		fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n";
579946d70077SConrad Meyer 	} else {
580046d70077SConrad Meyer 		fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n";
580146d70077SConrad Meyer 		fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n";
580246d70077SConrad Meyer 	}
580346d70077SConrad Meyer 
580446d70077SConrad Meyer 	db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests",
580546d70077SConrad Meyer 	    "Sleeps", "Bucket", "Total Mem", "XFree");
580646d70077SConrad Meyer 
580746d70077SConrad Meyer 	/* Sort the zones with largest size first. */
580846d70077SConrad Meyer 	last_zone = NULL;
580946d70077SConrad Meyer 	last_size = INT64_MAX;
581046d70077SConrad Meyer 	for (;;) {
581146d70077SConrad Meyer 		cur_zone = NULL;
581246d70077SConrad Meyer 		cur_size = -1;
581346d70077SConrad Meyer 		ties = 0;
581446d70077SConrad Meyer 		LIST_FOREACH(kz, &uma_kegs, uk_link) {
581546d70077SConrad Meyer 			LIST_FOREACH(z, &kz->uk_zones, uz_link) {
581646d70077SConrad Meyer 				/*
581746d70077SConrad Meyer 				 * In the case of size ties, print out zones
581846d70077SConrad Meyer 				 * in the order they are encountered.  That is,
581946d70077SConrad Meyer 				 * when we encounter the most recently output
582046d70077SConrad Meyer 				 * zone, we have already printed all preceding
582146d70077SConrad Meyer 				 * ties, and we must print all following ties.
582246d70077SConrad Meyer 				 */
582346d70077SConrad Meyer 				if (z == last_zone) {
582446d70077SConrad Meyer 					ties = 1;
582546d70077SConrad Meyer 					continue;
582646d70077SConrad Meyer 				}
582746d70077SConrad Meyer 				size = get_uma_stats(kz, z, &allocs, &used,
582846d70077SConrad Meyer 				    &sleeps, &cachefree, &xdomain);
582946d70077SConrad Meyer 				if (size > cur_size && size < last_size + ties)
583046d70077SConrad Meyer 				{
583146d70077SConrad Meyer 					cur_size = size;
583246d70077SConrad Meyer 					cur_zone = z;
583346d70077SConrad Meyer 					cur_keg = kz;
583446d70077SConrad Meyer 				}
583546d70077SConrad Meyer 			}
583646d70077SConrad Meyer 		}
583746d70077SConrad Meyer 		if (cur_zone == NULL)
583846d70077SConrad Meyer 			break;
583946d70077SConrad Meyer 
584046d70077SConrad Meyer 		size = get_uma_stats(cur_keg, cur_zone, &allocs, &used,
584146d70077SConrad Meyer 		    &sleeps, &cachefree, &xdomain);
584246d70077SConrad Meyer 		db_printf(fmt_entry, cur_zone->uz_name,
584346d70077SConrad Meyer 		    (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree,
584446d70077SConrad Meyer 		    (uintmax_t)allocs, (uintmax_t)sleeps,
584520a4e154SJeff Roberson 		    (unsigned)cur_zone->uz_bucket_size, (intmax_t)size,
584620a4e154SJeff Roberson 		    xdomain);
584746d70077SConrad Meyer 
5848687c94aaSJohn Baldwin 		if (db_pager_quit)
5849687c94aaSJohn Baldwin 			return;
585046d70077SConrad Meyer 		last_zone = cur_zone;
585146d70077SConrad Meyer 		last_size = cur_size;
585248c5777eSRobert Watson 	}
585348c5777eSRobert Watson }
585403175483SAlexander Motin 
585503175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache)
585603175483SAlexander Motin {
585703175483SAlexander Motin 	uma_zone_t z;
5858ab3185d1SJeff Roberson 	uint64_t allocs, frees;
58590f9b7bf3SMark Johnston 	long cachefree;
58600f9b7bf3SMark Johnston 	int i;
586103175483SAlexander Motin 
586203175483SAlexander Motin 	db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
586303175483SAlexander Motin 	    "Requests", "Bucket");
586403175483SAlexander Motin 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
5865c1685086SJeff Roberson 		uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL);
58660f9b7bf3SMark Johnston 		for (i = 0; i < vm_ndomains; i++)
5867c6fd3e23SJeff Roberson 			cachefree += ZDOM_GET(z, i)->uzd_nitems;
58680f9b7bf3SMark Johnston 		db_printf("%18s %8ju %8jd %8ld %12ju %8u\n",
586903175483SAlexander Motin 		    z->uz_name, (uintmax_t)z->uz_size,
587003175483SAlexander Motin 		    (intmax_t)(allocs - frees), cachefree,
587120a4e154SJeff Roberson 		    (uintmax_t)allocs, z->uz_bucket_size);
587203175483SAlexander Motin 		if (db_pager_quit)
587303175483SAlexander Motin 			return;
587403175483SAlexander Motin 	}
587503175483SAlexander Motin }
58769542ea7bSGleb Smirnoff #endif	/* DDB */
5877