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