Annotation of src/sys/kern/subr_pool.c, Revision 1.28
1.28 ! thorpej 1: /* $NetBSD: subr_pool.c,v 1.27 1999/06/06 22:20:15 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.25 thorpej 100: #define PI_MAGIC 0xdeadbeef
1.3 pk 101: #endif
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:
218: if (pp->pr_entered_file != NULL) {
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:
235: if (pp->pr_entered_file == NULL) {
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: */
407: if (!powerof2(pagesz) || pagesz > PAGE_SIZE)
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:
428: /*
429: * Initialize the pool structure.
430: */
431: TAILQ_INIT(&pp->pr_pagelist);
432: pp->pr_curpage = NULL;
433: pp->pr_npages = 0;
434: pp->pr_minitems = 0;
435: pp->pr_minpages = 0;
436: pp->pr_maxpages = UINT_MAX;
1.20 thorpej 437: pp->pr_roflags = flags;
438: pp->pr_flags = 0;
1.3 pk 439: pp->pr_size = ALIGN(size);
440: pp->pr_align = align;
441: pp->pr_wchan = wchan;
442: pp->pr_mtype = mtype;
443: pp->pr_alloc = alloc;
444: pp->pr_free = release;
445: pp->pr_pagesz = pagesz;
446: pp->pr_pagemask = ~(pagesz - 1);
447: pp->pr_pageshift = ffs(pagesz) - 1;
1.20 thorpej 448: pp->pr_nitems = 0;
449: pp->pr_nout = 0;
450: pp->pr_hardlimit = UINT_MAX;
451: pp->pr_hardlimit_warning = NULL;
452: pp->pr_hardlimit_ratecap = 0;
453: memset(&pp->pr_hardlimit_warning_last, 0,
454: sizeof(pp->pr_hardlimit_warning_last));
1.3 pk 455:
456: /*
457: * Decide whether to put the page header off page to avoid
458: * wasting too large a part of the page. Off-page page headers
459: * go on a hash table, so we can match a returned item
460: * with its header based on the page address.
461: * We use 1/16 of the page size as the threshold (XXX: tune)
462: */
463: if (pp->pr_size < pagesz/16) {
464: /* Use the end of the page for the page header */
1.20 thorpej 465: pp->pr_roflags |= PR_PHINPAGE;
1.3 pk 466: pp->pr_phoffset = off =
467: pagesz - ALIGN(sizeof(struct pool_item_header));
1.2 pk 468: } else {
1.3 pk 469: /* The page header will be taken from our page header pool */
470: pp->pr_phoffset = 0;
471: off = pagesz;
1.16 briggs 472: for (i = 0; i < PR_HASHTABSIZE; i++) {
473: LIST_INIT(&pp->pr_hashtab[i]);
474: }
1.2 pk 475: }
1.1 pk 476:
1.3 pk 477: /*
478: * Alignment is to take place at `ioff' within the item. This means
479: * we must reserve up to `align - 1' bytes on the page to allow
480: * appropriate positioning of each item.
481: *
482: * Silently enforce `0 <= ioff < align'.
483: */
484: pp->pr_itemoffset = ioff = ioff % align;
485: pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
486:
487: /*
488: * Use the slack between the chunks and the page header
489: * for "cache coloring".
490: */
491: slack = off - pp->pr_itemsperpage * pp->pr_size;
492: pp->pr_maxcolor = (slack / align) * align;
493: pp->pr_curcolor = 0;
494:
495: pp->pr_nget = 0;
496: pp->pr_nfail = 0;
497: pp->pr_nput = 0;
498: pp->pr_npagealloc = 0;
499: pp->pr_npagefree = 0;
1.1 pk 500: pp->pr_hiwat = 0;
1.8 thorpej 501: pp->pr_nidle = 0;
1.3 pk 502:
1.25 thorpej 503: if (flags & PR_LOGGING) {
504: if (kmem_map == NULL ||
505: (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
506: M_TEMP, M_NOWAIT)) == NULL)
1.20 thorpej 507: pp->pr_roflags &= ~PR_LOGGING;
1.3 pk 508: pp->pr_curlogentry = 0;
509: pp->pr_logsize = pool_logsize;
510: }
1.25 thorpej 511:
512: pp->pr_entered_file = NULL;
513: pp->pr_entered_line = 0;
1.3 pk 514:
1.21 thorpej 515: simple_lock_init(&pp->pr_slock);
1.1 pk 516:
1.3 pk 517: /*
518: * Initialize private page header pool if we haven't done so yet.
1.23 thorpej 519: * XXX LOCKING.
1.3 pk 520: */
521: if (phpool.pr_size == 0) {
522: pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
523: 0, "phpool", 0, 0, 0, 0);
1.1 pk 524: }
525:
1.23 thorpej 526: /* Insert into the list of all pools. */
527: simple_lock(&pool_head_slock);
528: TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
529: simple_unlock(&pool_head_slock);
1.1 pk 530: }
531:
532: /*
533: * De-commision a pool resource.
534: */
535: void
536: pool_destroy(pp)
537: struct pool *pp;
538: {
1.3 pk 539: struct pool_item_header *ph;
540:
541: #ifdef DIAGNOSTIC
1.20 thorpej 542: if (pp->pr_nout != 0) {
1.25 thorpej 543: pr_printlog(pp, NULL, printf);
1.20 thorpej 544: panic("pool_destroy: pool busy: still out: %u\n",
545: pp->pr_nout);
1.3 pk 546: }
547: #endif
1.1 pk 548:
1.3 pk 549: /* Remove all pages */
1.20 thorpej 550: if ((pp->pr_roflags & PR_STATIC) == 0)
1.3 pk 551: while ((ph = pp->pr_pagelist.tqh_first) != NULL)
552: pr_rmpage(pp, ph);
553:
554: /* Remove from global pool list */
1.23 thorpej 555: simple_lock(&pool_head_slock);
1.3 pk 556: TAILQ_REMOVE(&pool_head, pp, pr_poollist);
1.23 thorpej 557: /* XXX Only clear this if we were drainpp? */
1.3 pk 558: drainpp = NULL;
1.23 thorpej 559: simple_unlock(&pool_head_slock);
1.3 pk 560:
1.20 thorpej 561: if ((pp->pr_roflags & PR_LOGGING) != 0)
1.3 pk 562: free(pp->pr_log, M_TEMP);
1.2 pk 563:
1.20 thorpej 564: if (pp->pr_roflags & PR_FREEHEADER)
1.3 pk 565: free(pp, M_POOL);
1.1 pk 566: }
567:
568:
569: /*
1.3 pk 570: * Grab an item from the pool; must be called at appropriate spl level
1.1 pk 571: */
1.3 pk 572: void *
573: _pool_get(pp, flags, file, line)
574: struct pool *pp;
575: int flags;
576: const char *file;
577: long line;
1.1 pk 578: {
579: void *v;
580: struct pool_item *pi;
1.3 pk 581: struct pool_item_header *ph;
1.1 pk 582:
1.2 pk 583: #ifdef DIAGNOSTIC
1.20 thorpej 584: if ((pp->pr_roflags & PR_STATIC) && (flags & PR_MALLOCOK)) {
1.25 thorpej 585: pr_printlog(pp, NULL, printf);
1.2 pk 586: panic("pool_get: static");
1.3 pk 587: }
1.2 pk 588: #endif
589:
1.3 pk 590: if (curproc == NULL && (flags & PR_WAITOK) != 0)
591: panic("pool_get: must have NOWAIT");
1.1 pk 592:
1.21 thorpej 593: simple_lock(&pp->pr_slock);
1.25 thorpej 594: pr_enter(pp, file, line);
1.20 thorpej 595:
596: startover:
597: /*
598: * Check to see if we've reached the hard limit. If we have,
599: * and we can wait, then wait until an item has been returned to
600: * the pool.
601: */
602: #ifdef DIAGNOSTIC
603: if (pp->pr_nout > pp->pr_hardlimit) {
1.25 thorpej 604: pr_leave(pp);
1.21 thorpej 605: simple_unlock(&pp->pr_slock);
1.20 thorpej 606: panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
607: }
608: #endif
609: if (pp->pr_nout == pp->pr_hardlimit) {
610: if (flags & PR_WAITOK) {
611: /*
612: * XXX: A warning isn't logged in this case. Should
613: * it be?
614: */
615: pp->pr_flags |= PR_WANTED;
1.25 thorpej 616: pr_leave(pp);
1.21 thorpej 617: simple_unlock(&pp->pr_slock);
1.20 thorpej 618: tsleep((caddr_t)pp, PSWP, pp->pr_wchan, 0);
1.21 thorpej 619: simple_lock(&pp->pr_slock);
1.25 thorpej 620: pr_enter(pp, file, line);
1.20 thorpej 621: goto startover;
622: }
623: if (pp->pr_hardlimit_warning != NULL) {
624: /*
625: * Log a message that the hard limit has been hit.
626: */
627: struct timeval curtime, logdiff;
628: int s = splclock();
629: curtime = mono_time;
630: splx(s);
631: timersub(&curtime, &pp->pr_hardlimit_warning_last,
632: &logdiff);
633: if (logdiff.tv_sec >= pp->pr_hardlimit_ratecap) {
634: pp->pr_hardlimit_warning_last = curtime;
635: log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
636: }
637: }
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.21 thorpej 726: if ((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
732: if (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
743: if (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
759: if (pp->pr_nidle == 0)
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
767: if (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.3 pk 833:
834: pr_log(pp, v, PRLOG_PUT, file, line);
835:
836: if ((ph = pr_find_pagehead(pp, page)) == NULL) {
1.25 thorpej 837: pr_printlog(pp, NULL, printf);
1.3 pk 838: panic("pool_put: %s: page header missing", pp->pr_wchan);
839: }
1.28 ! thorpej 840:
! 841: #ifdef LOCKDEBUG
! 842: /*
! 843: * Check if we're freeing a locked simple lock.
! 844: */
! 845: simple_lock_freecheck((caddr_t)pi, ((caddr_t)pi) + pp->pr_size);
! 846: #endif
1.3 pk 847:
848: /*
849: * Return to item list.
850: */
1.2 pk 851: #ifdef DIAGNOSTIC
1.25 thorpej 852: /* XXX Should fill the item. */
1.3 pk 853: pi->pi_magic = PI_MAGIC;
854: #endif
855: TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
856: ph->ph_nmissing--;
857: pp->pr_nput++;
1.20 thorpej 858: pp->pr_nitems++;
859: pp->pr_nout--;
1.3 pk 860:
861: /* Cancel "pool empty" condition if it exists */
862: if (pp->pr_curpage == NULL)
863: pp->pr_curpage = ph;
864:
865: if (pp->pr_flags & PR_WANTED) {
866: pp->pr_flags &= ~PR_WANTED;
1.15 pk 867: if (ph->ph_nmissing == 0)
868: pp->pr_nidle++;
1.25 thorpej 869: pr_leave(pp);
1.21 thorpej 870: simple_unlock(&pp->pr_slock);
1.3 pk 871: wakeup((caddr_t)pp);
872: return;
873: }
874:
875: /*
1.21 thorpej 876: * If this page is now complete, do one of two things:
877: *
878: * (1) If we have more pages than the page high water
879: * mark, free the page back to the system.
880: *
881: * (2) Move it to the end of the page list, so that
882: * we minimize our chances of fragmenting the
883: * pool. Idle pages migrate to the end (along with
884: * completely empty pages, so that we find un-empty
885: * pages more quickly when we update curpage) of the
886: * list so they can be more easily swept up by
887: * the pagedaemon when pages are scarce.
1.3 pk 888: */
889: if (ph->ph_nmissing == 0) {
1.6 thorpej 890: pp->pr_nidle++;
1.3 pk 891: if (pp->pr_npages > pp->pr_maxpages) {
892: pr_rmpage(pp, ph);
893: } else {
894: TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
895: TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
896:
1.21 thorpej 897: /*
898: * Update the timestamp on the page. A page must
899: * be idle for some period of time before it can
900: * be reclaimed by the pagedaemon. This minimizes
901: * ping-pong'ing for memory.
902: */
903: s = splclock();
904: ph->ph_time = mono_time;
905: splx(s);
906:
907: /*
908: * Update the current page pointer. Just look for
909: * the first page with any free items.
910: *
911: * XXX: Maybe we want an option to look for the
912: * page with the fewest available items, to minimize
913: * fragmentation?
914: */
1.3 pk 915: for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
916: ph = TAILQ_NEXT(ph, ph_pagelist))
917: if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
918: break;
1.1 pk 919:
1.3 pk 920: pp->pr_curpage = ph;
1.1 pk 921: }
922: }
1.21 thorpej 923: /*
924: * If the page has just become un-empty, move it to the head of
925: * the list, and make it the current page. The next allocation
926: * will get the item from this page, instead of further fragmenting
927: * the pool.
928: */
929: else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
930: TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
931: TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
932: pp->pr_curpage = ph;
933: }
934:
1.25 thorpej 935: pr_leave(pp);
1.21 thorpej 936: simple_unlock(&pp->pr_slock);
1.3 pk 937:
1.1 pk 938: }
939:
940: /*
1.3 pk 941: * Add N items to the pool.
1.1 pk 942: */
943: int
1.2 pk 944: pool_prime(pp, n, storage)
1.1 pk 945: struct pool *pp;
946: int n;
1.2 pk 947: caddr_t storage;
1.1 pk 948: {
1.3 pk 949: caddr_t cp;
950: int newnitems, newpages;
1.2 pk 951:
952: #ifdef DIAGNOSTIC
1.20 thorpej 953: if (storage && !(pp->pr_roflags & PR_STATIC))
1.2 pk 954: panic("pool_prime: static");
955: /* !storage && static caught below */
956: #endif
1.1 pk 957:
1.21 thorpej 958: simple_lock(&pp->pr_slock);
959:
1.3 pk 960: newnitems = pp->pr_minitems + n;
961: newpages =
1.18 thorpej 962: roundup(newnitems, pp->pr_itemsperpage) / pp->pr_itemsperpage
1.3 pk 963: - pp->pr_minpages;
964:
965: while (newpages-- > 0) {
1.20 thorpej 966: if (pp->pr_roflags & PR_STATIC) {
1.3 pk 967: cp = storage;
968: storage += pp->pr_pagesz;
969: } else {
1.21 thorpej 970: simple_unlock(&pp->pr_slock);
1.3 pk 971: cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
1.21 thorpej 972: simple_lock(&pp->pr_slock);
1.3 pk 973: }
1.2 pk 974:
1.3 pk 975: if (cp == NULL) {
1.21 thorpej 976: simple_unlock(&pp->pr_slock);
1.1 pk 977: return (ENOMEM);
978: }
979:
1.26 thorpej 980: pp->pr_npagealloc++;
1.3 pk 981: pool_prime_page(pp, cp);
982: pp->pr_minpages++;
1.1 pk 983: }
1.3 pk 984:
985: pp->pr_minitems = newnitems;
986:
987: if (pp->pr_minpages >= pp->pr_maxpages)
988: pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */
989:
1.21 thorpej 990: simple_unlock(&pp->pr_slock);
1.1 pk 991: return (0);
992: }
1.3 pk 993:
994: /*
995: * Add a page worth of items to the pool.
1.21 thorpej 996: *
997: * Note, we must be called with the pool descriptor LOCKED.
1.3 pk 998: */
1.21 thorpej 999: static void
1.3 pk 1000: pool_prime_page(pp, storage)
1001: struct pool *pp;
1002: caddr_t storage;
1003: {
1004: struct pool_item *pi;
1005: struct pool_item_header *ph;
1006: caddr_t cp = storage;
1007: unsigned int align = pp->pr_align;
1008: unsigned int ioff = pp->pr_itemoffset;
1.27 pk 1009: int s, n;
1.3 pk 1010:
1.20 thorpej 1011: if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
1.3 pk 1012: ph = (struct pool_item_header *)(cp + pp->pr_phoffset);
1013: } else {
1.27 pk 1014: s = splhigh();
1.3 pk 1015: ph = pool_get(&phpool, PR_URGENT);
1.27 pk 1016: splx(s);
1.3 pk 1017: LIST_INSERT_HEAD(&pp->pr_hashtab[PR_HASH_INDEX(pp, cp)],
1018: ph, ph_hashlist);
1019: }
1020:
1021: /*
1022: * Insert page header.
1023: */
1024: TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
1025: TAILQ_INIT(&ph->ph_itemlist);
1026: ph->ph_page = storage;
1027: ph->ph_nmissing = 0;
1.21 thorpej 1028: memset(&ph->ph_time, 0, sizeof(ph->ph_time));
1.3 pk 1029:
1.6 thorpej 1030: pp->pr_nidle++;
1031:
1.3 pk 1032: /*
1033: * Color this page.
1034: */
1035: cp = (caddr_t)(cp + pp->pr_curcolor);
1036: if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
1037: pp->pr_curcolor = 0;
1038:
1039: /*
1040: * Adjust storage to apply aligment to `pr_itemoffset' in each item.
1041: */
1042: if (ioff != 0)
1043: cp = (caddr_t)(cp + (align - ioff));
1044:
1045: /*
1046: * Insert remaining chunks on the bucket list.
1047: */
1048: n = pp->pr_itemsperpage;
1.20 thorpej 1049: pp->pr_nitems += n;
1.3 pk 1050:
1051: while (n--) {
1052: pi = (struct pool_item *)cp;
1053:
1054: /* Insert on page list */
1055: TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
1056: #ifdef DIAGNOSTIC
1057: pi->pi_magic = PI_MAGIC;
1058: #endif
1059: cp = (caddr_t)(cp + pp->pr_size);
1060: }
1061:
1062: /*
1063: * If the pool was depleted, point at the new page.
1064: */
1065: if (pp->pr_curpage == NULL)
1066: pp->pr_curpage = ph;
1067:
1068: if (++pp->pr_npages > pp->pr_hiwat)
1069: pp->pr_hiwat = pp->pr_npages;
1070: }
1071:
1.20 thorpej 1072: /*
1073: * Like pool_prime(), except this is used by pool_get() when nitems
1074: * drops below the low water mark. This is used to catch up nitmes
1075: * with the low water mark.
1076: *
1.21 thorpej 1077: * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20 thorpej 1078: *
1079: * Note 2, this doesn't work with static pools.
1080: *
1081: * Note 3, we must be called with the pool already locked, and we return
1082: * with it locked.
1083: */
1084: static int
1085: pool_catchup(pp)
1086: struct pool *pp;
1087: {
1088: caddr_t cp;
1089: int error = 0;
1090:
1091: if (pp->pr_roflags & PR_STATIC) {
1092: /*
1093: * We dropped below the low water mark, and this is not a
1094: * good thing. Log a warning.
1.21 thorpej 1095: *
1096: * XXX: rate-limit this?
1.20 thorpej 1097: */
1098: printf("WARNING: static pool `%s' dropped below low water "
1099: "mark\n", pp->pr_wchan);
1100: return (0);
1101: }
1102:
1.21 thorpej 1103: while (pp->pr_nitems < pp->pr_minitems) {
1.20 thorpej 1104: /*
1.21 thorpej 1105: * Call the page back-end allocator for more memory.
1106: *
1107: * XXX: We never wait, so should we bother unlocking
1108: * the pool descriptor?
1.20 thorpej 1109: */
1.21 thorpej 1110: simple_unlock(&pp->pr_slock);
1.20 thorpej 1111: cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
1.21 thorpej 1112: simple_lock(&pp->pr_slock);
1.20 thorpej 1113: if (cp == NULL) {
1114: error = ENOMEM;
1115: break;
1116: }
1.26 thorpej 1117: pp->pr_npagealloc++;
1.20 thorpej 1118: pool_prime_page(pp, cp);
1119: }
1120:
1121: return (error);
1122: }
1123:
1.3 pk 1124: void
1125: pool_setlowat(pp, n)
1126: pool_handle_t pp;
1127: int n;
1128: {
1.20 thorpej 1129: int error;
1.15 pk 1130:
1.21 thorpej 1131: simple_lock(&pp->pr_slock);
1132:
1.3 pk 1133: pp->pr_minitems = n;
1.15 pk 1134: pp->pr_minpages = (n == 0)
1135: ? 0
1.18 thorpej 1136: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20 thorpej 1137:
1138: /* Make sure we're caught up with the newly-set low water mark. */
1.21 thorpej 1139: if ((error = pool_catchup(pp)) != 0) {
1.20 thorpej 1140: /*
1141: * XXX: Should we log a warning? Should we set up a timeout
1142: * to try again in a second or so? The latter could break
1143: * a caller's assumptions about interrupt protection, etc.
1144: */
1145: }
1.21 thorpej 1146:
1147: simple_unlock(&pp->pr_slock);
1.3 pk 1148: }
1149:
1150: void
1151: pool_sethiwat(pp, n)
1152: pool_handle_t pp;
1153: int n;
1154: {
1.15 pk 1155:
1.21 thorpej 1156: simple_lock(&pp->pr_slock);
1157:
1.15 pk 1158: pp->pr_maxpages = (n == 0)
1159: ? 0
1.18 thorpej 1160: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1161:
1162: simple_unlock(&pp->pr_slock);
1.3 pk 1163: }
1164:
1.20 thorpej 1165: void
1166: pool_sethardlimit(pp, n, warnmess, ratecap)
1167: pool_handle_t pp;
1168: int n;
1169: const char *warnmess;
1170: int ratecap;
1171: {
1172:
1.21 thorpej 1173: simple_lock(&pp->pr_slock);
1.20 thorpej 1174:
1175: pp->pr_hardlimit = n;
1176: pp->pr_hardlimit_warning = warnmess;
1177: pp->pr_hardlimit_ratecap = ratecap;
1178: memset(&pp->pr_hardlimit_warning_last, 0,
1179: sizeof(pp->pr_hardlimit_warning_last));
1180:
1181: /*
1.21 thorpej 1182: * In-line version of pool_sethiwat(), because we don't want to
1183: * release the lock.
1.20 thorpej 1184: */
1185: pp->pr_maxpages = (n == 0)
1186: ? 0
1187: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1188:
1189: simple_unlock(&pp->pr_slock);
1.20 thorpej 1190: }
1.3 pk 1191:
1192: /*
1193: * Default page allocator.
1194: */
1195: static void *
1196: pool_page_alloc(sz, flags, mtype)
1197: unsigned long sz;
1198: int flags;
1199: int mtype;
1200: {
1.11 thorpej 1201: boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1.3 pk 1202:
1.11 thorpej 1203: return ((void *)uvm_km_alloc_poolpage(waitok));
1.3 pk 1204: }
1205:
1206: static void
1207: pool_page_free(v, sz, mtype)
1208: void *v;
1209: unsigned long sz;
1210: int mtype;
1211: {
1212:
1.10 eeh 1213: uvm_km_free_poolpage((vaddr_t)v);
1.3 pk 1214: }
1.12 thorpej 1215:
1216: /*
1217: * Alternate pool page allocator for pools that know they will
1218: * never be accessed in interrupt context.
1219: */
1220: void *
1221: pool_page_alloc_nointr(sz, flags, mtype)
1222: unsigned long sz;
1223: int flags;
1224: int mtype;
1225: {
1226: boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1227:
1228: return ((void *)uvm_km_alloc_poolpage1(kernel_map, uvm.kernel_object,
1229: waitok));
1230: }
1231:
1232: void
1233: pool_page_free_nointr(v, sz, mtype)
1234: void *v;
1235: unsigned long sz;
1236: int mtype;
1237: {
1238:
1239: uvm_km_free_poolpage1(kernel_map, (vaddr_t)v);
1240: }
1241:
1.3 pk 1242:
1243: /*
1244: * Release all complete pages that have not been used recently.
1245: */
1246: void
1.25 thorpej 1247: _pool_reclaim(pp, file, line)
1.3 pk 1248: pool_handle_t pp;
1.25 thorpej 1249: const char *file;
1250: long line;
1.3 pk 1251: {
1252: struct pool_item_header *ph, *phnext;
1.21 thorpej 1253: struct timeval curtime;
1254: int s;
1.3 pk 1255:
1.20 thorpej 1256: if (pp->pr_roflags & PR_STATIC)
1.3 pk 1257: return;
1258:
1.21 thorpej 1259: if (simple_lock_try(&pp->pr_slock) == 0)
1.3 pk 1260: return;
1.25 thorpej 1261: pr_enter(pp, file, line);
1.3 pk 1262:
1.21 thorpej 1263: s = splclock();
1264: curtime = mono_time;
1265: splx(s);
1266:
1.3 pk 1267: for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL; ph = phnext) {
1268: phnext = TAILQ_NEXT(ph, ph_pagelist);
1269:
1270: /* Check our minimum page claim */
1271: if (pp->pr_npages <= pp->pr_minpages)
1272: break;
1273:
1274: if (ph->ph_nmissing == 0) {
1275: struct timeval diff;
1276: timersub(&curtime, &ph->ph_time, &diff);
1277: if (diff.tv_sec < pool_inactive_time)
1278: continue;
1.21 thorpej 1279:
1280: /*
1281: * If freeing this page would put us below
1282: * the low water mark, stop now.
1283: */
1284: if ((pp->pr_nitems - pp->pr_itemsperpage) <
1285: pp->pr_minitems)
1286: break;
1287:
1.3 pk 1288: pr_rmpage(pp, ph);
1289: }
1290: }
1291:
1.25 thorpej 1292: pr_leave(pp);
1.21 thorpej 1293: simple_unlock(&pp->pr_slock);
1.3 pk 1294: }
1295:
1296:
1297: /*
1298: * Drain pools, one at a time.
1.21 thorpej 1299: *
1300: * Note, we must never be called from an interrupt context.
1.3 pk 1301: */
1302: void
1303: pool_drain(arg)
1304: void *arg;
1305: {
1306: struct pool *pp;
1.23 thorpej 1307: int s;
1.3 pk 1308:
1.23 thorpej 1309: s = splimp();
1310: simple_lock(&pool_head_slock);
1311:
1312: if (drainpp == NULL && (drainpp = TAILQ_FIRST(&pool_head)) == NULL)
1313: goto out;
1.3 pk 1314:
1315: pp = drainpp;
1316: drainpp = TAILQ_NEXT(pp, pr_poollist);
1317:
1318: pool_reclaim(pp);
1.23 thorpej 1319:
1320: out:
1321: simple_unlock(&pool_head_slock);
1.3 pk 1322: splx(s);
1323: }
1324:
1325:
1326: /*
1327: * Diagnostic helpers.
1328: */
1329: void
1.25 thorpej 1330: pool_print(pp, modif)
1.3 pk 1331: struct pool *pp;
1.25 thorpej 1332: const char *modif;
1.21 thorpej 1333: {
1334: int s;
1335:
1336: s = splimp();
1.25 thorpej 1337: if (simple_lock_try(&pp->pr_slock) == 0) {
1338: printf("pool %s is locked; try again later\n",
1339: pp->pr_wchan);
1340: splx(s);
1341: return;
1342: }
1343: pool_print1(pp, modif, printf);
1.21 thorpej 1344: simple_unlock(&pp->pr_slock);
1345: splx(s);
1346: }
1347:
1.25 thorpej 1348: void
1349: pool_printit(pp, modif, pr)
1350: struct pool *pp;
1351: const char *modif;
1352: void (*pr) __P((const char *, ...));
1353: {
1354: int didlock = 0;
1355:
1356: if (pp == NULL) {
1357: (*pr)("Must specify a pool to print.\n");
1358: return;
1359: }
1360:
1361: /*
1362: * Called from DDB; interrupts should be blocked, and all
1363: * other processors should be paused. We can skip locking
1364: * the pool in this case.
1365: *
1366: * We do a simple_lock_try() just to print the lock
1367: * status, however.
1368: */
1369:
1370: if (simple_lock_try(&pp->pr_slock) == 0)
1371: (*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
1372: else
1373: didlock = 1;
1374:
1375: pool_print1(pp, modif, pr);
1376:
1377: if (didlock)
1378: simple_unlock(&pp->pr_slock);
1379: }
1380:
1.21 thorpej 1381: static void
1.25 thorpej 1382: pool_print1(pp, modif, pr)
1.21 thorpej 1383: struct pool *pp;
1.25 thorpej 1384: const char *modif;
1385: void (*pr) __P((const char *, ...));
1.3 pk 1386: {
1.25 thorpej 1387: struct pool_item_header *ph;
1388: #ifdef DIAGNOSTIC
1389: struct pool_item *pi;
1390: #endif
1391: int print_log = 0, print_pagelist = 0;
1392: char c;
1393:
1394: while ((c = *modif++) != '\0') {
1395: if (c == 'l')
1396: print_log = 1;
1397: if (c == 'p')
1398: print_pagelist = 1;
1399: modif++;
1400: }
1401:
1402: (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1403: pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1404: pp->pr_roflags);
1405: (*pr)("\tpagesz %u, mtype %d\n", pp->pr_pagesz, pp->pr_mtype);
1406: (*pr)("\talloc %p, release %p\n", pp->pr_alloc, pp->pr_free);
1407: (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1408: pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1409: (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1410: pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1411:
1412: (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
1413: pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1414: (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1415: pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1416:
1417: if (print_pagelist == 0)
1418: goto skip_pagelist;
1419:
1420: if ((ph = TAILQ_FIRST(&pp->pr_pagelist)) != NULL)
1421: (*pr)("\n\tpage list:\n");
1422: for (; ph != NULL; ph = TAILQ_NEXT(ph, ph_pagelist)) {
1423: (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
1424: ph->ph_page, ph->ph_nmissing,
1425: (u_long)ph->ph_time.tv_sec,
1426: (u_long)ph->ph_time.tv_usec);
1427: #ifdef DIAGNOSTIC
1428: for (pi = TAILQ_FIRST(&ph->ph_itemlist); pi != NULL;
1429: pi = TAILQ_NEXT(pi, pi_list)) {
1430: if (pi->pi_magic != PI_MAGIC) {
1431: (*pr)("\t\t\titem %p, magic 0x%x\n",
1432: pi, pi->pi_magic);
1433: }
1434: }
1435: #endif
1436: }
1437: if (pp->pr_curpage == NULL)
1438: (*pr)("\tno current page\n");
1439: else
1440: (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1441:
1442: skip_pagelist:
1443:
1444: if (print_log == 0)
1445: goto skip_log;
1446:
1447: (*pr)("\n");
1448: if ((pp->pr_roflags & PR_LOGGING) == 0)
1449: (*pr)("\tno log\n");
1450: else
1451: pr_printlog(pp, NULL, pr);
1.3 pk 1452:
1.25 thorpej 1453: skip_log:
1.3 pk 1454:
1.25 thorpej 1455: pr_enter_check(pp, pr);
1.3 pk 1456: }
1457:
1458: int
1459: pool_chk(pp, label)
1460: struct pool *pp;
1461: char *label;
1462: {
1463: struct pool_item_header *ph;
1464: int r = 0;
1465:
1.21 thorpej 1466: simple_lock(&pp->pr_slock);
1.3 pk 1467:
1468: for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
1469: ph = TAILQ_NEXT(ph, ph_pagelist)) {
1470:
1471: struct pool_item *pi;
1472: int n;
1473: caddr_t page;
1474:
1475: page = (caddr_t)((u_long)ph & pp->pr_pagemask);
1.20 thorpej 1476: if (page != ph->ph_page &&
1477: (pp->pr_roflags & PR_PHINPAGE) != 0) {
1.3 pk 1478: if (label != NULL)
1479: printf("%s: ", label);
1.16 briggs 1480: printf("pool(%p:%s): page inconsistency: page %p;"
1481: " at page head addr %p (p %p)\n", pp,
1.3 pk 1482: pp->pr_wchan, ph->ph_page,
1483: ph, page);
1484: r++;
1485: goto out;
1486: }
1487:
1488: for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
1489: pi != NULL;
1490: pi = TAILQ_NEXT(pi,pi_list), n++) {
1491:
1492: #ifdef DIAGNOSTIC
1493: if (pi->pi_magic != PI_MAGIC) {
1494: if (label != NULL)
1495: printf("%s: ", label);
1496: printf("pool(%s): free list modified: magic=%x;"
1497: " page %p; item ordinal %d;"
1498: " addr %p (p %p)\n",
1499: pp->pr_wchan, pi->pi_magic, ph->ph_page,
1500: n, pi, page);
1501: panic("pool");
1502: }
1503: #endif
1504: page = (caddr_t)((u_long)pi & pp->pr_pagemask);
1505: if (page == ph->ph_page)
1506: continue;
1507:
1508: if (label != NULL)
1509: printf("%s: ", label);
1.16 briggs 1510: printf("pool(%p:%s): page inconsistency: page %p;"
1511: " item ordinal %d; addr %p (p %p)\n", pp,
1.3 pk 1512: pp->pr_wchan, ph->ph_page,
1513: n, pi, page);
1514: r++;
1515: goto out;
1516: }
1517: }
1518: out:
1.21 thorpej 1519: simple_unlock(&pp->pr_slock);
1.3 pk 1520: return (r);
1521: }
CVSweb <webmaster@jp.NetBSD.org>