[BACK]Return to subr_pool.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/subr_pool.c, Revision 1.190.2.2

1.190.2.2! yamt        1: /*     $NetBSD: subr_pool.c,v 1.190.2.1 2012/04/17 00:08:28 yamt Exp $ */
1.1       pk          2:
                      3: /*-
1.183     ad          4:  * Copyright (c) 1997, 1999, 2000, 2002, 2007, 2008, 2010
                      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.134     ad         10:  * Simulation Facility, NASA Ames Research Center, and by Andrew Doran.
1.1       pk         11:  *
                     12:  * Redistribution and use in source and binary forms, with or without
                     13:  * modification, are permitted provided that the following conditions
                     14:  * are met:
                     15:  * 1. Redistributions of source code must retain the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer.
                     17:  * 2. Redistributions in binary form must reproduce the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer in the
                     19:  *    documentation and/or other materials provided with the distribution.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     22:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     23:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     24:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     25:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     26:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     27:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     28:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     29:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     30:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     31:  * POSSIBILITY OF SUCH DAMAGE.
                     32:  */
1.64      lukem      33:
                     34: #include <sys/cdefs.h>
1.190.2.2! yamt       35: __KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.190.2.1 2012/04/17 00:08:28 yamt Exp $");
1.24      scottr     36:
1.141     yamt       37: #include "opt_ddb.h"
1.28      thorpej    38: #include "opt_lockdebug.h"
1.1       pk         39:
                     40: #include <sys/param.h>
                     41: #include <sys/systm.h>
1.135     yamt       42: #include <sys/bitops.h>
1.1       pk         43: #include <sys/proc.h>
                     44: #include <sys/errno.h>
                     45: #include <sys/kernel.h>
1.190.2.1  yamt       46: #include <sys/vmem.h>
1.1       pk         47: #include <sys/pool.h>
1.20      thorpej    48: #include <sys/syslog.h>
1.125     ad         49: #include <sys/debug.h>
1.134     ad         50: #include <sys/lockdebug.h>
                     51: #include <sys/xcall.h>
                     52: #include <sys/cpu.h>
1.145     ad         53: #include <sys/atomic.h>
1.3       pk         54:
1.187     uebayasi   55: #include <uvm/uvm_extern.h>
1.3       pk         56:
1.1       pk         57: /*
                     58:  * Pool resource management utility.
1.3       pk         59:  *
1.88      chs        60:  * Memory is allocated in pages which are split into pieces according to
                     61:  * the pool item size. Each page is kept on one of three lists in the
                     62:  * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages',
                     63:  * for empty, full and partially-full pages respectively. The individual
                     64:  * pool items are on a linked list headed by `ph_itemlist' in each page
                     65:  * header. The memory for building the page list is either taken from
                     66:  * the allocated pages themselves (for small pool items) or taken from
                     67:  * an internal pool of page headers (`phpool').
1.1       pk         68:  */
                     69:
1.3       pk         70: /* List of all pools */
1.173     rmind      71: static TAILQ_HEAD(, pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
1.134     ad         72:
1.3       pk         73: /* Private pool for page header structures */
1.97      yamt       74: #define        PHPOOL_MAX      8
                     75: static struct pool phpool[PHPOOL_MAX];
1.135     yamt       76: #define        PHPOOL_FREELIST_NELEM(idx) \
                     77:        (((idx) == 0) ? 0 : BITMAP_SIZE * (1 << (idx)))
1.3       pk         78:
1.62      bjh21      79: #ifdef POOL_SUBPAGE
                     80: /* Pool of subpages for use by normal pools. */
                     81: static struct pool psppool;
                     82: #endif
                     83:
1.98      yamt       84: static void *pool_page_alloc_meta(struct pool *, int);
                     85: static void pool_page_free_meta(struct pool *, void *);
                     86:
                     87: /* allocator for pool metadata */
1.134     ad         88: struct pool_allocator pool_allocator_meta = {
1.190.2.1  yamt       89:        .pa_alloc = pool_page_alloc_meta,
                     90:        .pa_free = pool_page_free_meta,
                     91:        .pa_pagesz = 0
1.98      yamt       92: };
                     93:
1.3       pk         94: /* # of seconds to retain page after last use */
                     95: int pool_inactive_time = 10;
                     96:
                     97: /* Next candidate for drainage (see pool_drain()) */
1.23      thorpej    98: static struct pool     *drainpp;
                     99:
1.134     ad        100: /* This lock protects both pool_head and drainpp. */
                    101: static kmutex_t pool_head_lock;
                    102: static kcondvar_t pool_busy;
1.3       pk        103:
1.178     elad      104: /* This lock protects initialization of a potentially shared pool allocator */
                    105: static kmutex_t pool_allocator_lock;
                    106:
1.135     yamt      107: typedef uint32_t pool_item_bitmap_t;
                    108: #define        BITMAP_SIZE     (CHAR_BIT * sizeof(pool_item_bitmap_t))
                    109: #define        BITMAP_MASK     (BITMAP_SIZE - 1)
1.99      yamt      110:
1.3       pk        111: struct pool_item_header {
                    112:        /* Page headers */
1.88      chs       113:        LIST_ENTRY(pool_item_header)
1.3       pk        114:                                ph_pagelist;    /* pool page list */
1.88      chs       115:        SPLAY_ENTRY(pool_item_header)
                    116:                                ph_node;        /* Off-page page headers */
1.128     christos  117:        void *                  ph_page;        /* this page's address */
1.151     yamt      118:        uint32_t                ph_time;        /* last referenced */
1.135     yamt      119:        uint16_t                ph_nmissing;    /* # of chunks in use */
1.141     yamt      120:        uint16_t                ph_off;         /* start offset in page */
1.97      yamt      121:        union {
                    122:                /* !PR_NOTOUCH */
                    123:                struct {
1.102     chs       124:                        LIST_HEAD(, pool_item)
1.97      yamt      125:                                phu_itemlist;   /* chunk list for this page */
                    126:                } phu_normal;
                    127:                /* PR_NOTOUCH */
                    128:                struct {
1.141     yamt      129:                        pool_item_bitmap_t phu_bitmap[1];
1.97      yamt      130:                } phu_notouch;
                    131:        } ph_u;
1.3       pk        132: };
1.97      yamt      133: #define        ph_itemlist     ph_u.phu_normal.phu_itemlist
1.135     yamt      134: #define        ph_bitmap       ph_u.phu_notouch.phu_bitmap
1.3       pk        135:
1.1       pk        136: struct pool_item {
1.3       pk        137: #ifdef DIAGNOSTIC
1.82      thorpej   138:        u_int pi_magic;
1.33      chs       139: #endif
1.134     ad        140: #define        PI_MAGIC 0xdeaddeadU
1.3       pk        141:        /* Other entries use only this list entry */
1.102     chs       142:        LIST_ENTRY(pool_item)   pi_list;
1.3       pk        143: };
                    144:
1.53      thorpej   145: #define        POOL_NEEDS_CATCHUP(pp)                                          \
                    146:        ((pp)->pr_nitems < (pp)->pr_minitems)
                    147:
1.43      thorpej   148: /*
                    149:  * Pool cache management.
                    150:  *
                    151:  * Pool caches provide a way for constructed objects to be cached by the
                    152:  * pool subsystem.  This can lead to performance improvements by avoiding
                    153:  * needless object construction/destruction; it is deferred until absolutely
                    154:  * necessary.
                    155:  *
1.134     ad        156:  * Caches are grouped into cache groups.  Each cache group references up
                    157:  * to PCG_NUMOBJECTS constructed objects.  When a cache allocates an
                    158:  * object from the pool, it calls the object's constructor and places it
                    159:  * into a cache group.  When a cache group frees an object back to the
                    160:  * pool, it first calls the object's destructor.  This allows the object
                    161:  * to persist in constructed form while freed to the cache.
                    162:  *
                    163:  * The pool references each cache, so that when a pool is drained by the
                    164:  * pagedaemon, it can drain each individual cache as well.  Each time a
                    165:  * cache is drained, the most idle cache group is freed to the pool in
                    166:  * its entirety.
1.43      thorpej   167:  *
                    168:  * Pool caches are layed on top of pools.  By layering them, we can avoid
                    169:  * the complexity of cache management for pools which would not benefit
                    170:  * from it.
                    171:  */
                    172:
1.142     ad        173: static struct pool pcg_normal_pool;
                    174: static struct pool pcg_large_pool;
1.134     ad        175: static struct pool cache_pool;
                    176: static struct pool cache_cpu_pool;
1.3       pk        177:
1.189     pooka     178: pool_cache_t pnbuf_cache;      /* pathname buffer cache */
                    179:
1.145     ad        180: /* List of all caches. */
                    181: TAILQ_HEAD(,pool_cache) pool_cache_head =
                    182:     TAILQ_HEAD_INITIALIZER(pool_cache_head);
                    183:
1.162     ad        184: int pool_cache_disable;                /* global disable for caching */
1.169     yamt      185: static const pcg_t pcg_dummy;  /* zero sized: always empty, yet always full */
1.145     ad        186:
1.162     ad        187: static bool    pool_cache_put_slow(pool_cache_cpu_t *, int,
                    188:                                    void *);
                    189: static bool    pool_cache_get_slow(pool_cache_cpu_t *, int,
                    190:                                    void **, paddr_t *, int);
1.134     ad        191: static void    pool_cache_cpu_init1(struct cpu_info *, pool_cache_t);
                    192: static void    pool_cache_invalidate_groups(pool_cache_t, pcg_t *);
1.175     jym       193: static void    pool_cache_invalidate_cpu(pool_cache_t, u_int);
1.134     ad        194: static void    pool_cache_xcall(pool_cache_t);
1.3       pk        195:
1.42      thorpej   196: static int     pool_catchup(struct pool *);
1.128     christos  197: static void    pool_prime_page(struct pool *, void *,
1.55      thorpej   198:                    struct pool_item_header *);
1.88      chs       199: static void    pool_update_curpage(struct pool *);
1.66      thorpej   200:
1.113     yamt      201: static int     pool_grow(struct pool *, int);
1.117     yamt      202: static void    *pool_allocator_alloc(struct pool *, int);
                    203: static void    pool_allocator_free(struct pool *, void *);
1.3       pk        204:
1.97      yamt      205: static void pool_print_pagelist(struct pool *, struct pool_pagelist *,
1.88      chs       206:        void (*)(const char *, ...));
1.42      thorpej   207: static void pool_print1(struct pool *, const char *,
                    208:        void (*)(const char *, ...));
1.3       pk        209:
1.88      chs       210: static int pool_chk_page(struct pool *, const char *,
                    211:                         struct pool_item_header *);
                    212:
1.135     yamt      213: static inline unsigned int
1.97      yamt      214: pr_item_notouch_index(const struct pool *pp, const struct pool_item_header *ph,
                    215:     const void *v)
                    216: {
                    217:        const char *cp = v;
1.135     yamt      218:        unsigned int idx;
1.97      yamt      219:
                    220:        KASSERT(pp->pr_roflags & PR_NOTOUCH);
1.128     christos  221:        idx = (cp - (char *)ph->ph_page - ph->ph_off) / pp->pr_size;
1.97      yamt      222:        KASSERT(idx < pp->pr_itemsperpage);
                    223:        return idx;
                    224: }
                    225:
1.110     perry     226: static inline void
1.97      yamt      227: pr_item_notouch_put(const struct pool *pp, struct pool_item_header *ph,
                    228:     void *obj)
                    229: {
1.135     yamt      230:        unsigned int idx = pr_item_notouch_index(pp, ph, obj);
                    231:        pool_item_bitmap_t *bitmap = ph->ph_bitmap + (idx / BITMAP_SIZE);
                    232:        pool_item_bitmap_t mask = 1 << (idx & BITMAP_MASK);
1.97      yamt      233:
1.135     yamt      234:        KASSERT((*bitmap & mask) == 0);
                    235:        *bitmap |= mask;
1.97      yamt      236: }
                    237:
1.110     perry     238: static inline void *
1.97      yamt      239: pr_item_notouch_get(const struct pool *pp, struct pool_item_header *ph)
                    240: {
1.135     yamt      241:        pool_item_bitmap_t *bitmap = ph->ph_bitmap;
                    242:        unsigned int idx;
                    243:        int i;
1.97      yamt      244:
1.135     yamt      245:        for (i = 0; ; i++) {
                    246:                int bit;
1.97      yamt      247:
1.135     yamt      248:                KASSERT((i * BITMAP_SIZE) < pp->pr_itemsperpage);
                    249:                bit = ffs32(bitmap[i]);
                    250:                if (bit) {
                    251:                        pool_item_bitmap_t mask;
                    252:
                    253:                        bit--;
                    254:                        idx = (i * BITMAP_SIZE) + bit;
                    255:                        mask = 1 << bit;
                    256:                        KASSERT((bitmap[i] & mask) != 0);
                    257:                        bitmap[i] &= ~mask;
                    258:                        break;
                    259:                }
                    260:        }
                    261:        KASSERT(idx < pp->pr_itemsperpage);
1.128     christos  262:        return (char *)ph->ph_page + ph->ph_off + idx * pp->pr_size;
1.97      yamt      263: }
                    264:
1.135     yamt      265: static inline void
1.141     yamt      266: pr_item_notouch_init(const struct pool *pp, struct pool_item_header *ph)
1.135     yamt      267: {
                    268:        pool_item_bitmap_t *bitmap = ph->ph_bitmap;
                    269:        const int n = howmany(pp->pr_itemsperpage, BITMAP_SIZE);
                    270:        int i;
                    271:
                    272:        for (i = 0; i < n; i++) {
                    273:                bitmap[i] = (pool_item_bitmap_t)-1;
                    274:        }
                    275: }
                    276:
1.110     perry     277: static inline int
1.88      chs       278: phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
                    279: {
1.121     yamt      280:
                    281:        /*
                    282:         * we consider pool_item_header with smaller ph_page bigger.
                    283:         * (this unnatural ordering is for the benefit of pr_find_pagehead.)
                    284:         */
                    285:
1.88      chs       286:        if (a->ph_page < b->ph_page)
1.121     yamt      287:                return (1);
                    288:        else if (a->ph_page > b->ph_page)
1.88      chs       289:                return (-1);
                    290:        else
                    291:                return (0);
                    292: }
                    293:
                    294: SPLAY_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare);
                    295: SPLAY_GENERATE(phtree, pool_item_header, ph_node, phtree_compare);
                    296:
1.141     yamt      297: static inline struct pool_item_header *
                    298: pr_find_pagehead_noalign(struct pool *pp, void *v)
                    299: {
                    300:        struct pool_item_header *ph, tmp;
                    301:
                    302:        tmp.ph_page = (void *)(uintptr_t)v;
                    303:        ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
                    304:        if (ph == NULL) {
                    305:                ph = SPLAY_ROOT(&pp->pr_phtree);
                    306:                if (ph != NULL && phtree_compare(&tmp, ph) >= 0) {
                    307:                        ph = SPLAY_NEXT(phtree, &pp->pr_phtree, ph);
                    308:                }
                    309:                KASSERT(ph == NULL || phtree_compare(&tmp, ph) < 0);
                    310:        }
                    311:
                    312:        return ph;
                    313: }
                    314:
1.3       pk        315: /*
1.121     yamt      316:  * Return the pool page header based on item address.
1.3       pk        317:  */
1.110     perry     318: static inline struct pool_item_header *
1.121     yamt      319: pr_find_pagehead(struct pool *pp, void *v)
1.3       pk        320: {
1.88      chs       321:        struct pool_item_header *ph, tmp;
1.3       pk        322:
1.121     yamt      323:        if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1.141     yamt      324:                ph = pr_find_pagehead_noalign(pp, v);
1.121     yamt      325:        } else {
1.128     christos  326:                void *page =
                    327:                    (void *)((uintptr_t)v & pp->pr_alloc->pa_pagemask);
1.121     yamt      328:
                    329:                if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
1.128     christos  330:                        ph = (struct pool_item_header *)((char *)page + pp->pr_phoffset);
1.121     yamt      331:                } else {
                    332:                        tmp.ph_page = page;
                    333:                        ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
                    334:                }
                    335:        }
1.3       pk        336:
1.121     yamt      337:        KASSERT(ph == NULL || ((pp->pr_roflags & PR_PHINPAGE) != 0) ||
1.128     christos  338:            ((char *)ph->ph_page <= (char *)v &&
                    339:            (char *)v < (char *)ph->ph_page + pp->pr_alloc->pa_pagesz));
1.88      chs       340:        return ph;
1.3       pk        341: }
                    342:
1.101     thorpej   343: static void
                    344: pr_pagelist_free(struct pool *pp, struct pool_pagelist *pq)
                    345: {
                    346:        struct pool_item_header *ph;
                    347:
                    348:        while ((ph = LIST_FIRST(pq)) != NULL) {
                    349:                LIST_REMOVE(ph, ph_pagelist);
                    350:                pool_allocator_free(pp, ph->ph_page);
1.134     ad        351:                if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1.101     thorpej   352:                        pool_put(pp->pr_phpool, ph);
                    353:        }
                    354: }
                    355:
1.3       pk        356: /*
                    357:  * Remove a page from the pool.
                    358:  */
1.110     perry     359: static inline void
1.61      chs       360: pr_rmpage(struct pool *pp, struct pool_item_header *ph,
                    361:      struct pool_pagelist *pq)
1.3       pk        362: {
                    363:
1.134     ad        364:        KASSERT(mutex_owned(&pp->pr_lock));
1.91      yamt      365:
1.3       pk        366:        /*
1.7       thorpej   367:         * If the page was idle, decrement the idle page count.
1.3       pk        368:         */
1.6       thorpej   369:        if (ph->ph_nmissing == 0) {
                    370: #ifdef DIAGNOSTIC
                    371:                if (pp->pr_nidle == 0)
                    372:                        panic("pr_rmpage: nidle inconsistent");
1.20      thorpej   373:                if (pp->pr_nitems < pp->pr_itemsperpage)
                    374:                        panic("pr_rmpage: nitems inconsistent");
1.6       thorpej   375: #endif
                    376:                pp->pr_nidle--;
                    377:        }
1.7       thorpej   378:
1.20      thorpej   379:        pp->pr_nitems -= pp->pr_itemsperpage;
                    380:
1.7       thorpej   381:        /*
1.101     thorpej   382:         * Unlink the page from the pool and queue it for release.
1.7       thorpej   383:         */
1.88      chs       384:        LIST_REMOVE(ph, ph_pagelist);
1.91      yamt      385:        if ((pp->pr_roflags & PR_PHINPAGE) == 0)
                    386:                SPLAY_REMOVE(phtree, &pp->pr_phtree, ph);
1.101     thorpej   387:        LIST_INSERT_HEAD(pq, ph, ph_pagelist);
                    388:
1.7       thorpej   389:        pp->pr_npages--;
                    390:        pp->pr_npagefree++;
1.6       thorpej   391:
1.88      chs       392:        pool_update_curpage(pp);
1.3       pk        393: }
                    394:
                    395: /*
1.94      simonb    396:  * Initialize all the pools listed in the "pools" link set.
                    397:  */
                    398: void
1.117     yamt      399: pool_subsystem_init(void)
1.94      simonb    400: {
1.190.2.1  yamt      401:        size_t size;
                    402:        int idx;
1.94      simonb    403:
1.134     ad        404:        mutex_init(&pool_head_lock, MUTEX_DEFAULT, IPL_NONE);
1.179     mlelstv   405:        mutex_init(&pool_allocator_lock, MUTEX_DEFAULT, IPL_NONE);
1.134     ad        406:        cv_init(&pool_busy, "poolbusy");
                    407:
1.190.2.1  yamt      408:        /*
                    409:         * Initialize private page header pool and cache magazine pool if we
                    410:         * haven't done so yet.
                    411:         */
                    412:        for (idx = 0; idx < PHPOOL_MAX; idx++) {
                    413:                static char phpool_names[PHPOOL_MAX][6+1+6+1];
                    414:                int nelem;
                    415:                size_t sz;
                    416:
                    417:                nelem = PHPOOL_FREELIST_NELEM(idx);
                    418:                snprintf(phpool_names[idx], sizeof(phpool_names[idx]),
                    419:                    "phpool-%d", nelem);
                    420:                sz = sizeof(struct pool_item_header);
                    421:                if (nelem) {
                    422:                        sz = offsetof(struct pool_item_header,
                    423:                            ph_bitmap[howmany(nelem, BITMAP_SIZE)]);
                    424:                }
                    425:                pool_init(&phpool[idx], sz, 0, 0, 0,
                    426:                    phpool_names[idx], &pool_allocator_meta, IPL_VM);
1.117     yamt      427:        }
1.190.2.1  yamt      428: #ifdef POOL_SUBPAGE
                    429:        pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0,
                    430:            PR_RECURSIVE, "psppool", &pool_allocator_meta, IPL_VM);
                    431: #endif
                    432:
                    433:        size = sizeof(pcg_t) +
                    434:            (PCG_NOBJECTS_NORMAL - 1) * sizeof(pcgpair_t);
                    435:        pool_init(&pcg_normal_pool, size, coherency_unit, 0, 0,
                    436:            "pcgnormal", &pool_allocator_meta, IPL_VM);
                    437:
                    438:        size = sizeof(pcg_t) +
                    439:            (PCG_NOBJECTS_LARGE - 1) * sizeof(pcgpair_t);
                    440:        pool_init(&pcg_large_pool, size, coherency_unit, 0, 0,
                    441:            "pcglarge", &pool_allocator_meta, IPL_VM);
1.134     ad        442:
1.156     ad        443:        pool_init(&cache_pool, sizeof(struct pool_cache), coherency_unit,
1.190.2.1  yamt      444:            0, 0, "pcache", &pool_allocator_meta, IPL_NONE);
1.134     ad        445:
1.156     ad        446:        pool_init(&cache_cpu_pool, sizeof(pool_cache_cpu_t), coherency_unit,
1.190.2.1  yamt      447:            0, 0, "pcachecpu", &pool_allocator_meta, IPL_NONE);
1.94      simonb    448: }
                    449:
                    450: /*
1.3       pk        451:  * Initialize the given pool resource structure.
                    452:  *
                    453:  * We export this routine to allow other kernel parts to declare
1.190.2.2! yamt      454:  * static pools that must be initialized before kmem(9) is available.
1.3       pk        455:  */
                    456: void
1.42      thorpej   457: pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
1.129     ad        458:     const char *wchan, struct pool_allocator *palloc, int ipl)
1.3       pk        459: {
1.116     simonb    460:        struct pool *pp1;
1.92      enami     461:        size_t trysize, phsize;
1.134     ad        462:        int off, slack;
1.3       pk        463:
1.116     simonb    464: #ifdef DEBUG
                    465:        /*
                    466:         * Check that the pool hasn't already been initialised and
                    467:         * added to the list of all pools.
                    468:         */
1.145     ad        469:        TAILQ_FOREACH(pp1, &pool_head, pr_poollist) {
1.116     simonb    470:                if (pp == pp1)
                    471:                        panic("pool_init: pool %s already initialised",
                    472:                            wchan);
                    473:        }
                    474: #endif
                    475:
1.66      thorpej   476:        if (palloc == NULL)
                    477:                palloc = &pool_allocator_kmem;
1.112     bjh21     478: #ifdef POOL_SUBPAGE
                    479:        if (size > palloc->pa_pagesz) {
                    480:                if (palloc == &pool_allocator_kmem)
                    481:                        palloc = &pool_allocator_kmem_fullpage;
                    482:                else if (palloc == &pool_allocator_nointr)
                    483:                        palloc = &pool_allocator_nointr_fullpage;
                    484:        }
1.66      thorpej   485: #endif /* POOL_SUBPAGE */
1.180     mlelstv   486:        if (!cold)
                    487:                mutex_enter(&pool_allocator_lock);
1.178     elad      488:        if (palloc->pa_refcnt++ == 0) {
1.112     bjh21     489:                if (palloc->pa_pagesz == 0)
1.66      thorpej   490:                        palloc->pa_pagesz = PAGE_SIZE;
                    491:
                    492:                TAILQ_INIT(&palloc->pa_list);
                    493:
1.134     ad        494:                mutex_init(&palloc->pa_lock, MUTEX_DEFAULT, IPL_VM);
1.66      thorpej   495:                palloc->pa_pagemask = ~(palloc->pa_pagesz - 1);
                    496:                palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1;
1.4       thorpej   497:        }
1.180     mlelstv   498:        if (!cold)
                    499:                mutex_exit(&pool_allocator_lock);
1.3       pk        500:
                    501:        if (align == 0)
                    502:                align = ALIGN(1);
1.14      thorpej   503:
1.120     yamt      504:        if ((flags & PR_NOTOUCH) == 0 && size < sizeof(struct pool_item))
1.14      thorpej   505:                size = sizeof(struct pool_item);
1.3       pk        506:
1.78      thorpej   507:        size = roundup(size, align);
1.66      thorpej   508: #ifdef DIAGNOSTIC
                    509:        if (size > palloc->pa_pagesz)
1.121     yamt      510:                panic("pool_init: pool item size (%zu) too large", size);
1.66      thorpej   511: #endif
1.35      pk        512:
1.3       pk        513:        /*
                    514:         * Initialize the pool structure.
                    515:         */
1.88      chs       516:        LIST_INIT(&pp->pr_emptypages);
                    517:        LIST_INIT(&pp->pr_fullpages);
                    518:        LIST_INIT(&pp->pr_partpages);
1.134     ad        519:        pp->pr_cache = NULL;
1.3       pk        520:        pp->pr_curpage = NULL;
                    521:        pp->pr_npages = 0;
                    522:        pp->pr_minitems = 0;
                    523:        pp->pr_minpages = 0;
                    524:        pp->pr_maxpages = UINT_MAX;
1.20      thorpej   525:        pp->pr_roflags = flags;
                    526:        pp->pr_flags = 0;
1.35      pk        527:        pp->pr_size = size;
1.3       pk        528:        pp->pr_align = align;
                    529:        pp->pr_wchan = wchan;
1.66      thorpej   530:        pp->pr_alloc = palloc;
1.20      thorpej   531:        pp->pr_nitems = 0;
                    532:        pp->pr_nout = 0;
                    533:        pp->pr_hardlimit = UINT_MAX;
                    534:        pp->pr_hardlimit_warning = NULL;
1.31      thorpej   535:        pp->pr_hardlimit_ratecap.tv_sec = 0;
                    536:        pp->pr_hardlimit_ratecap.tv_usec = 0;
                    537:        pp->pr_hardlimit_warning_last.tv_sec = 0;
                    538:        pp->pr_hardlimit_warning_last.tv_usec = 0;
1.68      thorpej   539:        pp->pr_drain_hook = NULL;
                    540:        pp->pr_drain_hook_arg = NULL;
1.125     ad        541:        pp->pr_freecheck = NULL;
1.3       pk        542:
                    543:        /*
                    544:         * Decide whether to put the page header off page to avoid
1.92      enami     545:         * wasting too large a part of the page or too big item.
                    546:         * Off-page page headers go on a hash table, so we can match
                    547:         * a returned item with its header based on the page address.
                    548:         * We use 1/16 of the page size and about 8 times of the item
                    549:         * size as the threshold (XXX: tune)
                    550:         *
                    551:         * However, we'll put the header into the page if we can put
                    552:         * it without wasting any items.
                    553:         *
                    554:         * Silently enforce `0 <= ioff < align'.
1.3       pk        555:         */
1.92      enami     556:        pp->pr_itemoffset = ioff %= align;
                    557:        /* See the comment below about reserved bytes. */
                    558:        trysize = palloc->pa_pagesz - ((align - ioff) % align);
                    559:        phsize = ALIGN(sizeof(struct pool_item_header));
1.121     yamt      560:        if ((pp->pr_roflags & (PR_NOTOUCH | PR_NOALIGN)) == 0 &&
1.97      yamt      561:            (pp->pr_size < MIN(palloc->pa_pagesz / 16, phsize << 3) ||
                    562:            trysize / pp->pr_size == (trysize - phsize) / pp->pr_size)) {
1.3       pk        563:                /* Use the end of the page for the page header */
1.20      thorpej   564:                pp->pr_roflags |= PR_PHINPAGE;
1.92      enami     565:                pp->pr_phoffset = off = palloc->pa_pagesz - phsize;
1.2       pk        566:        } else {
1.3       pk        567:                /* The page header will be taken from our page header pool */
                    568:                pp->pr_phoffset = 0;
1.66      thorpej   569:                off = palloc->pa_pagesz;
1.88      chs       570:                SPLAY_INIT(&pp->pr_phtree);
1.2       pk        571:        }
1.1       pk        572:
1.3       pk        573:        /*
                    574:         * Alignment is to take place at `ioff' within the item. This means
                    575:         * we must reserve up to `align - 1' bytes on the page to allow
                    576:         * appropriate positioning of each item.
                    577:         */
                    578:        pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
1.43      thorpej   579:        KASSERT(pp->pr_itemsperpage != 0);
1.97      yamt      580:        if ((pp->pr_roflags & PR_NOTOUCH)) {
                    581:                int idx;
                    582:
                    583:                for (idx = 0; pp->pr_itemsperpage > PHPOOL_FREELIST_NELEM(idx);
                    584:                    idx++) {
                    585:                        /* nothing */
                    586:                }
                    587:                if (idx >= PHPOOL_MAX) {
                    588:                        /*
                    589:                         * if you see this panic, consider to tweak
                    590:                         * PHPOOL_MAX and PHPOOL_FREELIST_NELEM.
                    591:                         */
                    592:                        panic("%s: too large itemsperpage(%d) for PR_NOTOUCH",
                    593:                            pp->pr_wchan, pp->pr_itemsperpage);
                    594:                }
                    595:                pp->pr_phpool = &phpool[idx];
                    596:        } else if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
                    597:                pp->pr_phpool = &phpool[0];
                    598:        }
                    599: #if defined(DIAGNOSTIC)
                    600:        else {
                    601:                pp->pr_phpool = NULL;
                    602:        }
                    603: #endif
1.3       pk        604:
                    605:        /*
                    606:         * Use the slack between the chunks and the page header
                    607:         * for "cache coloring".
                    608:         */
                    609:        slack = off - pp->pr_itemsperpage * pp->pr_size;
                    610:        pp->pr_maxcolor = (slack / align) * align;
                    611:        pp->pr_curcolor = 0;
                    612:
                    613:        pp->pr_nget = 0;
                    614:        pp->pr_nfail = 0;
                    615:        pp->pr_nput = 0;
                    616:        pp->pr_npagealloc = 0;
                    617:        pp->pr_npagefree = 0;
1.1       pk        618:        pp->pr_hiwat = 0;
1.8       thorpej   619:        pp->pr_nidle = 0;
1.134     ad        620:        pp->pr_refcnt = 0;
1.3       pk        621:
1.157     ad        622:        mutex_init(&pp->pr_lock, MUTEX_DEFAULT, ipl);
1.134     ad        623:        cv_init(&pp->pr_cv, wchan);
                    624:        pp->pr_ipl = ipl;
1.1       pk        625:
1.145     ad        626:        /* Insert into the list of all pools. */
1.181     mlelstv   627:        if (!cold)
1.134     ad        628:                mutex_enter(&pool_head_lock);
1.145     ad        629:        TAILQ_FOREACH(pp1, &pool_head, pr_poollist) {
                    630:                if (strcmp(pp1->pr_wchan, pp->pr_wchan) > 0)
                    631:                        break;
                    632:        }
                    633:        if (pp1 == NULL)
                    634:                TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
                    635:        else
                    636:                TAILQ_INSERT_BEFORE(pp1, pp, pr_poollist);
1.181     mlelstv   637:        if (!cold)
1.134     ad        638:                mutex_exit(&pool_head_lock);
                    639:
1.167     skrll     640:        /* Insert this into the list of pools using this allocator. */
1.181     mlelstv   641:        if (!cold)
1.134     ad        642:                mutex_enter(&palloc->pa_lock);
1.145     ad        643:        TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
1.181     mlelstv   644:        if (!cold)
1.134     ad        645:                mutex_exit(&palloc->pa_lock);
1.1       pk        646: }
                    647:
                    648: /*
                    649:  * De-commision a pool resource.
                    650:  */
                    651: void
1.42      thorpej   652: pool_destroy(struct pool *pp)
1.1       pk        653: {
1.101     thorpej   654:        struct pool_pagelist pq;
1.3       pk        655:        struct pool_item_header *ph;
1.43      thorpej   656:
1.101     thorpej   657:        /* Remove from global pool list */
1.134     ad        658:        mutex_enter(&pool_head_lock);
                    659:        while (pp->pr_refcnt != 0)
                    660:                cv_wait(&pool_busy, &pool_head_lock);
1.145     ad        661:        TAILQ_REMOVE(&pool_head, pp, pr_poollist);
1.101     thorpej   662:        if (drainpp == pp)
                    663:                drainpp = NULL;
1.134     ad        664:        mutex_exit(&pool_head_lock);
1.101     thorpej   665:
                    666:        /* Remove this pool from its allocator's list of pools. */
1.134     ad        667:        mutex_enter(&pp->pr_alloc->pa_lock);
1.66      thorpej   668:        TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list);
1.134     ad        669:        mutex_exit(&pp->pr_alloc->pa_lock);
1.66      thorpej   670:
1.178     elad      671:        mutex_enter(&pool_allocator_lock);
                    672:        if (--pp->pr_alloc->pa_refcnt == 0)
                    673:                mutex_destroy(&pp->pr_alloc->pa_lock);
                    674:        mutex_exit(&pool_allocator_lock);
                    675:
1.134     ad        676:        mutex_enter(&pp->pr_lock);
1.101     thorpej   677:
1.134     ad        678:        KASSERT(pp->pr_cache == NULL);
1.3       pk        679:
                    680: #ifdef DIAGNOSTIC
1.20      thorpej   681:        if (pp->pr_nout != 0) {
1.80      provos    682:                panic("pool_destroy: pool busy: still out: %u",
1.20      thorpej   683:                    pp->pr_nout);
1.3       pk        684:        }
                    685: #endif
1.1       pk        686:
1.101     thorpej   687:        KASSERT(LIST_EMPTY(&pp->pr_fullpages));
                    688:        KASSERT(LIST_EMPTY(&pp->pr_partpages));
                    689:
1.3       pk        690:        /* Remove all pages */
1.101     thorpej   691:        LIST_INIT(&pq);
1.88      chs       692:        while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1.101     thorpej   693:                pr_rmpage(pp, ph, &pq);
                    694:
1.134     ad        695:        mutex_exit(&pp->pr_lock);
1.3       pk        696:
1.101     thorpej   697:        pr_pagelist_free(pp, &pq);
1.134     ad        698:        cv_destroy(&pp->pr_cv);
                    699:        mutex_destroy(&pp->pr_lock);
1.1       pk        700: }
                    701:
1.68      thorpej   702: void
                    703: pool_set_drain_hook(struct pool *pp, void (*fn)(void *, int), void *arg)
                    704: {
                    705:
                    706:        /* XXX no locking -- must be used just after pool_init() */
                    707: #ifdef DIAGNOSTIC
                    708:        if (pp->pr_drain_hook != NULL)
                    709:                panic("pool_set_drain_hook(%s): already set", pp->pr_wchan);
                    710: #endif
                    711:        pp->pr_drain_hook = fn;
                    712:        pp->pr_drain_hook_arg = arg;
                    713: }
                    714:
1.88      chs       715: static struct pool_item_header *
1.128     christos  716: pool_alloc_item_header(struct pool *pp, void *storage, int flags)
1.55      thorpej   717: {
                    718:        struct pool_item_header *ph;
                    719:
                    720:        if ((pp->pr_roflags & PR_PHINPAGE) != 0)
1.128     christos  721:                ph = (struct pool_item_header *) ((char *)storage + pp->pr_phoffset);
1.134     ad        722:        else
1.97      yamt      723:                ph = pool_get(pp->pr_phpool, flags);
1.55      thorpej   724:
                    725:        return (ph);
                    726: }
1.1       pk        727:
                    728: /*
1.134     ad        729:  * Grab an item from the pool.
1.1       pk        730:  */
1.3       pk        731: void *
1.56      sommerfe  732: pool_get(struct pool *pp, int flags)
1.1       pk        733: {
                    734:        struct pool_item *pi;
1.3       pk        735:        struct pool_item_header *ph;
1.55      thorpej   736:        void *v;
1.1       pk        737:
1.2       pk        738: #ifdef DIAGNOSTIC
1.184     rmind     739:        if (pp->pr_itemsperpage == 0)
                    740:                panic("pool_get: pool '%s': pr_itemsperpage is zero, "
                    741:                    "pool not initialized?", pp->pr_wchan);
1.185     rmind     742:        if ((cpu_intr_p() || cpu_softintr_p()) && pp->pr_ipl == IPL_NONE &&
                    743:            !cold && panicstr == NULL)
1.184     rmind     744:                panic("pool '%s' is IPL_NONE, but called from "
                    745:                    "interrupt context\n", pp->pr_wchan);
                    746: #endif
1.155     ad        747:        if (flags & PR_WAITOK) {
1.154     yamt      748:                ASSERT_SLEEPABLE();
1.155     ad        749:        }
1.1       pk        750:
1.134     ad        751:        mutex_enter(&pp->pr_lock);
1.20      thorpej   752:  startover:
                    753:        /*
                    754:         * Check to see if we've reached the hard limit.  If we have,
                    755:         * and we can wait, then wait until an item has been returned to
                    756:         * the pool.
                    757:         */
                    758: #ifdef DIAGNOSTIC
1.34      thorpej   759:        if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
1.134     ad        760:                mutex_exit(&pp->pr_lock);
1.20      thorpej   761:                panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
                    762:        }
                    763: #endif
1.34      thorpej   764:        if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
1.68      thorpej   765:                if (pp->pr_drain_hook != NULL) {
                    766:                        /*
                    767:                         * Since the drain hook is going to free things
                    768:                         * back to the pool, unlock, call the hook, re-lock,
                    769:                         * and check the hardlimit condition again.
                    770:                         */
1.134     ad        771:                        mutex_exit(&pp->pr_lock);
1.68      thorpej   772:                        (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
1.134     ad        773:                        mutex_enter(&pp->pr_lock);
1.68      thorpej   774:                        if (pp->pr_nout < pp->pr_hardlimit)
                    775:                                goto startover;
                    776:                }
                    777:
1.29      sommerfe  778:                if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
1.20      thorpej   779:                        /*
                    780:                         * XXX: A warning isn't logged in this case.  Should
                    781:                         * it be?
                    782:                         */
                    783:                        pp->pr_flags |= PR_WANTED;
1.134     ad        784:                        cv_wait(&pp->pr_cv, &pp->pr_lock);
1.20      thorpej   785:                        goto startover;
                    786:                }
1.31      thorpej   787:
                    788:                /*
                    789:                 * Log a message that the hard limit has been hit.
                    790:                 */
                    791:                if (pp->pr_hardlimit_warning != NULL &&
                    792:                    ratecheck(&pp->pr_hardlimit_warning_last,
                    793:                              &pp->pr_hardlimit_ratecap))
                    794:                        log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1.21      thorpej   795:
                    796:                pp->pr_nfail++;
                    797:
1.134     ad        798:                mutex_exit(&pp->pr_lock);
1.20      thorpej   799:                return (NULL);
                    800:        }
                    801:
1.3       pk        802:        /*
                    803:         * The convention we use is that if `curpage' is not NULL, then
                    804:         * it points at a non-empty bucket. In particular, `curpage'
                    805:         * never points at a page header which has PR_PHINPAGE set and
                    806:         * has no items in its bucket.
                    807:         */
1.20      thorpej   808:        if ((ph = pp->pr_curpage) == NULL) {
1.113     yamt      809:                int error;
                    810:
1.20      thorpej   811: #ifdef DIAGNOSTIC
                    812:                if (pp->pr_nitems != 0) {
1.134     ad        813:                        mutex_exit(&pp->pr_lock);
1.20      thorpej   814:                        printf("pool_get: %s: curpage NULL, nitems %u\n",
                    815:                            pp->pr_wchan, pp->pr_nitems);
1.80      provos    816:                        panic("pool_get: nitems inconsistent");
1.20      thorpej   817:                }
                    818: #endif
                    819:
1.21      thorpej   820:                /*
                    821:                 * Call the back-end page allocator for more memory.
                    822:                 * Release the pool lock, as the back-end page allocator
                    823:                 * may block.
                    824:                 */
1.113     yamt      825:                error = pool_grow(pp, flags);
                    826:                if (error != 0) {
1.21      thorpej   827:                        /*
1.55      thorpej   828:                         * We were unable to allocate a page or item
                    829:                         * header, but we released the lock during
                    830:                         * allocation, so perhaps items were freed
                    831:                         * back to the pool.  Check for this case.
1.21      thorpej   832:                         */
                    833:                        if (pp->pr_curpage != NULL)
                    834:                                goto startover;
1.15      pk        835:
1.117     yamt      836:                        pp->pr_nfail++;
1.134     ad        837:                        mutex_exit(&pp->pr_lock);
1.117     yamt      838:                        return (NULL);
1.1       pk        839:                }
1.3       pk        840:
1.20      thorpej   841:                /* Start the allocation process over. */
                    842:                goto startover;
1.3       pk        843:        }
1.97      yamt      844:        if (pp->pr_roflags & PR_NOTOUCH) {
                    845: #ifdef DIAGNOSTIC
                    846:                if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) {
1.134     ad        847:                        mutex_exit(&pp->pr_lock);
1.97      yamt      848:                        panic("pool_get: %s: page empty", pp->pr_wchan);
                    849:                }
                    850: #endif
                    851:                v = pr_item_notouch_get(pp, ph);
                    852:        } else {
1.102     chs       853:                v = pi = LIST_FIRST(&ph->ph_itemlist);
1.97      yamt      854:                if (__predict_false(v == NULL)) {
1.134     ad        855:                        mutex_exit(&pp->pr_lock);
1.97      yamt      856:                        panic("pool_get: %s: page empty", pp->pr_wchan);
                    857:                }
1.20      thorpej   858: #ifdef DIAGNOSTIC
1.97      yamt      859:                if (__predict_false(pp->pr_nitems == 0)) {
1.134     ad        860:                        mutex_exit(&pp->pr_lock);
1.97      yamt      861:                        printf("pool_get: %s: items on itemlist, nitems %u\n",
                    862:                            pp->pr_wchan, pp->pr_nitems);
                    863:                        panic("pool_get: nitems inconsistent");
                    864:                }
1.65      enami     865: #endif
1.56      sommerfe  866:
1.65      enami     867: #ifdef DIAGNOSTIC
1.97      yamt      868:                if (__predict_false(pi->pi_magic != PI_MAGIC)) {
                    869:                        panic("pool_get(%s): free list modified: "
                    870:                            "magic=%x; page %p; item addr %p\n",
                    871:                            pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
                    872:                }
1.3       pk        873: #endif
                    874:
1.97      yamt      875:                /*
                    876:                 * Remove from item list.
                    877:                 */
1.102     chs       878:                LIST_REMOVE(pi, pi_list);
1.97      yamt      879:        }
1.20      thorpej   880:        pp->pr_nitems--;
                    881:        pp->pr_nout++;
1.6       thorpej   882:        if (ph->ph_nmissing == 0) {
                    883: #ifdef DIAGNOSTIC
1.34      thorpej   884:                if (__predict_false(pp->pr_nidle == 0))
1.6       thorpej   885:                        panic("pool_get: nidle inconsistent");
                    886: #endif
                    887:                pp->pr_nidle--;
1.88      chs       888:
                    889:                /*
                    890:                 * This page was previously empty.  Move it to the list of
                    891:                 * partially-full pages.  This page is already curpage.
                    892:                 */
                    893:                LIST_REMOVE(ph, ph_pagelist);
                    894:                LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.6       thorpej   895:        }
1.3       pk        896:        ph->ph_nmissing++;
1.97      yamt      897:        if (ph->ph_nmissing == pp->pr_itemsperpage) {
1.21      thorpej   898: #ifdef DIAGNOSTIC
1.97      yamt      899:                if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 &&
1.102     chs       900:                    !LIST_EMPTY(&ph->ph_itemlist))) {
1.134     ad        901:                        mutex_exit(&pp->pr_lock);
1.21      thorpej   902:                        panic("pool_get: %s: nmissing inconsistent",
                    903:                            pp->pr_wchan);
                    904:                }
                    905: #endif
1.3       pk        906:                /*
1.88      chs       907:                 * This page is now full.  Move it to the full list
                    908:                 * and select a new current page.
1.3       pk        909:                 */
1.88      chs       910:                LIST_REMOVE(ph, ph_pagelist);
                    911:                LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
                    912:                pool_update_curpage(pp);
1.1       pk        913:        }
1.3       pk        914:
                    915:        pp->pr_nget++;
1.20      thorpej   916:
                    917:        /*
                    918:         * If we have a low water mark and we are now below that low
                    919:         * water mark, add more items to the pool.
                    920:         */
1.53      thorpej   921:        if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20      thorpej   922:                /*
                    923:                 * XXX: Should we log a warning?  Should we set up a timeout
                    924:                 * to try again in a second or so?  The latter could break
                    925:                 * a caller's assumptions about interrupt protection, etc.
                    926:                 */
                    927:        }
                    928:
1.134     ad        929:        mutex_exit(&pp->pr_lock);
1.125     ad        930:        KASSERT((((vaddr_t)v + pp->pr_itemoffset) & (pp->pr_align - 1)) == 0);
                    931:        FREECHECK_OUT(&pp->pr_freecheck, v);
1.1       pk        932:        return (v);
                    933: }
                    934:
                    935: /*
1.43      thorpej   936:  * Internal version of pool_put().  Pool is already locked/entered.
1.1       pk        937:  */
1.43      thorpej   938: static void
1.101     thorpej   939: pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq)
1.1       pk        940: {
                    941:        struct pool_item *pi = v;
1.3       pk        942:        struct pool_item_header *ph;
                    943:
1.134     ad        944:        KASSERT(mutex_owned(&pp->pr_lock));
1.125     ad        945:        FREECHECK_IN(&pp->pr_freecheck, v);
1.134     ad        946:        LOCKDEBUG_MEM_CHECK(v, pp->pr_size);
1.61      chs       947:
1.30      thorpej   948: #ifdef DIAGNOSTIC
1.34      thorpej   949:        if (__predict_false(pp->pr_nout == 0)) {
1.30      thorpej   950:                printf("pool %s: putting with none out\n",
                    951:                    pp->pr_wchan);
                    952:                panic("pool_put");
                    953:        }
                    954: #endif
1.3       pk        955:
1.121     yamt      956:        if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) {
1.3       pk        957:                panic("pool_put: %s: page header missing", pp->pr_wchan);
                    958:        }
1.28      thorpej   959:
1.3       pk        960:        /*
                    961:         * Return to item list.
                    962:         */
1.97      yamt      963:        if (pp->pr_roflags & PR_NOTOUCH) {
                    964:                pr_item_notouch_put(pp, ph, v);
                    965:        } else {
1.2       pk        966: #ifdef DIAGNOSTIC
1.97      yamt      967:                pi->pi_magic = PI_MAGIC;
1.3       pk        968: #endif
1.32      chs       969: #ifdef DEBUG
1.97      yamt      970:                {
                    971:                        int i, *ip = v;
1.32      chs       972:
1.97      yamt      973:                        for (i = 0; i < pp->pr_size / sizeof(int); i++) {
                    974:                                *ip++ = PI_MAGIC;
                    975:                        }
1.32      chs       976:                }
                    977: #endif
                    978:
1.102     chs       979:                LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.97      yamt      980:        }
1.79      thorpej   981:        KDASSERT(ph->ph_nmissing != 0);
1.3       pk        982:        ph->ph_nmissing--;
                    983:        pp->pr_nput++;
1.20      thorpej   984:        pp->pr_nitems++;
                    985:        pp->pr_nout--;
1.3       pk        986:
                    987:        /* Cancel "pool empty" condition if it exists */
                    988:        if (pp->pr_curpage == NULL)
                    989:                pp->pr_curpage = ph;
                    990:
                    991:        if (pp->pr_flags & PR_WANTED) {
                    992:                pp->pr_flags &= ~PR_WANTED;
1.134     ad        993:                cv_broadcast(&pp->pr_cv);
1.3       pk        994:        }
                    995:
                    996:        /*
1.88      chs       997:         * If this page is now empty, do one of two things:
1.21      thorpej   998:         *
1.88      chs       999:         *      (1) If we have more pages than the page high water mark,
1.96      thorpej  1000:         *          free the page back to the system.  ONLY CONSIDER
1.90      thorpej  1001:         *          FREEING BACK A PAGE IF WE HAVE MORE THAN OUR MINIMUM PAGE
                   1002:         *          CLAIM.
1.21      thorpej  1003:         *
1.88      chs      1004:         *      (2) Otherwise, move the page to the empty page list.
                   1005:         *
                   1006:         * Either way, select a new current page (so we use a partially-full
                   1007:         * page if one is available).
1.3       pk       1008:         */
                   1009:        if (ph->ph_nmissing == 0) {
1.6       thorpej  1010:                pp->pr_nidle++;
1.90      thorpej  1011:                if (pp->pr_npages > pp->pr_minpages &&
1.152     yamt     1012:                    pp->pr_npages > pp->pr_maxpages) {
1.101     thorpej  1013:                        pr_rmpage(pp, ph, pq);
1.3       pk       1014:                } else {
1.88      chs      1015:                        LIST_REMOVE(ph, ph_pagelist);
                   1016:                        LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.3       pk       1017:
1.21      thorpej  1018:                        /*
                   1019:                         * Update the timestamp on the page.  A page must
                   1020:                         * be idle for some period of time before it can
                   1021:                         * be reclaimed by the pagedaemon.  This minimizes
                   1022:                         * ping-pong'ing for memory.
1.151     yamt     1023:                         *
                   1024:                         * note for 64-bit time_t: truncating to 32-bit is not
                   1025:                         * a problem for our usage.
1.21      thorpej  1026:                         */
1.151     yamt     1027:                        ph->ph_time = time_uptime;
1.1       pk       1028:                }
1.88      chs      1029:                pool_update_curpage(pp);
1.1       pk       1030:        }
1.88      chs      1031:
1.21      thorpej  1032:        /*
1.88      chs      1033:         * If the page was previously completely full, move it to the
                   1034:         * partially-full list and make it the current page.  The next
                   1035:         * allocation will get the item from this page, instead of
                   1036:         * further fragmenting the pool.
1.21      thorpej  1037:         */
                   1038:        else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
1.88      chs      1039:                LIST_REMOVE(ph, ph_pagelist);
                   1040:                LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.21      thorpej  1041:                pp->pr_curpage = ph;
                   1042:        }
1.43      thorpej  1043: }
                   1044:
1.56      sommerfe 1045: void
                   1046: pool_put(struct pool *pp, void *v)
                   1047: {
1.101     thorpej  1048:        struct pool_pagelist pq;
                   1049:
                   1050:        LIST_INIT(&pq);
1.56      sommerfe 1051:
1.134     ad       1052:        mutex_enter(&pp->pr_lock);
1.101     thorpej  1053:        pool_do_put(pp, v, &pq);
1.134     ad       1054:        mutex_exit(&pp->pr_lock);
1.56      sommerfe 1055:
1.102     chs      1056:        pr_pagelist_free(pp, &pq);
1.56      sommerfe 1057: }
1.57      sommerfe 1058:
1.74      thorpej  1059: /*
1.113     yamt     1060:  * pool_grow: grow a pool by a page.
                   1061:  *
                   1062:  * => called with pool locked.
                   1063:  * => unlock and relock the pool.
                   1064:  * => return with pool locked.
                   1065:  */
                   1066:
                   1067: static int
                   1068: pool_grow(struct pool *pp, int flags)
                   1069: {
                   1070:        struct pool_item_header *ph = NULL;
                   1071:        char *cp;
                   1072:
1.134     ad       1073:        mutex_exit(&pp->pr_lock);
1.113     yamt     1074:        cp = pool_allocator_alloc(pp, flags);
                   1075:        if (__predict_true(cp != NULL)) {
                   1076:                ph = pool_alloc_item_header(pp, cp, flags);
                   1077:        }
                   1078:        if (__predict_false(cp == NULL || ph == NULL)) {
                   1079:                if (cp != NULL) {
                   1080:                        pool_allocator_free(pp, cp);
                   1081:                }
1.134     ad       1082:                mutex_enter(&pp->pr_lock);
1.113     yamt     1083:                return ENOMEM;
                   1084:        }
                   1085:
1.134     ad       1086:        mutex_enter(&pp->pr_lock);
1.113     yamt     1087:        pool_prime_page(pp, cp, ph);
                   1088:        pp->pr_npagealloc++;
                   1089:        return 0;
                   1090: }
                   1091:
                   1092: /*
1.74      thorpej  1093:  * Add N items to the pool.
                   1094:  */
                   1095: int
                   1096: pool_prime(struct pool *pp, int n)
                   1097: {
1.75      simonb   1098:        int newpages;
1.113     yamt     1099:        int error = 0;
1.74      thorpej  1100:
1.134     ad       1101:        mutex_enter(&pp->pr_lock);
1.74      thorpej  1102:
                   1103:        newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
                   1104:
                   1105:        while (newpages-- > 0) {
1.113     yamt     1106:                error = pool_grow(pp, PR_NOWAIT);
                   1107:                if (error) {
1.74      thorpej  1108:                        break;
                   1109:                }
                   1110:                pp->pr_minpages++;
                   1111:        }
                   1112:
                   1113:        if (pp->pr_minpages >= pp->pr_maxpages)
                   1114:                pp->pr_maxpages = pp->pr_minpages + 1;  /* XXX */
                   1115:
1.134     ad       1116:        mutex_exit(&pp->pr_lock);
1.113     yamt     1117:        return error;
1.74      thorpej  1118: }
1.55      thorpej  1119:
                   1120: /*
1.3       pk       1121:  * Add a page worth of items to the pool.
1.21      thorpej  1122:  *
                   1123:  * Note, we must be called with the pool descriptor LOCKED.
1.3       pk       1124:  */
1.55      thorpej  1125: static void
1.128     christos 1126: pool_prime_page(struct pool *pp, void *storage, struct pool_item_header *ph)
1.3       pk       1127: {
                   1128:        struct pool_item *pi;
1.128     christos 1129:        void *cp = storage;
1.125     ad       1130:        const unsigned int align = pp->pr_align;
                   1131:        const unsigned int ioff = pp->pr_itemoffset;
1.55      thorpej  1132:        int n;
1.36      pk       1133:
1.134     ad       1134:        KASSERT(mutex_owned(&pp->pr_lock));
1.91      yamt     1135:
1.66      thorpej  1136: #ifdef DIAGNOSTIC
1.121     yamt     1137:        if ((pp->pr_roflags & PR_NOALIGN) == 0 &&
1.150     skrll    1138:            ((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0)
1.36      pk       1139:                panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1.66      thorpej  1140: #endif
1.3       pk       1141:
                   1142:        /*
                   1143:         * Insert page header.
                   1144:         */
1.88      chs      1145:        LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.102     chs      1146:        LIST_INIT(&ph->ph_itemlist);
1.3       pk       1147:        ph->ph_page = storage;
                   1148:        ph->ph_nmissing = 0;
1.151     yamt     1149:        ph->ph_time = time_uptime;
1.88      chs      1150:        if ((pp->pr_roflags & PR_PHINPAGE) == 0)
                   1151:                SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
1.3       pk       1152:
1.6       thorpej  1153:        pp->pr_nidle++;
                   1154:
1.3       pk       1155:        /*
                   1156:         * Color this page.
                   1157:         */
1.141     yamt     1158:        ph->ph_off = pp->pr_curcolor;
                   1159:        cp = (char *)cp + ph->ph_off;
1.3       pk       1160:        if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
                   1161:                pp->pr_curcolor = 0;
                   1162:
                   1163:        /*
                   1164:         * Adjust storage to apply aligment to `pr_itemoffset' in each item.
                   1165:         */
                   1166:        if (ioff != 0)
1.128     christos 1167:                cp = (char *)cp + align - ioff;
1.3       pk       1168:
1.125     ad       1169:        KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
                   1170:
1.3       pk       1171:        /*
                   1172:         * Insert remaining chunks on the bucket list.
                   1173:         */
                   1174:        n = pp->pr_itemsperpage;
1.20      thorpej  1175:        pp->pr_nitems += n;
1.3       pk       1176:
1.97      yamt     1177:        if (pp->pr_roflags & PR_NOTOUCH) {
1.141     yamt     1178:                pr_item_notouch_init(pp, ph);
1.97      yamt     1179:        } else {
                   1180:                while (n--) {
                   1181:                        pi = (struct pool_item *)cp;
1.78      thorpej  1182:
1.97      yamt     1183:                        KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
1.3       pk       1184:
1.97      yamt     1185:                        /* Insert on page list */
1.102     chs      1186:                        LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.3       pk       1187: #ifdef DIAGNOSTIC
1.97      yamt     1188:                        pi->pi_magic = PI_MAGIC;
1.3       pk       1189: #endif
1.128     christos 1190:                        cp = (char *)cp + pp->pr_size;
1.125     ad       1191:
                   1192:                        KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1.97      yamt     1193:                }
1.3       pk       1194:        }
                   1195:
                   1196:        /*
                   1197:         * If the pool was depleted, point at the new page.
                   1198:         */
                   1199:        if (pp->pr_curpage == NULL)
                   1200:                pp->pr_curpage = ph;
                   1201:
                   1202:        if (++pp->pr_npages > pp->pr_hiwat)
                   1203:                pp->pr_hiwat = pp->pr_npages;
                   1204: }
                   1205:
1.20      thorpej  1206: /*
1.52      thorpej  1207:  * Used by pool_get() when nitems drops below the low water mark.  This
1.88      chs      1208:  * is used to catch up pr_nitems with the low water mark.
1.20      thorpej  1209:  *
1.21      thorpej  1210:  * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20      thorpej  1211:  *
1.73      thorpej  1212:  * Note 2, we must be called with the pool already locked, and we return
1.20      thorpej  1213:  * with it locked.
                   1214:  */
                   1215: static int
1.42      thorpej  1216: pool_catchup(struct pool *pp)
1.20      thorpej  1217: {
                   1218:        int error = 0;
                   1219:
1.54      thorpej  1220:        while (POOL_NEEDS_CATCHUP(pp)) {
1.113     yamt     1221:                error = pool_grow(pp, PR_NOWAIT);
                   1222:                if (error) {
1.20      thorpej  1223:                        break;
                   1224:                }
                   1225:        }
1.113     yamt     1226:        return error;
1.20      thorpej  1227: }
                   1228:
1.88      chs      1229: static void
                   1230: pool_update_curpage(struct pool *pp)
                   1231: {
                   1232:
                   1233:        pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
                   1234:        if (pp->pr_curpage == NULL) {
                   1235:                pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
                   1236:        }
1.168     yamt     1237:        KASSERT((pp->pr_curpage == NULL && pp->pr_nitems == 0) ||
                   1238:            (pp->pr_curpage != NULL && pp->pr_nitems > 0));
1.88      chs      1239: }
                   1240:
1.3       pk       1241: void
1.42      thorpej  1242: pool_setlowat(struct pool *pp, int n)
1.3       pk       1243: {
1.15      pk       1244:
1.134     ad       1245:        mutex_enter(&pp->pr_lock);
1.21      thorpej  1246:
1.3       pk       1247:        pp->pr_minitems = n;
1.15      pk       1248:        pp->pr_minpages = (n == 0)
                   1249:                ? 0
1.18      thorpej  1250:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20      thorpej  1251:
                   1252:        /* Make sure we're caught up with the newly-set low water mark. */
1.75      simonb   1253:        if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20      thorpej  1254:                /*
                   1255:                 * XXX: Should we log a warning?  Should we set up a timeout
                   1256:                 * to try again in a second or so?  The latter could break
                   1257:                 * a caller's assumptions about interrupt protection, etc.
                   1258:                 */
                   1259:        }
1.21      thorpej  1260:
1.134     ad       1261:        mutex_exit(&pp->pr_lock);
1.3       pk       1262: }
                   1263:
                   1264: void
1.42      thorpej  1265: pool_sethiwat(struct pool *pp, int n)
1.3       pk       1266: {
1.15      pk       1267:
1.134     ad       1268:        mutex_enter(&pp->pr_lock);
1.21      thorpej  1269:
1.15      pk       1270:        pp->pr_maxpages = (n == 0)
                   1271:                ? 0
1.18      thorpej  1272:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21      thorpej  1273:
1.134     ad       1274:        mutex_exit(&pp->pr_lock);
1.3       pk       1275: }
                   1276:
1.20      thorpej  1277: void
1.42      thorpej  1278: pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1.20      thorpej  1279: {
                   1280:
1.134     ad       1281:        mutex_enter(&pp->pr_lock);
1.20      thorpej  1282:
                   1283:        pp->pr_hardlimit = n;
                   1284:        pp->pr_hardlimit_warning = warnmess;
1.31      thorpej  1285:        pp->pr_hardlimit_ratecap.tv_sec = ratecap;
                   1286:        pp->pr_hardlimit_warning_last.tv_sec = 0;
                   1287:        pp->pr_hardlimit_warning_last.tv_usec = 0;
1.20      thorpej  1288:
                   1289:        /*
1.21      thorpej  1290:         * In-line version of pool_sethiwat(), because we don't want to
                   1291:         * release the lock.
1.20      thorpej  1292:         */
                   1293:        pp->pr_maxpages = (n == 0)
                   1294:                ? 0
                   1295:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21      thorpej  1296:
1.134     ad       1297:        mutex_exit(&pp->pr_lock);
1.20      thorpej  1298: }
1.3       pk       1299:
                   1300: /*
                   1301:  * Release all complete pages that have not been used recently.
1.184     rmind    1302:  *
                   1303:  * Might be called from interrupt context.
1.3       pk       1304:  */
1.66      thorpej  1305: int
1.56      sommerfe 1306: pool_reclaim(struct pool *pp)
1.3       pk       1307: {
                   1308:        struct pool_item_header *ph, *phnext;
1.61      chs      1309:        struct pool_pagelist pq;
1.151     yamt     1310:        uint32_t curtime;
1.134     ad       1311:        bool klock;
                   1312:        int rv;
1.3       pk       1313:
1.184     rmind    1314:        if (cpu_intr_p() || cpu_softintr_p()) {
                   1315:                KASSERT(pp->pr_ipl != IPL_NONE);
                   1316:        }
                   1317:
1.68      thorpej  1318:        if (pp->pr_drain_hook != NULL) {
                   1319:                /*
                   1320:                 * The drain hook must be called with the pool unlocked.
                   1321:                 */
                   1322:                (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT);
                   1323:        }
                   1324:
1.134     ad       1325:        /*
1.157     ad       1326:         * XXXSMP Because we do not want to cause non-MPSAFE code
                   1327:         * to block.
1.134     ad       1328:         */
                   1329:        if (pp->pr_ipl == IPL_SOFTNET || pp->pr_ipl == IPL_SOFTCLOCK ||
                   1330:            pp->pr_ipl == IPL_SOFTSERIAL) {
                   1331:                KERNEL_LOCK(1, NULL);
                   1332:                klock = true;
                   1333:        } else
                   1334:                klock = false;
                   1335:
                   1336:        /* Reclaim items from the pool's cache (if any). */
                   1337:        if (pp->pr_cache != NULL)
                   1338:                pool_cache_invalidate(pp->pr_cache);
                   1339:
                   1340:        if (mutex_tryenter(&pp->pr_lock) == 0) {
                   1341:                if (klock) {
                   1342:                        KERNEL_UNLOCK_ONE(NULL);
                   1343:                }
1.66      thorpej  1344:                return (0);
1.134     ad       1345:        }
1.68      thorpej  1346:
1.88      chs      1347:        LIST_INIT(&pq);
1.43      thorpej  1348:
1.151     yamt     1349:        curtime = time_uptime;
1.21      thorpej  1350:
1.88      chs      1351:        for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
                   1352:                phnext = LIST_NEXT(ph, ph_pagelist);
1.3       pk       1353:
                   1354:                /* Check our minimum page claim */
                   1355:                if (pp->pr_npages <= pp->pr_minpages)
                   1356:                        break;
                   1357:
1.88      chs      1358:                KASSERT(ph->ph_nmissing == 0);
1.190.2.1  yamt     1359:                if (curtime - ph->ph_time < pool_inactive_time)
1.88      chs      1360:                        continue;
1.21      thorpej  1361:
1.88      chs      1362:                /*
                   1363:                 * If freeing this page would put us below
                   1364:                 * the low water mark, stop now.
                   1365:                 */
                   1366:                if ((pp->pr_nitems - pp->pr_itemsperpage) <
                   1367:                    pp->pr_minitems)
                   1368:                        break;
1.21      thorpej  1369:
1.88      chs      1370:                pr_rmpage(pp, ph, &pq);
1.3       pk       1371:        }
                   1372:
1.134     ad       1373:        mutex_exit(&pp->pr_lock);
                   1374:
                   1375:        if (LIST_EMPTY(&pq))
                   1376:                rv = 0;
                   1377:        else {
                   1378:                pr_pagelist_free(pp, &pq);
                   1379:                rv = 1;
                   1380:        }
                   1381:
                   1382:        if (klock) {
                   1383:                KERNEL_UNLOCK_ONE(NULL);
                   1384:        }
1.66      thorpej  1385:
1.134     ad       1386:        return (rv);
1.3       pk       1387: }
                   1388:
                   1389: /*
1.134     ad       1390:  * Drain pools, one at a time.  This is a two stage process;
                   1391:  * drain_start kicks off a cross call to drain CPU-level caches
                   1392:  * if the pool has an associated pool_cache.  drain_end waits
                   1393:  * for those cross calls to finish, and then drains the cache
                   1394:  * (if any) and pool.
1.131     ad       1395:  *
1.134     ad       1396:  * Note, must never be called from interrupt context.
1.3       pk       1397:  */
                   1398: void
1.134     ad       1399: pool_drain_start(struct pool **ppp, uint64_t *wp)
1.3       pk       1400: {
                   1401:        struct pool *pp;
1.134     ad       1402:
1.145     ad       1403:        KASSERT(!TAILQ_EMPTY(&pool_head));
1.3       pk       1404:
1.61      chs      1405:        pp = NULL;
1.134     ad       1406:
                   1407:        /* Find next pool to drain, and add a reference. */
                   1408:        mutex_enter(&pool_head_lock);
                   1409:        do {
                   1410:                if (drainpp == NULL) {
1.145     ad       1411:                        drainpp = TAILQ_FIRST(&pool_head);
1.134     ad       1412:                }
                   1413:                if (drainpp != NULL) {
                   1414:                        pp = drainpp;
1.145     ad       1415:                        drainpp = TAILQ_NEXT(pp, pr_poollist);
1.134     ad       1416:                }
                   1417:                /*
                   1418:                 * Skip completely idle pools.  We depend on at least
                   1419:                 * one pool in the system being active.
                   1420:                 */
                   1421:        } while (pp == NULL || pp->pr_npages == 0);
                   1422:        pp->pr_refcnt++;
                   1423:        mutex_exit(&pool_head_lock);
                   1424:
                   1425:        /* If there is a pool_cache, drain CPU level caches. */
                   1426:        *ppp = pp;
                   1427:        if (pp->pr_cache != NULL) {
                   1428:                *wp = xc_broadcast(0, (xcfunc_t)pool_cache_xcall,
                   1429:                    pp->pr_cache, NULL);
                   1430:        }
                   1431: }
                   1432:
1.186     pooka    1433: bool
1.134     ad       1434: pool_drain_end(struct pool *pp, uint64_t where)
                   1435: {
1.186     pooka    1436:        bool reclaimed;
1.134     ad       1437:
                   1438:        if (pp == NULL)
1.186     pooka    1439:                return false;
1.134     ad       1440:
                   1441:        KASSERT(pp->pr_refcnt > 0);
                   1442:
                   1443:        /* Wait for remote draining to complete. */
                   1444:        if (pp->pr_cache != NULL)
                   1445:                xc_wait(where);
                   1446:
                   1447:        /* Drain the cache (if any) and pool.. */
1.186     pooka    1448:        reclaimed = pool_reclaim(pp);
1.134     ad       1449:
                   1450:        /* Finally, unlock the pool. */
                   1451:        mutex_enter(&pool_head_lock);
                   1452:        pp->pr_refcnt--;
                   1453:        cv_broadcast(&pool_busy);
                   1454:        mutex_exit(&pool_head_lock);
1.186     pooka    1455:
                   1456:        return reclaimed;
1.3       pk       1457: }
                   1458:
                   1459: /*
                   1460:  * Diagnostic helpers.
                   1461:  */
1.21      thorpej  1462:
1.25      thorpej  1463: void
1.108     yamt     1464: pool_printall(const char *modif, void (*pr)(const char *, ...))
                   1465: {
                   1466:        struct pool *pp;
                   1467:
1.145     ad       1468:        TAILQ_FOREACH(pp, &pool_head, pr_poollist) {
1.108     yamt     1469:                pool_printit(pp, modif, pr);
                   1470:        }
                   1471: }
                   1472:
                   1473: void
1.42      thorpej  1474: pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.25      thorpej  1475: {
                   1476:
                   1477:        if (pp == NULL) {
                   1478:                (*pr)("Must specify a pool to print.\n");
                   1479:                return;
                   1480:        }
                   1481:
                   1482:        pool_print1(pp, modif, pr);
                   1483: }
                   1484:
1.21      thorpej  1485: static void
1.124     yamt     1486: pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
1.97      yamt     1487:     void (*pr)(const char *, ...))
1.88      chs      1488: {
                   1489:        struct pool_item_header *ph;
                   1490: #ifdef DIAGNOSTIC
                   1491:        struct pool_item *pi;
                   1492: #endif
                   1493:
                   1494:        LIST_FOREACH(ph, pl, ph_pagelist) {
1.151     yamt     1495:                (*pr)("\t\tpage %p, nmissing %d, time %" PRIu32 "\n",
                   1496:                    ph->ph_page, ph->ph_nmissing, ph->ph_time);
1.88      chs      1497: #ifdef DIAGNOSTIC
1.97      yamt     1498:                if (!(pp->pr_roflags & PR_NOTOUCH)) {
1.102     chs      1499:                        LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1.97      yamt     1500:                                if (pi->pi_magic != PI_MAGIC) {
                   1501:                                        (*pr)("\t\t\titem %p, magic 0x%x\n",
                   1502:                                            pi, pi->pi_magic);
                   1503:                                }
1.88      chs      1504:                        }
                   1505:                }
                   1506: #endif
                   1507:        }
                   1508: }
                   1509:
                   1510: static void
1.42      thorpej  1511: pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.3       pk       1512: {
1.25      thorpej  1513:        struct pool_item_header *ph;
1.134     ad       1514:        pool_cache_t pc;
                   1515:        pcg_t *pcg;
                   1516:        pool_cache_cpu_t *cc;
                   1517:        uint64_t cpuhit, cpumiss;
1.44      thorpej  1518:        int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1.25      thorpej  1519:        char c;
                   1520:
                   1521:        while ((c = *modif++) != '\0') {
                   1522:                if (c == 'l')
                   1523:                        print_log = 1;
                   1524:                if (c == 'p')
                   1525:                        print_pagelist = 1;
1.44      thorpej  1526:                if (c == 'c')
                   1527:                        print_cache = 1;
1.25      thorpej  1528:        }
                   1529:
1.134     ad       1530:        if ((pc = pp->pr_cache) != NULL) {
                   1531:                (*pr)("POOL CACHE");
                   1532:        } else {
                   1533:                (*pr)("POOL");
                   1534:        }
                   1535:
                   1536:        (*pr)(" %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1.25      thorpej  1537:            pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
                   1538:            pp->pr_roflags);
1.66      thorpej  1539:        (*pr)("\talloc %p\n", pp->pr_alloc);
1.25      thorpej  1540:        (*pr)("\tminitems %u, minpages %u, maxpages %u,