subr_pool.c revision 1.21.2.2.2.1 1 /* $NetBSD: subr_pool.c,v 1.21.2.2.2.1 1999/06/21 01:24:03 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1999 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
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/lock.h>
50 #include <sys/pool.h>
51 #include <sys/syslog.h>
52
53 #include <vm/vm.h>
54 #include <vm/vm_kern.h>
55
56 #include <uvm/uvm.h>
57
58 /*
59 * Pool resource management utility.
60 *
61 * Memory is allocated in pages which are split into pieces according
62 * to the pool item size. Each page is kept on a list headed by `pr_pagelist'
63 * in the pool structure and the individual pool items are on a linked list
64 * headed by `ph_itemlist' in each page header. The memory for building
65 * the page list is either taken from the allocated pages themselves (for
66 * small pool items) or taken from an internal pool of page headers (`phpool').
67 */
68
69 /* List of all pools */
70 TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
71
72 /* Private pool for page header structures */
73 static struct pool phpool;
74
75 /* # of seconds to retain page after last use */
76 int pool_inactive_time = 10;
77
78 /* Next candidate for drainage (see pool_drain()) */
79 static struct pool *drainpp;
80
81 /* This spin lock protects both pool_head and drainpp. */
82 struct simplelock pool_head_slock = SIMPLELOCK_INITIALIZER;
83
84 struct pool_item_header {
85 /* Page headers */
86 TAILQ_ENTRY(pool_item_header)
87 ph_pagelist; /* pool page list */
88 TAILQ_HEAD(,pool_item) ph_itemlist; /* chunk list for this page */
89 LIST_ENTRY(pool_item_header)
90 ph_hashlist; /* Off-page page headers */
91 int ph_nmissing; /* # of chunks in use */
92 caddr_t ph_page; /* this page's address */
93 struct timeval ph_time; /* last referenced */
94 };
95
96 struct pool_item {
97 #ifdef DIAGNOSTIC
98 int pi_magic;
99 #define PI_MAGIC 0xdeadbeef
100 #endif
101 /* Other entries use only this list entry */
102 TAILQ_ENTRY(pool_item) pi_list;
103 };
104
105
106 #define PR_HASH_INDEX(pp,addr) \
107 (((u_long)(addr) >> (pp)->pr_pageshift) & (PR_HASHTABSIZE - 1))
108
109
110
111 static struct pool_item_header
112 *pr_find_pagehead __P((struct pool *, caddr_t));
113 static void pr_rmpage __P((struct pool *, struct pool_item_header *));
114 static int pool_catchup __P((struct pool *));
115 static void pool_prime_page __P((struct pool *, caddr_t));
116 static void *pool_page_alloc __P((unsigned long, int, int));
117 static void pool_page_free __P((void *, unsigned long, int));
118
119 static void pool_print1 __P((struct pool *, const char *,
120 void (*)(const char *, ...)));
121
122 /*
123 * Pool log entry. An array of these is allocated in pool_create().
124 */
125 struct pool_log {
126 const char *pl_file;
127 long pl_line;
128 int pl_action;
129 #define PRLOG_GET 1
130 #define PRLOG_PUT 2
131 void *pl_addr;
132 };
133
134 /* Number of entries in pool log buffers */
135 #ifndef POOL_LOGSIZE
136 #define POOL_LOGSIZE 10
137 #endif
138
139 int pool_logsize = POOL_LOGSIZE;
140
141 #ifdef DIAGNOSTIC
142 static void pr_log __P((struct pool *, void *, int, const char *, long));
143 static void pr_printlog __P((struct pool *, struct pool_item *,
144 void (*)(const char *, ...)));
145 static void pr_enter __P((struct pool *, const char *, long));
146 static void pr_leave __P((struct pool *));
147 static void pr_enter_check __P((struct pool *,
148 void (*)(const char *, ...)));
149
150 static __inline__ void
151 pr_log(pp, v, action, file, line)
152 struct pool *pp;
153 void *v;
154 int action;
155 const char *file;
156 long line;
157 {
158 int n = pp->pr_curlogentry;
159 struct pool_log *pl;
160
161 if ((pp->pr_roflags & PR_LOGGING) == 0)
162 return;
163
164 /*
165 * Fill in the current entry. Wrap around and overwrite
166 * the oldest entry if necessary.
167 */
168 pl = &pp->pr_log[n];
169 pl->pl_file = file;
170 pl->pl_line = line;
171 pl->pl_action = action;
172 pl->pl_addr = v;
173 if (++n >= pp->pr_logsize)
174 n = 0;
175 pp->pr_curlogentry = n;
176 }
177
178 static void
179 pr_printlog(pp, pi, pr)
180 struct pool *pp;
181 struct pool_item *pi;
182 void (*pr) __P((const char *, ...));
183 {
184 int i = pp->pr_logsize;
185 int n = pp->pr_curlogentry;
186
187 if ((pp->pr_roflags & PR_LOGGING) == 0)
188 return;
189
190 /*
191 * Print all entries in this pool's log.
192 */
193 while (i-- > 0) {
194 struct pool_log *pl = &pp->pr_log[n];
195 if (pl->pl_action != 0) {
196 if (pi == NULL || pi == pl->pl_addr) {
197 (*pr)("\tlog entry %d:\n", i);
198 (*pr)("\t\taction = %s, addr = %p\n",
199 pl->pl_action == PRLOG_GET ? "get" : "put",
200 pl->pl_addr);
201 (*pr)("\t\tfile: %s at line %lu\n",
202 pl->pl_file, pl->pl_line);
203 }
204 }
205 if (++n >= pp->pr_logsize)
206 n = 0;
207 }
208 }
209
210 static __inline__ void
211 pr_enter(pp, file, line)
212 struct pool *pp;
213 const char *file;
214 long line;
215 {
216
217 if (pp->pr_entered_file != NULL) {
218 printf("pool %s: reentrancy at file %s line %ld\n",
219 pp->pr_wchan, file, line);
220 printf(" previous entry at file %s line %ld\n",
221 pp->pr_entered_file, pp->pr_entered_line);
222 panic("pr_enter");
223 }
224
225 pp->pr_entered_file = file;
226 pp->pr_entered_line = line;
227 }
228
229 static __inline__ void
230 pr_leave(pp)
231 struct pool *pp;
232 {
233
234 if (pp->pr_entered_file == NULL) {
235 printf("pool %s not entered?\n", pp->pr_wchan);
236 panic("pr_leave");
237 }
238
239 pp->pr_entered_file = NULL;
240 pp->pr_entered_line = 0;
241 }
242
243 static __inline__ void
244 pr_enter_check(pp, pr)
245 struct pool *pp;
246 void (*pr) __P((const char *, ...));
247 {
248
249 if (pp->pr_entered_file != NULL)
250 (*pr)("\n\tcurrently entered from file %s line %ld\n",
251 pp->pr_entered_file, pp->pr_entered_line);
252 }
253 #else
254 #define pr_log(pp, v, action, file, line)
255 #define pr_printlog(pp, pi, pr)
256 #define pr_enter(pp, file, line)
257 #define pr_leave(pp)
258 #define pr_enter_check(pp, pr)
259 #endif /* DIAGNOSTIC */
260
261 /*
262 * Return the pool page header based on page address.
263 */
264 static __inline__ struct pool_item_header *
265 pr_find_pagehead(pp, page)
266 struct pool *pp;
267 caddr_t page;
268 {
269 struct pool_item_header *ph;
270
271 if ((pp->pr_roflags & PR_PHINPAGE) != 0)
272 return ((struct pool_item_header *)(page + pp->pr_phoffset));
273
274 for (ph = LIST_FIRST(&pp->pr_hashtab[PR_HASH_INDEX(pp, page)]);
275 ph != NULL;
276 ph = LIST_NEXT(ph, ph_hashlist)) {
277 if (ph->ph_page == page)
278 return (ph);
279 }
280 return (NULL);
281 }
282
283 /*
284 * Remove a page from the pool.
285 */
286 static __inline__ void
287 pr_rmpage(pp, ph)
288 struct pool *pp;
289 struct pool_item_header *ph;
290 {
291
292 /*
293 * If the page was idle, decrement the idle page count.
294 */
295 if (ph->ph_nmissing == 0) {
296 #ifdef DIAGNOSTIC
297 if (pp->pr_nidle == 0)
298 panic("pr_rmpage: nidle inconsistent");
299 if (pp->pr_nitems < pp->pr_itemsperpage)
300 panic("pr_rmpage: nitems inconsistent");
301 #endif
302 pp->pr_nidle--;
303 }
304
305 pp->pr_nitems -= pp->pr_itemsperpage;
306
307 /*
308 * Unlink a page from the pool and release it.
309 */
310 TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
311 (*pp->pr_free)(ph->ph_page, pp->pr_pagesz, pp->pr_mtype);
312 pp->pr_npages--;
313 pp->pr_npagefree++;
314
315 if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
316 int s;
317 LIST_REMOVE(ph, ph_hashlist);
318 s = splhigh();
319 pool_put(&phpool, ph);
320 splx(s);
321 }
322
323 if (pp->pr_curpage == ph) {
324 /*
325 * Find a new non-empty page header, if any.
326 * Start search from the page head, to increase the
327 * chance for "high water" pages to be freed.
328 */
329 for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
330 ph = TAILQ_NEXT(ph, ph_pagelist))
331 if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
332 break;
333
334 pp->pr_curpage = ph;
335 }
336 }
337
338 /*
339 * Allocate and initialize a pool.
340 */
341 struct pool *
342 pool_create(size, align, ioff, nitems, wchan, pagesz, alloc, release, mtype)
343 size_t size;
344 u_int align;
345 u_int ioff;
346 int nitems;
347 const char *wchan;
348 size_t pagesz;
349 void *(*alloc) __P((unsigned long, int, int));
350 void (*release) __P((void *, unsigned long, int));
351 int mtype;
352 {
353 struct pool *pp;
354 int flags;
355
356 pp = (struct pool *)malloc(sizeof(*pp), M_POOL, M_NOWAIT);
357 if (pp == NULL)
358 return (NULL);
359
360 flags = PR_FREEHEADER;
361 pool_init(pp, size, align, ioff, flags, wchan, pagesz,
362 alloc, release, mtype);
363
364 if (nitems != 0) {
365 if (pool_prime(pp, nitems, NULL) != 0) {
366 pool_destroy(pp);
367 return (NULL);
368 }
369 }
370
371 return (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(pp, size, align, ioff, flags, wchan, pagesz, alloc, release, mtype)
382 struct pool *pp;
383 size_t size;
384 u_int align;
385 u_int ioff;
386 int flags;
387 const char *wchan;
388 size_t pagesz;
389 void *(*alloc) __P((unsigned long, int, int));
390 void (*release) __P((void *, unsigned long, int));
391 int mtype;
392 {
393 int off, slack, i;
394
395 #ifdef POOL_DIAGNOSTIC
396 /*
397 * Always log if POOL_DIAGNOSTIC is defined.
398 */
399 if (pool_logsize != 0)
400 flags |= PR_LOGGING;
401 #endif
402
403 /*
404 * Check arguments and construct default values.
405 */
406 if (!powerof2(pagesz) || pagesz > PAGE_SIZE)
407 panic("pool_init: page size invalid (%lx)\n", (u_long)pagesz);
408
409 if (alloc == NULL && release == NULL) {
410 alloc = pool_page_alloc;
411 release = pool_page_free;
412 pagesz = PAGE_SIZE; /* Rounds to PAGE_SIZE anyhow. */
413 } else if ((alloc != NULL && release != NULL) == 0) {
414 /* If you specifiy one, must specify both. */
415 panic("pool_init: must specify alloc and release together");
416 }
417
418 if (pagesz == 0)
419 pagesz = PAGE_SIZE;
420
421 if (align == 0)
422 align = ALIGN(1);
423
424 if (size < sizeof(struct pool_item))
425 size = sizeof(struct pool_item);
426
427 /*
428 * Initialize the pool structure.
429 */
430 TAILQ_INIT(&pp->pr_pagelist);
431 pp->pr_curpage = NULL;
432 pp->pr_npages = 0;
433 pp->pr_minitems = 0;
434 pp->pr_minpages = 0;
435 pp->pr_maxpages = UINT_MAX;
436 pp->pr_roflags = flags;
437 pp->pr_flags = 0;
438 pp->pr_size = ALIGN(size);
439 pp->pr_align = align;
440 pp->pr_wchan = wchan;
441 pp->pr_mtype = mtype;
442 pp->pr_alloc = alloc;
443 pp->pr_free = release;
444 pp->pr_pagesz = pagesz;
445 pp->pr_pagemask = ~(pagesz - 1);
446 pp->pr_pageshift = ffs(pagesz) - 1;
447 pp->pr_nitems = 0;
448 pp->pr_nout = 0;
449 pp->pr_hardlimit = UINT_MAX;
450 pp->pr_hardlimit_warning = NULL;
451 pp->pr_hardlimit_ratecap = 0;
452 memset(&pp->pr_hardlimit_warning_last, 0,
453 sizeof(pp->pr_hardlimit_warning_last));
454
455 /*
456 * Decide whether to put the page header off page to avoid
457 * wasting too large a part of the page. Off-page page headers
458 * go on a hash table, so we can match a returned item
459 * with its header based on the page address.
460 * We use 1/16 of the page size as the threshold (XXX: tune)
461 */
462 if (pp->pr_size < pagesz/16) {
463 /* Use the end of the page for the page header */
464 pp->pr_roflags |= PR_PHINPAGE;
465 pp->pr_phoffset = off =
466 pagesz - ALIGN(sizeof(struct pool_item_header));
467 } else {
468 /* The page header will be taken from our page header pool */
469 pp->pr_phoffset = 0;
470 off = pagesz;
471 for (i = 0; i < PR_HASHTABSIZE; i++) {
472 LIST_INIT(&pp->pr_hashtab[i]);
473 }
474 }
475
476 /*
477 * Alignment is to take place at `ioff' within the item. This means
478 * we must reserve up to `align - 1' bytes on the page to allow
479 * appropriate positioning of each item.
480 *
481 * Silently enforce `0 <= ioff < align'.
482 */
483 pp->pr_itemoffset = ioff = ioff % align;
484 pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
485
486 /*
487 * Use the slack between the chunks and the page header
488 * for "cache coloring".
489 */
490 slack = off - pp->pr_itemsperpage * pp->pr_size;
491 pp->pr_maxcolor = (slack / align) * align;
492 pp->pr_curcolor = 0;
493
494 pp->pr_nget = 0;
495 pp->pr_nfail = 0;
496 pp->pr_nput = 0;
497 pp->pr_npagealloc = 0;
498 pp->pr_npagefree = 0;
499 pp->pr_hiwat = 0;
500 pp->pr_nidle = 0;
501
502 if (flags & PR_LOGGING) {
503 if (kmem_map == NULL ||
504 (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
505 M_TEMP, M_NOWAIT)) == NULL)
506 pp->pr_roflags &= ~PR_LOGGING;
507 pp->pr_curlogentry = 0;
508 pp->pr_logsize = pool_logsize;
509 }
510
511 pp->pr_entered_file = NULL;
512 pp->pr_entered_line = 0;
513
514 simple_lock_init(&pp->pr_slock);
515
516 /*
517 * Initialize private page header pool if we haven't done so yet.
518 * XXX LOCKING.
519 */
520 if (phpool.pr_size == 0) {
521 pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
522 0, "phpool", 0, 0, 0, 0);
523 }
524
525 /* Insert into the list of all pools. */
526 simple_lock(&pool_head_slock);
527 TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
528 simple_unlock(&pool_head_slock);
529 }
530
531 /*
532 * De-commision a pool resource.
533 */
534 void
535 pool_destroy(pp)
536 struct pool *pp;
537 {
538 struct pool_item_header *ph;
539
540 #ifdef DIAGNOSTIC
541 if (pp->pr_nout != 0) {
542 pr_printlog(pp, NULL, printf);
543 panic("pool_destroy: pool busy: still out: %u\n",
544 pp->pr_nout);
545 }
546 #endif
547
548 /* Remove all pages */
549 if ((pp->pr_roflags & PR_STATIC) == 0)
550 while ((ph = pp->pr_pagelist.tqh_first) != NULL)
551 pr_rmpage(pp, ph);
552
553 /* Remove from global pool list */
554 simple_lock(&pool_head_slock);
555 TAILQ_REMOVE(&pool_head, pp, pr_poollist);
556 /* XXX Only clear this if we were drainpp? */
557 drainpp = NULL;
558 simple_unlock(&pool_head_slock);
559
560 if ((pp->pr_roflags & PR_LOGGING) != 0)
561 free(pp->pr_log, M_TEMP);
562
563 if (pp->pr_roflags & PR_FREEHEADER)
564 free(pp, M_POOL);
565 }
566
567
568 /*
569 * Grab an item from the pool; must be called at appropriate spl level
570 */
571 void *
572 _pool_get(pp, flags, file, line)
573 struct pool *pp;
574 int flags;
575 const char *file;
576 long line;
577 {
578 void *v;
579 struct pool_item *pi;
580 struct pool_item_header *ph;
581
582 #ifdef DIAGNOSTIC
583 if ((pp->pr_roflags & PR_STATIC) && (flags & PR_MALLOCOK)) {
584 pr_printlog(pp, NULL, printf);
585 panic("pool_get: static");
586 }
587 #endif
588
589 if (curproc == NULL && (flags & PR_WAITOK) != 0)
590 panic("pool_get: must have NOWAIT");
591
592 simple_lock(&pp->pr_slock);
593 pr_enter(pp, file, line);
594
595 startover:
596 /*
597 * Check to see if we've reached the hard limit. If we have,
598 * and we can wait, then wait until an item has been returned to
599 * the pool.
600 */
601 #ifdef DIAGNOSTIC
602 if (pp->pr_nout > pp->pr_hardlimit) {
603 pr_leave(pp);
604 simple_unlock(&pp->pr_slock);
605 panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
606 }
607 #endif
608 if (pp->pr_nout == pp->pr_hardlimit) {
609 if (flags & PR_WAITOK) {
610 /*
611 * XXX: A warning isn't logged in this case. Should
612 * it be?
613 */
614 pp->pr_flags |= PR_WANTED;
615 pr_leave(pp);
616 simple_unlock(&pp->pr_slock);
617 tsleep((caddr_t)pp, PSWP, pp->pr_wchan, 0);
618 simple_lock(&pp->pr_slock);
619 pr_enter(pp, file, line);
620 goto startover;
621 }
622 if (pp->pr_hardlimit_warning != NULL) {
623 /*
624 * Log a message that the hard limit has been hit.
625 */
626 struct timeval curtime, logdiff;
627 int s = splclock();
628 curtime = mono_time;
629 splx(s);
630 timersub(&curtime, &pp->pr_hardlimit_warning_last,
631 &logdiff);
632 if (logdiff.tv_sec >= pp->pr_hardlimit_ratecap) {
633 pp->pr_hardlimit_warning_last = curtime;
634 log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
635 }
636 }
637
638 if (flags & PR_URGENT)
639 panic("pool_get: urgent");
640
641 pp->pr_nfail++;
642
643 pr_leave(pp);
644 simple_unlock(&pp->pr_slock);
645 return (NULL);
646 }
647
648 /*
649 * The convention we use is that if `curpage' is not NULL, then
650 * it points at a non-empty bucket. In particular, `curpage'
651 * never points at a page header which has PR_PHINPAGE set and
652 * has no items in its bucket.
653 */
654 if ((ph = pp->pr_curpage) == NULL) {
655 void *v;
656
657 #ifdef DIAGNOSTIC
658 if (pp->pr_nitems != 0) {
659 simple_unlock(&pp->pr_slock);
660 printf("pool_get: %s: curpage NULL, nitems %u\n",
661 pp->pr_wchan, pp->pr_nitems);
662 panic("pool_get: nitems inconsistent\n");
663 }
664 #endif
665
666 /*
667 * Call the back-end page allocator for more memory.
668 * Release the pool lock, as the back-end page allocator
669 * may block.
670 */
671 pr_leave(pp);
672 simple_unlock(&pp->pr_slock);
673 v = (*pp->pr_alloc)(pp->pr_pagesz, flags, pp->pr_mtype);
674 simple_lock(&pp->pr_slock);
675 pr_enter(pp, file, line);
676
677 if (v == NULL) {
678 /*
679 * We were unable to allocate a page, but
680 * we released the lock during allocation,
681 * so perhaps items were freed back to the
682 * pool. Check for this case.
683 */
684 if (pp->pr_curpage != NULL)
685 goto startover;
686
687 if (flags & PR_URGENT)
688 panic("pool_get: urgent");
689
690 if ((flags & PR_WAITOK) == 0) {
691 pp->pr_nfail++;
692 pr_leave(pp);
693 simple_unlock(&pp->pr_slock);
694 return (NULL);
695 }
696
697 /*
698 * Wait for items to be returned to this pool.
699 *
700 * XXX: we actually want to wait just until
701 * the page allocator has memory again. Depending
702 * on this pool's usage, we might get stuck here
703 * for a long time.
704 *
705 * XXX: maybe we should wake up once a second and
706 * try again?
707 */
708 pp->pr_flags |= PR_WANTED;
709 pr_leave(pp);
710 simple_unlock(&pp->pr_slock);
711 tsleep((caddr_t)pp, PSWP, pp->pr_wchan, 0);
712 simple_lock(&pp->pr_slock);
713 pr_enter(pp, file, line);
714 goto startover;
715 }
716
717 /* We have more memory; add it to the pool */
718 pp->pr_npagealloc++;
719 pool_prime_page(pp, v);
720
721 /* Start the allocation process over. */
722 goto startover;
723 }
724
725 if ((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL) {
726 pr_leave(pp);
727 simple_unlock(&pp->pr_slock);
728 panic("pool_get: %s: page empty", pp->pr_wchan);
729 }
730 #ifdef DIAGNOSTIC
731 if (pp->pr_nitems == 0) {
732 pr_leave(pp);
733 simple_unlock(&pp->pr_slock);
734 printf("pool_get: %s: items on itemlist, nitems %u\n",
735 pp->pr_wchan, pp->pr_nitems);
736 panic("pool_get: nitems inconsistent\n");
737 }
738 #endif
739 pr_log(pp, v, PRLOG_GET, file, line);
740
741 #ifdef DIAGNOSTIC
742 if (pi->pi_magic != PI_MAGIC) {
743 pr_printlog(pp, pi, printf);
744 panic("pool_get(%s): free list modified: magic=%x; page %p;"
745 " item addr %p\n",
746 pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
747 }
748 #endif
749
750 /*
751 * Remove from item list.
752 */
753 TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
754 pp->pr_nitems--;
755 pp->pr_nout++;
756 if (ph->ph_nmissing == 0) {
757 #ifdef DIAGNOSTIC
758 if (pp->pr_nidle == 0)
759 panic("pool_get: nidle inconsistent");
760 #endif
761 pp->pr_nidle--;
762 }
763 ph->ph_nmissing++;
764 if (TAILQ_FIRST(&ph->ph_itemlist) == NULL) {
765 #ifdef DIAGNOSTIC
766 if (ph->ph_nmissing != pp->pr_itemsperpage) {
767 pr_leave(pp);
768 simple_unlock(&pp->pr_slock);
769 panic("pool_get: %s: nmissing inconsistent",
770 pp->pr_wchan);
771 }
772 #endif
773 /*
774 * Find a new non-empty page header, if any.
775 * Start search from the page head, to increase
776 * the chance for "high water" pages to be freed.
777 *
778 * Migrate empty pages to the end of the list. This
779 * will speed the update of curpage as pages become
780 * idle. Empty pages intermingled with idle pages
781 * is no big deal. As soon as a page becomes un-empty,
782 * it will move back to the head of the list.
783 */
784 TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
785 TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
786 for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
787 ph = TAILQ_NEXT(ph, ph_pagelist))
788 if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
789 break;
790
791 pp->pr_curpage = ph;
792 }
793
794 pp->pr_nget++;
795
796 /*
797 * If we have a low water mark and we are now below that low
798 * water mark, add more items to the pool.
799 */
800 if (pp->pr_nitems < pp->pr_minitems && pool_catchup(pp) != 0) {
801 /*
802 * XXX: Should we log a warning? Should we set up a timeout
803 * to try again in a second or so? The latter could break
804 * a caller's assumptions about interrupt protection, etc.
805 */
806 }
807
808 pr_leave(pp);
809 simple_unlock(&pp->pr_slock);
810 return (v);
811 }
812
813 /*
814 * Return resource to the pool; must be called at appropriate spl level
815 */
816 void
817 _pool_put(pp, v, file, line)
818 struct pool *pp;
819 void *v;
820 const char *file;
821 long line;
822 {
823 struct pool_item *pi = v;
824 struct pool_item_header *ph;
825 caddr_t page;
826 int s;
827
828 page = (caddr_t)((u_long)v & pp->pr_pagemask);
829
830 simple_lock(&pp->pr_slock);
831 pr_enter(pp, file, line);
832
833 pr_log(pp, v, PRLOG_PUT, file, line);
834
835 if ((ph = pr_find_pagehead(pp, page)) == NULL) {
836 pr_printlog(pp, NULL, printf);
837 panic("pool_put: %s: page header missing", pp->pr_wchan);
838 }
839
840 /*
841 * Return to item list.
842 */
843 #ifdef DIAGNOSTIC
844 /* XXX Should fill the item. */
845 pi->pi_magic = PI_MAGIC;
846 #endif
847 TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
848 ph->ph_nmissing--;
849 pp->pr_nput++;
850 pp->pr_nitems++;
851 pp->pr_nout--;
852
853 /* Cancel "pool empty" condition if it exists */
854 if (pp->pr_curpage == NULL)
855 pp->pr_curpage = ph;
856
857 if (pp->pr_flags & PR_WANTED) {
858 pp->pr_flags &= ~PR_WANTED;
859 if (ph->ph_nmissing == 0)
860 pp->pr_nidle++;
861 pr_leave(pp);
862 simple_unlock(&pp->pr_slock);
863 wakeup((caddr_t)pp);
864 return;
865 }
866
867 /*
868 * If this page is now complete, do one of two things:
869 *
870 * (1) If we have more pages than the page high water
871 * mark, free the page back to the system.
872 *
873 * (2) Move it to the end of the page list, so that
874 * we minimize our chances of fragmenting the
875 * pool. Idle pages migrate to the end (along with
876 * completely empty pages, so that we find un-empty
877 * pages more quickly when we update curpage) of the
878 * list so they can be more easily swept up by
879 * the pagedaemon when pages are scarce.
880 */
881 if (ph->ph_nmissing == 0) {
882 pp->pr_nidle++;
883 if (pp->pr_npages > pp->pr_maxpages) {
884 pr_rmpage(pp, ph);
885 } else {
886 TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
887 TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
888
889 /*
890 * Update the timestamp on the page. A page must
891 * be idle for some period of time before it can
892 * be reclaimed by the pagedaemon. This minimizes
893 * ping-pong'ing for memory.
894 */
895 s = splclock();
896 ph->ph_time = mono_time;
897 splx(s);
898
899 /*
900 * Update the current page pointer. Just look for
901 * the first page with any free items.
902 *
903 * XXX: Maybe we want an option to look for the
904 * page with the fewest available items, to minimize
905 * fragmentation?
906 */
907 for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
908 ph = TAILQ_NEXT(ph, ph_pagelist))
909 if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
910 break;
911
912 pp->pr_curpage = ph;
913 }
914 }
915 /*
916 * If the page has just become un-empty, move it to the head of
917 * the list, and make it the current page. The next allocation
918 * will get the item from this page, instead of further fragmenting
919 * the pool.
920 */
921 else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
922 TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
923 TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
924 pp->pr_curpage = ph;
925 }
926
927 pr_leave(pp);
928 simple_unlock(&pp->pr_slock);
929
930 }
931
932 /*
933 * Add N items to the pool.
934 */
935 int
936 pool_prime(pp, n, storage)
937 struct pool *pp;
938 int n;
939 caddr_t storage;
940 {
941 caddr_t cp;
942 int newnitems, newpages;
943
944 #ifdef DIAGNOSTIC
945 if (storage && !(pp->pr_roflags & PR_STATIC))
946 panic("pool_prime: static");
947 /* !storage && static caught below */
948 #endif
949
950 simple_lock(&pp->pr_slock);
951
952 newnitems = pp->pr_minitems + n;
953 newpages =
954 roundup(newnitems, pp->pr_itemsperpage) / pp->pr_itemsperpage
955 - pp->pr_minpages;
956
957 while (newpages-- > 0) {
958 if (pp->pr_roflags & PR_STATIC) {
959 cp = storage;
960 storage += pp->pr_pagesz;
961 } else {
962 simple_unlock(&pp->pr_slock);
963 cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
964 simple_lock(&pp->pr_slock);
965 }
966
967 if (cp == NULL) {
968 simple_unlock(&pp->pr_slock);
969 return (ENOMEM);
970 }
971
972 pp->pr_npagealloc++;
973 pool_prime_page(pp, cp);
974 pp->pr_minpages++;
975 }
976
977 pp->pr_minitems = newnitems;
978
979 if (pp->pr_minpages >= pp->pr_maxpages)
980 pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */
981
982 simple_unlock(&pp->pr_slock);
983 return (0);
984 }
985
986 /*
987 * Add a page worth of items to the pool.
988 *
989 * Note, we must be called with the pool descriptor LOCKED.
990 */
991 static void
992 pool_prime_page(pp, storage)
993 struct pool *pp;
994 caddr_t storage;
995 {
996 struct pool_item *pi;
997 struct pool_item_header *ph;
998 caddr_t cp = storage;
999 unsigned int align = pp->pr_align;
1000 unsigned int ioff = pp->pr_itemoffset;
1001 int s, n;
1002
1003 if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
1004 ph = (struct pool_item_header *)(cp + pp->pr_phoffset);
1005 } else {
1006 s = splhigh();
1007 ph = pool_get(&phpool, PR_URGENT);
1008 splx(s);
1009 LIST_INSERT_HEAD(&pp->pr_hashtab[PR_HASH_INDEX(pp, cp)],
1010 ph, ph_hashlist);
1011 }
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 * Like pool_prime(), except this is used by pool_get() when nitems
1066 * drops below the low water mark. This is used to catch up nitmes
1067 * with the low water mark.
1068 *
1069 * Note 1, we never wait for memory here, we let the caller decide what to do.
1070 *
1071 * Note 2, this doesn't work with static pools.
1072 *
1073 * Note 3, we must be called with the pool already locked, and we return
1074 * with it locked.
1075 */
1076 static int
1077 pool_catchup(pp)
1078 struct pool *pp;
1079 {
1080 caddr_t cp;
1081 int error = 0;
1082
1083 if (pp->pr_roflags & PR_STATIC) {
1084 /*
1085 * We dropped below the low water mark, and this is not a
1086 * good thing. Log a warning.
1087 *
1088 * XXX: rate-limit this?
1089 */
1090 printf("WARNING: static pool `%s' dropped below low water "
1091 "mark\n", pp->pr_wchan);
1092 return (0);
1093 }
1094
1095 while (pp->pr_nitems < pp->pr_minitems) {
1096 /*
1097 * Call the page back-end allocator for more memory.
1098 *
1099 * XXX: We never wait, so should we bother unlocking
1100 * the pool descriptor?
1101 */
1102 simple_unlock(&pp->pr_slock);
1103 cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
1104 simple_lock(&pp->pr_slock);
1105 if (cp == NULL) {
1106 error = ENOMEM;
1107 break;
1108 }
1109 pp->pr_npagealloc++;
1110 pool_prime_page(pp, cp);
1111 }
1112
1113 return (error);
1114 }
1115
1116 void
1117 pool_setlowat(pp, n)
1118 pool_handle_t pp;
1119 int n;
1120 {
1121 int error;
1122
1123 simple_lock(&pp->pr_slock);
1124
1125 pp->pr_minitems = n;
1126 pp->pr_minpages = (n == 0)
1127 ? 0
1128 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1129
1130 /* Make sure we're caught up with the newly-set low water mark. */
1131 if ((error = pool_catchup(pp)) != 0) {
1132 /*
1133 * XXX: Should we log a warning? Should we set up a timeout
1134 * to try again in a second or so? The latter could break
1135 * a caller's assumptions about interrupt protection, etc.
1136 */
1137 }
1138
1139 simple_unlock(&pp->pr_slock);
1140 }
1141
1142 void
1143 pool_sethiwat(pp, n)
1144 pool_handle_t pp;
1145 int n;
1146 {
1147
1148 simple_lock(&pp->pr_slock);
1149
1150 pp->pr_maxpages = (n == 0)
1151 ? 0
1152 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1153
1154 simple_unlock(&pp->pr_slock);
1155 }
1156
1157 void
1158 pool_sethardlimit(pp, n, warnmess, ratecap)
1159 pool_handle_t pp;
1160 int n;
1161 const char *warnmess;
1162 int ratecap;
1163 {
1164
1165 simple_lock(&pp->pr_slock);
1166
1167 pp->pr_hardlimit = n;
1168 pp->pr_hardlimit_warning = warnmess;
1169 pp->pr_hardlimit_ratecap = ratecap;
1170 memset(&pp->pr_hardlimit_warning_last, 0,
1171 sizeof(pp->pr_hardlimit_warning_last));
1172
1173 /*
1174 * In-line version of pool_sethiwat(), because we don't want to
1175 * release the lock.
1176 */
1177 pp->pr_maxpages = (n == 0)
1178 ? 0
1179 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1180
1181 simple_unlock(&pp->pr_slock);
1182 }
1183
1184 /*
1185 * Default page allocator.
1186 */
1187 static void *
1188 pool_page_alloc(sz, flags, mtype)
1189 unsigned long sz;
1190 int flags;
1191 int mtype;
1192 {
1193 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1194
1195 return ((void *)uvm_km_alloc_poolpage(waitok));
1196 }
1197
1198 static void
1199 pool_page_free(v, sz, mtype)
1200 void *v;
1201 unsigned long sz;
1202 int mtype;
1203 {
1204
1205 uvm_km_free_poolpage((vaddr_t)v);
1206 }
1207
1208 /*
1209 * Alternate pool page allocator for pools that know they will
1210 * never be accessed in interrupt context.
1211 */
1212 void *
1213 pool_page_alloc_nointr(sz, flags, mtype)
1214 unsigned long sz;
1215 int flags;
1216 int mtype;
1217 {
1218 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1219
1220 return ((void *)uvm_km_alloc_poolpage1(kernel_map, uvm.kernel_object,
1221 waitok));
1222 }
1223
1224 void
1225 pool_page_free_nointr(v, sz, mtype)
1226 void *v;
1227 unsigned long sz;
1228 int mtype;
1229 {
1230
1231 uvm_km_free_poolpage1(kernel_map, (vaddr_t)v);
1232 }
1233
1234
1235 /*
1236 * Release all complete pages that have not been used recently.
1237 */
1238 void
1239 _pool_reclaim(pp, file, line)
1240 pool_handle_t pp;
1241 const char *file;
1242 long line;
1243 {
1244 struct pool_item_header *ph, *phnext;
1245 struct timeval curtime;
1246 int s;
1247
1248 if (pp->pr_roflags & PR_STATIC)
1249 return;
1250
1251 if (simple_lock_try(&pp->pr_slock) == 0)
1252 return;
1253 pr_enter(pp, file, line);
1254
1255 s = splclock();
1256 curtime = mono_time;
1257 splx(s);
1258
1259 for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL; ph = phnext) {
1260 phnext = TAILQ_NEXT(ph, ph_pagelist);
1261
1262 /* Check our minimum page claim */
1263 if (pp->pr_npages <= pp->pr_minpages)
1264 break;
1265
1266 if (ph->ph_nmissing == 0) {
1267 struct timeval diff;
1268 timersub(&curtime, &ph->ph_time, &diff);
1269 if (diff.tv_sec < pool_inactive_time)
1270 continue;
1271
1272 /*
1273 * If freeing this page would put us below
1274 * the low water mark, stop now.
1275 */
1276 if ((pp->pr_nitems - pp->pr_itemsperpage) <
1277 pp->pr_minitems)
1278 break;
1279
1280 pr_rmpage(pp, ph);
1281 }
1282 }
1283
1284 pr_leave(pp);
1285 simple_unlock(&pp->pr_slock);
1286 }
1287
1288
1289 /*
1290 * Drain pools, one at a time.
1291 *
1292 * Note, we must never be called from an interrupt context.
1293 */
1294 void
1295 pool_drain(arg)
1296 void *arg;
1297 {
1298 struct pool *pp;
1299 int s;
1300
1301 s = splimp();
1302 simple_lock(&pool_head_slock);
1303
1304 if (drainpp == NULL && (drainpp = TAILQ_FIRST(&pool_head)) == NULL)
1305 goto out;
1306
1307 pp = drainpp;
1308 drainpp = TAILQ_NEXT(pp, pr_poollist);
1309
1310 pool_reclaim(pp);
1311
1312 out:
1313 simple_unlock(&pool_head_slock);
1314 splx(s);
1315 }
1316
1317
1318 /*
1319 * Diagnostic helpers.
1320 */
1321 void
1322 pool_print(pp, modif)
1323 struct pool *pp;
1324 const char *modif;
1325 {
1326 int s;
1327
1328 s = splimp();
1329 if (simple_lock_try(&pp->pr_slock) == 0) {
1330 printf("pool %s is locked; try again later\n",
1331 pp->pr_wchan);
1332 splx(s);
1333 return;
1334 }
1335 pool_print1(pp, modif, printf);
1336 simple_unlock(&pp->pr_slock);
1337 splx(s);
1338 }
1339
1340 void
1341 pool_printit(pp, modif, pr)
1342 struct pool *pp;
1343 const char *modif;
1344 void (*pr) __P((const char *, ...));
1345 {
1346 int didlock = 0;
1347
1348 if (pp == NULL) {
1349 (*pr)("Must specify a pool to print.\n");
1350 return;
1351 }
1352
1353 /*
1354 * Called from DDB; interrupts should be blocked, and all
1355 * other processors should be paused. We can skip locking
1356 * the pool in this case.
1357 *
1358 * We do a simple_lock_try() just to print the lock
1359 * status, however.
1360 */
1361
1362 if (simple_lock_try(&pp->pr_slock) == 0)
1363 (*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
1364 else
1365 didlock = 1;
1366
1367 pool_print1(pp, modif, pr);
1368
1369 if (didlock)
1370 simple_unlock(&pp->pr_slock);
1371 }
1372
1373 static void
1374 pool_print1(pp, modif, pr)
1375 struct pool *pp;
1376 const char *modif;
1377 void (*pr) __P((const char *, ...));
1378 {
1379 struct pool_item_header *ph;
1380 #ifdef DIAGNOSTIC
1381 struct pool_item *pi;
1382 #endif
1383 int print_log = 0, print_pagelist = 0;
1384 char c;
1385
1386 while ((c = *modif++) != '\0') {
1387 if (c == 'l')
1388 print_log = 1;
1389 if (c == 'p')
1390 print_pagelist = 1;
1391 modif++;
1392 }
1393
1394 (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1395 pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1396 pp->pr_roflags);
1397 (*pr)("\tpagesz %u, mtype %d\n", pp->pr_pagesz, pp->pr_mtype);
1398 (*pr)("\talloc %p, release %p\n", pp->pr_alloc, pp->pr_free);
1399 (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1400 pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1401 (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1402 pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1403
1404 (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
1405 pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1406 (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1407 pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1408
1409 if (print_pagelist == 0)
1410 goto skip_pagelist;
1411
1412 if ((ph = TAILQ_FIRST(&pp->pr_pagelist)) != NULL)
1413 (*pr)("\n\tpage list:\n");
1414 for (; ph != NULL; ph = TAILQ_NEXT(ph, ph_pagelist)) {
1415 (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
1416 ph->ph_page, ph->ph_nmissing,
1417 (u_long)ph->ph_time.tv_sec,
1418 (u_long)ph->ph_time.tv_usec);
1419 #ifdef DIAGNOSTIC
1420 for (pi = TAILQ_FIRST(&ph->ph_itemlist); pi != NULL;
1421 pi = TAILQ_NEXT(pi, pi_list)) {
1422 if (pi->pi_magic != PI_MAGIC) {
1423 (*pr)("\t\t\titem %p, magic 0x%x\n",
1424 pi, pi->pi_magic);
1425 }
1426 }
1427 #endif
1428 }
1429 if (pp->pr_curpage == NULL)
1430 (*pr)("\tno current page\n");
1431 else
1432 (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1433
1434 skip_pagelist:
1435
1436 if (print_log == 0)
1437 goto skip_log;
1438
1439 (*pr)("\n");
1440 if ((pp->pr_roflags & PR_LOGGING) == 0)
1441 (*pr)("\tno log\n");
1442 else
1443 pr_printlog(pp, NULL, pr);
1444
1445 skip_log:
1446
1447 pr_enter_check(pp, pr);
1448 }
1449
1450 int
1451 pool_chk(pp, label)
1452 struct pool *pp;
1453 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