Home | History | Annotate | Line # | Download | only in kern
subr_pool.c revision 1.15
      1 /*	$NetBSD: subr_pool.c,v 1.15 1998/09/29 18:09:29 pk Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 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.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/proc.h>
     42 #include <sys/errno.h>
     43 #include <sys/kernel.h>
     44 #include <sys/malloc.h>
     45 #include <sys/lock.h>
     46 #include <sys/pool.h>
     47 
     48 #include <vm/vm.h>
     49 #include <vm/vm_kern.h>
     50 
     51 #if defined(UVM)
     52 #include <uvm/uvm.h>
     53 #endif
     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 
     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 = NULL;
     78 
     79 struct pool_item_header {
     80 	/* Page headers */
     81 	TAILQ_ENTRY(pool_item_header)
     82 				ph_pagelist;	/* pool page list */
     83 	TAILQ_HEAD(,pool_item)	ph_itemlist;	/* chunk list for this page */
     84 	LIST_ENTRY(pool_item_header)
     85 				ph_hashlist;	/* Off-page page headers */
     86 	int			ph_nmissing;	/* # of chunks in use */
     87 	caddr_t			ph_page;	/* this page's address */
     88 	struct timeval		ph_time;	/* last referenced */
     89 };
     90 
     91 struct pool_item {
     92 #ifdef DIAGNOSTIC
     93 	int pi_magic;
     94 #define PI_MAGIC 0xdeadbeef
     95 #endif
     96 	/* Other entries use only this list entry */
     97 	TAILQ_ENTRY(pool_item)	pi_list;
     98 };
     99 
    100 
    101 #define PR_HASH_INDEX(pp,addr) \
    102 	(((u_long)(addr) >> (pp)->pr_pageshift) & (PR_HASHTABSIZE - 1))
    103 
    104 
    105 
    106 static struct pool_item_header
    107 		*pr_find_pagehead __P((struct pool *, caddr_t));
    108 static void	pr_rmpage __P((struct pool *, struct pool_item_header *));
    109 static int	pool_prime_page __P((struct pool *, caddr_t));
    110 static void	*pool_page_alloc __P((unsigned long, int, int));
    111 static void	pool_page_free __P((void *, unsigned long, int));
    112 int pool_chk __P((struct pool *, char *));
    113 
    114 
    115 #ifdef POOL_DIAGNOSTIC
    116 /*
    117  * Pool log entry. An array of these is allocated in pool_create().
    118  */
    119 struct pool_log {
    120 	const char	*pl_file;
    121 	long		pl_line;
    122 	int		pl_action;
    123 #define PRLOG_GET	1
    124 #define PRLOG_PUT	2
    125 	void		*pl_addr;
    126 };
    127 
    128 /* Number of entries in pool log buffers */
    129 int pool_logsize = 10;
    130 
    131 static void	pr_log __P((struct pool *, void *, int, const char *, long));
    132 static void	pr_printlog __P((struct pool *));
    133 
    134 static __inline__ void
    135 pr_log(pp, v, action, file, line)
    136 	struct pool	*pp;
    137 	void		*v;
    138 	int		action;
    139 	const char	*file;
    140 	long		line;
    141 {
    142 	int n = pp->pr_curlogentry;
    143 	struct pool_log *pl;
    144 
    145 	if ((pp->pr_flags & PR_LOGGING) == 0)
    146 		return;
    147 
    148 	/*
    149 	 * Fill in the current entry. Wrap around and overwrite
    150 	 * the oldest entry if necessary.
    151 	 */
    152 	pl = &pp->pr_log[n];
    153 	pl->pl_file = file;
    154 	pl->pl_line = line;
    155 	pl->pl_action = action;
    156 	pl->pl_addr = v;
    157 	if (++n >= pp->pr_logsize)
    158 		n = 0;
    159 	pp->pr_curlogentry = n;
    160 }
    161 
    162 static void
    163 pr_printlog(pp)
    164 	struct pool *pp;
    165 {
    166 	int i = pp->pr_logsize;
    167 	int n = pp->pr_curlogentry;
    168 
    169 	if ((pp->pr_flags & PR_LOGGING) == 0)
    170 		return;
    171 
    172 	pool_print(pp, "printlog");
    173 
    174 	/*
    175 	 * Print all entries in this pool's log.
    176 	 */
    177 	while (i-- > 0) {
    178 		struct pool_log *pl = &pp->pr_log[n];
    179 		if (pl->pl_action != 0) {
    180 			printf("log entry %d:\n", i);
    181 			printf("\taction = %s, addr = %p\n",
    182 				pl->pl_action == PRLOG_GET ? "get" : "put",
    183 				pl->pl_addr);
    184 			printf("\tfile: %s at line %lu\n",
    185 				pl->pl_file, pl->pl_line);
    186 		}
    187 		if (++n >= pp->pr_logsize)
    188 			n = 0;
    189 	}
    190 }
    191 #else
    192 #define pr_log(pp, v, action, file, line)
    193 #define pr_printlog(pp)
    194 #endif
    195 
    196 
    197 /*
    198  * Return the pool page header based on page address.
    199  */
    200 static __inline__ struct pool_item_header *
    201 pr_find_pagehead(pp, page)
    202 	struct pool *pp;
    203 	caddr_t page;
    204 {
    205 	struct pool_item_header *ph;
    206 
    207 	if ((pp->pr_flags & PR_PHINPAGE) != 0)
    208 		return ((struct pool_item_header *)(page + pp->pr_phoffset));
    209 
    210 	for (ph = LIST_FIRST(&pp->pr_hashtab[PR_HASH_INDEX(pp, page)]);
    211 	     ph != NULL;
    212 	     ph = LIST_NEXT(ph, ph_hashlist)) {
    213 		if (ph->ph_page == page)
    214 			return (ph);
    215 	}
    216 	return (NULL);
    217 }
    218 
    219 /*
    220  * Remove a page from the pool.
    221  */
    222 static __inline__ void
    223 pr_rmpage(pp, ph)
    224 	struct pool *pp;
    225 	struct pool_item_header *ph;
    226 {
    227 
    228 	/*
    229 	 * If the page was idle, decrement the idle page count.
    230 	 */
    231 	if (ph->ph_nmissing == 0) {
    232 #ifdef DIAGNOSTIC
    233 		if (pp->pr_nidle == 0)
    234 			panic("pr_rmpage: nidle inconsistent");
    235 #endif
    236 		pp->pr_nidle--;
    237 	}
    238 
    239 	/*
    240 	 * Unlink a page from the pool and release it.
    241 	 */
    242 	TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
    243 	(*pp->pr_free)(ph->ph_page, pp->pr_pagesz, pp->pr_mtype);
    244 	pp->pr_npages--;
    245 	pp->pr_npagefree++;
    246 
    247 	if ((pp->pr_flags & PR_PHINPAGE) == 0) {
    248 		LIST_REMOVE(ph, ph_hashlist);
    249 		pool_put(&phpool, ph);
    250 	}
    251 
    252 	if (pp->pr_curpage == ph) {
    253 		/*
    254 		 * Find a new non-empty page header, if any.
    255 		 * Start search from the page head, to increase the
    256 		 * chance for "high water" pages to be freed.
    257 		 */
    258 		for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
    259 		     ph = TAILQ_NEXT(ph, ph_pagelist))
    260 			if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
    261 				break;
    262 
    263 		pp->pr_curpage = ph;
    264 	}
    265 }
    266 
    267 /*
    268  * Allocate and initialize a pool.
    269  */
    270 struct pool *
    271 pool_create(size, align, ioff, nitems, wchan, pagesz, alloc, release, mtype)
    272 	size_t	size;
    273 	u_int	align;
    274 	u_int	ioff;
    275 	int	nitems;
    276 	char	*wchan;
    277 	size_t	pagesz;
    278 	void	*(*alloc) __P((unsigned long, int, int));
    279 	void	(*release) __P((void *, unsigned long, int));
    280 	int	mtype;
    281 {
    282 	struct pool *pp;
    283 	int flags;
    284 
    285 	pp = (struct pool *)malloc(sizeof(*pp), M_POOL, M_NOWAIT);
    286 	if (pp == NULL)
    287 		return (NULL);
    288 
    289 	flags = PR_FREEHEADER;
    290 #ifdef POOL_DIAGNOSTIC
    291 	if (pool_logsize != 0)
    292 		flags |= PR_LOGGING;
    293 #endif
    294 
    295 	pool_init(pp, size, align, ioff, flags, wchan, pagesz,
    296 		  alloc, release, mtype);
    297 
    298 	if (nitems != 0) {
    299 		if (pool_prime(pp, nitems, NULL) != 0) {
    300 			pool_destroy(pp);
    301 			return (NULL);
    302 		}
    303 	}
    304 
    305 	return (pp);
    306 }
    307 
    308 /*
    309  * Initialize the given pool resource structure.
    310  *
    311  * We export this routine to allow other kernel parts to declare
    312  * static pools that must be initialized before malloc() is available.
    313  */
    314 void
    315 pool_init(pp, size, align, ioff, flags, wchan, pagesz, alloc, release, mtype)
    316 	struct pool	*pp;
    317 	size_t		size;
    318 	u_int		align;
    319 	u_int		ioff;
    320 	int		flags;
    321 	char		*wchan;
    322 	size_t		pagesz;
    323 	void		*(*alloc) __P((unsigned long, int, int));
    324 	void		(*release) __P((void *, unsigned long, int));
    325 	int		mtype;
    326 {
    327 	int off, slack;
    328 
    329 	/*
    330 	 * Check arguments and construct default values.
    331 	 */
    332 	if (!powerof2(pagesz) || pagesz > PAGE_SIZE)
    333 		panic("pool_init: page size invalid (%lx)\n", (u_long)pagesz);
    334 
    335 	if (alloc == NULL && release == NULL) {
    336 		alloc = pool_page_alloc;
    337 		release = pool_page_free;
    338 		pagesz = PAGE_SIZE;	/* Rounds to PAGE_SIZE anyhow. */
    339 	} else if ((alloc != NULL && release != NULL) == 0) {
    340 		/* If you specifiy one, must specify both. */
    341 		panic("pool_init: must specify alloc and release together");
    342 	}
    343 
    344 	if (pagesz == 0)
    345 		pagesz = PAGE_SIZE;
    346 
    347 	if (align == 0)
    348 		align = ALIGN(1);
    349 
    350 	if (size < sizeof(struct pool_item))
    351 		size = sizeof(struct pool_item);
    352 
    353 	/*
    354 	 * Initialize the pool structure.
    355 	 */
    356 	TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
    357 	TAILQ_INIT(&pp->pr_pagelist);
    358 	pp->pr_curpage = NULL;
    359 	pp->pr_npages = 0;
    360 	pp->pr_minitems = 0;
    361 	pp->pr_minpages = 0;
    362 	pp->pr_maxpages = UINT_MAX;
    363 	pp->pr_flags = flags;
    364 	pp->pr_size = ALIGN(size);
    365 	pp->pr_align = align;
    366 	pp->pr_wchan = wchan;
    367 	pp->pr_mtype = mtype;
    368 	pp->pr_alloc = alloc;
    369 	pp->pr_free = release;
    370 	pp->pr_pagesz = pagesz;
    371 	pp->pr_pagemask = ~(pagesz - 1);
    372 	pp->pr_pageshift = ffs(pagesz) - 1;
    373 
    374 	/*
    375 	 * Decide whether to put the page header off page to avoid
    376 	 * wasting too large a part of the page. Off-page page headers
    377 	 * go on a hash table, so we can match a returned item
    378 	 * with its header based on the page address.
    379 	 * We use 1/16 of the page size as the threshold (XXX: tune)
    380 	 */
    381 	if (pp->pr_size < pagesz/16) {
    382 		/* Use the end of the page for the page header */
    383 		pp->pr_flags |= PR_PHINPAGE;
    384 		pp->pr_phoffset = off =
    385 			pagesz - ALIGN(sizeof(struct pool_item_header));
    386 	} else {
    387 		/* The page header will be taken from our page header pool */
    388 		pp->pr_phoffset = 0;
    389 		off = pagesz;
    390 		memset(pp->pr_hashtab, 0, sizeof(pp->pr_hashtab));
    391 	}
    392 
    393 	/*
    394 	 * Alignment is to take place at `ioff' within the item. This means
    395 	 * we must reserve up to `align - 1' bytes on the page to allow
    396 	 * appropriate positioning of each item.
    397 	 *
    398 	 * Silently enforce `0 <= ioff < align'.
    399 	 */
    400 	pp->pr_itemoffset = ioff = ioff % align;
    401 	pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
    402 
    403 	/*
    404 	 * Use the slack between the chunks and the page header
    405 	 * for "cache coloring".
    406 	 */
    407 	slack = off - pp->pr_itemsperpage * pp->pr_size;
    408 	pp->pr_maxcolor = (slack / align) * align;
    409 	pp->pr_curcolor = 0;
    410 
    411 	pp->pr_nget = 0;
    412 	pp->pr_nfail = 0;
    413 	pp->pr_nput = 0;
    414 	pp->pr_npagealloc = 0;
    415 	pp->pr_npagefree = 0;
    416 	pp->pr_hiwat = 0;
    417 	pp->pr_nidle = 0;
    418 
    419 #ifdef POOL_DIAGNOSTIC
    420 	if ((flags & PR_LOGGING) != 0) {
    421 		pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
    422 				    M_TEMP, M_NOWAIT);
    423 		if (pp->pr_log == NULL)
    424 			pp->pr_flags &= ~PR_LOGGING;
    425 		pp->pr_curlogentry = 0;
    426 		pp->pr_logsize = pool_logsize;
    427 	}
    428 #endif
    429 
    430 	simple_lock_init(&pp->pr_lock);
    431 	lockinit(&pp->pr_resourcelock, PSWP, wchan, 0, 0);
    432 
    433 	/*
    434 	 * Initialize private page header pool if we haven't done so yet.
    435 	 */
    436 	if (phpool.pr_size == 0) {
    437 		pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
    438 			  0, "phpool", 0, 0, 0, 0);
    439 	}
    440 
    441 	return;
    442 }
    443 
    444 /*
    445  * De-commision a pool resource.
    446  */
    447 void
    448 pool_destroy(pp)
    449 	struct pool *pp;
    450 {
    451 	struct pool_item_header *ph;
    452 
    453 #ifdef DIAGNOSTIC
    454 	if (pp->pr_nget - pp->pr_nput != 0) {
    455 		pr_printlog(pp);
    456 		panic("pool_destroy: pool busy: still out: %lu\n",
    457 		      pp->pr_nget - pp->pr_nput);
    458 	}
    459 #endif
    460 
    461 	/* Remove all pages */
    462 	if ((pp->pr_flags & PR_STATIC) == 0)
    463 		while ((ph = pp->pr_pagelist.tqh_first) != NULL)
    464 			pr_rmpage(pp, ph);
    465 
    466 	/* Remove from global pool list */
    467 	TAILQ_REMOVE(&pool_head, pp, pr_poollist);
    468 	drainpp = NULL;
    469 
    470 #ifdef POOL_DIAGNOSTIC
    471 	if ((pp->pr_flags & PR_LOGGING) != 0)
    472 		free(pp->pr_log, M_TEMP);
    473 #endif
    474 
    475 	if (pp->pr_flags & PR_FREEHEADER)
    476 		free(pp, M_POOL);
    477 }
    478 
    479 
    480 /*
    481  * Grab an item from the pool; must be called at appropriate spl level
    482  */
    483 #ifdef POOL_DIAGNOSTIC
    484 void *
    485 _pool_get(pp, flags, file, line)
    486 	struct pool *pp;
    487 	int flags;
    488 	const char *file;
    489 	long line;
    490 #else
    491 void *
    492 pool_get(pp, flags)
    493 	struct pool *pp;
    494 	int flags;
    495 #endif
    496 {
    497 	void *v;
    498 	struct pool_item *pi;
    499 	struct pool_item_header *ph;
    500 
    501 #ifdef DIAGNOSTIC
    502 	if ((pp->pr_flags & PR_STATIC) && (flags & PR_MALLOCOK)) {
    503 		pr_printlog(pp);
    504 		panic("pool_get: static");
    505 	}
    506 #endif
    507 
    508 	simple_lock(&pp->pr_lock);
    509 	if (curproc == NULL && (flags & PR_WAITOK) != 0)
    510 		panic("pool_get: must have NOWAIT");
    511 
    512 	/*
    513 	 * The convention we use is that if `curpage' is not NULL, then
    514 	 * it points at a non-empty bucket. In particular, `curpage'
    515 	 * never points at a page header which has PR_PHINPAGE set and
    516 	 * has no items in its bucket.
    517 	 */
    518 	while ((ph = pp->pr_curpage) == NULL) {
    519 		void *v;
    520 		int lkflags = LK_EXCLUSIVE | LK_INTERLOCK |
    521 			      ((flags & PR_WAITOK) == 0 ? LK_NOWAIT : 0);
    522 
    523 		/* Get long-term lock on pool */
    524 		if (lockmgr(&pp->pr_resourcelock, lkflags, &pp->pr_lock) != 0)
    525 			return (NULL);
    526 
    527 		/* Check if pool became non-empty while we slept */
    528 		if ((ph = pp->pr_curpage) != NULL)
    529 			goto again;
    530 
    531 		/* Call the page back-end allocator for more memory */
    532 		v = (*pp->pr_alloc)(pp->pr_pagesz, flags, pp->pr_mtype);
    533 		if (v == NULL) {
    534 			if (flags & PR_URGENT)
    535 				panic("pool_get: urgent");
    536 			if ((flags & PR_WAITOK) == 0) {
    537 				pp->pr_nfail++;
    538 				lockmgr(&pp->pr_resourcelock, LK_RELEASE, NULL);
    539 				return (NULL);
    540 			}
    541 
    542 			/*
    543 			 * Wait for items to be returned to this pool.
    544 			 * XXX: we actually want to wait just until
    545 			 * the page allocator has memory again. Depending
    546 			 * on this pool's usage, we might get stuck here
    547 			 * for a long time.
    548 			 */
    549 			pp->pr_flags |= PR_WANTED;
    550 			lockmgr(&pp->pr_resourcelock, LK_RELEASE, NULL);
    551 			tsleep((caddr_t)pp, PSWP, pp->pr_wchan, 0);
    552 			simple_lock(&pp->pr_lock);
    553 			continue;
    554 		}
    555 
    556 		/* We have more memory; add it to the pool */
    557 		pp->pr_npagealloc++;
    558 		pool_prime_page(pp, v);
    559 
    560 again:
    561 		/* Re-acquire pool interlock */
    562 		simple_lock(&pp->pr_lock);
    563 		lockmgr(&pp->pr_resourcelock, LK_RELEASE, NULL);
    564 	}
    565 
    566 	if ((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)
    567 		panic("pool_get: %s: page empty", pp->pr_wchan);
    568 
    569 	pr_log(pp, v, PRLOG_GET, file, line);
    570 
    571 #ifdef DIAGNOSTIC
    572 	if (pi->pi_magic != PI_MAGIC) {
    573 		pr_printlog(pp);
    574 		panic("pool_get(%s): free list modified: magic=%x; page %p;"
    575 		       " item addr %p\n",
    576 			pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
    577 	}
    578 #endif
    579 
    580 	/*
    581 	 * Remove from item list.
    582 	 */
    583 	TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
    584 	if (ph->ph_nmissing == 0) {
    585 #ifdef DIAGNOSTIC
    586 		if (pp->pr_nidle == 0)
    587 			panic("pool_get: nidle inconsistent");
    588 #endif
    589 		pp->pr_nidle--;
    590 	}
    591 	ph->ph_nmissing++;
    592 	if (TAILQ_FIRST(&ph->ph_itemlist) == NULL) {
    593 		/*
    594 		 * Find a new non-empty page header, if any.
    595 		 * Start search from the page head, to increase
    596 		 * the chance for "high water" pages to be freed.
    597 		 *
    598 		 * First, move the now empty page to the head of
    599 		 * the page list.
    600 		 */
    601 		TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
    602 		TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
    603 		while ((ph = TAILQ_NEXT(ph, ph_pagelist)) != NULL)
    604 			if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
    605 				break;
    606 
    607 		pp->pr_curpage = ph;
    608 	}
    609 
    610 	pp->pr_nget++;
    611 	simple_unlock(&pp->pr_lock);
    612 	return (v);
    613 }
    614 
    615 /*
    616  * Return resource to the pool; must be called at appropriate spl level
    617  */
    618 #ifdef POOL_DIAGNOSTIC
    619 void
    620 _pool_put(pp, v, file, line)
    621 	struct pool *pp;
    622 	void *v;
    623 	const char *file;
    624 	long line;
    625 #else
    626 void
    627 pool_put(pp, v)
    628 	struct pool *pp;
    629 	void *v;
    630 #endif
    631 {
    632 	struct pool_item *pi = v;
    633 	struct pool_item_header *ph;
    634 	caddr_t page;
    635 
    636 	page = (caddr_t)((u_long)v & pp->pr_pagemask);
    637 
    638 	simple_lock(&pp->pr_lock);
    639 
    640 	pr_log(pp, v, PRLOG_PUT, file, line);
    641 
    642 	if ((ph = pr_find_pagehead(pp, page)) == NULL) {
    643 		pr_printlog(pp);
    644 		panic("pool_put: %s: page header missing", pp->pr_wchan);
    645 	}
    646 
    647 	/*
    648 	 * Return to item list.
    649 	 */
    650 #ifdef DIAGNOSTIC
    651 	pi->pi_magic = PI_MAGIC;
    652 #endif
    653 	TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
    654 	ph->ph_nmissing--;
    655 	pp->pr_nput++;
    656 
    657 	/* Cancel "pool empty" condition if it exists */
    658 	if (pp->pr_curpage == NULL)
    659 		pp->pr_curpage = ph;
    660 
    661 	if (pp->pr_flags & PR_WANTED) {
    662 		pp->pr_flags &= ~PR_WANTED;
    663 		if (ph->ph_nmissing == 0)
    664 			pp->pr_nidle++;
    665 		wakeup((caddr_t)pp);
    666 		simple_unlock(&pp->pr_lock);
    667 		return;
    668 	}
    669 
    670 	/*
    671 	 * If this page is now complete, move it to the end of the pagelist.
    672 	 * If this page has just become un-empty, move it the head.
    673 	 */
    674 	if (ph->ph_nmissing == 0) {
    675 		pp->pr_nidle++;
    676 		if (pp->pr_npages > pp->pr_maxpages) {
    677 #if 0
    678 			timeout(pool_drain, 0, pool_inactive_time*hz);
    679 #else
    680 			pr_rmpage(pp, ph);
    681 #endif
    682 		} else {
    683 			TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
    684 			TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
    685 			ph->ph_time = time;
    686 
    687 			/* XXX - update curpage */
    688 			for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
    689 			     ph = TAILQ_NEXT(ph, ph_pagelist))
    690 				if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
    691 					break;
    692 
    693 			pp->pr_curpage = ph;
    694 		}
    695 	}
    696 
    697 	simple_unlock(&pp->pr_lock);
    698 }
    699 
    700 /*
    701  * Add N items to the pool.
    702  */
    703 int
    704 pool_prime(pp, n, storage)
    705 	struct pool *pp;
    706 	int n;
    707 	caddr_t storage;
    708 {
    709 	caddr_t cp;
    710 	int newnitems, newpages;
    711 
    712 #ifdef DIAGNOSTIC
    713 	if (storage && !(pp->pr_flags & PR_STATIC))
    714 		panic("pool_prime: static");
    715 	/* !storage && static caught below */
    716 #endif
    717 
    718 	(void)lockmgr(&pp->pr_resourcelock, LK_EXCLUSIVE, NULL);
    719 	newnitems = pp->pr_minitems + n;
    720 	newpages =
    721 		roundup(pp->pr_itemsperpage,newnitems) / pp->pr_itemsperpage
    722 		- pp->pr_minpages;
    723 
    724 	while (newpages-- > 0) {
    725 
    726 		if (pp->pr_flags & PR_STATIC) {
    727 			cp = storage;
    728 			storage += pp->pr_pagesz;
    729 		} else {
    730 			cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
    731 		}
    732 
    733 		if (cp == NULL) {
    734 			(void)lockmgr(&pp->pr_resourcelock, LK_RELEASE, NULL);
    735 			return (ENOMEM);
    736 		}
    737 
    738 		pool_prime_page(pp, cp);
    739 		pp->pr_minpages++;
    740 	}
    741 
    742 	pp->pr_minitems = newnitems;
    743 
    744 	if (pp->pr_minpages >= pp->pr_maxpages)
    745 		pp->pr_maxpages = pp->pr_minpages + 1;	/* XXX */
    746 
    747 	(void)lockmgr(&pp->pr_resourcelock, LK_RELEASE, NULL);
    748 	return (0);
    749 }
    750 
    751 /*
    752  * Add a page worth of items to the pool.
    753  */
    754 int
    755 pool_prime_page(pp, storage)
    756 	struct pool *pp;
    757 	caddr_t storage;
    758 {
    759 	struct pool_item *pi;
    760 	struct pool_item_header *ph;
    761 	caddr_t cp = storage;
    762 	unsigned int align = pp->pr_align;
    763 	unsigned int ioff = pp->pr_itemoffset;
    764 	int n;
    765 
    766 	simple_lock(&pp->pr_lock);
    767 
    768 	if ((pp->pr_flags & PR_PHINPAGE) != 0) {
    769 		ph = (struct pool_item_header *)(cp + pp->pr_phoffset);
    770 	} else {
    771 		ph = pool_get(&phpool, PR_URGENT);
    772 		LIST_INSERT_HEAD(&pp->pr_hashtab[PR_HASH_INDEX(pp, cp)],
    773 				 ph, ph_hashlist);
    774 	}
    775 
    776 	/*
    777 	 * Insert page header.
    778 	 */
    779 	TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
    780 	TAILQ_INIT(&ph->ph_itemlist);
    781 	ph->ph_page = storage;
    782 	ph->ph_nmissing = 0;
    783 	ph->ph_time.tv_sec = ph->ph_time.tv_usec = 0;
    784 
    785 	pp->pr_nidle++;
    786 
    787 	/*
    788 	 * Color this page.
    789 	 */
    790 	cp = (caddr_t)(cp + pp->pr_curcolor);
    791 	if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
    792 		pp->pr_curcolor = 0;
    793 
    794 	/*
    795 	 * Adjust storage to apply aligment to `pr_itemoffset' in each item.
    796 	 */
    797 	if (ioff != 0)
    798 		cp = (caddr_t)(cp + (align - ioff));
    799 
    800 	/*
    801 	 * Insert remaining chunks on the bucket list.
    802 	 */
    803 	n = pp->pr_itemsperpage;
    804 
    805 	while (n--) {
    806 		pi = (struct pool_item *)cp;
    807 
    808 		/* Insert on page list */
    809 		TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
    810 #ifdef DIAGNOSTIC
    811 		pi->pi_magic = PI_MAGIC;
    812 #endif
    813 		cp = (caddr_t)(cp + pp->pr_size);
    814 	}
    815 
    816 	/*
    817 	 * If the pool was depleted, point at the new page.
    818 	 */
    819 	if (pp->pr_curpage == NULL)
    820 		pp->pr_curpage = ph;
    821 
    822 	if (++pp->pr_npages > pp->pr_hiwat)
    823 		pp->pr_hiwat = pp->pr_npages;
    824 
    825 	simple_unlock(&pp->pr_lock);
    826 	return (0);
    827 }
    828 
    829 void
    830 pool_setlowat(pp, n)
    831 	pool_handle_t	pp;
    832 	int n;
    833 {
    834 
    835 	(void)lockmgr(&pp->pr_resourcelock, LK_EXCLUSIVE, NULL);
    836 	pp->pr_minitems = n;
    837 	pp->pr_minpages = (n == 0)
    838 		? 0
    839 		: roundup(pp->pr_itemsperpage,n) / pp->pr_itemsperpage;
    840 	(void)lockmgr(&pp->pr_resourcelock, LK_RELEASE, NULL);
    841 }
    842 
    843 void
    844 pool_sethiwat(pp, n)
    845 	pool_handle_t	pp;
    846 	int n;
    847 {
    848 
    849 	(void)lockmgr(&pp->pr_resourcelock, LK_EXCLUSIVE, NULL);
    850 	pp->pr_maxpages = (n == 0)
    851 		? 0
    852 		: roundup(pp->pr_itemsperpage,n) / pp->pr_itemsperpage;
    853 	(void)lockmgr(&pp->pr_resourcelock, LK_RELEASE, NULL);
    854 }
    855 
    856 
    857 /*
    858  * Default page allocator.
    859  */
    860 static void *
    861 pool_page_alloc(sz, flags, mtype)
    862 	unsigned long sz;
    863 	int flags;
    864 	int mtype;
    865 {
    866 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
    867 
    868 #if defined(UVM)
    869 	return ((void *)uvm_km_alloc_poolpage(waitok));
    870 #else
    871 	return ((void *)kmem_alloc_poolpage(waitok));
    872 #endif
    873 }
    874 
    875 static void
    876 pool_page_free(v, sz, mtype)
    877 	void *v;
    878 	unsigned long sz;
    879 	int mtype;
    880 {
    881 
    882 #if defined(UVM)
    883 	uvm_km_free_poolpage((vaddr_t)v);
    884 #else
    885 	kmem_free_poolpage((vaddr_t)v);
    886 #endif
    887 }
    888 
    889 /*
    890  * Alternate pool page allocator for pools that know they will
    891  * never be accessed in interrupt context.
    892  */
    893 void *
    894 pool_page_alloc_nointr(sz, flags, mtype)
    895 	unsigned long sz;
    896 	int flags;
    897 	int mtype;
    898 {
    899 #if defined(UVM)
    900 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
    901 
    902 	/*
    903 	 * With UVM, we can use the kernel_map.
    904 	 */
    905 	return ((void *)uvm_km_alloc_poolpage1(kernel_map, uvm.kernel_object,
    906 	    waitok));
    907 #else
    908 	/*
    909 	 * Can't do anything so cool with Mach VM.
    910 	 */
    911 	return (pool_page_alloc(sz, flags, mtype));
    912 #endif
    913 }
    914 
    915 void
    916 pool_page_free_nointr(v, sz, mtype)
    917 	void *v;
    918 	unsigned long sz;
    919 	int mtype;
    920 {
    921 
    922 #if defined(UVM)
    923 	uvm_km_free_poolpage1(kernel_map, (vaddr_t)v);
    924 #else
    925 	pool_page_free(v, sz, mtype);
    926 #endif
    927 }
    928 
    929 
    930 /*
    931  * Release all complete pages that have not been used recently.
    932  */
    933 void
    934 pool_reclaim (pp)
    935 	pool_handle_t pp;
    936 {
    937 	struct pool_item_header *ph, *phnext;
    938 	struct timeval curtime = time;
    939 
    940 	if (pp->pr_flags & PR_STATIC)
    941 		return;
    942 
    943 	if (simple_lock_try(&pp->pr_lock) == 0)
    944 		return;
    945 
    946 	for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL; ph = phnext) {
    947 		phnext = TAILQ_NEXT(ph, ph_pagelist);
    948 
    949 		/* Check our minimum page claim */
    950 		if (pp->pr_npages <= pp->pr_minpages)
    951 			break;
    952 
    953 		if (ph->ph_nmissing == 0) {
    954 			struct timeval diff;
    955 			timersub(&curtime, &ph->ph_time, &diff);
    956 			if (diff.tv_sec < pool_inactive_time)
    957 				continue;
    958 			pr_rmpage(pp, ph);
    959 		}
    960 	}
    961 
    962 	simple_unlock(&pp->pr_lock);
    963 }
    964 
    965 
    966 /*
    967  * Drain pools, one at a time.
    968  */
    969 void
    970 pool_drain(arg)
    971 	void *arg;
    972 {
    973 	struct pool *pp;
    974 	int s = splimp();
    975 
    976 	/* XXX:lock pool head */
    977 	if (drainpp == NULL && (drainpp = TAILQ_FIRST(&pool_head)) == NULL) {
    978 		splx(s);
    979 		return;
    980 	}
    981 
    982 	pp = drainpp;
    983 	drainpp = TAILQ_NEXT(pp, pr_poollist);
    984 	/* XXX:unlock pool head */
    985 
    986 	pool_reclaim(pp);
    987 	splx(s);
    988 }
    989 
    990 
    991 #ifdef DEBUG
    992 /*
    993  * Diagnostic helpers.
    994  */
    995 void
    996 pool_print(pp, label)
    997 	struct pool *pp;
    998 	char *label;
    999 {
   1000 
   1001 	if (label != NULL)
   1002 		printf("%s: ", label);
   1003 
   1004 	printf("pool %s: nalloc %lu nfree %lu npagealloc %lu npagefree %lu\n"
   1005 	       "         npages %u minitems %u itemsperpage %u itemoffset %u\n"
   1006 	       "         nidle %lu\n",
   1007 		pp->pr_wchan,
   1008 		pp->pr_nget,
   1009 		pp->pr_nput,
   1010 		pp->pr_npagealloc,
   1011 		pp->pr_npagefree,
   1012 		pp->pr_npages,
   1013 		pp->pr_minitems,
   1014 		pp->pr_itemsperpage,
   1015 		pp->pr_itemoffset,
   1016 		pp->pr_nidle);
   1017 }
   1018 
   1019 int
   1020 pool_chk(pp, label)
   1021 	struct pool *pp;
   1022 	char *label;
   1023 {
   1024 	struct pool_item_header *ph;
   1025 	int r = 0;
   1026 
   1027 	simple_lock(&pp->pr_lock);
   1028 
   1029 	for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
   1030 	     ph = TAILQ_NEXT(ph, ph_pagelist)) {
   1031 
   1032 		struct pool_item *pi;
   1033 		int n;
   1034 		caddr_t page;
   1035 
   1036 		page = (caddr_t)((u_long)ph & pp->pr_pagemask);
   1037 		if (page != ph->ph_page) {
   1038 			if (label != NULL)
   1039 				printf("%s: ", label);
   1040 			printf("pool(%s): page inconsistency: page %p;"
   1041 			       " at page head addr %p (p %p)\n",
   1042 				pp->pr_wchan, ph->ph_page,
   1043 				ph, page);
   1044 			r++;
   1045 			goto out;
   1046 		}
   1047 
   1048 		for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
   1049 		     pi != NULL;
   1050 		     pi = TAILQ_NEXT(pi,pi_list), n++) {
   1051 
   1052 #ifdef DIAGNOSTIC
   1053 			if (pi->pi_magic != PI_MAGIC) {
   1054 				if (label != NULL)
   1055 					printf("%s: ", label);
   1056 				printf("pool(%s): free list modified: magic=%x;"
   1057 				       " page %p; item ordinal %d;"
   1058 				       " addr %p (p %p)\n",
   1059 					pp->pr_wchan, pi->pi_magic, ph->ph_page,
   1060 					n, pi, page);
   1061 				panic("pool");
   1062 			}
   1063 #endif
   1064 			page = (caddr_t)((u_long)pi & pp->pr_pagemask);
   1065 			if (page == ph->ph_page)
   1066 				continue;
   1067 
   1068 			if (label != NULL)
   1069 				printf("%s: ", label);
   1070 			printf("pool(%s): page inconsistency: page %p;"
   1071 			       " item ordinal %d; addr %p (p %p)\n",
   1072 				pp->pr_wchan, ph->ph_page,
   1073 				n, pi, page);
   1074 			r++;
   1075 			goto out;
   1076 		}
   1077 	}
   1078 out:
   1079 	simple_unlock(&pp->pr_lock);
   1080 	return (r);
   1081 }
   1082 #endif
   1083