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