xref: /freebsd/sys/vm/uma_core.c (revision 2dba2288aa364c36c756770ac77590816b15d954)
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  */
21093cd28eaSMark Johnston static struct timeout_task uma_timeout_task;
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);
31493cd28eaSMark Johnston static void uma_timeout(void *, int);
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;
1054a04ce833SMark Johnston 	smr_seq_t seq;
1055c6fd3e23SJeff Roberson 
1056c6fd3e23SJeff Roberson 	/*
1057c6fd3e23SJeff Roberson 	 * Avoid the lock if possible.
1058c6fd3e23SJeff Roberson 	 */
1059c6fd3e23SJeff Roberson 	zdom = ZDOM_GET(zone, domain);
1060c6fd3e23SJeff Roberson 	if (zdom->uzd_nitems == 0)
1061c6fd3e23SJeff Roberson 		return (NULL);
1062c6fd3e23SJeff Roberson 
1063c6fd3e23SJeff Roberson 	if ((cache_uz_flags(cache) & UMA_ZONE_SMR) != 0 &&
1064a04ce833SMark Johnston 	    (seq = atomic_load_32(&zdom->uzd_seq)) != SMR_SEQ_INVALID &&
1065a04ce833SMark Johnston 	    !smr_poll(zone->uz_smr, seq, false))
1066c6fd3e23SJeff Roberson 		return (NULL);
1067c6fd3e23SJeff Roberson 
1068c6fd3e23SJeff Roberson 	/*
1069c6fd3e23SJeff Roberson 	 * Check the zone's cache of buckets.
1070c6fd3e23SJeff Roberson 	 */
1071c6fd3e23SJeff Roberson 	zdom = zone_domain_lock(zone, domain);
107206d8bdcbSMark Johnston 	if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL)
1073c6fd3e23SJeff Roberson 		return (bucket);
1074c6fd3e23SJeff Roberson 	ZDOM_UNLOCK(zdom);
1075c6fd3e23SJeff Roberson 
1076c6fd3e23SJeff Roberson 	return (NULL);
1077c6fd3e23SJeff Roberson }
1078c6fd3e23SJeff Roberson 
10792f891cd5SPawel Jakub Dawidek static void
10802f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone)
10812f891cd5SPawel Jakub Dawidek {
10822f891cd5SPawel Jakub Dawidek 	static const struct timeval warninterval = { 300, 0 };
10832f891cd5SPawel Jakub Dawidek 
10842f891cd5SPawel Jakub Dawidek 	if (!zone_warnings || zone->uz_warning == NULL)
10852f891cd5SPawel Jakub Dawidek 		return;
10862f891cd5SPawel Jakub Dawidek 
10872f891cd5SPawel Jakub Dawidek 	if (ratecheck(&zone->uz_ratecheck, &warninterval))
10882f891cd5SPawel Jakub Dawidek 		printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
10892f891cd5SPawel Jakub Dawidek }
10902f891cd5SPawel Jakub Dawidek 
109154503a13SJonathan T. Looney static inline void
109254503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone)
109354503a13SJonathan T. Looney {
1094e60b2fcbSGleb Smirnoff 
1095e60b2fcbSGleb Smirnoff 	if (zone->uz_maxaction.ta_func != NULL)
1096e60b2fcbSGleb Smirnoff 		taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction);
109754503a13SJonathan T. Looney }
109854503a13SJonathan T. Looney 
10998355f576SJeff Roberson /*
11008355f576SJeff Roberson  * Routine called by timeout which is used to fire off some time interval
11019643769aSJeff Roberson  * based calculations.  (stats, hash size, etc.)
11028355f576SJeff Roberson  *
11038355f576SJeff Roberson  * Arguments:
11048355f576SJeff Roberson  *	arg   Unused
11058355f576SJeff Roberson  *
11068355f576SJeff Roberson  * Returns:
11078355f576SJeff Roberson  *	Nothing
11088355f576SJeff Roberson  */
11098355f576SJeff Roberson static void
111031508912SMark Johnston uma_timeout(void *context __unused, int pending __unused)
11118355f576SJeff Roberson {
111286bbae32SJeff Roberson 	bucket_enable();
111320a4e154SJeff Roberson 	zone_foreach(zone_timeout, NULL);
11148355f576SJeff Roberson 
11158355f576SJeff Roberson 	/* Reschedule this event */
111693cd28eaSMark Johnston 	taskqueue_enqueue_timeout(taskqueue_thread, &uma_timeout_task,
111793cd28eaSMark Johnston 	    UMA_TIMEOUT * hz);
11188355f576SJeff Roberson }
11198355f576SJeff Roberson 
11208355f576SJeff Roberson /*
11212760658bSAlexander Motin  * Update the working set size estimates for the zone's bucket cache.
11222760658bSAlexander Motin  * The constants chosen here are somewhat arbitrary.
11230f9b7bf3SMark Johnston  */
11240f9b7bf3SMark Johnston static void
11250f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom)
11260f9b7bf3SMark Johnston {
11272760658bSAlexander Motin 	long m;
11280f9b7bf3SMark Johnston 
11292760658bSAlexander Motin 	ZDOM_LOCK_ASSERT(zdom);
11302760658bSAlexander Motin 	MPASS(zdom->uzd_imax >= zdom->uzd_nitems);
11312760658bSAlexander Motin 	MPASS(zdom->uzd_nitems >= zdom->uzd_bimin);
11322760658bSAlexander Motin 	MPASS(zdom->uzd_bimin >= zdom->uzd_imin);
11332760658bSAlexander Motin 
11342760658bSAlexander Motin 	/*
11352760658bSAlexander Motin 	 * Estimate WSS as modified moving average of biggest allocation
11362760658bSAlexander Motin 	 * batches for each period over few minutes (UMA_TIMEOUT of 20s).
11372760658bSAlexander Motin 	 */
11382760658bSAlexander Motin 	zdom->uzd_wss = lmax(zdom->uzd_wss * 3 / 4,
11392760658bSAlexander Motin 	    zdom->uzd_imax - zdom->uzd_bimin);
11402760658bSAlexander Motin 
11412760658bSAlexander Motin 	/*
11422760658bSAlexander Motin 	 * Estimate longtime minimum item count as a combination of recent
11432760658bSAlexander Motin 	 * minimum item count, adjusted by WSS for safety, and the modified
11442760658bSAlexander Motin 	 * moving average over the last several hours (UMA_TIMEOUT of 20s).
11452760658bSAlexander Motin 	 * timin measures time since limin tried to go negative, that means
11462760658bSAlexander Motin 	 * we were dangerously close to or got out of cache.
11472760658bSAlexander Motin 	 */
11482760658bSAlexander Motin 	m = zdom->uzd_imin - zdom->uzd_wss;
11492760658bSAlexander Motin 	if (m >= 0) {
11502760658bSAlexander Motin 		if (zdom->uzd_limin >= m)
11512760658bSAlexander Motin 			zdom->uzd_limin = m;
11522760658bSAlexander Motin 		else
11532760658bSAlexander Motin 			zdom->uzd_limin = (m + zdom->uzd_limin * 255) / 256;
11542760658bSAlexander Motin 		zdom->uzd_timin++;
11552760658bSAlexander Motin 	} else {
11562760658bSAlexander Motin 		zdom->uzd_limin = 0;
11572760658bSAlexander Motin 		zdom->uzd_timin = 0;
11582760658bSAlexander Motin 	}
11592760658bSAlexander Motin 
11602760658bSAlexander Motin 	/* To reduce period edge effects on WSS keep half of the imax. */
11612760658bSAlexander Motin 	atomic_subtract_long(&zdom->uzd_imax,
11622760658bSAlexander Motin 	    (zdom->uzd_imax - zdom->uzd_nitems + 1) / 2);
11632760658bSAlexander Motin 	zdom->uzd_imin = zdom->uzd_bimin = zdom->uzd_nitems;
11640f9b7bf3SMark Johnston }
11650f9b7bf3SMark Johnston 
11660f9b7bf3SMark Johnston /*
11679643769aSJeff Roberson  * Routine to perform timeout driven calculations.  This expands the
11689643769aSJeff Roberson  * hashes and does per cpu statistics aggregation.
11698355f576SJeff Roberson  *
1170e20a199fSJeff Roberson  *  Returns nothing.
11718355f576SJeff Roberson  */
11728355f576SJeff Roberson static void
117320a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused)
11748355f576SJeff Roberson {
117508034d10SKonstantin Belousov 	uma_keg_t keg;
11768b987a77SJeff Roberson 	u_int slabs, pages;
11778355f576SJeff Roberson 
117854c5ae80SRyan Libby 	if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0)
11792760658bSAlexander Motin 		goto trim;
118008034d10SKonstantin Belousov 
118108034d10SKonstantin Belousov 	keg = zone->uz_keg;
11828b987a77SJeff Roberson 
11838b987a77SJeff Roberson 	/*
11848b987a77SJeff Roberson 	 * Hash zones are non-numa by definition so the first domain
11858b987a77SJeff Roberson 	 * is the only one present.
11868b987a77SJeff Roberson 	 */
11878b987a77SJeff Roberson 	KEG_LOCK(keg, 0);
11888b987a77SJeff Roberson 	pages = keg->uk_domain[0].ud_pages;
11898b987a77SJeff Roberson 
11908355f576SJeff Roberson 	/*
1191e20a199fSJeff Roberson 	 * Expand the keg hash table.
11928355f576SJeff Roberson 	 *
11938355f576SJeff Roberson 	 * This is done if the number of slabs is larger than the hash size.
11948355f576SJeff Roberson 	 * What I'm trying to do here is completely reduce collisions.  This
11958355f576SJeff Roberson 	 * may be a little aggressive.  Should I allow for two collisions max?
11968355f576SJeff Roberson 	 */
11978b987a77SJeff Roberson 	if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) {
11980aef6126SJeff Roberson 		struct uma_hash newhash;
11990aef6126SJeff Roberson 		struct uma_hash oldhash;
12000aef6126SJeff Roberson 		int ret;
12015300d9ddSJeff Roberson 
12020aef6126SJeff Roberson 		/*
12030aef6126SJeff Roberson 		 * This is so involved because allocating and freeing
1204e20a199fSJeff Roberson 		 * while the keg lock is held will lead to deadlock.
12050aef6126SJeff Roberson 		 * I have to do everything in stages and check for
12060aef6126SJeff Roberson 		 * races.
12070aef6126SJeff Roberson 		 */
12088b987a77SJeff Roberson 		KEG_UNLOCK(keg, 0);
12093b2f2cb8SAlexander Motin 		ret = hash_alloc(&newhash, 1 << fls(slabs));
12108b987a77SJeff Roberson 		KEG_LOCK(keg, 0);
12110aef6126SJeff Roberson 		if (ret) {
1212099a0e58SBosko Milekic 			if (hash_expand(&keg->uk_hash, &newhash)) {
1213099a0e58SBosko Milekic 				oldhash = keg->uk_hash;
1214099a0e58SBosko Milekic 				keg->uk_hash = newhash;
12150aef6126SJeff Roberson 			} else
12160aef6126SJeff Roberson 				oldhash = newhash;
12170aef6126SJeff Roberson 
12188b987a77SJeff Roberson 			KEG_UNLOCK(keg, 0);
12190aef6126SJeff Roberson 			hash_free(&oldhash);
12202760658bSAlexander Motin 			goto trim;
12210aef6126SJeff Roberson 		}
12225300d9ddSJeff Roberson 	}
12238b987a77SJeff Roberson 	KEG_UNLOCK(keg, 0);
1224e20a199fSJeff Roberson 
12252760658bSAlexander Motin trim:
12262760658bSAlexander Motin 	/* Trim caches not used for a long time. */
1227389a3fa6SMark Johnston 	if ((zone->uz_flags & UMA_ZONE_UNMANAGED) == 0) {
12282760658bSAlexander Motin 		for (int i = 0; i < vm_ndomains; i++) {
12292760658bSAlexander Motin 			if (bucket_cache_reclaim_domain(zone, false, false, i) &&
12302760658bSAlexander Motin 			    (zone->uz_flags & UMA_ZFLAG_CACHE) == 0)
12312760658bSAlexander Motin 				keg_drain(zone->uz_keg, i);
12322760658bSAlexander Motin 		}
12338355f576SJeff Roberson 	}
1234389a3fa6SMark Johnston }
12358355f576SJeff Roberson 
12368355f576SJeff Roberson /*
12375300d9ddSJeff Roberson  * Allocate and zero fill the next sized hash table from the appropriate
12385300d9ddSJeff Roberson  * backing store.
12395300d9ddSJeff Roberson  *
12405300d9ddSJeff Roberson  * Arguments:
12410aef6126SJeff Roberson  *	hash  A new hash structure with the old hash size in uh_hashsize
12425300d9ddSJeff Roberson  *
12435300d9ddSJeff Roberson  * Returns:
1244763df3ecSPedro F. Giffuni  *	1 on success and 0 on failure.
12455300d9ddSJeff Roberson  */
124637c84183SPoul-Henning Kamp static int
12473b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size)
12485300d9ddSJeff Roberson {
124959568a0eSAlexander Motin 	size_t alloc;
12505300d9ddSJeff Roberson 
12513b2f2cb8SAlexander Motin 	KASSERT(powerof2(size), ("hash size must be power of 2"));
12523b2f2cb8SAlexander Motin 	if (size > UMA_HASH_SIZE_INIT)  {
12533b2f2cb8SAlexander Motin 		hash->uh_hashsize = size;
12540aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
12551e0701e1SJeff Roberson 		hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT);
12565300d9ddSJeff Roberson 	} else {
12570aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
1258e20a199fSJeff Roberson 		hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
1259ab3185d1SJeff Roberson 		    UMA_ANYDOMAIN, M_WAITOK);
12600aef6126SJeff Roberson 		hash->uh_hashsize = UMA_HASH_SIZE_INIT;
12615300d9ddSJeff Roberson 	}
12620aef6126SJeff Roberson 	if (hash->uh_slab_hash) {
12630aef6126SJeff Roberson 		bzero(hash->uh_slab_hash, alloc);
12640aef6126SJeff Roberson 		hash->uh_hashmask = hash->uh_hashsize - 1;
12650aef6126SJeff Roberson 		return (1);
12660aef6126SJeff Roberson 	}
12675300d9ddSJeff Roberson 
12680aef6126SJeff Roberson 	return (0);
12695300d9ddSJeff Roberson }
12705300d9ddSJeff Roberson 
12715300d9ddSJeff Roberson /*
127264f051e9SJeff Roberson  * Expands the hash table for HASH zones.  This is done from zone_timeout
127364f051e9SJeff Roberson  * to reduce collisions.  This must not be done in the regular allocation
127464f051e9SJeff Roberson  * path, otherwise, we can recurse on the vm while allocating pages.
12758355f576SJeff Roberson  *
12768355f576SJeff Roberson  * Arguments:
12770aef6126SJeff Roberson  *	oldhash  The hash you want to expand
12780aef6126SJeff Roberson  *	newhash  The hash structure for the new table
12798355f576SJeff Roberson  *
12808355f576SJeff Roberson  * Returns:
12818355f576SJeff Roberson  *	Nothing
12828355f576SJeff Roberson  *
12838355f576SJeff Roberson  * Discussion:
12848355f576SJeff Roberson  */
12850aef6126SJeff Roberson static int
12860aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
12878355f576SJeff Roberson {
12881e0701e1SJeff Roberson 	uma_hash_slab_t slab;
12896929b7d1SPedro F. Giffuni 	u_int hval;
12906929b7d1SPedro F. Giffuni 	u_int idx;
12918355f576SJeff Roberson 
12920aef6126SJeff Roberson 	if (!newhash->uh_slab_hash)
12930aef6126SJeff Roberson 		return (0);
12948355f576SJeff Roberson 
12950aef6126SJeff Roberson 	if (oldhash->uh_hashsize >= newhash->uh_hashsize)
12960aef6126SJeff Roberson 		return (0);
12978355f576SJeff Roberson 
12988355f576SJeff Roberson 	/*
12998355f576SJeff Roberson 	 * I need to investigate hash algorithms for resizing without a
13008355f576SJeff Roberson 	 * full rehash.
13018355f576SJeff Roberson 	 */
13028355f576SJeff Roberson 
13036929b7d1SPedro F. Giffuni 	for (idx = 0; idx < oldhash->uh_hashsize; idx++)
13041e0701e1SJeff Roberson 		while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) {
13051e0701e1SJeff Roberson 			slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]);
13061e0701e1SJeff Roberson 			LIST_REMOVE(slab, uhs_hlink);
13071e0701e1SJeff Roberson 			hval = UMA_HASH(newhash, slab->uhs_data);
13081e0701e1SJeff Roberson 			LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
13091e0701e1SJeff Roberson 			    slab, uhs_hlink);
13108355f576SJeff Roberson 		}
13118355f576SJeff Roberson 
13120aef6126SJeff Roberson 	return (1);
13139c2cd7e5SJeff Roberson }
13149c2cd7e5SJeff Roberson 
13155300d9ddSJeff Roberson /*
13165300d9ddSJeff Roberson  * Free the hash bucket to the appropriate backing store.
13175300d9ddSJeff Roberson  *
13185300d9ddSJeff Roberson  * Arguments:
13195300d9ddSJeff Roberson  *	slab_hash  The hash bucket we're freeing
13205300d9ddSJeff Roberson  *	hashsize   The number of entries in that hash bucket
13215300d9ddSJeff Roberson  *
13225300d9ddSJeff Roberson  * Returns:
13235300d9ddSJeff Roberson  *	Nothing
13245300d9ddSJeff Roberson  */
13259c2cd7e5SJeff Roberson static void
13260aef6126SJeff Roberson hash_free(struct uma_hash *hash)
13279c2cd7e5SJeff Roberson {
13280aef6126SJeff Roberson 	if (hash->uh_slab_hash == NULL)
13290aef6126SJeff Roberson 		return;
13300aef6126SJeff Roberson 	if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
13310095a784SJeff Roberson 		zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
13328355f576SJeff Roberson 	else
1333961647dfSJeff Roberson 		free(hash->uh_slab_hash, M_UMAHASH);
13348355f576SJeff Roberson }
13358355f576SJeff Roberson 
13368355f576SJeff Roberson /*
13378355f576SJeff Roberson  * Frees all outstanding items in a bucket
13388355f576SJeff Roberson  *
13398355f576SJeff Roberson  * Arguments:
13408355f576SJeff Roberson  *	zone   The zone to free to, must be unlocked.
13414bd61e19SJeff Roberson  *	bucket The free/alloc bucket with items.
13428355f576SJeff Roberson  *
13438355f576SJeff Roberson  * Returns:
13448355f576SJeff Roberson  *	Nothing
13458355f576SJeff Roberson  */
13468355f576SJeff Roberson static void
13478355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
13488355f576SJeff Roberson {
13490095a784SJeff Roberson 	int i;
13508355f576SJeff Roberson 
1351c6fd3e23SJeff Roberson 	if (bucket->ub_cnt == 0)
13528355f576SJeff Roberson 		return;
13538355f576SJeff Roberson 
1354d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0 &&
1355d4665eaaSJeff Roberson 	    bucket->ub_seq != SMR_SEQ_INVALID) {
1356d4665eaaSJeff Roberson 		smr_wait(zone->uz_smr, bucket->ub_seq);
1357543117beSJeff Roberson 		bucket->ub_seq = SMR_SEQ_INVALID;
1358d4665eaaSJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
1359d4665eaaSJeff Roberson 			item_dtor(zone, bucket->ub_bucket[i],
1360d4665eaaSJeff Roberson 			    zone->uz_size, NULL, SKIP_NONE);
1361d4665eaaSJeff Roberson 	}
13620095a784SJeff Roberson 	if (zone->uz_fini)
136309c8cb71SMark Johnston 		for (i = 0; i < bucket->ub_cnt; i++) {
136409c8cb71SMark Johnston 			kasan_mark_item_valid(zone, bucket->ub_bucket[i]);
13650095a784SJeff Roberson 			zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
136609c8cb71SMark Johnston 			kasan_mark_item_invalid(zone, bucket->ub_bucket[i]);
136709c8cb71SMark Johnston 		}
13680095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
13694bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
13704bd61e19SJeff Roberson 		zone_free_limit(zone, bucket->ub_cnt);
1371d4665eaaSJeff Roberson #ifdef INVARIANTS
1372d4665eaaSJeff Roberson 	bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt);
1373d4665eaaSJeff Roberson #endif
13740095a784SJeff Roberson 	bucket->ub_cnt = 0;
13758355f576SJeff Roberson }
13768355f576SJeff Roberson 
13778355f576SJeff Roberson /*
13788355f576SJeff Roberson  * Drains the per cpu caches for a zone.
13798355f576SJeff Roberson  *
1380727c6918SJeff Roberson  * NOTE: This may only be called while the zone is being torn down, and not
13815d1ae027SRobert Watson  * during normal operation.  This is necessary in order that we do not have
13825d1ae027SRobert Watson  * to migrate CPUs to drain the per-CPU caches.
13835d1ae027SRobert Watson  *
13848355f576SJeff Roberson  * Arguments:
13858355f576SJeff Roberson  *	zone     The zone to drain, must be unlocked.
13868355f576SJeff Roberson  *
13878355f576SJeff Roberson  * Returns:
13888355f576SJeff Roberson  *	Nothing
13898355f576SJeff Roberson  */
13908355f576SJeff Roberson static void
13919643769aSJeff Roberson cache_drain(uma_zone_t zone)
13928355f576SJeff Roberson {
13938355f576SJeff Roberson 	uma_cache_t cache;
1394376b1ba3SJeff Roberson 	uma_bucket_t bucket;
1395543117beSJeff Roberson 	smr_seq_t seq;
13968355f576SJeff Roberson 	int cpu;
13978355f576SJeff Roberson 
13988355f576SJeff Roberson 	/*
13995d1ae027SRobert Watson 	 * XXX: It is safe to not lock the per-CPU caches, because we're
14005d1ae027SRobert Watson 	 * tearing down the zone anyway.  I.e., there will be no further use
14015d1ae027SRobert Watson 	 * of the caches at this point.
14025d1ae027SRobert Watson 	 *
14035d1ae027SRobert Watson 	 * XXX: It would good to be able to assert that the zone is being
14045d1ae027SRobert Watson 	 * torn down to prevent improper use of cache_drain().
14058355f576SJeff Roberson 	 */
1406543117beSJeff Roberson 	seq = SMR_SEQ_INVALID;
1407543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
1408226dd6dbSJeff Roberson 		seq = smr_advance(zone->uz_smr);
14093aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
14108355f576SJeff Roberson 		cache = &zone->uz_cpu[cpu];
1411376b1ba3SJeff Roberson 		bucket = cache_bucket_unload_alloc(cache);
1412c6fd3e23SJeff Roberson 		if (bucket != NULL)
1413376b1ba3SJeff Roberson 			bucket_free(zone, bucket, NULL);
1414376b1ba3SJeff Roberson 		bucket = cache_bucket_unload_free(cache);
1415376b1ba3SJeff Roberson 		if (bucket != NULL) {
1416543117beSJeff Roberson 			bucket->ub_seq = seq;
1417376b1ba3SJeff Roberson 			bucket_free(zone, bucket, NULL);
1418376b1ba3SJeff Roberson 		}
1419376b1ba3SJeff Roberson 		bucket = cache_bucket_unload_cross(cache);
1420376b1ba3SJeff Roberson 		if (bucket != NULL) {
1421543117beSJeff Roberson 			bucket->ub_seq = seq;
1422376b1ba3SJeff Roberson 			bucket_free(zone, bucket, NULL);
1423376b1ba3SJeff Roberson 		}
1424d56368d7SBosko Milekic 	}
1425aabe13f1SMark Johnston 	bucket_cache_reclaim(zone, true, UMA_ANYDOMAIN);
1426aaa8bb16SJeff Roberson }
1427aaa8bb16SJeff Roberson 
1428a2de44abSAlexander Motin static void
142920a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused)
1430a2de44abSAlexander Motin {
1431a2de44abSAlexander Motin 
1432a2de44abSAlexander Motin 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
1433a2de44abSAlexander Motin 		return;
1434a2de44abSAlexander Motin 
1435aabe13f1SMark Johnston 	ZONE_LOCK(zone);
143620a4e154SJeff Roberson 	zone->uz_bucket_size =
143720a4e154SJeff Roberson 	    (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2;
1438aabe13f1SMark Johnston 	ZONE_UNLOCK(zone);
1439a2de44abSAlexander Motin }
1440a2de44abSAlexander Motin 
1441a2de44abSAlexander Motin static void
144220a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused)
1443a2de44abSAlexander Motin {
1444a2de44abSAlexander Motin 	uma_cache_t cache;
1445c1685086SJeff Roberson 	uma_bucket_t b1, b2, b3;
1446ab3185d1SJeff Roberson 	int domain;
1447a2de44abSAlexander Motin 
1448a2de44abSAlexander Motin 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
1449a2de44abSAlexander Motin 		return;
1450a2de44abSAlexander Motin 
1451c1685086SJeff Roberson 	b1 = b2 = b3 = NULL;
1452a2de44abSAlexander Motin 	critical_enter();
1453a2de44abSAlexander Motin 	cache = &zone->uz_cpu[curcpu];
1454c6fd3e23SJeff Roberson 	domain = PCPU_GET(domain);
1455376b1ba3SJeff Roberson 	b1 = cache_bucket_unload_alloc(cache);
1456d4665eaaSJeff Roberson 
1457d4665eaaSJeff Roberson 	/*
1458d4665eaaSJeff Roberson 	 * Don't flush SMR zone buckets.  This leaves the zone without a
1459d4665eaaSJeff Roberson 	 * bucket and forces every free to synchronize().
1460d4665eaaSJeff Roberson 	 */
1461543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0) {
1462376b1ba3SJeff Roberson 		b2 = cache_bucket_unload_free(cache);
1463543117beSJeff Roberson 		b3 = cache_bucket_unload_cross(cache);
1464543117beSJeff Roberson 	}
1465543117beSJeff Roberson 	critical_exit();
1466543117beSJeff Roberson 
1467543117beSJeff Roberson 	if (b1 != NULL)
1468c6fd3e23SJeff Roberson 		zone_free_bucket(zone, b1, NULL, domain, false);
1469543117beSJeff Roberson 	if (b2 != NULL)
1470c6fd3e23SJeff Roberson 		zone_free_bucket(zone, b2, NULL, domain, false);
1471543117beSJeff Roberson 	if (b3 != NULL) {
1472c6fd3e23SJeff Roberson 		/* Adjust the domain so it goes to zone_free_cross. */
1473c6fd3e23SJeff Roberson 		domain = (domain + 1) % vm_ndomains;
1474c6fd3e23SJeff Roberson 		zone_free_bucket(zone, b3, NULL, domain, false);
1475c1685086SJeff Roberson 	}
1476a2de44abSAlexander Motin }
1477a2de44abSAlexander Motin 
1478a2de44abSAlexander Motin /*
1479a2de44abSAlexander Motin  * Safely drain per-CPU caches of a zone(s) to alloc bucket.
1480a2de44abSAlexander Motin  * This is an expensive call because it needs to bind to all CPUs
1481a2de44abSAlexander Motin  * one by one and enter a critical section on each of them in order
1482a2de44abSAlexander Motin  * to safely access their cache buckets.
1483a2de44abSAlexander Motin  * Zone lock must not be held on call this function.
1484a2de44abSAlexander Motin  */
1485a2de44abSAlexander Motin static void
148608cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone)
1487a2de44abSAlexander Motin {
1488a2de44abSAlexander Motin 	int cpu;
1489a2de44abSAlexander Motin 
1490a2de44abSAlexander Motin 	/*
1491727c6918SJeff Roberson 	 * Polite bucket sizes shrinking was not enough, shrink aggressively.
1492a2de44abSAlexander Motin 	 */
1493a2de44abSAlexander Motin 	if (zone)
149420a4e154SJeff Roberson 		cache_shrink(zone, NULL);
1495a2de44abSAlexander Motin 	else
149620a4e154SJeff Roberson 		zone_foreach(cache_shrink, NULL);
1497a2de44abSAlexander Motin 
1498a2de44abSAlexander Motin 	CPU_FOREACH(cpu) {
1499a2de44abSAlexander Motin 		thread_lock(curthread);
1500a2de44abSAlexander Motin 		sched_bind(curthread, cpu);
1501a2de44abSAlexander Motin 		thread_unlock(curthread);
1502a2de44abSAlexander Motin 
1503a2de44abSAlexander Motin 		if (zone)
150420a4e154SJeff Roberson 			cache_drain_safe_cpu(zone, NULL);
1505a2de44abSAlexander Motin 		else
150620a4e154SJeff Roberson 			zone_foreach(cache_drain_safe_cpu, NULL);
1507a2de44abSAlexander Motin 	}
1508a2de44abSAlexander Motin 	thread_lock(curthread);
1509a2de44abSAlexander Motin 	sched_unbind(curthread);
1510a2de44abSAlexander Motin 	thread_unlock(curthread);
1511a2de44abSAlexander Motin }
1512a2de44abSAlexander Motin 
1513aaa8bb16SJeff Roberson /*
151408cfa56eSMark Johnston  * Reclaim cached buckets from a zone.  All buckets are reclaimed if the caller
151508cfa56eSMark Johnston  * requested a drain, otherwise the per-domain caches are trimmed to either
151608cfa56eSMark Johnston  * estimated working set size.
1517aaa8bb16SJeff Roberson  */
15182760658bSAlexander Motin static bool
15192760658bSAlexander Motin bucket_cache_reclaim_domain(uma_zone_t zone, bool drain, bool trim, int domain)
1520aaa8bb16SJeff Roberson {
1521ab3185d1SJeff Roberson 	uma_zone_domain_t zdom;
1522aaa8bb16SJeff Roberson 	uma_bucket_t bucket;
1523c6fd3e23SJeff Roberson 	long target;
15242760658bSAlexander Motin 	bool done = false;
15258355f576SJeff Roberson 
1526c6fd3e23SJeff Roberson 	/*
152791d947bfSJeff Roberson 	 * The cross bucket is partially filled and not part of
152891d947bfSJeff Roberson 	 * the item count.  Reclaim it individually here.
152991d947bfSJeff Roberson 	 */
153054f421f9SMark Johnston 	zdom = ZDOM_GET(zone, domain);
1531226dd6dbSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) == 0 || drain) {
153291d947bfSJeff Roberson 		ZONE_CROSS_LOCK(zone);
153391d947bfSJeff Roberson 		bucket = zdom->uzd_cross;
153491d947bfSJeff Roberson 		zdom->uzd_cross = NULL;
153591d947bfSJeff Roberson 		ZONE_CROSS_UNLOCK(zone);
1536c6fd3e23SJeff Roberson 		if (bucket != NULL)
153791d947bfSJeff Roberson 			bucket_free(zone, bucket, NULL);
153891d947bfSJeff Roberson 	}
153991d947bfSJeff Roberson 
154091d947bfSJeff Roberson 	/*
154108cfa56eSMark Johnston 	 * If we were asked to drain the zone, we are done only once
15422760658bSAlexander Motin 	 * this bucket cache is empty.  If trim, we reclaim items in
15432760658bSAlexander Motin 	 * excess of the zone's estimated working set size.  Multiple
15442760658bSAlexander Motin 	 * consecutive calls will shrink the WSS and so reclaim more.
15452760658bSAlexander Motin 	 * If neither drain nor trim, then voluntarily reclaim 1/4
15462760658bSAlexander Motin 	 * (to reduce first spike) of items not used for a long time.
154708cfa56eSMark Johnston 	 */
1548c6fd3e23SJeff Roberson 	ZDOM_LOCK(zdom);
15492760658bSAlexander Motin 	zone_domain_update_wss(zdom);
15502760658bSAlexander Motin 	if (drain)
15512760658bSAlexander Motin 		target = 0;
15522760658bSAlexander Motin 	else if (trim)
15532760658bSAlexander Motin 		target = zdom->uzd_wss;
15542760658bSAlexander Motin 	else if (zdom->uzd_timin > 900 / UMA_TIMEOUT)
15552760658bSAlexander Motin 		target = zdom->uzd_nitems - zdom->uzd_limin / 4;
15562760658bSAlexander Motin 	else {
15572760658bSAlexander Motin 		ZDOM_UNLOCK(zdom);
15582760658bSAlexander Motin 		return (done);
15592760658bSAlexander Motin 	}
15602760658bSAlexander Motin 	while ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) != NULL &&
15612760658bSAlexander Motin 	    zdom->uzd_nitems >= target + bucket->ub_cnt) {
1562c6fd3e23SJeff Roberson 		bucket = zone_fetch_bucket(zone, zdom, true);
156308cfa56eSMark Johnston 		if (bucket == NULL)
156408cfa56eSMark Johnston 			break;
15656fd34d6fSJeff Roberson 		bucket_free(zone, bucket, NULL);
15662760658bSAlexander Motin 		done = true;
1567c6fd3e23SJeff Roberson 		ZDOM_LOCK(zdom);
15688355f576SJeff Roberson 	}
1569c6fd3e23SJeff Roberson 	ZDOM_UNLOCK(zdom);
15702760658bSAlexander Motin 	return (done);
1571ab3185d1SJeff Roberson }
157254f421f9SMark Johnston 
157354f421f9SMark Johnston static void
1574aabe13f1SMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain, int domain)
157554f421f9SMark Johnston {
157654f421f9SMark Johnston 	int i;
157754f421f9SMark Johnston 
157854f421f9SMark Johnston 	/*
157954f421f9SMark Johnston 	 * Shrink the zone bucket size to ensure that the per-CPU caches
158054f421f9SMark Johnston 	 * don't grow too large.
158154f421f9SMark Johnston 	 */
158254f421f9SMark Johnston 	if (zone->uz_bucket_size > zone->uz_bucket_size_min)
158354f421f9SMark Johnston 		zone->uz_bucket_size--;
158454f421f9SMark Johnston 
1585aabe13f1SMark Johnston 	if (domain != UMA_ANYDOMAIN &&
1586aabe13f1SMark Johnston 	    (zone->uz_flags & UMA_ZONE_ROUNDROBIN) == 0) {
15872760658bSAlexander Motin 		bucket_cache_reclaim_domain(zone, drain, true, domain);
1588aabe13f1SMark Johnston 	} else {
158954f421f9SMark Johnston 		for (i = 0; i < vm_ndomains; i++)
15902760658bSAlexander Motin 			bucket_cache_reclaim_domain(zone, drain, true, i);
15918355f576SJeff Roberson 	}
1592aabe13f1SMark Johnston }
1593fc03d22bSJeff Roberson 
1594fc03d22bSJeff Roberson static void
1595fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
1596fc03d22bSJeff Roberson {
1597fc03d22bSJeff Roberson 	uint8_t *mem;
159809c8cb71SMark Johnston 	size_t size;
1599fc03d22bSJeff Roberson 	int i;
1600fc03d22bSJeff Roberson 	uint8_t flags;
1601fc03d22bSJeff Roberson 
16021431a748SGleb Smirnoff 	CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes",
16031431a748SGleb Smirnoff 	    keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera);
16041431a748SGleb Smirnoff 
16051e0701e1SJeff Roberson 	mem = slab_data(slab, keg);
160609c8cb71SMark Johnston 	size = PAGE_SIZE * keg->uk_ppera;
160709c8cb71SMark Johnston 
160809c8cb71SMark Johnston 	kasan_mark_slab_valid(keg, mem);
1609fc03d22bSJeff Roberson 	if (keg->uk_fini != NULL) {
161009c8cb71SMark Johnston 		for (i = start - 1; i > -1; i--)
1611c5deaf04SGleb Smirnoff #ifdef INVARIANTS
1612c5deaf04SGleb Smirnoff 		/*
1613c5deaf04SGleb Smirnoff 		 * trash_fini implies that dtor was trash_dtor. trash_fini
1614c5deaf04SGleb Smirnoff 		 * would check that memory hasn't been modified since free,
1615c5deaf04SGleb Smirnoff 		 * which executed trash_dtor.
1616c5deaf04SGleb Smirnoff 		 * That's why we need to run uma_dbg_kskip() check here,
1617c5deaf04SGleb Smirnoff 		 * albeit we don't make skip check for other init/fini
1618c5deaf04SGleb Smirnoff 		 * invocations.
1619c5deaf04SGleb Smirnoff 		 */
16201e0701e1SJeff Roberson 		if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) ||
1621c5deaf04SGleb Smirnoff 		    keg->uk_fini != trash_fini)
1622c5deaf04SGleb Smirnoff #endif
16231e0701e1SJeff Roberson 			keg->uk_fini(slab_item(slab, keg, i), keg->uk_size);
1624fc03d22bSJeff Roberson 	}
162509c8cb71SMark Johnston 	flags = slab->us_flags;
162609c8cb71SMark Johnston 	if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) {
16279b8db4d0SRyan Libby 		zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab),
16289b8db4d0SRyan Libby 		    NULL, SKIP_NONE);
162909c8cb71SMark Johnston 	}
163009c8cb71SMark Johnston 	keg->uk_freef(mem, size, flags);
163109c8cb71SMark Johnston 	uma_total_dec(size);
16328355f576SJeff Roberson }
16338355f576SJeff Roberson 
1634f09cbea3SMark Johnston static void
1635f09cbea3SMark Johnston keg_drain_domain(uma_keg_t keg, int domain)
1636f09cbea3SMark Johnston {
1637f09cbea3SMark Johnston 	struct slabhead freeslabs;
1638f09cbea3SMark Johnston 	uma_domain_t dom;
1639f09cbea3SMark Johnston 	uma_slab_t slab, tmp;
1640f09cbea3SMark Johnston 	uint32_t i, stofree, stokeep, partial;
1641f09cbea3SMark Johnston 
1642f09cbea3SMark Johnston 	dom = &keg->uk_domain[domain];
1643f09cbea3SMark Johnston 	LIST_INIT(&freeslabs);
1644f09cbea3SMark Johnston 
1645f09cbea3SMark Johnston 	CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u",
1646575a4437SEd Maste 	    keg->uk_name, keg, domain, dom->ud_free_items);
1647f09cbea3SMark Johnston 
1648f09cbea3SMark Johnston 	KEG_LOCK(keg, domain);
1649f09cbea3SMark Johnston 
1650f09cbea3SMark Johnston 	/*
1651f09cbea3SMark Johnston 	 * Are the free items in partially allocated slabs sufficient to meet
1652f09cbea3SMark Johnston 	 * the reserve? If not, compute the number of fully free slabs that must
1653f09cbea3SMark Johnston 	 * be kept.
1654f09cbea3SMark Johnston 	 */
1655f09cbea3SMark Johnston 	partial = dom->ud_free_items - dom->ud_free_slabs * keg->uk_ipers;
1656f09cbea3SMark Johnston 	if (partial < keg->uk_reserve) {
1657f09cbea3SMark Johnston 		stokeep = min(dom->ud_free_slabs,
1658f09cbea3SMark Johnston 		    howmany(keg->uk_reserve - partial, keg->uk_ipers));
1659f09cbea3SMark Johnston 	} else {
1660f09cbea3SMark Johnston 		stokeep = 0;
1661f09cbea3SMark Johnston 	}
1662f09cbea3SMark Johnston 	stofree = dom->ud_free_slabs - stokeep;
1663f09cbea3SMark Johnston 
1664f09cbea3SMark Johnston 	/*
1665f09cbea3SMark Johnston 	 * Partition the free slabs into two sets: those that must be kept in
1666f09cbea3SMark Johnston 	 * order to maintain the reserve, and those that may be released back to
1667f09cbea3SMark Johnston 	 * the system.  Since one set may be much larger than the other,
1668f09cbea3SMark Johnston 	 * populate the smaller of the two sets and swap them if necessary.
1669f09cbea3SMark Johnston 	 */
1670f09cbea3SMark Johnston 	for (i = min(stofree, stokeep); i > 0; i--) {
1671f09cbea3SMark Johnston 		slab = LIST_FIRST(&dom->ud_free_slab);
1672f09cbea3SMark Johnston 		LIST_REMOVE(slab, us_link);
1673f09cbea3SMark Johnston 		LIST_INSERT_HEAD(&freeslabs, slab, us_link);
1674f09cbea3SMark Johnston 	}
1675f09cbea3SMark Johnston 	if (stofree > stokeep)
1676f09cbea3SMark Johnston 		LIST_SWAP(&freeslabs, &dom->ud_free_slab, uma_slab, us_link);
1677f09cbea3SMark Johnston 
1678f09cbea3SMark Johnston 	if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) {
1679f09cbea3SMark Johnston 		LIST_FOREACH(slab, &freeslabs, us_link)
1680f09cbea3SMark Johnston 			UMA_HASH_REMOVE(&keg->uk_hash, slab);
1681f09cbea3SMark Johnston 	}
1682f09cbea3SMark Johnston 	dom->ud_free_items -= stofree * keg->uk_ipers;
1683f09cbea3SMark Johnston 	dom->ud_free_slabs -= stofree;
1684f09cbea3SMark Johnston 	dom->ud_pages -= stofree * keg->uk_ppera;
1685f09cbea3SMark Johnston 	KEG_UNLOCK(keg, domain);
1686f09cbea3SMark Johnston 
1687f09cbea3SMark Johnston 	LIST_FOREACH_SAFE(slab, &freeslabs, us_link, tmp)
1688f09cbea3SMark Johnston 		keg_free_slab(keg, slab, keg->uk_ipers);
1689f09cbea3SMark Johnston }
1690f09cbea3SMark Johnston 
16918355f576SJeff Roberson /*
1692e20a199fSJeff Roberson  * Frees pages from a keg back to the system.  This is done on demand from
16938355f576SJeff Roberson  * the pageout daemon.
16948355f576SJeff Roberson  *
1695e20a199fSJeff Roberson  * Returns nothing.
16968355f576SJeff Roberson  */
1697e20a199fSJeff Roberson static void
1698aabe13f1SMark Johnston keg_drain(uma_keg_t keg, int domain)
16998355f576SJeff Roberson {
1700f09cbea3SMark Johnston 	int i;
17018355f576SJeff Roberson 
1702f09cbea3SMark Johnston 	if ((keg->uk_flags & UMA_ZONE_NOFREE) != 0)
17038355f576SJeff Roberson 		return;
1704aabe13f1SMark Johnston 	if (domain != UMA_ANYDOMAIN) {
1705aabe13f1SMark Johnston 		keg_drain_domain(keg, domain);
1706aabe13f1SMark Johnston 	} else {
1707f09cbea3SMark Johnston 		for (i = 0; i < vm_ndomains; i++)
1708f09cbea3SMark Johnston 			keg_drain_domain(keg, i);
17098355f576SJeff Roberson 	}
1710aabe13f1SMark Johnston }
17118355f576SJeff Roberson 
1712e20a199fSJeff Roberson static void
1713aabe13f1SMark Johnston zone_reclaim(uma_zone_t zone, int domain, int waitok, bool drain)
1714e20a199fSJeff Roberson {
17158355f576SJeff Roberson 	/*
1716aabe13f1SMark Johnston 	 * Count active reclaim operations in order to interlock with
1717aabe13f1SMark Johnston 	 * zone_dtor(), which removes the zone from global lists before
1718aabe13f1SMark Johnston 	 * attempting to reclaim items itself.
1719aabe13f1SMark Johnston 	 *
1720aabe13f1SMark Johnston 	 * The zone may be destroyed while sleeping, so only zone_dtor() should
1721aabe13f1SMark Johnston 	 * specify M_WAITOK.
1722e20a199fSJeff Roberson 	 */
1723e20a199fSJeff Roberson 	ZONE_LOCK(zone);
1724aabe13f1SMark Johnston 	if (waitok == M_WAITOK) {
1725aabe13f1SMark Johnston 		while (zone->uz_reclaimers > 0)
1726aabe13f1SMark Johnston 			msleep(zone, ZONE_LOCKPTR(zone), PVM, "zonedrain", 1);
1727e20a199fSJeff Roberson 	}
1728aabe13f1SMark Johnston 	zone->uz_reclaimers++;
1729e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
1730aabe13f1SMark Johnston 	bucket_cache_reclaim(zone, drain, domain);
173108cfa56eSMark Johnston 
173208034d10SKonstantin Belousov 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0)
1733aabe13f1SMark Johnston 		keg_drain(zone->uz_keg, domain);
1734e20a199fSJeff Roberson 	ZONE_LOCK(zone);
1735aabe13f1SMark Johnston 	zone->uz_reclaimers--;
1736aabe13f1SMark Johnston 	if (zone->uz_reclaimers == 0)
1737e20a199fSJeff Roberson 		wakeup(zone);
1738e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
1739e20a199fSJeff Roberson }
1740e20a199fSJeff Roberson 
1741e20a199fSJeff Roberson /*
17428b987a77SJeff Roberson  * Allocate a new slab for a keg and inserts it into the partial slab list.
17438b987a77SJeff Roberson  * The keg should be unlocked on entry.  If the allocation succeeds it will
17448b987a77SJeff Roberson  * be locked on return.
17458355f576SJeff Roberson  *
17468355f576SJeff Roberson  * Arguments:
174786220393SMark Johnston  *	flags   Wait flags for the item initialization routine
174886220393SMark Johnston  *	aflags  Wait flags for the slab allocation
17498355f576SJeff Roberson  *
17508355f576SJeff Roberson  * Returns:
17518355f576SJeff Roberson  *	The slab that was allocated or NULL if there is no memory and the
17528355f576SJeff Roberson  *	caller specified M_NOWAIT.
17538355f576SJeff Roberson  */
17548355f576SJeff Roberson static uma_slab_t
175586220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags,
175686220393SMark Johnston     int aflags)
17578355f576SJeff Roberson {
17588b987a77SJeff Roberson 	uma_domain_t dom;
1759099a0e58SBosko Milekic 	uma_slab_t slab;
17602e47807cSJeff Roberson 	unsigned long size;
176185dcf349SGleb Smirnoff 	uint8_t *mem;
176286220393SMark Johnston 	uint8_t sflags;
17638355f576SJeff Roberson 	int i;
17648355f576SJeff Roberson 
1765b9fd884aSColin Percival 	TSENTER();
1766b9fd884aSColin Percival 
1767ab3185d1SJeff Roberson 	KASSERT(domain >= 0 && domain < vm_ndomains,
1768ab3185d1SJeff Roberson 	    ("keg_alloc_slab: domain %d out of range", domain));
1769a553d4b8SJeff Roberson 
1770194a979eSMark Johnston 	slab = NULL;
1771194a979eSMark Johnston 	mem = NULL;
177254c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) {
17739b8db4d0SRyan Libby 		uma_hash_slab_t hslab;
17749b8db4d0SRyan Libby 		hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL,
17759b8db4d0SRyan Libby 		    domain, aflags);
17769b8db4d0SRyan Libby 		if (hslab == NULL)
1777727c6918SJeff Roberson 			goto fail;
17789b8db4d0SRyan Libby 		slab = &hslab->uhs_slab;
1779a553d4b8SJeff Roberson 	}
1780a553d4b8SJeff Roberson 
17813370c5bfSJeff Roberson 	/*
17823370c5bfSJeff Roberson 	 * This reproduces the old vm_zone behavior of zero filling pages the
17833370c5bfSJeff Roberson 	 * first time they are added to a zone.
17843370c5bfSJeff Roberson 	 *
17853370c5bfSJeff Roberson 	 * Malloced items are zeroed in uma_zalloc.
17863370c5bfSJeff Roberson 	 */
17873370c5bfSJeff Roberson 
1788099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
178986220393SMark Johnston 		aflags |= M_ZERO;
17903370c5bfSJeff Roberson 	else
179186220393SMark Johnston 		aflags &= ~M_ZERO;
17923370c5bfSJeff Roberson 
1793263811f7SKip Macy 	if (keg->uk_flags & UMA_ZONE_NODUMP)
179486220393SMark Johnston 		aflags |= M_NODUMP;
1795263811f7SKip Macy 
1796e20a199fSJeff Roberson 	/* zone is passed for legacy reasons. */
1797194a979eSMark Johnston 	size = keg->uk_ppera * PAGE_SIZE;
179809c8cb71SMark Johnston 	mem = keg->uk_allocf(zone, size, domain, &sflags, aflags);
1799a553d4b8SJeff Roberson 	if (mem == NULL) {
180054c5ae80SRyan Libby 		if (keg->uk_flags & UMA_ZFLAG_OFFPAGE)
18019b8db4d0SRyan Libby 			zone_free_item(slabzone(keg->uk_ipers),
18029b8db4d0SRyan Libby 			    slab_tohashslab(slab), NULL, SKIP_NONE);
1803727c6918SJeff Roberson 		goto fail;
1804a553d4b8SJeff Roberson 	}
18052e47807cSJeff Roberson 	uma_total_inc(size);
18068355f576SJeff Roberson 
18078b987a77SJeff Roberson 	/* For HASH zones all pages go to the same uma_domain. */
180854c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0)
18098b987a77SJeff Roberson 		domain = 0;
18108b987a77SJeff Roberson 
1811a932a5a6SMark Johnston 	kmsan_mark(mem, size,
1812a932a5a6SMark Johnston 	    (aflags & M_ZERO) != 0 ? KMSAN_STATE_INITED : KMSAN_STATE_UNINIT);
1813a932a5a6SMark Johnston 
18145c0e403bSJeff Roberson 	/* Point the slab into the allocated memory */
181554c5ae80SRyan Libby 	if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE))
1816099a0e58SBosko Milekic 		slab = (uma_slab_t)(mem + keg->uk_pgoff);
18171e0701e1SJeff Roberson 	else
18189b8db4d0SRyan Libby 		slab_tohashslab(slab)->uhs_data = mem;
18195c0e403bSJeff Roberson 
182054c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_VTOSLAB)
1821099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ppera; i++)
1822584061b4SJeff Roberson 			vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE),
1823584061b4SJeff Roberson 			    zone, slab);
18248355f576SJeff Roberson 
1825099a0e58SBosko Milekic 	slab->us_freecount = keg->uk_ipers;
182686220393SMark Johnston 	slab->us_flags = sflags;
1827ab3185d1SJeff Roberson 	slab->us_domain = domain;
18288b987a77SJeff Roberson 
18299b78b1f4SJeff Roberson 	BIT_FILL(keg->uk_ipers, &slab->us_free);
1830ef72505eSJeff Roberson #ifdef INVARIANTS
1831815db204SRyan Libby 	BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg));
1832ef72505eSJeff Roberson #endif
1833099a0e58SBosko Milekic 
1834b23f72e9SBrian Feldman 	if (keg->uk_init != NULL) {
1835099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ipers; i++)
18361e0701e1SJeff Roberson 			if (keg->uk_init(slab_item(slab, keg, i),
183786220393SMark Johnston 			    keg->uk_size, flags) != 0)
1838b23f72e9SBrian Feldman 				break;
1839b23f72e9SBrian Feldman 		if (i != keg->uk_ipers) {
1840fc03d22bSJeff Roberson 			keg_free_slab(keg, slab, i);
1841727c6918SJeff Roberson 			goto fail;
1842b23f72e9SBrian Feldman 		}
1843b23f72e9SBrian Feldman 	}
184409c8cb71SMark Johnston 	kasan_mark_slab_invalid(keg, mem);
18458b987a77SJeff Roberson 	KEG_LOCK(keg, domain);
18465c0e403bSJeff Roberson 
18471431a748SGleb Smirnoff 	CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)",
18481431a748SGleb Smirnoff 	    slab, keg->uk_name, keg);
18491431a748SGleb Smirnoff 
185054c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_HASH)
1851099a0e58SBosko Milekic 		UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
18528355f576SJeff Roberson 
18538b987a77SJeff Roberson 	/*
18548b987a77SJeff Roberson 	 * If we got a slab here it's safe to mark it partially used
18558b987a77SJeff Roberson 	 * and return.  We assume that the caller is going to remove
18568b987a77SJeff Roberson 	 * at least one item.
18578b987a77SJeff Roberson 	 */
18588b987a77SJeff Roberson 	dom = &keg->uk_domain[domain];
18598b987a77SJeff Roberson 	LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
18608b987a77SJeff Roberson 	dom->ud_pages += keg->uk_ppera;
18614ab3aee8SMark Johnston 	dom->ud_free_items += keg->uk_ipers;
18628355f576SJeff Roberson 
1863b9fd884aSColin Percival 	TSEXIT();
18648355f576SJeff Roberson 	return (slab);
1865727c6918SJeff Roberson 
1866727c6918SJeff Roberson fail:
1867727c6918SJeff Roberson 	return (NULL);
18688355f576SJeff Roberson }
18698355f576SJeff Roberson 
18708355f576SJeff Roberson /*
1871537f92cdSMark Johnston  * This function is intended to be used early on in place of page_alloc().  It
1872537f92cdSMark Johnston  * performs contiguous physical memory allocations and uses a bump allocator for
1873537f92cdSMark Johnston  * KVA, so is usable before the kernel map is initialized.
1874009b6fcbSJeff Roberson  */
1875009b6fcbSJeff Roberson static void *
1876ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1877ab3185d1SJeff Roberson     int wait)
1878009b6fcbSJeff Roberson {
1879a81c400eSJeff Roberson 	vm_paddr_t pa;
1880a81c400eSJeff Roberson 	vm_page_t m;
188184c39222SMark Johnston 	int i, pages;
1882099a0e58SBosko Milekic 
1883f7d35785SGleb Smirnoff 	pages = howmany(bytes, PAGE_SIZE);
1884f7d35785SGleb Smirnoff 	KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__));
1885a81c400eSJeff Roberson 
1886f7d35785SGleb Smirnoff 	*pflag = UMA_SLAB_BOOT;
188784c39222SMark Johnston 	m = vm_page_alloc_noobj_contig_domain(domain, malloc2vm_flags(wait) |
188884c39222SMark Johnston 	    VM_ALLOC_WIRED, pages, (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0,
188984c39222SMark Johnston 	    VM_MEMATTR_DEFAULT);
1890a81c400eSJeff Roberson 	if (m == NULL)
1891a81c400eSJeff Roberson 		return (NULL);
1892a81c400eSJeff Roberson 
1893a81c400eSJeff Roberson 	pa = VM_PAGE_TO_PHYS(m);
1894a81c400eSJeff Roberson 	for (i = 0; i < pages; i++, pa += PAGE_SIZE) {
1895c25a30e2SKonstantin Belousov #if defined(__aarch64__) || defined(__amd64__) || \
1896a81c400eSJeff Roberson     defined(__riscv) || defined(__powerpc64__)
1897a81c400eSJeff Roberson 		if ((wait & M_NODUMP) == 0)
1898a81c400eSJeff Roberson 			dump_add_page(pa);
1899a81c400eSJeff Roberson #endif
1900a81c400eSJeff Roberson 	}
1901f7d35785SGleb Smirnoff 
190284c39222SMark Johnston 	/* Allocate KVA and indirectly advance bootmem. */
190384c39222SMark Johnston 	return ((void *)pmap_map(&bootmem, m->phys_addr,
190484c39222SMark Johnston 	    m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE));
1905f7d35785SGleb Smirnoff }
1906f7d35785SGleb Smirnoff 
1907a81c400eSJeff Roberson static void
1908a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes)
1909a81c400eSJeff Roberson {
1910a81c400eSJeff Roberson 	vm_offset_t va;
1911a81c400eSJeff Roberson 	vm_page_t m;
1912a81c400eSJeff Roberson 
1913a81c400eSJeff Roberson 	va = (vm_offset_t)mem;
1914a81c400eSJeff Roberson 	m = PHYS_TO_VM_PAGE(pmap_kextract(va));
1915663de81fSMark Johnston 
1916663de81fSMark Johnston 	/*
1917663de81fSMark Johnston 	 * startup_alloc() returns direct-mapped slabs on some platforms.  Avoid
1918663de81fSMark Johnston 	 * unmapping ranges of the direct map.
1919663de81fSMark Johnston 	 */
1920663de81fSMark Johnston 	if (va >= bootstart && va + bytes <= bootmem)
1921a81c400eSJeff Roberson 		pmap_remove(kernel_pmap, va, va + bytes);
1922a81c400eSJeff Roberson 	for (; bytes != 0; bytes -= PAGE_SIZE, m++) {
1923c25a30e2SKonstantin Belousov #if defined(__aarch64__) || defined(__amd64__) || \
1924a81c400eSJeff Roberson     defined(__riscv) || defined(__powerpc64__)
1925a81c400eSJeff Roberson 		dump_drop_page(VM_PAGE_TO_PHYS(m));
1926a81c400eSJeff Roberson #endif
1927a81c400eSJeff Roberson 		vm_page_unwire_noq(m);
1928a81c400eSJeff Roberson 		vm_page_free(m);
1929a81c400eSJeff Roberson 	}
1930a81c400eSJeff Roberson }
1931a81c400eSJeff Roberson 
1932f7d35785SGleb Smirnoff /*
19338355f576SJeff Roberson  * Allocates a number of pages from the system
19348355f576SJeff Roberson  *
19358355f576SJeff Roberson  * Arguments:
19368355f576SJeff Roberson  *	bytes  The number of bytes requested
19378355f576SJeff Roberson  *	wait  Shall we wait?
19388355f576SJeff Roberson  *
19398355f576SJeff Roberson  * Returns:
19408355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
19418355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
19428355f576SJeff Roberson  */
19438355f576SJeff Roberson static void *
1944ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1945ab3185d1SJeff Roberson     int wait)
19468355f576SJeff Roberson {
19478355f576SJeff Roberson 	void *p;	/* Returned page */
19488355f576SJeff Roberson 
19492e47807cSJeff Roberson 	*pflag = UMA_SLAB_KERNEL;
1950f49fd63aSJohn Baldwin 	p = kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait);
19518355f576SJeff Roberson 
19528355f576SJeff Roberson 	return (p);
19538355f576SJeff Roberson }
19548355f576SJeff Roberson 
1955ab3059a8SMatt Macy static void *
1956ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1957ab3059a8SMatt Macy     int wait)
1958ab3059a8SMatt Macy {
1959ab3059a8SMatt Macy 	struct pglist alloctail;
1960ab3059a8SMatt Macy 	vm_offset_t addr, zkva;
1961ab3059a8SMatt Macy 	int cpu, flags;
1962ab3059a8SMatt Macy 	vm_page_t p, p_next;
1963ab3059a8SMatt Macy #ifdef NUMA
1964ab3059a8SMatt Macy 	struct pcpu *pc;
1965ab3059a8SMatt Macy #endif
1966ab3059a8SMatt Macy 
1967ab3059a8SMatt Macy 	MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE);
1968ab3059a8SMatt Macy 
1969013072f0SMark Johnston 	TAILQ_INIT(&alloctail);
1970a4667e09SMark Johnston 	flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | malloc2vm_flags(wait);
1971013072f0SMark Johnston 	*pflag = UMA_SLAB_KERNEL;
1972ab3059a8SMatt Macy 	for (cpu = 0; cpu <= mp_maxid; cpu++) {
1973ab3059a8SMatt Macy 		if (CPU_ABSENT(cpu)) {
1974a4667e09SMark Johnston 			p = vm_page_alloc_noobj(flags);
1975ab3059a8SMatt Macy 		} else {
1976ab3059a8SMatt Macy #ifndef NUMA
1977a4667e09SMark Johnston 			p = vm_page_alloc_noobj(flags);
1978ab3059a8SMatt Macy #else
1979ab3059a8SMatt Macy 			pc = pcpu_find(cpu);
198020526802SAndrew Gallatin 			if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain)))
198120526802SAndrew Gallatin 				p = NULL;
198220526802SAndrew Gallatin 			else
1983a4667e09SMark Johnston 				p = vm_page_alloc_noobj_domain(pc->pc_domain,
1984a4667e09SMark Johnston 				    flags);
1985ab3059a8SMatt Macy 			if (__predict_false(p == NULL))
1986a4667e09SMark Johnston 				p = vm_page_alloc_noobj(flags);
1987ab3059a8SMatt Macy #endif
1988ab3059a8SMatt Macy 		}
1989ab3059a8SMatt Macy 		if (__predict_false(p == NULL))
1990ab3059a8SMatt Macy 			goto fail;
1991ab3059a8SMatt Macy 		TAILQ_INSERT_TAIL(&alloctail, p, listq);
1992ab3059a8SMatt Macy 	}
1993ab3059a8SMatt Macy 	if ((addr = kva_alloc(bytes)) == 0)
1994ab3059a8SMatt Macy 		goto fail;
1995ab3059a8SMatt Macy 	zkva = addr;
1996ab3059a8SMatt Macy 	TAILQ_FOREACH(p, &alloctail, listq) {
1997ab3059a8SMatt Macy 		pmap_qenter(zkva, &p, 1);
1998ab3059a8SMatt Macy 		zkva += PAGE_SIZE;
1999ab3059a8SMatt Macy 	}
2000ab3059a8SMatt Macy 	return ((void*)addr);
2001ab3059a8SMatt Macy fail:
2002ab3059a8SMatt Macy 	TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
200388ea538aSMark Johnston 		vm_page_unwire_noq(p);
2004ab3059a8SMatt Macy 		vm_page_free(p);
2005ab3059a8SMatt Macy 	}
2006ab3059a8SMatt Macy 	return (NULL);
2007ab3059a8SMatt Macy }
2008ab3059a8SMatt Macy 
20098355f576SJeff Roberson /*
2010a9d6f1feSMark Johnston  * Allocates a number of pages not belonging to a VM object
20118355f576SJeff Roberson  *
20128355f576SJeff Roberson  * Arguments:
20138355f576SJeff Roberson  *	bytes  The number of bytes requested
20148355f576SJeff Roberson  *	wait   Shall we wait?
20158355f576SJeff Roberson  *
20168355f576SJeff Roberson  * Returns:
20178355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
20188355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
20198355f576SJeff Roberson  */
20208355f576SJeff Roberson static void *
2021ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
2022ab3185d1SJeff Roberson     int wait)
20238355f576SJeff Roberson {
2024a4915c21SAttilio Rao 	TAILQ_HEAD(, vm_page) alloctail;
2025a4915c21SAttilio Rao 	u_long npages;
2026b245ac95SAlan Cox 	vm_offset_t retkva, zkva;
2027a4915c21SAttilio Rao 	vm_page_t p, p_next;
2028e20a199fSJeff Roberson 	uma_keg_t keg;
2029a4667e09SMark Johnston 	int req;
20308355f576SJeff Roberson 
2031a4915c21SAttilio Rao 	TAILQ_INIT(&alloctail);
2032bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
2033a4667e09SMark Johnston 	req = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED;
2034a4667e09SMark Johnston 	if ((wait & M_WAITOK) != 0)
2035a4667e09SMark Johnston 		req |= VM_ALLOC_WAITOK;
2036a4915c21SAttilio Rao 
2037a4915c21SAttilio Rao 	npages = howmany(bytes, PAGE_SIZE);
2038a4915c21SAttilio Rao 	while (npages > 0) {
2039a4667e09SMark Johnston 		p = vm_page_alloc_noobj_domain(domain, req);
2040a4915c21SAttilio Rao 		if (p != NULL) {
2041a4915c21SAttilio Rao 			/*
2042a4915c21SAttilio Rao 			 * Since the page does not belong to an object, its
2043a4915c21SAttilio Rao 			 * listq is unused.
2044a4915c21SAttilio Rao 			 */
2045a4915c21SAttilio Rao 			TAILQ_INSERT_TAIL(&alloctail, p, listq);
2046a4915c21SAttilio Rao 			npages--;
2047a4915c21SAttilio Rao 			continue;
2048a4915c21SAttilio Rao 		}
20498355f576SJeff Roberson 		/*
2050a4915c21SAttilio Rao 		 * Page allocation failed, free intermediate pages and
2051a4915c21SAttilio Rao 		 * exit.
20528355f576SJeff Roberson 		 */
2053a4915c21SAttilio Rao 		TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
205488ea538aSMark Johnston 			vm_page_unwire_noq(p);
2055b245ac95SAlan Cox 			vm_page_free(p);
2056b245ac95SAlan Cox 		}
2057a4915c21SAttilio Rao 		return (NULL);
2058b245ac95SAlan Cox 	}
20598355f576SJeff Roberson 	*flags = UMA_SLAB_PRIV;
2060a4915c21SAttilio Rao 	zkva = keg->uk_kva +
2061a4915c21SAttilio Rao 	    atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
2062a4915c21SAttilio Rao 	retkva = zkva;
2063a4915c21SAttilio Rao 	TAILQ_FOREACH(p, &alloctail, listq) {
2064a4915c21SAttilio Rao 		pmap_qenter(zkva, &p, 1);
2065a4915c21SAttilio Rao 		zkva += PAGE_SIZE;
2066a4915c21SAttilio Rao 	}
20678355f576SJeff Roberson 
20688355f576SJeff Roberson 	return ((void *)retkva);
20698355f576SJeff Roberson }
20708355f576SJeff Roberson 
20718355f576SJeff Roberson /*
2072ec0d8280SRyan Libby  * Allocate physically contiguous pages.
2073ec0d8280SRyan Libby  */
2074ec0d8280SRyan Libby static void *
2075ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
2076ec0d8280SRyan Libby     int wait)
2077ec0d8280SRyan Libby {
2078ec0d8280SRyan Libby 
2079ec0d8280SRyan Libby 	*pflag = UMA_SLAB_KERNEL;
2080ec0d8280SRyan Libby 	return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain),
2081ec0d8280SRyan Libby 	    bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT));
2082ec0d8280SRyan Libby }
2083ec0d8280SRyan Libby 
2084ec0d8280SRyan Libby /*
20858355f576SJeff Roberson  * Frees a number of pages to the system
20868355f576SJeff Roberson  *
20878355f576SJeff Roberson  * Arguments:
20888355f576SJeff Roberson  *	mem   A pointer to the memory to be freed
20898355f576SJeff Roberson  *	size  The size of the memory being freed
20908355f576SJeff Roberson  *	flags The original p->us_flags field
20918355f576SJeff Roberson  *
20928355f576SJeff Roberson  * Returns:
20938355f576SJeff Roberson  *	Nothing
20948355f576SJeff Roberson  */
20958355f576SJeff Roberson static void
2096f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags)
20978355f576SJeff Roberson {
20983370c5bfSJeff Roberson 
2099a81c400eSJeff Roberson 	if ((flags & UMA_SLAB_BOOT) != 0) {
2100a81c400eSJeff Roberson 		startup_free(mem, size);
2101a81c400eSJeff Roberson 		return;
2102a81c400eSJeff Roberson 	}
2103a81c400eSJeff Roberson 
2104ec0d8280SRyan Libby 	KASSERT((flags & UMA_SLAB_KERNEL) != 0,
2105ec0d8280SRyan Libby 	    ("UMA: page_free used with invalid flags %x", flags));
21068355f576SJeff Roberson 
2107f49fd63aSJohn Baldwin 	kmem_free(mem, size);
21088355f576SJeff Roberson }
21098355f576SJeff Roberson 
21108355f576SJeff Roberson /*
2111ab3059a8SMatt Macy  * Frees pcpu zone allocations
2112ab3059a8SMatt Macy  *
2113ab3059a8SMatt Macy  * Arguments:
2114ab3059a8SMatt Macy  *	mem   A pointer to the memory to be freed
2115ab3059a8SMatt Macy  *	size  The size of the memory being freed
2116ab3059a8SMatt Macy  *	flags The original p->us_flags field
2117ab3059a8SMatt Macy  *
2118ab3059a8SMatt Macy  * Returns:
2119ab3059a8SMatt Macy  *	Nothing
2120ab3059a8SMatt Macy  */
2121ab3059a8SMatt Macy static void
2122ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags)
2123ab3059a8SMatt Macy {
2124ab3059a8SMatt Macy 	vm_offset_t sva, curva;
2125ab3059a8SMatt Macy 	vm_paddr_t paddr;
2126ab3059a8SMatt Macy 	vm_page_t m;
2127ab3059a8SMatt Macy 
2128ab3059a8SMatt Macy 	MPASS(size == (mp_maxid+1)*PAGE_SIZE);
21295ba16cf3SRyan Libby 
21305ba16cf3SRyan Libby 	if ((flags & UMA_SLAB_BOOT) != 0) {
21315ba16cf3SRyan Libby 		startup_free(mem, size);
21325ba16cf3SRyan Libby 		return;
21335ba16cf3SRyan Libby 	}
21345ba16cf3SRyan Libby 
2135ab3059a8SMatt Macy 	sva = (vm_offset_t)mem;
2136ab3059a8SMatt Macy 	for (curva = sva; curva < sva + size; curva += PAGE_SIZE) {
2137ab3059a8SMatt Macy 		paddr = pmap_kextract(curva);
2138ab3059a8SMatt Macy 		m = PHYS_TO_VM_PAGE(paddr);
213988ea538aSMark Johnston 		vm_page_unwire_noq(m);
2140ab3059a8SMatt Macy 		vm_page_free(m);
2141ab3059a8SMatt Macy 	}
2142ab3059a8SMatt Macy 	pmap_qremove(sva, size >> PAGE_SHIFT);
2143ab3059a8SMatt Macy 	kva_free(sva, size);
2144ab3059a8SMatt Macy }
2145ab3059a8SMatt Macy 
2146ab3059a8SMatt Macy /*
21478355f576SJeff Roberson  * Zero fill initializer
21488355f576SJeff Roberson  *
21498355f576SJeff Roberson  * Arguments/Returns follow uma_init specifications
21508355f576SJeff Roberson  */
2151b23f72e9SBrian Feldman static int
2152b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags)
21538355f576SJeff Roberson {
21548355f576SJeff Roberson 	bzero(mem, size);
2155b23f72e9SBrian Feldman 	return (0);
21568355f576SJeff Roberson }
21578355f576SJeff Roberson 
2158815db204SRyan Libby #ifdef INVARIANTS
215954007ce8SMark Johnston static struct noslabbits *
2160815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg)
2161815db204SRyan Libby {
2162815db204SRyan Libby 
2163815db204SRyan Libby 	return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers)));
2164815db204SRyan Libby }
2165815db204SRyan Libby #endif
2166815db204SRyan Libby 
21678355f576SJeff Roberson /*
21689b78b1f4SJeff Roberson  * Actual size of embedded struct slab (!OFFPAGE).
21699b78b1f4SJeff Roberson  */
217054007ce8SMark Johnston static size_t
21719b78b1f4SJeff Roberson slab_sizeof(int nitems)
21729b78b1f4SJeff Roberson {
21739b78b1f4SJeff Roberson 	size_t s;
21749b78b1f4SJeff Roberson 
2175815db204SRyan Libby 	s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS;
21769b78b1f4SJeff Roberson 	return (roundup(s, UMA_ALIGN_PTR + 1));
21779b78b1f4SJeff Roberson }
21789b78b1f4SJeff Roberson 
21794a8b575cSRyan Libby #define	UMA_FIXPT_SHIFT	31
21804a8b575cSRyan Libby #define	UMA_FRAC_FIXPT(n, d)						\
21814a8b575cSRyan Libby 	((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d)))
21824a8b575cSRyan Libby #define	UMA_FIXPT_PCT(f)						\
21834a8b575cSRyan Libby 	((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT))
21844a8b575cSRyan Libby #define	UMA_PCT_FIXPT(pct)	UMA_FRAC_FIXPT((pct), 100)
21854a8b575cSRyan Libby #define	UMA_MIN_EFF	UMA_PCT_FIXPT(100 - UMA_MAX_WASTE)
21864a8b575cSRyan Libby 
21879b78b1f4SJeff Roberson /*
21884a8b575cSRyan Libby  * Compute the number of items that will fit in a slab.  If hdr is true, the
21894a8b575cSRyan Libby  * item count may be limited to provide space in the slab for an inline slab
21904a8b575cSRyan Libby  * header.  Otherwise, all slab space will be provided for item storage.
21914a8b575cSRyan Libby  */
21924a8b575cSRyan Libby static u_int
21934a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr)
21944a8b575cSRyan Libby {
21954a8b575cSRyan Libby 	u_int ipers;
21964a8b575cSRyan Libby 	u_int padpi;
21974a8b575cSRyan Libby 
21984a8b575cSRyan Libby 	/* The padding between items is not needed after the last item. */
21994a8b575cSRyan Libby 	padpi = rsize - size;
22004a8b575cSRyan Libby 
22014a8b575cSRyan Libby 	if (hdr) {
22024a8b575cSRyan Libby 		/*
22034a8b575cSRyan Libby 		 * Start with the maximum item count and remove items until
22044a8b575cSRyan Libby 		 * the slab header first alongside the allocatable memory.
22054a8b575cSRyan Libby 		 */
22064a8b575cSRyan Libby 		for (ipers = MIN(SLAB_MAX_SETSIZE,
22074a8b575cSRyan Libby 		    (slabsize + padpi - slab_sizeof(1)) / rsize);
22084a8b575cSRyan Libby 		    ipers > 0 &&
22094a8b575cSRyan Libby 		    ipers * rsize - padpi + slab_sizeof(ipers) > slabsize;
22104a8b575cSRyan Libby 		    ipers--)
22114a8b575cSRyan Libby 			continue;
22124a8b575cSRyan Libby 	} else {
22134a8b575cSRyan Libby 		ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE);
22144a8b575cSRyan Libby 	}
22154a8b575cSRyan Libby 
22164a8b575cSRyan Libby 	return (ipers);
22174a8b575cSRyan Libby }
22184a8b575cSRyan Libby 
221927ca37acSRyan Libby struct keg_layout_result {
222027ca37acSRyan Libby 	u_int format;
222127ca37acSRyan Libby 	u_int slabsize;
222227ca37acSRyan Libby 	u_int ipers;
222327ca37acSRyan Libby 	u_int eff;
222427ca37acSRyan Libby };
222527ca37acSRyan Libby 
222627ca37acSRyan Libby static void
222727ca37acSRyan Libby keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt,
222827ca37acSRyan Libby     struct keg_layout_result *kl)
222927ca37acSRyan Libby {
223027ca37acSRyan Libby 	u_int total;
223127ca37acSRyan Libby 
223227ca37acSRyan Libby 	kl->format = fmt;
223327ca37acSRyan Libby 	kl->slabsize = slabsize;
223427ca37acSRyan Libby 
223527ca37acSRyan Libby 	/* Handle INTERNAL as inline with an extra page. */
223627ca37acSRyan Libby 	if ((fmt & UMA_ZFLAG_INTERNAL) != 0) {
223727ca37acSRyan Libby 		kl->format &= ~UMA_ZFLAG_INTERNAL;
223827ca37acSRyan Libby 		kl->slabsize += PAGE_SIZE;
223927ca37acSRyan Libby 	}
224027ca37acSRyan Libby 
224127ca37acSRyan Libby 	kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize,
224227ca37acSRyan Libby 	    (fmt & UMA_ZFLAG_OFFPAGE) == 0);
224327ca37acSRyan Libby 
224427ca37acSRyan Libby 	/* Account for memory used by an offpage slab header. */
224527ca37acSRyan Libby 	total = kl->slabsize;
224627ca37acSRyan Libby 	if ((fmt & UMA_ZFLAG_OFFPAGE) != 0)
224727ca37acSRyan Libby 		total += slabzone(kl->ipers)->uz_keg->uk_rsize;
224827ca37acSRyan Libby 
224927ca37acSRyan Libby 	kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total);
225027ca37acSRyan Libby }
225127ca37acSRyan Libby 
22529b78b1f4SJeff Roberson /*
22534a8b575cSRyan Libby  * Determine the format of a uma keg.  This determines where the slab header
22544a8b575cSRyan Libby  * will be placed (inline or offpage) and calculates ipers, rsize, and ppera.
22558355f576SJeff Roberson  *
22568355f576SJeff Roberson  * Arguments
2257e20a199fSJeff Roberson  *	keg  The zone we should initialize
22588355f576SJeff Roberson  *
22598355f576SJeff Roberson  * Returns
22608355f576SJeff Roberson  *	Nothing
22618355f576SJeff Roberson  */
22628355f576SJeff Roberson static void
22634a8b575cSRyan Libby keg_layout(uma_keg_t keg)
22648355f576SJeff Roberson {
226527ca37acSRyan Libby 	struct keg_layout_result kl = {}, kl_tmp;
226627ca37acSRyan Libby 	u_int fmts[2];
22674a8b575cSRyan Libby 	u_int alignsize;
226827ca37acSRyan Libby 	u_int nfmt;
22694a8b575cSRyan Libby 	u_int pages;
2270244f4554SBosko Milekic 	u_int rsize;
2271a55ebb7cSAndriy Gapon 	u_int slabsize;
227227ca37acSRyan Libby 	u_int i, j;
22738355f576SJeff Roberson 
22744a8b575cSRyan Libby 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
22754a8b575cSRyan Libby 	    (keg->uk_size <= UMA_PCPU_ALLOC_SIZE &&
22764a8b575cSRyan Libby 	     (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0),
22774a8b575cSRyan Libby 	    ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b",
22784a8b575cSRyan Libby 	     __func__, keg->uk_name, keg->uk_size, keg->uk_flags,
22794a8b575cSRyan Libby 	     PRINT_UMA_ZFLAGS));
2280bae55c4aSRyan Libby 	KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 ||
22814a8b575cSRyan Libby 	    (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0,
22824a8b575cSRyan Libby 	    ("%s: incompatible flags 0x%b", __func__, keg->uk_flags,
22834a8b575cSRyan Libby 	     PRINT_UMA_ZFLAGS));
2284e28a647dSGleb Smirnoff 
22854a8b575cSRyan Libby 	alignsize = keg->uk_align + 1;
2286b0dfc486SMark Johnston #ifdef KASAN
2287b0dfc486SMark Johnston 	/*
2288b0dfc486SMark Johnston 	 * ASAN requires that each allocation be aligned to the shadow map
2289b0dfc486SMark Johnston 	 * scale factor.
2290b0dfc486SMark Johnston 	 */
2291b0dfc486SMark Johnston 	if (alignsize < KASAN_SHADOW_SCALE)
2292b0dfc486SMark Johnston 		alignsize = KASAN_SHADOW_SCALE;
2293b0dfc486SMark Johnston #endif
2294ad97af7eSGleb Smirnoff 
2295ef72505eSJeff Roberson 	/*
2296ef72505eSJeff Roberson 	 * Calculate the size of each allocation (rsize) according to
2297ef72505eSJeff Roberson 	 * alignment.  If the requested size is smaller than we have
2298ef72505eSJeff Roberson 	 * allocation bits for we round it up.
2299ef72505eSJeff Roberson 	 */
23009b8db4d0SRyan Libby 	rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT);
23014a8b575cSRyan Libby 	rsize = roundup2(rsize, alignsize);
2302ad97af7eSGleb Smirnoff 
230327ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) {
23049b78b1f4SJeff Roberson 		/*
23054a8b575cSRyan Libby 		 * We want one item to start on every align boundary in a page.
23064a8b575cSRyan Libby 		 * To do this we will span pages.  We will also extend the item
23074a8b575cSRyan Libby 		 * by the size of align if it is an even multiple of align.
23084a8b575cSRyan Libby 		 * Otherwise, it would fall on the same boundary every time.
23099b78b1f4SJeff Roberson 		 */
23104a8b575cSRyan Libby 		if ((rsize & alignsize) == 0)
23114a8b575cSRyan Libby 			rsize += alignsize;
23124a8b575cSRyan Libby 		slabsize = rsize * (PAGE_SIZE / alignsize);
23134a8b575cSRyan Libby 		slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE);
23144a8b575cSRyan Libby 		slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE);
231527ca37acSRyan Libby 		slabsize = round_page(slabsize);
23164a8b575cSRyan Libby 	} else {
23174a8b575cSRyan Libby 		/*
231827ca37acSRyan Libby 		 * Start with a slab size of as many pages as it takes to
231927ca37acSRyan Libby 		 * represent a single item.  We will try to fit as many
232027ca37acSRyan Libby 		 * additional items into the slab as possible.
23214a8b575cSRyan Libby 		 */
232227ca37acSRyan Libby 		slabsize = round_page(keg->uk_size);
23231ca6ed45SGleb Smirnoff 	}
2324ad97af7eSGleb Smirnoff 
232527ca37acSRyan Libby 	/* Build a list of all of the available formats for this keg. */
232627ca37acSRyan Libby 	nfmt = 0;
232727ca37acSRyan Libby 
23284a8b575cSRyan Libby 	/* Evaluate an inline slab layout. */
23294a8b575cSRyan Libby 	if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0)
233027ca37acSRyan Libby 		fmts[nfmt++] = 0;
23314a8b575cSRyan Libby 
23324a8b575cSRyan Libby 	/* TODO: vm_page-embedded slab. */
2333244f4554SBosko Milekic 
233420e8e865SBosko Milekic 	/*
2335244f4554SBosko Milekic 	 * We can't do OFFPAGE if we're internal or if we've been
233620e8e865SBosko Milekic 	 * asked to not go to the VM for buckets.  If we do this we
2337bae55c4aSRyan Libby 	 * may end up going to the VM for slabs which we do not want
2338bae55c4aSRyan Libby 	 * to do if we're UMA_ZONE_VM, which clearly forbids it.
2339bae55c4aSRyan Libby 	 * In those cases, evaluate a pseudo-format called INTERNAL
2340bae55c4aSRyan Libby 	 * which has an inline slab header and one extra page to
2341bae55c4aSRyan Libby 	 * guarantee that it fits.
234227ca37acSRyan Libby 	 *
234327ca37acSRyan Libby 	 * Otherwise, see if using an OFFPAGE slab will improve our
234427ca37acSRyan Libby 	 * efficiency.
234520e8e865SBosko Milekic 	 */
2346bae55c4aSRyan Libby 	if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0)
234727ca37acSRyan Libby 		fmts[nfmt++] = UMA_ZFLAG_INTERNAL;
234827ca37acSRyan Libby 	else
234927ca37acSRyan Libby 		fmts[nfmt++] = UMA_ZFLAG_OFFPAGE;
2350244f4554SBosko Milekic 
2351ef72505eSJeff Roberson 	/*
235227ca37acSRyan Libby 	 * Choose a slab size and format which satisfy the minimum efficiency.
235327ca37acSRyan Libby 	 * Prefer the smallest slab size that meets the constraints.
2354ef72505eSJeff Roberson 	 *
235527ca37acSRyan Libby 	 * Start with a minimum slab size, to accommodate CACHESPREAD.  Then,
235627ca37acSRyan Libby 	 * for small items (up to PAGE_SIZE), the iteration increment is one
235727ca37acSRyan Libby 	 * page; and for large items, the increment is one item.
2358ef72505eSJeff Roberson 	 */
235927ca37acSRyan Libby 	i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize);
236027ca37acSRyan Libby 	KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u",
236127ca37acSRyan Libby 	    keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize,
236227ca37acSRyan Libby 	    rsize, i));
236327ca37acSRyan Libby 	for ( ; ; i++) {
236427ca37acSRyan Libby 		slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) :
236527ca37acSRyan Libby 		    round_page(rsize * (i - 1) + keg->uk_size);
236627ca37acSRyan Libby 
236727ca37acSRyan Libby 		for (j = 0; j < nfmt; j++) {
236827ca37acSRyan Libby 			/* Only if we have no viable format yet. */
236927ca37acSRyan Libby 			if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 &&
237027ca37acSRyan Libby 			    kl.ipers > 0)
237127ca37acSRyan Libby 				continue;
237227ca37acSRyan Libby 
237327ca37acSRyan Libby 			keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp);
237427ca37acSRyan Libby 			if (kl_tmp.eff <= kl.eff)
237527ca37acSRyan Libby 				continue;
237627ca37acSRyan Libby 
237727ca37acSRyan Libby 			kl = kl_tmp;
237827ca37acSRyan Libby 
237927ca37acSRyan Libby 			CTR6(KTR_UMA, "keg %s layout: format %#x "
238027ca37acSRyan Libby 			    "(ipers %u * rsize %u) / slabsize %#x = %u%% eff",
238127ca37acSRyan Libby 			    keg->uk_name, kl.format, kl.ipers, rsize,
238227ca37acSRyan Libby 			    kl.slabsize, UMA_FIXPT_PCT(kl.eff));
238327ca37acSRyan Libby 
238427ca37acSRyan Libby 			/* Stop when we reach the minimum efficiency. */
238527ca37acSRyan Libby 			if (kl.eff >= UMA_MIN_EFF)
238627ca37acSRyan Libby 				break;
23878355f576SJeff Roberson 		}
2388ad97af7eSGleb Smirnoff 
238933e5a1eaSRyan Libby 		if (kl.eff >= UMA_MIN_EFF || !multipage_slabs ||
239027ca37acSRyan Libby 		    slabsize >= SLAB_MAX_SETSIZE * rsize ||
239127ca37acSRyan Libby 		    (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0)
239227ca37acSRyan Libby 			break;
239327ca37acSRyan Libby 	}
239427ca37acSRyan Libby 
239527ca37acSRyan Libby 	pages = atop(kl.slabsize);
239627ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
239727ca37acSRyan Libby 		pages *= mp_maxid + 1;
239827ca37acSRyan Libby 
239927ca37acSRyan Libby 	keg->uk_rsize = rsize;
240027ca37acSRyan Libby 	keg->uk_ipers = kl.ipers;
240127ca37acSRyan Libby 	keg->uk_ppera = pages;
240227ca37acSRyan Libby 	keg->uk_flags |= kl.format;
240327ca37acSRyan Libby 
24044a8b575cSRyan Libby 	/*
24054a8b575cSRyan Libby 	 * How do we find the slab header if it is offpage or if not all item
24064a8b575cSRyan Libby 	 * start addresses are in the same page?  We could solve the latter
24074a8b575cSRyan Libby 	 * case with vaddr alignment, but we don't.
24084a8b575cSRyan Libby 	 */
240927ca37acSRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 ||
241027ca37acSRyan Libby 	    (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) {
241154c5ae80SRyan Libby 		if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0)
241227ca37acSRyan Libby 			keg->uk_flags |= UMA_ZFLAG_HASH;
241354c5ae80SRyan Libby 		else
241427ca37acSRyan Libby 			keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
241554c5ae80SRyan Libby 	}
241627ca37acSRyan Libby 
2417e63a1c2fSRyan Libby 	CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u",
241827ca37acSRyan Libby 	    __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers,
241927ca37acSRyan Libby 	    pages);
24204a8b575cSRyan Libby 	KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE,
24214a8b575cSRyan Libby 	    ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__,
242227ca37acSRyan Libby 	     keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize,
242327ca37acSRyan Libby 	     keg->uk_ipers, pages));
2424e20a199fSJeff Roberson }
2425e20a199fSJeff Roberson 
24268355f576SJeff Roberson /*
2427099a0e58SBosko Milekic  * Keg header ctor.  This initializes all fields, locks, etc.  And inserts
2428099a0e58SBosko Milekic  * the keg onto the global keg list.
24298355f576SJeff Roberson  *
24308355f576SJeff Roberson  * Arguments/Returns follow uma_ctor specifications
2431099a0e58SBosko Milekic  *	udata  Actually uma_kctor_args
2432099a0e58SBosko Milekic  */
2433b23f72e9SBrian Feldman static int
2434b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags)
2435099a0e58SBosko Milekic {
2436099a0e58SBosko Milekic 	struct uma_kctor_args *arg = udata;
2437099a0e58SBosko Milekic 	uma_keg_t keg = mem;
2438099a0e58SBosko Milekic 	uma_zone_t zone;
24398b987a77SJeff Roberson 	int i;
2440099a0e58SBosko Milekic 
2441099a0e58SBosko Milekic 	bzero(keg, size);
2442099a0e58SBosko Milekic 	keg->uk_size = arg->size;
2443099a0e58SBosko Milekic 	keg->uk_init = arg->uminit;
2444099a0e58SBosko Milekic 	keg->uk_fini = arg->fini;
2445099a0e58SBosko Milekic 	keg->uk_align = arg->align;
24466fd34d6fSJeff Roberson 	keg->uk_reserve = 0;
2447099a0e58SBosko Milekic 	keg->uk_flags = arg->flags;
2448099a0e58SBosko Milekic 
2449099a0e58SBosko Milekic 	/*
2450194a979eSMark Johnston 	 * We use a global round-robin policy by default.  Zones with
2451dfe13344SJeff Roberson 	 * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which
2452dfe13344SJeff Roberson 	 * case the iterator is never run.
2453194a979eSMark Johnston 	 */
2454194a979eSMark Johnston 	keg->uk_dr.dr_policy = DOMAINSET_RR();
2455194a979eSMark Johnston 	keg->uk_dr.dr_iter = 0;
2456194a979eSMark Johnston 
2457194a979eSMark Johnston 	/*
2458c8b0a88bSJeff Roberson 	 * The primary zone is passed to us at keg-creation time.
2459099a0e58SBosko Milekic 	 */
2460099a0e58SBosko Milekic 	zone = arg->zone;
2461e20a199fSJeff Roberson 	keg->uk_name = zone->uz_name;
2462099a0e58SBosko Milekic 
2463099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_ZINIT)
2464099a0e58SBosko Milekic 		keg->uk_init = zero_init;
2465099a0e58SBosko Milekic 
2466cfcae3f8SGleb Smirnoff 	if (arg->flags & UMA_ZONE_MALLOC)
246754c5ae80SRyan Libby 		keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
2468e20a199fSJeff Roberson 
246954c5ae80SRyan Libby #ifndef SMP
2470ad97af7eSGleb Smirnoff 	keg->uk_flags &= ~UMA_ZONE_PCPU;
2471ad97af7eSGleb Smirnoff #endif
2472ad97af7eSGleb Smirnoff 
24734a8b575cSRyan Libby 	keg_layout(keg);
2474099a0e58SBosko Milekic 
24758b987a77SJeff Roberson 	/*
2476c6fd3e23SJeff Roberson 	 * Use a first-touch NUMA policy for kegs that pmap_extract() will
2477c6fd3e23SJeff Roberson 	 * work on.  Use round-robin for everything else.
2478dfe13344SJeff Roberson 	 *
2479dfe13344SJeff Roberson 	 * Zones may override the default by specifying either.
24808b987a77SJeff Roberson 	 */
2481dfe13344SJeff Roberson #ifdef NUMA
2482dfe13344SJeff Roberson 	if ((keg->uk_flags &
2483c6fd3e23SJeff Roberson 	    (UMA_ZONE_ROUNDROBIN | UMA_ZFLAG_CACHE | UMA_ZONE_NOTPAGE)) == 0)
2484dfe13344SJeff Roberson 		keg->uk_flags |= UMA_ZONE_FIRSTTOUCH;
2485dfe13344SJeff Roberson 	else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0)
2486dfe13344SJeff Roberson 		keg->uk_flags |= UMA_ZONE_ROUNDROBIN;
24878b987a77SJeff Roberson #endif
24888b987a77SJeff Roberson 
2489099a0e58SBosko Milekic 	/*
2490099a0e58SBosko Milekic 	 * If we haven't booted yet we need allocations to go through the
2491099a0e58SBosko Milekic 	 * startup cache until the vm is ready.
2492099a0e58SBosko Milekic 	 */
249377e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC
2494a81c400eSJeff Roberson 	if (keg->uk_ppera == 1)
249577e19437SGleb Smirnoff 		keg->uk_allocf = uma_small_alloc;
2496a81c400eSJeff Roberson 	else
24978cd02d00SAlan Cox #endif
2498a81c400eSJeff Roberson 	if (booted < BOOT_KVA)
2499a81c400eSJeff Roberson 		keg->uk_allocf = startup_alloc;
2500ab3059a8SMatt Macy 	else if (keg->uk_flags & UMA_ZONE_PCPU)
2501ab3059a8SMatt Macy 		keg->uk_allocf = pcpu_page_alloc;
2502ec0d8280SRyan Libby 	else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1)
2503ec0d8280SRyan Libby 		keg->uk_allocf = contig_alloc;
250477e19437SGleb Smirnoff 	else
250577e19437SGleb Smirnoff 		keg->uk_allocf = page_alloc;
250677e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC
250777e19437SGleb Smirnoff 	if (keg->uk_ppera == 1)
250877e19437SGleb Smirnoff 		keg->uk_freef = uma_small_free;
250977e19437SGleb Smirnoff 	else
251077e19437SGleb Smirnoff #endif
2511ab3059a8SMatt Macy 	if (keg->uk_flags & UMA_ZONE_PCPU)
2512ab3059a8SMatt Macy 		keg->uk_freef = pcpu_page_free;
2513ab3059a8SMatt Macy 	else
251477e19437SGleb Smirnoff 		keg->uk_freef = page_free;
2515099a0e58SBosko Milekic 
2516099a0e58SBosko Milekic 	/*
25178b987a77SJeff Roberson 	 * Initialize keg's locks.
2518099a0e58SBosko Milekic 	 */
25198b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
25208b987a77SJeff Roberson 		KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS));
2521099a0e58SBosko Milekic 
2522099a0e58SBosko Milekic 	/*
2523099a0e58SBosko Milekic 	 * If we're putting the slab header in the actual page we need to
25249b78b1f4SJeff Roberson 	 * figure out where in each page it goes.  See slab_sizeof
25259b78b1f4SJeff Roberson 	 * definition.
2526099a0e58SBosko Milekic 	 */
252754c5ae80SRyan Libby 	if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) {
25289b78b1f4SJeff Roberson 		size_t shsize;
25299b78b1f4SJeff Roberson 
25309b78b1f4SJeff Roberson 		shsize = slab_sizeof(keg->uk_ipers);
25319b78b1f4SJeff Roberson 		keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize;
2532244f4554SBosko Milekic 		/*
2533244f4554SBosko Milekic 		 * The only way the following is possible is if with our
2534244f4554SBosko Milekic 		 * UMA_ALIGN_PTR adjustments we are now bigger than
2535244f4554SBosko Milekic 		 * UMA_SLAB_SIZE.  I haven't checked whether this is
2536244f4554SBosko Milekic 		 * mathematically possible for all cases, so we make
2537244f4554SBosko Milekic 		 * sure here anyway.
2538244f4554SBosko Milekic 		 */
25399b78b1f4SJeff Roberson 		KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera,
25403d5e3df7SGleb Smirnoff 		    ("zone %s ipers %d rsize %d size %d slab won't fit",
25413d5e3df7SGleb Smirnoff 		    zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size));
2542099a0e58SBosko Milekic 	}
2543099a0e58SBosko Milekic 
254454c5ae80SRyan Libby 	if (keg->uk_flags & UMA_ZFLAG_HASH)
25453b2f2cb8SAlexander Motin 		hash_alloc(&keg->uk_hash, 0);
2546099a0e58SBosko Milekic 
2547e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone);
2548099a0e58SBosko Milekic 
2549099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
2550099a0e58SBosko Milekic 
2551111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
2552099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
2553111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
2554b23f72e9SBrian Feldman 	return (0);
2555099a0e58SBosko Milekic }
2556099a0e58SBosko Milekic 
25572efcc8cbSGleb Smirnoff static void
2558a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused)
2559a81c400eSJeff Roberson {
2560a81c400eSJeff Roberson 	uma_keg_t keg;
2561a81c400eSJeff Roberson 
2562a81c400eSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
2563a81c400eSJeff Roberson 		return;
2564a81c400eSJeff Roberson 	KEG_GET(zone, keg);
2565ec0d8280SRyan Libby 
2566ec0d8280SRyan Libby 	if (keg->uk_allocf == startup_alloc) {
2567ec0d8280SRyan Libby 		/* Switch to the real allocator. */
2568f96d4157SJeff Roberson 		if (keg->uk_flags & UMA_ZONE_PCPU)
2569f96d4157SJeff Roberson 			keg->uk_allocf = pcpu_page_alloc;
2570ec0d8280SRyan Libby 		else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 &&
2571ec0d8280SRyan Libby 		    keg->uk_ppera > 1)
2572ec0d8280SRyan Libby 			keg->uk_allocf = contig_alloc;
2573ec0d8280SRyan Libby 		else
2574a81c400eSJeff Roberson 			keg->uk_allocf = page_alloc;
2575a81c400eSJeff Roberson 	}
2576ec0d8280SRyan Libby }
2577a81c400eSJeff Roberson 
2578a81c400eSJeff Roberson static void
257920a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused)
25802efcc8cbSGleb Smirnoff {
25812efcc8cbSGleb Smirnoff 
25822efcc8cbSGleb Smirnoff 	zone->uz_allocs = counter_u64_alloc(M_WAITOK);
25832efcc8cbSGleb Smirnoff 	zone->uz_frees = counter_u64_alloc(M_WAITOK);
25842efcc8cbSGleb Smirnoff 	zone->uz_fails = counter_u64_alloc(M_WAITOK);
2585c6fd3e23SJeff Roberson 	zone->uz_xdomain = counter_u64_alloc(M_WAITOK);
25862efcc8cbSGleb Smirnoff }
25872efcc8cbSGleb Smirnoff 
258820a4e154SJeff Roberson static void
258920a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused)
259020a4e154SJeff Roberson {
259120a4e154SJeff Roberson 	uma_zone_domain_t zdom;
25928b987a77SJeff Roberson 	uma_domain_t dom;
259320a4e154SJeff Roberson 	uma_keg_t keg;
259420a4e154SJeff Roberson 	struct sysctl_oid *oid, *domainoid;
25953b490537SJeff Roberson 	int domains, i, cnt;
259620a4e154SJeff Roberson 	static const char *nokeg = "cache zone";
259720a4e154SJeff Roberson 	char *c;
259820a4e154SJeff Roberson 
259920a4e154SJeff Roberson 	/*
260020a4e154SJeff Roberson 	 * Make a sysctl safe copy of the zone name by removing
260120a4e154SJeff Roberson 	 * any special characters and handling dups by appending
260220a4e154SJeff Roberson 	 * an index.
260320a4e154SJeff Roberson 	 */
260420a4e154SJeff Roberson 	if (zone->uz_namecnt != 0) {
26053b490537SJeff Roberson 		/* Count the number of decimal digits and '_' separator. */
26063b490537SJeff Roberson 		for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++)
26073b490537SJeff Roberson 			cnt /= 10;
26083b490537SJeff Roberson 		zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1,
26093b490537SJeff Roberson 		    M_UMA, M_WAITOK);
261020a4e154SJeff Roberson 		sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name,
261120a4e154SJeff Roberson 		    zone->uz_namecnt);
261220a4e154SJeff Roberson 	} else
261320a4e154SJeff Roberson 		zone->uz_ctlname = strdup(zone->uz_name, M_UMA);
261420a4e154SJeff Roberson 	for (c = zone->uz_ctlname; *c != '\0'; c++)
261520a4e154SJeff Roberson 		if (strchr("./\\ -", *c) != NULL)
261620a4e154SJeff Roberson 			*c = '_';
261720a4e154SJeff Roberson 
261820a4e154SJeff Roberson 	/*
261920a4e154SJeff Roberson 	 * Basic parameters at the root.
262020a4e154SJeff Roberson 	 */
262120a4e154SJeff Roberson 	zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma),
26227029da5cSPawel Biernacki 	    OID_AUTO, zone->uz_ctlname, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
262320a4e154SJeff Roberson 	oid = zone->uz_oid;
262420a4e154SJeff Roberson 	SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
262520a4e154SJeff Roberson 	    "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size");
26266d204a6aSRyan Libby 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
26276d204a6aSRyan Libby 	    "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE,
26286d204a6aSRyan Libby 	    zone, 0, sysctl_handle_uma_zone_flags, "A",
262920a4e154SJeff Roberson 	    "Allocator configuration flags");
263020a4e154SJeff Roberson 	SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
263120a4e154SJeff Roberson 	    "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0,
263220a4e154SJeff Roberson 	    "Desired per-cpu cache size");
263320a4e154SJeff Roberson 	SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
263420a4e154SJeff Roberson 	    "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0,
263520a4e154SJeff Roberson 	    "Maximum allowed per-cpu cache size");
263620a4e154SJeff Roberson 
263720a4e154SJeff Roberson 	/*
263820a4e154SJeff Roberson 	 * keg if present.
263920a4e154SJeff Roberson 	 */
264054c5ae80SRyan Libby 	if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0)
26418b987a77SJeff Roberson 		domains = vm_ndomains;
26428b987a77SJeff Roberson 	else
26438b987a77SJeff Roberson 		domains = 1;
264420a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
26457029da5cSPawel Biernacki 	    "keg", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
264620a4e154SJeff Roberson 	keg = zone->uz_keg;
26473b490537SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) {
264820a4e154SJeff Roberson 		SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
264920a4e154SJeff Roberson 		    "name", CTLFLAG_RD, keg->uk_name, "Keg name");
265020a4e154SJeff Roberson 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
265120a4e154SJeff Roberson 		    "rsize", CTLFLAG_RD, &keg->uk_rsize, 0,
265220a4e154SJeff Roberson 		    "Real object size with alignment");
265320a4e154SJeff Roberson 		SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
265420a4e154SJeff Roberson 		    "ppera", CTLFLAG_RD, &keg->uk_ppera, 0,
265520a4e154SJeff Roberson 		    "pages per-slab allocation");
265620a4e154SJeff Roberson 		SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
265720a4e154SJeff Roberson 		    "ipers", CTLFLAG_RD, &keg->uk_ipers, 0,
265820a4e154SJeff Roberson 		    "items available per-slab");
265920a4e154SJeff Roberson 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
266020a4e154SJeff Roberson 		    "align", CTLFLAG_RD, &keg->uk_align, 0,
266120a4e154SJeff Roberson 		    "item alignment mask");
2662f09cbea3SMark Johnston 		SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2663f09cbea3SMark Johnston 		    "reserve", CTLFLAG_RD, &keg->uk_reserve, 0,
2664f09cbea3SMark Johnston 		    "number of reserved items");
2665f7af5015SRyan Libby 		SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2666f7af5015SRyan Libby 		    "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
2667f7af5015SRyan Libby 		    keg, 0, sysctl_handle_uma_slab_efficiency, "I",
2668f7af5015SRyan Libby 		    "Slab utilization (100 - internal fragmentation %)");
26698b987a77SJeff Roberson 		domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid),
26707029da5cSPawel Biernacki 		    OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
26718b987a77SJeff Roberson 		for (i = 0; i < domains; i++) {
26728b987a77SJeff Roberson 			dom = &keg->uk_domain[i];
26738b987a77SJeff Roberson 			oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
26747029da5cSPawel Biernacki 			    OID_AUTO, VM_DOMAIN(i)->vmd_name,
26757029da5cSPawel Biernacki 			    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
26768b987a77SJeff Roberson 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
26778b987a77SJeff Roberson 			    "pages", CTLFLAG_RD, &dom->ud_pages, 0,
26788b987a77SJeff Roberson 			    "Total pages currently allocated from VM");
26798b987a77SJeff Roberson 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
26804ab3aee8SMark Johnston 			    "free_items", CTLFLAG_RD, &dom->ud_free_items, 0,
2681d6e77cdaSMark Johnston 			    "Items free in the slab layer");
2682d6e77cdaSMark Johnston 			SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2683d6e77cdaSMark Johnston 			    "free_slabs", CTLFLAG_RD, &dom->ud_free_slabs, 0,
2684d6e77cdaSMark Johnston 			    "Unused slabs");
26858b987a77SJeff Roberson 		}
268620a4e154SJeff Roberson 	} else
268720a4e154SJeff Roberson 		SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
268820a4e154SJeff Roberson 		    "name", CTLFLAG_RD, nokeg, "Keg name");
268920a4e154SJeff Roberson 
269020a4e154SJeff Roberson 	/*
269120a4e154SJeff Roberson 	 * Information about zone limits.
269220a4e154SJeff Roberson 	 */
269320a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
26947029da5cSPawel Biernacki 	    "limit", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
26954bd61e19SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
26964bd61e19SJeff Roberson 	    "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
26974bd61e19SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_items, "QU",
2698e574d407SMark Johnston 	    "Current number of allocated items if limit is set");
269920a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
270020a4e154SJeff Roberson 	    "max_items", CTLFLAG_RD, &zone->uz_max_items, 0,
2701e574d407SMark Johnston 	    "Maximum number of allocated and cached items");
270220a4e154SJeff Roberson 	SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
270320a4e154SJeff Roberson 	    "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0,
270420a4e154SJeff Roberson 	    "Number of threads sleeping at limit");
270520a4e154SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
270620a4e154SJeff Roberson 	    "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0,
270720a4e154SJeff Roberson 	    "Total zone limit sleeps");
27084bd61e19SJeff Roberson 	SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2709c6fd3e23SJeff Roberson 	    "bucket_max", CTLFLAG_RD, &zone->uz_bucket_max, 0,
2710c6fd3e23SJeff Roberson 	    "Maximum number of items in each domain's bucket cache");
271120a4e154SJeff Roberson 
271220a4e154SJeff Roberson 	/*
27138b987a77SJeff Roberson 	 * Per-domain zone information.
271420a4e154SJeff Roberson 	 */
271520a4e154SJeff Roberson 	domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid),
27167029da5cSPawel Biernacki 	    OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
271720a4e154SJeff Roberson 	for (i = 0; i < domains; i++) {
2718c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(zone, i);
271920a4e154SJeff Roberson 		oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
27207029da5cSPawel Biernacki 		    OID_AUTO, VM_DOMAIN(i)->vmd_name,
27217029da5cSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
272220a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
272320a4e154SJeff Roberson 		    "nitems", CTLFLAG_RD, &zdom->uzd_nitems,
272420a4e154SJeff Roberson 		    "number of items in this domain");
272520a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
272620a4e154SJeff Roberson 		    "imax", CTLFLAG_RD, &zdom->uzd_imax,
272720a4e154SJeff Roberson 		    "maximum item count in this period");
272820a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
272920a4e154SJeff Roberson 		    "imin", CTLFLAG_RD, &zdom->uzd_imin,
273020a4e154SJeff Roberson 		    "minimum item count in this period");
273120a4e154SJeff Roberson 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
27322760658bSAlexander Motin 		    "bimin", CTLFLAG_RD, &zdom->uzd_bimin,
27332760658bSAlexander Motin 		    "Minimum item count in this batch");
27342760658bSAlexander Motin 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
273520a4e154SJeff Roberson 		    "wss", CTLFLAG_RD, &zdom->uzd_wss,
273620a4e154SJeff Roberson 		    "Working set size");
27372760658bSAlexander Motin 		SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
27382760658bSAlexander Motin 		    "limin", CTLFLAG_RD, &zdom->uzd_limin,
27392760658bSAlexander Motin 		    "Long time minimum item count");
27402760658bSAlexander Motin 		SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
27412760658bSAlexander Motin 		    "timin", CTLFLAG_RD, &zdom->uzd_timin, 0,
27422760658bSAlexander Motin 		    "Time since zero long time minimum item count");
274320a4e154SJeff Roberson 	}
274420a4e154SJeff Roberson 
274520a4e154SJeff Roberson 	/*
274620a4e154SJeff Roberson 	 * General statistics.
274720a4e154SJeff Roberson 	 */
274820a4e154SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
27497029da5cSPawel Biernacki 	    "stats", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
275020a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
275120a4e154SJeff Roberson 	    "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
275220a4e154SJeff Roberson 	    zone, 1, sysctl_handle_uma_zone_cur, "I",
275320a4e154SJeff Roberson 	    "Current number of allocated items");
275420a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
275520a4e154SJeff Roberson 	    "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
275620a4e154SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_allocs, "QU",
275720a4e154SJeff Roberson 	    "Total allocation calls");
275820a4e154SJeff Roberson 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
275920a4e154SJeff Roberson 	    "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
276020a4e154SJeff Roberson 	    zone, 0, sysctl_handle_uma_zone_frees, "QU",
276120a4e154SJeff Roberson 	    "Total free calls");
276220a4e154SJeff Roberson 	SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
276320a4e154SJeff Roberson 	    "fails", CTLFLAG_RD, &zone->uz_fails,
276420a4e154SJeff Roberson 	    "Number of allocation failures");
2765c6fd3e23SJeff Roberson 	SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2766c6fd3e23SJeff Roberson 	    "xdomain", CTLFLAG_RD, &zone->uz_xdomain,
276720a4e154SJeff Roberson 	    "Free calls from the wrong domain");
276820a4e154SJeff Roberson }
276920a4e154SJeff Roberson 
277020a4e154SJeff Roberson struct uma_zone_count {
277120a4e154SJeff Roberson 	const char	*name;
277220a4e154SJeff Roberson 	int		count;
277320a4e154SJeff Roberson };
277420a4e154SJeff Roberson 
277520a4e154SJeff Roberson static void
277620a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg)
277720a4e154SJeff Roberson {
277820a4e154SJeff Roberson 	struct uma_zone_count *cnt;
277920a4e154SJeff Roberson 
278020a4e154SJeff Roberson 	cnt = arg;
27813b490537SJeff Roberson 	/*
27823b490537SJeff Roberson 	 * Some zones are rapidly created with identical names and
27833b490537SJeff Roberson 	 * destroyed out of order.  This can lead to gaps in the count.
27843b490537SJeff Roberson 	 * Use one greater than the maximum observed for this name.
27853b490537SJeff Roberson 	 */
278620a4e154SJeff Roberson 	if (strcmp(zone->uz_name, cnt->name) == 0)
27873b490537SJeff Roberson 		cnt->count = MAX(cnt->count,
27883b490537SJeff Roberson 		    zone->uz_namecnt + 1);
278920a4e154SJeff Roberson }
279020a4e154SJeff Roberson 
2791cc7ce83aSJeff Roberson static void
2792cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone)
2793cc7ce83aSJeff Roberson {
2794cc7ce83aSJeff Roberson 	int i;
2795cc7ce83aSJeff Roberson 
2796cc7ce83aSJeff Roberson 	for (i = 0; i <= mp_maxid; i++) {
2797cc7ce83aSJeff Roberson 		cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size);
2798cc7ce83aSJeff Roberson 		cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags);
2799cc7ce83aSJeff Roberson 	}
2800cc7ce83aSJeff Roberson }
2801cc7ce83aSJeff Roberson 
2802099a0e58SBosko Milekic /*
2803099a0e58SBosko Milekic  * Zone header ctor.  This initializes all fields, locks, etc.
2804099a0e58SBosko Milekic  *
2805099a0e58SBosko Milekic  * Arguments/Returns follow uma_ctor specifications
2806099a0e58SBosko Milekic  *	udata  Actually uma_zctor_args
28078355f576SJeff Roberson  */
2808b23f72e9SBrian Feldman static int
2809b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags)
28108355f576SJeff Roberson {
281120a4e154SJeff Roberson 	struct uma_zone_count cnt;
28128355f576SJeff Roberson 	struct uma_zctor_args *arg = udata;
2813c6fd3e23SJeff Roberson 	uma_zone_domain_t zdom;
28148355f576SJeff Roberson 	uma_zone_t zone = mem;
2815099a0e58SBosko Milekic 	uma_zone_t z;
2816099a0e58SBosko Milekic 	uma_keg_t keg;
281708cfa56eSMark Johnston 	int i;
28188355f576SJeff Roberson 
28198355f576SJeff Roberson 	bzero(zone, size);
28208355f576SJeff Roberson 	zone->uz_name = arg->name;
28218355f576SJeff Roberson 	zone->uz_ctor = arg->ctor;
28228355f576SJeff Roberson 	zone->uz_dtor = arg->dtor;
2823099a0e58SBosko Milekic 	zone->uz_init = NULL;
2824099a0e58SBosko Milekic 	zone->uz_fini = NULL;
2825bf965959SSean Bruno 	zone->uz_sleeps = 0;
282620a4e154SJeff Roberson 	zone->uz_bucket_size = 0;
282720a4e154SJeff Roberson 	zone->uz_bucket_size_min = 0;
282820a4e154SJeff Roberson 	zone->uz_bucket_size_max = BUCKET_MAX;
2829d4665eaaSJeff Roberson 	zone->uz_flags = (arg->flags & UMA_ZONE_SMR);
28302f891cd5SPawel Jakub Dawidek 	zone->uz_warning = NULL;
2831ab3185d1SJeff Roberson 	/* The domain structures follow the cpu structures. */
2832c6fd3e23SJeff Roberson 	zone->uz_bucket_max = ULONG_MAX;
28332f891cd5SPawel Jakub Dawidek 	timevalclear(&zone->uz_ratecheck);
2834af526374SJeff Roberson 
283520a4e154SJeff Roberson 	/* Count the number of duplicate names. */
283620a4e154SJeff Roberson 	cnt.name = arg->name;
283720a4e154SJeff Roberson 	cnt.count = 0;
283820a4e154SJeff Roberson 	zone_foreach(zone_count, &cnt);
283920a4e154SJeff Roberson 	zone->uz_namecnt = cnt.count;
284091d947bfSJeff Roberson 	ZONE_CROSS_LOCK_INIT(zone);
28412efcc8cbSGleb Smirnoff 
2842c6fd3e23SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
2843c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(zone, i);
2844c6fd3e23SJeff Roberson 		ZDOM_LOCK_INIT(zone, zdom, (arg->flags & UMA_ZONE_MTXCLASS));
2845c6fd3e23SJeff Roberson 		STAILQ_INIT(&zdom->uzd_buckets);
2846c6fd3e23SJeff Roberson 	}
284708cfa56eSMark Johnston 
284810094910SMark Johnston #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN)
2849ca293436SRyan Libby 	if (arg->uminit == trash_init && arg->fini == trash_fini)
2850cc7ce83aSJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR;
285109c8cb71SMark Johnston #elif defined(KASAN)
285209c8cb71SMark Johnston 	if ((arg->flags & (UMA_ZONE_NOFREE | UMA_ZFLAG_CACHE)) != 0)
285309c8cb71SMark Johnston 		arg->flags |= UMA_ZONE_NOKASAN;
2854ca293436SRyan Libby #endif
2855ca293436SRyan Libby 
28560095a784SJeff Roberson 	/*
28570095a784SJeff Roberson 	 * This is a pure cache zone, no kegs.
28580095a784SJeff Roberson 	 */
28590095a784SJeff Roberson 	if (arg->import) {
2860727c6918SJeff Roberson 		KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0,
2861727c6918SJeff Roberson 		    ("zone_ctor: Import specified for non-cache zone."));
28626fd34d6fSJeff Roberson 		zone->uz_flags = arg->flags;
2863af526374SJeff Roberson 		zone->uz_size = arg->size;
28640095a784SJeff Roberson 		zone->uz_import = arg->import;
28650095a784SJeff Roberson 		zone->uz_release = arg->release;
28660095a784SJeff Roberson 		zone->uz_arg = arg->arg;
2867c6fd3e23SJeff Roberson #ifdef NUMA
2868c6fd3e23SJeff Roberson 		/*
2869c6fd3e23SJeff Roberson 		 * Cache zones are round-robin unless a policy is
2870c6fd3e23SJeff Roberson 		 * specified because they may have incompatible
2871c6fd3e23SJeff Roberson 		 * constraints.
2872c6fd3e23SJeff Roberson 		 */
2873c6fd3e23SJeff Roberson 		if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0)
2874c6fd3e23SJeff Roberson 			zone->uz_flags |= UMA_ZONE_ROUNDROBIN;
2875c6fd3e23SJeff Roberson #endif
2876111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
287703175483SAlexander Motin 		LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
2878111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
2879af526374SJeff Roberson 		goto out;
28800095a784SJeff Roberson 	}
28810095a784SJeff Roberson 
28820095a784SJeff Roberson 	/*
28830095a784SJeff Roberson 	 * Use the regular zone/keg/slab allocator.
28840095a784SJeff Roberson 	 */
2885b75c4efcSAndrew Turner 	zone->uz_import = zone_import;
2886b75c4efcSAndrew Turner 	zone->uz_release = zone_release;
28870095a784SJeff Roberson 	zone->uz_arg = zone;
2888bb15d1c7SGleb Smirnoff 	keg = arg->keg;
28890095a784SJeff Roberson 
2890099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_SECONDARY) {
289120a4e154SJeff Roberson 		KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
289220a4e154SJeff Roberson 		    ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
2893099a0e58SBosko Milekic 		KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
28948355f576SJeff Roberson 		zone->uz_init = arg->uminit;
2895e221e841SJeff Roberson 		zone->uz_fini = arg->fini;
2896e20a199fSJeff Roberson 		zone->uz_flags |= UMA_ZONE_SECONDARY;
2897111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
2898099a0e58SBosko Milekic 		ZONE_LOCK(zone);
2899099a0e58SBosko Milekic 		LIST_FOREACH(z, &keg->uk_zones, uz_link) {
2900099a0e58SBosko Milekic 			if (LIST_NEXT(z, uz_link) == NULL) {
2901099a0e58SBosko Milekic 				LIST_INSERT_AFTER(z, zone, uz_link);
2902099a0e58SBosko Milekic 				break;
2903099a0e58SBosko Milekic 			}
2904099a0e58SBosko Milekic 		}
2905099a0e58SBosko Milekic 		ZONE_UNLOCK(zone);
2906111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
2907e20a199fSJeff Roberson 	} else if (keg == NULL) {
2908e20a199fSJeff Roberson 		if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
2909e20a199fSJeff Roberson 		    arg->align, arg->flags)) == NULL)
2910b23f72e9SBrian Feldman 			return (ENOMEM);
2911099a0e58SBosko Milekic 	} else {
2912099a0e58SBosko Milekic 		struct uma_kctor_args karg;
2913b23f72e9SBrian Feldman 		int error;
2914099a0e58SBosko Milekic 
2915099a0e58SBosko Milekic 		/* We should only be here from uma_startup() */
2916099a0e58SBosko Milekic 		karg.size = arg->size;
2917099a0e58SBosko Milekic 		karg.uminit = arg->uminit;
2918099a0e58SBosko Milekic 		karg.fini = arg->fini;
2919099a0e58SBosko Milekic 		karg.align = arg->align;
2920d4665eaaSJeff Roberson 		karg.flags = (arg->flags & ~UMA_ZONE_SMR);
2921099a0e58SBosko Milekic 		karg.zone = zone;
2922b23f72e9SBrian Feldman 		error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
2923b23f72e9SBrian Feldman 		    flags);
2924b23f72e9SBrian Feldman 		if (error)
2925b23f72e9SBrian Feldman 			return (error);
2926099a0e58SBosko Milekic 	}
29270095a784SJeff Roberson 
292820a4e154SJeff Roberson 	/* Inherit properties from the keg. */
2929bb15d1c7SGleb Smirnoff 	zone->uz_keg = keg;
2930e20a199fSJeff Roberson 	zone->uz_size = keg->uk_size;
2931e20a199fSJeff Roberson 	zone->uz_flags |= (keg->uk_flags &
2932e20a199fSJeff Roberson 	    (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
29338355f576SJeff Roberson 
293420a4e154SJeff Roberson out:
2935dc2b3205SMark Johnston 	if (booted >= BOOT_PCPU) {
293620a4e154SJeff Roberson 		zone_alloc_counters(zone, NULL);
2937dc2b3205SMark Johnston 		if (booted >= BOOT_RUNNING)
293820a4e154SJeff Roberson 			zone_alloc_sysctl(zone, NULL);
293920a4e154SJeff Roberson 	} else {
294020a4e154SJeff Roberson 		zone->uz_allocs = EARLY_COUNTER;
294120a4e154SJeff Roberson 		zone->uz_frees = EARLY_COUNTER;
294220a4e154SJeff Roberson 		zone->uz_fails = EARLY_COUNTER;
2943099a0e58SBosko Milekic 	}
29448355f576SJeff Roberson 
2945d4665eaaSJeff Roberson 	/* Caller requests a private SMR context. */
2946d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
2947226dd6dbSJeff Roberson 		zone->uz_smr = smr_create(zone->uz_name, 0, 0);
2948d4665eaaSJeff Roberson 
29497e28037aSMark Johnston 	KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) !=
29507e28037aSMark Johnston 	    (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET),
29517e28037aSMark Johnston 	    ("Invalid zone flag combination"));
295220a4e154SJeff Roberson 	if (arg->flags & UMA_ZFLAG_INTERNAL)
295320a4e154SJeff Roberson 		zone->uz_bucket_size_max = zone->uz_bucket_size = 0;
295420a4e154SJeff Roberson 	if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0)
295520a4e154SJeff Roberson 		zone->uz_bucket_size = BUCKET_MAX;
295620a4e154SJeff Roberson 	else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0)
295720a4e154SJeff Roberson 		zone->uz_bucket_size = 0;
29587e28037aSMark Johnston 	else
295920a4e154SJeff Roberson 		zone->uz_bucket_size = bucket_select(zone->uz_size);
296020a4e154SJeff Roberson 	zone->uz_bucket_size_min = zone->uz_bucket_size;
2961cc7ce83aSJeff Roberson 	if (zone->uz_dtor != NULL || zone->uz_ctor != NULL)
2962cc7ce83aSJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_CTORDTOR;
2963cc7ce83aSJeff Roberson 	zone_update_caches(zone);
2964fc03d22bSJeff Roberson 
2965b23f72e9SBrian Feldman 	return (0);
29668355f576SJeff Roberson }
29678355f576SJeff Roberson 
29688355f576SJeff Roberson /*
2969099a0e58SBosko Milekic  * Keg header dtor.  This frees all data, destroys locks, frees the hash
2970099a0e58SBosko Milekic  * table and removes the keg from the global list.
29719c2cd7e5SJeff Roberson  *
29729c2cd7e5SJeff Roberson  * Arguments/Returns follow uma_dtor specifications
29739c2cd7e5SJeff Roberson  *	udata  unused
29749c2cd7e5SJeff Roberson  */
2975099a0e58SBosko Milekic static void
2976099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata)
2977099a0e58SBosko Milekic {
2978099a0e58SBosko Milekic 	uma_keg_t keg;
29798b987a77SJeff Roberson 	uint32_t free, pages;
29808b987a77SJeff Roberson 	int i;
29819c2cd7e5SJeff Roberson 
2982099a0e58SBosko Milekic 	keg = (uma_keg_t)arg;
29838b987a77SJeff Roberson 	free = pages = 0;
29848b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
29854ab3aee8SMark Johnston 		free += keg->uk_domain[i].ud_free_items;
29868b987a77SJeff Roberson 		pages += keg->uk_domain[i].ud_pages;
29878b987a77SJeff Roberson 		KEG_LOCK_FINI(keg, i);
2988099a0e58SBosko Milekic 	}
29897e240677SRyan Libby 	if (pages != 0)
29908b987a77SJeff Roberson 		printf("Freed UMA keg (%s) was not empty (%u items). "
29918b987a77SJeff Roberson 		    " Lost %u pages of memory.\n",
29928b987a77SJeff Roberson 		    keg->uk_name ? keg->uk_name : "",
29937e240677SRyan Libby 		    pages / keg->uk_ppera * keg->uk_ipers - free, pages);
2994099a0e58SBosko Milekic 
2995099a0e58SBosko Milekic 	hash_free(&keg->uk_hash);
2996099a0e58SBosko Milekic }
2997099a0e58SBosko Milekic 
2998099a0e58SBosko Milekic /*
2999099a0e58SBosko Milekic  * Zone header dtor.
3000099a0e58SBosko Milekic  *
3001099a0e58SBosko Milekic  * Arguments/Returns follow uma_dtor specifications
3002099a0e58SBosko Milekic  *	udata  unused
3003099a0e58SBosko Milekic  */
30049c2cd7e5SJeff Roberson static void
30059c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata)
30069c2cd7e5SJeff Roberson {
30079c2cd7e5SJeff Roberson 	uma_zone_t zone;
3008099a0e58SBosko Milekic 	uma_keg_t keg;
3009c6fd3e23SJeff Roberson 	int i;
30109c2cd7e5SJeff Roberson 
30119c2cd7e5SJeff Roberson 	zone = (uma_zone_t)arg;
30129643769aSJeff Roberson 
301320a4e154SJeff Roberson 	sysctl_remove_oid(zone->uz_oid, 1, 1);
301420a4e154SJeff Roberson 
3015e20a199fSJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
30169643769aSJeff Roberson 		cache_drain(zone);
3017099a0e58SBosko Milekic 
3018111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
3019099a0e58SBosko Milekic 	LIST_REMOVE(zone, uz_link);
3020111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
30217b516613SJonathan T. Looney 	if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) {
30227b516613SJonathan T. Looney 		keg = zone->uz_keg;
30237b516613SJonathan T. Looney 		keg->uk_reserve = 0;
30247b516613SJonathan T. Looney 	}
3025aabe13f1SMark Johnston 	zone_reclaim(zone, UMA_ANYDOMAIN, M_WAITOK, true);
3026c6fd3e23SJeff Roberson 
3027e20a199fSJeff Roberson 	/*
3028323ad386STycho Nightingale 	 * We only destroy kegs from non secondary/non cache zones.
3029e20a199fSJeff Roberson 	 */
3030323ad386STycho Nightingale 	if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) {
3031323ad386STycho Nightingale 		keg = zone->uz_keg;
3032111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
3033099a0e58SBosko Milekic 		LIST_REMOVE(keg, uk_link);
3034111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
30350095a784SJeff Roberson 		zone_free_item(kegs, keg, NULL, SKIP_NONE);
30369c2cd7e5SJeff Roberson 	}
30372efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_allocs);
30382efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_frees);
30392efcc8cbSGleb Smirnoff 	counter_u64_free(zone->uz_fails);
3040c6fd3e23SJeff Roberson 	counter_u64_free(zone->uz_xdomain);
304120a4e154SJeff Roberson 	free(zone->uz_ctlname, M_UMA);
3042c6fd3e23SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
3043c6fd3e23SJeff Roberson 		ZDOM_LOCK_FINI(ZDOM_GET(zone, i));
304491d947bfSJeff Roberson 	ZONE_CROSS_LOCK_FINI(zone);
3045099a0e58SBosko Milekic }
3046099a0e58SBosko Milekic 
3047a81c400eSJeff Roberson static void
3048a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg)
3049a81c400eSJeff Roberson {
3050a81c400eSJeff Roberson 	uma_keg_t keg;
3051a81c400eSJeff Roberson 	uma_zone_t zone;
3052a81c400eSJeff Roberson 
3053a81c400eSJeff Roberson 	LIST_FOREACH(keg, &uma_kegs, uk_link) {
3054a81c400eSJeff Roberson 		LIST_FOREACH(zone, &keg->uk_zones, uz_link)
3055a81c400eSJeff Roberson 			zfunc(zone, arg);
3056a81c400eSJeff Roberson 	}
3057a81c400eSJeff Roberson 	LIST_FOREACH(zone, &uma_cachezones, uz_link)
3058a81c400eSJeff Roberson 		zfunc(zone, arg);
3059a81c400eSJeff Roberson }
3060a81c400eSJeff Roberson 
30619c2cd7e5SJeff Roberson /*
30628355f576SJeff Roberson  * Traverses every zone in the system and calls a callback
30638355f576SJeff Roberson  *
30648355f576SJeff Roberson  * Arguments:
30658355f576SJeff Roberson  *	zfunc  A pointer to a function which accepts a zone
30668355f576SJeff Roberson  *		as an argument.
30678355f576SJeff Roberson  *
30688355f576SJeff Roberson  * Returns:
30698355f576SJeff Roberson  *	Nothing
30708355f576SJeff Roberson  */
30718355f576SJeff Roberson static void
307220a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg)
30738355f576SJeff Roberson {
30748355f576SJeff Roberson 
3075111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
3076a81c400eSJeff Roberson 	zone_foreach_unlocked(zfunc, arg);
3077111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
30788355f576SJeff Roberson }
30798355f576SJeff Roberson 
3080f4bef67cSGleb Smirnoff /*
3081a81c400eSJeff Roberson  * Initialize the kernel memory allocator.  This is done after pages can be
3082a81c400eSJeff Roberson  * allocated but before general KVA is available.
3083f4bef67cSGleb Smirnoff  */
3084a81c400eSJeff Roberson void
3085a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail)
3086f4bef67cSGleb Smirnoff {
3087a81c400eSJeff Roberson 	struct uma_zctor_args args;
3088a81c400eSJeff Roberson 	size_t ksize, zsize, size;
3089c8b0a88bSJeff Roberson 	uma_keg_t primarykeg;
3090a81c400eSJeff Roberson 	uintptr_t m;
309181302f1dSMark Johnston 	int domain;
3092a81c400eSJeff Roberson 	uint8_t pflag;
3093a81c400eSJeff Roberson 
3094a81c400eSJeff Roberson 	bootstart = bootmem = virtual_avail;
3095a81c400eSJeff Roberson 
3096a81c400eSJeff Roberson 	rw_init(&uma_rwlock, "UMA lock");
3097a81c400eSJeff Roberson 	sx_init(&uma_reclaim_lock, "umareclaim");
3098f4bef67cSGleb Smirnoff 
3099f4bef67cSGleb Smirnoff 	ksize = sizeof(struct uma_keg) +
3100f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_domain) * vm_ndomains);
310179c9f942SJeff Roberson 	ksize = roundup(ksize, UMA_SUPER_ALIGN);
3102f4bef67cSGleb Smirnoff 	zsize = sizeof(struct uma_zone) +
3103f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_cache) * (mp_maxid + 1)) +
3104f4bef67cSGleb Smirnoff 	    (sizeof(struct uma_zone_domain) * vm_ndomains);
310579c9f942SJeff Roberson 	zsize = roundup(zsize, UMA_SUPER_ALIGN);
3106f4bef67cSGleb Smirnoff 
3107a81c400eSJeff Roberson 	/* Allocate the zone of zones, zone of kegs, and zone of zones keg. */
3108a81c400eSJeff Roberson 	size = (zsize * 2) + ksize;
310981302f1dSMark Johnston 	for (domain = 0; domain < vm_ndomains; domain++) {
311081302f1dSMark Johnston 		m = (uintptr_t)startup_alloc(NULL, size, domain, &pflag,
311181302f1dSMark Johnston 		    M_NOWAIT | M_ZERO);
311281302f1dSMark Johnston 		if (m != 0)
311381302f1dSMark Johnston 			break;
311481302f1dSMark Johnston 	}
3115ab3185d1SJeff Roberson 	zones = (uma_zone_t)m;
311679c9f942SJeff Roberson 	m += zsize;
3117ab3185d1SJeff Roberson 	kegs = (uma_zone_t)m;
311879c9f942SJeff Roberson 	m += zsize;
3119c8b0a88bSJeff Roberson 	primarykeg = (uma_keg_t)m;
3120ab3185d1SJeff Roberson 
3121099a0e58SBosko Milekic 	/* "manually" create the initial zone */
31220095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
3123099a0e58SBosko Milekic 	args.name = "UMA Kegs";
3124ab3185d1SJeff Roberson 	args.size = ksize;
3125099a0e58SBosko Milekic 	args.ctor = keg_ctor;
3126099a0e58SBosko Milekic 	args.dtor = keg_dtor;
31278355f576SJeff Roberson 	args.uminit = zero_init;
31288355f576SJeff Roberson 	args.fini = NULL;
3129c8b0a88bSJeff Roberson 	args.keg = primarykeg;
313079c9f942SJeff Roberson 	args.align = UMA_SUPER_ALIGN - 1;
3131b60f5b79SJeff Roberson 	args.flags = UMA_ZFLAG_INTERNAL;
3132ab3185d1SJeff Roberson 	zone_ctor(kegs, zsize, &args, M_WAITOK);
31338355f576SJeff Roberson 
3134099a0e58SBosko Milekic 	args.name = "UMA Zones";
3135f4bef67cSGleb Smirnoff 	args.size = zsize;
3136099a0e58SBosko Milekic 	args.ctor = zone_ctor;
3137099a0e58SBosko Milekic 	args.dtor = zone_dtor;
3138099a0e58SBosko Milekic 	args.uminit = zero_init;
3139099a0e58SBosko Milekic 	args.fini = NULL;
3140099a0e58SBosko Milekic 	args.keg = NULL;
314179c9f942SJeff Roberson 	args.align = UMA_SUPER_ALIGN - 1;
3142099a0e58SBosko Milekic 	args.flags = UMA_ZFLAG_INTERNAL;
3143ab3185d1SJeff Roberson 	zone_ctor(zones, zsize, &args, M_WAITOK);
3144099a0e58SBosko Milekic 
31459b8db4d0SRyan Libby 	/* Now make zones for slab headers */
31469b8db4d0SRyan Libby 	slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE,
31479b8db4d0SRyan Libby 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
31489b8db4d0SRyan Libby 	slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE,
31491e0701e1SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
31508355f576SJeff Roberson 
31518355f576SJeff Roberson 	hashzone = uma_zcreate("UMA Hash",
31528355f576SJeff Roberson 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
31531e0701e1SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
31548355f576SJeff Roberson 
3155a81c400eSJeff Roberson 	bucket_init();
3156d4665eaaSJeff Roberson 	smr_init();
31578355f576SJeff Roberson }
31588355f576SJeff Roberson 
3159a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC
3160a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void);
3161f4bef67cSGleb Smirnoff #endif
3162f4bef67cSGleb Smirnoff 
3163a81c400eSJeff Roberson /*
3164a81c400eSJeff Roberson  * Advertise the availability of normal kva allocations and switch to
3165a81c400eSJeff Roberson  * the default back-end allocator.  Marks the KVA we consumed on startup
3166a81c400eSJeff Roberson  * as used in the map.
3167a81c400eSJeff Roberson  */
31688355f576SJeff Roberson void
316999571dc3SJeff Roberson uma_startup2(void)
31708355f576SJeff Roberson {
3171f4bef67cSGleb Smirnoff 
3172530cc6a2SJeff Roberson 	if (bootstart != bootmem) {
3173a81c400eSJeff Roberson 		vm_map_lock(kernel_map);
3174a81c400eSJeff Roberson 		(void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem,
3175a81c400eSJeff Roberson 		    VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
3176a81c400eSJeff Roberson 		vm_map_unlock(kernel_map);
3177a81c400eSJeff Roberson 	}
3178a81c400eSJeff Roberson 
3179a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC
3180a81c400eSJeff Roberson 	/* Set up radix zone to use noobj_alloc. */
3181a81c400eSJeff Roberson 	vm_radix_reserve_kva();
3182f7d35785SGleb Smirnoff #endif
3183a81c400eSJeff Roberson 
3184a81c400eSJeff Roberson 	booted = BOOT_KVA;
3185a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_kva_available, NULL);
3186f4bef67cSGleb Smirnoff 	bucket_enable();
31878355f576SJeff Roberson }
31888355f576SJeff Roberson 
3189a81c400eSJeff Roberson /*
3190dc2b3205SMark Johnston  * Allocate counters as early as possible so that boot-time allocations are
3191dc2b3205SMark Johnston  * accounted more precisely.
3192dc2b3205SMark Johnston  */
3193dc2b3205SMark Johnston static void
3194dc2b3205SMark Johnston uma_startup_pcpu(void *arg __unused)
3195dc2b3205SMark Johnston {
3196dc2b3205SMark Johnston 
3197dc2b3205SMark Johnston 	zone_foreach_unlocked(zone_alloc_counters, NULL);
3198dc2b3205SMark Johnston 	booted = BOOT_PCPU;
3199dc2b3205SMark Johnston }
3200dc2b3205SMark Johnston SYSINIT(uma_startup_pcpu, SI_SUB_COUNTER, SI_ORDER_ANY, uma_startup_pcpu, NULL);
3201dc2b3205SMark Johnston 
3202dc2b3205SMark Johnston /*
3203a81c400eSJeff Roberson  * Finish our initialization steps.
3204a81c400eSJeff Roberson  */
32058355f576SJeff Roberson static void
3206dc2b3205SMark Johnston uma_startup3(void *arg __unused)
32078355f576SJeff Roberson {
32081431a748SGleb Smirnoff 
3209c5deaf04SGleb Smirnoff #ifdef INVARIANTS
3210c5deaf04SGleb Smirnoff 	TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor);
3211c5deaf04SGleb Smirnoff 	uma_dbg_cnt = counter_u64_alloc(M_WAITOK);
3212c5deaf04SGleb Smirnoff 	uma_skip_cnt = counter_u64_alloc(M_WAITOK);
3213c5deaf04SGleb Smirnoff #endif
3214a81c400eSJeff Roberson 	zone_foreach_unlocked(zone_alloc_sysctl, NULL);
3215c5deaf04SGleb Smirnoff 	booted = BOOT_RUNNING;
3216860bb7a0SMark Johnston 
3217860bb7a0SMark Johnston 	EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL,
3218860bb7a0SMark Johnston 	    EVENTHANDLER_PRI_FIRST);
3219860bb7a0SMark Johnston }
3220dc2b3205SMark Johnston SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
3221860bb7a0SMark Johnston 
3222860bb7a0SMark Johnston static void
322393cd28eaSMark Johnston uma_startup4(void *arg __unused)
322493cd28eaSMark Johnston {
322593cd28eaSMark Johnston 	TIMEOUT_TASK_INIT(taskqueue_thread, &uma_timeout_task, 0, uma_timeout,
322693cd28eaSMark Johnston 	    NULL);
322793cd28eaSMark Johnston 	taskqueue_enqueue_timeout(taskqueue_thread, &uma_timeout_task,
322893cd28eaSMark Johnston 	    UMA_TIMEOUT * hz);
322993cd28eaSMark Johnston }
323093cd28eaSMark Johnston SYSINIT(uma_startup4, SI_SUB_TASKQ, SI_ORDER_ANY, uma_startup4, NULL);
323193cd28eaSMark Johnston 
323293cd28eaSMark Johnston static void
3233860bb7a0SMark Johnston uma_shutdown(void)
3234860bb7a0SMark Johnston {
3235860bb7a0SMark Johnston 
3236860bb7a0SMark Johnston 	booted = BOOT_SHUTDOWN;
32378355f576SJeff Roberson }
32388355f576SJeff Roberson 
3239e20a199fSJeff Roberson static uma_keg_t
3240099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
324185dcf349SGleb Smirnoff 		int align, uint32_t flags)
3242099a0e58SBosko Milekic {
3243099a0e58SBosko Milekic 	struct uma_kctor_args args;
3244099a0e58SBosko Milekic 
3245099a0e58SBosko Milekic 	args.size = size;
3246099a0e58SBosko Milekic 	args.uminit = uminit;
3247099a0e58SBosko Milekic 	args.fini = fini;
32481e319f6dSRobert Watson 	args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
3249099a0e58SBosko Milekic 	args.flags = flags;
3250099a0e58SBosko Milekic 	args.zone = zone;
3251ab3185d1SJeff Roberson 	return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK));
3252099a0e58SBosko Milekic }
3253099a0e58SBosko Milekic 
3254f4bef67cSGleb Smirnoff /* Public functions */
32558355f576SJeff Roberson /* See uma.h */
32561e319f6dSRobert Watson void
32571e319f6dSRobert Watson uma_set_align(int align)
32581e319f6dSRobert Watson {
32591e319f6dSRobert Watson 
32601e319f6dSRobert Watson 	if (align != UMA_ALIGN_CACHE)
32611e319f6dSRobert Watson 		uma_align_cache = align;
32621e319f6dSRobert Watson }
32631e319f6dSRobert Watson 
32641e319f6dSRobert Watson /* See uma.h */
32658355f576SJeff Roberson uma_zone_t
3266bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
326785dcf349SGleb Smirnoff 		uma_init uminit, uma_fini fini, int align, uint32_t flags)
32688355f576SJeff Roberson 
32698355f576SJeff Roberson {
32708355f576SJeff Roberson 	struct uma_zctor_args args;
327195c4bf75SKonstantin Belousov 	uma_zone_t res;
32728355f576SJeff Roberson 
3273a5a35578SJohn Baldwin 	KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
3274a5a35578SJohn Baldwin 	    align, name));
3275a5a35578SJohn Baldwin 
32768355f576SJeff Roberson 	/* This stuff is essential for the zone ctor */
32770095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
32788355f576SJeff Roberson 	args.name = name;
32798355f576SJeff Roberson 	args.size = size;
32808355f576SJeff Roberson 	args.ctor = ctor;
32818355f576SJeff Roberson 	args.dtor = dtor;
32828355f576SJeff Roberson 	args.uminit = uminit;
32838355f576SJeff Roberson 	args.fini = fini;
328410094910SMark Johnston #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN)
3285afc6dc36SJohn-Mark Gurney 	/*
3286ca293436SRyan Libby 	 * Inject procedures which check for memory use after free if we are
3287ca293436SRyan Libby 	 * allowed to scramble the memory while it is not allocated.  This
3288ca293436SRyan Libby 	 * requires that: UMA is actually able to access the memory, no init
3289ca293436SRyan Libby 	 * or fini procedures, no dependency on the initial value of the
3290ca293436SRyan Libby 	 * memory, and no (legitimate) use of the memory after free.  Note,
3291ca293436SRyan Libby 	 * the ctor and dtor do not need to be empty.
3292afc6dc36SJohn-Mark Gurney 	 */
329354c5ae80SRyan Libby 	if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH |
329454c5ae80SRyan Libby 	    UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) {
3295afc6dc36SJohn-Mark Gurney 		args.uminit = trash_init;
3296afc6dc36SJohn-Mark Gurney 		args.fini = trash_fini;
3297afc6dc36SJohn-Mark Gurney 	}
3298afc6dc36SJohn-Mark Gurney #endif
32998355f576SJeff Roberson 	args.align = align;
33008355f576SJeff Roberson 	args.flags = flags;
3301099a0e58SBosko Milekic 	args.keg = NULL;
3302099a0e58SBosko Milekic 
3303aabe13f1SMark Johnston 	sx_xlock(&uma_reclaim_lock);
3304ab3185d1SJeff Roberson 	res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
3305aabe13f1SMark Johnston 	sx_xunlock(&uma_reclaim_lock);
3306a81c400eSJeff Roberson 
330795c4bf75SKonstantin Belousov 	return (res);
3308099a0e58SBosko Milekic }
3309099a0e58SBosko Milekic 
3310099a0e58SBosko Milekic /* See uma.h */
3311099a0e58SBosko Milekic uma_zone_t
33120464f16eSMark Johnston uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor,
3313c8b0a88bSJeff Roberson     uma_init zinit, uma_fini zfini, uma_zone_t primary)
3314099a0e58SBosko Milekic {
3315099a0e58SBosko Milekic 	struct uma_zctor_args args;
3316e20a199fSJeff Roberson 	uma_keg_t keg;
331795c4bf75SKonstantin Belousov 	uma_zone_t res;
3318099a0e58SBosko Milekic 
3319c8b0a88bSJeff Roberson 	keg = primary->uz_keg;
33200095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
3321099a0e58SBosko Milekic 	args.name = name;
3322e20a199fSJeff Roberson 	args.size = keg->uk_size;
3323099a0e58SBosko Milekic 	args.ctor = ctor;
3324099a0e58SBosko Milekic 	args.dtor = dtor;
3325099a0e58SBosko Milekic 	args.uminit = zinit;
3326099a0e58SBosko Milekic 	args.fini = zfini;
3327e20a199fSJeff Roberson 	args.align = keg->uk_align;
3328e20a199fSJeff Roberson 	args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
3329e20a199fSJeff Roberson 	args.keg = keg;
33308355f576SJeff Roberson 
3331aabe13f1SMark Johnston 	sx_xlock(&uma_reclaim_lock);
3332ab3185d1SJeff Roberson 	res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
3333aabe13f1SMark Johnston 	sx_xunlock(&uma_reclaim_lock);
3334a81c400eSJeff Roberson 
333595c4bf75SKonstantin Belousov 	return (res);
33368355f576SJeff Roberson }
33378355f576SJeff Roberson 
33380095a784SJeff Roberson /* See uma.h */
33390095a784SJeff Roberson uma_zone_t
33400464f16eSMark Johnston uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor,
33410464f16eSMark Johnston     uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease,
33420464f16eSMark Johnston     void *arg, int flags)
33430095a784SJeff Roberson {
33440095a784SJeff Roberson 	struct uma_zctor_args args;
33450095a784SJeff Roberson 
33460095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
33470095a784SJeff Roberson 	args.name = name;
3348af526374SJeff Roberson 	args.size = size;
33490095a784SJeff Roberson 	args.ctor = ctor;
33500095a784SJeff Roberson 	args.dtor = dtor;
33510095a784SJeff Roberson 	args.uminit = zinit;
33520095a784SJeff Roberson 	args.fini = zfini;
33530095a784SJeff Roberson 	args.import = zimport;
33540095a784SJeff Roberson 	args.release = zrelease;
33550095a784SJeff Roberson 	args.arg = arg;
33560095a784SJeff Roberson 	args.align = 0;
3357bb15d1c7SGleb Smirnoff 	args.flags = flags | UMA_ZFLAG_CACHE;
33580095a784SJeff Roberson 
3359ab3185d1SJeff Roberson 	return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK));
33600095a784SJeff Roberson }
33610095a784SJeff Roberson 
33628355f576SJeff Roberson /* See uma.h */
33639c2cd7e5SJeff Roberson void
33649c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone)
33659c2cd7e5SJeff Roberson {
3366f4ff923bSRobert Watson 
3367860bb7a0SMark Johnston 	/*
3368860bb7a0SMark Johnston 	 * Large slabs are expensive to reclaim, so don't bother doing
3369860bb7a0SMark Johnston 	 * unnecessary work if we're shutting down.
3370860bb7a0SMark Johnston 	 */
3371860bb7a0SMark Johnston 	if (booted == BOOT_SHUTDOWN &&
3372860bb7a0SMark Johnston 	    zone->uz_fini == NULL && zone->uz_release == zone_release)
3373860bb7a0SMark Johnston 		return;
3374aabe13f1SMark Johnston 	sx_xlock(&uma_reclaim_lock);
33750095a784SJeff Roberson 	zone_free_item(zones, zone, NULL, SKIP_NONE);
3376aabe13f1SMark Johnston 	sx_xunlock(&uma_reclaim_lock);
33779c2cd7e5SJeff Roberson }
33789c2cd7e5SJeff Roberson 
33798d6fbbb8SJeff Roberson void
33808d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone)
33818d6fbbb8SJeff Roberson {
33828d6fbbb8SJeff Roberson 
338370260874SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
338470260874SJeff Roberson 		uma_zfree_smr(zone, uma_zalloc_smr(zone, M_WAITOK));
338570260874SJeff Roberson 	else if ((zone->uz_flags & UMA_ZONE_PCPU) != 0)
338670260874SJeff Roberson 		uma_zfree_pcpu(zone, uma_zalloc_pcpu(zone, M_WAITOK));
338770260874SJeff Roberson 	else
338870260874SJeff Roberson 		uma_zfree(zone, uma_zalloc(zone, M_WAITOK));
33898d6fbbb8SJeff Roberson }
33908d6fbbb8SJeff Roberson 
33914e180881SMateusz Guzik void *
33924e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags)
33934e180881SMateusz Guzik {
33943acb6572SMateusz Guzik 	void *item, *pcpu_item;
3395b4799947SRuslan Bukin #ifdef SMP
33964e180881SMateusz Guzik 	int i;
33974e180881SMateusz Guzik 
33984e180881SMateusz Guzik 	MPASS(zone->uz_flags & UMA_ZONE_PCPU);
3399b4799947SRuslan Bukin #endif
34004e180881SMateusz Guzik 	item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO);
34013acb6572SMateusz Guzik 	if (item == NULL)
34023acb6572SMateusz Guzik 		return (NULL);
34033acb6572SMateusz Guzik 	pcpu_item = zpcpu_base_to_offset(item);
34043acb6572SMateusz Guzik 	if (flags & M_ZERO) {
3405b4799947SRuslan Bukin #ifdef SMP
3406013072f0SMark Johnston 		for (i = 0; i <= mp_maxid; i++)
34073acb6572SMateusz Guzik 			bzero(zpcpu_get_cpu(pcpu_item, i), zone->uz_size);
3408b4799947SRuslan Bukin #else
3409b4799947SRuslan Bukin 		bzero(item, zone->uz_size);
3410b4799947SRuslan Bukin #endif
34114e180881SMateusz Guzik 	}
34123acb6572SMateusz Guzik 	return (pcpu_item);
34134e180881SMateusz Guzik }
34144e180881SMateusz Guzik 
34154e180881SMateusz Guzik /*
34164e180881SMateusz Guzik  * A stub while both regular and pcpu cases are identical.
34174e180881SMateusz Guzik  */
34184e180881SMateusz Guzik void
34193acb6572SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *pcpu_item, void *udata)
34204e180881SMateusz Guzik {
34213acb6572SMateusz Guzik 	void *item;
34224e180881SMateusz Guzik 
3423c5b7751fSIan Lepore #ifdef SMP
34244e180881SMateusz Guzik 	MPASS(zone->uz_flags & UMA_ZONE_PCPU);
3425c5b7751fSIan Lepore #endif
3426b8f7267dSKristof Provost 
3427b8f7267dSKristof Provost         /* uma_zfree_pcu_*(..., NULL) does nothing, to match free(9). */
3428b8f7267dSKristof Provost         if (pcpu_item == NULL)
3429b8f7267dSKristof Provost                 return;
3430b8f7267dSKristof Provost 
34313acb6572SMateusz Guzik 	item = zpcpu_offset_to_base(pcpu_item);
34324e180881SMateusz Guzik 	uma_zfree_arg(zone, item, udata);
34334e180881SMateusz Guzik }
34344e180881SMateusz Guzik 
3435d4665eaaSJeff Roberson static inline void *
3436d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags,
3437d4665eaaSJeff Roberson     void *item)
3438beb8beefSJeff Roberson {
3439beb8beefSJeff Roberson #ifdef INVARIANTS
3440ca293436SRyan Libby 	bool skipdbg;
344109c8cb71SMark Johnston #endif
3442beb8beefSJeff Roberson 
344309c8cb71SMark Johnston 	kasan_mark_item_valid(zone, item);
344410094910SMark Johnston 	kmsan_mark_item_uninitialized(zone, item);
344509c8cb71SMark Johnston 
344609c8cb71SMark Johnston #ifdef INVARIANTS
3447beb8beefSJeff Roberson 	skipdbg = uma_dbg_zskip(zone, item);
344809c8cb71SMark Johnston 	if (!skipdbg && (uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3449ca293436SRyan Libby 	    zone->uz_ctor != trash_ctor)
3450cc7ce83aSJeff Roberson 		trash_ctor(item, size, udata, flags);
3451beb8beefSJeff Roberson #endif
345209c8cb71SMark Johnston 
3453d4665eaaSJeff Roberson 	/* Check flags before loading ctor pointer. */
3454d4665eaaSJeff Roberson 	if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) &&
3455d4665eaaSJeff Roberson 	    __predict_false(zone->uz_ctor != NULL) &&
3456cc7ce83aSJeff Roberson 	    zone->uz_ctor(item, size, udata, flags) != 0) {
3457beb8beefSJeff Roberson 		counter_u64_add(zone->uz_fails, 1);
3458beb8beefSJeff Roberson 		zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT);
3459beb8beefSJeff Roberson 		return (NULL);
3460beb8beefSJeff Roberson 	}
3461beb8beefSJeff Roberson #ifdef INVARIANTS
3462beb8beefSJeff Roberson 	if (!skipdbg)
3463beb8beefSJeff Roberson 		uma_dbg_alloc(zone, NULL, item);
3464beb8beefSJeff Roberson #endif
34656d88d784SJeff Roberson 	if (__predict_false(flags & M_ZERO))
34666d88d784SJeff Roberson 		return (memset(item, 0, size));
3467beb8beefSJeff Roberson 
3468beb8beefSJeff Roberson 	return (item);
3469beb8beefSJeff Roberson }
3470beb8beefSJeff Roberson 
3471ca293436SRyan Libby static inline void
3472cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata,
3473cc7ce83aSJeff Roberson     enum zfreeskip skip)
3474ca293436SRyan Libby {
3475ca293436SRyan Libby #ifdef INVARIANTS
3476ca293436SRyan Libby 	bool skipdbg;
3477ca293436SRyan Libby 
3478ca293436SRyan Libby 	skipdbg = uma_dbg_zskip(zone, item);
3479ca293436SRyan Libby 	if (skip == SKIP_NONE && !skipdbg) {
3480ca293436SRyan Libby 		if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0)
3481ca293436SRyan Libby 			uma_dbg_free(zone, udata, item);
3482ca293436SRyan Libby 		else
3483ca293436SRyan Libby 			uma_dbg_free(zone, NULL, item);
3484ca293436SRyan Libby 	}
3485ca293436SRyan Libby #endif
3486cc7ce83aSJeff Roberson 	if (__predict_true(skip < SKIP_DTOR)) {
3487ca293436SRyan Libby 		if (zone->uz_dtor != NULL)
3488cc7ce83aSJeff Roberson 			zone->uz_dtor(item, size, udata);
3489ca293436SRyan Libby #ifdef INVARIANTS
3490ca293436SRyan Libby 		if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3491ca293436SRyan Libby 		    zone->uz_dtor != trash_dtor)
3492cc7ce83aSJeff Roberson 			trash_dtor(item, size, udata);
3493ca293436SRyan Libby #endif
3494ca293436SRyan Libby 	}
349509c8cb71SMark Johnston 	kasan_mark_item_invalid(zone, item);
3496ca293436SRyan Libby }
3497ca293436SRyan Libby 
34981c58c09fSMateusz Guzik #ifdef NUMA
349981302f1dSMark Johnston static int
350081302f1dSMark Johnston item_domain(void *item)
350181302f1dSMark Johnston {
350281302f1dSMark Johnston 	int domain;
350381302f1dSMark Johnston 
3504431fb8abSMark Johnston 	domain = vm_phys_domain(vtophys(item));
350581302f1dSMark Johnston 	KASSERT(domain >= 0 && domain < vm_ndomains,
350681302f1dSMark Johnston 	    ("%s: unknown domain for item %p", __func__, item));
350781302f1dSMark Johnston 	return (domain);
350881302f1dSMark Johnston }
35091c58c09fSMateusz Guzik #endif
351081302f1dSMark Johnston 
3511d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS)
3512a8cbb835SEric van Gyzen #if defined(INVARIANTS) && (defined(DDB) || defined(STACK))
3513a8cbb835SEric van Gyzen #include <sys/stack.h>
3514a8cbb835SEric van Gyzen #endif
3515d4665eaaSJeff Roberson #define	UMA_ZALLOC_DEBUG
3516d4665eaaSJeff Roberson static int
3517d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags)
3518d4665eaaSJeff Roberson {
3519d4665eaaSJeff Roberson 	int error;
3520d4665eaaSJeff Roberson 
3521d4665eaaSJeff Roberson 	error = 0;
3522d4665eaaSJeff Roberson #ifdef WITNESS
3523d4665eaaSJeff Roberson 	if (flags & M_WAITOK) {
3524d4665eaaSJeff Roberson 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
3525d4665eaaSJeff Roberson 		    "uma_zalloc_debug: zone \"%s\"", zone->uz_name);
3526d4665eaaSJeff Roberson 	}
3527d4665eaaSJeff Roberson #endif
3528d4665eaaSJeff Roberson 
3529d4665eaaSJeff Roberson #ifdef INVARIANTS
3530d4665eaaSJeff Roberson 	KASSERT((flags & M_EXEC) == 0,
3531d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: called with M_EXEC"));
3532d4665eaaSJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3533d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: called within spinlock or critical section"));
3534d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0,
3535d4665eaaSJeff Roberson 	    ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO"));
3536a8cbb835SEric van Gyzen 
3537a8cbb835SEric van Gyzen 	_Static_assert(M_NOWAIT != 0 && M_WAITOK != 0,
3538a8cbb835SEric van Gyzen 	    "M_NOWAIT and M_WAITOK must be non-zero for this assertion:");
3539a8cbb835SEric van Gyzen #if 0
3540a8cbb835SEric van Gyzen 	/*
3541a8cbb835SEric van Gyzen 	 * Give the #elif clause time to find problems, then remove it
3542a8cbb835SEric van Gyzen 	 * and enable this.  (Remove <sys/stack.h> above, too.)
3543a8cbb835SEric van Gyzen 	 */
3544a8cbb835SEric van Gyzen 	KASSERT((flags & (M_NOWAIT|M_WAITOK)) == M_NOWAIT ||
3545a8cbb835SEric van Gyzen 	    (flags & (M_NOWAIT|M_WAITOK)) == M_WAITOK,
3546a8cbb835SEric van Gyzen 	    ("uma_zalloc_debug: must pass one of M_NOWAIT or M_WAITOK"));
3547a8cbb835SEric van Gyzen #elif defined(DDB) || defined(STACK)
3548a8cbb835SEric van Gyzen 	if (__predict_false((flags & (M_NOWAIT|M_WAITOK)) != M_NOWAIT &&
3549a8cbb835SEric van Gyzen 	    (flags & (M_NOWAIT|M_WAITOK)) != M_WAITOK)) {
3550a8cbb835SEric van Gyzen 		static int stack_count;
3551a8cbb835SEric van Gyzen 		struct stack st;
3552a8cbb835SEric van Gyzen 
3553a8cbb835SEric van Gyzen 		if (stack_count < 10) {
3554a8cbb835SEric van Gyzen 			++stack_count;
3555a8cbb835SEric van Gyzen 			printf("uma_zalloc* called with bad WAIT flags:\n");
3556a8cbb835SEric van Gyzen 			stack_save(&st);
3557a8cbb835SEric van Gyzen 			stack_print(&st);
3558a8cbb835SEric van Gyzen 		}
3559a8cbb835SEric van Gyzen 	}
3560a8cbb835SEric van Gyzen #endif
3561d4665eaaSJeff Roberson #endif
3562d4665eaaSJeff Roberson 
3563d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD
3564*2dba2288SMark Johnston 	if ((zone->uz_flags & (UMA_ZONE_SMR | UMA_ZFLAG_CACHE)) == 0 &&
3565*2dba2288SMark Johnston 	    memguard_cmp_zone(zone)) {
3566d4665eaaSJeff Roberson 		void *item;
3567d4665eaaSJeff Roberson 		item = memguard_alloc(zone->uz_size, flags);
3568d4665eaaSJeff Roberson 		if (item != NULL) {
3569d4665eaaSJeff Roberson 			error = EJUSTRETURN;
3570d4665eaaSJeff Roberson 			if (zone->uz_init != NULL &&
3571d4665eaaSJeff Roberson 			    zone->uz_init(item, zone->uz_size, flags) != 0) {
3572d4665eaaSJeff Roberson 				*itemp = NULL;
3573d4665eaaSJeff Roberson 				return (error);
3574d4665eaaSJeff Roberson 			}
3575d4665eaaSJeff Roberson 			if (zone->uz_ctor != NULL &&
3576d4665eaaSJeff Roberson 			    zone->uz_ctor(item, zone->uz_size, udata,
3577d4665eaaSJeff Roberson 			    flags) != 0) {
3578d4665eaaSJeff Roberson 				counter_u64_add(zone->uz_fails, 1);
3579389a3fa6SMark Johnston 				if (zone->uz_fini != NULL)
3580d4665eaaSJeff Roberson 					zone->uz_fini(item, zone->uz_size);
3581d4665eaaSJeff Roberson 				*itemp = NULL;
3582d4665eaaSJeff Roberson 				return (error);
3583d4665eaaSJeff Roberson 			}
3584d4665eaaSJeff Roberson 			*itemp = item;
3585d4665eaaSJeff Roberson 			return (error);
3586d4665eaaSJeff Roberson 		}
3587d4665eaaSJeff Roberson 		/* This is unfortunate but should not be fatal. */
3588d4665eaaSJeff Roberson 	}
3589d4665eaaSJeff Roberson #endif
3590d4665eaaSJeff Roberson 	return (error);
3591d4665eaaSJeff Roberson }
3592d4665eaaSJeff Roberson 
3593d4665eaaSJeff Roberson static int
3594d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata)
3595d4665eaaSJeff Roberson {
3596d4665eaaSJeff Roberson 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3597d4665eaaSJeff Roberson 	    ("uma_zfree_debug: called with spinlock or critical section held"));
3598d4665eaaSJeff Roberson 
3599d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD
3600*2dba2288SMark Johnston 	if ((zone->uz_flags & (UMA_ZONE_SMR | UMA_ZFLAG_CACHE)) == 0 &&
3601*2dba2288SMark Johnston 	    is_memguard_addr(item)) {
3602d4665eaaSJeff Roberson 		if (zone->uz_dtor != NULL)
3603d4665eaaSJeff Roberson 			zone->uz_dtor(item, zone->uz_size, udata);
3604d4665eaaSJeff Roberson 		if (zone->uz_fini != NULL)
3605d4665eaaSJeff Roberson 			zone->uz_fini(item, zone->uz_size);
3606d4665eaaSJeff Roberson 		memguard_free(item);
3607d4665eaaSJeff Roberson 		return (EJUSTRETURN);
3608d4665eaaSJeff Roberson 	}
3609d4665eaaSJeff Roberson #endif
3610d4665eaaSJeff Roberson 	return (0);
3611d4665eaaSJeff Roberson }
3612d4665eaaSJeff Roberson #endif
3613d4665eaaSJeff Roberson 
36146d88d784SJeff Roberson static inline void *
36156d88d784SJeff Roberson cache_alloc_item(uma_zone_t zone, uma_cache_t cache, uma_cache_bucket_t bucket,
36166d88d784SJeff Roberson     void *udata, int flags)
3617d4665eaaSJeff Roberson {
36186d88d784SJeff Roberson 	void *item;
36196d88d784SJeff Roberson 	int size, uz_flags;
36206d88d784SJeff Roberson 
36216d88d784SJeff Roberson 	item = cache_bucket_pop(cache, bucket);
36226d88d784SJeff Roberson 	size = cache_uz_size(cache);
36236d88d784SJeff Roberson 	uz_flags = cache_uz_flags(cache);
36246d88d784SJeff Roberson 	critical_exit();
36256d88d784SJeff Roberson 	return (item_ctor(zone, uz_flags, size, udata, flags, item));
36266d88d784SJeff Roberson }
36276d88d784SJeff Roberson 
36286d88d784SJeff Roberson static __noinline void *
36296d88d784SJeff Roberson cache_alloc_retry(uma_zone_t zone, uma_cache_t cache, void *udata, int flags)
36306d88d784SJeff Roberson {
36316d88d784SJeff Roberson 	uma_cache_bucket_t bucket;
3632d4665eaaSJeff Roberson 	int domain;
3633d4665eaaSJeff Roberson 
36346d88d784SJeff Roberson 	while (cache_alloc(zone, cache, udata, flags)) {
36356d88d784SJeff Roberson 		cache = &zone->uz_cpu[curcpu];
36366d88d784SJeff Roberson 		bucket = &cache->uc_allocbucket;
36376d88d784SJeff Roberson 		if (__predict_false(bucket->ucb_cnt == 0))
36386d88d784SJeff Roberson 			continue;
36396d88d784SJeff Roberson 		return (cache_alloc_item(zone, cache, bucket, udata, flags));
36406d88d784SJeff Roberson 	}
36416d88d784SJeff Roberson 	critical_exit();
36426d88d784SJeff Roberson 
3643d4665eaaSJeff Roberson 	/*
3644d4665eaaSJeff Roberson 	 * We can not get a bucket so try to return a single item.
3645d4665eaaSJeff Roberson 	 */
3646d4665eaaSJeff Roberson 	if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH)
3647d4665eaaSJeff Roberson 		domain = PCPU_GET(domain);
3648d4665eaaSJeff Roberson 	else
3649d4665eaaSJeff Roberson 		domain = UMA_ANYDOMAIN;
3650d4665eaaSJeff Roberson 	return (zone_alloc_item(zone, udata, domain, flags));
3651d4665eaaSJeff Roberson }
3652d4665eaaSJeff Roberson 
3653d4665eaaSJeff Roberson /* See uma.h */
3654d4665eaaSJeff Roberson void *
3655d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags)
3656d4665eaaSJeff Roberson {
3657d4665eaaSJeff Roberson 	uma_cache_bucket_t bucket;
3658d4665eaaSJeff Roberson 	uma_cache_t cache;
3659d4665eaaSJeff Roberson 
3660841e0a87SGleb Smirnoff 	CTR3(KTR_UMA, "uma_zalloc_smr zone %s(%p) flags %d", zone->uz_name,
3661841e0a87SGleb Smirnoff 	    zone, flags);
3662841e0a87SGleb Smirnoff 
3663d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
36646d88d784SJeff Roberson 	void *item;
36656d88d784SJeff Roberson 
3666d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
3667952c8964SMark Johnston 	    ("uma_zalloc_arg: called with non-SMR zone."));
3668d4665eaaSJeff Roberson 	if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN)
3669d4665eaaSJeff Roberson 		return (item);
3670d4665eaaSJeff Roberson #endif
3671d4665eaaSJeff Roberson 
3672d4665eaaSJeff Roberson 	critical_enter();
3673d4665eaaSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3674d4665eaaSJeff Roberson 	bucket = &cache->uc_allocbucket;
36756d88d784SJeff Roberson 	if (__predict_false(bucket->ucb_cnt == 0))
36766d88d784SJeff Roberson 		return (cache_alloc_retry(zone, cache, NULL, flags));
36776d88d784SJeff Roberson 	return (cache_alloc_item(zone, cache, bucket, NULL, flags));
3678d4665eaaSJeff Roberson }
3679d4665eaaSJeff Roberson 
36809c2cd7e5SJeff Roberson /* See uma.h */
36818355f576SJeff Roberson void *
36822cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
36838355f576SJeff Roberson {
3684376b1ba3SJeff Roberson 	uma_cache_bucket_t bucket;
3685ab3185d1SJeff Roberson 	uma_cache_t cache;
36868355f576SJeff Roberson 
3687e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
368819fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
368910cb2424SMark Murray 
36908355f576SJeff Roberson 	/* This is the fast path allocation */
3691e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name,
3692e63a1c2fSRyan Libby 	    zone, flags);
3693a553d4b8SJeff Roberson 
3694d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
36956d88d784SJeff Roberson 	void *item;
36966d88d784SJeff Roberson 
3697d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
3698952c8964SMark Johnston 	    ("uma_zalloc_arg: called with SMR zone."));
3699d4665eaaSJeff Roberson 	if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN)
37008d689e04SGleb Smirnoff 		return (item);
37018d689e04SGleb Smirnoff #endif
3702d4665eaaSJeff Roberson 
37035d1ae027SRobert Watson 	/*
37045d1ae027SRobert Watson 	 * If possible, allocate from the per-CPU cache.  There are two
37055d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
37065d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
37075d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
37085d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
37095d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
37105d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to allocate from
37115d1ae027SRobert Watson 	 * the current cache; when we re-acquire the critical section, we
37125d1ae027SRobert Watson 	 * must detect and handle migration if it has occurred.
37135d1ae027SRobert Watson 	 */
37145d1ae027SRobert Watson 	critical_enter();
3715cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3716376b1ba3SJeff Roberson 	bucket = &cache->uc_allocbucket;
37176d88d784SJeff Roberson 	if (__predict_false(bucket->ucb_cnt == 0))
37186d88d784SJeff Roberson 		return (cache_alloc_retry(zone, cache, udata, flags));
37196d88d784SJeff Roberson 	return (cache_alloc_item(zone, cache, bucket, udata, flags));
3720fc03d22bSJeff Roberson }
3721fc03d22bSJeff Roberson 
37228355f576SJeff Roberson /*
3723beb8beefSJeff Roberson  * Replenish an alloc bucket and possibly restore an old one.  Called in
3724beb8beefSJeff Roberson  * a critical section.  Returns in a critical section.
3725beb8beefSJeff Roberson  *
37264bd61e19SJeff Roberson  * A false return value indicates an allocation failure.
37274bd61e19SJeff Roberson  * A true return value indicates success and the caller should retry.
3728beb8beefSJeff Roberson  */
3729beb8beefSJeff Roberson static __noinline bool
3730beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags)
3731beb8beefSJeff Roberson {
3732beb8beefSJeff Roberson 	uma_bucket_t bucket;
37338c277118SMark Johnston 	int curdomain, domain;
3734c6fd3e23SJeff Roberson 	bool new;
3735beb8beefSJeff Roberson 
3736beb8beefSJeff Roberson 	CRITICAL_ASSERT(curthread);
3737beb8beefSJeff Roberson 
3738beb8beefSJeff Roberson 	/*
3739beb8beefSJeff Roberson 	 * If we have run out of items in our alloc bucket see
3740beb8beefSJeff Roberson 	 * if we can switch with the free bucket.
3741d4665eaaSJeff Roberson 	 *
3742d4665eaaSJeff Roberson 	 * SMR Zones can't re-use the free bucket until the sequence has
3743d4665eaaSJeff Roberson 	 * expired.
37448355f576SJeff Roberson 	 */
3745c6fd3e23SJeff Roberson 	if ((cache_uz_flags(cache) & UMA_ZONE_SMR) == 0 &&
3746d4665eaaSJeff Roberson 	    cache->uc_freebucket.ucb_cnt != 0) {
3747d4665eaaSJeff Roberson 		cache_bucket_swap(&cache->uc_freebucket,
3748d4665eaaSJeff Roberson 		    &cache->uc_allocbucket);
3749beb8beefSJeff Roberson 		return (true);
37508355f576SJeff Roberson 	}
3751fc03d22bSJeff Roberson 
3752fc03d22bSJeff Roberson 	/*
3753fc03d22bSJeff Roberson 	 * Discard any empty allocation bucket while we hold no locks.
3754fc03d22bSJeff Roberson 	 */
3755376b1ba3SJeff Roberson 	bucket = cache_bucket_unload_alloc(cache);
3756fc03d22bSJeff Roberson 	critical_exit();
3757c6fd3e23SJeff Roberson 
3758c6fd3e23SJeff Roberson 	if (bucket != NULL) {
3759c6fd3e23SJeff Roberson 		KASSERT(bucket->ub_cnt == 0,
3760c6fd3e23SJeff Roberson 		    ("cache_alloc: Entered with non-empty alloc bucket."));
37616fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
3762c6fd3e23SJeff Roberson 	}
3763fc03d22bSJeff Roberson 
37645d1ae027SRobert Watson 	/*
37655d1ae027SRobert Watson 	 * Attempt to retrieve the item from the per-CPU cache has failed, so
3766c6fd3e23SJeff Roberson 	 * we must go back to the zone.  This requires the zdom lock, so we
37675d1ae027SRobert Watson 	 * must drop the critical section, then re-acquire it when we go back
37685d1ae027SRobert Watson 	 * to the cache.  Since the critical section is released, we may be
37695d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
37705d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
37715d1ae027SRobert Watson 	 * the critical section.
37725d1ae027SRobert Watson 	 */
3773c1685086SJeff Roberson 	domain = PCPU_GET(domain);
37748c277118SMark Johnston 	if ((cache_uz_flags(cache) & UMA_ZONE_ROUNDROBIN) != 0 ||
37758c277118SMark Johnston 	    VM_DOMAIN_EMPTY(domain))
3776c6fd3e23SJeff Roberson 		domain = zone_domain_highest(zone, domain);
3777c6fd3e23SJeff Roberson 	bucket = cache_fetch_bucket(zone, cache, domain);
3778af32cefdSMark Johnston 	if (bucket == NULL && zone->uz_bucket_size != 0 && !bucketdisable) {
3779beb8beefSJeff Roberson 		bucket = zone_alloc_bucket(zone, udata, domain, flags);
3780c6fd3e23SJeff Roberson 		new = true;
3781af32cefdSMark Johnston 	} else {
3782c6fd3e23SJeff Roberson 		new = false;
3783af32cefdSMark Johnston 	}
3784c6fd3e23SJeff Roberson 
37851431a748SGleb Smirnoff 	CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
37861431a748SGleb Smirnoff 	    zone->uz_name, zone, bucket);
37874bd61e19SJeff Roberson 	if (bucket == NULL) {
3788fc03d22bSJeff Roberson 		critical_enter();
3789beb8beefSJeff Roberson 		return (false);
37904bd61e19SJeff Roberson 	}
37910f9b7bf3SMark Johnston 
3792fc03d22bSJeff Roberson 	/*
3793fc03d22bSJeff Roberson 	 * See if we lost the race or were migrated.  Cache the
3794fc03d22bSJeff Roberson 	 * initialized bucket to make this less likely or claim
3795fc03d22bSJeff Roberson 	 * the memory directly.
3796fc03d22bSJeff Roberson 	 */
37974bd61e19SJeff Roberson 	critical_enter();
3798cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
3799376b1ba3SJeff Roberson 	if (cache->uc_allocbucket.ucb_bucket == NULL &&
3800c6fd3e23SJeff Roberson 	    ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) == 0 ||
38018c277118SMark Johnston 	    (curdomain = PCPU_GET(domain)) == domain ||
38028c277118SMark Johnston 	    VM_DOMAIN_EMPTY(curdomain))) {
3803c6fd3e23SJeff Roberson 		if (new)
3804c6fd3e23SJeff Roberson 			atomic_add_long(&ZDOM_GET(zone, domain)->uzd_imax,
3805c6fd3e23SJeff Roberson 			    bucket->ub_cnt);
3806376b1ba3SJeff Roberson 		cache_bucket_load_alloc(cache, bucket);
3807beb8beefSJeff Roberson 		return (true);
3808c6fd3e23SJeff Roberson 	}
3809c6fd3e23SJeff Roberson 
3810c6fd3e23SJeff Roberson 	/*
3811c6fd3e23SJeff Roberson 	 * We lost the race, release this bucket and start over.
3812c6fd3e23SJeff Roberson 	 */
3813c6fd3e23SJeff Roberson 	critical_exit();
38142760658bSAlexander Motin 	zone_put_bucket(zone, domain, bucket, udata, !new);
3815c6fd3e23SJeff Roberson 	critical_enter();
3816c6fd3e23SJeff Roberson 
3817beb8beefSJeff Roberson 	return (true);
3818bbee39c6SJeff Roberson }
3819bbee39c6SJeff Roberson 
3820ab3185d1SJeff Roberson void *
3821ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
3822bbee39c6SJeff Roberson {
382306d8bdcbSMark Johnston #ifdef NUMA
382406d8bdcbSMark Johnston 	uma_bucket_t bucket;
382506d8bdcbSMark Johnston 	uma_zone_domain_t zdom;
382606d8bdcbSMark Johnston 	void *item;
382706d8bdcbSMark Johnston #endif
3828ab3185d1SJeff Roberson 
3829ab3185d1SJeff Roberson 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
383019fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3831ab3185d1SJeff Roberson 
3832ab3185d1SJeff Roberson 	/* This is the fast path allocation */
3833e63a1c2fSRyan Libby 	CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d",
3834e63a1c2fSRyan Libby 	    zone->uz_name, zone, domain, flags);
3835ab3185d1SJeff Roberson 
383606d8bdcbSMark Johnston 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
383706d8bdcbSMark Johnston 	    ("uma_zalloc_domain: called with SMR zone."));
383806d8bdcbSMark Johnston #ifdef NUMA
383906d8bdcbSMark Johnston 	KASSERT((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0,
384006d8bdcbSMark Johnston 	    ("uma_zalloc_domain: called with non-FIRSTTOUCH zone."));
3841ab3185d1SJeff Roberson 
384206d8bdcbSMark Johnston 	if (vm_ndomains == 1)
384306d8bdcbSMark Johnston 		return (uma_zalloc_arg(zone, udata, flags));
384406d8bdcbSMark Johnston 
3845490b09f2SEric van Gyzen #ifdef UMA_ZALLOC_DEBUG
3846490b09f2SEric van Gyzen 	if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN)
3847490b09f2SEric van Gyzen 		return (item);
3848490b09f2SEric van Gyzen #endif
3849490b09f2SEric van Gyzen 
385006d8bdcbSMark Johnston 	/*
385106d8bdcbSMark Johnston 	 * Try to allocate from the bucket cache before falling back to the keg.
385206d8bdcbSMark Johnston 	 * We could try harder and attempt to allocate from per-CPU caches or
385306d8bdcbSMark Johnston 	 * the per-domain cross-domain buckets, but the complexity is probably
385406d8bdcbSMark Johnston 	 * not worth it.  It is more important that frees of previous
385506d8bdcbSMark Johnston 	 * cross-domain allocations do not blow up the cache.
385606d8bdcbSMark Johnston 	 */
385706d8bdcbSMark Johnston 	zdom = zone_domain_lock(zone, domain);
385806d8bdcbSMark Johnston 	if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) {
385906d8bdcbSMark Johnston 		item = bucket->ub_bucket[bucket->ub_cnt - 1];
386006d8bdcbSMark Johnston #ifdef INVARIANTS
386106d8bdcbSMark Johnston 		bucket->ub_bucket[bucket->ub_cnt - 1] = NULL;
386206d8bdcbSMark Johnston #endif
386306d8bdcbSMark Johnston 		bucket->ub_cnt--;
386406d8bdcbSMark Johnston 		zone_put_bucket(zone, domain, bucket, udata, true);
386506d8bdcbSMark Johnston 		item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata,
386606d8bdcbSMark Johnston 		    flags, item);
386706d8bdcbSMark Johnston 		if (item != NULL) {
386806d8bdcbSMark Johnston 			KASSERT(item_domain(item) == domain,
386906d8bdcbSMark Johnston 			    ("%s: bucket cache item %p from wrong domain",
387006d8bdcbSMark Johnston 			    __func__, item));
387106d8bdcbSMark Johnston 			counter_u64_add(zone->uz_allocs, 1);
387206d8bdcbSMark Johnston 		}
387306d8bdcbSMark Johnston 		return (item);
387406d8bdcbSMark Johnston 	}
387506d8bdcbSMark Johnston 	ZDOM_UNLOCK(zdom);
3876ab3185d1SJeff Roberson 	return (zone_alloc_item(zone, udata, domain, flags));
387706d8bdcbSMark Johnston #else
387806d8bdcbSMark Johnston 	return (uma_zalloc_arg(zone, udata, flags));
387906d8bdcbSMark Johnston #endif
3880ab3185d1SJeff Roberson }
3881ab3185d1SJeff Roberson 
3882ab3185d1SJeff Roberson /*
3883ab3185d1SJeff Roberson  * Find a slab with some space.  Prefer slabs that are partially used over those
3884ab3185d1SJeff Roberson  * that are totally full.  This helps to reduce fragmentation.
3885ab3185d1SJeff Roberson  *
3886ab3185d1SJeff Roberson  * If 'rr' is 1, search all domains starting from 'domain'.  Otherwise check
3887ab3185d1SJeff Roberson  * only 'domain'.
3888ab3185d1SJeff Roberson  */
3889ab3185d1SJeff Roberson static uma_slab_t
3890194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr)
3891ab3185d1SJeff Roberson {
3892ab3185d1SJeff Roberson 	uma_domain_t dom;
3893bbee39c6SJeff Roberson 	uma_slab_t slab;
3894ab3185d1SJeff Roberson 	int start;
3895ab3185d1SJeff Roberson 
3896ab3185d1SJeff Roberson 	KASSERT(domain >= 0 && domain < vm_ndomains,
3897ab3185d1SJeff Roberson 	    ("keg_first_slab: domain %d out of range", domain));
38988b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, domain);
3899ab3185d1SJeff Roberson 
3900ab3185d1SJeff Roberson 	slab = NULL;
3901ab3185d1SJeff Roberson 	start = domain;
3902ab3185d1SJeff Roberson 	do {
3903ab3185d1SJeff Roberson 		dom = &keg->uk_domain[domain];
39044ab3aee8SMark Johnston 		if ((slab = LIST_FIRST(&dom->ud_part_slab)) != NULL)
39054ab3aee8SMark Johnston 			return (slab);
39064ab3aee8SMark Johnston 		if ((slab = LIST_FIRST(&dom->ud_free_slab)) != NULL) {
3907ab3185d1SJeff Roberson 			LIST_REMOVE(slab, us_link);
39084ab3aee8SMark Johnston 			dom->ud_free_slabs--;
3909ab3185d1SJeff Roberson 			LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
3910ab3185d1SJeff Roberson 			return (slab);
3911ab3185d1SJeff Roberson 		}
3912ab3185d1SJeff Roberson 		if (rr)
3913ab3185d1SJeff Roberson 			domain = (domain + 1) % vm_ndomains;
3914ab3185d1SJeff Roberson 	} while (domain != start);
3915ab3185d1SJeff Roberson 
3916ab3185d1SJeff Roberson 	return (NULL);
3917ab3185d1SJeff Roberson }
3918ab3185d1SJeff Roberson 
39198b987a77SJeff Roberson /*
39208b987a77SJeff Roberson  * Fetch an existing slab from a free or partial list.  Returns with the
39218b987a77SJeff Roberson  * keg domain lock held if a slab was found or unlocked if not.
39228b987a77SJeff Roberson  */
3923ab3185d1SJeff Roberson static uma_slab_t
3924194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags)
3925ab3185d1SJeff Roberson {
39268b987a77SJeff Roberson 	uma_slab_t slab;
3927194a979eSMark Johnston 	uint32_t reserve;
3928099a0e58SBosko Milekic 
39298b987a77SJeff Roberson 	/* HASH has a single free list. */
393054c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0)
39318b987a77SJeff Roberson 		domain = 0;
3932194a979eSMark Johnston 
39338b987a77SJeff Roberson 	KEG_LOCK(keg, domain);
3934194a979eSMark Johnston 	reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve;
39354ab3aee8SMark Johnston 	if (keg->uk_domain[domain].ud_free_items <= reserve ||
39368b987a77SJeff Roberson 	    (slab = keg_first_slab(keg, domain, rr)) == NULL) {
39378b987a77SJeff Roberson 		KEG_UNLOCK(keg, domain);
3938194a979eSMark Johnston 		return (NULL);
39398b987a77SJeff Roberson 	}
39408b987a77SJeff Roberson 	return (slab);
3941194a979eSMark Johnston }
3942194a979eSMark Johnston 
3943194a979eSMark Johnston static uma_slab_t
3944194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags)
3945194a979eSMark Johnston {
3946194a979eSMark Johnston 	struct vm_domainset_iter di;
3947194a979eSMark Johnston 	uma_slab_t slab;
3948194a979eSMark Johnston 	int aflags, domain;
3949194a979eSMark Johnston 	bool rr;
3950194a979eSMark Johnston 
3951fab343a7SMark Johnston 	KASSERT((flags & (M_WAITOK | M_NOVM)) != (M_WAITOK | M_NOVM),
3952fab343a7SMark Johnston 	    ("%s: invalid flags %#x", __func__, flags));
3953fab343a7SMark Johnston 
3954194a979eSMark Johnston restart:
3955bbee39c6SJeff Roberson 	/*
3956194a979eSMark Johnston 	 * Use the keg's policy if upper layers haven't already specified a
3957194a979eSMark Johnston 	 * domain (as happens with first-touch zones).
3958194a979eSMark Johnston 	 *
3959194a979eSMark Johnston 	 * To avoid races we run the iterator with the keg lock held, but that
3960194a979eSMark Johnston 	 * means that we cannot allow the vm_domainset layer to sleep.  Thus,
3961194a979eSMark Johnston 	 * clear M_WAITOK and handle low memory conditions locally.
3962bbee39c6SJeff Roberson 	 */
3963ab3185d1SJeff Roberson 	rr = rdomain == UMA_ANYDOMAIN;
3964ab3185d1SJeff Roberson 	if (rr) {
3965194a979eSMark Johnston 		aflags = (flags & ~M_WAITOK) | M_NOWAIT;
3966194a979eSMark Johnston 		vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
3967194a979eSMark Johnston 		    &aflags);
3968194a979eSMark Johnston 	} else {
3969194a979eSMark Johnston 		aflags = flags;
3970194a979eSMark Johnston 		domain = rdomain;
3971194a979eSMark Johnston 	}
3972ab3185d1SJeff Roberson 
3973194a979eSMark Johnston 	for (;;) {
3974194a979eSMark Johnston 		slab = keg_fetch_free_slab(keg, domain, rr, flags);
3975584061b4SJeff Roberson 		if (slab != NULL)
3976bbee39c6SJeff Roberson 			return (slab);
3977bbee39c6SJeff Roberson 
3978bbee39c6SJeff Roberson 		/*
3979fab343a7SMark Johnston 		 * M_NOVM is used to break the recursion that can otherwise
3980fab343a7SMark Johnston 		 * occur if low-level memory management routines use UMA.
3981bbee39c6SJeff Roberson 		 */
3982fab343a7SMark Johnston 		if ((flags & M_NOVM) == 0) {
398386220393SMark Johnston 			slab = keg_alloc_slab(keg, zone, domain, flags, aflags);
39848b987a77SJeff Roberson 			if (slab != NULL)
3985bbee39c6SJeff Roberson 				return (slab);
3986fab343a7SMark Johnston 		}
3987fab343a7SMark Johnston 
3988fab343a7SMark Johnston 		if (!rr) {
3989fab343a7SMark Johnston 			if ((flags & M_USE_RESERVE) != 0) {
3990fab343a7SMark Johnston 				/*
3991fab343a7SMark Johnston 				 * Drain reserves from other domains before
3992fab343a7SMark Johnston 				 * giving up or sleeping.  It may be useful to
3993fab343a7SMark Johnston 				 * support per-domain reserves eventually.
3994fab343a7SMark Johnston 				 */
3995fab343a7SMark Johnston 				rdomain = UMA_ANYDOMAIN;
3996fab343a7SMark Johnston 				goto restart;
3997fab343a7SMark Johnston 			}
3998fab343a7SMark Johnston 			if ((flags & M_WAITOK) == 0)
39993639ac42SJeff Roberson 				break;
4000fab343a7SMark Johnston 			vm_wait_domain(domain);
4001fab343a7SMark Johnston 		} else if (vm_domainset_iter_policy(&di, &domain) != 0) {
4002194a979eSMark Johnston 			if ((flags & M_WAITOK) != 0) {
400389d2fb14SKonstantin Belousov 				vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0);
4004194a979eSMark Johnston 				goto restart;
400530c5525bSAndrew Gallatin 			}
4006194a979eSMark Johnston 			break;
4007194a979eSMark Johnston 		}
4008ab3185d1SJeff Roberson 	}
4009ab3185d1SJeff Roberson 
4010bbee39c6SJeff Roberson 	/*
4011bbee39c6SJeff Roberson 	 * We might not have been able to get a slab but another cpu
4012bbee39c6SJeff Roberson 	 * could have while we were unlocked.  Check again before we
4013bbee39c6SJeff Roberson 	 * fail.
4014bbee39c6SJeff Roberson 	 */
40158b987a77SJeff Roberson 	if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL)
4016bbee39c6SJeff Roberson 		return (slab);
40178b987a77SJeff Roberson 
4018ab3185d1SJeff Roberson 	return (NULL);
4019ab3185d1SJeff Roberson }
4020bbee39c6SJeff Roberson 
4021d56368d7SBosko Milekic static void *
40220095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
4023bbee39c6SJeff Roberson {
4024ab3185d1SJeff Roberson 	uma_domain_t dom;
4025bbee39c6SJeff Roberson 	void *item;
40269b8db4d0SRyan Libby 	int freei;
4027bbee39c6SJeff Roberson 
40288b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, slab->us_domain);
4029099a0e58SBosko Milekic 
40308b987a77SJeff Roberson 	dom = &keg->uk_domain[slab->us_domain];
40319b78b1f4SJeff Roberson 	freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1;
40329b78b1f4SJeff Roberson 	BIT_CLR(keg->uk_ipers, freei, &slab->us_free);
40331e0701e1SJeff Roberson 	item = slab_item(slab, keg, freei);
4034bbee39c6SJeff Roberson 	slab->us_freecount--;
40354ab3aee8SMark Johnston 	dom->ud_free_items--;
4036ef72505eSJeff Roberson 
40374ab3aee8SMark Johnston 	/*
40384ab3aee8SMark Johnston 	 * Move this slab to the full list.  It must be on the partial list, so
40394ab3aee8SMark Johnston 	 * we do not need to update the free slab count.  In particular,
40404ab3aee8SMark Johnston 	 * keg_fetch_slab() always returns slabs on the partial list.
40414ab3aee8SMark Johnston 	 */
4042bbee39c6SJeff Roberson 	if (slab->us_freecount == 0) {
4043bbee39c6SJeff Roberson 		LIST_REMOVE(slab, us_link);
4044ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link);
4045bbee39c6SJeff Roberson 	}
4046bbee39c6SJeff Roberson 
4047bbee39c6SJeff Roberson 	return (item);
4048bbee39c6SJeff Roberson }
4049bbee39c6SJeff Roberson 
4050bbee39c6SJeff Roberson static int
4051b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags)
40520095a784SJeff Roberson {
40538b987a77SJeff Roberson 	uma_domain_t dom;
4054b75c4efcSAndrew Turner 	uma_zone_t zone;
40550095a784SJeff Roberson 	uma_slab_t slab;
40560095a784SJeff Roberson 	uma_keg_t keg;
4057a03af342SSean Bruno #ifdef NUMA
4058ab3185d1SJeff Roberson 	int stripe;
4059a03af342SSean Bruno #endif
40600095a784SJeff Roberson 	int i;
40610095a784SJeff Roberson 
4062b75c4efcSAndrew Turner 	zone = arg;
40630095a784SJeff Roberson 	slab = NULL;
4064584061b4SJeff Roberson 	keg = zone->uz_keg;
4065af526374SJeff Roberson 	/* Try to keep the buckets totally full */
40660095a784SJeff Roberson 	for (i = 0; i < max; ) {
4067584061b4SJeff Roberson 		if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL)
40680095a784SJeff Roberson 			break;
4069a03af342SSean Bruno #ifdef NUMA
4070ab3185d1SJeff Roberson 		stripe = howmany(max, vm_ndomains);
4071a03af342SSean Bruno #endif
40728b987a77SJeff Roberson 		dom = &keg->uk_domain[slab->us_domain];
40731b2dcc8cSMark Johnston 		do {
40740095a784SJeff Roberson 			bucket[i++] = slab_alloc_item(keg, slab);
40757585c5dbSMark Johnston 			if (keg->uk_reserve > 0 &&
40767585c5dbSMark Johnston 			    dom->ud_free_items <= keg->uk_reserve) {
40771b2dcc8cSMark Johnston 				/*
40781b2dcc8cSMark Johnston 				 * Avoid depleting the reserve after a
40791b2dcc8cSMark Johnston 				 * successful item allocation, even if
40801b2dcc8cSMark Johnston 				 * M_USE_RESERVE is specified.
40811b2dcc8cSMark Johnston 				 */
40821b2dcc8cSMark Johnston 				KEG_UNLOCK(keg, slab->us_domain);
40831b2dcc8cSMark Johnston 				goto out;
40841b2dcc8cSMark Johnston 			}
4085b6715dabSJeff Roberson #ifdef NUMA
4086ab3185d1SJeff Roberson 			/*
4087ab3185d1SJeff Roberson 			 * If the zone is striped we pick a new slab for every
4088ab3185d1SJeff Roberson 			 * N allocations.  Eliminating this conditional will
4089ab3185d1SJeff Roberson 			 * instead pick a new domain for each bucket rather
4090ab3185d1SJeff Roberson 			 * than stripe within each bucket.  The current option
4091ab3185d1SJeff Roberson 			 * produces more fragmentation and requires more cpu
4092ab3185d1SJeff Roberson 			 * time but yields better distribution.
4093ab3185d1SJeff Roberson 			 */
4094dfe13344SJeff Roberson 			if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 &&
4095ab3185d1SJeff Roberson 			    vm_ndomains > 1 && --stripe == 0)
4096ab3185d1SJeff Roberson 				break;
4097ab3185d1SJeff Roberson #endif
40981b2dcc8cSMark Johnston 		} while (slab->us_freecount != 0 && i < max);
40998b987a77SJeff Roberson 		KEG_UNLOCK(keg, slab->us_domain);
41001b2dcc8cSMark Johnston 
4101ab3185d1SJeff Roberson 		/* Don't block if we allocated any successfully. */
41020095a784SJeff Roberson 		flags &= ~M_WAITOK;
41030095a784SJeff Roberson 		flags |= M_NOWAIT;
41040095a784SJeff Roberson 	}
41051b2dcc8cSMark Johnston out:
41060095a784SJeff Roberson 	return i;
41070095a784SJeff Roberson }
41080095a784SJeff Roberson 
41094bd61e19SJeff Roberson static int
41104bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags)
41114bd61e19SJeff Roberson {
41124bd61e19SJeff Roberson 	uint64_t old, new, total, max;
41134bd61e19SJeff Roberson 
41144bd61e19SJeff Roberson 	/*
41154bd61e19SJeff Roberson 	 * The hard case.  We're going to sleep because there were existing
41164bd61e19SJeff Roberson 	 * sleepers or because we ran out of items.  This routine enforces
41174bd61e19SJeff Roberson 	 * fairness by keeping fifo order.
41184bd61e19SJeff Roberson 	 *
41194bd61e19SJeff Roberson 	 * First release our ill gotten gains and make some noise.
41204bd61e19SJeff Roberson 	 */
41214bd61e19SJeff Roberson 	for (;;) {
41224bd61e19SJeff Roberson 		zone_free_limit(zone, count);
41234bd61e19SJeff Roberson 		zone_log_warning(zone);
41244bd61e19SJeff Roberson 		zone_maxaction(zone);
41254bd61e19SJeff Roberson 		if (flags & M_NOWAIT)
41264bd61e19SJeff Roberson 			return (0);
41274bd61e19SJeff Roberson 
41284bd61e19SJeff Roberson 		/*
41294bd61e19SJeff Roberson 		 * We need to allocate an item or set ourself as a sleeper
41304bd61e19SJeff Roberson 		 * while the sleepq lock is held to avoid wakeup races.  This
41314bd61e19SJeff Roberson 		 * is essentially a home rolled semaphore.
41324bd61e19SJeff Roberson 		 */
41334bd61e19SJeff Roberson 		sleepq_lock(&zone->uz_max_items);
41344bd61e19SJeff Roberson 		old = zone->uz_items;
41354bd61e19SJeff Roberson 		do {
41364bd61e19SJeff Roberson 			MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX);
41374bd61e19SJeff Roberson 			/* Cache the max since we will evaluate twice. */
41384bd61e19SJeff Roberson 			max = zone->uz_max_items;
41394bd61e19SJeff Roberson 			if (UZ_ITEMS_SLEEPERS(old) != 0 ||
41404bd61e19SJeff Roberson 			    UZ_ITEMS_COUNT(old) >= max)
41414bd61e19SJeff Roberson 				new = old + UZ_ITEMS_SLEEPER;
41424bd61e19SJeff Roberson 			else
41434bd61e19SJeff Roberson 				new = old + MIN(count, max - old);
41444bd61e19SJeff Roberson 		} while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0);
41454bd61e19SJeff Roberson 
41464bd61e19SJeff Roberson 		/* We may have successfully allocated under the sleepq lock. */
41474bd61e19SJeff Roberson 		if (UZ_ITEMS_SLEEPERS(new) == 0) {
41484bd61e19SJeff Roberson 			sleepq_release(&zone->uz_max_items);
41494bd61e19SJeff Roberson 			return (new - old);
41504bd61e19SJeff Roberson 		}
41514bd61e19SJeff Roberson 
41524bd61e19SJeff Roberson 		/*
41534bd61e19SJeff Roberson 		 * This is in a different cacheline from uz_items so that we
41544bd61e19SJeff Roberson 		 * don't constantly invalidate the fastpath cacheline when we
41554bd61e19SJeff Roberson 		 * adjust item counts.  This could be limited to toggling on
41564bd61e19SJeff Roberson 		 * transitions.
41574bd61e19SJeff Roberson 		 */
41584bd61e19SJeff Roberson 		atomic_add_32(&zone->uz_sleepers, 1);
41594bd61e19SJeff Roberson 		atomic_add_64(&zone->uz_sleeps, 1);
41604bd61e19SJeff Roberson 
41614bd61e19SJeff Roberson 		/*
41624bd61e19SJeff Roberson 		 * We have added ourselves as a sleeper.  The sleepq lock
41634bd61e19SJeff Roberson 		 * protects us from wakeup races.  Sleep now and then retry.
41644bd61e19SJeff Roberson 		 */
41654bd61e19SJeff Roberson 		sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0);
41664bd61e19SJeff Roberson 		sleepq_wait(&zone->uz_max_items, PVM);
41674bd61e19SJeff Roberson 
41684bd61e19SJeff Roberson 		/*
41694bd61e19SJeff Roberson 		 * After wakeup, remove ourselves as a sleeper and try
41704bd61e19SJeff Roberson 		 * again.  We no longer have the sleepq lock for protection.
41714bd61e19SJeff Roberson 		 *
41724bd61e19SJeff Roberson 		 * Subract ourselves as a sleeper while attempting to add
41734bd61e19SJeff Roberson 		 * our count.
41744bd61e19SJeff Roberson 		 */
41754bd61e19SJeff Roberson 		atomic_subtract_32(&zone->uz_sleepers, 1);
41764bd61e19SJeff Roberson 		old = atomic_fetchadd_64(&zone->uz_items,
41774bd61e19SJeff Roberson 		    -(UZ_ITEMS_SLEEPER - count));
41784bd61e19SJeff Roberson 		/* We're no longer a sleeper. */
41794bd61e19SJeff Roberson 		old -= UZ_ITEMS_SLEEPER;
41804bd61e19SJeff Roberson 
41814bd61e19SJeff Roberson 		/*
41824bd61e19SJeff Roberson 		 * If we're still at the limit, restart.  Notably do not
41834bd61e19SJeff Roberson 		 * block on other sleepers.  Cache the max value to protect
41844bd61e19SJeff Roberson 		 * against changes via sysctl.
41854bd61e19SJeff Roberson 		 */
41864bd61e19SJeff Roberson 		total = UZ_ITEMS_COUNT(old);
41874bd61e19SJeff Roberson 		max = zone->uz_max_items;
41884bd61e19SJeff Roberson 		if (total >= max)
41894bd61e19SJeff Roberson 			continue;
41904bd61e19SJeff Roberson 		/* Truncate if necessary, otherwise wake other sleepers. */
41914bd61e19SJeff Roberson 		if (total + count > max) {
41924bd61e19SJeff Roberson 			zone_free_limit(zone, total + count - max);
41934bd61e19SJeff Roberson 			count = max - total;
41944bd61e19SJeff Roberson 		} else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0)
41954bd61e19SJeff Roberson 			wakeup_one(&zone->uz_max_items);
41964bd61e19SJeff Roberson 
41974bd61e19SJeff Roberson 		return (count);
41984bd61e19SJeff Roberson 	}
41994bd61e19SJeff Roberson }
42004bd61e19SJeff Roberson 
42014bd61e19SJeff Roberson /*
42024bd61e19SJeff Roberson  * Allocate 'count' items from our max_items limit.  Returns the number
42034bd61e19SJeff Roberson  * available.  If M_NOWAIT is not specified it will sleep until at least
42044bd61e19SJeff Roberson  * one item can be allocated.
42054bd61e19SJeff Roberson  */
42064bd61e19SJeff Roberson static int
42074bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags)
42084bd61e19SJeff Roberson {
42094bd61e19SJeff Roberson 	uint64_t old;
42104bd61e19SJeff Roberson 	uint64_t max;
42114bd61e19SJeff Roberson 
42124bd61e19SJeff Roberson 	max = zone->uz_max_items;
42134bd61e19SJeff Roberson 	MPASS(max > 0);
42144bd61e19SJeff Roberson 
42154bd61e19SJeff Roberson 	/*
42164bd61e19SJeff Roberson 	 * We expect normal allocations to succeed with a simple
42174bd61e19SJeff Roberson 	 * fetchadd.
42184bd61e19SJeff Roberson 	 */
42194bd61e19SJeff Roberson 	old = atomic_fetchadd_64(&zone->uz_items, count);
42204bd61e19SJeff Roberson 	if (__predict_true(old + count <= max))
42214bd61e19SJeff Roberson 		return (count);
42224bd61e19SJeff Roberson 
42234bd61e19SJeff Roberson 	/*
42244bd61e19SJeff Roberson 	 * If we had some items and no sleepers just return the
42254bd61e19SJeff Roberson 	 * truncated value.  We have to release the excess space
42264bd61e19SJeff Roberson 	 * though because that may wake sleepers who weren't woken
42274bd61e19SJeff Roberson 	 * because we were temporarily over the limit.
42284bd61e19SJeff Roberson 	 */
42294bd61e19SJeff Roberson 	if (old < max) {
42304bd61e19SJeff Roberson 		zone_free_limit(zone, (old + count) - max);
42314bd61e19SJeff Roberson 		return (max - old);
42324bd61e19SJeff Roberson 	}
42334bd61e19SJeff Roberson 	return (zone_alloc_limit_hard(zone, count, flags));
42344bd61e19SJeff Roberson }
42354bd61e19SJeff Roberson 
42364bd61e19SJeff Roberson /*
42374bd61e19SJeff Roberson  * Free a number of items back to the limit.
42384bd61e19SJeff Roberson  */
42394bd61e19SJeff Roberson static void
42404bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count)
42414bd61e19SJeff Roberson {
42424bd61e19SJeff Roberson 	uint64_t old;
42434bd61e19SJeff Roberson 
42444bd61e19SJeff Roberson 	MPASS(count > 0);
42454bd61e19SJeff Roberson 
42464bd61e19SJeff Roberson 	/*
42474bd61e19SJeff Roberson 	 * In the common case we either have no sleepers or
42484bd61e19SJeff Roberson 	 * are still over the limit and can just return.
42494bd61e19SJeff Roberson 	 */
42504bd61e19SJeff Roberson 	old = atomic_fetchadd_64(&zone->uz_items, -count);
42514bd61e19SJeff Roberson 	if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 ||
42524bd61e19SJeff Roberson 	   UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items))
42534bd61e19SJeff Roberson 		return;
42544bd61e19SJeff Roberson 
42554bd61e19SJeff Roberson 	/*
42564bd61e19SJeff Roberson 	 * Moderate the rate of wakeups.  Sleepers will continue
42574bd61e19SJeff Roberson 	 * to generate wakeups if necessary.
42584bd61e19SJeff Roberson 	 */
42594bd61e19SJeff Roberson 	wakeup_one(&zone->uz_max_items);
42604bd61e19SJeff Roberson }
42614bd61e19SJeff Roberson 
4262fc03d22bSJeff Roberson static uma_bucket_t
4263beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags)
4264bbee39c6SJeff Roberson {
4265bbee39c6SJeff Roberson 	uma_bucket_t bucket;
426609c8cb71SMark Johnston 	int error, maxbucket, cnt;
4267bbee39c6SJeff Roberson 
4268e63a1c2fSRyan Libby 	CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name,
4269e63a1c2fSRyan Libby 	    zone, domain);
427030c5525bSAndrew Gallatin 
4271c1685086SJeff Roberson 	/* Avoid allocs targeting empty domains. */
4272c1685086SJeff Roberson 	if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
4273c1685086SJeff Roberson 		domain = UMA_ANYDOMAIN;
42748c277118SMark Johnston 	else if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0)
4275c6fd3e23SJeff Roberson 		domain = UMA_ANYDOMAIN;
4276c1685086SJeff Roberson 
42774bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
42784bd61e19SJeff Roberson 		maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size,
42794bd61e19SJeff Roberson 		    M_NOWAIT);
42804bd61e19SJeff Roberson 	else
428120a4e154SJeff Roberson 		maxbucket = zone->uz_bucket_size;
42824bd61e19SJeff Roberson 	if (maxbucket == 0)
428354361f90SMark Johnston 		return (NULL);
4284beb8beefSJeff Roberson 
42856fd34d6fSJeff Roberson 	/* Don't wait for buckets, preserve caller's NOVM setting. */
42866fd34d6fSJeff Roberson 	bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
4287beb8beefSJeff Roberson 	if (bucket == NULL) {
4288beb8beefSJeff Roberson 		cnt = 0;
4289beb8beefSJeff Roberson 		goto out;
4290beb8beefSJeff Roberson 	}
42910095a784SJeff Roberson 
42920095a784SJeff Roberson 	bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
4293beb8beefSJeff Roberson 	    MIN(maxbucket, bucket->ub_entries), domain, flags);
42940095a784SJeff Roberson 
42950095a784SJeff Roberson 	/*
42960095a784SJeff Roberson 	 * Initialize the memory if necessary.
42970095a784SJeff Roberson 	 */
42980095a784SJeff Roberson 	if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
4299099a0e58SBosko Milekic 		int i;
4300bbee39c6SJeff Roberson 
430109c8cb71SMark Johnston 		for (i = 0; i < bucket->ub_cnt; i++) {
430209c8cb71SMark Johnston 			kasan_mark_item_valid(zone, bucket->ub_bucket[i]);
430309c8cb71SMark Johnston 			error = zone->uz_init(bucket->ub_bucket[i],
430409c8cb71SMark Johnston 			    zone->uz_size, flags);
430509c8cb71SMark Johnston 			kasan_mark_item_invalid(zone, bucket->ub_bucket[i]);
430609c8cb71SMark Johnston 			if (error != 0)
4307b23f72e9SBrian Feldman 				break;
430809c8cb71SMark Johnston 		}
430909c8cb71SMark Johnston 
4310b23f72e9SBrian Feldman 		/*
4311b23f72e9SBrian Feldman 		 * If we couldn't initialize the whole bucket, put the
4312b23f72e9SBrian Feldman 		 * rest back onto the freelist.
4313b23f72e9SBrian Feldman 		 */
4314b23f72e9SBrian Feldman 		if (i != bucket->ub_cnt) {
4315af526374SJeff Roberson 			zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
43160095a784SJeff Roberson 			    bucket->ub_cnt - i);
4317a5a262c6SBosko Milekic #ifdef INVARIANTS
43180095a784SJeff Roberson 			bzero(&bucket->ub_bucket[i],
43190095a784SJeff Roberson 			    sizeof(void *) * (bucket->ub_cnt - i));
4320a5a262c6SBosko Milekic #endif
4321b23f72e9SBrian Feldman 			bucket->ub_cnt = i;
4322b23f72e9SBrian Feldman 		}
4323099a0e58SBosko Milekic 	}
4324099a0e58SBosko Milekic 
4325beb8beefSJeff Roberson 	cnt = bucket->ub_cnt;
4326f7104ccdSAlexander Motin 	if (bucket->ub_cnt == 0) {
43276fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
43282efcc8cbSGleb Smirnoff 		counter_u64_add(zone->uz_fails, 1);
4329beb8beefSJeff Roberson 		bucket = NULL;
4330beb8beefSJeff Roberson 	}
4331beb8beefSJeff Roberson out:
43324bd61e19SJeff Roberson 	if (zone->uz_max_items > 0 && cnt < maxbucket)
43334bd61e19SJeff Roberson 		zone_free_limit(zone, maxbucket - cnt);
4334fc03d22bSJeff Roberson 
4335fc03d22bSJeff Roberson 	return (bucket);
4336fc03d22bSJeff Roberson }
4337fc03d22bSJeff Roberson 
43388355f576SJeff Roberson /*
43390095a784SJeff Roberson  * Allocates a single item from a zone.
43408355f576SJeff Roberson  *
43418355f576SJeff Roberson  * Arguments
43428355f576SJeff Roberson  *	zone   The zone to alloc for.
43438355f576SJeff Roberson  *	udata  The data to be passed to the constructor.
4344ab3185d1SJeff Roberson  *	domain The domain to allocate from or UMA_ANYDOMAIN.
4345a163d034SWarner Losh  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
43468355f576SJeff Roberson  *
43478355f576SJeff Roberson  * Returns
43488355f576SJeff Roberson  *	NULL if there is no memory and M_NOWAIT is set
4349bbee39c6SJeff Roberson  *	An item if successful
43508355f576SJeff Roberson  */
43518355f576SJeff Roberson 
43528355f576SJeff Roberson static void *
4353ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags)
43548355f576SJeff Roberson {
43558355f576SJeff Roberson 	void *item;
43568355f576SJeff Roberson 
4357791dda87SAndrew Gallatin 	if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) {
4358791dda87SAndrew Gallatin 		counter_u64_add(zone->uz_fails, 1);
4359bb15d1c7SGleb Smirnoff 		return (NULL);
4360791dda87SAndrew Gallatin 	}
43618355f576SJeff Roberson 
4362c1685086SJeff Roberson 	/* Avoid allocs targeting empty domains. */
4363c1685086SJeff Roberson 	if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
436430c5525bSAndrew Gallatin 		domain = UMA_ANYDOMAIN;
4365c1685086SJeff Roberson 
4366ab3185d1SJeff Roberson 	if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1)
4367beb8beefSJeff Roberson 		goto fail_cnt;
43688355f576SJeff Roberson 
4369099a0e58SBosko Milekic 	/*
4370099a0e58SBosko Milekic 	 * We have to call both the zone's init (not the keg's init)
4371099a0e58SBosko Milekic 	 * and the zone's ctor.  This is because the item is going from
4372099a0e58SBosko Milekic 	 * a keg slab directly to the user, and the user is expecting it
4373099a0e58SBosko Milekic 	 * to be both zone-init'd as well as zone-ctor'd.
4374099a0e58SBosko Milekic 	 */
4375b23f72e9SBrian Feldman 	if (zone->uz_init != NULL) {
437609c8cb71SMark Johnston 		int error;
437709c8cb71SMark Johnston 
437809c8cb71SMark Johnston 		kasan_mark_item_valid(zone, item);
437909c8cb71SMark Johnston 		error = zone->uz_init(item, zone->uz_size, flags);
438009c8cb71SMark Johnston 		kasan_mark_item_invalid(zone, item);
438109c8cb71SMark Johnston 		if (error != 0) {
4382bb15d1c7SGleb Smirnoff 			zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT);
4383beb8beefSJeff Roberson 			goto fail_cnt;
4384beb8beefSJeff Roberson 		}
4385beb8beefSJeff Roberson 	}
4386d4665eaaSJeff Roberson 	item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags,
4387d4665eaaSJeff Roberson 	    item);
4388beb8beefSJeff Roberson 	if (item == NULL)
43890095a784SJeff Roberson 		goto fail;
43908355f576SJeff Roberson 
43912efcc8cbSGleb Smirnoff 	counter_u64_add(zone->uz_allocs, 1);
43921431a748SGleb Smirnoff 	CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
43931431a748SGleb Smirnoff 	    zone->uz_name, zone);
43941431a748SGleb Smirnoff 
43958355f576SJeff Roberson 	return (item);
43960095a784SJeff Roberson 
4397beb8beefSJeff Roberson fail_cnt:
4398beb8beefSJeff Roberson 	counter_u64_add(zone->uz_fails, 1);
43990095a784SJeff Roberson fail:
44004bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
44014bd61e19SJeff Roberson 		zone_free_limit(zone, 1);
44021431a748SGleb Smirnoff 	CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
44031431a748SGleb Smirnoff 	    zone->uz_name, zone);
44044bd61e19SJeff Roberson 
44050095a784SJeff Roberson 	return (NULL);
44068355f576SJeff Roberson }
44078355f576SJeff Roberson 
44088355f576SJeff Roberson /* See uma.h */
44098355f576SJeff Roberson void
4410d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item)
4411d4665eaaSJeff Roberson {
4412d4665eaaSJeff Roberson 	uma_cache_t cache;
4413d4665eaaSJeff Roberson 	uma_cache_bucket_t bucket;
4414a7e1a585SJohn Baldwin 	int itemdomain;
4415a7e1a585SJohn Baldwin #ifdef NUMA
4416a7e1a585SJohn Baldwin 	int uz_flags;
4417a7e1a585SJohn Baldwin #endif
4418d4665eaaSJeff Roberson 
4419841e0a87SGleb Smirnoff 	CTR3(KTR_UMA, "uma_zfree_smr zone %s(%p) item %p",
4420841e0a87SGleb Smirnoff 	    zone->uz_name, zone, item);
4421841e0a87SGleb Smirnoff 
4422d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
4423d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
4424952c8964SMark Johnston 	    ("uma_zfree_smr: called with non-SMR zone."));
4425d4665eaaSJeff Roberson 	KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer."));
4426c6fd3e23SJeff Roberson 	SMR_ASSERT_NOT_ENTERED(zone->uz_smr);
4427d4665eaaSJeff Roberson 	if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN)
4428d4665eaaSJeff Roberson 		return;
4429d4665eaaSJeff Roberson #endif
4430d4665eaaSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4431c6fd3e23SJeff Roberson 	itemdomain = 0;
4432d4665eaaSJeff Roberson #ifdef NUMA
4433a7e1a585SJohn Baldwin 	uz_flags = cache_uz_flags(cache);
4434d4665eaaSJeff Roberson 	if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
443581302f1dSMark Johnston 		itemdomain = item_domain(item);
4436d4665eaaSJeff Roberson #endif
4437d4665eaaSJeff Roberson 	critical_enter();
4438d4665eaaSJeff Roberson 	do {
4439d4665eaaSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
4440d4665eaaSJeff Roberson 		/* SMR Zones must free to the free bucket. */
4441d4665eaaSJeff Roberson 		bucket = &cache->uc_freebucket;
4442d4665eaaSJeff Roberson #ifdef NUMA
4443d4665eaaSJeff Roberson 		if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4444c6fd3e23SJeff Roberson 		    PCPU_GET(domain) != itemdomain) {
4445d4665eaaSJeff Roberson 			bucket = &cache->uc_crossbucket;
4446d4665eaaSJeff Roberson 		}
4447d4665eaaSJeff Roberson #endif
4448d4665eaaSJeff Roberson 		if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
4449d4665eaaSJeff Roberson 			cache_bucket_push(cache, bucket, item);
4450d4665eaaSJeff Roberson 			critical_exit();
4451d4665eaaSJeff Roberson 			return;
4452d4665eaaSJeff Roberson 		}
44532cb67bd7SGleb Smirnoff 	} while (cache_free(zone, cache, NULL, itemdomain));
4454d4665eaaSJeff Roberson 	critical_exit();
4455d4665eaaSJeff Roberson 
4456d4665eaaSJeff Roberson 	/*
4457d4665eaaSJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
4458d4665eaaSJeff Roberson 	 */
4459d4665eaaSJeff Roberson 	zone_free_item(zone, item, NULL, SKIP_NONE);
4460d4665eaaSJeff Roberson }
4461d4665eaaSJeff Roberson 
4462d4665eaaSJeff Roberson /* See uma.h */
4463d4665eaaSJeff Roberson void
44648355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
44658355f576SJeff Roberson {
44668355f576SJeff Roberson 	uma_cache_t cache;
4467376b1ba3SJeff Roberson 	uma_cache_bucket_t bucket;
4468c6fd3e23SJeff Roberson 	int itemdomain, uz_flags;
44698355f576SJeff Roberson 
4470e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
447119fa89e9SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
447210cb2424SMark Murray 
447328782f73SGleb Smirnoff 	CTR3(KTR_UMA, "uma_zfree_arg zone %s(%p) item %p",
447428782f73SGleb Smirnoff 	    zone->uz_name, zone, item);
44753659f747SRobert Watson 
4476d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG
4477d4665eaaSJeff Roberson 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
4478952c8964SMark Johnston 	    ("uma_zfree_arg: called with SMR zone."));
4479d4665eaaSJeff Roberson 	if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN)
4480d4665eaaSJeff Roberson 		return;
4481d4665eaaSJeff Roberson #endif
448220ed0cb0SMatthew D Fleming         /* uma_zfree(..., NULL) does nothing, to match free(9). */
448320ed0cb0SMatthew D Fleming         if (item == NULL)
448420ed0cb0SMatthew D Fleming                 return;
4485cc7ce83aSJeff Roberson 
4486cc7ce83aSJeff Roberson 	/*
4487cc7ce83aSJeff Roberson 	 * We are accessing the per-cpu cache without a critical section to
4488cc7ce83aSJeff Roberson 	 * fetch size and flags.  This is acceptable, if we are preempted we
4489cc7ce83aSJeff Roberson 	 * will simply read another cpu's line.
4490cc7ce83aSJeff Roberson 	 */
4491cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4492cc7ce83aSJeff Roberson 	uz_flags = cache_uz_flags(cache);
4493d4665eaaSJeff Roberson 	if (UMA_ALWAYS_CTORDTOR ||
4494d4665eaaSJeff Roberson 	    __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0))
4495cc7ce83aSJeff Roberson 		item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE);
4496ef72505eSJeff Roberson 
4497af7f9b97SJeff Roberson 	/*
4498af7f9b97SJeff Roberson 	 * The race here is acceptable.  If we miss it we'll just have to wait
4499af7f9b97SJeff Roberson 	 * a little longer for the limits to be reset.
4500af7f9b97SJeff Roberson 	 */
4501cc7ce83aSJeff Roberson 	if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) {
45028a6776caSMark Johnston 		if (atomic_load_32(&zone->uz_sleepers) > 0)
4503fc03d22bSJeff Roberson 			goto zfree_item;
4504cc7ce83aSJeff Roberson 	}
4505af7f9b97SJeff Roberson 
45065d1ae027SRobert Watson 	/*
45075d1ae027SRobert Watson 	 * If possible, free to the per-CPU cache.  There are two
45085d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
45095d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
45105d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
45115d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
45125d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
45135d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to free to the
45145d1ae027SRobert Watson 	 * current cache; when we re-acquire the critical section, we must
45155d1ae027SRobert Watson 	 * detect and handle migration if it has occurred.
45165d1ae027SRobert Watson 	 */
4517c6fd3e23SJeff Roberson 	itemdomain = 0;
4518dfe13344SJeff Roberson #ifdef NUMA
4519dfe13344SJeff Roberson 	if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
452081302f1dSMark Johnston 		itemdomain = item_domain(item);
4521dfe13344SJeff Roberson #endif
45225d1ae027SRobert Watson 	critical_enter();
45230a81b439SJeff Roberson 	do {
4524cc7ce83aSJeff Roberson 		cache = &zone->uz_cpu[curcpu];
4525a553d4b8SJeff Roberson 		/*
4526dfe13344SJeff Roberson 		 * Try to free into the allocbucket first to give LIFO
4527dfe13344SJeff Roberson 		 * ordering for cache-hot datastructures.  Spill over
4528dfe13344SJeff Roberson 		 * into the freebucket if necessary.  Alloc will swap
4529dfe13344SJeff Roberson 		 * them if one runs dry.
4530a553d4b8SJeff Roberson 		 */
4531dfe13344SJeff Roberson 		bucket = &cache->uc_allocbucket;
4532d4665eaaSJeff Roberson #ifdef NUMA
4533d4665eaaSJeff Roberson 		if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4534c6fd3e23SJeff Roberson 		    PCPU_GET(domain) != itemdomain) {
4535d4665eaaSJeff Roberson 			bucket = &cache->uc_crossbucket;
4536d4665eaaSJeff Roberson 		} else
4537d4665eaaSJeff Roberson #endif
4538fe835cbfSJeff Roberson 		if (bucket->ucb_cnt == bucket->ucb_entries &&
4539fe835cbfSJeff Roberson 		   cache->uc_freebucket.ucb_cnt <
4540fe835cbfSJeff Roberson 		   cache->uc_freebucket.ucb_entries)
4541fe835cbfSJeff Roberson 			cache_bucket_swap(&cache->uc_freebucket,
4542fe835cbfSJeff Roberson 			    &cache->uc_allocbucket);
4543376b1ba3SJeff Roberson 		if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
4544376b1ba3SJeff Roberson 			cache_bucket_push(cache, bucket, item);
45455d1ae027SRobert Watson 			critical_exit();
45468355f576SJeff Roberson 			return;
4547fc03d22bSJeff Roberson 		}
45482cb67bd7SGleb Smirnoff 	} while (cache_free(zone, cache, udata, itemdomain));
45490a81b439SJeff Roberson 	critical_exit();
4550fc03d22bSJeff Roberson 
45518355f576SJeff Roberson 	/*
45520a81b439SJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
45538355f576SJeff Roberson 	 */
45540a81b439SJeff Roberson zfree_item:
45550a81b439SJeff Roberson 	zone_free_item(zone, item, udata, SKIP_DTOR);
45560a81b439SJeff Roberson }
4557fc03d22bSJeff Roberson 
4558dfe13344SJeff Roberson #ifdef NUMA
455991d947bfSJeff Roberson /*
456091d947bfSJeff Roberson  * sort crossdomain free buckets to domain correct buckets and cache
456191d947bfSJeff Roberson  * them.
456291d947bfSJeff Roberson  */
456391d947bfSJeff Roberson static void
456491d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata)
456591d947bfSJeff Roberson {
4566991f23efSMark Johnston 	struct uma_bucketlist emptybuckets, fullbuckets;
456791d947bfSJeff Roberson 	uma_zone_domain_t zdom;
456891d947bfSJeff Roberson 	uma_bucket_t b;
4569543117beSJeff Roberson 	smr_seq_t seq;
457091d947bfSJeff Roberson 	void *item;
457191d947bfSJeff Roberson 	int domain;
457291d947bfSJeff Roberson 
457391d947bfSJeff Roberson 	CTR3(KTR_UMA,
457491d947bfSJeff Roberson 	    "uma_zfree: zone %s(%p) draining cross bucket %p",
457591d947bfSJeff Roberson 	    zone->uz_name, zone, bucket);
457691d947bfSJeff Roberson 
4577543117beSJeff Roberson 	/*
4578543117beSJeff Roberson 	 * It is possible for buckets to arrive here out of order so we fetch
4579543117beSJeff Roberson 	 * the current smr seq rather than accepting the bucket's.
4580543117beSJeff Roberson 	 */
4581543117beSJeff Roberson 	seq = SMR_SEQ_INVALID;
4582543117beSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
4583226dd6dbSJeff Roberson 		seq = smr_advance(zone->uz_smr);
4584226dd6dbSJeff Roberson 
4585226dd6dbSJeff Roberson 	/*
4586226dd6dbSJeff Roberson 	 * To avoid having ndomain * ndomain buckets for sorting we have a
4587226dd6dbSJeff Roberson 	 * lock on the current crossfree bucket.  A full matrix with
4588226dd6dbSJeff Roberson 	 * per-domain locking could be used if necessary.
4589226dd6dbSJeff Roberson 	 */
4590991f23efSMark Johnston 	STAILQ_INIT(&emptybuckets);
4591226dd6dbSJeff Roberson 	STAILQ_INIT(&fullbuckets);
4592226dd6dbSJeff Roberson 	ZONE_CROSS_LOCK(zone);
4593991f23efSMark Johnston 	for (; bucket->ub_cnt > 0; bucket->ub_cnt--) {
459491d947bfSJeff Roberson 		item = bucket->ub_bucket[bucket->ub_cnt - 1];
459581302f1dSMark Johnston 		domain = item_domain(item);
4596c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(zone, domain);
459791d947bfSJeff Roberson 		if (zdom->uzd_cross == NULL) {
4598991f23efSMark Johnston 			if ((b = STAILQ_FIRST(&emptybuckets)) != NULL) {
4599991f23efSMark Johnston 				STAILQ_REMOVE_HEAD(&emptybuckets, ub_link);
4600991f23efSMark Johnston 				zdom->uzd_cross = b;
4601991f23efSMark Johnston 			} else {
4602991f23efSMark Johnston 				/*
4603991f23efSMark Johnston 				 * Avoid allocating a bucket with the cross lock
4604991f23efSMark Johnston 				 * held, since allocation can trigger a
4605991f23efSMark Johnston 				 * cross-domain free and bucket zones may
4606991f23efSMark Johnston 				 * allocate from each other.
4607991f23efSMark Johnston 				 */
4608991f23efSMark Johnston 				ZONE_CROSS_UNLOCK(zone);
4609991f23efSMark Johnston 				b = bucket_alloc(zone, udata, M_NOWAIT);
4610991f23efSMark Johnston 				if (b == NULL)
4611991f23efSMark Johnston 					goto out;
4612991f23efSMark Johnston 				ZONE_CROSS_LOCK(zone);
4613991f23efSMark Johnston 				if (zdom->uzd_cross != NULL) {
4614991f23efSMark Johnston 					STAILQ_INSERT_HEAD(&emptybuckets, b,
4615991f23efSMark Johnston 					    ub_link);
4616991f23efSMark Johnston 				} else {
4617991f23efSMark Johnston 					zdom->uzd_cross = b;
4618991f23efSMark Johnston 				}
4619991f23efSMark Johnston 			}
462091d947bfSJeff Roberson 		}
4621543117beSJeff Roberson 		b = zdom->uzd_cross;
4622543117beSJeff Roberson 		b->ub_bucket[b->ub_cnt++] = item;
4623543117beSJeff Roberson 		b->ub_seq = seq;
4624543117beSJeff Roberson 		if (b->ub_cnt == b->ub_entries) {
4625543117beSJeff Roberson 			STAILQ_INSERT_HEAD(&fullbuckets, b, ub_link);
4626991f23efSMark Johnston 			if ((b = STAILQ_FIRST(&emptybuckets)) != NULL)
4627991f23efSMark Johnston 				STAILQ_REMOVE_HEAD(&emptybuckets, ub_link);
4628991f23efSMark Johnston 			zdom->uzd_cross = b;
462991d947bfSJeff Roberson 		}
463091d947bfSJeff Roberson 	}
463191d947bfSJeff Roberson 	ZONE_CROSS_UNLOCK(zone);
4632991f23efSMark Johnston out:
4633c6fd3e23SJeff Roberson 	if (bucket->ub_cnt == 0)
4634d4665eaaSJeff Roberson 		bucket->ub_seq = SMR_SEQ_INVALID;
463591d947bfSJeff Roberson 	bucket_free(zone, bucket, udata);
4636c6fd3e23SJeff Roberson 
4637991f23efSMark Johnston 	while ((b = STAILQ_FIRST(&emptybuckets)) != NULL) {
4638991f23efSMark Johnston 		STAILQ_REMOVE_HEAD(&emptybuckets, ub_link);
4639991f23efSMark Johnston 		bucket_free(zone, b, udata);
4640991f23efSMark Johnston 	}
4641c6fd3e23SJeff Roberson 	while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) {
4642c6fd3e23SJeff Roberson 		STAILQ_REMOVE_HEAD(&fullbuckets, ub_link);
464381302f1dSMark Johnston 		domain = item_domain(b->ub_bucket[0]);
4644c6fd3e23SJeff Roberson 		zone_put_bucket(zone, domain, b, udata, true);
4645c6fd3e23SJeff Roberson 	}
464691d947bfSJeff Roberson }
464791d947bfSJeff Roberson #endif
464891d947bfSJeff Roberson 
46490a81b439SJeff Roberson static void
46500a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata,
4651c6fd3e23SJeff Roberson     int itemdomain, bool ws)
46520a81b439SJeff Roberson {
46530a81b439SJeff Roberson 
4654dfe13344SJeff Roberson #ifdef NUMA
46550a81b439SJeff Roberson 	/*
46560a81b439SJeff Roberson 	 * Buckets coming from the wrong domain will be entirely for the
46570a81b439SJeff Roberson 	 * only other domain on two domain systems.  In this case we can
46580a81b439SJeff Roberson 	 * simply cache them.  Otherwise we need to sort them back to
465991d947bfSJeff Roberson 	 * correct domains.
46600a81b439SJeff Roberson 	 */
4661c6fd3e23SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4662c6fd3e23SJeff Roberson 	    vm_ndomains > 2 && PCPU_GET(domain) != itemdomain) {
466391d947bfSJeff Roberson 		zone_free_cross(zone, bucket, udata);
46640a81b439SJeff Roberson 		return;
46650a81b439SJeff Roberson 	}
46660a81b439SJeff Roberson #endif
466791d947bfSJeff Roberson 
46680a81b439SJeff Roberson 	/*
46690a81b439SJeff Roberson 	 * Attempt to save the bucket in the zone's domain bucket cache.
46700a81b439SJeff Roberson 	 */
46710a81b439SJeff Roberson 	CTR3(KTR_UMA,
46720a81b439SJeff Roberson 	    "uma_zfree: zone %s(%p) putting bucket %p on free list",
46730a81b439SJeff Roberson 	    zone->uz_name, zone, bucket);
46740a81b439SJeff Roberson 	/* ub_cnt is pointing to the last free item */
4675c6fd3e23SJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0)
4676c6fd3e23SJeff Roberson 		itemdomain = zone_domain_lowest(zone, itemdomain);
4677c6fd3e23SJeff Roberson 	zone_put_bucket(zone, itemdomain, bucket, udata, ws);
46788355f576SJeff Roberson }
4679fc03d22bSJeff Roberson 
46804d104ba0SAlexander Motin /*
46810a81b439SJeff Roberson  * Populate a free or cross bucket for the current cpu cache.  Free any
46820a81b439SJeff Roberson  * existing full bucket either to the zone cache or back to the slab layer.
46830a81b439SJeff Roberson  *
46840a81b439SJeff Roberson  * Enters and returns in a critical section.  false return indicates that
46850a81b439SJeff Roberson  * we can not satisfy this free in the cache layer.  true indicates that
46860a81b439SJeff Roberson  * the caller should retry.
46874d104ba0SAlexander Motin  */
46880a81b439SJeff Roberson static __noinline bool
46892cb67bd7SGleb Smirnoff cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, int itemdomain)
46900a81b439SJeff Roberson {
4691dfe13344SJeff Roberson 	uma_cache_bucket_t cbucket;
4692d4665eaaSJeff Roberson 	uma_bucket_t newbucket, bucket;
46930a81b439SJeff Roberson 
46940a81b439SJeff Roberson 	CRITICAL_ASSERT(curthread);
46950a81b439SJeff Roberson 
4696d4665eaaSJeff Roberson 	if (zone->uz_bucket_size == 0)
46970a81b439SJeff Roberson 		return false;
46980a81b439SJeff Roberson 
4699cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4700d4665eaaSJeff Roberson 	newbucket = NULL;
47010a81b439SJeff Roberson 
47020a81b439SJeff Roberson 	/*
4703dfe13344SJeff Roberson 	 * FIRSTTOUCH domains need to free to the correct zdom.  When
4704dfe13344SJeff Roberson 	 * enabled this is the zdom of the item.   The bucket is the
4705dfe13344SJeff Roberson 	 * cross bucket if the current domain and itemdomain do not match.
47060a81b439SJeff Roberson 	 */
4707dfe13344SJeff Roberson 	cbucket = &cache->uc_freebucket;
4708dfe13344SJeff Roberson #ifdef NUMA
4709c6fd3e23SJeff Roberson 	if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) {
4710c6fd3e23SJeff Roberson 		if (PCPU_GET(domain) != itemdomain) {
4711dfe13344SJeff Roberson 			cbucket = &cache->uc_crossbucket;
4712dfe13344SJeff Roberson 			if (cbucket->ucb_cnt != 0)
4713c6fd3e23SJeff Roberson 				counter_u64_add(zone->uz_xdomain,
4714dfe13344SJeff Roberson 				    cbucket->ucb_cnt);
4715dfe13344SJeff Roberson 		}
4716c6fd3e23SJeff Roberson 	}
47170a81b439SJeff Roberson #endif
4718dfe13344SJeff Roberson 	bucket = cache_bucket_unload(cbucket);
4719c6fd3e23SJeff Roberson 	KASSERT(bucket == NULL || bucket->ub_cnt == bucket->ub_entries,
4720c6fd3e23SJeff Roberson 	    ("cache_free: Entered with non-full free bucket."));
47210a81b439SJeff Roberson 
47220a81b439SJeff Roberson 	/* We are no longer associated with this CPU. */
47230a81b439SJeff Roberson 	critical_exit();
47240a81b439SJeff Roberson 
4725d4665eaaSJeff Roberson 	/*
4726d4665eaaSJeff Roberson 	 * Don't let SMR zones operate without a free bucket.  Force
4727d4665eaaSJeff Roberson 	 * a synchronize and re-use this one.  We will only degrade
4728d4665eaaSJeff Roberson 	 * to a synchronize every bucket_size items rather than every
4729d4665eaaSJeff Roberson 	 * item if we fail to allocate a bucket.
4730d4665eaaSJeff Roberson 	 */
4731d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0) {
4732d4665eaaSJeff Roberson 		if (bucket != NULL)
4733d4665eaaSJeff Roberson 			bucket->ub_seq = smr_advance(zone->uz_smr);
4734d4665eaaSJeff Roberson 		newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4735d4665eaaSJeff Roberson 		if (newbucket == NULL && bucket != NULL) {
4736d4665eaaSJeff Roberson 			bucket_drain(zone, bucket);
4737d4665eaaSJeff Roberson 			newbucket = bucket;
4738d4665eaaSJeff Roberson 			bucket = NULL;
4739d4665eaaSJeff Roberson 		}
4740d4665eaaSJeff Roberson 	} else if (!bucketdisable)
4741d4665eaaSJeff Roberson 		newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4742d4665eaaSJeff Roberson 
47430a81b439SJeff Roberson 	if (bucket != NULL)
4744c6fd3e23SJeff Roberson 		zone_free_bucket(zone, bucket, udata, itemdomain, true);
4745a553d4b8SJeff Roberson 
4746fc03d22bSJeff Roberson 	critical_enter();
4747d4665eaaSJeff Roberson 	if ((bucket = newbucket) == NULL)
47480a81b439SJeff Roberson 		return (false);
4749cc7ce83aSJeff Roberson 	cache = &zone->uz_cpu[curcpu];
4750dfe13344SJeff Roberson #ifdef NUMA
4751fc03d22bSJeff Roberson 	/*
47520a81b439SJeff Roberson 	 * Check to see if we should be populating the cross bucket.  If it
47530a81b439SJeff Roberson 	 * is already populated we will fall through and attempt to populate
47540a81b439SJeff Roberson 	 * the free bucket.
4755fc03d22bSJeff Roberson 	 */
4756c6fd3e23SJeff Roberson 	if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) {
4757c6fd3e23SJeff Roberson 		if (PCPU_GET(domain) != itemdomain &&
4758376b1ba3SJeff Roberson 		    cache->uc_crossbucket.ucb_bucket == NULL) {
4759376b1ba3SJeff Roberson 			cache_bucket_load_cross(cache, bucket);
47600a81b439SJeff Roberson 			return (true);
47610a81b439SJeff Roberson 		}
47620a81b439SJeff Roberson 	}
47630a81b439SJeff Roberson #endif
47640a81b439SJeff Roberson 	/*
47650a81b439SJeff Roberson 	 * We may have lost the race to fill the bucket or switched CPUs.
47660a81b439SJeff Roberson 	 */
4767376b1ba3SJeff Roberson 	if (cache->uc_freebucket.ucb_bucket != NULL) {
4768fc03d22bSJeff Roberson 		critical_exit();
47696fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
47700a81b439SJeff Roberson 		critical_enter();
47710a81b439SJeff Roberson 	} else
4772376b1ba3SJeff Roberson 		cache_bucket_load_free(cache, bucket);
47738355f576SJeff Roberson 
47740a81b439SJeff Roberson 	return (true);
47758355f576SJeff Roberson }
47768355f576SJeff Roberson 
47778355f576SJeff Roberson static void
4778bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item)
47798355f576SJeff Roberson {
4780bb15d1c7SGleb Smirnoff 	uma_keg_t keg;
4781ab3185d1SJeff Roberson 	uma_domain_t dom;
47829b8db4d0SRyan Libby 	int freei;
4783099a0e58SBosko Milekic 
4784bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
47858b987a77SJeff Roberson 	KEG_LOCK_ASSERT(keg, slab->us_domain);
4786ab3185d1SJeff Roberson 
47878355f576SJeff Roberson 	/* Do we need to remove from any lists? */
47888b987a77SJeff Roberson 	dom = &keg->uk_domain[slab->us_domain];
4789099a0e58SBosko Milekic 	if (slab->us_freecount + 1 == keg->uk_ipers) {
47908355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
4791ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
47924ab3aee8SMark Johnston 		dom->ud_free_slabs++;
47938355f576SJeff Roberson 	} else if (slab->us_freecount == 0) {
47948355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
4795ab3185d1SJeff Roberson 		LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
47968355f576SJeff Roberson 	}
47978355f576SJeff Roberson 
4798ef72505eSJeff Roberson 	/* Slab management. */
47991e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
48009b78b1f4SJeff Roberson 	BIT_SET(keg->uk_ipers, freei, &slab->us_free);
48018355f576SJeff Roberson 	slab->us_freecount++;
48028355f576SJeff Roberson 
4803ef72505eSJeff Roberson 	/* Keg statistics. */
48044ab3aee8SMark Johnston 	dom->ud_free_items++;
48050095a784SJeff Roberson }
48060095a784SJeff Roberson 
48070095a784SJeff Roberson static void
4808b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt)
48090095a784SJeff Roberson {
48108b987a77SJeff Roberson 	struct mtx *lock;
4811b75c4efcSAndrew Turner 	uma_zone_t zone;
48120095a784SJeff Roberson 	uma_slab_t slab;
48130095a784SJeff Roberson 	uma_keg_t keg;
48140095a784SJeff Roberson 	uint8_t *mem;
48158b987a77SJeff Roberson 	void *item;
48160095a784SJeff Roberson 	int i;
48178355f576SJeff Roberson 
4818b75c4efcSAndrew Turner 	zone = arg;
4819bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
48208b987a77SJeff Roberson 	lock = NULL;
482154c5ae80SRyan Libby 	if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0))
48228b987a77SJeff Roberson 		lock = KEG_LOCK(keg, 0);
48230095a784SJeff Roberson 	for (i = 0; i < cnt; i++) {
48240095a784SJeff Roberson 		item = bucket[i];
482554c5ae80SRyan Libby 		if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) {
48260095a784SJeff Roberson 			slab = vtoslab((vm_offset_t)item);
48278b987a77SJeff Roberson 		} else {
48288b987a77SJeff Roberson 			mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
482954c5ae80SRyan Libby 			if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0)
48308b987a77SJeff Roberson 				slab = hash_sfind(&keg->uk_hash, mem);
48318b987a77SJeff Roberson 			else
48328b987a77SJeff Roberson 				slab = (uma_slab_t)(mem + keg->uk_pgoff);
48338b987a77SJeff Roberson 		}
48348b987a77SJeff Roberson 		if (lock != KEG_LOCKPTR(keg, slab->us_domain)) {
48358b987a77SJeff Roberson 			if (lock != NULL)
48368b987a77SJeff Roberson 				mtx_unlock(lock);
48378b987a77SJeff Roberson 			lock = KEG_LOCK(keg, slab->us_domain);
48388b987a77SJeff Roberson 		}
4839bb15d1c7SGleb Smirnoff 		slab_free_item(zone, slab, item);
48400095a784SJeff Roberson 	}
48418b987a77SJeff Roberson 	if (lock != NULL)
48428b987a77SJeff Roberson 		mtx_unlock(lock);
48438355f576SJeff Roberson }
48448355f576SJeff Roberson 
48450095a784SJeff Roberson /*
48460095a784SJeff Roberson  * Frees a single item to any zone.
48470095a784SJeff Roberson  *
48480095a784SJeff Roberson  * Arguments:
48490095a784SJeff Roberson  *	zone   The zone to free to
48500095a784SJeff Roberson  *	item   The item we're freeing
48510095a784SJeff Roberson  *	udata  User supplied data for the dtor
48520095a784SJeff Roberson  *	skip   Skip dtors and finis
48530095a784SJeff Roberson  */
48546d88d784SJeff Roberson static __noinline void
48550095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
48560095a784SJeff Roberson {
4857c5deaf04SGleb Smirnoff 
4858d4665eaaSJeff Roberson 	/*
4859d4665eaaSJeff Roberson 	 * If a free is sent directly to an SMR zone we have to
4860d4665eaaSJeff Roberson 	 * synchronize immediately because the item can instantly
4861d4665eaaSJeff Roberson 	 * be reallocated. This should only happen in degenerate
4862d4665eaaSJeff Roberson 	 * cases when no memory is available for per-cpu caches.
4863d4665eaaSJeff Roberson 	 */
4864d4665eaaSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE)
4865d4665eaaSJeff Roberson 		smr_synchronize(zone->uz_smr);
4866d4665eaaSJeff Roberson 
4867cc7ce83aSJeff Roberson 	item_dtor(zone, item, zone->uz_size, udata, skip);
48680095a784SJeff Roberson 
486909c8cb71SMark Johnston 	if (skip < SKIP_FINI && zone->uz_fini) {
487009c8cb71SMark Johnston 		kasan_mark_item_valid(zone, item);
48710095a784SJeff Roberson 		zone->uz_fini(item, zone->uz_size);
487209c8cb71SMark Johnston 		kasan_mark_item_invalid(zone, item);
487309c8cb71SMark Johnston 	}
48740095a784SJeff Roberson 
48750095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, &item, 1);
4876bb15d1c7SGleb Smirnoff 
4877bb15d1c7SGleb Smirnoff 	if (skip & SKIP_CNT)
4878bb15d1c7SGleb Smirnoff 		return;
4879bb15d1c7SGleb Smirnoff 
48802efcc8cbSGleb Smirnoff 	counter_u64_add(zone->uz_frees, 1);
48812efcc8cbSGleb Smirnoff 
48824bd61e19SJeff Roberson 	if (zone->uz_max_items > 0)
48834bd61e19SJeff Roberson 		zone_free_limit(zone, 1);
4884bb45b411SGleb Smirnoff }
48850095a784SJeff Roberson 
48868355f576SJeff Roberson /* See uma.h */
48871c6cae97SLawrence Stewart int
4888736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems)
4889736ee590SJeff Roberson {
4890e574d407SMark Johnston 
4891e574d407SMark Johnston 	/*
4892e574d407SMark Johnston 	 * If the limit is small, we may need to constrain the maximum per-CPU
4893e574d407SMark Johnston 	 * cache size, or disable caching entirely.
4894e574d407SMark Johnston 	 */
4895e574d407SMark Johnston 	uma_zone_set_maxcache(zone, nitems);
4896bb15d1c7SGleb Smirnoff 
48974bd61e19SJeff Roberson 	/*
48984bd61e19SJeff Roberson 	 * XXX This can misbehave if the zone has any allocations with
48994bd61e19SJeff Roberson 	 * no limit and a limit is imposed.  There is currently no
49004bd61e19SJeff Roberson 	 * way to clear a limit.
49014bd61e19SJeff Roberson 	 */
4902bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4903d53927b0SMark Johnston 	if (zone->uz_max_items == 0)
4904d53927b0SMark Johnston 		ZONE_ASSERT_COLD(zone);
4905bb15d1c7SGleb Smirnoff 	zone->uz_max_items = nitems;
4906cc7ce83aSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_LIMIT;
4907cc7ce83aSJeff Roberson 	zone_update_caches(zone);
49084bd61e19SJeff Roberson 	/* We may need to wake waiters. */
49094bd61e19SJeff Roberson 	wakeup(&zone->uz_max_items);
4910bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4911bb15d1c7SGleb Smirnoff 
4912bb15d1c7SGleb Smirnoff 	return (nitems);
4913bb15d1c7SGleb Smirnoff }
4914bb15d1c7SGleb Smirnoff 
4915bb15d1c7SGleb Smirnoff /* See uma.h */
4916003cf08bSMark Johnston void
4917bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems)
4918bb15d1c7SGleb Smirnoff {
4919e574d407SMark Johnston 	int bpcpu, bpdom, bsize, nb;
4920bb15d1c7SGleb Smirnoff 
4921bb15d1c7SGleb Smirnoff 	ZONE_LOCK(zone);
4922e574d407SMark Johnston 
4923e574d407SMark Johnston 	/*
4924e574d407SMark Johnston 	 * Compute a lower bound on the number of items that may be cached in
4925e574d407SMark Johnston 	 * the zone.  Each CPU gets at least two buckets, and for cross-domain
4926e574d407SMark Johnston 	 * frees we use an additional bucket per CPU and per domain.  Select the
4927e574d407SMark Johnston 	 * largest bucket size that does not exceed half of the requested limit,
4928e574d407SMark Johnston 	 * with the left over space given to the full bucket cache.
4929e574d407SMark Johnston 	 */
4930e574d407SMark Johnston 	bpdom = 0;
4931003cf08bSMark Johnston 	bpcpu = 2;
4932e574d407SMark Johnston #ifdef NUMA
4933e574d407SMark Johnston 	if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && vm_ndomains > 1) {
4934003cf08bSMark Johnston 		bpcpu++;
4935e574d407SMark Johnston 		bpdom++;
4936003cf08bSMark Johnston 	}
4937e574d407SMark Johnston #endif
4938e574d407SMark Johnston 	nb = bpcpu * mp_ncpus + bpdom * vm_ndomains;
4939e574d407SMark Johnston 	bsize = nitems / nb / 2;
4940e574d407SMark Johnston 	if (bsize > BUCKET_MAX)
4941e574d407SMark Johnston 		bsize = BUCKET_MAX;
4942e574d407SMark Johnston 	else if (bsize == 0 && nitems / nb > 0)
4943e574d407SMark Johnston 		bsize = 1;
4944e574d407SMark Johnston 	zone->uz_bucket_size_max = zone->uz_bucket_size = bsize;
494520a4e154SJeff Roberson 	if (zone->uz_bucket_size_min > zone->uz_bucket_size_max)
494620a4e154SJeff Roberson 		zone->uz_bucket_size_min = zone->uz_bucket_size_max;
4947e574d407SMark Johnston 	zone->uz_bucket_max = nitems - nb * bsize;
4948bb15d1c7SGleb Smirnoff 	ZONE_UNLOCK(zone);
4949736ee590SJeff Roberson }
4950736ee590SJeff Roberson 
4951736ee590SJeff Roberson /* See uma.h */
4952e49471b0SAndre Oppermann int
4953e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone)
4954e49471b0SAndre Oppermann {
4955e49471b0SAndre Oppermann 	int nitems;
4956e49471b0SAndre Oppermann 
4957727c6918SJeff Roberson 	nitems = atomic_load_64(&zone->uz_max_items);
4958e49471b0SAndre Oppermann 
4959e49471b0SAndre Oppermann 	return (nitems);
4960e49471b0SAndre Oppermann }
4961e49471b0SAndre Oppermann 
4962e49471b0SAndre Oppermann /* See uma.h */
49632f891cd5SPawel Jakub Dawidek void
49642f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning)
49652f891cd5SPawel Jakub Dawidek {
49662f891cd5SPawel Jakub Dawidek 
4967727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
49682f891cd5SPawel Jakub Dawidek 	zone->uz_warning = warning;
49692f891cd5SPawel Jakub Dawidek }
49702f891cd5SPawel Jakub Dawidek 
49712f891cd5SPawel Jakub Dawidek /* See uma.h */
497254503a13SJonathan T. Looney void
497354503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
497454503a13SJonathan T. Looney {
497554503a13SJonathan T. Looney 
4976727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
4977e60b2fcbSGleb Smirnoff 	TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
497854503a13SJonathan T. Looney }
497954503a13SJonathan T. Looney 
498054503a13SJonathan T. Looney /* See uma.h */
4981c4ae7908SLawrence Stewart int
4982c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone)
4983c4ae7908SLawrence Stewart {
4984c4ae7908SLawrence Stewart 	int64_t nitems;
4985c4ae7908SLawrence Stewart 	u_int i;
4986c4ae7908SLawrence Stewart 
4987bfb6b7a1SJeff Roberson 	nitems = 0;
4988bfb6b7a1SJeff Roberson 	if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER)
49892efcc8cbSGleb Smirnoff 		nitems = counter_u64_fetch(zone->uz_allocs) -
49902efcc8cbSGleb Smirnoff 		    counter_u64_fetch(zone->uz_frees);
4991727c6918SJeff Roberson 	CPU_FOREACH(i)
4992727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) -
4993727c6918SJeff Roberson 		    atomic_load_64(&zone->uz_cpu[i].uc_frees);
4994c4ae7908SLawrence Stewart 
4995c4ae7908SLawrence Stewart 	return (nitems < 0 ? 0 : nitems);
4996c4ae7908SLawrence Stewart }
4997c4ae7908SLawrence Stewart 
499820a4e154SJeff Roberson static uint64_t
499920a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone)
500020a4e154SJeff Roberson {
500120a4e154SJeff Roberson 	uint64_t nitems;
500220a4e154SJeff Roberson 	u_int i;
500320a4e154SJeff Roberson 
5004bfb6b7a1SJeff Roberson 	nitems = 0;
5005bfb6b7a1SJeff Roberson 	if (zone->uz_allocs != EARLY_COUNTER)
500620a4e154SJeff Roberson 		nitems = counter_u64_fetch(zone->uz_allocs);
5007727c6918SJeff Roberson 	CPU_FOREACH(i)
5008727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs);
500920a4e154SJeff Roberson 
501020a4e154SJeff Roberson 	return (nitems);
501120a4e154SJeff Roberson }
501220a4e154SJeff Roberson 
501320a4e154SJeff Roberson static uint64_t
501420a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone)
501520a4e154SJeff Roberson {
501620a4e154SJeff Roberson 	uint64_t nitems;
501720a4e154SJeff Roberson 	u_int i;
501820a4e154SJeff Roberson 
5019bfb6b7a1SJeff Roberson 	nitems = 0;
5020bfb6b7a1SJeff Roberson 	if (zone->uz_frees != EARLY_COUNTER)
502120a4e154SJeff Roberson 		nitems = counter_u64_fetch(zone->uz_frees);
5022727c6918SJeff Roberson 	CPU_FOREACH(i)
5023727c6918SJeff Roberson 		nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees);
502420a4e154SJeff Roberson 
502520a4e154SJeff Roberson 	return (nitems);
502620a4e154SJeff Roberson }
502720a4e154SJeff Roberson 
502831c251a0SJeff Roberson #ifdef INVARIANTS
502931c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */
503031c251a0SJeff Roberson static uint64_t
503131c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg)
503231c251a0SJeff Roberson {
503331c251a0SJeff Roberson 	uma_zone_t z;
503431c251a0SJeff Roberson 	uint64_t nitems;
503531c251a0SJeff Roberson 
503631c251a0SJeff Roberson 	nitems = 0;
503731c251a0SJeff Roberson 	LIST_FOREACH(z, &keg->uk_zones, uz_link)
503831c251a0SJeff Roberson 		nitems += uma_zone_get_allocs(z);
503931c251a0SJeff Roberson 
504031c251a0SJeff Roberson 	return (nitems);
504131c251a0SJeff Roberson }
504231c251a0SJeff Roberson #endif
504331c251a0SJeff Roberson 
5044c4ae7908SLawrence Stewart /* See uma.h */
5045736ee590SJeff Roberson void
5046099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit)
5047099a0e58SBosko Milekic {
5048e20a199fSJeff Roberson 	uma_keg_t keg;
5049e20a199fSJeff Roberson 
5050bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5051727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
5052e20a199fSJeff Roberson 	keg->uk_init = uminit;
5053099a0e58SBosko Milekic }
5054099a0e58SBosko Milekic 
5055099a0e58SBosko Milekic /* See uma.h */
5056099a0e58SBosko Milekic void
5057099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
5058099a0e58SBosko Milekic {
5059e20a199fSJeff Roberson 	uma_keg_t keg;
5060e20a199fSJeff Roberson 
5061bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5062727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
5063e20a199fSJeff Roberson 	keg->uk_fini = fini;
5064099a0e58SBosko Milekic }
5065099a0e58SBosko Milekic 
5066099a0e58SBosko Milekic /* See uma.h */
5067099a0e58SBosko Milekic void
5068099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
5069099a0e58SBosko Milekic {
5070af526374SJeff Roberson 
5071727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
5072099a0e58SBosko Milekic 	zone->uz_init = zinit;
5073099a0e58SBosko Milekic }
5074099a0e58SBosko Milekic 
5075099a0e58SBosko Milekic /* See uma.h */
5076099a0e58SBosko Milekic void
5077099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
5078099a0e58SBosko Milekic {
5079af526374SJeff Roberson 
5080727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
5081099a0e58SBosko Milekic 	zone->uz_fini = zfini;
5082099a0e58SBosko Milekic }
5083099a0e58SBosko Milekic 
5084099a0e58SBosko Milekic /* See uma.h */
5085099a0e58SBosko Milekic void
50868355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef)
50878355f576SJeff Roberson {
50880095a784SJeff Roberson 	uma_keg_t keg;
5089e20a199fSJeff Roberson 
5090bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5091727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
50920095a784SJeff Roberson 	keg->uk_freef = freef;
50938355f576SJeff Roberson }
50948355f576SJeff Roberson 
50958355f576SJeff Roberson /* See uma.h */
50968355f576SJeff Roberson void
50978355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
50988355f576SJeff Roberson {
5099e20a199fSJeff Roberson 	uma_keg_t keg;
5100e20a199fSJeff Roberson 
5101bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5102727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
5103e20a199fSJeff Roberson 	keg->uk_allocf = allocf;
51048355f576SJeff Roberson }
51058355f576SJeff Roberson 
51068355f576SJeff Roberson /* See uma.h */
51076fd34d6fSJeff Roberson void
5108d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr)
5109d4665eaaSJeff Roberson {
5110d4665eaaSJeff Roberson 
5111d4665eaaSJeff Roberson 	ZONE_ASSERT_COLD(zone);
5112d4665eaaSJeff Roberson 
51137f746c9fSMateusz Guzik 	KASSERT(smr != NULL, ("Got NULL smr"));
51147f746c9fSMateusz Guzik 	KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
51157f746c9fSMateusz Guzik 	    ("zone %p (%s) already uses SMR", zone, zone->uz_name));
5116d4665eaaSJeff Roberson 	zone->uz_flags |= UMA_ZONE_SMR;
5117d4665eaaSJeff Roberson 	zone->uz_smr = smr;
5118d4665eaaSJeff Roberson 	zone_update_caches(zone);
5119d4665eaaSJeff Roberson }
5120d4665eaaSJeff Roberson 
5121d4665eaaSJeff Roberson smr_t
5122d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone)
5123d4665eaaSJeff Roberson {
5124d4665eaaSJeff Roberson 
5125d4665eaaSJeff Roberson 	return (zone->uz_smr);
5126d4665eaaSJeff Roberson }
5127d4665eaaSJeff Roberson 
5128d4665eaaSJeff Roberson /* See uma.h */
5129d4665eaaSJeff Roberson void
51306fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items)
51316fd34d6fSJeff Roberson {
51326fd34d6fSJeff Roberson 	uma_keg_t keg;
51336fd34d6fSJeff Roberson 
5134bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5135727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
51366fd34d6fSJeff Roberson 	keg->uk_reserve = items;
51376fd34d6fSJeff Roberson }
51386fd34d6fSJeff Roberson 
51396fd34d6fSJeff Roberson /* See uma.h */
51408355f576SJeff Roberson int
5141a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count)
51428355f576SJeff Roberson {
5143099a0e58SBosko Milekic 	uma_keg_t keg;
51448355f576SJeff Roberson 	vm_offset_t kva;
51459ba30bcbSZbigniew Bodek 	u_int pages;
51468355f576SJeff Roberson 
5147bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
5148727c6918SJeff Roberson 	KEG_ASSERT_COLD(keg);
5149727c6918SJeff Roberson 	ZONE_ASSERT_COLD(zone);
51508355f576SJeff Roberson 
515179c9f942SJeff Roberson 	pages = howmany(count, keg->uk_ipers) * keg->uk_ppera;
5152a553d4b8SJeff Roberson 
5153a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
5154a4915c21SAttilio Rao 	if (keg->uk_ppera > 1) {
5155a4915c21SAttilio Rao #else
5156a4915c21SAttilio Rao 	if (1) {
5157a4915c21SAttilio Rao #endif
515857223e99SAndriy Gapon 		kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
5159d1f42ac2SAlan Cox 		if (kva == 0)
51608355f576SJeff Roberson 			return (0);
5161a4915c21SAttilio Rao 	} else
5162a4915c21SAttilio Rao 		kva = 0;
5163bb15d1c7SGleb Smirnoff 
5164bb15d1c7SGleb Smirnoff 	MPASS(keg->uk_kva == 0);
5165099a0e58SBosko Milekic 	keg->uk_kva = kva;
5166a4915c21SAttilio Rao 	keg->uk_offset = 0;
5167bb15d1c7SGleb Smirnoff 	zone->uz_max_items = pages * keg->uk_ipers;
5168a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
5169a4915c21SAttilio Rao 	keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
5170a4915c21SAttilio Rao #else
5171a4915c21SAttilio Rao 	keg->uk_allocf = noobj_alloc;
5172a4915c21SAttilio Rao #endif
5173cc7ce83aSJeff Roberson 	keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
5174cc7ce83aSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
5175cc7ce83aSJeff Roberson 	zone_update_caches(zone);
5176af526374SJeff Roberson 
51778355f576SJeff Roberson 	return (1);
51788355f576SJeff Roberson }
51798355f576SJeff Roberson 
51808355f576SJeff Roberson /* See uma.h */
51818355f576SJeff Roberson void
51828355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items)
51838355f576SJeff Roberson {
5184920239efSMark Johnston 	struct vm_domainset_iter di;
5185ab3185d1SJeff Roberson 	uma_domain_t dom;
51868355f576SJeff Roberson 	uma_slab_t slab;
5187099a0e58SBosko Milekic 	uma_keg_t keg;
518886220393SMark Johnston 	int aflags, domain, slabs;
51898355f576SJeff Roberson 
5190bb15d1c7SGleb Smirnoff 	KEG_GET(zone, keg);
519179c9f942SJeff Roberson 	slabs = howmany(items, keg->uk_ipers);
5192194a979eSMark Johnston 	while (slabs-- > 0) {
519386220393SMark Johnston 		aflags = M_NOWAIT;
519486220393SMark Johnston 		vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
519586220393SMark Johnston 		    &aflags);
519686220393SMark Johnston 		for (;;) {
519786220393SMark Johnston 			slab = keg_alloc_slab(keg, zone, domain, M_WAITOK,
519886220393SMark Johnston 			    aflags);
519986220393SMark Johnston 			if (slab != NULL) {
5200ab3185d1SJeff Roberson 				dom = &keg->uk_domain[slab->us_domain];
52014ab3aee8SMark Johnston 				/*
52024ab3aee8SMark Johnston 				 * keg_alloc_slab() always returns a slab on the
52034ab3aee8SMark Johnston 				 * partial list.
52044ab3aee8SMark Johnston 				 */
52058b987a77SJeff Roberson 				LIST_REMOVE(slab, us_link);
520686220393SMark Johnston 				LIST_INSERT_HEAD(&dom->ud_free_slab, slab,
520786220393SMark Johnston 				    us_link);
52084ab3aee8SMark Johnston 				dom->ud_free_slabs++;
52098b987a77SJeff Roberson 				KEG_UNLOCK(keg, slab->us_domain);
5210920239efSMark Johnston 				break;
52118355f576SJeff Roberson 			}
52128b987a77SJeff Roberson 			if (vm_domainset_iter_policy(&di, &domain) != 0)
521389d2fb14SKonstantin Belousov 				vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0);
521486220393SMark Johnston 		}
521586220393SMark Johnston 	}
521686220393SMark Johnston }
52178355f576SJeff Roberson 
5218ed581bf6SJeff Roberson /*
5219ed581bf6SJeff Roberson  * Returns a snapshot of memory consumption in bytes.
5220ed581bf6SJeff Roberson  */
5221ed581bf6SJeff Roberson size_t
5222ed581bf6SJeff Roberson uma_zone_memory(uma_zone_t zone)
5223ed581bf6SJeff Roberson {
5224ed581bf6SJeff Roberson 	size_t sz;
5225ed581bf6SJeff Roberson 	int i;
5226ed581bf6SJeff Roberson 
5227ed581bf6SJeff Roberson 	sz = 0;
5228ed581bf6SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_CACHE) {
5229ed581bf6SJeff Roberson 		for (i = 0; i < vm_ndomains; i++)
5230c6fd3e23SJeff Roberson 			sz += ZDOM_GET(zone, i)->uzd_nitems;
5231ed581bf6SJeff Roberson 		return (sz * zone->uz_size);
5232ed581bf6SJeff Roberson 	}
5233ed581bf6SJeff Roberson 	for (i = 0; i < vm_ndomains; i++)
5234ed581bf6SJeff Roberson 		sz += zone->uz_keg->uk_domain[i].ud_pages;
5235ed581bf6SJeff Roberson 
5236ed581bf6SJeff Roberson 	return (sz * PAGE_SIZE);
5237ed581bf6SJeff Roberson }
5238ed581bf6SJeff Roberson 
5239389a3fa6SMark Johnston struct uma_reclaim_args {
5240389a3fa6SMark Johnston 	int	domain;
5241389a3fa6SMark Johnston 	int	req;
5242389a3fa6SMark Johnston };
5243389a3fa6SMark Johnston 
5244389a3fa6SMark Johnston static void
5245389a3fa6SMark Johnston uma_reclaim_domain_cb(uma_zone_t zone, void *arg)
5246389a3fa6SMark Johnston {
5247389a3fa6SMark Johnston 	struct uma_reclaim_args *args;
5248389a3fa6SMark Johnston 
5249389a3fa6SMark Johnston 	args = arg;
5250389a3fa6SMark Johnston 	if ((zone->uz_flags & UMA_ZONE_UNMANAGED) == 0)
5251389a3fa6SMark Johnston 		uma_zone_reclaim_domain(zone, args->req, args->domain);
5252389a3fa6SMark Johnston }
5253389a3fa6SMark Johnston 
52548355f576SJeff Roberson /* See uma.h */
525508cfa56eSMark Johnston void
525608cfa56eSMark Johnston uma_reclaim(int req)
52578355f576SJeff Roberson {
5258aabe13f1SMark Johnston 	uma_reclaim_domain(req, UMA_ANYDOMAIN);
5259aabe13f1SMark Johnston }
526044ec2b63SKonstantin Belousov 
5261aabe13f1SMark Johnston void
5262aabe13f1SMark Johnston uma_reclaim_domain(int req, int domain)
5263aabe13f1SMark Johnston {
5264389a3fa6SMark Johnston 	struct uma_reclaim_args args;
5265aabe13f1SMark Johnston 
526686bbae32SJeff Roberson 	bucket_enable();
526708cfa56eSMark Johnston 
5268389a3fa6SMark Johnston 	args.domain = domain;
5269389a3fa6SMark Johnston 	args.req = req;
5270389a3fa6SMark Johnston 
5271aabe13f1SMark Johnston 	sx_slock(&uma_reclaim_lock);
527208cfa56eSMark Johnston 	switch (req) {
527308cfa56eSMark Johnston 	case UMA_RECLAIM_TRIM:
527408cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN:
5275389a3fa6SMark Johnston 		zone_foreach(uma_reclaim_domain_cb, &args);
5276aabe13f1SMark Johnston 		break;
527708cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN_CPU:
5278389a3fa6SMark Johnston 		zone_foreach(uma_reclaim_domain_cb, &args);
527908cfa56eSMark Johnston 		pcpu_cache_drain_safe(NULL);
5280389a3fa6SMark Johnston 		zone_foreach(uma_reclaim_domain_cb, &args);
528108cfa56eSMark Johnston 		break;
528208cfa56eSMark Johnston 	default:
528308cfa56eSMark Johnston 		panic("unhandled reclamation request %d", req);
528408cfa56eSMark Johnston 	}
52850f9b7bf3SMark Johnston 
52868355f576SJeff Roberson 	/*
52878355f576SJeff Roberson 	 * Some slabs may have been freed but this zone will be visited early
52888355f576SJeff Roberson 	 * we visit again so that we can free pages that are empty once other
52898355f576SJeff Roberson 	 * zones are drained.  We have to do the same for buckets.
52908355f576SJeff Roberson 	 */
5291389a3fa6SMark Johnston 	uma_zone_reclaim_domain(slabzones[0], UMA_RECLAIM_DRAIN, domain);
5292389a3fa6SMark Johnston 	uma_zone_reclaim_domain(slabzones[1], UMA_RECLAIM_DRAIN, domain);
5293aabe13f1SMark Johnston 	bucket_zone_drain(domain);
5294aabe13f1SMark Johnston 	sx_sunlock(&uma_reclaim_lock);
52958355f576SJeff Roberson }
52968355f576SJeff Roberson 
52972e47807cSJeff Roberson static volatile int uma_reclaim_needed;
529844ec2b63SKonstantin Belousov 
529944ec2b63SKonstantin Belousov void
530044ec2b63SKonstantin Belousov uma_reclaim_wakeup(void)
530144ec2b63SKonstantin Belousov {
530244ec2b63SKonstantin Belousov 
53032e47807cSJeff Roberson 	if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
53042e47807cSJeff Roberson 		wakeup(uma_reclaim);
530544ec2b63SKonstantin Belousov }
530644ec2b63SKonstantin Belousov 
530744ec2b63SKonstantin Belousov void
530844ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused)
530944ec2b63SKonstantin Belousov {
531044ec2b63SKonstantin Belousov 
531144ec2b63SKonstantin Belousov 	for (;;) {
531208cfa56eSMark Johnston 		sx_xlock(&uma_reclaim_lock);
5313200f8117SKonstantin Belousov 		while (atomic_load_int(&uma_reclaim_needed) == 0)
531408cfa56eSMark Johnston 			sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl",
53152e47807cSJeff Roberson 			    hz);
531608cfa56eSMark Johnston 		sx_xunlock(&uma_reclaim_lock);
53179b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
531808cfa56eSMark Johnston 		uma_reclaim(UMA_RECLAIM_DRAIN_CPU);
5319200f8117SKonstantin Belousov 		atomic_store_int(&uma_reclaim_needed, 0);
53202e47807cSJeff Roberson 		/* Don't fire more than once per-second. */
53212e47807cSJeff Roberson 		pause("umarclslp", hz);
532244ec2b63SKonstantin Belousov 	}
532344ec2b63SKonstantin Belousov }
532444ec2b63SKonstantin Belousov 
5325663b416fSJohn Baldwin /* See uma.h */
532608cfa56eSMark Johnston void
532708cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req)
532808cfa56eSMark Johnston {
5329aabe13f1SMark Johnston 	uma_zone_reclaim_domain(zone, req, UMA_ANYDOMAIN);
5330aabe13f1SMark Johnston }
533108cfa56eSMark Johnston 
5332aabe13f1SMark Johnston void
5333aabe13f1SMark Johnston uma_zone_reclaim_domain(uma_zone_t zone, int req, int domain)
5334aabe13f1SMark Johnston {
533508cfa56eSMark Johnston 	switch (req) {
533608cfa56eSMark Johnston 	case UMA_RECLAIM_TRIM:
5337389a3fa6SMark Johnston 		zone_reclaim(zone, domain, M_NOWAIT, false);
533808cfa56eSMark Johnston 		break;
533908cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN:
5340389a3fa6SMark Johnston 		zone_reclaim(zone, domain, M_NOWAIT, true);
534108cfa56eSMark Johnston 		break;
534208cfa56eSMark Johnston 	case UMA_RECLAIM_DRAIN_CPU:
534308cfa56eSMark Johnston 		pcpu_cache_drain_safe(zone);
5344389a3fa6SMark Johnston 		zone_reclaim(zone, domain, M_NOWAIT, true);
534508cfa56eSMark Johnston 		break;
534608cfa56eSMark Johnston 	default:
534708cfa56eSMark Johnston 		panic("unhandled reclamation request %d", req);
534808cfa56eSMark Johnston 	}
534908cfa56eSMark Johnston }
535008cfa56eSMark Johnston 
535108cfa56eSMark Johnston /* See uma.h */
5352663b416fSJohn Baldwin int
5353663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone)
5354663b416fSJohn Baldwin {
5355663b416fSJohn Baldwin 
5356727c6918SJeff Roberson 	return (atomic_load_32(&zone->uz_sleepers) > 0);
53576c125b8dSMohan Srinivasan }
53586c125b8dSMohan Srinivasan 
53592e47807cSJeff Roberson unsigned long
53602e47807cSJeff Roberson uma_limit(void)
53612e47807cSJeff Roberson {
53622e47807cSJeff Roberson 
53632e47807cSJeff Roberson 	return (uma_kmem_limit);
53642e47807cSJeff Roberson }
53652e47807cSJeff Roberson 
53662e47807cSJeff Roberson void
53672e47807cSJeff Roberson uma_set_limit(unsigned long limit)
53682e47807cSJeff Roberson {
53692e47807cSJeff Roberson 
53702e47807cSJeff Roberson 	uma_kmem_limit = limit;
53712e47807cSJeff Roberson }
53722e47807cSJeff Roberson 
53732e47807cSJeff Roberson unsigned long
53742e47807cSJeff Roberson uma_size(void)
53752e47807cSJeff Roberson {
53762e47807cSJeff Roberson 
5377058f0f74SMark Johnston 	return (atomic_load_long(&uma_kmem_total));
5378ad5b0f5bSJeff Roberson }
5379ad5b0f5bSJeff Roberson 
5380ad5b0f5bSJeff Roberson long
5381ad5b0f5bSJeff Roberson uma_avail(void)
5382ad5b0f5bSJeff Roberson {
5383ad5b0f5bSJeff Roberson 
5384058f0f74SMark Johnston 	return (uma_kmem_limit - uma_size());
53852e47807cSJeff Roberson }
53862e47807cSJeff Roberson 
5387a0d4b0aeSRobert Watson #ifdef DDB
53888355f576SJeff Roberson /*
53897a52a97eSRobert Watson  * Generate statistics across both the zone and its per-cpu cache's.  Return
53907a52a97eSRobert Watson  * desired statistics if the pointer is non-NULL for that statistic.
53917a52a97eSRobert Watson  *
53927a52a97eSRobert Watson  * Note: does not update the zone statistics, as it can't safely clear the
53937a52a97eSRobert Watson  * per-CPU cache statistic.
53947a52a97eSRobert Watson  *
53957a52a97eSRobert Watson  */
53967a52a97eSRobert Watson static void
53970f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp,
5398c1685086SJeff Roberson     uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp)
53997a52a97eSRobert Watson {
54007a52a97eSRobert Watson 	uma_cache_t cache;
5401c1685086SJeff Roberson 	uint64_t allocs, frees, sleeps, xdomain;
54027a52a97eSRobert Watson 	int cachefree, cpu;
54037a52a97eSRobert Watson 
5404c1685086SJeff Roberson 	allocs = frees = sleeps = xdomain = 0;
54057a52a97eSRobert Watson 	cachefree = 0;
54063aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
54077a52a97eSRobert Watson 		cache = &z->uz_cpu[cpu];
5408376b1ba3SJeff Roberson 		cachefree += cache->uc_allocbucket.ucb_cnt;
5409376b1ba3SJeff Roberson 		cachefree += cache->uc_freebucket.ucb_cnt;
5410376b1ba3SJeff Roberson 		xdomain += cache->uc_crossbucket.ucb_cnt;
5411376b1ba3SJeff Roberson 		cachefree += cache->uc_crossbucket.ucb_cnt;
54127a52a97eSRobert Watson 		allocs += cache->uc_allocs;
54137a52a97eSRobert Watson 		frees += cache->uc_frees;
54147a52a97eSRobert Watson 	}
54152efcc8cbSGleb Smirnoff 	allocs += counter_u64_fetch(z->uz_allocs);
54162efcc8cbSGleb Smirnoff 	frees += counter_u64_fetch(z->uz_frees);
5417c6fd3e23SJeff Roberson 	xdomain += counter_u64_fetch(z->uz_xdomain);
5418bf965959SSean Bruno 	sleeps += z->uz_sleeps;
54197a52a97eSRobert Watson 	if (cachefreep != NULL)
54207a52a97eSRobert Watson 		*cachefreep = cachefree;
54217a52a97eSRobert Watson 	if (allocsp != NULL)
54227a52a97eSRobert Watson 		*allocsp = allocs;
54237a52a97eSRobert Watson 	if (freesp != NULL)
54247a52a97eSRobert Watson 		*freesp = frees;
5425bf965959SSean Bruno 	if (sleepsp != NULL)
5426bf965959SSean Bruno 		*sleepsp = sleeps;
5427c1685086SJeff Roberson 	if (xdomainp != NULL)
5428c1685086SJeff Roberson 		*xdomainp = xdomain;
54297a52a97eSRobert Watson }
5430a0d4b0aeSRobert Watson #endif /* DDB */
54317a52a97eSRobert Watson 
54327a52a97eSRobert Watson static int
54337a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
54347a52a97eSRobert Watson {
54357a52a97eSRobert Watson 	uma_keg_t kz;
54367a52a97eSRobert Watson 	uma_zone_t z;
54377a52a97eSRobert Watson 	int count;
54387a52a97eSRobert Watson 
54397a52a97eSRobert Watson 	count = 0;
5440111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
54417a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
54427a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
54437a52a97eSRobert Watson 			count++;
54447a52a97eSRobert Watson 	}
5445b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link)
5446b47acb0aSGleb Smirnoff 		count++;
5447b47acb0aSGleb Smirnoff 
5448111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
54497a52a97eSRobert Watson 	return (sysctl_handle_int(oidp, &count, 0, req));
54507a52a97eSRobert Watson }
54517a52a97eSRobert Watson 
5452b47acb0aSGleb Smirnoff static void
5453b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf,
5454b47acb0aSGleb Smirnoff     struct uma_percpu_stat *ups, bool internal)
5455b47acb0aSGleb Smirnoff {
5456b47acb0aSGleb Smirnoff 	uma_zone_domain_t zdom;
5457b47acb0aSGleb Smirnoff 	uma_cache_t cache;
5458b47acb0aSGleb Smirnoff 	int i;
5459b47acb0aSGleb Smirnoff 
5460b47acb0aSGleb Smirnoff 	for (i = 0; i < vm_ndomains; i++) {
5461c6fd3e23SJeff Roberson 		zdom = ZDOM_GET(z, i);
5462b47acb0aSGleb Smirnoff 		uth->uth_zone_free += zdom->uzd_nitems;
5463b47acb0aSGleb Smirnoff 	}
5464b47acb0aSGleb Smirnoff 	uth->uth_allocs = counter_u64_fetch(z->uz_allocs);
5465b47acb0aSGleb Smirnoff 	uth->uth_frees = counter_u64_fetch(z->uz_frees);
5466b47acb0aSGleb Smirnoff 	uth->uth_fails = counter_u64_fetch(z->uz_fails);
5467c6fd3e23SJeff Roberson 	uth->uth_xdomain = counter_u64_fetch(z->uz_xdomain);
5468b47acb0aSGleb Smirnoff 	uth->uth_sleeps = z->uz_sleeps;
54691de9724eSMark Johnston 
5470b47acb0aSGleb Smirnoff 	for (i = 0; i < mp_maxid + 1; i++) {
5471b47acb0aSGleb Smirnoff 		bzero(&ups[i], sizeof(*ups));
5472b47acb0aSGleb Smirnoff 		if (internal || CPU_ABSENT(i))
5473b47acb0aSGleb Smirnoff 			continue;
5474b47acb0aSGleb Smirnoff 		cache = &z->uz_cpu[i];
5475376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt;
5476376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt;
5477376b1ba3SJeff Roberson 		ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt;
5478b47acb0aSGleb Smirnoff 		ups[i].ups_allocs = cache->uc_allocs;
5479b47acb0aSGleb Smirnoff 		ups[i].ups_frees = cache->uc_frees;
5480b47acb0aSGleb Smirnoff 	}
5481b47acb0aSGleb Smirnoff }
5482b47acb0aSGleb Smirnoff 
54837a52a97eSRobert Watson static int
54847a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
54857a52a97eSRobert Watson {
54867a52a97eSRobert Watson 	struct uma_stream_header ush;
54877a52a97eSRobert Watson 	struct uma_type_header uth;
548863b5d112SKonstantin Belousov 	struct uma_percpu_stat *ups;
54897a52a97eSRobert Watson 	struct sbuf sbuf;
54907a52a97eSRobert Watson 	uma_keg_t kz;
54917a52a97eSRobert Watson 	uma_zone_t z;
54924bd61e19SJeff Roberson 	uint64_t items;
54938b987a77SJeff Roberson 	uint32_t kfree, pages;
54944e657159SMatthew D Fleming 	int count, error, i;
54957a52a97eSRobert Watson 
549600f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
549700f0e671SMatthew D Fleming 	if (error != 0)
549800f0e671SMatthew D Fleming 		return (error);
54994e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
55001eafc078SIan Lepore 	sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
550163b5d112SKonstantin Belousov 	ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK);
55024e657159SMatthew D Fleming 
5503404a593eSMatthew D Fleming 	count = 0;
5504111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
55057a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
55067a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
55077a52a97eSRobert Watson 			count++;
55087a52a97eSRobert Watson 	}
55097a52a97eSRobert Watson 
5510b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link)
5511b47acb0aSGleb Smirnoff 		count++;
5512b47acb0aSGleb Smirnoff 
55137a52a97eSRobert Watson 	/*
55147a52a97eSRobert Watson 	 * Insert stream header.
55157a52a97eSRobert Watson 	 */
55167a52a97eSRobert Watson 	bzero(&ush, sizeof(ush));
55177a52a97eSRobert Watson 	ush.ush_version = UMA_STREAM_VERSION;
5518ab3a57c0SRobert Watson 	ush.ush_maxcpus = (mp_maxid + 1);
55197a52a97eSRobert Watson 	ush.ush_count = count;
55204e657159SMatthew D Fleming 	(void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
55217a52a97eSRobert Watson 
55227a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
55238b987a77SJeff Roberson 		kfree = pages = 0;
55248b987a77SJeff Roberson 		for (i = 0; i < vm_ndomains; i++) {
55254ab3aee8SMark Johnston 			kfree += kz->uk_domain[i].ud_free_items;
55268b987a77SJeff Roberson 			pages += kz->uk_domain[i].ud_pages;
55278b987a77SJeff Roberson 		}
55287a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
55297a52a97eSRobert Watson 			bzero(&uth, sizeof(uth));
5530cbbb4a00SRobert Watson 			strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
55317a52a97eSRobert Watson 			uth.uth_align = kz->uk_align;
55327a52a97eSRobert Watson 			uth.uth_size = kz->uk_size;
55337a52a97eSRobert Watson 			uth.uth_rsize = kz->uk_rsize;
55344bd61e19SJeff Roberson 			if (z->uz_max_items > 0) {
55354bd61e19SJeff Roberson 				items = UZ_ITEMS_COUNT(z->uz_items);
55364bd61e19SJeff Roberson 				uth.uth_pages = (items / kz->uk_ipers) *
5537bb15d1c7SGleb Smirnoff 					kz->uk_ppera;
55384bd61e19SJeff Roberson 			} else
55398b987a77SJeff Roberson 				uth.uth_pages = pages;
5540f8c86a5fSGleb Smirnoff 			uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) *
5541bb15d1c7SGleb Smirnoff 			    kz->uk_ppera;
5542bb15d1c7SGleb Smirnoff 			uth.uth_limit = z->uz_max_items;
55438b987a77SJeff Roberson 			uth.uth_keg_free = kfree;
5544cbbb4a00SRobert Watson 
5545cbbb4a00SRobert Watson 			/*
5546cbbb4a00SRobert Watson 			 * A zone is secondary is it is not the first entry
5547cbbb4a00SRobert Watson 			 * on the keg's zone list.
5548cbbb4a00SRobert Watson 			 */
5549e20a199fSJeff Roberson 			if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
5550cbbb4a00SRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z))
5551cbbb4a00SRobert Watson 				uth.uth_zone_flags = UTH_ZONE_SECONDARY;
5552b47acb0aSGleb Smirnoff 			uma_vm_zone_stats(&uth, z, &sbuf, ups,
5553b47acb0aSGleb Smirnoff 			    kz->uk_flags & UMA_ZFLAG_INTERNAL);
555463b5d112SKonstantin Belousov 			(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
555563b5d112SKonstantin Belousov 			for (i = 0; i < mp_maxid + 1; i++)
555663b5d112SKonstantin Belousov 				(void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
55577a52a97eSRobert Watson 		}
55587a52a97eSRobert Watson 	}
5559b47acb0aSGleb Smirnoff 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
5560b47acb0aSGleb Smirnoff 		bzero(&uth, sizeof(uth));
5561b47acb0aSGleb Smirnoff 		strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
5562b47acb0aSGleb Smirnoff 		uth.uth_size = z->uz_size;
5563b47acb0aSGleb Smirnoff 		uma_vm_zone_stats(&uth, z, &sbuf, ups, false);
5564b47acb0aSGleb Smirnoff 		(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
5565b47acb0aSGleb Smirnoff 		for (i = 0; i < mp_maxid + 1; i++)
5566b47acb0aSGleb Smirnoff 			(void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
5567b47acb0aSGleb Smirnoff 	}
5568b47acb0aSGleb Smirnoff 
5569111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
55704e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
55714e657159SMatthew D Fleming 	sbuf_delete(&sbuf);
557263b5d112SKonstantin Belousov 	free(ups, M_TEMP);
55737a52a97eSRobert Watson 	return (error);
55747a52a97eSRobert Watson }
557548c5777eSRobert Watson 
55760a5a3ccbSGleb Smirnoff int
55770a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
55780a5a3ccbSGleb Smirnoff {
55790a5a3ccbSGleb Smirnoff 	uma_zone_t zone = *(uma_zone_t *)arg1;
558016be9f54SGleb Smirnoff 	int error, max;
55810a5a3ccbSGleb Smirnoff 
558216be9f54SGleb Smirnoff 	max = uma_zone_get_max(zone);
55830a5a3ccbSGleb Smirnoff 	error = sysctl_handle_int(oidp, &max, 0, req);
55840a5a3ccbSGleb Smirnoff 	if (error || !req->newptr)
55850a5a3ccbSGleb Smirnoff 		return (error);
55860a5a3ccbSGleb Smirnoff 
55870a5a3ccbSGleb Smirnoff 	uma_zone_set_max(zone, max);
55880a5a3ccbSGleb Smirnoff 
55890a5a3ccbSGleb Smirnoff 	return (0);
55900a5a3ccbSGleb Smirnoff }
55910a5a3ccbSGleb Smirnoff 
55920a5a3ccbSGleb Smirnoff int
55930a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
55940a5a3ccbSGleb Smirnoff {
559520a4e154SJeff Roberson 	uma_zone_t zone;
55960a5a3ccbSGleb Smirnoff 	int cur;
55970a5a3ccbSGleb Smirnoff 
559820a4e154SJeff Roberson 	/*
559920a4e154SJeff Roberson 	 * Some callers want to add sysctls for global zones that
560020a4e154SJeff Roberson 	 * may not yet exist so they pass a pointer to a pointer.
560120a4e154SJeff Roberson 	 */
560220a4e154SJeff Roberson 	if (arg2 == 0)
560320a4e154SJeff Roberson 		zone = *(uma_zone_t *)arg1;
560420a4e154SJeff Roberson 	else
560520a4e154SJeff Roberson 		zone = arg1;
56060a5a3ccbSGleb Smirnoff 	cur = uma_zone_get_cur(zone);
56070a5a3ccbSGleb Smirnoff 	return (sysctl_handle_int(oidp, &cur, 0, req));
56080a5a3ccbSGleb Smirnoff }
56090a5a3ccbSGleb Smirnoff 
561020a4e154SJeff Roberson static int
561120a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS)
561220a4e154SJeff Roberson {
561320a4e154SJeff Roberson 	uma_zone_t zone = arg1;
561420a4e154SJeff Roberson 	uint64_t cur;
561520a4e154SJeff Roberson 
561620a4e154SJeff Roberson 	cur = uma_zone_get_allocs(zone);
561720a4e154SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
561820a4e154SJeff Roberson }
561920a4e154SJeff Roberson 
562020a4e154SJeff Roberson static int
562120a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS)
562220a4e154SJeff Roberson {
562320a4e154SJeff Roberson 	uma_zone_t zone = arg1;
562420a4e154SJeff Roberson 	uint64_t cur;
562520a4e154SJeff Roberson 
562620a4e154SJeff Roberson 	cur = uma_zone_get_frees(zone);
562720a4e154SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
562820a4e154SJeff Roberson }
562920a4e154SJeff Roberson 
56306d204a6aSRyan Libby static int
56316d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS)
56326d204a6aSRyan Libby {
56336d204a6aSRyan Libby 	struct sbuf sbuf;
56346d204a6aSRyan Libby 	uma_zone_t zone = arg1;
56356d204a6aSRyan Libby 	int error;
56366d204a6aSRyan Libby 
56376d204a6aSRyan Libby 	sbuf_new_for_sysctl(&sbuf, NULL, 0, req);
56386d204a6aSRyan Libby 	if (zone->uz_flags != 0)
56396d204a6aSRyan Libby 		sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS);
56406d204a6aSRyan Libby 	else
56416d204a6aSRyan Libby 		sbuf_printf(&sbuf, "0");
56426d204a6aSRyan Libby 	error = sbuf_finish(&sbuf);
56436d204a6aSRyan Libby 	sbuf_delete(&sbuf);
56446d204a6aSRyan Libby 
56456d204a6aSRyan Libby 	return (error);
56466d204a6aSRyan Libby }
56476d204a6aSRyan Libby 
5648f7af5015SRyan Libby static int
5649f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS)
5650f7af5015SRyan Libby {
5651f7af5015SRyan Libby 	uma_keg_t keg = arg1;
5652f7af5015SRyan Libby 	int avail, effpct, total;
5653f7af5015SRyan Libby 
5654f7af5015SRyan Libby 	total = keg->uk_ppera * PAGE_SIZE;
565554c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0)
56569b8db4d0SRyan Libby 		total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize;
5657f7af5015SRyan Libby 	/*
5658f7af5015SRyan Libby 	 * We consider the client's requested size and alignment here, not the
5659f7af5015SRyan Libby 	 * real size determination uk_rsize, because we also adjust the real
5660f7af5015SRyan Libby 	 * size for internal implementation reasons (max bitset size).
5661f7af5015SRyan Libby 	 */
5662f7af5015SRyan Libby 	avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1);
5663f7af5015SRyan Libby 	if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
5664f7af5015SRyan Libby 		avail *= mp_maxid + 1;
5665f7af5015SRyan Libby 	effpct = 100 * avail / total;
5666f7af5015SRyan Libby 	return (sysctl_handle_int(oidp, &effpct, 0, req));
5667f7af5015SRyan Libby }
5668f7af5015SRyan Libby 
56694bd61e19SJeff Roberson static int
56704bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS)
56714bd61e19SJeff Roberson {
56724bd61e19SJeff Roberson 	uma_zone_t zone = arg1;
56734bd61e19SJeff Roberson 	uint64_t cur;
56744bd61e19SJeff Roberson 
56754bd61e19SJeff Roberson 	cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items));
56764bd61e19SJeff Roberson 	return (sysctl_handle_64(oidp, &cur, 0, req));
56774bd61e19SJeff Roberson }
56784bd61e19SJeff Roberson 
56799542ea7bSGleb Smirnoff #ifdef INVARIANTS
56809542ea7bSGleb Smirnoff static uma_slab_t
56819542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item)
56829542ea7bSGleb Smirnoff {
56839542ea7bSGleb Smirnoff 	uma_slab_t slab;
56849542ea7bSGleb Smirnoff 	uma_keg_t keg;
56859542ea7bSGleb Smirnoff 	uint8_t *mem;
56869542ea7bSGleb Smirnoff 
56879542ea7bSGleb Smirnoff 	/*
56889542ea7bSGleb Smirnoff 	 * It is safe to return the slab here even though the
56899542ea7bSGleb Smirnoff 	 * zone is unlocked because the item's allocation state
56909542ea7bSGleb Smirnoff 	 * essentially holds a reference.
56919542ea7bSGleb Smirnoff 	 */
5692727c6918SJeff Roberson 	mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
5693727c6918SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5694bb15d1c7SGleb Smirnoff 		return (NULL);
569554c5ae80SRyan Libby 	if (zone->uz_flags & UMA_ZFLAG_VTOSLAB)
5696727c6918SJeff Roberson 		return (vtoslab((vm_offset_t)mem));
5697bb15d1c7SGleb Smirnoff 	keg = zone->uz_keg;
569854c5ae80SRyan Libby 	if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0)
5699727c6918SJeff Roberson 		return ((uma_slab_t)(mem + keg->uk_pgoff));
57008b987a77SJeff Roberson 	KEG_LOCK(keg, 0);
57019542ea7bSGleb Smirnoff 	slab = hash_sfind(&keg->uk_hash, mem);
57028b987a77SJeff Roberson 	KEG_UNLOCK(keg, 0);
57039542ea7bSGleb Smirnoff 
57049542ea7bSGleb Smirnoff 	return (slab);
57059542ea7bSGleb Smirnoff }
57069542ea7bSGleb Smirnoff 
5707c5deaf04SGleb Smirnoff static bool
5708c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem)
5709c5deaf04SGleb Smirnoff {
5710c5deaf04SGleb Smirnoff 
5711727c6918SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5712c5deaf04SGleb Smirnoff 		return (true);
5713c5deaf04SGleb Smirnoff 
5714bb15d1c7SGleb Smirnoff 	return (uma_dbg_kskip(zone->uz_keg, mem));
5715c5deaf04SGleb Smirnoff }
5716c5deaf04SGleb Smirnoff 
5717c5deaf04SGleb Smirnoff static bool
5718c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem)
5719c5deaf04SGleb Smirnoff {
5720c5deaf04SGleb Smirnoff 	uintptr_t idx;
5721c5deaf04SGleb Smirnoff 
5722c5deaf04SGleb Smirnoff 	if (dbg_divisor == 0)
5723c5deaf04SGleb Smirnoff 		return (true);
5724c5deaf04SGleb Smirnoff 
5725c5deaf04SGleb Smirnoff 	if (dbg_divisor == 1)
5726c5deaf04SGleb Smirnoff 		return (false);
5727c5deaf04SGleb Smirnoff 
5728c5deaf04SGleb Smirnoff 	idx = (uintptr_t)mem >> PAGE_SHIFT;
5729c5deaf04SGleb Smirnoff 	if (keg->uk_ipers > 1) {
5730c5deaf04SGleb Smirnoff 		idx *= keg->uk_ipers;
5731c5deaf04SGleb Smirnoff 		idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize;
5732c5deaf04SGleb Smirnoff 	}
5733c5deaf04SGleb Smirnoff 
5734c5deaf04SGleb Smirnoff 	if ((idx / dbg_divisor) * dbg_divisor != idx) {
5735c5deaf04SGleb Smirnoff 		counter_u64_add(uma_skip_cnt, 1);
5736c5deaf04SGleb Smirnoff 		return (true);
5737c5deaf04SGleb Smirnoff 	}
5738c5deaf04SGleb Smirnoff 	counter_u64_add(uma_dbg_cnt, 1);
5739c5deaf04SGleb Smirnoff 
5740c5deaf04SGleb Smirnoff 	return (false);
5741c5deaf04SGleb Smirnoff }
5742c5deaf04SGleb Smirnoff 
57439542ea7bSGleb Smirnoff /*
57449542ea7bSGleb Smirnoff  * Set up the slab's freei data such that uma_dbg_free can function.
57459542ea7bSGleb Smirnoff  *
57469542ea7bSGleb Smirnoff  */
57479542ea7bSGleb Smirnoff static void
57489542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
57499542ea7bSGleb Smirnoff {
57509542ea7bSGleb Smirnoff 	uma_keg_t keg;
57519542ea7bSGleb Smirnoff 	int freei;
57529542ea7bSGleb Smirnoff 
57539542ea7bSGleb Smirnoff 	if (slab == NULL) {
57549542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
57559542ea7bSGleb Smirnoff 		if (slab == NULL)
5756952c8964SMark Johnston 			panic("uma: item %p did not belong to zone %s",
57579542ea7bSGleb Smirnoff 			    item, zone->uz_name);
57589542ea7bSGleb Smirnoff 	}
5759584061b4SJeff Roberson 	keg = zone->uz_keg;
57601e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
57619542ea7bSGleb Smirnoff 
5762942951baSRyan Libby 	if (BIT_TEST_SET_ATOMIC(keg->uk_ipers, freei,
5763942951baSRyan Libby 	    slab_dbg_bits(slab, keg)))
5764952c8964SMark Johnston 		panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)",
57659542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
57669542ea7bSGleb Smirnoff }
57679542ea7bSGleb Smirnoff 
57689542ea7bSGleb Smirnoff /*
57699542ea7bSGleb Smirnoff  * Verifies freed addresses.  Checks for alignment, valid slab membership
57709542ea7bSGleb Smirnoff  * and duplicate frees.
57719542ea7bSGleb Smirnoff  *
57729542ea7bSGleb Smirnoff  */
57739542ea7bSGleb Smirnoff static void
57749542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
57759542ea7bSGleb Smirnoff {
57769542ea7bSGleb Smirnoff 	uma_keg_t keg;
57779542ea7bSGleb Smirnoff 	int freei;
57789542ea7bSGleb Smirnoff 
57799542ea7bSGleb Smirnoff 	if (slab == NULL) {
57809542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
57819542ea7bSGleb Smirnoff 		if (slab == NULL)
5782952c8964SMark Johnston 			panic("uma: Freed item %p did not belong to zone %s",
57839542ea7bSGleb Smirnoff 			    item, zone->uz_name);
57849542ea7bSGleb Smirnoff 	}
5785584061b4SJeff Roberson 	keg = zone->uz_keg;
57861e0701e1SJeff Roberson 	freei = slab_item_index(slab, keg, item);
57879542ea7bSGleb Smirnoff 
57889542ea7bSGleb Smirnoff 	if (freei >= keg->uk_ipers)
5789952c8964SMark Johnston 		panic("Invalid free of %p from zone %p(%s) slab %p(%d)",
57909542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
57919542ea7bSGleb Smirnoff 
57921e0701e1SJeff Roberson 	if (slab_item(slab, keg, freei) != item)
5793952c8964SMark Johnston 		panic("Unaligned free of %p from zone %p(%s) slab %p(%d)",
57949542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
57959542ea7bSGleb Smirnoff 
5796942951baSRyan Libby 	if (!BIT_TEST_CLR_ATOMIC(keg->uk_ipers, freei,
5797942951baSRyan Libby 	    slab_dbg_bits(slab, keg)))
5798952c8964SMark Johnston 		panic("Duplicate free of %p from zone %p(%s) slab %p(%d)",
57999542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
58009542ea7bSGleb Smirnoff }
58019542ea7bSGleb Smirnoff #endif /* INVARIANTS */
58029542ea7bSGleb Smirnoff 
580348c5777eSRobert Watson #ifdef DDB
580446d70077SConrad Meyer static int64_t
580546d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used,
58060223790fSConrad Meyer     uint64_t *sleeps, long *cachefree, uint64_t *xdomain)
580748c5777eSRobert Watson {
580846d70077SConrad Meyer 	uint64_t frees;
58090f9b7bf3SMark Johnston 	int i;
581048c5777eSRobert Watson 
581148c5777eSRobert Watson 	if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
581246d70077SConrad Meyer 		*allocs = counter_u64_fetch(z->uz_allocs);
58132efcc8cbSGleb Smirnoff 		frees = counter_u64_fetch(z->uz_frees);
581446d70077SConrad Meyer 		*sleeps = z->uz_sleeps;
581546d70077SConrad Meyer 		*cachefree = 0;
581646d70077SConrad Meyer 		*xdomain = 0;
581748c5777eSRobert Watson 	} else
581846d70077SConrad Meyer 		uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps,
581946d70077SConrad Meyer 		    xdomain);
58208b987a77SJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
5821c6fd3e23SJeff Roberson 		*cachefree += ZDOM_GET(z, i)->uzd_nitems;
5822e20a199fSJeff Roberson 		if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
582348c5777eSRobert Watson 		    (LIST_FIRST(&kz->uk_zones) != z)))
58244ab3aee8SMark Johnston 			*cachefree += kz->uk_domain[i].ud_free_items;
58258b987a77SJeff Roberson 	}
582646d70077SConrad Meyer 	*used = *allocs - frees;
582746d70077SConrad Meyer 	return (((int64_t)*used + *cachefree) * kz->uk_size);
582846d70077SConrad Meyer }
58290f9b7bf3SMark Johnston 
5830c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(uma, db_show_uma, DB_CMD_MEMSAFE)
583146d70077SConrad Meyer {
583246d70077SConrad Meyer 	const char *fmt_hdr, *fmt_entry;
583346d70077SConrad Meyer 	uma_keg_t kz;
583446d70077SConrad Meyer 	uma_zone_t z;
583546d70077SConrad Meyer 	uint64_t allocs, used, sleeps, xdomain;
583646d70077SConrad Meyer 	long cachefree;
583746d70077SConrad Meyer 	/* variables for sorting */
583846d70077SConrad Meyer 	uma_keg_t cur_keg;
583946d70077SConrad Meyer 	uma_zone_t cur_zone, last_zone;
584046d70077SConrad Meyer 	int64_t cur_size, last_size, size;
584146d70077SConrad Meyer 	int ties;
584246d70077SConrad Meyer 
584346d70077SConrad Meyer 	/* /i option produces machine-parseable CSV output */
584446d70077SConrad Meyer 	if (modif[0] == 'i') {
584546d70077SConrad Meyer 		fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n";
584646d70077SConrad Meyer 		fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n";
584746d70077SConrad Meyer 	} else {
584846d70077SConrad Meyer 		fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n";
584946d70077SConrad Meyer 		fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n";
585046d70077SConrad Meyer 	}
585146d70077SConrad Meyer 
585246d70077SConrad Meyer 	db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests",
585346d70077SConrad Meyer 	    "Sleeps", "Bucket", "Total Mem", "XFree");
585446d70077SConrad Meyer 
585546d70077SConrad Meyer 	/* Sort the zones with largest size first. */
585646d70077SConrad Meyer 	last_zone = NULL;
585746d70077SConrad Meyer 	last_size = INT64_MAX;
585846d70077SConrad Meyer 	for (;;) {
585946d70077SConrad Meyer 		cur_zone = NULL;
586046d70077SConrad Meyer 		cur_size = -1;
586146d70077SConrad Meyer 		ties = 0;
586246d70077SConrad Meyer 		LIST_FOREACH(kz, &uma_kegs, uk_link) {
586346d70077SConrad Meyer 			LIST_FOREACH(z, &kz->uk_zones, uz_link) {
586446d70077SConrad Meyer 				/*
586546d70077SConrad Meyer 				 * In the case of size ties, print out zones
586646d70077SConrad Meyer 				 * in the order they are encountered.  That is,
586746d70077SConrad Meyer 				 * when we encounter the most recently output
586846d70077SConrad Meyer 				 * zone, we have already printed all preceding
586946d70077SConrad Meyer 				 * ties, and we must print all following ties.
587046d70077SConrad Meyer 				 */
587146d70077SConrad Meyer 				if (z == last_zone) {
587246d70077SConrad Meyer 					ties = 1;
587346d70077SConrad Meyer 					continue;
587446d70077SConrad Meyer 				}
587546d70077SConrad Meyer 				size = get_uma_stats(kz, z, &allocs, &used,
587646d70077SConrad Meyer 				    &sleeps, &cachefree, &xdomain);
587746d70077SConrad Meyer 				if (size > cur_size && size < last_size + ties)
587846d70077SConrad Meyer 				{
587946d70077SConrad Meyer 					cur_size = size;
588046d70077SConrad Meyer 					cur_zone = z;
588146d70077SConrad Meyer 					cur_keg = kz;
588246d70077SConrad Meyer 				}
588346d70077SConrad Meyer 			}
588446d70077SConrad Meyer 		}
588546d70077SConrad Meyer 		if (cur_zone == NULL)
588646d70077SConrad Meyer 			break;
588746d70077SConrad Meyer 
588846d70077SConrad Meyer 		size = get_uma_stats(cur_keg, cur_zone, &allocs, &used,
588946d70077SConrad Meyer 		    &sleeps, &cachefree, &xdomain);
589046d70077SConrad Meyer 		db_printf(fmt_entry, cur_zone->uz_name,
589146d70077SConrad Meyer 		    (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree,
589246d70077SConrad Meyer 		    (uintmax_t)allocs, (uintmax_t)sleeps,
589320a4e154SJeff Roberson 		    (unsigned)cur_zone->uz_bucket_size, (intmax_t)size,
589420a4e154SJeff Roberson 		    xdomain);
589546d70077SConrad Meyer 
5896687c94aaSJohn Baldwin 		if (db_pager_quit)
5897687c94aaSJohn Baldwin 			return;
589846d70077SConrad Meyer 		last_zone = cur_zone;
589946d70077SConrad Meyer 		last_size = cur_size;
590048c5777eSRobert Watson 	}
590148c5777eSRobert Watson }
590203175483SAlexander Motin 
5903c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(umacache, db_show_umacache, DB_CMD_MEMSAFE)
590403175483SAlexander Motin {
590503175483SAlexander Motin 	uma_zone_t z;
5906ab3185d1SJeff Roberson 	uint64_t allocs, frees;
59070f9b7bf3SMark Johnston 	long cachefree;
59080f9b7bf3SMark Johnston 	int i;
590903175483SAlexander Motin 
591003175483SAlexander Motin 	db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
591103175483SAlexander Motin 	    "Requests", "Bucket");
591203175483SAlexander Motin 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
5913c1685086SJeff Roberson 		uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL);
59140f9b7bf3SMark Johnston 		for (i = 0; i < vm_ndomains; i++)
5915c6fd3e23SJeff Roberson 			cachefree += ZDOM_GET(z, i)->uzd_nitems;
59160f9b7bf3SMark Johnston 		db_printf("%18s %8ju %8jd %8ld %12ju %8u\n",
591703175483SAlexander Motin 		    z->uz_name, (uintmax_t)z->uz_size,
591803175483SAlexander Motin 		    (intmax_t)(allocs - frees), cachefree,
591920a4e154SJeff Roberson 		    (uintmax_t)allocs, z->uz_bucket_size);
592003175483SAlexander Motin 		if (db_pager_quit)
592103175483SAlexander Motin 			return;
592203175483SAlexander Motin 	}
592303175483SAlexander Motin }
59249542ea7bSGleb Smirnoff #endif	/* DDB */
5925