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