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