Annotation of src/sys/kern/subr_pool.c, Revision 1.60.4.2
1.60.4.2! fvdl 1: /* $NetBSD: subr_pool.c,v 1.60.4.1 2001/10/01 12:46:55 fvdl Exp $ */
1.1 pk 2:
3: /*-
1.43 thorpej 4: * Copyright (c) 1997, 1999, 2000 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.3 pk 53:
54: #include <uvm/uvm.h>
55:
1.1 pk 56: /*
57: * Pool resource management utility.
1.3 pk 58: *
59: * Memory is allocated in pages which are split into pieces according
60: * to the pool item size. Each page is kept on a list headed by `pr_pagelist'
61: * in the pool structure and the individual pool items are on a linked list
62: * headed by `ph_itemlist' in each page header. The memory for building
63: * the page list is either taken from the allocated pages themselves (for
64: * small pool items) or taken from an internal pool of page headers (`phpool').
1.1 pk 65: */
66:
1.3 pk 67: /* List of all pools */
1.5 thorpej 68: TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
1.3 pk 69:
70: /* Private pool for page header structures */
71: static struct pool phpool;
72:
1.60.4.2! fvdl 73: #ifdef POOL_SUBPAGE
! 74: /* Pool of subpages for use by normal pools. */
! 75: static struct pool psppool;
! 76: #endif
! 77:
1.3 pk 78: /* # of seconds to retain page after last use */
79: int pool_inactive_time = 10;
80:
81: /* Next candidate for drainage (see pool_drain()) */
1.23 thorpej 82: static struct pool *drainpp;
83:
84: /* This spin lock protects both pool_head and drainpp. */
85: struct simplelock pool_head_slock = SIMPLELOCK_INITIALIZER;
1.3 pk 86:
87: struct pool_item_header {
88: /* Page headers */
89: TAILQ_ENTRY(pool_item_header)
90: ph_pagelist; /* pool page list */
91: TAILQ_HEAD(,pool_item) ph_itemlist; /* chunk list for this page */
92: LIST_ENTRY(pool_item_header)
93: ph_hashlist; /* Off-page page headers */
94: int ph_nmissing; /* # of chunks in use */
95: caddr_t ph_page; /* this page's address */
96: struct timeval ph_time; /* last referenced */
97: };
1.60.4.1 fvdl 98: TAILQ_HEAD(pool_pagelist,pool_item_header);
1.3 pk 99:
1.1 pk 100: struct pool_item {
1.3 pk 101: #ifdef DIAGNOSTIC
102: int pi_magic;
1.33 chs 103: #endif
1.25 thorpej 104: #define PI_MAGIC 0xdeadbeef
1.3 pk 105: /* Other entries use only this list entry */
106: TAILQ_ENTRY(pool_item) pi_list;
107: };
108:
1.25 thorpej 109: #define PR_HASH_INDEX(pp,addr) \
1.3 pk 110: (((u_long)(addr) >> (pp)->pr_pageshift) & (PR_HASHTABSIZE - 1))
111:
1.53 thorpej 112: #define POOL_NEEDS_CATCHUP(pp) \
113: ((pp)->pr_nitems < (pp)->pr_minitems)
114:
1.43 thorpej 115: /*
116: * Pool cache management.
117: *
118: * Pool caches provide a way for constructed objects to be cached by the
119: * pool subsystem. This can lead to performance improvements by avoiding
120: * needless object construction/destruction; it is deferred until absolutely
121: * necessary.
122: *
123: * Caches are grouped into cache groups. Each cache group references
124: * up to 16 constructed objects. When a cache allocates an object
125: * from the pool, it calls the object's constructor and places it into
126: * a cache group. When a cache group frees an object back to the pool,
127: * it first calls the object's destructor. This allows the object to
128: * persist in constructed form while freed to the cache.
129: *
130: * Multiple caches may exist for each pool. This allows a single
131: * object type to have multiple constructed forms. The pool references
132: * each cache, so that when a pool is drained by the pagedaemon, it can
133: * drain each individual cache as well. Each time a cache is drained,
134: * the most idle cache group is freed to the pool in its entirety.
135: *
136: * Pool caches are layed on top of pools. By layering them, we can avoid
137: * the complexity of cache management for pools which would not benefit
138: * from it.
139: */
140:
141: /* The cache group pool. */
142: static struct pool pcgpool;
143:
144: /* The pool cache group. */
145: #define PCG_NOBJECTS 16
146: struct pool_cache_group {
147: TAILQ_ENTRY(pool_cache_group)
148: pcg_list; /* link in the pool cache's group list */
149: u_int pcg_avail; /* # available objects */
150: /* pointers to the objects */
151: void *pcg_objects[PCG_NOBJECTS];
152: };
1.3 pk 153:
1.43 thorpej 154: static void pool_cache_reclaim(struct pool_cache *);
1.3 pk 155:
1.42 thorpej 156: static int pool_catchup(struct pool *);
1.55 thorpej 157: static void pool_prime_page(struct pool *, caddr_t,
158: struct pool_item_header *);
1.42 thorpej 159: static void *pool_page_alloc(unsigned long, int, int);
160: static void pool_page_free(void *, unsigned long, int);
1.60.4.2! fvdl 161: #ifdef POOL_SUBPAGE
! 162: static void *pool_subpage_alloc(unsigned long, int, int);
! 163: static void pool_subpage_free(void *, unsigned long, int);
! 164: #endif
1.3 pk 165:
1.42 thorpej 166: static void pool_print1(struct pool *, const char *,
167: void (*)(const char *, ...));
1.3 pk 168:
169: /*
1.52 thorpej 170: * Pool log entry. An array of these is allocated in pool_init().
1.3 pk 171: */
172: struct pool_log {
173: const char *pl_file;
174: long pl_line;
175: int pl_action;
1.25 thorpej 176: #define PRLOG_GET 1
177: #define PRLOG_PUT 2
1.3 pk 178: void *pl_addr;
1.1 pk 179: };
180:
1.3 pk 181: /* Number of entries in pool log buffers */
1.17 thorpej 182: #ifndef POOL_LOGSIZE
183: #define POOL_LOGSIZE 10
184: #endif
185:
186: int pool_logsize = POOL_LOGSIZE;
1.1 pk 187:
1.59 thorpej 188: #ifdef POOL_DIAGNOSTIC
1.42 thorpej 189: static __inline void
190: pr_log(struct pool *pp, void *v, int action, const char *file, long line)
1.3 pk 191: {
192: int n = pp->pr_curlogentry;
193: struct pool_log *pl;
194:
1.20 thorpej 195: if ((pp->pr_roflags & PR_LOGGING) == 0)
1.3 pk 196: return;
197:
198: /*
199: * Fill in the current entry. Wrap around and overwrite
200: * the oldest entry if necessary.
201: */
202: pl = &pp->pr_log[n];
203: pl->pl_file = file;
204: pl->pl_line = line;
205: pl->pl_action = action;
206: pl->pl_addr = v;
207: if (++n >= pp->pr_logsize)
208: n = 0;
209: pp->pr_curlogentry = n;
210: }
211:
212: static void
1.42 thorpej 213: pr_printlog(struct pool *pp, struct pool_item *pi,
214: void (*pr)(const char *, ...))
1.3 pk 215: {
216: int i = pp->pr_logsize;
217: int n = pp->pr_curlogentry;
218:
1.20 thorpej 219: if ((pp->pr_roflags & PR_LOGGING) == 0)
1.3 pk 220: return;
221:
222: /*
223: * Print all entries in this pool's log.
224: */
225: while (i-- > 0) {
226: struct pool_log *pl = &pp->pr_log[n];
227: if (pl->pl_action != 0) {
1.25 thorpej 228: if (pi == NULL || pi == pl->pl_addr) {
229: (*pr)("\tlog entry %d:\n", i);
230: (*pr)("\t\taction = %s, addr = %p\n",
231: pl->pl_action == PRLOG_GET ? "get" : "put",
232: pl->pl_addr);
233: (*pr)("\t\tfile: %s at line %lu\n",
234: pl->pl_file, pl->pl_line);
235: }
1.3 pk 236: }
237: if (++n >= pp->pr_logsize)
238: n = 0;
239: }
240: }
1.25 thorpej 241:
1.42 thorpej 242: static __inline void
243: pr_enter(struct pool *pp, const char *file, long line)
1.25 thorpej 244: {
245:
1.34 thorpej 246: if (__predict_false(pp->pr_entered_file != NULL)) {
1.25 thorpej 247: printf("pool %s: reentrancy at file %s line %ld\n",
248: pp->pr_wchan, file, line);
249: printf(" previous entry at file %s line %ld\n",
250: pp->pr_entered_file, pp->pr_entered_line);
251: panic("pr_enter");
252: }
253:
254: pp->pr_entered_file = file;
255: pp->pr_entered_line = line;
256: }
257:
1.42 thorpej 258: static __inline void
259: pr_leave(struct pool *pp)
1.25 thorpej 260: {
261:
1.34 thorpej 262: if (__predict_false(pp->pr_entered_file == NULL)) {
1.25 thorpej 263: printf("pool %s not entered?\n", pp->pr_wchan);
264: panic("pr_leave");
265: }
266:
267: pp->pr_entered_file = NULL;
268: pp->pr_entered_line = 0;
269: }
270:
1.42 thorpej 271: static __inline void
272: pr_enter_check(struct pool *pp, void (*pr)(const char *, ...))
1.25 thorpej 273: {
274:
275: if (pp->pr_entered_file != NULL)
276: (*pr)("\n\tcurrently entered from file %s line %ld\n",
277: pp->pr_entered_file, pp->pr_entered_line);
278: }
1.3 pk 279: #else
1.25 thorpej 280: #define pr_log(pp, v, action, file, line)
281: #define pr_printlog(pp, pi, pr)
282: #define pr_enter(pp, file, line)
283: #define pr_leave(pp)
284: #define pr_enter_check(pp, pr)
1.59 thorpej 285: #endif /* POOL_DIAGNOSTIC */
1.3 pk 286:
287: /*
288: * Return the pool page header based on page address.
289: */
1.42 thorpej 290: static __inline struct pool_item_header *
291: pr_find_pagehead(struct pool *pp, caddr_t page)
1.3 pk 292: {
293: struct pool_item_header *ph;
294:
1.20 thorpej 295: if ((pp->pr_roflags & PR_PHINPAGE) != 0)
1.3 pk 296: return ((struct pool_item_header *)(page + pp->pr_phoffset));
297:
298: for (ph = LIST_FIRST(&pp->pr_hashtab[PR_HASH_INDEX(pp, page)]);
299: ph != NULL;
300: ph = LIST_NEXT(ph, ph_hashlist)) {
301: if (ph->ph_page == page)
302: return (ph);
303: }
304: return (NULL);
305: }
306:
307: /*
308: * Remove a page from the pool.
309: */
1.42 thorpej 310: static __inline void
1.60.4.1 fvdl 311: pr_rmpage(struct pool *pp, struct pool_item_header *ph,
312: struct pool_pagelist *pq)
1.3 pk 313: {
1.60.4.1 fvdl 314: int s;
1.3 pk 315:
316: /*
1.7 thorpej 317: * If the page was idle, decrement the idle page count.
1.3 pk 318: */
1.6 thorpej 319: if (ph->ph_nmissing == 0) {
320: #ifdef DIAGNOSTIC
321: if (pp->pr_nidle == 0)
322: panic("pr_rmpage: nidle inconsistent");
1.20 thorpej 323: if (pp->pr_nitems < pp->pr_itemsperpage)
324: panic("pr_rmpage: nitems inconsistent");
1.6 thorpej 325: #endif
326: pp->pr_nidle--;
327: }
1.7 thorpej 328:
1.20 thorpej 329: pp->pr_nitems -= pp->pr_itemsperpage;
330:
1.7 thorpej 331: /*
1.60.4.1 fvdl 332: * Unlink a page from the pool and release it (or queue it for release).
1.7 thorpej 333: */
334: TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
1.60.4.1 fvdl 335: if (pq) {
336: TAILQ_INSERT_HEAD(pq, ph, ph_pagelist);
337: } else {
338: (*pp->pr_free)(ph->ph_page, pp->pr_pagesz, pp->pr_mtype);
339: if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
340: LIST_REMOVE(ph, ph_hashlist);
341: s = splhigh();
342: pool_put(&phpool, ph);
343: splx(s);
344: }
345: }
1.7 thorpej 346: pp->pr_npages--;
347: pp->pr_npagefree++;
1.6 thorpej 348:
1.3 pk 349: if (pp->pr_curpage == ph) {
350: /*
351: * Find a new non-empty page header, if any.
352: * Start search from the page head, to increase the
353: * chance for "high water" pages to be freed.
354: */
1.60.4.1 fvdl 355: TAILQ_FOREACH(ph, &pp->pr_pagelist, ph_pagelist)
1.3 pk 356: if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
357: break;
358:
359: pp->pr_curpage = ph;
1.21 thorpej 360: }
1.3 pk 361: }
362:
363: /*
364: * Initialize the given pool resource structure.
365: *
366: * We export this routine to allow other kernel parts to declare
367: * static pools that must be initialized before malloc() is available.
368: */
369: void
1.42 thorpej 370: pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
371: const char *wchan, size_t pagesz,
372: void *(*alloc)(unsigned long, int, int),
373: void (*release)(void *, unsigned long, int),
374: int mtype)
1.3 pk 375: {
1.16 briggs 376: int off, slack, i;
1.3 pk 377:
1.25 thorpej 378: #ifdef POOL_DIAGNOSTIC
379: /*
380: * Always log if POOL_DIAGNOSTIC is defined.
381: */
382: if (pool_logsize != 0)
383: flags |= PR_LOGGING;
384: #endif
385:
1.3 pk 386: /*
387: * Check arguments and construct default values.
388: */
1.36 pk 389: if (!powerof2(pagesz))
1.3 pk 390: panic("pool_init: page size invalid (%lx)\n", (u_long)pagesz);
391:
1.4 thorpej 392: if (alloc == NULL && release == NULL) {
1.60.4.2! fvdl 393: #ifdef POOL_SUBPAGE
! 394: alloc = pool_subpage_alloc;
! 395: release = pool_subpage_free;
! 396: pagesz = POOL_SUBPAGE;
! 397: #else
1.3 pk 398: alloc = pool_page_alloc;
399: release = pool_page_free;
1.4 thorpej 400: pagesz = PAGE_SIZE; /* Rounds to PAGE_SIZE anyhow. */
1.60.4.2! fvdl 401: #endif
1.4 thorpej 402: } else if ((alloc != NULL && release != NULL) == 0) {
403: /* If you specifiy one, must specify both. */
404: panic("pool_init: must specify alloc and release together");
405: }
1.60.4.2! fvdl 406: #ifdef POOL_SUBPAGE
! 407: else if (alloc == pool_page_alloc_nointr &&
! 408: release == pool_page_free_nointr)
! 409: pagesz = POOL_SUBPAGE;
! 410: #endif
1.4 thorpej 411:
1.3 pk 412: if (pagesz == 0)
413: pagesz = PAGE_SIZE;
414:
415: if (align == 0)
416: align = ALIGN(1);
1.14 thorpej 417:
418: if (size < sizeof(struct pool_item))
419: size = sizeof(struct pool_item);
1.3 pk 420:
1.35 pk 421: size = ALIGN(size);
1.43 thorpej 422: if (size > pagesz)
1.35 pk 423: panic("pool_init: pool item size (%lu) too large",
424: (u_long)size);
425:
1.3 pk 426: /*
427: * Initialize the pool structure.
428: */
429: TAILQ_INIT(&pp->pr_pagelist);
1.43 thorpej 430: TAILQ_INIT(&pp->pr_cachelist);
1.3 pk 431: pp->pr_curpage = NULL;
432: pp->pr_npages = 0;
433: pp->pr_minitems = 0;
434: pp->pr_minpages = 0;
435: pp->pr_maxpages = UINT_MAX;
1.20 thorpej 436: pp->pr_roflags = flags;
437: pp->pr_flags = 0;
1.35 pk 438: pp->pr_size = size;
1.3 pk 439: pp->pr_align = align;
440: pp->pr_wchan = wchan;
441: pp->pr_mtype = mtype;
442: pp->pr_alloc = alloc;
443: pp->pr_free = release;
444: pp->pr_pagesz = pagesz;
445: pp->pr_pagemask = ~(pagesz - 1);
446: pp->pr_pageshift = ffs(pagesz) - 1;
1.20 thorpej 447: pp->pr_nitems = 0;
448: pp->pr_nout = 0;
449: pp->pr_hardlimit = UINT_MAX;
450: pp->pr_hardlimit_warning = NULL;
1.31 thorpej 451: pp->pr_hardlimit_ratecap.tv_sec = 0;
452: pp->pr_hardlimit_ratecap.tv_usec = 0;
453: pp->pr_hardlimit_warning_last.tv_sec = 0;
454: pp->pr_hardlimit_warning_last.tv_usec = 0;
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;
1.43 thorpej 486: KASSERT(pp->pr_itemsperpage != 0);
1.3 pk 487:
488: /*
489: * Use the slack between the chunks and the page header
490: * for "cache coloring".
491: */
492: slack = off - pp->pr_itemsperpage * pp->pr_size;
493: pp->pr_maxcolor = (slack / align) * align;
494: pp->pr_curcolor = 0;
495:
496: pp->pr_nget = 0;
497: pp->pr_nfail = 0;
498: pp->pr_nput = 0;
499: pp->pr_npagealloc = 0;
500: pp->pr_npagefree = 0;
1.1 pk 501: pp->pr_hiwat = 0;
1.8 thorpej 502: pp->pr_nidle = 0;
1.3 pk 503:
1.59 thorpej 504: #ifdef POOL_DIAGNOSTIC
1.25 thorpej 505: if (flags & PR_LOGGING) {
506: if (kmem_map == NULL ||
507: (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
508: M_TEMP, M_NOWAIT)) == NULL)
1.20 thorpej 509: pp->pr_roflags &= ~PR_LOGGING;
1.3 pk 510: pp->pr_curlogentry = 0;
511: pp->pr_logsize = pool_logsize;
512: }
1.59 thorpej 513: #endif
1.25 thorpej 514:
515: pp->pr_entered_file = NULL;
516: pp->pr_entered_line = 0;
1.3 pk 517:
1.21 thorpej 518: simple_lock_init(&pp->pr_slock);
1.1 pk 519:
1.3 pk 520: /*
1.43 thorpej 521: * Initialize private page header pool and cache magazine pool if we
522: * haven't done so yet.
1.23 thorpej 523: * XXX LOCKING.
1.3 pk 524: */
525: if (phpool.pr_size == 0) {
1.60.4.2! fvdl 526: #ifdef POOL_SUBPAGE
! 527: pool_init(&phpool, sizeof(struct pool_item_header), 0, 0, 0,
! 528: "phpool", PAGE_SIZE, pool_page_alloc, pool_page_free, 0);
! 529: pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0,
! 530: PR_RECURSIVE, "psppool", PAGE_SIZE,
! 531: pool_page_alloc, pool_page_free, 0);
! 532: #else
1.3 pk 533: pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
1.43 thorpej 534: 0, "phpool", 0, 0, 0, 0);
1.60.4.2! fvdl 535: #endif
1.43 thorpej 536: pool_init(&pcgpool, sizeof(struct pool_cache_group), 0, 0,
537: 0, "pcgpool", 0, 0, 0, 0);
1.1 pk 538: }
539:
1.23 thorpej 540: /* Insert into the list of all pools. */
541: simple_lock(&pool_head_slock);
542: TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
543: simple_unlock(&pool_head_slock);
1.1 pk 544: }
545:
546: /*
547: * De-commision a pool resource.
548: */
549: void
1.42 thorpej 550: pool_destroy(struct pool *pp)
1.1 pk 551: {
1.3 pk 552: struct pool_item_header *ph;
1.43 thorpej 553: struct pool_cache *pc;
554:
555: /* Destroy all caches for this pool. */
556: while ((pc = TAILQ_FIRST(&pp->pr_cachelist)) != NULL)
557: pool_cache_destroy(pc);
1.3 pk 558:
559: #ifdef DIAGNOSTIC
1.20 thorpej 560: if (pp->pr_nout != 0) {
1.25 thorpej 561: pr_printlog(pp, NULL, printf);
1.20 thorpej 562: panic("pool_destroy: pool busy: still out: %u\n",
563: pp->pr_nout);
1.3 pk 564: }
565: #endif
1.1 pk 566:
1.3 pk 567: /* Remove all pages */
1.20 thorpej 568: if ((pp->pr_roflags & PR_STATIC) == 0)
1.60.4.1 fvdl 569: while ((ph = TAILQ_FIRST(&pp->pr_pagelist)) != NULL)
570: pr_rmpage(pp, ph, NULL);
1.3 pk 571:
572: /* Remove from global pool list */
1.23 thorpej 573: simple_lock(&pool_head_slock);
1.3 pk 574: TAILQ_REMOVE(&pool_head, pp, pr_poollist);
1.60.4.1 fvdl 575: if (drainpp == pp) {
576: drainpp = NULL;
577: }
1.23 thorpej 578: simple_unlock(&pool_head_slock);
1.3 pk 579:
1.59 thorpej 580: #ifdef POOL_DIAGNOSTIC
1.20 thorpej 581: if ((pp->pr_roflags & PR_LOGGING) != 0)
1.3 pk 582: free(pp->pr_log, M_TEMP);
1.59 thorpej 583: #endif
1.2 pk 584:
1.20 thorpej 585: if (pp->pr_roflags & PR_FREEHEADER)
1.3 pk 586: free(pp, M_POOL);
1.1 pk 587: }
588:
1.55 thorpej 589: static __inline struct pool_item_header *
590: pool_alloc_item_header(struct pool *pp, caddr_t storage, int flags)
591: {
592: struct pool_item_header *ph;
593: int s;
594:
595: LOCK_ASSERT(simple_lock_held(&pp->pr_slock) == 0);
596:
597: if ((pp->pr_roflags & PR_PHINPAGE) != 0)
598: ph = (struct pool_item_header *) (storage + pp->pr_phoffset);
599: else {
600: s = splhigh();
601: ph = pool_get(&phpool, flags);
602: splx(s);
603: }
604:
605: return (ph);
606: }
1.1 pk 607:
608: /*
1.3 pk 609: * Grab an item from the pool; must be called at appropriate spl level
1.1 pk 610: */
1.3 pk 611: void *
1.59 thorpej 612: #ifdef POOL_DIAGNOSTIC
1.42 thorpej 613: _pool_get(struct pool *pp, int flags, const char *file, long line)
1.56 sommerfe 614: #else
615: pool_get(struct pool *pp, int flags)
616: #endif
1.1 pk 617: {
618: struct pool_item *pi;
1.3 pk 619: struct pool_item_header *ph;
1.55 thorpej 620: void *v;
1.1 pk 621:
1.2 pk 622: #ifdef DIAGNOSTIC
1.34 thorpej 623: if (__predict_false((pp->pr_roflags & PR_STATIC) &&
624: (flags & PR_MALLOCOK))) {
1.25 thorpej 625: pr_printlog(pp, NULL, printf);
1.2 pk 626: panic("pool_get: static");
1.3 pk 627: }
1.2 pk 628:
1.37 sommerfe 629: if (__predict_false(curproc == NULL && doing_shutdown == 0 &&
630: (flags & PR_WAITOK) != 0))
1.3 pk 631: panic("pool_get: must have NOWAIT");
1.58 thorpej 632:
633: #ifdef LOCKDEBUG
634: if (flags & PR_WAITOK)
635: simple_lock_only_held(NULL, "pool_get(PR_WAITOK)");
1.56 sommerfe 636: #endif
1.58 thorpej 637: #endif /* DIAGNOSTIC */
1.1 pk 638:
1.21 thorpej 639: simple_lock(&pp->pr_slock);
1.25 thorpej 640: pr_enter(pp, file, line);
1.20 thorpej 641:
642: startover:
643: /*
644: * Check to see if we've reached the hard limit. If we have,
645: * and we can wait, then wait until an item has been returned to
646: * the pool.
647: */
648: #ifdef DIAGNOSTIC
1.34 thorpej 649: if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
1.25 thorpej 650: pr_leave(pp);
1.21 thorpej 651: simple_unlock(&pp->pr_slock);
1.20 thorpej 652: panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
653: }
654: #endif
1.34 thorpej 655: if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
1.29 sommerfe 656: if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
1.20 thorpej 657: /*
658: * XXX: A warning isn't logged in this case. Should
659: * it be?
660: */
661: pp->pr_flags |= PR_WANTED;
1.25 thorpej 662: pr_leave(pp);
1.40 sommerfe 663: ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
1.25 thorpej 664: pr_enter(pp, file, line);
1.20 thorpej 665: goto startover;
666: }
1.31 thorpej 667:
668: /*
669: * Log a message that the hard limit has been hit.
670: */
671: if (pp->pr_hardlimit_warning != NULL &&
672: ratecheck(&pp->pr_hardlimit_warning_last,
673: &pp->pr_hardlimit_ratecap))
674: log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1.21 thorpej 675:
676: if (flags & PR_URGENT)
677: panic("pool_get: urgent");
678:
679: pp->pr_nfail++;
680:
1.25 thorpej 681: pr_leave(pp);
1.21 thorpej 682: simple_unlock(&pp->pr_slock);
1.20 thorpej 683: return (NULL);
684: }
685:
1.3 pk 686: /*
687: * The convention we use is that if `curpage' is not NULL, then
688: * it points at a non-empty bucket. In particular, `curpage'
689: * never points at a page header which has PR_PHINPAGE set and
690: * has no items in its bucket.
691: */
1.20 thorpej 692: if ((ph = pp->pr_curpage) == NULL) {
693: #ifdef DIAGNOSTIC
694: if (pp->pr_nitems != 0) {
1.21 thorpej 695: simple_unlock(&pp->pr_slock);
1.20 thorpej 696: printf("pool_get: %s: curpage NULL, nitems %u\n",
697: pp->pr_wchan, pp->pr_nitems);
698: panic("pool_get: nitems inconsistent\n");
699: }
700: #endif
701:
1.21 thorpej 702: /*
703: * Call the back-end page allocator for more memory.
704: * Release the pool lock, as the back-end page allocator
705: * may block.
706: */
1.25 thorpej 707: pr_leave(pp);
1.21 thorpej 708: simple_unlock(&pp->pr_slock);
709: v = (*pp->pr_alloc)(pp->pr_pagesz, flags, pp->pr_mtype);
1.55 thorpej 710: if (__predict_true(v != NULL))
711: ph = pool_alloc_item_header(pp, v, flags);
1.21 thorpej 712: simple_lock(&pp->pr_slock);
1.25 thorpej 713: pr_enter(pp, file, line);
1.15 pk 714:
1.55 thorpej 715: if (__predict_false(v == NULL || ph == NULL)) {
716: if (v != NULL)
717: (*pp->pr_free)(v, pp->pr_pagesz, pp->pr_mtype);
718:
1.21 thorpej 719: /*
1.55 thorpej 720: * We were unable to allocate a page or item
721: * header, but we released the lock during
722: * allocation, so perhaps items were freed
723: * back to the pool. Check for this case.
1.21 thorpej 724: */
725: if (pp->pr_curpage != NULL)
726: goto startover;
1.15 pk 727:
1.3 pk 728: if (flags & PR_URGENT)
729: panic("pool_get: urgent");
1.21 thorpej 730:
1.3 pk 731: if ((flags & PR_WAITOK) == 0) {
732: pp->pr_nfail++;
1.25 thorpej 733: pr_leave(pp);
1.21 thorpej 734: simple_unlock(&pp->pr_slock);
1.1 pk 735: return (NULL);
1.3 pk 736: }
737:
1.15 pk 738: /*
739: * Wait for items to be returned to this pool.
1.21 thorpej 740: *
1.15 pk 741: * XXX: we actually want to wait just until
742: * the page allocator has memory again. Depending
743: * on this pool's usage, we might get stuck here
744: * for a long time.
1.20 thorpej 745: *
746: * XXX: maybe we should wake up once a second and
747: * try again?
1.15 pk 748: */
1.1 pk 749: pp->pr_flags |= PR_WANTED;
1.25 thorpej 750: pr_leave(pp);
1.40 sommerfe 751: ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
1.25 thorpej 752: pr_enter(pp, file, line);
1.20 thorpej 753: goto startover;
1.1 pk 754: }
1.3 pk 755:
1.15 pk 756: /* We have more memory; add it to the pool */
1.55 thorpej 757: pool_prime_page(pp, v, ph);
1.15 pk 758: pp->pr_npagealloc++;
759:
1.20 thorpej 760: /* Start the allocation process over. */
761: goto startover;
1.3 pk 762: }
763:
1.34 thorpej 764: if (__predict_false((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)) {
1.25 thorpej 765: pr_leave(pp);
1.21 thorpej 766: simple_unlock(&pp->pr_slock);
1.3 pk 767: panic("pool_get: %s: page empty", pp->pr_wchan);
1.21 thorpej 768: }
1.20 thorpej 769: #ifdef DIAGNOSTIC
1.34 thorpej 770: if (__predict_false(pp->pr_nitems == 0)) {
1.25 thorpej 771: pr_leave(pp);
1.21 thorpej 772: simple_unlock(&pp->pr_slock);
1.20 thorpej 773: printf("pool_get: %s: items on itemlist, nitems %u\n",
774: pp->pr_wchan, pp->pr_nitems);
775: panic("pool_get: nitems inconsistent\n");
776: }
1.56 sommerfe 777:
1.3 pk 778: pr_log(pp, v, PRLOG_GET, file, line);
779:
1.34 thorpej 780: if (__predict_false(pi->pi_magic != PI_MAGIC)) {
1.25 thorpej 781: pr_printlog(pp, pi, printf);
1.3 pk 782: panic("pool_get(%s): free list modified: magic=%x; page %p;"
783: " item addr %p\n",
784: pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
785: }
786: #endif
787:
788: /*
789: * Remove from item list.
790: */
791: TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
1.20 thorpej 792: pp->pr_nitems--;
793: pp->pr_nout++;
1.6 thorpej 794: if (ph->ph_nmissing == 0) {
795: #ifdef DIAGNOSTIC
1.34 thorpej 796: if (__predict_false(pp->pr_nidle == 0))
1.6 thorpej 797: panic("pool_get: nidle inconsistent");
798: #endif
799: pp->pr_nidle--;
800: }
1.3 pk 801: ph->ph_nmissing++;
802: if (TAILQ_FIRST(&ph->ph_itemlist) == NULL) {
1.21 thorpej 803: #ifdef DIAGNOSTIC
1.34 thorpej 804: if (__predict_false(ph->ph_nmissing != pp->pr_itemsperpage)) {
1.25 thorpej 805: pr_leave(pp);
1.21 thorpej 806: simple_unlock(&pp->pr_slock);
807: panic("pool_get: %s: nmissing inconsistent",
808: pp->pr_wchan);
809: }
810: #endif
1.3 pk 811: /*
812: * Find a new non-empty page header, if any.
813: * Start search from the page head, to increase
814: * the chance for "high water" pages to be freed.
815: *
1.21 thorpej 816: * Migrate empty pages to the end of the list. This
817: * will speed the update of curpage as pages become
818: * idle. Empty pages intermingled with idle pages
819: * is no big deal. As soon as a page becomes un-empty,
820: * it will move back to the head of the list.
1.3 pk 821: */
822: TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
1.21 thorpej 823: TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
1.60.4.1 fvdl 824: TAILQ_FOREACH(ph, &pp->pr_pagelist, ph_pagelist)
1.3 pk 825: if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
826: break;
827:
828: pp->pr_curpage = ph;
1.1 pk 829: }
1.3 pk 830:
831: pp->pr_nget++;
1.20 thorpej 832:
833: /*
834: * If we have a low water mark and we are now below that low
835: * water mark, add more items to the pool.
836: */
1.53 thorpej 837: if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20 thorpej 838: /*
839: * XXX: Should we log a warning? Should we set up a timeout
840: * to try again in a second or so? The latter could break
841: * a caller's assumptions about interrupt protection, etc.
842: */
843: }
844:
1.25 thorpej 845: pr_leave(pp);
1.21 thorpej 846: simple_unlock(&pp->pr_slock);
1.1 pk 847: return (v);
848: }
849:
850: /*
1.43 thorpej 851: * Internal version of pool_put(). Pool is already locked/entered.
1.1 pk 852: */
1.43 thorpej 853: static void
1.56 sommerfe 854: pool_do_put(struct pool *pp, void *v)
1.1 pk 855: {
856: struct pool_item *pi = v;
1.3 pk 857: struct pool_item_header *ph;
858: caddr_t page;
1.21 thorpej 859: int s;
1.3 pk 860:
1.60.4.1 fvdl 861: LOCK_ASSERT(simple_lock_held(&pp->pr_slock));
862:
1.3 pk 863: page = (caddr_t)((u_long)v & pp->pr_pagemask);
1.1 pk 864:
1.30 thorpej 865: #ifdef DIAGNOSTIC
1.34 thorpej 866: if (__predict_false(pp->pr_nout == 0)) {
1.30 thorpej 867: printf("pool %s: putting with none out\n",
868: pp->pr_wchan);
869: panic("pool_put");
870: }
871: #endif
1.3 pk 872:
1.34 thorpej 873: if (__predict_false((ph = pr_find_pagehead(pp, page)) == NULL)) {
1.25 thorpej 874: pr_printlog(pp, NULL, printf);
1.3 pk 875: panic("pool_put: %s: page header missing", pp->pr_wchan);
876: }
1.28 thorpej 877:
878: #ifdef LOCKDEBUG
879: /*
880: * Check if we're freeing a locked simple lock.
881: */
882: simple_lock_freecheck((caddr_t)pi, ((caddr_t)pi) + pp->pr_size);
883: #endif
1.3 pk 884:
885: /*
886: * Return to item list.
887: */
1.2 pk 888: #ifdef DIAGNOSTIC
1.3 pk 889: pi->pi_magic = PI_MAGIC;
890: #endif
1.32 chs 891: #ifdef DEBUG
892: {
893: int i, *ip = v;
894:
895: for (i = 0; i < pp->pr_size / sizeof(int); i++) {
896: *ip++ = PI_MAGIC;
897: }
898: }
899: #endif
900:
1.3 pk 901: TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
902: ph->ph_nmissing--;
903: pp->pr_nput++;
1.20 thorpej 904: pp->pr_nitems++;
905: pp->pr_nout--;
1.3 pk 906:
907: /* Cancel "pool empty" condition if it exists */
908: if (pp->pr_curpage == NULL)
909: pp->pr_curpage = ph;
910:
911: if (pp->pr_flags & PR_WANTED) {
912: pp->pr_flags &= ~PR_WANTED;
1.15 pk 913: if (ph->ph_nmissing == 0)
914: pp->pr_nidle++;
1.3 pk 915: wakeup((caddr_t)pp);
916: return;
917: }
918:
919: /*
1.21 thorpej 920: * If this page is now complete, do one of two things:
921: *
922: * (1) If we have more pages than the page high water
923: * mark, free the page back to the system.
924: *
925: * (2) Move it to the end of the page list, so that
926: * we minimize our chances of fragmenting the
927: * pool. Idle pages migrate to the end (along with
928: * completely empty pages, so that we find un-empty
929: * pages more quickly when we update curpage) of the
930: * list so they can be more easily swept up by
931: * the pagedaemon when pages are scarce.
1.3 pk 932: */
933: if (ph->ph_nmissing == 0) {
1.6 thorpej 934: pp->pr_nidle++;
1.3 pk 935: if (pp->pr_npages > pp->pr_maxpages) {
1.60.4.1 fvdl 936: pr_rmpage(pp, ph, NULL);
1.3 pk 937: } else {
938: TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
939: TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
940:
1.21 thorpej 941: /*
942: * Update the timestamp on the page. A page must
943: * be idle for some period of time before it can
944: * be reclaimed by the pagedaemon. This minimizes
945: * ping-pong'ing for memory.
946: */
947: s = splclock();
948: ph->ph_time = mono_time;
949: splx(s);
950:
951: /*
952: * Update the current page pointer. Just look for
953: * the first page with any free items.
954: *
955: * XXX: Maybe we want an option to look for the
956: * page with the fewest available items, to minimize
957: * fragmentation?
958: */
1.60.4.1 fvdl 959: TAILQ_FOREACH(ph, &pp->pr_pagelist, ph_pagelist)
1.3 pk 960: if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
961: break;
1.1 pk 962:
1.3 pk 963: pp->pr_curpage = ph;
1.1 pk 964: }
965: }
1.21 thorpej 966: /*
967: * If the page has just become un-empty, move it to the head of
968: * the list, and make it the current page. The next allocation
969: * will get the item from this page, instead of further fragmenting
970: * the pool.
971: */
972: else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
973: TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
974: TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
975: pp->pr_curpage = ph;
976: }
1.43 thorpej 977: }
978:
979: /*
980: * Return resource to the pool; must be called at appropriate spl level
981: */
1.59 thorpej 982: #ifdef POOL_DIAGNOSTIC
1.43 thorpej 983: void
984: _pool_put(struct pool *pp, void *v, const char *file, long line)
985: {
986:
987: simple_lock(&pp->pr_slock);
988: pr_enter(pp, file, line);
989:
1.56 sommerfe 990: pr_log(pp, v, PRLOG_PUT, file, line);
991:
992: pool_do_put(pp, v);
1.21 thorpej 993:
1.25 thorpej 994: pr_leave(pp);
1.21 thorpej 995: simple_unlock(&pp->pr_slock);
1.1 pk 996: }
1.57 sommerfe 997: #undef pool_put
1.59 thorpej 998: #endif /* POOL_DIAGNOSTIC */
1.1 pk 999:
1.56 sommerfe 1000: void
1001: pool_put(struct pool *pp, void *v)
1002: {
1003:
1004: simple_lock(&pp->pr_slock);
1005:
1006: pool_do_put(pp, v);
1007:
1008: simple_unlock(&pp->pr_slock);
1009: }
1.57 sommerfe 1010:
1.59 thorpej 1011: #ifdef POOL_DIAGNOSTIC
1.57 sommerfe 1012: #define pool_put(h, v) _pool_put((h), (v), __FILE__, __LINE__)
1.56 sommerfe 1013: #endif
1014:
1.1 pk 1015: /*
1.55 thorpej 1016: * Add N items to the pool.
1017: */
1018: int
1019: pool_prime(struct pool *pp, int n)
1020: {
1021: struct pool_item_header *ph;
1022: caddr_t cp;
1023: int newpages, error = 0;
1024:
1025: simple_lock(&pp->pr_slock);
1026:
1027: newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1028:
1029: while (newpages-- > 0) {
1030: simple_unlock(&pp->pr_slock);
1031: cp = (*pp->pr_alloc)(pp->pr_pagesz, PR_NOWAIT, pp->pr_mtype);
1032: if (__predict_true(cp != NULL))
1033: ph = pool_alloc_item_header(pp, cp, PR_NOWAIT);
1034: simple_lock(&pp->pr_slock);
1035:
1036: if (__predict_false(cp == NULL || ph == NULL)) {
1037: error = ENOMEM;
1038: if (cp != NULL)
1039: (*pp->pr_free)(cp, pp->pr_pagesz, pp->pr_mtype);
1040: break;
1041: }
1042:
1043: pool_prime_page(pp, cp, ph);
1044: pp->pr_npagealloc++;
1045: pp->pr_minpages++;
1046: }
1047:
1048: if (pp->pr_minpages >= pp->pr_maxpages)
1049: pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */
1050:
1051: simple_unlock(&pp->pr_slock);
1052: return (0);
1053: }
1054:
1055: /*
1.3 pk 1056: * Add a page worth of items to the pool.
1.21 thorpej 1057: *
1058: * Note, we must be called with the pool descriptor LOCKED.
1.3 pk 1059: */
1.55 thorpej 1060: static void
1061: pool_prime_page(struct pool *pp, caddr_t storage, struct pool_item_header *ph)
1.3 pk 1062: {
1063: struct pool_item *pi;
1064: caddr_t cp = storage;
1065: unsigned int align = pp->pr_align;
1066: unsigned int ioff = pp->pr_itemoffset;
1.55 thorpej 1067: int n;
1.36 pk 1068:
1069: if (((u_long)cp & (pp->pr_pagesz - 1)) != 0)
1070: panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1.3 pk 1071:
1.55 thorpej 1072: if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1.3 pk 1073: LIST_INSERT_HEAD(&pp->pr_hashtab[PR_HASH_INDEX(pp, cp)],
1.55 thorpej 1074: ph, ph_hashlist);
1.3 pk 1075:
1076: /*
1077: * Insert page header.
1078: */
1079: TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
1080: TAILQ_INIT(&ph->ph_itemlist);
1081: ph->ph_page = storage;
1082: ph->ph_nmissing = 0;
1.21 thorpej 1083: memset(&ph->ph_time, 0, sizeof(ph->ph_time));
1.3 pk 1084:
1.6 thorpej 1085: pp->pr_nidle++;
1086:
1.3 pk 1087: /*
1088: * Color this page.
1089: */
1090: cp = (caddr_t)(cp + pp->pr_curcolor);
1091: if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
1092: pp->pr_curcolor = 0;
1093:
1094: /*
1095: * Adjust storage to apply aligment to `pr_itemoffset' in each item.
1096: */
1097: if (ioff != 0)
1098: cp = (caddr_t)(cp + (align - ioff));
1099:
1100: /*
1101: * Insert remaining chunks on the bucket list.
1102: */
1103: n = pp->pr_itemsperpage;
1.20 thorpej 1104: pp->pr_nitems += n;
1.3 pk 1105:
1106: while (n--) {
1107: pi = (struct pool_item *)cp;
1108:
1109: /* Insert on page list */
1110: TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
1111: #ifdef DIAGNOSTIC
1112: pi->pi_magic = PI_MAGIC;
1113: #endif
1114: cp = (caddr_t)(cp + pp->pr_size);
1115: }
1116:
1117: /*
1118: * If the pool was depleted, point at the new page.
1119: */
1120: if (pp->pr_curpage == NULL)
1121: pp->pr_curpage = ph;
1122:
1123: if (++pp->pr_npages > pp->pr_hiwat)
1124: pp->pr_hiwat = pp->pr_npages;
1125: }
1126:
1.20 thorpej 1127: /*
1.52 thorpej 1128: * Used by pool_get() when nitems drops below the low water mark. This
1129: * is used to catch up nitmes with the low water mark.
1.20 thorpej 1130: *
1.21 thorpej 1131: * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20 thorpej 1132: *
1133: * Note 2, this doesn't work with static pools.
1134: *
1135: * Note 3, we must be called with the pool already locked, and we return
1136: * with it locked.
1137: */
1138: static int
1.42 thorpej 1139: pool_catchup(struct pool *pp)
1.20 thorpej 1140: {
1.55 thorpej 1141: struct pool_item_header *ph;
1.20 thorpej 1142: caddr_t cp;
1143: int error = 0;
1144:
1145: if (pp->pr_roflags & PR_STATIC) {
1146: /*
1147: * We dropped below the low water mark, and this is not a
1148: * good thing. Log a warning.
1.21 thorpej 1149: *
1150: * XXX: rate-limit this?
1.20 thorpej 1151: */
1152: printf("WARNING: static pool `%s' dropped below low water "
1153: "mark\n", pp->pr_wchan);
1154: return (0);
1155: }
1156:
1.54 thorpej 1157: while (POOL_NEEDS_CATCHUP(pp)) {
1.20 thorpej 1158: /*
1.21 thorpej 1159: * Call the page back-end allocator for more memory.
1160: *
1161: * XXX: We never wait, so should we bother unlocking
1162: * the pool descriptor?
1.20 thorpej 1163: */
1.21 thorpej 1164: simple_unlock(&pp->pr_slock);
1.55 thorpej 1165: cp = (*pp->pr_alloc)(pp->pr_pagesz, PR_NOWAIT, pp->pr_mtype);
1166: if (__predict_true(cp != NULL))
1167: ph = pool_alloc_item_header(pp, cp, PR_NOWAIT);
1.21 thorpej 1168: simple_lock(&pp->pr_slock);
1.55 thorpej 1169: if (__predict_false(cp == NULL || ph == NULL)) {
1170: if (cp != NULL)
1171: (*pp->pr_free)(cp, pp->pr_pagesz, pp->pr_mtype);
1.20 thorpej 1172: error = ENOMEM;
1173: break;
1174: }
1.55 thorpej 1175: pool_prime_page(pp, cp, ph);
1.26 thorpej 1176: pp->pr_npagealloc++;
1.20 thorpej 1177: }
1178:
1179: return (error);
1180: }
1181:
1.3 pk 1182: void
1.42 thorpej 1183: pool_setlowat(struct pool *pp, int n)
1.3 pk 1184: {
1.20 thorpej 1185: int error;
1.15 pk 1186:
1.21 thorpej 1187: simple_lock(&pp->pr_slock);
1188:
1.3 pk 1189: pp->pr_minitems = n;
1.15 pk 1190: pp->pr_minpages = (n == 0)
1191: ? 0
1.18 thorpej 1192: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20 thorpej 1193:
1194: /* Make sure we're caught up with the newly-set low water mark. */
1.53 thorpej 1195: if (POOL_NEEDS_CATCHUP(pp) && (error = pool_catchup(pp) != 0)) {
1.20 thorpej 1196: /*
1197: * XXX: Should we log a warning? Should we set up a timeout
1198: * to try again in a second or so? The latter could break
1199: * a caller's assumptions about interrupt protection, etc.
1200: */
1201: }
1.21 thorpej 1202:
1203: simple_unlock(&pp->pr_slock);
1.3 pk 1204: }
1205:
1206: void
1.42 thorpej 1207: pool_sethiwat(struct pool *pp, int n)
1.3 pk 1208: {
1.15 pk 1209:
1.21 thorpej 1210: simple_lock(&pp->pr_slock);
1211:
1.15 pk 1212: pp->pr_maxpages = (n == 0)
1213: ? 0
1.18 thorpej 1214: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1215:
1216: simple_unlock(&pp->pr_slock);
1.3 pk 1217: }
1218:
1.20 thorpej 1219: void
1.42 thorpej 1220: pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1.20 thorpej 1221: {
1222:
1.21 thorpej 1223: simple_lock(&pp->pr_slock);
1.20 thorpej 1224:
1225: pp->pr_hardlimit = n;
1226: pp->pr_hardlimit_warning = warnmess;
1.31 thorpej 1227: pp->pr_hardlimit_ratecap.tv_sec = ratecap;
1228: pp->pr_hardlimit_warning_last.tv_sec = 0;
1229: pp->pr_hardlimit_warning_last.tv_usec = 0;
1.20 thorpej 1230:
1231: /*
1.21 thorpej 1232: * In-line version of pool_sethiwat(), because we don't want to
1233: * release the lock.
1.20 thorpej 1234: */
1235: pp->pr_maxpages = (n == 0)
1236: ? 0
1237: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1238:
1239: simple_unlock(&pp->pr_slock);
1.20 thorpej 1240: }
1.3 pk 1241:
1242: /*
1243: * Default page allocator.
1244: */
1245: static void *
1.42 thorpej 1246: pool_page_alloc(unsigned long sz, int flags, int mtype)
1.3 pk 1247: {
1.11 thorpej 1248: boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1.3 pk 1249:
1.11 thorpej 1250: return ((void *)uvm_km_alloc_poolpage(waitok));
1.3 pk 1251: }
1252:
1253: static void
1.42 thorpej 1254: pool_page_free(void *v, unsigned long sz, int mtype)
1.3 pk 1255: {
1256:
1.10 eeh 1257: uvm_km_free_poolpage((vaddr_t)v);
1.3 pk 1258: }
1.12 thorpej 1259:
1.60.4.2! fvdl 1260: #ifdef POOL_SUBPAGE
! 1261: /*
! 1262: * Sub-page allocator, for machines with large hardware pages.
! 1263: */
! 1264: static void *
! 1265: pool_subpage_alloc(unsigned long sz, int flags, int mtype)
! 1266: {
! 1267:
! 1268: return pool_get(&psppool, flags);
! 1269: }
! 1270:
! 1271: static void
! 1272: pool_subpage_free(void *v, unsigned long sz, int mtype)
! 1273: {
! 1274:
! 1275: pool_put(&psppool, v);
! 1276: }
! 1277: #endif
! 1278:
! 1279: #ifdef POOL_SUBPAGE
! 1280: /* We don't provide a real nointr allocator. Maybe later. */
! 1281: void *
! 1282: pool_page_alloc_nointr(unsigned long sz, int flags, int mtype)
! 1283: {
! 1284:
! 1285: return pool_subpage_alloc(sz, flags, mtype);
! 1286: }
! 1287:
! 1288: void
! 1289: pool_page_free_nointr(void *v, unsigned long sz, int mtype)
! 1290: {
! 1291:
! 1292: pool_subpage_free(v, sz, mtype);
! 1293: }
! 1294: #else
1.12 thorpej 1295: /*
1296: * Alternate pool page allocator for pools that know they will
1297: * never be accessed in interrupt context.
1298: */
1299: void *
1.42 thorpej 1300: pool_page_alloc_nointr(unsigned long sz, int flags, int mtype)
1.12 thorpej 1301: {
1302: boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1303:
1304: return ((void *)uvm_km_alloc_poolpage1(kernel_map, uvm.kernel_object,
1305: waitok));
1306: }
1307:
1308: void
1.42 thorpej 1309: pool_page_free_nointr(void *v, unsigned long sz, int mtype)
1.12 thorpej 1310: {
1311:
1312: uvm_km_free_poolpage1(kernel_map, (vaddr_t)v);
1313: }
1.60.4.2! fvdl 1314: #endif
1.12 thorpej 1315:
1.3 pk 1316:
1317: /*
1318: * Release all complete pages that have not been used recently.
1319: */
1320: void
1.59 thorpej 1321: #ifdef POOL_DIAGNOSTIC
1.42 thorpej 1322: _pool_reclaim(struct pool *pp, const char *file, long line)
1.56 sommerfe 1323: #else
1324: pool_reclaim(struct pool *pp)
1325: #endif
1.3 pk 1326: {
1327: struct pool_item_header *ph, *phnext;
1.43 thorpej 1328: struct pool_cache *pc;
1.21 thorpej 1329: struct timeval curtime;
1.60.4.1 fvdl 1330: struct pool_pagelist pq;
1.21 thorpej 1331: int s;
1.3 pk 1332:
1.20 thorpej 1333: if (pp->pr_roflags & PR_STATIC)
1.3 pk 1334: return;
1335:
1.21 thorpej 1336: if (simple_lock_try(&pp->pr_slock) == 0)
1.3 pk 1337: return;
1.25 thorpej 1338: pr_enter(pp, file, line);
1.60.4.1 fvdl 1339: TAILQ_INIT(&pq);
1.3 pk 1340:
1.43 thorpej 1341: /*
1342: * Reclaim items from the pool's caches.
1343: */
1.60.4.1 fvdl 1344: TAILQ_FOREACH(pc, &pp->pr_cachelist, pc_poollist)
1.43 thorpej 1345: pool_cache_reclaim(pc);
1346:
1.21 thorpej 1347: s = splclock();
1348: curtime = mono_time;
1349: splx(s);
1350:
1.3 pk 1351: for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL; ph = phnext) {
1352: phnext = TAILQ_NEXT(ph, ph_pagelist);
1353:
1354: /* Check our minimum page claim */
1355: if (pp->pr_npages <= pp->pr_minpages)
1356: break;
1357:
1358: if (ph->ph_nmissing == 0) {
1359: struct timeval diff;
1360: timersub(&curtime, &ph->ph_time, &diff);
1361: if (diff.tv_sec < pool_inactive_time)
1362: continue;
1.21 thorpej 1363:
1364: /*
1365: * If freeing this page would put us below
1366: * the low water mark, stop now.
1367: */
1368: if ((pp->pr_nitems - pp->pr_itemsperpage) <
1369: pp->pr_minitems)
1370: break;
1371:
1.60.4.1 fvdl 1372: pr_rmpage(pp, ph, &pq);
1.3 pk 1373: }
1374: }
1375:
1.25 thorpej 1376: pr_leave(pp);
1.21 thorpej 1377: simple_unlock(&pp->pr_slock);
1.60.4.1 fvdl 1378: if (TAILQ_EMPTY(&pq)) {
1379: return;
1380: }
1381: while ((ph = TAILQ_FIRST(&pq)) != NULL) {
1382: TAILQ_REMOVE(&pq, ph, ph_pagelist);
1383: (*pp->pr_free)(ph->ph_page, pp->pr_pagesz, pp->pr_mtype);
1384: if (pp->pr_roflags & PR_PHINPAGE) {
1385: continue;
1386: }
1387: LIST_REMOVE(ph, ph_hashlist);
1388: s = splhigh();
1389: pool_put(&phpool, ph);
1390: splx(s);
1391: }
1.3 pk 1392: }
1393:
1394:
1395: /*
1396: * Drain pools, one at a time.
1.21 thorpej 1397: *
1398: * Note, we must never be called from an interrupt context.
1.3 pk 1399: */
1400: void
1.42 thorpej 1401: pool_drain(void *arg)
1.3 pk 1402: {
1403: struct pool *pp;
1.23 thorpej 1404: int s;
1.3 pk 1405:
1.60.4.1 fvdl 1406: pp = NULL;
1.49 thorpej 1407: s = splvm();
1.23 thorpej 1408: simple_lock(&pool_head_slock);
1.60.4.1 fvdl 1409: if (drainpp == NULL) {
1410: drainpp = TAILQ_FIRST(&pool_head);
1411: }
1412: if (drainpp) {
1413: pp = drainpp;
1414: drainpp = TAILQ_NEXT(pp, pr_poollist);
1415: }
1.23 thorpej 1416: simple_unlock(&pool_head_slock);
1.3 pk 1417: splx(s);
1.60.4.1 fvdl 1418:
1419: pool_reclaim(pp);
1.3 pk 1420: }
1421:
1422:
1423: /*
1424: * Diagnostic helpers.
1425: */
1426: void
1.42 thorpej 1427: pool_print(struct pool *pp, const char *modif)
1.21 thorpej 1428: {
1429: int s;
1430:
1.49 thorpej 1431: s = splvm();
1.25 thorpej 1432: if (simple_lock_try(&pp->pr_slock) == 0) {
1433: printf("pool %s is locked; try again later\n",
1434: pp->pr_wchan);
1435: splx(s);
1436: return;
1437: }
1438: pool_print1(pp, modif, printf);
1.21 thorpej 1439: simple_unlock(&pp->pr_slock);
1440: splx(s);
1441: }
1442:
1.25 thorpej 1443: void
1.42 thorpej 1444: pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.25 thorpej 1445: {
1446: int didlock = 0;
1447:
1448: if (pp == NULL) {
1449: (*pr)("Must specify a pool to print.\n");
1450: return;
1451: }
1452:
1453: /*
1454: * Called from DDB; interrupts should be blocked, and all
1455: * other processors should be paused. We can skip locking
1456: * the pool in this case.
1457: *
1458: * We do a simple_lock_try() just to print the lock
1459: * status, however.
1460: */
1461:
1462: if (simple_lock_try(&pp->pr_slock) == 0)
1463: (*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
1464: else
1465: didlock = 1;
1466:
1467: pool_print1(pp, modif, pr);
1468:
1469: if (didlock)
1470: simple_unlock(&pp->pr_slock);
1471: }
1472:
1.21 thorpej 1473: static void
1.42 thorpej 1474: pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.3 pk 1475: {
1.25 thorpej 1476: struct pool_item_header *ph;
1.44 thorpej 1477: struct pool_cache *pc;
1478: struct pool_cache_group *pcg;
1.25 thorpej 1479: #ifdef DIAGNOSTIC
1480: struct pool_item *pi;
1481: #endif
1.44 thorpej 1482: int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1.25 thorpej 1483: char c;
1484:
1485: while ((c = *modif++) != '\0') {
1486: if (c == 'l')
1487: print_log = 1;
1488: if (c == 'p')
1489: print_pagelist = 1;
1.44 thorpej 1490: if (c == 'c')
1491: print_cache = 1;
1.25 thorpej 1492: modif++;
1493: }
1494:
1495: (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1496: pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1497: pp->pr_roflags);
1498: (*pr)("\tpagesz %u, mtype %d\n", pp->pr_pagesz, pp->pr_mtype);
1499: (*pr)("\talloc %p, release %p\n", pp->pr_alloc, pp->pr_free);
1500: (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1501: pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1502: (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1503: pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1504:
1505: (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
1506: pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1507: (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1508: pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1509:
1510: if (print_pagelist == 0)
1511: goto skip_pagelist;
1512:
1513: if ((ph = TAILQ_FIRST(&pp->pr_pagelist)) != NULL)
1514: (*pr)("\n\tpage list:\n");
1515: for (; ph != NULL; ph = TAILQ_NEXT(ph, ph_pagelist)) {
1516: (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
1517: ph->ph_page, ph->ph_nmissing,
1518: (u_long)ph->ph_time.tv_sec,
1519: (u_long)ph->ph_time.tv_usec);
1520: #ifdef DIAGNOSTIC
1.60.4.1 fvdl 1521: TAILQ_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1.25 thorpej 1522: if (pi->pi_magic != PI_MAGIC) {
1523: (*pr)("\t\t\titem %p, magic 0x%x\n",
1524: pi, pi->pi_magic);
1525: }
1526: }
1527: #endif
1528: }
1529: if (pp->pr_curpage == NULL)
1530: (*pr)("\tno current page\n");
1531: else
1532: (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1533:
1534: skip_pagelist:
1535:
1536: if (print_log == 0)
1537: goto skip_log;
1538:
1539: (*pr)("\n");
1540: if ((pp->pr_roflags & PR_LOGGING) == 0)
1541: (*pr)("\tno log\n");
1542: else
1543: pr_printlog(pp, NULL, pr);
1.3 pk 1544:
1.25 thorpej 1545: skip_log:
1.44 thorpej 1546:
1547: if (print_cache == 0)
1548: goto skip_cache;
1549:
1.60.4.1 fvdl 1550: TAILQ_FOREACH(pc, &pp->pr_cachelist, pc_poollist) {
1.44 thorpej 1551: (*pr)("\tcache %p: allocfrom %p freeto %p\n", pc,
1552: pc->pc_allocfrom, pc->pc_freeto);
1.48 thorpej 1553: (*pr)("\t hits %lu misses %lu ngroups %lu nitems %lu\n",
1554: pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems);
1.60.4.1 fvdl 1555: TAILQ_FOREACH(pcg, &pc->pc_grouplist, pcg_list) {
1.44 thorpej 1556: (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail);
1557: for (i = 0; i < PCG_NOBJECTS; i++)
1558: (*pr)("\t\t\t%p\n", pcg->pcg_objects[i]);
1559: }
1560: }
1561:
1562: skip_cache:
1.3 pk 1563:
1.25 thorpej 1564: pr_enter_check(pp, pr);
1.3 pk 1565: }
1566:
1567: int
1.42 thorpej 1568: pool_chk(struct pool *pp, const char *label)
1.3 pk 1569: {
1570: struct pool_item_header *ph;
1571: int r = 0;
1572:
1.21 thorpej 1573: simple_lock(&pp->pr_slock);
1.3 pk 1574:
1.60.4.1 fvdl 1575: TAILQ_FOREACH(ph, &pp->pr_pagelist, ph_pagelist) {
1.3 pk 1576: struct pool_item *pi;
1577: int n;
1578: caddr_t page;
1579:
1580: page = (caddr_t)((u_long)ph & pp->pr_pagemask);
1.20 thorpej 1581: if (page != ph->ph_page &&
1582: (pp->pr_roflags & PR_PHINPAGE) != 0) {
1.3 pk 1583: if (label != NULL)
1584: printf("%s: ", label);
1.16 briggs 1585: printf("pool(%p:%s): page inconsistency: page %p;"
1586: " at page head addr %p (p %p)\n", pp,
1.3 pk 1587: pp->pr_wchan, ph->ph_page,
1588: ph, page);
1589: r++;
1590: goto out;
1591: }
1592:
1593: for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
1594: pi != NULL;
1595: pi = TAILQ_NEXT(pi,pi_list), n++) {
1596:
1597: #ifdef DIAGNOSTIC
1598: if (pi->pi_magic != PI_MAGIC) {
1599: if (label != NULL)
1600: printf("%s: ", label);
1601: printf("pool(%s): free list modified: magic=%x;"
1602: " page %p; item ordinal %d;"
1603: " addr %p (p %p)\n",
1604: pp->pr_wchan, pi->pi_magic, ph->ph_page,
1605: n, pi, page);
1606: panic("pool");
1607: }
1608: #endif
1609: page = (caddr_t)((u_long)pi & pp->pr_pagemask);
1610: if (page == ph->ph_page)
1611: continue;
1612:
1613: if (label != NULL)
1614: printf("%s: ", label);
1.16 briggs 1615: printf("pool(%p:%s): page inconsistency: page %p;"
1616: " item ordinal %d; addr %p (p %p)\n", pp,
1.3 pk 1617: pp->pr_wchan, ph->ph_page,
1618: n, pi, page);
1619: r++;
1620: goto out;
1621: }
1622: }
1623: out:
1.21 thorpej 1624: simple_unlock(&pp->pr_slock);
1.3 pk 1625: return (r);
1.43 thorpej 1626: }
1627:
1628: /*
1629: * pool_cache_init:
1630: *
1631: * Initialize a pool cache.
1632: *
1633: * NOTE: If the pool must be protected from interrupts, we expect
1634: * to be called at the appropriate interrupt priority level.
1635: */
1636: void
1637: pool_cache_init(struct pool_cache *pc, struct pool *pp,
1638: int (*ctor)(void *, void *, int),
1639: void (*dtor)(void *, void *),
1640: void *arg)
1641: {
1642:
1643: TAILQ_INIT(&pc->pc_grouplist);
1644: simple_lock_init(&pc->pc_slock);
1645:
1646: pc->pc_allocfrom = NULL;
1647: pc->pc_freeto = NULL;
1648: pc->pc_pool = pp;
1649:
1650: pc->pc_ctor = ctor;
1651: pc->pc_dtor = dtor;
1652: pc->pc_arg = arg;
1653:
1.48 thorpej 1654: pc->pc_hits = 0;
1655: pc->pc_misses = 0;
1656:
1657: pc->pc_ngroups = 0;
1658:
1659: pc->pc_nitems = 0;
1660:
1.43 thorpej 1661: simple_lock(&pp->pr_slock);
1662: TAILQ_INSERT_TAIL(&pp->pr_cachelist, pc, pc_poollist);
1663: simple_unlock(&pp->pr_slock);
1664: }
1665:
1666: /*
1667: * pool_cache_destroy:
1668: *
1669: * Destroy a pool cache.
1670: */
1671: void
1672: pool_cache_destroy(struct pool_cache *pc)
1673: {
1674: struct pool *pp = pc->pc_pool;
1675:
1676: /* First, invalidate the entire cache. */
1677: pool_cache_invalidate(pc);
1678:
1679: /* ...and remove it from the pool's cache list. */
1680: simple_lock(&pp->pr_slock);
1681: TAILQ_REMOVE(&pp->pr_cachelist, pc, pc_poollist);
1682: simple_unlock(&pp->pr_slock);
1683: }
1684:
1685: static __inline void *
1686: pcg_get(struct pool_cache_group *pcg)
1687: {
1688: void *object;
1689: u_int idx;
1690:
1691: KASSERT(pcg->pcg_avail <= PCG_NOBJECTS);
1.45 thorpej 1692: KASSERT(pcg->pcg_avail != 0);
1.43 thorpej 1693: idx = --pcg->pcg_avail;
1694:
1695: KASSERT(pcg->pcg_objects[idx] != NULL);
1696: object = pcg->pcg_objects[idx];
1697: pcg->pcg_objects[idx] = NULL;
1698:
1699: return (object);
1700: }
1701:
1702: static __inline void
1703: pcg_put(struct pool_cache_group *pcg, void *object)
1704: {
1705: u_int idx;
1706:
1707: KASSERT(pcg->pcg_avail < PCG_NOBJECTS);
1708: idx = pcg->pcg_avail++;
1709:
1710: KASSERT(pcg->pcg_objects[idx] == NULL);
1711: pcg->pcg_objects[idx] = object;
1712: }
1713:
1714: /*
1715: * pool_cache_get:
1716: *
1717: * Get an object from a pool cache.
1718: */
1719: void *
1720: pool_cache_get(struct pool_cache *pc, int flags)
1721: {
1722: struct pool_cache_group *pcg;
1723: void *object;
1.58 thorpej 1724:
1725: #ifdef LOCKDEBUG
1726: if (flags & PR_WAITOK)
1727: simple_lock_only_held(NULL, "pool_cache_get(PR_WAITOK)");
1728: #endif
1.43 thorpej 1729:
1730: simple_lock(&pc->pc_slock);
1731:
1732: if ((pcg = pc->pc_allocfrom) == NULL) {
1.60.4.1 fvdl 1733: TAILQ_FOREACH(pcg, &pc->pc_grouplist, pcg_list) {
1.43 thorpej 1734: if (pcg->pcg_avail != 0) {
1735: pc->pc_allocfrom = pcg;
1736: goto have_group;
1737: }
1738: }
1739:
1740: /*
1741: * No groups with any available objects. Allocate
1742: * a new object, construct it, and return it to
1743: * the caller. We will allocate a group, if necessary,
1744: * when the object is freed back to the cache.
1745: */
1.48 thorpej 1746: pc->pc_misses++;
1.43 thorpej 1747: simple_unlock(&pc->pc_slock);
1748: object = pool_get(pc->pc_pool, flags);
1749: if (object != NULL && pc->pc_ctor != NULL) {
1750: if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) {
1751: pool_put(pc->pc_pool, object);
1752: return (NULL);
1753: }
1754: }
1755: return (object);
1756: }
1757:
1758: have_group:
1.48 thorpej 1759: pc->pc_hits++;
1760: pc->pc_nitems--;
1.43 thorpej 1761: object = pcg_get(pcg);
1762:
1763: if (pcg->pcg_avail == 0)
1764: pc->pc_allocfrom = NULL;
1.45 thorpej 1765:
1.43 thorpej 1766: simple_unlock(&pc->pc_slock);
1767:
1768: return (object);
1769: }
1770:
1771: /*
1772: * pool_cache_put:
1773: *
1774: * Put an object back to the pool cache.
1775: */
1776: void
1777: pool_cache_put(struct pool_cache *pc, void *object)
1778: {
1779: struct pool_cache_group *pcg;
1.60 thorpej 1780: int s;
1.43 thorpej 1781:
1782: simple_lock(&pc->pc_slock);
1783:
1784: if ((pcg = pc->pc_freeto) == NULL) {
1.60.4.1 fvdl 1785: TAILQ_FOREACH(pcg, &pc->pc_grouplist, pcg_list) {
1.43 thorpej 1786: if (pcg->pcg_avail != PCG_NOBJECTS) {
1787: pc->pc_freeto = pcg;
1788: goto have_group;
1789: }
1790: }
1791:
1792: /*
1793: * No empty groups to free the object to. Attempt to
1.47 thorpej 1794: * allocate one.
1.43 thorpej 1795: */
1.47 thorpej 1796: simple_unlock(&pc->pc_slock);
1.60 thorpej 1797: s = splvm();
1.43 thorpej 1798: pcg = pool_get(&pcgpool, PR_NOWAIT);
1.60 thorpej 1799: splx(s);
1.43 thorpej 1800: if (pcg != NULL) {
1801: memset(pcg, 0, sizeof(*pcg));
1.47 thorpej 1802: simple_lock(&pc->pc_slock);
1.48 thorpej 1803: pc->pc_ngroups++;
1.43 thorpej 1804: TAILQ_INSERT_TAIL(&pc->pc_grouplist, pcg, pcg_list);
1.47 thorpej 1805: if (pc->pc_freeto == NULL)
1806: pc->pc_freeto = pcg;
1.43 thorpej 1807: goto have_group;
1808: }
1809:
1810: /*
1811: * Unable to allocate a cache group; destruct the object
1812: * and free it back to the pool.
1813: */
1.51 thorpej 1814: pool_cache_destruct_object(pc, object);
1.43 thorpej 1815: return;
1816: }
1817:
1818: have_group:
1.48 thorpej 1819: pc->pc_nitems++;
1.43 thorpej 1820: pcg_put(pcg, object);
1821:
1822: if (pcg->pcg_avail == PCG_NOBJECTS)
1823: pc->pc_freeto = NULL;
1824:
1825: simple_unlock(&pc->pc_slock);
1.51 thorpej 1826: }
1827:
1828: /*
1829: * pool_cache_destruct_object:
1830: *
1831: * Force destruction of an object and its release back into
1832: * the pool.
1833: */
1834: void
1835: pool_cache_destruct_object(struct pool_cache *pc, void *object)
1836: {
1837:
1838: if (pc->pc_dtor != NULL)
1839: (*pc->pc_dtor)(pc->pc_arg, object);
1840: pool_put(pc->pc_pool, object);
1.43 thorpej 1841: }
1842:
1843: /*
1844: * pool_cache_do_invalidate:
1845: *
1846: * This internal function implements pool_cache_invalidate() and
1847: * pool_cache_reclaim().
1848: */
1849: static void
1850: pool_cache_do_invalidate(struct pool_cache *pc, int free_groups,
1.56 sommerfe 1851: void (*putit)(struct pool *, void *))
1.43 thorpej 1852: {
1853: struct pool_cache_group *pcg, *npcg;
1854: void *object;
1.60 thorpej 1855: int s;
1.43 thorpej 1856:
1857: for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL;
1858: pcg = npcg) {
1859: npcg = TAILQ_NEXT(pcg, pcg_list);
1860: while (pcg->pcg_avail != 0) {
1.48 thorpej 1861: pc->pc_nitems--;
1.43 thorpej 1862: object = pcg_get(pcg);
1.45 thorpej 1863: if (pcg->pcg_avail == 0 && pc->pc_allocfrom == pcg)
1864: pc->pc_allocfrom = NULL;
1.43 thorpej 1865: if (pc->pc_dtor != NULL)
1866: (*pc->pc_dtor)(pc->pc_arg, object);
1.56 sommerfe 1867: (*putit)(pc->pc_pool, object);
1.43 thorpej 1868: }
1869: if (free_groups) {
1.48 thorpej 1870: pc->pc_ngroups--;
1.43 thorpej 1871: TAILQ_REMOVE(&pc->pc_grouplist, pcg, pcg_list);
1.46 thorpej 1872: if (pc->pc_freeto == pcg)
1873: pc->pc_freeto = NULL;
1.60 thorpej 1874: s = splvm();
1.43 thorpej 1875: pool_put(&pcgpool, pcg);
1.60 thorpej 1876: splx(s);
1.43 thorpej 1877: }
1878: }
1879: }
1880:
1881: /*
1882: * pool_cache_invalidate:
1883: *
1884: * Invalidate a pool cache (destruct and release all of the
1885: * cached objects).
1886: */
1887: void
1888: pool_cache_invalidate(struct pool_cache *pc)
1889: {
1890:
1891: simple_lock(&pc->pc_slock);
1.56 sommerfe 1892: pool_cache_do_invalidate(pc, 0, pool_put);
1.43 thorpej 1893: simple_unlock(&pc->pc_slock);
1894: }
1895:
1896: /*
1897: * pool_cache_reclaim:
1898: *
1899: * Reclaim a pool cache for pool_reclaim().
1900: */
1901: static void
1902: pool_cache_reclaim(struct pool_cache *pc)
1903: {
1904:
1.47 thorpej 1905: simple_lock(&pc->pc_slock);
1.43 thorpej 1906: pool_cache_do_invalidate(pc, 1, pool_do_put);
1907: simple_unlock(&pc->pc_slock);
1.3 pk 1908: }
CVSweb <webmaster@jp.NetBSD.org>