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