xref: /freebsd/sys/vm/uma_core.c (revision 7773002178c8dbc52b44e4d705f07706409af8e4)
1 /*
2  * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * uma_core.c  Implementation of the Universal Memory allocator
29  *
30  * This allocator is intended to replace the multitude of similar object caches
31  * in the standard FreeBSD kernel.  The intent is to be flexible as well as
32  * effecient.  A primary design goal is to return unused memory to the rest of
33  * the system.  This will make the system as a whole more flexible due to the
34  * ability to move memory to subsystems which most need it instead of leaving
35  * pools of reserved memory unused.
36  *
37  * The basic ideas stem from similar slab/zone based allocators whose algorithms
38  * are well known.
39  *
40  */
41 
42 /*
43  * TODO:
44  *	- Improve memory usage for large allocations
45  *	- Investigate cache size adjustments
46  */
47 
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50 
51 /* I should really use ktr.. */
52 /*
53 #define UMA_DEBUG 1
54 #define UMA_DEBUG_ALLOC 1
55 #define UMA_DEBUG_ALLOC_1 1
56 */
57 
58 #include "opt_param.h"
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/malloc.h>
65 #include <sys/lock.h>
66 #include <sys/sysctl.h>
67 #include <sys/mutex.h>
68 #include <sys/proc.h>
69 #include <sys/smp.h>
70 #include <sys/vmmeter.h>
71 #include <sys/mbuf.h>
72 
73 #include <vm/vm.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_param.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_extern.h>
80 #include <vm/uma.h>
81 #include <vm/uma_int.h>
82 #include <vm/uma_dbg.h>
83 
84 #include <machine/vmparam.h>
85 
86 /*
87  * This is the zone from which all zones are spawned.  The idea is that even
88  * the zone heads are allocated from the allocator, so we use the bss section
89  * to bootstrap us.
90  */
91 static struct uma_zone masterzone;
92 static uma_zone_t zones = &masterzone;
93 
94 /* This is the zone from which all of uma_slab_t's are allocated. */
95 static uma_zone_t slabzone;
96 
97 /*
98  * The initial hash tables come out of this zone so they can be allocated
99  * prior to malloc coming up.
100  */
101 static uma_zone_t hashzone;
102 
103 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
104 
105 /*
106  * Are we allowed to allocate buckets?
107  */
108 static int bucketdisable = 1;
109 
110 /* Linked list of all zones in the system */
111 static LIST_HEAD(,uma_zone) uma_zones = LIST_HEAD_INITIALIZER(&uma_zones);
112 
113 /* This mutex protects the zone list */
114 static struct mtx uma_mtx;
115 
116 /* These are the pcpu cache locks */
117 static struct mtx uma_pcpu_mtx[MAXCPU];
118 
119 /* Linked list of boot time pages */
120 static LIST_HEAD(,uma_slab) uma_boot_pages =
121     LIST_HEAD_INITIALIZER(&uma_boot_pages);
122 
123 /* Count of free boottime pages */
124 static int uma_boot_free = 0;
125 
126 /* Is the VM done starting up? */
127 static int booted = 0;
128 
129 /*
130  * This is the handle used to schedule events that need to happen
131  * outside of the allocation fast path.
132  */
133 static struct callout uma_callout;
134 #define	UMA_TIMEOUT	20		/* Seconds for callout interval. */
135 
136 /* This is mp_maxid + 1, for use while looping over each cpu */
137 static int maxcpu;
138 
139 /*
140  * This structure is passed as the zone ctor arg so that I don't have to create
141  * a special allocation function just for zones.
142  */
143 struct uma_zctor_args {
144 	char *name;
145 	size_t size;
146 	uma_ctor ctor;
147 	uma_dtor dtor;
148 	uma_init uminit;
149 	uma_fini fini;
150 	int align;
151 	u_int16_t flags;
152 };
153 
154 struct uma_bucket_zone {
155 	uma_zone_t	ubz_zone;
156 	char		*ubz_name;
157 	int		ubz_entries;
158 };
159 
160 #define	BUCKET_MAX	128
161 
162 struct uma_bucket_zone bucket_zones[] = {
163 	{ NULL, "16 Bucket", 16 },
164 	{ NULL, "32 Bucket", 32 },
165 	{ NULL, "64 Bucket", 64 },
166 	{ NULL, "128 Bucket", 128 },
167 	{ NULL, NULL, 0}
168 };
169 
170 #define	BUCKET_SHIFT	4
171 #define	BUCKET_ZONES	((BUCKET_MAX >> BUCKET_SHIFT) + 1)
172 
173 uint8_t bucket_size[BUCKET_ZONES];
174 
175 /* Prototypes.. */
176 
177 static void *obj_alloc(uma_zone_t, int, u_int8_t *, int);
178 static void *page_alloc(uma_zone_t, int, u_int8_t *, int);
179 static void *startup_alloc(uma_zone_t, int, u_int8_t *, int);
180 static void page_free(void *, int, u_int8_t);
181 static uma_slab_t slab_zalloc(uma_zone_t, int);
182 static void cache_drain(uma_zone_t);
183 static void bucket_drain(uma_zone_t, uma_bucket_t);
184 static void zone_ctor(void *, int, void *);
185 static void zone_dtor(void *, int, void *);
186 static void zero_init(void *, int);
187 static void zone_small_init(uma_zone_t zone);
188 static void zone_large_init(uma_zone_t zone);
189 static void zone_foreach(void (*zfunc)(uma_zone_t));
190 static void zone_timeout(uma_zone_t zone);
191 static int hash_alloc(struct uma_hash *);
192 static int hash_expand(struct uma_hash *, struct uma_hash *);
193 static void hash_free(struct uma_hash *hash);
194 static void uma_timeout(void *);
195 static void uma_startup3(void);
196 static void *uma_zalloc_internal(uma_zone_t, void *, int);
197 static void uma_zfree_internal(uma_zone_t, void *, void *, int);
198 static void bucket_enable(void);
199 static void bucket_init(void);
200 static uma_bucket_t bucket_alloc(int, int);
201 static void bucket_free(uma_bucket_t);
202 static void bucket_zone_drain(void);
203 static int uma_zalloc_bucket(uma_zone_t zone, int flags);
204 static uma_slab_t uma_zone_slab(uma_zone_t zone, int flags);
205 static void *uma_slab_alloc(uma_zone_t zone, uma_slab_t slab);
206 static void zone_drain(uma_zone_t);
207 
208 void uma_print_zone(uma_zone_t);
209 void uma_print_stats(void);
210 static int sysctl_vm_zone(SYSCTL_HANDLER_ARGS);
211 
212 SYSCTL_OID(_vm, OID_AUTO, zone, CTLTYPE_STRING|CTLFLAG_RD,
213     NULL, 0, sysctl_vm_zone, "A", "Zone Info");
214 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
215 
216 /*
217  * This routine checks to see whether or not it's safe to enable buckets.
218  */
219 
220 static void
221 bucket_enable(void)
222 {
223 	if (cnt.v_free_count < cnt.v_free_min)
224 		bucketdisable = 1;
225 	else
226 		bucketdisable = 0;
227 }
228 
229 static void
230 bucket_init(void)
231 {
232 	struct uma_bucket_zone *ubz;
233 	int i;
234 	int j;
235 
236 	for (i = 0, j = 0; bucket_zones[j].ubz_entries != 0; j++) {
237 		int size;
238 
239 		ubz = &bucket_zones[j];
240 		size = roundup(sizeof(struct uma_bucket), sizeof(void *));
241 		size += sizeof(void *) * ubz->ubz_entries;
242 		ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
243 	    	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
244 		for (; i <= ubz->ubz_entries; i += (1 << BUCKET_SHIFT))
245 			bucket_size[i >> BUCKET_SHIFT] = j;
246 	}
247 }
248 
249 static uma_bucket_t
250 bucket_alloc(int entries, int bflags)
251 {
252 	struct uma_bucket_zone *ubz;
253 	uma_bucket_t bucket;
254 	int idx;
255 
256 	/*
257 	 * This is to stop us from allocating per cpu buckets while we're
258 	 * running out of UMA_BOOT_PAGES.  Otherwise, we would exhaust the
259 	 * boot pages.  This also prevents us from allocating buckets in
260 	 * low memory situations.
261 	 */
262 
263 	if (bucketdisable)
264 		return (NULL);
265 	idx = howmany(entries, 1 << BUCKET_SHIFT);
266 	ubz = &bucket_zones[bucket_size[idx]];
267 	bucket = uma_zalloc_internal(ubz->ubz_zone, NULL, bflags);
268 	if (bucket) {
269 #ifdef INVARIANTS
270 		bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
271 #endif
272 		bucket->ub_cnt = 0;
273 		bucket->ub_entries = ubz->ubz_entries;
274 	}
275 
276 	return (bucket);
277 }
278 
279 static void
280 bucket_free(uma_bucket_t bucket)
281 {
282 	struct uma_bucket_zone *ubz;
283 	int idx;
284 
285 	idx = howmany(bucket->ub_entries, 1 << BUCKET_SHIFT);
286 	ubz = &bucket_zones[bucket_size[idx]];
287 	uma_zfree_internal(ubz->ubz_zone, bucket, NULL, 0);
288 }
289 
290 static void
291 bucket_zone_drain(void)
292 {
293 	struct uma_bucket_zone *ubz;
294 
295 	for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
296 		zone_drain(ubz->ubz_zone);
297 }
298 
299 
300 /*
301  * Routine called by timeout which is used to fire off some time interval
302  * based calculations.  (stats, hash size, etc.)
303  *
304  * Arguments:
305  *	arg   Unused
306  *
307  * Returns:
308  *	Nothing
309  */
310 static void
311 uma_timeout(void *unused)
312 {
313 	bucket_enable();
314 	zone_foreach(zone_timeout);
315 
316 	/* Reschedule this event */
317 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
318 }
319 
320 /*
321  * Routine to perform timeout driven calculations.  This expands the
322  * hashes and does per cpu statistics aggregation.
323  *
324  *  Arguments:
325  *	zone  The zone to operate on
326  *
327  *  Returns:
328  *	Nothing
329  */
330 static void
331 zone_timeout(uma_zone_t zone)
332 {
333 	uma_cache_t cache;
334 	u_int64_t alloc;
335 	int cpu;
336 
337 	alloc = 0;
338 
339 	/*
340 	 * Aggregate per cpu cache statistics back to the zone.
341 	 *
342 	 * I may rewrite this to set a flag in the per cpu cache instead of
343 	 * locking.  If the flag is not cleared on the next round I will have
344 	 * to lock and do it here instead so that the statistics don't get too
345 	 * far out of sync.
346 	 */
347 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) {
348 		for (cpu = 0; cpu < maxcpu; cpu++) {
349 			if (CPU_ABSENT(cpu))
350 				continue;
351 			CPU_LOCK(cpu);
352 			cache = &zone->uz_cpu[cpu];
353 			/* Add them up, and reset */
354 			alloc += cache->uc_allocs;
355 			cache->uc_allocs = 0;
356 			CPU_UNLOCK(cpu);
357 		}
358 	}
359 
360 	/* Now push these stats back into the zone.. */
361 	ZONE_LOCK(zone);
362 	zone->uz_allocs += alloc;
363 
364 	/*
365 	 * Expand the zone hash table.
366 	 *
367 	 * This is done if the number of slabs is larger than the hash size.
368 	 * What I'm trying to do here is completely reduce collisions.  This
369 	 * may be a little aggressive.  Should I allow for two collisions max?
370 	 */
371 
372 	if (zone->uz_flags & UMA_ZONE_HASH &&
373 	    zone->uz_pages / zone->uz_ppera >= zone->uz_hash.uh_hashsize) {
374 		struct uma_hash newhash;
375 		struct uma_hash oldhash;
376 		int ret;
377 
378 		/*
379 		 * This is so involved because allocating and freeing
380 		 * while the zone lock is held will lead to deadlock.
381 		 * I have to do everything in stages and check for
382 		 * races.
383 		 */
384 		newhash = zone->uz_hash;
385 		ZONE_UNLOCK(zone);
386 		ret = hash_alloc(&newhash);
387 		ZONE_LOCK(zone);
388 		if (ret) {
389 			if (hash_expand(&zone->uz_hash, &newhash)) {
390 				oldhash = zone->uz_hash;
391 				zone->uz_hash = newhash;
392 			} else
393 				oldhash = newhash;
394 
395 			ZONE_UNLOCK(zone);
396 			hash_free(&oldhash);
397 			ZONE_LOCK(zone);
398 		}
399 	}
400 	ZONE_UNLOCK(zone);
401 }
402 
403 /*
404  * Allocate and zero fill the next sized hash table from the appropriate
405  * backing store.
406  *
407  * Arguments:
408  *	hash  A new hash structure with the old hash size in uh_hashsize
409  *
410  * Returns:
411  *	1 on sucess and 0 on failure.
412  */
413 static int
414 hash_alloc(struct uma_hash *hash)
415 {
416 	int oldsize;
417 	int alloc;
418 
419 	oldsize = hash->uh_hashsize;
420 
421 	/* We're just going to go to a power of two greater */
422 	if (oldsize)  {
423 		hash->uh_hashsize = oldsize * 2;
424 		alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
425 		hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
426 		    M_UMAHASH, M_NOWAIT);
427 	} else {
428 		alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
429 		hash->uh_slab_hash = uma_zalloc_internal(hashzone, NULL,
430 		    M_WAITOK);
431 		hash->uh_hashsize = UMA_HASH_SIZE_INIT;
432 	}
433 	if (hash->uh_slab_hash) {
434 		bzero(hash->uh_slab_hash, alloc);
435 		hash->uh_hashmask = hash->uh_hashsize - 1;
436 		return (1);
437 	}
438 
439 	return (0);
440 }
441 
442 /*
443  * Expands the hash table for HASH zones.  This is done from zone_timeout
444  * to reduce collisions.  This must not be done in the regular allocation
445  * path, otherwise, we can recurse on the vm while allocating pages.
446  *
447  * Arguments:
448  *	oldhash  The hash you want to expand
449  *	newhash  The hash structure for the new table
450  *
451  * Returns:
452  * 	Nothing
453  *
454  * Discussion:
455  */
456 static int
457 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
458 {
459 	uma_slab_t slab;
460 	int hval;
461 	int i;
462 
463 	if (!newhash->uh_slab_hash)
464 		return (0);
465 
466 	if (oldhash->uh_hashsize >= newhash->uh_hashsize)
467 		return (0);
468 
469 	/*
470 	 * I need to investigate hash algorithms for resizing without a
471 	 * full rehash.
472 	 */
473 
474 	for (i = 0; i < oldhash->uh_hashsize; i++)
475 		while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
476 			slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
477 			SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
478 			hval = UMA_HASH(newhash, slab->us_data);
479 			SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
480 			    slab, us_hlink);
481 		}
482 
483 	return (1);
484 }
485 
486 /*
487  * Free the hash bucket to the appropriate backing store.
488  *
489  * Arguments:
490  *	slab_hash  The hash bucket we're freeing
491  *	hashsize   The number of entries in that hash bucket
492  *
493  * Returns:
494  *	Nothing
495  */
496 static void
497 hash_free(struct uma_hash *hash)
498 {
499 	if (hash->uh_slab_hash == NULL)
500 		return;
501 	if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
502 		uma_zfree_internal(hashzone,
503 		    hash->uh_slab_hash, NULL, 0);
504 	else
505 		free(hash->uh_slab_hash, M_UMAHASH);
506 }
507 
508 /*
509  * Frees all outstanding items in a bucket
510  *
511  * Arguments:
512  *	zone   The zone to free to, must be unlocked.
513  *	bucket The free/alloc bucket with items, cpu queue must be locked.
514  *
515  * Returns:
516  *	Nothing
517  */
518 
519 static void
520 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
521 {
522 	uma_slab_t slab;
523 	int mzone;
524 	void *item;
525 
526 	if (bucket == NULL)
527 		return;
528 
529 	slab = NULL;
530 	mzone = 0;
531 
532 	/* We have to lookup the slab again for malloc.. */
533 	if (zone->uz_flags & UMA_ZONE_MALLOC)
534 		mzone = 1;
535 
536 	while (bucket->ub_cnt > 0)  {
537 		bucket->ub_cnt--;
538 		item = bucket->ub_bucket[bucket->ub_cnt];
539 #ifdef INVARIANTS
540 		bucket->ub_bucket[bucket->ub_cnt] = NULL;
541 		KASSERT(item != NULL,
542 		    ("bucket_drain: botched ptr, item is NULL"));
543 #endif
544 		/*
545 		 * This is extremely inefficient.  The slab pointer was passed
546 		 * to uma_zfree_arg, but we lost it because the buckets don't
547 		 * hold them.  This will go away when free() gets a size passed
548 		 * to it.
549 		 */
550 		if (mzone)
551 			slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK));
552 		uma_zfree_internal(zone, item, slab, 1);
553 	}
554 }
555 
556 /*
557  * Drains the per cpu caches for a zone.
558  *
559  * Arguments:
560  *	zone     The zone to drain, must be unlocked.
561  *
562  * Returns:
563  *	Nothing
564  */
565 static void
566 cache_drain(uma_zone_t zone)
567 {
568 	uma_bucket_t bucket;
569 	uma_cache_t cache;
570 	int cpu;
571 
572 	/*
573 	 * We have to lock each cpu cache before locking the zone
574 	 */
575 	for (cpu = 0; cpu < maxcpu; cpu++) {
576 		if (CPU_ABSENT(cpu))
577 			continue;
578 		CPU_LOCK(cpu);
579 		cache = &zone->uz_cpu[cpu];
580 		bucket_drain(zone, cache->uc_allocbucket);
581 		bucket_drain(zone, cache->uc_freebucket);
582 		if (cache->uc_allocbucket != NULL)
583 			bucket_free(cache->uc_allocbucket);
584 		if (cache->uc_freebucket != NULL)
585 			bucket_free(cache->uc_freebucket);
586 		cache->uc_allocbucket = cache->uc_freebucket = NULL;
587 	}
588 
589 	/*
590 	 * Drain the bucket queues and free the buckets, we just keep two per
591 	 * cpu (alloc/free).
592 	 */
593 	ZONE_LOCK(zone);
594 	while ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
595 		LIST_REMOVE(bucket, ub_link);
596 		ZONE_UNLOCK(zone);
597 		bucket_drain(zone, bucket);
598 		bucket_free(bucket);
599 		ZONE_LOCK(zone);
600 	}
601 
602 	/* Now we do the free queue.. */
603 	while ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
604 		LIST_REMOVE(bucket, ub_link);
605 		bucket_free(bucket);
606 	}
607 	for (cpu = 0; cpu < maxcpu; cpu++) {
608 		if (CPU_ABSENT(cpu))
609 			continue;
610 		CPU_UNLOCK(cpu);
611 	}
612 	ZONE_UNLOCK(zone);
613 }
614 
615 /*
616  * Frees pages from a zone back to the system.  This is done on demand from
617  * the pageout daemon.
618  *
619  * Arguments:
620  *	zone  The zone to free pages from
621  *	 all  Should we drain all items?
622  *
623  * Returns:
624  *	Nothing.
625  */
626 static void
627 zone_drain(uma_zone_t zone)
628 {
629 	struct slabhead freeslabs = {};
630 	uma_slab_t slab;
631 	uma_slab_t n;
632 	u_int8_t flags;
633 	u_int8_t *mem;
634 	int i;
635 
636 	/*
637 	 * We don't want to take pages from staticly allocated zones at this
638 	 * time
639 	 */
640 	if (zone->uz_flags & UMA_ZONE_NOFREE || zone->uz_freef == NULL)
641 		return;
642 
643 	ZONE_LOCK(zone);
644 
645 #ifdef UMA_DEBUG
646 	printf("%s free items: %u\n", zone->uz_name, zone->uz_free);
647 #endif
648 	if (zone->uz_free == 0)
649 		goto finished;
650 
651 	slab = LIST_FIRST(&zone->uz_free_slab);
652 	while (slab) {
653 		n = LIST_NEXT(slab, us_link);
654 
655 		/* We have no where to free these to */
656 		if (slab->us_flags & UMA_SLAB_BOOT) {
657 			slab = n;
658 			continue;
659 		}
660 
661 		LIST_REMOVE(slab, us_link);
662 		zone->uz_pages -= zone->uz_ppera;
663 		zone->uz_free -= zone->uz_ipers;
664 
665 		if (zone->uz_flags & UMA_ZONE_HASH)
666 			UMA_HASH_REMOVE(&zone->uz_hash, slab, slab->us_data);
667 
668 		SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
669 
670 		slab = n;
671 	}
672 finished:
673 	ZONE_UNLOCK(zone);
674 
675 	while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
676 		SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
677 		if (zone->uz_fini)
678 			for (i = 0; i < zone->uz_ipers; i++)
679 				zone->uz_fini(
680 				    slab->us_data + (zone->uz_rsize * i),
681 				    zone->uz_size);
682 		flags = slab->us_flags;
683 		mem = slab->us_data;
684 
685 		if (zone->uz_flags & UMA_ZONE_OFFPAGE)
686 			uma_zfree_internal(slabzone, slab, NULL, 0);
687 		if (zone->uz_flags & UMA_ZONE_MALLOC) {
688 			vm_object_t obj;
689 
690 			if (flags & UMA_SLAB_KMEM)
691 				obj = kmem_object;
692 			else
693 				obj = NULL;
694 			for (i = 0; i < zone->uz_ppera; i++)
695 				vsetobj((vm_offset_t)mem + (i * PAGE_SIZE),
696 				    obj);
697 		}
698 #ifdef UMA_DEBUG
699 		printf("%s: Returning %d bytes.\n",
700 		    zone->uz_name, UMA_SLAB_SIZE * zone->uz_ppera);
701 #endif
702 		zone->uz_freef(mem, UMA_SLAB_SIZE * zone->uz_ppera, flags);
703 	}
704 
705 }
706 
707 /*
708  * Allocate a new slab for a zone.  This does not insert the slab onto a list.
709  *
710  * Arguments:
711  *	zone  The zone to allocate slabs for
712  *	wait  Shall we wait?
713  *
714  * Returns:
715  *	The slab that was allocated or NULL if there is no memory and the
716  *	caller specified M_NOWAIT.
717  */
718 static uma_slab_t
719 slab_zalloc(uma_zone_t zone, int wait)
720 {
721 	uma_slab_t slab;	/* Starting slab */
722 	u_int8_t *mem;
723 	u_int8_t flags;
724 	int i;
725 
726 	slab = NULL;
727 
728 #ifdef UMA_DEBUG
729 	printf("slab_zalloc:  Allocating a new slab for %s\n", zone->uz_name);
730 #endif
731 	ZONE_UNLOCK(zone);
732 
733 	if (zone->uz_flags & UMA_ZONE_OFFPAGE) {
734 		slab = uma_zalloc_internal(slabzone, NULL, wait);
735 		if (slab == NULL) {
736 			ZONE_LOCK(zone);
737 			return NULL;
738 		}
739 	}
740 
741 	/*
742 	 * This reproduces the old vm_zone behavior of zero filling pages the
743 	 * first time they are added to a zone.
744 	 *
745 	 * Malloced items are zeroed in uma_zalloc.
746 	 */
747 
748 	if ((zone->uz_flags & UMA_ZONE_MALLOC) == 0)
749 		wait |= M_ZERO;
750 	else
751 		wait &= ~M_ZERO;
752 
753 	mem = zone->uz_allocf(zone, zone->uz_ppera * UMA_SLAB_SIZE,
754 	    &flags, wait);
755 	if (mem == NULL) {
756 		ZONE_LOCK(zone);
757 		return (NULL);
758 	}
759 
760 	/* Point the slab into the allocated memory */
761 	if (!(zone->uz_flags & UMA_ZONE_OFFPAGE))
762 		slab = (uma_slab_t )(mem + zone->uz_pgoff);
763 
764 	if (zone->uz_flags & UMA_ZONE_MALLOC)
765 		for (i = 0; i < zone->uz_ppera; i++)
766 			vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
767 
768 	slab->us_zone = zone;
769 	slab->us_data = mem;
770 	slab->us_freecount = zone->uz_ipers;
771 	slab->us_firstfree = 0;
772 	slab->us_flags = flags;
773 	for (i = 0; i < zone->uz_ipers; i++)
774 		slab->us_freelist[i] = i+1;
775 
776 	if (zone->uz_init)
777 		for (i = 0; i < zone->uz_ipers; i++)
778 			zone->uz_init(slab->us_data + (zone->uz_rsize * i),
779 			    zone->uz_size);
780 	ZONE_LOCK(zone);
781 
782 	if (zone->uz_flags & UMA_ZONE_HASH)
783 		UMA_HASH_INSERT(&zone->uz_hash, slab, mem);
784 
785 	zone->uz_pages += zone->uz_ppera;
786 	zone->uz_free += zone->uz_ipers;
787 
788 	return (slab);
789 }
790 
791 /*
792  * This function is intended to be used early on in place of page_alloc() so
793  * that we may use the boot time page cache to satisfy allocations before
794  * the VM is ready.
795  */
796 static void *
797 startup_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
798 {
799 	/*
800 	 * Check our small startup cache to see if it has pages remaining.
801 	 */
802 	mtx_lock(&uma_mtx);
803 	if (uma_boot_free != 0) {
804 		uma_slab_t tmps;
805 
806 		tmps = LIST_FIRST(&uma_boot_pages);
807 		LIST_REMOVE(tmps, us_link);
808 		uma_boot_free--;
809 		mtx_unlock(&uma_mtx);
810 		*pflag = tmps->us_flags;
811 		return (tmps->us_data);
812 	}
813 	mtx_unlock(&uma_mtx);
814 	if (booted == 0)
815 		panic("UMA: Increase UMA_BOOT_PAGES");
816 	/*
817 	 * Now that we've booted reset these users to their real allocator.
818 	 */
819 #ifdef UMA_MD_SMALL_ALLOC
820 	zone->uz_allocf = uma_small_alloc;
821 #else
822 	zone->uz_allocf = page_alloc;
823 #endif
824 	return zone->uz_allocf(zone, bytes, pflag, wait);
825 }
826 
827 /*
828  * Allocates a number of pages from the system
829  *
830  * Arguments:
831  *	zone  Unused
832  *	bytes  The number of bytes requested
833  *	wait  Shall we wait?
834  *
835  * Returns:
836  *	A pointer to the alloced memory or possibly
837  *	NULL if M_NOWAIT is set.
838  */
839 static void *
840 page_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
841 {
842 	void *p;	/* Returned page */
843 
844 	*pflag = UMA_SLAB_KMEM;
845 	p = (void *) kmem_malloc(kmem_map, bytes, wait);
846 
847 	return (p);
848 }
849 
850 /*
851  * Allocates a number of pages from within an object
852  *
853  * Arguments:
854  *	zone   Unused
855  *	bytes  The number of bytes requested
856  *	wait   Shall we wait?
857  *
858  * Returns:
859  *	A pointer to the alloced memory or possibly
860  *	NULL if M_NOWAIT is set.
861  */
862 static void *
863 obj_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
864 {
865 	vm_object_t object;
866 	vm_offset_t retkva, zkva;
867 	vm_page_t p;
868 	int pages, startpages;
869 
870 	object = zone->uz_obj;
871 	retkva = 0;
872 
873 	/*
874 	 * This looks a little weird since we're getting one page at a time.
875 	 */
876 	VM_OBJECT_LOCK(object);
877 	p = TAILQ_LAST(&object->memq, pglist);
878 	pages = p != NULL ? p->pindex + 1 : 0;
879 	startpages = pages;
880 	zkva = zone->uz_kva + pages * PAGE_SIZE;
881 	for (; bytes > 0; bytes -= PAGE_SIZE) {
882 		p = vm_page_alloc(object, pages,
883 		    VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED);
884 		if (p == NULL) {
885 			if (pages != startpages)
886 				pmap_qremove(retkva, pages - startpages);
887 			while (pages != startpages) {
888 				pages--;
889 				p = TAILQ_LAST(&object->memq, pglist);
890 				vm_page_lock_queues();
891 				vm_page_unwire(p, 0);
892 				vm_page_free(p);
893 				vm_page_unlock_queues();
894 			}
895 			retkva = 0;
896 			goto done;
897 		}
898 		pmap_qenter(zkva, &p, 1);
899 		if (retkva == 0)
900 			retkva = zkva;
901 		zkva += PAGE_SIZE;
902 		pages += 1;
903 	}
904 done:
905 	VM_OBJECT_UNLOCK(object);
906 	*flags = UMA_SLAB_PRIV;
907 
908 	return ((void *)retkva);
909 }
910 
911 /*
912  * Frees a number of pages to the system
913  *
914  * Arguments:
915  *	mem   A pointer to the memory to be freed
916  *	size  The size of the memory being freed
917  *	flags The original p->us_flags field
918  *
919  * Returns:
920  *	Nothing
921  */
922 static void
923 page_free(void *mem, int size, u_int8_t flags)
924 {
925 	vm_map_t map;
926 
927 	if (flags & UMA_SLAB_KMEM)
928 		map = kmem_map;
929 	else
930 		panic("UMA: page_free used with invalid flags %d\n", flags);
931 
932 	kmem_free(map, (vm_offset_t)mem, size);
933 }
934 
935 /*
936  * Zero fill initializer
937  *
938  * Arguments/Returns follow uma_init specifications
939  */
940 static void
941 zero_init(void *mem, int size)
942 {
943 	bzero(mem, size);
944 }
945 
946 /*
947  * Finish creating a small uma zone.  This calculates ipers, and the zone size.
948  *
949  * Arguments
950  *	zone  The zone we should initialize
951  *
952  * Returns
953  *	Nothing
954  */
955 static void
956 zone_small_init(uma_zone_t zone)
957 {
958 	int rsize;
959 	int memused;
960 	int ipers;
961 
962 	rsize = zone->uz_size;
963 
964 	if (rsize < UMA_SMALLEST_UNIT)
965 		rsize = UMA_SMALLEST_UNIT;
966 
967 	if (rsize & zone->uz_align)
968 		rsize = (rsize & ~zone->uz_align) + (zone->uz_align + 1);
969 
970 	zone->uz_rsize = rsize;
971 
972 	rsize += 1;	/* Account for the byte of linkage */
973 	zone->uz_ipers = (UMA_SLAB_SIZE - sizeof(struct uma_slab)) / rsize;
974 	zone->uz_ppera = 1;
975 
976 	KASSERT(zone->uz_ipers != 0, ("zone_small_init: ipers is 0, uh-oh!"));
977 	memused = zone->uz_ipers * zone->uz_rsize;
978 
979 	/* Can we do any better? */
980 	if ((UMA_SLAB_SIZE - memused) >= UMA_MAX_WASTE) {
981 		/*
982 		 * We can't do this if we're internal or if we've been
983 		 * asked to not go to the VM for buckets.  If we do this we
984 		 * may end up going to the VM (kmem_map) for slabs which we
985 		 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a
986 		 * result of UMA_ZONE_VM, which clearly forbids it.
987 		 */
988 		if ((zone->uz_flags & UMA_ZFLAG_INTERNAL) ||
989 		    (zone->uz_flags & UMA_ZFLAG_CACHEONLY))
990 			return;
991 		ipers = UMA_SLAB_SIZE / zone->uz_rsize;
992 		if (ipers > zone->uz_ipers) {
993 			zone->uz_flags |= UMA_ZONE_OFFPAGE;
994 			if ((zone->uz_flags & UMA_ZONE_MALLOC) == 0)
995 				zone->uz_flags |= UMA_ZONE_HASH;
996 			zone->uz_ipers = ipers;
997 		}
998 	}
999 }
1000 
1001 /*
1002  * Finish creating a large (> UMA_SLAB_SIZE) uma zone.  Just give in and do
1003  * OFFPAGE for now.  When I can allow for more dynamic slab sizes this will be
1004  * more complicated.
1005  *
1006  * Arguments
1007  *	zone  The zone we should initialize
1008  *
1009  * Returns
1010  *	Nothing
1011  */
1012 static void
1013 zone_large_init(uma_zone_t zone)
1014 {
1015 	int pages;
1016 
1017 	KASSERT((zone->uz_flags & UMA_ZFLAG_CACHEONLY) == 0,
1018 	    ("zone_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY zone"));
1019 
1020 	pages = zone->uz_size / UMA_SLAB_SIZE;
1021 
1022 	/* Account for remainder */
1023 	if ((pages * UMA_SLAB_SIZE) < zone->uz_size)
1024 		pages++;
1025 
1026 	zone->uz_ppera = pages;
1027 	zone->uz_ipers = 1;
1028 
1029 	zone->uz_flags |= UMA_ZONE_OFFPAGE;
1030 	if ((zone->uz_flags & UMA_ZONE_MALLOC) == 0)
1031 		zone->uz_flags |= UMA_ZONE_HASH;
1032 
1033 	zone->uz_rsize = zone->uz_size;
1034 }
1035 
1036 /*
1037  * Zone header ctor.  This initializes all fields, locks, etc.  And inserts
1038  * the zone onto the global zone list.
1039  *
1040  * Arguments/Returns follow uma_ctor specifications
1041  *	udata  Actually uma_zcreat_args
1042  */
1043 
1044 static void
1045 zone_ctor(void *mem, int size, void *udata)
1046 {
1047 	struct uma_zctor_args *arg = udata;
1048 	uma_zone_t zone = mem;
1049 	int privlc;
1050 
1051 	bzero(zone, size);
1052 	zone->uz_name = arg->name;
1053 	zone->uz_size = arg->size;
1054 	zone->uz_ctor = arg->ctor;
1055 	zone->uz_dtor = arg->dtor;
1056 	zone->uz_init = arg->uminit;
1057 	zone->uz_fini = arg->fini;
1058 	zone->uz_align = arg->align;
1059 	zone->uz_free = 0;
1060 	zone->uz_pages = 0;
1061 	zone->uz_flags = arg->flags;
1062 	zone->uz_allocf = page_alloc;
1063 	zone->uz_freef = page_free;
1064 
1065 	if (arg->flags & UMA_ZONE_ZINIT)
1066 		zone->uz_init = zero_init;
1067 
1068 	if (arg->flags & UMA_ZONE_VM)
1069 		zone->uz_flags |= UMA_ZFLAG_CACHEONLY;
1070 
1071 	/*
1072 	 * XXX:
1073 	 * The +1 byte added to uz_size is to account for the byte of
1074 	 * linkage that is added to the size in zone_small_init().  If
1075 	 * we don't account for this here then we may end up in
1076 	 * zone_small_init() with a calculated 'ipers' of 0.
1077 	 */
1078 	if ((zone->uz_size+1) > (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1079 		zone_large_init(zone);
1080 	else
1081 		zone_small_init(zone);
1082 	/*
1083 	 * If we haven't booted yet we need allocations to go through the
1084 	 * startup cache until the vm is ready.
1085 	 */
1086 	if (zone->uz_ppera == 1) {
1087 #ifdef UMA_MD_SMALL_ALLOC
1088 		zone->uz_allocf = uma_small_alloc;
1089 		zone->uz_freef = uma_small_free;
1090 #endif
1091 		if (booted == 0)
1092 			zone->uz_allocf = startup_alloc;
1093 	}
1094 	if (arg->flags & UMA_ZONE_MTXCLASS)
1095 		privlc = 1;
1096 	else
1097 		privlc = 0;
1098 
1099 	/*
1100 	 * If we're putting the slab header in the actual page we need to
1101 	 * figure out where in each page it goes.  This calculates a right
1102 	 * justified offset into the memory on an ALIGN_PTR boundary.
1103 	 */
1104 	if (!(zone->uz_flags & UMA_ZONE_OFFPAGE)) {
1105 		int totsize;
1106 
1107 		/* Size of the slab struct and free list */
1108 		totsize = sizeof(struct uma_slab) + zone->uz_ipers;
1109 		if (totsize & UMA_ALIGN_PTR)
1110 			totsize = (totsize & ~UMA_ALIGN_PTR) +
1111 			    (UMA_ALIGN_PTR + 1);
1112 		zone->uz_pgoff = UMA_SLAB_SIZE - totsize;
1113 		totsize = zone->uz_pgoff + sizeof(struct uma_slab)
1114 		    + zone->uz_ipers;
1115 		/* I don't think it's possible, but I'll make sure anyway */
1116 		if (totsize > UMA_SLAB_SIZE) {
1117 			printf("zone %s ipers %d rsize %d size %d\n",
1118 			    zone->uz_name, zone->uz_ipers, zone->uz_rsize,
1119 			    zone->uz_size);
1120 			panic("UMA slab won't fit.\n");
1121 		}
1122 	}
1123 
1124 	if (zone->uz_flags & UMA_ZONE_HASH)
1125 		hash_alloc(&zone->uz_hash);
1126 
1127 #ifdef UMA_DEBUG
1128 	printf("%s(%p) size = %d ipers = %d ppera = %d pgoff = %d\n",
1129 	    zone->uz_name, zone,
1130 	    zone->uz_size, zone->uz_ipers,
1131 	    zone->uz_ppera, zone->uz_pgoff);
1132 #endif
1133 	ZONE_LOCK_INIT(zone, privlc);
1134 
1135 	mtx_lock(&uma_mtx);
1136 	LIST_INSERT_HEAD(&uma_zones, zone, uz_link);
1137 	mtx_unlock(&uma_mtx);
1138 
1139 	/*
1140 	 * Some internal zones don't have room allocated for the per cpu
1141 	 * caches.  If we're internal, bail out here.
1142 	 */
1143 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
1144 		return;
1145 
1146 	if (zone->uz_ipers <= BUCKET_MAX)
1147 		zone->uz_count = zone->uz_ipers;
1148 	else
1149 		zone->uz_count = BUCKET_MAX;
1150 }
1151 
1152 /*
1153  * Zone header dtor.  This frees all data, destroys locks, frees the hash table
1154  * and removes the zone from the global list.
1155  *
1156  * Arguments/Returns follow uma_dtor specifications
1157  *	udata  unused
1158  */
1159 
1160 static void
1161 zone_dtor(void *arg, int size, void *udata)
1162 {
1163 	uma_zone_t zone;
1164 
1165 	zone = (uma_zone_t)arg;
1166 
1167 	if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1168 		cache_drain(zone);
1169 	mtx_lock(&uma_mtx);
1170 	LIST_REMOVE(zone, uz_link);
1171 	zone_drain(zone);
1172 	mtx_unlock(&uma_mtx);
1173 
1174 	ZONE_LOCK(zone);
1175 	if (zone->uz_free != 0)
1176 		printf("Zone %s was not empty (%d items). "
1177 		    " Lost %d pages of memory.\n",
1178 		    zone->uz_name, zone->uz_free, zone->uz_pages);
1179 
1180 	ZONE_UNLOCK(zone);
1181 	if (zone->uz_flags & UMA_ZONE_HASH)
1182 		hash_free(&zone->uz_hash);
1183 
1184 	ZONE_LOCK_FINI(zone);
1185 }
1186 /*
1187  * Traverses every zone in the system and calls a callback
1188  *
1189  * Arguments:
1190  *	zfunc  A pointer to a function which accepts a zone
1191  *		as an argument.
1192  *
1193  * Returns:
1194  *	Nothing
1195  */
1196 static void
1197 zone_foreach(void (*zfunc)(uma_zone_t))
1198 {
1199 	uma_zone_t zone;
1200 
1201 	mtx_lock(&uma_mtx);
1202 	LIST_FOREACH(zone, &uma_zones, uz_link)
1203 		zfunc(zone);
1204 	mtx_unlock(&uma_mtx);
1205 }
1206 
1207 /* Public functions */
1208 /* See uma.h */
1209 void
1210 uma_startup(void *bootmem)
1211 {
1212 	struct uma_zctor_args args;
1213 	uma_slab_t slab;
1214 	int slabsize;
1215 	int i;
1216 
1217 #ifdef UMA_DEBUG
1218 	printf("Creating uma zone headers zone.\n");
1219 #endif
1220 #ifdef SMP
1221 	maxcpu = mp_maxid + 1;
1222 #else
1223 	maxcpu = 1;
1224 #endif
1225 #ifdef UMA_DEBUG
1226 	printf("Max cpu = %d, mp_maxid = %d\n", maxcpu, mp_maxid);
1227 #endif
1228 	mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
1229 	/* "manually" Create the initial zone */
1230 	args.name = "UMA Zones";
1231 	args.size = sizeof(struct uma_zone) +
1232 	    (sizeof(struct uma_cache) * (maxcpu - 1));
1233 	args.ctor = zone_ctor;
1234 	args.dtor = zone_dtor;
1235 	args.uminit = zero_init;
1236 	args.fini = NULL;
1237 	args.align = 32 - 1;
1238 	args.flags = UMA_ZFLAG_INTERNAL;
1239 	/* The initial zone has no Per cpu queues so it's smaller */
1240 	zone_ctor(zones, sizeof(struct uma_zone), &args);
1241 
1242 	/* Initialize the pcpu cache lock set once and for all */
1243 	for (i = 0; i < maxcpu; i++)
1244 		CPU_LOCK_INIT(i);
1245 
1246 #ifdef UMA_DEBUG
1247 	printf("Filling boot free list.\n");
1248 #endif
1249 	for (i = 0; i < UMA_BOOT_PAGES; i++) {
1250 		slab = (uma_slab_t)((u_int8_t *)bootmem + (i * UMA_SLAB_SIZE));
1251 		slab->us_data = (u_int8_t *)slab;
1252 		slab->us_flags = UMA_SLAB_BOOT;
1253 		LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
1254 		uma_boot_free++;
1255 	}
1256 
1257 #ifdef UMA_DEBUG
1258 	printf("Creating slab zone.\n");
1259 #endif
1260 
1261 	/*
1262 	 * This is the max number of free list items we'll have with
1263 	 * offpage slabs.
1264 	 */
1265 	slabsize = UMA_SLAB_SIZE - sizeof(struct uma_slab);
1266 	slabsize /= UMA_MAX_WASTE;
1267 	slabsize++;			/* In case there it's rounded */
1268 	slabsize += sizeof(struct uma_slab);
1269 
1270 	/* Now make a zone for slab headers */
1271 	slabzone = uma_zcreate("UMA Slabs",
1272 				slabsize,
1273 				NULL, NULL, NULL, NULL,
1274 				UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1275 
1276 	hashzone = uma_zcreate("UMA Hash",
1277 	    sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1278 	    NULL, NULL, NULL, NULL,
1279 	    UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1280 
1281 	bucket_init();
1282 
1283 #ifdef UMA_MD_SMALL_ALLOC
1284 	booted = 1;
1285 #endif
1286 
1287 #ifdef UMA_DEBUG
1288 	printf("UMA startup complete.\n");
1289 #endif
1290 }
1291 
1292 /* see uma.h */
1293 void
1294 uma_startup2(void)
1295 {
1296 	booted = 1;
1297 	bucket_enable();
1298 #ifdef UMA_DEBUG
1299 	printf("UMA startup2 complete.\n");
1300 #endif
1301 }
1302 
1303 /*
1304  * Initialize our callout handle
1305  *
1306  */
1307 
1308 static void
1309 uma_startup3(void)
1310 {
1311 #ifdef UMA_DEBUG
1312 	printf("Starting callout.\n");
1313 #endif
1314 	callout_init(&uma_callout, 0);
1315 	callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1316 #ifdef UMA_DEBUG
1317 	printf("UMA startup3 complete.\n");
1318 #endif
1319 }
1320 
1321 /* See uma.h */
1322 uma_zone_t
1323 uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1324 		uma_init uminit, uma_fini fini, int align, u_int16_t flags)
1325 
1326 {
1327 	struct uma_zctor_args args;
1328 
1329 	/* This stuff is essential for the zone ctor */
1330 	args.name = name;
1331 	args.size = size;
1332 	args.ctor = ctor;
1333 	args.dtor = dtor;
1334 	args.uminit = uminit;
1335 	args.fini = fini;
1336 	args.align = align;
1337 	args.flags = flags;
1338 
1339 	return (uma_zalloc_internal(zones, &args, M_WAITOK));
1340 }
1341 
1342 /* See uma.h */
1343 void
1344 uma_zdestroy(uma_zone_t zone)
1345 {
1346 	uma_zfree_internal(zones, zone, NULL, 0);
1347 }
1348 
1349 /* See uma.h */
1350 void *
1351 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
1352 {
1353 	void *item;
1354 	uma_cache_t cache;
1355 	uma_bucket_t bucket;
1356 	int cpu;
1357 
1358 	/* This is the fast path allocation */
1359 #ifdef UMA_DEBUG_ALLOC_1
1360 	printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
1361 #endif
1362 
1363 #ifdef INVARIANTS
1364 	/*
1365 	 * To make sure that WAITOK or NOWAIT is set, but not more than
1366 	 * one, and check against the API botches that are common.
1367 	 * The uma code implies M_WAITOK if M_NOWAIT is not set, so
1368 	 * we default to waiting if none of the flags is set.
1369 	 */
1370 	cpu = flags & (M_WAITOK | M_NOWAIT | M_DONTWAIT | M_TRYWAIT);
1371 	if (cpu != M_NOWAIT && cpu != M_WAITOK) {
1372 		static	struct timeval lasterr;
1373 		static	int curerr, once;
1374 		if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
1375 			printf("Bad uma_zalloc flags: %x\n", cpu);
1376 			backtrace();
1377 			once++;
1378 		}
1379 	}
1380 #endif
1381 	if (!(flags & M_NOWAIT)) {
1382 		KASSERT(curthread->td_intr_nesting_level == 0,
1383 		   ("malloc(M_WAITOK) in interrupt context"));
1384 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1385 		    "malloc() of \"%s\"", zone->uz_name);
1386 	}
1387 
1388 zalloc_restart:
1389 	cpu = PCPU_GET(cpuid);
1390 	CPU_LOCK(cpu);
1391 	cache = &zone->uz_cpu[cpu];
1392 
1393 zalloc_start:
1394 	bucket = cache->uc_allocbucket;
1395 
1396 	if (bucket) {
1397 		if (bucket->ub_cnt > 0) {
1398 			bucket->ub_cnt--;
1399 			item = bucket->ub_bucket[bucket->ub_cnt];
1400 #ifdef INVARIANTS
1401 			bucket->ub_bucket[bucket->ub_cnt] = NULL;
1402 #endif
1403 			KASSERT(item != NULL,
1404 			    ("uma_zalloc: Bucket pointer mangled."));
1405 			cache->uc_allocs++;
1406 #ifdef INVARIANTS
1407 			ZONE_LOCK(zone);
1408 			uma_dbg_alloc(zone, NULL, item);
1409 			ZONE_UNLOCK(zone);
1410 #endif
1411 			CPU_UNLOCK(cpu);
1412 			if (zone->uz_ctor)
1413 				zone->uz_ctor(item, zone->uz_size, udata);
1414 			if (flags & M_ZERO)
1415 				bzero(item, zone->uz_size);
1416 			return (item);
1417 		} else if (cache->uc_freebucket) {
1418 			/*
1419 			 * We have run out of items in our allocbucket.
1420 			 * See if we can switch with our free bucket.
1421 			 */
1422 			if (cache->uc_freebucket->ub_cnt > 0) {
1423 #ifdef UMA_DEBUG_ALLOC
1424 				printf("uma_zalloc: Swapping empty with"
1425 				    " alloc.\n");
1426 #endif
1427 				bucket = cache->uc_freebucket;
1428 				cache->uc_freebucket = cache->uc_allocbucket;
1429 				cache->uc_allocbucket = bucket;
1430 
1431 				goto zalloc_start;
1432 			}
1433 		}
1434 	}
1435 	ZONE_LOCK(zone);
1436 	/* Since we have locked the zone we may as well send back our stats */
1437 	zone->uz_allocs += cache->uc_allocs;
1438 	cache->uc_allocs = 0;
1439 
1440 	/* Our old one is now a free bucket */
1441 	if (cache->uc_allocbucket) {
1442 		KASSERT(cache->uc_allocbucket->ub_cnt == 0,
1443 		    ("uma_zalloc_arg: Freeing a non free bucket."));
1444 		LIST_INSERT_HEAD(&zone->uz_free_bucket,
1445 		    cache->uc_allocbucket, ub_link);
1446 		cache->uc_allocbucket = NULL;
1447 	}
1448 
1449 	/* Check the free list for a new alloc bucket */
1450 	if ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
1451 		KASSERT(bucket->ub_cnt != 0,
1452 		    ("uma_zalloc_arg: Returning an empty bucket."));
1453 
1454 		LIST_REMOVE(bucket, ub_link);
1455 		cache->uc_allocbucket = bucket;
1456 		ZONE_UNLOCK(zone);
1457 		goto zalloc_start;
1458 	}
1459 	/* We are no longer associated with this cpu!!! */
1460 	CPU_UNLOCK(cpu);
1461 
1462 	/* Bump up our uz_count so we get here less */
1463 	if (zone->uz_count < BUCKET_MAX)
1464 		zone->uz_count++;
1465 	/*
1466 	 * Now lets just fill a bucket and put it on the free list.  If that
1467 	 * works we'll restart the allocation from the begining.
1468 	 */
1469 	if (uma_zalloc_bucket(zone, flags)) {
1470 		ZONE_UNLOCK(zone);
1471 		goto zalloc_restart;
1472 	}
1473 	ZONE_UNLOCK(zone);
1474 	/*
1475 	 * We may not be able to get a bucket so return an actual item.
1476 	 */
1477 #ifdef UMA_DEBUG
1478 	printf("uma_zalloc_arg: Bucketzone returned NULL\n");
1479 #endif
1480 
1481 	return (uma_zalloc_internal(zone, udata, flags));
1482 }
1483 
1484 static uma_slab_t
1485 uma_zone_slab(uma_zone_t zone, int flags)
1486 {
1487 	uma_slab_t slab;
1488 
1489 	/*
1490 	 * This is to prevent us from recursively trying to allocate
1491 	 * buckets.  The problem is that if an allocation forces us to
1492 	 * grab a new bucket we will call page_alloc, which will go off
1493 	 * and cause the vm to allocate vm_map_entries.  If we need new
1494 	 * buckets there too we will recurse in kmem_alloc and bad
1495 	 * things happen.  So instead we return a NULL bucket, and make
1496 	 * the code that allocates buckets smart enough to deal with it
1497 	 */
1498 	if (zone->uz_flags & UMA_ZFLAG_INTERNAL && zone->uz_recurse != 0)
1499 		return (NULL);
1500 
1501 	slab = NULL;
1502 
1503 	for (;;) {
1504 		/*
1505 		 * Find a slab with some space.  Prefer slabs that are partially
1506 		 * used over those that are totally full.  This helps to reduce
1507 		 * fragmentation.
1508 		 */
1509 		if (zone->uz_free != 0) {
1510 			if (!LIST_EMPTY(&zone->uz_part_slab)) {
1511 				slab = LIST_FIRST(&zone->uz_part_slab);
1512 			} else {
1513 				slab = LIST_FIRST(&zone->uz_free_slab);
1514 				LIST_REMOVE(slab, us_link);
1515 				LIST_INSERT_HEAD(&zone->uz_part_slab, slab,
1516 				us_link);
1517 			}
1518 			return (slab);
1519 		}
1520 
1521 		/*
1522 		 * M_NOVM means don't ask at all!
1523 		 */
1524 		if (flags & M_NOVM)
1525 			break;
1526 
1527 		if (zone->uz_maxpages &&
1528 		    zone->uz_pages >= zone->uz_maxpages) {
1529 			zone->uz_flags |= UMA_ZFLAG_FULL;
1530 
1531 			if (flags & M_NOWAIT)
1532 				break;
1533 			else
1534 				msleep(zone, &zone->uz_lock, PVM,
1535 				    "zonelimit", 0);
1536 			continue;
1537 		}
1538 		zone->uz_recurse++;
1539 		slab = slab_zalloc(zone, flags);
1540 		zone->uz_recurse--;
1541 		/*
1542 		 * If we got a slab here it's safe to mark it partially used
1543 		 * and return.  We assume that the caller is going to remove
1544 		 * at least one item.
1545 		 */
1546 		if (slab) {
1547 			LIST_INSERT_HEAD(&zone->uz_part_slab, slab, us_link);
1548 			return (slab);
1549 		}
1550 		/*
1551 		 * We might not have been able to get a slab but another cpu
1552 		 * could have while we were unlocked.  Check again before we
1553 		 * fail.
1554 		 */
1555 		if (flags & M_NOWAIT)
1556 			flags |= M_NOVM;
1557 	}
1558 	return (slab);
1559 }
1560 
1561 static void *
1562 uma_slab_alloc(uma_zone_t zone, uma_slab_t slab)
1563 {
1564 	void *item;
1565 	u_int8_t freei;
1566 
1567 	freei = slab->us_firstfree;
1568 	slab->us_firstfree = slab->us_freelist[freei];
1569 	item = slab->us_data + (zone->uz_rsize * freei);
1570 
1571 	slab->us_freecount--;
1572 	zone->uz_free--;
1573 #ifdef INVARIANTS
1574 	uma_dbg_alloc(zone, slab, item);
1575 #endif
1576 	/* Move this slab to the full list */
1577 	if (slab->us_freecount == 0) {
1578 		LIST_REMOVE(slab, us_link);
1579 		LIST_INSERT_HEAD(&zone->uz_full_slab, slab, us_link);
1580 	}
1581 
1582 	return (item);
1583 }
1584 
1585 static int
1586 uma_zalloc_bucket(uma_zone_t zone, int flags)
1587 {
1588 	uma_bucket_t bucket;
1589 	uma_slab_t slab;
1590 	int max;
1591 
1592 	/*
1593 	 * Try this zone's free list first so we don't allocate extra buckets.
1594 	 */
1595 	if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
1596 		KASSERT(bucket->ub_cnt == 0,
1597 		    ("uma_zalloc_bucket: Bucket on free list is not empty."));
1598 		LIST_REMOVE(bucket, ub_link);
1599 	} else {
1600 		int bflags;
1601 
1602 		bflags = (flags & ~M_ZERO);
1603 		if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
1604 			bflags |= M_NOVM;
1605 
1606 		ZONE_UNLOCK(zone);
1607 		bucket = bucket_alloc(zone->uz_count, bflags);
1608 		ZONE_LOCK(zone);
1609 	}
1610 
1611 	if (bucket == NULL)
1612 		return (0);
1613 
1614 #ifdef SMP
1615 	/*
1616 	 * This code is here to limit the number of simultaneous bucket fills
1617 	 * for any given zone to the number of per cpu caches in this zone. This
1618 	 * is done so that we don't allocate more memory than we really need.
1619 	 */
1620 	if (zone->uz_fills >= mp_ncpus)
1621 		goto done;
1622 
1623 #endif
1624 	zone->uz_fills++;
1625 
1626 	max = MIN(bucket->ub_entries, zone->uz_count);
1627 	/* Try to keep the buckets totally full */
1628 	while (bucket->ub_cnt < max &&
1629 	    (slab = uma_zone_slab(zone, flags)) != NULL) {
1630 		while (slab->us_freecount && bucket->ub_cnt < max) {
1631 			bucket->ub_bucket[bucket->ub_cnt++] =
1632 			    uma_slab_alloc(zone, slab);
1633 		}
1634 		/* Don't block on the next fill */
1635 		flags |= M_NOWAIT;
1636 	}
1637 
1638 	zone->uz_fills--;
1639 
1640 	if (bucket->ub_cnt != 0) {
1641 		LIST_INSERT_HEAD(&zone->uz_full_bucket,
1642 		    bucket, ub_link);
1643 		return (1);
1644 	}
1645 #ifdef SMP
1646 done:
1647 #endif
1648 	bucket_free(bucket);
1649 
1650 	return (0);
1651 }
1652 /*
1653  * Allocates an item for an internal zone
1654  *
1655  * Arguments
1656  *	zone   The zone to alloc for.
1657  *	udata  The data to be passed to the constructor.
1658  *	flags  M_WAITOK, M_NOWAIT, M_ZERO.
1659  *
1660  * Returns
1661  *	NULL if there is no memory and M_NOWAIT is set
1662  *	An item if successful
1663  */
1664 
1665 static void *
1666 uma_zalloc_internal(uma_zone_t zone, void *udata, int flags)
1667 {
1668 	uma_slab_t slab;
1669 	void *item;
1670 
1671 	item = NULL;
1672 
1673 #ifdef UMA_DEBUG_ALLOC
1674 	printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
1675 #endif
1676 	ZONE_LOCK(zone);
1677 
1678 	slab = uma_zone_slab(zone, flags);
1679 	if (slab == NULL) {
1680 		ZONE_UNLOCK(zone);
1681 		return (NULL);
1682 	}
1683 
1684 	item = uma_slab_alloc(zone, slab);
1685 
1686 	ZONE_UNLOCK(zone);
1687 
1688 	if (zone->uz_ctor != NULL)
1689 		zone->uz_ctor(item, zone->uz_size, udata);
1690 	if (flags & M_ZERO)
1691 		bzero(item, zone->uz_size);
1692 
1693 	return (item);
1694 }
1695 
1696 /* See uma.h */
1697 void
1698 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
1699 {
1700 	uma_cache_t cache;
1701 	uma_bucket_t bucket;
1702 	int bflags;
1703 	int cpu;
1704 	int skip;
1705 
1706 	/* This is the fast path free */
1707 	skip = 0;
1708 #ifdef UMA_DEBUG_ALLOC_1
1709 	printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
1710 #endif
1711 	/*
1712 	 * The race here is acceptable.  If we miss it we'll just have to wait
1713 	 * a little longer for the limits to be reset.
1714 	 */
1715 
1716 	if (zone->uz_flags & UMA_ZFLAG_FULL)
1717 		goto zfree_internal;
1718 
1719 	if (zone->uz_dtor) {
1720 		zone->uz_dtor(item, zone->uz_size, udata);
1721 		skip = 1;
1722 	}
1723 
1724 zfree_restart:
1725 	cpu = PCPU_GET(cpuid);
1726 	CPU_LOCK(cpu);
1727 	cache = &zone->uz_cpu[cpu];
1728 
1729 zfree_start:
1730 	bucket = cache->uc_freebucket;
1731 
1732 	if (bucket) {
1733 		/*
1734 		 * Do we have room in our bucket? It is OK for this uz count
1735 		 * check to be slightly out of sync.
1736 		 */
1737 
1738 		if (bucket->ub_cnt < bucket->ub_entries) {
1739 			KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
1740 			    ("uma_zfree: Freeing to non free bucket index."));
1741 			bucket->ub_bucket[bucket->ub_cnt] = item;
1742 			bucket->ub_cnt++;
1743 #ifdef INVARIANTS
1744 			ZONE_LOCK(zone);
1745 			if (zone->uz_flags & UMA_ZONE_MALLOC)
1746 				uma_dbg_free(zone, udata, item);
1747 			else
1748 				uma_dbg_free(zone, NULL, item);
1749 			ZONE_UNLOCK(zone);
1750 #endif
1751 			CPU_UNLOCK(cpu);
1752 			return;
1753 		} else if (cache->uc_allocbucket) {
1754 #ifdef UMA_DEBUG_ALLOC
1755 			printf("uma_zfree: Swapping buckets.\n");
1756 #endif
1757 			/*
1758 			 * We have run out of space in our freebucket.
1759 			 * See if we can switch with our alloc bucket.
1760 			 */
1761 			if (cache->uc_allocbucket->ub_cnt <
1762 			    cache->uc_freebucket->ub_cnt) {
1763 				bucket = cache->uc_freebucket;
1764 				cache->uc_freebucket = cache->uc_allocbucket;
1765 				cache->uc_allocbucket = bucket;
1766 				goto zfree_start;
1767 			}
1768 		}
1769 	}
1770 	/*
1771 	 * We can get here for two reasons:
1772 	 *
1773 	 * 1) The buckets are NULL
1774 	 * 2) The alloc and free buckets are both somewhat full.
1775 	 */
1776 
1777 	ZONE_LOCK(zone);
1778 
1779 	bucket = cache->uc_freebucket;
1780 	cache->uc_freebucket = NULL;
1781 
1782 	/* Can we throw this on the zone full list? */
1783 	if (bucket != NULL) {
1784 #ifdef UMA_DEBUG_ALLOC
1785 		printf("uma_zfree: Putting old bucket on the free list.\n");
1786 #endif
1787 		/* ub_cnt is pointing to the last free item */
1788 		KASSERT(bucket->ub_cnt != 0,
1789 		    ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
1790 		LIST_INSERT_HEAD(&zone->uz_full_bucket,
1791 		    bucket, ub_link);
1792 	}
1793 	if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
1794 		LIST_REMOVE(bucket, ub_link);
1795 		ZONE_UNLOCK(zone);
1796 		cache->uc_freebucket = bucket;
1797 		goto zfree_start;
1798 	}
1799 	/* We're done with this CPU now */
1800 	CPU_UNLOCK(cpu);
1801 
1802 	/* And the zone.. */
1803 	ZONE_UNLOCK(zone);
1804 
1805 #ifdef UMA_DEBUG_ALLOC
1806 	printf("uma_zfree: Allocating new free bucket.\n");
1807 #endif
1808 	bflags = M_NOWAIT;
1809 
1810 	if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
1811 		bflags |= M_NOVM;
1812 	bucket = bucket_alloc(zone->uz_count, bflags);
1813 	if (bucket) {
1814 		ZONE_LOCK(zone);
1815 		LIST_INSERT_HEAD(&zone->uz_free_bucket,
1816 		    bucket, ub_link);
1817 		ZONE_UNLOCK(zone);
1818 		goto zfree_restart;
1819 	}
1820 
1821 	/*
1822 	 * If nothing else caught this, we'll just do an internal free.
1823 	 */
1824 
1825 zfree_internal:
1826 
1827 #ifdef INVARIANTS
1828 	/*
1829 	 * If we need to skip the dtor and the uma_dbg_free in
1830 	 * uma_zfree_internal because we've already called the dtor
1831 	 * above, but we ended up here, then we need to make sure
1832 	 * that we take care of the uma_dbg_free immediately.
1833 	 */
1834 	if (skip) {
1835 		ZONE_LOCK(zone);
1836 		if (zone->uz_flags & UMA_ZONE_MALLOC)
1837 			uma_dbg_free(zone, udata, item);
1838 		else
1839 			uma_dbg_free(zone, NULL, item);
1840 		ZONE_UNLOCK(zone);
1841 	}
1842 #endif
1843 	uma_zfree_internal(zone, item, udata, skip);
1844 
1845 	return;
1846 
1847 }
1848 
1849 /*
1850  * Frees an item to an INTERNAL zone or allocates a free bucket
1851  *
1852  * Arguments:
1853  *	zone   The zone to free to
1854  *	item   The item we're freeing
1855  *	udata  User supplied data for the dtor
1856  *	skip   Skip the dtor, it was done in uma_zfree_arg
1857  */
1858 static void
1859 uma_zfree_internal(uma_zone_t zone, void *item, void *udata, int skip)
1860 {
1861 	uma_slab_t slab;
1862 	u_int8_t *mem;
1863 	u_int8_t freei;
1864 
1865 	if (!skip && zone->uz_dtor)
1866 		zone->uz_dtor(item, zone->uz_size, udata);
1867 
1868 	ZONE_LOCK(zone);
1869 
1870 	if (!(zone->uz_flags & UMA_ZONE_MALLOC)) {
1871 		mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK));
1872 		if (zone->uz_flags & UMA_ZONE_HASH)
1873 			slab = hash_sfind(&zone->uz_hash, mem);
1874 		else {
1875 			mem += zone->uz_pgoff;
1876 			slab = (uma_slab_t)mem;
1877 		}
1878 	} else {
1879 		slab = (uma_slab_t)udata;
1880 	}
1881 
1882 	/* Do we need to remove from any lists? */
1883 	if (slab->us_freecount+1 == zone->uz_ipers) {
1884 		LIST_REMOVE(slab, us_link);
1885 		LIST_INSERT_HEAD(&zone->uz_free_slab, slab, us_link);
1886 	} else if (slab->us_freecount == 0) {
1887 		LIST_REMOVE(slab, us_link);
1888 		LIST_INSERT_HEAD(&zone->uz_part_slab, slab, us_link);
1889 	}
1890 
1891 	/* Slab management stuff */
1892 	freei = ((unsigned long)item - (unsigned long)slab->us_data)
1893 		/ zone->uz_rsize;
1894 
1895 #ifdef INVARIANTS
1896 	if (!skip)
1897 		uma_dbg_free(zone, slab, item);
1898 #endif
1899 
1900 	slab->us_freelist[freei] = slab->us_firstfree;
1901 	slab->us_firstfree = freei;
1902 	slab->us_freecount++;
1903 
1904 	/* Zone statistics */
1905 	zone->uz_free++;
1906 
1907 	if (zone->uz_flags & UMA_ZFLAG_FULL) {
1908 		if (zone->uz_pages < zone->uz_maxpages)
1909 			zone->uz_flags &= ~UMA_ZFLAG_FULL;
1910 
1911 		/* We can handle one more allocation */
1912 		wakeup_one(zone);
1913 	}
1914 
1915 	ZONE_UNLOCK(zone);
1916 }
1917 
1918 /* See uma.h */
1919 void
1920 uma_zone_set_max(uma_zone_t zone, int nitems)
1921 {
1922 	ZONE_LOCK(zone);
1923 	if (zone->uz_ppera > 1)
1924 		zone->uz_maxpages = nitems * zone->uz_ppera;
1925 	else
1926 		zone->uz_maxpages = nitems / zone->uz_ipers;
1927 
1928 	if (zone->uz_maxpages * zone->uz_ipers < nitems)
1929 		zone->uz_maxpages++;
1930 
1931 	ZONE_UNLOCK(zone);
1932 }
1933 
1934 /* See uma.h */
1935 void
1936 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
1937 {
1938 	ZONE_LOCK(zone);
1939 	zone->uz_freef = freef;
1940 	ZONE_UNLOCK(zone);
1941 }
1942 
1943 /* See uma.h */
1944 void
1945 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
1946 {
1947 	ZONE_LOCK(zone);
1948 	zone->uz_flags |= UMA_ZFLAG_PRIVALLOC;
1949 	zone->uz_allocf = allocf;
1950 	ZONE_UNLOCK(zone);
1951 }
1952 
1953 /* See uma.h */
1954 int
1955 uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int count)
1956 {
1957 	int pages;
1958 	vm_offset_t kva;
1959 
1960 	mtx_lock(&Giant);
1961 
1962 	pages = count / zone->uz_ipers;
1963 
1964 	if (pages * zone->uz_ipers < count)
1965 		pages++;
1966 
1967 	kva = kmem_alloc_pageable(kernel_map, pages * UMA_SLAB_SIZE);
1968 
1969 	if (kva == 0) {
1970 		mtx_unlock(&Giant);
1971 		return (0);
1972 	}
1973 	if (obj == NULL) {
1974 		obj = vm_object_allocate(OBJT_DEFAULT,
1975 		    pages);
1976 	} else {
1977 		VM_OBJECT_LOCK_INIT(obj);
1978 		_vm_object_allocate(OBJT_DEFAULT,
1979 		    pages, obj);
1980 	}
1981 	ZONE_LOCK(zone);
1982 	zone->uz_kva = kva;
1983 	zone->uz_obj = obj;
1984 	zone->uz_maxpages = pages;
1985 	zone->uz_allocf = obj_alloc;
1986 	zone->uz_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC;
1987 	ZONE_UNLOCK(zone);
1988 	mtx_unlock(&Giant);
1989 
1990 	return (1);
1991 }
1992 
1993 /* See uma.h */
1994 void
1995 uma_prealloc(uma_zone_t zone, int items)
1996 {
1997 	int slabs;
1998 	uma_slab_t slab;
1999 
2000 	ZONE_LOCK(zone);
2001 	slabs = items / zone->uz_ipers;
2002 	if (slabs * zone->uz_ipers < items)
2003 		slabs++;
2004 	while (slabs > 0) {
2005 		slab = slab_zalloc(zone, M_WAITOK);
2006 		LIST_INSERT_HEAD(&zone->uz_free_slab, slab, us_link);
2007 		slabs--;
2008 	}
2009 	ZONE_UNLOCK(zone);
2010 }
2011 
2012 /* See uma.h */
2013 void
2014 uma_reclaim(void)
2015 {
2016 #ifdef UMA_DEBUG
2017 	printf("UMA: vm asked us to release pages!\n");
2018 #endif
2019 	bucket_enable();
2020 	zone_foreach(zone_drain);
2021 	/*
2022 	 * Some slabs may have been freed but this zone will be visited early
2023 	 * we visit again so that we can free pages that are empty once other
2024 	 * zones are drained.  We have to do the same for buckets.
2025 	 */
2026 	zone_drain(slabzone);
2027 	bucket_zone_drain();
2028 }
2029 
2030 void *
2031 uma_large_malloc(int size, int wait)
2032 {
2033 	void *mem;
2034 	uma_slab_t slab;
2035 	u_int8_t flags;
2036 
2037 	slab = uma_zalloc_internal(slabzone, NULL, wait);
2038 	if (slab == NULL)
2039 		return (NULL);
2040 	mem = page_alloc(NULL, size, &flags, wait);
2041 	if (mem) {
2042 		vsetslab((vm_offset_t)mem, slab);
2043 		slab->us_data = mem;
2044 		slab->us_flags = flags | UMA_SLAB_MALLOC;
2045 		slab->us_size = size;
2046 	} else {
2047 		uma_zfree_internal(slabzone, slab, NULL, 0);
2048 	}
2049 
2050 
2051 	return (mem);
2052 }
2053 
2054 void
2055 uma_large_free(uma_slab_t slab)
2056 {
2057 	vsetobj((vm_offset_t)slab->us_data, kmem_object);
2058 	/*
2059 	 * XXX: We get a lock order reversal if we don't have Giant:
2060 	 * vm_map_remove (locks system map) -> vm_map_delete ->
2061 	 *    vm_map_entry_unwire -> vm_fault_unwire -> mtx_lock(&Giant)
2062 	 */
2063 	if (!mtx_owned(&Giant)) {
2064 		mtx_lock(&Giant);
2065 		page_free(slab->us_data, slab->us_size, slab->us_flags);
2066 		mtx_unlock(&Giant);
2067 	} else
2068 		page_free(slab->us_data, slab->us_size, slab->us_flags);
2069 	uma_zfree_internal(slabzone, slab, NULL, 0);
2070 }
2071 
2072 void
2073 uma_print_stats(void)
2074 {
2075 	zone_foreach(uma_print_zone);
2076 }
2077 
2078 void
2079 uma_print_zone(uma_zone_t zone)
2080 {
2081 	printf("%s(%p) size %d(%d) flags %d ipers %d ppera %d out %d free %d\n",
2082 	    zone->uz_name, zone, zone->uz_size, zone->uz_rsize, zone->uz_flags,
2083 	    zone->uz_ipers, zone->uz_ppera,
2084 	    (zone->uz_ipers * zone->uz_pages) - zone->uz_free, zone->uz_free);
2085 }
2086 
2087 /*
2088  * Sysctl handler for vm.zone
2089  *
2090  * stolen from vm_zone.c
2091  */
2092 static int
2093 sysctl_vm_zone(SYSCTL_HANDLER_ARGS)
2094 {
2095 	int error, len, cnt;
2096 	const int linesize = 128;	/* conservative */
2097 	int totalfree;
2098 	char *tmpbuf, *offset;
2099 	uma_zone_t z;
2100 	char *p;
2101 	int cpu;
2102 	int cachefree;
2103 	uma_bucket_t bucket;
2104 	uma_cache_t cache;
2105 
2106 	cnt = 0;
2107 	mtx_lock(&uma_mtx);
2108 	LIST_FOREACH(z, &uma_zones, uz_link)
2109 		cnt++;
2110 	mtx_unlock(&uma_mtx);
2111 	MALLOC(tmpbuf, char *, (cnt == 0 ? 1 : cnt) * linesize,
2112 			M_TEMP, M_WAITOK);
2113 	len = snprintf(tmpbuf, linesize,
2114 	    "\nITEM            SIZE     LIMIT     USED    FREE  REQUESTS\n\n");
2115 	if (cnt == 0)
2116 		tmpbuf[len - 1] = '\0';
2117 	error = SYSCTL_OUT(req, tmpbuf, cnt == 0 ? len-1 : len);
2118 	if (error || cnt == 0)
2119 		goto out;
2120 	offset = tmpbuf;
2121 	mtx_lock(&uma_mtx);
2122 	LIST_FOREACH(z, &uma_zones, uz_link) {
2123 		if (cnt == 0)	/* list may have changed size */
2124 			break;
2125 		if (!(z->uz_flags & UMA_ZFLAG_INTERNAL)) {
2126 			for (cpu = 0; cpu < maxcpu; cpu++) {
2127 				if (CPU_ABSENT(cpu))
2128 					continue;
2129 				CPU_LOCK(cpu);
2130 			}
2131 		}
2132 		ZONE_LOCK(z);
2133 		cachefree = 0;
2134 		if (!(z->uz_flags & UMA_ZFLAG_INTERNAL)) {
2135 			for (cpu = 0; cpu < maxcpu; cpu++) {
2136 				if (CPU_ABSENT(cpu))
2137 					continue;
2138 				cache = &z->uz_cpu[cpu];
2139 				if (cache->uc_allocbucket != NULL)
2140 					cachefree += cache->uc_allocbucket->ub_cnt;
2141 				if (cache->uc_freebucket != NULL)
2142 					cachefree += cache->uc_freebucket->ub_cnt;
2143 				CPU_UNLOCK(cpu);
2144 			}
2145 		}
2146 		LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link) {
2147 			cachefree += bucket->ub_cnt;
2148 		}
2149 		totalfree = z->uz_free + cachefree;
2150 		len = snprintf(offset, linesize,
2151 		    "%-12.12s  %6.6u, %8.8u, %6.6u, %6.6u, %8.8llu\n",
2152 		    z->uz_name, z->uz_size,
2153 		    z->uz_maxpages * z->uz_ipers,
2154 		    (z->uz_ipers * (z->uz_pages / z->uz_ppera)) - totalfree,
2155 		    totalfree,
2156 		    (unsigned long long)z->uz_allocs);
2157 		ZONE_UNLOCK(z);
2158 		for (p = offset + 12; p > offset && *p == ' '; --p)
2159 			/* nothing */ ;
2160 		p[1] = ':';
2161 		cnt--;
2162 		offset += len;
2163 	}
2164 	mtx_unlock(&uma_mtx);
2165 	*offset++ = '\0';
2166 	error = SYSCTL_OUT(req, tmpbuf, offset - tmpbuf);
2167 out:
2168 	FREE(tmpbuf, M_TEMP);
2169 	return (error);
2170 }
2171