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