xref: /freebsd/sys/vm/uma_core.c (revision a5a355788e27b33d3878c6f5604867b02e1bfa93)
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
34763df3ecSPedro F. Giffuni  * efficient.  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>
679b43bc27SAndriy Gapon #include <sys/eventhandler.h>
688355f576SJeff Roberson #include <sys/kernel.h>
698355f576SJeff Roberson #include <sys/types.h>
708355f576SJeff Roberson #include <sys/queue.h>
718355f576SJeff Roberson #include <sys/malloc.h>
723659f747SRobert Watson #include <sys/ktr.h>
738355f576SJeff Roberson #include <sys/lock.h>
748355f576SJeff Roberson #include <sys/sysctl.h>
758355f576SJeff Roberson #include <sys/mutex.h>
764c1cc01cSJohn Baldwin #include <sys/proc.h>
7710cb2424SMark Murray #include <sys/random.h>
7889f6b863SAttilio Rao #include <sys/rwlock.h>
797a52a97eSRobert Watson #include <sys/sbuf.h>
80a2de44abSAlexander Motin #include <sys/sched.h>
818355f576SJeff Roberson #include <sys/smp.h>
82e60b2fcbSGleb Smirnoff #include <sys/taskqueue.h>
8386bbae32SJeff Roberson #include <sys/vmmeter.h>
8486bbae32SJeff Roberson 
858355f576SJeff Roberson #include <vm/vm.h>
868355f576SJeff Roberson #include <vm/vm_object.h>
878355f576SJeff Roberson #include <vm/vm_page.h>
88a4915c21SAttilio Rao #include <vm/vm_pageout.h>
898355f576SJeff Roberson #include <vm/vm_param.h>
908355f576SJeff Roberson #include <vm/vm_map.h>
918355f576SJeff Roberson #include <vm/vm_kern.h>
928355f576SJeff Roberson #include <vm/vm_extern.h>
938355f576SJeff Roberson #include <vm/uma.h>
948355f576SJeff Roberson #include <vm/uma_int.h>
95639c9550SJeff Roberson #include <vm/uma_dbg.h>
968355f576SJeff Roberson 
9748c5777eSRobert Watson #include <ddb/ddb.h>
9848c5777eSRobert Watson 
998d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
1008d689e04SGleb Smirnoff #include <vm/memguard.h>
1018d689e04SGleb Smirnoff #endif
1028d689e04SGleb Smirnoff 
1038355f576SJeff Roberson /*
104099a0e58SBosko Milekic  * This is the zone and keg from which all zones are spawned.  The idea is that
105099a0e58SBosko Milekic  * even the zone & keg heads are allocated from the allocator, so we use the
106099a0e58SBosko Milekic  * bss section to bootstrap us.
1078355f576SJeff Roberson  */
108099a0e58SBosko Milekic static struct uma_keg masterkeg;
109099a0e58SBosko Milekic static struct uma_zone masterzone_k;
110099a0e58SBosko Milekic static struct uma_zone masterzone_z;
111099a0e58SBosko Milekic static uma_zone_t kegs = &masterzone_k;
112099a0e58SBosko Milekic static uma_zone_t zones = &masterzone_z;
1138355f576SJeff Roberson 
1148355f576SJeff Roberson /* This is the zone from which all of uma_slab_t's are allocated. */
1158355f576SJeff Roberson static uma_zone_t slabzone;
1168355f576SJeff Roberson 
1178355f576SJeff Roberson /*
1188355f576SJeff Roberson  * The initial hash tables come out of this zone so they can be allocated
1198355f576SJeff Roberson  * prior to malloc coming up.
1208355f576SJeff Roberson  */
1218355f576SJeff Roberson static uma_zone_t hashzone;
1228355f576SJeff Roberson 
1231e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */
124e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1;
1251e319f6dSRobert Watson 
126961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
127961647dfSJeff Roberson 
1288355f576SJeff Roberson /*
12986bbae32SJeff Roberson  * Are we allowed to allocate buckets?
13086bbae32SJeff Roberson  */
13186bbae32SJeff Roberson static int bucketdisable = 1;
13286bbae32SJeff Roberson 
133099a0e58SBosko Milekic /* Linked list of all kegs in the system */
13413e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
1358355f576SJeff Roberson 
13603175483SAlexander Motin /* Linked list of all cache-only zones in the system */
13703175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones =
13803175483SAlexander Motin     LIST_HEAD_INITIALIZER(uma_cachezones);
13903175483SAlexander Motin 
140111fbcd5SBryan Venteicher /* This RW lock protects the keg list */
141111fbcd5SBryan Venteicher static struct rwlock_padalign uma_rwlock;
1428355f576SJeff Roberson 
1438355f576SJeff Roberson /* Linked list of boot time pages */
1448355f576SJeff Roberson static LIST_HEAD(,uma_slab) uma_boot_pages =
14513e403fdSAntoine Brodin     LIST_HEAD_INITIALIZER(uma_boot_pages);
1468355f576SJeff Roberson 
147f353d338SAlan Cox /* This mutex protects the boot time pages list */
1480095a784SJeff Roberson static struct mtx_padalign uma_boot_pages_mtx;
1498355f576SJeff Roberson 
15095c4bf75SKonstantin Belousov static struct sx uma_drain_lock;
15195c4bf75SKonstantin Belousov 
1528355f576SJeff Roberson /* Is the VM done starting up? */
1538355f576SJeff Roberson static int booted = 0;
154342f1793SAlan Cox #define	UMA_STARTUP	1
155342f1793SAlan Cox #define	UMA_STARTUP2	2
1568355f576SJeff Roberson 
157ef72505eSJeff 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 
2061aa6c758SAlexander Motin #define	BUCKET_MAX	BUCKET_SIZE(256)
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 },
2171aa6c758SAlexander Motin 	{ NULL, "256 Bucket", BUCKET_SIZE(256), 64 },
218fc03d22bSJeff Roberson 	{ NULL, NULL, 0}
219fc03d22bSJeff Roberson };
220cae33c14SJeff Roberson 
2212019094aSRobert Watson /*
2222019094aSRobert Watson  * Flags and enumerations to be passed to internal functions.
2232019094aSRobert Watson  */
224ef72505eSJeff Roberson enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI };
225b23f72e9SBrian Feldman 
2268355f576SJeff Roberson /* Prototypes.. */
2278355f576SJeff Roberson 
228f2c2231eSRyan Stone static void *noobj_alloc(uma_zone_t, vm_size_t, uint8_t *, int);
229f2c2231eSRyan Stone static void *page_alloc(uma_zone_t, vm_size_t, uint8_t *, int);
230f2c2231eSRyan Stone static void *startup_alloc(uma_zone_t, vm_size_t, uint8_t *, int);
231f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t);
232e20a199fSJeff Roberson static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int);
2339643769aSJeff Roberson static void cache_drain(uma_zone_t);
2348355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t);
235aaa8bb16SJeff Roberson static void bucket_cache_drain(uma_zone_t zone);
236b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int);
237099a0e58SBosko Milekic static void keg_dtor(void *, int, void *);
238b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int);
2399c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *);
240b23f72e9SBrian Feldman static int zero_init(void *, int, int);
241e20a199fSJeff Roberson static void keg_small_init(uma_keg_t keg);
242e20a199fSJeff Roberson static void keg_large_init(uma_keg_t keg);
2438355f576SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t));
2448355f576SJeff Roberson static void zone_timeout(uma_zone_t zone);
2450aef6126SJeff Roberson static int hash_alloc(struct uma_hash *);
2460aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *);
2470aef6126SJeff Roberson static void hash_free(struct uma_hash *hash);
2488355f576SJeff Roberson static void uma_timeout(void *);
2498355f576SJeff Roberson static void uma_startup3(void);
250e20a199fSJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int);
2510095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
25286bbae32SJeff Roberson static void bucket_enable(void);
253cae33c14SJeff Roberson static void bucket_init(void);
2546fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int);
2556fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *);
256cae33c14SJeff Roberson static void bucket_zone_drain(void);
2576fd34d6fSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t zone, void *, int flags);
258e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags);
259e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags);
2600095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
2610095a784SJeff Roberson static void slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item);
262e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
26385dcf349SGleb Smirnoff     uma_fini fini, int align, uint32_t flags);
2640095a784SJeff Roberson static int zone_import(uma_zone_t zone, void **bucket, int max, int flags);
2650095a784SJeff Roberson static void zone_release(uma_zone_t zone, void **bucket, int cnt);
26648343a2fSGleb Smirnoff static void uma_zero_item(void *item, uma_zone_t zone);
267bbee39c6SJeff Roberson 
2688355f576SJeff Roberson void uma_print_zone(uma_zone_t);
2698355f576SJeff Roberson void uma_print_stats(void);
2707a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
2717a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
2728355f576SJeff Roberson 
2739542ea7bSGleb Smirnoff #ifdef INVARIANTS
2749542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item);
2759542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item);
2769542ea7bSGleb Smirnoff #endif
2779542ea7bSGleb Smirnoff 
2788355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
2798355f576SJeff Roberson 
2807a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
2817a52a97eSRobert Watson     0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
2827a52a97eSRobert Watson 
2837a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
2847a52a97eSRobert Watson     0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
2857a52a97eSRobert Watson 
2862f891cd5SPawel Jakub Dawidek static int zone_warnings = 1;
287af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0,
2882f891cd5SPawel Jakub Dawidek     "Warn when UMA zones becomes full");
2892f891cd5SPawel Jakub Dawidek 
29086bbae32SJeff Roberson /*
29186bbae32SJeff Roberson  * This routine checks to see whether or not it's safe to enable buckets.
29286bbae32SJeff Roberson  */
29386bbae32SJeff Roberson static void
29486bbae32SJeff Roberson bucket_enable(void)
29586bbae32SJeff Roberson {
296251386b4SMaksim Yevmenkin 	bucketdisable = vm_page_count_min();
29786bbae32SJeff Roberson }
29886bbae32SJeff Roberson 
299dc2c7965SRobert Watson /*
300dc2c7965SRobert Watson  * Initialize bucket_zones, the array of zones of buckets of various sizes.
301dc2c7965SRobert Watson  *
302dc2c7965SRobert Watson  * For each zone, calculate the memory required for each bucket, consisting
303fc03d22bSJeff Roberson  * of the header and an array of pointers.
304dc2c7965SRobert Watson  */
305cae33c14SJeff Roberson static void
306cae33c14SJeff Roberson bucket_init(void)
307cae33c14SJeff Roberson {
308cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
309cae33c14SJeff Roberson 	int size;
310cae33c14SJeff Roberson 
311d74e6a1dSAlan Cox 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
312cae33c14SJeff Roberson 		size = roundup(sizeof(struct uma_bucket), sizeof(void *));
313cae33c14SJeff Roberson 		size += sizeof(void *) * ubz->ubz_entries;
314cae33c14SJeff Roberson 		ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
315e20a199fSJeff Roberson 		    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
3166fd34d6fSJeff Roberson 		    UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET);
317cae33c14SJeff Roberson 	}
318cae33c14SJeff Roberson }
319cae33c14SJeff Roberson 
320dc2c7965SRobert Watson /*
321dc2c7965SRobert Watson  * Given a desired number of entries for a bucket, return the zone from which
322dc2c7965SRobert Watson  * to allocate the bucket.
323dc2c7965SRobert Watson  */
324dc2c7965SRobert Watson static struct uma_bucket_zone *
325dc2c7965SRobert Watson bucket_zone_lookup(int entries)
326dc2c7965SRobert Watson {
327fc03d22bSJeff Roberson 	struct uma_bucket_zone *ubz;
328dc2c7965SRobert Watson 
329fc03d22bSJeff Roberson 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
330fc03d22bSJeff Roberson 		if (ubz->ubz_entries >= entries)
331fc03d22bSJeff Roberson 			return (ubz);
332fc03d22bSJeff Roberson 	ubz--;
333fc03d22bSJeff Roberson 	return (ubz);
334fc03d22bSJeff Roberson }
335fc03d22bSJeff Roberson 
336fc03d22bSJeff Roberson static int
337fc03d22bSJeff Roberson bucket_select(int size)
338fc03d22bSJeff Roberson {
339fc03d22bSJeff Roberson 	struct uma_bucket_zone *ubz;
340fc03d22bSJeff Roberson 
341fc03d22bSJeff Roberson 	ubz = &bucket_zones[0];
342fc03d22bSJeff Roberson 	if (size > ubz->ubz_maxsize)
343fc03d22bSJeff Roberson 		return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
344fc03d22bSJeff Roberson 
345fc03d22bSJeff Roberson 	for (; ubz->ubz_entries != 0; ubz++)
346fc03d22bSJeff Roberson 		if (ubz->ubz_maxsize < size)
347fc03d22bSJeff Roberson 			break;
348fc03d22bSJeff Roberson 	ubz--;
349fc03d22bSJeff Roberson 	return (ubz->ubz_entries);
350dc2c7965SRobert Watson }
351dc2c7965SRobert Watson 
352cae33c14SJeff Roberson static uma_bucket_t
3536fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags)
354cae33c14SJeff Roberson {
355cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
356cae33c14SJeff Roberson 	uma_bucket_t bucket;
357cae33c14SJeff Roberson 
358cae33c14SJeff Roberson 	/*
359cae33c14SJeff Roberson 	 * This is to stop us from allocating per cpu buckets while we're
3603803b26bSDag-Erling Smørgrav 	 * running out of vm.boot_pages.  Otherwise, we would exhaust the
361cae33c14SJeff Roberson 	 * boot pages.  This also prevents us from allocating buckets in
362cae33c14SJeff Roberson 	 * low memory situations.
363cae33c14SJeff Roberson 	 */
364cae33c14SJeff Roberson 	if (bucketdisable)
365cae33c14SJeff Roberson 		return (NULL);
3666fd34d6fSJeff Roberson 	/*
3676fd34d6fSJeff Roberson 	 * To limit bucket recursion we store the original zone flags
3686fd34d6fSJeff Roberson 	 * in a cookie passed via zalloc_arg/zfree_arg.  This allows the
3696fd34d6fSJeff Roberson 	 * NOVM flag to persist even through deep recursions.  We also
3706fd34d6fSJeff Roberson 	 * store ZFLAG_BUCKET once we have recursed attempting to allocate
3716fd34d6fSJeff Roberson 	 * a bucket for a bucket zone so we do not allow infinite bucket
3726fd34d6fSJeff Roberson 	 * recursion.  This cookie will even persist to frees of unused
3736fd34d6fSJeff Roberson 	 * buckets via the allocation path or bucket allocations in the
3746fd34d6fSJeff Roberson 	 * free path.
3756fd34d6fSJeff Roberson 	 */
3766fd34d6fSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
3776fd34d6fSJeff Roberson 		udata = (void *)(uintptr_t)zone->uz_flags;
378e8a720feSAlexander Motin 	else {
379e8a720feSAlexander Motin 		if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
380e8a720feSAlexander Motin 			return (NULL);
3816fd34d6fSJeff Roberson 		udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
382e8a720feSAlexander Motin 	}
3836fd34d6fSJeff Roberson 	if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY)
384af526374SJeff Roberson 		flags |= M_NOVM;
385af526374SJeff Roberson 	ubz = bucket_zone_lookup(zone->uz_count);
38620d3ab87SAlexander Motin 	if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0)
38720d3ab87SAlexander Motin 		ubz++;
3886fd34d6fSJeff Roberson 	bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
389cae33c14SJeff Roberson 	if (bucket) {
390cae33c14SJeff Roberson #ifdef INVARIANTS
391cae33c14SJeff Roberson 		bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
392cae33c14SJeff Roberson #endif
393cae33c14SJeff Roberson 		bucket->ub_cnt = 0;
394cae33c14SJeff Roberson 		bucket->ub_entries = ubz->ubz_entries;
395cae33c14SJeff Roberson 	}
396cae33c14SJeff Roberson 
397cae33c14SJeff Roberson 	return (bucket);
398cae33c14SJeff Roberson }
399cae33c14SJeff Roberson 
400cae33c14SJeff Roberson static void
4016fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
402cae33c14SJeff Roberson {
403cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
404cae33c14SJeff Roberson 
405fc03d22bSJeff Roberson 	KASSERT(bucket->ub_cnt == 0,
406fc03d22bSJeff Roberson 	    ("bucket_free: Freeing a non free bucket."));
4076fd34d6fSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
4086fd34d6fSJeff Roberson 		udata = (void *)(uintptr_t)zone->uz_flags;
409dc2c7965SRobert Watson 	ubz = bucket_zone_lookup(bucket->ub_entries);
4106fd34d6fSJeff Roberson 	uma_zfree_arg(ubz->ubz_zone, bucket, udata);
411cae33c14SJeff Roberson }
412cae33c14SJeff Roberson 
413cae33c14SJeff Roberson static void
414cae33c14SJeff Roberson bucket_zone_drain(void)
415cae33c14SJeff Roberson {
416cae33c14SJeff Roberson 	struct uma_bucket_zone *ubz;
417cae33c14SJeff Roberson 
418cae33c14SJeff Roberson 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
419cae33c14SJeff Roberson 		zone_drain(ubz->ubz_zone);
420cae33c14SJeff Roberson }
421cae33c14SJeff Roberson 
4222f891cd5SPawel Jakub Dawidek static void
4232f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone)
4242f891cd5SPawel Jakub Dawidek {
4252f891cd5SPawel Jakub Dawidek 	static const struct timeval warninterval = { 300, 0 };
4262f891cd5SPawel Jakub Dawidek 
4272f891cd5SPawel Jakub Dawidek 	if (!zone_warnings || zone->uz_warning == NULL)
4282f891cd5SPawel Jakub Dawidek 		return;
4292f891cd5SPawel Jakub Dawidek 
4302f891cd5SPawel Jakub Dawidek 	if (ratecheck(&zone->uz_ratecheck, &warninterval))
4312f891cd5SPawel Jakub Dawidek 		printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
4322f891cd5SPawel Jakub Dawidek }
4332f891cd5SPawel Jakub Dawidek 
43454503a13SJonathan T. Looney static inline void
43554503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone)
43654503a13SJonathan T. Looney {
437e60b2fcbSGleb Smirnoff 
438e60b2fcbSGleb Smirnoff 	if (zone->uz_maxaction.ta_func != NULL)
439e60b2fcbSGleb Smirnoff 		taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction);
44054503a13SJonathan T. Looney }
44154503a13SJonathan T. Looney 
442e20a199fSJeff Roberson static void
443e20a199fSJeff Roberson zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
444e20a199fSJeff Roberson {
445e20a199fSJeff Roberson 	uma_klink_t klink;
446e20a199fSJeff Roberson 
447e20a199fSJeff Roberson 	LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
448e20a199fSJeff Roberson 		kegfn(klink->kl_keg);
449e20a199fSJeff Roberson }
4508355f576SJeff Roberson 
4518355f576SJeff Roberson /*
4528355f576SJeff Roberson  * Routine called by timeout which is used to fire off some time interval
4539643769aSJeff Roberson  * based calculations.  (stats, hash size, etc.)
4548355f576SJeff Roberson  *
4558355f576SJeff Roberson  * Arguments:
4568355f576SJeff Roberson  *	arg   Unused
4578355f576SJeff Roberson  *
4588355f576SJeff Roberson  * Returns:
4598355f576SJeff Roberson  *	Nothing
4608355f576SJeff Roberson  */
4618355f576SJeff Roberson static void
4628355f576SJeff Roberson uma_timeout(void *unused)
4638355f576SJeff Roberson {
46486bbae32SJeff Roberson 	bucket_enable();
4658355f576SJeff Roberson 	zone_foreach(zone_timeout);
4668355f576SJeff Roberson 
4678355f576SJeff Roberson 	/* Reschedule this event */
4689643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
4698355f576SJeff Roberson }
4708355f576SJeff Roberson 
4718355f576SJeff Roberson /*
4729643769aSJeff Roberson  * Routine to perform timeout driven calculations.  This expands the
4739643769aSJeff Roberson  * hashes and does per cpu statistics aggregation.
4748355f576SJeff Roberson  *
475e20a199fSJeff Roberson  *  Returns nothing.
4768355f576SJeff Roberson  */
4778355f576SJeff Roberson static void
478e20a199fSJeff Roberson keg_timeout(uma_keg_t keg)
4798355f576SJeff Roberson {
4808355f576SJeff Roberson 
481e20a199fSJeff Roberson 	KEG_LOCK(keg);
4828355f576SJeff Roberson 	/*
483e20a199fSJeff Roberson 	 * Expand the keg hash table.
4848355f576SJeff Roberson 	 *
4858355f576SJeff Roberson 	 * This is done if the number of slabs is larger than the hash size.
4868355f576SJeff Roberson 	 * What I'm trying to do here is completely reduce collisions.  This
4878355f576SJeff Roberson 	 * may be a little aggressive.  Should I allow for two collisions max?
4888355f576SJeff Roberson 	 */
489099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_HASH &&
490099a0e58SBosko Milekic 	    keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
4910aef6126SJeff Roberson 		struct uma_hash newhash;
4920aef6126SJeff Roberson 		struct uma_hash oldhash;
4930aef6126SJeff Roberson 		int ret;
4945300d9ddSJeff Roberson 
4950aef6126SJeff Roberson 		/*
4960aef6126SJeff Roberson 		 * This is so involved because allocating and freeing
497e20a199fSJeff Roberson 		 * while the keg lock is held will lead to deadlock.
4980aef6126SJeff Roberson 		 * I have to do everything in stages and check for
4990aef6126SJeff Roberson 		 * races.
5000aef6126SJeff Roberson 		 */
501099a0e58SBosko Milekic 		newhash = keg->uk_hash;
502e20a199fSJeff Roberson 		KEG_UNLOCK(keg);
5030aef6126SJeff Roberson 		ret = hash_alloc(&newhash);
504e20a199fSJeff Roberson 		KEG_LOCK(keg);
5050aef6126SJeff Roberson 		if (ret) {
506099a0e58SBosko Milekic 			if (hash_expand(&keg->uk_hash, &newhash)) {
507099a0e58SBosko Milekic 				oldhash = keg->uk_hash;
508099a0e58SBosko Milekic 				keg->uk_hash = newhash;
5090aef6126SJeff Roberson 			} else
5100aef6126SJeff Roberson 				oldhash = newhash;
5110aef6126SJeff Roberson 
512e20a199fSJeff Roberson 			KEG_UNLOCK(keg);
5130aef6126SJeff Roberson 			hash_free(&oldhash);
514a1dff920SDavide Italiano 			return;
5150aef6126SJeff Roberson 		}
5165300d9ddSJeff Roberson 	}
517e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
518e20a199fSJeff Roberson }
519e20a199fSJeff Roberson 
520e20a199fSJeff Roberson static void
521e20a199fSJeff Roberson zone_timeout(uma_zone_t zone)
522e20a199fSJeff Roberson {
523e20a199fSJeff Roberson 
524e20a199fSJeff Roberson 	zone_foreach_keg(zone, &keg_timeout);
5258355f576SJeff Roberson }
5268355f576SJeff Roberson 
5278355f576SJeff Roberson /*
5285300d9ddSJeff Roberson  * Allocate and zero fill the next sized hash table from the appropriate
5295300d9ddSJeff Roberson  * backing store.
5305300d9ddSJeff Roberson  *
5315300d9ddSJeff Roberson  * Arguments:
5320aef6126SJeff Roberson  *	hash  A new hash structure with the old hash size in uh_hashsize
5335300d9ddSJeff Roberson  *
5345300d9ddSJeff Roberson  * Returns:
535763df3ecSPedro F. Giffuni  *	1 on success and 0 on failure.
5365300d9ddSJeff Roberson  */
53737c84183SPoul-Henning Kamp static int
5380aef6126SJeff Roberson hash_alloc(struct uma_hash *hash)
5395300d9ddSJeff Roberson {
5400aef6126SJeff Roberson 	int oldsize;
5415300d9ddSJeff Roberson 	int alloc;
5425300d9ddSJeff Roberson 
5430aef6126SJeff Roberson 	oldsize = hash->uh_hashsize;
5440aef6126SJeff Roberson 
5455300d9ddSJeff Roberson 	/* We're just going to go to a power of two greater */
5460aef6126SJeff Roberson 	if (oldsize)  {
5470aef6126SJeff Roberson 		hash->uh_hashsize = oldsize * 2;
5480aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
5490aef6126SJeff Roberson 		hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
550961647dfSJeff Roberson 		    M_UMAHASH, M_NOWAIT);
5515300d9ddSJeff Roberson 	} else {
5520aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
553e20a199fSJeff Roberson 		hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
554a163d034SWarner Losh 		    M_WAITOK);
5550aef6126SJeff Roberson 		hash->uh_hashsize = UMA_HASH_SIZE_INIT;
5565300d9ddSJeff Roberson 	}
5570aef6126SJeff Roberson 	if (hash->uh_slab_hash) {
5580aef6126SJeff Roberson 		bzero(hash->uh_slab_hash, alloc);
5590aef6126SJeff Roberson 		hash->uh_hashmask = hash->uh_hashsize - 1;
5600aef6126SJeff Roberson 		return (1);
5610aef6126SJeff Roberson 	}
5625300d9ddSJeff Roberson 
5630aef6126SJeff Roberson 	return (0);
5645300d9ddSJeff Roberson }
5655300d9ddSJeff Roberson 
5665300d9ddSJeff Roberson /*
56764f051e9SJeff Roberson  * Expands the hash table for HASH zones.  This is done from zone_timeout
56864f051e9SJeff Roberson  * to reduce collisions.  This must not be done in the regular allocation
56964f051e9SJeff Roberson  * path, otherwise, we can recurse on the vm while allocating pages.
5708355f576SJeff Roberson  *
5718355f576SJeff Roberson  * Arguments:
5720aef6126SJeff Roberson  *	oldhash  The hash you want to expand
5730aef6126SJeff Roberson  *	newhash  The hash structure for the new table
5748355f576SJeff Roberson  *
5758355f576SJeff Roberson  * Returns:
5768355f576SJeff Roberson  *	Nothing
5778355f576SJeff Roberson  *
5788355f576SJeff Roberson  * Discussion:
5798355f576SJeff Roberson  */
5800aef6126SJeff Roberson static int
5810aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
5828355f576SJeff Roberson {
5838355f576SJeff Roberson 	uma_slab_t slab;
5848355f576SJeff Roberson 	int hval;
5858355f576SJeff Roberson 	int i;
5868355f576SJeff Roberson 
5870aef6126SJeff Roberson 	if (!newhash->uh_slab_hash)
5880aef6126SJeff Roberson 		return (0);
5898355f576SJeff Roberson 
5900aef6126SJeff Roberson 	if (oldhash->uh_hashsize >= newhash->uh_hashsize)
5910aef6126SJeff Roberson 		return (0);
5928355f576SJeff Roberson 
5938355f576SJeff Roberson 	/*
5948355f576SJeff Roberson 	 * I need to investigate hash algorithms for resizing without a
5958355f576SJeff Roberson 	 * full rehash.
5968355f576SJeff Roberson 	 */
5978355f576SJeff Roberson 
5980aef6126SJeff Roberson 	for (i = 0; i < oldhash->uh_hashsize; i++)
5990aef6126SJeff Roberson 		while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
6000aef6126SJeff Roberson 			slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
6010aef6126SJeff Roberson 			SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
6020aef6126SJeff Roberson 			hval = UMA_HASH(newhash, slab->us_data);
6030aef6126SJeff Roberson 			SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
6040aef6126SJeff Roberson 			    slab, us_hlink);
6058355f576SJeff Roberson 		}
6068355f576SJeff Roberson 
6070aef6126SJeff Roberson 	return (1);
6089c2cd7e5SJeff Roberson }
6099c2cd7e5SJeff Roberson 
6105300d9ddSJeff Roberson /*
6115300d9ddSJeff Roberson  * Free the hash bucket to the appropriate backing store.
6125300d9ddSJeff Roberson  *
6135300d9ddSJeff Roberson  * Arguments:
6145300d9ddSJeff Roberson  *	slab_hash  The hash bucket we're freeing
6155300d9ddSJeff Roberson  *	hashsize   The number of entries in that hash bucket
6165300d9ddSJeff Roberson  *
6175300d9ddSJeff Roberson  * Returns:
6185300d9ddSJeff Roberson  *	Nothing
6195300d9ddSJeff Roberson  */
6209c2cd7e5SJeff Roberson static void
6210aef6126SJeff Roberson hash_free(struct uma_hash *hash)
6229c2cd7e5SJeff Roberson {
6230aef6126SJeff Roberson 	if (hash->uh_slab_hash == NULL)
6240aef6126SJeff Roberson 		return;
6250aef6126SJeff Roberson 	if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
6260095a784SJeff Roberson 		zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
6278355f576SJeff Roberson 	else
628961647dfSJeff Roberson 		free(hash->uh_slab_hash, M_UMAHASH);
6298355f576SJeff Roberson }
6308355f576SJeff Roberson 
6318355f576SJeff Roberson /*
6328355f576SJeff Roberson  * Frees all outstanding items in a bucket
6338355f576SJeff Roberson  *
6348355f576SJeff Roberson  * Arguments:
6358355f576SJeff Roberson  *	zone   The zone to free to, must be unlocked.
6368355f576SJeff Roberson  *	bucket The free/alloc bucket with items, cpu queue must be locked.
6378355f576SJeff Roberson  *
6388355f576SJeff Roberson  * Returns:
6398355f576SJeff Roberson  *	Nothing
6408355f576SJeff Roberson  */
6418355f576SJeff Roberson 
6428355f576SJeff Roberson static void
6438355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
6448355f576SJeff Roberson {
6450095a784SJeff Roberson 	int i;
6468355f576SJeff Roberson 
6478355f576SJeff Roberson 	if (bucket == NULL)
6488355f576SJeff Roberson 		return;
6498355f576SJeff Roberson 
6500095a784SJeff Roberson 	if (zone->uz_fini)
6510095a784SJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
6520095a784SJeff Roberson 			zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
6530095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
6540095a784SJeff Roberson 	bucket->ub_cnt = 0;
6558355f576SJeff Roberson }
6568355f576SJeff Roberson 
6578355f576SJeff Roberson /*
6588355f576SJeff Roberson  * Drains the per cpu caches for a zone.
6598355f576SJeff Roberson  *
6605d1ae027SRobert Watson  * NOTE: This may only be called while the zone is being turn down, and not
6615d1ae027SRobert Watson  * during normal operation.  This is necessary in order that we do not have
6625d1ae027SRobert Watson  * to migrate CPUs to drain the per-CPU caches.
6635d1ae027SRobert Watson  *
6648355f576SJeff Roberson  * Arguments:
6658355f576SJeff Roberson  *	zone     The zone to drain, must be unlocked.
6668355f576SJeff Roberson  *
6678355f576SJeff Roberson  * Returns:
6688355f576SJeff Roberson  *	Nothing
6698355f576SJeff Roberson  */
6708355f576SJeff Roberson static void
6719643769aSJeff Roberson cache_drain(uma_zone_t zone)
6728355f576SJeff Roberson {
6738355f576SJeff Roberson 	uma_cache_t cache;
6748355f576SJeff Roberson 	int cpu;
6758355f576SJeff Roberson 
6768355f576SJeff Roberson 	/*
6775d1ae027SRobert Watson 	 * XXX: It is safe to not lock the per-CPU caches, because we're
6785d1ae027SRobert Watson 	 * tearing down the zone anyway.  I.e., there will be no further use
6795d1ae027SRobert Watson 	 * of the caches at this point.
6805d1ae027SRobert Watson 	 *
6815d1ae027SRobert Watson 	 * XXX: It would good to be able to assert that the zone is being
6825d1ae027SRobert Watson 	 * torn down to prevent improper use of cache_drain().
6835d1ae027SRobert Watson 	 *
6845d1ae027SRobert Watson 	 * XXX: We lock the zone before passing into bucket_cache_drain() as
6855d1ae027SRobert Watson 	 * it is used elsewhere.  Should the tear-down path be made special
6865d1ae027SRobert Watson 	 * there in some form?
6878355f576SJeff Roberson 	 */
6883aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
6898355f576SJeff Roberson 		cache = &zone->uz_cpu[cpu];
6908355f576SJeff Roberson 		bucket_drain(zone, cache->uc_allocbucket);
6918355f576SJeff Roberson 		bucket_drain(zone, cache->uc_freebucket);
692174ab450SBosko Milekic 		if (cache->uc_allocbucket != NULL)
6936fd34d6fSJeff Roberson 			bucket_free(zone, cache->uc_allocbucket, NULL);
694174ab450SBosko Milekic 		if (cache->uc_freebucket != NULL)
6956fd34d6fSJeff Roberson 			bucket_free(zone, cache->uc_freebucket, NULL);
696d56368d7SBosko Milekic 		cache->uc_allocbucket = cache->uc_freebucket = NULL;
697d56368d7SBosko Milekic 	}
698aaa8bb16SJeff Roberson 	ZONE_LOCK(zone);
699aaa8bb16SJeff Roberson 	bucket_cache_drain(zone);
700aaa8bb16SJeff Roberson 	ZONE_UNLOCK(zone);
701aaa8bb16SJeff Roberson }
702aaa8bb16SJeff Roberson 
703a2de44abSAlexander Motin static void
704a2de44abSAlexander Motin cache_shrink(uma_zone_t zone)
705a2de44abSAlexander Motin {
706a2de44abSAlexander Motin 
707a2de44abSAlexander Motin 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
708a2de44abSAlexander Motin 		return;
709a2de44abSAlexander Motin 
710a2de44abSAlexander Motin 	ZONE_LOCK(zone);
711a2de44abSAlexander Motin 	zone->uz_count = (zone->uz_count_min + zone->uz_count) / 2;
712a2de44abSAlexander Motin 	ZONE_UNLOCK(zone);
713a2de44abSAlexander Motin }
714a2de44abSAlexander Motin 
715a2de44abSAlexander Motin static void
716a2de44abSAlexander Motin cache_drain_safe_cpu(uma_zone_t zone)
717a2de44abSAlexander Motin {
718a2de44abSAlexander Motin 	uma_cache_t cache;
7198a8d9d14SAlexander Motin 	uma_bucket_t b1, b2;
720a2de44abSAlexander Motin 
721a2de44abSAlexander Motin 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
722a2de44abSAlexander Motin 		return;
723a2de44abSAlexander Motin 
7248a8d9d14SAlexander Motin 	b1 = b2 = NULL;
725a2de44abSAlexander Motin 	ZONE_LOCK(zone);
726a2de44abSAlexander Motin 	critical_enter();
727a2de44abSAlexander Motin 	cache = &zone->uz_cpu[curcpu];
728a2de44abSAlexander Motin 	if (cache->uc_allocbucket) {
7298a8d9d14SAlexander Motin 		if (cache->uc_allocbucket->ub_cnt != 0)
7308a8d9d14SAlexander Motin 			LIST_INSERT_HEAD(&zone->uz_buckets,
7318a8d9d14SAlexander Motin 			    cache->uc_allocbucket, ub_link);
7328a8d9d14SAlexander Motin 		else
7338a8d9d14SAlexander Motin 			b1 = cache->uc_allocbucket;
734a2de44abSAlexander Motin 		cache->uc_allocbucket = NULL;
735a2de44abSAlexander Motin 	}
736a2de44abSAlexander Motin 	if (cache->uc_freebucket) {
7378a8d9d14SAlexander Motin 		if (cache->uc_freebucket->ub_cnt != 0)
7388a8d9d14SAlexander Motin 			LIST_INSERT_HEAD(&zone->uz_buckets,
7398a8d9d14SAlexander Motin 			    cache->uc_freebucket, ub_link);
7408a8d9d14SAlexander Motin 		else
7418a8d9d14SAlexander Motin 			b2 = cache->uc_freebucket;
742a2de44abSAlexander Motin 		cache->uc_freebucket = NULL;
743a2de44abSAlexander Motin 	}
744a2de44abSAlexander Motin 	critical_exit();
745a2de44abSAlexander Motin 	ZONE_UNLOCK(zone);
7468a8d9d14SAlexander Motin 	if (b1)
7478a8d9d14SAlexander Motin 		bucket_free(zone, b1, NULL);
7488a8d9d14SAlexander Motin 	if (b2)
7498a8d9d14SAlexander Motin 		bucket_free(zone, b2, NULL);
750a2de44abSAlexander Motin }
751a2de44abSAlexander Motin 
752a2de44abSAlexander Motin /*
753a2de44abSAlexander Motin  * Safely drain per-CPU caches of a zone(s) to alloc bucket.
754a2de44abSAlexander Motin  * This is an expensive call because it needs to bind to all CPUs
755a2de44abSAlexander Motin  * one by one and enter a critical section on each of them in order
756a2de44abSAlexander Motin  * to safely access their cache buckets.
757a2de44abSAlexander Motin  * Zone lock must not be held on call this function.
758a2de44abSAlexander Motin  */
759a2de44abSAlexander Motin static void
760a2de44abSAlexander Motin cache_drain_safe(uma_zone_t zone)
761a2de44abSAlexander Motin {
762a2de44abSAlexander Motin 	int cpu;
763a2de44abSAlexander Motin 
764a2de44abSAlexander Motin 	/*
765a2de44abSAlexander Motin 	 * Polite bucket sizes shrinking was not enouth, shrink aggressively.
766a2de44abSAlexander Motin 	 */
767a2de44abSAlexander Motin 	if (zone)
768a2de44abSAlexander Motin 		cache_shrink(zone);
769a2de44abSAlexander Motin 	else
770a2de44abSAlexander Motin 		zone_foreach(cache_shrink);
771a2de44abSAlexander Motin 
772a2de44abSAlexander Motin 	CPU_FOREACH(cpu) {
773a2de44abSAlexander Motin 		thread_lock(curthread);
774a2de44abSAlexander Motin 		sched_bind(curthread, cpu);
775a2de44abSAlexander Motin 		thread_unlock(curthread);
776a2de44abSAlexander Motin 
777a2de44abSAlexander Motin 		if (zone)
778a2de44abSAlexander Motin 			cache_drain_safe_cpu(zone);
779a2de44abSAlexander Motin 		else
780a2de44abSAlexander Motin 			zone_foreach(cache_drain_safe_cpu);
781a2de44abSAlexander Motin 	}
782a2de44abSAlexander Motin 	thread_lock(curthread);
783a2de44abSAlexander Motin 	sched_unbind(curthread);
784a2de44abSAlexander Motin 	thread_unlock(curthread);
785a2de44abSAlexander Motin }
786a2de44abSAlexander Motin 
787aaa8bb16SJeff Roberson /*
788aaa8bb16SJeff Roberson  * Drain the cached buckets from a zone.  Expects a locked zone on entry.
789aaa8bb16SJeff Roberson  */
790aaa8bb16SJeff Roberson static void
791aaa8bb16SJeff Roberson bucket_cache_drain(uma_zone_t zone)
792aaa8bb16SJeff Roberson {
793aaa8bb16SJeff Roberson 	uma_bucket_t bucket;
7948355f576SJeff Roberson 
7958355f576SJeff Roberson 	/*
7968355f576SJeff Roberson 	 * Drain the bucket queues and free the buckets, we just keep two per
7978355f576SJeff Roberson 	 * cpu (alloc/free).
7988355f576SJeff Roberson 	 */
799fc03d22bSJeff Roberson 	while ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
8008355f576SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
8018355f576SJeff Roberson 		ZONE_UNLOCK(zone);
8028355f576SJeff Roberson 		bucket_drain(zone, bucket);
8036fd34d6fSJeff Roberson 		bucket_free(zone, bucket, NULL);
8048355f576SJeff Roberson 		ZONE_LOCK(zone);
8058355f576SJeff Roberson 	}
806ace66b56SAlexander Motin 
807ace66b56SAlexander Motin 	/*
808ace66b56SAlexander Motin 	 * Shrink further bucket sizes.  Price of single zone lock collision
809ace66b56SAlexander Motin 	 * is probably lower then price of global cache drain.
810ace66b56SAlexander Motin 	 */
811ace66b56SAlexander Motin 	if (zone->uz_count > zone->uz_count_min)
812ace66b56SAlexander Motin 		zone->uz_count--;
8138355f576SJeff Roberson }
814fc03d22bSJeff Roberson 
815fc03d22bSJeff Roberson static void
816fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
817fc03d22bSJeff Roberson {
818fc03d22bSJeff Roberson 	uint8_t *mem;
819fc03d22bSJeff Roberson 	int i;
820fc03d22bSJeff Roberson 	uint8_t flags;
821fc03d22bSJeff Roberson 
822fc03d22bSJeff Roberson 	mem = slab->us_data;
823fc03d22bSJeff Roberson 	flags = slab->us_flags;
824fc03d22bSJeff Roberson 	i = start;
825fc03d22bSJeff Roberson 	if (keg->uk_fini != NULL) {
826fc03d22bSJeff Roberson 		for (i--; i > -1; i--)
827fc03d22bSJeff Roberson 			keg->uk_fini(slab->us_data + (keg->uk_rsize * i),
828fc03d22bSJeff Roberson 			    keg->uk_size);
829fc03d22bSJeff Roberson 	}
830fc03d22bSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_OFFPAGE)
831fc03d22bSJeff Roberson 		zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
832fc03d22bSJeff Roberson #ifdef UMA_DEBUG
833fc03d22bSJeff Roberson 	printf("%s: Returning %d bytes.\n", keg->uk_name,
834fc03d22bSJeff Roberson 	    PAGE_SIZE * keg->uk_ppera);
835fc03d22bSJeff Roberson #endif
836fc03d22bSJeff Roberson 	keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
8378355f576SJeff Roberson }
8388355f576SJeff Roberson 
8398355f576SJeff Roberson /*
840e20a199fSJeff Roberson  * Frees pages from a keg back to the system.  This is done on demand from
8418355f576SJeff Roberson  * the pageout daemon.
8428355f576SJeff Roberson  *
843e20a199fSJeff Roberson  * Returns nothing.
8448355f576SJeff Roberson  */
845e20a199fSJeff Roberson static void
846e20a199fSJeff Roberson keg_drain(uma_keg_t keg)
8478355f576SJeff Roberson {
8481e183df2SStefan Farfeleder 	struct slabhead freeslabs = { 0 };
849829be516SMark Johnston 	uma_slab_t slab, tmp;
8508355f576SJeff Roberson 
8518355f576SJeff Roberson 	/*
852e20a199fSJeff Roberson 	 * We don't want to take pages from statically allocated kegs at this
8538355f576SJeff Roberson 	 * time
8548355f576SJeff Roberson 	 */
855099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
8568355f576SJeff Roberson 		return;
8578355f576SJeff Roberson 
8588355f576SJeff Roberson #ifdef UMA_DEBUG
859e20a199fSJeff Roberson 	printf("%s free items: %u\n", keg->uk_name, keg->uk_free);
8608355f576SJeff Roberson #endif
861e20a199fSJeff Roberson 	KEG_LOCK(keg);
862099a0e58SBosko Milekic 	if (keg->uk_free == 0)
8638355f576SJeff Roberson 		goto finished;
8648355f576SJeff Roberson 
865829be516SMark Johnston 	LIST_FOREACH_SAFE(slab, &keg->uk_free_slab, us_link, tmp) {
866829be516SMark Johnston 		/* We have nowhere to free these to. */
867829be516SMark Johnston 		if (slab->us_flags & UMA_SLAB_BOOT)
8688355f576SJeff Roberson 			continue;
8698355f576SJeff Roberson 
8708355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
871099a0e58SBosko Milekic 		keg->uk_pages -= keg->uk_ppera;
872099a0e58SBosko Milekic 		keg->uk_free -= keg->uk_ipers;
873713deb36SJeff Roberson 
874099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZONE_HASH)
875099a0e58SBosko Milekic 			UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
876713deb36SJeff Roberson 
877713deb36SJeff Roberson 		SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
878713deb36SJeff Roberson 	}
879713deb36SJeff Roberson finished:
880e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
881713deb36SJeff Roberson 
882713deb36SJeff Roberson 	while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
883713deb36SJeff Roberson 		SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
8841645995bSKirk McKusick 		keg_free_slab(keg, slab, keg->uk_ipers);
8858355f576SJeff Roberson 	}
8868355f576SJeff Roberson }
8878355f576SJeff Roberson 
888e20a199fSJeff Roberson static void
889e20a199fSJeff Roberson zone_drain_wait(uma_zone_t zone, int waitok)
890e20a199fSJeff Roberson {
891e20a199fSJeff Roberson 
8928355f576SJeff Roberson 	/*
893e20a199fSJeff Roberson 	 * Set draining to interlock with zone_dtor() so we can release our
894e20a199fSJeff Roberson 	 * locks as we go.  Only dtor() should do a WAITOK call since it
895e20a199fSJeff Roberson 	 * is the only call that knows the structure will still be available
896e20a199fSJeff Roberson 	 * when it wakes up.
897e20a199fSJeff Roberson 	 */
898e20a199fSJeff Roberson 	ZONE_LOCK(zone);
899e20a199fSJeff Roberson 	while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
900e20a199fSJeff Roberson 		if (waitok == M_NOWAIT)
901e20a199fSJeff Roberson 			goto out;
902af526374SJeff Roberson 		msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1);
903e20a199fSJeff Roberson 	}
904e20a199fSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_DRAINING;
905e20a199fSJeff Roberson 	bucket_cache_drain(zone);
906e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
907e20a199fSJeff Roberson 	/*
908e20a199fSJeff Roberson 	 * The DRAINING flag protects us from being freed while
909111fbcd5SBryan Venteicher 	 * we're running.  Normally the uma_rwlock would protect us but we
910e20a199fSJeff Roberson 	 * must be able to release and acquire the right lock for each keg.
911e20a199fSJeff Roberson 	 */
912e20a199fSJeff Roberson 	zone_foreach_keg(zone, &keg_drain);
913e20a199fSJeff Roberson 	ZONE_LOCK(zone);
914e20a199fSJeff Roberson 	zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
915e20a199fSJeff Roberson 	wakeup(zone);
916e20a199fSJeff Roberson out:
917e20a199fSJeff Roberson 	ZONE_UNLOCK(zone);
918e20a199fSJeff Roberson }
919e20a199fSJeff Roberson 
920e20a199fSJeff Roberson void
921e20a199fSJeff Roberson zone_drain(uma_zone_t zone)
922e20a199fSJeff Roberson {
923e20a199fSJeff Roberson 
924e20a199fSJeff Roberson 	zone_drain_wait(zone, M_NOWAIT);
925e20a199fSJeff Roberson }
926e20a199fSJeff Roberson 
927e20a199fSJeff Roberson /*
928e20a199fSJeff Roberson  * Allocate a new slab for a keg.  This does not insert the slab onto a list.
9298355f576SJeff Roberson  *
9308355f576SJeff Roberson  * Arguments:
9318355f576SJeff Roberson  *	wait  Shall we wait?
9328355f576SJeff Roberson  *
9338355f576SJeff Roberson  * Returns:
9348355f576SJeff Roberson  *	The slab that was allocated or NULL if there is no memory and the
9358355f576SJeff Roberson  *	caller specified M_NOWAIT.
9368355f576SJeff Roberson  */
9378355f576SJeff Roberson static uma_slab_t
938e20a199fSJeff Roberson keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait)
9398355f576SJeff Roberson {
940e20a199fSJeff Roberson 	uma_alloc allocf;
941099a0e58SBosko Milekic 	uma_slab_t slab;
94285dcf349SGleb Smirnoff 	uint8_t *mem;
94385dcf349SGleb Smirnoff 	uint8_t flags;
9448355f576SJeff Roberson 	int i;
9458355f576SJeff Roberson 
946e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
947a553d4b8SJeff Roberson 	slab = NULL;
948fc03d22bSJeff Roberson 	mem = NULL;
949a553d4b8SJeff Roberson 
9508355f576SJeff Roberson #ifdef UMA_DEBUG
9510095a784SJeff Roberson 	printf("alloc_slab:  Allocating a new slab for %s\n", keg->uk_name);
9528355f576SJeff Roberson #endif
953e20a199fSJeff Roberson 	allocf = keg->uk_allocf;
954e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
955a553d4b8SJeff Roberson 
956099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
957e20a199fSJeff Roberson 		slab = zone_alloc_item(keg->uk_slabzone, NULL, wait);
958fc03d22bSJeff Roberson 		if (slab == NULL)
959fc03d22bSJeff Roberson 			goto out;
960a553d4b8SJeff Roberson 	}
961a553d4b8SJeff Roberson 
9623370c5bfSJeff Roberson 	/*
9633370c5bfSJeff Roberson 	 * This reproduces the old vm_zone behavior of zero filling pages the
9643370c5bfSJeff Roberson 	 * first time they are added to a zone.
9653370c5bfSJeff Roberson 	 *
9663370c5bfSJeff Roberson 	 * Malloced items are zeroed in uma_zalloc.
9673370c5bfSJeff Roberson 	 */
9683370c5bfSJeff Roberson 
969099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
9703370c5bfSJeff Roberson 		wait |= M_ZERO;
9713370c5bfSJeff Roberson 	else
9723370c5bfSJeff Roberson 		wait &= ~M_ZERO;
9733370c5bfSJeff Roberson 
974263811f7SKip Macy 	if (keg->uk_flags & UMA_ZONE_NODUMP)
975263811f7SKip Macy 		wait |= M_NODUMP;
976263811f7SKip Macy 
977e20a199fSJeff Roberson 	/* zone is passed for legacy reasons. */
978ad97af7eSGleb Smirnoff 	mem = allocf(zone, keg->uk_ppera * PAGE_SIZE, &flags, wait);
979a553d4b8SJeff Roberson 	if (mem == NULL) {
980b23f72e9SBrian Feldman 		if (keg->uk_flags & UMA_ZONE_OFFPAGE)
9810095a784SJeff Roberson 			zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
982fc03d22bSJeff Roberson 		slab = NULL;
983fc03d22bSJeff Roberson 		goto out;
984a553d4b8SJeff Roberson 	}
9858355f576SJeff Roberson 
9865c0e403bSJeff Roberson 	/* Point the slab into the allocated memory */
987099a0e58SBosko Milekic 	if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
988099a0e58SBosko Milekic 		slab = (uma_slab_t )(mem + keg->uk_pgoff);
9895c0e403bSJeff Roberson 
990e20a199fSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_VTOSLAB)
991099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ppera; i++)
99299571dc3SJeff Roberson 			vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
9938355f576SJeff Roberson 
994099a0e58SBosko Milekic 	slab->us_keg = keg;
9958355f576SJeff Roberson 	slab->us_data = mem;
996099a0e58SBosko Milekic 	slab->us_freecount = keg->uk_ipers;
9978355f576SJeff Roberson 	slab->us_flags = flags;
998ef72505eSJeff Roberson 	BIT_FILL(SLAB_SETSIZE, &slab->us_free);
999ef72505eSJeff Roberson #ifdef INVARIANTS
1000ef72505eSJeff Roberson 	BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree);
1001ef72505eSJeff Roberson #endif
1002099a0e58SBosko Milekic 
1003b23f72e9SBrian Feldman 	if (keg->uk_init != NULL) {
1004099a0e58SBosko Milekic 		for (i = 0; i < keg->uk_ipers; i++)
1005b23f72e9SBrian Feldman 			if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
1006b23f72e9SBrian Feldman 			    keg->uk_size, wait) != 0)
1007b23f72e9SBrian Feldman 				break;
1008b23f72e9SBrian Feldman 		if (i != keg->uk_ipers) {
1009fc03d22bSJeff Roberson 			keg_free_slab(keg, slab, i);
1010fc03d22bSJeff Roberson 			slab = NULL;
1011fc03d22bSJeff Roberson 			goto out;
1012b23f72e9SBrian Feldman 		}
1013b23f72e9SBrian Feldman 	}
1014fc03d22bSJeff Roberson out:
1015e20a199fSJeff Roberson 	KEG_LOCK(keg);
10165c0e403bSJeff Roberson 
1017fc03d22bSJeff Roberson 	if (slab != NULL) {
1018099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZONE_HASH)
1019099a0e58SBosko Milekic 			UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
10208355f576SJeff Roberson 
1021099a0e58SBosko Milekic 		keg->uk_pages += keg->uk_ppera;
1022099a0e58SBosko Milekic 		keg->uk_free += keg->uk_ipers;
1023fc03d22bSJeff Roberson 	}
10248355f576SJeff Roberson 
10258355f576SJeff Roberson 	return (slab);
10268355f576SJeff Roberson }
10278355f576SJeff Roberson 
10288355f576SJeff Roberson /*
1029009b6fcbSJeff Roberson  * This function is intended to be used early on in place of page_alloc() so
1030009b6fcbSJeff Roberson  * that we may use the boot time page cache to satisfy allocations before
1031009b6fcbSJeff Roberson  * the VM is ready.
1032009b6fcbSJeff Roberson  */
1033009b6fcbSJeff Roberson static void *
1034f2c2231eSRyan Stone startup_alloc(uma_zone_t zone, vm_size_t bytes, uint8_t *pflag, int wait)
1035009b6fcbSJeff Roberson {
1036099a0e58SBosko Milekic 	uma_keg_t keg;
1037f353d338SAlan Cox 	uma_slab_t tmps;
1038e9a069d8SJohn Baldwin 	int pages, check_pages;
1039099a0e58SBosko Milekic 
1040e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
1041e9a069d8SJohn Baldwin 	pages = howmany(bytes, PAGE_SIZE);
1042e9a069d8SJohn Baldwin 	check_pages = pages - 1;
1043e9a069d8SJohn Baldwin 	KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n"));
1044099a0e58SBosko Milekic 
1045009b6fcbSJeff Roberson 	/*
1046009b6fcbSJeff Roberson 	 * Check our small startup cache to see if it has pages remaining.
1047009b6fcbSJeff Roberson 	 */
1048f353d338SAlan Cox 	mtx_lock(&uma_boot_pages_mtx);
1049e9a069d8SJohn Baldwin 
1050e9a069d8SJohn Baldwin 	/* First check if we have enough room. */
1051e9a069d8SJohn Baldwin 	tmps = LIST_FIRST(&uma_boot_pages);
1052e9a069d8SJohn Baldwin 	while (tmps != NULL && check_pages-- > 0)
1053e9a069d8SJohn Baldwin 		tmps = LIST_NEXT(tmps, us_link);
1054e9a069d8SJohn Baldwin 	if (tmps != NULL) {
1055e9a069d8SJohn Baldwin 		/*
1056e9a069d8SJohn Baldwin 		 * It's ok to lose tmps references.  The last one will
1057e9a069d8SJohn Baldwin 		 * have tmps->us_data pointing to the start address of
1058e9a069d8SJohn Baldwin 		 * "pages" contiguous pages of memory.
1059e9a069d8SJohn Baldwin 		 */
1060e9a069d8SJohn Baldwin 		while (pages-- > 0) {
1061e9a069d8SJohn Baldwin 			tmps = LIST_FIRST(&uma_boot_pages);
1062009b6fcbSJeff Roberson 			LIST_REMOVE(tmps, us_link);
1063e9a069d8SJohn Baldwin 		}
1064f353d338SAlan Cox 		mtx_unlock(&uma_boot_pages_mtx);
1065009b6fcbSJeff Roberson 		*pflag = tmps->us_flags;
1066009b6fcbSJeff Roberson 		return (tmps->us_data);
1067009b6fcbSJeff Roberson 	}
1068f353d338SAlan Cox 	mtx_unlock(&uma_boot_pages_mtx);
1069342f1793SAlan Cox 	if (booted < UMA_STARTUP2)
10703803b26bSDag-Erling Smørgrav 		panic("UMA: Increase vm.boot_pages");
1071009b6fcbSJeff Roberson 	/*
1072009b6fcbSJeff Roberson 	 * Now that we've booted reset these users to their real allocator.
1073009b6fcbSJeff Roberson 	 */
1074009b6fcbSJeff Roberson #ifdef UMA_MD_SMALL_ALLOC
1075e9a069d8SJohn Baldwin 	keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc;
1076009b6fcbSJeff Roberson #else
1077099a0e58SBosko Milekic 	keg->uk_allocf = page_alloc;
1078009b6fcbSJeff Roberson #endif
1079099a0e58SBosko Milekic 	return keg->uk_allocf(zone, bytes, pflag, wait);
1080009b6fcbSJeff Roberson }
1081009b6fcbSJeff Roberson 
1082009b6fcbSJeff Roberson /*
10838355f576SJeff Roberson  * Allocates a number of pages from the system
10848355f576SJeff Roberson  *
10858355f576SJeff Roberson  * Arguments:
10868355f576SJeff Roberson  *	bytes  The number of bytes requested
10878355f576SJeff Roberson  *	wait  Shall we wait?
10888355f576SJeff Roberson  *
10898355f576SJeff Roberson  * Returns:
10908355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
10918355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
10928355f576SJeff Roberson  */
10938355f576SJeff Roberson static void *
1094f2c2231eSRyan Stone page_alloc(uma_zone_t zone, vm_size_t bytes, uint8_t *pflag, int wait)
10958355f576SJeff Roberson {
10968355f576SJeff Roberson 	void *p;	/* Returned page */
10978355f576SJeff Roberson 
10988355f576SJeff Roberson 	*pflag = UMA_SLAB_KMEM;
10995df87b21SJeff Roberson 	p = (void *) kmem_malloc(kmem_arena, bytes, wait);
11008355f576SJeff Roberson 
11018355f576SJeff Roberson 	return (p);
11028355f576SJeff Roberson }
11038355f576SJeff Roberson 
11048355f576SJeff Roberson /*
11058355f576SJeff Roberson  * Allocates a number of pages from within an object
11068355f576SJeff Roberson  *
11078355f576SJeff Roberson  * Arguments:
11088355f576SJeff Roberson  *	bytes  The number of bytes requested
11098355f576SJeff Roberson  *	wait   Shall we wait?
11108355f576SJeff Roberson  *
11118355f576SJeff Roberson  * Returns:
11128355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
11138355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
11148355f576SJeff Roberson  */
11158355f576SJeff Roberson static void *
1116f2c2231eSRyan Stone noobj_alloc(uma_zone_t zone, vm_size_t bytes, uint8_t *flags, int wait)
11178355f576SJeff Roberson {
1118a4915c21SAttilio Rao 	TAILQ_HEAD(, vm_page) alloctail;
1119a4915c21SAttilio Rao 	u_long npages;
1120b245ac95SAlan Cox 	vm_offset_t retkva, zkva;
1121a4915c21SAttilio Rao 	vm_page_t p, p_next;
1122e20a199fSJeff Roberson 	uma_keg_t keg;
11238355f576SJeff Roberson 
1124a4915c21SAttilio Rao 	TAILQ_INIT(&alloctail);
1125e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
1126a4915c21SAttilio Rao 
1127a4915c21SAttilio Rao 	npages = howmany(bytes, PAGE_SIZE);
1128a4915c21SAttilio Rao 	while (npages > 0) {
1129a4915c21SAttilio Rao 		p = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT |
1130a4915c21SAttilio Rao 		    VM_ALLOC_WIRED | VM_ALLOC_NOOBJ);
1131a4915c21SAttilio Rao 		if (p != NULL) {
1132a4915c21SAttilio Rao 			/*
1133a4915c21SAttilio Rao 			 * Since the page does not belong to an object, its
1134a4915c21SAttilio Rao 			 * listq is unused.
1135a4915c21SAttilio Rao 			 */
1136a4915c21SAttilio Rao 			TAILQ_INSERT_TAIL(&alloctail, p, listq);
1137a4915c21SAttilio Rao 			npages--;
1138a4915c21SAttilio Rao 			continue;
1139a4915c21SAttilio Rao 		}
1140a4915c21SAttilio Rao 		if (wait & M_WAITOK) {
1141a4915c21SAttilio Rao 			VM_WAIT;
1142a4915c21SAttilio Rao 			continue;
1143a4915c21SAttilio Rao 		}
11448355f576SJeff Roberson 
11458355f576SJeff Roberson 		/*
1146a4915c21SAttilio Rao 		 * Page allocation failed, free intermediate pages and
1147a4915c21SAttilio Rao 		 * exit.
11488355f576SJeff Roberson 		 */
1149a4915c21SAttilio Rao 		TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1150087a6132SAlan Cox 			vm_page_unwire(p, PQ_NONE);
1151b245ac95SAlan Cox 			vm_page_free(p);
1152b245ac95SAlan Cox 		}
1153a4915c21SAttilio Rao 		return (NULL);
1154b245ac95SAlan Cox 	}
11558355f576SJeff Roberson 	*flags = UMA_SLAB_PRIV;
1156a4915c21SAttilio Rao 	zkva = keg->uk_kva +
1157a4915c21SAttilio Rao 	    atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1158a4915c21SAttilio Rao 	retkva = zkva;
1159a4915c21SAttilio Rao 	TAILQ_FOREACH(p, &alloctail, listq) {
1160a4915c21SAttilio Rao 		pmap_qenter(zkva, &p, 1);
1161a4915c21SAttilio Rao 		zkva += PAGE_SIZE;
1162a4915c21SAttilio Rao 	}
11638355f576SJeff Roberson 
11648355f576SJeff Roberson 	return ((void *)retkva);
11658355f576SJeff Roberson }
11668355f576SJeff Roberson 
11678355f576SJeff Roberson /*
11688355f576SJeff Roberson  * Frees a number of pages to the system
11698355f576SJeff Roberson  *
11708355f576SJeff Roberson  * Arguments:
11718355f576SJeff Roberson  *	mem   A pointer to the memory to be freed
11728355f576SJeff Roberson  *	size  The size of the memory being freed
11738355f576SJeff Roberson  *	flags The original p->us_flags field
11748355f576SJeff Roberson  *
11758355f576SJeff Roberson  * Returns:
11768355f576SJeff Roberson  *	Nothing
11778355f576SJeff Roberson  */
11788355f576SJeff Roberson static void
1179f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags)
11808355f576SJeff Roberson {
11815df87b21SJeff Roberson 	struct vmem *vmem;
11823370c5bfSJeff Roberson 
11838355f576SJeff Roberson 	if (flags & UMA_SLAB_KMEM)
11845df87b21SJeff Roberson 		vmem = kmem_arena;
1185aea6e893SAlan Cox 	else if (flags & UMA_SLAB_KERNEL)
11865df87b21SJeff Roberson 		vmem = kernel_arena;
11878355f576SJeff Roberson 	else
1188b5345ef1SJustin Hibbits 		panic("UMA: page_free used with invalid flags %x", flags);
11898355f576SJeff Roberson 
11905df87b21SJeff Roberson 	kmem_free(vmem, (vm_offset_t)mem, size);
11918355f576SJeff Roberson }
11928355f576SJeff Roberson 
11938355f576SJeff Roberson /*
11948355f576SJeff Roberson  * Zero fill initializer
11958355f576SJeff Roberson  *
11968355f576SJeff Roberson  * Arguments/Returns follow uma_init specifications
11978355f576SJeff Roberson  */
1198b23f72e9SBrian Feldman static int
1199b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags)
12008355f576SJeff Roberson {
12018355f576SJeff Roberson 	bzero(mem, size);
1202b23f72e9SBrian Feldman 	return (0);
12038355f576SJeff Roberson }
12048355f576SJeff Roberson 
12058355f576SJeff Roberson /*
1206e20a199fSJeff Roberson  * Finish creating a small uma keg.  This calculates ipers, and the keg size.
12078355f576SJeff Roberson  *
12088355f576SJeff Roberson  * Arguments
1209e20a199fSJeff Roberson  *	keg  The zone we should initialize
12108355f576SJeff Roberson  *
12118355f576SJeff Roberson  * Returns
12128355f576SJeff Roberson  *	Nothing
12138355f576SJeff Roberson  */
12148355f576SJeff Roberson static void
1215e20a199fSJeff Roberson keg_small_init(uma_keg_t keg)
12168355f576SJeff Roberson {
1217244f4554SBosko Milekic 	u_int rsize;
1218244f4554SBosko Milekic 	u_int memused;
1219244f4554SBosko Milekic 	u_int wastedspace;
1220244f4554SBosko Milekic 	u_int shsize;
1221a55ebb7cSAndriy Gapon 	u_int slabsize;
12228355f576SJeff Roberson 
1223ad97af7eSGleb Smirnoff 	if (keg->uk_flags & UMA_ZONE_PCPU) {
122496c85efbSNathan Whitehorn 		u_int ncpus = (mp_maxid + 1) ? (mp_maxid + 1) : MAXCPU;
1225e28a647dSGleb Smirnoff 
1226a55ebb7cSAndriy Gapon 		slabsize = sizeof(struct pcpu);
1227e28a647dSGleb Smirnoff 		keg->uk_ppera = howmany(ncpus * sizeof(struct pcpu),
1228ad97af7eSGleb Smirnoff 		    PAGE_SIZE);
1229ad97af7eSGleb Smirnoff 	} else {
1230a55ebb7cSAndriy Gapon 		slabsize = UMA_SLAB_SIZE;
1231ad97af7eSGleb Smirnoff 		keg->uk_ppera = 1;
1232ad97af7eSGleb Smirnoff 	}
1233ad97af7eSGleb Smirnoff 
1234ef72505eSJeff Roberson 	/*
1235ef72505eSJeff Roberson 	 * Calculate the size of each allocation (rsize) according to
1236ef72505eSJeff Roberson 	 * alignment.  If the requested size is smaller than we have
1237ef72505eSJeff Roberson 	 * allocation bits for we round it up.
1238ef72505eSJeff Roberson 	 */
1239099a0e58SBosko Milekic 	rsize = keg->uk_size;
1240a55ebb7cSAndriy Gapon 	if (rsize < slabsize / SLAB_SETSIZE)
1241a55ebb7cSAndriy Gapon 		rsize = slabsize / SLAB_SETSIZE;
1242099a0e58SBosko Milekic 	if (rsize & keg->uk_align)
1243099a0e58SBosko Milekic 		rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1244099a0e58SBosko Milekic 	keg->uk_rsize = rsize;
1245ad97af7eSGleb Smirnoff 
1246ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
1247ad97af7eSGleb Smirnoff 	    keg->uk_rsize < sizeof(struct pcpu),
1248ad97af7eSGleb Smirnoff 	    ("%s: size %u too large", __func__, keg->uk_rsize));
12498355f576SJeff Roberson 
1250ef72505eSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_OFFPAGE)
12512864dbbfSGleb Smirnoff 		shsize = 0;
1252ef72505eSJeff Roberson 	else
1253244f4554SBosko Milekic 		shsize = sizeof(struct uma_slab);
12548355f576SJeff Roberson 
1255a55ebb7cSAndriy Gapon 	keg->uk_ipers = (slabsize - shsize) / rsize;
1256ef72505eSJeff Roberson 	KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1257ad97af7eSGleb Smirnoff 	    ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1258ad97af7eSGleb Smirnoff 
1259244f4554SBosko Milekic 	memused = keg->uk_ipers * rsize + shsize;
1260a55ebb7cSAndriy Gapon 	wastedspace = slabsize - memused;
1261244f4554SBosko Milekic 
126220e8e865SBosko Milekic 	/*
1263244f4554SBosko Milekic 	 * We can't do OFFPAGE if we're internal or if we've been
126420e8e865SBosko Milekic 	 * asked to not go to the VM for buckets.  If we do this we
12656fd34d6fSJeff Roberson 	 * may end up going to the VM  for slabs which we do not
12666fd34d6fSJeff Roberson 	 * want to do if we're UMA_ZFLAG_CACHEONLY as a result
12676fd34d6fSJeff Roberson 	 * of UMA_ZONE_VM, which clearly forbids it.
126820e8e865SBosko Milekic 	 */
1269099a0e58SBosko Milekic 	if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1270099a0e58SBosko Milekic 	    (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
12718355f576SJeff Roberson 		return;
1272244f4554SBosko Milekic 
1273ef72505eSJeff Roberson 	/*
1274ef72505eSJeff Roberson 	 * See if using an OFFPAGE slab will limit our waste.  Only do
1275ef72505eSJeff Roberson 	 * this if it permits more items per-slab.
1276ef72505eSJeff Roberson 	 *
1277ef72505eSJeff Roberson 	 * XXX We could try growing slabsize to limit max waste as well.
1278ef72505eSJeff Roberson 	 * Historically this was not done because the VM could not
1279ef72505eSJeff Roberson 	 * efficiently handle contiguous allocations.
1280ef72505eSJeff Roberson 	 */
1281a55ebb7cSAndriy Gapon 	if ((wastedspace >= slabsize / UMA_MAX_WASTE) &&
1282a55ebb7cSAndriy Gapon 	    (keg->uk_ipers < (slabsize / keg->uk_rsize))) {
1283a55ebb7cSAndriy Gapon 		keg->uk_ipers = slabsize / keg->uk_rsize;
1284ef72505eSJeff Roberson 		KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1285ad97af7eSGleb Smirnoff 		    ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1286244f4554SBosko Milekic #ifdef UMA_DEBUG
1287244f4554SBosko Milekic 		printf("UMA decided we need offpage slab headers for "
1288e20a199fSJeff Roberson 		    "keg: %s, calculated wastedspace = %d, "
1289244f4554SBosko Milekic 		    "maximum wasted space allowed = %d, "
1290244f4554SBosko Milekic 		    "calculated ipers = %d, "
1291e20a199fSJeff Roberson 		    "new wasted space = %d\n", keg->uk_name, wastedspace,
1292a55ebb7cSAndriy Gapon 		    slabsize / UMA_MAX_WASTE, keg->uk_ipers,
1293a55ebb7cSAndriy Gapon 		    slabsize - keg->uk_ipers * keg->uk_rsize);
1294244f4554SBosko Milekic #endif
1295099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZONE_OFFPAGE;
12968355f576SJeff Roberson 	}
1297ad97af7eSGleb Smirnoff 
1298ad97af7eSGleb Smirnoff 	if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1299ad97af7eSGleb Smirnoff 	    (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1300ad97af7eSGleb Smirnoff 		keg->uk_flags |= UMA_ZONE_HASH;
13018355f576SJeff Roberson }
13028355f576SJeff Roberson 
13038355f576SJeff Roberson /*
1304e20a199fSJeff Roberson  * Finish creating a large (> UMA_SLAB_SIZE) uma kegs.  Just give in and do
13058355f576SJeff Roberson  * OFFPAGE for now.  When I can allow for more dynamic slab sizes this will be
13068355f576SJeff Roberson  * more complicated.
13078355f576SJeff Roberson  *
13088355f576SJeff Roberson  * Arguments
1309e20a199fSJeff Roberson  *	keg  The keg we should initialize
13108355f576SJeff Roberson  *
13118355f576SJeff Roberson  * Returns
13128355f576SJeff Roberson  *	Nothing
13138355f576SJeff Roberson  */
13148355f576SJeff Roberson static void
1315e20a199fSJeff Roberson keg_large_init(uma_keg_t keg)
13168355f576SJeff Roberson {
1317cec48e00SAlexander Motin 	u_int shsize;
13188355f576SJeff Roberson 
1319e20a199fSJeff Roberson 	KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1320099a0e58SBosko Milekic 	KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1321e20a199fSJeff Roberson 	    ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1322ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1323ad97af7eSGleb Smirnoff 	    ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__));
132420e8e865SBosko Milekic 
1325ad97af7eSGleb Smirnoff 	keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE);
1326099a0e58SBosko Milekic 	keg->uk_ipers = 1;
1327e9a069d8SJohn Baldwin 	keg->uk_rsize = keg->uk_size;
1328e9a069d8SJohn Baldwin 
1329e9a069d8SJohn Baldwin 	/* We can't do OFFPAGE if we're internal, bail out here. */
1330e9a069d8SJohn Baldwin 	if (keg->uk_flags & UMA_ZFLAG_INTERNAL)
1331e9a069d8SJohn Baldwin 		return;
13328355f576SJeff Roberson 
1333cec48e00SAlexander Motin 	/* Check whether we have enough space to not do OFFPAGE. */
1334cec48e00SAlexander Motin 	if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0) {
1335cec48e00SAlexander Motin 		shsize = sizeof(struct uma_slab);
1336cec48e00SAlexander Motin 		if (shsize & UMA_ALIGN_PTR)
1337cec48e00SAlexander Motin 			shsize = (shsize & ~UMA_ALIGN_PTR) +
1338cec48e00SAlexander Motin 			    (UMA_ALIGN_PTR + 1);
1339cec48e00SAlexander Motin 
1340cec48e00SAlexander Motin 		if ((PAGE_SIZE * keg->uk_ppera) - keg->uk_rsize < shsize)
1341099a0e58SBosko Milekic 			keg->uk_flags |= UMA_ZONE_OFFPAGE;
1342cec48e00SAlexander Motin 	}
1343cec48e00SAlexander Motin 
1344cec48e00SAlexander Motin 	if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1345cec48e00SAlexander Motin 	    (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1346099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZONE_HASH;
13478355f576SJeff Roberson }
13488355f576SJeff Roberson 
1349e20a199fSJeff Roberson static void
1350e20a199fSJeff Roberson keg_cachespread_init(uma_keg_t keg)
1351e20a199fSJeff Roberson {
1352e20a199fSJeff Roberson 	int alignsize;
1353e20a199fSJeff Roberson 	int trailer;
1354e20a199fSJeff Roberson 	int pages;
1355e20a199fSJeff Roberson 	int rsize;
1356e20a199fSJeff Roberson 
1357ad97af7eSGleb Smirnoff 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1358ad97af7eSGleb Smirnoff 	    ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__));
1359ad97af7eSGleb Smirnoff 
1360e20a199fSJeff Roberson 	alignsize = keg->uk_align + 1;
1361e20a199fSJeff Roberson 	rsize = keg->uk_size;
1362e20a199fSJeff Roberson 	/*
1363e20a199fSJeff Roberson 	 * We want one item to start on every align boundary in a page.  To
1364e20a199fSJeff Roberson 	 * do this we will span pages.  We will also extend the item by the
1365e20a199fSJeff Roberson 	 * size of align if it is an even multiple of align.  Otherwise, it
1366e20a199fSJeff Roberson 	 * would fall on the same boundary every time.
1367e20a199fSJeff Roberson 	 */
1368e20a199fSJeff Roberson 	if (rsize & keg->uk_align)
1369e20a199fSJeff Roberson 		rsize = (rsize & ~keg->uk_align) + alignsize;
1370e20a199fSJeff Roberson 	if ((rsize & alignsize) == 0)
1371e20a199fSJeff Roberson 		rsize += alignsize;
1372e20a199fSJeff Roberson 	trailer = rsize - keg->uk_size;
1373e20a199fSJeff Roberson 	pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1374e20a199fSJeff Roberson 	pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1375e20a199fSJeff Roberson 	keg->uk_rsize = rsize;
1376e20a199fSJeff Roberson 	keg->uk_ppera = pages;
1377e20a199fSJeff Roberson 	keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1378e20a199fSJeff Roberson 	keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
13792367b4ddSDimitry Andric 	KASSERT(keg->uk_ipers <= SLAB_SETSIZE,
138042321809SGleb Smirnoff 	    ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1381e20a199fSJeff Roberson 	    keg->uk_ipers));
1382e20a199fSJeff Roberson }
1383e20a199fSJeff Roberson 
13848355f576SJeff Roberson /*
1385099a0e58SBosko Milekic  * Keg header ctor.  This initializes all fields, locks, etc.  And inserts
1386099a0e58SBosko Milekic  * the keg onto the global keg list.
13878355f576SJeff Roberson  *
13888355f576SJeff Roberson  * Arguments/Returns follow uma_ctor specifications
1389099a0e58SBosko Milekic  *	udata  Actually uma_kctor_args
1390099a0e58SBosko Milekic  */
1391b23f72e9SBrian Feldman static int
1392b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags)
1393099a0e58SBosko Milekic {
1394099a0e58SBosko Milekic 	struct uma_kctor_args *arg = udata;
1395099a0e58SBosko Milekic 	uma_keg_t keg = mem;
1396099a0e58SBosko Milekic 	uma_zone_t zone;
1397099a0e58SBosko Milekic 
1398099a0e58SBosko Milekic 	bzero(keg, size);
1399099a0e58SBosko Milekic 	keg->uk_size = arg->size;
1400099a0e58SBosko Milekic 	keg->uk_init = arg->uminit;
1401099a0e58SBosko Milekic 	keg->uk_fini = arg->fini;
1402099a0e58SBosko Milekic 	keg->uk_align = arg->align;
1403099a0e58SBosko Milekic 	keg->uk_free = 0;
14046fd34d6fSJeff Roberson 	keg->uk_reserve = 0;
1405099a0e58SBosko Milekic 	keg->uk_pages = 0;
1406099a0e58SBosko Milekic 	keg->uk_flags = arg->flags;
1407099a0e58SBosko Milekic 	keg->uk_allocf = page_alloc;
1408099a0e58SBosko Milekic 	keg->uk_freef = page_free;
1409099a0e58SBosko Milekic 	keg->uk_slabzone = NULL;
1410099a0e58SBosko Milekic 
1411099a0e58SBosko Milekic 	/*
1412099a0e58SBosko Milekic 	 * The master zone is passed to us at keg-creation time.
1413099a0e58SBosko Milekic 	 */
1414099a0e58SBosko Milekic 	zone = arg->zone;
1415e20a199fSJeff Roberson 	keg->uk_name = zone->uz_name;
1416099a0e58SBosko Milekic 
1417099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_VM)
1418099a0e58SBosko Milekic 		keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1419099a0e58SBosko Milekic 
1420099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_ZINIT)
1421099a0e58SBosko Milekic 		keg->uk_init = zero_init;
1422099a0e58SBosko Milekic 
1423cfcae3f8SGleb Smirnoff 	if (arg->flags & UMA_ZONE_MALLOC)
1424e20a199fSJeff Roberson 		keg->uk_flags |= UMA_ZONE_VTOSLAB;
1425e20a199fSJeff Roberson 
1426ad97af7eSGleb Smirnoff 	if (arg->flags & UMA_ZONE_PCPU)
1427ad97af7eSGleb Smirnoff #ifdef SMP
1428ad97af7eSGleb Smirnoff 		keg->uk_flags |= UMA_ZONE_OFFPAGE;
1429ad97af7eSGleb Smirnoff #else
1430ad97af7eSGleb Smirnoff 		keg->uk_flags &= ~UMA_ZONE_PCPU;
1431ad97af7eSGleb Smirnoff #endif
1432ad97af7eSGleb Smirnoff 
1433ef72505eSJeff Roberson 	if (keg->uk_flags & UMA_ZONE_CACHESPREAD) {
1434e20a199fSJeff Roberson 		keg_cachespread_init(keg);
1435244f4554SBosko Milekic 	} else {
1436ef72505eSJeff Roberson 		if (keg->uk_size > (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1437e20a199fSJeff Roberson 			keg_large_init(keg);
1438244f4554SBosko Milekic 		else
1439e20a199fSJeff Roberson 			keg_small_init(keg);
1440244f4554SBosko Milekic 	}
1441099a0e58SBosko Milekic 
1442cfcae3f8SGleb Smirnoff 	if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1443099a0e58SBosko Milekic 		keg->uk_slabzone = slabzone;
1444099a0e58SBosko Milekic 
1445099a0e58SBosko Milekic 	/*
1446099a0e58SBosko Milekic 	 * If we haven't booted yet we need allocations to go through the
1447099a0e58SBosko Milekic 	 * startup cache until the vm is ready.
1448099a0e58SBosko Milekic 	 */
1449099a0e58SBosko Milekic 	if (keg->uk_ppera == 1) {
1450099a0e58SBosko Milekic #ifdef UMA_MD_SMALL_ALLOC
1451099a0e58SBosko Milekic 		keg->uk_allocf = uma_small_alloc;
1452099a0e58SBosko Milekic 		keg->uk_freef = uma_small_free;
14538cd02d00SAlan Cox 
1454342f1793SAlan Cox 		if (booted < UMA_STARTUP)
1455099a0e58SBosko Milekic 			keg->uk_allocf = startup_alloc;
14568cd02d00SAlan Cox #else
14578cd02d00SAlan Cox 		if (booted < UMA_STARTUP2)
14588cd02d00SAlan Cox 			keg->uk_allocf = startup_alloc;
14598cd02d00SAlan Cox #endif
1460342f1793SAlan Cox 	} else if (booted < UMA_STARTUP2 &&
1461342f1793SAlan Cox 	    (keg->uk_flags & UMA_ZFLAG_INTERNAL))
1462e9a069d8SJohn Baldwin 		keg->uk_allocf = startup_alloc;
1463099a0e58SBosko Milekic 
1464099a0e58SBosko Milekic 	/*
1465af526374SJeff Roberson 	 * Initialize keg's lock
1466099a0e58SBosko Milekic 	 */
1467af526374SJeff Roberson 	KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS));
1468099a0e58SBosko Milekic 
1469099a0e58SBosko Milekic 	/*
1470099a0e58SBosko Milekic 	 * If we're putting the slab header in the actual page we need to
1471099a0e58SBosko Milekic 	 * figure out where in each page it goes.  This calculates a right
1472099a0e58SBosko Milekic 	 * justified offset into the memory on an ALIGN_PTR boundary.
1473099a0e58SBosko Milekic 	 */
1474099a0e58SBosko Milekic 	if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1475244f4554SBosko Milekic 		u_int totsize;
1476099a0e58SBosko Milekic 
1477099a0e58SBosko Milekic 		/* Size of the slab struct and free list */
1478ef72505eSJeff Roberson 		totsize = sizeof(struct uma_slab);
1479ef72505eSJeff Roberson 
1480099a0e58SBosko Milekic 		if (totsize & UMA_ALIGN_PTR)
1481099a0e58SBosko Milekic 			totsize = (totsize & ~UMA_ALIGN_PTR) +
1482099a0e58SBosko Milekic 			    (UMA_ALIGN_PTR + 1);
1483ad97af7eSGleb Smirnoff 		keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize;
1484244f4554SBosko Milekic 
1485244f4554SBosko Milekic 		/*
1486244f4554SBosko Milekic 		 * The only way the following is possible is if with our
1487244f4554SBosko Milekic 		 * UMA_ALIGN_PTR adjustments we are now bigger than
1488244f4554SBosko Milekic 		 * UMA_SLAB_SIZE.  I haven't checked whether this is
1489244f4554SBosko Milekic 		 * mathematically possible for all cases, so we make
1490244f4554SBosko Milekic 		 * sure here anyway.
1491244f4554SBosko Milekic 		 */
1492ef72505eSJeff Roberson 		totsize = keg->uk_pgoff + sizeof(struct uma_slab);
1493ad97af7eSGleb Smirnoff 		if (totsize > PAGE_SIZE * keg->uk_ppera) {
1494099a0e58SBosko Milekic 			printf("zone %s ipers %d rsize %d size %d\n",
1495099a0e58SBosko Milekic 			    zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1496099a0e58SBosko Milekic 			    keg->uk_size);
1497aea6e893SAlan Cox 			panic("UMA slab won't fit.");
1498099a0e58SBosko Milekic 		}
1499099a0e58SBosko Milekic 	}
1500099a0e58SBosko Milekic 
1501099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZONE_HASH)
1502099a0e58SBosko Milekic 		hash_alloc(&keg->uk_hash);
1503099a0e58SBosko Milekic 
1504099a0e58SBosko Milekic #ifdef UMA_DEBUG
15050b80c1e4SEitan Adler 	printf("UMA: %s(%p) size %d(%d) flags %#x ipers %d ppera %d out %d free %d\n",
1506e20a199fSJeff Roberson 	    zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
1507e20a199fSJeff Roberson 	    keg->uk_ipers, keg->uk_ppera,
150857223e99SAndriy Gapon 	    (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
150957223e99SAndriy Gapon 	    keg->uk_free);
1510099a0e58SBosko Milekic #endif
1511099a0e58SBosko Milekic 
1512099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1513099a0e58SBosko Milekic 
1514111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
1515099a0e58SBosko Milekic 	LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1516111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
1517b23f72e9SBrian Feldman 	return (0);
1518099a0e58SBosko Milekic }
1519099a0e58SBosko Milekic 
1520099a0e58SBosko Milekic /*
1521099a0e58SBosko Milekic  * Zone header ctor.  This initializes all fields, locks, etc.
1522099a0e58SBosko Milekic  *
1523099a0e58SBosko Milekic  * Arguments/Returns follow uma_ctor specifications
1524099a0e58SBosko Milekic  *	udata  Actually uma_zctor_args
15258355f576SJeff Roberson  */
1526b23f72e9SBrian Feldman static int
1527b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags)
15288355f576SJeff Roberson {
15298355f576SJeff Roberson 	struct uma_zctor_args *arg = udata;
15308355f576SJeff Roberson 	uma_zone_t zone = mem;
1531099a0e58SBosko Milekic 	uma_zone_t z;
1532099a0e58SBosko Milekic 	uma_keg_t keg;
15338355f576SJeff Roberson 
15348355f576SJeff Roberson 	bzero(zone, size);
15358355f576SJeff Roberson 	zone->uz_name = arg->name;
15368355f576SJeff Roberson 	zone->uz_ctor = arg->ctor;
15378355f576SJeff Roberson 	zone->uz_dtor = arg->dtor;
1538e20a199fSJeff Roberson 	zone->uz_slab = zone_fetch_slab;
1539099a0e58SBosko Milekic 	zone->uz_init = NULL;
1540099a0e58SBosko Milekic 	zone->uz_fini = NULL;
1541099a0e58SBosko Milekic 	zone->uz_allocs = 0;
1542773df9abSRobert Watson 	zone->uz_frees = 0;
15432019094aSRobert Watson 	zone->uz_fails = 0;
1544bf965959SSean Bruno 	zone->uz_sleeps = 0;
1545fc03d22bSJeff Roberson 	zone->uz_count = 0;
1546ace66b56SAlexander Motin 	zone->uz_count_min = 0;
1547e20a199fSJeff Roberson 	zone->uz_flags = 0;
15482f891cd5SPawel Jakub Dawidek 	zone->uz_warning = NULL;
15492f891cd5SPawel Jakub Dawidek 	timevalclear(&zone->uz_ratecheck);
1550e20a199fSJeff Roberson 	keg = arg->keg;
1551099a0e58SBosko Milekic 
1552af526374SJeff Roberson 	ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
1553af526374SJeff Roberson 
15540095a784SJeff Roberson 	/*
15550095a784SJeff Roberson 	 * This is a pure cache zone, no kegs.
15560095a784SJeff Roberson 	 */
15570095a784SJeff Roberson 	if (arg->import) {
15586fd34d6fSJeff Roberson 		if (arg->flags & UMA_ZONE_VM)
15596fd34d6fSJeff Roberson 			arg->flags |= UMA_ZFLAG_CACHEONLY;
15606fd34d6fSJeff Roberson 		zone->uz_flags = arg->flags;
1561af526374SJeff Roberson 		zone->uz_size = arg->size;
15620095a784SJeff Roberson 		zone->uz_import = arg->import;
15630095a784SJeff Roberson 		zone->uz_release = arg->release;
15640095a784SJeff Roberson 		zone->uz_arg = arg->arg;
1565af526374SJeff Roberson 		zone->uz_lockptr = &zone->uz_lock;
1566111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
156703175483SAlexander Motin 		LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
1568111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
1569af526374SJeff Roberson 		goto out;
15700095a784SJeff Roberson 	}
15710095a784SJeff Roberson 
15720095a784SJeff Roberson 	/*
15730095a784SJeff Roberson 	 * Use the regular zone/keg/slab allocator.
15740095a784SJeff Roberson 	 */
15750095a784SJeff Roberson 	zone->uz_import = (uma_import)zone_import;
15760095a784SJeff Roberson 	zone->uz_release = (uma_release)zone_release;
15770095a784SJeff Roberson 	zone->uz_arg = zone;
15780095a784SJeff Roberson 
1579099a0e58SBosko Milekic 	if (arg->flags & UMA_ZONE_SECONDARY) {
1580099a0e58SBosko Milekic 		KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
15818355f576SJeff Roberson 		zone->uz_init = arg->uminit;
1582e221e841SJeff Roberson 		zone->uz_fini = arg->fini;
1583af526374SJeff Roberson 		zone->uz_lockptr = &keg->uk_lock;
1584e20a199fSJeff Roberson 		zone->uz_flags |= UMA_ZONE_SECONDARY;
1585111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
1586099a0e58SBosko Milekic 		ZONE_LOCK(zone);
1587099a0e58SBosko Milekic 		LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1588099a0e58SBosko Milekic 			if (LIST_NEXT(z, uz_link) == NULL) {
1589099a0e58SBosko Milekic 				LIST_INSERT_AFTER(z, zone, uz_link);
1590099a0e58SBosko Milekic 				break;
1591099a0e58SBosko Milekic 			}
1592099a0e58SBosko Milekic 		}
1593099a0e58SBosko Milekic 		ZONE_UNLOCK(zone);
1594111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
1595e20a199fSJeff Roberson 	} else if (keg == NULL) {
1596e20a199fSJeff Roberson 		if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1597e20a199fSJeff Roberson 		    arg->align, arg->flags)) == NULL)
1598b23f72e9SBrian Feldman 			return (ENOMEM);
1599099a0e58SBosko Milekic 	} else {
1600099a0e58SBosko Milekic 		struct uma_kctor_args karg;
1601b23f72e9SBrian Feldman 		int error;
1602099a0e58SBosko Milekic 
1603099a0e58SBosko Milekic 		/* We should only be here from uma_startup() */
1604099a0e58SBosko Milekic 		karg.size = arg->size;
1605099a0e58SBosko Milekic 		karg.uminit = arg->uminit;
1606099a0e58SBosko Milekic 		karg.fini = arg->fini;
1607099a0e58SBosko Milekic 		karg.align = arg->align;
1608099a0e58SBosko Milekic 		karg.flags = arg->flags;
1609099a0e58SBosko Milekic 		karg.zone = zone;
1610b23f72e9SBrian Feldman 		error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1611b23f72e9SBrian Feldman 		    flags);
1612b23f72e9SBrian Feldman 		if (error)
1613b23f72e9SBrian Feldman 			return (error);
1614099a0e58SBosko Milekic 	}
16150095a784SJeff Roberson 
1616e20a199fSJeff Roberson 	/*
1617e20a199fSJeff Roberson 	 * Link in the first keg.
1618e20a199fSJeff Roberson 	 */
1619e20a199fSJeff Roberson 	zone->uz_klink.kl_keg = keg;
1620e20a199fSJeff Roberson 	LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1621af526374SJeff Roberson 	zone->uz_lockptr = &keg->uk_lock;
1622e20a199fSJeff Roberson 	zone->uz_size = keg->uk_size;
1623e20a199fSJeff Roberson 	zone->uz_flags |= (keg->uk_flags &
1624e20a199fSJeff Roberson 	    (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
16258355f576SJeff Roberson 
16268355f576SJeff Roberson 	/*
16278355f576SJeff Roberson 	 * Some internal zones don't have room allocated for the per cpu
16288355f576SJeff Roberson 	 * caches.  If we're internal, bail out here.
16298355f576SJeff Roberson 	 */
1630099a0e58SBosko Milekic 	if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1631e20a199fSJeff Roberson 		KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1632099a0e58SBosko Milekic 		    ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1633b23f72e9SBrian Feldman 		return (0);
1634099a0e58SBosko Milekic 	}
16358355f576SJeff Roberson 
1636af526374SJeff Roberson out:
1637af526374SJeff Roberson 	if ((arg->flags & UMA_ZONE_MAXBUCKET) == 0)
1638af526374SJeff Roberson 		zone->uz_count = bucket_select(zone->uz_size);
16398355f576SJeff Roberson 	else
1640cae33c14SJeff Roberson 		zone->uz_count = BUCKET_MAX;
1641ace66b56SAlexander Motin 	zone->uz_count_min = zone->uz_count;
1642fc03d22bSJeff Roberson 
1643b23f72e9SBrian Feldman 	return (0);
16448355f576SJeff Roberson }
16458355f576SJeff Roberson 
16468355f576SJeff Roberson /*
1647099a0e58SBosko Milekic  * Keg header dtor.  This frees all data, destroys locks, frees the hash
1648099a0e58SBosko Milekic  * table and removes the keg from the global list.
16499c2cd7e5SJeff Roberson  *
16509c2cd7e5SJeff Roberson  * Arguments/Returns follow uma_dtor specifications
16519c2cd7e5SJeff Roberson  *	udata  unused
16529c2cd7e5SJeff Roberson  */
1653099a0e58SBosko Milekic static void
1654099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata)
1655099a0e58SBosko Milekic {
1656099a0e58SBosko Milekic 	uma_keg_t keg;
16579c2cd7e5SJeff Roberson 
1658099a0e58SBosko Milekic 	keg = (uma_keg_t)arg;
1659e20a199fSJeff Roberson 	KEG_LOCK(keg);
1660099a0e58SBosko Milekic 	if (keg->uk_free != 0) {
1661a3845534SCraig Rodrigues 		printf("Freed UMA keg (%s) was not empty (%d items). "
1662099a0e58SBosko Milekic 		    " Lost %d pages of memory.\n",
1663a3845534SCraig Rodrigues 		    keg->uk_name ? keg->uk_name : "",
1664099a0e58SBosko Milekic 		    keg->uk_free, keg->uk_pages);
1665099a0e58SBosko Milekic 	}
1666e20a199fSJeff Roberson 	KEG_UNLOCK(keg);
1667099a0e58SBosko Milekic 
1668099a0e58SBosko Milekic 	hash_free(&keg->uk_hash);
1669099a0e58SBosko Milekic 
1670e20a199fSJeff Roberson 	KEG_LOCK_FINI(keg);
1671099a0e58SBosko Milekic }
1672099a0e58SBosko Milekic 
1673099a0e58SBosko Milekic /*
1674099a0e58SBosko Milekic  * Zone header dtor.
1675099a0e58SBosko Milekic  *
1676099a0e58SBosko Milekic  * Arguments/Returns follow uma_dtor specifications
1677099a0e58SBosko Milekic  *	udata  unused
1678099a0e58SBosko Milekic  */
16799c2cd7e5SJeff Roberson static void
16809c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata)
16819c2cd7e5SJeff Roberson {
1682e20a199fSJeff Roberson 	uma_klink_t klink;
16839c2cd7e5SJeff Roberson 	uma_zone_t zone;
1684099a0e58SBosko Milekic 	uma_keg_t keg;
16859c2cd7e5SJeff Roberson 
16869c2cd7e5SJeff Roberson 	zone = (uma_zone_t)arg;
1687e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
16889643769aSJeff Roberson 
1689e20a199fSJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
16909643769aSJeff Roberson 		cache_drain(zone);
1691099a0e58SBosko Milekic 
1692111fbcd5SBryan Venteicher 	rw_wlock(&uma_rwlock);
1693099a0e58SBosko Milekic 	LIST_REMOVE(zone, uz_link);
1694111fbcd5SBryan Venteicher 	rw_wunlock(&uma_rwlock);
1695099a0e58SBosko Milekic 	/*
1696099a0e58SBosko Milekic 	 * XXX there are some races here where
1697099a0e58SBosko Milekic 	 * the zone can be drained but zone lock
1698099a0e58SBosko Milekic 	 * released and then refilled before we
1699099a0e58SBosko Milekic 	 * remove it... we dont care for now
1700099a0e58SBosko Milekic 	 */
1701e20a199fSJeff Roberson 	zone_drain_wait(zone, M_WAITOK);
1702e20a199fSJeff Roberson 	/*
1703e20a199fSJeff Roberson 	 * Unlink all of our kegs.
1704e20a199fSJeff Roberson 	 */
1705e20a199fSJeff Roberson 	while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1706e20a199fSJeff Roberson 		klink->kl_keg = NULL;
1707e20a199fSJeff Roberson 		LIST_REMOVE(klink, kl_link);
1708e20a199fSJeff Roberson 		if (klink == &zone->uz_klink)
1709e20a199fSJeff Roberson 			continue;
1710e20a199fSJeff Roberson 		free(klink, M_TEMP);
1711e20a199fSJeff Roberson 	}
1712e20a199fSJeff Roberson 	/*
1713e20a199fSJeff Roberson 	 * We only destroy kegs from non secondary zones.
1714e20a199fSJeff Roberson 	 */
17150095a784SJeff Roberson 	if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0)  {
1716111fbcd5SBryan Venteicher 		rw_wlock(&uma_rwlock);
1717099a0e58SBosko Milekic 		LIST_REMOVE(keg, uk_link);
1718111fbcd5SBryan Venteicher 		rw_wunlock(&uma_rwlock);
17190095a784SJeff Roberson 		zone_free_item(kegs, keg, NULL, SKIP_NONE);
17209c2cd7e5SJeff Roberson 	}
1721af526374SJeff Roberson 	ZONE_LOCK_FINI(zone);
1722099a0e58SBosko Milekic }
1723099a0e58SBosko Milekic 
17249c2cd7e5SJeff Roberson /*
17258355f576SJeff Roberson  * Traverses every zone in the system and calls a callback
17268355f576SJeff Roberson  *
17278355f576SJeff Roberson  * Arguments:
17288355f576SJeff Roberson  *	zfunc  A pointer to a function which accepts a zone
17298355f576SJeff Roberson  *		as an argument.
17308355f576SJeff Roberson  *
17318355f576SJeff Roberson  * Returns:
17328355f576SJeff Roberson  *	Nothing
17338355f576SJeff Roberson  */
17348355f576SJeff Roberson static void
17358355f576SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t))
17368355f576SJeff Roberson {
1737099a0e58SBosko Milekic 	uma_keg_t keg;
17388355f576SJeff Roberson 	uma_zone_t zone;
17398355f576SJeff Roberson 
1740111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
1741099a0e58SBosko Milekic 	LIST_FOREACH(keg, &uma_kegs, uk_link) {
1742099a0e58SBosko Milekic 		LIST_FOREACH(zone, &keg->uk_zones, uz_link)
17438355f576SJeff Roberson 			zfunc(zone);
1744099a0e58SBosko Milekic 	}
1745111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
17468355f576SJeff Roberson }
17478355f576SJeff Roberson 
17488355f576SJeff Roberson /* Public functions */
17498355f576SJeff Roberson /* See uma.h */
17508355f576SJeff Roberson void
17513803b26bSDag-Erling Smørgrav uma_startup(void *bootmem, int boot_pages)
17528355f576SJeff Roberson {
17538355f576SJeff Roberson 	struct uma_zctor_args args;
17548355f576SJeff Roberson 	uma_slab_t slab;
17558355f576SJeff Roberson 	int i;
17568355f576SJeff Roberson 
17578355f576SJeff Roberson #ifdef UMA_DEBUG
1758099a0e58SBosko Milekic 	printf("Creating uma keg headers zone and keg.\n");
17598355f576SJeff Roberson #endif
1760111fbcd5SBryan Venteicher 	rw_init(&uma_rwlock, "UMA lock");
1761099a0e58SBosko Milekic 
1762099a0e58SBosko Milekic 	/* "manually" create the initial zone */
17630095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
1764099a0e58SBosko Milekic 	args.name = "UMA Kegs";
1765099a0e58SBosko Milekic 	args.size = sizeof(struct uma_keg);
1766099a0e58SBosko Milekic 	args.ctor = keg_ctor;
1767099a0e58SBosko Milekic 	args.dtor = keg_dtor;
17688355f576SJeff Roberson 	args.uminit = zero_init;
17698355f576SJeff Roberson 	args.fini = NULL;
1770099a0e58SBosko Milekic 	args.keg = &masterkeg;
17718355f576SJeff Roberson 	args.align = 32 - 1;
1772b60f5b79SJeff Roberson 	args.flags = UMA_ZFLAG_INTERNAL;
17738355f576SJeff Roberson 	/* The initial zone has no Per cpu queues so it's smaller */
1774b23f72e9SBrian Feldman 	zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
17758355f576SJeff Roberson 
17768355f576SJeff Roberson #ifdef UMA_DEBUG
17778355f576SJeff Roberson 	printf("Filling boot free list.\n");
17788355f576SJeff Roberson #endif
17793803b26bSDag-Erling Smørgrav 	for (i = 0; i < boot_pages; i++) {
178085dcf349SGleb Smirnoff 		slab = (uma_slab_t)((uint8_t *)bootmem + (i * UMA_SLAB_SIZE));
178185dcf349SGleb Smirnoff 		slab->us_data = (uint8_t *)slab;
17828355f576SJeff Roberson 		slab->us_flags = UMA_SLAB_BOOT;
17838355f576SJeff Roberson 		LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
17848355f576SJeff Roberson 	}
1785f353d338SAlan Cox 	mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
17868355f576SJeff Roberson 
17878355f576SJeff Roberson #ifdef UMA_DEBUG
1788099a0e58SBosko Milekic 	printf("Creating uma zone headers zone and keg.\n");
1789099a0e58SBosko Milekic #endif
1790099a0e58SBosko Milekic 	args.name = "UMA Zones";
1791099a0e58SBosko Milekic 	args.size = sizeof(struct uma_zone) +
179251cfb0beSDmitry Chagin 	    (sizeof(struct uma_cache) * (mp_maxid + 1));
1793099a0e58SBosko Milekic 	args.ctor = zone_ctor;
1794099a0e58SBosko Milekic 	args.dtor = zone_dtor;
1795099a0e58SBosko Milekic 	args.uminit = zero_init;
1796099a0e58SBosko Milekic 	args.fini = NULL;
1797099a0e58SBosko Milekic 	args.keg = NULL;
1798099a0e58SBosko Milekic 	args.align = 32 - 1;
1799099a0e58SBosko Milekic 	args.flags = UMA_ZFLAG_INTERNAL;
1800099a0e58SBosko Milekic 	/* The initial zone has no Per cpu queues so it's smaller */
1801b23f72e9SBrian Feldman 	zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1802099a0e58SBosko Milekic 
1803099a0e58SBosko Milekic #ifdef UMA_DEBUG
1804099a0e58SBosko Milekic 	printf("Creating slab and hash zones.\n");
18058355f576SJeff Roberson #endif
18068355f576SJeff Roberson 
18078355f576SJeff Roberson 	/* Now make a zone for slab headers */
18088355f576SJeff Roberson 	slabzone = uma_zcreate("UMA Slabs",
1809ef72505eSJeff Roberson 				sizeof(struct uma_slab),
18108355f576SJeff Roberson 				NULL, NULL, NULL, NULL,
1811b60f5b79SJeff Roberson 				UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
18128355f576SJeff Roberson 
18138355f576SJeff Roberson 	hashzone = uma_zcreate("UMA Hash",
18148355f576SJeff Roberson 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
18158355f576SJeff Roberson 	    NULL, NULL, NULL, NULL,
1816b60f5b79SJeff Roberson 	    UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
18178355f576SJeff Roberson 
1818cae33c14SJeff Roberson 	bucket_init();
18198355f576SJeff Roberson 
1820342f1793SAlan Cox 	booted = UMA_STARTUP;
18218355f576SJeff Roberson 
18228355f576SJeff Roberson #ifdef UMA_DEBUG
18238355f576SJeff Roberson 	printf("UMA startup complete.\n");
18248355f576SJeff Roberson #endif
18258355f576SJeff Roberson }
18268355f576SJeff Roberson 
18278355f576SJeff Roberson /* see uma.h */
18288355f576SJeff Roberson void
182999571dc3SJeff Roberson uma_startup2(void)
18308355f576SJeff Roberson {
1831342f1793SAlan Cox 	booted = UMA_STARTUP2;
183286bbae32SJeff Roberson 	bucket_enable();
183395c4bf75SKonstantin Belousov 	sx_init(&uma_drain_lock, "umadrain");
18348355f576SJeff Roberson #ifdef UMA_DEBUG
18358355f576SJeff Roberson 	printf("UMA startup2 complete.\n");
18368355f576SJeff Roberson #endif
18378355f576SJeff Roberson }
18388355f576SJeff Roberson 
18398355f576SJeff Roberson /*
18408355f576SJeff Roberson  * Initialize our callout handle
18418355f576SJeff Roberson  *
18428355f576SJeff Roberson  */
18438355f576SJeff Roberson 
18448355f576SJeff Roberson static void
18458355f576SJeff Roberson uma_startup3(void)
18468355f576SJeff Roberson {
18478355f576SJeff Roberson #ifdef UMA_DEBUG
18488355f576SJeff Roberson 	printf("Starting callout.\n");
18498355f576SJeff Roberson #endif
1850fd90e2edSJung-uk Kim 	callout_init(&uma_callout, 1);
18519643769aSJeff Roberson 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
18528355f576SJeff Roberson #ifdef UMA_DEBUG
18538355f576SJeff Roberson 	printf("UMA startup3 complete.\n");
18548355f576SJeff Roberson #endif
18558355f576SJeff Roberson }
18568355f576SJeff Roberson 
1857e20a199fSJeff Roberson static uma_keg_t
1858099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
185985dcf349SGleb Smirnoff 		int align, uint32_t flags)
1860099a0e58SBosko Milekic {
1861099a0e58SBosko Milekic 	struct uma_kctor_args args;
1862099a0e58SBosko Milekic 
1863099a0e58SBosko Milekic 	args.size = size;
1864099a0e58SBosko Milekic 	args.uminit = uminit;
1865099a0e58SBosko Milekic 	args.fini = fini;
18661e319f6dSRobert Watson 	args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1867099a0e58SBosko Milekic 	args.flags = flags;
1868099a0e58SBosko Milekic 	args.zone = zone;
1869e20a199fSJeff Roberson 	return (zone_alloc_item(kegs, &args, M_WAITOK));
1870099a0e58SBosko Milekic }
1871099a0e58SBosko Milekic 
18728355f576SJeff Roberson /* See uma.h */
18731e319f6dSRobert Watson void
18741e319f6dSRobert Watson uma_set_align(int align)
18751e319f6dSRobert Watson {
18761e319f6dSRobert Watson 
18771e319f6dSRobert Watson 	if (align != UMA_ALIGN_CACHE)
18781e319f6dSRobert Watson 		uma_align_cache = align;
18791e319f6dSRobert Watson }
18801e319f6dSRobert Watson 
18811e319f6dSRobert Watson /* See uma.h */
18828355f576SJeff Roberson uma_zone_t
1883bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
188485dcf349SGleb Smirnoff 		uma_init uminit, uma_fini fini, int align, uint32_t flags)
18858355f576SJeff Roberson 
18868355f576SJeff Roberson {
18878355f576SJeff Roberson 	struct uma_zctor_args args;
188895c4bf75SKonstantin Belousov 	uma_zone_t res;
188995c4bf75SKonstantin Belousov 	bool locked;
18908355f576SJeff Roberson 
1891*a5a35578SJohn Baldwin 	KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
1892*a5a35578SJohn Baldwin 	    align, name));
1893*a5a35578SJohn Baldwin 
18948355f576SJeff Roberson 	/* This stuff is essential for the zone ctor */
18950095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
18968355f576SJeff Roberson 	args.name = name;
18978355f576SJeff Roberson 	args.size = size;
18988355f576SJeff Roberson 	args.ctor = ctor;
18998355f576SJeff Roberson 	args.dtor = dtor;
19008355f576SJeff Roberson 	args.uminit = uminit;
19018355f576SJeff Roberson 	args.fini = fini;
1902afc6dc36SJohn-Mark Gurney #ifdef  INVARIANTS
1903afc6dc36SJohn-Mark Gurney 	/*
1904afc6dc36SJohn-Mark Gurney 	 * If a zone is being created with an empty constructor and
1905afc6dc36SJohn-Mark Gurney 	 * destructor, pass UMA constructor/destructor which checks for
1906afc6dc36SJohn-Mark Gurney 	 * memory use after free.
1907afc6dc36SJohn-Mark Gurney 	 */
190819c591bfSMateusz Guzik 	if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOFREE))) &&
190919c591bfSMateusz Guzik 	    ctor == NULL && dtor == NULL && uminit == NULL && fini == NULL) {
1910afc6dc36SJohn-Mark Gurney 		args.ctor = trash_ctor;
1911afc6dc36SJohn-Mark Gurney 		args.dtor = trash_dtor;
1912afc6dc36SJohn-Mark Gurney 		args.uminit = trash_init;
1913afc6dc36SJohn-Mark Gurney 		args.fini = trash_fini;
1914afc6dc36SJohn-Mark Gurney 	}
1915afc6dc36SJohn-Mark Gurney #endif
19168355f576SJeff Roberson 	args.align = align;
19178355f576SJeff Roberson 	args.flags = flags;
1918099a0e58SBosko Milekic 	args.keg = NULL;
1919099a0e58SBosko Milekic 
192095c4bf75SKonstantin Belousov 	if (booted < UMA_STARTUP2) {
192195c4bf75SKonstantin Belousov 		locked = false;
192295c4bf75SKonstantin Belousov 	} else {
192395c4bf75SKonstantin Belousov 		sx_slock(&uma_drain_lock);
192495c4bf75SKonstantin Belousov 		locked = true;
192595c4bf75SKonstantin Belousov 	}
192695c4bf75SKonstantin Belousov 	res = zone_alloc_item(zones, &args, M_WAITOK);
192795c4bf75SKonstantin Belousov 	if (locked)
192895c4bf75SKonstantin Belousov 		sx_sunlock(&uma_drain_lock);
192995c4bf75SKonstantin Belousov 	return (res);
1930099a0e58SBosko Milekic }
1931099a0e58SBosko Milekic 
1932099a0e58SBosko Milekic /* See uma.h */
1933099a0e58SBosko Milekic uma_zone_t
1934099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1935099a0e58SBosko Milekic 		    uma_init zinit, uma_fini zfini, uma_zone_t master)
1936099a0e58SBosko Milekic {
1937099a0e58SBosko Milekic 	struct uma_zctor_args args;
1938e20a199fSJeff Roberson 	uma_keg_t keg;
193995c4bf75SKonstantin Belousov 	uma_zone_t res;
194095c4bf75SKonstantin Belousov 	bool locked;
1941099a0e58SBosko Milekic 
1942e20a199fSJeff Roberson 	keg = zone_first_keg(master);
19430095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
1944099a0e58SBosko Milekic 	args.name = name;
1945e20a199fSJeff Roberson 	args.size = keg->uk_size;
1946099a0e58SBosko Milekic 	args.ctor = ctor;
1947099a0e58SBosko Milekic 	args.dtor = dtor;
1948099a0e58SBosko Milekic 	args.uminit = zinit;
1949099a0e58SBosko Milekic 	args.fini = zfini;
1950e20a199fSJeff Roberson 	args.align = keg->uk_align;
1951e20a199fSJeff Roberson 	args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
1952e20a199fSJeff Roberson 	args.keg = keg;
19538355f576SJeff Roberson 
195495c4bf75SKonstantin Belousov 	if (booted < UMA_STARTUP2) {
195595c4bf75SKonstantin Belousov 		locked = false;
195695c4bf75SKonstantin Belousov 	} else {
195795c4bf75SKonstantin Belousov 		sx_slock(&uma_drain_lock);
195895c4bf75SKonstantin Belousov 		locked = true;
195995c4bf75SKonstantin Belousov 	}
1960e20a199fSJeff Roberson 	/* XXX Attaches only one keg of potentially many. */
196195c4bf75SKonstantin Belousov 	res = zone_alloc_item(zones, &args, M_WAITOK);
196295c4bf75SKonstantin Belousov 	if (locked)
196395c4bf75SKonstantin Belousov 		sx_sunlock(&uma_drain_lock);
196495c4bf75SKonstantin Belousov 	return (res);
19658355f576SJeff Roberson }
19668355f576SJeff Roberson 
19670095a784SJeff Roberson /* See uma.h */
19680095a784SJeff Roberson uma_zone_t
1969af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
1970af526374SJeff Roberson 		    uma_init zinit, uma_fini zfini, uma_import zimport,
1971af526374SJeff Roberson 		    uma_release zrelease, void *arg, int flags)
19720095a784SJeff Roberson {
19730095a784SJeff Roberson 	struct uma_zctor_args args;
19740095a784SJeff Roberson 
19750095a784SJeff Roberson 	memset(&args, 0, sizeof(args));
19760095a784SJeff Roberson 	args.name = name;
1977af526374SJeff Roberson 	args.size = size;
19780095a784SJeff Roberson 	args.ctor = ctor;
19790095a784SJeff Roberson 	args.dtor = dtor;
19800095a784SJeff Roberson 	args.uminit = zinit;
19810095a784SJeff Roberson 	args.fini = zfini;
19820095a784SJeff Roberson 	args.import = zimport;
19830095a784SJeff Roberson 	args.release = zrelease;
19840095a784SJeff Roberson 	args.arg = arg;
19850095a784SJeff Roberson 	args.align = 0;
19860095a784SJeff Roberson 	args.flags = flags;
19870095a784SJeff Roberson 
19880095a784SJeff Roberson 	return (zone_alloc_item(zones, &args, M_WAITOK));
19890095a784SJeff Roberson }
19900095a784SJeff Roberson 
1991e20a199fSJeff Roberson static void
1992e20a199fSJeff Roberson zone_lock_pair(uma_zone_t a, uma_zone_t b)
1993e20a199fSJeff Roberson {
1994e20a199fSJeff Roberson 	if (a < b) {
1995e20a199fSJeff Roberson 		ZONE_LOCK(a);
1996af526374SJeff Roberson 		mtx_lock_flags(b->uz_lockptr, MTX_DUPOK);
1997e20a199fSJeff Roberson 	} else {
1998e20a199fSJeff Roberson 		ZONE_LOCK(b);
1999af526374SJeff Roberson 		mtx_lock_flags(a->uz_lockptr, MTX_DUPOK);
2000e20a199fSJeff Roberson 	}
2001e20a199fSJeff Roberson }
2002e20a199fSJeff Roberson 
2003e20a199fSJeff Roberson static void
2004e20a199fSJeff Roberson zone_unlock_pair(uma_zone_t a, uma_zone_t b)
2005e20a199fSJeff Roberson {
2006e20a199fSJeff Roberson 
2007e20a199fSJeff Roberson 	ZONE_UNLOCK(a);
2008e20a199fSJeff Roberson 	ZONE_UNLOCK(b);
2009e20a199fSJeff Roberson }
2010e20a199fSJeff Roberson 
2011e20a199fSJeff Roberson int
2012e20a199fSJeff Roberson uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
2013e20a199fSJeff Roberson {
2014e20a199fSJeff Roberson 	uma_klink_t klink;
2015e20a199fSJeff Roberson 	uma_klink_t kl;
2016e20a199fSJeff Roberson 	int error;
2017e20a199fSJeff Roberson 
2018e20a199fSJeff Roberson 	error = 0;
2019e20a199fSJeff Roberson 	klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
2020e20a199fSJeff Roberson 
2021e20a199fSJeff Roberson 	zone_lock_pair(zone, master);
2022e20a199fSJeff Roberson 	/*
2023e20a199fSJeff Roberson 	 * zone must use vtoslab() to resolve objects and must already be
2024e20a199fSJeff Roberson 	 * a secondary.
2025e20a199fSJeff Roberson 	 */
2026e20a199fSJeff Roberson 	if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
2027e20a199fSJeff Roberson 	    != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
2028e20a199fSJeff Roberson 		error = EINVAL;
2029e20a199fSJeff Roberson 		goto out;
2030e20a199fSJeff Roberson 	}
2031e20a199fSJeff Roberson 	/*
2032e20a199fSJeff Roberson 	 * The new master must also use vtoslab().
2033e20a199fSJeff Roberson 	 */
2034e20a199fSJeff Roberson 	if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
2035e20a199fSJeff Roberson 		error = EINVAL;
2036e20a199fSJeff Roberson 		goto out;
2037e20a199fSJeff Roberson 	}
2038cfcae3f8SGleb Smirnoff 
2039e20a199fSJeff Roberson 	/*
2040e20a199fSJeff Roberson 	 * The underlying object must be the same size.  rsize
2041e20a199fSJeff Roberson 	 * may be different.
2042e20a199fSJeff Roberson 	 */
2043e20a199fSJeff Roberson 	if (master->uz_size != zone->uz_size) {
2044e20a199fSJeff Roberson 		error = E2BIG;
2045e20a199fSJeff Roberson 		goto out;
2046e20a199fSJeff Roberson 	}
2047e20a199fSJeff Roberson 	/*
2048e20a199fSJeff Roberson 	 * Put it at the end of the list.
2049e20a199fSJeff Roberson 	 */
2050e20a199fSJeff Roberson 	klink->kl_keg = zone_first_keg(master);
2051e20a199fSJeff Roberson 	LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
2052e20a199fSJeff Roberson 		if (LIST_NEXT(kl, kl_link) == NULL) {
2053e20a199fSJeff Roberson 			LIST_INSERT_AFTER(kl, klink, kl_link);
2054e20a199fSJeff Roberson 			break;
2055e20a199fSJeff Roberson 		}
2056e20a199fSJeff Roberson 	}
2057e20a199fSJeff Roberson 	klink = NULL;
2058e20a199fSJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_MULTI;
2059e20a199fSJeff Roberson 	zone->uz_slab = zone_fetch_slab_multi;
2060e20a199fSJeff Roberson 
2061e20a199fSJeff Roberson out:
2062e20a199fSJeff Roberson 	zone_unlock_pair(zone, master);
2063e20a199fSJeff Roberson 	if (klink != NULL)
2064e20a199fSJeff Roberson 		free(klink, M_TEMP);
2065e20a199fSJeff Roberson 
2066e20a199fSJeff Roberson 	return (error);
2067e20a199fSJeff Roberson }
2068e20a199fSJeff Roberson 
2069e20a199fSJeff Roberson 
20708355f576SJeff Roberson /* See uma.h */
20719c2cd7e5SJeff Roberson void
20729c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone)
20739c2cd7e5SJeff Roberson {
2074f4ff923bSRobert Watson 
207595c4bf75SKonstantin Belousov 	sx_slock(&uma_drain_lock);
20760095a784SJeff Roberson 	zone_free_item(zones, zone, NULL, SKIP_NONE);
207795c4bf75SKonstantin Belousov 	sx_sunlock(&uma_drain_lock);
20789c2cd7e5SJeff Roberson }
20799c2cd7e5SJeff Roberson 
20809c2cd7e5SJeff Roberson /* See uma.h */
20818355f576SJeff Roberson void *
20822cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
20838355f576SJeff Roberson {
20848355f576SJeff Roberson 	void *item;
20858355f576SJeff Roberson 	uma_cache_t cache;
20868355f576SJeff Roberson 	uma_bucket_t bucket;
2087fc03d22bSJeff Roberson 	int lockfail;
20888355f576SJeff Roberson 	int cpu;
20898355f576SJeff Roberson 
2090e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2091e866d8f0SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), 1, RANDOM_UMA);
209210cb2424SMark Murray 
20938355f576SJeff Roberson 	/* This is the fast path allocation */
20948355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1
20958355f576SJeff Roberson 	printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
20968355f576SJeff Roberson #endif
20973659f747SRobert Watson 	CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
20983659f747SRobert Watson 	    zone->uz_name, flags);
2099a553d4b8SJeff Roberson 
2100635fd505SRobert Watson 	if (flags & M_WAITOK) {
2101b23f72e9SBrian Feldman 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2102635fd505SRobert Watson 		    "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
21034c1cc01cSJohn Baldwin 	}
2104d9e2e68dSMark Johnston 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
21051067a2baSJonathan T. Looney 	    ("uma_zalloc_arg: called with spinlock or critical section held"));
21061067a2baSJonathan T. Looney 
21078d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
21088d689e04SGleb Smirnoff 	if (memguard_cmp_zone(zone)) {
21098d689e04SGleb Smirnoff 		item = memguard_alloc(zone->uz_size, flags);
21108d689e04SGleb Smirnoff 		if (item != NULL) {
21118d689e04SGleb Smirnoff 			if (zone->uz_init != NULL &&
21128d689e04SGleb Smirnoff 			    zone->uz_init(item, zone->uz_size, flags) != 0)
21138d689e04SGleb Smirnoff 				return (NULL);
21148d689e04SGleb Smirnoff 			if (zone->uz_ctor != NULL &&
2115fc03d22bSJeff Roberson 			    zone->uz_ctor(item, zone->uz_size, udata,
2116fc03d22bSJeff Roberson 			    flags) != 0) {
21178d689e04SGleb Smirnoff 			    	zone->uz_fini(item, zone->uz_size);
21188d689e04SGleb Smirnoff 				return (NULL);
21198d689e04SGleb Smirnoff 			}
21208d689e04SGleb Smirnoff 			return (item);
21218d689e04SGleb Smirnoff 		}
21228d689e04SGleb Smirnoff 		/* This is unfortunate but should not be fatal. */
21238d689e04SGleb Smirnoff 	}
21248d689e04SGleb Smirnoff #endif
21255d1ae027SRobert Watson 	/*
21265d1ae027SRobert Watson 	 * If possible, allocate from the per-CPU cache.  There are two
21275d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
21285d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
21295d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
21305d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
21315d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
21325d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to allocate from
21335d1ae027SRobert Watson 	 * the current cache; when we re-acquire the critical section, we
21345d1ae027SRobert Watson 	 * must detect and handle migration if it has occurred.
21355d1ae027SRobert Watson 	 */
21365d1ae027SRobert Watson 	critical_enter();
21375d1ae027SRobert Watson 	cpu = curcpu;
21388355f576SJeff Roberson 	cache = &zone->uz_cpu[cpu];
21398355f576SJeff Roberson 
21408355f576SJeff Roberson zalloc_start:
21418355f576SJeff Roberson 	bucket = cache->uc_allocbucket;
2142fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt > 0) {
2143cae33c14SJeff Roberson 		bucket->ub_cnt--;
2144cae33c14SJeff Roberson 		item = bucket->ub_bucket[bucket->ub_cnt];
21458355f576SJeff Roberson #ifdef INVARIANTS
2146cae33c14SJeff Roberson 		bucket->ub_bucket[bucket->ub_cnt] = NULL;
21478355f576SJeff Roberson #endif
2148fc03d22bSJeff Roberson 		KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
21498355f576SJeff Roberson 		cache->uc_allocs++;
21505d1ae027SRobert Watson 		critical_exit();
2151fc03d22bSJeff Roberson 		if (zone->uz_ctor != NULL &&
2152fc03d22bSJeff Roberson 		    zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
21530095a784SJeff Roberson 			atomic_add_long(&zone->uz_fails, 1);
2154fc03d22bSJeff Roberson 			zone_free_item(zone, item, udata, SKIP_DTOR);
2155b23f72e9SBrian Feldman 			return (NULL);
2156b23f72e9SBrian Feldman 		}
2157ef72505eSJeff Roberson #ifdef INVARIANTS
2158ef72505eSJeff Roberson 		uma_dbg_alloc(zone, NULL, item);
2159ef72505eSJeff Roberson #endif
21602cc35ff9SJeff Roberson 		if (flags & M_ZERO)
216148343a2fSGleb Smirnoff 			uma_zero_item(item, zone);
21628355f576SJeff Roberson 		return (item);
2163fc03d22bSJeff Roberson 	}
2164fc03d22bSJeff Roberson 
21658355f576SJeff Roberson 	/*
21668355f576SJeff Roberson 	 * We have run out of items in our alloc bucket.
21678355f576SJeff Roberson 	 * See if we can switch with our free bucket.
21688355f576SJeff Roberson 	 */
2169b983089aSJeff Roberson 	bucket = cache->uc_freebucket;
2170fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt > 0) {
2171fc03d22bSJeff Roberson #ifdef UMA_DEBUG_ALLOC
2172fc03d22bSJeff Roberson 		printf("uma_zalloc: Swapping empty with alloc.\n");
2173fc03d22bSJeff Roberson #endif
21748355f576SJeff Roberson 		cache->uc_freebucket = cache->uc_allocbucket;
2175b983089aSJeff Roberson 		cache->uc_allocbucket = bucket;
21768355f576SJeff Roberson 		goto zalloc_start;
21778355f576SJeff Roberson 	}
2178fc03d22bSJeff Roberson 
2179fc03d22bSJeff Roberson 	/*
2180fc03d22bSJeff Roberson 	 * Discard any empty allocation bucket while we hold no locks.
2181fc03d22bSJeff Roberson 	 */
2182fc03d22bSJeff Roberson 	bucket = cache->uc_allocbucket;
2183fc03d22bSJeff Roberson 	cache->uc_allocbucket = NULL;
2184fc03d22bSJeff Roberson 	critical_exit();
2185fc03d22bSJeff Roberson 	if (bucket != NULL)
21866fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
2187fc03d22bSJeff Roberson 
2188fc03d22bSJeff Roberson 	/* Short-circuit for zones without buckets and low memory. */
2189fc03d22bSJeff Roberson 	if (zone->uz_count == 0 || bucketdisable)
2190fc03d22bSJeff Roberson 		goto zalloc_item;
2191fc03d22bSJeff Roberson 
21925d1ae027SRobert Watson 	/*
21935d1ae027SRobert Watson 	 * Attempt to retrieve the item from the per-CPU cache has failed, so
21945d1ae027SRobert Watson 	 * we must go back to the zone.  This requires the zone lock, so we
21955d1ae027SRobert Watson 	 * must drop the critical section, then re-acquire it when we go back
21965d1ae027SRobert Watson 	 * to the cache.  Since the critical section is released, we may be
21975d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
21985d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
21995d1ae027SRobert Watson 	 * the critical section.
22005d1ae027SRobert Watson 	 */
2201fc03d22bSJeff Roberson 	lockfail = 0;
2202fc03d22bSJeff Roberson 	if (ZONE_TRYLOCK(zone) == 0) {
2203fc03d22bSJeff Roberson 		/* Record contention to size the buckets. */
2204a553d4b8SJeff Roberson 		ZONE_LOCK(zone);
2205fc03d22bSJeff Roberson 		lockfail = 1;
2206fc03d22bSJeff Roberson 	}
22075d1ae027SRobert Watson 	critical_enter();
22085d1ae027SRobert Watson 	cpu = curcpu;
22095d1ae027SRobert Watson 	cache = &zone->uz_cpu[cpu];
22105d1ae027SRobert Watson 
2211fc03d22bSJeff Roberson 	/*
2212fc03d22bSJeff Roberson 	 * Since we have locked the zone we may as well send back our stats.
2213fc03d22bSJeff Roberson 	 */
22140095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
22150095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, cache->uc_frees);
2216a553d4b8SJeff Roberson 	cache->uc_allocs = 0;
2217773df9abSRobert Watson 	cache->uc_frees = 0;
22188355f576SJeff Roberson 
2219fc03d22bSJeff Roberson 	/* See if we lost the race to fill the cache. */
2220fc03d22bSJeff Roberson 	if (cache->uc_allocbucket != NULL) {
2221fc03d22bSJeff Roberson 		ZONE_UNLOCK(zone);
2222fc03d22bSJeff Roberson 		goto zalloc_start;
2223a553d4b8SJeff Roberson 	}
22248355f576SJeff Roberson 
2225fc03d22bSJeff Roberson 	/*
2226fc03d22bSJeff Roberson 	 * Check the zone's cache of buckets.
2227fc03d22bSJeff Roberson 	 */
2228fc03d22bSJeff Roberson 	if ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
2229cae33c14SJeff Roberson 		KASSERT(bucket->ub_cnt != 0,
2230a553d4b8SJeff Roberson 		    ("uma_zalloc_arg: Returning an empty bucket."));
22318355f576SJeff Roberson 
2232a553d4b8SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
2233a553d4b8SJeff Roberson 		cache->uc_allocbucket = bucket;
2234a553d4b8SJeff Roberson 		ZONE_UNLOCK(zone);
22358355f576SJeff Roberson 		goto zalloc_start;
2236a553d4b8SJeff Roberson 	}
22375d1ae027SRobert Watson 	/* We are no longer associated with this CPU. */
22385d1ae027SRobert Watson 	critical_exit();
2239bbee39c6SJeff Roberson 
2240fc03d22bSJeff Roberson 	/*
2241fc03d22bSJeff Roberson 	 * We bump the uz count when the cache size is insufficient to
2242fc03d22bSJeff Roberson 	 * handle the working set.
2243fc03d22bSJeff Roberson 	 */
22446fd34d6fSJeff Roberson 	if (lockfail && zone->uz_count < BUCKET_MAX)
2245a553d4b8SJeff Roberson 		zone->uz_count++;
2246fc03d22bSJeff Roberson 	ZONE_UNLOCK(zone);
2247099a0e58SBosko Milekic 
22488355f576SJeff Roberson 	/*
2249a553d4b8SJeff Roberson 	 * Now lets just fill a bucket and put it on the free list.  If that
2250763df3ecSPedro F. Giffuni 	 * works we'll restart the allocation from the beginning and it
2251fc03d22bSJeff Roberson 	 * will use the just filled bucket.
2252bbee39c6SJeff Roberson 	 */
22536fd34d6fSJeff Roberson 	bucket = zone_alloc_bucket(zone, udata, flags);
2254fc03d22bSJeff Roberson 	if (bucket != NULL) {
2255fc03d22bSJeff Roberson 		ZONE_LOCK(zone);
2256fc03d22bSJeff Roberson 		critical_enter();
2257fc03d22bSJeff Roberson 		cpu = curcpu;
2258fc03d22bSJeff Roberson 		cache = &zone->uz_cpu[cpu];
2259fc03d22bSJeff Roberson 		/*
2260fc03d22bSJeff Roberson 		 * See if we lost the race or were migrated.  Cache the
2261fc03d22bSJeff Roberson 		 * initialized bucket to make this less likely or claim
2262fc03d22bSJeff Roberson 		 * the memory directly.
2263fc03d22bSJeff Roberson 		 */
2264fc03d22bSJeff Roberson 		if (cache->uc_allocbucket == NULL)
2265fc03d22bSJeff Roberson 			cache->uc_allocbucket = bucket;
2266fc03d22bSJeff Roberson 		else
2267fc03d22bSJeff Roberson 			LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
2268bbee39c6SJeff Roberson 		ZONE_UNLOCK(zone);
2269fc03d22bSJeff Roberson 		goto zalloc_start;
2270bbee39c6SJeff Roberson 	}
2271fc03d22bSJeff Roberson 
2272bbee39c6SJeff Roberson 	/*
2273bbee39c6SJeff Roberson 	 * We may not be able to get a bucket so return an actual item.
2274bbee39c6SJeff Roberson 	 */
2275bbee39c6SJeff Roberson #ifdef UMA_DEBUG
2276bbee39c6SJeff Roberson 	printf("uma_zalloc_arg: Bucketzone returned NULL\n");
2277bbee39c6SJeff Roberson #endif
2278bbee39c6SJeff Roberson 
2279fc03d22bSJeff Roberson zalloc_item:
2280e20a199fSJeff Roberson 	item = zone_alloc_item(zone, udata, flags);
2281fc03d22bSJeff Roberson 
2282e20a199fSJeff Roberson 	return (item);
2283bbee39c6SJeff Roberson }
2284bbee39c6SJeff Roberson 
2285bbee39c6SJeff Roberson static uma_slab_t
2286e20a199fSJeff Roberson keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags)
2287bbee39c6SJeff Roberson {
2288bbee39c6SJeff Roberson 	uma_slab_t slab;
22896fd34d6fSJeff Roberson 	int reserve;
2290099a0e58SBosko Milekic 
2291e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2292bbee39c6SJeff Roberson 	slab = NULL;
22936fd34d6fSJeff Roberson 	reserve = 0;
22946fd34d6fSJeff Roberson 	if ((flags & M_USE_RESERVE) == 0)
22956fd34d6fSJeff Roberson 		reserve = keg->uk_reserve;
2296bbee39c6SJeff Roberson 
2297bbee39c6SJeff Roberson 	for (;;) {
2298bbee39c6SJeff Roberson 		/*
2299bbee39c6SJeff Roberson 		 * Find a slab with some space.  Prefer slabs that are partially
2300bbee39c6SJeff Roberson 		 * used over those that are totally full.  This helps to reduce
2301bbee39c6SJeff Roberson 		 * fragmentation.
2302bbee39c6SJeff Roberson 		 */
23036fd34d6fSJeff Roberson 		if (keg->uk_free > reserve) {
2304099a0e58SBosko Milekic 			if (!LIST_EMPTY(&keg->uk_part_slab)) {
2305099a0e58SBosko Milekic 				slab = LIST_FIRST(&keg->uk_part_slab);
2306bbee39c6SJeff Roberson 			} else {
2307099a0e58SBosko Milekic 				slab = LIST_FIRST(&keg->uk_free_slab);
2308bbee39c6SJeff Roberson 				LIST_REMOVE(slab, us_link);
2309099a0e58SBosko Milekic 				LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2310bbee39c6SJeff Roberson 				    us_link);
2311bbee39c6SJeff Roberson 			}
2312e20a199fSJeff Roberson 			MPASS(slab->us_keg == keg);
2313bbee39c6SJeff Roberson 			return (slab);
2314bbee39c6SJeff Roberson 		}
2315bbee39c6SJeff Roberson 
2316bbee39c6SJeff Roberson 		/*
2317bbee39c6SJeff Roberson 		 * M_NOVM means don't ask at all!
2318bbee39c6SJeff Roberson 		 */
2319bbee39c6SJeff Roberson 		if (flags & M_NOVM)
2320bbee39c6SJeff Roberson 			break;
2321bbee39c6SJeff Roberson 
2322e20a199fSJeff Roberson 		if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2323099a0e58SBosko Milekic 			keg->uk_flags |= UMA_ZFLAG_FULL;
2324e20a199fSJeff Roberson 			/*
2325e20a199fSJeff Roberson 			 * If this is not a multi-zone, set the FULL bit.
2326e20a199fSJeff Roberson 			 * Otherwise slab_multi() takes care of it.
2327e20a199fSJeff Roberson 			 */
23282f891cd5SPawel Jakub Dawidek 			if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) {
2329e20a199fSJeff Roberson 				zone->uz_flags |= UMA_ZFLAG_FULL;
23302f891cd5SPawel Jakub Dawidek 				zone_log_warning(zone);
233154503a13SJonathan T. Looney 				zone_maxaction(zone);
23322f891cd5SPawel Jakub Dawidek 			}
2333ebc85edfSJeff Roberson 			if (flags & M_NOWAIT)
2334bbee39c6SJeff Roberson 				break;
2335c288b548SEitan Adler 			zone->uz_sleeps++;
2336e20a199fSJeff Roberson 			msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2337bbee39c6SJeff Roberson 			continue;
2338bbee39c6SJeff Roberson 		}
2339e20a199fSJeff Roberson 		slab = keg_alloc_slab(keg, zone, flags);
2340bbee39c6SJeff Roberson 		/*
2341bbee39c6SJeff Roberson 		 * If we got a slab here it's safe to mark it partially used
2342bbee39c6SJeff Roberson 		 * and return.  We assume that the caller is going to remove
2343bbee39c6SJeff Roberson 		 * at least one item.
2344bbee39c6SJeff Roberson 		 */
2345bbee39c6SJeff Roberson 		if (slab) {
2346e20a199fSJeff Roberson 			MPASS(slab->us_keg == keg);
2347099a0e58SBosko Milekic 			LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2348bbee39c6SJeff Roberson 			return (slab);
2349bbee39c6SJeff Roberson 		}
2350bbee39c6SJeff Roberson 		/*
2351bbee39c6SJeff Roberson 		 * We might not have been able to get a slab but another cpu
2352bbee39c6SJeff Roberson 		 * could have while we were unlocked.  Check again before we
2353bbee39c6SJeff Roberson 		 * fail.
2354bbee39c6SJeff Roberson 		 */
2355bbee39c6SJeff Roberson 		flags |= M_NOVM;
2356bbee39c6SJeff Roberson 	}
2357bbee39c6SJeff Roberson 	return (slab);
2358bbee39c6SJeff Roberson }
2359bbee39c6SJeff Roberson 
2360e20a199fSJeff Roberson static uma_slab_t
2361e20a199fSJeff Roberson zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags)
2362e20a199fSJeff Roberson {
2363e20a199fSJeff Roberson 	uma_slab_t slab;
2364e20a199fSJeff Roberson 
2365af526374SJeff Roberson 	if (keg == NULL) {
2366e20a199fSJeff Roberson 		keg = zone_first_keg(zone);
2367af526374SJeff Roberson 		KEG_LOCK(keg);
2368af526374SJeff Roberson 	}
2369e20a199fSJeff Roberson 
2370e20a199fSJeff Roberson 	for (;;) {
2371e20a199fSJeff Roberson 		slab = keg_fetch_slab(keg, zone, flags);
2372e20a199fSJeff Roberson 		if (slab)
2373e20a199fSJeff Roberson 			return (slab);
2374e20a199fSJeff Roberson 		if (flags & (M_NOWAIT | M_NOVM))
2375e20a199fSJeff Roberson 			break;
2376e20a199fSJeff Roberson 	}
2377af526374SJeff Roberson 	KEG_UNLOCK(keg);
2378e20a199fSJeff Roberson 	return (NULL);
2379e20a199fSJeff Roberson }
2380e20a199fSJeff Roberson 
2381e20a199fSJeff Roberson /*
2382e20a199fSJeff Roberson  * uma_zone_fetch_slab_multi:  Fetches a slab from one available keg.  Returns
2383af526374SJeff Roberson  * with the keg locked.  On NULL no lock is held.
2384e20a199fSJeff Roberson  *
2385e20a199fSJeff Roberson  * The last pointer is used to seed the search.  It is not required.
2386e20a199fSJeff Roberson  */
2387e20a199fSJeff Roberson static uma_slab_t
2388e20a199fSJeff Roberson zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags)
2389e20a199fSJeff Roberson {
2390e20a199fSJeff Roberson 	uma_klink_t klink;
2391e20a199fSJeff Roberson 	uma_slab_t slab;
2392e20a199fSJeff Roberson 	uma_keg_t keg;
2393e20a199fSJeff Roberson 	int flags;
2394e20a199fSJeff Roberson 	int empty;
2395e20a199fSJeff Roberson 	int full;
2396e20a199fSJeff Roberson 
2397e20a199fSJeff Roberson 	/*
2398e20a199fSJeff Roberson 	 * Don't wait on the first pass.  This will skip limit tests
2399e20a199fSJeff Roberson 	 * as well.  We don't want to block if we can find a provider
2400e20a199fSJeff Roberson 	 * without blocking.
2401e20a199fSJeff Roberson 	 */
2402e20a199fSJeff Roberson 	flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2403e20a199fSJeff Roberson 	/*
2404e20a199fSJeff Roberson 	 * Use the last slab allocated as a hint for where to start
2405e20a199fSJeff Roberson 	 * the search.
2406e20a199fSJeff Roberson 	 */
2407af526374SJeff Roberson 	if (last != NULL) {
2408e20a199fSJeff Roberson 		slab = keg_fetch_slab(last, zone, flags);
2409e20a199fSJeff Roberson 		if (slab)
2410e20a199fSJeff Roberson 			return (slab);
2411af526374SJeff Roberson 		KEG_UNLOCK(last);
2412e20a199fSJeff Roberson 	}
2413e20a199fSJeff Roberson 	/*
2414e20a199fSJeff Roberson 	 * Loop until we have a slab incase of transient failures
2415e20a199fSJeff Roberson 	 * while M_WAITOK is specified.  I'm not sure this is 100%
2416e20a199fSJeff Roberson 	 * required but we've done it for so long now.
2417e20a199fSJeff Roberson 	 */
2418e20a199fSJeff Roberson 	for (;;) {
2419e20a199fSJeff Roberson 		empty = 0;
2420e20a199fSJeff Roberson 		full = 0;
2421e20a199fSJeff Roberson 		/*
2422e20a199fSJeff Roberson 		 * Search the available kegs for slabs.  Be careful to hold the
2423e20a199fSJeff Roberson 		 * correct lock while calling into the keg layer.
2424e20a199fSJeff Roberson 		 */
2425e20a199fSJeff Roberson 		LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2426e20a199fSJeff Roberson 			keg = klink->kl_keg;
2427af526374SJeff Roberson 			KEG_LOCK(keg);
2428e20a199fSJeff Roberson 			if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2429e20a199fSJeff Roberson 				slab = keg_fetch_slab(keg, zone, flags);
2430e20a199fSJeff Roberson 				if (slab)
2431e20a199fSJeff Roberson 					return (slab);
2432e20a199fSJeff Roberson 			}
2433e20a199fSJeff Roberson 			if (keg->uk_flags & UMA_ZFLAG_FULL)
2434e20a199fSJeff Roberson 				full++;
2435e20a199fSJeff Roberson 			else
2436e20a199fSJeff Roberson 				empty++;
2437af526374SJeff Roberson 			KEG_UNLOCK(keg);
2438e20a199fSJeff Roberson 		}
2439e20a199fSJeff Roberson 		if (rflags & (M_NOWAIT | M_NOVM))
2440e20a199fSJeff Roberson 			break;
2441e20a199fSJeff Roberson 		flags = rflags;
2442e20a199fSJeff Roberson 		/*
2443e20a199fSJeff Roberson 		 * All kegs are full.  XXX We can't atomically check all kegs
2444e20a199fSJeff Roberson 		 * and sleep so just sleep for a short period and retry.
2445e20a199fSJeff Roberson 		 */
2446e20a199fSJeff Roberson 		if (full && !empty) {
2447af526374SJeff Roberson 			ZONE_LOCK(zone);
2448e20a199fSJeff Roberson 			zone->uz_flags |= UMA_ZFLAG_FULL;
2449bf965959SSean Bruno 			zone->uz_sleeps++;
24502f891cd5SPawel Jakub Dawidek 			zone_log_warning(zone);
245154503a13SJonathan T. Looney 			zone_maxaction(zone);
2452af526374SJeff Roberson 			msleep(zone, zone->uz_lockptr, PVM,
2453af526374SJeff Roberson 			    "zonelimit", hz/100);
2454e20a199fSJeff Roberson 			zone->uz_flags &= ~UMA_ZFLAG_FULL;
2455af526374SJeff Roberson 			ZONE_UNLOCK(zone);
2456e20a199fSJeff Roberson 			continue;
2457e20a199fSJeff Roberson 		}
2458e20a199fSJeff Roberson 	}
2459e20a199fSJeff Roberson 	return (NULL);
2460e20a199fSJeff Roberson }
2461e20a199fSJeff Roberson 
2462d56368d7SBosko Milekic static void *
24630095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
2464bbee39c6SJeff Roberson {
2465bbee39c6SJeff Roberson 	void *item;
246685dcf349SGleb Smirnoff 	uint8_t freei;
2467bbee39c6SJeff Roberson 
24680095a784SJeff Roberson 	MPASS(keg == slab->us_keg);
2469e20a199fSJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2470099a0e58SBosko Milekic 
2471ef72505eSJeff Roberson 	freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2472ef72505eSJeff Roberson 	BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2473099a0e58SBosko Milekic 	item = slab->us_data + (keg->uk_rsize * freei);
2474bbee39c6SJeff Roberson 	slab->us_freecount--;
2475099a0e58SBosko Milekic 	keg->uk_free--;
2476ef72505eSJeff Roberson 
2477bbee39c6SJeff Roberson 	/* Move this slab to the full list */
2478bbee39c6SJeff Roberson 	if (slab->us_freecount == 0) {
2479bbee39c6SJeff Roberson 		LIST_REMOVE(slab, us_link);
2480099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2481bbee39c6SJeff Roberson 	}
2482bbee39c6SJeff Roberson 
2483bbee39c6SJeff Roberson 	return (item);
2484bbee39c6SJeff Roberson }
2485bbee39c6SJeff Roberson 
2486bbee39c6SJeff Roberson static int
24870095a784SJeff Roberson zone_import(uma_zone_t zone, void **bucket, int max, int flags)
24880095a784SJeff Roberson {
24890095a784SJeff Roberson 	uma_slab_t slab;
24900095a784SJeff Roberson 	uma_keg_t keg;
24910095a784SJeff Roberson 	int i;
24920095a784SJeff Roberson 
24930095a784SJeff Roberson 	slab = NULL;
24940095a784SJeff Roberson 	keg = NULL;
2495af526374SJeff Roberson 	/* Try to keep the buckets totally full */
24960095a784SJeff Roberson 	for (i = 0; i < max; ) {
24970095a784SJeff Roberson 		if ((slab = zone->uz_slab(zone, keg, flags)) == NULL)
24980095a784SJeff Roberson 			break;
24990095a784SJeff Roberson 		keg = slab->us_keg;
25006fd34d6fSJeff Roberson 		while (slab->us_freecount && i < max) {
25010095a784SJeff Roberson 			bucket[i++] = slab_alloc_item(keg, slab);
25026fd34d6fSJeff Roberson 			if (keg->uk_free <= keg->uk_reserve)
25036fd34d6fSJeff Roberson 				break;
25046fd34d6fSJeff Roberson 		}
25056fd34d6fSJeff Roberson 		/* Don't grab more than one slab at a time. */
25060095a784SJeff Roberson 		flags &= ~M_WAITOK;
25070095a784SJeff Roberson 		flags |= M_NOWAIT;
25080095a784SJeff Roberson 	}
25090095a784SJeff Roberson 	if (slab != NULL)
25100095a784SJeff Roberson 		KEG_UNLOCK(keg);
25110095a784SJeff Roberson 
25120095a784SJeff Roberson 	return i;
25130095a784SJeff Roberson }
25140095a784SJeff Roberson 
2515fc03d22bSJeff Roberson static uma_bucket_t
25166fd34d6fSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int flags)
2517bbee39c6SJeff Roberson {
2518bbee39c6SJeff Roberson 	uma_bucket_t bucket;
25190095a784SJeff Roberson 	int max;
2520bbee39c6SJeff Roberson 
25216fd34d6fSJeff Roberson 	/* Don't wait for buckets, preserve caller's NOVM setting. */
25226fd34d6fSJeff Roberson 	bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
25230095a784SJeff Roberson 	if (bucket == NULL)
2524f7104ccdSAlexander Motin 		return (NULL);
25250095a784SJeff Roberson 
2526af526374SJeff Roberson 	max = MIN(bucket->ub_entries, zone->uz_count);
25270095a784SJeff Roberson 	bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
25280095a784SJeff Roberson 	    max, flags);
25290095a784SJeff Roberson 
25300095a784SJeff Roberson 	/*
25310095a784SJeff Roberson 	 * Initialize the memory if necessary.
25320095a784SJeff Roberson 	 */
25330095a784SJeff Roberson 	if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
2534099a0e58SBosko Milekic 		int i;
2535bbee39c6SJeff Roberson 
25360095a784SJeff Roberson 		for (i = 0; i < bucket->ub_cnt; i++)
2537e20a199fSJeff Roberson 			if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
25380095a784SJeff Roberson 			    flags) != 0)
2539b23f72e9SBrian Feldman 				break;
2540b23f72e9SBrian Feldman 		/*
2541b23f72e9SBrian Feldman 		 * If we couldn't initialize the whole bucket, put the
2542b23f72e9SBrian Feldman 		 * rest back onto the freelist.
2543b23f72e9SBrian Feldman 		 */
2544b23f72e9SBrian Feldman 		if (i != bucket->ub_cnt) {
2545af526374SJeff Roberson 			zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
25460095a784SJeff Roberson 			    bucket->ub_cnt - i);
2547a5a262c6SBosko Milekic #ifdef INVARIANTS
25480095a784SJeff Roberson 			bzero(&bucket->ub_bucket[i],
25490095a784SJeff Roberson 			    sizeof(void *) * (bucket->ub_cnt - i));
2550a5a262c6SBosko Milekic #endif
2551b23f72e9SBrian Feldman 			bucket->ub_cnt = i;
2552b23f72e9SBrian Feldman 		}
2553099a0e58SBosko Milekic 	}
2554099a0e58SBosko Milekic 
2555f7104ccdSAlexander Motin 	if (bucket->ub_cnt == 0) {
25566fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
2557fc03d22bSJeff Roberson 		atomic_add_long(&zone->uz_fails, 1);
2558fc03d22bSJeff Roberson 		return (NULL);
2559bbee39c6SJeff Roberson 	}
2560fc03d22bSJeff Roberson 
2561fc03d22bSJeff Roberson 	return (bucket);
2562fc03d22bSJeff Roberson }
2563fc03d22bSJeff Roberson 
25648355f576SJeff Roberson /*
25650095a784SJeff Roberson  * Allocates a single item from a zone.
25668355f576SJeff Roberson  *
25678355f576SJeff Roberson  * Arguments
25688355f576SJeff Roberson  *	zone   The zone to alloc for.
25698355f576SJeff Roberson  *	udata  The data to be passed to the constructor.
2570a163d034SWarner Losh  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
25718355f576SJeff Roberson  *
25728355f576SJeff Roberson  * Returns
25738355f576SJeff Roberson  *	NULL if there is no memory and M_NOWAIT is set
2574bbee39c6SJeff Roberson  *	An item if successful
25758355f576SJeff Roberson  */
25768355f576SJeff Roberson 
25778355f576SJeff Roberson static void *
2578e20a199fSJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int flags)
25798355f576SJeff Roberson {
25808355f576SJeff Roberson 	void *item;
25818355f576SJeff Roberson 
25828355f576SJeff Roberson 	item = NULL;
25838355f576SJeff Roberson 
25848355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
25858355f576SJeff Roberson 	printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
25868355f576SJeff Roberson #endif
25870095a784SJeff Roberson 	if (zone->uz_import(zone->uz_arg, &item, 1, flags) != 1)
25880095a784SJeff Roberson 		goto fail;
25890095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, 1);
25908355f576SJeff Roberson 
2591099a0e58SBosko Milekic 	/*
2592099a0e58SBosko Milekic 	 * We have to call both the zone's init (not the keg's init)
2593099a0e58SBosko Milekic 	 * and the zone's ctor.  This is because the item is going from
2594099a0e58SBosko Milekic 	 * a keg slab directly to the user, and the user is expecting it
2595099a0e58SBosko Milekic 	 * to be both zone-init'd as well as zone-ctor'd.
2596099a0e58SBosko Milekic 	 */
2597b23f72e9SBrian Feldman 	if (zone->uz_init != NULL) {
2598e20a199fSJeff Roberson 		if (zone->uz_init(item, zone->uz_size, flags) != 0) {
25990095a784SJeff Roberson 			zone_free_item(zone, item, udata, SKIP_FINI);
26000095a784SJeff Roberson 			goto fail;
2601b23f72e9SBrian Feldman 		}
2602b23f72e9SBrian Feldman 	}
2603b23f72e9SBrian Feldman 	if (zone->uz_ctor != NULL) {
2604e20a199fSJeff Roberson 		if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
26050095a784SJeff Roberson 			zone_free_item(zone, item, udata, SKIP_DTOR);
26060095a784SJeff Roberson 			goto fail;
2607b23f72e9SBrian Feldman 		}
2608b23f72e9SBrian Feldman 	}
2609ef72505eSJeff Roberson #ifdef INVARIANTS
26100095a784SJeff Roberson 	uma_dbg_alloc(zone, NULL, item);
2611ef72505eSJeff Roberson #endif
26122cc35ff9SJeff Roberson 	if (flags & M_ZERO)
261348343a2fSGleb Smirnoff 		uma_zero_item(item, zone);
26148355f576SJeff Roberson 
26158355f576SJeff Roberson 	return (item);
26160095a784SJeff Roberson 
26170095a784SJeff Roberson fail:
26180095a784SJeff Roberson 	atomic_add_long(&zone->uz_fails, 1);
26190095a784SJeff Roberson 	return (NULL);
26208355f576SJeff Roberson }
26218355f576SJeff Roberson 
26228355f576SJeff Roberson /* See uma.h */
26238355f576SJeff Roberson void
26248355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
26258355f576SJeff Roberson {
26268355f576SJeff Roberson 	uma_cache_t cache;
26278355f576SJeff Roberson 	uma_bucket_t bucket;
26284d104ba0SAlexander Motin 	int lockfail;
26298355f576SJeff Roberson 	int cpu;
26308355f576SJeff Roberson 
2631e866d8f0SMark Murray 	/* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2632e866d8f0SMark Murray 	random_harvest_fast_uma(&zone, sizeof(zone), 1, RANDOM_UMA);
263310cb2424SMark Murray 
26348355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1
26358355f576SJeff Roberson 	printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
26368355f576SJeff Roberson #endif
26373659f747SRobert Watson 	CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
26383659f747SRobert Watson 	    zone->uz_name);
26393659f747SRobert Watson 
2640d9e2e68dSMark Johnston 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
26411067a2baSJonathan T. Looney 	    ("uma_zfree_arg: called with spinlock or critical section held"));
26421067a2baSJonathan T. Looney 
264320ed0cb0SMatthew D Fleming         /* uma_zfree(..., NULL) does nothing, to match free(9). */
264420ed0cb0SMatthew D Fleming         if (item == NULL)
264520ed0cb0SMatthew D Fleming                 return;
26468d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD
26478d689e04SGleb Smirnoff 	if (is_memguard_addr(item)) {
2648bc9d08e1SMark Johnston 		if (zone->uz_dtor != NULL)
26498d689e04SGleb Smirnoff 			zone->uz_dtor(item, zone->uz_size, udata);
2650bc9d08e1SMark Johnston 		if (zone->uz_fini != NULL)
26518d689e04SGleb Smirnoff 			zone->uz_fini(item, zone->uz_size);
26528d689e04SGleb Smirnoff 		memguard_free(item);
26538d689e04SGleb Smirnoff 		return;
26548d689e04SGleb Smirnoff 	}
26558d689e04SGleb Smirnoff #endif
26565d1ae027SRobert Watson #ifdef INVARIANTS
2657e20a199fSJeff Roberson 	if (zone->uz_flags & UMA_ZONE_MALLOC)
26585d1ae027SRobert Watson 		uma_dbg_free(zone, udata, item);
26595d1ae027SRobert Watson 	else
26605d1ae027SRobert Watson 		uma_dbg_free(zone, NULL, item);
26615d1ae027SRobert Watson #endif
2662fc03d22bSJeff Roberson 	if (zone->uz_dtor != NULL)
2663ef72505eSJeff Roberson 		zone->uz_dtor(item, zone->uz_size, udata);
2664ef72505eSJeff Roberson 
2665af7f9b97SJeff Roberson 	/*
2666af7f9b97SJeff Roberson 	 * The race here is acceptable.  If we miss it we'll just have to wait
2667af7f9b97SJeff Roberson 	 * a little longer for the limits to be reset.
2668af7f9b97SJeff Roberson 	 */
2669e20a199fSJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_FULL)
2670fc03d22bSJeff Roberson 		goto zfree_item;
2671af7f9b97SJeff Roberson 
26725d1ae027SRobert Watson 	/*
26735d1ae027SRobert Watson 	 * If possible, free to the per-CPU cache.  There are two
26745d1ae027SRobert Watson 	 * requirements for safe access to the per-CPU cache: (1) the thread
26755d1ae027SRobert Watson 	 * accessing the cache must not be preempted or yield during access,
26765d1ae027SRobert Watson 	 * and (2) the thread must not migrate CPUs without switching which
26775d1ae027SRobert Watson 	 * cache it accesses.  We rely on a critical section to prevent
26785d1ae027SRobert Watson 	 * preemption and migration.  We release the critical section in
26795d1ae027SRobert Watson 	 * order to acquire the zone mutex if we are unable to free to the
26805d1ae027SRobert Watson 	 * current cache; when we re-acquire the critical section, we must
26815d1ae027SRobert Watson 	 * detect and handle migration if it has occurred.
26825d1ae027SRobert Watson 	 */
2683a553d4b8SJeff Roberson zfree_restart:
26845d1ae027SRobert Watson 	critical_enter();
26855d1ae027SRobert Watson 	cpu = curcpu;
26868355f576SJeff Roberson 	cache = &zone->uz_cpu[cpu];
26878355f576SJeff Roberson 
26888355f576SJeff Roberson zfree_start:
2689a553d4b8SJeff Roberson 	/*
2690fc03d22bSJeff Roberson 	 * Try to free into the allocbucket first to give LIFO ordering
2691fc03d22bSJeff Roberson 	 * for cache-hot datastructures.  Spill over into the freebucket
2692fc03d22bSJeff Roberson 	 * if necessary.  Alloc will swap them if one runs dry.
2693a553d4b8SJeff Roberson 	 */
2694fc03d22bSJeff Roberson 	bucket = cache->uc_allocbucket;
2695fc03d22bSJeff Roberson 	if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries)
2696fc03d22bSJeff Roberson 		bucket = cache->uc_freebucket;
2697fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2698cae33c14SJeff Roberson 		KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
26998355f576SJeff Roberson 		    ("uma_zfree: Freeing to non free bucket index."));
2700cae33c14SJeff Roberson 		bucket->ub_bucket[bucket->ub_cnt] = item;
2701cae33c14SJeff Roberson 		bucket->ub_cnt++;
2702773df9abSRobert Watson 		cache->uc_frees++;
27035d1ae027SRobert Watson 		critical_exit();
27048355f576SJeff Roberson 		return;
2705fc03d22bSJeff Roberson 	}
2706fc03d22bSJeff Roberson 
27078355f576SJeff Roberson 	/*
27085d1ae027SRobert Watson 	 * We must go back the zone, which requires acquiring the zone lock,
27095d1ae027SRobert Watson 	 * which in turn means we must release and re-acquire the critical
27105d1ae027SRobert Watson 	 * section.  Since the critical section is released, we may be
27115d1ae027SRobert Watson 	 * preempted or migrate.  As such, make sure not to maintain any
27125d1ae027SRobert Watson 	 * thread-local state specific to the cache from prior to releasing
27135d1ae027SRobert Watson 	 * the critical section.
27148355f576SJeff Roberson 	 */
27155d1ae027SRobert Watson 	critical_exit();
2716fc03d22bSJeff Roberson 	if (zone->uz_count == 0 || bucketdisable)
2717fc03d22bSJeff Roberson 		goto zfree_item;
2718fc03d22bSJeff Roberson 
27194d104ba0SAlexander Motin 	lockfail = 0;
27204d104ba0SAlexander Motin 	if (ZONE_TRYLOCK(zone) == 0) {
27214d104ba0SAlexander Motin 		/* Record contention to size the buckets. */
27228355f576SJeff Roberson 		ZONE_LOCK(zone);
27234d104ba0SAlexander Motin 		lockfail = 1;
27244d104ba0SAlexander Motin 	}
27255d1ae027SRobert Watson 	critical_enter();
27265d1ae027SRobert Watson 	cpu = curcpu;
27275d1ae027SRobert Watson 	cache = &zone->uz_cpu[cpu];
27288355f576SJeff Roberson 
2729fc03d22bSJeff Roberson 	/*
2730fc03d22bSJeff Roberson 	 * Since we have locked the zone we may as well send back our stats.
2731fc03d22bSJeff Roberson 	 */
27320095a784SJeff Roberson 	atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
27330095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, cache->uc_frees);
2734f4ff923bSRobert Watson 	cache->uc_allocs = 0;
2735f4ff923bSRobert Watson 	cache->uc_frees = 0;
2736f4ff923bSRobert Watson 
27378355f576SJeff Roberson 	bucket = cache->uc_freebucket;
2738fc03d22bSJeff Roberson 	if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2739fc03d22bSJeff Roberson 		ZONE_UNLOCK(zone);
2740fc03d22bSJeff Roberson 		goto zfree_start;
2741fc03d22bSJeff Roberson 	}
27428355f576SJeff Roberson 	cache->uc_freebucket = NULL;
2743afa5d703SMark Johnston 	/* We are no longer associated with this CPU. */
2744afa5d703SMark Johnston 	critical_exit();
27458355f576SJeff Roberson 
27468355f576SJeff Roberson 	/* Can we throw this on the zone full list? */
27478355f576SJeff Roberson 	if (bucket != NULL) {
27488355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
27498355f576SJeff Roberson 		printf("uma_zfree: Putting old bucket on the free list.\n");
27508355f576SJeff Roberson #endif
2751cae33c14SJeff Roberson 		/* ub_cnt is pointing to the last free item */
2752cae33c14SJeff Roberson 		KASSERT(bucket->ub_cnt != 0,
27538355f576SJeff Roberson 		    ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2754fc03d22bSJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
27558355f576SJeff Roberson 	}
2756fc03d22bSJeff Roberson 
27574d104ba0SAlexander Motin 	/*
27584d104ba0SAlexander Motin 	 * We bump the uz count when the cache size is insufficient to
27594d104ba0SAlexander Motin 	 * handle the working set.
27604d104ba0SAlexander Motin 	 */
27614d104ba0SAlexander Motin 	if (lockfail && zone->uz_count < BUCKET_MAX)
27624d104ba0SAlexander Motin 		zone->uz_count++;
2763a553d4b8SJeff Roberson 	ZONE_UNLOCK(zone);
2764a553d4b8SJeff Roberson 
27658355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
27668355f576SJeff Roberson 	printf("uma_zfree: Allocating new free bucket.\n");
27678355f576SJeff Roberson #endif
27686fd34d6fSJeff Roberson 	bucket = bucket_alloc(zone, udata, M_NOWAIT);
27694741dcbfSJeff Roberson 	if (bucket) {
2770fc03d22bSJeff Roberson 		critical_enter();
2771fc03d22bSJeff Roberson 		cpu = curcpu;
2772fc03d22bSJeff Roberson 		cache = &zone->uz_cpu[cpu];
2773fc03d22bSJeff Roberson 		if (cache->uc_freebucket == NULL) {
2774fc03d22bSJeff Roberson 			cache->uc_freebucket = bucket;
2775fc03d22bSJeff Roberson 			goto zfree_start;
2776fc03d22bSJeff Roberson 		}
2777fc03d22bSJeff Roberson 		/*
2778fc03d22bSJeff Roberson 		 * We lost the race, start over.  We have to drop our
2779fc03d22bSJeff Roberson 		 * critical section to free the bucket.
2780fc03d22bSJeff Roberson 		 */
2781fc03d22bSJeff Roberson 		critical_exit();
27826fd34d6fSJeff Roberson 		bucket_free(zone, bucket, udata);
2783a553d4b8SJeff Roberson 		goto zfree_restart;
27848355f576SJeff Roberson 	}
27858355f576SJeff Roberson 
2786a553d4b8SJeff Roberson 	/*
2787a553d4b8SJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
2788a553d4b8SJeff Roberson 	 */
2789fc03d22bSJeff Roberson zfree_item:
27900095a784SJeff Roberson 	zone_free_item(zone, item, udata, SKIP_DTOR);
27918355f576SJeff Roberson 
27928355f576SJeff Roberson 	return;
27938355f576SJeff Roberson }
27948355f576SJeff Roberson 
27958355f576SJeff Roberson static void
27960095a784SJeff Roberson slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item)
27978355f576SJeff Roberson {
279885dcf349SGleb Smirnoff 	uint8_t freei;
2799099a0e58SBosko Milekic 
28000095a784SJeff Roberson 	mtx_assert(&keg->uk_lock, MA_OWNED);
2801e20a199fSJeff Roberson 	MPASS(keg == slab->us_keg);
28028355f576SJeff Roberson 
28038355f576SJeff Roberson 	/* Do we need to remove from any lists? */
2804099a0e58SBosko Milekic 	if (slab->us_freecount+1 == keg->uk_ipers) {
28058355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
2806099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
28078355f576SJeff Roberson 	} else if (slab->us_freecount == 0) {
28088355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
2809099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
28108355f576SJeff Roberson 	}
28118355f576SJeff Roberson 
2812ef72505eSJeff Roberson 	/* Slab management. */
2813ef72505eSJeff Roberson 	freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
2814ef72505eSJeff Roberson 	BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
28158355f576SJeff Roberson 	slab->us_freecount++;
28168355f576SJeff Roberson 
2817ef72505eSJeff Roberson 	/* Keg statistics. */
2818099a0e58SBosko Milekic 	keg->uk_free++;
28190095a784SJeff Roberson }
28200095a784SJeff Roberson 
28210095a784SJeff Roberson static void
28220095a784SJeff Roberson zone_release(uma_zone_t zone, void **bucket, int cnt)
28230095a784SJeff Roberson {
28240095a784SJeff Roberson 	void *item;
28250095a784SJeff Roberson 	uma_slab_t slab;
28260095a784SJeff Roberson 	uma_keg_t keg;
28270095a784SJeff Roberson 	uint8_t *mem;
28280095a784SJeff Roberson 	int clearfull;
28290095a784SJeff Roberson 	int i;
28308355f576SJeff Roberson 
2831e20a199fSJeff Roberson 	clearfull = 0;
28320095a784SJeff Roberson 	keg = zone_first_keg(zone);
2833af526374SJeff Roberson 	KEG_LOCK(keg);
28340095a784SJeff Roberson 	for (i = 0; i < cnt; i++) {
28350095a784SJeff Roberson 		item = bucket[i];
28360095a784SJeff Roberson 		if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
28370095a784SJeff Roberson 			mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
28380095a784SJeff Roberson 			if (zone->uz_flags & UMA_ZONE_HASH) {
28390095a784SJeff Roberson 				slab = hash_sfind(&keg->uk_hash, mem);
28400095a784SJeff Roberson 			} else {
28410095a784SJeff Roberson 				mem += keg->uk_pgoff;
28420095a784SJeff Roberson 				slab = (uma_slab_t)mem;
28430095a784SJeff Roberson 			}
28440095a784SJeff Roberson 		} else {
28450095a784SJeff Roberson 			slab = vtoslab((vm_offset_t)item);
28460095a784SJeff Roberson 			if (slab->us_keg != keg) {
28470095a784SJeff Roberson 				KEG_UNLOCK(keg);
28480095a784SJeff Roberson 				keg = slab->us_keg;
28490095a784SJeff Roberson 				KEG_LOCK(keg);
28500095a784SJeff Roberson 			}
28510095a784SJeff Roberson 		}
28520095a784SJeff Roberson 		slab_free_item(keg, slab, item);
2853099a0e58SBosko Milekic 		if (keg->uk_flags & UMA_ZFLAG_FULL) {
2854e20a199fSJeff Roberson 			if (keg->uk_pages < keg->uk_maxpages) {
2855099a0e58SBosko Milekic 				keg->uk_flags &= ~UMA_ZFLAG_FULL;
2856e20a199fSJeff Roberson 				clearfull = 1;
2857e20a199fSJeff Roberson 			}
2858af7f9b97SJeff Roberson 
285977380291SMohan Srinivasan 			/*
2860ef72505eSJeff Roberson 			 * We can handle one more allocation. Since we're
2861ef72505eSJeff Roberson 			 * clearing ZFLAG_FULL, wake up all procs blocked
2862ef72505eSJeff Roberson 			 * on pages. This should be uncommon, so keeping this
2863ef72505eSJeff Roberson 			 * simple for now (rather than adding count of blocked
286477380291SMohan Srinivasan 			 * threads etc).
286577380291SMohan Srinivasan 			 */
286677380291SMohan Srinivasan 			wakeup(keg);
2867af7f9b97SJeff Roberson 		}
28680095a784SJeff Roberson 	}
2869af526374SJeff Roberson 	KEG_UNLOCK(keg);
28700095a784SJeff Roberson 	if (clearfull) {
2871af526374SJeff Roberson 		ZONE_LOCK(zone);
2872e20a199fSJeff Roberson 		zone->uz_flags &= ~UMA_ZFLAG_FULL;
2873e20a199fSJeff Roberson 		wakeup(zone);
2874605cbd6aSJeff Roberson 		ZONE_UNLOCK(zone);
2875af526374SJeff Roberson 	}
2876ef72505eSJeff Roberson 
28778355f576SJeff Roberson }
28788355f576SJeff Roberson 
28790095a784SJeff Roberson /*
28800095a784SJeff Roberson  * Frees a single item to any zone.
28810095a784SJeff Roberson  *
28820095a784SJeff Roberson  * Arguments:
28830095a784SJeff Roberson  *	zone   The zone to free to
28840095a784SJeff Roberson  *	item   The item we're freeing
28850095a784SJeff Roberson  *	udata  User supplied data for the dtor
28860095a784SJeff Roberson  *	skip   Skip dtors and finis
28870095a784SJeff Roberson  */
28880095a784SJeff Roberson static void
28890095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
28900095a784SJeff Roberson {
28910095a784SJeff Roberson 
28920095a784SJeff Roberson #ifdef INVARIANTS
28930095a784SJeff Roberson 	if (skip == SKIP_NONE) {
28940095a784SJeff Roberson 		if (zone->uz_flags & UMA_ZONE_MALLOC)
28950095a784SJeff Roberson 			uma_dbg_free(zone, udata, item);
28960095a784SJeff Roberson 		else
28970095a784SJeff Roberson 			uma_dbg_free(zone, NULL, item);
28980095a784SJeff Roberson 	}
28990095a784SJeff Roberson #endif
29000095a784SJeff Roberson 	if (skip < SKIP_DTOR && zone->uz_dtor)
29010095a784SJeff Roberson 		zone->uz_dtor(item, zone->uz_size, udata);
29020095a784SJeff Roberson 
29030095a784SJeff Roberson 	if (skip < SKIP_FINI && zone->uz_fini)
29040095a784SJeff Roberson 		zone->uz_fini(item, zone->uz_size);
29050095a784SJeff Roberson 
29060095a784SJeff Roberson 	atomic_add_long(&zone->uz_frees, 1);
29070095a784SJeff Roberson 	zone->uz_release(zone->uz_arg, &item, 1);
29080095a784SJeff Roberson }
29090095a784SJeff Roberson 
29108355f576SJeff Roberson /* See uma.h */
29111c6cae97SLawrence Stewart int
2912736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems)
2913736ee590SJeff Roberson {
2914099a0e58SBosko Milekic 	uma_keg_t keg;
2915099a0e58SBosko Milekic 
2916e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
29170095a784SJeff Roberson 	if (keg == NULL)
29180095a784SJeff Roberson 		return (0);
2919af526374SJeff Roberson 	KEG_LOCK(keg);
2920e20a199fSJeff Roberson 	keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
2921099a0e58SBosko Milekic 	if (keg->uk_maxpages * keg->uk_ipers < nitems)
2922e20a199fSJeff Roberson 		keg->uk_maxpages += keg->uk_ppera;
292357223e99SAndriy Gapon 	nitems = (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers;
2924af526374SJeff Roberson 	KEG_UNLOCK(keg);
29251c6cae97SLawrence Stewart 
29261c6cae97SLawrence Stewart 	return (nitems);
2927736ee590SJeff Roberson }
2928736ee590SJeff Roberson 
2929736ee590SJeff Roberson /* See uma.h */
2930e49471b0SAndre Oppermann int
2931e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone)
2932e49471b0SAndre Oppermann {
2933e49471b0SAndre Oppermann 	int nitems;
2934e49471b0SAndre Oppermann 	uma_keg_t keg;
2935e49471b0SAndre Oppermann 
2936e49471b0SAndre Oppermann 	keg = zone_first_keg(zone);
29370095a784SJeff Roberson 	if (keg == NULL)
29380095a784SJeff Roberson 		return (0);
2939af526374SJeff Roberson 	KEG_LOCK(keg);
294057223e99SAndriy Gapon 	nitems = (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers;
2941af526374SJeff Roberson 	KEG_UNLOCK(keg);
2942e49471b0SAndre Oppermann 
2943e49471b0SAndre Oppermann 	return (nitems);
2944e49471b0SAndre Oppermann }
2945e49471b0SAndre Oppermann 
2946e49471b0SAndre Oppermann /* See uma.h */
29472f891cd5SPawel Jakub Dawidek void
29482f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning)
29492f891cd5SPawel Jakub Dawidek {
29502f891cd5SPawel Jakub Dawidek 
29512f891cd5SPawel Jakub Dawidek 	ZONE_LOCK(zone);
29522f891cd5SPawel Jakub Dawidek 	zone->uz_warning = warning;
29532f891cd5SPawel Jakub Dawidek 	ZONE_UNLOCK(zone);
29542f891cd5SPawel Jakub Dawidek }
29552f891cd5SPawel Jakub Dawidek 
29562f891cd5SPawel Jakub Dawidek /* See uma.h */
295754503a13SJonathan T. Looney void
295854503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
295954503a13SJonathan T. Looney {
296054503a13SJonathan T. Looney 
296154503a13SJonathan T. Looney 	ZONE_LOCK(zone);
2962e60b2fcbSGleb Smirnoff 	TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
296354503a13SJonathan T. Looney 	ZONE_UNLOCK(zone);
296454503a13SJonathan T. Looney }
296554503a13SJonathan T. Looney 
296654503a13SJonathan T. Looney /* See uma.h */
2967c4ae7908SLawrence Stewart int
2968c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone)
2969c4ae7908SLawrence Stewart {
2970c4ae7908SLawrence Stewart 	int64_t nitems;
2971c4ae7908SLawrence Stewart 	u_int i;
2972c4ae7908SLawrence Stewart 
2973c4ae7908SLawrence Stewart 	ZONE_LOCK(zone);
2974c4ae7908SLawrence Stewart 	nitems = zone->uz_allocs - zone->uz_frees;
2975c4ae7908SLawrence Stewart 	CPU_FOREACH(i) {
2976c4ae7908SLawrence Stewart 		/*
2977c4ae7908SLawrence Stewart 		 * See the comment in sysctl_vm_zone_stats() regarding the
2978c4ae7908SLawrence Stewart 		 * safety of accessing the per-cpu caches. With the zone lock
2979c4ae7908SLawrence Stewart 		 * held, it is safe, but can potentially result in stale data.
2980c4ae7908SLawrence Stewart 		 */
2981c4ae7908SLawrence Stewart 		nitems += zone->uz_cpu[i].uc_allocs -
2982c4ae7908SLawrence Stewart 		    zone->uz_cpu[i].uc_frees;
2983c4ae7908SLawrence Stewart 	}
2984c4ae7908SLawrence Stewart 	ZONE_UNLOCK(zone);
2985c4ae7908SLawrence Stewart 
2986c4ae7908SLawrence Stewart 	return (nitems < 0 ? 0 : nitems);
2987c4ae7908SLawrence Stewart }
2988c4ae7908SLawrence Stewart 
2989c4ae7908SLawrence Stewart /* See uma.h */
2990736ee590SJeff Roberson void
2991099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2992099a0e58SBosko Milekic {
2993e20a199fSJeff Roberson 	uma_keg_t keg;
2994e20a199fSJeff Roberson 
2995e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
29960095a784SJeff Roberson 	KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
2997af526374SJeff Roberson 	KEG_LOCK(keg);
2998e20a199fSJeff Roberson 	KASSERT(keg->uk_pages == 0,
2999099a0e58SBosko Milekic 	    ("uma_zone_set_init on non-empty keg"));
3000e20a199fSJeff Roberson 	keg->uk_init = uminit;
3001af526374SJeff Roberson 	KEG_UNLOCK(keg);
3002099a0e58SBosko Milekic }
3003099a0e58SBosko Milekic 
3004099a0e58SBosko Milekic /* See uma.h */
3005099a0e58SBosko Milekic void
3006099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
3007099a0e58SBosko Milekic {
3008e20a199fSJeff Roberson 	uma_keg_t keg;
3009e20a199fSJeff Roberson 
3010e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
30111d2c0c46SDmitry Chagin 	KASSERT(keg != NULL, ("uma_zone_set_fini: Invalid zone type"));
3012af526374SJeff Roberson 	KEG_LOCK(keg);
3013e20a199fSJeff Roberson 	KASSERT(keg->uk_pages == 0,
3014099a0e58SBosko Milekic 	    ("uma_zone_set_fini on non-empty keg"));
3015e20a199fSJeff Roberson 	keg->uk_fini = fini;
3016af526374SJeff Roberson 	KEG_UNLOCK(keg);
3017099a0e58SBosko Milekic }
3018099a0e58SBosko Milekic 
3019099a0e58SBosko Milekic /* See uma.h */
3020099a0e58SBosko Milekic void
3021099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
3022099a0e58SBosko Milekic {
3023af526374SJeff Roberson 
3024099a0e58SBosko Milekic 	ZONE_LOCK(zone);
3025e20a199fSJeff Roberson 	KASSERT(zone_first_keg(zone)->uk_pages == 0,
3026099a0e58SBosko Milekic 	    ("uma_zone_set_zinit on non-empty keg"));
3027099a0e58SBosko Milekic 	zone->uz_init = zinit;
3028099a0e58SBosko Milekic 	ZONE_UNLOCK(zone);
3029099a0e58SBosko Milekic }
3030099a0e58SBosko Milekic 
3031099a0e58SBosko Milekic /* See uma.h */
3032099a0e58SBosko Milekic void
3033099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
3034099a0e58SBosko Milekic {
3035af526374SJeff Roberson 
3036099a0e58SBosko Milekic 	ZONE_LOCK(zone);
3037e20a199fSJeff Roberson 	KASSERT(zone_first_keg(zone)->uk_pages == 0,
3038099a0e58SBosko Milekic 	    ("uma_zone_set_zfini on non-empty keg"));
3039099a0e58SBosko Milekic 	zone->uz_fini = zfini;
3040099a0e58SBosko Milekic 	ZONE_UNLOCK(zone);
3041099a0e58SBosko Milekic }
3042099a0e58SBosko Milekic 
3043099a0e58SBosko Milekic /* See uma.h */
3044b23f72e9SBrian Feldman /* XXX uk_freef is not actually used with the zone locked */
3045099a0e58SBosko Milekic void
30468355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef)
30478355f576SJeff Roberson {
30480095a784SJeff Roberson 	uma_keg_t keg;
3049e20a199fSJeff Roberson 
30500095a784SJeff Roberson 	keg = zone_first_keg(zone);
30511d2c0c46SDmitry Chagin 	KASSERT(keg != NULL, ("uma_zone_set_freef: Invalid zone type"));
3052af526374SJeff Roberson 	KEG_LOCK(keg);
30530095a784SJeff Roberson 	keg->uk_freef = freef;
3054af526374SJeff Roberson 	KEG_UNLOCK(keg);
30558355f576SJeff Roberson }
30568355f576SJeff Roberson 
30578355f576SJeff Roberson /* See uma.h */
3058b23f72e9SBrian Feldman /* XXX uk_allocf is not actually used with the zone locked */
30598355f576SJeff Roberson void
30608355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
30618355f576SJeff Roberson {
3062e20a199fSJeff Roberson 	uma_keg_t keg;
3063e20a199fSJeff Roberson 
3064e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
3065af526374SJeff Roberson 	KEG_LOCK(keg);
3066e20a199fSJeff Roberson 	keg->uk_allocf = allocf;
3067af526374SJeff Roberson 	KEG_UNLOCK(keg);
30688355f576SJeff Roberson }
30698355f576SJeff Roberson 
30708355f576SJeff Roberson /* See uma.h */
30716fd34d6fSJeff Roberson void
30726fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items)
30736fd34d6fSJeff Roberson {
30746fd34d6fSJeff Roberson 	uma_keg_t keg;
30756fd34d6fSJeff Roberson 
30766fd34d6fSJeff Roberson 	keg = zone_first_keg(zone);
30776fd34d6fSJeff Roberson 	if (keg == NULL)
30786fd34d6fSJeff Roberson 		return;
30796fd34d6fSJeff Roberson 	KEG_LOCK(keg);
30806fd34d6fSJeff Roberson 	keg->uk_reserve = items;
30816fd34d6fSJeff Roberson 	KEG_UNLOCK(keg);
30826fd34d6fSJeff Roberson 
30836fd34d6fSJeff Roberson 	return;
30846fd34d6fSJeff Roberson }
30856fd34d6fSJeff Roberson 
30866fd34d6fSJeff Roberson /* See uma.h */
30878355f576SJeff Roberson int
3088a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count)
30898355f576SJeff Roberson {
3090099a0e58SBosko Milekic 	uma_keg_t keg;
30918355f576SJeff Roberson 	vm_offset_t kva;
30929ba30bcbSZbigniew Bodek 	u_int pages;
30938355f576SJeff Roberson 
3094e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
30950095a784SJeff Roberson 	if (keg == NULL)
30960095a784SJeff Roberson 		return (0);
3097099a0e58SBosko Milekic 	pages = count / keg->uk_ipers;
30988355f576SJeff Roberson 
3099099a0e58SBosko Milekic 	if (pages * keg->uk_ipers < count)
31008355f576SJeff Roberson 		pages++;
310157223e99SAndriy Gapon 	pages *= keg->uk_ppera;
3102a553d4b8SJeff Roberson 
3103a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
3104a4915c21SAttilio Rao 	if (keg->uk_ppera > 1) {
3105a4915c21SAttilio Rao #else
3106a4915c21SAttilio Rao 	if (1) {
3107a4915c21SAttilio Rao #endif
310857223e99SAndriy Gapon 		kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
3109d1f42ac2SAlan Cox 		if (kva == 0)
31108355f576SJeff Roberson 			return (0);
3111a4915c21SAttilio Rao 	} else
3112a4915c21SAttilio Rao 		kva = 0;
3113af526374SJeff Roberson 	KEG_LOCK(keg);
3114099a0e58SBosko Milekic 	keg->uk_kva = kva;
3115a4915c21SAttilio Rao 	keg->uk_offset = 0;
3116099a0e58SBosko Milekic 	keg->uk_maxpages = pages;
3117a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC
3118a4915c21SAttilio Rao 	keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
3119a4915c21SAttilio Rao #else
3120a4915c21SAttilio Rao 	keg->uk_allocf = noobj_alloc;
3121a4915c21SAttilio Rao #endif
31226fd34d6fSJeff Roberson 	keg->uk_flags |= UMA_ZONE_NOFREE;
3123af526374SJeff Roberson 	KEG_UNLOCK(keg);
3124af526374SJeff Roberson 
31258355f576SJeff Roberson 	return (1);
31268355f576SJeff Roberson }
31278355f576SJeff Roberson 
31288355f576SJeff Roberson /* See uma.h */
31298355f576SJeff Roberson void
31308355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items)
31318355f576SJeff Roberson {
31328355f576SJeff Roberson 	int slabs;
31338355f576SJeff Roberson 	uma_slab_t slab;
3134099a0e58SBosko Milekic 	uma_keg_t keg;
31358355f576SJeff Roberson 
3136e20a199fSJeff Roberson 	keg = zone_first_keg(zone);
31370095a784SJeff Roberson 	if (keg == NULL)
31380095a784SJeff Roberson 		return;
3139af526374SJeff Roberson 	KEG_LOCK(keg);
3140099a0e58SBosko Milekic 	slabs = items / keg->uk_ipers;
3141099a0e58SBosko Milekic 	if (slabs * keg->uk_ipers < items)
31428355f576SJeff Roberson 		slabs++;
31438355f576SJeff Roberson 	while (slabs > 0) {
3144e20a199fSJeff Roberson 		slab = keg_alloc_slab(keg, zone, M_WAITOK);
3145e20a199fSJeff Roberson 		if (slab == NULL)
3146e20a199fSJeff Roberson 			break;
3147e20a199fSJeff Roberson 		MPASS(slab->us_keg == keg);
3148099a0e58SBosko Milekic 		LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
31498355f576SJeff Roberson 		slabs--;
31508355f576SJeff Roberson 	}
3151af526374SJeff Roberson 	KEG_UNLOCK(keg);
31528355f576SJeff Roberson }
31538355f576SJeff Roberson 
31548355f576SJeff Roberson /* See uma.h */
315544ec2b63SKonstantin Belousov static void
315644ec2b63SKonstantin Belousov uma_reclaim_locked(bool kmem_danger)
31578355f576SJeff Roberson {
315844ec2b63SKonstantin Belousov 
31598355f576SJeff Roberson #ifdef UMA_DEBUG
31608355f576SJeff Roberson 	printf("UMA: vm asked us to release pages!\n");
31618355f576SJeff Roberson #endif
316244ec2b63SKonstantin Belousov 	sx_assert(&uma_drain_lock, SA_XLOCKED);
316386bbae32SJeff Roberson 	bucket_enable();
31648355f576SJeff Roberson 	zone_foreach(zone_drain);
316544ec2b63SKonstantin Belousov 	if (vm_page_count_min() || kmem_danger) {
3166a2de44abSAlexander Motin 		cache_drain_safe(NULL);
3167a2de44abSAlexander Motin 		zone_foreach(zone_drain);
3168a2de44abSAlexander Motin 	}
31698355f576SJeff Roberson 	/*
31708355f576SJeff Roberson 	 * Some slabs may have been freed but this zone will be visited early
31718355f576SJeff Roberson 	 * we visit again so that we can free pages that are empty once other
31728355f576SJeff Roberson 	 * zones are drained.  We have to do the same for buckets.
31738355f576SJeff Roberson 	 */
31749643769aSJeff Roberson 	zone_drain(slabzone);
3175cae33c14SJeff Roberson 	bucket_zone_drain();
317644ec2b63SKonstantin Belousov }
317744ec2b63SKonstantin Belousov 
317844ec2b63SKonstantin Belousov void
317944ec2b63SKonstantin Belousov uma_reclaim(void)
318044ec2b63SKonstantin Belousov {
318144ec2b63SKonstantin Belousov 
318244ec2b63SKonstantin Belousov 	sx_xlock(&uma_drain_lock);
318344ec2b63SKonstantin Belousov 	uma_reclaim_locked(false);
318495c4bf75SKonstantin Belousov 	sx_xunlock(&uma_drain_lock);
31858355f576SJeff Roberson }
31868355f576SJeff Roberson 
318744ec2b63SKonstantin Belousov static int uma_reclaim_needed;
318844ec2b63SKonstantin Belousov 
318944ec2b63SKonstantin Belousov void
319044ec2b63SKonstantin Belousov uma_reclaim_wakeup(void)
319144ec2b63SKonstantin Belousov {
319244ec2b63SKonstantin Belousov 
319344ec2b63SKonstantin Belousov 	uma_reclaim_needed = 1;
319444ec2b63SKonstantin Belousov 	wakeup(&uma_reclaim_needed);
319544ec2b63SKonstantin Belousov }
319644ec2b63SKonstantin Belousov 
319744ec2b63SKonstantin Belousov void
319844ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused)
319944ec2b63SKonstantin Belousov {
320044ec2b63SKonstantin Belousov 
320144ec2b63SKonstantin Belousov 	sx_xlock(&uma_drain_lock);
320244ec2b63SKonstantin Belousov 	for (;;) {
320344ec2b63SKonstantin Belousov 		sx_sleep(&uma_reclaim_needed, &uma_drain_lock, PVM,
320444ec2b63SKonstantin Belousov 		    "umarcl", 0);
320544ec2b63SKonstantin Belousov 		if (uma_reclaim_needed) {
320644ec2b63SKonstantin Belousov 			uma_reclaim_needed = 0;
32079b43bc27SAndriy Gapon 			sx_xunlock(&uma_drain_lock);
32089b43bc27SAndriy Gapon 			EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
32099b43bc27SAndriy Gapon 			sx_xlock(&uma_drain_lock);
321044ec2b63SKonstantin Belousov 			uma_reclaim_locked(true);
321144ec2b63SKonstantin Belousov 		}
321244ec2b63SKonstantin Belousov 	}
321344ec2b63SKonstantin Belousov }
321444ec2b63SKonstantin Belousov 
3215663b416fSJohn Baldwin /* See uma.h */
3216663b416fSJohn Baldwin int
3217663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone)
3218663b416fSJohn Baldwin {
3219663b416fSJohn Baldwin 	int full;
3220663b416fSJohn Baldwin 
3221663b416fSJohn Baldwin 	ZONE_LOCK(zone);
3222e20a199fSJeff Roberson 	full = (zone->uz_flags & UMA_ZFLAG_FULL);
3223663b416fSJohn Baldwin 	ZONE_UNLOCK(zone);
3224663b416fSJohn Baldwin 	return (full);
3225663b416fSJohn Baldwin }
3226663b416fSJohn Baldwin 
32276c125b8dSMohan Srinivasan int
32286c125b8dSMohan Srinivasan uma_zone_exhausted_nolock(uma_zone_t zone)
32296c125b8dSMohan Srinivasan {
3230e20a199fSJeff Roberson 	return (zone->uz_flags & UMA_ZFLAG_FULL);
32316c125b8dSMohan Srinivasan }
32326c125b8dSMohan Srinivasan 
32338355f576SJeff Roberson void *
3234f2c2231eSRyan Stone uma_large_malloc(vm_size_t size, int wait)
32358355f576SJeff Roberson {
32368355f576SJeff Roberson 	void *mem;
32378355f576SJeff Roberson 	uma_slab_t slab;
323885dcf349SGleb Smirnoff 	uint8_t flags;
32398355f576SJeff Roberson 
3240e20a199fSJeff Roberson 	slab = zone_alloc_item(slabzone, NULL, wait);
32418355f576SJeff Roberson 	if (slab == NULL)
32428355f576SJeff Roberson 		return (NULL);
32438355f576SJeff Roberson 	mem = page_alloc(NULL, size, &flags, wait);
32448355f576SJeff Roberson 	if (mem) {
324599571dc3SJeff Roberson 		vsetslab((vm_offset_t)mem, slab);
32468355f576SJeff Roberson 		slab->us_data = mem;
32478355f576SJeff Roberson 		slab->us_flags = flags | UMA_SLAB_MALLOC;
32488355f576SJeff Roberson 		slab->us_size = size;
32498355f576SJeff Roberson 	} else {
32500095a784SJeff Roberson 		zone_free_item(slabzone, slab, NULL, SKIP_NONE);
32518355f576SJeff Roberson 	}
32528355f576SJeff Roberson 
32538355f576SJeff Roberson 	return (mem);
32548355f576SJeff Roberson }
32558355f576SJeff Roberson 
32568355f576SJeff Roberson void
32578355f576SJeff Roberson uma_large_free(uma_slab_t slab)
32588355f576SJeff Roberson {
3259c325e866SKonstantin Belousov 
32608355f576SJeff Roberson 	page_free(slab->us_data, slab->us_size, slab->us_flags);
32610095a784SJeff Roberson 	zone_free_item(slabzone, slab, NULL, SKIP_NONE);
32628355f576SJeff Roberson }
32638355f576SJeff Roberson 
326448343a2fSGleb Smirnoff static void
326548343a2fSGleb Smirnoff uma_zero_item(void *item, uma_zone_t zone)
326648343a2fSGleb Smirnoff {
326796c85efbSNathan Whitehorn 	int i;
326848343a2fSGleb Smirnoff 
326948343a2fSGleb Smirnoff 	if (zone->uz_flags & UMA_ZONE_PCPU) {
327096c85efbSNathan Whitehorn 		CPU_FOREACH(i)
327148343a2fSGleb Smirnoff 			bzero(zpcpu_get_cpu(item, i), zone->uz_size);
327248343a2fSGleb Smirnoff 	} else
327348343a2fSGleb Smirnoff 		bzero(item, zone->uz_size);
327448343a2fSGleb Smirnoff }
327548343a2fSGleb Smirnoff 
32768355f576SJeff Roberson void
32778355f576SJeff Roberson uma_print_stats(void)
32788355f576SJeff Roberson {
32798355f576SJeff Roberson 	zone_foreach(uma_print_zone);
32808355f576SJeff Roberson }
32818355f576SJeff Roberson 
3282504d5de3SJeff Roberson static void
3283504d5de3SJeff Roberson slab_print(uma_slab_t slab)
3284504d5de3SJeff Roberson {
3285ef72505eSJeff Roberson 	printf("slab: keg %p, data %p, freecount %d\n",
3286ef72505eSJeff Roberson 		slab->us_keg, slab->us_data, slab->us_freecount);
3287504d5de3SJeff Roberson }
3288504d5de3SJeff Roberson 
3289504d5de3SJeff Roberson static void
3290504d5de3SJeff Roberson cache_print(uma_cache_t cache)
3291504d5de3SJeff Roberson {
3292504d5de3SJeff Roberson 	printf("alloc: %p(%d), free: %p(%d)\n",
3293504d5de3SJeff Roberson 		cache->uc_allocbucket,
3294504d5de3SJeff Roberson 		cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3295504d5de3SJeff Roberson 		cache->uc_freebucket,
3296504d5de3SJeff Roberson 		cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3297504d5de3SJeff Roberson }
3298504d5de3SJeff Roberson 
3299e20a199fSJeff Roberson static void
3300e20a199fSJeff Roberson uma_print_keg(uma_keg_t keg)
33018355f576SJeff Roberson {
3302504d5de3SJeff Roberson 	uma_slab_t slab;
3303504d5de3SJeff Roberson 
33040b80c1e4SEitan Adler 	printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3305e20a199fSJeff Roberson 	    "out %d free %d limit %d\n",
3306e20a199fSJeff Roberson 	    keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3307099a0e58SBosko Milekic 	    keg->uk_ipers, keg->uk_ppera,
330857223e99SAndriy Gapon 	    (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
330957223e99SAndriy Gapon 	    keg->uk_free, (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3310504d5de3SJeff Roberson 	printf("Part slabs:\n");
3311099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
3312504d5de3SJeff Roberson 		slab_print(slab);
3313504d5de3SJeff Roberson 	printf("Free slabs:\n");
3314099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
3315504d5de3SJeff Roberson 		slab_print(slab);
3316504d5de3SJeff Roberson 	printf("Full slabs:\n");
3317099a0e58SBosko Milekic 	LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
3318504d5de3SJeff Roberson 		slab_print(slab);
3319e20a199fSJeff Roberson }
3320e20a199fSJeff Roberson 
3321e20a199fSJeff Roberson void
3322e20a199fSJeff Roberson uma_print_zone(uma_zone_t zone)
3323e20a199fSJeff Roberson {
3324e20a199fSJeff Roberson 	uma_cache_t cache;
3325e20a199fSJeff Roberson 	uma_klink_t kl;
3326e20a199fSJeff Roberson 	int i;
3327e20a199fSJeff Roberson 
33280b80c1e4SEitan Adler 	printf("zone: %s(%p) size %d flags %#x\n",
3329e20a199fSJeff Roberson 	    zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3330e20a199fSJeff Roberson 	LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3331e20a199fSJeff Roberson 		uma_print_keg(kl->kl_keg);
33323aa6d94eSJohn Baldwin 	CPU_FOREACH(i) {
3333504d5de3SJeff Roberson 		cache = &zone->uz_cpu[i];
3334504d5de3SJeff Roberson 		printf("CPU %d Cache:\n", i);
3335504d5de3SJeff Roberson 		cache_print(cache);
3336504d5de3SJeff Roberson 	}
33378355f576SJeff Roberson }
33388355f576SJeff Roberson 
3339a0d4b0aeSRobert Watson #ifdef DDB
33408355f576SJeff Roberson /*
33417a52a97eSRobert Watson  * Generate statistics across both the zone and its per-cpu cache's.  Return
33427a52a97eSRobert Watson  * desired statistics if the pointer is non-NULL for that statistic.
33437a52a97eSRobert Watson  *
33447a52a97eSRobert Watson  * Note: does not update the zone statistics, as it can't safely clear the
33457a52a97eSRobert Watson  * per-CPU cache statistic.
33467a52a97eSRobert Watson  *
33477a52a97eSRobert Watson  * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
33487a52a97eSRobert Watson  * safe from off-CPU; we should modify the caches to track this information
33497a52a97eSRobert Watson  * directly so that we don't have to.
33507a52a97eSRobert Watson  */
33517a52a97eSRobert Watson static void
335285dcf349SGleb Smirnoff uma_zone_sumstat(uma_zone_t z, int *cachefreep, uint64_t *allocsp,
335385dcf349SGleb Smirnoff     uint64_t *freesp, uint64_t *sleepsp)
33547a52a97eSRobert Watson {
33557a52a97eSRobert Watson 	uma_cache_t cache;
335685dcf349SGleb Smirnoff 	uint64_t allocs, frees, sleeps;
33577a52a97eSRobert Watson 	int cachefree, cpu;
33587a52a97eSRobert Watson 
3359bf965959SSean Bruno 	allocs = frees = sleeps = 0;
33607a52a97eSRobert Watson 	cachefree = 0;
33613aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
33627a52a97eSRobert Watson 		cache = &z->uz_cpu[cpu];
33637a52a97eSRobert Watson 		if (cache->uc_allocbucket != NULL)
33647a52a97eSRobert Watson 			cachefree += cache->uc_allocbucket->ub_cnt;
33657a52a97eSRobert Watson 		if (cache->uc_freebucket != NULL)
33667a52a97eSRobert Watson 			cachefree += cache->uc_freebucket->ub_cnt;
33677a52a97eSRobert Watson 		allocs += cache->uc_allocs;
33687a52a97eSRobert Watson 		frees += cache->uc_frees;
33697a52a97eSRobert Watson 	}
33707a52a97eSRobert Watson 	allocs += z->uz_allocs;
33717a52a97eSRobert Watson 	frees += z->uz_frees;
3372bf965959SSean Bruno 	sleeps += z->uz_sleeps;
33737a52a97eSRobert Watson 	if (cachefreep != NULL)
33747a52a97eSRobert Watson 		*cachefreep = cachefree;
33757a52a97eSRobert Watson 	if (allocsp != NULL)
33767a52a97eSRobert Watson 		*allocsp = allocs;
33777a52a97eSRobert Watson 	if (freesp != NULL)
33787a52a97eSRobert Watson 		*freesp = frees;
3379bf965959SSean Bruno 	if (sleepsp != NULL)
3380bf965959SSean Bruno 		*sleepsp = sleeps;
33817a52a97eSRobert Watson }
3382a0d4b0aeSRobert Watson #endif /* DDB */
33837a52a97eSRobert Watson 
33847a52a97eSRobert Watson static int
33857a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
33867a52a97eSRobert Watson {
33877a52a97eSRobert Watson 	uma_keg_t kz;
33887a52a97eSRobert Watson 	uma_zone_t z;
33897a52a97eSRobert Watson 	int count;
33907a52a97eSRobert Watson 
33917a52a97eSRobert Watson 	count = 0;
3392111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
33937a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
33947a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
33957a52a97eSRobert Watson 			count++;
33967a52a97eSRobert Watson 	}
3397111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
33987a52a97eSRobert Watson 	return (sysctl_handle_int(oidp, &count, 0, req));
33997a52a97eSRobert Watson }
34007a52a97eSRobert Watson 
34017a52a97eSRobert Watson static int
34027a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
34037a52a97eSRobert Watson {
34047a52a97eSRobert Watson 	struct uma_stream_header ush;
34057a52a97eSRobert Watson 	struct uma_type_header uth;
34067a52a97eSRobert Watson 	struct uma_percpu_stat ups;
34077a52a97eSRobert Watson 	uma_bucket_t bucket;
34087a52a97eSRobert Watson 	struct sbuf sbuf;
34097a52a97eSRobert Watson 	uma_cache_t cache;
3410e20a199fSJeff Roberson 	uma_klink_t kl;
34117a52a97eSRobert Watson 	uma_keg_t kz;
34127a52a97eSRobert Watson 	uma_zone_t z;
3413e20a199fSJeff Roberson 	uma_keg_t k;
34144e657159SMatthew D Fleming 	int count, error, i;
34157a52a97eSRobert Watson 
341600f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
341700f0e671SMatthew D Fleming 	if (error != 0)
341800f0e671SMatthew D Fleming 		return (error);
34194e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
34201eafc078SIan Lepore 	sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
34214e657159SMatthew D Fleming 
3422404a593eSMatthew D Fleming 	count = 0;
3423111fbcd5SBryan Venteicher 	rw_rlock(&uma_rwlock);
34247a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
34257a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
34267a52a97eSRobert Watson 			count++;
34277a52a97eSRobert Watson 	}
34287a52a97eSRobert Watson 
34297a52a97eSRobert Watson 	/*
34307a52a97eSRobert Watson 	 * Insert stream header.
34317a52a97eSRobert Watson 	 */
34327a52a97eSRobert Watson 	bzero(&ush, sizeof(ush));
34337a52a97eSRobert Watson 	ush.ush_version = UMA_STREAM_VERSION;
3434ab3a57c0SRobert Watson 	ush.ush_maxcpus = (mp_maxid + 1);
34357a52a97eSRobert Watson 	ush.ush_count = count;
34364e657159SMatthew D Fleming 	(void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
34377a52a97eSRobert Watson 
34387a52a97eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
34397a52a97eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
34407a52a97eSRobert Watson 			bzero(&uth, sizeof(uth));
34417a52a97eSRobert Watson 			ZONE_LOCK(z);
3442cbbb4a00SRobert Watson 			strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
34437a52a97eSRobert Watson 			uth.uth_align = kz->uk_align;
34447a52a97eSRobert Watson 			uth.uth_size = kz->uk_size;
34457a52a97eSRobert Watson 			uth.uth_rsize = kz->uk_rsize;
3446e20a199fSJeff Roberson 			LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3447e20a199fSJeff Roberson 				k = kl->kl_keg;
3448e20a199fSJeff Roberson 				uth.uth_maxpages += k->uk_maxpages;
3449e20a199fSJeff Roberson 				uth.uth_pages += k->uk_pages;
3450e20a199fSJeff Roberson 				uth.uth_keg_free += k->uk_free;
3451e20a199fSJeff Roberson 				uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3452e20a199fSJeff Roberson 				    * k->uk_ipers;
3453e20a199fSJeff Roberson 			}
3454cbbb4a00SRobert Watson 
3455cbbb4a00SRobert Watson 			/*
3456cbbb4a00SRobert Watson 			 * A zone is secondary is it is not the first entry
3457cbbb4a00SRobert Watson 			 * on the keg's zone list.
3458cbbb4a00SRobert Watson 			 */
3459e20a199fSJeff Roberson 			if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3460cbbb4a00SRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z))
3461cbbb4a00SRobert Watson 				uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3462cbbb4a00SRobert Watson 
3463fc03d22bSJeff Roberson 			LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
34647a52a97eSRobert Watson 				uth.uth_zone_free += bucket->ub_cnt;
34657a52a97eSRobert Watson 			uth.uth_allocs = z->uz_allocs;
34667a52a97eSRobert Watson 			uth.uth_frees = z->uz_frees;
34672019094aSRobert Watson 			uth.uth_fails = z->uz_fails;
3468bf965959SSean Bruno 			uth.uth_sleeps = z->uz_sleeps;
34694e657159SMatthew D Fleming 			(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
34707a52a97eSRobert Watson 			/*
34712450bbb8SRobert Watson 			 * While it is not normally safe to access the cache
34722450bbb8SRobert Watson 			 * bucket pointers while not on the CPU that owns the
34732450bbb8SRobert Watson 			 * cache, we only allow the pointers to be exchanged
34742450bbb8SRobert Watson 			 * without the zone lock held, not invalidated, so
34752450bbb8SRobert Watson 			 * accept the possible race associated with bucket
34762450bbb8SRobert Watson 			 * exchange during monitoring.
34777a52a97eSRobert Watson 			 */
3478ab3a57c0SRobert Watson 			for (i = 0; i < (mp_maxid + 1); i++) {
34797a52a97eSRobert Watson 				bzero(&ups, sizeof(ups));
34807a52a97eSRobert Watson 				if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
34817a52a97eSRobert Watson 					goto skip;
3482082dc776SRobert Watson 				if (CPU_ABSENT(i))
3483082dc776SRobert Watson 					goto skip;
34847a52a97eSRobert Watson 				cache = &z->uz_cpu[i];
34857a52a97eSRobert Watson 				if (cache->uc_allocbucket != NULL)
34867a52a97eSRobert Watson 					ups.ups_cache_free +=
34877a52a97eSRobert Watson 					    cache->uc_allocbucket->ub_cnt;
34887a52a97eSRobert Watson 				if (cache->uc_freebucket != NULL)
34897a52a97eSRobert Watson 					ups.ups_cache_free +=
34907a52a97eSRobert Watson 					    cache->uc_freebucket->ub_cnt;
34917a52a97eSRobert Watson 				ups.ups_allocs = cache->uc_allocs;
34927a52a97eSRobert Watson 				ups.ups_frees = cache->uc_frees;
34937a52a97eSRobert Watson skip:
34944e657159SMatthew D Fleming 				(void)sbuf_bcat(&sbuf, &ups, sizeof(ups));
34957a52a97eSRobert Watson 			}
34962450bbb8SRobert Watson 			ZONE_UNLOCK(z);
34977a52a97eSRobert Watson 		}
34987a52a97eSRobert Watson 	}
3499111fbcd5SBryan Venteicher 	rw_runlock(&uma_rwlock);
35004e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
35014e657159SMatthew D Fleming 	sbuf_delete(&sbuf);
35027a52a97eSRobert Watson 	return (error);
35037a52a97eSRobert Watson }
350448c5777eSRobert Watson 
35050a5a3ccbSGleb Smirnoff int
35060a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
35070a5a3ccbSGleb Smirnoff {
35080a5a3ccbSGleb Smirnoff 	uma_zone_t zone = *(uma_zone_t *)arg1;
350916be9f54SGleb Smirnoff 	int error, max;
35100a5a3ccbSGleb Smirnoff 
351116be9f54SGleb Smirnoff 	max = uma_zone_get_max(zone);
35120a5a3ccbSGleb Smirnoff 	error = sysctl_handle_int(oidp, &max, 0, req);
35130a5a3ccbSGleb Smirnoff 	if (error || !req->newptr)
35140a5a3ccbSGleb Smirnoff 		return (error);
35150a5a3ccbSGleb Smirnoff 
35160a5a3ccbSGleb Smirnoff 	uma_zone_set_max(zone, max);
35170a5a3ccbSGleb Smirnoff 
35180a5a3ccbSGleb Smirnoff 	return (0);
35190a5a3ccbSGleb Smirnoff }
35200a5a3ccbSGleb Smirnoff 
35210a5a3ccbSGleb Smirnoff int
35220a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
35230a5a3ccbSGleb Smirnoff {
35240a5a3ccbSGleb Smirnoff 	uma_zone_t zone = *(uma_zone_t *)arg1;
35250a5a3ccbSGleb Smirnoff 	int cur;
35260a5a3ccbSGleb Smirnoff 
35270a5a3ccbSGleb Smirnoff 	cur = uma_zone_get_cur(zone);
35280a5a3ccbSGleb Smirnoff 	return (sysctl_handle_int(oidp, &cur, 0, req));
35290a5a3ccbSGleb Smirnoff }
35300a5a3ccbSGleb Smirnoff 
35319542ea7bSGleb Smirnoff #ifdef INVARIANTS
35329542ea7bSGleb Smirnoff static uma_slab_t
35339542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item)
35349542ea7bSGleb Smirnoff {
35359542ea7bSGleb Smirnoff 	uma_slab_t slab;
35369542ea7bSGleb Smirnoff 	uma_keg_t keg;
35379542ea7bSGleb Smirnoff 	uint8_t *mem;
35389542ea7bSGleb Smirnoff 
35399542ea7bSGleb Smirnoff 	mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
35409542ea7bSGleb Smirnoff 	if (zone->uz_flags & UMA_ZONE_VTOSLAB) {
35419542ea7bSGleb Smirnoff 		slab = vtoslab((vm_offset_t)mem);
35429542ea7bSGleb Smirnoff 	} else {
35439542ea7bSGleb Smirnoff 		/*
35449542ea7bSGleb Smirnoff 		 * It is safe to return the slab here even though the
35459542ea7bSGleb Smirnoff 		 * zone is unlocked because the item's allocation state
35469542ea7bSGleb Smirnoff 		 * essentially holds a reference.
35479542ea7bSGleb Smirnoff 		 */
35489542ea7bSGleb Smirnoff 		ZONE_LOCK(zone);
35499542ea7bSGleb Smirnoff 		keg = LIST_FIRST(&zone->uz_kegs)->kl_keg;
35509542ea7bSGleb Smirnoff 		if (keg->uk_flags & UMA_ZONE_HASH)
35519542ea7bSGleb Smirnoff 			slab = hash_sfind(&keg->uk_hash, mem);
35529542ea7bSGleb Smirnoff 		else
35539542ea7bSGleb Smirnoff 			slab = (uma_slab_t)(mem + keg->uk_pgoff);
35549542ea7bSGleb Smirnoff 		ZONE_UNLOCK(zone);
35559542ea7bSGleb Smirnoff 	}
35569542ea7bSGleb Smirnoff 
35579542ea7bSGleb Smirnoff 	return (slab);
35589542ea7bSGleb Smirnoff }
35599542ea7bSGleb Smirnoff 
35609542ea7bSGleb Smirnoff /*
35619542ea7bSGleb Smirnoff  * Set up the slab's freei data such that uma_dbg_free can function.
35629542ea7bSGleb Smirnoff  *
35639542ea7bSGleb Smirnoff  */
35649542ea7bSGleb Smirnoff static void
35659542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
35669542ea7bSGleb Smirnoff {
35679542ea7bSGleb Smirnoff 	uma_keg_t keg;
35689542ea7bSGleb Smirnoff 	int freei;
35699542ea7bSGleb Smirnoff 
35709542ea7bSGleb Smirnoff 	if (zone_first_keg(zone) == NULL)
35719542ea7bSGleb Smirnoff 		return;
35729542ea7bSGleb Smirnoff 	if (slab == NULL) {
35739542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
35749542ea7bSGleb Smirnoff 		if (slab == NULL)
35759542ea7bSGleb Smirnoff 			panic("uma: item %p did not belong to zone %s\n",
35769542ea7bSGleb Smirnoff 			    item, zone->uz_name);
35779542ea7bSGleb Smirnoff 	}
35789542ea7bSGleb Smirnoff 	keg = slab->us_keg;
35799542ea7bSGleb Smirnoff 	freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
35809542ea7bSGleb Smirnoff 
35819542ea7bSGleb Smirnoff 	if (BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
35829542ea7bSGleb Smirnoff 		panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
35839542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
35849542ea7bSGleb Smirnoff 	BIT_SET_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
35859542ea7bSGleb Smirnoff 
35869542ea7bSGleb Smirnoff 	return;
35879542ea7bSGleb Smirnoff }
35889542ea7bSGleb Smirnoff 
35899542ea7bSGleb Smirnoff /*
35909542ea7bSGleb Smirnoff  * Verifies freed addresses.  Checks for alignment, valid slab membership
35919542ea7bSGleb Smirnoff  * and duplicate frees.
35929542ea7bSGleb Smirnoff  *
35939542ea7bSGleb Smirnoff  */
35949542ea7bSGleb Smirnoff static void
35959542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
35969542ea7bSGleb Smirnoff {
35979542ea7bSGleb Smirnoff 	uma_keg_t keg;
35989542ea7bSGleb Smirnoff 	int freei;
35999542ea7bSGleb Smirnoff 
36009542ea7bSGleb Smirnoff 	if (zone_first_keg(zone) == NULL)
36019542ea7bSGleb Smirnoff 		return;
36029542ea7bSGleb Smirnoff 	if (slab == NULL) {
36039542ea7bSGleb Smirnoff 		slab = uma_dbg_getslab(zone, item);
36049542ea7bSGleb Smirnoff 		if (slab == NULL)
36059542ea7bSGleb Smirnoff 			panic("uma: Freed item %p did not belong to zone %s\n",
36069542ea7bSGleb Smirnoff 			    item, zone->uz_name);
36079542ea7bSGleb Smirnoff 	}
36089542ea7bSGleb Smirnoff 	keg = slab->us_keg;
36099542ea7bSGleb Smirnoff 	freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
36109542ea7bSGleb Smirnoff 
36119542ea7bSGleb Smirnoff 	if (freei >= keg->uk_ipers)
36129542ea7bSGleb Smirnoff 		panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
36139542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
36149542ea7bSGleb Smirnoff 
36159542ea7bSGleb Smirnoff 	if (((freei * keg->uk_rsize) + slab->us_data) != item)
36169542ea7bSGleb Smirnoff 		panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
36179542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
36189542ea7bSGleb Smirnoff 
36199542ea7bSGleb Smirnoff 	if (!BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
36209542ea7bSGleb Smirnoff 		panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
36219542ea7bSGleb Smirnoff 		    item, zone, zone->uz_name, slab, freei);
36229542ea7bSGleb Smirnoff 
36239542ea7bSGleb Smirnoff 	BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
36249542ea7bSGleb Smirnoff }
36259542ea7bSGleb Smirnoff #endif /* INVARIANTS */
36269542ea7bSGleb Smirnoff 
362748c5777eSRobert Watson #ifdef DDB
362848c5777eSRobert Watson DB_SHOW_COMMAND(uma, db_show_uma)
362948c5777eSRobert Watson {
363085dcf349SGleb Smirnoff 	uint64_t allocs, frees, sleeps;
363148c5777eSRobert Watson 	uma_bucket_t bucket;
363248c5777eSRobert Watson 	uma_keg_t kz;
363348c5777eSRobert Watson 	uma_zone_t z;
363448c5777eSRobert Watson 	int cachefree;
363548c5777eSRobert Watson 
363603175483SAlexander Motin 	db_printf("%18s %8s %8s %8s %12s %8s %8s\n", "Zone", "Size", "Used",
363703175483SAlexander Motin 	    "Free", "Requests", "Sleeps", "Bucket");
363848c5777eSRobert Watson 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
363948c5777eSRobert Watson 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
364048c5777eSRobert Watson 			if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
364148c5777eSRobert Watson 				allocs = z->uz_allocs;
364248c5777eSRobert Watson 				frees = z->uz_frees;
3643bf965959SSean Bruno 				sleeps = z->uz_sleeps;
364448c5777eSRobert Watson 				cachefree = 0;
364548c5777eSRobert Watson 			} else
364648c5777eSRobert Watson 				uma_zone_sumstat(z, &cachefree, &allocs,
3647bf965959SSean Bruno 				    &frees, &sleeps);
3648e20a199fSJeff Roberson 			if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
364948c5777eSRobert Watson 			    (LIST_FIRST(&kz->uk_zones) != z)))
365048c5777eSRobert Watson 				cachefree += kz->uk_free;
3651fc03d22bSJeff Roberson 			LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
365248c5777eSRobert Watson 				cachefree += bucket->ub_cnt;
365303175483SAlexander Motin 			db_printf("%18s %8ju %8jd %8d %12ju %8ju %8u\n",
365403175483SAlexander Motin 			    z->uz_name, (uintmax_t)kz->uk_size,
3655ae4e9636SRobert Watson 			    (intmax_t)(allocs - frees), cachefree,
365603175483SAlexander Motin 			    (uintmax_t)allocs, sleeps, z->uz_count);
3657687c94aaSJohn Baldwin 			if (db_pager_quit)
3658687c94aaSJohn Baldwin 				return;
365948c5777eSRobert Watson 		}
366048c5777eSRobert Watson 	}
366148c5777eSRobert Watson }
366203175483SAlexander Motin 
366303175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache)
366403175483SAlexander Motin {
366503175483SAlexander Motin 	uint64_t allocs, frees;
366603175483SAlexander Motin 	uma_bucket_t bucket;
366703175483SAlexander Motin 	uma_zone_t z;
366803175483SAlexander Motin 	int cachefree;
366903175483SAlexander Motin 
367003175483SAlexander Motin 	db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
367103175483SAlexander Motin 	    "Requests", "Bucket");
367203175483SAlexander Motin 	LIST_FOREACH(z, &uma_cachezones, uz_link) {
367303175483SAlexander Motin 		uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL);
367403175483SAlexander Motin 		LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
367503175483SAlexander Motin 			cachefree += bucket->ub_cnt;
367603175483SAlexander Motin 		db_printf("%18s %8ju %8jd %8d %12ju %8u\n",
367703175483SAlexander Motin 		    z->uz_name, (uintmax_t)z->uz_size,
367803175483SAlexander Motin 		    (intmax_t)(allocs - frees), cachefree,
367903175483SAlexander Motin 		    (uintmax_t)allocs, z->uz_count);
368003175483SAlexander Motin 		if (db_pager_quit)
368103175483SAlexander Motin 			return;
368203175483SAlexander Motin 	}
368303175483SAlexander Motin }
36849542ea7bSGleb Smirnoff #endif	/* DDB */
3685