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