Home | History | Annotate | Line # | Download | only in kern
subr_pool.c revision 1.42
      1 /*	$NetBSD: subr_pool.c,v 1.42 2000/12/06 18:20:52 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1999 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 "opt_pool.h"
     41 #include "opt_poollog.h"
     42 #include "opt_lockdebug.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/proc.h>
     47 #include <sys/errno.h>
     48 #include <sys/kernel.h>
     49 #include <sys/malloc.h>
     50 #include <sys/lock.h>
     51 #include <sys/pool.h>
     52 #include <sys/syslog.h>
     53 
     54 #include <uvm/uvm.h>
     55 
     56 /*
     57  * Pool resource management utility.
     58  *
     59  * Memory is allocated in pages which are split into pieces according
     60  * to the pool item size. Each page is kept on a list headed by `pr_pagelist'
     61  * in the pool structure and the individual pool items are on a linked list
     62  * headed by `ph_itemlist' in each page header. The memory for building
     63  * the page list is either taken from the allocated pages themselves (for
     64  * small pool items) or taken from an internal pool of page headers (`phpool').
     65  */
     66 
     67 /* List of all pools */
     68 TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
     69 
     70 /* Private pool for page header structures */
     71 static struct pool phpool;
     72 
     73 /* # of seconds to retain page after last use */
     74 int pool_inactive_time = 10;
     75 
     76 /* Next candidate for drainage (see pool_drain()) */
     77 static struct pool	*drainpp;
     78 
     79 /* This spin lock protects both pool_head and drainpp. */
     80 struct simplelock pool_head_slock = SIMPLELOCK_INITIALIZER;
     81 
     82 struct pool_item_header {
     83 	/* Page headers */
     84 	TAILQ_ENTRY(pool_item_header)
     85 				ph_pagelist;	/* pool page list */
     86 	TAILQ_HEAD(,pool_item)	ph_itemlist;	/* chunk list for this page */
     87 	LIST_ENTRY(pool_item_header)
     88 				ph_hashlist;	/* Off-page page headers */
     89 	int			ph_nmissing;	/* # of chunks in use */
     90 	caddr_t			ph_page;	/* this page's address */
     91 	struct timeval		ph_time;	/* last referenced */
     92 };
     93 
     94 struct pool_item {
     95 #ifdef DIAGNOSTIC
     96 	int pi_magic;
     97 #endif
     98 #define	PI_MAGIC 0xdeadbeef
     99 	/* Other entries use only this list entry */
    100 	TAILQ_ENTRY(pool_item)	pi_list;
    101 };
    102 
    103 
    104 #define	PR_HASH_INDEX(pp,addr) \
    105 	(((u_long)(addr) >> (pp)->pr_pageshift) & (PR_HASHTABSIZE - 1))
    106 
    107 
    108 
    109 static int	pool_catchup(struct pool *);
    110 static void	pool_prime_page(struct pool *, caddr_t);
    111 static void	*pool_page_alloc(unsigned long, int, int);
    112 static void	pool_page_free(void *, unsigned long, int);
    113 
    114 static void pool_print1(struct pool *, const char *,
    115 	void (*)(const char *, ...));
    116 
    117 /*
    118  * Pool log entry. An array of these is allocated in pool_create().
    119  */
    120 struct pool_log {
    121 	const char	*pl_file;
    122 	long		pl_line;
    123 	int		pl_action;
    124 #define	PRLOG_GET	1
    125 #define	PRLOG_PUT	2
    126 	void		*pl_addr;
    127 };
    128 
    129 /* Number of entries in pool log buffers */
    130 #ifndef POOL_LOGSIZE
    131 #define	POOL_LOGSIZE	10
    132 #endif
    133 
    134 int pool_logsize = POOL_LOGSIZE;
    135 
    136 #ifdef DIAGNOSTIC
    137 static __inline void
    138 pr_log(struct pool *pp, void *v, int action, const char *file, long line)
    139 {
    140 	int n = pp->pr_curlogentry;
    141 	struct pool_log *pl;
    142 
    143 	if ((pp->pr_roflags & PR_LOGGING) == 0)
    144 		return;
    145 
    146 	/*
    147 	 * Fill in the current entry. Wrap around and overwrite
    148 	 * the oldest entry if necessary.
    149 	 */
    150 	pl = &pp->pr_log[n];
    151 	pl->pl_file = file;
    152 	pl->pl_line = line;
    153 	pl->pl_action = action;
    154 	pl->pl_addr = v;
    155 	if (++n >= pp->pr_logsize)
    156 		n = 0;
    157 	pp->pr_curlogentry = n;
    158 }
    159 
    160 static void
    161 pr_printlog(struct pool *pp, struct pool_item *pi,
    162     void (*pr)(const char *, ...))
    163 {
    164 	int i = pp->pr_logsize;
    165 	int n = pp->pr_curlogentry;
    166 
    167 	if ((pp->pr_roflags & PR_LOGGING) == 0)
    168 		return;
    169 
    170 	/*
    171 	 * Print all entries in this pool's log.
    172 	 */
    173 	while (i-- > 0) {
    174 		struct pool_log *pl = &pp->pr_log[n];
    175 		if (pl->pl_action != 0) {
    176 			if (pi == NULL || pi == pl->pl_addr) {
    177 				(*pr)("\tlog entry %d:\n", i);
    178 				(*pr)("\t\taction = %s, addr = %p\n",
    179 				    pl->pl_action == PRLOG_GET ? "get" : "put",
    180 				    pl->pl_addr);
    181 				(*pr)("\t\tfile: %s at line %lu\n",
    182 				    pl->pl_file, pl->pl_line);
    183 			}
    184 		}
    185 		if (++n >= pp->pr_logsize)
    186 			n = 0;
    187 	}
    188 }
    189 
    190 static __inline void
    191 pr_enter(struct pool *pp, const char *file, long line)
    192 {
    193 
    194 	if (__predict_false(pp->pr_entered_file != NULL)) {
    195 		printf("pool %s: reentrancy at file %s line %ld\n",
    196 		    pp->pr_wchan, file, line);
    197 		printf("         previous entry at file %s line %ld\n",
    198 		    pp->pr_entered_file, pp->pr_entered_line);
    199 		panic("pr_enter");
    200 	}
    201 
    202 	pp->pr_entered_file = file;
    203 	pp->pr_entered_line = line;
    204 }
    205 
    206 static __inline void
    207 pr_leave(struct pool *pp)
    208 {
    209 
    210 	if (__predict_false(pp->pr_entered_file == NULL)) {
    211 		printf("pool %s not entered?\n", pp->pr_wchan);
    212 		panic("pr_leave");
    213 	}
    214 
    215 	pp->pr_entered_file = NULL;
    216 	pp->pr_entered_line = 0;
    217 }
    218 
    219 static __inline void
    220 pr_enter_check(struct pool *pp, void (*pr)(const char *, ...))
    221 {
    222 
    223 	if (pp->pr_entered_file != NULL)
    224 		(*pr)("\n\tcurrently entered from file %s line %ld\n",
    225 		    pp->pr_entered_file, pp->pr_entered_line);
    226 }
    227 #else
    228 #define	pr_log(pp, v, action, file, line)
    229 #define	pr_printlog(pp, pi, pr)
    230 #define	pr_enter(pp, file, line)
    231 #define	pr_leave(pp)
    232 #define	pr_enter_check(pp, pr)
    233 #endif /* DIAGNOSTIC */
    234 
    235 /*
    236  * Return the pool page header based on page address.
    237  */
    238 static __inline struct pool_item_header *
    239 pr_find_pagehead(struct pool *pp, caddr_t page)
    240 {
    241 	struct pool_item_header *ph;
    242 
    243 	if ((pp->pr_roflags & PR_PHINPAGE) != 0)
    244 		return ((struct pool_item_header *)(page + pp->pr_phoffset));
    245 
    246 	for (ph = LIST_FIRST(&pp->pr_hashtab[PR_HASH_INDEX(pp, page)]);
    247 	     ph != NULL;
    248 	     ph = LIST_NEXT(ph, ph_hashlist)) {
    249 		if (ph->ph_page == page)
    250 			return (ph);
    251 	}
    252 	return (NULL);
    253 }
    254 
    255 /*
    256  * Remove a page from the pool.
    257  */
    258 static __inline void
    259 pr_rmpage(struct pool *pp, struct pool_item_header *ph)
    260 {
    261 
    262 	/*
    263 	 * If the page was idle, decrement the idle page count.
    264 	 */
    265 	if (ph->ph_nmissing == 0) {
    266 #ifdef DIAGNOSTIC
    267 		if (pp->pr_nidle == 0)
    268 			panic("pr_rmpage: nidle inconsistent");
    269 		if (pp->pr_nitems < pp->pr_itemsperpage)
    270 			panic("pr_rmpage: nitems inconsistent");
    271 #endif
    272 		pp->pr_nidle--;
    273 	}
    274 
    275 	pp->pr_nitems -= pp->pr_itemsperpage;
    276 
    277 	/*
    278 	 * Unlink a page from the pool and release it.
    279 	 */
    280 	TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
    281 	(*pp->pr_free)(ph->ph_page, pp->pr_pagesz, pp->pr_mtype);
    282 	pp->pr_npages--;
    283 	pp->pr_npagefree++;
    284 
    285 	if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
    286 		int s;
    287 		LIST_REMOVE(ph, ph_hashlist);
    288 		s = splhigh();
    289 		pool_put(&phpool, ph);
    290 		splx(s);
    291 	}
    292 
    293 	if (pp->pr_curpage == ph) {
    294 		/*
    295 		 * Find a new non-empty page header, if any.
    296 		 * Start search from the page head, to increase the
    297 		 * chance for "high water" pages to be freed.
    298 		 */
    299 		for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
    300 		     ph = TAILQ_NEXT(ph, ph_pagelist))
    301 			if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
    302 				break;
    303 
    304 		pp->pr_curpage = ph;
    305 	}
    306 }
    307 
    308 /*
    309  * Allocate and initialize a pool.
    310  */
    311 struct pool *
    312 pool_create(size_t size, u_int align, u_int ioff, int nitems,
    313     const char *wchan, size_t pagesz,
    314     void *(*alloc)(unsigned long, int, int),
    315     void (*release)(void *, unsigned long, int),
    316     int mtype)
    317 {
    318 	struct pool *pp;
    319 	int flags;
    320 
    321 	pp = (struct pool *)malloc(sizeof(*pp), M_POOL, M_NOWAIT);
    322 	if (pp == NULL)
    323 		return (NULL);
    324 
    325 	flags = PR_FREEHEADER;
    326 	pool_init(pp, size, align, ioff, flags, wchan, pagesz,
    327 		  alloc, release, mtype);
    328 
    329 	if (nitems != 0) {
    330 		if (pool_prime(pp, nitems, NULL) != 0) {
    331 			pool_destroy(pp);
    332 			return (NULL);
    333 		}
    334 	}
    335 
    336 	return (pp);
    337 }
    338 
    339 /*
    340  * Initialize the given pool resource structure.
    341  *
    342  * We export this routine to allow other kernel parts to declare
    343  * static pools that must be initialized before malloc() is available.
    344  */
    345 void
    346 pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
    347     const char *wchan, size_t pagesz,
    348     void *(*alloc)(unsigned long, int, int),
    349     void (*release)(void *, unsigned long, int),
    350     int mtype)
    351 {
    352 	int off, slack, i;
    353 
    354 #ifdef POOL_DIAGNOSTIC
    355 	/*
    356 	 * Always log if POOL_DIAGNOSTIC is defined.
    357 	 */
    358 	if (pool_logsize != 0)
    359 		flags |= PR_LOGGING;
    360 #endif
    361 
    362 	/*
    363 	 * Check arguments and construct default values.
    364 	 */
    365 	if (!powerof2(pagesz))
    366 		panic("pool_init: page size invalid (%lx)\n", (u_long)pagesz);
    367 
    368 	if (alloc == NULL && release == NULL) {
    369 		alloc = pool_page_alloc;
    370 		release = pool_page_free;
    371 		pagesz = PAGE_SIZE;	/* Rounds to PAGE_SIZE anyhow. */
    372 	} else if ((alloc != NULL && release != NULL) == 0) {
    373 		/* If you specifiy one, must specify both. */
    374 		panic("pool_init: must specify alloc and release together");
    375 	}
    376 
    377 	if (pagesz == 0)
    378 		pagesz = PAGE_SIZE;
    379 
    380 	if (align == 0)
    381 		align = ALIGN(1);
    382 
    383 	if (size < sizeof(struct pool_item))
    384 		size = sizeof(struct pool_item);
    385 
    386 	size = ALIGN(size);
    387 	if (size >= pagesz)
    388 		panic("pool_init: pool item size (%lu) too large",
    389 		      (u_long)size);
    390 
    391 	/*
    392 	 * Initialize the pool structure.
    393 	 */
    394 	TAILQ_INIT(&pp->pr_pagelist);
    395 	pp->pr_curpage = NULL;
    396 	pp->pr_npages = 0;
    397 	pp->pr_minitems = 0;
    398 	pp->pr_minpages = 0;
    399 	pp->pr_maxpages = UINT_MAX;
    400 	pp->pr_roflags = flags;
    401 	pp->pr_flags = 0;
    402 	pp->pr_size = size;
    403 	pp->pr_align = align;
    404 	pp->pr_wchan = wchan;
    405 	pp->pr_mtype = mtype;
    406 	pp->pr_alloc = alloc;
    407 	pp->pr_free = release;
    408 	pp->pr_pagesz = pagesz;
    409 	pp->pr_pagemask = ~(pagesz - 1);
    410 	pp->pr_pageshift = ffs(pagesz) - 1;
    411 	pp->pr_nitems = 0;
    412 	pp->pr_nout = 0;
    413 	pp->pr_hardlimit = UINT_MAX;
    414 	pp->pr_hardlimit_warning = NULL;
    415 	pp->pr_hardlimit_ratecap.tv_sec = 0;
    416 	pp->pr_hardlimit_ratecap.tv_usec = 0;
    417 	pp->pr_hardlimit_warning_last.tv_sec = 0;
    418 	pp->pr_hardlimit_warning_last.tv_usec = 0;
    419 
    420 	/*
    421 	 * Decide whether to put the page header off page to avoid
    422 	 * wasting too large a part of the page. Off-page page headers
    423 	 * go on a hash table, so we can match a returned item
    424 	 * with its header based on the page address.
    425 	 * We use 1/16 of the page size as the threshold (XXX: tune)
    426 	 */
    427 	if (pp->pr_size < pagesz/16) {
    428 		/* Use the end of the page for the page header */
    429 		pp->pr_roflags |= PR_PHINPAGE;
    430 		pp->pr_phoffset = off =
    431 			pagesz - ALIGN(sizeof(struct pool_item_header));
    432 	} else {
    433 		/* The page header will be taken from our page header pool */
    434 		pp->pr_phoffset = 0;
    435 		off = pagesz;
    436 		for (i = 0; i < PR_HASHTABSIZE; i++) {
    437 			LIST_INIT(&pp->pr_hashtab[i]);
    438 		}
    439 	}
    440 
    441 	/*
    442 	 * Alignment is to take place at `ioff' within the item. This means
    443 	 * we must reserve up to `align - 1' bytes on the page to allow
    444 	 * appropriate positioning of each item.
    445 	 *
    446 	 * Silently enforce `0 <= ioff < align'.
    447 	 */
    448 	pp->pr_itemoffset = ioff = ioff % align;
    449 	pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
    450 
    451 	/*
    452 	 * Use the slack between the chunks and the page header
    453 	 * for "cache coloring".
    454 	 */
    455 	slack = off - pp->pr_itemsperpage * pp->pr_size;
    456 	pp->pr_maxcolor = (slack / align) * align;
    457 	pp->pr_curcolor = 0;
    458 
    459 	pp->pr_nget = 0;
    460 	pp->pr_nfail = 0;
    461 	pp->pr_nput = 0;
    462 	pp->pr_npagealloc = 0;
    463 	pp->pr_npagefree = 0;
    464 	pp->pr_hiwat = 0;
    465 	pp->pr_nidle = 0;
    466 
    467 	if (flags & PR_LOGGING) {
    468 		if (kmem_map == NULL ||
    469 		    (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
    470 		     M_TEMP, M_NOWAIT)) == NULL)
    471 			pp->pr_roflags &= ~PR_LOGGING;
    472 		pp->pr_curlogentry = 0;
    473 		pp->pr_logsize = pool_logsize;
    474 	}
    475 
    476 	pp->pr_entered_file = NULL;
    477 	pp->pr_entered_line = 0;
    478 
    479 	simple_lock_init(&pp->pr_slock);
    480 
    481 	/*
    482 	 * Initialize private page header pool if we haven't done so yet.
    483 	 * XXX LOCKING.
    484 	 */
    485 	if (phpool.pr_size == 0) {
    486 		pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
    487 			  0, "phpool", 0, 0, 0, 0);
    488 	}
    489 
    490 	/* Insert into the list of all pools. */
    491 	simple_lock(&pool_head_slock);
    492 	TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
    493 	simple_unlock(&pool_head_slock);
    494 }
    495 
    496 /*
    497  * De-commision a pool resource.
    498  */
    499 void
    500 pool_destroy(struct pool *pp)
    501 {
    502 	struct pool_item_header *ph;
    503 
    504 #ifdef DIAGNOSTIC
    505 	if (pp->pr_nout != 0) {
    506 		pr_printlog(pp, NULL, printf);
    507 		panic("pool_destroy: pool busy: still out: %u\n",
    508 		    pp->pr_nout);
    509 	}
    510 #endif
    511 
    512 	/* Remove all pages */
    513 	if ((pp->pr_roflags & PR_STATIC) == 0)
    514 		while ((ph = pp->pr_pagelist.tqh_first) != NULL)
    515 			pr_rmpage(pp, ph);
    516 
    517 	/* Remove from global pool list */
    518 	simple_lock(&pool_head_slock);
    519 	TAILQ_REMOVE(&pool_head, pp, pr_poollist);
    520 	/* XXX Only clear this if we were drainpp? */
    521 	drainpp = NULL;
    522 	simple_unlock(&pool_head_slock);
    523 
    524 	if ((pp->pr_roflags & PR_LOGGING) != 0)
    525 		free(pp->pr_log, M_TEMP);
    526 
    527 	if (pp->pr_roflags & PR_FREEHEADER)
    528 		free(pp, M_POOL);
    529 }
    530 
    531 
    532 /*
    533  * Grab an item from the pool; must be called at appropriate spl level
    534  */
    535 void *
    536 _pool_get(struct pool *pp, int flags, const char *file, long line)
    537 {
    538 	void *v;
    539 	struct pool_item *pi;
    540 	struct pool_item_header *ph;
    541 
    542 #ifdef DIAGNOSTIC
    543 	if (__predict_false((pp->pr_roflags & PR_STATIC) &&
    544 			    (flags & PR_MALLOCOK))) {
    545 		pr_printlog(pp, NULL, printf);
    546 		panic("pool_get: static");
    547 	}
    548 #endif
    549 
    550 	if (__predict_false(curproc == NULL && doing_shutdown == 0 &&
    551 			    (flags & PR_WAITOK) != 0))
    552 		panic("pool_get: must have NOWAIT");
    553 
    554 	simple_lock(&pp->pr_slock);
    555 	pr_enter(pp, file, line);
    556 
    557  startover:
    558 	/*
    559 	 * Check to see if we've reached the hard limit.  If we have,
    560 	 * and we can wait, then wait until an item has been returned to
    561 	 * the pool.
    562 	 */
    563 #ifdef DIAGNOSTIC
    564 	if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
    565 		pr_leave(pp);
    566 		simple_unlock(&pp->pr_slock);
    567 		panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
    568 	}
    569 #endif
    570 	if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
    571 		if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
    572 			/*
    573 			 * XXX: A warning isn't logged in this case.  Should
    574 			 * it be?
    575 			 */
    576 			pp->pr_flags |= PR_WANTED;
    577 			pr_leave(pp);
    578 			ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
    579 			pr_enter(pp, file, line);
    580 			goto startover;
    581 		}
    582 
    583 		/*
    584 		 * Log a message that the hard limit has been hit.
    585 		 */
    586 		if (pp->pr_hardlimit_warning != NULL &&
    587 		    ratecheck(&pp->pr_hardlimit_warning_last,
    588 			      &pp->pr_hardlimit_ratecap))
    589 			log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
    590 
    591 		if (flags & PR_URGENT)
    592 			panic("pool_get: urgent");
    593 
    594 		pp->pr_nfail++;
    595 
    596 		pr_leave(pp);
    597 		simple_unlock(&pp->pr_slock);
    598 		return (NULL);
    599 	}
    600 
    601 	/*
    602 	 * The convention we use is that if `curpage' is not NULL, then
    603 	 * it points at a non-empty bucket. In particular, `curpage'
    604 	 * never points at a page header which has PR_PHINPAGE set and
    605 	 * has no items in its bucket.
    606 	 */
    607 	if ((ph = pp->pr_curpage) == NULL) {
    608 		void *v;
    609 
    610 #ifdef DIAGNOSTIC
    611 		if (pp->pr_nitems != 0) {
    612 			simple_unlock(&pp->pr_slock);
    613 			printf("pool_get: %s: curpage NULL, nitems %u\n",
    614 			    pp->pr_wchan, pp->pr_nitems);
    615 			panic("pool_get: nitems inconsistent\n");
    616 		}
    617 #endif
    618 
    619 		/*
    620 		 * Call the back-end page allocator for more memory.
    621 		 * Release the pool lock, as the back-end page allocator
    622 		 * may block.
    623 		 */
    624 		pr_leave(pp);
    625 		simple_unlock(&pp->pr_slock);
    626 		v = (*pp->pr_alloc)(pp->pr_pagesz, flags, pp->pr_mtype);
    627 		simple_lock(&pp->pr_slock);
    628 		pr_enter(pp, file, line);
    629 
    630 		if (v == NULL) {
    631 			/*
    632 			 * We were unable to allocate a page, but
    633 			 * we released the lock during allocation,
    634 			 * so perhaps items were freed back to the
    635 			 * pool.  Check for this case.
    636 			 */
    637 			if (pp->pr_curpage != NULL)
    638 				goto startover;
    639 
    640 			if (flags & PR_URGENT)
    641 				panic("pool_get: urgent");
    642 
    643 			if ((flags & PR_WAITOK) == 0) {
    644 				pp->pr_nfail++;
    645 				pr_leave(pp);
    646 				simple_unlock(&pp->pr_slock);
    647 				return (NULL);
    648 			}
    649 
    650 			/*
    651 			 * Wait for items to be returned to this pool.
    652 			 *
    653 			 * XXX: we actually want to wait just until
    654 			 * the page allocator has memory again. Depending
    655 			 * on this pool's usage, we might get stuck here
    656 			 * for a long time.
    657 			 *
    658 			 * XXX: maybe we should wake up once a second and
    659 			 * try again?
    660 			 */
    661 			pp->pr_flags |= PR_WANTED;
    662 			pr_leave(pp);
    663 			ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
    664 			pr_enter(pp, file, line);
    665 			goto startover;
    666 		}
    667 
    668 		/* We have more memory; add it to the pool */
    669 		pp->pr_npagealloc++;
    670 		pool_prime_page(pp, v);
    671 
    672 		/* Start the allocation process over. */
    673 		goto startover;
    674 	}
    675 
    676 	if (__predict_false((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)) {
    677 		pr_leave(pp);
    678 		simple_unlock(&pp->pr_slock);
    679 		panic("pool_get: %s: page empty", pp->pr_wchan);
    680 	}
    681 #ifdef DIAGNOSTIC
    682 	if (__predict_false(pp->pr_nitems == 0)) {
    683 		pr_leave(pp);
    684 		simple_unlock(&pp->pr_slock);
    685 		printf("pool_get: %s: items on itemlist, nitems %u\n",
    686 		    pp->pr_wchan, pp->pr_nitems);
    687 		panic("pool_get: nitems inconsistent\n");
    688 	}
    689 #endif
    690 	pr_log(pp, v, PRLOG_GET, file, line);
    691 
    692 #ifdef DIAGNOSTIC
    693 	if (__predict_false(pi->pi_magic != PI_MAGIC)) {
    694 		pr_printlog(pp, pi, printf);
    695 		panic("pool_get(%s): free list modified: magic=%x; page %p;"
    696 		       " item addr %p\n",
    697 			pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
    698 	}
    699 #endif
    700 
    701 	/*
    702 	 * Remove from item list.
    703 	 */
    704 	TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
    705 	pp->pr_nitems--;
    706 	pp->pr_nout++;
    707 	if (ph->ph_nmissing == 0) {
    708 #ifdef DIAGNOSTIC
    709 		if (__predict_false(pp->pr_nidle == 0))
    710 			panic("pool_get: nidle inconsistent");
    711 #endif
    712 		pp->pr_nidle--;
    713 	}
    714 	ph->ph_nmissing++;
    715 	if (TAILQ_FIRST(&ph->ph_itemlist) == NULL) {
    716 #ifdef DIAGNOSTIC
    717 		if (__predict_false(ph->ph_nmissing != pp->pr_itemsperpage)) {
    718 			pr_leave(pp);
    719 			simple_unlock(&pp->pr_slock);
    720 			panic("pool_get: %s: nmissing inconsistent",
    721 			    pp->pr_wchan);
    722 		}
    723 #endif
    724 		/*
    725 		 * Find a new non-empty page header, if any.
    726 		 * Start search from the page head, to increase
    727 		 * the chance for "high water" pages to be freed.
    728 		 *
    729 		 * Migrate empty pages to the end of the list.  This
    730 		 * will speed the update of curpage as pages become
    731 		 * idle.  Empty pages intermingled with idle pages
    732 		 * is no big deal.  As soon as a page becomes un-empty,
    733 		 * it will move back to the head of the list.
    734 		 */
    735 		TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
    736 		TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
    737 		for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
    738 		     ph = TAILQ_NEXT(ph, ph_pagelist))
    739 			if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
    740 				break;
    741 
    742 		pp->pr_curpage = ph;
    743 	}
    744 
    745 	pp->pr_nget++;
    746 
    747 	/*
    748 	 * If we have a low water mark and we are now below that low
    749 	 * water mark, add more items to the pool.
    750 	 */
    751 	if (pp->pr_nitems < pp->pr_minitems && pool_catchup(pp) != 0) {
    752 		/*
    753 		 * XXX: Should we log a warning?  Should we set up a timeout
    754 		 * to try again in a second or so?  The latter could break
    755 		 * a caller's assumptions about interrupt protection, etc.
    756 		 */
    757 	}
    758 
    759 	pr_leave(pp);
    760 	simple_unlock(&pp->pr_slock);
    761 	return (v);
    762 }
    763 
    764 /*
    765  * Return resource to the pool; must be called at appropriate spl level
    766  */
    767 void
    768 _pool_put(struct pool *pp, void *v, const char *file, long line)
    769 {
    770 	struct pool_item *pi = v;
    771 	struct pool_item_header *ph;
    772 	caddr_t page;
    773 	int s;
    774 
    775 	page = (caddr_t)((u_long)v & pp->pr_pagemask);
    776 
    777 	simple_lock(&pp->pr_slock);
    778 	pr_enter(pp, file, line);
    779 
    780 #ifdef DIAGNOSTIC
    781 	if (__predict_false(pp->pr_nout == 0)) {
    782 		printf("pool %s: putting with none out\n",
    783 		    pp->pr_wchan);
    784 		panic("pool_put");
    785 	}
    786 #endif
    787 
    788 	pr_log(pp, v, PRLOG_PUT, file, line);
    789 
    790 	if (__predict_false((ph = pr_find_pagehead(pp, page)) == NULL)) {
    791 		pr_printlog(pp, NULL, printf);
    792 		panic("pool_put: %s: page header missing", pp->pr_wchan);
    793 	}
    794 
    795 #ifdef LOCKDEBUG
    796 	/*
    797 	 * Check if we're freeing a locked simple lock.
    798 	 */
    799 	simple_lock_freecheck((caddr_t)pi, ((caddr_t)pi) + pp->pr_size);
    800 #endif
    801 
    802 	/*
    803 	 * Return to item list.
    804 	 */
    805 #ifdef DIAGNOSTIC
    806 	pi->pi_magic = PI_MAGIC;
    807 #endif
    808 #ifdef DEBUG
    809 	{
    810 		int i, *ip = v;
    811 
    812 		for (i = 0; i < pp->pr_size / sizeof(int); i++) {
    813 			*ip++ = PI_MAGIC;
    814 		}
    815 	}
    816 #endif
    817 
    818 	TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
    819 	ph->ph_nmissing--;
    820 	pp->pr_nput++;
    821 	pp->pr_nitems++;
    822 	pp->pr_nout--;
    823 
    824 	/* Cancel "pool empty" condition if it exists */
    825 	if (pp->pr_curpage == NULL)
    826 		pp->pr_curpage = ph;
    827 
    828 	if (pp->pr_flags & PR_WANTED) {
    829 		pp->pr_flags &= ~PR_WANTED;
    830 		if (ph->ph_nmissing == 0)
    831 			pp->pr_nidle++;
    832 		pr_leave(pp);
    833 		simple_unlock(&pp->pr_slock);
    834 		wakeup((caddr_t)pp);
    835 		return;
    836 	}
    837 
    838 	/*
    839 	 * If this page is now complete, do one of two things:
    840 	 *
    841 	 *	(1) If we have more pages than the page high water
    842 	 *	    mark, free the page back to the system.
    843 	 *
    844 	 *	(2) Move it to the end of the page list, so that
    845 	 *	    we minimize our chances of fragmenting the
    846 	 *	    pool.  Idle pages migrate to the end (along with
    847 	 *	    completely empty pages, so that we find un-empty
    848 	 *	    pages more quickly when we update curpage) of the
    849 	 *	    list so they can be more easily swept up by
    850 	 *	    the pagedaemon when pages are scarce.
    851 	 */
    852 	if (ph->ph_nmissing == 0) {
    853 		pp->pr_nidle++;
    854 		if (pp->pr_npages > pp->pr_maxpages) {
    855 			pr_rmpage(pp, ph);
    856 		} else {
    857 			TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
    858 			TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
    859 
    860 			/*
    861 			 * Update the timestamp on the page.  A page must
    862 			 * be idle for some period of time before it can
    863 			 * be reclaimed by the pagedaemon.  This minimizes
    864 			 * ping-pong'ing for memory.
    865 			 */
    866 			s = splclock();
    867 			ph->ph_time = mono_time;
    868 			splx(s);
    869 
    870 			/*
    871 			 * Update the current page pointer.  Just look for
    872 			 * the first page with any free items.
    873 			 *
    874 			 * XXX: Maybe we want an option to look for the
    875 			 * page with the fewest available items, to minimize
    876 			 * fragmentation?
    877 			 */
    878 			for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
    879 			     ph = TAILQ_NEXT(ph, ph_pagelist))
    880 				if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
    881 					break;
    882 
    883 			pp->pr_curpage = ph;
    884 		}
    885 	}
    886 	/*
    887 	 * If the page has just become un-empty, move it to the head of
    888 	 * the list, and make it the current page.  The next allocation
    889 	 * will get the item from this page, instead of further fragmenting
    890 	 * the pool.
    891 	 */
    892 	else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
    893 		TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
    894 		TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
    895 		pp->pr_curpage = ph;
    896 	}
    897 
    898 	pr_leave(pp);
    899 	simple_unlock(&pp->pr_slock);
    900 
    901 }
    902 
    903 /*
    904  * Add N items to the pool.
    905  */
    906 int
    907 pool_prime(struct pool *pp, int n, caddr_t storage)
    908 {
    909 	caddr_t cp;
    910 	int newnitems, newpages;
    911 
    912 #ifdef DIAGNOSTIC
    913 	if (__predict_false(storage && !(pp->pr_roflags & PR_STATIC)))
    914 		panic("pool_prime: static");
    915 	/* !storage && static caught below */
    916 #endif
    917 
    918 	simple_lock(&pp->pr_slock);
    919 
    920 	newnitems = pp->pr_minitems + n;
    921 	newpages =
    922 		roundup(newnitems, pp->pr_itemsperpage) / pp->pr_itemsperpage
    923 		- pp->pr_minpages;
    924 
    925 	while (newpages-- > 0) {
    926 		if (pp->pr_roflags & PR_STATIC) {
    927 			cp = storage;
    928 			storage += pp->pr_pagesz;
    929 		} else {
    930 			simple_unlock(&pp->pr_slock);
    931 			cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
    932 			simple_lock(&pp->pr_slock);
    933 		}
    934 
    935 		if (cp == NULL) {
    936 			simple_unlock(&pp->pr_slock);
    937 			return (ENOMEM);
    938 		}
    939 
    940 		pp->pr_npagealloc++;
    941 		pool_prime_page(pp, cp);
    942 		pp->pr_minpages++;
    943 	}
    944 
    945 	pp->pr_minitems = newnitems;
    946 
    947 	if (pp->pr_minpages >= pp->pr_maxpages)
    948 		pp->pr_maxpages = pp->pr_minpages + 1;	/* XXX */
    949 
    950 	simple_unlock(&pp->pr_slock);
    951 	return (0);
    952 }
    953 
    954 /*
    955  * Add a page worth of items to the pool.
    956  *
    957  * Note, we must be called with the pool descriptor LOCKED.
    958  */
    959 static void
    960 pool_prime_page(struct pool *pp, caddr_t storage)
    961 {
    962 	struct pool_item *pi;
    963 	struct pool_item_header *ph;
    964 	caddr_t cp = storage;
    965 	unsigned int align = pp->pr_align;
    966 	unsigned int ioff = pp->pr_itemoffset;
    967 	int s, n;
    968 
    969 	if (((u_long)cp & (pp->pr_pagesz - 1)) != 0)
    970 		panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
    971 
    972 	if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
    973 		ph = (struct pool_item_header *)(cp + pp->pr_phoffset);
    974 	} else {
    975 		s = splhigh();
    976 		ph = pool_get(&phpool, PR_URGENT);
    977 		splx(s);
    978 		LIST_INSERT_HEAD(&pp->pr_hashtab[PR_HASH_INDEX(pp, cp)],
    979 				 ph, ph_hashlist);
    980 	}
    981 
    982 	/*
    983 	 * Insert page header.
    984 	 */
    985 	TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
    986 	TAILQ_INIT(&ph->ph_itemlist);
    987 	ph->ph_page = storage;
    988 	ph->ph_nmissing = 0;
    989 	memset(&ph->ph_time, 0, sizeof(ph->ph_time));
    990 
    991 	pp->pr_nidle++;
    992 
    993 	/*
    994 	 * Color this page.
    995 	 */
    996 	cp = (caddr_t)(cp + pp->pr_curcolor);
    997 	if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
    998 		pp->pr_curcolor = 0;
    999 
   1000 	/*
   1001 	 * Adjust storage to apply aligment to `pr_itemoffset' in each item.
   1002 	 */
   1003 	if (ioff != 0)
   1004 		cp = (caddr_t)(cp + (align - ioff));
   1005 
   1006 	/*
   1007 	 * Insert remaining chunks on the bucket list.
   1008 	 */
   1009 	n = pp->pr_itemsperpage;
   1010 	pp->pr_nitems += n;
   1011 
   1012 	while (n--) {
   1013 		pi = (struct pool_item *)cp;
   1014 
   1015 		/* Insert on page list */
   1016 		TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
   1017 #ifdef DIAGNOSTIC
   1018 		pi->pi_magic = PI_MAGIC;
   1019 #endif
   1020 		cp = (caddr_t)(cp + pp->pr_size);
   1021 	}
   1022 
   1023 	/*
   1024 	 * If the pool was depleted, point at the new page.
   1025 	 */
   1026 	if (pp->pr_curpage == NULL)
   1027 		pp->pr_curpage = ph;
   1028 
   1029 	if (++pp->pr_npages > pp->pr_hiwat)
   1030 		pp->pr_hiwat = pp->pr_npages;
   1031 }
   1032 
   1033 /*
   1034  * Like pool_prime(), except this is used by pool_get() when nitems
   1035  * drops below the low water mark.  This is used to catch up nitmes
   1036  * with the low water mark.
   1037  *
   1038  * Note 1, we never wait for memory here, we let the caller decide what to do.
   1039  *
   1040  * Note 2, this doesn't work with static pools.
   1041  *
   1042  * Note 3, we must be called with the pool already locked, and we return
   1043  * with it locked.
   1044  */
   1045 static int
   1046 pool_catchup(struct pool *pp)
   1047 {
   1048 	caddr_t cp;
   1049 	int error = 0;
   1050 
   1051 	if (pp->pr_roflags & PR_STATIC) {
   1052 		/*
   1053 		 * We dropped below the low water mark, and this is not a
   1054 		 * good thing.  Log a warning.
   1055 		 *
   1056 		 * XXX: rate-limit this?
   1057 		 */
   1058 		printf("WARNING: static pool `%s' dropped below low water "
   1059 		    "mark\n", pp->pr_wchan);
   1060 		return (0);
   1061 	}
   1062 
   1063 	while (pp->pr_nitems < pp->pr_minitems) {
   1064 		/*
   1065 		 * Call the page back-end allocator for more memory.
   1066 		 *
   1067 		 * XXX: We never wait, so should we bother unlocking
   1068 		 * the pool descriptor?
   1069 		 */
   1070 		simple_unlock(&pp->pr_slock);
   1071 		cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
   1072 		simple_lock(&pp->pr_slock);
   1073 		if (__predict_false(cp == NULL)) {
   1074 			error = ENOMEM;
   1075 			break;
   1076 		}
   1077 		pp->pr_npagealloc++;
   1078 		pool_prime_page(pp, cp);
   1079 	}
   1080 
   1081 	return (error);
   1082 }
   1083 
   1084 void
   1085 pool_setlowat(struct pool *pp, int n)
   1086 {
   1087 	int error;
   1088 
   1089 	simple_lock(&pp->pr_slock);
   1090 
   1091 	pp->pr_minitems = n;
   1092 	pp->pr_minpages = (n == 0)
   1093 		? 0
   1094 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
   1095 
   1096 	/* Make sure we're caught up with the newly-set low water mark. */
   1097 	if ((pp->pr_nitems < pp->pr_minitems) &&
   1098 	    (error = pool_catchup(pp)) != 0) {
   1099 		/*
   1100 		 * XXX: Should we log a warning?  Should we set up a timeout
   1101 		 * to try again in a second or so?  The latter could break
   1102 		 * a caller's assumptions about interrupt protection, etc.
   1103 		 */
   1104 	}
   1105 
   1106 	simple_unlock(&pp->pr_slock);
   1107 }
   1108 
   1109 void
   1110 pool_sethiwat(struct pool *pp, int n)
   1111 {
   1112 
   1113 	simple_lock(&pp->pr_slock);
   1114 
   1115 	pp->pr_maxpages = (n == 0)
   1116 		? 0
   1117 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
   1118 
   1119 	simple_unlock(&pp->pr_slock);
   1120 }
   1121 
   1122 void
   1123 pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
   1124 {
   1125 
   1126 	simple_lock(&pp->pr_slock);
   1127 
   1128 	pp->pr_hardlimit = n;
   1129 	pp->pr_hardlimit_warning = warnmess;
   1130 	pp->pr_hardlimit_ratecap.tv_sec = ratecap;
   1131 	pp->pr_hardlimit_warning_last.tv_sec = 0;
   1132 	pp->pr_hardlimit_warning_last.tv_usec = 0;
   1133 
   1134 	/*
   1135 	 * In-line version of pool_sethiwat(), because we don't want to
   1136 	 * release the lock.
   1137 	 */
   1138 	pp->pr_maxpages = (n == 0)
   1139 		? 0
   1140 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
   1141 
   1142 	simple_unlock(&pp->pr_slock);
   1143 }
   1144 
   1145 /*
   1146  * Default page allocator.
   1147  */
   1148 static void *
   1149 pool_page_alloc(unsigned long sz, int flags, int mtype)
   1150 {
   1151 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
   1152 
   1153 	return ((void *)uvm_km_alloc_poolpage(waitok));
   1154 }
   1155 
   1156 static void
   1157 pool_page_free(void *v, unsigned long sz, int mtype)
   1158 {
   1159 
   1160 	uvm_km_free_poolpage((vaddr_t)v);
   1161 }
   1162 
   1163 /*
   1164  * Alternate pool page allocator for pools that know they will
   1165  * never be accessed in interrupt context.
   1166  */
   1167 void *
   1168 pool_page_alloc_nointr(unsigned long sz, int flags, int mtype)
   1169 {
   1170 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
   1171 
   1172 	return ((void *)uvm_km_alloc_poolpage1(kernel_map, uvm.kernel_object,
   1173 	    waitok));
   1174 }
   1175 
   1176 void
   1177 pool_page_free_nointr(void *v, unsigned long sz, int mtype)
   1178 {
   1179 
   1180 	uvm_km_free_poolpage1(kernel_map, (vaddr_t)v);
   1181 }
   1182 
   1183 
   1184 /*
   1185  * Release all complete pages that have not been used recently.
   1186  */
   1187 void
   1188 _pool_reclaim(struct pool *pp, const char *file, long line)
   1189 {
   1190 	struct pool_item_header *ph, *phnext;
   1191 	struct timeval curtime;
   1192 	int s;
   1193 
   1194 	if (pp->pr_roflags & PR_STATIC)
   1195 		return;
   1196 
   1197 	if (simple_lock_try(&pp->pr_slock) == 0)
   1198 		return;
   1199 	pr_enter(pp, file, line);
   1200 
   1201 	s = splclock();
   1202 	curtime = mono_time;
   1203 	splx(s);
   1204 
   1205 	for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL; ph = phnext) {
   1206 		phnext = TAILQ_NEXT(ph, ph_pagelist);
   1207 
   1208 		/* Check our minimum page claim */
   1209 		if (pp->pr_npages <= pp->pr_minpages)
   1210 			break;
   1211 
   1212 		if (ph->ph_nmissing == 0) {
   1213 			struct timeval diff;
   1214 			timersub(&curtime, &ph->ph_time, &diff);
   1215 			if (diff.tv_sec < pool_inactive_time)
   1216 				continue;
   1217 
   1218 			/*
   1219 			 * If freeing this page would put us below
   1220 			 * the low water mark, stop now.
   1221 			 */
   1222 			if ((pp->pr_nitems - pp->pr_itemsperpage) <
   1223 			    pp->pr_minitems)
   1224 				break;
   1225 
   1226 			pr_rmpage(pp, ph);
   1227 		}
   1228 	}
   1229 
   1230 	pr_leave(pp);
   1231 	simple_unlock(&pp->pr_slock);
   1232 }
   1233 
   1234 
   1235 /*
   1236  * Drain pools, one at a time.
   1237  *
   1238  * Note, we must never be called from an interrupt context.
   1239  */
   1240 void
   1241 pool_drain(void *arg)
   1242 {
   1243 	struct pool *pp;
   1244 	int s;
   1245 
   1246 	s = splimp();
   1247 	simple_lock(&pool_head_slock);
   1248 
   1249 	if (drainpp == NULL && (drainpp = TAILQ_FIRST(&pool_head)) == NULL)
   1250 		goto out;
   1251 
   1252 	pp = drainpp;
   1253 	drainpp = TAILQ_NEXT(pp, pr_poollist);
   1254 
   1255 	pool_reclaim(pp);
   1256 
   1257  out:
   1258 	simple_unlock(&pool_head_slock);
   1259 	splx(s);
   1260 }
   1261 
   1262 
   1263 /*
   1264  * Diagnostic helpers.
   1265  */
   1266 void
   1267 pool_print(struct pool *pp, const char *modif)
   1268 {
   1269 	int s;
   1270 
   1271 	s = splimp();
   1272 	if (simple_lock_try(&pp->pr_slock) == 0) {
   1273 		printf("pool %s is locked; try again later\n",
   1274 		    pp->pr_wchan);
   1275 		splx(s);
   1276 		return;
   1277 	}
   1278 	pool_print1(pp, modif, printf);
   1279 	simple_unlock(&pp->pr_slock);
   1280 	splx(s);
   1281 }
   1282 
   1283 void
   1284 pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
   1285 {
   1286 	int didlock = 0;
   1287 
   1288 	if (pp == NULL) {
   1289 		(*pr)("Must specify a pool to print.\n");
   1290 		return;
   1291 	}
   1292 
   1293 	/*
   1294 	 * Called from DDB; interrupts should be blocked, and all
   1295 	 * other processors should be paused.  We can skip locking
   1296 	 * the pool in this case.
   1297 	 *
   1298 	 * We do a simple_lock_try() just to print the lock
   1299 	 * status, however.
   1300 	 */
   1301 
   1302 	if (simple_lock_try(&pp->pr_slock) == 0)
   1303 		(*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
   1304 	else
   1305 		didlock = 1;
   1306 
   1307 	pool_print1(pp, modif, pr);
   1308 
   1309 	if (didlock)
   1310 		simple_unlock(&pp->pr_slock);
   1311 }
   1312 
   1313 static void
   1314 pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
   1315 {
   1316 	struct pool_item_header *ph;
   1317 #ifdef DIAGNOSTIC
   1318 	struct pool_item *pi;
   1319 #endif
   1320 	int print_log = 0, print_pagelist = 0;
   1321 	char c;
   1322 
   1323 	while ((c = *modif++) != '\0') {
   1324 		if (c == 'l')
   1325 			print_log = 1;
   1326 		if (c == 'p')
   1327 			print_pagelist = 1;
   1328 		modif++;
   1329 	}
   1330 
   1331 	(*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
   1332 	    pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
   1333 	    pp->pr_roflags);
   1334 	(*pr)("\tpagesz %u, mtype %d\n", pp->pr_pagesz, pp->pr_mtype);
   1335 	(*pr)("\talloc %p, release %p\n", pp->pr_alloc, pp->pr_free);
   1336 	(*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
   1337 	    pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
   1338 	(*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
   1339 	    pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
   1340 
   1341 	(*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
   1342 	    pp->pr_nget, pp->pr_nfail, pp->pr_nput);
   1343 	(*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
   1344 	    pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
   1345 
   1346 	if (print_pagelist == 0)
   1347 		goto skip_pagelist;
   1348 
   1349 	if ((ph = TAILQ_FIRST(&pp->pr_pagelist)) != NULL)
   1350 		(*pr)("\n\tpage list:\n");
   1351 	for (; ph != NULL; ph = TAILQ_NEXT(ph, ph_pagelist)) {
   1352 		(*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
   1353 		    ph->ph_page, ph->ph_nmissing,
   1354 		    (u_long)ph->ph_time.tv_sec,
   1355 		    (u_long)ph->ph_time.tv_usec);
   1356 #ifdef DIAGNOSTIC
   1357 		for (pi = TAILQ_FIRST(&ph->ph_itemlist); pi != NULL;
   1358 		     pi = TAILQ_NEXT(pi, pi_list)) {
   1359 			if (pi->pi_magic != PI_MAGIC) {
   1360 				(*pr)("\t\t\titem %p, magic 0x%x\n",
   1361 				    pi, pi->pi_magic);
   1362 			}
   1363 		}
   1364 #endif
   1365 	}
   1366 	if (pp->pr_curpage == NULL)
   1367 		(*pr)("\tno current page\n");
   1368 	else
   1369 		(*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
   1370 
   1371  skip_pagelist:
   1372 
   1373 	if (print_log == 0)
   1374 		goto skip_log;
   1375 
   1376 	(*pr)("\n");
   1377 	if ((pp->pr_roflags & PR_LOGGING) == 0)
   1378 		(*pr)("\tno log\n");
   1379 	else
   1380 		pr_printlog(pp, NULL, pr);
   1381 
   1382  skip_log:
   1383 
   1384 	pr_enter_check(pp, pr);
   1385 }
   1386 
   1387 int
   1388 pool_chk(struct pool *pp, const char *label)
   1389 {
   1390 	struct pool_item_header *ph;
   1391 	int r = 0;
   1392 
   1393 	simple_lock(&pp->pr_slock);
   1394 
   1395 	for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
   1396 	     ph = TAILQ_NEXT(ph, ph_pagelist)) {
   1397 
   1398 		struct pool_item *pi;
   1399 		int n;
   1400 		caddr_t page;
   1401 
   1402 		page = (caddr_t)((u_long)ph & pp->pr_pagemask);
   1403 		if (page != ph->ph_page &&
   1404 		    (pp->pr_roflags & PR_PHINPAGE) != 0) {
   1405 			if (label != NULL)
   1406 				printf("%s: ", label);
   1407 			printf("pool(%p:%s): page inconsistency: page %p;"
   1408 			       " at page head addr %p (p %p)\n", pp,
   1409 				pp->pr_wchan, ph->ph_page,
   1410 				ph, page);
   1411 			r++;
   1412 			goto out;
   1413 		}
   1414 
   1415 		for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
   1416 		     pi != NULL;
   1417 		     pi = TAILQ_NEXT(pi,pi_list), n++) {
   1418 
   1419 #ifdef DIAGNOSTIC
   1420 			if (pi->pi_magic != PI_MAGIC) {
   1421 				if (label != NULL)
   1422 					printf("%s: ", label);
   1423 				printf("pool(%s): free list modified: magic=%x;"
   1424 				       " page %p; item ordinal %d;"
   1425 				       " addr %p (p %p)\n",
   1426 					pp->pr_wchan, pi->pi_magic, ph->ph_page,
   1427 					n, pi, page);
   1428 				panic("pool");
   1429 			}
   1430 #endif
   1431 			page = (caddr_t)((u_long)pi & pp->pr_pagemask);
   1432 			if (page == ph->ph_page)
   1433 				continue;
   1434 
   1435 			if (label != NULL)
   1436 				printf("%s: ", label);
   1437 			printf("pool(%p:%s): page inconsistency: page %p;"
   1438 			       " item ordinal %d; addr %p (p %p)\n", pp,
   1439 				pp->pr_wchan, ph->ph_page,
   1440 				n, pi, page);
   1441 			r++;
   1442 			goto out;
   1443 		}
   1444 	}
   1445 out:
   1446 	simple_unlock(&pp->pr_slock);
   1447 	return (r);
   1448 }
   1449