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