Annotation of src/sys/kern/subr_pool.c, Revision 1.214
1.214 ! christos 1: /* $NetBSD: subr_pool.c,v 1.213 2017/11/09 15:53:40 christos 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.214 ! christos 36: __KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.213 2017/11/09 15:53:40 christos 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.207 riastrad 754: KASSERTMSG((pp->pr_itemsperpage != 0),
1.213 christos 755: "%s: [%s] pr_itemsperpage is zero, "
756: "pool not initialized?", __func__, pp->pr_wchan);
1.207 riastrad 757: KASSERTMSG((!(cpu_intr_p() || cpu_softintr_p())
758: || pp->pr_ipl != IPL_NONE || cold || panicstr != NULL),
1.213 christos 759: "%s: [%s] is IPL_NONE, but called from interrupt context",
760: __func__, pp->pr_wchan);
1.155 ad 761: if (flags & PR_WAITOK) {
1.154 yamt 762: ASSERT_SLEEPABLE();
1.155 ad 763: }
1.1 pk 764:
1.134 ad 765: mutex_enter(&pp->pr_lock);
1.20 thorpej 766: startover:
767: /*
768: * Check to see if we've reached the hard limit. If we have,
769: * and we can wait, then wait until an item has been returned to
770: * the pool.
771: */
1.207 riastrad 772: KASSERTMSG((pp->pr_nout <= pp->pr_hardlimit),
1.213 christos 773: "%s: %s: crossed hard limit", __func__, pp->pr_wchan);
1.34 thorpej 774: if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
1.68 thorpej 775: if (pp->pr_drain_hook != NULL) {
776: /*
777: * Since the drain hook is going to free things
778: * back to the pool, unlock, call the hook, re-lock,
779: * and check the hardlimit condition again.
780: */
1.134 ad 781: mutex_exit(&pp->pr_lock);
1.68 thorpej 782: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
1.134 ad 783: mutex_enter(&pp->pr_lock);
1.68 thorpej 784: if (pp->pr_nout < pp->pr_hardlimit)
785: goto startover;
786: }
787:
1.29 sommerfe 788: if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
1.20 thorpej 789: /*
790: * XXX: A warning isn't logged in this case. Should
791: * it be?
792: */
793: pp->pr_flags |= PR_WANTED;
1.212 christos 794: do {
795: cv_wait(&pp->pr_cv, &pp->pr_lock);
796: } while (pp->pr_flags & PR_WANTED);
1.20 thorpej 797: goto startover;
798: }
1.31 thorpej 799:
800: /*
801: * Log a message that the hard limit has been hit.
802: */
803: if (pp->pr_hardlimit_warning != NULL &&
804: ratecheck(&pp->pr_hardlimit_warning_last,
805: &pp->pr_hardlimit_ratecap))
806: log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1.21 thorpej 807:
808: pp->pr_nfail++;
809:
1.134 ad 810: mutex_exit(&pp->pr_lock);
1.211 riastrad 811: KASSERT((flags & (PR_WAITOK|PR_NOWAIT)) == PR_NOWAIT);
1.20 thorpej 812: return (NULL);
813: }
814:
1.3 pk 815: /*
816: * The convention we use is that if `curpage' is not NULL, then
817: * it points at a non-empty bucket. In particular, `curpage'
818: * never points at a page header which has PR_PHINPAGE set and
819: * has no items in its bucket.
820: */
1.20 thorpej 821: if ((ph = pp->pr_curpage) == NULL) {
1.113 yamt 822: int error;
823:
1.207 riastrad 824: KASSERTMSG((pp->pr_nitems == 0),
1.213 christos 825: "%s: [%s] curpage NULL, inconsistent nitems %u",
826: __func__, pp->pr_wchan, pp->pr_nitems);
1.20 thorpej 827:
1.21 thorpej 828: /*
829: * Call the back-end page allocator for more memory.
830: * Release the pool lock, as the back-end page allocator
831: * may block.
832: */
1.113 yamt 833: error = pool_grow(pp, flags);
834: if (error != 0) {
1.21 thorpej 835: /*
1.210 mlelstv 836: * pool_grow aborts when another thread
837: * is allocating a new page. Retry if it
838: * waited for it.
839: */
840: if (error == ERESTART)
841: goto startover;
842:
843: /*
1.55 thorpej 844: * We were unable to allocate a page or item
845: * header, but we released the lock during
846: * allocation, so perhaps items were freed
847: * back to the pool. Check for this case.
1.21 thorpej 848: */
849: if (pp->pr_curpage != NULL)
850: goto startover;
1.15 pk 851:
1.117 yamt 852: pp->pr_nfail++;
1.134 ad 853: mutex_exit(&pp->pr_lock);
1.211 riastrad 854: KASSERT((flags & (PR_WAITOK|PR_NOWAIT)) == PR_NOWAIT);
1.117 yamt 855: return (NULL);
1.1 pk 856: }
1.3 pk 857:
1.20 thorpej 858: /* Start the allocation process over. */
859: goto startover;
1.3 pk 860: }
1.97 yamt 861: if (pp->pr_roflags & PR_NOTOUCH) {
1.207 riastrad 862: KASSERTMSG((ph->ph_nmissing < pp->pr_itemsperpage),
1.213 christos 863: "%s: %s: page empty", __func__, pp->pr_wchan);
1.97 yamt 864: v = pr_item_notouch_get(pp, ph);
865: } else {
1.102 chs 866: v = pi = LIST_FIRST(&ph->ph_itemlist);
1.97 yamt 867: if (__predict_false(v == NULL)) {
1.134 ad 868: mutex_exit(&pp->pr_lock);
1.213 christos 869: panic("%s: [%s] page empty", __func__, pp->pr_wchan);
1.97 yamt 870: }
1.207 riastrad 871: KASSERTMSG((pp->pr_nitems > 0),
1.213 christos 872: "%s: [%s] nitems %u inconsistent on itemlist",
873: __func__, pp->pr_wchan, pp->pr_nitems);
1.207 riastrad 874: KASSERTMSG((pi->pi_magic == PI_MAGIC),
1.213 christos 875: "%s: [%s] free list modified: "
876: "magic=%x; page %p; item addr %p", __func__,
1.207 riastrad 877: pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
1.3 pk 878:
1.97 yamt 879: /*
880: * Remove from item list.
881: */
1.102 chs 882: LIST_REMOVE(pi, pi_list);
1.97 yamt 883: }
1.20 thorpej 884: pp->pr_nitems--;
885: pp->pr_nout++;
1.6 thorpej 886: if (ph->ph_nmissing == 0) {
1.207 riastrad 887: KASSERT(pp->pr_nidle > 0);
1.6 thorpej 888: pp->pr_nidle--;
1.88 chs 889:
890: /*
891: * This page was previously empty. Move it to the list of
892: * partially-full pages. This page is already curpage.
893: */
894: LIST_REMOVE(ph, ph_pagelist);
895: LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.6 thorpej 896: }
1.3 pk 897: ph->ph_nmissing++;
1.97 yamt 898: if (ph->ph_nmissing == pp->pr_itemsperpage) {
1.207 riastrad 899: KASSERTMSG(((pp->pr_roflags & PR_NOTOUCH) ||
900: LIST_EMPTY(&ph->ph_itemlist)),
1.213 christos 901: "%s: [%s] nmissing (%u) inconsistent", __func__,
902: pp->pr_wchan, ph->ph_nmissing);
1.3 pk 903: /*
1.88 chs 904: * This page is now full. Move it to the full list
905: * and select a new current page.
1.3 pk 906: */
1.88 chs 907: LIST_REMOVE(ph, ph_pagelist);
908: LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
909: pool_update_curpage(pp);
1.1 pk 910: }
1.3 pk 911:
912: pp->pr_nget++;
1.20 thorpej 913:
914: /*
915: * If we have a low water mark and we are now below that low
916: * water mark, add more items to the pool.
917: */
1.53 thorpej 918: if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20 thorpej 919: /*
920: * XXX: Should we log a warning? Should we set up a timeout
921: * to try again in a second or so? The latter could break
922: * a caller's assumptions about interrupt protection, etc.
923: */
924: }
925:
1.134 ad 926: mutex_exit(&pp->pr_lock);
1.125 ad 927: KASSERT((((vaddr_t)v + pp->pr_itemoffset) & (pp->pr_align - 1)) == 0);
928: FREECHECK_OUT(&pp->pr_freecheck, v);
1.204 maxv 929: pool_redzone_fill(pp, v);
1.1 pk 930: return (v);
931: }
932:
933: /*
1.43 thorpej 934: * Internal version of pool_put(). Pool is already locked/entered.
1.1 pk 935: */
1.43 thorpej 936: static void
1.101 thorpej 937: pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq)
1.1 pk 938: {
939: struct pool_item *pi = v;
1.3 pk 940: struct pool_item_header *ph;
941:
1.134 ad 942: KASSERT(mutex_owned(&pp->pr_lock));
1.204 maxv 943: pool_redzone_check(pp, v);
1.125 ad 944: FREECHECK_IN(&pp->pr_freecheck, v);
1.134 ad 945: LOCKDEBUG_MEM_CHECK(v, pp->pr_size);
1.61 chs 946:
1.207 riastrad 947: KASSERTMSG((pp->pr_nout > 0),
1.213 christos 948: "%s: [%s] putting with none out", __func__, pp->pr_wchan);
1.3 pk 949:
1.121 yamt 950: if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) {
1.213 christos 951: panic("%s: [%s] page header missing", __func__, pp->pr_wchan);
1.3 pk 952: }
1.28 thorpej 953:
1.3 pk 954: /*
955: * Return to item list.
956: */
1.97 yamt 957: if (pp->pr_roflags & PR_NOTOUCH) {
958: pr_item_notouch_put(pp, ph, v);
959: } else {
1.2 pk 960: #ifdef DIAGNOSTIC
1.97 yamt 961: pi->pi_magic = PI_MAGIC;
1.3 pk 962: #endif
1.32 chs 963: #ifdef DEBUG
1.97 yamt 964: {
965: int i, *ip = v;
1.32 chs 966:
1.97 yamt 967: for (i = 0; i < pp->pr_size / sizeof(int); i++) {
968: *ip++ = PI_MAGIC;
969: }
1.32 chs 970: }
971: #endif
972:
1.102 chs 973: LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.97 yamt 974: }
1.79 thorpej 975: KDASSERT(ph->ph_nmissing != 0);
1.3 pk 976: ph->ph_nmissing--;
977: pp->pr_nput++;
1.20 thorpej 978: pp->pr_nitems++;
979: pp->pr_nout--;
1.3 pk 980:
981: /* Cancel "pool empty" condition if it exists */
982: if (pp->pr_curpage == NULL)
983: pp->pr_curpage = ph;
984:
985: if (pp->pr_flags & PR_WANTED) {
986: pp->pr_flags &= ~PR_WANTED;
1.134 ad 987: cv_broadcast(&pp->pr_cv);
1.3 pk 988: }
989:
990: /*
1.88 chs 991: * If this page is now empty, do one of two things:
1.21 thorpej 992: *
1.88 chs 993: * (1) If we have more pages than the page high water mark,
1.96 thorpej 994: * free the page back to the system. ONLY CONSIDER
1.90 thorpej 995: * FREEING BACK A PAGE IF WE HAVE MORE THAN OUR MINIMUM PAGE
996: * CLAIM.
1.21 thorpej 997: *
1.88 chs 998: * (2) Otherwise, move the page to the empty page list.
999: *
1000: * Either way, select a new current page (so we use a partially-full
1001: * page if one is available).
1.3 pk 1002: */
1003: if (ph->ph_nmissing == 0) {
1.6 thorpej 1004: pp->pr_nidle++;
1.90 thorpej 1005: if (pp->pr_npages > pp->pr_minpages &&
1.152 yamt 1006: pp->pr_npages > pp->pr_maxpages) {
1.101 thorpej 1007: pr_rmpage(pp, ph, pq);
1.3 pk 1008: } else {
1.88 chs 1009: LIST_REMOVE(ph, ph_pagelist);
1010: LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.3 pk 1011:
1.21 thorpej 1012: /*
1013: * Update the timestamp on the page. A page must
1014: * be idle for some period of time before it can
1015: * be reclaimed by the pagedaemon. This minimizes
1016: * ping-pong'ing for memory.
1.151 yamt 1017: *
1018: * note for 64-bit time_t: truncating to 32-bit is not
1019: * a problem for our usage.
1.21 thorpej 1020: */
1.151 yamt 1021: ph->ph_time = time_uptime;
1.1 pk 1022: }
1.88 chs 1023: pool_update_curpage(pp);
1.1 pk 1024: }
1.88 chs 1025:
1.21 thorpej 1026: /*
1.88 chs 1027: * If the page was previously completely full, move it to the
1028: * partially-full list and make it the current page. The next
1029: * allocation will get the item from this page, instead of
1030: * further fragmenting the pool.
1.21 thorpej 1031: */
1032: else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
1.88 chs 1033: LIST_REMOVE(ph, ph_pagelist);
1034: LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.21 thorpej 1035: pp->pr_curpage = ph;
1036: }
1.43 thorpej 1037: }
1038:
1.56 sommerfe 1039: void
1040: pool_put(struct pool *pp, void *v)
1041: {
1.101 thorpej 1042: struct pool_pagelist pq;
1043:
1044: LIST_INIT(&pq);
1.56 sommerfe 1045:
1.134 ad 1046: mutex_enter(&pp->pr_lock);
1.101 thorpej 1047: pool_do_put(pp, v, &pq);
1.134 ad 1048: mutex_exit(&pp->pr_lock);
1.56 sommerfe 1049:
1.102 chs 1050: pr_pagelist_free(pp, &pq);
1.56 sommerfe 1051: }
1.57 sommerfe 1052:
1.74 thorpej 1053: /*
1.113 yamt 1054: * pool_grow: grow a pool by a page.
1055: *
1056: * => called with pool locked.
1057: * => unlock and relock the pool.
1058: * => return with pool locked.
1059: */
1060:
1061: static int
1062: pool_grow(struct pool *pp, int flags)
1063: {
1064: struct pool_item_header *ph = NULL;
1065: char *cp;
1.209 riastrad 1066: int error;
1067:
1068: /*
1069: * If there's a pool_grow in progress, wait for it to complete
1070: * and try again from the top.
1071: */
1072: if (pp->pr_flags & PR_GROWING) {
1073: if (flags & PR_WAITOK) {
1074: do {
1075: cv_wait(&pp->pr_cv, &pp->pr_lock);
1076: } while (pp->pr_flags & PR_GROWING);
1077: return ERESTART;
1078: } else {
1079: return EWOULDBLOCK;
1080: }
1081: }
1082: pp->pr_flags |= PR_GROWING;
1.113 yamt 1083:
1.134 ad 1084: mutex_exit(&pp->pr_lock);
1.113 yamt 1085: cp = pool_allocator_alloc(pp, flags);
1086: if (__predict_true(cp != NULL)) {
1087: ph = pool_alloc_item_header(pp, cp, flags);
1088: }
1089: if (__predict_false(cp == NULL || ph == NULL)) {
1090: if (cp != NULL) {
1091: pool_allocator_free(pp, cp);
1092: }
1.134 ad 1093: mutex_enter(&pp->pr_lock);
1.209 riastrad 1094: error = ENOMEM;
1095: goto out;
1.113 yamt 1096: }
1097:
1.134 ad 1098: mutex_enter(&pp->pr_lock);
1.113 yamt 1099: pool_prime_page(pp, cp, ph);
1100: pp->pr_npagealloc++;
1.209 riastrad 1101: error = 0;
1102:
1103: out:
1104: /*
1105: * If anyone was waiting for pool_grow, notify them that we
1106: * may have just done it.
1107: */
1108: KASSERT(pp->pr_flags & PR_GROWING);
1109: pp->pr_flags &= ~PR_GROWING;
1110: cv_broadcast(&pp->pr_cv);
1111:
1112: return error;
1.113 yamt 1113: }
1114:
1115: /*
1.74 thorpej 1116: * Add N items to the pool.
1117: */
1118: int
1119: pool_prime(struct pool *pp, int n)
1120: {
1.75 simonb 1121: int newpages;
1.113 yamt 1122: int error = 0;
1.74 thorpej 1123:
1.134 ad 1124: mutex_enter(&pp->pr_lock);
1.74 thorpej 1125:
1126: newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1127:
1128: while (newpages-- > 0) {
1.113 yamt 1129: error = pool_grow(pp, PR_NOWAIT);
1130: if (error) {
1.214 ! christos 1131: if (error == ERESTART)
! 1132: continue;
1.74 thorpej 1133: break;
1134: }
1135: pp->pr_minpages++;
1136: }
1137:
1138: if (pp->pr_minpages >= pp->pr_maxpages)
1139: pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */
1140:
1.134 ad 1141: mutex_exit(&pp->pr_lock);
1.113 yamt 1142: return error;
1.74 thorpej 1143: }
1.55 thorpej 1144:
1145: /*
1.3 pk 1146: * Add a page worth of items to the pool.
1.21 thorpej 1147: *
1148: * Note, we must be called with the pool descriptor LOCKED.
1.3 pk 1149: */
1.55 thorpej 1150: static void
1.128 christos 1151: pool_prime_page(struct pool *pp, void *storage, struct pool_item_header *ph)
1.3 pk 1152: {
1153: struct pool_item *pi;
1.128 christos 1154: void *cp = storage;
1.125 ad 1155: const unsigned int align = pp->pr_align;
1156: const unsigned int ioff = pp->pr_itemoffset;
1.55 thorpej 1157: int n;
1.36 pk 1158:
1.134 ad 1159: KASSERT(mutex_owned(&pp->pr_lock));
1.207 riastrad 1160: KASSERTMSG(((pp->pr_roflags & PR_NOALIGN) ||
1161: (((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) == 0)),
1.213 christos 1162: "%s: [%s] unaligned page: %p", __func__, pp->pr_wchan, cp);
1.3 pk 1163:
1164: /*
1165: * Insert page header.
1166: */
1.88 chs 1167: LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.102 chs 1168: LIST_INIT(&ph->ph_itemlist);
1.3 pk 1169: ph->ph_page = storage;
1170: ph->ph_nmissing = 0;
1.151 yamt 1171: ph->ph_time = time_uptime;
1.88 chs 1172: if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1173: SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
1.3 pk 1174:
1.6 thorpej 1175: pp->pr_nidle++;
1176:
1.3 pk 1177: /*
1178: * Color this page.
1179: */
1.141 yamt 1180: ph->ph_off = pp->pr_curcolor;
1181: cp = (char *)cp + ph->ph_off;
1.3 pk 1182: if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
1183: pp->pr_curcolor = 0;
1184:
1185: /*
1186: * Adjust storage to apply aligment to `pr_itemoffset' in each item.
1187: */
1188: if (ioff != 0)
1.128 christos 1189: cp = (char *)cp + align - ioff;
1.3 pk 1190:
1.125 ad 1191: KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1192:
1.3 pk 1193: /*
1194: * Insert remaining chunks on the bucket list.
1195: */
1196: n = pp->pr_itemsperpage;
1.20 thorpej 1197: pp->pr_nitems += n;
1.3 pk 1198:
1.97 yamt 1199: if (pp->pr_roflags & PR_NOTOUCH) {
1.141 yamt 1200: pr_item_notouch_init(pp, ph);
1.97 yamt 1201: } else {
1202: while (n--) {
1203: pi = (struct pool_item *)cp;
1.78 thorpej 1204:
1.97 yamt 1205: KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
1.3 pk 1206:
1.97 yamt 1207: /* Insert on page list */
1.102 chs 1208: LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.3 pk 1209: #ifdef DIAGNOSTIC
1.97 yamt 1210: pi->pi_magic = PI_MAGIC;
1.3 pk 1211: #endif
1.128 christos 1212: cp = (char *)cp + pp->pr_size;
1.125 ad 1213:
1214: KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1.97 yamt 1215: }
1.3 pk 1216: }
1217:
1218: /*
1219: * If the pool was depleted, point at the new page.
1220: */
1221: if (pp->pr_curpage == NULL)
1222: pp->pr_curpage = ph;
1223:
1224: if (++pp->pr_npages > pp->pr_hiwat)
1225: pp->pr_hiwat = pp->pr_npages;
1226: }
1227:
1.20 thorpej 1228: /*
1.52 thorpej 1229: * Used by pool_get() when nitems drops below the low water mark. This
1.88 chs 1230: * is used to catch up pr_nitems with the low water mark.
1.20 thorpej 1231: *
1.21 thorpej 1232: * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20 thorpej 1233: *
1.73 thorpej 1234: * Note 2, we must be called with the pool already locked, and we return
1.20 thorpej 1235: * with it locked.
1236: */
1237: static int
1.42 thorpej 1238: pool_catchup(struct pool *pp)
1.20 thorpej 1239: {
1240: int error = 0;
1241:
1.54 thorpej 1242: while (POOL_NEEDS_CATCHUP(pp)) {
1.113 yamt 1243: error = pool_grow(pp, PR_NOWAIT);
1244: if (error) {
1.214 ! christos 1245: if (error == ERESTART)
! 1246: continue;
1.20 thorpej 1247: break;
1248: }
1249: }
1.113 yamt 1250: return error;
1.20 thorpej 1251: }
1252:
1.88 chs 1253: static void
1254: pool_update_curpage(struct pool *pp)
1255: {
1256:
1257: pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
1258: if (pp->pr_curpage == NULL) {
1259: pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
1260: }
1.168 yamt 1261: KASSERT((pp->pr_curpage == NULL && pp->pr_nitems == 0) ||
1262: (pp->pr_curpage != NULL && pp->pr_nitems > 0));
1.88 chs 1263: }
1264:
1.3 pk 1265: void
1.42 thorpej 1266: pool_setlowat(struct pool *pp, int n)
1.3 pk 1267: {
1.15 pk 1268:
1.134 ad 1269: mutex_enter(&pp->pr_lock);
1.21 thorpej 1270:
1.3 pk 1271: pp->pr_minitems = n;
1.15 pk 1272: pp->pr_minpages = (n == 0)
1273: ? 0
1.18 thorpej 1274: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20 thorpej 1275:
1276: /* Make sure we're caught up with the newly-set low water mark. */
1.75 simonb 1277: if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20 thorpej 1278: /*
1279: * XXX: Should we log a warning? Should we set up a timeout
1280: * to try again in a second or so? The latter could break
1281: * a caller's assumptions about interrupt protection, etc.
1282: */
1283: }
1.21 thorpej 1284:
1.134 ad 1285: mutex_exit(&pp->pr_lock);
1.3 pk 1286: }
1287:
1288: void
1.42 thorpej 1289: pool_sethiwat(struct pool *pp, int n)
1.3 pk 1290: {
1.15 pk 1291:
1.134 ad 1292: mutex_enter(&pp->pr_lock);
1.21 thorpej 1293:
1.15 pk 1294: pp->pr_maxpages = (n == 0)
1295: ? 0
1.18 thorpej 1296: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1297:
1.134 ad 1298: mutex_exit(&pp->pr_lock);
1.3 pk 1299: }
1300:
1.20 thorpej 1301: void
1.42 thorpej 1302: pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1.20 thorpej 1303: {
1304:
1.134 ad 1305: mutex_enter(&pp->pr_lock);
1.20 thorpej 1306:
1307: pp->pr_hardlimit = n;
1308: pp->pr_hardlimit_warning = warnmess;
1.31 thorpej 1309: pp->pr_hardlimit_ratecap.tv_sec = ratecap;
1310: pp->pr_hardlimit_warning_last.tv_sec = 0;
1311: pp->pr_hardlimit_warning_last.tv_usec = 0;
1.20 thorpej 1312:
1313: /*
1.21 thorpej 1314: * In-line version of pool_sethiwat(), because we don't want to
1315: * release the lock.
1.20 thorpej 1316: */
1317: pp->pr_maxpages = (n == 0)
1318: ? 0
1319: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1320:
1.134 ad 1321: mutex_exit(&pp->pr_lock);
1.20 thorpej 1322: }
1.3 pk 1323:
1324: /*
1325: * Release all complete pages that have not been used recently.
1.184 rmind 1326: *
1.197 jym 1327: * Must not be called from interrupt context.
1.3 pk 1328: */
1.66 thorpej 1329: int
1.56 sommerfe 1330: pool_reclaim(struct pool *pp)
1.3 pk 1331: {
1332: struct pool_item_header *ph, *phnext;
1.61 chs 1333: struct pool_pagelist pq;
1.151 yamt 1334: uint32_t curtime;
1.134 ad 1335: bool klock;
1336: int rv;
1.3 pk 1337:
1.197 jym 1338: KASSERT(!cpu_intr_p() && !cpu_softintr_p());
1.184 rmind 1339:
1.68 thorpej 1340: if (pp->pr_drain_hook != NULL) {
1341: /*
1342: * The drain hook must be called with the pool unlocked.
1343: */
1344: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT);
1345: }
1346:
1.134 ad 1347: /*
1.157 ad 1348: * XXXSMP Because we do not want to cause non-MPSAFE code
1349: * to block.
1.134 ad 1350: */
1351: if (pp->pr_ipl == IPL_SOFTNET || pp->pr_ipl == IPL_SOFTCLOCK ||
1352: pp->pr_ipl == IPL_SOFTSERIAL) {
1353: KERNEL_LOCK(1, NULL);
1354: klock = true;
1355: } else
1356: klock = false;
1357:
1358: /* Reclaim items from the pool's cache (if any). */
1359: if (pp->pr_cache != NULL)
1360: pool_cache_invalidate(pp->pr_cache);
1361:
1362: if (mutex_tryenter(&pp->pr_lock) == 0) {
1363: if (klock) {
1364: KERNEL_UNLOCK_ONE(NULL);
1365: }
1.66 thorpej 1366: return (0);
1.134 ad 1367: }
1.68 thorpej 1368:
1.88 chs 1369: LIST_INIT(&pq);
1.43 thorpej 1370:
1.151 yamt 1371: curtime = time_uptime;
1.21 thorpej 1372:
1.88 chs 1373: for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
1374: phnext = LIST_NEXT(ph, ph_pagelist);
1.3 pk 1375:
1376: /* Check our minimum page claim */
1377: if (pp->pr_npages <= pp->pr_minpages)
1378: break;
1379:
1.88 chs 1380: KASSERT(ph->ph_nmissing == 0);
1.191 para 1381: if (curtime - ph->ph_time < pool_inactive_time)
1.88 chs 1382: continue;
1.21 thorpej 1383:
1.88 chs 1384: /*
1385: * If freeing this page would put us below
1386: * the low water mark, stop now.
1387: */
1388: if ((pp->pr_nitems - pp->pr_itemsperpage) <
1389: pp->pr_minitems)
1390: break;
1.21 thorpej 1391:
1.88 chs 1392: pr_rmpage(pp, ph, &pq);
1.3 pk 1393: }
1394:
1.134 ad 1395: mutex_exit(&pp->pr_lock);
1396:
1397: if (LIST_EMPTY(&pq))
1398: rv = 0;
1399: else {
1400: pr_pagelist_free(pp, &pq);
1401: rv = 1;
1402: }
1403:
1404: if (klock) {
1405: KERNEL_UNLOCK_ONE(NULL);
1406: }
1.66 thorpej 1407:
1.134 ad 1408: return (rv);
1.3 pk 1409: }
1410:
1411: /*
1.197 jym 1412: * Drain pools, one at a time. The drained pool is returned within ppp.
1.131 ad 1413: *
1.134 ad 1414: * Note, must never be called from interrupt context.
1.3 pk 1415: */
1.197 jym 1416: bool
1417: pool_drain(struct pool **ppp)
1.3 pk 1418: {
1.197 jym 1419: bool reclaimed;
1.3 pk 1420: struct pool *pp;
1.134 ad 1421:
1.145 ad 1422: KASSERT(!TAILQ_EMPTY(&pool_head));
1.3 pk 1423:
1.61 chs 1424: pp = NULL;
1.134 ad 1425:
1426: /* Find next pool to drain, and add a reference. */
1427: mutex_enter(&pool_head_lock);
1428: do {
1429: if (drainpp == NULL) {
1.145 ad 1430: drainpp = TAILQ_FIRST(&pool_head);
1.134 ad 1431: }
1432: if (drainpp != NULL) {
1433: pp = drainpp;
1.145 ad 1434: drainpp = TAILQ_NEXT(pp, pr_poollist);
1.134 ad 1435: }
1436: /*
1437: * Skip completely idle pools. We depend on at least
1438: * one pool in the system being active.
1439: */
1440: } while (pp == NULL || pp->pr_npages == 0);
1441: pp->pr_refcnt++;
1442: mutex_exit(&pool_head_lock);
1443:
1444: /* Drain the cache (if any) and pool.. */
1.186 pooka 1445: reclaimed = pool_reclaim(pp);
1.134 ad 1446:
1447: /* Finally, unlock the pool. */
1448: mutex_enter(&pool_head_lock);
1449: pp->pr_refcnt--;
1450: cv_broadcast(&pool_busy);
1451: mutex_exit(&pool_head_lock);
1.186 pooka 1452:
1.197 jym 1453: if (ppp != NULL)
1454: *ppp = pp;
1455:
1.186 pooka 1456: return reclaimed;
1.3 pk 1457: }
1458:
1459: /*
1460: * Diagnostic helpers.
1461: */
1.21 thorpej 1462:
1.25 thorpej 1463: void
1.108 yamt 1464: pool_printall(const char *modif, void (*pr)(const char *, ...))
1465: {
1466: struct pool *pp;
1467:
1.145 ad 1468: TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
1.108 yamt 1469: pool_printit(pp, modif, pr);
1470: }
1471: }
1472:
1473: void
1.42 thorpej 1474: pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.25 thorpej 1475: {
1476:
1477: if (pp == NULL) {
1478: (*pr)("Must specify a pool to print.\n");
1479: return;
1480: }
1481:
1482: pool_print1(pp, modif, pr);
1483: }
1484:
1.21 thorpej 1485: static void
1.124 yamt 1486: pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
1.97 yamt 1487: void (*pr)(const char *, ...))
1.88 chs 1488: {
1489: struct pool_item_header *ph;
1.207 riastrad 1490: struct pool_item *pi __diagused;
1.88 chs 1491:
1492: LIST_FOREACH(ph, pl, ph_pagelist) {
1.151 yamt 1493: (*pr)("\t\tpage %p, nmissing %d, time %" PRIu32 "\n",
1494: ph->ph_page, ph->ph_nmissing, ph->ph_time);
1.88 chs 1495: #ifdef DIAGNOSTIC
1.97 yamt 1496: if (!(pp->pr_roflags & PR_NOTOUCH)) {
1.102 chs 1497: LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1.97 yamt 1498: if (pi->pi_magic != PI_MAGIC) {
1499: (*pr)("\t\t\titem %p, magic 0x%x\n",
1500: pi, pi->pi_magic);
1501: }
1.88 chs 1502: }
1503: }
1504: #endif
1505: }
1506: }
1507:
1508: static void
1.42 thorpej 1509: pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.3 pk 1510: {
1.25 thorpej 1511: struct pool_item_header *ph;
1.134 ad 1512: pool_cache_t pc;
1513: pcg_t *pcg;
1514: pool_cache_cpu_t *cc;
1515: uint64_t cpuhit, cpumiss;
1.44 thorpej 1516: int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1.25 thorpej 1517: char c;
1518:
1519: while ((c = *modif++) != '\0') {
1520: if (c == 'l')
1521: print_log = 1;
1522: if (c == 'p')
1523: print_pagelist = 1;
1.44 thorpej 1524: if (c == 'c')
1525: print_cache = 1;
1.25 thorpej 1526: }
1527:
1.134 ad 1528: if ((pc = pp->pr_cache) != NULL) {
1529: (*pr)("POOL CACHE");
1530: } else {
1531: (*pr)("POOL");
1532: }
1533:
1534: (*pr)(" %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1.25 thorpej 1535: pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1536: pp->pr_roflags);
1.66 thorpej 1537: (*pr)("\talloc %p\n", pp->pr_alloc);
1.25 thorpej 1538: (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1539: pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1540: (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1541: pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1542:
1.134 ad 1543: (*pr)("\tnget %lu, nfail %lu, nput %lu\n",
1.25 thorpej 1544: pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1545: (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1546: pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1547:
1548: if (print_pagelist == 0)
1549: goto skip_pagelist;
1550:
1.88 chs 1551: if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1552: (*pr)("\n\tempty page list:\n");
1.97 yamt 1553: pool_print_pagelist(pp, &pp->pr_emptypages, pr);
1.88 chs 1554: if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
1555: (*pr)("\n\tfull page list:\n");
1.97 yamt 1556: pool_print_pagelist(pp, &pp->pr_fullpages, pr);
1.88 chs 1557: if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
1558: (*pr)("\n\tpartial-page list:\n");
1.97 yamt 1559: pool_print_pagelist(pp, &pp->pr_partpages, pr);
1.88 chs 1560:
1.25 thorpej 1561: if (pp->pr_curpage == NULL)
1562: (*pr)("\tno current page\n");
1563: else
1564: (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1565:
1566: skip_pagelist:
1567: if (print_log == 0)
1568: goto skip_log;
1569:
1570: (*pr)("\n");
1.3 pk 1571:
1.25 thorpej 1572: skip_log:
1.44 thorpej 1573:
1.102 chs 1574: #define PR_GROUPLIST(pcg) \
1575: (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail); \
1.142 ad 1576: for (i = 0; i < pcg->pcg_size; i++) { \
1.102 chs 1577: if (pcg->pcg_objects[i].pcgo_pa != \
1578: POOL_PADDR_INVALID) { \
1579: (*pr)("\t\t\t%p, 0x%llx\n", \
1580: pcg->pcg_objects[i].pcgo_va, \
1581: (unsigned long long) \
1582: pcg->pcg_objects[i].pcgo_pa); \
1583: } else { \
1584: (*pr)("\t\t\t%p\n", \
1585: pcg->pcg_objects[i].pcgo_va); \
1586: } \
1587: }
1588:
1.134 ad 1589: if (pc != NULL) {
1590: cpuhit = 0;
1591: cpumiss = 0;
1.183 ad 1592: for (i = 0; i < __arraycount(pc->pc_cpus); i++) {
1.134 ad 1593: if ((cc = pc->pc_cpus[i]) == NULL)
1594: continue;
1595: cpuhit += cc->cc_hits;
1596: cpumiss += cc->cc_misses;
1597: }
1598: (*pr)("\tcpu layer hits %llu misses %llu\n", cpuhit, cpumiss);
1599: (*pr)("\tcache layer hits %llu misses %llu\n",
1600: pc->pc_hits, pc->pc_misses);
1601: (*pr)("\tcache layer entry uncontended %llu contended %llu\n",
1602: pc->pc_hits + pc->pc_misses - pc->pc_contended,
1603: pc->pc_contended);
1604: (*pr)("\tcache layer empty groups %u full groups %u\n",
1605: pc->pc_nempty, pc->pc_nfull);
1606: if (print_cache) {
1607: (*pr)("\tfull cache groups:\n");
1608: for (pcg = pc->pc_fullgroups; pcg != NULL;
1609: pcg = pcg->pcg_next) {
1610: PR_GROUPLIST(pcg);
1611: }
1612: (*pr)("\tempty cache groups:\n");
1613: for (pcg = pc->pc_emptygroups; pcg != NULL;
1614: pcg = pcg->pcg_next) {
1615: PR_GROUPLIST(pcg);
1616: }
1.103 chs 1617: }
1.44 thorpej 1618: }
1.102 chs 1619: #undef PR_GROUPLIST
1.88 chs 1620: }
1621:
1622: static int
1623: pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
1624: {
1625: struct pool_item *pi;
1.128 christos 1626: void *page;
1.88 chs 1627: int n;
1628:
1.121 yamt 1629: if ((pp->pr_roflags & PR_NOALIGN) == 0) {
1.128 christos 1630: page = (void *)((uintptr_t)ph & pp->pr_alloc->pa_pagemask);
1.121 yamt 1631: if (page != ph->ph_page &&
1632: (pp->pr_roflags & PR_PHINPAGE) != 0) {
1633: if (label != NULL)
1634: printf("%s: ", label);
1635: printf("pool(%p:%s): page inconsistency: page %p;"
1636: " at page head addr %p (p %p)\n", pp,
1637: pp->pr_wchan, ph->ph_page,
1638: ph, page);
1639: return 1;
1640: }
1.88 chs 1641: }
1.3 pk 1642:
1.97 yamt 1643: if ((pp->pr_roflags & PR_NOTOUCH) != 0)
1644: return 0;
1645:
1.102 chs 1646: for (pi = LIST_FIRST(&ph->ph_itemlist), n = 0;
1.88 chs 1647: pi != NULL;
1.102 chs 1648: pi = LIST_NEXT(pi,pi_list), n++) {
1.88 chs 1649:
1650: #ifdef DIAGNOSTIC
1651: if (pi->pi_magic != PI_MAGIC) {
1652: if (label != NULL)
1653: printf("%s: ", label);
1654: printf("pool(%s): free list modified: magic=%x;"
1.121 yamt 1655: " page %p; item ordinal %d; addr %p\n",
1.88 chs 1656: pp->pr_wchan, pi->pi_magic, ph->ph_page,
1.121 yamt 1657: n, pi);
1.88 chs 1658: panic("pool");
1659: }
1660: #endif
1.121 yamt 1661: if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1662: continue;
1663: }
1.128 christos 1664: page = (void *)((uintptr_t)pi & pp->pr_alloc->pa_pagemask);
1.88 chs 1665: if (page == ph->ph_page)
1666: continue;
1667:
1668: if (label != NULL)
1669: printf("%s: ", label);
1670: printf("pool(%p:%s): page inconsistency: page %p;"
1671: " item ordinal %d; addr %p (p %p)\n", pp,
1672: pp->pr_wchan, ph->ph_page,
1673: n, pi, page);
1674: return 1;
1675: }
1676: return 0;
1.3 pk 1677: }
1678:
1.88 chs 1679:
1.3 pk 1680: int
1.42 thorpej 1681: pool_chk(struct pool *pp, const char *label)
1.3 pk 1682: {
1683: struct pool_item_header *ph;
1684: int r = 0;
1685:
1.134 ad 1686: mutex_enter(&pp->pr_lock);
1.88 chs 1687: LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
1688: r = pool_chk_page(pp, label, ph);
1689: if (r) {
1690: goto out;
1691: }
1692: }
1693: LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
1694: r = pool_chk_page(pp, label, ph);
1695: if (r) {
1.3 pk 1696: goto out;
1697: }
1.88 chs 1698: }
1699: LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
1700: r = pool_chk_page(pp, label, ph);
1701: if (r) {
1.3 pk 1702: goto out;
1703: }
1704: }
1.88 chs 1705:
1.3 pk 1706: out:
1.134 ad 1707: mutex_exit(&pp->pr_lock);
1.3 pk 1708: return (r);
1.43 thorpej 1709: }
1710:
1711: /*
1712: * pool_cache_init:
1713: *
1714: * Initialize a pool cache.
1.134 ad 1715: */
1716: pool_cache_t
1717: pool_cache_init(size_t size, u_int align, u_int align_offset, u_int flags,
1718: const char *wchan, struct pool_allocator *palloc, int ipl,
1719: int (*ctor)(void *, void *, int), void (*dtor)(void *, void *), void *arg)
1720: {
1721: pool_cache_t pc;
1722:
1723: pc = pool_get(&cache_pool, PR_WAITOK);
1724: if (pc == NULL)
1725: return NULL;
1726:
1727: pool_cache_bootstrap(pc, size, align, align_offset, flags, wchan,
1728: palloc, ipl, ctor, dtor, arg);
1729:
1730: return pc;
1731: }
1732:
1733: /*
1734: * pool_cache_bootstrap:
1.43 thorpej 1735: *
1.134 ad 1736: * Kernel-private version of pool_cache_init(). The caller
1737: * provides initial storage.
1.43 thorpej 1738: */
1739: void
1.134 ad 1740: pool_cache_bootstrap(pool_cache_t pc, size_t size, u_int align,
1741: u_int align_offset, u_int flags, const char *wchan,
1742: struct pool_allocator *palloc, int ipl,
1743: int (*ctor)(void *, void *, int), void (*dtor)(void *, void *),
1.43 thorpej 1744: void *arg)
1745: {
1.134 ad 1746: CPU_INFO_ITERATOR cii;
1.145 ad 1747: pool_cache_t pc1;
1.134 ad 1748: struct cpu_info *ci;
1749: struct pool *pp;
1750:
1751: pp = &pc->pc_pool;
1.208 chs 1752: if (palloc == NULL && ipl == IPL_NONE) {
1753: if (size > PAGE_SIZE) {
1754: int bigidx = pool_bigidx(size);
1755:
1756: palloc = &pool_allocator_big[bigidx];
1757: } else
1758: palloc = &pool_allocator_nointr;
1759: }
1.134 ad 1760: pool_init(pp, size, align, align_offset, flags, wchan, palloc, ipl);
1.157 ad 1761: mutex_init(&pc->pc_lock, MUTEX_DEFAULT, ipl);
1.43 thorpej 1762:
1.134 ad 1763: if (ctor == NULL) {
1764: ctor = (int (*)(void *, void *, int))nullop;
1765: }
1766: if (dtor == NULL) {
1767: dtor = (void (*)(void *, void *))nullop;
1768: }
1.43 thorpej 1769:
1.134 ad 1770: pc->pc_emptygroups = NULL;
1771: pc->pc_fullgroups = NULL;
1772: pc->pc_partgroups = NULL;
1.43 thorpej 1773: pc->pc_ctor = ctor;
1774: pc->pc_dtor = dtor;
1775: pc->pc_arg = arg;
1.134 ad 1776: pc->pc_hits = 0;
1.48 thorpej 1777: pc->pc_misses = 0;
1.134 ad 1778: pc->pc_nempty = 0;
1779: pc->pc_npart = 0;
1780: pc->pc_nfull = 0;
1781: pc->pc_contended = 0;
1782: pc->pc_refcnt = 0;
1.136 yamt 1783: pc->pc_freecheck = NULL;
1.134 ad 1784:
1.142 ad 1785: if ((flags & PR_LARGECACHE) != 0) {
1786: pc->pc_pcgsize = PCG_NOBJECTS_LARGE;
1.163 ad 1787: pc->pc_pcgpool = &pcg_large_pool;
1.142 ad 1788: } else {
1789: pc->pc_pcgsize = PCG_NOBJECTS_NORMAL;
1.163 ad 1790: pc->pc_pcgpool = &pcg_normal_pool;
1.142 ad 1791: }
1792:
1.134 ad 1793: /* Allocate per-CPU caches. */
1794: memset(pc->pc_cpus, 0, sizeof(pc->pc_cpus));
1795: pc->pc_ncpu = 0;
1.139 ad 1796: if (ncpu < 2) {
1.137 ad 1797: /* XXX For sparc: boot CPU is not attached yet. */
1798: pool_cache_cpu_init1(curcpu(), pc);
1799: } else {
1800: for (CPU_INFO_FOREACH(cii, ci)) {
1801: pool_cache_cpu_init1(ci, pc);
1802: }
1.134 ad 1803: }
1.145 ad 1804:
1805: /* Add to list of all pools. */
1806: if (__predict_true(!cold))
1.134 ad 1807: mutex_enter(&pool_head_lock);
1.145 ad 1808: TAILQ_FOREACH(pc1, &pool_cache_head, pc_cachelist) {
1809: if (strcmp(pc1->pc_pool.pr_wchan, pc->pc_pool.pr_wchan) > 0)
1810: break;
1811: }
1812: if (pc1 == NULL)
1813: TAILQ_INSERT_TAIL(&pool_cache_head, pc, pc_cachelist);
1814: else
1815: TAILQ_INSERT_BEFORE(pc1, pc, pc_cachelist);
1816: if (__predict_true(!cold))
1.134 ad 1817: mutex_exit(&pool_head_lock);
1.145 ad 1818:
1819: membar_sync();
1820: pp->pr_cache = pc;
1.43 thorpej 1821: }
1822:
1823: /*
1824: * pool_cache_destroy:
1825: *
1826: * Destroy a pool cache.
1827: */
1828: void
1.134 ad 1829: pool_cache_destroy(pool_cache_t pc)
1.43 thorpej 1830: {
1.191 para 1831:
1832: pool_cache_bootstrap_destroy(pc);
1833: pool_put(&cache_pool, pc);
1834: }
1835:
1836: /*
1837: * pool_cache_bootstrap_destroy:
1838: *
1839: * Destroy a pool cache.
1840: */
1841: void
1842: pool_cache_bootstrap_destroy(pool_cache_t pc)
1843: {
1.134 ad 1844: struct pool *pp = &pc->pc_pool;
1.175 jym 1845: u_int i;
1.134 ad 1846:
1847: /* Remove it from the global list. */
1848: mutex_enter(&pool_head_lock);
1849: while (pc->pc_refcnt != 0)
1850: cv_wait(&pool_busy, &pool_head_lock);
1.145 ad 1851: TAILQ_REMOVE(&pool_cache_head, pc, pc_cachelist);
1.134 ad 1852: mutex_exit(&pool_head_lock);
1.43 thorpej 1853:
1854: /* First, invalidate the entire cache. */
1855: pool_cache_invalidate(pc);
1856:
1.134 ad 1857: /* Disassociate it from the pool. */
1858: mutex_enter(&pp->pr_lock);
1859: pp->pr_cache = NULL;
1860: mutex_exit(&pp->pr_lock);
1861:
1862: /* Destroy per-CPU data */
1.183 ad 1863: for (i = 0; i < __arraycount(pc->pc_cpus); i++)
1.175 jym 1864: pool_cache_invalidate_cpu(pc, i);
1.134 ad 1865:
1866: /* Finally, destroy it. */
1867: mutex_destroy(&pc->pc_lock);
1868: pool_destroy(pp);
1869: }
1870:
1871: /*
1872: * pool_cache_cpu_init1:
1873: *
1874: * Called for each pool_cache whenever a new CPU is attached.
1875: */
1876: static void
1877: pool_cache_cpu_init1(struct cpu_info *ci, pool_cache_t pc)
1878: {
1879: pool_cache_cpu_t *cc;
1.137 ad 1880: int index;
1.134 ad 1881:
1.137 ad 1882: index = ci->ci_index;
1883:
1.183 ad 1884: KASSERT(index < __arraycount(pc->pc_cpus));
1.134 ad 1885:
1.137 ad 1886: if ((cc = pc->pc_cpus[index]) != NULL) {
1887: KASSERT(cc->cc_cpuindex == index);
1.134 ad 1888: return;
1889: }
1890:
1891: /*
1892: * The first CPU is 'free'. This needs to be the case for
1893: * bootstrap - we may not be able to allocate yet.
1894: */
1895: if (pc->pc_ncpu == 0) {
1896: cc = &pc->pc_cpu0;
1897: pc->pc_ncpu = 1;
1898: } else {
1899: mutex_enter(&pc->pc_lock);
1900: pc->pc_ncpu++;
1901: mutex_exit(&pc->pc_lock);
1902: cc = pool_get(&cache_cpu_pool, PR_WAITOK);
1903: }
1904:
1905: cc->cc_ipl = pc->pc_pool.pr_ipl;
1906: cc->cc_iplcookie = makeiplcookie(cc->cc_ipl);
1907: cc->cc_cache = pc;
1.137 ad 1908: cc->cc_cpuindex = index;
1.134 ad 1909: cc->cc_hits = 0;
1910: cc->cc_misses = 0;
1.169 yamt 1911: cc->cc_current = __UNCONST(&pcg_dummy);
1912: cc->cc_previous = __UNCONST(&pcg_dummy);
1.134 ad 1913:
1.137 ad 1914: pc->pc_cpus[index] = cc;
1.43 thorpej 1915: }
1916:
1.134 ad 1917: /*
1918: * pool_cache_cpu_init:
1919: *
1920: * Called whenever a new CPU is attached.
1921: */
1922: void
1923: pool_cache_cpu_init(struct cpu_info *ci)
1.43 thorpej 1924: {
1.134 ad 1925: pool_cache_t pc;
1926:
1927: mutex_enter(&pool_head_lock);
1.145 ad 1928: TAILQ_FOREACH(pc, &pool_cache_head, pc_cachelist) {
1.134 ad 1929: pc->pc_refcnt++;
1930: mutex_exit(&pool_head_lock);
1.43 thorpej 1931:
1.134 ad 1932: pool_cache_cpu_init1(ci, pc);
1.43 thorpej 1933:
1.134 ad 1934: mutex_enter(&pool_head_lock);
1935: pc->pc_refcnt--;
1936: cv_broadcast(&pool_busy);
1937: }
1938: mutex_exit(&pool_head_lock);
1.43 thorpej 1939: }
1940:
1.134 ad 1941: /*
1942: * pool_cache_reclaim:
1943: *
1944: * Reclaim memory from a pool cache.
1945: */
1946: bool
1947: pool_cache_reclaim(pool_cache_t pc)
1.43 thorpej 1948: {
1949:
1.134 ad 1950: return pool_reclaim(&pc->pc_pool);
1951: }
1.43 thorpej 1952:
1.136 yamt 1953: static void
1954: pool_cache_destruct_object1(pool_cache_t pc, void *object)
1955: {
1956:
1957: (*pc->pc_dtor)(pc->pc_arg, object);
1958: pool_put(&pc->pc_pool, object);
1959: }
1960:
1.134 ad 1961: /*
1962: * pool_cache_destruct_object:
1963: *
1964: * Force destruction of an object and its release back into
1965: * the pool.
1966: */
1967: void
1968: pool_cache_destruct_object(pool_cache_t pc, void *object)
1969: {
1970:
1.136 yamt 1971: FREECHECK_IN(&pc->pc_freecheck, object);
1972:
1973: pool_cache_destruct_object1(pc, object);
1.43 thorpej 1974: }
1975:
1.134 ad 1976: /*
1977: * pool_cache_invalidate_groups:
1978: *
1979: * Invalidate a chain of groups and destruct all objects.
1980: */
1.102 chs 1981: static void
1.134 ad 1982: pool_cache_invalidate_groups(pool_cache_t pc, pcg_t *pcg)
1.102 chs 1983: {
1.134 ad 1984: void *object;
1985: pcg_t *next;
1986: int i;
1987:
1988: for (; pcg != NULL; pcg = next) {
1989: next = pcg->pcg_next;
1990:
1991: for (i = 0; i < pcg->pcg_avail; i++) {
1992: object = pcg->pcg_objects[i].pcgo_va;
1.136 yamt 1993: pool_cache_destruct_object1(pc, object);
1.134 ad 1994: }
1.102 chs 1995:
1.142 ad 1996: if (pcg->pcg_size == PCG_NOBJECTS_LARGE) {
1997: pool_put(&pcg_large_pool, pcg);
1998: } else {
1999: KASSERT(pcg->pcg_size == PCG_NOBJECTS_NORMAL);
2000: pool_put(&pcg_normal_pool, pcg);
2001: }
1.102 chs 2002: }
2003: }
2004:
1.43 thorpej 2005: /*
1.134 ad 2006: * pool_cache_invalidate:
1.43 thorpej 2007: *
1.134 ad 2008: * Invalidate a pool cache (destruct and release all of the
2009: * cached objects). Does not reclaim objects from the pool.
1.176 thorpej 2010: *
2011: * Note: For pool caches that provide constructed objects, there
2012: * is an assumption that another level of synchronization is occurring
2013: * between the input to the constructor and the cache invalidation.
1.196 jym 2014: *
2015: * Invalidation is a costly process and should not be called from
2016: * interrupt context.
1.43 thorpej 2017: */
1.134 ad 2018: void
2019: pool_cache_invalidate(pool_cache_t pc)
2020: {
1.196 jym 2021: uint64_t where;
1.134 ad 2022: pcg_t *full, *empty, *part;
1.196 jym 2023:
2024: KASSERT(!cpu_intr_p() && !cpu_softintr_p());
1.176 thorpej 2025:
1.177 jym 2026: if (ncpu < 2 || !mp_online) {
1.176 thorpej 2027: /*
2028: * We might be called early enough in the boot process
2029: * for the CPU data structures to not be fully initialized.
1.196 jym 2030: * In this case, transfer the content of the local CPU's
2031: * cache back into global cache as only this CPU is currently
2032: * running.
1.176 thorpej 2033: */
1.196 jym 2034: pool_cache_transfer(pc);
1.176 thorpej 2035: } else {
2036: /*
1.196 jym 2037: * Signal all CPUs that they must transfer their local
2038: * cache back to the global pool then wait for the xcall to
2039: * complete.
1.176 thorpej 2040: */
1.196 jym 2041: where = xc_broadcast(0, (xcfunc_t)pool_cache_transfer,
2042: pc, NULL);
1.176 thorpej 2043: xc_wait(where);
2044: }
1.196 jym 2045:
2046: /* Empty pool caches, then invalidate objects */
1.134 ad 2047: mutex_enter(&pc->pc_lock);
2048: full = pc->pc_fullgroups;
2049: empty = pc->pc_emptygroups;
2050: part = pc->pc_partgroups;
2051: pc->pc_fullgroups = NULL;
2052: pc->pc_emptygroups = NULL;
2053: pc->pc_partgroups = NULL;
2054: pc->pc_nfull = 0;
2055: pc->pc_nempty = 0;
2056: pc->pc_npart = 0;
2057: mutex_exit(&pc->pc_lock);
2058:
2059: pool_cache_invalidate_groups(pc, full);
2060: pool_cache_invalidate_groups(pc, empty);
2061: pool_cache_invalidate_groups(pc, part);
2062: }
2063:
1.175 jym 2064: /*
2065: * pool_cache_invalidate_cpu:
2066: *
2067: * Invalidate all CPU-bound cached objects in pool cache, the CPU being
2068: * identified by its associated index.
2069: * It is caller's responsibility to ensure that no operation is
2070: * taking place on this pool cache while doing this invalidation.
2071: * WARNING: as no inter-CPU locking is enforced, trying to invalidate
2072: * pool cached objects from a CPU different from the one currently running
2073: * may result in an undefined behaviour.
2074: */
2075: static void
2076: pool_cache_invalidate_cpu(pool_cache_t pc, u_int index)
2077: {
2078: pool_cache_cpu_t *cc;
2079: pcg_t *pcg;
2080:
2081: if ((cc = pc->pc_cpus[index]) == NULL)
2082: return;
2083:
2084: if ((pcg = cc->cc_current) != &pcg_dummy) {
2085: pcg->pcg_next = NULL;
2086: pool_cache_invalidate_groups(pc, pcg);
2087: }
2088: if ((pcg = cc->cc_previous) != &pcg_dummy) {
2089: pcg->pcg_next = NULL;
2090: pool_cache_invalidate_groups(pc, pcg);
2091: }
2092: if (cc != &pc->pc_cpu0)
2093: pool_put(&cache_cpu_pool, cc);
2094:
2095: }
2096:
1.134 ad 2097: void
2098: pool_cache_set_drain_hook(pool_cache_t pc, void (*fn)(void *, int), void *arg)
2099: {
2100:
2101: pool_set_drain_hook(&pc->pc_pool, fn, arg);
2102: }
2103:
2104: void
2105: pool_cache_setlowat(pool_cache_t pc, int n)
2106: {
2107:
2108: pool_setlowat(&pc->pc_pool, n);
2109: }
2110:
2111: void
2112: pool_cache_sethiwat(pool_cache_t pc, int n)
2113: {
2114:
2115: pool_sethiwat(&pc->pc_pool, n);
2116: }
2117:
2118: void
2119: pool_cache_sethardlimit(pool_cache_t pc, int n, const char *warnmess, int ratecap)
2120: {
2121:
2122: pool_sethardlimit(&pc->pc_pool, n, warnmess, ratecap);
2123: }
2124:
1.162 ad 2125: static bool __noinline
2126: pool_cache_get_slow(pool_cache_cpu_t *cc, int s, void **objectp,
1.134 ad 2127: paddr_t *pap, int flags)
1.43 thorpej 2128: {
1.134 ad 2129: pcg_t *pcg, *cur;
2130: uint64_t ncsw;
2131: pool_cache_t pc;
1.43 thorpej 2132: void *object;
1.58 thorpej 2133:
1.168 yamt 2134: KASSERT(cc->cc_current->pcg_avail == 0);
2135: KASSERT(cc->cc_previous->pcg_avail == 0);
2136:
1.134 ad 2137: pc = cc->cc_cache;
2138: cc->cc_misses++;
1.43 thorpej 2139:
1.134 ad 2140: /*
2141: * Nothing was available locally. Try and grab a group
2142: * from the cache.
2143: */
1.162 ad 2144: if (__predict_false(!mutex_tryenter(&pc->pc_lock))) {
1.134 ad 2145: ncsw = curlwp->l_ncsw;
2146: mutex_enter(&pc->pc_lock);
2147: pc->pc_contended++;
1.43 thorpej 2148:
1.134 ad 2149: /*
2150: * If we context switched while locking, then
2151: * our view of the per-CPU data is invalid:
2152: * retry.
2153: */
2154: if (curlwp->l_ncsw != ncsw) {
2155: mutex_exit(&pc->pc_lock);
1.162 ad 2156: return true;
1.43 thorpej 2157: }
1.102 chs 2158: }
1.43 thorpej 2159:
1.162 ad 2160: if (__predict_true((pcg = pc->pc_fullgroups) != NULL)) {
1.43 thorpej 2161: /*
1.134 ad 2162: * If there's a full group, release our empty
2163: * group back to the cache. Install the full
2164: * group as cc_current and return.
1.43 thorpej 2165: */
1.162 ad 2166: if (__predict_true((cur = cc->cc_current) != &pcg_dummy)) {
1.134 ad 2167: KASSERT(cur->pcg_avail == 0);
2168: cur->pcg_next = pc->pc_emptygroups;
2169: pc->pc_emptygroups = cur;
2170: pc->pc_nempty++;
1.87 thorpej 2171: }
1.142 ad 2172: KASSERT(pcg->pcg_avail == pcg->pcg_size);
1.134 ad 2173: cc->cc_current = pcg;
2174: pc->pc_fullgroups = pcg->pcg_next;
2175: pc->pc_hits++;
2176: pc->pc_nfull--;
2177: mutex_exit(&pc->pc_lock);
1.162 ad 2178: return true;
1.134 ad 2179: }
2180:
2181: /*
2182: * Nothing available locally or in cache. Take the slow
2183: * path: fetch a new object from the pool and construct
2184: * it.
2185: */
2186: pc->pc_misses++;
2187: mutex_exit(&pc->pc_lock);
1.162 ad 2188: splx(s);
1.134 ad 2189:
2190: object = pool_get(&pc->pc_pool, flags);
2191: *objectp = object;
1.211 riastrad 2192: if (__predict_false(object == NULL)) {
2193: KASSERT((flags & (PR_WAITOK|PR_NOWAIT)) == PR_NOWAIT);
1.162 ad 2194: return false;
1.211 riastrad 2195: }
1.125 ad 2196:
1.162 ad 2197: if (__predict_false((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0)) {
1.134 ad 2198: pool_put(&pc->pc_pool, object);
2199: *objectp = NULL;
1.162 ad 2200: return false;
1.43 thorpej 2201: }
2202:
1.134 ad 2203: KASSERT((((vaddr_t)object + pc->pc_pool.pr_itemoffset) &
2204: (pc->pc_pool.pr_align - 1)) == 0);
1.43 thorpej 2205:
1.134 ad 2206: if (pap != NULL) {
2207: #ifdef POOL_VTOPHYS
2208: *pap = POOL_VTOPHYS(object);
2209: #else
2210: *pap = POOL_PADDR_INVALID;
2211: #endif
1.102 chs 2212: }
1.43 thorpej 2213:
1.125 ad 2214: FREECHECK_OUT(&pc->pc_freecheck, object);
1.204 maxv 2215: pool_redzone_fill(&pc->pc_pool, object);
1.162 ad 2216: return false;
1.43 thorpej 2217: }
2218:
2219: /*
1.134 ad 2220: * pool_cache_get{,_paddr}:
1.43 thorpej 2221: *
1.134 ad 2222: * Get an object from a pool cache (optionally returning
2223: * the physical address of the object).
1.43 thorpej 2224: */
1.134 ad 2225: void *
2226: pool_cache_get_paddr(pool_cache_t pc, int flags, paddr_t *pap)
1.43 thorpej 2227: {
1.134 ad 2228: pool_cache_cpu_t *cc;
2229: pcg_t *pcg;
2230: void *object;
1.60 thorpej 2231: int s;
1.43 thorpej 2232:
1.184 rmind 2233: KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()) ||
1.185 rmind 2234: (pc->pc_pool.pr_ipl != IPL_NONE || cold || panicstr != NULL),
1.213 christos 2235: "%s: [%s] is IPL_NONE, but called from interrupt context",
2236: __func__, pc->pc_pool.pr_wchan);
1.184 rmind 2237:
1.155 ad 2238: if (flags & PR_WAITOK) {
1.154 yamt 2239: ASSERT_SLEEPABLE();
1.155 ad 2240: }
1.125 ad 2241:
1.162 ad 2242: /* Lock out interrupts and disable preemption. */
2243: s = splvm();
1.165 yamt 2244: while (/* CONSTCOND */ true) {
1.134 ad 2245: /* Try and allocate an object from the current group. */
1.162 ad 2246: cc = pc->pc_cpus[curcpu()->ci_index];
2247: KASSERT(cc->cc_cache == pc);
1.134 ad 2248: pcg = cc->cc_current;
1.162 ad 2249: if (__predict_true(pcg->pcg_avail > 0)) {
1.134 ad 2250: object = pcg->pcg_objects[--pcg->pcg_avail].pcgo_va;
1.162 ad 2251: if (__predict_false(pap != NULL))
1.134 ad 2252: *pap = pcg->pcg_objects[pcg->pcg_avail].pcgo_pa;
1.148 yamt 2253: #if defined(DIAGNOSTIC)
1.134 ad 2254: pcg->pcg_objects[pcg->pcg_avail].pcgo_va = NULL;
1.163 ad 2255: KASSERT(pcg->pcg_avail < pcg->pcg_size);
1.134 ad 2256: KASSERT(object != NULL);
1.163 ad 2257: #endif
1.134 ad 2258: cc->cc_hits++;
1.162 ad 2259: splx(s);
1.134 ad 2260: FREECHECK_OUT(&pc->pc_freecheck, object);
1.204 maxv 2261: pool_redzone_fill(&pc->pc_pool, object);
1.134 ad 2262: return object;
1.43 thorpej 2263: }
2264:
2265: /*
1.134 ad 2266: * That failed. If the previous group isn't empty, swap
2267: * it with the current group and allocate from there.
1.43 thorpej 2268: */
1.134 ad 2269: pcg = cc->cc_previous;
1.162 ad 2270: if (__predict_true(pcg->pcg_avail > 0)) {
1.134 ad 2271: cc->cc_previous = cc->cc_current;
2272: cc->cc_current = pcg;
2273: continue;
1.43 thorpej 2274: }
2275:
1.134 ad 2276: /*
2277: * Can't allocate from either group: try the slow path.
2278: * If get_slow() allocated an object for us, or if
1.162 ad 2279: * no more objects are available, it will return false.
1.134 ad 2280: * Otherwise, we need to retry.
2281: */
1.165 yamt 2282: if (!pool_cache_get_slow(cc, s, &object, pap, flags))
2283: break;
2284: }
1.43 thorpej 2285:
1.211 riastrad 2286: /*
2287: * We would like to KASSERT(object || (flags & PR_NOWAIT)), but
2288: * pool_cache_get can fail even in the PR_WAITOK case, if the
2289: * constructor fails.
2290: */
1.134 ad 2291: return object;
1.51 thorpej 2292: }
2293:
1.162 ad 2294: static bool __noinline
2295: pool_cache_put_slow(pool_cache_cpu_t *cc, int s, void *object)
1.51 thorpej 2296: {
1.200 pooka 2297: struct lwp *l = curlwp;
1.163 ad 2298: pcg_t *pcg, *cur;
1.134 ad 2299: uint64_t ncsw;
2300: pool_cache_t pc;
1.51 thorpej 2301:
1.168 yamt 2302: KASSERT(cc->cc_current->pcg_avail == cc->cc_current->pcg_size);
2303: KASSERT(cc->cc_previous->pcg_avail == cc->cc_previous->pcg_size);
2304:
1.134 ad 2305: pc = cc->cc_cache;
1.171 ad 2306: pcg = NULL;
1.134 ad 2307: cc->cc_misses++;
1.200 pooka 2308: ncsw = l->l_ncsw;
1.43 thorpej 2309:
1.171 ad 2310: /*
2311: * If there are no empty groups in the cache then allocate one
2312: * while still unlocked.
2313: */
2314: if (__predict_false(pc->pc_emptygroups == NULL)) {
2315: if (__predict_true(!pool_cache_disable)) {
2316: pcg = pool_get(pc->pc_pcgpool, PR_NOWAIT);
2317: }
1.200 pooka 2318: /*
2319: * If pool_get() blocked, then our view of
2320: * the per-CPU data is invalid: retry.
2321: */
2322: if (__predict_false(l->l_ncsw != ncsw)) {
2323: if (pcg != NULL) {
2324: pool_put(pc->pc_pcgpool, pcg);
2325: }
2326: return true;
2327: }
1.171 ad 2328: if (__predict_true(pcg != NULL)) {
2329: pcg->pcg_avail = 0;
2330: pcg->pcg_size = pc->pc_pcgsize;
2331: }
2332: }
2333:
1.162 ad 2334: /* Lock the cache. */
2335: if (__predict_false(!mutex_tryenter(&pc->pc_lock))) {
1.134 ad 2336: mutex_enter(&pc->pc_lock);
2337: pc->pc_contended++;
1.162 ad 2338:
1.163 ad 2339: /*
2340: * If we context switched while locking, then our view of
2341: * the per-CPU data is invalid: retry.
2342: */
1.200 pooka 2343: if (__predict_false(l->l_ncsw != ncsw)) {
1.163 ad 2344: mutex_exit(&pc->pc_lock);
1.171 ad 2345: if (pcg != NULL) {
2346: pool_put(pc->pc_pcgpool, pcg);
2347: }
1.163 ad 2348: return true;
2349: }
1.162 ad 2350: }
1.102 chs 2351:
1.163 ad 2352: /* If there are no empty groups in the cache then allocate one. */
1.171 ad 2353: if (pcg == NULL && pc->pc_emptygroups != NULL) {
2354: pcg = pc->pc_emptygroups;
1.163 ad 2355: pc->pc_emptygroups = pcg->pcg_next;
2356: pc->pc_nempty--;
1.134 ad 2357: }
1.130 ad 2358:
1.162 ad 2359: /*
2360: * If there's a empty group, release our full group back
2361: * to the cache. Install the empty group to the local CPU
2362: * and return.
2363: */
1.163 ad 2364: if (pcg != NULL) {
1.134 ad 2365: KASSERT(pcg->pcg_avail == 0);
1.162 ad 2366: if (__predict_false(cc->cc_previous == &pcg_dummy)) {
1.146 ad 2367: cc->cc_previous = pcg;
2368: } else {
1.162 ad 2369: cur = cc->cc_current;
2370: if (__predict_true(cur != &pcg_dummy)) {
1.163 ad 2371: KASSERT(cur->pcg_avail == cur->pcg_size);
1.146 ad 2372: cur->pcg_next = pc->pc_fullgroups;
2373: pc->pc_fullgroups = cur;
2374: pc->pc_nfull++;
2375: }
2376: cc->cc_current = pcg;
2377: }
1.163 ad 2378: pc->pc_hits++;
1.134 ad 2379: mutex_exit(&pc->pc_lock);
1.162 ad 2380: return true;
1.102 chs 2381: }
1.105 christos 2382:
1.134 ad 2383: /*
1.162 ad 2384: * Nothing available locally or in cache, and we didn't
2385: * allocate an empty group. Take the slow path and destroy
2386: * the object here and now.
1.134 ad 2387: */
2388: pc->pc_misses++;
2389: mutex_exit(&pc->pc_lock);
1.162 ad 2390: splx(s);
2391: pool_cache_destruct_object(pc, object);
1.105 christos 2392:
1.162 ad 2393: return false;
1.134 ad 2394: }
1.102 chs 2395:
1.43 thorpej 2396: /*
1.134 ad 2397: * pool_cache_put{,_paddr}:
1.43 thorpej 2398: *
1.134 ad 2399: * Put an object back to the pool cache (optionally caching the
2400: * physical address of the object).
1.43 thorpej 2401: */
1.101 thorpej 2402: void
1.134 ad 2403: pool_cache_put_paddr(pool_cache_t pc, void *object, paddr_t pa)
1.43 thorpej 2404: {
1.134 ad 2405: pool_cache_cpu_t *cc;
2406: pcg_t *pcg;
2407: int s;
1.101 thorpej 2408:
1.172 yamt 2409: KASSERT(object != NULL);
1.204 maxv 2410: pool_redzone_check(&pc->pc_pool, object);
1.134 ad 2411: FREECHECK_IN(&pc->pc_freecheck, object);
1.101 thorpej 2412:
1.162 ad 2413: /* Lock out interrupts and disable preemption. */
2414: s = splvm();
1.165 yamt 2415: while (/* CONSTCOND */ true) {
1.134 ad 2416: /* If the current group isn't full, release it there. */
1.162 ad 2417: cc = pc->pc_cpus[curcpu()->ci_index];
2418: KASSERT(cc->cc_cache == pc);
1.134 ad 2419: pcg = cc->cc_current;
1.162 ad 2420: if (__predict_true(pcg->pcg_avail < pcg->pcg_size)) {
1.134 ad 2421: pcg->pcg_objects[pcg->pcg_avail].pcgo_va = object;
2422: pcg->pcg_objects[pcg->pcg_avail].pcgo_pa = pa;
2423: pcg->pcg_avail++;
2424: cc->cc_hits++;
1.162 ad 2425: splx(s);
1.134 ad 2426: return;
2427: }
1.43 thorpej 2428:
1.134 ad 2429: /*
1.162 ad 2430: * That failed. If the previous group isn't full, swap
1.134 ad 2431: * it with the current group and try again.
2432: */
2433: pcg = cc->cc_previous;
1.162 ad 2434: if (__predict_true(pcg->pcg_avail < pcg->pcg_size)) {
1.134 ad 2435: cc->cc_previous = cc->cc_current;
2436: cc->cc_current = pcg;
2437: continue;
2438: }
1.43 thorpej 2439:
1.134 ad 2440: /*
2441: * Can't free to either group: try the slow path.
2442: * If put_slow() releases the object for us, it
1.162 ad 2443: * will return false. Otherwise we need to retry.
1.134 ad 2444: */
1.165 yamt 2445: if (!pool_cache_put_slow(cc, s, object))
2446: break;
2447: }
1.43 thorpej 2448: }
2449:
2450: /*
1.196 jym 2451: * pool_cache_transfer:
1.43 thorpej 2452: *
1.134 ad 2453: * Transfer objects from the per-CPU cache to the global cache.
2454: * Run within a cross-call thread.
1.43 thorpej 2455: */
2456: static void
1.196 jym 2457: pool_cache_transfer(pool_cache_t pc)
1.43 thorpej 2458: {
1.134 ad 2459: pool_cache_cpu_t *cc;
2460: pcg_t *prev, *cur, **list;
1.162 ad 2461: int s;
1.134 ad 2462:
1.162 ad 2463: s = splvm();
2464: mutex_enter(&pc->pc_lock);
2465: cc = pc->pc_cpus[curcpu()->ci_index];
1.134 ad 2466: cur = cc->cc_current;
1.169 yamt 2467: cc->cc_current = __UNCONST(&pcg_dummy);
1.134 ad 2468: prev = cc->cc_previous;
1.169 yamt 2469: cc->cc_previous = __UNCONST(&pcg_dummy);
1.162 ad 2470: if (cur != &pcg_dummy) {
1.142 ad 2471: if (cur->pcg_avail == cur->pcg_size) {
1.134 ad 2472: list = &pc->pc_fullgroups;
2473: pc->pc_nfull++;
2474: } else if (cur->pcg_avail == 0) {
2475: list = &pc->pc_emptygroups;
2476: pc->pc_nempty++;
2477: } else {
2478: list = &pc->pc_partgroups;
2479: pc->pc_npart++;
2480: }
2481: cur->pcg_next = *list;
2482: *list = cur;
2483: }
1.162 ad 2484: if (prev != &pcg_dummy) {
1.142 ad 2485: if (prev->pcg_avail == prev->pcg_size) {
1.134 ad 2486: list = &pc->pc_fullgroups;
2487: pc->pc_nfull++;
2488: } else if (prev->pcg_avail == 0) {
2489: list = &pc->pc_emptygroups;
2490: pc->pc_nempty++;
2491: } else {
2492: list = &pc->pc_partgroups;
2493: pc->pc_npart++;
2494: }
2495: prev->pcg_next = *list;
2496: *list = prev;
2497: }
2498: mutex_exit(&pc->pc_lock);
2499: splx(s);
1.3 pk 2500: }
1.66 thorpej 2501:
2502: /*
2503: * Pool backend allocators.
2504: *
2505: * Each pool has a backend allocator that handles allocation, deallocation,
2506: * and any additional draining that might be needed.
2507: *
2508: * We provide two standard allocators:
2509: *
2510: * pool_allocator_kmem - the default when no allocator is specified
2511: *
2512: * pool_allocator_nointr - used for pools that will not be accessed
2513: * in interrupt context.
2514: */
2515: void *pool_page_alloc(struct pool *, int);
2516: void pool_page_free(struct pool *, void *);
2517:
1.112 bjh21 2518: #ifdef POOL_SUBPAGE
2519: struct pool_allocator pool_allocator_kmem_fullpage = {
1.192 rmind 2520: .pa_alloc = pool_page_alloc,
2521: .pa_free = pool_page_free,
2522: .pa_pagesz = 0
1.112 bjh21 2523: };
2524: #else
1.66 thorpej 2525: struct pool_allocator pool_allocator_kmem = {
1.191 para 2526: .pa_alloc = pool_page_alloc,
2527: .pa_free = pool_page_free,
2528: .pa_pagesz = 0
1.66 thorpej 2529: };
1.112 bjh21 2530: #endif
1.66 thorpej 2531:
1.112 bjh21 2532: #ifdef POOL_SUBPAGE
2533: struct pool_allocator pool_allocator_nointr_fullpage = {
1.194 para 2534: .pa_alloc = pool_page_alloc,
2535: .pa_free = pool_page_free,
1.192 rmind 2536: .pa_pagesz = 0
1.112 bjh21 2537: };
2538: #else
1.66 thorpej 2539: struct pool_allocator pool_allocator_nointr = {
1.191 para 2540: .pa_alloc = pool_page_alloc,
2541: .pa_free = pool_page_free,
2542: .pa_pagesz = 0
1.66 thorpej 2543: };
1.112 bjh21 2544: #endif
1.66 thorpej 2545:
2546: #ifdef POOL_SUBPAGE
2547: void *pool_subpage_alloc(struct pool *, int);
2548: void pool_subpage_free(struct pool *, void *);
2549:
1.112 bjh21 2550: struct pool_allocator pool_allocator_kmem = {
1.193 he 2551: .pa_alloc = pool_subpage_alloc,
2552: .pa_free = pool_subpage_free,
2553: .pa_pagesz = POOL_SUBPAGE
1.112 bjh21 2554: };
2555:
2556: struct pool_allocator pool_allocator_nointr = {
1.192 rmind 2557: .pa_alloc = pool_subpage_alloc,
2558: .pa_free = pool_subpage_free,
2559: .pa_pagesz = POOL_SUBPAGE
1.66 thorpej 2560: };
2561: #endif /* POOL_SUBPAGE */
2562:
1.208 chs 2563: struct pool_allocator pool_allocator_big[] = {
2564: {
2565: .pa_alloc = pool_page_alloc,
2566: .pa_free = pool_page_free,
2567: .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 0),
2568: },
2569: {
2570: .pa_alloc = pool_page_alloc,
2571: .pa_free = pool_page_free,
2572: .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 1),
2573: },
2574: {
2575: .pa_alloc = pool_page_alloc,
2576: .pa_free = pool_page_free,
2577: .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 2),
2578: },
2579: {
2580: .pa_alloc = pool_page_alloc,
2581: .pa_free = pool_page_free,
2582: .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 3),
2583: },
2584: {
2585: .pa_alloc = pool_page_alloc,
2586: .pa_free = pool_page_free,
2587: .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 4),
2588: },
2589: {
2590: .pa_alloc = pool_page_alloc,
2591: .pa_free = pool_page_free,
2592: .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 5),
2593: },
2594: {
2595: .pa_alloc = pool_page_alloc,
2596: .pa_free = pool_page_free,
2597: .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 6),
2598: },
2599: {
2600: .pa_alloc = pool_page_alloc,
2601: .pa_free = pool_page_free,
2602: .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 7),
2603: }
2604: };
2605:
2606: static int
2607: pool_bigidx(size_t size)
2608: {
2609: int i;
2610:
2611: for (i = 0; i < __arraycount(pool_allocator_big); i++) {
2612: if (1 << (i + POOL_ALLOCATOR_BIG_BASE) >= size)
2613: return i;
2614: }
2615: panic("pool item size %zu too large, use a custom allocator", size);
2616: }
2617:
1.117 yamt 2618: static void *
2619: pool_allocator_alloc(struct pool *pp, int flags)
1.66 thorpej 2620: {
1.117 yamt 2621: struct pool_allocator *pa = pp->pr_alloc;
1.66 thorpej 2622: void *res;
2623:
1.117 yamt 2624: res = (*pa->pa_alloc)(pp, flags);
2625: if (res == NULL && (flags & PR_WAITOK) == 0) {
1.66 thorpej 2626: /*
1.117 yamt 2627: * We only run the drain hook here if PR_NOWAIT.
2628: * In other cases, the hook will be run in
2629: * pool_reclaim().
1.66 thorpej 2630: */
1.117 yamt 2631: if (pp->pr_drain_hook != NULL) {
2632: (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
2633: res = (*pa->pa_alloc)(pp, flags);
1.66 thorpej 2634: }
1.117 yamt 2635: }
2636: return res;
1.66 thorpej 2637: }
2638:
1.117 yamt 2639: static void
1.66 thorpej 2640: pool_allocator_free(struct pool *pp, void *v)
2641: {
2642: struct pool_allocator *pa = pp->pr_alloc;
2643:
2644: (*pa->pa_free)(pp, v);
2645: }
2646:
2647: void *
1.124 yamt 2648: pool_page_alloc(struct pool *pp, int flags)
1.66 thorpej 2649: {
1.192 rmind 2650: const vm_flag_t vflags = (flags & PR_WAITOK) ? VM_SLEEP: VM_NOSLEEP;
1.191 para 2651: vmem_addr_t va;
1.192 rmind 2652: int ret;
1.191 para 2653:
1.192 rmind 2654: ret = uvm_km_kmem_alloc(kmem_va_arena, pp->pr_alloc->pa_pagesz,
2655: vflags | VM_INSTANTFIT, &va);
1.66 thorpej 2656:
1.192 rmind 2657: return ret ? NULL : (void *)va;
1.66 thorpej 2658: }
2659:
2660: void
1.124 yamt 2661: pool_page_free(struct pool *pp, void *v)
1.66 thorpej 2662: {
2663:
1.191 para 2664: uvm_km_kmem_free(kmem_va_arena, (vaddr_t)v, pp->pr_alloc->pa_pagesz);
1.98 yamt 2665: }
2666:
2667: static void *
1.124 yamt 2668: pool_page_alloc_meta(struct pool *pp, int flags)
1.98 yamt 2669: {
1.192 rmind 2670: const vm_flag_t vflags = (flags & PR_WAITOK) ? VM_SLEEP: VM_NOSLEEP;
2671: vmem_addr_t va;
2672: int ret;
1.191 para 2673:
1.192 rmind 2674: ret = vmem_alloc(kmem_meta_arena, pp->pr_alloc->pa_pagesz,
2675: vflags | VM_INSTANTFIT, &va);
1.98 yamt 2676:
1.192 rmind 2677: return ret ? NULL : (void *)va;
1.98 yamt 2678: }
2679:
2680: static void
1.124 yamt 2681: pool_page_free_meta(struct pool *pp, void *v)
1.98 yamt 2682: {
2683:
1.192 rmind 2684: vmem_free(kmem_meta_arena, (vmem_addr_t)v, pp->pr_alloc->pa_pagesz);
1.66 thorpej 2685: }
2686:
1.204 maxv 2687: #ifdef POOL_REDZONE
2688: #if defined(_LP64)
2689: # define PRIME 0x9e37fffffffc0000UL
2690: #else /* defined(_LP64) */
2691: # define PRIME 0x9e3779b1
2692: #endif /* defined(_LP64) */
2693: #define STATIC_BYTE 0xFE
2694: CTASSERT(POOL_REDZONE_SIZE > 1);
2695:
2696: static inline uint8_t
2697: pool_pattern_generate(const void *p)
2698: {
2699: return (uint8_t)(((uintptr_t)p) * PRIME
2700: >> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT);
2701: }
2702:
2703: static void
2704: pool_redzone_init(struct pool *pp, size_t requested_size)
2705: {
2706: size_t nsz;
2707:
2708: if (pp->pr_roflags & PR_NOTOUCH) {
2709: pp->pr_reqsize = 0;
2710: pp->pr_redzone = false;
2711: return;
2712: }
2713:
2714: /*
2715: * We may have extended the requested size earlier; check if
2716: * there's naturally space in the padding for a red zone.
2717: */
2718: if (pp->pr_size - requested_size >= POOL_REDZONE_SIZE) {
2719: pp->pr_reqsize = requested_size;
2720: pp->pr_redzone = true;
2721: return;
2722: }
2723:
2724: /*
2725: * No space in the natural padding; check if we can extend a
2726: * bit the size of the pool.
2727: */
2728: nsz = roundup(pp->pr_size + POOL_REDZONE_SIZE, pp->pr_align);
2729: if (nsz <= pp->pr_alloc->pa_pagesz) {
2730: /* Ok, we can */
2731: pp->pr_size = nsz;
2732: pp->pr_reqsize = requested_size;
2733: pp->pr_redzone = true;
2734: } else {
2735: /* No space for a red zone... snif :'( */
2736: pp->pr_reqsize = 0;
2737: pp->pr_redzone = false;
2738: printf("pool redzone disabled for '%s'\n", pp->pr_wchan);
2739: }
2740: }
2741:
2742: static void
2743: pool_redzone_fill(struct pool *pp, void *p)
2744: {
2745: uint8_t *cp, pat;
2746: const uint8_t *ep;
2747:
2748: if (!pp->pr_redzone)
2749: return;
2750:
2751: cp = (uint8_t *)p + pp->pr_reqsize;
2752: ep = cp + POOL_REDZONE_SIZE;
2753:
2754: /*
2755: * We really don't want the first byte of the red zone to be '\0';
2756: * an off-by-one in a string may not be properly detected.
2757: */
2758: pat = pool_pattern_generate(cp);
2759: *cp = (pat == '\0') ? STATIC_BYTE: pat;
2760: cp++;
2761:
2762: while (cp < ep) {
2763: *cp = pool_pattern_generate(cp);
2764: cp++;
2765: }
2766: }
2767:
2768: static void
2769: pool_redzone_check(struct pool *pp, void *p)
2770: {
2771: uint8_t *cp, pat, expected;
2772: const uint8_t *ep;
2773:
2774: if (!pp->pr_redzone)
2775: return;
2776:
2777: cp = (uint8_t *)p + pp->pr_reqsize;
2778: ep = cp + POOL_REDZONE_SIZE;
2779:
2780: pat = pool_pattern_generate(cp);
2781: expected = (pat == '\0') ? STATIC_BYTE: pat;
2782: if (expected != *cp) {
2783: panic("%s: %p: 0x%02x != 0x%02x\n",
2784: __func__, cp, *cp, expected);
2785: }
2786: cp++;
2787:
2788: while (cp < ep) {
2789: expected = pool_pattern_generate(cp);
2790: if (*cp != expected) {
2791: panic("%s: %p: 0x%02x != 0x%02x\n",
2792: __func__, cp, *cp, expected);
2793: }
2794: cp++;
2795: }
2796: }
2797:
2798: #endif /* POOL_REDZONE */
2799:
2800:
1.66 thorpej 2801: #ifdef POOL_SUBPAGE
2802: /* Sub-page allocator, for machines with large hardware pages. */
2803: void *
2804: pool_subpage_alloc(struct pool *pp, int flags)
2805: {
1.134 ad 2806: return pool_get(&psppool, flags);
1.66 thorpej 2807: }
2808:
2809: void
2810: pool_subpage_free(struct pool *pp, void *v)
2811: {
2812: pool_put(&psppool, v);
2813: }
2814:
1.112 bjh21 2815: #endif /* POOL_SUBPAGE */
1.141 yamt 2816:
2817: #if defined(DDB)
2818: static bool
2819: pool_in_page(struct pool *pp, struct pool_item_header *ph, uintptr_t addr)
2820: {
2821:
2822: return (uintptr_t)ph->ph_page <= addr &&
2823: addr < (uintptr_t)ph->ph_page + pp->pr_alloc->pa_pagesz;
2824: }
2825:
1.143 yamt 2826: static bool
2827: pool_in_item(struct pool *pp, void *item, uintptr_t addr)
2828: {
2829:
2830: return (uintptr_t)item <= addr && addr < (uintptr_t)item + pp->pr_size;
2831: }
2832:
2833: static bool
2834: pool_in_cg(struct pool *pp, struct pool_cache_group *pcg, uintptr_t addr)
2835: {
2836: int i;
2837:
2838: if (pcg == NULL) {
2839: return false;
2840: }
1.144 yamt 2841: for (i = 0; i < pcg->pcg_avail; i++) {
1.143 yamt 2842: if (pool_in_item(pp, pcg->pcg_objects[i].pcgo_va, addr)) {
2843: return true;
2844: }
2845: }
2846: return false;
2847: }
2848:
2849: static bool
2850: pool_allocated(struct pool *pp, struct pool_item_header *ph, uintptr_t addr)
2851: {
2852:
2853: if ((pp->pr_roflags & PR_NOTOUCH) != 0) {
2854: unsigned int idx = pr_item_notouch_index(pp, ph, (void *)addr);
2855: pool_item_bitmap_t *bitmap =
2856: ph->ph_bitmap + (idx / BITMAP_SIZE);
2857: pool_item_bitmap_t mask = 1 << (idx & BITMAP_MASK);
2858:
2859: return (*bitmap & mask) == 0;
2860: } else {
2861: struct pool_item *pi;
2862:
2863: LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
2864: if (pool_in_item(pp, pi, addr)) {
2865: return false;
2866: }
2867: }
2868: return true;
2869: }
2870: }
2871:
1.141 yamt 2872: void
2873: pool_whatis(uintptr_t addr, void (*pr)(const char *, ...))
2874: {
2875: struct pool *pp;
2876:
1.145 ad 2877: TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
1.141 yamt 2878: struct pool_item_header *ph;
2879: uintptr_t item;
1.143 yamt 2880: bool allocated = true;
2881: bool incache = false;
2882: bool incpucache = false;
2883: char cpucachestr[32];
1.141 yamt 2884:
2885: if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
2886: LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
2887: if (pool_in_page(pp, ph, addr)) {
2888: goto found;
2889: }
2890: }
2891: LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
2892: if (pool_in_page(pp, ph, addr)) {
1.143 yamt 2893: allocated =
2894: pool_allocated(pp, ph, addr);
2895: goto found;
2896: }
2897: }
2898: LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
2899: if (pool_in_page(pp, ph, addr)) {
2900: allocated = false;
1.141 yamt 2901: goto found;
2902: }
2903: }
2904: continue;
2905: } else {
2906: ph = pr_find_pagehead_noalign(pp, (void *)addr);
2907: if (ph == NULL || !pool_in_page(pp, ph, addr)) {
2908: continue;
2909: }
1.143 yamt 2910: allocated = pool_allocated(pp, ph, addr);
1.141 yamt 2911: }
2912: found:
1.143 yamt 2913: if (allocated && pp->pr_cache) {
2914: pool_cache_t pc = pp->pr_cache;
2915: struct pool_cache_group *pcg;
2916: int i;
2917:
2918: for (pcg = pc->pc_fullgroups; pcg != NULL;
2919: pcg = pcg->pcg_next) {
2920: if (pool_in_cg(pp, pcg, addr)) {
2921: incache = true;
2922: goto print;
2923: }
2924: }
1.183 ad 2925: for (i = 0; i < __arraycount(pc->pc_cpus); i++) {
1.143 yamt 2926: pool_cache_cpu_t *cc;
2927:
2928: if ((cc = pc->pc_cpus[i]) == NULL) {
2929: continue;
2930: }
2931: if (pool_in_cg(pp, cc->cc_current, addr) ||
2932: pool_in_cg(pp, cc->cc_previous, addr)) {
2933: struct cpu_info *ci =
1.170 ad 2934: cpu_lookup(i);
1.143 yamt 2935:
2936: incpucache = true;
2937: snprintf(cpucachestr,
2938: sizeof(cpucachestr),
2939: "cached by CPU %u",
1.153 martin 2940: ci->ci_index);
1.143 yamt 2941: goto print;
2942: }
2943: }
2944: }
2945: print:
1.141 yamt 2946: item = (uintptr_t)ph->ph_page + ph->ph_off;
2947: item = item + rounddown(addr - item, pp->pr_size);
1.143 yamt 2948: (*pr)("%p is %p+%zu in POOL '%s' (%s)\n",
1.141 yamt 2949: (void *)addr, item, (size_t)(addr - item),
1.143 yamt 2950: pp->pr_wchan,
2951: incpucache ? cpucachestr :
2952: incache ? "cached" : allocated ? "allocated" : "free");
1.141 yamt 2953: }
2954: }
2955: #endif /* defined(DDB) */
1.203 joerg 2956:
2957: static int
2958: pool_sysctl(SYSCTLFN_ARGS)
2959: {
2960: struct pool_sysctl data;
2961: struct pool *pp;
2962: struct pool_cache *pc;
2963: pool_cache_cpu_t *cc;
2964: int error;
2965: size_t i, written;
2966:
2967: if (oldp == NULL) {
2968: *oldlenp = 0;
2969: TAILQ_FOREACH(pp, &pool_head, pr_poollist)
2970: *oldlenp += sizeof(data);
2971: return 0;
2972: }
2973:
2974: memset(&data, 0, sizeof(data));
2975: error = 0;
2976: written = 0;
2977: TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
2978: if (written + sizeof(data) > *oldlenp)
2979: break;
2980: strlcpy(data.pr_wchan, pp->pr_wchan, sizeof(data.pr_wchan));
2981: data.pr_pagesize = pp->pr_alloc->pa_pagesz;
2982: data.pr_flags = pp->pr_roflags | pp->pr_flags;
2983: #define COPY(field) data.field = pp->field
2984: COPY(pr_size);
2985:
2986: COPY(pr_itemsperpage);
2987: COPY(pr_nitems);
2988: COPY(pr_nout);
2989: COPY(pr_hardlimit);
2990: COPY(pr_npages);
2991: COPY(pr_minpages);
2992: COPY(pr_maxpages);
2993:
2994: COPY(pr_nget);
2995: COPY(pr_nfail);
2996: COPY(pr_nput);
2997: COPY(pr_npagealloc);
2998: COPY(pr_npagefree);
2999: COPY(pr_hiwat);
3000: COPY(pr_nidle);
3001: #undef COPY
3002:
3003: data.pr_cache_nmiss_pcpu = 0;
3004: data.pr_cache_nhit_pcpu = 0;
3005: if (pp->pr_cache) {
3006: pc = pp->pr_cache;
3007: data.pr_cache_meta_size = pc->pc_pcgsize;
3008: data.pr_cache_nfull = pc->pc_nfull;
3009: data.pr_cache_npartial = pc->pc_npart;
3010: data.pr_cache_nempty = pc->pc_nempty;
3011: data.pr_cache_ncontended = pc->pc_contended;
3012: data.pr_cache_nmiss_global = pc->pc_misses;
3013: data.pr_cache_nhit_global = pc->pc_hits;
3014: for (i = 0; i < pc->pc_ncpu; ++i) {
3015: cc = pc->pc_cpus[i];
3016: if (cc == NULL)
3017: continue;
1.206 knakahar 3018: data.pr_cache_nmiss_pcpu += cc->cc_misses;
3019: data.pr_cache_nhit_pcpu += cc->cc_hits;
1.203 joerg 3020: }
3021: } else {
3022: data.pr_cache_meta_size = 0;
3023: data.pr_cache_nfull = 0;
3024: data.pr_cache_npartial = 0;
3025: data.pr_cache_nempty = 0;
3026: data.pr_cache_ncontended = 0;
3027: data.pr_cache_nmiss_global = 0;
3028: data.pr_cache_nhit_global = 0;
3029: }
3030:
3031: error = sysctl_copyout(l, &data, oldp, sizeof(data));
3032: if (error)
3033: break;
3034: written += sizeof(data);
3035: oldp = (char *)oldp + sizeof(data);
3036: }
3037:
3038: *oldlenp = written;
3039: return error;
3040: }
3041:
3042: SYSCTL_SETUP(sysctl_pool_setup, "sysctl kern.pool setup")
3043: {
3044: const struct sysctlnode *rnode = NULL;
3045:
3046: sysctl_createv(clog, 0, NULL, &rnode,
3047: CTLFLAG_PERMANENT,
3048: CTLTYPE_STRUCT, "pool",
3049: SYSCTL_DESCR("Get pool statistics"),
3050: pool_sysctl, 0, NULL, 0,
3051: CTL_KERN, CTL_CREATE, CTL_EOL);
3052: }
CVSweb <webmaster@jp.NetBSD.org>