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