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