Home | History | Annotate | Line # | Download | only in kern
subr_pool.c revision 1.114
      1 /*	$NetBSD: subr_pool.c,v 1.114 2006/04/02 13:25:34 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1999, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.114 2006/04/02 13:25:34 yamt Exp $");
     42 
     43 #include "opt_pool.h"
     44 #include "opt_poollog.h"
     45 #include "opt_lockdebug.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/proc.h>
     50 #include <sys/errno.h>
     51 #include <sys/kernel.h>
     52 #include <sys/malloc.h>
     53 #include <sys/lock.h>
     54 #include <sys/pool.h>
     55 #include <sys/syslog.h>
     56 
     57 #include <uvm/uvm.h>
     58 
     59 /*
     60  * Pool resource management utility.
     61  *
     62  * Memory is allocated in pages which are split into pieces according to
     63  * the pool item size. Each page is kept on one of three lists in the
     64  * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages',
     65  * for empty, full and partially-full pages respectively. The individual
     66  * pool items are on a linked list headed by `ph_itemlist' in each page
     67  * header. The memory for building the page list is either taken from
     68  * the allocated pages themselves (for small pool items) or taken from
     69  * an internal pool of page headers (`phpool').
     70  */
     71 
     72 /* List of all pools */
     73 LIST_HEAD(,pool) pool_head = LIST_HEAD_INITIALIZER(pool_head);
     74 
     75 /* Private pool for page header structures */
     76 #define	PHPOOL_MAX	8
     77 static struct pool phpool[PHPOOL_MAX];
     78 #define	PHPOOL_FREELIST_NELEM(idx)	(((idx) == 0) ? 0 : (1 << (idx)))
     79 
     80 #ifdef POOL_SUBPAGE
     81 /* Pool of subpages for use by normal pools. */
     82 static struct pool psppool;
     83 #endif
     84 
     85 static void *pool_page_alloc_meta(struct pool *, int);
     86 static void pool_page_free_meta(struct pool *, void *);
     87 
     88 /* allocator for pool metadata */
     89 static struct pool_allocator pool_allocator_meta = {
     90 	pool_page_alloc_meta, pool_page_free_meta
     91 };
     92 
     93 /* # of seconds to retain page after last use */
     94 int pool_inactive_time = 10;
     95 
     96 /* Next candidate for drainage (see pool_drain()) */
     97 static struct pool	*drainpp;
     98 
     99 /* This spin lock protects both pool_head and drainpp. */
    100 struct simplelock pool_head_slock = SIMPLELOCK_INITIALIZER;
    101 
    102 typedef uint8_t pool_item_freelist_t;
    103 
    104 struct pool_item_header {
    105 	/* Page headers */
    106 	LIST_ENTRY(pool_item_header)
    107 				ph_pagelist;	/* pool page list */
    108 	SPLAY_ENTRY(pool_item_header)
    109 				ph_node;	/* Off-page page headers */
    110 	caddr_t			ph_page;	/* this page's address */
    111 	struct timeval		ph_time;	/* last referenced */
    112 	union {
    113 		/* !PR_NOTOUCH */
    114 		struct {
    115 			LIST_HEAD(, pool_item)
    116 				phu_itemlist;	/* chunk list for this page */
    117 		} phu_normal;
    118 		/* PR_NOTOUCH */
    119 		struct {
    120 			uint16_t
    121 				phu_off;	/* start offset in page */
    122 			pool_item_freelist_t
    123 				phu_firstfree;	/* first free item */
    124 			/*
    125 			 * XXX it might be better to use
    126 			 * a simple bitmap and ffs(3)
    127 			 */
    128 		} phu_notouch;
    129 	} ph_u;
    130 	uint16_t		ph_nmissing;	/* # of chunks in use */
    131 };
    132 #define	ph_itemlist	ph_u.phu_normal.phu_itemlist
    133 #define	ph_off		ph_u.phu_notouch.phu_off
    134 #define	ph_firstfree	ph_u.phu_notouch.phu_firstfree
    135 
    136 struct pool_item {
    137 #ifdef DIAGNOSTIC
    138 	u_int pi_magic;
    139 #endif
    140 #define	PI_MAGIC 0xdeadbeefU
    141 	/* Other entries use only this list entry */
    142 	LIST_ENTRY(pool_item)	pi_list;
    143 };
    144 
    145 #define	POOL_NEEDS_CATCHUP(pp)						\
    146 	((pp)->pr_nitems < (pp)->pr_minitems)
    147 
    148 /*
    149  * Pool cache management.
    150  *
    151  * Pool caches provide a way for constructed objects to be cached by the
    152  * pool subsystem.  This can lead to performance improvements by avoiding
    153  * needless object construction/destruction; it is deferred until absolutely
    154  * necessary.
    155  *
    156  * Caches are grouped into cache groups.  Each cache group references
    157  * up to 16 constructed objects.  When a cache allocates an object
    158  * from the pool, it calls the object's constructor and places it into
    159  * a cache group.  When a cache group frees an object back to the pool,
    160  * it first calls the object's destructor.  This allows the object to
    161  * persist in constructed form while freed to the cache.
    162  *
    163  * Multiple caches may exist for each pool.  This allows a single
    164  * object type to have multiple constructed forms.  The pool references
    165  * each cache, so that when a pool is drained by the pagedaemon, it can
    166  * drain each individual cache as well.  Each time a cache is drained,
    167  * the most idle cache group is freed to the pool in its entirety.
    168  *
    169  * Pool caches are layed on top of pools.  By layering them, we can avoid
    170  * the complexity of cache management for pools which would not benefit
    171  * from it.
    172  */
    173 
    174 /* The cache group pool. */
    175 static struct pool pcgpool;
    176 
    177 static void	pool_cache_reclaim(struct pool_cache *, struct pool_pagelist *,
    178 				   struct pool_cache_grouplist *);
    179 static void	pcg_grouplist_free(struct pool_cache_grouplist *);
    180 
    181 static int	pool_catchup(struct pool *);
    182 static void	pool_prime_page(struct pool *, caddr_t,
    183 		    struct pool_item_header *);
    184 static void	pool_update_curpage(struct pool *);
    185 
    186 static int	pool_grow(struct pool *, int);
    187 void		*pool_allocator_alloc(struct pool *, int);
    188 void		pool_allocator_free(struct pool *, void *);
    189 
    190 static void pool_print_pagelist(struct pool *, struct pool_pagelist *,
    191 	void (*)(const char *, ...));
    192 static void pool_print1(struct pool *, const char *,
    193 	void (*)(const char *, ...));
    194 
    195 static int pool_chk_page(struct pool *, const char *,
    196 			 struct pool_item_header *);
    197 
    198 /*
    199  * Pool log entry. An array of these is allocated in pool_init().
    200  */
    201 struct pool_log {
    202 	const char	*pl_file;
    203 	long		pl_line;
    204 	int		pl_action;
    205 #define	PRLOG_GET	1
    206 #define	PRLOG_PUT	2
    207 	void		*pl_addr;
    208 };
    209 
    210 #ifdef POOL_DIAGNOSTIC
    211 /* Number of entries in pool log buffers */
    212 #ifndef POOL_LOGSIZE
    213 #define	POOL_LOGSIZE	10
    214 #endif
    215 
    216 int pool_logsize = POOL_LOGSIZE;
    217 
    218 static inline void
    219 pr_log(struct pool *pp, void *v, int action, const char *file, long line)
    220 {
    221 	int n = pp->pr_curlogentry;
    222 	struct pool_log *pl;
    223 
    224 	if ((pp->pr_roflags & PR_LOGGING) == 0)
    225 		return;
    226 
    227 	/*
    228 	 * Fill in the current entry. Wrap around and overwrite
    229 	 * the oldest entry if necessary.
    230 	 */
    231 	pl = &pp->pr_log[n];
    232 	pl->pl_file = file;
    233 	pl->pl_line = line;
    234 	pl->pl_action = action;
    235 	pl->pl_addr = v;
    236 	if (++n >= pp->pr_logsize)
    237 		n = 0;
    238 	pp->pr_curlogentry = n;
    239 }
    240 
    241 static void
    242 pr_printlog(struct pool *pp, struct pool_item *pi,
    243     void (*pr)(const char *, ...))
    244 {
    245 	int i = pp->pr_logsize;
    246 	int n = pp->pr_curlogentry;
    247 
    248 	if ((pp->pr_roflags & PR_LOGGING) == 0)
    249 		return;
    250 
    251 	/*
    252 	 * Print all entries in this pool's log.
    253 	 */
    254 	while (i-- > 0) {
    255 		struct pool_log *pl = &pp->pr_log[n];
    256 		if (pl->pl_action != 0) {
    257 			if (pi == NULL || pi == pl->pl_addr) {
    258 				(*pr)("\tlog entry %d:\n", i);
    259 				(*pr)("\t\taction = %s, addr = %p\n",
    260 				    pl->pl_action == PRLOG_GET ? "get" : "put",
    261 				    pl->pl_addr);
    262 				(*pr)("\t\tfile: %s at line %lu\n",
    263 				    pl->pl_file, pl->pl_line);
    264 			}
    265 		}
    266 		if (++n >= pp->pr_logsize)
    267 			n = 0;
    268 	}
    269 }
    270 
    271 static inline void
    272 pr_enter(struct pool *pp, const char *file, long line)
    273 {
    274 
    275 	if (__predict_false(pp->pr_entered_file != NULL)) {
    276 		printf("pool %s: reentrancy at file %s line %ld\n",
    277 		    pp->pr_wchan, file, line);
    278 		printf("         previous entry at file %s line %ld\n",
    279 		    pp->pr_entered_file, pp->pr_entered_line);
    280 		panic("pr_enter");
    281 	}
    282 
    283 	pp->pr_entered_file = file;
    284 	pp->pr_entered_line = line;
    285 }
    286 
    287 static inline void
    288 pr_leave(struct pool *pp)
    289 {
    290 
    291 	if (__predict_false(pp->pr_entered_file == NULL)) {
    292 		printf("pool %s not entered?\n", pp->pr_wchan);
    293 		panic("pr_leave");
    294 	}
    295 
    296 	pp->pr_entered_file = NULL;
    297 	pp->pr_entered_line = 0;
    298 }
    299 
    300 static inline void
    301 pr_enter_check(struct pool *pp, void (*pr)(const char *, ...))
    302 {
    303 
    304 	if (pp->pr_entered_file != NULL)
    305 		(*pr)("\n\tcurrently entered from file %s line %ld\n",
    306 		    pp->pr_entered_file, pp->pr_entered_line);
    307 }
    308 #else
    309 #define	pr_log(pp, v, action, file, line)
    310 #define	pr_printlog(pp, pi, pr)
    311 #define	pr_enter(pp, file, line)
    312 #define	pr_leave(pp)
    313 #define	pr_enter_check(pp, pr)
    314 #endif /* POOL_DIAGNOSTIC */
    315 
    316 static inline int
    317 pr_item_notouch_index(const struct pool *pp, const struct pool_item_header *ph,
    318     const void *v)
    319 {
    320 	const char *cp = v;
    321 	int idx;
    322 
    323 	KASSERT(pp->pr_roflags & PR_NOTOUCH);
    324 	idx = (cp - ph->ph_page - ph->ph_off) / pp->pr_size;
    325 	KASSERT(idx < pp->pr_itemsperpage);
    326 	return idx;
    327 }
    328 
    329 #define	PR_FREELIST_ALIGN(p) \
    330 	roundup((uintptr_t)(p), sizeof(pool_item_freelist_t))
    331 #define	PR_FREELIST(ph)	((pool_item_freelist_t *)PR_FREELIST_ALIGN((ph) + 1))
    332 #define	PR_INDEX_USED	((pool_item_freelist_t)-1)
    333 #define	PR_INDEX_EOL	((pool_item_freelist_t)-2)
    334 
    335 static inline void
    336 pr_item_notouch_put(const struct pool *pp, struct pool_item_header *ph,
    337     void *obj)
    338 {
    339 	int idx = pr_item_notouch_index(pp, ph, obj);
    340 	pool_item_freelist_t *freelist = PR_FREELIST(ph);
    341 
    342 	KASSERT(freelist[idx] == PR_INDEX_USED);
    343 	freelist[idx] = ph->ph_firstfree;
    344 	ph->ph_firstfree = idx;
    345 }
    346 
    347 static inline void *
    348 pr_item_notouch_get(const struct pool *pp, struct pool_item_header *ph)
    349 {
    350 	int idx = ph->ph_firstfree;
    351 	pool_item_freelist_t *freelist = PR_FREELIST(ph);
    352 
    353 	KASSERT(freelist[idx] != PR_INDEX_USED);
    354 	ph->ph_firstfree = freelist[idx];
    355 	freelist[idx] = PR_INDEX_USED;
    356 
    357 	return ph->ph_page + ph->ph_off + idx * pp->pr_size;
    358 }
    359 
    360 static inline int
    361 phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
    362 {
    363 	if (a->ph_page < b->ph_page)
    364 		return (-1);
    365 	else if (a->ph_page > b->ph_page)
    366 		return (1);
    367 	else
    368 		return (0);
    369 }
    370 
    371 SPLAY_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare);
    372 SPLAY_GENERATE(phtree, pool_item_header, ph_node, phtree_compare);
    373 
    374 /*
    375  * Return the pool page header based on page address.
    376  */
    377 static inline struct pool_item_header *
    378 pr_find_pagehead(struct pool *pp, caddr_t page)
    379 {
    380 	struct pool_item_header *ph, tmp;
    381 
    382 	if ((pp->pr_roflags & PR_PHINPAGE) != 0)
    383 		return ((struct pool_item_header *)(page + pp->pr_phoffset));
    384 
    385 	tmp.ph_page = page;
    386 	ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
    387 	return ph;
    388 }
    389 
    390 static void
    391 pr_pagelist_free(struct pool *pp, struct pool_pagelist *pq)
    392 {
    393 	struct pool_item_header *ph;
    394 	int s;
    395 
    396 	while ((ph = LIST_FIRST(pq)) != NULL) {
    397 		LIST_REMOVE(ph, ph_pagelist);
    398 		pool_allocator_free(pp, ph->ph_page);
    399 		if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
    400 			s = splvm();
    401 			pool_put(pp->pr_phpool, ph);
    402 			splx(s);
    403 		}
    404 	}
    405 }
    406 
    407 /*
    408  * Remove a page from the pool.
    409  */
    410 static inline void
    411 pr_rmpage(struct pool *pp, struct pool_item_header *ph,
    412      struct pool_pagelist *pq)
    413 {
    414 
    415 	LOCK_ASSERT(simple_lock_held(&pp->pr_slock));
    416 
    417 	/*
    418 	 * If the page was idle, decrement the idle page count.
    419 	 */
    420 	if (ph->ph_nmissing == 0) {
    421 #ifdef DIAGNOSTIC
    422 		if (pp->pr_nidle == 0)
    423 			panic("pr_rmpage: nidle inconsistent");
    424 		if (pp->pr_nitems < pp->pr_itemsperpage)
    425 			panic("pr_rmpage: nitems inconsistent");
    426 #endif
    427 		pp->pr_nidle--;
    428 	}
    429 
    430 	pp->pr_nitems -= pp->pr_itemsperpage;
    431 
    432 	/*
    433 	 * Unlink the page from the pool and queue it for release.
    434 	 */
    435 	LIST_REMOVE(ph, ph_pagelist);
    436 	if ((pp->pr_roflags & PR_PHINPAGE) == 0)
    437 		SPLAY_REMOVE(phtree, &pp->pr_phtree, ph);
    438 	LIST_INSERT_HEAD(pq, ph, ph_pagelist);
    439 
    440 	pp->pr_npages--;
    441 	pp->pr_npagefree++;
    442 
    443 	pool_update_curpage(pp);
    444 }
    445 
    446 /*
    447  * Initialize all the pools listed in the "pools" link set.
    448  */
    449 void
    450 link_pool_init(void)
    451 {
    452 	__link_set_decl(pools, struct link_pool_init);
    453 	struct link_pool_init * const *pi;
    454 
    455 	__link_set_foreach(pi, pools)
    456 		pool_init((*pi)->pp, (*pi)->size, (*pi)->align,
    457 		    (*pi)->align_offset, (*pi)->flags, (*pi)->wchan,
    458 		    (*pi)->palloc);
    459 }
    460 
    461 /*
    462  * Initialize the given pool resource structure.
    463  *
    464  * We export this routine to allow other kernel parts to declare
    465  * static pools that must be initialized before malloc() is available.
    466  */
    467 void
    468 pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
    469     const char *wchan, struct pool_allocator *palloc)
    470 {
    471 	int off, slack;
    472 	size_t trysize, phsize;
    473 	int s;
    474 
    475 	KASSERT((1UL << (CHAR_BIT * sizeof(pool_item_freelist_t))) - 2 >=
    476 	    PHPOOL_FREELIST_NELEM(PHPOOL_MAX - 1));
    477 
    478 #ifdef POOL_DIAGNOSTIC
    479 	/*
    480 	 * Always log if POOL_DIAGNOSTIC is defined.
    481 	 */
    482 	if (pool_logsize != 0)
    483 		flags |= PR_LOGGING;
    484 #endif
    485 
    486 	if (palloc == NULL)
    487 		palloc = &pool_allocator_kmem;
    488 #ifdef POOL_SUBPAGE
    489 	if (size > palloc->pa_pagesz) {
    490 		if (palloc == &pool_allocator_kmem)
    491 			palloc = &pool_allocator_kmem_fullpage;
    492 		else if (palloc == &pool_allocator_nointr)
    493 			palloc = &pool_allocator_nointr_fullpage;
    494 	}
    495 #endif /* POOL_SUBPAGE */
    496 	if ((palloc->pa_flags & PA_INITIALIZED) == 0) {
    497 		if (palloc->pa_pagesz == 0)
    498 			palloc->pa_pagesz = PAGE_SIZE;
    499 
    500 		TAILQ_INIT(&palloc->pa_list);
    501 
    502 		simple_lock_init(&palloc->pa_slock);
    503 		palloc->pa_pagemask = ~(palloc->pa_pagesz - 1);
    504 		palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1;
    505 		palloc->pa_flags |= PA_INITIALIZED;
    506 	}
    507 
    508 	if (align == 0)
    509 		align = ALIGN(1);
    510 
    511 	if (size < sizeof(struct pool_item))
    512 		size = sizeof(struct pool_item);
    513 
    514 	size = roundup(size, align);
    515 #ifdef DIAGNOSTIC
    516 	if (size > palloc->pa_pagesz)
    517 		panic("pool_init: pool item size (%lu) too large",
    518 		      (u_long)size);
    519 #endif
    520 
    521 	/*
    522 	 * Initialize the pool structure.
    523 	 */
    524 	LIST_INIT(&pp->pr_emptypages);
    525 	LIST_INIT(&pp->pr_fullpages);
    526 	LIST_INIT(&pp->pr_partpages);
    527 	LIST_INIT(&pp->pr_cachelist);
    528 	pp->pr_curpage = NULL;
    529 	pp->pr_npages = 0;
    530 	pp->pr_minitems = 0;
    531 	pp->pr_minpages = 0;
    532 	pp->pr_maxpages = UINT_MAX;
    533 	pp->pr_roflags = flags;
    534 	pp->pr_flags = 0;
    535 	pp->pr_size = size;
    536 	pp->pr_align = align;
    537 	pp->pr_wchan = wchan;
    538 	pp->pr_alloc = palloc;
    539 	pp->pr_nitems = 0;
    540 	pp->pr_nout = 0;
    541 	pp->pr_hardlimit = UINT_MAX;
    542 	pp->pr_hardlimit_warning = NULL;
    543 	pp->pr_hardlimit_ratecap.tv_sec = 0;
    544 	pp->pr_hardlimit_ratecap.tv_usec = 0;
    545 	pp->pr_hardlimit_warning_last.tv_sec = 0;
    546 	pp->pr_hardlimit_warning_last.tv_usec = 0;
    547 	pp->pr_drain_hook = NULL;
    548 	pp->pr_drain_hook_arg = NULL;
    549 
    550 	/*
    551 	 * Decide whether to put the page header off page to avoid
    552 	 * wasting too large a part of the page or too big item.
    553 	 * Off-page page headers go on a hash table, so we can match
    554 	 * a returned item with its header based on the page address.
    555 	 * We use 1/16 of the page size and about 8 times of the item
    556 	 * size as the threshold (XXX: tune)
    557 	 *
    558 	 * However, we'll put the header into the page if we can put
    559 	 * it without wasting any items.
    560 	 *
    561 	 * Silently enforce `0 <= ioff < align'.
    562 	 */
    563 	pp->pr_itemoffset = ioff %= align;
    564 	/* See the comment below about reserved bytes. */
    565 	trysize = palloc->pa_pagesz - ((align - ioff) % align);
    566 	phsize = ALIGN(sizeof(struct pool_item_header));
    567 	if ((pp->pr_roflags & PR_NOTOUCH) == 0 &&
    568 	    (pp->pr_size < MIN(palloc->pa_pagesz / 16, phsize << 3) ||
    569 	    trysize / pp->pr_size == (trysize - phsize) / pp->pr_size)) {
    570 		/* Use the end of the page for the page header */
    571 		pp->pr_roflags |= PR_PHINPAGE;
    572 		pp->pr_phoffset = off = palloc->pa_pagesz - phsize;
    573 	} else {
    574 		/* The page header will be taken from our page header pool */
    575 		pp->pr_phoffset = 0;
    576 		off = palloc->pa_pagesz;
    577 		SPLAY_INIT(&pp->pr_phtree);
    578 	}
    579 
    580 	/*
    581 	 * Alignment is to take place at `ioff' within the item. This means
    582 	 * we must reserve up to `align - 1' bytes on the page to allow
    583 	 * appropriate positioning of each item.
    584 	 */
    585 	pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
    586 	KASSERT(pp->pr_itemsperpage != 0);
    587 	if ((pp->pr_roflags & PR_NOTOUCH)) {
    588 		int idx;
    589 
    590 		for (idx = 0; pp->pr_itemsperpage > PHPOOL_FREELIST_NELEM(idx);
    591 		    idx++) {
    592 			/* nothing */
    593 		}
    594 		if (idx >= PHPOOL_MAX) {
    595 			/*
    596 			 * if you see this panic, consider to tweak
    597 			 * PHPOOL_MAX and PHPOOL_FREELIST_NELEM.
    598 			 */
    599 			panic("%s: too large itemsperpage(%d) for PR_NOTOUCH",
    600 			    pp->pr_wchan, pp->pr_itemsperpage);
    601 		}
    602 		pp->pr_phpool = &phpool[idx];
    603 	} else if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
    604 		pp->pr_phpool = &phpool[0];
    605 	}
    606 #if defined(DIAGNOSTIC)
    607 	else {
    608 		pp->pr_phpool = NULL;
    609 	}
    610 #endif
    611 
    612 	/*
    613 	 * Use the slack between the chunks and the page header
    614 	 * for "cache coloring".
    615 	 */
    616 	slack = off - pp->pr_itemsperpage * pp->pr_size;
    617 	pp->pr_maxcolor = (slack / align) * align;
    618 	pp->pr_curcolor = 0;
    619 
    620 	pp->pr_nget = 0;
    621 	pp->pr_nfail = 0;
    622 	pp->pr_nput = 0;
    623 	pp->pr_npagealloc = 0;
    624 	pp->pr_npagefree = 0;
    625 	pp->pr_hiwat = 0;
    626 	pp->pr_nidle = 0;
    627 
    628 #ifdef POOL_DIAGNOSTIC
    629 	if (flags & PR_LOGGING) {
    630 		if (kmem_map == NULL ||
    631 		    (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
    632 		     M_TEMP, M_NOWAIT)) == NULL)
    633 			pp->pr_roflags &= ~PR_LOGGING;
    634 		pp->pr_curlogentry = 0;
    635 		pp->pr_logsize = pool_logsize;
    636 	}
    637 #endif
    638 
    639 	pp->pr_entered_file = NULL;
    640 	pp->pr_entered_line = 0;
    641 
    642 	simple_lock_init(&pp->pr_slock);
    643 
    644 	/*
    645 	 * Initialize private page header pool and cache magazine pool if we
    646 	 * haven't done so yet.
    647 	 * XXX LOCKING.
    648 	 */
    649 	if (phpool[0].pr_size == 0) {
    650 		int idx;
    651 		for (idx = 0; idx < PHPOOL_MAX; idx++) {
    652 			static char phpool_names[PHPOOL_MAX][6+1+6+1];
    653 			int nelem;
    654 			size_t sz;
    655 
    656 			nelem = PHPOOL_FREELIST_NELEM(idx);
    657 			snprintf(phpool_names[idx], sizeof(phpool_names[idx]),
    658 			    "phpool-%d", nelem);
    659 			sz = sizeof(struct pool_item_header);
    660 			if (nelem) {
    661 				sz = PR_FREELIST_ALIGN(sz)
    662 				    + nelem * sizeof(pool_item_freelist_t);
    663 			}
    664 			pool_init(&phpool[idx], sz, 0, 0, 0,
    665 			    phpool_names[idx], &pool_allocator_meta);
    666 		}
    667 #ifdef POOL_SUBPAGE
    668 		pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0,
    669 		    PR_RECURSIVE, "psppool", &pool_allocator_meta);
    670 #endif
    671 		pool_init(&pcgpool, sizeof(struct pool_cache_group), 0, 0,
    672 		    0, "pcgpool", &pool_allocator_meta);
    673 	}
    674 
    675 	/* Insert into the list of all pools. */
    676 	simple_lock(&pool_head_slock);
    677 	LIST_INSERT_HEAD(&pool_head, pp, pr_poollist);
    678 	simple_unlock(&pool_head_slock);
    679 
    680 	/* Insert this into the list of pools using this allocator. */
    681 	s = splvm();
    682 	simple_lock(&palloc->pa_slock);
    683 	TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
    684 	simple_unlock(&palloc->pa_slock);
    685 	splx(s);
    686 }
    687 
    688 /*
    689  * De-commision a pool resource.
    690  */
    691 void
    692 pool_destroy(struct pool *pp)
    693 {
    694 	struct pool_pagelist pq;
    695 	struct pool_item_header *ph;
    696 	int s;
    697 
    698 	/* Remove from global pool list */
    699 	simple_lock(&pool_head_slock);
    700 	LIST_REMOVE(pp, pr_poollist);
    701 	if (drainpp == pp)
    702 		drainpp = NULL;
    703 	simple_unlock(&pool_head_slock);
    704 
    705 	/* Remove this pool from its allocator's list of pools. */
    706 	s = splvm();
    707 	simple_lock(&pp->pr_alloc->pa_slock);
    708 	TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list);
    709 	simple_unlock(&pp->pr_alloc->pa_slock);
    710 	splx(s);
    711 
    712 	s = splvm();
    713 	simple_lock(&pp->pr_slock);
    714 
    715 	KASSERT(LIST_EMPTY(&pp->pr_cachelist));
    716 
    717 #ifdef DIAGNOSTIC
    718 	if (pp->pr_nout != 0) {
    719 		pr_printlog(pp, NULL, printf);
    720 		panic("pool_destroy: pool busy: still out: %u",
    721 		    pp->pr_nout);
    722 	}
    723 #endif
    724 
    725 	KASSERT(LIST_EMPTY(&pp->pr_fullpages));
    726 	KASSERT(LIST_EMPTY(&pp->pr_partpages));
    727 
    728 	/* Remove all pages */
    729 	LIST_INIT(&pq);
    730 	while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
    731 		pr_rmpage(pp, ph, &pq);
    732 
    733 	simple_unlock(&pp->pr_slock);
    734 	splx(s);
    735 
    736 	pr_pagelist_free(pp, &pq);
    737 
    738 #ifdef POOL_DIAGNOSTIC
    739 	if ((pp->pr_roflags & PR_LOGGING) != 0)
    740 		free(pp->pr_log, M_TEMP);
    741 #endif
    742 }
    743 
    744 void
    745 pool_set_drain_hook(struct pool *pp, void (*fn)(void *, int), void *arg)
    746 {
    747 
    748 	/* XXX no locking -- must be used just after pool_init() */
    749 #ifdef DIAGNOSTIC
    750 	if (pp->pr_drain_hook != NULL)
    751 		panic("pool_set_drain_hook(%s): already set", pp->pr_wchan);
    752 #endif
    753 	pp->pr_drain_hook = fn;
    754 	pp->pr_drain_hook_arg = arg;
    755 }
    756 
    757 static struct pool_item_header *
    758 pool_alloc_item_header(struct pool *pp, caddr_t storage, int flags)
    759 {
    760 	struct pool_item_header *ph;
    761 	int s;
    762 
    763 	LOCK_ASSERT(simple_lock_held(&pp->pr_slock) == 0);
    764 
    765 	if ((pp->pr_roflags & PR_PHINPAGE) != 0)
    766 		ph = (struct pool_item_header *) (storage + pp->pr_phoffset);
    767 	else {
    768 		s = splvm();
    769 		ph = pool_get(pp->pr_phpool, flags);
    770 		splx(s);
    771 	}
    772 
    773 	return (ph);
    774 }
    775 
    776 /*
    777  * Grab an item from the pool; must be called at appropriate spl level
    778  */
    779 void *
    780 #ifdef POOL_DIAGNOSTIC
    781 _pool_get(struct pool *pp, int flags, const char *file, long line)
    782 #else
    783 pool_get(struct pool *pp, int flags)
    784 #endif
    785 {
    786 	struct pool_item *pi;
    787 	struct pool_item_header *ph;
    788 	void *v;
    789 
    790 #ifdef DIAGNOSTIC
    791 	if (__predict_false(pp->pr_itemsperpage == 0))
    792 		panic("pool_get: pool %p: pr_itemsperpage is zero, "
    793 		    "pool not initialized?", pp);
    794 	if (__predict_false(curlwp == NULL && doing_shutdown == 0 &&
    795 			    (flags & PR_WAITOK) != 0))
    796 		panic("pool_get: %s: must have NOWAIT", pp->pr_wchan);
    797 
    798 #endif /* DIAGNOSTIC */
    799 #ifdef LOCKDEBUG
    800 	if (flags & PR_WAITOK)
    801 		simple_lock_only_held(NULL, "pool_get(PR_WAITOK)");
    802 	SCHED_ASSERT_UNLOCKED();
    803 #endif
    804 
    805 	simple_lock(&pp->pr_slock);
    806 	pr_enter(pp, file, line);
    807 
    808  startover:
    809 	/*
    810 	 * Check to see if we've reached the hard limit.  If we have,
    811 	 * and we can wait, then wait until an item has been returned to
    812 	 * the pool.
    813 	 */
    814 #ifdef DIAGNOSTIC
    815 	if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
    816 		pr_leave(pp);
    817 		simple_unlock(&pp->pr_slock);
    818 		panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
    819 	}
    820 #endif
    821 	if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
    822 		if (pp->pr_drain_hook != NULL) {
    823 			/*
    824 			 * Since the drain hook is going to free things
    825 			 * back to the pool, unlock, call the hook, re-lock,
    826 			 * and check the hardlimit condition again.
    827 			 */
    828 			pr_leave(pp);
    829 			simple_unlock(&pp->pr_slock);
    830 			(*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
    831 			simple_lock(&pp->pr_slock);
    832 			pr_enter(pp, file, line);
    833 			if (pp->pr_nout < pp->pr_hardlimit)
    834 				goto startover;
    835 		}
    836 
    837 		if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
    838 			/*
    839 			 * XXX: A warning isn't logged in this case.  Should
    840 			 * it be?
    841 			 */
    842 			pp->pr_flags |= PR_WANTED;
    843 			pr_leave(pp);
    844 			ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
    845 			pr_enter(pp, file, line);
    846 			goto startover;
    847 		}
    848 
    849 		/*
    850 		 * Log a message that the hard limit has been hit.
    851 		 */
    852 		if (pp->pr_hardlimit_warning != NULL &&
    853 		    ratecheck(&pp->pr_hardlimit_warning_last,
    854 			      &pp->pr_hardlimit_ratecap))
    855 			log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
    856 
    857 		pp->pr_nfail++;
    858 
    859 		pr_leave(pp);
    860 		simple_unlock(&pp->pr_slock);
    861 		return (NULL);
    862 	}
    863 
    864 	/*
    865 	 * The convention we use is that if `curpage' is not NULL, then
    866 	 * it points at a non-empty bucket. In particular, `curpage'
    867 	 * never points at a page header which has PR_PHINPAGE set and
    868 	 * has no items in its bucket.
    869 	 */
    870 	if ((ph = pp->pr_curpage) == NULL) {
    871 		int error;
    872 
    873 #ifdef DIAGNOSTIC
    874 		if (pp->pr_nitems != 0) {
    875 			simple_unlock(&pp->pr_slock);
    876 			printf("pool_get: %s: curpage NULL, nitems %u\n",
    877 			    pp->pr_wchan, pp->pr_nitems);
    878 			panic("pool_get: nitems inconsistent");
    879 		}
    880 #endif
    881 
    882 		/*
    883 		 * Call the back-end page allocator for more memory.
    884 		 * Release the pool lock, as the back-end page allocator
    885 		 * may block.
    886 		 */
    887 		pr_leave(pp);
    888 		error = pool_grow(pp, flags);
    889 		pr_enter(pp, file, line);
    890 		if (error != 0) {
    891 			/*
    892 			 * We were unable to allocate a page or item
    893 			 * header, but we released the lock during
    894 			 * allocation, so perhaps items were freed
    895 			 * back to the pool.  Check for this case.
    896 			 */
    897 			if (pp->pr_curpage != NULL)
    898 				goto startover;
    899 
    900 			if ((flags & PR_WAITOK) == 0) {
    901 				pp->pr_nfail++;
    902 				pr_leave(pp);
    903 				simple_unlock(&pp->pr_slock);
    904 				return (NULL);
    905 			}
    906 
    907 			/*
    908 			 * Wait for items to be returned to this pool.
    909 			 *
    910 			 * wake up once a second and try again,
    911 			 * as the check in pool_cache_put_paddr() is racy.
    912 			 */
    913 			pp->pr_flags |= PR_WANTED;
    914 			/* PA_WANTED is already set on the allocator. */
    915 			pr_leave(pp);
    916 			ltsleep(pp, PSWP, pp->pr_wchan, hz, &pp->pr_slock);
    917 			pr_enter(pp, file, line);
    918 		}
    919 
    920 		/* Start the allocation process over. */
    921 		goto startover;
    922 	}
    923 	if (pp->pr_roflags & PR_NOTOUCH) {
    924 #ifdef DIAGNOSTIC
    925 		if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) {
    926 			pr_leave(pp);
    927 			simple_unlock(&pp->pr_slock);
    928 			panic("pool_get: %s: page empty", pp->pr_wchan);
    929 		}
    930 #endif
    931 		v = pr_item_notouch_get(pp, ph);
    932 #ifdef POOL_DIAGNOSTIC
    933 		pr_log(pp, v, PRLOG_GET, file, line);
    934 #endif
    935 	} else {
    936 		v = pi = LIST_FIRST(&ph->ph_itemlist);
    937 		if (__predict_false(v == NULL)) {
    938 			pr_leave(pp);
    939 			simple_unlock(&pp->pr_slock);
    940 			panic("pool_get: %s: page empty", pp->pr_wchan);
    941 		}
    942 #ifdef DIAGNOSTIC
    943 		if (__predict_false(pp->pr_nitems == 0)) {
    944 			pr_leave(pp);
    945 			simple_unlock(&pp->pr_slock);
    946 			printf("pool_get: %s: items on itemlist, nitems %u\n",
    947 			    pp->pr_wchan, pp->pr_nitems);
    948 			panic("pool_get: nitems inconsistent");
    949 		}
    950 #endif
    951 
    952 #ifdef POOL_DIAGNOSTIC
    953 		pr_log(pp, v, PRLOG_GET, file, line);
    954 #endif
    955 
    956 #ifdef DIAGNOSTIC
    957 		if (__predict_false(pi->pi_magic != PI_MAGIC)) {
    958 			pr_printlog(pp, pi, printf);
    959 			panic("pool_get(%s): free list modified: "
    960 			    "magic=%x; page %p; item addr %p\n",
    961 			    pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
    962 		}
    963 #endif
    964 
    965 		/*
    966 		 * Remove from item list.
    967 		 */
    968 		LIST_REMOVE(pi, pi_list);
    969 	}
    970 	pp->pr_nitems--;
    971 	pp->pr_nout++;
    972 	if (ph->ph_nmissing == 0) {
    973 #ifdef DIAGNOSTIC
    974 		if (__predict_false(pp->pr_nidle == 0))
    975 			panic("pool_get: nidle inconsistent");
    976 #endif
    977 		pp->pr_nidle--;
    978 
    979 		/*
    980 		 * This page was previously empty.  Move it to the list of
    981 		 * partially-full pages.  This page is already curpage.
    982 		 */
    983 		LIST_REMOVE(ph, ph_pagelist);
    984 		LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
    985 	}
    986 	ph->ph_nmissing++;
    987 	if (ph->ph_nmissing == pp->pr_itemsperpage) {
    988 #ifdef DIAGNOSTIC
    989 		if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 &&
    990 		    !LIST_EMPTY(&ph->ph_itemlist))) {
    991 			pr_leave(pp);
    992 			simple_unlock(&pp->pr_slock);
    993 			panic("pool_get: %s: nmissing inconsistent",
    994 			    pp->pr_wchan);
    995 		}
    996 #endif
    997 		/*
    998 		 * This page is now full.  Move it to the full list
    999 		 * and select a new current page.
   1000 		 */
   1001 		LIST_REMOVE(ph, ph_pagelist);
   1002 		LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
   1003 		pool_update_curpage(pp);
   1004 	}
   1005 
   1006 	pp->pr_nget++;
   1007 	pr_leave(pp);
   1008 
   1009 	/*
   1010 	 * If we have a low water mark and we are now below that low
   1011 	 * water mark, add more items to the pool.
   1012 	 */
   1013 	if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
   1014 		/*
   1015 		 * XXX: Should we log a warning?  Should we set up a timeout
   1016 		 * to try again in a second or so?  The latter could break
   1017 		 * a caller's assumptions about interrupt protection, etc.
   1018 		 */
   1019 	}
   1020 
   1021 	simple_unlock(&pp->pr_slock);
   1022 	return (v);
   1023 }
   1024 
   1025 /*
   1026  * Internal version of pool_put().  Pool is already locked/entered.
   1027  */
   1028 static void
   1029 pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq)
   1030 {
   1031 	struct pool_item *pi = v;
   1032 	struct pool_item_header *ph;
   1033 	caddr_t page;
   1034 	int s;
   1035 
   1036 	LOCK_ASSERT(simple_lock_held(&pp->pr_slock));
   1037 	SCHED_ASSERT_UNLOCKED();
   1038 
   1039 	page = (caddr_t)((u_long)v & pp->pr_alloc->pa_pagemask);
   1040 
   1041 #ifdef DIAGNOSTIC
   1042 	if (__predict_false(pp->pr_nout == 0)) {
   1043 		printf("pool %s: putting with none out\n",
   1044 		    pp->pr_wchan);
   1045 		panic("pool_put");
   1046 	}
   1047 #endif
   1048 
   1049 	if (__predict_false((ph = pr_find_pagehead(pp, page)) == NULL)) {
   1050 		pr_printlog(pp, NULL, printf);
   1051 		panic("pool_put: %s: page header missing", pp->pr_wchan);
   1052 	}
   1053 
   1054 #ifdef LOCKDEBUG
   1055 	/*
   1056 	 * Check if we're freeing a locked simple lock.
   1057 	 */
   1058 	simple_lock_freecheck((caddr_t)pi, ((caddr_t)pi) + pp->pr_size);
   1059 #endif
   1060 
   1061 	/*
   1062 	 * Return to item list.
   1063 	 */
   1064 	if (pp->pr_roflags & PR_NOTOUCH) {
   1065 		pr_item_notouch_put(pp, ph, v);
   1066 	} else {
   1067 #ifdef DIAGNOSTIC
   1068 		pi->pi_magic = PI_MAGIC;
   1069 #endif
   1070 #ifdef DEBUG
   1071 		{
   1072 			int i, *ip = v;
   1073 
   1074 			for (i = 0; i < pp->pr_size / sizeof(int); i++) {
   1075 				*ip++ = PI_MAGIC;
   1076 			}
   1077 		}
   1078 #endif
   1079 
   1080 		LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
   1081 	}
   1082 	KDASSERT(ph->ph_nmissing != 0);
   1083 	ph->ph_nmissing--;
   1084 	pp->pr_nput++;
   1085 	pp->pr_nitems++;
   1086 	pp->pr_nout--;
   1087 
   1088 	/* Cancel "pool empty" condition if it exists */
   1089 	if (pp->pr_curpage == NULL)
   1090 		pp->pr_curpage = ph;
   1091 
   1092 	if (pp->pr_flags & PR_WANTED) {
   1093 		pp->pr_flags &= ~PR_WANTED;
   1094 		if (ph->ph_nmissing == 0)
   1095 			pp->pr_nidle++;
   1096 		wakeup((caddr_t)pp);
   1097 		return;
   1098 	}
   1099 
   1100 	/*
   1101 	 * If this page is now empty, do one of two things:
   1102 	 *
   1103 	 *	(1) If we have more pages than the page high water mark,
   1104 	 *	    free the page back to the system.  ONLY CONSIDER
   1105 	 *	    FREEING BACK A PAGE IF WE HAVE MORE THAN OUR MINIMUM PAGE
   1106 	 *	    CLAIM.
   1107 	 *
   1108 	 *	(2) Otherwise, move the page to the empty page list.
   1109 	 *
   1110 	 * Either way, select a new current page (so we use a partially-full
   1111 	 * page if one is available).
   1112 	 */
   1113 	if (ph->ph_nmissing == 0) {
   1114 		pp->pr_nidle++;
   1115 		if (pp->pr_npages > pp->pr_minpages &&
   1116 		    (pp->pr_npages > pp->pr_maxpages ||
   1117 		     (pp->pr_alloc->pa_flags & PA_WANT) != 0)) {
   1118 			pr_rmpage(pp, ph, pq);
   1119 		} else {
   1120 			LIST_REMOVE(ph, ph_pagelist);
   1121 			LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
   1122 
   1123 			/*
   1124 			 * Update the timestamp on the page.  A page must
   1125 			 * be idle for some period of time before it can
   1126 			 * be reclaimed by the pagedaemon.  This minimizes
   1127 			 * ping-pong'ing for memory.
   1128 			 */
   1129 			s = splclock();
   1130 			ph->ph_time = mono_time;
   1131 			splx(s);
   1132 		}
   1133 		pool_update_curpage(pp);
   1134 	}
   1135 
   1136 	/*
   1137 	 * If the page was previously completely full, move it to the
   1138 	 * partially-full list and make it the current page.  The next
   1139 	 * allocation will get the item from this page, instead of
   1140 	 * further fragmenting the pool.
   1141 	 */
   1142 	else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
   1143 		LIST_REMOVE(ph, ph_pagelist);
   1144 		LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
   1145 		pp->pr_curpage = ph;
   1146 	}
   1147 }
   1148 
   1149 /*
   1150  * Return resource to the pool; must be called at appropriate spl level
   1151  */
   1152 #ifdef POOL_DIAGNOSTIC
   1153 void
   1154 _pool_put(struct pool *pp, void *v, const char *file, long line)
   1155 {
   1156 	struct pool_pagelist pq;
   1157 
   1158 	LIST_INIT(&pq);
   1159 
   1160 	simple_lock(&pp->pr_slock);
   1161 	pr_enter(pp, file, line);
   1162 
   1163 	pr_log(pp, v, PRLOG_PUT, file, line);
   1164 
   1165 	pool_do_put(pp, v, &pq);
   1166 
   1167 	pr_leave(pp);
   1168 	simple_unlock(&pp->pr_slock);
   1169 
   1170 	pr_pagelist_free(pp, &pq);
   1171 }
   1172 #undef pool_put
   1173 #endif /* POOL_DIAGNOSTIC */
   1174 
   1175 void
   1176 pool_put(struct pool *pp, void *v)
   1177 {
   1178 	struct pool_pagelist pq;
   1179 
   1180 	LIST_INIT(&pq);
   1181 
   1182 	simple_lock(&pp->pr_slock);
   1183 	pool_do_put(pp, v, &pq);
   1184 	simple_unlock(&pp->pr_slock);
   1185 
   1186 	pr_pagelist_free(pp, &pq);
   1187 }
   1188 
   1189 #ifdef POOL_DIAGNOSTIC
   1190 #define		pool_put(h, v)	_pool_put((h), (v), __FILE__, __LINE__)
   1191 #endif
   1192 
   1193 /*
   1194  * pool_grow: grow a pool by a page.
   1195  *
   1196  * => called with pool locked.
   1197  * => unlock and relock the pool.
   1198  * => return with pool locked.
   1199  */
   1200 
   1201 static int
   1202 pool_grow(struct pool *pp, int flags)
   1203 {
   1204 	struct pool_item_header *ph = NULL;
   1205 	char *cp;
   1206 
   1207 	simple_unlock(&pp->pr_slock);
   1208 	cp = pool_allocator_alloc(pp, flags);
   1209 	if (__predict_true(cp != NULL)) {
   1210 		ph = pool_alloc_item_header(pp, cp, flags);
   1211 	}
   1212 	if (__predict_false(cp == NULL || ph == NULL)) {
   1213 		if (cp != NULL) {
   1214 			pool_allocator_free(pp, cp);
   1215 		}
   1216 		simple_lock(&pp->pr_slock);
   1217 		return ENOMEM;
   1218 	}
   1219 
   1220 	simple_lock(&pp->pr_slock);
   1221 	pool_prime_page(pp, cp, ph);
   1222 	pp->pr_npagealloc++;
   1223 	return 0;
   1224 }
   1225 
   1226 /*
   1227  * Add N items to the pool.
   1228  */
   1229 int
   1230 pool_prime(struct pool *pp, int n)
   1231 {
   1232 	int newpages;
   1233 	int error = 0;
   1234 
   1235 	simple_lock(&pp->pr_slock);
   1236 
   1237 	newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
   1238 
   1239 	while (newpages-- > 0) {
   1240 		error = pool_grow(pp, PR_NOWAIT);
   1241 		if (error) {
   1242 			break;
   1243 		}
   1244 		pp->pr_minpages++;
   1245 	}
   1246 
   1247 	if (pp->pr_minpages >= pp->pr_maxpages)
   1248 		pp->pr_maxpages = pp->pr_minpages + 1;	/* XXX */
   1249 
   1250 	simple_unlock(&pp->pr_slock);
   1251 	return error;
   1252 }
   1253 
   1254 /*
   1255  * Add a page worth of items to the pool.
   1256  *
   1257  * Note, we must be called with the pool descriptor LOCKED.
   1258  */
   1259 static void
   1260 pool_prime_page(struct pool *pp, caddr_t storage, struct pool_item_header *ph)
   1261 {
   1262 	struct pool_item *pi;
   1263 	caddr_t cp = storage;
   1264 	unsigned int align = pp->pr_align;
   1265 	unsigned int ioff = pp->pr_itemoffset;
   1266 	int n;
   1267 	int s;
   1268 
   1269 	LOCK_ASSERT(simple_lock_held(&pp->pr_slock));
   1270 
   1271 #ifdef DIAGNOSTIC
   1272 	if (((u_long)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0)
   1273 		panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
   1274 #endif
   1275 
   1276 	/*
   1277 	 * Insert page header.
   1278 	 */
   1279 	LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
   1280 	LIST_INIT(&ph->ph_itemlist);
   1281 	ph->ph_page = storage;
   1282 	ph->ph_nmissing = 0;
   1283 	s = splclock();
   1284 	ph->ph_time = mono_time;
   1285 	splx(s);
   1286 	if ((pp->pr_roflags & PR_PHINPAGE) == 0)
   1287 		SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
   1288 
   1289 	pp->pr_nidle++;
   1290 
   1291 	/*
   1292 	 * Color this page.
   1293 	 */
   1294 	cp = (caddr_t)(cp + pp->pr_curcolor);
   1295 	if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
   1296 		pp->pr_curcolor = 0;
   1297 
   1298 	/*
   1299 	 * Adjust storage to apply aligment to `pr_itemoffset' in each item.
   1300 	 */
   1301 	if (ioff != 0)
   1302 		cp = (caddr_t)(cp + (align - ioff));
   1303 
   1304 	/*
   1305 	 * Insert remaining chunks on the bucket list.
   1306 	 */
   1307 	n = pp->pr_itemsperpage;
   1308 	pp->pr_nitems += n;
   1309 
   1310 	if (pp->pr_roflags & PR_NOTOUCH) {
   1311 		pool_item_freelist_t *freelist = PR_FREELIST(ph);
   1312 		int i;
   1313 
   1314 		ph->ph_off = cp - storage;
   1315 		ph->ph_firstfree = 0;
   1316 		for (i = 0; i < n - 1; i++)
   1317 			freelist[i] = i + 1;
   1318 		freelist[n - 1] = PR_INDEX_EOL;
   1319 	} else {
   1320 		while (n--) {
   1321 			pi = (struct pool_item *)cp;
   1322 
   1323 			KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
   1324 
   1325 			/* Insert on page list */
   1326 			LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
   1327 #ifdef DIAGNOSTIC
   1328 			pi->pi_magic = PI_MAGIC;
   1329 #endif
   1330 			cp = (caddr_t)(cp + pp->pr_size);
   1331 		}
   1332 	}
   1333 
   1334 	/*
   1335 	 * If the pool was depleted, point at the new page.
   1336 	 */
   1337 	if (pp->pr_curpage == NULL)
   1338 		pp->pr_curpage = ph;
   1339 
   1340 	if (++pp->pr_npages > pp->pr_hiwat)
   1341 		pp->pr_hiwat = pp->pr_npages;
   1342 }
   1343 
   1344 /*
   1345  * Used by pool_get() when nitems drops below the low water mark.  This
   1346  * is used to catch up pr_nitems with the low water mark.
   1347  *
   1348  * Note 1, we never wait for memory here, we let the caller decide what to do.
   1349  *
   1350  * Note 2, we must be called with the pool already locked, and we return
   1351  * with it locked.
   1352  */
   1353 static int
   1354 pool_catchup(struct pool *pp)
   1355 {
   1356 	int error = 0;
   1357 
   1358 	while (POOL_NEEDS_CATCHUP(pp)) {
   1359 		error = pool_grow(pp, PR_NOWAIT);
   1360 		if (error) {
   1361 			break;
   1362 		}
   1363 	}
   1364 	return error;
   1365 }
   1366 
   1367 static void
   1368 pool_update_curpage(struct pool *pp)
   1369 {
   1370 
   1371 	pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
   1372 	if (pp->pr_curpage == NULL) {
   1373 		pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
   1374 	}
   1375 }
   1376 
   1377 void
   1378 pool_setlowat(struct pool *pp, int n)
   1379 {
   1380 
   1381 	simple_lock(&pp->pr_slock);
   1382 
   1383 	pp->pr_minitems = n;
   1384 	pp->pr_minpages = (n == 0)
   1385 		? 0
   1386 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
   1387 
   1388 	/* Make sure we're caught up with the newly-set low water mark. */
   1389 	if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
   1390 		/*
   1391 		 * XXX: Should we log a warning?  Should we set up a timeout
   1392 		 * to try again in a second or so?  The latter could break
   1393 		 * a caller's assumptions about interrupt protection, etc.
   1394 		 */
   1395 	}
   1396 
   1397 	simple_unlock(&pp->pr_slock);
   1398 }
   1399 
   1400 void
   1401 pool_sethiwat(struct pool *pp, int n)
   1402 {
   1403 
   1404 	simple_lock(&pp->pr_slock);
   1405 
   1406 	pp->pr_maxpages = (n == 0)
   1407 		? 0
   1408 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
   1409 
   1410 	simple_unlock(&pp->pr_slock);
   1411 }
   1412 
   1413 void
   1414 pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
   1415 {
   1416 
   1417 	simple_lock(&pp->pr_slock);
   1418 
   1419 	pp->pr_hardlimit = n;
   1420 	pp->pr_hardlimit_warning = warnmess;
   1421 	pp->pr_hardlimit_ratecap.tv_sec = ratecap;
   1422 	pp->pr_hardlimit_warning_last.tv_sec = 0;
   1423 	pp->pr_hardlimit_warning_last.tv_usec = 0;
   1424 
   1425 	/*
   1426 	 * In-line version of pool_sethiwat(), because we don't want to
   1427 	 * release the lock.
   1428 	 */
   1429 	pp->pr_maxpages = (n == 0)
   1430 		? 0
   1431 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
   1432 
   1433 	simple_unlock(&pp->pr_slock);
   1434 }
   1435 
   1436 /*
   1437  * Release all complete pages that have not been used recently.
   1438  */
   1439 int
   1440 #ifdef POOL_DIAGNOSTIC
   1441 _pool_reclaim(struct pool *pp, const char *file, long line)
   1442 #else
   1443 pool_reclaim(struct pool *pp)
   1444 #endif
   1445 {
   1446 	struct pool_item_header *ph, *phnext;
   1447 	struct pool_cache *pc;
   1448 	struct pool_pagelist pq;
   1449 	struct pool_cache_grouplist pcgl;
   1450 	struct timeval curtime, diff;
   1451 	int s;
   1452 
   1453 	if (pp->pr_drain_hook != NULL) {
   1454 		/*
   1455 		 * The drain hook must be called with the pool unlocked.
   1456 		 */
   1457 		(*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT);
   1458 	}
   1459 
   1460 	if (simple_lock_try(&pp->pr_slock) == 0)
   1461 		return (0);
   1462 	pr_enter(pp, file, line);
   1463 
   1464 	LIST_INIT(&pq);
   1465 	LIST_INIT(&pcgl);
   1466 
   1467 	/*
   1468 	 * Reclaim items from the pool's caches.
   1469 	 */
   1470 	LIST_FOREACH(pc, &pp->pr_cachelist, pc_poollist)
   1471 		pool_cache_reclaim(pc, &pq, &pcgl);
   1472 
   1473 	s = splclock();
   1474 	curtime = mono_time;
   1475 	splx(s);
   1476 
   1477 	for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
   1478 		phnext = LIST_NEXT(ph, ph_pagelist);
   1479 
   1480 		/* Check our minimum page claim */
   1481 		if (pp->pr_npages <= pp->pr_minpages)
   1482 			break;
   1483 
   1484 		KASSERT(ph->ph_nmissing == 0);
   1485 		timersub(&curtime, &ph->ph_time, &diff);
   1486 		if (diff.tv_sec < pool_inactive_time)
   1487 			continue;
   1488 
   1489 		/*
   1490 		 * If freeing this page would put us below
   1491 		 * the low water mark, stop now.
   1492 		 */
   1493 		if ((pp->pr_nitems - pp->pr_itemsperpage) <
   1494 		    pp->pr_minitems)
   1495 			break;
   1496 
   1497 		pr_rmpage(pp, ph, &pq);
   1498 	}
   1499 
   1500 	pr_leave(pp);
   1501 	simple_unlock(&pp->pr_slock);
   1502 	if (LIST_EMPTY(&pq) && LIST_EMPTY(&pcgl))
   1503 		return 0;
   1504 
   1505 	pr_pagelist_free(pp, &pq);
   1506 	pcg_grouplist_free(&pcgl);
   1507 	return (1);
   1508 }
   1509 
   1510 /*
   1511  * Drain pools, one at a time.
   1512  *
   1513  * Note, we must never be called from an interrupt context.
   1514  */
   1515 void
   1516 pool_drain(void *arg)
   1517 {
   1518 	struct pool *pp;
   1519 	int s;
   1520 
   1521 	pp = NULL;
   1522 	s = splvm();
   1523 	simple_lock(&pool_head_slock);
   1524 	if (drainpp == NULL) {
   1525 		drainpp = LIST_FIRST(&pool_head);
   1526 	}
   1527 	if (drainpp) {
   1528 		pp = drainpp;
   1529 		drainpp = LIST_NEXT(pp, pr_poollist);
   1530 	}
   1531 	simple_unlock(&pool_head_slock);
   1532 	pool_reclaim(pp);
   1533 	splx(s);
   1534 }
   1535 
   1536 /*
   1537  * Diagnostic helpers.
   1538  */
   1539 void
   1540 pool_print(struct pool *pp, const char *modif)
   1541 {
   1542 	int s;
   1543 
   1544 	s = splvm();
   1545 	if (simple_lock_try(&pp->pr_slock) == 0) {
   1546 		printf("pool %s is locked; try again later\n",
   1547 		    pp->pr_wchan);
   1548 		splx(s);
   1549 		return;
   1550 	}
   1551 	pool_print1(pp, modif, printf);
   1552 	simple_unlock(&pp->pr_slock);
   1553 	splx(s);
   1554 }
   1555 
   1556 void
   1557 pool_printall(const char *modif, void (*pr)(const char *, ...))
   1558 {
   1559 	struct pool *pp;
   1560 
   1561 	if (simple_lock_try(&pool_head_slock) == 0) {
   1562 		(*pr)("WARNING: pool_head_slock is locked\n");
   1563 	} else {
   1564 		simple_unlock(&pool_head_slock);
   1565 	}
   1566 
   1567 	LIST_FOREACH(pp, &pool_head, pr_poollist) {
   1568 		pool_printit(pp, modif, pr);
   1569 	}
   1570 }
   1571 
   1572 void
   1573 pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
   1574 {
   1575 
   1576 	if (pp == NULL) {
   1577 		(*pr)("Must specify a pool to print.\n");
   1578 		return;
   1579 	}
   1580 
   1581 	/*
   1582 	 * Called from DDB; interrupts should be blocked, and all
   1583 	 * other processors should be paused.  We can skip locking
   1584 	 * the pool in this case.
   1585 	 *
   1586 	 * We do a simple_lock_try() just to print the lock
   1587 	 * status, however.
   1588 	 */
   1589 
   1590 	if (simple_lock_try(&pp->pr_slock) == 0)
   1591 		(*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
   1592 	else
   1593 		simple_unlock(&pp->pr_slock);
   1594 
   1595 	pool_print1(pp, modif, pr);
   1596 }
   1597 
   1598 static void
   1599 pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
   1600     void (*pr)(const char *, ...))
   1601 {
   1602 	struct pool_item_header *ph;
   1603 #ifdef DIAGNOSTIC
   1604 	struct pool_item *pi;
   1605 #endif
   1606 
   1607 	LIST_FOREACH(ph, pl, ph_pagelist) {
   1608 		(*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
   1609 		    ph->ph_page, ph->ph_nmissing,
   1610 		    (u_long)ph->ph_time.tv_sec,
   1611 		    (u_long)ph->ph_time.tv_usec);
   1612 #ifdef DIAGNOSTIC
   1613 		if (!(pp->pr_roflags & PR_NOTOUCH)) {
   1614 			LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
   1615 				if (pi->pi_magic != PI_MAGIC) {
   1616 					(*pr)("\t\t\titem %p, magic 0x%x\n",
   1617 					    pi, pi->pi_magic);
   1618 				}
   1619 			}
   1620 		}
   1621 #endif
   1622 	}
   1623 }
   1624 
   1625 static void
   1626 pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
   1627 {
   1628 	struct pool_item_header *ph;
   1629 	struct pool_cache *pc;
   1630 	struct pool_cache_group *pcg;
   1631 	int i, print_log = 0, print_pagelist = 0, print_cache = 0;
   1632 	char c;
   1633 
   1634 	while ((c = *modif++) != '\0') {
   1635 		if (c == 'l')
   1636 			print_log = 1;
   1637 		if (c == 'p')
   1638 			print_pagelist = 1;
   1639 		if (c == 'c')
   1640 			print_cache = 1;
   1641 	}
   1642 
   1643 	(*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
   1644 	    pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
   1645 	    pp->pr_roflags);
   1646 	(*pr)("\talloc %p\n", pp->pr_alloc);
   1647 	(*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
   1648 	    pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
   1649 	(*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
   1650 	    pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
   1651 
   1652 	(*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
   1653 	    pp->pr_nget, pp->pr_nfail, pp->pr_nput);
   1654 	(*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
   1655 	    pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
   1656 
   1657 	if (print_pagelist == 0)
   1658 		goto skip_pagelist;
   1659 
   1660 	if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
   1661 		(*pr)("\n\tempty page list:\n");
   1662 	pool_print_pagelist(pp, &pp->pr_emptypages, pr);
   1663 	if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
   1664 		(*pr)("\n\tfull page list:\n");
   1665 	pool_print_pagelist(pp, &pp->pr_fullpages, pr);
   1666 	if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
   1667 		(*pr)("\n\tpartial-page list:\n");
   1668 	pool_print_pagelist(pp, &pp->pr_partpages, pr);
   1669 
   1670 	if (pp->pr_curpage == NULL)
   1671 		(*pr)("\tno current page\n");
   1672 	else
   1673 		(*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
   1674 
   1675  skip_pagelist:
   1676 	if (print_log == 0)
   1677 		goto skip_log;
   1678 
   1679 	(*pr)("\n");
   1680 	if ((pp->pr_roflags & PR_LOGGING) == 0)
   1681 		(*pr)("\tno log\n");
   1682 	else
   1683 		pr_printlog(pp, NULL, pr);
   1684 
   1685  skip_log:
   1686 	if (print_cache == 0)
   1687 		goto skip_cache;
   1688 
   1689 #define PR_GROUPLIST(pcg)						\
   1690 	(*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail);		\
   1691 	for (i = 0; i < PCG_NOBJECTS; i++) {				\
   1692 		if (pcg->pcg_objects[i].pcgo_pa !=			\
   1693 		    POOL_PADDR_INVALID) {				\
   1694 			(*pr)("\t\t\t%p, 0x%llx\n",			\
   1695 			    pcg->pcg_objects[i].pcgo_va,		\
   1696 			    (unsigned long long)			\
   1697 			    pcg->pcg_objects[i].pcgo_pa);		\
   1698 		} else {						\
   1699 			(*pr)("\t\t\t%p\n",				\
   1700 			    pcg->pcg_objects[i].pcgo_va);		\
   1701 		}							\
   1702 	}
   1703 
   1704 	LIST_FOREACH(pc, &pp->pr_cachelist, pc_poollist) {
   1705 		(*pr)("\tcache %p\n", pc);
   1706 		(*pr)("\t    hits %lu misses %lu ngroups %lu nitems %lu\n",
   1707 		    pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems);
   1708 		(*pr)("\t    full groups:\n");
   1709 		LIST_FOREACH(pcg, &pc->pc_fullgroups, pcg_list) {
   1710 			PR_GROUPLIST(pcg);
   1711 		}
   1712 		(*pr)("\t    partial groups:\n");
   1713 		LIST_FOREACH(pcg, &pc->pc_partgroups, pcg_list) {
   1714 			PR_GROUPLIST(pcg);
   1715 		}
   1716 		(*pr)("\t    empty groups:\n");
   1717 		LIST_FOREACH(pcg, &pc->pc_emptygroups, pcg_list) {
   1718 			PR_GROUPLIST(pcg);
   1719 		}
   1720 	}
   1721 #undef PR_GROUPLIST
   1722 
   1723  skip_cache:
   1724 	pr_enter_check(pp, pr);
   1725 }
   1726 
   1727 static int
   1728 pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
   1729 {
   1730 	struct pool_item *pi;
   1731 	caddr_t page;
   1732 	int n;
   1733 
   1734 	page = (caddr_t)((u_long)ph & pp->pr_alloc->pa_pagemask);
   1735 	if (page != ph->ph_page &&
   1736 	    (pp->pr_roflags & PR_PHINPAGE) != 0) {
   1737 		if (label != NULL)
   1738 			printf("%s: ", label);
   1739 		printf("pool(%p:%s): page inconsistency: page %p;"
   1740 		       " at page head addr %p (p %p)\n", pp,
   1741 			pp->pr_wchan, ph->ph_page,
   1742 			ph, page);
   1743 		return 1;
   1744 	}
   1745 
   1746 	if ((pp->pr_roflags & PR_NOTOUCH) != 0)
   1747 		return 0;
   1748 
   1749 	for (pi = LIST_FIRST(&ph->ph_itemlist), n = 0;
   1750 	     pi != NULL;
   1751 	     pi = LIST_NEXT(pi,pi_list), n++) {
   1752 
   1753 #ifdef DIAGNOSTIC
   1754 		if (pi->pi_magic != PI_MAGIC) {
   1755 			if (label != NULL)
   1756 				printf("%s: ", label);
   1757 			printf("pool(%s): free list modified: magic=%x;"
   1758 			       " page %p; item ordinal %d;"
   1759 			       " addr %p (p %p)\n",
   1760 				pp->pr_wchan, pi->pi_magic, ph->ph_page,
   1761 				n, pi, page);
   1762 			panic("pool");
   1763 		}
   1764 #endif
   1765 		page =
   1766 		    (caddr_t)((u_long)pi & pp->pr_alloc->pa_pagemask);
   1767 		if (page == ph->ph_page)
   1768 			continue;
   1769 
   1770 		if (label != NULL)
   1771 			printf("%s: ", label);
   1772 		printf("pool(%p:%s): page inconsistency: page %p;"
   1773 		       " item ordinal %d; addr %p (p %p)\n", pp,
   1774 			pp->pr_wchan, ph->ph_page,
   1775 			n, pi, page);
   1776 		return 1;
   1777 	}
   1778 	return 0;
   1779 }
   1780 
   1781 
   1782 int
   1783 pool_chk(struct pool *pp, const char *label)
   1784 {
   1785 	struct pool_item_header *ph;
   1786 	int r = 0;
   1787 
   1788 	simple_lock(&pp->pr_slock);
   1789 	LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
   1790 		r = pool_chk_page(pp, label, ph);
   1791 		if (r) {
   1792 			goto out;
   1793 		}
   1794 	}
   1795 	LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
   1796 		r = pool_chk_page(pp, label, ph);
   1797 		if (r) {
   1798 			goto out;
   1799 		}
   1800 	}
   1801 	LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
   1802 		r = pool_chk_page(pp, label, ph);
   1803 		if (r) {
   1804 			goto out;
   1805 		}
   1806 	}
   1807 
   1808 out:
   1809 	simple_unlock(&pp->pr_slock);
   1810 	return (r);
   1811 }
   1812 
   1813 /*
   1814  * pool_cache_init:
   1815  *
   1816  *	Initialize a pool cache.
   1817  *
   1818  *	NOTE: If the pool must be protected from interrupts, we expect
   1819  *	to be called at the appropriate interrupt priority level.
   1820  */
   1821 void
   1822 pool_cache_init(struct pool_cache *pc, struct pool *pp,
   1823     int (*ctor)(void *, void *, int),
   1824     void (*dtor)(void *, void *),
   1825     void *arg)
   1826 {
   1827 
   1828 	LIST_INIT(&pc->pc_emptygroups);
   1829 	LIST_INIT(&pc->pc_fullgroups);
   1830 	LIST_INIT(&pc->pc_partgroups);
   1831 	simple_lock_init(&pc->pc_slock);
   1832 
   1833 	pc->pc_pool = pp;
   1834 
   1835 	pc->pc_ctor = ctor;
   1836 	pc->pc_dtor = dtor;
   1837 	pc->pc_arg  = arg;
   1838 
   1839 	pc->pc_hits   = 0;
   1840 	pc->pc_misses = 0;
   1841 
   1842 	pc->pc_ngroups = 0;
   1843 
   1844 	pc->pc_nitems = 0;
   1845 
   1846 	simple_lock(&pp->pr_slock);
   1847 	LIST_INSERT_HEAD(&pp->pr_cachelist, pc, pc_poollist);
   1848 	simple_unlock(&pp->pr_slock);
   1849 }
   1850 
   1851 /*
   1852  * pool_cache_destroy:
   1853  *
   1854  *	Destroy a pool cache.
   1855  */
   1856 void
   1857 pool_cache_destroy(struct pool_cache *pc)
   1858 {
   1859 	struct pool *pp = pc->pc_pool;
   1860 
   1861 	/* First, invalidate the entire cache. */
   1862 	pool_cache_invalidate(pc);
   1863 
   1864 	/* ...and remove it from the pool's cache list. */
   1865 	simple_lock(&pp->pr_slock);
   1866 	LIST_REMOVE(pc, pc_poollist);
   1867 	simple_unlock(&pp->pr_slock);
   1868 }
   1869 
   1870 static inline void *
   1871 pcg_get(struct pool_cache_group *pcg, paddr_t *pap)
   1872 {
   1873 	void *object;
   1874 	u_int idx;
   1875 
   1876 	KASSERT(pcg->pcg_avail <= PCG_NOBJECTS);
   1877 	KASSERT(pcg->pcg_avail != 0);
   1878 	idx = --pcg->pcg_avail;
   1879 
   1880 	KASSERT(pcg->pcg_objects[idx].pcgo_va != NULL);
   1881 	object = pcg->pcg_objects[idx].pcgo_va;
   1882 	if (pap != NULL)
   1883 		*pap = pcg->pcg_objects[idx].pcgo_pa;
   1884 	pcg->pcg_objects[idx].pcgo_va = NULL;
   1885 
   1886 	return (object);
   1887 }
   1888 
   1889 static inline void
   1890 pcg_put(struct pool_cache_group *pcg, void *object, paddr_t pa)
   1891 {
   1892 	u_int idx;
   1893 
   1894 	KASSERT(pcg->pcg_avail < PCG_NOBJECTS);
   1895 	idx = pcg->pcg_avail++;
   1896 
   1897 	KASSERT(pcg->pcg_objects[idx].pcgo_va == NULL);
   1898 	pcg->pcg_objects[idx].pcgo_va = object;
   1899 	pcg->pcg_objects[idx].pcgo_pa = pa;
   1900 }
   1901 
   1902 static void
   1903 pcg_grouplist_free(struct pool_cache_grouplist *pcgl)
   1904 {
   1905 	struct pool_cache_group *pcg;
   1906 	int s;
   1907 
   1908 	s = splvm();
   1909 	while ((pcg = LIST_FIRST(pcgl)) != NULL) {
   1910 		LIST_REMOVE(pcg, pcg_list);
   1911 		pool_put(&pcgpool, pcg);
   1912 	}
   1913 	splx(s);
   1914 }
   1915 
   1916 /*
   1917  * pool_cache_get{,_paddr}:
   1918  *
   1919  *	Get an object from a pool cache (optionally returning
   1920  *	the physical address of the object).
   1921  */
   1922 void *
   1923 pool_cache_get_paddr(struct pool_cache *pc, int flags, paddr_t *pap)
   1924 {
   1925 	struct pool_cache_group *pcg;
   1926 	void *object;
   1927 
   1928 #ifdef LOCKDEBUG
   1929 	if (flags & PR_WAITOK)
   1930 		simple_lock_only_held(NULL, "pool_cache_get(PR_WAITOK)");
   1931 #endif
   1932 
   1933 	simple_lock(&pc->pc_slock);
   1934 
   1935 	pcg = LIST_FIRST(&pc->pc_partgroups);
   1936 	if (pcg == NULL) {
   1937 		pcg = LIST_FIRST(&pc->pc_fullgroups);
   1938 		if (pcg != NULL) {
   1939 			LIST_REMOVE(pcg, pcg_list);
   1940 			LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
   1941 		}
   1942 	}
   1943 	if (pcg == NULL) {
   1944 
   1945 		/*
   1946 		 * No groups with any available objects.  Allocate
   1947 		 * a new object, construct it, and return it to
   1948 		 * the caller.  We will allocate a group, if necessary,
   1949 		 * when the object is freed back to the cache.
   1950 		 */
   1951 		pc->pc_misses++;
   1952 		simple_unlock(&pc->pc_slock);
   1953 		object = pool_get(pc->pc_pool, flags);
   1954 		if (object != NULL && pc->pc_ctor != NULL) {
   1955 			if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) {
   1956 				pool_put(pc->pc_pool, object);
   1957 				return (NULL);
   1958 			}
   1959 		}
   1960 		if (object != NULL && pap != NULL) {
   1961 #ifdef POOL_VTOPHYS
   1962 			*pap = POOL_VTOPHYS(object);
   1963 #else
   1964 			*pap = POOL_PADDR_INVALID;
   1965 #endif
   1966 		}
   1967 		return (object);
   1968 	}
   1969 
   1970 	pc->pc_hits++;
   1971 	pc->pc_nitems--;
   1972 	object = pcg_get(pcg, pap);
   1973 
   1974 	if (pcg->pcg_avail == 0) {
   1975 		LIST_REMOVE(pcg, pcg_list);
   1976 		LIST_INSERT_HEAD(&pc->pc_emptygroups, pcg, pcg_list);
   1977 	}
   1978 	simple_unlock(&pc->pc_slock);
   1979 
   1980 	return (object);
   1981 }
   1982 
   1983 /*
   1984  * pool_cache_put{,_paddr}:
   1985  *
   1986  *	Put an object back to the pool cache (optionally caching the
   1987  *	physical address of the object).
   1988  */
   1989 void
   1990 pool_cache_put_paddr(struct pool_cache *pc, void *object, paddr_t pa)
   1991 {
   1992 	struct pool_cache_group *pcg;
   1993 	int s;
   1994 
   1995 	if (__predict_false((pc->pc_pool->pr_flags & PR_WANTED) != 0)) {
   1996 		goto destruct;
   1997 	}
   1998 
   1999 	simple_lock(&pc->pc_slock);
   2000 
   2001 	pcg = LIST_FIRST(&pc->pc_partgroups);
   2002 	if (pcg == NULL) {
   2003 		pcg = LIST_FIRST(&pc->pc_emptygroups);
   2004 		if (pcg != NULL) {
   2005 			LIST_REMOVE(pcg, pcg_list);
   2006 			LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
   2007 		}
   2008 	}
   2009 	if (pcg == NULL) {
   2010 
   2011 		/*
   2012 		 * No empty groups to free the object to.  Attempt to
   2013 		 * allocate one.
   2014 		 */
   2015 		simple_unlock(&pc->pc_slock);
   2016 		s = splvm();
   2017 		pcg = pool_get(&pcgpool, PR_NOWAIT);
   2018 		splx(s);
   2019 		if (pcg == NULL) {
   2020 destruct:
   2021 
   2022 			/*
   2023 			 * Unable to allocate a cache group; destruct the object
   2024 			 * and free it back to the pool.
   2025 			 */
   2026 			pool_cache_destruct_object(pc, object);
   2027 			return;
   2028 		}
   2029 		memset(pcg, 0, sizeof(*pcg));
   2030 		simple_lock(&pc->pc_slock);
   2031 		pc->pc_ngroups++;
   2032 		LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
   2033 	}
   2034 
   2035 	pc->pc_nitems++;
   2036 	pcg_put(pcg, object, pa);
   2037 
   2038 	if (pcg->pcg_avail == PCG_NOBJECTS) {
   2039 		LIST_REMOVE(pcg, pcg_list);
   2040 		LIST_INSERT_HEAD(&pc->pc_fullgroups, pcg, pcg_list);
   2041 	}
   2042 	simple_unlock(&pc->pc_slock);
   2043 }
   2044 
   2045 /*
   2046  * pool_cache_destruct_object:
   2047  *
   2048  *	Force destruction of an object and its release back into
   2049  *	the pool.
   2050  */
   2051 void
   2052 pool_cache_destruct_object(struct pool_cache *pc, void *object)
   2053 {
   2054 
   2055 	if (pc->pc_dtor != NULL)
   2056 		(*pc->pc_dtor)(pc->pc_arg, object);
   2057 	pool_put(pc->pc_pool, object);
   2058 }
   2059 
   2060 static void
   2061 pool_do_cache_invalidate_grouplist(struct pool_cache_grouplist *pcgsl,
   2062     struct pool_cache *pc, struct pool_pagelist *pq,
   2063     struct pool_cache_grouplist *pcgdl)
   2064 {
   2065 	struct pool_cache_group *pcg, *npcg;
   2066 	void *object;
   2067 
   2068 	for (pcg = LIST_FIRST(pcgsl); pcg != NULL; pcg = npcg) {
   2069 		npcg = LIST_NEXT(pcg, pcg_list);
   2070 		while (pcg->pcg_avail != 0) {
   2071 			pc->pc_nitems--;
   2072 			object = pcg_get(pcg, NULL);
   2073 			if (pc->pc_dtor != NULL)
   2074 				(*pc->pc_dtor)(pc->pc_arg, object);
   2075 			pool_do_put(pc->pc_pool, object, pq);
   2076 		}
   2077 		pc->pc_ngroups--;
   2078 		LIST_REMOVE(pcg, pcg_list);
   2079 		LIST_INSERT_HEAD(pcgdl, pcg, pcg_list);
   2080 	}
   2081 }
   2082 
   2083 static void
   2084 pool_do_cache_invalidate(struct pool_cache *pc, struct pool_pagelist *pq,
   2085     struct pool_cache_grouplist *pcgl)
   2086 {
   2087 
   2088 	LOCK_ASSERT(simple_lock_held(&pc->pc_slock));
   2089 	LOCK_ASSERT(simple_lock_held(&pc->pc_pool->pr_slock));
   2090 
   2091 	pool_do_cache_invalidate_grouplist(&pc->pc_fullgroups, pc, pq, pcgl);
   2092 	pool_do_cache_invalidate_grouplist(&pc->pc_partgroups, pc, pq, pcgl);
   2093 
   2094 	KASSERT(LIST_EMPTY(&pc->pc_partgroups));
   2095 	KASSERT(LIST_EMPTY(&pc->pc_fullgroups));
   2096 	KASSERT(pc->pc_nitems == 0);
   2097 }
   2098 
   2099 /*
   2100  * pool_cache_invalidate:
   2101  *
   2102  *	Invalidate a pool cache (destruct and release all of the
   2103  *	cached objects).
   2104  */
   2105 void
   2106 pool_cache_invalidate(struct pool_cache *pc)
   2107 {
   2108 	struct pool_pagelist pq;
   2109 	struct pool_cache_grouplist pcgl;
   2110 
   2111 	LIST_INIT(&pq);
   2112 	LIST_INIT(&pcgl);
   2113 
   2114 	simple_lock(&pc->pc_slock);
   2115 	simple_lock(&pc->pc_pool->pr_slock);
   2116 
   2117 	pool_do_cache_invalidate(pc, &pq, &pcgl);
   2118 
   2119 	simple_unlock(&pc->pc_pool->pr_slock);
   2120 	simple_unlock(&pc->pc_slock);
   2121 
   2122 	pr_pagelist_free(pc->pc_pool, &pq);
   2123 	pcg_grouplist_free(&pcgl);
   2124 }
   2125 
   2126 /*
   2127  * pool_cache_reclaim:
   2128  *
   2129  *	Reclaim a pool cache for pool_reclaim().
   2130  */
   2131 static void
   2132 pool_cache_reclaim(struct pool_cache *pc, struct pool_pagelist *pq,
   2133     struct pool_cache_grouplist *pcgl)
   2134 {
   2135 
   2136 	/*
   2137 	 * We're locking in the wrong order (normally pool_cache -> pool,
   2138 	 * but the pool is already locked when we get here), so we have
   2139 	 * to use trylock.  If we can't lock the pool_cache, it's not really
   2140 	 * a big deal here.
   2141 	 */
   2142 	if (simple_lock_try(&pc->pc_slock) == 0)
   2143 		return;
   2144 
   2145 	pool_do_cache_invalidate(pc, pq, pcgl);
   2146 
   2147 	simple_unlock(&pc->pc_slock);
   2148 }
   2149 
   2150 /*
   2151  * Pool backend allocators.
   2152  *
   2153  * Each pool has a backend allocator that handles allocation, deallocation,
   2154  * and any additional draining that might be needed.
   2155  *
   2156  * We provide two standard allocators:
   2157  *
   2158  *	pool_allocator_kmem - the default when no allocator is specified
   2159  *
   2160  *	pool_allocator_nointr - used for pools that will not be accessed
   2161  *	in interrupt context.
   2162  */
   2163 void	*pool_page_alloc(struct pool *, int);
   2164 void	pool_page_free(struct pool *, void *);
   2165 
   2166 #ifdef POOL_SUBPAGE
   2167 struct pool_allocator pool_allocator_kmem_fullpage = {
   2168 	pool_page_alloc, pool_page_free, 0,
   2169 };
   2170 #else
   2171 struct pool_allocator pool_allocator_kmem = {
   2172 	pool_page_alloc, pool_page_free, 0,
   2173 };
   2174 #endif
   2175 
   2176 void	*pool_page_alloc_nointr(struct pool *, int);
   2177 void	pool_page_free_nointr(struct pool *, void *);
   2178 
   2179 #ifdef POOL_SUBPAGE
   2180 struct pool_allocator pool_allocator_nointr_fullpage = {
   2181 	pool_page_alloc_nointr, pool_page_free_nointr, 0,
   2182 };
   2183 #else
   2184 struct pool_allocator pool_allocator_nointr = {
   2185 	pool_page_alloc_nointr, pool_page_free_nointr, 0,
   2186 };
   2187 #endif
   2188 
   2189 #ifdef POOL_SUBPAGE
   2190 void	*pool_subpage_alloc(struct pool *, int);
   2191 void	pool_subpage_free(struct pool *, void *);
   2192 
   2193 struct pool_allocator pool_allocator_kmem = {
   2194 	pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
   2195 };
   2196 
   2197 void	*pool_subpage_alloc_nointr(struct pool *, int);
   2198 void	pool_subpage_free_nointr(struct pool *, void *);
   2199 
   2200 struct pool_allocator pool_allocator_nointr = {
   2201 	pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
   2202 };
   2203 #endif /* POOL_SUBPAGE */
   2204 
   2205 /*
   2206  * We have at least three different resources for the same allocation and
   2207  * each resource can be depleted.  First, we have the ready elements in the
   2208  * pool.  Then we have the resource (typically a vm_map) for this allocator.
   2209  * Finally, we have physical memory.  Waiting for any of these can be
   2210  * unnecessary when any other is freed, but the kernel doesn't support
   2211  * sleeping on multiple wait channels, so we have to employ another strategy.
   2212  *
   2213  * The caller sleeps on the pool (so that it can be awakened when an item
   2214  * is returned to the pool), but we set PA_WANT on the allocator.  When a
   2215  * page is returned to the allocator and PA_WANT is set, pool_allocator_free
   2216  * will wake up all sleeping pools belonging to this allocator.
   2217  *
   2218  * XXX Thundering herd.
   2219  */
   2220 void *
   2221 pool_allocator_alloc(struct pool *org, int flags)
   2222 {
   2223 	struct pool_allocator *pa = org->pr_alloc;
   2224 	struct pool *pp, *start;
   2225 	int s, freed;
   2226 	void *res;
   2227 
   2228 	LOCK_ASSERT(!simple_lock_held(&org->pr_slock));
   2229 
   2230 	do {
   2231 		if ((res = (*pa->pa_alloc)(org, flags)) != NULL)
   2232 			return (res);
   2233 		if ((flags & PR_WAITOK) == 0) {
   2234 			/*
   2235 			 * We only run the drain hookhere if PR_NOWAIT.
   2236 			 * In other cases, the hook will be run in
   2237 			 * pool_reclaim().
   2238 			 */
   2239 			if (org->pr_drain_hook != NULL) {
   2240 				(*org->pr_drain_hook)(org->pr_drain_hook_arg,
   2241 				    flags);
   2242 				if ((res = (*pa->pa_alloc)(org, flags)) != NULL)
   2243 					return (res);
   2244 			}
   2245 			break;
   2246 		}
   2247 
   2248 		/*
   2249 		 * Drain all pools, that use this allocator.
   2250 		 * We do this to reclaim VA space.
   2251 		 * pa_alloc is responsible for waiting for
   2252 		 * physical memory.
   2253 		 *
   2254 		 * XXX We risk looping forever if start if someone
   2255 		 * calls pool_destroy on "start".  But there is no
   2256 		 * other way to have potentially sleeping pool_reclaim,
   2257 		 * non-sleeping locks on pool_allocator, and some
   2258 		 * stirring of drained pools in the allocator.
   2259 		 *
   2260 		 * XXX Maybe we should use pool_head_slock for locking
   2261 		 * the allocators?
   2262 		 */
   2263 		freed = 0;
   2264 
   2265 		s = splvm();
   2266 		simple_lock(&pa->pa_slock);
   2267 		pp = start = TAILQ_FIRST(&pa->pa_list);
   2268 		do {
   2269 			TAILQ_REMOVE(&pa->pa_list, pp, pr_alloc_list);
   2270 			TAILQ_INSERT_TAIL(&pa->pa_list, pp, pr_alloc_list);
   2271 			simple_unlock(&pa->pa_slock);
   2272 			freed = pool_reclaim(pp);
   2273 			simple_lock(&pa->pa_slock);
   2274 		} while ((pp = TAILQ_FIRST(&pa->pa_list)) != start &&
   2275 			 freed == 0);
   2276 
   2277 		if (freed == 0) {
   2278 			/*
   2279 			 * We set PA_WANT here, the caller will most likely
   2280 			 * sleep waiting for pages (if not, this won't hurt
   2281 			 * that much), and there is no way to set this in
   2282 			 * the caller without violating locking order.
   2283 			 */
   2284 			pa->pa_flags |= PA_WANT;
   2285 		}
   2286 		simple_unlock(&pa->pa_slock);
   2287 		splx(s);
   2288 	} while (freed);
   2289 	return (NULL);
   2290 }
   2291 
   2292 void
   2293 pool_allocator_free(struct pool *pp, void *v)
   2294 {
   2295 	struct pool_allocator *pa = pp->pr_alloc;
   2296 	int s;
   2297 
   2298 	LOCK_ASSERT(!simple_lock_held(&pp->pr_slock));
   2299 
   2300 	(*pa->pa_free)(pp, v);
   2301 
   2302 	s = splvm();
   2303 	simple_lock(&pa->pa_slock);
   2304 	if ((pa->pa_flags & PA_WANT) == 0) {
   2305 		simple_unlock(&pa->pa_slock);
   2306 		splx(s);
   2307 		return;
   2308 	}
   2309 
   2310 	TAILQ_FOREACH(pp, &pa->pa_list, pr_alloc_list) {
   2311 		simple_lock(&pp->pr_slock);
   2312 		if ((pp->pr_flags & PR_WANTED) != 0) {
   2313 			pp->pr_flags &= ~PR_WANTED;
   2314 			wakeup(pp);
   2315 		}
   2316 		simple_unlock(&pp->pr_slock);
   2317 	}
   2318 	pa->pa_flags &= ~PA_WANT;
   2319 	simple_unlock(&pa->pa_slock);
   2320 	splx(s);
   2321 }
   2322 
   2323 void *
   2324 pool_page_alloc(struct pool *pp, int flags)
   2325 {
   2326 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
   2327 
   2328 	return ((void *) uvm_km_alloc_poolpage_cache(kmem_map, waitok));
   2329 }
   2330 
   2331 void
   2332 pool_page_free(struct pool *pp, void *v)
   2333 {
   2334 
   2335 	uvm_km_free_poolpage_cache(kmem_map, (vaddr_t) v);
   2336 }
   2337 
   2338 static void *
   2339 pool_page_alloc_meta(struct pool *pp, int flags)
   2340 {
   2341 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
   2342 
   2343 	return ((void *) uvm_km_alloc_poolpage(kmem_map, waitok));
   2344 }
   2345 
   2346 static void
   2347 pool_page_free_meta(struct pool *pp, void *v)
   2348 {
   2349 
   2350 	uvm_km_free_poolpage(kmem_map, (vaddr_t) v);
   2351 }
   2352 
   2353 #ifdef POOL_SUBPAGE
   2354 /* Sub-page allocator, for machines with large hardware pages. */
   2355 void *
   2356 pool_subpage_alloc(struct pool *pp, int flags)
   2357 {
   2358 	void *v;
   2359 	int s;
   2360 	s = splvm();
   2361 	v = pool_get(&psppool, flags);
   2362 	splx(s);
   2363 	return v;
   2364 }
   2365 
   2366 void
   2367 pool_subpage_free(struct pool *pp, void *v)
   2368 {
   2369 	int s;
   2370 	s = splvm();
   2371 	pool_put(&psppool, v);
   2372 	splx(s);
   2373 }
   2374 
   2375 /* We don't provide a real nointr allocator.  Maybe later. */
   2376 void *
   2377 pool_subpage_alloc_nointr(struct pool *pp, int flags)
   2378 {
   2379 
   2380 	return (pool_subpage_alloc(pp, flags));
   2381 }
   2382 
   2383 void
   2384 pool_subpage_free_nointr(struct pool *pp, void *v)
   2385 {
   2386 
   2387 	pool_subpage_free(pp, v);
   2388 }
   2389 #endif /* POOL_SUBPAGE */
   2390 void *
   2391 pool_page_alloc_nointr(struct pool *pp, int flags)
   2392 {
   2393 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
   2394 
   2395 	return ((void *) uvm_km_alloc_poolpage_cache(kernel_map, waitok));
   2396 }
   2397 
   2398 void
   2399 pool_page_free_nointr(struct pool *pp, void *v)
   2400 {
   2401 
   2402 	uvm_km_free_poolpage_cache(kernel_map, (vaddr_t) v);
   2403 }
   2404