xref: /freebsd/sys/vm/uma_core.c (revision af5263743c65ac117daf33e52916a3d400fd0a61)
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>
7689f6b863SAttilio Rao #include <sys/rwlock.h>
777a52a97eSRobert Watson #include <sys/sbuf.h>
788355f576SJeff Roberson #include <sys/smp.h>
7986bbae32SJeff Roberson #include <sys/vmmeter.h>
8086bbae32SJeff Roberson 
818355f576SJeff Roberson #include <vm/vm.h>
828355f576SJeff Roberson #include <vm/vm_object.h>
838355f576SJeff Roberson #include <vm/vm_page.h>
84a4915c21SAttilio Rao #include <vm/vm_pageout.h>
858355f576SJeff Roberson #include <vm/vm_param.h>
868355f576SJeff Roberson #include <vm/vm_map.h>
878355f576SJeff Roberson #include <vm/vm_kern.h>
888355f576SJeff Roberson #include <vm/vm_extern.h>
898355f576SJeff Roberson #include <vm/uma.h>
908355f576SJeff Roberson #include <vm/uma_int.h>
91639c9550SJeff Roberson #include <vm/uma_dbg.h>
928355f576SJeff Roberson 
9348c5777eSRobert Watson #include <ddb/ddb.h>
9448c5777eSRobert Watson 
958d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
968d689e04SGleb Smirnoff #include <vm/memguard.h>
978d689e04SGleb Smirnoff #endif
988d689e04SGleb Smirnoff 
998355f576SJeff Roberson /*
100099a0e58SBosko Milekic  * This is the zone and keg from which all zones are spawned.  The idea is that
101099a0e58SBosko Milekic  * even the zone & keg heads are allocated from the allocator, so we use the
102099a0e58SBosko Milekic  * bss section to bootstrap us.
1038355f576SJeff Roberson  */
104099a0e58SBosko Milekic static struct uma_keg masterkeg;
105099a0e58SBosko Milekic static struct uma_zone masterzone_k;
106099a0e58SBosko Milekic static struct uma_zone masterzone_z;
107099a0e58SBosko Milekic static uma_zone_t kegs = &masterzone_k;
108099a0e58SBosko Milekic static uma_zone_t zones = &masterzone_z;
1098355f576SJeff Roberson 
1108355f576SJeff Roberson /* This is the zone from which all of uma_slab_t's are allocated. */
1118355f576SJeff Roberson static uma_zone_t slabzone;
112099a0e58SBosko Milekic static uma_zone_t slabrefzone;	/* With refcounters (for UMA_ZONE_REFCNT) */
1138355f576SJeff Roberson 
1148355f576SJeff Roberson /*
1158355f576SJeff Roberson  * The initial hash tables come out of this zone so they can be allocated
1168355f576SJeff Roberson  * prior to malloc coming up.
1178355f576SJeff Roberson  */
1188355f576SJeff Roberson static uma_zone_t hashzone;
1198355f576SJeff Roberson 
1201e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */
121e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1;
1221e319f6dSRobert Watson 
123961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
124961647dfSJeff Roberson 
1258355f576SJeff Roberson /*
12686bbae32SJeff Roberson  * Are we allowed to allocate buckets?
12786bbae32SJeff Roberson  */
12886bbae32SJeff Roberson static int bucketdisable = 1;
12986bbae32SJeff Roberson 
130099a0e58SBosko Milekic /* Linked list of all kegs in the system */
13113e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
1328355f576SJeff Roberson 
133099a0e58SBosko Milekic /* This mutex protects the keg list */
1340095a784SJeff Roberson static struct mtx_padalign uma_mtx;
1358355f576SJeff Roberson 
1368355f576SJeff Roberson /* Linked list of boot time pages */
1378355f576SJeff Roberson static LIST_HEAD(,uma_slab) uma_boot_pages =
13813e403fdSAntoine Brodin     LIST_HEAD_INITIALIZER(uma_boot_pages);
1398355f576SJeff Roberson 
140f353d338SAlan Cox /* This mutex protects the boot time pages list */
1410095a784SJeff Roberson static struct mtx_padalign uma_boot_pages_mtx;
1428355f576SJeff Roberson 
1438355f576SJeff Roberson /* Is the VM done starting up? */
1448355f576SJeff Roberson static int booted = 0;
145342f1793SAlan Cox #define	UMA_STARTUP	1
146342f1793SAlan Cox #define	UMA_STARTUP2	2
1478355f576SJeff Roberson 
148244f4554SBosko Milekic /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */
149ef72505eSJeff Roberson static const u_int uma_max_ipers = SLAB_SETSIZE;
150ef72505eSJeff Roberson 
151ef72505eSJeff Roberson /*
152ef72505eSJeff Roberson  * Only mbuf clusters use ref zones.  Just provide enough references
153ef72505eSJeff Roberson  * to support the one user.  New code should not use the ref facility.
154ef72505eSJeff Roberson  */
155ef72505eSJeff Roberson static const u_int uma_max_ipers_ref = PAGE_SIZE / MCLBYTES;
156244f4554SBosko Milekic 
1579643769aSJeff Roberson /*
1589643769aSJeff Roberson  * This is the handle used to schedule events that need to happen
1599643769aSJeff Roberson  * outside of the allocation fast path.
1609643769aSJeff Roberson  */
1618355f576SJeff Roberson static struct callout uma_callout;
1629643769aSJeff Roberson #define	UMA_TIMEOUT	20		/* Seconds for callout interval. */
1638355f576SJeff Roberson 
1648355f576SJeff Roberson /*
1658355f576SJeff Roberson  * This structure is passed as the zone ctor arg so that I don't have to create
1668355f576SJeff Roberson  * a special allocation function just for zones.
1678355f576SJeff Roberson  */
1688355f576SJeff Roberson struct uma_zctor_args {
169bb196eb4SMatthew D Fleming 	const char *name;
170c3bdc05fSAndrew R. Reiter 	size_t size;
1718355f576SJeff Roberson 	uma_ctor ctor;
1728355f576SJeff Roberson 	uma_dtor dtor;
1738355f576SJeff Roberson 	uma_init uminit;
1748355f576SJeff Roberson 	uma_fini fini;
1750095a784SJeff Roberson 	uma_import import;
1760095a784SJeff Roberson 	uma_release release;
1770095a784SJeff Roberson 	void *arg;
178099a0e58SBosko Milekic 	uma_keg_t keg;
179099a0e58SBosko Milekic 	int align;
18085dcf349SGleb Smirnoff 	uint32_t flags;
181099a0e58SBosko Milekic };
182099a0e58SBosko Milekic 
183099a0e58SBosko Milekic struct uma_kctor_args {
184099a0e58SBosko Milekic 	uma_zone_t zone;
185099a0e58SBosko Milekic 	size_t size;
186099a0e58SBosko Milekic 	uma_init uminit;
187099a0e58SBosko Milekic 	uma_fini fini;
1888355f576SJeff Roberson 	int align;
18985dcf349SGleb Smirnoff 	uint32_t flags;
1908355f576SJeff Roberson };
1918355f576SJeff Roberson 
192cae33c14SJeff Roberson struct uma_bucket_zone {
193cae33c14SJeff Roberson 	uma_zone_t	ubz_zone;
194cae33c14SJeff Roberson 	char		*ubz_name;
195fc03d22bSJeff Roberson 	int		ubz_entries;	/* Number of items it can hold. */
196fc03d22bSJeff Roberson 	int		ubz_maxsize;	/* Maximum allocation size per-item. */
197cae33c14SJeff Roberson };
198cae33c14SJeff Roberson 
199f9d27e75SRobert Watson /*
200fc03d22bSJeff Roberson  * Compute the actual number of bucket entries to pack them in power
201fc03d22bSJeff Roberson  * of two sizes for more efficient space utilization.
202f9d27e75SRobert Watson  */
203fc03d22bSJeff Roberson #define	BUCKET_SIZE(n)						\
204fc03d22bSJeff Roberson     (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *))
205fc03d22bSJeff Roberson 
206fc03d22bSJeff Roberson #define	BUCKET_MAX	BUCKET_SIZE(128)
207fc03d22bSJeff Roberson 
208fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = {
209fc03d22bSJeff Roberson 	{ NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
210fc03d22bSJeff Roberson 	{ NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
211fc03d22bSJeff Roberson 	{ NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
212fc03d22bSJeff Roberson 	{ NULL, NULL, 0}
213fc03d22bSJeff Roberson };
214fc03d22bSJeff Roberson static uma_zone_t largebucket;
215cae33c14SJeff Roberson 
2162019094aSRobert Watson /*
2172019094aSRobert Watson  * Flags and enumerations to be passed to internal functions.
2182019094aSRobert Watson  */
219ef72505eSJeff Roberson enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI };
220b23f72e9SBrian Feldman 
2218355f576SJeff Roberson /* Prototypes.. */
2228355f576SJeff Roberson 
22385dcf349SGleb Smirnoff static void *noobj_alloc(uma_zone_t, int, uint8_t *, int);
22485dcf349SGleb Smirnoff static void *page_alloc(uma_zone_t, int, uint8_t *, int);
22585dcf349SGleb Smirnoff static void *startup_alloc(uma_zone_t, int, uint8_t *, int);
22685dcf349SGleb Smirnoff static void page_free(void *, int, uint8_t);
227e20a199fSJeff Roberson static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int);
2289643769aSJeff Roberson static void cache_drain(uma_zone_t);
2298355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t);
230aaa8bb16SJeff Roberson static void bucket_cache_drain(uma_zone_t zone);
231b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int);
232099a0e58SBosko Milekic static void keg_dtor(void *, int, void *);
233b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int);
2349c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *);
235b23f72e9SBrian Feldman static int zero_init(void *, int, int);
236e20a199fSJeff Roberson static void keg_small_init(uma_keg_t keg);
237e20a199fSJeff Roberson static void keg_large_init(uma_keg_t keg);
2388355f576SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t));
2398355f576SJeff Roberson static void zone_timeout(uma_zone_t zone);
2400aef6126SJeff Roberson static int hash_alloc(struct uma_hash *);
2410aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *);
2420aef6126SJeff Roberson static void hash_free(struct uma_hash *hash);
2438355f576SJeff Roberson static void uma_timeout(void *);
2448355f576SJeff Roberson static void uma_startup3(void);
245e20a199fSJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int);
2460095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
24786bbae32SJeff Roberson static void bucket_enable(void);
248cae33c14SJeff Roberson static void bucket_init(void);
249*af526374SJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, int);
250*af526374SJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t);
251cae33c14SJeff Roberson static void bucket_zone_drain(void);
252fc03d22bSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t zone, int flags);
253e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags);
254e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags);
2550095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
2560095a784SJeff Roberson static void slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item);
257e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
25885dcf349SGleb Smirnoff     uma_fini fini, int align, uint32_t flags);
2590095a784SJeff Roberson static int zone_import(uma_zone_t zone, void **bucket, int max, int flags);
2600095a784SJeff Roberson static void zone_release(uma_zone_t zone, void **bucket, int cnt);
261bbee39c6SJeff Roberson 
2628355f576SJeff Roberson void uma_print_zone(uma_zone_t);
2638355f576SJeff Roberson void uma_print_stats(void);
2647a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
2657a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
2668355f576SJeff Roberson 
2678355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
2688355f576SJeff Roberson 
2697a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
2707a52a97eSRobert Watson     0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
2717a52a97eSRobert Watson 
2727a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
2737a52a97eSRobert Watson     0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
2747a52a97eSRobert Watson 
2752f891cd5SPawel Jakub Dawidek static int zone_warnings = 1;
2762f891cd5SPawel Jakub Dawidek TUNABLE_INT("vm.zone_warnings", &zone_warnings);
2772f891cd5SPawel Jakub Dawidek SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RW, &zone_warnings, 0,
2782f891cd5SPawel Jakub Dawidek     "Warn when UMA zones becomes full");
2792f891cd5SPawel Jakub Dawidek 
28086bbae32SJeff Roberson /*
28186bbae32SJeff Roberson  * This routine checks to see whether or not it's safe to enable buckets.
28286bbae32SJeff Roberson  */
28386bbae32SJeff Roberson static void
28486bbae32SJeff Roberson bucket_enable(void)
28586bbae32SJeff Roberson {
286251386b4SMaksim Yevmenkin 	bucketdisable = vm_page_count_min();
28786bbae32SJeff Roberson }
28886bbae32SJeff Roberson 
289dc2c7965SRobert Watson /*
290dc2c7965SRobert Watson  * Initialize bucket_zones, the array of zones of buckets of various sizes.
291dc2c7965SRobert Watson  *
292dc2c7965SRobert Watson  * For each zone, calculate the memory required for each bucket, consisting
293fc03d22bSJeff Roberson  * of the header and an array of pointers.
294dc2c7965SRobert Watson  */
295cae33c14SJeff Roberson static void
296cae33c14SJeff Roberson bucket_init(void)
297cae33c14SJeff Roberson {
298cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
299cae33c14SJeff Roberson 	int size;
300fc03d22bSJeff Roberson 	int i;
301cae33c14SJeff Roberson 
302fc03d22bSJeff Roberson 	for (i = 0, ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
303cae33c14SJeff Roberson 		size = roundup(sizeof(struct uma_bucket), sizeof(void *));
304cae33c14SJeff Roberson 		size += sizeof(void *) * ubz->ubz_entries;
305cae33c14SJeff Roberson 		ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
306e20a199fSJeff Roberson 		    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
307fc03d22bSJeff Roberson 		    UMA_ZONE_MAXBUCKET | UMA_ZONE_MTXCLASS);
308cae33c14SJeff Roberson 	}
309fc03d22bSJeff Roberson 	/*
310fc03d22bSJeff Roberson 	 * To avoid recursive bucket allocation loops we disable buckets
311fc03d22bSJeff Roberson 	 * on the smallest bucket zone and use it for the largest zone.
312fc03d22bSJeff Roberson 	 * The remainder of the zones all use the largest zone.
313fc03d22bSJeff Roberson 	 */
314fc03d22bSJeff Roberson 	ubz--;
315fc03d22bSJeff Roberson 	ubz->ubz_zone->uz_count = bucket_zones[0].ubz_entries;
316fc03d22bSJeff Roberson 	bucket_zones[0].ubz_zone->uz_count = 0;
317fc03d22bSJeff Roberson 	largebucket = ubz->ubz_zone;
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
353*af526374SJeff Roberson bucket_alloc(uma_zone_t zone, 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);
366dc2c7965SRobert Watson 
367*af526374SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
368*af526374SJeff Roberson 		flags |= M_NOVM;
369*af526374SJeff Roberson 	ubz = bucket_zone_lookup(zone->uz_count);
370*af526374SJeff Roberson 	bucket = uma_zalloc(ubz->ubz_zone, flags);
371cae33c14SJeff Roberson 	if (bucket) {
372cae33c14SJeff Roberson #ifdef INVARIANTS
373cae33c14SJeff Roberson 		bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
374cae33c14SJeff Roberson #endif
375cae33c14SJeff Roberson 		bucket->ub_cnt = 0;
376cae33c14SJeff Roberson 		bucket->ub_entries = ubz->ubz_entries;
377cae33c14SJeff Roberson 	}
378cae33c14SJeff Roberson 
379cae33c14SJeff Roberson 	return (bucket);
380cae33c14SJeff Roberson }
381cae33c14SJeff Roberson 
382cae33c14SJeff Roberson static void
383*af526374SJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket)
384cae33c14SJeff Roberson {
385cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
386cae33c14SJeff Roberson 
387fc03d22bSJeff Roberson 	KASSERT(bucket->ub_cnt == 0,
388fc03d22bSJeff Roberson 	    ("bucket_free: Freeing a non free bucket."));
389dc2c7965SRobert Watson 	ubz = bucket_zone_lookup(bucket->ub_entries);
390fc03d22bSJeff Roberson 	uma_zfree(ubz->ubz_zone, bucket);
391cae33c14SJeff Roberson }
392cae33c14SJeff Roberson 
393cae33c14SJeff Roberson static void
394cae33c14SJeff Roberson bucket_zone_drain(void)
395cae33c14SJeff Roberson {
396cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
397cae33c14SJeff Roberson 
398cae33c14SJeff Roberson 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
399cae33c14SJeff Roberson 		zone_drain(ubz->ubz_zone);
400cae33c14SJeff Roberson }
401cae33c14SJeff Roberson 
4022f891cd5SPawel Jakub Dawidek static void
4032f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone)
4042f891cd5SPawel Jakub Dawidek {
4052f891cd5SPawel Jakub Dawidek 	static const struct timeval warninterval = { 300, 0 };
4062f891cd5SPawel Jakub Dawidek 
4072f891cd5SPawel Jakub Dawidek 	if (!zone_warnings || zone->uz_warning == NULL)
4082f891cd5SPawel Jakub Dawidek 		return;
4092f891cd5SPawel Jakub Dawidek 
4102f891cd5SPawel Jakub Dawidek 	if (ratecheck(&zone->uz_ratecheck, &warninterval))
4112f891cd5SPawel Jakub Dawidek 		printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
4122f891cd5SPawel Jakub Dawidek }
4132f891cd5SPawel Jakub Dawidek 
414e20a199fSJeff Roberson static void
415e20a199fSJeff Roberson zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
416e20a199fSJeff Roberson {
417e20a199fSJeff Roberson 	uma_klink_t klink;
418e20a199fSJeff Roberson 
419e20a199fSJeff Roberson 	LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
420e20a199fSJeff Roberson 		kegfn(klink->kl_keg);
421e20a199fSJeff Roberson }
4228355f576SJeff Roberson 
4238355f576SJeff Roberson /*
4248355f576SJeff Roberson  * Routine called by timeout which is used to fire off some time interval
4259643769aSJeff Roberson  * based calculations.  (stats, hash size, etc.)
4268355f576SJeff Roberson  *
4278355f576SJeff Roberson  * Arguments:
4288355f576SJeff Roberson  *	arg   Unused
4298355f576SJeff Roberson  *
4308355f576SJeff Roberson  * Returns:
4318355f576SJeff Roberson  *	Nothing
4328355f576SJeff Roberson  */
4338355f576SJeff Roberson static void
4348355f576SJeff Roberson uma_timeout(void *unused)
4358355f576SJeff Roberson {
43686bbae32SJeff Roberson 	bucket_enable();
4378355f576SJeff Roberson 	zone_foreach(zone_timeout);
4388355f576SJeff Roberson 
4398355f576SJeff Roberson 	/* Reschedule this event */
4409643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
4418355f576SJeff Roberson }
4428355f576SJeff Roberson 
4438355f576SJeff Roberson /*
4449643769aSJeff Roberson  * Routine to perform timeout driven calculations.  This expands the
4459643769aSJeff Roberson  * hashes and does per cpu statistics aggregation.
4468355f576SJeff Roberson  *
447e20a199fSJeff Roberson  *  Returns nothing.
4488355f576SJeff Roberson  */
4498355f576SJeff Roberson static void
450e20a199fSJeff Roberson keg_timeout(uma_keg_t keg)
4518355f576SJeff Roberson {
4528355f576SJeff Roberson 
453e20a199fSJeff Roberson 	KEG_LOCK(keg);
4548355f576SJeff Roberson 	/*
455e20a199fSJeff Roberson 	 * Expand the keg hash table.
4568355f576SJeff Roberson 	 *
4578355f576SJeff Roberson 	 * This is done if the number of slabs is larger than the hash size.
4588355f576SJeff Roberson 	 * What I'm trying to do here is completely reduce collisions.  This
4598355f576SJeff Roberson 	 * may be a little aggressive.  Should I allow for two collisions max?
4608355f576SJeff Roberson 	 */
461099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_HASH &&
462099a0e58SBosko Milekic 	    keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
4630aef6126SJeff Roberson 		struct uma_hash newhash;
4640aef6126SJeff Roberson 		struct uma_hash oldhash;
4650aef6126SJeff Roberson 		int ret;
4665300d9ddSJeff Roberson 
4670aef6126SJeff Roberson 		/*
4680aef6126SJeff Roberson 		 * This is so involved because allocating and freeing
469e20a199fSJeff Roberson 		 * while the keg lock is held will lead to deadlock.
4700aef6126SJeff Roberson 		 * I have to do everything in stages and check for
4710aef6126SJeff Roberson 		 * races.
4720aef6126SJeff Roberson 		 */
473099a0e58SBosko Milekic 		newhash = keg->uk_hash;
474e20a199fSJeff Roberson 		KEG_UNLOCK(keg);
4750aef6126SJeff Roberson 		ret = hash_alloc(&newhash);
476e20a199fSJeff Roberson 		KEG_LOCK(keg);
4770aef6126SJeff Roberson 		if (ret) {
478099a0e58SBosko Milekic 			if (hash_expand(&keg->uk_hash, &newhash)) {
479099a0e58SBosko Milekic 				oldhash = keg->uk_hash;
480099a0e58SBosko Milekic 				keg->uk_hash = newhash;
4810aef6126SJeff Roberson 			} else
4820aef6126SJeff Roberson 				oldhash = newhash;
4830aef6126SJeff Roberson 
484e20a199fSJeff Roberson 			KEG_UNLOCK(keg);
4850aef6126SJeff Roberson 			hash_free(&oldhash);
486e20a199fSJeff Roberson 			KEG_LOCK(keg);
4870aef6126SJeff Roberson 		}
4885300d9ddSJeff Roberson 	}
489e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
490e20a199fSJeff Roberson }
491e20a199fSJeff Roberson 
492e20a199fSJeff Roberson static void
493e20a199fSJeff Roberson zone_timeout(uma_zone_t zone)
494e20a199fSJeff Roberson {
495e20a199fSJeff Roberson 
496e20a199fSJeff Roberson 	zone_foreach_keg(zone, &keg_timeout);
4978355f576SJeff Roberson }
4988355f576SJeff Roberson 
4998355f576SJeff Roberson /*
5005300d9ddSJeff Roberson  * Allocate and zero fill the next sized hash table from the appropriate
5015300d9ddSJeff Roberson  * backing store.
5025300d9ddSJeff Roberson  *
5035300d9ddSJeff Roberson  * Arguments:
5040aef6126SJeff Roberson  *	hash  A new hash structure with the old hash size in uh_hashsize
5055300d9ddSJeff Roberson  *
5065300d9ddSJeff Roberson  * Returns:
5070aef6126SJeff Roberson  *	1 on sucess and 0 on failure.
5085300d9ddSJeff Roberson  */
50937c84183SPoul-Henning Kamp static int
5100aef6126SJeff Roberson hash_alloc(struct uma_hash *hash)
5115300d9ddSJeff Roberson {
5120aef6126SJeff Roberson 	int oldsize;
5135300d9ddSJeff Roberson 	int alloc;
5145300d9ddSJeff Roberson 
5150aef6126SJeff Roberson 	oldsize = hash->uh_hashsize;
5160aef6126SJeff Roberson 
5175300d9ddSJeff Roberson 	/* We're just going to go to a power of two greater */
5180aef6126SJeff Roberson 	if (oldsize)  {
5190aef6126SJeff Roberson 		hash->uh_hashsize = oldsize * 2;
5200aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
5210aef6126SJeff Roberson 		hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
522961647dfSJeff Roberson 		    M_UMAHASH, M_NOWAIT);
5235300d9ddSJeff Roberson 	} else {
5240aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
525e20a199fSJeff Roberson 		hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
526a163d034SWarner Losh 		    M_WAITOK);
5270aef6126SJeff Roberson 		hash->uh_hashsize = UMA_HASH_SIZE_INIT;
5285300d9ddSJeff Roberson 	}
5290aef6126SJeff Roberson 	if (hash->uh_slab_hash) {
5300aef6126SJeff Roberson 		bzero(hash->uh_slab_hash, alloc);
5310aef6126SJeff Roberson 		hash->uh_hashmask = hash->uh_hashsize - 1;
5320aef6126SJeff Roberson 		return (1);
5330aef6126SJeff Roberson 	}
5345300d9ddSJeff Roberson 
5350aef6126SJeff Roberson 	return (0);
5365300d9ddSJeff Roberson }
5375300d9ddSJeff Roberson 
5385300d9ddSJeff Roberson /*
53964f051e9SJeff Roberson  * Expands the hash table for HASH zones.  This is done from zone_timeout
54064f051e9SJeff Roberson  * to reduce collisions.  This must not be done in the regular allocation
54164f051e9SJeff Roberson  * path, otherwise, we can recurse on the vm while allocating pages.
5428355f576SJeff Roberson  *
5438355f576SJeff Roberson  * Arguments:
5440aef6126SJeff Roberson  *	oldhash  The hash you want to expand
5450aef6126SJeff Roberson  *	newhash  The hash structure for the new table
5468355f576SJeff Roberson  *
5478355f576SJeff Roberson  * Returns:
5488355f576SJeff Roberson  *	Nothing
5498355f576SJeff Roberson  *
5508355f576SJeff Roberson  * Discussion:
5518355f576SJeff Roberson  */
5520aef6126SJeff Roberson static int
5530aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
5548355f576SJeff Roberson {
5558355f576SJeff Roberson 	uma_slab_t slab;
5568355f576SJeff Roberson 	int hval;
5578355f576SJeff Roberson 	int i;
5588355f576SJeff Roberson 
5590aef6126SJeff Roberson 	if (!newhash->uh_slab_hash)
5600aef6126SJeff Roberson 		return (0);
5618355f576SJeff Roberson 
5620aef6126SJeff Roberson 	if (oldhash->uh_hashsize >= newhash->uh_hashsize)
5630aef6126SJeff Roberson 		return (0);
5648355f576SJeff Roberson 
5658355f576SJeff Roberson 	/*
5668355f576SJeff Roberson 	 * I need to investigate hash algorithms for resizing without a
5678355f576SJeff Roberson 	 * full rehash.
5688355f576SJeff Roberson 	 */
5698355f576SJeff Roberson 
5700aef6126SJeff Roberson 	for (i = 0; i < oldhash->uh_hashsize; i++)
5710aef6126SJeff Roberson 		while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
5720aef6126SJeff Roberson 			slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
5730aef6126SJeff Roberson 			SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
5740aef6126SJeff Roberson 			hval = UMA_HASH(newhash, slab->us_data);
5750aef6126SJeff Roberson 			SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
5760aef6126SJeff Roberson 			    slab, us_hlink);
5778355f576SJeff Roberson 		}
5788355f576SJeff Roberson 
5790aef6126SJeff Roberson 	return (1);
5809c2cd7e5SJeff Roberson }
5819c2cd7e5SJeff Roberson 
5825300d9ddSJeff Roberson /*
5835300d9ddSJeff Roberson  * Free the hash bucket to the appropriate backing store.
5845300d9ddSJeff Roberson  *
5855300d9ddSJeff Roberson  * Arguments:
5865300d9ddSJeff Roberson  *	slab_hash  The hash bucket we're freeing
5875300d9ddSJeff Roberson  *	hashsize   The number of entries in that hash bucket
5885300d9ddSJeff Roberson  *
5895300d9ddSJeff Roberson  * Returns:
5905300d9ddSJeff Roberson  *	Nothing
5915300d9ddSJeff Roberson  */
5929c2cd7e5SJeff Roberson static void
5930aef6126SJeff Roberson hash_free(struct uma_hash *hash)
5949c2cd7e5SJeff Roberson {
5950aef6126SJeff Roberson 	if (hash->uh_slab_hash == NULL)
5960aef6126SJeff Roberson 		return;
5970aef6126SJeff Roberson 	if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
5980095a784SJeff Roberson 		zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
5998355f576SJeff Roberson 	else
600961647dfSJeff Roberson 		free(hash->uh_slab_hash, M_UMAHASH);
6018355f576SJeff Roberson }
6028355f576SJeff Roberson 
6038355f576SJeff Roberson /*
6048355f576SJeff Roberson  * Frees all outstanding items in a bucket
6058355f576SJeff Roberson  *
6068355f576SJeff Roberson  * Arguments:
6078355f576SJeff Roberson  *	zone   The zone to free to, must be unlocked.
6088355f576SJeff Roberson  *	bucket The free/alloc bucket with items, cpu queue must be locked.
6098355f576SJeff Roberson  *
6108355f576SJeff Roberson  * Returns:
6118355f576SJeff Roberson  *	Nothing
6128355f576SJeff Roberson  */
6138355f576SJeff Roberson 
6148355f576SJeff Roberson static void
6158355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
6168355f576SJeff Roberson {
6170095a784SJeff Roberson 	int i;
6188355f576SJeff Roberson 
6198355f576SJeff Roberson 	if (bucket == NULL)
6208355f576SJeff Roberson 		return;
6218355f576SJeff Roberson 
6220095a784SJeff Roberson 	if (zone->uz_fini)
6230095a784SJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
6240095a784SJeff Roberson 			zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
6250095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
6260095a784SJeff Roberson 	bucket->ub_cnt = 0;
6278355f576SJeff Roberson }
6288355f576SJeff Roberson 
6298355f576SJeff Roberson /*
6308355f576SJeff Roberson  * Drains the per cpu caches for a zone.
6318355f576SJeff Roberson  *
6325d1ae027SRobert Watson  * NOTE: This may only be called while the zone is being turn down, and not
6335d1ae027SRobert Watson  * during normal operation.  This is necessary in order that we do not have
6345d1ae027SRobert Watson  * to migrate CPUs to drain the per-CPU caches.
6355d1ae027SRobert Watson  *
6368355f576SJeff Roberson  * Arguments:
6378355f576SJeff Roberson  *	zone     The zone to drain, must be unlocked.
6388355f576SJeff Roberson  *
6398355f576SJeff Roberson  * Returns:
6408355f576SJeff Roberson  *	Nothing
6418355f576SJeff Roberson  */
6428355f576SJeff Roberson static void
6439643769aSJeff Roberson cache_drain(uma_zone_t zone)
6448355f576SJeff Roberson {
6458355f576SJeff Roberson 	uma_cache_t cache;
6468355f576SJeff Roberson 	int cpu;
6478355f576SJeff Roberson 
6488355f576SJeff Roberson 	/*
6495d1ae027SRobert Watson 	 * XXX: It is safe to not lock the per-CPU caches, because we're
6505d1ae027SRobert Watson 	 * tearing down the zone anyway.  I.e., there will be no further use
6515d1ae027SRobert Watson 	 * of the caches at this point.
6525d1ae027SRobert Watson 	 *
6535d1ae027SRobert Watson 	 * XXX: It would good to be able to assert that the zone is being
6545d1ae027SRobert Watson 	 * torn down to prevent improper use of cache_drain().
6555d1ae027SRobert Watson 	 *
6565d1ae027SRobert Watson 	 * XXX: We lock the zone before passing into bucket_cache_drain() as
6575d1ae027SRobert Watson 	 * it is used elsewhere.  Should the tear-down path be made special
6585d1ae027SRobert Watson 	 * there in some form?
6598355f576SJeff Roberson 	 */
6603aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
6618355f576SJeff Roberson 		cache = &zone->uz_cpu[cpu];
6628355f576SJeff Roberson 		bucket_drain(zone, cache->uc_allocbucket);
6638355f576SJeff Roberson 		bucket_drain(zone, cache->uc_freebucket);
664174ab450SBosko Milekic 		if (cache->uc_allocbucket != NULL)
665*af526374SJeff Roberson 			bucket_free(zone, cache->uc_allocbucket);
666174ab450SBosko Milekic 		if (cache->uc_freebucket != NULL)
667*af526374SJeff Roberson 			bucket_free(zone, cache->uc_freebucket);
668d56368d7SBosko Milekic 		cache->uc_allocbucket = cache->uc_freebucket = NULL;
669d56368d7SBosko Milekic 	}
670aaa8bb16SJeff Roberson 	ZONE_LOCK(zone);
671aaa8bb16SJeff Roberson 	bucket_cache_drain(zone);
672aaa8bb16SJeff Roberson 	ZONE_UNLOCK(zone);
673aaa8bb16SJeff Roberson }
674aaa8bb16SJeff Roberson 
675aaa8bb16SJeff Roberson /*
676aaa8bb16SJeff Roberson  * Drain the cached buckets from a zone.  Expects a locked zone on entry.
677aaa8bb16SJeff Roberson  */
678aaa8bb16SJeff Roberson static void
679aaa8bb16SJeff Roberson bucket_cache_drain(uma_zone_t zone)
680aaa8bb16SJeff Roberson {
681aaa8bb16SJeff Roberson 	uma_bucket_t bucket;
6828355f576SJeff Roberson 
6838355f576SJeff Roberson 	/*
6848355f576SJeff Roberson 	 * Drain the bucket queues and free the buckets, we just keep two per
6858355f576SJeff Roberson 	 * cpu (alloc/free).
6868355f576SJeff Roberson 	 */
687fc03d22bSJeff Roberson 	while ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
6888355f576SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
6898355f576SJeff Roberson 		ZONE_UNLOCK(zone);
6908355f576SJeff Roberson 		bucket_drain(zone, bucket);
691*af526374SJeff Roberson 		bucket_free(zone, bucket);
6928355f576SJeff Roberson 		ZONE_LOCK(zone);
6938355f576SJeff Roberson 	}
6948355f576SJeff Roberson }
695fc03d22bSJeff Roberson 
696fc03d22bSJeff Roberson static void
697fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
698fc03d22bSJeff Roberson {
699fc03d22bSJeff Roberson 	uint8_t *mem;
700fc03d22bSJeff Roberson 	int i;
701fc03d22bSJeff Roberson 	uint8_t flags;
702fc03d22bSJeff Roberson 
703fc03d22bSJeff Roberson 	mem = slab->us_data;
704fc03d22bSJeff Roberson 	flags = slab->us_flags;
705fc03d22bSJeff Roberson 	i = start;
706fc03d22bSJeff Roberson 	if (keg->uk_fini != NULL) {
707fc03d22bSJeff Roberson 		for (i--; i > -1; i--)
708fc03d22bSJeff Roberson 			keg->uk_fini(slab->us_data + (keg->uk_rsize * i),
709fc03d22bSJeff Roberson 			    keg->uk_size);
710fc03d22bSJeff Roberson 	}
711fc03d22bSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_VTOSLAB) {
712fc03d22bSJeff Roberson 		vm_object_t obj;
713fc03d22bSJeff Roberson 
714fc03d22bSJeff Roberson 		if (flags & UMA_SLAB_KMEM)
715fc03d22bSJeff Roberson 			obj = kmem_object;
716fc03d22bSJeff Roberson 		else if (flags & UMA_SLAB_KERNEL)
717fc03d22bSJeff Roberson 			obj = kernel_object;
718fc03d22bSJeff Roberson 		else
719fc03d22bSJeff Roberson 			obj = NULL;
720fc03d22bSJeff Roberson 		for (i = 0; i < keg->uk_ppera; i++)
721fc03d22bSJeff Roberson 			vsetobj((vm_offset_t)mem + (i * PAGE_SIZE), obj);
722fc03d22bSJeff Roberson 	}
723fc03d22bSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_OFFPAGE)
724fc03d22bSJeff Roberson 		zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
725fc03d22bSJeff Roberson #ifdef UMA_DEBUG
726fc03d22bSJeff Roberson 	printf("%s: Returning %d bytes.\n", keg->uk_name,
727fc03d22bSJeff Roberson 	    PAGE_SIZE * keg->uk_ppera);
728fc03d22bSJeff Roberson #endif
729fc03d22bSJeff Roberson 	keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
7308355f576SJeff Roberson }
7318355f576SJeff Roberson 
7328355f576SJeff Roberson /*
733e20a199fSJeff Roberson  * Frees pages from a keg back to the system.  This is done on demand from
7348355f576SJeff Roberson  * the pageout daemon.
7358355f576SJeff Roberson  *
736e20a199fSJeff Roberson  * Returns nothing.
7378355f576SJeff Roberson  */
738e20a199fSJeff Roberson static void
739e20a199fSJeff Roberson keg_drain(uma_keg_t keg)
7408355f576SJeff Roberson {
7411e183df2SStefan Farfeleder 	struct slabhead freeslabs = { 0 };
7428355f576SJeff Roberson 	uma_slab_t slab;
7438355f576SJeff Roberson 	uma_slab_t n;
7448355f576SJeff Roberson 
7458355f576SJeff Roberson 	/*
746e20a199fSJeff Roberson 	 * We don't want to take pages from statically allocated kegs at this
7478355f576SJeff Roberson 	 * time
7488355f576SJeff Roberson 	 */
749099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
7508355f576SJeff Roberson 		return;
7518355f576SJeff Roberson 
7528355f576SJeff Roberson #ifdef UMA_DEBUG
753e20a199fSJeff Roberson 	printf("%s free items: %u\n", keg->uk_name, keg->uk_free);
7548355f576SJeff Roberson #endif
755e20a199fSJeff Roberson 	KEG_LOCK(keg);
756099a0e58SBosko Milekic 	if (keg->uk_free == 0)
7578355f576SJeff Roberson 		goto finished;
7588355f576SJeff Roberson 
759099a0e58SBosko Milekic 	slab = LIST_FIRST(&keg->uk_free_slab);
7609643769aSJeff Roberson 	while (slab) {
7618355f576SJeff Roberson 		n = LIST_NEXT(slab, us_link);
7628355f576SJeff Roberson 
7638355f576SJeff Roberson 		/* We have no where to free these to */
7648355f576SJeff Roberson 		if (slab->us_flags & UMA_SLAB_BOOT) {
7658355f576SJeff Roberson 			slab = n;
7668355f576SJeff Roberson 			continue;
7678355f576SJeff Roberson 		}
7688355f576SJeff Roberson 
7698355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
770099a0e58SBosko Milekic 		keg->uk_pages -= keg->uk_ppera;
771099a0e58SBosko Milekic 		keg->uk_free -= keg->uk_ipers;
772713deb36SJeff Roberson 
773099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZONE_HASH)
774099a0e58SBosko Milekic 			UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
775713deb36SJeff Roberson 
776713deb36SJeff Roberson 		SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
777713deb36SJeff Roberson 
778713deb36SJeff Roberson 		slab = n;
779713deb36SJeff Roberson 	}
780713deb36SJeff Roberson finished:
781e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
782713deb36SJeff Roberson 
783713deb36SJeff Roberson 	while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
784713deb36SJeff Roberson 		SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
785fc03d22bSJeff Roberson 		keg_free_slab(keg, slab, 0);
7868355f576SJeff Roberson 	}
7878355f576SJeff Roberson }
7888355f576SJeff Roberson 
789e20a199fSJeff Roberson static void
790e20a199fSJeff Roberson zone_drain_wait(uma_zone_t zone, int waitok)
791e20a199fSJeff Roberson {
792e20a199fSJeff Roberson 
7938355f576SJeff Roberson 	/*
794e20a199fSJeff Roberson 	 * Set draining to interlock with zone_dtor() so we can release our
795e20a199fSJeff Roberson 	 * locks as we go.  Only dtor() should do a WAITOK call since it
796e20a199fSJeff Roberson 	 * is the only call that knows the structure will still be available
797e20a199fSJeff Roberson 	 * when it wakes up.
798e20a199fSJeff Roberson 	 */
799e20a199fSJeff Roberson 	ZONE_LOCK(zone);
800e20a199fSJeff Roberson 	while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
801e20a199fSJeff Roberson 		if (waitok == M_NOWAIT)
802e20a199fSJeff Roberson 			goto out;
803e20a199fSJeff Roberson 		mtx_unlock(&uma_mtx);
804*af526374SJeff Roberson 		msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1);
805e20a199fSJeff Roberson 		mtx_lock(&uma_mtx);
806e20a199fSJeff Roberson 	}
807e20a199fSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_DRAINING;
808e20a199fSJeff Roberson 	bucket_cache_drain(zone);
809e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
810e20a199fSJeff Roberson 	/*
811e20a199fSJeff Roberson 	 * The DRAINING flag protects us from being freed while
812e20a199fSJeff Roberson 	 * we're running.  Normally the uma_mtx would protect us but we
813e20a199fSJeff Roberson 	 * must be able to release and acquire the right lock for each keg.
814e20a199fSJeff Roberson 	 */
815e20a199fSJeff Roberson 	zone_foreach_keg(zone, &keg_drain);
816e20a199fSJeff Roberson 	ZONE_LOCK(zone);
817e20a199fSJeff Roberson 	zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
818e20a199fSJeff Roberson 	wakeup(zone);
819e20a199fSJeff Roberson out:
820e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
821e20a199fSJeff Roberson }
822e20a199fSJeff Roberson 
823e20a199fSJeff Roberson void
824e20a199fSJeff Roberson zone_drain(uma_zone_t zone)
825e20a199fSJeff Roberson {
826e20a199fSJeff Roberson 
827e20a199fSJeff Roberson 	zone_drain_wait(zone, M_NOWAIT);
828e20a199fSJeff Roberson }
829e20a199fSJeff Roberson 
830e20a199fSJeff Roberson /*
831e20a199fSJeff Roberson  * Allocate a new slab for a keg.  This does not insert the slab onto a list.
8328355f576SJeff Roberson  *
8338355f576SJeff Roberson  * Arguments:
8348355f576SJeff Roberson  *	wait  Shall we wait?
8358355f576SJeff Roberson  *
8368355f576SJeff Roberson  * Returns:
8378355f576SJeff Roberson  *	The slab that was allocated or NULL if there is no memory and the
8388355f576SJeff Roberson  *	caller specified M_NOWAIT.
8398355f576SJeff Roberson  */
8408355f576SJeff Roberson static uma_slab_t
841e20a199fSJeff Roberson keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait)
8428355f576SJeff Roberson {
843099a0e58SBosko Milekic 	uma_slabrefcnt_t slabref;
844e20a199fSJeff Roberson 	uma_alloc allocf;
845099a0e58SBosko Milekic 	uma_slab_t slab;
84685dcf349SGleb Smirnoff 	uint8_t *mem;
84785dcf349SGleb Smirnoff 	uint8_t flags;
8488355f576SJeff Roberson 	int i;
8498355f576SJeff Roberson 
850e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
851a553d4b8SJeff Roberson 	slab = NULL;
852fc03d22bSJeff Roberson 	mem = NULL;
853a553d4b8SJeff Roberson 
8548355f576SJeff Roberson #ifdef UMA_DEBUG
8550095a784SJeff Roberson 	printf("alloc_slab:  Allocating a new slab for %s\n", keg->uk_name);
8568355f576SJeff Roberson #endif
857e20a199fSJeff Roberson 	allocf = keg->uk_allocf;
858e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
859a553d4b8SJeff Roberson 
860099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
861e20a199fSJeff Roberson 		slab = zone_alloc_item(keg->uk_slabzone, NULL, wait);
862fc03d22bSJeff Roberson 		if (slab == NULL)
863fc03d22bSJeff Roberson 			goto out;
864a553d4b8SJeff Roberson 	}
865a553d4b8SJeff Roberson 
8663370c5bfSJeff Roberson 	/*
8673370c5bfSJeff Roberson 	 * This reproduces the old vm_zone behavior of zero filling pages the
8683370c5bfSJeff Roberson 	 * first time they are added to a zone.
8693370c5bfSJeff Roberson 	 *
8703370c5bfSJeff Roberson 	 * Malloced items are zeroed in uma_zalloc.
8713370c5bfSJeff Roberson 	 */
8723370c5bfSJeff Roberson 
873099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
8743370c5bfSJeff Roberson 		wait |= M_ZERO;
8753370c5bfSJeff Roberson 	else
8763370c5bfSJeff Roberson 		wait &= ~M_ZERO;
8773370c5bfSJeff Roberson 
878263811f7SKip Macy 	if (keg->uk_flags & UMA_ZONE_NODUMP)
879263811f7SKip Macy 		wait |= M_NODUMP;
880263811f7SKip Macy 
881e20a199fSJeff Roberson 	/* zone is passed for legacy reasons. */
882ad97af7eSGleb Smirnoff 	mem = allocf(zone, keg->uk_ppera * PAGE_SIZE, &flags, wait);
883a553d4b8SJeff Roberson 	if (mem == NULL) {
884b23f72e9SBrian Feldman 		if (keg->uk_flags & UMA_ZONE_OFFPAGE)
8850095a784SJeff Roberson 			zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
886fc03d22bSJeff Roberson 		slab = NULL;
887fc03d22bSJeff Roberson 		goto out;
888a553d4b8SJeff Roberson 	}
8898355f576SJeff Roberson 
8905c0e403bSJeff Roberson 	/* Point the slab into the allocated memory */
891099a0e58SBosko Milekic 	if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
892099a0e58SBosko Milekic 		slab = (uma_slab_t )(mem + keg->uk_pgoff);
8935c0e403bSJeff Roberson 
894e20a199fSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_VTOSLAB)
895099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ppera; i++)
89699571dc3SJeff Roberson 			vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
8978355f576SJeff Roberson 
898099a0e58SBosko Milekic 	slab->us_keg = keg;
8998355f576SJeff Roberson 	slab->us_data = mem;
900099a0e58SBosko Milekic 	slab->us_freecount = keg->uk_ipers;
9018355f576SJeff Roberson 	slab->us_flags = flags;
902ef72505eSJeff Roberson 	BIT_FILL(SLAB_SETSIZE, &slab->us_free);
903ef72505eSJeff Roberson #ifdef INVARIANTS
904ef72505eSJeff Roberson 	BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree);
905ef72505eSJeff Roberson #endif
906099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_REFCNT) {
907099a0e58SBosko Milekic 		slabref = (uma_slabrefcnt_t)slab;
908ab14a3f7SBrian Feldman 		for (i = 0; i < keg->uk_ipers; i++)
909ef72505eSJeff Roberson 			slabref->us_refcnt[i] = 0;
910099a0e58SBosko Milekic 	}
911099a0e58SBosko Milekic 
912b23f72e9SBrian Feldman 	if (keg->uk_init != NULL) {
913099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ipers; i++)
914b23f72e9SBrian Feldman 			if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
915b23f72e9SBrian Feldman 			    keg->uk_size, wait) != 0)
916b23f72e9SBrian Feldman 				break;
917b23f72e9SBrian Feldman 		if (i != keg->uk_ipers) {
918fc03d22bSJeff Roberson 			keg_free_slab(keg, slab, i);
919fc03d22bSJeff Roberson 			slab = NULL;
920fc03d22bSJeff Roberson 			goto out;
921b23f72e9SBrian Feldman 		}
922b23f72e9SBrian Feldman 	}
923fc03d22bSJeff Roberson out:
924e20a199fSJeff Roberson 	KEG_LOCK(keg);
9255c0e403bSJeff Roberson 
926fc03d22bSJeff Roberson 	if (slab != NULL) {
927099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZONE_HASH)
928099a0e58SBosko Milekic 			UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
9298355f576SJeff Roberson 
930099a0e58SBosko Milekic 		keg->uk_pages += keg->uk_ppera;
931099a0e58SBosko Milekic 		keg->uk_free += keg->uk_ipers;
932fc03d22bSJeff Roberson 	}
9338355f576SJeff Roberson 
9348355f576SJeff Roberson 	return (slab);
9358355f576SJeff Roberson }
9368355f576SJeff Roberson 
9378355f576SJeff Roberson /*
938009b6fcbSJeff Roberson  * This function is intended to be used early on in place of page_alloc() so
939009b6fcbSJeff Roberson  * that we may use the boot time page cache to satisfy allocations before
940009b6fcbSJeff Roberson  * the VM is ready.
941009b6fcbSJeff Roberson  */
942009b6fcbSJeff Roberson static void *
94385dcf349SGleb Smirnoff startup_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait)
944009b6fcbSJeff Roberson {
945099a0e58SBosko Milekic 	uma_keg_t keg;
946f353d338SAlan Cox 	uma_slab_t tmps;
947e9a069d8SJohn Baldwin 	int pages, check_pages;
948099a0e58SBosko Milekic 
949e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
950e9a069d8SJohn Baldwin 	pages = howmany(bytes, PAGE_SIZE);
951e9a069d8SJohn Baldwin 	check_pages = pages - 1;
952e9a069d8SJohn Baldwin 	KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n"));
953099a0e58SBosko Milekic 
954009b6fcbSJeff Roberson 	/*
955009b6fcbSJeff Roberson 	 * Check our small startup cache to see if it has pages remaining.
956009b6fcbSJeff Roberson 	 */
957f353d338SAlan Cox 	mtx_lock(&uma_boot_pages_mtx);
958e9a069d8SJohn Baldwin 
959e9a069d8SJohn Baldwin 	/* First check if we have enough room. */
960e9a069d8SJohn Baldwin 	tmps = LIST_FIRST(&uma_boot_pages);
961e9a069d8SJohn Baldwin 	while (tmps != NULL && check_pages-- > 0)
962e9a069d8SJohn Baldwin 		tmps = LIST_NEXT(tmps, us_link);
963e9a069d8SJohn Baldwin 	if (tmps != NULL) {
964e9a069d8SJohn Baldwin 		/*
965e9a069d8SJohn Baldwin 		 * It's ok to lose tmps references.  The last one will
966e9a069d8SJohn Baldwin 		 * have tmps->us_data pointing to the start address of
967e9a069d8SJohn Baldwin 		 * "pages" contiguous pages of memory.
968e9a069d8SJohn Baldwin 		 */
969e9a069d8SJohn Baldwin 		while (pages-- > 0) {
970e9a069d8SJohn Baldwin 			tmps = LIST_FIRST(&uma_boot_pages);
971009b6fcbSJeff Roberson 			LIST_REMOVE(tmps, us_link);
972e9a069d8SJohn Baldwin 		}
973f353d338SAlan Cox 		mtx_unlock(&uma_boot_pages_mtx);
974009b6fcbSJeff Roberson 		*pflag = tmps->us_flags;
975009b6fcbSJeff Roberson 		return (tmps->us_data);
976009b6fcbSJeff Roberson 	}
977f353d338SAlan Cox 	mtx_unlock(&uma_boot_pages_mtx);
978342f1793SAlan Cox 	if (booted < UMA_STARTUP2)
9793803b26bSDag-Erling Smørgrav 		panic("UMA: Increase vm.boot_pages");
980009b6fcbSJeff Roberson 	/*
981009b6fcbSJeff Roberson 	 * Now that we've booted reset these users to their real allocator.
982009b6fcbSJeff Roberson 	 */
983009b6fcbSJeff Roberson #ifdef UMA_MD_SMALL_ALLOC
984e9a069d8SJohn Baldwin 	keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc;
985009b6fcbSJeff Roberson #else
986099a0e58SBosko Milekic 	keg->uk_allocf = page_alloc;
987009b6fcbSJeff Roberson #endif
988099a0e58SBosko Milekic 	return keg->uk_allocf(zone, bytes, pflag, wait);
989009b6fcbSJeff Roberson }
990009b6fcbSJeff Roberson 
991009b6fcbSJeff Roberson /*
9928355f576SJeff Roberson  * Allocates a number of pages from the system
9938355f576SJeff Roberson  *
9948355f576SJeff Roberson  * Arguments:
9958355f576SJeff Roberson  *	bytes  The number of bytes requested
9968355f576SJeff Roberson  *	wait  Shall we wait?
9978355f576SJeff Roberson  *
9988355f576SJeff Roberson  * Returns:
9998355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
10008355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
10018355f576SJeff Roberson  */
10028355f576SJeff Roberson static void *
100385dcf349SGleb Smirnoff page_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait)
10048355f576SJeff Roberson {
10058355f576SJeff Roberson 	void *p;	/* Returned page */
10068355f576SJeff Roberson 
10078355f576SJeff Roberson 	*pflag = UMA_SLAB_KMEM;
10088355f576SJeff Roberson 	p = (void *) kmem_malloc(kmem_map, bytes, wait);
10098355f576SJeff Roberson 
10108355f576SJeff Roberson 	return (p);
10118355f576SJeff Roberson }
10128355f576SJeff Roberson 
10138355f576SJeff Roberson /*
10148355f576SJeff Roberson  * Allocates a number of pages from within an object
10158355f576SJeff Roberson  *
10168355f576SJeff Roberson  * Arguments:
10178355f576SJeff Roberson  *	bytes  The number of bytes requested
10188355f576SJeff Roberson  *	wait   Shall we wait?
10198355f576SJeff Roberson  *
10208355f576SJeff Roberson  * Returns:
10218355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
10228355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
10238355f576SJeff Roberson  */
10248355f576SJeff Roberson static void *
102585dcf349SGleb Smirnoff noobj_alloc(uma_zone_t zone, int bytes, uint8_t *flags, int wait)
10268355f576SJeff Roberson {
1027a4915c21SAttilio Rao 	TAILQ_HEAD(, vm_page) alloctail;
1028a4915c21SAttilio Rao 	u_long npages;
1029b245ac95SAlan Cox 	vm_offset_t retkva, zkva;
1030a4915c21SAttilio Rao 	vm_page_t p, p_next;
1031e20a199fSJeff Roberson 	uma_keg_t keg;
10328355f576SJeff Roberson 
1033a4915c21SAttilio Rao 	TAILQ_INIT(&alloctail);
1034e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
1035a4915c21SAttilio Rao 
1036a4915c21SAttilio Rao 	npages = howmany(bytes, PAGE_SIZE);
1037a4915c21SAttilio Rao 	while (npages > 0) {
1038a4915c21SAttilio Rao 		p = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT |
1039a4915c21SAttilio Rao 		    VM_ALLOC_WIRED | VM_ALLOC_NOOBJ);
1040a4915c21SAttilio Rao 		if (p != NULL) {
1041a4915c21SAttilio Rao 			/*
1042a4915c21SAttilio Rao 			 * Since the page does not belong to an object, its
1043a4915c21SAttilio Rao 			 * listq is unused.
1044a4915c21SAttilio Rao 			 */
1045a4915c21SAttilio Rao 			TAILQ_INSERT_TAIL(&alloctail, p, listq);
1046a4915c21SAttilio Rao 			npages--;
1047a4915c21SAttilio Rao 			continue;
1048a4915c21SAttilio Rao 		}
1049a4915c21SAttilio Rao 		if (wait & M_WAITOK) {
1050a4915c21SAttilio Rao 			VM_WAIT;
1051a4915c21SAttilio Rao 			continue;
1052a4915c21SAttilio Rao 		}
10538355f576SJeff Roberson 
10548355f576SJeff Roberson 		/*
1055a4915c21SAttilio Rao 		 * Page allocation failed, free intermediate pages and
1056a4915c21SAttilio Rao 		 * exit.
10578355f576SJeff Roberson 		 */
1058a4915c21SAttilio Rao 		TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1059b245ac95SAlan Cox 			vm_page_unwire(p, 0);
1060b245ac95SAlan Cox 			vm_page_free(p);
1061b245ac95SAlan Cox 		}
1062a4915c21SAttilio Rao 		return (NULL);
1063b245ac95SAlan Cox 	}
10648355f576SJeff Roberson 	*flags = UMA_SLAB_PRIV;
1065a4915c21SAttilio Rao 	zkva = keg->uk_kva +
1066a4915c21SAttilio Rao 	    atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1067a4915c21SAttilio Rao 	retkva = zkva;
1068a4915c21SAttilio Rao 	TAILQ_FOREACH(p, &alloctail, listq) {
1069a4915c21SAttilio Rao 		pmap_qenter(zkva, &p, 1);
1070a4915c21SAttilio Rao 		zkva += PAGE_SIZE;
1071a4915c21SAttilio Rao 	}
10728355f576SJeff Roberson 
10738355f576SJeff Roberson 	return ((void *)retkva);
10748355f576SJeff Roberson }
10758355f576SJeff Roberson 
10768355f576SJeff Roberson /*
10778355f576SJeff Roberson  * Frees a number of pages to the system
10788355f576SJeff Roberson  *
10798355f576SJeff Roberson  * Arguments:
10808355f576SJeff Roberson  *	mem   A pointer to the memory to be freed
10818355f576SJeff Roberson  *	size  The size of the memory being freed
10828355f576SJeff Roberson  *	flags The original p->us_flags field
10838355f576SJeff Roberson  *
10848355f576SJeff Roberson  * Returns:
10858355f576SJeff Roberson  *	Nothing
10868355f576SJeff Roberson  */
10878355f576SJeff Roberson static void
108885dcf349SGleb Smirnoff page_free(void *mem, int size, uint8_t flags)
10898355f576SJeff Roberson {
10908355f576SJeff Roberson 	vm_map_t map;
10913370c5bfSJeff Roberson 
10928355f576SJeff Roberson 	if (flags & UMA_SLAB_KMEM)
10938355f576SJeff Roberson 		map = kmem_map;
1094aea6e893SAlan Cox 	else if (flags & UMA_SLAB_KERNEL)
1095aea6e893SAlan Cox 		map = kernel_map;
10968355f576SJeff Roberson 	else
1097aea6e893SAlan Cox 		panic("UMA: page_free used with invalid flags %d", flags);
10988355f576SJeff Roberson 
10998355f576SJeff Roberson 	kmem_free(map, (vm_offset_t)mem, size);
11008355f576SJeff Roberson }
11018355f576SJeff Roberson 
11028355f576SJeff Roberson /*
11038355f576SJeff Roberson  * Zero fill initializer
11048355f576SJeff Roberson  *
11058355f576SJeff Roberson  * Arguments/Returns follow uma_init specifications
11068355f576SJeff Roberson  */
1107b23f72e9SBrian Feldman static int
1108b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags)
11098355f576SJeff Roberson {
11108355f576SJeff Roberson 	bzero(mem, size);
1111b23f72e9SBrian Feldman 	return (0);
11128355f576SJeff Roberson }
11138355f576SJeff Roberson 
11148355f576SJeff Roberson /*
1115e20a199fSJeff Roberson  * Finish creating a small uma keg.  This calculates ipers, and the keg size.
11168355f576SJeff Roberson  *
11178355f576SJeff Roberson  * Arguments
1118e20a199fSJeff Roberson  *	keg  The zone we should initialize
11198355f576SJeff Roberson  *
11208355f576SJeff Roberson  * Returns
11218355f576SJeff Roberson  *	Nothing
11228355f576SJeff Roberson  */
11238355f576SJeff Roberson static void
1124e20a199fSJeff Roberson keg_small_init(uma_keg_t keg)
11258355f576SJeff Roberson {
1126244f4554SBosko Milekic 	u_int rsize;
1127244f4554SBosko Milekic 	u_int memused;
1128244f4554SBosko Milekic 	u_int wastedspace;
1129244f4554SBosko Milekic 	u_int shsize;
11308355f576SJeff Roberson 
1131ad97af7eSGleb Smirnoff 	if (keg->uk_flags & UMA_ZONE_PCPU) {
113208a3102cSGleb Smirnoff 		KASSERT(mp_ncpus > 0, ("%s: ncpus %d\n", __func__, mp_ncpus));
1133ad97af7eSGleb Smirnoff 		keg->uk_slabsize = sizeof(struct pcpu);
1134ad97af7eSGleb Smirnoff 		keg->uk_ppera = howmany(mp_ncpus * sizeof(struct pcpu),
1135ad97af7eSGleb Smirnoff 		    PAGE_SIZE);
1136ad97af7eSGleb Smirnoff 	} else {
1137ad97af7eSGleb Smirnoff 		keg->uk_slabsize = UMA_SLAB_SIZE;
1138ad97af7eSGleb Smirnoff 		keg->uk_ppera = 1;
1139ad97af7eSGleb Smirnoff 	}
1140ad97af7eSGleb Smirnoff 
1141ef72505eSJeff Roberson 	/*
1142ef72505eSJeff Roberson 	 * Calculate the size of each allocation (rsize) according to
1143ef72505eSJeff Roberson 	 * alignment.  If the requested size is smaller than we have
1144ef72505eSJeff Roberson 	 * allocation bits for we round it up.
1145ef72505eSJeff Roberson 	 */
1146099a0e58SBosko Milekic 	rsize = keg->uk_size;
1147ef72505eSJeff Roberson 	if (rsize < keg->uk_slabsize / SLAB_SETSIZE)
1148ef72505eSJeff Roberson 		rsize = keg->uk_slabsize / SLAB_SETSIZE;
1149099a0e58SBosko Milekic 	if (rsize & keg->uk_align)
1150099a0e58SBosko Milekic 		rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1151099a0e58SBosko Milekic 	keg->uk_rsize = rsize;
1152ad97af7eSGleb Smirnoff 
1153ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
1154ad97af7eSGleb Smirnoff 	    keg->uk_rsize < sizeof(struct pcpu),
1155ad97af7eSGleb Smirnoff 	    ("%s: size %u too large", __func__, keg->uk_rsize));
11568355f576SJeff Roberson 
1157ef72505eSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_REFCNT)
1158ef72505eSJeff Roberson 		rsize += sizeof(uint32_t);
1159ef72505eSJeff Roberson 
1160ef72505eSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_OFFPAGE)
11612864dbbfSGleb Smirnoff 		shsize = 0;
1162ef72505eSJeff Roberson 	else
1163244f4554SBosko Milekic 		shsize = sizeof(struct uma_slab);
11648355f576SJeff Roberson 
1165ad97af7eSGleb Smirnoff 	keg->uk_ipers = (keg->uk_slabsize - shsize) / rsize;
1166ef72505eSJeff Roberson 	KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1167ad97af7eSGleb Smirnoff 	    ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1168ad97af7eSGleb Smirnoff 
1169244f4554SBosko Milekic 	memused = keg->uk_ipers * rsize + shsize;
1170ad97af7eSGleb Smirnoff 	wastedspace = keg->uk_slabsize - memused;
1171244f4554SBosko Milekic 
117220e8e865SBosko Milekic 	/*
1173244f4554SBosko Milekic 	 * We can't do OFFPAGE if we're internal or if we've been
117420e8e865SBosko Milekic 	 * asked to not go to the VM for buckets.  If we do this we
117520e8e865SBosko Milekic 	 * may end up going to the VM (kmem_map) for slabs which we
117620e8e865SBosko Milekic 	 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a
117720e8e865SBosko Milekic 	 * result of UMA_ZONE_VM, which clearly forbids it.
117820e8e865SBosko Milekic 	 */
1179099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1180099a0e58SBosko Milekic 	    (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
11818355f576SJeff Roberson 		return;
1182244f4554SBosko Milekic 
1183ef72505eSJeff Roberson 	/*
1184ef72505eSJeff Roberson 	 * See if using an OFFPAGE slab will limit our waste.  Only do
1185ef72505eSJeff Roberson 	 * this if it permits more items per-slab.
1186ef72505eSJeff Roberson 	 *
1187ef72505eSJeff Roberson 	 * XXX We could try growing slabsize to limit max waste as well.
1188ef72505eSJeff Roberson 	 * Historically this was not done because the VM could not
1189ef72505eSJeff Roberson 	 * efficiently handle contiguous allocations.
1190ef72505eSJeff Roberson 	 */
1191ad97af7eSGleb Smirnoff 	if ((wastedspace >= keg->uk_slabsize / UMA_MAX_WASTE) &&
1192ad97af7eSGleb Smirnoff 	    (keg->uk_ipers < (keg->uk_slabsize / keg->uk_rsize))) {
1193ad97af7eSGleb Smirnoff 		keg->uk_ipers = keg->uk_slabsize / keg->uk_rsize;
1194ef72505eSJeff Roberson 		KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1195ad97af7eSGleb Smirnoff 		    ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1196244f4554SBosko Milekic #ifdef UMA_DEBUG
1197244f4554SBosko Milekic 		printf("UMA decided we need offpage slab headers for "
1198e20a199fSJeff Roberson 		    "keg: %s, calculated wastedspace = %d, "
1199244f4554SBosko Milekic 		    "maximum wasted space allowed = %d, "
1200244f4554SBosko Milekic 		    "calculated ipers = %d, "
1201e20a199fSJeff Roberson 		    "new wasted space = %d\n", keg->uk_name, wastedspace,
1202ad97af7eSGleb Smirnoff 		    keg->uk_slabsize / UMA_MAX_WASTE, keg->uk_ipers,
1203ad97af7eSGleb Smirnoff 		    keg->uk_slabsize - keg->uk_ipers * keg->uk_rsize);
1204244f4554SBosko Milekic #endif
1205099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZONE_OFFPAGE;
12068355f576SJeff Roberson 	}
1207ad97af7eSGleb Smirnoff 
1208ad97af7eSGleb Smirnoff 	if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1209ad97af7eSGleb Smirnoff 	    (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1210ad97af7eSGleb Smirnoff 		keg->uk_flags |= UMA_ZONE_HASH;
12118355f576SJeff Roberson }
12128355f576SJeff Roberson 
12138355f576SJeff Roberson /*
1214e20a199fSJeff Roberson  * Finish creating a large (> UMA_SLAB_SIZE) uma kegs.  Just give in and do
12158355f576SJeff Roberson  * OFFPAGE for now.  When I can allow for more dynamic slab sizes this will be
12168355f576SJeff Roberson  * more complicated.
12178355f576SJeff Roberson  *
12188355f576SJeff Roberson  * Arguments
1219e20a199fSJeff Roberson  *	keg  The keg we should initialize
12208355f576SJeff Roberson  *
12218355f576SJeff Roberson  * Returns
12228355f576SJeff Roberson  *	Nothing
12238355f576SJeff Roberson  */
12248355f576SJeff Roberson static void
1225e20a199fSJeff Roberson keg_large_init(uma_keg_t keg)
12268355f576SJeff Roberson {
12278355f576SJeff Roberson 
1228e20a199fSJeff Roberson 	KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1229099a0e58SBosko Milekic 	KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1230e20a199fSJeff Roberson 	    ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1231ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1232ad97af7eSGleb Smirnoff 	    ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__));
123320e8e865SBosko Milekic 
1234ad97af7eSGleb Smirnoff 	keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE);
1235ad97af7eSGleb Smirnoff 	keg->uk_slabsize = keg->uk_ppera * PAGE_SIZE;
1236099a0e58SBosko Milekic 	keg->uk_ipers = 1;
1237e9a069d8SJohn Baldwin 	keg->uk_rsize = keg->uk_size;
1238e9a069d8SJohn Baldwin 
1239e9a069d8SJohn Baldwin 	/* We can't do OFFPAGE if we're internal, bail out here. */
1240e9a069d8SJohn Baldwin 	if (keg->uk_flags & UMA_ZFLAG_INTERNAL)
1241e9a069d8SJohn Baldwin 		return;
12428355f576SJeff Roberson 
1243099a0e58SBosko Milekic 	keg->uk_flags |= UMA_ZONE_OFFPAGE;
1244e20a199fSJeff Roberson 	if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1245099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZONE_HASH;
12468355f576SJeff Roberson }
12478355f576SJeff Roberson 
1248e20a199fSJeff Roberson static void
1249e20a199fSJeff Roberson keg_cachespread_init(uma_keg_t keg)
1250e20a199fSJeff Roberson {
1251e20a199fSJeff Roberson 	int alignsize;
1252e20a199fSJeff Roberson 	int trailer;
1253e20a199fSJeff Roberson 	int pages;
1254e20a199fSJeff Roberson 	int rsize;
1255e20a199fSJeff Roberson 
1256ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1257ad97af7eSGleb Smirnoff 	    ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__));
1258ad97af7eSGleb Smirnoff 
1259e20a199fSJeff Roberson 	alignsize = keg->uk_align + 1;
1260e20a199fSJeff Roberson 	rsize = keg->uk_size;
1261e20a199fSJeff Roberson 	/*
1262e20a199fSJeff Roberson 	 * We want one item to start on every align boundary in a page.  To
1263e20a199fSJeff Roberson 	 * do this we will span pages.  We will also extend the item by the
1264e20a199fSJeff Roberson 	 * size of align if it is an even multiple of align.  Otherwise, it
1265e20a199fSJeff Roberson 	 * would fall on the same boundary every time.
1266e20a199fSJeff Roberson 	 */
1267e20a199fSJeff Roberson 	if (rsize & keg->uk_align)
1268e20a199fSJeff Roberson 		rsize = (rsize & ~keg->uk_align) + alignsize;
1269e20a199fSJeff Roberson 	if ((rsize & alignsize) == 0)
1270e20a199fSJeff Roberson 		rsize += alignsize;
1271e20a199fSJeff Roberson 	trailer = rsize - keg->uk_size;
1272e20a199fSJeff Roberson 	pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1273e20a199fSJeff Roberson 	pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1274e20a199fSJeff Roberson 	keg->uk_rsize = rsize;
1275e20a199fSJeff Roberson 	keg->uk_ppera = pages;
1276ad97af7eSGleb Smirnoff 	keg->uk_slabsize = UMA_SLAB_SIZE;
1277e20a199fSJeff Roberson 	keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1278e20a199fSJeff Roberson 	keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1279e20a199fSJeff Roberson 	KASSERT(keg->uk_ipers <= uma_max_ipers,
128042321809SGleb Smirnoff 	    ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1281e20a199fSJeff Roberson 	    keg->uk_ipers));
1282e20a199fSJeff Roberson }
1283e20a199fSJeff Roberson 
12848355f576SJeff Roberson /*
1285099a0e58SBosko Milekic  * Keg header ctor.  This initializes all fields, locks, etc.  And inserts
1286099a0e58SBosko Milekic  * the keg onto the global keg list.
12878355f576SJeff Roberson  *
12888355f576SJeff Roberson  * Arguments/Returns follow uma_ctor specifications
1289099a0e58SBosko Milekic  *	udata  Actually uma_kctor_args
1290099a0e58SBosko Milekic  */
1291b23f72e9SBrian Feldman static int
1292b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags)
1293099a0e58SBosko Milekic {
1294099a0e58SBosko Milekic 	struct uma_kctor_args *arg = udata;
1295099a0e58SBosko Milekic 	uma_keg_t keg = mem;
1296099a0e58SBosko Milekic 	uma_zone_t zone;
1297099a0e58SBosko Milekic 
1298099a0e58SBosko Milekic 	bzero(keg, size);
1299099a0e58SBosko Milekic 	keg->uk_size = arg->size;
1300099a0e58SBosko Milekic 	keg->uk_init = arg->uminit;
1301099a0e58SBosko Milekic 	keg->uk_fini = arg->fini;
1302099a0e58SBosko Milekic 	keg->uk_align = arg->align;
1303099a0e58SBosko Milekic 	keg->uk_free = 0;
1304099a0e58SBosko Milekic 	keg->uk_pages = 0;
1305099a0e58SBosko Milekic 	keg->uk_flags = arg->flags;
1306099a0e58SBosko Milekic 	keg->uk_allocf = page_alloc;
1307099a0e58SBosko Milekic 	keg->uk_freef = page_free;
1308099a0e58SBosko Milekic 	keg->uk_slabzone = NULL;
1309099a0e58SBosko Milekic 
1310099a0e58SBosko Milekic 	/*
1311099a0e58SBosko Milekic 	 * The master zone is passed to us at keg-creation time.
1312099a0e58SBosko Milekic 	 */
1313099a0e58SBosko Milekic 	zone = arg->zone;
1314e20a199fSJeff Roberson 	keg->uk_name = zone->uz_name;
1315099a0e58SBosko Milekic 
1316099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_VM)
1317099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1318099a0e58SBosko Milekic 
1319099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_ZINIT)
1320099a0e58SBosko Milekic 		keg->uk_init = zero_init;
1321099a0e58SBosko Milekic 
1322e20a199fSJeff Roberson 	if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC)
1323e20a199fSJeff Roberson 		keg->uk_flags |= UMA_ZONE_VTOSLAB;
1324e20a199fSJeff Roberson 
1325ad97af7eSGleb Smirnoff 	if (arg->flags & UMA_ZONE_PCPU)
1326ad97af7eSGleb Smirnoff #ifdef SMP
1327ad97af7eSGleb Smirnoff 		keg->uk_flags |= UMA_ZONE_OFFPAGE;
1328ad97af7eSGleb Smirnoff #else
1329ad97af7eSGleb Smirnoff 		keg->uk_flags &= ~UMA_ZONE_PCPU;
1330ad97af7eSGleb Smirnoff #endif
1331ad97af7eSGleb Smirnoff 
1332ef72505eSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_CACHESPREAD) {
1333e20a199fSJeff Roberson 		keg_cachespread_init(keg);
1334ef72505eSJeff Roberson 	} else if (keg->uk_flags & UMA_ZONE_REFCNT) {
1335ef72505eSJeff Roberson 		if (keg->uk_size >
1336ef72505eSJeff Roberson 		    (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) -
1337ef72505eSJeff Roberson 		    sizeof(uint32_t)))
1338e20a199fSJeff Roberson 			keg_large_init(keg);
1339099a0e58SBosko Milekic 		else
1340e20a199fSJeff Roberson 			keg_small_init(keg);
1341244f4554SBosko Milekic 	} else {
1342ef72505eSJeff Roberson 		if (keg->uk_size > (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1343e20a199fSJeff Roberson 			keg_large_init(keg);
1344244f4554SBosko Milekic 		else
1345e20a199fSJeff Roberson 			keg_small_init(keg);
1346244f4554SBosko Milekic 	}
1347099a0e58SBosko Milekic 
1348244f4554SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1349ef72505eSJeff Roberson 		if (keg->uk_flags & UMA_ZONE_REFCNT) {
1350ef72505eSJeff Roberson 			if (keg->uk_ipers > uma_max_ipers_ref)
1351ef72505eSJeff Roberson 				panic("Too many ref items per zone: %d > %d\n",
1352ef72505eSJeff Roberson 				    keg->uk_ipers, uma_max_ipers_ref);
1353099a0e58SBosko Milekic 			keg->uk_slabzone = slabrefzone;
1354ef72505eSJeff Roberson 		} else
1355099a0e58SBosko Milekic 			keg->uk_slabzone = slabzone;
1356244f4554SBosko Milekic 	}
1357099a0e58SBosko Milekic 
1358099a0e58SBosko Milekic 	/*
1359099a0e58SBosko Milekic 	 * If we haven't booted yet we need allocations to go through the
1360099a0e58SBosko Milekic 	 * startup cache until the vm is ready.
1361099a0e58SBosko Milekic 	 */
1362099a0e58SBosko Milekic 	if (keg->uk_ppera == 1) {
1363099a0e58SBosko Milekic #ifdef UMA_MD_SMALL_ALLOC
1364099a0e58SBosko Milekic 		keg->uk_allocf = uma_small_alloc;
1365099a0e58SBosko Milekic 		keg->uk_freef = uma_small_free;
13668cd02d00SAlan Cox 
1367342f1793SAlan Cox 		if (booted < UMA_STARTUP)
1368099a0e58SBosko Milekic 			keg->uk_allocf = startup_alloc;
13698cd02d00SAlan Cox #else
13708cd02d00SAlan Cox 		if (booted < UMA_STARTUP2)
13718cd02d00SAlan Cox 			keg->uk_allocf = startup_alloc;
13728cd02d00SAlan Cox #endif
1373342f1793SAlan Cox 	} else if (booted < UMA_STARTUP2 &&
1374342f1793SAlan Cox 	    (keg->uk_flags & UMA_ZFLAG_INTERNAL))
1375e9a069d8SJohn Baldwin 		keg->uk_allocf = startup_alloc;
1376099a0e58SBosko Milekic 
1377099a0e58SBosko Milekic 	/*
1378*af526374SJeff Roberson 	 * Initialize keg's lock
1379099a0e58SBosko Milekic 	 */
1380*af526374SJeff Roberson 	KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS));
1381099a0e58SBosko Milekic 
1382099a0e58SBosko Milekic 	/*
1383099a0e58SBosko Milekic 	 * If we're putting the slab header in the actual page we need to
1384099a0e58SBosko Milekic 	 * figure out where in each page it goes.  This calculates a right
1385099a0e58SBosko Milekic 	 * justified offset into the memory on an ALIGN_PTR boundary.
1386099a0e58SBosko Milekic 	 */
1387099a0e58SBosko Milekic 	if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1388244f4554SBosko Milekic 		u_int totsize;
1389099a0e58SBosko Milekic 
1390099a0e58SBosko Milekic 		/* Size of the slab struct and free list */
1391ef72505eSJeff Roberson 		totsize = sizeof(struct uma_slab);
1392ef72505eSJeff Roberson 
1393ef72505eSJeff Roberson 		/* Size of the reference counts. */
1394244f4554SBosko Milekic 		if (keg->uk_flags & UMA_ZONE_REFCNT)
1395ef72505eSJeff Roberson 			totsize += keg->uk_ipers * sizeof(uint32_t);
1396244f4554SBosko Milekic 
1397099a0e58SBosko Milekic 		if (totsize & UMA_ALIGN_PTR)
1398099a0e58SBosko Milekic 			totsize = (totsize & ~UMA_ALIGN_PTR) +
1399099a0e58SBosko Milekic 			    (UMA_ALIGN_PTR + 1);
1400ad97af7eSGleb Smirnoff 		keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize;
1401244f4554SBosko Milekic 
1402244f4554SBosko Milekic 		/*
1403244f4554SBosko Milekic 		 * The only way the following is possible is if with our
1404244f4554SBosko Milekic 		 * UMA_ALIGN_PTR adjustments we are now bigger than
1405244f4554SBosko Milekic 		 * UMA_SLAB_SIZE.  I haven't checked whether this is
1406244f4554SBosko Milekic 		 * mathematically possible for all cases, so we make
1407244f4554SBosko Milekic 		 * sure here anyway.
1408244f4554SBosko Milekic 		 */
1409ef72505eSJeff Roberson 		totsize = keg->uk_pgoff + sizeof(struct uma_slab);
1410ef72505eSJeff Roberson 		if (keg->uk_flags & UMA_ZONE_REFCNT)
1411ef72505eSJeff Roberson 			totsize += keg->uk_ipers * sizeof(uint32_t);
1412ad97af7eSGleb Smirnoff 		if (totsize > PAGE_SIZE * keg->uk_ppera) {
1413099a0e58SBosko Milekic 			printf("zone %s ipers %d rsize %d size %d\n",
1414099a0e58SBosko Milekic 			    zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1415099a0e58SBosko Milekic 			    keg->uk_size);
1416aea6e893SAlan Cox 			panic("UMA slab won't fit.");
1417099a0e58SBosko Milekic 		}
1418099a0e58SBosko Milekic 	}
1419099a0e58SBosko Milekic 
1420099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_HASH)
1421099a0e58SBosko Milekic 		hash_alloc(&keg->uk_hash);
1422099a0e58SBosko Milekic 
1423099a0e58SBosko Milekic #ifdef UMA_DEBUG
14240b80c1e4SEitan Adler 	printf("UMA: %s(%p) size %d(%d) flags %#x ipers %d ppera %d out %d free %d\n",
1425e20a199fSJeff Roberson 	    zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
1426e20a199fSJeff Roberson 	    keg->uk_ipers, keg->uk_ppera,
1427e20a199fSJeff Roberson 	    (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
1428099a0e58SBosko Milekic #endif
1429099a0e58SBosko Milekic 
1430099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1431099a0e58SBosko Milekic 
1432099a0e58SBosko Milekic 	mtx_lock(&uma_mtx);
1433099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1434099a0e58SBosko Milekic 	mtx_unlock(&uma_mtx);
1435b23f72e9SBrian Feldman 	return (0);
1436099a0e58SBosko Milekic }
1437099a0e58SBosko Milekic 
1438099a0e58SBosko Milekic /*
1439099a0e58SBosko Milekic  * Zone header ctor.  This initializes all fields, locks, etc.
1440099a0e58SBosko Milekic  *
1441099a0e58SBosko Milekic  * Arguments/Returns follow uma_ctor specifications
1442099a0e58SBosko Milekic  *	udata  Actually uma_zctor_args
14438355f576SJeff Roberson  */
1444b23f72e9SBrian Feldman static int
1445b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags)
14468355f576SJeff Roberson {
14478355f576SJeff Roberson 	struct uma_zctor_args *arg = udata;
14488355f576SJeff Roberson 	uma_zone_t zone = mem;
1449099a0e58SBosko Milekic 	uma_zone_t z;
1450099a0e58SBosko Milekic 	uma_keg_t keg;
14518355f576SJeff Roberson 
14528355f576SJeff Roberson 	bzero(zone, size);
14538355f576SJeff Roberson 	zone->uz_name = arg->name;
14548355f576SJeff Roberson 	zone->uz_ctor = arg->ctor;
14558355f576SJeff Roberson 	zone->uz_dtor = arg->dtor;
1456e20a199fSJeff Roberson 	zone->uz_slab = zone_fetch_slab;
1457099a0e58SBosko Milekic 	zone->uz_init = NULL;
1458099a0e58SBosko Milekic 	zone->uz_fini = NULL;
1459099a0e58SBosko Milekic 	zone->uz_allocs = 0;
1460773df9abSRobert Watson 	zone->uz_frees = 0;
14612019094aSRobert Watson 	zone->uz_fails = 0;
1462bf965959SSean Bruno 	zone->uz_sleeps = 0;
1463fc03d22bSJeff Roberson 	zone->uz_count = 0;
1464e20a199fSJeff Roberson 	zone->uz_flags = 0;
14652f891cd5SPawel Jakub Dawidek 	zone->uz_warning = NULL;
14662f891cd5SPawel Jakub Dawidek 	timevalclear(&zone->uz_ratecheck);
1467e20a199fSJeff Roberson 	keg = arg->keg;
1468099a0e58SBosko Milekic 
1469*af526374SJeff Roberson 	ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
1470*af526374SJeff Roberson 
14710095a784SJeff Roberson 	/*
14720095a784SJeff Roberson 	 * This is a pure cache zone, no kegs.
14730095a784SJeff Roberson 	 */
14740095a784SJeff Roberson 	if (arg->import) {
1475*af526374SJeff Roberson 		zone->uz_size = arg->size;
14760095a784SJeff Roberson 		zone->uz_import = arg->import;
14770095a784SJeff Roberson 		zone->uz_release = arg->release;
14780095a784SJeff Roberson 		zone->uz_arg = arg->arg;
1479*af526374SJeff Roberson 		zone->uz_lockptr = &zone->uz_lock;
1480*af526374SJeff Roberson 		goto out;
14810095a784SJeff Roberson 	}
14820095a784SJeff Roberson 
14830095a784SJeff Roberson 	/*
14840095a784SJeff Roberson 	 * Use the regular zone/keg/slab allocator.
14850095a784SJeff Roberson 	 */
14860095a784SJeff Roberson 	zone->uz_import = (uma_import)zone_import;
14870095a784SJeff Roberson 	zone->uz_release = (uma_release)zone_release;
14880095a784SJeff Roberson 	zone->uz_arg = zone;
14890095a784SJeff Roberson 
1490099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_SECONDARY) {
1491099a0e58SBosko Milekic 		KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
14928355f576SJeff Roberson 		zone->uz_init = arg->uminit;
1493e221e841SJeff Roberson 		zone->uz_fini = arg->fini;
1494*af526374SJeff Roberson 		zone->uz_lockptr = &keg->uk_lock;
1495e20a199fSJeff Roberson 		zone->uz_flags |= UMA_ZONE_SECONDARY;
14968355f576SJeff Roberson 		mtx_lock(&uma_mtx);
1497099a0e58SBosko Milekic 		ZONE_LOCK(zone);
1498099a0e58SBosko Milekic 		LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1499099a0e58SBosko Milekic 			if (LIST_NEXT(z, uz_link) == NULL) {
1500099a0e58SBosko Milekic 				LIST_INSERT_AFTER(z, zone, uz_link);
1501099a0e58SBosko Milekic 				break;
1502099a0e58SBosko Milekic 			}
1503099a0e58SBosko Milekic 		}
1504099a0e58SBosko Milekic 		ZONE_UNLOCK(zone);
15058355f576SJeff Roberson 		mtx_unlock(&uma_mtx);
1506e20a199fSJeff Roberson 	} else if (keg == NULL) {
1507e20a199fSJeff Roberson 		if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1508e20a199fSJeff Roberson 		    arg->align, arg->flags)) == NULL)
1509b23f72e9SBrian Feldman 			return (ENOMEM);
1510099a0e58SBosko Milekic 	} else {
1511099a0e58SBosko Milekic 		struct uma_kctor_args karg;
1512b23f72e9SBrian Feldman 		int error;
1513099a0e58SBosko Milekic 
1514099a0e58SBosko Milekic 		/* We should only be here from uma_startup() */
1515099a0e58SBosko Milekic 		karg.size = arg->size;
1516099a0e58SBosko Milekic 		karg.uminit = arg->uminit;
1517099a0e58SBosko Milekic 		karg.fini = arg->fini;
1518099a0e58SBosko Milekic 		karg.align = arg->align;
1519099a0e58SBosko Milekic 		karg.flags = arg->flags;
1520099a0e58SBosko Milekic 		karg.zone = zone;
1521b23f72e9SBrian Feldman 		error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1522b23f72e9SBrian Feldman 		    flags);
1523b23f72e9SBrian Feldman 		if (error)
1524b23f72e9SBrian Feldman 			return (error);
1525099a0e58SBosko Milekic 	}
15260095a784SJeff Roberson 
1527e20a199fSJeff Roberson 	/*
1528e20a199fSJeff Roberson 	 * Link in the first keg.
1529e20a199fSJeff Roberson 	 */
1530e20a199fSJeff Roberson 	zone->uz_klink.kl_keg = keg;
1531e20a199fSJeff Roberson 	LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1532*af526374SJeff Roberson 	zone->uz_lockptr = &keg->uk_lock;
1533e20a199fSJeff Roberson 	zone->uz_size = keg->uk_size;
1534e20a199fSJeff Roberson 	zone->uz_flags |= (keg->uk_flags &
1535e20a199fSJeff Roberson 	    (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
15368355f576SJeff Roberson 
15378355f576SJeff Roberson 	/*
15388355f576SJeff Roberson 	 * Some internal zones don't have room allocated for the per cpu
15398355f576SJeff Roberson 	 * caches.  If we're internal, bail out here.
15408355f576SJeff Roberson 	 */
1541099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1542e20a199fSJeff Roberson 		KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1543099a0e58SBosko Milekic 		    ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1544b23f72e9SBrian Feldman 		return (0);
1545099a0e58SBosko Milekic 	}
15468355f576SJeff Roberson 
1547*af526374SJeff Roberson out:
1548*af526374SJeff Roberson 	if ((arg->flags & UMA_ZONE_MAXBUCKET) == 0)
1549*af526374SJeff Roberson 		zone->uz_count = bucket_select(zone->uz_size);
15508355f576SJeff Roberson 	else
1551cae33c14SJeff Roberson 		zone->uz_count = BUCKET_MAX;
1552fc03d22bSJeff Roberson 
1553b23f72e9SBrian Feldman 	return (0);
15548355f576SJeff Roberson }
15558355f576SJeff Roberson 
15568355f576SJeff Roberson /*
1557099a0e58SBosko Milekic  * Keg header dtor.  This frees all data, destroys locks, frees the hash
1558099a0e58SBosko Milekic  * table and removes the keg from the global list.
15599c2cd7e5SJeff Roberson  *
15609c2cd7e5SJeff Roberson  * Arguments/Returns follow uma_dtor specifications
15619c2cd7e5SJeff Roberson  *	udata  unused
15629c2cd7e5SJeff Roberson  */
1563099a0e58SBosko Milekic static void
1564099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata)
1565099a0e58SBosko Milekic {
1566099a0e58SBosko Milekic 	uma_keg_t keg;
15679c2cd7e5SJeff Roberson 
1568099a0e58SBosko Milekic 	keg = (uma_keg_t)arg;
1569e20a199fSJeff Roberson 	KEG_LOCK(keg);
1570099a0e58SBosko Milekic 	if (keg->uk_free != 0) {
1571099a0e58SBosko Milekic 		printf("Freed UMA keg was not empty (%d items). "
1572099a0e58SBosko Milekic 		    " Lost %d pages of memory.\n",
1573099a0e58SBosko Milekic 		    keg->uk_free, keg->uk_pages);
1574099a0e58SBosko Milekic 	}
1575e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
1576099a0e58SBosko Milekic 
1577099a0e58SBosko Milekic 	hash_free(&keg->uk_hash);
1578099a0e58SBosko Milekic 
1579e20a199fSJeff Roberson 	KEG_LOCK_FINI(keg);
1580099a0e58SBosko Milekic }
1581099a0e58SBosko Milekic 
1582099a0e58SBosko Milekic /*
1583099a0e58SBosko Milekic  * Zone header dtor.
1584099a0e58SBosko Milekic  *
1585099a0e58SBosko Milekic  * Arguments/Returns follow uma_dtor specifications
1586099a0e58SBosko Milekic  *	udata  unused
1587099a0e58SBosko Milekic  */
15889c2cd7e5SJeff Roberson static void
15899c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata)
15909c2cd7e5SJeff Roberson {
1591e20a199fSJeff Roberson 	uma_klink_t klink;
15929c2cd7e5SJeff Roberson 	uma_zone_t zone;
1593099a0e58SBosko Milekic 	uma_keg_t keg;
15949c2cd7e5SJeff Roberson 
15959c2cd7e5SJeff Roberson 	zone = (uma_zone_t)arg;
1596e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
15979643769aSJeff Roberson 
1598e20a199fSJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
15999643769aSJeff Roberson 		cache_drain(zone);
1600099a0e58SBosko Milekic 
160117b9cc49SJeff Roberson 	mtx_lock(&uma_mtx);
1602099a0e58SBosko Milekic 	LIST_REMOVE(zone, uz_link);
1603e20a199fSJeff Roberson 	mtx_unlock(&uma_mtx);
1604099a0e58SBosko Milekic 	/*
1605099a0e58SBosko Milekic 	 * XXX there are some races here where
1606099a0e58SBosko Milekic 	 * the zone can be drained but zone lock
1607099a0e58SBosko Milekic 	 * released and then refilled before we
1608099a0e58SBosko Milekic 	 * remove it... we dont care for now
1609099a0e58SBosko Milekic 	 */
1610e20a199fSJeff Roberson 	zone_drain_wait(zone, M_WAITOK);
1611e20a199fSJeff Roberson 	/*
1612e20a199fSJeff Roberson 	 * Unlink all of our kegs.
1613e20a199fSJeff Roberson 	 */
1614e20a199fSJeff Roberson 	while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1615e20a199fSJeff Roberson 		klink->kl_keg = NULL;
1616e20a199fSJeff Roberson 		LIST_REMOVE(klink, kl_link);
1617e20a199fSJeff Roberson 		if (klink == &zone->uz_klink)
1618e20a199fSJeff Roberson 			continue;
1619e20a199fSJeff Roberson 		free(klink, M_TEMP);
1620e20a199fSJeff Roberson 	}
1621e20a199fSJeff Roberson 	/*
1622e20a199fSJeff Roberson 	 * We only destroy kegs from non secondary zones.
1623e20a199fSJeff Roberson 	 */
16240095a784SJeff Roberson 	if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0)  {
1625e20a199fSJeff Roberson 		mtx_lock(&uma_mtx);
1626099a0e58SBosko Milekic 		LIST_REMOVE(keg, uk_link);
1627099a0e58SBosko Milekic 		mtx_unlock(&uma_mtx);
16280095a784SJeff Roberson 		zone_free_item(kegs, keg, NULL, SKIP_NONE);
16299c2cd7e5SJeff Roberson 	}
1630*af526374SJeff Roberson 	ZONE_LOCK_FINI(zone);
1631099a0e58SBosko Milekic }
1632099a0e58SBosko Milekic 
16339c2cd7e5SJeff Roberson /*
16348355f576SJeff Roberson  * Traverses every zone in the system and calls a callback
16358355f576SJeff Roberson  *
16368355f576SJeff Roberson  * Arguments:
16378355f576SJeff Roberson  *	zfunc  A pointer to a function which accepts a zone
16388355f576SJeff Roberson  *		as an argument.
16398355f576SJeff Roberson  *
16408355f576SJeff Roberson  * Returns:
16418355f576SJeff Roberson  *	Nothing
16428355f576SJeff Roberson  */
16438355f576SJeff Roberson static void
16448355f576SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t))
16458355f576SJeff Roberson {
1646099a0e58SBosko Milekic 	uma_keg_t keg;
16478355f576SJeff Roberson 	uma_zone_t zone;
16488355f576SJeff Roberson 
16498355f576SJeff Roberson 	mtx_lock(&uma_mtx);
1650099a0e58SBosko Milekic 	LIST_FOREACH(keg, &uma_kegs, uk_link) {
1651099a0e58SBosko Milekic 		LIST_FOREACH(zone, &keg->uk_zones, uz_link)
16528355f576SJeff Roberson 			zfunc(zone);
1653099a0e58SBosko Milekic 	}
16548355f576SJeff Roberson 	mtx_unlock(&uma_mtx);
16558355f576SJeff Roberson }
16568355f576SJeff Roberson 
16578355f576SJeff Roberson /* Public functions */
16588355f576SJeff Roberson /* See uma.h */
16598355f576SJeff Roberson void
16603803b26bSDag-Erling Smørgrav uma_startup(void *bootmem, int boot_pages)
16618355f576SJeff Roberson {
16628355f576SJeff Roberson 	struct uma_zctor_args args;
16638355f576SJeff Roberson 	uma_slab_t slab;
1664244f4554SBosko Milekic 	u_int slabsize;
16658355f576SJeff Roberson 	int i;
16668355f576SJeff Roberson 
16678355f576SJeff Roberson #ifdef UMA_DEBUG
1668099a0e58SBosko Milekic 	printf("Creating uma keg headers zone and keg.\n");
16698355f576SJeff Roberson #endif
1670f353d338SAlan Cox 	mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
1671099a0e58SBosko Milekic 
1672099a0e58SBosko Milekic 	/* "manually" create the initial zone */
16730095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
1674099a0e58SBosko Milekic 	args.name = "UMA Kegs";
1675099a0e58SBosko Milekic 	args.size = sizeof(struct uma_keg);
1676099a0e58SBosko Milekic 	args.ctor = keg_ctor;
1677099a0e58SBosko Milekic 	args.dtor = keg_dtor;
16788355f576SJeff Roberson 	args.uminit = zero_init;
16798355f576SJeff Roberson 	args.fini = NULL;
1680099a0e58SBosko Milekic 	args.keg = &masterkeg;
16818355f576SJeff Roberson 	args.align = 32 - 1;
1682b60f5b79SJeff Roberson 	args.flags = UMA_ZFLAG_INTERNAL;
16838355f576SJeff Roberson 	/* The initial zone has no Per cpu queues so it's smaller */
1684b23f72e9SBrian Feldman 	zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
16858355f576SJeff Roberson 
16868355f576SJeff Roberson #ifdef UMA_DEBUG
16878355f576SJeff Roberson 	printf("Filling boot free list.\n");
16888355f576SJeff Roberson #endif
16893803b26bSDag-Erling Smørgrav 	for (i = 0; i < boot_pages; i++) {
169085dcf349SGleb Smirnoff 		slab = (uma_slab_t)((uint8_t *)bootmem + (i * UMA_SLAB_SIZE));
169185dcf349SGleb Smirnoff 		slab->us_data = (uint8_t *)slab;
16928355f576SJeff Roberson 		slab->us_flags = UMA_SLAB_BOOT;
16938355f576SJeff Roberson 		LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
16948355f576SJeff Roberson 	}
1695f353d338SAlan Cox 	mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
16968355f576SJeff Roberson 
16978355f576SJeff Roberson #ifdef UMA_DEBUG
1698099a0e58SBosko Milekic 	printf("Creating uma zone headers zone and keg.\n");
1699099a0e58SBosko Milekic #endif
1700099a0e58SBosko Milekic 	args.name = "UMA Zones";
1701099a0e58SBosko Milekic 	args.size = sizeof(struct uma_zone) +
1702099a0e58SBosko Milekic 	    (sizeof(struct uma_cache) * (mp_maxid + 1));
1703099a0e58SBosko Milekic 	args.ctor = zone_ctor;
1704099a0e58SBosko Milekic 	args.dtor = zone_dtor;
1705099a0e58SBosko Milekic 	args.uminit = zero_init;
1706099a0e58SBosko Milekic 	args.fini = NULL;
1707099a0e58SBosko Milekic 	args.keg = NULL;
1708099a0e58SBosko Milekic 	args.align = 32 - 1;
1709099a0e58SBosko Milekic 	args.flags = UMA_ZFLAG_INTERNAL;
1710099a0e58SBosko Milekic 	/* The initial zone has no Per cpu queues so it's smaller */
1711b23f72e9SBrian Feldman 	zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1712099a0e58SBosko Milekic 
1713099a0e58SBosko Milekic #ifdef UMA_DEBUG
1714099a0e58SBosko Milekic 	printf("Initializing pcpu cache locks.\n");
1715099a0e58SBosko Milekic #endif
1716099a0e58SBosko Milekic #ifdef UMA_DEBUG
1717099a0e58SBosko Milekic 	printf("Creating slab and hash zones.\n");
17188355f576SJeff Roberson #endif
17198355f576SJeff Roberson 
17208355f576SJeff Roberson 	/* Now make a zone for slab headers */
17218355f576SJeff Roberson 	slabzone = uma_zcreate("UMA Slabs",
1722ef72505eSJeff Roberson 				sizeof(struct uma_slab),
17238355f576SJeff Roberson 				NULL, NULL, NULL, NULL,
1724b60f5b79SJeff Roberson 				UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
17258355f576SJeff Roberson 
1726099a0e58SBosko Milekic 	/*
1727099a0e58SBosko Milekic 	 * We also create a zone for the bigger slabs with reference
1728099a0e58SBosko Milekic 	 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1729099a0e58SBosko Milekic 	 */
1730ef72505eSJeff Roberson 	slabsize = sizeof(struct uma_slab_refcnt);
1731ef72505eSJeff Roberson 	slabsize += uma_max_ipers_ref * sizeof(uint32_t);
1732099a0e58SBosko Milekic 	slabrefzone = uma_zcreate("UMA RCntSlabs",
1733099a0e58SBosko Milekic 				  slabsize,
1734099a0e58SBosko Milekic 				  NULL, NULL, NULL, NULL,
1735e66468eaSBosko Milekic 				  UMA_ALIGN_PTR,
17367fd87882SBosko Milekic 				  UMA_ZFLAG_INTERNAL);
1737099a0e58SBosko Milekic 
17388355f576SJeff Roberson 	hashzone = uma_zcreate("UMA Hash",
17398355f576SJeff Roberson 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
17408355f576SJeff Roberson 	    NULL, NULL, NULL, NULL,
1741b60f5b79SJeff Roberson 	    UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
17428355f576SJeff Roberson 
1743cae33c14SJeff Roberson 	bucket_init();
17448355f576SJeff Roberson 
1745342f1793SAlan Cox 	booted = UMA_STARTUP;
17468355f576SJeff Roberson 
17478355f576SJeff Roberson #ifdef UMA_DEBUG
17488355f576SJeff Roberson 	printf("UMA startup complete.\n");
17498355f576SJeff Roberson #endif
17508355f576SJeff Roberson }
17518355f576SJeff Roberson 
17528355f576SJeff Roberson /* see uma.h */
17538355f576SJeff Roberson void
175499571dc3SJeff Roberson uma_startup2(void)
17558355f576SJeff Roberson {
1756342f1793SAlan Cox 	booted = UMA_STARTUP2;
175786bbae32SJeff Roberson 	bucket_enable();
17588355f576SJeff Roberson #ifdef UMA_DEBUG
17598355f576SJeff Roberson 	printf("UMA startup2 complete.\n");
17608355f576SJeff Roberson #endif
17618355f576SJeff Roberson }
17628355f576SJeff Roberson 
17638355f576SJeff Roberson /*
17648355f576SJeff Roberson  * Initialize our callout handle
17658355f576SJeff Roberson  *
17668355f576SJeff Roberson  */
17678355f576SJeff Roberson 
17688355f576SJeff Roberson static void
17698355f576SJeff Roberson uma_startup3(void)
17708355f576SJeff Roberson {
17718355f576SJeff Roberson #ifdef UMA_DEBUG
17728355f576SJeff Roberson 	printf("Starting callout.\n");
17738355f576SJeff Roberson #endif
1774a3c07611SRobert Watson 	callout_init(&uma_callout, CALLOUT_MPSAFE);
17759643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
17768355f576SJeff Roberson #ifdef UMA_DEBUG
17778355f576SJeff Roberson 	printf("UMA startup3 complete.\n");
17788355f576SJeff Roberson #endif
17798355f576SJeff Roberson }
17808355f576SJeff Roberson 
1781e20a199fSJeff Roberson static uma_keg_t
1782099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
178385dcf349SGleb Smirnoff 		int align, uint32_t flags)
1784099a0e58SBosko Milekic {
1785099a0e58SBosko Milekic 	struct uma_kctor_args args;
1786099a0e58SBosko Milekic 
1787099a0e58SBosko Milekic 	args.size = size;
1788099a0e58SBosko Milekic 	args.uminit = uminit;
1789099a0e58SBosko Milekic 	args.fini = fini;
17901e319f6dSRobert Watson 	args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1791099a0e58SBosko Milekic 	args.flags = flags;
1792099a0e58SBosko Milekic 	args.zone = zone;
1793e20a199fSJeff Roberson 	return (zone_alloc_item(kegs, &args, M_WAITOK));
1794099a0e58SBosko Milekic }
1795099a0e58SBosko Milekic 
17968355f576SJeff Roberson /* See uma.h */
17971e319f6dSRobert Watson void
17981e319f6dSRobert Watson uma_set_align(int align)
17991e319f6dSRobert Watson {
18001e319f6dSRobert Watson 
18011e319f6dSRobert Watson 	if (align != UMA_ALIGN_CACHE)
18021e319f6dSRobert Watson 		uma_align_cache = align;
18031e319f6dSRobert Watson }
18041e319f6dSRobert Watson 
18051e319f6dSRobert Watson /* See uma.h */
18068355f576SJeff Roberson uma_zone_t
1807bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
180885dcf349SGleb Smirnoff 		uma_init uminit, uma_fini fini, int align, uint32_t flags)
18098355f576SJeff Roberson 
18108355f576SJeff Roberson {
18118355f576SJeff Roberson 	struct uma_zctor_args args;
18128355f576SJeff Roberson 
18138355f576SJeff Roberson 	/* This stuff is essential for the zone ctor */
18140095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
18158355f576SJeff Roberson 	args.name = name;
18168355f576SJeff Roberson 	args.size = size;
18178355f576SJeff Roberson 	args.ctor = ctor;
18188355f576SJeff Roberson 	args.dtor = dtor;
18198355f576SJeff Roberson 	args.uminit = uminit;
18208355f576SJeff Roberson 	args.fini = fini;
18218355f576SJeff Roberson 	args.align = align;
18228355f576SJeff Roberson 	args.flags = flags;
1823099a0e58SBosko Milekic 	args.keg = NULL;
1824099a0e58SBosko Milekic 
1825e20a199fSJeff Roberson 	return (zone_alloc_item(zones, &args, M_WAITOK));
1826099a0e58SBosko Milekic }
1827099a0e58SBosko Milekic 
1828099a0e58SBosko Milekic /* See uma.h */
1829099a0e58SBosko Milekic uma_zone_t
1830099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1831099a0e58SBosko Milekic 		    uma_init zinit, uma_fini zfini, uma_zone_t master)
1832099a0e58SBosko Milekic {
1833099a0e58SBosko Milekic 	struct uma_zctor_args args;
1834e20a199fSJeff Roberson 	uma_keg_t keg;
1835099a0e58SBosko Milekic 
1836e20a199fSJeff Roberson 	keg = zone_first_keg(master);
18370095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
1838099a0e58SBosko Milekic 	args.name = name;
1839e20a199fSJeff Roberson 	args.size = keg->uk_size;
1840099a0e58SBosko Milekic 	args.ctor = ctor;
1841099a0e58SBosko Milekic 	args.dtor = dtor;
1842099a0e58SBosko Milekic 	args.uminit = zinit;
1843099a0e58SBosko Milekic 	args.fini = zfini;
1844e20a199fSJeff Roberson 	args.align = keg->uk_align;
1845e20a199fSJeff Roberson 	args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
1846e20a199fSJeff Roberson 	args.keg = keg;
18478355f576SJeff Roberson 
1848e20a199fSJeff Roberson 	/* XXX Attaches only one keg of potentially many. */
1849e20a199fSJeff Roberson 	return (zone_alloc_item(zones, &args, M_WAITOK));
18508355f576SJeff Roberson }
18518355f576SJeff Roberson 
18520095a784SJeff Roberson /* See uma.h */
18530095a784SJeff Roberson uma_zone_t
1854*af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
1855*af526374SJeff Roberson 		    uma_init zinit, uma_fini zfini, uma_import zimport,
1856*af526374SJeff Roberson 		    uma_release zrelease, void *arg, int flags)
18570095a784SJeff Roberson {
18580095a784SJeff Roberson 	struct uma_zctor_args args;
18590095a784SJeff Roberson 
18600095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
18610095a784SJeff Roberson 	args.name = name;
1862*af526374SJeff Roberson 	args.size = size;
18630095a784SJeff Roberson 	args.ctor = ctor;
18640095a784SJeff Roberson 	args.dtor = dtor;
18650095a784SJeff Roberson 	args.uminit = zinit;
18660095a784SJeff Roberson 	args.fini = zfini;
18670095a784SJeff Roberson 	args.import = zimport;
18680095a784SJeff Roberson 	args.release = zrelease;
18690095a784SJeff Roberson 	args.arg = arg;
18700095a784SJeff Roberson 	args.align = 0;
18710095a784SJeff Roberson 	args.flags = flags;
18720095a784SJeff Roberson 
18730095a784SJeff Roberson 	return (zone_alloc_item(zones, &args, M_WAITOK));
18740095a784SJeff Roberson }
18750095a784SJeff Roberson 
1876e20a199fSJeff Roberson static void
1877e20a199fSJeff Roberson zone_lock_pair(uma_zone_t a, uma_zone_t b)
1878e20a199fSJeff Roberson {
1879e20a199fSJeff Roberson 	if (a < b) {
1880e20a199fSJeff Roberson 		ZONE_LOCK(a);
1881*af526374SJeff Roberson 		mtx_lock_flags(b->uz_lockptr, MTX_DUPOK);
1882e20a199fSJeff Roberson 	} else {
1883e20a199fSJeff Roberson 		ZONE_LOCK(b);
1884*af526374SJeff Roberson 		mtx_lock_flags(a->uz_lockptr, MTX_DUPOK);
1885e20a199fSJeff Roberson 	}
1886e20a199fSJeff Roberson }
1887e20a199fSJeff Roberson 
1888e20a199fSJeff Roberson static void
1889e20a199fSJeff Roberson zone_unlock_pair(uma_zone_t a, uma_zone_t b)
1890e20a199fSJeff Roberson {
1891e20a199fSJeff Roberson 
1892e20a199fSJeff Roberson 	ZONE_UNLOCK(a);
1893e20a199fSJeff Roberson 	ZONE_UNLOCK(b);
1894e20a199fSJeff Roberson }
1895e20a199fSJeff Roberson 
1896e20a199fSJeff Roberson int
1897e20a199fSJeff Roberson uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
1898e20a199fSJeff Roberson {
1899e20a199fSJeff Roberson 	uma_klink_t klink;
1900e20a199fSJeff Roberson 	uma_klink_t kl;
1901e20a199fSJeff Roberson 	int error;
1902e20a199fSJeff Roberson 
1903e20a199fSJeff Roberson 	error = 0;
1904e20a199fSJeff Roberson 	klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
1905e20a199fSJeff Roberson 
1906e20a199fSJeff Roberson 	zone_lock_pair(zone, master);
1907e20a199fSJeff Roberson 	/*
1908e20a199fSJeff Roberson 	 * zone must use vtoslab() to resolve objects and must already be
1909e20a199fSJeff Roberson 	 * a secondary.
1910e20a199fSJeff Roberson 	 */
1911e20a199fSJeff Roberson 	if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
1912e20a199fSJeff Roberson 	    != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
1913e20a199fSJeff Roberson 		error = EINVAL;
1914e20a199fSJeff Roberson 		goto out;
1915e20a199fSJeff Roberson 	}
1916e20a199fSJeff Roberson 	/*
1917e20a199fSJeff Roberson 	 * The new master must also use vtoslab().
1918e20a199fSJeff Roberson 	 */
1919e20a199fSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
1920e20a199fSJeff Roberson 		error = EINVAL;
1921e20a199fSJeff Roberson 		goto out;
1922e20a199fSJeff Roberson 	}
1923e20a199fSJeff Roberson 	/*
1924e20a199fSJeff Roberson 	 * Both must either be refcnt, or not be refcnt.
1925e20a199fSJeff Roberson 	 */
1926e20a199fSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_REFCNT) !=
1927e20a199fSJeff Roberson 	    (master->uz_flags & UMA_ZONE_REFCNT)) {
1928e20a199fSJeff Roberson 		error = EINVAL;
1929e20a199fSJeff Roberson 		goto out;
1930e20a199fSJeff Roberson 	}
1931e20a199fSJeff Roberson 	/*
1932e20a199fSJeff Roberson 	 * The underlying object must be the same size.  rsize
1933e20a199fSJeff Roberson 	 * may be different.
1934e20a199fSJeff Roberson 	 */
1935e20a199fSJeff Roberson 	if (master->uz_size != zone->uz_size) {
1936e20a199fSJeff Roberson 		error = E2BIG;
1937e20a199fSJeff Roberson 		goto out;
1938e20a199fSJeff Roberson 	}
1939e20a199fSJeff Roberson 	/*
1940e20a199fSJeff Roberson 	 * Put it at the end of the list.
1941e20a199fSJeff Roberson 	 */
1942e20a199fSJeff Roberson 	klink->kl_keg = zone_first_keg(master);
1943e20a199fSJeff Roberson 	LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
1944e20a199fSJeff Roberson 		if (LIST_NEXT(kl, kl_link) == NULL) {
1945e20a199fSJeff Roberson 			LIST_INSERT_AFTER(kl, klink, kl_link);
1946e20a199fSJeff Roberson 			break;
1947e20a199fSJeff Roberson 		}
1948e20a199fSJeff Roberson 	}
1949e20a199fSJeff Roberson 	klink = NULL;
1950e20a199fSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_MULTI;
1951e20a199fSJeff Roberson 	zone->uz_slab = zone_fetch_slab_multi;
1952e20a199fSJeff Roberson 
1953e20a199fSJeff Roberson out:
1954e20a199fSJeff Roberson 	zone_unlock_pair(zone, master);
1955e20a199fSJeff Roberson 	if (klink != NULL)
1956e20a199fSJeff Roberson 		free(klink, M_TEMP);
1957e20a199fSJeff Roberson 
1958e20a199fSJeff Roberson 	return (error);
1959e20a199fSJeff Roberson }
1960e20a199fSJeff Roberson 
1961e20a199fSJeff Roberson 
19628355f576SJeff Roberson /* See uma.h */
19639c2cd7e5SJeff Roberson void
19649c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone)
19659c2cd7e5SJeff Roberson {
1966f4ff923bSRobert Watson 
19670095a784SJeff Roberson 	zone_free_item(zones, zone, NULL, SKIP_NONE);
19689c2cd7e5SJeff Roberson }
19699c2cd7e5SJeff Roberson 
19709c2cd7e5SJeff Roberson /* See uma.h */
19718355f576SJeff Roberson void *
19722cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
19738355f576SJeff Roberson {
19748355f576SJeff Roberson 	void *item;
19758355f576SJeff Roberson 	uma_cache_t cache;
19768355f576SJeff Roberson 	uma_bucket_t bucket;
1977fc03d22bSJeff Roberson 	int lockfail;
19788355f576SJeff Roberson 	int cpu;
19798355f576SJeff Roberson 
19808355f576SJeff Roberson 	/* This is the fast path allocation */
19818355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1
19828355f576SJeff Roberson 	printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
19838355f576SJeff Roberson #endif
19843659f747SRobert Watson 	CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
19853659f747SRobert Watson 	    zone->uz_name, flags);
1986a553d4b8SJeff Roberson 
1987635fd505SRobert Watson 	if (flags & M_WAITOK) {
1988b23f72e9SBrian Feldman 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1989635fd505SRobert Watson 		    "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
19904c1cc01cSJohn Baldwin 	}
19918d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
19928d689e04SGleb Smirnoff 	if (memguard_cmp_zone(zone)) {
19938d689e04SGleb Smirnoff 		item = memguard_alloc(zone->uz_size, flags);
19948d689e04SGleb Smirnoff 		if (item != NULL) {
19958d689e04SGleb Smirnoff 			/*
19968d689e04SGleb Smirnoff 			 * Avoid conflict with the use-after-free
19978d689e04SGleb Smirnoff 			 * protecting infrastructure from INVARIANTS.
19988d689e04SGleb Smirnoff 			 */
19998d689e04SGleb Smirnoff 			if (zone->uz_init != NULL &&
20008d689e04SGleb Smirnoff 			    zone->uz_init != mtrash_init &&
20018d689e04SGleb Smirnoff 			    zone->uz_init(item, zone->uz_size, flags) != 0)
20028d689e04SGleb Smirnoff 				return (NULL);
20038d689e04SGleb Smirnoff 			if (zone->uz_ctor != NULL &&
20048d689e04SGleb Smirnoff 			    zone->uz_ctor != mtrash_ctor &&
2005fc03d22bSJeff Roberson 			    zone->uz_ctor(item, zone->uz_size, udata,
2006fc03d22bSJeff Roberson 			    flags) != 0) {
20078d689e04SGleb Smirnoff 			    	zone->uz_fini(item, zone->uz_size);
20088d689e04SGleb Smirnoff 				return (NULL);
20098d689e04SGleb Smirnoff 			}
20108d689e04SGleb Smirnoff 			return (item);
20118d689e04SGleb Smirnoff 		}
20128d689e04SGleb Smirnoff 		/* This is unfortunate but should not be fatal. */
20138d689e04SGleb Smirnoff 	}
20148d689e04SGleb Smirnoff #endif
20155d1ae027SRobert Watson 	/*
20165d1ae027SRobert Watson 	 * If possible, allocate from the per-CPU cache.  There are two
20175d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
20185d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
20195d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
20205d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
20215d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
20225d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to allocate from
20235d1ae027SRobert Watson 	 * the current cache; when we re-acquire the critical section, we
20245d1ae027SRobert Watson 	 * must detect and handle migration if it has occurred.
20255d1ae027SRobert Watson 	 */
20265d1ae027SRobert Watson 	critical_enter();
20275d1ae027SRobert Watson 	cpu = curcpu;
20288355f576SJeff Roberson 	cache = &zone->uz_cpu[cpu];
20298355f576SJeff Roberson 
20308355f576SJeff Roberson zalloc_start:
20318355f576SJeff Roberson 	bucket = cache->uc_allocbucket;
2032fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt > 0) {
2033cae33c14SJeff Roberson 		bucket->ub_cnt--;
2034cae33c14SJeff Roberson 		item = bucket->ub_bucket[bucket->ub_cnt];
20358355f576SJeff Roberson #ifdef INVARIANTS
2036cae33c14SJeff Roberson 		bucket->ub_bucket[bucket->ub_cnt] = NULL;
20378355f576SJeff Roberson #endif
2038fc03d22bSJeff Roberson 		KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
20398355f576SJeff Roberson 		cache->uc_allocs++;
20405d1ae027SRobert Watson 		critical_exit();
2041fc03d22bSJeff Roberson 		if (zone->uz_ctor != NULL &&
2042fc03d22bSJeff Roberson 		    zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
20430095a784SJeff Roberson 			atomic_add_long(&zone->uz_fails, 1);
2044fc03d22bSJeff Roberson 			zone_free_item(zone, item, udata, SKIP_DTOR);
2045b23f72e9SBrian Feldman 			return (NULL);
2046b23f72e9SBrian Feldman 		}
2047ef72505eSJeff Roberson #ifdef INVARIANTS
2048ef72505eSJeff Roberson 		uma_dbg_alloc(zone, NULL, item);
2049ef72505eSJeff Roberson #endif
20502cc35ff9SJeff Roberson 		if (flags & M_ZERO)
2051e20a199fSJeff Roberson 			bzero(item, zone->uz_size);
20528355f576SJeff Roberson 		return (item);
2053fc03d22bSJeff Roberson 	}
2054fc03d22bSJeff Roberson 
20558355f576SJeff Roberson 	/*
20568355f576SJeff Roberson 	 * We have run out of items in our alloc bucket.
20578355f576SJeff Roberson 	 * See if we can switch with our free bucket.
20588355f576SJeff Roberson 	 */
2059b983089aSJeff Roberson 	bucket = cache->uc_freebucket;
2060fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt > 0) {
2061fc03d22bSJeff Roberson #ifdef UMA_DEBUG_ALLOC
2062fc03d22bSJeff Roberson 		printf("uma_zalloc: Swapping empty with alloc.\n");
2063fc03d22bSJeff Roberson #endif
20648355f576SJeff Roberson 		cache->uc_freebucket = cache->uc_allocbucket;
2065b983089aSJeff Roberson 		cache->uc_allocbucket = bucket;
20668355f576SJeff Roberson 		goto zalloc_start;
20678355f576SJeff Roberson 	}
2068fc03d22bSJeff Roberson 
2069fc03d22bSJeff Roberson 	/*
2070fc03d22bSJeff Roberson 	 * Discard any empty allocation bucket while we hold no locks.
2071fc03d22bSJeff Roberson 	 */
2072fc03d22bSJeff Roberson 	bucket = cache->uc_allocbucket;
2073fc03d22bSJeff Roberson 	cache->uc_allocbucket = NULL;
2074fc03d22bSJeff Roberson 	critical_exit();
2075fc03d22bSJeff Roberson 	if (bucket != NULL)
2076*af526374SJeff Roberson 		bucket_free(zone, bucket);
2077fc03d22bSJeff Roberson 
2078fc03d22bSJeff Roberson 	/* Short-circuit for zones without buckets and low memory. */
2079fc03d22bSJeff Roberson 	if (zone->uz_count == 0 || bucketdisable)
2080fc03d22bSJeff Roberson 		goto zalloc_item;
2081fc03d22bSJeff Roberson 
20825d1ae027SRobert Watson 	/*
20835d1ae027SRobert Watson 	 * Attempt to retrieve the item from the per-CPU cache has failed, so
20845d1ae027SRobert Watson 	 * we must go back to the zone.  This requires the zone lock, so we
20855d1ae027SRobert Watson 	 * must drop the critical section, then re-acquire it when we go back
20865d1ae027SRobert Watson 	 * to the cache.  Since the critical section is released, we may be
20875d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
20885d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
20895d1ae027SRobert Watson 	 * the critical section.
20905d1ae027SRobert Watson 	 */
2091fc03d22bSJeff Roberson 	lockfail = 0;
2092fc03d22bSJeff Roberson 	if (ZONE_TRYLOCK(zone) == 0) {
2093fc03d22bSJeff Roberson 		/* Record contention to size the buckets. */
2094a553d4b8SJeff Roberson 		ZONE_LOCK(zone);
2095fc03d22bSJeff Roberson 		lockfail = 1;
2096fc03d22bSJeff Roberson 	}
20975d1ae027SRobert Watson 	critical_enter();
20985d1ae027SRobert Watson 	cpu = curcpu;
20995d1ae027SRobert Watson 	cache = &zone->uz_cpu[cpu];
21005d1ae027SRobert Watson 
2101fc03d22bSJeff Roberson 	/*
2102fc03d22bSJeff Roberson 	 * Since we have locked the zone we may as well send back our stats.
2103fc03d22bSJeff Roberson 	 */
21040095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
21050095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, cache->uc_frees);
2106a553d4b8SJeff Roberson 	cache->uc_allocs = 0;
2107773df9abSRobert Watson 	cache->uc_frees = 0;
21088355f576SJeff Roberson 
2109fc03d22bSJeff Roberson 	/* See if we lost the race to fill the cache. */
2110fc03d22bSJeff Roberson 	if (cache->uc_allocbucket != NULL) {
2111fc03d22bSJeff Roberson 		ZONE_UNLOCK(zone);
2112fc03d22bSJeff Roberson 		goto zalloc_start;
2113a553d4b8SJeff Roberson 	}
21148355f576SJeff Roberson 
2115fc03d22bSJeff Roberson 	/*
2116fc03d22bSJeff Roberson 	 * Check the zone's cache of buckets.
2117fc03d22bSJeff Roberson 	 */
2118fc03d22bSJeff Roberson 	if ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
2119cae33c14SJeff Roberson 		KASSERT(bucket->ub_cnt != 0,
2120a553d4b8SJeff Roberson 		    ("uma_zalloc_arg: Returning an empty bucket."));
21218355f576SJeff Roberson 
2122a553d4b8SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
2123a553d4b8SJeff Roberson 		cache->uc_allocbucket = bucket;
2124a553d4b8SJeff Roberson 		ZONE_UNLOCK(zone);
21258355f576SJeff Roberson 		goto zalloc_start;
2126a553d4b8SJeff Roberson 	}
21275d1ae027SRobert Watson 	/* We are no longer associated with this CPU. */
21285d1ae027SRobert Watson 	critical_exit();
2129bbee39c6SJeff Roberson 
2130fc03d22bSJeff Roberson 	/*
2131fc03d22bSJeff Roberson 	 * We bump the uz count when the cache size is insufficient to
2132fc03d22bSJeff Roberson 	 * handle the working set.
2133fc03d22bSJeff Roberson 	 */
2134fc03d22bSJeff Roberson 	if (lockfail && zone->uz_count < BUCKET_MAX && zone->uz_count != 0 &&
2135fc03d22bSJeff Roberson 	    zone != largebucket)
2136a553d4b8SJeff Roberson 		zone->uz_count++;
2137fc03d22bSJeff Roberson 	ZONE_UNLOCK(zone);
2138099a0e58SBosko Milekic 
21398355f576SJeff Roberson 	/*
2140a553d4b8SJeff Roberson 	 * Now lets just fill a bucket and put it on the free list.  If that
2141fc03d22bSJeff Roberson 	 * works we'll restart the allocation from the begining and it
2142fc03d22bSJeff Roberson 	 * will use the just filled bucket.
2143bbee39c6SJeff Roberson 	 */
2144fc03d22bSJeff Roberson 	bucket = zone_alloc_bucket(zone, flags);
2145fc03d22bSJeff Roberson 	if (bucket != NULL) {
2146fc03d22bSJeff Roberson 		ZONE_LOCK(zone);
2147fc03d22bSJeff Roberson 		critical_enter();
2148fc03d22bSJeff Roberson 		cpu = curcpu;
2149fc03d22bSJeff Roberson 		cache = &zone->uz_cpu[cpu];
2150fc03d22bSJeff Roberson 		/*
2151fc03d22bSJeff Roberson 		 * See if we lost the race or were migrated.  Cache the
2152fc03d22bSJeff Roberson 		 * initialized bucket to make this less likely or claim
2153fc03d22bSJeff Roberson 		 * the memory directly.
2154fc03d22bSJeff Roberson 		 */
2155fc03d22bSJeff Roberson 		if (cache->uc_allocbucket == NULL)
2156fc03d22bSJeff Roberson 			cache->uc_allocbucket = bucket;
2157fc03d22bSJeff Roberson 		else
2158fc03d22bSJeff Roberson 			LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
2159bbee39c6SJeff Roberson 		ZONE_UNLOCK(zone);
2160fc03d22bSJeff Roberson 		goto zalloc_start;
2161bbee39c6SJeff Roberson 	}
2162fc03d22bSJeff Roberson 
2163bbee39c6SJeff Roberson 	/*
2164bbee39c6SJeff Roberson 	 * We may not be able to get a bucket so return an actual item.
2165bbee39c6SJeff Roberson 	 */
2166bbee39c6SJeff Roberson #ifdef UMA_DEBUG
2167bbee39c6SJeff Roberson 	printf("uma_zalloc_arg: Bucketzone returned NULL\n");
2168bbee39c6SJeff Roberson #endif
2169bbee39c6SJeff Roberson 
2170fc03d22bSJeff Roberson zalloc_item:
2171e20a199fSJeff Roberson 	item = zone_alloc_item(zone, udata, flags);
2172fc03d22bSJeff Roberson 
2173e20a199fSJeff Roberson 	return (item);
2174bbee39c6SJeff Roberson }
2175bbee39c6SJeff Roberson 
2176bbee39c6SJeff Roberson static uma_slab_t
2177e20a199fSJeff Roberson keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags)
2178bbee39c6SJeff Roberson {
2179bbee39c6SJeff Roberson 	uma_slab_t slab;
2180099a0e58SBosko Milekic 
2181e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2182bbee39c6SJeff Roberson 	slab = NULL;
2183bbee39c6SJeff Roberson 
2184bbee39c6SJeff Roberson 	for (;;) {
2185bbee39c6SJeff Roberson 		/*
2186bbee39c6SJeff Roberson 		 * Find a slab with some space.  Prefer slabs that are partially
2187bbee39c6SJeff Roberson 		 * used over those that are totally full.  This helps to reduce
2188bbee39c6SJeff Roberson 		 * fragmentation.
2189bbee39c6SJeff Roberson 		 */
2190099a0e58SBosko Milekic 		if (keg->uk_free != 0) {
2191099a0e58SBosko Milekic 			if (!LIST_EMPTY(&keg->uk_part_slab)) {
2192099a0e58SBosko Milekic 				slab = LIST_FIRST(&keg->uk_part_slab);
2193bbee39c6SJeff Roberson 			} else {
2194099a0e58SBosko Milekic 				slab = LIST_FIRST(&keg->uk_free_slab);
2195bbee39c6SJeff Roberson 				LIST_REMOVE(slab, us_link);
2196099a0e58SBosko Milekic 				LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2197bbee39c6SJeff Roberson 				    us_link);
2198bbee39c6SJeff Roberson 			}
2199e20a199fSJeff Roberson 			MPASS(slab->us_keg == keg);
2200bbee39c6SJeff Roberson 			return (slab);
2201bbee39c6SJeff Roberson 		}
2202bbee39c6SJeff Roberson 
2203bbee39c6SJeff Roberson 		/*
2204bbee39c6SJeff Roberson 		 * M_NOVM means don't ask at all!
2205bbee39c6SJeff Roberson 		 */
2206bbee39c6SJeff Roberson 		if (flags & M_NOVM)
2207bbee39c6SJeff Roberson 			break;
2208bbee39c6SJeff Roberson 
2209e20a199fSJeff Roberson 		if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2210099a0e58SBosko Milekic 			keg->uk_flags |= UMA_ZFLAG_FULL;
2211e20a199fSJeff Roberson 			/*
2212e20a199fSJeff Roberson 			 * If this is not a multi-zone, set the FULL bit.
2213e20a199fSJeff Roberson 			 * Otherwise slab_multi() takes care of it.
2214e20a199fSJeff Roberson 			 */
22152f891cd5SPawel Jakub Dawidek 			if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) {
2216e20a199fSJeff Roberson 				zone->uz_flags |= UMA_ZFLAG_FULL;
22172f891cd5SPawel Jakub Dawidek 				zone_log_warning(zone);
22182f891cd5SPawel Jakub Dawidek 			}
2219ebc85edfSJeff Roberson 			if (flags & M_NOWAIT)
2220bbee39c6SJeff Roberson 				break;
2221c288b548SEitan Adler 			zone->uz_sleeps++;
2222e20a199fSJeff Roberson 			msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2223bbee39c6SJeff Roberson 			continue;
2224bbee39c6SJeff Roberson 		}
2225e20a199fSJeff Roberson 		slab = keg_alloc_slab(keg, zone, flags);
2226bbee39c6SJeff Roberson 		/*
2227bbee39c6SJeff Roberson 		 * If we got a slab here it's safe to mark it partially used
2228bbee39c6SJeff Roberson 		 * and return.  We assume that the caller is going to remove
2229bbee39c6SJeff Roberson 		 * at least one item.
2230bbee39c6SJeff Roberson 		 */
2231bbee39c6SJeff Roberson 		if (slab) {
2232e20a199fSJeff Roberson 			MPASS(slab->us_keg == keg);
2233099a0e58SBosko Milekic 			LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2234bbee39c6SJeff Roberson 			return (slab);
2235bbee39c6SJeff Roberson 		}
2236bbee39c6SJeff Roberson 		/*
2237bbee39c6SJeff Roberson 		 * We might not have been able to get a slab but another cpu
2238bbee39c6SJeff Roberson 		 * could have while we were unlocked.  Check again before we
2239bbee39c6SJeff Roberson 		 * fail.
2240bbee39c6SJeff Roberson 		 */
2241bbee39c6SJeff Roberson 		flags |= M_NOVM;
2242bbee39c6SJeff Roberson 	}
2243bbee39c6SJeff Roberson 	return (slab);
2244bbee39c6SJeff Roberson }
2245bbee39c6SJeff Roberson 
2246e20a199fSJeff Roberson static uma_slab_t
2247e20a199fSJeff Roberson zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags)
2248e20a199fSJeff Roberson {
2249e20a199fSJeff Roberson 	uma_slab_t slab;
2250e20a199fSJeff Roberson 
2251*af526374SJeff Roberson 	if (keg == NULL) {
2252e20a199fSJeff Roberson 		keg = zone_first_keg(zone);
2253*af526374SJeff Roberson 		KEG_LOCK(keg);
2254*af526374SJeff Roberson 	}
2255e20a199fSJeff Roberson 
2256e20a199fSJeff Roberson 	for (;;) {
2257e20a199fSJeff Roberson 		slab = keg_fetch_slab(keg, zone, flags);
2258e20a199fSJeff Roberson 		if (slab)
2259e20a199fSJeff Roberson 			return (slab);
2260e20a199fSJeff Roberson 		if (flags & (M_NOWAIT | M_NOVM))
2261e20a199fSJeff Roberson 			break;
2262e20a199fSJeff Roberson 	}
2263*af526374SJeff Roberson 	KEG_UNLOCK(keg);
2264e20a199fSJeff Roberson 	return (NULL);
2265e20a199fSJeff Roberson }
2266e20a199fSJeff Roberson 
2267e20a199fSJeff Roberson /*
2268e20a199fSJeff Roberson  * uma_zone_fetch_slab_multi:  Fetches a slab from one available keg.  Returns
2269*af526374SJeff Roberson  * with the keg locked.  On NULL no lock is held.
2270e20a199fSJeff Roberson  *
2271e20a199fSJeff Roberson  * The last pointer is used to seed the search.  It is not required.
2272e20a199fSJeff Roberson  */
2273e20a199fSJeff Roberson static uma_slab_t
2274e20a199fSJeff Roberson zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags)
2275e20a199fSJeff Roberson {
2276e20a199fSJeff Roberson 	uma_klink_t klink;
2277e20a199fSJeff Roberson 	uma_slab_t slab;
2278e20a199fSJeff Roberson 	uma_keg_t keg;
2279e20a199fSJeff Roberson 	int flags;
2280e20a199fSJeff Roberson 	int empty;
2281e20a199fSJeff Roberson 	int full;
2282e20a199fSJeff Roberson 
2283e20a199fSJeff Roberson 	/*
2284e20a199fSJeff Roberson 	 * Don't wait on the first pass.  This will skip limit tests
2285e20a199fSJeff Roberson 	 * as well.  We don't want to block if we can find a provider
2286e20a199fSJeff Roberson 	 * without blocking.
2287e20a199fSJeff Roberson 	 */
2288e20a199fSJeff Roberson 	flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2289e20a199fSJeff Roberson 	/*
2290e20a199fSJeff Roberson 	 * Use the last slab allocated as a hint for where to start
2291e20a199fSJeff Roberson 	 * the search.
2292e20a199fSJeff Roberson 	 */
2293*af526374SJeff Roberson 	if (last != NULL) {
2294e20a199fSJeff Roberson 		slab = keg_fetch_slab(last, zone, flags);
2295e20a199fSJeff Roberson 		if (slab)
2296e20a199fSJeff Roberson 			return (slab);
2297*af526374SJeff Roberson 		KEG_UNLOCK(last);
2298e20a199fSJeff Roberson 	}
2299e20a199fSJeff Roberson 	/*
2300e20a199fSJeff Roberson 	 * Loop until we have a slab incase of transient failures
2301e20a199fSJeff Roberson 	 * while M_WAITOK is specified.  I'm not sure this is 100%
2302e20a199fSJeff Roberson 	 * required but we've done it for so long now.
2303e20a199fSJeff Roberson 	 */
2304e20a199fSJeff Roberson 	for (;;) {
2305e20a199fSJeff Roberson 		empty = 0;
2306e20a199fSJeff Roberson 		full = 0;
2307e20a199fSJeff Roberson 		/*
2308e20a199fSJeff Roberson 		 * Search the available kegs for slabs.  Be careful to hold the
2309e20a199fSJeff Roberson 		 * correct lock while calling into the keg layer.
2310e20a199fSJeff Roberson 		 */
2311e20a199fSJeff Roberson 		LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2312e20a199fSJeff Roberson 			keg = klink->kl_keg;
2313*af526374SJeff Roberson 			KEG_LOCK(keg);
2314e20a199fSJeff Roberson 			if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2315e20a199fSJeff Roberson 				slab = keg_fetch_slab(keg, zone, flags);
2316e20a199fSJeff Roberson 				if (slab)
2317e20a199fSJeff Roberson 					return (slab);
2318e20a199fSJeff Roberson 			}
2319e20a199fSJeff Roberson 			if (keg->uk_flags & UMA_ZFLAG_FULL)
2320e20a199fSJeff Roberson 				full++;
2321e20a199fSJeff Roberson 			else
2322e20a199fSJeff Roberson 				empty++;
2323*af526374SJeff Roberson 			KEG_UNLOCK(keg);
2324e20a199fSJeff Roberson 		}
2325e20a199fSJeff Roberson 		if (rflags & (M_NOWAIT | M_NOVM))
2326e20a199fSJeff Roberson 			break;
2327e20a199fSJeff Roberson 		flags = rflags;
2328e20a199fSJeff Roberson 		/*
2329e20a199fSJeff Roberson 		 * All kegs are full.  XXX We can't atomically check all kegs
2330e20a199fSJeff Roberson 		 * and sleep so just sleep for a short period and retry.
2331e20a199fSJeff Roberson 		 */
2332e20a199fSJeff Roberson 		if (full && !empty) {
2333*af526374SJeff Roberson 			ZONE_LOCK(zone);
2334e20a199fSJeff Roberson 			zone->uz_flags |= UMA_ZFLAG_FULL;
2335bf965959SSean Bruno 			zone->uz_sleeps++;
23362f891cd5SPawel Jakub Dawidek 			zone_log_warning(zone);
2337*af526374SJeff Roberson 			msleep(zone, zone->uz_lockptr, PVM,
2338*af526374SJeff Roberson 			    "zonelimit", hz/100);
2339e20a199fSJeff Roberson 			zone->uz_flags &= ~UMA_ZFLAG_FULL;
2340*af526374SJeff Roberson 			ZONE_UNLOCK(zone);
2341e20a199fSJeff Roberson 			continue;
2342e20a199fSJeff Roberson 		}
2343e20a199fSJeff Roberson 	}
2344e20a199fSJeff Roberson 	return (NULL);
2345e20a199fSJeff Roberson }
2346e20a199fSJeff Roberson 
2347d56368d7SBosko Milekic static void *
23480095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
2349bbee39c6SJeff Roberson {
2350bbee39c6SJeff Roberson 	void *item;
235185dcf349SGleb Smirnoff 	uint8_t freei;
2352bbee39c6SJeff Roberson 
23530095a784SJeff Roberson 	MPASS(keg == slab->us_keg);
2354e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2355099a0e58SBosko Milekic 
2356ef72505eSJeff Roberson 	freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2357ef72505eSJeff Roberson 	BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2358099a0e58SBosko Milekic 	item = slab->us_data + (keg->uk_rsize * freei);
2359bbee39c6SJeff Roberson 	slab->us_freecount--;
2360099a0e58SBosko Milekic 	keg->uk_free--;
2361ef72505eSJeff Roberson 
2362bbee39c6SJeff Roberson 	/* Move this slab to the full list */
2363bbee39c6SJeff Roberson 	if (slab->us_freecount == 0) {
2364bbee39c6SJeff Roberson 		LIST_REMOVE(slab, us_link);
2365099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2366bbee39c6SJeff Roberson 	}
2367bbee39c6SJeff Roberson 
2368bbee39c6SJeff Roberson 	return (item);
2369bbee39c6SJeff Roberson }
2370bbee39c6SJeff Roberson 
2371bbee39c6SJeff Roberson static int
23720095a784SJeff Roberson zone_import(uma_zone_t zone, void **bucket, int max, int flags)
23730095a784SJeff Roberson {
23740095a784SJeff Roberson 	uma_slab_t slab;
23750095a784SJeff Roberson 	uma_keg_t keg;
23760095a784SJeff Roberson 	int i;
23770095a784SJeff Roberson 
23780095a784SJeff Roberson 	slab = NULL;
23790095a784SJeff Roberson 	keg = NULL;
2380*af526374SJeff Roberson 	/* Try to keep the buckets totally full */
23810095a784SJeff Roberson 	for (i = 0; i < max; ) {
23820095a784SJeff Roberson 		if ((slab = zone->uz_slab(zone, keg, flags)) == NULL)
23830095a784SJeff Roberson 			break;
23840095a784SJeff Roberson 		keg = slab->us_keg;
23850095a784SJeff Roberson 		while (slab->us_freecount && i < max)
23860095a784SJeff Roberson 			bucket[i++] = slab_alloc_item(keg, slab);
23870095a784SJeff Roberson 
23880095a784SJeff Roberson 		/* Don't block on the next fill */
23890095a784SJeff Roberson 		flags &= ~M_WAITOK;
23900095a784SJeff Roberson 		flags |= M_NOWAIT;
23910095a784SJeff Roberson 	}
23920095a784SJeff Roberson 	if (slab != NULL)
23930095a784SJeff Roberson 		KEG_UNLOCK(keg);
23940095a784SJeff Roberson 
23950095a784SJeff Roberson 	return i;
23960095a784SJeff Roberson }
23970095a784SJeff Roberson 
2398fc03d22bSJeff Roberson static uma_bucket_t
2399e20a199fSJeff Roberson zone_alloc_bucket(uma_zone_t zone, int flags)
2400bbee39c6SJeff Roberson {
2401bbee39c6SJeff Roberson 	uma_bucket_t bucket;
24020095a784SJeff Roberson 	int max;
2403bbee39c6SJeff Roberson 
2404*af526374SJeff Roberson 	bucket = bucket_alloc(zone, M_NOWAIT | (flags & M_NOVM));
24050095a784SJeff Roberson 	if (bucket == NULL)
24060095a784SJeff Roberson 		goto out;
24070095a784SJeff Roberson 
2408*af526374SJeff Roberson 	max = MIN(bucket->ub_entries, zone->uz_count);
24090095a784SJeff Roberson 	bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
24100095a784SJeff Roberson 	    max, flags);
24110095a784SJeff Roberson 
24120095a784SJeff Roberson 	/*
24130095a784SJeff Roberson 	 * Initialize the memory if necessary.
24140095a784SJeff Roberson 	 */
24150095a784SJeff Roberson 	if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
2416099a0e58SBosko Milekic 		int i;
2417bbee39c6SJeff Roberson 
24180095a784SJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
2419e20a199fSJeff Roberson 			if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
24200095a784SJeff Roberson 			    flags) != 0)
2421b23f72e9SBrian Feldman 				break;
2422b23f72e9SBrian Feldman 		/*
2423b23f72e9SBrian Feldman 		 * If we couldn't initialize the whole bucket, put the
2424b23f72e9SBrian Feldman 		 * rest back onto the freelist.
2425b23f72e9SBrian Feldman 		 */
2426b23f72e9SBrian Feldman 		if (i != bucket->ub_cnt) {
2427*af526374SJeff Roberson 			zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
24280095a784SJeff Roberson 			    bucket->ub_cnt - i);
2429a5a262c6SBosko Milekic #ifdef INVARIANTS
24300095a784SJeff Roberson 			bzero(&bucket->ub_bucket[i],
24310095a784SJeff Roberson 			    sizeof(void *) * (bucket->ub_cnt - i));
2432a5a262c6SBosko Milekic #endif
2433b23f72e9SBrian Feldman 			bucket->ub_cnt = i;
2434b23f72e9SBrian Feldman 		}
2435099a0e58SBosko Milekic 	}
2436099a0e58SBosko Milekic 
24370095a784SJeff Roberson out:
2438fc03d22bSJeff Roberson 	if (bucket == NULL || bucket->ub_cnt == 0) {
24390095a784SJeff Roberson 		if (bucket != NULL)
2440*af526374SJeff Roberson 			bucket_free(zone, bucket);
2441fc03d22bSJeff Roberson 		atomic_add_long(&zone->uz_fails, 1);
2442fc03d22bSJeff Roberson 		return (NULL);
2443bbee39c6SJeff Roberson 	}
2444fc03d22bSJeff Roberson 
2445fc03d22bSJeff Roberson 	return (bucket);
2446fc03d22bSJeff Roberson }
2447fc03d22bSJeff Roberson 
24488355f576SJeff Roberson /*
24490095a784SJeff Roberson  * Allocates a single item from a zone.
24508355f576SJeff Roberson  *
24518355f576SJeff Roberson  * Arguments
24528355f576SJeff Roberson  *	zone   The zone to alloc for.
24538355f576SJeff Roberson  *	udata  The data to be passed to the constructor.
2454a163d034SWarner Losh  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
24558355f576SJeff Roberson  *
24568355f576SJeff Roberson  * Returns
24578355f576SJeff Roberson  *	NULL if there is no memory and M_NOWAIT is set
2458bbee39c6SJeff Roberson  *	An item if successful
24598355f576SJeff Roberson  */
24608355f576SJeff Roberson 
24618355f576SJeff Roberson static void *
2462e20a199fSJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int flags)
24638355f576SJeff Roberson {
24648355f576SJeff Roberson 	void *item;
24658355f576SJeff Roberson 
24668355f576SJeff Roberson 	item = NULL;
24678355f576SJeff Roberson 
24688355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
24698355f576SJeff Roberson 	printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
24708355f576SJeff Roberson #endif
24710095a784SJeff Roberson 	if (zone->uz_import(zone->uz_arg, &item, 1, flags) != 1)
24720095a784SJeff Roberson 		goto fail;
24730095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, 1);
24748355f576SJeff Roberson 
2475099a0e58SBosko Milekic 	/*
2476099a0e58SBosko Milekic 	 * We have to call both the zone's init (not the keg's init)
2477099a0e58SBosko Milekic 	 * and the zone's ctor.  This is because the item is going from
2478099a0e58SBosko Milekic 	 * a keg slab directly to the user, and the user is expecting it
2479099a0e58SBosko Milekic 	 * to be both zone-init'd as well as zone-ctor'd.
2480099a0e58SBosko Milekic 	 */
2481b23f72e9SBrian Feldman 	if (zone->uz_init != NULL) {
2482e20a199fSJeff Roberson 		if (zone->uz_init(item, zone->uz_size, flags) != 0) {
24830095a784SJeff Roberson 			zone_free_item(zone, item, udata, SKIP_FINI);
24840095a784SJeff Roberson 			goto fail;
2485b23f72e9SBrian Feldman 		}
2486b23f72e9SBrian Feldman 	}
2487b23f72e9SBrian Feldman 	if (zone->uz_ctor != NULL) {
2488e20a199fSJeff Roberson 		if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
24890095a784SJeff Roberson 			zone_free_item(zone, item, udata, SKIP_DTOR);
24900095a784SJeff Roberson 			goto fail;
2491b23f72e9SBrian Feldman 		}
2492b23f72e9SBrian Feldman 	}
2493ef72505eSJeff Roberson #ifdef INVARIANTS
24940095a784SJeff Roberson 	uma_dbg_alloc(zone, NULL, item);
2495ef72505eSJeff Roberson #endif
24962cc35ff9SJeff Roberson 	if (flags & M_ZERO)
2497e20a199fSJeff Roberson 		bzero(item, zone->uz_size);
24988355f576SJeff Roberson 
24998355f576SJeff Roberson 	return (item);
25000095a784SJeff Roberson 
25010095a784SJeff Roberson fail:
25020095a784SJeff Roberson 	atomic_add_long(&zone->uz_fails, 1);
25030095a784SJeff Roberson 	return (NULL);
25048355f576SJeff Roberson }
25058355f576SJeff Roberson 
25068355f576SJeff Roberson /* See uma.h */
25078355f576SJeff Roberson void
25088355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
25098355f576SJeff Roberson {
25108355f576SJeff Roberson 	uma_cache_t cache;
25118355f576SJeff Roberson 	uma_bucket_t bucket;
25128355f576SJeff Roberson 	int cpu;
25138355f576SJeff Roberson 
25148355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1
25158355f576SJeff Roberson 	printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
25168355f576SJeff Roberson #endif
25173659f747SRobert Watson 	CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
25183659f747SRobert Watson 	    zone->uz_name);
25193659f747SRobert Watson 
252020ed0cb0SMatthew D Fleming         /* uma_zfree(..., NULL) does nothing, to match free(9). */
252120ed0cb0SMatthew D Fleming         if (item == NULL)
252220ed0cb0SMatthew D Fleming                 return;
25238d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
25248d689e04SGleb Smirnoff 	if (is_memguard_addr(item)) {
25258d689e04SGleb Smirnoff 		if (zone->uz_dtor != NULL && zone->uz_dtor != mtrash_dtor)
25268d689e04SGleb Smirnoff 			zone->uz_dtor(item, zone->uz_size, udata);
25278d689e04SGleb Smirnoff 		if (zone->uz_fini != NULL && zone->uz_fini != mtrash_fini)
25288d689e04SGleb Smirnoff 			zone->uz_fini(item, zone->uz_size);
25298d689e04SGleb Smirnoff 		memguard_free(item);
25308d689e04SGleb Smirnoff 		return;
25318d689e04SGleb Smirnoff 	}
25328d689e04SGleb Smirnoff #endif
25335d1ae027SRobert Watson #ifdef INVARIANTS
2534e20a199fSJeff Roberson 	if (zone->uz_flags & UMA_ZONE_MALLOC)
25355d1ae027SRobert Watson 		uma_dbg_free(zone, udata, item);
25365d1ae027SRobert Watson 	else
25375d1ae027SRobert Watson 		uma_dbg_free(zone, NULL, item);
25385d1ae027SRobert Watson #endif
2539fc03d22bSJeff Roberson 	if (zone->uz_dtor != NULL)
2540ef72505eSJeff Roberson 		zone->uz_dtor(item, zone->uz_size, udata);
2541ef72505eSJeff Roberson 
2542af7f9b97SJeff Roberson 	/*
2543af7f9b97SJeff Roberson 	 * The race here is acceptable.  If we miss it we'll just have to wait
2544af7f9b97SJeff Roberson 	 * a little longer for the limits to be reset.
2545af7f9b97SJeff Roberson 	 */
2546e20a199fSJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_FULL)
2547fc03d22bSJeff Roberson 		goto zfree_item;
2548af7f9b97SJeff Roberson 
25495d1ae027SRobert Watson 	/*
25505d1ae027SRobert Watson 	 * If possible, free to the per-CPU cache.  There are two
25515d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
25525d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
25535d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
25545d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
25555d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
25565d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to free to the
25575d1ae027SRobert Watson 	 * current cache; when we re-acquire the critical section, we must
25585d1ae027SRobert Watson 	 * detect and handle migration if it has occurred.
25595d1ae027SRobert Watson 	 */
2560a553d4b8SJeff Roberson zfree_restart:
25615d1ae027SRobert Watson 	critical_enter();
25625d1ae027SRobert Watson 	cpu = curcpu;
25638355f576SJeff Roberson 	cache = &zone->uz_cpu[cpu];
25648355f576SJeff Roberson 
25658355f576SJeff Roberson zfree_start:
2566a553d4b8SJeff Roberson 	/*
2567fc03d22bSJeff Roberson 	 * Try to free into the allocbucket first to give LIFO ordering
2568fc03d22bSJeff Roberson 	 * for cache-hot datastructures.  Spill over into the freebucket
2569fc03d22bSJeff Roberson 	 * if necessary.  Alloc will swap them if one runs dry.
2570a553d4b8SJeff Roberson 	 */
2571fc03d22bSJeff Roberson 	bucket = cache->uc_allocbucket;
2572fc03d22bSJeff Roberson 	if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries)
2573fc03d22bSJeff Roberson 		bucket = cache->uc_freebucket;
2574fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2575cae33c14SJeff Roberson 		KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
25768355f576SJeff Roberson 		    ("uma_zfree: Freeing to non free bucket index."));
2577cae33c14SJeff Roberson 		bucket->ub_bucket[bucket->ub_cnt] = item;
2578cae33c14SJeff Roberson 		bucket->ub_cnt++;
2579773df9abSRobert Watson 		cache->uc_frees++;
25805d1ae027SRobert Watson 		critical_exit();
25818355f576SJeff Roberson 		return;
2582fc03d22bSJeff Roberson 	}
2583fc03d22bSJeff Roberson 
25848355f576SJeff Roberson 	/*
25855d1ae027SRobert Watson 	 * We must go back the zone, which requires acquiring the zone lock,
25865d1ae027SRobert Watson 	 * which in turn means we must release and re-acquire the critical
25875d1ae027SRobert Watson 	 * section.  Since the critical section is released, we may be
25885d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
25895d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
25905d1ae027SRobert Watson 	 * the critical section.
25918355f576SJeff Roberson 	 */
25925d1ae027SRobert Watson 	critical_exit();
2593fc03d22bSJeff Roberson 	if (zone->uz_count == 0 || bucketdisable)
2594fc03d22bSJeff Roberson 		goto zfree_item;
2595fc03d22bSJeff Roberson 
25968355f576SJeff Roberson 	ZONE_LOCK(zone);
25975d1ae027SRobert Watson 	critical_enter();
25985d1ae027SRobert Watson 	cpu = curcpu;
25995d1ae027SRobert Watson 	cache = &zone->uz_cpu[cpu];
26008355f576SJeff Roberson 
2601fc03d22bSJeff Roberson 	/*
2602fc03d22bSJeff Roberson 	 * Since we have locked the zone we may as well send back our stats.
2603fc03d22bSJeff Roberson 	 */
26040095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
26050095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, cache->uc_frees);
2606f4ff923bSRobert Watson 	cache->uc_allocs = 0;
2607f4ff923bSRobert Watson 	cache->uc_frees = 0;
2608f4ff923bSRobert Watson 
26098355f576SJeff Roberson 	bucket = cache->uc_freebucket;
2610fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2611fc03d22bSJeff Roberson 		ZONE_UNLOCK(zone);
2612fc03d22bSJeff Roberson 		goto zfree_start;
2613fc03d22bSJeff Roberson 	}
26148355f576SJeff Roberson 	cache->uc_freebucket = NULL;
26158355f576SJeff Roberson 
26168355f576SJeff Roberson 	/* Can we throw this on the zone full list? */
26178355f576SJeff Roberson 	if (bucket != NULL) {
26188355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
26198355f576SJeff Roberson 		printf("uma_zfree: Putting old bucket on the free list.\n");
26208355f576SJeff Roberson #endif
2621cae33c14SJeff Roberson 		/* ub_cnt is pointing to the last free item */
2622cae33c14SJeff Roberson 		KASSERT(bucket->ub_cnt != 0,
26238355f576SJeff Roberson 		    ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2624fc03d22bSJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
26258355f576SJeff Roberson 	}
2626fc03d22bSJeff Roberson 
26275d1ae027SRobert Watson 	/* We are no longer associated with this CPU. */
26285d1ae027SRobert Watson 	critical_exit();
2629a553d4b8SJeff Roberson 
2630a553d4b8SJeff Roberson 	/* And the zone.. */
2631a553d4b8SJeff Roberson 	ZONE_UNLOCK(zone);
2632a553d4b8SJeff Roberson 
26338355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
26348355f576SJeff Roberson 	printf("uma_zfree: Allocating new free bucket.\n");
26358355f576SJeff Roberson #endif
2636*af526374SJeff Roberson 	bucket = bucket_alloc(zone, M_NOWAIT);
26374741dcbfSJeff Roberson 	if (bucket) {
2638fc03d22bSJeff Roberson 		critical_enter();
2639fc03d22bSJeff Roberson 		cpu = curcpu;
2640fc03d22bSJeff Roberson 		cache = &zone->uz_cpu[cpu];
2641fc03d22bSJeff Roberson 		if (cache->uc_freebucket == NULL) {
2642fc03d22bSJeff Roberson 			cache->uc_freebucket = bucket;
2643fc03d22bSJeff Roberson 			goto zfree_start;
2644fc03d22bSJeff Roberson 		}
2645fc03d22bSJeff Roberson 		/*
2646fc03d22bSJeff Roberson 		 * We lost the race, start over.  We have to drop our
2647fc03d22bSJeff Roberson 		 * critical section to free the bucket.
2648fc03d22bSJeff Roberson 		 */
2649fc03d22bSJeff Roberson 		critical_exit();
2650*af526374SJeff Roberson 		bucket_free(zone, bucket);
2651a553d4b8SJeff Roberson 		goto zfree_restart;
26528355f576SJeff Roberson 	}
26538355f576SJeff Roberson 
2654a553d4b8SJeff Roberson 	/*
2655a553d4b8SJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
2656a553d4b8SJeff Roberson 	 */
2657fc03d22bSJeff Roberson zfree_item:
26580095a784SJeff Roberson 	zone_free_item(zone, item, udata, SKIP_DTOR);
26598355f576SJeff Roberson 
26608355f576SJeff Roberson 	return;
26618355f576SJeff Roberson }
26628355f576SJeff Roberson 
26638355f576SJeff Roberson static void
26640095a784SJeff Roberson slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item)
26658355f576SJeff Roberson {
266685dcf349SGleb Smirnoff 	uint8_t freei;
2667099a0e58SBosko Milekic 
26680095a784SJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2669e20a199fSJeff Roberson 	MPASS(keg == slab->us_keg);
26708355f576SJeff Roberson 
26718355f576SJeff Roberson 	/* Do we need to remove from any lists? */
2672099a0e58SBosko Milekic 	if (slab->us_freecount+1 == keg->uk_ipers) {
26738355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
2674099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
26758355f576SJeff Roberson 	} else if (slab->us_freecount == 0) {
26768355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
2677099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
26788355f576SJeff Roberson 	}
26798355f576SJeff Roberson 
2680ef72505eSJeff Roberson 	/* Slab management. */
2681ef72505eSJeff Roberson 	freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
2682ef72505eSJeff Roberson 	BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
26838355f576SJeff Roberson 	slab->us_freecount++;
26848355f576SJeff Roberson 
2685ef72505eSJeff Roberson 	/* Keg statistics. */
2686099a0e58SBosko Milekic 	keg->uk_free++;
26870095a784SJeff Roberson }
26880095a784SJeff Roberson 
26890095a784SJeff Roberson static void
26900095a784SJeff Roberson zone_release(uma_zone_t zone, void **bucket, int cnt)
26910095a784SJeff Roberson {
26920095a784SJeff Roberson 	void *item;
26930095a784SJeff Roberson 	uma_slab_t slab;
26940095a784SJeff Roberson 	uma_keg_t keg;
26950095a784SJeff Roberson 	uint8_t *mem;
26960095a784SJeff Roberson 	int clearfull;
26970095a784SJeff Roberson 	int i;
26988355f576SJeff Roberson 
2699e20a199fSJeff Roberson 	clearfull = 0;
27000095a784SJeff Roberson 	keg = zone_first_keg(zone);
2701*af526374SJeff Roberson 	KEG_LOCK(keg);
27020095a784SJeff Roberson 	for (i = 0; i < cnt; i++) {
27030095a784SJeff Roberson 		item = bucket[i];
27040095a784SJeff Roberson 		if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
27050095a784SJeff Roberson 			mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
27060095a784SJeff Roberson 			if (zone->uz_flags & UMA_ZONE_HASH) {
27070095a784SJeff Roberson 				slab = hash_sfind(&keg->uk_hash, mem);
27080095a784SJeff Roberson 			} else {
27090095a784SJeff Roberson 				mem += keg->uk_pgoff;
27100095a784SJeff Roberson 				slab = (uma_slab_t)mem;
27110095a784SJeff Roberson 			}
27120095a784SJeff Roberson 		} else {
27130095a784SJeff Roberson 			slab = vtoslab((vm_offset_t)item);
27140095a784SJeff Roberson 			if (slab->us_keg != keg) {
27150095a784SJeff Roberson 				KEG_UNLOCK(keg);
27160095a784SJeff Roberson 				keg = slab->us_keg;
27170095a784SJeff Roberson 				KEG_LOCK(keg);
27180095a784SJeff Roberson 			}
27190095a784SJeff Roberson 		}
27200095a784SJeff Roberson 		slab_free_item(keg, slab, item);
2721099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZFLAG_FULL) {
2722e20a199fSJeff Roberson 			if (keg->uk_pages < keg->uk_maxpages) {
2723099a0e58SBosko Milekic 				keg->uk_flags &= ~UMA_ZFLAG_FULL;
2724e20a199fSJeff Roberson 				clearfull = 1;
2725e20a199fSJeff Roberson 			}
2726af7f9b97SJeff Roberson 
272777380291SMohan Srinivasan 			/*
2728ef72505eSJeff Roberson 			 * We can handle one more allocation. Since we're
2729ef72505eSJeff Roberson 			 * clearing ZFLAG_FULL, wake up all procs blocked
2730ef72505eSJeff Roberson 			 * on pages. This should be uncommon, so keeping this
2731ef72505eSJeff Roberson 			 * simple for now (rather than adding count of blocked
273277380291SMohan Srinivasan 			 * threads etc).
273377380291SMohan Srinivasan 			 */
273477380291SMohan Srinivasan 			wakeup(keg);
2735af7f9b97SJeff Roberson 		}
27360095a784SJeff Roberson 	}
2737*af526374SJeff Roberson 	KEG_UNLOCK(keg);
27380095a784SJeff Roberson 	if (clearfull) {
2739*af526374SJeff Roberson 		ZONE_LOCK(zone);
2740e20a199fSJeff Roberson 		zone->uz_flags &= ~UMA_ZFLAG_FULL;
2741e20a199fSJeff Roberson 		wakeup(zone);
2742605cbd6aSJeff Roberson 		ZONE_UNLOCK(zone);
2743*af526374SJeff Roberson 	}
2744ef72505eSJeff Roberson 
27458355f576SJeff Roberson }
27468355f576SJeff Roberson 
27470095a784SJeff Roberson /*
27480095a784SJeff Roberson  * Frees a single item to any zone.
27490095a784SJeff Roberson  *
27500095a784SJeff Roberson  * Arguments:
27510095a784SJeff Roberson  *	zone   The zone to free to
27520095a784SJeff Roberson  *	item   The item we're freeing
27530095a784SJeff Roberson  *	udata  User supplied data for the dtor
27540095a784SJeff Roberson  *	skip   Skip dtors and finis
27550095a784SJeff Roberson  */
27560095a784SJeff Roberson static void
27570095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
27580095a784SJeff Roberson {
27590095a784SJeff Roberson 
27600095a784SJeff Roberson #ifdef INVARIANTS
27610095a784SJeff Roberson 	if (skip == SKIP_NONE) {
27620095a784SJeff Roberson 		if (zone->uz_flags & UMA_ZONE_MALLOC)
27630095a784SJeff Roberson 			uma_dbg_free(zone, udata, item);
27640095a784SJeff Roberson 		else
27650095a784SJeff Roberson 			uma_dbg_free(zone, NULL, item);
27660095a784SJeff Roberson 	}
27670095a784SJeff Roberson #endif
27680095a784SJeff Roberson 	if (skip < SKIP_DTOR && zone->uz_dtor)
27690095a784SJeff Roberson 		zone->uz_dtor(item, zone->uz_size, udata);
27700095a784SJeff Roberson 
27710095a784SJeff Roberson 	if (skip < SKIP_FINI && zone->uz_fini)
27720095a784SJeff Roberson 		zone->uz_fini(item, zone->uz_size);
27730095a784SJeff Roberson 
27740095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, 1);
27750095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, &item, 1);
27760095a784SJeff Roberson }
27770095a784SJeff Roberson 
27788355f576SJeff Roberson /* See uma.h */
27791c6cae97SLawrence Stewart int
2780736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems)
2781736ee590SJeff Roberson {
2782099a0e58SBosko Milekic 	uma_keg_t keg;
2783099a0e58SBosko Milekic 
2784e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
27850095a784SJeff Roberson 	if (keg == NULL)
27860095a784SJeff Roberson 		return (0);
2787*af526374SJeff Roberson 	KEG_LOCK(keg);
2788e20a199fSJeff Roberson 	keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
2789099a0e58SBosko Milekic 	if (keg->uk_maxpages * keg->uk_ipers < nitems)
2790e20a199fSJeff Roberson 		keg->uk_maxpages += keg->uk_ppera;
27911c6cae97SLawrence Stewart 	nitems = keg->uk_maxpages * keg->uk_ipers;
2792*af526374SJeff Roberson 	KEG_UNLOCK(keg);
27931c6cae97SLawrence Stewart 
27941c6cae97SLawrence Stewart 	return (nitems);
2795736ee590SJeff Roberson }
2796736ee590SJeff Roberson 
2797736ee590SJeff Roberson /* See uma.h */
2798e49471b0SAndre Oppermann int
2799e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone)
2800e49471b0SAndre Oppermann {
2801e49471b0SAndre Oppermann 	int nitems;
2802e49471b0SAndre Oppermann 	uma_keg_t keg;
2803e49471b0SAndre Oppermann 
2804e49471b0SAndre Oppermann 	keg = zone_first_keg(zone);
28050095a784SJeff Roberson 	if (keg == NULL)
28060095a784SJeff Roberson 		return (0);
2807*af526374SJeff Roberson 	KEG_LOCK(keg);
2808e49471b0SAndre Oppermann 	nitems = keg->uk_maxpages * keg->uk_ipers;
2809*af526374SJeff Roberson 	KEG_UNLOCK(keg);
2810e49471b0SAndre Oppermann 
2811e49471b0SAndre Oppermann 	return (nitems);
2812e49471b0SAndre Oppermann }
2813e49471b0SAndre Oppermann 
2814e49471b0SAndre Oppermann /* See uma.h */
28152f891cd5SPawel Jakub Dawidek void
28162f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning)
28172f891cd5SPawel Jakub Dawidek {
28182f891cd5SPawel Jakub Dawidek 
28192f891cd5SPawel Jakub Dawidek 	ZONE_LOCK(zone);
28202f891cd5SPawel Jakub Dawidek 	zone->uz_warning = warning;
28212f891cd5SPawel Jakub Dawidek 	ZONE_UNLOCK(zone);
28222f891cd5SPawel Jakub Dawidek }
28232f891cd5SPawel Jakub Dawidek 
28242f891cd5SPawel Jakub Dawidek /* See uma.h */
2825c4ae7908SLawrence Stewart int
2826c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone)
2827c4ae7908SLawrence Stewart {
2828c4ae7908SLawrence Stewart 	int64_t nitems;
2829c4ae7908SLawrence Stewart 	u_int i;
2830c4ae7908SLawrence Stewart 
2831c4ae7908SLawrence Stewart 	ZONE_LOCK(zone);
2832c4ae7908SLawrence Stewart 	nitems = zone->uz_allocs - zone->uz_frees;
2833c4ae7908SLawrence Stewart 	CPU_FOREACH(i) {
2834c4ae7908SLawrence Stewart 		/*
2835c4ae7908SLawrence Stewart 		 * See the comment in sysctl_vm_zone_stats() regarding the
2836c4ae7908SLawrence Stewart 		 * safety of accessing the per-cpu caches. With the zone lock
2837c4ae7908SLawrence Stewart 		 * held, it is safe, but can potentially result in stale data.
2838c4ae7908SLawrence Stewart 		 */
2839c4ae7908SLawrence Stewart 		nitems += zone->uz_cpu[i].uc_allocs -
2840c4ae7908SLawrence Stewart 		    zone->uz_cpu[i].uc_frees;
2841c4ae7908SLawrence Stewart 	}
2842c4ae7908SLawrence Stewart 	ZONE_UNLOCK(zone);
2843c4ae7908SLawrence Stewart 
2844c4ae7908SLawrence Stewart 	return (nitems < 0 ? 0 : nitems);
2845c4ae7908SLawrence Stewart }
2846c4ae7908SLawrence Stewart 
2847c4ae7908SLawrence Stewart /* See uma.h */
2848736ee590SJeff Roberson void
2849099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2850099a0e58SBosko Milekic {
2851e20a199fSJeff Roberson 	uma_keg_t keg;
2852e20a199fSJeff Roberson 
2853e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
28540095a784SJeff Roberson 	KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
2855*af526374SJeff Roberson 	KEG_LOCK(keg);
2856e20a199fSJeff Roberson 	KASSERT(keg->uk_pages == 0,
2857099a0e58SBosko Milekic 	    ("uma_zone_set_init on non-empty keg"));
2858e20a199fSJeff Roberson 	keg->uk_init = uminit;
2859*af526374SJeff Roberson 	KEG_UNLOCK(keg);
2860099a0e58SBosko Milekic }
2861099a0e58SBosko Milekic 
2862099a0e58SBosko Milekic /* See uma.h */
2863099a0e58SBosko Milekic void
2864099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2865099a0e58SBosko Milekic {
2866e20a199fSJeff Roberson 	uma_keg_t keg;
2867e20a199fSJeff Roberson 
2868e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
28690095a784SJeff Roberson 	KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
2870*af526374SJeff Roberson 	KEG_LOCK(keg);
2871e20a199fSJeff Roberson 	KASSERT(keg->uk_pages == 0,
2872099a0e58SBosko Milekic 	    ("uma_zone_set_fini on non-empty keg"));
2873e20a199fSJeff Roberson 	keg->uk_fini = fini;
2874*af526374SJeff Roberson 	KEG_UNLOCK(keg);
2875099a0e58SBosko Milekic }
2876099a0e58SBosko Milekic 
2877099a0e58SBosko Milekic /* See uma.h */
2878099a0e58SBosko Milekic void
2879099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2880099a0e58SBosko Milekic {
2881*af526374SJeff Roberson 
2882099a0e58SBosko Milekic 	ZONE_LOCK(zone);
2883e20a199fSJeff Roberson 	KASSERT(zone_first_keg(zone)->uk_pages == 0,
2884099a0e58SBosko Milekic 	    ("uma_zone_set_zinit on non-empty keg"));
2885099a0e58SBosko Milekic 	zone->uz_init = zinit;
2886099a0e58SBosko Milekic 	ZONE_UNLOCK(zone);
2887099a0e58SBosko Milekic }
2888099a0e58SBosko Milekic 
2889099a0e58SBosko Milekic /* See uma.h */
2890099a0e58SBosko Milekic void
2891099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2892099a0e58SBosko Milekic {
2893*af526374SJeff Roberson 
2894099a0e58SBosko Milekic 	ZONE_LOCK(zone);
2895e20a199fSJeff Roberson 	KASSERT(zone_first_keg(zone)->uk_pages == 0,
2896099a0e58SBosko Milekic 	    ("uma_zone_set_zfini on non-empty keg"));
2897099a0e58SBosko Milekic 	zone->uz_fini = zfini;
2898099a0e58SBosko Milekic 	ZONE_UNLOCK(zone);
2899099a0e58SBosko Milekic }
2900099a0e58SBosko Milekic 
2901099a0e58SBosko Milekic /* See uma.h */
2902b23f72e9SBrian Feldman /* XXX uk_freef is not actually used with the zone locked */
2903099a0e58SBosko Milekic void
29048355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef)
29058355f576SJeff Roberson {
29060095a784SJeff Roberson 	uma_keg_t keg;
2907e20a199fSJeff Roberson 
29080095a784SJeff Roberson 	keg = zone_first_keg(zone);
29090095a784SJeff Roberson 	KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
2910*af526374SJeff Roberson 	KEG_LOCK(keg);
29110095a784SJeff Roberson 	keg->uk_freef = freef;
2912*af526374SJeff Roberson 	KEG_UNLOCK(keg);
29138355f576SJeff Roberson }
29148355f576SJeff Roberson 
29158355f576SJeff Roberson /* See uma.h */
2916b23f72e9SBrian Feldman /* XXX uk_allocf is not actually used with the zone locked */
29178355f576SJeff Roberson void
29188355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
29198355f576SJeff Roberson {
2920e20a199fSJeff Roberson 	uma_keg_t keg;
2921e20a199fSJeff Roberson 
2922e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
2923*af526374SJeff Roberson 	KEG_LOCK(keg);
2924e20a199fSJeff Roberson 	keg->uk_flags |= UMA_ZFLAG_PRIVALLOC;
2925e20a199fSJeff Roberson 	keg->uk_allocf = allocf;
2926*af526374SJeff Roberson 	KEG_UNLOCK(keg);
29278355f576SJeff Roberson }
29288355f576SJeff Roberson 
29298355f576SJeff Roberson /* See uma.h */
29308355f576SJeff Roberson int
2931a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count)
29328355f576SJeff Roberson {
2933099a0e58SBosko Milekic 	uma_keg_t keg;
29348355f576SJeff Roberson 	vm_offset_t kva;
2935099a0e58SBosko Milekic 	int pages;
29368355f576SJeff Roberson 
2937e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
29380095a784SJeff Roberson 	if (keg == NULL)
29390095a784SJeff Roberson 		return (0);
2940099a0e58SBosko Milekic 	pages = count / keg->uk_ipers;
29418355f576SJeff Roberson 
2942099a0e58SBosko Milekic 	if (pages * keg->uk_ipers < count)
29438355f576SJeff Roberson 		pages++;
2944a553d4b8SJeff Roberson 
2945a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
2946a4915c21SAttilio Rao 	if (keg->uk_ppera > 1) {
2947a4915c21SAttilio Rao #else
2948a4915c21SAttilio Rao 	if (1) {
2949a4915c21SAttilio Rao #endif
29505285558aSAlan Cox 		kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE);
2951d1f42ac2SAlan Cox 		if (kva == 0)
29528355f576SJeff Roberson 			return (0);
2953a4915c21SAttilio Rao 	} else
2954a4915c21SAttilio Rao 		kva = 0;
2955*af526374SJeff Roberson 	KEG_LOCK(keg);
2956099a0e58SBosko Milekic 	keg->uk_kva = kva;
2957a4915c21SAttilio Rao 	keg->uk_offset = 0;
2958099a0e58SBosko Milekic 	keg->uk_maxpages = pages;
2959a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
2960a4915c21SAttilio Rao 	keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
2961a4915c21SAttilio Rao #else
2962a4915c21SAttilio Rao 	keg->uk_allocf = noobj_alloc;
2963a4915c21SAttilio Rao #endif
2964099a0e58SBosko Milekic 	keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC;
2965*af526374SJeff Roberson 	KEG_UNLOCK(keg);
2966*af526374SJeff Roberson 
29678355f576SJeff Roberson 	return (1);
29688355f576SJeff Roberson }
29698355f576SJeff Roberson 
29708355f576SJeff Roberson /* See uma.h */
29718355f576SJeff Roberson void
29728355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items)
29738355f576SJeff Roberson {
29748355f576SJeff Roberson 	int slabs;
29758355f576SJeff Roberson 	uma_slab_t slab;
2976099a0e58SBosko Milekic 	uma_keg_t keg;
29778355f576SJeff Roberson 
2978e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
29790095a784SJeff Roberson 	if (keg == NULL)
29800095a784SJeff Roberson 		return;
2981*af526374SJeff Roberson 	KEG_LOCK(keg);
2982099a0e58SBosko Milekic 	slabs = items / keg->uk_ipers;
2983099a0e58SBosko Milekic 	if (slabs * keg->uk_ipers < items)
29848355f576SJeff Roberson 		slabs++;
29858355f576SJeff Roberson 	while (slabs > 0) {
2986e20a199fSJeff Roberson 		slab = keg_alloc_slab(keg, zone, M_WAITOK);
2987e20a199fSJeff Roberson 		if (slab == NULL)
2988e20a199fSJeff Roberson 			break;
2989e20a199fSJeff Roberson 		MPASS(slab->us_keg == keg);
2990099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
29918355f576SJeff Roberson 		slabs--;
29928355f576SJeff Roberson 	}
2993*af526374SJeff Roberson 	KEG_UNLOCK(keg);
29948355f576SJeff Roberson }
29958355f576SJeff Roberson 
29968355f576SJeff Roberson /* See uma.h */
299785dcf349SGleb Smirnoff uint32_t *
2998099a0e58SBosko Milekic uma_find_refcnt(uma_zone_t zone, void *item)
2999099a0e58SBosko Milekic {
3000ab14a3f7SBrian Feldman 	uma_slabrefcnt_t slabref;
3001ef72505eSJeff Roberson 	uma_slab_t slab;
3002099a0e58SBosko Milekic 	uma_keg_t keg;
300385dcf349SGleb Smirnoff 	uint32_t *refcnt;
3004099a0e58SBosko Milekic 	int idx;
3005099a0e58SBosko Milekic 
3006ef72505eSJeff Roberson 	slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK));
3007ef72505eSJeff Roberson 	slabref = (uma_slabrefcnt_t)slab;
3008ef72505eSJeff Roberson 	keg = slab->us_keg;
3009ef72505eSJeff Roberson 	KASSERT(keg->uk_flags & UMA_ZONE_REFCNT,
3010099a0e58SBosko Milekic 	    ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
3011ef72505eSJeff Roberson 	idx = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3012ef72505eSJeff Roberson 	refcnt = &slabref->us_refcnt[idx];
3013099a0e58SBosko Milekic 	return refcnt;
3014099a0e58SBosko Milekic }
3015099a0e58SBosko Milekic 
3016099a0e58SBosko Milekic /* See uma.h */
30178355f576SJeff Roberson void
30188355f576SJeff Roberson uma_reclaim(void)
30198355f576SJeff Roberson {
30208355f576SJeff Roberson #ifdef UMA_DEBUG
30218355f576SJeff Roberson 	printf("UMA: vm asked us to release pages!\n");
30228355f576SJeff Roberson #endif
302386bbae32SJeff Roberson 	bucket_enable();
30248355f576SJeff Roberson 	zone_foreach(zone_drain);
30258355f576SJeff Roberson 	/*
30268355f576SJeff Roberson 	 * Some slabs may have been freed but this zone will be visited early
30278355f576SJeff Roberson 	 * we visit again so that we can free pages that are empty once other
30288355f576SJeff Roberson 	 * zones are drained.  We have to do the same for buckets.
30298355f576SJeff Roberson 	 */
30309643769aSJeff Roberson 	zone_drain(slabzone);
3031099a0e58SBosko Milekic 	zone_drain(slabrefzone);
3032cae33c14SJeff Roberson 	bucket_zone_drain();
30338355f576SJeff Roberson }
30348355f576SJeff Roberson 
3035663b416fSJohn Baldwin /* See uma.h */
3036663b416fSJohn Baldwin int
3037663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone)
3038663b416fSJohn Baldwin {
3039663b416fSJohn Baldwin 	int full;
3040663b416fSJohn Baldwin 
3041663b416fSJohn Baldwin 	ZONE_LOCK(zone);
3042e20a199fSJeff Roberson 	full = (zone->uz_flags & UMA_ZFLAG_FULL);
3043663b416fSJohn Baldwin 	ZONE_UNLOCK(zone);
3044663b416fSJohn Baldwin 	return (full);
3045663b416fSJohn Baldwin }
3046663b416fSJohn Baldwin 
30476c125b8dSMohan Srinivasan int
30486c125b8dSMohan Srinivasan uma_zone_exhausted_nolock(uma_zone_t zone)
30496c125b8dSMohan Srinivasan {
3050e20a199fSJeff Roberson 	return (zone->uz_flags & UMA_ZFLAG_FULL);
30516c125b8dSMohan Srinivasan }
30526c125b8dSMohan Srinivasan 
30538355f576SJeff Roberson void *
30548355f576SJeff Roberson uma_large_malloc(int size, int wait)
30558355f576SJeff Roberson {
30568355f576SJeff Roberson 	void *mem;
30578355f576SJeff Roberson 	uma_slab_t slab;
305885dcf349SGleb Smirnoff 	uint8_t flags;
30598355f576SJeff Roberson 
3060e20a199fSJeff Roberson 	slab = zone_alloc_item(slabzone, NULL, wait);
30618355f576SJeff Roberson 	if (slab == NULL)
30628355f576SJeff Roberson 		return (NULL);
30638355f576SJeff Roberson 	mem = page_alloc(NULL, size, &flags, wait);
30648355f576SJeff Roberson 	if (mem) {
306599571dc3SJeff Roberson 		vsetslab((vm_offset_t)mem, slab);
30668355f576SJeff Roberson 		slab->us_data = mem;
30678355f576SJeff Roberson 		slab->us_flags = flags | UMA_SLAB_MALLOC;
30688355f576SJeff Roberson 		slab->us_size = size;
30698355f576SJeff Roberson 	} else {
30700095a784SJeff Roberson 		zone_free_item(slabzone, slab, NULL, SKIP_NONE);
30718355f576SJeff Roberson 	}
30728355f576SJeff Roberson 
30738355f576SJeff Roberson 	return (mem);
30748355f576SJeff Roberson }
30758355f576SJeff Roberson 
30768355f576SJeff Roberson void
30778355f576SJeff Roberson uma_large_free(uma_slab_t slab)
30788355f576SJeff Roberson {
307999571dc3SJeff Roberson 	vsetobj((vm_offset_t)slab->us_data, kmem_object);
30808355f576SJeff Roberson 	page_free(slab->us_data, slab->us_size, slab->us_flags);
30810095a784SJeff Roberson 	zone_free_item(slabzone, slab, NULL, SKIP_NONE);
30828355f576SJeff Roberson }
30838355f576SJeff Roberson 
30848355f576SJeff Roberson void
30858355f576SJeff Roberson uma_print_stats(void)
30868355f576SJeff Roberson {
30878355f576SJeff Roberson 	zone_foreach(uma_print_zone);
30888355f576SJeff Roberson }
30898355f576SJeff Roberson 
3090504d5de3SJeff Roberson static void
3091504d5de3SJeff Roberson slab_print(uma_slab_t slab)
3092504d5de3SJeff Roberson {
3093ef72505eSJeff Roberson 	printf("slab: keg %p, data %p, freecount %d\n",
3094ef72505eSJeff Roberson 		slab->us_keg, slab->us_data, slab->us_freecount);
3095504d5de3SJeff Roberson }
3096504d5de3SJeff Roberson 
3097504d5de3SJeff Roberson static void
3098504d5de3SJeff Roberson cache_print(uma_cache_t cache)
3099504d5de3SJeff Roberson {
3100504d5de3SJeff Roberson 	printf("alloc: %p(%d), free: %p(%d)\n",
3101504d5de3SJeff Roberson 		cache->uc_allocbucket,
3102504d5de3SJeff Roberson 		cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3103504d5de3SJeff Roberson 		cache->uc_freebucket,
3104504d5de3SJeff Roberson 		cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3105504d5de3SJeff Roberson }
3106504d5de3SJeff Roberson 
3107e20a199fSJeff Roberson static void
3108e20a199fSJeff Roberson uma_print_keg(uma_keg_t keg)
31098355f576SJeff Roberson {
3110504d5de3SJeff Roberson 	uma_slab_t slab;
3111504d5de3SJeff Roberson 
31120b80c1e4SEitan Adler 	printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3113e20a199fSJeff Roberson 	    "out %d free %d limit %d\n",
3114e20a199fSJeff Roberson 	    keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3115099a0e58SBosko Milekic 	    keg->uk_ipers, keg->uk_ppera,
3116e20a199fSJeff Roberson 	    (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free,
3117e20a199fSJeff Roberson 	    (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3118504d5de3SJeff Roberson 	printf("Part slabs:\n");
3119099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
3120504d5de3SJeff Roberson 		slab_print(slab);
3121504d5de3SJeff Roberson 	printf("Free slabs:\n");
3122099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
3123504d5de3SJeff Roberson 		slab_print(slab);
3124504d5de3SJeff Roberson 	printf("Full slabs:\n");
3125099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
3126504d5de3SJeff Roberson 		slab_print(slab);
3127e20a199fSJeff Roberson }
3128e20a199fSJeff Roberson 
3129e20a199fSJeff Roberson void
3130e20a199fSJeff Roberson uma_print_zone(uma_zone_t zone)
3131e20a199fSJeff Roberson {
3132e20a199fSJeff Roberson 	uma_cache_t cache;
3133e20a199fSJeff Roberson 	uma_klink_t kl;
3134e20a199fSJeff Roberson 	int i;
3135e20a199fSJeff Roberson 
31360b80c1e4SEitan Adler 	printf("zone: %s(%p) size %d flags %#x\n",
3137e20a199fSJeff Roberson 	    zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3138e20a199fSJeff Roberson 	LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3139e20a199fSJeff Roberson 		uma_print_keg(kl->kl_keg);
31403aa6d94eSJohn Baldwin 	CPU_FOREACH(i) {
3141504d5de3SJeff Roberson 		cache = &zone->uz_cpu[i];
3142504d5de3SJeff Roberson 		printf("CPU %d Cache:\n", i);
3143504d5de3SJeff Roberson 		cache_print(cache);
3144504d5de3SJeff Roberson 	}
31458355f576SJeff Roberson }
31468355f576SJeff Roberson 
3147a0d4b0aeSRobert Watson #ifdef DDB
31488355f576SJeff Roberson /*
31497a52a97eSRobert Watson  * Generate statistics across both the zone and its per-cpu cache's.  Return
31507a52a97eSRobert Watson  * desired statistics if the pointer is non-NULL for that statistic.
31517a52a97eSRobert Watson  *
31527a52a97eSRobert Watson  * Note: does not update the zone statistics, as it can't safely clear the
31537a52a97eSRobert Watson  * per-CPU cache statistic.
31547a52a97eSRobert Watson  *
31557a52a97eSRobert Watson  * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
31567a52a97eSRobert Watson  * safe from off-CPU; we should modify the caches to track this information
31577a52a97eSRobert Watson  * directly so that we don't have to.
31587a52a97eSRobert Watson  */
31597a52a97eSRobert Watson static void
316085dcf349SGleb Smirnoff uma_zone_sumstat(uma_zone_t z, int *cachefreep, uint64_t *allocsp,
316185dcf349SGleb Smirnoff     uint64_t *freesp, uint64_t *sleepsp)
31627a52a97eSRobert Watson {
31637a52a97eSRobert Watson 	uma_cache_t cache;
316485dcf349SGleb Smirnoff 	uint64_t allocs, frees, sleeps;
31657a52a97eSRobert Watson 	int cachefree, cpu;
31667a52a97eSRobert Watson 
3167bf965959SSean Bruno 	allocs = frees = sleeps = 0;
31687a52a97eSRobert Watson 	cachefree = 0;
31693aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
31707a52a97eSRobert Watson 		cache = &z->uz_cpu[cpu];
31717a52a97eSRobert Watson 		if (cache->uc_allocbucket != NULL)
31727a52a97eSRobert Watson 			cachefree += cache->uc_allocbucket->ub_cnt;
31737a52a97eSRobert Watson 		if (cache->uc_freebucket != NULL)
31747a52a97eSRobert Watson 			cachefree += cache->uc_freebucket->ub_cnt;
31757a52a97eSRobert Watson 		allocs += cache->uc_allocs;
31767a52a97eSRobert Watson 		frees += cache->uc_frees;
31777a52a97eSRobert Watson 	}
31787a52a97eSRobert Watson 	allocs += z->uz_allocs;
31797a52a97eSRobert Watson 	frees += z->uz_frees;
3180bf965959SSean Bruno 	sleeps += z->uz_sleeps;
31817a52a97eSRobert Watson 	if (cachefreep != NULL)
31827a52a97eSRobert Watson 		*cachefreep = cachefree;
31837a52a97eSRobert Watson 	if (allocsp != NULL)
31847a52a97eSRobert Watson 		*allocsp = allocs;
31857a52a97eSRobert Watson 	if (freesp != NULL)
31867a52a97eSRobert Watson 		*freesp = frees;
3187bf965959SSean Bruno 	if (sleepsp != NULL)
3188bf965959SSean Bruno 		*sleepsp = sleeps;
31897a52a97eSRobert Watson }
3190a0d4b0aeSRobert Watson #endif /* DDB */
31917a52a97eSRobert Watson 
31927a52a97eSRobert Watson static int
31937a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
31947a52a97eSRobert Watson {
31957a52a97eSRobert Watson 	uma_keg_t kz;
31967a52a97eSRobert Watson 	uma_zone_t z;
31977a52a97eSRobert Watson 	int count;
31987a52a97eSRobert Watson 
31997a52a97eSRobert Watson 	count = 0;
32007a52a97eSRobert Watson 	mtx_lock(&uma_mtx);
32017a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
32027a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
32037a52a97eSRobert Watson 			count++;
32047a52a97eSRobert Watson 	}
32057a52a97eSRobert Watson 	mtx_unlock(&uma_mtx);
32067a52a97eSRobert Watson 	return (sysctl_handle_int(oidp, &count, 0, req));
32077a52a97eSRobert Watson }
32087a52a97eSRobert Watson 
32097a52a97eSRobert Watson static int
32107a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
32117a52a97eSRobert Watson {
32127a52a97eSRobert Watson 	struct uma_stream_header ush;
32137a52a97eSRobert Watson 	struct uma_type_header uth;
32147a52a97eSRobert Watson 	struct uma_percpu_stat ups;
32157a52a97eSRobert Watson 	uma_bucket_t bucket;
32167a52a97eSRobert Watson 	struct sbuf sbuf;
32177a52a97eSRobert Watson 	uma_cache_t cache;
3218e20a199fSJeff Roberson 	uma_klink_t kl;
32197a52a97eSRobert Watson 	uma_keg_t kz;
32207a52a97eSRobert Watson 	uma_zone_t z;
3221e20a199fSJeff Roberson 	uma_keg_t k;
32224e657159SMatthew D Fleming 	int count, error, i;
32237a52a97eSRobert Watson 
322400f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
322500f0e671SMatthew D Fleming 	if (error != 0)
322600f0e671SMatthew D Fleming 		return (error);
32274e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
32284e657159SMatthew D Fleming 
3229404a593eSMatthew D Fleming 	count = 0;
32304e657159SMatthew D Fleming 	mtx_lock(&uma_mtx);
32317a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
32327a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
32337a52a97eSRobert Watson 			count++;
32347a52a97eSRobert Watson 	}
32357a52a97eSRobert Watson 
32367a52a97eSRobert Watson 	/*
32377a52a97eSRobert Watson 	 * Insert stream header.
32387a52a97eSRobert Watson 	 */
32397a52a97eSRobert Watson 	bzero(&ush, sizeof(ush));
32407a52a97eSRobert Watson 	ush.ush_version = UMA_STREAM_VERSION;
3241ab3a57c0SRobert Watson 	ush.ush_maxcpus = (mp_maxid + 1);
32427a52a97eSRobert Watson 	ush.ush_count = count;
32434e657159SMatthew D Fleming 	(void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
32447a52a97eSRobert Watson 
32457a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
32467a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
32477a52a97eSRobert Watson 			bzero(&uth, sizeof(uth));
32487a52a97eSRobert Watson 			ZONE_LOCK(z);
3249cbbb4a00SRobert Watson 			strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
32507a52a97eSRobert Watson 			uth.uth_align = kz->uk_align;
32517a52a97eSRobert Watson 			uth.uth_size = kz->uk_size;
32527a52a97eSRobert Watson 			uth.uth_rsize = kz->uk_rsize;
3253e20a199fSJeff Roberson 			LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3254e20a199fSJeff Roberson 				k = kl->kl_keg;
3255e20a199fSJeff Roberson 				uth.uth_maxpages += k->uk_maxpages;
3256e20a199fSJeff Roberson 				uth.uth_pages += k->uk_pages;
3257e20a199fSJeff Roberson 				uth.uth_keg_free += k->uk_free;
3258e20a199fSJeff Roberson 				uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3259e20a199fSJeff Roberson 				    * k->uk_ipers;
3260e20a199fSJeff Roberson 			}
3261cbbb4a00SRobert Watson 
3262cbbb4a00SRobert Watson 			/*
3263cbbb4a00SRobert Watson 			 * A zone is secondary is it is not the first entry
3264cbbb4a00SRobert Watson 			 * on the keg's zone list.
3265cbbb4a00SRobert Watson 			 */
3266e20a199fSJeff Roberson 			if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3267cbbb4a00SRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z))
3268cbbb4a00SRobert Watson 				uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3269cbbb4a00SRobert Watson 
3270fc03d22bSJeff Roberson 			LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
32717a52a97eSRobert Watson 				uth.uth_zone_free += bucket->ub_cnt;
32727a52a97eSRobert Watson 			uth.uth_allocs = z->uz_allocs;
32737a52a97eSRobert Watson 			uth.uth_frees = z->uz_frees;
32742019094aSRobert Watson 			uth.uth_fails = z->uz_fails;
3275bf965959SSean Bruno 			uth.uth_sleeps = z->uz_sleeps;
32764e657159SMatthew D Fleming 			(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
32777a52a97eSRobert Watson 			/*
32782450bbb8SRobert Watson 			 * While it is not normally safe to access the cache
32792450bbb8SRobert Watson 			 * bucket pointers while not on the CPU that owns the
32802450bbb8SRobert Watson 			 * cache, we only allow the pointers to be exchanged
32812450bbb8SRobert Watson 			 * without the zone lock held, not invalidated, so
32822450bbb8SRobert Watson 			 * accept the possible race associated with bucket
32832450bbb8SRobert Watson 			 * exchange during monitoring.
32847a52a97eSRobert Watson 			 */
3285ab3a57c0SRobert Watson 			for (i = 0; i < (mp_maxid + 1); i++) {
32867a52a97eSRobert Watson 				bzero(&ups, sizeof(ups));
32877a52a97eSRobert Watson 				if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
32887a52a97eSRobert Watson 					goto skip;
3289082dc776SRobert Watson 				if (CPU_ABSENT(i))
3290082dc776SRobert Watson 					goto skip;
32917a52a97eSRobert Watson 				cache = &z->uz_cpu[i];
32927a52a97eSRobert Watson 				if (cache->uc_allocbucket != NULL)
32937a52a97eSRobert Watson 					ups.ups_cache_free +=
32947a52a97eSRobert Watson 					    cache->uc_allocbucket->ub_cnt;
32957a52a97eSRobert Watson 				if (cache->uc_freebucket != NULL)
32967a52a97eSRobert Watson 					ups.ups_cache_free +=
32977a52a97eSRobert Watson 					    cache->uc_freebucket->ub_cnt;
32987a52a97eSRobert Watson 				ups.ups_allocs = cache->uc_allocs;
32997a52a97eSRobert Watson 				ups.ups_frees = cache->uc_frees;
33007a52a97eSRobert Watson skip:
33014e657159SMatthew D Fleming 				(void)sbuf_bcat(&sbuf, &ups, sizeof(ups));
33027a52a97eSRobert Watson 			}
33032450bbb8SRobert Watson 			ZONE_UNLOCK(z);
33047a52a97eSRobert Watson 		}
33057a52a97eSRobert Watson 	}
33067a52a97eSRobert Watson 	mtx_unlock(&uma_mtx);
33074e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
33084e657159SMatthew D Fleming 	sbuf_delete(&sbuf);
33097a52a97eSRobert Watson 	return (error);
33107a52a97eSRobert Watson }
331148c5777eSRobert Watson 
331248c5777eSRobert Watson #ifdef DDB
331348c5777eSRobert Watson DB_SHOW_COMMAND(uma, db_show_uma)
331448c5777eSRobert Watson {
331585dcf349SGleb Smirnoff 	uint64_t allocs, frees, sleeps;
331648c5777eSRobert Watson 	uma_bucket_t bucket;
331748c5777eSRobert Watson 	uma_keg_t kz;
331848c5777eSRobert Watson 	uma_zone_t z;
331948c5777eSRobert Watson 	int cachefree;
332048c5777eSRobert Watson 
3321bf965959SSean Bruno 	db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
3322bf965959SSean Bruno 	    "Requests", "Sleeps");
332348c5777eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
332448c5777eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
332548c5777eSRobert Watson 			if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
332648c5777eSRobert Watson 				allocs = z->uz_allocs;
332748c5777eSRobert Watson 				frees = z->uz_frees;
3328bf965959SSean Bruno 				sleeps = z->uz_sleeps;
332948c5777eSRobert Watson 				cachefree = 0;
333048c5777eSRobert Watson 			} else
333148c5777eSRobert Watson 				uma_zone_sumstat(z, &cachefree, &allocs,
3332bf965959SSean Bruno 				    &frees, &sleeps);
3333e20a199fSJeff Roberson 			if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
333448c5777eSRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z)))
333548c5777eSRobert Watson 				cachefree += kz->uk_free;
3336fc03d22bSJeff Roberson 			LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
333748c5777eSRobert Watson 				cachefree += bucket->ub_cnt;
3338bf965959SSean Bruno 			db_printf("%18s %8ju %8jd %8d %12ju %8ju\n", z->uz_name,
3339ae4e9636SRobert Watson 			    (uintmax_t)kz->uk_size,
3340ae4e9636SRobert Watson 			    (intmax_t)(allocs - frees), cachefree,
3341bf965959SSean Bruno 			    (uintmax_t)allocs, sleeps);
3342687c94aaSJohn Baldwin 			if (db_pager_quit)
3343687c94aaSJohn Baldwin 				return;
334448c5777eSRobert Watson 		}
334548c5777eSRobert Watson 	}
334648c5777eSRobert Watson }
334748c5777eSRobert Watson #endif
3348