xref: /netbsd-src/sys/kern/subr_pool.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*	$NetBSD: subr_pool.c,v 1.166 2008/07/09 02:43:53 yamt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1999, 2000, 2002, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center, and by Andrew Doran.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.166 2008/07/09 02:43:53 yamt Exp $");
35 
36 #include "opt_ddb.h"
37 #include "opt_pool.h"
38 #include "opt_poollog.h"
39 #include "opt_lockdebug.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/bitops.h>
44 #include <sys/proc.h>
45 #include <sys/errno.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/pool.h>
49 #include <sys/syslog.h>
50 #include <sys/debug.h>
51 #include <sys/lockdebug.h>
52 #include <sys/xcall.h>
53 #include <sys/cpu.h>
54 #include <sys/atomic.h>
55 
56 #include <uvm/uvm.h>
57 
58 /*
59  * Pool resource management utility.
60  *
61  * Memory is allocated in pages which are split into pieces according to
62  * the pool item size. Each page is kept on one of three lists in the
63  * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages',
64  * for empty, full and partially-full pages respectively. The individual
65  * pool items are on a linked list headed by `ph_itemlist' in each page
66  * header. The memory for building the page list is either taken from
67  * the allocated pages themselves (for small pool items) or taken from
68  * an internal pool of page headers (`phpool').
69  */
70 
71 /* List of all pools */
72 TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
73 
74 /* Private pool for page header structures */
75 #define	PHPOOL_MAX	8
76 static struct pool phpool[PHPOOL_MAX];
77 #define	PHPOOL_FREELIST_NELEM(idx) \
78 	(((idx) == 0) ? 0 : BITMAP_SIZE * (1 << (idx)))
79 
80 #ifdef POOL_SUBPAGE
81 /* Pool of subpages for use by normal pools. */
82 static struct pool psppool;
83 #endif
84 
85 static SLIST_HEAD(, pool_allocator) pa_deferinitq =
86     SLIST_HEAD_INITIALIZER(pa_deferinitq);
87 
88 static void *pool_page_alloc_meta(struct pool *, int);
89 static void pool_page_free_meta(struct pool *, void *);
90 
91 /* allocator for pool metadata */
92 struct pool_allocator pool_allocator_meta = {
93 	pool_page_alloc_meta, pool_page_free_meta,
94 	.pa_backingmapptr = &kmem_map,
95 };
96 
97 /* # of seconds to retain page after last use */
98 int pool_inactive_time = 10;
99 
100 /* Next candidate for drainage (see pool_drain()) */
101 static struct pool	*drainpp;
102 
103 /* This lock protects both pool_head and drainpp. */
104 static kmutex_t pool_head_lock;
105 static kcondvar_t pool_busy;
106 
107 typedef uint32_t pool_item_bitmap_t;
108 #define	BITMAP_SIZE	(CHAR_BIT * sizeof(pool_item_bitmap_t))
109 #define	BITMAP_MASK	(BITMAP_SIZE - 1)
110 
111 struct pool_item_header {
112 	/* Page headers */
113 	LIST_ENTRY(pool_item_header)
114 				ph_pagelist;	/* pool page list */
115 	SPLAY_ENTRY(pool_item_header)
116 				ph_node;	/* Off-page page headers */
117 	void *			ph_page;	/* this page's address */
118 	uint32_t		ph_time;	/* last referenced */
119 	uint16_t		ph_nmissing;	/* # of chunks in use */
120 	uint16_t		ph_off;		/* start offset in page */
121 	union {
122 		/* !PR_NOTOUCH */
123 		struct {
124 			LIST_HEAD(, pool_item)
125 				phu_itemlist;	/* chunk list for this page */
126 		} phu_normal;
127 		/* PR_NOTOUCH */
128 		struct {
129 			pool_item_bitmap_t phu_bitmap[1];
130 		} phu_notouch;
131 	} ph_u;
132 };
133 #define	ph_itemlist	ph_u.phu_normal.phu_itemlist
134 #define	ph_bitmap	ph_u.phu_notouch.phu_bitmap
135 
136 struct pool_item {
137 #ifdef DIAGNOSTIC
138 	u_int pi_magic;
139 #endif
140 #define	PI_MAGIC 0xdeaddeadU
141 	/* Other entries use only this list entry */
142 	LIST_ENTRY(pool_item)	pi_list;
143 };
144 
145 #define	POOL_NEEDS_CATCHUP(pp)						\
146 	((pp)->pr_nitems < (pp)->pr_minitems)
147 
148 /*
149  * Pool cache management.
150  *
151  * Pool caches provide a way for constructed objects to be cached by the
152  * pool subsystem.  This can lead to performance improvements by avoiding
153  * needless object construction/destruction; it is deferred until absolutely
154  * necessary.
155  *
156  * Caches are grouped into cache groups.  Each cache group references up
157  * to PCG_NUMOBJECTS constructed objects.  When a cache allocates an
158  * object from the pool, it calls the object's constructor and places it
159  * into a cache group.  When a cache group frees an object back to the
160  * pool, it first calls the object's destructor.  This allows the object
161  * to persist in constructed form while freed to the cache.
162  *
163  * The pool references each cache, so that when a pool is drained by the
164  * pagedaemon, it can drain each individual cache as well.  Each time a
165  * cache is drained, the most idle cache group is freed to the pool in
166  * its entirety.
167  *
168  * Pool caches are layed on top of pools.  By layering them, we can avoid
169  * the complexity of cache management for pools which would not benefit
170  * from it.
171  */
172 
173 static struct pool pcg_normal_pool;
174 static struct pool pcg_large_pool;
175 static struct pool cache_pool;
176 static struct pool cache_cpu_pool;
177 
178 /* List of all caches. */
179 TAILQ_HEAD(,pool_cache) pool_cache_head =
180     TAILQ_HEAD_INITIALIZER(pool_cache_head);
181 
182 int pool_cache_disable;		/* global disable for caching */
183 static pcg_t pcg_dummy;		/* zero sized: always empty, yet always full */
184 
185 static bool	pool_cache_put_slow(pool_cache_cpu_t *, int,
186 				    void *);
187 static bool	pool_cache_get_slow(pool_cache_cpu_t *, int,
188 				    void **, paddr_t *, int);
189 static void	pool_cache_cpu_init1(struct cpu_info *, pool_cache_t);
190 static void	pool_cache_invalidate_groups(pool_cache_t, pcg_t *);
191 static void	pool_cache_xcall(pool_cache_t);
192 
193 static int	pool_catchup(struct pool *);
194 static void	pool_prime_page(struct pool *, void *,
195 		    struct pool_item_header *);
196 static void	pool_update_curpage(struct pool *);
197 
198 static int	pool_grow(struct pool *, int);
199 static void	*pool_allocator_alloc(struct pool *, int);
200 static void	pool_allocator_free(struct pool *, void *);
201 
202 static void pool_print_pagelist(struct pool *, struct pool_pagelist *,
203 	void (*)(const char *, ...));
204 static void pool_print1(struct pool *, const char *,
205 	void (*)(const char *, ...));
206 
207 static int pool_chk_page(struct pool *, const char *,
208 			 struct pool_item_header *);
209 
210 /*
211  * Pool log entry. An array of these is allocated in pool_init().
212  */
213 struct pool_log {
214 	const char	*pl_file;
215 	long		pl_line;
216 	int		pl_action;
217 #define	PRLOG_GET	1
218 #define	PRLOG_PUT	2
219 	void		*pl_addr;
220 };
221 
222 #ifdef POOL_DIAGNOSTIC
223 /* Number of entries in pool log buffers */
224 #ifndef POOL_LOGSIZE
225 #define	POOL_LOGSIZE	10
226 #endif
227 
228 int pool_logsize = POOL_LOGSIZE;
229 
230 static inline void
231 pr_log(struct pool *pp, void *v, int action, const char *file, long line)
232 {
233 	int n = pp->pr_curlogentry;
234 	struct pool_log *pl;
235 
236 	if ((pp->pr_roflags & PR_LOGGING) == 0)
237 		return;
238 
239 	/*
240 	 * Fill in the current entry. Wrap around and overwrite
241 	 * the oldest entry if necessary.
242 	 */
243 	pl = &pp->pr_log[n];
244 	pl->pl_file = file;
245 	pl->pl_line = line;
246 	pl->pl_action = action;
247 	pl->pl_addr = v;
248 	if (++n >= pp->pr_logsize)
249 		n = 0;
250 	pp->pr_curlogentry = n;
251 }
252 
253 static void
254 pr_printlog(struct pool *pp, struct pool_item *pi,
255     void (*pr)(const char *, ...))
256 {
257 	int i = pp->pr_logsize;
258 	int n = pp->pr_curlogentry;
259 
260 	if ((pp->pr_roflags & PR_LOGGING) == 0)
261 		return;
262 
263 	/*
264 	 * Print all entries in this pool's log.
265 	 */
266 	while (i-- > 0) {
267 		struct pool_log *pl = &pp->pr_log[n];
268 		if (pl->pl_action != 0) {
269 			if (pi == NULL || pi == pl->pl_addr) {
270 				(*pr)("\tlog entry %d:\n", i);
271 				(*pr)("\t\taction = %s, addr = %p\n",
272 				    pl->pl_action == PRLOG_GET ? "get" : "put",
273 				    pl->pl_addr);
274 				(*pr)("\t\tfile: %s at line %lu\n",
275 				    pl->pl_file, pl->pl_line);
276 			}
277 		}
278 		if (++n >= pp->pr_logsize)
279 			n = 0;
280 	}
281 }
282 
283 static inline void
284 pr_enter(struct pool *pp, const char *file, long line)
285 {
286 
287 	if (__predict_false(pp->pr_entered_file != NULL)) {
288 		printf("pool %s: reentrancy at file %s line %ld\n",
289 		    pp->pr_wchan, file, line);
290 		printf("         previous entry at file %s line %ld\n",
291 		    pp->pr_entered_file, pp->pr_entered_line);
292 		panic("pr_enter");
293 	}
294 
295 	pp->pr_entered_file = file;
296 	pp->pr_entered_line = line;
297 }
298 
299 static inline void
300 pr_leave(struct pool *pp)
301 {
302 
303 	if (__predict_false(pp->pr_entered_file == NULL)) {
304 		printf("pool %s not entered?\n", pp->pr_wchan);
305 		panic("pr_leave");
306 	}
307 
308 	pp->pr_entered_file = NULL;
309 	pp->pr_entered_line = 0;
310 }
311 
312 static inline void
313 pr_enter_check(struct pool *pp, void (*pr)(const char *, ...))
314 {
315 
316 	if (pp->pr_entered_file != NULL)
317 		(*pr)("\n\tcurrently entered from file %s line %ld\n",
318 		    pp->pr_entered_file, pp->pr_entered_line);
319 }
320 #else
321 #define	pr_log(pp, v, action, file, line)
322 #define	pr_printlog(pp, pi, pr)
323 #define	pr_enter(pp, file, line)
324 #define	pr_leave(pp)
325 #define	pr_enter_check(pp, pr)
326 #endif /* POOL_DIAGNOSTIC */
327 
328 static inline unsigned int
329 pr_item_notouch_index(const struct pool *pp, const struct pool_item_header *ph,
330     const void *v)
331 {
332 	const char *cp = v;
333 	unsigned int idx;
334 
335 	KASSERT(pp->pr_roflags & PR_NOTOUCH);
336 	idx = (cp - (char *)ph->ph_page - ph->ph_off) / pp->pr_size;
337 	KASSERT(idx < pp->pr_itemsperpage);
338 	return idx;
339 }
340 
341 static inline void
342 pr_item_notouch_put(const struct pool *pp, struct pool_item_header *ph,
343     void *obj)
344 {
345 	unsigned int idx = pr_item_notouch_index(pp, ph, obj);
346 	pool_item_bitmap_t *bitmap = ph->ph_bitmap + (idx / BITMAP_SIZE);
347 	pool_item_bitmap_t mask = 1 << (idx & BITMAP_MASK);
348 
349 	KASSERT((*bitmap & mask) == 0);
350 	*bitmap |= mask;
351 }
352 
353 static inline void *
354 pr_item_notouch_get(const struct pool *pp, struct pool_item_header *ph)
355 {
356 	pool_item_bitmap_t *bitmap = ph->ph_bitmap;
357 	unsigned int idx;
358 	int i;
359 
360 	for (i = 0; ; i++) {
361 		int bit;
362 
363 		KASSERT((i * BITMAP_SIZE) < pp->pr_itemsperpage);
364 		bit = ffs32(bitmap[i]);
365 		if (bit) {
366 			pool_item_bitmap_t mask;
367 
368 			bit--;
369 			idx = (i * BITMAP_SIZE) + bit;
370 			mask = 1 << bit;
371 			KASSERT((bitmap[i] & mask) != 0);
372 			bitmap[i] &= ~mask;
373 			break;
374 		}
375 	}
376 	KASSERT(idx < pp->pr_itemsperpage);
377 	return (char *)ph->ph_page + ph->ph_off + idx * pp->pr_size;
378 }
379 
380 static inline void
381 pr_item_notouch_init(const struct pool *pp, struct pool_item_header *ph)
382 {
383 	pool_item_bitmap_t *bitmap = ph->ph_bitmap;
384 	const int n = howmany(pp->pr_itemsperpage, BITMAP_SIZE);
385 	int i;
386 
387 	for (i = 0; i < n; i++) {
388 		bitmap[i] = (pool_item_bitmap_t)-1;
389 	}
390 }
391 
392 static inline int
393 phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
394 {
395 
396 	/*
397 	 * we consider pool_item_header with smaller ph_page bigger.
398 	 * (this unnatural ordering is for the benefit of pr_find_pagehead.)
399 	 */
400 
401 	if (a->ph_page < b->ph_page)
402 		return (1);
403 	else if (a->ph_page > b->ph_page)
404 		return (-1);
405 	else
406 		return (0);
407 }
408 
409 SPLAY_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare);
410 SPLAY_GENERATE(phtree, pool_item_header, ph_node, phtree_compare);
411 
412 static inline struct pool_item_header *
413 pr_find_pagehead_noalign(struct pool *pp, void *v)
414 {
415 	struct pool_item_header *ph, tmp;
416 
417 	tmp.ph_page = (void *)(uintptr_t)v;
418 	ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
419 	if (ph == NULL) {
420 		ph = SPLAY_ROOT(&pp->pr_phtree);
421 		if (ph != NULL && phtree_compare(&tmp, ph) >= 0) {
422 			ph = SPLAY_NEXT(phtree, &pp->pr_phtree, ph);
423 		}
424 		KASSERT(ph == NULL || phtree_compare(&tmp, ph) < 0);
425 	}
426 
427 	return ph;
428 }
429 
430 /*
431  * Return the pool page header based on item address.
432  */
433 static inline struct pool_item_header *
434 pr_find_pagehead(struct pool *pp, void *v)
435 {
436 	struct pool_item_header *ph, tmp;
437 
438 	if ((pp->pr_roflags & PR_NOALIGN) != 0) {
439 		ph = pr_find_pagehead_noalign(pp, v);
440 	} else {
441 		void *page =
442 		    (void *)((uintptr_t)v & pp->pr_alloc->pa_pagemask);
443 
444 		if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
445 			ph = (struct pool_item_header *)((char *)page + pp->pr_phoffset);
446 		} else {
447 			tmp.ph_page = page;
448 			ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
449 		}
450 	}
451 
452 	KASSERT(ph == NULL || ((pp->pr_roflags & PR_PHINPAGE) != 0) ||
453 	    ((char *)ph->ph_page <= (char *)v &&
454 	    (char *)v < (char *)ph->ph_page + pp->pr_alloc->pa_pagesz));
455 	return ph;
456 }
457 
458 static void
459 pr_pagelist_free(struct pool *pp, struct pool_pagelist *pq)
460 {
461 	struct pool_item_header *ph;
462 
463 	while ((ph = LIST_FIRST(pq)) != NULL) {
464 		LIST_REMOVE(ph, ph_pagelist);
465 		pool_allocator_free(pp, ph->ph_page);
466 		if ((pp->pr_roflags & PR_PHINPAGE) == 0)
467 			pool_put(pp->pr_phpool, ph);
468 	}
469 }
470 
471 /*
472  * Remove a page from the pool.
473  */
474 static inline void
475 pr_rmpage(struct pool *pp, struct pool_item_header *ph,
476      struct pool_pagelist *pq)
477 {
478 
479 	KASSERT(mutex_owned(&pp->pr_lock));
480 
481 	/*
482 	 * If the page was idle, decrement the idle page count.
483 	 */
484 	if (ph->ph_nmissing == 0) {
485 #ifdef DIAGNOSTIC
486 		if (pp->pr_nidle == 0)
487 			panic("pr_rmpage: nidle inconsistent");
488 		if (pp->pr_nitems < pp->pr_itemsperpage)
489 			panic("pr_rmpage: nitems inconsistent");
490 #endif
491 		pp->pr_nidle--;
492 	}
493 
494 	pp->pr_nitems -= pp->pr_itemsperpage;
495 
496 	/*
497 	 * Unlink the page from the pool and queue it for release.
498 	 */
499 	LIST_REMOVE(ph, ph_pagelist);
500 	if ((pp->pr_roflags & PR_PHINPAGE) == 0)
501 		SPLAY_REMOVE(phtree, &pp->pr_phtree, ph);
502 	LIST_INSERT_HEAD(pq, ph, ph_pagelist);
503 
504 	pp->pr_npages--;
505 	pp->pr_npagefree++;
506 
507 	pool_update_curpage(pp);
508 }
509 
510 static bool
511 pa_starved_p(struct pool_allocator *pa)
512 {
513 
514 	if (pa->pa_backingmap != NULL) {
515 		return vm_map_starved_p(pa->pa_backingmap);
516 	}
517 	return false;
518 }
519 
520 static int
521 pool_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
522 {
523 	struct pool *pp = obj;
524 	struct pool_allocator *pa = pp->pr_alloc;
525 
526 	KASSERT(&pp->pr_reclaimerentry == ce);
527 	pool_reclaim(pp);
528 	if (!pa_starved_p(pa)) {
529 		return CALLBACK_CHAIN_ABORT;
530 	}
531 	return CALLBACK_CHAIN_CONTINUE;
532 }
533 
534 static void
535 pool_reclaim_register(struct pool *pp)
536 {
537 	struct vm_map *map = pp->pr_alloc->pa_backingmap;
538 	int s;
539 
540 	if (map == NULL) {
541 		return;
542 	}
543 
544 	s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
545 	callback_register(&vm_map_to_kernel(map)->vmk_reclaim_callback,
546 	    &pp->pr_reclaimerentry, pp, pool_reclaim_callback);
547 	splx(s);
548 }
549 
550 static void
551 pool_reclaim_unregister(struct pool *pp)
552 {
553 	struct vm_map *map = pp->pr_alloc->pa_backingmap;
554 	int s;
555 
556 	if (map == NULL) {
557 		return;
558 	}
559 
560 	s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
561 	callback_unregister(&vm_map_to_kernel(map)->vmk_reclaim_callback,
562 	    &pp->pr_reclaimerentry);
563 	splx(s);
564 }
565 
566 static void
567 pa_reclaim_register(struct pool_allocator *pa)
568 {
569 	struct vm_map *map = *pa->pa_backingmapptr;
570 	struct pool *pp;
571 
572 	KASSERT(pa->pa_backingmap == NULL);
573 	if (map == NULL) {
574 		SLIST_INSERT_HEAD(&pa_deferinitq, pa, pa_q);
575 		return;
576 	}
577 	pa->pa_backingmap = map;
578 	TAILQ_FOREACH(pp, &pa->pa_list, pr_alloc_list) {
579 		pool_reclaim_register(pp);
580 	}
581 }
582 
583 /*
584  * Initialize all the pools listed in the "pools" link set.
585  */
586 void
587 pool_subsystem_init(void)
588 {
589 	struct pool_allocator *pa;
590 	__link_set_decl(pools, struct link_pool_init);
591 	struct link_pool_init * const *pi;
592 
593 	mutex_init(&pool_head_lock, MUTEX_DEFAULT, IPL_NONE);
594 	cv_init(&pool_busy, "poolbusy");
595 
596 	__link_set_foreach(pi, pools)
597 		pool_init((*pi)->pp, (*pi)->size, (*pi)->align,
598 		    (*pi)->align_offset, (*pi)->flags, (*pi)->wchan,
599 		    (*pi)->palloc, (*pi)->ipl);
600 
601 	while ((pa = SLIST_FIRST(&pa_deferinitq)) != NULL) {
602 		KASSERT(pa->pa_backingmapptr != NULL);
603 		KASSERT(*pa->pa_backingmapptr != NULL);
604 		SLIST_REMOVE_HEAD(&pa_deferinitq, pa_q);
605 		pa_reclaim_register(pa);
606 	}
607 
608 	pool_init(&cache_pool, sizeof(struct pool_cache), coherency_unit,
609 	    0, 0, "pcache", &pool_allocator_nointr, IPL_NONE);
610 
611 	pool_init(&cache_cpu_pool, sizeof(pool_cache_cpu_t), coherency_unit,
612 	    0, 0, "pcachecpu", &pool_allocator_nointr, IPL_NONE);
613 }
614 
615 /*
616  * Initialize the given pool resource structure.
617  *
618  * We export this routine to allow other kernel parts to declare
619  * static pools that must be initialized before malloc() is available.
620  */
621 void
622 pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
623     const char *wchan, struct pool_allocator *palloc, int ipl)
624 {
625 	struct pool *pp1;
626 	size_t trysize, phsize;
627 	int off, slack;
628 
629 #ifdef DEBUG
630 	/*
631 	 * Check that the pool hasn't already been initialised and
632 	 * added to the list of all pools.
633 	 */
634 	TAILQ_FOREACH(pp1, &pool_head, pr_poollist) {
635 		if (pp == pp1)
636 			panic("pool_init: pool %s already initialised",
637 			    wchan);
638 	}
639 #endif
640 
641 #ifdef POOL_DIAGNOSTIC
642 	/*
643 	 * Always log if POOL_DIAGNOSTIC is defined.
644 	 */
645 	if (pool_logsize != 0)
646 		flags |= PR_LOGGING;
647 #endif
648 
649 	if (palloc == NULL)
650 		palloc = &pool_allocator_kmem;
651 #ifdef POOL_SUBPAGE
652 	if (size > palloc->pa_pagesz) {
653 		if (palloc == &pool_allocator_kmem)
654 			palloc = &pool_allocator_kmem_fullpage;
655 		else if (palloc == &pool_allocator_nointr)
656 			palloc = &pool_allocator_nointr_fullpage;
657 	}
658 #endif /* POOL_SUBPAGE */
659 	if ((palloc->pa_flags & PA_INITIALIZED) == 0) {
660 		if (palloc->pa_pagesz == 0)
661 			palloc->pa_pagesz = PAGE_SIZE;
662 
663 		TAILQ_INIT(&palloc->pa_list);
664 
665 		mutex_init(&palloc->pa_lock, MUTEX_DEFAULT, IPL_VM);
666 		palloc->pa_pagemask = ~(palloc->pa_pagesz - 1);
667 		palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1;
668 
669 		if (palloc->pa_backingmapptr != NULL) {
670 			pa_reclaim_register(palloc);
671 		}
672 		palloc->pa_flags |= PA_INITIALIZED;
673 	}
674 
675 	if (align == 0)
676 		align = ALIGN(1);
677 
678 	if ((flags & PR_NOTOUCH) == 0 && size < sizeof(struct pool_item))
679 		size = sizeof(struct pool_item);
680 
681 	size = roundup(size, align);
682 #ifdef DIAGNOSTIC
683 	if (size > palloc->pa_pagesz)
684 		panic("pool_init: pool item size (%zu) too large", size);
685 #endif
686 
687 	/*
688 	 * Initialize the pool structure.
689 	 */
690 	LIST_INIT(&pp->pr_emptypages);
691 	LIST_INIT(&pp->pr_fullpages);
692 	LIST_INIT(&pp->pr_partpages);
693 	pp->pr_cache = NULL;
694 	pp->pr_curpage = NULL;
695 	pp->pr_npages = 0;
696 	pp->pr_minitems = 0;
697 	pp->pr_minpages = 0;
698 	pp->pr_maxpages = UINT_MAX;
699 	pp->pr_roflags = flags;
700 	pp->pr_flags = 0;
701 	pp->pr_size = size;
702 	pp->pr_align = align;
703 	pp->pr_wchan = wchan;
704 	pp->pr_alloc = palloc;
705 	pp->pr_nitems = 0;
706 	pp->pr_nout = 0;
707 	pp->pr_hardlimit = UINT_MAX;
708 	pp->pr_hardlimit_warning = NULL;
709 	pp->pr_hardlimit_ratecap.tv_sec = 0;
710 	pp->pr_hardlimit_ratecap.tv_usec = 0;
711 	pp->pr_hardlimit_warning_last.tv_sec = 0;
712 	pp->pr_hardlimit_warning_last.tv_usec = 0;
713 	pp->pr_drain_hook = NULL;
714 	pp->pr_drain_hook_arg = NULL;
715 	pp->pr_freecheck = NULL;
716 
717 	/*
718 	 * Decide whether to put the page header off page to avoid
719 	 * wasting too large a part of the page or too big item.
720 	 * Off-page page headers go on a hash table, so we can match
721 	 * a returned item with its header based on the page address.
722 	 * We use 1/16 of the page size and about 8 times of the item
723 	 * size as the threshold (XXX: tune)
724 	 *
725 	 * However, we'll put the header into the page if we can put
726 	 * it without wasting any items.
727 	 *
728 	 * Silently enforce `0 <= ioff < align'.
729 	 */
730 	pp->pr_itemoffset = ioff %= align;
731 	/* See the comment below about reserved bytes. */
732 	trysize = palloc->pa_pagesz - ((align - ioff) % align);
733 	phsize = ALIGN(sizeof(struct pool_item_header));
734 	if ((pp->pr_roflags & (PR_NOTOUCH | PR_NOALIGN)) == 0 &&
735 	    (pp->pr_size < MIN(palloc->pa_pagesz / 16, phsize << 3) ||
736 	    trysize / pp->pr_size == (trysize - phsize) / pp->pr_size)) {
737 		/* Use the end of the page for the page header */
738 		pp->pr_roflags |= PR_PHINPAGE;
739 		pp->pr_phoffset = off = palloc->pa_pagesz - phsize;
740 	} else {
741 		/* The page header will be taken from our page header pool */
742 		pp->pr_phoffset = 0;
743 		off = palloc->pa_pagesz;
744 		SPLAY_INIT(&pp->pr_phtree);
745 	}
746 
747 	/*
748 	 * Alignment is to take place at `ioff' within the item. This means
749 	 * we must reserve up to `align - 1' bytes on the page to allow
750 	 * appropriate positioning of each item.
751 	 */
752 	pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
753 	KASSERT(pp->pr_itemsperpage != 0);
754 	if ((pp->pr_roflags & PR_NOTOUCH)) {
755 		int idx;
756 
757 		for (idx = 0; pp->pr_itemsperpage > PHPOOL_FREELIST_NELEM(idx);
758 		    idx++) {
759 			/* nothing */
760 		}
761 		if (idx >= PHPOOL_MAX) {
762 			/*
763 			 * if you see this panic, consider to tweak
764 			 * PHPOOL_MAX and PHPOOL_FREELIST_NELEM.
765 			 */
766 			panic("%s: too large itemsperpage(%d) for PR_NOTOUCH",
767 			    pp->pr_wchan, pp->pr_itemsperpage);
768 		}
769 		pp->pr_phpool = &phpool[idx];
770 	} else if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
771 		pp->pr_phpool = &phpool[0];
772 	}
773 #if defined(DIAGNOSTIC)
774 	else {
775 		pp->pr_phpool = NULL;
776 	}
777 #endif
778 
779 	/*
780 	 * Use the slack between the chunks and the page header
781 	 * for "cache coloring".
782 	 */
783 	slack = off - pp->pr_itemsperpage * pp->pr_size;
784 	pp->pr_maxcolor = (slack / align) * align;
785 	pp->pr_curcolor = 0;
786 
787 	pp->pr_nget = 0;
788 	pp->pr_nfail = 0;
789 	pp->pr_nput = 0;
790 	pp->pr_npagealloc = 0;
791 	pp->pr_npagefree = 0;
792 	pp->pr_hiwat = 0;
793 	pp->pr_nidle = 0;
794 	pp->pr_refcnt = 0;
795 
796 #ifdef POOL_DIAGNOSTIC
797 	if (flags & PR_LOGGING) {
798 		if (kmem_map == NULL ||
799 		    (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
800 		     M_TEMP, M_NOWAIT)) == NULL)
801 			pp->pr_roflags &= ~PR_LOGGING;
802 		pp->pr_curlogentry = 0;
803 		pp->pr_logsize = pool_logsize;
804 	}
805 #endif
806 
807 	pp->pr_entered_file = NULL;
808 	pp->pr_entered_line = 0;
809 
810 	mutex_init(&pp->pr_lock, MUTEX_DEFAULT, ipl);
811 	cv_init(&pp->pr_cv, wchan);
812 	pp->pr_ipl = ipl;
813 
814 	/*
815 	 * Initialize private page header pool and cache magazine pool if we
816 	 * haven't done so yet.
817 	 * XXX LOCKING.
818 	 */
819 	if (phpool[0].pr_size == 0) {
820 		int idx;
821 		for (idx = 0; idx < PHPOOL_MAX; idx++) {
822 			static char phpool_names[PHPOOL_MAX][6+1+6+1];
823 			int nelem;
824 			size_t sz;
825 
826 			nelem = PHPOOL_FREELIST_NELEM(idx);
827 			snprintf(phpool_names[idx], sizeof(phpool_names[idx]),
828 			    "phpool-%d", nelem);
829 			sz = sizeof(struct pool_item_header);
830 			if (nelem) {
831 				sz = offsetof(struct pool_item_header,
832 				    ph_bitmap[howmany(nelem, BITMAP_SIZE)]);
833 			}
834 			pool_init(&phpool[idx], sz, 0, 0, 0,
835 			    phpool_names[idx], &pool_allocator_meta, IPL_VM);
836 		}
837 #ifdef POOL_SUBPAGE
838 		pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0,
839 		    PR_RECURSIVE, "psppool", &pool_allocator_meta, IPL_VM);
840 #endif
841 
842 		size = sizeof(pcg_t) +
843 		    (PCG_NOBJECTS_NORMAL - 1) * sizeof(pcgpair_t);
844 		pool_init(&pcg_normal_pool, size, coherency_unit, 0, 0,
845 		    "pcgnormal", &pool_allocator_meta, IPL_VM);
846 
847 		size = sizeof(pcg_t) +
848 		    (PCG_NOBJECTS_LARGE - 1) * sizeof(pcgpair_t);
849 		pool_init(&pcg_large_pool, size, coherency_unit, 0, 0,
850 		    "pcglarge", &pool_allocator_meta, IPL_VM);
851 	}
852 
853 	/* Insert into the list of all pools. */
854 	if (__predict_true(!cold))
855 		mutex_enter(&pool_head_lock);
856 	TAILQ_FOREACH(pp1, &pool_head, pr_poollist) {
857 		if (strcmp(pp1->pr_wchan, pp->pr_wchan) > 0)
858 			break;
859 	}
860 	if (pp1 == NULL)
861 		TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
862 	else
863 		TAILQ_INSERT_BEFORE(pp1, pp, pr_poollist);
864 	if (__predict_true(!cold))
865 		mutex_exit(&pool_head_lock);
866 
867 		/* Insert this into the list of pools using this allocator. */
868 	if (__predict_true(!cold))
869 		mutex_enter(&palloc->pa_lock);
870 	TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
871 	if (__predict_true(!cold))
872 		mutex_exit(&palloc->pa_lock);
873 
874 	pool_reclaim_register(pp);
875 }
876 
877 /*
878  * De-commision a pool resource.
879  */
880 void
881 pool_destroy(struct pool *pp)
882 {
883 	struct pool_pagelist pq;
884 	struct pool_item_header *ph;
885 
886 	/* Remove from global pool list */
887 	mutex_enter(&pool_head_lock);
888 	while (pp->pr_refcnt != 0)
889 		cv_wait(&pool_busy, &pool_head_lock);
890 	TAILQ_REMOVE(&pool_head, pp, pr_poollist);
891 	if (drainpp == pp)
892 		drainpp = NULL;
893 	mutex_exit(&pool_head_lock);
894 
895 	/* Remove this pool from its allocator's list of pools. */
896 	pool_reclaim_unregister(pp);
897 	mutex_enter(&pp->pr_alloc->pa_lock);
898 	TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list);
899 	mutex_exit(&pp->pr_alloc->pa_lock);
900 
901 	mutex_enter(&pp->pr_lock);
902 
903 	KASSERT(pp->pr_cache == NULL);
904 
905 #ifdef DIAGNOSTIC
906 	if (pp->pr_nout != 0) {
907 		pr_printlog(pp, NULL, printf);
908 		panic("pool_destroy: pool busy: still out: %u",
909 		    pp->pr_nout);
910 	}
911 #endif
912 
913 	KASSERT(LIST_EMPTY(&pp->pr_fullpages));
914 	KASSERT(LIST_EMPTY(&pp->pr_partpages));
915 
916 	/* Remove all pages */
917 	LIST_INIT(&pq);
918 	while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
919 		pr_rmpage(pp, ph, &pq);
920 
921 	mutex_exit(&pp->pr_lock);
922 
923 	pr_pagelist_free(pp, &pq);
924 
925 #ifdef POOL_DIAGNOSTIC
926 	if ((pp->pr_roflags & PR_LOGGING) != 0)
927 		free(pp->pr_log, M_TEMP);
928 #endif
929 
930 	cv_destroy(&pp->pr_cv);
931 	mutex_destroy(&pp->pr_lock);
932 }
933 
934 void
935 pool_set_drain_hook(struct pool *pp, void (*fn)(void *, int), void *arg)
936 {
937 
938 	/* XXX no locking -- must be used just after pool_init() */
939 #ifdef DIAGNOSTIC
940 	if (pp->pr_drain_hook != NULL)
941 		panic("pool_set_drain_hook(%s): already set", pp->pr_wchan);
942 #endif
943 	pp->pr_drain_hook = fn;
944 	pp->pr_drain_hook_arg = arg;
945 }
946 
947 static struct pool_item_header *
948 pool_alloc_item_header(struct pool *pp, void *storage, int flags)
949 {
950 	struct pool_item_header *ph;
951 
952 	if ((pp->pr_roflags & PR_PHINPAGE) != 0)
953 		ph = (struct pool_item_header *) ((char *)storage + pp->pr_phoffset);
954 	else
955 		ph = pool_get(pp->pr_phpool, flags);
956 
957 	return (ph);
958 }
959 
960 /*
961  * Grab an item from the pool.
962  */
963 void *
964 #ifdef POOL_DIAGNOSTIC
965 _pool_get(struct pool *pp, int flags, const char *file, long line)
966 #else
967 pool_get(struct pool *pp, int flags)
968 #endif
969 {
970 	struct pool_item *pi;
971 	struct pool_item_header *ph;
972 	void *v;
973 
974 #ifdef DIAGNOSTIC
975 	if (__predict_false(pp->pr_itemsperpage == 0))
976 		panic("pool_get: pool %p: pr_itemsperpage is zero, "
977 		    "pool not initialized?", pp);
978 	if (__predict_false(curlwp == NULL && doing_shutdown == 0 &&
979 			    (flags & PR_WAITOK) != 0))
980 		panic("pool_get: %s: must have NOWAIT", pp->pr_wchan);
981 
982 #endif /* DIAGNOSTIC */
983 #ifdef LOCKDEBUG
984 	if (flags & PR_WAITOK) {
985 		ASSERT_SLEEPABLE();
986 	}
987 #endif
988 
989 	mutex_enter(&pp->pr_lock);
990 	pr_enter(pp, file, line);
991 
992  startover:
993 	/*
994 	 * Check to see if we've reached the hard limit.  If we have,
995 	 * and we can wait, then wait until an item has been returned to
996 	 * the pool.
997 	 */
998 #ifdef DIAGNOSTIC
999 	if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
1000 		pr_leave(pp);
1001 		mutex_exit(&pp->pr_lock);
1002 		panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
1003 	}
1004 #endif
1005 	if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
1006 		if (pp->pr_drain_hook != NULL) {
1007 			/*
1008 			 * Since the drain hook is going to free things
1009 			 * back to the pool, unlock, call the hook, re-lock,
1010 			 * and check the hardlimit condition again.
1011 			 */
1012 			pr_leave(pp);
1013 			mutex_exit(&pp->pr_lock);
1014 			(*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
1015 			mutex_enter(&pp->pr_lock);
1016 			pr_enter(pp, file, line);
1017 			if (pp->pr_nout < pp->pr_hardlimit)
1018 				goto startover;
1019 		}
1020 
1021 		if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
1022 			/*
1023 			 * XXX: A warning isn't logged in this case.  Should
1024 			 * it be?
1025 			 */
1026 			pp->pr_flags |= PR_WANTED;
1027 			pr_leave(pp);
1028 			cv_wait(&pp->pr_cv, &pp->pr_lock);
1029 			pr_enter(pp, file, line);
1030 			goto startover;
1031 		}
1032 
1033 		/*
1034 		 * Log a message that the hard limit has been hit.
1035 		 */
1036 		if (pp->pr_hardlimit_warning != NULL &&
1037 		    ratecheck(&pp->pr_hardlimit_warning_last,
1038 			      &pp->pr_hardlimit_ratecap))
1039 			log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1040 
1041 		pp->pr_nfail++;
1042 
1043 		pr_leave(pp);
1044 		mutex_exit(&pp->pr_lock);
1045 		return (NULL);
1046 	}
1047 
1048 	/*
1049 	 * The convention we use is that if `curpage' is not NULL, then
1050 	 * it points at a non-empty bucket. In particular, `curpage'
1051 	 * never points at a page header which has PR_PHINPAGE set and
1052 	 * has no items in its bucket.
1053 	 */
1054 	if ((ph = pp->pr_curpage) == NULL) {
1055 		int error;
1056 
1057 #ifdef DIAGNOSTIC
1058 		if (pp->pr_nitems != 0) {
1059 			mutex_exit(&pp->pr_lock);
1060 			printf("pool_get: %s: curpage NULL, nitems %u\n",
1061 			    pp->pr_wchan, pp->pr_nitems);
1062 			panic("pool_get: nitems inconsistent");
1063 		}
1064 #endif
1065 
1066 		/*
1067 		 * Call the back-end page allocator for more memory.
1068 		 * Release the pool lock, as the back-end page allocator
1069 		 * may block.
1070 		 */
1071 		pr_leave(pp);
1072 		error = pool_grow(pp, flags);
1073 		pr_enter(pp, file, line);
1074 		if (error != 0) {
1075 			/*
1076 			 * We were unable to allocate a page or item
1077 			 * header, but we released the lock during
1078 			 * allocation, so perhaps items were freed
1079 			 * back to the pool.  Check for this case.
1080 			 */
1081 			if (pp->pr_curpage != NULL)
1082 				goto startover;
1083 
1084 			pp->pr_nfail++;
1085 			pr_leave(pp);
1086 			mutex_exit(&pp->pr_lock);
1087 			return (NULL);
1088 		}
1089 
1090 		/* Start the allocation process over. */
1091 		goto startover;
1092 	}
1093 	if (pp->pr_roflags & PR_NOTOUCH) {
1094 #ifdef DIAGNOSTIC
1095 		if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) {
1096 			pr_leave(pp);
1097 			mutex_exit(&pp->pr_lock);
1098 			panic("pool_get: %s: page empty", pp->pr_wchan);
1099 		}
1100 #endif
1101 		v = pr_item_notouch_get(pp, ph);
1102 #ifdef POOL_DIAGNOSTIC
1103 		pr_log(pp, v, PRLOG_GET, file, line);
1104 #endif
1105 	} else {
1106 		v = pi = LIST_FIRST(&ph->ph_itemlist);
1107 		if (__predict_false(v == NULL)) {
1108 			pr_leave(pp);
1109 			mutex_exit(&pp->pr_lock);
1110 			panic("pool_get: %s: page empty", pp->pr_wchan);
1111 		}
1112 #ifdef DIAGNOSTIC
1113 		if (__predict_false(pp->pr_nitems == 0)) {
1114 			pr_leave(pp);
1115 			mutex_exit(&pp->pr_lock);
1116 			printf("pool_get: %s: items on itemlist, nitems %u\n",
1117 			    pp->pr_wchan, pp->pr_nitems);
1118 			panic("pool_get: nitems inconsistent");
1119 		}
1120 #endif
1121 
1122 #ifdef POOL_DIAGNOSTIC
1123 		pr_log(pp, v, PRLOG_GET, file, line);
1124 #endif
1125 
1126 #ifdef DIAGNOSTIC
1127 		if (__predict_false(pi->pi_magic != PI_MAGIC)) {
1128 			pr_printlog(pp, pi, printf);
1129 			panic("pool_get(%s): free list modified: "
1130 			    "magic=%x; page %p; item addr %p\n",
1131 			    pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
1132 		}
1133 #endif
1134 
1135 		/*
1136 		 * Remove from item list.
1137 		 */
1138 		LIST_REMOVE(pi, pi_list);
1139 	}
1140 	pp->pr_nitems--;
1141 	pp->pr_nout++;
1142 	if (ph->ph_nmissing == 0) {
1143 #ifdef DIAGNOSTIC
1144 		if (__predict_false(pp->pr_nidle == 0))
1145 			panic("pool_get: nidle inconsistent");
1146 #endif
1147 		pp->pr_nidle--;
1148 
1149 		/*
1150 		 * This page was previously empty.  Move it to the list of
1151 		 * partially-full pages.  This page is already curpage.
1152 		 */
1153 		LIST_REMOVE(ph, ph_pagelist);
1154 		LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1155 	}
1156 	ph->ph_nmissing++;
1157 	if (ph->ph_nmissing == pp->pr_itemsperpage) {
1158 #ifdef DIAGNOSTIC
1159 		if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 &&
1160 		    !LIST_EMPTY(&ph->ph_itemlist))) {
1161 			pr_leave(pp);
1162 			mutex_exit(&pp->pr_lock);
1163 			panic("pool_get: %s: nmissing inconsistent",
1164 			    pp->pr_wchan);
1165 		}
1166 #endif
1167 		/*
1168 		 * This page is now full.  Move it to the full list
1169 		 * and select a new current page.
1170 		 */
1171 		LIST_REMOVE(ph, ph_pagelist);
1172 		LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
1173 		pool_update_curpage(pp);
1174 	}
1175 
1176 	pp->pr_nget++;
1177 	pr_leave(pp);
1178 
1179 	/*
1180 	 * If we have a low water mark and we are now below that low
1181 	 * water mark, add more items to the pool.
1182 	 */
1183 	if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1184 		/*
1185 		 * XXX: Should we log a warning?  Should we set up a timeout
1186 		 * to try again in a second or so?  The latter could break
1187 		 * a caller's assumptions about interrupt protection, etc.
1188 		 */
1189 	}
1190 
1191 	mutex_exit(&pp->pr_lock);
1192 	KASSERT((((vaddr_t)v + pp->pr_itemoffset) & (pp->pr_align - 1)) == 0);
1193 	FREECHECK_OUT(&pp->pr_freecheck, v);
1194 	return (v);
1195 }
1196 
1197 /*
1198  * Internal version of pool_put().  Pool is already locked/entered.
1199  */
1200 static void
1201 pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq)
1202 {
1203 	struct pool_item *pi = v;
1204 	struct pool_item_header *ph;
1205 
1206 	KASSERT(mutex_owned(&pp->pr_lock));
1207 	FREECHECK_IN(&pp->pr_freecheck, v);
1208 	LOCKDEBUG_MEM_CHECK(v, pp->pr_size);
1209 
1210 #ifdef DIAGNOSTIC
1211 	if (__predict_false(pp->pr_nout == 0)) {
1212 		printf("pool %s: putting with none out\n",
1213 		    pp->pr_wchan);
1214 		panic("pool_put");
1215 	}
1216 #endif
1217 
1218 	if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) {
1219 		pr_printlog(pp, NULL, printf);
1220 		panic("pool_put: %s: page header missing", pp->pr_wchan);
1221 	}
1222 
1223 	/*
1224 	 * Return to item list.
1225 	 */
1226 	if (pp->pr_roflags & PR_NOTOUCH) {
1227 		pr_item_notouch_put(pp, ph, v);
1228 	} else {
1229 #ifdef DIAGNOSTIC
1230 		pi->pi_magic = PI_MAGIC;
1231 #endif
1232 #ifdef DEBUG
1233 		{
1234 			int i, *ip = v;
1235 
1236 			for (i = 0; i < pp->pr_size / sizeof(int); i++) {
1237 				*ip++ = PI_MAGIC;
1238 			}
1239 		}
1240 #endif
1241 
1242 		LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1243 	}
1244 	KDASSERT(ph->ph_nmissing != 0);
1245 	ph->ph_nmissing--;
1246 	pp->pr_nput++;
1247 	pp->pr_nitems++;
1248 	pp->pr_nout--;
1249 
1250 	/* Cancel "pool empty" condition if it exists */
1251 	if (pp->pr_curpage == NULL)
1252 		pp->pr_curpage = ph;
1253 
1254 	if (pp->pr_flags & PR_WANTED) {
1255 		pp->pr_flags &= ~PR_WANTED;
1256 		cv_broadcast(&pp->pr_cv);
1257 	}
1258 
1259 	/*
1260 	 * If this page is now empty, do one of two things:
1261 	 *
1262 	 *	(1) If we have more pages than the page high water mark,
1263 	 *	    free the page back to the system.  ONLY CONSIDER
1264 	 *	    FREEING BACK A PAGE IF WE HAVE MORE THAN OUR MINIMUM PAGE
1265 	 *	    CLAIM.
1266 	 *
1267 	 *	(2) Otherwise, move the page to the empty page list.
1268 	 *
1269 	 * Either way, select a new current page (so we use a partially-full
1270 	 * page if one is available).
1271 	 */
1272 	if (ph->ph_nmissing == 0) {
1273 		pp->pr_nidle++;
1274 		if (pp->pr_npages > pp->pr_minpages &&
1275 		    pp->pr_npages > pp->pr_maxpages) {
1276 			pr_rmpage(pp, ph, pq);
1277 		} else {
1278 			LIST_REMOVE(ph, ph_pagelist);
1279 			LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1280 
1281 			/*
1282 			 * Update the timestamp on the page.  A page must
1283 			 * be idle for some period of time before it can
1284 			 * be reclaimed by the pagedaemon.  This minimizes
1285 			 * ping-pong'ing for memory.
1286 			 *
1287 			 * note for 64-bit time_t: truncating to 32-bit is not
1288 			 * a problem for our usage.
1289 			 */
1290 			ph->ph_time = time_uptime;
1291 		}
1292 		pool_update_curpage(pp);
1293 	}
1294 
1295 	/*
1296 	 * If the page was previously completely full, move it to the
1297 	 * partially-full list and make it the current page.  The next
1298 	 * allocation will get the item from this page, instead of
1299 	 * further fragmenting the pool.
1300 	 */
1301 	else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
1302 		LIST_REMOVE(ph, ph_pagelist);
1303 		LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1304 		pp->pr_curpage = ph;
1305 	}
1306 }
1307 
1308 /*
1309  * Return resource to the pool.
1310  */
1311 #ifdef POOL_DIAGNOSTIC
1312 void
1313 _pool_put(struct pool *pp, void *v, const char *file, long line)
1314 {
1315 	struct pool_pagelist pq;
1316 
1317 	LIST_INIT(&pq);
1318 
1319 	mutex_enter(&pp->pr_lock);
1320 	pr_enter(pp, file, line);
1321 
1322 	pr_log(pp, v, PRLOG_PUT, file, line);
1323 
1324 	pool_do_put(pp, v, &pq);
1325 
1326 	pr_leave(pp);
1327 	mutex_exit(&pp->pr_lock);
1328 
1329 	pr_pagelist_free(pp, &pq);
1330 }
1331 #undef pool_put
1332 #endif /* POOL_DIAGNOSTIC */
1333 
1334 void
1335 pool_put(struct pool *pp, void *v)
1336 {
1337 	struct pool_pagelist pq;
1338 
1339 	LIST_INIT(&pq);
1340 
1341 	mutex_enter(&pp->pr_lock);
1342 	pool_do_put(pp, v, &pq);
1343 	mutex_exit(&pp->pr_lock);
1344 
1345 	pr_pagelist_free(pp, &pq);
1346 }
1347 
1348 #ifdef POOL_DIAGNOSTIC
1349 #define		pool_put(h, v)	_pool_put((h), (v), __FILE__, __LINE__)
1350 #endif
1351 
1352 /*
1353  * pool_grow: grow a pool by a page.
1354  *
1355  * => called with pool locked.
1356  * => unlock and relock the pool.
1357  * => return with pool locked.
1358  */
1359 
1360 static int
1361 pool_grow(struct pool *pp, int flags)
1362 {
1363 	struct pool_item_header *ph = NULL;
1364 	char *cp;
1365 
1366 	mutex_exit(&pp->pr_lock);
1367 	cp = pool_allocator_alloc(pp, flags);
1368 	if (__predict_true(cp != NULL)) {
1369 		ph = pool_alloc_item_header(pp, cp, flags);
1370 	}
1371 	if (__predict_false(cp == NULL || ph == NULL)) {
1372 		if (cp != NULL) {
1373 			pool_allocator_free(pp, cp);
1374 		}
1375 		mutex_enter(&pp->pr_lock);
1376 		return ENOMEM;
1377 	}
1378 
1379 	mutex_enter(&pp->pr_lock);
1380 	pool_prime_page(pp, cp, ph);
1381 	pp->pr_npagealloc++;
1382 	return 0;
1383 }
1384 
1385 /*
1386  * Add N items to the pool.
1387  */
1388 int
1389 pool_prime(struct pool *pp, int n)
1390 {
1391 	int newpages;
1392 	int error = 0;
1393 
1394 	mutex_enter(&pp->pr_lock);
1395 
1396 	newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1397 
1398 	while (newpages-- > 0) {
1399 		error = pool_grow(pp, PR_NOWAIT);
1400 		if (error) {
1401 			break;
1402 		}
1403 		pp->pr_minpages++;
1404 	}
1405 
1406 	if (pp->pr_minpages >= pp->pr_maxpages)
1407 		pp->pr_maxpages = pp->pr_minpages + 1;	/* XXX */
1408 
1409 	mutex_exit(&pp->pr_lock);
1410 	return error;
1411 }
1412 
1413 /*
1414  * Add a page worth of items to the pool.
1415  *
1416  * Note, we must be called with the pool descriptor LOCKED.
1417  */
1418 static void
1419 pool_prime_page(struct pool *pp, void *storage, struct pool_item_header *ph)
1420 {
1421 	struct pool_item *pi;
1422 	void *cp = storage;
1423 	const unsigned int align = pp->pr_align;
1424 	const unsigned int ioff = pp->pr_itemoffset;
1425 	int n;
1426 
1427 	KASSERT(mutex_owned(&pp->pr_lock));
1428 
1429 #ifdef DIAGNOSTIC
1430 	if ((pp->pr_roflags & PR_NOALIGN) == 0 &&
1431 	    ((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0)
1432 		panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1433 #endif
1434 
1435 	/*
1436 	 * Insert page header.
1437 	 */
1438 	LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1439 	LIST_INIT(&ph->ph_itemlist);
1440 	ph->ph_page = storage;
1441 	ph->ph_nmissing = 0;
1442 	ph->ph_time = time_uptime;
1443 	if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1444 		SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
1445 
1446 	pp->pr_nidle++;
1447 
1448 	/*
1449 	 * Color this page.
1450 	 */
1451 	ph->ph_off = pp->pr_curcolor;
1452 	cp = (char *)cp + ph->ph_off;
1453 	if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
1454 		pp->pr_curcolor = 0;
1455 
1456 	/*
1457 	 * Adjust storage to apply aligment to `pr_itemoffset' in each item.
1458 	 */
1459 	if (ioff != 0)
1460 		cp = (char *)cp + align - ioff;
1461 
1462 	KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1463 
1464 	/*
1465 	 * Insert remaining chunks on the bucket list.
1466 	 */
1467 	n = pp->pr_itemsperpage;
1468 	pp->pr_nitems += n;
1469 
1470 	if (pp->pr_roflags & PR_NOTOUCH) {
1471 		pr_item_notouch_init(pp, ph);
1472 	} else {
1473 		while (n--) {
1474 			pi = (struct pool_item *)cp;
1475 
1476 			KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
1477 
1478 			/* Insert on page list */
1479 			LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1480 #ifdef DIAGNOSTIC
1481 			pi->pi_magic = PI_MAGIC;
1482 #endif
1483 			cp = (char *)cp + pp->pr_size;
1484 
1485 			KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1486 		}
1487 	}
1488 
1489 	/*
1490 	 * If the pool was depleted, point at the new page.
1491 	 */
1492 	if (pp->pr_curpage == NULL)
1493 		pp->pr_curpage = ph;
1494 
1495 	if (++pp->pr_npages > pp->pr_hiwat)
1496 		pp->pr_hiwat = pp->pr_npages;
1497 }
1498 
1499 /*
1500  * Used by pool_get() when nitems drops below the low water mark.  This
1501  * is used to catch up pr_nitems with the low water mark.
1502  *
1503  * Note 1, we never wait for memory here, we let the caller decide what to do.
1504  *
1505  * Note 2, we must be called with the pool already locked, and we return
1506  * with it locked.
1507  */
1508 static int
1509 pool_catchup(struct pool *pp)
1510 {
1511 	int error = 0;
1512 
1513 	while (POOL_NEEDS_CATCHUP(pp)) {
1514 		error = pool_grow(pp, PR_NOWAIT);
1515 		if (error) {
1516 			break;
1517 		}
1518 	}
1519 	return error;
1520 }
1521 
1522 static void
1523 pool_update_curpage(struct pool *pp)
1524 {
1525 
1526 	pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
1527 	if (pp->pr_curpage == NULL) {
1528 		pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
1529 	}
1530 }
1531 
1532 void
1533 pool_setlowat(struct pool *pp, int n)
1534 {
1535 
1536 	mutex_enter(&pp->pr_lock);
1537 
1538 	pp->pr_minitems = n;
1539 	pp->pr_minpages = (n == 0)
1540 		? 0
1541 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1542 
1543 	/* Make sure we're caught up with the newly-set low water mark. */
1544 	if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1545 		/*
1546 		 * XXX: Should we log a warning?  Should we set up a timeout
1547 		 * to try again in a second or so?  The latter could break
1548 		 * a caller's assumptions about interrupt protection, etc.
1549 		 */
1550 	}
1551 
1552 	mutex_exit(&pp->pr_lock);
1553 }
1554 
1555 void
1556 pool_sethiwat(struct pool *pp, int n)
1557 {
1558 
1559 	mutex_enter(&pp->pr_lock);
1560 
1561 	pp->pr_maxpages = (n == 0)
1562 		? 0
1563 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1564 
1565 	mutex_exit(&pp->pr_lock);
1566 }
1567 
1568 void
1569 pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1570 {
1571 
1572 	mutex_enter(&pp->pr_lock);
1573 
1574 	pp->pr_hardlimit = n;
1575 	pp->pr_hardlimit_warning = warnmess;
1576 	pp->pr_hardlimit_ratecap.tv_sec = ratecap;
1577 	pp->pr_hardlimit_warning_last.tv_sec = 0;
1578 	pp->pr_hardlimit_warning_last.tv_usec = 0;
1579 
1580 	/*
1581 	 * In-line version of pool_sethiwat(), because we don't want to
1582 	 * release the lock.
1583 	 */
1584 	pp->pr_maxpages = (n == 0)
1585 		? 0
1586 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1587 
1588 	mutex_exit(&pp->pr_lock);
1589 }
1590 
1591 /*
1592  * Release all complete pages that have not been used recently.
1593  */
1594 int
1595 #ifdef POOL_DIAGNOSTIC
1596 _pool_reclaim(struct pool *pp, const char *file, long line)
1597 #else
1598 pool_reclaim(struct pool *pp)
1599 #endif
1600 {
1601 	struct pool_item_header *ph, *phnext;
1602 	struct pool_pagelist pq;
1603 	uint32_t curtime;
1604 	bool klock;
1605 	int rv;
1606 
1607 	if (pp->pr_drain_hook != NULL) {
1608 		/*
1609 		 * The drain hook must be called with the pool unlocked.
1610 		 */
1611 		(*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT);
1612 	}
1613 
1614 	/*
1615 	 * XXXSMP Because we do not want to cause non-MPSAFE code
1616 	 * to block.
1617 	 */
1618 	if (pp->pr_ipl == IPL_SOFTNET || pp->pr_ipl == IPL_SOFTCLOCK ||
1619 	    pp->pr_ipl == IPL_SOFTSERIAL) {
1620 		KERNEL_LOCK(1, NULL);
1621 		klock = true;
1622 	} else
1623 		klock = false;
1624 
1625 	/* Reclaim items from the pool's cache (if any). */
1626 	if (pp->pr_cache != NULL)
1627 		pool_cache_invalidate(pp->pr_cache);
1628 
1629 	if (mutex_tryenter(&pp->pr_lock) == 0) {
1630 		if (klock) {
1631 			KERNEL_UNLOCK_ONE(NULL);
1632 		}
1633 		return (0);
1634 	}
1635 	pr_enter(pp, file, line);
1636 
1637 	LIST_INIT(&pq);
1638 
1639 	curtime = time_uptime;
1640 
1641 	for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
1642 		phnext = LIST_NEXT(ph, ph_pagelist);
1643 
1644 		/* Check our minimum page claim */
1645 		if (pp->pr_npages <= pp->pr_minpages)
1646 			break;
1647 
1648 		KASSERT(ph->ph_nmissing == 0);
1649 		if (curtime - ph->ph_time < pool_inactive_time
1650 		    && !pa_starved_p(pp->pr_alloc))
1651 			continue;
1652 
1653 		/*
1654 		 * If freeing this page would put us below
1655 		 * the low water mark, stop now.
1656 		 */
1657 		if ((pp->pr_nitems - pp->pr_itemsperpage) <
1658 		    pp->pr_minitems)
1659 			break;
1660 
1661 		pr_rmpage(pp, ph, &pq);
1662 	}
1663 
1664 	pr_leave(pp);
1665 	mutex_exit(&pp->pr_lock);
1666 
1667 	if (LIST_EMPTY(&pq))
1668 		rv = 0;
1669 	else {
1670 		pr_pagelist_free(pp, &pq);
1671 		rv = 1;
1672 	}
1673 
1674 	if (klock) {
1675 		KERNEL_UNLOCK_ONE(NULL);
1676 	}
1677 
1678 	return (rv);
1679 }
1680 
1681 /*
1682  * Drain pools, one at a time.  This is a two stage process;
1683  * drain_start kicks off a cross call to drain CPU-level caches
1684  * if the pool has an associated pool_cache.  drain_end waits
1685  * for those cross calls to finish, and then drains the cache
1686  * (if any) and pool.
1687  *
1688  * Note, must never be called from interrupt context.
1689  */
1690 void
1691 pool_drain_start(struct pool **ppp, uint64_t *wp)
1692 {
1693 	struct pool *pp;
1694 
1695 	KASSERT(!TAILQ_EMPTY(&pool_head));
1696 
1697 	pp = NULL;
1698 
1699 	/* Find next pool to drain, and add a reference. */
1700 	mutex_enter(&pool_head_lock);
1701 	do {
1702 		if (drainpp == NULL) {
1703 			drainpp = TAILQ_FIRST(&pool_head);
1704 		}
1705 		if (drainpp != NULL) {
1706 			pp = drainpp;
1707 			drainpp = TAILQ_NEXT(pp, pr_poollist);
1708 		}
1709 		/*
1710 		 * Skip completely idle pools.  We depend on at least
1711 		 * one pool in the system being active.
1712 		 */
1713 	} while (pp == NULL || pp->pr_npages == 0);
1714 	pp->pr_refcnt++;
1715 	mutex_exit(&pool_head_lock);
1716 
1717 	/* If there is a pool_cache, drain CPU level caches. */
1718 	*ppp = pp;
1719 	if (pp->pr_cache != NULL) {
1720 		*wp = xc_broadcast(0, (xcfunc_t)pool_cache_xcall,
1721 		    pp->pr_cache, NULL);
1722 	}
1723 }
1724 
1725 void
1726 pool_drain_end(struct pool *pp, uint64_t where)
1727 {
1728 
1729 	if (pp == NULL)
1730 		return;
1731 
1732 	KASSERT(pp->pr_refcnt > 0);
1733 
1734 	/* Wait for remote draining to complete. */
1735 	if (pp->pr_cache != NULL)
1736 		xc_wait(where);
1737 
1738 	/* Drain the cache (if any) and pool.. */
1739 	pool_reclaim(pp);
1740 
1741 	/* Finally, unlock the pool. */
1742 	mutex_enter(&pool_head_lock);
1743 	pp->pr_refcnt--;
1744 	cv_broadcast(&pool_busy);
1745 	mutex_exit(&pool_head_lock);
1746 }
1747 
1748 /*
1749  * Diagnostic helpers.
1750  */
1751 void
1752 pool_print(struct pool *pp, const char *modif)
1753 {
1754 
1755 	pool_print1(pp, modif, printf);
1756 }
1757 
1758 void
1759 pool_printall(const char *modif, void (*pr)(const char *, ...))
1760 {
1761 	struct pool *pp;
1762 
1763 	TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
1764 		pool_printit(pp, modif, pr);
1765 	}
1766 }
1767 
1768 void
1769 pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1770 {
1771 
1772 	if (pp == NULL) {
1773 		(*pr)("Must specify a pool to print.\n");
1774 		return;
1775 	}
1776 
1777 	pool_print1(pp, modif, pr);
1778 }
1779 
1780 static void
1781 pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
1782     void (*pr)(const char *, ...))
1783 {
1784 	struct pool_item_header *ph;
1785 #ifdef DIAGNOSTIC
1786 	struct pool_item *pi;
1787 #endif
1788 
1789 	LIST_FOREACH(ph, pl, ph_pagelist) {
1790 		(*pr)("\t\tpage %p, nmissing %d, time %" PRIu32 "\n",
1791 		    ph->ph_page, ph->ph_nmissing, ph->ph_time);
1792 #ifdef DIAGNOSTIC
1793 		if (!(pp->pr_roflags & PR_NOTOUCH)) {
1794 			LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1795 				if (pi->pi_magic != PI_MAGIC) {
1796 					(*pr)("\t\t\titem %p, magic 0x%x\n",
1797 					    pi, pi->pi_magic);
1798 				}
1799 			}
1800 		}
1801 #endif
1802 	}
1803 }
1804 
1805 static void
1806 pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1807 {
1808 	struct pool_item_header *ph;
1809 	pool_cache_t pc;
1810 	pcg_t *pcg;
1811 	pool_cache_cpu_t *cc;
1812 	uint64_t cpuhit, cpumiss;
1813 	int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1814 	char c;
1815 
1816 	while ((c = *modif++) != '\0') {
1817 		if (c == 'l')
1818 			print_log = 1;
1819 		if (c == 'p')
1820 			print_pagelist = 1;
1821 		if (c == 'c')
1822 			print_cache = 1;
1823 	}
1824 
1825 	if ((pc = pp->pr_cache) != NULL) {
1826 		(*pr)("POOL CACHE");
1827 	} else {
1828 		(*pr)("POOL");
1829 	}
1830 
1831 	(*pr)(" %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1832 	    pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1833 	    pp->pr_roflags);
1834 	(*pr)("\talloc %p\n", pp->pr_alloc);
1835 	(*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1836 	    pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1837 	(*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1838 	    pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1839 
1840 	(*pr)("\tnget %lu, nfail %lu, nput %lu\n",
1841 	    pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1842 	(*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1843 	    pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1844 
1845 	if (print_pagelist == 0)
1846 		goto skip_pagelist;
1847 
1848 	if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1849 		(*pr)("\n\tempty page list:\n");
1850 	pool_print_pagelist(pp, &pp->pr_emptypages, pr);
1851 	if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
1852 		(*pr)("\n\tfull page list:\n");
1853 	pool_print_pagelist(pp, &pp->pr_fullpages, pr);
1854 	if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
1855 		(*pr)("\n\tpartial-page list:\n");
1856 	pool_print_pagelist(pp, &pp->pr_partpages, pr);
1857 
1858 	if (pp->pr_curpage == NULL)
1859 		(*pr)("\tno current page\n");
1860 	else
1861 		(*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1862 
1863  skip_pagelist:
1864 	if (print_log == 0)
1865 		goto skip_log;
1866 
1867 	(*pr)("\n");
1868 	if ((pp->pr_roflags & PR_LOGGING) == 0)
1869 		(*pr)("\tno log\n");
1870 	else {
1871 		pr_printlog(pp, NULL, pr);
1872 	}
1873 
1874  skip_log:
1875 
1876 #define PR_GROUPLIST(pcg)						\
1877 	(*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail);		\
1878 	for (i = 0; i < pcg->pcg_size; i++) {				\
1879 		if (pcg->pcg_objects[i].pcgo_pa !=			\
1880 		    POOL_PADDR_INVALID) {				\
1881 			(*pr)("\t\t\t%p, 0x%llx\n",			\
1882 			    pcg->pcg_objects[i].pcgo_va,		\
1883 			    (unsigned long long)			\
1884 			    pcg->pcg_objects[i].pcgo_pa);		\
1885 		} else {						\
1886 			(*pr)("\t\t\t%p\n",				\
1887 			    pcg->pcg_objects[i].pcgo_va);		\
1888 		}							\
1889 	}
1890 
1891 	if (pc != NULL) {
1892 		cpuhit = 0;
1893 		cpumiss = 0;
1894 		for (i = 0; i < MAXCPUS; i++) {
1895 			if ((cc = pc->pc_cpus[i]) == NULL)
1896 				continue;
1897 			cpuhit += cc->cc_hits;
1898 			cpumiss += cc->cc_misses;
1899 		}
1900 		(*pr)("\tcpu layer hits %llu misses %llu\n", cpuhit, cpumiss);
1901 		(*pr)("\tcache layer hits %llu misses %llu\n",
1902 		    pc->pc_hits, pc->pc_misses);
1903 		(*pr)("\tcache layer entry uncontended %llu contended %llu\n",
1904 		    pc->pc_hits + pc->pc_misses - pc->pc_contended,
1905 		    pc->pc_contended);
1906 		(*pr)("\tcache layer empty groups %u full groups %u\n",
1907 		    pc->pc_nempty, pc->pc_nfull);
1908 		if (print_cache) {
1909 			(*pr)("\tfull cache groups:\n");
1910 			for (pcg = pc->pc_fullgroups; pcg != NULL;
1911 			    pcg = pcg->pcg_next) {
1912 				PR_GROUPLIST(pcg);
1913 			}
1914 			(*pr)("\tempty cache groups:\n");
1915 			for (pcg = pc->pc_emptygroups; pcg != NULL;
1916 			    pcg = pcg->pcg_next) {
1917 				PR_GROUPLIST(pcg);
1918 			}
1919 		}
1920 	}
1921 #undef PR_GROUPLIST
1922 
1923 	pr_enter_check(pp, pr);
1924 }
1925 
1926 static int
1927 pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
1928 {
1929 	struct pool_item *pi;
1930 	void *page;
1931 	int n;
1932 
1933 	if ((pp->pr_roflags & PR_NOALIGN) == 0) {
1934 		page = (void *)((uintptr_t)ph & pp->pr_alloc->pa_pagemask);
1935 		if (page != ph->ph_page &&
1936 		    (pp->pr_roflags & PR_PHINPAGE) != 0) {
1937 			if (label != NULL)
1938 				printf("%s: ", label);
1939 			printf("pool(%p:%s): page inconsistency: page %p;"
1940 			       " at page head addr %p (p %p)\n", pp,
1941 				pp->pr_wchan, ph->ph_page,
1942 				ph, page);
1943 			return 1;
1944 		}
1945 	}
1946 
1947 	if ((pp->pr_roflags & PR_NOTOUCH) != 0)
1948 		return 0;
1949 
1950 	for (pi = LIST_FIRST(&ph->ph_itemlist), n = 0;
1951 	     pi != NULL;
1952 	     pi = LIST_NEXT(pi,pi_list), n++) {
1953 
1954 #ifdef DIAGNOSTIC
1955 		if (pi->pi_magic != PI_MAGIC) {
1956 			if (label != NULL)
1957 				printf("%s: ", label);
1958 			printf("pool(%s): free list modified: magic=%x;"
1959 			       " page %p; item ordinal %d; addr %p\n",
1960 				pp->pr_wchan, pi->pi_magic, ph->ph_page,
1961 				n, pi);
1962 			panic("pool");
1963 		}
1964 #endif
1965 		if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1966 			continue;
1967 		}
1968 		page = (void *)((uintptr_t)pi & pp->pr_alloc->pa_pagemask);
1969 		if (page == ph->ph_page)
1970 			continue;
1971 
1972 		if (label != NULL)
1973 			printf("%s: ", label);
1974 		printf("pool(%p:%s): page inconsistency: page %p;"
1975 		       " item ordinal %d; addr %p (p %p)\n", pp,
1976 			pp->pr_wchan, ph->ph_page,
1977 			n, pi, page);
1978 		return 1;
1979 	}
1980 	return 0;
1981 }
1982 
1983 
1984 int
1985 pool_chk(struct pool *pp, const char *label)
1986 {
1987 	struct pool_item_header *ph;
1988 	int r = 0;
1989 
1990 	mutex_enter(&pp->pr_lock);
1991 	LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
1992 		r = pool_chk_page(pp, label, ph);
1993 		if (r) {
1994 			goto out;
1995 		}
1996 	}
1997 	LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
1998 		r = pool_chk_page(pp, label, ph);
1999 		if (r) {
2000 			goto out;
2001 		}
2002 	}
2003 	LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
2004 		r = pool_chk_page(pp, label, ph);
2005 		if (r) {
2006 			goto out;
2007 		}
2008 	}
2009 
2010 out:
2011 	mutex_exit(&pp->pr_lock);
2012 	return (r);
2013 }
2014 
2015 /*
2016  * pool_cache_init:
2017  *
2018  *	Initialize a pool cache.
2019  */
2020 pool_cache_t
2021 pool_cache_init(size_t size, u_int align, u_int align_offset, u_int flags,
2022     const char *wchan, struct pool_allocator *palloc, int ipl,
2023     int (*ctor)(void *, void *, int), void (*dtor)(void *, void *), void *arg)
2024 {
2025 	pool_cache_t pc;
2026 
2027 	pc = pool_get(&cache_pool, PR_WAITOK);
2028 	if (pc == NULL)
2029 		return NULL;
2030 
2031 	pool_cache_bootstrap(pc, size, align, align_offset, flags, wchan,
2032 	   palloc, ipl, ctor, dtor, arg);
2033 
2034 	return pc;
2035 }
2036 
2037 /*
2038  * pool_cache_bootstrap:
2039  *
2040  *	Kernel-private version of pool_cache_init().  The caller
2041  *	provides initial storage.
2042  */
2043 void
2044 pool_cache_bootstrap(pool_cache_t pc, size_t size, u_int align,
2045     u_int align_offset, u_int flags, const char *wchan,
2046     struct pool_allocator *palloc, int ipl,
2047     int (*ctor)(void *, void *, int), void (*dtor)(void *, void *),
2048     void *arg)
2049 {
2050 	CPU_INFO_ITERATOR cii;
2051 	pool_cache_t pc1;
2052 	struct cpu_info *ci;
2053 	struct pool *pp;
2054 
2055 	pp = &pc->pc_pool;
2056 	if (palloc == NULL && ipl == IPL_NONE)
2057 		palloc = &pool_allocator_nointr;
2058 	pool_init(pp, size, align, align_offset, flags, wchan, palloc, ipl);
2059 	mutex_init(&pc->pc_lock, MUTEX_DEFAULT, ipl);
2060 
2061 	if (ctor == NULL) {
2062 		ctor = (int (*)(void *, void *, int))nullop;
2063 	}
2064 	if (dtor == NULL) {
2065 		dtor = (void (*)(void *, void *))nullop;
2066 	}
2067 
2068 	pc->pc_emptygroups = NULL;
2069 	pc->pc_fullgroups = NULL;
2070 	pc->pc_partgroups = NULL;
2071 	pc->pc_ctor = ctor;
2072 	pc->pc_dtor = dtor;
2073 	pc->pc_arg  = arg;
2074 	pc->pc_hits  = 0;
2075 	pc->pc_misses = 0;
2076 	pc->pc_nempty = 0;
2077 	pc->pc_npart = 0;
2078 	pc->pc_nfull = 0;
2079 	pc->pc_contended = 0;
2080 	pc->pc_refcnt = 0;
2081 	pc->pc_freecheck = NULL;
2082 
2083 	if ((flags & PR_LARGECACHE) != 0) {
2084 		pc->pc_pcgsize = PCG_NOBJECTS_LARGE;
2085 		pc->pc_pcgpool = &pcg_large_pool;
2086 	} else {
2087 		pc->pc_pcgsize = PCG_NOBJECTS_NORMAL;
2088 		pc->pc_pcgpool = &pcg_normal_pool;
2089 	}
2090 
2091 	/* Allocate per-CPU caches. */
2092 	memset(pc->pc_cpus, 0, sizeof(pc->pc_cpus));
2093 	pc->pc_ncpu = 0;
2094 	if (ncpu < 2) {
2095 		/* XXX For sparc: boot CPU is not attached yet. */
2096 		pool_cache_cpu_init1(curcpu(), pc);
2097 	} else {
2098 		for (CPU_INFO_FOREACH(cii, ci)) {
2099 			pool_cache_cpu_init1(ci, pc);
2100 		}
2101 	}
2102 
2103 	/* Add to list of all pools. */
2104 	if (__predict_true(!cold))
2105 		mutex_enter(&pool_head_lock);
2106 	TAILQ_FOREACH(pc1, &pool_cache_head, pc_cachelist) {
2107 		if (strcmp(pc1->pc_pool.pr_wchan, pc->pc_pool.pr_wchan) > 0)
2108 			break;
2109 	}
2110 	if (pc1 == NULL)
2111 		TAILQ_INSERT_TAIL(&pool_cache_head, pc, pc_cachelist);
2112 	else
2113 		TAILQ_INSERT_BEFORE(pc1, pc, pc_cachelist);
2114 	if (__predict_true(!cold))
2115 		mutex_exit(&pool_head_lock);
2116 
2117 	membar_sync();
2118 	pp->pr_cache = pc;
2119 }
2120 
2121 /*
2122  * pool_cache_destroy:
2123  *
2124  *	Destroy a pool cache.
2125  */
2126 void
2127 pool_cache_destroy(pool_cache_t pc)
2128 {
2129 	struct pool *pp = &pc->pc_pool;
2130 	pool_cache_cpu_t *cc;
2131 	pcg_t *pcg;
2132 	int i;
2133 
2134 	/* Remove it from the global list. */
2135 	mutex_enter(&pool_head_lock);
2136 	while (pc->pc_refcnt != 0)
2137 		cv_wait(&pool_busy, &pool_head_lock);
2138 	TAILQ_REMOVE(&pool_cache_head, pc, pc_cachelist);
2139 	mutex_exit(&pool_head_lock);
2140 
2141 	/* First, invalidate the entire cache. */
2142 	pool_cache_invalidate(pc);
2143 
2144 	/* Disassociate it from the pool. */
2145 	mutex_enter(&pp->pr_lock);
2146 	pp->pr_cache = NULL;
2147 	mutex_exit(&pp->pr_lock);
2148 
2149 	/* Destroy per-CPU data */
2150 	for (i = 0; i < MAXCPUS; i++) {
2151 		if ((cc = pc->pc_cpus[i]) == NULL)
2152 			continue;
2153 		if ((pcg = cc->cc_current) != &pcg_dummy) {
2154 			pcg->pcg_next = NULL;
2155 			pool_cache_invalidate_groups(pc, pcg);
2156 		}
2157 		if ((pcg = cc->cc_previous) != &pcg_dummy) {
2158 			pcg->pcg_next = NULL;
2159 			pool_cache_invalidate_groups(pc, pcg);
2160 		}
2161 		if (cc != &pc->pc_cpu0)
2162 			pool_put(&cache_cpu_pool, cc);
2163 	}
2164 
2165 	/* Finally, destroy it. */
2166 	mutex_destroy(&pc->pc_lock);
2167 	pool_destroy(pp);
2168 	pool_put(&cache_pool, pc);
2169 }
2170 
2171 /*
2172  * pool_cache_cpu_init1:
2173  *
2174  *	Called for each pool_cache whenever a new CPU is attached.
2175  */
2176 static void
2177 pool_cache_cpu_init1(struct cpu_info *ci, pool_cache_t pc)
2178 {
2179 	pool_cache_cpu_t *cc;
2180 	int index;
2181 
2182 	index = ci->ci_index;
2183 
2184 	KASSERT(index < MAXCPUS);
2185 
2186 	if ((cc = pc->pc_cpus[index]) != NULL) {
2187 		KASSERT(cc->cc_cpuindex == index);
2188 		return;
2189 	}
2190 
2191 	/*
2192 	 * The first CPU is 'free'.  This needs to be the case for
2193 	 * bootstrap - we may not be able to allocate yet.
2194 	 */
2195 	if (pc->pc_ncpu == 0) {
2196 		cc = &pc->pc_cpu0;
2197 		pc->pc_ncpu = 1;
2198 	} else {
2199 		mutex_enter(&pc->pc_lock);
2200 		pc->pc_ncpu++;
2201 		mutex_exit(&pc->pc_lock);
2202 		cc = pool_get(&cache_cpu_pool, PR_WAITOK);
2203 	}
2204 
2205 	cc->cc_ipl = pc->pc_pool.pr_ipl;
2206 	cc->cc_iplcookie = makeiplcookie(cc->cc_ipl);
2207 	cc->cc_cache = pc;
2208 	cc->cc_cpuindex = index;
2209 	cc->cc_hits = 0;
2210 	cc->cc_misses = 0;
2211 	cc->cc_current = &pcg_dummy;
2212 	cc->cc_previous = &pcg_dummy;
2213 
2214 	pc->pc_cpus[index] = cc;
2215 }
2216 
2217 /*
2218  * pool_cache_cpu_init:
2219  *
2220  *	Called whenever a new CPU is attached.
2221  */
2222 void
2223 pool_cache_cpu_init(struct cpu_info *ci)
2224 {
2225 	pool_cache_t pc;
2226 
2227 	mutex_enter(&pool_head_lock);
2228 	TAILQ_FOREACH(pc, &pool_cache_head, pc_cachelist) {
2229 		pc->pc_refcnt++;
2230 		mutex_exit(&pool_head_lock);
2231 
2232 		pool_cache_cpu_init1(ci, pc);
2233 
2234 		mutex_enter(&pool_head_lock);
2235 		pc->pc_refcnt--;
2236 		cv_broadcast(&pool_busy);
2237 	}
2238 	mutex_exit(&pool_head_lock);
2239 }
2240 
2241 /*
2242  * pool_cache_reclaim:
2243  *
2244  *	Reclaim memory from a pool cache.
2245  */
2246 bool
2247 pool_cache_reclaim(pool_cache_t pc)
2248 {
2249 
2250 	return pool_reclaim(&pc->pc_pool);
2251 }
2252 
2253 static void
2254 pool_cache_destruct_object1(pool_cache_t pc, void *object)
2255 {
2256 
2257 	(*pc->pc_dtor)(pc->pc_arg, object);
2258 	pool_put(&pc->pc_pool, object);
2259 }
2260 
2261 /*
2262  * pool_cache_destruct_object:
2263  *
2264  *	Force destruction of an object and its release back into
2265  *	the pool.
2266  */
2267 void
2268 pool_cache_destruct_object(pool_cache_t pc, void *object)
2269 {
2270 
2271 	FREECHECK_IN(&pc->pc_freecheck, object);
2272 
2273 	pool_cache_destruct_object1(pc, object);
2274 }
2275 
2276 /*
2277  * pool_cache_invalidate_groups:
2278  *
2279  *	Invalidate a chain of groups and destruct all objects.
2280  */
2281 static void
2282 pool_cache_invalidate_groups(pool_cache_t pc, pcg_t *pcg)
2283 {
2284 	void *object;
2285 	pcg_t *next;
2286 	int i;
2287 
2288 	for (; pcg != NULL; pcg = next) {
2289 		next = pcg->pcg_next;
2290 
2291 		for (i = 0; i < pcg->pcg_avail; i++) {
2292 			object = pcg->pcg_objects[i].pcgo_va;
2293 			pool_cache_destruct_object1(pc, object);
2294 		}
2295 
2296 		if (pcg->pcg_size == PCG_NOBJECTS_LARGE) {
2297 			pool_put(&pcg_large_pool, pcg);
2298 		} else {
2299 			KASSERT(pcg->pcg_size == PCG_NOBJECTS_NORMAL);
2300 			pool_put(&pcg_normal_pool, pcg);
2301 		}
2302 	}
2303 }
2304 
2305 /*
2306  * pool_cache_invalidate:
2307  *
2308  *	Invalidate a pool cache (destruct and release all of the
2309  *	cached objects).  Does not reclaim objects from the pool.
2310  */
2311 void
2312 pool_cache_invalidate(pool_cache_t pc)
2313 {
2314 	pcg_t *full, *empty, *part;
2315 
2316 	mutex_enter(&pc->pc_lock);
2317 	full = pc->pc_fullgroups;
2318 	empty = pc->pc_emptygroups;
2319 	part = pc->pc_partgroups;
2320 	pc->pc_fullgroups = NULL;
2321 	pc->pc_emptygroups = NULL;
2322 	pc->pc_partgroups = NULL;
2323 	pc->pc_nfull = 0;
2324 	pc->pc_nempty = 0;
2325 	pc->pc_npart = 0;
2326 	mutex_exit(&pc->pc_lock);
2327 
2328 	pool_cache_invalidate_groups(pc, full);
2329 	pool_cache_invalidate_groups(pc, empty);
2330 	pool_cache_invalidate_groups(pc, part);
2331 }
2332 
2333 void
2334 pool_cache_set_drain_hook(pool_cache_t pc, void (*fn)(void *, int), void *arg)
2335 {
2336 
2337 	pool_set_drain_hook(&pc->pc_pool, fn, arg);
2338 }
2339 
2340 void
2341 pool_cache_setlowat(pool_cache_t pc, int n)
2342 {
2343 
2344 	pool_setlowat(&pc->pc_pool, n);
2345 }
2346 
2347 void
2348 pool_cache_sethiwat(pool_cache_t pc, int n)
2349 {
2350 
2351 	pool_sethiwat(&pc->pc_pool, n);
2352 }
2353 
2354 void
2355 pool_cache_sethardlimit(pool_cache_t pc, int n, const char *warnmess, int ratecap)
2356 {
2357 
2358 	pool_sethardlimit(&pc->pc_pool, n, warnmess, ratecap);
2359 }
2360 
2361 static bool __noinline
2362 pool_cache_get_slow(pool_cache_cpu_t *cc, int s, void **objectp,
2363 		    paddr_t *pap, int flags)
2364 {
2365 	pcg_t *pcg, *cur;
2366 	uint64_t ncsw;
2367 	pool_cache_t pc;
2368 	void *object;
2369 
2370 	pc = cc->cc_cache;
2371 	cc->cc_misses++;
2372 
2373 	/*
2374 	 * Nothing was available locally.  Try and grab a group
2375 	 * from the cache.
2376 	 */
2377 	if (__predict_false(!mutex_tryenter(&pc->pc_lock))) {
2378 		ncsw = curlwp->l_ncsw;
2379 		mutex_enter(&pc->pc_lock);
2380 		pc->pc_contended++;
2381 
2382 		/*
2383 		 * If we context switched while locking, then
2384 		 * our view of the per-CPU data is invalid:
2385 		 * retry.
2386 		 */
2387 		if (curlwp->l_ncsw != ncsw) {
2388 			mutex_exit(&pc->pc_lock);
2389 			return true;
2390 		}
2391 	}
2392 
2393 	if (__predict_true((pcg = pc->pc_fullgroups) != NULL)) {
2394 		/*
2395 		 * If there's a full group, release our empty
2396 		 * group back to the cache.  Install the full
2397 		 * group as cc_current and return.
2398 		 */
2399 		if (__predict_true((cur = cc->cc_current) != &pcg_dummy)) {
2400 			KASSERT(cur->pcg_avail == 0);
2401 			cur->pcg_next = pc->pc_emptygroups;
2402 			pc->pc_emptygroups = cur;
2403 			pc->pc_nempty++;
2404 		}
2405 		KASSERT(pcg->pcg_avail == pcg->pcg_size);
2406 		cc->cc_current = pcg;
2407 		pc->pc_fullgroups = pcg->pcg_next;
2408 		pc->pc_hits++;
2409 		pc->pc_nfull--;
2410 		mutex_exit(&pc->pc_lock);
2411 		return true;
2412 	}
2413 
2414 	/*
2415 	 * Nothing available locally or in cache.  Take the slow
2416 	 * path: fetch a new object from the pool and construct
2417 	 * it.
2418 	 */
2419 	pc->pc_misses++;
2420 	mutex_exit(&pc->pc_lock);
2421 	splx(s);
2422 
2423 	object = pool_get(&pc->pc_pool, flags);
2424 	*objectp = object;
2425 	if (__predict_false(object == NULL))
2426 		return false;
2427 
2428 	if (__predict_false((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0)) {
2429 		pool_put(&pc->pc_pool, object);
2430 		*objectp = NULL;
2431 		return false;
2432 	}
2433 
2434 	KASSERT((((vaddr_t)object + pc->pc_pool.pr_itemoffset) &
2435 	    (pc->pc_pool.pr_align - 1)) == 0);
2436 
2437 	if (pap != NULL) {
2438 #ifdef POOL_VTOPHYS
2439 		*pap = POOL_VTOPHYS(object);
2440 #else
2441 		*pap = POOL_PADDR_INVALID;
2442 #endif
2443 	}
2444 
2445 	FREECHECK_OUT(&pc->pc_freecheck, object);
2446 	return false;
2447 }
2448 
2449 /*
2450  * pool_cache_get{,_paddr}:
2451  *
2452  *	Get an object from a pool cache (optionally returning
2453  *	the physical address of the object).
2454  */
2455 void *
2456 pool_cache_get_paddr(pool_cache_t pc, int flags, paddr_t *pap)
2457 {
2458 	pool_cache_cpu_t *cc;
2459 	pcg_t *pcg;
2460 	void *object;
2461 	int s;
2462 
2463 #ifdef LOCKDEBUG
2464 	if (flags & PR_WAITOK) {
2465 		ASSERT_SLEEPABLE();
2466 	}
2467 #endif
2468 
2469 	/* Lock out interrupts and disable preemption. */
2470 	s = splvm();
2471 	while (/* CONSTCOND */ true) {
2472 		/* Try and allocate an object from the current group. */
2473 		cc = pc->pc_cpus[curcpu()->ci_index];
2474 		KASSERT(cc->cc_cache == pc);
2475 	 	pcg = cc->cc_current;
2476 		if (__predict_true(pcg->pcg_avail > 0)) {
2477 			object = pcg->pcg_objects[--pcg->pcg_avail].pcgo_va;
2478 			if (__predict_false(pap != NULL))
2479 				*pap = pcg->pcg_objects[pcg->pcg_avail].pcgo_pa;
2480 #if defined(DIAGNOSTIC)
2481 			pcg->pcg_objects[pcg->pcg_avail].pcgo_va = NULL;
2482 			KASSERT(pcg->pcg_avail < pcg->pcg_size);
2483 			KASSERT(object != NULL);
2484 #endif
2485 			cc->cc_hits++;
2486 			splx(s);
2487 			FREECHECK_OUT(&pc->pc_freecheck, object);
2488 			return object;
2489 		}
2490 
2491 		/*
2492 		 * That failed.  If the previous group isn't empty, swap
2493 		 * it with the current group and allocate from there.
2494 		 */
2495 		pcg = cc->cc_previous;
2496 		if (__predict_true(pcg->pcg_avail > 0)) {
2497 			cc->cc_previous = cc->cc_current;
2498 			cc->cc_current = pcg;
2499 			continue;
2500 		}
2501 
2502 		/*
2503 		 * Can't allocate from either group: try the slow path.
2504 		 * If get_slow() allocated an object for us, or if
2505 		 * no more objects are available, it will return false.
2506 		 * Otherwise, we need to retry.
2507 		 */
2508 		if (!pool_cache_get_slow(cc, s, &object, pap, flags))
2509 			break;
2510 	}
2511 
2512 	return object;
2513 }
2514 
2515 static bool __noinline
2516 pool_cache_put_slow(pool_cache_cpu_t *cc, int s, void *object)
2517 {
2518 	pcg_t *pcg, *cur;
2519 	uint64_t ncsw;
2520 	pool_cache_t pc;
2521 
2522 	pc = cc->cc_cache;
2523 	cc->cc_misses++;
2524 
2525 	/* Lock the cache. */
2526 	if (__predict_false(!mutex_tryenter(&pc->pc_lock))) {
2527 		ncsw = curlwp->l_ncsw;
2528 		mutex_enter(&pc->pc_lock);
2529 		pc->pc_contended++;
2530 
2531 		/*
2532 		 * If we context switched while locking, then our view of
2533 		 * the per-CPU data is invalid: retry.
2534 		 */
2535 		if (__predict_false(curlwp->l_ncsw != ncsw)) {
2536 			mutex_exit(&pc->pc_lock);
2537 			return true;
2538 		}
2539 	}
2540 
2541 	/* If there are no empty groups in the cache then allocate one. */
2542 	if (__predict_false((pcg = pc->pc_emptygroups) == NULL)) {
2543 		if (__predict_true(!pool_cache_disable)) {
2544 			pcg = pool_get(pc->pc_pcgpool, PR_NOWAIT);
2545 		}
2546 		if (__predict_true(pcg != NULL)) {
2547 			pcg->pcg_avail = 0;
2548 			pcg->pcg_size = pc->pc_pcgsize;
2549 		}
2550 	} else {
2551 		pc->pc_emptygroups = pcg->pcg_next;
2552 		pc->pc_nempty--;
2553 	}
2554 
2555 	/*
2556 	 * If there's a empty group, release our full group back
2557 	 * to the cache.  Install the empty group to the local CPU
2558 	 * and return.
2559 	 */
2560 	if (pcg != NULL) {
2561 		KASSERT(pcg->pcg_avail == 0);
2562 		if (__predict_false(cc->cc_previous == &pcg_dummy)) {
2563 			cc->cc_previous = pcg;
2564 		} else {
2565 			cur = cc->cc_current;
2566 			if (__predict_true(cur != &pcg_dummy)) {
2567 				KASSERT(cur->pcg_avail == cur->pcg_size);
2568 				cur->pcg_next = pc->pc_fullgroups;
2569 				pc->pc_fullgroups = cur;
2570 				pc->pc_nfull++;
2571 			}
2572 			cc->cc_current = pcg;
2573 		}
2574 		pc->pc_hits++;
2575 		mutex_exit(&pc->pc_lock);
2576 		return true;
2577 	}
2578 
2579 	/*
2580 	 * Nothing available locally or in cache, and we didn't
2581 	 * allocate an empty group.  Take the slow path and destroy
2582 	 * the object here and now.
2583 	 */
2584 	pc->pc_misses++;
2585 	mutex_exit(&pc->pc_lock);
2586 	splx(s);
2587 	pool_cache_destruct_object(pc, object);
2588 
2589 	return false;
2590 }
2591 
2592 /*
2593  * pool_cache_put{,_paddr}:
2594  *
2595  *	Put an object back to the pool cache (optionally caching the
2596  *	physical address of the object).
2597  */
2598 void
2599 pool_cache_put_paddr(pool_cache_t pc, void *object, paddr_t pa)
2600 {
2601 	pool_cache_cpu_t *cc;
2602 	pcg_t *pcg;
2603 	int s;
2604 
2605 	FREECHECK_IN(&pc->pc_freecheck, object);
2606 
2607 	/* Lock out interrupts and disable preemption. */
2608 	s = splvm();
2609 	while (/* CONSTCOND */ true) {
2610 		/* If the current group isn't full, release it there. */
2611 		cc = pc->pc_cpus[curcpu()->ci_index];
2612 		KASSERT(cc->cc_cache == pc);
2613 	 	pcg = cc->cc_current;
2614 		if (__predict_true(pcg->pcg_avail < pcg->pcg_size)) {
2615 			pcg->pcg_objects[pcg->pcg_avail].pcgo_va = object;
2616 			pcg->pcg_objects[pcg->pcg_avail].pcgo_pa = pa;
2617 			pcg->pcg_avail++;
2618 			cc->cc_hits++;
2619 			splx(s);
2620 			return;
2621 		}
2622 
2623 		/*
2624 		 * That failed.  If the previous group isn't full, swap
2625 		 * it with the current group and try again.
2626 		 */
2627 		pcg = cc->cc_previous;
2628 		if (__predict_true(pcg->pcg_avail < pcg->pcg_size)) {
2629 			cc->cc_previous = cc->cc_current;
2630 			cc->cc_current = pcg;
2631 			continue;
2632 		}
2633 
2634 		/*
2635 		 * Can't free to either group: try the slow path.
2636 		 * If put_slow() releases the object for us, it
2637 		 * will return false.  Otherwise we need to retry.
2638 		 */
2639 		if (!pool_cache_put_slow(cc, s, object))
2640 			break;
2641 	}
2642 }
2643 
2644 /*
2645  * pool_cache_xcall:
2646  *
2647  *	Transfer objects from the per-CPU cache to the global cache.
2648  *	Run within a cross-call thread.
2649  */
2650 static void
2651 pool_cache_xcall(pool_cache_t pc)
2652 {
2653 	pool_cache_cpu_t *cc;
2654 	pcg_t *prev, *cur, **list;
2655 	int s;
2656 
2657 	s = splvm();
2658 	mutex_enter(&pc->pc_lock);
2659 	cc = pc->pc_cpus[curcpu()->ci_index];
2660 	cur = cc->cc_current;
2661 	cc->cc_current = &pcg_dummy;
2662 	prev = cc->cc_previous;
2663 	cc->cc_previous = &pcg_dummy;
2664 	if (cur != &pcg_dummy) {
2665 		if (cur->pcg_avail == cur->pcg_size) {
2666 			list = &pc->pc_fullgroups;
2667 			pc->pc_nfull++;
2668 		} else if (cur->pcg_avail == 0) {
2669 			list = &pc->pc_emptygroups;
2670 			pc->pc_nempty++;
2671 		} else {
2672 			list = &pc->pc_partgroups;
2673 			pc->pc_npart++;
2674 		}
2675 		cur->pcg_next = *list;
2676 		*list = cur;
2677 	}
2678 	if (prev != &pcg_dummy) {
2679 		if (prev->pcg_avail == prev->pcg_size) {
2680 			list = &pc->pc_fullgroups;
2681 			pc->pc_nfull++;
2682 		} else if (prev->pcg_avail == 0) {
2683 			list = &pc->pc_emptygroups;
2684 			pc->pc_nempty++;
2685 		} else {
2686 			list = &pc->pc_partgroups;
2687 			pc->pc_npart++;
2688 		}
2689 		prev->pcg_next = *list;
2690 		*list = prev;
2691 	}
2692 	mutex_exit(&pc->pc_lock);
2693 	splx(s);
2694 }
2695 
2696 /*
2697  * Pool backend allocators.
2698  *
2699  * Each pool has a backend allocator that handles allocation, deallocation,
2700  * and any additional draining that might be needed.
2701  *
2702  * We provide two standard allocators:
2703  *
2704  *	pool_allocator_kmem - the default when no allocator is specified
2705  *
2706  *	pool_allocator_nointr - used for pools that will not be accessed
2707  *	in interrupt context.
2708  */
2709 void	*pool_page_alloc(struct pool *, int);
2710 void	pool_page_free(struct pool *, void *);
2711 
2712 #ifdef POOL_SUBPAGE
2713 struct pool_allocator pool_allocator_kmem_fullpage = {
2714 	pool_page_alloc, pool_page_free, 0,
2715 	.pa_backingmapptr = &kmem_map,
2716 };
2717 #else
2718 struct pool_allocator pool_allocator_kmem = {
2719 	pool_page_alloc, pool_page_free, 0,
2720 	.pa_backingmapptr = &kmem_map,
2721 };
2722 #endif
2723 
2724 void	*pool_page_alloc_nointr(struct pool *, int);
2725 void	pool_page_free_nointr(struct pool *, void *);
2726 
2727 #ifdef POOL_SUBPAGE
2728 struct pool_allocator pool_allocator_nointr_fullpage = {
2729 	pool_page_alloc_nointr, pool_page_free_nointr, 0,
2730 	.pa_backingmapptr = &kernel_map,
2731 };
2732 #else
2733 struct pool_allocator pool_allocator_nointr = {
2734 	pool_page_alloc_nointr, pool_page_free_nointr, 0,
2735 	.pa_backingmapptr = &kernel_map,
2736 };
2737 #endif
2738 
2739 #ifdef POOL_SUBPAGE
2740 void	*pool_subpage_alloc(struct pool *, int);
2741 void	pool_subpage_free(struct pool *, void *);
2742 
2743 struct pool_allocator pool_allocator_kmem = {
2744 	pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
2745 	.pa_backingmapptr = &kmem_map,
2746 };
2747 
2748 void	*pool_subpage_alloc_nointr(struct pool *, int);
2749 void	pool_subpage_free_nointr(struct pool *, void *);
2750 
2751 struct pool_allocator pool_allocator_nointr = {
2752 	pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
2753 	.pa_backingmapptr = &kmem_map,
2754 };
2755 #endif /* POOL_SUBPAGE */
2756 
2757 static void *
2758 pool_allocator_alloc(struct pool *pp, int flags)
2759 {
2760 	struct pool_allocator *pa = pp->pr_alloc;
2761 	void *res;
2762 
2763 	res = (*pa->pa_alloc)(pp, flags);
2764 	if (res == NULL && (flags & PR_WAITOK) == 0) {
2765 		/*
2766 		 * We only run the drain hook here if PR_NOWAIT.
2767 		 * In other cases, the hook will be run in
2768 		 * pool_reclaim().
2769 		 */
2770 		if (pp->pr_drain_hook != NULL) {
2771 			(*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
2772 			res = (*pa->pa_alloc)(pp, flags);
2773 		}
2774 	}
2775 	return res;
2776 }
2777 
2778 static void
2779 pool_allocator_free(struct pool *pp, void *v)
2780 {
2781 	struct pool_allocator *pa = pp->pr_alloc;
2782 
2783 	(*pa->pa_free)(pp, v);
2784 }
2785 
2786 void *
2787 pool_page_alloc(struct pool *pp, int flags)
2788 {
2789 	bool waitok = (flags & PR_WAITOK) ? true : false;
2790 
2791 	return ((void *) uvm_km_alloc_poolpage_cache(kmem_map, waitok));
2792 }
2793 
2794 void
2795 pool_page_free(struct pool *pp, void *v)
2796 {
2797 
2798 	uvm_km_free_poolpage_cache(kmem_map, (vaddr_t) v);
2799 }
2800 
2801 static void *
2802 pool_page_alloc_meta(struct pool *pp, int flags)
2803 {
2804 	bool waitok = (flags & PR_WAITOK) ? true : false;
2805 
2806 	return ((void *) uvm_km_alloc_poolpage(kmem_map, waitok));
2807 }
2808 
2809 static void
2810 pool_page_free_meta(struct pool *pp, void *v)
2811 {
2812 
2813 	uvm_km_free_poolpage(kmem_map, (vaddr_t) v);
2814 }
2815 
2816 #ifdef POOL_SUBPAGE
2817 /* Sub-page allocator, for machines with large hardware pages. */
2818 void *
2819 pool_subpage_alloc(struct pool *pp, int flags)
2820 {
2821 	return pool_get(&psppool, flags);
2822 }
2823 
2824 void
2825 pool_subpage_free(struct pool *pp, void *v)
2826 {
2827 	pool_put(&psppool, v);
2828 }
2829 
2830 /* We don't provide a real nointr allocator.  Maybe later. */
2831 void *
2832 pool_subpage_alloc_nointr(struct pool *pp, int flags)
2833 {
2834 
2835 	return (pool_subpage_alloc(pp, flags));
2836 }
2837 
2838 void
2839 pool_subpage_free_nointr(struct pool *pp, void *v)
2840 {
2841 
2842 	pool_subpage_free(pp, v);
2843 }
2844 #endif /* POOL_SUBPAGE */
2845 void *
2846 pool_page_alloc_nointr(struct pool *pp, int flags)
2847 {
2848 	bool waitok = (flags & PR_WAITOK) ? true : false;
2849 
2850 	return ((void *) uvm_km_alloc_poolpage_cache(kernel_map, waitok));
2851 }
2852 
2853 void
2854 pool_page_free_nointr(struct pool *pp, void *v)
2855 {
2856 
2857 	uvm_km_free_poolpage_cache(kernel_map, (vaddr_t) v);
2858 }
2859 
2860 #if defined(DDB)
2861 static bool
2862 pool_in_page(struct pool *pp, struct pool_item_header *ph, uintptr_t addr)
2863 {
2864 
2865 	return (uintptr_t)ph->ph_page <= addr &&
2866 	    addr < (uintptr_t)ph->ph_page + pp->pr_alloc->pa_pagesz;
2867 }
2868 
2869 static bool
2870 pool_in_item(struct pool *pp, void *item, uintptr_t addr)
2871 {
2872 
2873 	return (uintptr_t)item <= addr && addr < (uintptr_t)item + pp->pr_size;
2874 }
2875 
2876 static bool
2877 pool_in_cg(struct pool *pp, struct pool_cache_group *pcg, uintptr_t addr)
2878 {
2879 	int i;
2880 
2881 	if (pcg == NULL) {
2882 		return false;
2883 	}
2884 	for (i = 0; i < pcg->pcg_avail; i++) {
2885 		if (pool_in_item(pp, pcg->pcg_objects[i].pcgo_va, addr)) {
2886 			return true;
2887 		}
2888 	}
2889 	return false;
2890 }
2891 
2892 static bool
2893 pool_allocated(struct pool *pp, struct pool_item_header *ph, uintptr_t addr)
2894 {
2895 
2896 	if ((pp->pr_roflags & PR_NOTOUCH) != 0) {
2897 		unsigned int idx = pr_item_notouch_index(pp, ph, (void *)addr);
2898 		pool_item_bitmap_t *bitmap =
2899 		    ph->ph_bitmap + (idx / BITMAP_SIZE);
2900 		pool_item_bitmap_t mask = 1 << (idx & BITMAP_MASK);
2901 
2902 		return (*bitmap & mask) == 0;
2903 	} else {
2904 		struct pool_item *pi;
2905 
2906 		LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
2907 			if (pool_in_item(pp, pi, addr)) {
2908 				return false;
2909 			}
2910 		}
2911 		return true;
2912 	}
2913 }
2914 
2915 void
2916 pool_whatis(uintptr_t addr, void (*pr)(const char *, ...))
2917 {
2918 	struct pool *pp;
2919 
2920 	TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
2921 		struct pool_item_header *ph;
2922 		uintptr_t item;
2923 		bool allocated = true;
2924 		bool incache = false;
2925 		bool incpucache = false;
2926 		char cpucachestr[32];
2927 
2928 		if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
2929 			LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
2930 				if (pool_in_page(pp, ph, addr)) {
2931 					goto found;
2932 				}
2933 			}
2934 			LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
2935 				if (pool_in_page(pp, ph, addr)) {
2936 					allocated =
2937 					    pool_allocated(pp, ph, addr);
2938 					goto found;
2939 				}
2940 			}
2941 			LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
2942 				if (pool_in_page(pp, ph, addr)) {
2943 					allocated = false;
2944 					goto found;
2945 				}
2946 			}
2947 			continue;
2948 		} else {
2949 			ph = pr_find_pagehead_noalign(pp, (void *)addr);
2950 			if (ph == NULL || !pool_in_page(pp, ph, addr)) {
2951 				continue;
2952 			}
2953 			allocated = pool_allocated(pp, ph, addr);
2954 		}
2955 found:
2956 		if (allocated && pp->pr_cache) {
2957 			pool_cache_t pc = pp->pr_cache;
2958 			struct pool_cache_group *pcg;
2959 			int i;
2960 
2961 			for (pcg = pc->pc_fullgroups; pcg != NULL;
2962 			    pcg = pcg->pcg_next) {
2963 				if (pool_in_cg(pp, pcg, addr)) {
2964 					incache = true;
2965 					goto print;
2966 				}
2967 			}
2968 			for (i = 0; i < MAXCPUS; i++) {
2969 				pool_cache_cpu_t *cc;
2970 
2971 				if ((cc = pc->pc_cpus[i]) == NULL) {
2972 					continue;
2973 				}
2974 				if (pool_in_cg(pp, cc->cc_current, addr) ||
2975 				    pool_in_cg(pp, cc->cc_previous, addr)) {
2976 					struct cpu_info *ci =
2977 					    cpu_lookup_byindex(i);
2978 
2979 					incpucache = true;
2980 					snprintf(cpucachestr,
2981 					    sizeof(cpucachestr),
2982 					    "cached by CPU %u",
2983 					    ci->ci_index);
2984 					goto print;
2985 				}
2986 			}
2987 		}
2988 print:
2989 		item = (uintptr_t)ph->ph_page + ph->ph_off;
2990 		item = item + rounddown(addr - item, pp->pr_size);
2991 		(*pr)("%p is %p+%zu in POOL '%s' (%s)\n",
2992 		    (void *)addr, item, (size_t)(addr - item),
2993 		    pp->pr_wchan,
2994 		    incpucache ? cpucachestr :
2995 		    incache ? "cached" : allocated ? "allocated" : "free");
2996 	}
2997 }
2998 #endif /* defined(DDB) */
2999