[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.201

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

CVSweb <webmaster@jp.NetBSD.org>