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

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

CVSweb <webmaster@jp.NetBSD.org>