Home | History | Annotate | Line # | Download | only in kern
subr_pool.c revision 1.165.2.2
      1  1.165.2.2  haad /*	$NetBSD: subr_pool.c,v 1.165.2.2 2008/10/19 22:17:28 haad Exp $	*/
      2  1.165.2.2  haad 
      3  1.165.2.2  haad /*-
      4  1.165.2.2  haad  * Copyright (c) 1997, 1999, 2000, 2002, 2007, 2008 The NetBSD Foundation, Inc.
      5  1.165.2.2  haad  * All rights reserved.
      6  1.165.2.2  haad  *
      7  1.165.2.2  haad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.165.2.2  haad  * by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace
      9  1.165.2.2  haad  * Simulation Facility, NASA Ames Research Center, and by Andrew Doran.
     10  1.165.2.2  haad  *
     11  1.165.2.2  haad  * Redistribution and use in source and binary forms, with or without
     12  1.165.2.2  haad  * modification, are permitted provided that the following conditions
     13  1.165.2.2  haad  * are met:
     14  1.165.2.2  haad  * 1. Redistributions of source code must retain the above copyright
     15  1.165.2.2  haad  *    notice, this list of conditions and the following disclaimer.
     16  1.165.2.2  haad  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.165.2.2  haad  *    notice, this list of conditions and the following disclaimer in the
     18  1.165.2.2  haad  *    documentation and/or other materials provided with the distribution.
     19  1.165.2.2  haad  *
     20  1.165.2.2  haad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.165.2.2  haad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.165.2.2  haad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.165.2.2  haad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.165.2.2  haad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.165.2.2  haad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.165.2.2  haad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.165.2.2  haad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.165.2.2  haad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.165.2.2  haad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.165.2.2  haad  * POSSIBILITY OF SUCH DAMAGE.
     31  1.165.2.2  haad  */
     32  1.165.2.2  haad 
     33  1.165.2.2  haad #include <sys/cdefs.h>
     34  1.165.2.2  haad __KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.165.2.2 2008/10/19 22:17:28 haad Exp $");
     35  1.165.2.2  haad 
     36  1.165.2.2  haad #include "opt_ddb.h"
     37  1.165.2.2  haad #include "opt_pool.h"
     38  1.165.2.2  haad #include "opt_poollog.h"
     39  1.165.2.2  haad #include "opt_lockdebug.h"
     40  1.165.2.2  haad 
     41  1.165.2.2  haad #include <sys/param.h>
     42  1.165.2.2  haad #include <sys/systm.h>
     43  1.165.2.2  haad #include <sys/bitops.h>
     44  1.165.2.2  haad #include <sys/proc.h>
     45  1.165.2.2  haad #include <sys/errno.h>
     46  1.165.2.2  haad #include <sys/kernel.h>
     47  1.165.2.2  haad #include <sys/malloc.h>
     48  1.165.2.2  haad #include <sys/pool.h>
     49  1.165.2.2  haad #include <sys/syslog.h>
     50  1.165.2.2  haad #include <sys/debug.h>
     51  1.165.2.2  haad #include <sys/lockdebug.h>
     52  1.165.2.2  haad #include <sys/xcall.h>
     53  1.165.2.2  haad #include <sys/cpu.h>
     54  1.165.2.2  haad #include <sys/atomic.h>
     55  1.165.2.2  haad 
     56  1.165.2.2  haad #include <uvm/uvm.h>
     57  1.165.2.2  haad 
     58  1.165.2.2  haad /*
     59  1.165.2.2  haad  * Pool resource management utility.
     60  1.165.2.2  haad  *
     61  1.165.2.2  haad  * Memory is allocated in pages which are split into pieces according to
     62  1.165.2.2  haad  * the pool item size. Each page is kept on one of three lists in the
     63  1.165.2.2  haad  * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages',
     64  1.165.2.2  haad  * for empty, full and partially-full pages respectively. The individual
     65  1.165.2.2  haad  * pool items are on a linked list headed by `ph_itemlist' in each page
     66  1.165.2.2  haad  * header. The memory for building the page list is either taken from
     67  1.165.2.2  haad  * the allocated pages themselves (for small pool items) or taken from
     68  1.165.2.2  haad  * an internal pool of page headers (`phpool').
     69  1.165.2.2  haad  */
     70  1.165.2.2  haad 
     71  1.165.2.2  haad /* List of all pools */
     72  1.165.2.2  haad TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
     73  1.165.2.2  haad 
     74  1.165.2.2  haad /* Private pool for page header structures */
     75  1.165.2.2  haad #define	PHPOOL_MAX	8
     76  1.165.2.2  haad static struct pool phpool[PHPOOL_MAX];
     77  1.165.2.2  haad #define	PHPOOL_FREELIST_NELEM(idx) \
     78  1.165.2.2  haad 	(((idx) == 0) ? 0 : BITMAP_SIZE * (1 << (idx)))
     79  1.165.2.2  haad 
     80  1.165.2.2  haad #ifdef POOL_SUBPAGE
     81  1.165.2.2  haad /* Pool of subpages for use by normal pools. */
     82  1.165.2.2  haad static struct pool psppool;
     83  1.165.2.2  haad #endif
     84  1.165.2.2  haad 
     85  1.165.2.2  haad static SLIST_HEAD(, pool_allocator) pa_deferinitq =
     86  1.165.2.2  haad     SLIST_HEAD_INITIALIZER(pa_deferinitq);
     87  1.165.2.2  haad 
     88  1.165.2.2  haad static void *pool_page_alloc_meta(struct pool *, int);
     89  1.165.2.2  haad static void pool_page_free_meta(struct pool *, void *);
     90  1.165.2.2  haad 
     91  1.165.2.2  haad /* allocator for pool metadata */
     92  1.165.2.2  haad struct pool_allocator pool_allocator_meta = {
     93  1.165.2.2  haad 	pool_page_alloc_meta, pool_page_free_meta,
     94  1.165.2.2  haad 	.pa_backingmapptr = &kmem_map,
     95  1.165.2.2  haad };
     96  1.165.2.2  haad 
     97  1.165.2.2  haad /* # of seconds to retain page after last use */
     98  1.165.2.2  haad int pool_inactive_time = 10;
     99  1.165.2.2  haad 
    100  1.165.2.2  haad /* Next candidate for drainage (see pool_drain()) */
    101  1.165.2.2  haad static struct pool	*drainpp;
    102  1.165.2.2  haad 
    103  1.165.2.2  haad /* This lock protects both pool_head and drainpp. */
    104  1.165.2.2  haad static kmutex_t pool_head_lock;
    105  1.165.2.2  haad static kcondvar_t pool_busy;
    106  1.165.2.2  haad 
    107  1.165.2.2  haad typedef uint32_t pool_item_bitmap_t;
    108  1.165.2.2  haad #define	BITMAP_SIZE	(CHAR_BIT * sizeof(pool_item_bitmap_t))
    109  1.165.2.2  haad #define	BITMAP_MASK	(BITMAP_SIZE - 1)
    110  1.165.2.2  haad 
    111  1.165.2.2  haad struct pool_item_header {
    112  1.165.2.2  haad 	/* Page headers */
    113  1.165.2.2  haad 	LIST_ENTRY(pool_item_header)
    114  1.165.2.2  haad 				ph_pagelist;	/* pool page list */
    115  1.165.2.2  haad 	SPLAY_ENTRY(pool_item_header)
    116  1.165.2.2  haad 				ph_node;	/* Off-page page headers */
    117  1.165.2.2  haad 	void *			ph_page;	/* this page's address */
    118  1.165.2.2  haad 	uint32_t		ph_time;	/* last referenced */
    119  1.165.2.2  haad 	uint16_t		ph_nmissing;	/* # of chunks in use */
    120  1.165.2.2  haad 	uint16_t		ph_off;		/* start offset in page */
    121  1.165.2.2  haad 	union {
    122  1.165.2.2  haad 		/* !PR_NOTOUCH */
    123  1.165.2.2  haad 		struct {
    124  1.165.2.2  haad 			LIST_HEAD(, pool_item)
    125  1.165.2.2  haad 				phu_itemlist;	/* chunk list for this page */
    126  1.165.2.2  haad 		} phu_normal;
    127  1.165.2.2  haad 		/* PR_NOTOUCH */
    128  1.165.2.2  haad 		struct {
    129  1.165.2.2  haad 			pool_item_bitmap_t phu_bitmap[1];
    130  1.165.2.2  haad 		} phu_notouch;
    131  1.165.2.2  haad 	} ph_u;
    132  1.165.2.2  haad };
    133  1.165.2.2  haad #define	ph_itemlist	ph_u.phu_normal.phu_itemlist
    134  1.165.2.2  haad #define	ph_bitmap	ph_u.phu_notouch.phu_bitmap
    135  1.165.2.2  haad 
    136  1.165.2.2  haad struct pool_item {
    137  1.165.2.2  haad #ifdef DIAGNOSTIC
    138  1.165.2.2  haad 	u_int pi_magic;
    139  1.165.2.2  haad #endif
    140  1.165.2.2  haad #define	PI_MAGIC 0xdeaddeadU
    141  1.165.2.2  haad 	/* Other entries use only this list entry */
    142  1.165.2.2  haad 	LIST_ENTRY(pool_item)	pi_list;
    143  1.165.2.2  haad };
    144  1.165.2.2  haad 
    145  1.165.2.2  haad #define	POOL_NEEDS_CATCHUP(pp)						\
    146  1.165.2.2  haad 	((pp)->pr_nitems < (pp)->pr_minitems)
    147  1.165.2.2  haad 
    148  1.165.2.2  haad /*
    149  1.165.2.2  haad  * Pool cache management.
    150  1.165.2.2  haad  *
    151  1.165.2.2  haad  * Pool caches provide a way for constructed objects to be cached by the
    152  1.165.2.2  haad  * pool subsystem.  This can lead to performance improvements by avoiding
    153  1.165.2.2  haad  * needless object construction/destruction; it is deferred until absolutely
    154  1.165.2.2  haad  * necessary.
    155  1.165.2.2  haad  *
    156  1.165.2.2  haad  * Caches are grouped into cache groups.  Each cache group references up
    157  1.165.2.2  haad  * to PCG_NUMOBJECTS constructed objects.  When a cache allocates an
    158  1.165.2.2  haad  * object from the pool, it calls the object's constructor and places it
    159  1.165.2.2  haad  * into a cache group.  When a cache group frees an object back to the
    160  1.165.2.2  haad  * pool, it first calls the object's destructor.  This allows the object
    161  1.165.2.2  haad  * to persist in constructed form while freed to the cache.
    162  1.165.2.2  haad  *
    163  1.165.2.2  haad  * The pool references each cache, so that when a pool is drained by the
    164  1.165.2.2  haad  * pagedaemon, it can drain each individual cache as well.  Each time a
    165  1.165.2.2  haad  * cache is drained, the most idle cache group is freed to the pool in
    166  1.165.2.2  haad  * its entirety.
    167  1.165.2.2  haad  *
    168  1.165.2.2  haad  * Pool caches are layed on top of pools.  By layering them, we can avoid
    169  1.165.2.2  haad  * the complexity of cache management for pools which would not benefit
    170  1.165.2.2  haad  * from it.
    171  1.165.2.2  haad  */
    172  1.165.2.2  haad 
    173  1.165.2.2  haad static struct pool pcg_normal_pool;
    174  1.165.2.2  haad static struct pool pcg_large_pool;
    175  1.165.2.2  haad static struct pool cache_pool;
    176  1.165.2.2  haad static struct pool cache_cpu_pool;
    177  1.165.2.2  haad 
    178  1.165.2.2  haad /* List of all caches. */
    179  1.165.2.2  haad TAILQ_HEAD(,pool_cache) pool_cache_head =
    180  1.165.2.2  haad     TAILQ_HEAD_INITIALIZER(pool_cache_head);
    181  1.165.2.2  haad 
    182  1.165.2.2  haad int pool_cache_disable;		/* global disable for caching */
    183  1.165.2.2  haad static const pcg_t pcg_dummy;	/* zero sized: always empty, yet always full */
    184  1.165.2.2  haad 
    185  1.165.2.2  haad static bool	pool_cache_put_slow(pool_cache_cpu_t *, int,
    186  1.165.2.2  haad 				    void *);
    187  1.165.2.2  haad static bool	pool_cache_get_slow(pool_cache_cpu_t *, int,
    188  1.165.2.2  haad 				    void **, paddr_t *, int);
    189  1.165.2.2  haad static void	pool_cache_cpu_init1(struct cpu_info *, pool_cache_t);
    190  1.165.2.2  haad static void	pool_cache_invalidate_groups(pool_cache_t, pcg_t *);
    191  1.165.2.2  haad static void	pool_cache_xcall(pool_cache_t);
    192  1.165.2.2  haad 
    193  1.165.2.2  haad static int	pool_catchup(struct pool *);
    194  1.165.2.2  haad static void	pool_prime_page(struct pool *, void *,
    195  1.165.2.2  haad 		    struct pool_item_header *);
    196  1.165.2.2  haad static void	pool_update_curpage(struct pool *);
    197  1.165.2.2  haad 
    198  1.165.2.2  haad static int	pool_grow(struct pool *, int);
    199  1.165.2.2  haad static void	*pool_allocator_alloc(struct pool *, int);
    200  1.165.2.2  haad static void	pool_allocator_free(struct pool *, void *);
    201  1.165.2.2  haad 
    202  1.165.2.2  haad static void pool_print_pagelist(struct pool *, struct pool_pagelist *,
    203  1.165.2.2  haad 	void (*)(const char *, ...));
    204  1.165.2.2  haad static void pool_print1(struct pool *, const char *,
    205  1.165.2.2  haad 	void (*)(const char *, ...));
    206  1.165.2.2  haad 
    207  1.165.2.2  haad static int pool_chk_page(struct pool *, const char *,
    208  1.165.2.2  haad 			 struct pool_item_header *);
    209  1.165.2.2  haad 
    210  1.165.2.2  haad /*
    211  1.165.2.2  haad  * Pool log entry. An array of these is allocated in pool_init().
    212  1.165.2.2  haad  */
    213  1.165.2.2  haad struct pool_log {
    214  1.165.2.2  haad 	const char	*pl_file;
    215  1.165.2.2  haad 	long		pl_line;
    216  1.165.2.2  haad 	int		pl_action;
    217  1.165.2.2  haad #define	PRLOG_GET	1
    218  1.165.2.2  haad #define	PRLOG_PUT	2
    219  1.165.2.2  haad 	void		*pl_addr;
    220  1.165.2.2  haad };
    221  1.165.2.2  haad 
    222  1.165.2.2  haad #ifdef POOL_DIAGNOSTIC
    223  1.165.2.2  haad /* Number of entries in pool log buffers */
    224  1.165.2.2  haad #ifndef POOL_LOGSIZE
    225  1.165.2.2  haad #define	POOL_LOGSIZE	10
    226  1.165.2.2  haad #endif
    227  1.165.2.2  haad 
    228  1.165.2.2  haad int pool_logsize = POOL_LOGSIZE;
    229  1.165.2.2  haad 
    230  1.165.2.2  haad static inline void
    231  1.165.2.2  haad pr_log(struct pool *pp, void *v, int action, const char *file, long line)
    232  1.165.2.2  haad {
    233  1.165.2.2  haad 	int n = pp->pr_curlogentry;
    234  1.165.2.2  haad 	struct pool_log *pl;
    235  1.165.2.2  haad 
    236  1.165.2.2  haad 	if ((pp->pr_roflags & PR_LOGGING) == 0)
    237  1.165.2.2  haad 		return;
    238  1.165.2.2  haad 
    239  1.165.2.2  haad 	/*
    240  1.165.2.2  haad 	 * Fill in the current entry. Wrap around and overwrite
    241  1.165.2.2  haad 	 * the oldest entry if necessary.
    242  1.165.2.2  haad 	 */
    243  1.165.2.2  haad 	pl = &pp->pr_log[n];
    244  1.165.2.2  haad 	pl->pl_file = file;
    245  1.165.2.2  haad 	pl->pl_line = line;
    246  1.165.2.2  haad 	pl->pl_action = action;
    247  1.165.2.2  haad 	pl->pl_addr = v;
    248  1.165.2.2  haad 	if (++n >= pp->pr_logsize)
    249  1.165.2.2  haad 		n = 0;
    250  1.165.2.2  haad 	pp->pr_curlogentry = n;
    251  1.165.2.2  haad }
    252  1.165.2.2  haad 
    253  1.165.2.2  haad static void
    254  1.165.2.2  haad pr_printlog(struct pool *pp, struct pool_item *pi,
    255  1.165.2.2  haad     void (*pr)(const char *, ...))
    256  1.165.2.2  haad {
    257  1.165.2.2  haad 	int i = pp->pr_logsize;
    258  1.165.2.2  haad 	int n = pp->pr_curlogentry;
    259  1.165.2.2  haad 
    260  1.165.2.2  haad 	if ((pp->pr_roflags & PR_LOGGING) == 0)
    261  1.165.2.2  haad 		return;
    262  1.165.2.2  haad 
    263  1.165.2.2  haad 	/*
    264  1.165.2.2  haad 	 * Print all entries in this pool's log.
    265  1.165.2.2  haad 	 */
    266  1.165.2.2  haad 	while (i-- > 0) {
    267  1.165.2.2  haad 		struct pool_log *pl = &pp->pr_log[n];
    268  1.165.2.2  haad 		if (pl->pl_action != 0) {
    269  1.165.2.2  haad 			if (pi == NULL || pi == pl->pl_addr) {
    270  1.165.2.2  haad 				(*pr)("\tlog entry %d:\n", i);
    271  1.165.2.2  haad 				(*pr)("\t\taction = %s, addr = %p\n",
    272  1.165.2.2  haad 				    pl->pl_action == PRLOG_GET ? "get" : "put",
    273  1.165.2.2  haad 				    pl->pl_addr);
    274  1.165.2.2  haad 				(*pr)("\t\tfile: %s at line %lu\n",
    275  1.165.2.2  haad 				    pl->pl_file, pl->pl_line);
    276  1.165.2.2  haad 			}
    277  1.165.2.2  haad 		}
    278  1.165.2.2  haad 		if (++n >= pp->pr_logsize)
    279  1.165.2.2  haad 			n = 0;
    280  1.165.2.2  haad 	}
    281  1.165.2.2  haad }
    282  1.165.2.2  haad 
    283  1.165.2.2  haad static inline void
    284  1.165.2.2  haad pr_enter(struct pool *pp, const char *file, long line)
    285  1.165.2.2  haad {
    286  1.165.2.2  haad 
    287  1.165.2.2  haad 	if (__predict_false(pp->pr_entered_file != NULL)) {
    288  1.165.2.2  haad 		printf("pool %s: reentrancy at file %s line %ld\n",
    289  1.165.2.2  haad 		    pp->pr_wchan, file, line);
    290  1.165.2.2  haad 		printf("         previous entry at file %s line %ld\n",
    291  1.165.2.2  haad 		    pp->pr_entered_file, pp->pr_entered_line);
    292  1.165.2.2  haad 		panic("pr_enter");
    293  1.165.2.2  haad 	}
    294  1.165.2.2  haad 
    295  1.165.2.2  haad 	pp->pr_entered_file = file;
    296  1.165.2.2  haad 	pp->pr_entered_line = line;
    297  1.165.2.2  haad }
    298  1.165.2.2  haad 
    299  1.165.2.2  haad static inline void
    300  1.165.2.2  haad pr_leave(struct pool *pp)
    301  1.165.2.2  haad {
    302  1.165.2.2  haad 
    303  1.165.2.2  haad 	if (__predict_false(pp->pr_entered_file == NULL)) {
    304  1.165.2.2  haad 		printf("pool %s not entered?\n", pp->pr_wchan);
    305  1.165.2.2  haad 		panic("pr_leave");
    306  1.165.2.2  haad 	}
    307  1.165.2.2  haad 
    308  1.165.2.2  haad 	pp->pr_entered_file = NULL;
    309  1.165.2.2  haad 	pp->pr_entered_line = 0;
    310  1.165.2.2  haad }
    311  1.165.2.2  haad 
    312  1.165.2.2  haad static inline void
    313  1.165.2.2  haad pr_enter_check(struct pool *pp, void (*pr)(const char *, ...))
    314  1.165.2.2  haad {
    315  1.165.2.2  haad 
    316  1.165.2.2  haad 	if (pp->pr_entered_file != NULL)
    317  1.165.2.2  haad 		(*pr)("\n\tcurrently entered from file %s line %ld\n",
    318  1.165.2.2  haad 		    pp->pr_entered_file, pp->pr_entered_line);
    319  1.165.2.2  haad }
    320  1.165.2.2  haad #else
    321  1.165.2.2  haad #define	pr_log(pp, v, action, file, line)
    322  1.165.2.2  haad #define	pr_printlog(pp, pi, pr)
    323  1.165.2.2  haad #define	pr_enter(pp, file, line)
    324  1.165.2.2  haad #define	pr_leave(pp)
    325  1.165.2.2  haad #define	pr_enter_check(pp, pr)
    326  1.165.2.2  haad #endif /* POOL_DIAGNOSTIC */
    327  1.165.2.2  haad 
    328  1.165.2.2  haad static inline unsigned int
    329  1.165.2.2  haad pr_item_notouch_index(const struct pool *pp, const struct pool_item_header *ph,
    330  1.165.2.2  haad     const void *v)
    331  1.165.2.2  haad {
    332  1.165.2.2  haad 	const char *cp = v;
    333  1.165.2.2  haad 	unsigned int idx;
    334  1.165.2.2  haad 
    335  1.165.2.2  haad 	KASSERT(pp->pr_roflags & PR_NOTOUCH);
    336  1.165.2.2  haad 	idx = (cp - (char *)ph->ph_page - ph->ph_off) / pp->pr_size;
    337  1.165.2.2  haad 	KASSERT(idx < pp->pr_itemsperpage);
    338  1.165.2.2  haad 	return idx;
    339  1.165.2.2  haad }
    340  1.165.2.2  haad 
    341  1.165.2.2  haad static inline void
    342  1.165.2.2  haad pr_item_notouch_put(const struct pool *pp, struct pool_item_header *ph,
    343  1.165.2.2  haad     void *obj)
    344  1.165.2.2  haad {
    345  1.165.2.2  haad 	unsigned int idx = pr_item_notouch_index(pp, ph, obj);
    346  1.165.2.2  haad 	pool_item_bitmap_t *bitmap = ph->ph_bitmap + (idx / BITMAP_SIZE);
    347  1.165.2.2  haad 	pool_item_bitmap_t mask = 1 << (idx & BITMAP_MASK);
    348  1.165.2.2  haad 
    349  1.165.2.2  haad 	KASSERT((*bitmap & mask) == 0);
    350  1.165.2.2  haad 	*bitmap |= mask;
    351  1.165.2.2  haad }
    352  1.165.2.2  haad 
    353  1.165.2.2  haad static inline void *
    354  1.165.2.2  haad pr_item_notouch_get(const struct pool *pp, struct pool_item_header *ph)
    355  1.165.2.2  haad {
    356  1.165.2.2  haad 	pool_item_bitmap_t *bitmap = ph->ph_bitmap;
    357  1.165.2.2  haad 	unsigned int idx;
    358  1.165.2.2  haad 	int i;
    359  1.165.2.2  haad 
    360  1.165.2.2  haad 	for (i = 0; ; i++) {
    361  1.165.2.2  haad 		int bit;
    362  1.165.2.2  haad 
    363  1.165.2.2  haad 		KASSERT((i * BITMAP_SIZE) < pp->pr_itemsperpage);
    364  1.165.2.2  haad 		bit = ffs32(bitmap[i]);
    365  1.165.2.2  haad 		if (bit) {
    366  1.165.2.2  haad 			pool_item_bitmap_t mask;
    367  1.165.2.2  haad 
    368  1.165.2.2  haad 			bit--;
    369  1.165.2.2  haad 			idx = (i * BITMAP_SIZE) + bit;
    370  1.165.2.2  haad 			mask = 1 << bit;
    371  1.165.2.2  haad 			KASSERT((bitmap[i] & mask) != 0);
    372  1.165.2.2  haad 			bitmap[i] &= ~mask;
    373  1.165.2.2  haad 			break;
    374  1.165.2.2  haad 		}
    375  1.165.2.2  haad 	}
    376  1.165.2.2  haad 	KASSERT(idx < pp->pr_itemsperpage);
    377  1.165.2.2  haad 	return (char *)ph->ph_page + ph->ph_off + idx * pp->pr_size;
    378  1.165.2.2  haad }
    379  1.165.2.2  haad 
    380  1.165.2.2  haad static inline void
    381  1.165.2.2  haad pr_item_notouch_init(const struct pool *pp, struct pool_item_header *ph)
    382  1.165.2.2  haad {
    383  1.165.2.2  haad 	pool_item_bitmap_t *bitmap = ph->ph_bitmap;
    384  1.165.2.2  haad 	const int n = howmany(pp->pr_itemsperpage, BITMAP_SIZE);
    385  1.165.2.2  haad 	int i;
    386  1.165.2.2  haad 
    387  1.165.2.2  haad 	for (i = 0; i < n; i++) {
    388  1.165.2.2  haad 		bitmap[i] = (pool_item_bitmap_t)-1;
    389  1.165.2.2  haad 	}
    390  1.165.2.2  haad }
    391  1.165.2.2  haad 
    392  1.165.2.2  haad static inline int
    393  1.165.2.2  haad phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
    394  1.165.2.2  haad {
    395  1.165.2.2  haad 
    396  1.165.2.2  haad 	/*
    397  1.165.2.2  haad 	 * we consider pool_item_header with smaller ph_page bigger.
    398  1.165.2.2  haad 	 * (this unnatural ordering is for the benefit of pr_find_pagehead.)
    399  1.165.2.2  haad 	 */
    400  1.165.2.2  haad 
    401  1.165.2.2  haad 	if (a->ph_page < b->ph_page)
    402  1.165.2.2  haad 		return (1);
    403  1.165.2.2  haad 	else if (a->ph_page > b->ph_page)
    404  1.165.2.2  haad 		return (-1);
    405  1.165.2.2  haad 	else
    406  1.165.2.2  haad 		return (0);
    407  1.165.2.2  haad }
    408  1.165.2.2  haad 
    409  1.165.2.2  haad SPLAY_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare);
    410  1.165.2.2  haad SPLAY_GENERATE(phtree, pool_item_header, ph_node, phtree_compare);
    411  1.165.2.2  haad 
    412  1.165.2.2  haad static inline struct pool_item_header *
    413  1.165.2.2  haad pr_find_pagehead_noalign(struct pool *pp, void *v)
    414  1.165.2.2  haad {
    415  1.165.2.2  haad 	struct pool_item_header *ph, tmp;
    416  1.165.2.2  haad 
    417  1.165.2.2  haad 	tmp.ph_page = (void *)(uintptr_t)v;
    418  1.165.2.2  haad 	ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
    419  1.165.2.2  haad 	if (ph == NULL) {
    420  1.165.2.2  haad 		ph = SPLAY_ROOT(&pp->pr_phtree);
    421  1.165.2.2  haad 		if (ph != NULL && phtree_compare(&tmp, ph) >= 0) {
    422  1.165.2.2  haad 			ph = SPLAY_NEXT(phtree, &pp->pr_phtree, ph);
    423  1.165.2.2  haad 		}
    424  1.165.2.2  haad 		KASSERT(ph == NULL || phtree_compare(&tmp, ph) < 0);
    425  1.165.2.2  haad 	}
    426  1.165.2.2  haad 
    427  1.165.2.2  haad 	return ph;
    428  1.165.2.2  haad }
    429  1.165.2.2  haad 
    430  1.165.2.2  haad /*
    431  1.165.2.2  haad  * Return the pool page header based on item address.
    432  1.165.2.2  haad  */
    433  1.165.2.2  haad static inline struct pool_item_header *
    434  1.165.2.2  haad pr_find_pagehead(struct pool *pp, void *v)
    435  1.165.2.2  haad {
    436  1.165.2.2  haad 	struct pool_item_header *ph, tmp;
    437  1.165.2.2  haad 
    438  1.165.2.2  haad 	if ((pp->pr_roflags & PR_NOALIGN) != 0) {
    439  1.165.2.2  haad 		ph = pr_find_pagehead_noalign(pp, v);
    440  1.165.2.2  haad 	} else {
    441  1.165.2.2  haad 		void *page =
    442  1.165.2.2  haad 		    (void *)((uintptr_t)v & pp->pr_alloc->pa_pagemask);
    443  1.165.2.2  haad 
    444  1.165.2.2  haad 		if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
    445  1.165.2.2  haad 			ph = (struct pool_item_header *)((char *)page + pp->pr_phoffset);
    446  1.165.2.2  haad 		} else {
    447  1.165.2.2  haad 			tmp.ph_page = page;
    448  1.165.2.2  haad 			ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
    449  1.165.2.2  haad 		}
    450  1.165.2.2  haad 	}
    451  1.165.2.2  haad 
    452  1.165.2.2  haad 	KASSERT(ph == NULL || ((pp->pr_roflags & PR_PHINPAGE) != 0) ||
    453  1.165.2.2  haad 	    ((char *)ph->ph_page <= (char *)v &&
    454  1.165.2.2  haad 	    (char *)v < (char *)ph->ph_page + pp->pr_alloc->pa_pagesz));
    455  1.165.2.2  haad 	return ph;
    456  1.165.2.2  haad }
    457  1.165.2.2  haad 
    458  1.165.2.2  haad static void
    459  1.165.2.2  haad pr_pagelist_free(struct pool *pp, struct pool_pagelist *pq)
    460  1.165.2.2  haad {
    461  1.165.2.2  haad 	struct pool_item_header *ph;
    462  1.165.2.2  haad 
    463  1.165.2.2  haad 	while ((ph = LIST_FIRST(pq)) != NULL) {
    464  1.165.2.2  haad 		LIST_REMOVE(ph, ph_pagelist);
    465  1.165.2.2  haad 		pool_allocator_free(pp, ph->ph_page);
    466  1.165.2.2  haad 		if ((pp->pr_roflags & PR_PHINPAGE) == 0)
    467  1.165.2.2  haad 			pool_put(pp->pr_phpool, ph);
    468  1.165.2.2  haad 	}
    469  1.165.2.2  haad }
    470  1.165.2.2  haad 
    471  1.165.2.2  haad /*
    472  1.165.2.2  haad  * Remove a page from the pool.
    473  1.165.2.2  haad  */
    474  1.165.2.2  haad static inline void
    475  1.165.2.2  haad pr_rmpage(struct pool *pp, struct pool_item_header *ph,
    476  1.165.2.2  haad      struct pool_pagelist *pq)
    477  1.165.2.2  haad {
    478  1.165.2.2  haad 
    479  1.165.2.2  haad 	KASSERT(mutex_owned(&pp->pr_lock));
    480  1.165.2.2  haad 
    481  1.165.2.2  haad 	/*
    482  1.165.2.2  haad 	 * If the page was idle, decrement the idle page count.
    483  1.165.2.2  haad 	 */
    484  1.165.2.2  haad 	if (ph->ph_nmissing == 0) {
    485  1.165.2.2  haad #ifdef DIAGNOSTIC
    486  1.165.2.2  haad 		if (pp->pr_nidle == 0)
    487  1.165.2.2  haad 			panic("pr_rmpage: nidle inconsistent");
    488  1.165.2.2  haad 		if (pp->pr_nitems < pp->pr_itemsperpage)
    489  1.165.2.2  haad 			panic("pr_rmpage: nitems inconsistent");
    490  1.165.2.2  haad #endif
    491  1.165.2.2  haad 		pp->pr_nidle--;
    492  1.165.2.2  haad 	}
    493  1.165.2.2  haad 
    494  1.165.2.2  haad 	pp->pr_nitems -= pp->pr_itemsperpage;
    495  1.165.2.2  haad 
    496  1.165.2.2  haad 	/*
    497  1.165.2.2  haad 	 * Unlink the page from the pool and queue it for release.
    498  1.165.2.2  haad 	 */
    499  1.165.2.2  haad 	LIST_REMOVE(ph, ph_pagelist);
    500  1.165.2.2  haad 	if ((pp->pr_roflags & PR_PHINPAGE) == 0)
    501  1.165.2.2  haad 		SPLAY_REMOVE(phtree, &pp->pr_phtree, ph);
    502  1.165.2.2  haad 	LIST_INSERT_HEAD(pq, ph, ph_pagelist);
    503  1.165.2.2  haad 
    504  1.165.2.2  haad 	pp->pr_npages--;
    505  1.165.2.2  haad 	pp->pr_npagefree++;
    506  1.165.2.2  haad 
    507  1.165.2.2  haad 	pool_update_curpage(pp);
    508  1.165.2.2  haad }
    509  1.165.2.2  haad 
    510  1.165.2.2  haad static bool
    511  1.165.2.2  haad pa_starved_p(struct pool_allocator *pa)
    512  1.165.2.2  haad {
    513  1.165.2.2  haad 
    514  1.165.2.2  haad 	if (pa->pa_backingmap != NULL) {
    515  1.165.2.2  haad 		return vm_map_starved_p(pa->pa_backingmap);
    516  1.165.2.2  haad 	}
    517  1.165.2.2  haad 	return false;
    518  1.165.2.2  haad }
    519  1.165.2.2  haad 
    520  1.165.2.2  haad static int
    521  1.165.2.2  haad pool_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
    522  1.165.2.2  haad {
    523  1.165.2.2  haad 	struct pool *pp = obj;
    524  1.165.2.2  haad 	struct pool_allocator *pa = pp->pr_alloc;
    525  1.165.2.2  haad 
    526  1.165.2.2  haad 	KASSERT(&pp->pr_reclaimerentry == ce);
    527  1.165.2.2  haad 	pool_reclaim(pp);
    528  1.165.2.2  haad 	if (!pa_starved_p(pa)) {
    529  1.165.2.2  haad 		return CALLBACK_CHAIN_ABORT;
    530  1.165.2.2  haad 	}
    531  1.165.2.2  haad 	return CALLBACK_CHAIN_CONTINUE;
    532  1.165.2.2  haad }
    533  1.165.2.2  haad 
    534  1.165.2.2  haad static void
    535  1.165.2.2  haad pool_reclaim_register(struct pool *pp)
    536  1.165.2.2  haad {
    537  1.165.2.2  haad 	struct vm_map *map = pp->pr_alloc->pa_backingmap;
    538  1.165.2.2  haad 	int s;
    539  1.165.2.2  haad 
    540  1.165.2.2  haad 	if (map == NULL) {
    541  1.165.2.2  haad 		return;
    542  1.165.2.2  haad 	}
    543  1.165.2.2  haad 
    544  1.165.2.2  haad 	s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
    545  1.165.2.2  haad 	callback_register(&vm_map_to_kernel(map)->vmk_reclaim_callback,
    546  1.165.2.2  haad 	    &pp->pr_reclaimerentry, pp, pool_reclaim_callback);
    547  1.165.2.2  haad 	splx(s);
    548  1.165.2.2  haad }
    549  1.165.2.2  haad 
    550  1.165.2.2  haad static void
    551  1.165.2.2  haad pool_reclaim_unregister(struct pool *pp)
    552  1.165.2.2  haad {
    553  1.165.2.2  haad 	struct vm_map *map = pp->pr_alloc->pa_backingmap;
    554  1.165.2.2  haad 	int s;
    555  1.165.2.2  haad 
    556  1.165.2.2  haad 	if (map == NULL) {
    557  1.165.2.2  haad 		return;
    558  1.165.2.2  haad 	}
    559  1.165.2.2  haad 
    560  1.165.2.2  haad 	s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
    561  1.165.2.2  haad 	callback_unregister(&vm_map_to_kernel(map)->vmk_reclaim_callback,
    562  1.165.2.2  haad 	    &pp->pr_reclaimerentry);
    563  1.165.2.2  haad 	splx(s);
    564  1.165.2.2  haad }
    565  1.165.2.2  haad 
    566  1.165.2.2  haad static void
    567  1.165.2.2  haad pa_reclaim_register(struct pool_allocator *pa)
    568  1.165.2.2  haad {
    569  1.165.2.2  haad 	struct vm_map *map = *pa->pa_backingmapptr;
    570  1.165.2.2  haad 	struct pool *pp;
    571  1.165.2.2  haad 
    572  1.165.2.2  haad 	KASSERT(pa->pa_backingmap == NULL);
    573  1.165.2.2  haad 	if (map == NULL) {
    574  1.165.2.2  haad 		SLIST_INSERT_HEAD(&pa_deferinitq, pa, pa_q);
    575  1.165.2.2  haad 		return;
    576  1.165.2.2  haad 	}
    577  1.165.2.2  haad 	pa->pa_backingmap = map;
    578  1.165.2.2  haad 	TAILQ_FOREACH(pp, &pa->pa_list, pr_alloc_list) {
    579  1.165.2.2  haad 		pool_reclaim_register(pp);
    580  1.165.2.2  haad 	}
    581  1.165.2.2  haad }
    582  1.165.2.2  haad 
    583  1.165.2.2  haad /*
    584  1.165.2.2  haad  * Initialize all the pools listed in the "pools" link set.
    585  1.165.2.2  haad  */
    586  1.165.2.2  haad void
    587  1.165.2.2  haad pool_subsystem_init(void)
    588  1.165.2.2  haad {
    589  1.165.2.2  haad 	struct pool_allocator *pa;
    590  1.165.2.2  haad 	__link_set_decl(pools, struct link_pool_init);
    591  1.165.2.2  haad 	struct link_pool_init * const *pi;
    592  1.165.2.2  haad 
    593  1.165.2.2  haad 	mutex_init(&pool_head_lock, MUTEX_DEFAULT, IPL_NONE);
    594  1.165.2.2  haad 	cv_init(&pool_busy, "poolbusy");
    595  1.165.2.2  haad 
    596  1.165.2.2  haad 	__link_set_foreach(pi, pools)
    597  1.165.2.2  haad 		pool_init((*pi)->pp, (*pi)->size, (*pi)->align,
    598  1.165.2.2  haad 		    (*pi)->align_offset, (*pi)->flags, (*pi)->wchan,
    599  1.165.2.2  haad 		    (*pi)->palloc, (*pi)->ipl);
    600  1.165.2.2  haad 
    601  1.165.2.2  haad 	while ((pa = SLIST_FIRST(&pa_deferinitq)) != NULL) {
    602  1.165.2.2  haad 		KASSERT(pa->pa_backingmapptr != NULL);
    603  1.165.2.2  haad 		KASSERT(*pa->pa_backingmapptr != NULL);
    604  1.165.2.2  haad 		SLIST_REMOVE_HEAD(&pa_deferinitq, pa_q);
    605  1.165.2.2  haad 		pa_reclaim_register(pa);
    606  1.165.2.2  haad 	}
    607  1.165.2.2  haad 
    608  1.165.2.2  haad 	pool_init(&cache_pool, sizeof(struct pool_cache), coherency_unit,
    609  1.165.2.2  haad 	    0, 0, "pcache", &pool_allocator_nointr, IPL_NONE);
    610  1.165.2.2  haad 
    611  1.165.2.2  haad 	pool_init(&cache_cpu_pool, sizeof(pool_cache_cpu_t), coherency_unit,
    612  1.165.2.2  haad 	    0, 0, "pcachecpu", &pool_allocator_nointr, IPL_NONE);
    613  1.165.2.2  haad }
    614  1.165.2.2  haad 
    615  1.165.2.2  haad /*
    616  1.165.2.2  haad  * Initialize the given pool resource structure.
    617  1.165.2.2  haad  *
    618  1.165.2.2  haad  * We export this routine to allow other kernel parts to declare
    619  1.165.2.2  haad  * static pools that must be initialized before malloc() is available.
    620  1.165.2.2  haad  */
    621  1.165.2.2  haad void
    622  1.165.2.2  haad pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
    623  1.165.2.2  haad     const char *wchan, struct pool_allocator *palloc, int ipl)
    624  1.165.2.2  haad {
    625  1.165.2.2  haad 	struct pool *pp1;
    626  1.165.2.2  haad 	size_t trysize, phsize;
    627  1.165.2.2  haad 	int off, slack;
    628  1.165.2.2  haad 
    629  1.165.2.2  haad #ifdef DEBUG
    630  1.165.2.2  haad 	/*
    631  1.165.2.2  haad 	 * Check that the pool hasn't already been initialised and
    632  1.165.2.2  haad 	 * added to the list of all pools.
    633  1.165.2.2  haad 	 */
    634  1.165.2.2  haad 	TAILQ_FOREACH(pp1, &pool_head, pr_poollist) {
    635  1.165.2.2  haad 		if (pp == pp1)
    636  1.165.2.2  haad 			panic("pool_init: pool %s already initialised",
    637  1.165.2.2  haad 			    wchan);
    638  1.165.2.2  haad 	}
    639  1.165.2.2  haad #endif
    640  1.165.2.2  haad 
    641  1.165.2.2  haad #ifdef POOL_DIAGNOSTIC
    642  1.165.2.2  haad 	/*
    643  1.165.2.2  haad 	 * Always log if POOL_DIAGNOSTIC is defined.
    644  1.165.2.2  haad 	 */
    645  1.165.2.2  haad 	if (pool_logsize != 0)
    646  1.165.2.2  haad 		flags |= PR_LOGGING;
    647  1.165.2.2  haad #endif
    648  1.165.2.2  haad 
    649  1.165.2.2  haad 	if (palloc == NULL)
    650  1.165.2.2  haad 		palloc = &pool_allocator_kmem;
    651  1.165.2.2  haad #ifdef POOL_SUBPAGE
    652  1.165.2.2  haad 	if (size > palloc->pa_pagesz) {
    653  1.165.2.2  haad 		if (palloc == &pool_allocator_kmem)
    654  1.165.2.2  haad 			palloc = &pool_allocator_kmem_fullpage;
    655  1.165.2.2  haad 		else if (palloc == &pool_allocator_nointr)
    656  1.165.2.2  haad 			palloc = &pool_allocator_nointr_fullpage;
    657  1.165.2.2  haad 	}
    658  1.165.2.2  haad #endif /* POOL_SUBPAGE */
    659  1.165.2.2  haad 	if ((palloc->pa_flags & PA_INITIALIZED) == 0) {
    660  1.165.2.2  haad 		if (palloc->pa_pagesz == 0)
    661  1.165.2.2  haad 			palloc->pa_pagesz = PAGE_SIZE;
    662  1.165.2.2  haad 
    663  1.165.2.2  haad 		TAILQ_INIT(&palloc->pa_list);
    664  1.165.2.2  haad 
    665  1.165.2.2  haad 		mutex_init(&palloc->pa_lock, MUTEX_DEFAULT, IPL_VM);
    666  1.165.2.2  haad 		palloc->pa_pagemask = ~(palloc->pa_pagesz - 1);
    667  1.165.2.2  haad 		palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1;
    668  1.165.2.2  haad 
    669  1.165.2.2  haad 		if (palloc->pa_backingmapptr != NULL) {
    670  1.165.2.2  haad 			pa_reclaim_register(palloc);
    671  1.165.2.2  haad 		}
    672  1.165.2.2  haad 		palloc->pa_flags |= PA_INITIALIZED;
    673  1.165.2.2  haad 	}
    674  1.165.2.2  haad 
    675  1.165.2.2  haad 	if (align == 0)
    676  1.165.2.2  haad 		align = ALIGN(1);
    677  1.165.2.2  haad 
    678  1.165.2.2  haad 	if ((flags & PR_NOTOUCH) == 0 && size < sizeof(struct pool_item))
    679  1.165.2.2  haad 		size = sizeof(struct pool_item);
    680  1.165.2.2  haad 
    681  1.165.2.2  haad 	size = roundup(size, align);
    682  1.165.2.2  haad #ifdef DIAGNOSTIC
    683  1.165.2.2  haad 	if (size > palloc->pa_pagesz)
    684  1.165.2.2  haad 		panic("pool_init: pool item size (%zu) too large", size);
    685  1.165.2.2  haad #endif
    686  1.165.2.2  haad 
    687  1.165.2.2  haad 	/*
    688  1.165.2.2  haad 	 * Initialize the pool structure.
    689  1.165.2.2  haad 	 */
    690  1.165.2.2  haad 	LIST_INIT(&pp->pr_emptypages);
    691  1.165.2.2  haad 	LIST_INIT(&pp->pr_fullpages);
    692  1.165.2.2  haad 	LIST_INIT(&pp->pr_partpages);
    693  1.165.2.2  haad 	pp->pr_cache = NULL;
    694  1.165.2.2  haad 	pp->pr_curpage = NULL;
    695  1.165.2.2  haad 	pp->pr_npages = 0;
    696  1.165.2.2  haad 	pp->pr_minitems = 0;
    697  1.165.2.2  haad 	pp->pr_minpages = 0;
    698  1.165.2.2  haad 	pp->pr_maxpages = UINT_MAX;
    699  1.165.2.2  haad 	pp->pr_roflags = flags;
    700  1.165.2.2  haad 	pp->pr_flags = 0;
    701  1.165.2.2  haad 	pp->pr_size = size;
    702  1.165.2.2  haad 	pp->pr_align = align;
    703  1.165.2.2  haad 	pp->pr_wchan = wchan;
    704  1.165.2.2  haad 	pp->pr_alloc = palloc;
    705  1.165.2.2  haad 	pp->pr_nitems = 0;
    706  1.165.2.2  haad 	pp->pr_nout = 0;
    707  1.165.2.2  haad 	pp->pr_hardlimit = UINT_MAX;
    708  1.165.2.2  haad 	pp->pr_hardlimit_warning = NULL;
    709  1.165.2.2  haad 	pp->pr_hardlimit_ratecap.tv_sec = 0;
    710  1.165.2.2  haad 	pp->pr_hardlimit_ratecap.tv_usec = 0;
    711  1.165.2.2  haad 	pp->pr_hardlimit_warning_last.tv_sec = 0;
    712  1.165.2.2  haad 	pp->pr_hardlimit_warning_last.tv_usec = 0;
    713  1.165.2.2  haad 	pp->pr_drain_hook = NULL;
    714  1.165.2.2  haad 	pp->pr_drain_hook_arg = NULL;
    715  1.165.2.2  haad 	pp->pr_freecheck = NULL;
    716  1.165.2.2  haad 
    717  1.165.2.2  haad 	/*
    718  1.165.2.2  haad 	 * Decide whether to put the page header off page to avoid
    719  1.165.2.2  haad 	 * wasting too large a part of the page or too big item.
    720  1.165.2.2  haad 	 * Off-page page headers go on a hash table, so we can match
    721  1.165.2.2  haad 	 * a returned item with its header based on the page address.
    722  1.165.2.2  haad 	 * We use 1/16 of the page size and about 8 times of the item
    723  1.165.2.2  haad 	 * size as the threshold (XXX: tune)
    724  1.165.2.2  haad 	 *
    725  1.165.2.2  haad 	 * However, we'll put the header into the page if we can put
    726  1.165.2.2  haad 	 * it without wasting any items.
    727  1.165.2.2  haad 	 *
    728  1.165.2.2  haad 	 * Silently enforce `0 <= ioff < align'.
    729  1.165.2.2  haad 	 */
    730  1.165.2.2  haad 	pp->pr_itemoffset = ioff %= align;
    731  1.165.2.2  haad 	/* See the comment below about reserved bytes. */
    732  1.165.2.2  haad 	trysize = palloc->pa_pagesz - ((align - ioff) % align);
    733  1.165.2.2  haad 	phsize = ALIGN(sizeof(struct pool_item_header));
    734  1.165.2.2  haad 	if ((pp->pr_roflags & (PR_NOTOUCH | PR_NOALIGN)) == 0 &&
    735  1.165.2.2  haad 	    (pp->pr_size < MIN(palloc->pa_pagesz / 16, phsize << 3) ||
    736  1.165.2.2  haad 	    trysize / pp->pr_size == (trysize - phsize) / pp->pr_size)) {
    737  1.165.2.2  haad 		/* Use the end of the page for the page header */
    738  1.165.2.2  haad 		pp->pr_roflags |= PR_PHINPAGE;
    739  1.165.2.2  haad 		pp->pr_phoffset = off = palloc->pa_pagesz - phsize;
    740  1.165.2.2  haad 	} else {
    741  1.165.2.2  haad 		/* The page header will be taken from our page header pool */
    742  1.165.2.2  haad 		pp->pr_phoffset = 0;
    743  1.165.2.2  haad 		off = palloc->pa_pagesz;
    744  1.165.2.2  haad 		SPLAY_INIT(&pp->pr_phtree);
    745  1.165.2.2  haad 	}
    746  1.165.2.2  haad 
    747  1.165.2.2  haad 	/*
    748  1.165.2.2  haad 	 * Alignment is to take place at `ioff' within the item. This means
    749  1.165.2.2  haad 	 * we must reserve up to `align - 1' bytes on the page to allow
    750  1.165.2.2  haad 	 * appropriate positioning of each item.
    751  1.165.2.2  haad 	 */
    752  1.165.2.2  haad 	pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
    753  1.165.2.2  haad 	KASSERT(pp->pr_itemsperpage != 0);
    754  1.165.2.2  haad 	if ((pp->pr_roflags & PR_NOTOUCH)) {
    755  1.165.2.2  haad 		int idx;
    756  1.165.2.2  haad 
    757  1.165.2.2  haad 		for (idx = 0; pp->pr_itemsperpage > PHPOOL_FREELIST_NELEM(idx);
    758  1.165.2.2  haad 		    idx++) {
    759  1.165.2.2  haad 			/* nothing */
    760  1.165.2.2  haad 		}
    761  1.165.2.2  haad 		if (idx >= PHPOOL_MAX) {
    762  1.165.2.2  haad 			/*
    763  1.165.2.2  haad 			 * if you see this panic, consider to tweak
    764  1.165.2.2  haad 			 * PHPOOL_MAX and PHPOOL_FREELIST_NELEM.
    765  1.165.2.2  haad 			 */
    766  1.165.2.2  haad 			panic("%s: too large itemsperpage(%d) for PR_NOTOUCH",
    767  1.165.2.2  haad 			    pp->pr_wchan, pp->pr_itemsperpage);
    768  1.165.2.2  haad 		}
    769  1.165.2.2  haad 		pp->pr_phpool = &phpool[idx];
    770  1.165.2.2  haad 	} else if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
    771  1.165.2.2  haad 		pp->pr_phpool = &phpool[0];
    772  1.165.2.2  haad 	}
    773  1.165.2.2  haad #if defined(DIAGNOSTIC)
    774  1.165.2.2  haad 	else {
    775  1.165.2.2  haad 		pp->pr_phpool = NULL;
    776  1.165.2.2  haad 	}
    777  1.165.2.2  haad #endif
    778  1.165.2.2  haad 
    779  1.165.2.2  haad 	/*
    780  1.165.2.2  haad 	 * Use the slack between the chunks and the page header
    781  1.165.2.2  haad 	 * for "cache coloring".
    782  1.165.2.2  haad 	 */
    783  1.165.2.2  haad 	slack = off - pp->pr_itemsperpage * pp->pr_size;
    784  1.165.2.2  haad 	pp->pr_maxcolor = (slack / align) * align;
    785  1.165.2.2  haad 	pp->pr_curcolor = 0;
    786  1.165.2.2  haad 
    787  1.165.2.2  haad 	pp->pr_nget = 0;
    788  1.165.2.2  haad 	pp->pr_nfail = 0;
    789  1.165.2.2  haad 	pp->pr_nput = 0;
    790  1.165.2.2  haad 	pp->pr_npagealloc = 0;
    791  1.165.2.2  haad 	pp->pr_npagefree = 0;
    792  1.165.2.2  haad 	pp->pr_hiwat = 0;
    793  1.165.2.2  haad 	pp->pr_nidle = 0;
    794  1.165.2.2  haad 	pp->pr_refcnt = 0;
    795  1.165.2.2  haad 
    796  1.165.2.2  haad #ifdef POOL_DIAGNOSTIC
    797  1.165.2.2  haad 	if (flags & PR_LOGGING) {
    798  1.165.2.2  haad 		if (kmem_map == NULL ||
    799  1.165.2.2  haad 		    (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
    800  1.165.2.2  haad 		     M_TEMP, M_NOWAIT)) == NULL)
    801  1.165.2.2  haad 			pp->pr_roflags &= ~PR_LOGGING;
    802  1.165.2.2  haad 		pp->pr_curlogentry = 0;
    803  1.165.2.2  haad 		pp->pr_logsize = pool_logsize;
    804  1.165.2.2  haad 	}
    805  1.165.2.2  haad #endif
    806  1.165.2.2  haad 
    807  1.165.2.2  haad 	pp->pr_entered_file = NULL;
    808  1.165.2.2  haad 	pp->pr_entered_line = 0;
    809  1.165.2.2  haad 
    810  1.165.2.2  haad 	mutex_init(&pp->pr_lock, MUTEX_DEFAULT, ipl);
    811  1.165.2.2  haad 	cv_init(&pp->pr_cv, wchan);
    812  1.165.2.2  haad 	pp->pr_ipl = ipl;
    813  1.165.2.2  haad 
    814  1.165.2.2  haad 	/*
    815  1.165.2.2  haad 	 * Initialize private page header pool and cache magazine pool if we
    816  1.165.2.2  haad 	 * haven't done so yet.
    817  1.165.2.2  haad 	 * XXX LOCKING.
    818  1.165.2.2  haad 	 */
    819  1.165.2.2  haad 	if (phpool[0].pr_size == 0) {
    820  1.165.2.2  haad 		int idx;
    821  1.165.2.2  haad 		for (idx = 0; idx < PHPOOL_MAX; idx++) {
    822  1.165.2.2  haad 			static char phpool_names[PHPOOL_MAX][6+1+6+1];
    823  1.165.2.2  haad 			int nelem;
    824  1.165.2.2  haad 			size_t sz;
    825  1.165.2.2  haad 
    826  1.165.2.2  haad 			nelem = PHPOOL_FREELIST_NELEM(idx);
    827  1.165.2.2  haad 			snprintf(phpool_names[idx], sizeof(phpool_names[idx]),
    828  1.165.2.2  haad 			    "phpool-%d", nelem);
    829  1.165.2.2  haad 			sz = sizeof(struct pool_item_header);
    830  1.165.2.2  haad 			if (nelem) {
    831  1.165.2.2  haad 				sz = offsetof(struct pool_item_header,
    832  1.165.2.2  haad 				    ph_bitmap[howmany(nelem, BITMAP_SIZE)]);
    833  1.165.2.2  haad 			}
    834  1.165.2.2  haad 			pool_init(&phpool[idx], sz, 0, 0, 0,
    835  1.165.2.2  haad 			    phpool_names[idx], &pool_allocator_meta, IPL_VM);
    836  1.165.2.2  haad 		}
    837  1.165.2.2  haad #ifdef POOL_SUBPAGE
    838  1.165.2.2  haad 		pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0,
    839  1.165.2.2  haad 		    PR_RECURSIVE, "psppool", &pool_allocator_meta, IPL_VM);
    840  1.165.2.2  haad #endif
    841  1.165.2.2  haad 
    842  1.165.2.2  haad 		size = sizeof(pcg_t) +
    843  1.165.2.2  haad 		    (PCG_NOBJECTS_NORMAL - 1) * sizeof(pcgpair_t);
    844  1.165.2.2  haad 		pool_init(&pcg_normal_pool, size, coherency_unit, 0, 0,
    845  1.165.2.2  haad 		    "pcgnormal", &pool_allocator_meta, IPL_VM);
    846  1.165.2.2  haad 
    847  1.165.2.2  haad 		size = sizeof(pcg_t) +
    848  1.165.2.2  haad 		    (PCG_NOBJECTS_LARGE - 1) * sizeof(pcgpair_t);
    849  1.165.2.2  haad 		pool_init(&pcg_large_pool, size, coherency_unit, 0, 0,
    850  1.165.2.2  haad 		    "pcglarge", &pool_allocator_meta, IPL_VM);
    851  1.165.2.2  haad 	}
    852  1.165.2.2  haad 
    853  1.165.2.2  haad 	/* Insert into the list of all pools. */
    854  1.165.2.2  haad 	if (__predict_true(!cold))
    855  1.165.2.2  haad 		mutex_enter(&pool_head_lock);
    856  1.165.2.2  haad 	TAILQ_FOREACH(pp1, &pool_head, pr_poollist) {
    857  1.165.2.2  haad 		if (strcmp(pp1->pr_wchan, pp->pr_wchan) > 0)
    858  1.165.2.2  haad 			break;
    859  1.165.2.2  haad 	}
    860  1.165.2.2  haad 	if (pp1 == NULL)
    861  1.165.2.2  haad 		TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
    862  1.165.2.2  haad 	else
    863  1.165.2.2  haad 		TAILQ_INSERT_BEFORE(pp1, pp, pr_poollist);
    864  1.165.2.2  haad 	if (__predict_true(!cold))
    865  1.165.2.2  haad 		mutex_exit(&pool_head_lock);
    866  1.165.2.2  haad 
    867  1.165.2.2  haad 	/* Insert this into the list of pools using this allocator. */
    868  1.165.2.2  haad 	if (__predict_true(!cold))
    869  1.165.2.2  haad 		mutex_enter(&palloc->pa_lock);
    870  1.165.2.2  haad 	TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
    871  1.165.2.2  haad 	if (__predict_true(!cold))
    872  1.165.2.2  haad 		mutex_exit(&palloc->pa_lock);
    873  1.165.2.2  haad 
    874  1.165.2.2  haad 	pool_reclaim_register(pp);
    875  1.165.2.2  haad }
    876  1.165.2.2  haad 
    877  1.165.2.2  haad /*
    878  1.165.2.2  haad  * De-commision a pool resource.
    879  1.165.2.2  haad  */
    880  1.165.2.2  haad void
    881  1.165.2.2  haad pool_destroy(struct pool *pp)
    882  1.165.2.2  haad {
    883  1.165.2.2  haad 	struct pool_pagelist pq;
    884  1.165.2.2  haad 	struct pool_item_header *ph;
    885  1.165.2.2  haad 
    886  1.165.2.2  haad 	/* Remove from global pool list */
    887  1.165.2.2  haad 	mutex_enter(&pool_head_lock);
    888  1.165.2.2  haad 	while (pp->pr_refcnt != 0)
    889  1.165.2.2  haad 		cv_wait(&pool_busy, &pool_head_lock);
    890  1.165.2.2  haad 	TAILQ_REMOVE(&pool_head, pp, pr_poollist);
    891  1.165.2.2  haad 	if (drainpp == pp)
    892  1.165.2.2  haad 		drainpp = NULL;
    893  1.165.2.2  haad 	mutex_exit(&pool_head_lock);
    894  1.165.2.2  haad 
    895  1.165.2.2  haad 	/* Remove this pool from its allocator's list of pools. */
    896  1.165.2.2  haad 	pool_reclaim_unregister(pp);
    897  1.165.2.2  haad 	mutex_enter(&pp->pr_alloc->pa_lock);
    898  1.165.2.2  haad 	TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list);
    899  1.165.2.2  haad 	mutex_exit(&pp->pr_alloc->pa_lock);
    900  1.165.2.2  haad 
    901  1.165.2.2  haad 	mutex_enter(&pp->pr_lock);
    902  1.165.2.2  haad 
    903  1.165.2.2  haad 	KASSERT(pp->pr_cache == NULL);
    904  1.165.2.2  haad 
    905  1.165.2.2  haad #ifdef DIAGNOSTIC
    906  1.165.2.2  haad 	if (pp->pr_nout != 0) {
    907  1.165.2.2  haad 		pr_printlog(pp, NULL, printf);
    908  1.165.2.2  haad 		panic("pool_destroy: pool busy: still out: %u",
    909  1.165.2.2  haad 		    pp->pr_nout);
    910  1.165.2.2  haad 	}
    911  1.165.2.2  haad #endif
    912  1.165.2.2  haad 
    913  1.165.2.2  haad 	KASSERT(LIST_EMPTY(&pp->pr_fullpages));
    914  1.165.2.2  haad 	KASSERT(LIST_EMPTY(&pp->pr_partpages));
    915  1.165.2.2  haad 
    916  1.165.2.2  haad 	/* Remove all pages */
    917  1.165.2.2  haad 	LIST_INIT(&pq);
    918  1.165.2.2  haad 	while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
    919  1.165.2.2  haad 		pr_rmpage(pp, ph, &pq);
    920  1.165.2.2  haad 
    921  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
    922  1.165.2.2  haad 
    923  1.165.2.2  haad 	pr_pagelist_free(pp, &pq);
    924  1.165.2.2  haad 
    925  1.165.2.2  haad #ifdef POOL_DIAGNOSTIC
    926  1.165.2.2  haad 	if ((pp->pr_roflags & PR_LOGGING) != 0)
    927  1.165.2.2  haad 		free(pp->pr_log, M_TEMP);
    928  1.165.2.2  haad #endif
    929  1.165.2.2  haad 
    930  1.165.2.2  haad 	cv_destroy(&pp->pr_cv);
    931  1.165.2.2  haad 	mutex_destroy(&pp->pr_lock);
    932  1.165.2.2  haad }
    933  1.165.2.2  haad 
    934  1.165.2.2  haad void
    935  1.165.2.2  haad pool_set_drain_hook(struct pool *pp, void (*fn)(void *, int), void *arg)
    936  1.165.2.2  haad {
    937  1.165.2.2  haad 
    938  1.165.2.2  haad 	/* XXX no locking -- must be used just after pool_init() */
    939  1.165.2.2  haad #ifdef DIAGNOSTIC
    940  1.165.2.2  haad 	if (pp->pr_drain_hook != NULL)
    941  1.165.2.2  haad 		panic("pool_set_drain_hook(%s): already set", pp->pr_wchan);
    942  1.165.2.2  haad #endif
    943  1.165.2.2  haad 	pp->pr_drain_hook = fn;
    944  1.165.2.2  haad 	pp->pr_drain_hook_arg = arg;
    945  1.165.2.2  haad }
    946  1.165.2.2  haad 
    947  1.165.2.2  haad static struct pool_item_header *
    948  1.165.2.2  haad pool_alloc_item_header(struct pool *pp, void *storage, int flags)
    949  1.165.2.2  haad {
    950  1.165.2.2  haad 	struct pool_item_header *ph;
    951  1.165.2.2  haad 
    952  1.165.2.2  haad 	if ((pp->pr_roflags & PR_PHINPAGE) != 0)
    953  1.165.2.2  haad 		ph = (struct pool_item_header *) ((char *)storage + pp->pr_phoffset);
    954  1.165.2.2  haad 	else
    955  1.165.2.2  haad 		ph = pool_get(pp->pr_phpool, flags);
    956  1.165.2.2  haad 
    957  1.165.2.2  haad 	return (ph);
    958  1.165.2.2  haad }
    959  1.165.2.2  haad 
    960  1.165.2.2  haad /*
    961  1.165.2.2  haad  * Grab an item from the pool.
    962  1.165.2.2  haad  */
    963  1.165.2.2  haad void *
    964  1.165.2.2  haad #ifdef POOL_DIAGNOSTIC
    965  1.165.2.2  haad _pool_get(struct pool *pp, int flags, const char *file, long line)
    966  1.165.2.2  haad #else
    967  1.165.2.2  haad pool_get(struct pool *pp, int flags)
    968  1.165.2.2  haad #endif
    969  1.165.2.2  haad {
    970  1.165.2.2  haad 	struct pool_item *pi;
    971  1.165.2.2  haad 	struct pool_item_header *ph;
    972  1.165.2.2  haad 	void *v;
    973  1.165.2.2  haad 
    974  1.165.2.2  haad #ifdef DIAGNOSTIC
    975  1.165.2.2  haad 	if (__predict_false(pp->pr_itemsperpage == 0))
    976  1.165.2.2  haad 		panic("pool_get: pool %p: pr_itemsperpage is zero, "
    977  1.165.2.2  haad 		    "pool not initialized?", pp);
    978  1.165.2.2  haad 	if (__predict_false(curlwp == NULL && doing_shutdown == 0 &&
    979  1.165.2.2  haad 			    (flags & PR_WAITOK) != 0))
    980  1.165.2.2  haad 		panic("pool_get: %s: must have NOWAIT", pp->pr_wchan);
    981  1.165.2.2  haad 
    982  1.165.2.2  haad #endif /* DIAGNOSTIC */
    983  1.165.2.2  haad #ifdef LOCKDEBUG
    984  1.165.2.2  haad 	if (flags & PR_WAITOK) {
    985  1.165.2.2  haad 		ASSERT_SLEEPABLE();
    986  1.165.2.2  haad 	}
    987  1.165.2.2  haad #endif
    988  1.165.2.2  haad 
    989  1.165.2.2  haad 	mutex_enter(&pp->pr_lock);
    990  1.165.2.2  haad 	pr_enter(pp, file, line);
    991  1.165.2.2  haad 
    992  1.165.2.2  haad  startover:
    993  1.165.2.2  haad 	/*
    994  1.165.2.2  haad 	 * Check to see if we've reached the hard limit.  If we have,
    995  1.165.2.2  haad 	 * and we can wait, then wait until an item has been returned to
    996  1.165.2.2  haad 	 * the pool.
    997  1.165.2.2  haad 	 */
    998  1.165.2.2  haad #ifdef DIAGNOSTIC
    999  1.165.2.2  haad 	if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
   1000  1.165.2.2  haad 		pr_leave(pp);
   1001  1.165.2.2  haad 		mutex_exit(&pp->pr_lock);
   1002  1.165.2.2  haad 		panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
   1003  1.165.2.2  haad 	}
   1004  1.165.2.2  haad #endif
   1005  1.165.2.2  haad 	if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
   1006  1.165.2.2  haad 		if (pp->pr_drain_hook != NULL) {
   1007  1.165.2.2  haad 			/*
   1008  1.165.2.2  haad 			 * Since the drain hook is going to free things
   1009  1.165.2.2  haad 			 * back to the pool, unlock, call the hook, re-lock,
   1010  1.165.2.2  haad 			 * and check the hardlimit condition again.
   1011  1.165.2.2  haad 			 */
   1012  1.165.2.2  haad 			pr_leave(pp);
   1013  1.165.2.2  haad 			mutex_exit(&pp->pr_lock);
   1014  1.165.2.2  haad 			(*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
   1015  1.165.2.2  haad 			mutex_enter(&pp->pr_lock);
   1016  1.165.2.2  haad 			pr_enter(pp, file, line);
   1017  1.165.2.2  haad 			if (pp->pr_nout < pp->pr_hardlimit)
   1018  1.165.2.2  haad 				goto startover;
   1019  1.165.2.2  haad 		}
   1020  1.165.2.2  haad 
   1021  1.165.2.2  haad 		if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
   1022  1.165.2.2  haad 			/*
   1023  1.165.2.2  haad 			 * XXX: A warning isn't logged in this case.  Should
   1024  1.165.2.2  haad 			 * it be?
   1025  1.165.2.2  haad 			 */
   1026  1.165.2.2  haad 			pp->pr_flags |= PR_WANTED;
   1027  1.165.2.2  haad 			pr_leave(pp);
   1028  1.165.2.2  haad 			cv_wait(&pp->pr_cv, &pp->pr_lock);
   1029  1.165.2.2  haad 			pr_enter(pp, file, line);
   1030  1.165.2.2  haad 			goto startover;
   1031  1.165.2.2  haad 		}
   1032  1.165.2.2  haad 
   1033  1.165.2.2  haad 		/*
   1034  1.165.2.2  haad 		 * Log a message that the hard limit has been hit.
   1035  1.165.2.2  haad 		 */
   1036  1.165.2.2  haad 		if (pp->pr_hardlimit_warning != NULL &&
   1037  1.165.2.2  haad 		    ratecheck(&pp->pr_hardlimit_warning_last,
   1038  1.165.2.2  haad 			      &pp->pr_hardlimit_ratecap))
   1039  1.165.2.2  haad 			log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
   1040  1.165.2.2  haad 
   1041  1.165.2.2  haad 		pp->pr_nfail++;
   1042  1.165.2.2  haad 
   1043  1.165.2.2  haad 		pr_leave(pp);
   1044  1.165.2.2  haad 		mutex_exit(&pp->pr_lock);
   1045  1.165.2.2  haad 		return (NULL);
   1046  1.165.2.2  haad 	}
   1047  1.165.2.2  haad 
   1048  1.165.2.2  haad 	/*
   1049  1.165.2.2  haad 	 * The convention we use is that if `curpage' is not NULL, then
   1050  1.165.2.2  haad 	 * it points at a non-empty bucket. In particular, `curpage'
   1051  1.165.2.2  haad 	 * never points at a page header which has PR_PHINPAGE set and
   1052  1.165.2.2  haad 	 * has no items in its bucket.
   1053  1.165.2.2  haad 	 */
   1054  1.165.2.2  haad 	if ((ph = pp->pr_curpage) == NULL) {
   1055  1.165.2.2  haad 		int error;
   1056  1.165.2.2  haad 
   1057  1.165.2.2  haad #ifdef DIAGNOSTIC
   1058  1.165.2.2  haad 		if (pp->pr_nitems != 0) {
   1059  1.165.2.2  haad 			mutex_exit(&pp->pr_lock);
   1060  1.165.2.2  haad 			printf("pool_get: %s: curpage NULL, nitems %u\n",
   1061  1.165.2.2  haad 			    pp->pr_wchan, pp->pr_nitems);
   1062  1.165.2.2  haad 			panic("pool_get: nitems inconsistent");
   1063  1.165.2.2  haad 		}
   1064  1.165.2.2  haad #endif
   1065  1.165.2.2  haad 
   1066  1.165.2.2  haad 		/*
   1067  1.165.2.2  haad 		 * Call the back-end page allocator for more memory.
   1068  1.165.2.2  haad 		 * Release the pool lock, as the back-end page allocator
   1069  1.165.2.2  haad 		 * may block.
   1070  1.165.2.2  haad 		 */
   1071  1.165.2.2  haad 		pr_leave(pp);
   1072  1.165.2.2  haad 		error = pool_grow(pp, flags);
   1073  1.165.2.2  haad 		pr_enter(pp, file, line);
   1074  1.165.2.2  haad 		if (error != 0) {
   1075  1.165.2.2  haad 			/*
   1076  1.165.2.2  haad 			 * We were unable to allocate a page or item
   1077  1.165.2.2  haad 			 * header, but we released the lock during
   1078  1.165.2.2  haad 			 * allocation, so perhaps items were freed
   1079  1.165.2.2  haad 			 * back to the pool.  Check for this case.
   1080  1.165.2.2  haad 			 */
   1081  1.165.2.2  haad 			if (pp->pr_curpage != NULL)
   1082  1.165.2.2  haad 				goto startover;
   1083  1.165.2.2  haad 
   1084  1.165.2.2  haad 			pp->pr_nfail++;
   1085  1.165.2.2  haad 			pr_leave(pp);
   1086  1.165.2.2  haad 			mutex_exit(&pp->pr_lock);
   1087  1.165.2.2  haad 			return (NULL);
   1088  1.165.2.2  haad 		}
   1089  1.165.2.2  haad 
   1090  1.165.2.2  haad 		/* Start the allocation process over. */
   1091  1.165.2.2  haad 		goto startover;
   1092  1.165.2.2  haad 	}
   1093  1.165.2.2  haad 	if (pp->pr_roflags & PR_NOTOUCH) {
   1094  1.165.2.2  haad #ifdef DIAGNOSTIC
   1095  1.165.2.2  haad 		if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) {
   1096  1.165.2.2  haad 			pr_leave(pp);
   1097  1.165.2.2  haad 			mutex_exit(&pp->pr_lock);
   1098  1.165.2.2  haad 			panic("pool_get: %s: page empty", pp->pr_wchan);
   1099  1.165.2.2  haad 		}
   1100  1.165.2.2  haad #endif
   1101  1.165.2.2  haad 		v = pr_item_notouch_get(pp, ph);
   1102  1.165.2.2  haad #ifdef POOL_DIAGNOSTIC
   1103  1.165.2.2  haad 		pr_log(pp, v, PRLOG_GET, file, line);
   1104  1.165.2.2  haad #endif
   1105  1.165.2.2  haad 	} else {
   1106  1.165.2.2  haad 		v = pi = LIST_FIRST(&ph->ph_itemlist);
   1107  1.165.2.2  haad 		if (__predict_false(v == NULL)) {
   1108  1.165.2.2  haad 			pr_leave(pp);
   1109  1.165.2.2  haad 			mutex_exit(&pp->pr_lock);
   1110  1.165.2.2  haad 			panic("pool_get: %s: page empty", pp->pr_wchan);
   1111  1.165.2.2  haad 		}
   1112  1.165.2.2  haad #ifdef DIAGNOSTIC
   1113  1.165.2.2  haad 		if (__predict_false(pp->pr_nitems == 0)) {
   1114  1.165.2.2  haad 			pr_leave(pp);
   1115  1.165.2.2  haad 			mutex_exit(&pp->pr_lock);
   1116  1.165.2.2  haad 			printf("pool_get: %s: items on itemlist, nitems %u\n",
   1117  1.165.2.2  haad 			    pp->pr_wchan, pp->pr_nitems);
   1118  1.165.2.2  haad 			panic("pool_get: nitems inconsistent");
   1119  1.165.2.2  haad 		}
   1120  1.165.2.2  haad #endif
   1121  1.165.2.2  haad 
   1122  1.165.2.2  haad #ifdef POOL_DIAGNOSTIC
   1123  1.165.2.2  haad 		pr_log(pp, v, PRLOG_GET, file, line);
   1124  1.165.2.2  haad #endif
   1125  1.165.2.2  haad 
   1126  1.165.2.2  haad #ifdef DIAGNOSTIC
   1127  1.165.2.2  haad 		if (__predict_false(pi->pi_magic != PI_MAGIC)) {
   1128  1.165.2.2  haad 			pr_printlog(pp, pi, printf);
   1129  1.165.2.2  haad 			panic("pool_get(%s): free list modified: "
   1130  1.165.2.2  haad 			    "magic=%x; page %p; item addr %p\n",
   1131  1.165.2.2  haad 			    pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
   1132  1.165.2.2  haad 		}
   1133  1.165.2.2  haad #endif
   1134  1.165.2.2  haad 
   1135  1.165.2.2  haad 		/*
   1136  1.165.2.2  haad 		 * Remove from item list.
   1137  1.165.2.2  haad 		 */
   1138  1.165.2.2  haad 		LIST_REMOVE(pi, pi_list);
   1139  1.165.2.2  haad 	}
   1140  1.165.2.2  haad 	pp->pr_nitems--;
   1141  1.165.2.2  haad 	pp->pr_nout++;
   1142  1.165.2.2  haad 	if (ph->ph_nmissing == 0) {
   1143  1.165.2.2  haad #ifdef DIAGNOSTIC
   1144  1.165.2.2  haad 		if (__predict_false(pp->pr_nidle == 0))
   1145  1.165.2.2  haad 			panic("pool_get: nidle inconsistent");
   1146  1.165.2.2  haad #endif
   1147  1.165.2.2  haad 		pp->pr_nidle--;
   1148  1.165.2.2  haad 
   1149  1.165.2.2  haad 		/*
   1150  1.165.2.2  haad 		 * This page was previously empty.  Move it to the list of
   1151  1.165.2.2  haad 		 * partially-full pages.  This page is already curpage.
   1152  1.165.2.2  haad 		 */
   1153  1.165.2.2  haad 		LIST_REMOVE(ph, ph_pagelist);
   1154  1.165.2.2  haad 		LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
   1155  1.165.2.2  haad 	}
   1156  1.165.2.2  haad 	ph->ph_nmissing++;
   1157  1.165.2.2  haad 	if (ph->ph_nmissing == pp->pr_itemsperpage) {
   1158  1.165.2.2  haad #ifdef DIAGNOSTIC
   1159  1.165.2.2  haad 		if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 &&
   1160  1.165.2.2  haad 		    !LIST_EMPTY(&ph->ph_itemlist))) {
   1161  1.165.2.2  haad 			pr_leave(pp);
   1162  1.165.2.2  haad 			mutex_exit(&pp->pr_lock);
   1163  1.165.2.2  haad 			panic("pool_get: %s: nmissing inconsistent",
   1164  1.165.2.2  haad 			    pp->pr_wchan);
   1165  1.165.2.2  haad 		}
   1166  1.165.2.2  haad #endif
   1167  1.165.2.2  haad 		/*
   1168  1.165.2.2  haad 		 * This page is now full.  Move it to the full list
   1169  1.165.2.2  haad 		 * and select a new current page.
   1170  1.165.2.2  haad 		 */
   1171  1.165.2.2  haad 		LIST_REMOVE(ph, ph_pagelist);
   1172  1.165.2.2  haad 		LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
   1173  1.165.2.2  haad 		pool_update_curpage(pp);
   1174  1.165.2.2  haad 	}
   1175  1.165.2.2  haad 
   1176  1.165.2.2  haad 	pp->pr_nget++;
   1177  1.165.2.2  haad 	pr_leave(pp);
   1178  1.165.2.2  haad 
   1179  1.165.2.2  haad 	/*
   1180  1.165.2.2  haad 	 * If we have a low water mark and we are now below that low
   1181  1.165.2.2  haad 	 * water mark, add more items to the pool.
   1182  1.165.2.2  haad 	 */
   1183  1.165.2.2  haad 	if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
   1184  1.165.2.2  haad 		/*
   1185  1.165.2.2  haad 		 * XXX: Should we log a warning?  Should we set up a timeout
   1186  1.165.2.2  haad 		 * to try again in a second or so?  The latter could break
   1187  1.165.2.2  haad 		 * a caller's assumptions about interrupt protection, etc.
   1188  1.165.2.2  haad 		 */
   1189  1.165.2.2  haad 	}
   1190  1.165.2.2  haad 
   1191  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
   1192  1.165.2.2  haad 	KASSERT((((vaddr_t)v + pp->pr_itemoffset) & (pp->pr_align - 1)) == 0);
   1193  1.165.2.2  haad 	FREECHECK_OUT(&pp->pr_freecheck, v);
   1194  1.165.2.2  haad 	return (v);
   1195  1.165.2.2  haad }
   1196  1.165.2.2  haad 
   1197  1.165.2.2  haad /*
   1198  1.165.2.2  haad  * Internal version of pool_put().  Pool is already locked/entered.
   1199  1.165.2.2  haad  */
   1200  1.165.2.2  haad static void
   1201  1.165.2.2  haad pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq)
   1202  1.165.2.2  haad {
   1203  1.165.2.2  haad 	struct pool_item *pi = v;
   1204  1.165.2.2  haad 	struct pool_item_header *ph;
   1205  1.165.2.2  haad 
   1206  1.165.2.2  haad 	KASSERT(mutex_owned(&pp->pr_lock));
   1207  1.165.2.2  haad 	FREECHECK_IN(&pp->pr_freecheck, v);
   1208  1.165.2.2  haad 	LOCKDEBUG_MEM_CHECK(v, pp->pr_size);
   1209  1.165.2.2  haad 
   1210  1.165.2.2  haad #ifdef DIAGNOSTIC
   1211  1.165.2.2  haad 	if (__predict_false(pp->pr_nout == 0)) {
   1212  1.165.2.2  haad 		printf("pool %s: putting with none out\n",
   1213  1.165.2.2  haad 		    pp->pr_wchan);
   1214  1.165.2.2  haad 		panic("pool_put");
   1215  1.165.2.2  haad 	}
   1216  1.165.2.2  haad #endif
   1217  1.165.2.2  haad 
   1218  1.165.2.2  haad 	if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) {
   1219  1.165.2.2  haad 		pr_printlog(pp, NULL, printf);
   1220  1.165.2.2  haad 		panic("pool_put: %s: page header missing", pp->pr_wchan);
   1221  1.165.2.2  haad 	}
   1222  1.165.2.2  haad 
   1223  1.165.2.2  haad 	/*
   1224  1.165.2.2  haad 	 * Return to item list.
   1225  1.165.2.2  haad 	 */
   1226  1.165.2.2  haad 	if (pp->pr_roflags & PR_NOTOUCH) {
   1227  1.165.2.2  haad 		pr_item_notouch_put(pp, ph, v);
   1228  1.165.2.2  haad 	} else {
   1229  1.165.2.2  haad #ifdef DIAGNOSTIC
   1230  1.165.2.2  haad 		pi->pi_magic = PI_MAGIC;
   1231  1.165.2.2  haad #endif
   1232  1.165.2.2  haad #ifdef DEBUG
   1233  1.165.2.2  haad 		{
   1234  1.165.2.2  haad 			int i, *ip = v;
   1235  1.165.2.2  haad 
   1236  1.165.2.2  haad 			for (i = 0; i < pp->pr_size / sizeof(int); i++) {
   1237  1.165.2.2  haad 				*ip++ = PI_MAGIC;
   1238  1.165.2.2  haad 			}
   1239  1.165.2.2  haad 		}
   1240  1.165.2.2  haad #endif
   1241  1.165.2.2  haad 
   1242  1.165.2.2  haad 		LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
   1243  1.165.2.2  haad 	}
   1244  1.165.2.2  haad 	KDASSERT(ph->ph_nmissing != 0);
   1245  1.165.2.2  haad 	ph->ph_nmissing--;
   1246  1.165.2.2  haad 	pp->pr_nput++;
   1247  1.165.2.2  haad 	pp->pr_nitems++;
   1248  1.165.2.2  haad 	pp->pr_nout--;
   1249  1.165.2.2  haad 
   1250  1.165.2.2  haad 	/* Cancel "pool empty" condition if it exists */
   1251  1.165.2.2  haad 	if (pp->pr_curpage == NULL)
   1252  1.165.2.2  haad 		pp->pr_curpage = ph;
   1253  1.165.2.2  haad 
   1254  1.165.2.2  haad 	if (pp->pr_flags & PR_WANTED) {
   1255  1.165.2.2  haad 		pp->pr_flags &= ~PR_WANTED;
   1256  1.165.2.2  haad 		cv_broadcast(&pp->pr_cv);
   1257  1.165.2.2  haad 	}
   1258  1.165.2.2  haad 
   1259  1.165.2.2  haad 	/*
   1260  1.165.2.2  haad 	 * If this page is now empty, do one of two things:
   1261  1.165.2.2  haad 	 *
   1262  1.165.2.2  haad 	 *	(1) If we have more pages than the page high water mark,
   1263  1.165.2.2  haad 	 *	    free the page back to the system.  ONLY CONSIDER
   1264  1.165.2.2  haad 	 *	    FREEING BACK A PAGE IF WE HAVE MORE THAN OUR MINIMUM PAGE
   1265  1.165.2.2  haad 	 *	    CLAIM.
   1266  1.165.2.2  haad 	 *
   1267  1.165.2.2  haad 	 *	(2) Otherwise, move the page to the empty page list.
   1268  1.165.2.2  haad 	 *
   1269  1.165.2.2  haad 	 * Either way, select a new current page (so we use a partially-full
   1270  1.165.2.2  haad 	 * page if one is available).
   1271  1.165.2.2  haad 	 */
   1272  1.165.2.2  haad 	if (ph->ph_nmissing == 0) {
   1273  1.165.2.2  haad 		pp->pr_nidle++;
   1274  1.165.2.2  haad 		if (pp->pr_npages > pp->pr_minpages &&
   1275  1.165.2.2  haad 		    pp->pr_npages > pp->pr_maxpages) {
   1276  1.165.2.2  haad 			pr_rmpage(pp, ph, pq);
   1277  1.165.2.2  haad 		} else {
   1278  1.165.2.2  haad 			LIST_REMOVE(ph, ph_pagelist);
   1279  1.165.2.2  haad 			LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
   1280  1.165.2.2  haad 
   1281  1.165.2.2  haad 			/*
   1282  1.165.2.2  haad 			 * Update the timestamp on the page.  A page must
   1283  1.165.2.2  haad 			 * be idle for some period of time before it can
   1284  1.165.2.2  haad 			 * be reclaimed by the pagedaemon.  This minimizes
   1285  1.165.2.2  haad 			 * ping-pong'ing for memory.
   1286  1.165.2.2  haad 			 *
   1287  1.165.2.2  haad 			 * note for 64-bit time_t: truncating to 32-bit is not
   1288  1.165.2.2  haad 			 * a problem for our usage.
   1289  1.165.2.2  haad 			 */
   1290  1.165.2.2  haad 			ph->ph_time = time_uptime;
   1291  1.165.2.2  haad 		}
   1292  1.165.2.2  haad 		pool_update_curpage(pp);
   1293  1.165.2.2  haad 	}
   1294  1.165.2.2  haad 
   1295  1.165.2.2  haad 	/*
   1296  1.165.2.2  haad 	 * If the page was previously completely full, move it to the
   1297  1.165.2.2  haad 	 * partially-full list and make it the current page.  The next
   1298  1.165.2.2  haad 	 * allocation will get the item from this page, instead of
   1299  1.165.2.2  haad 	 * further fragmenting the pool.
   1300  1.165.2.2  haad 	 */
   1301  1.165.2.2  haad 	else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
   1302  1.165.2.2  haad 		LIST_REMOVE(ph, ph_pagelist);
   1303  1.165.2.2  haad 		LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
   1304  1.165.2.2  haad 		pp->pr_curpage = ph;
   1305  1.165.2.2  haad 	}
   1306  1.165.2.2  haad }
   1307  1.165.2.2  haad 
   1308  1.165.2.2  haad /*
   1309  1.165.2.2  haad  * Return resource to the pool.
   1310  1.165.2.2  haad  */
   1311  1.165.2.2  haad #ifdef POOL_DIAGNOSTIC
   1312  1.165.2.2  haad void
   1313  1.165.2.2  haad _pool_put(struct pool *pp, void *v, const char *file, long line)
   1314  1.165.2.2  haad {
   1315  1.165.2.2  haad 	struct pool_pagelist pq;
   1316  1.165.2.2  haad 
   1317  1.165.2.2  haad 	LIST_INIT(&pq);
   1318  1.165.2.2  haad 
   1319  1.165.2.2  haad 	mutex_enter(&pp->pr_lock);
   1320  1.165.2.2  haad 	pr_enter(pp, file, line);
   1321  1.165.2.2  haad 
   1322  1.165.2.2  haad 	pr_log(pp, v, PRLOG_PUT, file, line);
   1323  1.165.2.2  haad 
   1324  1.165.2.2  haad 	pool_do_put(pp, v, &pq);
   1325  1.165.2.2  haad 
   1326  1.165.2.2  haad 	pr_leave(pp);
   1327  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
   1328  1.165.2.2  haad 
   1329  1.165.2.2  haad 	pr_pagelist_free(pp, &pq);
   1330  1.165.2.2  haad }
   1331  1.165.2.2  haad #undef pool_put
   1332  1.165.2.2  haad #endif /* POOL_DIAGNOSTIC */
   1333  1.165.2.2  haad 
   1334  1.165.2.2  haad void
   1335  1.165.2.2  haad pool_put(struct pool *pp, void *v)
   1336  1.165.2.2  haad {
   1337  1.165.2.2  haad 	struct pool_pagelist pq;
   1338  1.165.2.2  haad 
   1339  1.165.2.2  haad 	LIST_INIT(&pq);
   1340  1.165.2.2  haad 
   1341  1.165.2.2  haad 	mutex_enter(&pp->pr_lock);
   1342  1.165.2.2  haad 	pool_do_put(pp, v, &pq);
   1343  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
   1344  1.165.2.2  haad 
   1345  1.165.2.2  haad 	pr_pagelist_free(pp, &pq);
   1346  1.165.2.2  haad }
   1347  1.165.2.2  haad 
   1348  1.165.2.2  haad #ifdef POOL_DIAGNOSTIC
   1349  1.165.2.2  haad #define		pool_put(h, v)	_pool_put((h), (v), __FILE__, __LINE__)
   1350  1.165.2.2  haad #endif
   1351  1.165.2.2  haad 
   1352  1.165.2.2  haad /*
   1353  1.165.2.2  haad  * pool_grow: grow a pool by a page.
   1354  1.165.2.2  haad  *
   1355  1.165.2.2  haad  * => called with pool locked.
   1356  1.165.2.2  haad  * => unlock and relock the pool.
   1357  1.165.2.2  haad  * => return with pool locked.
   1358  1.165.2.2  haad  */
   1359  1.165.2.2  haad 
   1360  1.165.2.2  haad static int
   1361  1.165.2.2  haad pool_grow(struct pool *pp, int flags)
   1362  1.165.2.2  haad {
   1363  1.165.2.2  haad 	struct pool_item_header *ph = NULL;
   1364  1.165.2.2  haad 	char *cp;
   1365  1.165.2.2  haad 
   1366  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
   1367  1.165.2.2  haad 	cp = pool_allocator_alloc(pp, flags);
   1368  1.165.2.2  haad 	if (__predict_true(cp != NULL)) {
   1369  1.165.2.2  haad 		ph = pool_alloc_item_header(pp, cp, flags);
   1370  1.165.2.2  haad 	}
   1371  1.165.2.2  haad 	if (__predict_false(cp == NULL || ph == NULL)) {
   1372  1.165.2.2  haad 		if (cp != NULL) {
   1373  1.165.2.2  haad 			pool_allocator_free(pp, cp);
   1374  1.165.2.2  haad 		}
   1375  1.165.2.2  haad 		mutex_enter(&pp->pr_lock);
   1376  1.165.2.2  haad 		return ENOMEM;
   1377  1.165.2.2  haad 	}
   1378  1.165.2.2  haad 
   1379  1.165.2.2  haad 	mutex_enter(&pp->pr_lock);
   1380  1.165.2.2  haad 	pool_prime_page(pp, cp, ph);
   1381  1.165.2.2  haad 	pp->pr_npagealloc++;
   1382  1.165.2.2  haad 	return 0;
   1383  1.165.2.2  haad }
   1384  1.165.2.2  haad 
   1385  1.165.2.2  haad /*
   1386  1.165.2.2  haad  * Add N items to the pool.
   1387  1.165.2.2  haad  */
   1388  1.165.2.2  haad int
   1389  1.165.2.2  haad pool_prime(struct pool *pp, int n)
   1390  1.165.2.2  haad {
   1391  1.165.2.2  haad 	int newpages;
   1392  1.165.2.2  haad 	int error = 0;
   1393  1.165.2.2  haad 
   1394  1.165.2.2  haad 	mutex_enter(&pp->pr_lock);
   1395  1.165.2.2  haad 
   1396  1.165.2.2  haad 	newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
   1397  1.165.2.2  haad 
   1398  1.165.2.2  haad 	while (newpages-- > 0) {
   1399  1.165.2.2  haad 		error = pool_grow(pp, PR_NOWAIT);
   1400  1.165.2.2  haad 		if (error) {
   1401  1.165.2.2  haad 			break;
   1402  1.165.2.2  haad 		}
   1403  1.165.2.2  haad 		pp->pr_minpages++;
   1404  1.165.2.2  haad 	}
   1405  1.165.2.2  haad 
   1406  1.165.2.2  haad 	if (pp->pr_minpages >= pp->pr_maxpages)
   1407  1.165.2.2  haad 		pp->pr_maxpages = pp->pr_minpages + 1;	/* XXX */
   1408  1.165.2.2  haad 
   1409  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
   1410  1.165.2.2  haad 	return error;
   1411  1.165.2.2  haad }
   1412  1.165.2.2  haad 
   1413  1.165.2.2  haad /*
   1414  1.165.2.2  haad  * Add a page worth of items to the pool.
   1415  1.165.2.2  haad  *
   1416  1.165.2.2  haad  * Note, we must be called with the pool descriptor LOCKED.
   1417  1.165.2.2  haad  */
   1418  1.165.2.2  haad static void
   1419  1.165.2.2  haad pool_prime_page(struct pool *pp, void *storage, struct pool_item_header *ph)
   1420  1.165.2.2  haad {
   1421  1.165.2.2  haad 	struct pool_item *pi;
   1422  1.165.2.2  haad 	void *cp = storage;
   1423  1.165.2.2  haad 	const unsigned int align = pp->pr_align;
   1424  1.165.2.2  haad 	const unsigned int ioff = pp->pr_itemoffset;
   1425  1.165.2.2  haad 	int n;
   1426  1.165.2.2  haad 
   1427  1.165.2.2  haad 	KASSERT(mutex_owned(&pp->pr_lock));
   1428  1.165.2.2  haad 
   1429  1.165.2.2  haad #ifdef DIAGNOSTIC
   1430  1.165.2.2  haad 	if ((pp->pr_roflags & PR_NOALIGN) == 0 &&
   1431  1.165.2.2  haad 	    ((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0)
   1432  1.165.2.2  haad 		panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
   1433  1.165.2.2  haad #endif
   1434  1.165.2.2  haad 
   1435  1.165.2.2  haad 	/*
   1436  1.165.2.2  haad 	 * Insert page header.
   1437  1.165.2.2  haad 	 */
   1438  1.165.2.2  haad 	LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
   1439  1.165.2.2  haad 	LIST_INIT(&ph->ph_itemlist);
   1440  1.165.2.2  haad 	ph->ph_page = storage;
   1441  1.165.2.2  haad 	ph->ph_nmissing = 0;
   1442  1.165.2.2  haad 	ph->ph_time = time_uptime;
   1443  1.165.2.2  haad 	if ((pp->pr_roflags & PR_PHINPAGE) == 0)
   1444  1.165.2.2  haad 		SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
   1445  1.165.2.2  haad 
   1446  1.165.2.2  haad 	pp->pr_nidle++;
   1447  1.165.2.2  haad 
   1448  1.165.2.2  haad 	/*
   1449  1.165.2.2  haad 	 * Color this page.
   1450  1.165.2.2  haad 	 */
   1451  1.165.2.2  haad 	ph->ph_off = pp->pr_curcolor;
   1452  1.165.2.2  haad 	cp = (char *)cp + ph->ph_off;
   1453  1.165.2.2  haad 	if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
   1454  1.165.2.2  haad 		pp->pr_curcolor = 0;
   1455  1.165.2.2  haad 
   1456  1.165.2.2  haad 	/*
   1457  1.165.2.2  haad 	 * Adjust storage to apply aligment to `pr_itemoffset' in each item.
   1458  1.165.2.2  haad 	 */
   1459  1.165.2.2  haad 	if (ioff != 0)
   1460  1.165.2.2  haad 		cp = (char *)cp + align - ioff;
   1461  1.165.2.2  haad 
   1462  1.165.2.2  haad 	KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
   1463  1.165.2.2  haad 
   1464  1.165.2.2  haad 	/*
   1465  1.165.2.2  haad 	 * Insert remaining chunks on the bucket list.
   1466  1.165.2.2  haad 	 */
   1467  1.165.2.2  haad 	n = pp->pr_itemsperpage;
   1468  1.165.2.2  haad 	pp->pr_nitems += n;
   1469  1.165.2.2  haad 
   1470  1.165.2.2  haad 	if (pp->pr_roflags & PR_NOTOUCH) {
   1471  1.165.2.2  haad 		pr_item_notouch_init(pp, ph);
   1472  1.165.2.2  haad 	} else {
   1473  1.165.2.2  haad 		while (n--) {
   1474  1.165.2.2  haad 			pi = (struct pool_item *)cp;
   1475  1.165.2.2  haad 
   1476  1.165.2.2  haad 			KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
   1477  1.165.2.2  haad 
   1478  1.165.2.2  haad 			/* Insert on page list */
   1479  1.165.2.2  haad 			LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
   1480  1.165.2.2  haad #ifdef DIAGNOSTIC
   1481  1.165.2.2  haad 			pi->pi_magic = PI_MAGIC;
   1482  1.165.2.2  haad #endif
   1483  1.165.2.2  haad 			cp = (char *)cp + pp->pr_size;
   1484  1.165.2.2  haad 
   1485  1.165.2.2  haad 			KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
   1486  1.165.2.2  haad 		}
   1487  1.165.2.2  haad 	}
   1488  1.165.2.2  haad 
   1489  1.165.2.2  haad 	/*
   1490  1.165.2.2  haad 	 * If the pool was depleted, point at the new page.
   1491  1.165.2.2  haad 	 */
   1492  1.165.2.2  haad 	if (pp->pr_curpage == NULL)
   1493  1.165.2.2  haad 		pp->pr_curpage = ph;
   1494  1.165.2.2  haad 
   1495  1.165.2.2  haad 	if (++pp->pr_npages > pp->pr_hiwat)
   1496  1.165.2.2  haad 		pp->pr_hiwat = pp->pr_npages;
   1497  1.165.2.2  haad }
   1498  1.165.2.2  haad 
   1499  1.165.2.2  haad /*
   1500  1.165.2.2  haad  * Used by pool_get() when nitems drops below the low water mark.  This
   1501  1.165.2.2  haad  * is used to catch up pr_nitems with the low water mark.
   1502  1.165.2.2  haad  *
   1503  1.165.2.2  haad  * Note 1, we never wait for memory here, we let the caller decide what to do.
   1504  1.165.2.2  haad  *
   1505  1.165.2.2  haad  * Note 2, we must be called with the pool already locked, and we return
   1506  1.165.2.2  haad  * with it locked.
   1507  1.165.2.2  haad  */
   1508  1.165.2.2  haad static int
   1509  1.165.2.2  haad pool_catchup(struct pool *pp)
   1510  1.165.2.2  haad {
   1511  1.165.2.2  haad 	int error = 0;
   1512  1.165.2.2  haad 
   1513  1.165.2.2  haad 	while (POOL_NEEDS_CATCHUP(pp)) {
   1514  1.165.2.2  haad 		error = pool_grow(pp, PR_NOWAIT);
   1515  1.165.2.2  haad 		if (error) {
   1516  1.165.2.2  haad 			break;
   1517  1.165.2.2  haad 		}
   1518  1.165.2.2  haad 	}
   1519  1.165.2.2  haad 	return error;
   1520  1.165.2.2  haad }
   1521  1.165.2.2  haad 
   1522  1.165.2.2  haad static void
   1523  1.165.2.2  haad pool_update_curpage(struct pool *pp)
   1524  1.165.2.2  haad {
   1525  1.165.2.2  haad 
   1526  1.165.2.2  haad 	pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
   1527  1.165.2.2  haad 	if (pp->pr_curpage == NULL) {
   1528  1.165.2.2  haad 		pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
   1529  1.165.2.2  haad 	}
   1530  1.165.2.2  haad 	KASSERT((pp->pr_curpage == NULL && pp->pr_nitems == 0) ||
   1531  1.165.2.2  haad 	    (pp->pr_curpage != NULL && pp->pr_nitems > 0));
   1532  1.165.2.2  haad }
   1533  1.165.2.2  haad 
   1534  1.165.2.2  haad void
   1535  1.165.2.2  haad pool_setlowat(struct pool *pp, int n)
   1536  1.165.2.2  haad {
   1537  1.165.2.2  haad 
   1538  1.165.2.2  haad 	mutex_enter(&pp->pr_lock);
   1539  1.165.2.2  haad 
   1540  1.165.2.2  haad 	pp->pr_minitems = n;
   1541  1.165.2.2  haad 	pp->pr_minpages = (n == 0)
   1542  1.165.2.2  haad 		? 0
   1543  1.165.2.2  haad 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
   1544  1.165.2.2  haad 
   1545  1.165.2.2  haad 	/* Make sure we're caught up with the newly-set low water mark. */
   1546  1.165.2.2  haad 	if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
   1547  1.165.2.2  haad 		/*
   1548  1.165.2.2  haad 		 * XXX: Should we log a warning?  Should we set up a timeout
   1549  1.165.2.2  haad 		 * to try again in a second or so?  The latter could break
   1550  1.165.2.2  haad 		 * a caller's assumptions about interrupt protection, etc.
   1551  1.165.2.2  haad 		 */
   1552  1.165.2.2  haad 	}
   1553  1.165.2.2  haad 
   1554  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
   1555  1.165.2.2  haad }
   1556  1.165.2.2  haad 
   1557  1.165.2.2  haad void
   1558  1.165.2.2  haad pool_sethiwat(struct pool *pp, int n)
   1559  1.165.2.2  haad {
   1560  1.165.2.2  haad 
   1561  1.165.2.2  haad 	mutex_enter(&pp->pr_lock);
   1562  1.165.2.2  haad 
   1563  1.165.2.2  haad 	pp->pr_maxpages = (n == 0)
   1564  1.165.2.2  haad 		? 0
   1565  1.165.2.2  haad 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
   1566  1.165.2.2  haad 
   1567  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
   1568  1.165.2.2  haad }
   1569  1.165.2.2  haad 
   1570  1.165.2.2  haad void
   1571  1.165.2.2  haad pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
   1572  1.165.2.2  haad {
   1573  1.165.2.2  haad 
   1574  1.165.2.2  haad 	mutex_enter(&pp->pr_lock);
   1575  1.165.2.2  haad 
   1576  1.165.2.2  haad 	pp->pr_hardlimit = n;
   1577  1.165.2.2  haad 	pp->pr_hardlimit_warning = warnmess;
   1578  1.165.2.2  haad 	pp->pr_hardlimit_ratecap.tv_sec = ratecap;
   1579  1.165.2.2  haad 	pp->pr_hardlimit_warning_last.tv_sec = 0;
   1580  1.165.2.2  haad 	pp->pr_hardlimit_warning_last.tv_usec = 0;
   1581  1.165.2.2  haad 
   1582  1.165.2.2  haad 	/*
   1583  1.165.2.2  haad 	 * In-line version of pool_sethiwat(), because we don't want to
   1584  1.165.2.2  haad 	 * release the lock.
   1585  1.165.2.2  haad 	 */
   1586  1.165.2.2  haad 	pp->pr_maxpages = (n == 0)
   1587  1.165.2.2  haad 		? 0
   1588  1.165.2.2  haad 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
   1589  1.165.2.2  haad 
   1590  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
   1591  1.165.2.2  haad }
   1592  1.165.2.2  haad 
   1593  1.165.2.2  haad /*
   1594  1.165.2.2  haad  * Release all complete pages that have not been used recently.
   1595  1.165.2.2  haad  */
   1596  1.165.2.2  haad int
   1597  1.165.2.2  haad #ifdef POOL_DIAGNOSTIC
   1598  1.165.2.2  haad _pool_reclaim(struct pool *pp, const char *file, long line)
   1599  1.165.2.2  haad #else
   1600  1.165.2.2  haad pool_reclaim(struct pool *pp)
   1601  1.165.2.2  haad #endif
   1602  1.165.2.2  haad {
   1603  1.165.2.2  haad 	struct pool_item_header *ph, *phnext;
   1604  1.165.2.2  haad 	struct pool_pagelist pq;
   1605  1.165.2.2  haad 	uint32_t curtime;
   1606  1.165.2.2  haad 	bool klock;
   1607  1.165.2.2  haad 	int rv;
   1608  1.165.2.2  haad 
   1609  1.165.2.2  haad 	if (pp->pr_drain_hook != NULL) {
   1610  1.165.2.2  haad 		/*
   1611  1.165.2.2  haad 		 * The drain hook must be called with the pool unlocked.
   1612  1.165.2.2  haad 		 */
   1613  1.165.2.2  haad 		(*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT);
   1614  1.165.2.2  haad 	}
   1615  1.165.2.2  haad 
   1616  1.165.2.2  haad 	/*
   1617  1.165.2.2  haad 	 * XXXSMP Because we do not want to cause non-MPSAFE code
   1618  1.165.2.2  haad 	 * to block.
   1619  1.165.2.2  haad 	 */
   1620  1.165.2.2  haad 	if (pp->pr_ipl == IPL_SOFTNET || pp->pr_ipl == IPL_SOFTCLOCK ||
   1621  1.165.2.2  haad 	    pp->pr_ipl == IPL_SOFTSERIAL) {
   1622  1.165.2.2  haad 		KERNEL_LOCK(1, NULL);
   1623  1.165.2.2  haad 		klock = true;
   1624  1.165.2.2  haad 	} else
   1625  1.165.2.2  haad 		klock = false;
   1626  1.165.2.2  haad 
   1627  1.165.2.2  haad 	/* Reclaim items from the pool's cache (if any). */
   1628  1.165.2.2  haad 	if (pp->pr_cache != NULL)
   1629  1.165.2.2  haad 		pool_cache_invalidate(pp->pr_cache);
   1630  1.165.2.2  haad 
   1631  1.165.2.2  haad 	if (mutex_tryenter(&pp->pr_lock) == 0) {
   1632  1.165.2.2  haad 		if (klock) {
   1633  1.165.2.2  haad 			KERNEL_UNLOCK_ONE(NULL);
   1634  1.165.2.2  haad 		}
   1635  1.165.2.2  haad 		return (0);
   1636  1.165.2.2  haad 	}
   1637  1.165.2.2  haad 	pr_enter(pp, file, line);
   1638  1.165.2.2  haad 
   1639  1.165.2.2  haad 	LIST_INIT(&pq);
   1640  1.165.2.2  haad 
   1641  1.165.2.2  haad 	curtime = time_uptime;
   1642  1.165.2.2  haad 
   1643  1.165.2.2  haad 	for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
   1644  1.165.2.2  haad 		phnext = LIST_NEXT(ph, ph_pagelist);
   1645  1.165.2.2  haad 
   1646  1.165.2.2  haad 		/* Check our minimum page claim */
   1647  1.165.2.2  haad 		if (pp->pr_npages <= pp->pr_minpages)
   1648  1.165.2.2  haad 			break;
   1649  1.165.2.2  haad 
   1650  1.165.2.2  haad 		KASSERT(ph->ph_nmissing == 0);
   1651  1.165.2.2  haad 		if (curtime - ph->ph_time < pool_inactive_time
   1652  1.165.2.2  haad 		    && !pa_starved_p(pp->pr_alloc))
   1653  1.165.2.2  haad 			continue;
   1654  1.165.2.2  haad 
   1655  1.165.2.2  haad 		/*
   1656  1.165.2.2  haad 		 * If freeing this page would put us below
   1657  1.165.2.2  haad 		 * the low water mark, stop now.
   1658  1.165.2.2  haad 		 */
   1659  1.165.2.2  haad 		if ((pp->pr_nitems - pp->pr_itemsperpage) <
   1660  1.165.2.2  haad 		    pp->pr_minitems)
   1661  1.165.2.2  haad 			break;
   1662  1.165.2.2  haad 
   1663  1.165.2.2  haad 		pr_rmpage(pp, ph, &pq);
   1664  1.165.2.2  haad 	}
   1665  1.165.2.2  haad 
   1666  1.165.2.2  haad 	pr_leave(pp);
   1667  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
   1668  1.165.2.2  haad 
   1669  1.165.2.2  haad 	if (LIST_EMPTY(&pq))
   1670  1.165.2.2  haad 		rv = 0;
   1671  1.165.2.2  haad 	else {
   1672  1.165.2.2  haad 		pr_pagelist_free(pp, &pq);
   1673  1.165.2.2  haad 		rv = 1;
   1674  1.165.2.2  haad 	}
   1675  1.165.2.2  haad 
   1676  1.165.2.2  haad 	if (klock) {
   1677  1.165.2.2  haad 		KERNEL_UNLOCK_ONE(NULL);
   1678  1.165.2.2  haad 	}
   1679  1.165.2.2  haad 
   1680  1.165.2.2  haad 	return (rv);
   1681  1.165.2.2  haad }
   1682  1.165.2.2  haad 
   1683  1.165.2.2  haad /*
   1684  1.165.2.2  haad  * Drain pools, one at a time.  This is a two stage process;
   1685  1.165.2.2  haad  * drain_start kicks off a cross call to drain CPU-level caches
   1686  1.165.2.2  haad  * if the pool has an associated pool_cache.  drain_end waits
   1687  1.165.2.2  haad  * for those cross calls to finish, and then drains the cache
   1688  1.165.2.2  haad  * (if any) and pool.
   1689  1.165.2.2  haad  *
   1690  1.165.2.2  haad  * Note, must never be called from interrupt context.
   1691  1.165.2.2  haad  */
   1692  1.165.2.2  haad void
   1693  1.165.2.2  haad pool_drain_start(struct pool **ppp, uint64_t *wp)
   1694  1.165.2.2  haad {
   1695  1.165.2.2  haad 	struct pool *pp;
   1696  1.165.2.2  haad 
   1697  1.165.2.2  haad 	KASSERT(!TAILQ_EMPTY(&pool_head));
   1698  1.165.2.2  haad 
   1699  1.165.2.2  haad 	pp = NULL;
   1700  1.165.2.2  haad 
   1701  1.165.2.2  haad 	/* Find next pool to drain, and add a reference. */
   1702  1.165.2.2  haad 	mutex_enter(&pool_head_lock);
   1703  1.165.2.2  haad 	do {
   1704  1.165.2.2  haad 		if (drainpp == NULL) {
   1705  1.165.2.2  haad 			drainpp = TAILQ_FIRST(&pool_head);
   1706  1.165.2.2  haad 		}
   1707  1.165.2.2  haad 		if (drainpp != NULL) {
   1708  1.165.2.2  haad 			pp = drainpp;
   1709  1.165.2.2  haad 			drainpp = TAILQ_NEXT(pp, pr_poollist);
   1710  1.165.2.2  haad 		}
   1711  1.165.2.2  haad 		/*
   1712  1.165.2.2  haad 		 * Skip completely idle pools.  We depend on at least
   1713  1.165.2.2  haad 		 * one pool in the system being active.
   1714  1.165.2.2  haad 		 */
   1715  1.165.2.2  haad 	} while (pp == NULL || pp->pr_npages == 0);
   1716  1.165.2.2  haad 	pp->pr_refcnt++;
   1717  1.165.2.2  haad 	mutex_exit(&pool_head_lock);
   1718  1.165.2.2  haad 
   1719  1.165.2.2  haad 	/* If there is a pool_cache, drain CPU level caches. */
   1720  1.165.2.2  haad 	*ppp = pp;
   1721  1.165.2.2  haad 	if (pp->pr_cache != NULL) {
   1722  1.165.2.2  haad 		*wp = xc_broadcast(0, (xcfunc_t)pool_cache_xcall,
   1723  1.165.2.2  haad 		    pp->pr_cache, NULL);
   1724  1.165.2.2  haad 	}
   1725  1.165.2.2  haad }
   1726  1.165.2.2  haad 
   1727  1.165.2.2  haad void
   1728  1.165.2.2  haad pool_drain_end(struct pool *pp, uint64_t where)
   1729  1.165.2.2  haad {
   1730  1.165.2.2  haad 
   1731  1.165.2.2  haad 	if (pp == NULL)
   1732  1.165.2.2  haad 		return;
   1733  1.165.2.2  haad 
   1734  1.165.2.2  haad 	KASSERT(pp->pr_refcnt > 0);
   1735  1.165.2.2  haad 
   1736  1.165.2.2  haad 	/* Wait for remote draining to complete. */
   1737  1.165.2.2  haad 	if (pp->pr_cache != NULL)
   1738  1.165.2.2  haad 		xc_wait(where);
   1739  1.165.2.2  haad 
   1740  1.165.2.2  haad 	/* Drain the cache (if any) and pool.. */
   1741  1.165.2.2  haad 	pool_reclaim(pp);
   1742  1.165.2.2  haad 
   1743  1.165.2.2  haad 	/* Finally, unlock the pool. */
   1744  1.165.2.2  haad 	mutex_enter(&pool_head_lock);
   1745  1.165.2.2  haad 	pp->pr_refcnt--;
   1746  1.165.2.2  haad 	cv_broadcast(&pool_busy);
   1747  1.165.2.2  haad 	mutex_exit(&pool_head_lock);
   1748  1.165.2.2  haad }
   1749  1.165.2.2  haad 
   1750  1.165.2.2  haad /*
   1751  1.165.2.2  haad  * Diagnostic helpers.
   1752  1.165.2.2  haad  */
   1753  1.165.2.2  haad void
   1754  1.165.2.2  haad pool_print(struct pool *pp, const char *modif)
   1755  1.165.2.2  haad {
   1756  1.165.2.2  haad 
   1757  1.165.2.2  haad 	pool_print1(pp, modif, printf);
   1758  1.165.2.2  haad }
   1759  1.165.2.2  haad 
   1760  1.165.2.2  haad void
   1761  1.165.2.2  haad pool_printall(const char *modif, void (*pr)(const char *, ...))
   1762  1.165.2.2  haad {
   1763  1.165.2.2  haad 	struct pool *pp;
   1764  1.165.2.2  haad 
   1765  1.165.2.2  haad 	TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
   1766  1.165.2.2  haad 		pool_printit(pp, modif, pr);
   1767  1.165.2.2  haad 	}
   1768  1.165.2.2  haad }
   1769  1.165.2.2  haad 
   1770  1.165.2.2  haad void
   1771  1.165.2.2  haad pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
   1772  1.165.2.2  haad {
   1773  1.165.2.2  haad 
   1774  1.165.2.2  haad 	if (pp == NULL) {
   1775  1.165.2.2  haad 		(*pr)("Must specify a pool to print.\n");
   1776  1.165.2.2  haad 		return;
   1777  1.165.2.2  haad 	}
   1778  1.165.2.2  haad 
   1779  1.165.2.2  haad 	pool_print1(pp, modif, pr);
   1780  1.165.2.2  haad }
   1781  1.165.2.2  haad 
   1782  1.165.2.2  haad static void
   1783  1.165.2.2  haad pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
   1784  1.165.2.2  haad     void (*pr)(const char *, ...))
   1785  1.165.2.2  haad {
   1786  1.165.2.2  haad 	struct pool_item_header *ph;
   1787  1.165.2.2  haad #ifdef DIAGNOSTIC
   1788  1.165.2.2  haad 	struct pool_item *pi;
   1789  1.165.2.2  haad #endif
   1790  1.165.2.2  haad 
   1791  1.165.2.2  haad 	LIST_FOREACH(ph, pl, ph_pagelist) {
   1792  1.165.2.2  haad 		(*pr)("\t\tpage %p, nmissing %d, time %" PRIu32 "\n",
   1793  1.165.2.2  haad 		    ph->ph_page, ph->ph_nmissing, ph->ph_time);
   1794  1.165.2.2  haad #ifdef DIAGNOSTIC
   1795  1.165.2.2  haad 		if (!(pp->pr_roflags & PR_NOTOUCH)) {
   1796  1.165.2.2  haad 			LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
   1797  1.165.2.2  haad 				if (pi->pi_magic != PI_MAGIC) {
   1798  1.165.2.2  haad 					(*pr)("\t\t\titem %p, magic 0x%x\n",
   1799  1.165.2.2  haad 					    pi, pi->pi_magic);
   1800  1.165.2.2  haad 				}
   1801  1.165.2.2  haad 			}
   1802  1.165.2.2  haad 		}
   1803  1.165.2.2  haad #endif
   1804  1.165.2.2  haad 	}
   1805  1.165.2.2  haad }
   1806  1.165.2.2  haad 
   1807  1.165.2.2  haad static void
   1808  1.165.2.2  haad pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
   1809  1.165.2.2  haad {
   1810  1.165.2.2  haad 	struct pool_item_header *ph;
   1811  1.165.2.2  haad 	pool_cache_t pc;
   1812  1.165.2.2  haad 	pcg_t *pcg;
   1813  1.165.2.2  haad 	pool_cache_cpu_t *cc;
   1814  1.165.2.2  haad 	uint64_t cpuhit, cpumiss;
   1815  1.165.2.2  haad 	int i, print_log = 0, print_pagelist = 0, print_cache = 0;
   1816  1.165.2.2  haad 	char c;
   1817  1.165.2.2  haad 
   1818  1.165.2.2  haad 	while ((c = *modif++) != '\0') {
   1819  1.165.2.2  haad 		if (c == 'l')
   1820  1.165.2.2  haad 			print_log = 1;
   1821  1.165.2.2  haad 		if (c == 'p')
   1822  1.165.2.2  haad 			print_pagelist = 1;
   1823  1.165.2.2  haad 		if (c == 'c')
   1824  1.165.2.2  haad 			print_cache = 1;
   1825  1.165.2.2  haad 	}
   1826  1.165.2.2  haad 
   1827  1.165.2.2  haad 	if ((pc = pp->pr_cache) != NULL) {
   1828  1.165.2.2  haad 		(*pr)("POOL CACHE");
   1829  1.165.2.2  haad 	} else {
   1830  1.165.2.2  haad 		(*pr)("POOL");
   1831  1.165.2.2  haad 	}
   1832  1.165.2.2  haad 
   1833  1.165.2.2  haad 	(*pr)(" %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
   1834  1.165.2.2  haad 	    pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
   1835  1.165.2.2  haad 	    pp->pr_roflags);
   1836  1.165.2.2  haad 	(*pr)("\talloc %p\n", pp->pr_alloc);
   1837  1.165.2.2  haad 	(*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
   1838  1.165.2.2  haad 	    pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
   1839  1.165.2.2  haad 	(*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
   1840  1.165.2.2  haad 	    pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
   1841  1.165.2.2  haad 
   1842  1.165.2.2  haad 	(*pr)("\tnget %lu, nfail %lu, nput %lu\n",
   1843  1.165.2.2  haad 	    pp->pr_nget, pp->pr_nfail, pp->pr_nput);
   1844  1.165.2.2  haad 	(*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
   1845  1.165.2.2  haad 	    pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
   1846  1.165.2.2  haad 
   1847  1.165.2.2  haad 	if (print_pagelist == 0)
   1848  1.165.2.2  haad 		goto skip_pagelist;
   1849  1.165.2.2  haad 
   1850  1.165.2.2  haad 	if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
   1851  1.165.2.2  haad 		(*pr)("\n\tempty page list:\n");
   1852  1.165.2.2  haad 	pool_print_pagelist(pp, &pp->pr_emptypages, pr);
   1853  1.165.2.2  haad 	if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
   1854  1.165.2.2  haad 		(*pr)("\n\tfull page list:\n");
   1855  1.165.2.2  haad 	pool_print_pagelist(pp, &pp->pr_fullpages, pr);
   1856  1.165.2.2  haad 	if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
   1857  1.165.2.2  haad 		(*pr)("\n\tpartial-page list:\n");
   1858  1.165.2.2  haad 	pool_print_pagelist(pp, &pp->pr_partpages, pr);
   1859  1.165.2.2  haad 
   1860  1.165.2.2  haad 	if (pp->pr_curpage == NULL)
   1861  1.165.2.2  haad 		(*pr)("\tno current page\n");
   1862  1.165.2.2  haad 	else
   1863  1.165.2.2  haad 		(*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
   1864  1.165.2.2  haad 
   1865  1.165.2.2  haad  skip_pagelist:
   1866  1.165.2.2  haad 	if (print_log == 0)
   1867  1.165.2.2  haad 		goto skip_log;
   1868  1.165.2.2  haad 
   1869  1.165.2.2  haad 	(*pr)("\n");
   1870  1.165.2.2  haad 	if ((pp->pr_roflags & PR_LOGGING) == 0)
   1871  1.165.2.2  haad 		(*pr)("\tno log\n");
   1872  1.165.2.2  haad 	else {
   1873  1.165.2.2  haad 		pr_printlog(pp, NULL, pr);
   1874  1.165.2.2  haad 	}
   1875  1.165.2.2  haad 
   1876  1.165.2.2  haad  skip_log:
   1877  1.165.2.2  haad 
   1878  1.165.2.2  haad #define PR_GROUPLIST(pcg)						\
   1879  1.165.2.2  haad 	(*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail);		\
   1880  1.165.2.2  haad 	for (i = 0; i < pcg->pcg_size; i++) {				\
   1881  1.165.2.2  haad 		if (pcg->pcg_objects[i].pcgo_pa !=			\
   1882  1.165.2.2  haad 		    POOL_PADDR_INVALID) {				\
   1883  1.165.2.2  haad 			(*pr)("\t\t\t%p, 0x%llx\n",			\
   1884  1.165.2.2  haad 			    pcg->pcg_objects[i].pcgo_va,		\
   1885  1.165.2.2  haad 			    (unsigned long long)			\
   1886  1.165.2.2  haad 			    pcg->pcg_objects[i].pcgo_pa);		\
   1887  1.165.2.2  haad 		} else {						\
   1888  1.165.2.2  haad 			(*pr)("\t\t\t%p\n",				\
   1889  1.165.2.2  haad 			    pcg->pcg_objects[i].pcgo_va);		\
   1890  1.165.2.2  haad 		}							\
   1891  1.165.2.2  haad 	}
   1892  1.165.2.2  haad 
   1893  1.165.2.2  haad 	if (pc != NULL) {
   1894  1.165.2.2  haad 		cpuhit = 0;
   1895  1.165.2.2  haad 		cpumiss = 0;
   1896  1.165.2.2  haad 		for (i = 0; i < MAXCPUS; i++) {
   1897  1.165.2.2  haad 			if ((cc = pc->pc_cpus[i]) == NULL)
   1898  1.165.2.2  haad 				continue;
   1899  1.165.2.2  haad 			cpuhit += cc->cc_hits;
   1900  1.165.2.2  haad 			cpumiss += cc->cc_misses;
   1901  1.165.2.2  haad 		}
   1902  1.165.2.2  haad 		(*pr)("\tcpu layer hits %llu misses %llu\n", cpuhit, cpumiss);
   1903  1.165.2.2  haad 		(*pr)("\tcache layer hits %llu misses %llu\n",
   1904  1.165.2.2  haad 		    pc->pc_hits, pc->pc_misses);
   1905  1.165.2.2  haad 		(*pr)("\tcache layer entry uncontended %llu contended %llu\n",
   1906  1.165.2.2  haad 		    pc->pc_hits + pc->pc_misses - pc->pc_contended,
   1907  1.165.2.2  haad 		    pc->pc_contended);
   1908  1.165.2.2  haad 		(*pr)("\tcache layer empty groups %u full groups %u\n",
   1909  1.165.2.2  haad 		    pc->pc_nempty, pc->pc_nfull);
   1910  1.165.2.2  haad 		if (print_cache) {
   1911  1.165.2.2  haad 			(*pr)("\tfull cache groups:\n");
   1912  1.165.2.2  haad 			for (pcg = pc->pc_fullgroups; pcg != NULL;
   1913  1.165.2.2  haad 			    pcg = pcg->pcg_next) {
   1914  1.165.2.2  haad 				PR_GROUPLIST(pcg);
   1915  1.165.2.2  haad 			}
   1916  1.165.2.2  haad 			(*pr)("\tempty cache groups:\n");
   1917  1.165.2.2  haad 			for (pcg = pc->pc_emptygroups; pcg != NULL;
   1918  1.165.2.2  haad 			    pcg = pcg->pcg_next) {
   1919  1.165.2.2  haad 				PR_GROUPLIST(pcg);
   1920  1.165.2.2  haad 			}
   1921  1.165.2.2  haad 		}
   1922  1.165.2.2  haad 	}
   1923  1.165.2.2  haad #undef PR_GROUPLIST
   1924  1.165.2.2  haad 
   1925  1.165.2.2  haad 	pr_enter_check(pp, pr);
   1926  1.165.2.2  haad }
   1927  1.165.2.2  haad 
   1928  1.165.2.2  haad static int
   1929  1.165.2.2  haad pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
   1930  1.165.2.2  haad {
   1931  1.165.2.2  haad 	struct pool_item *pi;
   1932  1.165.2.2  haad 	void *page;
   1933  1.165.2.2  haad 	int n;
   1934  1.165.2.2  haad 
   1935  1.165.2.2  haad 	if ((pp->pr_roflags & PR_NOALIGN) == 0) {
   1936  1.165.2.2  haad 		page = (void *)((uintptr_t)ph & pp->pr_alloc->pa_pagemask);
   1937  1.165.2.2  haad 		if (page != ph->ph_page &&
   1938  1.165.2.2  haad 		    (pp->pr_roflags & PR_PHINPAGE) != 0) {
   1939  1.165.2.2  haad 			if (label != NULL)
   1940  1.165.2.2  haad 				printf("%s: ", label);
   1941  1.165.2.2  haad 			printf("pool(%p:%s): page inconsistency: page %p;"
   1942  1.165.2.2  haad 			       " at page head addr %p (p %p)\n", pp,
   1943  1.165.2.2  haad 				pp->pr_wchan, ph->ph_page,
   1944  1.165.2.2  haad 				ph, page);
   1945  1.165.2.2  haad 			return 1;
   1946  1.165.2.2  haad 		}
   1947  1.165.2.2  haad 	}
   1948  1.165.2.2  haad 
   1949  1.165.2.2  haad 	if ((pp->pr_roflags & PR_NOTOUCH) != 0)
   1950  1.165.2.2  haad 		return 0;
   1951  1.165.2.2  haad 
   1952  1.165.2.2  haad 	for (pi = LIST_FIRST(&ph->ph_itemlist), n = 0;
   1953  1.165.2.2  haad 	     pi != NULL;
   1954  1.165.2.2  haad 	     pi = LIST_NEXT(pi,pi_list), n++) {
   1955  1.165.2.2  haad 
   1956  1.165.2.2  haad #ifdef DIAGNOSTIC
   1957  1.165.2.2  haad 		if (pi->pi_magic != PI_MAGIC) {
   1958  1.165.2.2  haad 			if (label != NULL)
   1959  1.165.2.2  haad 				printf("%s: ", label);
   1960  1.165.2.2  haad 			printf("pool(%s): free list modified: magic=%x;"
   1961  1.165.2.2  haad 			       " page %p; item ordinal %d; addr %p\n",
   1962  1.165.2.2  haad 				pp->pr_wchan, pi->pi_magic, ph->ph_page,
   1963  1.165.2.2  haad 				n, pi);
   1964  1.165.2.2  haad 			panic("pool");
   1965  1.165.2.2  haad 		}
   1966  1.165.2.2  haad #endif
   1967  1.165.2.2  haad 		if ((pp->pr_roflags & PR_NOALIGN) != 0) {
   1968  1.165.2.2  haad 			continue;
   1969  1.165.2.2  haad 		}
   1970  1.165.2.2  haad 		page = (void *)((uintptr_t)pi & pp->pr_alloc->pa_pagemask);
   1971  1.165.2.2  haad 		if (page == ph->ph_page)
   1972  1.165.2.2  haad 			continue;
   1973  1.165.2.2  haad 
   1974  1.165.2.2  haad 		if (label != NULL)
   1975  1.165.2.2  haad 			printf("%s: ", label);
   1976  1.165.2.2  haad 		printf("pool(%p:%s): page inconsistency: page %p;"
   1977  1.165.2.2  haad 		       " item ordinal %d; addr %p (p %p)\n", pp,
   1978  1.165.2.2  haad 			pp->pr_wchan, ph->ph_page,
   1979  1.165.2.2  haad 			n, pi, page);
   1980  1.165.2.2  haad 		return 1;
   1981  1.165.2.2  haad 	}
   1982  1.165.2.2  haad 	return 0;
   1983  1.165.2.2  haad }
   1984  1.165.2.2  haad 
   1985  1.165.2.2  haad 
   1986  1.165.2.2  haad int
   1987  1.165.2.2  haad pool_chk(struct pool *pp, const char *label)
   1988  1.165.2.2  haad {
   1989  1.165.2.2  haad 	struct pool_item_header *ph;
   1990  1.165.2.2  haad 	int r = 0;
   1991  1.165.2.2  haad 
   1992  1.165.2.2  haad 	mutex_enter(&pp->pr_lock);
   1993  1.165.2.2  haad 	LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
   1994  1.165.2.2  haad 		r = pool_chk_page(pp, label, ph);
   1995  1.165.2.2  haad 		if (r) {
   1996  1.165.2.2  haad 			goto out;
   1997  1.165.2.2  haad 		}
   1998  1.165.2.2  haad 	}
   1999  1.165.2.2  haad 	LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
   2000  1.165.2.2  haad 		r = pool_chk_page(pp, label, ph);
   2001  1.165.2.2  haad 		if (r) {
   2002  1.165.2.2  haad 			goto out;
   2003  1.165.2.2  haad 		}
   2004  1.165.2.2  haad 	}
   2005  1.165.2.2  haad 	LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
   2006  1.165.2.2  haad 		r = pool_chk_page(pp, label, ph);
   2007  1.165.2.2  haad 		if (r) {
   2008  1.165.2.2  haad 			goto out;
   2009  1.165.2.2  haad 		}
   2010  1.165.2.2  haad 	}
   2011  1.165.2.2  haad 
   2012  1.165.2.2  haad out:
   2013  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
   2014  1.165.2.2  haad 	return (r);
   2015  1.165.2.2  haad }
   2016  1.165.2.2  haad 
   2017  1.165.2.2  haad /*
   2018  1.165.2.2  haad  * pool_cache_init:
   2019  1.165.2.2  haad  *
   2020  1.165.2.2  haad  *	Initialize a pool cache.
   2021  1.165.2.2  haad  */
   2022  1.165.2.2  haad pool_cache_t
   2023  1.165.2.2  haad pool_cache_init(size_t size, u_int align, u_int align_offset, u_int flags,
   2024  1.165.2.2  haad     const char *wchan, struct pool_allocator *palloc, int ipl,
   2025  1.165.2.2  haad     int (*ctor)(void *, void *, int), void (*dtor)(void *, void *), void *arg)
   2026  1.165.2.2  haad {
   2027  1.165.2.2  haad 	pool_cache_t pc;
   2028  1.165.2.2  haad 
   2029  1.165.2.2  haad 	pc = pool_get(&cache_pool, PR_WAITOK);
   2030  1.165.2.2  haad 	if (pc == NULL)
   2031  1.165.2.2  haad 		return NULL;
   2032  1.165.2.2  haad 
   2033  1.165.2.2  haad 	pool_cache_bootstrap(pc, size, align, align_offset, flags, wchan,
   2034  1.165.2.2  haad 	   palloc, ipl, ctor, dtor, arg);
   2035  1.165.2.2  haad 
   2036  1.165.2.2  haad 	return pc;
   2037  1.165.2.2  haad }
   2038  1.165.2.2  haad 
   2039  1.165.2.2  haad /*
   2040  1.165.2.2  haad  * pool_cache_bootstrap:
   2041  1.165.2.2  haad  *
   2042  1.165.2.2  haad  *	Kernel-private version of pool_cache_init().  The caller
   2043  1.165.2.2  haad  *	provides initial storage.
   2044  1.165.2.2  haad  */
   2045  1.165.2.2  haad void
   2046  1.165.2.2  haad pool_cache_bootstrap(pool_cache_t pc, size_t size, u_int align,
   2047  1.165.2.2  haad     u_int align_offset, u_int flags, const char *wchan,
   2048  1.165.2.2  haad     struct pool_allocator *palloc, int ipl,
   2049  1.165.2.2  haad     int (*ctor)(void *, void *, int), void (*dtor)(void *, void *),
   2050  1.165.2.2  haad     void *arg)
   2051  1.165.2.2  haad {
   2052  1.165.2.2  haad 	CPU_INFO_ITERATOR cii;
   2053  1.165.2.2  haad 	pool_cache_t pc1;
   2054  1.165.2.2  haad 	struct cpu_info *ci;
   2055  1.165.2.2  haad 	struct pool *pp;
   2056  1.165.2.2  haad 
   2057  1.165.2.2  haad 	pp = &pc->pc_pool;
   2058  1.165.2.2  haad 	if (palloc == NULL && ipl == IPL_NONE)
   2059  1.165.2.2  haad 		palloc = &pool_allocator_nointr;
   2060  1.165.2.2  haad 	pool_init(pp, size, align, align_offset, flags, wchan, palloc, ipl);
   2061  1.165.2.2  haad 	mutex_init(&pc->pc_lock, MUTEX_DEFAULT, ipl);
   2062  1.165.2.2  haad 
   2063  1.165.2.2  haad 	if (ctor == NULL) {
   2064  1.165.2.2  haad 		ctor = (int (*)(void *, void *, int))nullop;
   2065  1.165.2.2  haad 	}
   2066  1.165.2.2  haad 	if (dtor == NULL) {
   2067  1.165.2.2  haad 		dtor = (void (*)(void *, void *))nullop;
   2068  1.165.2.2  haad 	}
   2069  1.165.2.2  haad 
   2070  1.165.2.2  haad 	pc->pc_emptygroups = NULL;
   2071  1.165.2.2  haad 	pc->pc_fullgroups = NULL;
   2072  1.165.2.2  haad 	pc->pc_partgroups = NULL;
   2073  1.165.2.2  haad 	pc->pc_ctor = ctor;
   2074  1.165.2.2  haad 	pc->pc_dtor = dtor;
   2075  1.165.2.2  haad 	pc->pc_arg  = arg;
   2076  1.165.2.2  haad 	pc->pc_hits  = 0;
   2077  1.165.2.2  haad 	pc->pc_misses = 0;
   2078  1.165.2.2  haad 	pc->pc_nempty = 0;
   2079  1.165.2.2  haad 	pc->pc_npart = 0;
   2080  1.165.2.2  haad 	pc->pc_nfull = 0;
   2081  1.165.2.2  haad 	pc->pc_contended = 0;
   2082  1.165.2.2  haad 	pc->pc_refcnt = 0;
   2083  1.165.2.2  haad 	pc->pc_freecheck = NULL;
   2084  1.165.2.2  haad 
   2085  1.165.2.2  haad 	if ((flags & PR_LARGECACHE) != 0) {
   2086  1.165.2.2  haad 		pc->pc_pcgsize = PCG_NOBJECTS_LARGE;
   2087  1.165.2.2  haad 		pc->pc_pcgpool = &pcg_large_pool;
   2088  1.165.2.2  haad 	} else {
   2089  1.165.2.2  haad 		pc->pc_pcgsize = PCG_NOBJECTS_NORMAL;
   2090  1.165.2.2  haad 		pc->pc_pcgpool = &pcg_normal_pool;
   2091  1.165.2.2  haad 	}
   2092  1.165.2.2  haad 
   2093  1.165.2.2  haad 	/* Allocate per-CPU caches. */
   2094  1.165.2.2  haad 	memset(pc->pc_cpus, 0, sizeof(pc->pc_cpus));
   2095  1.165.2.2  haad 	pc->pc_ncpu = 0;
   2096  1.165.2.2  haad 	if (ncpu < 2) {
   2097  1.165.2.2  haad 		/* XXX For sparc: boot CPU is not attached yet. */
   2098  1.165.2.2  haad 		pool_cache_cpu_init1(curcpu(), pc);
   2099  1.165.2.2  haad 	} else {
   2100  1.165.2.2  haad 		for (CPU_INFO_FOREACH(cii, ci)) {
   2101  1.165.2.2  haad 			pool_cache_cpu_init1(ci, pc);
   2102  1.165.2.2  haad 		}
   2103  1.165.2.2  haad 	}
   2104  1.165.2.2  haad 
   2105  1.165.2.2  haad 	/* Add to list of all pools. */
   2106  1.165.2.2  haad 	if (__predict_true(!cold))
   2107  1.165.2.2  haad 		mutex_enter(&pool_head_lock);
   2108  1.165.2.2  haad 	TAILQ_FOREACH(pc1, &pool_cache_head, pc_cachelist) {
   2109  1.165.2.2  haad 		if (strcmp(pc1->pc_pool.pr_wchan, pc->pc_pool.pr_wchan) > 0)
   2110  1.165.2.2  haad 			break;
   2111  1.165.2.2  haad 	}
   2112  1.165.2.2  haad 	if (pc1 == NULL)
   2113  1.165.2.2  haad 		TAILQ_INSERT_TAIL(&pool_cache_head, pc, pc_cachelist);
   2114  1.165.2.2  haad 	else
   2115  1.165.2.2  haad 		TAILQ_INSERT_BEFORE(pc1, pc, pc_cachelist);
   2116  1.165.2.2  haad 	if (__predict_true(!cold))
   2117  1.165.2.2  haad 		mutex_exit(&pool_head_lock);
   2118  1.165.2.2  haad 
   2119  1.165.2.2  haad 	membar_sync();
   2120  1.165.2.2  haad 	pp->pr_cache = pc;
   2121  1.165.2.2  haad }
   2122  1.165.2.2  haad 
   2123  1.165.2.2  haad /*
   2124  1.165.2.2  haad  * pool_cache_destroy:
   2125  1.165.2.2  haad  *
   2126  1.165.2.2  haad  *	Destroy a pool cache.
   2127  1.165.2.2  haad  */
   2128  1.165.2.2  haad void
   2129  1.165.2.2  haad pool_cache_destroy(pool_cache_t pc)
   2130  1.165.2.2  haad {
   2131  1.165.2.2  haad 	struct pool *pp = &pc->pc_pool;
   2132  1.165.2.2  haad 	pool_cache_cpu_t *cc;
   2133  1.165.2.2  haad 	pcg_t *pcg;
   2134  1.165.2.2  haad 	int i;
   2135  1.165.2.2  haad 
   2136  1.165.2.2  haad 	/* Remove it from the global list. */
   2137  1.165.2.2  haad 	mutex_enter(&pool_head_lock);
   2138  1.165.2.2  haad 	while (pc->pc_refcnt != 0)
   2139  1.165.2.2  haad 		cv_wait(&pool_busy, &pool_head_lock);
   2140  1.165.2.2  haad 	TAILQ_REMOVE(&pool_cache_head, pc, pc_cachelist);
   2141  1.165.2.2  haad 	mutex_exit(&pool_head_lock);
   2142  1.165.2.2  haad 
   2143  1.165.2.2  haad 	/* First, invalidate the entire cache. */
   2144  1.165.2.2  haad 	pool_cache_invalidate(pc);
   2145  1.165.2.2  haad 
   2146  1.165.2.2  haad 	/* Disassociate it from the pool. */
   2147  1.165.2.2  haad 	mutex_enter(&pp->pr_lock);
   2148  1.165.2.2  haad 	pp->pr_cache = NULL;
   2149  1.165.2.2  haad 	mutex_exit(&pp->pr_lock);
   2150  1.165.2.2  haad 
   2151  1.165.2.2  haad 	/* Destroy per-CPU data */
   2152  1.165.2.2  haad 	for (i = 0; i < MAXCPUS; i++) {
   2153  1.165.2.2  haad 		if ((cc = pc->pc_cpus[i]) == NULL)
   2154  1.165.2.2  haad 			continue;
   2155  1.165.2.2  haad 		if ((pcg = cc->cc_current) != &pcg_dummy) {
   2156  1.165.2.2  haad 			pcg->pcg_next = NULL;
   2157  1.165.2.2  haad 			pool_cache_invalidate_groups(pc, pcg);
   2158  1.165.2.2  haad 		}
   2159  1.165.2.2  haad 		if ((pcg = cc->cc_previous) != &pcg_dummy) {
   2160  1.165.2.2  haad 			pcg->pcg_next = NULL;
   2161  1.165.2.2  haad 			pool_cache_invalidate_groups(pc, pcg);
   2162  1.165.2.2  haad 		}
   2163  1.165.2.2  haad 		if (cc != &pc->pc_cpu0)
   2164  1.165.2.2  haad 			pool_put(&cache_cpu_pool, cc);
   2165  1.165.2.2  haad 	}
   2166  1.165.2.2  haad 
   2167  1.165.2.2  haad 	/* Finally, destroy it. */
   2168  1.165.2.2  haad 	mutex_destroy(&pc->pc_lock);
   2169  1.165.2.2  haad 	pool_destroy(pp);
   2170  1.165.2.2  haad 	pool_put(&cache_pool, pc);
   2171  1.165.2.2  haad }
   2172  1.165.2.2  haad 
   2173  1.165.2.2  haad /*
   2174  1.165.2.2  haad  * pool_cache_cpu_init1:
   2175  1.165.2.2  haad  *
   2176  1.165.2.2  haad  *	Called for each pool_cache whenever a new CPU is attached.
   2177  1.165.2.2  haad  */
   2178  1.165.2.2  haad static void
   2179  1.165.2.2  haad pool_cache_cpu_init1(struct cpu_info *ci, pool_cache_t pc)
   2180  1.165.2.2  haad {
   2181  1.165.2.2  haad 	pool_cache_cpu_t *cc;
   2182  1.165.2.2  haad 	int index;
   2183  1.165.2.2  haad 
   2184  1.165.2.2  haad 	index = ci->ci_index;
   2185  1.165.2.2  haad 
   2186  1.165.2.2  haad 	KASSERT(index < MAXCPUS);
   2187  1.165.2.2  haad 
   2188  1.165.2.2  haad 	if ((cc = pc->pc_cpus[index]) != NULL) {
   2189  1.165.2.2  haad 		KASSERT(cc->cc_cpuindex == index);
   2190  1.165.2.2  haad 		return;
   2191  1.165.2.2  haad 	}
   2192  1.165.2.2  haad 
   2193  1.165.2.2  haad 	/*
   2194  1.165.2.2  haad 	 * The first CPU is 'free'.  This needs to be the case for
   2195  1.165.2.2  haad 	 * bootstrap - we may not be able to allocate yet.
   2196  1.165.2.2  haad 	 */
   2197  1.165.2.2  haad 	if (pc->pc_ncpu == 0) {
   2198  1.165.2.2  haad 		cc = &pc->pc_cpu0;
   2199  1.165.2.2  haad 		pc->pc_ncpu = 1;
   2200  1.165.2.2  haad 	} else {
   2201  1.165.2.2  haad 		mutex_enter(&pc->pc_lock);
   2202  1.165.2.2  haad 		pc->pc_ncpu++;
   2203  1.165.2.2  haad 		mutex_exit(&pc->pc_lock);
   2204  1.165.2.2  haad 		cc = pool_get(&cache_cpu_pool, PR_WAITOK);
   2205  1.165.2.2  haad 	}
   2206  1.165.2.2  haad 
   2207  1.165.2.2  haad 	cc->cc_ipl = pc->pc_pool.pr_ipl;
   2208  1.165.2.2  haad 	cc->cc_iplcookie = makeiplcookie(cc->cc_ipl);
   2209  1.165.2.2  haad 	cc->cc_cache = pc;
   2210  1.165.2.2  haad 	cc->cc_cpuindex = index;
   2211  1.165.2.2  haad 	cc->cc_hits = 0;
   2212  1.165.2.2  haad 	cc->cc_misses = 0;
   2213  1.165.2.2  haad 	cc->cc_current = __UNCONST(&pcg_dummy);
   2214  1.165.2.2  haad 	cc->cc_previous = __UNCONST(&pcg_dummy);
   2215  1.165.2.2  haad 
   2216  1.165.2.2  haad 	pc->pc_cpus[index] = cc;
   2217  1.165.2.2  haad }
   2218  1.165.2.2  haad 
   2219  1.165.2.2  haad /*
   2220  1.165.2.2  haad  * pool_cache_cpu_init:
   2221  1.165.2.2  haad  *
   2222  1.165.2.2  haad  *	Called whenever a new CPU is attached.
   2223  1.165.2.2  haad  */
   2224  1.165.2.2  haad void
   2225  1.165.2.2  haad pool_cache_cpu_init(struct cpu_info *ci)
   2226  1.165.2.2  haad {
   2227  1.165.2.2  haad 	pool_cache_t pc;
   2228  1.165.2.2  haad 
   2229  1.165.2.2  haad 	mutex_enter(&pool_head_lock);
   2230  1.165.2.2  haad 	TAILQ_FOREACH(pc, &pool_cache_head, pc_cachelist) {
   2231  1.165.2.2  haad 		pc->pc_refcnt++;
   2232  1.165.2.2  haad 		mutex_exit(&pool_head_lock);
   2233  1.165.2.2  haad 
   2234  1.165.2.2  haad 		pool_cache_cpu_init1(ci, pc);
   2235  1.165.2.2  haad 
   2236  1.165.2.2  haad 		mutex_enter(&pool_head_lock);
   2237  1.165.2.2  haad 		pc->pc_refcnt--;
   2238  1.165.2.2  haad 		cv_broadcast(&pool_busy);
   2239  1.165.2.2  haad 	}
   2240  1.165.2.2  haad 	mutex_exit(&pool_head_lock);
   2241  1.165.2.2  haad }
   2242  1.165.2.2  haad 
   2243  1.165.2.2  haad /*
   2244  1.165.2.2  haad  * pool_cache_reclaim:
   2245  1.165.2.2  haad  *
   2246  1.165.2.2  haad  *	Reclaim memory from a pool cache.
   2247  1.165.2.2  haad  */
   2248  1.165.2.2  haad bool
   2249  1.165.2.2  haad pool_cache_reclaim(pool_cache_t pc)
   2250  1.165.2.2  haad {
   2251  1.165.2.2  haad 
   2252  1.165.2.2  haad 	return pool_reclaim(&pc->pc_pool);
   2253  1.165.2.2  haad }
   2254  1.165.2.2  haad 
   2255  1.165.2.2  haad static void
   2256  1.165.2.2  haad pool_cache_destruct_object1(pool_cache_t pc, void *object)
   2257  1.165.2.2  haad {
   2258  1.165.2.2  haad 
   2259  1.165.2.2  haad 	(*pc->pc_dtor)(pc->pc_arg, object);
   2260  1.165.2.2  haad 	pool_put(&pc->pc_pool, object);
   2261  1.165.2.2  haad }
   2262  1.165.2.2  haad 
   2263  1.165.2.2  haad /*
   2264  1.165.2.2  haad  * pool_cache_destruct_object:
   2265  1.165.2.2  haad  *
   2266  1.165.2.2  haad  *	Force destruction of an object and its release back into
   2267  1.165.2.2  haad  *	the pool.
   2268  1.165.2.2  haad  */
   2269  1.165.2.2  haad void
   2270  1.165.2.2  haad pool_cache_destruct_object(pool_cache_t pc, void *object)
   2271  1.165.2.2  haad {
   2272  1.165.2.2  haad 
   2273  1.165.2.2  haad 	FREECHECK_IN(&pc->pc_freecheck, object);
   2274  1.165.2.2  haad 
   2275  1.165.2.2  haad 	pool_cache_destruct_object1(pc, object);
   2276  1.165.2.2  haad }
   2277  1.165.2.2  haad 
   2278  1.165.2.2  haad /*
   2279  1.165.2.2  haad  * pool_cache_invalidate_groups:
   2280  1.165.2.2  haad  *
   2281  1.165.2.2  haad  *	Invalidate a chain of groups and destruct all objects.
   2282  1.165.2.2  haad  */
   2283  1.165.2.2  haad static void
   2284  1.165.2.2  haad pool_cache_invalidate_groups(pool_cache_t pc, pcg_t *pcg)
   2285  1.165.2.2  haad {
   2286  1.165.2.2  haad 	void *object;
   2287  1.165.2.2  haad 	pcg_t *next;
   2288  1.165.2.2  haad 	int i;
   2289  1.165.2.2  haad 
   2290  1.165.2.2  haad 	for (; pcg != NULL; pcg = next) {
   2291  1.165.2.2  haad 		next = pcg->pcg_next;
   2292  1.165.2.2  haad 
   2293  1.165.2.2  haad 		for (i = 0; i < pcg->pcg_avail; i++) {
   2294  1.165.2.2  haad 			object = pcg->pcg_objects[i].pcgo_va;
   2295  1.165.2.2  haad 			pool_cache_destruct_object1(pc, object);
   2296  1.165.2.2  haad 		}
   2297  1.165.2.2  haad 
   2298  1.165.2.2  haad 		if (pcg->pcg_size == PCG_NOBJECTS_LARGE) {
   2299  1.165.2.2  haad 			pool_put(&pcg_large_pool, pcg);
   2300  1.165.2.2  haad 		} else {
   2301  1.165.2.2  haad 			KASSERT(pcg->pcg_size == PCG_NOBJECTS_NORMAL);
   2302  1.165.2.2  haad 			pool_put(&pcg_normal_pool, pcg);
   2303  1.165.2.2  haad 		}
   2304  1.165.2.2  haad 	}
   2305  1.165.2.2  haad }
   2306  1.165.2.2  haad 
   2307  1.165.2.2  haad /*
   2308  1.165.2.2  haad  * pool_cache_invalidate:
   2309  1.165.2.2  haad  *
   2310  1.165.2.2  haad  *	Invalidate a pool cache (destruct and release all of the
   2311  1.165.2.2  haad  *	cached objects).  Does not reclaim objects from the pool.
   2312  1.165.2.2  haad  */
   2313  1.165.2.2  haad void
   2314  1.165.2.2  haad pool_cache_invalidate(pool_cache_t pc)
   2315  1.165.2.2  haad {
   2316  1.165.2.2  haad 	pcg_t *full, *empty, *part;
   2317  1.165.2.2  haad 
   2318  1.165.2.2  haad 	mutex_enter(&pc->pc_lock);
   2319  1.165.2.2  haad 	full = pc->pc_fullgroups;
   2320  1.165.2.2  haad 	empty = pc->pc_emptygroups;
   2321  1.165.2.2  haad 	part = pc->pc_partgroups;
   2322  1.165.2.2  haad 	pc->pc_fullgroups = NULL;
   2323  1.165.2.2  haad 	pc->pc_emptygroups = NULL;
   2324  1.165.2.2  haad 	pc->pc_partgroups = NULL;
   2325  1.165.2.2  haad 	pc->pc_nfull = 0;
   2326  1.165.2.2  haad 	pc->pc_nempty = 0;
   2327  1.165.2.2  haad 	pc->pc_npart = 0;
   2328  1.165.2.2  haad 	mutex_exit(&pc->pc_lock);
   2329  1.165.2.2  haad 
   2330  1.165.2.2  haad 	pool_cache_invalidate_groups(pc, full);
   2331  1.165.2.2  haad 	pool_cache_invalidate_groups(pc, empty);
   2332  1.165.2.2  haad 	pool_cache_invalidate_groups(pc, part);
   2333  1.165.2.2  haad }
   2334  1.165.2.2  haad 
   2335  1.165.2.2  haad void
   2336  1.165.2.2  haad pool_cache_set_drain_hook(pool_cache_t pc, void (*fn)(void *, int), void *arg)
   2337  1.165.2.2  haad {
   2338  1.165.2.2  haad 
   2339  1.165.2.2  haad 	pool_set_drain_hook(&pc->pc_pool, fn, arg);
   2340  1.165.2.2  haad }
   2341  1.165.2.2  haad 
   2342  1.165.2.2  haad void
   2343  1.165.2.2  haad pool_cache_setlowat(pool_cache_t pc, int n)
   2344  1.165.2.2  haad {
   2345  1.165.2.2  haad 
   2346  1.165.2.2  haad 	pool_setlowat(&pc->pc_pool, n);
   2347  1.165.2.2  haad }
   2348  1.165.2.2  haad 
   2349  1.165.2.2  haad void
   2350  1.165.2.2  haad pool_cache_sethiwat(pool_cache_t pc, int n)
   2351  1.165.2.2  haad {
   2352  1.165.2.2  haad 
   2353  1.165.2.2  haad 	pool_sethiwat(&pc->pc_pool, n);
   2354  1.165.2.2  haad }
   2355  1.165.2.2  haad 
   2356  1.165.2.2  haad void
   2357  1.165.2.2  haad pool_cache_sethardlimit(pool_cache_t pc, int n, const char *warnmess, int ratecap)
   2358  1.165.2.2  haad {
   2359  1.165.2.2  haad 
   2360  1.165.2.2  haad 	pool_sethardlimit(&pc->pc_pool, n, warnmess, ratecap);
   2361  1.165.2.2  haad }
   2362  1.165.2.2  haad 
   2363  1.165.2.2  haad static bool __noinline
   2364  1.165.2.2  haad pool_cache_get_slow(pool_cache_cpu_t *cc, int s, void **objectp,
   2365  1.165.2.2  haad 		    paddr_t *pap, int flags)
   2366  1.165.2.2  haad {
   2367  1.165.2.2  haad 	pcg_t *pcg, *cur;
   2368  1.165.2.2  haad 	uint64_t ncsw;
   2369  1.165.2.2  haad 	pool_cache_t pc;
   2370  1.165.2.2  haad 	void *object;
   2371  1.165.2.2  haad 
   2372  1.165.2.2  haad 	KASSERT(cc->cc_current->pcg_avail == 0);
   2373  1.165.2.2  haad 	KASSERT(cc->cc_previous->pcg_avail == 0);
   2374  1.165.2.2  haad 
   2375  1.165.2.2  haad 	pc = cc->cc_cache;
   2376  1.165.2.2  haad 	cc->cc_misses++;
   2377  1.165.2.2  haad 
   2378  1.165.2.2  haad 	/*
   2379  1.165.2.2  haad 	 * Nothing was available locally.  Try and grab a group
   2380  1.165.2.2  haad 	 * from the cache.
   2381  1.165.2.2  haad 	 */
   2382  1.165.2.2  haad 	if (__predict_false(!mutex_tryenter(&pc->pc_lock))) {
   2383  1.165.2.2  haad 		ncsw = curlwp->l_ncsw;
   2384  1.165.2.2  haad 		mutex_enter(&pc->pc_lock);
   2385  1.165.2.2  haad 		pc->pc_contended++;
   2386  1.165.2.2  haad 
   2387  1.165.2.2  haad 		/*
   2388  1.165.2.2  haad 		 * If we context switched while locking, then
   2389  1.165.2.2  haad 		 * our view of the per-CPU data is invalid:
   2390  1.165.2.2  haad 		 * retry.
   2391  1.165.2.2  haad 		 */
   2392  1.165.2.2  haad 		if (curlwp->l_ncsw != ncsw) {
   2393  1.165.2.2  haad 			mutex_exit(&pc->pc_lock);
   2394  1.165.2.2  haad 			return true;
   2395  1.165.2.2  haad 		}
   2396  1.165.2.2  haad 	}
   2397  1.165.2.2  haad 
   2398  1.165.2.2  haad 	if (__predict_true((pcg = pc->pc_fullgroups) != NULL)) {
   2399  1.165.2.2  haad 		/*
   2400  1.165.2.2  haad 		 * If there's a full group, release our empty
   2401  1.165.2.2  haad 		 * group back to the cache.  Install the full
   2402  1.165.2.2  haad 		 * group as cc_current and return.
   2403  1.165.2.2  haad 		 */
   2404  1.165.2.2  haad 		if (__predict_true((cur = cc->cc_current) != &pcg_dummy)) {
   2405  1.165.2.2  haad 			KASSERT(cur->pcg_avail == 0);
   2406  1.165.2.2  haad 			cur->pcg_next = pc->pc_emptygroups;
   2407  1.165.2.2  haad 			pc->pc_emptygroups = cur;
   2408  1.165.2.2  haad 			pc->pc_nempty++;
   2409  1.165.2.2  haad 		}
   2410  1.165.2.2  haad 		KASSERT(pcg->pcg_avail == pcg->pcg_size);
   2411  1.165.2.2  haad 		cc->cc_current = pcg;
   2412  1.165.2.2  haad 		pc->pc_fullgroups = pcg->pcg_next;
   2413  1.165.2.2  haad 		pc->pc_hits++;
   2414  1.165.2.2  haad 		pc->pc_nfull--;
   2415  1.165.2.2  haad 		mutex_exit(&pc->pc_lock);
   2416  1.165.2.2  haad 		return true;
   2417  1.165.2.2  haad 	}
   2418  1.165.2.2  haad 
   2419  1.165.2.2  haad 	/*
   2420  1.165.2.2  haad 	 * Nothing available locally or in cache.  Take the slow
   2421  1.165.2.2  haad 	 * path: fetch a new object from the pool and construct
   2422  1.165.2.2  haad 	 * it.
   2423  1.165.2.2  haad 	 */
   2424  1.165.2.2  haad 	pc->pc_misses++;
   2425  1.165.2.2  haad 	mutex_exit(&pc->pc_lock);
   2426  1.165.2.2  haad 	splx(s);
   2427  1.165.2.2  haad 
   2428  1.165.2.2  haad 	object = pool_get(&pc->pc_pool, flags);
   2429  1.165.2.2  haad 	*objectp = object;
   2430  1.165.2.2  haad 	if (__predict_false(object == NULL))
   2431  1.165.2.2  haad 		return false;
   2432  1.165.2.2  haad 
   2433  1.165.2.2  haad 	if (__predict_false((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0)) {
   2434  1.165.2.2  haad 		pool_put(&pc->pc_pool, object);
   2435  1.165.2.2  haad 		*objectp = NULL;
   2436  1.165.2.2  haad 		return false;
   2437  1.165.2.2  haad 	}
   2438  1.165.2.2  haad 
   2439  1.165.2.2  haad 	KASSERT((((vaddr_t)object + pc->pc_pool.pr_itemoffset) &
   2440  1.165.2.2  haad 	    (pc->pc_pool.pr_align - 1)) == 0);
   2441  1.165.2.2  haad 
   2442  1.165.2.2  haad 	if (pap != NULL) {
   2443  1.165.2.2  haad #ifdef POOL_VTOPHYS
   2444  1.165.2.2  haad 		*pap = POOL_VTOPHYS(object);
   2445  1.165.2.2  haad #else
   2446  1.165.2.2  haad 		*pap = POOL_PADDR_INVALID;
   2447  1.165.2.2  haad #endif
   2448  1.165.2.2  haad 	}
   2449  1.165.2.2  haad 
   2450  1.165.2.2  haad 	FREECHECK_OUT(&pc->pc_freecheck, object);
   2451  1.165.2.2  haad 	return false;
   2452  1.165.2.2  haad }
   2453  1.165.2.2  haad 
   2454  1.165.2.2  haad /*
   2455  1.165.2.2  haad  * pool_cache_get{,_paddr}:
   2456  1.165.2.2  haad  *
   2457  1.165.2.2  haad  *	Get an object from a pool cache (optionally returning
   2458  1.165.2.2  haad  *	the physical address of the object).
   2459  1.165.2.2  haad  */
   2460  1.165.2.2  haad void *
   2461  1.165.2.2  haad pool_cache_get_paddr(pool_cache_t pc, int flags, paddr_t *pap)
   2462  1.165.2.2  haad {
   2463  1.165.2.2  haad 	pool_cache_cpu_t *cc;
   2464  1.165.2.2  haad 	pcg_t *pcg;
   2465  1.165.2.2  haad 	void *object;
   2466  1.165.2.2  haad 	int s;
   2467  1.165.2.2  haad 
   2468  1.165.2.2  haad #ifdef LOCKDEBUG
   2469  1.165.2.2  haad 	if (flags & PR_WAITOK) {
   2470  1.165.2.2  haad 		ASSERT_SLEEPABLE();
   2471  1.165.2.2  haad 	}
   2472  1.165.2.2  haad #endif
   2473  1.165.2.2  haad 
   2474  1.165.2.2  haad 	/* Lock out interrupts and disable preemption. */
   2475  1.165.2.2  haad 	s = splvm();
   2476  1.165.2.2  haad 	while (/* CONSTCOND */ true) {
   2477  1.165.2.2  haad 		/* Try and allocate an object from the current group. */
   2478  1.165.2.2  haad 		cc = pc->pc_cpus[curcpu()->ci_index];
   2479  1.165.2.2  haad 		KASSERT(cc->cc_cache == pc);
   2480  1.165.2.2  haad 	 	pcg = cc->cc_current;
   2481  1.165.2.2  haad 		if (__predict_true(pcg->pcg_avail > 0)) {
   2482  1.165.2.2  haad 			object = pcg->pcg_objects[--pcg->pcg_avail].pcgo_va;
   2483  1.165.2.2  haad 			if (__predict_false(pap != NULL))
   2484  1.165.2.2  haad 				*pap = pcg->pcg_objects[pcg->pcg_avail].pcgo_pa;
   2485  1.165.2.2  haad #if defined(DIAGNOSTIC)
   2486  1.165.2.2  haad 			pcg->pcg_objects[pcg->pcg_avail].pcgo_va = NULL;
   2487  1.165.2.2  haad 			KASSERT(pcg->pcg_avail < pcg->pcg_size);
   2488  1.165.2.2  haad 			KASSERT(object != NULL);
   2489  1.165.2.2  haad #endif
   2490  1.165.2.2  haad 			cc->cc_hits++;
   2491  1.165.2.2  haad 			splx(s);
   2492  1.165.2.2  haad 			FREECHECK_OUT(&pc->pc_freecheck, object);
   2493  1.165.2.2  haad 			return object;
   2494  1.165.2.2  haad 		}
   2495  1.165.2.2  haad 
   2496  1.165.2.2  haad 		/*
   2497  1.165.2.2  haad 		 * That failed.  If the previous group isn't empty, swap
   2498  1.165.2.2  haad 		 * it with the current group and allocate from there.
   2499  1.165.2.2  haad 		 */
   2500  1.165.2.2  haad 		pcg = cc->cc_previous;
   2501  1.165.2.2  haad 		if (__predict_true(pcg->pcg_avail > 0)) {
   2502  1.165.2.2  haad 			cc->cc_previous = cc->cc_current;
   2503  1.165.2.2  haad 			cc->cc_current = pcg;
   2504  1.165.2.2  haad 			continue;
   2505  1.165.2.2  haad 		}
   2506  1.165.2.2  haad 
   2507  1.165.2.2  haad 		/*
   2508  1.165.2.2  haad 		 * Can't allocate from either group: try the slow path.
   2509  1.165.2.2  haad 		 * If get_slow() allocated an object for us, or if
   2510  1.165.2.2  haad 		 * no more objects are available, it will return false.
   2511  1.165.2.2  haad 		 * Otherwise, we need to retry.
   2512  1.165.2.2  haad 		 */
   2513  1.165.2.2  haad 		if (!pool_cache_get_slow(cc, s, &object, pap, flags))
   2514  1.165.2.2  haad 			break;
   2515  1.165.2.2  haad 	}
   2516  1.165.2.2  haad 
   2517  1.165.2.2  haad 	return object;
   2518  1.165.2.2  haad }
   2519  1.165.2.2  haad 
   2520  1.165.2.2  haad static bool __noinline
   2521  1.165.2.2  haad pool_cache_put_slow(pool_cache_cpu_t *cc, int s, void *object)
   2522  1.165.2.2  haad {
   2523  1.165.2.2  haad 	pcg_t *pcg, *cur;
   2524  1.165.2.2  haad 	uint64_t ncsw;
   2525  1.165.2.2  haad 	pool_cache_t pc;
   2526  1.165.2.2  haad 
   2527  1.165.2.2  haad 	KASSERT(cc->cc_current->pcg_avail == cc->cc_current->pcg_size);
   2528  1.165.2.2  haad 	KASSERT(cc->cc_previous->pcg_avail == cc->cc_previous->pcg_size);
   2529  1.165.2.2  haad 
   2530  1.165.2.2  haad 	pc = cc->cc_cache;
   2531  1.165.2.2  haad 	cc->cc_misses++;
   2532  1.165.2.2  haad 
   2533  1.165.2.2  haad 	/* Lock the cache. */
   2534  1.165.2.2  haad 	if (__predict_false(!mutex_tryenter(&pc->pc_lock))) {
   2535  1.165.2.2  haad 		ncsw = curlwp->l_ncsw;
   2536  1.165.2.2  haad 		mutex_enter(&pc->pc_lock);
   2537  1.165.2.2  haad 		pc->pc_contended++;
   2538  1.165.2.2  haad 
   2539  1.165.2.2  haad 		/*
   2540  1.165.2.2  haad 		 * If we context switched while locking, then our view of
   2541  1.165.2.2  haad 		 * the per-CPU data is invalid: retry.
   2542  1.165.2.2  haad 		 */
   2543  1.165.2.2  haad 		if (__predict_false(curlwp->l_ncsw != ncsw)) {
   2544  1.165.2.2  haad 			mutex_exit(&pc->pc_lock);
   2545  1.165.2.2  haad 			return true;
   2546  1.165.2.2  haad 		}
   2547  1.165.2.2  haad 	}
   2548  1.165.2.2  haad 
   2549  1.165.2.2  haad 	/* If there are no empty groups in the cache then allocate one. */
   2550  1.165.2.2  haad 	if (__predict_false((pcg = pc->pc_emptygroups) == NULL)) {
   2551  1.165.2.2  haad 		if (__predict_true(!pool_cache_disable)) {
   2552  1.165.2.2  haad 			pcg = pool_get(pc->pc_pcgpool, PR_NOWAIT);
   2553  1.165.2.2  haad 		}
   2554  1.165.2.2  haad 		if (__predict_true(pcg != NULL)) {
   2555  1.165.2.2  haad 			pcg->pcg_avail = 0;
   2556  1.165.2.2  haad 			pcg->pcg_size = pc->pc_pcgsize;
   2557  1.165.2.2  haad 		}
   2558  1.165.2.2  haad 	} else {
   2559  1.165.2.2  haad 		pc->pc_emptygroups = pcg->pcg_next;
   2560  1.165.2.2  haad 		pc->pc_nempty--;
   2561  1.165.2.2  haad 	}
   2562  1.165.2.2  haad 
   2563  1.165.2.2  haad 	/*
   2564  1.165.2.2  haad 	 * If there's a empty group, release our full group back
   2565  1.165.2.2  haad 	 * to the cache.  Install the empty group to the local CPU
   2566  1.165.2.2  haad 	 * and return.
   2567  1.165.2.2  haad 	 */
   2568  1.165.2.2  haad 	if (pcg != NULL) {
   2569  1.165.2.2  haad 		KASSERT(pcg->pcg_avail == 0);
   2570  1.165.2.2  haad 		if (__predict_false(cc->cc_previous == &pcg_dummy)) {
   2571  1.165.2.2  haad 			cc->cc_previous = pcg;
   2572  1.165.2.2  haad 		} else {
   2573  1.165.2.2  haad 			cur = cc->cc_current;
   2574  1.165.2.2  haad 			if (__predict_true(cur != &pcg_dummy)) {
   2575  1.165.2.2  haad 				KASSERT(cur->pcg_avail == cur->pcg_size);
   2576  1.165.2.2  haad 				cur->pcg_next = pc->pc_fullgroups;
   2577  1.165.2.2  haad 				pc->pc_fullgroups = cur;
   2578  1.165.2.2  haad 				pc->pc_nfull++;
   2579  1.165.2.2  haad 			}
   2580  1.165.2.2  haad 			cc->cc_current = pcg;
   2581  1.165.2.2  haad 		}
   2582  1.165.2.2  haad 		pc->pc_hits++;
   2583  1.165.2.2  haad 		mutex_exit(&pc->pc_lock);
   2584  1.165.2.2  haad 		return true;
   2585  1.165.2.2  haad 	}
   2586  1.165.2.2  haad 
   2587  1.165.2.2  haad 	/*
   2588  1.165.2.2  haad 	 * Nothing available locally or in cache, and we didn't
   2589  1.165.2.2  haad 	 * allocate an empty group.  Take the slow path and destroy
   2590  1.165.2.2  haad 	 * the object here and now.
   2591  1.165.2.2  haad 	 */
   2592  1.165.2.2  haad 	pc->pc_misses++;
   2593  1.165.2.2  haad 	mutex_exit(&pc->pc_lock);
   2594  1.165.2.2  haad 	splx(s);
   2595  1.165.2.2  haad 	pool_cache_destruct_object(pc, object);
   2596  1.165.2.2  haad 
   2597  1.165.2.2  haad 	return false;
   2598  1.165.2.2  haad }
   2599  1.165.2.2  haad 
   2600  1.165.2.2  haad /*
   2601  1.165.2.2  haad  * pool_cache_put{,_paddr}:
   2602  1.165.2.2  haad  *
   2603  1.165.2.2  haad  *	Put an object back to the pool cache (optionally caching the
   2604  1.165.2.2  haad  *	physical address of the object).
   2605  1.165.2.2  haad  */
   2606  1.165.2.2  haad void
   2607  1.165.2.2  haad pool_cache_put_paddr(pool_cache_t pc, void *object, paddr_t pa)
   2608  1.165.2.2  haad {
   2609  1.165.2.2  haad 	pool_cache_cpu_t *cc;
   2610  1.165.2.2  haad 	pcg_t *pcg;
   2611  1.165.2.2  haad 	int s;
   2612  1.165.2.2  haad 
   2613  1.165.2.2  haad 	FREECHECK_IN(&pc->pc_freecheck, object);
   2614  1.165.2.2  haad 
   2615  1.165.2.2  haad 	/* Lock out interrupts and disable preemption. */
   2616  1.165.2.2  haad 	s = splvm();
   2617  1.165.2.2  haad 	while (/* CONSTCOND */ true) {
   2618  1.165.2.2  haad 		/* If the current group isn't full, release it there. */
   2619  1.165.2.2  haad 		cc = pc->pc_cpus[curcpu()->ci_index];
   2620  1.165.2.2  haad 		KASSERT(cc->cc_cache == pc);
   2621  1.165.2.2  haad 	 	pcg = cc->cc_current;
   2622  1.165.2.2  haad 		if (__predict_true(pcg->pcg_avail < pcg->pcg_size)) {
   2623  1.165.2.2  haad 			pcg->pcg_objects[pcg->pcg_avail].pcgo_va = object;
   2624  1.165.2.2  haad 			pcg->pcg_objects[pcg->pcg_avail].pcgo_pa = pa;
   2625  1.165.2.2  haad 			pcg->pcg_avail++;
   2626  1.165.2.2  haad 			cc->cc_hits++;
   2627  1.165.2.2  haad 			splx(s);
   2628  1.165.2.2  haad 			return;
   2629  1.165.2.2  haad 		}
   2630  1.165.2.2  haad 
   2631  1.165.2.2  haad 		/*
   2632  1.165.2.2  haad 		 * That failed.  If the previous group isn't full, swap
   2633  1.165.2.2  haad 		 * it with the current group and try again.
   2634  1.165.2.2  haad 		 */
   2635  1.165.2.2  haad 		pcg = cc->cc_previous;
   2636  1.165.2.2  haad 		if (__predict_true(pcg->pcg_avail < pcg->pcg_size)) {
   2637  1.165.2.2  haad 			cc->cc_previous = cc->cc_current;
   2638  1.165.2.2  haad 			cc->cc_current = pcg;
   2639  1.165.2.2  haad 			continue;
   2640  1.165.2.2  haad 		}
   2641  1.165.2.2  haad 
   2642  1.165.2.2  haad 		/*
   2643  1.165.2.2  haad 		 * Can't free to either group: try the slow path.
   2644  1.165.2.2  haad 		 * If put_slow() releases the object for us, it
   2645  1.165.2.2  haad 		 * will return false.  Otherwise we need to retry.
   2646  1.165.2.2  haad 		 */
   2647  1.165.2.2  haad 		if (!pool_cache_put_slow(cc, s, object))
   2648  1.165.2.2  haad 			break;
   2649  1.165.2.2  haad 	}
   2650  1.165.2.2  haad }
   2651  1.165.2.2  haad 
   2652  1.165.2.2  haad /*
   2653  1.165.2.2  haad  * pool_cache_xcall:
   2654  1.165.2.2  haad  *
   2655  1.165.2.2  haad  *	Transfer objects from the per-CPU cache to the global cache.
   2656  1.165.2.2  haad  *	Run within a cross-call thread.
   2657  1.165.2.2  haad  */
   2658  1.165.2.2  haad static void
   2659  1.165.2.2  haad pool_cache_xcall(pool_cache_t pc)
   2660  1.165.2.2  haad {
   2661  1.165.2.2  haad 	pool_cache_cpu_t *cc;
   2662  1.165.2.2  haad 	pcg_t *prev, *cur, **list;
   2663  1.165.2.2  haad 	int s;
   2664  1.165.2.2  haad 
   2665  1.165.2.2  haad 	s = splvm();
   2666  1.165.2.2  haad 	mutex_enter(&pc->pc_lock);
   2667  1.165.2.2  haad 	cc = pc->pc_cpus[curcpu()->ci_index];
   2668  1.165.2.2  haad 	cur = cc->cc_current;
   2669  1.165.2.2  haad 	cc->cc_current = __UNCONST(&pcg_dummy);
   2670  1.165.2.2  haad 	prev = cc->cc_previous;
   2671  1.165.2.2  haad 	cc->cc_previous = __UNCONST(&pcg_dummy);
   2672  1.165.2.2  haad 	if (cur != &pcg_dummy) {
   2673  1.165.2.2  haad 		if (cur->pcg_avail == cur->pcg_size) {
   2674  1.165.2.2  haad 			list = &pc->pc_fullgroups;
   2675  1.165.2.2  haad 			pc->pc_nfull++;
   2676  1.165.2.2  haad 		} else if (cur->pcg_avail == 0) {
   2677  1.165.2.2  haad 			list = &pc->pc_emptygroups;
   2678  1.165.2.2  haad 			pc->pc_nempty++;
   2679  1.165.2.2  haad 		} else {
   2680  1.165.2.2  haad 			list = &pc->pc_partgroups;
   2681  1.165.2.2  haad 			pc->pc_npart++;
   2682  1.165.2.2  haad 		}
   2683  1.165.2.2  haad 		cur->pcg_next = *list;
   2684  1.165.2.2  haad 		*list = cur;
   2685  1.165.2.2  haad 	}
   2686  1.165.2.2  haad 	if (prev != &pcg_dummy) {
   2687  1.165.2.2  haad 		if (prev->pcg_avail == prev->pcg_size) {
   2688  1.165.2.2  haad 			list = &pc->pc_fullgroups;
   2689  1.165.2.2  haad 			pc->pc_nfull++;
   2690  1.165.2.2  haad 		} else if (prev->pcg_avail == 0) {
   2691  1.165.2.2  haad 			list = &pc->pc_emptygroups;
   2692  1.165.2.2  haad 			pc->pc_nempty++;
   2693  1.165.2.2  haad 		} else {
   2694  1.165.2.2  haad 			list = &pc->pc_partgroups;
   2695  1.165.2.2  haad 			pc->pc_npart++;
   2696  1.165.2.2  haad 		}
   2697  1.165.2.2  haad 		prev->pcg_next = *list;
   2698  1.165.2.2  haad 		*list = prev;
   2699  1.165.2.2  haad 	}
   2700  1.165.2.2  haad 	mutex_exit(&pc->pc_lock);
   2701  1.165.2.2  haad 	splx(s);
   2702  1.165.2.2  haad }
   2703  1.165.2.2  haad 
   2704  1.165.2.2  haad /*
   2705  1.165.2.2  haad  * Pool backend allocators.
   2706  1.165.2.2  haad  *
   2707  1.165.2.2  haad  * Each pool has a backend allocator that handles allocation, deallocation,
   2708  1.165.2.2  haad  * and any additional draining that might be needed.
   2709  1.165.2.2  haad  *
   2710  1.165.2.2  haad  * We provide two standard allocators:
   2711  1.165.2.2  haad  *
   2712  1.165.2.2  haad  *	pool_allocator_kmem - the default when no allocator is specified
   2713  1.165.2.2  haad  *
   2714  1.165.2.2  haad  *	pool_allocator_nointr - used for pools that will not be accessed
   2715  1.165.2.2  haad  *	in interrupt context.
   2716  1.165.2.2  haad  */
   2717  1.165.2.2  haad void	*pool_page_alloc(struct pool *, int);
   2718  1.165.2.2  haad void	pool_page_free(struct pool *, void *);
   2719  1.165.2.2  haad 
   2720  1.165.2.2  haad #ifdef POOL_SUBPAGE
   2721  1.165.2.2  haad struct pool_allocator pool_allocator_kmem_fullpage = {
   2722  1.165.2.2  haad 	pool_page_alloc, pool_page_free, 0,
   2723  1.165.2.2  haad 	.pa_backingmapptr = &kmem_map,
   2724  1.165.2.2  haad };
   2725  1.165.2.2  haad #else
   2726  1.165.2.2  haad struct pool_allocator pool_allocator_kmem = {
   2727  1.165.2.2  haad 	pool_page_alloc, pool_page_free, 0,
   2728  1.165.2.2  haad 	.pa_backingmapptr = &kmem_map,
   2729  1.165.2.2  haad };
   2730  1.165.2.2  haad #endif
   2731  1.165.2.2  haad 
   2732  1.165.2.2  haad void	*pool_page_alloc_nointr(struct pool *, int);
   2733  1.165.2.2  haad void	pool_page_free_nointr(struct pool *, void *);
   2734  1.165.2.2  haad 
   2735  1.165.2.2  haad #ifdef POOL_SUBPAGE
   2736  1.165.2.2  haad struct pool_allocator pool_allocator_nointr_fullpage = {
   2737  1.165.2.2  haad 	pool_page_alloc_nointr, pool_page_free_nointr, 0,
   2738  1.165.2.2  haad 	.pa_backingmapptr = &kernel_map,
   2739  1.165.2.2  haad };
   2740  1.165.2.2  haad #else
   2741  1.165.2.2  haad struct pool_allocator pool_allocator_nointr = {
   2742  1.165.2.2  haad 	pool_page_alloc_nointr, pool_page_free_nointr, 0,
   2743  1.165.2.2  haad 	.pa_backingmapptr = &kernel_map,
   2744  1.165.2.2  haad };
   2745  1.165.2.2  haad #endif
   2746  1.165.2.2  haad 
   2747  1.165.2.2  haad #ifdef POOL_SUBPAGE
   2748  1.165.2.2  haad void	*pool_subpage_alloc(struct pool *, int);
   2749  1.165.2.2  haad void	pool_subpage_free(struct pool *, void *);
   2750  1.165.2.2  haad 
   2751  1.165.2.2  haad struct pool_allocator pool_allocator_kmem = {
   2752  1.165.2.2  haad 	pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
   2753  1.165.2.2  haad 	.pa_backingmapptr = &kmem_map,
   2754  1.165.2.2  haad };
   2755  1.165.2.2  haad 
   2756  1.165.2.2  haad void	*pool_subpage_alloc_nointr(struct pool *, int);
   2757  1.165.2.2  haad void	pool_subpage_free_nointr(struct pool *, void *);
   2758  1.165.2.2  haad 
   2759  1.165.2.2  haad struct pool_allocator pool_allocator_nointr = {
   2760  1.165.2.2  haad 	pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
   2761  1.165.2.2  haad 	.pa_backingmapptr = &kmem_map,
   2762  1.165.2.2  haad };
   2763  1.165.2.2  haad #endif /* POOL_SUBPAGE */
   2764  1.165.2.2  haad 
   2765  1.165.2.2  haad static void *
   2766  1.165.2.2  haad pool_allocator_alloc(struct pool *pp, int flags)
   2767  1.165.2.2  haad {
   2768  1.165.2.2  haad 	struct pool_allocator *pa = pp->pr_alloc;
   2769  1.165.2.2  haad 	void *res;
   2770  1.165.2.2  haad 
   2771  1.165.2.2  haad 	res = (*pa->pa_alloc)(pp, flags);
   2772  1.165.2.2  haad 	if (res == NULL && (flags & PR_WAITOK) == 0) {
   2773  1.165.2.2  haad 		/*
   2774  1.165.2.2  haad 		 * We only run the drain hook here if PR_NOWAIT.
   2775  1.165.2.2  haad 		 * In other cases, the hook will be run in
   2776  1.165.2.2  haad 		 * pool_reclaim().
   2777  1.165.2.2  haad 		 */
   2778  1.165.2.2  haad 		if (pp->pr_drain_hook != NULL) {
   2779  1.165.2.2  haad 			(*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
   2780  1.165.2.2  haad 			res = (*pa->pa_alloc)(pp, flags);
   2781  1.165.2.2  haad 		}
   2782  1.165.2.2  haad 	}
   2783  1.165.2.2  haad 	return res;
   2784  1.165.2.2  haad }
   2785  1.165.2.2  haad 
   2786  1.165.2.2  haad static void
   2787  1.165.2.2  haad pool_allocator_free(struct pool *pp, void *v)
   2788  1.165.2.2  haad {
   2789  1.165.2.2  haad 	struct pool_allocator *pa = pp->pr_alloc;
   2790  1.165.2.2  haad 
   2791  1.165.2.2  haad 	(*pa->pa_free)(pp, v);
   2792  1.165.2.2  haad }
   2793  1.165.2.2  haad 
   2794  1.165.2.2  haad void *
   2795  1.165.2.2  haad pool_page_alloc(struct pool *pp, int flags)
   2796  1.165.2.2  haad {
   2797  1.165.2.2  haad 	bool waitok = (flags & PR_WAITOK) ? true : false;
   2798  1.165.2.2  haad 
   2799  1.165.2.2  haad 	return ((void *) uvm_km_alloc_poolpage_cache(kmem_map, waitok));
   2800  1.165.2.2  haad }
   2801  1.165.2.2  haad 
   2802  1.165.2.2  haad void
   2803  1.165.2.2  haad pool_page_free(struct pool *pp, void *v)
   2804  1.165.2.2  haad {
   2805  1.165.2.2  haad 
   2806  1.165.2.2  haad 	uvm_km_free_poolpage_cache(kmem_map, (vaddr_t) v);
   2807  1.165.2.2  haad }
   2808  1.165.2.2  haad 
   2809  1.165.2.2  haad static void *
   2810  1.165.2.2  haad pool_page_alloc_meta(struct pool *pp, int flags)
   2811  1.165.2.2  haad {
   2812  1.165.2.2  haad 	bool waitok = (flags & PR_WAITOK) ? true : false;
   2813  1.165.2.2  haad 
   2814  1.165.2.2  haad 	return ((void *) uvm_km_alloc_poolpage(kmem_map, waitok));
   2815  1.165.2.2  haad }
   2816  1.165.2.2  haad 
   2817  1.165.2.2  haad static void
   2818  1.165.2.2  haad pool_page_free_meta(struct pool *pp, void *v)
   2819  1.165.2.2  haad {
   2820  1.165.2.2  haad 
   2821  1.165.2.2  haad 	uvm_km_free_poolpage(kmem_map, (vaddr_t) v);
   2822  1.165.2.2  haad }
   2823  1.165.2.2  haad 
   2824  1.165.2.2  haad #ifdef POOL_SUBPAGE
   2825  1.165.2.2  haad /* Sub-page allocator, for machines with large hardware pages. */
   2826  1.165.2.2  haad void *
   2827  1.165.2.2  haad pool_subpage_alloc(struct pool *pp, int flags)
   2828  1.165.2.2  haad {
   2829  1.165.2.2  haad 	return pool_get(&psppool, flags);
   2830  1.165.2.2  haad }
   2831  1.165.2.2  haad 
   2832  1.165.2.2  haad void
   2833  1.165.2.2  haad pool_subpage_free(struct pool *pp, void *v)
   2834  1.165.2.2  haad {
   2835  1.165.2.2  haad 	pool_put(&psppool, v);
   2836  1.165.2.2  haad }
   2837  1.165.2.2  haad 
   2838  1.165.2.2  haad /* We don't provide a real nointr allocator.  Maybe later. */
   2839  1.165.2.2  haad void *
   2840  1.165.2.2  haad pool_subpage_alloc_nointr(struct pool *pp, int flags)
   2841  1.165.2.2  haad {
   2842  1.165.2.2  haad 
   2843  1.165.2.2  haad 	return (pool_subpage_alloc(pp, flags));
   2844  1.165.2.2  haad }
   2845  1.165.2.2  haad 
   2846  1.165.2.2  haad void
   2847  1.165.2.2  haad pool_subpage_free_nointr(struct pool *pp, void *v)
   2848  1.165.2.2  haad {
   2849  1.165.2.2  haad 
   2850  1.165.2.2  haad 	pool_subpage_free(pp, v);
   2851  1.165.2.2  haad }
   2852  1.165.2.2  haad #endif /* POOL_SUBPAGE */
   2853  1.165.2.2  haad void *
   2854  1.165.2.2  haad pool_page_alloc_nointr(struct pool *pp, int flags)
   2855  1.165.2.2  haad {
   2856  1.165.2.2  haad 	bool waitok = (flags & PR_WAITOK) ? true : false;
   2857  1.165.2.2  haad 
   2858  1.165.2.2  haad 	return ((void *) uvm_km_alloc_poolpage_cache(kernel_map, waitok));
   2859  1.165.2.2  haad }
   2860  1.165.2.2  haad 
   2861  1.165.2.2  haad void
   2862  1.165.2.2  haad pool_page_free_nointr(struct pool *pp, void *v)
   2863  1.165.2.2  haad {
   2864  1.165.2.2  haad 
   2865  1.165.2.2  haad 	uvm_km_free_poolpage_cache(kernel_map, (vaddr_t) v);
   2866  1.165.2.2  haad }
   2867  1.165.2.2  haad 
   2868  1.165.2.2  haad #if defined(DDB)
   2869  1.165.2.2  haad static bool
   2870  1.165.2.2  haad pool_in_page(struct pool *pp, struct pool_item_header *ph, uintptr_t addr)
   2871  1.165.2.2  haad {
   2872  1.165.2.2  haad 
   2873  1.165.2.2  haad 	return (uintptr_t)ph->ph_page <= addr &&
   2874  1.165.2.2  haad 	    addr < (uintptr_t)ph->ph_page + pp->pr_alloc->pa_pagesz;
   2875  1.165.2.2  haad }
   2876  1.165.2.2  haad 
   2877  1.165.2.2  haad static bool
   2878  1.165.2.2  haad pool_in_item(struct pool *pp, void *item, uintptr_t addr)
   2879  1.165.2.2  haad {
   2880  1.165.2.2  haad 
   2881  1.165.2.2  haad 	return (uintptr_t)item <= addr && addr < (uintptr_t)item + pp->pr_size;
   2882  1.165.2.2  haad }
   2883  1.165.2.2  haad 
   2884  1.165.2.2  haad static bool
   2885  1.165.2.2  haad pool_in_cg(struct pool *pp, struct pool_cache_group *pcg, uintptr_t addr)
   2886  1.165.2.2  haad {
   2887  1.165.2.2  haad 	int i;
   2888  1.165.2.2  haad 
   2889  1.165.2.2  haad 	if (pcg == NULL) {
   2890  1.165.2.2  haad 		return false;
   2891  1.165.2.2  haad 	}
   2892  1.165.2.2  haad 	for (i = 0; i < pcg->pcg_avail; i++) {
   2893  1.165.2.2  haad 		if (pool_in_item(pp, pcg->pcg_objects[i].pcgo_va, addr)) {
   2894  1.165.2.2  haad 			return true;
   2895  1.165.2.2  haad 		}
   2896  1.165.2.2  haad 	}
   2897  1.165.2.2  haad 	return false;
   2898  1.165.2.2  haad }
   2899  1.165.2.2  haad 
   2900  1.165.2.2  haad static bool
   2901  1.165.2.2  haad pool_allocated(struct pool *pp, struct pool_item_header *ph, uintptr_t addr)
   2902  1.165.2.2  haad {
   2903  1.165.2.2  haad 
   2904  1.165.2.2  haad 	if ((pp->pr_roflags & PR_NOTOUCH) != 0) {
   2905  1.165.2.2  haad 		unsigned int idx = pr_item_notouch_index(pp, ph, (void *)addr);
   2906  1.165.2.2  haad 		pool_item_bitmap_t *bitmap =
   2907  1.165.2.2  haad 		    ph->ph_bitmap + (idx / BITMAP_SIZE);
   2908  1.165.2.2  haad 		pool_item_bitmap_t mask = 1 << (idx & BITMAP_MASK);
   2909  1.165.2.2  haad 
   2910  1.165.2.2  haad 		return (*bitmap & mask) == 0;
   2911  1.165.2.2  haad 	} else {
   2912  1.165.2.2  haad 		struct pool_item *pi;
   2913  1.165.2.2  haad 
   2914  1.165.2.2  haad 		LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
   2915  1.165.2.2  haad 			if (pool_in_item(pp, pi, addr)) {
   2916  1.165.2.2  haad 				return false;
   2917  1.165.2.2  haad 			}
   2918  1.165.2.2  haad 		}
   2919  1.165.2.2  haad 		return true;
   2920  1.165.2.2  haad 	}
   2921  1.165.2.2  haad }
   2922  1.165.2.2  haad 
   2923  1.165.2.2  haad void
   2924  1.165.2.2  haad pool_whatis(uintptr_t addr, void (*pr)(const char *, ...))
   2925  1.165.2.2  haad {
   2926  1.165.2.2  haad 	struct pool *pp;
   2927  1.165.2.2  haad 
   2928  1.165.2.2  haad 	TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
   2929  1.165.2.2  haad 		struct pool_item_header *ph;
   2930  1.165.2.2  haad 		uintptr_t item;
   2931  1.165.2.2  haad 		bool allocated = true;
   2932  1.165.2.2  haad 		bool incache = false;
   2933  1.165.2.2  haad 		bool incpucache = false;
   2934  1.165.2.2  haad 		char cpucachestr[32];
   2935  1.165.2.2  haad 
   2936  1.165.2.2  haad 		if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
   2937  1.165.2.2  haad 			LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
   2938  1.165.2.2  haad 				if (pool_in_page(pp, ph, addr)) {
   2939  1.165.2.2  haad 					goto found;
   2940  1.165.2.2  haad 				}
   2941  1.165.2.2  haad 			}
   2942  1.165.2.2  haad 			LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
   2943  1.165.2.2  haad 				if (pool_in_page(pp, ph, addr)) {
   2944  1.165.2.2  haad 					allocated =
   2945  1.165.2.2  haad 					    pool_allocated(pp, ph, addr);
   2946  1.165.2.2  haad 					goto found;
   2947  1.165.2.2  haad 				}
   2948  1.165.2.2  haad 			}
   2949  1.165.2.2  haad 			LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
   2950  1.165.2.2  haad 				if (pool_in_page(pp, ph, addr)) {
   2951  1.165.2.2  haad 					allocated = false;
   2952  1.165.2.2  haad 					goto found;
   2953  1.165.2.2  haad 				}
   2954  1.165.2.2  haad 			}
   2955  1.165.2.2  haad 			continue;
   2956  1.165.2.2  haad 		} else {
   2957  1.165.2.2  haad 			ph = pr_find_pagehead_noalign(pp, (void *)addr);
   2958  1.165.2.2  haad 			if (ph == NULL || !pool_in_page(pp, ph, addr)) {
   2959  1.165.2.2  haad 				continue;
   2960  1.165.2.2  haad 			}
   2961  1.165.2.2  haad 			allocated = pool_allocated(pp, ph, addr);
   2962  1.165.2.2  haad 		}
   2963  1.165.2.2  haad found:
   2964  1.165.2.2  haad 		if (allocated && pp->pr_cache) {
   2965  1.165.2.2  haad 			pool_cache_t pc = pp->pr_cache;
   2966  1.165.2.2  haad 			struct pool_cache_group *pcg;
   2967  1.165.2.2  haad 			int i;
   2968  1.165.2.2  haad 
   2969  1.165.2.2  haad 			for (pcg = pc->pc_fullgroups; pcg != NULL;
   2970  1.165.2.2  haad 			    pcg = pcg->pcg_next) {
   2971  1.165.2.2  haad 				if (pool_in_cg(pp, pcg, addr)) {
   2972  1.165.2.2  haad 					incache = true;
   2973  1.165.2.2  haad 					goto print;
   2974  1.165.2.2  haad 				}
   2975  1.165.2.2  haad 			}
   2976  1.165.2.2  haad 			for (i = 0; i < MAXCPUS; i++) {
   2977  1.165.2.2  haad 				pool_cache_cpu_t *cc;
   2978  1.165.2.2  haad 
   2979  1.165.2.2  haad 				if ((cc = pc->pc_cpus[i]) == NULL) {
   2980  1.165.2.2  haad 					continue;
   2981  1.165.2.2  haad 				}
   2982  1.165.2.2  haad 				if (pool_in_cg(pp, cc->cc_current, addr) ||
   2983  1.165.2.2  haad 				    pool_in_cg(pp, cc->cc_previous, addr)) {
   2984  1.165.2.2  haad 					struct cpu_info *ci =
   2985  1.165.2.2  haad 					    cpu_lookup(i);
   2986  1.165.2.2  haad 
   2987  1.165.2.2  haad 					incpucache = true;
   2988  1.165.2.2  haad 					snprintf(cpucachestr,
   2989  1.165.2.2  haad 					    sizeof(cpucachestr),
   2990  1.165.2.2  haad 					    "cached by CPU %u",
   2991  1.165.2.2  haad 					    ci->ci_index);
   2992  1.165.2.2  haad 					goto print;
   2993  1.165.2.2  haad 				}
   2994  1.165.2.2  haad 			}
   2995  1.165.2.2  haad 		}
   2996  1.165.2.2  haad print:
   2997  1.165.2.2  haad 		item = (uintptr_t)ph->ph_page + ph->ph_off;
   2998  1.165.2.2  haad 		item = item + rounddown(addr - item, pp->pr_size);
   2999  1.165.2.2  haad 		(*pr)("%p is %p+%zu in POOL '%s' (%s)\n",
   3000  1.165.2.2  haad 		    (void *)addr, item, (size_t)(addr - item),
   3001  1.165.2.2  haad 		    pp->pr_wchan,
   3002  1.165.2.2  haad 		    incpucache ? cpucachestr :
   3003  1.165.2.2  haad 		    incache ? "cached" : allocated ? "allocated" : "free");
   3004  1.165.2.2  haad 	}
   3005  1.165.2.2  haad }
   3006  1.165.2.2  haad #endif /* defined(DDB) */
   3007