xref: /freebsd/sys/vm/uma_core.c (revision b245ac95cfb06f9c99216744a926ce18801f7642)
18355f576SJeff Roberson /*
2f461cf22SJeff Roberson  * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
38355f576SJeff Roberson  * All rights reserved.
48355f576SJeff Roberson  *
58355f576SJeff Roberson  * Redistribution and use in source and binary forms, with or without
68355f576SJeff Roberson  * modification, are permitted provided that the following conditions
78355f576SJeff Roberson  * are met:
88355f576SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
98355f576SJeff Roberson  *    notice unmodified, this list of conditions, and the following
108355f576SJeff Roberson  *    disclaimer.
118355f576SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
128355f576SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
138355f576SJeff Roberson  *    documentation and/or other materials provided with the distribution.
148355f576SJeff Roberson  *
158355f576SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
168355f576SJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
178355f576SJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188355f576SJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
198355f576SJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
208355f576SJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
218355f576SJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
228355f576SJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
238355f576SJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
248355f576SJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258355f576SJeff Roberson  */
268355f576SJeff Roberson 
278355f576SJeff Roberson /*
288355f576SJeff Roberson  * uma_core.c  Implementation of the Universal Memory allocator
298355f576SJeff Roberson  *
308355f576SJeff Roberson  * This allocator is intended to replace the multitude of similar object caches
318355f576SJeff Roberson  * in the standard FreeBSD kernel.  The intent is to be flexible as well as
328355f576SJeff Roberson  * effecient.  A primary design goal is to return unused memory to the rest of
338355f576SJeff Roberson  * the system.  This will make the system as a whole more flexible due to the
348355f576SJeff Roberson  * ability to move memory to subsystems which most need it instead of leaving
358355f576SJeff Roberson  * pools of reserved memory unused.
368355f576SJeff Roberson  *
378355f576SJeff Roberson  * The basic ideas stem from similar slab/zone based allocators whose algorithms
388355f576SJeff Roberson  * are well known.
398355f576SJeff Roberson  *
408355f576SJeff Roberson  */
418355f576SJeff Roberson 
428355f576SJeff Roberson /*
438355f576SJeff Roberson  * TODO:
448355f576SJeff Roberson  *	- Improve memory usage for large allocations
458355f576SJeff Roberson  *	- Investigate cache size adjustments
468355f576SJeff Roberson  */
478355f576SJeff Roberson 
48874651b1SDavid E. O'Brien #include <sys/cdefs.h>
49874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
50874651b1SDavid E. O'Brien 
518355f576SJeff Roberson /* I should really use ktr.. */
528355f576SJeff Roberson /*
538355f576SJeff Roberson #define UMA_DEBUG 1
548355f576SJeff Roberson #define UMA_DEBUG_ALLOC 1
558355f576SJeff Roberson #define UMA_DEBUG_ALLOC_1 1
568355f576SJeff Roberson */
578355f576SJeff Roberson 
588355f576SJeff Roberson #include "opt_param.h"
598355f576SJeff Roberson #include <sys/param.h>
608355f576SJeff Roberson #include <sys/systm.h>
618355f576SJeff Roberson #include <sys/kernel.h>
628355f576SJeff Roberson #include <sys/types.h>
638355f576SJeff Roberson #include <sys/queue.h>
648355f576SJeff Roberson #include <sys/malloc.h>
658355f576SJeff Roberson #include <sys/lock.h>
668355f576SJeff Roberson #include <sys/sysctl.h>
678355f576SJeff Roberson #include <sys/mutex.h>
684c1cc01cSJohn Baldwin #include <sys/proc.h>
698355f576SJeff Roberson #include <sys/smp.h>
7086bbae32SJeff Roberson #include <sys/vmmeter.h>
718522511bSHartmut Brandt #include <sys/mbuf.h>
7286bbae32SJeff Roberson 
738355f576SJeff Roberson #include <vm/vm.h>
748355f576SJeff Roberson #include <vm/vm_object.h>
758355f576SJeff Roberson #include <vm/vm_page.h>
768355f576SJeff Roberson #include <vm/vm_param.h>
778355f576SJeff Roberson #include <vm/vm_map.h>
788355f576SJeff Roberson #include <vm/vm_kern.h>
798355f576SJeff Roberson #include <vm/vm_extern.h>
808355f576SJeff Roberson #include <vm/uma.h>
818355f576SJeff Roberson #include <vm/uma_int.h>
82639c9550SJeff Roberson #include <vm/uma_dbg.h>
838355f576SJeff Roberson 
8448eea375SJeff Roberson #include <machine/vmparam.h>
8548eea375SJeff Roberson 
868355f576SJeff Roberson /*
878355f576SJeff Roberson  * This is the zone from which all zones are spawned.  The idea is that even
888355f576SJeff Roberson  * the zone heads are allocated from the allocator, so we use the bss section
898355f576SJeff Roberson  * to bootstrap us.
908355f576SJeff Roberson  */
9186bbae32SJeff Roberson static struct uma_zone masterzone;
9286bbae32SJeff Roberson static uma_zone_t zones = &masterzone;
938355f576SJeff Roberson 
948355f576SJeff Roberson /* This is the zone from which all of uma_slab_t's are allocated. */
958355f576SJeff Roberson static uma_zone_t slabzone;
968355f576SJeff Roberson 
978355f576SJeff Roberson /*
988355f576SJeff Roberson  * The initial hash tables come out of this zone so they can be allocated
998355f576SJeff Roberson  * prior to malloc coming up.
1008355f576SJeff Roberson  */
1018355f576SJeff Roberson static uma_zone_t hashzone;
1028355f576SJeff Roberson 
1038355f576SJeff Roberson /*
1048355f576SJeff Roberson  * Zone that buckets come from.
1058355f576SJeff Roberson  */
1068355f576SJeff Roberson static uma_zone_t bucketzone;
1078355f576SJeff Roberson 
10886bbae32SJeff Roberson /*
10986bbae32SJeff Roberson  * Are we allowed to allocate buckets?
11086bbae32SJeff Roberson  */
11186bbae32SJeff Roberson static int bucketdisable = 1;
11286bbae32SJeff Roberson 
1138355f576SJeff Roberson /* Linked list of all zones in the system */
1148355f576SJeff Roberson static LIST_HEAD(,uma_zone) uma_zones = LIST_HEAD_INITIALIZER(&uma_zones);
1158355f576SJeff Roberson 
1168355f576SJeff Roberson /* This mutex protects the zone list */
1178355f576SJeff Roberson static struct mtx uma_mtx;
1188355f576SJeff Roberson 
119d88797c2SBosko Milekic /* These are the pcpu cache locks */
120d88797c2SBosko Milekic static struct mtx uma_pcpu_mtx[MAXCPU];
121d88797c2SBosko Milekic 
1228355f576SJeff Roberson /* Linked list of boot time pages */
1238355f576SJeff Roberson static LIST_HEAD(,uma_slab) uma_boot_pages =
1248355f576SJeff Roberson     LIST_HEAD_INITIALIZER(&uma_boot_pages);
1258355f576SJeff Roberson 
1268355f576SJeff Roberson /* Count of free boottime pages */
1278355f576SJeff Roberson static int uma_boot_free = 0;
1288355f576SJeff Roberson 
1298355f576SJeff Roberson /* Is the VM done starting up? */
1308355f576SJeff Roberson static int booted = 0;
1318355f576SJeff Roberson 
1328355f576SJeff Roberson /* This is the handle used to schedule our working set calculator */
1338355f576SJeff Roberson static struct callout uma_callout;
1348355f576SJeff Roberson 
1358355f576SJeff Roberson /* This is mp_maxid + 1, for use while looping over each cpu */
1368355f576SJeff Roberson static int maxcpu;
1378355f576SJeff Roberson 
1388355f576SJeff Roberson /*
1398355f576SJeff Roberson  * This structure is passed as the zone ctor arg so that I don't have to create
1408355f576SJeff Roberson  * a special allocation function just for zones.
1418355f576SJeff Roberson  */
1428355f576SJeff Roberson struct uma_zctor_args {
1438355f576SJeff Roberson 	char *name;
144c3bdc05fSAndrew R. Reiter 	size_t size;
1458355f576SJeff Roberson 	uma_ctor ctor;
1468355f576SJeff Roberson 	uma_dtor dtor;
1478355f576SJeff Roberson 	uma_init uminit;
1488355f576SJeff Roberson 	uma_fini fini;
1498355f576SJeff Roberson 	int align;
1508355f576SJeff Roberson 	u_int16_t flags;
1518355f576SJeff Roberson };
1528355f576SJeff Roberson 
1538355f576SJeff Roberson /* Prototypes.. */
1548355f576SJeff Roberson 
1558355f576SJeff Roberson static void *obj_alloc(uma_zone_t, int, u_int8_t *, int);
1568355f576SJeff Roberson static void *page_alloc(uma_zone_t, int, u_int8_t *, int);
1578355f576SJeff Roberson static void page_free(void *, int, u_int8_t);
1588355f576SJeff Roberson static uma_slab_t slab_zalloc(uma_zone_t, int);
159d56368d7SBosko Milekic static void cache_drain(uma_zone_t, int);
1608355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t);
161d56368d7SBosko Milekic static void zone_drain_common(uma_zone_t, int);
1628355f576SJeff Roberson static void zone_ctor(void *, int, void *);
1639c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *);
1648355f576SJeff Roberson static void zero_init(void *, int);
1658355f576SJeff Roberson static void zone_small_init(uma_zone_t zone);
1668355f576SJeff Roberson static void zone_large_init(uma_zone_t zone);
1678355f576SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t));
1688355f576SJeff Roberson static void zone_timeout(uma_zone_t zone);
1690aef6126SJeff Roberson static int hash_alloc(struct uma_hash *);
1700aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *);
1710aef6126SJeff Roberson static void hash_free(struct uma_hash *hash);
1728355f576SJeff Roberson static void uma_timeout(void *);
1738355f576SJeff Roberson static void uma_startup3(void);
174bbee39c6SJeff Roberson static void *uma_zalloc_internal(uma_zone_t, void *, int);
17586bbae32SJeff Roberson static void uma_zfree_internal(uma_zone_t, void *, void *, int);
17686bbae32SJeff Roberson static void bucket_enable(void);
177bbee39c6SJeff Roberson static int uma_zalloc_bucket(uma_zone_t zone, int flags);
178bbee39c6SJeff Roberson static uma_slab_t uma_zone_slab(uma_zone_t zone, int flags);
179bbee39c6SJeff Roberson static void *uma_slab_alloc(uma_zone_t zone, uma_slab_t slab);
180d56368d7SBosko Milekic static __inline void zone_drain(uma_zone_t);
181bbee39c6SJeff Roberson 
1828355f576SJeff Roberson void uma_print_zone(uma_zone_t);
1838355f576SJeff Roberson void uma_print_stats(void);
1848355f576SJeff Roberson static int sysctl_vm_zone(SYSCTL_HANDLER_ARGS);
1858355f576SJeff Roberson 
1868355f576SJeff Roberson SYSCTL_OID(_vm, OID_AUTO, zone, CTLTYPE_STRING|CTLFLAG_RD,
1878355f576SJeff Roberson     NULL, 0, sysctl_vm_zone, "A", "Zone Info");
1888355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
1898355f576SJeff Roberson 
19086bbae32SJeff Roberson /*
19186bbae32SJeff Roberson  * This routine checks to see whether or not it's safe to enable buckets.
19286bbae32SJeff Roberson  */
19386bbae32SJeff Roberson 
19486bbae32SJeff Roberson static void
19586bbae32SJeff Roberson bucket_enable(void)
19686bbae32SJeff Roberson {
19786bbae32SJeff Roberson 	if (cnt.v_free_count < cnt.v_free_min)
19886bbae32SJeff Roberson 		bucketdisable = 1;
19986bbae32SJeff Roberson 	else
20086bbae32SJeff Roberson 		bucketdisable = 0;
20186bbae32SJeff Roberson }
20286bbae32SJeff Roberson 
2038355f576SJeff Roberson 
2048355f576SJeff Roberson /*
2058355f576SJeff Roberson  * Routine called by timeout which is used to fire off some time interval
2068355f576SJeff Roberson  * based calculations.  (working set, stats, etc.)
2078355f576SJeff Roberson  *
2088355f576SJeff Roberson  * Arguments:
2098355f576SJeff Roberson  *	arg   Unused
2108355f576SJeff Roberson  *
2118355f576SJeff Roberson  * Returns:
2128355f576SJeff Roberson  *	Nothing
2138355f576SJeff Roberson  */
2148355f576SJeff Roberson static void
2158355f576SJeff Roberson uma_timeout(void *unused)
2168355f576SJeff Roberson {
21786bbae32SJeff Roberson 	bucket_enable();
2188355f576SJeff Roberson 	zone_foreach(zone_timeout);
2198355f576SJeff Roberson 
2208355f576SJeff Roberson 	/* Reschedule this event */
2218355f576SJeff Roberson 	callout_reset(&uma_callout, UMA_WORKING_TIME * hz, uma_timeout, NULL);
2228355f576SJeff Roberson }
2238355f576SJeff Roberson 
2248355f576SJeff Roberson /*
2258355f576SJeff Roberson  * Routine to perform timeout driven calculations.  This does the working set
2268355f576SJeff Roberson  * as well as hash expanding, and per cpu statistics aggregation.
2278355f576SJeff Roberson  *
2288355f576SJeff Roberson  *  Arguments:
2298355f576SJeff Roberson  *	zone  The zone to operate on
2308355f576SJeff Roberson  *
2318355f576SJeff Roberson  *  Returns:
2328355f576SJeff Roberson  *	Nothing
2338355f576SJeff Roberson  */
2348355f576SJeff Roberson static void
2358355f576SJeff Roberson zone_timeout(uma_zone_t zone)
2368355f576SJeff Roberson {
2378355f576SJeff Roberson 	uma_cache_t cache;
2388355f576SJeff Roberson 	u_int64_t alloc;
2398355f576SJeff Roberson 	int cpu;
2408355f576SJeff Roberson 
2418355f576SJeff Roberson 	alloc = 0;
2428355f576SJeff Roberson 
2438355f576SJeff Roberson 	/*
2448355f576SJeff Roberson 	 * Aggregate per cpu cache statistics back to the zone.
2458355f576SJeff Roberson 	 *
2468355f576SJeff Roberson 	 * I may rewrite this to set a flag in the per cpu cache instead of
2478355f576SJeff Roberson 	 * locking.  If the flag is not cleared on the next round I will have
2488355f576SJeff Roberson 	 * to lock and do it here instead so that the statistics don't get too
2498355f576SJeff Roberson 	 * far out of sync.
2508355f576SJeff Roberson 	 */
2518355f576SJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) {
2528355f576SJeff Roberson 		for (cpu = 0; cpu < maxcpu; cpu++) {
2538355f576SJeff Roberson 			if (CPU_ABSENT(cpu))
2548355f576SJeff Roberson 				continue;
255d88797c2SBosko Milekic 			CPU_LOCK(cpu);
2568355f576SJeff Roberson 			cache = &zone->uz_cpu[cpu];
2578355f576SJeff Roberson 			/* Add them up, and reset */
2588355f576SJeff Roberson 			alloc += cache->uc_allocs;
2598355f576SJeff Roberson 			cache->uc_allocs = 0;
260d88797c2SBosko Milekic 			CPU_UNLOCK(cpu);
2618355f576SJeff Roberson 		}
2628355f576SJeff Roberson 	}
2638355f576SJeff Roberson 
2648355f576SJeff Roberson 	/* Now push these stats back into the zone.. */
2658355f576SJeff Roberson 	ZONE_LOCK(zone);
2668355f576SJeff Roberson 	zone->uz_allocs += alloc;
2678355f576SJeff Roberson 
2688355f576SJeff Roberson 	/*
2698355f576SJeff Roberson 	 * Expand the zone hash table.
2708355f576SJeff Roberson 	 *
2718355f576SJeff Roberson 	 * This is done if the number of slabs is larger than the hash size.
2728355f576SJeff Roberson 	 * What I'm trying to do here is completely reduce collisions.  This
2738355f576SJeff Roberson 	 * may be a little aggressive.  Should I allow for two collisions max?
2748355f576SJeff Roberson 	 */
2758355f576SJeff Roberson 
27699571dc3SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_HASH &&
27799571dc3SJeff Roberson 	    zone->uz_pages / zone->uz_ppera >= zone->uz_hash.uh_hashsize) {
2780aef6126SJeff Roberson 		struct uma_hash newhash;
2790aef6126SJeff Roberson 		struct uma_hash oldhash;
2800aef6126SJeff Roberson 		int ret;
2815300d9ddSJeff Roberson 
2820aef6126SJeff Roberson 		/*
2830aef6126SJeff Roberson 		 * This is so involved because allocating and freeing
2840aef6126SJeff Roberson 		 * while the zone lock is held will lead to deadlock.
2850aef6126SJeff Roberson 		 * I have to do everything in stages and check for
2860aef6126SJeff Roberson 		 * races.
2870aef6126SJeff Roberson 		 */
2880aef6126SJeff Roberson 		newhash = zone->uz_hash;
2895300d9ddSJeff Roberson 		ZONE_UNLOCK(zone);
2900aef6126SJeff Roberson 		ret = hash_alloc(&newhash);
2915300d9ddSJeff Roberson 		ZONE_LOCK(zone);
2920aef6126SJeff Roberson 		if (ret) {
2930aef6126SJeff Roberson 			if (hash_expand(&zone->uz_hash, &newhash)) {
2940aef6126SJeff Roberson 				oldhash = zone->uz_hash;
2950aef6126SJeff Roberson 				zone->uz_hash = newhash;
2960aef6126SJeff Roberson 			} else
2970aef6126SJeff Roberson 				oldhash = newhash;
2980aef6126SJeff Roberson 
2990aef6126SJeff Roberson 			ZONE_UNLOCK(zone);
3000aef6126SJeff Roberson 			hash_free(&oldhash);
3010aef6126SJeff Roberson 			ZONE_LOCK(zone);
3020aef6126SJeff Roberson 		}
3035300d9ddSJeff Roberson 	}
3048355f576SJeff Roberson 
3058355f576SJeff Roberson 	/*
3068355f576SJeff Roberson 	 * Here we compute the working set size as the total number of items
3078355f576SJeff Roberson 	 * left outstanding since the last time interval.  This is slightly
3088355f576SJeff Roberson 	 * suboptimal. What we really want is the highest number of outstanding
3098355f576SJeff Roberson 	 * items during the last time quantum.  This should be close enough.
3108355f576SJeff Roberson 	 *
3118355f576SJeff Roberson 	 * The working set size is used to throttle the zone_drain function.
3128355f576SJeff Roberson 	 * We don't want to return memory that we may need again immediately.
3138355f576SJeff Roberson 	 */
3148355f576SJeff Roberson 	alloc = zone->uz_allocs - zone->uz_oallocs;
3158355f576SJeff Roberson 	zone->uz_oallocs = zone->uz_allocs;
3168355f576SJeff Roberson 	zone->uz_wssize = alloc;
3178355f576SJeff Roberson 
3188355f576SJeff Roberson 	ZONE_UNLOCK(zone);
3198355f576SJeff Roberson }
3208355f576SJeff Roberson 
3218355f576SJeff Roberson /*
3225300d9ddSJeff Roberson  * Allocate and zero fill the next sized hash table from the appropriate
3235300d9ddSJeff Roberson  * backing store.
3245300d9ddSJeff Roberson  *
3255300d9ddSJeff Roberson  * Arguments:
3260aef6126SJeff Roberson  *	hash  A new hash structure with the old hash size in uh_hashsize
3275300d9ddSJeff Roberson  *
3285300d9ddSJeff Roberson  * Returns:
3290aef6126SJeff Roberson  *	1 on sucess and 0 on failure.
3305300d9ddSJeff Roberson  */
33137c84183SPoul-Henning Kamp static int
3320aef6126SJeff Roberson hash_alloc(struct uma_hash *hash)
3335300d9ddSJeff Roberson {
3340aef6126SJeff Roberson 	int oldsize;
3355300d9ddSJeff Roberson 	int alloc;
3365300d9ddSJeff Roberson 
3370aef6126SJeff Roberson 	oldsize = hash->uh_hashsize;
3380aef6126SJeff Roberson 
3395300d9ddSJeff Roberson 	/* We're just going to go to a power of two greater */
3400aef6126SJeff Roberson 	if (oldsize)  {
3410aef6126SJeff Roberson 		hash->uh_hashsize = oldsize * 2;
3420aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
3435300d9ddSJeff Roberson 		/* XXX Shouldn't be abusing DEVBUF here */
3440aef6126SJeff Roberson 		hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
3450aef6126SJeff Roberson 		    M_DEVBUF, M_NOWAIT);
3465300d9ddSJeff Roberson 	} else {
3470aef6126SJeff Roberson 		alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
3480aef6126SJeff Roberson 		hash->uh_slab_hash = uma_zalloc_internal(hashzone, NULL,
349a163d034SWarner Losh 		    M_WAITOK);
3500aef6126SJeff Roberson 		hash->uh_hashsize = UMA_HASH_SIZE_INIT;
3515300d9ddSJeff Roberson 	}
3520aef6126SJeff Roberson 	if (hash->uh_slab_hash) {
3530aef6126SJeff Roberson 		bzero(hash->uh_slab_hash, alloc);
3540aef6126SJeff Roberson 		hash->uh_hashmask = hash->uh_hashsize - 1;
3550aef6126SJeff Roberson 		return (1);
3560aef6126SJeff Roberson 	}
3575300d9ddSJeff Roberson 
3580aef6126SJeff Roberson 	return (0);
3595300d9ddSJeff Roberson }
3605300d9ddSJeff Roberson 
3615300d9ddSJeff Roberson /*
3628355f576SJeff Roberson  * Expands the hash table for OFFPAGE zones.  This is done from zone_timeout
3638355f576SJeff Roberson  * to reduce collisions.  This must not be done in the regular allocation path,
3648355f576SJeff Roberson  * otherwise, we can recurse on the vm while allocating pages.
3658355f576SJeff Roberson  *
3668355f576SJeff Roberson  * Arguments:
3670aef6126SJeff Roberson  *	oldhash  The hash you want to expand
3680aef6126SJeff Roberson  *	newhash  The hash structure for the new table
3698355f576SJeff Roberson  *
3708355f576SJeff Roberson  * Returns:
3718355f576SJeff Roberson  * 	Nothing
3728355f576SJeff Roberson  *
3738355f576SJeff Roberson  * Discussion:
3748355f576SJeff Roberson  */
3750aef6126SJeff Roberson static int
3760aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
3778355f576SJeff Roberson {
3788355f576SJeff Roberson 	uma_slab_t slab;
3798355f576SJeff Roberson 	int hval;
3808355f576SJeff Roberson 	int i;
3818355f576SJeff Roberson 
3820aef6126SJeff Roberson 	if (!newhash->uh_slab_hash)
3830aef6126SJeff Roberson 		return (0);
3848355f576SJeff Roberson 
3850aef6126SJeff Roberson 	if (oldhash->uh_hashsize >= newhash->uh_hashsize)
3860aef6126SJeff Roberson 		return (0);
3878355f576SJeff Roberson 
3888355f576SJeff Roberson 	/*
3898355f576SJeff Roberson 	 * I need to investigate hash algorithms for resizing without a
3908355f576SJeff Roberson 	 * full rehash.
3918355f576SJeff Roberson 	 */
3928355f576SJeff Roberson 
3930aef6126SJeff Roberson 	for (i = 0; i < oldhash->uh_hashsize; i++)
3940aef6126SJeff Roberson 		while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
3950aef6126SJeff Roberson 			slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
3960aef6126SJeff Roberson 			SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
3970aef6126SJeff Roberson 			hval = UMA_HASH(newhash, slab->us_data);
3980aef6126SJeff Roberson 			SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
3990aef6126SJeff Roberson 			    slab, us_hlink);
4008355f576SJeff Roberson 		}
4018355f576SJeff Roberson 
4020aef6126SJeff Roberson 	return (1);
4039c2cd7e5SJeff Roberson }
4049c2cd7e5SJeff Roberson 
4055300d9ddSJeff Roberson /*
4065300d9ddSJeff Roberson  * Free the hash bucket to the appropriate backing store.
4075300d9ddSJeff Roberson  *
4085300d9ddSJeff Roberson  * Arguments:
4095300d9ddSJeff Roberson  *	slab_hash  The hash bucket we're freeing
4105300d9ddSJeff Roberson  *	hashsize   The number of entries in that hash bucket
4115300d9ddSJeff Roberson  *
4125300d9ddSJeff Roberson  * Returns:
4135300d9ddSJeff Roberson  *	Nothing
4145300d9ddSJeff Roberson  */
4159c2cd7e5SJeff Roberson static void
4160aef6126SJeff Roberson hash_free(struct uma_hash *hash)
4179c2cd7e5SJeff Roberson {
4180aef6126SJeff Roberson 	if (hash->uh_slab_hash == NULL)
4190aef6126SJeff Roberson 		return;
4200aef6126SJeff Roberson 	if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
4218355f576SJeff Roberson 		uma_zfree_internal(hashzone,
4220aef6126SJeff Roberson 		    hash->uh_slab_hash, NULL, 0);
4238355f576SJeff Roberson 	else
4240aef6126SJeff Roberson 		free(hash->uh_slab_hash, M_DEVBUF);
4258355f576SJeff Roberson }
4268355f576SJeff Roberson 
4278355f576SJeff Roberson /*
4288355f576SJeff Roberson  * Frees all outstanding items in a bucket
4298355f576SJeff Roberson  *
4308355f576SJeff Roberson  * Arguments:
4318355f576SJeff Roberson  *	zone   The zone to free to, must be unlocked.
4328355f576SJeff Roberson  *	bucket The free/alloc bucket with items, cpu queue must be locked.
4338355f576SJeff Roberson  *
4348355f576SJeff Roberson  * Returns:
4358355f576SJeff Roberson  *	Nothing
4368355f576SJeff Roberson  */
4378355f576SJeff Roberson 
4388355f576SJeff Roberson static void
4398355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
4408355f576SJeff Roberson {
4418355f576SJeff Roberson 	uma_slab_t slab;
4428355f576SJeff Roberson 	int mzone;
4438355f576SJeff Roberson 	void *item;
4448355f576SJeff Roberson 
4458355f576SJeff Roberson 	if (bucket == NULL)
4468355f576SJeff Roberson 		return;
4478355f576SJeff Roberson 
4488355f576SJeff Roberson 	slab = NULL;
4498355f576SJeff Roberson 	mzone = 0;
4508355f576SJeff Roberson 
4518355f576SJeff Roberson 	/* We have to lookup the slab again for malloc.. */
4528355f576SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_MALLOC)
4538355f576SJeff Roberson 		mzone = 1;
4548355f576SJeff Roberson 
4558355f576SJeff Roberson 	while (bucket->ub_ptr > -1)  {
4568355f576SJeff Roberson 		item = bucket->ub_bucket[bucket->ub_ptr];
4578355f576SJeff Roberson #ifdef INVARIANTS
4588355f576SJeff Roberson 		bucket->ub_bucket[bucket->ub_ptr] = NULL;
4598355f576SJeff Roberson 		KASSERT(item != NULL,
4608355f576SJeff Roberson 		    ("bucket_drain: botched ptr, item is NULL"));
4618355f576SJeff Roberson #endif
4628355f576SJeff Roberson 		bucket->ub_ptr--;
4638355f576SJeff Roberson 		/*
4648355f576SJeff Roberson 		 * This is extremely inefficient.  The slab pointer was passed
4658355f576SJeff Roberson 		 * to uma_zfree_arg, but we lost it because the buckets don't
4668355f576SJeff Roberson 		 * hold them.  This will go away when free() gets a size passed
4678355f576SJeff Roberson 		 * to it.
4688355f576SJeff Roberson 		 */
46999571dc3SJeff Roberson 		if (mzone)
47099571dc3SJeff Roberson 			slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK));
4718355f576SJeff Roberson 		uma_zfree_internal(zone, item, slab, 1);
4728355f576SJeff Roberson 	}
4738355f576SJeff Roberson }
4748355f576SJeff Roberson 
4758355f576SJeff Roberson /*
4768355f576SJeff Roberson  * Drains the per cpu caches for a zone.
4778355f576SJeff Roberson  *
4788355f576SJeff Roberson  * Arguments:
4798355f576SJeff Roberson  *	zone     The zone to drain, must be unlocked.
480d56368d7SBosko Milekic  *	destroy  Whether or not to destroy the pcpu buckets (from zone_dtor)
4818355f576SJeff Roberson  *
4828355f576SJeff Roberson  * Returns:
4838355f576SJeff Roberson  *	Nothing
4848355f576SJeff Roberson  *
4858355f576SJeff Roberson  * This function returns with the zone locked so that the per cpu queues can
4868355f576SJeff Roberson  * not be filled until zone_drain is finished.
4878355f576SJeff Roberson  *
4888355f576SJeff Roberson  */
4898355f576SJeff Roberson static void
490d56368d7SBosko Milekic cache_drain(uma_zone_t zone, int destroy)
4918355f576SJeff Roberson {
4928355f576SJeff Roberson 	uma_bucket_t bucket;
4938355f576SJeff Roberson 	uma_cache_t cache;
4948355f576SJeff Roberson 	int cpu;
4958355f576SJeff Roberson 
4968355f576SJeff Roberson 	/*
4978355f576SJeff Roberson 	 * Flush out the per cpu queues.
4988355f576SJeff Roberson 	 *
499157d7b35SAlfred Perlstein 	 * XXX This causes unnecessary thrashing due to immediately having
5008355f576SJeff Roberson 	 * empty per cpu queues.  I need to improve this.
5018355f576SJeff Roberson 	 */
5028355f576SJeff Roberson 
5038355f576SJeff Roberson 	/*
5048355f576SJeff Roberson 	 * We have to lock each cpu cache before locking the zone
5058355f576SJeff Roberson 	 */
5068355f576SJeff Roberson 	ZONE_UNLOCK(zone);
5078355f576SJeff Roberson 
5088355f576SJeff Roberson 	for (cpu = 0; cpu < maxcpu; cpu++) {
5098355f576SJeff Roberson 		if (CPU_ABSENT(cpu))
5108355f576SJeff Roberson 			continue;
511d88797c2SBosko Milekic 		CPU_LOCK(cpu);
5128355f576SJeff Roberson 		cache = &zone->uz_cpu[cpu];
5138355f576SJeff Roberson 		bucket_drain(zone, cache->uc_allocbucket);
5148355f576SJeff Roberson 		bucket_drain(zone, cache->uc_freebucket);
515d56368d7SBosko Milekic 		if (destroy) {
516174ab450SBosko Milekic 			if (cache->uc_allocbucket != NULL)
517174ab450SBosko Milekic 				uma_zfree_internal(bucketzone,
518174ab450SBosko Milekic 				    cache->uc_allocbucket, NULL, 0);
519174ab450SBosko Milekic 			if (cache->uc_freebucket != NULL)
520174ab450SBosko Milekic 				uma_zfree_internal(bucketzone,
521174ab450SBosko Milekic 				    cache->uc_freebucket, NULL, 0);
522d56368d7SBosko Milekic 			cache->uc_allocbucket = cache->uc_freebucket = NULL;
523d56368d7SBosko Milekic 		}
5248355f576SJeff Roberson 	}
5258355f576SJeff Roberson 
5268355f576SJeff Roberson 	/*
5278355f576SJeff Roberson 	 * Drain the bucket queues and free the buckets, we just keep two per
5288355f576SJeff Roberson 	 * cpu (alloc/free).
5298355f576SJeff Roberson 	 */
5308355f576SJeff Roberson 	ZONE_LOCK(zone);
5318355f576SJeff Roberson 	while ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
5328355f576SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
5338355f576SJeff Roberson 		ZONE_UNLOCK(zone);
5348355f576SJeff Roberson 		bucket_drain(zone, bucket);
5358355f576SJeff Roberson 		uma_zfree_internal(bucketzone, bucket, NULL, 0);
5368355f576SJeff Roberson 		ZONE_LOCK(zone);
5378355f576SJeff Roberson 	}
5388355f576SJeff Roberson 
5398355f576SJeff Roberson 	/* Now we do the free queue.. */
5408355f576SJeff Roberson 	while ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
5418355f576SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
5428355f576SJeff Roberson 		uma_zfree_internal(bucketzone, bucket, NULL, 0);
5438355f576SJeff Roberson 	}
5448355f576SJeff Roberson 
5458355f576SJeff Roberson 	/* We unlock here, but they will all block until the zone is unlocked */
5468355f576SJeff Roberson 	for (cpu = 0; cpu < maxcpu; cpu++) {
5478355f576SJeff Roberson 		if (CPU_ABSENT(cpu))
5488355f576SJeff Roberson 			continue;
549d88797c2SBosko Milekic 		CPU_UNLOCK(cpu);
5508355f576SJeff Roberson 	}
5518355f576SJeff Roberson }
5528355f576SJeff Roberson 
5538355f576SJeff Roberson /*
5548355f576SJeff Roberson  * Frees pages from a zone back to the system.  This is done on demand from
5558355f576SJeff Roberson  * the pageout daemon.
5568355f576SJeff Roberson  *
5578355f576SJeff Roberson  * Arguments:
5588355f576SJeff Roberson  *	zone  The zone to free pages from
5599c2cd7e5SJeff Roberson  *	 all  Should we drain all items?
560d56368d7SBosko Milekic  *   destroy  Whether to destroy the zone and pcpu buckets (from zone_dtor)
5618355f576SJeff Roberson  *
5628355f576SJeff Roberson  * Returns:
5638355f576SJeff Roberson  *	Nothing.
5648355f576SJeff Roberson  */
5658355f576SJeff Roberson static void
566d56368d7SBosko Milekic zone_drain_common(uma_zone_t zone, int destroy)
5678355f576SJeff Roberson {
568713deb36SJeff Roberson 	struct slabhead freeslabs = {};
5698355f576SJeff Roberson 	uma_slab_t slab;
5708355f576SJeff Roberson 	uma_slab_t n;
5718355f576SJeff Roberson 	u_int64_t extra;
5728355f576SJeff Roberson 	u_int8_t flags;
5738355f576SJeff Roberson 	u_int8_t *mem;
5748355f576SJeff Roberson 	int i;
5758355f576SJeff Roberson 
5768355f576SJeff Roberson 	/*
5778355f576SJeff Roberson 	 * We don't want to take pages from staticly allocated zones at this
5788355f576SJeff Roberson 	 * time
5798355f576SJeff Roberson 	 */
5808355f576SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_NOFREE || zone->uz_freef == NULL)
5818355f576SJeff Roberson 		return;
5828355f576SJeff Roberson 
5838355f576SJeff Roberson 	ZONE_LOCK(zone);
5848355f576SJeff Roberson 
5858355f576SJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
586d56368d7SBosko Milekic 		cache_drain(zone, destroy);
587d56368d7SBosko Milekic 
588d56368d7SBosko Milekic 	if (destroy)
589d56368d7SBosko Milekic 		zone->uz_wssize = 0;
5908355f576SJeff Roberson 
5918355f576SJeff Roberson 	if (zone->uz_free < zone->uz_wssize)
5928355f576SJeff Roberson 		goto finished;
5938355f576SJeff Roberson #ifdef UMA_DEBUG
5948355f576SJeff Roberson 	printf("%s working set size: %llu free items: %u\n",
5958355f576SJeff Roberson 	    zone->uz_name, (unsigned long long)zone->uz_wssize, zone->uz_free);
5968355f576SJeff Roberson #endif
5979c2cd7e5SJeff Roberson 	extra = zone->uz_free - zone->uz_wssize;
5988355f576SJeff Roberson 	extra /= zone->uz_ipers;
5998355f576SJeff Roberson 
6008355f576SJeff Roberson 	/* extra is now the number of extra slabs that we can free */
6018355f576SJeff Roberson 
6028355f576SJeff Roberson 	if (extra == 0)
6038355f576SJeff Roberson 		goto finished;
6048355f576SJeff Roberson 
6058355f576SJeff Roberson 	slab = LIST_FIRST(&zone->uz_free_slab);
6068355f576SJeff Roberson 	while (slab && extra) {
6078355f576SJeff Roberson 		n = LIST_NEXT(slab, us_link);
6088355f576SJeff Roberson 
6098355f576SJeff Roberson 		/* We have no where to free these to */
6108355f576SJeff Roberson 		if (slab->us_flags & UMA_SLAB_BOOT) {
6118355f576SJeff Roberson 			slab = n;
6128355f576SJeff Roberson 			continue;
6138355f576SJeff Roberson 		}
6148355f576SJeff Roberson 
6158355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
6168355f576SJeff Roberson 		zone->uz_pages -= zone->uz_ppera;
6178355f576SJeff Roberson 		zone->uz_free -= zone->uz_ipers;
618713deb36SJeff Roberson 
61999571dc3SJeff Roberson 		if (zone->uz_flags & UMA_ZFLAG_HASH)
620713deb36SJeff Roberson 			UMA_HASH_REMOVE(&zone->uz_hash, slab, slab->us_data);
621713deb36SJeff Roberson 
622713deb36SJeff Roberson 		SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
623713deb36SJeff Roberson 
624713deb36SJeff Roberson 		slab = n;
625713deb36SJeff Roberson 		extra--;
626713deb36SJeff Roberson 	}
627713deb36SJeff Roberson finished:
628713deb36SJeff Roberson 	ZONE_UNLOCK(zone);
629713deb36SJeff Roberson 
630713deb36SJeff Roberson 	while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
631713deb36SJeff Roberson 		SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
6328355f576SJeff Roberson 		if (zone->uz_fini)
6338355f576SJeff Roberson 			for (i = 0; i < zone->uz_ipers; i++)
6348355f576SJeff Roberson 				zone->uz_fini(
6358355f576SJeff Roberson 				    slab->us_data + (zone->uz_rsize * i),
6368355f576SJeff Roberson 				    zone->uz_size);
6378355f576SJeff Roberson 		flags = slab->us_flags;
6388355f576SJeff Roberson 		mem = slab->us_data;
63999571dc3SJeff Roberson 
64099571dc3SJeff Roberson 		if (zone->uz_flags & UMA_ZFLAG_OFFPAGE)
6418355f576SJeff Roberson 			uma_zfree_internal(slabzone, slab, NULL, 0);
64248eea375SJeff Roberson 		if (zone->uz_flags & UMA_ZFLAG_MALLOC) {
64348eea375SJeff Roberson 			vm_object_t obj;
64448eea375SJeff Roberson 
64548eea375SJeff Roberson 			if (flags & UMA_SLAB_KMEM)
64648eea375SJeff Roberson 				obj = kmem_object;
64748eea375SJeff Roberson 			else
64848eea375SJeff Roberson 				obj = NULL;
64999571dc3SJeff Roberson 			for (i = 0; i < zone->uz_ppera; i++)
65099571dc3SJeff Roberson 				vsetobj((vm_offset_t)mem + (i * PAGE_SIZE),
65148eea375SJeff Roberson 				    obj);
65248eea375SJeff Roberson 		}
6538355f576SJeff Roberson #ifdef UMA_DEBUG
6548355f576SJeff Roberson 		printf("%s: Returning %d bytes.\n",
6558355f576SJeff Roberson 		    zone->uz_name, UMA_SLAB_SIZE * zone->uz_ppera);
6568355f576SJeff Roberson #endif
6578355f576SJeff Roberson 		zone->uz_freef(mem, UMA_SLAB_SIZE * zone->uz_ppera, flags);
6588355f576SJeff Roberson 	}
6598355f576SJeff Roberson 
6608355f576SJeff Roberson }
6618355f576SJeff Roberson 
662d56368d7SBosko Milekic static __inline
663d56368d7SBosko Milekic void
664d56368d7SBosko Milekic zone_drain(uma_zone_t zone)
665d56368d7SBosko Milekic {
666d56368d7SBosko Milekic 	zone_drain_common(zone, 0);
667d56368d7SBosko Milekic }
668d56368d7SBosko Milekic 
6698355f576SJeff Roberson /*
6708355f576SJeff Roberson  * Allocate a new slab for a zone.  This does not insert the slab onto a list.
6718355f576SJeff Roberson  *
6728355f576SJeff Roberson  * Arguments:
6738355f576SJeff Roberson  *	zone  The zone to allocate slabs for
6748355f576SJeff Roberson  *	wait  Shall we wait?
6758355f576SJeff Roberson  *
6768355f576SJeff Roberson  * Returns:
6778355f576SJeff Roberson  *	The slab that was allocated or NULL if there is no memory and the
6788355f576SJeff Roberson  *	caller specified M_NOWAIT.
6798355f576SJeff Roberson  *
6808355f576SJeff Roberson  */
6818355f576SJeff Roberson static uma_slab_t
6828355f576SJeff Roberson slab_zalloc(uma_zone_t zone, int wait)
6838355f576SJeff Roberson {
6848355f576SJeff Roberson 	uma_slab_t slab;	/* Starting slab */
6858355f576SJeff Roberson 	u_int8_t *mem;
6868355f576SJeff Roberson 	u_int8_t flags;
6878355f576SJeff Roberson 	int i;
6888355f576SJeff Roberson 
689a553d4b8SJeff Roberson 	slab = NULL;
690a553d4b8SJeff Roberson 
6918355f576SJeff Roberson #ifdef UMA_DEBUG
6928355f576SJeff Roberson 	printf("slab_zalloc:  Allocating a new slab for %s\n", zone->uz_name);
6938355f576SJeff Roberson #endif
6948355f576SJeff Roberson 	ZONE_UNLOCK(zone);
695a553d4b8SJeff Roberson 
696a553d4b8SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_OFFPAGE) {
697bbee39c6SJeff Roberson 		slab = uma_zalloc_internal(slabzone, NULL, wait);
698a553d4b8SJeff Roberson 		if (slab == NULL) {
699a553d4b8SJeff Roberson 			ZONE_LOCK(zone);
700a553d4b8SJeff Roberson 			return NULL;
701a553d4b8SJeff Roberson 		}
702a553d4b8SJeff Roberson 	}
703a553d4b8SJeff Roberson 
7043370c5bfSJeff Roberson 	/*
7053370c5bfSJeff Roberson 	 * This reproduces the old vm_zone behavior of zero filling pages the
7063370c5bfSJeff Roberson 	 * first time they are added to a zone.
7073370c5bfSJeff Roberson 	 *
7083370c5bfSJeff Roberson 	 * Malloced items are zeroed in uma_zalloc.
7093370c5bfSJeff Roberson 	 */
7103370c5bfSJeff Roberson 
7113370c5bfSJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_MALLOC) == 0)
7123370c5bfSJeff Roberson 		wait |= M_ZERO;
7133370c5bfSJeff Roberson 	else
7143370c5bfSJeff Roberson 		wait &= ~M_ZERO;
7153370c5bfSJeff Roberson 
716a553d4b8SJeff Roberson 	if (booted || (zone->uz_flags & UMA_ZFLAG_PRIVALLOC)) {
717234c7726SAlan Cox 		mem = zone->uz_allocf(zone, zone->uz_ppera * UMA_SLAB_SIZE,
718234c7726SAlan Cox 		    &flags, wait);
719a553d4b8SJeff Roberson 		if (mem == NULL) {
7208355f576SJeff Roberson 			ZONE_LOCK(zone);
7218355f576SJeff Roberson 			return (NULL);
722a553d4b8SJeff Roberson 		}
7238355f576SJeff Roberson 	} else {
724a553d4b8SJeff Roberson 		uma_slab_t tmps;
7258355f576SJeff Roberson 
7268355f576SJeff Roberson 		if (zone->uz_ppera > 1)
7278355f576SJeff Roberson 			panic("UMA: Attemping to allocate multiple pages before vm has started.\n");
7288355f576SJeff Roberson 		if (zone->uz_flags & UMA_ZFLAG_MALLOC)
7298355f576SJeff Roberson 			panic("Mallocing before uma_startup2 has been called.\n");
7308355f576SJeff Roberson 		if (uma_boot_free == 0)
7318355f576SJeff Roberson 			panic("UMA: Ran out of pre init pages, increase UMA_BOOT_PAGES\n");
732a553d4b8SJeff Roberson 		tmps = LIST_FIRST(&uma_boot_pages);
733a553d4b8SJeff Roberson 		LIST_REMOVE(tmps, us_link);
7348355f576SJeff Roberson 		uma_boot_free--;
735a553d4b8SJeff Roberson 		mem = tmps->us_data;
736f3da1873SJeff Roberson 		flags = tmps->us_flags;
7378355f576SJeff Roberson 	}
7388355f576SJeff Roberson 
7395c0e403bSJeff Roberson 	/* Point the slab into the allocated memory */
74099571dc3SJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_OFFPAGE))
7418355f576SJeff Roberson 		slab = (uma_slab_t )(mem + zone->uz_pgoff);
7425c0e403bSJeff Roberson 
74399571dc3SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_MALLOC)
74499571dc3SJeff Roberson 		for (i = 0; i < zone->uz_ppera; i++)
74599571dc3SJeff Roberson 			vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
7468355f576SJeff Roberson 
7478355f576SJeff Roberson 	slab->us_zone = zone;
7488355f576SJeff Roberson 	slab->us_data = mem;
7498355f576SJeff Roberson 
7508355f576SJeff Roberson 	/*
7518355f576SJeff Roberson 	 * This is intended to spread data out across cache lines.
7528355f576SJeff Roberson 	 *
7538355f576SJeff Roberson 	 * This code doesn't seem to work properly on x86, and on alpha
7548355f576SJeff Roberson 	 * it makes absolutely no performance difference. I'm sure it could
7558355f576SJeff Roberson 	 * use some tuning, but sun makes outrageous claims about it's
7568355f576SJeff Roberson 	 * performance.
7578355f576SJeff Roberson 	 */
7588355f576SJeff Roberson #if 0
7598355f576SJeff Roberson 	if (zone->uz_cachemax) {
7608355f576SJeff Roberson 		slab->us_data += zone->uz_cacheoff;
7618355f576SJeff Roberson 		zone->uz_cacheoff += UMA_CACHE_INC;
7628355f576SJeff Roberson 		if (zone->uz_cacheoff > zone->uz_cachemax)
7638355f576SJeff Roberson 			zone->uz_cacheoff = 0;
7648355f576SJeff Roberson 	}
7658355f576SJeff Roberson #endif
7668355f576SJeff Roberson 
7678355f576SJeff Roberson 	slab->us_freecount = zone->uz_ipers;
7688355f576SJeff Roberson 	slab->us_firstfree = 0;
7698355f576SJeff Roberson 	slab->us_flags = flags;
7708355f576SJeff Roberson 	for (i = 0; i < zone->uz_ipers; i++)
7718355f576SJeff Roberson 		slab->us_freelist[i] = i+1;
7728355f576SJeff Roberson 
7738355f576SJeff Roberson 	if (zone->uz_init)
7748355f576SJeff Roberson 		for (i = 0; i < zone->uz_ipers; i++)
7758355f576SJeff Roberson 			zone->uz_init(slab->us_data + (zone->uz_rsize * i),
7768355f576SJeff Roberson 			    zone->uz_size);
7775c0e403bSJeff Roberson 	ZONE_LOCK(zone);
7785c0e403bSJeff Roberson 
77999571dc3SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_HASH)
7805c0e403bSJeff Roberson 		UMA_HASH_INSERT(&zone->uz_hash, slab, mem);
7818355f576SJeff Roberson 
7828355f576SJeff Roberson 	zone->uz_pages += zone->uz_ppera;
7838355f576SJeff Roberson 	zone->uz_free += zone->uz_ipers;
7848355f576SJeff Roberson 
7855c0e403bSJeff Roberson 
7868355f576SJeff Roberson 	return (slab);
7878355f576SJeff Roberson }
7888355f576SJeff Roberson 
7898355f576SJeff Roberson /*
7908355f576SJeff Roberson  * Allocates a number of pages from the system
7918355f576SJeff Roberson  *
7928355f576SJeff Roberson  * Arguments:
7938355f576SJeff Roberson  *	zone  Unused
7948355f576SJeff Roberson  *	bytes  The number of bytes requested
7958355f576SJeff Roberson  *	wait  Shall we wait?
7968355f576SJeff Roberson  *
7978355f576SJeff Roberson  * Returns:
7988355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
7998355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
8008355f576SJeff Roberson  */
8018355f576SJeff Roberson static void *
8028355f576SJeff Roberson page_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
8038355f576SJeff Roberson {
8048355f576SJeff Roberson 	void *p;	/* Returned page */
8058355f576SJeff Roberson 
8068355f576SJeff Roberson 	*pflag = UMA_SLAB_KMEM;
8078355f576SJeff Roberson 	p = (void *) kmem_malloc(kmem_map, bytes, wait);
8088355f576SJeff Roberson 
8098355f576SJeff Roberson 	return (p);
8108355f576SJeff Roberson }
8118355f576SJeff Roberson 
8128355f576SJeff Roberson /*
8138355f576SJeff Roberson  * Allocates a number of pages from within an object
8148355f576SJeff Roberson  *
8158355f576SJeff Roberson  * Arguments:
8168355f576SJeff Roberson  *	zone   Unused
8178355f576SJeff Roberson  *	bytes  The number of bytes requested
8188355f576SJeff Roberson  *	wait   Shall we wait?
8198355f576SJeff Roberson  *
8208355f576SJeff Roberson  * Returns:
8218355f576SJeff Roberson  *	A pointer to the alloced memory or possibly
8228355f576SJeff Roberson  *	NULL if M_NOWAIT is set.
823494273beSJeff Roberson  *
8248355f576SJeff Roberson  */
8258355f576SJeff Roberson static void *
8268355f576SJeff Roberson obj_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
8278355f576SJeff Roberson {
828b245ac95SAlan Cox 	vm_object_t object;
829b245ac95SAlan Cox 	vm_offset_t retkva, zkva;
8308355f576SJeff Roberson 	vm_page_t p;
831b245ac95SAlan Cox 	int pages, startpages;
8328355f576SJeff Roberson 
833b245ac95SAlan Cox 	object = zone->uz_obj;
83455f7c614SArchie Cobbs 	retkva = 0;
8358355f576SJeff Roberson 
8368355f576SJeff Roberson 	/*
8378355f576SJeff Roberson 	 * This looks a little weird since we're getting one page at a time
8388355f576SJeff Roberson 	 */
839b245ac95SAlan Cox 	VM_OBJECT_LOCK(object);
840b245ac95SAlan Cox 	p = TAILQ_LAST(&object->memq, pglist);
841b245ac95SAlan Cox 	pages = p != NULL ? p->pindex + 1 : 0;
842b245ac95SAlan Cox 	startpages = pages;
8438355f576SJeff Roberson 	zkva = zone->uz_kva + pages * PAGE_SIZE;
844b245ac95SAlan Cox 	for (; bytes > 0; bytes -= PAGE_SIZE) {
845b245ac95SAlan Cox 		p = vm_page_alloc(object, pages,
846b245ac95SAlan Cox 		    VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED);
847b245ac95SAlan Cox 		if (p == NULL) {
848b245ac95SAlan Cox 			if (pages != startpages)
849b245ac95SAlan Cox 				pmap_qremove(retkva, pages - startpages);
850b245ac95SAlan Cox 			while (pages != startpages) {
851b245ac95SAlan Cox 				pages--;
852b245ac95SAlan Cox 				p = TAILQ_LAST(&object->memq, pglist);
853b245ac95SAlan Cox 				vm_page_lock_queues();
854b245ac95SAlan Cox 				vm_page_unwire(p, 0);
855b245ac95SAlan Cox 				vm_page_free(p);
856b245ac95SAlan Cox 				vm_page_unlock_queues();
857b245ac95SAlan Cox 			}
858b245ac95SAlan Cox 			retkva = 0;
859b245ac95SAlan Cox 			goto done;
860b245ac95SAlan Cox 		}
861b245ac95SAlan Cox 		pmap_qenter(zkva, &p, 1);
86255f7c614SArchie Cobbs 		if (retkva == 0)
8638355f576SJeff Roberson 			retkva = zkva;
864b245ac95SAlan Cox 		zkva += PAGE_SIZE;
8658355f576SJeff Roberson 		pages += 1;
8668355f576SJeff Roberson 	}
867b245ac95SAlan Cox done:
868b245ac95SAlan Cox 	VM_OBJECT_UNLOCK(object);
8698355f576SJeff Roberson 
8708355f576SJeff Roberson 	*flags = UMA_SLAB_PRIV;
8718355f576SJeff Roberson 
8728355f576SJeff Roberson 	return ((void *)retkva);
8738355f576SJeff Roberson }
8748355f576SJeff Roberson 
8758355f576SJeff Roberson /*
8768355f576SJeff Roberson  * Frees a number of pages to the system
8778355f576SJeff Roberson  *
8788355f576SJeff Roberson  * Arguments:
8798355f576SJeff Roberson  *	mem   A pointer to the memory to be freed
8808355f576SJeff Roberson  *	size  The size of the memory being freed
8818355f576SJeff Roberson  *	flags The original p->us_flags field
8828355f576SJeff Roberson  *
8838355f576SJeff Roberson  * Returns:
8848355f576SJeff Roberson  *	Nothing
8858355f576SJeff Roberson  *
8868355f576SJeff Roberson  */
8878355f576SJeff Roberson static void
8888355f576SJeff Roberson page_free(void *mem, int size, u_int8_t flags)
8898355f576SJeff Roberson {
8908355f576SJeff Roberson 	vm_map_t map;
8913370c5bfSJeff Roberson 
8928355f576SJeff Roberson 	if (flags & UMA_SLAB_KMEM)
8938355f576SJeff Roberson 		map = kmem_map;
8948355f576SJeff Roberson 	else
8958355f576SJeff Roberson 		panic("UMA: page_free used with invalid flags %d\n", flags);
8968355f576SJeff Roberson 
8978355f576SJeff Roberson 	kmem_free(map, (vm_offset_t)mem, size);
8988355f576SJeff Roberson }
8998355f576SJeff Roberson 
9008355f576SJeff Roberson /*
9018355f576SJeff Roberson  * Zero fill initializer
9028355f576SJeff Roberson  *
9038355f576SJeff Roberson  * Arguments/Returns follow uma_init specifications
9048355f576SJeff Roberson  *
9058355f576SJeff Roberson  */
9068355f576SJeff Roberson static void
9078355f576SJeff Roberson zero_init(void *mem, int size)
9088355f576SJeff Roberson {
9098355f576SJeff Roberson 	bzero(mem, size);
9108355f576SJeff Roberson }
9118355f576SJeff Roberson 
9128355f576SJeff Roberson /*
9138355f576SJeff Roberson  * Finish creating a small uma zone.  This calculates ipers, and the zone size.
9148355f576SJeff Roberson  *
9158355f576SJeff Roberson  * Arguments
9168355f576SJeff Roberson  *	zone  The zone we should initialize
9178355f576SJeff Roberson  *
9188355f576SJeff Roberson  * Returns
9198355f576SJeff Roberson  *	Nothing
9208355f576SJeff Roberson  */
9218355f576SJeff Roberson static void
9228355f576SJeff Roberson zone_small_init(uma_zone_t zone)
9238355f576SJeff Roberson {
9248355f576SJeff Roberson 	int rsize;
9258355f576SJeff Roberson 	int memused;
9268355f576SJeff Roberson 	int ipers;
9278355f576SJeff Roberson 
9288355f576SJeff Roberson 	rsize = zone->uz_size;
9298355f576SJeff Roberson 
9308355f576SJeff Roberson 	if (rsize < UMA_SMALLEST_UNIT)
9318355f576SJeff Roberson 		rsize = UMA_SMALLEST_UNIT;
9328355f576SJeff Roberson 
9338355f576SJeff Roberson 	if (rsize & zone->uz_align)
9348355f576SJeff Roberson 		rsize = (rsize & ~zone->uz_align) + (zone->uz_align + 1);
9358355f576SJeff Roberson 
9368355f576SJeff Roberson 	zone->uz_rsize = rsize;
9378355f576SJeff Roberson 
9388355f576SJeff Roberson 	rsize += 1;	/* Account for the byte of linkage */
9398355f576SJeff Roberson 	zone->uz_ipers = (UMA_SLAB_SIZE - sizeof(struct uma_slab)) / rsize;
9408355f576SJeff Roberson 	zone->uz_ppera = 1;
9418355f576SJeff Roberson 
9428355f576SJeff Roberson 	memused = zone->uz_ipers * zone->uz_rsize;
9438355f576SJeff Roberson 
9448355f576SJeff Roberson 	/* Can we do any better? */
9458355f576SJeff Roberson 	if ((UMA_SLAB_SIZE - memused) >= UMA_MAX_WASTE) {
9468355f576SJeff Roberson 		if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
9478355f576SJeff Roberson 			return;
9488355f576SJeff Roberson 		ipers = UMA_SLAB_SIZE / zone->uz_rsize;
9498355f576SJeff Roberson 		if (ipers > zone->uz_ipers) {
9508355f576SJeff Roberson 			zone->uz_flags |= UMA_ZFLAG_OFFPAGE;
95199571dc3SJeff Roberson 			if ((zone->uz_flags & UMA_ZFLAG_MALLOC) == 0)
95299571dc3SJeff Roberson 				zone->uz_flags |= UMA_ZFLAG_HASH;
9538355f576SJeff Roberson 			zone->uz_ipers = ipers;
9548355f576SJeff Roberson 		}
9558355f576SJeff Roberson 	}
9568355f576SJeff Roberson 
9578355f576SJeff Roberson }
9588355f576SJeff Roberson 
9598355f576SJeff Roberson /*
9608355f576SJeff Roberson  * Finish creating a large (> UMA_SLAB_SIZE) uma zone.  Just give in and do
9618355f576SJeff Roberson  * OFFPAGE for now.  When I can allow for more dynamic slab sizes this will be
9628355f576SJeff Roberson  * more complicated.
9638355f576SJeff Roberson  *
9648355f576SJeff Roberson  * Arguments
9658355f576SJeff Roberson  *	zone  The zone we should initialize
9668355f576SJeff Roberson  *
9678355f576SJeff Roberson  * Returns
9688355f576SJeff Roberson  *	Nothing
9698355f576SJeff Roberson  */
9708355f576SJeff Roberson static void
9718355f576SJeff Roberson zone_large_init(uma_zone_t zone)
9728355f576SJeff Roberson {
9738355f576SJeff Roberson 	int pages;
9748355f576SJeff Roberson 
9758355f576SJeff Roberson 	pages = zone->uz_size / UMA_SLAB_SIZE;
9768355f576SJeff Roberson 
9778355f576SJeff Roberson 	/* Account for remainder */
9788355f576SJeff Roberson 	if ((pages * UMA_SLAB_SIZE) < zone->uz_size)
9798355f576SJeff Roberson 		pages++;
9808355f576SJeff Roberson 
9818355f576SJeff Roberson 	zone->uz_ppera = pages;
9828355f576SJeff Roberson 	zone->uz_ipers = 1;
9838355f576SJeff Roberson 
9848355f576SJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_OFFPAGE;
98599571dc3SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_MALLOC) == 0)
98699571dc3SJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_HASH;
98799571dc3SJeff Roberson 
9888355f576SJeff Roberson 	zone->uz_rsize = zone->uz_size;
9898355f576SJeff Roberson }
9908355f576SJeff Roberson 
9918355f576SJeff Roberson /*
9928355f576SJeff Roberson  * Zone header ctor.  This initializes all fields, locks, etc.  And inserts
9938355f576SJeff Roberson  * the zone onto the global zone list.
9948355f576SJeff Roberson  *
9958355f576SJeff Roberson  * Arguments/Returns follow uma_ctor specifications
9968355f576SJeff Roberson  *	udata  Actually uma_zcreat_args
9978355f576SJeff Roberson  *
9988355f576SJeff Roberson  */
9998355f576SJeff Roberson 
10008355f576SJeff Roberson static void
10018355f576SJeff Roberson zone_ctor(void *mem, int size, void *udata)
10028355f576SJeff Roberson {
10038355f576SJeff Roberson 	struct uma_zctor_args *arg = udata;
10048355f576SJeff Roberson 	uma_zone_t zone = mem;
100528bc4419SJeff Roberson 	int privlc;
10068355f576SJeff Roberson 
10078355f576SJeff Roberson 	bzero(zone, size);
10088355f576SJeff Roberson 	zone->uz_name = arg->name;
10098355f576SJeff Roberson 	zone->uz_size = arg->size;
10108355f576SJeff Roberson 	zone->uz_ctor = arg->ctor;
10118355f576SJeff Roberson 	zone->uz_dtor = arg->dtor;
10128355f576SJeff Roberson 	zone->uz_init = arg->uminit;
1013e221e841SJeff Roberson 	zone->uz_fini = arg->fini;
10148355f576SJeff Roberson 	zone->uz_align = arg->align;
10158355f576SJeff Roberson 	zone->uz_free = 0;
10168355f576SJeff Roberson 	zone->uz_pages = 0;
10178355f576SJeff Roberson 	zone->uz_flags = 0;
10188355f576SJeff Roberson 	zone->uz_allocf = page_alloc;
10198355f576SJeff Roberson 	zone->uz_freef = page_free;
10208355f576SJeff Roberson 
10218355f576SJeff Roberson 	if (arg->flags & UMA_ZONE_ZINIT)
10228355f576SJeff Roberson 		zone->uz_init = zero_init;
10238355f576SJeff Roberson 
10248355f576SJeff Roberson 	if (arg->flags & UMA_ZONE_INTERNAL)
10258355f576SJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_INTERNAL;
10268355f576SJeff Roberson 
10278355f576SJeff Roberson 	if (arg->flags & UMA_ZONE_MALLOC)
10288355f576SJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_MALLOC;
10298355f576SJeff Roberson 
10308355f576SJeff Roberson 	if (arg->flags & UMA_ZONE_NOFREE)
10318355f576SJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_NOFREE;
10328355f576SJeff Roberson 
103318aa2de5SJeff Roberson 	if (arg->flags & UMA_ZONE_VM)
103418aa2de5SJeff Roberson 		zone->uz_flags |= UMA_ZFLAG_BUCKETCACHE;
103518aa2de5SJeff Roberson 
10368355f576SJeff Roberson 	if (zone->uz_size > UMA_SLAB_SIZE)
10378355f576SJeff Roberson 		zone_large_init(zone);
10388355f576SJeff Roberson 	else
10398355f576SJeff Roberson 		zone_small_init(zone);
104048eea375SJeff Roberson #ifdef UMA_MD_SMALL_ALLOC
104148eea375SJeff Roberson 	if (zone->uz_ppera == 1) {
104248eea375SJeff Roberson 		zone->uz_allocf = uma_small_alloc;
104348eea375SJeff Roberson 		zone->uz_freef = uma_small_free;
104448eea375SJeff Roberson 	}
104548eea375SJeff Roberson #endif	/* UMA_MD_SMALL_ALLOC */
10468355f576SJeff Roberson 
104728bc4419SJeff Roberson 	if (arg->flags & UMA_ZONE_MTXCLASS)
104828bc4419SJeff Roberson 		privlc = 1;
104928bc4419SJeff Roberson 	else
105028bc4419SJeff Roberson 		privlc = 0;
105128bc4419SJeff Roberson 
10528355f576SJeff Roberson 	/*
10538355f576SJeff Roberson 	 * If we're putting the slab header in the actual page we need to
10548355f576SJeff Roberson 	 * figure out where in each page it goes.  This calculates a right
10559d5abbddSJens Schweikhardt 	 * justified offset into the memory on an ALIGN_PTR boundary.
10568355f576SJeff Roberson 	 */
10578355f576SJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_OFFPAGE)) {
10588355f576SJeff Roberson 		int totsize;
10598355f576SJeff Roberson 		int waste;
10608355f576SJeff Roberson 
10618355f576SJeff Roberson 		/* Size of the slab struct and free list */
10628355f576SJeff Roberson 		totsize = sizeof(struct uma_slab) + zone->uz_ipers;
10638355f576SJeff Roberson 		if (totsize & UMA_ALIGN_PTR)
10648355f576SJeff Roberson 			totsize = (totsize & ~UMA_ALIGN_PTR) +
10658355f576SJeff Roberson 			    (UMA_ALIGN_PTR + 1);
10668355f576SJeff Roberson 		zone->uz_pgoff = UMA_SLAB_SIZE - totsize;
10678355f576SJeff Roberson 
10688355f576SJeff Roberson 		waste = zone->uz_pgoff;
10698355f576SJeff Roberson 		waste -= (zone->uz_ipers * zone->uz_rsize);
10708355f576SJeff Roberson 
10718355f576SJeff Roberson 		/*
10728355f576SJeff Roberson 		 * This calculates how much space we have for cache line size
10738355f576SJeff Roberson 		 * optimizations.  It works by offseting each slab slightly.
10748355f576SJeff Roberson 		 * Currently it breaks on x86, and so it is disabled.
10758355f576SJeff Roberson 		 */
10768355f576SJeff Roberson 
10778355f576SJeff Roberson 		if (zone->uz_align < UMA_CACHE_INC && waste > UMA_CACHE_INC) {
10788355f576SJeff Roberson 			zone->uz_cachemax = waste - UMA_CACHE_INC;
10798355f576SJeff Roberson 			zone->uz_cacheoff = 0;
10808355f576SJeff Roberson 		}
10818355f576SJeff Roberson 
10828355f576SJeff Roberson 		totsize = zone->uz_pgoff + sizeof(struct uma_slab)
10838355f576SJeff Roberson 		    + zone->uz_ipers;
10848355f576SJeff Roberson 		/* I don't think it's possible, but I'll make sure anyway */
10858355f576SJeff Roberson 		if (totsize > UMA_SLAB_SIZE) {
10868355f576SJeff Roberson 			printf("zone %s ipers %d rsize %d size %d\n",
10878355f576SJeff Roberson 			    zone->uz_name, zone->uz_ipers, zone->uz_rsize,
10888355f576SJeff Roberson 			    zone->uz_size);
10898355f576SJeff Roberson 			panic("UMA slab won't fit.\n");
10908355f576SJeff Roberson 		}
10918355f576SJeff Roberson 	}
10928355f576SJeff Roberson 
109399571dc3SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_HASH)
109499571dc3SJeff Roberson 		hash_alloc(&zone->uz_hash);
109599571dc3SJeff Roberson 
10968355f576SJeff Roberson #ifdef UMA_DEBUG
10978355f576SJeff Roberson 	printf("%s(%p) size = %d ipers = %d ppera = %d pgoff = %d\n",
10988355f576SJeff Roberson 	    zone->uz_name, zone,
10998355f576SJeff Roberson 	    zone->uz_size, zone->uz_ipers,
11008355f576SJeff Roberson 	    zone->uz_ppera, zone->uz_pgoff);
11018355f576SJeff Roberson #endif
110228bc4419SJeff Roberson 	ZONE_LOCK_INIT(zone, privlc);
11038355f576SJeff Roberson 
11048355f576SJeff Roberson 	mtx_lock(&uma_mtx);
11058355f576SJeff Roberson 	LIST_INSERT_HEAD(&uma_zones, zone, uz_link);
11068355f576SJeff Roberson 	mtx_unlock(&uma_mtx);
11078355f576SJeff Roberson 
11088355f576SJeff Roberson 	/*
11098355f576SJeff Roberson 	 * Some internal zones don't have room allocated for the per cpu
11108355f576SJeff Roberson 	 * caches.  If we're internal, bail out here.
11118355f576SJeff Roberson 	 */
11128355f576SJeff Roberson 
11138355f576SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
11148355f576SJeff Roberson 		return;
11158355f576SJeff Roberson 
11168355f576SJeff Roberson 	if (zone->uz_ipers < UMA_BUCKET_SIZE)
1117a553d4b8SJeff Roberson 		zone->uz_count = zone->uz_ipers - 1;
11188355f576SJeff Roberson 	else
1119a553d4b8SJeff Roberson 		zone->uz_count = UMA_BUCKET_SIZE - 1;
11208355f576SJeff Roberson }
11218355f576SJeff Roberson 
11228355f576SJeff Roberson /*
11239c2cd7e5SJeff Roberson  * Zone header dtor.  This frees all data, destroys locks, frees the hash table
11249c2cd7e5SJeff Roberson  * and removes the zone from the global list.
11259c2cd7e5SJeff Roberson  *
11269c2cd7e5SJeff Roberson  * Arguments/Returns follow uma_dtor specifications
11279c2cd7e5SJeff Roberson  *	udata  unused
11289c2cd7e5SJeff Roberson  */
11299c2cd7e5SJeff Roberson 
11309c2cd7e5SJeff Roberson static void
11319c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata)
11329c2cd7e5SJeff Roberson {
11339c2cd7e5SJeff Roberson 	uma_zone_t zone;
11349c2cd7e5SJeff Roberson 
11359c2cd7e5SJeff Roberson 	zone = (uma_zone_t)arg;
113617b9cc49SJeff Roberson 	mtx_lock(&uma_mtx);
113717b9cc49SJeff Roberson 	LIST_REMOVE(zone, uz_link);
1138d56368d7SBosko Milekic 	zone_drain_common(zone, 1);
113917b9cc49SJeff Roberson 	mtx_unlock(&uma_mtx);
114017b9cc49SJeff Roberson 
11419c2cd7e5SJeff Roberson 	ZONE_LOCK(zone);
11429c2cd7e5SJeff Roberson 	if (zone->uz_free != 0)
1143886eaaacSPoul-Henning Kamp 		printf("Zone %s was not empty (%d items).  Lost %d pages of memory.\n",
1144886eaaacSPoul-Henning Kamp 		    zone->uz_name, zone->uz_free, zone->uz_pages);
11459c2cd7e5SJeff Roberson 
11469c2cd7e5SJeff Roberson 	ZONE_UNLOCK(zone);
11470aef6126SJeff Roberson 	if ((zone->uz_flags & UMA_ZFLAG_OFFPAGE) != 0)
11480aef6126SJeff Roberson 		hash_free(&zone->uz_hash);
11490aef6126SJeff Roberson 
11509c2cd7e5SJeff Roberson 	ZONE_LOCK_FINI(zone);
11519c2cd7e5SJeff Roberson }
11529c2cd7e5SJeff Roberson /*
11538355f576SJeff Roberson  * Traverses every zone in the system and calls a callback
11548355f576SJeff Roberson  *
11558355f576SJeff Roberson  * Arguments:
11568355f576SJeff Roberson  *	zfunc  A pointer to a function which accepts a zone
11578355f576SJeff Roberson  *		as an argument.
11588355f576SJeff Roberson  *
11598355f576SJeff Roberson  * Returns:
11608355f576SJeff Roberson  *	Nothing
11618355f576SJeff Roberson  */
11628355f576SJeff Roberson static void
11638355f576SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t))
11648355f576SJeff Roberson {
11658355f576SJeff Roberson 	uma_zone_t zone;
11668355f576SJeff Roberson 
11678355f576SJeff Roberson 	mtx_lock(&uma_mtx);
11688355f576SJeff Roberson 	LIST_FOREACH(zone, &uma_zones, uz_link) {
11698355f576SJeff Roberson 		zfunc(zone);
11708355f576SJeff Roberson 	}
11718355f576SJeff Roberson 	mtx_unlock(&uma_mtx);
11728355f576SJeff Roberson }
11738355f576SJeff Roberson 
11748355f576SJeff Roberson /* Public functions */
11758355f576SJeff Roberson /* See uma.h */
11768355f576SJeff Roberson void
11778355f576SJeff Roberson uma_startup(void *bootmem)
11788355f576SJeff Roberson {
11798355f576SJeff Roberson 	struct uma_zctor_args args;
11808355f576SJeff Roberson 	uma_slab_t slab;
11818355f576SJeff Roberson 	int slabsize;
11828355f576SJeff Roberson 	int i;
11838355f576SJeff Roberson 
11848355f576SJeff Roberson #ifdef UMA_DEBUG
11858355f576SJeff Roberson 	printf("Creating uma zone headers zone.\n");
11868355f576SJeff Roberson #endif
11878355f576SJeff Roberson #ifdef SMP
11888355f576SJeff Roberson 	maxcpu = mp_maxid + 1;
11898355f576SJeff Roberson #else
11908355f576SJeff Roberson 	maxcpu = 1;
11918355f576SJeff Roberson #endif
11928355f576SJeff Roberson #ifdef UMA_DEBUG
11938355f576SJeff Roberson 	printf("Max cpu = %d, mp_maxid = %d\n", maxcpu, mp_maxid);
11948355f576SJeff Roberson 	Debugger("stop");
11958355f576SJeff Roberson #endif
11966008862bSJohn Baldwin 	mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
11978355f576SJeff Roberson 	/* "manually" Create the initial zone */
11988355f576SJeff Roberson 	args.name = "UMA Zones";
11998355f576SJeff Roberson 	args.size = sizeof(struct uma_zone) +
12008355f576SJeff Roberson 	    (sizeof(struct uma_cache) * (maxcpu - 1));
12018355f576SJeff Roberson 	args.ctor = zone_ctor;
12029c2cd7e5SJeff Roberson 	args.dtor = zone_dtor;
12038355f576SJeff Roberson 	args.uminit = zero_init;
12048355f576SJeff Roberson 	args.fini = NULL;
12058355f576SJeff Roberson 	args.align = 32 - 1;
12068355f576SJeff Roberson 	args.flags = UMA_ZONE_INTERNAL;
12078355f576SJeff Roberson 	/* The initial zone has no Per cpu queues so it's smaller */
12088355f576SJeff Roberson 	zone_ctor(zones, sizeof(struct uma_zone), &args);
12098355f576SJeff Roberson 
1210d88797c2SBosko Milekic 	/* Initialize the pcpu cache lock set once and for all */
1211d88797c2SBosko Milekic 	for (i = 0; i < maxcpu; i++)
1212d88797c2SBosko Milekic 		CPU_LOCK_INIT(i);
1213d88797c2SBosko Milekic 
12148355f576SJeff Roberson #ifdef UMA_DEBUG
12158355f576SJeff Roberson 	printf("Filling boot free list.\n");
12168355f576SJeff Roberson #endif
12178355f576SJeff Roberson 	for (i = 0; i < UMA_BOOT_PAGES; i++) {
12188355f576SJeff Roberson 		slab = (uma_slab_t)((u_int8_t *)bootmem + (i * UMA_SLAB_SIZE));
12198355f576SJeff Roberson 		slab->us_data = (u_int8_t *)slab;
12208355f576SJeff Roberson 		slab->us_flags = UMA_SLAB_BOOT;
12218355f576SJeff Roberson 		LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
12228355f576SJeff Roberson 		uma_boot_free++;
12238355f576SJeff Roberson 	}
12248355f576SJeff Roberson 
12258355f576SJeff Roberson #ifdef UMA_DEBUG
12268355f576SJeff Roberson 	printf("Creating slab zone.\n");
12278355f576SJeff Roberson #endif
12288355f576SJeff Roberson 
12298355f576SJeff Roberson 	/*
12308355f576SJeff Roberson 	 * This is the max number of free list items we'll have with
12318355f576SJeff Roberson 	 * offpage slabs.
12328355f576SJeff Roberson 	 */
12338355f576SJeff Roberson 
12348355f576SJeff Roberson 	slabsize = UMA_SLAB_SIZE - sizeof(struct uma_slab);
12358355f576SJeff Roberson 	slabsize /= UMA_MAX_WASTE;
12368355f576SJeff Roberson 	slabsize++;			/* In case there it's rounded */
12378355f576SJeff Roberson 	slabsize += sizeof(struct uma_slab);
12388355f576SJeff Roberson 
12398355f576SJeff Roberson 	/* Now make a zone for slab headers */
12408355f576SJeff Roberson 	slabzone = uma_zcreate("UMA Slabs",
12418355f576SJeff Roberson 				slabsize,
12428355f576SJeff Roberson 				NULL, NULL, NULL, NULL,
12438355f576SJeff Roberson 				UMA_ALIGN_PTR, UMA_ZONE_INTERNAL);
12448355f576SJeff Roberson 
12458355f576SJeff Roberson 	hashzone = uma_zcreate("UMA Hash",
12468355f576SJeff Roberson 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
12478355f576SJeff Roberson 	    NULL, NULL, NULL, NULL,
12488355f576SJeff Roberson 	    UMA_ALIGN_PTR, UMA_ZONE_INTERNAL);
12498355f576SJeff Roberson 
12508355f576SJeff Roberson 	bucketzone = uma_zcreate("UMA Buckets", sizeof(struct uma_bucket),
12518355f576SJeff Roberson 	    NULL, NULL, NULL, NULL,
12528355f576SJeff Roberson 	    UMA_ALIGN_PTR, UMA_ZONE_INTERNAL);
12538355f576SJeff Roberson 
125448eea375SJeff Roberson #ifdef UMA_MD_SMALL_ALLOC
125548eea375SJeff Roberson 	booted = 1;
125648eea375SJeff Roberson #endif
12578355f576SJeff Roberson 
12588355f576SJeff Roberson #ifdef UMA_DEBUG
12598355f576SJeff Roberson 	printf("UMA startup complete.\n");
12608355f576SJeff Roberson #endif
12618355f576SJeff Roberson }
12628355f576SJeff Roberson 
12638355f576SJeff Roberson /* see uma.h */
12648355f576SJeff Roberson void
126599571dc3SJeff Roberson uma_startup2(void)
12668355f576SJeff Roberson {
12678355f576SJeff Roberson 	booted = 1;
126886bbae32SJeff Roberson 	bucket_enable();
12698355f576SJeff Roberson #ifdef UMA_DEBUG
12708355f576SJeff Roberson 	printf("UMA startup2 complete.\n");
12718355f576SJeff Roberson #endif
12728355f576SJeff Roberson }
12738355f576SJeff Roberson 
12748355f576SJeff Roberson /*
12758355f576SJeff Roberson  * Initialize our callout handle
12768355f576SJeff Roberson  *
12778355f576SJeff Roberson  */
12788355f576SJeff Roberson 
12798355f576SJeff Roberson static void
12808355f576SJeff Roberson uma_startup3(void)
12818355f576SJeff Roberson {
12828355f576SJeff Roberson #ifdef UMA_DEBUG
12838355f576SJeff Roberson 	printf("Starting callout.\n");
12848355f576SJeff Roberson #endif
12858355f576SJeff Roberson 	callout_init(&uma_callout, 0);
12868355f576SJeff Roberson 	callout_reset(&uma_callout, UMA_WORKING_TIME * hz, uma_timeout, NULL);
12878355f576SJeff Roberson #ifdef UMA_DEBUG
12888355f576SJeff Roberson 	printf("UMA startup3 complete.\n");
12898355f576SJeff Roberson #endif
12908355f576SJeff Roberson }
12918355f576SJeff Roberson 
12928355f576SJeff Roberson /* See uma.h */
12938355f576SJeff Roberson uma_zone_t
1294c3bdc05fSAndrew R. Reiter uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1295c3bdc05fSAndrew R. Reiter 		uma_init uminit, uma_fini fini, int align, u_int16_t flags)
12968355f576SJeff Roberson 
12978355f576SJeff Roberson {
12988355f576SJeff Roberson 	struct uma_zctor_args args;
12998355f576SJeff Roberson 
13008355f576SJeff Roberson 	/* This stuff is essential for the zone ctor */
13018355f576SJeff Roberson 	args.name = name;
13028355f576SJeff Roberson 	args.size = size;
13038355f576SJeff Roberson 	args.ctor = ctor;
13048355f576SJeff Roberson 	args.dtor = dtor;
13058355f576SJeff Roberson 	args.uminit = uminit;
13068355f576SJeff Roberson 	args.fini = fini;
13078355f576SJeff Roberson 	args.align = align;
13088355f576SJeff Roberson 	args.flags = flags;
13098355f576SJeff Roberson 
1310a163d034SWarner Losh 	return (uma_zalloc_internal(zones, &args, M_WAITOK));
13118355f576SJeff Roberson }
13128355f576SJeff Roberson 
13138355f576SJeff Roberson /* See uma.h */
13149c2cd7e5SJeff Roberson void
13159c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone)
13169c2cd7e5SJeff Roberson {
13179c2cd7e5SJeff Roberson 	uma_zfree_internal(zones, zone, NULL, 0);
13189c2cd7e5SJeff Roberson }
13199c2cd7e5SJeff Roberson 
13209c2cd7e5SJeff Roberson /* See uma.h */
13218355f576SJeff Roberson void *
13222cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
13238355f576SJeff Roberson {
13248355f576SJeff Roberson 	void *item;
13258355f576SJeff Roberson 	uma_cache_t cache;
13268355f576SJeff Roberson 	uma_bucket_t bucket;
13278355f576SJeff Roberson 	int cpu;
13288355f576SJeff Roberson 
13298355f576SJeff Roberson 	/* This is the fast path allocation */
13308355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1
13318355f576SJeff Roberson 	printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
13328355f576SJeff Roberson #endif
1333a553d4b8SJeff Roberson 
13348522511bSHartmut Brandt #ifdef INVARIANTS
13358522511bSHartmut Brandt 	/*
13368522511bSHartmut Brandt 	 * To make sure that WAITOK or NOWAIT is set, but not more than
13378522511bSHartmut Brandt 	 * one, and check against the API botches that are common.
13388522511bSHartmut Brandt 	 * The uma code implies M_WAITOK if M_NOWAIT is not set, so
13398522511bSHartmut Brandt 	 * we default to waiting if none of the flags is set.
13408522511bSHartmut Brandt 	 */
13418522511bSHartmut Brandt 	cpu = flags & (M_WAITOK | M_NOWAIT | M_DONTWAIT | M_TRYWAIT);
13428522511bSHartmut Brandt 	if (cpu != M_NOWAIT && cpu != M_WAITOK) {
13438522511bSHartmut Brandt 		static	struct timeval lasterr;
13448522511bSHartmut Brandt 		static	int curerr, once;
13458522511bSHartmut Brandt 		if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
13468522511bSHartmut Brandt 			printf("Bad uma_zalloc flags: %x\n", cpu);
13478522511bSHartmut Brandt 			backtrace();
13488522511bSHartmut Brandt 			once++;
13498522511bSHartmut Brandt 		}
13508522511bSHartmut Brandt 	}
13518522511bSHartmut Brandt #endif
13528522511bSHartmut Brandt 
13534c1cc01cSJohn Baldwin 	if (!(flags & M_NOWAIT)) {
13544c1cc01cSJohn Baldwin 		KASSERT(curthread->td_intr_nesting_level == 0,
1355a163d034SWarner Losh 		   ("malloc(M_WAITOK) in interrupt context"));
135626306795SJohn Baldwin 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
135726306795SJohn Baldwin 		    "malloc() of \"%s\"", zone->uz_name);
13584c1cc01cSJohn Baldwin 	}
13594c1cc01cSJohn Baldwin 
1360a553d4b8SJeff Roberson zalloc_restart:
13618355f576SJeff Roberson 	cpu = PCPU_GET(cpuid);
1362d88797c2SBosko Milekic 	CPU_LOCK(cpu);
13638355f576SJeff Roberson 	cache = &zone->uz_cpu[cpu];
13648355f576SJeff Roberson 
13658355f576SJeff Roberson zalloc_start:
13668355f576SJeff Roberson 	bucket = cache->uc_allocbucket;
13678355f576SJeff Roberson 
13688355f576SJeff Roberson 	if (bucket) {
13698355f576SJeff Roberson 		if (bucket->ub_ptr > -1) {
13708355f576SJeff Roberson 			item = bucket->ub_bucket[bucket->ub_ptr];
13718355f576SJeff Roberson #ifdef INVARIANTS
13728355f576SJeff Roberson 			bucket->ub_bucket[bucket->ub_ptr] = NULL;
13738355f576SJeff Roberson #endif
13748355f576SJeff Roberson 			bucket->ub_ptr--;
13758355f576SJeff Roberson 			KASSERT(item != NULL,
13768355f576SJeff Roberson 			    ("uma_zalloc: Bucket pointer mangled."));
13778355f576SJeff Roberson 			cache->uc_allocs++;
1378639c9550SJeff Roberson #ifdef INVARIANTS
137981f71edaSMatt Jacob 			ZONE_LOCK(zone);
1380639c9550SJeff Roberson 			uma_dbg_alloc(zone, NULL, item);
138181f71edaSMatt Jacob 			ZONE_UNLOCK(zone);
1382639c9550SJeff Roberson #endif
1383d88797c2SBosko Milekic 			CPU_UNLOCK(cpu);
13848355f576SJeff Roberson 			if (zone->uz_ctor)
13858355f576SJeff Roberson 				zone->uz_ctor(item, zone->uz_size, udata);
13862cc35ff9SJeff Roberson 			if (flags & M_ZERO)
13872cc35ff9SJeff Roberson 				bzero(item, zone->uz_size);
13888355f576SJeff Roberson 			return (item);
13898355f576SJeff Roberson 		} else if (cache->uc_freebucket) {
13908355f576SJeff Roberson 			/*
13918355f576SJeff Roberson 			 * We have run out of items in our allocbucket.
13928355f576SJeff Roberson 			 * See if we can switch with our free bucket.
13938355f576SJeff Roberson 			 */
13948355f576SJeff Roberson 			if (cache->uc_freebucket->ub_ptr > -1) {
13958355f576SJeff Roberson 				uma_bucket_t swap;
13968355f576SJeff Roberson 
13978355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
13988355f576SJeff Roberson 				printf("uma_zalloc: Swapping empty with alloc.\n");
13998355f576SJeff Roberson #endif
14008355f576SJeff Roberson 				swap = cache->uc_freebucket;
14018355f576SJeff Roberson 				cache->uc_freebucket = cache->uc_allocbucket;
14028355f576SJeff Roberson 				cache->uc_allocbucket = swap;
14038355f576SJeff Roberson 
14048355f576SJeff Roberson 				goto zalloc_start;
14058355f576SJeff Roberson 			}
14068355f576SJeff Roberson 		}
14078355f576SJeff Roberson 	}
1408a553d4b8SJeff Roberson 	ZONE_LOCK(zone);
1409a553d4b8SJeff Roberson 	/* Since we have locked the zone we may as well send back our stats */
1410a553d4b8SJeff Roberson 	zone->uz_allocs += cache->uc_allocs;
1411a553d4b8SJeff Roberson 	cache->uc_allocs = 0;
14128355f576SJeff Roberson 
1413a553d4b8SJeff Roberson 	/* Our old one is now a free bucket */
1414a553d4b8SJeff Roberson 	if (cache->uc_allocbucket) {
1415a553d4b8SJeff Roberson 		KASSERT(cache->uc_allocbucket->ub_ptr == -1,
1416a553d4b8SJeff Roberson 		    ("uma_zalloc_arg: Freeing a non free bucket."));
1417a553d4b8SJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_free_bucket,
1418a553d4b8SJeff Roberson 		    cache->uc_allocbucket, ub_link);
1419a553d4b8SJeff Roberson 		cache->uc_allocbucket = NULL;
1420a553d4b8SJeff Roberson 	}
14218355f576SJeff Roberson 
1422a553d4b8SJeff Roberson 	/* Check the free list for a new alloc bucket */
1423a553d4b8SJeff Roberson 	if ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
1424a553d4b8SJeff Roberson 		KASSERT(bucket->ub_ptr != -1,
1425a553d4b8SJeff Roberson 		    ("uma_zalloc_arg: Returning an empty bucket."));
14268355f576SJeff Roberson 
1427a553d4b8SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
1428a553d4b8SJeff Roberson 		cache->uc_allocbucket = bucket;
1429a553d4b8SJeff Roberson 		ZONE_UNLOCK(zone);
14308355f576SJeff Roberson 		goto zalloc_start;
1431a553d4b8SJeff Roberson 	}
1432bbee39c6SJeff Roberson 	/* We are no longer associated with this cpu!!! */
1433d88797c2SBosko Milekic 	CPU_UNLOCK(cpu);
1434bbee39c6SJeff Roberson 
1435a553d4b8SJeff Roberson 	/* Bump up our uz_count so we get here less */
1436a553d4b8SJeff Roberson 	if (zone->uz_count < UMA_BUCKET_SIZE - 1)
1437a553d4b8SJeff Roberson 		zone->uz_count++;
1438a553d4b8SJeff Roberson 
14398355f576SJeff Roberson 	/*
1440a553d4b8SJeff Roberson 	 * Now lets just fill a bucket and put it on the free list.  If that
1441a553d4b8SJeff Roberson 	 * works we'll restart the allocation from the begining.
1442bbee39c6SJeff Roberson 	 */
1443bbee39c6SJeff Roberson 
1444bbee39c6SJeff Roberson 	if (uma_zalloc_bucket(zone, flags)) {
1445bbee39c6SJeff Roberson 		ZONE_UNLOCK(zone);
1446bbee39c6SJeff Roberson 		goto zalloc_restart;
1447bbee39c6SJeff Roberson 	}
1448bbee39c6SJeff Roberson 	ZONE_UNLOCK(zone);
1449bbee39c6SJeff Roberson 	/*
1450bbee39c6SJeff Roberson 	 * We may not be able to get a bucket so return an actual item.
1451bbee39c6SJeff Roberson 	 */
1452bbee39c6SJeff Roberson #ifdef UMA_DEBUG
1453bbee39c6SJeff Roberson 	printf("uma_zalloc_arg: Bucketzone returned NULL\n");
1454bbee39c6SJeff Roberson #endif
1455bbee39c6SJeff Roberson 
1456bbee39c6SJeff Roberson 	return (uma_zalloc_internal(zone, udata, flags));
1457bbee39c6SJeff Roberson }
1458bbee39c6SJeff Roberson 
1459bbee39c6SJeff Roberson static uma_slab_t
1460bbee39c6SJeff Roberson uma_zone_slab(uma_zone_t zone, int flags)
1461bbee39c6SJeff Roberson {
1462bbee39c6SJeff Roberson 	uma_slab_t slab;
1463bbee39c6SJeff Roberson 
1464bbee39c6SJeff Roberson 	/*
1465bbee39c6SJeff Roberson 	 * This is to prevent us from recursively trying to allocate
1466bbee39c6SJeff Roberson 	 * buckets.  The problem is that if an allocation forces us to
1467bbee39c6SJeff Roberson 	 * grab a new bucket we will call page_alloc, which will go off
1468bbee39c6SJeff Roberson 	 * and cause the vm to allocate vm_map_entries.  If we need new
1469bbee39c6SJeff Roberson 	 * buckets there too we will recurse in kmem_alloc and bad
1470bbee39c6SJeff Roberson 	 * things happen.  So instead we return a NULL bucket, and make
1471bbee39c6SJeff Roberson 	 * the code that allocates buckets smart enough to deal with it
1472bbee39c6SJeff Roberson 	 */
1473bbee39c6SJeff Roberson 	if (zone == bucketzone && zone->uz_recurse != 0)
1474bbee39c6SJeff Roberson 		return (NULL);
1475bbee39c6SJeff Roberson 
1476bbee39c6SJeff Roberson 	slab = NULL;
1477bbee39c6SJeff Roberson 
1478bbee39c6SJeff Roberson 	for (;;) {
1479bbee39c6SJeff Roberson 		/*
1480bbee39c6SJeff Roberson 		 * Find a slab with some space.  Prefer slabs that are partially
1481bbee39c6SJeff Roberson 		 * used over those that are totally full.  This helps to reduce
1482bbee39c6SJeff Roberson 		 * fragmentation.
1483bbee39c6SJeff Roberson 		 */
1484bbee39c6SJeff Roberson 		if (zone->uz_free != 0) {
1485bbee39c6SJeff Roberson 			if (!LIST_EMPTY(&zone->uz_part_slab)) {
1486bbee39c6SJeff Roberson 				slab = LIST_FIRST(&zone->uz_part_slab);
1487bbee39c6SJeff Roberson 			} else {
1488bbee39c6SJeff Roberson 				slab = LIST_FIRST(&zone->uz_free_slab);
1489bbee39c6SJeff Roberson 				LIST_REMOVE(slab, us_link);
1490bbee39c6SJeff Roberson 				LIST_INSERT_HEAD(&zone->uz_part_slab, slab,
1491bbee39c6SJeff Roberson 				us_link);
1492bbee39c6SJeff Roberson 			}
1493bbee39c6SJeff Roberson 			return (slab);
1494bbee39c6SJeff Roberson 		}
1495bbee39c6SJeff Roberson 
1496bbee39c6SJeff Roberson 		/*
1497bbee39c6SJeff Roberson 		 * M_NOVM means don't ask at all!
1498bbee39c6SJeff Roberson 		 */
1499bbee39c6SJeff Roberson 		if (flags & M_NOVM)
1500bbee39c6SJeff Roberson 			break;
1501bbee39c6SJeff Roberson 
1502bbee39c6SJeff Roberson 		if (zone->uz_maxpages &&
1503bbee39c6SJeff Roberson 		    zone->uz_pages >= zone->uz_maxpages) {
1504bbee39c6SJeff Roberson 			zone->uz_flags |= UMA_ZFLAG_FULL;
1505bbee39c6SJeff Roberson 
1506ebc85edfSJeff Roberson 			if (flags & M_NOWAIT)
1507bbee39c6SJeff Roberson 				break;
1508ebc85edfSJeff Roberson 			else
1509ebc85edfSJeff Roberson 				msleep(zone, &zone->uz_lock, PVM, "zonelimit", 0);
1510bbee39c6SJeff Roberson 			continue;
1511bbee39c6SJeff Roberson 		}
1512bbee39c6SJeff Roberson 		zone->uz_recurse++;
1513bbee39c6SJeff Roberson 		slab = slab_zalloc(zone, flags);
1514bbee39c6SJeff Roberson 		zone->uz_recurse--;
1515bbee39c6SJeff Roberson 		/*
1516bbee39c6SJeff Roberson 		 * If we got a slab here it's safe to mark it partially used
1517bbee39c6SJeff Roberson 		 * and return.  We assume that the caller is going to remove
1518bbee39c6SJeff Roberson 		 * at least one item.
1519bbee39c6SJeff Roberson 		 */
1520bbee39c6SJeff Roberson 		if (slab) {
1521bbee39c6SJeff Roberson 			LIST_INSERT_HEAD(&zone->uz_part_slab, slab, us_link);
1522bbee39c6SJeff Roberson 			return (slab);
1523bbee39c6SJeff Roberson 		}
1524bbee39c6SJeff Roberson 		/*
1525bbee39c6SJeff Roberson 		 * We might not have been able to get a slab but another cpu
1526bbee39c6SJeff Roberson 		 * could have while we were unlocked.  Check again before we
1527bbee39c6SJeff Roberson 		 * fail.
1528bbee39c6SJeff Roberson 		 */
1529ebc85edfSJeff Roberson 		if (flags & M_NOWAIT)
1530bbee39c6SJeff Roberson 			flags |= M_NOVM;
1531bbee39c6SJeff Roberson 	}
1532bbee39c6SJeff Roberson 	return (slab);
1533bbee39c6SJeff Roberson }
1534bbee39c6SJeff Roberson 
1535d56368d7SBosko Milekic static void *
1536bbee39c6SJeff Roberson uma_slab_alloc(uma_zone_t zone, uma_slab_t slab)
1537bbee39c6SJeff Roberson {
1538bbee39c6SJeff Roberson 	void *item;
1539bbee39c6SJeff Roberson 	u_int8_t freei;
1540bbee39c6SJeff Roberson 
1541bbee39c6SJeff Roberson 	freei = slab->us_firstfree;
1542bbee39c6SJeff Roberson 	slab->us_firstfree = slab->us_freelist[freei];
1543bbee39c6SJeff Roberson 	item = slab->us_data + (zone->uz_rsize * freei);
1544bbee39c6SJeff Roberson 
1545bbee39c6SJeff Roberson 	slab->us_freecount--;
1546bbee39c6SJeff Roberson 	zone->uz_free--;
1547bbee39c6SJeff Roberson #ifdef INVARIANTS
1548bbee39c6SJeff Roberson 	uma_dbg_alloc(zone, slab, item);
1549bbee39c6SJeff Roberson #endif
1550bbee39c6SJeff Roberson 	/* Move this slab to the full list */
1551bbee39c6SJeff Roberson 	if (slab->us_freecount == 0) {
1552bbee39c6SJeff Roberson 		LIST_REMOVE(slab, us_link);
1553bbee39c6SJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_full_slab, slab, us_link);
1554bbee39c6SJeff Roberson 	}
1555bbee39c6SJeff Roberson 
1556bbee39c6SJeff Roberson 	return (item);
1557bbee39c6SJeff Roberson }
1558bbee39c6SJeff Roberson 
1559bbee39c6SJeff Roberson static int
1560bbee39c6SJeff Roberson uma_zalloc_bucket(uma_zone_t zone, int flags)
1561bbee39c6SJeff Roberson {
1562bbee39c6SJeff Roberson 	uma_bucket_t bucket;
1563bbee39c6SJeff Roberson 	uma_slab_t slab;
1564bbee39c6SJeff Roberson 
1565bbee39c6SJeff Roberson 	/*
1566a553d4b8SJeff Roberson 	 * Try this zone's free list first so we don't allocate extra buckets.
15678355f576SJeff Roberson 	 */
1568a553d4b8SJeff Roberson 
1569bbee39c6SJeff Roberson 	if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
1570bbee39c6SJeff Roberson 		KASSERT(bucket->ub_ptr == -1,
1571bbee39c6SJeff Roberson 		    ("uma_zalloc_bucket: Bucket on free list is not empty."));
1572a553d4b8SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
1573bbee39c6SJeff Roberson 	} else {
157418aa2de5SJeff Roberson 		int bflags;
157518aa2de5SJeff Roberson 
157618aa2de5SJeff Roberson 		bflags = flags;
157718aa2de5SJeff Roberson 		if (zone->uz_flags & UMA_ZFLAG_BUCKETCACHE)
157818aa2de5SJeff Roberson 			bflags |= M_NOVM;
157918aa2de5SJeff Roberson 
1580bbee39c6SJeff Roberson 		ZONE_UNLOCK(zone);
1581a553d4b8SJeff Roberson 		bucket = uma_zalloc_internal(bucketzone,
1582bbee39c6SJeff Roberson 		    NULL, bflags);
1583bbee39c6SJeff Roberson 		ZONE_LOCK(zone);
1584a553d4b8SJeff Roberson 		if (bucket != NULL) {
1585a553d4b8SJeff Roberson #ifdef INVARIANTS
1586a553d4b8SJeff Roberson 			bzero(bucket, bucketzone->uz_size);
1587a553d4b8SJeff Roberson #endif
1588a553d4b8SJeff Roberson 			bucket->ub_ptr = -1;
1589a553d4b8SJeff Roberson 		}
1590bbee39c6SJeff Roberson 	}
1591bbee39c6SJeff Roberson 
1592bbee39c6SJeff Roberson 	if (bucket == NULL)
1593bbee39c6SJeff Roberson 		return (0);
1594bbee39c6SJeff Roberson 
1595bbee39c6SJeff Roberson #ifdef SMP
1596a553d4b8SJeff Roberson 	/*
1597bbee39c6SJeff Roberson 	 * This code is here to limit the number of simultaneous bucket fills
1598bbee39c6SJeff Roberson 	 * for any given zone to the number of per cpu caches in this zone. This
1599bbee39c6SJeff Roberson 	 * is done so that we don't allocate more memory than we really need.
1600a553d4b8SJeff Roberson 	 */
1601bbee39c6SJeff Roberson 	if (zone->uz_fills >= mp_ncpus)
1602bbee39c6SJeff Roberson 		goto done;
1603a553d4b8SJeff Roberson 
1604bbee39c6SJeff Roberson #endif
1605bbee39c6SJeff Roberson 	zone->uz_fills++;
1606bbee39c6SJeff Roberson 
1607bbee39c6SJeff Roberson 	/* Try to keep the buckets totally full */
1608d11e0ba5SJeff Roberson 	while (bucket->ub_ptr < zone->uz_count &&
1609d11e0ba5SJeff Roberson 	    (slab = uma_zone_slab(zone, flags)) != NULL) {
1610bbee39c6SJeff Roberson 		while (slab->us_freecount &&
1611bbee39c6SJeff Roberson 		    bucket->ub_ptr < zone->uz_count) {
1612bbee39c6SJeff Roberson 			bucket->ub_bucket[++bucket->ub_ptr] =
1613bbee39c6SJeff Roberson 			    uma_slab_alloc(zone, slab);
1614bbee39c6SJeff Roberson 		}
1615bbee39c6SJeff Roberson 		/* Don't block on the next fill */
1616bbee39c6SJeff Roberson 		flags |= M_NOWAIT;
16178355f576SJeff Roberson 	}
16188355f576SJeff Roberson 
1619bbee39c6SJeff Roberson 	zone->uz_fills--;
1620bbee39c6SJeff Roberson 
1621bbee39c6SJeff Roberson 	if (bucket->ub_ptr != -1) {
1622bbee39c6SJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_full_bucket,
1623bbee39c6SJeff Roberson 		    bucket, ub_link);
1624bbee39c6SJeff Roberson 		return (1);
1625bbee39c6SJeff Roberson 	}
1626bbee39c6SJeff Roberson #ifdef SMP
1627bbee39c6SJeff Roberson done:
1628bbee39c6SJeff Roberson #endif
1629bbee39c6SJeff Roberson 	uma_zfree_internal(bucketzone, bucket, NULL, 0);
1630bbee39c6SJeff Roberson 
1631bbee39c6SJeff Roberson 	return (0);
1632bbee39c6SJeff Roberson }
16338355f576SJeff Roberson /*
1634bbee39c6SJeff Roberson  * Allocates an item for an internal zone
16358355f576SJeff Roberson  *
16368355f576SJeff Roberson  * Arguments
16378355f576SJeff Roberson  *	zone   The zone to alloc for.
16388355f576SJeff Roberson  *	udata  The data to be passed to the constructor.
1639a163d034SWarner Losh  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
16408355f576SJeff Roberson  *
16418355f576SJeff Roberson  * Returns
16428355f576SJeff Roberson  *	NULL if there is no memory and M_NOWAIT is set
1643bbee39c6SJeff Roberson  *	An item if successful
16448355f576SJeff Roberson  */
16458355f576SJeff Roberson 
16468355f576SJeff Roberson static void *
1647bbee39c6SJeff Roberson uma_zalloc_internal(uma_zone_t zone, void *udata, int flags)
16488355f576SJeff Roberson {
16498355f576SJeff Roberson 	uma_slab_t slab;
16508355f576SJeff Roberson 	void *item;
16518355f576SJeff Roberson 
16528355f576SJeff Roberson 	item = NULL;
16538355f576SJeff Roberson 
16548355f576SJeff Roberson 	/*
1655a553d4b8SJeff Roberson 	 * This is to stop us from allocating per cpu buckets while we're
1656a553d4b8SJeff Roberson 	 * running out of UMA_BOOT_PAGES.  Otherwise, we would exhaust the
1657a553d4b8SJeff Roberson 	 * boot pages.
16588355f576SJeff Roberson 	 */
16598355f576SJeff Roberson 
166086bbae32SJeff Roberson 	if (bucketdisable && zone == bucketzone)
16618355f576SJeff Roberson 		return (NULL);
16628355f576SJeff Roberson 
16638355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
16648355f576SJeff Roberson 	printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
16658355f576SJeff Roberson #endif
16668355f576SJeff Roberson 	ZONE_LOCK(zone);
16678355f576SJeff Roberson 
1668bbee39c6SJeff Roberson 	slab = uma_zone_slab(zone, flags);
1669bbee39c6SJeff Roberson 	if (slab == NULL) {
1670bce97791SJeff Roberson 		ZONE_UNLOCK(zone);
1671a553d4b8SJeff Roberson 		return (NULL);
1672bce97791SJeff Roberson 	}
1673a553d4b8SJeff Roberson 
1674bbee39c6SJeff Roberson 	item = uma_slab_alloc(zone, slab);
16758355f576SJeff Roberson 
16768355f576SJeff Roberson 	ZONE_UNLOCK(zone);
16778355f576SJeff Roberson 
16783370c5bfSJeff Roberson 	if (zone->uz_ctor != NULL)
16798355f576SJeff Roberson 		zone->uz_ctor(item, zone->uz_size, udata);
16802cc35ff9SJeff Roberson 	if (flags & M_ZERO)
16812cc35ff9SJeff Roberson 		bzero(item, zone->uz_size);
16828355f576SJeff Roberson 
16838355f576SJeff Roberson 	return (item);
16848355f576SJeff Roberson }
16858355f576SJeff Roberson 
16868355f576SJeff Roberson /* See uma.h */
16878355f576SJeff Roberson void
16888355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
16898355f576SJeff Roberson {
16908355f576SJeff Roberson 	uma_cache_t cache;
16918355f576SJeff Roberson 	uma_bucket_t bucket;
16924741dcbfSJeff Roberson 	int bflags;
16938355f576SJeff Roberson 	int cpu;
16945c133dfaSBosko Milekic 	int skip;
16958355f576SJeff Roberson 
16968355f576SJeff Roberson 	/* This is the fast path free */
16975c133dfaSBosko Milekic 	skip = 0;
16988355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1
16998355f576SJeff Roberson 	printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
17008355f576SJeff Roberson #endif
1701af7f9b97SJeff Roberson 	/*
1702af7f9b97SJeff Roberson 	 * The race here is acceptable.  If we miss it we'll just have to wait
1703af7f9b97SJeff Roberson 	 * a little longer for the limits to be reset.
1704af7f9b97SJeff Roberson 	 */
1705af7f9b97SJeff Roberson 
1706af7f9b97SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_FULL)
1707af7f9b97SJeff Roberson 		goto zfree_internal;
1708af7f9b97SJeff Roberson 
17095c133dfaSBosko Milekic 	if (zone->uz_dtor) {
1710bba739abSJeff Roberson 		zone->uz_dtor(item, zone->uz_size, udata);
17115c133dfaSBosko Milekic 		skip = 1;
17125c133dfaSBosko Milekic 	}
1713bba739abSJeff Roberson 
1714a553d4b8SJeff Roberson zfree_restart:
17158355f576SJeff Roberson 	cpu = PCPU_GET(cpuid);
1716d88797c2SBosko Milekic 	CPU_LOCK(cpu);
17178355f576SJeff Roberson 	cache = &zone->uz_cpu[cpu];
17188355f576SJeff Roberson 
17198355f576SJeff Roberson zfree_start:
17208355f576SJeff Roberson 	bucket = cache->uc_freebucket;
17218355f576SJeff Roberson 
17228355f576SJeff Roberson 	if (bucket) {
1723a553d4b8SJeff Roberson 		/*
1724a553d4b8SJeff Roberson 		 * Do we have room in our bucket? It is OK for this uz count
1725a553d4b8SJeff Roberson 		 * check to be slightly out of sync.
1726a553d4b8SJeff Roberson 		 */
1727a553d4b8SJeff Roberson 
1728a553d4b8SJeff Roberson 		if (bucket->ub_ptr < zone->uz_count) {
17298355f576SJeff Roberson 			bucket->ub_ptr++;
17308355f576SJeff Roberson 			KASSERT(bucket->ub_bucket[bucket->ub_ptr] == NULL,
17318355f576SJeff Roberson 			    ("uma_zfree: Freeing to non free bucket index."));
17328355f576SJeff Roberson 			bucket->ub_bucket[bucket->ub_ptr] = item;
1733b9ba8931SJeff Roberson #ifdef INVARIANTS
173481f71edaSMatt Jacob 			ZONE_LOCK(zone);
1735b9ba8931SJeff Roberson 			if (zone->uz_flags & UMA_ZFLAG_MALLOC)
1736b9ba8931SJeff Roberson 				uma_dbg_free(zone, udata, item);
1737b9ba8931SJeff Roberson 			else
1738b9ba8931SJeff Roberson 				uma_dbg_free(zone, NULL, item);
173981f71edaSMatt Jacob 			ZONE_UNLOCK(zone);
1740b9ba8931SJeff Roberson #endif
1741d88797c2SBosko Milekic 			CPU_UNLOCK(cpu);
17428355f576SJeff Roberson 			return;
17438355f576SJeff Roberson 		} else if (cache->uc_allocbucket) {
17448355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
17458355f576SJeff Roberson 			printf("uma_zfree: Swapping buckets.\n");
17468355f576SJeff Roberson #endif
17478355f576SJeff Roberson 			/*
17488355f576SJeff Roberson 			 * We have run out of space in our freebucket.
17498355f576SJeff Roberson 			 * See if we can switch with our alloc bucket.
17508355f576SJeff Roberson 			 */
17518355f576SJeff Roberson 			if (cache->uc_allocbucket->ub_ptr <
17528355f576SJeff Roberson 			    cache->uc_freebucket->ub_ptr) {
17538355f576SJeff Roberson 				uma_bucket_t swap;
17548355f576SJeff Roberson 
17558355f576SJeff Roberson 				swap = cache->uc_freebucket;
17568355f576SJeff Roberson 				cache->uc_freebucket = cache->uc_allocbucket;
17578355f576SJeff Roberson 				cache->uc_allocbucket = swap;
17588355f576SJeff Roberson 
17598355f576SJeff Roberson 				goto zfree_start;
17608355f576SJeff Roberson 			}
17618355f576SJeff Roberson 		}
17628355f576SJeff Roberson 	}
17638355f576SJeff Roberson 
17648355f576SJeff Roberson 	/*
1765a553d4b8SJeff Roberson 	 * We can get here for two reasons:
17668355f576SJeff Roberson 	 *
17678355f576SJeff Roberson 	 * 1) The buckets are NULL
1768a553d4b8SJeff Roberson 	 * 2) The alloc and free buckets are both somewhat full.
17698355f576SJeff Roberson 	 *
17708355f576SJeff Roberson 	 */
17718355f576SJeff Roberson 
17728355f576SJeff Roberson 	ZONE_LOCK(zone);
17738355f576SJeff Roberson 
17748355f576SJeff Roberson 	bucket = cache->uc_freebucket;
17758355f576SJeff Roberson 	cache->uc_freebucket = NULL;
17768355f576SJeff Roberson 
17778355f576SJeff Roberson 	/* Can we throw this on the zone full list? */
17788355f576SJeff Roberson 	if (bucket != NULL) {
17798355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
17808355f576SJeff Roberson 		printf("uma_zfree: Putting old bucket on the free list.\n");
17818355f576SJeff Roberson #endif
17828355f576SJeff Roberson 		/* ub_ptr is pointing to the last free item */
17838355f576SJeff Roberson 		KASSERT(bucket->ub_ptr != -1,
17848355f576SJeff Roberson 		    ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
17858355f576SJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_full_bucket,
17868355f576SJeff Roberson 		    bucket, ub_link);
17878355f576SJeff Roberson 	}
1788a553d4b8SJeff Roberson 	if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
1789a553d4b8SJeff Roberson 		LIST_REMOVE(bucket, ub_link);
1790a553d4b8SJeff Roberson 		ZONE_UNLOCK(zone);
1791a553d4b8SJeff Roberson 		cache->uc_freebucket = bucket;
1792a553d4b8SJeff Roberson 		goto zfree_start;
1793a553d4b8SJeff Roberson 	}
1794a553d4b8SJeff Roberson 	/* We're done with this CPU now */
1795d88797c2SBosko Milekic 	CPU_UNLOCK(cpu);
1796a553d4b8SJeff Roberson 
1797a553d4b8SJeff Roberson 	/* And the zone.. */
1798a553d4b8SJeff Roberson 	ZONE_UNLOCK(zone);
1799a553d4b8SJeff Roberson 
18008355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC
18018355f576SJeff Roberson 	printf("uma_zfree: Allocating new free bucket.\n");
18028355f576SJeff Roberson #endif
18034741dcbfSJeff Roberson 	bflags = M_NOWAIT;
18044741dcbfSJeff Roberson 
18054741dcbfSJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_BUCKETCACHE)
18064741dcbfSJeff Roberson 		bflags |= M_NOVM;
18078355f576SJeff Roberson #ifdef INVARIANTS
18084741dcbfSJeff Roberson 	bflags |= M_ZERO;
18098355f576SJeff Roberson #endif
18104741dcbfSJeff Roberson 	bucket = uma_zalloc_internal(bucketzone,
1811bbee39c6SJeff Roberson 	    NULL, bflags);
18124741dcbfSJeff Roberson 	if (bucket) {
18138355f576SJeff Roberson 		bucket->ub_ptr = -1;
1814a553d4b8SJeff Roberson 		ZONE_LOCK(zone);
1815a553d4b8SJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_free_bucket,
1816a553d4b8SJeff Roberson 		    bucket, ub_link);
18178355f576SJeff Roberson 		ZONE_UNLOCK(zone);
1818a553d4b8SJeff Roberson 		goto zfree_restart;
18198355f576SJeff Roberson 	}
18208355f576SJeff Roberson 
1821a553d4b8SJeff Roberson 	/*
1822a553d4b8SJeff Roberson 	 * If nothing else caught this, we'll just do an internal free.
1823a553d4b8SJeff Roberson 	 */
18248355f576SJeff Roberson 
1825af7f9b97SJeff Roberson zfree_internal:
1826af7f9b97SJeff Roberson 
182748bf8725SBosko Milekic #ifdef INVARIANTS
182848bf8725SBosko Milekic 	/*
182948bf8725SBosko Milekic 	 * If we need to skip the dtor and the uma_dbg_free in uma_zfree_internal
183048bf8725SBosko Milekic 	 * because we've already called the dtor above, but we ended up here, then
183148bf8725SBosko Milekic 	 * we need to make sure that we take care of the uma_dbg_free immediately.
183248bf8725SBosko Milekic 	 */
183348bf8725SBosko Milekic 	if (skip) {
183448bf8725SBosko Milekic 		ZONE_LOCK(zone);
183548bf8725SBosko Milekic 		if (zone->uz_flags & UMA_ZFLAG_MALLOC)
183648bf8725SBosko Milekic 			uma_dbg_free(zone, udata, item);
183748bf8725SBosko Milekic 		else
183848bf8725SBosko Milekic 			uma_dbg_free(zone, NULL, item);
183948bf8725SBosko Milekic 		ZONE_UNLOCK(zone);
184048bf8725SBosko Milekic 	}
184148bf8725SBosko Milekic #endif
18425c133dfaSBosko Milekic 	uma_zfree_internal(zone, item, udata, skip);
18438355f576SJeff Roberson 
18448355f576SJeff Roberson 	return;
18458355f576SJeff Roberson 
18468355f576SJeff Roberson }
18478355f576SJeff Roberson 
18488355f576SJeff Roberson /*
18498355f576SJeff Roberson  * Frees an item to an INTERNAL zone or allocates a free bucket
18508355f576SJeff Roberson  *
18518355f576SJeff Roberson  * Arguments:
18528355f576SJeff Roberson  *	zone   The zone to free to
18538355f576SJeff Roberson  *	item   The item we're freeing
18548355f576SJeff Roberson  *	udata  User supplied data for the dtor
18558355f576SJeff Roberson  *	skip   Skip the dtor, it was done in uma_zfree_arg
18568355f576SJeff Roberson  */
18578355f576SJeff Roberson 
18588355f576SJeff Roberson static void
18598355f576SJeff Roberson uma_zfree_internal(uma_zone_t zone, void *item, void *udata, int skip)
18608355f576SJeff Roberson {
18618355f576SJeff Roberson 	uma_slab_t slab;
18628355f576SJeff Roberson 	u_int8_t *mem;
18638355f576SJeff Roberson 	u_int8_t freei;
18648355f576SJeff Roberson 
1865bba739abSJeff Roberson 	if (!skip && zone->uz_dtor)
1866bba739abSJeff Roberson 		zone->uz_dtor(item, zone->uz_size, udata);
1867bba739abSJeff Roberson 
18688355f576SJeff Roberson 	ZONE_LOCK(zone);
18698355f576SJeff Roberson 
18708355f576SJeff Roberson 	if (!(zone->uz_flags & UMA_ZFLAG_MALLOC)) {
18718355f576SJeff Roberson 		mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK));
187299571dc3SJeff Roberson 		if (zone->uz_flags & UMA_ZFLAG_HASH)
18738355f576SJeff Roberson 			slab = hash_sfind(&zone->uz_hash, mem);
18748355f576SJeff Roberson 		else {
18758355f576SJeff Roberson 			mem += zone->uz_pgoff;
18768355f576SJeff Roberson 			slab = (uma_slab_t)mem;
18778355f576SJeff Roberson 		}
18788355f576SJeff Roberson 	} else {
18798355f576SJeff Roberson 		slab = (uma_slab_t)udata;
18808355f576SJeff Roberson 	}
18818355f576SJeff Roberson 
18828355f576SJeff Roberson 	/* Do we need to remove from any lists? */
18838355f576SJeff Roberson 	if (slab->us_freecount+1 == zone->uz_ipers) {
18848355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
18858355f576SJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_free_slab, slab, us_link);
18868355f576SJeff Roberson 	} else if (slab->us_freecount == 0) {
18878355f576SJeff Roberson 		LIST_REMOVE(slab, us_link);
18888355f576SJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_part_slab, slab, us_link);
18898355f576SJeff Roberson 	}
18908355f576SJeff Roberson 
18918355f576SJeff Roberson 	/* Slab management stuff */
18928355f576SJeff Roberson 	freei = ((unsigned long)item - (unsigned long)slab->us_data)
18938355f576SJeff Roberson 		/ zone->uz_rsize;
18948355f576SJeff Roberson 
1895639c9550SJeff Roberson #ifdef INVARIANTS
1896639c9550SJeff Roberson 	if (!skip)
1897639c9550SJeff Roberson 		uma_dbg_free(zone, slab, item);
18988355f576SJeff Roberson #endif
1899639c9550SJeff Roberson 
19008355f576SJeff Roberson 	slab->us_freelist[freei] = slab->us_firstfree;
19018355f576SJeff Roberson 	slab->us_firstfree = freei;
19028355f576SJeff Roberson 	slab->us_freecount++;
19038355f576SJeff Roberson 
19048355f576SJeff Roberson 	/* Zone statistics */
19058355f576SJeff Roberson 	zone->uz_free++;
19068355f576SJeff Roberson 
1907af7f9b97SJeff Roberson 	if (zone->uz_flags & UMA_ZFLAG_FULL) {
1908af7f9b97SJeff Roberson 		if (zone->uz_pages < zone->uz_maxpages)
1909af7f9b97SJeff Roberson 			zone->uz_flags &= ~UMA_ZFLAG_FULL;
1910af7f9b97SJeff Roberson 
1911af7f9b97SJeff Roberson 		/* We can handle one more allocation */
191274c924b5SJeff Roberson 		wakeup_one(zone);
1913af7f9b97SJeff Roberson 	}
1914af7f9b97SJeff Roberson 
1915605cbd6aSJeff Roberson 	ZONE_UNLOCK(zone);
19168355f576SJeff Roberson }
19178355f576SJeff Roberson 
19188355f576SJeff Roberson /* See uma.h */
19198355f576SJeff Roberson void
1920736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems)
1921736ee590SJeff Roberson {
1922736ee590SJeff Roberson 	ZONE_LOCK(zone);
1923736ee590SJeff Roberson 	if (zone->uz_ppera > 1)
1924af7f9b97SJeff Roberson 		zone->uz_maxpages = nitems * zone->uz_ppera;
1925736ee590SJeff Roberson 	else
1926736ee590SJeff Roberson 		zone->uz_maxpages = nitems / zone->uz_ipers;
192728bc4419SJeff Roberson 
1928d4d6aee5SAndrew R. Reiter 	if (zone->uz_maxpages * zone->uz_ipers < nitems)
1929d4d6aee5SAndrew R. Reiter 		zone->uz_maxpages++;
193028bc4419SJeff Roberson 
1931736ee590SJeff Roberson 	ZONE_UNLOCK(zone);
1932736ee590SJeff Roberson }
1933736ee590SJeff Roberson 
1934736ee590SJeff Roberson /* See uma.h */
1935736ee590SJeff Roberson void
19368355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef)
19378355f576SJeff Roberson {
19388355f576SJeff Roberson 	ZONE_LOCK(zone);
19398355f576SJeff Roberson 
19408355f576SJeff Roberson 	zone->uz_freef = freef;
19418355f576SJeff Roberson 
19428355f576SJeff Roberson 	ZONE_UNLOCK(zone);
19438355f576SJeff Roberson }
19448355f576SJeff Roberson 
19458355f576SJeff Roberson /* See uma.h */
19468355f576SJeff Roberson void
19478355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
19488355f576SJeff Roberson {
19498355f576SJeff Roberson 	ZONE_LOCK(zone);
19508355f576SJeff Roberson 
19518355f576SJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_PRIVALLOC;
19528355f576SJeff Roberson 	zone->uz_allocf = allocf;
19538355f576SJeff Roberson 
19548355f576SJeff Roberson 	ZONE_UNLOCK(zone);
19558355f576SJeff Roberson }
19568355f576SJeff Roberson 
19578355f576SJeff Roberson /* See uma.h */
19588355f576SJeff Roberson int
19598355f576SJeff Roberson uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int count)
19608355f576SJeff Roberson {
19618355f576SJeff Roberson 	int pages;
19628355f576SJeff Roberson 	vm_offset_t kva;
19638355f576SJeff Roberson 
19648355f576SJeff Roberson 	mtx_lock(&Giant);
19658355f576SJeff Roberson 
19668355f576SJeff Roberson 	pages = count / zone->uz_ipers;
19678355f576SJeff Roberson 
19688355f576SJeff Roberson 	if (pages * zone->uz_ipers < count)
19698355f576SJeff Roberson 		pages++;
1970a553d4b8SJeff Roberson 
19718355f576SJeff Roberson 	kva = kmem_alloc_pageable(kernel_map, pages * UMA_SLAB_SIZE);
19728355f576SJeff Roberson 
1973a553d4b8SJeff Roberson 	if (kva == 0) {
1974a553d4b8SJeff Roberson 		mtx_unlock(&Giant);
19758355f576SJeff Roberson 		return (0);
19768355f576SJeff Roberson 	}
19778355f576SJeff Roberson 
19788355f576SJeff Roberson 
1979a553d4b8SJeff Roberson 	if (obj == NULL)
1980a553d4b8SJeff Roberson 		obj = vm_object_allocate(OBJT_DEFAULT,
1981c7173f58SJeff Roberson 		    pages);
198282774d80SAlan Cox 	else {
198382774d80SAlan Cox 		VM_OBJECT_LOCK_INIT(obj);
19848355f576SJeff Roberson 		_vm_object_allocate(OBJT_DEFAULT,
1985c7173f58SJeff Roberson 		    pages, obj);
198682774d80SAlan Cox 	}
1987a553d4b8SJeff Roberson 	ZONE_LOCK(zone);
1988a553d4b8SJeff Roberson 	zone->uz_kva = kva;
1989a553d4b8SJeff Roberson 	zone->uz_obj = obj;
1990a553d4b8SJeff Roberson 	zone->uz_maxpages = pages;
19918355f576SJeff Roberson 
19928355f576SJeff Roberson 	zone->uz_allocf = obj_alloc;
19938355f576SJeff Roberson 	zone->uz_flags |= UMA_ZFLAG_NOFREE | UMA_ZFLAG_PRIVALLOC;
19948355f576SJeff Roberson 
19958355f576SJeff Roberson 	ZONE_UNLOCK(zone);
1996a553d4b8SJeff Roberson 	mtx_unlock(&Giant);
19978355f576SJeff Roberson 
19988355f576SJeff Roberson 	return (1);
19998355f576SJeff Roberson }
20008355f576SJeff Roberson 
20018355f576SJeff Roberson /* See uma.h */
20028355f576SJeff Roberson void
20038355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items)
20048355f576SJeff Roberson {
20058355f576SJeff Roberson 	int slabs;
20068355f576SJeff Roberson 	uma_slab_t slab;
20078355f576SJeff Roberson 
20088355f576SJeff Roberson 	ZONE_LOCK(zone);
20098355f576SJeff Roberson 	slabs = items / zone->uz_ipers;
20108355f576SJeff Roberson 	if (slabs * zone->uz_ipers < items)
20118355f576SJeff Roberson 		slabs++;
20128355f576SJeff Roberson 
20138355f576SJeff Roberson 	while (slabs > 0) {
2014a163d034SWarner Losh 		slab = slab_zalloc(zone, M_WAITOK);
20158355f576SJeff Roberson 		LIST_INSERT_HEAD(&zone->uz_free_slab, slab, us_link);
20168355f576SJeff Roberson 		slabs--;
20178355f576SJeff Roberson 	}
20188355f576SJeff Roberson 	ZONE_UNLOCK(zone);
20198355f576SJeff Roberson }
20208355f576SJeff Roberson 
20218355f576SJeff Roberson /* See uma.h */
20228355f576SJeff Roberson void
20238355f576SJeff Roberson uma_reclaim(void)
20248355f576SJeff Roberson {
20258355f576SJeff Roberson 	/*
20268355f576SJeff Roberson 	 * You might think that the delay below would improve performance since
20278355f576SJeff Roberson 	 * the allocator will give away memory that it may ask for immediately.
20288355f576SJeff Roberson 	 * Really, it makes things worse, since cpu cycles are so much cheaper
20298355f576SJeff Roberson 	 * than disk activity.
20308355f576SJeff Roberson 	 */
20318355f576SJeff Roberson #if 0
20328355f576SJeff Roberson 	static struct timeval tv = {0};
20338355f576SJeff Roberson 	struct timeval now;
20348355f576SJeff Roberson 	getmicrouptime(&now);
20358355f576SJeff Roberson 	if (now.tv_sec > tv.tv_sec + 30)
20368355f576SJeff Roberson 		tv = now;
20378355f576SJeff Roberson 	else
20388355f576SJeff Roberson 		return;
20398355f576SJeff Roberson #endif
20408355f576SJeff Roberson #ifdef UMA_DEBUG
20418355f576SJeff Roberson 	printf("UMA: vm asked us to release pages!\n");
20428355f576SJeff Roberson #endif
204386bbae32SJeff Roberson 	bucket_enable();
20448355f576SJeff Roberson 	zone_foreach(zone_drain);
20458355f576SJeff Roberson 
20468355f576SJeff Roberson 	/*
20478355f576SJeff Roberson 	 * Some slabs may have been freed but this zone will be visited early
20488355f576SJeff Roberson 	 * we visit again so that we can free pages that are empty once other
20498355f576SJeff Roberson 	 * zones are drained.  We have to do the same for buckets.
20508355f576SJeff Roberson 	 */
20518355f576SJeff Roberson 	zone_drain(slabzone);
20528355f576SJeff Roberson 	zone_drain(bucketzone);
20538355f576SJeff Roberson }
20548355f576SJeff Roberson 
20558355f576SJeff Roberson void *
20568355f576SJeff Roberson uma_large_malloc(int size, int wait)
20578355f576SJeff Roberson {
20588355f576SJeff Roberson 	void *mem;
20598355f576SJeff Roberson 	uma_slab_t slab;
20608355f576SJeff Roberson 	u_int8_t flags;
20618355f576SJeff Roberson 
2062bbee39c6SJeff Roberson 	slab = uma_zalloc_internal(slabzone, NULL, wait);
20638355f576SJeff Roberson 	if (slab == NULL)
20648355f576SJeff Roberson 		return (NULL);
20658355f576SJeff Roberson 
20668355f576SJeff Roberson 	mem = page_alloc(NULL, size, &flags, wait);
20678355f576SJeff Roberson 	if (mem) {
206899571dc3SJeff Roberson 		vsetslab((vm_offset_t)mem, slab);
20698355f576SJeff Roberson 		slab->us_data = mem;
20708355f576SJeff Roberson 		slab->us_flags = flags | UMA_SLAB_MALLOC;
20718355f576SJeff Roberson 		slab->us_size = size;
20728355f576SJeff Roberson 	} else {
20738355f576SJeff Roberson 		uma_zfree_internal(slabzone, slab, NULL, 0);
20748355f576SJeff Roberson 	}
20758355f576SJeff Roberson 
20768355f576SJeff Roberson 
20778355f576SJeff Roberson 	return (mem);
20788355f576SJeff Roberson }
20798355f576SJeff Roberson 
20808355f576SJeff Roberson void
20818355f576SJeff Roberson uma_large_free(uma_slab_t slab)
20828355f576SJeff Roberson {
208399571dc3SJeff Roberson 	vsetobj((vm_offset_t)slab->us_data, kmem_object);
2084125ee0d1STor Egge 	/*
20855103186cSAlan Cox 	 * XXX: We get a lock order reversal if we don't have Giant:
2086125ee0d1STor Egge 	 * vm_map_remove (locks system map) -> vm_map_delete ->
2087125ee0d1STor Egge 	 *    vm_map_entry_unwire -> vm_fault_unwire -> mtx_lock(&Giant)
2088125ee0d1STor Egge 	 */
2089125ee0d1STor Egge 	if (!mtx_owned(&Giant)) {
2090125ee0d1STor Egge 		mtx_lock(&Giant);
2091125ee0d1STor Egge 		page_free(slab->us_data, slab->us_size, slab->us_flags);
2092125ee0d1STor Egge 		mtx_unlock(&Giant);
2093125ee0d1STor Egge 	} else
20948355f576SJeff Roberson 		page_free(slab->us_data, slab->us_size, slab->us_flags);
20958355f576SJeff Roberson 	uma_zfree_internal(slabzone, slab, NULL, 0);
20968355f576SJeff Roberson }
20978355f576SJeff Roberson 
20988355f576SJeff Roberson void
20998355f576SJeff Roberson uma_print_stats(void)
21008355f576SJeff Roberson {
21018355f576SJeff Roberson 	zone_foreach(uma_print_zone);
21028355f576SJeff Roberson }
21038355f576SJeff Roberson 
21048355f576SJeff Roberson void
21058355f576SJeff Roberson uma_print_zone(uma_zone_t zone)
21068355f576SJeff Roberson {
21078355f576SJeff Roberson 	printf("%s(%p) size %d(%d) flags %d ipers %d ppera %d out %d free %d\n",
21088355f576SJeff Roberson 	    zone->uz_name, zone, zone->uz_size, zone->uz_rsize, zone->uz_flags,
21098355f576SJeff Roberson 	    zone->uz_ipers, zone->uz_ppera,
21108355f576SJeff Roberson 	    (zone->uz_ipers * zone->uz_pages) - zone->uz_free, zone->uz_free);
21118355f576SJeff Roberson }
21128355f576SJeff Roberson 
21138355f576SJeff Roberson /*
21148355f576SJeff Roberson  * Sysctl handler for vm.zone
21158355f576SJeff Roberson  *
21168355f576SJeff Roberson  * stolen from vm_zone.c
21178355f576SJeff Roberson  */
21188355f576SJeff Roberson static int
21198355f576SJeff Roberson sysctl_vm_zone(SYSCTL_HANDLER_ARGS)
21208355f576SJeff Roberson {
21218355f576SJeff Roberson 	int error, len, cnt;
21228355f576SJeff Roberson 	const int linesize = 128;	/* conservative */
21238355f576SJeff Roberson 	int totalfree;
21248355f576SJeff Roberson 	char *tmpbuf, *offset;
21258355f576SJeff Roberson 	uma_zone_t z;
21268355f576SJeff Roberson 	char *p;
2127f828e5beSJeff Roberson 	int cpu;
2128f828e5beSJeff Roberson 	int cachefree;
2129f828e5beSJeff Roberson 	uma_bucket_t bucket;
2130f828e5beSJeff Roberson 	uma_cache_t cache;
21318355f576SJeff Roberson 
21328355f576SJeff Roberson 	cnt = 0;
21330da47b2fSJeff Roberson 	mtx_lock(&uma_mtx);
21348355f576SJeff Roberson 	LIST_FOREACH(z, &uma_zones, uz_link)
21358355f576SJeff Roberson 		cnt++;
21360da47b2fSJeff Roberson 	mtx_unlock(&uma_mtx);
21378355f576SJeff Roberson 	MALLOC(tmpbuf, char *, (cnt == 0 ? 1 : cnt) * linesize,
2138a163d034SWarner Losh 			M_TEMP, M_WAITOK);
21398355f576SJeff Roberson 	len = snprintf(tmpbuf, linesize,
21408355f576SJeff Roberson 	    "\nITEM            SIZE     LIMIT     USED    FREE  REQUESTS\n\n");
21418355f576SJeff Roberson 	if (cnt == 0)
21428355f576SJeff Roberson 		tmpbuf[len - 1] = '\0';
21438355f576SJeff Roberson 	error = SYSCTL_OUT(req, tmpbuf, cnt == 0 ? len-1 : len);
21448355f576SJeff Roberson 	if (error || cnt == 0)
21458355f576SJeff Roberson 		goto out;
21468355f576SJeff Roberson 	offset = tmpbuf;
2147f4af24d5SJeff Roberson 	mtx_lock(&uma_mtx);
21488355f576SJeff Roberson 	LIST_FOREACH(z, &uma_zones, uz_link) {
21498355f576SJeff Roberson 		if (cnt == 0)	/* list may have changed size */
21508355f576SJeff Roberson 			break;
2151f828e5beSJeff Roberson 		for (cpu = 0; cpu < maxcpu; cpu++) {
2152f828e5beSJeff Roberson 			if (CPU_ABSENT(cpu))
2153f828e5beSJeff Roberson 				continue;
2154f828e5beSJeff Roberson 			CPU_LOCK(cpu);
2155f828e5beSJeff Roberson 		}
21568355f576SJeff Roberson 		ZONE_LOCK(z);
2157f828e5beSJeff Roberson 		cachefree = 0;
2158f828e5beSJeff Roberson 		for (cpu = 0; cpu < maxcpu; cpu++) {
2159f828e5beSJeff Roberson 			if (CPU_ABSENT(cpu))
2160f828e5beSJeff Roberson 				continue;
2161f828e5beSJeff Roberson 			cache = &z->uz_cpu[cpu];
2162f828e5beSJeff Roberson 			if (cache->uc_allocbucket != NULL)
2163f828e5beSJeff Roberson 				cachefree += cache->uc_allocbucket->ub_ptr + 1;
2164f828e5beSJeff Roberson 			if (cache->uc_freebucket != NULL)
2165f828e5beSJeff Roberson 				cachefree += cache->uc_freebucket->ub_ptr + 1;
2166f828e5beSJeff Roberson 			CPU_UNLOCK(cpu);
2167f828e5beSJeff Roberson 		}
2168a40fdcb4SBosko Milekic 		/*
2169a40fdcb4SBosko Milekic 		 * The "UMA Zones" zone (master zone) does not have pcpu
2170a40fdcb4SBosko Milekic 		 * caches allocated for it, so the above computation is entirely
2171a40fdcb4SBosko Milekic 		 * bogus.  Re-set cachefree to 0 in that case.
2172a40fdcb4SBosko Milekic 		 */
2173a40fdcb4SBosko Milekic 		if (z == zones)
2174a40fdcb4SBosko Milekic 			cachefree = 0;
2175f828e5beSJeff Roberson 		LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link) {
2176f828e5beSJeff Roberson 			cachefree += bucket->ub_ptr + 1;
2177f828e5beSJeff Roberson 		}
2178f828e5beSJeff Roberson 		totalfree = z->uz_free + cachefree;
21798355f576SJeff Roberson 		len = snprintf(offset, linesize,
21808355f576SJeff Roberson 		    "%-12.12s  %6.6u, %8.8u, %6.6u, %6.6u, %8.8llu\n",
21818355f576SJeff Roberson 		    z->uz_name, z->uz_size,
21828355f576SJeff Roberson 		    z->uz_maxpages * z->uz_ipers,
21838355f576SJeff Roberson 		    (z->uz_ipers * (z->uz_pages / z->uz_ppera)) - totalfree,
21848355f576SJeff Roberson 		    totalfree,
21858355f576SJeff Roberson 		    (unsigned long long)z->uz_allocs);
21868355f576SJeff Roberson 		ZONE_UNLOCK(z);
21878355f576SJeff Roberson 		for (p = offset + 12; p > offset && *p == ' '; --p)
21888355f576SJeff Roberson 			/* nothing */ ;
21898355f576SJeff Roberson 		p[1] = ':';
21908355f576SJeff Roberson 		cnt--;
21918355f576SJeff Roberson 		offset += len;
21928355f576SJeff Roberson 	}
2193f4af24d5SJeff Roberson 	mtx_unlock(&uma_mtx);
21948355f576SJeff Roberson 	*offset++ = '\0';
21958355f576SJeff Roberson 	error = SYSCTL_OUT(req, tmpbuf, offset - tmpbuf);
21968355f576SJeff Roberson out:
21978355f576SJeff Roberson 	FREE(tmpbuf, M_TEMP);
21988355f576SJeff Roberson 	return (error);
21998355f576SJeff Roberson }
2200