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