subr_pool.c revision 1.6 1 /* $NetBSD: subr_pool.c,v 1.6 1998/08/01 23:44:21 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997 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.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/errno.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/lock.h>
46 #include <sys/pool.h>
47
48 #include <vm/vm.h>
49 #include <vm/vm_kern.h>
50
51 #if defined(UVM)
52 #include <uvm/uvm.h>
53 #endif
54
55 /*
56 * Pool resource management utility.
57 *
58 * Memory is allocated in pages which are split into pieces according
59 * to the pool item size. Each page is kept on a list headed by `pr_pagelist'
60 * in the pool structure and the individual pool items are on a linked list
61 * headed by `ph_itemlist' in each page header. The memory for building
62 * the page list is either taken from the allocated pages themselves (for
63 * small pool items) or taken from an internal pool of page headers (`phpool').
64 *
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 = NULL;
78
79 struct pool_item_header {
80 /* Page headers */
81 TAILQ_ENTRY(pool_item_header)
82 ph_pagelist; /* pool page list */
83 TAILQ_HEAD(,pool_item) ph_itemlist; /* chunk list for this page */
84 LIST_ENTRY(pool_item_header)
85 ph_hashlist; /* Off-page page headers */
86 int ph_nmissing; /* # of chunks in use */
87 caddr_t ph_page; /* this page's address */
88 struct timeval ph_time; /* last referenced */
89 };
90
91 struct pool_item {
92 #ifdef DIAGNOSTIC
93 int pi_magic;
94 #define PI_MAGIC 0xdeadbeef
95 #endif
96 /* Other entries use only this list entry */
97 TAILQ_ENTRY(pool_item) pi_list;
98 };
99
100
101 #define PR_HASH_INDEX(pp,addr) \
102 (((u_long)(addr) >> (pp)->pr_pageshift) & (PR_HASHTABSIZE - 1))
103
104
105
106 static struct pool_item_header
107 *pr_find_pagehead __P((struct pool *, caddr_t));
108 static void pr_rmpage __P((struct pool *, struct pool_item_header *));
109 static int pool_prime_page __P((struct pool *, caddr_t));
110 static void *pool_page_alloc __P((unsigned long, int, int));
111 static void pool_page_free __P((void *, unsigned long, int));
112 int pool_chk __P((struct pool *, char *));
113
114
115 #ifdef POOL_DIAGNOSTIC
116 /*
117 * Pool log entry. An array of these is allocated in pool_create().
118 */
119 struct pool_log {
120 const char *pl_file;
121 long pl_line;
122 int pl_action;
123 #define PRLOG_GET 1
124 #define PRLOG_PUT 2
125 void *pl_addr;
126 };
127
128 /* Number of entries in pool log buffers */
129 int pool_logsize = 10;
130
131 static void pr_log __P((struct pool *, void *, int, const char *, long));
132 static void pr_printlog __P((struct pool *));
133
134 static __inline__ void
135 pr_log(pp, v, action, file, line)
136 struct pool *pp;
137 void *v;
138 int action;
139 const char *file;
140 long line;
141 {
142 int n = pp->pr_curlogentry;
143 struct pool_log *pl;
144
145 if ((pp->pr_flags & PR_LOGGING) == 0)
146 return;
147
148 /*
149 * Fill in the current entry. Wrap around and overwrite
150 * the oldest entry if necessary.
151 */
152 pl = &pp->pr_log[n];
153 pl->pl_file = file;
154 pl->pl_line = line;
155 pl->pl_action = action;
156 pl->pl_addr = v;
157 if (++n >= pp->pr_logsize)
158 n = 0;
159 pp->pr_curlogentry = n;
160 }
161
162 static void
163 pr_printlog(pp)
164 struct pool *pp;
165 {
166 int i = pp->pr_logsize;
167 int n = pp->pr_curlogentry;
168
169 if ((pp->pr_flags & PR_LOGGING) == 0)
170 return;
171
172 pool_print(pp, "printlog");
173
174 /*
175 * Print all entries in this pool's log.
176 */
177 while (i-- > 0) {
178 struct pool_log *pl = &pp->pr_log[n];
179 if (pl->pl_action != 0) {
180 printf("log entry %d:\n", i);
181 printf("\taction = %s, addr = %p\n",
182 pl->pl_action == PRLOG_GET ? "get" : "put",
183 pl->pl_addr);
184 printf("\tfile: %s at line %lu\n",
185 pl->pl_file, pl->pl_line);
186 }
187 if (++n >= pp->pr_logsize)
188 n = 0;
189 }
190 }
191 #else
192 #define pr_log(pp, v, action, file, line)
193 #define pr_printlog(pp)
194 #endif
195
196
197 /*
198 * Return the pool page header based on page address.
199 */
200 static __inline__ struct pool_item_header *
201 pr_find_pagehead(pp, page)
202 struct pool *pp;
203 caddr_t page;
204 {
205 struct pool_item_header *ph;
206
207 if ((pp->pr_flags & PR_PHINPAGE) != 0)
208 return ((struct pool_item_header *)(page + pp->pr_phoffset));
209
210 for (ph = LIST_FIRST(&pp->pr_hashtab[PR_HASH_INDEX(pp, page)]);
211 ph != NULL;
212 ph = LIST_NEXT(ph, ph_hashlist)) {
213 if (ph->ph_page == page)
214 return (ph);
215 }
216 return (NULL);
217 }
218
219 /*
220 * Remove a page from the pool.
221 */
222 static __inline__ void
223 pr_rmpage(pp, ph)
224 struct pool *pp;
225 struct pool_item_header *ph;
226 {
227
228 /*
229 * Unlink a page from the pool and release it.
230 */
231 TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
232 (*pp->pr_free)(ph->ph_page, pp->pr_pagesz, pp->pr_mtype);
233 pp->pr_npages--;
234 pp->pr_npagefree++;
235
236 if (ph->ph_nmissing == 0) {
237 #ifdef DIAGNOSTIC
238 if (pp->pr_nidle == 0)
239 panic("pr_rmpage: nidle inconsistent");
240 #endif
241 pp->pr_nidle--;
242 }
243
244 if ((pp->pr_flags & PR_PHINPAGE) == 0) {
245 LIST_REMOVE(ph, ph_hashlist);
246 pool_put(&phpool, ph);
247 }
248
249 if (pp->pr_curpage == ph) {
250 /*
251 * Find a new non-empty page header, if any.
252 * Start search from the page head, to increase the
253 * chance for "high water" pages to be freed.
254 */
255 for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
256 ph = TAILQ_NEXT(ph, ph_pagelist))
257 if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
258 break;
259
260 pp->pr_curpage = ph;
261 }
262 }
263
264 /*
265 * Allocate and initialize a pool.
266 */
267 struct pool *
268 pool_create(size, align, ioff, nitems, wchan, pagesz, alloc, release, mtype)
269 size_t size;
270 u_int align;
271 u_int ioff;
272 int nitems;
273 char *wchan;
274 size_t pagesz;
275 void *(*alloc) __P((unsigned long, int, int));
276 void (*release) __P((void *, unsigned long, int));
277 int mtype;
278 {
279 struct pool *pp;
280 int flags;
281
282 pp = (struct pool *)malloc(sizeof(*pp), M_POOL, M_NOWAIT);
283 if (pp == NULL)
284 return (NULL);
285
286 flags = PR_FREEHEADER;
287 #ifdef POOL_DIAGNOSTIC
288 if (pool_logsize != 0)
289 flags |= PR_LOGGING;
290 #endif
291
292 pool_init(pp, size, align, ioff, flags, wchan, pagesz,
293 alloc, release, mtype);
294
295 if (nitems != 0) {
296 if (pool_prime(pp, nitems, NULL) != 0) {
297 pool_destroy(pp);
298 return (NULL);
299 }
300 }
301
302 return (pp);
303 }
304
305 /*
306 * Initialize the given pool resource structure.
307 *
308 * We export this routine to allow other kernel parts to declare
309 * static pools that must be initialized before malloc() is available.
310 */
311 void
312 pool_init(pp, size, align, ioff, flags, wchan, pagesz, alloc, release, mtype)
313 struct pool *pp;
314 size_t size;
315 u_int align;
316 u_int ioff;
317 int flags;
318 char *wchan;
319 size_t pagesz;
320 void *(*alloc) __P((unsigned long, int, int));
321 void (*release) __P((void *, unsigned long, int));
322 int mtype;
323 {
324 int off, slack;
325
326 /*
327 * Check arguments and construct default values.
328 */
329 if (!powerof2(pagesz) || pagesz > PAGE_SIZE)
330 panic("pool_init: page size invalid (%lx)\n", (u_long)pagesz);
331
332 if (alloc == NULL && release == NULL) {
333 alloc = pool_page_alloc;
334 release = pool_page_free;
335 pagesz = PAGE_SIZE; /* Rounds to PAGE_SIZE anyhow. */
336 } else if ((alloc != NULL && release != NULL) == 0) {
337 /* If you specifiy one, must specify both. */
338 panic("pool_init: must specify alloc and release together");
339 }
340
341 if (pagesz == 0)
342 pagesz = PAGE_SIZE;
343
344 if (align == 0)
345 align = ALIGN(1);
346
347 /*
348 * Initialize the pool structure.
349 */
350 TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
351 TAILQ_INIT(&pp->pr_pagelist);
352 pp->pr_curpage = NULL;
353 pp->pr_npages = 0;
354 pp->pr_minitems = 0;
355 pp->pr_minpages = 0;
356 pp->pr_maxpages = UINT_MAX;
357 pp->pr_flags = flags;
358 pp->pr_size = ALIGN(size);
359 pp->pr_align = align;
360 pp->pr_wchan = wchan;
361 pp->pr_mtype = mtype;
362 pp->pr_alloc = alloc;
363 pp->pr_free = release;
364 pp->pr_pagesz = pagesz;
365 pp->pr_pagemask = ~(pagesz - 1);
366 pp->pr_pageshift = ffs(pagesz) - 1;
367
368 /*
369 * Decide whether to put the page header off page to avoid
370 * wasting too large a part of the page. Off-page page headers
371 * go on a hash table, so we can match a returned item
372 * with its header based on the page address.
373 * We use 1/16 of the page size as the threshold (XXX: tune)
374 */
375 if (pp->pr_size < pagesz/16) {
376 /* Use the end of the page for the page header */
377 pp->pr_flags |= PR_PHINPAGE;
378 pp->pr_phoffset = off =
379 pagesz - ALIGN(sizeof(struct pool_item_header));
380 } else {
381 /* The page header will be taken from our page header pool */
382 pp->pr_phoffset = 0;
383 off = pagesz;
384 bzero(pp->pr_hashtab, sizeof(pp->pr_hashtab));
385 }
386
387 /*
388 * Alignment is to take place at `ioff' within the item. This means
389 * we must reserve up to `align - 1' bytes on the page to allow
390 * appropriate positioning of each item.
391 *
392 * Silently enforce `0 <= ioff < align'.
393 */
394 pp->pr_itemoffset = ioff = ioff % align;
395 pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
396
397 /*
398 * Use the slack between the chunks and the page header
399 * for "cache coloring".
400 */
401 slack = off - pp->pr_itemsperpage * pp->pr_size;
402 pp->pr_maxcolor = (slack / align) * align;
403 pp->pr_curcolor = 0;
404
405 pp->pr_nget = 0;
406 pp->pr_nfail = 0;
407 pp->pr_nput = 0;
408 pp->pr_npagealloc = 0;
409 pp->pr_npagefree = 0;
410 pp->pr_hiwat = 0;
411
412 #ifdef POOL_DIAGNOSTIC
413 if ((flags & PR_LOGGING) != 0) {
414 pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
415 M_TEMP, M_NOWAIT);
416 if (pp->pr_log == NULL)
417 pp->pr_flags &= ~PR_LOGGING;
418 pp->pr_curlogentry = 0;
419 pp->pr_logsize = pool_logsize;
420 }
421 #endif
422
423 simple_lock_init(&pp->pr_lock);
424
425 /*
426 * Initialize private page header pool if we haven't done so yet.
427 */
428 if (phpool.pr_size == 0) {
429 pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
430 0, "phpool", 0, 0, 0, 0);
431 }
432
433 return;
434 }
435
436 /*
437 * De-commision a pool resource.
438 */
439 void
440 pool_destroy(pp)
441 struct pool *pp;
442 {
443 struct pool_item_header *ph;
444
445 #ifdef DIAGNOSTIC
446 if (pp->pr_nget - pp->pr_nput != 0) {
447 pr_printlog(pp);
448 panic("pool_destroy: pool busy: still out: %lu\n",
449 pp->pr_nget - pp->pr_nput);
450 }
451 #endif
452
453 /* Remove all pages */
454 if ((pp->pr_flags & PR_STATIC) == 0)
455 while ((ph = pp->pr_pagelist.tqh_first) != NULL)
456 pr_rmpage(pp, ph);
457
458 /* Remove from global pool list */
459 TAILQ_REMOVE(&pool_head, pp, pr_poollist);
460 drainpp = NULL;
461
462 #ifdef POOL_DIAGNOSTIC
463 if ((pp->pr_flags & PR_LOGGING) != 0)
464 free(pp->pr_log, M_TEMP);
465 #endif
466
467 if (pp->pr_flags & PR_FREEHEADER)
468 free(pp, M_POOL);
469 }
470
471
472 /*
473 * Grab an item from the pool; must be called at appropriate spl level
474 */
475 #ifdef POOL_DIAGNOSTIC
476 void *
477 _pool_get(pp, flags, file, line)
478 struct pool *pp;
479 int flags;
480 const char *file;
481 long line;
482 #else
483 void *
484 pool_get(pp, flags)
485 struct pool *pp;
486 int flags;
487 #endif
488 {
489 void *v;
490 struct pool_item *pi;
491 struct pool_item_header *ph;
492
493 #ifdef DIAGNOSTIC
494 if ((pp->pr_flags & PR_STATIC) && (flags & PR_MALLOCOK)) {
495 pr_printlog(pp);
496 panic("pool_get: static");
497 }
498 #endif
499
500 simple_lock(&pp->pr_lock);
501 if (curproc == NULL && (flags & PR_WAITOK) != 0)
502 panic("pool_get: must have NOWAIT");
503
504 /*
505 * The convention we use is that if `curpage' is not NULL, then
506 * it points at a non-empty bucket. In particular, `curpage'
507 * never points at a page header which has PR_PHINPAGE set and
508 * has no items in its bucket.
509 */
510 again:
511 if ((ph = pp->pr_curpage) == NULL) {
512 void *v = (*pp->pr_alloc)(pp->pr_pagesz, flags, pp->pr_mtype);
513 if (v == NULL) {
514 if (flags & PR_URGENT)
515 panic("pool_get: urgent");
516 if ((flags & PR_WAITOK) == 0) {
517 pp->pr_nfail++;
518 simple_unlock(&pp->pr_lock);
519 return (NULL);
520 }
521
522 pp->pr_flags |= PR_WANTED;
523 simple_unlock(&pp->pr_lock);
524 tsleep((caddr_t)pp, PSWP, pp->pr_wchan, 0);
525 simple_lock(&pp->pr_lock);
526 } else {
527 pp->pr_npagealloc++;
528 pool_prime_page(pp, v);
529 }
530
531 goto again;
532 }
533
534 if ((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)
535 panic("pool_get: %s: page empty", pp->pr_wchan);
536
537 pr_log(pp, v, PRLOG_GET, file, line);
538
539 #ifdef DIAGNOSTIC
540 if (pi->pi_magic != PI_MAGIC) {
541 pr_printlog(pp);
542 panic("pool_get(%s): free list modified: magic=%x; page %p;"
543 " item addr %p\n",
544 pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
545 }
546 #endif
547
548 /*
549 * Remove from item list.
550 */
551 TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
552 if (ph->ph_nmissing == 0) {
553 #ifdef DIAGNOSTIC
554 if (pp->pr_nidle == 0)
555 panic("pool_get: nidle inconsistent");
556 #endif
557 pp->pr_nidle--;
558 }
559 ph->ph_nmissing++;
560 if (TAILQ_FIRST(&ph->ph_itemlist) == NULL) {
561 /*
562 * Find a new non-empty page header, if any.
563 * Start search from the page head, to increase
564 * the chance for "high water" pages to be freed.
565 *
566 * First, move the now empty page to the head of
567 * the page list.
568 */
569 TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
570 TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
571 while ((ph = TAILQ_NEXT(ph, ph_pagelist)) != NULL)
572 if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
573 break;
574
575 pp->pr_curpage = ph;
576 }
577
578 pp->pr_nget++;
579 simple_unlock(&pp->pr_lock);
580 return (v);
581 }
582
583 /*
584 * Return resource to the pool; must be called at appropriate spl level
585 */
586 #ifdef POOL_DIAGNOSTIC
587 void
588 _pool_put(pp, v, file, line)
589 struct pool *pp;
590 void *v;
591 const char *file;
592 long line;
593 #else
594 void
595 pool_put(pp, v)
596 struct pool *pp;
597 void *v;
598 #endif
599 {
600 struct pool_item *pi = v;
601 struct pool_item_header *ph;
602 caddr_t page;
603
604 page = (caddr_t)((u_long)v & pp->pr_pagemask);
605
606 simple_lock(&pp->pr_lock);
607
608 pr_log(pp, v, PRLOG_PUT, file, line);
609
610 if ((ph = pr_find_pagehead(pp, page)) == NULL) {
611 pr_printlog(pp);
612 panic("pool_put: %s: page header missing", pp->pr_wchan);
613 }
614
615 /*
616 * Return to item list.
617 */
618 #ifdef DIAGNOSTIC
619 pi->pi_magic = PI_MAGIC;
620 #endif
621 TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
622 ph->ph_nmissing--;
623 pp->pr_nput++;
624
625 /* Cancel "pool empty" condition if it exists */
626 if (pp->pr_curpage == NULL)
627 pp->pr_curpage = ph;
628
629 if (pp->pr_flags & PR_WANTED) {
630 pp->pr_flags &= ~PR_WANTED;
631 wakeup((caddr_t)pp);
632 simple_unlock(&pp->pr_lock);
633 return;
634 }
635
636 /*
637 * If this page is now complete, move it to the end of the pagelist.
638 * If this page has just become un-empty, move it the head.
639 */
640 if (ph->ph_nmissing == 0) {
641 pp->pr_nidle++;
642 if (pp->pr_npages > pp->pr_maxpages) {
643 #if 0
644 timeout(pool_drain, 0, pool_inactive_time*hz);
645 #else
646 pr_rmpage(pp, ph);
647 #endif
648 } else {
649 TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
650 TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
651 ph->ph_time = time;
652
653 /* XXX - update curpage */
654 for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
655 ph = TAILQ_NEXT(ph, ph_pagelist))
656 if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
657 break;
658
659 pp->pr_curpage = ph;
660 }
661 }
662
663 simple_unlock(&pp->pr_lock);
664 }
665
666 /*
667 * Add N items to the pool.
668 */
669 int
670 pool_prime(pp, n, storage)
671 struct pool *pp;
672 int n;
673 caddr_t storage;
674 {
675 caddr_t cp;
676 int newnitems, newpages;
677
678 #ifdef DIAGNOSTIC
679 if (storage && !(pp->pr_flags & PR_STATIC))
680 panic("pool_prime: static");
681 /* !storage && static caught below */
682 #endif
683
684 newnitems = pp->pr_minitems + n;
685 newpages =
686 roundup(pp->pr_itemsperpage,newnitems) / pp->pr_itemsperpage
687 - pp->pr_minpages;
688
689 simple_lock(&pp->pr_lock);
690 while (newpages-- > 0) {
691
692 if (pp->pr_flags & PR_STATIC) {
693 cp = storage;
694 storage += pp->pr_pagesz;
695 } else {
696 cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
697 }
698
699 if (cp == NULL) {
700 simple_unlock(&pp->pr_lock);
701 return (ENOMEM);
702 }
703
704 pool_prime_page(pp, cp);
705 pp->pr_minpages++;
706 }
707
708 pp->pr_minitems = newnitems;
709
710 if (pp->pr_minpages >= pp->pr_maxpages)
711 pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */
712
713 simple_unlock(&pp->pr_lock);
714 return (0);
715 }
716
717 /*
718 * Add a page worth of items to the pool.
719 */
720 int
721 pool_prime_page(pp, storage)
722 struct pool *pp;
723 caddr_t storage;
724 {
725 struct pool_item *pi;
726 struct pool_item_header *ph;
727 caddr_t cp = storage;
728 unsigned int align = pp->pr_align;
729 unsigned int ioff = pp->pr_itemoffset;
730 int n;
731
732 if ((pp->pr_flags & PR_PHINPAGE) != 0) {
733 ph = (struct pool_item_header *)(cp + pp->pr_phoffset);
734 } else {
735 ph = pool_get(&phpool, PR_URGENT);
736 LIST_INSERT_HEAD(&pp->pr_hashtab[PR_HASH_INDEX(pp, cp)],
737 ph, ph_hashlist);
738 }
739
740 /*
741 * Insert page header.
742 */
743 TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
744 TAILQ_INIT(&ph->ph_itemlist);
745 ph->ph_page = storage;
746 ph->ph_nmissing = 0;
747 ph->ph_time.tv_sec = ph->ph_time.tv_usec = 0;
748
749 pp->pr_nidle++;
750
751 /*
752 * Color this page.
753 */
754 cp = (caddr_t)(cp + pp->pr_curcolor);
755 if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
756 pp->pr_curcolor = 0;
757
758 /*
759 * Adjust storage to apply aligment to `pr_itemoffset' in each item.
760 */
761 if (ioff != 0)
762 cp = (caddr_t)(cp + (align - ioff));
763
764 /*
765 * Insert remaining chunks on the bucket list.
766 */
767 n = pp->pr_itemsperpage;
768
769 while (n--) {
770 pi = (struct pool_item *)cp;
771
772 /* Insert on page list */
773 TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
774 #ifdef DIAGNOSTIC
775 pi->pi_magic = PI_MAGIC;
776 #endif
777 cp = (caddr_t)(cp + pp->pr_size);
778 }
779
780 /*
781 * If the pool was depleted, point at the new page.
782 */
783 if (pp->pr_curpage == NULL)
784 pp->pr_curpage = ph;
785
786 if (++pp->pr_npages > pp->pr_hiwat)
787 pp->pr_hiwat = pp->pr_npages;
788
789 return (0);
790 }
791
792 void
793 pool_setlowat(pp, n)
794 pool_handle_t pp;
795 int n;
796 {
797 pp->pr_minitems = n;
798 if (n == 0) {
799 pp->pr_minpages = 0;
800 return;
801 }
802 pp->pr_minpages =
803 roundup(pp->pr_itemsperpage,n) / pp->pr_itemsperpage;
804 }
805
806 void
807 pool_sethiwat(pp, n)
808 pool_handle_t pp;
809 int n;
810 {
811 if (n == 0) {
812 pp->pr_maxpages = 0;
813 return;
814 }
815 pp->pr_maxpages =
816 roundup(pp->pr_itemsperpage,n) / pp->pr_itemsperpage;
817 }
818
819
820 /*
821 * Default page allocator.
822 */
823 static void *
824 pool_page_alloc(sz, flags, mtype)
825 unsigned long sz;
826 int flags;
827 int mtype;
828 {
829
830 #if defined(UVM)
831 return ((void *)uvm_km_alloc_poolpage());
832 #else
833 return ((void *)kmem_alloc_poolpage());
834 #endif
835 }
836
837 static void
838 pool_page_free(v, sz, mtype)
839 void *v;
840 unsigned long sz;
841 int mtype;
842 {
843
844 #if defined(UVM)
845 uvm_km_free_poolpage((vm_offset_t)v);
846 #else
847 kmem_free_poolpage((vm_offset_t)v);
848 #endif
849 }
850
851 /*
852 * Release all complete pages that have not been used recently.
853 */
854 void
855 pool_reclaim (pp)
856 pool_handle_t pp;
857 {
858 struct pool_item_header *ph, *phnext;
859 struct timeval curtime = time;
860
861 if (pp->pr_flags & PR_STATIC)
862 return;
863
864 if (simple_lock_try(&pp->pr_lock) == 0)
865 return;
866
867 for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL; ph = phnext) {
868 phnext = TAILQ_NEXT(ph, ph_pagelist);
869
870 /* Check our minimum page claim */
871 if (pp->pr_npages <= pp->pr_minpages)
872 break;
873
874 if (ph->ph_nmissing == 0) {
875 struct timeval diff;
876 timersub(&curtime, &ph->ph_time, &diff);
877 if (diff.tv_sec < pool_inactive_time)
878 continue;
879 pr_rmpage(pp, ph);
880 }
881 }
882
883 simple_unlock(&pp->pr_lock);
884 }
885
886
887 /*
888 * Drain pools, one at a time.
889 */
890 void
891 pool_drain(arg)
892 void *arg;
893 {
894 struct pool *pp;
895 int s = splimp();
896
897 /* XXX:lock pool head */
898 if (drainpp == NULL && (drainpp = TAILQ_FIRST(&pool_head)) == NULL) {
899 splx(s);
900 return;
901 }
902
903 pp = drainpp;
904 drainpp = TAILQ_NEXT(pp, pr_poollist);
905 /* XXX:unlock pool head */
906
907 pool_reclaim(pp);
908 splx(s);
909 }
910
911
912 #ifdef DEBUG
913 /*
914 * Diagnostic helpers.
915 */
916 void
917 pool_print(pp, label)
918 struct pool *pp;
919 char *label;
920 {
921
922 if (label != NULL)
923 printf("%s: ", label);
924
925 printf("pool %s: nalloc %lu nfree %lu npagealloc %lu npagefree %lu\n"
926 " npages %u minitems %u itemsperpage %u itemoffset %u\n"
927 " nidle %lu\n",
928 pp->pr_wchan,
929 pp->pr_nget,
930 pp->pr_nput,
931 pp->pr_npagealloc,
932 pp->pr_npagefree,
933 pp->pr_npages,
934 pp->pr_minitems,
935 pp->pr_itemsperpage,
936 pp->pr_itemoffset,
937 pp->pr_nidle);
938 }
939
940 int
941 pool_chk(pp, label)
942 struct pool *pp;
943 char *label;
944 {
945 struct pool_item_header *ph;
946 int r = 0;
947
948 simple_lock(&pp->pr_lock);
949
950 for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
951 ph = TAILQ_NEXT(ph, ph_pagelist)) {
952
953 struct pool_item *pi;
954 int n;
955 caddr_t page;
956
957 page = (caddr_t)((u_long)ph & pp->pr_pagemask);
958 if (page != ph->ph_page) {
959 if (label != NULL)
960 printf("%s: ", label);
961 printf("pool(%s): page inconsistency: page %p;"
962 " at page head addr %p (p %p)\n",
963 pp->pr_wchan, ph->ph_page,
964 ph, page);
965 r++;
966 goto out;
967 }
968
969 for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
970 pi != NULL;
971 pi = TAILQ_NEXT(pi,pi_list), n++) {
972
973 #ifdef DIAGNOSTIC
974 if (pi->pi_magic != PI_MAGIC) {
975 if (label != NULL)
976 printf("%s: ", label);
977 printf("pool(%s): free list modified: magic=%x;"
978 " page %p; item ordinal %d;"
979 " addr %p (p %p)\n",
980 pp->pr_wchan, pi->pi_magic, ph->ph_page,
981 n, pi, page);
982 panic("pool");
983 }
984 #endif
985 page = (caddr_t)((u_long)pi & pp->pr_pagemask);
986 if (page == ph->ph_page)
987 continue;
988
989 if (label != NULL)
990 printf("%s: ", label);
991 printf("pool(%s): page inconsistency: page %p;"
992 " item ordinal %d; addr %p (p %p)\n",
993 pp->pr_wchan, ph->ph_page,
994 n, pi, page);
995 r++;
996 goto out;
997 }
998 }
999 out:
1000 simple_unlock(&pp->pr_lock);
1001 return (r);
1002 }
1003 #endif
1004