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