xref: /freebsd/sys/vm/uma_core.c (revision d74e6a1d27b4d8e779ef5e2b456fd557934be54b)
160727d8bSWarner Losh /*-
2ef72505eSJeff Roberson  * Copyright (c) 2002-2005, 2009, 2013 Jeffrey Roberson <jeff@FreeBSD.org>
308ecce74SRobert Watson  * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
4ae4e9636SRobert Watson  * Copyright (c) 2004-2006 Robert N. M. Watson
508ecce74SRobert Watson  * All rights reserved.
68355f576SJeff Roberson  *
78355f576SJeff Roberson  * Redistribution and use in source and binary forms, with or without
88355f576SJeff Roberson  * modification, are permitted provided that the following conditions
98355f576SJeff Roberson  * are met:
108355f576SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
118355f576SJeff Roberson  *    notice unmodified, this list of conditions, and the following
128355f576SJeff Roberson  *    disclaimer.
138355f576SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
148355f576SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
158355f576SJeff Roberson  *    documentation and/or other materials provided with the distribution.
168355f576SJeff Roberson  *
178355f576SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
188355f576SJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
198355f576SJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
208355f576SJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
218355f576SJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
228355f576SJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238355f576SJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
248355f576SJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
258355f576SJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
268355f576SJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278355f576SJeff Roberson  */
288355f576SJeff Roberson 
298355f576SJeff Roberson /*
308355f576SJeff Roberson  * uma_core.c  Implementation of the Universal Memory allocator
318355f576SJeff Roberson  *
328355f576SJeff Roberson  * This allocator is intended to replace the multitude of similar object caches
338355f576SJeff Roberson  * in the standard FreeBSD kernel.  The intent is to be flexible as well as
348355f576SJeff Roberson  * effecient.  A primary design goal is to return unused memory to the rest of
358355f576SJeff Roberson  * the system.  This will make the system as a whole more flexible due to the
368355f576SJeff Roberson  * ability to move memory to subsystems which most need it instead of leaving
378355f576SJeff Roberson  * pools of reserved memory unused.
388355f576SJeff Roberson  *
398355f576SJeff Roberson  * The basic ideas stem from similar slab/zone based allocators whose algorithms
408355f576SJeff Roberson  * are well known.
418355f576SJeff Roberson  *
428355f576SJeff Roberson  */
438355f576SJeff Roberson 
448355f576SJeff Roberson /*
458355f576SJeff Roberson  * TODO:
468355f576SJeff Roberson  *	- Improve memory usage for large allocations
478355f576SJeff Roberson  *	- Investigate cache size adjustments
488355f576SJeff Roberson  */
498355f576SJeff Roberson 
50874651b1SDavid E. O'Brien #include <sys/cdefs.h>
51874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
52874651b1SDavid E. O'Brien 
538355f576SJeff Roberson /* I should really use ktr.. */
548355f576SJeff Roberson /*
558355f576SJeff Roberson #define UMA_DEBUG 1
568355f576SJeff Roberson #define UMA_DEBUG_ALLOC 1
578355f576SJeff Roberson #define UMA_DEBUG_ALLOC_1 1
588355f576SJeff Roberson */
598355f576SJeff Roberson 
6048c5777eSRobert Watson #include "opt_ddb.h"
618355f576SJeff Roberson #include "opt_param.h"
628d689e04SGleb Smirnoff #include "opt_vm.h"
6348c5777eSRobert Watson 
648355f576SJeff Roberson #include <sys/param.h>
658355f576SJeff Roberson #include <sys/systm.h>
66ef72505eSJeff Roberson #include <sys/bitset.h>
678355f576SJeff Roberson #include <sys/kernel.h>
688355f576SJeff Roberson #include <sys/types.h>
698355f576SJeff Roberson #include <sys/queue.h>
708355f576SJeff Roberson #include <sys/malloc.h>
713659f747SRobert Watson #include <sys/ktr.h>
728355f576SJeff Roberson #include <sys/lock.h>
738355f576SJeff Roberson #include <sys/sysctl.h>
748355f576SJeff Roberson #include <sys/mutex.h>
754c1cc01cSJohn Baldwin #include <sys/proc.h>
7610cb2424SMark Murray #include <sys/random.h>
7789f6b863SAttilio Rao #include <sys/rwlock.h>
787a52a97eSRobert Watson #include <sys/sbuf.h>
79a2de44abSAlexander Motin #include <sys/sched.h>
808355f576SJeff Roberson #include <sys/smp.h>
8186bbae32SJeff Roberson #include <sys/vmmeter.h>
8286bbae32SJeff Roberson 
838355f576SJeff Roberson #include <vm/vm.h>
848355f576SJeff Roberson #include <vm/vm_object.h>
858355f576SJeff Roberson #include <vm/vm_page.h>
86a4915c21SAttilio Rao #include <vm/vm_pageout.h>
878355f576SJeff Roberson #include <vm/vm_param.h>
888355f576SJeff Roberson #include <vm/vm_map.h>
898355f576SJeff Roberson #include <vm/vm_kern.h>
908355f576SJeff Roberson #include <vm/vm_extern.h>
918355f576SJeff Roberson #include <vm/uma.h>
928355f576SJeff Roberson #include <vm/uma_int.h>
93639c9550SJeff Roberson #include <vm/uma_dbg.h>
948355f576SJeff Roberson 
9548c5777eSRobert Watson #include <ddb/ddb.h>
9648c5777eSRobert Watson 
978d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
988d689e04SGleb Smirnoff #include <vm/memguard.h>
998d689e04SGleb Smirnoff #endif
1008d689e04SGleb Smirnoff 
1018355f576SJeff Roberson /*
102099a0e58SBosko Milekic  * This is the zone and keg from which all zones are spawned.  The idea is that
103099a0e58SBosko Milekic  * even the zone & keg heads are allocated from the allocator, so we use the
104099a0e58SBosko Milekic  * bss section to bootstrap us.
1058355f576SJeff Roberson  */
106099a0e58SBosko Milekic static struct uma_keg masterkeg;
107099a0e58SBosko Milekic static struct uma_zone masterzone_k;
108099a0e58SBosko Milekic static struct uma_zone masterzone_z;
109099a0e58SBosko Milekic static uma_zone_t kegs = &masterzone_k;
110099a0e58SBosko Milekic static uma_zone_t zones = &masterzone_z;
1118355f576SJeff Roberson 
1128355f576SJeff Roberson /* This is the zone from which all of uma_slab_t's are allocated. */
1138355f576SJeff Roberson static uma_zone_t slabzone;
114099a0e58SBosko Milekic static uma_zone_t slabrefzone;	/* With refcounters (for UMA_ZONE_REFCNT) */
1158355f576SJeff Roberson 
1168355f576SJeff Roberson /*
1178355f576SJeff Roberson  * The initial hash tables come out of this zone so they can be allocated
1188355f576SJeff Roberson  * prior to malloc coming up.
1198355f576SJeff Roberson  */
1208355f576SJeff Roberson static uma_zone_t hashzone;
1218355f576SJeff Roberson 
1221e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */
123e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1;
1241e319f6dSRobert Watson 
125961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
126961647dfSJeff Roberson 
1278355f576SJeff Roberson /*
12886bbae32SJeff Roberson  * Are we allowed to allocate buckets?
12986bbae32SJeff Roberson  */
13086bbae32SJeff Roberson static int bucketdisable = 1;
13186bbae32SJeff Roberson 
132099a0e58SBosko Milekic /* Linked list of all kegs in the system */
13313e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
1348355f576SJeff Roberson 
13503175483SAlexander Motin /* Linked list of all cache-only zones in the system */
13603175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones =
13703175483SAlexander Motin     LIST_HEAD_INITIALIZER(uma_cachezones);
13803175483SAlexander Motin 
139111fbcd5SBryan Venteicher /* This RW lock protects the keg list */
140111fbcd5SBryan Venteicher static struct rwlock_padalign uma_rwlock;
1418355f576SJeff Roberson 
1428355f576SJeff Roberson /* Linked list of boot time pages */
1438355f576SJeff Roberson static LIST_HEAD(,uma_slab) uma_boot_pages =
14413e403fdSAntoine Brodin     LIST_HEAD_INITIALIZER(uma_boot_pages);
1458355f576SJeff Roberson 
146f353d338SAlan Cox /* This mutex protects the boot time pages list */
1470095a784SJeff Roberson static struct mtx_padalign uma_boot_pages_mtx;
1488355f576SJeff Roberson 
14995c4bf75SKonstantin Belousov static struct sx uma_drain_lock;
15095c4bf75SKonstantin Belousov 
1518355f576SJeff Roberson /* Is the VM done starting up? */
1528355f576SJeff Roberson static int booted = 0;
153342f1793SAlan Cox #define	UMA_STARTUP	1
154342f1793SAlan Cox #define	UMA_STARTUP2	2
1558355f576SJeff Roberson 
156ef72505eSJeff Roberson /*
157ef72505eSJeff Roberson  * Only mbuf clusters use ref zones.  Just provide enough references
158ef72505eSJeff Roberson  * to support the one user.  New code should not use the ref facility.
159ef72505eSJeff Roberson  */
160ef72505eSJeff Roberson static const u_int uma_max_ipers_ref = PAGE_SIZE / MCLBYTES;
161244f4554SBosko Milekic 
1629643769aSJeff Roberson /*
1639643769aSJeff Roberson  * This is the handle used to schedule events that need to happen
1649643769aSJeff Roberson  * outside of the allocation fast path.
1659643769aSJeff Roberson  */
1668355f576SJeff Roberson static struct callout uma_callout;
1679643769aSJeff Roberson #define	UMA_TIMEOUT	20		/* Seconds for callout interval. */
1688355f576SJeff Roberson 
1698355f576SJeff Roberson /*
1708355f576SJeff Roberson  * This structure is passed as the zone ctor arg so that I don't have to create
1718355f576SJeff Roberson  * a special allocation function just for zones.
1728355f576SJeff Roberson  */
1738355f576SJeff Roberson struct uma_zctor_args {
174bb196eb4SMatthew D Fleming 	const char *name;
175c3bdc05fSAndrew R. Reiter 	size_t size;
1768355f576SJeff Roberson 	uma_ctor ctor;
1778355f576SJeff Roberson 	uma_dtor dtor;
1788355f576SJeff Roberson 	uma_init uminit;
1798355f576SJeff Roberson 	uma_fini fini;
1800095a784SJeff Roberson 	uma_import import;
1810095a784SJeff Roberson 	uma_release release;
1820095a784SJeff Roberson 	void *arg;
183099a0e58SBosko Milekic 	uma_keg_t keg;
184099a0e58SBosko Milekic 	int align;
18585dcf349SGleb Smirnoff 	uint32_t flags;
186099a0e58SBosko Milekic };
187099a0e58SBosko Milekic 
188099a0e58SBosko Milekic struct uma_kctor_args {
189099a0e58SBosko Milekic 	uma_zone_t zone;
190099a0e58SBosko Milekic 	size_t size;
191099a0e58SBosko Milekic 	uma_init uminit;
192099a0e58SBosko Milekic 	uma_fini fini;
1938355f576SJeff Roberson 	int align;
19485dcf349SGleb Smirnoff 	uint32_t flags;
1958355f576SJeff Roberson };
1968355f576SJeff Roberson 
197cae33c14SJeff Roberson struct uma_bucket_zone {
198cae33c14SJeff Roberson 	uma_zone_t	ubz_zone;
199cae33c14SJeff Roberson 	char		*ubz_name;
200fc03d22bSJeff Roberson 	int		ubz_entries;	/* Number of items it can hold. */
201fc03d22bSJeff Roberson 	int		ubz_maxsize;	/* Maximum allocation size per-item. */
202cae33c14SJeff Roberson };
203cae33c14SJeff Roberson 
204f9d27e75SRobert Watson /*
205fc03d22bSJeff Roberson  * Compute the actual number of bucket entries to pack them in power
206fc03d22bSJeff Roberson  * of two sizes for more efficient space utilization.
207f9d27e75SRobert Watson  */
208fc03d22bSJeff Roberson #define	BUCKET_SIZE(n)						\
209fc03d22bSJeff Roberson     (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *))
210fc03d22bSJeff Roberson 
2111aa6c758SAlexander Motin #define	BUCKET_MAX	BUCKET_SIZE(256)
212fc03d22bSJeff Roberson 
213fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = {
2146fd34d6fSJeff Roberson 	{ NULL, "4 Bucket", BUCKET_SIZE(4), 4096 },
215f3932e90SAlexander Motin 	{ NULL, "6 Bucket", BUCKET_SIZE(6), 3072 },
2166fd34d6fSJeff Roberson 	{ NULL, "8 Bucket", BUCKET_SIZE(8), 2048 },
217f3932e90SAlexander Motin 	{ NULL, "12 Bucket", BUCKET_SIZE(12), 1536 },
2186fd34d6fSJeff Roberson 	{ NULL, "16 Bucket", BUCKET_SIZE(16), 1024 },
219fc03d22bSJeff Roberson 	{ NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
220fc03d22bSJeff Roberson 	{ NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
221fc03d22bSJeff Roberson 	{ NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
2221aa6c758SAlexander Motin 	{ NULL, "256 Bucket", BUCKET_SIZE(256), 64 },
223fc03d22bSJeff Roberson 	{ NULL, NULL, 0}
224fc03d22bSJeff Roberson };
225cae33c14SJeff Roberson 
2262019094aSRobert Watson /*
2272019094aSRobert Watson  * Flags and enumerations to be passed to internal functions.
2282019094aSRobert Watson  */
229ef72505eSJeff Roberson enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI };
230b23f72e9SBrian Feldman 
2318355f576SJeff Roberson /* Prototypes.. */
2328355f576SJeff Roberson 
233f2c2231eSRyan Stone static void *noobj_alloc(uma_zone_t, vm_size_t, uint8_t *, int);
234f2c2231eSRyan Stone static void *page_alloc(uma_zone_t, vm_size_t, uint8_t *, int);
235f2c2231eSRyan Stone static void *startup_alloc(uma_zone_t, vm_size_t, uint8_t *, int);
236f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t);
237e20a199fSJeff Roberson static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int);
2389643769aSJeff Roberson static void cache_drain(uma_zone_t);
2398355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t);
240aaa8bb16SJeff Roberson static void bucket_cache_drain(uma_zone_t zone);
241b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int);
242099a0e58SBosko Milekic static void keg_dtor(void *, int, void *);
243b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int);
2449c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *);
245b23f72e9SBrian Feldman static int zero_init(void *, int, int);
246e20a199fSJeff Roberson static void keg_small_init(uma_keg_t keg);
247e20a199fSJeff Roberson static void keg_large_init(uma_keg_t keg);
2488355f576SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t));
2498355f576SJeff Roberson static void zone_timeout(uma_zone_t zone);
2500aef6126SJeff Roberson static int hash_alloc(struct uma_hash *);
2510aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *);
2520aef6126SJeff Roberson static void hash_free(struct uma_hash *hash);
2538355f576SJeff Roberson static void uma_timeout(void *);
2548355f576SJeff Roberson static void uma_startup3(void);
255e20a199fSJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int);
2560095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
25786bbae32SJeff Roberson static void bucket_enable(void);
258cae33c14SJeff Roberson static void bucket_init(void);
2596fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int);
2606fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *);
261cae33c14SJeff Roberson static void bucket_zone_drain(void);
2626fd34d6fSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t zone, void *, int flags);
263e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags);
264e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags);
2650095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
2660095a784SJeff Roberson static void slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item);
267e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
26885dcf349SGleb Smirnoff     uma_fini fini, int align, uint32_t flags);
2690095a784SJeff Roberson static int zone_import(uma_zone_t zone, void **bucket, int max, int flags);
2700095a784SJeff Roberson static void zone_release(uma_zone_t zone, void **bucket, int cnt);
27148343a2fSGleb Smirnoff static void uma_zero_item(void *item, uma_zone_t zone);
272bbee39c6SJeff Roberson 
2738355f576SJeff Roberson void uma_print_zone(uma_zone_t);
2748355f576SJeff Roberson void uma_print_stats(void);
2757a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
2767a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
2778355f576SJeff Roberson 
2788355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
2798355f576SJeff Roberson 
2807a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
2817a52a97eSRobert Watson     0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
2827a52a97eSRobert Watson 
2837a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
2847a52a97eSRobert Watson     0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
2857a52a97eSRobert Watson 
2862f891cd5SPawel Jakub Dawidek static int zone_warnings = 1;
287af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0,
2882f891cd5SPawel Jakub Dawidek     "Warn when UMA zones becomes full");
2892f891cd5SPawel Jakub Dawidek 
29086bbae32SJeff Roberson /*
29186bbae32SJeff Roberson  * This routine checks to see whether or not it's safe to enable buckets.
29286bbae32SJeff Roberson  */
29386bbae32SJeff Roberson static void
29486bbae32SJeff Roberson bucket_enable(void)
29586bbae32SJeff Roberson {
296251386b4SMaksim Yevmenkin 	bucketdisable = vm_page_count_min();
29786bbae32SJeff Roberson }
29886bbae32SJeff Roberson 
299dc2c7965SRobert Watson /*
300dc2c7965SRobert Watson  * Initialize bucket_zones, the array of zones of buckets of various sizes.
301dc2c7965SRobert Watson  *
302dc2c7965SRobert Watson  * For each zone, calculate the memory required for each bucket, consisting
303fc03d22bSJeff Roberson  * of the header and an array of pointers.
304dc2c7965SRobert Watson  */
305cae33c14SJeff Roberson static void
306cae33c14SJeff Roberson bucket_init(void)
307cae33c14SJeff Roberson {
308cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
309cae33c14SJeff Roberson 	int size;
310cae33c14SJeff Roberson 
311*d74e6a1dSAlan Cox 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
312cae33c14SJeff Roberson 		size = roundup(sizeof(struct uma_bucket), sizeof(void *));
313cae33c14SJeff Roberson 		size += sizeof(void *) * ubz->ubz_entries;
314cae33c14SJeff Roberson 		ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
315e20a199fSJeff Roberson 		    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
3166fd34d6fSJeff Roberson 		    UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET);
317cae33c14SJeff Roberson 	}
318cae33c14SJeff Roberson }
319cae33c14SJeff Roberson 
320dc2c7965SRobert Watson /*
321dc2c7965SRobert Watson  * Given a desired number of entries for a bucket, return the zone from which
322dc2c7965SRobert Watson  * to allocate the bucket.
323dc2c7965SRobert Watson  */
324dc2c7965SRobert Watson static struct uma_bucket_zone *
325dc2c7965SRobert Watson bucket_zone_lookup(int entries)
326dc2c7965SRobert Watson {
327fc03d22bSJeff Roberson 	struct uma_bucket_zone *ubz;
328dc2c7965SRobert Watson 
329fc03d22bSJeff Roberson 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
330fc03d22bSJeff Roberson 		if (ubz->ubz_entries >= entries)
331fc03d22bSJeff Roberson 			return (ubz);
332fc03d22bSJeff Roberson 	ubz--;
333fc03d22bSJeff Roberson 	return (ubz);
334fc03d22bSJeff Roberson }
335fc03d22bSJeff Roberson 
336fc03d22bSJeff Roberson static int
337fc03d22bSJeff Roberson bucket_select(int size)
338fc03d22bSJeff Roberson {
339fc03d22bSJeff Roberson 	struct uma_bucket_zone *ubz;
340fc03d22bSJeff Roberson 
341fc03d22bSJeff Roberson 	ubz = &bucket_zones[0];
342fc03d22bSJeff Roberson 	if (size > ubz->ubz_maxsize)
343fc03d22bSJeff Roberson 		return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
344fc03d22bSJeff Roberson 
345fc03d22bSJeff Roberson 	for (; ubz->ubz_entries != 0; ubz++)
346fc03d22bSJeff Roberson 		if (ubz->ubz_maxsize < size)
347fc03d22bSJeff Roberson 			break;
348fc03d22bSJeff Roberson 	ubz--;
349fc03d22bSJeff Roberson 	return (ubz->ubz_entries);
350dc2c7965SRobert Watson }
351dc2c7965SRobert Watson 
352cae33c14SJeff Roberson static uma_bucket_t
3536fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags)
354cae33c14SJeff Roberson {
355cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
356cae33c14SJeff Roberson 	uma_bucket_t bucket;
357cae33c14SJeff Roberson 
358cae33c14SJeff Roberson 	/*
359cae33c14SJeff Roberson 	 * This is to stop us from allocating per cpu buckets while we're
3603803b26bSDag-Erling Smørgrav 	 * running out of vm.boot_pages.  Otherwise, we would exhaust the
361cae33c14SJeff Roberson 	 * boot pages.  This also prevents us from allocating buckets in
362cae33c14SJeff Roberson 	 * low memory situations.
363cae33c14SJeff Roberson 	 */
364cae33c14SJeff Roberson 	if (bucketdisable)
365cae33c14SJeff Roberson 		return (NULL);
3666fd34d6fSJeff Roberson 	/*
3676fd34d6fSJeff Roberson 	 * To limit bucket recursion we store the original zone flags
3686fd34d6fSJeff Roberson 	 * in a cookie passed via zalloc_arg/zfree_arg.  This allows the
3696fd34d6fSJeff Roberson 	 * NOVM flag to persist even through deep recursions.  We also
3706fd34d6fSJeff Roberson 	 * store ZFLAG_BUCKET once we have recursed attempting to allocate
3716fd34d6fSJeff Roberson 	 * a bucket for a bucket zone so we do not allow infinite bucket
3726fd34d6fSJeff Roberson 	 * recursion.  This cookie will even persist to frees of unused
3736fd34d6fSJeff Roberson 	 * buckets via the allocation path or bucket allocations in the
3746fd34d6fSJeff Roberson 	 * free path.
3756fd34d6fSJeff Roberson 	 */
3766fd34d6fSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
3776fd34d6fSJeff Roberson 		udata = (void *)(uintptr_t)zone->uz_flags;
378e8a720feSAlexander Motin 	else {
379e8a720feSAlexander Motin 		if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
380e8a720feSAlexander Motin 			return (NULL);
3816fd34d6fSJeff Roberson 		udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
382e8a720feSAlexander Motin 	}
3836fd34d6fSJeff Roberson 	if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY)
384af526374SJeff Roberson 		flags |= M_NOVM;
385af526374SJeff Roberson 	ubz = bucket_zone_lookup(zone->uz_count);
38620d3ab87SAlexander Motin 	if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0)
38720d3ab87SAlexander Motin 		ubz++;
3886fd34d6fSJeff Roberson 	bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
389cae33c14SJeff Roberson 	if (bucket) {
390cae33c14SJeff Roberson #ifdef INVARIANTS
391cae33c14SJeff Roberson 		bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
392cae33c14SJeff Roberson #endif
393cae33c14SJeff Roberson 		bucket->ub_cnt = 0;
394cae33c14SJeff Roberson 		bucket->ub_entries = ubz->ubz_entries;
395cae33c14SJeff Roberson 	}
396cae33c14SJeff Roberson 
397cae33c14SJeff Roberson 	return (bucket);
398cae33c14SJeff Roberson }
399cae33c14SJeff Roberson 
400cae33c14SJeff Roberson static void
4016fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
402cae33c14SJeff Roberson {
403cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
404cae33c14SJeff Roberson 
405fc03d22bSJeff Roberson 	KASSERT(bucket->ub_cnt == 0,
406fc03d22bSJeff Roberson 	    ("bucket_free: Freeing a non free bucket."));
4076fd34d6fSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
4086fd34d6fSJeff Roberson 		udata = (void *)(uintptr_t)zone->uz_flags;
409dc2c7965SRobert Watson 	ubz = bucket_zone_lookup(bucket->ub_entries);
4106fd34d6fSJeff Roberson 	uma_zfree_arg(ubz->ubz_zone, bucket, udata);
411cae33c14SJeff Roberson }
412cae33c14SJeff Roberson 
413cae33c14SJeff Roberson static void
414cae33c14SJeff Roberson bucket_zone_drain(void)
415cae33c14SJeff Roberson {
416cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
417cae33c14SJeff Roberson 
418cae33c14SJeff Roberson 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
419cae33c14SJeff Roberson 		zone_drain(ubz->ubz_zone);
420cae33c14SJeff Roberson }
421cae33c14SJeff Roberson 
4222f891cd5SPawel Jakub Dawidek static void
4232f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone)
4242f891cd5SPawel Jakub Dawidek {
4252f891cd5SPawel Jakub Dawidek 	static const struct timeval warninterval = { 300, 0 };
4262f891cd5SPawel Jakub Dawidek 
4272f891cd5SPawel Jakub Dawidek 	if (!zone_warnings || zone->uz_warning == NULL)
4282f891cd5SPawel Jakub Dawidek 		return;
4292f891cd5SPawel Jakub Dawidek 
4302f891cd5SPawel Jakub Dawidek 	if (ratecheck(&zone->uz_ratecheck, &warninterval))
4312f891cd5SPawel Jakub Dawidek 		printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
4322f891cd5SPawel Jakub Dawidek }
4332f891cd5SPawel Jakub Dawidek 
434e20a199fSJeff Roberson static void
435e20a199fSJeff Roberson zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
436e20a199fSJeff Roberson {
437e20a199fSJeff Roberson 	uma_klink_t klink;
438e20a199fSJeff Roberson 
439e20a199fSJeff Roberson 	LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
440e20a199fSJeff Roberson 		kegfn(klink->kl_keg);
441e20a199fSJeff Roberson }
4428355f576SJeff Roberson 
4438355f576SJeff Roberson /*
4448355f576SJeff Roberson  * Routine called by timeout which is used to fire off some time interval
4459643769aSJeff Roberson  * based calculations.  (stats, hash size, etc.)
4468355f576SJeff Roberson  *
4478355f576SJeff Roberson  * Arguments:
4488355f576SJeff Roberson  *	arg   Unused
4498355f576SJeff Roberson  *
4508355f576SJeff Roberson  * Returns:
4518355f576SJeff Roberson  *	Nothing
4528355f576SJeff Roberson  */
4538355f576SJeff Roberson static void
4548355f576SJeff Roberson uma_timeout(void *unused)
4558355f576SJeff Roberson {
45686bbae32SJeff Roberson 	bucket_enable();
4578355f576SJeff Roberson 	zone_foreach(zone_timeout);
4588355f576SJeff Roberson 
4598355f576SJeff Roberson 	/* Reschedule this event */
4609643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
4618355f576SJeff Roberson }
4628355f576SJeff Roberson 
4638355f576SJeff Roberson /*
4649643769aSJeff Roberson  * Routine to perform timeout driven calculations.  This expands the
4659643769aSJeff Roberson  * hashes and does per cpu statistics aggregation.
4668355f576SJeff Roberson  *
467e20a199fSJeff Roberson  *  Returns nothing.
4688355f576SJeff Roberson  */
4698355f576SJeff Roberson static void
470e20a199fSJeff Roberson keg_timeout(uma_keg_t keg)
4718355f576SJeff Roberson {
4728355f576SJeff Roberson 
473e20a199fSJeff Roberson 	KEG_LOCK(keg);
4748355f576SJeff Roberson 	/*
475e20a199fSJeff Roberson 	 * Expand the keg hash table.
4768355f576SJeff Roberson 	 *
4778355f576SJeff Roberson 	 * This is done if the number of slabs is larger than the hash size.
4788355f576SJeff Roberson 	 * What I'm trying to do here is completely reduce collisions.  This
4798355f576SJeff Roberson 	 * may be a little aggressive.  Should I allow for two collisions max?
4808355f576SJeff Roberson 	 */
481099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_HASH &&
482099a0e58SBosko Milekic 	    keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
4830aef6126SJeff Roberson 		struct uma_hash newhash;
4840aef6126SJeff Roberson 		struct uma_hash oldhash;
4850aef6126SJeff Roberson 		int ret;
4865300d9ddSJeff Roberson 
4870aef6126SJeff Roberson 		/*
4880aef6126SJeff Roberson 		 * This is so involved because allocating and freeing
489e20a199fSJeff Roberson 		 * while the keg lock is held will lead to deadlock.
4900aef6126SJeff Roberson 		 * I have to do everything in stages and check for
4910aef6126SJeff Roberson 		 * races.
4920aef6126SJeff Roberson 		 */
493099a0e58SBosko Milekic 		newhash = keg->uk_hash;
494e20a199fSJeff Roberson 		KEG_UNLOCK(keg);
4950aef6126SJeff Roberson 		ret = hash_alloc(&newhash);
496e20a199fSJeff Roberson 		KEG_LOCK(keg);
4970aef6126SJeff Roberson 		if (ret) {
498099a0e58SBosko Milekic 			if (hash_expand(&keg->uk_hash, &newhash)) {
499099a0e58SBosko Milekic 				oldhash = keg->uk_hash;
500099a0e58SBosko Milekic 				keg->uk_hash = newhash;
5010aef6126SJeff Roberson 			} else
5020aef6126SJeff Roberson 				oldhash = newhash;
5030aef6126SJeff Roberson 
504e20a199fSJeff Roberson 			KEG_UNLOCK(keg);
5050aef6126SJeff Roberson 			hash_free(&oldhash);
506a1dff920SDavide Italiano 			return;
5070aef6126SJeff Roberson 		}
5085300d9ddSJeff Roberson 	}
509e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
510e20a199fSJeff Roberson }
511e20a199fSJeff Roberson 
512e20a199fSJeff Roberson static void
513e20a199fSJeff Roberson zone_timeout(uma_zone_t zone)
514e20a199fSJeff Roberson {
515e20a199fSJeff Roberson 
516e20a199fSJeff Roberson 	zone_foreach_keg(zone, &keg_timeout);
5178355f576SJeff Roberson }
5188355f576SJeff Roberson 
5198355f576SJeff Roberson /*
5205300d9ddSJeff Roberson  * Allocate and zero fill the next sized hash table from the appropriate
5215300d9ddSJeff Roberson  * backing store.
5225300d9ddSJeff Roberson  *
5235300d9ddSJeff Roberson  * Arguments:
5240aef6126SJeff Roberson  *	hash  A new hash structure with the old hash size in uh_hashsize
5255300d9ddSJeff Roberson  *
5265300d9ddSJeff Roberson  * Returns:
5270aef6126SJeff Roberson  *	1 on sucess and 0 on failure.
5285300d9ddSJeff Roberson  */
52937c84183SPoul-Henning Kamp static int
5300aef6126SJeff Roberson hash_alloc(struct uma_hash *hash)
5315300d9ddSJeff Roberson {
5320aef6126SJeff Roberson 	int oldsize;
5335300d9ddSJeff Roberson 	int alloc;
5345300d9ddSJeff Roberson 
5350aef6126SJeff Roberson 	oldsize = hash->uh_hashsize;
5360aef6126SJeff Roberson 
5375300d9ddSJeff Roberson 	/* We're just going to go to a power of two greater */
5380aef6126SJeff Roberson 	if (oldsize)  {
5390aef6126SJeff Roberson 		hash->uh_hashsize = oldsize * 2;
5400aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
5410aef6126SJeff Roberson 		hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
542961647dfSJeff Roberson 		    M_UMAHASH, M_NOWAIT);
5435300d9ddSJeff Roberson 	} else {
5440aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
545e20a199fSJeff Roberson 		hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
546a163d034SWarner Losh 		    M_WAITOK);
5470aef6126SJeff Roberson 		hash->uh_hashsize = UMA_HASH_SIZE_INIT;
5485300d9ddSJeff Roberson 	}
5490aef6126SJeff Roberson 	if (hash->uh_slab_hash) {
5500aef6126SJeff Roberson 		bzero(hash->uh_slab_hash, alloc);
5510aef6126SJeff Roberson 		hash->uh_hashmask = hash->uh_hashsize - 1;
5520aef6126SJeff Roberson 		return (1);
5530aef6126SJeff Roberson 	}
5545300d9ddSJeff Roberson 
5550aef6126SJeff Roberson 	return (0);
5565300d9ddSJeff Roberson }
5575300d9ddSJeff Roberson 
5585300d9ddSJeff Roberson /*
55964f051e9SJeff Roberson  * Expands the hash table for HASH zones.  This is done from zone_timeout
56064f051e9SJeff Roberson  * to reduce collisions.  This must not be done in the regular allocation
56164f051e9SJeff Roberson  * path, otherwise, we can recurse on the vm while allocating pages.
5628355f576SJeff Roberson  *
5638355f576SJeff Roberson  * Arguments:
5640aef6126SJeff Roberson  *	oldhash  The hash you want to expand
5650aef6126SJeff Roberson  *	newhash  The hash structure for the new table
5668355f576SJeff Roberson  *
5678355f576SJeff Roberson  * Returns:
5688355f576SJeff Roberson  *	Nothing
5698355f576SJeff Roberson  *
5708355f576SJeff Roberson  * Discussion:
5718355f576SJeff Roberson  */
5720aef6126SJeff Roberson static int
5730aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
5748355f576SJeff Roberson {
5758355f576SJeff Roberson 	uma_slab_t slab;
5768355f576SJeff Roberson 	int hval;
5778355f576SJeff Roberson 	int i;
5788355f576SJeff Roberson 
5790aef6126SJeff Roberson 	if (!newhash->uh_slab_hash)
5800aef6126SJeff Roberson 		return (0);
5818355f576SJeff Roberson 
5820aef6126SJeff Roberson 	if (oldhash->uh_hashsize >= newhash->uh_hashsize)
5830aef6126SJeff Roberson 		return (0);
5848355f576SJeff Roberson 
5858355f576SJeff Roberson 	/*
5868355f576SJeff Roberson 	 * I need to investigate hash algorithms for resizing without a
5878355f576SJeff Roberson 	 * full rehash.
5888355f576SJeff Roberson 	 */
5898355f576SJeff Roberson 
5900aef6126SJeff Roberson 	for (i = 0; i < oldhash->uh_hashsize; i++)
5910aef6126SJeff Roberson 		while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
5920aef6126SJeff Roberson 			slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
5930aef6126SJeff Roberson 			SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
5940aef6126SJeff Roberson 			hval = UMA_HASH(newhash, slab->us_data);
5950aef6126SJeff Roberson 			SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
5960aef6126SJeff Roberson 			    slab, us_hlink);
5978355f576SJeff Roberson 		}
5988355f576SJeff Roberson 
5990aef6126SJeff Roberson 	return (1);
6009c2cd7e5SJeff Roberson }
6019c2cd7e5SJeff Roberson 
6025300d9ddSJeff Roberson /*
6035300d9ddSJeff Roberson  * Free the hash bucket to the appropriate backing store.
6045300d9ddSJeff Roberson  *
6055300d9ddSJeff Roberson  * Arguments:
6065300d9ddSJeff Roberson  *	slab_hash  The hash bucket we're freeing
6075300d9ddSJeff Roberson  *	hashsize   The number of entries in that hash bucket
6085300d9ddSJeff Roberson  *
6095300d9ddSJeff Roberson  * Returns:
6105300d9ddSJeff Roberson  *	Nothing
6115300d9ddSJeff Roberson  */
6129c2cd7e5SJeff Roberson static void
6130aef6126SJeff Roberson hash_free(struct uma_hash *hash)
6149c2cd7e5SJeff Roberson {
6150aef6126SJeff Roberson 	if (hash->uh_slab_hash == NULL)
6160aef6126SJeff Roberson 		return;
6170aef6126SJeff Roberson 	if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
6180095a784SJeff Roberson 		zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
6198355f576SJeff Roberson 	else
620961647dfSJeff Roberson 		free(hash->uh_slab_hash, M_UMAHASH);
6218355f576SJeff Roberson }
6228355f576SJeff Roberson 
6238355f576SJeff Roberson /*
6248355f576SJeff Roberson  * Frees all outstanding items in a bucket
6258355f576SJeff Roberson  *
6268355f576SJeff Roberson  * Arguments:
6278355f576SJeff Roberson  *	zone   The zone to free to, must be unlocked.
6288355f576SJeff Roberson  *	bucket The free/alloc bucket with items, cpu queue must be locked.
6298355f576SJeff Roberson  *
6308355f576SJeff Roberson  * Returns:
6318355f576SJeff Roberson  *	Nothing
6328355f576SJeff Roberson  */
6338355f576SJeff Roberson 
6348355f576SJeff Roberson static void
6358355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
6368355f576SJeff Roberson {
6370095a784SJeff Roberson 	int i;
6388355f576SJeff Roberson 
6398355f576SJeff Roberson 	if (bucket == NULL)
6408355f576SJeff Roberson 		return;
6418355f576SJeff Roberson 
6420095a784SJeff Roberson 	if (zone->uz_fini)
6430095a784SJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
6440095a784SJeff Roberson 			zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
6450095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
6460095a784SJeff Roberson 	bucket->ub_cnt = 0;
6478355f576SJeff Roberson }
6488355f576SJeff Roberson 
6498355f576SJeff Roberson /*
6508355f576SJeff Roberson  * Drains the per cpu caches for a zone.
6518355f576SJeff Roberson  *
6525d1ae027SRobert Watson  * NOTE: This may only be called while the zone is being turn down, and not
6535d1ae027SRobert Watson  * during normal operation.  This is necessary in order that we do not have
6545d1ae027SRobert Watson  * to migrate CPUs to drain the per-CPU caches.
6555d1ae027SRobert Watson  *
6568355f576SJeff Roberson  * Arguments:
6578355f576SJeff Roberson  *	zone     The zone to drain, must be unlocked.
6588355f576SJeff Roberson  *
6598355f576SJeff Roberson  * Returns:
6608355f576SJeff Roberson  *	Nothing
6618355f576SJeff Roberson  */
6628355f576SJeff Roberson static void
6639643769aSJeff Roberson cache_drain(uma_zone_t zone)
6648355f576SJeff Roberson {
6658355f576SJeff Roberson 	uma_cache_t cache;
6668355f576SJeff Roberson 	int cpu;
6678355f576SJeff Roberson 
6688355f576SJeff Roberson 	/*
6695d1ae027SRobert Watson 	 * XXX: It is safe to not lock the per-CPU caches, because we're
6705d1ae027SRobert Watson 	 * tearing down the zone anyway.  I.e., there will be no further use
6715d1ae027SRobert Watson 	 * of the caches at this point.
6725d1ae027SRobert Watson 	 *
6735d1ae027SRobert Watson 	 * XXX: It would good to be able to assert that the zone is being
6745d1ae027SRobert Watson 	 * torn down to prevent improper use of cache_drain().
6755d1ae027SRobert Watson 	 *
6765d1ae027SRobert Watson 	 * XXX: We lock the zone before passing into bucket_cache_drain() as
6775d1ae027SRobert Watson 	 * it is used elsewhere.  Should the tear-down path be made special
6785d1ae027SRobert Watson 	 * there in some form?
6798355f576SJeff Roberson 	 */
6803aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
6818355f576SJeff Roberson 		cache = &zone->uz_cpu[cpu];
6828355f576SJeff Roberson 		bucket_drain(zone, cache->uc_allocbucket);
6838355f576SJeff Roberson 		bucket_drain(zone, cache->uc_freebucket);
684174ab450SBosko Milekic 		if (cache->uc_allocbucket != NULL)
6856fd34d6fSJeff Roberson 			bucket_free(zone, cache->uc_allocbucket, NULL);
686174ab450SBosko Milekic 		if (cache->uc_freebucket != NULL)
6876fd34d6fSJeff Roberson 			bucket_free(zone, cache->uc_freebucket, NULL);
688d56368d7SBosko Milekic 		cache->uc_allocbucket = cache->uc_freebucket = NULL;
689d56368d7SBosko Milekic 	}
690aaa8bb16SJeff Roberson 	ZONE_LOCK(zone);
691aaa8bb16SJeff Roberson 	bucket_cache_drain(zone);
692aaa8bb16SJeff Roberson 	ZONE_UNLOCK(zone);
693aaa8bb16SJeff Roberson }
694aaa8bb16SJeff Roberson 
695a2de44abSAlexander Motin static void
696a2de44abSAlexander Motin cache_shrink(uma_zone_t zone)
697a2de44abSAlexander Motin {
698a2de44abSAlexander Motin 
699a2de44abSAlexander Motin 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
700a2de44abSAlexander Motin 		return;
701a2de44abSAlexander Motin 
702a2de44abSAlexander Motin 	ZONE_LOCK(zone);
703a2de44abSAlexander Motin 	zone->uz_count = (zone->uz_count_min + zone->uz_count) / 2;
704a2de44abSAlexander Motin 	ZONE_UNLOCK(zone);
705a2de44abSAlexander Motin }
706a2de44abSAlexander Motin 
707a2de44abSAlexander Motin static void
708a2de44abSAlexander Motin cache_drain_safe_cpu(uma_zone_t zone)
709a2de44abSAlexander Motin {
710a2de44abSAlexander Motin 	uma_cache_t cache;
7118a8d9d14SAlexander Motin 	uma_bucket_t b1, b2;
712a2de44abSAlexander Motin 
713a2de44abSAlexander Motin 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
714a2de44abSAlexander Motin 		return;
715a2de44abSAlexander Motin 
7168a8d9d14SAlexander Motin 	b1 = b2 = NULL;
717a2de44abSAlexander Motin 	ZONE_LOCK(zone);
718a2de44abSAlexander Motin 	critical_enter();
719a2de44abSAlexander Motin 	cache = &zone->uz_cpu[curcpu];
720a2de44abSAlexander Motin 	if (cache->uc_allocbucket) {
7218a8d9d14SAlexander Motin 		if (cache->uc_allocbucket->ub_cnt != 0)
7228a8d9d14SAlexander Motin 			LIST_INSERT_HEAD(&zone->uz_buckets,
7238a8d9d14SAlexander Motin 			    cache->uc_allocbucket, ub_link);
7248a8d9d14SAlexander Motin 		else
7258a8d9d14SAlexander Motin 			b1 = cache->uc_allocbucket;
726a2de44abSAlexander Motin 		cache->uc_allocbucket = NULL;
727a2de44abSAlexander Motin 	}
728a2de44abSAlexander Motin 	if (cache->uc_freebucket) {
7298a8d9d14SAlexander Motin 		if (cache->uc_freebucket->ub_cnt != 0)
7308a8d9d14SAlexander Motin 			LIST_INSERT_HEAD(&zone->uz_buckets,
7318a8d9d14SAlexander Motin 			    cache->uc_freebucket, ub_link);
7328a8d9d14SAlexander Motin 		else
7338a8d9d14SAlexander Motin 			b2 = cache->uc_freebucket;
734a2de44abSAlexander Motin 		cache->uc_freebucket = NULL;
735a2de44abSAlexander Motin 	}
736a2de44abSAlexander Motin 	critical_exit();
737a2de44abSAlexander Motin 	ZONE_UNLOCK(zone);
7388a8d9d14SAlexander Motin 	if (b1)
7398a8d9d14SAlexander Motin 		bucket_free(zone, b1, NULL);
7408a8d9d14SAlexander Motin 	if (b2)
7418a8d9d14SAlexander Motin 		bucket_free(zone, b2, NULL);
742a2de44abSAlexander Motin }
743a2de44abSAlexander Motin 
744a2de44abSAlexander Motin /*
745a2de44abSAlexander Motin  * Safely drain per-CPU caches of a zone(s) to alloc bucket.
746a2de44abSAlexander Motin  * This is an expensive call because it needs to bind to all CPUs
747a2de44abSAlexander Motin  * one by one and enter a critical section on each of them in order
748a2de44abSAlexander Motin  * to safely access their cache buckets.
749a2de44abSAlexander Motin  * Zone lock must not be held on call this function.
750a2de44abSAlexander Motin  */
751a2de44abSAlexander Motin static void
752a2de44abSAlexander Motin cache_drain_safe(uma_zone_t zone)
753a2de44abSAlexander Motin {
754a2de44abSAlexander Motin 	int cpu;
755a2de44abSAlexander Motin 
756a2de44abSAlexander Motin 	/*
757a2de44abSAlexander Motin 	 * Polite bucket sizes shrinking was not enouth, shrink aggressively.
758a2de44abSAlexander Motin 	 */
759a2de44abSAlexander Motin 	if (zone)
760a2de44abSAlexander Motin 		cache_shrink(zone);
761a2de44abSAlexander Motin 	else
762a2de44abSAlexander Motin 		zone_foreach(cache_shrink);
763a2de44abSAlexander Motin 
764a2de44abSAlexander Motin 	CPU_FOREACH(cpu) {
765a2de44abSAlexander Motin 		thread_lock(curthread);
766a2de44abSAlexander Motin 		sched_bind(curthread, cpu);
767a2de44abSAlexander Motin 		thread_unlock(curthread);
768a2de44abSAlexander Motin 
769a2de44abSAlexander Motin 		if (zone)
770a2de44abSAlexander Motin 			cache_drain_safe_cpu(zone);
771a2de44abSAlexander Motin 		else
772a2de44abSAlexander Motin 			zone_foreach(cache_drain_safe_cpu);
773a2de44abSAlexander Motin 	}
774a2de44abSAlexander Motin 	thread_lock(curthread);
775a2de44abSAlexander Motin 	sched_unbind(curthread);
776a2de44abSAlexander Motin 	thread_unlock(curthread);
777a2de44abSAlexander Motin }
778a2de44abSAlexander Motin 
779aaa8bb16SJeff Roberson /*
780aaa8bb16SJeff Roberson  * Drain the cached buckets from a zone.  Expects a locked zone on entry.
781aaa8bb16SJeff Roberson  */
782aaa8bb16SJeff Roberson static void
783aaa8bb16SJeff Roberson bucket_cache_drain(uma_zone_t zone)
784aaa8bb16SJeff Roberson {
785aaa8bb16SJeff Roberson 	uma_bucket_t bucket;
7868355f576SJeff Roberson 
7878355f576SJeff Roberson 	/*
7888355f576SJeff Roberson 	 * Drain the bucket queues and free the buckets, we just keep two per
7898355f576SJeff Roberson 	 * cpu (alloc/free).
7908355f576SJeff Roberson 	 */
791fc03d22bSJeff Roberson 	while ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
7928355f576SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
7938355f576SJeff Roberson 		ZONE_UNLOCK(zone);
7948355f576SJeff Roberson 		bucket_drain(zone, bucket);
7956fd34d6fSJeff Roberson 		bucket_free(zone, bucket, NULL);
7968355f576SJeff Roberson 		ZONE_LOCK(zone);
7978355f576SJeff Roberson 	}
798ace66b56SAlexander Motin 
799ace66b56SAlexander Motin 	/*
800ace66b56SAlexander Motin 	 * Shrink further bucket sizes.  Price of single zone lock collision
801ace66b56SAlexander Motin 	 * is probably lower then price of global cache drain.
802ace66b56SAlexander Motin 	 */
803ace66b56SAlexander Motin 	if (zone->uz_count > zone->uz_count_min)
804ace66b56SAlexander Motin 		zone->uz_count--;
8058355f576SJeff Roberson }
806fc03d22bSJeff Roberson 
807fc03d22bSJeff Roberson static void
808fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
809fc03d22bSJeff Roberson {
810fc03d22bSJeff Roberson 	uint8_t *mem;
811fc03d22bSJeff Roberson 	int i;
812fc03d22bSJeff Roberson 	uint8_t flags;
813fc03d22bSJeff Roberson 
814fc03d22bSJeff Roberson 	mem = slab->us_data;
815fc03d22bSJeff Roberson 	flags = slab->us_flags;
816fc03d22bSJeff Roberson 	i = start;
817fc03d22bSJeff Roberson 	if (keg->uk_fini != NULL) {
818fc03d22bSJeff Roberson 		for (i--; i > -1; i--)
819fc03d22bSJeff Roberson 			keg->uk_fini(slab->us_data + (keg->uk_rsize * i),
820fc03d22bSJeff Roberson 			    keg->uk_size);
821fc03d22bSJeff Roberson 	}
822fc03d22bSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_OFFPAGE)
823fc03d22bSJeff Roberson 		zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
824fc03d22bSJeff Roberson #ifdef UMA_DEBUG
825fc03d22bSJeff Roberson 	printf("%s: Returning %d bytes.\n", keg->uk_name,
826fc03d22bSJeff Roberson 	    PAGE_SIZE * keg->uk_ppera);
827fc03d22bSJeff Roberson #endif
828fc03d22bSJeff Roberson 	keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
8298355f576SJeff Roberson }
8308355f576SJeff Roberson 
8318355f576SJeff Roberson /*
832e20a199fSJeff Roberson  * Frees pages from a keg back to the system.  This is done on demand from
8338355f576SJeff Roberson  * the pageout daemon.
8348355f576SJeff Roberson  *
835e20a199fSJeff Roberson  * Returns nothing.
8368355f576SJeff Roberson  */
837e20a199fSJeff Roberson static void
838e20a199fSJeff Roberson keg_drain(uma_keg_t keg)
8398355f576SJeff Roberson {
8401e183df2SStefan Farfeleder 	struct slabhead freeslabs = { 0 };
8418355f576SJeff Roberson 	uma_slab_t slab;
8428355f576SJeff Roberson 	uma_slab_t n;
8438355f576SJeff Roberson 
8448355f576SJeff Roberson 	/*
845e20a199fSJeff Roberson 	 * We don't want to take pages from statically allocated kegs at this
8468355f576SJeff Roberson 	 * time
8478355f576SJeff Roberson 	 */
848099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
8498355f576SJeff Roberson 		return;
8508355f576SJeff Roberson 
8518355f576SJeff Roberson #ifdef UMA_DEBUG
852e20a199fSJeff Roberson 	printf("%s free items: %u\n", keg->uk_name, keg->uk_free);
8538355f576SJeff Roberson #endif
854e20a199fSJeff Roberson 	KEG_LOCK(keg);
855099a0e58SBosko Milekic 	if (keg->uk_free == 0)
8568355f576SJeff Roberson 		goto finished;
8578355f576SJeff Roberson 
858099a0e58SBosko Milekic 	slab = LIST_FIRST(&keg->uk_free_slab);
8599643769aSJeff Roberson 	while (slab) {
8608355f576SJeff Roberson 		n = LIST_NEXT(slab, us_link);
8618355f576SJeff Roberson 
8628355f576SJeff Roberson 		/* We have no where to free these to */
8638355f576SJeff Roberson 		if (slab->us_flags & UMA_SLAB_BOOT) {
8648355f576SJeff Roberson 			slab = n;
8658355f576SJeff Roberson 			continue;
8668355f576SJeff Roberson 		}
8678355f576SJeff Roberson 
8688355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
869099a0e58SBosko Milekic 		keg->uk_pages -= keg->uk_ppera;
870099a0e58SBosko Milekic 		keg->uk_free -= keg->uk_ipers;
871713deb36SJeff Roberson 
872099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZONE_HASH)
873099a0e58SBosko Milekic 			UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
874713deb36SJeff Roberson 
875713deb36SJeff Roberson 		SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
876713deb36SJeff Roberson 
877713deb36SJeff Roberson 		slab = n;
878713deb36SJeff Roberson 	}
879713deb36SJeff Roberson finished:
880e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
881713deb36SJeff Roberson 
882713deb36SJeff Roberson 	while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
883713deb36SJeff Roberson 		SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
8841645995bSKirk McKusick 		keg_free_slab(keg, slab, keg->uk_ipers);
8858355f576SJeff Roberson 	}
8868355f576SJeff Roberson }
8878355f576SJeff Roberson 
888e20a199fSJeff Roberson static void
889e20a199fSJeff Roberson zone_drain_wait(uma_zone_t zone, int waitok)
890e20a199fSJeff Roberson {
891e20a199fSJeff Roberson 
8928355f576SJeff Roberson 	/*
893e20a199fSJeff Roberson 	 * Set draining to interlock with zone_dtor() so we can release our
894e20a199fSJeff Roberson 	 * locks as we go.  Only dtor() should do a WAITOK call since it
895e20a199fSJeff Roberson 	 * is the only call that knows the structure will still be available
896e20a199fSJeff Roberson 	 * when it wakes up.
897e20a199fSJeff Roberson 	 */
898e20a199fSJeff Roberson 	ZONE_LOCK(zone);
899e20a199fSJeff Roberson 	while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
900e20a199fSJeff Roberson 		if (waitok == M_NOWAIT)
901e20a199fSJeff Roberson 			goto out;
902af526374SJeff Roberson 		msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1);
903e20a199fSJeff Roberson 	}
904e20a199fSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_DRAINING;
905e20a199fSJeff Roberson 	bucket_cache_drain(zone);
906e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
907e20a199fSJeff Roberson 	/*
908e20a199fSJeff Roberson 	 * The DRAINING flag protects us from being freed while
909111fbcd5SBryan Venteicher 	 * we're running.  Normally the uma_rwlock would protect us but we
910e20a199fSJeff Roberson 	 * must be able to release and acquire the right lock for each keg.
911e20a199fSJeff Roberson 	 */
912e20a199fSJeff Roberson 	zone_foreach_keg(zone, &keg_drain);
913e20a199fSJeff Roberson 	ZONE_LOCK(zone);
914e20a199fSJeff Roberson 	zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
915e20a199fSJeff Roberson 	wakeup(zone);
916e20a199fSJeff Roberson out:
917e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
918e20a199fSJeff Roberson }
919e20a199fSJeff Roberson 
920e20a199fSJeff Roberson void
921e20a199fSJeff Roberson zone_drain(uma_zone_t zone)
922e20a199fSJeff Roberson {
923e20a199fSJeff Roberson 
924e20a199fSJeff Roberson 	zone_drain_wait(zone, M_NOWAIT);
925e20a199fSJeff Roberson }
926e20a199fSJeff Roberson 
927e20a199fSJeff Roberson /*
928e20a199fSJeff Roberson  * Allocate a new slab for a keg.  This does not insert the slab onto a list.
9298355f576SJeff Roberson  *
9308355f576SJeff Roberson  * Arguments:
9318355f576SJeff Roberson  *	wait  Shall we wait?
9328355f576SJeff Roberson  *
9338355f576SJeff Roberson  * Returns:
9348355f576SJeff Roberson  *	The slab that was allocated or NULL if there is no memory and the
9358355f576SJeff Roberson  *	caller specified M_NOWAIT.
9368355f576SJeff Roberson  */
9378355f576SJeff Roberson static uma_slab_t
938e20a199fSJeff Roberson keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait)
9398355f576SJeff Roberson {
940099a0e58SBosko Milekic 	uma_slabrefcnt_t slabref;
941e20a199fSJeff Roberson 	uma_alloc allocf;
942099a0e58SBosko Milekic 	uma_slab_t slab;
94385dcf349SGleb Smirnoff 	uint8_t *mem;
94485dcf349SGleb Smirnoff 	uint8_t flags;
9458355f576SJeff Roberson 	int i;
9468355f576SJeff Roberson 
947e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
948a553d4b8SJeff Roberson 	slab = NULL;
949fc03d22bSJeff Roberson 	mem = NULL;
950a553d4b8SJeff Roberson 
9518355f576SJeff Roberson #ifdef UMA_DEBUG
9520095a784SJeff Roberson 	printf("alloc_slab:  Allocating a new slab for %s\n", keg->uk_name);
9538355f576SJeff Roberson #endif
954e20a199fSJeff Roberson 	allocf = keg->uk_allocf;
955e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
956a553d4b8SJeff Roberson 
957099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
958e20a199fSJeff Roberson 		slab = zone_alloc_item(keg->uk_slabzone, NULL, wait);
959fc03d22bSJeff Roberson 		if (slab == NULL)
960fc03d22bSJeff Roberson 			goto out;
961a553d4b8SJeff Roberson 	}
962a553d4b8SJeff Roberson 
9633370c5bfSJeff Roberson 	/*
9643370c5bfSJeff Roberson 	 * This reproduces the old vm_zone behavior of zero filling pages the
9653370c5bfSJeff Roberson 	 * first time they are added to a zone.
9663370c5bfSJeff Roberson 	 *
9673370c5bfSJeff Roberson 	 * Malloced items are zeroed in uma_zalloc.
9683370c5bfSJeff Roberson 	 */
9693370c5bfSJeff Roberson 
970099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
9713370c5bfSJeff Roberson 		wait |= M_ZERO;
9723370c5bfSJeff Roberson 	else
9733370c5bfSJeff Roberson 		wait &= ~M_ZERO;
9743370c5bfSJeff Roberson 
975263811f7SKip Macy 	if (keg->uk_flags & UMA_ZONE_NODUMP)
976263811f7SKip Macy 		wait |= M_NODUMP;
977263811f7SKip Macy 
978e20a199fSJeff Roberson 	/* zone is passed for legacy reasons. */
979ad97af7eSGleb Smirnoff 	mem = allocf(zone, keg->uk_ppera * PAGE_SIZE, &flags, wait);
980a553d4b8SJeff Roberson 	if (mem == NULL) {
981b23f72e9SBrian Feldman 		if (keg->uk_flags & UMA_ZONE_OFFPAGE)
9820095a784SJeff Roberson 			zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
983fc03d22bSJeff Roberson 		slab = NULL;
984fc03d22bSJeff Roberson 		goto out;
985a553d4b8SJeff Roberson 	}
9868355f576SJeff Roberson 
9875c0e403bSJeff Roberson 	/* Point the slab into the allocated memory */
988099a0e58SBosko Milekic 	if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
989099a0e58SBosko Milekic 		slab = (uma_slab_t )(mem + keg->uk_pgoff);
9905c0e403bSJeff Roberson 
991e20a199fSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_VTOSLAB)
992099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ppera; i++)
99399571dc3SJeff Roberson 			vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
9948355f576SJeff Roberson 
995099a0e58SBosko Milekic 	slab->us_keg = keg;
9968355f576SJeff Roberson 	slab->us_data = mem;
997099a0e58SBosko Milekic 	slab->us_freecount = keg->uk_ipers;
9988355f576SJeff Roberson 	slab->us_flags = flags;
999ef72505eSJeff Roberson 	BIT_FILL(SLAB_SETSIZE, &slab->us_free);
1000ef72505eSJeff Roberson #ifdef INVARIANTS
1001ef72505eSJeff Roberson 	BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree);
1002ef72505eSJeff Roberson #endif
1003099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_REFCNT) {
1004099a0e58SBosko Milekic 		slabref = (uma_slabrefcnt_t)slab;
1005ab14a3f7SBrian Feldman 		for (i = 0; i < keg->uk_ipers; i++)
1006ef72505eSJeff Roberson 			slabref->us_refcnt[i] = 0;
1007099a0e58SBosko Milekic 	}
1008099a0e58SBosko Milekic 
1009b23f72e9SBrian Feldman 	if (keg->uk_init != NULL) {
1010099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ipers; i++)
1011b23f72e9SBrian Feldman 			if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
1012b23f72e9SBrian Feldman 			    keg->uk_size, wait) != 0)
1013b23f72e9SBrian Feldman 				break;
1014b23f72e9SBrian Feldman 		if (i != keg->uk_ipers) {
1015fc03d22bSJeff Roberson 			keg_free_slab(keg, slab, i);
1016fc03d22bSJeff Roberson 			slab = NULL;
1017fc03d22bSJeff Roberson 			goto out;
1018b23f72e9SBrian Feldman 		}
1019b23f72e9SBrian Feldman 	}
1020fc03d22bSJeff Roberson out:
1021e20a199fSJeff Roberson 	KEG_LOCK(keg);
10225c0e403bSJeff Roberson 
1023fc03d22bSJeff Roberson 	if (slab != NULL) {
1024099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZONE_HASH)
1025099a0e58SBosko Milekic 			UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
10268355f576SJeff Roberson 
1027099a0e58SBosko Milekic 		keg->uk_pages += keg->uk_ppera;
1028099a0e58SBosko Milekic 		keg->uk_free += keg->uk_ipers;
1029fc03d22bSJeff Roberson 	}
10308355f576SJeff Roberson 
10318355f576SJeff Roberson 	return (slab);
10328355f576SJeff Roberson }
10338355f576SJeff Roberson 
10348355f576SJeff Roberson /*
1035009b6fcbSJeff Roberson  * This function is intended to be used early on in place of page_alloc() so
1036009b6fcbSJeff Roberson  * that we may use the boot time page cache to satisfy allocations before
1037009b6fcbSJeff Roberson  * the VM is ready.
1038009b6fcbSJeff Roberson  */
1039009b6fcbSJeff Roberson static void *
1040f2c2231eSRyan Stone startup_alloc(uma_zone_t zone, vm_size_t bytes, uint8_t *pflag, int wait)
1041009b6fcbSJeff Roberson {
1042099a0e58SBosko Milekic 	uma_keg_t keg;
1043f353d338SAlan Cox 	uma_slab_t tmps;
1044e9a069d8SJohn Baldwin 	int pages, check_pages;
1045099a0e58SBosko Milekic 
1046e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
1047e9a069d8SJohn Baldwin 	pages = howmany(bytes, PAGE_SIZE);
1048e9a069d8SJohn Baldwin 	check_pages = pages - 1;
1049e9a069d8SJohn Baldwin 	KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n"));
1050099a0e58SBosko Milekic 
1051009b6fcbSJeff Roberson 	/*
1052009b6fcbSJeff Roberson 	 * Check our small startup cache to see if it has pages remaining.
1053009b6fcbSJeff Roberson 	 */
1054f353d338SAlan Cox 	mtx_lock(&uma_boot_pages_mtx);
1055e9a069d8SJohn Baldwin 
1056e9a069d8SJohn Baldwin 	/* First check if we have enough room. */
1057e9a069d8SJohn Baldwin 	tmps = LIST_FIRST(&uma_boot_pages);
1058e9a069d8SJohn Baldwin 	while (tmps != NULL && check_pages-- > 0)
1059e9a069d8SJohn Baldwin 		tmps = LIST_NEXT(tmps, us_link);
1060e9a069d8SJohn Baldwin 	if (tmps != NULL) {
1061e9a069d8SJohn Baldwin 		/*
1062e9a069d8SJohn Baldwin 		 * It's ok to lose tmps references.  The last one will
1063e9a069d8SJohn Baldwin 		 * have tmps->us_data pointing to the start address of
1064e9a069d8SJohn Baldwin 		 * "pages" contiguous pages of memory.
1065e9a069d8SJohn Baldwin 		 */
1066e9a069d8SJohn Baldwin 		while (pages-- > 0) {
1067e9a069d8SJohn Baldwin 			tmps = LIST_FIRST(&uma_boot_pages);
1068009b6fcbSJeff Roberson 			LIST_REMOVE(tmps, us_link);
1069e9a069d8SJohn Baldwin 		}
1070f353d338SAlan Cox 		mtx_unlock(&uma_boot_pages_mtx);
1071009b6fcbSJeff Roberson 		*pflag = tmps->us_flags;
1072009b6fcbSJeff Roberson 		return (tmps->us_data);
1073009b6fcbSJeff Roberson 	}
1074f353d338SAlan Cox 	mtx_unlock(&uma_boot_pages_mtx);
1075342f1793SAlan Cox 	if (booted < UMA_STARTUP2)
10763803b26bSDag-Erling Smørgrav 		panic("UMA: Increase vm.boot_pages");
1077009b6fcbSJeff Roberson 	/*
1078009b6fcbSJeff Roberson 	 * Now that we've booted reset these users to their real allocator.
1079009b6fcbSJeff Roberson 	 */
1080009b6fcbSJeff Roberson #ifdef UMA_MD_SMALL_ALLOC
1081e9a069d8SJohn Baldwin 	keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc;
1082009b6fcbSJeff Roberson #else
1083099a0e58SBosko Milekic 	keg->uk_allocf = page_alloc;
1084009b6fcbSJeff Roberson #endif
1085099a0e58SBosko Milekic 	return keg->uk_allocf(zone, bytes, pflag, wait);
1086009b6fcbSJeff Roberson }
1087009b6fcbSJeff Roberson 
1088009b6fcbSJeff Roberson /*
10898355f576SJeff Roberson  * Allocates a number of pages from the system
10908355f576SJeff Roberson  *
10918355f576SJeff Roberson  * Arguments:
10928355f576SJeff Roberson  *	bytes  The number of bytes requested
10938355f576SJeff Roberson  *	wait  Shall we wait?
10948355f576SJeff Roberson  *
10958355f576SJeff Roberson  * Returns:
10968355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
10978355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
10988355f576SJeff Roberson  */
10998355f576SJeff Roberson static void *
1100f2c2231eSRyan Stone page_alloc(uma_zone_t zone, vm_size_t bytes, uint8_t *pflag, int wait)
11018355f576SJeff Roberson {
11028355f576SJeff Roberson 	void *p;	/* Returned page */
11038355f576SJeff Roberson 
11048355f576SJeff Roberson 	*pflag = UMA_SLAB_KMEM;
11055df87b21SJeff Roberson 	p = (void *) kmem_malloc(kmem_arena, bytes, wait);
11068355f576SJeff Roberson 
11078355f576SJeff Roberson 	return (p);
11088355f576SJeff Roberson }
11098355f576SJeff Roberson 
11108355f576SJeff Roberson /*
11118355f576SJeff Roberson  * Allocates a number of pages from within an object
11128355f576SJeff Roberson  *
11138355f576SJeff Roberson  * Arguments:
11148355f576SJeff Roberson  *	bytes  The number of bytes requested
11158355f576SJeff Roberson  *	wait   Shall we wait?
11168355f576SJeff Roberson  *
11178355f576SJeff Roberson  * Returns:
11188355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
11198355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
11208355f576SJeff Roberson  */
11218355f576SJeff Roberson static void *
1122f2c2231eSRyan Stone noobj_alloc(uma_zone_t zone, vm_size_t bytes, uint8_t *flags, int wait)
11238355f576SJeff Roberson {
1124a4915c21SAttilio Rao 	TAILQ_HEAD(, vm_page) alloctail;
1125a4915c21SAttilio Rao 	u_long npages;
1126b245ac95SAlan Cox 	vm_offset_t retkva, zkva;
1127a4915c21SAttilio Rao 	vm_page_t p, p_next;
1128e20a199fSJeff Roberson 	uma_keg_t keg;
11298355f576SJeff Roberson 
1130a4915c21SAttilio Rao 	TAILQ_INIT(&alloctail);
1131e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
1132a4915c21SAttilio Rao 
1133a4915c21SAttilio Rao 	npages = howmany(bytes, PAGE_SIZE);
1134a4915c21SAttilio Rao 	while (npages > 0) {
1135a4915c21SAttilio Rao 		p = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT |
1136a4915c21SAttilio Rao 		    VM_ALLOC_WIRED | VM_ALLOC_NOOBJ);
1137a4915c21SAttilio Rao 		if (p != NULL) {
1138a4915c21SAttilio Rao 			/*
1139a4915c21SAttilio Rao 			 * Since the page does not belong to an object, its
1140a4915c21SAttilio Rao 			 * listq is unused.
1141a4915c21SAttilio Rao 			 */
1142a4915c21SAttilio Rao 			TAILQ_INSERT_TAIL(&alloctail, p, listq);
1143a4915c21SAttilio Rao 			npages--;
1144a4915c21SAttilio Rao 			continue;
1145a4915c21SAttilio Rao 		}
1146a4915c21SAttilio Rao 		if (wait & M_WAITOK) {
1147a4915c21SAttilio Rao 			VM_WAIT;
1148a4915c21SAttilio Rao 			continue;
1149a4915c21SAttilio Rao 		}
11508355f576SJeff Roberson 
11518355f576SJeff Roberson 		/*
1152a4915c21SAttilio Rao 		 * Page allocation failed, free intermediate pages and
1153a4915c21SAttilio Rao 		 * exit.
11548355f576SJeff Roberson 		 */
1155a4915c21SAttilio Rao 		TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
11563ae10f74SAttilio Rao 			vm_page_unwire(p, PQ_INACTIVE);
1157b245ac95SAlan Cox 			vm_page_free(p);
1158b245ac95SAlan Cox 		}
1159a4915c21SAttilio Rao 		return (NULL);
1160b245ac95SAlan Cox 	}
11618355f576SJeff Roberson 	*flags = UMA_SLAB_PRIV;
1162a4915c21SAttilio Rao 	zkva = keg->uk_kva +
1163a4915c21SAttilio Rao 	    atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1164a4915c21SAttilio Rao 	retkva = zkva;
1165a4915c21SAttilio Rao 	TAILQ_FOREACH(p, &alloctail, listq) {
1166a4915c21SAttilio Rao 		pmap_qenter(zkva, &p, 1);
1167a4915c21SAttilio Rao 		zkva += PAGE_SIZE;
1168a4915c21SAttilio Rao 	}
11698355f576SJeff Roberson 
11708355f576SJeff Roberson 	return ((void *)retkva);
11718355f576SJeff Roberson }
11728355f576SJeff Roberson 
11738355f576SJeff Roberson /*
11748355f576SJeff Roberson  * Frees a number of pages to the system
11758355f576SJeff Roberson  *
11768355f576SJeff Roberson  * Arguments:
11778355f576SJeff Roberson  *	mem   A pointer to the memory to be freed
11788355f576SJeff Roberson  *	size  The size of the memory being freed
11798355f576SJeff Roberson  *	flags The original p->us_flags field
11808355f576SJeff Roberson  *
11818355f576SJeff Roberson  * Returns:
11828355f576SJeff Roberson  *	Nothing
11838355f576SJeff Roberson  */
11848355f576SJeff Roberson static void
1185f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags)
11868355f576SJeff Roberson {
11875df87b21SJeff Roberson 	struct vmem *vmem;
11883370c5bfSJeff Roberson 
11898355f576SJeff Roberson 	if (flags & UMA_SLAB_KMEM)
11905df87b21SJeff Roberson 		vmem = kmem_arena;
1191aea6e893SAlan Cox 	else if (flags & UMA_SLAB_KERNEL)
11925df87b21SJeff Roberson 		vmem = kernel_arena;
11938355f576SJeff Roberson 	else
1194aea6e893SAlan Cox 		panic("UMA: page_free used with invalid flags %d", flags);
11958355f576SJeff Roberson 
11965df87b21SJeff Roberson 	kmem_free(vmem, (vm_offset_t)mem, size);
11978355f576SJeff Roberson }
11988355f576SJeff Roberson 
11998355f576SJeff Roberson /*
12008355f576SJeff Roberson  * Zero fill initializer
12018355f576SJeff Roberson  *
12028355f576SJeff Roberson  * Arguments/Returns follow uma_init specifications
12038355f576SJeff Roberson  */
1204b23f72e9SBrian Feldman static int
1205b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags)
12068355f576SJeff Roberson {
12078355f576SJeff Roberson 	bzero(mem, size);
1208b23f72e9SBrian Feldman 	return (0);
12098355f576SJeff Roberson }
12108355f576SJeff Roberson 
12118355f576SJeff Roberson /*
1212e20a199fSJeff Roberson  * Finish creating a small uma keg.  This calculates ipers, and the keg size.
12138355f576SJeff Roberson  *
12148355f576SJeff Roberson  * Arguments
1215e20a199fSJeff Roberson  *	keg  The zone we should initialize
12168355f576SJeff Roberson  *
12178355f576SJeff Roberson  * Returns
12188355f576SJeff Roberson  *	Nothing
12198355f576SJeff Roberson  */
12208355f576SJeff Roberson static void
1221e20a199fSJeff Roberson keg_small_init(uma_keg_t keg)
12228355f576SJeff Roberson {
1223244f4554SBosko Milekic 	u_int rsize;
1224244f4554SBosko Milekic 	u_int memused;
1225244f4554SBosko Milekic 	u_int wastedspace;
1226244f4554SBosko Milekic 	u_int shsize;
12278355f576SJeff Roberson 
1228ad97af7eSGleb Smirnoff 	if (keg->uk_flags & UMA_ZONE_PCPU) {
1229e28a647dSGleb Smirnoff 		u_int ncpus = mp_ncpus ? mp_ncpus : MAXCPU;
1230e28a647dSGleb Smirnoff 
1231ad97af7eSGleb Smirnoff 		keg->uk_slabsize = sizeof(struct pcpu);
1232e28a647dSGleb Smirnoff 		keg->uk_ppera = howmany(ncpus * sizeof(struct pcpu),
1233ad97af7eSGleb Smirnoff 		    PAGE_SIZE);
1234ad97af7eSGleb Smirnoff 	} else {
1235ad97af7eSGleb Smirnoff 		keg->uk_slabsize = UMA_SLAB_SIZE;
1236ad97af7eSGleb Smirnoff 		keg->uk_ppera = 1;
1237ad97af7eSGleb Smirnoff 	}
1238ad97af7eSGleb Smirnoff 
1239ef72505eSJeff Roberson 	/*
1240ef72505eSJeff Roberson 	 * Calculate the size of each allocation (rsize) according to
1241ef72505eSJeff Roberson 	 * alignment.  If the requested size is smaller than we have
1242ef72505eSJeff Roberson 	 * allocation bits for we round it up.
1243ef72505eSJeff Roberson 	 */
1244099a0e58SBosko Milekic 	rsize = keg->uk_size;
1245ef72505eSJeff Roberson 	if (rsize < keg->uk_slabsize / SLAB_SETSIZE)
1246ef72505eSJeff Roberson 		rsize = keg->uk_slabsize / SLAB_SETSIZE;
1247099a0e58SBosko Milekic 	if (rsize & keg->uk_align)
1248099a0e58SBosko Milekic 		rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1249099a0e58SBosko Milekic 	keg->uk_rsize = rsize;
1250ad97af7eSGleb Smirnoff 
1251ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
1252ad97af7eSGleb Smirnoff 	    keg->uk_rsize < sizeof(struct pcpu),
1253ad97af7eSGleb Smirnoff 	    ("%s: size %u too large", __func__, keg->uk_rsize));
12548355f576SJeff Roberson 
1255ef72505eSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_REFCNT)
1256ef72505eSJeff Roberson 		rsize += sizeof(uint32_t);
1257ef72505eSJeff Roberson 
1258ef72505eSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_OFFPAGE)
12592864dbbfSGleb Smirnoff 		shsize = 0;
1260ef72505eSJeff Roberson 	else
1261244f4554SBosko Milekic 		shsize = sizeof(struct uma_slab);
12628355f576SJeff Roberson 
1263ad97af7eSGleb Smirnoff 	keg->uk_ipers = (keg->uk_slabsize - shsize) / rsize;
1264ef72505eSJeff Roberson 	KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1265ad97af7eSGleb Smirnoff 	    ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1266ad97af7eSGleb Smirnoff 
1267244f4554SBosko Milekic 	memused = keg->uk_ipers * rsize + shsize;
1268ad97af7eSGleb Smirnoff 	wastedspace = keg->uk_slabsize - memused;
1269244f4554SBosko Milekic 
127020e8e865SBosko Milekic 	/*
1271244f4554SBosko Milekic 	 * We can't do OFFPAGE if we're internal or if we've been
127220e8e865SBosko Milekic 	 * asked to not go to the VM for buckets.  If we do this we
12736fd34d6fSJeff Roberson 	 * may end up going to the VM  for slabs which we do not
12746fd34d6fSJeff Roberson 	 * want to do if we're UMA_ZFLAG_CACHEONLY as a result
12756fd34d6fSJeff Roberson 	 * of UMA_ZONE_VM, which clearly forbids it.
127620e8e865SBosko Milekic 	 */
1277099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1278099a0e58SBosko Milekic 	    (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
12798355f576SJeff Roberson 		return;
1280244f4554SBosko Milekic 
1281ef72505eSJeff Roberson 	/*
1282ef72505eSJeff Roberson 	 * See if using an OFFPAGE slab will limit our waste.  Only do
1283ef72505eSJeff Roberson 	 * this if it permits more items per-slab.
1284ef72505eSJeff Roberson 	 *
1285ef72505eSJeff Roberson 	 * XXX We could try growing slabsize to limit max waste as well.
1286ef72505eSJeff Roberson 	 * Historically this was not done because the VM could not
1287ef72505eSJeff Roberson 	 * efficiently handle contiguous allocations.
1288ef72505eSJeff Roberson 	 */
1289ad97af7eSGleb Smirnoff 	if ((wastedspace >= keg->uk_slabsize / UMA_MAX_WASTE) &&
1290ad97af7eSGleb Smirnoff 	    (keg->uk_ipers < (keg->uk_slabsize / keg->uk_rsize))) {
1291ad97af7eSGleb Smirnoff 		keg->uk_ipers = keg->uk_slabsize / keg->uk_rsize;
1292ef72505eSJeff Roberson 		KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1293ad97af7eSGleb Smirnoff 		    ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1294244f4554SBosko Milekic #ifdef UMA_DEBUG
1295244f4554SBosko Milekic 		printf("UMA decided we need offpage slab headers for "
1296e20a199fSJeff Roberson 		    "keg: %s, calculated wastedspace = %d, "
1297244f4554SBosko Milekic 		    "maximum wasted space allowed = %d, "
1298244f4554SBosko Milekic 		    "calculated ipers = %d, "
1299e20a199fSJeff Roberson 		    "new wasted space = %d\n", keg->uk_name, wastedspace,
1300ad97af7eSGleb Smirnoff 		    keg->uk_slabsize / UMA_MAX_WASTE, keg->uk_ipers,
1301ad97af7eSGleb Smirnoff 		    keg->uk_slabsize - keg->uk_ipers * keg->uk_rsize);
1302244f4554SBosko Milekic #endif
1303099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZONE_OFFPAGE;
13048355f576SJeff Roberson 	}
1305ad97af7eSGleb Smirnoff 
1306ad97af7eSGleb Smirnoff 	if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1307ad97af7eSGleb Smirnoff 	    (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1308ad97af7eSGleb Smirnoff 		keg->uk_flags |= UMA_ZONE_HASH;
13098355f576SJeff Roberson }
13108355f576SJeff Roberson 
13118355f576SJeff Roberson /*
1312e20a199fSJeff Roberson  * Finish creating a large (> UMA_SLAB_SIZE) uma kegs.  Just give in and do
13138355f576SJeff Roberson  * OFFPAGE for now.  When I can allow for more dynamic slab sizes this will be
13148355f576SJeff Roberson  * more complicated.
13158355f576SJeff Roberson  *
13168355f576SJeff Roberson  * Arguments
1317e20a199fSJeff Roberson  *	keg  The keg we should initialize
13188355f576SJeff Roberson  *
13198355f576SJeff Roberson  * Returns
13208355f576SJeff Roberson  *	Nothing
13218355f576SJeff Roberson  */
13228355f576SJeff Roberson static void
1323e20a199fSJeff Roberson keg_large_init(uma_keg_t keg)
13248355f576SJeff Roberson {
1325cec48e00SAlexander Motin 	u_int shsize;
13268355f576SJeff Roberson 
1327e20a199fSJeff Roberson 	KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1328099a0e58SBosko Milekic 	KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1329e20a199fSJeff Roberson 	    ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1330ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1331ad97af7eSGleb Smirnoff 	    ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__));
133220e8e865SBosko Milekic 
1333ad97af7eSGleb Smirnoff 	keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE);
1334ad97af7eSGleb Smirnoff 	keg->uk_slabsize = keg->uk_ppera * PAGE_SIZE;
1335099a0e58SBosko Milekic 	keg->uk_ipers = 1;
1336e9a069d8SJohn Baldwin 	keg->uk_rsize = keg->uk_size;
1337e9a069d8SJohn Baldwin 
1338e9a069d8SJohn Baldwin 	/* We can't do OFFPAGE if we're internal, bail out here. */
1339e9a069d8SJohn Baldwin 	if (keg->uk_flags & UMA_ZFLAG_INTERNAL)
1340e9a069d8SJohn Baldwin 		return;
13418355f576SJeff Roberson 
1342cec48e00SAlexander Motin 	/* Check whether we have enough space to not do OFFPAGE. */
1343cec48e00SAlexander Motin 	if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0) {
1344cec48e00SAlexander Motin 		shsize = sizeof(struct uma_slab);
1345cec48e00SAlexander Motin 		if (keg->uk_flags & UMA_ZONE_REFCNT)
1346cec48e00SAlexander Motin 			shsize += keg->uk_ipers * sizeof(uint32_t);
1347cec48e00SAlexander Motin 		if (shsize & UMA_ALIGN_PTR)
1348cec48e00SAlexander Motin 			shsize = (shsize & ~UMA_ALIGN_PTR) +
1349cec48e00SAlexander Motin 			    (UMA_ALIGN_PTR + 1);
1350cec48e00SAlexander Motin 
1351cec48e00SAlexander Motin 		if ((PAGE_SIZE * keg->uk_ppera) - keg->uk_rsize < shsize)
1352099a0e58SBosko Milekic 			keg->uk_flags |= UMA_ZONE_OFFPAGE;
1353cec48e00SAlexander Motin 	}
1354cec48e00SAlexander Motin 
1355cec48e00SAlexander Motin 	if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1356cec48e00SAlexander Motin 	    (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1357099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZONE_HASH;
13588355f576SJeff Roberson }
13598355f576SJeff Roberson 
1360e20a199fSJeff Roberson static void
1361e20a199fSJeff Roberson keg_cachespread_init(uma_keg_t keg)
1362e20a199fSJeff Roberson {
1363e20a199fSJeff Roberson 	int alignsize;
1364e20a199fSJeff Roberson 	int trailer;
1365e20a199fSJeff Roberson 	int pages;
1366e20a199fSJeff Roberson 	int rsize;
1367e20a199fSJeff Roberson 
1368ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1369ad97af7eSGleb Smirnoff 	    ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__));
1370ad97af7eSGleb Smirnoff 
1371e20a199fSJeff Roberson 	alignsize = keg->uk_align + 1;
1372e20a199fSJeff Roberson 	rsize = keg->uk_size;
1373e20a199fSJeff Roberson 	/*
1374e20a199fSJeff Roberson 	 * We want one item to start on every align boundary in a page.  To
1375e20a199fSJeff Roberson 	 * do this we will span pages.  We will also extend the item by the
1376e20a199fSJeff Roberson 	 * size of align if it is an even multiple of align.  Otherwise, it
1377e20a199fSJeff Roberson 	 * would fall on the same boundary every time.
1378e20a199fSJeff Roberson 	 */
1379e20a199fSJeff Roberson 	if (rsize & keg->uk_align)
1380e20a199fSJeff Roberson 		rsize = (rsize & ~keg->uk_align) + alignsize;
1381e20a199fSJeff Roberson 	if ((rsize & alignsize) == 0)
1382e20a199fSJeff Roberson 		rsize += alignsize;
1383e20a199fSJeff Roberson 	trailer = rsize - keg->uk_size;
1384e20a199fSJeff Roberson 	pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1385e20a199fSJeff Roberson 	pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1386e20a199fSJeff Roberson 	keg->uk_rsize = rsize;
1387e20a199fSJeff Roberson 	keg->uk_ppera = pages;
1388ad97af7eSGleb Smirnoff 	keg->uk_slabsize = UMA_SLAB_SIZE;
1389e20a199fSJeff Roberson 	keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1390e20a199fSJeff Roberson 	keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
13912367b4ddSDimitry Andric 	KASSERT(keg->uk_ipers <= SLAB_SETSIZE,
139242321809SGleb Smirnoff 	    ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1393e20a199fSJeff Roberson 	    keg->uk_ipers));
1394e20a199fSJeff Roberson }
1395e20a199fSJeff Roberson 
13968355f576SJeff Roberson /*
1397099a0e58SBosko Milekic  * Keg header ctor.  This initializes all fields, locks, etc.  And inserts
1398099a0e58SBosko Milekic  * the keg onto the global keg list.
13998355f576SJeff Roberson  *
14008355f576SJeff Roberson  * Arguments/Returns follow uma_ctor specifications
1401099a0e58SBosko Milekic  *	udata  Actually uma_kctor_args
1402099a0e58SBosko Milekic  */
1403b23f72e9SBrian Feldman static int
1404b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags)
1405099a0e58SBosko Milekic {
1406099a0e58SBosko Milekic 	struct uma_kctor_args *arg = udata;
1407099a0e58SBosko Milekic 	uma_keg_t keg = mem;
1408099a0e58SBosko Milekic 	uma_zone_t zone;
1409099a0e58SBosko Milekic 
1410099a0e58SBosko Milekic 	bzero(keg, size);
1411099a0e58SBosko Milekic 	keg->uk_size = arg->size;
1412099a0e58SBosko Milekic 	keg->uk_init = arg->uminit;
1413099a0e58SBosko Milekic 	keg->uk_fini = arg->fini;
1414099a0e58SBosko Milekic 	keg->uk_align = arg->align;
1415099a0e58SBosko Milekic 	keg->uk_free = 0;
14166fd34d6fSJeff Roberson 	keg->uk_reserve = 0;
1417099a0e58SBosko Milekic 	keg->uk_pages = 0;
1418099a0e58SBosko Milekic 	keg->uk_flags = arg->flags;
1419099a0e58SBosko Milekic 	keg->uk_allocf = page_alloc;
1420099a0e58SBosko Milekic 	keg->uk_freef = page_free;
1421099a0e58SBosko Milekic 	keg->uk_slabzone = NULL;
1422099a0e58SBosko Milekic 
1423099a0e58SBosko Milekic 	/*
1424099a0e58SBosko Milekic 	 * The master zone is passed to us at keg-creation time.
1425099a0e58SBosko Milekic 	 */
1426099a0e58SBosko Milekic 	zone = arg->zone;
1427e20a199fSJeff Roberson 	keg->uk_name = zone->uz_name;
1428099a0e58SBosko Milekic 
1429099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_VM)
1430099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1431099a0e58SBosko Milekic 
1432099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_ZINIT)
1433099a0e58SBosko Milekic 		keg->uk_init = zero_init;
1434099a0e58SBosko Milekic 
1435e20a199fSJeff Roberson 	if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC)
1436e20a199fSJeff Roberson 		keg->uk_flags |= UMA_ZONE_VTOSLAB;
1437e20a199fSJeff Roberson 
1438ad97af7eSGleb Smirnoff 	if (arg->flags & UMA_ZONE_PCPU)
1439ad97af7eSGleb Smirnoff #ifdef SMP
1440ad97af7eSGleb Smirnoff 		keg->uk_flags |= UMA_ZONE_OFFPAGE;
1441ad97af7eSGleb Smirnoff #else
1442ad97af7eSGleb Smirnoff 		keg->uk_flags &= ~UMA_ZONE_PCPU;
1443ad97af7eSGleb Smirnoff #endif
1444ad97af7eSGleb Smirnoff 
1445ef72505eSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_CACHESPREAD) {
1446e20a199fSJeff Roberson 		keg_cachespread_init(keg);
1447ef72505eSJeff Roberson 	} else if (keg->uk_flags & UMA_ZONE_REFCNT) {
1448ef72505eSJeff Roberson 		if (keg->uk_size >
1449ef72505eSJeff Roberson 		    (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) -
1450ef72505eSJeff Roberson 		    sizeof(uint32_t)))
1451e20a199fSJeff Roberson 			keg_large_init(keg);
1452099a0e58SBosko Milekic 		else
1453e20a199fSJeff Roberson 			keg_small_init(keg);
1454244f4554SBosko Milekic 	} else {
1455ef72505eSJeff Roberson 		if (keg->uk_size > (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1456e20a199fSJeff Roberson 			keg_large_init(keg);
1457244f4554SBosko Milekic 		else
1458e20a199fSJeff Roberson 			keg_small_init(keg);
1459244f4554SBosko Milekic 	}
1460099a0e58SBosko Milekic 
1461244f4554SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1462ef72505eSJeff Roberson 		if (keg->uk_flags & UMA_ZONE_REFCNT) {
1463ef72505eSJeff Roberson 			if (keg->uk_ipers > uma_max_ipers_ref)
1464ef72505eSJeff Roberson 				panic("Too many ref items per zone: %d > %d\n",
1465ef72505eSJeff Roberson 				    keg->uk_ipers, uma_max_ipers_ref);
1466099a0e58SBosko Milekic 			keg->uk_slabzone = slabrefzone;
1467ef72505eSJeff Roberson 		} else
1468099a0e58SBosko Milekic 			keg->uk_slabzone = slabzone;
1469244f4554SBosko Milekic 	}
1470099a0e58SBosko Milekic 
1471099a0e58SBosko Milekic 	/*
1472099a0e58SBosko Milekic 	 * If we haven't booted yet we need allocations to go through the
1473099a0e58SBosko Milekic 	 * startup cache until the vm is ready.
1474099a0e58SBosko Milekic 	 */
1475099a0e58SBosko Milekic 	if (keg->uk_ppera == 1) {
1476099a0e58SBosko Milekic #ifdef UMA_MD_SMALL_ALLOC
1477099a0e58SBosko Milekic 		keg->uk_allocf = uma_small_alloc;
1478099a0e58SBosko Milekic 		keg->uk_freef = uma_small_free;
14798cd02d00SAlan Cox 
1480342f1793SAlan Cox 		if (booted < UMA_STARTUP)
1481099a0e58SBosko Milekic 			keg->uk_allocf = startup_alloc;
14828cd02d00SAlan Cox #else
14838cd02d00SAlan Cox 		if (booted < UMA_STARTUP2)
14848cd02d00SAlan Cox 			keg->uk_allocf = startup_alloc;
14858cd02d00SAlan Cox #endif
1486342f1793SAlan Cox 	} else if (booted < UMA_STARTUP2 &&
1487342f1793SAlan Cox 	    (keg->uk_flags & UMA_ZFLAG_INTERNAL))
1488e9a069d8SJohn Baldwin 		keg->uk_allocf = startup_alloc;
1489099a0e58SBosko Milekic 
1490099a0e58SBosko Milekic 	/*
1491af526374SJeff Roberson 	 * Initialize keg's lock
1492099a0e58SBosko Milekic 	 */
1493af526374SJeff Roberson 	KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS));
1494099a0e58SBosko Milekic 
1495099a0e58SBosko Milekic 	/*
1496099a0e58SBosko Milekic 	 * If we're putting the slab header in the actual page we need to
1497099a0e58SBosko Milekic 	 * figure out where in each page it goes.  This calculates a right
1498099a0e58SBosko Milekic 	 * justified offset into the memory on an ALIGN_PTR boundary.
1499099a0e58SBosko Milekic 	 */
1500099a0e58SBosko Milekic 	if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1501244f4554SBosko Milekic 		u_int totsize;
1502099a0e58SBosko Milekic 
1503099a0e58SBosko Milekic 		/* Size of the slab struct and free list */
1504ef72505eSJeff Roberson 		totsize = sizeof(struct uma_slab);
1505ef72505eSJeff Roberson 
1506ef72505eSJeff Roberson 		/* Size of the reference counts. */
1507244f4554SBosko Milekic 		if (keg->uk_flags & UMA_ZONE_REFCNT)
1508ef72505eSJeff Roberson 			totsize += keg->uk_ipers * sizeof(uint32_t);
1509244f4554SBosko Milekic 
1510099a0e58SBosko Milekic 		if (totsize & UMA_ALIGN_PTR)
1511099a0e58SBosko Milekic 			totsize = (totsize & ~UMA_ALIGN_PTR) +
1512099a0e58SBosko Milekic 			    (UMA_ALIGN_PTR + 1);
1513ad97af7eSGleb Smirnoff 		keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize;
1514244f4554SBosko Milekic 
1515244f4554SBosko Milekic 		/*
1516244f4554SBosko Milekic 		 * The only way the following is possible is if with our
1517244f4554SBosko Milekic 		 * UMA_ALIGN_PTR adjustments we are now bigger than
1518244f4554SBosko Milekic 		 * UMA_SLAB_SIZE.  I haven't checked whether this is
1519244f4554SBosko Milekic 		 * mathematically possible for all cases, so we make
1520244f4554SBosko Milekic 		 * sure here anyway.
1521244f4554SBosko Milekic 		 */
1522ef72505eSJeff Roberson 		totsize = keg->uk_pgoff + sizeof(struct uma_slab);
1523ef72505eSJeff Roberson 		if (keg->uk_flags & UMA_ZONE_REFCNT)
1524ef72505eSJeff Roberson 			totsize += keg->uk_ipers * sizeof(uint32_t);
1525ad97af7eSGleb Smirnoff 		if (totsize > PAGE_SIZE * keg->uk_ppera) {
1526099a0e58SBosko Milekic 			printf("zone %s ipers %d rsize %d size %d\n",
1527099a0e58SBosko Milekic 			    zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1528099a0e58SBosko Milekic 			    keg->uk_size);
1529aea6e893SAlan Cox 			panic("UMA slab won't fit.");
1530099a0e58SBosko Milekic 		}
1531099a0e58SBosko Milekic 	}
1532099a0e58SBosko Milekic 
1533099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_HASH)
1534099a0e58SBosko Milekic 		hash_alloc(&keg->uk_hash);
1535099a0e58SBosko Milekic 
1536099a0e58SBosko Milekic #ifdef UMA_DEBUG
15370b80c1e4SEitan Adler 	printf("UMA: %s(%p) size %d(%d) flags %#x ipers %d ppera %d out %d free %d\n",
1538e20a199fSJeff Roberson 	    zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
1539e20a199fSJeff Roberson 	    keg->uk_ipers, keg->uk_ppera,
1540e20a199fSJeff Roberson 	    (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
1541099a0e58SBosko Milekic #endif
1542099a0e58SBosko Milekic 
1543099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1544099a0e58SBosko Milekic 
1545111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
1546099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1547111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
1548b23f72e9SBrian Feldman 	return (0);
1549099a0e58SBosko Milekic }
1550099a0e58SBosko Milekic 
1551099a0e58SBosko Milekic /*
1552099a0e58SBosko Milekic  * Zone header ctor.  This initializes all fields, locks, etc.
1553099a0e58SBosko Milekic  *
1554099a0e58SBosko Milekic  * Arguments/Returns follow uma_ctor specifications
1555099a0e58SBosko Milekic  *	udata  Actually uma_zctor_args
15568355f576SJeff Roberson  */
1557b23f72e9SBrian Feldman static int
1558b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags)
15598355f576SJeff Roberson {
15608355f576SJeff Roberson 	struct uma_zctor_args *arg = udata;
15618355f576SJeff Roberson 	uma_zone_t zone = mem;
1562099a0e58SBosko Milekic 	uma_zone_t z;
1563099a0e58SBosko Milekic 	uma_keg_t keg;
15648355f576SJeff Roberson 
15658355f576SJeff Roberson 	bzero(zone, size);
15668355f576SJeff Roberson 	zone->uz_name = arg->name;
15678355f576SJeff Roberson 	zone->uz_ctor = arg->ctor;
15688355f576SJeff Roberson 	zone->uz_dtor = arg->dtor;
1569e20a199fSJeff Roberson 	zone->uz_slab = zone_fetch_slab;
1570099a0e58SBosko Milekic 	zone->uz_init = NULL;
1571099a0e58SBosko Milekic 	zone->uz_fini = NULL;
1572099a0e58SBosko Milekic 	zone->uz_allocs = 0;
1573773df9abSRobert Watson 	zone->uz_frees = 0;
15742019094aSRobert Watson 	zone->uz_fails = 0;
1575bf965959SSean Bruno 	zone->uz_sleeps = 0;
1576fc03d22bSJeff Roberson 	zone->uz_count = 0;
1577ace66b56SAlexander Motin 	zone->uz_count_min = 0;
1578e20a199fSJeff Roberson 	zone->uz_flags = 0;
15792f891cd5SPawel Jakub Dawidek 	zone->uz_warning = NULL;
15802f891cd5SPawel Jakub Dawidek 	timevalclear(&zone->uz_ratecheck);
1581e20a199fSJeff Roberson 	keg = arg->keg;
1582099a0e58SBosko Milekic 
1583af526374SJeff Roberson 	ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
1584af526374SJeff Roberson 
15850095a784SJeff Roberson 	/*
15860095a784SJeff Roberson 	 * This is a pure cache zone, no kegs.
15870095a784SJeff Roberson 	 */
15880095a784SJeff Roberson 	if (arg->import) {
15896fd34d6fSJeff Roberson 		if (arg->flags & UMA_ZONE_VM)
15906fd34d6fSJeff Roberson 			arg->flags |= UMA_ZFLAG_CACHEONLY;
15916fd34d6fSJeff Roberson 		zone->uz_flags = arg->flags;
1592af526374SJeff Roberson 		zone->uz_size = arg->size;
15930095a784SJeff Roberson 		zone->uz_import = arg->import;
15940095a784SJeff Roberson 		zone->uz_release = arg->release;
15950095a784SJeff Roberson 		zone->uz_arg = arg->arg;
1596af526374SJeff Roberson 		zone->uz_lockptr = &zone->uz_lock;
1597111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
159803175483SAlexander Motin 		LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
1599111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
1600af526374SJeff Roberson 		goto out;
16010095a784SJeff Roberson 	}
16020095a784SJeff Roberson 
16030095a784SJeff Roberson 	/*
16040095a784SJeff Roberson 	 * Use the regular zone/keg/slab allocator.
16050095a784SJeff Roberson 	 */
16060095a784SJeff Roberson 	zone->uz_import = (uma_import)zone_import;
16070095a784SJeff Roberson 	zone->uz_release = (uma_release)zone_release;
16080095a784SJeff Roberson 	zone->uz_arg = zone;
16090095a784SJeff Roberson 
1610099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_SECONDARY) {
1611099a0e58SBosko Milekic 		KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
16128355f576SJeff Roberson 		zone->uz_init = arg->uminit;
1613e221e841SJeff Roberson 		zone->uz_fini = arg->fini;
1614af526374SJeff Roberson 		zone->uz_lockptr = &keg->uk_lock;
1615e20a199fSJeff Roberson 		zone->uz_flags |= UMA_ZONE_SECONDARY;
1616111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
1617099a0e58SBosko Milekic 		ZONE_LOCK(zone);
1618099a0e58SBosko Milekic 		LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1619099a0e58SBosko Milekic 			if (LIST_NEXT(z, uz_link) == NULL) {
1620099a0e58SBosko Milekic 				LIST_INSERT_AFTER(z, zone, uz_link);
1621099a0e58SBosko Milekic 				break;
1622099a0e58SBosko Milekic 			}
1623099a0e58SBosko Milekic 		}
1624099a0e58SBosko Milekic 		ZONE_UNLOCK(zone);
1625111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
1626e20a199fSJeff Roberson 	} else if (keg == NULL) {
1627e20a199fSJeff Roberson 		if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1628e20a199fSJeff Roberson 		    arg->align, arg->flags)) == NULL)
1629b23f72e9SBrian Feldman 			return (ENOMEM);
1630099a0e58SBosko Milekic 	} else {
1631099a0e58SBosko Milekic 		struct uma_kctor_args karg;
1632b23f72e9SBrian Feldman 		int error;
1633099a0e58SBosko Milekic 
1634099a0e58SBosko Milekic 		/* We should only be here from uma_startup() */
1635099a0e58SBosko Milekic 		karg.size = arg->size;
1636099a0e58SBosko Milekic 		karg.uminit = arg->uminit;
1637099a0e58SBosko Milekic 		karg.fini = arg->fini;
1638099a0e58SBosko Milekic 		karg.align = arg->align;
1639099a0e58SBosko Milekic 		karg.flags = arg->flags;
1640099a0e58SBosko Milekic 		karg.zone = zone;
1641b23f72e9SBrian Feldman 		error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1642b23f72e9SBrian Feldman 		    flags);
1643b23f72e9SBrian Feldman 		if (error)
1644b23f72e9SBrian Feldman 			return (error);
1645099a0e58SBosko Milekic 	}
16460095a784SJeff Roberson 
1647e20a199fSJeff Roberson 	/*
1648e20a199fSJeff Roberson 	 * Link in the first keg.
1649e20a199fSJeff Roberson 	 */
1650e20a199fSJeff Roberson 	zone->uz_klink.kl_keg = keg;
1651e20a199fSJeff Roberson 	LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1652af526374SJeff Roberson 	zone->uz_lockptr = &keg->uk_lock;
1653e20a199fSJeff Roberson 	zone->uz_size = keg->uk_size;
1654e20a199fSJeff Roberson 	zone->uz_flags |= (keg->uk_flags &
1655e20a199fSJeff Roberson 	    (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
16568355f576SJeff Roberson 
16578355f576SJeff Roberson 	/*
16588355f576SJeff Roberson 	 * Some internal zones don't have room allocated for the per cpu
16598355f576SJeff Roberson 	 * caches.  If we're internal, bail out here.
16608355f576SJeff Roberson 	 */
1661099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1662e20a199fSJeff Roberson 		KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1663099a0e58SBosko Milekic 		    ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1664b23f72e9SBrian Feldman 		return (0);
1665099a0e58SBosko Milekic 	}
16668355f576SJeff Roberson 
1667af526374SJeff Roberson out:
1668af526374SJeff Roberson 	if ((arg->flags & UMA_ZONE_MAXBUCKET) == 0)
1669af526374SJeff Roberson 		zone->uz_count = bucket_select(zone->uz_size);
16708355f576SJeff Roberson 	else
1671cae33c14SJeff Roberson 		zone->uz_count = BUCKET_MAX;
1672ace66b56SAlexander Motin 	zone->uz_count_min = zone->uz_count;
1673fc03d22bSJeff Roberson 
1674b23f72e9SBrian Feldman 	return (0);
16758355f576SJeff Roberson }
16768355f576SJeff Roberson 
16778355f576SJeff Roberson /*
1678099a0e58SBosko Milekic  * Keg header dtor.  This frees all data, destroys locks, frees the hash
1679099a0e58SBosko Milekic  * table and removes the keg from the global list.
16809c2cd7e5SJeff Roberson  *
16819c2cd7e5SJeff Roberson  * Arguments/Returns follow uma_dtor specifications
16829c2cd7e5SJeff Roberson  *	udata  unused
16839c2cd7e5SJeff Roberson  */
1684099a0e58SBosko Milekic static void
1685099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata)
1686099a0e58SBosko Milekic {
1687099a0e58SBosko Milekic 	uma_keg_t keg;
16889c2cd7e5SJeff Roberson 
1689099a0e58SBosko Milekic 	keg = (uma_keg_t)arg;
1690e20a199fSJeff Roberson 	KEG_LOCK(keg);
1691099a0e58SBosko Milekic 	if (keg->uk_free != 0) {
1692a3845534SCraig Rodrigues 		printf("Freed UMA keg (%s) was not empty (%d items). "
1693099a0e58SBosko Milekic 		    " Lost %d pages of memory.\n",
1694a3845534SCraig Rodrigues 		    keg->uk_name ? keg->uk_name : "",
1695099a0e58SBosko Milekic 		    keg->uk_free, keg->uk_pages);
1696099a0e58SBosko Milekic 	}
1697e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
1698099a0e58SBosko Milekic 
1699099a0e58SBosko Milekic 	hash_free(&keg->uk_hash);
1700099a0e58SBosko Milekic 
1701e20a199fSJeff Roberson 	KEG_LOCK_FINI(keg);
1702099a0e58SBosko Milekic }
1703099a0e58SBosko Milekic 
1704099a0e58SBosko Milekic /*
1705099a0e58SBosko Milekic  * Zone header dtor.
1706099a0e58SBosko Milekic  *
1707099a0e58SBosko Milekic  * Arguments/Returns follow uma_dtor specifications
1708099a0e58SBosko Milekic  *	udata  unused
1709099a0e58SBosko Milekic  */
17109c2cd7e5SJeff Roberson static void
17119c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata)
17129c2cd7e5SJeff Roberson {
1713e20a199fSJeff Roberson 	uma_klink_t klink;
17149c2cd7e5SJeff Roberson 	uma_zone_t zone;
1715099a0e58SBosko Milekic 	uma_keg_t keg;
17169c2cd7e5SJeff Roberson 
17179c2cd7e5SJeff Roberson 	zone = (uma_zone_t)arg;
1718e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
17199643769aSJeff Roberson 
1720e20a199fSJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
17219643769aSJeff Roberson 		cache_drain(zone);
1722099a0e58SBosko Milekic 
1723111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
1724099a0e58SBosko Milekic 	LIST_REMOVE(zone, uz_link);
1725111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
1726099a0e58SBosko Milekic 	/*
1727099a0e58SBosko Milekic 	 * XXX there are some races here where
1728099a0e58SBosko Milekic 	 * the zone can be drained but zone lock
1729099a0e58SBosko Milekic 	 * released and then refilled before we
1730099a0e58SBosko Milekic 	 * remove it... we dont care for now
1731099a0e58SBosko Milekic 	 */
1732e20a199fSJeff Roberson 	zone_drain_wait(zone, M_WAITOK);
1733e20a199fSJeff Roberson 	/*
1734e20a199fSJeff Roberson 	 * Unlink all of our kegs.
1735e20a199fSJeff Roberson 	 */
1736e20a199fSJeff Roberson 	while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1737e20a199fSJeff Roberson 		klink->kl_keg = NULL;
1738e20a199fSJeff Roberson 		LIST_REMOVE(klink, kl_link);
1739e20a199fSJeff Roberson 		if (klink == &zone->uz_klink)
1740e20a199fSJeff Roberson 			continue;
1741e20a199fSJeff Roberson 		free(klink, M_TEMP);
1742e20a199fSJeff Roberson 	}
1743e20a199fSJeff Roberson 	/*
1744e20a199fSJeff Roberson 	 * We only destroy kegs from non secondary zones.
1745e20a199fSJeff Roberson 	 */
17460095a784SJeff Roberson 	if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0)  {
1747111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
1748099a0e58SBosko Milekic 		LIST_REMOVE(keg, uk_link);
1749111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
17500095a784SJeff Roberson 		zone_free_item(kegs, keg, NULL, SKIP_NONE);
17519c2cd7e5SJeff Roberson 	}
1752af526374SJeff Roberson 	ZONE_LOCK_FINI(zone);
1753099a0e58SBosko Milekic }
1754099a0e58SBosko Milekic 
17559c2cd7e5SJeff Roberson /*
17568355f576SJeff Roberson  * Traverses every zone in the system and calls a callback
17578355f576SJeff Roberson  *
17588355f576SJeff Roberson  * Arguments:
17598355f576SJeff Roberson  *	zfunc  A pointer to a function which accepts a zone
17608355f576SJeff Roberson  *		as an argument.
17618355f576SJeff Roberson  *
17628355f576SJeff Roberson  * Returns:
17638355f576SJeff Roberson  *	Nothing
17648355f576SJeff Roberson  */
17658355f576SJeff Roberson static void
17668355f576SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t))
17678355f576SJeff Roberson {
1768099a0e58SBosko Milekic 	uma_keg_t keg;
17698355f576SJeff Roberson 	uma_zone_t zone;
17708355f576SJeff Roberson 
1771111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
1772099a0e58SBosko Milekic 	LIST_FOREACH(keg, &uma_kegs, uk_link) {
1773099a0e58SBosko Milekic 		LIST_FOREACH(zone, &keg->uk_zones, uz_link)
17748355f576SJeff Roberson 			zfunc(zone);
1775099a0e58SBosko Milekic 	}
1776111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
17778355f576SJeff Roberson }
17788355f576SJeff Roberson 
17798355f576SJeff Roberson /* Public functions */
17808355f576SJeff Roberson /* See uma.h */
17818355f576SJeff Roberson void
17823803b26bSDag-Erling Smørgrav uma_startup(void *bootmem, int boot_pages)
17838355f576SJeff Roberson {
17848355f576SJeff Roberson 	struct uma_zctor_args args;
17858355f576SJeff Roberson 	uma_slab_t slab;
1786244f4554SBosko Milekic 	u_int slabsize;
17878355f576SJeff Roberson 	int i;
17888355f576SJeff Roberson 
17898355f576SJeff Roberson #ifdef UMA_DEBUG
1790099a0e58SBosko Milekic 	printf("Creating uma keg headers zone and keg.\n");
17918355f576SJeff Roberson #endif
1792111fbcd5SBryan Venteicher 	rw_init(&uma_rwlock, "UMA lock");
1793099a0e58SBosko Milekic 
1794099a0e58SBosko Milekic 	/* "manually" create the initial zone */
17950095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
1796099a0e58SBosko Milekic 	args.name = "UMA Kegs";
1797099a0e58SBosko Milekic 	args.size = sizeof(struct uma_keg);
1798099a0e58SBosko Milekic 	args.ctor = keg_ctor;
1799099a0e58SBosko Milekic 	args.dtor = keg_dtor;
18008355f576SJeff Roberson 	args.uminit = zero_init;
18018355f576SJeff Roberson 	args.fini = NULL;
1802099a0e58SBosko Milekic 	args.keg = &masterkeg;
18038355f576SJeff Roberson 	args.align = 32 - 1;
1804b60f5b79SJeff Roberson 	args.flags = UMA_ZFLAG_INTERNAL;
18058355f576SJeff Roberson 	/* The initial zone has no Per cpu queues so it's smaller */
1806b23f72e9SBrian Feldman 	zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
18078355f576SJeff Roberson 
18088355f576SJeff Roberson #ifdef UMA_DEBUG
18098355f576SJeff Roberson 	printf("Filling boot free list.\n");
18108355f576SJeff Roberson #endif
18113803b26bSDag-Erling Smørgrav 	for (i = 0; i < boot_pages; i++) {
181285dcf349SGleb Smirnoff 		slab = (uma_slab_t)((uint8_t *)bootmem + (i * UMA_SLAB_SIZE));
181385dcf349SGleb Smirnoff 		slab->us_data = (uint8_t *)slab;
18148355f576SJeff Roberson 		slab->us_flags = UMA_SLAB_BOOT;
18158355f576SJeff Roberson 		LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
18168355f576SJeff Roberson 	}
1817f353d338SAlan Cox 	mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
18188355f576SJeff Roberson 
18198355f576SJeff Roberson #ifdef UMA_DEBUG
1820099a0e58SBosko Milekic 	printf("Creating uma zone headers zone and keg.\n");
1821099a0e58SBosko Milekic #endif
1822099a0e58SBosko Milekic 	args.name = "UMA Zones";
1823099a0e58SBosko Milekic 	args.size = sizeof(struct uma_zone) +
182451cfb0beSDmitry Chagin 	    (sizeof(struct uma_cache) * (mp_maxid + 1));
1825099a0e58SBosko Milekic 	args.ctor = zone_ctor;
1826099a0e58SBosko Milekic 	args.dtor = zone_dtor;
1827099a0e58SBosko Milekic 	args.uminit = zero_init;
1828099a0e58SBosko Milekic 	args.fini = NULL;
1829099a0e58SBosko Milekic 	args.keg = NULL;
1830099a0e58SBosko Milekic 	args.align = 32 - 1;
1831099a0e58SBosko Milekic 	args.flags = UMA_ZFLAG_INTERNAL;
1832099a0e58SBosko Milekic 	/* The initial zone has no Per cpu queues so it's smaller */
1833b23f72e9SBrian Feldman 	zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1834099a0e58SBosko Milekic 
1835099a0e58SBosko Milekic #ifdef UMA_DEBUG
1836099a0e58SBosko Milekic 	printf("Creating slab and hash zones.\n");
18378355f576SJeff Roberson #endif
18388355f576SJeff Roberson 
18398355f576SJeff Roberson 	/* Now make a zone for slab headers */
18408355f576SJeff Roberson 	slabzone = uma_zcreate("UMA Slabs",
1841ef72505eSJeff Roberson 				sizeof(struct uma_slab),
18428355f576SJeff Roberson 				NULL, NULL, NULL, NULL,
1843b60f5b79SJeff Roberson 				UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
18448355f576SJeff Roberson 
1845099a0e58SBosko Milekic 	/*
1846099a0e58SBosko Milekic 	 * We also create a zone for the bigger slabs with reference
1847099a0e58SBosko Milekic 	 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1848099a0e58SBosko Milekic 	 */
1849ef72505eSJeff Roberson 	slabsize = sizeof(struct uma_slab_refcnt);
1850ef72505eSJeff Roberson 	slabsize += uma_max_ipers_ref * sizeof(uint32_t);
1851099a0e58SBosko Milekic 	slabrefzone = uma_zcreate("UMA RCntSlabs",
1852099a0e58SBosko Milekic 				  slabsize,
1853099a0e58SBosko Milekic 				  NULL, NULL, NULL, NULL,
1854e66468eaSBosko Milekic 				  UMA_ALIGN_PTR,
18557fd87882SBosko Milekic 				  UMA_ZFLAG_INTERNAL);
1856099a0e58SBosko Milekic 
18578355f576SJeff Roberson 	hashzone = uma_zcreate("UMA Hash",
18588355f576SJeff Roberson 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
18598355f576SJeff Roberson 	    NULL, NULL, NULL, NULL,
1860b60f5b79SJeff Roberson 	    UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
18618355f576SJeff Roberson 
1862cae33c14SJeff Roberson 	bucket_init();
18638355f576SJeff Roberson 
1864342f1793SAlan Cox 	booted = UMA_STARTUP;
18658355f576SJeff Roberson 
18668355f576SJeff Roberson #ifdef UMA_DEBUG
18678355f576SJeff Roberson 	printf("UMA startup complete.\n");
18688355f576SJeff Roberson #endif
18698355f576SJeff Roberson }
18708355f576SJeff Roberson 
18718355f576SJeff Roberson /* see uma.h */
18728355f576SJeff Roberson void
187399571dc3SJeff Roberson uma_startup2(void)
18748355f576SJeff Roberson {
1875342f1793SAlan Cox 	booted = UMA_STARTUP2;
187686bbae32SJeff Roberson 	bucket_enable();
187795c4bf75SKonstantin Belousov 	sx_init(&uma_drain_lock, "umadrain");
18788355f576SJeff Roberson #ifdef UMA_DEBUG
18798355f576SJeff Roberson 	printf("UMA startup2 complete.\n");
18808355f576SJeff Roberson #endif
18818355f576SJeff Roberson }
18828355f576SJeff Roberson 
18838355f576SJeff Roberson /*
18848355f576SJeff Roberson  * Initialize our callout handle
18858355f576SJeff Roberson  *
18868355f576SJeff Roberson  */
18878355f576SJeff Roberson 
18888355f576SJeff Roberson static void
18898355f576SJeff Roberson uma_startup3(void)
18908355f576SJeff Roberson {
18918355f576SJeff Roberson #ifdef UMA_DEBUG
18928355f576SJeff Roberson 	printf("Starting callout.\n");
18938355f576SJeff Roberson #endif
1894a3c07611SRobert Watson 	callout_init(&uma_callout, CALLOUT_MPSAFE);
18959643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
18968355f576SJeff Roberson #ifdef UMA_DEBUG
18978355f576SJeff Roberson 	printf("UMA startup3 complete.\n");
18988355f576SJeff Roberson #endif
18998355f576SJeff Roberson }
19008355f576SJeff Roberson 
1901e20a199fSJeff Roberson static uma_keg_t
1902099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
190385dcf349SGleb Smirnoff 		int align, uint32_t flags)
1904099a0e58SBosko Milekic {
1905099a0e58SBosko Milekic 	struct uma_kctor_args args;
1906099a0e58SBosko Milekic 
1907099a0e58SBosko Milekic 	args.size = size;
1908099a0e58SBosko Milekic 	args.uminit = uminit;
1909099a0e58SBosko Milekic 	args.fini = fini;
19101e319f6dSRobert Watson 	args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1911099a0e58SBosko Milekic 	args.flags = flags;
1912099a0e58SBosko Milekic 	args.zone = zone;
1913e20a199fSJeff Roberson 	return (zone_alloc_item(kegs, &args, M_WAITOK));
1914099a0e58SBosko Milekic }
1915099a0e58SBosko Milekic 
19168355f576SJeff Roberson /* See uma.h */
19171e319f6dSRobert Watson void
19181e319f6dSRobert Watson uma_set_align(int align)
19191e319f6dSRobert Watson {
19201e319f6dSRobert Watson 
19211e319f6dSRobert Watson 	if (align != UMA_ALIGN_CACHE)
19221e319f6dSRobert Watson 		uma_align_cache = align;
19231e319f6dSRobert Watson }
19241e319f6dSRobert Watson 
19251e319f6dSRobert Watson /* See uma.h */
19268355f576SJeff Roberson uma_zone_t
1927bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
192885dcf349SGleb Smirnoff 		uma_init uminit, uma_fini fini, int align, uint32_t flags)
19298355f576SJeff Roberson 
19308355f576SJeff Roberson {
19318355f576SJeff Roberson 	struct uma_zctor_args args;
193295c4bf75SKonstantin Belousov 	uma_zone_t res;
193395c4bf75SKonstantin Belousov 	bool locked;
19348355f576SJeff Roberson 
19358355f576SJeff Roberson 	/* This stuff is essential for the zone ctor */
19360095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
19378355f576SJeff Roberson 	args.name = name;
19388355f576SJeff Roberson 	args.size = size;
19398355f576SJeff Roberson 	args.ctor = ctor;
19408355f576SJeff Roberson 	args.dtor = dtor;
19418355f576SJeff Roberson 	args.uminit = uminit;
19428355f576SJeff Roberson 	args.fini = fini;
19438355f576SJeff Roberson 	args.align = align;
19448355f576SJeff Roberson 	args.flags = flags;
1945099a0e58SBosko Milekic 	args.keg = NULL;
1946099a0e58SBosko Milekic 
194795c4bf75SKonstantin Belousov 	if (booted < UMA_STARTUP2) {
194895c4bf75SKonstantin Belousov 		locked = false;
194995c4bf75SKonstantin Belousov 	} else {
195095c4bf75SKonstantin Belousov 		sx_slock(&uma_drain_lock);
195195c4bf75SKonstantin Belousov 		locked = true;
195295c4bf75SKonstantin Belousov 	}
195395c4bf75SKonstantin Belousov 	res = zone_alloc_item(zones, &args, M_WAITOK);
195495c4bf75SKonstantin Belousov 	if (locked)
195595c4bf75SKonstantin Belousov 		sx_sunlock(&uma_drain_lock);
195695c4bf75SKonstantin Belousov 	return (res);
1957099a0e58SBosko Milekic }
1958099a0e58SBosko Milekic 
1959099a0e58SBosko Milekic /* See uma.h */
1960099a0e58SBosko Milekic uma_zone_t
1961099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1962099a0e58SBosko Milekic 		    uma_init zinit, uma_fini zfini, uma_zone_t master)
1963099a0e58SBosko Milekic {
1964099a0e58SBosko Milekic 	struct uma_zctor_args args;
1965e20a199fSJeff Roberson 	uma_keg_t keg;
196695c4bf75SKonstantin Belousov 	uma_zone_t res;
196795c4bf75SKonstantin Belousov 	bool locked;
1968099a0e58SBosko Milekic 
1969e20a199fSJeff Roberson 	keg = zone_first_keg(master);
19700095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
1971099a0e58SBosko Milekic 	args.name = name;
1972e20a199fSJeff Roberson 	args.size = keg->uk_size;
1973099a0e58SBosko Milekic 	args.ctor = ctor;
1974099a0e58SBosko Milekic 	args.dtor = dtor;
1975099a0e58SBosko Milekic 	args.uminit = zinit;
1976099a0e58SBosko Milekic 	args.fini = zfini;
1977e20a199fSJeff Roberson 	args.align = keg->uk_align;
1978e20a199fSJeff Roberson 	args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
1979e20a199fSJeff Roberson 	args.keg = keg;
19808355f576SJeff Roberson 
198195c4bf75SKonstantin Belousov 	if (booted < UMA_STARTUP2) {
198295c4bf75SKonstantin Belousov 		locked = false;
198395c4bf75SKonstantin Belousov 	} else {
198495c4bf75SKonstantin Belousov 		sx_slock(&uma_drain_lock);
198595c4bf75SKonstantin Belousov 		locked = true;
198695c4bf75SKonstantin Belousov 	}
1987e20a199fSJeff Roberson 	/* XXX Attaches only one keg of potentially many. */
198895c4bf75SKonstantin Belousov 	res = zone_alloc_item(zones, &args, M_WAITOK);
198995c4bf75SKonstantin Belousov 	if (locked)
199095c4bf75SKonstantin Belousov 		sx_sunlock(&uma_drain_lock);
199195c4bf75SKonstantin Belousov 	return (res);
19928355f576SJeff Roberson }
19938355f576SJeff Roberson 
19940095a784SJeff Roberson /* See uma.h */
19950095a784SJeff Roberson uma_zone_t
1996af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
1997af526374SJeff Roberson 		    uma_init zinit, uma_fini zfini, uma_import zimport,
1998af526374SJeff Roberson 		    uma_release zrelease, void *arg, int flags)
19990095a784SJeff Roberson {
20000095a784SJeff Roberson 	struct uma_zctor_args args;
20010095a784SJeff Roberson 
20020095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
20030095a784SJeff Roberson 	args.name = name;
2004af526374SJeff Roberson 	args.size = size;
20050095a784SJeff Roberson 	args.ctor = ctor;
20060095a784SJeff Roberson 	args.dtor = dtor;
20070095a784SJeff Roberson 	args.uminit = zinit;
20080095a784SJeff Roberson 	args.fini = zfini;
20090095a784SJeff Roberson 	args.import = zimport;
20100095a784SJeff Roberson 	args.release = zrelease;
20110095a784SJeff Roberson 	args.arg = arg;
20120095a784SJeff Roberson 	args.align = 0;
20130095a784SJeff Roberson 	args.flags = flags;
20140095a784SJeff Roberson 
20150095a784SJeff Roberson 	return (zone_alloc_item(zones, &args, M_WAITOK));
20160095a784SJeff Roberson }
20170095a784SJeff Roberson 
2018e20a199fSJeff Roberson static void
2019e20a199fSJeff Roberson zone_lock_pair(uma_zone_t a, uma_zone_t b)
2020e20a199fSJeff Roberson {
2021e20a199fSJeff Roberson 	if (a < b) {
2022e20a199fSJeff Roberson 		ZONE_LOCK(a);
2023af526374SJeff Roberson 		mtx_lock_flags(b->uz_lockptr, MTX_DUPOK);
2024e20a199fSJeff Roberson 	} else {
2025e20a199fSJeff Roberson 		ZONE_LOCK(b);
2026af526374SJeff Roberson 		mtx_lock_flags(a->uz_lockptr, MTX_DUPOK);
2027e20a199fSJeff Roberson 	}
2028e20a199fSJeff Roberson }
2029e20a199fSJeff Roberson 
2030e20a199fSJeff Roberson static void
2031e20a199fSJeff Roberson zone_unlock_pair(uma_zone_t a, uma_zone_t b)
2032e20a199fSJeff Roberson {
2033e20a199fSJeff Roberson 
2034e20a199fSJeff Roberson 	ZONE_UNLOCK(a);
2035e20a199fSJeff Roberson 	ZONE_UNLOCK(b);
2036e20a199fSJeff Roberson }
2037e20a199fSJeff Roberson 
2038e20a199fSJeff Roberson int
2039e20a199fSJeff Roberson uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
2040e20a199fSJeff Roberson {
2041e20a199fSJeff Roberson 	uma_klink_t klink;
2042e20a199fSJeff Roberson 	uma_klink_t kl;
2043e20a199fSJeff Roberson 	int error;
2044e20a199fSJeff Roberson 
2045e20a199fSJeff Roberson 	error = 0;
2046e20a199fSJeff Roberson 	klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
2047e20a199fSJeff Roberson 
2048e20a199fSJeff Roberson 	zone_lock_pair(zone, master);
2049e20a199fSJeff Roberson 	/*
2050e20a199fSJeff Roberson 	 * zone must use vtoslab() to resolve objects and must already be
2051e20a199fSJeff Roberson 	 * a secondary.
2052e20a199fSJeff Roberson 	 */
2053e20a199fSJeff Roberson 	if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
2054e20a199fSJeff Roberson 	    != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
2055e20a199fSJeff Roberson 		error = EINVAL;
2056e20a199fSJeff Roberson 		goto out;
2057e20a199fSJeff Roberson 	}
2058e20a199fSJeff Roberson 	/*
2059e20a199fSJeff Roberson 	 * The new master must also use vtoslab().
2060e20a199fSJeff Roberson 	 */
2061e20a199fSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
2062e20a199fSJeff Roberson 		error = EINVAL;
2063e20a199fSJeff Roberson 		goto out;
2064e20a199fSJeff Roberson 	}
2065e20a199fSJeff Roberson 	/*
2066e20a199fSJeff Roberson 	 * Both must either be refcnt, or not be refcnt.
2067e20a199fSJeff Roberson 	 */
2068e20a199fSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_REFCNT) !=
2069e20a199fSJeff Roberson 	    (master->uz_flags & UMA_ZONE_REFCNT)) {
2070e20a199fSJeff Roberson 		error = EINVAL;
2071e20a199fSJeff Roberson 		goto out;
2072e20a199fSJeff Roberson 	}
2073e20a199fSJeff Roberson 	/*
2074e20a199fSJeff Roberson 	 * The underlying object must be the same size.  rsize
2075e20a199fSJeff Roberson 	 * may be different.
2076e20a199fSJeff Roberson 	 */
2077e20a199fSJeff Roberson 	if (master->uz_size != zone->uz_size) {
2078e20a199fSJeff Roberson 		error = E2BIG;
2079e20a199fSJeff Roberson 		goto out;
2080e20a199fSJeff Roberson 	}
2081e20a199fSJeff Roberson 	/*
2082e20a199fSJeff Roberson 	 * Put it at the end of the list.
2083e20a199fSJeff Roberson 	 */
2084e20a199fSJeff Roberson 	klink->kl_keg = zone_first_keg(master);
2085e20a199fSJeff Roberson 	LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
2086e20a199fSJeff Roberson 		if (LIST_NEXT(kl, kl_link) == NULL) {
2087e20a199fSJeff Roberson 			LIST_INSERT_AFTER(kl, klink, kl_link);
2088e20a199fSJeff Roberson 			break;
2089e20a199fSJeff Roberson 		}
2090e20a199fSJeff Roberson 	}
2091e20a199fSJeff Roberson 	klink = NULL;
2092e20a199fSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_MULTI;
2093e20a199fSJeff Roberson 	zone->uz_slab = zone_fetch_slab_multi;
2094e20a199fSJeff Roberson 
2095e20a199fSJeff Roberson out:
2096e20a199fSJeff Roberson 	zone_unlock_pair(zone, master);
2097e20a199fSJeff Roberson 	if (klink != NULL)
2098e20a199fSJeff Roberson 		free(klink, M_TEMP);
2099e20a199fSJeff Roberson 
2100e20a199fSJeff Roberson 	return (error);
2101e20a199fSJeff Roberson }
2102e20a199fSJeff Roberson 
2103e20a199fSJeff Roberson 
21048355f576SJeff Roberson /* See uma.h */
21059c2cd7e5SJeff Roberson void
21069c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone)
21079c2cd7e5SJeff Roberson {
2108f4ff923bSRobert Watson 
210995c4bf75SKonstantin Belousov 	sx_slock(&uma_drain_lock);
21100095a784SJeff Roberson 	zone_free_item(zones, zone, NULL, SKIP_NONE);
211195c4bf75SKonstantin Belousov 	sx_sunlock(&uma_drain_lock);
21129c2cd7e5SJeff Roberson }
21139c2cd7e5SJeff Roberson 
21149c2cd7e5SJeff Roberson /* See uma.h */
21158355f576SJeff Roberson void *
21162cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
21178355f576SJeff Roberson {
21188355f576SJeff Roberson 	void *item;
21198355f576SJeff Roberson 	uma_cache_t cache;
21208355f576SJeff Roberson 	uma_bucket_t bucket;
2121fc03d22bSJeff Roberson 	int lockfail;
21228355f576SJeff Roberson 	int cpu;
21238355f576SJeff Roberson 
212410cb2424SMark Murray #if 0
212510cb2424SMark Murray 	/* XXX: FIX!! Do not enable this in CURRENT!! MarkM */
212610cb2424SMark Murray 	/* The entropy here is desirable, but the harvesting is expensive */
212710cb2424SMark Murray 	random_harvest(&(zone->uz_name), sizeof(void *), 1, RANDOM_UMA_ALLOC);
212810cb2424SMark Murray #endif
212910cb2424SMark Murray 
21308355f576SJeff Roberson 	/* This is the fast path allocation */
21318355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1
21328355f576SJeff Roberson 	printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
21338355f576SJeff Roberson #endif
21343659f747SRobert Watson 	CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
21353659f747SRobert Watson 	    zone->uz_name, flags);
2136a553d4b8SJeff Roberson 
2137635fd505SRobert Watson 	if (flags & M_WAITOK) {
2138b23f72e9SBrian Feldman 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2139635fd505SRobert Watson 		    "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
21404c1cc01cSJohn Baldwin 	}
21418d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
21428d689e04SGleb Smirnoff 	if (memguard_cmp_zone(zone)) {
21438d689e04SGleb Smirnoff 		item = memguard_alloc(zone->uz_size, flags);
21448d689e04SGleb Smirnoff 		if (item != NULL) {
21458d689e04SGleb Smirnoff 			/*
21468d689e04SGleb Smirnoff 			 * Avoid conflict with the use-after-free
21478d689e04SGleb Smirnoff 			 * protecting infrastructure from INVARIANTS.
21488d689e04SGleb Smirnoff 			 */
21498d689e04SGleb Smirnoff 			if (zone->uz_init != NULL &&
21508d689e04SGleb Smirnoff 			    zone->uz_init != mtrash_init &&
21518d689e04SGleb Smirnoff 			    zone->uz_init(item, zone->uz_size, flags) != 0)
21528d689e04SGleb Smirnoff 				return (NULL);
21538d689e04SGleb Smirnoff 			if (zone->uz_ctor != NULL &&
21548d689e04SGleb Smirnoff 			    zone->uz_ctor != mtrash_ctor &&
2155fc03d22bSJeff Roberson 			    zone->uz_ctor(item, zone->uz_size, udata,
2156fc03d22bSJeff Roberson 			    flags) != 0) {
21578d689e04SGleb Smirnoff 			    	zone->uz_fini(item, zone->uz_size);
21588d689e04SGleb Smirnoff 				return (NULL);
21598d689e04SGleb Smirnoff 			}
216010cb2424SMark Murray #if 0
216110cb2424SMark Murray 			/* XXX: FIX!! Do not enable this in CURRENT!! MarkM */
216210cb2424SMark Murray 			/* The entropy here is desirable, but the harvesting is expensive */
216310cb2424SMark Murray 			random_harvest(&item, sizeof(void *), 1, RANDOM_UMA_ALLOC);
216410cb2424SMark Murray #endif
21658d689e04SGleb Smirnoff 			return (item);
21668d689e04SGleb Smirnoff 		}
21678d689e04SGleb Smirnoff 		/* This is unfortunate but should not be fatal. */
21688d689e04SGleb Smirnoff 	}
21698d689e04SGleb Smirnoff #endif
21705d1ae027SRobert Watson 	/*
21715d1ae027SRobert Watson 	 * If possible, allocate from the per-CPU cache.  There are two
21725d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
21735d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
21745d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
21755d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
21765d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
21775d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to allocate from
21785d1ae027SRobert Watson 	 * the current cache; when we re-acquire the critical section, we
21795d1ae027SRobert Watson 	 * must detect and handle migration if it has occurred.
21805d1ae027SRobert Watson 	 */
21815d1ae027SRobert Watson 	critical_enter();
21825d1ae027SRobert Watson 	cpu = curcpu;
21838355f576SJeff Roberson 	cache = &zone->uz_cpu[cpu];
21848355f576SJeff Roberson 
21858355f576SJeff Roberson zalloc_start:
21868355f576SJeff Roberson 	bucket = cache->uc_allocbucket;
2187fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt > 0) {
2188cae33c14SJeff Roberson 		bucket->ub_cnt--;
2189cae33c14SJeff Roberson 		item = bucket->ub_bucket[bucket->ub_cnt];
21908355f576SJeff Roberson #ifdef INVARIANTS
2191cae33c14SJeff Roberson 		bucket->ub_bucket[bucket->ub_cnt] = NULL;
21928355f576SJeff Roberson #endif
2193fc03d22bSJeff Roberson 		KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
21948355f576SJeff Roberson 		cache->uc_allocs++;
21955d1ae027SRobert Watson 		critical_exit();
2196fc03d22bSJeff Roberson 		if (zone->uz_ctor != NULL &&
2197fc03d22bSJeff Roberson 		    zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
21980095a784SJeff Roberson 			atomic_add_long(&zone->uz_fails, 1);
2199fc03d22bSJeff Roberson 			zone_free_item(zone, item, udata, SKIP_DTOR);
2200b23f72e9SBrian Feldman 			return (NULL);
2201b23f72e9SBrian Feldman 		}
2202ef72505eSJeff Roberson #ifdef INVARIANTS
2203ef72505eSJeff Roberson 		uma_dbg_alloc(zone, NULL, item);
2204ef72505eSJeff Roberson #endif
22052cc35ff9SJeff Roberson 		if (flags & M_ZERO)
220648343a2fSGleb Smirnoff 			uma_zero_item(item, zone);
220710cb2424SMark Murray #if 0
220810cb2424SMark Murray 		/* XXX: FIX!! Do not enable this in CURRENT!! MarkM */
220910cb2424SMark Murray 		/* The entropy here is desirable, but the harvesting is expensive */
221010cb2424SMark Murray 		random_harvest(&item, sizeof(void *), 1, RANDOM_UMA_ALLOC);
221110cb2424SMark Murray #endif
22128355f576SJeff Roberson 		return (item);
2213fc03d22bSJeff Roberson 	}
2214fc03d22bSJeff Roberson 
22158355f576SJeff Roberson 	/*
22168355f576SJeff Roberson 	 * We have run out of items in our alloc bucket.
22178355f576SJeff Roberson 	 * See if we can switch with our free bucket.
22188355f576SJeff Roberson 	 */
2219b983089aSJeff Roberson 	bucket = cache->uc_freebucket;
2220fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt > 0) {
2221fc03d22bSJeff Roberson #ifdef UMA_DEBUG_ALLOC
2222fc03d22bSJeff Roberson 		printf("uma_zalloc: Swapping empty with alloc.\n");
2223fc03d22bSJeff Roberson #endif
22248355f576SJeff Roberson 		cache->uc_freebucket = cache->uc_allocbucket;
2225b983089aSJeff Roberson 		cache->uc_allocbucket = bucket;
22268355f576SJeff Roberson 		goto zalloc_start;
22278355f576SJeff Roberson 	}
2228fc03d22bSJeff Roberson 
2229fc03d22bSJeff Roberson 	/*
2230fc03d22bSJeff Roberson 	 * Discard any empty allocation bucket while we hold no locks.
2231fc03d22bSJeff Roberson 	 */
2232fc03d22bSJeff Roberson 	bucket = cache->uc_allocbucket;
2233fc03d22bSJeff Roberson 	cache->uc_allocbucket = NULL;
2234fc03d22bSJeff Roberson 	critical_exit();
2235fc03d22bSJeff Roberson 	if (bucket != NULL)
22366fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
2237fc03d22bSJeff Roberson 
2238fc03d22bSJeff Roberson 	/* Short-circuit for zones without buckets and low memory. */
2239fc03d22bSJeff Roberson 	if (zone->uz_count == 0 || bucketdisable)
2240fc03d22bSJeff Roberson 		goto zalloc_item;
2241fc03d22bSJeff Roberson 
22425d1ae027SRobert Watson 	/*
22435d1ae027SRobert Watson 	 * Attempt to retrieve the item from the per-CPU cache has failed, so
22445d1ae027SRobert Watson 	 * we must go back to the zone.  This requires the zone lock, so we
22455d1ae027SRobert Watson 	 * must drop the critical section, then re-acquire it when we go back
22465d1ae027SRobert Watson 	 * to the cache.  Since the critical section is released, we may be
22475d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
22485d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
22495d1ae027SRobert Watson 	 * the critical section.
22505d1ae027SRobert Watson 	 */
2251fc03d22bSJeff Roberson 	lockfail = 0;
2252fc03d22bSJeff Roberson 	if (ZONE_TRYLOCK(zone) == 0) {
2253fc03d22bSJeff Roberson 		/* Record contention to size the buckets. */
2254a553d4b8SJeff Roberson 		ZONE_LOCK(zone);
2255fc03d22bSJeff Roberson 		lockfail = 1;
2256fc03d22bSJeff Roberson 	}
22575d1ae027SRobert Watson 	critical_enter();
22585d1ae027SRobert Watson 	cpu = curcpu;
22595d1ae027SRobert Watson 	cache = &zone->uz_cpu[cpu];
22605d1ae027SRobert Watson 
2261fc03d22bSJeff Roberson 	/*
2262fc03d22bSJeff Roberson 	 * Since we have locked the zone we may as well send back our stats.
2263fc03d22bSJeff Roberson 	 */
22640095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
22650095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, cache->uc_frees);
2266a553d4b8SJeff Roberson 	cache->uc_allocs = 0;
2267773df9abSRobert Watson 	cache->uc_frees = 0;
22688355f576SJeff Roberson 
2269fc03d22bSJeff Roberson 	/* See if we lost the race to fill the cache. */
2270fc03d22bSJeff Roberson 	if (cache->uc_allocbucket != NULL) {
2271fc03d22bSJeff Roberson 		ZONE_UNLOCK(zone);
2272fc03d22bSJeff Roberson 		goto zalloc_start;
2273a553d4b8SJeff Roberson 	}
22748355f576SJeff Roberson 
2275fc03d22bSJeff Roberson 	/*
2276fc03d22bSJeff Roberson 	 * Check the zone's cache of buckets.
2277fc03d22bSJeff Roberson 	 */
2278fc03d22bSJeff Roberson 	if ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
2279cae33c14SJeff Roberson 		KASSERT(bucket->ub_cnt != 0,
2280a553d4b8SJeff Roberson 		    ("uma_zalloc_arg: Returning an empty bucket."));
22818355f576SJeff Roberson 
2282a553d4b8SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
2283a553d4b8SJeff Roberson 		cache->uc_allocbucket = bucket;
2284a553d4b8SJeff Roberson 		ZONE_UNLOCK(zone);
22858355f576SJeff Roberson 		goto zalloc_start;
2286a553d4b8SJeff Roberson 	}
22875d1ae027SRobert Watson 	/* We are no longer associated with this CPU. */
22885d1ae027SRobert Watson 	critical_exit();
2289bbee39c6SJeff Roberson 
2290fc03d22bSJeff Roberson 	/*
2291fc03d22bSJeff Roberson 	 * We bump the uz count when the cache size is insufficient to
2292fc03d22bSJeff Roberson 	 * handle the working set.
2293fc03d22bSJeff Roberson 	 */
22946fd34d6fSJeff Roberson 	if (lockfail && zone->uz_count < BUCKET_MAX)
2295a553d4b8SJeff Roberson 		zone->uz_count++;
2296fc03d22bSJeff Roberson 	ZONE_UNLOCK(zone);
2297099a0e58SBosko Milekic 
22988355f576SJeff Roberson 	/*
2299a553d4b8SJeff Roberson 	 * Now lets just fill a bucket and put it on the free list.  If that
2300fc03d22bSJeff Roberson 	 * works we'll restart the allocation from the begining and it
2301fc03d22bSJeff Roberson 	 * will use the just filled bucket.
2302bbee39c6SJeff Roberson 	 */
23036fd34d6fSJeff Roberson 	bucket = zone_alloc_bucket(zone, udata, flags);
2304fc03d22bSJeff Roberson 	if (bucket != NULL) {
2305fc03d22bSJeff Roberson 		ZONE_LOCK(zone);
2306fc03d22bSJeff Roberson 		critical_enter();
2307fc03d22bSJeff Roberson 		cpu = curcpu;
2308fc03d22bSJeff Roberson 		cache = &zone->uz_cpu[cpu];
2309fc03d22bSJeff Roberson 		/*
2310fc03d22bSJeff Roberson 		 * See if we lost the race or were migrated.  Cache the
2311fc03d22bSJeff Roberson 		 * initialized bucket to make this less likely or claim
2312fc03d22bSJeff Roberson 		 * the memory directly.
2313fc03d22bSJeff Roberson 		 */
2314fc03d22bSJeff Roberson 		if (cache->uc_allocbucket == NULL)
2315fc03d22bSJeff Roberson 			cache->uc_allocbucket = bucket;
2316fc03d22bSJeff Roberson 		else
2317fc03d22bSJeff Roberson 			LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
2318bbee39c6SJeff Roberson 		ZONE_UNLOCK(zone);
2319fc03d22bSJeff Roberson 		goto zalloc_start;
2320bbee39c6SJeff Roberson 	}
2321fc03d22bSJeff Roberson 
2322bbee39c6SJeff Roberson 	/*
2323bbee39c6SJeff Roberson 	 * We may not be able to get a bucket so return an actual item.
2324bbee39c6SJeff Roberson 	 */
2325bbee39c6SJeff Roberson #ifdef UMA_DEBUG
2326bbee39c6SJeff Roberson 	printf("uma_zalloc_arg: Bucketzone returned NULL\n");
2327bbee39c6SJeff Roberson #endif
2328bbee39c6SJeff Roberson 
2329fc03d22bSJeff Roberson zalloc_item:
2330e20a199fSJeff Roberson 	item = zone_alloc_item(zone, udata, flags);
2331fc03d22bSJeff Roberson 
233210cb2424SMark Murray #if 0
233310cb2424SMark Murray 	/* XXX: FIX!! Do not enable this in CURRENT!! MarkM */
233410cb2424SMark Murray 	/* The entropy here is desirable, but the harvesting is expensive */
233510cb2424SMark Murray 	random_harvest(&item, sizeof(void *), 1, RANDOM_UMA_ALLOC);
233610cb2424SMark Murray #endif
2337e20a199fSJeff Roberson 	return (item);
2338bbee39c6SJeff Roberson }
2339bbee39c6SJeff Roberson 
2340bbee39c6SJeff Roberson static uma_slab_t
2341e20a199fSJeff Roberson keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags)
2342bbee39c6SJeff Roberson {
2343bbee39c6SJeff Roberson 	uma_slab_t slab;
23446fd34d6fSJeff Roberson 	int reserve;
2345099a0e58SBosko Milekic 
2346e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2347bbee39c6SJeff Roberson 	slab = NULL;
23486fd34d6fSJeff Roberson 	reserve = 0;
23496fd34d6fSJeff Roberson 	if ((flags & M_USE_RESERVE) == 0)
23506fd34d6fSJeff Roberson 		reserve = keg->uk_reserve;
2351bbee39c6SJeff Roberson 
2352bbee39c6SJeff Roberson 	for (;;) {
2353bbee39c6SJeff Roberson 		/*
2354bbee39c6SJeff Roberson 		 * Find a slab with some space.  Prefer slabs that are partially
2355bbee39c6SJeff Roberson 		 * used over those that are totally full.  This helps to reduce
2356bbee39c6SJeff Roberson 		 * fragmentation.
2357bbee39c6SJeff Roberson 		 */
23586fd34d6fSJeff Roberson 		if (keg->uk_free > reserve) {
2359099a0e58SBosko Milekic 			if (!LIST_EMPTY(&keg->uk_part_slab)) {
2360099a0e58SBosko Milekic 				slab = LIST_FIRST(&keg->uk_part_slab);
2361bbee39c6SJeff Roberson 			} else {
2362099a0e58SBosko Milekic 				slab = LIST_FIRST(&keg->uk_free_slab);
2363bbee39c6SJeff Roberson 				LIST_REMOVE(slab, us_link);
2364099a0e58SBosko Milekic 				LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2365bbee39c6SJeff Roberson 				    us_link);
2366bbee39c6SJeff Roberson 			}
2367e20a199fSJeff Roberson 			MPASS(slab->us_keg == keg);
2368bbee39c6SJeff Roberson 			return (slab);
2369bbee39c6SJeff Roberson 		}
2370bbee39c6SJeff Roberson 
2371bbee39c6SJeff Roberson 		/*
2372bbee39c6SJeff Roberson 		 * M_NOVM means don't ask at all!
2373bbee39c6SJeff Roberson 		 */
2374bbee39c6SJeff Roberson 		if (flags & M_NOVM)
2375bbee39c6SJeff Roberson 			break;
2376bbee39c6SJeff Roberson 
2377e20a199fSJeff Roberson 		if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2378099a0e58SBosko Milekic 			keg->uk_flags |= UMA_ZFLAG_FULL;
2379e20a199fSJeff Roberson 			/*
2380e20a199fSJeff Roberson 			 * If this is not a multi-zone, set the FULL bit.
2381e20a199fSJeff Roberson 			 * Otherwise slab_multi() takes care of it.
2382e20a199fSJeff Roberson 			 */
23832f891cd5SPawel Jakub Dawidek 			if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) {
2384e20a199fSJeff Roberson 				zone->uz_flags |= UMA_ZFLAG_FULL;
23852f891cd5SPawel Jakub Dawidek 				zone_log_warning(zone);
23862f891cd5SPawel Jakub Dawidek 			}
2387ebc85edfSJeff Roberson 			if (flags & M_NOWAIT)
2388bbee39c6SJeff Roberson 				break;
2389c288b548SEitan Adler 			zone->uz_sleeps++;
2390e20a199fSJeff Roberson 			msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2391bbee39c6SJeff Roberson 			continue;
2392bbee39c6SJeff Roberson 		}
2393e20a199fSJeff Roberson 		slab = keg_alloc_slab(keg, zone, flags);
2394bbee39c6SJeff Roberson 		/*
2395bbee39c6SJeff Roberson 		 * If we got a slab here it's safe to mark it partially used
2396bbee39c6SJeff Roberson 		 * and return.  We assume that the caller is going to remove
2397bbee39c6SJeff Roberson 		 * at least one item.
2398bbee39c6SJeff Roberson 		 */
2399bbee39c6SJeff Roberson 		if (slab) {
2400e20a199fSJeff Roberson 			MPASS(slab->us_keg == keg);
2401099a0e58SBosko Milekic 			LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2402bbee39c6SJeff Roberson 			return (slab);
2403bbee39c6SJeff Roberson 		}
2404bbee39c6SJeff Roberson 		/*
2405bbee39c6SJeff Roberson 		 * We might not have been able to get a slab but another cpu
2406bbee39c6SJeff Roberson 		 * could have while we were unlocked.  Check again before we
2407bbee39c6SJeff Roberson 		 * fail.
2408bbee39c6SJeff Roberson 		 */
2409bbee39c6SJeff Roberson 		flags |= M_NOVM;
2410bbee39c6SJeff Roberson 	}
2411bbee39c6SJeff Roberson 	return (slab);
2412bbee39c6SJeff Roberson }
2413bbee39c6SJeff Roberson 
2414e20a199fSJeff Roberson static uma_slab_t
2415e20a199fSJeff Roberson zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags)
2416e20a199fSJeff Roberson {
2417e20a199fSJeff Roberson 	uma_slab_t slab;
2418e20a199fSJeff Roberson 
2419af526374SJeff Roberson 	if (keg == NULL) {
2420e20a199fSJeff Roberson 		keg = zone_first_keg(zone);
2421af526374SJeff Roberson 		KEG_LOCK(keg);
2422af526374SJeff Roberson 	}
2423e20a199fSJeff Roberson 
2424e20a199fSJeff Roberson 	for (;;) {
2425e20a199fSJeff Roberson 		slab = keg_fetch_slab(keg, zone, flags);
2426e20a199fSJeff Roberson 		if (slab)
2427e20a199fSJeff Roberson 			return (slab);
2428e20a199fSJeff Roberson 		if (flags & (M_NOWAIT | M_NOVM))
2429e20a199fSJeff Roberson 			break;
2430e20a199fSJeff Roberson 	}
2431af526374SJeff Roberson 	KEG_UNLOCK(keg);
2432e20a199fSJeff Roberson 	return (NULL);
2433e20a199fSJeff Roberson }
2434e20a199fSJeff Roberson 
2435e20a199fSJeff Roberson /*
2436e20a199fSJeff Roberson  * uma_zone_fetch_slab_multi:  Fetches a slab from one available keg.  Returns
2437af526374SJeff Roberson  * with the keg locked.  On NULL no lock is held.
2438e20a199fSJeff Roberson  *
2439e20a199fSJeff Roberson  * The last pointer is used to seed the search.  It is not required.
2440e20a199fSJeff Roberson  */
2441e20a199fSJeff Roberson static uma_slab_t
2442e20a199fSJeff Roberson zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags)
2443e20a199fSJeff Roberson {
2444e20a199fSJeff Roberson 	uma_klink_t klink;
2445e20a199fSJeff Roberson 	uma_slab_t slab;
2446e20a199fSJeff Roberson 	uma_keg_t keg;
2447e20a199fSJeff Roberson 	int flags;
2448e20a199fSJeff Roberson 	int empty;
2449e20a199fSJeff Roberson 	int full;
2450e20a199fSJeff Roberson 
2451e20a199fSJeff Roberson 	/*
2452e20a199fSJeff Roberson 	 * Don't wait on the first pass.  This will skip limit tests
2453e20a199fSJeff Roberson 	 * as well.  We don't want to block if we can find a provider
2454e20a199fSJeff Roberson 	 * without blocking.
2455e20a199fSJeff Roberson 	 */
2456e20a199fSJeff Roberson 	flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2457e20a199fSJeff Roberson 	/*
2458e20a199fSJeff Roberson 	 * Use the last slab allocated as a hint for where to start
2459e20a199fSJeff Roberson 	 * the search.
2460e20a199fSJeff Roberson 	 */
2461af526374SJeff Roberson 	if (last != NULL) {
2462e20a199fSJeff Roberson 		slab = keg_fetch_slab(last, zone, flags);
2463e20a199fSJeff Roberson 		if (slab)
2464e20a199fSJeff Roberson 			return (slab);
2465af526374SJeff Roberson 		KEG_UNLOCK(last);
2466e20a199fSJeff Roberson 	}
2467e20a199fSJeff Roberson 	/*
2468e20a199fSJeff Roberson 	 * Loop until we have a slab incase of transient failures
2469e20a199fSJeff Roberson 	 * while M_WAITOK is specified.  I'm not sure this is 100%
2470e20a199fSJeff Roberson 	 * required but we've done it for so long now.
2471e20a199fSJeff Roberson 	 */
2472e20a199fSJeff Roberson 	for (;;) {
2473e20a199fSJeff Roberson 		empty = 0;
2474e20a199fSJeff Roberson 		full = 0;
2475e20a199fSJeff Roberson 		/*
2476e20a199fSJeff Roberson 		 * Search the available kegs for slabs.  Be careful to hold the
2477e20a199fSJeff Roberson 		 * correct lock while calling into the keg layer.
2478e20a199fSJeff Roberson 		 */
2479e20a199fSJeff Roberson 		LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2480e20a199fSJeff Roberson 			keg = klink->kl_keg;
2481af526374SJeff Roberson 			KEG_LOCK(keg);
2482e20a199fSJeff Roberson 			if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2483e20a199fSJeff Roberson 				slab = keg_fetch_slab(keg, zone, flags);
2484e20a199fSJeff Roberson 				if (slab)
2485e20a199fSJeff Roberson 					return (slab);
2486e20a199fSJeff Roberson 			}
2487e20a199fSJeff Roberson 			if (keg->uk_flags & UMA_ZFLAG_FULL)
2488e20a199fSJeff Roberson 				full++;
2489e20a199fSJeff Roberson 			else
2490e20a199fSJeff Roberson 				empty++;
2491af526374SJeff Roberson 			KEG_UNLOCK(keg);
2492e20a199fSJeff Roberson 		}
2493e20a199fSJeff Roberson 		if (rflags & (M_NOWAIT | M_NOVM))
2494e20a199fSJeff Roberson 			break;
2495e20a199fSJeff Roberson 		flags = rflags;
2496e20a199fSJeff Roberson 		/*
2497e20a199fSJeff Roberson 		 * All kegs are full.  XXX We can't atomically check all kegs
2498e20a199fSJeff Roberson 		 * and sleep so just sleep for a short period and retry.
2499e20a199fSJeff Roberson 		 */
2500e20a199fSJeff Roberson 		if (full && !empty) {
2501af526374SJeff Roberson 			ZONE_LOCK(zone);
2502e20a199fSJeff Roberson 			zone->uz_flags |= UMA_ZFLAG_FULL;
2503bf965959SSean Bruno 			zone->uz_sleeps++;
25042f891cd5SPawel Jakub Dawidek 			zone_log_warning(zone);
2505af526374SJeff Roberson 			msleep(zone, zone->uz_lockptr, PVM,
2506af526374SJeff Roberson 			    "zonelimit", hz/100);
2507e20a199fSJeff Roberson 			zone->uz_flags &= ~UMA_ZFLAG_FULL;
2508af526374SJeff Roberson 			ZONE_UNLOCK(zone);
2509e20a199fSJeff Roberson 			continue;
2510e20a199fSJeff Roberson 		}
2511e20a199fSJeff Roberson 	}
2512e20a199fSJeff Roberson 	return (NULL);
2513e20a199fSJeff Roberson }
2514e20a199fSJeff Roberson 
2515d56368d7SBosko Milekic static void *
25160095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
2517bbee39c6SJeff Roberson {
2518bbee39c6SJeff Roberson 	void *item;
251985dcf349SGleb Smirnoff 	uint8_t freei;
2520bbee39c6SJeff Roberson 
25210095a784SJeff Roberson 	MPASS(keg == slab->us_keg);
2522e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2523099a0e58SBosko Milekic 
2524ef72505eSJeff Roberson 	freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2525ef72505eSJeff Roberson 	BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2526099a0e58SBosko Milekic 	item = slab->us_data + (keg->uk_rsize * freei);
2527bbee39c6SJeff Roberson 	slab->us_freecount--;
2528099a0e58SBosko Milekic 	keg->uk_free--;
2529ef72505eSJeff Roberson 
2530bbee39c6SJeff Roberson 	/* Move this slab to the full list */
2531bbee39c6SJeff Roberson 	if (slab->us_freecount == 0) {
2532bbee39c6SJeff Roberson 		LIST_REMOVE(slab, us_link);
2533099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2534bbee39c6SJeff Roberson 	}
2535bbee39c6SJeff Roberson 
2536bbee39c6SJeff Roberson 	return (item);
2537bbee39c6SJeff Roberson }
2538bbee39c6SJeff Roberson 
2539bbee39c6SJeff Roberson static int
25400095a784SJeff Roberson zone_import(uma_zone_t zone, void **bucket, int max, int flags)
25410095a784SJeff Roberson {
25420095a784SJeff Roberson 	uma_slab_t slab;
25430095a784SJeff Roberson 	uma_keg_t keg;
25440095a784SJeff Roberson 	int i;
25450095a784SJeff Roberson 
25460095a784SJeff Roberson 	slab = NULL;
25470095a784SJeff Roberson 	keg = NULL;
2548af526374SJeff Roberson 	/* Try to keep the buckets totally full */
25490095a784SJeff Roberson 	for (i = 0; i < max; ) {
25500095a784SJeff Roberson 		if ((slab = zone->uz_slab(zone, keg, flags)) == NULL)
25510095a784SJeff Roberson 			break;
25520095a784SJeff Roberson 		keg = slab->us_keg;
25536fd34d6fSJeff Roberson 		while (slab->us_freecount && i < max) {
25540095a784SJeff Roberson 			bucket[i++] = slab_alloc_item(keg, slab);
25556fd34d6fSJeff Roberson 			if (keg->uk_free <= keg->uk_reserve)
25566fd34d6fSJeff Roberson 				break;
25576fd34d6fSJeff Roberson 		}
25586fd34d6fSJeff Roberson 		/* Don't grab more than one slab at a time. */
25590095a784SJeff Roberson 		flags &= ~M_WAITOK;
25600095a784SJeff Roberson 		flags |= M_NOWAIT;
25610095a784SJeff Roberson 	}
25620095a784SJeff Roberson 	if (slab != NULL)
25630095a784SJeff Roberson 		KEG_UNLOCK(keg);
25640095a784SJeff Roberson 
25650095a784SJeff Roberson 	return i;
25660095a784SJeff Roberson }
25670095a784SJeff Roberson 
2568fc03d22bSJeff Roberson static uma_bucket_t
25696fd34d6fSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int flags)
2570bbee39c6SJeff Roberson {
2571bbee39c6SJeff Roberson 	uma_bucket_t bucket;
25720095a784SJeff Roberson 	int max;
2573bbee39c6SJeff Roberson 
25746fd34d6fSJeff Roberson 	/* Don't wait for buckets, preserve caller's NOVM setting. */
25756fd34d6fSJeff Roberson 	bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
25760095a784SJeff Roberson 	if (bucket == NULL)
2577f7104ccdSAlexander Motin 		return (NULL);
25780095a784SJeff Roberson 
2579af526374SJeff Roberson 	max = MIN(bucket->ub_entries, zone->uz_count);
25800095a784SJeff Roberson 	bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
25810095a784SJeff Roberson 	    max, flags);
25820095a784SJeff Roberson 
25830095a784SJeff Roberson 	/*
25840095a784SJeff Roberson 	 * Initialize the memory if necessary.
25850095a784SJeff Roberson 	 */
25860095a784SJeff Roberson 	if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
2587099a0e58SBosko Milekic 		int i;
2588bbee39c6SJeff Roberson 
25890095a784SJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
2590e20a199fSJeff Roberson 			if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
25910095a784SJeff Roberson 			    flags) != 0)
2592b23f72e9SBrian Feldman 				break;
2593b23f72e9SBrian Feldman 		/*
2594b23f72e9SBrian Feldman 		 * If we couldn't initialize the whole bucket, put the
2595b23f72e9SBrian Feldman 		 * rest back onto the freelist.
2596b23f72e9SBrian Feldman 		 */
2597b23f72e9SBrian Feldman 		if (i != bucket->ub_cnt) {
2598af526374SJeff Roberson 			zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
25990095a784SJeff Roberson 			    bucket->ub_cnt - i);
2600a5a262c6SBosko Milekic #ifdef INVARIANTS
26010095a784SJeff Roberson 			bzero(&bucket->ub_bucket[i],
26020095a784SJeff Roberson 			    sizeof(void *) * (bucket->ub_cnt - i));
2603a5a262c6SBosko Milekic #endif
2604b23f72e9SBrian Feldman 			bucket->ub_cnt = i;
2605b23f72e9SBrian Feldman 		}
2606099a0e58SBosko Milekic 	}
2607099a0e58SBosko Milekic 
2608f7104ccdSAlexander Motin 	if (bucket->ub_cnt == 0) {
26096fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
2610fc03d22bSJeff Roberson 		atomic_add_long(&zone->uz_fails, 1);
2611fc03d22bSJeff Roberson 		return (NULL);
2612bbee39c6SJeff Roberson 	}
2613fc03d22bSJeff Roberson 
2614fc03d22bSJeff Roberson 	return (bucket);
2615fc03d22bSJeff Roberson }
2616fc03d22bSJeff Roberson 
26178355f576SJeff Roberson /*
26180095a784SJeff Roberson  * Allocates a single item from a zone.
26198355f576SJeff Roberson  *
26208355f576SJeff Roberson  * Arguments
26218355f576SJeff Roberson  *	zone   The zone to alloc for.
26228355f576SJeff Roberson  *	udata  The data to be passed to the constructor.
2623a163d034SWarner Losh  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
26248355f576SJeff Roberson  *
26258355f576SJeff Roberson  * Returns
26268355f576SJeff Roberson  *	NULL if there is no memory and M_NOWAIT is set
2627bbee39c6SJeff Roberson  *	An item if successful
26288355f576SJeff Roberson  */
26298355f576SJeff Roberson 
26308355f576SJeff Roberson static void *
2631e20a199fSJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int flags)
26328355f576SJeff Roberson {
26338355f576SJeff Roberson 	void *item;
26348355f576SJeff Roberson 
26358355f576SJeff Roberson 	item = NULL;
26368355f576SJeff Roberson 
26378355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
26388355f576SJeff Roberson 	printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
26398355f576SJeff Roberson #endif
26400095a784SJeff Roberson 	if (zone->uz_import(zone->uz_arg, &item, 1, flags) != 1)
26410095a784SJeff Roberson 		goto fail;
26420095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, 1);
26438355f576SJeff Roberson 
2644099a0e58SBosko Milekic 	/*
2645099a0e58SBosko Milekic 	 * We have to call both the zone's init (not the keg's init)
2646099a0e58SBosko Milekic 	 * and the zone's ctor.  This is because the item is going from
2647099a0e58SBosko Milekic 	 * a keg slab directly to the user, and the user is expecting it
2648099a0e58SBosko Milekic 	 * to be both zone-init'd as well as zone-ctor'd.
2649099a0e58SBosko Milekic 	 */
2650b23f72e9SBrian Feldman 	if (zone->uz_init != NULL) {
2651e20a199fSJeff Roberson 		if (zone->uz_init(item, zone->uz_size, flags) != 0) {
26520095a784SJeff Roberson 			zone_free_item(zone, item, udata, SKIP_FINI);
26530095a784SJeff Roberson 			goto fail;
2654b23f72e9SBrian Feldman 		}
2655b23f72e9SBrian Feldman 	}
2656b23f72e9SBrian Feldman 	if (zone->uz_ctor != NULL) {
2657e20a199fSJeff Roberson 		if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
26580095a784SJeff Roberson 			zone_free_item(zone, item, udata, SKIP_DTOR);
26590095a784SJeff Roberson 			goto fail;
2660b23f72e9SBrian Feldman 		}
2661b23f72e9SBrian Feldman 	}
2662ef72505eSJeff Roberson #ifdef INVARIANTS
26630095a784SJeff Roberson 	uma_dbg_alloc(zone, NULL, item);
2664ef72505eSJeff Roberson #endif
26652cc35ff9SJeff Roberson 	if (flags & M_ZERO)
266648343a2fSGleb Smirnoff 		uma_zero_item(item, zone);
26678355f576SJeff Roberson 
26688355f576SJeff Roberson 	return (item);
26690095a784SJeff Roberson 
26700095a784SJeff Roberson fail:
26710095a784SJeff Roberson 	atomic_add_long(&zone->uz_fails, 1);
26720095a784SJeff Roberson 	return (NULL);
26738355f576SJeff Roberson }
26748355f576SJeff Roberson 
26758355f576SJeff Roberson /* See uma.h */
26768355f576SJeff Roberson void
26778355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
26788355f576SJeff Roberson {
26798355f576SJeff Roberson 	uma_cache_t cache;
26808355f576SJeff Roberson 	uma_bucket_t bucket;
26814d104ba0SAlexander Motin 	int lockfail;
26828355f576SJeff Roberson 	int cpu;
26838355f576SJeff Roberson 
268410cb2424SMark Murray #if 0
268510cb2424SMark Murray 	/* XXX: FIX!! Do not enable this in CURRENT!! MarkM */
268610cb2424SMark Murray 	/* The entropy here is desirable, but the harvesting is expensive */
268710cb2424SMark Murray 	struct entropy {
268810cb2424SMark Murray 		const void *uz_name;
268910cb2424SMark Murray 		const void *item;
269010cb2424SMark Murray 	} entropy;
269110cb2424SMark Murray 
269210cb2424SMark Murray 	entropy.uz_name = zone->uz_name;
269310cb2424SMark Murray 	entropy.item = item;
269410cb2424SMark Murray 	random_harvest(&entropy, sizeof(struct entropy), 2, RANDOM_UMA_ALLOC);
269510cb2424SMark Murray #endif
269610cb2424SMark Murray 
26978355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1
26988355f576SJeff Roberson 	printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
26998355f576SJeff Roberson #endif
27003659f747SRobert Watson 	CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
27013659f747SRobert Watson 	    zone->uz_name);
27023659f747SRobert Watson 
270320ed0cb0SMatthew D Fleming         /* uma_zfree(..., NULL) does nothing, to match free(9). */
270420ed0cb0SMatthew D Fleming         if (item == NULL)
270520ed0cb0SMatthew D Fleming                 return;
27068d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
27078d689e04SGleb Smirnoff 	if (is_memguard_addr(item)) {
27088d689e04SGleb Smirnoff 		if (zone->uz_dtor != NULL && zone->uz_dtor != mtrash_dtor)
27098d689e04SGleb Smirnoff 			zone->uz_dtor(item, zone->uz_size, udata);
27108d689e04SGleb Smirnoff 		if (zone->uz_fini != NULL && zone->uz_fini != mtrash_fini)
27118d689e04SGleb Smirnoff 			zone->uz_fini(item, zone->uz_size);
27128d689e04SGleb Smirnoff 		memguard_free(item);
27138d689e04SGleb Smirnoff 		return;
27148d689e04SGleb Smirnoff 	}
27158d689e04SGleb Smirnoff #endif
27165d1ae027SRobert Watson #ifdef INVARIANTS
2717e20a199fSJeff Roberson 	if (zone->uz_flags & UMA_ZONE_MALLOC)
27185d1ae027SRobert Watson 		uma_dbg_free(zone, udata, item);
27195d1ae027SRobert Watson 	else
27205d1ae027SRobert Watson 		uma_dbg_free(zone, NULL, item);
27215d1ae027SRobert Watson #endif
2722fc03d22bSJeff Roberson 	if (zone->uz_dtor != NULL)
2723ef72505eSJeff Roberson 		zone->uz_dtor(item, zone->uz_size, udata);
2724ef72505eSJeff Roberson 
2725af7f9b97SJeff Roberson 	/*
2726af7f9b97SJeff Roberson 	 * The race here is acceptable.  If we miss it we'll just have to wait
2727af7f9b97SJeff Roberson 	 * a little longer for the limits to be reset.
2728af7f9b97SJeff Roberson 	 */
2729e20a199fSJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_FULL)
2730fc03d22bSJeff Roberson 		goto zfree_item;
2731af7f9b97SJeff Roberson 
27325d1ae027SRobert Watson 	/*
27335d1ae027SRobert Watson 	 * If possible, free to the per-CPU cache.  There are two
27345d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
27355d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
27365d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
27375d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
27385d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
27395d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to free to the
27405d1ae027SRobert Watson 	 * current cache; when we re-acquire the critical section, we must
27415d1ae027SRobert Watson 	 * detect and handle migration if it has occurred.
27425d1ae027SRobert Watson 	 */
2743a553d4b8SJeff Roberson zfree_restart:
27445d1ae027SRobert Watson 	critical_enter();
27455d1ae027SRobert Watson 	cpu = curcpu;
27468355f576SJeff Roberson 	cache = &zone->uz_cpu[cpu];
27478355f576SJeff Roberson 
27488355f576SJeff Roberson zfree_start:
2749a553d4b8SJeff Roberson 	/*
2750fc03d22bSJeff Roberson 	 * Try to free into the allocbucket first to give LIFO ordering
2751fc03d22bSJeff Roberson 	 * for cache-hot datastructures.  Spill over into the freebucket
2752fc03d22bSJeff Roberson 	 * if necessary.  Alloc will swap them if one runs dry.
2753a553d4b8SJeff Roberson 	 */
2754fc03d22bSJeff Roberson 	bucket = cache->uc_allocbucket;
2755fc03d22bSJeff Roberson 	if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries)
2756fc03d22bSJeff Roberson 		bucket = cache->uc_freebucket;
2757fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2758cae33c14SJeff Roberson 		KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
27598355f576SJeff Roberson 		    ("uma_zfree: Freeing to non free bucket index."));
2760cae33c14SJeff Roberson 		bucket->ub_bucket[bucket->ub_cnt] = item;
2761cae33c14SJeff Roberson 		bucket->ub_cnt++;
2762773df9abSRobert Watson 		cache->uc_frees++;
27635d1ae027SRobert Watson 		critical_exit();
27648355f576SJeff Roberson 		return;
2765fc03d22bSJeff Roberson 	}
2766fc03d22bSJeff Roberson 
27678355f576SJeff Roberson 	/*
27685d1ae027SRobert Watson 	 * We must go back the zone, which requires acquiring the zone lock,
27695d1ae027SRobert Watson 	 * which in turn means we must release and re-acquire the critical
27705d1ae027SRobert Watson 	 * section.  Since the critical section is released, we may be
27715d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
27725d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
27735d1ae027SRobert Watson 	 * the critical section.
27748355f576SJeff Roberson 	 */
27755d1ae027SRobert Watson 	critical_exit();
2776fc03d22bSJeff Roberson 	if (zone->uz_count == 0 || bucketdisable)
2777fc03d22bSJeff Roberson 		goto zfree_item;
2778fc03d22bSJeff Roberson 
27794d104ba0SAlexander Motin 	lockfail = 0;
27804d104ba0SAlexander Motin 	if (ZONE_TRYLOCK(zone) == 0) {
27814d104ba0SAlexander Motin 		/* Record contention to size the buckets. */
27828355f576SJeff Roberson 		ZONE_LOCK(zone);
27834d104ba0SAlexander Motin 		lockfail = 1;
27844d104ba0SAlexander Motin 	}
27855d1ae027SRobert Watson 	critical_enter();
27865d1ae027SRobert Watson 	cpu = curcpu;
27875d1ae027SRobert Watson 	cache = &zone->uz_cpu[cpu];
27888355f576SJeff Roberson 
2789fc03d22bSJeff Roberson 	/*
2790fc03d22bSJeff Roberson 	 * Since we have locked the zone we may as well send back our stats.
2791fc03d22bSJeff Roberson 	 */
27920095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
27930095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, cache->uc_frees);
2794f4ff923bSRobert Watson 	cache->uc_allocs = 0;
2795f4ff923bSRobert Watson 	cache->uc_frees = 0;
2796f4ff923bSRobert Watson 
27978355f576SJeff Roberson 	bucket = cache->uc_freebucket;
2798fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2799fc03d22bSJeff Roberson 		ZONE_UNLOCK(zone);
2800fc03d22bSJeff Roberson 		goto zfree_start;
2801fc03d22bSJeff Roberson 	}
28028355f576SJeff Roberson 	cache->uc_freebucket = NULL;
28038355f576SJeff Roberson 
28048355f576SJeff Roberson 	/* Can we throw this on the zone full list? */
28058355f576SJeff Roberson 	if (bucket != NULL) {
28068355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
28078355f576SJeff Roberson 		printf("uma_zfree: Putting old bucket on the free list.\n");
28088355f576SJeff Roberson #endif
2809cae33c14SJeff Roberson 		/* ub_cnt is pointing to the last free item */
2810cae33c14SJeff Roberson 		KASSERT(bucket->ub_cnt != 0,
28118355f576SJeff Roberson 		    ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2812fc03d22bSJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
28138355f576SJeff Roberson 	}
2814fc03d22bSJeff Roberson 
28155d1ae027SRobert Watson 	/* We are no longer associated with this CPU. */
28165d1ae027SRobert Watson 	critical_exit();
2817a553d4b8SJeff Roberson 
28184d104ba0SAlexander Motin 	/*
28194d104ba0SAlexander Motin 	 * We bump the uz count when the cache size is insufficient to
28204d104ba0SAlexander Motin 	 * handle the working set.
28214d104ba0SAlexander Motin 	 */
28224d104ba0SAlexander Motin 	if (lockfail && zone->uz_count < BUCKET_MAX)
28234d104ba0SAlexander Motin 		zone->uz_count++;
2824a553d4b8SJeff Roberson 	ZONE_UNLOCK(zone);
2825a553d4b8SJeff Roberson 
28268355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
28278355f576SJeff Roberson 	printf("uma_zfree: Allocating new free bucket.\n");
28288355f576SJeff Roberson #endif
28296fd34d6fSJeff Roberson 	bucket = bucket_alloc(zone, udata, M_NOWAIT);
28304741dcbfSJeff Roberson 	if (bucket) {
2831fc03d22bSJeff Roberson 		critical_enter();
2832fc03d22bSJeff Roberson 		cpu = curcpu;
2833fc03d22bSJeff Roberson 		cache = &zone->uz_cpu[cpu];
2834fc03d22bSJeff Roberson 		if (cache->uc_freebucket == NULL) {
2835fc03d22bSJeff Roberson 			cache->uc_freebucket = bucket;
2836fc03d22bSJeff Roberson 			goto zfree_start;
2837fc03d22bSJeff Roberson 		}
2838fc03d22bSJeff Roberson 		/*
2839fc03d22bSJeff Roberson 		 * We lost the race, start over.  We have to drop our
2840fc03d22bSJeff Roberson 		 * critical section to free the bucket.
2841fc03d22bSJeff Roberson 		 */
2842fc03d22bSJeff Roberson 		critical_exit();
28436fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
2844a553d4b8SJeff Roberson 		goto zfree_restart;
28458355f576SJeff Roberson 	}
28468355f576SJeff Roberson 
2847a553d4b8SJeff Roberson 	/*
2848a553d4b8SJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
2849a553d4b8SJeff Roberson 	 */
2850fc03d22bSJeff Roberson zfree_item:
28510095a784SJeff Roberson 	zone_free_item(zone, item, udata, SKIP_DTOR);
28528355f576SJeff Roberson 
28538355f576SJeff Roberson 	return;
28548355f576SJeff Roberson }
28558355f576SJeff Roberson 
28568355f576SJeff Roberson static void
28570095a784SJeff Roberson slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item)
28588355f576SJeff Roberson {
285985dcf349SGleb Smirnoff 	uint8_t freei;
2860099a0e58SBosko Milekic 
28610095a784SJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2862e20a199fSJeff Roberson 	MPASS(keg == slab->us_keg);
28638355f576SJeff Roberson 
28648355f576SJeff Roberson 	/* Do we need to remove from any lists? */
2865099a0e58SBosko Milekic 	if (slab->us_freecount+1 == keg->uk_ipers) {
28668355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
2867099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
28688355f576SJeff Roberson 	} else if (slab->us_freecount == 0) {
28698355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
2870099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
28718355f576SJeff Roberson 	}
28728355f576SJeff Roberson 
2873ef72505eSJeff Roberson 	/* Slab management. */
2874ef72505eSJeff Roberson 	freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
2875ef72505eSJeff Roberson 	BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
28768355f576SJeff Roberson 	slab->us_freecount++;
28778355f576SJeff Roberson 
2878ef72505eSJeff Roberson 	/* Keg statistics. */
2879099a0e58SBosko Milekic 	keg->uk_free++;
28800095a784SJeff Roberson }
28810095a784SJeff Roberson 
28820095a784SJeff Roberson static void
28830095a784SJeff Roberson zone_release(uma_zone_t zone, void **bucket, int cnt)
28840095a784SJeff Roberson {
28850095a784SJeff Roberson 	void *item;
28860095a784SJeff Roberson 	uma_slab_t slab;
28870095a784SJeff Roberson 	uma_keg_t keg;
28880095a784SJeff Roberson 	uint8_t *mem;
28890095a784SJeff Roberson 	int clearfull;
28900095a784SJeff Roberson 	int i;
28918355f576SJeff Roberson 
2892e20a199fSJeff Roberson 	clearfull = 0;
28930095a784SJeff Roberson 	keg = zone_first_keg(zone);
2894af526374SJeff Roberson 	KEG_LOCK(keg);
28950095a784SJeff Roberson 	for (i = 0; i < cnt; i++) {
28960095a784SJeff Roberson 		item = bucket[i];
28970095a784SJeff Roberson 		if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
28980095a784SJeff Roberson 			mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
28990095a784SJeff Roberson 			if (zone->uz_flags & UMA_ZONE_HASH) {
29000095a784SJeff Roberson 				slab = hash_sfind(&keg->uk_hash, mem);
29010095a784SJeff Roberson 			} else {
29020095a784SJeff Roberson 				mem += keg->uk_pgoff;
29030095a784SJeff Roberson 				slab = (uma_slab_t)mem;
29040095a784SJeff Roberson 			}
29050095a784SJeff Roberson 		} else {
29060095a784SJeff Roberson 			slab = vtoslab((vm_offset_t)item);
29070095a784SJeff Roberson 			if (slab->us_keg != keg) {
29080095a784SJeff Roberson 				KEG_UNLOCK(keg);
29090095a784SJeff Roberson 				keg = slab->us_keg;
29100095a784SJeff Roberson 				KEG_LOCK(keg);
29110095a784SJeff Roberson 			}
29120095a784SJeff Roberson 		}
29130095a784SJeff Roberson 		slab_free_item(keg, slab, item);
2914099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZFLAG_FULL) {
2915e20a199fSJeff Roberson 			if (keg->uk_pages < keg->uk_maxpages) {
2916099a0e58SBosko Milekic 				keg->uk_flags &= ~UMA_ZFLAG_FULL;
2917e20a199fSJeff Roberson 				clearfull = 1;
2918e20a199fSJeff Roberson 			}
2919af7f9b97SJeff Roberson 
292077380291SMohan Srinivasan 			/*
2921ef72505eSJeff Roberson 			 * We can handle one more allocation. Since we're
2922ef72505eSJeff Roberson 			 * clearing ZFLAG_FULL, wake up all procs blocked
2923ef72505eSJeff Roberson 			 * on pages. This should be uncommon, so keeping this
2924ef72505eSJeff Roberson 			 * simple for now (rather than adding count of blocked
292577380291SMohan Srinivasan 			 * threads etc).
292677380291SMohan Srinivasan 			 */
292777380291SMohan Srinivasan 			wakeup(keg);
2928af7f9b97SJeff Roberson 		}
29290095a784SJeff Roberson 	}
2930af526374SJeff Roberson 	KEG_UNLOCK(keg);
29310095a784SJeff Roberson 	if (clearfull) {
2932af526374SJeff Roberson 		ZONE_LOCK(zone);
2933e20a199fSJeff Roberson 		zone->uz_flags &= ~UMA_ZFLAG_FULL;
2934e20a199fSJeff Roberson 		wakeup(zone);
2935605cbd6aSJeff Roberson 		ZONE_UNLOCK(zone);
2936af526374SJeff Roberson 	}
2937ef72505eSJeff Roberson 
29388355f576SJeff Roberson }
29398355f576SJeff Roberson 
29400095a784SJeff Roberson /*
29410095a784SJeff Roberson  * Frees a single item to any zone.
29420095a784SJeff Roberson  *
29430095a784SJeff Roberson  * Arguments:
29440095a784SJeff Roberson  *	zone   The zone to free to
29450095a784SJeff Roberson  *	item   The item we're freeing
29460095a784SJeff Roberson  *	udata  User supplied data for the dtor
29470095a784SJeff Roberson  *	skip   Skip dtors and finis
29480095a784SJeff Roberson  */
29490095a784SJeff Roberson static void
29500095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
29510095a784SJeff Roberson {
29520095a784SJeff Roberson 
29530095a784SJeff Roberson #ifdef INVARIANTS
29540095a784SJeff Roberson 	if (skip == SKIP_NONE) {
29550095a784SJeff Roberson 		if (zone->uz_flags & UMA_ZONE_MALLOC)
29560095a784SJeff Roberson 			uma_dbg_free(zone, udata, item);
29570095a784SJeff Roberson 		else
29580095a784SJeff Roberson 			uma_dbg_free(zone, NULL, item);
29590095a784SJeff Roberson 	}
29600095a784SJeff Roberson #endif
29610095a784SJeff Roberson 	if (skip < SKIP_DTOR && zone->uz_dtor)
29620095a784SJeff Roberson 		zone->uz_dtor(item, zone->uz_size, udata);
29630095a784SJeff Roberson 
29640095a784SJeff Roberson 	if (skip < SKIP_FINI && zone->uz_fini)
29650095a784SJeff Roberson 		zone->uz_fini(item, zone->uz_size);
29660095a784SJeff Roberson 
29670095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, 1);
29680095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, &item, 1);
29690095a784SJeff Roberson }
29700095a784SJeff Roberson 
29718355f576SJeff Roberson /* See uma.h */
29721c6cae97SLawrence Stewart int
2973736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems)
2974736ee590SJeff Roberson {
2975099a0e58SBosko Milekic 	uma_keg_t keg;
2976099a0e58SBosko Milekic 
2977e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
29780095a784SJeff Roberson 	if (keg == NULL)
29790095a784SJeff Roberson 		return (0);
2980af526374SJeff Roberson 	KEG_LOCK(keg);
2981e20a199fSJeff Roberson 	keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
2982099a0e58SBosko Milekic 	if (keg->uk_maxpages * keg->uk_ipers < nitems)
2983e20a199fSJeff Roberson 		keg->uk_maxpages += keg->uk_ppera;
29841c6cae97SLawrence Stewart 	nitems = keg->uk_maxpages * keg->uk_ipers;
2985af526374SJeff Roberson 	KEG_UNLOCK(keg);
29861c6cae97SLawrence Stewart 
29871c6cae97SLawrence Stewart 	return (nitems);
2988736ee590SJeff Roberson }
2989736ee590SJeff Roberson 
2990736ee590SJeff Roberson /* See uma.h */
2991e49471b0SAndre Oppermann int
2992e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone)
2993e49471b0SAndre Oppermann {
2994e49471b0SAndre Oppermann 	int nitems;
2995e49471b0SAndre Oppermann 	uma_keg_t keg;
2996e49471b0SAndre Oppermann 
2997e49471b0SAndre Oppermann 	keg = zone_first_keg(zone);
29980095a784SJeff Roberson 	if (keg == NULL)
29990095a784SJeff Roberson 		return (0);
3000af526374SJeff Roberson 	KEG_LOCK(keg);
3001e49471b0SAndre Oppermann 	nitems = keg->uk_maxpages * keg->uk_ipers;
3002af526374SJeff Roberson 	KEG_UNLOCK(keg);
3003e49471b0SAndre Oppermann 
3004e49471b0SAndre Oppermann 	return (nitems);
3005e49471b0SAndre Oppermann }
3006e49471b0SAndre Oppermann 
3007e49471b0SAndre Oppermann /* See uma.h */
30082f891cd5SPawel Jakub Dawidek void
30092f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning)
30102f891cd5SPawel Jakub Dawidek {
30112f891cd5SPawel Jakub Dawidek 
30122f891cd5SPawel Jakub Dawidek 	ZONE_LOCK(zone);
30132f891cd5SPawel Jakub Dawidek 	zone->uz_warning = warning;
30142f891cd5SPawel Jakub Dawidek 	ZONE_UNLOCK(zone);
30152f891cd5SPawel Jakub Dawidek }
30162f891cd5SPawel Jakub Dawidek 
30172f891cd5SPawel Jakub Dawidek /* See uma.h */
3018c4ae7908SLawrence Stewart int
3019c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone)
3020c4ae7908SLawrence Stewart {
3021c4ae7908SLawrence Stewart 	int64_t nitems;
3022c4ae7908SLawrence Stewart 	u_int i;
3023c4ae7908SLawrence Stewart 
3024c4ae7908SLawrence Stewart 	ZONE_LOCK(zone);
3025c4ae7908SLawrence Stewart 	nitems = zone->uz_allocs - zone->uz_frees;
3026c4ae7908SLawrence Stewart 	CPU_FOREACH(i) {
3027c4ae7908SLawrence Stewart 		/*
3028c4ae7908SLawrence Stewart 		 * See the comment in sysctl_vm_zone_stats() regarding the
3029c4ae7908SLawrence Stewart 		 * safety of accessing the per-cpu caches. With the zone lock
3030c4ae7908SLawrence Stewart 		 * held, it is safe, but can potentially result in stale data.
3031c4ae7908SLawrence Stewart 		 */
3032c4ae7908SLawrence Stewart 		nitems += zone->uz_cpu[i].uc_allocs -
3033c4ae7908SLawrence Stewart 		    zone->uz_cpu[i].uc_frees;
3034c4ae7908SLawrence Stewart 	}
3035c4ae7908SLawrence Stewart 	ZONE_UNLOCK(zone);
3036c4ae7908SLawrence Stewart 
3037c4ae7908SLawrence Stewart 	return (nitems < 0 ? 0 : nitems);
3038c4ae7908SLawrence Stewart }
3039c4ae7908SLawrence Stewart 
3040c4ae7908SLawrence Stewart /* See uma.h */
3041736ee590SJeff Roberson void
3042099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit)
3043099a0e58SBosko Milekic {
3044e20a199fSJeff Roberson 	uma_keg_t keg;
3045e20a199fSJeff Roberson 
3046e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
30470095a784SJeff Roberson 	KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
3048af526374SJeff Roberson 	KEG_LOCK(keg);
3049e20a199fSJeff Roberson 	KASSERT(keg->uk_pages == 0,
3050099a0e58SBosko Milekic 	    ("uma_zone_set_init on non-empty keg"));
3051e20a199fSJeff Roberson 	keg->uk_init = uminit;
3052af526374SJeff Roberson 	KEG_UNLOCK(keg);
3053099a0e58SBosko Milekic }
3054099a0e58SBosko Milekic 
3055099a0e58SBosko Milekic /* See uma.h */
3056099a0e58SBosko Milekic void
3057099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
3058099a0e58SBosko Milekic {
3059e20a199fSJeff Roberson 	uma_keg_t keg;
3060e20a199fSJeff Roberson 
3061e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
30621d2c0c46SDmitry Chagin 	KASSERT(keg != NULL, ("uma_zone_set_fini: Invalid zone type"));
3063af526374SJeff Roberson 	KEG_LOCK(keg);
3064e20a199fSJeff Roberson 	KASSERT(keg->uk_pages == 0,
3065099a0e58SBosko Milekic 	    ("uma_zone_set_fini on non-empty keg"));
3066e20a199fSJeff Roberson 	keg->uk_fini = fini;
3067af526374SJeff Roberson 	KEG_UNLOCK(keg);
3068099a0e58SBosko Milekic }
3069099a0e58SBosko Milekic 
3070099a0e58SBosko Milekic /* See uma.h */
3071099a0e58SBosko Milekic void
3072099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
3073099a0e58SBosko Milekic {
3074af526374SJeff Roberson 
3075099a0e58SBosko Milekic 	ZONE_LOCK(zone);
3076e20a199fSJeff Roberson 	KASSERT(zone_first_keg(zone)->uk_pages == 0,
3077099a0e58SBosko Milekic 	    ("uma_zone_set_zinit on non-empty keg"));
3078099a0e58SBosko Milekic 	zone->uz_init = zinit;
3079099a0e58SBosko Milekic 	ZONE_UNLOCK(zone);
3080099a0e58SBosko Milekic }
3081099a0e58SBosko Milekic 
3082099a0e58SBosko Milekic /* See uma.h */
3083099a0e58SBosko Milekic void
3084099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
3085099a0e58SBosko Milekic {
3086af526374SJeff Roberson 
3087099a0e58SBosko Milekic 	ZONE_LOCK(zone);
3088e20a199fSJeff Roberson 	KASSERT(zone_first_keg(zone)->uk_pages == 0,
3089099a0e58SBosko Milekic 	    ("uma_zone_set_zfini on non-empty keg"));
3090099a0e58SBosko Milekic 	zone->uz_fini = zfini;
3091099a0e58SBosko Milekic 	ZONE_UNLOCK(zone);
3092099a0e58SBosko Milekic }
3093099a0e58SBosko Milekic 
3094099a0e58SBosko Milekic /* See uma.h */
3095b23f72e9SBrian Feldman /* XXX uk_freef is not actually used with the zone locked */
3096099a0e58SBosko Milekic void
30978355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef)
30988355f576SJeff Roberson {
30990095a784SJeff Roberson 	uma_keg_t keg;
3100e20a199fSJeff Roberson 
31010095a784SJeff Roberson 	keg = zone_first_keg(zone);
31021d2c0c46SDmitry Chagin 	KASSERT(keg != NULL, ("uma_zone_set_freef: Invalid zone type"));
3103af526374SJeff Roberson 	KEG_LOCK(keg);
31040095a784SJeff Roberson 	keg->uk_freef = freef;
3105af526374SJeff Roberson 	KEG_UNLOCK(keg);
31068355f576SJeff Roberson }
31078355f576SJeff Roberson 
31088355f576SJeff Roberson /* See uma.h */
3109b23f72e9SBrian Feldman /* XXX uk_allocf is not actually used with the zone locked */
31108355f576SJeff Roberson void
31118355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
31128355f576SJeff Roberson {
3113e20a199fSJeff Roberson 	uma_keg_t keg;
3114e20a199fSJeff Roberson 
3115e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
3116af526374SJeff Roberson 	KEG_LOCK(keg);
3117e20a199fSJeff Roberson 	keg->uk_allocf = allocf;
3118af526374SJeff Roberson 	KEG_UNLOCK(keg);
31198355f576SJeff Roberson }
31208355f576SJeff Roberson 
31218355f576SJeff Roberson /* See uma.h */
31226fd34d6fSJeff Roberson void
31236fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items)
31246fd34d6fSJeff Roberson {
31256fd34d6fSJeff Roberson 	uma_keg_t keg;
31266fd34d6fSJeff Roberson 
31276fd34d6fSJeff Roberson 	keg = zone_first_keg(zone);
31286fd34d6fSJeff Roberson 	if (keg == NULL)
31296fd34d6fSJeff Roberson 		return;
31306fd34d6fSJeff Roberson 	KEG_LOCK(keg);
31316fd34d6fSJeff Roberson 	keg->uk_reserve = items;
31326fd34d6fSJeff Roberson 	KEG_UNLOCK(keg);
31336fd34d6fSJeff Roberson 
31346fd34d6fSJeff Roberson 	return;
31356fd34d6fSJeff Roberson }
31366fd34d6fSJeff Roberson 
31376fd34d6fSJeff Roberson /* See uma.h */
31388355f576SJeff Roberson int
3139a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count)
31408355f576SJeff Roberson {
3141099a0e58SBosko Milekic 	uma_keg_t keg;
31428355f576SJeff Roberson 	vm_offset_t kva;
3143099a0e58SBosko Milekic 	int pages;
31448355f576SJeff Roberson 
3145e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
31460095a784SJeff Roberson 	if (keg == NULL)
31470095a784SJeff Roberson 		return (0);
3148099a0e58SBosko Milekic 	pages = count / keg->uk_ipers;
31498355f576SJeff Roberson 
3150099a0e58SBosko Milekic 	if (pages * keg->uk_ipers < count)
31518355f576SJeff Roberson 		pages++;
3152a553d4b8SJeff Roberson 
3153a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
3154a4915c21SAttilio Rao 	if (keg->uk_ppera > 1) {
3155a4915c21SAttilio Rao #else
3156a4915c21SAttilio Rao 	if (1) {
3157a4915c21SAttilio Rao #endif
31585df87b21SJeff Roberson 		kva = kva_alloc(pages * UMA_SLAB_SIZE);
3159d1f42ac2SAlan Cox 		if (kva == 0)
31608355f576SJeff Roberson 			return (0);
3161a4915c21SAttilio Rao 	} else
3162a4915c21SAttilio Rao 		kva = 0;
3163af526374SJeff Roberson 	KEG_LOCK(keg);
3164099a0e58SBosko Milekic 	keg->uk_kva = kva;
3165a4915c21SAttilio Rao 	keg->uk_offset = 0;
3166099a0e58SBosko Milekic 	keg->uk_maxpages = pages;
3167a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
3168a4915c21SAttilio Rao 	keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
3169a4915c21SAttilio Rao #else
3170a4915c21SAttilio Rao 	keg->uk_allocf = noobj_alloc;
3171a4915c21SAttilio Rao #endif
31726fd34d6fSJeff Roberson 	keg->uk_flags |= UMA_ZONE_NOFREE;
3173af526374SJeff Roberson 	KEG_UNLOCK(keg);
3174af526374SJeff Roberson 
31758355f576SJeff Roberson 	return (1);
31768355f576SJeff Roberson }
31778355f576SJeff Roberson 
31788355f576SJeff Roberson /* See uma.h */
31798355f576SJeff Roberson void
31808355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items)
31818355f576SJeff Roberson {
31828355f576SJeff Roberson 	int slabs;
31838355f576SJeff Roberson 	uma_slab_t slab;
3184099a0e58SBosko Milekic 	uma_keg_t keg;
31858355f576SJeff Roberson 
3186e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
31870095a784SJeff Roberson 	if (keg == NULL)
31880095a784SJeff Roberson 		return;
3189af526374SJeff Roberson 	KEG_LOCK(keg);
3190099a0e58SBosko Milekic 	slabs = items / keg->uk_ipers;
3191099a0e58SBosko Milekic 	if (slabs * keg->uk_ipers < items)
31928355f576SJeff Roberson 		slabs++;
31938355f576SJeff Roberson 	while (slabs > 0) {
3194e20a199fSJeff Roberson 		slab = keg_alloc_slab(keg, zone, M_WAITOK);
3195e20a199fSJeff Roberson 		if (slab == NULL)
3196e20a199fSJeff Roberson 			break;
3197e20a199fSJeff Roberson 		MPASS(slab->us_keg == keg);
3198099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
31998355f576SJeff Roberson 		slabs--;
32008355f576SJeff Roberson 	}
3201af526374SJeff Roberson 	KEG_UNLOCK(keg);
32028355f576SJeff Roberson }
32038355f576SJeff Roberson 
32048355f576SJeff Roberson /* See uma.h */
320585dcf349SGleb Smirnoff uint32_t *
3206099a0e58SBosko Milekic uma_find_refcnt(uma_zone_t zone, void *item)
3207099a0e58SBosko Milekic {
3208ab14a3f7SBrian Feldman 	uma_slabrefcnt_t slabref;
3209ef72505eSJeff Roberson 	uma_slab_t slab;
3210099a0e58SBosko Milekic 	uma_keg_t keg;
321185dcf349SGleb Smirnoff 	uint32_t *refcnt;
3212099a0e58SBosko Milekic 	int idx;
3213099a0e58SBosko Milekic 
3214ef72505eSJeff Roberson 	slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK));
3215ef72505eSJeff Roberson 	slabref = (uma_slabrefcnt_t)slab;
3216ef72505eSJeff Roberson 	keg = slab->us_keg;
3217ef72505eSJeff Roberson 	KASSERT(keg->uk_flags & UMA_ZONE_REFCNT,
3218099a0e58SBosko Milekic 	    ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
3219ef72505eSJeff Roberson 	idx = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3220ef72505eSJeff Roberson 	refcnt = &slabref->us_refcnt[idx];
3221099a0e58SBosko Milekic 	return refcnt;
3222099a0e58SBosko Milekic }
3223099a0e58SBosko Milekic 
3224099a0e58SBosko Milekic /* See uma.h */
32258355f576SJeff Roberson void
32268355f576SJeff Roberson uma_reclaim(void)
32278355f576SJeff Roberson {
32288355f576SJeff Roberson #ifdef UMA_DEBUG
32298355f576SJeff Roberson 	printf("UMA: vm asked us to release pages!\n");
32308355f576SJeff Roberson #endif
323195c4bf75SKonstantin Belousov 	sx_xlock(&uma_drain_lock);
323286bbae32SJeff Roberson 	bucket_enable();
32338355f576SJeff Roberson 	zone_foreach(zone_drain);
3234a2de44abSAlexander Motin 	if (vm_page_count_min()) {
3235a2de44abSAlexander Motin 		cache_drain_safe(NULL);
3236a2de44abSAlexander Motin 		zone_foreach(zone_drain);
3237a2de44abSAlexander Motin 	}
32388355f576SJeff Roberson 	/*
32398355f576SJeff Roberson 	 * Some slabs may have been freed but this zone will be visited early
32408355f576SJeff Roberson 	 * we visit again so that we can free pages that are empty once other
32418355f576SJeff Roberson 	 * zones are drained.  We have to do the same for buckets.
32428355f576SJeff Roberson 	 */
32439643769aSJeff Roberson 	zone_drain(slabzone);
3244099a0e58SBosko Milekic 	zone_drain(slabrefzone);
3245cae33c14SJeff Roberson 	bucket_zone_drain();
324695c4bf75SKonstantin Belousov 	sx_xunlock(&uma_drain_lock);
32478355f576SJeff Roberson }
32488355f576SJeff Roberson 
3249663b416fSJohn Baldwin /* See uma.h */
3250663b416fSJohn Baldwin int
3251663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone)
3252663b416fSJohn Baldwin {
3253663b416fSJohn Baldwin 	int full;
3254663b416fSJohn Baldwin 
3255663b416fSJohn Baldwin 	ZONE_LOCK(zone);
3256e20a199fSJeff Roberson 	full = (zone->uz_flags & UMA_ZFLAG_FULL);
3257663b416fSJohn Baldwin 	ZONE_UNLOCK(zone);
3258663b416fSJohn Baldwin 	return (full);
3259663b416fSJohn Baldwin }
3260663b416fSJohn Baldwin 
32616c125b8dSMohan Srinivasan int
32626c125b8dSMohan Srinivasan uma_zone_exhausted_nolock(uma_zone_t zone)
32636c125b8dSMohan Srinivasan {
3264e20a199fSJeff Roberson 	return (zone->uz_flags & UMA_ZFLAG_FULL);
32656c125b8dSMohan Srinivasan }
32666c125b8dSMohan Srinivasan 
32678355f576SJeff Roberson void *
3268f2c2231eSRyan Stone uma_large_malloc(vm_size_t size, int wait)
32698355f576SJeff Roberson {
32708355f576SJeff Roberson 	void *mem;
32718355f576SJeff Roberson 	uma_slab_t slab;
327285dcf349SGleb Smirnoff 	uint8_t flags;
32738355f576SJeff Roberson 
3274e20a199fSJeff Roberson 	slab = zone_alloc_item(slabzone, NULL, wait);
32758355f576SJeff Roberson 	if (slab == NULL)
32768355f576SJeff Roberson 		return (NULL);
32778355f576SJeff Roberson 	mem = page_alloc(NULL, size, &flags, wait);
32788355f576SJeff Roberson 	if (mem) {
327999571dc3SJeff Roberson 		vsetslab((vm_offset_t)mem, slab);
32808355f576SJeff Roberson 		slab->us_data = mem;
32818355f576SJeff Roberson 		slab->us_flags = flags | UMA_SLAB_MALLOC;
32828355f576SJeff Roberson 		slab->us_size = size;
32838355f576SJeff Roberson 	} else {
32840095a784SJeff Roberson 		zone_free_item(slabzone, slab, NULL, SKIP_NONE);
32858355f576SJeff Roberson 	}
32868355f576SJeff Roberson 
32878355f576SJeff Roberson 	return (mem);
32888355f576SJeff Roberson }
32898355f576SJeff Roberson 
32908355f576SJeff Roberson void
32918355f576SJeff Roberson uma_large_free(uma_slab_t slab)
32928355f576SJeff Roberson {
3293c325e866SKonstantin Belousov 
32948355f576SJeff Roberson 	page_free(slab->us_data, slab->us_size, slab->us_flags);
32950095a784SJeff Roberson 	zone_free_item(slabzone, slab, NULL, SKIP_NONE);
32968355f576SJeff Roberson }
32978355f576SJeff Roberson 
329848343a2fSGleb Smirnoff static void
329948343a2fSGleb Smirnoff uma_zero_item(void *item, uma_zone_t zone)
330048343a2fSGleb Smirnoff {
330148343a2fSGleb Smirnoff 
330248343a2fSGleb Smirnoff 	if (zone->uz_flags & UMA_ZONE_PCPU) {
330348343a2fSGleb Smirnoff 		for (int i = 0; i < mp_ncpus; i++)
330448343a2fSGleb Smirnoff 			bzero(zpcpu_get_cpu(item, i), zone->uz_size);
330548343a2fSGleb Smirnoff 	} else
330648343a2fSGleb Smirnoff 		bzero(item, zone->uz_size);
330748343a2fSGleb Smirnoff }
330848343a2fSGleb Smirnoff 
33098355f576SJeff Roberson void
33108355f576SJeff Roberson uma_print_stats(void)
33118355f576SJeff Roberson {
33128355f576SJeff Roberson 	zone_foreach(uma_print_zone);
33138355f576SJeff Roberson }
33148355f576SJeff Roberson 
3315504d5de3SJeff Roberson static void
3316504d5de3SJeff Roberson slab_print(uma_slab_t slab)
3317504d5de3SJeff Roberson {
3318ef72505eSJeff Roberson 	printf("slab: keg %p, data %p, freecount %d\n",
3319ef72505eSJeff Roberson 		slab->us_keg, slab->us_data, slab->us_freecount);
3320504d5de3SJeff Roberson }
3321504d5de3SJeff Roberson 
3322504d5de3SJeff Roberson static void
3323504d5de3SJeff Roberson cache_print(uma_cache_t cache)
3324504d5de3SJeff Roberson {
3325504d5de3SJeff Roberson 	printf("alloc: %p(%d), free: %p(%d)\n",
3326504d5de3SJeff Roberson 		cache->uc_allocbucket,
3327504d5de3SJeff Roberson 		cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3328504d5de3SJeff Roberson 		cache->uc_freebucket,
3329504d5de3SJeff Roberson 		cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3330504d5de3SJeff Roberson }
3331504d5de3SJeff Roberson 
3332e20a199fSJeff Roberson static void
3333e20a199fSJeff Roberson uma_print_keg(uma_keg_t keg)
33348355f576SJeff Roberson {
3335504d5de3SJeff Roberson 	uma_slab_t slab;
3336504d5de3SJeff Roberson 
33370b80c1e4SEitan Adler 	printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3338e20a199fSJeff Roberson 	    "out %d free %d limit %d\n",
3339e20a199fSJeff Roberson 	    keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3340099a0e58SBosko Milekic 	    keg->uk_ipers, keg->uk_ppera,
3341e20a199fSJeff Roberson 	    (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free,
3342e20a199fSJeff Roberson 	    (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3343504d5de3SJeff Roberson 	printf("Part slabs:\n");
3344099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
3345504d5de3SJeff Roberson 		slab_print(slab);
3346504d5de3SJeff Roberson 	printf("Free slabs:\n");
3347099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
3348504d5de3SJeff Roberson 		slab_print(slab);
3349504d5de3SJeff Roberson 	printf("Full slabs:\n");
3350099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
3351504d5de3SJeff Roberson 		slab_print(slab);
3352e20a199fSJeff Roberson }
3353e20a199fSJeff Roberson 
3354e20a199fSJeff Roberson void
3355e20a199fSJeff Roberson uma_print_zone(uma_zone_t zone)
3356e20a199fSJeff Roberson {
3357e20a199fSJeff Roberson 	uma_cache_t cache;
3358e20a199fSJeff Roberson 	uma_klink_t kl;
3359e20a199fSJeff Roberson 	int i;
3360e20a199fSJeff Roberson 
33610b80c1e4SEitan Adler 	printf("zone: %s(%p) size %d flags %#x\n",
3362e20a199fSJeff Roberson 	    zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3363e20a199fSJeff Roberson 	LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3364e20a199fSJeff Roberson 		uma_print_keg(kl->kl_keg);
33653aa6d94eSJohn Baldwin 	CPU_FOREACH(i) {
3366504d5de3SJeff Roberson 		cache = &zone->uz_cpu[i];
3367504d5de3SJeff Roberson 		printf("CPU %d Cache:\n", i);
3368504d5de3SJeff Roberson 		cache_print(cache);
3369504d5de3SJeff Roberson 	}
33708355f576SJeff Roberson }
33718355f576SJeff Roberson 
3372a0d4b0aeSRobert Watson #ifdef DDB
33738355f576SJeff Roberson /*
33747a52a97eSRobert Watson  * Generate statistics across both the zone and its per-cpu cache's.  Return
33757a52a97eSRobert Watson  * desired statistics if the pointer is non-NULL for that statistic.
33767a52a97eSRobert Watson  *
33777a52a97eSRobert Watson  * Note: does not update the zone statistics, as it can't safely clear the
33787a52a97eSRobert Watson  * per-CPU cache statistic.
33797a52a97eSRobert Watson  *
33807a52a97eSRobert Watson  * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
33817a52a97eSRobert Watson  * safe from off-CPU; we should modify the caches to track this information
33827a52a97eSRobert Watson  * directly so that we don't have to.
33837a52a97eSRobert Watson  */
33847a52a97eSRobert Watson static void
338585dcf349SGleb Smirnoff uma_zone_sumstat(uma_zone_t z, int *cachefreep, uint64_t *allocsp,
338685dcf349SGleb Smirnoff     uint64_t *freesp, uint64_t *sleepsp)
33877a52a97eSRobert Watson {
33887a52a97eSRobert Watson 	uma_cache_t cache;
338985dcf349SGleb Smirnoff 	uint64_t allocs, frees, sleeps;
33907a52a97eSRobert Watson 	int cachefree, cpu;
33917a52a97eSRobert Watson 
3392bf965959SSean Bruno 	allocs = frees = sleeps = 0;
33937a52a97eSRobert Watson 	cachefree = 0;
33943aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
33957a52a97eSRobert Watson 		cache = &z->uz_cpu[cpu];
33967a52a97eSRobert Watson 		if (cache->uc_allocbucket != NULL)
33977a52a97eSRobert Watson 			cachefree += cache->uc_allocbucket->ub_cnt;
33987a52a97eSRobert Watson 		if (cache->uc_freebucket != NULL)
33997a52a97eSRobert Watson 			cachefree += cache->uc_freebucket->ub_cnt;
34007a52a97eSRobert Watson 		allocs += cache->uc_allocs;
34017a52a97eSRobert Watson 		frees += cache->uc_frees;
34027a52a97eSRobert Watson 	}
34037a52a97eSRobert Watson 	allocs += z->uz_allocs;
34047a52a97eSRobert Watson 	frees += z->uz_frees;
3405bf965959SSean Bruno 	sleeps += z->uz_sleeps;
34067a52a97eSRobert Watson 	if (cachefreep != NULL)
34077a52a97eSRobert Watson 		*cachefreep = cachefree;
34087a52a97eSRobert Watson 	if (allocsp != NULL)
34097a52a97eSRobert Watson 		*allocsp = allocs;
34107a52a97eSRobert Watson 	if (freesp != NULL)
34117a52a97eSRobert Watson 		*freesp = frees;
3412bf965959SSean Bruno 	if (sleepsp != NULL)
3413bf965959SSean Bruno 		*sleepsp = sleeps;
34147a52a97eSRobert Watson }
3415a0d4b0aeSRobert Watson #endif /* DDB */
34167a52a97eSRobert Watson 
34177a52a97eSRobert Watson static int
34187a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
34197a52a97eSRobert Watson {
34207a52a97eSRobert Watson 	uma_keg_t kz;
34217a52a97eSRobert Watson 	uma_zone_t z;
34227a52a97eSRobert Watson 	int count;
34237a52a97eSRobert Watson 
34247a52a97eSRobert Watson 	count = 0;
3425111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
34267a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
34277a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
34287a52a97eSRobert Watson 			count++;
34297a52a97eSRobert Watson 	}
3430111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
34317a52a97eSRobert Watson 	return (sysctl_handle_int(oidp, &count, 0, req));
34327a52a97eSRobert Watson }
34337a52a97eSRobert Watson 
34347a52a97eSRobert Watson static int
34357a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
34367a52a97eSRobert Watson {
34377a52a97eSRobert Watson 	struct uma_stream_header ush;
34387a52a97eSRobert Watson 	struct uma_type_header uth;
34397a52a97eSRobert Watson 	struct uma_percpu_stat ups;
34407a52a97eSRobert Watson 	uma_bucket_t bucket;
34417a52a97eSRobert Watson 	struct sbuf sbuf;
34427a52a97eSRobert Watson 	uma_cache_t cache;
3443e20a199fSJeff Roberson 	uma_klink_t kl;
34447a52a97eSRobert Watson 	uma_keg_t kz;
34457a52a97eSRobert Watson 	uma_zone_t z;
3446e20a199fSJeff Roberson 	uma_keg_t k;
34474e657159SMatthew D Fleming 	int count, error, i;
34487a52a97eSRobert Watson 
344900f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
345000f0e671SMatthew D Fleming 	if (error != 0)
345100f0e671SMatthew D Fleming 		return (error);
34524e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
34531eafc078SIan Lepore 	sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
34544e657159SMatthew D Fleming 
3455404a593eSMatthew D Fleming 	count = 0;
3456111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
34577a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
34587a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
34597a52a97eSRobert Watson 			count++;
34607a52a97eSRobert Watson 	}
34617a52a97eSRobert Watson 
34627a52a97eSRobert Watson 	/*
34637a52a97eSRobert Watson 	 * Insert stream header.
34647a52a97eSRobert Watson 	 */
34657a52a97eSRobert Watson 	bzero(&ush, sizeof(ush));
34667a52a97eSRobert Watson 	ush.ush_version = UMA_STREAM_VERSION;
3467ab3a57c0SRobert Watson 	ush.ush_maxcpus = (mp_maxid + 1);
34687a52a97eSRobert Watson 	ush.ush_count = count;
34694e657159SMatthew D Fleming 	(void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
34707a52a97eSRobert Watson 
34717a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
34727a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
34737a52a97eSRobert Watson 			bzero(&uth, sizeof(uth));
34747a52a97eSRobert Watson 			ZONE_LOCK(z);
3475cbbb4a00SRobert Watson 			strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
34767a52a97eSRobert Watson 			uth.uth_align = kz->uk_align;
34777a52a97eSRobert Watson 			uth.uth_size = kz->uk_size;
34787a52a97eSRobert Watson 			uth.uth_rsize = kz->uk_rsize;
3479e20a199fSJeff Roberson 			LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3480e20a199fSJeff Roberson 				k = kl->kl_keg;
3481e20a199fSJeff Roberson 				uth.uth_maxpages += k->uk_maxpages;
3482e20a199fSJeff Roberson 				uth.uth_pages += k->uk_pages;
3483e20a199fSJeff Roberson 				uth.uth_keg_free += k->uk_free;
3484e20a199fSJeff Roberson 				uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3485e20a199fSJeff Roberson 				    * k->uk_ipers;
3486e20a199fSJeff Roberson 			}
3487cbbb4a00SRobert Watson 
3488cbbb4a00SRobert Watson 			/*
3489cbbb4a00SRobert Watson 			 * A zone is secondary is it is not the first entry
3490cbbb4a00SRobert Watson 			 * on the keg's zone list.
3491cbbb4a00SRobert Watson 			 */
3492e20a199fSJeff Roberson 			if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3493cbbb4a00SRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z))
3494cbbb4a00SRobert Watson 				uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3495cbbb4a00SRobert Watson 
3496fc03d22bSJeff Roberson 			LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
34977a52a97eSRobert Watson 				uth.uth_zone_free += bucket->ub_cnt;
34987a52a97eSRobert Watson 			uth.uth_allocs = z->uz_allocs;
34997a52a97eSRobert Watson 			uth.uth_frees = z->uz_frees;
35002019094aSRobert Watson 			uth.uth_fails = z->uz_fails;
3501bf965959SSean Bruno 			uth.uth_sleeps = z->uz_sleeps;
35024e657159SMatthew D Fleming 			(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
35037a52a97eSRobert Watson 			/*
35042450bbb8SRobert Watson 			 * While it is not normally safe to access the cache
35052450bbb8SRobert Watson 			 * bucket pointers while not on the CPU that owns the
35062450bbb8SRobert Watson 			 * cache, we only allow the pointers to be exchanged
35072450bbb8SRobert Watson 			 * without the zone lock held, not invalidated, so
35082450bbb8SRobert Watson 			 * accept the possible race associated with bucket
35092450bbb8SRobert Watson 			 * exchange during monitoring.
35107a52a97eSRobert Watson 			 */
3511ab3a57c0SRobert Watson 			for (i = 0; i < (mp_maxid + 1); i++) {
35127a52a97eSRobert Watson 				bzero(&ups, sizeof(ups));
35137a52a97eSRobert Watson 				if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
35147a52a97eSRobert Watson 					goto skip;
3515082dc776SRobert Watson 				if (CPU_ABSENT(i))
3516082dc776SRobert Watson 					goto skip;
35177a52a97eSRobert Watson 				cache = &z->uz_cpu[i];
35187a52a97eSRobert Watson 				if (cache->uc_allocbucket != NULL)
35197a52a97eSRobert Watson 					ups.ups_cache_free +=
35207a52a97eSRobert Watson 					    cache->uc_allocbucket->ub_cnt;
35217a52a97eSRobert Watson 				if (cache->uc_freebucket != NULL)
35227a52a97eSRobert Watson 					ups.ups_cache_free +=
35237a52a97eSRobert Watson 					    cache->uc_freebucket->ub_cnt;
35247a52a97eSRobert Watson 				ups.ups_allocs = cache->uc_allocs;
35257a52a97eSRobert Watson 				ups.ups_frees = cache->uc_frees;
35267a52a97eSRobert Watson skip:
35274e657159SMatthew D Fleming 				(void)sbuf_bcat(&sbuf, &ups, sizeof(ups));
35287a52a97eSRobert Watson 			}
35292450bbb8SRobert Watson 			ZONE_UNLOCK(z);
35307a52a97eSRobert Watson 		}
35317a52a97eSRobert Watson 	}
3532111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
35334e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
35344e657159SMatthew D Fleming 	sbuf_delete(&sbuf);
35357a52a97eSRobert Watson 	return (error);
35367a52a97eSRobert Watson }
353748c5777eSRobert Watson 
35380a5a3ccbSGleb Smirnoff int
35390a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
35400a5a3ccbSGleb Smirnoff {
35410a5a3ccbSGleb Smirnoff 	uma_zone_t zone = *(uma_zone_t *)arg1;
354216be9f54SGleb Smirnoff 	int error, max;
35430a5a3ccbSGleb Smirnoff 
354416be9f54SGleb Smirnoff 	max = uma_zone_get_max(zone);
35450a5a3ccbSGleb Smirnoff 	error = sysctl_handle_int(oidp, &max, 0, req);
35460a5a3ccbSGleb Smirnoff 	if (error || !req->newptr)
35470a5a3ccbSGleb Smirnoff 		return (error);
35480a5a3ccbSGleb Smirnoff 
35490a5a3ccbSGleb Smirnoff 	uma_zone_set_max(zone, max);
35500a5a3ccbSGleb Smirnoff 
35510a5a3ccbSGleb Smirnoff 	return (0);
35520a5a3ccbSGleb Smirnoff }
35530a5a3ccbSGleb Smirnoff 
35540a5a3ccbSGleb Smirnoff int
35550a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
35560a5a3ccbSGleb Smirnoff {
35570a5a3ccbSGleb Smirnoff 	uma_zone_t zone = *(uma_zone_t *)arg1;
35580a5a3ccbSGleb Smirnoff 	int cur;
35590a5a3ccbSGleb Smirnoff 
35600a5a3ccbSGleb Smirnoff 	cur = uma_zone_get_cur(zone);
35610a5a3ccbSGleb Smirnoff 	return (sysctl_handle_int(oidp, &cur, 0, req));
35620a5a3ccbSGleb Smirnoff }
35630a5a3ccbSGleb Smirnoff 
356448c5777eSRobert Watson #ifdef DDB
356548c5777eSRobert Watson DB_SHOW_COMMAND(uma, db_show_uma)
356648c5777eSRobert Watson {
356785dcf349SGleb Smirnoff 	uint64_t allocs, frees, sleeps;
356848c5777eSRobert Watson 	uma_bucket_t bucket;
356948c5777eSRobert Watson 	uma_keg_t kz;
357048c5777eSRobert Watson 	uma_zone_t z;
357148c5777eSRobert Watson 	int cachefree;
357248c5777eSRobert Watson 
357303175483SAlexander Motin 	db_printf("%18s %8s %8s %8s %12s %8s %8s\n", "Zone", "Size", "Used",
357403175483SAlexander Motin 	    "Free", "Requests", "Sleeps", "Bucket");
357548c5777eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
357648c5777eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
357748c5777eSRobert Watson 			if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
357848c5777eSRobert Watson 				allocs = z->uz_allocs;
357948c5777eSRobert Watson 				frees = z->uz_frees;
3580bf965959SSean Bruno 				sleeps = z->uz_sleeps;
358148c5777eSRobert Watson 				cachefree = 0;
358248c5777eSRobert Watson 			} else
358348c5777eSRobert Watson 				uma_zone_sumstat(z, &cachefree, &allocs,
3584bf965959SSean Bruno 				    &frees, &sleeps);
3585e20a199fSJeff Roberson 			if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
358648c5777eSRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z)))
358748c5777eSRobert Watson 				cachefree += kz->uk_free;
3588fc03d22bSJeff Roberson 			LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
358948c5777eSRobert Watson 				cachefree += bucket->ub_cnt;
359003175483SAlexander Motin 			db_printf("%18s %8ju %8jd %8d %12ju %8ju %8u\n",
359103175483SAlexander Motin 			    z->uz_name, (uintmax_t)kz->uk_size,
3592ae4e9636SRobert Watson 			    (intmax_t)(allocs - frees), cachefree,
359303175483SAlexander Motin 			    (uintmax_t)allocs, sleeps, z->uz_count);
3594687c94aaSJohn Baldwin 			if (db_pager_quit)
3595687c94aaSJohn Baldwin 				return;
359648c5777eSRobert Watson 		}
359748c5777eSRobert Watson 	}
359848c5777eSRobert Watson }
359903175483SAlexander Motin 
360003175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache)
360103175483SAlexander Motin {
360203175483SAlexander Motin 	uint64_t allocs, frees;
360303175483SAlexander Motin 	uma_bucket_t bucket;
360403175483SAlexander Motin 	uma_zone_t z;
360503175483SAlexander Motin 	int cachefree;
360603175483SAlexander Motin 
360703175483SAlexander Motin 	db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
360803175483SAlexander Motin 	    "Requests", "Bucket");
360903175483SAlexander Motin 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
361003175483SAlexander Motin 		uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL);
361103175483SAlexander Motin 		LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
361203175483SAlexander Motin 			cachefree += bucket->ub_cnt;
361303175483SAlexander Motin 		db_printf("%18s %8ju %8jd %8d %12ju %8u\n",
361403175483SAlexander Motin 		    z->uz_name, (uintmax_t)z->uz_size,
361503175483SAlexander Motin 		    (intmax_t)(allocs - frees), cachefree,
361603175483SAlexander Motin 		    (uintmax_t)allocs, z->uz_count);
361703175483SAlexander Motin 		if (db_pager_quit)
361803175483SAlexander Motin 			return;
361903175483SAlexander Motin 	}
362003175483SAlexander Motin }
362148c5777eSRobert Watson #endif
3622