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