Annotation of src/sys/kern/subr_pool.c, Revision 1.101.2.8
1.101.2.8! yamt 1: /* $NetBSD: subr_pool.c,v 1.101.2.7 2007/12/07 17:33:07 yamt Exp $ */
1.1 pk 2:
3: /*-
1.101.2.6 yamt 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.101.2.6 yamt 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.101.2.8! yamt 41: __KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.101.2.7 2007/12/07 17:33:07 yamt Exp $");
1.24 scottr 42:
1.101.2.8! 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.101.2.6 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.101.2.3 yamt 57: #include <sys/debug.h>
1.101.2.6 yamt 58: #include <sys/lockdebug.h>
59: #include <sys/xcall.h>
60: #include <sys/cpu.h>
1.101.2.8! yamt 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.101.2.8! yamt 79: TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
1.101.2.6 yamt 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.101.2.6 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.101.2.1 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.101.2.6 yamt 99: struct pool_allocator pool_allocator_meta = {
1.101.2.1 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.101.2.6 yamt 110: /* This lock protects both pool_head and drainpp. */
111: static kmutex_t pool_head_lock;
112: static kcondvar_t pool_busy;
113:
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.101.2.4 yamt 124: void * ph_page; /* this page's address */
1.3 pk 125: struct timeval ph_time; /* last referenced */
1.101.2.6 yamt 126: uint16_t ph_nmissing; /* # of chunks in use */
1.101.2.8! yamt 127: uint16_t ph_off; /* start offset in page */
1.97 yamt 128: union {
129: /* !PR_NOTOUCH */
130: struct {
1.101.2.1 yamt 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.101.2.8! 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.101.2.6 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.101.2.6 yamt 147: #define PI_MAGIC 0xdeaddeadU
1.3 pk 148: /* Other entries use only this list entry */
1.101.2.1 yamt 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.101.2.6 yamt 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.101.2.8! yamt 180: static struct pool pcg_normal_pool;
! 181: static struct pool pcg_large_pool;
1.101.2.6 yamt 182: static struct pool cache_pool;
183: static struct pool cache_cpu_pool;
1.3 pk 184:
1.101.2.8! yamt 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.101.2.6 yamt 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.101.2.4 yamt 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.101.2.1 yamt 205: static int pool_grow(struct pool *, int);
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.101.2.1 yamt 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.101.2.1 yamt 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.101.2.1 yamt 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.101.2.1 yamt 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.101.2.6 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.101.2.6 yamt 340: unsigned int idx;
1.97 yamt 341:
342: KASSERT(pp->pr_roflags & PR_NOTOUCH);
1.101.2.4 yamt 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.101.2.1 yamt 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.101.2.6 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.101.2.6 yamt 356: KASSERT((*bitmap & mask) == 0);
357: *bitmap |= mask;
1.97 yamt 358: }
359:
1.101.2.1 yamt 360: static inline void *
1.97 yamt 361: pr_item_notouch_get(const struct pool *pp, struct pool_item_header *ph)
362: {
1.101.2.6 yamt 363: pool_item_bitmap_t *bitmap = ph->ph_bitmap;
364: unsigned int idx;
365: int i;
366:
367: for (i = 0; ; i++) {
368: int bit;
369:
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);
384: return (char *)ph->ph_page + ph->ph_off + idx * pp->pr_size;
385: }
1.97 yamt 386:
1.101.2.6 yamt 387: static inline void
388: pr_item_notouch_init(const struct pool *pp, struct pool_item_header *ph)
389: {
390: pool_item_bitmap_t *bitmap = ph->ph_bitmap;
391: const int n = howmany(pp->pr_itemsperpage, BITMAP_SIZE);
392: int i;
1.97 yamt 393:
1.101.2.6 yamt 394: for (i = 0; i < n; i++) {
395: bitmap[i] = (pool_item_bitmap_t)-1;
396: }
1.97 yamt 397: }
398:
1.101.2.1 yamt 399: static inline int
1.88 chs 400: phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
401: {
1.101.2.2 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)
409: return (1);
1.101.2.2 yamt 410: else if (a->ph_page > b->ph_page)
411: return (-1);
1.88 chs 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.101.2.8! 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.101.2.2 yamt 438: * Return the pool page header based on item address.
1.3 pk 439: */
1.101.2.1 yamt 440: static inline struct pool_item_header *
1.101.2.2 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.101.2.2 yamt 445: if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1.101.2.8! yamt 446: ph = pr_find_pagehead_noalign(pp, v);
1.101.2.2 yamt 447: } else {
1.101.2.4 yamt 448: void *page =
449: (void *)((uintptr_t)v & pp->pr_alloc->pa_pagemask);
1.101.2.2 yamt 450:
451: if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
1.101.2.4 yamt 452: ph = (struct pool_item_header *)((char *)page + pp->pr_phoffset);
1.101.2.2 yamt 453: } else {
454: tmp.ph_page = page;
455: ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
456: }
457: }
1.3 pk 458:
1.101.2.2 yamt 459: KASSERT(ph == NULL || ((pp->pr_roflags & PR_PHINPAGE) != 0) ||
1.101.2.4 yamt 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.101.2.6 yamt 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.101.2.1 yamt 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.101.2.6 yamt 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.101.2.3 yamt 517: static bool
1.101.2.1 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.101.2.3 yamt 524: return false;
1.101.2.1 yamt 525: }
526:
527: static int
528: pool_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
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.101.2.1 yamt 594: pool_subsystem_init(void)
1.94 simonb 595: {
1.101.2.1 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.101.2.6 yamt 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.101.2.4 yamt 606: (*pi)->palloc, (*pi)->ipl);
1.101.2.1 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.101.2.6 yamt 614:
615: pool_init(&cache_pool, sizeof(struct pool_cache), CACHE_LINE_SIZE,
616: 0, 0, "pcache", &pool_allocator_nointr, IPL_NONE);
617:
618: pool_init(&cache_cpu_pool, sizeof(pool_cache_cpu_t), CACHE_LINE_SIZE,
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.101.2.4 yamt 630: const char *wchan, struct pool_allocator *palloc, int ipl)
1.3 pk 631: {
1.101.2.1 yamt 632: struct pool *pp1;
1.92 enami 633: size_t trysize, phsize;
1.101.2.6 yamt 634: int off, slack;
1.99 yamt 635:
1.101.2.1 yamt 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.101.2.8! yamt 641: TAILQ_FOREACH(pp1, &pool_head, pr_poollist) {
1.101.2.1 yamt 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.101.2.1 yamt 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.101.2.1 yamt 667: if (palloc->pa_pagesz == 0)
1.66 thorpej 668: palloc->pa_pagesz = PAGE_SIZE;
669:
670: TAILQ_INIT(&palloc->pa_list);
671:
1.101.2.6 yamt 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.101.2.1 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.101.2.2 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.101.2.2 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.101.2.6 yamt 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.101.2.3 yamt 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.101.2.2 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.101.2.6 yamt 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.101.2.7 yamt 817: /*
818: * XXXAD hack to prevent IP input processing from blocking.
819: */
820: if (ipl == IPL_SOFTNET) {
821: mutex_init(&pp->pr_lock, MUTEX_DEFAULT, IPL_VM);
822: } else {
823: mutex_init(&pp->pr_lock, MUTEX_DEFAULT, ipl);
824: }
1.101.2.6 yamt 825: cv_init(&pp->pr_cv, wchan);
826: pp->pr_ipl = ipl;
1.1 pk 827:
1.3 pk 828: /*
1.43 thorpej 829: * Initialize private page header pool and cache magazine pool if we
830: * haven't done so yet.
1.23 thorpej 831: * XXX LOCKING.
1.3 pk 832: */
1.97 yamt 833: if (phpool[0].pr_size == 0) {
834: int idx;
835: for (idx = 0; idx < PHPOOL_MAX; idx++) {
836: static char phpool_names[PHPOOL_MAX][6+1+6+1];
837: int nelem;
838: size_t sz;
839:
840: nelem = PHPOOL_FREELIST_NELEM(idx);
841: snprintf(phpool_names[idx], sizeof(phpool_names[idx]),
842: "phpool-%d", nelem);
843: sz = sizeof(struct pool_item_header);
844: if (nelem) {
1.101.2.6 yamt 845: sz = offsetof(struct pool_item_header,
846: ph_bitmap[howmany(nelem, BITMAP_SIZE)]);
1.97 yamt 847: }
848: pool_init(&phpool[idx], sz, 0, 0, 0,
1.101.2.4 yamt 849: phpool_names[idx], &pool_allocator_meta, IPL_VM);
1.97 yamt 850: }
1.62 bjh21 851: #ifdef POOL_SUBPAGE
852: pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0,
1.101.2.4 yamt 853: PR_RECURSIVE, "psppool", &pool_allocator_meta, IPL_VM);
1.62 bjh21 854: #endif
1.101.2.8! yamt 855:
! 856: size = sizeof(pcg_t) +
! 857: (PCG_NOBJECTS_NORMAL - 1) * sizeof(pcgpair_t);
! 858: pool_init(&pcg_normal_pool, size, CACHE_LINE_SIZE, 0, 0,
! 859: "pcgnormal", &pool_allocator_meta, IPL_VM);
! 860:
! 861: size = sizeof(pcg_t) +
! 862: (PCG_NOBJECTS_LARGE - 1) * sizeof(pcgpair_t);
! 863: pool_init(&pcg_large_pool, size, CACHE_LINE_SIZE, 0, 0,
! 864: "pcglarge", &pool_allocator_meta, IPL_VM);
1.1 pk 865: }
866:
1.101.2.8! yamt 867: /* Insert into the list of all pools. */
! 868: if (__predict_true(!cold))
1.101.2.6 yamt 869: mutex_enter(&pool_head_lock);
1.101.2.8! yamt 870: TAILQ_FOREACH(pp1, &pool_head, pr_poollist) {
! 871: if (strcmp(pp1->pr_wchan, pp->pr_wchan) > 0)
! 872: break;
! 873: }
! 874: if (pp1 == NULL)
! 875: TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
! 876: else
! 877: TAILQ_INSERT_BEFORE(pp1, pp, pr_poollist);
! 878: if (__predict_true(!cold))
1.101.2.6 yamt 879: mutex_exit(&pool_head_lock);
880:
881: /* Insert this into the list of pools using this allocator. */
1.101.2.8! yamt 882: if (__predict_true(!cold))
1.101.2.6 yamt 883: mutex_enter(&palloc->pa_lock);
1.101.2.8! yamt 884: TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
! 885: if (__predict_true(!cold))
1.101.2.6 yamt 886: mutex_exit(&palloc->pa_lock);
1.66 thorpej 887:
1.101.2.1 yamt 888: pool_reclaim_register(pp);
1.1 pk 889: }
890:
891: /*
892: * De-commision a pool resource.
893: */
894: void
1.42 thorpej 895: pool_destroy(struct pool *pp)
1.1 pk 896: {
1.101 thorpej 897: struct pool_pagelist pq;
1.3 pk 898: struct pool_item_header *ph;
1.43 thorpej 899:
1.101 thorpej 900: /* Remove from global pool list */
1.101.2.6 yamt 901: mutex_enter(&pool_head_lock);
902: while (pp->pr_refcnt != 0)
903: cv_wait(&pool_busy, &pool_head_lock);
1.101.2.8! yamt 904: TAILQ_REMOVE(&pool_head, pp, pr_poollist);
1.101 thorpej 905: if (drainpp == pp)
906: drainpp = NULL;
1.101.2.6 yamt 907: mutex_exit(&pool_head_lock);
1.101 thorpej 908:
909: /* Remove this pool from its allocator's list of pools. */
1.101.2.1 yamt 910: pool_reclaim_unregister(pp);
1.101.2.6 yamt 911: mutex_enter(&pp->pr_alloc->pa_lock);
1.66 thorpej 912: TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list);
1.101.2.6 yamt 913: mutex_exit(&pp->pr_alloc->pa_lock);
1.66 thorpej 914:
1.101.2.6 yamt 915: mutex_enter(&pp->pr_lock);
1.101 thorpej 916:
1.101.2.6 yamt 917: KASSERT(pp->pr_cache == NULL);
1.3 pk 918:
919: #ifdef DIAGNOSTIC
1.20 thorpej 920: if (pp->pr_nout != 0) {
1.25 thorpej 921: pr_printlog(pp, NULL, printf);
1.80 provos 922: panic("pool_destroy: pool busy: still out: %u",
1.20 thorpej 923: pp->pr_nout);
1.3 pk 924: }
925: #endif
1.1 pk 926:
1.101 thorpej 927: KASSERT(LIST_EMPTY(&pp->pr_fullpages));
928: KASSERT(LIST_EMPTY(&pp->pr_partpages));
929:
1.3 pk 930: /* Remove all pages */
1.101 thorpej 931: LIST_INIT(&pq);
1.88 chs 932: while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1.101 thorpej 933: pr_rmpage(pp, ph, &pq);
934:
1.101.2.6 yamt 935: mutex_exit(&pp->pr_lock);
1.3 pk 936:
1.101 thorpej 937: pr_pagelist_free(pp, &pq);
1.3 pk 938:
1.59 thorpej 939: #ifdef POOL_DIAGNOSTIC
1.20 thorpej 940: if ((pp->pr_roflags & PR_LOGGING) != 0)
1.3 pk 941: free(pp->pr_log, M_TEMP);
1.59 thorpej 942: #endif
1.101.2.6 yamt 943:
944: cv_destroy(&pp->pr_cv);
945: mutex_destroy(&pp->pr_lock);
1.1 pk 946: }
947:
1.68 thorpej 948: void
949: pool_set_drain_hook(struct pool *pp, void (*fn)(void *, int), void *arg)
950: {
951:
952: /* XXX no locking -- must be used just after pool_init() */
953: #ifdef DIAGNOSTIC
954: if (pp->pr_drain_hook != NULL)
955: panic("pool_set_drain_hook(%s): already set", pp->pr_wchan);
956: #endif
957: pp->pr_drain_hook = fn;
958: pp->pr_drain_hook_arg = arg;
959: }
960:
1.88 chs 961: static struct pool_item_header *
1.101.2.4 yamt 962: pool_alloc_item_header(struct pool *pp, void *storage, int flags)
1.55 thorpej 963: {
964: struct pool_item_header *ph;
965:
966: if ((pp->pr_roflags & PR_PHINPAGE) != 0)
1.101.2.4 yamt 967: ph = (struct pool_item_header *) ((char *)storage + pp->pr_phoffset);
1.101.2.6 yamt 968: else
1.97 yamt 969: ph = pool_get(pp->pr_phpool, flags);
1.55 thorpej 970:
971: return (ph);
972: }
1.1 pk 973:
974: /*
1.101.2.6 yamt 975: * Grab an item from the pool.
1.1 pk 976: */
1.3 pk 977: void *
1.59 thorpej 978: #ifdef POOL_DIAGNOSTIC
1.42 thorpej 979: _pool_get(struct pool *pp, int flags, const char *file, long line)
1.56 sommerfe 980: #else
981: pool_get(struct pool *pp, int flags)
982: #endif
1.1 pk 983: {
984: struct pool_item *pi;
1.3 pk 985: struct pool_item_header *ph;
1.55 thorpej 986: void *v;
1.1 pk 987:
1.2 pk 988: #ifdef DIAGNOSTIC
1.95 atatat 989: if (__predict_false(pp->pr_itemsperpage == 0))
990: panic("pool_get: pool %p: pr_itemsperpage is zero, "
991: "pool not initialized?", pp);
1.84 thorpej 992: if (__predict_false(curlwp == NULL && doing_shutdown == 0 &&
1.37 sommerfe 993: (flags & PR_WAITOK) != 0))
1.77 matt 994: panic("pool_get: %s: must have NOWAIT", pp->pr_wchan);
1.58 thorpej 995:
1.101.2.1 yamt 996: #endif /* DIAGNOSTIC */
1.58 thorpej 997: #ifdef LOCKDEBUG
998: if (flags & PR_WAITOK)
1.101.2.2 yamt 999: ASSERT_SLEEPABLE(NULL, "pool_get(PR_WAITOK)");
1.56 sommerfe 1000: #endif
1.1 pk 1001:
1.101.2.6 yamt 1002: mutex_enter(&pp->pr_lock);
1.25 thorpej 1003: pr_enter(pp, file, line);
1.20 thorpej 1004:
1005: startover:
1006: /*
1007: * Check to see if we've reached the hard limit. If we have,
1008: * and we can wait, then wait until an item has been returned to
1009: * the pool.
1010: */
1011: #ifdef DIAGNOSTIC
1.34 thorpej 1012: if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
1.25 thorpej 1013: pr_leave(pp);
1.101.2.6 yamt 1014: mutex_exit(&pp->pr_lock);
1.20 thorpej 1015: panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
1016: }
1017: #endif
1.34 thorpej 1018: if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
1.68 thorpej 1019: if (pp->pr_drain_hook != NULL) {
1020: /*
1021: * Since the drain hook is going to free things
1022: * back to the pool, unlock, call the hook, re-lock,
1023: * and check the hardlimit condition again.
1024: */
1025: pr_leave(pp);
1.101.2.6 yamt 1026: mutex_exit(&pp->pr_lock);
1.68 thorpej 1027: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
1.101.2.6 yamt 1028: mutex_enter(&pp->pr_lock);
1.68 thorpej 1029: pr_enter(pp, file, line);
1030: if (pp->pr_nout < pp->pr_hardlimit)
1031: goto startover;
1032: }
1033:
1.29 sommerfe 1034: if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
1.20 thorpej 1035: /*
1036: * XXX: A warning isn't logged in this case. Should
1037: * it be?
1038: */
1039: pp->pr_flags |= PR_WANTED;
1.25 thorpej 1040: pr_leave(pp);
1.101.2.6 yamt 1041: cv_wait(&pp->pr_cv, &pp->pr_lock);
1.25 thorpej 1042: pr_enter(pp, file, line);
1.20 thorpej 1043: goto startover;
1044: }
1.31 thorpej 1045:
1046: /*
1047: * Log a message that the hard limit has been hit.
1048: */
1049: if (pp->pr_hardlimit_warning != NULL &&
1050: ratecheck(&pp->pr_hardlimit_warning_last,
1051: &pp->pr_hardlimit_ratecap))
1052: log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1.21 thorpej 1053:
1054: pp->pr_nfail++;
1055:
1.25 thorpej 1056: pr_leave(pp);
1.101.2.6 yamt 1057: mutex_exit(&pp->pr_lock);
1.20 thorpej 1058: return (NULL);
1059: }
1060:
1.3 pk 1061: /*
1062: * The convention we use is that if `curpage' is not NULL, then
1063: * it points at a non-empty bucket. In particular, `curpage'
1064: * never points at a page header which has PR_PHINPAGE set and
1065: * has no items in its bucket.
1066: */
1.20 thorpej 1067: if ((ph = pp->pr_curpage) == NULL) {
1.101.2.1 yamt 1068: int error;
1069:
1.20 thorpej 1070: #ifdef DIAGNOSTIC
1071: if (pp->pr_nitems != 0) {
1.101.2.6 yamt 1072: mutex_exit(&pp->pr_lock);
1.20 thorpej 1073: printf("pool_get: %s: curpage NULL, nitems %u\n",
1074: pp->pr_wchan, pp->pr_nitems);
1.80 provos 1075: panic("pool_get: nitems inconsistent");
1.20 thorpej 1076: }
1077: #endif
1078:
1.21 thorpej 1079: /*
1080: * Call the back-end page allocator for more memory.
1081: * Release the pool lock, as the back-end page allocator
1082: * may block.
1083: */
1.25 thorpej 1084: pr_leave(pp);
1.101.2.1 yamt 1085: error = pool_grow(pp, flags);
1086: pr_enter(pp, file, line);
1087: if (error != 0) {
1.21 thorpej 1088: /*
1.55 thorpej 1089: * We were unable to allocate a page or item
1090: * header, but we released the lock during
1091: * allocation, so perhaps items were freed
1092: * back to the pool. Check for this case.
1.21 thorpej 1093: */
1094: if (pp->pr_curpage != NULL)
1095: goto startover;
1.15 pk 1096:
1.101.2.1 yamt 1097: pp->pr_nfail++;
1.25 thorpej 1098: pr_leave(pp);
1.101.2.6 yamt 1099: mutex_exit(&pp->pr_lock);
1.101.2.1 yamt 1100: return (NULL);
1.1 pk 1101: }
1.3 pk 1102:
1.20 thorpej 1103: /* Start the allocation process over. */
1104: goto startover;
1.3 pk 1105: }
1.97 yamt 1106: if (pp->pr_roflags & PR_NOTOUCH) {
1107: #ifdef DIAGNOSTIC
1108: if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) {
1109: pr_leave(pp);
1.101.2.6 yamt 1110: mutex_exit(&pp->pr_lock);
1.97 yamt 1111: panic("pool_get: %s: page empty", pp->pr_wchan);
1112: }
1113: #endif
1114: v = pr_item_notouch_get(pp, ph);
1115: #ifdef POOL_DIAGNOSTIC
1116: pr_log(pp, v, PRLOG_GET, file, line);
1117: #endif
1118: } else {
1.101.2.1 yamt 1119: v = pi = LIST_FIRST(&ph->ph_itemlist);
1.97 yamt 1120: if (__predict_false(v == NULL)) {
1121: pr_leave(pp);
1.101.2.6 yamt 1122: mutex_exit(&pp->pr_lock);
1.97 yamt 1123: panic("pool_get: %s: page empty", pp->pr_wchan);
1124: }
1.20 thorpej 1125: #ifdef DIAGNOSTIC
1.97 yamt 1126: if (__predict_false(pp->pr_nitems == 0)) {
1127: pr_leave(pp);
1.101.2.6 yamt 1128: mutex_exit(&pp->pr_lock);
1.97 yamt 1129: printf("pool_get: %s: items on itemlist, nitems %u\n",
1130: pp->pr_wchan, pp->pr_nitems);
1131: panic("pool_get: nitems inconsistent");
1132: }
1.65 enami 1133: #endif
1.56 sommerfe 1134:
1.65 enami 1135: #ifdef POOL_DIAGNOSTIC
1.97 yamt 1136: pr_log(pp, v, PRLOG_GET, file, line);
1.65 enami 1137: #endif
1.3 pk 1138:
1.65 enami 1139: #ifdef DIAGNOSTIC
1.97 yamt 1140: if (__predict_false(pi->pi_magic != PI_MAGIC)) {
1141: pr_printlog(pp, pi, printf);
1142: panic("pool_get(%s): free list modified: "
1143: "magic=%x; page %p; item addr %p\n",
1144: pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
1145: }
1.3 pk 1146: #endif
1147:
1.97 yamt 1148: /*
1149: * Remove from item list.
1150: */
1.101.2.1 yamt 1151: LIST_REMOVE(pi, pi_list);
1.97 yamt 1152: }
1.20 thorpej 1153: pp->pr_nitems--;
1154: pp->pr_nout++;
1.6 thorpej 1155: if (ph->ph_nmissing == 0) {
1156: #ifdef DIAGNOSTIC
1.34 thorpej 1157: if (__predict_false(pp->pr_nidle == 0))
1.6 thorpej 1158: panic("pool_get: nidle inconsistent");
1159: #endif
1160: pp->pr_nidle--;
1.88 chs 1161:
1162: /*
1163: * This page was previously empty. Move it to the list of
1164: * partially-full pages. This page is already curpage.
1165: */
1166: LIST_REMOVE(ph, ph_pagelist);
1167: LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.6 thorpej 1168: }
1.3 pk 1169: ph->ph_nmissing++;
1.97 yamt 1170: if (ph->ph_nmissing == pp->pr_itemsperpage) {
1.21 thorpej 1171: #ifdef DIAGNOSTIC
1.97 yamt 1172: if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 &&
1.101.2.1 yamt 1173: !LIST_EMPTY(&ph->ph_itemlist))) {
1.25 thorpej 1174: pr_leave(pp);
1.101.2.6 yamt 1175: mutex_exit(&pp->pr_lock);
1.21 thorpej 1176: panic("pool_get: %s: nmissing inconsistent",
1177: pp->pr_wchan);
1178: }
1179: #endif
1.3 pk 1180: /*
1.88 chs 1181: * This page is now full. Move it to the full list
1182: * and select a new current page.
1.3 pk 1183: */
1.88 chs 1184: LIST_REMOVE(ph, ph_pagelist);
1185: LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
1186: pool_update_curpage(pp);
1.1 pk 1187: }
1.3 pk 1188:
1189: pp->pr_nget++;
1.101.2.1 yamt 1190: pr_leave(pp);
1.20 thorpej 1191:
1192: /*
1193: * If we have a low water mark and we are now below that low
1194: * water mark, add more items to the pool.
1195: */
1.53 thorpej 1196: if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20 thorpej 1197: /*
1198: * XXX: Should we log a warning? Should we set up a timeout
1199: * to try again in a second or so? The latter could break
1200: * a caller's assumptions about interrupt protection, etc.
1201: */
1202: }
1203:
1.101.2.6 yamt 1204: mutex_exit(&pp->pr_lock);
1.101.2.3 yamt 1205: KASSERT((((vaddr_t)v + pp->pr_itemoffset) & (pp->pr_align - 1)) == 0);
1206: FREECHECK_OUT(&pp->pr_freecheck, v);
1.1 pk 1207: return (v);
1208: }
1209:
1210: /*
1.43 thorpej 1211: * Internal version of pool_put(). Pool is already locked/entered.
1.1 pk 1212: */
1.43 thorpej 1213: static void
1.101 thorpej 1214: pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq)
1.1 pk 1215: {
1216: struct pool_item *pi = v;
1.3 pk 1217: struct pool_item_header *ph;
1218:
1.101.2.6 yamt 1219: KASSERT(mutex_owned(&pp->pr_lock));
1.101.2.3 yamt 1220: FREECHECK_IN(&pp->pr_freecheck, v);
1.101.2.6 yamt 1221: LOCKDEBUG_MEM_CHECK(v, pp->pr_size);
1.61 chs 1222:
1.30 thorpej 1223: #ifdef DIAGNOSTIC
1.34 thorpej 1224: if (__predict_false(pp->pr_nout == 0)) {
1.30 thorpej 1225: printf("pool %s: putting with none out\n",
1226: pp->pr_wchan);
1227: panic("pool_put");
1228: }
1229: #endif
1.3 pk 1230:
1.101.2.2 yamt 1231: if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) {
1.25 thorpej 1232: pr_printlog(pp, NULL, printf);
1.3 pk 1233: panic("pool_put: %s: page header missing", pp->pr_wchan);
1234: }
1.28 thorpej 1235:
1.3 pk 1236: /*
1237: * Return to item list.
1238: */
1.97 yamt 1239: if (pp->pr_roflags & PR_NOTOUCH) {
1240: pr_item_notouch_put(pp, ph, v);
1241: } else {
1.2 pk 1242: #ifdef DIAGNOSTIC
1.97 yamt 1243: pi->pi_magic = PI_MAGIC;
1.3 pk 1244: #endif
1.32 chs 1245: #ifdef DEBUG
1.97 yamt 1246: {
1247: int i, *ip = v;
1.32 chs 1248:
1.97 yamt 1249: for (i = 0; i < pp->pr_size / sizeof(int); i++) {
1250: *ip++ = PI_MAGIC;
1251: }
1.32 chs 1252: }
1253: #endif
1254:
1.101.2.1 yamt 1255: LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.97 yamt 1256: }
1.79 thorpej 1257: KDASSERT(ph->ph_nmissing != 0);
1.3 pk 1258: ph->ph_nmissing--;
1259: pp->pr_nput++;
1.20 thorpej 1260: pp->pr_nitems++;
1261: pp->pr_nout--;
1.3 pk 1262:
1263: /* Cancel "pool empty" condition if it exists */
1264: if (pp->pr_curpage == NULL)
1265: pp->pr_curpage = ph;
1266:
1267: if (pp->pr_flags & PR_WANTED) {
1268: pp->pr_flags &= ~PR_WANTED;
1.15 pk 1269: if (ph->ph_nmissing == 0)
1270: pp->pr_nidle++;
1.101.2.6 yamt 1271: cv_broadcast(&pp->pr_cv);
1.3 pk 1272: return;
1273: }
1274:
1275: /*
1.88 chs 1276: * If this page is now empty, do one of two things:
1.21 thorpej 1277: *
1.88 chs 1278: * (1) If we have more pages than the page high water mark,
1.96 thorpej 1279: * free the page back to the system. ONLY CONSIDER
1.90 thorpej 1280: * FREEING BACK A PAGE IF WE HAVE MORE THAN OUR MINIMUM PAGE
1281: * CLAIM.
1.21 thorpej 1282: *
1.88 chs 1283: * (2) Otherwise, move the page to the empty page list.
1284: *
1285: * Either way, select a new current page (so we use a partially-full
1286: * page if one is available).
1.3 pk 1287: */
1288: if (ph->ph_nmissing == 0) {
1.6 thorpej 1289: pp->pr_nidle++;
1.90 thorpej 1290: if (pp->pr_npages > pp->pr_minpages &&
1291: (pp->pr_npages > pp->pr_maxpages ||
1.101.2.1 yamt 1292: pa_starved_p(pp->pr_alloc))) {
1.101 thorpej 1293: pr_rmpage(pp, ph, pq);
1.3 pk 1294: } else {
1.88 chs 1295: LIST_REMOVE(ph, ph_pagelist);
1296: LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.3 pk 1297:
1.21 thorpej 1298: /*
1299: * Update the timestamp on the page. A page must
1300: * be idle for some period of time before it can
1301: * be reclaimed by the pagedaemon. This minimizes
1302: * ping-pong'ing for memory.
1303: */
1.101.2.1 yamt 1304: getmicrotime(&ph->ph_time);
1.1 pk 1305: }
1.88 chs 1306: pool_update_curpage(pp);
1.1 pk 1307: }
1.88 chs 1308:
1.21 thorpej 1309: /*
1.88 chs 1310: * If the page was previously completely full, move it to the
1311: * partially-full list and make it the current page. The next
1312: * allocation will get the item from this page, instead of
1313: * further fragmenting the pool.
1.21 thorpej 1314: */
1315: else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
1.88 chs 1316: LIST_REMOVE(ph, ph_pagelist);
1317: LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.21 thorpej 1318: pp->pr_curpage = ph;
1319: }
1.43 thorpej 1320: }
1321:
1322: /*
1.101.2.6 yamt 1323: * Return resource to the pool.
1.43 thorpej 1324: */
1.59 thorpej 1325: #ifdef POOL_DIAGNOSTIC
1.43 thorpej 1326: void
1327: _pool_put(struct pool *pp, void *v, const char *file, long line)
1328: {
1.101 thorpej 1329: struct pool_pagelist pq;
1330:
1331: LIST_INIT(&pq);
1.43 thorpej 1332:
1.101.2.6 yamt 1333: mutex_enter(&pp->pr_lock);
1.43 thorpej 1334: pr_enter(pp, file, line);
1335:
1.56 sommerfe 1336: pr_log(pp, v, PRLOG_PUT, file, line);
1337:
1.101 thorpej 1338: pool_do_put(pp, v, &pq);
1.21 thorpej 1339:
1.25 thorpej 1340: pr_leave(pp);
1.101.2.6 yamt 1341: mutex_exit(&pp->pr_lock);
1.101 thorpej 1342:
1.101.2.1 yamt 1343: pr_pagelist_free(pp, &pq);
1.1 pk 1344: }
1.57 sommerfe 1345: #undef pool_put
1.59 thorpej 1346: #endif /* POOL_DIAGNOSTIC */
1.1 pk 1347:
1.56 sommerfe 1348: void
1349: pool_put(struct pool *pp, void *v)
1350: {
1.101 thorpej 1351: struct pool_pagelist pq;
1352:
1353: LIST_INIT(&pq);
1.56 sommerfe 1354:
1.101.2.6 yamt 1355: mutex_enter(&pp->pr_lock);
1.101 thorpej 1356: pool_do_put(pp, v, &pq);
1.101.2.6 yamt 1357: mutex_exit(&pp->pr_lock);
1.56 sommerfe 1358:
1.101.2.1 yamt 1359: pr_pagelist_free(pp, &pq);
1.56 sommerfe 1360: }
1.57 sommerfe 1361:
1.59 thorpej 1362: #ifdef POOL_DIAGNOSTIC
1.57 sommerfe 1363: #define pool_put(h, v) _pool_put((h), (v), __FILE__, __LINE__)
1.56 sommerfe 1364: #endif
1.74 thorpej 1365:
1366: /*
1.101.2.1 yamt 1367: * pool_grow: grow a pool by a page.
1368: *
1369: * => called with pool locked.
1370: * => unlock and relock the pool.
1371: * => return with pool locked.
1372: */
1373:
1374: static int
1375: pool_grow(struct pool *pp, int flags)
1376: {
1377: struct pool_item_header *ph = NULL;
1378: char *cp;
1379:
1.101.2.6 yamt 1380: mutex_exit(&pp->pr_lock);
1.101.2.1 yamt 1381: cp = pool_allocator_alloc(pp, flags);
1382: if (__predict_true(cp != NULL)) {
1383: ph = pool_alloc_item_header(pp, cp, flags);
1384: }
1385: if (__predict_false(cp == NULL || ph == NULL)) {
1386: if (cp != NULL) {
1387: pool_allocator_free(pp, cp);
1388: }
1.101.2.6 yamt 1389: mutex_enter(&pp->pr_lock);
1.101.2.1 yamt 1390: return ENOMEM;
1391: }
1392:
1.101.2.6 yamt 1393: mutex_enter(&pp->pr_lock);
1.101.2.1 yamt 1394: pool_prime_page(pp, cp, ph);
1395: pp->pr_npagealloc++;
1396: return 0;
1397: }
1398:
1399: /*
1.74 thorpej 1400: * Add N items to the pool.
1401: */
1402: int
1403: pool_prime(struct pool *pp, int n)
1404: {
1.75 simonb 1405: int newpages;
1.101.2.1 yamt 1406: int error = 0;
1.74 thorpej 1407:
1.101.2.6 yamt 1408: mutex_enter(&pp->pr_lock);
1.74 thorpej 1409:
1410: newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1411:
1412: while (newpages-- > 0) {
1.101.2.1 yamt 1413: error = pool_grow(pp, PR_NOWAIT);
1414: if (error) {
1.74 thorpej 1415: break;
1416: }
1417: pp->pr_minpages++;
1418: }
1419:
1420: if (pp->pr_minpages >= pp->pr_maxpages)
1421: pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */
1422:
1.101.2.6 yamt 1423: mutex_exit(&pp->pr_lock);
1.101.2.1 yamt 1424: return error;
1.74 thorpej 1425: }
1.55 thorpej 1426:
1427: /*
1.3 pk 1428: * Add a page worth of items to the pool.
1.21 thorpej 1429: *
1430: * Note, we must be called with the pool descriptor LOCKED.
1.3 pk 1431: */
1.55 thorpej 1432: static void
1.101.2.4 yamt 1433: pool_prime_page(struct pool *pp, void *storage, struct pool_item_header *ph)
1.3 pk 1434: {
1435: struct pool_item *pi;
1.101.2.4 yamt 1436: void *cp = storage;
1.101.2.3 yamt 1437: const unsigned int align = pp->pr_align;
1438: const unsigned int ioff = pp->pr_itemoffset;
1.55 thorpej 1439: int n;
1.36 pk 1440:
1.101.2.6 yamt 1441: KASSERT(mutex_owned(&pp->pr_lock));
1.91 yamt 1442:
1.66 thorpej 1443: #ifdef DIAGNOSTIC
1.101.2.2 yamt 1444: if ((pp->pr_roflags & PR_NOALIGN) == 0 &&
1445: ((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0)
1.36 pk 1446: panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1.66 thorpej 1447: #endif
1.3 pk 1448:
1449: /*
1450: * Insert page header.
1451: */
1.88 chs 1452: LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.101.2.1 yamt 1453: LIST_INIT(&ph->ph_itemlist);
1.3 pk 1454: ph->ph_page = storage;
1455: ph->ph_nmissing = 0;
1.101.2.1 yamt 1456: getmicrotime(&ph->ph_time);
1.88 chs 1457: if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1458: SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
1.3 pk 1459:
1.6 thorpej 1460: pp->pr_nidle++;
1461:
1.3 pk 1462: /*
1463: * Color this page.
1464: */
1.101.2.8! yamt 1465: ph->ph_off = pp->pr_curcolor;
! 1466: cp = (char *)cp + ph->ph_off;
1.3 pk 1467: if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
1468: pp->pr_curcolor = 0;
1469:
1470: /*
1471: * Adjust storage to apply aligment to `pr_itemoffset' in each item.
1472: */
1473: if (ioff != 0)
1.101.2.4 yamt 1474: cp = (char *)cp + align - ioff;
1.3 pk 1475:
1.101.2.3 yamt 1476: KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1477:
1.3 pk 1478: /*
1479: * Insert remaining chunks on the bucket list.
1480: */
1481: n = pp->pr_itemsperpage;
1.20 thorpej 1482: pp->pr_nitems += n;
1.3 pk 1483:
1.97 yamt 1484: if (pp->pr_roflags & PR_NOTOUCH) {
1.101.2.6 yamt 1485: pr_item_notouch_init(pp, ph);
1.97 yamt 1486: } else {
1487: while (n--) {
1488: pi = (struct pool_item *)cp;
1.78 thorpej 1489:
1.97 yamt 1490: KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
1.3 pk 1491:
1.97 yamt 1492: /* Insert on page list */
1.101.2.1 yamt 1493: LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.3 pk 1494: #ifdef DIAGNOSTIC
1.97 yamt 1495: pi->pi_magic = PI_MAGIC;
1.3 pk 1496: #endif
1.101.2.4 yamt 1497: cp = (char *)cp + pp->pr_size;
1.101.2.3 yamt 1498:
1499: KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1.97 yamt 1500: }
1.3 pk 1501: }
1502:
1503: /*
1504: * If the pool was depleted, point at the new page.
1505: */
1506: if (pp->pr_curpage == NULL)
1507: pp->pr_curpage = ph;
1508:
1509: if (++pp->pr_npages > pp->pr_hiwat)
1510: pp->pr_hiwat = pp->pr_npages;
1511: }
1512:
1.20 thorpej 1513: /*
1.52 thorpej 1514: * Used by pool_get() when nitems drops below the low water mark. This
1.88 chs 1515: * is used to catch up pr_nitems with the low water mark.
1.20 thorpej 1516: *
1.21 thorpej 1517: * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20 thorpej 1518: *
1.73 thorpej 1519: * Note 2, we must be called with the pool already locked, and we return
1.20 thorpej 1520: * with it locked.
1521: */
1522: static int
1.42 thorpej 1523: pool_catchup(struct pool *pp)
1.20 thorpej 1524: {
1525: int error = 0;
1526:
1.54 thorpej 1527: while (POOL_NEEDS_CATCHUP(pp)) {
1.101.2.1 yamt 1528: error = pool_grow(pp, PR_NOWAIT);
1529: if (error) {
1.20 thorpej 1530: break;
1531: }
1532: }
1.101.2.1 yamt 1533: return error;
1.20 thorpej 1534: }
1535:
1.88 chs 1536: static void
1537: pool_update_curpage(struct pool *pp)
1538: {
1539:
1540: pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
1541: if (pp->pr_curpage == NULL) {
1542: pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
1543: }
1544: }
1545:
1.3 pk 1546: void
1.42 thorpej 1547: pool_setlowat(struct pool *pp, int n)
1.3 pk 1548: {
1.15 pk 1549:
1.101.2.6 yamt 1550: mutex_enter(&pp->pr_lock);
1.21 thorpej 1551:
1.3 pk 1552: pp->pr_minitems = n;
1.15 pk 1553: pp->pr_minpages = (n == 0)
1554: ? 0
1.18 thorpej 1555: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20 thorpej 1556:
1557: /* Make sure we're caught up with the newly-set low water mark. */
1.75 simonb 1558: if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20 thorpej 1559: /*
1560: * XXX: Should we log a warning? Should we set up a timeout
1561: * to try again in a second or so? The latter could break
1562: * a caller's assumptions about interrupt protection, etc.
1563: */
1564: }
1.21 thorpej 1565:
1.101.2.6 yamt 1566: mutex_exit(&pp->pr_lock);
1.3 pk 1567: }
1568:
1569: void
1.42 thorpej 1570: pool_sethiwat(struct pool *pp, int n)
1.3 pk 1571: {
1.15 pk 1572:
1.101.2.6 yamt 1573: mutex_enter(&pp->pr_lock);
1.21 thorpej 1574:
1.15 pk 1575: pp->pr_maxpages = (n == 0)
1576: ? 0
1.18 thorpej 1577: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1578:
1.101.2.6 yamt 1579: mutex_exit(&pp->pr_lock);
1.3 pk 1580: }
1581:
1.20 thorpej 1582: void
1.42 thorpej 1583: pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1.20 thorpej 1584: {
1585:
1.101.2.6 yamt 1586: mutex_enter(&pp->pr_lock);
1.20 thorpej 1587:
1588: pp->pr_hardlimit = n;
1589: pp->pr_hardlimit_warning = warnmess;
1.31 thorpej 1590: pp->pr_hardlimit_ratecap.tv_sec = ratecap;
1591: pp->pr_hardlimit_warning_last.tv_sec = 0;
1592: pp->pr_hardlimit_warning_last.tv_usec = 0;
1.20 thorpej 1593:
1594: /*
1.21 thorpej 1595: * In-line version of pool_sethiwat(), because we don't want to
1596: * release the lock.
1.20 thorpej 1597: */
1598: pp->pr_maxpages = (n == 0)
1599: ? 0
1600: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1601:
1.101.2.6 yamt 1602: mutex_exit(&pp->pr_lock);
1.20 thorpej 1603: }
1.3 pk 1604:
1605: /*
1606: * Release all complete pages that have not been used recently.
1607: */
1.66 thorpej 1608: int
1.59 thorpej 1609: #ifdef POOL_DIAGNOSTIC
1.42 thorpej 1610: _pool_reclaim(struct pool *pp, const char *file, long line)
1.56 sommerfe 1611: #else
1612: pool_reclaim(struct pool *pp)
1613: #endif
1.3 pk 1614: {
1615: struct pool_item_header *ph, *phnext;
1.61 chs 1616: struct pool_pagelist pq;
1.101.2.1 yamt 1617: struct timeval curtime, diff;
1.101.2.6 yamt 1618: bool klock;
1619: int rv;
1.3 pk 1620:
1.68 thorpej 1621: if (pp->pr_drain_hook != NULL) {
1622: /*
1623: * The drain hook must be called with the pool unlocked.
1624: */
1625: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT);
1626: }
1627:
1.101.2.6 yamt 1628: /*
1629: * XXXSMP Because mutexes at IPL_SOFTXXX are still spinlocks,
1630: * and we are called from the pagedaemon without kernel_lock.
1631: * Does not apply to IPL_SOFTBIO.
1632: */
1633: if (pp->pr_ipl == IPL_SOFTNET || pp->pr_ipl == IPL_SOFTCLOCK ||
1634: pp->pr_ipl == IPL_SOFTSERIAL) {
1635: KERNEL_LOCK(1, NULL);
1636: klock = true;
1637: } else
1638: klock = false;
1639:
1640: /* Reclaim items from the pool's cache (if any). */
1641: if (pp->pr_cache != NULL)
1642: pool_cache_invalidate(pp->pr_cache);
1643:
1644: if (mutex_tryenter(&pp->pr_lock) == 0) {
1645: if (klock) {
1646: KERNEL_UNLOCK_ONE(NULL);
1647: }
1.66 thorpej 1648: return (0);
1.101.2.6 yamt 1649: }
1.25 thorpej 1650: pr_enter(pp, file, line);
1.68 thorpej 1651:
1.88 chs 1652: LIST_INIT(&pq);
1.43 thorpej 1653:
1.101.2.1 yamt 1654: getmicrotime(&curtime);
1.21 thorpej 1655:
1.88 chs 1656: for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
1657: phnext = LIST_NEXT(ph, ph_pagelist);
1.3 pk 1658:
1659: /* Check our minimum page claim */
1660: if (pp->pr_npages <= pp->pr_minpages)
1661: break;
1662:
1.88 chs 1663: KASSERT(ph->ph_nmissing == 0);
1664: timersub(&curtime, &ph->ph_time, &diff);
1.101.2.1 yamt 1665: if (diff.tv_sec < pool_inactive_time
1666: && !pa_starved_p(pp->pr_alloc))
1.88 chs 1667: continue;
1.21 thorpej 1668:
1.88 chs 1669: /*
1670: * If freeing this page would put us below
1671: * the low water mark, stop now.
1672: */
1673: if ((pp->pr_nitems - pp->pr_itemsperpage) <
1674: pp->pr_minitems)
1675: break;
1.21 thorpej 1676:
1.88 chs 1677: pr_rmpage(pp, ph, &pq);
1.3 pk 1678: }
1679:
1.25 thorpej 1680: pr_leave(pp);
1.101.2.6 yamt 1681: mutex_exit(&pp->pr_lock);
1.66 thorpej 1682:
1.101.2.6 yamt 1683: if (LIST_EMPTY(&pq))
1684: rv = 0;
1685: else {
1686: pr_pagelist_free(pp, &pq);
1687: rv = 1;
1688: }
1689:
1690: if (klock) {
1691: KERNEL_UNLOCK_ONE(NULL);
1692: }
1693:
1694: return (rv);
1.3 pk 1695: }
1696:
1697: /*
1.101.2.6 yamt 1698: * Drain pools, one at a time. This is a two stage process;
1699: * drain_start kicks off a cross call to drain CPU-level caches
1700: * if the pool has an associated pool_cache. drain_end waits
1701: * for those cross calls to finish, and then drains the cache
1702: * (if any) and pool.
1.101.2.4 yamt 1703: *
1.101.2.6 yamt 1704: * Note, must never be called from interrupt context.
1.3 pk 1705: */
1706: void
1.101.2.6 yamt 1707: pool_drain_start(struct pool **ppp, uint64_t *wp)
1.3 pk 1708: {
1709: struct pool *pp;
1.101.2.6 yamt 1710:
1.101.2.8! yamt 1711: KASSERT(!TAILQ_EMPTY(&pool_head));
1.3 pk 1712:
1.61 chs 1713: pp = NULL;
1.101.2.6 yamt 1714:
1715: /* Find next pool to drain, and add a reference. */
1716: mutex_enter(&pool_head_lock);
1717: do {
1718: if (drainpp == NULL) {
1.101.2.8! yamt 1719: drainpp = TAILQ_FIRST(&pool_head);
1.101.2.6 yamt 1720: }
1721: if (drainpp != NULL) {
1722: pp = drainpp;
1.101.2.8! yamt 1723: drainpp = TAILQ_NEXT(pp, pr_poollist);
1.101.2.6 yamt 1724: }
1725: /*
1726: * Skip completely idle pools. We depend on at least
1727: * one pool in the system being active.
1728: */
1729: } while (pp == NULL || pp->pr_npages == 0);
1730: pp->pr_refcnt++;
1731: mutex_exit(&pool_head_lock);
1732:
1733: /* If there is a pool_cache, drain CPU level caches. */
1734: *ppp = pp;
1735: if (pp->pr_cache != NULL) {
1736: *wp = xc_broadcast(0, (xcfunc_t)pool_cache_xcall,
1737: pp->pr_cache, NULL);
1738: }
1739: }
1740:
1741: void
1742: pool_drain_end(struct pool *pp, uint64_t where)
1743: {
1744:
1745: if (pp == NULL)
1746: return;
1747:
1748: KASSERT(pp->pr_refcnt > 0);
1749:
1750: /* Wait for remote draining to complete. */
1751: if (pp->pr_cache != NULL)
1752: xc_wait(where);
1753:
1754: /* Drain the cache (if any) and pool.. */
1755: pool_reclaim(pp);
1756:
1757: /* Finally, unlock the pool. */
1758: mutex_enter(&pool_head_lock);
1759: pp->pr_refcnt--;
1760: cv_broadcast(&pool_busy);
1761: mutex_exit(&pool_head_lock);
1.3 pk 1762: }
1763:
1764: /*
1765: * Diagnostic helpers.
1766: */
1767: void
1.42 thorpej 1768: pool_print(struct pool *pp, const char *modif)
1.21 thorpej 1769: {
1770:
1.25 thorpej 1771: pool_print1(pp, modif, printf);
1.21 thorpej 1772: }
1773:
1.25 thorpej 1774: void
1.101.2.1 yamt 1775: pool_printall(const char *modif, void (*pr)(const char *, ...))
1776: {
1777: struct pool *pp;
1778:
1.101.2.8! yamt 1779: TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
1.101.2.1 yamt 1780: pool_printit(pp, modif, pr);
1781: }
1782: }
1783:
1784: void
1.42 thorpej 1785: pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.25 thorpej 1786: {
1787:
1788: if (pp == NULL) {
1789: (*pr)("Must specify a pool to print.\n");
1790: return;
1791: }
1792:
1793: pool_print1(pp, modif, pr);
1794: }
1795:
1.21 thorpej 1796: static void
1.97 yamt 1797: pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
1798: void (*pr)(const char *, ...))
1.88 chs 1799: {
1800: struct pool_item_header *ph;
1801: #ifdef DIAGNOSTIC
1802: struct pool_item *pi;
1803: #endif
1804:
1805: LIST_FOREACH(ph, pl, ph_pagelist) {
1806: (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
1807: ph->ph_page, ph->ph_nmissing,
1808: (u_long)ph->ph_time.tv_sec,
1809: (u_long)ph->ph_time.tv_usec);
1810: #ifdef DIAGNOSTIC
1.97 yamt 1811: if (!(pp->pr_roflags & PR_NOTOUCH)) {
1.101.2.1 yamt 1812: LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1.97 yamt 1813: if (pi->pi_magic != PI_MAGIC) {
1814: (*pr)("\t\t\titem %p, magic 0x%x\n",
1815: pi, pi->pi_magic);
1816: }
1.88 chs 1817: }
1818: }
1819: #endif
1820: }
1821: }
1822:
1823: static void
1.42 thorpej 1824: pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.3 pk 1825: {
1.25 thorpej 1826: struct pool_item_header *ph;
1.101.2.6 yamt 1827: pool_cache_t pc;
1828: pcg_t *pcg;
1829: pool_cache_cpu_t *cc;
1830: uint64_t cpuhit, cpumiss;
1.44 thorpej 1831: int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1.25 thorpej 1832: char c;
1833:
1834: while ((c = *modif++) != '\0') {
1835: if (c == 'l')
1836: print_log = 1;
1837: if (c == 'p')
1838: print_pagelist = 1;
1.44 thorpej 1839: if (c == 'c')
1840: print_cache = 1;
1.25 thorpej 1841: }
1842:
1.101.2.6 yamt 1843: if ((pc = pp->pr_cache) != NULL) {
1844: (*pr)("POOL CACHE");
1845: } else {
1846: (*pr)("POOL");
1847: }
1848:
1849: (*pr)(" %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1.25 thorpej 1850: pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1851: pp->pr_roflags);
1.66 thorpej 1852: (*pr)("\talloc %p\n", pp->pr_alloc);
1.25 thorpej 1853: (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1854: pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1855: (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1856: pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1857:
1.101.2.6 yamt 1858: (*pr)("\tnget %lu, nfail %lu, nput %lu\n",
1.25 thorpej 1859: pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1860: (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1861: pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1862:
1863: if (print_pagelist == 0)
1864: goto skip_pagelist;
1865:
1.88 chs 1866: if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1867: (*pr)("\n\tempty page list:\n");
1.97 yamt 1868: pool_print_pagelist(pp, &pp->pr_emptypages, pr);
1.88 chs 1869: if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
1870: (*pr)("\n\tfull page list:\n");
1.97 yamt 1871: pool_print_pagelist(pp, &pp->pr_fullpages, pr);
1.88 chs 1872: if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
1873: (*pr)("\n\tpartial-page list:\n");
1.97 yamt 1874: pool_print_pagelist(pp, &pp->pr_partpages, pr);
1.88 chs 1875:
1.25 thorpej 1876: if (pp->pr_curpage == NULL)
1877: (*pr)("\tno current page\n");
1878: else
1879: (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1880:
1881: skip_pagelist:
1882: if (print_log == 0)
1883: goto skip_log;
1884:
1885: (*pr)("\n");
1886: if ((pp->pr_roflags & PR_LOGGING) == 0)
1887: (*pr)("\tno log\n");
1.101.2.2 yamt 1888: else {
1.25 thorpej 1889: pr_printlog(pp, NULL, pr);
1.101.2.2 yamt 1890: }
1.3 pk 1891:
1.25 thorpej 1892: skip_log:
1.44 thorpej 1893:
1.101.2.1 yamt 1894: #define PR_GROUPLIST(pcg) \
1895: (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail); \
1.101.2.8! yamt 1896: for (i = 0; i < pcg->pcg_size; i++) { \
1.101.2.1 yamt 1897: if (pcg->pcg_objects[i].pcgo_pa != \
1898: POOL_PADDR_INVALID) { \
1899: (*pr)("\t\t\t%p, 0x%llx\n", \
1900: pcg->pcg_objects[i].pcgo_va, \
1901: (unsigned long long) \
1902: pcg->pcg_objects[i].pcgo_pa); \
1903: } else { \
1904: (*pr)("\t\t\t%p\n", \
1905: pcg->pcg_objects[i].pcgo_va); \
1906: } \
1907: }
1908:
1.101.2.6 yamt 1909: if (pc != NULL) {
1910: cpuhit = 0;
1911: cpumiss = 0;
1912: for (i = 0; i < MAXCPUS; i++) {
1913: if ((cc = pc->pc_cpus[i]) == NULL)
1914: continue;
1915: cpuhit += cc->cc_hits;
1916: cpumiss += cc->cc_misses;
1917: }
1918: (*pr)("\tcpu layer hits %llu misses %llu\n", cpuhit, cpumiss);
1919: (*pr)("\tcache layer hits %llu misses %llu\n",
1920: pc->pc_hits, pc->pc_misses);
1921: (*pr)("\tcache layer entry uncontended %llu contended %llu\n",
1922: pc->pc_hits + pc->pc_misses - pc->pc_contended,
1923: pc->pc_contended);
1924: (*pr)("\tcache layer empty groups %u full groups %u\n",
1925: pc->pc_nempty, pc->pc_nfull);
1926: if (print_cache) {
1927: (*pr)("\tfull cache groups:\n");
1928: for (pcg = pc->pc_fullgroups; pcg != NULL;
1929: pcg = pcg->pcg_next) {
1930: PR_GROUPLIST(pcg);
1931: }
1932: (*pr)("\tempty cache groups:\n");
1933: for (pcg = pc->pc_emptygroups; pcg != NULL;
1934: pcg = pcg->pcg_next) {
1935: PR_GROUPLIST(pcg);
1936: }
1.44 thorpej 1937: }
1938: }
1.101.2.1 yamt 1939: #undef PR_GROUPLIST
1.44 thorpej 1940:
1.88 chs 1941: pr_enter_check(pp, pr);
1942: }
1943:
1944: static int
1945: pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
1946: {
1947: struct pool_item *pi;
1.101.2.4 yamt 1948: void *page;
1.88 chs 1949: int n;
1950:
1.101.2.2 yamt 1951: if ((pp->pr_roflags & PR_NOALIGN) == 0) {
1.101.2.4 yamt 1952: page = (void *)((uintptr_t)ph & pp->pr_alloc->pa_pagemask);
1.101.2.2 yamt 1953: if (page != ph->ph_page &&
1954: (pp->pr_roflags & PR_PHINPAGE) != 0) {
1955: if (label != NULL)
1956: printf("%s: ", label);
1957: printf("pool(%p:%s): page inconsistency: page %p;"
1958: " at page head addr %p (p %p)\n", pp,
1959: pp->pr_wchan, ph->ph_page,
1960: ph, page);
1961: return 1;
1962: }
1.88 chs 1963: }
1.3 pk 1964:
1.97 yamt 1965: if ((pp->pr_roflags & PR_NOTOUCH) != 0)
1966: return 0;
1967:
1.101.2.1 yamt 1968: for (pi = LIST_FIRST(&ph->ph_itemlist), n = 0;
1.88 chs 1969: pi != NULL;
1.101.2.1 yamt 1970: pi = LIST_NEXT(pi,pi_list), n++) {
1.88 chs 1971:
1972: #ifdef DIAGNOSTIC
1973: if (pi->pi_magic != PI_MAGIC) {
1974: if (label != NULL)
1975: printf("%s: ", label);
1976: printf("pool(%s): free list modified: magic=%x;"
1.101.2.2 yamt 1977: " page %p; item ordinal %d; addr %p\n",
1.88 chs 1978: pp->pr_wchan, pi->pi_magic, ph->ph_page,
1.101.2.2 yamt 1979: n, pi);
1.88 chs 1980: panic("pool");
1981: }
1982: #endif
1.101.2.2 yamt 1983: if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1984: continue;
1985: }
1.101.2.4 yamt 1986: page = (void *)((uintptr_t)pi & pp->pr_alloc->pa_pagemask);
1.88 chs 1987: if (page == ph->ph_page)
1988: continue;
1989:
1990: if (label != NULL)
1991: printf("%s: ", label);
1992: printf("pool(%p:%s): page inconsistency: page %p;"
1993: " item ordinal %d; addr %p (p %p)\n", pp,
1994: pp->pr_wchan, ph->ph_page,
1995: n, pi, page);
1996: return 1;
1997: }
1998: return 0;
1.3 pk 1999: }
2000:
1.88 chs 2001:
1.3 pk 2002: int
1.42 thorpej 2003: pool_chk(struct pool *pp, const char *label)
1.3 pk 2004: {
2005: struct pool_item_header *ph;
2006: int r = 0;
2007:
1.101.2.6 yamt 2008: mutex_enter(&pp->pr_lock);
1.88 chs 2009: LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
2010: r = pool_chk_page(pp, label, ph);
2011: if (r) {
2012: goto out;
2013: }
2014: }
2015: LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
2016: r = pool_chk_page(pp, label, ph);
2017: if (r) {
1.3 pk 2018: goto out;
2019: }
1.88 chs 2020: }
2021: LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
2022: r = pool_chk_page(pp, label, ph);
2023: if (r) {
1.3 pk 2024: goto out;
2025: }
2026: }
1.88 chs 2027:
1.3 pk 2028: out:
1.101.2.6 yamt 2029: mutex_exit(&pp->pr_lock);
1.3 pk 2030: return (r);
1.43 thorpej 2031: }
2032:
2033: /*
2034: * pool_cache_init:
2035: *
2036: * Initialize a pool cache.
1.101.2.6 yamt 2037: */
2038: pool_cache_t
2039: pool_cache_init(size_t size, u_int align, u_int align_offset, u_int flags,
2040: const char *wchan, struct pool_allocator *palloc, int ipl,
2041: int (*ctor)(void *, void *, int), void (*dtor)(void *, void *), void *arg)
2042: {
2043: pool_cache_t pc;
2044:
2045: pc = pool_get(&cache_pool, PR_WAITOK);
2046: if (pc == NULL)
2047: return NULL;
2048:
2049: pool_cache_bootstrap(pc, size, align, align_offset, flags, wchan,
2050: palloc, ipl, ctor, dtor, arg);
2051:
2052: return pc;
2053: }
2054:
2055: /*
2056: * pool_cache_bootstrap:
1.43 thorpej 2057: *
1.101.2.6 yamt 2058: * Kernel-private version of pool_cache_init(). The caller
2059: * provides initial storage.
1.43 thorpej 2060: */
2061: void
1.101.2.6 yamt 2062: pool_cache_bootstrap(pool_cache_t pc, size_t size, u_int align,
2063: u_int align_offset, u_int flags, const char *wchan,
2064: struct pool_allocator *palloc, int ipl,
2065: int (*ctor)(void *, void *, int), void (*dtor)(void *, void *),
1.43 thorpej 2066: void *arg)
2067: {
1.101.2.6 yamt 2068: CPU_INFO_ITERATOR cii;
1.101.2.8! yamt 2069: pool_cache_t pc1;
1.101.2.6 yamt 2070: struct cpu_info *ci;
2071: struct pool *pp;
2072:
2073: pp = &pc->pc_pool;
2074: if (palloc == NULL && ipl == IPL_NONE)
2075: palloc = &pool_allocator_nointr;
2076: pool_init(pp, size, align, align_offset, flags, wchan, palloc, ipl);
1.43 thorpej 2077:
1.101.2.7 yamt 2078: /*
2079: * XXXAD hack to prevent IP input processing from blocking.
2080: */
2081: if (ipl == IPL_SOFTNET) {
2082: mutex_init(&pc->pc_lock, MUTEX_DEFAULT, IPL_VM);
2083: } else {
2084: mutex_init(&pc->pc_lock, MUTEX_DEFAULT, ipl);
2085: }
1.43 thorpej 2086:
1.101.2.6 yamt 2087: if (ctor == NULL) {
2088: ctor = (int (*)(void *, void *, int))nullop;
2089: }
2090: if (dtor == NULL) {
2091: dtor = (void (*)(void *, void *))nullop;
2092: }
1.43 thorpej 2093:
1.101.2.6 yamt 2094: pc->pc_emptygroups = NULL;
2095: pc->pc_fullgroups = NULL;
2096: pc->pc_partgroups = NULL;
1.43 thorpej 2097: pc->pc_ctor = ctor;
2098: pc->pc_dtor = dtor;
2099: pc->pc_arg = arg;
1.101.2.6 yamt 2100: pc->pc_hits = 0;
1.48 thorpej 2101: pc->pc_misses = 0;
1.101.2.6 yamt 2102: pc->pc_nempty = 0;
2103: pc->pc_npart = 0;
2104: pc->pc_nfull = 0;
2105: pc->pc_contended = 0;
2106: pc->pc_refcnt = 0;
2107: pc->pc_freecheck = NULL;
2108:
1.101.2.8! yamt 2109: if ((flags & PR_LARGECACHE) != 0) {
! 2110: pc->pc_pcgsize = PCG_NOBJECTS_LARGE;
! 2111: } else {
! 2112: pc->pc_pcgsize = PCG_NOBJECTS_NORMAL;
! 2113: }
! 2114:
1.101.2.6 yamt 2115: /* Allocate per-CPU caches. */
2116: memset(pc->pc_cpus, 0, sizeof(pc->pc_cpus));
2117: pc->pc_ncpu = 0;
1.101.2.8! yamt 2118: if (ncpu < 2) {
1.101.2.7 yamt 2119: /* XXX For sparc: boot CPU is not attached yet. */
2120: pool_cache_cpu_init1(curcpu(), pc);
2121: } else {
2122: for (CPU_INFO_FOREACH(cii, ci)) {
2123: pool_cache_cpu_init1(ci, pc);
2124: }
1.101.2.6 yamt 2125: }
1.101.2.8! yamt 2126:
! 2127: /* Add to list of all pools. */
! 2128: if (__predict_true(!cold))
1.101.2.6 yamt 2129: mutex_enter(&pool_head_lock);
1.101.2.8! yamt 2130: TAILQ_FOREACH(pc1, &pool_cache_head, pc_cachelist) {
! 2131: if (strcmp(pc1->pc_pool.pr_wchan, pc->pc_pool.pr_wchan) > 0)
! 2132: break;
1.101.2.6 yamt 2133: }
1.101.2.8! yamt 2134: if (pc1 == NULL)
! 2135: TAILQ_INSERT_TAIL(&pool_cache_head, pc, pc_cachelist);
! 2136: else
! 2137: TAILQ_INSERT_BEFORE(pc1, pc, pc_cachelist);
! 2138: if (__predict_true(!cold))
! 2139: mutex_exit(&pool_head_lock);
! 2140:
! 2141: membar_sync();
! 2142: pp->pr_cache = pc;
1.43 thorpej 2143: }
2144:
2145: /*
2146: * pool_cache_destroy:
2147: *
2148: * Destroy a pool cache.
2149: */
2150: void
1.101.2.6 yamt 2151: pool_cache_destroy(pool_cache_t pc)
1.43 thorpej 2152: {
1.101.2.6 yamt 2153: struct pool *pp = &pc->pc_pool;
2154: pool_cache_cpu_t *cc;
2155: pcg_t *pcg;
2156: int i;
2157:
2158: /* Remove it from the global list. */
2159: mutex_enter(&pool_head_lock);
2160: while (pc->pc_refcnt != 0)
2161: cv_wait(&pool_busy, &pool_head_lock);
1.101.2.8! yamt 2162: TAILQ_REMOVE(&pool_cache_head, pc, pc_cachelist);
1.101.2.6 yamt 2163: mutex_exit(&pool_head_lock);
1.43 thorpej 2164:
2165: /* First, invalidate the entire cache. */
2166: pool_cache_invalidate(pc);
2167:
1.101.2.6 yamt 2168: /* Disassociate it from the pool. */
2169: mutex_enter(&pp->pr_lock);
2170: pp->pr_cache = NULL;
2171: mutex_exit(&pp->pr_lock);
2172:
2173: /* Destroy per-CPU data */
2174: for (i = 0; i < MAXCPUS; i++) {
2175: if ((cc = pc->pc_cpus[i]) == NULL)
2176: continue;
2177: if ((pcg = cc->cc_current) != NULL) {
2178: pcg->pcg_next = NULL;
2179: pool_cache_invalidate_groups(pc, pcg);
2180: }
2181: if ((pcg = cc->cc_previous) != NULL) {
2182: pcg->pcg_next = NULL;
2183: pool_cache_invalidate_groups(pc, pcg);
2184: }
2185: if (cc != &pc->pc_cpu0)
2186: pool_put(&cache_cpu_pool, cc);
2187: }
2188:
2189: /* Finally, destroy it. */
2190: mutex_destroy(&pc->pc_lock);
2191: pool_destroy(pp);
2192: pool_put(&cache_pool, pc);
1.43 thorpej 2193: }
2194:
1.101.2.6 yamt 2195: /*
2196: * pool_cache_cpu_init1:
2197: *
2198: * Called for each pool_cache whenever a new CPU is attached.
2199: */
2200: static void
2201: pool_cache_cpu_init1(struct cpu_info *ci, pool_cache_t pc)
1.43 thorpej 2202: {
1.101.2.6 yamt 2203: pool_cache_cpu_t *cc;
1.101.2.7 yamt 2204: int index;
2205:
2206: index = ci->ci_index;
1.43 thorpej 2207:
1.101.2.7 yamt 2208: KASSERT(index < MAXCPUS);
1.101.2.6 yamt 2209: KASSERT(((uintptr_t)pc->pc_cpus & (CACHE_LINE_SIZE - 1)) == 0);
1.43 thorpej 2210:
1.101.2.7 yamt 2211: if ((cc = pc->pc_cpus[index]) != NULL) {
2212: KASSERT(cc->cc_cpuindex == index);
1.101.2.6 yamt 2213: return;
2214: }
2215:
2216: /*
2217: * The first CPU is 'free'. This needs to be the case for
2218: * bootstrap - we may not be able to allocate yet.
2219: */
2220: if (pc->pc_ncpu == 0) {
2221: cc = &pc->pc_cpu0;
2222: pc->pc_ncpu = 1;
2223: } else {
2224: mutex_enter(&pc->pc_lock);
2225: pc->pc_ncpu++;
2226: mutex_exit(&pc->pc_lock);
2227: cc = pool_get(&cache_cpu_pool, PR_WAITOK);
2228: }
2229:
2230: cc->cc_ipl = pc->pc_pool.pr_ipl;
2231: cc->cc_iplcookie = makeiplcookie(cc->cc_ipl);
2232: cc->cc_cache = pc;
1.101.2.7 yamt 2233: cc->cc_cpuindex = index;
1.101.2.6 yamt 2234: cc->cc_hits = 0;
2235: cc->cc_misses = 0;
2236: cc->cc_current = NULL;
2237: cc->cc_previous = NULL;
2238:
1.101.2.7 yamt 2239: pc->pc_cpus[index] = cc;
1.43 thorpej 2240: }
2241:
1.101.2.6 yamt 2242: /*
2243: * pool_cache_cpu_init:
2244: *
2245: * Called whenever a new CPU is attached.
2246: */
2247: void
2248: pool_cache_cpu_init(struct cpu_info *ci)
1.43 thorpej 2249: {
1.101.2.6 yamt 2250: pool_cache_t pc;
1.43 thorpej 2251:
1.101.2.6 yamt 2252: mutex_enter(&pool_head_lock);
1.101.2.8! yamt 2253: TAILQ_FOREACH(pc, &pool_cache_head, pc_cachelist) {
1.101.2.6 yamt 2254: pc->pc_refcnt++;
2255: mutex_exit(&pool_head_lock);
1.43 thorpej 2256:
1.101.2.6 yamt 2257: pool_cache_cpu_init1(ci, pc);
2258:
2259: mutex_enter(&pool_head_lock);
2260: pc->pc_refcnt--;
2261: cv_broadcast(&pool_busy);
2262: }
2263: mutex_exit(&pool_head_lock);
2264: }
2265:
2266: /*
2267: * pool_cache_reclaim:
2268: *
2269: * Reclaim memory from a pool cache.
2270: */
2271: bool
2272: pool_cache_reclaim(pool_cache_t pc)
2273: {
2274:
2275: return pool_reclaim(&pc->pc_pool);
1.43 thorpej 2276: }
2277:
1.101.2.1 yamt 2278: static void
1.101.2.6 yamt 2279: pool_cache_destruct_object1(pool_cache_t pc, void *object)
1.101.2.1 yamt 2280: {
2281:
1.101.2.6 yamt 2282: (*pc->pc_dtor)(pc->pc_arg, object);
2283: pool_put(&pc->pc_pool, object);
1.101.2.1 yamt 2284: }
2285:
1.43 thorpej 2286: /*
1.101.2.6 yamt 2287: * pool_cache_destruct_object:
1.43 thorpej 2288: *
1.101.2.6 yamt 2289: * Force destruction of an object and its release back into
2290: * the pool.
1.43 thorpej 2291: */
1.101.2.6 yamt 2292: void
2293: pool_cache_destruct_object(pool_cache_t pc, void *object)
1.43 thorpej 2294: {
1.58 thorpej 2295:
1.101.2.6 yamt 2296: FREECHECK_IN(&pc->pc_freecheck, object);
1.43 thorpej 2297:
1.101.2.6 yamt 2298: pool_cache_destruct_object1(pc, object);
2299: }
1.43 thorpej 2300:
1.101.2.6 yamt 2301: /*
2302: * pool_cache_invalidate_groups:
2303: *
2304: * Invalidate a chain of groups and destruct all objects.
2305: */
2306: static void
2307: pool_cache_invalidate_groups(pool_cache_t pc, pcg_t *pcg)
2308: {
2309: void *object;
2310: pcg_t *next;
2311: int i;
1.101.2.3 yamt 2312:
1.101.2.6 yamt 2313: for (; pcg != NULL; pcg = next) {
2314: next = pcg->pcg_next;
1.43 thorpej 2315:
1.101.2.6 yamt 2316: for (i = 0; i < pcg->pcg_avail; i++) {
2317: object = pcg->pcg_objects[i].pcgo_va;
2318: pool_cache_destruct_object1(pc, object);
2319: }
1.43 thorpej 2320:
1.101.2.8! yamt 2321: if (pcg->pcg_size == PCG_NOBJECTS_LARGE) {
! 2322: pool_put(&pcg_large_pool, pcg);
! 2323: } else {
! 2324: KASSERT(pcg->pcg_size == PCG_NOBJECTS_NORMAL);
! 2325: pool_put(&pcg_normal_pool, pcg);
! 2326: }
1.101.2.1 yamt 2327: }
1.43 thorpej 2328: }
2329:
2330: /*
1.101.2.6 yamt 2331: * pool_cache_invalidate:
1.43 thorpej 2332: *
1.101.2.6 yamt 2333: * Invalidate a pool cache (destruct and release all of the
2334: * cached objects). Does not reclaim objects from the pool.
1.43 thorpej 2335: */
2336: void
1.101.2.6 yamt 2337: pool_cache_invalidate(pool_cache_t pc)
1.43 thorpej 2338: {
1.101.2.6 yamt 2339: pcg_t *full, *empty, *part;
1.43 thorpej 2340:
1.101.2.6 yamt 2341: mutex_enter(&pc->pc_lock);
2342: full = pc->pc_fullgroups;
2343: empty = pc->pc_emptygroups;
2344: part = pc->pc_partgroups;
2345: pc->pc_fullgroups = NULL;
2346: pc->pc_emptygroups = NULL;
2347: pc->pc_partgroups = NULL;
2348: pc->pc_nfull = 0;
2349: pc->pc_nempty = 0;
2350: pc->pc_npart = 0;
2351: mutex_exit(&pc->pc_lock);
2352:
2353: pool_cache_invalidate_groups(pc, full);
2354: pool_cache_invalidate_groups(pc, empty);
2355: pool_cache_invalidate_groups(pc, part);
2356: }
2357:
2358: void
2359: pool_cache_set_drain_hook(pool_cache_t pc, void (*fn)(void *, int), void *arg)
2360: {
2361:
2362: pool_set_drain_hook(&pc->pc_pool, fn, arg);
2363: }
2364:
2365: void
2366: pool_cache_setlowat(pool_cache_t pc, int n)
2367: {
2368:
2369: pool_setlowat(&pc->pc_pool, n);
2370: }
2371:
2372: void
2373: pool_cache_sethiwat(pool_cache_t pc, int n)
2374: {
2375:
2376: pool_sethiwat(&pc->pc_pool, n);
2377: }
2378:
2379: void
2380: pool_cache_sethardlimit(pool_cache_t pc, int n, const char *warnmess, int ratecap)
2381: {
2382:
2383: pool_sethardlimit(&pc->pc_pool, n, warnmess, ratecap);
2384: }
2385:
2386: static inline pool_cache_cpu_t *
2387: pool_cache_cpu_enter(pool_cache_t pc, int *s)
2388: {
2389: pool_cache_cpu_t *cc;
2390:
2391: /*
2392: * Prevent other users of the cache from accessing our
2393: * CPU-local data. To avoid touching shared state, we
2394: * pull the neccessary information from CPU local data.
2395: */
1.101.2.7 yamt 2396: crit_enter();
2397: cc = pc->pc_cpus[curcpu()->ci_index];
1.101.2.6 yamt 2398: KASSERT(cc->cc_cache == pc);
1.101.2.7 yamt 2399: if (cc->cc_ipl != IPL_NONE) {
1.101.2.6 yamt 2400: *s = splraiseipl(cc->cc_iplcookie);
2401: }
2402: KASSERT(((uintptr_t)cc & (CACHE_LINE_SIZE - 1)) == 0);
1.43 thorpej 2403:
1.101.2.6 yamt 2404: return cc;
2405: }
2406:
2407: static inline void
2408: pool_cache_cpu_exit(pool_cache_cpu_t *cc, int *s)
2409: {
2410:
2411: /* No longer need exclusive access to the per-CPU data. */
1.101.2.7 yamt 2412: if (cc->cc_ipl != IPL_NONE) {
1.101.2.6 yamt 2413: splx(*s);
1.101.2.1 yamt 2414: }
1.101.2.7 yamt 2415: crit_exit();
1.101.2.6 yamt 2416: }
2417:
2418: #if __GNUC_PREREQ__(3, 0)
2419: __attribute ((noinline))
2420: #endif
2421: pool_cache_cpu_t *
2422: pool_cache_get_slow(pool_cache_cpu_t *cc, int *s, void **objectp,
2423: paddr_t *pap, int flags)
2424: {
2425: pcg_t *pcg, *cur;
2426: uint64_t ncsw;
2427: pool_cache_t pc;
2428: void *object;
2429:
2430: pc = cc->cc_cache;
2431: cc->cc_misses++;
2432:
2433: /*
2434: * Nothing was available locally. Try and grab a group
2435: * from the cache.
2436: */
2437: if (!mutex_tryenter(&pc->pc_lock)) {
2438: ncsw = curlwp->l_ncsw;
2439: mutex_enter(&pc->pc_lock);
2440: pc->pc_contended++;
1.43 thorpej 2441:
2442: /*
1.101.2.6 yamt 2443: * If we context switched while locking, then
2444: * our view of the per-CPU data is invalid:
2445: * retry.
1.43 thorpej 2446: */
1.101.2.6 yamt 2447: if (curlwp->l_ncsw != ncsw) {
2448: mutex_exit(&pc->pc_lock);
2449: pool_cache_cpu_exit(cc, s);
2450: return pool_cache_cpu_enter(pc, s);
2451: }
2452: }
1.43 thorpej 2453:
1.101.2.6 yamt 2454: if ((pcg = pc->pc_fullgroups) != NULL) {
2455: /*
2456: * If there's a full group, release our empty
2457: * group back to the cache. Install the full
2458: * group as cc_current and return.
2459: */
2460: if ((cur = cc->cc_current) != NULL) {
2461: KASSERT(cur->pcg_avail == 0);
2462: cur->pcg_next = pc->pc_emptygroups;
2463: pc->pc_emptygroups = cur;
2464: pc->pc_nempty++;
1.101.2.1 yamt 2465: }
1.101.2.8! yamt 2466: KASSERT(pcg->pcg_avail == pcg->pcg_size);
1.101.2.6 yamt 2467: cc->cc_current = pcg;
2468: pc->pc_fullgroups = pcg->pcg_next;
2469: pc->pc_hits++;
2470: pc->pc_nfull--;
2471: mutex_exit(&pc->pc_lock);
2472: return cc;
1.43 thorpej 2473: }
2474:
1.101.2.6 yamt 2475: /*
2476: * Nothing available locally or in cache. Take the slow
2477: * path: fetch a new object from the pool and construct
2478: * it.
2479: */
2480: pc->pc_misses++;
2481: mutex_exit(&pc->pc_lock);
2482: pool_cache_cpu_exit(cc, s);
1.43 thorpej 2483:
1.101.2.6 yamt 2484: object = pool_get(&pc->pc_pool, flags);
2485: *objectp = object;
2486: if (object == NULL)
2487: return NULL;
2488:
2489: if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) {
2490: pool_put(&pc->pc_pool, object);
2491: *objectp = NULL;
2492: return NULL;
1.101.2.1 yamt 2493: }
1.51 thorpej 2494:
1.101.2.6 yamt 2495: KASSERT((((vaddr_t)object + pc->pc_pool.pr_itemoffset) &
2496: (pc->pc_pool.pr_align - 1)) == 0);
2497:
2498: if (pap != NULL) {
2499: #ifdef POOL_VTOPHYS
2500: *pap = POOL_VTOPHYS(object);
2501: #else
2502: *pap = POOL_PADDR_INVALID;
2503: #endif
2504: }
1.51 thorpej 2505:
1.101.2.6 yamt 2506: FREECHECK_OUT(&pc->pc_freecheck, object);
2507: return NULL;
1.43 thorpej 2508: }
2509:
1.101.2.4 yamt 2510: /*
1.101.2.6 yamt 2511: * pool_cache_get{,_paddr}:
1.101.2.4 yamt 2512: *
1.101.2.6 yamt 2513: * Get an object from a pool cache (optionally returning
2514: * the physical address of the object).
1.101.2.4 yamt 2515: */
1.101.2.6 yamt 2516: void *
2517: pool_cache_get_paddr(pool_cache_t pc, int flags, paddr_t *pap)
1.101.2.1 yamt 2518: {
1.101.2.6 yamt 2519: pool_cache_cpu_t *cc;
2520: pcg_t *pcg;
1.101.2.1 yamt 2521: void *object;
1.101.2.6 yamt 2522: int s;
1.101.2.1 yamt 2523:
1.101.2.6 yamt 2524: #ifdef LOCKDEBUG
2525: if (flags & PR_WAITOK)
2526: ASSERT_SLEEPABLE(NULL, "pool_cache_get(PR_WAITOK)");
2527: #endif
1.101.2.4 yamt 2528:
1.101.2.6 yamt 2529: cc = pool_cache_cpu_enter(pc, &s);
2530: do {
2531: /* Try and allocate an object from the current group. */
2532: pcg = cc->cc_current;
2533: if (pcg != NULL && pcg->pcg_avail > 0) {
2534: object = pcg->pcg_objects[--pcg->pcg_avail].pcgo_va;
2535: if (pap != NULL)
2536: *pap = pcg->pcg_objects[pcg->pcg_avail].pcgo_pa;
2537: pcg->pcg_objects[pcg->pcg_avail].pcgo_va = NULL;
1.101.2.8! yamt 2538: KASSERT(pcg->pcg_avail <= pcg->pcg_size);
1.101.2.6 yamt 2539: KASSERT(object != NULL);
2540: cc->cc_hits++;
2541: pool_cache_cpu_exit(cc, &s);
2542: FREECHECK_OUT(&pc->pc_freecheck, object);
2543: return object;
1.101.2.1 yamt 2544: }
1.101.2.4 yamt 2545:
1.101.2.6 yamt 2546: /*
2547: * That failed. If the previous group isn't empty, swap
2548: * it with the current group and allocate from there.
2549: */
2550: pcg = cc->cc_previous;
2551: if (pcg != NULL && pcg->pcg_avail > 0) {
2552: cc->cc_previous = cc->cc_current;
2553: cc->cc_current = pcg;
2554: continue;
2555: }
2556:
2557: /*
2558: * Can't allocate from either group: try the slow path.
2559: * If get_slow() allocated an object for us, or if
2560: * no more objects are available, it will return NULL.
2561: * Otherwise, we need to retry.
2562: */
2563: cc = pool_cache_get_slow(cc, &s, &object, pap, flags);
2564: } while (cc != NULL);
2565:
2566: return object;
1.101.2.1 yamt 2567: }
2568:
1.101.2.6 yamt 2569: #if __GNUC_PREREQ__(3, 0)
2570: __attribute ((noinline))
2571: #endif
2572: pool_cache_cpu_t *
2573: pool_cache_put_slow(pool_cache_cpu_t *cc, int *s, void *object, paddr_t pa)
1.101.2.1 yamt 2574: {
1.101.2.6 yamt 2575: pcg_t *pcg, *cur;
2576: uint64_t ncsw;
2577: pool_cache_t pc;
1.101.2.8! yamt 2578: u_int nobj;
1.101.2.1 yamt 2579:
1.101.2.6 yamt 2580: pc = cc->cc_cache;
2581: cc->cc_misses++;
1.101.2.1 yamt 2582:
1.101.2.6 yamt 2583: /*
2584: * No free slots locally. Try to grab an empty, unused
2585: * group from the cache.
2586: */
2587: if (!mutex_tryenter(&pc->pc_lock)) {
2588: ncsw = curlwp->l_ncsw;
2589: mutex_enter(&pc->pc_lock);
2590: pc->pc_contended++;
1.101.2.1 yamt 2591:
1.101.2.6 yamt 2592: /*
2593: * If we context switched while locking, then
2594: * our view of the per-CPU data is invalid:
2595: * retry.
2596: */
2597: if (curlwp->l_ncsw != ncsw) {
2598: mutex_exit(&pc->pc_lock);
2599: pool_cache_cpu_exit(cc, s);
2600: return pool_cache_cpu_enter(pc, s);
2601: }
2602: }
2603:
2604: if ((pcg = pc->pc_emptygroups) != NULL) {
2605: /*
2606: * If there's a empty group, release our full
2607: * group back to the cache. Install the empty
1.101.2.8! yamt 2608: * group and return.
1.101.2.6 yamt 2609: */
2610: KASSERT(pcg->pcg_avail == 0);
2611: pc->pc_emptygroups = pcg->pcg_next;
1.101.2.8! yamt 2612: if (cc->cc_previous == NULL) {
! 2613: cc->cc_previous = pcg;
! 2614: } else {
! 2615: if ((cur = cc->cc_current) != NULL) {
! 2616: KASSERT(cur->pcg_avail == pcg->pcg_size);
! 2617: cur->pcg_next = pc->pc_fullgroups;
! 2618: pc->pc_fullgroups = cur;
! 2619: pc->pc_nfull++;
! 2620: }
! 2621: cc->cc_current = pcg;
! 2622: }
1.101.2.6 yamt 2623: pc->pc_hits++;
2624: pc->pc_nempty--;
2625: mutex_exit(&pc->pc_lock);
2626: return cc;
2627: }
2628:
2629: /*
2630: * Nothing available locally or in cache. Take the
2631: * slow path and try to allocate a new group that we
2632: * can release to.
2633: */
2634: pc->pc_misses++;
2635: mutex_exit(&pc->pc_lock);
2636: pool_cache_cpu_exit(cc, s);
2637:
2638: /*
2639: * If we can't allocate a new group, just throw the
2640: * object away.
2641: */
1.101.2.8! yamt 2642: nobj = pc->pc_pcgsize;
! 2643: if (pool_cache_disable) {
! 2644: pcg = NULL;
! 2645: } else if (nobj == PCG_NOBJECTS_LARGE) {
! 2646: pcg = pool_get(&pcg_large_pool, PR_NOWAIT);
! 2647: } else {
! 2648: pcg = pool_get(&pcg_normal_pool, PR_NOWAIT);
! 2649: }
1.101.2.6 yamt 2650: if (pcg == NULL) {
2651: pool_cache_destruct_object(pc, object);
2652: return NULL;
2653: }
2654: pcg->pcg_avail = 0;
1.101.2.8! yamt 2655: pcg->pcg_size = nobj;
1.101.2.6 yamt 2656:
2657: /*
2658: * Add the empty group to the cache and try again.
2659: */
2660: mutex_enter(&pc->pc_lock);
2661: pcg->pcg_next = pc->pc_emptygroups;
2662: pc->pc_emptygroups = pcg;
2663: pc->pc_nempty++;
2664: mutex_exit(&pc->pc_lock);
2665:
2666: return pool_cache_cpu_enter(pc, s);
2667: }
1.101.2.1 yamt 2668:
1.43 thorpej 2669: /*
1.101.2.6 yamt 2670: * pool_cache_put{,_paddr}:
1.43 thorpej 2671: *
1.101.2.6 yamt 2672: * Put an object back to the pool cache (optionally caching the
2673: * physical address of the object).
1.43 thorpej 2674: */
1.101 thorpej 2675: void
1.101.2.6 yamt 2676: pool_cache_put_paddr(pool_cache_t pc, void *object, paddr_t pa)
1.43 thorpej 2677: {
1.101.2.6 yamt 2678: pool_cache_cpu_t *cc;
2679: pcg_t *pcg;
2680: int s;
1.101 thorpej 2681:
1.101.2.6 yamt 2682: FREECHECK_IN(&pc->pc_freecheck, object);
1.43 thorpej 2683:
1.101.2.6 yamt 2684: cc = pool_cache_cpu_enter(pc, &s);
2685: do {
2686: /* If the current group isn't full, release it there. */
2687: pcg = cc->cc_current;
1.101.2.8! yamt 2688: if (pcg != NULL && pcg->pcg_avail < pcg->pcg_size) {
1.101.2.6 yamt 2689: pcg->pcg_objects[pcg->pcg_avail].pcgo_va = object;
2690: pcg->pcg_objects[pcg->pcg_avail].pcgo_pa = pa;
2691: pcg->pcg_avail++;
2692: cc->cc_hits++;
2693: pool_cache_cpu_exit(cc, &s);
2694: return;
2695: }
1.43 thorpej 2696:
1.101.2.6 yamt 2697: /*
2698: * That failed. If the previous group is empty, swap
2699: * it with the current group and try again.
2700: */
2701: pcg = cc->cc_previous;
2702: if (pcg != NULL && pcg->pcg_avail == 0) {
2703: cc->cc_previous = cc->cc_current;
2704: cc->cc_current = pcg;
2705: continue;
2706: }
1.43 thorpej 2707:
1.101.2.6 yamt 2708: /*
2709: * Can't free to either group: try the slow path.
2710: * If put_slow() releases the object for us, it
2711: * will return NULL. Otherwise we need to retry.
2712: */
2713: cc = pool_cache_put_slow(cc, &s, object, pa);
2714: } while (cc != NULL);
1.43 thorpej 2715: }
2716:
2717: /*
1.101.2.6 yamt 2718: * pool_cache_xcall:
1.43 thorpej 2719: *
1.101.2.6 yamt 2720: * Transfer objects from the per-CPU cache to the global cache.
2721: * Run within a cross-call thread.
1.43 thorpej 2722: */
2723: static void
1.101.2.6 yamt 2724: pool_cache_xcall(pool_cache_t pc)
1.43 thorpej 2725: {
1.101.2.6 yamt 2726: pool_cache_cpu_t *cc;
2727: pcg_t *prev, *cur, **list;
2728: int s = 0; /* XXXgcc */
2729:
2730: cc = pool_cache_cpu_enter(pc, &s);
2731: cur = cc->cc_current;
2732: cc->cc_current = NULL;
2733: prev = cc->cc_previous;
2734: cc->cc_previous = NULL;
2735: pool_cache_cpu_exit(cc, &s);
2736:
2737: /*
2738: * XXXSMP Go to splvm to prevent kernel_lock from being taken,
2739: * because locks at IPL_SOFTXXX are still spinlocks. Does not
2740: * apply to IPL_SOFTBIO. Cross-call threads do not take the
2741: * kernel_lock.
1.101 thorpej 2742: */
1.101.2.6 yamt 2743: s = splvm();
2744: mutex_enter(&pc->pc_lock);
2745: if (cur != NULL) {
1.101.2.8! yamt 2746: if (cur->pcg_avail == cur->pcg_size) {
1.101.2.6 yamt 2747: list = &pc->pc_fullgroups;
2748: pc->pc_nfull++;
2749: } else if (cur->pcg_avail == 0) {
2750: list = &pc->pc_emptygroups;
2751: pc->pc_nempty++;
2752: } else {
2753: list = &pc->pc_partgroups;
2754: pc->pc_npart++;
2755: }
2756: cur->pcg_next = *list;
2757: *list = cur;
2758: }
2759: if (prev != NULL) {
1.101.2.8! yamt 2760: if (prev->pcg_avail == prev->pcg_size) {
1.101.2.6 yamt 2761: list = &pc->pc_fullgroups;
2762: pc->pc_nfull++;
2763: } else if (prev->pcg_avail == 0) {
2764: list = &pc->pc_emptygroups;
2765: pc->pc_nempty++;
2766: } else {
2767: list = &pc->pc_partgroups;
2768: pc->pc_npart++;
2769: }
2770: prev->pcg_next = *list;
2771: *list = prev;
2772: }
2773: mutex_exit(&pc->pc_lock);
2774: splx(s);
1.3 pk 2775: }
1.66 thorpej 2776:
2777: /*
2778: * Pool backend allocators.
2779: *
2780: * Each pool has a backend allocator that handles allocation, deallocation,
2781: * and any additional draining that might be needed.
2782: *
2783: * We provide two standard allocators:
2784: *
2785: * pool_allocator_kmem - the default when no allocator is specified
2786: *
2787: * pool_allocator_nointr - used for pools that will not be accessed
2788: * in interrupt context.
2789: */
2790: void *pool_page_alloc(struct pool *, int);
2791: void pool_page_free(struct pool *, void *);
2792:
1.101.2.1 yamt 2793: #ifdef POOL_SUBPAGE
2794: struct pool_allocator pool_allocator_kmem_fullpage = {
2795: pool_page_alloc, pool_page_free, 0,
2796: .pa_backingmapptr = &kmem_map,
2797: };
2798: #else
1.66 thorpej 2799: struct pool_allocator pool_allocator_kmem = {
2800: pool_page_alloc, pool_page_free, 0,
1.101.2.1 yamt 2801: .pa_backingmapptr = &kmem_map,
1.66 thorpej 2802: };
1.101.2.1 yamt 2803: #endif
1.66 thorpej 2804:
2805: void *pool_page_alloc_nointr(struct pool *, int);
2806: void pool_page_free_nointr(struct pool *, void *);
2807:
1.101.2.1 yamt 2808: #ifdef POOL_SUBPAGE
2809: struct pool_allocator pool_allocator_nointr_fullpage = {
2810: pool_page_alloc_nointr, pool_page_free_nointr, 0,
2811: .pa_backingmapptr = &kernel_map,
2812: };
2813: #else
1.66 thorpej 2814: struct pool_allocator pool_allocator_nointr = {
2815: pool_page_alloc_nointr, pool_page_free_nointr, 0,
1.101.2.1 yamt 2816: .pa_backingmapptr = &kernel_map,
1.66 thorpej 2817: };
1.101.2.1 yamt 2818: #endif
1.66 thorpej 2819:
2820: #ifdef POOL_SUBPAGE
2821: void *pool_subpage_alloc(struct pool *, int);
2822: void pool_subpage_free(struct pool *, void *);
2823:
1.101.2.1 yamt 2824: struct pool_allocator pool_allocator_kmem = {
2825: pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
2826: .pa_backingmapptr = &kmem_map,
2827: };
2828:
2829: void *pool_subpage_alloc_nointr(struct pool *, int);
2830: void pool_subpage_free_nointr(struct pool *, void *);
2831:
2832: struct pool_allocator pool_allocator_nointr = {
2833: pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
2834: .pa_backingmapptr = &kmem_map,
1.66 thorpej 2835: };
2836: #endif /* POOL_SUBPAGE */
2837:
1.101.2.1 yamt 2838: static void *
2839: pool_allocator_alloc(struct pool *pp, int flags)
1.66 thorpej 2840: {
1.101.2.1 yamt 2841: struct pool_allocator *pa = pp->pr_alloc;
1.66 thorpej 2842: void *res;
2843:
1.101.2.1 yamt 2844: res = (*pa->pa_alloc)(pp, flags);
2845: if (res == NULL && (flags & PR_WAITOK) == 0) {
1.66 thorpej 2846: /*
1.101.2.1 yamt 2847: * We only run the drain hook here if PR_NOWAIT.
2848: * In other cases, the hook will be run in
2849: * pool_reclaim().
1.66 thorpej 2850: */
1.101.2.1 yamt 2851: if (pp->pr_drain_hook != NULL) {
2852: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
2853: res = (*pa->pa_alloc)(pp, flags);
1.66 thorpej 2854: }
1.101.2.1 yamt 2855: }
2856: return res;
1.66 thorpej 2857: }
2858:
1.101.2.1 yamt 2859: static void
1.66 thorpej 2860: pool_allocator_free(struct pool *pp, void *v)
2861: {
2862: struct pool_allocator *pa = pp->pr_alloc;
2863:
2864: (*pa->pa_free)(pp, v);
2865: }
2866:
2867: void *
2868: pool_page_alloc(struct pool *pp, int flags)
2869: {
1.101.2.3 yamt 2870: bool waitok = (flags & PR_WAITOK) ? true : false;
1.66 thorpej 2871:
1.100 yamt 2872: return ((void *) uvm_km_alloc_poolpage_cache(kmem_map, waitok));
1.66 thorpej 2873: }
2874:
2875: void
2876: pool_page_free(struct pool *pp, void *v)
2877: {
2878:
1.98 yamt 2879: uvm_km_free_poolpage_cache(kmem_map, (vaddr_t) v);
2880: }
2881:
2882: static void *
2883: pool_page_alloc_meta(struct pool *pp, int flags)
2884: {
1.101.2.3 yamt 2885: bool waitok = (flags & PR_WAITOK) ? true : false;
1.98 yamt 2886:
1.100 yamt 2887: return ((void *) uvm_km_alloc_poolpage(kmem_map, waitok));
1.98 yamt 2888: }
2889:
2890: static void
2891: pool_page_free_meta(struct pool *pp, void *v)
2892: {
2893:
1.100 yamt 2894: uvm_km_free_poolpage(kmem_map, (vaddr_t) v);
1.66 thorpej 2895: }
2896:
2897: #ifdef POOL_SUBPAGE
2898: /* Sub-page allocator, for machines with large hardware pages. */
2899: void *
2900: pool_subpage_alloc(struct pool *pp, int flags)
2901: {
1.101.2.6 yamt 2902: return pool_get(&psppool, flags);
1.66 thorpej 2903: }
2904:
2905: void
2906: pool_subpage_free(struct pool *pp, void *v)
2907: {
2908: pool_put(&psppool, v);
2909: }
2910:
2911: /* We don't provide a real nointr allocator. Maybe later. */
2912: void *
1.101.2.1 yamt 2913: pool_subpage_alloc_nointr(struct pool *pp, int flags)
1.66 thorpej 2914: {
2915:
2916: return (pool_subpage_alloc(pp, flags));
2917: }
2918:
2919: void
1.101.2.1 yamt 2920: pool_subpage_free_nointr(struct pool *pp, void *v)
1.66 thorpej 2921: {
2922:
2923: pool_subpage_free(pp, v);
2924: }
1.101.2.1 yamt 2925: #endif /* POOL_SUBPAGE */
1.66 thorpej 2926: void *
2927: pool_page_alloc_nointr(struct pool *pp, int flags)
2928: {
1.101.2.3 yamt 2929: bool waitok = (flags & PR_WAITOK) ? true : false;
1.66 thorpej 2930:
1.100 yamt 2931: return ((void *) uvm_km_alloc_poolpage_cache(kernel_map, waitok));
1.66 thorpej 2932: }
2933:
2934: void
2935: pool_page_free_nointr(struct pool *pp, void *v)
2936: {
2937:
1.98 yamt 2938: uvm_km_free_poolpage_cache(kernel_map, (vaddr_t) v);
1.66 thorpej 2939: }
1.101.2.8! yamt 2940:
! 2941: #if defined(DDB)
! 2942: static bool
! 2943: pool_in_page(struct pool *pp, struct pool_item_header *ph, uintptr_t addr)
! 2944: {
! 2945:
! 2946: return (uintptr_t)ph->ph_page <= addr &&
! 2947: addr < (uintptr_t)ph->ph_page + pp->pr_alloc->pa_pagesz;
! 2948: }
! 2949:
! 2950: static bool
! 2951: pool_in_item(struct pool *pp, void *item, uintptr_t addr)
! 2952: {
! 2953:
! 2954: return (uintptr_t)item <= addr && addr < (uintptr_t)item + pp->pr_size;
! 2955: }
! 2956:
! 2957: static bool
! 2958: pool_in_cg(struct pool *pp, struct pool_cache_group *pcg, uintptr_t addr)
! 2959: {
! 2960: int i;
! 2961:
! 2962: if (pcg == NULL) {
! 2963: return false;
! 2964: }
! 2965: for (i = 0; i < pcg->pcg_avail; i++) {
! 2966: if (pool_in_item(pp, pcg->pcg_objects[i].pcgo_va, addr)) {
! 2967: return true;
! 2968: }
! 2969: }
! 2970: return false;
! 2971: }
! 2972:
! 2973: static bool
! 2974: pool_allocated(struct pool *pp, struct pool_item_header *ph, uintptr_t addr)
! 2975: {
! 2976:
! 2977: if ((pp->pr_roflags & PR_NOTOUCH) != 0) {
! 2978: unsigned int idx = pr_item_notouch_index(pp, ph, (void *)addr);
! 2979: pool_item_bitmap_t *bitmap =
! 2980: ph->ph_bitmap + (idx / BITMAP_SIZE);
! 2981: pool_item_bitmap_t mask = 1 << (idx & BITMAP_MASK);
! 2982:
! 2983: return (*bitmap & mask) == 0;
! 2984: } else {
! 2985: struct pool_item *pi;
! 2986:
! 2987: LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
! 2988: if (pool_in_item(pp, pi, addr)) {
! 2989: return false;
! 2990: }
! 2991: }
! 2992: return true;
! 2993: }
! 2994: }
! 2995:
! 2996: void
! 2997: pool_whatis(uintptr_t addr, void (*pr)(const char *, ...))
! 2998: {
! 2999: struct pool *pp;
! 3000:
! 3001: TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
! 3002: struct pool_item_header *ph;
! 3003: uintptr_t item;
! 3004: bool allocated = true;
! 3005: bool incache = false;
! 3006: bool incpucache = false;
! 3007: char cpucachestr[32];
! 3008:
! 3009: if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
! 3010: LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
! 3011: if (pool_in_page(pp, ph, addr)) {
! 3012: goto found;
! 3013: }
! 3014: }
! 3015: LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
! 3016: if (pool_in_page(pp, ph, addr)) {
! 3017: allocated =
! 3018: pool_allocated(pp, ph, addr);
! 3019: goto found;
! 3020: }
! 3021: }
! 3022: LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
! 3023: if (pool_in_page(pp, ph, addr)) {
! 3024: allocated = false;
! 3025: goto found;
! 3026: }
! 3027: }
! 3028: continue;
! 3029: } else {
! 3030: ph = pr_find_pagehead_noalign(pp, (void *)addr);
! 3031: if (ph == NULL || !pool_in_page(pp, ph, addr)) {
! 3032: continue;
! 3033: }
! 3034: allocated = pool_allocated(pp, ph, addr);
! 3035: }
! 3036: found:
! 3037: if (allocated && pp->pr_cache) {
! 3038: pool_cache_t pc = pp->pr_cache;
! 3039: struct pool_cache_group *pcg;
! 3040: int i;
! 3041:
! 3042: for (pcg = pc->pc_fullgroups; pcg != NULL;
! 3043: pcg = pcg->pcg_next) {
! 3044: if (pool_in_cg(pp, pcg, addr)) {
! 3045: incache = true;
! 3046: goto print;
! 3047: }
! 3048: }
! 3049: for (i = 0; i < MAXCPUS; i++) {
! 3050: pool_cache_cpu_t *cc;
! 3051:
! 3052: if ((cc = pc->pc_cpus[i]) == NULL) {
! 3053: continue;
! 3054: }
! 3055: if (pool_in_cg(pp, cc->cc_current, addr) ||
! 3056: pool_in_cg(pp, cc->cc_previous, addr)) {
! 3057: struct cpu_info *ci =
! 3058: cpu_lookup_byindex(i);
! 3059:
! 3060: incpucache = true;
! 3061: snprintf(cpucachestr,
! 3062: sizeof(cpucachestr),
! 3063: "cached by CPU %u",
! 3064: (u_int)ci->ci_cpuid);
! 3065: goto print;
! 3066: }
! 3067: }
! 3068: }
! 3069: print:
! 3070: item = (uintptr_t)ph->ph_page + ph->ph_off;
! 3071: item = item + rounddown(addr - item, pp->pr_size);
! 3072: (*pr)("%p is %p+%zu in POOL '%s' (%s)\n",
! 3073: (void *)addr, item, (size_t)(addr - item),
! 3074: pp->pr_wchan,
! 3075: incpucache ? cpucachestr :
! 3076: incache ? "cached" : allocated ? "allocated" : "free");
! 3077: }
! 3078: }
! 3079: #endif /* defined(DDB) */
CVSweb <webmaster@jp.NetBSD.org>