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