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