xref: /freebsd/sys/vm/uma_core.c (revision 7d99ab9fd0cc2c1ce2ecef0ed6d0672c2a50b0cb)
1 /*-
2  * Copyright (c) 2002-2005, 2009, 2013 Jeffrey Roberson <jeff@FreeBSD.org>
3  * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
4  * Copyright (c) 2004-2006 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * uma_core.c  Implementation of the Universal Memory allocator
31  *
32  * This allocator is intended to replace the multitude of similar object caches
33  * in the standard FreeBSD kernel.  The intent is to be flexible as well as
34  * effecient.  A primary design goal is to return unused memory to the rest of
35  * the system.  This will make the system as a whole more flexible due to the
36  * ability to move memory to subsystems which most need it instead of leaving
37  * pools of reserved memory unused.
38  *
39  * The basic ideas stem from similar slab/zone based allocators whose algorithms
40  * are well known.
41  *
42  */
43 
44 /*
45  * TODO:
46  *	- Improve memory usage for large allocations
47  *	- Investigate cache size adjustments
48  */
49 
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
52 
53 /* I should really use ktr.. */
54 /*
55 #define UMA_DEBUG 1
56 #define UMA_DEBUG_ALLOC 1
57 #define UMA_DEBUG_ALLOC_1 1
58 */
59 
60 #include "opt_ddb.h"
61 #include "opt_param.h"
62 #include "opt_vm.h"
63 
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/bitset.h>
67 #include <sys/kernel.h>
68 #include <sys/types.h>
69 #include <sys/queue.h>
70 #include <sys/malloc.h>
71 #include <sys/ktr.h>
72 #include <sys/lock.h>
73 #include <sys/sysctl.h>
74 #include <sys/mutex.h>
75 #include <sys/proc.h>
76 #include <sys/rwlock.h>
77 #include <sys/sbuf.h>
78 #include <sys/smp.h>
79 #include <sys/vmmeter.h>
80 
81 #include <vm/vm.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_pageout.h>
85 #include <vm/vm_param.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_kern.h>
88 #include <vm/vm_extern.h>
89 #include <vm/uma.h>
90 #include <vm/uma_int.h>
91 #include <vm/uma_dbg.h>
92 
93 #include <ddb/ddb.h>
94 
95 #ifdef DEBUG_MEMGUARD
96 #include <vm/memguard.h>
97 #endif
98 
99 /*
100  * This is the zone and keg from which all zones are spawned.  The idea is that
101  * even the zone & keg heads are allocated from the allocator, so we use the
102  * bss section to bootstrap us.
103  */
104 static struct uma_keg masterkeg;
105 static struct uma_zone masterzone_k;
106 static struct uma_zone masterzone_z;
107 static uma_zone_t kegs = &masterzone_k;
108 static uma_zone_t zones = &masterzone_z;
109 
110 /* This is the zone from which all of uma_slab_t's are allocated. */
111 static uma_zone_t slabzone;
112 static uma_zone_t slabrefzone;	/* With refcounters (for UMA_ZONE_REFCNT) */
113 
114 /*
115  * The initial hash tables come out of this zone so they can be allocated
116  * prior to malloc coming up.
117  */
118 static uma_zone_t hashzone;
119 
120 /* The boot-time adjusted value for cache line alignment. */
121 int uma_align_cache = 64 - 1;
122 
123 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
124 
125 /*
126  * Are we allowed to allocate buckets?
127  */
128 static int bucketdisable = 1;
129 
130 /* Linked list of all kegs in the system */
131 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
132 
133 /* This mutex protects the keg list */
134 static struct mtx uma_mtx;
135 
136 /* Linked list of boot time pages */
137 static LIST_HEAD(,uma_slab) uma_boot_pages =
138     LIST_HEAD_INITIALIZER(uma_boot_pages);
139 
140 /* This mutex protects the boot time pages list */
141 static struct mtx uma_boot_pages_mtx;
142 
143 /* Is the VM done starting up? */
144 static int booted = 0;
145 #define	UMA_STARTUP	1
146 #define	UMA_STARTUP2	2
147 
148 /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */
149 static const u_int uma_max_ipers = SLAB_SETSIZE;
150 
151 /*
152  * Only mbuf clusters use ref zones.  Just provide enough references
153  * to support the one user.  New code should not use the ref facility.
154  */
155 static const u_int uma_max_ipers_ref = PAGE_SIZE / MCLBYTES;
156 
157 /*
158  * This is the handle used to schedule events that need to happen
159  * outside of the allocation fast path.
160  */
161 static struct callout uma_callout;
162 #define	UMA_TIMEOUT	20		/* Seconds for callout interval. */
163 
164 /*
165  * This structure is passed as the zone ctor arg so that I don't have to create
166  * a special allocation function just for zones.
167  */
168 struct uma_zctor_args {
169 	const char *name;
170 	size_t size;
171 	uma_ctor ctor;
172 	uma_dtor dtor;
173 	uma_init uminit;
174 	uma_fini fini;
175 	uma_keg_t keg;
176 	int align;
177 	uint32_t flags;
178 };
179 
180 struct uma_kctor_args {
181 	uma_zone_t zone;
182 	size_t size;
183 	uma_init uminit;
184 	uma_fini fini;
185 	int align;
186 	uint32_t flags;
187 };
188 
189 struct uma_bucket_zone {
190 	uma_zone_t	ubz_zone;
191 	char		*ubz_name;
192 	int		ubz_entries;
193 };
194 
195 #define	BUCKET_MAX	128
196 
197 struct uma_bucket_zone bucket_zones[] = {
198 	{ NULL, "16 Bucket", 16 },
199 	{ NULL, "32 Bucket", 32 },
200 	{ NULL, "64 Bucket", 64 },
201 	{ NULL, "128 Bucket", 128 },
202 	{ NULL, NULL, 0}
203 };
204 
205 #define	BUCKET_SHIFT	4
206 #define	BUCKET_ZONES	((BUCKET_MAX >> BUCKET_SHIFT) + 1)
207 
208 /*
209  * bucket_size[] maps requested bucket sizes to zones that allocate a bucket
210  * of approximately the right size.
211  */
212 static uint8_t bucket_size[BUCKET_ZONES];
213 
214 /*
215  * Flags and enumerations to be passed to internal functions.
216  */
217 enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI };
218 
219 #define	ZFREE_STATFAIL	0x00000001	/* Update zone failure statistic. */
220 #define	ZFREE_STATFREE	0x00000002	/* Update zone free statistic. */
221 
222 /* Prototypes.. */
223 
224 static void *noobj_alloc(uma_zone_t, int, uint8_t *, int);
225 static void *page_alloc(uma_zone_t, int, uint8_t *, int);
226 static void *startup_alloc(uma_zone_t, int, uint8_t *, int);
227 static void page_free(void *, int, uint8_t);
228 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int);
229 static void cache_drain(uma_zone_t);
230 static void bucket_drain(uma_zone_t, uma_bucket_t);
231 static void bucket_cache_drain(uma_zone_t zone);
232 static int keg_ctor(void *, int, void *, int);
233 static void keg_dtor(void *, int, void *);
234 static int zone_ctor(void *, int, void *, int);
235 static void zone_dtor(void *, int, void *);
236 static int zero_init(void *, int, int);
237 static void keg_small_init(uma_keg_t keg);
238 static void keg_large_init(uma_keg_t keg);
239 static void zone_foreach(void (*zfunc)(uma_zone_t));
240 static void zone_timeout(uma_zone_t zone);
241 static int hash_alloc(struct uma_hash *);
242 static int hash_expand(struct uma_hash *, struct uma_hash *);
243 static void hash_free(struct uma_hash *hash);
244 static void uma_timeout(void *);
245 static void uma_startup3(void);
246 static void *zone_alloc_item(uma_zone_t, void *, int);
247 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip,
248     int);
249 static void bucket_enable(void);
250 static void bucket_init(void);
251 static uma_bucket_t bucket_alloc(int, int);
252 static void bucket_free(uma_bucket_t);
253 static void bucket_zone_drain(void);
254 static int zone_alloc_bucket(uma_zone_t zone, int flags);
255 static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags);
256 static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags);
257 static void *slab_alloc_item(uma_zone_t zone, uma_slab_t slab);
258 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
259     uma_fini fini, int align, uint32_t flags);
260 static inline void zone_relock(uma_zone_t zone, uma_keg_t keg);
261 static inline void keg_relock(uma_keg_t keg, uma_zone_t zone);
262 
263 void uma_print_zone(uma_zone_t);
264 void uma_print_stats(void);
265 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
266 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
267 
268 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
269 
270 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
271     0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
272 
273 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
274     0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
275 
276 static int zone_warnings = 1;
277 TUNABLE_INT("vm.zone_warnings", &zone_warnings);
278 SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RW, &zone_warnings, 0,
279     "Warn when UMA zones becomes full");
280 
281 /*
282  * This routine checks to see whether or not it's safe to enable buckets.
283  */
284 
285 static void
286 bucket_enable(void)
287 {
288 	bucketdisable = vm_page_count_min();
289 }
290 
291 /*
292  * Initialize bucket_zones, the array of zones of buckets of various sizes.
293  *
294  * For each zone, calculate the memory required for each bucket, consisting
295  * of the header and an array of pointers.  Initialize bucket_size[] to point
296  * the range of appropriate bucket sizes at the zone.
297  */
298 static void
299 bucket_init(void)
300 {
301 	struct uma_bucket_zone *ubz;
302 	int i;
303 	int j;
304 
305 	for (i = 0, j = 0; bucket_zones[j].ubz_entries != 0; j++) {
306 		int size;
307 
308 		ubz = &bucket_zones[j];
309 		size = roundup(sizeof(struct uma_bucket), sizeof(void *));
310 		size += sizeof(void *) * ubz->ubz_entries;
311 		ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
312 		    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
313 		    UMA_ZFLAG_INTERNAL | UMA_ZFLAG_BUCKET);
314 		for (; i <= ubz->ubz_entries; i += (1 << BUCKET_SHIFT))
315 			bucket_size[i >> BUCKET_SHIFT] = j;
316 	}
317 }
318 
319 /*
320  * Given a desired number of entries for a bucket, return the zone from which
321  * to allocate the bucket.
322  */
323 static struct uma_bucket_zone *
324 bucket_zone_lookup(int entries)
325 {
326 	int idx;
327 
328 	idx = howmany(entries, 1 << BUCKET_SHIFT);
329 	return (&bucket_zones[bucket_size[idx]]);
330 }
331 
332 static uma_bucket_t
333 bucket_alloc(int entries, int bflags)
334 {
335 	struct uma_bucket_zone *ubz;
336 	uma_bucket_t bucket;
337 
338 	/*
339 	 * This is to stop us from allocating per cpu buckets while we're
340 	 * running out of vm.boot_pages.  Otherwise, we would exhaust the
341 	 * boot pages.  This also prevents us from allocating buckets in
342 	 * low memory situations.
343 	 */
344 	if (bucketdisable)
345 		return (NULL);
346 
347 	ubz = bucket_zone_lookup(entries);
348 	bucket = zone_alloc_item(ubz->ubz_zone, NULL, bflags);
349 	if (bucket) {
350 #ifdef INVARIANTS
351 		bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
352 #endif
353 		bucket->ub_cnt = 0;
354 		bucket->ub_entries = ubz->ubz_entries;
355 	}
356 
357 	return (bucket);
358 }
359 
360 static void
361 bucket_free(uma_bucket_t bucket)
362 {
363 	struct uma_bucket_zone *ubz;
364 
365 	ubz = bucket_zone_lookup(bucket->ub_entries);
366 	zone_free_item(ubz->ubz_zone, bucket, NULL, SKIP_NONE,
367 	    ZFREE_STATFREE);
368 }
369 
370 static void
371 bucket_zone_drain(void)
372 {
373 	struct uma_bucket_zone *ubz;
374 
375 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
376 		zone_drain(ubz->ubz_zone);
377 }
378 
379 static void
380 zone_log_warning(uma_zone_t zone)
381 {
382 	static const struct timeval warninterval = { 300, 0 };
383 
384 	if (!zone_warnings || zone->uz_warning == NULL)
385 		return;
386 
387 	if (ratecheck(&zone->uz_ratecheck, &warninterval))
388 		printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
389 }
390 
391 static inline uma_keg_t
392 zone_first_keg(uma_zone_t zone)
393 {
394 
395 	return (LIST_FIRST(&zone->uz_kegs)->kl_keg);
396 }
397 
398 static void
399 zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
400 {
401 	uma_klink_t klink;
402 
403 	LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
404 		kegfn(klink->kl_keg);
405 }
406 
407 /*
408  * Routine called by timeout which is used to fire off some time interval
409  * based calculations.  (stats, hash size, etc.)
410  *
411  * Arguments:
412  *	arg   Unused
413  *
414  * Returns:
415  *	Nothing
416  */
417 static void
418 uma_timeout(void *unused)
419 {
420 	bucket_enable();
421 	zone_foreach(zone_timeout);
422 
423 	/* Reschedule this event */
424 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
425 }
426 
427 /*
428  * Routine to perform timeout driven calculations.  This expands the
429  * hashes and does per cpu statistics aggregation.
430  *
431  *  Returns nothing.
432  */
433 static void
434 keg_timeout(uma_keg_t keg)
435 {
436 
437 	KEG_LOCK(keg);
438 	/*
439 	 * Expand the keg hash table.
440 	 *
441 	 * This is done if the number of slabs is larger than the hash size.
442 	 * What I'm trying to do here is completely reduce collisions.  This
443 	 * may be a little aggressive.  Should I allow for two collisions max?
444 	 */
445 	if (keg->uk_flags & UMA_ZONE_HASH &&
446 	    keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
447 		struct uma_hash newhash;
448 		struct uma_hash oldhash;
449 		int ret;
450 
451 		/*
452 		 * This is so involved because allocating and freeing
453 		 * while the keg lock is held will lead to deadlock.
454 		 * I have to do everything in stages and check for
455 		 * races.
456 		 */
457 		newhash = keg->uk_hash;
458 		KEG_UNLOCK(keg);
459 		ret = hash_alloc(&newhash);
460 		KEG_LOCK(keg);
461 		if (ret) {
462 			if (hash_expand(&keg->uk_hash, &newhash)) {
463 				oldhash = keg->uk_hash;
464 				keg->uk_hash = newhash;
465 			} else
466 				oldhash = newhash;
467 
468 			KEG_UNLOCK(keg);
469 			hash_free(&oldhash);
470 			KEG_LOCK(keg);
471 		}
472 	}
473 	KEG_UNLOCK(keg);
474 }
475 
476 static void
477 zone_timeout(uma_zone_t zone)
478 {
479 
480 	zone_foreach_keg(zone, &keg_timeout);
481 }
482 
483 /*
484  * Allocate and zero fill the next sized hash table from the appropriate
485  * backing store.
486  *
487  * Arguments:
488  *	hash  A new hash structure with the old hash size in uh_hashsize
489  *
490  * Returns:
491  *	1 on sucess and 0 on failure.
492  */
493 static int
494 hash_alloc(struct uma_hash *hash)
495 {
496 	int oldsize;
497 	int alloc;
498 
499 	oldsize = hash->uh_hashsize;
500 
501 	/* We're just going to go to a power of two greater */
502 	if (oldsize)  {
503 		hash->uh_hashsize = oldsize * 2;
504 		alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
505 		hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
506 		    M_UMAHASH, M_NOWAIT);
507 	} else {
508 		alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
509 		hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
510 		    M_WAITOK);
511 		hash->uh_hashsize = UMA_HASH_SIZE_INIT;
512 	}
513 	if (hash->uh_slab_hash) {
514 		bzero(hash->uh_slab_hash, alloc);
515 		hash->uh_hashmask = hash->uh_hashsize - 1;
516 		return (1);
517 	}
518 
519 	return (0);
520 }
521 
522 /*
523  * Expands the hash table for HASH zones.  This is done from zone_timeout
524  * to reduce collisions.  This must not be done in the regular allocation
525  * path, otherwise, we can recurse on the vm while allocating pages.
526  *
527  * Arguments:
528  *	oldhash  The hash you want to expand
529  *	newhash  The hash structure for the new table
530  *
531  * Returns:
532  *	Nothing
533  *
534  * Discussion:
535  */
536 static int
537 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
538 {
539 	uma_slab_t slab;
540 	int hval;
541 	int i;
542 
543 	if (!newhash->uh_slab_hash)
544 		return (0);
545 
546 	if (oldhash->uh_hashsize >= newhash->uh_hashsize)
547 		return (0);
548 
549 	/*
550 	 * I need to investigate hash algorithms for resizing without a
551 	 * full rehash.
552 	 */
553 
554 	for (i = 0; i < oldhash->uh_hashsize; i++)
555 		while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
556 			slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
557 			SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
558 			hval = UMA_HASH(newhash, slab->us_data);
559 			SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
560 			    slab, us_hlink);
561 		}
562 
563 	return (1);
564 }
565 
566 /*
567  * Free the hash bucket to the appropriate backing store.
568  *
569  * Arguments:
570  *	slab_hash  The hash bucket we're freeing
571  *	hashsize   The number of entries in that hash bucket
572  *
573  * Returns:
574  *	Nothing
575  */
576 static void
577 hash_free(struct uma_hash *hash)
578 {
579 	if (hash->uh_slab_hash == NULL)
580 		return;
581 	if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
582 		zone_free_item(hashzone,
583 		    hash->uh_slab_hash, NULL, SKIP_NONE, ZFREE_STATFREE);
584 	else
585 		free(hash->uh_slab_hash, M_UMAHASH);
586 }
587 
588 /*
589  * Frees all outstanding items in a bucket
590  *
591  * Arguments:
592  *	zone   The zone to free to, must be unlocked.
593  *	bucket The free/alloc bucket with items, cpu queue must be locked.
594  *
595  * Returns:
596  *	Nothing
597  */
598 
599 static void
600 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
601 {
602 	void *item;
603 
604 	if (bucket == NULL)
605 		return;
606 
607 	while (bucket->ub_cnt > 0)  {
608 		bucket->ub_cnt--;
609 		item = bucket->ub_bucket[bucket->ub_cnt];
610 #ifdef INVARIANTS
611 		bucket->ub_bucket[bucket->ub_cnt] = NULL;
612 		KASSERT(item != NULL,
613 		    ("bucket_drain: botched ptr, item is NULL"));
614 #endif
615 		zone_free_item(zone, item, NULL, SKIP_DTOR, 0);
616 	}
617 }
618 
619 /*
620  * Drains the per cpu caches for a zone.
621  *
622  * NOTE: This may only be called while the zone is being turn down, and not
623  * during normal operation.  This is necessary in order that we do not have
624  * to migrate CPUs to drain the per-CPU caches.
625  *
626  * Arguments:
627  *	zone     The zone to drain, must be unlocked.
628  *
629  * Returns:
630  *	Nothing
631  */
632 static void
633 cache_drain(uma_zone_t zone)
634 {
635 	uma_cache_t cache;
636 	int cpu;
637 
638 	/*
639 	 * XXX: It is safe to not lock the per-CPU caches, because we're
640 	 * tearing down the zone anyway.  I.e., there will be no further use
641 	 * of the caches at this point.
642 	 *
643 	 * XXX: It would good to be able to assert that the zone is being
644 	 * torn down to prevent improper use of cache_drain().
645 	 *
646 	 * XXX: We lock the zone before passing into bucket_cache_drain() as
647 	 * it is used elsewhere.  Should the tear-down path be made special
648 	 * there in some form?
649 	 */
650 	CPU_FOREACH(cpu) {
651 		cache = &zone->uz_cpu[cpu];
652 		bucket_drain(zone, cache->uc_allocbucket);
653 		bucket_drain(zone, cache->uc_freebucket);
654 		if (cache->uc_allocbucket != NULL)
655 			bucket_free(cache->uc_allocbucket);
656 		if (cache->uc_freebucket != NULL)
657 			bucket_free(cache->uc_freebucket);
658 		cache->uc_allocbucket = cache->uc_freebucket = NULL;
659 	}
660 	ZONE_LOCK(zone);
661 	bucket_cache_drain(zone);
662 	ZONE_UNLOCK(zone);
663 }
664 
665 /*
666  * Drain the cached buckets from a zone.  Expects a locked zone on entry.
667  */
668 static void
669 bucket_cache_drain(uma_zone_t zone)
670 {
671 	uma_bucket_t bucket;
672 
673 	/*
674 	 * Drain the bucket queues and free the buckets, we just keep two per
675 	 * cpu (alloc/free).
676 	 */
677 	while ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
678 		LIST_REMOVE(bucket, ub_link);
679 		ZONE_UNLOCK(zone);
680 		bucket_drain(zone, bucket);
681 		bucket_free(bucket);
682 		ZONE_LOCK(zone);
683 	}
684 
685 	/* Now we do the free queue.. */
686 	while ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
687 		LIST_REMOVE(bucket, ub_link);
688 		bucket_free(bucket);
689 	}
690 }
691 
692 /*
693  * Frees pages from a keg back to the system.  This is done on demand from
694  * the pageout daemon.
695  *
696  * Returns nothing.
697  */
698 static void
699 keg_drain(uma_keg_t keg)
700 {
701 	struct slabhead freeslabs = { 0 };
702 	uma_slab_t slab;
703 	uma_slab_t n;
704 	uint8_t flags;
705 	uint8_t *mem;
706 	int i;
707 
708 	/*
709 	 * We don't want to take pages from statically allocated kegs at this
710 	 * time
711 	 */
712 	if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
713 		return;
714 
715 #ifdef UMA_DEBUG
716 	printf("%s free items: %u\n", keg->uk_name, keg->uk_free);
717 #endif
718 	KEG_LOCK(keg);
719 	if (keg->uk_free == 0)
720 		goto finished;
721 
722 	slab = LIST_FIRST(&keg->uk_free_slab);
723 	while (slab) {
724 		n = LIST_NEXT(slab, us_link);
725 
726 		/* We have no where to free these to */
727 		if (slab->us_flags & UMA_SLAB_BOOT) {
728 			slab = n;
729 			continue;
730 		}
731 
732 		LIST_REMOVE(slab, us_link);
733 		keg->uk_pages -= keg->uk_ppera;
734 		keg->uk_free -= keg->uk_ipers;
735 
736 		if (keg->uk_flags & UMA_ZONE_HASH)
737 			UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
738 
739 		SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
740 
741 		slab = n;
742 	}
743 finished:
744 	KEG_UNLOCK(keg);
745 
746 	while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
747 		SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
748 		if (keg->uk_fini)
749 			for (i = 0; i < keg->uk_ipers; i++)
750 				keg->uk_fini(
751 				    slab->us_data + (keg->uk_rsize * i),
752 				    keg->uk_size);
753 		flags = slab->us_flags;
754 		mem = slab->us_data;
755 
756 		if (keg->uk_flags & UMA_ZONE_VTOSLAB) {
757 			vm_object_t obj;
758 
759 			if (flags & UMA_SLAB_KMEM)
760 				obj = kmem_object;
761 			else if (flags & UMA_SLAB_KERNEL)
762 				obj = kernel_object;
763 			else
764 				obj = NULL;
765 			for (i = 0; i < keg->uk_ppera; i++)
766 				vsetobj((vm_offset_t)mem + (i * PAGE_SIZE),
767 				    obj);
768 		}
769 		if (keg->uk_flags & UMA_ZONE_OFFPAGE)
770 			zone_free_item(keg->uk_slabzone, slab, NULL,
771 			    SKIP_NONE, ZFREE_STATFREE);
772 #ifdef UMA_DEBUG
773 		printf("%s: Returning %d bytes.\n",
774 		    keg->uk_name, PAGE_SIZE * keg->uk_ppera);
775 #endif
776 		keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
777 	}
778 }
779 
780 static void
781 zone_drain_wait(uma_zone_t zone, int waitok)
782 {
783 
784 	/*
785 	 * Set draining to interlock with zone_dtor() so we can release our
786 	 * locks as we go.  Only dtor() should do a WAITOK call since it
787 	 * is the only call that knows the structure will still be available
788 	 * when it wakes up.
789 	 */
790 	ZONE_LOCK(zone);
791 	while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
792 		if (waitok == M_NOWAIT)
793 			goto out;
794 		mtx_unlock(&uma_mtx);
795 		msleep(zone, zone->uz_lock, PVM, "zonedrain", 1);
796 		mtx_lock(&uma_mtx);
797 	}
798 	zone->uz_flags |= UMA_ZFLAG_DRAINING;
799 	bucket_cache_drain(zone);
800 	ZONE_UNLOCK(zone);
801 	/*
802 	 * The DRAINING flag protects us from being freed while
803 	 * we're running.  Normally the uma_mtx would protect us but we
804 	 * must be able to release and acquire the right lock for each keg.
805 	 */
806 	zone_foreach_keg(zone, &keg_drain);
807 	ZONE_LOCK(zone);
808 	zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
809 	wakeup(zone);
810 out:
811 	ZONE_UNLOCK(zone);
812 }
813 
814 void
815 zone_drain(uma_zone_t zone)
816 {
817 
818 	zone_drain_wait(zone, M_NOWAIT);
819 }
820 
821 /*
822  * Allocate a new slab for a keg.  This does not insert the slab onto a list.
823  *
824  * Arguments:
825  *	wait  Shall we wait?
826  *
827  * Returns:
828  *	The slab that was allocated or NULL if there is no memory and the
829  *	caller specified M_NOWAIT.
830  */
831 static uma_slab_t
832 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait)
833 {
834 	uma_slabrefcnt_t slabref;
835 	uma_alloc allocf;
836 	uma_slab_t slab;
837 	uint8_t *mem;
838 	uint8_t flags;
839 	int i;
840 
841 	mtx_assert(&keg->uk_lock, MA_OWNED);
842 	slab = NULL;
843 
844 #ifdef UMA_DEBUG
845 	printf("slab_zalloc:  Allocating a new slab for %s\n", keg->uk_name);
846 #endif
847 	allocf = keg->uk_allocf;
848 	KEG_UNLOCK(keg);
849 
850 	if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
851 		slab = zone_alloc_item(keg->uk_slabzone, NULL, wait);
852 		if (slab == NULL) {
853 			KEG_LOCK(keg);
854 			return NULL;
855 		}
856 	}
857 
858 	/*
859 	 * This reproduces the old vm_zone behavior of zero filling pages the
860 	 * first time they are added to a zone.
861 	 *
862 	 * Malloced items are zeroed in uma_zalloc.
863 	 */
864 
865 	if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
866 		wait |= M_ZERO;
867 	else
868 		wait &= ~M_ZERO;
869 
870 	if (keg->uk_flags & UMA_ZONE_NODUMP)
871 		wait |= M_NODUMP;
872 
873 	/* zone is passed for legacy reasons. */
874 	mem = allocf(zone, keg->uk_ppera * PAGE_SIZE, &flags, wait);
875 	if (mem == NULL) {
876 		if (keg->uk_flags & UMA_ZONE_OFFPAGE)
877 			zone_free_item(keg->uk_slabzone, slab, NULL,
878 			    SKIP_NONE, ZFREE_STATFREE);
879 		KEG_LOCK(keg);
880 		return (NULL);
881 	}
882 
883 	/* Point the slab into the allocated memory */
884 	if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
885 		slab = (uma_slab_t )(mem + keg->uk_pgoff);
886 
887 	if (keg->uk_flags & UMA_ZONE_VTOSLAB)
888 		for (i = 0; i < keg->uk_ppera; i++)
889 			vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
890 
891 	slab->us_keg = keg;
892 	slab->us_data = mem;
893 	slab->us_freecount = keg->uk_ipers;
894 	slab->us_flags = flags;
895 	BIT_FILL(SLAB_SETSIZE, &slab->us_free);
896 #ifdef INVARIANTS
897 	BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree);
898 #endif
899 	if (keg->uk_flags & UMA_ZONE_REFCNT) {
900 		slabref = (uma_slabrefcnt_t)slab;
901 		for (i = 0; i < keg->uk_ipers; i++)
902 			slabref->us_refcnt[i] = 0;
903 	}
904 
905 	if (keg->uk_init != NULL) {
906 		for (i = 0; i < keg->uk_ipers; i++)
907 			if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
908 			    keg->uk_size, wait) != 0)
909 				break;
910 		if (i != keg->uk_ipers) {
911 			if (keg->uk_fini != NULL) {
912 				for (i--; i > -1; i--)
913 					keg->uk_fini(slab->us_data +
914 					    (keg->uk_rsize * i),
915 					    keg->uk_size);
916 			}
917 			if (keg->uk_flags & UMA_ZONE_VTOSLAB) {
918 				vm_object_t obj;
919 
920 				if (flags & UMA_SLAB_KMEM)
921 					obj = kmem_object;
922 				else if (flags & UMA_SLAB_KERNEL)
923 					obj = kernel_object;
924 				else
925 					obj = NULL;
926 				for (i = 0; i < keg->uk_ppera; i++)
927 					vsetobj((vm_offset_t)mem +
928 					    (i * PAGE_SIZE), obj);
929 			}
930 			if (keg->uk_flags & UMA_ZONE_OFFPAGE)
931 				zone_free_item(keg->uk_slabzone, slab,
932 				    NULL, SKIP_NONE, ZFREE_STATFREE);
933 			keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera,
934 			    flags);
935 			KEG_LOCK(keg);
936 			return (NULL);
937 		}
938 	}
939 	KEG_LOCK(keg);
940 
941 	if (keg->uk_flags & UMA_ZONE_HASH)
942 		UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
943 
944 	keg->uk_pages += keg->uk_ppera;
945 	keg->uk_free += keg->uk_ipers;
946 
947 	return (slab);
948 }
949 
950 /*
951  * This function is intended to be used early on in place of page_alloc() so
952  * that we may use the boot time page cache to satisfy allocations before
953  * the VM is ready.
954  */
955 static void *
956 startup_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait)
957 {
958 	uma_keg_t keg;
959 	uma_slab_t tmps;
960 	int pages, check_pages;
961 
962 	keg = zone_first_keg(zone);
963 	pages = howmany(bytes, PAGE_SIZE);
964 	check_pages = pages - 1;
965 	KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n"));
966 
967 	/*
968 	 * Check our small startup cache to see if it has pages remaining.
969 	 */
970 	mtx_lock(&uma_boot_pages_mtx);
971 
972 	/* First check if we have enough room. */
973 	tmps = LIST_FIRST(&uma_boot_pages);
974 	while (tmps != NULL && check_pages-- > 0)
975 		tmps = LIST_NEXT(tmps, us_link);
976 	if (tmps != NULL) {
977 		/*
978 		 * It's ok to lose tmps references.  The last one will
979 		 * have tmps->us_data pointing to the start address of
980 		 * "pages" contiguous pages of memory.
981 		 */
982 		while (pages-- > 0) {
983 			tmps = LIST_FIRST(&uma_boot_pages);
984 			LIST_REMOVE(tmps, us_link);
985 		}
986 		mtx_unlock(&uma_boot_pages_mtx);
987 		*pflag = tmps->us_flags;
988 		return (tmps->us_data);
989 	}
990 	mtx_unlock(&uma_boot_pages_mtx);
991 	if (booted < UMA_STARTUP2)
992 		panic("UMA: Increase vm.boot_pages");
993 	/*
994 	 * Now that we've booted reset these users to their real allocator.
995 	 */
996 #ifdef UMA_MD_SMALL_ALLOC
997 	keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc;
998 #else
999 	keg->uk_allocf = page_alloc;
1000 #endif
1001 	return keg->uk_allocf(zone, bytes, pflag, wait);
1002 }
1003 
1004 /*
1005  * Allocates a number of pages from the system
1006  *
1007  * Arguments:
1008  *	bytes  The number of bytes requested
1009  *	wait  Shall we wait?
1010  *
1011  * Returns:
1012  *	A pointer to the alloced memory or possibly
1013  *	NULL if M_NOWAIT is set.
1014  */
1015 static void *
1016 page_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait)
1017 {
1018 	void *p;	/* Returned page */
1019 
1020 	*pflag = UMA_SLAB_KMEM;
1021 	p = (void *) kmem_malloc(kmem_map, bytes, wait);
1022 
1023 	return (p);
1024 }
1025 
1026 /*
1027  * Allocates a number of pages from within an object
1028  *
1029  * Arguments:
1030  *	bytes  The number of bytes requested
1031  *	wait   Shall we wait?
1032  *
1033  * Returns:
1034  *	A pointer to the alloced memory or possibly
1035  *	NULL if M_NOWAIT is set.
1036  */
1037 static void *
1038 noobj_alloc(uma_zone_t zone, int bytes, uint8_t *flags, int wait)
1039 {
1040 	TAILQ_HEAD(, vm_page) alloctail;
1041 	u_long npages;
1042 	vm_offset_t retkva, zkva;
1043 	vm_page_t p, p_next;
1044 	uma_keg_t keg;
1045 
1046 	TAILQ_INIT(&alloctail);
1047 	keg = zone_first_keg(zone);
1048 
1049 	npages = howmany(bytes, PAGE_SIZE);
1050 	while (npages > 0) {
1051 		p = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT |
1052 		    VM_ALLOC_WIRED | VM_ALLOC_NOOBJ);
1053 		if (p != NULL) {
1054 			/*
1055 			 * Since the page does not belong to an object, its
1056 			 * listq is unused.
1057 			 */
1058 			TAILQ_INSERT_TAIL(&alloctail, p, listq);
1059 			npages--;
1060 			continue;
1061 		}
1062 		if (wait & M_WAITOK) {
1063 			VM_WAIT;
1064 			continue;
1065 		}
1066 
1067 		/*
1068 		 * Page allocation failed, free intermediate pages and
1069 		 * exit.
1070 		 */
1071 		TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1072 			vm_page_unwire(p, 0);
1073 			vm_page_free(p);
1074 		}
1075 		return (NULL);
1076 	}
1077 	*flags = UMA_SLAB_PRIV;
1078 	zkva = keg->uk_kva +
1079 	    atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1080 	retkva = zkva;
1081 	TAILQ_FOREACH(p, &alloctail, listq) {
1082 		pmap_qenter(zkva, &p, 1);
1083 		zkva += PAGE_SIZE;
1084 	}
1085 
1086 	return ((void *)retkva);
1087 }
1088 
1089 /*
1090  * Frees a number of pages to the system
1091  *
1092  * Arguments:
1093  *	mem   A pointer to the memory to be freed
1094  *	size  The size of the memory being freed
1095  *	flags The original p->us_flags field
1096  *
1097  * Returns:
1098  *	Nothing
1099  */
1100 static void
1101 page_free(void *mem, int size, uint8_t flags)
1102 {
1103 	vm_map_t map;
1104 
1105 	if (flags & UMA_SLAB_KMEM)
1106 		map = kmem_map;
1107 	else if (flags & UMA_SLAB_KERNEL)
1108 		map = kernel_map;
1109 	else
1110 		panic("UMA: page_free used with invalid flags %d", flags);
1111 
1112 	kmem_free(map, (vm_offset_t)mem, size);
1113 }
1114 
1115 /*
1116  * Zero fill initializer
1117  *
1118  * Arguments/Returns follow uma_init specifications
1119  */
1120 static int
1121 zero_init(void *mem, int size, int flags)
1122 {
1123 	bzero(mem, size);
1124 	return (0);
1125 }
1126 
1127 /*
1128  * Finish creating a small uma keg.  This calculates ipers, and the keg size.
1129  *
1130  * Arguments
1131  *	keg  The zone we should initialize
1132  *
1133  * Returns
1134  *	Nothing
1135  */
1136 static void
1137 keg_small_init(uma_keg_t keg)
1138 {
1139 	u_int rsize;
1140 	u_int memused;
1141 	u_int wastedspace;
1142 	u_int shsize;
1143 
1144 	if (keg->uk_flags & UMA_ZONE_PCPU) {
1145 		KASSERT(mp_ncpus > 0, ("%s: ncpus %d\n", __func__, mp_ncpus));
1146 		keg->uk_slabsize = sizeof(struct pcpu);
1147 		keg->uk_ppera = howmany(mp_ncpus * sizeof(struct pcpu),
1148 		    PAGE_SIZE);
1149 	} else {
1150 		keg->uk_slabsize = UMA_SLAB_SIZE;
1151 		keg->uk_ppera = 1;
1152 	}
1153 
1154 	/*
1155 	 * Calculate the size of each allocation (rsize) according to
1156 	 * alignment.  If the requested size is smaller than we have
1157 	 * allocation bits for we round it up.
1158 	 */
1159 	rsize = keg->uk_size;
1160 	if (rsize < keg->uk_slabsize / SLAB_SETSIZE)
1161 		rsize = keg->uk_slabsize / SLAB_SETSIZE;
1162 	if (rsize & keg->uk_align)
1163 		rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1164 	keg->uk_rsize = rsize;
1165 
1166 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
1167 	    keg->uk_rsize < sizeof(struct pcpu),
1168 	    ("%s: size %u too large", __func__, keg->uk_rsize));
1169 
1170 	if (keg->uk_flags & UMA_ZONE_REFCNT)
1171 		rsize += sizeof(uint32_t);
1172 
1173 	if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1174 		shsize = 0;
1175 	else
1176 		shsize = sizeof(struct uma_slab);
1177 
1178 	keg->uk_ipers = (keg->uk_slabsize - shsize) / rsize;
1179 	KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1180 	    ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1181 
1182 	memused = keg->uk_ipers * rsize + shsize;
1183 	wastedspace = keg->uk_slabsize - memused;
1184 
1185 	/*
1186 	 * We can't do OFFPAGE if we're internal or if we've been
1187 	 * asked to not go to the VM for buckets.  If we do this we
1188 	 * may end up going to the VM (kmem_map) for slabs which we
1189 	 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a
1190 	 * result of UMA_ZONE_VM, which clearly forbids it.
1191 	 */
1192 	if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1193 	    (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1194 		return;
1195 
1196 	/*
1197 	 * See if using an OFFPAGE slab will limit our waste.  Only do
1198 	 * this if it permits more items per-slab.
1199 	 *
1200 	 * XXX We could try growing slabsize to limit max waste as well.
1201 	 * Historically this was not done because the VM could not
1202 	 * efficiently handle contiguous allocations.
1203 	 */
1204 	if ((wastedspace >= keg->uk_slabsize / UMA_MAX_WASTE) &&
1205 	    (keg->uk_ipers < (keg->uk_slabsize / keg->uk_rsize))) {
1206 		keg->uk_ipers = keg->uk_slabsize / keg->uk_rsize;
1207 		KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1208 		    ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1209 #ifdef UMA_DEBUG
1210 		printf("UMA decided we need offpage slab headers for "
1211 		    "keg: %s, calculated wastedspace = %d, "
1212 		    "maximum wasted space allowed = %d, "
1213 		    "calculated ipers = %d, "
1214 		    "new wasted space = %d\n", keg->uk_name, wastedspace,
1215 		    keg->uk_slabsize / UMA_MAX_WASTE, keg->uk_ipers,
1216 		    keg->uk_slabsize - keg->uk_ipers * keg->uk_rsize);
1217 #endif
1218 		keg->uk_flags |= UMA_ZONE_OFFPAGE;
1219 	}
1220 
1221 	if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1222 	    (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1223 		keg->uk_flags |= UMA_ZONE_HASH;
1224 }
1225 
1226 /*
1227  * Finish creating a large (> UMA_SLAB_SIZE) uma kegs.  Just give in and do
1228  * OFFPAGE for now.  When I can allow for more dynamic slab sizes this will be
1229  * more complicated.
1230  *
1231  * Arguments
1232  *	keg  The keg we should initialize
1233  *
1234  * Returns
1235  *	Nothing
1236  */
1237 static void
1238 keg_large_init(uma_keg_t keg)
1239 {
1240 
1241 	KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1242 	KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1243 	    ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1244 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1245 	    ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__));
1246 
1247 	keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE);
1248 	keg->uk_slabsize = keg->uk_ppera * PAGE_SIZE;
1249 	keg->uk_ipers = 1;
1250 	keg->uk_rsize = keg->uk_size;
1251 
1252 	/* We can't do OFFPAGE if we're internal, bail out here. */
1253 	if (keg->uk_flags & UMA_ZFLAG_INTERNAL)
1254 		return;
1255 
1256 	keg->uk_flags |= UMA_ZONE_OFFPAGE;
1257 	if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1258 		keg->uk_flags |= UMA_ZONE_HASH;
1259 }
1260 
1261 static void
1262 keg_cachespread_init(uma_keg_t keg)
1263 {
1264 	int alignsize;
1265 	int trailer;
1266 	int pages;
1267 	int rsize;
1268 
1269 	KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1270 	    ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__));
1271 
1272 	alignsize = keg->uk_align + 1;
1273 	rsize = keg->uk_size;
1274 	/*
1275 	 * We want one item to start on every align boundary in a page.  To
1276 	 * do this we will span pages.  We will also extend the item by the
1277 	 * size of align if it is an even multiple of align.  Otherwise, it
1278 	 * would fall on the same boundary every time.
1279 	 */
1280 	if (rsize & keg->uk_align)
1281 		rsize = (rsize & ~keg->uk_align) + alignsize;
1282 	if ((rsize & alignsize) == 0)
1283 		rsize += alignsize;
1284 	trailer = rsize - keg->uk_size;
1285 	pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1286 	pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1287 	keg->uk_rsize = rsize;
1288 	keg->uk_ppera = pages;
1289 	keg->uk_slabsize = UMA_SLAB_SIZE;
1290 	keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1291 	keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1292 	KASSERT(keg->uk_ipers <= uma_max_ipers,
1293 	    ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1294 	    keg->uk_ipers));
1295 }
1296 
1297 /*
1298  * Keg header ctor.  This initializes all fields, locks, etc.  And inserts
1299  * the keg onto the global keg list.
1300  *
1301  * Arguments/Returns follow uma_ctor specifications
1302  *	udata  Actually uma_kctor_args
1303  */
1304 static int
1305 keg_ctor(void *mem, int size, void *udata, int flags)
1306 {
1307 	struct uma_kctor_args *arg = udata;
1308 	uma_keg_t keg = mem;
1309 	uma_zone_t zone;
1310 
1311 	bzero(keg, size);
1312 	keg->uk_size = arg->size;
1313 	keg->uk_init = arg->uminit;
1314 	keg->uk_fini = arg->fini;
1315 	keg->uk_align = arg->align;
1316 	keg->uk_free = 0;
1317 	keg->uk_pages = 0;
1318 	keg->uk_flags = arg->flags;
1319 	keg->uk_allocf = page_alloc;
1320 	keg->uk_freef = page_free;
1321 	keg->uk_recurse = 0;
1322 	keg->uk_slabzone = NULL;
1323 
1324 	/*
1325 	 * The master zone is passed to us at keg-creation time.
1326 	 */
1327 	zone = arg->zone;
1328 	keg->uk_name = zone->uz_name;
1329 
1330 	if (arg->flags & UMA_ZONE_VM)
1331 		keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1332 
1333 	if (arg->flags & UMA_ZONE_ZINIT)
1334 		keg->uk_init = zero_init;
1335 
1336 	if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC)
1337 		keg->uk_flags |= UMA_ZONE_VTOSLAB;
1338 
1339 	if (arg->flags & UMA_ZONE_PCPU)
1340 #ifdef SMP
1341 		keg->uk_flags |= UMA_ZONE_OFFPAGE;
1342 #else
1343 		keg->uk_flags &= ~UMA_ZONE_PCPU;
1344 #endif
1345 
1346 	if (keg->uk_flags & UMA_ZONE_CACHESPREAD) {
1347 		keg_cachespread_init(keg);
1348 	} else if (keg->uk_flags & UMA_ZONE_REFCNT) {
1349 		if (keg->uk_size >
1350 		    (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) -
1351 		    sizeof(uint32_t)))
1352 			keg_large_init(keg);
1353 		else
1354 			keg_small_init(keg);
1355 	} else {
1356 		if (keg->uk_size > (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1357 			keg_large_init(keg);
1358 		else
1359 			keg_small_init(keg);
1360 	}
1361 
1362 	if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1363 		if (keg->uk_flags & UMA_ZONE_REFCNT) {
1364 			if (keg->uk_ipers > uma_max_ipers_ref)
1365 				panic("Too many ref items per zone: %d > %d\n",
1366 				    keg->uk_ipers, uma_max_ipers_ref);
1367 			keg->uk_slabzone = slabrefzone;
1368 		} else
1369 			keg->uk_slabzone = slabzone;
1370 	}
1371 
1372 	/*
1373 	 * If we haven't booted yet we need allocations to go through the
1374 	 * startup cache until the vm is ready.
1375 	 */
1376 	if (keg->uk_ppera == 1) {
1377 #ifdef UMA_MD_SMALL_ALLOC
1378 		keg->uk_allocf = uma_small_alloc;
1379 		keg->uk_freef = uma_small_free;
1380 
1381 		if (booted < UMA_STARTUP)
1382 			keg->uk_allocf = startup_alloc;
1383 #else
1384 		if (booted < UMA_STARTUP2)
1385 			keg->uk_allocf = startup_alloc;
1386 #endif
1387 	} else if (booted < UMA_STARTUP2 &&
1388 	    (keg->uk_flags & UMA_ZFLAG_INTERNAL))
1389 		keg->uk_allocf = startup_alloc;
1390 
1391 	/*
1392 	 * Initialize keg's lock (shared among zones).
1393 	 */
1394 	if (arg->flags & UMA_ZONE_MTXCLASS)
1395 		KEG_LOCK_INIT(keg, 1);
1396 	else
1397 		KEG_LOCK_INIT(keg, 0);
1398 
1399 	/*
1400 	 * If we're putting the slab header in the actual page we need to
1401 	 * figure out where in each page it goes.  This calculates a right
1402 	 * justified offset into the memory on an ALIGN_PTR boundary.
1403 	 */
1404 	if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1405 		u_int totsize;
1406 
1407 		/* Size of the slab struct and free list */
1408 		totsize = sizeof(struct uma_slab);
1409 
1410 		/* Size of the reference counts. */
1411 		if (keg->uk_flags & UMA_ZONE_REFCNT)
1412 			totsize += keg->uk_ipers * sizeof(uint32_t);
1413 
1414 		if (totsize & UMA_ALIGN_PTR)
1415 			totsize = (totsize & ~UMA_ALIGN_PTR) +
1416 			    (UMA_ALIGN_PTR + 1);
1417 		keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize;
1418 
1419 		/*
1420 		 * The only way the following is possible is if with our
1421 		 * UMA_ALIGN_PTR adjustments we are now bigger than
1422 		 * UMA_SLAB_SIZE.  I haven't checked whether this is
1423 		 * mathematically possible for all cases, so we make
1424 		 * sure here anyway.
1425 		 */
1426 		totsize = keg->uk_pgoff + sizeof(struct uma_slab);
1427 		if (keg->uk_flags & UMA_ZONE_REFCNT)
1428 			totsize += keg->uk_ipers * sizeof(uint32_t);
1429 		if (totsize > PAGE_SIZE * keg->uk_ppera) {
1430 			printf("zone %s ipers %d rsize %d size %d\n",
1431 			    zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1432 			    keg->uk_size);
1433 			panic("UMA slab won't fit.");
1434 		}
1435 	}
1436 
1437 	if (keg->uk_flags & UMA_ZONE_HASH)
1438 		hash_alloc(&keg->uk_hash);
1439 
1440 #ifdef UMA_DEBUG
1441 	printf("UMA: %s(%p) size %d(%d) flags %#x ipers %d ppera %d out %d free %d\n",
1442 	    zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
1443 	    keg->uk_ipers, keg->uk_ppera,
1444 	    (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
1445 #endif
1446 
1447 	LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1448 
1449 	mtx_lock(&uma_mtx);
1450 	LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1451 	mtx_unlock(&uma_mtx);
1452 	return (0);
1453 }
1454 
1455 /*
1456  * Zone header ctor.  This initializes all fields, locks, etc.
1457  *
1458  * Arguments/Returns follow uma_ctor specifications
1459  *	udata  Actually uma_zctor_args
1460  */
1461 static int
1462 zone_ctor(void *mem, int size, void *udata, int flags)
1463 {
1464 	struct uma_zctor_args *arg = udata;
1465 	uma_zone_t zone = mem;
1466 	uma_zone_t z;
1467 	uma_keg_t keg;
1468 
1469 	bzero(zone, size);
1470 	zone->uz_name = arg->name;
1471 	zone->uz_ctor = arg->ctor;
1472 	zone->uz_dtor = arg->dtor;
1473 	zone->uz_slab = zone_fetch_slab;
1474 	zone->uz_init = NULL;
1475 	zone->uz_fini = NULL;
1476 	zone->uz_allocs = 0;
1477 	zone->uz_frees = 0;
1478 	zone->uz_fails = 0;
1479 	zone->uz_sleeps = 0;
1480 	zone->uz_fills = zone->uz_count = 0;
1481 	zone->uz_flags = 0;
1482 	zone->uz_warning = NULL;
1483 	timevalclear(&zone->uz_ratecheck);
1484 	keg = arg->keg;
1485 
1486 	if (arg->flags & UMA_ZONE_SECONDARY) {
1487 		KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1488 		zone->uz_init = arg->uminit;
1489 		zone->uz_fini = arg->fini;
1490 		zone->uz_lock = &keg->uk_lock;
1491 		zone->uz_flags |= UMA_ZONE_SECONDARY;
1492 		mtx_lock(&uma_mtx);
1493 		ZONE_LOCK(zone);
1494 		LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1495 			if (LIST_NEXT(z, uz_link) == NULL) {
1496 				LIST_INSERT_AFTER(z, zone, uz_link);
1497 				break;
1498 			}
1499 		}
1500 		ZONE_UNLOCK(zone);
1501 		mtx_unlock(&uma_mtx);
1502 	} else if (keg == NULL) {
1503 		if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1504 		    arg->align, arg->flags)) == NULL)
1505 			return (ENOMEM);
1506 	} else {
1507 		struct uma_kctor_args karg;
1508 		int error;
1509 
1510 		/* We should only be here from uma_startup() */
1511 		karg.size = arg->size;
1512 		karg.uminit = arg->uminit;
1513 		karg.fini = arg->fini;
1514 		karg.align = arg->align;
1515 		karg.flags = arg->flags;
1516 		karg.zone = zone;
1517 		error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1518 		    flags);
1519 		if (error)
1520 			return (error);
1521 	}
1522 	/*
1523 	 * Link in the first keg.
1524 	 */
1525 	zone->uz_klink.kl_keg = keg;
1526 	LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1527 	zone->uz_lock = &keg->uk_lock;
1528 	zone->uz_size = keg->uk_size;
1529 	zone->uz_flags |= (keg->uk_flags &
1530 	    (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1531 
1532 	/*
1533 	 * Some internal zones don't have room allocated for the per cpu
1534 	 * caches.  If we're internal, bail out here.
1535 	 */
1536 	if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1537 		KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1538 		    ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1539 		return (0);
1540 	}
1541 
1542 	if (keg->uk_flags & UMA_ZONE_MAXBUCKET)
1543 		zone->uz_count = BUCKET_MAX;
1544 	else if (keg->uk_ipers <= BUCKET_MAX)
1545 		zone->uz_count = keg->uk_ipers;
1546 	else
1547 		zone->uz_count = BUCKET_MAX;
1548 	return (0);
1549 }
1550 
1551 /*
1552  * Keg header dtor.  This frees all data, destroys locks, frees the hash
1553  * table and removes the keg from the global list.
1554  *
1555  * Arguments/Returns follow uma_dtor specifications
1556  *	udata  unused
1557  */
1558 static void
1559 keg_dtor(void *arg, int size, void *udata)
1560 {
1561 	uma_keg_t keg;
1562 
1563 	keg = (uma_keg_t)arg;
1564 	KEG_LOCK(keg);
1565 	if (keg->uk_free != 0) {
1566 		printf("Freed UMA keg was not empty (%d items). "
1567 		    " Lost %d pages of memory.\n",
1568 		    keg->uk_free, keg->uk_pages);
1569 	}
1570 	KEG_UNLOCK(keg);
1571 
1572 	hash_free(&keg->uk_hash);
1573 
1574 	KEG_LOCK_FINI(keg);
1575 }
1576 
1577 /*
1578  * Zone header dtor.
1579  *
1580  * Arguments/Returns follow uma_dtor specifications
1581  *	udata  unused
1582  */
1583 static void
1584 zone_dtor(void *arg, int size, void *udata)
1585 {
1586 	uma_klink_t klink;
1587 	uma_zone_t zone;
1588 	uma_keg_t keg;
1589 
1590 	zone = (uma_zone_t)arg;
1591 	keg = zone_first_keg(zone);
1592 
1593 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1594 		cache_drain(zone);
1595 
1596 	mtx_lock(&uma_mtx);
1597 	LIST_REMOVE(zone, uz_link);
1598 	mtx_unlock(&uma_mtx);
1599 	/*
1600 	 * XXX there are some races here where
1601 	 * the zone can be drained but zone lock
1602 	 * released and then refilled before we
1603 	 * remove it... we dont care for now
1604 	 */
1605 	zone_drain_wait(zone, M_WAITOK);
1606 	/*
1607 	 * Unlink all of our kegs.
1608 	 */
1609 	while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1610 		klink->kl_keg = NULL;
1611 		LIST_REMOVE(klink, kl_link);
1612 		if (klink == &zone->uz_klink)
1613 			continue;
1614 		free(klink, M_TEMP);
1615 	}
1616 	/*
1617 	 * We only destroy kegs from non secondary zones.
1618 	 */
1619 	if ((zone->uz_flags & UMA_ZONE_SECONDARY) == 0)  {
1620 		mtx_lock(&uma_mtx);
1621 		LIST_REMOVE(keg, uk_link);
1622 		mtx_unlock(&uma_mtx);
1623 		zone_free_item(kegs, keg, NULL, SKIP_NONE,
1624 		    ZFREE_STATFREE);
1625 	}
1626 }
1627 
1628 /*
1629  * Traverses every zone in the system and calls a callback
1630  *
1631  * Arguments:
1632  *	zfunc  A pointer to a function which accepts a zone
1633  *		as an argument.
1634  *
1635  * Returns:
1636  *	Nothing
1637  */
1638 static void
1639 zone_foreach(void (*zfunc)(uma_zone_t))
1640 {
1641 	uma_keg_t keg;
1642 	uma_zone_t zone;
1643 
1644 	mtx_lock(&uma_mtx);
1645 	LIST_FOREACH(keg, &uma_kegs, uk_link) {
1646 		LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1647 			zfunc(zone);
1648 	}
1649 	mtx_unlock(&uma_mtx);
1650 }
1651 
1652 /* Public functions */
1653 /* See uma.h */
1654 void
1655 uma_startup(void *bootmem, int boot_pages)
1656 {
1657 	struct uma_zctor_args args;
1658 	uma_slab_t slab;
1659 	u_int slabsize;
1660 	int i;
1661 
1662 #ifdef UMA_DEBUG
1663 	printf("Creating uma keg headers zone and keg.\n");
1664 #endif
1665 	mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
1666 
1667 	/* "manually" create the initial zone */
1668 	args.name = "UMA Kegs";
1669 	args.size = sizeof(struct uma_keg);
1670 	args.ctor = keg_ctor;
1671 	args.dtor = keg_dtor;
1672 	args.uminit = zero_init;
1673 	args.fini = NULL;
1674 	args.keg = &masterkeg;
1675 	args.align = 32 - 1;
1676 	args.flags = UMA_ZFLAG_INTERNAL;
1677 	/* The initial zone has no Per cpu queues so it's smaller */
1678 	zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
1679 
1680 #ifdef UMA_DEBUG
1681 	printf("Filling boot free list.\n");
1682 #endif
1683 	for (i = 0; i < boot_pages; i++) {
1684 		slab = (uma_slab_t)((uint8_t *)bootmem + (i * UMA_SLAB_SIZE));
1685 		slab->us_data = (uint8_t *)slab;
1686 		slab->us_flags = UMA_SLAB_BOOT;
1687 		LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
1688 	}
1689 	mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
1690 
1691 #ifdef UMA_DEBUG
1692 	printf("Creating uma zone headers zone and keg.\n");
1693 #endif
1694 	args.name = "UMA Zones";
1695 	args.size = sizeof(struct uma_zone) +
1696 	    (sizeof(struct uma_cache) * (mp_maxid + 1));
1697 	args.ctor = zone_ctor;
1698 	args.dtor = zone_dtor;
1699 	args.uminit = zero_init;
1700 	args.fini = NULL;
1701 	args.keg = NULL;
1702 	args.align = 32 - 1;
1703 	args.flags = UMA_ZFLAG_INTERNAL;
1704 	/* The initial zone has no Per cpu queues so it's smaller */
1705 	zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1706 
1707 #ifdef UMA_DEBUG
1708 	printf("Initializing pcpu cache locks.\n");
1709 #endif
1710 #ifdef UMA_DEBUG
1711 	printf("Creating slab and hash zones.\n");
1712 #endif
1713 
1714 	/* Now make a zone for slab headers */
1715 	slabzone = uma_zcreate("UMA Slabs",
1716 				sizeof(struct uma_slab),
1717 				NULL, NULL, NULL, NULL,
1718 				UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1719 
1720 	/*
1721 	 * We also create a zone for the bigger slabs with reference
1722 	 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1723 	 */
1724 	slabsize = sizeof(struct uma_slab_refcnt);
1725 	slabsize += uma_max_ipers_ref * sizeof(uint32_t);
1726 	slabrefzone = uma_zcreate("UMA RCntSlabs",
1727 				  slabsize,
1728 				  NULL, NULL, NULL, NULL,
1729 				  UMA_ALIGN_PTR,
1730 				  UMA_ZFLAG_INTERNAL);
1731 
1732 	hashzone = uma_zcreate("UMA Hash",
1733 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1734 	    NULL, NULL, NULL, NULL,
1735 	    UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1736 
1737 	bucket_init();
1738 
1739 	booted = UMA_STARTUP;
1740 
1741 #ifdef UMA_DEBUG
1742 	printf("UMA startup complete.\n");
1743 #endif
1744 }
1745 
1746 /* see uma.h */
1747 void
1748 uma_startup2(void)
1749 {
1750 	booted = UMA_STARTUP2;
1751 	bucket_enable();
1752 #ifdef UMA_DEBUG
1753 	printf("UMA startup2 complete.\n");
1754 #endif
1755 }
1756 
1757 /*
1758  * Initialize our callout handle
1759  *
1760  */
1761 
1762 static void
1763 uma_startup3(void)
1764 {
1765 #ifdef UMA_DEBUG
1766 	printf("Starting callout.\n");
1767 #endif
1768 	callout_init(&uma_callout, CALLOUT_MPSAFE);
1769 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1770 #ifdef UMA_DEBUG
1771 	printf("UMA startup3 complete.\n");
1772 #endif
1773 }
1774 
1775 static uma_keg_t
1776 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
1777 		int align, uint32_t flags)
1778 {
1779 	struct uma_kctor_args args;
1780 
1781 	args.size = size;
1782 	args.uminit = uminit;
1783 	args.fini = fini;
1784 	args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1785 	args.flags = flags;
1786 	args.zone = zone;
1787 	return (zone_alloc_item(kegs, &args, M_WAITOK));
1788 }
1789 
1790 /* See uma.h */
1791 void
1792 uma_set_align(int align)
1793 {
1794 
1795 	if (align != UMA_ALIGN_CACHE)
1796 		uma_align_cache = align;
1797 }
1798 
1799 /* See uma.h */
1800 uma_zone_t
1801 uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1802 		uma_init uminit, uma_fini fini, int align, uint32_t flags)
1803 
1804 {
1805 	struct uma_zctor_args args;
1806 
1807 	/* This stuff is essential for the zone ctor */
1808 	args.name = name;
1809 	args.size = size;
1810 	args.ctor = ctor;
1811 	args.dtor = dtor;
1812 	args.uminit = uminit;
1813 	args.fini = fini;
1814 	args.align = align;
1815 	args.flags = flags;
1816 	args.keg = NULL;
1817 
1818 	return (zone_alloc_item(zones, &args, M_WAITOK));
1819 }
1820 
1821 /* See uma.h */
1822 uma_zone_t
1823 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1824 		    uma_init zinit, uma_fini zfini, uma_zone_t master)
1825 {
1826 	struct uma_zctor_args args;
1827 	uma_keg_t keg;
1828 
1829 	keg = zone_first_keg(master);
1830 	args.name = name;
1831 	args.size = keg->uk_size;
1832 	args.ctor = ctor;
1833 	args.dtor = dtor;
1834 	args.uminit = zinit;
1835 	args.fini = zfini;
1836 	args.align = keg->uk_align;
1837 	args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
1838 	args.keg = keg;
1839 
1840 	/* XXX Attaches only one keg of potentially many. */
1841 	return (zone_alloc_item(zones, &args, M_WAITOK));
1842 }
1843 
1844 static void
1845 zone_lock_pair(uma_zone_t a, uma_zone_t b)
1846 {
1847 	if (a < b) {
1848 		ZONE_LOCK(a);
1849 		mtx_lock_flags(b->uz_lock, MTX_DUPOK);
1850 	} else {
1851 		ZONE_LOCK(b);
1852 		mtx_lock_flags(a->uz_lock, MTX_DUPOK);
1853 	}
1854 }
1855 
1856 static void
1857 zone_unlock_pair(uma_zone_t a, uma_zone_t b)
1858 {
1859 
1860 	ZONE_UNLOCK(a);
1861 	ZONE_UNLOCK(b);
1862 }
1863 
1864 int
1865 uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
1866 {
1867 	uma_klink_t klink;
1868 	uma_klink_t kl;
1869 	int error;
1870 
1871 	error = 0;
1872 	klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
1873 
1874 	zone_lock_pair(zone, master);
1875 	/*
1876 	 * zone must use vtoslab() to resolve objects and must already be
1877 	 * a secondary.
1878 	 */
1879 	if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
1880 	    != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
1881 		error = EINVAL;
1882 		goto out;
1883 	}
1884 	/*
1885 	 * The new master must also use vtoslab().
1886 	 */
1887 	if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
1888 		error = EINVAL;
1889 		goto out;
1890 	}
1891 	/*
1892 	 * Both must either be refcnt, or not be refcnt.
1893 	 */
1894 	if ((zone->uz_flags & UMA_ZONE_REFCNT) !=
1895 	    (master->uz_flags & UMA_ZONE_REFCNT)) {
1896 		error = EINVAL;
1897 		goto out;
1898 	}
1899 	/*
1900 	 * The underlying object must be the same size.  rsize
1901 	 * may be different.
1902 	 */
1903 	if (master->uz_size != zone->uz_size) {
1904 		error = E2BIG;
1905 		goto out;
1906 	}
1907 	/*
1908 	 * Put it at the end of the list.
1909 	 */
1910 	klink->kl_keg = zone_first_keg(master);
1911 	LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
1912 		if (LIST_NEXT(kl, kl_link) == NULL) {
1913 			LIST_INSERT_AFTER(kl, klink, kl_link);
1914 			break;
1915 		}
1916 	}
1917 	klink = NULL;
1918 	zone->uz_flags |= UMA_ZFLAG_MULTI;
1919 	zone->uz_slab = zone_fetch_slab_multi;
1920 
1921 out:
1922 	zone_unlock_pair(zone, master);
1923 	if (klink != NULL)
1924 		free(klink, M_TEMP);
1925 
1926 	return (error);
1927 }
1928 
1929 
1930 /* See uma.h */
1931 void
1932 uma_zdestroy(uma_zone_t zone)
1933 {
1934 
1935 	zone_free_item(zones, zone, NULL, SKIP_NONE, ZFREE_STATFREE);
1936 }
1937 
1938 /* See uma.h */
1939 void *
1940 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
1941 {
1942 	void *item;
1943 	uma_cache_t cache;
1944 	uma_bucket_t bucket;
1945 	int cpu;
1946 
1947 	/* This is the fast path allocation */
1948 #ifdef UMA_DEBUG_ALLOC_1
1949 	printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
1950 #endif
1951 	CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
1952 	    zone->uz_name, flags);
1953 
1954 	if (flags & M_WAITOK) {
1955 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1956 		    "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
1957 	}
1958 #ifdef DEBUG_MEMGUARD
1959 	if (memguard_cmp_zone(zone)) {
1960 		item = memguard_alloc(zone->uz_size, flags);
1961 		if (item != NULL) {
1962 			/*
1963 			 * Avoid conflict with the use-after-free
1964 			 * protecting infrastructure from INVARIANTS.
1965 			 */
1966 			if (zone->uz_init != NULL &&
1967 			    zone->uz_init != mtrash_init &&
1968 			    zone->uz_init(item, zone->uz_size, flags) != 0)
1969 				return (NULL);
1970 			if (zone->uz_ctor != NULL &&
1971 			    zone->uz_ctor != mtrash_ctor &&
1972 			    zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
1973 			    	zone->uz_fini(item, zone->uz_size);
1974 				return (NULL);
1975 			}
1976 			return (item);
1977 		}
1978 		/* This is unfortunate but should not be fatal. */
1979 	}
1980 #endif
1981 	/*
1982 	 * If possible, allocate from the per-CPU cache.  There are two
1983 	 * requirements for safe access to the per-CPU cache: (1) the thread
1984 	 * accessing the cache must not be preempted or yield during access,
1985 	 * and (2) the thread must not migrate CPUs without switching which
1986 	 * cache it accesses.  We rely on a critical section to prevent
1987 	 * preemption and migration.  We release the critical section in
1988 	 * order to acquire the zone mutex if we are unable to allocate from
1989 	 * the current cache; when we re-acquire the critical section, we
1990 	 * must detect and handle migration if it has occurred.
1991 	 */
1992 zalloc_restart:
1993 	critical_enter();
1994 	cpu = curcpu;
1995 	cache = &zone->uz_cpu[cpu];
1996 
1997 zalloc_start:
1998 	bucket = cache->uc_allocbucket;
1999 
2000 	if (bucket) {
2001 		if (bucket->ub_cnt > 0) {
2002 			bucket->ub_cnt--;
2003 			item = bucket->ub_bucket[bucket->ub_cnt];
2004 #ifdef INVARIANTS
2005 			bucket->ub_bucket[bucket->ub_cnt] = NULL;
2006 #endif
2007 			KASSERT(item != NULL,
2008 			    ("uma_zalloc: Bucket pointer mangled."));
2009 			cache->uc_allocs++;
2010 			critical_exit();
2011 			if (zone->uz_ctor != NULL) {
2012 				if (zone->uz_ctor(item, zone->uz_size,
2013 				    udata, flags) != 0) {
2014 					zone_free_item(zone, item, udata,
2015 					    SKIP_DTOR, ZFREE_STATFAIL |
2016 					    ZFREE_STATFREE);
2017 					return (NULL);
2018 				}
2019 			}
2020 #ifdef INVARIANTS
2021 			uma_dbg_alloc(zone, NULL, item);
2022 #endif
2023 			if (flags & M_ZERO)
2024 				bzero(item, zone->uz_size);
2025 			return (item);
2026 		} else if (cache->uc_freebucket) {
2027 			/*
2028 			 * We have run out of items in our allocbucket.
2029 			 * See if we can switch with our free bucket.
2030 			 */
2031 			if (cache->uc_freebucket->ub_cnt > 0) {
2032 #ifdef UMA_DEBUG_ALLOC
2033 				printf("uma_zalloc: Swapping empty with"
2034 				    " alloc.\n");
2035 #endif
2036 				bucket = cache->uc_freebucket;
2037 				cache->uc_freebucket = cache->uc_allocbucket;
2038 				cache->uc_allocbucket = bucket;
2039 
2040 				goto zalloc_start;
2041 			}
2042 		}
2043 	}
2044 	/*
2045 	 * Attempt to retrieve the item from the per-CPU cache has failed, so
2046 	 * we must go back to the zone.  This requires the zone lock, so we
2047 	 * must drop the critical section, then re-acquire it when we go back
2048 	 * to the cache.  Since the critical section is released, we may be
2049 	 * preempted or migrate.  As such, make sure not to maintain any
2050 	 * thread-local state specific to the cache from prior to releasing
2051 	 * the critical section.
2052 	 */
2053 	critical_exit();
2054 	ZONE_LOCK(zone);
2055 	critical_enter();
2056 	cpu = curcpu;
2057 	cache = &zone->uz_cpu[cpu];
2058 	bucket = cache->uc_allocbucket;
2059 	if (bucket != NULL) {
2060 		if (bucket->ub_cnt > 0) {
2061 			ZONE_UNLOCK(zone);
2062 			goto zalloc_start;
2063 		}
2064 		bucket = cache->uc_freebucket;
2065 		if (bucket != NULL && bucket->ub_cnt > 0) {
2066 			ZONE_UNLOCK(zone);
2067 			goto zalloc_start;
2068 		}
2069 	}
2070 
2071 	/* Since we have locked the zone we may as well send back our stats */
2072 	zone->uz_allocs += cache->uc_allocs;
2073 	cache->uc_allocs = 0;
2074 	zone->uz_frees += cache->uc_frees;
2075 	cache->uc_frees = 0;
2076 
2077 	/* Our old one is now a free bucket */
2078 	if (cache->uc_allocbucket) {
2079 		KASSERT(cache->uc_allocbucket->ub_cnt == 0,
2080 		    ("uma_zalloc_arg: Freeing a non free bucket."));
2081 		LIST_INSERT_HEAD(&zone->uz_free_bucket,
2082 		    cache->uc_allocbucket, ub_link);
2083 		cache->uc_allocbucket = NULL;
2084 	}
2085 
2086 	/* Check the free list for a new alloc bucket */
2087 	if ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
2088 		KASSERT(bucket->ub_cnt != 0,
2089 		    ("uma_zalloc_arg: Returning an empty bucket."));
2090 
2091 		LIST_REMOVE(bucket, ub_link);
2092 		cache->uc_allocbucket = bucket;
2093 		ZONE_UNLOCK(zone);
2094 		goto zalloc_start;
2095 	}
2096 	/* We are no longer associated with this CPU. */
2097 	critical_exit();
2098 
2099 	/* Bump up our uz_count so we get here less */
2100 	if (zone->uz_count < BUCKET_MAX)
2101 		zone->uz_count++;
2102 
2103 	/*
2104 	 * Now lets just fill a bucket and put it on the free list.  If that
2105 	 * works we'll restart the allocation from the begining.
2106 	 */
2107 	if (zone_alloc_bucket(zone, flags)) {
2108 		ZONE_UNLOCK(zone);
2109 		goto zalloc_restart;
2110 	}
2111 	ZONE_UNLOCK(zone);
2112 	/*
2113 	 * We may not be able to get a bucket so return an actual item.
2114 	 */
2115 #ifdef UMA_DEBUG
2116 	printf("uma_zalloc_arg: Bucketzone returned NULL\n");
2117 #endif
2118 
2119 	item = zone_alloc_item(zone, udata, flags);
2120 	return (item);
2121 }
2122 
2123 static uma_slab_t
2124 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags)
2125 {
2126 	uma_slab_t slab;
2127 
2128 	mtx_assert(&keg->uk_lock, MA_OWNED);
2129 	slab = NULL;
2130 
2131 	for (;;) {
2132 		/*
2133 		 * Find a slab with some space.  Prefer slabs that are partially
2134 		 * used over those that are totally full.  This helps to reduce
2135 		 * fragmentation.
2136 		 */
2137 		if (keg->uk_free != 0) {
2138 			if (!LIST_EMPTY(&keg->uk_part_slab)) {
2139 				slab = LIST_FIRST(&keg->uk_part_slab);
2140 			} else {
2141 				slab = LIST_FIRST(&keg->uk_free_slab);
2142 				LIST_REMOVE(slab, us_link);
2143 				LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2144 				    us_link);
2145 			}
2146 			MPASS(slab->us_keg == keg);
2147 			return (slab);
2148 		}
2149 
2150 		/*
2151 		 * M_NOVM means don't ask at all!
2152 		 */
2153 		if (flags & M_NOVM)
2154 			break;
2155 
2156 		if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2157 			keg->uk_flags |= UMA_ZFLAG_FULL;
2158 			/*
2159 			 * If this is not a multi-zone, set the FULL bit.
2160 			 * Otherwise slab_multi() takes care of it.
2161 			 */
2162 			if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) {
2163 				zone->uz_flags |= UMA_ZFLAG_FULL;
2164 				zone_log_warning(zone);
2165 			}
2166 			if (flags & M_NOWAIT)
2167 				break;
2168 			zone->uz_sleeps++;
2169 			msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2170 			continue;
2171 		}
2172 		keg->uk_recurse++;
2173 		slab = keg_alloc_slab(keg, zone, flags);
2174 		keg->uk_recurse--;
2175 		/*
2176 		 * If we got a slab here it's safe to mark it partially used
2177 		 * and return.  We assume that the caller is going to remove
2178 		 * at least one item.
2179 		 */
2180 		if (slab) {
2181 			MPASS(slab->us_keg == keg);
2182 			LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2183 			return (slab);
2184 		}
2185 		/*
2186 		 * We might not have been able to get a slab but another cpu
2187 		 * could have while we were unlocked.  Check again before we
2188 		 * fail.
2189 		 */
2190 		flags |= M_NOVM;
2191 	}
2192 	return (slab);
2193 }
2194 
2195 static inline void
2196 zone_relock(uma_zone_t zone, uma_keg_t keg)
2197 {
2198 	if (zone->uz_lock != &keg->uk_lock) {
2199 		KEG_UNLOCK(keg);
2200 		ZONE_LOCK(zone);
2201 	}
2202 }
2203 
2204 static inline void
2205 keg_relock(uma_keg_t keg, uma_zone_t zone)
2206 {
2207 	if (zone->uz_lock != &keg->uk_lock) {
2208 		ZONE_UNLOCK(zone);
2209 		KEG_LOCK(keg);
2210 	}
2211 }
2212 
2213 static uma_slab_t
2214 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags)
2215 {
2216 	uma_slab_t slab;
2217 
2218 	if (keg == NULL)
2219 		keg = zone_first_keg(zone);
2220 	/*
2221 	 * This is to prevent us from recursively trying to allocate
2222 	 * buckets.  The problem is that if an allocation forces us to
2223 	 * grab a new bucket we will call page_alloc, which will go off
2224 	 * and cause the vm to allocate vm_map_entries.  If we need new
2225 	 * buckets there too we will recurse in kmem_alloc and bad
2226 	 * things happen.  So instead we return a NULL bucket, and make
2227 	 * the code that allocates buckets smart enough to deal with it
2228 	 */
2229 	if (keg->uk_flags & UMA_ZFLAG_BUCKET && keg->uk_recurse != 0)
2230 		return (NULL);
2231 
2232 	for (;;) {
2233 		slab = keg_fetch_slab(keg, zone, flags);
2234 		if (slab)
2235 			return (slab);
2236 		if (flags & (M_NOWAIT | M_NOVM))
2237 			break;
2238 	}
2239 	return (NULL);
2240 }
2241 
2242 /*
2243  * uma_zone_fetch_slab_multi:  Fetches a slab from one available keg.  Returns
2244  * with the keg locked.  Caller must call zone_relock() afterwards if the
2245  * zone lock is required.  On NULL the zone lock is held.
2246  *
2247  * The last pointer is used to seed the search.  It is not required.
2248  */
2249 static uma_slab_t
2250 zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags)
2251 {
2252 	uma_klink_t klink;
2253 	uma_slab_t slab;
2254 	uma_keg_t keg;
2255 	int flags;
2256 	int empty;
2257 	int full;
2258 
2259 	/*
2260 	 * Don't wait on the first pass.  This will skip limit tests
2261 	 * as well.  We don't want to block if we can find a provider
2262 	 * without blocking.
2263 	 */
2264 	flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2265 	/*
2266 	 * Use the last slab allocated as a hint for where to start
2267 	 * the search.
2268 	 */
2269 	if (last) {
2270 		slab = keg_fetch_slab(last, zone, flags);
2271 		if (slab)
2272 			return (slab);
2273 		zone_relock(zone, last);
2274 		last = NULL;
2275 	}
2276 	/*
2277 	 * Loop until we have a slab incase of transient failures
2278 	 * while M_WAITOK is specified.  I'm not sure this is 100%
2279 	 * required but we've done it for so long now.
2280 	 */
2281 	for (;;) {
2282 		empty = 0;
2283 		full = 0;
2284 		/*
2285 		 * Search the available kegs for slabs.  Be careful to hold the
2286 		 * correct lock while calling into the keg layer.
2287 		 */
2288 		LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2289 			keg = klink->kl_keg;
2290 			keg_relock(keg, zone);
2291 			if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2292 				slab = keg_fetch_slab(keg, zone, flags);
2293 				if (slab)
2294 					return (slab);
2295 			}
2296 			if (keg->uk_flags & UMA_ZFLAG_FULL)
2297 				full++;
2298 			else
2299 				empty++;
2300 			zone_relock(zone, keg);
2301 		}
2302 		if (rflags & (M_NOWAIT | M_NOVM))
2303 			break;
2304 		flags = rflags;
2305 		/*
2306 		 * All kegs are full.  XXX We can't atomically check all kegs
2307 		 * and sleep so just sleep for a short period and retry.
2308 		 */
2309 		if (full && !empty) {
2310 			zone->uz_flags |= UMA_ZFLAG_FULL;
2311 			zone->uz_sleeps++;
2312 			zone_log_warning(zone);
2313 			msleep(zone, zone->uz_lock, PVM, "zonelimit", hz/100);
2314 			zone->uz_flags &= ~UMA_ZFLAG_FULL;
2315 			continue;
2316 		}
2317 	}
2318 	return (NULL);
2319 }
2320 
2321 static void *
2322 slab_alloc_item(uma_zone_t zone, uma_slab_t slab)
2323 {
2324 	uma_keg_t keg;
2325 	void *item;
2326 	uint8_t freei;
2327 
2328 	keg = slab->us_keg;
2329 	mtx_assert(&keg->uk_lock, MA_OWNED);
2330 
2331 	freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2332 	BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2333 	item = slab->us_data + (keg->uk_rsize * freei);
2334 	slab->us_freecount--;
2335 	keg->uk_free--;
2336 
2337 	/* Move this slab to the full list */
2338 	if (slab->us_freecount == 0) {
2339 		LIST_REMOVE(slab, us_link);
2340 		LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2341 	}
2342 
2343 	return (item);
2344 }
2345 
2346 static int
2347 zone_alloc_bucket(uma_zone_t zone, int flags)
2348 {
2349 	uma_bucket_t bucket;
2350 	uma_slab_t slab;
2351 	uma_keg_t keg;
2352 	int16_t saved;
2353 	int max, origflags = flags;
2354 
2355 	/*
2356 	 * Try this zone's free list first so we don't allocate extra buckets.
2357 	 */
2358 	if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2359 		KASSERT(bucket->ub_cnt == 0,
2360 		    ("zone_alloc_bucket: Bucket on free list is not empty."));
2361 		LIST_REMOVE(bucket, ub_link);
2362 	} else {
2363 		int bflags;
2364 
2365 		bflags = (flags & ~M_ZERO);
2366 		if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
2367 			bflags |= M_NOVM;
2368 
2369 		ZONE_UNLOCK(zone);
2370 		bucket = bucket_alloc(zone->uz_count, bflags);
2371 		ZONE_LOCK(zone);
2372 	}
2373 
2374 	if (bucket == NULL) {
2375 		return (0);
2376 	}
2377 
2378 #ifdef SMP
2379 	/*
2380 	 * This code is here to limit the number of simultaneous bucket fills
2381 	 * for any given zone to the number of per cpu caches in this zone. This
2382 	 * is done so that we don't allocate more memory than we really need.
2383 	 */
2384 	if (zone->uz_fills >= mp_ncpus)
2385 		goto done;
2386 
2387 #endif
2388 	zone->uz_fills++;
2389 
2390 	max = MIN(bucket->ub_entries, zone->uz_count);
2391 	/* Try to keep the buckets totally full */
2392 	saved = bucket->ub_cnt;
2393 	slab = NULL;
2394 	keg = NULL;
2395 	while (bucket->ub_cnt < max &&
2396 	    (slab = zone->uz_slab(zone, keg, flags)) != NULL) {
2397 		keg = slab->us_keg;
2398 		while (slab->us_freecount && bucket->ub_cnt < max) {
2399 			bucket->ub_bucket[bucket->ub_cnt++] =
2400 			    slab_alloc_item(zone, slab);
2401 		}
2402 
2403 		/* Don't block on the next fill */
2404 		flags |= M_NOWAIT;
2405 	}
2406 	if (slab)
2407 		zone_relock(zone, keg);
2408 
2409 	/*
2410 	 * We unlock here because we need to call the zone's init.
2411 	 * It should be safe to unlock because the slab dealt with
2412 	 * above is already on the appropriate list within the keg
2413 	 * and the bucket we filled is not yet on any list, so we
2414 	 * own it.
2415 	 */
2416 	if (zone->uz_init != NULL) {
2417 		int i;
2418 
2419 		ZONE_UNLOCK(zone);
2420 		for (i = saved; i < bucket->ub_cnt; i++)
2421 			if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
2422 			    origflags) != 0)
2423 				break;
2424 		/*
2425 		 * If we couldn't initialize the whole bucket, put the
2426 		 * rest back onto the freelist.
2427 		 */
2428 		if (i != bucket->ub_cnt) {
2429 			int j;
2430 
2431 			for (j = i; j < bucket->ub_cnt; j++) {
2432 				zone_free_item(zone, bucket->ub_bucket[j],
2433 				    NULL, SKIP_FINI, 0);
2434 #ifdef INVARIANTS
2435 				bucket->ub_bucket[j] = NULL;
2436 #endif
2437 			}
2438 			bucket->ub_cnt = i;
2439 		}
2440 		ZONE_LOCK(zone);
2441 	}
2442 
2443 	zone->uz_fills--;
2444 	if (bucket->ub_cnt != 0) {
2445 		LIST_INSERT_HEAD(&zone->uz_full_bucket,
2446 		    bucket, ub_link);
2447 		return (1);
2448 	}
2449 #ifdef SMP
2450 done:
2451 #endif
2452 	bucket_free(bucket);
2453 
2454 	return (0);
2455 }
2456 /*
2457  * Allocates an item for an internal zone
2458  *
2459  * Arguments
2460  *	zone   The zone to alloc for.
2461  *	udata  The data to be passed to the constructor.
2462  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
2463  *
2464  * Returns
2465  *	NULL if there is no memory and M_NOWAIT is set
2466  *	An item if successful
2467  */
2468 
2469 static void *
2470 zone_alloc_item(uma_zone_t zone, void *udata, int flags)
2471 {
2472 	uma_slab_t slab;
2473 	void *item;
2474 
2475 	item = NULL;
2476 
2477 #ifdef UMA_DEBUG_ALLOC
2478 	printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
2479 #endif
2480 	ZONE_LOCK(zone);
2481 
2482 	slab = zone->uz_slab(zone, NULL, flags);
2483 	if (slab == NULL) {
2484 		zone->uz_fails++;
2485 		ZONE_UNLOCK(zone);
2486 		return (NULL);
2487 	}
2488 
2489 	item = slab_alloc_item(zone, slab);
2490 
2491 	zone_relock(zone, slab->us_keg);
2492 	zone->uz_allocs++;
2493 	ZONE_UNLOCK(zone);
2494 
2495 	/*
2496 	 * We have to call both the zone's init (not the keg's init)
2497 	 * and the zone's ctor.  This is because the item is going from
2498 	 * a keg slab directly to the user, and the user is expecting it
2499 	 * to be both zone-init'd as well as zone-ctor'd.
2500 	 */
2501 	if (zone->uz_init != NULL) {
2502 		if (zone->uz_init(item, zone->uz_size, flags) != 0) {
2503 			zone_free_item(zone, item, udata, SKIP_FINI,
2504 			    ZFREE_STATFAIL | ZFREE_STATFREE);
2505 			return (NULL);
2506 		}
2507 	}
2508 	if (zone->uz_ctor != NULL) {
2509 		if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2510 			zone_free_item(zone, item, udata, SKIP_DTOR,
2511 			    ZFREE_STATFAIL | ZFREE_STATFREE);
2512 			return (NULL);
2513 		}
2514 	}
2515 #ifdef INVARIANTS
2516 	uma_dbg_alloc(zone, slab, item);
2517 #endif
2518 	if (flags & M_ZERO)
2519 		bzero(item, zone->uz_size);
2520 
2521 	return (item);
2522 }
2523 
2524 /* See uma.h */
2525 void
2526 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
2527 {
2528 	uma_cache_t cache;
2529 	uma_bucket_t bucket;
2530 	int bflags;
2531 	int cpu;
2532 
2533 #ifdef UMA_DEBUG_ALLOC_1
2534 	printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
2535 #endif
2536 	CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
2537 	    zone->uz_name);
2538 
2539         /* uma_zfree(..., NULL) does nothing, to match free(9). */
2540         if (item == NULL)
2541                 return;
2542 #ifdef DEBUG_MEMGUARD
2543 	if (is_memguard_addr(item)) {
2544 		if (zone->uz_dtor != NULL && zone->uz_dtor != mtrash_dtor)
2545 			zone->uz_dtor(item, zone->uz_size, udata);
2546 		if (zone->uz_fini != NULL && zone->uz_fini != mtrash_fini)
2547 			zone->uz_fini(item, zone->uz_size);
2548 		memguard_free(item);
2549 		return;
2550 	}
2551 #endif
2552 #ifdef INVARIANTS
2553 	if (zone->uz_flags & UMA_ZONE_MALLOC)
2554 		uma_dbg_free(zone, udata, item);
2555 	else
2556 		uma_dbg_free(zone, NULL, item);
2557 #endif
2558 	if (zone->uz_dtor)
2559 		zone->uz_dtor(item, zone->uz_size, udata);
2560 
2561 	/*
2562 	 * The race here is acceptable.  If we miss it we'll just have to wait
2563 	 * a little longer for the limits to be reset.
2564 	 */
2565 	if (zone->uz_flags & UMA_ZFLAG_FULL)
2566 		goto zfree_internal;
2567 
2568 	/*
2569 	 * If possible, free to the per-CPU cache.  There are two
2570 	 * requirements for safe access to the per-CPU cache: (1) the thread
2571 	 * accessing the cache must not be preempted or yield during access,
2572 	 * and (2) the thread must not migrate CPUs without switching which
2573 	 * cache it accesses.  We rely on a critical section to prevent
2574 	 * preemption and migration.  We release the critical section in
2575 	 * order to acquire the zone mutex if we are unable to free to the
2576 	 * current cache; when we re-acquire the critical section, we must
2577 	 * detect and handle migration if it has occurred.
2578 	 */
2579 zfree_restart:
2580 	critical_enter();
2581 	cpu = curcpu;
2582 	cache = &zone->uz_cpu[cpu];
2583 
2584 zfree_start:
2585 	bucket = cache->uc_freebucket;
2586 
2587 	if (bucket) {
2588 		/*
2589 		 * Do we have room in our bucket? It is OK for this uz count
2590 		 * check to be slightly out of sync.
2591 		 */
2592 
2593 		if (bucket->ub_cnt < bucket->ub_entries) {
2594 			KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
2595 			    ("uma_zfree: Freeing to non free bucket index."));
2596 			bucket->ub_bucket[bucket->ub_cnt] = item;
2597 			bucket->ub_cnt++;
2598 			cache->uc_frees++;
2599 			critical_exit();
2600 			return;
2601 		} else if (cache->uc_allocbucket) {
2602 #ifdef UMA_DEBUG_ALLOC
2603 			printf("uma_zfree: Swapping buckets.\n");
2604 #endif
2605 			/*
2606 			 * We have run out of space in our freebucket.
2607 			 * See if we can switch with our alloc bucket.
2608 			 */
2609 			if (cache->uc_allocbucket->ub_cnt <
2610 			    cache->uc_freebucket->ub_cnt) {
2611 				bucket = cache->uc_freebucket;
2612 				cache->uc_freebucket = cache->uc_allocbucket;
2613 				cache->uc_allocbucket = bucket;
2614 				goto zfree_start;
2615 			}
2616 		}
2617 	}
2618 	/*
2619 	 * We can get here for two reasons:
2620 	 *
2621 	 * 1) The buckets are NULL
2622 	 * 2) The alloc and free buckets are both somewhat full.
2623 	 *
2624 	 * We must go back the zone, which requires acquiring the zone lock,
2625 	 * which in turn means we must release and re-acquire the critical
2626 	 * section.  Since the critical section is released, we may be
2627 	 * preempted or migrate.  As such, make sure not to maintain any
2628 	 * thread-local state specific to the cache from prior to releasing
2629 	 * the critical section.
2630 	 */
2631 	critical_exit();
2632 	ZONE_LOCK(zone);
2633 	critical_enter();
2634 	cpu = curcpu;
2635 	cache = &zone->uz_cpu[cpu];
2636 	if (cache->uc_freebucket != NULL) {
2637 		if (cache->uc_freebucket->ub_cnt <
2638 		    cache->uc_freebucket->ub_entries) {
2639 			ZONE_UNLOCK(zone);
2640 			goto zfree_start;
2641 		}
2642 		if (cache->uc_allocbucket != NULL &&
2643 		    (cache->uc_allocbucket->ub_cnt <
2644 		    cache->uc_freebucket->ub_cnt)) {
2645 			ZONE_UNLOCK(zone);
2646 			goto zfree_start;
2647 		}
2648 	}
2649 
2650 	/* Since we have locked the zone we may as well send back our stats */
2651 	zone->uz_allocs += cache->uc_allocs;
2652 	cache->uc_allocs = 0;
2653 	zone->uz_frees += cache->uc_frees;
2654 	cache->uc_frees = 0;
2655 
2656 	bucket = cache->uc_freebucket;
2657 	cache->uc_freebucket = NULL;
2658 
2659 	/* Can we throw this on the zone full list? */
2660 	if (bucket != NULL) {
2661 #ifdef UMA_DEBUG_ALLOC
2662 		printf("uma_zfree: Putting old bucket on the free list.\n");
2663 #endif
2664 		/* ub_cnt is pointing to the last free item */
2665 		KASSERT(bucket->ub_cnt != 0,
2666 		    ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2667 		LIST_INSERT_HEAD(&zone->uz_full_bucket,
2668 		    bucket, ub_link);
2669 	}
2670 	if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2671 		LIST_REMOVE(bucket, ub_link);
2672 		ZONE_UNLOCK(zone);
2673 		cache->uc_freebucket = bucket;
2674 		goto zfree_start;
2675 	}
2676 	/* We are no longer associated with this CPU. */
2677 	critical_exit();
2678 
2679 	/* And the zone.. */
2680 	ZONE_UNLOCK(zone);
2681 
2682 #ifdef UMA_DEBUG_ALLOC
2683 	printf("uma_zfree: Allocating new free bucket.\n");
2684 #endif
2685 	bflags = M_NOWAIT;
2686 
2687 	if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
2688 		bflags |= M_NOVM;
2689 	bucket = bucket_alloc(zone->uz_count, bflags);
2690 	if (bucket) {
2691 		ZONE_LOCK(zone);
2692 		LIST_INSERT_HEAD(&zone->uz_free_bucket,
2693 		    bucket, ub_link);
2694 		ZONE_UNLOCK(zone);
2695 		goto zfree_restart;
2696 	}
2697 
2698 	/*
2699 	 * If nothing else caught this, we'll just do an internal free.
2700 	 */
2701 zfree_internal:
2702 	zone_free_item(zone, item, udata, SKIP_DTOR, ZFREE_STATFREE);
2703 
2704 	return;
2705 }
2706 
2707 /*
2708  * Frees an item to an INTERNAL zone or allocates a free bucket
2709  *
2710  * Arguments:
2711  *	zone   The zone to free to
2712  *	item   The item we're freeing
2713  *	udata  User supplied data for the dtor
2714  *	skip   Skip dtors and finis
2715  */
2716 static void
2717 zone_free_item(uma_zone_t zone, void *item, void *udata,
2718     enum zfreeskip skip, int flags)
2719 {
2720 	uma_slab_t slab;
2721 	uma_keg_t keg;
2722 	uint8_t *mem;
2723 	uint8_t freei;
2724 	int clearfull;
2725 
2726 #ifdef INVARIANTS
2727 	if (skip == SKIP_NONE) {
2728 		if (zone->uz_flags & UMA_ZONE_MALLOC)
2729 			uma_dbg_free(zone, udata, item);
2730 		else
2731 			uma_dbg_free(zone, NULL, item);
2732 	}
2733 #endif
2734 	if (skip < SKIP_DTOR && zone->uz_dtor)
2735 		zone->uz_dtor(item, zone->uz_size, udata);
2736 
2737 	if (skip < SKIP_FINI && zone->uz_fini)
2738 		zone->uz_fini(item, zone->uz_size);
2739 
2740 	ZONE_LOCK(zone);
2741 
2742 	if (flags & ZFREE_STATFAIL)
2743 		zone->uz_fails++;
2744 	if (flags & ZFREE_STATFREE)
2745 		zone->uz_frees++;
2746 
2747 	if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
2748 		mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
2749 		keg = zone_first_keg(zone); /* Must only be one. */
2750 		if (zone->uz_flags & UMA_ZONE_HASH) {
2751 			slab = hash_sfind(&keg->uk_hash, mem);
2752 		} else {
2753 			mem += keg->uk_pgoff;
2754 			slab = (uma_slab_t)mem;
2755 		}
2756 	} else {
2757 		/* This prevents redundant lookups via free(). */
2758 		if ((zone->uz_flags & UMA_ZONE_MALLOC) && udata != NULL)
2759 			slab = (uma_slab_t)udata;
2760 		else
2761 			slab = vtoslab((vm_offset_t)item);
2762 		keg = slab->us_keg;
2763 		keg_relock(keg, zone);
2764 	}
2765 	MPASS(keg == slab->us_keg);
2766 
2767 	/* Do we need to remove from any lists? */
2768 	if (slab->us_freecount+1 == keg->uk_ipers) {
2769 		LIST_REMOVE(slab, us_link);
2770 		LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2771 	} else if (slab->us_freecount == 0) {
2772 		LIST_REMOVE(slab, us_link);
2773 		LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2774 	}
2775 
2776 	/* Slab management. */
2777 	freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
2778 	BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
2779 	slab->us_freecount++;
2780 
2781 	/* Keg statistics. */
2782 	keg->uk_free++;
2783 
2784 	clearfull = 0;
2785 	if (keg->uk_flags & UMA_ZFLAG_FULL) {
2786 		if (keg->uk_pages < keg->uk_maxpages) {
2787 			keg->uk_flags &= ~UMA_ZFLAG_FULL;
2788 			clearfull = 1;
2789 		}
2790 
2791 		/*
2792 		 * We can handle one more allocation. Since we're
2793 		 * clearing ZFLAG_FULL, wake up all procs blocked
2794 		 * on pages. This should be uncommon, so keeping this
2795 		 * simple for now (rather than adding count of blocked
2796 		 * threads etc).
2797 		 */
2798 		wakeup(keg);
2799 	}
2800 	if (clearfull) {
2801 		zone_relock(zone, keg);
2802 		zone->uz_flags &= ~UMA_ZFLAG_FULL;
2803 		wakeup(zone);
2804 		ZONE_UNLOCK(zone);
2805 	} else
2806 		KEG_UNLOCK(keg);
2807 
2808 }
2809 
2810 /* See uma.h */
2811 int
2812 uma_zone_set_max(uma_zone_t zone, int nitems)
2813 {
2814 	uma_keg_t keg;
2815 
2816 	ZONE_LOCK(zone);
2817 	keg = zone_first_keg(zone);
2818 	keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
2819 	if (keg->uk_maxpages * keg->uk_ipers < nitems)
2820 		keg->uk_maxpages += keg->uk_ppera;
2821 	nitems = keg->uk_maxpages * keg->uk_ipers;
2822 	ZONE_UNLOCK(zone);
2823 
2824 	return (nitems);
2825 }
2826 
2827 /* See uma.h */
2828 int
2829 uma_zone_get_max(uma_zone_t zone)
2830 {
2831 	int nitems;
2832 	uma_keg_t keg;
2833 
2834 	ZONE_LOCK(zone);
2835 	keg = zone_first_keg(zone);
2836 	nitems = keg->uk_maxpages * keg->uk_ipers;
2837 	ZONE_UNLOCK(zone);
2838 
2839 	return (nitems);
2840 }
2841 
2842 /* See uma.h */
2843 void
2844 uma_zone_set_warning(uma_zone_t zone, const char *warning)
2845 {
2846 
2847 	ZONE_LOCK(zone);
2848 	zone->uz_warning = warning;
2849 	ZONE_UNLOCK(zone);
2850 }
2851 
2852 /* See uma.h */
2853 int
2854 uma_zone_get_cur(uma_zone_t zone)
2855 {
2856 	int64_t nitems;
2857 	u_int i;
2858 
2859 	ZONE_LOCK(zone);
2860 	nitems = zone->uz_allocs - zone->uz_frees;
2861 	CPU_FOREACH(i) {
2862 		/*
2863 		 * See the comment in sysctl_vm_zone_stats() regarding the
2864 		 * safety of accessing the per-cpu caches. With the zone lock
2865 		 * held, it is safe, but can potentially result in stale data.
2866 		 */
2867 		nitems += zone->uz_cpu[i].uc_allocs -
2868 		    zone->uz_cpu[i].uc_frees;
2869 	}
2870 	ZONE_UNLOCK(zone);
2871 
2872 	return (nitems < 0 ? 0 : nitems);
2873 }
2874 
2875 /* See uma.h */
2876 void
2877 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2878 {
2879 	uma_keg_t keg;
2880 
2881 	ZONE_LOCK(zone);
2882 	keg = zone_first_keg(zone);
2883 	KASSERT(keg->uk_pages == 0,
2884 	    ("uma_zone_set_init on non-empty keg"));
2885 	keg->uk_init = uminit;
2886 	ZONE_UNLOCK(zone);
2887 }
2888 
2889 /* See uma.h */
2890 void
2891 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2892 {
2893 	uma_keg_t keg;
2894 
2895 	ZONE_LOCK(zone);
2896 	keg = zone_first_keg(zone);
2897 	KASSERT(keg->uk_pages == 0,
2898 	    ("uma_zone_set_fini on non-empty keg"));
2899 	keg->uk_fini = fini;
2900 	ZONE_UNLOCK(zone);
2901 }
2902 
2903 /* See uma.h */
2904 void
2905 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2906 {
2907 	ZONE_LOCK(zone);
2908 	KASSERT(zone_first_keg(zone)->uk_pages == 0,
2909 	    ("uma_zone_set_zinit on non-empty keg"));
2910 	zone->uz_init = zinit;
2911 	ZONE_UNLOCK(zone);
2912 }
2913 
2914 /* See uma.h */
2915 void
2916 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2917 {
2918 	ZONE_LOCK(zone);
2919 	KASSERT(zone_first_keg(zone)->uk_pages == 0,
2920 	    ("uma_zone_set_zfini on non-empty keg"));
2921 	zone->uz_fini = zfini;
2922 	ZONE_UNLOCK(zone);
2923 }
2924 
2925 /* See uma.h */
2926 /* XXX uk_freef is not actually used with the zone locked */
2927 void
2928 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
2929 {
2930 
2931 	ZONE_LOCK(zone);
2932 	zone_first_keg(zone)->uk_freef = freef;
2933 	ZONE_UNLOCK(zone);
2934 }
2935 
2936 /* See uma.h */
2937 /* XXX uk_allocf is not actually used with the zone locked */
2938 void
2939 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
2940 {
2941 	uma_keg_t keg;
2942 
2943 	ZONE_LOCK(zone);
2944 	keg = zone_first_keg(zone);
2945 	keg->uk_flags |= UMA_ZFLAG_PRIVALLOC;
2946 	keg->uk_allocf = allocf;
2947 	ZONE_UNLOCK(zone);
2948 }
2949 
2950 /* See uma.h */
2951 int
2952 uma_zone_reserve_kva(uma_zone_t zone, int count)
2953 {
2954 	uma_keg_t keg;
2955 	vm_offset_t kva;
2956 	int pages;
2957 
2958 	keg = zone_first_keg(zone);
2959 	pages = count / keg->uk_ipers;
2960 
2961 	if (pages * keg->uk_ipers < count)
2962 		pages++;
2963 
2964 #ifdef UMA_MD_SMALL_ALLOC
2965 	if (keg->uk_ppera > 1) {
2966 #else
2967 	if (1) {
2968 #endif
2969 		kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE);
2970 		if (kva == 0)
2971 			return (0);
2972 	} else
2973 		kva = 0;
2974 	ZONE_LOCK(zone);
2975 	keg->uk_kva = kva;
2976 	keg->uk_offset = 0;
2977 	keg->uk_maxpages = pages;
2978 #ifdef UMA_MD_SMALL_ALLOC
2979 	keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
2980 #else
2981 	keg->uk_allocf = noobj_alloc;
2982 #endif
2983 	keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC;
2984 	ZONE_UNLOCK(zone);
2985 	return (1);
2986 }
2987 
2988 /* See uma.h */
2989 void
2990 uma_prealloc(uma_zone_t zone, int items)
2991 {
2992 	int slabs;
2993 	uma_slab_t slab;
2994 	uma_keg_t keg;
2995 
2996 	keg = zone_first_keg(zone);
2997 	ZONE_LOCK(zone);
2998 	slabs = items / keg->uk_ipers;
2999 	if (slabs * keg->uk_ipers < items)
3000 		slabs++;
3001 	while (slabs > 0) {
3002 		slab = keg_alloc_slab(keg, zone, M_WAITOK);
3003 		if (slab == NULL)
3004 			break;
3005 		MPASS(slab->us_keg == keg);
3006 		LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
3007 		slabs--;
3008 	}
3009 	ZONE_UNLOCK(zone);
3010 }
3011 
3012 /* See uma.h */
3013 uint32_t *
3014 uma_find_refcnt(uma_zone_t zone, void *item)
3015 {
3016 	uma_slabrefcnt_t slabref;
3017 	uma_slab_t slab;
3018 	uma_keg_t keg;
3019 	uint32_t *refcnt;
3020 	int idx;
3021 
3022 	slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK));
3023 	slabref = (uma_slabrefcnt_t)slab;
3024 	keg = slab->us_keg;
3025 	KASSERT(keg->uk_flags & UMA_ZONE_REFCNT,
3026 	    ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
3027 	idx = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3028 	refcnt = &slabref->us_refcnt[idx];
3029 	return refcnt;
3030 }
3031 
3032 /* See uma.h */
3033 void
3034 uma_reclaim(void)
3035 {
3036 #ifdef UMA_DEBUG
3037 	printf("UMA: vm asked us to release pages!\n");
3038 #endif
3039 	bucket_enable();
3040 	zone_foreach(zone_drain);
3041 	/*
3042 	 * Some slabs may have been freed but this zone will be visited early
3043 	 * we visit again so that we can free pages that are empty once other
3044 	 * zones are drained.  We have to do the same for buckets.
3045 	 */
3046 	zone_drain(slabzone);
3047 	zone_drain(slabrefzone);
3048 	bucket_zone_drain();
3049 }
3050 
3051 /* See uma.h */
3052 int
3053 uma_zone_exhausted(uma_zone_t zone)
3054 {
3055 	int full;
3056 
3057 	ZONE_LOCK(zone);
3058 	full = (zone->uz_flags & UMA_ZFLAG_FULL);
3059 	ZONE_UNLOCK(zone);
3060 	return (full);
3061 }
3062 
3063 int
3064 uma_zone_exhausted_nolock(uma_zone_t zone)
3065 {
3066 	return (zone->uz_flags & UMA_ZFLAG_FULL);
3067 }
3068 
3069 void *
3070 uma_large_malloc(int size, int wait)
3071 {
3072 	void *mem;
3073 	uma_slab_t slab;
3074 	uint8_t flags;
3075 
3076 	slab = zone_alloc_item(slabzone, NULL, wait);
3077 	if (slab == NULL)
3078 		return (NULL);
3079 	mem = page_alloc(NULL, size, &flags, wait);
3080 	if (mem) {
3081 		vsetslab((vm_offset_t)mem, slab);
3082 		slab->us_data = mem;
3083 		slab->us_flags = flags | UMA_SLAB_MALLOC;
3084 		slab->us_size = size;
3085 	} else {
3086 		zone_free_item(slabzone, slab, NULL, SKIP_NONE,
3087 		    ZFREE_STATFAIL | ZFREE_STATFREE);
3088 	}
3089 
3090 	return (mem);
3091 }
3092 
3093 void
3094 uma_large_free(uma_slab_t slab)
3095 {
3096 	vsetobj((vm_offset_t)slab->us_data, kmem_object);
3097 	page_free(slab->us_data, slab->us_size, slab->us_flags);
3098 	zone_free_item(slabzone, slab, NULL, SKIP_NONE, ZFREE_STATFREE);
3099 }
3100 
3101 void
3102 uma_print_stats(void)
3103 {
3104 	zone_foreach(uma_print_zone);
3105 }
3106 
3107 static void
3108 slab_print(uma_slab_t slab)
3109 {
3110 	printf("slab: keg %p, data %p, freecount %d\n",
3111 		slab->us_keg, slab->us_data, slab->us_freecount);
3112 }
3113 
3114 static void
3115 cache_print(uma_cache_t cache)
3116 {
3117 	printf("alloc: %p(%d), free: %p(%d)\n",
3118 		cache->uc_allocbucket,
3119 		cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3120 		cache->uc_freebucket,
3121 		cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3122 }
3123 
3124 static void
3125 uma_print_keg(uma_keg_t keg)
3126 {
3127 	uma_slab_t slab;
3128 
3129 	printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3130 	    "out %d free %d limit %d\n",
3131 	    keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3132 	    keg->uk_ipers, keg->uk_ppera,
3133 	    (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free,
3134 	    (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3135 	printf("Part slabs:\n");
3136 	LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
3137 		slab_print(slab);
3138 	printf("Free slabs:\n");
3139 	LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
3140 		slab_print(slab);
3141 	printf("Full slabs:\n");
3142 	LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
3143 		slab_print(slab);
3144 }
3145 
3146 void
3147 uma_print_zone(uma_zone_t zone)
3148 {
3149 	uma_cache_t cache;
3150 	uma_klink_t kl;
3151 	int i;
3152 
3153 	printf("zone: %s(%p) size %d flags %#x\n",
3154 	    zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3155 	LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3156 		uma_print_keg(kl->kl_keg);
3157 	CPU_FOREACH(i) {
3158 		cache = &zone->uz_cpu[i];
3159 		printf("CPU %d Cache:\n", i);
3160 		cache_print(cache);
3161 	}
3162 }
3163 
3164 #ifdef DDB
3165 /*
3166  * Generate statistics across both the zone and its per-cpu cache's.  Return
3167  * desired statistics if the pointer is non-NULL for that statistic.
3168  *
3169  * Note: does not update the zone statistics, as it can't safely clear the
3170  * per-CPU cache statistic.
3171  *
3172  * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3173  * safe from off-CPU; we should modify the caches to track this information
3174  * directly so that we don't have to.
3175  */
3176 static void
3177 uma_zone_sumstat(uma_zone_t z, int *cachefreep, uint64_t *allocsp,
3178     uint64_t *freesp, uint64_t *sleepsp)
3179 {
3180 	uma_cache_t cache;
3181 	uint64_t allocs, frees, sleeps;
3182 	int cachefree, cpu;
3183 
3184 	allocs = frees = sleeps = 0;
3185 	cachefree = 0;
3186 	CPU_FOREACH(cpu) {
3187 		cache = &z->uz_cpu[cpu];
3188 		if (cache->uc_allocbucket != NULL)
3189 			cachefree += cache->uc_allocbucket->ub_cnt;
3190 		if (cache->uc_freebucket != NULL)
3191 			cachefree += cache->uc_freebucket->ub_cnt;
3192 		allocs += cache->uc_allocs;
3193 		frees += cache->uc_frees;
3194 	}
3195 	allocs += z->uz_allocs;
3196 	frees += z->uz_frees;
3197 	sleeps += z->uz_sleeps;
3198 	if (cachefreep != NULL)
3199 		*cachefreep = cachefree;
3200 	if (allocsp != NULL)
3201 		*allocsp = allocs;
3202 	if (freesp != NULL)
3203 		*freesp = frees;
3204 	if (sleepsp != NULL)
3205 		*sleepsp = sleeps;
3206 }
3207 #endif /* DDB */
3208 
3209 static int
3210 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
3211 {
3212 	uma_keg_t kz;
3213 	uma_zone_t z;
3214 	int count;
3215 
3216 	count = 0;
3217 	mtx_lock(&uma_mtx);
3218 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
3219 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
3220 			count++;
3221 	}
3222 	mtx_unlock(&uma_mtx);
3223 	return (sysctl_handle_int(oidp, &count, 0, req));
3224 }
3225 
3226 static int
3227 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
3228 {
3229 	struct uma_stream_header ush;
3230 	struct uma_type_header uth;
3231 	struct uma_percpu_stat ups;
3232 	uma_bucket_t bucket;
3233 	struct sbuf sbuf;
3234 	uma_cache_t cache;
3235 	uma_klink_t kl;
3236 	uma_keg_t kz;
3237 	uma_zone_t z;
3238 	uma_keg_t k;
3239 	int count, error, i;
3240 
3241 	error = sysctl_wire_old_buffer(req, 0);
3242 	if (error != 0)
3243 		return (error);
3244 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
3245 
3246 	count = 0;
3247 	mtx_lock(&uma_mtx);
3248 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
3249 		LIST_FOREACH(z, &kz->uk_zones, uz_link)
3250 			count++;
3251 	}
3252 
3253 	/*
3254 	 * Insert stream header.
3255 	 */
3256 	bzero(&ush, sizeof(ush));
3257 	ush.ush_version = UMA_STREAM_VERSION;
3258 	ush.ush_maxcpus = (mp_maxid + 1);
3259 	ush.ush_count = count;
3260 	(void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
3261 
3262 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
3263 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3264 			bzero(&uth, sizeof(uth));
3265 			ZONE_LOCK(z);
3266 			strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
3267 			uth.uth_align = kz->uk_align;
3268 			uth.uth_size = kz->uk_size;
3269 			uth.uth_rsize = kz->uk_rsize;
3270 			LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3271 				k = kl->kl_keg;
3272 				uth.uth_maxpages += k->uk_maxpages;
3273 				uth.uth_pages += k->uk_pages;
3274 				uth.uth_keg_free += k->uk_free;
3275 				uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3276 				    * k->uk_ipers;
3277 			}
3278 
3279 			/*
3280 			 * A zone is secondary is it is not the first entry
3281 			 * on the keg's zone list.
3282 			 */
3283 			if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3284 			    (LIST_FIRST(&kz->uk_zones) != z))
3285 				uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3286 
3287 			LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3288 				uth.uth_zone_free += bucket->ub_cnt;
3289 			uth.uth_allocs = z->uz_allocs;
3290 			uth.uth_frees = z->uz_frees;
3291 			uth.uth_fails = z->uz_fails;
3292 			uth.uth_sleeps = z->uz_sleeps;
3293 			(void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
3294 			/*
3295 			 * While it is not normally safe to access the cache
3296 			 * bucket pointers while not on the CPU that owns the
3297 			 * cache, we only allow the pointers to be exchanged
3298 			 * without the zone lock held, not invalidated, so
3299 			 * accept the possible race associated with bucket
3300 			 * exchange during monitoring.
3301 			 */
3302 			for (i = 0; i < (mp_maxid + 1); i++) {
3303 				bzero(&ups, sizeof(ups));
3304 				if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
3305 					goto skip;
3306 				if (CPU_ABSENT(i))
3307 					goto skip;
3308 				cache = &z->uz_cpu[i];
3309 				if (cache->uc_allocbucket != NULL)
3310 					ups.ups_cache_free +=
3311 					    cache->uc_allocbucket->ub_cnt;
3312 				if (cache->uc_freebucket != NULL)
3313 					ups.ups_cache_free +=
3314 					    cache->uc_freebucket->ub_cnt;
3315 				ups.ups_allocs = cache->uc_allocs;
3316 				ups.ups_frees = cache->uc_frees;
3317 skip:
3318 				(void)sbuf_bcat(&sbuf, &ups, sizeof(ups));
3319 			}
3320 			ZONE_UNLOCK(z);
3321 		}
3322 	}
3323 	mtx_unlock(&uma_mtx);
3324 	error = sbuf_finish(&sbuf);
3325 	sbuf_delete(&sbuf);
3326 	return (error);
3327 }
3328 
3329 #ifdef DDB
3330 DB_SHOW_COMMAND(uma, db_show_uma)
3331 {
3332 	uint64_t allocs, frees, sleeps;
3333 	uma_bucket_t bucket;
3334 	uma_keg_t kz;
3335 	uma_zone_t z;
3336 	int cachefree;
3337 
3338 	db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
3339 	    "Requests", "Sleeps");
3340 	LIST_FOREACH(kz, &uma_kegs, uk_link) {
3341 		LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3342 			if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
3343 				allocs = z->uz_allocs;
3344 				frees = z->uz_frees;
3345 				sleeps = z->uz_sleeps;
3346 				cachefree = 0;
3347 			} else
3348 				uma_zone_sumstat(z, &cachefree, &allocs,
3349 				    &frees, &sleeps);
3350 			if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
3351 			    (LIST_FIRST(&kz->uk_zones) != z)))
3352 				cachefree += kz->uk_free;
3353 			LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3354 				cachefree += bucket->ub_cnt;
3355 			db_printf("%18s %8ju %8jd %8d %12ju %8ju\n", z->uz_name,
3356 			    (uintmax_t)kz->uk_size,
3357 			    (intmax_t)(allocs - frees), cachefree,
3358 			    (uintmax_t)allocs, sleeps);
3359 			if (db_pager_quit)
3360 				return;
3361 		}
3362 	}
3363 }
3364 #endif
3365