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

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

CVSweb <webmaster@jp.NetBSD.org>