xref: /openbsd-src/sys/kern/subr_pool.c (revision 94fd4554194a14f126fba33b837cc68a1df42468)
1 /*	$OpenBSD: subr_pool.c,v 1.51 2007/04/23 09:27:59 art 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  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the NetBSD
23  *	Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/errno.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/lock.h>
48 #include <sys/pool.h>
49 #include <sys/syslog.h>
50 #include <sys/sysctl.h>
51 
52 #include <uvm/uvm.h>
53 
54 /*
55  * XXX - for now.
56  */
57 #ifdef LOCKDEBUG
58 #define simple_lock_freecheck(a, s) do { /* nothing */ } while (0)
59 #define simple_lock_only_held(lkp, str) do { /* nothing */ } while (0)
60 #endif
61 
62 /*
63  * Pool resource management utility.
64  *
65  * Memory is allocated in pages which are split into pieces according to
66  * the pool item size. Each page is kept on one of three lists in the
67  * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages',
68  * for empty, full and partially-full pages respectively. The individual
69  * pool items are on a linked list headed by `ph_itemlist' in each page
70  * header. The memory for building the page list is either taken from
71  * the allocated pages themselves (for small pool items) or taken from
72  * an internal pool of page headers (`phpool').
73  */
74 
75 /* List of all pools */
76 TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
77 
78 /* Private pool for page header structures */
79 static struct pool phpool;
80 
81 /* # of seconds to retain page after last use */
82 int pool_inactive_time = 10;
83 
84 /* This spin lock protects both pool_head */
85 struct simplelock pool_head_slock;
86 
87 struct pool_item_header {
88 	/* Page headers */
89 	LIST_ENTRY(pool_item_header)
90 				ph_pagelist;	/* pool page list */
91 	TAILQ_HEAD(,pool_item)	ph_itemlist;	/* chunk list for this page */
92 	SPLAY_ENTRY(pool_item_header)
93 				ph_node;	/* Off-page page headers */
94 	int			ph_nmissing;	/* # of chunks in use */
95 	caddr_t			ph_page;	/* this page's address */
96 	struct timeval		ph_time;	/* last referenced */
97 };
98 
99 struct pool_item {
100 #ifdef DIAGNOSTIC
101 	int pi_magic;
102 #endif
103 #ifdef DEADBEEF1
104 #define	PI_MAGIC DEADBEEF1
105 #else
106 #define	PI_MAGIC 0xdeafbeef
107 #endif
108 	/* Other entries use only this list entry */
109 	TAILQ_ENTRY(pool_item)	pi_list;
110 };
111 
112 #define	POOL_NEEDS_CATCHUP(pp)						\
113 	((pp)->pr_nitems < (pp)->pr_minitems)
114 
115 /*
116  * Every pool gets a unique serial number assigned to it. If this counter
117  * wraps, we're screwed, but we shouldn't create so many pools anyway.
118  */
119 unsigned int pool_serial;
120 
121 /*
122  * Pool cache management.
123  *
124  * Pool caches provide a way for constructed objects to be cached by the
125  * pool subsystem.  This can lead to performance improvements by avoiding
126  * needless object construction/destruction; it is deferred until absolutely
127  * necessary.
128  *
129  * Caches are grouped into cache groups.  Each cache group references
130  * up to 16 constructed objects.  When a cache allocates an object
131  * from the pool, it calls the object's constructor and places it into
132  * a cache group.  When a cache group frees an object back to the pool,
133  * it first calls the object's destructor.  This allows the object to
134  * persist in constructed form while freed to the cache.
135  *
136  * Multiple caches may exist for each pool.  This allows a single
137  * object type to have multiple constructed forms.  The pool references
138  * each cache, so that when a pool is drained by the pagedaemon, it can
139  * drain each individual cache as well.  Each time a cache is drained,
140  * the most idle cache group is freed to the pool in its entirety.
141  *
142  * Pool caches are layed on top of pools.  By layering them, we can avoid
143  * the complexity of cache management for pools which would not benefit
144  * from it.
145  */
146 
147 /* The cache group pool. */
148 static struct pool pcgpool;
149 
150 /* The pool cache group. */
151 #define	PCG_NOBJECTS		16
152 struct pool_cache_group {
153 	TAILQ_ENTRY(pool_cache_group)
154 		pcg_list;	/* link in the pool cache's group list */
155 	u_int	pcg_avail;	/* # available objects */
156 				/* pointers to the objects */
157 	void	*pcg_objects[PCG_NOBJECTS];
158 };
159 
160 void	pool_cache_reclaim(struct pool_cache *);
161 void	pool_cache_do_invalidate(struct pool_cache *, int,
162     void (*)(struct pool *, void *));
163 
164 int	pool_catchup(struct pool *);
165 void	pool_prime_page(struct pool *, caddr_t, struct pool_item_header *);
166 void	pool_update_curpage(struct pool *);
167 void	pool_do_put(struct pool *, void *);
168 void	pr_rmpage(struct pool *, struct pool_item_header *,
169     struct pool_pagelist *);
170 int	pool_chk_page(struct pool *, const char *, struct pool_item_header *);
171 
172 void	*pool_allocator_alloc(struct pool *, int);
173 void	pool_allocator_free(struct pool *, void *);
174 
175 #ifdef DDB
176 void pool_print_pagelist(struct pool_pagelist *, int (*)(const char *, ...));
177 void pool_print1(struct pool *, const char *, int (*)(const char *, ...));
178 #endif
179 
180 
181 /*
182  * Pool log entry. An array of these is allocated in pool_init().
183  */
184 struct pool_log {
185 	const char	*pl_file;
186 	long		pl_line;
187 	int		pl_action;
188 #define	PRLOG_GET	1
189 #define	PRLOG_PUT	2
190 	void		*pl_addr;
191 };
192 
193 /* Number of entries in pool log buffers */
194 #ifndef POOL_LOGSIZE
195 #define	POOL_LOGSIZE	10
196 #endif
197 
198 int pool_logsize = POOL_LOGSIZE;
199 
200 #ifdef POOL_DIAGNOSTIC
201 static __inline void
202 pr_log(struct pool *pp, void *v, int action, const char *file, long line)
203 {
204 	int n = pp->pr_curlogentry;
205 	struct pool_log *pl;
206 
207 	if ((pp->pr_roflags & PR_LOGGING) == 0)
208 		return;
209 
210 	/*
211 	 * Fill in the current entry. Wrap around and overwrite
212 	 * the oldest entry if necessary.
213 	 */
214 	pl = &pp->pr_log[n];
215 	pl->pl_file = file;
216 	pl->pl_line = line;
217 	pl->pl_action = action;
218 	pl->pl_addr = v;
219 	if (++n >= pp->pr_logsize)
220 		n = 0;
221 	pp->pr_curlogentry = n;
222 }
223 
224 static void
225 pr_printlog(struct pool *pp, struct pool_item *pi,
226     int (*pr)(const char *, ...))
227 {
228 	int i = pp->pr_logsize;
229 	int n = pp->pr_curlogentry;
230 
231 	if ((pp->pr_roflags & PR_LOGGING) == 0)
232 		return;
233 
234 	/*
235 	 * Print all entries in this pool's log.
236 	 */
237 	while (i-- > 0) {
238 		struct pool_log *pl = &pp->pr_log[n];
239 		if (pl->pl_action != 0) {
240 			if (pi == NULL || pi == pl->pl_addr) {
241 				(*pr)("\tlog entry %d:\n", i);
242 				(*pr)("\t\taction = %s, addr = %p\n",
243 				    pl->pl_action == PRLOG_GET ? "get" : "put",
244 				    pl->pl_addr);
245 				(*pr)("\t\tfile: %s at line %lu\n",
246 				    pl->pl_file, pl->pl_line);
247 			}
248 		}
249 		if (++n >= pp->pr_logsize)
250 			n = 0;
251 	}
252 }
253 
254 static __inline void
255 pr_enter(struct pool *pp, const char *file, long line)
256 {
257 
258 	if (__predict_false(pp->pr_entered_file != NULL)) {
259 		printf("pool %s: reentrancy at file %s line %ld\n",
260 		    pp->pr_wchan, file, line);
261 		printf("         previous entry at file %s line %ld\n",
262 		    pp->pr_entered_file, pp->pr_entered_line);
263 		panic("pr_enter");
264 	}
265 
266 	pp->pr_entered_file = file;
267 	pp->pr_entered_line = line;
268 }
269 
270 static __inline void
271 pr_leave(struct pool *pp)
272 {
273 
274 	if (__predict_false(pp->pr_entered_file == NULL)) {
275 		printf("pool %s not entered?\n", pp->pr_wchan);
276 		panic("pr_leave");
277 	}
278 
279 	pp->pr_entered_file = NULL;
280 	pp->pr_entered_line = 0;
281 }
282 
283 static __inline void
284 pr_enter_check(struct pool *pp, int (*pr)(const char *, ...))
285 {
286 
287 	if (pp->pr_entered_file != NULL)
288 		(*pr)("\n\tcurrently entered from file %s line %ld\n",
289 		    pp->pr_entered_file, pp->pr_entered_line);
290 }
291 #else
292 #define	pr_log(pp, v, action, file, line)
293 #define	pr_printlog(pp, pi, pr)
294 #define	pr_enter(pp, file, line)
295 #define	pr_leave(pp)
296 #define	pr_enter_check(pp, pr)
297 #endif /* POOL_DIAGNOSTIC */
298 
299 static __inline int
300 phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
301 {
302 	if (a->ph_page < b->ph_page)
303 		return (-1);
304 	else if (a->ph_page > b->ph_page)
305 		return (1);
306 	else
307 		return (0);
308 }
309 
310 SPLAY_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare);
311 SPLAY_GENERATE(phtree, pool_item_header, ph_node, phtree_compare);
312 
313 /*
314  * Return the pool page header based on page address.
315  */
316 static __inline struct pool_item_header *
317 pr_find_pagehead(struct pool *pp, caddr_t page)
318 {
319 	struct pool_item_header *ph, tmp;
320 
321 	if ((pp->pr_roflags & PR_PHINPAGE) != 0)
322 		return ((struct pool_item_header *)(page + pp->pr_phoffset));
323 
324 	tmp.ph_page = page;
325 	ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
326 	return ph;
327 }
328 
329 /*
330  * Remove a page from the pool.
331  */
332 void
333 pr_rmpage(struct pool *pp, struct pool_item_header *ph,
334      struct pool_pagelist *pq)
335 {
336 	int s;
337 
338 	/*
339 	 * If the page was idle, decrement the idle page count.
340 	 */
341 	if (ph->ph_nmissing == 0) {
342 #ifdef DIAGNOSTIC
343 		if (pp->pr_nidle == 0)
344 			panic("pr_rmpage: nidle inconsistent");
345 		if (pp->pr_nitems < pp->pr_itemsperpage)
346 			panic("pr_rmpage: nitems inconsistent");
347 #endif
348 		pp->pr_nidle--;
349 	}
350 
351 	pp->pr_nitems -= pp->pr_itemsperpage;
352 
353 	/*
354 	 * Unlink a page from the pool and release it (or queue it for release).
355 	 */
356 	LIST_REMOVE(ph, ph_pagelist);
357 	if (pq) {
358 		LIST_INSERT_HEAD(pq, ph, ph_pagelist);
359 	} else {
360 		pool_allocator_free(pp, ph->ph_page);
361 		if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
362 			SPLAY_REMOVE(phtree, &pp->pr_phtree, ph);
363 			s = splhigh();
364 			pool_put(&phpool, ph);
365 			splx(s);
366 		}
367 	}
368 	pp->pr_npages--;
369 	pp->pr_npagefree++;
370 
371 	pool_update_curpage(pp);
372 }
373 
374 /*
375  * Initialize the given pool resource structure.
376  *
377  * We export this routine to allow other kernel parts to declare
378  * static pools that must be initialized before malloc() is available.
379  */
380 void
381 pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
382     const char *wchan, struct pool_allocator *palloc)
383 {
384 	int off, slack;
385 
386 #ifdef POOL_DIAGNOSTIC
387 	/*
388 	 * Always log if POOL_DIAGNOSTIC is defined.
389 	 */
390 	if (pool_logsize != 0)
391 		flags |= PR_LOGGING;
392 #endif
393 
394 #ifdef MALLOC_DEBUG
395 	if ((flags & PR_DEBUG) && (ioff != 0 || align != 0))
396 		flags &= ~PR_DEBUG;
397 #endif
398 	/*
399 	 * Check arguments and construct default values.
400 	 */
401 	if (palloc == NULL)
402 		palloc = &pool_allocator_nointr;
403 	if ((palloc->pa_flags & PA_INITIALIZED) == 0) {
404 		if (palloc->pa_pagesz == 0)
405 			palloc->pa_pagesz = PAGE_SIZE;
406 
407 		TAILQ_INIT(&palloc->pa_list);
408 
409 		simple_lock_init(&palloc->pa_slock);
410 		palloc->pa_pagemask = ~(palloc->pa_pagesz - 1);
411 		palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1;
412 		palloc->pa_flags |= PA_INITIALIZED;
413 	}
414 
415 	if (align == 0)
416 		align = ALIGN(1);
417 
418 	if (size < sizeof(struct pool_item))
419 		size = sizeof(struct pool_item);
420 
421 	size = roundup(size, align);
422 #ifdef DIAGNOSTIC
423 	if (size > palloc->pa_pagesz)
424 		panic("pool_init: pool item size (%lu) too large",
425 		      (u_long)size);
426 #endif
427 
428 	/*
429 	 * Initialize the pool structure.
430 	 */
431 	LIST_INIT(&pp->pr_emptypages);
432 	LIST_INIT(&pp->pr_fullpages);
433 	LIST_INIT(&pp->pr_partpages);
434 	TAILQ_INIT(&pp->pr_cachelist);
435 	pp->pr_curpage = NULL;
436 	pp->pr_npages = 0;
437 	pp->pr_minitems = 0;
438 	pp->pr_minpages = 0;
439 	pp->pr_maxpages = 8;
440 	pp->pr_roflags = flags;
441 	pp->pr_flags = 0;
442 	pp->pr_size = size;
443 	pp->pr_align = align;
444 	pp->pr_wchan = wchan;
445 	pp->pr_alloc = palloc;
446 	pp->pr_nitems = 0;
447 	pp->pr_nout = 0;
448 	pp->pr_hardlimit = UINT_MAX;
449 	pp->pr_hardlimit_warning = NULL;
450 	pp->pr_hardlimit_ratecap.tv_sec = 0;
451 	pp->pr_hardlimit_ratecap.tv_usec = 0;
452 	pp->pr_hardlimit_warning_last.tv_sec = 0;
453 	pp->pr_hardlimit_warning_last.tv_usec = 0;
454 	pp->pr_serial = ++pool_serial;
455 	if (pool_serial == 0)
456 		panic("pool_init: too much uptime");
457 
458 	/*
459 	 * Decide whether to put the page header off page to avoid
460 	 * wasting too large a part of the page. Off-page page headers
461 	 * go on a hash table, so we can match a returned item
462 	 * with its header based on the page address.
463 	 * We use 1/16 of the page size as the threshold (XXX: tune)
464 	 */
465 	if (pp->pr_size < palloc->pa_pagesz/16) {
466 		/* Use the end of the page for the page header */
467 		pp->pr_roflags |= PR_PHINPAGE;
468 		pp->pr_phoffset = off = palloc->pa_pagesz -
469 		    ALIGN(sizeof(struct pool_item_header));
470 	} else {
471 		/* The page header will be taken from our page header pool */
472 		pp->pr_phoffset = 0;
473 		off = palloc->pa_pagesz;
474 		SPLAY_INIT(&pp->pr_phtree);
475 	}
476 
477 	/*
478 	 * Alignment is to take place at `ioff' within the item. This means
479 	 * we must reserve up to `align - 1' bytes on the page to allow
480 	 * appropriate positioning of each item.
481 	 *
482 	 * Silently enforce `0 <= ioff < align'.
483 	 */
484 	pp->pr_itemoffset = ioff = ioff % align;
485 	pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
486 	KASSERT(pp->pr_itemsperpage != 0);
487 
488 	/*
489 	 * Use the slack between the chunks and the page header
490 	 * for "cache coloring".
491 	 */
492 	slack = off - pp->pr_itemsperpage * pp->pr_size;
493 	pp->pr_maxcolor = (slack / align) * align;
494 	pp->pr_curcolor = 0;
495 
496 	pp->pr_nget = 0;
497 	pp->pr_nfail = 0;
498 	pp->pr_nput = 0;
499 	pp->pr_npagealloc = 0;
500 	pp->pr_npagefree = 0;
501 	pp->pr_hiwat = 0;
502 	pp->pr_nidle = 0;
503 
504 #ifdef POOL_DIAGNOSTIC
505 	if (flags & PR_LOGGING) {
506 		if (kmem_map == NULL ||
507 		    (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
508 		     M_TEMP, M_NOWAIT)) == NULL)
509 			pp->pr_roflags &= ~PR_LOGGING;
510 		pp->pr_curlogentry = 0;
511 		pp->pr_logsize = pool_logsize;
512 	}
513 #endif
514 
515 	pp->pr_entered_file = NULL;
516 	pp->pr_entered_line = 0;
517 
518 	simple_lock_init(&pp->pr_slock);
519 
520 	/*
521 	 * Initialize private page header pool and cache magazine pool if we
522 	 * haven't done so yet.
523 	 * XXX LOCKING.
524 	 */
525 	if (phpool.pr_size == 0) {
526 		pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
527 		    0, "phpool", NULL);
528 		pool_init(&pcgpool, sizeof(struct pool_cache_group), 0, 0,
529 		    0, "pcgpool", NULL);
530 	}
531 
532 	simple_lock_init(&pool_head_slock);
533 
534 	/* Insert this into the list of all pools. */
535 	simple_lock(&pool_head_slock);
536 	TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
537 	simple_unlock(&pool_head_slock);
538 
539 	/* Insert into the list of pools using this allocator. */
540 	simple_lock(&palloc->pa_slock);
541 	TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
542 	simple_unlock(&palloc->pa_slock);
543 }
544 
545 /*
546  * Decommission a pool resource.
547  */
548 void
549 pool_destroy(struct pool *pp)
550 {
551 	struct pool_item_header *ph;
552 	struct pool_cache *pc;
553 
554 	/* Locking order: pool_allocator -> pool */
555 	simple_lock(&pp->pr_alloc->pa_slock);
556 	TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list);
557 	simple_unlock(&pp->pr_alloc->pa_slock);
558 
559 	/* Destroy all caches for this pool. */
560 	while ((pc = TAILQ_FIRST(&pp->pr_cachelist)) != NULL)
561 		pool_cache_destroy(pc);
562 
563 #ifdef DIAGNOSTIC
564 	if (pp->pr_nout != 0) {
565 		pr_printlog(pp, NULL, printf);
566 		panic("pool_destroy: pool busy: still out: %u",
567 		    pp->pr_nout);
568 	}
569 #endif
570 
571 	/* Remove all pages */
572 	while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
573 		pr_rmpage(pp, ph, NULL);
574 	KASSERT(LIST_EMPTY(&pp->pr_fullpages));
575 	KASSERT(LIST_EMPTY(&pp->pr_partpages));
576 
577 	/* Remove from global pool list */
578 	simple_lock(&pool_head_slock);
579 	TAILQ_REMOVE(&pool_head, pp, pr_poollist);
580 	simple_unlock(&pool_head_slock);
581 
582 #ifdef POOL_DIAGNOSTIC
583 	if ((pp->pr_roflags & PR_LOGGING) != 0)
584 		free(pp->pr_log, M_TEMP);
585 #endif
586 }
587 
588 static struct pool_item_header *
589 pool_alloc_item_header(struct pool *pp, caddr_t storage, int flags)
590 {
591 	struct pool_item_header *ph;
592 	int s;
593 
594 	LOCK_ASSERT(simple_lock_held(&pp->pr_slock) == 0);
595 
596 	if ((pp->pr_roflags & PR_PHINPAGE) != 0)
597 		ph = (struct pool_item_header *) (storage + pp->pr_phoffset);
598 	else {
599 		s = splhigh();
600 		ph = pool_get(&phpool, flags);
601 		splx(s);
602 	}
603 
604 	return (ph);
605 }
606 
607 /*
608  * Grab an item from the pool; must be called at appropriate spl level
609  */
610 void *
611 #ifdef POOL_DIAGNOSTIC
612 _pool_get(struct pool *pp, int flags, const char *file, long line)
613 #else
614 pool_get(struct pool *pp, int flags)
615 #endif
616 {
617 	struct pool_item *pi;
618 	struct pool_item_header *ph;
619 	void *v;
620 
621 #ifdef DIAGNOSTIC
622 	if ((flags & PR_WAITOK) != 0)
623 		splassert(IPL_NONE);
624 	if (__predict_false(curproc == NULL && /* doing_shutdown == 0 && XXX*/
625 			    (flags & PR_WAITOK) != 0))
626 		panic("pool_get: %s:must have NOWAIT", pp->pr_wchan);
627 
628 #ifdef LOCKDEBUG
629 	if (flags & PR_WAITOK)
630 		simple_lock_only_held(NULL, "pool_get(PR_WAITOK)");
631 #endif
632 #endif /* DIAGNOSTIC */
633 
634 #ifdef MALLOC_DEBUG
635 	if (pp->pr_roflags & PR_DEBUG) {
636 		void *addr;
637 
638 		addr = NULL;
639 		debug_malloc(pp->pr_size, M_DEBUG,
640 		    (flags & PR_WAITOK) ? M_WAITOK : M_NOWAIT, &addr);
641 		return (addr);
642 	}
643 #endif
644 
645 	simple_lock(&pp->pr_slock);
646 	pr_enter(pp, file, line);
647 
648  startover:
649 	/*
650 	 * Check to see if we've reached the hard limit.  If we have,
651 	 * and we can wait, then wait until an item has been returned to
652 	 * the pool.
653 	 */
654 #ifdef DIAGNOSTIC
655 	if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
656 		pr_leave(pp);
657 		simple_unlock(&pp->pr_slock);
658 		panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
659 	}
660 #endif
661 	if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
662 		if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
663 			/*
664 			 * XXX: A warning isn't logged in this case.  Should
665 			 * it be?
666 			 */
667 			pp->pr_flags |= PR_WANTED;
668 			pr_leave(pp);
669 			ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
670 			pr_enter(pp, file, line);
671 			goto startover;
672 		}
673 
674 		/*
675 		 * Log a message that the hard limit has been hit.
676 		 */
677 		if (pp->pr_hardlimit_warning != NULL &&
678 		    ratecheck(&pp->pr_hardlimit_warning_last,
679 			      &pp->pr_hardlimit_ratecap))
680 			log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
681 
682 		pp->pr_nfail++;
683 
684 		pr_leave(pp);
685 		simple_unlock(&pp->pr_slock);
686 		return (NULL);
687 	}
688 
689 	/*
690 	 * The convention we use is that if `curpage' is not NULL, then
691 	 * it points at a non-empty bucket. In particular, `curpage'
692 	 * never points at a page header which has PR_PHINPAGE set and
693 	 * has no items in its bucket.
694 	 */
695 	if ((ph = pp->pr_curpage) == NULL) {
696 #ifdef DIAGNOSTIC
697 		if (pp->pr_nitems != 0) {
698 			simple_unlock(&pp->pr_slock);
699 			printf("pool_get: %s: curpage NULL, nitems %u\n",
700 			    pp->pr_wchan, pp->pr_nitems);
701 			panic("pool_get: nitems inconsistent");
702 		}
703 #endif
704 
705 		/*
706 		 * Call the back-end page allocator for more memory.
707 		 * Release the pool lock, as the back-end page allocator
708 		 * may block.
709 		 */
710 		pr_leave(pp);
711 		simple_unlock(&pp->pr_slock);
712 		v = pool_allocator_alloc(pp, flags);
713 		if (__predict_true(v != NULL))
714 			ph = pool_alloc_item_header(pp, v, flags);
715 		simple_lock(&pp->pr_slock);
716 		pr_enter(pp, file, line);
717 
718 		if (__predict_false(v == NULL || ph == NULL)) {
719 			if (v != NULL)
720 				pool_allocator_free(pp, v);
721 
722 			/*
723 			 * We were unable to allocate a page or item
724 			 * header, but we released the lock during
725 			 * allocation, so perhaps items were freed
726 			 * back to the pool.  Check for this case.
727 			 */
728 			if (pp->pr_curpage != NULL)
729 				goto startover;
730 
731 			if ((flags & PR_WAITOK) == 0) {
732 				pp->pr_nfail++;
733 				pr_leave(pp);
734 				simple_unlock(&pp->pr_slock);
735 				return (NULL);
736 			}
737 
738 			/*
739 			 * Wait for items to be returned to this pool.
740 			 *
741 			 * XXX: maybe we should wake up once a second and
742 			 * try again?
743 			 */
744 			pp->pr_flags |= PR_WANTED;
745 			/* PA_WANTED is already set on the allocator. */
746 			pr_leave(pp);
747 			ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
748 			pr_enter(pp, file, line);
749 			goto startover;
750 		}
751 
752 		/* We have more memory; add it to the pool */
753 		pool_prime_page(pp, v, ph);
754 		pp->pr_npagealloc++;
755 
756 		/* Start the allocation process over. */
757 		goto startover;
758 	}
759 	if (__predict_false((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)) {
760 		pr_leave(pp);
761 		simple_unlock(&pp->pr_slock);
762 		panic("pool_get: %s: page empty", pp->pr_wchan);
763 	}
764 #ifdef DIAGNOSTIC
765 	if (__predict_false(pp->pr_nitems == 0)) {
766 		pr_leave(pp);
767 		simple_unlock(&pp->pr_slock);
768 		printf("pool_get: %s: items on itemlist, nitems %u\n",
769 		    pp->pr_wchan, pp->pr_nitems);
770 		panic("pool_get: nitems inconsistent");
771 	}
772 #endif
773 
774 #ifdef POOL_DIAGNOSTIC
775 	pr_log(pp, v, PRLOG_GET, file, line);
776 #endif
777 
778 #ifdef DIAGNOSTIC
779 	if (__predict_false(pi->pi_magic != PI_MAGIC)) {
780 		pr_printlog(pp, pi, printf);
781 		panic("pool_get(%s): free list modified: magic=%x; page %p;"
782 		       " item addr %p",
783 			pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
784 	}
785 #endif
786 
787 	/*
788 	 * Remove from item list.
789 	 */
790 	TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
791 	pp->pr_nitems--;
792 	pp->pr_nout++;
793 	if (ph->ph_nmissing == 0) {
794 #ifdef DIAGNOSTIC
795 		if (__predict_false(pp->pr_nidle == 0))
796 			panic("pool_get: nidle inconsistent");
797 #endif
798 		pp->pr_nidle--;
799 
800 		/*
801 		 * This page was previously empty.  Move it to the list of
802 		 * partially-full pages.  This page is already curpage.
803 		 */
804 		LIST_REMOVE(ph, ph_pagelist);
805 		LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
806 	}
807 	ph->ph_nmissing++;
808 	if (TAILQ_EMPTY(&ph->ph_itemlist)) {
809 #ifdef DIAGNOSTIC
810 		if (__predict_false(ph->ph_nmissing != pp->pr_itemsperpage)) {
811 			pr_leave(pp);
812 			simple_unlock(&pp->pr_slock);
813 			panic("pool_get: %s: nmissing inconsistent",
814 			    pp->pr_wchan);
815 		}
816 #endif
817 		/*
818 		 * This page is now full.  Move it to the full list
819 		 * and select a new current page.
820 		 */
821 		LIST_REMOVE(ph, ph_pagelist);
822 		LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
823 		pool_update_curpage(pp);
824 	}
825 
826 	pp->pr_nget++;
827 
828 	/*
829 	 * If we have a low water mark and we are now below that low
830 	 * water mark, add more items to the pool.
831 	 */
832 	if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
833 		/*
834 		 * XXX: Should we log a warning?  Should we set up a timeout
835 		 * to try again in a second or so?  The latter could break
836 		 * a caller's assumptions about interrupt protection, etc.
837 		 */
838 	}
839 
840 	pr_leave(pp);
841 	simple_unlock(&pp->pr_slock);
842 	return (v);
843 }
844 
845 /*
846  * Internal version of pool_put().  Pool is already locked/entered.
847  */
848 void
849 pool_do_put(struct pool *pp, void *v)
850 {
851 	struct pool_item *pi = v;
852 	struct pool_item_header *ph;
853 	caddr_t page;
854 
855 #ifdef MALLOC_DEBUG
856 	if (pp->pr_roflags & PR_DEBUG) {
857 		debug_free(v, M_DEBUG);
858 		return;
859 	}
860 #endif
861 
862 	LOCK_ASSERT(simple_lock_held(&pp->pr_slock));
863 
864 	page = (caddr_t)((vaddr_t)v & pp->pr_alloc->pa_pagemask);
865 
866 #ifdef DIAGNOSTIC
867 	if (__predict_false(pp->pr_nout == 0)) {
868 		printf("pool %s: putting with none out\n",
869 		    pp->pr_wchan);
870 		panic("pool_put");
871 	}
872 #endif
873 
874 	if (__predict_false((ph = pr_find_pagehead(pp, page)) == NULL)) {
875 		pr_printlog(pp, NULL, printf);
876 		panic("pool_put: %s: page header missing", pp->pr_wchan);
877 	}
878 
879 #ifdef LOCKDEBUG
880 	/*
881 	 * Check if we're freeing a locked simple lock.
882 	 */
883 	simple_lock_freecheck((caddr_t)pi, ((caddr_t)pi) + pp->pr_size);
884 #endif
885 
886 	/*
887 	 * Return to item list.
888 	 */
889 #ifdef DIAGNOSTIC
890 	pi->pi_magic = PI_MAGIC;
891 #endif
892 #ifdef DEBUG
893 	{
894 		int i, *ip = v;
895 
896 		for (i = 0; i < pp->pr_size / sizeof(int); i++) {
897 			*ip++ = PI_MAGIC;
898 		}
899 	}
900 #endif
901 
902 	TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
903 	ph->ph_nmissing--;
904 	pp->pr_nput++;
905 	pp->pr_nitems++;
906 	pp->pr_nout--;
907 
908 	/* Cancel "pool empty" condition if it exists */
909 	if (pp->pr_curpage == NULL)
910 		pp->pr_curpage = ph;
911 
912 	if (pp->pr_flags & PR_WANTED) {
913 		pp->pr_flags &= ~PR_WANTED;
914 		if (ph->ph_nmissing == 0)
915 			pp->pr_nidle++;
916 		wakeup(pp);
917 		return;
918 	}
919 
920 	/*
921 	 * If this page is now empty, do one of two things:
922 	 *
923 	 *	(1) If we have more pages than the page high water mark,
924 	 *	    free the page back to the system.
925 	 *
926 	 *	(2) Otherwise, move the page to the empty page list.
927 	 *
928 	 * Either way, select a new current page (so we use a partially-full
929 	 * page if one is available).
930 	 */
931 	if (ph->ph_nmissing == 0) {
932 		pp->pr_nidle++;
933 		if (pp->pr_nidle > pp->pr_maxpages ||
934 		    (pp->pr_alloc->pa_flags & PA_WANT) != 0) {
935 			pr_rmpage(pp, ph, NULL);
936 		} else {
937 			LIST_REMOVE(ph, ph_pagelist);
938 			LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
939 
940 			/*
941 			 * Update the timestamp on the page.  A page must
942 			 * be idle for some period of time before it can
943 			 * be reclaimed by the pagedaemon.  This minimizes
944 			 * ping-pong'ing for memory.
945 			 */
946 			microuptime(&ph->ph_time);
947 		}
948 		pool_update_curpage(pp);
949 	}
950 
951 	/*
952 	 * If the page was previously completely full, move it to the
953 	 * partially-full list and make it the current page.  The next
954 	 * allocation will get the item from this page, instead of
955 	 * further fragmenting the pool.
956 	 */
957 	else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
958 		LIST_REMOVE(ph, ph_pagelist);
959 		LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
960 		pp->pr_curpage = ph;
961 	}
962 }
963 
964 /*
965  * Return resource to the pool; must be called at appropriate spl level
966  */
967 #ifdef POOL_DIAGNOSTIC
968 void
969 _pool_put(struct pool *pp, void *v, const char *file, long line)
970 {
971 
972 	simple_lock(&pp->pr_slock);
973 	pr_enter(pp, file, line);
974 
975 	pr_log(pp, v, PRLOG_PUT, file, line);
976 
977 	pool_do_put(pp, v);
978 
979 	pr_leave(pp);
980 	simple_unlock(&pp->pr_slock);
981 }
982 #undef pool_put
983 #endif /* POOL_DIAGNOSTIC */
984 
985 void
986 pool_put(struct pool *pp, void *v)
987 {
988 
989 	simple_lock(&pp->pr_slock);
990 
991 	pool_do_put(pp, v);
992 
993 	simple_unlock(&pp->pr_slock);
994 }
995 
996 #ifdef POOL_DIAGNOSTIC
997 #define		pool_put(h, v)	_pool_put((h), (v), __FILE__, __LINE__)
998 #endif
999 
1000 /*
1001  * Add N items to the pool.
1002  */
1003 int
1004 pool_prime(struct pool *pp, int n)
1005 {
1006 	struct pool_item_header *ph;
1007 	caddr_t cp;
1008 	int newpages;
1009 
1010 	simple_lock(&pp->pr_slock);
1011 
1012 	newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1013 
1014 	while (newpages-- > 0) {
1015 		simple_unlock(&pp->pr_slock);
1016 		cp = pool_allocator_alloc(pp, PR_NOWAIT);
1017 		if (__predict_true(cp != NULL))
1018 			ph = pool_alloc_item_header(pp, cp, PR_NOWAIT);
1019 		simple_lock(&pp->pr_slock);
1020 
1021 		if (__predict_false(cp == NULL || ph == NULL)) {
1022 			if (cp != NULL)
1023 				pool_allocator_free(pp, cp);
1024 			break;
1025 		}
1026 
1027 		pool_prime_page(pp, cp, ph);
1028 		pp->pr_npagealloc++;
1029 		pp->pr_minpages++;
1030 	}
1031 
1032 	if (pp->pr_minpages >= pp->pr_maxpages)
1033 		pp->pr_maxpages = pp->pr_minpages + 1;	/* XXX */
1034 
1035 	simple_unlock(&pp->pr_slock);
1036 	return (0);
1037 }
1038 
1039 /*
1040  * Add a page worth of items to the pool.
1041  *
1042  * Note, we must be called with the pool descriptor LOCKED.
1043  */
1044 void
1045 pool_prime_page(struct pool *pp, caddr_t storage, struct pool_item_header *ph)
1046 {
1047 	struct pool_item *pi;
1048 	caddr_t cp = storage;
1049 	unsigned int align = pp->pr_align;
1050 	unsigned int ioff = pp->pr_itemoffset;
1051 	int n;
1052 
1053 #ifdef DIAGNOSTIC
1054 	if (((u_long)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0)
1055 		panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1056 #endif
1057 
1058 	/*
1059 	 * Insert page header.
1060 	 */
1061 	LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1062 	TAILQ_INIT(&ph->ph_itemlist);
1063 	ph->ph_page = storage;
1064 	ph->ph_nmissing = 0;
1065 	memset(&ph->ph_time, 0, sizeof(ph->ph_time));
1066 	if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1067 		SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
1068 
1069 	pp->pr_nidle++;
1070 
1071 	/*
1072 	 * Color this page.
1073 	 */
1074 	cp = (caddr_t)(cp + pp->pr_curcolor);
1075 	if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
1076 		pp->pr_curcolor = 0;
1077 
1078 	/*
1079 	 * Adjust storage to apply aligment to `pr_itemoffset' in each item.
1080 	 */
1081 	if (ioff != 0)
1082 		cp = (caddr_t)(cp + (align - ioff));
1083 
1084 	/*
1085 	 * Insert remaining chunks on the bucket list.
1086 	 */
1087 	n = pp->pr_itemsperpage;
1088 	pp->pr_nitems += n;
1089 
1090 	while (n--) {
1091 		pi = (struct pool_item *)cp;
1092 
1093 		KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
1094 
1095 		/* Insert on page list */
1096 		TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
1097 #ifdef DIAGNOSTIC
1098 		pi->pi_magic = PI_MAGIC;
1099 #endif
1100 		cp = (caddr_t)(cp + pp->pr_size);
1101 	}
1102 
1103 	/*
1104 	 * If the pool was depleted, point at the new page.
1105 	 */
1106 	if (pp->pr_curpage == NULL)
1107 		pp->pr_curpage = ph;
1108 
1109 	if (++pp->pr_npages > pp->pr_hiwat)
1110 		pp->pr_hiwat = pp->pr_npages;
1111 }
1112 
1113 /*
1114  * Used by pool_get() when nitems drops below the low water mark.  This
1115  * is used to catch up pr_nitems with the low water mark.
1116  *
1117  * Note 1, we never wait for memory here, we let the caller decide what to do.
1118  *
1119  * Note 2, we must be called with the pool already locked, and we return
1120  * with it locked.
1121  */
1122 int
1123 pool_catchup(struct pool *pp)
1124 {
1125 	struct pool_item_header *ph;
1126 	caddr_t cp;
1127 	int error = 0;
1128 
1129 	while (POOL_NEEDS_CATCHUP(pp)) {
1130 		/*
1131 		 * Call the page back-end allocator for more memory.
1132 		 *
1133 		 * XXX: We never wait, so should we bother unlocking
1134 		 * the pool descriptor?
1135 		 */
1136 		simple_unlock(&pp->pr_slock);
1137 		cp = pool_allocator_alloc(pp, PR_NOWAIT);
1138 		if (__predict_true(cp != NULL))
1139 			ph = pool_alloc_item_header(pp, cp, PR_NOWAIT);
1140 		simple_lock(&pp->pr_slock);
1141 		if (__predict_false(cp == NULL || ph == NULL)) {
1142 			if (cp != NULL)
1143 				pool_allocator_free(pp, cp);
1144 			error = ENOMEM;
1145 			break;
1146 		}
1147 		pool_prime_page(pp, cp, ph);
1148 		pp->pr_npagealloc++;
1149 	}
1150 
1151 	return (error);
1152 }
1153 
1154 void
1155 pool_update_curpage(struct pool *pp)
1156 {
1157 
1158 	pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
1159 	if (pp->pr_curpage == NULL) {
1160 		pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
1161 	}
1162 }
1163 
1164 void
1165 pool_setlowat(struct pool *pp, int n)
1166 {
1167 
1168 	simple_lock(&pp->pr_slock);
1169 
1170 	pp->pr_minitems = n;
1171 	pp->pr_minpages = (n == 0)
1172 		? 0
1173 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1174 
1175 	/* Make sure we're caught up with the newly-set low water mark. */
1176 	if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1177 		/*
1178 		 * XXX: Should we log a warning?  Should we set up a timeout
1179 		 * to try again in a second or so?  The latter could break
1180 		 * a caller's assumptions about interrupt protection, etc.
1181 		 */
1182 	}
1183 
1184 	simple_unlock(&pp->pr_slock);
1185 }
1186 
1187 void
1188 pool_sethiwat(struct pool *pp, int n)
1189 {
1190 
1191 	simple_lock(&pp->pr_slock);
1192 
1193 	pp->pr_maxpages = (n == 0)
1194 		? 0
1195 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1196 
1197 	simple_unlock(&pp->pr_slock);
1198 }
1199 
1200 int
1201 pool_sethardlimit(struct pool *pp, unsigned n, const char *warnmess, int ratecap)
1202 {
1203 	int error = 0;
1204 
1205 	simple_lock(&pp->pr_slock);
1206 
1207 	if (n < pp->pr_nout) {
1208 		error = EINVAL;
1209 		goto done;
1210 	}
1211 
1212 	pp->pr_hardlimit = n;
1213 	pp->pr_hardlimit_warning = warnmess;
1214 	pp->pr_hardlimit_ratecap.tv_sec = ratecap;
1215 	pp->pr_hardlimit_warning_last.tv_sec = 0;
1216 	pp->pr_hardlimit_warning_last.tv_usec = 0;
1217 
1218 	/*
1219 	 * In-line version of pool_sethiwat(), because we don't want to
1220 	 * release the lock.
1221 	 */
1222 	pp->pr_maxpages = (n == 0 || n == UINT_MAX)
1223 		? n
1224 		: roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1225 
1226  done:
1227 	simple_unlock(&pp->pr_slock);
1228 
1229 	return (error);
1230 }
1231 
1232 /*
1233  * Release all complete pages that have not been used recently.
1234  *
1235  * Returns non-zero if any pages have been reclaimed.
1236  */
1237 int
1238 #ifdef POOL_DIAGNOSTIC
1239 _pool_reclaim(struct pool *pp, const char *file, long line)
1240 #else
1241 pool_reclaim(struct pool *pp)
1242 #endif
1243 {
1244 	struct pool_item_header *ph, *phnext;
1245 	struct pool_cache *pc;
1246 	struct timeval curtime;
1247 	struct pool_pagelist pq;
1248 	struct timeval diff;
1249 	int s;
1250 
1251 	if (simple_lock_try(&pp->pr_slock) == 0)
1252 		return (0);
1253 	pr_enter(pp, file, line);
1254 
1255 	LIST_INIT(&pq);
1256 
1257 	/*
1258 	 * Reclaim items from the pool's caches.
1259 	 */
1260 	TAILQ_FOREACH(pc, &pp->pr_cachelist, pc_poollist)
1261 		pool_cache_reclaim(pc);
1262 
1263 	microuptime(&curtime);
1264 
1265 	for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
1266 		phnext = LIST_NEXT(ph, ph_pagelist);
1267 
1268 		/* Check our minimum page claim */
1269 		if (pp->pr_npages <= pp->pr_minpages)
1270 			break;
1271 
1272 		KASSERT(ph->ph_nmissing == 0);
1273 		timersub(&curtime, &ph->ph_time, &diff);
1274 		if (diff.tv_sec < pool_inactive_time)
1275 			continue;
1276 
1277 		/*
1278 		 * If freeing this page would put us below
1279 		 * the low water mark, stop now.
1280 		 */
1281 		if ((pp->pr_nitems - pp->pr_itemsperpage) <
1282 		    pp->pr_minitems)
1283 			break;
1284 
1285 		pr_rmpage(pp, ph, &pq);
1286 	}
1287 
1288 	pr_leave(pp);
1289 	simple_unlock(&pp->pr_slock);
1290 	if (LIST_EMPTY(&pq))
1291 		return (0);
1292 	while ((ph = LIST_FIRST(&pq)) != NULL) {
1293 		LIST_REMOVE(ph, ph_pagelist);
1294 		pool_allocator_free(pp, ph->ph_page);
1295 		if (pp->pr_roflags & PR_PHINPAGE) {
1296 			continue;
1297 		}
1298 		SPLAY_REMOVE(phtree, &pp->pr_phtree, ph);
1299 		s = splhigh();
1300 		pool_put(&phpool, ph);
1301 		splx(s);
1302 	}
1303 
1304 	return (1);
1305 }
1306 
1307 #ifdef DDB
1308 #include <machine/db_machdep.h>
1309 #include <ddb/db_interface.h>
1310 #include <ddb/db_output.h>
1311 
1312 /*
1313  * Diagnostic helpers.
1314  */
1315 void
1316 pool_printit(struct pool *pp, const char *modif, int (*pr)(const char *, ...))
1317 {
1318 	int s;
1319 
1320 	s = splvm();
1321 	if (simple_lock_try(&pp->pr_slock) == 0) {
1322 		pr("pool %s is locked; try again later\n",
1323 		    pp->pr_wchan);
1324 		splx(s);
1325 		return;
1326 	}
1327 	pool_print1(pp, modif, pr);
1328 	simple_unlock(&pp->pr_slock);
1329 	splx(s);
1330 }
1331 
1332 void
1333 pool_print_pagelist(struct pool_pagelist *pl, int (*pr)(const char *, ...))
1334 {
1335 	struct pool_item_header *ph;
1336 #ifdef DIAGNOSTIC
1337 	struct pool_item *pi;
1338 #endif
1339 
1340 	LIST_FOREACH(ph, pl, ph_pagelist) {
1341 		(*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
1342 		    ph->ph_page, ph->ph_nmissing,
1343 		    (u_long)ph->ph_time.tv_sec,
1344 		    (u_long)ph->ph_time.tv_usec);
1345 #ifdef DIAGNOSTIC
1346 		TAILQ_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1347 			if (pi->pi_magic != PI_MAGIC) {
1348 				(*pr)("\t\t\titem %p, magic 0x%x\n",
1349 				    pi, pi->pi_magic);
1350 			}
1351 		}
1352 #endif
1353 	}
1354 }
1355 
1356 void
1357 pool_print1(struct pool *pp, const char *modif, int (*pr)(const char *, ...))
1358 {
1359 	struct pool_item_header *ph;
1360 	struct pool_cache *pc;
1361 	struct pool_cache_group *pcg;
1362 	int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1363 	char c;
1364 
1365 	while ((c = *modif++) != '\0') {
1366 		if (c == 'l')
1367 			print_log = 1;
1368 		if (c == 'p')
1369 			print_pagelist = 1;
1370 		if (c == 'c')
1371 			print_cache = 1;
1372 		modif++;
1373 	}
1374 
1375 	(*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1376 	    pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1377 	    pp->pr_roflags);
1378 	(*pr)("\talloc %p\n", pp->pr_alloc);
1379 	(*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1380 	    pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1381 	(*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1382 	    pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1383 
1384 	(*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
1385 	    pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1386 	(*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1387 	    pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1388 
1389 	if (print_pagelist == 0)
1390 		goto skip_pagelist;
1391 
1392 	if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1393 		(*pr)("\n\tempty page list:\n");
1394 	pool_print_pagelist(&pp->pr_emptypages, pr);
1395 	if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
1396 		(*pr)("\n\tfull page list:\n");
1397 	pool_print_pagelist(&pp->pr_fullpages, pr);
1398 	if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
1399 		(*pr)("\n\tpartial-page list:\n");
1400 	pool_print_pagelist(&pp->pr_partpages, pr);
1401 
1402 	if (pp->pr_curpage == NULL)
1403 		(*pr)("\tno current page\n");
1404 	else
1405 		(*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1406 
1407 skip_pagelist:
1408 	if (print_log == 0)
1409 		goto skip_log;
1410 
1411 	(*pr)("\n");
1412 	if ((pp->pr_roflags & PR_LOGGING) == 0)
1413 		(*pr)("\tno log\n");
1414 	else
1415 		pr_printlog(pp, NULL, pr);
1416 
1417 skip_log:
1418 	if (print_cache == 0)
1419 		goto skip_cache;
1420 
1421 	TAILQ_FOREACH(pc, &pp->pr_cachelist, pc_poollist) {
1422 		(*pr)("\tcache %p: allocfrom %p freeto %p\n", pc,
1423 		    pc->pc_allocfrom, pc->pc_freeto);
1424 		(*pr)("\t    hits %lu misses %lu ngroups %lu nitems %lu\n",
1425 		    pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems);
1426 		TAILQ_FOREACH(pcg, &pc->pc_grouplist, pcg_list) {
1427 			(*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail);
1428 			for (i = 0; i < PCG_NOBJECTS; i++)
1429 				(*pr)("\t\t\t%p\n", pcg->pcg_objects[i]);
1430 		}
1431 	}
1432 
1433 skip_cache:
1434 	pr_enter_check(pp, pr);
1435 }
1436 
1437 void
1438 db_show_all_pools(db_expr_t expr, int haddr, db_expr_t count, char *modif)
1439 {
1440 	struct pool *pp;
1441 	char maxp[16];
1442 	int ovflw;
1443 	char mode;
1444 
1445 	mode = modif[0];
1446 	if (mode != '\0' && mode != 'a') {
1447 		db_printf("usage: show all pools [/a]\n");
1448 		return;
1449 	}
1450 
1451 	if (mode == '\0')
1452 		db_printf("%-10s%4s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n",
1453 		    "Name",
1454 		    "Size",
1455 		    "Requests",
1456 		    "Fail",
1457 		    "Releases",
1458 		    "Pgreq",
1459 		    "Pgrel",
1460 		    "Npage",
1461 		    "Hiwat",
1462 		    "Minpg",
1463 		    "Maxpg",
1464 		    "Idle");
1465 	else
1466 		db_printf("%-10s %18s %18s\n",
1467 		    "Name", "Address", "Allocator");
1468 
1469 	TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
1470 		if (mode == 'a') {
1471 			db_printf("%-10s %18p %18p\n", pp->pr_wchan, pp,
1472 			    pp->pr_alloc);
1473 			continue;
1474 		}
1475 
1476 		if (!pp->pr_nget)
1477 			continue;
1478 
1479 		if (pp->pr_maxpages == UINT_MAX)
1480 			snprintf(maxp, sizeof maxp, "inf");
1481 		else
1482 			snprintf(maxp, sizeof maxp, "%u", pp->pr_maxpages);
1483 
1484 #define PRWORD(ovflw, fmt, width, fixed, val) do {	\
1485 	(ovflw) += db_printf((fmt),			\
1486 	    (width) - (fixed) - (ovflw) > 0 ?		\
1487 	    (width) - (fixed) - (ovflw) : 0,		\
1488 	    (val)) - (width);				\
1489 	if ((ovflw) < 0)				\
1490 		(ovflw) = 0;				\
1491 } while (/* CONSTCOND */0)
1492 
1493 		ovflw = 0;
1494 		PRWORD(ovflw, "%-*s", 10, 0, pp->pr_wchan);
1495 		PRWORD(ovflw, " %*u", 4, 1, pp->pr_size);
1496 		PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget);
1497 		PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
1498 		PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput);
1499 		PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc);
1500 		PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree);
1501 		PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages);
1502 		PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat);
1503 		PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages);
1504 		PRWORD(ovflw, " %*s", 6, 1, maxp);
1505 		PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle);
1506 	}
1507 }
1508 
1509 int
1510 pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
1511 {
1512 	struct pool_item *pi;
1513 	caddr_t page;
1514 	int n;
1515 
1516 	page = (caddr_t)((u_long)ph & pp->pr_alloc->pa_pagemask);
1517 	if (page != ph->ph_page &&
1518 	    (pp->pr_roflags & PR_PHINPAGE) != 0) {
1519 		if (label != NULL)
1520 			printf("%s: ", label);
1521 		printf("pool(%p:%s): page inconsistency: page %p;"
1522 		       " at page head addr %p (p %p)\n", pp,
1523 			pp->pr_wchan, ph->ph_page,
1524 			ph, page);
1525 		return 1;
1526 	}
1527 
1528 	for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
1529 	     pi != NULL;
1530 	     pi = TAILQ_NEXT(pi,pi_list), n++) {
1531 
1532 #ifdef DIAGNOSTIC
1533 		if (pi->pi_magic != PI_MAGIC) {
1534 			if (label != NULL)
1535 				printf("%s: ", label);
1536 			printf("pool(%s): free list modified: magic=%x;"
1537 			       " page %p; item ordinal %d;"
1538 			       " addr %p (p %p)\n",
1539 				pp->pr_wchan, pi->pi_magic, ph->ph_page,
1540 				n, pi, page);
1541 			panic("pool");
1542 		}
1543 #endif
1544 		page =
1545 		    (caddr_t)((u_long)pi & pp->pr_alloc->pa_pagemask);
1546 		if (page == ph->ph_page)
1547 			continue;
1548 
1549 		if (label != NULL)
1550 			printf("%s: ", label);
1551 		printf("pool(%p:%s): page inconsistency: page %p;"
1552 		       " item ordinal %d; addr %p (p %p)\n", pp,
1553 			pp->pr_wchan, ph->ph_page,
1554 			n, pi, page);
1555 		return 1;
1556 	}
1557 	return 0;
1558 }
1559 
1560 int
1561 pool_chk(struct pool *pp, const char *label)
1562 {
1563 	struct pool_item_header *ph;
1564 	int r = 0;
1565 
1566 	simple_lock(&pp->pr_slock);
1567 	LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
1568 		r = pool_chk_page(pp, label, ph);
1569 		if (r) {
1570 			goto out;
1571 		}
1572 	}
1573 	LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
1574 		r = pool_chk_page(pp, label, ph);
1575 		if (r) {
1576 			goto out;
1577 		}
1578 	}
1579 	LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
1580 		r = pool_chk_page(pp, label, ph);
1581 		if (r) {
1582 			goto out;
1583 		}
1584 	}
1585 
1586 out:
1587 	simple_unlock(&pp->pr_slock);
1588 	return (r);
1589 }
1590 #endif
1591 
1592 /*
1593  * pool_cache_init:
1594  *
1595  *	Initialize a pool cache.
1596  *
1597  *	NOTE: If the pool must be protected from interrupts, we expect
1598  *	to be called at the appropriate interrupt priority level.
1599  */
1600 void
1601 pool_cache_init(struct pool_cache *pc, struct pool *pp,
1602     int (*ctor)(void *, void *, int),
1603     void (*dtor)(void *, void *),
1604     void *arg)
1605 {
1606 
1607 	TAILQ_INIT(&pc->pc_grouplist);
1608 	simple_lock_init(&pc->pc_slock);
1609 
1610 	pc->pc_allocfrom = NULL;
1611 	pc->pc_freeto = NULL;
1612 	pc->pc_pool = pp;
1613 
1614 	pc->pc_ctor = ctor;
1615 	pc->pc_dtor = dtor;
1616 	pc->pc_arg  = arg;
1617 
1618 	pc->pc_hits   = 0;
1619 	pc->pc_misses = 0;
1620 
1621 	pc->pc_ngroups = 0;
1622 
1623 	pc->pc_nitems = 0;
1624 
1625 	simple_lock(&pp->pr_slock);
1626 	TAILQ_INSERT_TAIL(&pp->pr_cachelist, pc, pc_poollist);
1627 	simple_unlock(&pp->pr_slock);
1628 }
1629 
1630 /*
1631  * pool_cache_destroy:
1632  *
1633  *	Destroy a pool cache.
1634  */
1635 void
1636 pool_cache_destroy(struct pool_cache *pc)
1637 {
1638 	struct pool *pp = pc->pc_pool;
1639 
1640 	/* First, invalidate the entire cache. */
1641 	pool_cache_invalidate(pc);
1642 
1643 	/* ...and remove it from the pool's cache list. */
1644 	simple_lock(&pp->pr_slock);
1645 	TAILQ_REMOVE(&pp->pr_cachelist, pc, pc_poollist);
1646 	simple_unlock(&pp->pr_slock);
1647 }
1648 
1649 static __inline void *
1650 pcg_get(struct pool_cache_group *pcg)
1651 {
1652 	void *object;
1653 	u_int idx;
1654 
1655 	KASSERT(pcg->pcg_avail <= PCG_NOBJECTS);
1656 	KASSERT(pcg->pcg_avail != 0);
1657 	idx = --pcg->pcg_avail;
1658 
1659 	KASSERT(pcg->pcg_objects[idx] != NULL);
1660 	object = pcg->pcg_objects[idx];
1661 	pcg->pcg_objects[idx] = NULL;
1662 
1663 	return (object);
1664 }
1665 
1666 static __inline void
1667 pcg_put(struct pool_cache_group *pcg, void *object)
1668 {
1669 	u_int idx;
1670 
1671 	KASSERT(pcg->pcg_avail < PCG_NOBJECTS);
1672 	idx = pcg->pcg_avail++;
1673 
1674 	KASSERT(pcg->pcg_objects[idx] == NULL);
1675 	pcg->pcg_objects[idx] = object;
1676 }
1677 
1678 /*
1679  * pool_cache_get:
1680  *
1681  *	Get an object from a pool cache.
1682  */
1683 void *
1684 pool_cache_get(struct pool_cache *pc, int flags)
1685 {
1686 	struct pool_cache_group *pcg;
1687 	void *object;
1688 
1689 #ifdef LOCKDEBUG
1690 	if (flags & PR_WAITOK)
1691 		simple_lock_only_held(NULL, "pool_cache_get(PR_WAITOK)");
1692 #endif
1693 
1694 	simple_lock(&pc->pc_slock);
1695 
1696 	if ((pcg = pc->pc_allocfrom) == NULL) {
1697 		TAILQ_FOREACH(pcg, &pc->pc_grouplist, pcg_list) {
1698 			if (pcg->pcg_avail != 0) {
1699 				pc->pc_allocfrom = pcg;
1700 				goto have_group;
1701 			}
1702 		}
1703 
1704 		/*
1705 		 * No groups with any available objects.  Allocate
1706 		 * a new object, construct it, and return it to
1707 		 * the caller.  We will allocate a group, if necessary,
1708 		 * when the object is freed back to the cache.
1709 		 */
1710 		pc->pc_misses++;
1711 		simple_unlock(&pc->pc_slock);
1712 		object = pool_get(pc->pc_pool, flags);
1713 		if (object != NULL && pc->pc_ctor != NULL) {
1714 			if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) {
1715 				pool_put(pc->pc_pool, object);
1716 				return (NULL);
1717 			}
1718 		}
1719 		return (object);
1720 	}
1721 
1722  have_group:
1723 	pc->pc_hits++;
1724 	pc->pc_nitems--;
1725 	object = pcg_get(pcg);
1726 
1727 	if (pcg->pcg_avail == 0)
1728 		pc->pc_allocfrom = NULL;
1729 
1730 	simple_unlock(&pc->pc_slock);
1731 
1732 	return (object);
1733 }
1734 
1735 /*
1736  * pool_cache_put:
1737  *
1738  *	Put an object back to the pool cache.
1739  */
1740 void
1741 pool_cache_put(struct pool_cache *pc, void *object)
1742 {
1743 	struct pool_cache_group *pcg;
1744 	int s;
1745 
1746 	simple_lock(&pc->pc_slock);
1747 
1748 	if ((pcg = pc->pc_freeto) == NULL) {
1749 		TAILQ_FOREACH(pcg, &pc->pc_grouplist, pcg_list) {
1750 			if (pcg->pcg_avail != PCG_NOBJECTS) {
1751 				pc->pc_freeto = pcg;
1752 				goto have_group;
1753 			}
1754 		}
1755 
1756 		/*
1757 		 * No empty groups to free the object to.  Attempt to
1758 		 * allocate one.
1759 		 */
1760 		simple_unlock(&pc->pc_slock);
1761 		s = splvm();
1762 		pcg = pool_get(&pcgpool, PR_NOWAIT);
1763 		splx(s);
1764 		if (pcg != NULL) {
1765 			memset(pcg, 0, sizeof(*pcg));
1766 			simple_lock(&pc->pc_slock);
1767 			pc->pc_ngroups++;
1768 			TAILQ_INSERT_TAIL(&pc->pc_grouplist, pcg, pcg_list);
1769 			if (pc->pc_freeto == NULL)
1770 				pc->pc_freeto = pcg;
1771 			goto have_group;
1772 		}
1773 
1774 		/*
1775 		 * Unable to allocate a cache group; destruct the object
1776 		 * and free it back to the pool.
1777 		 */
1778 		pool_cache_destruct_object(pc, object);
1779 		return;
1780 	}
1781 
1782  have_group:
1783 	pc->pc_nitems++;
1784 	pcg_put(pcg, object);
1785 
1786 	if (pcg->pcg_avail == PCG_NOBJECTS)
1787 		pc->pc_freeto = NULL;
1788 
1789 	simple_unlock(&pc->pc_slock);
1790 }
1791 
1792 /*
1793  * pool_cache_destruct_object:
1794  *
1795  *	Force destruction of an object and its release back into
1796  *	the pool.
1797  */
1798 void
1799 pool_cache_destruct_object(struct pool_cache *pc, void *object)
1800 {
1801 
1802 	if (pc->pc_dtor != NULL)
1803 		(*pc->pc_dtor)(pc->pc_arg, object);
1804 	pool_put(pc->pc_pool, object);
1805 }
1806 
1807 /*
1808  * pool_cache_do_invalidate:
1809  *
1810  *	This internal function implements pool_cache_invalidate() and
1811  *	pool_cache_reclaim().
1812  */
1813 void
1814 pool_cache_do_invalidate(struct pool_cache *pc, int free_groups,
1815     void (*putit)(struct pool *, void *))
1816 {
1817 	struct pool_cache_group *pcg, *npcg;
1818 	void *object;
1819 	int s;
1820 
1821 	for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL;
1822 	     pcg = npcg) {
1823 		npcg = TAILQ_NEXT(pcg, pcg_list);
1824 		while (pcg->pcg_avail != 0) {
1825 			pc->pc_nitems--;
1826 			object = pcg_get(pcg);
1827 			if (pcg->pcg_avail == 0 && pc->pc_allocfrom == pcg)
1828 				pc->pc_allocfrom = NULL;
1829 			if (pc->pc_dtor != NULL)
1830 				(*pc->pc_dtor)(pc->pc_arg, object);
1831 			(*putit)(pc->pc_pool, object);
1832 		}
1833 		if (free_groups) {
1834 			pc->pc_ngroups--;
1835 			TAILQ_REMOVE(&pc->pc_grouplist, pcg, pcg_list);
1836 			if (pc->pc_freeto == pcg)
1837 				pc->pc_freeto = NULL;
1838 			s = splvm();
1839 			pool_put(&pcgpool, pcg);
1840 			splx(s);
1841 		}
1842 	}
1843 }
1844 
1845 /*
1846  * pool_cache_invalidate:
1847  *
1848  *	Invalidate a pool cache (destruct and release all of the
1849  *	cached objects).
1850  */
1851 void
1852 pool_cache_invalidate(struct pool_cache *pc)
1853 {
1854 
1855 	simple_lock(&pc->pc_slock);
1856 	pool_cache_do_invalidate(pc, 0, pool_put);
1857 	simple_unlock(&pc->pc_slock);
1858 }
1859 
1860 /*
1861  * pool_cache_reclaim:
1862  *
1863  *	Reclaim a pool cache for pool_reclaim().
1864  */
1865 void
1866 pool_cache_reclaim(struct pool_cache *pc)
1867 {
1868 
1869 	simple_lock(&pc->pc_slock);
1870 	pool_cache_do_invalidate(pc, 1, pool_do_put);
1871 	simple_unlock(&pc->pc_slock);
1872 }
1873 
1874 /*
1875  * We have three different sysctls.
1876  * kern.pool.npools - the number of pools.
1877  * kern.pool.pool.<pool#> - the pool struct for the pool#.
1878  * kern.pool.name.<pool#> - the name for pool#.
1879  */
1880 int
1881 sysctl_dopool(int *name, u_int namelen, char *where, size_t *sizep)
1882 {
1883 	struct pool *pp, *foundpool = NULL;
1884 	size_t buflen = where != NULL ? *sizep : 0;
1885 	int npools = 0, s;
1886 	unsigned int lookfor;
1887 	size_t len;
1888 
1889 	switch (*name) {
1890 	case KERN_POOL_NPOOLS:
1891 		if (namelen != 1 || buflen != sizeof(int))
1892 			return (EINVAL);
1893 		lookfor = 0;
1894 		break;
1895 	case KERN_POOL_NAME:
1896 		if (namelen != 2 || buflen < 1)
1897 			return (EINVAL);
1898 		lookfor = name[1];
1899 		break;
1900 	case KERN_POOL_POOL:
1901 		if (namelen != 2 || buflen != sizeof(struct pool))
1902 			return (EINVAL);
1903 		lookfor = name[1];
1904 		break;
1905 	default:
1906 		return (EINVAL);
1907 	}
1908 
1909 	s = splvm();
1910 	simple_lock(&pool_head_slock);
1911 
1912 	TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
1913 		npools++;
1914 		if (lookfor == pp->pr_serial) {
1915 			foundpool = pp;
1916 			break;
1917 		}
1918 	}
1919 
1920 	simple_unlock(&pool_head_slock);
1921 	splx(s);
1922 
1923 	if (lookfor != 0 && foundpool == NULL)
1924 		return (ENOENT);
1925 
1926 	switch (*name) {
1927 	case KERN_POOL_NPOOLS:
1928 		return copyout(&npools, where, buflen);
1929 	case KERN_POOL_NAME:
1930 		len = strlen(foundpool->pr_wchan) + 1;
1931 		if (*sizep < len)
1932 			return (ENOMEM);
1933 		*sizep = len;
1934 		return copyout(foundpool->pr_wchan, where, len);
1935 	case KERN_POOL_POOL:
1936 		return copyout(foundpool, where, buflen);
1937 	}
1938 	/* NOTREACHED */
1939 	return (0); /* XXX - Stupid gcc */
1940 }
1941 
1942 /*
1943  * Pool backend allocators.
1944  *
1945  * Each pool has a backend allocator that handles allocation, deallocation
1946  */
1947 void	*pool_page_alloc_oldnointr(struct pool *, int);
1948 void	pool_page_free_oldnointr(struct pool *, void *);
1949 void	*pool_page_alloc(struct pool *, int);
1950 void	pool_page_free(struct pool *, void *);
1951 
1952 /* previous nointr.  handles large allocations safely */
1953 struct pool_allocator pool_allocator_oldnointr = {
1954 	pool_page_alloc_oldnointr, pool_page_free_oldnointr, 0,
1955 };
1956 /* safe for interrupts, name preserved for compat
1957  * this is the default allocator */
1958 struct pool_allocator pool_allocator_nointr = {
1959 	pool_page_alloc, pool_page_free, 0,
1960 };
1961 
1962 /*
1963  * XXX - we have at least three different resources for the same allocation
1964  *  and each resource can be depleted. First we have the ready elements in
1965  *  the pool. Then we have the resource (typically a vm_map) for this
1966  *  allocator, then we have physical memory. Waiting for any of these can
1967  *  be unnecessary when any other is freed, but the kernel doesn't support
1968  *  sleeping on multiple addresses, so we have to fake. The caller sleeps on
1969  *  the pool (so that we can be awakened when an item is returned to the pool),
1970  *  but we set PA_WANT on the allocator. When a page is returned to
1971  *  the allocator and PA_WANT is set pool_allocator_free will wakeup all
1972  *  sleeping pools belonging to this allocator. (XXX - thundering herd).
1973  *  We also wake up the allocator in case someone without a pool (malloc)
1974  *  is sleeping waiting for this allocator.
1975  */
1976 
1977 void *
1978 pool_allocator_alloc(struct pool *pp, int flags)
1979 {
1980 
1981 	return (pp->pr_alloc->pa_alloc(pp, flags));
1982 }
1983 
1984 void
1985 pool_allocator_free(struct pool *pp, void *v)
1986 {
1987 	struct pool_allocator *pa = pp->pr_alloc;
1988 	int s;
1989 
1990 	(*pa->pa_free)(pp, v);
1991 
1992 	s = splvm();
1993 	simple_lock(&pa->pa_slock);
1994 	if ((pa->pa_flags & PA_WANT) == 0) {
1995 		simple_unlock(&pa->pa_slock);
1996 		splx(s);
1997 		return;
1998 	}
1999 
2000 	TAILQ_FOREACH(pp, &pa->pa_list, pr_alloc_list) {
2001 		simple_lock(&pp->pr_slock);
2002 		if ((pp->pr_flags & PR_WANTED) != 0) {
2003 			pp->pr_flags &= ~PR_WANTED;
2004 			wakeup(pp);
2005 		}
2006 		simple_unlock(&pp->pr_slock);
2007 	}
2008 	pa->pa_flags &= ~PA_WANT;
2009 	simple_unlock(&pa->pa_slock);
2010 	splx(s);
2011 }
2012 
2013 void *
2014 pool_page_alloc(struct pool *pp, int flags)
2015 {
2016 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
2017 
2018 	return (uvm_km_getpage(waitok));
2019 }
2020 
2021 void
2022 pool_page_free(struct pool *pp, void *v)
2023 {
2024 
2025 	uvm_km_putpage(v);
2026 }
2027 
2028 void *
2029 pool_page_alloc_oldnointr(struct pool *pp, int flags)
2030 {
2031 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
2032 
2033 	splassert(IPL_NONE);
2034 
2035 	return ((void *)uvm_km_alloc_poolpage1(kernel_map, uvm.kernel_object,
2036 	    waitok));
2037 }
2038 
2039 void
2040 pool_page_free_oldnointr(struct pool *pp, void *v)
2041 {
2042 	splassert(IPL_NONE);
2043 
2044 	uvm_km_free_poolpage1(kernel_map, (vaddr_t)v);
2045 }
2046