xref: /freebsd/sys/vm/uma_core.c (revision 4d104ba02490516ecb551d5873eab4e296d5446a)
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[] = {
2096fd34d6fSJeff Roberson 	{ NULL, "4 Bucket", BUCKET_SIZE(4), 4096 },
210f3932e90SAlexander Motin 	{ NULL, "6 Bucket", BUCKET_SIZE(6), 3072 },
2116fd34d6fSJeff Roberson 	{ NULL, "8 Bucket", BUCKET_SIZE(8), 2048 },
212f3932e90SAlexander Motin 	{ NULL, "12 Bucket", BUCKET_SIZE(12), 1536 },
2136fd34d6fSJeff Roberson 	{ NULL, "16 Bucket", BUCKET_SIZE(16), 1024 },
214fc03d22bSJeff Roberson 	{ NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
215fc03d22bSJeff Roberson 	{ NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
216fc03d22bSJeff Roberson 	{ NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
217fc03d22bSJeff Roberson 	{ NULL, NULL, 0}
218fc03d22bSJeff Roberson };
219cae33c14SJeff Roberson 
2202019094aSRobert Watson /*
2212019094aSRobert Watson  * Flags and enumerations to be passed to internal functions.
2222019094aSRobert Watson  */
223ef72505eSJeff Roberson enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI };
224b23f72e9SBrian Feldman 
2258355f576SJeff Roberson /* Prototypes.. */
2268355f576SJeff Roberson 
22785dcf349SGleb Smirnoff static void *noobj_alloc(uma_zone_t, int, uint8_t *, int);
22885dcf349SGleb Smirnoff static void *page_alloc(uma_zone_t, int, uint8_t *, int);
22985dcf349SGleb Smirnoff static void *startup_alloc(uma_zone_t, int, uint8_t *, int);
23085dcf349SGleb Smirnoff static void page_free(void *, int, uint8_t);
231e20a199fSJeff Roberson static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int);
2329643769aSJeff Roberson static void cache_drain(uma_zone_t);
2338355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t);
234aaa8bb16SJeff Roberson static void bucket_cache_drain(uma_zone_t zone);
235b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int);
236099a0e58SBosko Milekic static void keg_dtor(void *, int, void *);
237b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int);
2389c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *);
239b23f72e9SBrian Feldman static int zero_init(void *, int, int);
240e20a199fSJeff Roberson static void keg_small_init(uma_keg_t keg);
241e20a199fSJeff Roberson static void keg_large_init(uma_keg_t keg);
2428355f576SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t));
2438355f576SJeff Roberson static void zone_timeout(uma_zone_t zone);
2440aef6126SJeff Roberson static int hash_alloc(struct uma_hash *);
2450aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *);
2460aef6126SJeff Roberson static void hash_free(struct uma_hash *hash);
2478355f576SJeff Roberson static void uma_timeout(void *);
2488355f576SJeff Roberson static void uma_startup3(void);
249e20a199fSJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int);
2500095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
25186bbae32SJeff Roberson static void bucket_enable(void);
252cae33c14SJeff Roberson static void bucket_init(void);
2536fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int);
2546fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *);
255cae33c14SJeff Roberson static void bucket_zone_drain(void);
2566fd34d6fSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t zone, void *, int flags);
257e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags);
258e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags);
2590095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
2600095a784SJeff Roberson static void slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item);
261e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
26285dcf349SGleb Smirnoff     uma_fini fini, int align, uint32_t flags);
2630095a784SJeff Roberson static int zone_import(uma_zone_t zone, void **bucket, int max, int flags);
2640095a784SJeff Roberson static void zone_release(uma_zone_t zone, void **bucket, int cnt);
265bbee39c6SJeff Roberson 
2668355f576SJeff Roberson void uma_print_zone(uma_zone_t);
2678355f576SJeff Roberson void uma_print_stats(void);
2687a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
2697a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
2708355f576SJeff Roberson 
2718355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
2728355f576SJeff Roberson 
2737a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
2747a52a97eSRobert Watson     0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
2757a52a97eSRobert Watson 
2767a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
2777a52a97eSRobert Watson     0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
2787a52a97eSRobert Watson 
2792f891cd5SPawel Jakub Dawidek static int zone_warnings = 1;
2802f891cd5SPawel Jakub Dawidek TUNABLE_INT("vm.zone_warnings", &zone_warnings);
2812f891cd5SPawel Jakub Dawidek SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RW, &zone_warnings, 0,
2822f891cd5SPawel Jakub Dawidek     "Warn when UMA zones becomes full");
2832f891cd5SPawel Jakub Dawidek 
28486bbae32SJeff Roberson /*
28586bbae32SJeff Roberson  * This routine checks to see whether or not it's safe to enable buckets.
28686bbae32SJeff Roberson  */
28786bbae32SJeff Roberson static void
28886bbae32SJeff Roberson bucket_enable(void)
28986bbae32SJeff Roberson {
290251386b4SMaksim Yevmenkin 	bucketdisable = vm_page_count_min();
29186bbae32SJeff Roberson }
29286bbae32SJeff Roberson 
293dc2c7965SRobert Watson /*
294dc2c7965SRobert Watson  * Initialize bucket_zones, the array of zones of buckets of various sizes.
295dc2c7965SRobert Watson  *
296dc2c7965SRobert Watson  * For each zone, calculate the memory required for each bucket, consisting
297fc03d22bSJeff Roberson  * of the header and an array of pointers.
298dc2c7965SRobert Watson  */
299cae33c14SJeff Roberson static void
300cae33c14SJeff Roberson bucket_init(void)
301cae33c14SJeff Roberson {
302cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
303cae33c14SJeff Roberson 	int size;
304fc03d22bSJeff Roberson 	int i;
305cae33c14SJeff Roberson 
306fc03d22bSJeff Roberson 	for (i = 0, ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
307cae33c14SJeff Roberson 		size = roundup(sizeof(struct uma_bucket), sizeof(void *));
308cae33c14SJeff Roberson 		size += sizeof(void *) * ubz->ubz_entries;
309cae33c14SJeff Roberson 		ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
310e20a199fSJeff Roberson 		    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
3116fd34d6fSJeff Roberson 		    UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET);
312cae33c14SJeff Roberson 	}
313cae33c14SJeff Roberson }
314cae33c14SJeff Roberson 
315dc2c7965SRobert Watson /*
316dc2c7965SRobert Watson  * Given a desired number of entries for a bucket, return the zone from which
317dc2c7965SRobert Watson  * to allocate the bucket.
318dc2c7965SRobert Watson  */
319dc2c7965SRobert Watson static struct uma_bucket_zone *
320dc2c7965SRobert Watson bucket_zone_lookup(int entries)
321dc2c7965SRobert Watson {
322fc03d22bSJeff Roberson 	struct uma_bucket_zone *ubz;
323dc2c7965SRobert Watson 
324fc03d22bSJeff Roberson 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
325fc03d22bSJeff Roberson 		if (ubz->ubz_entries >= entries)
326fc03d22bSJeff Roberson 			return (ubz);
327fc03d22bSJeff Roberson 	ubz--;
328fc03d22bSJeff Roberson 	return (ubz);
329fc03d22bSJeff Roberson }
330fc03d22bSJeff Roberson 
331fc03d22bSJeff Roberson static int
332fc03d22bSJeff Roberson bucket_select(int size)
333fc03d22bSJeff Roberson {
334fc03d22bSJeff Roberson 	struct uma_bucket_zone *ubz;
335fc03d22bSJeff Roberson 
336fc03d22bSJeff Roberson 	ubz = &bucket_zones[0];
337fc03d22bSJeff Roberson 	if (size > ubz->ubz_maxsize)
338fc03d22bSJeff Roberson 		return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
339fc03d22bSJeff Roberson 
340fc03d22bSJeff Roberson 	for (; ubz->ubz_entries != 0; ubz++)
341fc03d22bSJeff Roberson 		if (ubz->ubz_maxsize < size)
342fc03d22bSJeff Roberson 			break;
343fc03d22bSJeff Roberson 	ubz--;
344fc03d22bSJeff Roberson 	return (ubz->ubz_entries);
345dc2c7965SRobert Watson }
346dc2c7965SRobert Watson 
347cae33c14SJeff Roberson static uma_bucket_t
3486fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags)
349cae33c14SJeff Roberson {
350cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
351cae33c14SJeff Roberson 	uma_bucket_t bucket;
352cae33c14SJeff Roberson 
353cae33c14SJeff Roberson 	/*
354cae33c14SJeff Roberson 	 * This is to stop us from allocating per cpu buckets while we're
3553803b26bSDag-Erling Smørgrav 	 * running out of vm.boot_pages.  Otherwise, we would exhaust the
356cae33c14SJeff Roberson 	 * boot pages.  This also prevents us from allocating buckets in
357cae33c14SJeff Roberson 	 * low memory situations.
358cae33c14SJeff Roberson 	 */
359cae33c14SJeff Roberson 	if (bucketdisable)
360cae33c14SJeff Roberson 		return (NULL);
3616fd34d6fSJeff Roberson 	/*
3626fd34d6fSJeff Roberson 	 * To limit bucket recursion we store the original zone flags
3636fd34d6fSJeff Roberson 	 * in a cookie passed via zalloc_arg/zfree_arg.  This allows the
3646fd34d6fSJeff Roberson 	 * NOVM flag to persist even through deep recursions.  We also
3656fd34d6fSJeff Roberson 	 * store ZFLAG_BUCKET once we have recursed attempting to allocate
3666fd34d6fSJeff Roberson 	 * a bucket for a bucket zone so we do not allow infinite bucket
3676fd34d6fSJeff Roberson 	 * recursion.  This cookie will even persist to frees of unused
3686fd34d6fSJeff Roberson 	 * buckets via the allocation path or bucket allocations in the
3696fd34d6fSJeff Roberson 	 * free path.
3706fd34d6fSJeff Roberson 	 */
3716fd34d6fSJeff Roberson 	if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
3726fd34d6fSJeff Roberson 		return (NULL);
3736fd34d6fSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
3746fd34d6fSJeff Roberson 		udata = (void *)(uintptr_t)zone->uz_flags;
3756fd34d6fSJeff Roberson 	else
3766fd34d6fSJeff Roberson 		udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
3776fd34d6fSJeff Roberson 	if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY)
378af526374SJeff Roberson 		flags |= M_NOVM;
379af526374SJeff Roberson 	ubz = bucket_zone_lookup(zone->uz_count);
3806fd34d6fSJeff Roberson 	bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
381cae33c14SJeff Roberson 	if (bucket) {
382cae33c14SJeff Roberson #ifdef INVARIANTS
383cae33c14SJeff Roberson 		bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
384cae33c14SJeff Roberson #endif
385cae33c14SJeff Roberson 		bucket->ub_cnt = 0;
386cae33c14SJeff Roberson 		bucket->ub_entries = ubz->ubz_entries;
387cae33c14SJeff Roberson 	}
388cae33c14SJeff Roberson 
389cae33c14SJeff Roberson 	return (bucket);
390cae33c14SJeff Roberson }
391cae33c14SJeff Roberson 
392cae33c14SJeff Roberson static void
3936fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
394cae33c14SJeff Roberson {
395cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
396cae33c14SJeff Roberson 
397fc03d22bSJeff Roberson 	KASSERT(bucket->ub_cnt == 0,
398fc03d22bSJeff Roberson 	    ("bucket_free: Freeing a non free bucket."));
3996fd34d6fSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
4006fd34d6fSJeff Roberson 		udata = (void *)(uintptr_t)zone->uz_flags;
401dc2c7965SRobert Watson 	ubz = bucket_zone_lookup(bucket->ub_entries);
4026fd34d6fSJeff Roberson 	uma_zfree_arg(ubz->ubz_zone, bucket, udata);
403cae33c14SJeff Roberson }
404cae33c14SJeff Roberson 
405cae33c14SJeff Roberson static void
406cae33c14SJeff Roberson bucket_zone_drain(void)
407cae33c14SJeff Roberson {
408cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
409cae33c14SJeff Roberson 
410cae33c14SJeff Roberson 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
411cae33c14SJeff Roberson 		zone_drain(ubz->ubz_zone);
412cae33c14SJeff Roberson }
413cae33c14SJeff Roberson 
4142f891cd5SPawel Jakub Dawidek static void
4152f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone)
4162f891cd5SPawel Jakub Dawidek {
4172f891cd5SPawel Jakub Dawidek 	static const struct timeval warninterval = { 300, 0 };
4182f891cd5SPawel Jakub Dawidek 
4192f891cd5SPawel Jakub Dawidek 	if (!zone_warnings || zone->uz_warning == NULL)
4202f891cd5SPawel Jakub Dawidek 		return;
4212f891cd5SPawel Jakub Dawidek 
4222f891cd5SPawel Jakub Dawidek 	if (ratecheck(&zone->uz_ratecheck, &warninterval))
4232f891cd5SPawel Jakub Dawidek 		printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
4242f891cd5SPawel Jakub Dawidek }
4252f891cd5SPawel Jakub Dawidek 
426e20a199fSJeff Roberson static void
427e20a199fSJeff Roberson zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
428e20a199fSJeff Roberson {
429e20a199fSJeff Roberson 	uma_klink_t klink;
430e20a199fSJeff Roberson 
431e20a199fSJeff Roberson 	LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
432e20a199fSJeff Roberson 		kegfn(klink->kl_keg);
433e20a199fSJeff Roberson }
4348355f576SJeff Roberson 
4358355f576SJeff Roberson /*
4368355f576SJeff Roberson  * Routine called by timeout which is used to fire off some time interval
4379643769aSJeff Roberson  * based calculations.  (stats, hash size, etc.)
4388355f576SJeff Roberson  *
4398355f576SJeff Roberson  * Arguments:
4408355f576SJeff Roberson  *	arg   Unused
4418355f576SJeff Roberson  *
4428355f576SJeff Roberson  * Returns:
4438355f576SJeff Roberson  *	Nothing
4448355f576SJeff Roberson  */
4458355f576SJeff Roberson static void
4468355f576SJeff Roberson uma_timeout(void *unused)
4478355f576SJeff Roberson {
44886bbae32SJeff Roberson 	bucket_enable();
4498355f576SJeff Roberson 	zone_foreach(zone_timeout);
4508355f576SJeff Roberson 
4518355f576SJeff Roberson 	/* Reschedule this event */
4529643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
4538355f576SJeff Roberson }
4548355f576SJeff Roberson 
4558355f576SJeff Roberson /*
4569643769aSJeff Roberson  * Routine to perform timeout driven calculations.  This expands the
4579643769aSJeff Roberson  * hashes and does per cpu statistics aggregation.
4588355f576SJeff Roberson  *
459e20a199fSJeff Roberson  *  Returns nothing.
4608355f576SJeff Roberson  */
4618355f576SJeff Roberson static void
462e20a199fSJeff Roberson keg_timeout(uma_keg_t keg)
4638355f576SJeff Roberson {
4648355f576SJeff Roberson 
465e20a199fSJeff Roberson 	KEG_LOCK(keg);
4668355f576SJeff Roberson 	/*
467e20a199fSJeff Roberson 	 * Expand the keg hash table.
4688355f576SJeff Roberson 	 *
4698355f576SJeff Roberson 	 * This is done if the number of slabs is larger than the hash size.
4708355f576SJeff Roberson 	 * What I'm trying to do here is completely reduce collisions.  This
4718355f576SJeff Roberson 	 * may be a little aggressive.  Should I allow for two collisions max?
4728355f576SJeff Roberson 	 */
473099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_HASH &&
474099a0e58SBosko Milekic 	    keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
4750aef6126SJeff Roberson 		struct uma_hash newhash;
4760aef6126SJeff Roberson 		struct uma_hash oldhash;
4770aef6126SJeff Roberson 		int ret;
4785300d9ddSJeff Roberson 
4790aef6126SJeff Roberson 		/*
4800aef6126SJeff Roberson 		 * This is so involved because allocating and freeing
481e20a199fSJeff Roberson 		 * while the keg lock is held will lead to deadlock.
4820aef6126SJeff Roberson 		 * I have to do everything in stages and check for
4830aef6126SJeff Roberson 		 * races.
4840aef6126SJeff Roberson 		 */
485099a0e58SBosko Milekic 		newhash = keg->uk_hash;
486e20a199fSJeff Roberson 		KEG_UNLOCK(keg);
4870aef6126SJeff Roberson 		ret = hash_alloc(&newhash);
488e20a199fSJeff Roberson 		KEG_LOCK(keg);
4890aef6126SJeff Roberson 		if (ret) {
490099a0e58SBosko Milekic 			if (hash_expand(&keg->uk_hash, &newhash)) {
491099a0e58SBosko Milekic 				oldhash = keg->uk_hash;
492099a0e58SBosko Milekic 				keg->uk_hash = newhash;
4930aef6126SJeff Roberson 			} else
4940aef6126SJeff Roberson 				oldhash = newhash;
4950aef6126SJeff Roberson 
496e20a199fSJeff Roberson 			KEG_UNLOCK(keg);
4970aef6126SJeff Roberson 			hash_free(&oldhash);
498a1dff920SDavide Italiano 			return;
4990aef6126SJeff Roberson 		}
5005300d9ddSJeff Roberson 	}
501e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
502e20a199fSJeff Roberson }
503e20a199fSJeff Roberson 
504e20a199fSJeff Roberson static void
505e20a199fSJeff Roberson zone_timeout(uma_zone_t zone)
506e20a199fSJeff Roberson {
507e20a199fSJeff Roberson 
508e20a199fSJeff Roberson 	zone_foreach_keg(zone, &keg_timeout);
5098355f576SJeff Roberson }
5108355f576SJeff Roberson 
5118355f576SJeff Roberson /*
5125300d9ddSJeff Roberson  * Allocate and zero fill the next sized hash table from the appropriate
5135300d9ddSJeff Roberson  * backing store.
5145300d9ddSJeff Roberson  *
5155300d9ddSJeff Roberson  * Arguments:
5160aef6126SJeff Roberson  *	hash  A new hash structure with the old hash size in uh_hashsize
5175300d9ddSJeff Roberson  *
5185300d9ddSJeff Roberson  * Returns:
5190aef6126SJeff Roberson  *	1 on sucess and 0 on failure.
5205300d9ddSJeff Roberson  */
52137c84183SPoul-Henning Kamp static int
5220aef6126SJeff Roberson hash_alloc(struct uma_hash *hash)
5235300d9ddSJeff Roberson {
5240aef6126SJeff Roberson 	int oldsize;
5255300d9ddSJeff Roberson 	int alloc;
5265300d9ddSJeff Roberson 
5270aef6126SJeff Roberson 	oldsize = hash->uh_hashsize;
5280aef6126SJeff Roberson 
5295300d9ddSJeff Roberson 	/* We're just going to go to a power of two greater */
5300aef6126SJeff Roberson 	if (oldsize)  {
5310aef6126SJeff Roberson 		hash->uh_hashsize = oldsize * 2;
5320aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
5330aef6126SJeff Roberson 		hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
534961647dfSJeff Roberson 		    M_UMAHASH, M_NOWAIT);
5355300d9ddSJeff Roberson 	} else {
5360aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
537e20a199fSJeff Roberson 		hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
538a163d034SWarner Losh 		    M_WAITOK);
5390aef6126SJeff Roberson 		hash->uh_hashsize = UMA_HASH_SIZE_INIT;
5405300d9ddSJeff Roberson 	}
5410aef6126SJeff Roberson 	if (hash->uh_slab_hash) {
5420aef6126SJeff Roberson 		bzero(hash->uh_slab_hash, alloc);
5430aef6126SJeff Roberson 		hash->uh_hashmask = hash->uh_hashsize - 1;
5440aef6126SJeff Roberson 		return (1);
5450aef6126SJeff Roberson 	}
5465300d9ddSJeff Roberson 
5470aef6126SJeff Roberson 	return (0);
5485300d9ddSJeff Roberson }
5495300d9ddSJeff Roberson 
5505300d9ddSJeff Roberson /*
55164f051e9SJeff Roberson  * Expands the hash table for HASH zones.  This is done from zone_timeout
55264f051e9SJeff Roberson  * to reduce collisions.  This must not be done in the regular allocation
55364f051e9SJeff Roberson  * path, otherwise, we can recurse on the vm while allocating pages.
5548355f576SJeff Roberson  *
5558355f576SJeff Roberson  * Arguments:
5560aef6126SJeff Roberson  *	oldhash  The hash you want to expand
5570aef6126SJeff Roberson  *	newhash  The hash structure for the new table
5588355f576SJeff Roberson  *
5598355f576SJeff Roberson  * Returns:
5608355f576SJeff Roberson  *	Nothing
5618355f576SJeff Roberson  *
5628355f576SJeff Roberson  * Discussion:
5638355f576SJeff Roberson  */
5640aef6126SJeff Roberson static int
5650aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
5668355f576SJeff Roberson {
5678355f576SJeff Roberson 	uma_slab_t slab;
5688355f576SJeff Roberson 	int hval;
5698355f576SJeff Roberson 	int i;
5708355f576SJeff Roberson 
5710aef6126SJeff Roberson 	if (!newhash->uh_slab_hash)
5720aef6126SJeff Roberson 		return (0);
5738355f576SJeff Roberson 
5740aef6126SJeff Roberson 	if (oldhash->uh_hashsize >= newhash->uh_hashsize)
5750aef6126SJeff Roberson 		return (0);
5768355f576SJeff Roberson 
5778355f576SJeff Roberson 	/*
5788355f576SJeff Roberson 	 * I need to investigate hash algorithms for resizing without a
5798355f576SJeff Roberson 	 * full rehash.
5808355f576SJeff Roberson 	 */
5818355f576SJeff Roberson 
5820aef6126SJeff Roberson 	for (i = 0; i < oldhash->uh_hashsize; i++)
5830aef6126SJeff Roberson 		while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
5840aef6126SJeff Roberson 			slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
5850aef6126SJeff Roberson 			SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
5860aef6126SJeff Roberson 			hval = UMA_HASH(newhash, slab->us_data);
5870aef6126SJeff Roberson 			SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
5880aef6126SJeff Roberson 			    slab, us_hlink);
5898355f576SJeff Roberson 		}
5908355f576SJeff Roberson 
5910aef6126SJeff Roberson 	return (1);
5929c2cd7e5SJeff Roberson }
5939c2cd7e5SJeff Roberson 
5945300d9ddSJeff Roberson /*
5955300d9ddSJeff Roberson  * Free the hash bucket to the appropriate backing store.
5965300d9ddSJeff Roberson  *
5975300d9ddSJeff Roberson  * Arguments:
5985300d9ddSJeff Roberson  *	slab_hash  The hash bucket we're freeing
5995300d9ddSJeff Roberson  *	hashsize   The number of entries in that hash bucket
6005300d9ddSJeff Roberson  *
6015300d9ddSJeff Roberson  * Returns:
6025300d9ddSJeff Roberson  *	Nothing
6035300d9ddSJeff Roberson  */
6049c2cd7e5SJeff Roberson static void
6050aef6126SJeff Roberson hash_free(struct uma_hash *hash)
6069c2cd7e5SJeff Roberson {
6070aef6126SJeff Roberson 	if (hash->uh_slab_hash == NULL)
6080aef6126SJeff Roberson 		return;
6090aef6126SJeff Roberson 	if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
6100095a784SJeff Roberson 		zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
6118355f576SJeff Roberson 	else
612961647dfSJeff Roberson 		free(hash->uh_slab_hash, M_UMAHASH);
6138355f576SJeff Roberson }
6148355f576SJeff Roberson 
6158355f576SJeff Roberson /*
6168355f576SJeff Roberson  * Frees all outstanding items in a bucket
6178355f576SJeff Roberson  *
6188355f576SJeff Roberson  * Arguments:
6198355f576SJeff Roberson  *	zone   The zone to free to, must be unlocked.
6208355f576SJeff Roberson  *	bucket The free/alloc bucket with items, cpu queue must be locked.
6218355f576SJeff Roberson  *
6228355f576SJeff Roberson  * Returns:
6238355f576SJeff Roberson  *	Nothing
6248355f576SJeff Roberson  */
6258355f576SJeff Roberson 
6268355f576SJeff Roberson static void
6278355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
6288355f576SJeff Roberson {
6290095a784SJeff Roberson 	int i;
6308355f576SJeff Roberson 
6318355f576SJeff Roberson 	if (bucket == NULL)
6328355f576SJeff Roberson 		return;
6338355f576SJeff Roberson 
6340095a784SJeff Roberson 	if (zone->uz_fini)
6350095a784SJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
6360095a784SJeff Roberson 			zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
6370095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
6380095a784SJeff Roberson 	bucket->ub_cnt = 0;
6398355f576SJeff Roberson }
6408355f576SJeff Roberson 
6418355f576SJeff Roberson /*
6428355f576SJeff Roberson  * Drains the per cpu caches for a zone.
6438355f576SJeff Roberson  *
6445d1ae027SRobert Watson  * NOTE: This may only be called while the zone is being turn down, and not
6455d1ae027SRobert Watson  * during normal operation.  This is necessary in order that we do not have
6465d1ae027SRobert Watson  * to migrate CPUs to drain the per-CPU caches.
6475d1ae027SRobert Watson  *
6488355f576SJeff Roberson  * Arguments:
6498355f576SJeff Roberson  *	zone     The zone to drain, must be unlocked.
6508355f576SJeff Roberson  *
6518355f576SJeff Roberson  * Returns:
6528355f576SJeff Roberson  *	Nothing
6538355f576SJeff Roberson  */
6548355f576SJeff Roberson static void
6559643769aSJeff Roberson cache_drain(uma_zone_t zone)
6568355f576SJeff Roberson {
6578355f576SJeff Roberson 	uma_cache_t cache;
6588355f576SJeff Roberson 	int cpu;
6598355f576SJeff Roberson 
6608355f576SJeff Roberson 	/*
6615d1ae027SRobert Watson 	 * XXX: It is safe to not lock the per-CPU caches, because we're
6625d1ae027SRobert Watson 	 * tearing down the zone anyway.  I.e., there will be no further use
6635d1ae027SRobert Watson 	 * of the caches at this point.
6645d1ae027SRobert Watson 	 *
6655d1ae027SRobert Watson 	 * XXX: It would good to be able to assert that the zone is being
6665d1ae027SRobert Watson 	 * torn down to prevent improper use of cache_drain().
6675d1ae027SRobert Watson 	 *
6685d1ae027SRobert Watson 	 * XXX: We lock the zone before passing into bucket_cache_drain() as
6695d1ae027SRobert Watson 	 * it is used elsewhere.  Should the tear-down path be made special
6705d1ae027SRobert Watson 	 * there in some form?
6718355f576SJeff Roberson 	 */
6723aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
6738355f576SJeff Roberson 		cache = &zone->uz_cpu[cpu];
6748355f576SJeff Roberson 		bucket_drain(zone, cache->uc_allocbucket);
6758355f576SJeff Roberson 		bucket_drain(zone, cache->uc_freebucket);
676174ab450SBosko Milekic 		if (cache->uc_allocbucket != NULL)
6776fd34d6fSJeff Roberson 			bucket_free(zone, cache->uc_allocbucket, NULL);
678174ab450SBosko Milekic 		if (cache->uc_freebucket != NULL)
6796fd34d6fSJeff Roberson 			bucket_free(zone, cache->uc_freebucket, NULL);
680d56368d7SBosko Milekic 		cache->uc_allocbucket = cache->uc_freebucket = NULL;
681d56368d7SBosko Milekic 	}
682aaa8bb16SJeff Roberson 	ZONE_LOCK(zone);
683aaa8bb16SJeff Roberson 	bucket_cache_drain(zone);
684aaa8bb16SJeff Roberson 	ZONE_UNLOCK(zone);
685aaa8bb16SJeff Roberson }
686aaa8bb16SJeff Roberson 
687aaa8bb16SJeff Roberson /*
688aaa8bb16SJeff Roberson  * Drain the cached buckets from a zone.  Expects a locked zone on entry.
689aaa8bb16SJeff Roberson  */
690aaa8bb16SJeff Roberson static void
691aaa8bb16SJeff Roberson bucket_cache_drain(uma_zone_t zone)
692aaa8bb16SJeff Roberson {
693aaa8bb16SJeff Roberson 	uma_bucket_t bucket;
6948355f576SJeff Roberson 
6958355f576SJeff Roberson 	/*
6968355f576SJeff Roberson 	 * Drain the bucket queues and free the buckets, we just keep two per
6978355f576SJeff Roberson 	 * cpu (alloc/free).
6988355f576SJeff Roberson 	 */
699fc03d22bSJeff Roberson 	while ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
7008355f576SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
7018355f576SJeff Roberson 		ZONE_UNLOCK(zone);
7028355f576SJeff Roberson 		bucket_drain(zone, bucket);
7036fd34d6fSJeff Roberson 		bucket_free(zone, bucket, NULL);
7048355f576SJeff Roberson 		ZONE_LOCK(zone);
7058355f576SJeff Roberson 	}
706ace66b56SAlexander Motin 
707ace66b56SAlexander Motin 	/*
708ace66b56SAlexander Motin 	 * Shrink further bucket sizes.  Price of single zone lock collision
709ace66b56SAlexander Motin 	 * is probably lower then price of global cache drain.
710ace66b56SAlexander Motin 	 */
711ace66b56SAlexander Motin 	if (zone->uz_count > zone->uz_count_min)
712ace66b56SAlexander Motin 		zone->uz_count--;
7138355f576SJeff Roberson }
714fc03d22bSJeff Roberson 
715fc03d22bSJeff Roberson static void
716fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
717fc03d22bSJeff Roberson {
718fc03d22bSJeff Roberson 	uint8_t *mem;
719fc03d22bSJeff Roberson 	int i;
720fc03d22bSJeff Roberson 	uint8_t flags;
721fc03d22bSJeff Roberson 
722fc03d22bSJeff Roberson 	mem = slab->us_data;
723fc03d22bSJeff Roberson 	flags = slab->us_flags;
724fc03d22bSJeff Roberson 	i = start;
725fc03d22bSJeff Roberson 	if (keg->uk_fini != NULL) {
726fc03d22bSJeff Roberson 		for (i--; i > -1; i--)
727fc03d22bSJeff Roberson 			keg->uk_fini(slab->us_data + (keg->uk_rsize * i),
728fc03d22bSJeff Roberson 			    keg->uk_size);
729fc03d22bSJeff Roberson 	}
730fc03d22bSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_OFFPAGE)
731fc03d22bSJeff Roberson 		zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
732fc03d22bSJeff Roberson #ifdef UMA_DEBUG
733fc03d22bSJeff Roberson 	printf("%s: Returning %d bytes.\n", keg->uk_name,
734fc03d22bSJeff Roberson 	    PAGE_SIZE * keg->uk_ppera);
735fc03d22bSJeff Roberson #endif
736fc03d22bSJeff Roberson 	keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
7378355f576SJeff Roberson }
7388355f576SJeff Roberson 
7398355f576SJeff Roberson /*
740e20a199fSJeff Roberson  * Frees pages from a keg back to the system.  This is done on demand from
7418355f576SJeff Roberson  * the pageout daemon.
7428355f576SJeff Roberson  *
743e20a199fSJeff Roberson  * Returns nothing.
7448355f576SJeff Roberson  */
745e20a199fSJeff Roberson static void
746e20a199fSJeff Roberson keg_drain(uma_keg_t keg)
7478355f576SJeff Roberson {
7481e183df2SStefan Farfeleder 	struct slabhead freeslabs = { 0 };
7498355f576SJeff Roberson 	uma_slab_t slab;
7508355f576SJeff Roberson 	uma_slab_t n;
7518355f576SJeff Roberson 
7528355f576SJeff Roberson 	/*
753e20a199fSJeff Roberson 	 * We don't want to take pages from statically allocated kegs at this
7548355f576SJeff Roberson 	 * time
7558355f576SJeff Roberson 	 */
756099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
7578355f576SJeff Roberson 		return;
7588355f576SJeff Roberson 
7598355f576SJeff Roberson #ifdef UMA_DEBUG
760e20a199fSJeff Roberson 	printf("%s free items: %u\n", keg->uk_name, keg->uk_free);
7618355f576SJeff Roberson #endif
762e20a199fSJeff Roberson 	KEG_LOCK(keg);
763099a0e58SBosko Milekic 	if (keg->uk_free == 0)
7648355f576SJeff Roberson 		goto finished;
7658355f576SJeff Roberson 
766099a0e58SBosko Milekic 	slab = LIST_FIRST(&keg->uk_free_slab);
7679643769aSJeff Roberson 	while (slab) {
7688355f576SJeff Roberson 		n = LIST_NEXT(slab, us_link);
7698355f576SJeff Roberson 
7708355f576SJeff Roberson 		/* We have no where to free these to */
7718355f576SJeff Roberson 		if (slab->us_flags & UMA_SLAB_BOOT) {
7728355f576SJeff Roberson 			slab = n;
7738355f576SJeff Roberson 			continue;
7748355f576SJeff Roberson 		}
7758355f576SJeff Roberson 
7768355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
777099a0e58SBosko Milekic 		keg->uk_pages -= keg->uk_ppera;
778099a0e58SBosko Milekic 		keg->uk_free -= keg->uk_ipers;
779713deb36SJeff Roberson 
780099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZONE_HASH)
781099a0e58SBosko Milekic 			UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
782713deb36SJeff Roberson 
783713deb36SJeff Roberson 		SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
784713deb36SJeff Roberson 
785713deb36SJeff Roberson 		slab = n;
786713deb36SJeff Roberson 	}
787713deb36SJeff Roberson finished:
788e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
789713deb36SJeff Roberson 
790713deb36SJeff Roberson 	while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
791713deb36SJeff Roberson 		SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
7921645995bSKirk McKusick 		keg_free_slab(keg, slab, keg->uk_ipers);
7938355f576SJeff Roberson 	}
7948355f576SJeff Roberson }
7958355f576SJeff Roberson 
796e20a199fSJeff Roberson static void
797e20a199fSJeff Roberson zone_drain_wait(uma_zone_t zone, int waitok)
798e20a199fSJeff Roberson {
799e20a199fSJeff Roberson 
8008355f576SJeff Roberson 	/*
801e20a199fSJeff Roberson 	 * Set draining to interlock with zone_dtor() so we can release our
802e20a199fSJeff Roberson 	 * locks as we go.  Only dtor() should do a WAITOK call since it
803e20a199fSJeff Roberson 	 * is the only call that knows the structure will still be available
804e20a199fSJeff Roberson 	 * when it wakes up.
805e20a199fSJeff Roberson 	 */
806e20a199fSJeff Roberson 	ZONE_LOCK(zone);
807e20a199fSJeff Roberson 	while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
808e20a199fSJeff Roberson 		if (waitok == M_NOWAIT)
809e20a199fSJeff Roberson 			goto out;
810e20a199fSJeff Roberson 		mtx_unlock(&uma_mtx);
811af526374SJeff Roberson 		msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1);
812e20a199fSJeff Roberson 		mtx_lock(&uma_mtx);
813e20a199fSJeff Roberson 	}
814e20a199fSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_DRAINING;
815e20a199fSJeff Roberson 	bucket_cache_drain(zone);
816e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
817e20a199fSJeff Roberson 	/*
818e20a199fSJeff Roberson 	 * The DRAINING flag protects us from being freed while
819e20a199fSJeff Roberson 	 * we're running.  Normally the uma_mtx would protect us but we
820e20a199fSJeff Roberson 	 * must be able to release and acquire the right lock for each keg.
821e20a199fSJeff Roberson 	 */
822e20a199fSJeff Roberson 	zone_foreach_keg(zone, &keg_drain);
823e20a199fSJeff Roberson 	ZONE_LOCK(zone);
824e20a199fSJeff Roberson 	zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
825e20a199fSJeff Roberson 	wakeup(zone);
826e20a199fSJeff Roberson out:
827e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
828e20a199fSJeff Roberson }
829e20a199fSJeff Roberson 
830e20a199fSJeff Roberson void
831e20a199fSJeff Roberson zone_drain(uma_zone_t zone)
832e20a199fSJeff Roberson {
833e20a199fSJeff Roberson 
834e20a199fSJeff Roberson 	zone_drain_wait(zone, M_NOWAIT);
835e20a199fSJeff Roberson }
836e20a199fSJeff Roberson 
837e20a199fSJeff Roberson /*
838e20a199fSJeff Roberson  * Allocate a new slab for a keg.  This does not insert the slab onto a list.
8398355f576SJeff Roberson  *
8408355f576SJeff Roberson  * Arguments:
8418355f576SJeff Roberson  *	wait  Shall we wait?
8428355f576SJeff Roberson  *
8438355f576SJeff Roberson  * Returns:
8448355f576SJeff Roberson  *	The slab that was allocated or NULL if there is no memory and the
8458355f576SJeff Roberson  *	caller specified M_NOWAIT.
8468355f576SJeff Roberson  */
8478355f576SJeff Roberson static uma_slab_t
848e20a199fSJeff Roberson keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait)
8498355f576SJeff Roberson {
850099a0e58SBosko Milekic 	uma_slabrefcnt_t slabref;
851e20a199fSJeff Roberson 	uma_alloc allocf;
852099a0e58SBosko Milekic 	uma_slab_t slab;
85385dcf349SGleb Smirnoff 	uint8_t *mem;
85485dcf349SGleb Smirnoff 	uint8_t flags;
8558355f576SJeff Roberson 	int i;
8568355f576SJeff Roberson 
857e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
858a553d4b8SJeff Roberson 	slab = NULL;
859fc03d22bSJeff Roberson 	mem = NULL;
860a553d4b8SJeff Roberson 
8618355f576SJeff Roberson #ifdef UMA_DEBUG
8620095a784SJeff Roberson 	printf("alloc_slab:  Allocating a new slab for %s\n", keg->uk_name);
8638355f576SJeff Roberson #endif
864e20a199fSJeff Roberson 	allocf = keg->uk_allocf;
865e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
866a553d4b8SJeff Roberson 
867099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
868e20a199fSJeff Roberson 		slab = zone_alloc_item(keg->uk_slabzone, NULL, wait);
869fc03d22bSJeff Roberson 		if (slab == NULL)
870fc03d22bSJeff Roberson 			goto out;
871a553d4b8SJeff Roberson 	}
872a553d4b8SJeff Roberson 
8733370c5bfSJeff Roberson 	/*
8743370c5bfSJeff Roberson 	 * This reproduces the old vm_zone behavior of zero filling pages the
8753370c5bfSJeff Roberson 	 * first time they are added to a zone.
8763370c5bfSJeff Roberson 	 *
8773370c5bfSJeff Roberson 	 * Malloced items are zeroed in uma_zalloc.
8783370c5bfSJeff Roberson 	 */
8793370c5bfSJeff Roberson 
880099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
8813370c5bfSJeff Roberson 		wait |= M_ZERO;
8823370c5bfSJeff Roberson 	else
8833370c5bfSJeff Roberson 		wait &= ~M_ZERO;
8843370c5bfSJeff Roberson 
885263811f7SKip Macy 	if (keg->uk_flags & UMA_ZONE_NODUMP)
886263811f7SKip Macy 		wait |= M_NODUMP;
887263811f7SKip Macy 
888e20a199fSJeff Roberson 	/* zone is passed for legacy reasons. */
889ad97af7eSGleb Smirnoff 	mem = allocf(zone, keg->uk_ppera * PAGE_SIZE, &flags, wait);
890a553d4b8SJeff Roberson 	if (mem == NULL) {
891b23f72e9SBrian Feldman 		if (keg->uk_flags & UMA_ZONE_OFFPAGE)
8920095a784SJeff Roberson 			zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
893fc03d22bSJeff Roberson 		slab = NULL;
894fc03d22bSJeff Roberson 		goto out;
895a553d4b8SJeff Roberson 	}
8968355f576SJeff Roberson 
8975c0e403bSJeff Roberson 	/* Point the slab into the allocated memory */
898099a0e58SBosko Milekic 	if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
899099a0e58SBosko Milekic 		slab = (uma_slab_t )(mem + keg->uk_pgoff);
9005c0e403bSJeff Roberson 
901e20a199fSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_VTOSLAB)
902099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ppera; i++)
90399571dc3SJeff Roberson 			vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
9048355f576SJeff Roberson 
905099a0e58SBosko Milekic 	slab->us_keg = keg;
9068355f576SJeff Roberson 	slab->us_data = mem;
907099a0e58SBosko Milekic 	slab->us_freecount = keg->uk_ipers;
9088355f576SJeff Roberson 	slab->us_flags = flags;
909ef72505eSJeff Roberson 	BIT_FILL(SLAB_SETSIZE, &slab->us_free);
910ef72505eSJeff Roberson #ifdef INVARIANTS
911ef72505eSJeff Roberson 	BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree);
912ef72505eSJeff Roberson #endif
913099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_REFCNT) {
914099a0e58SBosko Milekic 		slabref = (uma_slabrefcnt_t)slab;
915ab14a3f7SBrian Feldman 		for (i = 0; i < keg->uk_ipers; i++)
916ef72505eSJeff Roberson 			slabref->us_refcnt[i] = 0;
917099a0e58SBosko Milekic 	}
918099a0e58SBosko Milekic 
919b23f72e9SBrian Feldman 	if (keg->uk_init != NULL) {
920099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ipers; i++)
921b23f72e9SBrian Feldman 			if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
922b23f72e9SBrian Feldman 			    keg->uk_size, wait) != 0)
923b23f72e9SBrian Feldman 				break;
924b23f72e9SBrian Feldman 		if (i != keg->uk_ipers) {
925fc03d22bSJeff Roberson 			keg_free_slab(keg, slab, i);
926fc03d22bSJeff Roberson 			slab = NULL;
927fc03d22bSJeff Roberson 			goto out;
928b23f72e9SBrian Feldman 		}
929b23f72e9SBrian Feldman 	}
930fc03d22bSJeff Roberson out:
931e20a199fSJeff Roberson 	KEG_LOCK(keg);
9325c0e403bSJeff Roberson 
933fc03d22bSJeff Roberson 	if (slab != NULL) {
934099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZONE_HASH)
935099a0e58SBosko Milekic 			UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
9368355f576SJeff Roberson 
937099a0e58SBosko Milekic 		keg->uk_pages += keg->uk_ppera;
938099a0e58SBosko Milekic 		keg->uk_free += keg->uk_ipers;
939fc03d22bSJeff Roberson 	}
9408355f576SJeff Roberson 
9418355f576SJeff Roberson 	return (slab);
9428355f576SJeff Roberson }
9438355f576SJeff Roberson 
9448355f576SJeff Roberson /*
945009b6fcbSJeff Roberson  * This function is intended to be used early on in place of page_alloc() so
946009b6fcbSJeff Roberson  * that we may use the boot time page cache to satisfy allocations before
947009b6fcbSJeff Roberson  * the VM is ready.
948009b6fcbSJeff Roberson  */
949009b6fcbSJeff Roberson static void *
95085dcf349SGleb Smirnoff startup_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait)
951009b6fcbSJeff Roberson {
952099a0e58SBosko Milekic 	uma_keg_t keg;
953f353d338SAlan Cox 	uma_slab_t tmps;
954e9a069d8SJohn Baldwin 	int pages, check_pages;
955099a0e58SBosko Milekic 
956e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
957e9a069d8SJohn Baldwin 	pages = howmany(bytes, PAGE_SIZE);
958e9a069d8SJohn Baldwin 	check_pages = pages - 1;
959e9a069d8SJohn Baldwin 	KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n"));
960099a0e58SBosko Milekic 
961009b6fcbSJeff Roberson 	/*
962009b6fcbSJeff Roberson 	 * Check our small startup cache to see if it has pages remaining.
963009b6fcbSJeff Roberson 	 */
964f353d338SAlan Cox 	mtx_lock(&uma_boot_pages_mtx);
965e9a069d8SJohn Baldwin 
966e9a069d8SJohn Baldwin 	/* First check if we have enough room. */
967e9a069d8SJohn Baldwin 	tmps = LIST_FIRST(&uma_boot_pages);
968e9a069d8SJohn Baldwin 	while (tmps != NULL && check_pages-- > 0)
969e9a069d8SJohn Baldwin 		tmps = LIST_NEXT(tmps, us_link);
970e9a069d8SJohn Baldwin 	if (tmps != NULL) {
971e9a069d8SJohn Baldwin 		/*
972e9a069d8SJohn Baldwin 		 * It's ok to lose tmps references.  The last one will
973e9a069d8SJohn Baldwin 		 * have tmps->us_data pointing to the start address of
974e9a069d8SJohn Baldwin 		 * "pages" contiguous pages of memory.
975e9a069d8SJohn Baldwin 		 */
976e9a069d8SJohn Baldwin 		while (pages-- > 0) {
977e9a069d8SJohn Baldwin 			tmps = LIST_FIRST(&uma_boot_pages);
978009b6fcbSJeff Roberson 			LIST_REMOVE(tmps, us_link);
979e9a069d8SJohn Baldwin 		}
980f353d338SAlan Cox 		mtx_unlock(&uma_boot_pages_mtx);
981009b6fcbSJeff Roberson 		*pflag = tmps->us_flags;
982009b6fcbSJeff Roberson 		return (tmps->us_data);
983009b6fcbSJeff Roberson 	}
984f353d338SAlan Cox 	mtx_unlock(&uma_boot_pages_mtx);
985342f1793SAlan Cox 	if (booted < UMA_STARTUP2)
9863803b26bSDag-Erling Smørgrav 		panic("UMA: Increase vm.boot_pages");
987009b6fcbSJeff Roberson 	/*
988009b6fcbSJeff Roberson 	 * Now that we've booted reset these users to their real allocator.
989009b6fcbSJeff Roberson 	 */
990009b6fcbSJeff Roberson #ifdef UMA_MD_SMALL_ALLOC
991e9a069d8SJohn Baldwin 	keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc;
992009b6fcbSJeff Roberson #else
993099a0e58SBosko Milekic 	keg->uk_allocf = page_alloc;
994009b6fcbSJeff Roberson #endif
995099a0e58SBosko Milekic 	return keg->uk_allocf(zone, bytes, pflag, wait);
996009b6fcbSJeff Roberson }
997009b6fcbSJeff Roberson 
998009b6fcbSJeff Roberson /*
9998355f576SJeff Roberson  * Allocates a number of pages from the system
10008355f576SJeff Roberson  *
10018355f576SJeff Roberson  * Arguments:
10028355f576SJeff Roberson  *	bytes  The number of bytes requested
10038355f576SJeff Roberson  *	wait  Shall we wait?
10048355f576SJeff Roberson  *
10058355f576SJeff Roberson  * Returns:
10068355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
10078355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
10088355f576SJeff Roberson  */
10098355f576SJeff Roberson static void *
101085dcf349SGleb Smirnoff page_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait)
10118355f576SJeff Roberson {
10128355f576SJeff Roberson 	void *p;	/* Returned page */
10138355f576SJeff Roberson 
10148355f576SJeff Roberson 	*pflag = UMA_SLAB_KMEM;
10155df87b21SJeff Roberson 	p = (void *) kmem_malloc(kmem_arena, bytes, wait);
10168355f576SJeff Roberson 
10178355f576SJeff Roberson 	return (p);
10188355f576SJeff Roberson }
10198355f576SJeff Roberson 
10208355f576SJeff Roberson /*
10218355f576SJeff Roberson  * Allocates a number of pages from within an object
10228355f576SJeff Roberson  *
10238355f576SJeff Roberson  * Arguments:
10248355f576SJeff Roberson  *	bytes  The number of bytes requested
10258355f576SJeff Roberson  *	wait   Shall we wait?
10268355f576SJeff Roberson  *
10278355f576SJeff Roberson  * Returns:
10288355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
10298355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
10308355f576SJeff Roberson  */
10318355f576SJeff Roberson static void *
103285dcf349SGleb Smirnoff noobj_alloc(uma_zone_t zone, int bytes, uint8_t *flags, int wait)
10338355f576SJeff Roberson {
1034a4915c21SAttilio Rao 	TAILQ_HEAD(, vm_page) alloctail;
1035a4915c21SAttilio Rao 	u_long npages;
1036b245ac95SAlan Cox 	vm_offset_t retkva, zkva;
1037a4915c21SAttilio Rao 	vm_page_t p, p_next;
1038e20a199fSJeff Roberson 	uma_keg_t keg;
10398355f576SJeff Roberson 
1040a4915c21SAttilio Rao 	TAILQ_INIT(&alloctail);
1041e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
1042a4915c21SAttilio Rao 
1043a4915c21SAttilio Rao 	npages = howmany(bytes, PAGE_SIZE);
1044a4915c21SAttilio Rao 	while (npages > 0) {
1045a4915c21SAttilio Rao 		p = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT |
1046a4915c21SAttilio Rao 		    VM_ALLOC_WIRED | VM_ALLOC_NOOBJ);
1047a4915c21SAttilio Rao 		if (p != NULL) {
1048a4915c21SAttilio Rao 			/*
1049a4915c21SAttilio Rao 			 * Since the page does not belong to an object, its
1050a4915c21SAttilio Rao 			 * listq is unused.
1051a4915c21SAttilio Rao 			 */
1052a4915c21SAttilio Rao 			TAILQ_INSERT_TAIL(&alloctail, p, listq);
1053a4915c21SAttilio Rao 			npages--;
1054a4915c21SAttilio Rao 			continue;
1055a4915c21SAttilio Rao 		}
1056a4915c21SAttilio Rao 		if (wait & M_WAITOK) {
1057a4915c21SAttilio Rao 			VM_WAIT;
1058a4915c21SAttilio Rao 			continue;
1059a4915c21SAttilio Rao 		}
10608355f576SJeff Roberson 
10618355f576SJeff Roberson 		/*
1062a4915c21SAttilio Rao 		 * Page allocation failed, free intermediate pages and
1063a4915c21SAttilio Rao 		 * exit.
10648355f576SJeff Roberson 		 */
1065a4915c21SAttilio Rao 		TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1066b245ac95SAlan Cox 			vm_page_unwire(p, 0);
1067b245ac95SAlan Cox 			vm_page_free(p);
1068b245ac95SAlan Cox 		}
1069a4915c21SAttilio Rao 		return (NULL);
1070b245ac95SAlan Cox 	}
10718355f576SJeff Roberson 	*flags = UMA_SLAB_PRIV;
1072a4915c21SAttilio Rao 	zkva = keg->uk_kva +
1073a4915c21SAttilio Rao 	    atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1074a4915c21SAttilio Rao 	retkva = zkva;
1075a4915c21SAttilio Rao 	TAILQ_FOREACH(p, &alloctail, listq) {
1076a4915c21SAttilio Rao 		pmap_qenter(zkva, &p, 1);
1077a4915c21SAttilio Rao 		zkva += PAGE_SIZE;
1078a4915c21SAttilio Rao 	}
10798355f576SJeff Roberson 
10808355f576SJeff Roberson 	return ((void *)retkva);
10818355f576SJeff Roberson }
10828355f576SJeff Roberson 
10838355f576SJeff Roberson /*
10848355f576SJeff Roberson  * Frees a number of pages to the system
10858355f576SJeff Roberson  *
10868355f576SJeff Roberson  * Arguments:
10878355f576SJeff Roberson  *	mem   A pointer to the memory to be freed
10888355f576SJeff Roberson  *	size  The size of the memory being freed
10898355f576SJeff Roberson  *	flags The original p->us_flags field
10908355f576SJeff Roberson  *
10918355f576SJeff Roberson  * Returns:
10928355f576SJeff Roberson  *	Nothing
10938355f576SJeff Roberson  */
10948355f576SJeff Roberson static void
109585dcf349SGleb Smirnoff page_free(void *mem, int size, uint8_t flags)
10968355f576SJeff Roberson {
10975df87b21SJeff Roberson 	struct vmem *vmem;
10983370c5bfSJeff Roberson 
10998355f576SJeff Roberson 	if (flags & UMA_SLAB_KMEM)
11005df87b21SJeff Roberson 		vmem = kmem_arena;
1101aea6e893SAlan Cox 	else if (flags & UMA_SLAB_KERNEL)
11025df87b21SJeff Roberson 		vmem = kernel_arena;
11038355f576SJeff Roberson 	else
1104aea6e893SAlan Cox 		panic("UMA: page_free used with invalid flags %d", flags);
11058355f576SJeff Roberson 
11065df87b21SJeff Roberson 	kmem_free(vmem, (vm_offset_t)mem, size);
11078355f576SJeff Roberson }
11088355f576SJeff Roberson 
11098355f576SJeff Roberson /*
11108355f576SJeff Roberson  * Zero fill initializer
11118355f576SJeff Roberson  *
11128355f576SJeff Roberson  * Arguments/Returns follow uma_init specifications
11138355f576SJeff Roberson  */
1114b23f72e9SBrian Feldman static int
1115b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags)
11168355f576SJeff Roberson {
11178355f576SJeff Roberson 	bzero(mem, size);
1118b23f72e9SBrian Feldman 	return (0);
11198355f576SJeff Roberson }
11208355f576SJeff Roberson 
11218355f576SJeff Roberson /*
1122e20a199fSJeff Roberson  * Finish creating a small uma keg.  This calculates ipers, and the keg size.
11238355f576SJeff Roberson  *
11248355f576SJeff Roberson  * Arguments
1125e20a199fSJeff Roberson  *	keg  The zone we should initialize
11268355f576SJeff Roberson  *
11278355f576SJeff Roberson  * Returns
11288355f576SJeff Roberson  *	Nothing
11298355f576SJeff Roberson  */
11308355f576SJeff Roberson static void
1131e20a199fSJeff Roberson keg_small_init(uma_keg_t keg)
11328355f576SJeff Roberson {
1133244f4554SBosko Milekic 	u_int rsize;
1134244f4554SBosko Milekic 	u_int memused;
1135244f4554SBosko Milekic 	u_int wastedspace;
1136244f4554SBosko Milekic 	u_int shsize;
11378355f576SJeff Roberson 
1138ad97af7eSGleb Smirnoff 	if (keg->uk_flags & UMA_ZONE_PCPU) {
1139e28a647dSGleb Smirnoff 		u_int ncpus = mp_ncpus ? mp_ncpus : MAXCPU;
1140e28a647dSGleb Smirnoff 
1141ad97af7eSGleb Smirnoff 		keg->uk_slabsize = sizeof(struct pcpu);
1142e28a647dSGleb Smirnoff 		keg->uk_ppera = howmany(ncpus * sizeof(struct pcpu),
1143ad97af7eSGleb Smirnoff 		    PAGE_SIZE);
1144ad97af7eSGleb Smirnoff 	} else {
1145ad97af7eSGleb Smirnoff 		keg->uk_slabsize = UMA_SLAB_SIZE;
1146ad97af7eSGleb Smirnoff 		keg->uk_ppera = 1;
1147ad97af7eSGleb Smirnoff 	}
1148ad97af7eSGleb Smirnoff 
1149ef72505eSJeff Roberson 	/*
1150ef72505eSJeff Roberson 	 * Calculate the size of each allocation (rsize) according to
1151ef72505eSJeff Roberson 	 * alignment.  If the requested size is smaller than we have
1152ef72505eSJeff Roberson 	 * allocation bits for we round it up.
1153ef72505eSJeff Roberson 	 */
1154099a0e58SBosko Milekic 	rsize = keg->uk_size;
1155ef72505eSJeff Roberson 	if (rsize < keg->uk_slabsize / SLAB_SETSIZE)
1156ef72505eSJeff Roberson 		rsize = keg->uk_slabsize / SLAB_SETSIZE;
1157099a0e58SBosko Milekic 	if (rsize & keg->uk_align)
1158099a0e58SBosko Milekic 		rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1159099a0e58SBosko Milekic 	keg->uk_rsize = rsize;
1160ad97af7eSGleb Smirnoff 
1161ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
1162ad97af7eSGleb Smirnoff 	    keg->uk_rsize < sizeof(struct pcpu),
1163ad97af7eSGleb Smirnoff 	    ("%s: size %u too large", __func__, keg->uk_rsize));
11648355f576SJeff Roberson 
1165ef72505eSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_REFCNT)
1166ef72505eSJeff Roberson 		rsize += sizeof(uint32_t);
1167ef72505eSJeff Roberson 
1168ef72505eSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_OFFPAGE)
11692864dbbfSGleb Smirnoff 		shsize = 0;
1170ef72505eSJeff Roberson 	else
1171244f4554SBosko Milekic 		shsize = sizeof(struct uma_slab);
11728355f576SJeff Roberson 
1173ad97af7eSGleb Smirnoff 	keg->uk_ipers = (keg->uk_slabsize - shsize) / rsize;
1174ef72505eSJeff Roberson 	KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1175ad97af7eSGleb Smirnoff 	    ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1176ad97af7eSGleb Smirnoff 
1177244f4554SBosko Milekic 	memused = keg->uk_ipers * rsize + shsize;
1178ad97af7eSGleb Smirnoff 	wastedspace = keg->uk_slabsize - memused;
1179244f4554SBosko Milekic 
118020e8e865SBosko Milekic 	/*
1181244f4554SBosko Milekic 	 * We can't do OFFPAGE if we're internal or if we've been
118220e8e865SBosko Milekic 	 * asked to not go to the VM for buckets.  If we do this we
11836fd34d6fSJeff Roberson 	 * may end up going to the VM  for slabs which we do not
11846fd34d6fSJeff Roberson 	 * want to do if we're UMA_ZFLAG_CACHEONLY as a result
11856fd34d6fSJeff Roberson 	 * of UMA_ZONE_VM, which clearly forbids it.
118620e8e865SBosko Milekic 	 */
1187099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1188099a0e58SBosko Milekic 	    (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
11898355f576SJeff Roberson 		return;
1190244f4554SBosko Milekic 
1191ef72505eSJeff Roberson 	/*
1192ef72505eSJeff Roberson 	 * See if using an OFFPAGE slab will limit our waste.  Only do
1193ef72505eSJeff Roberson 	 * this if it permits more items per-slab.
1194ef72505eSJeff Roberson 	 *
1195ef72505eSJeff Roberson 	 * XXX We could try growing slabsize to limit max waste as well.
1196ef72505eSJeff Roberson 	 * Historically this was not done because the VM could not
1197ef72505eSJeff Roberson 	 * efficiently handle contiguous allocations.
1198ef72505eSJeff Roberson 	 */
1199ad97af7eSGleb Smirnoff 	if ((wastedspace >= keg->uk_slabsize / UMA_MAX_WASTE) &&
1200ad97af7eSGleb Smirnoff 	    (keg->uk_ipers < (keg->uk_slabsize / keg->uk_rsize))) {
1201ad97af7eSGleb Smirnoff 		keg->uk_ipers = keg->uk_slabsize / keg->uk_rsize;
1202ef72505eSJeff Roberson 		KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1203ad97af7eSGleb Smirnoff 		    ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1204244f4554SBosko Milekic #ifdef UMA_DEBUG
1205244f4554SBosko Milekic 		printf("UMA decided we need offpage slab headers for "
1206e20a199fSJeff Roberson 		    "keg: %s, calculated wastedspace = %d, "
1207244f4554SBosko Milekic 		    "maximum wasted space allowed = %d, "
1208244f4554SBosko Milekic 		    "calculated ipers = %d, "
1209e20a199fSJeff Roberson 		    "new wasted space = %d\n", keg->uk_name, wastedspace,
1210ad97af7eSGleb Smirnoff 		    keg->uk_slabsize / UMA_MAX_WASTE, keg->uk_ipers,
1211ad97af7eSGleb Smirnoff 		    keg->uk_slabsize - keg->uk_ipers * keg->uk_rsize);
1212244f4554SBosko Milekic #endif
1213099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZONE_OFFPAGE;
12148355f576SJeff Roberson 	}
1215ad97af7eSGleb Smirnoff 
1216ad97af7eSGleb Smirnoff 	if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1217ad97af7eSGleb Smirnoff 	    (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1218ad97af7eSGleb Smirnoff 		keg->uk_flags |= UMA_ZONE_HASH;
12198355f576SJeff Roberson }
12208355f576SJeff Roberson 
12218355f576SJeff Roberson /*
1222e20a199fSJeff Roberson  * Finish creating a large (> UMA_SLAB_SIZE) uma kegs.  Just give in and do
12238355f576SJeff Roberson  * OFFPAGE for now.  When I can allow for more dynamic slab sizes this will be
12248355f576SJeff Roberson  * more complicated.
12258355f576SJeff Roberson  *
12268355f576SJeff Roberson  * Arguments
1227e20a199fSJeff Roberson  *	keg  The keg we should initialize
12288355f576SJeff Roberson  *
12298355f576SJeff Roberson  * Returns
12308355f576SJeff Roberson  *	Nothing
12318355f576SJeff Roberson  */
12328355f576SJeff Roberson static void
1233e20a199fSJeff Roberson keg_large_init(uma_keg_t keg)
12348355f576SJeff Roberson {
12358355f576SJeff Roberson 
1236e20a199fSJeff Roberson 	KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1237099a0e58SBosko Milekic 	KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1238e20a199fSJeff Roberson 	    ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1239ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1240ad97af7eSGleb Smirnoff 	    ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__));
124120e8e865SBosko Milekic 
1242ad97af7eSGleb Smirnoff 	keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE);
1243ad97af7eSGleb Smirnoff 	keg->uk_slabsize = keg->uk_ppera * PAGE_SIZE;
1244099a0e58SBosko Milekic 	keg->uk_ipers = 1;
1245e9a069d8SJohn Baldwin 	keg->uk_rsize = keg->uk_size;
1246e9a069d8SJohn Baldwin 
1247e9a069d8SJohn Baldwin 	/* We can't do OFFPAGE if we're internal, bail out here. */
1248e9a069d8SJohn Baldwin 	if (keg->uk_flags & UMA_ZFLAG_INTERNAL)
1249e9a069d8SJohn Baldwin 		return;
12508355f576SJeff Roberson 
1251099a0e58SBosko Milekic 	keg->uk_flags |= UMA_ZONE_OFFPAGE;
1252e20a199fSJeff Roberson 	if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1253099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZONE_HASH;
12548355f576SJeff Roberson }
12558355f576SJeff Roberson 
1256e20a199fSJeff Roberson static void
1257e20a199fSJeff Roberson keg_cachespread_init(uma_keg_t keg)
1258e20a199fSJeff Roberson {
1259e20a199fSJeff Roberson 	int alignsize;
1260e20a199fSJeff Roberson 	int trailer;
1261e20a199fSJeff Roberson 	int pages;
1262e20a199fSJeff Roberson 	int rsize;
1263e20a199fSJeff Roberson 
1264ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1265ad97af7eSGleb Smirnoff 	    ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__));
1266ad97af7eSGleb Smirnoff 
1267e20a199fSJeff Roberson 	alignsize = keg->uk_align + 1;
1268e20a199fSJeff Roberson 	rsize = keg->uk_size;
1269e20a199fSJeff Roberson 	/*
1270e20a199fSJeff Roberson 	 * We want one item to start on every align boundary in a page.  To
1271e20a199fSJeff Roberson 	 * do this we will span pages.  We will also extend the item by the
1272e20a199fSJeff Roberson 	 * size of align if it is an even multiple of align.  Otherwise, it
1273e20a199fSJeff Roberson 	 * would fall on the same boundary every time.
1274e20a199fSJeff Roberson 	 */
1275e20a199fSJeff Roberson 	if (rsize & keg->uk_align)
1276e20a199fSJeff Roberson 		rsize = (rsize & ~keg->uk_align) + alignsize;
1277e20a199fSJeff Roberson 	if ((rsize & alignsize) == 0)
1278e20a199fSJeff Roberson 		rsize += alignsize;
1279e20a199fSJeff Roberson 	trailer = rsize - keg->uk_size;
1280e20a199fSJeff Roberson 	pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1281e20a199fSJeff Roberson 	pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1282e20a199fSJeff Roberson 	keg->uk_rsize = rsize;
1283e20a199fSJeff Roberson 	keg->uk_ppera = pages;
1284ad97af7eSGleb Smirnoff 	keg->uk_slabsize = UMA_SLAB_SIZE;
1285e20a199fSJeff Roberson 	keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1286e20a199fSJeff Roberson 	keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1287e20a199fSJeff Roberson 	KASSERT(keg->uk_ipers <= uma_max_ipers,
128842321809SGleb Smirnoff 	    ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1289e20a199fSJeff Roberson 	    keg->uk_ipers));
1290e20a199fSJeff Roberson }
1291e20a199fSJeff Roberson 
12928355f576SJeff Roberson /*
1293099a0e58SBosko Milekic  * Keg header ctor.  This initializes all fields, locks, etc.  And inserts
1294099a0e58SBosko Milekic  * the keg onto the global keg list.
12958355f576SJeff Roberson  *
12968355f576SJeff Roberson  * Arguments/Returns follow uma_ctor specifications
1297099a0e58SBosko Milekic  *	udata  Actually uma_kctor_args
1298099a0e58SBosko Milekic  */
1299b23f72e9SBrian Feldman static int
1300b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags)
1301099a0e58SBosko Milekic {
1302099a0e58SBosko Milekic 	struct uma_kctor_args *arg = udata;
1303099a0e58SBosko Milekic 	uma_keg_t keg = mem;
1304099a0e58SBosko Milekic 	uma_zone_t zone;
1305099a0e58SBosko Milekic 
1306099a0e58SBosko Milekic 	bzero(keg, size);
1307099a0e58SBosko Milekic 	keg->uk_size = arg->size;
1308099a0e58SBosko Milekic 	keg->uk_init = arg->uminit;
1309099a0e58SBosko Milekic 	keg->uk_fini = arg->fini;
1310099a0e58SBosko Milekic 	keg->uk_align = arg->align;
1311099a0e58SBosko Milekic 	keg->uk_free = 0;
13126fd34d6fSJeff Roberson 	keg->uk_reserve = 0;
1313099a0e58SBosko Milekic 	keg->uk_pages = 0;
1314099a0e58SBosko Milekic 	keg->uk_flags = arg->flags;
1315099a0e58SBosko Milekic 	keg->uk_allocf = page_alloc;
1316099a0e58SBosko Milekic 	keg->uk_freef = page_free;
1317099a0e58SBosko Milekic 	keg->uk_slabzone = NULL;
1318099a0e58SBosko Milekic 
1319099a0e58SBosko Milekic 	/*
1320099a0e58SBosko Milekic 	 * The master zone is passed to us at keg-creation time.
1321099a0e58SBosko Milekic 	 */
1322099a0e58SBosko Milekic 	zone = arg->zone;
1323e20a199fSJeff Roberson 	keg->uk_name = zone->uz_name;
1324099a0e58SBosko Milekic 
1325099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_VM)
1326099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1327099a0e58SBosko Milekic 
1328099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_ZINIT)
1329099a0e58SBosko Milekic 		keg->uk_init = zero_init;
1330099a0e58SBosko Milekic 
1331e20a199fSJeff Roberson 	if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC)
1332e20a199fSJeff Roberson 		keg->uk_flags |= UMA_ZONE_VTOSLAB;
1333e20a199fSJeff Roberson 
1334ad97af7eSGleb Smirnoff 	if (arg->flags & UMA_ZONE_PCPU)
1335ad97af7eSGleb Smirnoff #ifdef SMP
1336ad97af7eSGleb Smirnoff 		keg->uk_flags |= UMA_ZONE_OFFPAGE;
1337ad97af7eSGleb Smirnoff #else
1338ad97af7eSGleb Smirnoff 		keg->uk_flags &= ~UMA_ZONE_PCPU;
1339ad97af7eSGleb Smirnoff #endif
1340ad97af7eSGleb Smirnoff 
1341ef72505eSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_CACHESPREAD) {
1342e20a199fSJeff Roberson 		keg_cachespread_init(keg);
1343ef72505eSJeff Roberson 	} else if (keg->uk_flags & UMA_ZONE_REFCNT) {
1344ef72505eSJeff Roberson 		if (keg->uk_size >
1345ef72505eSJeff Roberson 		    (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) -
1346ef72505eSJeff Roberson 		    sizeof(uint32_t)))
1347e20a199fSJeff Roberson 			keg_large_init(keg);
1348099a0e58SBosko Milekic 		else
1349e20a199fSJeff Roberson 			keg_small_init(keg);
1350244f4554SBosko Milekic 	} else {
1351ef72505eSJeff Roberson 		if (keg->uk_size > (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1352e20a199fSJeff Roberson 			keg_large_init(keg);
1353244f4554SBosko Milekic 		else
1354e20a199fSJeff Roberson 			keg_small_init(keg);
1355244f4554SBosko Milekic 	}
1356099a0e58SBosko Milekic 
1357244f4554SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1358ef72505eSJeff Roberson 		if (keg->uk_flags & UMA_ZONE_REFCNT) {
1359ef72505eSJeff Roberson 			if (keg->uk_ipers > uma_max_ipers_ref)
1360ef72505eSJeff Roberson 				panic("Too many ref items per zone: %d > %d\n",
1361ef72505eSJeff Roberson 				    keg->uk_ipers, uma_max_ipers_ref);
1362099a0e58SBosko Milekic 			keg->uk_slabzone = slabrefzone;
1363ef72505eSJeff Roberson 		} else
1364099a0e58SBosko Milekic 			keg->uk_slabzone = slabzone;
1365244f4554SBosko Milekic 	}
1366099a0e58SBosko Milekic 
1367099a0e58SBosko Milekic 	/*
1368099a0e58SBosko Milekic 	 * If we haven't booted yet we need allocations to go through the
1369099a0e58SBosko Milekic 	 * startup cache until the vm is ready.
1370099a0e58SBosko Milekic 	 */
1371099a0e58SBosko Milekic 	if (keg->uk_ppera == 1) {
1372099a0e58SBosko Milekic #ifdef UMA_MD_SMALL_ALLOC
1373099a0e58SBosko Milekic 		keg->uk_allocf = uma_small_alloc;
1374099a0e58SBosko Milekic 		keg->uk_freef = uma_small_free;
13758cd02d00SAlan Cox 
1376342f1793SAlan Cox 		if (booted < UMA_STARTUP)
1377099a0e58SBosko Milekic 			keg->uk_allocf = startup_alloc;
13788cd02d00SAlan Cox #else
13798cd02d00SAlan Cox 		if (booted < UMA_STARTUP2)
13808cd02d00SAlan Cox 			keg->uk_allocf = startup_alloc;
13818cd02d00SAlan Cox #endif
1382342f1793SAlan Cox 	} else if (booted < UMA_STARTUP2 &&
1383342f1793SAlan Cox 	    (keg->uk_flags & UMA_ZFLAG_INTERNAL))
1384e9a069d8SJohn Baldwin 		keg->uk_allocf = startup_alloc;
1385099a0e58SBosko Milekic 
1386099a0e58SBosko Milekic 	/*
1387af526374SJeff Roberson 	 * Initialize keg's lock
1388099a0e58SBosko Milekic 	 */
1389af526374SJeff Roberson 	KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS));
1390099a0e58SBosko Milekic 
1391099a0e58SBosko Milekic 	/*
1392099a0e58SBosko Milekic 	 * If we're putting the slab header in the actual page we need to
1393099a0e58SBosko Milekic 	 * figure out where in each page it goes.  This calculates a right
1394099a0e58SBosko Milekic 	 * justified offset into the memory on an ALIGN_PTR boundary.
1395099a0e58SBosko Milekic 	 */
1396099a0e58SBosko Milekic 	if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1397244f4554SBosko Milekic 		u_int totsize;
1398099a0e58SBosko Milekic 
1399099a0e58SBosko Milekic 		/* Size of the slab struct and free list */
1400ef72505eSJeff Roberson 		totsize = sizeof(struct uma_slab);
1401ef72505eSJeff Roberson 
1402ef72505eSJeff Roberson 		/* Size of the reference counts. */
1403244f4554SBosko Milekic 		if (keg->uk_flags & UMA_ZONE_REFCNT)
1404ef72505eSJeff Roberson 			totsize += keg->uk_ipers * sizeof(uint32_t);
1405244f4554SBosko Milekic 
1406099a0e58SBosko Milekic 		if (totsize & UMA_ALIGN_PTR)
1407099a0e58SBosko Milekic 			totsize = (totsize & ~UMA_ALIGN_PTR) +
1408099a0e58SBosko Milekic 			    (UMA_ALIGN_PTR + 1);
1409ad97af7eSGleb Smirnoff 		keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize;
1410244f4554SBosko Milekic 
1411244f4554SBosko Milekic 		/*
1412244f4554SBosko Milekic 		 * The only way the following is possible is if with our
1413244f4554SBosko Milekic 		 * UMA_ALIGN_PTR adjustments we are now bigger than
1414244f4554SBosko Milekic 		 * UMA_SLAB_SIZE.  I haven't checked whether this is
1415244f4554SBosko Milekic 		 * mathematically possible for all cases, so we make
1416244f4554SBosko Milekic 		 * sure here anyway.
1417244f4554SBosko Milekic 		 */
1418ef72505eSJeff Roberson 		totsize = keg->uk_pgoff + sizeof(struct uma_slab);
1419ef72505eSJeff Roberson 		if (keg->uk_flags & UMA_ZONE_REFCNT)
1420ef72505eSJeff Roberson 			totsize += keg->uk_ipers * sizeof(uint32_t);
1421ad97af7eSGleb Smirnoff 		if (totsize > PAGE_SIZE * keg->uk_ppera) {
1422099a0e58SBosko Milekic 			printf("zone %s ipers %d rsize %d size %d\n",
1423099a0e58SBosko Milekic 			    zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1424099a0e58SBosko Milekic 			    keg->uk_size);
1425aea6e893SAlan Cox 			panic("UMA slab won't fit.");
1426099a0e58SBosko Milekic 		}
1427099a0e58SBosko Milekic 	}
1428099a0e58SBosko Milekic 
1429099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_HASH)
1430099a0e58SBosko Milekic 		hash_alloc(&keg->uk_hash);
1431099a0e58SBosko Milekic 
1432099a0e58SBosko Milekic #ifdef UMA_DEBUG
14330b80c1e4SEitan Adler 	printf("UMA: %s(%p) size %d(%d) flags %#x ipers %d ppera %d out %d free %d\n",
1434e20a199fSJeff Roberson 	    zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
1435e20a199fSJeff Roberson 	    keg->uk_ipers, keg->uk_ppera,
1436e20a199fSJeff Roberson 	    (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
1437099a0e58SBosko Milekic #endif
1438099a0e58SBosko Milekic 
1439099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1440099a0e58SBosko Milekic 
1441099a0e58SBosko Milekic 	mtx_lock(&uma_mtx);
1442099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1443099a0e58SBosko Milekic 	mtx_unlock(&uma_mtx);
1444b23f72e9SBrian Feldman 	return (0);
1445099a0e58SBosko Milekic }
1446099a0e58SBosko Milekic 
1447099a0e58SBosko Milekic /*
1448099a0e58SBosko Milekic  * Zone header ctor.  This initializes all fields, locks, etc.
1449099a0e58SBosko Milekic  *
1450099a0e58SBosko Milekic  * Arguments/Returns follow uma_ctor specifications
1451099a0e58SBosko Milekic  *	udata  Actually uma_zctor_args
14528355f576SJeff Roberson  */
1453b23f72e9SBrian Feldman static int
1454b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags)
14558355f576SJeff Roberson {
14568355f576SJeff Roberson 	struct uma_zctor_args *arg = udata;
14578355f576SJeff Roberson 	uma_zone_t zone = mem;
1458099a0e58SBosko Milekic 	uma_zone_t z;
1459099a0e58SBosko Milekic 	uma_keg_t keg;
14608355f576SJeff Roberson 
14618355f576SJeff Roberson 	bzero(zone, size);
14628355f576SJeff Roberson 	zone->uz_name = arg->name;
14638355f576SJeff Roberson 	zone->uz_ctor = arg->ctor;
14648355f576SJeff Roberson 	zone->uz_dtor = arg->dtor;
1465e20a199fSJeff Roberson 	zone->uz_slab = zone_fetch_slab;
1466099a0e58SBosko Milekic 	zone->uz_init = NULL;
1467099a0e58SBosko Milekic 	zone->uz_fini = NULL;
1468099a0e58SBosko Milekic 	zone->uz_allocs = 0;
1469773df9abSRobert Watson 	zone->uz_frees = 0;
14702019094aSRobert Watson 	zone->uz_fails = 0;
1471bf965959SSean Bruno 	zone->uz_sleeps = 0;
1472fc03d22bSJeff Roberson 	zone->uz_count = 0;
1473ace66b56SAlexander Motin 	zone->uz_count_min = 0;
1474e20a199fSJeff Roberson 	zone->uz_flags = 0;
14752f891cd5SPawel Jakub Dawidek 	zone->uz_warning = NULL;
14762f891cd5SPawel Jakub Dawidek 	timevalclear(&zone->uz_ratecheck);
1477e20a199fSJeff Roberson 	keg = arg->keg;
1478099a0e58SBosko Milekic 
1479af526374SJeff Roberson 	ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
1480af526374SJeff Roberson 
14810095a784SJeff Roberson 	/*
14820095a784SJeff Roberson 	 * This is a pure cache zone, no kegs.
14830095a784SJeff Roberson 	 */
14840095a784SJeff Roberson 	if (arg->import) {
14856fd34d6fSJeff Roberson 		if (arg->flags & UMA_ZONE_VM)
14866fd34d6fSJeff Roberson 			arg->flags |= UMA_ZFLAG_CACHEONLY;
14876fd34d6fSJeff Roberson 		zone->uz_flags = arg->flags;
1488af526374SJeff Roberson 		zone->uz_size = arg->size;
14890095a784SJeff Roberson 		zone->uz_import = arg->import;
14900095a784SJeff Roberson 		zone->uz_release = arg->release;
14910095a784SJeff Roberson 		zone->uz_arg = arg->arg;
1492af526374SJeff Roberson 		zone->uz_lockptr = &zone->uz_lock;
1493af526374SJeff Roberson 		goto out;
14940095a784SJeff Roberson 	}
14950095a784SJeff Roberson 
14960095a784SJeff Roberson 	/*
14970095a784SJeff Roberson 	 * Use the regular zone/keg/slab allocator.
14980095a784SJeff Roberson 	 */
14990095a784SJeff Roberson 	zone->uz_import = (uma_import)zone_import;
15000095a784SJeff Roberson 	zone->uz_release = (uma_release)zone_release;
15010095a784SJeff Roberson 	zone->uz_arg = zone;
15020095a784SJeff Roberson 
1503099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_SECONDARY) {
1504099a0e58SBosko Milekic 		KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
15058355f576SJeff Roberson 		zone->uz_init = arg->uminit;
1506e221e841SJeff Roberson 		zone->uz_fini = arg->fini;
1507af526374SJeff Roberson 		zone->uz_lockptr = &keg->uk_lock;
1508e20a199fSJeff Roberson 		zone->uz_flags |= UMA_ZONE_SECONDARY;
15098355f576SJeff Roberson 		mtx_lock(&uma_mtx);
1510099a0e58SBosko Milekic 		ZONE_LOCK(zone);
1511099a0e58SBosko Milekic 		LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1512099a0e58SBosko Milekic 			if (LIST_NEXT(z, uz_link) == NULL) {
1513099a0e58SBosko Milekic 				LIST_INSERT_AFTER(z, zone, uz_link);
1514099a0e58SBosko Milekic 				break;
1515099a0e58SBosko Milekic 			}
1516099a0e58SBosko Milekic 		}
1517099a0e58SBosko Milekic 		ZONE_UNLOCK(zone);
15188355f576SJeff Roberson 		mtx_unlock(&uma_mtx);
1519e20a199fSJeff Roberson 	} else if (keg == NULL) {
1520e20a199fSJeff Roberson 		if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1521e20a199fSJeff Roberson 		    arg->align, arg->flags)) == NULL)
1522b23f72e9SBrian Feldman 			return (ENOMEM);
1523099a0e58SBosko Milekic 	} else {
1524099a0e58SBosko Milekic 		struct uma_kctor_args karg;
1525b23f72e9SBrian Feldman 		int error;
1526099a0e58SBosko Milekic 
1527099a0e58SBosko Milekic 		/* We should only be here from uma_startup() */
1528099a0e58SBosko Milekic 		karg.size = arg->size;
1529099a0e58SBosko Milekic 		karg.uminit = arg->uminit;
1530099a0e58SBosko Milekic 		karg.fini = arg->fini;
1531099a0e58SBosko Milekic 		karg.align = arg->align;
1532099a0e58SBosko Milekic 		karg.flags = arg->flags;
1533099a0e58SBosko Milekic 		karg.zone = zone;
1534b23f72e9SBrian Feldman 		error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1535b23f72e9SBrian Feldman 		    flags);
1536b23f72e9SBrian Feldman 		if (error)
1537b23f72e9SBrian Feldman 			return (error);
1538099a0e58SBosko Milekic 	}
15390095a784SJeff Roberson 
1540e20a199fSJeff Roberson 	/*
1541e20a199fSJeff Roberson 	 * Link in the first keg.
1542e20a199fSJeff Roberson 	 */
1543e20a199fSJeff Roberson 	zone->uz_klink.kl_keg = keg;
1544e20a199fSJeff Roberson 	LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1545af526374SJeff Roberson 	zone->uz_lockptr = &keg->uk_lock;
1546e20a199fSJeff Roberson 	zone->uz_size = keg->uk_size;
1547e20a199fSJeff Roberson 	zone->uz_flags |= (keg->uk_flags &
1548e20a199fSJeff Roberson 	    (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
15498355f576SJeff Roberson 
15508355f576SJeff Roberson 	/*
15518355f576SJeff Roberson 	 * Some internal zones don't have room allocated for the per cpu
15528355f576SJeff Roberson 	 * caches.  If we're internal, bail out here.
15538355f576SJeff Roberson 	 */
1554099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1555e20a199fSJeff Roberson 		KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1556099a0e58SBosko Milekic 		    ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1557b23f72e9SBrian Feldman 		return (0);
1558099a0e58SBosko Milekic 	}
15598355f576SJeff Roberson 
1560af526374SJeff Roberson out:
1561af526374SJeff Roberson 	if ((arg->flags & UMA_ZONE_MAXBUCKET) == 0)
1562af526374SJeff Roberson 		zone->uz_count = bucket_select(zone->uz_size);
15638355f576SJeff Roberson 	else
1564cae33c14SJeff Roberson 		zone->uz_count = BUCKET_MAX;
1565ace66b56SAlexander Motin 	zone->uz_count_min = zone->uz_count;
1566fc03d22bSJeff Roberson 
1567b23f72e9SBrian Feldman 	return (0);
15688355f576SJeff Roberson }
15698355f576SJeff Roberson 
15708355f576SJeff Roberson /*
1571099a0e58SBosko Milekic  * Keg header dtor.  This frees all data, destroys locks, frees the hash
1572099a0e58SBosko Milekic  * table and removes the keg from the global list.
15739c2cd7e5SJeff Roberson  *
15749c2cd7e5SJeff Roberson  * Arguments/Returns follow uma_dtor specifications
15759c2cd7e5SJeff Roberson  *	udata  unused
15769c2cd7e5SJeff Roberson  */
1577099a0e58SBosko Milekic static void
1578099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata)
1579099a0e58SBosko Milekic {
1580099a0e58SBosko Milekic 	uma_keg_t keg;
15819c2cd7e5SJeff Roberson 
1582099a0e58SBosko Milekic 	keg = (uma_keg_t)arg;
1583e20a199fSJeff Roberson 	KEG_LOCK(keg);
1584099a0e58SBosko Milekic 	if (keg->uk_free != 0) {
1585099a0e58SBosko Milekic 		printf("Freed UMA keg was not empty (%d items). "
1586099a0e58SBosko Milekic 		    " Lost %d pages of memory.\n",
1587099a0e58SBosko Milekic 		    keg->uk_free, keg->uk_pages);
1588099a0e58SBosko Milekic 	}
1589e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
1590099a0e58SBosko Milekic 
1591099a0e58SBosko Milekic 	hash_free(&keg->uk_hash);
1592099a0e58SBosko Milekic 
1593e20a199fSJeff Roberson 	KEG_LOCK_FINI(keg);
1594099a0e58SBosko Milekic }
1595099a0e58SBosko Milekic 
1596099a0e58SBosko Milekic /*
1597099a0e58SBosko Milekic  * Zone header dtor.
1598099a0e58SBosko Milekic  *
1599099a0e58SBosko Milekic  * Arguments/Returns follow uma_dtor specifications
1600099a0e58SBosko Milekic  *	udata  unused
1601099a0e58SBosko Milekic  */
16029c2cd7e5SJeff Roberson static void
16039c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata)
16049c2cd7e5SJeff Roberson {
1605e20a199fSJeff Roberson 	uma_klink_t klink;
16069c2cd7e5SJeff Roberson 	uma_zone_t zone;
1607099a0e58SBosko Milekic 	uma_keg_t keg;
16089c2cd7e5SJeff Roberson 
16099c2cd7e5SJeff Roberson 	zone = (uma_zone_t)arg;
1610e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
16119643769aSJeff Roberson 
1612e20a199fSJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
16139643769aSJeff Roberson 		cache_drain(zone);
1614099a0e58SBosko Milekic 
161517b9cc49SJeff Roberson 	mtx_lock(&uma_mtx);
1616099a0e58SBosko Milekic 	LIST_REMOVE(zone, uz_link);
1617e20a199fSJeff Roberson 	mtx_unlock(&uma_mtx);
1618099a0e58SBosko Milekic 	/*
1619099a0e58SBosko Milekic 	 * XXX there are some races here where
1620099a0e58SBosko Milekic 	 * the zone can be drained but zone lock
1621099a0e58SBosko Milekic 	 * released and then refilled before we
1622099a0e58SBosko Milekic 	 * remove it... we dont care for now
1623099a0e58SBosko Milekic 	 */
1624e20a199fSJeff Roberson 	zone_drain_wait(zone, M_WAITOK);
1625e20a199fSJeff Roberson 	/*
1626e20a199fSJeff Roberson 	 * Unlink all of our kegs.
1627e20a199fSJeff Roberson 	 */
1628e20a199fSJeff Roberson 	while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1629e20a199fSJeff Roberson 		klink->kl_keg = NULL;
1630e20a199fSJeff Roberson 		LIST_REMOVE(klink, kl_link);
1631e20a199fSJeff Roberson 		if (klink == &zone->uz_klink)
1632e20a199fSJeff Roberson 			continue;
1633e20a199fSJeff Roberson 		free(klink, M_TEMP);
1634e20a199fSJeff Roberson 	}
1635e20a199fSJeff Roberson 	/*
1636e20a199fSJeff Roberson 	 * We only destroy kegs from non secondary zones.
1637e20a199fSJeff Roberson 	 */
16380095a784SJeff Roberson 	if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0)  {
1639e20a199fSJeff Roberson 		mtx_lock(&uma_mtx);
1640099a0e58SBosko Milekic 		LIST_REMOVE(keg, uk_link);
1641099a0e58SBosko Milekic 		mtx_unlock(&uma_mtx);
16420095a784SJeff Roberson 		zone_free_item(kegs, keg, NULL, SKIP_NONE);
16439c2cd7e5SJeff Roberson 	}
1644af526374SJeff Roberson 	ZONE_LOCK_FINI(zone);
1645099a0e58SBosko Milekic }
1646099a0e58SBosko Milekic 
16479c2cd7e5SJeff Roberson /*
16488355f576SJeff Roberson  * Traverses every zone in the system and calls a callback
16498355f576SJeff Roberson  *
16508355f576SJeff Roberson  * Arguments:
16518355f576SJeff Roberson  *	zfunc  A pointer to a function which accepts a zone
16528355f576SJeff Roberson  *		as an argument.
16538355f576SJeff Roberson  *
16548355f576SJeff Roberson  * Returns:
16558355f576SJeff Roberson  *	Nothing
16568355f576SJeff Roberson  */
16578355f576SJeff Roberson static void
16588355f576SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t))
16598355f576SJeff Roberson {
1660099a0e58SBosko Milekic 	uma_keg_t keg;
16618355f576SJeff Roberson 	uma_zone_t zone;
16628355f576SJeff Roberson 
16638355f576SJeff Roberson 	mtx_lock(&uma_mtx);
1664099a0e58SBosko Milekic 	LIST_FOREACH(keg, &uma_kegs, uk_link) {
1665099a0e58SBosko Milekic 		LIST_FOREACH(zone, &keg->uk_zones, uz_link)
16668355f576SJeff Roberson 			zfunc(zone);
1667099a0e58SBosko Milekic 	}
16688355f576SJeff Roberson 	mtx_unlock(&uma_mtx);
16698355f576SJeff Roberson }
16708355f576SJeff Roberson 
16718355f576SJeff Roberson /* Public functions */
16728355f576SJeff Roberson /* See uma.h */
16738355f576SJeff Roberson void
16743803b26bSDag-Erling Smørgrav uma_startup(void *bootmem, int boot_pages)
16758355f576SJeff Roberson {
16768355f576SJeff Roberson 	struct uma_zctor_args args;
16778355f576SJeff Roberson 	uma_slab_t slab;
1678244f4554SBosko Milekic 	u_int slabsize;
16798355f576SJeff Roberson 	int i;
16808355f576SJeff Roberson 
16818355f576SJeff Roberson #ifdef UMA_DEBUG
1682099a0e58SBosko Milekic 	printf("Creating uma keg headers zone and keg.\n");
16838355f576SJeff Roberson #endif
1684f353d338SAlan Cox 	mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
1685099a0e58SBosko Milekic 
1686099a0e58SBosko Milekic 	/* "manually" create the initial zone */
16870095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
1688099a0e58SBosko Milekic 	args.name = "UMA Kegs";
1689099a0e58SBosko Milekic 	args.size = sizeof(struct uma_keg);
1690099a0e58SBosko Milekic 	args.ctor = keg_ctor;
1691099a0e58SBosko Milekic 	args.dtor = keg_dtor;
16928355f576SJeff Roberson 	args.uminit = zero_init;
16938355f576SJeff Roberson 	args.fini = NULL;
1694099a0e58SBosko Milekic 	args.keg = &masterkeg;
16958355f576SJeff Roberson 	args.align = 32 - 1;
1696b60f5b79SJeff Roberson 	args.flags = UMA_ZFLAG_INTERNAL;
16978355f576SJeff Roberson 	/* The initial zone has no Per cpu queues so it's smaller */
1698b23f72e9SBrian Feldman 	zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
16998355f576SJeff Roberson 
17008355f576SJeff Roberson #ifdef UMA_DEBUG
17018355f576SJeff Roberson 	printf("Filling boot free list.\n");
17028355f576SJeff Roberson #endif
17033803b26bSDag-Erling Smørgrav 	for (i = 0; i < boot_pages; i++) {
170485dcf349SGleb Smirnoff 		slab = (uma_slab_t)((uint8_t *)bootmem + (i * UMA_SLAB_SIZE));
170585dcf349SGleb Smirnoff 		slab->us_data = (uint8_t *)slab;
17068355f576SJeff Roberson 		slab->us_flags = UMA_SLAB_BOOT;
17078355f576SJeff Roberson 		LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
17088355f576SJeff Roberson 	}
1709f353d338SAlan Cox 	mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
17108355f576SJeff Roberson 
17118355f576SJeff Roberson #ifdef UMA_DEBUG
1712099a0e58SBosko Milekic 	printf("Creating uma zone headers zone and keg.\n");
1713099a0e58SBosko Milekic #endif
1714099a0e58SBosko Milekic 	args.name = "UMA Zones";
1715099a0e58SBosko Milekic 	args.size = sizeof(struct uma_zone) +
1716099a0e58SBosko Milekic 	    (sizeof(struct uma_cache) * (mp_maxid + 1));
1717099a0e58SBosko Milekic 	args.ctor = zone_ctor;
1718099a0e58SBosko Milekic 	args.dtor = zone_dtor;
1719099a0e58SBosko Milekic 	args.uminit = zero_init;
1720099a0e58SBosko Milekic 	args.fini = NULL;
1721099a0e58SBosko Milekic 	args.keg = NULL;
1722099a0e58SBosko Milekic 	args.align = 32 - 1;
1723099a0e58SBosko Milekic 	args.flags = UMA_ZFLAG_INTERNAL;
1724099a0e58SBosko Milekic 	/* The initial zone has no Per cpu queues so it's smaller */
1725b23f72e9SBrian Feldman 	zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1726099a0e58SBosko Milekic 
1727099a0e58SBosko Milekic #ifdef UMA_DEBUG
1728099a0e58SBosko Milekic 	printf("Initializing pcpu cache locks.\n");
1729099a0e58SBosko Milekic #endif
1730099a0e58SBosko Milekic #ifdef UMA_DEBUG
1731099a0e58SBosko Milekic 	printf("Creating slab and hash zones.\n");
17328355f576SJeff Roberson #endif
17338355f576SJeff Roberson 
17348355f576SJeff Roberson 	/* Now make a zone for slab headers */
17358355f576SJeff Roberson 	slabzone = uma_zcreate("UMA Slabs",
1736ef72505eSJeff Roberson 				sizeof(struct uma_slab),
17378355f576SJeff Roberson 				NULL, NULL, NULL, NULL,
1738b60f5b79SJeff Roberson 				UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
17398355f576SJeff Roberson 
1740099a0e58SBosko Milekic 	/*
1741099a0e58SBosko Milekic 	 * We also create a zone for the bigger slabs with reference
1742099a0e58SBosko Milekic 	 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1743099a0e58SBosko Milekic 	 */
1744ef72505eSJeff Roberson 	slabsize = sizeof(struct uma_slab_refcnt);
1745ef72505eSJeff Roberson 	slabsize += uma_max_ipers_ref * sizeof(uint32_t);
1746099a0e58SBosko Milekic 	slabrefzone = uma_zcreate("UMA RCntSlabs",
1747099a0e58SBosko Milekic 				  slabsize,
1748099a0e58SBosko Milekic 				  NULL, NULL, NULL, NULL,
1749e66468eaSBosko Milekic 				  UMA_ALIGN_PTR,
17507fd87882SBosko Milekic 				  UMA_ZFLAG_INTERNAL);
1751099a0e58SBosko Milekic 
17528355f576SJeff Roberson 	hashzone = uma_zcreate("UMA Hash",
17538355f576SJeff Roberson 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
17548355f576SJeff Roberson 	    NULL, NULL, NULL, NULL,
1755b60f5b79SJeff Roberson 	    UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
17568355f576SJeff Roberson 
1757cae33c14SJeff Roberson 	bucket_init();
17588355f576SJeff Roberson 
1759342f1793SAlan Cox 	booted = UMA_STARTUP;
17608355f576SJeff Roberson 
17618355f576SJeff Roberson #ifdef UMA_DEBUG
17628355f576SJeff Roberson 	printf("UMA startup complete.\n");
17638355f576SJeff Roberson #endif
17648355f576SJeff Roberson }
17658355f576SJeff Roberson 
17668355f576SJeff Roberson /* see uma.h */
17678355f576SJeff Roberson void
176899571dc3SJeff Roberson uma_startup2(void)
17698355f576SJeff Roberson {
1770342f1793SAlan Cox 	booted = UMA_STARTUP2;
177186bbae32SJeff Roberson 	bucket_enable();
17728355f576SJeff Roberson #ifdef UMA_DEBUG
17738355f576SJeff Roberson 	printf("UMA startup2 complete.\n");
17748355f576SJeff Roberson #endif
17758355f576SJeff Roberson }
17768355f576SJeff Roberson 
17778355f576SJeff Roberson /*
17788355f576SJeff Roberson  * Initialize our callout handle
17798355f576SJeff Roberson  *
17808355f576SJeff Roberson  */
17818355f576SJeff Roberson 
17828355f576SJeff Roberson static void
17838355f576SJeff Roberson uma_startup3(void)
17848355f576SJeff Roberson {
17858355f576SJeff Roberson #ifdef UMA_DEBUG
17868355f576SJeff Roberson 	printf("Starting callout.\n");
17878355f576SJeff Roberson #endif
1788a3c07611SRobert Watson 	callout_init(&uma_callout, CALLOUT_MPSAFE);
17899643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
17908355f576SJeff Roberson #ifdef UMA_DEBUG
17918355f576SJeff Roberson 	printf("UMA startup3 complete.\n");
17928355f576SJeff Roberson #endif
17938355f576SJeff Roberson }
17948355f576SJeff Roberson 
1795e20a199fSJeff Roberson static uma_keg_t
1796099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
179785dcf349SGleb Smirnoff 		int align, uint32_t flags)
1798099a0e58SBosko Milekic {
1799099a0e58SBosko Milekic 	struct uma_kctor_args args;
1800099a0e58SBosko Milekic 
1801099a0e58SBosko Milekic 	args.size = size;
1802099a0e58SBosko Milekic 	args.uminit = uminit;
1803099a0e58SBosko Milekic 	args.fini = fini;
18041e319f6dSRobert Watson 	args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1805099a0e58SBosko Milekic 	args.flags = flags;
1806099a0e58SBosko Milekic 	args.zone = zone;
1807e20a199fSJeff Roberson 	return (zone_alloc_item(kegs, &args, M_WAITOK));
1808099a0e58SBosko Milekic }
1809099a0e58SBosko Milekic 
18108355f576SJeff Roberson /* See uma.h */
18111e319f6dSRobert Watson void
18121e319f6dSRobert Watson uma_set_align(int align)
18131e319f6dSRobert Watson {
18141e319f6dSRobert Watson 
18151e319f6dSRobert Watson 	if (align != UMA_ALIGN_CACHE)
18161e319f6dSRobert Watson 		uma_align_cache = align;
18171e319f6dSRobert Watson }
18181e319f6dSRobert Watson 
18191e319f6dSRobert Watson /* See uma.h */
18208355f576SJeff Roberson uma_zone_t
1821bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
182285dcf349SGleb Smirnoff 		uma_init uminit, uma_fini fini, int align, uint32_t flags)
18238355f576SJeff Roberson 
18248355f576SJeff Roberson {
18258355f576SJeff Roberson 	struct uma_zctor_args args;
18268355f576SJeff Roberson 
18278355f576SJeff Roberson 	/* This stuff is essential for the zone ctor */
18280095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
18298355f576SJeff Roberson 	args.name = name;
18308355f576SJeff Roberson 	args.size = size;
18318355f576SJeff Roberson 	args.ctor = ctor;
18328355f576SJeff Roberson 	args.dtor = dtor;
18338355f576SJeff Roberson 	args.uminit = uminit;
18348355f576SJeff Roberson 	args.fini = fini;
18358355f576SJeff Roberson 	args.align = align;
18368355f576SJeff Roberson 	args.flags = flags;
1837099a0e58SBosko Milekic 	args.keg = NULL;
1838099a0e58SBosko Milekic 
1839e20a199fSJeff Roberson 	return (zone_alloc_item(zones, &args, M_WAITOK));
1840099a0e58SBosko Milekic }
1841099a0e58SBosko Milekic 
1842099a0e58SBosko Milekic /* See uma.h */
1843099a0e58SBosko Milekic uma_zone_t
1844099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1845099a0e58SBosko Milekic 		    uma_init zinit, uma_fini zfini, uma_zone_t master)
1846099a0e58SBosko Milekic {
1847099a0e58SBosko Milekic 	struct uma_zctor_args args;
1848e20a199fSJeff Roberson 	uma_keg_t keg;
1849099a0e58SBosko Milekic 
1850e20a199fSJeff Roberson 	keg = zone_first_keg(master);
18510095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
1852099a0e58SBosko Milekic 	args.name = name;
1853e20a199fSJeff Roberson 	args.size = keg->uk_size;
1854099a0e58SBosko Milekic 	args.ctor = ctor;
1855099a0e58SBosko Milekic 	args.dtor = dtor;
1856099a0e58SBosko Milekic 	args.uminit = zinit;
1857099a0e58SBosko Milekic 	args.fini = zfini;
1858e20a199fSJeff Roberson 	args.align = keg->uk_align;
1859e20a199fSJeff Roberson 	args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
1860e20a199fSJeff Roberson 	args.keg = keg;
18618355f576SJeff Roberson 
1862e20a199fSJeff Roberson 	/* XXX Attaches only one keg of potentially many. */
1863e20a199fSJeff Roberson 	return (zone_alloc_item(zones, &args, M_WAITOK));
18648355f576SJeff Roberson }
18658355f576SJeff Roberson 
18660095a784SJeff Roberson /* See uma.h */
18670095a784SJeff Roberson uma_zone_t
1868af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
1869af526374SJeff Roberson 		    uma_init zinit, uma_fini zfini, uma_import zimport,
1870af526374SJeff Roberson 		    uma_release zrelease, void *arg, int flags)
18710095a784SJeff Roberson {
18720095a784SJeff Roberson 	struct uma_zctor_args args;
18730095a784SJeff Roberson 
18740095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
18750095a784SJeff Roberson 	args.name = name;
1876af526374SJeff Roberson 	args.size = size;
18770095a784SJeff Roberson 	args.ctor = ctor;
18780095a784SJeff Roberson 	args.dtor = dtor;
18790095a784SJeff Roberson 	args.uminit = zinit;
18800095a784SJeff Roberson 	args.fini = zfini;
18810095a784SJeff Roberson 	args.import = zimport;
18820095a784SJeff Roberson 	args.release = zrelease;
18830095a784SJeff Roberson 	args.arg = arg;
18840095a784SJeff Roberson 	args.align = 0;
18850095a784SJeff Roberson 	args.flags = flags;
18860095a784SJeff Roberson 
18870095a784SJeff Roberson 	return (zone_alloc_item(zones, &args, M_WAITOK));
18880095a784SJeff Roberson }
18890095a784SJeff Roberson 
1890e20a199fSJeff Roberson static void
1891e20a199fSJeff Roberson zone_lock_pair(uma_zone_t a, uma_zone_t b)
1892e20a199fSJeff Roberson {
1893e20a199fSJeff Roberson 	if (a < b) {
1894e20a199fSJeff Roberson 		ZONE_LOCK(a);
1895af526374SJeff Roberson 		mtx_lock_flags(b->uz_lockptr, MTX_DUPOK);
1896e20a199fSJeff Roberson 	} else {
1897e20a199fSJeff Roberson 		ZONE_LOCK(b);
1898af526374SJeff Roberson 		mtx_lock_flags(a->uz_lockptr, MTX_DUPOK);
1899e20a199fSJeff Roberson 	}
1900e20a199fSJeff Roberson }
1901e20a199fSJeff Roberson 
1902e20a199fSJeff Roberson static void
1903e20a199fSJeff Roberson zone_unlock_pair(uma_zone_t a, uma_zone_t b)
1904e20a199fSJeff Roberson {
1905e20a199fSJeff Roberson 
1906e20a199fSJeff Roberson 	ZONE_UNLOCK(a);
1907e20a199fSJeff Roberson 	ZONE_UNLOCK(b);
1908e20a199fSJeff Roberson }
1909e20a199fSJeff Roberson 
1910e20a199fSJeff Roberson int
1911e20a199fSJeff Roberson uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
1912e20a199fSJeff Roberson {
1913e20a199fSJeff Roberson 	uma_klink_t klink;
1914e20a199fSJeff Roberson 	uma_klink_t kl;
1915e20a199fSJeff Roberson 	int error;
1916e20a199fSJeff Roberson 
1917e20a199fSJeff Roberson 	error = 0;
1918e20a199fSJeff Roberson 	klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
1919e20a199fSJeff Roberson 
1920e20a199fSJeff Roberson 	zone_lock_pair(zone, master);
1921e20a199fSJeff Roberson 	/*
1922e20a199fSJeff Roberson 	 * zone must use vtoslab() to resolve objects and must already be
1923e20a199fSJeff Roberson 	 * a secondary.
1924e20a199fSJeff Roberson 	 */
1925e20a199fSJeff Roberson 	if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
1926e20a199fSJeff Roberson 	    != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
1927e20a199fSJeff Roberson 		error = EINVAL;
1928e20a199fSJeff Roberson 		goto out;
1929e20a199fSJeff Roberson 	}
1930e20a199fSJeff Roberson 	/*
1931e20a199fSJeff Roberson 	 * The new master must also use vtoslab().
1932e20a199fSJeff Roberson 	 */
1933e20a199fSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
1934e20a199fSJeff Roberson 		error = EINVAL;
1935e20a199fSJeff Roberson 		goto out;
1936e20a199fSJeff Roberson 	}
1937e20a199fSJeff Roberson 	/*
1938e20a199fSJeff Roberson 	 * Both must either be refcnt, or not be refcnt.
1939e20a199fSJeff Roberson 	 */
1940e20a199fSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_REFCNT) !=
1941e20a199fSJeff Roberson 	    (master->uz_flags & UMA_ZONE_REFCNT)) {
1942e20a199fSJeff Roberson 		error = EINVAL;
1943e20a199fSJeff Roberson 		goto out;
1944e20a199fSJeff Roberson 	}
1945e20a199fSJeff Roberson 	/*
1946e20a199fSJeff Roberson 	 * The underlying object must be the same size.  rsize
1947e20a199fSJeff Roberson 	 * may be different.
1948e20a199fSJeff Roberson 	 */
1949e20a199fSJeff Roberson 	if (master->uz_size != zone->uz_size) {
1950e20a199fSJeff Roberson 		error = E2BIG;
1951e20a199fSJeff Roberson 		goto out;
1952e20a199fSJeff Roberson 	}
1953e20a199fSJeff Roberson 	/*
1954e20a199fSJeff Roberson 	 * Put it at the end of the list.
1955e20a199fSJeff Roberson 	 */
1956e20a199fSJeff Roberson 	klink->kl_keg = zone_first_keg(master);
1957e20a199fSJeff Roberson 	LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
1958e20a199fSJeff Roberson 		if (LIST_NEXT(kl, kl_link) == NULL) {
1959e20a199fSJeff Roberson 			LIST_INSERT_AFTER(kl, klink, kl_link);
1960e20a199fSJeff Roberson 			break;
1961e20a199fSJeff Roberson 		}
1962e20a199fSJeff Roberson 	}
1963e20a199fSJeff Roberson 	klink = NULL;
1964e20a199fSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_MULTI;
1965e20a199fSJeff Roberson 	zone->uz_slab = zone_fetch_slab_multi;
1966e20a199fSJeff Roberson 
1967e20a199fSJeff Roberson out:
1968e20a199fSJeff Roberson 	zone_unlock_pair(zone, master);
1969e20a199fSJeff Roberson 	if (klink != NULL)
1970e20a199fSJeff Roberson 		free(klink, M_TEMP);
1971e20a199fSJeff Roberson 
1972e20a199fSJeff Roberson 	return (error);
1973e20a199fSJeff Roberson }
1974e20a199fSJeff Roberson 
1975e20a199fSJeff Roberson 
19768355f576SJeff Roberson /* See uma.h */
19779c2cd7e5SJeff Roberson void
19789c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone)
19799c2cd7e5SJeff Roberson {
1980f4ff923bSRobert Watson 
19810095a784SJeff Roberson 	zone_free_item(zones, zone, NULL, SKIP_NONE);
19829c2cd7e5SJeff Roberson }
19839c2cd7e5SJeff Roberson 
19849c2cd7e5SJeff Roberson /* See uma.h */
19858355f576SJeff Roberson void *
19862cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
19878355f576SJeff Roberson {
19888355f576SJeff Roberson 	void *item;
19898355f576SJeff Roberson 	uma_cache_t cache;
19908355f576SJeff Roberson 	uma_bucket_t bucket;
1991fc03d22bSJeff Roberson 	int lockfail;
19928355f576SJeff Roberson 	int cpu;
19938355f576SJeff Roberson 
19948355f576SJeff Roberson 	/* This is the fast path allocation */
19958355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1
19968355f576SJeff Roberson 	printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
19978355f576SJeff Roberson #endif
19983659f747SRobert Watson 	CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
19993659f747SRobert Watson 	    zone->uz_name, flags);
2000a553d4b8SJeff Roberson 
2001635fd505SRobert Watson 	if (flags & M_WAITOK) {
2002b23f72e9SBrian Feldman 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2003635fd505SRobert Watson 		    "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
20044c1cc01cSJohn Baldwin 	}
20058d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
20068d689e04SGleb Smirnoff 	if (memguard_cmp_zone(zone)) {
20078d689e04SGleb Smirnoff 		item = memguard_alloc(zone->uz_size, flags);
20088d689e04SGleb Smirnoff 		if (item != NULL) {
20098d689e04SGleb Smirnoff 			/*
20108d689e04SGleb Smirnoff 			 * Avoid conflict with the use-after-free
20118d689e04SGleb Smirnoff 			 * protecting infrastructure from INVARIANTS.
20128d689e04SGleb Smirnoff 			 */
20138d689e04SGleb Smirnoff 			if (zone->uz_init != NULL &&
20148d689e04SGleb Smirnoff 			    zone->uz_init != mtrash_init &&
20158d689e04SGleb Smirnoff 			    zone->uz_init(item, zone->uz_size, flags) != 0)
20168d689e04SGleb Smirnoff 				return (NULL);
20178d689e04SGleb Smirnoff 			if (zone->uz_ctor != NULL &&
20188d689e04SGleb Smirnoff 			    zone->uz_ctor != mtrash_ctor &&
2019fc03d22bSJeff Roberson 			    zone->uz_ctor(item, zone->uz_size, udata,
2020fc03d22bSJeff Roberson 			    flags) != 0) {
20218d689e04SGleb Smirnoff 			    	zone->uz_fini(item, zone->uz_size);
20228d689e04SGleb Smirnoff 				return (NULL);
20238d689e04SGleb Smirnoff 			}
20248d689e04SGleb Smirnoff 			return (item);
20258d689e04SGleb Smirnoff 		}
20268d689e04SGleb Smirnoff 		/* This is unfortunate but should not be fatal. */
20278d689e04SGleb Smirnoff 	}
20288d689e04SGleb Smirnoff #endif
20295d1ae027SRobert Watson 	/*
20305d1ae027SRobert Watson 	 * If possible, allocate from the per-CPU cache.  There are two
20315d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
20325d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
20335d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
20345d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
20355d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
20365d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to allocate from
20375d1ae027SRobert Watson 	 * the current cache; when we re-acquire the critical section, we
20385d1ae027SRobert Watson 	 * must detect and handle migration if it has occurred.
20395d1ae027SRobert Watson 	 */
20405d1ae027SRobert Watson 	critical_enter();
20415d1ae027SRobert Watson 	cpu = curcpu;
20428355f576SJeff Roberson 	cache = &zone->uz_cpu[cpu];
20438355f576SJeff Roberson 
20448355f576SJeff Roberson zalloc_start:
20458355f576SJeff Roberson 	bucket = cache->uc_allocbucket;
2046fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt > 0) {
2047cae33c14SJeff Roberson 		bucket->ub_cnt--;
2048cae33c14SJeff Roberson 		item = bucket->ub_bucket[bucket->ub_cnt];
20498355f576SJeff Roberson #ifdef INVARIANTS
2050cae33c14SJeff Roberson 		bucket->ub_bucket[bucket->ub_cnt] = NULL;
20518355f576SJeff Roberson #endif
2052fc03d22bSJeff Roberson 		KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
20538355f576SJeff Roberson 		cache->uc_allocs++;
20545d1ae027SRobert Watson 		critical_exit();
2055fc03d22bSJeff Roberson 		if (zone->uz_ctor != NULL &&
2056fc03d22bSJeff Roberson 		    zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
20570095a784SJeff Roberson 			atomic_add_long(&zone->uz_fails, 1);
2058fc03d22bSJeff Roberson 			zone_free_item(zone, item, udata, SKIP_DTOR);
2059b23f72e9SBrian Feldman 			return (NULL);
2060b23f72e9SBrian Feldman 		}
2061ef72505eSJeff Roberson #ifdef INVARIANTS
2062ef72505eSJeff Roberson 		uma_dbg_alloc(zone, NULL, item);
2063ef72505eSJeff Roberson #endif
20642cc35ff9SJeff Roberson 		if (flags & M_ZERO)
2065e20a199fSJeff Roberson 			bzero(item, zone->uz_size);
20668355f576SJeff Roberson 		return (item);
2067fc03d22bSJeff Roberson 	}
2068fc03d22bSJeff Roberson 
20698355f576SJeff Roberson 	/*
20708355f576SJeff Roberson 	 * We have run out of items in our alloc bucket.
20718355f576SJeff Roberson 	 * See if we can switch with our free bucket.
20728355f576SJeff Roberson 	 */
2073b983089aSJeff Roberson 	bucket = cache->uc_freebucket;
2074fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt > 0) {
2075fc03d22bSJeff Roberson #ifdef UMA_DEBUG_ALLOC
2076fc03d22bSJeff Roberson 		printf("uma_zalloc: Swapping empty with alloc.\n");
2077fc03d22bSJeff Roberson #endif
20788355f576SJeff Roberson 		cache->uc_freebucket = cache->uc_allocbucket;
2079b983089aSJeff Roberson 		cache->uc_allocbucket = bucket;
20808355f576SJeff Roberson 		goto zalloc_start;
20818355f576SJeff Roberson 	}
2082fc03d22bSJeff Roberson 
2083fc03d22bSJeff Roberson 	/*
2084fc03d22bSJeff Roberson 	 * Discard any empty allocation bucket while we hold no locks.
2085fc03d22bSJeff Roberson 	 */
2086fc03d22bSJeff Roberson 	bucket = cache->uc_allocbucket;
2087fc03d22bSJeff Roberson 	cache->uc_allocbucket = NULL;
2088fc03d22bSJeff Roberson 	critical_exit();
2089fc03d22bSJeff Roberson 	if (bucket != NULL)
20906fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
2091fc03d22bSJeff Roberson 
2092fc03d22bSJeff Roberson 	/* Short-circuit for zones without buckets and low memory. */
2093fc03d22bSJeff Roberson 	if (zone->uz_count == 0 || bucketdisable)
2094fc03d22bSJeff Roberson 		goto zalloc_item;
2095fc03d22bSJeff Roberson 
20965d1ae027SRobert Watson 	/*
20975d1ae027SRobert Watson 	 * Attempt to retrieve the item from the per-CPU cache has failed, so
20985d1ae027SRobert Watson 	 * we must go back to the zone.  This requires the zone lock, so we
20995d1ae027SRobert Watson 	 * must drop the critical section, then re-acquire it when we go back
21005d1ae027SRobert Watson 	 * to the cache.  Since the critical section is released, we may be
21015d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
21025d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
21035d1ae027SRobert Watson 	 * the critical section.
21045d1ae027SRobert Watson 	 */
2105fc03d22bSJeff Roberson 	lockfail = 0;
2106fc03d22bSJeff Roberson 	if (ZONE_TRYLOCK(zone) == 0) {
2107fc03d22bSJeff Roberson 		/* Record contention to size the buckets. */
2108a553d4b8SJeff Roberson 		ZONE_LOCK(zone);
2109fc03d22bSJeff Roberson 		lockfail = 1;
2110fc03d22bSJeff Roberson 	}
21115d1ae027SRobert Watson 	critical_enter();
21125d1ae027SRobert Watson 	cpu = curcpu;
21135d1ae027SRobert Watson 	cache = &zone->uz_cpu[cpu];
21145d1ae027SRobert Watson 
2115fc03d22bSJeff Roberson 	/*
2116fc03d22bSJeff Roberson 	 * Since we have locked the zone we may as well send back our stats.
2117fc03d22bSJeff Roberson 	 */
21180095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
21190095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, cache->uc_frees);
2120a553d4b8SJeff Roberson 	cache->uc_allocs = 0;
2121773df9abSRobert Watson 	cache->uc_frees = 0;
21228355f576SJeff Roberson 
2123fc03d22bSJeff Roberson 	/* See if we lost the race to fill the cache. */
2124fc03d22bSJeff Roberson 	if (cache->uc_allocbucket != NULL) {
2125fc03d22bSJeff Roberson 		ZONE_UNLOCK(zone);
2126fc03d22bSJeff Roberson 		goto zalloc_start;
2127a553d4b8SJeff Roberson 	}
21288355f576SJeff Roberson 
2129fc03d22bSJeff Roberson 	/*
2130fc03d22bSJeff Roberson 	 * Check the zone's cache of buckets.
2131fc03d22bSJeff Roberson 	 */
2132fc03d22bSJeff Roberson 	if ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
2133cae33c14SJeff Roberson 		KASSERT(bucket->ub_cnt != 0,
2134a553d4b8SJeff Roberson 		    ("uma_zalloc_arg: Returning an empty bucket."));
21358355f576SJeff Roberson 
2136a553d4b8SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
2137a553d4b8SJeff Roberson 		cache->uc_allocbucket = bucket;
2138a553d4b8SJeff Roberson 		ZONE_UNLOCK(zone);
21398355f576SJeff Roberson 		goto zalloc_start;
2140a553d4b8SJeff Roberson 	}
21415d1ae027SRobert Watson 	/* We are no longer associated with this CPU. */
21425d1ae027SRobert Watson 	critical_exit();
2143bbee39c6SJeff Roberson 
2144fc03d22bSJeff Roberson 	/*
2145fc03d22bSJeff Roberson 	 * We bump the uz count when the cache size is insufficient to
2146fc03d22bSJeff Roberson 	 * handle the working set.
2147fc03d22bSJeff Roberson 	 */
21486fd34d6fSJeff Roberson 	if (lockfail && zone->uz_count < BUCKET_MAX)
2149a553d4b8SJeff Roberson 		zone->uz_count++;
2150fc03d22bSJeff Roberson 	ZONE_UNLOCK(zone);
2151099a0e58SBosko Milekic 
21528355f576SJeff Roberson 	/*
2153a553d4b8SJeff Roberson 	 * Now lets just fill a bucket and put it on the free list.  If that
2154fc03d22bSJeff Roberson 	 * works we'll restart the allocation from the begining and it
2155fc03d22bSJeff Roberson 	 * will use the just filled bucket.
2156bbee39c6SJeff Roberson 	 */
21576fd34d6fSJeff Roberson 	bucket = zone_alloc_bucket(zone, udata, flags);
2158fc03d22bSJeff Roberson 	if (bucket != NULL) {
2159fc03d22bSJeff Roberson 		ZONE_LOCK(zone);
2160fc03d22bSJeff Roberson 		critical_enter();
2161fc03d22bSJeff Roberson 		cpu = curcpu;
2162fc03d22bSJeff Roberson 		cache = &zone->uz_cpu[cpu];
2163fc03d22bSJeff Roberson 		/*
2164fc03d22bSJeff Roberson 		 * See if we lost the race or were migrated.  Cache the
2165fc03d22bSJeff Roberson 		 * initialized bucket to make this less likely or claim
2166fc03d22bSJeff Roberson 		 * the memory directly.
2167fc03d22bSJeff Roberson 		 */
2168fc03d22bSJeff Roberson 		if (cache->uc_allocbucket == NULL)
2169fc03d22bSJeff Roberson 			cache->uc_allocbucket = bucket;
2170fc03d22bSJeff Roberson 		else
2171fc03d22bSJeff Roberson 			LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
2172bbee39c6SJeff Roberson 		ZONE_UNLOCK(zone);
2173fc03d22bSJeff Roberson 		goto zalloc_start;
2174bbee39c6SJeff Roberson 	}
2175fc03d22bSJeff Roberson 
2176bbee39c6SJeff Roberson 	/*
2177bbee39c6SJeff Roberson 	 * We may not be able to get a bucket so return an actual item.
2178bbee39c6SJeff Roberson 	 */
2179bbee39c6SJeff Roberson #ifdef UMA_DEBUG
2180bbee39c6SJeff Roberson 	printf("uma_zalloc_arg: Bucketzone returned NULL\n");
2181bbee39c6SJeff Roberson #endif
2182bbee39c6SJeff Roberson 
2183fc03d22bSJeff Roberson zalloc_item:
2184e20a199fSJeff Roberson 	item = zone_alloc_item(zone, udata, flags);
2185fc03d22bSJeff Roberson 
2186e20a199fSJeff Roberson 	return (item);
2187bbee39c6SJeff Roberson }
2188bbee39c6SJeff Roberson 
2189bbee39c6SJeff Roberson static uma_slab_t
2190e20a199fSJeff Roberson keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags)
2191bbee39c6SJeff Roberson {
2192bbee39c6SJeff Roberson 	uma_slab_t slab;
21936fd34d6fSJeff Roberson 	int reserve;
2194099a0e58SBosko Milekic 
2195e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2196bbee39c6SJeff Roberson 	slab = NULL;
21976fd34d6fSJeff Roberson 	reserve = 0;
21986fd34d6fSJeff Roberson 	if ((flags & M_USE_RESERVE) == 0)
21996fd34d6fSJeff Roberson 		reserve = keg->uk_reserve;
2200bbee39c6SJeff Roberson 
2201bbee39c6SJeff Roberson 	for (;;) {
2202bbee39c6SJeff Roberson 		/*
2203bbee39c6SJeff Roberson 		 * Find a slab with some space.  Prefer slabs that are partially
2204bbee39c6SJeff Roberson 		 * used over those that are totally full.  This helps to reduce
2205bbee39c6SJeff Roberson 		 * fragmentation.
2206bbee39c6SJeff Roberson 		 */
22076fd34d6fSJeff Roberson 		if (keg->uk_free > reserve) {
2208099a0e58SBosko Milekic 			if (!LIST_EMPTY(&keg->uk_part_slab)) {
2209099a0e58SBosko Milekic 				slab = LIST_FIRST(&keg->uk_part_slab);
2210bbee39c6SJeff Roberson 			} else {
2211099a0e58SBosko Milekic 				slab = LIST_FIRST(&keg->uk_free_slab);
2212bbee39c6SJeff Roberson 				LIST_REMOVE(slab, us_link);
2213099a0e58SBosko Milekic 				LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2214bbee39c6SJeff Roberson 				    us_link);
2215bbee39c6SJeff Roberson 			}
2216e20a199fSJeff Roberson 			MPASS(slab->us_keg == keg);
2217bbee39c6SJeff Roberson 			return (slab);
2218bbee39c6SJeff Roberson 		}
2219bbee39c6SJeff Roberson 
2220bbee39c6SJeff Roberson 		/*
2221bbee39c6SJeff Roberson 		 * M_NOVM means don't ask at all!
2222bbee39c6SJeff Roberson 		 */
2223bbee39c6SJeff Roberson 		if (flags & M_NOVM)
2224bbee39c6SJeff Roberson 			break;
2225bbee39c6SJeff Roberson 
2226e20a199fSJeff Roberson 		if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2227099a0e58SBosko Milekic 			keg->uk_flags |= UMA_ZFLAG_FULL;
2228e20a199fSJeff Roberson 			/*
2229e20a199fSJeff Roberson 			 * If this is not a multi-zone, set the FULL bit.
2230e20a199fSJeff Roberson 			 * Otherwise slab_multi() takes care of it.
2231e20a199fSJeff Roberson 			 */
22322f891cd5SPawel Jakub Dawidek 			if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) {
2233e20a199fSJeff Roberson 				zone->uz_flags |= UMA_ZFLAG_FULL;
22342f891cd5SPawel Jakub Dawidek 				zone_log_warning(zone);
22352f891cd5SPawel Jakub Dawidek 			}
2236ebc85edfSJeff Roberson 			if (flags & M_NOWAIT)
2237bbee39c6SJeff Roberson 				break;
2238c288b548SEitan Adler 			zone->uz_sleeps++;
2239e20a199fSJeff Roberson 			msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2240bbee39c6SJeff Roberson 			continue;
2241bbee39c6SJeff Roberson 		}
2242e20a199fSJeff Roberson 		slab = keg_alloc_slab(keg, zone, flags);
2243bbee39c6SJeff Roberson 		/*
2244bbee39c6SJeff Roberson 		 * If we got a slab here it's safe to mark it partially used
2245bbee39c6SJeff Roberson 		 * and return.  We assume that the caller is going to remove
2246bbee39c6SJeff Roberson 		 * at least one item.
2247bbee39c6SJeff Roberson 		 */
2248bbee39c6SJeff Roberson 		if (slab) {
2249e20a199fSJeff Roberson 			MPASS(slab->us_keg == keg);
2250099a0e58SBosko Milekic 			LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2251bbee39c6SJeff Roberson 			return (slab);
2252bbee39c6SJeff Roberson 		}
2253bbee39c6SJeff Roberson 		/*
2254bbee39c6SJeff Roberson 		 * We might not have been able to get a slab but another cpu
2255bbee39c6SJeff Roberson 		 * could have while we were unlocked.  Check again before we
2256bbee39c6SJeff Roberson 		 * fail.
2257bbee39c6SJeff Roberson 		 */
2258bbee39c6SJeff Roberson 		flags |= M_NOVM;
2259bbee39c6SJeff Roberson 	}
2260bbee39c6SJeff Roberson 	return (slab);
2261bbee39c6SJeff Roberson }
2262bbee39c6SJeff Roberson 
2263e20a199fSJeff Roberson static uma_slab_t
2264e20a199fSJeff Roberson zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags)
2265e20a199fSJeff Roberson {
2266e20a199fSJeff Roberson 	uma_slab_t slab;
2267e20a199fSJeff Roberson 
2268af526374SJeff Roberson 	if (keg == NULL) {
2269e20a199fSJeff Roberson 		keg = zone_first_keg(zone);
2270af526374SJeff Roberson 		KEG_LOCK(keg);
2271af526374SJeff Roberson 	}
2272e20a199fSJeff Roberson 
2273e20a199fSJeff Roberson 	for (;;) {
2274e20a199fSJeff Roberson 		slab = keg_fetch_slab(keg, zone, flags);
2275e20a199fSJeff Roberson 		if (slab)
2276e20a199fSJeff Roberson 			return (slab);
2277e20a199fSJeff Roberson 		if (flags & (M_NOWAIT | M_NOVM))
2278e20a199fSJeff Roberson 			break;
2279e20a199fSJeff Roberson 	}
2280af526374SJeff Roberson 	KEG_UNLOCK(keg);
2281e20a199fSJeff Roberson 	return (NULL);
2282e20a199fSJeff Roberson }
2283e20a199fSJeff Roberson 
2284e20a199fSJeff Roberson /*
2285e20a199fSJeff Roberson  * uma_zone_fetch_slab_multi:  Fetches a slab from one available keg.  Returns
2286af526374SJeff Roberson  * with the keg locked.  On NULL no lock is held.
2287e20a199fSJeff Roberson  *
2288e20a199fSJeff Roberson  * The last pointer is used to seed the search.  It is not required.
2289e20a199fSJeff Roberson  */
2290e20a199fSJeff Roberson static uma_slab_t
2291e20a199fSJeff Roberson zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags)
2292e20a199fSJeff Roberson {
2293e20a199fSJeff Roberson 	uma_klink_t klink;
2294e20a199fSJeff Roberson 	uma_slab_t slab;
2295e20a199fSJeff Roberson 	uma_keg_t keg;
2296e20a199fSJeff Roberson 	int flags;
2297e20a199fSJeff Roberson 	int empty;
2298e20a199fSJeff Roberson 	int full;
2299e20a199fSJeff Roberson 
2300e20a199fSJeff Roberson 	/*
2301e20a199fSJeff Roberson 	 * Don't wait on the first pass.  This will skip limit tests
2302e20a199fSJeff Roberson 	 * as well.  We don't want to block if we can find a provider
2303e20a199fSJeff Roberson 	 * without blocking.
2304e20a199fSJeff Roberson 	 */
2305e20a199fSJeff Roberson 	flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2306e20a199fSJeff Roberson 	/*
2307e20a199fSJeff Roberson 	 * Use the last slab allocated as a hint for where to start
2308e20a199fSJeff Roberson 	 * the search.
2309e20a199fSJeff Roberson 	 */
2310af526374SJeff Roberson 	if (last != NULL) {
2311e20a199fSJeff Roberson 		slab = keg_fetch_slab(last, zone, flags);
2312e20a199fSJeff Roberson 		if (slab)
2313e20a199fSJeff Roberson 			return (slab);
2314af526374SJeff Roberson 		KEG_UNLOCK(last);
2315e20a199fSJeff Roberson 	}
2316e20a199fSJeff Roberson 	/*
2317e20a199fSJeff Roberson 	 * Loop until we have a slab incase of transient failures
2318e20a199fSJeff Roberson 	 * while M_WAITOK is specified.  I'm not sure this is 100%
2319e20a199fSJeff Roberson 	 * required but we've done it for so long now.
2320e20a199fSJeff Roberson 	 */
2321e20a199fSJeff Roberson 	for (;;) {
2322e20a199fSJeff Roberson 		empty = 0;
2323e20a199fSJeff Roberson 		full = 0;
2324e20a199fSJeff Roberson 		/*
2325e20a199fSJeff Roberson 		 * Search the available kegs for slabs.  Be careful to hold the
2326e20a199fSJeff Roberson 		 * correct lock while calling into the keg layer.
2327e20a199fSJeff Roberson 		 */
2328e20a199fSJeff Roberson 		LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2329e20a199fSJeff Roberson 			keg = klink->kl_keg;
2330af526374SJeff Roberson 			KEG_LOCK(keg);
2331e20a199fSJeff Roberson 			if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2332e20a199fSJeff Roberson 				slab = keg_fetch_slab(keg, zone, flags);
2333e20a199fSJeff Roberson 				if (slab)
2334e20a199fSJeff Roberson 					return (slab);
2335e20a199fSJeff Roberson 			}
2336e20a199fSJeff Roberson 			if (keg->uk_flags & UMA_ZFLAG_FULL)
2337e20a199fSJeff Roberson 				full++;
2338e20a199fSJeff Roberson 			else
2339e20a199fSJeff Roberson 				empty++;
2340af526374SJeff Roberson 			KEG_UNLOCK(keg);
2341e20a199fSJeff Roberson 		}
2342e20a199fSJeff Roberson 		if (rflags & (M_NOWAIT | M_NOVM))
2343e20a199fSJeff Roberson 			break;
2344e20a199fSJeff Roberson 		flags = rflags;
2345e20a199fSJeff Roberson 		/*
2346e20a199fSJeff Roberson 		 * All kegs are full.  XXX We can't atomically check all kegs
2347e20a199fSJeff Roberson 		 * and sleep so just sleep for a short period and retry.
2348e20a199fSJeff Roberson 		 */
2349e20a199fSJeff Roberson 		if (full && !empty) {
2350af526374SJeff Roberson 			ZONE_LOCK(zone);
2351e20a199fSJeff Roberson 			zone->uz_flags |= UMA_ZFLAG_FULL;
2352bf965959SSean Bruno 			zone->uz_sleeps++;
23532f891cd5SPawel Jakub Dawidek 			zone_log_warning(zone);
2354af526374SJeff Roberson 			msleep(zone, zone->uz_lockptr, PVM,
2355af526374SJeff Roberson 			    "zonelimit", hz/100);
2356e20a199fSJeff Roberson 			zone->uz_flags &= ~UMA_ZFLAG_FULL;
2357af526374SJeff Roberson 			ZONE_UNLOCK(zone);
2358e20a199fSJeff Roberson 			continue;
2359e20a199fSJeff Roberson 		}
2360e20a199fSJeff Roberson 	}
2361e20a199fSJeff Roberson 	return (NULL);
2362e20a199fSJeff Roberson }
2363e20a199fSJeff Roberson 
2364d56368d7SBosko Milekic static void *
23650095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
2366bbee39c6SJeff Roberson {
2367bbee39c6SJeff Roberson 	void *item;
236885dcf349SGleb Smirnoff 	uint8_t freei;
2369bbee39c6SJeff Roberson 
23700095a784SJeff Roberson 	MPASS(keg == slab->us_keg);
2371e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2372099a0e58SBosko Milekic 
2373ef72505eSJeff Roberson 	freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2374ef72505eSJeff Roberson 	BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2375099a0e58SBosko Milekic 	item = slab->us_data + (keg->uk_rsize * freei);
2376bbee39c6SJeff Roberson 	slab->us_freecount--;
2377099a0e58SBosko Milekic 	keg->uk_free--;
2378ef72505eSJeff Roberson 
2379bbee39c6SJeff Roberson 	/* Move this slab to the full list */
2380bbee39c6SJeff Roberson 	if (slab->us_freecount == 0) {
2381bbee39c6SJeff Roberson 		LIST_REMOVE(slab, us_link);
2382099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2383bbee39c6SJeff Roberson 	}
2384bbee39c6SJeff Roberson 
2385bbee39c6SJeff Roberson 	return (item);
2386bbee39c6SJeff Roberson }
2387bbee39c6SJeff Roberson 
2388bbee39c6SJeff Roberson static int
23890095a784SJeff Roberson zone_import(uma_zone_t zone, void **bucket, int max, int flags)
23900095a784SJeff Roberson {
23910095a784SJeff Roberson 	uma_slab_t slab;
23920095a784SJeff Roberson 	uma_keg_t keg;
23930095a784SJeff Roberson 	int i;
23940095a784SJeff Roberson 
23950095a784SJeff Roberson 	slab = NULL;
23960095a784SJeff Roberson 	keg = NULL;
2397af526374SJeff Roberson 	/* Try to keep the buckets totally full */
23980095a784SJeff Roberson 	for (i = 0; i < max; ) {
23990095a784SJeff Roberson 		if ((slab = zone->uz_slab(zone, keg, flags)) == NULL)
24000095a784SJeff Roberson 			break;
24010095a784SJeff Roberson 		keg = slab->us_keg;
24026fd34d6fSJeff Roberson 		while (slab->us_freecount && i < max) {
24030095a784SJeff Roberson 			bucket[i++] = slab_alloc_item(keg, slab);
24046fd34d6fSJeff Roberson 			if (keg->uk_free <= keg->uk_reserve)
24056fd34d6fSJeff Roberson 				break;
24066fd34d6fSJeff Roberson 		}
24076fd34d6fSJeff Roberson 		/* Don't grab more than one slab at a time. */
24080095a784SJeff Roberson 		flags &= ~M_WAITOK;
24090095a784SJeff Roberson 		flags |= M_NOWAIT;
24100095a784SJeff Roberson 	}
24110095a784SJeff Roberson 	if (slab != NULL)
24120095a784SJeff Roberson 		KEG_UNLOCK(keg);
24130095a784SJeff Roberson 
24140095a784SJeff Roberson 	return i;
24150095a784SJeff Roberson }
24160095a784SJeff Roberson 
2417fc03d22bSJeff Roberson static uma_bucket_t
24186fd34d6fSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int flags)
2419bbee39c6SJeff Roberson {
2420bbee39c6SJeff Roberson 	uma_bucket_t bucket;
24210095a784SJeff Roberson 	int max;
2422bbee39c6SJeff Roberson 
24236fd34d6fSJeff Roberson 	/* Don't wait for buckets, preserve caller's NOVM setting. */
24246fd34d6fSJeff Roberson 	bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
24250095a784SJeff Roberson 	if (bucket == NULL)
24260095a784SJeff Roberson 		goto out;
24270095a784SJeff Roberson 
2428af526374SJeff Roberson 	max = MIN(bucket->ub_entries, zone->uz_count);
24290095a784SJeff Roberson 	bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
24300095a784SJeff Roberson 	    max, flags);
24310095a784SJeff Roberson 
24320095a784SJeff Roberson 	/*
24330095a784SJeff Roberson 	 * Initialize the memory if necessary.
24340095a784SJeff Roberson 	 */
24350095a784SJeff Roberson 	if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
2436099a0e58SBosko Milekic 		int i;
2437bbee39c6SJeff Roberson 
24380095a784SJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
2439e20a199fSJeff Roberson 			if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
24400095a784SJeff Roberson 			    flags) != 0)
2441b23f72e9SBrian Feldman 				break;
2442b23f72e9SBrian Feldman 		/*
2443b23f72e9SBrian Feldman 		 * If we couldn't initialize the whole bucket, put the
2444b23f72e9SBrian Feldman 		 * rest back onto the freelist.
2445b23f72e9SBrian Feldman 		 */
2446b23f72e9SBrian Feldman 		if (i != bucket->ub_cnt) {
2447af526374SJeff Roberson 			zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
24480095a784SJeff Roberson 			    bucket->ub_cnt - i);
2449a5a262c6SBosko Milekic #ifdef INVARIANTS
24500095a784SJeff Roberson 			bzero(&bucket->ub_bucket[i],
24510095a784SJeff Roberson 			    sizeof(void *) * (bucket->ub_cnt - i));
2452a5a262c6SBosko Milekic #endif
2453b23f72e9SBrian Feldman 			bucket->ub_cnt = i;
2454b23f72e9SBrian Feldman 		}
2455099a0e58SBosko Milekic 	}
2456099a0e58SBosko Milekic 
24570095a784SJeff Roberson out:
2458fc03d22bSJeff Roberson 	if (bucket == NULL || bucket->ub_cnt == 0) {
24590095a784SJeff Roberson 		if (bucket != NULL)
24606fd34d6fSJeff Roberson 			bucket_free(zone, bucket, udata);
2461fc03d22bSJeff Roberson 		atomic_add_long(&zone->uz_fails, 1);
2462fc03d22bSJeff Roberson 		return (NULL);
2463bbee39c6SJeff Roberson 	}
2464fc03d22bSJeff Roberson 
2465fc03d22bSJeff Roberson 	return (bucket);
2466fc03d22bSJeff Roberson }
2467fc03d22bSJeff Roberson 
24688355f576SJeff Roberson /*
24690095a784SJeff Roberson  * Allocates a single item from a zone.
24708355f576SJeff Roberson  *
24718355f576SJeff Roberson  * Arguments
24728355f576SJeff Roberson  *	zone   The zone to alloc for.
24738355f576SJeff Roberson  *	udata  The data to be passed to the constructor.
2474a163d034SWarner Losh  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
24758355f576SJeff Roberson  *
24768355f576SJeff Roberson  * Returns
24778355f576SJeff Roberson  *	NULL if there is no memory and M_NOWAIT is set
2478bbee39c6SJeff Roberson  *	An item if successful
24798355f576SJeff Roberson  */
24808355f576SJeff Roberson 
24818355f576SJeff Roberson static void *
2482e20a199fSJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int flags)
24838355f576SJeff Roberson {
24848355f576SJeff Roberson 	void *item;
24858355f576SJeff Roberson 
24868355f576SJeff Roberson 	item = NULL;
24878355f576SJeff Roberson 
24888355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
24898355f576SJeff Roberson 	printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
24908355f576SJeff Roberson #endif
24910095a784SJeff Roberson 	if (zone->uz_import(zone->uz_arg, &item, 1, flags) != 1)
24920095a784SJeff Roberson 		goto fail;
24930095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, 1);
24948355f576SJeff Roberson 
2495099a0e58SBosko Milekic 	/*
2496099a0e58SBosko Milekic 	 * We have to call both the zone's init (not the keg's init)
2497099a0e58SBosko Milekic 	 * and the zone's ctor.  This is because the item is going from
2498099a0e58SBosko Milekic 	 * a keg slab directly to the user, and the user is expecting it
2499099a0e58SBosko Milekic 	 * to be both zone-init'd as well as zone-ctor'd.
2500099a0e58SBosko Milekic 	 */
2501b23f72e9SBrian Feldman 	if (zone->uz_init != NULL) {
2502e20a199fSJeff Roberson 		if (zone->uz_init(item, zone->uz_size, flags) != 0) {
25030095a784SJeff Roberson 			zone_free_item(zone, item, udata, SKIP_FINI);
25040095a784SJeff Roberson 			goto fail;
2505b23f72e9SBrian Feldman 		}
2506b23f72e9SBrian Feldman 	}
2507b23f72e9SBrian Feldman 	if (zone->uz_ctor != NULL) {
2508e20a199fSJeff Roberson 		if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
25090095a784SJeff Roberson 			zone_free_item(zone, item, udata, SKIP_DTOR);
25100095a784SJeff Roberson 			goto fail;
2511b23f72e9SBrian Feldman 		}
2512b23f72e9SBrian Feldman 	}
2513ef72505eSJeff Roberson #ifdef INVARIANTS
25140095a784SJeff Roberson 	uma_dbg_alloc(zone, NULL, item);
2515ef72505eSJeff Roberson #endif
25162cc35ff9SJeff Roberson 	if (flags & M_ZERO)
2517e20a199fSJeff Roberson 		bzero(item, zone->uz_size);
25188355f576SJeff Roberson 
25198355f576SJeff Roberson 	return (item);
25200095a784SJeff Roberson 
25210095a784SJeff Roberson fail:
25220095a784SJeff Roberson 	atomic_add_long(&zone->uz_fails, 1);
25230095a784SJeff Roberson 	return (NULL);
25248355f576SJeff Roberson }
25258355f576SJeff Roberson 
25268355f576SJeff Roberson /* See uma.h */
25278355f576SJeff Roberson void
25288355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
25298355f576SJeff Roberson {
25308355f576SJeff Roberson 	uma_cache_t cache;
25318355f576SJeff Roberson 	uma_bucket_t bucket;
2532*4d104ba0SAlexander Motin 	int lockfail;
25338355f576SJeff Roberson 	int cpu;
25348355f576SJeff Roberson 
25358355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1
25368355f576SJeff Roberson 	printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
25378355f576SJeff Roberson #endif
25383659f747SRobert Watson 	CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
25393659f747SRobert Watson 	    zone->uz_name);
25403659f747SRobert Watson 
254120ed0cb0SMatthew D Fleming         /* uma_zfree(..., NULL) does nothing, to match free(9). */
254220ed0cb0SMatthew D Fleming         if (item == NULL)
254320ed0cb0SMatthew D Fleming                 return;
25448d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
25458d689e04SGleb Smirnoff 	if (is_memguard_addr(item)) {
25468d689e04SGleb Smirnoff 		if (zone->uz_dtor != NULL && zone->uz_dtor != mtrash_dtor)
25478d689e04SGleb Smirnoff 			zone->uz_dtor(item, zone->uz_size, udata);
25488d689e04SGleb Smirnoff 		if (zone->uz_fini != NULL && zone->uz_fini != mtrash_fini)
25498d689e04SGleb Smirnoff 			zone->uz_fini(item, zone->uz_size);
25508d689e04SGleb Smirnoff 		memguard_free(item);
25518d689e04SGleb Smirnoff 		return;
25528d689e04SGleb Smirnoff 	}
25538d689e04SGleb Smirnoff #endif
25545d1ae027SRobert Watson #ifdef INVARIANTS
2555e20a199fSJeff Roberson 	if (zone->uz_flags & UMA_ZONE_MALLOC)
25565d1ae027SRobert Watson 		uma_dbg_free(zone, udata, item);
25575d1ae027SRobert Watson 	else
25585d1ae027SRobert Watson 		uma_dbg_free(zone, NULL, item);
25595d1ae027SRobert Watson #endif
2560fc03d22bSJeff Roberson 	if (zone->uz_dtor != NULL)
2561ef72505eSJeff Roberson 		zone->uz_dtor(item, zone->uz_size, udata);
2562ef72505eSJeff Roberson 
2563af7f9b97SJeff Roberson 	/*
2564af7f9b97SJeff Roberson 	 * The race here is acceptable.  If we miss it we'll just have to wait
2565af7f9b97SJeff Roberson 	 * a little longer for the limits to be reset.
2566af7f9b97SJeff Roberson 	 */
2567e20a199fSJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_FULL)
2568fc03d22bSJeff Roberson 		goto zfree_item;
2569af7f9b97SJeff Roberson 
25705d1ae027SRobert Watson 	/*
25715d1ae027SRobert Watson 	 * If possible, free to the per-CPU cache.  There are two
25725d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
25735d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
25745d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
25755d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
25765d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
25775d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to free to the
25785d1ae027SRobert Watson 	 * current cache; when we re-acquire the critical section, we must
25795d1ae027SRobert Watson 	 * detect and handle migration if it has occurred.
25805d1ae027SRobert Watson 	 */
2581a553d4b8SJeff Roberson zfree_restart:
25825d1ae027SRobert Watson 	critical_enter();
25835d1ae027SRobert Watson 	cpu = curcpu;
25848355f576SJeff Roberson 	cache = &zone->uz_cpu[cpu];
25858355f576SJeff Roberson 
25868355f576SJeff Roberson zfree_start:
2587a553d4b8SJeff Roberson 	/*
2588fc03d22bSJeff Roberson 	 * Try to free into the allocbucket first to give LIFO ordering
2589fc03d22bSJeff Roberson 	 * for cache-hot datastructures.  Spill over into the freebucket
2590fc03d22bSJeff Roberson 	 * if necessary.  Alloc will swap them if one runs dry.
2591a553d4b8SJeff Roberson 	 */
2592fc03d22bSJeff Roberson 	bucket = cache->uc_allocbucket;
2593fc03d22bSJeff Roberson 	if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries)
2594fc03d22bSJeff Roberson 		bucket = cache->uc_freebucket;
2595fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2596cae33c14SJeff Roberson 		KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
25978355f576SJeff Roberson 		    ("uma_zfree: Freeing to non free bucket index."));
2598cae33c14SJeff Roberson 		bucket->ub_bucket[bucket->ub_cnt] = item;
2599cae33c14SJeff Roberson 		bucket->ub_cnt++;
2600773df9abSRobert Watson 		cache->uc_frees++;
26015d1ae027SRobert Watson 		critical_exit();
26028355f576SJeff Roberson 		return;
2603fc03d22bSJeff Roberson 	}
2604fc03d22bSJeff Roberson 
26058355f576SJeff Roberson 	/*
26065d1ae027SRobert Watson 	 * We must go back the zone, which requires acquiring the zone lock,
26075d1ae027SRobert Watson 	 * which in turn means we must release and re-acquire the critical
26085d1ae027SRobert Watson 	 * section.  Since the critical section is released, we may be
26095d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
26105d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
26115d1ae027SRobert Watson 	 * the critical section.
26128355f576SJeff Roberson 	 */
26135d1ae027SRobert Watson 	critical_exit();
2614fc03d22bSJeff Roberson 	if (zone->uz_count == 0 || bucketdisable)
2615fc03d22bSJeff Roberson 		goto zfree_item;
2616fc03d22bSJeff Roberson 
2617*4d104ba0SAlexander Motin 	lockfail = 0;
2618*4d104ba0SAlexander Motin 	if (ZONE_TRYLOCK(zone) == 0) {
2619*4d104ba0SAlexander Motin 		/* Record contention to size the buckets. */
26208355f576SJeff Roberson 		ZONE_LOCK(zone);
2621*4d104ba0SAlexander Motin 		lockfail = 1;
2622*4d104ba0SAlexander Motin 	}
26235d1ae027SRobert Watson 	critical_enter();
26245d1ae027SRobert Watson 	cpu = curcpu;
26255d1ae027SRobert Watson 	cache = &zone->uz_cpu[cpu];
26268355f576SJeff Roberson 
2627fc03d22bSJeff Roberson 	/*
2628fc03d22bSJeff Roberson 	 * Since we have locked the zone we may as well send back our stats.
2629fc03d22bSJeff Roberson 	 */
26300095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
26310095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, cache->uc_frees);
2632f4ff923bSRobert Watson 	cache->uc_allocs = 0;
2633f4ff923bSRobert Watson 	cache->uc_frees = 0;
2634f4ff923bSRobert Watson 
26358355f576SJeff Roberson 	bucket = cache->uc_freebucket;
2636fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2637fc03d22bSJeff Roberson 		ZONE_UNLOCK(zone);
2638fc03d22bSJeff Roberson 		goto zfree_start;
2639fc03d22bSJeff Roberson 	}
26408355f576SJeff Roberson 	cache->uc_freebucket = NULL;
26418355f576SJeff Roberson 
26428355f576SJeff Roberson 	/* Can we throw this on the zone full list? */
26438355f576SJeff Roberson 	if (bucket != NULL) {
26448355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
26458355f576SJeff Roberson 		printf("uma_zfree: Putting old bucket on the free list.\n");
26468355f576SJeff Roberson #endif
2647cae33c14SJeff Roberson 		/* ub_cnt is pointing to the last free item */
2648cae33c14SJeff Roberson 		KASSERT(bucket->ub_cnt != 0,
26498355f576SJeff Roberson 		    ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2650fc03d22bSJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
26518355f576SJeff Roberson 	}
2652fc03d22bSJeff Roberson 
26535d1ae027SRobert Watson 	/* We are no longer associated with this CPU. */
26545d1ae027SRobert Watson 	critical_exit();
2655a553d4b8SJeff Roberson 
2656*4d104ba0SAlexander Motin 	/*
2657*4d104ba0SAlexander Motin 	 * We bump the uz count when the cache size is insufficient to
2658*4d104ba0SAlexander Motin 	 * handle the working set.
2659*4d104ba0SAlexander Motin 	 */
2660*4d104ba0SAlexander Motin 	if (lockfail && zone->uz_count < BUCKET_MAX)
2661*4d104ba0SAlexander Motin 		zone->uz_count++;
2662a553d4b8SJeff Roberson 	ZONE_UNLOCK(zone);
2663a553d4b8SJeff Roberson 
26648355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
26658355f576SJeff Roberson 	printf("uma_zfree: Allocating new free bucket.\n");
26668355f576SJeff Roberson #endif
26676fd34d6fSJeff Roberson 	bucket = bucket_alloc(zone, udata, M_NOWAIT);
26684741dcbfSJeff Roberson 	if (bucket) {
2669fc03d22bSJeff Roberson 		critical_enter();
2670fc03d22bSJeff Roberson 		cpu = curcpu;
2671fc03d22bSJeff Roberson 		cache = &zone->uz_cpu[cpu];
2672fc03d22bSJeff Roberson 		if (cache->uc_freebucket == NULL) {
2673fc03d22bSJeff Roberson 			cache->uc_freebucket = bucket;
2674fc03d22bSJeff Roberson 			goto zfree_start;
2675fc03d22bSJeff Roberson 		}
2676fc03d22bSJeff Roberson 		/*
2677fc03d22bSJeff Roberson 		 * We lost the race, start over.  We have to drop our
2678fc03d22bSJeff Roberson 		 * critical section to free the bucket.
2679fc03d22bSJeff Roberson 		 */
2680fc03d22bSJeff Roberson 		critical_exit();
26816fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
2682a553d4b8SJeff Roberson 		goto zfree_restart;
26838355f576SJeff Roberson 	}
26848355f576SJeff Roberson 
2685a553d4b8SJeff Roberson 	/*
2686a553d4b8SJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
2687a553d4b8SJeff Roberson 	 */
2688fc03d22bSJeff Roberson zfree_item:
26890095a784SJeff Roberson 	zone_free_item(zone, item, udata, SKIP_DTOR);
26908355f576SJeff Roberson 
26918355f576SJeff Roberson 	return;
26928355f576SJeff Roberson }
26938355f576SJeff Roberson 
26948355f576SJeff Roberson static void
26950095a784SJeff Roberson slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item)
26968355f576SJeff Roberson {
269785dcf349SGleb Smirnoff 	uint8_t freei;
2698099a0e58SBosko Milekic 
26990095a784SJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2700e20a199fSJeff Roberson 	MPASS(keg == slab->us_keg);
27018355f576SJeff Roberson 
27028355f576SJeff Roberson 	/* Do we need to remove from any lists? */
2703099a0e58SBosko Milekic 	if (slab->us_freecount+1 == keg->uk_ipers) {
27048355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
2705099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
27068355f576SJeff Roberson 	} else if (slab->us_freecount == 0) {
27078355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
2708099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
27098355f576SJeff Roberson 	}
27108355f576SJeff Roberson 
2711ef72505eSJeff Roberson 	/* Slab management. */
2712ef72505eSJeff Roberson 	freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
2713ef72505eSJeff Roberson 	BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
27148355f576SJeff Roberson 	slab->us_freecount++;
27158355f576SJeff Roberson 
2716ef72505eSJeff Roberson 	/* Keg statistics. */
2717099a0e58SBosko Milekic 	keg->uk_free++;
27180095a784SJeff Roberson }
27190095a784SJeff Roberson 
27200095a784SJeff Roberson static void
27210095a784SJeff Roberson zone_release(uma_zone_t zone, void **bucket, int cnt)
27220095a784SJeff Roberson {
27230095a784SJeff Roberson 	void *item;
27240095a784SJeff Roberson 	uma_slab_t slab;
27250095a784SJeff Roberson 	uma_keg_t keg;
27260095a784SJeff Roberson 	uint8_t *mem;
27270095a784SJeff Roberson 	int clearfull;
27280095a784SJeff Roberson 	int i;
27298355f576SJeff Roberson 
2730e20a199fSJeff Roberson 	clearfull = 0;
27310095a784SJeff Roberson 	keg = zone_first_keg(zone);
2732af526374SJeff Roberson 	KEG_LOCK(keg);
27330095a784SJeff Roberson 	for (i = 0; i < cnt; i++) {
27340095a784SJeff Roberson 		item = bucket[i];
27350095a784SJeff Roberson 		if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
27360095a784SJeff Roberson 			mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
27370095a784SJeff Roberson 			if (zone->uz_flags & UMA_ZONE_HASH) {
27380095a784SJeff Roberson 				slab = hash_sfind(&keg->uk_hash, mem);
27390095a784SJeff Roberson 			} else {
27400095a784SJeff Roberson 				mem += keg->uk_pgoff;
27410095a784SJeff Roberson 				slab = (uma_slab_t)mem;
27420095a784SJeff Roberson 			}
27430095a784SJeff Roberson 		} else {
27440095a784SJeff Roberson 			slab = vtoslab((vm_offset_t)item);
27450095a784SJeff Roberson 			if (slab->us_keg != keg) {
27460095a784SJeff Roberson 				KEG_UNLOCK(keg);
27470095a784SJeff Roberson 				keg = slab->us_keg;
27480095a784SJeff Roberson 				KEG_LOCK(keg);
27490095a784SJeff Roberson 			}
27500095a784SJeff Roberson 		}
27510095a784SJeff Roberson 		slab_free_item(keg, slab, item);
2752099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZFLAG_FULL) {
2753e20a199fSJeff Roberson 			if (keg->uk_pages < keg->uk_maxpages) {
2754099a0e58SBosko Milekic 				keg->uk_flags &= ~UMA_ZFLAG_FULL;
2755e20a199fSJeff Roberson 				clearfull = 1;
2756e20a199fSJeff Roberson 			}
2757af7f9b97SJeff Roberson 
275877380291SMohan Srinivasan 			/*
2759ef72505eSJeff Roberson 			 * We can handle one more allocation. Since we're
2760ef72505eSJeff Roberson 			 * clearing ZFLAG_FULL, wake up all procs blocked
2761ef72505eSJeff Roberson 			 * on pages. This should be uncommon, so keeping this
2762ef72505eSJeff Roberson 			 * simple for now (rather than adding count of blocked
276377380291SMohan Srinivasan 			 * threads etc).
276477380291SMohan Srinivasan 			 */
276577380291SMohan Srinivasan 			wakeup(keg);
2766af7f9b97SJeff Roberson 		}
27670095a784SJeff Roberson 	}
2768af526374SJeff Roberson 	KEG_UNLOCK(keg);
27690095a784SJeff Roberson 	if (clearfull) {
2770af526374SJeff Roberson 		ZONE_LOCK(zone);
2771e20a199fSJeff Roberson 		zone->uz_flags &= ~UMA_ZFLAG_FULL;
2772e20a199fSJeff Roberson 		wakeup(zone);
2773605cbd6aSJeff Roberson 		ZONE_UNLOCK(zone);
2774af526374SJeff Roberson 	}
2775ef72505eSJeff Roberson 
27768355f576SJeff Roberson }
27778355f576SJeff Roberson 
27780095a784SJeff Roberson /*
27790095a784SJeff Roberson  * Frees a single item to any zone.
27800095a784SJeff Roberson  *
27810095a784SJeff Roberson  * Arguments:
27820095a784SJeff Roberson  *	zone   The zone to free to
27830095a784SJeff Roberson  *	item   The item we're freeing
27840095a784SJeff Roberson  *	udata  User supplied data for the dtor
27850095a784SJeff Roberson  *	skip   Skip dtors and finis
27860095a784SJeff Roberson  */
27870095a784SJeff Roberson static void
27880095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
27890095a784SJeff Roberson {
27900095a784SJeff Roberson 
27910095a784SJeff Roberson #ifdef INVARIANTS
27920095a784SJeff Roberson 	if (skip == SKIP_NONE) {
27930095a784SJeff Roberson 		if (zone->uz_flags & UMA_ZONE_MALLOC)
27940095a784SJeff Roberson 			uma_dbg_free(zone, udata, item);
27950095a784SJeff Roberson 		else
27960095a784SJeff Roberson 			uma_dbg_free(zone, NULL, item);
27970095a784SJeff Roberson 	}
27980095a784SJeff Roberson #endif
27990095a784SJeff Roberson 	if (skip < SKIP_DTOR && zone->uz_dtor)
28000095a784SJeff Roberson 		zone->uz_dtor(item, zone->uz_size, udata);
28010095a784SJeff Roberson 
28020095a784SJeff Roberson 	if (skip < SKIP_FINI && zone->uz_fini)
28030095a784SJeff Roberson 		zone->uz_fini(item, zone->uz_size);
28040095a784SJeff Roberson 
28050095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, 1);
28060095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, &item, 1);
28070095a784SJeff Roberson }
28080095a784SJeff Roberson 
28098355f576SJeff Roberson /* See uma.h */
28101c6cae97SLawrence Stewart int
2811736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems)
2812736ee590SJeff Roberson {
2813099a0e58SBosko Milekic 	uma_keg_t keg;
2814099a0e58SBosko Milekic 
2815e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
28160095a784SJeff Roberson 	if (keg == NULL)
28170095a784SJeff Roberson 		return (0);
2818af526374SJeff Roberson 	KEG_LOCK(keg);
2819e20a199fSJeff Roberson 	keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
2820099a0e58SBosko Milekic 	if (keg->uk_maxpages * keg->uk_ipers < nitems)
2821e20a199fSJeff Roberson 		keg->uk_maxpages += keg->uk_ppera;
28221c6cae97SLawrence Stewart 	nitems = keg->uk_maxpages * keg->uk_ipers;
2823af526374SJeff Roberson 	KEG_UNLOCK(keg);
28241c6cae97SLawrence Stewart 
28251c6cae97SLawrence Stewart 	return (nitems);
2826736ee590SJeff Roberson }
2827736ee590SJeff Roberson 
2828736ee590SJeff Roberson /* See uma.h */
2829e49471b0SAndre Oppermann int
2830e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone)
2831e49471b0SAndre Oppermann {
2832e49471b0SAndre Oppermann 	int nitems;
2833e49471b0SAndre Oppermann 	uma_keg_t keg;
2834e49471b0SAndre Oppermann 
2835e49471b0SAndre Oppermann 	keg = zone_first_keg(zone);
28360095a784SJeff Roberson 	if (keg == NULL)
28370095a784SJeff Roberson 		return (0);
2838af526374SJeff Roberson 	KEG_LOCK(keg);
2839e49471b0SAndre Oppermann 	nitems = keg->uk_maxpages * keg->uk_ipers;
2840af526374SJeff Roberson 	KEG_UNLOCK(keg);
2841e49471b0SAndre Oppermann 
2842e49471b0SAndre Oppermann 	return (nitems);
2843e49471b0SAndre Oppermann }
2844e49471b0SAndre Oppermann 
2845e49471b0SAndre Oppermann /* See uma.h */
28462f891cd5SPawel Jakub Dawidek void
28472f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning)
28482f891cd5SPawel Jakub Dawidek {
28492f891cd5SPawel Jakub Dawidek 
28502f891cd5SPawel Jakub Dawidek 	ZONE_LOCK(zone);
28512f891cd5SPawel Jakub Dawidek 	zone->uz_warning = warning;
28522f891cd5SPawel Jakub Dawidek 	ZONE_UNLOCK(zone);
28532f891cd5SPawel Jakub Dawidek }
28542f891cd5SPawel Jakub Dawidek 
28552f891cd5SPawel Jakub Dawidek /* See uma.h */
2856c4ae7908SLawrence Stewart int
2857c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone)
2858c4ae7908SLawrence Stewart {
2859c4ae7908SLawrence Stewart 	int64_t nitems;
2860c4ae7908SLawrence Stewart 	u_int i;
2861c4ae7908SLawrence Stewart 
2862c4ae7908SLawrence Stewart 	ZONE_LOCK(zone);
2863c4ae7908SLawrence Stewart 	nitems = zone->uz_allocs - zone->uz_frees;
2864c4ae7908SLawrence Stewart 	CPU_FOREACH(i) {
2865c4ae7908SLawrence Stewart 		/*
2866c4ae7908SLawrence Stewart 		 * See the comment in sysctl_vm_zone_stats() regarding the
2867c4ae7908SLawrence Stewart 		 * safety of accessing the per-cpu caches. With the zone lock
2868c4ae7908SLawrence Stewart 		 * held, it is safe, but can potentially result in stale data.
2869c4ae7908SLawrence Stewart 		 */
2870c4ae7908SLawrence Stewart 		nitems += zone->uz_cpu[i].uc_allocs -
2871c4ae7908SLawrence Stewart 		    zone->uz_cpu[i].uc_frees;
2872c4ae7908SLawrence Stewart 	}
2873c4ae7908SLawrence Stewart 	ZONE_UNLOCK(zone);
2874c4ae7908SLawrence Stewart 
2875c4ae7908SLawrence Stewart 	return (nitems < 0 ? 0 : nitems);
2876c4ae7908SLawrence Stewart }
2877c4ae7908SLawrence Stewart 
2878c4ae7908SLawrence Stewart /* See uma.h */
2879736ee590SJeff Roberson void
2880099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2881099a0e58SBosko Milekic {
2882e20a199fSJeff Roberson 	uma_keg_t keg;
2883e20a199fSJeff Roberson 
2884e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
28850095a784SJeff Roberson 	KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
2886af526374SJeff Roberson 	KEG_LOCK(keg);
2887e20a199fSJeff Roberson 	KASSERT(keg->uk_pages == 0,
2888099a0e58SBosko Milekic 	    ("uma_zone_set_init on non-empty keg"));
2889e20a199fSJeff Roberson 	keg->uk_init = uminit;
2890af526374SJeff Roberson 	KEG_UNLOCK(keg);
2891099a0e58SBosko Milekic }
2892099a0e58SBosko Milekic 
2893099a0e58SBosko Milekic /* See uma.h */
2894099a0e58SBosko Milekic void
2895099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2896099a0e58SBosko Milekic {
2897e20a199fSJeff Roberson 	uma_keg_t keg;
2898e20a199fSJeff Roberson 
2899e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
29000095a784SJeff Roberson 	KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
2901af526374SJeff Roberson 	KEG_LOCK(keg);
2902e20a199fSJeff Roberson 	KASSERT(keg->uk_pages == 0,
2903099a0e58SBosko Milekic 	    ("uma_zone_set_fini on non-empty keg"));
2904e20a199fSJeff Roberson 	keg->uk_fini = fini;
2905af526374SJeff Roberson 	KEG_UNLOCK(keg);
2906099a0e58SBosko Milekic }
2907099a0e58SBosko Milekic 
2908099a0e58SBosko Milekic /* See uma.h */
2909099a0e58SBosko Milekic void
2910099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2911099a0e58SBosko Milekic {
2912af526374SJeff Roberson 
2913099a0e58SBosko Milekic 	ZONE_LOCK(zone);
2914e20a199fSJeff Roberson 	KASSERT(zone_first_keg(zone)->uk_pages == 0,
2915099a0e58SBosko Milekic 	    ("uma_zone_set_zinit on non-empty keg"));
2916099a0e58SBosko Milekic 	zone->uz_init = zinit;
2917099a0e58SBosko Milekic 	ZONE_UNLOCK(zone);
2918099a0e58SBosko Milekic }
2919099a0e58SBosko Milekic 
2920099a0e58SBosko Milekic /* See uma.h */
2921099a0e58SBosko Milekic void
2922099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2923099a0e58SBosko Milekic {
2924af526374SJeff Roberson 
2925099a0e58SBosko Milekic 	ZONE_LOCK(zone);
2926e20a199fSJeff Roberson 	KASSERT(zone_first_keg(zone)->uk_pages == 0,
2927099a0e58SBosko Milekic 	    ("uma_zone_set_zfini on non-empty keg"));
2928099a0e58SBosko Milekic 	zone->uz_fini = zfini;
2929099a0e58SBosko Milekic 	ZONE_UNLOCK(zone);
2930099a0e58SBosko Milekic }
2931099a0e58SBosko Milekic 
2932099a0e58SBosko Milekic /* See uma.h */
2933b23f72e9SBrian Feldman /* XXX uk_freef is not actually used with the zone locked */
2934099a0e58SBosko Milekic void
29358355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef)
29368355f576SJeff Roberson {
29370095a784SJeff Roberson 	uma_keg_t keg;
2938e20a199fSJeff Roberson 
29390095a784SJeff Roberson 	keg = zone_first_keg(zone);
29400095a784SJeff Roberson 	KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
2941af526374SJeff Roberson 	KEG_LOCK(keg);
29420095a784SJeff Roberson 	keg->uk_freef = freef;
2943af526374SJeff Roberson 	KEG_UNLOCK(keg);
29448355f576SJeff Roberson }
29458355f576SJeff Roberson 
29468355f576SJeff Roberson /* See uma.h */
2947b23f72e9SBrian Feldman /* XXX uk_allocf is not actually used with the zone locked */
29488355f576SJeff Roberson void
29498355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
29508355f576SJeff Roberson {
2951e20a199fSJeff Roberson 	uma_keg_t keg;
2952e20a199fSJeff Roberson 
2953e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
2954af526374SJeff Roberson 	KEG_LOCK(keg);
2955e20a199fSJeff Roberson 	keg->uk_allocf = allocf;
2956af526374SJeff Roberson 	KEG_UNLOCK(keg);
29578355f576SJeff Roberson }
29588355f576SJeff Roberson 
29598355f576SJeff Roberson /* See uma.h */
29606fd34d6fSJeff Roberson void
29616fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items)
29626fd34d6fSJeff Roberson {
29636fd34d6fSJeff Roberson 	uma_keg_t keg;
29646fd34d6fSJeff Roberson 
29656fd34d6fSJeff Roberson 	keg = zone_first_keg(zone);
29666fd34d6fSJeff Roberson 	if (keg == NULL)
29676fd34d6fSJeff Roberson 		return;
29686fd34d6fSJeff Roberson 	KEG_LOCK(keg);
29696fd34d6fSJeff Roberson 	keg->uk_reserve = items;
29706fd34d6fSJeff Roberson 	KEG_UNLOCK(keg);
29716fd34d6fSJeff Roberson 
29726fd34d6fSJeff Roberson 	return;
29736fd34d6fSJeff Roberson }
29746fd34d6fSJeff Roberson 
29756fd34d6fSJeff Roberson /* See uma.h */
29768355f576SJeff Roberson int
2977a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count)
29788355f576SJeff Roberson {
2979099a0e58SBosko Milekic 	uma_keg_t keg;
29808355f576SJeff Roberson 	vm_offset_t kva;
2981099a0e58SBosko Milekic 	int pages;
29828355f576SJeff Roberson 
2983e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
29840095a784SJeff Roberson 	if (keg == NULL)
29850095a784SJeff Roberson 		return (0);
2986099a0e58SBosko Milekic 	pages = count / keg->uk_ipers;
29878355f576SJeff Roberson 
2988099a0e58SBosko Milekic 	if (pages * keg->uk_ipers < count)
29898355f576SJeff Roberson 		pages++;
2990a553d4b8SJeff Roberson 
2991a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
2992a4915c21SAttilio Rao 	if (keg->uk_ppera > 1) {
2993a4915c21SAttilio Rao #else
2994a4915c21SAttilio Rao 	if (1) {
2995a4915c21SAttilio Rao #endif
29965df87b21SJeff Roberson 		kva = kva_alloc(pages * UMA_SLAB_SIZE);
2997d1f42ac2SAlan Cox 		if (kva == 0)
29988355f576SJeff Roberson 			return (0);
2999a4915c21SAttilio Rao 	} else
3000a4915c21SAttilio Rao 		kva = 0;
3001af526374SJeff Roberson 	KEG_LOCK(keg);
3002099a0e58SBosko Milekic 	keg->uk_kva = kva;
3003a4915c21SAttilio Rao 	keg->uk_offset = 0;
3004099a0e58SBosko Milekic 	keg->uk_maxpages = pages;
3005a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
3006a4915c21SAttilio Rao 	keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
3007a4915c21SAttilio Rao #else
3008a4915c21SAttilio Rao 	keg->uk_allocf = noobj_alloc;
3009a4915c21SAttilio Rao #endif
30106fd34d6fSJeff Roberson 	keg->uk_flags |= UMA_ZONE_NOFREE;
3011af526374SJeff Roberson 	KEG_UNLOCK(keg);
3012af526374SJeff Roberson 
30138355f576SJeff Roberson 	return (1);
30148355f576SJeff Roberson }
30158355f576SJeff Roberson 
30168355f576SJeff Roberson /* See uma.h */
30178355f576SJeff Roberson void
30188355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items)
30198355f576SJeff Roberson {
30208355f576SJeff Roberson 	int slabs;
30218355f576SJeff Roberson 	uma_slab_t slab;
3022099a0e58SBosko Milekic 	uma_keg_t keg;
30238355f576SJeff Roberson 
3024e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
30250095a784SJeff Roberson 	if (keg == NULL)
30260095a784SJeff Roberson 		return;
3027af526374SJeff Roberson 	KEG_LOCK(keg);
3028099a0e58SBosko Milekic 	slabs = items / keg->uk_ipers;
3029099a0e58SBosko Milekic 	if (slabs * keg->uk_ipers < items)
30308355f576SJeff Roberson 		slabs++;
30318355f576SJeff Roberson 	while (slabs > 0) {
3032e20a199fSJeff Roberson 		slab = keg_alloc_slab(keg, zone, M_WAITOK);
3033e20a199fSJeff Roberson 		if (slab == NULL)
3034e20a199fSJeff Roberson 			break;
3035e20a199fSJeff Roberson 		MPASS(slab->us_keg == keg);
3036099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
30378355f576SJeff Roberson 		slabs--;
30388355f576SJeff Roberson 	}
3039af526374SJeff Roberson 	KEG_UNLOCK(keg);
30408355f576SJeff Roberson }
30418355f576SJeff Roberson 
30428355f576SJeff Roberson /* See uma.h */
304385dcf349SGleb Smirnoff uint32_t *
3044099a0e58SBosko Milekic uma_find_refcnt(uma_zone_t zone, void *item)
3045099a0e58SBosko Milekic {
3046ab14a3f7SBrian Feldman 	uma_slabrefcnt_t slabref;
3047ef72505eSJeff Roberson 	uma_slab_t slab;
3048099a0e58SBosko Milekic 	uma_keg_t keg;
304985dcf349SGleb Smirnoff 	uint32_t *refcnt;
3050099a0e58SBosko Milekic 	int idx;
3051099a0e58SBosko Milekic 
3052ef72505eSJeff Roberson 	slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK));
3053ef72505eSJeff Roberson 	slabref = (uma_slabrefcnt_t)slab;
3054ef72505eSJeff Roberson 	keg = slab->us_keg;
3055ef72505eSJeff Roberson 	KASSERT(keg->uk_flags & UMA_ZONE_REFCNT,
3056099a0e58SBosko Milekic 	    ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
3057ef72505eSJeff Roberson 	idx = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3058ef72505eSJeff Roberson 	refcnt = &slabref->us_refcnt[idx];
3059099a0e58SBosko Milekic 	return refcnt;
3060099a0e58SBosko Milekic }
3061099a0e58SBosko Milekic 
3062099a0e58SBosko Milekic /* See uma.h */
30638355f576SJeff Roberson void
30648355f576SJeff Roberson uma_reclaim(void)
30658355f576SJeff Roberson {
30668355f576SJeff Roberson #ifdef UMA_DEBUG
30678355f576SJeff Roberson 	printf("UMA: vm asked us to release pages!\n");
30688355f576SJeff Roberson #endif
306986bbae32SJeff Roberson 	bucket_enable();
30708355f576SJeff Roberson 	zone_foreach(zone_drain);
30718355f576SJeff Roberson 	/*
30728355f576SJeff Roberson 	 * Some slabs may have been freed but this zone will be visited early
30738355f576SJeff Roberson 	 * we visit again so that we can free pages that are empty once other
30748355f576SJeff Roberson 	 * zones are drained.  We have to do the same for buckets.
30758355f576SJeff Roberson 	 */
30769643769aSJeff Roberson 	zone_drain(slabzone);
3077099a0e58SBosko Milekic 	zone_drain(slabrefzone);
3078cae33c14SJeff Roberson 	bucket_zone_drain();
30798355f576SJeff Roberson }
30808355f576SJeff Roberson 
3081663b416fSJohn Baldwin /* See uma.h */
3082663b416fSJohn Baldwin int
3083663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone)
3084663b416fSJohn Baldwin {
3085663b416fSJohn Baldwin 	int full;
3086663b416fSJohn Baldwin 
3087663b416fSJohn Baldwin 	ZONE_LOCK(zone);
3088e20a199fSJeff Roberson 	full = (zone->uz_flags & UMA_ZFLAG_FULL);
3089663b416fSJohn Baldwin 	ZONE_UNLOCK(zone);
3090663b416fSJohn Baldwin 	return (full);
3091663b416fSJohn Baldwin }
3092663b416fSJohn Baldwin 
30936c125b8dSMohan Srinivasan int
30946c125b8dSMohan Srinivasan uma_zone_exhausted_nolock(uma_zone_t zone)
30956c125b8dSMohan Srinivasan {
3096e20a199fSJeff Roberson 	return (zone->uz_flags & UMA_ZFLAG_FULL);
30976c125b8dSMohan Srinivasan }
30986c125b8dSMohan Srinivasan 
30998355f576SJeff Roberson void *
31008355f576SJeff Roberson uma_large_malloc(int size, int wait)
31018355f576SJeff Roberson {
31028355f576SJeff Roberson 	void *mem;
31038355f576SJeff Roberson 	uma_slab_t slab;
310485dcf349SGleb Smirnoff 	uint8_t flags;
31058355f576SJeff Roberson 
3106e20a199fSJeff Roberson 	slab = zone_alloc_item(slabzone, NULL, wait);
31078355f576SJeff Roberson 	if (slab == NULL)
31088355f576SJeff Roberson 		return (NULL);
31098355f576SJeff Roberson 	mem = page_alloc(NULL, size, &flags, wait);
31108355f576SJeff Roberson 	if (mem) {
311199571dc3SJeff Roberson 		vsetslab((vm_offset_t)mem, slab);
31128355f576SJeff Roberson 		slab->us_data = mem;
31138355f576SJeff Roberson 		slab->us_flags = flags | UMA_SLAB_MALLOC;
31148355f576SJeff Roberson 		slab->us_size = size;
31158355f576SJeff Roberson 	} else {
31160095a784SJeff Roberson 		zone_free_item(slabzone, slab, NULL, SKIP_NONE);
31178355f576SJeff Roberson 	}
31188355f576SJeff Roberson 
31198355f576SJeff Roberson 	return (mem);
31208355f576SJeff Roberson }
31218355f576SJeff Roberson 
31228355f576SJeff Roberson void
31238355f576SJeff Roberson uma_large_free(uma_slab_t slab)
31248355f576SJeff Roberson {
3125c325e866SKonstantin Belousov 
31268355f576SJeff Roberson 	page_free(slab->us_data, slab->us_size, slab->us_flags);
31270095a784SJeff Roberson 	zone_free_item(slabzone, slab, NULL, SKIP_NONE);
31288355f576SJeff Roberson }
31298355f576SJeff Roberson 
31308355f576SJeff Roberson void
31318355f576SJeff Roberson uma_print_stats(void)
31328355f576SJeff Roberson {
31338355f576SJeff Roberson 	zone_foreach(uma_print_zone);
31348355f576SJeff Roberson }
31358355f576SJeff Roberson 
3136504d5de3SJeff Roberson static void
3137504d5de3SJeff Roberson slab_print(uma_slab_t slab)
3138504d5de3SJeff Roberson {
3139ef72505eSJeff Roberson 	printf("slab: keg %p, data %p, freecount %d\n",
3140ef72505eSJeff Roberson 		slab->us_keg, slab->us_data, slab->us_freecount);
3141504d5de3SJeff Roberson }
3142504d5de3SJeff Roberson 
3143504d5de3SJeff Roberson static void
3144504d5de3SJeff Roberson cache_print(uma_cache_t cache)
3145504d5de3SJeff Roberson {
3146504d5de3SJeff Roberson 	printf("alloc: %p(%d), free: %p(%d)\n",
3147504d5de3SJeff Roberson 		cache->uc_allocbucket,
3148504d5de3SJeff Roberson 		cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3149504d5de3SJeff Roberson 		cache->uc_freebucket,
3150504d5de3SJeff Roberson 		cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3151504d5de3SJeff Roberson }
3152504d5de3SJeff Roberson 
3153e20a199fSJeff Roberson static void
3154e20a199fSJeff Roberson uma_print_keg(uma_keg_t keg)
31558355f576SJeff Roberson {
3156504d5de3SJeff Roberson 	uma_slab_t slab;
3157504d5de3SJeff Roberson 
31580b80c1e4SEitan Adler 	printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3159e20a199fSJeff Roberson 	    "out %d free %d limit %d\n",
3160e20a199fSJeff Roberson 	    keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3161099a0e58SBosko Milekic 	    keg->uk_ipers, keg->uk_ppera,
3162e20a199fSJeff Roberson 	    (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free,
3163e20a199fSJeff Roberson 	    (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3164504d5de3SJeff Roberson 	printf("Part slabs:\n");
3165099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
3166504d5de3SJeff Roberson 		slab_print(slab);
3167504d5de3SJeff Roberson 	printf("Free slabs:\n");
3168099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
3169504d5de3SJeff Roberson 		slab_print(slab);
3170504d5de3SJeff Roberson 	printf("Full slabs:\n");
3171099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
3172504d5de3SJeff Roberson 		slab_print(slab);
3173e20a199fSJeff Roberson }
3174e20a199fSJeff Roberson 
3175e20a199fSJeff Roberson void
3176e20a199fSJeff Roberson uma_print_zone(uma_zone_t zone)
3177e20a199fSJeff Roberson {
3178e20a199fSJeff Roberson 	uma_cache_t cache;
3179e20a199fSJeff Roberson 	uma_klink_t kl;
3180e20a199fSJeff Roberson 	int i;
3181e20a199fSJeff Roberson 
31820b80c1e4SEitan Adler 	printf("zone: %s(%p) size %d flags %#x\n",
3183e20a199fSJeff Roberson 	    zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3184e20a199fSJeff Roberson 	LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3185e20a199fSJeff Roberson 		uma_print_keg(kl->kl_keg);
31863aa6d94eSJohn Baldwin 	CPU_FOREACH(i) {
3187504d5de3SJeff Roberson 		cache = &zone->uz_cpu[i];
3188504d5de3SJeff Roberson 		printf("CPU %d Cache:\n", i);
3189504d5de3SJeff Roberson 		cache_print(cache);
3190504d5de3SJeff Roberson 	}
31918355f576SJeff Roberson }
31928355f576SJeff Roberson 
3193a0d4b0aeSRobert Watson #ifdef DDB
31948355f576SJeff Roberson /*
31957a52a97eSRobert Watson  * Generate statistics across both the zone and its per-cpu cache's.  Return
31967a52a97eSRobert Watson  * desired statistics if the pointer is non-NULL for that statistic.
31977a52a97eSRobert Watson  *
31987a52a97eSRobert Watson  * Note: does not update the zone statistics, as it can't safely clear the
31997a52a97eSRobert Watson  * per-CPU cache statistic.
32007a52a97eSRobert Watson  *
32017a52a97eSRobert Watson  * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
32027a52a97eSRobert Watson  * safe from off-CPU; we should modify the caches to track this information
32037a52a97eSRobert Watson  * directly so that we don't have to.
32047a52a97eSRobert Watson  */
32057a52a97eSRobert Watson static void
320685dcf349SGleb Smirnoff uma_zone_sumstat(uma_zone_t z, int *cachefreep, uint64_t *allocsp,
320785dcf349SGleb Smirnoff     uint64_t *freesp, uint64_t *sleepsp)
32087a52a97eSRobert Watson {
32097a52a97eSRobert Watson 	uma_cache_t cache;
321085dcf349SGleb Smirnoff 	uint64_t allocs, frees, sleeps;
32117a52a97eSRobert Watson 	int cachefree, cpu;
32127a52a97eSRobert Watson 
3213bf965959SSean Bruno 	allocs = frees = sleeps = 0;
32147a52a97eSRobert Watson 	cachefree = 0;
32153aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
32167a52a97eSRobert Watson 		cache = &z->uz_cpu[cpu];
32177a52a97eSRobert Watson 		if (cache->uc_allocbucket != NULL)
32187a52a97eSRobert Watson 			cachefree += cache->uc_allocbucket->ub_cnt;
32197a52a97eSRobert Watson 		if (cache->uc_freebucket != NULL)
32207a52a97eSRobert Watson 			cachefree += cache->uc_freebucket->ub_cnt;
32217a52a97eSRobert Watson 		allocs += cache->uc_allocs;
32227a52a97eSRobert Watson 		frees += cache->uc_frees;
32237a52a97eSRobert Watson 	}
32247a52a97eSRobert Watson 	allocs += z->uz_allocs;
32257a52a97eSRobert Watson 	frees += z->uz_frees;
3226bf965959SSean Bruno 	sleeps += z->uz_sleeps;
32277a52a97eSRobert Watson 	if (cachefreep != NULL)
32287a52a97eSRobert Watson 		*cachefreep = cachefree;
32297a52a97eSRobert Watson 	if (allocsp != NULL)
32307a52a97eSRobert Watson 		*allocsp = allocs;
32317a52a97eSRobert Watson 	if (freesp != NULL)
32327a52a97eSRobert Watson 		*freesp = frees;
3233bf965959SSean Bruno 	if (sleepsp != NULL)
3234bf965959SSean Bruno 		*sleepsp = sleeps;
32357a52a97eSRobert Watson }
3236a0d4b0aeSRobert Watson #endif /* DDB */
32377a52a97eSRobert Watson 
32387a52a97eSRobert Watson static int
32397a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
32407a52a97eSRobert Watson {
32417a52a97eSRobert Watson 	uma_keg_t kz;
32427a52a97eSRobert Watson 	uma_zone_t z;
32437a52a97eSRobert Watson 	int count;
32447a52a97eSRobert Watson 
32457a52a97eSRobert Watson 	count = 0;
32467a52a97eSRobert Watson 	mtx_lock(&uma_mtx);
32477a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
32487a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
32497a52a97eSRobert Watson 			count++;
32507a52a97eSRobert Watson 	}
32517a52a97eSRobert Watson 	mtx_unlock(&uma_mtx);
32527a52a97eSRobert Watson 	return (sysctl_handle_int(oidp, &count, 0, req));
32537a52a97eSRobert Watson }
32547a52a97eSRobert Watson 
32557a52a97eSRobert Watson static int
32567a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
32577a52a97eSRobert Watson {
32587a52a97eSRobert Watson 	struct uma_stream_header ush;
32597a52a97eSRobert Watson 	struct uma_type_header uth;
32607a52a97eSRobert Watson 	struct uma_percpu_stat ups;
32617a52a97eSRobert Watson 	uma_bucket_t bucket;
32627a52a97eSRobert Watson 	struct sbuf sbuf;
32637a52a97eSRobert Watson 	uma_cache_t cache;
3264e20a199fSJeff Roberson 	uma_klink_t kl;
32657a52a97eSRobert Watson 	uma_keg_t kz;
32667a52a97eSRobert Watson 	uma_zone_t z;
3267e20a199fSJeff Roberson 	uma_keg_t k;
32684e657159SMatthew D Fleming 	int count, error, i;
32697a52a97eSRobert Watson 
327000f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
327100f0e671SMatthew D Fleming 	if (error != 0)
327200f0e671SMatthew D Fleming 		return (error);
32734e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
32744e657159SMatthew D Fleming 
3275404a593eSMatthew D Fleming 	count = 0;
32764e657159SMatthew D Fleming 	mtx_lock(&uma_mtx);
32777a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
32787a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
32797a52a97eSRobert Watson 			count++;
32807a52a97eSRobert Watson 	}
32817a52a97eSRobert Watson 
32827a52a97eSRobert Watson 	/*
32837a52a97eSRobert Watson 	 * Insert stream header.
32847a52a97eSRobert Watson 	 */
32857a52a97eSRobert Watson 	bzero(&ush, sizeof(ush));
32867a52a97eSRobert Watson 	ush.ush_version = UMA_STREAM_VERSION;
3287ab3a57c0SRobert Watson 	ush.ush_maxcpus = (mp_maxid + 1);
32887a52a97eSRobert Watson 	ush.ush_count = count;
32894e657159SMatthew D Fleming 	(void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
32907a52a97eSRobert Watson 
32917a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
32927a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
32937a52a97eSRobert Watson 			bzero(&uth, sizeof(uth));
32947a52a97eSRobert Watson 			ZONE_LOCK(z);
3295cbbb4a00SRobert Watson 			strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
32967a52a97eSRobert Watson 			uth.uth_align = kz->uk_align;
32977a52a97eSRobert Watson 			uth.uth_size = kz->uk_size;
32987a52a97eSRobert Watson 			uth.uth_rsize = kz->uk_rsize;
3299e20a199fSJeff Roberson 			LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3300e20a199fSJeff Roberson 				k = kl->kl_keg;
3301e20a199fSJeff Roberson 				uth.uth_maxpages += k->uk_maxpages;
3302e20a199fSJeff Roberson 				uth.uth_pages += k->uk_pages;
3303e20a199fSJeff Roberson 				uth.uth_keg_free += k->uk_free;
3304e20a199fSJeff Roberson 				uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3305e20a199fSJeff Roberson 				    * k->uk_ipers;
3306e20a199fSJeff Roberson 			}
3307cbbb4a00SRobert Watson 
3308cbbb4a00SRobert Watson 			/*
3309cbbb4a00SRobert Watson 			 * A zone is secondary is it is not the first entry
3310cbbb4a00SRobert Watson 			 * on the keg's zone list.
3311cbbb4a00SRobert Watson 			 */
3312e20a199fSJeff Roberson 			if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3313cbbb4a00SRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z))
3314cbbb4a00SRobert Watson 				uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3315cbbb4a00SRobert Watson 
3316fc03d22bSJeff Roberson 			LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
33177a52a97eSRobert Watson 				uth.uth_zone_free += bucket->ub_cnt;
33187a52a97eSRobert Watson 			uth.uth_allocs = z->uz_allocs;
33197a52a97eSRobert Watson 			uth.uth_frees = z->uz_frees;
33202019094aSRobert Watson 			uth.uth_fails = z->uz_fails;
3321bf965959SSean Bruno 			uth.uth_sleeps = z->uz_sleeps;
33224e657159SMatthew D Fleming 			(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
33237a52a97eSRobert Watson 			/*
33242450bbb8SRobert Watson 			 * While it is not normally safe to access the cache
33252450bbb8SRobert Watson 			 * bucket pointers while not on the CPU that owns the
33262450bbb8SRobert Watson 			 * cache, we only allow the pointers to be exchanged
33272450bbb8SRobert Watson 			 * without the zone lock held, not invalidated, so
33282450bbb8SRobert Watson 			 * accept the possible race associated with bucket
33292450bbb8SRobert Watson 			 * exchange during monitoring.
33307a52a97eSRobert Watson 			 */
3331ab3a57c0SRobert Watson 			for (i = 0; i < (mp_maxid + 1); i++) {
33327a52a97eSRobert Watson 				bzero(&ups, sizeof(ups));
33337a52a97eSRobert Watson 				if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
33347a52a97eSRobert Watson 					goto skip;
3335082dc776SRobert Watson 				if (CPU_ABSENT(i))
3336082dc776SRobert Watson 					goto skip;
33377a52a97eSRobert Watson 				cache = &z->uz_cpu[i];
33387a52a97eSRobert Watson 				if (cache->uc_allocbucket != NULL)
33397a52a97eSRobert Watson 					ups.ups_cache_free +=
33407a52a97eSRobert Watson 					    cache->uc_allocbucket->ub_cnt;
33417a52a97eSRobert Watson 				if (cache->uc_freebucket != NULL)
33427a52a97eSRobert Watson 					ups.ups_cache_free +=
33437a52a97eSRobert Watson 					    cache->uc_freebucket->ub_cnt;
33447a52a97eSRobert Watson 				ups.ups_allocs = cache->uc_allocs;
33457a52a97eSRobert Watson 				ups.ups_frees = cache->uc_frees;
33467a52a97eSRobert Watson skip:
33474e657159SMatthew D Fleming 				(void)sbuf_bcat(&sbuf, &ups, sizeof(ups));
33487a52a97eSRobert Watson 			}
33492450bbb8SRobert Watson 			ZONE_UNLOCK(z);
33507a52a97eSRobert Watson 		}
33517a52a97eSRobert Watson 	}
33527a52a97eSRobert Watson 	mtx_unlock(&uma_mtx);
33534e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
33544e657159SMatthew D Fleming 	sbuf_delete(&sbuf);
33557a52a97eSRobert Watson 	return (error);
33567a52a97eSRobert Watson }
335748c5777eSRobert Watson 
335848c5777eSRobert Watson #ifdef DDB
335948c5777eSRobert Watson DB_SHOW_COMMAND(uma, db_show_uma)
336048c5777eSRobert Watson {
336185dcf349SGleb Smirnoff 	uint64_t allocs, frees, sleeps;
336248c5777eSRobert Watson 	uma_bucket_t bucket;
336348c5777eSRobert Watson 	uma_keg_t kz;
336448c5777eSRobert Watson 	uma_zone_t z;
336548c5777eSRobert Watson 	int cachefree;
336648c5777eSRobert Watson 
3367bf965959SSean Bruno 	db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
3368bf965959SSean Bruno 	    "Requests", "Sleeps");
336948c5777eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
337048c5777eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
337148c5777eSRobert Watson 			if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
337248c5777eSRobert Watson 				allocs = z->uz_allocs;
337348c5777eSRobert Watson 				frees = z->uz_frees;
3374bf965959SSean Bruno 				sleeps = z->uz_sleeps;
337548c5777eSRobert Watson 				cachefree = 0;
337648c5777eSRobert Watson 			} else
337748c5777eSRobert Watson 				uma_zone_sumstat(z, &cachefree, &allocs,
3378bf965959SSean Bruno 				    &frees, &sleeps);
3379e20a199fSJeff Roberson 			if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
338048c5777eSRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z)))
338148c5777eSRobert Watson 				cachefree += kz->uk_free;
3382fc03d22bSJeff Roberson 			LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
338348c5777eSRobert Watson 				cachefree += bucket->ub_cnt;
3384bf965959SSean Bruno 			db_printf("%18s %8ju %8jd %8d %12ju %8ju\n", z->uz_name,
3385ae4e9636SRobert Watson 			    (uintmax_t)kz->uk_size,
3386ae4e9636SRobert Watson 			    (intmax_t)(allocs - frees), cachefree,
3387bf965959SSean Bruno 			    (uintmax_t)allocs, sleeps);
3388687c94aaSJohn Baldwin 			if (db_pager_quit)
3389687c94aaSJohn Baldwin 				return;
339048c5777eSRobert Watson 		}
339148c5777eSRobert Watson 	}
339248c5777eSRobert Watson }
339348c5777eSRobert Watson #endif
3394