Annotation of src/sys/kern/subr_pool.c, Revision 1.128.2.6
1.128.2.6! ad 1: /* $NetBSD: subr_pool.c,v 1.128.2.5 2007/07/29 11:34:47 ad Exp $ */
1.1 pk 2:
3: /*-
1.128.2.2 ad 4: * Copyright (c) 1997, 1999, 2000, 2002, 2007 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.64 lukem 39:
40: #include <sys/cdefs.h>
1.128.2.6! ad 41: __KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.128.2.5 2007/07/29 11:34:47 ad Exp $");
1.24 scottr 42:
1.25 thorpej 43: #include "opt_pool.h"
1.24 scottr 44: #include "opt_poollog.h"
1.28 thorpej 45: #include "opt_lockdebug.h"
1.1 pk 46:
47: #include <sys/param.h>
48: #include <sys/systm.h>
49: #include <sys/proc.h>
50: #include <sys/errno.h>
51: #include <sys/kernel.h>
52: #include <sys/malloc.h>
53: #include <sys/lock.h>
54: #include <sys/pool.h>
1.20 thorpej 55: #include <sys/syslog.h>
1.125 ad 56: #include <sys/debug.h>
1.128.2.5 ad 57: #include <sys/lockdebug.h>
1.3 pk 58:
59: #include <uvm/uvm.h>
60:
1.1 pk 61: /*
62: * Pool resource management utility.
1.3 pk 63: *
1.88 chs 64: * Memory is allocated in pages which are split into pieces according to
65: * the pool item size. Each page is kept on one of three lists in the
66: * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages',
67: * for empty, full and partially-full pages respectively. The individual
68: * pool items are on a linked list headed by `ph_itemlist' in each page
69: * header. The memory for building the page list is either taken from
70: * the allocated pages themselves (for small pool items) or taken from
71: * an internal pool of page headers (`phpool').
1.1 pk 72: */
73:
1.3 pk 74: /* List of all pools */
1.102 chs 75: LIST_HEAD(,pool) pool_head = LIST_HEAD_INITIALIZER(pool_head);
1.3 pk 76:
77: /* Private pool for page header structures */
1.97 yamt 78: #define PHPOOL_MAX 8
79: static struct pool phpool[PHPOOL_MAX];
80: #define PHPOOL_FREELIST_NELEM(idx) (((idx) == 0) ? 0 : (1 << (idx)))
1.3 pk 81:
1.62 bjh21 82: #ifdef POOL_SUBPAGE
83: /* Pool of subpages for use by normal pools. */
84: static struct pool psppool;
85: #endif
86:
1.117 yamt 87: static SLIST_HEAD(, pool_allocator) pa_deferinitq =
88: SLIST_HEAD_INITIALIZER(pa_deferinitq);
89:
1.98 yamt 90: static void *pool_page_alloc_meta(struct pool *, int);
91: static void pool_page_free_meta(struct pool *, void *);
92:
93: /* allocator for pool metadata */
94: static struct pool_allocator pool_allocator_meta = {
1.117 yamt 95: pool_page_alloc_meta, pool_page_free_meta,
96: .pa_backingmapptr = &kmem_map,
1.98 yamt 97: };
98:
1.3 pk 99: /* # of seconds to retain page after last use */
100: int pool_inactive_time = 10;
101:
102: /* Next candidate for drainage (see pool_drain()) */
1.23 thorpej 103: static struct pool *drainpp;
104:
1.128.2.2 ad 105: /* This lock protects both pool_head and drainpp. */
106: static kmutex_t pool_head_lock;
1.3 pk 107:
1.99 yamt 108: typedef uint8_t pool_item_freelist_t;
109:
1.3 pk 110: struct pool_item_header {
111: /* Page headers */
1.88 chs 112: LIST_ENTRY(pool_item_header)
1.3 pk 113: ph_pagelist; /* pool page list */
1.88 chs 114: SPLAY_ENTRY(pool_item_header)
115: ph_node; /* Off-page page headers */
1.128 christos 116: void * ph_page; /* this page's address */
1.3 pk 117: struct timeval ph_time; /* last referenced */
1.97 yamt 118: union {
119: /* !PR_NOTOUCH */
120: struct {
1.102 chs 121: LIST_HEAD(, pool_item)
1.97 yamt 122: phu_itemlist; /* chunk list for this page */
123: } phu_normal;
124: /* PR_NOTOUCH */
125: struct {
126: uint16_t
127: phu_off; /* start offset in page */
1.99 yamt 128: pool_item_freelist_t
1.97 yamt 129: phu_firstfree; /* first free item */
1.99 yamt 130: /*
131: * XXX it might be better to use
132: * a simple bitmap and ffs(3)
133: */
1.97 yamt 134: } phu_notouch;
135: } ph_u;
136: uint16_t ph_nmissing; /* # of chunks in use */
1.3 pk 137: };
1.97 yamt 138: #define ph_itemlist ph_u.phu_normal.phu_itemlist
139: #define ph_off ph_u.phu_notouch.phu_off
140: #define ph_firstfree ph_u.phu_notouch.phu_firstfree
1.3 pk 141:
1.1 pk 142: struct pool_item {
1.3 pk 143: #ifdef DIAGNOSTIC
1.82 thorpej 144: u_int pi_magic;
1.33 chs 145: #endif
1.82 thorpej 146: #define PI_MAGIC 0xdeadbeefU
1.3 pk 147: /* Other entries use only this list entry */
1.102 chs 148: LIST_ENTRY(pool_item) pi_list;
1.3 pk 149: };
150:
1.53 thorpej 151: #define POOL_NEEDS_CATCHUP(pp) \
152: ((pp)->pr_nitems < (pp)->pr_minitems)
153:
1.43 thorpej 154: /*
155: * Pool cache management.
156: *
157: * Pool caches provide a way for constructed objects to be cached by the
158: * pool subsystem. This can lead to performance improvements by avoiding
159: * needless object construction/destruction; it is deferred until absolutely
160: * necessary.
161: *
162: * Caches are grouped into cache groups. Each cache group references
163: * up to 16 constructed objects. When a cache allocates an object
164: * from the pool, it calls the object's constructor and places it into
165: * a cache group. When a cache group frees an object back to the pool,
166: * it first calls the object's destructor. This allows the object to
167: * persist in constructed form while freed to the cache.
168: *
169: * Multiple caches may exist for each pool. This allows a single
170: * object type to have multiple constructed forms. The pool references
171: * each cache, so that when a pool is drained by the pagedaemon, it can
172: * drain each individual cache as well. Each time a cache is drained,
173: * the most idle cache group is freed to the pool in its entirety.
174: *
175: * Pool caches are layed on top of pools. By layering them, we can avoid
176: * the complexity of cache management for pools which would not benefit
177: * from it.
178: */
179:
180: /* The cache group pool. */
181: static struct pool pcgpool;
1.3 pk 182:
1.102 chs 183: static void pool_cache_reclaim(struct pool_cache *, struct pool_pagelist *,
184: struct pool_cache_grouplist *);
185: static void pcg_grouplist_free(struct pool_cache_grouplist *);
1.3 pk 186:
1.42 thorpej 187: static int pool_catchup(struct pool *);
1.128 christos 188: static void pool_prime_page(struct pool *, void *,
1.55 thorpej 189: struct pool_item_header *);
1.88 chs 190: static void pool_update_curpage(struct pool *);
1.66 thorpej 191:
1.113 yamt 192: static int pool_grow(struct pool *, int);
1.117 yamt 193: static void *pool_allocator_alloc(struct pool *, int);
194: static void pool_allocator_free(struct pool *, void *);
1.3 pk 195:
1.97 yamt 196: static void pool_print_pagelist(struct pool *, struct pool_pagelist *,
1.88 chs 197: void (*)(const char *, ...));
1.42 thorpej 198: static void pool_print1(struct pool *, const char *,
199: void (*)(const char *, ...));
1.3 pk 200:
1.88 chs 201: static int pool_chk_page(struct pool *, const char *,
202: struct pool_item_header *);
203:
1.3 pk 204: /*
1.52 thorpej 205: * Pool log entry. An array of these is allocated in pool_init().
1.3 pk 206: */
207: struct pool_log {
208: const char *pl_file;
209: long pl_line;
210: int pl_action;
1.25 thorpej 211: #define PRLOG_GET 1
212: #define PRLOG_PUT 2
1.3 pk 213: void *pl_addr;
1.1 pk 214: };
215:
1.86 matt 216: #ifdef POOL_DIAGNOSTIC
1.3 pk 217: /* Number of entries in pool log buffers */
1.17 thorpej 218: #ifndef POOL_LOGSIZE
219: #define POOL_LOGSIZE 10
220: #endif
221:
222: int pool_logsize = POOL_LOGSIZE;
1.1 pk 223:
1.110 perry 224: static inline void
1.42 thorpej 225: pr_log(struct pool *pp, void *v, int action, const char *file, long line)
1.3 pk 226: {
227: int n = pp->pr_curlogentry;
228: struct pool_log *pl;
229:
1.20 thorpej 230: if ((pp->pr_roflags & PR_LOGGING) == 0)
1.3 pk 231: return;
232:
233: /*
234: * Fill in the current entry. Wrap around and overwrite
235: * the oldest entry if necessary.
236: */
237: pl = &pp->pr_log[n];
238: pl->pl_file = file;
239: pl->pl_line = line;
240: pl->pl_action = action;
241: pl->pl_addr = v;
242: if (++n >= pp->pr_logsize)
243: n = 0;
244: pp->pr_curlogentry = n;
245: }
246:
247: static void
1.42 thorpej 248: pr_printlog(struct pool *pp, struct pool_item *pi,
249: void (*pr)(const char *, ...))
1.3 pk 250: {
251: int i = pp->pr_logsize;
252: int n = pp->pr_curlogentry;
253:
1.20 thorpej 254: if ((pp->pr_roflags & PR_LOGGING) == 0)
1.3 pk 255: return;
256:
257: /*
258: * Print all entries in this pool's log.
259: */
260: while (i-- > 0) {
261: struct pool_log *pl = &pp->pr_log[n];
262: if (pl->pl_action != 0) {
1.25 thorpej 263: if (pi == NULL || pi == pl->pl_addr) {
264: (*pr)("\tlog entry %d:\n", i);
265: (*pr)("\t\taction = %s, addr = %p\n",
266: pl->pl_action == PRLOG_GET ? "get" : "put",
267: pl->pl_addr);
268: (*pr)("\t\tfile: %s at line %lu\n",
269: pl->pl_file, pl->pl_line);
270: }
1.3 pk 271: }
272: if (++n >= pp->pr_logsize)
273: n = 0;
274: }
275: }
1.25 thorpej 276:
1.110 perry 277: static inline void
1.42 thorpej 278: pr_enter(struct pool *pp, const char *file, long line)
1.25 thorpej 279: {
280:
1.34 thorpej 281: if (__predict_false(pp->pr_entered_file != NULL)) {
1.25 thorpej 282: printf("pool %s: reentrancy at file %s line %ld\n",
283: pp->pr_wchan, file, line);
284: printf(" previous entry at file %s line %ld\n",
285: pp->pr_entered_file, pp->pr_entered_line);
286: panic("pr_enter");
287: }
288:
289: pp->pr_entered_file = file;
290: pp->pr_entered_line = line;
291: }
292:
1.110 perry 293: static inline void
1.42 thorpej 294: pr_leave(struct pool *pp)
1.25 thorpej 295: {
296:
1.34 thorpej 297: if (__predict_false(pp->pr_entered_file == NULL)) {
1.25 thorpej 298: printf("pool %s not entered?\n", pp->pr_wchan);
299: panic("pr_leave");
300: }
301:
302: pp->pr_entered_file = NULL;
303: pp->pr_entered_line = 0;
304: }
305:
1.110 perry 306: static inline void
1.42 thorpej 307: pr_enter_check(struct pool *pp, void (*pr)(const char *, ...))
1.25 thorpej 308: {
309:
310: if (pp->pr_entered_file != NULL)
311: (*pr)("\n\tcurrently entered from file %s line %ld\n",
312: pp->pr_entered_file, pp->pr_entered_line);
313: }
1.3 pk 314: #else
1.25 thorpej 315: #define pr_log(pp, v, action, file, line)
316: #define pr_printlog(pp, pi, pr)
317: #define pr_enter(pp, file, line)
318: #define pr_leave(pp)
319: #define pr_enter_check(pp, pr)
1.59 thorpej 320: #endif /* POOL_DIAGNOSTIC */
1.3 pk 321:
1.110 perry 322: static inline int
1.97 yamt 323: pr_item_notouch_index(const struct pool *pp, const struct pool_item_header *ph,
324: const void *v)
325: {
326: const char *cp = v;
327: int idx;
328:
329: KASSERT(pp->pr_roflags & PR_NOTOUCH);
1.128 christos 330: idx = (cp - (char *)ph->ph_page - ph->ph_off) / pp->pr_size;
1.97 yamt 331: KASSERT(idx < pp->pr_itemsperpage);
332: return idx;
333: }
334:
1.99 yamt 335: #define PR_FREELIST_ALIGN(p) \
336: roundup((uintptr_t)(p), sizeof(pool_item_freelist_t))
337: #define PR_FREELIST(ph) ((pool_item_freelist_t *)PR_FREELIST_ALIGN((ph) + 1))
338: #define PR_INDEX_USED ((pool_item_freelist_t)-1)
339: #define PR_INDEX_EOL ((pool_item_freelist_t)-2)
1.97 yamt 340:
1.110 perry 341: static inline void
1.97 yamt 342: pr_item_notouch_put(const struct pool *pp, struct pool_item_header *ph,
343: void *obj)
344: {
345: int idx = pr_item_notouch_index(pp, ph, obj);
1.99 yamt 346: pool_item_freelist_t *freelist = PR_FREELIST(ph);
1.97 yamt 347:
348: KASSERT(freelist[idx] == PR_INDEX_USED);
349: freelist[idx] = ph->ph_firstfree;
350: ph->ph_firstfree = idx;
351: }
352:
1.110 perry 353: static inline void *
1.97 yamt 354: pr_item_notouch_get(const struct pool *pp, struct pool_item_header *ph)
355: {
356: int idx = ph->ph_firstfree;
1.99 yamt 357: pool_item_freelist_t *freelist = PR_FREELIST(ph);
1.97 yamt 358:
359: KASSERT(freelist[idx] != PR_INDEX_USED);
360: ph->ph_firstfree = freelist[idx];
361: freelist[idx] = PR_INDEX_USED;
362:
1.128 christos 363: return (char *)ph->ph_page + ph->ph_off + idx * pp->pr_size;
1.97 yamt 364: }
365:
1.110 perry 366: static inline int
1.88 chs 367: phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
368: {
1.121 yamt 369:
370: /*
371: * we consider pool_item_header with smaller ph_page bigger.
372: * (this unnatural ordering is for the benefit of pr_find_pagehead.)
373: */
374:
1.88 chs 375: if (a->ph_page < b->ph_page)
1.121 yamt 376: return (1);
377: else if (a->ph_page > b->ph_page)
1.88 chs 378: return (-1);
379: else
380: return (0);
381: }
382:
383: SPLAY_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare);
384: SPLAY_GENERATE(phtree, pool_item_header, ph_node, phtree_compare);
385:
1.3 pk 386: /*
1.121 yamt 387: * Return the pool page header based on item address.
1.3 pk 388: */
1.110 perry 389: static inline struct pool_item_header *
1.121 yamt 390: pr_find_pagehead(struct pool *pp, void *v)
1.3 pk 391: {
1.88 chs 392: struct pool_item_header *ph, tmp;
1.3 pk 393:
1.121 yamt 394: if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1.128 christos 395: tmp.ph_page = (void *)(uintptr_t)v;
1.121 yamt 396: ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
397: if (ph == NULL) {
398: ph = SPLAY_ROOT(&pp->pr_phtree);
399: if (ph != NULL && phtree_compare(&tmp, ph) >= 0) {
400: ph = SPLAY_NEXT(phtree, &pp->pr_phtree, ph);
401: }
402: KASSERT(ph == NULL || phtree_compare(&tmp, ph) < 0);
403: }
404: } else {
1.128 christos 405: void *page =
406: (void *)((uintptr_t)v & pp->pr_alloc->pa_pagemask);
1.121 yamt 407:
408: if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
1.128 christos 409: ph = (struct pool_item_header *)((char *)page + pp->pr_phoffset);
1.121 yamt 410: } else {
411: tmp.ph_page = page;
412: ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
413: }
414: }
1.3 pk 415:
1.121 yamt 416: KASSERT(ph == NULL || ((pp->pr_roflags & PR_PHINPAGE) != 0) ||
1.128 christos 417: ((char *)ph->ph_page <= (char *)v &&
418: (char *)v < (char *)ph->ph_page + pp->pr_alloc->pa_pagesz));
1.88 chs 419: return ph;
1.3 pk 420: }
421:
1.101 thorpej 422: static void
423: pr_pagelist_free(struct pool *pp, struct pool_pagelist *pq)
424: {
425: struct pool_item_header *ph;
426:
427: while ((ph = LIST_FIRST(pq)) != NULL) {
428: LIST_REMOVE(ph, ph_pagelist);
429: pool_allocator_free(pp, ph->ph_page);
1.128.2.2 ad 430: if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1.101 thorpej 431: pool_put(pp->pr_phpool, ph);
432: }
433: }
434:
1.3 pk 435: /*
436: * Remove a page from the pool.
437: */
1.110 perry 438: static inline void
1.61 chs 439: pr_rmpage(struct pool *pp, struct pool_item_header *ph,
440: struct pool_pagelist *pq)
1.3 pk 441: {
442:
1.128.2.2 ad 443: KASSERT(mutex_owned(&pp->pr_lock));
1.91 yamt 444:
1.3 pk 445: /*
1.7 thorpej 446: * If the page was idle, decrement the idle page count.
1.3 pk 447: */
1.6 thorpej 448: if (ph->ph_nmissing == 0) {
449: #ifdef DIAGNOSTIC
450: if (pp->pr_nidle == 0)
451: panic("pr_rmpage: nidle inconsistent");
1.20 thorpej 452: if (pp->pr_nitems < pp->pr_itemsperpage)
453: panic("pr_rmpage: nitems inconsistent");
1.6 thorpej 454: #endif
455: pp->pr_nidle--;
456: }
1.7 thorpej 457:
1.20 thorpej 458: pp->pr_nitems -= pp->pr_itemsperpage;
459:
1.7 thorpej 460: /*
1.101 thorpej 461: * Unlink the page from the pool and queue it for release.
1.7 thorpej 462: */
1.88 chs 463: LIST_REMOVE(ph, ph_pagelist);
1.91 yamt 464: if ((pp->pr_roflags & PR_PHINPAGE) == 0)
465: SPLAY_REMOVE(phtree, &pp->pr_phtree, ph);
1.101 thorpej 466: LIST_INSERT_HEAD(pq, ph, ph_pagelist);
467:
1.7 thorpej 468: pp->pr_npages--;
469: pp->pr_npagefree++;
1.6 thorpej 470:
1.88 chs 471: pool_update_curpage(pp);
1.3 pk 472: }
473:
1.126 thorpej 474: static bool
1.117 yamt 475: pa_starved_p(struct pool_allocator *pa)
476: {
477:
478: if (pa->pa_backingmap != NULL) {
479: return vm_map_starved_p(pa->pa_backingmap);
480: }
1.127 thorpej 481: return false;
1.117 yamt 482: }
483:
484: static int
1.124 yamt 485: pool_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
1.117 yamt 486: {
487: struct pool *pp = obj;
488: struct pool_allocator *pa = pp->pr_alloc;
489:
490: KASSERT(&pp->pr_reclaimerentry == ce);
491: pool_reclaim(pp);
492: if (!pa_starved_p(pa)) {
493: return CALLBACK_CHAIN_ABORT;
494: }
495: return CALLBACK_CHAIN_CONTINUE;
496: }
497:
498: static void
499: pool_reclaim_register(struct pool *pp)
500: {
501: struct vm_map *map = pp->pr_alloc->pa_backingmap;
502: int s;
503:
504: if (map == NULL) {
505: return;
506: }
507:
508: s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
509: callback_register(&vm_map_to_kernel(map)->vmk_reclaim_callback,
510: &pp->pr_reclaimerentry, pp, pool_reclaim_callback);
511: splx(s);
512: }
513:
514: static void
515: pool_reclaim_unregister(struct pool *pp)
516: {
517: struct vm_map *map = pp->pr_alloc->pa_backingmap;
518: int s;
519:
520: if (map == NULL) {
521: return;
522: }
523:
524: s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
525: callback_unregister(&vm_map_to_kernel(map)->vmk_reclaim_callback,
526: &pp->pr_reclaimerentry);
527: splx(s);
528: }
529:
530: static void
531: pa_reclaim_register(struct pool_allocator *pa)
532: {
533: struct vm_map *map = *pa->pa_backingmapptr;
534: struct pool *pp;
535:
536: KASSERT(pa->pa_backingmap == NULL);
537: if (map == NULL) {
538: SLIST_INSERT_HEAD(&pa_deferinitq, pa, pa_q);
539: return;
540: }
541: pa->pa_backingmap = map;
542: TAILQ_FOREACH(pp, &pa->pa_list, pr_alloc_list) {
543: pool_reclaim_register(pp);
544: }
545: }
546:
1.3 pk 547: /*
1.94 simonb 548: * Initialize all the pools listed in the "pools" link set.
549: */
550: void
1.117 yamt 551: pool_subsystem_init(void)
1.94 simonb 552: {
1.117 yamt 553: struct pool_allocator *pa;
1.94 simonb 554: __link_set_decl(pools, struct link_pool_init);
555: struct link_pool_init * const *pi;
556:
1.128.2.2 ad 557: mutex_init(&pool_head_lock, MUTEX_DEFAULT, IPL_NONE);
558:
1.94 simonb 559: __link_set_foreach(pi, pools)
560: pool_init((*pi)->pp, (*pi)->size, (*pi)->align,
561: (*pi)->align_offset, (*pi)->flags, (*pi)->wchan,
1.128.2.1 ad 562: (*pi)->palloc, (*pi)->ipl);
1.117 yamt 563:
564: while ((pa = SLIST_FIRST(&pa_deferinitq)) != NULL) {
565: KASSERT(pa->pa_backingmapptr != NULL);
566: KASSERT(*pa->pa_backingmapptr != NULL);
567: SLIST_REMOVE_HEAD(&pa_deferinitq, pa_q);
568: pa_reclaim_register(pa);
569: }
1.94 simonb 570: }
571:
572: /*
1.3 pk 573: * Initialize the given pool resource structure.
574: *
575: * We export this routine to allow other kernel parts to declare
576: * static pools that must be initialized before malloc() is available.
577: */
578: void
1.42 thorpej 579: pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
1.128.2.1 ad 580: const char *wchan, struct pool_allocator *palloc, int ipl)
1.3 pk 581: {
1.116 simonb 582: #ifdef DEBUG
583: struct pool *pp1;
584: #endif
1.92 enami 585: size_t trysize, phsize;
1.128.2.2 ad 586: int off, slack;
1.3 pk 587:
1.99 yamt 588: KASSERT((1UL << (CHAR_BIT * sizeof(pool_item_freelist_t))) - 2 >=
589: PHPOOL_FREELIST_NELEM(PHPOOL_MAX - 1));
590:
1.116 simonb 591: #ifdef DEBUG
592: /*
593: * Check that the pool hasn't already been initialised and
594: * added to the list of all pools.
595: */
596: LIST_FOREACH(pp1, &pool_head, pr_poollist) {
597: if (pp == pp1)
598: panic("pool_init: pool %s already initialised",
599: wchan);
600: }
601: #endif
602:
1.25 thorpej 603: #ifdef POOL_DIAGNOSTIC
604: /*
605: * Always log if POOL_DIAGNOSTIC is defined.
606: */
607: if (pool_logsize != 0)
608: flags |= PR_LOGGING;
609: #endif
610:
1.66 thorpej 611: if (palloc == NULL)
612: palloc = &pool_allocator_kmem;
1.112 bjh21 613: #ifdef POOL_SUBPAGE
614: if (size > palloc->pa_pagesz) {
615: if (palloc == &pool_allocator_kmem)
616: palloc = &pool_allocator_kmem_fullpage;
617: else if (palloc == &pool_allocator_nointr)
618: palloc = &pool_allocator_nointr_fullpage;
619: }
1.66 thorpej 620: #endif /* POOL_SUBPAGE */
621: if ((palloc->pa_flags & PA_INITIALIZED) == 0) {
1.112 bjh21 622: if (palloc->pa_pagesz == 0)
1.66 thorpej 623: palloc->pa_pagesz = PAGE_SIZE;
624:
625: TAILQ_INIT(&palloc->pa_list);
626:
1.128.2.2 ad 627: mutex_init(&palloc->pa_lock, MUTEX_DRIVER, IPL_VM);
1.66 thorpej 628: palloc->pa_pagemask = ~(palloc->pa_pagesz - 1);
629: palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1;
1.117 yamt 630:
631: if (palloc->pa_backingmapptr != NULL) {
632: pa_reclaim_register(palloc);
633: }
1.66 thorpej 634: palloc->pa_flags |= PA_INITIALIZED;
1.4 thorpej 635: }
1.3 pk 636:
637: if (align == 0)
638: align = ALIGN(1);
1.14 thorpej 639:
1.120 yamt 640: if ((flags & PR_NOTOUCH) == 0 && size < sizeof(struct pool_item))
1.14 thorpej 641: size = sizeof(struct pool_item);
1.3 pk 642:
1.78 thorpej 643: size = roundup(size, align);
1.66 thorpej 644: #ifdef DIAGNOSTIC
645: if (size > palloc->pa_pagesz)
1.121 yamt 646: panic("pool_init: pool item size (%zu) too large", size);
1.66 thorpej 647: #endif
1.35 pk 648:
1.3 pk 649: /*
650: * Initialize the pool structure.
651: */
1.88 chs 652: LIST_INIT(&pp->pr_emptypages);
653: LIST_INIT(&pp->pr_fullpages);
654: LIST_INIT(&pp->pr_partpages);
1.102 chs 655: LIST_INIT(&pp->pr_cachelist);
1.3 pk 656: pp->pr_curpage = NULL;
657: pp->pr_npages = 0;
658: pp->pr_minitems = 0;
659: pp->pr_minpages = 0;
660: pp->pr_maxpages = UINT_MAX;
1.20 thorpej 661: pp->pr_roflags = flags;
662: pp->pr_flags = 0;
1.35 pk 663: pp->pr_size = size;
1.3 pk 664: pp->pr_align = align;
665: pp->pr_wchan = wchan;
1.66 thorpej 666: pp->pr_alloc = palloc;
1.20 thorpej 667: pp->pr_nitems = 0;
668: pp->pr_nout = 0;
669: pp->pr_hardlimit = UINT_MAX;
670: pp->pr_hardlimit_warning = NULL;
1.31 thorpej 671: pp->pr_hardlimit_ratecap.tv_sec = 0;
672: pp->pr_hardlimit_ratecap.tv_usec = 0;
673: pp->pr_hardlimit_warning_last.tv_sec = 0;
674: pp->pr_hardlimit_warning_last.tv_usec = 0;
1.68 thorpej 675: pp->pr_drain_hook = NULL;
676: pp->pr_drain_hook_arg = NULL;
1.125 ad 677: pp->pr_freecheck = NULL;
1.3 pk 678:
679: /*
680: * Decide whether to put the page header off page to avoid
1.92 enami 681: * wasting too large a part of the page or too big item.
682: * Off-page page headers go on a hash table, so we can match
683: * a returned item with its header based on the page address.
684: * We use 1/16 of the page size and about 8 times of the item
685: * size as the threshold (XXX: tune)
686: *
687: * However, we'll put the header into the page if we can put
688: * it without wasting any items.
689: *
690: * Silently enforce `0 <= ioff < align'.
1.3 pk 691: */
1.92 enami 692: pp->pr_itemoffset = ioff %= align;
693: /* See the comment below about reserved bytes. */
694: trysize = palloc->pa_pagesz - ((align - ioff) % align);
695: phsize = ALIGN(sizeof(struct pool_item_header));
1.121 yamt 696: if ((pp->pr_roflags & (PR_NOTOUCH | PR_NOALIGN)) == 0 &&
1.97 yamt 697: (pp->pr_size < MIN(palloc->pa_pagesz / 16, phsize << 3) ||
698: trysize / pp->pr_size == (trysize - phsize) / pp->pr_size)) {
1.3 pk 699: /* Use the end of the page for the page header */
1.20 thorpej 700: pp->pr_roflags |= PR_PHINPAGE;
1.92 enami 701: pp->pr_phoffset = off = palloc->pa_pagesz - phsize;
1.2 pk 702: } else {
1.3 pk 703: /* The page header will be taken from our page header pool */
704: pp->pr_phoffset = 0;
1.66 thorpej 705: off = palloc->pa_pagesz;
1.88 chs 706: SPLAY_INIT(&pp->pr_phtree);
1.2 pk 707: }
1.1 pk 708:
1.3 pk 709: /*
710: * Alignment is to take place at `ioff' within the item. This means
711: * we must reserve up to `align - 1' bytes on the page to allow
712: * appropriate positioning of each item.
713: */
714: pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
1.43 thorpej 715: KASSERT(pp->pr_itemsperpage != 0);
1.97 yamt 716: if ((pp->pr_roflags & PR_NOTOUCH)) {
717: int idx;
718:
719: for (idx = 0; pp->pr_itemsperpage > PHPOOL_FREELIST_NELEM(idx);
720: idx++) {
721: /* nothing */
722: }
723: if (idx >= PHPOOL_MAX) {
724: /*
725: * if you see this panic, consider to tweak
726: * PHPOOL_MAX and PHPOOL_FREELIST_NELEM.
727: */
728: panic("%s: too large itemsperpage(%d) for PR_NOTOUCH",
729: pp->pr_wchan, pp->pr_itemsperpage);
730: }
731: pp->pr_phpool = &phpool[idx];
732: } else if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
733: pp->pr_phpool = &phpool[0];
734: }
735: #if defined(DIAGNOSTIC)
736: else {
737: pp->pr_phpool = NULL;
738: }
739: #endif
1.3 pk 740:
741: /*
742: * Use the slack between the chunks and the page header
743: * for "cache coloring".
744: */
745: slack = off - pp->pr_itemsperpage * pp->pr_size;
746: pp->pr_maxcolor = (slack / align) * align;
747: pp->pr_curcolor = 0;
748:
749: pp->pr_nget = 0;
750: pp->pr_nfail = 0;
751: pp->pr_nput = 0;
752: pp->pr_npagealloc = 0;
753: pp->pr_npagefree = 0;
1.1 pk 754: pp->pr_hiwat = 0;
1.8 thorpej 755: pp->pr_nidle = 0;
1.3 pk 756:
1.59 thorpej 757: #ifdef POOL_DIAGNOSTIC
1.25 thorpej 758: if (flags & PR_LOGGING) {
759: if (kmem_map == NULL ||
760: (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
761: M_TEMP, M_NOWAIT)) == NULL)
1.20 thorpej 762: pp->pr_roflags &= ~PR_LOGGING;
1.3 pk 763: pp->pr_curlogentry = 0;
764: pp->pr_logsize = pool_logsize;
765: }
1.59 thorpej 766: #endif
1.25 thorpej 767:
768: pp->pr_entered_file = NULL;
769: pp->pr_entered_line = 0;
1.3 pk 770:
1.128.2.2 ad 771: mutex_init(&pp->pr_lock, MUTEX_DRIVER, ipl);
772: cv_init(&pp->pr_cv, wchan);
773: pp->pr_ipl = ipl;
774:
1.3 pk 775: /*
1.43 thorpej 776: * Initialize private page header pool and cache magazine pool if we
777: * haven't done so yet.
1.23 thorpej 778: * XXX LOCKING.
1.3 pk 779: */
1.97 yamt 780: if (phpool[0].pr_size == 0) {
781: int idx;
782: for (idx = 0; idx < PHPOOL_MAX; idx++) {
783: static char phpool_names[PHPOOL_MAX][6+1+6+1];
784: int nelem;
785: size_t sz;
786:
787: nelem = PHPOOL_FREELIST_NELEM(idx);
788: snprintf(phpool_names[idx], sizeof(phpool_names[idx]),
789: "phpool-%d", nelem);
790: sz = sizeof(struct pool_item_header);
791: if (nelem) {
792: sz = PR_FREELIST_ALIGN(sz)
1.99 yamt 793: + nelem * sizeof(pool_item_freelist_t);
1.97 yamt 794: }
795: pool_init(&phpool[idx], sz, 0, 0, 0,
1.128.2.1 ad 796: phpool_names[idx], &pool_allocator_meta, IPL_VM);
1.97 yamt 797: }
1.62 bjh21 798: #ifdef POOL_SUBPAGE
799: pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0,
1.128.2.1 ad 800: PR_RECURSIVE, "psppool", &pool_allocator_meta, IPL_VM);
1.62 bjh21 801: #endif
1.43 thorpej 802: pool_init(&pcgpool, sizeof(struct pool_cache_group), 0, 0,
1.128.2.1 ad 803: 0, "pcgpool", &pool_allocator_meta, IPL_VM);
1.1 pk 804: }
805:
1.128.2.2 ad 806: if (__predict_true(!cold)) {
807: /* Insert into the list of all pools. */
808: mutex_enter(&pool_head_lock);
809: LIST_INSERT_HEAD(&pool_head, pp, pr_poollist);
810: mutex_exit(&pool_head_lock);
811:
812: /* Insert this into the list of pools using this allocator. */
813: mutex_enter(&palloc->pa_lock);
814: TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
815: mutex_exit(&palloc->pa_lock);
816: } else {
817: LIST_INSERT_HEAD(&pool_head, pp, pr_poollist);
818: TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
819: }
820:
1.117 yamt 821: pool_reclaim_register(pp);
1.1 pk 822: }
823:
824: /*
825: * De-commision a pool resource.
826: */
827: void
1.42 thorpej 828: pool_destroy(struct pool *pp)
1.1 pk 829: {
1.101 thorpej 830: struct pool_pagelist pq;
1.3 pk 831: struct pool_item_header *ph;
1.43 thorpej 832:
1.101 thorpej 833: /* Remove from global pool list */
1.128.2.2 ad 834: mutex_enter(&pool_head_lock);
1.102 chs 835: LIST_REMOVE(pp, pr_poollist);
1.101 thorpej 836: if (drainpp == pp)
837: drainpp = NULL;
1.128.2.2 ad 838: mutex_exit(&pool_head_lock);
1.101 thorpej 839:
840: /* Remove this pool from its allocator's list of pools. */
1.117 yamt 841: pool_reclaim_unregister(pp);
1.128.2.2 ad 842: mutex_enter(&pp->pr_alloc->pa_lock);
1.66 thorpej 843: TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list);
1.128.2.2 ad 844: mutex_exit(&pp->pr_alloc->pa_lock);
1.66 thorpej 845:
1.128.2.2 ad 846: mutex_enter(&pp->pr_lock);
1.101 thorpej 847:
1.102 chs 848: KASSERT(LIST_EMPTY(&pp->pr_cachelist));
1.3 pk 849:
850: #ifdef DIAGNOSTIC
1.20 thorpej 851: if (pp->pr_nout != 0) {
1.25 thorpej 852: pr_printlog(pp, NULL, printf);
1.80 provos 853: panic("pool_destroy: pool busy: still out: %u",
1.20 thorpej 854: pp->pr_nout);
1.3 pk 855: }
856: #endif
1.1 pk 857:
1.101 thorpej 858: KASSERT(LIST_EMPTY(&pp->pr_fullpages));
859: KASSERT(LIST_EMPTY(&pp->pr_partpages));
860:
1.3 pk 861: /* Remove all pages */
1.101 thorpej 862: LIST_INIT(&pq);
1.88 chs 863: while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1.101 thorpej 864: pr_rmpage(pp, ph, &pq);
865:
1.128.2.2 ad 866: mutex_exit(&pp->pr_lock);
1.3 pk 867:
1.101 thorpej 868: pr_pagelist_free(pp, &pq);
1.3 pk 869:
1.59 thorpej 870: #ifdef POOL_DIAGNOSTIC
1.20 thorpej 871: if ((pp->pr_roflags & PR_LOGGING) != 0)
1.3 pk 872: free(pp->pr_log, M_TEMP);
1.59 thorpej 873: #endif
1.128.2.2 ad 874:
875: cv_destroy(&pp->pr_cv);
876: mutex_destroy(&pp->pr_lock);
1.1 pk 877: }
878:
1.68 thorpej 879: void
880: pool_set_drain_hook(struct pool *pp, void (*fn)(void *, int), void *arg)
881: {
882:
883: /* XXX no locking -- must be used just after pool_init() */
884: #ifdef DIAGNOSTIC
885: if (pp->pr_drain_hook != NULL)
886: panic("pool_set_drain_hook(%s): already set", pp->pr_wchan);
887: #endif
888: pp->pr_drain_hook = fn;
889: pp->pr_drain_hook_arg = arg;
890: }
891:
1.88 chs 892: static struct pool_item_header *
1.128 christos 893: pool_alloc_item_header(struct pool *pp, void *storage, int flags)
1.55 thorpej 894: {
895: struct pool_item_header *ph;
896:
897: if ((pp->pr_roflags & PR_PHINPAGE) != 0)
1.128 christos 898: ph = (struct pool_item_header *) ((char *)storage + pp->pr_phoffset);
1.128.2.2 ad 899: else
1.97 yamt 900: ph = pool_get(pp->pr_phpool, flags);
1.55 thorpej 901:
902: return (ph);
903: }
1.1 pk 904:
905: /*
1.3 pk 906: * Grab an item from the pool; must be called at appropriate spl level
1.1 pk 907: */
1.3 pk 908: void *
1.59 thorpej 909: #ifdef POOL_DIAGNOSTIC
1.42 thorpej 910: _pool_get(struct pool *pp, int flags, const char *file, long line)
1.56 sommerfe 911: #else
912: pool_get(struct pool *pp, int flags)
913: #endif
1.1 pk 914: {
915: struct pool_item *pi;
1.3 pk 916: struct pool_item_header *ph;
1.55 thorpej 917: void *v;
1.1 pk 918:
1.2 pk 919: #ifdef DIAGNOSTIC
1.95 atatat 920: if (__predict_false(pp->pr_itemsperpage == 0))
921: panic("pool_get: pool %p: pr_itemsperpage is zero, "
922: "pool not initialized?", pp);
1.84 thorpej 923: if (__predict_false(curlwp == NULL && doing_shutdown == 0 &&
1.37 sommerfe 924: (flags & PR_WAITOK) != 0))
1.77 matt 925: panic("pool_get: %s: must have NOWAIT", pp->pr_wchan);
1.58 thorpej 926:
1.102 chs 927: #endif /* DIAGNOSTIC */
1.58 thorpej 928: #ifdef LOCKDEBUG
929: if (flags & PR_WAITOK)
1.119 yamt 930: ASSERT_SLEEPABLE(NULL, "pool_get(PR_WAITOK)");
1.56 sommerfe 931: #endif
1.1 pk 932:
1.128.2.2 ad 933: mutex_enter(&pp->pr_lock);
1.25 thorpej 934: pr_enter(pp, file, line);
1.20 thorpej 935:
936: startover:
937: /*
938: * Check to see if we've reached the hard limit. If we have,
939: * and we can wait, then wait until an item has been returned to
940: * the pool.
941: */
942: #ifdef DIAGNOSTIC
1.34 thorpej 943: if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
1.25 thorpej 944: pr_leave(pp);
1.128.2.2 ad 945: mutex_exit(&pp->pr_lock);
1.20 thorpej 946: panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
947: }
948: #endif
1.34 thorpej 949: if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
1.68 thorpej 950: if (pp->pr_drain_hook != NULL) {
951: /*
952: * Since the drain hook is going to free things
953: * back to the pool, unlock, call the hook, re-lock,
954: * and check the hardlimit condition again.
955: */
956: pr_leave(pp);
1.128.2.2 ad 957: mutex_exit(&pp->pr_lock);
1.68 thorpej 958: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
1.128.2.2 ad 959: mutex_enter(&pp->pr_lock);
1.68 thorpej 960: pr_enter(pp, file, line);
961: if (pp->pr_nout < pp->pr_hardlimit)
962: goto startover;
963: }
964:
1.29 sommerfe 965: if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
1.20 thorpej 966: /*
967: * XXX: A warning isn't logged in this case. Should
968: * it be?
969: */
970: pp->pr_flags |= PR_WANTED;
1.25 thorpej 971: pr_leave(pp);
1.128.2.2 ad 972: cv_wait(&pp->pr_cv, &pp->pr_lock);
1.25 thorpej 973: pr_enter(pp, file, line);
1.20 thorpej 974: goto startover;
975: }
1.31 thorpej 976:
977: /*
978: * Log a message that the hard limit has been hit.
979: */
980: if (pp->pr_hardlimit_warning != NULL &&
981: ratecheck(&pp->pr_hardlimit_warning_last,
982: &pp->pr_hardlimit_ratecap))
983: log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1.21 thorpej 984:
985: pp->pr_nfail++;
986:
1.25 thorpej 987: pr_leave(pp);
1.128.2.2 ad 988: mutex_exit(&pp->pr_lock);
1.20 thorpej 989: return (NULL);
990: }
991:
1.3 pk 992: /*
993: * The convention we use is that if `curpage' is not NULL, then
994: * it points at a non-empty bucket. In particular, `curpage'
995: * never points at a page header which has PR_PHINPAGE set and
996: * has no items in its bucket.
997: */
1.20 thorpej 998: if ((ph = pp->pr_curpage) == NULL) {
1.113 yamt 999: int error;
1000:
1.20 thorpej 1001: #ifdef DIAGNOSTIC
1002: if (pp->pr_nitems != 0) {
1.128.2.2 ad 1003: mutex_exit(&pp->pr_lock);
1.20 thorpej 1004: printf("pool_get: %s: curpage NULL, nitems %u\n",
1005: pp->pr_wchan, pp->pr_nitems);
1.80 provos 1006: panic("pool_get: nitems inconsistent");
1.20 thorpej 1007: }
1008: #endif
1009:
1.21 thorpej 1010: /*
1011: * Call the back-end page allocator for more memory.
1012: * Release the pool lock, as the back-end page allocator
1013: * may block.
1014: */
1.25 thorpej 1015: pr_leave(pp);
1.113 yamt 1016: error = pool_grow(pp, flags);
1017: pr_enter(pp, file, line);
1018: if (error != 0) {
1.21 thorpej 1019: /*
1.55 thorpej 1020: * We were unable to allocate a page or item
1021: * header, but we released the lock during
1022: * allocation, so perhaps items were freed
1023: * back to the pool. Check for this case.
1.21 thorpej 1024: */
1025: if (pp->pr_curpage != NULL)
1026: goto startover;
1.15 pk 1027:
1.117 yamt 1028: pp->pr_nfail++;
1.25 thorpej 1029: pr_leave(pp);
1.128.2.2 ad 1030: mutex_exit(&pp->pr_lock);
1.117 yamt 1031: return (NULL);
1.1 pk 1032: }
1.3 pk 1033:
1.20 thorpej 1034: /* Start the allocation process over. */
1035: goto startover;
1.3 pk 1036: }
1.97 yamt 1037: if (pp->pr_roflags & PR_NOTOUCH) {
1038: #ifdef DIAGNOSTIC
1039: if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) {
1040: pr_leave(pp);
1.128.2.2 ad 1041: mutex_exit(&pp->pr_lock);
1.97 yamt 1042: panic("pool_get: %s: page empty", pp->pr_wchan);
1043: }
1044: #endif
1045: v = pr_item_notouch_get(pp, ph);
1046: #ifdef POOL_DIAGNOSTIC
1047: pr_log(pp, v, PRLOG_GET, file, line);
1048: #endif
1049: } else {
1.102 chs 1050: v = pi = LIST_FIRST(&ph->ph_itemlist);
1.97 yamt 1051: if (__predict_false(v == NULL)) {
1052: pr_leave(pp);
1.128.2.2 ad 1053: mutex_exit(&pp->pr_lock);
1.97 yamt 1054: panic("pool_get: %s: page empty", pp->pr_wchan);
1055: }
1.20 thorpej 1056: #ifdef DIAGNOSTIC
1.97 yamt 1057: if (__predict_false(pp->pr_nitems == 0)) {
1058: pr_leave(pp);
1.128.2.2 ad 1059: mutex_exit(&pp->pr_lock);
1.97 yamt 1060: printf("pool_get: %s: items on itemlist, nitems %u\n",
1061: pp->pr_wchan, pp->pr_nitems);
1062: panic("pool_get: nitems inconsistent");
1063: }
1.65 enami 1064: #endif
1.56 sommerfe 1065:
1.65 enami 1066: #ifdef POOL_DIAGNOSTIC
1.97 yamt 1067: pr_log(pp, v, PRLOG_GET, file, line);
1.65 enami 1068: #endif
1.3 pk 1069:
1.65 enami 1070: #ifdef DIAGNOSTIC
1.97 yamt 1071: if (__predict_false(pi->pi_magic != PI_MAGIC)) {
1072: pr_printlog(pp, pi, printf);
1073: panic("pool_get(%s): free list modified: "
1074: "magic=%x; page %p; item addr %p\n",
1075: pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
1076: }
1.3 pk 1077: #endif
1078:
1.97 yamt 1079: /*
1080: * Remove from item list.
1081: */
1.102 chs 1082: LIST_REMOVE(pi, pi_list);
1.97 yamt 1083: }
1.20 thorpej 1084: pp->pr_nitems--;
1085: pp->pr_nout++;
1.6 thorpej 1086: if (ph->ph_nmissing == 0) {
1087: #ifdef DIAGNOSTIC
1.34 thorpej 1088: if (__predict_false(pp->pr_nidle == 0))
1.6 thorpej 1089: panic("pool_get: nidle inconsistent");
1090: #endif
1091: pp->pr_nidle--;
1.88 chs 1092:
1093: /*
1094: * This page was previously empty. Move it to the list of
1095: * partially-full pages. This page is already curpage.
1096: */
1097: LIST_REMOVE(ph, ph_pagelist);
1098: LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.6 thorpej 1099: }
1.3 pk 1100: ph->ph_nmissing++;
1.97 yamt 1101: if (ph->ph_nmissing == pp->pr_itemsperpage) {
1.21 thorpej 1102: #ifdef DIAGNOSTIC
1.97 yamt 1103: if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 &&
1.102 chs 1104: !LIST_EMPTY(&ph->ph_itemlist))) {
1.25 thorpej 1105: pr_leave(pp);
1.128.2.2 ad 1106: mutex_exit(&pp->pr_lock);
1.21 thorpej 1107: panic("pool_get: %s: nmissing inconsistent",
1108: pp->pr_wchan);
1109: }
1110: #endif
1.3 pk 1111: /*
1.88 chs 1112: * This page is now full. Move it to the full list
1113: * and select a new current page.
1.3 pk 1114: */
1.88 chs 1115: LIST_REMOVE(ph, ph_pagelist);
1116: LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
1117: pool_update_curpage(pp);
1.1 pk 1118: }
1.3 pk 1119:
1120: pp->pr_nget++;
1.111 christos 1121: pr_leave(pp);
1.20 thorpej 1122:
1123: /*
1124: * If we have a low water mark and we are now below that low
1125: * water mark, add more items to the pool.
1126: */
1.53 thorpej 1127: if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20 thorpej 1128: /*
1129: * XXX: Should we log a warning? Should we set up a timeout
1130: * to try again in a second or so? The latter could break
1131: * a caller's assumptions about interrupt protection, etc.
1132: */
1133: }
1134:
1.128.2.2 ad 1135: mutex_exit(&pp->pr_lock);
1.125 ad 1136: KASSERT((((vaddr_t)v + pp->pr_itemoffset) & (pp->pr_align - 1)) == 0);
1137: FREECHECK_OUT(&pp->pr_freecheck, v);
1.1 pk 1138: return (v);
1139: }
1140:
1141: /*
1.43 thorpej 1142: * Internal version of pool_put(). Pool is already locked/entered.
1.1 pk 1143: */
1.43 thorpej 1144: static void
1.101 thorpej 1145: pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq)
1.1 pk 1146: {
1147: struct pool_item *pi = v;
1.3 pk 1148: struct pool_item_header *ph;
1149:
1.128.2.2 ad 1150: KASSERT(mutex_owned(&pp->pr_lock));
1.125 ad 1151: FREECHECK_IN(&pp->pr_freecheck, v);
1.128.2.5 ad 1152: LOCKDEBUG_MEM_CHECK(v, pp->pr_size);
1.61 chs 1153:
1.30 thorpej 1154: #ifdef DIAGNOSTIC
1.34 thorpej 1155: if (__predict_false(pp->pr_nout == 0)) {
1.30 thorpej 1156: printf("pool %s: putting with none out\n",
1157: pp->pr_wchan);
1158: panic("pool_put");
1159: }
1160: #endif
1.3 pk 1161:
1.121 yamt 1162: if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) {
1.25 thorpej 1163: pr_printlog(pp, NULL, printf);
1.3 pk 1164: panic("pool_put: %s: page header missing", pp->pr_wchan);
1165: }
1.28 thorpej 1166:
1.3 pk 1167: /*
1168: * Return to item list.
1169: */
1.97 yamt 1170: if (pp->pr_roflags & PR_NOTOUCH) {
1171: pr_item_notouch_put(pp, ph, v);
1172: } else {
1.2 pk 1173: #ifdef DIAGNOSTIC
1.97 yamt 1174: pi->pi_magic = PI_MAGIC;
1.3 pk 1175: #endif
1.32 chs 1176: #ifdef DEBUG
1.97 yamt 1177: {
1178: int i, *ip = v;
1.32 chs 1179:
1.97 yamt 1180: for (i = 0; i < pp->pr_size / sizeof(int); i++) {
1181: *ip++ = PI_MAGIC;
1182: }
1.32 chs 1183: }
1184: #endif
1185:
1.102 chs 1186: LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.97 yamt 1187: }
1.79 thorpej 1188: KDASSERT(ph->ph_nmissing != 0);
1.3 pk 1189: ph->ph_nmissing--;
1190: pp->pr_nput++;
1.20 thorpej 1191: pp->pr_nitems++;
1192: pp->pr_nout--;
1.3 pk 1193:
1194: /* Cancel "pool empty" condition if it exists */
1195: if (pp->pr_curpage == NULL)
1196: pp->pr_curpage = ph;
1197:
1198: if (pp->pr_flags & PR_WANTED) {
1199: pp->pr_flags &= ~PR_WANTED;
1.15 pk 1200: if (ph->ph_nmissing == 0)
1201: pp->pr_nidle++;
1.128.2.4 ad 1202: cv_broadcast(&pp->pr_cv);
1.3 pk 1203: return;
1204: }
1205:
1206: /*
1.88 chs 1207: * If this page is now empty, do one of two things:
1.21 thorpej 1208: *
1.88 chs 1209: * (1) If we have more pages than the page high water mark,
1.96 thorpej 1210: * free the page back to the system. ONLY CONSIDER
1.90 thorpej 1211: * FREEING BACK A PAGE IF WE HAVE MORE THAN OUR MINIMUM PAGE
1212: * CLAIM.
1.21 thorpej 1213: *
1.88 chs 1214: * (2) Otherwise, move the page to the empty page list.
1215: *
1216: * Either way, select a new current page (so we use a partially-full
1217: * page if one is available).
1.3 pk 1218: */
1219: if (ph->ph_nmissing == 0) {
1.6 thorpej 1220: pp->pr_nidle++;
1.90 thorpej 1221: if (pp->pr_npages > pp->pr_minpages &&
1222: (pp->pr_npages > pp->pr_maxpages ||
1.117 yamt 1223: pa_starved_p(pp->pr_alloc))) {
1.101 thorpej 1224: pr_rmpage(pp, ph, pq);
1.3 pk 1225: } else {
1.88 chs 1226: LIST_REMOVE(ph, ph_pagelist);
1227: LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.3 pk 1228:
1.21 thorpej 1229: /*
1230: * Update the timestamp on the page. A page must
1231: * be idle for some period of time before it can
1232: * be reclaimed by the pagedaemon. This minimizes
1233: * ping-pong'ing for memory.
1234: */
1.118 kardel 1235: getmicrotime(&ph->ph_time);
1.1 pk 1236: }
1.88 chs 1237: pool_update_curpage(pp);
1.1 pk 1238: }
1.88 chs 1239:
1.21 thorpej 1240: /*
1.88 chs 1241: * If the page was previously completely full, move it to the
1242: * partially-full list and make it the current page. The next
1243: * allocation will get the item from this page, instead of
1244: * further fragmenting the pool.
1.21 thorpej 1245: */
1246: else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
1.88 chs 1247: LIST_REMOVE(ph, ph_pagelist);
1248: LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.21 thorpej 1249: pp->pr_curpage = ph;
1250: }
1.43 thorpej 1251: }
1252:
1253: /*
1254: * Return resource to the pool; must be called at appropriate spl level
1255: */
1.59 thorpej 1256: #ifdef POOL_DIAGNOSTIC
1.43 thorpej 1257: void
1258: _pool_put(struct pool *pp, void *v, const char *file, long line)
1259: {
1.101 thorpej 1260: struct pool_pagelist pq;
1261:
1262: LIST_INIT(&pq);
1.43 thorpej 1263:
1.128.2.2 ad 1264: mutex_enter(&pp->pr_lock);
1.43 thorpej 1265: pr_enter(pp, file, line);
1266:
1.56 sommerfe 1267: pr_log(pp, v, PRLOG_PUT, file, line);
1268:
1.101 thorpej 1269: pool_do_put(pp, v, &pq);
1.21 thorpej 1270:
1.25 thorpej 1271: pr_leave(pp);
1.128.2.2 ad 1272: mutex_exit(&pp->pr_lock);
1.101 thorpej 1273:
1.102 chs 1274: pr_pagelist_free(pp, &pq);
1.1 pk 1275: }
1.57 sommerfe 1276: #undef pool_put
1.59 thorpej 1277: #endif /* POOL_DIAGNOSTIC */
1.1 pk 1278:
1.56 sommerfe 1279: void
1280: pool_put(struct pool *pp, void *v)
1281: {
1.101 thorpej 1282: struct pool_pagelist pq;
1283:
1284: LIST_INIT(&pq);
1.56 sommerfe 1285:
1.128.2.2 ad 1286: mutex_enter(&pp->pr_lock);
1.101 thorpej 1287: pool_do_put(pp, v, &pq);
1.128.2.2 ad 1288: mutex_exit(&pp->pr_lock);
1.56 sommerfe 1289:
1.102 chs 1290: pr_pagelist_free(pp, &pq);
1.56 sommerfe 1291: }
1.57 sommerfe 1292:
1.59 thorpej 1293: #ifdef POOL_DIAGNOSTIC
1.57 sommerfe 1294: #define pool_put(h, v) _pool_put((h), (v), __FILE__, __LINE__)
1.56 sommerfe 1295: #endif
1.74 thorpej 1296:
1297: /*
1.113 yamt 1298: * pool_grow: grow a pool by a page.
1299: *
1300: * => called with pool locked.
1301: * => unlock and relock the pool.
1302: * => return with pool locked.
1303: */
1304:
1305: static int
1306: pool_grow(struct pool *pp, int flags)
1307: {
1308: struct pool_item_header *ph = NULL;
1309: char *cp;
1310:
1.128.2.2 ad 1311: mutex_exit(&pp->pr_lock);
1.113 yamt 1312: cp = pool_allocator_alloc(pp, flags);
1313: if (__predict_true(cp != NULL)) {
1314: ph = pool_alloc_item_header(pp, cp, flags);
1315: }
1316: if (__predict_false(cp == NULL || ph == NULL)) {
1317: if (cp != NULL) {
1318: pool_allocator_free(pp, cp);
1319: }
1.128.2.2 ad 1320: mutex_enter(&pp->pr_lock);
1.113 yamt 1321: return ENOMEM;
1322: }
1323:
1.128.2.2 ad 1324: mutex_enter(&pp->pr_lock);
1.113 yamt 1325: pool_prime_page(pp, cp, ph);
1326: pp->pr_npagealloc++;
1327: return 0;
1328: }
1329:
1330: /*
1.74 thorpej 1331: * Add N items to the pool.
1332: */
1333: int
1334: pool_prime(struct pool *pp, int n)
1335: {
1.75 simonb 1336: int newpages;
1.113 yamt 1337: int error = 0;
1.74 thorpej 1338:
1.128.2.2 ad 1339: mutex_enter(&pp->pr_lock);
1.74 thorpej 1340:
1341: newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1342:
1343: while (newpages-- > 0) {
1.113 yamt 1344: error = pool_grow(pp, PR_NOWAIT);
1345: if (error) {
1.74 thorpej 1346: break;
1347: }
1348: pp->pr_minpages++;
1349: }
1350:
1351: if (pp->pr_minpages >= pp->pr_maxpages)
1352: pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */
1353:
1.128.2.2 ad 1354: mutex_exit(&pp->pr_lock);
1.113 yamt 1355: return error;
1.74 thorpej 1356: }
1.55 thorpej 1357:
1358: /*
1.3 pk 1359: * Add a page worth of items to the pool.
1.21 thorpej 1360: *
1361: * Note, we must be called with the pool descriptor LOCKED.
1.3 pk 1362: */
1.55 thorpej 1363: static void
1.128 christos 1364: pool_prime_page(struct pool *pp, void *storage, struct pool_item_header *ph)
1.3 pk 1365: {
1366: struct pool_item *pi;
1.128 christos 1367: void *cp = storage;
1.125 ad 1368: const unsigned int align = pp->pr_align;
1369: const unsigned int ioff = pp->pr_itemoffset;
1.55 thorpej 1370: int n;
1.36 pk 1371:
1.128.2.2 ad 1372: KASSERT(mutex_owned(&pp->pr_lock));
1.91 yamt 1373:
1.66 thorpej 1374: #ifdef DIAGNOSTIC
1.121 yamt 1375: if ((pp->pr_roflags & PR_NOALIGN) == 0 &&
1376: ((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0)
1.36 pk 1377: panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1.66 thorpej 1378: #endif
1.3 pk 1379:
1380: /*
1381: * Insert page header.
1382: */
1.88 chs 1383: LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.102 chs 1384: LIST_INIT(&ph->ph_itemlist);
1.3 pk 1385: ph->ph_page = storage;
1386: ph->ph_nmissing = 0;
1.118 kardel 1387: getmicrotime(&ph->ph_time);
1.88 chs 1388: if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1389: SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
1.3 pk 1390:
1.6 thorpej 1391: pp->pr_nidle++;
1392:
1.3 pk 1393: /*
1394: * Color this page.
1395: */
1.128 christos 1396: cp = (char *)cp + pp->pr_curcolor;
1.3 pk 1397: if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
1398: pp->pr_curcolor = 0;
1399:
1400: /*
1401: * Adjust storage to apply aligment to `pr_itemoffset' in each item.
1402: */
1403: if (ioff != 0)
1.128 christos 1404: cp = (char *)cp + align - ioff;
1.3 pk 1405:
1.125 ad 1406: KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1407:
1.3 pk 1408: /*
1409: * Insert remaining chunks on the bucket list.
1410: */
1411: n = pp->pr_itemsperpage;
1.20 thorpej 1412: pp->pr_nitems += n;
1.3 pk 1413:
1.97 yamt 1414: if (pp->pr_roflags & PR_NOTOUCH) {
1.99 yamt 1415: pool_item_freelist_t *freelist = PR_FREELIST(ph);
1.97 yamt 1416: int i;
1417:
1.128 christos 1418: ph->ph_off = (char *)cp - (char *)storage;
1.97 yamt 1419: ph->ph_firstfree = 0;
1420: for (i = 0; i < n - 1; i++)
1421: freelist[i] = i + 1;
1422: freelist[n - 1] = PR_INDEX_EOL;
1423: } else {
1424: while (n--) {
1425: pi = (struct pool_item *)cp;
1.78 thorpej 1426:
1.97 yamt 1427: KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
1.3 pk 1428:
1.97 yamt 1429: /* Insert on page list */
1.102 chs 1430: LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.3 pk 1431: #ifdef DIAGNOSTIC
1.97 yamt 1432: pi->pi_magic = PI_MAGIC;
1.3 pk 1433: #endif
1.128 christos 1434: cp = (char *)cp + pp->pr_size;
1.125 ad 1435:
1436: KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1.97 yamt 1437: }
1.3 pk 1438: }
1439:
1440: /*
1441: * If the pool was depleted, point at the new page.
1442: */
1443: if (pp->pr_curpage == NULL)
1444: pp->pr_curpage = ph;
1445:
1446: if (++pp->pr_npages > pp->pr_hiwat)
1447: pp->pr_hiwat = pp->pr_npages;
1448: }
1449:
1.20 thorpej 1450: /*
1.52 thorpej 1451: * Used by pool_get() when nitems drops below the low water mark. This
1.88 chs 1452: * is used to catch up pr_nitems with the low water mark.
1.20 thorpej 1453: *
1.21 thorpej 1454: * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20 thorpej 1455: *
1.73 thorpej 1456: * Note 2, we must be called with the pool already locked, and we return
1.20 thorpej 1457: * with it locked.
1458: */
1459: static int
1.42 thorpej 1460: pool_catchup(struct pool *pp)
1.20 thorpej 1461: {
1462: int error = 0;
1463:
1.54 thorpej 1464: while (POOL_NEEDS_CATCHUP(pp)) {
1.113 yamt 1465: error = pool_grow(pp, PR_NOWAIT);
1466: if (error) {
1.20 thorpej 1467: break;
1468: }
1469: }
1.113 yamt 1470: return error;
1.20 thorpej 1471: }
1472:
1.88 chs 1473: static void
1474: pool_update_curpage(struct pool *pp)
1475: {
1476:
1477: pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
1478: if (pp->pr_curpage == NULL) {
1479: pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
1480: }
1481: }
1482:
1.3 pk 1483: void
1.42 thorpej 1484: pool_setlowat(struct pool *pp, int n)
1.3 pk 1485: {
1.15 pk 1486:
1.128.2.2 ad 1487: mutex_enter(&pp->pr_lock);
1.21 thorpej 1488:
1.3 pk 1489: pp->pr_minitems = n;
1.15 pk 1490: pp->pr_minpages = (n == 0)
1491: ? 0
1.18 thorpej 1492: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20 thorpej 1493:
1494: /* Make sure we're caught up with the newly-set low water mark. */
1.75 simonb 1495: if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20 thorpej 1496: /*
1497: * XXX: Should we log a warning? Should we set up a timeout
1498: * to try again in a second or so? The latter could break
1499: * a caller's assumptions about interrupt protection, etc.
1500: */
1501: }
1.21 thorpej 1502:
1.128.2.2 ad 1503: mutex_exit(&pp->pr_lock);
1.3 pk 1504: }
1505:
1506: void
1.42 thorpej 1507: pool_sethiwat(struct pool *pp, int n)
1.3 pk 1508: {
1.15 pk 1509:
1.128.2.2 ad 1510: mutex_enter(&pp->pr_lock);
1.21 thorpej 1511:
1.15 pk 1512: pp->pr_maxpages = (n == 0)
1513: ? 0
1.18 thorpej 1514: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1515:
1.128.2.2 ad 1516: mutex_exit(&pp->pr_lock);
1.3 pk 1517: }
1518:
1.20 thorpej 1519: void
1.42 thorpej 1520: pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1.20 thorpej 1521: {
1522:
1.128.2.2 ad 1523: mutex_enter(&pp->pr_lock);
1.20 thorpej 1524:
1525: pp->pr_hardlimit = n;
1526: pp->pr_hardlimit_warning = warnmess;
1.31 thorpej 1527: pp->pr_hardlimit_ratecap.tv_sec = ratecap;
1528: pp->pr_hardlimit_warning_last.tv_sec = 0;
1529: pp->pr_hardlimit_warning_last.tv_usec = 0;
1.20 thorpej 1530:
1531: /*
1.21 thorpej 1532: * In-line version of pool_sethiwat(), because we don't want to
1533: * release the lock.
1.20 thorpej 1534: */
1535: pp->pr_maxpages = (n == 0)
1536: ? 0
1537: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1538:
1.128.2.2 ad 1539: mutex_exit(&pp->pr_lock);
1.20 thorpej 1540: }
1.3 pk 1541:
1542: /*
1543: * Release all complete pages that have not been used recently.
1544: */
1.66 thorpej 1545: int
1.59 thorpej 1546: #ifdef POOL_DIAGNOSTIC
1.42 thorpej 1547: _pool_reclaim(struct pool *pp, const char *file, long line)
1.56 sommerfe 1548: #else
1549: pool_reclaim(struct pool *pp)
1550: #endif
1.3 pk 1551: {
1552: struct pool_item_header *ph, *phnext;
1.43 thorpej 1553: struct pool_cache *pc;
1.61 chs 1554: struct pool_pagelist pq;
1.102 chs 1555: struct pool_cache_grouplist pcgl;
1556: struct timeval curtime, diff;
1.3 pk 1557:
1.68 thorpej 1558: if (pp->pr_drain_hook != NULL) {
1559: /*
1560: * The drain hook must be called with the pool unlocked.
1561: */
1562: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT);
1563: }
1564:
1.128.2.2 ad 1565: if (mutex_tryenter(&pp->pr_lock) == 0)
1.66 thorpej 1566: return (0);
1.25 thorpej 1567: pr_enter(pp, file, line);
1.68 thorpej 1568:
1.88 chs 1569: LIST_INIT(&pq);
1.102 chs 1570: LIST_INIT(&pcgl);
1.3 pk 1571:
1.43 thorpej 1572: /*
1573: * Reclaim items from the pool's caches.
1574: */
1.102 chs 1575: LIST_FOREACH(pc, &pp->pr_cachelist, pc_poollist)
1576: pool_cache_reclaim(pc, &pq, &pcgl);
1.43 thorpej 1577:
1.118 kardel 1578: getmicrotime(&curtime);
1.21 thorpej 1579:
1.88 chs 1580: for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
1581: phnext = LIST_NEXT(ph, ph_pagelist);
1.3 pk 1582:
1583: /* Check our minimum page claim */
1584: if (pp->pr_npages <= pp->pr_minpages)
1585: break;
1586:
1.88 chs 1587: KASSERT(ph->ph_nmissing == 0);
1588: timersub(&curtime, &ph->ph_time, &diff);
1.117 yamt 1589: if (diff.tv_sec < pool_inactive_time
1590: && !pa_starved_p(pp->pr_alloc))
1.88 chs 1591: continue;
1.21 thorpej 1592:
1.88 chs 1593: /*
1594: * If freeing this page would put us below
1595: * the low water mark, stop now.
1596: */
1597: if ((pp->pr_nitems - pp->pr_itemsperpage) <
1598: pp->pr_minitems)
1599: break;
1.21 thorpej 1600:
1.88 chs 1601: pr_rmpage(pp, ph, &pq);
1.3 pk 1602: }
1603:
1.25 thorpej 1604: pr_leave(pp);
1.128.2.2 ad 1605: mutex_exit(&pp->pr_lock);
1.102 chs 1606: if (LIST_EMPTY(&pq) && LIST_EMPTY(&pcgl))
1607: return 0;
1.66 thorpej 1608:
1.101 thorpej 1609: pr_pagelist_free(pp, &pq);
1.102 chs 1610: pcg_grouplist_free(&pcgl);
1.66 thorpej 1611: return (1);
1.3 pk 1612: }
1613:
1614: /*
1615: * Drain pools, one at a time.
1.21 thorpej 1616: *
1617: * Note, we must never be called from an interrupt context.
1.128.2.6! ad 1618: *
! 1619: * XXX Pool can disappear while draining.
1.3 pk 1620: */
1621: void
1.124 yamt 1622: pool_drain(void *arg)
1.3 pk 1623: {
1624: struct pool *pp;
1.23 thorpej 1625: int s;
1.3 pk 1626:
1.61 chs 1627: pp = NULL;
1.128.2.2 ad 1628: s = splvm(); /* XXX why? */
1629: mutex_enter(&pool_head_lock);
1.61 chs 1630: if (drainpp == NULL) {
1.102 chs 1631: drainpp = LIST_FIRST(&pool_head);
1.61 chs 1632: }
1633: if (drainpp) {
1634: pp = drainpp;
1.102 chs 1635: drainpp = LIST_NEXT(pp, pr_poollist);
1.61 chs 1636: }
1.128.2.2 ad 1637: mutex_exit(&pool_head_lock);
1.115 christos 1638: if (pp)
1639: pool_reclaim(pp);
1.61 chs 1640: splx(s);
1.3 pk 1641: }
1642:
1643: /*
1644: * Diagnostic helpers.
1645: */
1646: void
1.42 thorpej 1647: pool_print(struct pool *pp, const char *modif)
1.21 thorpej 1648: {
1649:
1.128.2.2 ad 1650: if (mutex_tryenter(&pp->pr_lock) == 0) {
1.25 thorpej 1651: printf("pool %s is locked; try again later\n",
1652: pp->pr_wchan);
1653: return;
1654: }
1655: pool_print1(pp, modif, printf);
1.128.2.2 ad 1656: mutex_exit(&pp->pr_lock);
1.21 thorpej 1657: }
1658:
1.25 thorpej 1659: void
1.108 yamt 1660: pool_printall(const char *modif, void (*pr)(const char *, ...))
1661: {
1662: struct pool *pp;
1663:
1.128.2.2 ad 1664: if (mutex_tryenter(&pool_head_lock) == 0) {
1.108 yamt 1665: (*pr)("WARNING: pool_head_slock is locked\n");
1666: } else {
1.128.2.2 ad 1667: mutex_exit(&pool_head_lock);
1.108 yamt 1668: }
1669:
1670: LIST_FOREACH(pp, &pool_head, pr_poollist) {
1671: pool_printit(pp, modif, pr);
1672: }
1673: }
1674:
1675: void
1.42 thorpej 1676: pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.25 thorpej 1677: {
1678:
1679: if (pp == NULL) {
1680: (*pr)("Must specify a pool to print.\n");
1681: return;
1682: }
1683:
1684: /*
1685: * Called from DDB; interrupts should be blocked, and all
1686: * other processors should be paused. We can skip locking
1687: * the pool in this case.
1688: *
1.128.2.2 ad 1689: * We do a mutex_tryenter() just to print the lock
1.25 thorpej 1690: * status, however.
1691: */
1692:
1.128.2.2 ad 1693: if (mutex_tryenter(&pp->pr_lock) == 0)
1.25 thorpej 1694: (*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
1695: else
1.128.2.2 ad 1696: mutex_exit(&pp->pr_lock);
1.25 thorpej 1697:
1698: pool_print1(pp, modif, pr);
1699: }
1700:
1.21 thorpej 1701: static void
1.124 yamt 1702: pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
1.97 yamt 1703: void (*pr)(const char *, ...))
1.88 chs 1704: {
1705: struct pool_item_header *ph;
1706: #ifdef DIAGNOSTIC
1707: struct pool_item *pi;
1708: #endif
1709:
1710: LIST_FOREACH(ph, pl, ph_pagelist) {
1711: (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
1712: ph->ph_page, ph->ph_nmissing,
1713: (u_long)ph->ph_time.tv_sec,
1714: (u_long)ph->ph_time.tv_usec);
1715: #ifdef DIAGNOSTIC
1.97 yamt 1716: if (!(pp->pr_roflags & PR_NOTOUCH)) {
1.102 chs 1717: LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1.97 yamt 1718: if (pi->pi_magic != PI_MAGIC) {
1719: (*pr)("\t\t\titem %p, magic 0x%x\n",
1720: pi, pi->pi_magic);
1721: }
1.88 chs 1722: }
1723: }
1724: #endif
1725: }
1726: }
1727:
1728: static void
1.42 thorpej 1729: pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.3 pk 1730: {
1.25 thorpej 1731: struct pool_item_header *ph;
1.44 thorpej 1732: struct pool_cache *pc;
1733: struct pool_cache_group *pcg;
1734: int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1.25 thorpej 1735: char c;
1736:
1737: while ((c = *modif++) != '\0') {
1738: if (c == 'l')
1739: print_log = 1;
1740: if (c == 'p')
1741: print_pagelist = 1;
1.44 thorpej 1742: if (c == 'c')
1743: print_cache = 1;
1.25 thorpej 1744: }
1745:
1746: (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1747: pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1748: pp->pr_roflags);
1.66 thorpej 1749: (*pr)("\talloc %p\n", pp->pr_alloc);
1.25 thorpej 1750: (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1751: pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1752: (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1753: pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1754:
1755: (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
1756: pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1757: (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1758: pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1759:
1760: if (print_pagelist == 0)
1761: goto skip_pagelist;
1762:
1.88 chs 1763: if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1764: (*pr)("\n\tempty page list:\n");
1.97 yamt 1765: pool_print_pagelist(pp, &pp->pr_emptypages, pr);
1.88 chs 1766: if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
1767: (*pr)("\n\tfull page list:\n");
1.97 yamt 1768: pool_print_pagelist(pp, &pp->pr_fullpages, pr);
1.88 chs 1769: if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
1770: (*pr)("\n\tpartial-page list:\n");
1.97 yamt 1771: pool_print_pagelist(pp, &pp->pr_partpages, pr);
1.88 chs 1772:
1.25 thorpej 1773: if (pp->pr_curpage == NULL)
1774: (*pr)("\tno current page\n");
1775: else
1776: (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1777:
1778: skip_pagelist:
1779: if (print_log == 0)
1780: goto skip_log;
1781:
1782: (*pr)("\n");
1783: if ((pp->pr_roflags & PR_LOGGING) == 0)
1784: (*pr)("\tno log\n");
1.122 christos 1785: else {
1.25 thorpej 1786: pr_printlog(pp, NULL, pr);
1.122 christos 1787: }
1.3 pk 1788:
1.25 thorpej 1789: skip_log:
1.44 thorpej 1790: if (print_cache == 0)
1791: goto skip_cache;
1792:
1.102 chs 1793: #define PR_GROUPLIST(pcg) \
1794: (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail); \
1795: for (i = 0; i < PCG_NOBJECTS; i++) { \
1796: if (pcg->pcg_objects[i].pcgo_pa != \
1797: POOL_PADDR_INVALID) { \
1798: (*pr)("\t\t\t%p, 0x%llx\n", \
1799: pcg->pcg_objects[i].pcgo_va, \
1800: (unsigned long long) \
1801: pcg->pcg_objects[i].pcgo_pa); \
1802: } else { \
1803: (*pr)("\t\t\t%p\n", \
1804: pcg->pcg_objects[i].pcgo_va); \
1805: } \
1806: }
1807:
1808: LIST_FOREACH(pc, &pp->pr_cachelist, pc_poollist) {
1.103 chs 1809: (*pr)("\tcache %p\n", pc);
1.48 thorpej 1810: (*pr)("\t hits %lu misses %lu ngroups %lu nitems %lu\n",
1811: pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems);
1.102 chs 1812: (*pr)("\t full groups:\n");
1.103 chs 1813: LIST_FOREACH(pcg, &pc->pc_fullgroups, pcg_list) {
1.102 chs 1814: PR_GROUPLIST(pcg);
1.103 chs 1815: }
1.102 chs 1816: (*pr)("\t partial groups:\n");
1.103 chs 1817: LIST_FOREACH(pcg, &pc->pc_partgroups, pcg_list) {
1.102 chs 1818: PR_GROUPLIST(pcg);
1.103 chs 1819: }
1.102 chs 1820: (*pr)("\t empty groups:\n");
1.103 chs 1821: LIST_FOREACH(pcg, &pc->pc_emptygroups, pcg_list) {
1.102 chs 1822: PR_GROUPLIST(pcg);
1.103 chs 1823: }
1.44 thorpej 1824: }
1.102 chs 1825: #undef PR_GROUPLIST
1.44 thorpej 1826:
1827: skip_cache:
1.88 chs 1828: pr_enter_check(pp, pr);
1829: }
1830:
1831: static int
1832: pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
1833: {
1834: struct pool_item *pi;
1.128 christos 1835: void *page;
1.88 chs 1836: int n;
1837:
1.121 yamt 1838: if ((pp->pr_roflags & PR_NOALIGN) == 0) {
1.128 christos 1839: page = (void *)((uintptr_t)ph & pp->pr_alloc->pa_pagemask);
1.121 yamt 1840: if (page != ph->ph_page &&
1841: (pp->pr_roflags & PR_PHINPAGE) != 0) {
1842: if (label != NULL)
1843: printf("%s: ", label);
1844: printf("pool(%p:%s): page inconsistency: page %p;"
1845: " at page head addr %p (p %p)\n", pp,
1846: pp->pr_wchan, ph->ph_page,
1847: ph, page);
1848: return 1;
1849: }
1.88 chs 1850: }
1.3 pk 1851:
1.97 yamt 1852: if ((pp->pr_roflags & PR_NOTOUCH) != 0)
1853: return 0;
1854:
1.102 chs 1855: for (pi = LIST_FIRST(&ph->ph_itemlist), n = 0;
1.88 chs 1856: pi != NULL;
1.102 chs 1857: pi = LIST_NEXT(pi,pi_list), n++) {
1.88 chs 1858:
1859: #ifdef DIAGNOSTIC
1860: if (pi->pi_magic != PI_MAGIC) {
1861: if (label != NULL)
1862: printf("%s: ", label);
1863: printf("pool(%s): free list modified: magic=%x;"
1.121 yamt 1864: " page %p; item ordinal %d; addr %p\n",
1.88 chs 1865: pp->pr_wchan, pi->pi_magic, ph->ph_page,
1.121 yamt 1866: n, pi);
1.88 chs 1867: panic("pool");
1868: }
1869: #endif
1.121 yamt 1870: if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1871: continue;
1872: }
1.128 christos 1873: page = (void *)((uintptr_t)pi & pp->pr_alloc->pa_pagemask);
1.88 chs 1874: if (page == ph->ph_page)
1875: continue;
1876:
1877: if (label != NULL)
1878: printf("%s: ", label);
1879: printf("pool(%p:%s): page inconsistency: page %p;"
1880: " item ordinal %d; addr %p (p %p)\n", pp,
1881: pp->pr_wchan, ph->ph_page,
1882: n, pi, page);
1883: return 1;
1884: }
1885: return 0;
1.3 pk 1886: }
1887:
1.88 chs 1888:
1.3 pk 1889: int
1.42 thorpej 1890: pool_chk(struct pool *pp, const char *label)
1.3 pk 1891: {
1892: struct pool_item_header *ph;
1893: int r = 0;
1894:
1.128.2.2 ad 1895: mutex_enter(&pp->pr_lock);
1.88 chs 1896: LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
1897: r = pool_chk_page(pp, label, ph);
1898: if (r) {
1899: goto out;
1900: }
1901: }
1902: LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
1903: r = pool_chk_page(pp, label, ph);
1904: if (r) {
1.3 pk 1905: goto out;
1906: }
1.88 chs 1907: }
1908: LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
1909: r = pool_chk_page(pp, label, ph);
1910: if (r) {
1.3 pk 1911: goto out;
1912: }
1913: }
1.88 chs 1914:
1.3 pk 1915: out:
1.128.2.2 ad 1916: mutex_exit(&pp->pr_lock);
1.3 pk 1917: return (r);
1.43 thorpej 1918: }
1919:
1920: /*
1921: * pool_cache_init:
1922: *
1923: * Initialize a pool cache.
1924: *
1925: * NOTE: If the pool must be protected from interrupts, we expect
1926: * to be called at the appropriate interrupt priority level.
1927: */
1928: void
1929: pool_cache_init(struct pool_cache *pc, struct pool *pp,
1930: int (*ctor)(void *, void *, int),
1931: void (*dtor)(void *, void *),
1932: void *arg)
1933: {
1934:
1.102 chs 1935: LIST_INIT(&pc->pc_emptygroups);
1936: LIST_INIT(&pc->pc_fullgroups);
1937: LIST_INIT(&pc->pc_partgroups);
1.128.2.2 ad 1938: mutex_init(&pc->pc_lock, MUTEX_DRIVER, pp->pr_ipl);
1.43 thorpej 1939:
1940: pc->pc_pool = pp;
1941:
1942: pc->pc_ctor = ctor;
1943: pc->pc_dtor = dtor;
1944: pc->pc_arg = arg;
1945:
1.48 thorpej 1946: pc->pc_hits = 0;
1947: pc->pc_misses = 0;
1948:
1949: pc->pc_ngroups = 0;
1950:
1951: pc->pc_nitems = 0;
1952:
1.128.2.2 ad 1953: if (__predict_true(!cold)) {
1954: mutex_enter(&pp->pr_lock);
1955: LIST_INSERT_HEAD(&pp->pr_cachelist, pc, pc_poollist);
1956: mutex_exit(&pp->pr_lock);
1957: } else
1958: LIST_INSERT_HEAD(&pp->pr_cachelist, pc, pc_poollist);
1.43 thorpej 1959: }
1960:
1961: /*
1962: * pool_cache_destroy:
1963: *
1964: * Destroy a pool cache.
1965: */
1966: void
1967: pool_cache_destroy(struct pool_cache *pc)
1968: {
1969: struct pool *pp = pc->pc_pool;
1970:
1971: /* First, invalidate the entire cache. */
1972: pool_cache_invalidate(pc);
1973:
1974: /* ...and remove it from the pool's cache list. */
1.128.2.2 ad 1975: mutex_enter(&pp->pr_lock);
1.102 chs 1976: LIST_REMOVE(pc, pc_poollist);
1.128.2.2 ad 1977: mutex_exit(&pp->pr_lock);
1978:
1979: mutex_destroy(&pc->pc_lock);
1.43 thorpej 1980: }
1981:
1.110 perry 1982: static inline void *
1.87 thorpej 1983: pcg_get(struct pool_cache_group *pcg, paddr_t *pap)
1.43 thorpej 1984: {
1985: void *object;
1986: u_int idx;
1987:
1988: KASSERT(pcg->pcg_avail <= PCG_NOBJECTS);
1.45 thorpej 1989: KASSERT(pcg->pcg_avail != 0);
1.43 thorpej 1990: idx = --pcg->pcg_avail;
1991:
1.87 thorpej 1992: KASSERT(pcg->pcg_objects[idx].pcgo_va != NULL);
1993: object = pcg->pcg_objects[idx].pcgo_va;
1994: if (pap != NULL)
1995: *pap = pcg->pcg_objects[idx].pcgo_pa;
1996: pcg->pcg_objects[idx].pcgo_va = NULL;
1.43 thorpej 1997:
1998: return (object);
1999: }
2000:
1.110 perry 2001: static inline void
1.87 thorpej 2002: pcg_put(struct pool_cache_group *pcg, void *object, paddr_t pa)
1.43 thorpej 2003: {
2004: u_int idx;
2005:
2006: KASSERT(pcg->pcg_avail < PCG_NOBJECTS);
2007: idx = pcg->pcg_avail++;
2008:
1.87 thorpej 2009: KASSERT(pcg->pcg_objects[idx].pcgo_va == NULL);
2010: pcg->pcg_objects[idx].pcgo_va = object;
2011: pcg->pcg_objects[idx].pcgo_pa = pa;
1.43 thorpej 2012: }
2013:
1.102 chs 2014: static void
2015: pcg_grouplist_free(struct pool_cache_grouplist *pcgl)
2016: {
2017: struct pool_cache_group *pcg;
2018:
2019: while ((pcg = LIST_FIRST(pcgl)) != NULL) {
2020: LIST_REMOVE(pcg, pcg_list);
2021: pool_put(&pcgpool, pcg);
2022: }
2023: }
2024:
1.43 thorpej 2025: /*
1.87 thorpej 2026: * pool_cache_get{,_paddr}:
1.43 thorpej 2027: *
1.87 thorpej 2028: * Get an object from a pool cache (optionally returning
2029: * the physical address of the object).
1.43 thorpej 2030: */
2031: void *
1.87 thorpej 2032: pool_cache_get_paddr(struct pool_cache *pc, int flags, paddr_t *pap)
1.43 thorpej 2033: {
2034: struct pool_cache_group *pcg;
2035: void *object;
1.58 thorpej 2036:
2037: #ifdef LOCKDEBUG
2038: if (flags & PR_WAITOK)
1.119 yamt 2039: ASSERT_SLEEPABLE(NULL, "pool_cache_get(PR_WAITOK)");
1.58 thorpej 2040: #endif
1.43 thorpej 2041:
1.128.2.2 ad 2042: mutex_enter(&pc->pc_lock);
1.43 thorpej 2043:
1.102 chs 2044: pcg = LIST_FIRST(&pc->pc_partgroups);
2045: if (pcg == NULL) {
2046: pcg = LIST_FIRST(&pc->pc_fullgroups);
2047: if (pcg != NULL) {
2048: LIST_REMOVE(pcg, pcg_list);
2049: LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
1.43 thorpej 2050: }
1.102 chs 2051: }
2052: if (pcg == NULL) {
1.43 thorpej 2053:
2054: /*
2055: * No groups with any available objects. Allocate
2056: * a new object, construct it, and return it to
2057: * the caller. We will allocate a group, if necessary,
2058: * when the object is freed back to the cache.
2059: */
1.48 thorpej 2060: pc->pc_misses++;
1.128.2.2 ad 2061: mutex_exit(&pc->pc_lock);
1.43 thorpej 2062: object = pool_get(pc->pc_pool, flags);
2063: if (object != NULL && pc->pc_ctor != NULL) {
2064: if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) {
2065: pool_put(pc->pc_pool, object);
2066: return (NULL);
2067: }
2068: }
1.125 ad 2069: KASSERT((((vaddr_t)object + pc->pc_pool->pr_itemoffset) &
2070: (pc->pc_pool->pr_align - 1)) == 0);
1.87 thorpej 2071: if (object != NULL && pap != NULL) {
2072: #ifdef POOL_VTOPHYS
2073: *pap = POOL_VTOPHYS(object);
2074: #else
2075: *pap = POOL_PADDR_INVALID;
2076: #endif
2077: }
1.125 ad 2078:
2079: FREECHECK_OUT(&pc->pc_freecheck, object);
1.43 thorpej 2080: return (object);
2081: }
2082:
1.48 thorpej 2083: pc->pc_hits++;
2084: pc->pc_nitems--;
1.87 thorpej 2085: object = pcg_get(pcg, pap);
1.43 thorpej 2086:
1.102 chs 2087: if (pcg->pcg_avail == 0) {
2088: LIST_REMOVE(pcg, pcg_list);
2089: LIST_INSERT_HEAD(&pc->pc_emptygroups, pcg, pcg_list);
2090: }
1.128.2.2 ad 2091: mutex_exit(&pc->pc_lock);
1.43 thorpej 2092:
1.125 ad 2093: KASSERT((((vaddr_t)object + pc->pc_pool->pr_itemoffset) &
2094: (pc->pc_pool->pr_align - 1)) == 0);
2095: FREECHECK_OUT(&pc->pc_freecheck, object);
1.43 thorpej 2096: return (object);
2097: }
2098:
2099: /*
1.87 thorpej 2100: * pool_cache_put{,_paddr}:
1.43 thorpej 2101: *
1.87 thorpej 2102: * Put an object back to the pool cache (optionally caching the
2103: * physical address of the object).
1.43 thorpej 2104: */
2105: void
1.87 thorpej 2106: pool_cache_put_paddr(struct pool_cache *pc, void *object, paddr_t pa)
1.43 thorpej 2107: {
2108: struct pool_cache_group *pcg;
2109:
1.125 ad 2110: FREECHECK_IN(&pc->pc_freecheck, object);
2111:
1.109 christos 2112: if (__predict_false((pc->pc_pool->pr_flags & PR_WANTED) != 0)) {
2113: goto destruct;
2114: }
2115:
1.128.2.2 ad 2116: mutex_enter(&pc->pc_lock);
1.43 thorpej 2117:
1.102 chs 2118: pcg = LIST_FIRST(&pc->pc_partgroups);
2119: if (pcg == NULL) {
2120: pcg = LIST_FIRST(&pc->pc_emptygroups);
2121: if (pcg != NULL) {
2122: LIST_REMOVE(pcg, pcg_list);
2123: LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
1.43 thorpej 2124: }
1.102 chs 2125: }
2126: if (pcg == NULL) {
1.43 thorpej 2127:
2128: /*
2129: * No empty groups to free the object to. Attempt to
1.47 thorpej 2130: * allocate one.
1.43 thorpej 2131: */
1.128.2.2 ad 2132: mutex_exit(&pc->pc_lock);
1.43 thorpej 2133: pcg = pool_get(&pcgpool, PR_NOWAIT);
1.102 chs 2134: if (pcg == NULL) {
1.109 christos 2135: destruct:
1.102 chs 2136:
2137: /*
2138: * Unable to allocate a cache group; destruct the object
2139: * and free it back to the pool.
2140: */
2141: pool_cache_destruct_object(pc, object);
2142: return;
1.43 thorpej 2143: }
1.102 chs 2144: memset(pcg, 0, sizeof(*pcg));
1.128.2.2 ad 2145: mutex_enter(&pc->pc_lock);
1.102 chs 2146: pc->pc_ngroups++;
2147: LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
1.43 thorpej 2148: }
2149:
1.48 thorpej 2150: pc->pc_nitems++;
1.87 thorpej 2151: pcg_put(pcg, object, pa);
1.43 thorpej 2152:
1.102 chs 2153: if (pcg->pcg_avail == PCG_NOBJECTS) {
2154: LIST_REMOVE(pcg, pcg_list);
2155: LIST_INSERT_HEAD(&pc->pc_fullgroups, pcg, pcg_list);
2156: }
1.128.2.2 ad 2157: mutex_exit(&pc->pc_lock);
1.51 thorpej 2158: }
2159:
2160: /*
2161: * pool_cache_destruct_object:
2162: *
2163: * Force destruction of an object and its release back into
2164: * the pool.
2165: */
2166: void
2167: pool_cache_destruct_object(struct pool_cache *pc, void *object)
2168: {
2169:
2170: if (pc->pc_dtor != NULL)
2171: (*pc->pc_dtor)(pc->pc_arg, object);
2172: pool_put(pc->pc_pool, object);
1.43 thorpej 2173: }
2174:
1.128.2.6! ad 2175: /*
! 2176: * pool_do_cache_invalidate_grouplist:
! 2177: *
! 2178: * Invalidate a single grouplist and destruct all objects.
! 2179: * XXX This is too expensive. We should swap the list then
! 2180: * unlock.
! 2181: */
1.102 chs 2182: static void
1.106 christos 2183: pool_do_cache_invalidate_grouplist(struct pool_cache_grouplist *pcgsl,
1.105 christos 2184: struct pool_cache *pc, struct pool_pagelist *pq,
1.106 christos 2185: struct pool_cache_grouplist *pcgdl)
1.102 chs 2186: {
1.128.2.6! ad 2187: struct pool_cache_group *pcg;
1.102 chs 2188: void *object;
2189:
1.128.2.6! ad 2190: KASSERT(mutex_owned(&pc->pc_lock));
! 2191: KASSERT(mutex_owned(&pc->pc_pool->pr_lock));
! 2192:
! 2193: while ((pcg = LIST_FIRST(pcgsl)) != NULL) {
! 2194: pc->pc_ngroups--;
! 2195: LIST_REMOVE(pcg, pcg_list);
! 2196: LIST_INSERT_HEAD(pcgdl, pcg, pcg_list);
! 2197: pc->pc_nitems -= pcg->pcg_avail;
! 2198: mutex_exit(&pc->pc_pool->pr_lock);
! 2199: mutex_exit(&pc->pc_lock);
! 2200:
1.102 chs 2201: while (pcg->pcg_avail != 0) {
2202: object = pcg_get(pcg, NULL);
2203: if (pc->pc_dtor != NULL)
2204: (*pc->pc_dtor)(pc->pc_arg, object);
1.128.2.6! ad 2205: mutex_enter(&pc->pc_pool->pr_lock);
1.102 chs 2206: pool_do_put(pc->pc_pool, object, pq);
1.128.2.6! ad 2207: mutex_exit(&pc->pc_pool->pr_lock);
1.102 chs 2208: }
1.128.2.6! ad 2209:
! 2210: mutex_enter(&pc->pc_lock);
! 2211: mutex_enter(&pc->pc_pool->pr_lock);
1.102 chs 2212: }
1.105 christos 2213: }
2214:
2215: static void
2216: pool_do_cache_invalidate(struct pool_cache *pc, struct pool_pagelist *pq,
2217: struct pool_cache_grouplist *pcgl)
2218: {
2219:
1.128.2.2 ad 2220: KASSERT(mutex_owned(&pc->pc_lock));
2221: KASSERT(mutex_owned(&pc->pc_pool->pr_lock));
1.105 christos 2222:
1.106 christos 2223: pool_do_cache_invalidate_grouplist(&pc->pc_fullgroups, pc, pq, pcgl);
2224: pool_do_cache_invalidate_grouplist(&pc->pc_partgroups, pc, pq, pcgl);
1.103 chs 2225:
2226: KASSERT(LIST_EMPTY(&pc->pc_partgroups));
2227: KASSERT(LIST_EMPTY(&pc->pc_fullgroups));
2228: KASSERT(pc->pc_nitems == 0);
1.102 chs 2229: }
2230:
1.43 thorpej 2231: /*
1.101 thorpej 2232: * pool_cache_invalidate:
1.43 thorpej 2233: *
1.101 thorpej 2234: * Invalidate a pool cache (destruct and release all of the
2235: * cached objects).
1.43 thorpej 2236: */
1.101 thorpej 2237: void
2238: pool_cache_invalidate(struct pool_cache *pc)
1.43 thorpej 2239: {
1.101 thorpej 2240: struct pool_pagelist pq;
1.102 chs 2241: struct pool_cache_grouplist pcgl;
1.101 thorpej 2242:
2243: LIST_INIT(&pq);
1.102 chs 2244: LIST_INIT(&pcgl);
1.101 thorpej 2245:
1.128.2.2 ad 2246: mutex_enter(&pc->pc_lock);
2247: mutex_enter(&pc->pc_pool->pr_lock);
1.43 thorpej 2248:
1.102 chs 2249: pool_do_cache_invalidate(pc, &pq, &pcgl);
1.43 thorpej 2250:
1.128.2.2 ad 2251: mutex_exit(&pc->pc_pool->pr_lock);
2252: mutex_exit(&pc->pc_lock);
1.43 thorpej 2253:
1.102 chs 2254: pr_pagelist_free(pc->pc_pool, &pq);
2255: pcg_grouplist_free(&pcgl);
1.43 thorpej 2256: }
2257:
2258: /*
2259: * pool_cache_reclaim:
2260: *
2261: * Reclaim a pool cache for pool_reclaim().
2262: */
2263: static void
1.102 chs 2264: pool_cache_reclaim(struct pool_cache *pc, struct pool_pagelist *pq,
2265: struct pool_cache_grouplist *pcgl)
1.43 thorpej 2266: {
1.101 thorpej 2267:
2268: /*
2269: * We're locking in the wrong order (normally pool_cache -> pool,
2270: * but the pool is already locked when we get here), so we have
2271: * to use trylock. If we can't lock the pool_cache, it's not really
2272: * a big deal here.
2273: */
1.128.2.2 ad 2274: if (mutex_tryenter(&pc->pc_lock) == 0)
1.101 thorpej 2275: return;
2276:
1.102 chs 2277: pool_do_cache_invalidate(pc, pq, pcgl);
1.43 thorpej 2278:
1.128.2.2 ad 2279: mutex_exit(&pc->pc_lock);
1.3 pk 2280: }
1.66 thorpej 2281:
2282: /*
2283: * Pool backend allocators.
2284: *
2285: * Each pool has a backend allocator that handles allocation, deallocation,
2286: * and any additional draining that might be needed.
2287: *
2288: * We provide two standard allocators:
2289: *
2290: * pool_allocator_kmem - the default when no allocator is specified
2291: *
2292: * pool_allocator_nointr - used for pools that will not be accessed
2293: * in interrupt context.
2294: */
2295: void *pool_page_alloc(struct pool *, int);
2296: void pool_page_free(struct pool *, void *);
2297:
1.112 bjh21 2298: #ifdef POOL_SUBPAGE
2299: struct pool_allocator pool_allocator_kmem_fullpage = {
2300: pool_page_alloc, pool_page_free, 0,
1.117 yamt 2301: .pa_backingmapptr = &kmem_map,
1.112 bjh21 2302: };
2303: #else
1.66 thorpej 2304: struct pool_allocator pool_allocator_kmem = {
2305: pool_page_alloc, pool_page_free, 0,
1.117 yamt 2306: .pa_backingmapptr = &kmem_map,
1.66 thorpej 2307: };
1.112 bjh21 2308: #endif
1.66 thorpej 2309:
2310: void *pool_page_alloc_nointr(struct pool *, int);
2311: void pool_page_free_nointr(struct pool *, void *);
2312:
1.112 bjh21 2313: #ifdef POOL_SUBPAGE
2314: struct pool_allocator pool_allocator_nointr_fullpage = {
2315: pool_page_alloc_nointr, pool_page_free_nointr, 0,
1.117 yamt 2316: .pa_backingmapptr = &kernel_map,
1.112 bjh21 2317: };
2318: #else
1.66 thorpej 2319: struct pool_allocator pool_allocator_nointr = {
2320: pool_page_alloc_nointr, pool_page_free_nointr, 0,
1.117 yamt 2321: .pa_backingmapptr = &kernel_map,
1.66 thorpej 2322: };
1.112 bjh21 2323: #endif
1.66 thorpej 2324:
2325: #ifdef POOL_SUBPAGE
2326: void *pool_subpage_alloc(struct pool *, int);
2327: void pool_subpage_free(struct pool *, void *);
2328:
1.112 bjh21 2329: struct pool_allocator pool_allocator_kmem = {
2330: pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
1.117 yamt 2331: .pa_backingmapptr = &kmem_map,
1.112 bjh21 2332: };
2333:
2334: void *pool_subpage_alloc_nointr(struct pool *, int);
2335: void pool_subpage_free_nointr(struct pool *, void *);
2336:
2337: struct pool_allocator pool_allocator_nointr = {
2338: pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
1.117 yamt 2339: .pa_backingmapptr = &kmem_map,
1.66 thorpej 2340: };
2341: #endif /* POOL_SUBPAGE */
2342:
1.117 yamt 2343: static void *
2344: pool_allocator_alloc(struct pool *pp, int flags)
1.66 thorpej 2345: {
1.117 yamt 2346: struct pool_allocator *pa = pp->pr_alloc;
1.66 thorpej 2347: void *res;
2348:
1.117 yamt 2349: res = (*pa->pa_alloc)(pp, flags);
2350: if (res == NULL && (flags & PR_WAITOK) == 0) {
1.66 thorpej 2351: /*
1.117 yamt 2352: * We only run the drain hook here if PR_NOWAIT.
2353: * In other cases, the hook will be run in
2354: * pool_reclaim().
1.66 thorpej 2355: */
1.117 yamt 2356: if (pp->pr_drain_hook != NULL) {
2357: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
2358: res = (*pa->pa_alloc)(pp, flags);
1.66 thorpej 2359: }
1.117 yamt 2360: }
2361: return res;
1.66 thorpej 2362: }
2363:
1.117 yamt 2364: static void
1.66 thorpej 2365: pool_allocator_free(struct pool *pp, void *v)
2366: {
2367: struct pool_allocator *pa = pp->pr_alloc;
2368:
2369: (*pa->pa_free)(pp, v);
2370: }
2371:
2372: void *
1.124 yamt 2373: pool_page_alloc(struct pool *pp, int flags)
1.66 thorpej 2374: {
1.127 thorpej 2375: bool waitok = (flags & PR_WAITOK) ? true : false;
1.66 thorpej 2376:
1.100 yamt 2377: return ((void *) uvm_km_alloc_poolpage_cache(kmem_map, waitok));
1.66 thorpej 2378: }
2379:
2380: void
1.124 yamt 2381: pool_page_free(struct pool *pp, void *v)
1.66 thorpej 2382: {
2383:
1.98 yamt 2384: uvm_km_free_poolpage_cache(kmem_map, (vaddr_t) v);
2385: }
2386:
2387: static void *
1.124 yamt 2388: pool_page_alloc_meta(struct pool *pp, int flags)
1.98 yamt 2389: {
1.127 thorpej 2390: bool waitok = (flags & PR_WAITOK) ? true : false;
1.98 yamt 2391:
1.100 yamt 2392: return ((void *) uvm_km_alloc_poolpage(kmem_map, waitok));
1.98 yamt 2393: }
2394:
2395: static void
1.124 yamt 2396: pool_page_free_meta(struct pool *pp, void *v)
1.98 yamt 2397: {
2398:
1.100 yamt 2399: uvm_km_free_poolpage(kmem_map, (vaddr_t) v);
1.66 thorpej 2400: }
2401:
2402: #ifdef POOL_SUBPAGE
2403: /* Sub-page allocator, for machines with large hardware pages. */
2404: void *
2405: pool_subpage_alloc(struct pool *pp, int flags)
2406: {
1.128.2.2 ad 2407: return pool_get(&psppool, flags);
1.66 thorpej 2408: }
2409:
2410: void
2411: pool_subpage_free(struct pool *pp, void *v)
2412: {
2413: pool_put(&psppool, v);
2414: }
2415:
2416: /* We don't provide a real nointr allocator. Maybe later. */
2417: void *
1.112 bjh21 2418: pool_subpage_alloc_nointr(struct pool *pp, int flags)
1.66 thorpej 2419: {
2420:
2421: return (pool_subpage_alloc(pp, flags));
2422: }
2423:
2424: void
1.112 bjh21 2425: pool_subpage_free_nointr(struct pool *pp, void *v)
1.66 thorpej 2426: {
2427:
2428: pool_subpage_free(pp, v);
2429: }
1.112 bjh21 2430: #endif /* POOL_SUBPAGE */
1.66 thorpej 2431: void *
1.124 yamt 2432: pool_page_alloc_nointr(struct pool *pp, int flags)
1.66 thorpej 2433: {
1.127 thorpej 2434: bool waitok = (flags & PR_WAITOK) ? true : false;
1.66 thorpej 2435:
1.100 yamt 2436: return ((void *) uvm_km_alloc_poolpage_cache(kernel_map, waitok));
1.66 thorpej 2437: }
2438:
2439: void
1.124 yamt 2440: pool_page_free_nointr(struct pool *pp, void *v)
1.66 thorpej 2441: {
2442:
1.98 yamt 2443: uvm_km_free_poolpage_cache(kernel_map, (vaddr_t) v);
1.66 thorpej 2444: }
CVSweb <webmaster@jp.NetBSD.org>