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

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

CVSweb <webmaster@jp.NetBSD.org>