xref: /openbsd-src/sys/kern/subr_pool.c (revision 2022e1d0d0c97c4d7d9792ab9ba9026238cdb95e)
1 /*	$OpenBSD: subr_pool.c,v 1.105 2011/07/05 16:36:15 tedu Exp $	*/
2 /*	$NetBSD: subr_pool.c,v 1.61 2001/09/26 07:14:56 chs Exp $	*/
3 
4 /*-
5  * Copyright (c) 1997, 1999, 2000 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.
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/param.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/errno.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/pool.h>
41 #include <sys/syslog.h>
42 #include <sys/sysctl.h>
43 
44 #include <uvm/uvm.h>
45 #include <dev/rndvar.h>
46 
47 /*
48  * Pool resource management utility.
49  *
50  * Memory is allocated in pages which are split into pieces according to
51  * the pool item size. Each page is kept on one of three lists in the
52  * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages',
53  * for empty, full and partially-full pages respectively. The individual
54  * pool items are on a linked list headed by `ph_itemlist' in each page
55  * header. The memory for building the page list is either taken from
56  * the allocated pages themselves (for small pool items) or taken from
57  * an internal pool of page headers (`phpool').
58  */
59 
60 /* List of all pools */
61 TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
62 
63 /* Private pool for page header structures */
64 struct pool phpool;
65 
66 struct pool_item_header {
67 	/* Page headers */
68 	LIST_ENTRY(pool_item_header)
69 				ph_pagelist;	/* pool page list */
70 	TAILQ_HEAD(,pool_item)	ph_itemlist;	/* chunk list for this page */
71 	RB_ENTRY(pool_item_header)
72 				ph_node;	/* Off-page page headers */
73 	int			ph_nmissing;	/* # of chunks in use */
74 	caddr_t			ph_page;	/* this page's address */
75 	caddr_t			ph_colored;	/* page's colored address */
76 	int			ph_pagesize;
77 	int			ph_magic;
78 };
79 
80 struct pool_item {
81 #ifdef DIAGNOSTIC
82 	u_int32_t pi_magic;
83 #endif
84 	/* Other entries use only this list entry */
85 	TAILQ_ENTRY(pool_item)	pi_list;
86 };
87 
88 #ifdef DEADBEEF1
89 #define	PI_MAGIC DEADBEEF1
90 #else
91 #define	PI_MAGIC 0xdeafbeef
92 #endif
93 
94 #ifdef POOL_DEBUG
95 int	pool_debug = 1;
96 #else
97 int	pool_debug = 0;
98 #endif
99 
100 #define	POOL_NEEDS_CATCHUP(pp)						\
101 	((pp)->pr_nitems < (pp)->pr_minitems)
102 
103 /*
104  * Every pool gets a unique serial number assigned to it. If this counter
105  * wraps, we're screwed, but we shouldn't create so many pools anyway.
106  */
107 unsigned int pool_serial;
108 
109 int	 pool_catchup(struct pool *);
110 void	 pool_prime_page(struct pool *, caddr_t, struct pool_item_header *);
111 void	 pool_update_curpage(struct pool *);
112 void	*pool_do_get(struct pool *, int);
113 void	 pool_do_put(struct pool *, void *);
114 void	 pr_rmpage(struct pool *, struct pool_item_header *,
115 	    struct pool_pagelist *);
116 int	pool_chk_page(struct pool *, struct pool_item_header *, int);
117 struct pool_item_header *pool_alloc_item_header(struct pool *, caddr_t , int);
118 
119 void	*pool_allocator_alloc(struct pool *, int, int *);
120 void	 pool_allocator_free(struct pool *, void *);
121 
122 /*
123  * XXX - quick hack. For pools with large items we want to use a special
124  *       allocator. For now, instead of having the allocator figure out
125  *       the allocation size from the pool (which can be done trivially
126  *       with round_page(pr_itemsperpage * pr_size)) which would require
127  *	 lots of changes everywhere, we just create allocators for each
128  *	 size. We limit those to 128 pages.
129  */
130 #define POOL_LARGE_MAXPAGES 128
131 struct pool_allocator pool_allocator_large[POOL_LARGE_MAXPAGES];
132 struct pool_allocator pool_allocator_large_ni[POOL_LARGE_MAXPAGES];
133 void	*pool_large_alloc(struct pool *, int, int *);
134 void	pool_large_free(struct pool *, void *);
135 void	*pool_large_alloc_ni(struct pool *, int, int *);
136 void	pool_large_free_ni(struct pool *, void *);
137 
138 
139 #ifdef DDB
140 void	 pool_print_pagelist(struct pool_pagelist *,
141 	    int (*)(const char *, ...));
142 void	 pool_print1(struct pool *, const char *, int (*)(const char *, ...));
143 #endif
144 
145 #define pool_sleep(pl) msleep(pl, &pl->pr_mtx, PSWP, pl->pr_wchan, 0)
146 
147 static __inline int
148 phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
149 {
150 	long diff = (vaddr_t)a->ph_page - (vaddr_t)b->ph_page;
151 	if (diff < 0)
152 		return -(-diff >= a->ph_pagesize);
153 	else if (diff > 0)
154 		return (diff >= b->ph_pagesize);
155 	else
156 		return (0);
157 }
158 
159 RB_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare);
160 RB_GENERATE(phtree, pool_item_header, ph_node, phtree_compare);
161 
162 /*
163  * Return the pool page header based on page address.
164  */
165 static __inline struct pool_item_header *
166 pr_find_pagehead(struct pool *pp, void *v)
167 {
168 	struct pool_item_header *ph, tmp;
169 
170 	if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
171 		caddr_t page;
172 
173 		page = (caddr_t)((vaddr_t)v & pp->pr_alloc->pa_pagemask);
174 
175 		return ((struct pool_item_header *)(page + pp->pr_phoffset));
176 	}
177 
178 	/*
179 	 * The trick we're using in the tree compare function is to compare
180 	 * two elements equal when they overlap. We want to return the
181 	 * page header that belongs to the element just before this address.
182 	 * We don't want this element to compare equal to the next element,
183 	 * so the compare function takes the pagesize from the lower element.
184 	 * If this header is the lower, its pagesize is zero, so it can't
185 	 * overlap with the next header. But if the header we're looking for
186 	 * is lower, we'll use its pagesize and it will overlap and return
187 	 * equal.
188 	 */
189 	tmp.ph_page = v;
190 	tmp.ph_pagesize = 0;
191 	ph = RB_FIND(phtree, &pp->pr_phtree, &tmp);
192 
193 	if (ph) {
194 		KASSERT(ph->ph_page <= (caddr_t)v);
195 		KASSERT(ph->ph_page + ph->ph_pagesize > (caddr_t)v);
196 	}
197 	return ph;
198 }
199 
200 /*
201  * Remove a page from the pool.
202  */
203 void
204 pr_rmpage(struct pool *pp, struct pool_item_header *ph,
205     struct pool_pagelist *pq)
206 {
207 
208 	/*
209 	 * If the page was idle, decrement the idle page count.
210 	 */
211 	if (ph->ph_nmissing == 0) {
212 #ifdef DIAGNOSTIC
213 		if (pp->pr_nidle == 0)
214 			panic("pr_rmpage: nidle inconsistent");
215 		if (pp->pr_nitems < pp->pr_itemsperpage)
216 			panic("pr_rmpage: nitems inconsistent");
217 #endif
218 		pp->pr_nidle--;
219 	}
220 
221 	pp->pr_nitems -= pp->pr_itemsperpage;
222 
223 	/*
224 	 * Unlink a page from the pool and release it (or queue it for release).
225 	 */
226 	LIST_REMOVE(ph, ph_pagelist);
227 	if ((pp->pr_roflags & PR_PHINPAGE) == 0)
228 		RB_REMOVE(phtree, &pp->pr_phtree, ph);
229 	if (pq) {
230 		LIST_INSERT_HEAD(pq, ph, ph_pagelist);
231 	} else {
232 		pool_allocator_free(pp, ph->ph_page);
233 		if ((pp->pr_roflags & PR_PHINPAGE) == 0)
234 			pool_put(&phpool, ph);
235 	}
236 	pp->pr_npages--;
237 	pp->pr_npagefree++;
238 
239 	pool_update_curpage(pp);
240 }
241 
242 /*
243  * Initialize the given pool resource structure.
244  *
245  * We export this routine to allow other kernel parts to declare
246  * static pools that must be initialized before malloc() is available.
247  */
248 void
249 pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
250     const char *wchan, struct pool_allocator *palloc)
251 {
252 	int off, slack;
253 
254 #ifdef MALLOC_DEBUG
255 	if ((flags & PR_DEBUG) && (ioff != 0 || align != 0))
256 		flags &= ~PR_DEBUG;
257 #endif
258 	/*
259 	 * Check arguments and construct default values.
260 	 */
261 	if (palloc == NULL) {
262 		if (size > PAGE_SIZE) {
263 			int psize;
264 
265 			/*
266 			 * XXX - should take align into account as well.
267 			 */
268 			if (size == round_page(size))
269 				psize = size / PAGE_SIZE;
270 			else
271 				psize = PAGE_SIZE / roundup(size % PAGE_SIZE,
272 				    1024);
273 			if (psize > POOL_LARGE_MAXPAGES)
274 				psize = POOL_LARGE_MAXPAGES;
275 			if (flags & PR_WAITOK)
276 				palloc = &pool_allocator_large_ni[psize-1];
277 			else
278 				palloc = &pool_allocator_large[psize-1];
279 			if (palloc->pa_pagesz == 0) {
280 				palloc->pa_pagesz = psize * PAGE_SIZE;
281 				if (flags & PR_WAITOK) {
282 					palloc->pa_alloc = pool_large_alloc_ni;
283 					palloc->pa_free = pool_large_free_ni;
284 				} else {
285 					palloc->pa_alloc = pool_large_alloc;
286 					palloc->pa_free = pool_large_free;
287 				}
288 			}
289 		} else {
290 			palloc = &pool_allocator_nointr;
291 		}
292 	}
293 	if (palloc->pa_pagesz == 0) {
294 		palloc->pa_pagesz = PAGE_SIZE;
295 	}
296 	if (palloc->pa_pagemask == 0) {
297 		palloc->pa_pagemask = ~(palloc->pa_pagesz - 1);
298 		palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1;
299 	}
300 
301 	if (align == 0)
302 		align = ALIGN(1);
303 
304 	if (size < sizeof(struct pool_item))
305 		size = sizeof(struct pool_item);
306 
307 	size = roundup(size, align);
308 #ifdef DIAGNOSTIC
309 	if (size > palloc->pa_pagesz)
310 		panic("pool_init: pool item size (%lu) too large",
311 		    (u_long)size);
312 #endif
313 
314 	/*
315 	 * Initialize the pool structure.
316 	 */
317 	LIST_INIT(&pp->pr_emptypages);
318 	LIST_INIT(&pp->pr_fullpages);
319 	LIST_INIT(&pp->pr_partpages);
320 	pp->pr_curpage = NULL;
321 	pp->pr_npages = 0;
322 	pp->pr_minitems = 0;
323 	pp->pr_minpages = 0;
324 	pp->pr_maxpages = 8;
325 	pp->pr_roflags = flags;
326 	pp->pr_flags = 0;
327 	pp->pr_size = size;
328 	pp->pr_align = align;
329 	pp->pr_wchan = wchan;
330 	pp->pr_alloc = palloc;
331 	pp->pr_nitems = 0;
332 	pp->pr_nout = 0;
333 	pp->pr_hardlimit = UINT_MAX;
334 	pp->pr_hardlimit_warning = NULL;
335 	pp->pr_hardlimit_ratecap.tv_sec = 0;
336 	pp->pr_hardlimit_ratecap.tv_usec = 0;
337 	pp->pr_hardlimit_warning_last.tv_sec = 0;
338 	pp->pr_hardlimit_warning_last.tv_usec = 0;
339 	pp->pr_serial = ++pool_serial;
340 	if (pool_serial == 0)
341 		panic("pool_init: too much uptime");
342 
343         /* constructor, destructor, and arg */
344 	pp->pr_ctor = NULL;
345 	pp->pr_dtor = NULL;
346 	pp->pr_arg = NULL;
347 
348 	/*
349 	 * Decide whether to put the page header off page to avoid
350 	 * wasting too large a part of the page. Off-page page headers
351 	 * go into an RB tree, so we can match a returned item with
352 	 * its header based on the page address.
353 	 * We use 1/16 of the page size as the threshold (XXX: tune)
354 	 */
355 	if (pp->pr_size < palloc->pa_pagesz/16 && pp->pr_size < PAGE_SIZE) {
356 		/* Use the end of the page for the page header */
357 		pp->pr_roflags |= PR_PHINPAGE;
358 		pp->pr_phoffset = off = palloc->pa_pagesz -
359 		    ALIGN(sizeof(struct pool_item_header));
360 	} else {
361 		/* The page header will be taken from our page header pool */
362 		pp->pr_phoffset = 0;
363 		off = palloc->pa_pagesz;
364 		RB_INIT(&pp->pr_phtree);
365 	}
366 
367 	/*
368 	 * Alignment is to take place at `ioff' within the item. This means
369 	 * we must reserve up to `align - 1' bytes on the page to allow
370 	 * appropriate positioning of each item.
371 	 *
372 	 * Silently enforce `0 <= ioff < align'.
373 	 */
374 	pp->pr_itemoffset = ioff = ioff % align;
375 	pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
376 	KASSERT(pp->pr_itemsperpage != 0);
377 
378 	/*
379 	 * Use the slack between the chunks and the page header
380 	 * for "cache coloring".
381 	 */
382 	slack = off - pp->pr_itemsperpage * pp->pr_size;
383 	pp->pr_maxcolor = (slack / align) * align;
384 	pp->pr_curcolor = 0;
385 
386 	pp->pr_nget = 0;
387 	pp->pr_nfail = 0;
388 	pp->pr_nput = 0;
389 	pp->pr_npagealloc = 0;
390 	pp->pr_npagefree = 0;
391 	pp->pr_hiwat = 0;
392 	pp->pr_nidle = 0;
393 
394 	pp->pr_ipl = -1;
395 	mtx_init(&pp->pr_mtx, IPL_NONE);
396 
397 	if (phpool.pr_size == 0) {
398 		pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
399 		    0, "phpool", NULL);
400 		pool_setipl(&phpool, IPL_HIGH);
401 	}
402 
403 	/* pglistalloc/constraint parameters */
404 	pp->pr_crange = &kp_dirty;
405 
406 	/* Insert this into the list of all pools. */
407 	TAILQ_INSERT_HEAD(&pool_head, pp, pr_poollist);
408 }
409 
410 void
411 pool_setipl(struct pool *pp, int ipl)
412 {
413 	pp->pr_ipl = ipl;
414 	mtx_init(&pp->pr_mtx, ipl);
415 }
416 
417 /*
418  * Decommission a pool resource.
419  */
420 void
421 pool_destroy(struct pool *pp)
422 {
423 	struct pool_item_header *ph;
424 
425 #ifdef DIAGNOSTIC
426 	if (pp->pr_nout != 0)
427 		panic("pool_destroy: pool busy: still out: %u", pp->pr_nout);
428 #endif
429 
430 	/* Remove all pages */
431 	while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
432 		pr_rmpage(pp, ph, NULL);
433 	KASSERT(LIST_EMPTY(&pp->pr_fullpages));
434 	KASSERT(LIST_EMPTY(&pp->pr_partpages));
435 
436 	/* Remove from global pool list */
437 	TAILQ_REMOVE(&pool_head, pp, pr_poollist);
438 }
439 
440 struct pool_item_header *
441 pool_alloc_item_header(struct pool *pp, caddr_t storage, int flags)
442 {
443 	struct pool_item_header *ph;
444 
445 	if ((pp->pr_roflags & PR_PHINPAGE) != 0)
446 		ph = (struct pool_item_header *)(storage + pp->pr_phoffset);
447 	else
448 		ph = pool_get(&phpool, (flags & ~(PR_WAITOK | PR_ZERO)) |
449 		    PR_NOWAIT);
450 	if (pool_debug)
451 		ph->ph_magic = PI_MAGIC;
452 	return (ph);
453 }
454 
455 /*
456  * Grab an item from the pool; must be called at appropriate spl level
457  */
458 void *
459 pool_get(struct pool *pp, int flags)
460 {
461 	void *v;
462 
463 	KASSERT(flags & (PR_WAITOK | PR_NOWAIT));
464 
465 #ifdef DIAGNOSTIC
466 	if ((flags & PR_WAITOK) != 0)
467 		assertwaitok();
468 #endif /* DIAGNOSTIC */
469 
470 	mtx_enter(&pp->pr_mtx);
471 #ifdef POOL_DEBUG
472 	if (pp->pr_roflags & PR_DEBUGCHK) {
473 		if (pool_chk(pp))
474 			panic("before pool_get");
475 	}
476 #endif
477 	v = pool_do_get(pp, flags);
478 #ifdef POOL_DEBUG
479 	if (pp->pr_roflags & PR_DEBUGCHK) {
480 		if (pool_chk(pp))
481 			panic("after pool_get");
482 	}
483 #endif
484 	mtx_leave(&pp->pr_mtx);
485 	if (v == NULL)
486 		return (v);
487 
488 	if (pp->pr_ctor) {
489 		if (flags & PR_ZERO)
490 			panic("pool_get: PR_ZERO when ctor set");
491 		if (pp->pr_ctor(pp->pr_arg, v, flags)) {
492 			mtx_enter(&pp->pr_mtx);
493 			pool_do_put(pp, v);
494 			mtx_leave(&pp->pr_mtx);
495 			v = NULL;
496 		}
497 	} else {
498 		if (flags & PR_ZERO)
499 			memset(v, 0, pp->pr_size);
500 	}
501 	if (v != NULL)
502 		pp->pr_nget++;
503 	return (v);
504 }
505 
506 void *
507 pool_do_get(struct pool *pp, int flags)
508 {
509 	struct pool_item *pi;
510 	struct pool_item_header *ph;
511 	void *v;
512 	int slowdown = 0;
513 #if defined(DIAGNOSTIC) && defined(POOL_DEBUG)
514 	int i, *ip;
515 #endif
516 
517 #ifdef MALLOC_DEBUG
518 	if (pp->pr_roflags & PR_DEBUG) {
519 		void *addr;
520 
521 		addr = NULL;
522 		debug_malloc(pp->pr_size, M_DEBUG,
523 		    (flags & PR_WAITOK) ? M_WAITOK : M_NOWAIT, &addr);
524 		return (addr);
525 	}
526 #endif
527 
528 startover:
529 	/*
530 	 * Check to see if we've reached the hard limit.  If we have,
531 	 * and we can wait, then wait until an item has been returned to
532 	 * the pool.
533 	 */
534 #ifdef DIAGNOSTIC
535 	if (__predict_false(pp->pr_nout > pp->pr_hardlimit))
536 		panic("pool_do_get: %s: crossed hard limit", pp->pr_wchan);
537 #endif
538 	if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
539 		if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
540 			/*
541 			 * XXX: A warning isn't logged in this case.  Should
542 			 * it be?
543 			 */
544 			pp->pr_flags |= PR_WANTED;
545 			pool_sleep(pp);
546 			goto startover;
547 		}
548 
549 		/*
550 		 * Log a message that the hard limit has been hit.
551 		 */
552 		if (pp->pr_hardlimit_warning != NULL &&
553 		    ratecheck(&pp->pr_hardlimit_warning_last,
554 		    &pp->pr_hardlimit_ratecap))
555 			log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
556 
557 		pp->pr_nfail++;
558 		return (NULL);
559 	}
560 
561 	/*
562 	 * The convention we use is that if `curpage' is not NULL, then
563 	 * it points at a non-empty bucket. In particular, `curpage'
564 	 * never points at a page header which has PR_PHINPAGE set and
565 	 * has no items in its bucket.
566 	 */
567 	if ((ph = pp->pr_curpage) == NULL) {
568 #ifdef DIAGNOSTIC
569 		if (pp->pr_nitems != 0) {
570 			printf("pool_do_get: %s: curpage NULL, nitems %u\n",
571 			    pp->pr_wchan, pp->pr_nitems);
572 			panic("pool_do_get: nitems inconsistent");
573 		}
574 #endif
575 
576 		/*
577 		 * Call the back-end page allocator for more memory.
578 		 */
579 		v = pool_allocator_alloc(pp, flags, &slowdown);
580 		if (__predict_true(v != NULL))
581 			ph = pool_alloc_item_header(pp, v, flags);
582 
583 		if (__predict_false(v == NULL || ph == NULL)) {
584 			if (v != NULL)
585 				pool_allocator_free(pp, v);
586 
587 			if ((flags & PR_WAITOK) == 0) {
588 				pp->pr_nfail++;
589 				return (NULL);
590 			}
591 
592 			/*
593 			 * Wait for items to be returned to this pool.
594 			 *
595 			 * XXX: maybe we should wake up once a second and
596 			 * try again?
597 			 */
598 			pp->pr_flags |= PR_WANTED;
599 			pool_sleep(pp);
600 			goto startover;
601 		}
602 
603 		/* We have more memory; add it to the pool */
604 		pool_prime_page(pp, v, ph);
605 		pp->pr_npagealloc++;
606 
607 		if (slowdown && (flags & PR_WAITOK)) {
608 			mtx_leave(&pp->pr_mtx);
609 			yield();
610 			mtx_enter(&pp->pr_mtx);
611 		}
612 
613 		/* Start the allocation process over. */
614 		goto startover;
615 	}
616 	if (__predict_false((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)) {
617 		panic("pool_do_get: %s: page empty", pp->pr_wchan);
618 	}
619 #ifdef DIAGNOSTIC
620 	if (__predict_false(pp->pr_nitems == 0)) {
621 		printf("pool_do_get: %s: items on itemlist, nitems %u\n",
622 		    pp->pr_wchan, pp->pr_nitems);
623 		panic("pool_do_get: nitems inconsistent");
624 	}
625 #endif
626 
627 #ifdef DIAGNOSTIC
628 	if (__predict_false(pi->pi_magic != PI_MAGIC))
629 		panic("pool_do_get(%s): free list modified: "
630 		    "page %p; item addr %p; offset 0x%x=0x%x",
631 		    pp->pr_wchan, ph->ph_page, pi, 0, pi->pi_magic);
632 #ifdef POOL_DEBUG
633 	if (pool_debug && ph->ph_magic) {
634 		for (ip = (int *)pi, i = sizeof(*pi) / sizeof(int);
635 		    i < pp->pr_size / sizeof(int); i++) {
636 			if (ip[i] != ph->ph_magic) {
637 				panic("pool_do_get(%s): free list modified: "
638 				    "page %p; item addr %p; offset 0x%x=0x%x",
639 				    pp->pr_wchan, ph->ph_page, pi,
640 				    i * sizeof(int), ip[i]);
641 			}
642 		}
643 	}
644 #endif /* POOL_DEBUG */
645 #endif /* DIAGNOSTIC */
646 
647 	/*
648 	 * Remove from item list.
649 	 */
650 	TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
651 	pp->pr_nitems--;
652 	pp->pr_nout++;
653 	if (ph->ph_nmissing == 0) {
654 #ifdef DIAGNOSTIC
655 		if (__predict_false(pp->pr_nidle == 0))
656 			panic("pool_do_get: nidle inconsistent");
657 #endif
658 		pp->pr_nidle--;
659 
660 		/*
661 		 * This page was previously empty.  Move it to the list of
662 		 * partially-full pages.  This page is already curpage.
663 		 */
664 		LIST_REMOVE(ph, ph_pagelist);
665 		LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
666 	}
667 	ph->ph_nmissing++;
668 	if (TAILQ_EMPTY(&ph->ph_itemlist)) {
669 #ifdef DIAGNOSTIC
670 		if (__predict_false(ph->ph_nmissing != pp->pr_itemsperpage)) {
671 			panic("pool_do_get: %s: nmissing inconsistent",
672 			    pp->pr_wchan);
673 		}
674 #endif
675 		/*
676 		 * This page is now full.  Move it to the full list
677 		 * and select a new current page.
678 		 */
679 		LIST_REMOVE(ph, ph_pagelist);
680 		LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
681 		pool_update_curpage(pp);
682 	}
683 
684 	/*
685 	 * If we have a low water mark and we are now below that low
686 	 * water mark, add more items to the pool.
687 	 */
688 	if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
689 		/*
690 		 * XXX: Should we log a warning?  Should we set up a timeout
691 		 * to try again in a second or so?  The latter could break
692 		 * a caller's assumptions about interrupt protection, etc.
693 		 */
694 	}
695 	return (v);
696 }
697 
698 /*
699  * Return resource to the pool; must be called at appropriate spl level
700  */
701 void
702 pool_put(struct pool *pp, void *v)
703 {
704 	if (pp->pr_dtor)
705 		pp->pr_dtor(pp->pr_arg, v);
706 	mtx_enter(&pp->pr_mtx);
707 #ifdef POOL_DEBUG
708 	if (pp->pr_roflags & PR_DEBUGCHK) {
709 		if (pool_chk(pp))
710 			panic("before pool_put");
711 	}
712 #endif
713 	pool_do_put(pp, v);
714 #ifdef POOL_DEBUG
715 	if (pp->pr_roflags & PR_DEBUGCHK) {
716 		if (pool_chk(pp))
717 			panic("after pool_put");
718 	}
719 #endif
720 	mtx_leave(&pp->pr_mtx);
721 	pp->pr_nput++;
722 }
723 
724 /*
725  * Internal version of pool_put().
726  */
727 void
728 pool_do_put(struct pool *pp, void *v)
729 {
730 	struct pool_item *pi = v;
731 	struct pool_item_header *ph;
732 #if defined(DIAGNOSTIC) && defined(POOL_DEBUG)
733 	int i, *ip;
734 #endif
735 
736 	if (v == NULL)
737 		panic("pool_put of NULL");
738 
739 #ifdef MALLOC_DEBUG
740 	if (pp->pr_roflags & PR_DEBUG) {
741 		debug_free(v, M_DEBUG);
742 		return;
743 	}
744 #endif
745 
746 #ifdef DIAGNOSTIC
747 	if (pp->pr_ipl != -1)
748 		splassert(pp->pr_ipl);
749 
750 	if (__predict_false(pp->pr_nout == 0)) {
751 		printf("pool %s: putting with none out\n",
752 		    pp->pr_wchan);
753 		panic("pool_do_put");
754 	}
755 #endif
756 
757 	if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) {
758 		panic("pool_do_put: %s: page header missing", pp->pr_wchan);
759 	}
760 
761 	/*
762 	 * Return to item list.
763 	 */
764 #ifdef DIAGNOSTIC
765 	pi->pi_magic = PI_MAGIC;
766 #ifdef POOL_DEBUG
767 	if (ph->ph_magic) {
768 		for (ip = (int *)pi, i = sizeof(*pi)/sizeof(int);
769 		    i < pp->pr_size / sizeof(int); i++)
770 			ip[i] = ph->ph_magic;
771 	}
772 #endif /* POOL_DEBUG */
773 #endif /* DIAGNOSTIC */
774 
775 	TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
776 	ph->ph_nmissing--;
777 	pp->pr_nitems++;
778 	pp->pr_nout--;
779 
780 	/* Cancel "pool empty" condition if it exists */
781 	if (pp->pr_curpage == NULL)
782 		pp->pr_curpage = ph;
783 
784 	if (pp->pr_flags & PR_WANTED) {
785 		pp->pr_flags &= ~PR_WANTED;
786 		if (ph->ph_nmissing == 0)
787 			pp->pr_nidle++;
788 		wakeup(pp);
789 		return;
790 	}
791 
792 	/*
793 	 * If this page is now empty, do one of two things:
794 	 *
795 	 *	(1) If we have more pages than the page high water mark,
796 	 *	    free the page back to the system.
797 	 *
798 	 *	(2) Otherwise, move the page to the empty page list.
799 	 *
800 	 * Either way, select a new current page (so we use a partially-full
801 	 * page if one is available).
802 	 */
803 	if (ph->ph_nmissing == 0) {
804 		pp->pr_nidle++;
805 		if (pp->pr_nidle > pp->pr_maxpages) {
806 			pr_rmpage(pp, ph, NULL);
807 		} else {
808 			LIST_REMOVE(ph, ph_pagelist);
809 			LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
810 		}
811 		pool_update_curpage(pp);
812 	}
813 
814 	/*
815 	 * If the page was previously completely full, move it to the
816 	 * partially-full list and make it the current page.  The next
817 	 * allocation will get the item from this page, instead of
818 	 * further fragmenting the pool.
819 	 */
820 	else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
821 		LIST_REMOVE(ph, ph_pagelist);
822 		LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
823 		pp->pr_curpage = ph;
824 	}
825 }
826 
827 /*
828  * Add N items to the pool.
829  */
830 int
831 pool_prime(struct pool *pp, int n)
832 {
833 	struct pool_item_header *ph;
834 	caddr_t cp;
835 	int newpages;
836 	int slowdown;
837 
838 	mtx_enter(&pp->pr_mtx);
839 	newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
840 
841 	while (newpages-- > 0) {
842 		cp = pool_allocator_alloc(pp, PR_NOWAIT, &slowdown);
843 		if (__predict_true(cp != NULL))
844 			ph = pool_alloc_item_header(pp, cp, PR_NOWAIT);
845 		if (__predict_false(cp == NULL || ph == NULL)) {
846 			if (cp != NULL)
847 				pool_allocator_free(pp, cp);
848 			break;
849 		}
850 
851 		pool_prime_page(pp, cp, ph);
852 		pp->pr_npagealloc++;
853 		pp->pr_minpages++;
854 	}
855 
856 	if (pp->pr_minpages >= pp->pr_maxpages)
857 		pp->pr_maxpages = pp->pr_minpages + 1;	/* XXX */
858 
859 	mtx_leave(&pp->pr_mtx);
860 	return (0);
861 }
862 
863 /*
864  * Add a page worth of items to the pool.
865  *
866  * Note, we must be called with the pool descriptor LOCKED.
867  */
868 void
869 pool_prime_page(struct pool *pp, caddr_t storage, struct pool_item_header *ph)
870 {
871 	struct pool_item *pi;
872 	caddr_t cp = storage;
873 	unsigned int align = pp->pr_align;
874 	unsigned int ioff = pp->pr_itemoffset;
875 	int n;
876 #if defined(DIAGNOSTIC) && defined(POOL_DEBUG)
877 	int i, *ip;
878 #endif
879 
880 	/*
881 	 * Insert page header.
882 	 */
883 	LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
884 	TAILQ_INIT(&ph->ph_itemlist);
885 	ph->ph_page = storage;
886 	ph->ph_pagesize = pp->pr_alloc->pa_pagesz;
887 	ph->ph_nmissing = 0;
888 	if ((pp->pr_roflags & PR_PHINPAGE) == 0)
889 		RB_INSERT(phtree, &pp->pr_phtree, ph);
890 
891 	pp->pr_nidle++;
892 
893 	/*
894 	 * Color this page.
895 	 */
896 	cp = (caddr_t)(cp + pp->pr_curcolor);
897 	if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
898 		pp->pr_curcolor = 0;
899 
900 	/*
901 	 * Adjust storage to apply alignment to `pr_itemoffset' in each item.
902 	 */
903 	if (ioff != 0)
904 		cp = (caddr_t)(cp + (align - ioff));
905 	ph->ph_colored = cp;
906 
907 	/*
908 	 * Insert remaining chunks on the bucket list.
909 	 */
910 	n = pp->pr_itemsperpage;
911 	pp->pr_nitems += n;
912 
913 	while (n--) {
914 		pi = (struct pool_item *)cp;
915 
916 		KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
917 
918 		/* Insert on page list */
919 		TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
920 
921 #ifdef DIAGNOSTIC
922 		pi->pi_magic = PI_MAGIC;
923 #ifdef POOL_DEBUG
924 		if (ph->ph_magic) {
925 			for (ip = (int *)pi, i = sizeof(*pi)/sizeof(int);
926 			    i < pp->pr_size / sizeof(int); i++)
927 				ip[i] = ph->ph_magic;
928 		}
929 #endif /* POOL_DEBUG */
930 #endif /* DIAGNOSTIC */
931 		cp = (caddr_t)(cp + pp->pr_size);
932 	}
933 
934 	/*
935 	 * If the pool was depleted, point at the new page.
936 	 */
937 	if (pp->pr_curpage == NULL)
938 		pp->pr_curpage = ph;
939 
940 	if (++pp->pr_npages > pp->pr_hiwat)
941 		pp->pr_hiwat = pp->pr_npages;
942 }
943 
944 /*
945  * Used by pool_get() when nitems drops below the low water mark.  This
946  * is used to catch up pr_nitems with the low water mark.
947  *
948  * Note we never wait for memory here, we let the caller decide what to do.
949  */
950 int
951 pool_catchup(struct pool *pp)
952 {
953 	struct pool_item_header *ph;
954 	caddr_t cp;
955 	int error = 0;
956 	int slowdown;
957 
958 	while (POOL_NEEDS_CATCHUP(pp)) {
959 		/*
960 		 * Call the page back-end allocator for more memory.
961 		 */
962 		cp = pool_allocator_alloc(pp, PR_NOWAIT, &slowdown);
963 		if (__predict_true(cp != NULL))
964 			ph = pool_alloc_item_header(pp, cp, PR_NOWAIT);
965 		if (__predict_false(cp == NULL || ph == NULL)) {
966 			if (cp != NULL)
967 				pool_allocator_free(pp, cp);
968 			error = ENOMEM;
969 			break;
970 		}
971 		pool_prime_page(pp, cp, ph);
972 		pp->pr_npagealloc++;
973 	}
974 
975 	return (error);
976 }
977 
978 void
979 pool_update_curpage(struct pool *pp)
980 {
981 
982 	pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
983 	if (pp->pr_curpage == NULL) {
984 		pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
985 	}
986 }
987 
988 void
989 pool_setlowat(struct pool *pp, int n)
990 {
991 
992 	pp->pr_minitems = n;
993 	pp->pr_minpages = (n == 0)
994 		? 0
995 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
996 
997 	mtx_enter(&pp->pr_mtx);
998 	/* Make sure we're caught up with the newly-set low water mark. */
999 	if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1000 		/*
1001 		 * XXX: Should we log a warning?  Should we set up a timeout
1002 		 * to try again in a second or so?  The latter could break
1003 		 * a caller's assumptions about interrupt protection, etc.
1004 		 */
1005 	}
1006 	mtx_leave(&pp->pr_mtx);
1007 }
1008 
1009 void
1010 pool_sethiwat(struct pool *pp, int n)
1011 {
1012 
1013 	pp->pr_maxpages = (n == 0)
1014 		? 0
1015 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1016 }
1017 
1018 int
1019 pool_sethardlimit(struct pool *pp, u_int n, const char *warnmsg, int ratecap)
1020 {
1021 	int error = 0;
1022 
1023 	if (n < pp->pr_nout) {
1024 		error = EINVAL;
1025 		goto done;
1026 	}
1027 
1028 	pp->pr_hardlimit = n;
1029 	pp->pr_hardlimit_warning = warnmsg;
1030 	pp->pr_hardlimit_ratecap.tv_sec = ratecap;
1031 	pp->pr_hardlimit_warning_last.tv_sec = 0;
1032 	pp->pr_hardlimit_warning_last.tv_usec = 0;
1033 
1034 done:
1035 	return (error);
1036 }
1037 
1038 void
1039 pool_set_constraints(struct pool *pp, const struct kmem_pa_mode *mode)
1040 {
1041 	pp->pr_crange = mode;
1042 }
1043 
1044 void
1045 pool_set_ctordtor(struct pool *pp, int (*ctor)(void *, void *, int),
1046     void (*dtor)(void *, void *), void *arg)
1047 {
1048 	pp->pr_ctor = ctor;
1049 	pp->pr_dtor = dtor;
1050 	pp->pr_arg = arg;
1051 }
1052 /*
1053  * Release all complete pages that have not been used recently.
1054  *
1055  * Returns non-zero if any pages have been reclaimed.
1056  */
1057 int
1058 pool_reclaim(struct pool *pp)
1059 {
1060 	struct pool_item_header *ph, *phnext;
1061 	struct pool_pagelist pq;
1062 
1063 	LIST_INIT(&pq);
1064 
1065 	mtx_enter(&pp->pr_mtx);
1066 	for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
1067 		phnext = LIST_NEXT(ph, ph_pagelist);
1068 
1069 		/* Check our minimum page claim */
1070 		if (pp->pr_npages <= pp->pr_minpages)
1071 			break;
1072 
1073 		KASSERT(ph->ph_nmissing == 0);
1074 
1075 		/*
1076 		 * If freeing this page would put us below
1077 		 * the low water mark, stop now.
1078 		 */
1079 		if ((pp->pr_nitems - pp->pr_itemsperpage) <
1080 		    pp->pr_minitems)
1081 			break;
1082 
1083 		pr_rmpage(pp, ph, &pq);
1084 	}
1085 	mtx_leave(&pp->pr_mtx);
1086 
1087 	if (LIST_EMPTY(&pq))
1088 		return (0);
1089 	while ((ph = LIST_FIRST(&pq)) != NULL) {
1090 		LIST_REMOVE(ph, ph_pagelist);
1091 		pool_allocator_free(pp, ph->ph_page);
1092 		if (pp->pr_roflags & PR_PHINPAGE)
1093 			continue;
1094 		pool_put(&phpool, ph);
1095 	}
1096 
1097 	return (1);
1098 }
1099 
1100 /*
1101  * Release all complete pages that have not been used recently
1102  * from all pools.
1103  */
1104 void
1105 pool_reclaim_all(void)
1106 {
1107 	struct pool	*pp;
1108 	TAILQ_FOREACH(pp, &pool_head, pr_poollist)
1109 		pool_reclaim(pp);
1110 }
1111 
1112 #ifdef DDB
1113 #include <machine/db_machdep.h>
1114 #include <ddb/db_interface.h>
1115 #include <ddb/db_output.h>
1116 
1117 /*
1118  * Diagnostic helpers.
1119  */
1120 void
1121 pool_printit(struct pool *pp, const char *modif, int (*pr)(const char *, ...))
1122 {
1123 	pool_print1(pp, modif, pr);
1124 }
1125 
1126 void
1127 pool_print_pagelist(struct pool_pagelist *pl, int (*pr)(const char *, ...))
1128 {
1129 	struct pool_item_header *ph;
1130 #ifdef DIAGNOSTIC
1131 	struct pool_item *pi;
1132 #endif
1133 
1134 	LIST_FOREACH(ph, pl, ph_pagelist) {
1135 		(*pr)("\t\tpage %p, nmissing %d\n",
1136 		    ph->ph_page, ph->ph_nmissing);
1137 #ifdef DIAGNOSTIC
1138 		TAILQ_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1139 			if (pi->pi_magic != PI_MAGIC) {
1140 				(*pr)("\t\t\titem %p, magic 0x%x\n",
1141 				    pi, pi->pi_magic);
1142 			}
1143 		}
1144 #endif
1145 	}
1146 }
1147 
1148 void
1149 pool_print1(struct pool *pp, const char *modif, int (*pr)(const char *, ...))
1150 {
1151 	struct pool_item_header *ph;
1152 	int print_pagelist = 0;
1153 	char c;
1154 
1155 	while ((c = *modif++) != '\0') {
1156 		if (c == 'p')
1157 			print_pagelist = 1;
1158 		modif++;
1159 	}
1160 
1161 	(*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1162 	    pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1163 	    pp->pr_roflags);
1164 	(*pr)("\talloc %p\n", pp->pr_alloc);
1165 	(*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1166 	    pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1167 	(*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1168 	    pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1169 
1170 	(*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
1171 	    pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1172 	(*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1173 	    pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1174 
1175 	if (print_pagelist == 0)
1176 		return;
1177 
1178 	if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1179 		(*pr)("\n\tempty page list:\n");
1180 	pool_print_pagelist(&pp->pr_emptypages, pr);
1181 	if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
1182 		(*pr)("\n\tfull page list:\n");
1183 	pool_print_pagelist(&pp->pr_fullpages, pr);
1184 	if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
1185 		(*pr)("\n\tpartial-page list:\n");
1186 	pool_print_pagelist(&pp->pr_partpages, pr);
1187 
1188 	if (pp->pr_curpage == NULL)
1189 		(*pr)("\tno current page\n");
1190 	else
1191 		(*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1192 }
1193 
1194 void
1195 db_show_all_pools(db_expr_t expr, int haddr, db_expr_t count, char *modif)
1196 {
1197 	struct pool *pp;
1198 	char maxp[16];
1199 	int ovflw;
1200 	char mode;
1201 
1202 	mode = modif[0];
1203 	if (mode != '\0' && mode != 'a') {
1204 		db_printf("usage: show all pools [/a]\n");
1205 		return;
1206 	}
1207 
1208 	if (mode == '\0')
1209 		db_printf("%-10s%4s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n",
1210 		    "Name",
1211 		    "Size",
1212 		    "Requests",
1213 		    "Fail",
1214 		    "Releases",
1215 		    "Pgreq",
1216 		    "Pgrel",
1217 		    "Npage",
1218 		    "Hiwat",
1219 		    "Minpg",
1220 		    "Maxpg",
1221 		    "Idle");
1222 	else
1223 		db_printf("%-10s %18s %18s\n",
1224 		    "Name", "Address", "Allocator");
1225 
1226 	TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
1227 		if (mode == 'a') {
1228 			db_printf("%-10s %18p %18p\n", pp->pr_wchan, pp,
1229 			    pp->pr_alloc);
1230 			continue;
1231 		}
1232 
1233 		if (!pp->pr_nget)
1234 			continue;
1235 
1236 		if (pp->pr_maxpages == UINT_MAX)
1237 			snprintf(maxp, sizeof maxp, "inf");
1238 		else
1239 			snprintf(maxp, sizeof maxp, "%u", pp->pr_maxpages);
1240 
1241 #define PRWORD(ovflw, fmt, width, fixed, val) do {	\
1242 	(ovflw) += db_printf((fmt),			\
1243 	    (width) - (fixed) - (ovflw) > 0 ?		\
1244 	    (width) - (fixed) - (ovflw) : 0,		\
1245 	    (val)) - (width);				\
1246 	if ((ovflw) < 0)				\
1247 		(ovflw) = 0;				\
1248 } while (/* CONSTCOND */0)
1249 
1250 		ovflw = 0;
1251 		PRWORD(ovflw, "%-*s", 10, 0, pp->pr_wchan);
1252 		PRWORD(ovflw, " %*u", 4, 1, pp->pr_size);
1253 		PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget);
1254 		PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
1255 		PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput);
1256 		PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc);
1257 		PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree);
1258 		PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages);
1259 		PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat);
1260 		PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages);
1261 		PRWORD(ovflw, " %*s", 6, 1, maxp);
1262 		PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle);
1263 
1264 		pool_chk(pp);
1265 	}
1266 }
1267 
1268 int
1269 pool_chk_page(struct pool *pp, struct pool_item_header *ph, int expected)
1270 {
1271 	struct pool_item *pi;
1272 	caddr_t page;
1273 	int n;
1274 #if defined(DIAGNOSTIC) && defined(POOL_DEBUG)
1275 	int i, *ip;
1276 #endif
1277 	const char *label = pp->pr_wchan;
1278 
1279 	page = (caddr_t)((u_long)ph & pp->pr_alloc->pa_pagemask);
1280 	if (page != ph->ph_page &&
1281 	    (pp->pr_roflags & PR_PHINPAGE) != 0) {
1282 		printf("%s: ", label);
1283 		printf("pool(%p:%s): page inconsistency: page %p; "
1284 		    "at page head addr %p (p %p)\n",
1285 		    pp, pp->pr_wchan, ph->ph_page, ph, page);
1286 		return 1;
1287 	}
1288 
1289 	for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
1290 	     pi != NULL;
1291 	     pi = TAILQ_NEXT(pi,pi_list), n++) {
1292 
1293 #ifdef DIAGNOSTIC
1294 		if (pi->pi_magic != PI_MAGIC) {
1295 			printf("%s: ", label);
1296 			printf("pool(%s): free list modified: "
1297 			    "page %p; item ordinal %d; addr %p "
1298 			    "(p %p); offset 0x%x=0x%x\n",
1299 			    pp->pr_wchan, ph->ph_page, n, pi, page,
1300 			    0, pi->pi_magic);
1301 		}
1302 #ifdef POOL_DEBUG
1303 		if (pool_debug && ph->ph_magic) {
1304 			for (ip = (int *)pi, i = sizeof(*pi) / sizeof(int);
1305 			    i < pp->pr_size / sizeof(int); i++) {
1306 				if (ip[i] != ph->ph_magic) {
1307 					printf("pool(%s): free list modified: "
1308 					    "page %p; item ordinal %d; addr %p "
1309 					    "(p %p); offset 0x%x=0x%x\n",
1310 					    pp->pr_wchan, ph->ph_page, n, pi,
1311 					    page, i * sizeof(int), ip[i]);
1312 				}
1313 			}
1314 		}
1315 
1316 #endif /* POOL_DEBUG */
1317 #endif /* DIAGNOSTIC */
1318 		page =
1319 		    (caddr_t)((u_long)pi & pp->pr_alloc->pa_pagemask);
1320 		if (page == ph->ph_page)
1321 			continue;
1322 
1323 		printf("%s: ", label);
1324 		printf("pool(%p:%s): page inconsistency: page %p;"
1325 		    " item ordinal %d; addr %p (p %p)\n", pp,
1326 		    pp->pr_wchan, ph->ph_page, n, pi, page);
1327 		return 1;
1328 	}
1329 	if (n + ph->ph_nmissing != pp->pr_itemsperpage) {
1330 		printf("pool(%p:%s): page inconsistency: page %p;"
1331 		    " %d on list, %d missing, %d items per page\n", pp,
1332 		    pp->pr_wchan, ph->ph_page, n, ph->ph_nmissing,
1333 		    pp->pr_itemsperpage);
1334 		return 1;
1335 	}
1336 	if (expected >= 0 && n != expected) {
1337 		printf("pool(%p:%s): page inconsistency: page %p;"
1338 		    " %d on list, %d missing, %d expected\n", pp,
1339 		    pp->pr_wchan, ph->ph_page, n, ph->ph_nmissing,
1340 		    expected);
1341 		return 1;
1342 	}
1343 	return 0;
1344 }
1345 
1346 int
1347 pool_chk(struct pool *pp)
1348 {
1349 	struct pool_item_header *ph;
1350 	int r = 0;
1351 
1352 	LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist)
1353 		r += pool_chk_page(pp, ph, pp->pr_itemsperpage);
1354 	LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist)
1355 		r += pool_chk_page(pp, ph, 0);
1356 	LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist)
1357 		r += pool_chk_page(pp, ph, -1);
1358 
1359 	return (r);
1360 }
1361 
1362 void
1363 pool_walk(struct pool *pp, int full, int (*pr)(const char *, ...),
1364     void (*func)(void *, int, int (*)(const char *, ...)))
1365 {
1366 	struct pool_item_header *ph;
1367 	struct pool_item *pi;
1368 	caddr_t cp;
1369 	int n;
1370 
1371 	LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
1372 		cp = ph->ph_colored;
1373 		n = ph->ph_nmissing;
1374 
1375 		while (n--) {
1376 			func(cp, full, pr);
1377 			cp += pp->pr_size;
1378 		}
1379 	}
1380 
1381 	LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
1382 		cp = ph->ph_colored;
1383 		n = ph->ph_nmissing;
1384 
1385 		do {
1386 			TAILQ_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1387 				if (cp == (caddr_t)pi)
1388 					break;
1389 			}
1390 			if (cp != (caddr_t)pi) {
1391 				func(cp, full, pr);
1392 				n--;
1393 			}
1394 
1395 			cp += pp->pr_size;
1396 		} while (n > 0);
1397 	}
1398 }
1399 #endif
1400 
1401 /*
1402  * We have three different sysctls.
1403  * kern.pool.npools - the number of pools.
1404  * kern.pool.pool.<pool#> - the pool struct for the pool#.
1405  * kern.pool.name.<pool#> - the name for pool#.
1406  */
1407 int
1408 sysctl_dopool(int *name, u_int namelen, char *where, size_t *sizep)
1409 {
1410 	struct pool *pp, *foundpool = NULL;
1411 	size_t buflen = where != NULL ? *sizep : 0;
1412 	int npools = 0, s;
1413 	unsigned int lookfor;
1414 	size_t len;
1415 
1416 	switch (*name) {
1417 	case KERN_POOL_NPOOLS:
1418 		if (namelen != 1 || buflen != sizeof(int))
1419 			return (EINVAL);
1420 		lookfor = 0;
1421 		break;
1422 	case KERN_POOL_NAME:
1423 		if (namelen != 2 || buflen < 1)
1424 			return (EINVAL);
1425 		lookfor = name[1];
1426 		break;
1427 	case KERN_POOL_POOL:
1428 		if (namelen != 2 || buflen != sizeof(struct pool))
1429 			return (EINVAL);
1430 		lookfor = name[1];
1431 		break;
1432 	default:
1433 		return (EINVAL);
1434 	}
1435 
1436 	s = splvm();
1437 
1438 	TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
1439 		npools++;
1440 		if (lookfor == pp->pr_serial) {
1441 			foundpool = pp;
1442 			break;
1443 		}
1444 	}
1445 
1446 	splx(s);
1447 
1448 	if (*name != KERN_POOL_NPOOLS && foundpool == NULL)
1449 		return (ENOENT);
1450 
1451 	switch (*name) {
1452 	case KERN_POOL_NPOOLS:
1453 		return copyout(&npools, where, buflen);
1454 	case KERN_POOL_NAME:
1455 		len = strlen(foundpool->pr_wchan) + 1;
1456 		if (*sizep < len)
1457 			return (ENOMEM);
1458 		*sizep = len;
1459 		return copyout(foundpool->pr_wchan, where, len);
1460 	case KERN_POOL_POOL:
1461 		return copyout(foundpool, where, buflen);
1462 	}
1463 	/* NOTREACHED */
1464 	return (0); /* XXX - Stupid gcc */
1465 }
1466 
1467 /*
1468  * Pool backend allocators.
1469  *
1470  * Each pool has a backend allocator that handles allocation, deallocation
1471  */
1472 void	*pool_page_alloc(struct pool *, int, int *);
1473 void	pool_page_free(struct pool *, void *);
1474 
1475 /*
1476  * safe for interrupts, name preserved for compat this is the default
1477  * allocator
1478  */
1479 struct pool_allocator pool_allocator_nointr = {
1480 	pool_page_alloc, pool_page_free, 0,
1481 };
1482 
1483 /*
1484  * XXX - we have at least three different resources for the same allocation
1485  *  and each resource can be depleted. First we have the ready elements in
1486  *  the pool. Then we have the resource (typically a vm_map) for this
1487  *  allocator, then we have physical memory. Waiting for any of these can
1488  *  be unnecessary when any other is freed, but the kernel doesn't support
1489  *  sleeping on multiple addresses, so we have to fake. The caller sleeps on
1490  *  the pool (so that we can be awakened when an item is returned to the pool),
1491  *  but we set PA_WANT on the allocator. When a page is returned to
1492  *  the allocator and PA_WANT is set pool_allocator_free will wakeup all
1493  *  sleeping pools belonging to this allocator. (XXX - thundering herd).
1494  *  We also wake up the allocator in case someone without a pool (malloc)
1495  *  is sleeping waiting for this allocator.
1496  */
1497 
1498 void *
1499 pool_allocator_alloc(struct pool *pp, int flags, int *slowdown)
1500 {
1501 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1502 	void *v;
1503 
1504 	if (waitok)
1505 		mtx_leave(&pp->pr_mtx);
1506 	v = pp->pr_alloc->pa_alloc(pp, flags, slowdown);
1507 	if (waitok)
1508 		mtx_enter(&pp->pr_mtx);
1509 
1510 	return (v);
1511 }
1512 
1513 void
1514 pool_allocator_free(struct pool *pp, void *v)
1515 {
1516 	struct pool_allocator *pa = pp->pr_alloc;
1517 
1518 	(*pa->pa_free)(pp, v);
1519 }
1520 
1521 void *
1522 pool_page_alloc(struct pool *pp, int flags, int *slowdown)
1523 {
1524 	struct kmem_dyn_mode kd = KMEM_DYN_INITIALIZER;
1525 
1526 	kd.kd_waitok = (flags & PR_WAITOK);
1527 	kd.kd_slowdown = slowdown;
1528 
1529 	return (km_alloc(PAGE_SIZE, &kv_page, pp->pr_crange, &kd));
1530 }
1531 
1532 void
1533 pool_page_free(struct pool *pp, void *v)
1534 {
1535 	km_free(v, PAGE_SIZE, &kv_page, pp->pr_crange);
1536 }
1537 
1538 void *
1539 pool_large_alloc(struct pool *pp, int flags, int *slowdown)
1540 {
1541 	struct kmem_dyn_mode kd = KMEM_DYN_INITIALIZER;
1542 	void *v;
1543 	int s;
1544 
1545 	kd.kd_waitok = (flags & PR_WAITOK);
1546 	kd.kd_slowdown = slowdown;
1547 
1548 	s = splvm();
1549 	v = km_alloc(pp->pr_alloc->pa_pagesz, &kv_intrsafe, pp->pr_crange,
1550 	    &kd);
1551 	splx(s);
1552 
1553 	return (v);
1554 }
1555 
1556 void
1557 pool_large_free(struct pool *pp, void *v)
1558 {
1559 	int s;
1560 
1561 	s = splvm();
1562 	km_free(v, pp->pr_alloc->pa_pagesz, &kv_intrsafe, pp->pr_crange);
1563 	splx(s);
1564 }
1565 
1566 void *
1567 pool_large_alloc_ni(struct pool *pp, int flags, int *slowdown)
1568 {
1569 	struct kmem_dyn_mode kd = KMEM_DYN_INITIALIZER;
1570 
1571 	kd.kd_waitok = (flags & PR_WAITOK);
1572 	kd.kd_slowdown = slowdown;
1573 
1574 	return (km_alloc(pp->pr_alloc->pa_pagesz, &kv_any, pp->pr_crange, &kd));
1575 }
1576 
1577 void
1578 pool_large_free_ni(struct pool *pp, void *v)
1579 {
1580 	km_free(v, pp->pr_alloc->pa_pagesz, &kv_any, pp->pr_crange);
1581 }
1582