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