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

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

CVSweb <webmaster@jp.NetBSD.org>