Annotation of src/sys/kern/subr_pool.c, Revision 1.183
1.183 ! ad 1: /* $NetBSD: subr_pool.c,v 1.182 2010/01/20 23:40:42 rmind Exp $ */
1.1 pk 2:
3: /*-
1.183 ! ad 4: * Copyright (c) 1997, 1999, 2000, 2002, 2007, 2008, 2010
! 5: * The NetBSD Foundation, Inc.
1.1 pk 6: * All rights reserved.
7: *
8: * This code is derived from software contributed to The NetBSD Foundation
1.20 thorpej 9: * by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace
1.134 ad 10: * Simulation Facility, NASA Ames Research Center, and by Andrew Doran.
1.1 pk 11: *
12: * Redistribution and use in source and binary forms, with or without
13: * modification, are permitted provided that the following conditions
14: * are met:
15: * 1. Redistributions of source code must retain the above copyright
16: * notice, this list of conditions and the following disclaimer.
17: * 2. Redistributions in binary form must reproduce the above copyright
18: * notice, this list of conditions and the following disclaimer in the
19: * documentation and/or other materials provided with the distribution.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31: * POSSIBILITY OF SUCH DAMAGE.
32: */
1.64 lukem 33:
34: #include <sys/cdefs.h>
1.183 ! ad 35: __KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.182 2010/01/20 23:40:42 rmind Exp $");
1.24 scottr 36:
1.141 yamt 37: #include "opt_ddb.h"
1.25 thorpej 38: #include "opt_pool.h"
1.24 scottr 39: #include "opt_poollog.h"
1.28 thorpej 40: #include "opt_lockdebug.h"
1.1 pk 41:
42: #include <sys/param.h>
43: #include <sys/systm.h>
1.135 yamt 44: #include <sys/bitops.h>
1.1 pk 45: #include <sys/proc.h>
46: #include <sys/errno.h>
47: #include <sys/kernel.h>
48: #include <sys/malloc.h>
49: #include <sys/pool.h>
1.20 thorpej 50: #include <sys/syslog.h>
1.125 ad 51: #include <sys/debug.h>
1.134 ad 52: #include <sys/lockdebug.h>
53: #include <sys/xcall.h>
54: #include <sys/cpu.h>
1.145 ad 55: #include <sys/atomic.h>
1.3 pk 56:
57: #include <uvm/uvm.h>
58:
1.1 pk 59: /*
60: * Pool resource management utility.
1.3 pk 61: *
1.88 chs 62: * Memory is allocated in pages which are split into pieces according to
63: * the pool item size. Each page is kept on one of three lists in the
64: * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages',
65: * for empty, full and partially-full pages respectively. The individual
66: * pool items are on a linked list headed by `ph_itemlist' in each page
67: * header. The memory for building the page list is either taken from
68: * the allocated pages themselves (for small pool items) or taken from
69: * an internal pool of page headers (`phpool').
1.1 pk 70: */
71:
1.3 pk 72: /* List of all pools */
1.173 rmind 73: static TAILQ_HEAD(, pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
1.134 ad 74:
1.3 pk 75: /* Private pool for page header structures */
1.97 yamt 76: #define PHPOOL_MAX 8
77: static struct pool phpool[PHPOOL_MAX];
1.135 yamt 78: #define PHPOOL_FREELIST_NELEM(idx) \
79: (((idx) == 0) ? 0 : BITMAP_SIZE * (1 << (idx)))
1.3 pk 80:
1.62 bjh21 81: #ifdef POOL_SUBPAGE
82: /* Pool of subpages for use by normal pools. */
83: static struct pool psppool;
84: #endif
85:
1.117 yamt 86: static SLIST_HEAD(, pool_allocator) pa_deferinitq =
87: SLIST_HEAD_INITIALIZER(pa_deferinitq);
88:
1.98 yamt 89: static void *pool_page_alloc_meta(struct pool *, int);
90: static void pool_page_free_meta(struct pool *, void *);
91:
92: /* allocator for pool metadata */
1.134 ad 93: struct pool_allocator pool_allocator_meta = {
1.117 yamt 94: pool_page_alloc_meta, pool_page_free_meta,
95: .pa_backingmapptr = &kmem_map,
1.98 yamt 96: };
97:
1.3 pk 98: /* # of seconds to retain page after last use */
99: int pool_inactive_time = 10;
100:
101: /* Next candidate for drainage (see pool_drain()) */
1.23 thorpej 102: static struct pool *drainpp;
103:
1.134 ad 104: /* This lock protects both pool_head and drainpp. */
105: static kmutex_t pool_head_lock;
106: static kcondvar_t pool_busy;
1.3 pk 107:
1.178 elad 108: /* This lock protects initialization of a potentially shared pool allocator */
109: static kmutex_t pool_allocator_lock;
110:
1.135 yamt 111: typedef uint32_t pool_item_bitmap_t;
112: #define BITMAP_SIZE (CHAR_BIT * sizeof(pool_item_bitmap_t))
113: #define BITMAP_MASK (BITMAP_SIZE - 1)
1.99 yamt 114:
1.3 pk 115: struct pool_item_header {
116: /* Page headers */
1.88 chs 117: LIST_ENTRY(pool_item_header)
1.3 pk 118: ph_pagelist; /* pool page list */
1.88 chs 119: SPLAY_ENTRY(pool_item_header)
120: ph_node; /* Off-page page headers */
1.128 christos 121: void * ph_page; /* this page's address */
1.151 yamt 122: uint32_t ph_time; /* last referenced */
1.135 yamt 123: uint16_t ph_nmissing; /* # of chunks in use */
1.141 yamt 124: uint16_t ph_off; /* start offset in page */
1.97 yamt 125: union {
126: /* !PR_NOTOUCH */
127: struct {
1.102 chs 128: LIST_HEAD(, pool_item)
1.97 yamt 129: phu_itemlist; /* chunk list for this page */
130: } phu_normal;
131: /* PR_NOTOUCH */
132: struct {
1.141 yamt 133: pool_item_bitmap_t phu_bitmap[1];
1.97 yamt 134: } phu_notouch;
135: } ph_u;
1.3 pk 136: };
1.97 yamt 137: #define ph_itemlist ph_u.phu_normal.phu_itemlist
1.135 yamt 138: #define ph_bitmap ph_u.phu_notouch.phu_bitmap
1.3 pk 139:
1.1 pk 140: struct pool_item {
1.3 pk 141: #ifdef DIAGNOSTIC
1.82 thorpej 142: u_int pi_magic;
1.33 chs 143: #endif
1.134 ad 144: #define PI_MAGIC 0xdeaddeadU
1.3 pk 145: /* Other entries use only this list entry */
1.102 chs 146: LIST_ENTRY(pool_item) pi_list;
1.3 pk 147: };
148:
1.53 thorpej 149: #define POOL_NEEDS_CATCHUP(pp) \
150: ((pp)->pr_nitems < (pp)->pr_minitems)
151:
1.43 thorpej 152: /*
153: * Pool cache management.
154: *
155: * Pool caches provide a way for constructed objects to be cached by the
156: * pool subsystem. This can lead to performance improvements by avoiding
157: * needless object construction/destruction; it is deferred until absolutely
158: * necessary.
159: *
1.134 ad 160: * Caches are grouped into cache groups. Each cache group references up
161: * to PCG_NUMOBJECTS constructed objects. When a cache allocates an
162: * object from the pool, it calls the object's constructor and places it
163: * into a cache group. When a cache group frees an object back to the
164: * pool, it first calls the object's destructor. This allows the object
165: * to persist in constructed form while freed to the cache.
166: *
167: * The pool references each cache, so that when a pool is drained by the
168: * pagedaemon, it can drain each individual cache as well. Each time a
169: * cache is drained, the most idle cache group is freed to the pool in
170: * its entirety.
1.43 thorpej 171: *
172: * Pool caches are layed on top of pools. By layering them, we can avoid
173: * the complexity of cache management for pools which would not benefit
174: * from it.
175: */
176:
1.142 ad 177: static struct pool pcg_normal_pool;
178: static struct pool pcg_large_pool;
1.134 ad 179: static struct pool cache_pool;
180: static struct pool cache_cpu_pool;
1.3 pk 181:
1.145 ad 182: /* List of all caches. */
183: TAILQ_HEAD(,pool_cache) pool_cache_head =
184: TAILQ_HEAD_INITIALIZER(pool_cache_head);
185:
1.162 ad 186: int pool_cache_disable; /* global disable for caching */
1.169 yamt 187: static const pcg_t pcg_dummy; /* zero sized: always empty, yet always full */
1.145 ad 188:
1.162 ad 189: static bool pool_cache_put_slow(pool_cache_cpu_t *, int,
190: void *);
191: static bool pool_cache_get_slow(pool_cache_cpu_t *, int,
192: void **, paddr_t *, int);
1.134 ad 193: static void pool_cache_cpu_init1(struct cpu_info *, pool_cache_t);
194: static void pool_cache_invalidate_groups(pool_cache_t, pcg_t *);
1.175 jym 195: static void pool_cache_invalidate_cpu(pool_cache_t, u_int);
1.134 ad 196: static void pool_cache_xcall(pool_cache_t);
1.3 pk 197:
1.42 thorpej 198: static int pool_catchup(struct pool *);
1.128 christos 199: static void pool_prime_page(struct pool *, void *,
1.55 thorpej 200: struct pool_item_header *);
1.88 chs 201: static void pool_update_curpage(struct pool *);
1.66 thorpej 202:
1.113 yamt 203: static int pool_grow(struct pool *, int);
1.117 yamt 204: static void *pool_allocator_alloc(struct pool *, int);
205: static void pool_allocator_free(struct pool *, void *);
1.3 pk 206:
1.97 yamt 207: static void pool_print_pagelist(struct pool *, struct pool_pagelist *,
1.88 chs 208: void (*)(const char *, ...));
1.42 thorpej 209: static void pool_print1(struct pool *, const char *,
210: void (*)(const char *, ...));
1.3 pk 211:
1.88 chs 212: static int pool_chk_page(struct pool *, const char *,
213: struct pool_item_header *);
214:
1.3 pk 215: /*
1.52 thorpej 216: * Pool log entry. An array of these is allocated in pool_init().
1.3 pk 217: */
218: struct pool_log {
219: const char *pl_file;
220: long pl_line;
221: int pl_action;
1.25 thorpej 222: #define PRLOG_GET 1
223: #define PRLOG_PUT 2
1.3 pk 224: void *pl_addr;
1.1 pk 225: };
226:
1.86 matt 227: #ifdef POOL_DIAGNOSTIC
1.3 pk 228: /* Number of entries in pool log buffers */
1.17 thorpej 229: #ifndef POOL_LOGSIZE
230: #define POOL_LOGSIZE 10
231: #endif
232:
233: int pool_logsize = POOL_LOGSIZE;
1.1 pk 234:
1.110 perry 235: static inline void
1.42 thorpej 236: pr_log(struct pool *pp, void *v, int action, const char *file, long line)
1.3 pk 237: {
1.179 mlelstv 238: int n;
1.3 pk 239: struct pool_log *pl;
240:
1.20 thorpej 241: if ((pp->pr_roflags & PR_LOGGING) == 0)
1.3 pk 242: return;
243:
1.179 mlelstv 244: if (pp->pr_log == NULL) {
245: if (kmem_map != NULL)
246: pp->pr_log = malloc(
247: pool_logsize * sizeof(struct pool_log),
248: M_TEMP, M_NOWAIT | M_ZERO);
249: if (pp->pr_log == NULL)
250: return;
251: pp->pr_curlogentry = 0;
252: pp->pr_logsize = pool_logsize;
253: }
254:
1.3 pk 255: /*
256: * Fill in the current entry. Wrap around and overwrite
257: * the oldest entry if necessary.
258: */
1.179 mlelstv 259: n = pp->pr_curlogentry;
1.3 pk 260: pl = &pp->pr_log[n];
261: pl->pl_file = file;
262: pl->pl_line = line;
263: pl->pl_action = action;
264: pl->pl_addr = v;
265: if (++n >= pp->pr_logsize)
266: n = 0;
267: pp->pr_curlogentry = n;
268: }
269:
270: static void
1.42 thorpej 271: pr_printlog(struct pool *pp, struct pool_item *pi,
272: void (*pr)(const char *, ...))
1.3 pk 273: {
274: int i = pp->pr_logsize;
275: int n = pp->pr_curlogentry;
276:
1.179 mlelstv 277: if (pp->pr_log == NULL)
1.3 pk 278: return;
279:
280: /*
281: * Print all entries in this pool's log.
282: */
283: while (i-- > 0) {
284: struct pool_log *pl = &pp->pr_log[n];
285: if (pl->pl_action != 0) {
1.25 thorpej 286: if (pi == NULL || pi == pl->pl_addr) {
287: (*pr)("\tlog entry %d:\n", i);
288: (*pr)("\t\taction = %s, addr = %p\n",
289: pl->pl_action == PRLOG_GET ? "get" : "put",
290: pl->pl_addr);
291: (*pr)("\t\tfile: %s at line %lu\n",
292: pl->pl_file, pl->pl_line);
293: }
1.3 pk 294: }
295: if (++n >= pp->pr_logsize)
296: n = 0;
297: }
298: }
1.25 thorpej 299:
1.110 perry 300: static inline void
1.42 thorpej 301: pr_enter(struct pool *pp, const char *file, long line)
1.25 thorpej 302: {
303:
1.34 thorpej 304: if (__predict_false(pp->pr_entered_file != NULL)) {
1.25 thorpej 305: printf("pool %s: reentrancy at file %s line %ld\n",
306: pp->pr_wchan, file, line);
307: printf(" previous entry at file %s line %ld\n",
308: pp->pr_entered_file, pp->pr_entered_line);
309: panic("pr_enter");
310: }
311:
312: pp->pr_entered_file = file;
313: pp->pr_entered_line = line;
314: }
315:
1.110 perry 316: static inline void
1.42 thorpej 317: pr_leave(struct pool *pp)
1.25 thorpej 318: {
319:
1.34 thorpej 320: if (__predict_false(pp->pr_entered_file == NULL)) {
1.25 thorpej 321: printf("pool %s not entered?\n", pp->pr_wchan);
322: panic("pr_leave");
323: }
324:
325: pp->pr_entered_file = NULL;
326: pp->pr_entered_line = 0;
327: }
328:
1.110 perry 329: static inline void
1.42 thorpej 330: pr_enter_check(struct pool *pp, void (*pr)(const char *, ...))
1.25 thorpej 331: {
332:
333: if (pp->pr_entered_file != NULL)
334: (*pr)("\n\tcurrently entered from file %s line %ld\n",
335: pp->pr_entered_file, pp->pr_entered_line);
336: }
1.3 pk 337: #else
1.25 thorpej 338: #define pr_log(pp, v, action, file, line)
339: #define pr_printlog(pp, pi, pr)
340: #define pr_enter(pp, file, line)
341: #define pr_leave(pp)
342: #define pr_enter_check(pp, pr)
1.59 thorpej 343: #endif /* POOL_DIAGNOSTIC */
1.3 pk 344:
1.135 yamt 345: static inline unsigned int
1.97 yamt 346: pr_item_notouch_index(const struct pool *pp, const struct pool_item_header *ph,
347: const void *v)
348: {
349: const char *cp = v;
1.135 yamt 350: unsigned int idx;
1.97 yamt 351:
352: KASSERT(pp->pr_roflags & PR_NOTOUCH);
1.128 christos 353: idx = (cp - (char *)ph->ph_page - ph->ph_off) / pp->pr_size;
1.97 yamt 354: KASSERT(idx < pp->pr_itemsperpage);
355: return idx;
356: }
357:
1.110 perry 358: static inline void
1.97 yamt 359: pr_item_notouch_put(const struct pool *pp, struct pool_item_header *ph,
360: void *obj)
361: {
1.135 yamt 362: unsigned int idx = pr_item_notouch_index(pp, ph, obj);
363: pool_item_bitmap_t *bitmap = ph->ph_bitmap + (idx / BITMAP_SIZE);
364: pool_item_bitmap_t mask = 1 << (idx & BITMAP_MASK);
1.97 yamt 365:
1.135 yamt 366: KASSERT((*bitmap & mask) == 0);
367: *bitmap |= mask;
1.97 yamt 368: }
369:
1.110 perry 370: static inline void *
1.97 yamt 371: pr_item_notouch_get(const struct pool *pp, struct pool_item_header *ph)
372: {
1.135 yamt 373: pool_item_bitmap_t *bitmap = ph->ph_bitmap;
374: unsigned int idx;
375: int i;
1.97 yamt 376:
1.135 yamt 377: for (i = 0; ; i++) {
378: int bit;
1.97 yamt 379:
1.135 yamt 380: KASSERT((i * BITMAP_SIZE) < pp->pr_itemsperpage);
381: bit = ffs32(bitmap[i]);
382: if (bit) {
383: pool_item_bitmap_t mask;
384:
385: bit--;
386: idx = (i * BITMAP_SIZE) + bit;
387: mask = 1 << bit;
388: KASSERT((bitmap[i] & mask) != 0);
389: bitmap[i] &= ~mask;
390: break;
391: }
392: }
393: KASSERT(idx < pp->pr_itemsperpage);
1.128 christos 394: return (char *)ph->ph_page + ph->ph_off + idx * pp->pr_size;
1.97 yamt 395: }
396:
1.135 yamt 397: static inline void
1.141 yamt 398: pr_item_notouch_init(const struct pool *pp, struct pool_item_header *ph)
1.135 yamt 399: {
400: pool_item_bitmap_t *bitmap = ph->ph_bitmap;
401: const int n = howmany(pp->pr_itemsperpage, BITMAP_SIZE);
402: int i;
403:
404: for (i = 0; i < n; i++) {
405: bitmap[i] = (pool_item_bitmap_t)-1;
406: }
407: }
408:
1.110 perry 409: static inline int
1.88 chs 410: phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
411: {
1.121 yamt 412:
413: /*
414: * we consider pool_item_header with smaller ph_page bigger.
415: * (this unnatural ordering is for the benefit of pr_find_pagehead.)
416: */
417:
1.88 chs 418: if (a->ph_page < b->ph_page)
1.121 yamt 419: return (1);
420: else if (a->ph_page > b->ph_page)
1.88 chs 421: return (-1);
422: else
423: return (0);
424: }
425:
426: SPLAY_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare);
427: SPLAY_GENERATE(phtree, pool_item_header, ph_node, phtree_compare);
428:
1.141 yamt 429: static inline struct pool_item_header *
430: pr_find_pagehead_noalign(struct pool *pp, void *v)
431: {
432: struct pool_item_header *ph, tmp;
433:
434: tmp.ph_page = (void *)(uintptr_t)v;
435: ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
436: if (ph == NULL) {
437: ph = SPLAY_ROOT(&pp->pr_phtree);
438: if (ph != NULL && phtree_compare(&tmp, ph) >= 0) {
439: ph = SPLAY_NEXT(phtree, &pp->pr_phtree, ph);
440: }
441: KASSERT(ph == NULL || phtree_compare(&tmp, ph) < 0);
442: }
443:
444: return ph;
445: }
446:
1.3 pk 447: /*
1.121 yamt 448: * Return the pool page header based on item address.
1.3 pk 449: */
1.110 perry 450: static inline struct pool_item_header *
1.121 yamt 451: pr_find_pagehead(struct pool *pp, void *v)
1.3 pk 452: {
1.88 chs 453: struct pool_item_header *ph, tmp;
1.3 pk 454:
1.121 yamt 455: if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1.141 yamt 456: ph = pr_find_pagehead_noalign(pp, v);
1.121 yamt 457: } else {
1.128 christos 458: void *page =
459: (void *)((uintptr_t)v & pp->pr_alloc->pa_pagemask);
1.121 yamt 460:
461: if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
1.128 christos 462: ph = (struct pool_item_header *)((char *)page + pp->pr_phoffset);
1.121 yamt 463: } else {
464: tmp.ph_page = page;
465: ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
466: }
467: }
1.3 pk 468:
1.121 yamt 469: KASSERT(ph == NULL || ((pp->pr_roflags & PR_PHINPAGE) != 0) ||
1.128 christos 470: ((char *)ph->ph_page <= (char *)v &&
471: (char *)v < (char *)ph->ph_page + pp->pr_alloc->pa_pagesz));
1.88 chs 472: return ph;
1.3 pk 473: }
474:
1.101 thorpej 475: static void
476: pr_pagelist_free(struct pool *pp, struct pool_pagelist *pq)
477: {
478: struct pool_item_header *ph;
479:
480: while ((ph = LIST_FIRST(pq)) != NULL) {
481: LIST_REMOVE(ph, ph_pagelist);
482: pool_allocator_free(pp, ph->ph_page);
1.134 ad 483: if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1.101 thorpej 484: pool_put(pp->pr_phpool, ph);
485: }
486: }
487:
1.3 pk 488: /*
489: * Remove a page from the pool.
490: */
1.110 perry 491: static inline void
1.61 chs 492: pr_rmpage(struct pool *pp, struct pool_item_header *ph,
493: struct pool_pagelist *pq)
1.3 pk 494: {
495:
1.134 ad 496: KASSERT(mutex_owned(&pp->pr_lock));
1.91 yamt 497:
1.3 pk 498: /*
1.7 thorpej 499: * If the page was idle, decrement the idle page count.
1.3 pk 500: */
1.6 thorpej 501: if (ph->ph_nmissing == 0) {
502: #ifdef DIAGNOSTIC
503: if (pp->pr_nidle == 0)
504: panic("pr_rmpage: nidle inconsistent");
1.20 thorpej 505: if (pp->pr_nitems < pp->pr_itemsperpage)
506: panic("pr_rmpage: nitems inconsistent");
1.6 thorpej 507: #endif
508: pp->pr_nidle--;
509: }
1.7 thorpej 510:
1.20 thorpej 511: pp->pr_nitems -= pp->pr_itemsperpage;
512:
1.7 thorpej 513: /*
1.101 thorpej 514: * Unlink the page from the pool and queue it for release.
1.7 thorpej 515: */
1.88 chs 516: LIST_REMOVE(ph, ph_pagelist);
1.91 yamt 517: if ((pp->pr_roflags & PR_PHINPAGE) == 0)
518: SPLAY_REMOVE(phtree, &pp->pr_phtree, ph);
1.101 thorpej 519: LIST_INSERT_HEAD(pq, ph, ph_pagelist);
520:
1.7 thorpej 521: pp->pr_npages--;
522: pp->pr_npagefree++;
1.6 thorpej 523:
1.88 chs 524: pool_update_curpage(pp);
1.3 pk 525: }
526:
1.126 thorpej 527: static bool
1.117 yamt 528: pa_starved_p(struct pool_allocator *pa)
529: {
530:
531: if (pa->pa_backingmap != NULL) {
532: return vm_map_starved_p(pa->pa_backingmap);
533: }
1.127 thorpej 534: return false;
1.117 yamt 535: }
536:
537: static int
1.124 yamt 538: pool_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
1.117 yamt 539: {
540: struct pool *pp = obj;
541: struct pool_allocator *pa = pp->pr_alloc;
542:
543: KASSERT(&pp->pr_reclaimerentry == ce);
544: pool_reclaim(pp);
545: if (!pa_starved_p(pa)) {
546: return CALLBACK_CHAIN_ABORT;
547: }
548: return CALLBACK_CHAIN_CONTINUE;
549: }
550:
551: static void
552: pool_reclaim_register(struct pool *pp)
553: {
554: struct vm_map *map = pp->pr_alloc->pa_backingmap;
555: int s;
556:
557: if (map == NULL) {
558: return;
559: }
560:
561: s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
562: callback_register(&vm_map_to_kernel(map)->vmk_reclaim_callback,
563: &pp->pr_reclaimerentry, pp, pool_reclaim_callback);
564: splx(s);
565: }
566:
567: static void
568: pool_reclaim_unregister(struct pool *pp)
569: {
570: struct vm_map *map = pp->pr_alloc->pa_backingmap;
571: int s;
572:
573: if (map == NULL) {
574: return;
575: }
576:
577: s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
578: callback_unregister(&vm_map_to_kernel(map)->vmk_reclaim_callback,
579: &pp->pr_reclaimerentry);
580: splx(s);
581: }
582:
583: static void
584: pa_reclaim_register(struct pool_allocator *pa)
585: {
586: struct vm_map *map = *pa->pa_backingmapptr;
587: struct pool *pp;
588:
589: KASSERT(pa->pa_backingmap == NULL);
590: if (map == NULL) {
591: SLIST_INSERT_HEAD(&pa_deferinitq, pa, pa_q);
592: return;
593: }
594: pa->pa_backingmap = map;
595: TAILQ_FOREACH(pp, &pa->pa_list, pr_alloc_list) {
596: pool_reclaim_register(pp);
597: }
598: }
599:
1.3 pk 600: /*
1.94 simonb 601: * Initialize all the pools listed in the "pools" link set.
602: */
603: void
1.117 yamt 604: pool_subsystem_init(void)
1.94 simonb 605: {
1.117 yamt 606: struct pool_allocator *pa;
1.94 simonb 607:
1.134 ad 608: mutex_init(&pool_head_lock, MUTEX_DEFAULT, IPL_NONE);
1.179 mlelstv 609: mutex_init(&pool_allocator_lock, MUTEX_DEFAULT, IPL_NONE);
1.134 ad 610: cv_init(&pool_busy, "poolbusy");
611:
1.117 yamt 612: while ((pa = SLIST_FIRST(&pa_deferinitq)) != NULL) {
613: KASSERT(pa->pa_backingmapptr != NULL);
614: KASSERT(*pa->pa_backingmapptr != NULL);
615: SLIST_REMOVE_HEAD(&pa_deferinitq, pa_q);
616: pa_reclaim_register(pa);
617: }
1.134 ad 618:
1.156 ad 619: pool_init(&cache_pool, sizeof(struct pool_cache), coherency_unit,
1.134 ad 620: 0, 0, "pcache", &pool_allocator_nointr, IPL_NONE);
621:
1.156 ad 622: pool_init(&cache_cpu_pool, sizeof(pool_cache_cpu_t), coherency_unit,
1.134 ad 623: 0, 0, "pcachecpu", &pool_allocator_nointr, IPL_NONE);
1.94 simonb 624: }
625:
626: /*
1.3 pk 627: * Initialize the given pool resource structure.
628: *
629: * We export this routine to allow other kernel parts to declare
630: * static pools that must be initialized before malloc() is available.
631: */
632: void
1.42 thorpej 633: pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
1.129 ad 634: const char *wchan, struct pool_allocator *palloc, int ipl)
1.3 pk 635: {
1.116 simonb 636: struct pool *pp1;
1.92 enami 637: size_t trysize, phsize;
1.134 ad 638: int off, slack;
1.3 pk 639:
1.116 simonb 640: #ifdef DEBUG
641: /*
642: * Check that the pool hasn't already been initialised and
643: * added to the list of all pools.
644: */
1.145 ad 645: TAILQ_FOREACH(pp1, &pool_head, pr_poollist) {
1.116 simonb 646: if (pp == pp1)
647: panic("pool_init: pool %s already initialised",
648: wchan);
649: }
650: #endif
651:
1.25 thorpej 652: #ifdef POOL_DIAGNOSTIC
653: /*
654: * Always log if POOL_DIAGNOSTIC is defined.
655: */
656: if (pool_logsize != 0)
657: flags |= PR_LOGGING;
658: #endif
659:
1.66 thorpej 660: if (palloc == NULL)
661: palloc = &pool_allocator_kmem;
1.112 bjh21 662: #ifdef POOL_SUBPAGE
663: if (size > palloc->pa_pagesz) {
664: if (palloc == &pool_allocator_kmem)
665: palloc = &pool_allocator_kmem_fullpage;
666: else if (palloc == &pool_allocator_nointr)
667: palloc = &pool_allocator_nointr_fullpage;
668: }
1.66 thorpej 669: #endif /* POOL_SUBPAGE */
1.180 mlelstv 670: if (!cold)
671: mutex_enter(&pool_allocator_lock);
1.178 elad 672: if (palloc->pa_refcnt++ == 0) {
1.112 bjh21 673: if (palloc->pa_pagesz == 0)
1.66 thorpej 674: palloc->pa_pagesz = PAGE_SIZE;
675:
676: TAILQ_INIT(&palloc->pa_list);
677:
1.134 ad 678: mutex_init(&palloc->pa_lock, MUTEX_DEFAULT, IPL_VM);
1.66 thorpej 679: palloc->pa_pagemask = ~(palloc->pa_pagesz - 1);
680: palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1;
1.117 yamt 681:
682: if (palloc->pa_backingmapptr != NULL) {
683: pa_reclaim_register(palloc);
684: }
1.4 thorpej 685: }
1.180 mlelstv 686: if (!cold)
687: mutex_exit(&pool_allocator_lock);
1.3 pk 688:
689: if (align == 0)
690: align = ALIGN(1);
1.14 thorpej 691:
1.120 yamt 692: if ((flags & PR_NOTOUCH) == 0 && size < sizeof(struct pool_item))
1.14 thorpej 693: size = sizeof(struct pool_item);
1.3 pk 694:
1.78 thorpej 695: size = roundup(size, align);
1.66 thorpej 696: #ifdef DIAGNOSTIC
697: if (size > palloc->pa_pagesz)
1.121 yamt 698: panic("pool_init: pool item size (%zu) too large", size);
1.66 thorpej 699: #endif
1.35 pk 700:
1.3 pk 701: /*
702: * Initialize the pool structure.
703: */
1.88 chs 704: LIST_INIT(&pp->pr_emptypages);
705: LIST_INIT(&pp->pr_fullpages);
706: LIST_INIT(&pp->pr_partpages);
1.134 ad 707: pp->pr_cache = NULL;
1.3 pk 708: pp->pr_curpage = NULL;
709: pp->pr_npages = 0;
710: pp->pr_minitems = 0;
711: pp->pr_minpages = 0;
712: pp->pr_maxpages = UINT_MAX;
1.20 thorpej 713: pp->pr_roflags = flags;
714: pp->pr_flags = 0;
1.35 pk 715: pp->pr_size = size;
1.3 pk 716: pp->pr_align = align;
717: pp->pr_wchan = wchan;
1.66 thorpej 718: pp->pr_alloc = palloc;
1.20 thorpej 719: pp->pr_nitems = 0;
720: pp->pr_nout = 0;
721: pp->pr_hardlimit = UINT_MAX;
722: pp->pr_hardlimit_warning = NULL;
1.31 thorpej 723: pp->pr_hardlimit_ratecap.tv_sec = 0;
724: pp->pr_hardlimit_ratecap.tv_usec = 0;
725: pp->pr_hardlimit_warning_last.tv_sec = 0;
726: pp->pr_hardlimit_warning_last.tv_usec = 0;
1.68 thorpej 727: pp->pr_drain_hook = NULL;
728: pp->pr_drain_hook_arg = NULL;
1.125 ad 729: pp->pr_freecheck = NULL;
1.3 pk 730:
731: /*
732: * Decide whether to put the page header off page to avoid
1.92 enami 733: * wasting too large a part of the page or too big item.
734: * Off-page page headers go on a hash table, so we can match
735: * a returned item with its header based on the page address.
736: * We use 1/16 of the page size and about 8 times of the item
737: * size as the threshold (XXX: tune)
738: *
739: * However, we'll put the header into the page if we can put
740: * it without wasting any items.
741: *
742: * Silently enforce `0 <= ioff < align'.
1.3 pk 743: */
1.92 enami 744: pp->pr_itemoffset = ioff %= align;
745: /* See the comment below about reserved bytes. */
746: trysize = palloc->pa_pagesz - ((align - ioff) % align);
747: phsize = ALIGN(sizeof(struct pool_item_header));
1.121 yamt 748: if ((pp->pr_roflags & (PR_NOTOUCH | PR_NOALIGN)) == 0 &&
1.97 yamt 749: (pp->pr_size < MIN(palloc->pa_pagesz / 16, phsize << 3) ||
750: trysize / pp->pr_size == (trysize - phsize) / pp->pr_size)) {
1.3 pk 751: /* Use the end of the page for the page header */
1.20 thorpej 752: pp->pr_roflags |= PR_PHINPAGE;
1.92 enami 753: pp->pr_phoffset = off = palloc->pa_pagesz - phsize;
1.2 pk 754: } else {
1.3 pk 755: /* The page header will be taken from our page header pool */
756: pp->pr_phoffset = 0;
1.66 thorpej 757: off = palloc->pa_pagesz;
1.88 chs 758: SPLAY_INIT(&pp->pr_phtree);
1.2 pk 759: }
1.1 pk 760:
1.3 pk 761: /*
762: * Alignment is to take place at `ioff' within the item. This means
763: * we must reserve up to `align - 1' bytes on the page to allow
764: * appropriate positioning of each item.
765: */
766: pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
1.43 thorpej 767: KASSERT(pp->pr_itemsperpage != 0);
1.97 yamt 768: if ((pp->pr_roflags & PR_NOTOUCH)) {
769: int idx;
770:
771: for (idx = 0; pp->pr_itemsperpage > PHPOOL_FREELIST_NELEM(idx);
772: idx++) {
773: /* nothing */
774: }
775: if (idx >= PHPOOL_MAX) {
776: /*
777: * if you see this panic, consider to tweak
778: * PHPOOL_MAX and PHPOOL_FREELIST_NELEM.
779: */
780: panic("%s: too large itemsperpage(%d) for PR_NOTOUCH",
781: pp->pr_wchan, pp->pr_itemsperpage);
782: }
783: pp->pr_phpool = &phpool[idx];
784: } else if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
785: pp->pr_phpool = &phpool[0];
786: }
787: #if defined(DIAGNOSTIC)
788: else {
789: pp->pr_phpool = NULL;
790: }
791: #endif
1.3 pk 792:
793: /*
794: * Use the slack between the chunks and the page header
795: * for "cache coloring".
796: */
797: slack = off - pp->pr_itemsperpage * pp->pr_size;
798: pp->pr_maxcolor = (slack / align) * align;
799: pp->pr_curcolor = 0;
800:
801: pp->pr_nget = 0;
802: pp->pr_nfail = 0;
803: pp->pr_nput = 0;
804: pp->pr_npagealloc = 0;
805: pp->pr_npagefree = 0;
1.1 pk 806: pp->pr_hiwat = 0;
1.8 thorpej 807: pp->pr_nidle = 0;
1.134 ad 808: pp->pr_refcnt = 0;
1.3 pk 809:
1.179 mlelstv 810: pp->pr_log = NULL;
1.25 thorpej 811:
812: pp->pr_entered_file = NULL;
813: pp->pr_entered_line = 0;
1.3 pk 814:
1.157 ad 815: mutex_init(&pp->pr_lock, MUTEX_DEFAULT, ipl);
1.134 ad 816: cv_init(&pp->pr_cv, wchan);
817: pp->pr_ipl = ipl;
1.1 pk 818:
1.3 pk 819: /*
1.43 thorpej 820: * Initialize private page header pool and cache magazine pool if we
821: * haven't done so yet.
1.23 thorpej 822: * XXX LOCKING.
1.3 pk 823: */
1.97 yamt 824: if (phpool[0].pr_size == 0) {
825: int idx;
826: for (idx = 0; idx < PHPOOL_MAX; idx++) {
827: static char phpool_names[PHPOOL_MAX][6+1+6+1];
828: int nelem;
829: size_t sz;
830:
831: nelem = PHPOOL_FREELIST_NELEM(idx);
832: snprintf(phpool_names[idx], sizeof(phpool_names[idx]),
833: "phpool-%d", nelem);
834: sz = sizeof(struct pool_item_header);
835: if (nelem) {
1.135 yamt 836: sz = offsetof(struct pool_item_header,
837: ph_bitmap[howmany(nelem, BITMAP_SIZE)]);
1.97 yamt 838: }
839: pool_init(&phpool[idx], sz, 0, 0, 0,
1.129 ad 840: phpool_names[idx], &pool_allocator_meta, IPL_VM);
1.97 yamt 841: }
1.62 bjh21 842: #ifdef POOL_SUBPAGE
843: pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0,
1.129 ad 844: PR_RECURSIVE, "psppool", &pool_allocator_meta, IPL_VM);
1.62 bjh21 845: #endif
1.142 ad 846:
847: size = sizeof(pcg_t) +
848: (PCG_NOBJECTS_NORMAL - 1) * sizeof(pcgpair_t);
1.156 ad 849: pool_init(&pcg_normal_pool, size, coherency_unit, 0, 0,
1.142 ad 850: "pcgnormal", &pool_allocator_meta, IPL_VM);
851:
852: size = sizeof(pcg_t) +
853: (PCG_NOBJECTS_LARGE - 1) * sizeof(pcgpair_t);
1.156 ad 854: pool_init(&pcg_large_pool, size, coherency_unit, 0, 0,
1.142 ad 855: "pcglarge", &pool_allocator_meta, IPL_VM);
1.1 pk 856: }
857:
1.145 ad 858: /* Insert into the list of all pools. */
1.181 mlelstv 859: if (!cold)
1.134 ad 860: mutex_enter(&pool_head_lock);
1.145 ad 861: TAILQ_FOREACH(pp1, &pool_head, pr_poollist) {
862: if (strcmp(pp1->pr_wchan, pp->pr_wchan) > 0)
863: break;
864: }
865: if (pp1 == NULL)
866: TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
867: else
868: TAILQ_INSERT_BEFORE(pp1, pp, pr_poollist);
1.181 mlelstv 869: if (!cold)
1.134 ad 870: mutex_exit(&pool_head_lock);
871:
1.167 skrll 872: /* Insert this into the list of pools using this allocator. */
1.181 mlelstv 873: if (!cold)
1.134 ad 874: mutex_enter(&palloc->pa_lock);
1.145 ad 875: TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
1.181 mlelstv 876: if (!cold)
1.134 ad 877: mutex_exit(&palloc->pa_lock);
1.66 thorpej 878:
1.117 yamt 879: pool_reclaim_register(pp);
1.1 pk 880: }
881:
882: /*
883: * De-commision a pool resource.
884: */
885: void
1.42 thorpej 886: pool_destroy(struct pool *pp)
1.1 pk 887: {
1.101 thorpej 888: struct pool_pagelist pq;
1.3 pk 889: struct pool_item_header *ph;
1.43 thorpej 890:
1.101 thorpej 891: /* Remove from global pool list */
1.134 ad 892: mutex_enter(&pool_head_lock);
893: while (pp->pr_refcnt != 0)
894: cv_wait(&pool_busy, &pool_head_lock);
1.145 ad 895: TAILQ_REMOVE(&pool_head, pp, pr_poollist);
1.101 thorpej 896: if (drainpp == pp)
897: drainpp = NULL;
1.134 ad 898: mutex_exit(&pool_head_lock);
1.101 thorpej 899:
900: /* Remove this pool from its allocator's list of pools. */
1.117 yamt 901: pool_reclaim_unregister(pp);
1.134 ad 902: mutex_enter(&pp->pr_alloc->pa_lock);
1.66 thorpej 903: TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list);
1.134 ad 904: mutex_exit(&pp->pr_alloc->pa_lock);
1.66 thorpej 905:
1.178 elad 906: mutex_enter(&pool_allocator_lock);
907: if (--pp->pr_alloc->pa_refcnt == 0)
908: mutex_destroy(&pp->pr_alloc->pa_lock);
909: mutex_exit(&pool_allocator_lock);
910:
1.134 ad 911: mutex_enter(&pp->pr_lock);
1.101 thorpej 912:
1.134 ad 913: KASSERT(pp->pr_cache == NULL);
1.3 pk 914:
915: #ifdef DIAGNOSTIC
1.20 thorpej 916: if (pp->pr_nout != 0) {
1.25 thorpej 917: pr_printlog(pp, NULL, printf);
1.80 provos 918: panic("pool_destroy: pool busy: still out: %u",
1.20 thorpej 919: pp->pr_nout);
1.3 pk 920: }
921: #endif
1.1 pk 922:
1.101 thorpej 923: KASSERT(LIST_EMPTY(&pp->pr_fullpages));
924: KASSERT(LIST_EMPTY(&pp->pr_partpages));
925:
1.3 pk 926: /* Remove all pages */
1.101 thorpej 927: LIST_INIT(&pq);
1.88 chs 928: while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1.101 thorpej 929: pr_rmpage(pp, ph, &pq);
930:
1.134 ad 931: mutex_exit(&pp->pr_lock);
1.3 pk 932:
1.101 thorpej 933: pr_pagelist_free(pp, &pq);
1.3 pk 934:
1.59 thorpej 935: #ifdef POOL_DIAGNOSTIC
1.179 mlelstv 936: if (pp->pr_log != NULL) {
1.3 pk 937: free(pp->pr_log, M_TEMP);
1.179 mlelstv 938: pp->pr_log = NULL;
939: }
1.59 thorpej 940: #endif
1.134 ad 941:
942: cv_destroy(&pp->pr_cv);
943: mutex_destroy(&pp->pr_lock);
1.1 pk 944: }
945:
1.68 thorpej 946: void
947: pool_set_drain_hook(struct pool *pp, void (*fn)(void *, int), void *arg)
948: {
949:
950: /* XXX no locking -- must be used just after pool_init() */
951: #ifdef DIAGNOSTIC
952: if (pp->pr_drain_hook != NULL)
953: panic("pool_set_drain_hook(%s): already set", pp->pr_wchan);
954: #endif
955: pp->pr_drain_hook = fn;
956: pp->pr_drain_hook_arg = arg;
957: }
958:
1.88 chs 959: static struct pool_item_header *
1.128 christos 960: pool_alloc_item_header(struct pool *pp, void *storage, int flags)
1.55 thorpej 961: {
962: struct pool_item_header *ph;
963:
964: if ((pp->pr_roflags & PR_PHINPAGE) != 0)
1.128 christos 965: ph = (struct pool_item_header *) ((char *)storage + pp->pr_phoffset);
1.134 ad 966: else
1.97 yamt 967: ph = pool_get(pp->pr_phpool, flags);
1.55 thorpej 968:
969: return (ph);
970: }
1.1 pk 971:
972: /*
1.134 ad 973: * Grab an item from the pool.
1.1 pk 974: */
1.3 pk 975: void *
1.59 thorpej 976: #ifdef POOL_DIAGNOSTIC
1.42 thorpej 977: _pool_get(struct pool *pp, int flags, const char *file, long line)
1.56 sommerfe 978: #else
979: pool_get(struct pool *pp, int flags)
980: #endif
1.1 pk 981: {
982: struct pool_item *pi;
1.3 pk 983: struct pool_item_header *ph;
1.55 thorpej 984: void *v;
1.1 pk 985:
1.2 pk 986: #ifdef DIAGNOSTIC
1.95 atatat 987: if (__predict_false(pp->pr_itemsperpage == 0))
988: panic("pool_get: pool %p: pr_itemsperpage is zero, "
989: "pool not initialized?", pp);
1.84 thorpej 990: if (__predict_false(curlwp == NULL && doing_shutdown == 0 &&
1.37 sommerfe 991: (flags & PR_WAITOK) != 0))
1.77 matt 992: panic("pool_get: %s: must have NOWAIT", pp->pr_wchan);
1.58 thorpej 993:
1.102 chs 994: #endif /* DIAGNOSTIC */
1.58 thorpej 995: #ifdef LOCKDEBUG
1.155 ad 996: if (flags & PR_WAITOK) {
1.154 yamt 997: ASSERT_SLEEPABLE();
1.155 ad 998: }
1.56 sommerfe 999: #endif
1.1 pk 1000:
1.134 ad 1001: mutex_enter(&pp->pr_lock);
1.25 thorpej 1002: pr_enter(pp, file, line);
1.20 thorpej 1003:
1004: startover:
1005: /*
1006: * Check to see if we've reached the hard limit. If we have,
1007: * and we can wait, then wait until an item has been returned to
1008: * the pool.
1009: */
1010: #ifdef DIAGNOSTIC
1.34 thorpej 1011: if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
1.25 thorpej 1012: pr_leave(pp);
1.134 ad 1013: mutex_exit(&pp->pr_lock);
1.20 thorpej 1014: panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
1015: }
1016: #endif
1.34 thorpej 1017: if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
1.68 thorpej 1018: if (pp->pr_drain_hook != NULL) {
1019: /*
1020: * Since the drain hook is going to free things
1021: * back to the pool, unlock, call the hook, re-lock,
1022: * and check the hardlimit condition again.
1023: */
1024: pr_leave(pp);
1.134 ad 1025: mutex_exit(&pp->pr_lock);
1.68 thorpej 1026: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
1.134 ad 1027: mutex_enter(&pp->pr_lock);
1.68 thorpej 1028: pr_enter(pp, file, line);
1029: if (pp->pr_nout < pp->pr_hardlimit)
1030: goto startover;
1031: }
1032:
1.29 sommerfe 1033: if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
1.20 thorpej 1034: /*
1035: * XXX: A warning isn't logged in this case. Should
1036: * it be?
1037: */
1038: pp->pr_flags |= PR_WANTED;
1.25 thorpej 1039: pr_leave(pp);
1.134 ad 1040: cv_wait(&pp->pr_cv, &pp->pr_lock);
1.25 thorpej 1041: pr_enter(pp, file, line);
1.20 thorpej 1042: goto startover;
1043: }
1.31 thorpej 1044:
1045: /*
1046: * Log a message that the hard limit has been hit.
1047: */
1048: if (pp->pr_hardlimit_warning != NULL &&
1049: ratecheck(&pp->pr_hardlimit_warning_last,
1050: &pp->pr_hardlimit_ratecap))
1051: log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1.21 thorpej 1052:
1053: pp->pr_nfail++;
1054:
1.25 thorpej 1055: pr_leave(pp);
1.134 ad 1056: mutex_exit(&pp->pr_lock);
1.20 thorpej 1057: return (NULL);
1058: }
1059:
1.3 pk 1060: /*
1061: * The convention we use is that if `curpage' is not NULL, then
1062: * it points at a non-empty bucket. In particular, `curpage'
1063: * never points at a page header which has PR_PHINPAGE set and
1064: * has no items in its bucket.
1065: */
1.20 thorpej 1066: if ((ph = pp->pr_curpage) == NULL) {
1.113 yamt 1067: int error;
1068:
1.20 thorpej 1069: #ifdef DIAGNOSTIC
1070: if (pp->pr_nitems != 0) {
1.134 ad 1071: mutex_exit(&pp->pr_lock);
1.20 thorpej 1072: printf("pool_get: %s: curpage NULL, nitems %u\n",
1073: pp->pr_wchan, pp->pr_nitems);
1.80 provos 1074: panic("pool_get: nitems inconsistent");
1.20 thorpej 1075: }
1076: #endif
1077:
1.21 thorpej 1078: /*
1079: * Call the back-end page allocator for more memory.
1080: * Release the pool lock, as the back-end page allocator
1081: * may block.
1082: */
1.25 thorpej 1083: pr_leave(pp);
1.113 yamt 1084: error = pool_grow(pp, flags);
1085: pr_enter(pp, file, line);
1086: if (error != 0) {
1.21 thorpej 1087: /*
1.55 thorpej 1088: * We were unable to allocate a page or item
1089: * header, but we released the lock during
1090: * allocation, so perhaps items were freed
1091: * back to the pool. Check for this case.
1.21 thorpej 1092: */
1093: if (pp->pr_curpage != NULL)
1094: goto startover;
1.15 pk 1095:
1.117 yamt 1096: pp->pr_nfail++;
1.25 thorpej 1097: pr_leave(pp);
1.134 ad 1098: mutex_exit(&pp->pr_lock);
1.117 yamt 1099: return (NULL);
1.1 pk 1100: }
1.3 pk 1101:
1.20 thorpej 1102: /* Start the allocation process over. */
1103: goto startover;
1.3 pk 1104: }
1.97 yamt 1105: if (pp->pr_roflags & PR_NOTOUCH) {
1106: #ifdef DIAGNOSTIC
1107: if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) {
1108: pr_leave(pp);
1.134 ad 1109: mutex_exit(&pp->pr_lock);
1.97 yamt 1110: panic("pool_get: %s: page empty", pp->pr_wchan);
1111: }
1112: #endif
1113: v = pr_item_notouch_get(pp, ph);
1114: #ifdef POOL_DIAGNOSTIC
1115: pr_log(pp, v, PRLOG_GET, file, line);
1116: #endif
1117: } else {
1.102 chs 1118: v = pi = LIST_FIRST(&ph->ph_itemlist);
1.97 yamt 1119: if (__predict_false(v == NULL)) {
1120: pr_leave(pp);
1.134 ad 1121: mutex_exit(&pp->pr_lock);
1.97 yamt 1122: panic("pool_get: %s: page empty", pp->pr_wchan);
1123: }
1.20 thorpej 1124: #ifdef DIAGNOSTIC
1.97 yamt 1125: if (__predict_false(pp->pr_nitems == 0)) {
1126: pr_leave(pp);
1.134 ad 1127: mutex_exit(&pp->pr_lock);
1.97 yamt 1128: printf("pool_get: %s: items on itemlist, nitems %u\n",
1129: pp->pr_wchan, pp->pr_nitems);
1130: panic("pool_get: nitems inconsistent");
1131: }
1.65 enami 1132: #endif
1.56 sommerfe 1133:
1.65 enami 1134: #ifdef POOL_DIAGNOSTIC
1.97 yamt 1135: pr_log(pp, v, PRLOG_GET, file, line);
1.65 enami 1136: #endif
1.3 pk 1137:
1.65 enami 1138: #ifdef DIAGNOSTIC
1.97 yamt 1139: if (__predict_false(pi->pi_magic != PI_MAGIC)) {
1140: pr_printlog(pp, pi, printf);
1141: panic("pool_get(%s): free list modified: "
1142: "magic=%x; page %p; item addr %p\n",
1143: pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
1144: }
1.3 pk 1145: #endif
1146:
1.97 yamt 1147: /*
1148: * Remove from item list.
1149: */
1.102 chs 1150: LIST_REMOVE(pi, pi_list);
1.97 yamt 1151: }
1.20 thorpej 1152: pp->pr_nitems--;
1153: pp->pr_nout++;
1.6 thorpej 1154: if (ph->ph_nmissing == 0) {
1155: #ifdef DIAGNOSTIC
1.34 thorpej 1156: if (__predict_false(pp->pr_nidle == 0))
1.6 thorpej 1157: panic("pool_get: nidle inconsistent");
1158: #endif
1159: pp->pr_nidle--;
1.88 chs 1160:
1161: /*
1162: * This page was previously empty. Move it to the list of
1163: * partially-full pages. This page is already curpage.
1164: */
1165: LIST_REMOVE(ph, ph_pagelist);
1166: LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.6 thorpej 1167: }
1.3 pk 1168: ph->ph_nmissing++;
1.97 yamt 1169: if (ph->ph_nmissing == pp->pr_itemsperpage) {
1.21 thorpej 1170: #ifdef DIAGNOSTIC
1.97 yamt 1171: if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 &&
1.102 chs 1172: !LIST_EMPTY(&ph->ph_itemlist))) {
1.25 thorpej 1173: pr_leave(pp);
1.134 ad 1174: mutex_exit(&pp->pr_lock);
1.21 thorpej 1175: panic("pool_get: %s: nmissing inconsistent",
1176: pp->pr_wchan);
1177: }
1178: #endif
1.3 pk 1179: /*
1.88 chs 1180: * This page is now full. Move it to the full list
1181: * and select a new current page.
1.3 pk 1182: */
1.88 chs 1183: LIST_REMOVE(ph, ph_pagelist);
1184: LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
1185: pool_update_curpage(pp);
1.1 pk 1186: }
1.3 pk 1187:
1188: pp->pr_nget++;
1.111 christos 1189: pr_leave(pp);
1.20 thorpej 1190:
1191: /*
1192: * If we have a low water mark and we are now below that low
1193: * water mark, add more items to the pool.
1194: */
1.53 thorpej 1195: if (POOL_NEEDS_CATCHUP(pp) && 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: }
1202:
1.134 ad 1203: mutex_exit(&pp->pr_lock);
1.125 ad 1204: KASSERT((((vaddr_t)v + pp->pr_itemoffset) & (pp->pr_align - 1)) == 0);
1205: FREECHECK_OUT(&pp->pr_freecheck, v);
1.1 pk 1206: return (v);
1207: }
1208:
1209: /*
1.43 thorpej 1210: * Internal version of pool_put(). Pool is already locked/entered.
1.1 pk 1211: */
1.43 thorpej 1212: static void
1.101 thorpej 1213: pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq)
1.1 pk 1214: {
1215: struct pool_item *pi = v;
1.3 pk 1216: struct pool_item_header *ph;
1217:
1.134 ad 1218: KASSERT(mutex_owned(&pp->pr_lock));
1.125 ad 1219: FREECHECK_IN(&pp->pr_freecheck, v);
1.134 ad 1220: LOCKDEBUG_MEM_CHECK(v, pp->pr_size);
1.61 chs 1221:
1.30 thorpej 1222: #ifdef DIAGNOSTIC
1.34 thorpej 1223: if (__predict_false(pp->pr_nout == 0)) {
1.30 thorpej 1224: printf("pool %s: putting with none out\n",
1225: pp->pr_wchan);
1226: panic("pool_put");
1227: }
1228: #endif
1.3 pk 1229:
1.121 yamt 1230: if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) {
1.25 thorpej 1231: pr_printlog(pp, NULL, printf);
1.3 pk 1232: panic("pool_put: %s: page header missing", pp->pr_wchan);
1233: }
1.28 thorpej 1234:
1.3 pk 1235: /*
1236: * Return to item list.
1237: */
1.97 yamt 1238: if (pp->pr_roflags & PR_NOTOUCH) {
1239: pr_item_notouch_put(pp, ph, v);
1240: } else {
1.2 pk 1241: #ifdef DIAGNOSTIC
1.97 yamt 1242: pi->pi_magic = PI_MAGIC;
1.3 pk 1243: #endif
1.32 chs 1244: #ifdef DEBUG
1.97 yamt 1245: {
1246: int i, *ip = v;
1.32 chs 1247:
1.97 yamt 1248: for (i = 0; i < pp->pr_size / sizeof(int); i++) {
1249: *ip++ = PI_MAGIC;
1250: }
1.32 chs 1251: }
1252: #endif
1253:
1.102 chs 1254: LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.97 yamt 1255: }
1.79 thorpej 1256: KDASSERT(ph->ph_nmissing != 0);
1.3 pk 1257: ph->ph_nmissing--;
1258: pp->pr_nput++;
1.20 thorpej 1259: pp->pr_nitems++;
1260: pp->pr_nout--;
1.3 pk 1261:
1262: /* Cancel "pool empty" condition if it exists */
1263: if (pp->pr_curpage == NULL)
1264: pp->pr_curpage = ph;
1265:
1266: if (pp->pr_flags & PR_WANTED) {
1267: pp->pr_flags &= ~PR_WANTED;
1.134 ad 1268: cv_broadcast(&pp->pr_cv);
1.3 pk 1269: }
1270:
1271: /*
1.88 chs 1272: * If this page is now empty, do one of two things:
1.21 thorpej 1273: *
1.88 chs 1274: * (1) If we have more pages than the page high water mark,
1.96 thorpej 1275: * free the page back to the system. ONLY CONSIDER
1.90 thorpej 1276: * FREEING BACK A PAGE IF WE HAVE MORE THAN OUR MINIMUM PAGE
1277: * CLAIM.
1.21 thorpej 1278: *
1.88 chs 1279: * (2) Otherwise, move the page to the empty page list.
1280: *
1281: * Either way, select a new current page (so we use a partially-full
1282: * page if one is available).
1.3 pk 1283: */
1284: if (ph->ph_nmissing == 0) {
1.6 thorpej 1285: pp->pr_nidle++;
1.90 thorpej 1286: if (pp->pr_npages > pp->pr_minpages &&
1.152 yamt 1287: pp->pr_npages > pp->pr_maxpages) {
1.101 thorpej 1288: pr_rmpage(pp, ph, pq);
1.3 pk 1289: } else {
1.88 chs 1290: LIST_REMOVE(ph, ph_pagelist);
1291: LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.3 pk 1292:
1.21 thorpej 1293: /*
1294: * Update the timestamp on the page. A page must
1295: * be idle for some period of time before it can
1296: * be reclaimed by the pagedaemon. This minimizes
1297: * ping-pong'ing for memory.
1.151 yamt 1298: *
1299: * note for 64-bit time_t: truncating to 32-bit is not
1300: * a problem for our usage.
1.21 thorpej 1301: */
1.151 yamt 1302: ph->ph_time = time_uptime;
1.1 pk 1303: }
1.88 chs 1304: pool_update_curpage(pp);
1.1 pk 1305: }
1.88 chs 1306:
1.21 thorpej 1307: /*
1.88 chs 1308: * If the page was previously completely full, move it to the
1309: * partially-full list and make it the current page. The next
1310: * allocation will get the item from this page, instead of
1311: * further fragmenting the pool.
1.21 thorpej 1312: */
1313: else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
1.88 chs 1314: LIST_REMOVE(ph, ph_pagelist);
1315: LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.21 thorpej 1316: pp->pr_curpage = ph;
1317: }
1.43 thorpej 1318: }
1319:
1320: /*
1.134 ad 1321: * Return resource to the pool.
1.43 thorpej 1322: */
1.59 thorpej 1323: #ifdef POOL_DIAGNOSTIC
1.43 thorpej 1324: void
1325: _pool_put(struct pool *pp, void *v, const char *file, long line)
1326: {
1.101 thorpej 1327: struct pool_pagelist pq;
1328:
1329: LIST_INIT(&pq);
1.43 thorpej 1330:
1.134 ad 1331: mutex_enter(&pp->pr_lock);
1.43 thorpej 1332: pr_enter(pp, file, line);
1333:
1.56 sommerfe 1334: pr_log(pp, v, PRLOG_PUT, file, line);
1335:
1.101 thorpej 1336: pool_do_put(pp, v, &pq);
1.21 thorpej 1337:
1.25 thorpej 1338: pr_leave(pp);
1.134 ad 1339: mutex_exit(&pp->pr_lock);
1.101 thorpej 1340:
1.102 chs 1341: pr_pagelist_free(pp, &pq);
1.1 pk 1342: }
1.57 sommerfe 1343: #undef pool_put
1.59 thorpej 1344: #endif /* POOL_DIAGNOSTIC */
1.1 pk 1345:
1.56 sommerfe 1346: void
1347: pool_put(struct pool *pp, void *v)
1348: {
1.101 thorpej 1349: struct pool_pagelist pq;
1350:
1351: LIST_INIT(&pq);
1.56 sommerfe 1352:
1.134 ad 1353: mutex_enter(&pp->pr_lock);
1.101 thorpej 1354: pool_do_put(pp, v, &pq);
1.134 ad 1355: mutex_exit(&pp->pr_lock);
1.56 sommerfe 1356:
1.102 chs 1357: pr_pagelist_free(pp, &pq);
1.56 sommerfe 1358: }
1.57 sommerfe 1359:
1.59 thorpej 1360: #ifdef POOL_DIAGNOSTIC
1.57 sommerfe 1361: #define pool_put(h, v) _pool_put((h), (v), __FILE__, __LINE__)
1.56 sommerfe 1362: #endif
1.74 thorpej 1363:
1364: /*
1.113 yamt 1365: * pool_grow: grow a pool by a page.
1366: *
1367: * => called with pool locked.
1368: * => unlock and relock the pool.
1369: * => return with pool locked.
1370: */
1371:
1372: static int
1373: pool_grow(struct pool *pp, int flags)
1374: {
1375: struct pool_item_header *ph = NULL;
1376: char *cp;
1377:
1.134 ad 1378: mutex_exit(&pp->pr_lock);
1.113 yamt 1379: cp = pool_allocator_alloc(pp, flags);
1380: if (__predict_true(cp != NULL)) {
1381: ph = pool_alloc_item_header(pp, cp, flags);
1382: }
1383: if (__predict_false(cp == NULL || ph == NULL)) {
1384: if (cp != NULL) {
1385: pool_allocator_free(pp, cp);
1386: }
1.134 ad 1387: mutex_enter(&pp->pr_lock);
1.113 yamt 1388: return ENOMEM;
1389: }
1390:
1.134 ad 1391: mutex_enter(&pp->pr_lock);
1.113 yamt 1392: pool_prime_page(pp, cp, ph);
1393: pp->pr_npagealloc++;
1394: return 0;
1395: }
1396:
1397: /*
1.74 thorpej 1398: * Add N items to the pool.
1399: */
1400: int
1401: pool_prime(struct pool *pp, int n)
1402: {
1.75 simonb 1403: int newpages;
1.113 yamt 1404: int error = 0;
1.74 thorpej 1405:
1.134 ad 1406: mutex_enter(&pp->pr_lock);
1.74 thorpej 1407:
1408: newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1409:
1410: while (newpages-- > 0) {
1.113 yamt 1411: error = pool_grow(pp, PR_NOWAIT);
1412: if (error) {
1.74 thorpej 1413: break;
1414: }
1415: pp->pr_minpages++;
1416: }
1417:
1418: if (pp->pr_minpages >= pp->pr_maxpages)
1419: pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */
1420:
1.134 ad 1421: mutex_exit(&pp->pr_lock);
1.113 yamt 1422: return error;
1.74 thorpej 1423: }
1.55 thorpej 1424:
1425: /*
1.3 pk 1426: * Add a page worth of items to the pool.
1.21 thorpej 1427: *
1428: * Note, we must be called with the pool descriptor LOCKED.
1.3 pk 1429: */
1.55 thorpej 1430: static void
1.128 christos 1431: pool_prime_page(struct pool *pp, void *storage, struct pool_item_header *ph)
1.3 pk 1432: {
1433: struct pool_item *pi;
1.128 christos 1434: void *cp = storage;
1.125 ad 1435: const unsigned int align = pp->pr_align;
1436: const unsigned int ioff = pp->pr_itemoffset;
1.55 thorpej 1437: int n;
1.36 pk 1438:
1.134 ad 1439: KASSERT(mutex_owned(&pp->pr_lock));
1.91 yamt 1440:
1.66 thorpej 1441: #ifdef DIAGNOSTIC
1.121 yamt 1442: if ((pp->pr_roflags & PR_NOALIGN) == 0 &&
1.150 skrll 1443: ((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0)
1.36 pk 1444: panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1.66 thorpej 1445: #endif
1.3 pk 1446:
1447: /*
1448: * Insert page header.
1449: */
1.88 chs 1450: LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.102 chs 1451: LIST_INIT(&ph->ph_itemlist);
1.3 pk 1452: ph->ph_page = storage;
1453: ph->ph_nmissing = 0;
1.151 yamt 1454: ph->ph_time = time_uptime;
1.88 chs 1455: if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1456: SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
1.3 pk 1457:
1.6 thorpej 1458: pp->pr_nidle++;
1459:
1.3 pk 1460: /*
1461: * Color this page.
1462: */
1.141 yamt 1463: ph->ph_off = pp->pr_curcolor;
1464: cp = (char *)cp + ph->ph_off;
1.3 pk 1465: if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
1466: pp->pr_curcolor = 0;
1467:
1468: /*
1469: * Adjust storage to apply aligment to `pr_itemoffset' in each item.
1470: */
1471: if (ioff != 0)
1.128 christos 1472: cp = (char *)cp + align - ioff;
1.3 pk 1473:
1.125 ad 1474: KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1475:
1.3 pk 1476: /*
1477: * Insert remaining chunks on the bucket list.
1478: */
1479: n = pp->pr_itemsperpage;
1.20 thorpej 1480: pp->pr_nitems += n;
1.3 pk 1481:
1.97 yamt 1482: if (pp->pr_roflags & PR_NOTOUCH) {
1.141 yamt 1483: pr_item_notouch_init(pp, ph);
1.97 yamt 1484: } else {
1485: while (n--) {
1486: pi = (struct pool_item *)cp;
1.78 thorpej 1487:
1.97 yamt 1488: KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
1.3 pk 1489:
1.97 yamt 1490: /* Insert on page list */
1.102 chs 1491: LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.3 pk 1492: #ifdef DIAGNOSTIC
1.97 yamt 1493: pi->pi_magic = PI_MAGIC;
1.3 pk 1494: #endif
1.128 christos 1495: cp = (char *)cp + pp->pr_size;
1.125 ad 1496:
1497: KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1.97 yamt 1498: }
1.3 pk 1499: }
1500:
1501: /*
1502: * If the pool was depleted, point at the new page.
1503: */
1504: if (pp->pr_curpage == NULL)
1505: pp->pr_curpage = ph;
1506:
1507: if (++pp->pr_npages > pp->pr_hiwat)
1508: pp->pr_hiwat = pp->pr_npages;
1509: }
1510:
1.20 thorpej 1511: /*
1.52 thorpej 1512: * Used by pool_get() when nitems drops below the low water mark. This
1.88 chs 1513: * is used to catch up pr_nitems with the low water mark.
1.20 thorpej 1514: *
1.21 thorpej 1515: * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20 thorpej 1516: *
1.73 thorpej 1517: * Note 2, we must be called with the pool already locked, and we return
1.20 thorpej 1518: * with it locked.
1519: */
1520: static int
1.42 thorpej 1521: pool_catchup(struct pool *pp)
1.20 thorpej 1522: {
1523: int error = 0;
1524:
1.54 thorpej 1525: while (POOL_NEEDS_CATCHUP(pp)) {
1.113 yamt 1526: error = pool_grow(pp, PR_NOWAIT);
1527: if (error) {
1.20 thorpej 1528: break;
1529: }
1530: }
1.113 yamt 1531: return error;
1.20 thorpej 1532: }
1533:
1.88 chs 1534: static void
1535: pool_update_curpage(struct pool *pp)
1536: {
1537:
1538: pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
1539: if (pp->pr_curpage == NULL) {
1540: pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
1541: }
1.168 yamt 1542: KASSERT((pp->pr_curpage == NULL && pp->pr_nitems == 0) ||
1543: (pp->pr_curpage != NULL && pp->pr_nitems > 0));
1.88 chs 1544: }
1545:
1.3 pk 1546: void
1.42 thorpej 1547: pool_setlowat(struct pool *pp, int n)
1.3 pk 1548: {
1.15 pk 1549:
1.134 ad 1550: mutex_enter(&pp->pr_lock);
1.21 thorpej 1551:
1.3 pk 1552: pp->pr_minitems = n;
1.15 pk 1553: pp->pr_minpages = (n == 0)
1554: ? 0
1.18 thorpej 1555: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20 thorpej 1556:
1557: /* Make sure we're caught up with the newly-set low water mark. */
1.75 simonb 1558: if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20 thorpej 1559: /*
1560: * XXX: Should we log a warning? Should we set up a timeout
1561: * to try again in a second or so? The latter could break
1562: * a caller's assumptions about interrupt protection, etc.
1563: */
1564: }
1.21 thorpej 1565:
1.134 ad 1566: mutex_exit(&pp->pr_lock);
1.3 pk 1567: }
1568:
1569: void
1.42 thorpej 1570: pool_sethiwat(struct pool *pp, int n)
1.3 pk 1571: {
1.15 pk 1572:
1.134 ad 1573: mutex_enter(&pp->pr_lock);
1.21 thorpej 1574:
1.15 pk 1575: pp->pr_maxpages = (n == 0)
1576: ? 0
1.18 thorpej 1577: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1578:
1.134 ad 1579: mutex_exit(&pp->pr_lock);
1.3 pk 1580: }
1581:
1.20 thorpej 1582: void
1.42 thorpej 1583: pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1.20 thorpej 1584: {
1585:
1.134 ad 1586: mutex_enter(&pp->pr_lock);
1.20 thorpej 1587:
1588: pp->pr_hardlimit = n;
1589: pp->pr_hardlimit_warning = warnmess;
1.31 thorpej 1590: pp->pr_hardlimit_ratecap.tv_sec = ratecap;
1591: pp->pr_hardlimit_warning_last.tv_sec = 0;
1592: pp->pr_hardlimit_warning_last.tv_usec = 0;
1.20 thorpej 1593:
1594: /*
1.21 thorpej 1595: * In-line version of pool_sethiwat(), because we don't want to
1596: * release the lock.
1.20 thorpej 1597: */
1598: pp->pr_maxpages = (n == 0)
1599: ? 0
1600: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1601:
1.134 ad 1602: mutex_exit(&pp->pr_lock);
1.20 thorpej 1603: }
1.3 pk 1604:
1605: /*
1606: * Release all complete pages that have not been used recently.
1607: */
1.66 thorpej 1608: int
1.59 thorpej 1609: #ifdef POOL_DIAGNOSTIC
1.42 thorpej 1610: _pool_reclaim(struct pool *pp, const char *file, long line)
1.56 sommerfe 1611: #else
1612: pool_reclaim(struct pool *pp)
1613: #endif
1.3 pk 1614: {
1615: struct pool_item_header *ph, *phnext;
1.61 chs 1616: struct pool_pagelist pq;
1.151 yamt 1617: uint32_t curtime;
1.134 ad 1618: bool klock;
1619: int rv;
1.3 pk 1620:
1.68 thorpej 1621: if (pp->pr_drain_hook != NULL) {
1622: /*
1623: * The drain hook must be called with the pool unlocked.
1624: */
1625: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT);
1626: }
1627:
1.134 ad 1628: /*
1.157 ad 1629: * XXXSMP Because we do not want to cause non-MPSAFE code
1630: * to block.
1.134 ad 1631: */
1632: if (pp->pr_ipl == IPL_SOFTNET || pp->pr_ipl == IPL_SOFTCLOCK ||
1633: pp->pr_ipl == IPL_SOFTSERIAL) {
1634: KERNEL_LOCK(1, NULL);
1635: klock = true;
1636: } else
1637: klock = false;
1638:
1639: /* Reclaim items from the pool's cache (if any). */
1640: if (pp->pr_cache != NULL)
1641: pool_cache_invalidate(pp->pr_cache);
1642:
1643: if (mutex_tryenter(&pp->pr_lock) == 0) {
1644: if (klock) {
1645: KERNEL_UNLOCK_ONE(NULL);
1646: }
1.66 thorpej 1647: return (0);
1.134 ad 1648: }
1.25 thorpej 1649: pr_enter(pp, file, line);
1.68 thorpej 1650:
1.88 chs 1651: LIST_INIT(&pq);
1.43 thorpej 1652:
1.151 yamt 1653: curtime = time_uptime;
1.21 thorpej 1654:
1.88 chs 1655: for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
1656: phnext = LIST_NEXT(ph, ph_pagelist);
1.3 pk 1657:
1658: /* Check our minimum page claim */
1659: if (pp->pr_npages <= pp->pr_minpages)
1660: break;
1661:
1.88 chs 1662: KASSERT(ph->ph_nmissing == 0);
1.151 yamt 1663: if (curtime - ph->ph_time < pool_inactive_time
1.117 yamt 1664: && !pa_starved_p(pp->pr_alloc))
1.88 chs 1665: continue;
1.21 thorpej 1666:
1.88 chs 1667: /*
1668: * If freeing this page would put us below
1669: * the low water mark, stop now.
1670: */
1671: if ((pp->pr_nitems - pp->pr_itemsperpage) <
1672: pp->pr_minitems)
1673: break;
1.21 thorpej 1674:
1.88 chs 1675: pr_rmpage(pp, ph, &pq);
1.3 pk 1676: }
1677:
1.25 thorpej 1678: pr_leave(pp);
1.134 ad 1679: mutex_exit(&pp->pr_lock);
1680:
1681: if (LIST_EMPTY(&pq))
1682: rv = 0;
1683: else {
1684: pr_pagelist_free(pp, &pq);
1685: rv = 1;
1686: }
1687:
1688: if (klock) {
1689: KERNEL_UNLOCK_ONE(NULL);
1690: }
1.66 thorpej 1691:
1.134 ad 1692: return (rv);
1.3 pk 1693: }
1694:
1695: /*
1.134 ad 1696: * Drain pools, one at a time. This is a two stage process;
1697: * drain_start kicks off a cross call to drain CPU-level caches
1698: * if the pool has an associated pool_cache. drain_end waits
1699: * for those cross calls to finish, and then drains the cache
1700: * (if any) and pool.
1.131 ad 1701: *
1.134 ad 1702: * Note, must never be called from interrupt context.
1.3 pk 1703: */
1704: void
1.134 ad 1705: pool_drain_start(struct pool **ppp, uint64_t *wp)
1.3 pk 1706: {
1707: struct pool *pp;
1.134 ad 1708:
1.145 ad 1709: KASSERT(!TAILQ_EMPTY(&pool_head));
1.3 pk 1710:
1.61 chs 1711: pp = NULL;
1.134 ad 1712:
1713: /* Find next pool to drain, and add a reference. */
1714: mutex_enter(&pool_head_lock);
1715: do {
1716: if (drainpp == NULL) {
1.145 ad 1717: drainpp = TAILQ_FIRST(&pool_head);
1.134 ad 1718: }
1719: if (drainpp != NULL) {
1720: pp = drainpp;
1.145 ad 1721: drainpp = TAILQ_NEXT(pp, pr_poollist);
1.134 ad 1722: }
1723: /*
1724: * Skip completely idle pools. We depend on at least
1725: * one pool in the system being active.
1726: */
1727: } while (pp == NULL || pp->pr_npages == 0);
1728: pp->pr_refcnt++;
1729: mutex_exit(&pool_head_lock);
1730:
1731: /* If there is a pool_cache, drain CPU level caches. */
1732: *ppp = pp;
1733: if (pp->pr_cache != NULL) {
1734: *wp = xc_broadcast(0, (xcfunc_t)pool_cache_xcall,
1735: pp->pr_cache, NULL);
1736: }
1737: }
1738:
1739: void
1740: pool_drain_end(struct pool *pp, uint64_t where)
1741: {
1742:
1743: if (pp == NULL)
1744: return;
1745:
1746: KASSERT(pp->pr_refcnt > 0);
1747:
1748: /* Wait for remote draining to complete. */
1749: if (pp->pr_cache != NULL)
1750: xc_wait(where);
1751:
1752: /* Drain the cache (if any) and pool.. */
1753: pool_reclaim(pp);
1754:
1755: /* Finally, unlock the pool. */
1756: mutex_enter(&pool_head_lock);
1757: pp->pr_refcnt--;
1758: cv_broadcast(&pool_busy);
1759: mutex_exit(&pool_head_lock);
1.3 pk 1760: }
1761:
1762: /*
1763: * Diagnostic helpers.
1764: */
1765: void
1.42 thorpej 1766: pool_print(struct pool *pp, const char *modif)
1.21 thorpej 1767: {
1768:
1.25 thorpej 1769: pool_print1(pp, modif, printf);
1.21 thorpej 1770: }
1771:
1.25 thorpej 1772: void
1.108 yamt 1773: pool_printall(const char *modif, void (*pr)(const char *, ...))
1774: {
1775: struct pool *pp;
1776:
1.145 ad 1777: TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
1.108 yamt 1778: pool_printit(pp, modif, pr);
1779: }
1780: }
1781:
1782: void
1.42 thorpej 1783: pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.25 thorpej 1784: {
1785:
1786: if (pp == NULL) {
1787: (*pr)("Must specify a pool to print.\n");
1788: return;
1789: }
1790:
1791: pool_print1(pp, modif, pr);
1792: }
1793:
1.21 thorpej 1794: static void
1.124 yamt 1795: pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
1.97 yamt 1796: void (*pr)(const char *, ...))
1.88 chs 1797: {
1798: struct pool_item_header *ph;
1799: #ifdef DIAGNOSTIC
1800: struct pool_item *pi;
1801: #endif
1802:
1803: LIST_FOREACH(ph, pl, ph_pagelist) {
1.151 yamt 1804: (*pr)("\t\tpage %p, nmissing %d, time %" PRIu32 "\n",
1805: ph->ph_page, ph->ph_nmissing, ph->ph_time);
1.88 chs 1806: #ifdef DIAGNOSTIC
1.97 yamt 1807: if (!(pp->pr_roflags & PR_NOTOUCH)) {
1.102 chs 1808: LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1.97 yamt 1809: if (pi->pi_magic != PI_MAGIC) {
1810: (*pr)("\t\t\titem %p, magic 0x%x\n",
1811: pi, pi->pi_magic);
1812: }
1.88 chs 1813: }
1814: }
1815: #endif
1816: }
1817: }
1818:
1819: static void
1.42 thorpej 1820: pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.3 pk 1821: {
1.25 thorpej 1822: struct pool_item_header *ph;
1.134 ad 1823: pool_cache_t pc;
1824: pcg_t *pcg;
1825: pool_cache_cpu_t *cc;
1826: uint64_t cpuhit, cpumiss;
1.44 thorpej 1827: int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1.25 thorpej 1828: char c;
1829:
1830: while ((c = *modif++) != '\0') {
1831: if (c == 'l')
1832: print_log = 1;
1833: if (c == 'p')
1834: print_pagelist = 1;
1.44 thorpej 1835: if (c == 'c')
1836: print_cache = 1;
1.25 thorpej 1837: }
1838:
1.134 ad 1839: if ((pc = pp->pr_cache) != NULL) {
1840: (*pr)("POOL CACHE");
1841: } else {
1842: (*pr)("POOL");
1843: }
1844:
1845: (*pr)(" %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1.25 thorpej 1846: pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1847: pp->pr_roflags);
1.66 thorpej 1848: (*pr)("\talloc %p\n", pp->pr_alloc);
1.25 thorpej 1849: (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1850: pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1851: (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1852: pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1853:
1.134 ad 1854: (*pr)("\tnget %lu, nfail %lu, nput %lu\n",
1.25 thorpej 1855: pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1856: (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1857: pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1858:
1859: if (print_pagelist == 0)
1860: goto skip_pagelist;
1861:
1.88 chs 1862: if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1863: (*pr)("\n\tempty page list:\n");
1.97 yamt 1864: pool_print_pagelist(pp, &pp->pr_emptypages, pr);
1.88 chs 1865: if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
1866: (*pr)("\n\tfull page list:\n");
1.97 yamt 1867: pool_print_pagelist(pp, &pp->pr_fullpages, pr);
1.88 chs 1868: if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
1869: (*pr)("\n\tpartial-page list:\n");
1.97 yamt 1870: pool_print_pagelist(pp, &pp->pr_partpages, pr);
1.88 chs 1871:
1.25 thorpej 1872: if (pp->pr_curpage == NULL)
1873: (*pr)("\tno current page\n");
1874: else
1875: (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1876:
1877: skip_pagelist:
1878: if (print_log == 0)
1879: goto skip_log;
1880:
1881: (*pr)("\n");
1882: if ((pp->pr_roflags & PR_LOGGING) == 0)
1883: (*pr)("\tno log\n");
1.122 christos 1884: else {
1.25 thorpej 1885: pr_printlog(pp, NULL, pr);
1.122 christos 1886: }
1.3 pk 1887:
1.25 thorpej 1888: skip_log:
1.44 thorpej 1889:
1.102 chs 1890: #define PR_GROUPLIST(pcg) \
1891: (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail); \
1.142 ad 1892: for (i = 0; i < pcg->pcg_size; i++) { \
1.102 chs 1893: if (pcg->pcg_objects[i].pcgo_pa != \
1894: POOL_PADDR_INVALID) { \
1895: (*pr)("\t\t\t%p, 0x%llx\n", \
1896: pcg->pcg_objects[i].pcgo_va, \
1897: (unsigned long long) \
1898: pcg->pcg_objects[i].pcgo_pa); \
1899: } else { \
1900: (*pr)("\t\t\t%p\n", \
1901: pcg->pcg_objects[i].pcgo_va); \
1902: } \
1903: }
1904:
1.134 ad 1905: if (pc != NULL) {
1906: cpuhit = 0;
1907: cpumiss = 0;
1.183 ! ad 1908: for (i = 0; i < __arraycount(pc->pc_cpus); i++) {
1.134 ad 1909: if ((cc = pc->pc_cpus[i]) == NULL)
1910: continue;
1911: cpuhit += cc->cc_hits;
1912: cpumiss += cc->cc_misses;
1913: }
1914: (*pr)("\tcpu layer hits %llu misses %llu\n", cpuhit, cpumiss);
1915: (*pr)("\tcache layer hits %llu misses %llu\n",
1916: pc->pc_hits, pc->pc_misses);
1917: (*pr)("\tcache layer entry uncontended %llu contended %llu\n",
1918: pc->pc_hits + pc->pc_misses - pc->pc_contended,
1919: pc->pc_contended);
1920: (*pr)("\tcache layer empty groups %u full groups %u\n",
1921: pc->pc_nempty, pc->pc_nfull);
1922: if (print_cache) {
1923: (*pr)("\tfull cache groups:\n");
1924: for (pcg = pc->pc_fullgroups; pcg != NULL;
1925: pcg = pcg->pcg_next) {
1926: PR_GROUPLIST(pcg);
1927: }
1928: (*pr)("\tempty cache groups:\n");
1929: for (pcg = pc->pc_emptygroups; pcg != NULL;
1930: pcg = pcg->pcg_next) {
1931: PR_GROUPLIST(pcg);
1932: }
1.103 chs 1933: }
1.44 thorpej 1934: }
1.102 chs 1935: #undef PR_GROUPLIST
1.44 thorpej 1936:
1.88 chs 1937: pr_enter_check(pp, pr);
1938: }
1939:
1940: static int
1941: pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
1942: {
1943: struct pool_item *pi;
1.128 christos 1944: void *page;
1.88 chs 1945: int n;
1946:
1.121 yamt 1947: if ((pp->pr_roflags & PR_NOALIGN) == 0) {
1.128 christos 1948: page = (void *)((uintptr_t)ph & pp->pr_alloc->pa_pagemask);
1.121 yamt 1949: if (page != ph->ph_page &&
1950: (pp->pr_roflags & PR_PHINPAGE) != 0) {
1951: if (label != NULL)
1952: printf("%s: ", label);
1953: printf("pool(%p:%s): page inconsistency: page %p;"
1954: " at page head addr %p (p %p)\n", pp,
1955: pp->pr_wchan, ph->ph_page,
1956: ph, page);
1957: return 1;
1958: }
1.88 chs 1959: }
1.3 pk 1960:
1.97 yamt 1961: if ((pp->pr_roflags & PR_NOTOUCH) != 0)
1962: return 0;
1963:
1.102 chs 1964: for (pi = LIST_FIRST(&ph->ph_itemlist), n = 0;
1.88 chs 1965: pi != NULL;
1.102 chs 1966: pi = LIST_NEXT(pi,pi_list), n++) {
1.88 chs 1967:
1968: #ifdef DIAGNOSTIC
1969: if (pi->pi_magic != PI_MAGIC) {
1970: if (label != NULL)
1971: printf("%s: ", label);
1972: printf("pool(%s): free list modified: magic=%x;"
1.121 yamt 1973: " page %p; item ordinal %d; addr %p\n",
1.88 chs 1974: pp->pr_wchan, pi->pi_magic, ph->ph_page,
1.121 yamt 1975: n, pi);
1.88 chs 1976: panic("pool");
1977: }
1978: #endif
1.121 yamt 1979: if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1980: continue;
1981: }
1.128 christos 1982: page = (void *)((uintptr_t)pi & pp->pr_alloc->pa_pagemask);
1.88 chs 1983: if (page == ph->ph_page)
1984: continue;
1985:
1986: if (label != NULL)
1987: printf("%s: ", label);
1988: printf("pool(%p:%s): page inconsistency: page %p;"
1989: " item ordinal %d; addr %p (p %p)\n", pp,
1990: pp->pr_wchan, ph->ph_page,
1991: n, pi, page);
1992: return 1;
1993: }
1994: return 0;
1.3 pk 1995: }
1996:
1.88 chs 1997:
1.3 pk 1998: int
1.42 thorpej 1999: pool_chk(struct pool *pp, const char *label)
1.3 pk 2000: {
2001: struct pool_item_header *ph;
2002: int r = 0;
2003:
1.134 ad 2004: mutex_enter(&pp->pr_lock);
1.88 chs 2005: LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
2006: r = pool_chk_page(pp, label, ph);
2007: if (r) {
2008: goto out;
2009: }
2010: }
2011: LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
2012: r = pool_chk_page(pp, label, ph);
2013: if (r) {
1.3 pk 2014: goto out;
2015: }
1.88 chs 2016: }
2017: LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
2018: r = pool_chk_page(pp, label, ph);
2019: if (r) {
1.3 pk 2020: goto out;
2021: }
2022: }
1.88 chs 2023:
1.3 pk 2024: out:
1.134 ad 2025: mutex_exit(&pp->pr_lock);
1.3 pk 2026: return (r);
1.43 thorpej 2027: }
2028:
2029: /*
2030: * pool_cache_init:
2031: *
2032: * Initialize a pool cache.
1.134 ad 2033: */
2034: pool_cache_t
2035: pool_cache_init(size_t size, u_int align, u_int align_offset, u_int flags,
2036: const char *wchan, struct pool_allocator *palloc, int ipl,
2037: int (*ctor)(void *, void *, int), void (*dtor)(void *, void *), void *arg)
2038: {
2039: pool_cache_t pc;
2040:
2041: pc = pool_get(&cache_pool, PR_WAITOK);
2042: if (pc == NULL)
2043: return NULL;
2044:
2045: pool_cache_bootstrap(pc, size, align, align_offset, flags, wchan,
2046: palloc, ipl, ctor, dtor, arg);
2047:
2048: return pc;
2049: }
2050:
2051: /*
2052: * pool_cache_bootstrap:
1.43 thorpej 2053: *
1.134 ad 2054: * Kernel-private version of pool_cache_init(). The caller
2055: * provides initial storage.
1.43 thorpej 2056: */
2057: void
1.134 ad 2058: pool_cache_bootstrap(pool_cache_t pc, size_t size, u_int align,
2059: u_int align_offset, u_int flags, const char *wchan,
2060: struct pool_allocator *palloc, int ipl,
2061: int (*ctor)(void *, void *, int), void (*dtor)(void *, void *),
1.43 thorpej 2062: void *arg)
2063: {
1.134 ad 2064: CPU_INFO_ITERATOR cii;
1.145 ad 2065: pool_cache_t pc1;
1.134 ad 2066: struct cpu_info *ci;
2067: struct pool *pp;
2068:
2069: pp = &pc->pc_pool;
2070: if (palloc == NULL && ipl == IPL_NONE)
2071: palloc = &pool_allocator_nointr;
2072: pool_init(pp, size, align, align_offset, flags, wchan, palloc, ipl);
1.157 ad 2073: mutex_init(&pc->pc_lock, MUTEX_DEFAULT, ipl);
1.43 thorpej 2074:
1.134 ad 2075: if (ctor == NULL) {
2076: ctor = (int (*)(void *, void *, int))nullop;
2077: }
2078: if (dtor == NULL) {
2079: dtor = (void (*)(void *, void *))nullop;
2080: }
1.43 thorpej 2081:
1.134 ad 2082: pc->pc_emptygroups = NULL;
2083: pc->pc_fullgroups = NULL;
2084: pc->pc_partgroups = NULL;
1.43 thorpej 2085: pc->pc_ctor = ctor;
2086: pc->pc_dtor = dtor;
2087: pc->pc_arg = arg;
1.134 ad 2088: pc->pc_hits = 0;
1.48 thorpej 2089: pc->pc_misses = 0;
1.134 ad 2090: pc->pc_nempty = 0;
2091: pc->pc_npart = 0;
2092: pc->pc_nfull = 0;
2093: pc->pc_contended = 0;
2094: pc->pc_refcnt = 0;
1.136 yamt 2095: pc->pc_freecheck = NULL;
1.134 ad 2096:
1.142 ad 2097: if ((flags & PR_LARGECACHE) != 0) {
2098: pc->pc_pcgsize = PCG_NOBJECTS_LARGE;
1.163 ad 2099: pc->pc_pcgpool = &pcg_large_pool;
1.142 ad 2100: } else {
2101: pc->pc_pcgsize = PCG_NOBJECTS_NORMAL;
1.163 ad 2102: pc->pc_pcgpool = &pcg_normal_pool;
1.142 ad 2103: }
2104:
1.134 ad 2105: /* Allocate per-CPU caches. */
2106: memset(pc->pc_cpus, 0, sizeof(pc->pc_cpus));
2107: pc->pc_ncpu = 0;
1.139 ad 2108: if (ncpu < 2) {
1.137 ad 2109: /* XXX For sparc: boot CPU is not attached yet. */
2110: pool_cache_cpu_init1(curcpu(), pc);
2111: } else {
2112: for (CPU_INFO_FOREACH(cii, ci)) {
2113: pool_cache_cpu_init1(ci, pc);
2114: }
1.134 ad 2115: }
1.145 ad 2116:
2117: /* Add to list of all pools. */
2118: if (__predict_true(!cold))
1.134 ad 2119: mutex_enter(&pool_head_lock);
1.145 ad 2120: TAILQ_FOREACH(pc1, &pool_cache_head, pc_cachelist) {
2121: if (strcmp(pc1->pc_pool.pr_wchan, pc->pc_pool.pr_wchan) > 0)
2122: break;
2123: }
2124: if (pc1 == NULL)
2125: TAILQ_INSERT_TAIL(&pool_cache_head, pc, pc_cachelist);
2126: else
2127: TAILQ_INSERT_BEFORE(pc1, pc, pc_cachelist);
2128: if (__predict_true(!cold))
1.134 ad 2129: mutex_exit(&pool_head_lock);
1.145 ad 2130:
2131: membar_sync();
2132: pp->pr_cache = pc;
1.43 thorpej 2133: }
2134:
2135: /*
2136: * pool_cache_destroy:
2137: *
2138: * Destroy a pool cache.
2139: */
2140: void
1.134 ad 2141: pool_cache_destroy(pool_cache_t pc)
1.43 thorpej 2142: {
1.134 ad 2143: struct pool *pp = &pc->pc_pool;
1.175 jym 2144: u_int i;
1.134 ad 2145:
2146: /* Remove it from the global list. */
2147: mutex_enter(&pool_head_lock);
2148: while (pc->pc_refcnt != 0)
2149: cv_wait(&pool_busy, &pool_head_lock);
1.145 ad 2150: TAILQ_REMOVE(&pool_cache_head, pc, pc_cachelist);
1.134 ad 2151: mutex_exit(&pool_head_lock);
1.43 thorpej 2152:
2153: /* First, invalidate the entire cache. */
2154: pool_cache_invalidate(pc);
2155:
1.134 ad 2156: /* Disassociate it from the pool. */
2157: mutex_enter(&pp->pr_lock);
2158: pp->pr_cache = NULL;
2159: mutex_exit(&pp->pr_lock);
2160:
2161: /* Destroy per-CPU data */
1.183 ! ad 2162: for (i = 0; i < __arraycount(pc->pc_cpus); i++)
1.175 jym 2163: pool_cache_invalidate_cpu(pc, i);
1.134 ad 2164:
2165: /* Finally, destroy it. */
2166: mutex_destroy(&pc->pc_lock);
2167: pool_destroy(pp);
2168: pool_put(&cache_pool, pc);
2169: }
2170:
2171: /*
2172: * pool_cache_cpu_init1:
2173: *
2174: * Called for each pool_cache whenever a new CPU is attached.
2175: */
2176: static void
2177: pool_cache_cpu_init1(struct cpu_info *ci, pool_cache_t pc)
2178: {
2179: pool_cache_cpu_t *cc;
1.137 ad 2180: int index;
1.134 ad 2181:
1.137 ad 2182: index = ci->ci_index;
2183:
1.183 ! ad 2184: KASSERT(index < __arraycount(pc->pc_cpus));
1.134 ad 2185:
1.137 ad 2186: if ((cc = pc->pc_cpus[index]) != NULL) {
2187: KASSERT(cc->cc_cpuindex == index);
1.134 ad 2188: return;
2189: }
2190:
2191: /*
2192: * The first CPU is 'free'. This needs to be the case for
2193: * bootstrap - we may not be able to allocate yet.
2194: */
2195: if (pc->pc_ncpu == 0) {
2196: cc = &pc->pc_cpu0;
2197: pc->pc_ncpu = 1;
2198: } else {
2199: mutex_enter(&pc->pc_lock);
2200: pc->pc_ncpu++;
2201: mutex_exit(&pc->pc_lock);
2202: cc = pool_get(&cache_cpu_pool, PR_WAITOK);
2203: }
2204:
2205: cc->cc_ipl = pc->pc_pool.pr_ipl;
2206: cc->cc_iplcookie = makeiplcookie(cc->cc_ipl);
2207: cc->cc_cache = pc;
1.137 ad 2208: cc->cc_cpuindex = index;
1.134 ad 2209: cc->cc_hits = 0;
2210: cc->cc_misses = 0;
1.169 yamt 2211: cc->cc_current = __UNCONST(&pcg_dummy);
2212: cc->cc_previous = __UNCONST(&pcg_dummy);
1.134 ad 2213:
1.137 ad 2214: pc->pc_cpus[index] = cc;
1.43 thorpej 2215: }
2216:
1.134 ad 2217: /*
2218: * pool_cache_cpu_init:
2219: *
2220: * Called whenever a new CPU is attached.
2221: */
2222: void
2223: pool_cache_cpu_init(struct cpu_info *ci)
1.43 thorpej 2224: {
1.134 ad 2225: pool_cache_t pc;
2226:
2227: mutex_enter(&pool_head_lock);
1.145 ad 2228: TAILQ_FOREACH(pc, &pool_cache_head, pc_cachelist) {
1.134 ad 2229: pc->pc_refcnt++;
2230: mutex_exit(&pool_head_lock);
1.43 thorpej 2231:
1.134 ad 2232: pool_cache_cpu_init1(ci, pc);
1.43 thorpej 2233:
1.134 ad 2234: mutex_enter(&pool_head_lock);
2235: pc->pc_refcnt--;
2236: cv_broadcast(&pool_busy);
2237: }
2238: mutex_exit(&pool_head_lock);
1.43 thorpej 2239: }
2240:
1.134 ad 2241: /*
2242: * pool_cache_reclaim:
2243: *
2244: * Reclaim memory from a pool cache.
2245: */
2246: bool
2247: pool_cache_reclaim(pool_cache_t pc)
1.43 thorpej 2248: {
2249:
1.134 ad 2250: return pool_reclaim(&pc->pc_pool);
2251: }
1.43 thorpej 2252:
1.136 yamt 2253: static void
2254: pool_cache_destruct_object1(pool_cache_t pc, void *object)
2255: {
2256:
2257: (*pc->pc_dtor)(pc->pc_arg, object);
2258: pool_put(&pc->pc_pool, object);
2259: }
2260:
1.134 ad 2261: /*
2262: * pool_cache_destruct_object:
2263: *
2264: * Force destruction of an object and its release back into
2265: * the pool.
2266: */
2267: void
2268: pool_cache_destruct_object(pool_cache_t pc, void *object)
2269: {
2270:
1.136 yamt 2271: FREECHECK_IN(&pc->pc_freecheck, object);
2272:
2273: pool_cache_destruct_object1(pc, object);
1.43 thorpej 2274: }
2275:
1.134 ad 2276: /*
2277: * pool_cache_invalidate_groups:
2278: *
2279: * Invalidate a chain of groups and destruct all objects.
2280: */
1.102 chs 2281: static void
1.134 ad 2282: pool_cache_invalidate_groups(pool_cache_t pc, pcg_t *pcg)
1.102 chs 2283: {
1.134 ad 2284: void *object;
2285: pcg_t *next;
2286: int i;
2287:
2288: for (; pcg != NULL; pcg = next) {
2289: next = pcg->pcg_next;
2290:
2291: for (i = 0; i < pcg->pcg_avail; i++) {
2292: object = pcg->pcg_objects[i].pcgo_va;
1.136 yamt 2293: pool_cache_destruct_object1(pc, object);
1.134 ad 2294: }
1.102 chs 2295:
1.142 ad 2296: if (pcg->pcg_size == PCG_NOBJECTS_LARGE) {
2297: pool_put(&pcg_large_pool, pcg);
2298: } else {
2299: KASSERT(pcg->pcg_size == PCG_NOBJECTS_NORMAL);
2300: pool_put(&pcg_normal_pool, pcg);
2301: }
1.102 chs 2302: }
2303: }
2304:
1.43 thorpej 2305: /*
1.134 ad 2306: * pool_cache_invalidate:
1.43 thorpej 2307: *
1.134 ad 2308: * Invalidate a pool cache (destruct and release all of the
2309: * cached objects). Does not reclaim objects from the pool.
1.176 thorpej 2310: *
2311: * Note: For pool caches that provide constructed objects, there
2312: * is an assumption that another level of synchronization is occurring
2313: * between the input to the constructor and the cache invalidation.
1.43 thorpej 2314: */
1.134 ad 2315: void
2316: pool_cache_invalidate(pool_cache_t pc)
2317: {
2318: pcg_t *full, *empty, *part;
1.182 rmind 2319: #if 0
1.176 thorpej 2320: uint64_t where;
2321:
1.177 jym 2322: if (ncpu < 2 || !mp_online) {
1.176 thorpej 2323: /*
2324: * We might be called early enough in the boot process
2325: * for the CPU data structures to not be fully initialized.
2326: * In this case, simply gather the local CPU's cache now
2327: * since it will be the only one running.
2328: */
2329: pool_cache_xcall(pc);
2330: } else {
2331: /*
2332: * Gather all of the CPU-specific caches into the
2333: * global cache.
2334: */
2335: where = xc_broadcast(0, (xcfunc_t)pool_cache_xcall, pc, NULL);
2336: xc_wait(where);
2337: }
1.182 rmind 2338: #endif
1.134 ad 2339: mutex_enter(&pc->pc_lock);
2340: full = pc->pc_fullgroups;
2341: empty = pc->pc_emptygroups;
2342: part = pc->pc_partgroups;
2343: pc->pc_fullgroups = NULL;
2344: pc->pc_emptygroups = NULL;
2345: pc->pc_partgroups = NULL;
2346: pc->pc_nfull = 0;
2347: pc->pc_nempty = 0;
2348: pc->pc_npart = 0;
2349: mutex_exit(&pc->pc_lock);
2350:
2351: pool_cache_invalidate_groups(pc, full);
2352: pool_cache_invalidate_groups(pc, empty);
2353: pool_cache_invalidate_groups(pc, part);
2354: }
2355:
1.175 jym 2356: /*
2357: * pool_cache_invalidate_cpu:
2358: *
2359: * Invalidate all CPU-bound cached objects in pool cache, the CPU being
2360: * identified by its associated index.
2361: * It is caller's responsibility to ensure that no operation is
2362: * taking place on this pool cache while doing this invalidation.
2363: * WARNING: as no inter-CPU locking is enforced, trying to invalidate
2364: * pool cached objects from a CPU different from the one currently running
2365: * may result in an undefined behaviour.
2366: */
2367: static void
2368: pool_cache_invalidate_cpu(pool_cache_t pc, u_int index)
2369: {
2370:
2371: pool_cache_cpu_t *cc;
2372: pcg_t *pcg;
2373:
2374: if ((cc = pc->pc_cpus[index]) == NULL)
2375: return;
2376:
2377: if ((pcg = cc->cc_current) != &pcg_dummy) {
2378: pcg->pcg_next = NULL;
2379: pool_cache_invalidate_groups(pc, pcg);
2380: }
2381: if ((pcg = cc->cc_previous) != &pcg_dummy) {
2382: pcg->pcg_next = NULL;
2383: pool_cache_invalidate_groups(pc, pcg);
2384: }
2385: if (cc != &pc->pc_cpu0)
2386: pool_put(&cache_cpu_pool, cc);
2387:
2388: }
2389:
1.134 ad 2390: void
2391: pool_cache_set_drain_hook(pool_cache_t pc, void (*fn)(void *, int), void *arg)
2392: {
2393:
2394: pool_set_drain_hook(&pc->pc_pool, fn, arg);
2395: }
2396:
2397: void
2398: pool_cache_setlowat(pool_cache_t pc, int n)
2399: {
2400:
2401: pool_setlowat(&pc->pc_pool, n);
2402: }
2403:
2404: void
2405: pool_cache_sethiwat(pool_cache_t pc, int n)
2406: {
2407:
2408: pool_sethiwat(&pc->pc_pool, n);
2409: }
2410:
2411: void
2412: pool_cache_sethardlimit(pool_cache_t pc, int n, const char *warnmess, int ratecap)
2413: {
2414:
2415: pool_sethardlimit(&pc->pc_pool, n, warnmess, ratecap);
2416: }
2417:
1.162 ad 2418: static bool __noinline
2419: pool_cache_get_slow(pool_cache_cpu_t *cc, int s, void **objectp,
1.134 ad 2420: paddr_t *pap, int flags)
1.43 thorpej 2421: {
1.134 ad 2422: pcg_t *pcg, *cur;
2423: uint64_t ncsw;
2424: pool_cache_t pc;
1.43 thorpej 2425: void *object;
1.58 thorpej 2426:
1.168 yamt 2427: KASSERT(cc->cc_current->pcg_avail == 0);
2428: KASSERT(cc->cc_previous->pcg_avail == 0);
2429:
1.134 ad 2430: pc = cc->cc_cache;
2431: cc->cc_misses++;
1.43 thorpej 2432:
1.134 ad 2433: /*
2434: * Nothing was available locally. Try and grab a group
2435: * from the cache.
2436: */
1.162 ad 2437: if (__predict_false(!mutex_tryenter(&pc->pc_lock))) {
1.134 ad 2438: ncsw = curlwp->l_ncsw;
2439: mutex_enter(&pc->pc_lock);
2440: pc->pc_contended++;
1.43 thorpej 2441:
1.134 ad 2442: /*
2443: * If we context switched while locking, then
2444: * our view of the per-CPU data is invalid:
2445: * retry.
2446: */
2447: if (curlwp->l_ncsw != ncsw) {
2448: mutex_exit(&pc->pc_lock);
1.162 ad 2449: return true;
1.43 thorpej 2450: }
1.102 chs 2451: }
1.43 thorpej 2452:
1.162 ad 2453: if (__predict_true((pcg = pc->pc_fullgroups) != NULL)) {
1.43 thorpej 2454: /*
1.134 ad 2455: * If there's a full group, release our empty
2456: * group back to the cache. Install the full
2457: * group as cc_current and return.
1.43 thorpej 2458: */
1.162 ad 2459: if (__predict_true((cur = cc->cc_current) != &pcg_dummy)) {
1.134 ad 2460: KASSERT(cur->pcg_avail == 0);
2461: cur->pcg_next = pc->pc_emptygroups;
2462: pc->pc_emptygroups = cur;
2463: pc->pc_nempty++;
1.87 thorpej 2464: }
1.142 ad 2465: KASSERT(pcg->pcg_avail == pcg->pcg_size);
1.134 ad 2466: cc->cc_current = pcg;
2467: pc->pc_fullgroups = pcg->pcg_next;
2468: pc->pc_hits++;
2469: pc->pc_nfull--;
2470: mutex_exit(&pc->pc_lock);
1.162 ad 2471: return true;
1.134 ad 2472: }
2473:
2474: /*
2475: * Nothing available locally or in cache. Take the slow
2476: * path: fetch a new object from the pool and construct
2477: * it.
2478: */
2479: pc->pc_misses++;
2480: mutex_exit(&pc->pc_lock);
1.162 ad 2481: splx(s);
1.134 ad 2482:
2483: object = pool_get(&pc->pc_pool, flags);
2484: *objectp = object;
1.162 ad 2485: if (__predict_false(object == NULL))
2486: return false;
1.125 ad 2487:
1.162 ad 2488: if (__predict_false((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0)) {
1.134 ad 2489: pool_put(&pc->pc_pool, object);
2490: *objectp = NULL;
1.162 ad 2491: return false;
1.43 thorpej 2492: }
2493:
1.134 ad 2494: KASSERT((((vaddr_t)object + pc->pc_pool.pr_itemoffset) &
2495: (pc->pc_pool.pr_align - 1)) == 0);
1.43 thorpej 2496:
1.134 ad 2497: if (pap != NULL) {
2498: #ifdef POOL_VTOPHYS
2499: *pap = POOL_VTOPHYS(object);
2500: #else
2501: *pap = POOL_PADDR_INVALID;
2502: #endif
1.102 chs 2503: }
1.43 thorpej 2504:
1.125 ad 2505: FREECHECK_OUT(&pc->pc_freecheck, object);
1.162 ad 2506: return false;
1.43 thorpej 2507: }
2508:
2509: /*
1.134 ad 2510: * pool_cache_get{,_paddr}:
1.43 thorpej 2511: *
1.134 ad 2512: * Get an object from a pool cache (optionally returning
2513: * the physical address of the object).
1.43 thorpej 2514: */
1.134 ad 2515: void *
2516: pool_cache_get_paddr(pool_cache_t pc, int flags, paddr_t *pap)
1.43 thorpej 2517: {
1.134 ad 2518: pool_cache_cpu_t *cc;
2519: pcg_t *pcg;
2520: void *object;
1.60 thorpej 2521: int s;
1.43 thorpej 2522:
1.134 ad 2523: #ifdef LOCKDEBUG
1.155 ad 2524: if (flags & PR_WAITOK) {
1.154 yamt 2525: ASSERT_SLEEPABLE();
1.155 ad 2526: }
1.134 ad 2527: #endif
1.125 ad 2528:
1.162 ad 2529: /* Lock out interrupts and disable preemption. */
2530: s = splvm();
1.165 yamt 2531: while (/* CONSTCOND */ true) {
1.134 ad 2532: /* Try and allocate an object from the current group. */
1.162 ad 2533: cc = pc->pc_cpus[curcpu()->ci_index];
2534: KASSERT(cc->cc_cache == pc);
1.134 ad 2535: pcg = cc->cc_current;
1.162 ad 2536: if (__predict_true(pcg->pcg_avail > 0)) {
1.134 ad 2537: object = pcg->pcg_objects[--pcg->pcg_avail].pcgo_va;
1.162 ad 2538: if (__predict_false(pap != NULL))
1.134 ad 2539: *pap = pcg->pcg_objects[pcg->pcg_avail].pcgo_pa;
1.148 yamt 2540: #if defined(DIAGNOSTIC)
1.134 ad 2541: pcg->pcg_objects[pcg->pcg_avail].pcgo_va = NULL;
1.163 ad 2542: KASSERT(pcg->pcg_avail < pcg->pcg_size);
1.134 ad 2543: KASSERT(object != NULL);
1.163 ad 2544: #endif
1.134 ad 2545: cc->cc_hits++;
1.162 ad 2546: splx(s);
1.134 ad 2547: FREECHECK_OUT(&pc->pc_freecheck, object);
2548: return object;
1.43 thorpej 2549: }
2550:
2551: /*
1.134 ad 2552: * That failed. If the previous group isn't empty, swap
2553: * it with the current group and allocate from there.
1.43 thorpej 2554: */
1.134 ad 2555: pcg = cc->cc_previous;
1.162 ad 2556: if (__predict_true(pcg->pcg_avail > 0)) {
1.134 ad 2557: cc->cc_previous = cc->cc_current;
2558: cc->cc_current = pcg;
2559: continue;
1.43 thorpej 2560: }
2561:
1.134 ad 2562: /*
2563: * Can't allocate from either group: try the slow path.
2564: * If get_slow() allocated an object for us, or if
1.162 ad 2565: * no more objects are available, it will return false.
1.134 ad 2566: * Otherwise, we need to retry.
2567: */
1.165 yamt 2568: if (!pool_cache_get_slow(cc, s, &object, pap, flags))
2569: break;
2570: }
1.43 thorpej 2571:
1.134 ad 2572: return object;
1.51 thorpej 2573: }
2574:
1.162 ad 2575: static bool __noinline
2576: pool_cache_put_slow(pool_cache_cpu_t *cc, int s, void *object)
1.51 thorpej 2577: {
1.163 ad 2578: pcg_t *pcg, *cur;
1.134 ad 2579: uint64_t ncsw;
2580: pool_cache_t pc;
1.51 thorpej 2581:
1.168 yamt 2582: KASSERT(cc->cc_current->pcg_avail == cc->cc_current->pcg_size);
2583: KASSERT(cc->cc_previous->pcg_avail == cc->cc_previous->pcg_size);
2584:
1.134 ad 2585: pc = cc->cc_cache;
1.171 ad 2586: pcg = NULL;
1.134 ad 2587: cc->cc_misses++;
1.43 thorpej 2588:
1.171 ad 2589: /*
2590: * If there are no empty groups in the cache then allocate one
2591: * while still unlocked.
2592: */
2593: if (__predict_false(pc->pc_emptygroups == NULL)) {
2594: if (__predict_true(!pool_cache_disable)) {
2595: pcg = pool_get(pc->pc_pcgpool, PR_NOWAIT);
2596: }
2597: if (__predict_true(pcg != NULL)) {
2598: pcg->pcg_avail = 0;
2599: pcg->pcg_size = pc->pc_pcgsize;
2600: }
2601: }
2602:
1.162 ad 2603: /* Lock the cache. */
2604: if (__predict_false(!mutex_tryenter(&pc->pc_lock))) {
1.164 ad 2605: ncsw = curlwp->l_ncsw;
1.134 ad 2606: mutex_enter(&pc->pc_lock);
2607: pc->pc_contended++;
1.162 ad 2608:
1.163 ad 2609: /*
2610: * If we context switched while locking, then our view of
2611: * the per-CPU data is invalid: retry.
2612: */
2613: if (__predict_false(curlwp->l_ncsw != ncsw)) {
2614: mutex_exit(&pc->pc_lock);
1.171 ad 2615: if (pcg != NULL) {
2616: pool_put(pc->pc_pcgpool, pcg);
2617: }
1.163 ad 2618: return true;
2619: }
1.162 ad 2620: }
1.102 chs 2621:
1.163 ad 2622: /* If there are no empty groups in the cache then allocate one. */
1.171 ad 2623: if (pcg == NULL && pc->pc_emptygroups != NULL) {
2624: pcg = pc->pc_emptygroups;
1.163 ad 2625: pc->pc_emptygroups = pcg->pcg_next;
2626: pc->pc_nempty--;
1.134 ad 2627: }
1.130 ad 2628:
1.162 ad 2629: /*
2630: * If there's a empty group, release our full group back
2631: * to the cache. Install the empty group to the local CPU
2632: * and return.
2633: */
1.163 ad 2634: if (pcg != NULL) {
1.134 ad 2635: KASSERT(pcg->pcg_avail == 0);
1.162 ad 2636: if (__predict_false(cc->cc_previous == &pcg_dummy)) {
1.146 ad 2637: cc->cc_previous = pcg;
2638: } else {
1.162 ad 2639: cur = cc->cc_current;
2640: if (__predict_true(cur != &pcg_dummy)) {
1.163 ad 2641: KASSERT(cur->pcg_avail == cur->pcg_size);
1.146 ad 2642: cur->pcg_next = pc->pc_fullgroups;
2643: pc->pc_fullgroups = cur;
2644: pc->pc_nfull++;
2645: }
2646: cc->cc_current = pcg;
2647: }
1.163 ad 2648: pc->pc_hits++;
1.134 ad 2649: mutex_exit(&pc->pc_lock);
1.162 ad 2650: return true;
1.102 chs 2651: }
1.105 christos 2652:
1.134 ad 2653: /*
1.162 ad 2654: * Nothing available locally or in cache, and we didn't
2655: * allocate an empty group. Take the slow path and destroy
2656: * the object here and now.
1.134 ad 2657: */
2658: pc->pc_misses++;
2659: mutex_exit(&pc->pc_lock);
1.162 ad 2660: splx(s);
2661: pool_cache_destruct_object(pc, object);
1.105 christos 2662:
1.162 ad 2663: return false;
1.134 ad 2664: }
1.102 chs 2665:
1.43 thorpej 2666: /*
1.134 ad 2667: * pool_cache_put{,_paddr}:
1.43 thorpej 2668: *
1.134 ad 2669: * Put an object back to the pool cache (optionally caching the
2670: * physical address of the object).
1.43 thorpej 2671: */
1.101 thorpej 2672: void
1.134 ad 2673: pool_cache_put_paddr(pool_cache_t pc, void *object, paddr_t pa)
1.43 thorpej 2674: {
1.134 ad 2675: pool_cache_cpu_t *cc;
2676: pcg_t *pcg;
2677: int s;
1.101 thorpej 2678:
1.172 yamt 2679: KASSERT(object != NULL);
1.134 ad 2680: FREECHECK_IN(&pc->pc_freecheck, object);
1.101 thorpej 2681:
1.162 ad 2682: /* Lock out interrupts and disable preemption. */
2683: s = splvm();
1.165 yamt 2684: while (/* CONSTCOND */ true) {
1.134 ad 2685: /* If the current group isn't full, release it there. */
1.162 ad 2686: cc = pc->pc_cpus[curcpu()->ci_index];
2687: KASSERT(cc->cc_cache == pc);
1.134 ad 2688: pcg = cc->cc_current;
1.162 ad 2689: if (__predict_true(pcg->pcg_avail < pcg->pcg_size)) {
1.134 ad 2690: pcg->pcg_objects[pcg->pcg_avail].pcgo_va = object;
2691: pcg->pcg_objects[pcg->pcg_avail].pcgo_pa = pa;
2692: pcg->pcg_avail++;
2693: cc->cc_hits++;
1.162 ad 2694: splx(s);
1.134 ad 2695: return;
2696: }
1.43 thorpej 2697:
1.134 ad 2698: /*
1.162 ad 2699: * That failed. If the previous group isn't full, swap
1.134 ad 2700: * it with the current group and try again.
2701: */
2702: pcg = cc->cc_previous;
1.162 ad 2703: if (__predict_true(pcg->pcg_avail < pcg->pcg_size)) {
1.134 ad 2704: cc->cc_previous = cc->cc_current;
2705: cc->cc_current = pcg;
2706: continue;
2707: }
1.43 thorpej 2708:
1.134 ad 2709: /*
2710: * Can't free to either group: try the slow path.
2711: * If put_slow() releases the object for us, it
1.162 ad 2712: * will return false. Otherwise we need to retry.
1.134 ad 2713: */
1.165 yamt 2714: if (!pool_cache_put_slow(cc, s, object))
2715: break;
2716: }
1.43 thorpej 2717: }
2718:
2719: /*
1.134 ad 2720: * pool_cache_xcall:
1.43 thorpej 2721: *
1.134 ad 2722: * Transfer objects from the per-CPU cache to the global cache.
2723: * Run within a cross-call thread.
1.43 thorpej 2724: */
2725: static void
1.134 ad 2726: pool_cache_xcall(pool_cache_t pc)
1.43 thorpej 2727: {
1.134 ad 2728: pool_cache_cpu_t *cc;
2729: pcg_t *prev, *cur, **list;
1.162 ad 2730: int s;
1.134 ad 2731:
1.162 ad 2732: s = splvm();
2733: mutex_enter(&pc->pc_lock);
2734: cc = pc->pc_cpus[curcpu()->ci_index];
1.134 ad 2735: cur = cc->cc_current;
1.169 yamt 2736: cc->cc_current = __UNCONST(&pcg_dummy);
1.134 ad 2737: prev = cc->cc_previous;
1.169 yamt 2738: cc->cc_previous = __UNCONST(&pcg_dummy);
1.162 ad 2739: if (cur != &pcg_dummy) {
1.142 ad 2740: if (cur->pcg_avail == cur->pcg_size) {
1.134 ad 2741: list = &pc->pc_fullgroups;
2742: pc->pc_nfull++;
2743: } else if (cur->pcg_avail == 0) {
2744: list = &pc->pc_emptygroups;
2745: pc->pc_nempty++;
2746: } else {
2747: list = &pc->pc_partgroups;
2748: pc->pc_npart++;
2749: }
2750: cur->pcg_next = *list;
2751: *list = cur;
2752: }
1.162 ad 2753: if (prev != &pcg_dummy) {
1.142 ad 2754: if (prev->pcg_avail == prev->pcg_size) {
1.134 ad 2755: list = &pc->pc_fullgroups;
2756: pc->pc_nfull++;
2757: } else if (prev->pcg_avail == 0) {
2758: list = &pc->pc_emptygroups;
2759: pc->pc_nempty++;
2760: } else {
2761: list = &pc->pc_partgroups;
2762: pc->pc_npart++;
2763: }
2764: prev->pcg_next = *list;
2765: *list = prev;
2766: }
2767: mutex_exit(&pc->pc_lock);
2768: splx(s);
1.3 pk 2769: }
1.66 thorpej 2770:
2771: /*
2772: * Pool backend allocators.
2773: *
2774: * Each pool has a backend allocator that handles allocation, deallocation,
2775: * and any additional draining that might be needed.
2776: *
2777: * We provide two standard allocators:
2778: *
2779: * pool_allocator_kmem - the default when no allocator is specified
2780: *
2781: * pool_allocator_nointr - used for pools that will not be accessed
2782: * in interrupt context.
2783: */
2784: void *pool_page_alloc(struct pool *, int);
2785: void pool_page_free(struct pool *, void *);
2786:
1.112 bjh21 2787: #ifdef POOL_SUBPAGE
2788: struct pool_allocator pool_allocator_kmem_fullpage = {
2789: pool_page_alloc, pool_page_free, 0,
1.117 yamt 2790: .pa_backingmapptr = &kmem_map,
1.112 bjh21 2791: };
2792: #else
1.66 thorpej 2793: struct pool_allocator pool_allocator_kmem = {
2794: pool_page_alloc, pool_page_free, 0,
1.117 yamt 2795: .pa_backingmapptr = &kmem_map,
1.66 thorpej 2796: };
1.112 bjh21 2797: #endif
1.66 thorpej 2798:
2799: void *pool_page_alloc_nointr(struct pool *, int);
2800: void pool_page_free_nointr(struct pool *, void *);
2801:
1.112 bjh21 2802: #ifdef POOL_SUBPAGE
2803: struct pool_allocator pool_allocator_nointr_fullpage = {
2804: pool_page_alloc_nointr, pool_page_free_nointr, 0,
1.117 yamt 2805: .pa_backingmapptr = &kernel_map,
1.112 bjh21 2806: };
2807: #else
1.66 thorpej 2808: struct pool_allocator pool_allocator_nointr = {
2809: pool_page_alloc_nointr, pool_page_free_nointr, 0,
1.117 yamt 2810: .pa_backingmapptr = &kernel_map,
1.66 thorpej 2811: };
1.112 bjh21 2812: #endif
1.66 thorpej 2813:
2814: #ifdef POOL_SUBPAGE
2815: void *pool_subpage_alloc(struct pool *, int);
2816: void pool_subpage_free(struct pool *, void *);
2817:
1.112 bjh21 2818: struct pool_allocator pool_allocator_kmem = {
2819: pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
1.117 yamt 2820: .pa_backingmapptr = &kmem_map,
1.112 bjh21 2821: };
2822:
2823: void *pool_subpage_alloc_nointr(struct pool *, int);
2824: void pool_subpage_free_nointr(struct pool *, void *);
2825:
2826: struct pool_allocator pool_allocator_nointr = {
2827: pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
1.117 yamt 2828: .pa_backingmapptr = &kmem_map,
1.66 thorpej 2829: };
2830: #endif /* POOL_SUBPAGE */
2831:
1.117 yamt 2832: static void *
2833: pool_allocator_alloc(struct pool *pp, int flags)
1.66 thorpej 2834: {
1.117 yamt 2835: struct pool_allocator *pa = pp->pr_alloc;
1.66 thorpej 2836: void *res;
2837:
1.117 yamt 2838: res = (*pa->pa_alloc)(pp, flags);
2839: if (res == NULL && (flags & PR_WAITOK) == 0) {
1.66 thorpej 2840: /*
1.117 yamt 2841: * We only run the drain hook here if PR_NOWAIT.
2842: * In other cases, the hook will be run in
2843: * pool_reclaim().
1.66 thorpej 2844: */
1.117 yamt 2845: if (pp->pr_drain_hook != NULL) {
2846: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
2847: res = (*pa->pa_alloc)(pp, flags);
1.66 thorpej 2848: }
1.117 yamt 2849: }
2850: return res;
1.66 thorpej 2851: }
2852:
1.117 yamt 2853: static void
1.66 thorpej 2854: pool_allocator_free(struct pool *pp, void *v)
2855: {
2856: struct pool_allocator *pa = pp->pr_alloc;
2857:
2858: (*pa->pa_free)(pp, v);
2859: }
2860:
2861: void *
1.124 yamt 2862: pool_page_alloc(struct pool *pp, int flags)
1.66 thorpej 2863: {
1.127 thorpej 2864: bool waitok = (flags & PR_WAITOK) ? true : false;
1.66 thorpej 2865:
1.100 yamt 2866: return ((void *) uvm_km_alloc_poolpage_cache(kmem_map, waitok));
1.66 thorpej 2867: }
2868:
2869: void
1.124 yamt 2870: pool_page_free(struct pool *pp, void *v)
1.66 thorpej 2871: {
2872:
1.98 yamt 2873: uvm_km_free_poolpage_cache(kmem_map, (vaddr_t) v);
2874: }
2875:
2876: static void *
1.124 yamt 2877: pool_page_alloc_meta(struct pool *pp, int flags)
1.98 yamt 2878: {
1.127 thorpej 2879: bool waitok = (flags & PR_WAITOK) ? true : false;
1.98 yamt 2880:
1.100 yamt 2881: return ((void *) uvm_km_alloc_poolpage(kmem_map, waitok));
1.98 yamt 2882: }
2883:
2884: static void
1.124 yamt 2885: pool_page_free_meta(struct pool *pp, void *v)
1.98 yamt 2886: {
2887:
1.100 yamt 2888: uvm_km_free_poolpage(kmem_map, (vaddr_t) v);
1.66 thorpej 2889: }
2890:
2891: #ifdef POOL_SUBPAGE
2892: /* Sub-page allocator, for machines with large hardware pages. */
2893: void *
2894: pool_subpage_alloc(struct pool *pp, int flags)
2895: {
1.134 ad 2896: return pool_get(&psppool, flags);
1.66 thorpej 2897: }
2898:
2899: void
2900: pool_subpage_free(struct pool *pp, void *v)
2901: {
2902: pool_put(&psppool, v);
2903: }
2904:
2905: /* We don't provide a real nointr allocator. Maybe later. */
2906: void *
1.112 bjh21 2907: pool_subpage_alloc_nointr(struct pool *pp, int flags)
1.66 thorpej 2908: {
2909:
2910: return (pool_subpage_alloc(pp, flags));
2911: }
2912:
2913: void
1.112 bjh21 2914: pool_subpage_free_nointr(struct pool *pp, void *v)
1.66 thorpej 2915: {
2916:
2917: pool_subpage_free(pp, v);
2918: }
1.112 bjh21 2919: #endif /* POOL_SUBPAGE */
1.66 thorpej 2920: void *
1.124 yamt 2921: pool_page_alloc_nointr(struct pool *pp, int flags)
1.66 thorpej 2922: {
1.127 thorpej 2923: bool waitok = (flags & PR_WAITOK) ? true : false;
1.66 thorpej 2924:
1.100 yamt 2925: return ((void *) uvm_km_alloc_poolpage_cache(kernel_map, waitok));
1.66 thorpej 2926: }
2927:
2928: void
1.124 yamt 2929: pool_page_free_nointr(struct pool *pp, void *v)
1.66 thorpej 2930: {
2931:
1.98 yamt 2932: uvm_km_free_poolpage_cache(kernel_map, (vaddr_t) v);
1.66 thorpej 2933: }
1.141 yamt 2934:
2935: #if defined(DDB)
2936: static bool
2937: pool_in_page(struct pool *pp, struct pool_item_header *ph, uintptr_t addr)
2938: {
2939:
2940: return (uintptr_t)ph->ph_page <= addr &&
2941: addr < (uintptr_t)ph->ph_page + pp->pr_alloc->pa_pagesz;
2942: }
2943:
1.143 yamt 2944: static bool
2945: pool_in_item(struct pool *pp, void *item, uintptr_t addr)
2946: {
2947:
2948: return (uintptr_t)item <= addr && addr < (uintptr_t)item + pp->pr_size;
2949: }
2950:
2951: static bool
2952: pool_in_cg(struct pool *pp, struct pool_cache_group *pcg, uintptr_t addr)
2953: {
2954: int i;
2955:
2956: if (pcg == NULL) {
2957: return false;
2958: }
1.144 yamt 2959: for (i = 0; i < pcg->pcg_avail; i++) {
1.143 yamt 2960: if (pool_in_item(pp, pcg->pcg_objects[i].pcgo_va, addr)) {
2961: return true;
2962: }
2963: }
2964: return false;
2965: }
2966:
2967: static bool
2968: pool_allocated(struct pool *pp, struct pool_item_header *ph, uintptr_t addr)
2969: {
2970:
2971: if ((pp->pr_roflags & PR_NOTOUCH) != 0) {
2972: unsigned int idx = pr_item_notouch_index(pp, ph, (void *)addr);
2973: pool_item_bitmap_t *bitmap =
2974: ph->ph_bitmap + (idx / BITMAP_SIZE);
2975: pool_item_bitmap_t mask = 1 << (idx & BITMAP_MASK);
2976:
2977: return (*bitmap & mask) == 0;
2978: } else {
2979: struct pool_item *pi;
2980:
2981: LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
2982: if (pool_in_item(pp, pi, addr)) {
2983: return false;
2984: }
2985: }
2986: return true;
2987: }
2988: }
2989:
1.141 yamt 2990: void
2991: pool_whatis(uintptr_t addr, void (*pr)(const char *, ...))
2992: {
2993: struct pool *pp;
2994:
1.145 ad 2995: TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
1.141 yamt 2996: struct pool_item_header *ph;
2997: uintptr_t item;
1.143 yamt 2998: bool allocated = true;
2999: bool incache = false;
3000: bool incpucache = false;
3001: char cpucachestr[32];
1.141 yamt 3002:
3003: if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
3004: LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
3005: if (pool_in_page(pp, ph, addr)) {
3006: goto found;
3007: }
3008: }
3009: LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
3010: if (pool_in_page(pp, ph, addr)) {
1.143 yamt 3011: allocated =
3012: pool_allocated(pp, ph, addr);
3013: goto found;
3014: }
3015: }
3016: LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
3017: if (pool_in_page(pp, ph, addr)) {
3018: allocated = false;
1.141 yamt 3019: goto found;
3020: }
3021: }
3022: continue;
3023: } else {
3024: ph = pr_find_pagehead_noalign(pp, (void *)addr);
3025: if (ph == NULL || !pool_in_page(pp, ph, addr)) {
3026: continue;
3027: }
1.143 yamt 3028: allocated = pool_allocated(pp, ph, addr);
1.141 yamt 3029: }
3030: found:
1.143 yamt 3031: if (allocated && pp->pr_cache) {
3032: pool_cache_t pc = pp->pr_cache;
3033: struct pool_cache_group *pcg;
3034: int i;
3035:
3036: for (pcg = pc->pc_fullgroups; pcg != NULL;
3037: pcg = pcg->pcg_next) {
3038: if (pool_in_cg(pp, pcg, addr)) {
3039: incache = true;
3040: goto print;
3041: }
3042: }
1.183 ! ad 3043: for (i = 0; i < __arraycount(pc->pc_cpus); i++) {
1.143 yamt 3044: pool_cache_cpu_t *cc;
3045:
3046: if ((cc = pc->pc_cpus[i]) == NULL) {
3047: continue;
3048: }
3049: if (pool_in_cg(pp, cc->cc_current, addr) ||
3050: pool_in_cg(pp, cc->cc_previous, addr)) {
3051: struct cpu_info *ci =
1.170 ad 3052: cpu_lookup(i);
1.143 yamt 3053:
3054: incpucache = true;
3055: snprintf(cpucachestr,
3056: sizeof(cpucachestr),
3057: "cached by CPU %u",
1.153 martin 3058: ci->ci_index);
1.143 yamt 3059: goto print;
3060: }
3061: }
3062: }
3063: print:
1.141 yamt 3064: item = (uintptr_t)ph->ph_page + ph->ph_off;
3065: item = item + rounddown(addr - item, pp->pr_size);
1.143 yamt 3066: (*pr)("%p is %p+%zu in POOL '%s' (%s)\n",
1.141 yamt 3067: (void *)addr, item, (size_t)(addr - item),
1.143 yamt 3068: pp->pr_wchan,
3069: incpucache ? cpucachestr :
3070: incache ? "cached" : allocated ? "allocated" : "free");
1.141 yamt 3071: }
3072: }
3073: #endif /* defined(DDB) */
CVSweb <webmaster@jp.NetBSD.org>