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

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

CVSweb <webmaster@jp.NetBSD.org>