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