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