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

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

CVSweb <webmaster@jp.NetBSD.org>