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