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