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

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

CVSweb <webmaster@jp.NetBSD.org>