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

1.56    ! sommerfe    1: /*     $NetBSD: subr_pool.c,v 1.55 2001/05/10 04:51:41 thorpej Exp $   */
1.1       pk          2:
                      3: /*-
1.43      thorpej     4:  * Copyright (c) 1997, 1999, 2000 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
                      9:  * Simulation Facility, NASA Ames Research Center.
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.24      scottr     39:
1.25      thorpej    40: #include "opt_pool.h"
1.24      scottr     41: #include "opt_poollog.h"
1.28      thorpej    42: #include "opt_lockdebug.h"
1.1       pk         43:
                     44: #include <sys/param.h>
                     45: #include <sys/systm.h>
                     46: #include <sys/proc.h>
                     47: #include <sys/errno.h>
                     48: #include <sys/kernel.h>
                     49: #include <sys/malloc.h>
                     50: #include <sys/lock.h>
                     51: #include <sys/pool.h>
1.20      thorpej    52: #include <sys/syslog.h>
1.3       pk         53:
                     54: #include <uvm/uvm.h>
                     55:
1.1       pk         56: /*
                     57:  * Pool resource management utility.
1.3       pk         58:  *
                     59:  * Memory is allocated in pages which are split into pieces according
                     60:  * to the pool item size. Each page is kept on a list headed by `pr_pagelist'
                     61:  * in the pool structure and the individual pool items are on a linked list
                     62:  * headed by `ph_itemlist' in each page header. The memory for building
                     63:  * the page list is either taken from the allocated pages themselves (for
                     64:  * small pool items) or taken from an internal pool of page headers (`phpool').
1.1       pk         65:  */
                     66:
1.3       pk         67: /* List of all pools */
1.5       thorpej    68: TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
1.3       pk         69:
                     70: /* Private pool for page header structures */
                     71: static struct pool phpool;
                     72:
                     73: /* # of seconds to retain page after last use */
                     74: int pool_inactive_time = 10;
                     75:
                     76: /* Next candidate for drainage (see pool_drain()) */
1.23      thorpej    77: static struct pool     *drainpp;
                     78:
                     79: /* This spin lock protects both pool_head and drainpp. */
                     80: struct simplelock pool_head_slock = SIMPLELOCK_INITIALIZER;
1.3       pk         81:
                     82: struct pool_item_header {
                     83:        /* Page headers */
                     84:        TAILQ_ENTRY(pool_item_header)
                     85:                                ph_pagelist;    /* pool page list */
                     86:        TAILQ_HEAD(,pool_item)  ph_itemlist;    /* chunk list for this page */
                     87:        LIST_ENTRY(pool_item_header)
                     88:                                ph_hashlist;    /* Off-page page headers */
                     89:        int                     ph_nmissing;    /* # of chunks in use */
                     90:        caddr_t                 ph_page;        /* this page's address */
                     91:        struct timeval          ph_time;        /* last referenced */
                     92: };
                     93:
1.1       pk         94: struct pool_item {
1.3       pk         95: #ifdef DIAGNOSTIC
                     96:        int pi_magic;
1.33      chs        97: #endif
1.25      thorpej    98: #define        PI_MAGIC 0xdeadbeef
1.3       pk         99:        /* Other entries use only this list entry */
                    100:        TAILQ_ENTRY(pool_item)  pi_list;
                    101: };
                    102:
1.25      thorpej   103: #define        PR_HASH_INDEX(pp,addr) \
1.3       pk        104:        (((u_long)(addr) >> (pp)->pr_pageshift) & (PR_HASHTABSIZE - 1))
                    105:
1.53      thorpej   106: #define        POOL_NEEDS_CATCHUP(pp)                                          \
                    107:        ((pp)->pr_nitems < (pp)->pr_minitems)
                    108:
1.43      thorpej   109: /*
                    110:  * Pool cache management.
                    111:  *
                    112:  * Pool caches provide a way for constructed objects to be cached by the
                    113:  * pool subsystem.  This can lead to performance improvements by avoiding
                    114:  * needless object construction/destruction; it is deferred until absolutely
                    115:  * necessary.
                    116:  *
                    117:  * Caches are grouped into cache groups.  Each cache group references
                    118:  * up to 16 constructed objects.  When a cache allocates an object
                    119:  * from the pool, it calls the object's constructor and places it into
                    120:  * a cache group.  When a cache group frees an object back to the pool,
                    121:  * it first calls the object's destructor.  This allows the object to
                    122:  * persist in constructed form while freed to the cache.
                    123:  *
                    124:  * Multiple caches may exist for each pool.  This allows a single
                    125:  * object type to have multiple constructed forms.  The pool references
                    126:  * each cache, so that when a pool is drained by the pagedaemon, it can
                    127:  * drain each individual cache as well.  Each time a cache is drained,
                    128:  * the most idle cache group is freed to the pool in its entirety.
                    129:  *
                    130:  * Pool caches are layed on top of pools.  By layering them, we can avoid
                    131:  * the complexity of cache management for pools which would not benefit
                    132:  * from it.
                    133:  */
                    134:
                    135: /* The cache group pool. */
                    136: static struct pool pcgpool;
                    137:
                    138: /* The pool cache group. */
                    139: #define        PCG_NOBJECTS            16
                    140: struct pool_cache_group {
                    141:        TAILQ_ENTRY(pool_cache_group)
                    142:                pcg_list;       /* link in the pool cache's group list */
                    143:        u_int   pcg_avail;      /* # available objects */
                    144:                                /* pointers to the objects */
                    145:        void    *pcg_objects[PCG_NOBJECTS];
                    146: };
1.3       pk        147:
1.43      thorpej   148: static void    pool_cache_reclaim(struct pool_cache *);
1.3       pk        149:
1.42      thorpej   150: static int     pool_catchup(struct pool *);
1.55      thorpej   151: static void    pool_prime_page(struct pool *, caddr_t,
                    152:                    struct pool_item_header *);
1.42      thorpej   153: static void    *pool_page_alloc(unsigned long, int, int);
                    154: static void    pool_page_free(void *, unsigned long, int);
1.3       pk        155:
1.42      thorpej   156: static void pool_print1(struct pool *, const char *,
                    157:        void (*)(const char *, ...));
1.3       pk        158:
                    159: /*
1.52      thorpej   160:  * Pool log entry. An array of these is allocated in pool_init().
1.3       pk        161:  */
                    162: struct pool_log {
                    163:        const char      *pl_file;
                    164:        long            pl_line;
                    165:        int             pl_action;
1.25      thorpej   166: #define        PRLOG_GET       1
                    167: #define        PRLOG_PUT       2
1.3       pk        168:        void            *pl_addr;
1.1       pk        169: };
                    170:
1.3       pk        171: /* Number of entries in pool log buffers */
1.17      thorpej   172: #ifndef POOL_LOGSIZE
                    173: #define        POOL_LOGSIZE    10
                    174: #endif
                    175:
                    176: int pool_logsize = POOL_LOGSIZE;
1.1       pk        177:
1.25      thorpej   178: #ifdef DIAGNOSTIC
1.42      thorpej   179: static __inline void
                    180: pr_log(struct pool *pp, void *v, int action, const char *file, long line)
1.3       pk        181: {
                    182:        int n = pp->pr_curlogentry;
                    183:        struct pool_log *pl;
                    184:
1.20      thorpej   185:        if ((pp->pr_roflags & PR_LOGGING) == 0)
1.3       pk        186:                return;
                    187:
                    188:        /*
                    189:         * Fill in the current entry. Wrap around and overwrite
                    190:         * the oldest entry if necessary.
                    191:         */
                    192:        pl = &pp->pr_log[n];
                    193:        pl->pl_file = file;
                    194:        pl->pl_line = line;
                    195:        pl->pl_action = action;
                    196:        pl->pl_addr = v;
                    197:        if (++n >= pp->pr_logsize)
                    198:                n = 0;
                    199:        pp->pr_curlogentry = n;
                    200: }
                    201:
                    202: static void
1.42      thorpej   203: pr_printlog(struct pool *pp, struct pool_item *pi,
                    204:     void (*pr)(const char *, ...))
1.3       pk        205: {
                    206:        int i = pp->pr_logsize;
                    207:        int n = pp->pr_curlogentry;
                    208:
1.20      thorpej   209:        if ((pp->pr_roflags & PR_LOGGING) == 0)
1.3       pk        210:                return;
                    211:
                    212:        /*
                    213:         * Print all entries in this pool's log.
                    214:         */
                    215:        while (i-- > 0) {
                    216:                struct pool_log *pl = &pp->pr_log[n];
                    217:                if (pl->pl_action != 0) {
1.25      thorpej   218:                        if (pi == NULL || pi == pl->pl_addr) {
                    219:                                (*pr)("\tlog entry %d:\n", i);
                    220:                                (*pr)("\t\taction = %s, addr = %p\n",
                    221:                                    pl->pl_action == PRLOG_GET ? "get" : "put",
                    222:                                    pl->pl_addr);
                    223:                                (*pr)("\t\tfile: %s at line %lu\n",
                    224:                                    pl->pl_file, pl->pl_line);
                    225:                        }
1.3       pk        226:                }
                    227:                if (++n >= pp->pr_logsize)
                    228:                        n = 0;
                    229:        }
                    230: }
1.25      thorpej   231:
1.42      thorpej   232: static __inline void
                    233: pr_enter(struct pool *pp, const char *file, long line)
1.25      thorpej   234: {
                    235:
1.34      thorpej   236:        if (__predict_false(pp->pr_entered_file != NULL)) {
1.25      thorpej   237:                printf("pool %s: reentrancy at file %s line %ld\n",
                    238:                    pp->pr_wchan, file, line);
                    239:                printf("         previous entry at file %s line %ld\n",
                    240:                    pp->pr_entered_file, pp->pr_entered_line);
                    241:                panic("pr_enter");
                    242:        }
                    243:
                    244:        pp->pr_entered_file = file;
                    245:        pp->pr_entered_line = line;
                    246: }
                    247:
1.42      thorpej   248: static __inline void
                    249: pr_leave(struct pool *pp)
1.25      thorpej   250: {
                    251:
1.34      thorpej   252:        if (__predict_false(pp->pr_entered_file == NULL)) {
1.25      thorpej   253:                printf("pool %s not entered?\n", pp->pr_wchan);
                    254:                panic("pr_leave");
                    255:        }
                    256:
                    257:        pp->pr_entered_file = NULL;
                    258:        pp->pr_entered_line = 0;
                    259: }
                    260:
1.42      thorpej   261: static __inline void
                    262: pr_enter_check(struct pool *pp, void (*pr)(const char *, ...))
1.25      thorpej   263: {
                    264:
                    265:        if (pp->pr_entered_file != NULL)
                    266:                (*pr)("\n\tcurrently entered from file %s line %ld\n",
                    267:                    pp->pr_entered_file, pp->pr_entered_line);
                    268: }
1.3       pk        269: #else
1.25      thorpej   270: #define        pr_log(pp, v, action, file, line)
                    271: #define        pr_printlog(pp, pi, pr)
                    272: #define        pr_enter(pp, file, line)
                    273: #define        pr_leave(pp)
                    274: #define        pr_enter_check(pp, pr)
                    275: #endif /* DIAGNOSTIC */
1.3       pk        276:
                    277: /*
                    278:  * Return the pool page header based on page address.
                    279:  */
1.42      thorpej   280: static __inline struct pool_item_header *
                    281: pr_find_pagehead(struct pool *pp, caddr_t page)
1.3       pk        282: {
                    283:        struct pool_item_header *ph;
                    284:
1.20      thorpej   285:        if ((pp->pr_roflags & PR_PHINPAGE) != 0)
1.3       pk        286:                return ((struct pool_item_header *)(page + pp->pr_phoffset));
                    287:
                    288:        for (ph = LIST_FIRST(&pp->pr_hashtab[PR_HASH_INDEX(pp, page)]);
                    289:             ph != NULL;
                    290:             ph = LIST_NEXT(ph, ph_hashlist)) {
                    291:                if (ph->ph_page == page)
                    292:                        return (ph);
                    293:        }
                    294:        return (NULL);
                    295: }
                    296:
                    297: /*
                    298:  * Remove a page from the pool.
                    299:  */
1.42      thorpej   300: static __inline void
                    301: pr_rmpage(struct pool *pp, struct pool_item_header *ph)
1.3       pk        302: {
                    303:
                    304:        /*
1.7       thorpej   305:         * If the page was idle, decrement the idle page count.
1.3       pk        306:         */
1.6       thorpej   307:        if (ph->ph_nmissing == 0) {
                    308: #ifdef DIAGNOSTIC
                    309:                if (pp->pr_nidle == 0)
                    310:                        panic("pr_rmpage: nidle inconsistent");
1.20      thorpej   311:                if (pp->pr_nitems < pp->pr_itemsperpage)
                    312:                        panic("pr_rmpage: nitems inconsistent");
1.6       thorpej   313: #endif
                    314:                pp->pr_nidle--;
                    315:        }
1.7       thorpej   316:
1.20      thorpej   317:        pp->pr_nitems -= pp->pr_itemsperpage;
                    318:
1.7       thorpej   319:        /*
                    320:         * Unlink a page from the pool and release it.
                    321:         */
                    322:        TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
                    323:        (*pp->pr_free)(ph->ph_page, pp->pr_pagesz, pp->pr_mtype);
                    324:        pp->pr_npages--;
                    325:        pp->pr_npagefree++;
1.6       thorpej   326:
1.22      chs       327:        if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
1.27      pk        328:                int s;
1.22      chs       329:                LIST_REMOVE(ph, ph_hashlist);
1.27      pk        330:                s = splhigh();
1.22      chs       331:                pool_put(&phpool, ph);
1.27      pk        332:                splx(s);
1.22      chs       333:        }
                    334:
1.3       pk        335:        if (pp->pr_curpage == ph) {
                    336:                /*
                    337:                 * Find a new non-empty page header, if any.
                    338:                 * Start search from the page head, to increase the
                    339:                 * chance for "high water" pages to be freed.
                    340:                 */
                    341:                for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
                    342:                     ph = TAILQ_NEXT(ph, ph_pagelist))
                    343:                        if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
                    344:                                break;
                    345:
                    346:                pp->pr_curpage = ph;
1.21      thorpej   347:        }
1.3       pk        348: }
                    349:
                    350: /*
                    351:  * Initialize the given pool resource structure.
                    352:  *
                    353:  * We export this routine to allow other kernel parts to declare
                    354:  * static pools that must be initialized before malloc() is available.
                    355:  */
                    356: void
1.42      thorpej   357: pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
                    358:     const char *wchan, size_t pagesz,
                    359:     void *(*alloc)(unsigned long, int, int),
                    360:     void (*release)(void *, unsigned long, int),
                    361:     int mtype)
1.3       pk        362: {
1.16      briggs    363:        int off, slack, i;
1.3       pk        364:
1.25      thorpej   365: #ifdef POOL_DIAGNOSTIC
                    366:        /*
                    367:         * Always log if POOL_DIAGNOSTIC is defined.
                    368:         */
                    369:        if (pool_logsize != 0)
                    370:                flags |= PR_LOGGING;
                    371: #endif
                    372:
1.3       pk        373:        /*
                    374:         * Check arguments and construct default values.
                    375:         */
1.36      pk        376:        if (!powerof2(pagesz))
1.3       pk        377:                panic("pool_init: page size invalid (%lx)\n", (u_long)pagesz);
                    378:
1.4       thorpej   379:        if (alloc == NULL && release == NULL) {
1.3       pk        380:                alloc = pool_page_alloc;
                    381:                release = pool_page_free;
1.4       thorpej   382:                pagesz = PAGE_SIZE;     /* Rounds to PAGE_SIZE anyhow. */
                    383:        } else if ((alloc != NULL && release != NULL) == 0) {
                    384:                /* If you specifiy one, must specify both. */
                    385:                panic("pool_init: must specify alloc and release together");
                    386:        }
                    387:
1.3       pk        388:        if (pagesz == 0)
                    389:                pagesz = PAGE_SIZE;
                    390:
                    391:        if (align == 0)
                    392:                align = ALIGN(1);
1.14      thorpej   393:
                    394:        if (size < sizeof(struct pool_item))
                    395:                size = sizeof(struct pool_item);
1.3       pk        396:
1.35      pk        397:        size = ALIGN(size);
1.43      thorpej   398:        if (size > pagesz)
1.35      pk        399:                panic("pool_init: pool item size (%lu) too large",
                    400:                      (u_long)size);
                    401:
1.3       pk        402:        /*
                    403:         * Initialize the pool structure.
                    404:         */
                    405:        TAILQ_INIT(&pp->pr_pagelist);
1.43      thorpej   406:        TAILQ_INIT(&pp->pr_cachelist);
1.3       pk        407:        pp->pr_curpage = NULL;
                    408:        pp->pr_npages = 0;
                    409:        pp->pr_minitems = 0;
                    410:        pp->pr_minpages = 0;
                    411:        pp->pr_maxpages = UINT_MAX;
1.20      thorpej   412:        pp->pr_roflags = flags;
                    413:        pp->pr_flags = 0;
1.35      pk        414:        pp->pr_size = size;
1.3       pk        415:        pp->pr_align = align;
                    416:        pp->pr_wchan = wchan;
                    417:        pp->pr_mtype = mtype;
                    418:        pp->pr_alloc = alloc;
                    419:        pp->pr_free = release;
                    420:        pp->pr_pagesz = pagesz;
                    421:        pp->pr_pagemask = ~(pagesz - 1);
                    422:        pp->pr_pageshift = ffs(pagesz) - 1;
1.20      thorpej   423:        pp->pr_nitems = 0;
                    424:        pp->pr_nout = 0;
                    425:        pp->pr_hardlimit = UINT_MAX;
                    426:        pp->pr_hardlimit_warning = NULL;
1.31      thorpej   427:        pp->pr_hardlimit_ratecap.tv_sec = 0;
                    428:        pp->pr_hardlimit_ratecap.tv_usec = 0;
                    429:        pp->pr_hardlimit_warning_last.tv_sec = 0;
                    430:        pp->pr_hardlimit_warning_last.tv_usec = 0;
1.3       pk        431:
                    432:        /*
                    433:         * Decide whether to put the page header off page to avoid
                    434:         * wasting too large a part of the page. Off-page page headers
                    435:         * go on a hash table, so we can match a returned item
                    436:         * with its header based on the page address.
                    437:         * We use 1/16 of the page size as the threshold (XXX: tune)
                    438:         */
                    439:        if (pp->pr_size < pagesz/16) {
                    440:                /* Use the end of the page for the page header */
1.20      thorpej   441:                pp->pr_roflags |= PR_PHINPAGE;
1.3       pk        442:                pp->pr_phoffset = off =
                    443:                        pagesz - ALIGN(sizeof(struct pool_item_header));
1.2       pk        444:        } else {
1.3       pk        445:                /* The page header will be taken from our page header pool */
                    446:                pp->pr_phoffset = 0;
                    447:                off = pagesz;
1.16      briggs    448:                for (i = 0; i < PR_HASHTABSIZE; i++) {
                    449:                        LIST_INIT(&pp->pr_hashtab[i]);
                    450:                }
1.2       pk        451:        }
1.1       pk        452:
1.3       pk        453:        /*
                    454:         * Alignment is to take place at `ioff' within the item. This means
                    455:         * we must reserve up to `align - 1' bytes on the page to allow
                    456:         * appropriate positioning of each item.
                    457:         *
                    458:         * Silently enforce `0 <= ioff < align'.
                    459:         */
                    460:        pp->pr_itemoffset = ioff = ioff % align;
                    461:        pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
1.43      thorpej   462:        KASSERT(pp->pr_itemsperpage != 0);
1.3       pk        463:
                    464:        /*
                    465:         * Use the slack between the chunks and the page header
                    466:         * for "cache coloring".
                    467:         */
                    468:        slack = off - pp->pr_itemsperpage * pp->pr_size;
                    469:        pp->pr_maxcolor = (slack / align) * align;
                    470:        pp->pr_curcolor = 0;
                    471:
                    472:        pp->pr_nget = 0;
                    473:        pp->pr_nfail = 0;
                    474:        pp->pr_nput = 0;
                    475:        pp->pr_npagealloc = 0;
                    476:        pp->pr_npagefree = 0;
1.1       pk        477:        pp->pr_hiwat = 0;
1.8       thorpej   478:        pp->pr_nidle = 0;
1.3       pk        479:
1.25      thorpej   480:        if (flags & PR_LOGGING) {
                    481:                if (kmem_map == NULL ||
                    482:                    (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
                    483:                     M_TEMP, M_NOWAIT)) == NULL)
1.20      thorpej   484:                        pp->pr_roflags &= ~PR_LOGGING;
1.3       pk        485:                pp->pr_curlogentry = 0;
                    486:                pp->pr_logsize = pool_logsize;
                    487:        }
1.25      thorpej   488:
                    489:        pp->pr_entered_file = NULL;
                    490:        pp->pr_entered_line = 0;
1.3       pk        491:
1.21      thorpej   492:        simple_lock_init(&pp->pr_slock);
1.1       pk        493:
1.3       pk        494:        /*
1.43      thorpej   495:         * Initialize private page header pool and cache magazine pool if we
                    496:         * haven't done so yet.
1.23      thorpej   497:         * XXX LOCKING.
1.3       pk        498:         */
                    499:        if (phpool.pr_size == 0) {
                    500:                pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
1.43      thorpej   501:                    0, "phpool", 0, 0, 0, 0);
                    502:                pool_init(&pcgpool, sizeof(struct pool_cache_group), 0, 0,
                    503:                    0, "pcgpool", 0, 0, 0, 0);
1.1       pk        504:        }
                    505:
1.23      thorpej   506:        /* Insert into the list of all pools. */
                    507:        simple_lock(&pool_head_slock);
                    508:        TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
                    509:        simple_unlock(&pool_head_slock);
1.1       pk        510: }
                    511:
                    512: /*
                    513:  * De-commision a pool resource.
                    514:  */
                    515: void
1.42      thorpej   516: pool_destroy(struct pool *pp)
1.1       pk        517: {
1.3       pk        518:        struct pool_item_header *ph;
1.43      thorpej   519:        struct pool_cache *pc;
                    520:
                    521:        /* Destroy all caches for this pool. */
                    522:        while ((pc = TAILQ_FIRST(&pp->pr_cachelist)) != NULL)
                    523:                pool_cache_destroy(pc);
1.3       pk        524:
                    525: #ifdef DIAGNOSTIC
1.20      thorpej   526:        if (pp->pr_nout != 0) {
1.25      thorpej   527:                pr_printlog(pp, NULL, printf);
1.20      thorpej   528:                panic("pool_destroy: pool busy: still out: %u\n",
                    529:                    pp->pr_nout);
1.3       pk        530:        }
                    531: #endif
1.1       pk        532:
1.3       pk        533:        /* Remove all pages */
1.20      thorpej   534:        if ((pp->pr_roflags & PR_STATIC) == 0)
1.3       pk        535:                while ((ph = pp->pr_pagelist.tqh_first) != NULL)
                    536:                        pr_rmpage(pp, ph);
                    537:
                    538:        /* Remove from global pool list */
1.23      thorpej   539:        simple_lock(&pool_head_slock);
1.3       pk        540:        TAILQ_REMOVE(&pool_head, pp, pr_poollist);
1.23      thorpej   541:        /* XXX Only clear this if we were drainpp? */
1.3       pk        542:        drainpp = NULL;
1.23      thorpej   543:        simple_unlock(&pool_head_slock);
1.3       pk        544:
1.20      thorpej   545:        if ((pp->pr_roflags & PR_LOGGING) != 0)
1.3       pk        546:                free(pp->pr_log, M_TEMP);
1.2       pk        547:
1.20      thorpej   548:        if (pp->pr_roflags & PR_FREEHEADER)
1.3       pk        549:                free(pp, M_POOL);
1.1       pk        550: }
                    551:
1.55      thorpej   552: static __inline struct pool_item_header *
                    553: pool_alloc_item_header(struct pool *pp, caddr_t storage, int flags)
                    554: {
                    555:        struct pool_item_header *ph;
                    556:        int s;
                    557:
                    558:        LOCK_ASSERT(simple_lock_held(&pp->pr_slock) == 0);
                    559:
                    560:        if ((pp->pr_roflags & PR_PHINPAGE) != 0)
                    561:                ph = (struct pool_item_header *) (storage + pp->pr_phoffset);
                    562:        else {
                    563:                s = splhigh();
                    564:                ph = pool_get(&phpool, flags);
                    565:                splx(s);
                    566:        }
                    567:
                    568:        return (ph);
                    569: }
1.1       pk        570:
                    571: /*
1.3       pk        572:  * Grab an item from the pool; must be called at appropriate spl level
1.1       pk        573:  */
1.3       pk        574: void *
1.56    ! sommerfe  575: #ifdef DIAGNOSTIC
1.42      thorpej   576: _pool_get(struct pool *pp, int flags, const char *file, long line)
1.56    ! sommerfe  577: #else
        !           578: pool_get(struct pool *pp, int flags)
        !           579: #endif
1.1       pk        580: {
                    581:        struct pool_item *pi;
1.3       pk        582:        struct pool_item_header *ph;
1.55      thorpej   583:        void *v;
1.1       pk        584:
1.2       pk        585: #ifdef DIAGNOSTIC
1.34      thorpej   586:        if (__predict_false((pp->pr_roflags & PR_STATIC) &&
                    587:                            (flags & PR_MALLOCOK))) {
1.25      thorpej   588:                pr_printlog(pp, NULL, printf);
1.2       pk        589:                panic("pool_get: static");
1.3       pk        590:        }
1.2       pk        591:
1.37      sommerfe  592:        if (__predict_false(curproc == NULL && doing_shutdown == 0 &&
                    593:                            (flags & PR_WAITOK) != 0))
1.3       pk        594:                panic("pool_get: must have NOWAIT");
1.56    ! sommerfe  595: #endif
1.1       pk        596:
1.21      thorpej   597:        simple_lock(&pp->pr_slock);
1.25      thorpej   598:        pr_enter(pp, file, line);
1.20      thorpej   599:
                    600:  startover:
                    601:        /*
                    602:         * Check to see if we've reached the hard limit.  If we have,
                    603:         * and we can wait, then wait until an item has been returned to
                    604:         * the pool.
                    605:         */
                    606: #ifdef DIAGNOSTIC
1.34      thorpej   607:        if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
1.25      thorpej   608:                pr_leave(pp);
1.21      thorpej   609:                simple_unlock(&pp->pr_slock);
1.20      thorpej   610:                panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
                    611:        }
                    612: #endif
1.34      thorpej   613:        if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
1.29      sommerfe  614:                if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
1.20      thorpej   615:                        /*
                    616:                         * XXX: A warning isn't logged in this case.  Should
                    617:                         * it be?
                    618:                         */
                    619:                        pp->pr_flags |= PR_WANTED;
1.25      thorpej   620:                        pr_leave(pp);
1.40      sommerfe  621:                        ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
1.25      thorpej   622:                        pr_enter(pp, file, line);
1.20      thorpej   623:                        goto startover;
                    624:                }
1.31      thorpej   625:
                    626:                /*
                    627:                 * Log a message that the hard limit has been hit.
                    628:                 */
                    629:                if (pp->pr_hardlimit_warning != NULL &&
                    630:                    ratecheck(&pp->pr_hardlimit_warning_last,
                    631:                              &pp->pr_hardlimit_ratecap))
                    632:                        log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1.21      thorpej   633:
                    634:                if (flags & PR_URGENT)
                    635:                        panic("pool_get: urgent");
                    636:
                    637:                pp->pr_nfail++;
                    638:
1.25      thorpej   639:                pr_leave(pp);
1.21      thorpej   640:                simple_unlock(&pp->pr_slock);
1.20      thorpej   641:                return (NULL);
                    642:        }
                    643:
1.3       pk        644:        /*
                    645:         * The convention we use is that if `curpage' is not NULL, then
                    646:         * it points at a non-empty bucket. In particular, `curpage'
                    647:         * never points at a page header which has PR_PHINPAGE set and
                    648:         * has no items in its bucket.
                    649:         */
1.20      thorpej   650:        if ((ph = pp->pr_curpage) == NULL) {
                    651: #ifdef DIAGNOSTIC
                    652:                if (pp->pr_nitems != 0) {
1.21      thorpej   653:                        simple_unlock(&pp->pr_slock);
1.20      thorpej   654:                        printf("pool_get: %s: curpage NULL, nitems %u\n",
                    655:                            pp->pr_wchan, pp->pr_nitems);
                    656:                        panic("pool_get: nitems inconsistent\n");
                    657:                }
                    658: #endif
                    659:
1.21      thorpej   660:                /*
                    661:                 * Call the back-end page allocator for more memory.
                    662:                 * Release the pool lock, as the back-end page allocator
                    663:                 * may block.
                    664:                 */
1.25      thorpej   665:                pr_leave(pp);
1.21      thorpej   666:                simple_unlock(&pp->pr_slock);
                    667:                v = (*pp->pr_alloc)(pp->pr_pagesz, flags, pp->pr_mtype);
1.55      thorpej   668:                if (__predict_true(v != NULL))
                    669:                        ph = pool_alloc_item_header(pp, v, flags);
1.21      thorpej   670:                simple_lock(&pp->pr_slock);
1.25      thorpej   671:                pr_enter(pp, file, line);
1.15      pk        672:
1.55      thorpej   673:                if (__predict_false(v == NULL || ph == NULL)) {
                    674:                        if (v != NULL)
                    675:                                (*pp->pr_free)(v, pp->pr_pagesz, pp->pr_mtype);
                    676:
1.21      thorpej   677:                        /*
1.55      thorpej   678:                         * We were unable to allocate a page or item
                    679:                         * header, but we released the lock during
                    680:                         * allocation, so perhaps items were freed
                    681:                         * back to the pool.  Check for this case.
1.21      thorpej   682:                         */
                    683:                        if (pp->pr_curpage != NULL)
                    684:                                goto startover;
1.15      pk        685:
1.3       pk        686:                        if (flags & PR_URGENT)
                    687:                                panic("pool_get: urgent");
1.21      thorpej   688:
1.3       pk        689:                        if ((flags & PR_WAITOK) == 0) {
                    690:                                pp->pr_nfail++;
1.25      thorpej   691:                                pr_leave(pp);
1.21      thorpej   692:                                simple_unlock(&pp->pr_slock);
1.1       pk        693:                                return (NULL);
1.3       pk        694:                        }
                    695:
1.15      pk        696:                        /*
                    697:                         * Wait for items to be returned to this pool.
1.21      thorpej   698:                         *
1.15      pk        699:                         * XXX: we actually want to wait just until
                    700:                         * the page allocator has memory again. Depending
                    701:                         * on this pool's usage, we might get stuck here
                    702:                         * for a long time.
1.20      thorpej   703:                         *
                    704:                         * XXX: maybe we should wake up once a second and
                    705:                         * try again?
1.15      pk        706:                         */
1.1       pk        707:                        pp->pr_flags |= PR_WANTED;
1.25      thorpej   708:                        pr_leave(pp);
1.40      sommerfe  709:                        ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
1.25      thorpej   710:                        pr_enter(pp, file, line);
1.20      thorpej   711:                        goto startover;
1.1       pk        712:                }
1.3       pk        713:
1.15      pk        714:                /* We have more memory; add it to the pool */
1.55      thorpej   715:                pool_prime_page(pp, v, ph);
1.15      pk        716:                pp->pr_npagealloc++;
                    717:
1.20      thorpej   718:                /* Start the allocation process over. */
                    719:                goto startover;
1.3       pk        720:        }
                    721:
1.34      thorpej   722:        if (__predict_false((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)) {
1.25      thorpej   723:                pr_leave(pp);
1.21      thorpej   724:                simple_unlock(&pp->pr_slock);
1.3       pk        725:                panic("pool_get: %s: page empty", pp->pr_wchan);
1.21      thorpej   726:        }
1.20      thorpej   727: #ifdef DIAGNOSTIC
1.34      thorpej   728:        if (__predict_false(pp->pr_nitems == 0)) {
1.25      thorpej   729:                pr_leave(pp);
1.21      thorpej   730:                simple_unlock(&pp->pr_slock);
1.20      thorpej   731:                printf("pool_get: %s: items on itemlist, nitems %u\n",
                    732:                    pp->pr_wchan, pp->pr_nitems);
                    733:                panic("pool_get: nitems inconsistent\n");
                    734:        }
1.56    ! sommerfe  735:
1.3       pk        736:        pr_log(pp, v, PRLOG_GET, file, line);
                    737:
1.34      thorpej   738:        if (__predict_false(pi->pi_magic != PI_MAGIC)) {
1.25      thorpej   739:                pr_printlog(pp, pi, printf);
1.3       pk        740:                panic("pool_get(%s): free list modified: magic=%x; page %p;"
                    741:                       " item addr %p\n",
                    742:                        pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
                    743:        }
                    744: #endif
                    745:
                    746:        /*
                    747:         * Remove from item list.
                    748:         */
                    749:        TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
1.20      thorpej   750:        pp->pr_nitems--;
                    751:        pp->pr_nout++;
1.6       thorpej   752:        if (ph->ph_nmissing == 0) {
                    753: #ifdef DIAGNOSTIC
1.34      thorpej   754:                if (__predict_false(pp->pr_nidle == 0))
1.6       thorpej   755:                        panic("pool_get: nidle inconsistent");
                    756: #endif
                    757:                pp->pr_nidle--;
                    758:        }
1.3       pk        759:        ph->ph_nmissing++;
                    760:        if (TAILQ_FIRST(&ph->ph_itemlist) == NULL) {
1.21      thorpej   761: #ifdef DIAGNOSTIC
1.34      thorpej   762:                if (__predict_false(ph->ph_nmissing != pp->pr_itemsperpage)) {
1.25      thorpej   763:                        pr_leave(pp);
1.21      thorpej   764:                        simple_unlock(&pp->pr_slock);
                    765:                        panic("pool_get: %s: nmissing inconsistent",
                    766:                            pp->pr_wchan);
                    767:                }
                    768: #endif
1.3       pk        769:                /*
                    770:                 * Find a new non-empty page header, if any.
                    771:                 * Start search from the page head, to increase
                    772:                 * the chance for "high water" pages to be freed.
                    773:                 *
1.21      thorpej   774:                 * Migrate empty pages to the end of the list.  This
                    775:                 * will speed the update of curpage as pages become
                    776:                 * idle.  Empty pages intermingled with idle pages
                    777:                 * is no big deal.  As soon as a page becomes un-empty,
                    778:                 * it will move back to the head of the list.
1.3       pk        779:                 */
                    780:                TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
1.21      thorpej   781:                TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
                    782:                for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
                    783:                     ph = TAILQ_NEXT(ph, ph_pagelist))
1.3       pk        784:                        if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
                    785:                                break;
                    786:
                    787:                pp->pr_curpage = ph;
1.1       pk        788:        }
1.3       pk        789:
                    790:        pp->pr_nget++;
1.20      thorpej   791:
                    792:        /*
                    793:         * If we have a low water mark and we are now below that low
                    794:         * water mark, add more items to the pool.
                    795:         */
1.53      thorpej   796:        if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20      thorpej   797:                /*
                    798:                 * XXX: Should we log a warning?  Should we set up a timeout
                    799:                 * to try again in a second or so?  The latter could break
                    800:                 * a caller's assumptions about interrupt protection, etc.
                    801:                 */
                    802:        }
                    803:
1.25      thorpej   804:        pr_leave(pp);
1.21      thorpej   805:        simple_unlock(&pp->pr_slock);
1.1       pk        806:        return (v);
                    807: }
                    808:
                    809: /*
1.43      thorpej   810:  * Internal version of pool_put().  Pool is already locked/entered.
1.1       pk        811:  */
1.43      thorpej   812: static void
1.56    ! sommerfe  813: pool_do_put(struct pool *pp, void *v)
1.1       pk        814: {
                    815:        struct pool_item *pi = v;
1.3       pk        816:        struct pool_item_header *ph;
                    817:        caddr_t page;
1.21      thorpej   818:        int s;
1.3       pk        819:
                    820:        page = (caddr_t)((u_long)v & pp->pr_pagemask);
1.1       pk        821:
1.30      thorpej   822: #ifdef DIAGNOSTIC
1.34      thorpej   823:        if (__predict_false(pp->pr_nout == 0)) {
1.30      thorpej   824:                printf("pool %s: putting with none out\n",
                    825:                    pp->pr_wchan);
                    826:                panic("pool_put");
                    827:        }
                    828: #endif
1.3       pk        829:
1.34      thorpej   830:        if (__predict_false((ph = pr_find_pagehead(pp, page)) == NULL)) {
1.25      thorpej   831:                pr_printlog(pp, NULL, printf);
1.3       pk        832:                panic("pool_put: %s: page header missing", pp->pr_wchan);
                    833:        }
1.28      thorpej   834:
                    835: #ifdef LOCKDEBUG
                    836:        /*
                    837:         * Check if we're freeing a locked simple lock.
                    838:         */
                    839:        simple_lock_freecheck((caddr_t)pi, ((caddr_t)pi) + pp->pr_size);
                    840: #endif
1.3       pk        841:
                    842:        /*
                    843:         * Return to item list.
                    844:         */
1.2       pk        845: #ifdef DIAGNOSTIC
1.3       pk        846:        pi->pi_magic = PI_MAGIC;
                    847: #endif
1.32      chs       848: #ifdef DEBUG
                    849:        {
                    850:                int i, *ip = v;
                    851:
                    852:                for (i = 0; i < pp->pr_size / sizeof(int); i++) {
                    853:                        *ip++ = PI_MAGIC;
                    854:                }
                    855:        }
                    856: #endif
                    857:
1.3       pk        858:        TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
                    859:        ph->ph_nmissing--;
                    860:        pp->pr_nput++;
1.20      thorpej   861:        pp->pr_nitems++;
                    862:        pp->pr_nout--;
1.3       pk        863:
                    864:        /* Cancel "pool empty" condition if it exists */
                    865:        if (pp->pr_curpage == NULL)
                    866:                pp->pr_curpage = ph;
                    867:
                    868:        if (pp->pr_flags & PR_WANTED) {
                    869:                pp->pr_flags &= ~PR_WANTED;
1.15      pk        870:                if (ph->ph_nmissing == 0)
                    871:                        pp->pr_nidle++;
1.3       pk        872:                wakeup((caddr_t)pp);
                    873:                return;
                    874:        }
                    875:
                    876:        /*
1.21      thorpej   877:         * If this page is now complete, do one of two things:
                    878:         *
                    879:         *      (1) If we have more pages than the page high water
                    880:         *          mark, free the page back to the system.
                    881:         *
                    882:         *      (2) Move it to the end of the page list, so that
                    883:         *          we minimize our chances of fragmenting the
                    884:         *          pool.  Idle pages migrate to the end (along with
                    885:         *          completely empty pages, so that we find un-empty
                    886:         *          pages more quickly when we update curpage) of the
                    887:         *          list so they can be more easily swept up by
                    888:         *          the pagedaemon when pages are scarce.
1.3       pk        889:         */
                    890:        if (ph->ph_nmissing == 0) {
1.6       thorpej   891:                pp->pr_nidle++;
1.3       pk        892:                if (pp->pr_npages > pp->pr_maxpages) {
                    893:                        pr_rmpage(pp, ph);
                    894:                } else {
                    895:                        TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
                    896:                        TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
                    897:
1.21      thorpej   898:                        /*
                    899:                         * Update the timestamp on the page.  A page must
                    900:                         * be idle for some period of time before it can
                    901:                         * be reclaimed by the pagedaemon.  This minimizes
                    902:                         * ping-pong'ing for memory.
                    903:                         */
                    904:                        s = splclock();
                    905:                        ph->ph_time = mono_time;
                    906:                        splx(s);
                    907:
                    908:                        /*
                    909:                         * Update the current page pointer.  Just look for
                    910:                         * the first page with any free items.
                    911:                         *
                    912:                         * XXX: Maybe we want an option to look for the
                    913:                         * page with the fewest available items, to minimize
                    914:                         * fragmentation?
                    915:                         */
1.3       pk        916:                        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
                    917:                             ph = TAILQ_NEXT(ph, ph_pagelist))
                    918:                                if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
                    919:                                        break;
1.1       pk        920:
1.3       pk        921:                        pp->pr_curpage = ph;
1.1       pk        922:                }
                    923:        }
1.21      thorpej   924:        /*
                    925:         * If the page has just become un-empty, move it to the head of
                    926:         * the list, and make it the current page.  The next allocation
                    927:         * will get the item from this page, instead of further fragmenting
                    928:         * the pool.
                    929:         */
                    930:        else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
                    931:                TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
                    932:                TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
                    933:                pp->pr_curpage = ph;
                    934:        }
1.43      thorpej   935: }
                    936:
                    937: /*
                    938:  * Return resource to the pool; must be called at appropriate spl level
                    939:  */
1.56    ! sommerfe  940: #ifdef DIAGNOSTIC
1.43      thorpej   941: void
                    942: _pool_put(struct pool *pp, void *v, const char *file, long line)
                    943: {
                    944:
                    945:        simple_lock(&pp->pr_slock);
                    946:        pr_enter(pp, file, line);
                    947:
1.56    ! sommerfe  948:        pr_log(pp, v, PRLOG_PUT, file, line);
        !           949:
        !           950:        pool_do_put(pp, v);
1.21      thorpej   951:
1.25      thorpej   952:        pr_leave(pp);
1.21      thorpej   953:        simple_unlock(&pp->pr_slock);
1.1       pk        954: }
                    955:
1.56    ! sommerfe  956: #else
        !           957: void
        !           958: pool_put(struct pool *pp, void *v)
        !           959: {
        !           960:
        !           961:        simple_lock(&pp->pr_slock);
        !           962:
        !           963:        pool_do_put(pp, v);
        !           964:
        !           965:        simple_unlock(&pp->pr_slock);
        !           966: }
        !           967: #endif
        !           968:
1.1       pk        969: /*
1.55      thorpej   970:  * Add N items to the pool.
                    971:  */
                    972: int
                    973: pool_prime(struct pool *pp, int n)
                    974: {
                    975:        struct pool_item_header *ph;
                    976:        caddr_t cp;
                    977:        int newpages, error = 0;
                    978:
                    979:        simple_lock(&pp->pr_slock);
                    980:
                    981:        newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
                    982:
                    983:        while (newpages-- > 0) {
                    984:                simple_unlock(&pp->pr_slock);
                    985:                cp = (*pp->pr_alloc)(pp->pr_pagesz, PR_NOWAIT, pp->pr_mtype);
                    986:                if (__predict_true(cp != NULL))
                    987:                        ph = pool_alloc_item_header(pp, cp, PR_NOWAIT);
                    988:                simple_lock(&pp->pr_slock);
                    989:
                    990:                if (__predict_false(cp == NULL || ph == NULL)) {
                    991:                        error = ENOMEM;
                    992:                        if (cp != NULL)
                    993:                                (*pp->pr_free)(cp, pp->pr_pagesz, pp->pr_mtype);
                    994:                        break;
                    995:                }
                    996:
                    997:                pool_prime_page(pp, cp, ph);
                    998:                pp->pr_npagealloc++;
                    999:                pp->pr_minpages++;
                   1000:        }
                   1001:
                   1002:        if (pp->pr_minpages >= pp->pr_maxpages)
                   1003:                pp->pr_maxpages = pp->pr_minpages + 1;  /* XXX */
                   1004:
                   1005:        simple_unlock(&pp->pr_slock);
                   1006:        return (0);
                   1007: }
                   1008:
                   1009: /*
1.3       pk       1010:  * Add a page worth of items to the pool.
1.21      thorpej  1011:  *
                   1012:  * Note, we must be called with the pool descriptor LOCKED.
1.3       pk       1013:  */
1.55      thorpej  1014: static void
                   1015: pool_prime_page(struct pool *pp, caddr_t storage, struct pool_item_header *ph)
1.3       pk       1016: {
                   1017:        struct pool_item *pi;
                   1018:        caddr_t cp = storage;
                   1019:        unsigned int align = pp->pr_align;
                   1020:        unsigned int ioff = pp->pr_itemoffset;
1.55      thorpej  1021:        int n;
1.36      pk       1022:
                   1023:        if (((u_long)cp & (pp->pr_pagesz - 1)) != 0)
                   1024:                panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1.3       pk       1025:
1.55      thorpej  1026:        if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1.3       pk       1027:                LIST_INSERT_HEAD(&pp->pr_hashtab[PR_HASH_INDEX(pp, cp)],
1.55      thorpej  1028:                    ph, ph_hashlist);
1.3       pk       1029:
                   1030:        /*
                   1031:         * Insert page header.
                   1032:         */
                   1033:        TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
                   1034:        TAILQ_INIT(&ph->ph_itemlist);
                   1035:        ph->ph_page = storage;
                   1036:        ph->ph_nmissing = 0;
1.21      thorpej  1037:        memset(&ph->ph_time, 0, sizeof(ph->ph_time));
1.3       pk       1038:
1.6       thorpej  1039:        pp->pr_nidle++;
                   1040:
1.3       pk       1041:        /*
                   1042:         * Color this page.
                   1043:         */
                   1044:        cp = (caddr_t)(cp + pp->pr_curcolor);
                   1045:        if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
                   1046:                pp->pr_curcolor = 0;
                   1047:
                   1048:        /*
                   1049:         * Adjust storage to apply aligment to `pr_itemoffset' in each item.
                   1050:         */
                   1051:        if (ioff != 0)
                   1052:                cp = (caddr_t)(cp + (align - ioff));
                   1053:
                   1054:        /*
                   1055:         * Insert remaining chunks on the bucket list.
                   1056:         */
                   1057:        n = pp->pr_itemsperpage;
1.20      thorpej  1058:        pp->pr_nitems += n;
1.3       pk       1059:
                   1060:        while (n--) {
                   1061:                pi = (struct pool_item *)cp;
                   1062:
                   1063:                /* Insert on page list */
                   1064:                TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
                   1065: #ifdef DIAGNOSTIC
                   1066:                pi->pi_magic = PI_MAGIC;
                   1067: #endif
                   1068:                cp = (caddr_t)(cp + pp->pr_size);
                   1069:        }
                   1070:
                   1071:        /*
                   1072:         * If the pool was depleted, point at the new page.
                   1073:         */
                   1074:        if (pp->pr_curpage == NULL)
                   1075:                pp->pr_curpage = ph;
                   1076:
                   1077:        if (++pp->pr_npages > pp->pr_hiwat)
                   1078:                pp->pr_hiwat = pp->pr_npages;
                   1079: }
                   1080:
1.20      thorpej  1081: /*
1.52      thorpej  1082:  * Used by pool_get() when nitems drops below the low water mark.  This
                   1083:  * is used to catch up nitmes with the low water mark.
1.20      thorpej  1084:  *
1.21      thorpej  1085:  * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20      thorpej  1086:  *
                   1087:  * Note 2, this doesn't work with static pools.
                   1088:  *
                   1089:  * Note 3, we must be called with the pool already locked, and we return
                   1090:  * with it locked.
                   1091:  */
                   1092: static int
1.42      thorpej  1093: pool_catchup(struct pool *pp)
1.20      thorpej  1094: {
1.55      thorpej  1095:        struct pool_item_header *ph;
1.20      thorpej  1096:        caddr_t cp;
                   1097:        int error = 0;
                   1098:
                   1099:        if (pp->pr_roflags & PR_STATIC) {
                   1100:                /*
                   1101:                 * We dropped below the low water mark, and this is not a
                   1102:                 * good thing.  Log a warning.
1.21      thorpej  1103:                 *
                   1104:                 * XXX: rate-limit this?
1.20      thorpej  1105:                 */
                   1106:                printf("WARNING: static pool `%s' dropped below low water "
                   1107:                    "mark\n", pp->pr_wchan);
                   1108:                return (0);
                   1109:        }
                   1110:
1.54      thorpej  1111:        while (POOL_NEEDS_CATCHUP(pp)) {
1.20      thorpej  1112:                /*
1.21      thorpej  1113:                 * Call the page back-end allocator for more memory.
                   1114:                 *
                   1115:                 * XXX: We never wait, so should we bother unlocking
                   1116:                 * the pool descriptor?
1.20      thorpej  1117:                 */
1.21      thorpej  1118:                simple_unlock(&pp->pr_slock);
1.55      thorpej  1119:                cp = (*pp->pr_alloc)(pp->pr_pagesz, PR_NOWAIT, pp->pr_mtype);
                   1120:                if (__predict_true(cp != NULL))
                   1121:                        ph = pool_alloc_item_header(pp, cp, PR_NOWAIT);
1.21      thorpej  1122:                simple_lock(&pp->pr_slock);
1.55      thorpej  1123:                if (__predict_false(cp == NULL || ph == NULL)) {
                   1124:                        if (cp != NULL)
                   1125:                                (*pp->pr_free)(cp, pp->pr_pagesz, pp->pr_mtype);
1.20      thorpej  1126:                        error = ENOMEM;
                   1127:                        break;
                   1128:                }
1.55      thorpej  1129:                pool_prime_page(pp, cp, ph);
1.26      thorpej  1130:                pp->pr_npagealloc++;
1.20      thorpej  1131:        }
                   1132:
                   1133:        return (error);
                   1134: }
                   1135:
1.3       pk       1136: void
1.42      thorpej  1137: pool_setlowat(struct pool *pp, int n)
1.3       pk       1138: {
1.20      thorpej  1139:        int error;
1.15      pk       1140:
1.21      thorpej  1141:        simple_lock(&pp->pr_slock);
                   1142:
1.3       pk       1143:        pp->pr_minitems = n;
1.15      pk       1144:        pp->pr_minpages = (n == 0)
                   1145:                ? 0
1.18      thorpej  1146:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20      thorpej  1147:
                   1148:        /* Make sure we're caught up with the newly-set low water mark. */
1.53      thorpej  1149:        if (POOL_NEEDS_CATCHUP(pp) && (error = pool_catchup(pp) != 0)) {
1.20      thorpej  1150:                /*
                   1151:                 * XXX: Should we log a warning?  Should we set up a timeout
                   1152:                 * to try again in a second or so?  The latter could break
                   1153:                 * a caller's assumptions about interrupt protection, etc.
                   1154:                 */
                   1155:        }
1.21      thorpej  1156:
                   1157:        simple_unlock(&pp->pr_slock);
1.3       pk       1158: }
                   1159:
                   1160: void
1.42      thorpej  1161: pool_sethiwat(struct pool *pp, int n)
1.3       pk       1162: {
1.15      pk       1163:
1.21      thorpej  1164:        simple_lock(&pp->pr_slock);
                   1165:
1.15      pk       1166:        pp->pr_maxpages = (n == 0)
                   1167:                ? 0
1.18      thorpej  1168:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21      thorpej  1169:
                   1170:        simple_unlock(&pp->pr_slock);
1.3       pk       1171: }
                   1172:
1.20      thorpej  1173: void
1.42      thorpej  1174: pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1.20      thorpej  1175: {
                   1176:
1.21      thorpej  1177:        simple_lock(&pp->pr_slock);
1.20      thorpej  1178:
                   1179:        pp->pr_hardlimit = n;
                   1180:        pp->pr_hardlimit_warning = warnmess;
1.31      thorpej  1181:        pp->pr_hardlimit_ratecap.tv_sec = ratecap;
                   1182:        pp->pr_hardlimit_warning_last.tv_sec = 0;
                   1183:        pp->pr_hardlimit_warning_last.tv_usec = 0;
1.20      thorpej  1184:
                   1185:        /*
1.21      thorpej  1186:         * In-line version of pool_sethiwat(), because we don't want to
                   1187:         * release the lock.
1.20      thorpej  1188:         */
                   1189:        pp->pr_maxpages = (n == 0)
                   1190:                ? 0
                   1191:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21      thorpej  1192:
                   1193:        simple_unlock(&pp->pr_slock);
1.20      thorpej  1194: }
1.3       pk       1195:
                   1196: /*
                   1197:  * Default page allocator.
                   1198:  */
                   1199: static void *
1.42      thorpej  1200: pool_page_alloc(unsigned long sz, int flags, int mtype)
1.3       pk       1201: {
1.11      thorpej  1202:        boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1.3       pk       1203:
1.11      thorpej  1204:        return ((void *)uvm_km_alloc_poolpage(waitok));
1.3       pk       1205: }
                   1206:
                   1207: static void
1.42      thorpej  1208: pool_page_free(void *v, unsigned long sz, int mtype)
1.3       pk       1209: {
                   1210:
1.10      eeh      1211:        uvm_km_free_poolpage((vaddr_t)v);
1.3       pk       1212: }
1.12      thorpej  1213:
                   1214: /*
                   1215:  * Alternate pool page allocator for pools that know they will
                   1216:  * never be accessed in interrupt context.
                   1217:  */
                   1218: void *
1.42      thorpej  1219: pool_page_alloc_nointr(unsigned long sz, int flags, int mtype)
1.12      thorpej  1220: {
                   1221:        boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
                   1222:
                   1223:        return ((void *)uvm_km_alloc_poolpage1(kernel_map, uvm.kernel_object,
                   1224:            waitok));
                   1225: }
                   1226:
                   1227: void
1.42      thorpej  1228: pool_page_free_nointr(void *v, unsigned long sz, int mtype)
1.12      thorpej  1229: {
                   1230:
                   1231:        uvm_km_free_poolpage1(kernel_map, (vaddr_t)v);
                   1232: }
                   1233:
1.3       pk       1234:
                   1235: /*
                   1236:  * Release all complete pages that have not been used recently.
                   1237:  */
                   1238: void
1.56    ! sommerfe 1239: #ifdef DIAGNOSTIC
1.42      thorpej  1240: _pool_reclaim(struct pool *pp, const char *file, long line)
1.56    ! sommerfe 1241: #else
        !          1242: pool_reclaim(struct pool *pp)
        !          1243: #endif
1.3       pk       1244: {
                   1245:        struct pool_item_header *ph, *phnext;
1.43      thorpej  1246:        struct pool_cache *pc;
1.21      thorpej  1247:        struct timeval curtime;
                   1248:        int s;
1.3       pk       1249:
1.20      thorpej  1250:        if (pp->pr_roflags & PR_STATIC)
1.3       pk       1251:                return;
                   1252:
1.21      thorpej  1253:        if (simple_lock_try(&pp->pr_slock) == 0)
1.3       pk       1254:                return;
1.25      thorpej  1255:        pr_enter(pp, file, line);
1.3       pk       1256:
1.43      thorpej  1257:        /*
                   1258:         * Reclaim items from the pool's caches.
                   1259:         */
                   1260:        for (pc = TAILQ_FIRST(&pp->pr_cachelist); pc != NULL;
                   1261:             pc = TAILQ_NEXT(pc, pc_poollist))
                   1262:                pool_cache_reclaim(pc);
                   1263:
1.21      thorpej  1264:        s = splclock();
                   1265:        curtime = mono_time;
                   1266:        splx(s);
                   1267:
1.3       pk       1268:        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL; ph = phnext) {
                   1269:                phnext = TAILQ_NEXT(ph, ph_pagelist);
                   1270:
                   1271:                /* Check our minimum page claim */
                   1272:                if (pp->pr_npages <= pp->pr_minpages)
                   1273:                        break;
                   1274:
                   1275:                if (ph->ph_nmissing == 0) {
                   1276:                        struct timeval diff;
                   1277:                        timersub(&curtime, &ph->ph_time, &diff);
                   1278:                        if (diff.tv_sec < pool_inactive_time)
                   1279:                                continue;
1.21      thorpej  1280:
                   1281:                        /*
                   1282:                         * If freeing this page would put us below
                   1283:                         * the low water mark, stop now.
                   1284:                         */
                   1285:                        if ((pp->pr_nitems - pp->pr_itemsperpage) <
                   1286:                            pp->pr_minitems)
                   1287:                                break;
                   1288:
1.3       pk       1289:                        pr_rmpage(pp, ph);
                   1290:                }
                   1291:        }
                   1292:
1.25      thorpej  1293:        pr_leave(pp);
1.21      thorpej  1294:        simple_unlock(&pp->pr_slock);
1.3       pk       1295: }
                   1296:
                   1297:
                   1298: /*
                   1299:  * Drain pools, one at a time.
1.21      thorpej  1300:  *
                   1301:  * Note, we must never be called from an interrupt context.
1.3       pk       1302:  */
                   1303: void
1.42      thorpej  1304: pool_drain(void *arg)
1.3       pk       1305: {
                   1306:        struct pool *pp;
1.23      thorpej  1307:        int s;
1.3       pk       1308:
1.49      thorpej  1309:        s = splvm();
1.23      thorpej  1310:        simple_lock(&pool_head_slock);
                   1311:
                   1312:        if (drainpp == NULL && (drainpp = TAILQ_FIRST(&pool_head)) == NULL)
                   1313:                goto out;
1.3       pk       1314:
                   1315:        pp = drainpp;
                   1316:        drainpp = TAILQ_NEXT(pp, pr_poollist);
                   1317:
                   1318:        pool_reclaim(pp);
1.23      thorpej  1319:
                   1320:  out:
                   1321:        simple_unlock(&pool_head_slock);
1.3       pk       1322:        splx(s);
                   1323: }
                   1324:
                   1325:
                   1326: /*
                   1327:  * Diagnostic helpers.
                   1328:  */
                   1329: void
1.42      thorpej  1330: pool_print(struct pool *pp, const char *modif)
1.21      thorpej  1331: {
                   1332:        int s;
                   1333:
1.49      thorpej  1334:        s = splvm();
1.25      thorpej  1335:        if (simple_lock_try(&pp->pr_slock) == 0) {
                   1336:                printf("pool %s is locked; try again later\n",
                   1337:                    pp->pr_wchan);
                   1338:                splx(s);
                   1339:                return;
                   1340:        }
                   1341:        pool_print1(pp, modif, printf);
1.21      thorpej  1342:        simple_unlock(&pp->pr_slock);
                   1343:        splx(s);
                   1344: }
                   1345:
1.25      thorpej  1346: void
1.42      thorpej  1347: pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.25      thorpej  1348: {
                   1349:        int didlock = 0;
                   1350:
                   1351:        if (pp == NULL) {
                   1352:                (*pr)("Must specify a pool to print.\n");
                   1353:                return;
                   1354:        }
                   1355:
                   1356:        /*
                   1357:         * Called from DDB; interrupts should be blocked, and all
                   1358:         * other processors should be paused.  We can skip locking
                   1359:         * the pool in this case.
                   1360:         *
                   1361:         * We do a simple_lock_try() just to print the lock
                   1362:         * status, however.
                   1363:         */
                   1364:
                   1365:        if (simple_lock_try(&pp->pr_slock) == 0)
                   1366:                (*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
                   1367:        else
                   1368:                didlock = 1;
                   1369:
                   1370:        pool_print1(pp, modif, pr);
                   1371:
                   1372:        if (didlock)
                   1373:                simple_unlock(&pp->pr_slock);
                   1374: }
                   1375:
1.21      thorpej  1376: static void
1.42      thorpej  1377: pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.3       pk       1378: {
1.25      thorpej  1379:        struct pool_item_header *ph;
1.44      thorpej  1380:        struct pool_cache *pc;
                   1381:        struct pool_cache_group *pcg;
1.25      thorpej  1382: #ifdef DIAGNOSTIC
                   1383:        struct pool_item *pi;
                   1384: #endif
1.44      thorpej  1385:        int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1.25      thorpej  1386:        char c;
                   1387:
                   1388:        while ((c = *modif++) != '\0') {
                   1389:                if (c == 'l')
                   1390:                        print_log = 1;
                   1391:                if (c == 'p')
                   1392:                        print_pagelist = 1;
1.44      thorpej  1393:                if (c == 'c')
                   1394:                        print_cache = 1;
1.25      thorpej  1395:                modif++;
                   1396:        }
                   1397:
                   1398:        (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
                   1399:            pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
                   1400:            pp->pr_roflags);
                   1401:        (*pr)("\tpagesz %u, mtype %d\n", pp->pr_pagesz, pp->pr_mtype);
                   1402:        (*pr)("\talloc %p, release %p\n", pp->pr_alloc, pp->pr_free);
                   1403:        (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
                   1404:            pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
                   1405:        (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
                   1406:            pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
                   1407:
                   1408:        (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
                   1409:            pp->pr_nget, pp->pr_nfail, pp->pr_nput);
                   1410:        (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
                   1411:            pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
                   1412:
                   1413:        if (print_pagelist == 0)
                   1414:                goto skip_pagelist;
                   1415:
                   1416:        if ((ph = TAILQ_FIRST(&pp->pr_pagelist)) != NULL)
                   1417:                (*pr)("\n\tpage list:\n");
                   1418:        for (; ph != NULL; ph = TAILQ_NEXT(ph, ph_pagelist)) {
                   1419:                (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
                   1420:                    ph->ph_page, ph->ph_nmissing,
                   1421:                    (u_long)ph->ph_time.tv_sec,
                   1422:                    (u_long)ph->ph_time.tv_usec);
                   1423: #ifdef DIAGNOSTIC
                   1424:                for (pi = TAILQ_FIRST(&ph->ph_itemlist); pi != NULL;
                   1425:                     pi = TAILQ_NEXT(pi, pi_list)) {
                   1426:                        if (pi->pi_magic != PI_MAGIC) {
                   1427:                                (*pr)("\t\t\titem %p, magic 0x%x\n",
                   1428:                                    pi, pi->pi_magic);
                   1429:                        }
                   1430:                }
                   1431: #endif
                   1432:        }
                   1433:        if (pp->pr_curpage == NULL)
                   1434:                (*pr)("\tno current page\n");
                   1435:        else
                   1436:                (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
                   1437:
                   1438:  skip_pagelist:
                   1439:
                   1440:        if (print_log == 0)
                   1441:                goto skip_log;
                   1442:
                   1443:        (*pr)("\n");
                   1444:        if ((pp->pr_roflags & PR_LOGGING) == 0)
                   1445:                (*pr)("\tno log\n");
                   1446:        else
                   1447:                pr_printlog(pp, NULL, pr);
1.3       pk       1448:
1.25      thorpej  1449:  skip_log:
1.44      thorpej  1450:
                   1451:        if (print_cache == 0)
                   1452:                goto skip_cache;
                   1453:
                   1454:        for (pc = TAILQ_FIRST(&pp->pr_cachelist); pc != NULL;
                   1455:             pc = TAILQ_NEXT(pc, pc_poollist)) {
                   1456:                (*pr)("\tcache %p: allocfrom %p freeto %p\n", pc,
                   1457:                    pc->pc_allocfrom, pc->pc_freeto);
1.48      thorpej  1458:                (*pr)("\t    hits %lu misses %lu ngroups %lu nitems %lu\n",
                   1459:                    pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems);
1.44      thorpej  1460:                for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL;
                   1461:                     pcg = TAILQ_NEXT(pcg, pcg_list)) {
                   1462:                        (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail);
                   1463:                        for (i = 0; i < PCG_NOBJECTS; i++)
                   1464:                                (*pr)("\t\t\t%p\n", pcg->pcg_objects[i]);
                   1465:                }
                   1466:        }
                   1467:
                   1468:  skip_cache:
1.3       pk       1469:
1.25      thorpej  1470:        pr_enter_check(pp, pr);
1.3       pk       1471: }
                   1472:
                   1473: int
1.42      thorpej  1474: pool_chk(struct pool *pp, const char *label)
1.3       pk       1475: {
                   1476:        struct pool_item_header *ph;
                   1477:        int r = 0;
                   1478:
1.21      thorpej  1479:        simple_lock(&pp->pr_slock);
1.3       pk       1480:
                   1481:        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
                   1482:             ph = TAILQ_NEXT(ph, ph_pagelist)) {
                   1483:
                   1484:                struct pool_item *pi;
                   1485:                int n;
                   1486:                caddr_t page;
                   1487:
                   1488:                page = (caddr_t)((u_long)ph & pp->pr_pagemask);
1.20      thorpej  1489:                if (page != ph->ph_page &&
                   1490:                    (pp->pr_roflags & PR_PHINPAGE) != 0) {
1.3       pk       1491:                        if (label != NULL)
                   1492:                                printf("%s: ", label);
1.16      briggs   1493:                        printf("pool(%p:%s): page inconsistency: page %p;"
                   1494:                               " at page head addr %p (p %p)\n", pp,
1.3       pk       1495:                                pp->pr_wchan, ph->ph_page,
                   1496:                                ph, page);
                   1497:                        r++;
                   1498:                        goto out;
                   1499:                }
                   1500:
                   1501:                for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
                   1502:                     pi != NULL;
                   1503:                     pi = TAILQ_NEXT(pi,pi_list), n++) {
                   1504:
                   1505: #ifdef DIAGNOSTIC
                   1506:                        if (pi->pi_magic != PI_MAGIC) {
                   1507:                                if (label != NULL)
                   1508:                                        printf("%s: ", label);
                   1509:                                printf("pool(%s): free list modified: magic=%x;"
                   1510:                                       " page %p; item ordinal %d;"
                   1511:                                       " addr %p (p %p)\n",
                   1512:                                        pp->pr_wchan, pi->pi_magic, ph->ph_page,
                   1513:                                        n, pi, page);
                   1514:                                panic("pool");
                   1515:                        }
                   1516: #endif
                   1517:                        page = (caddr_t)((u_long)pi & pp->pr_pagemask);
                   1518:                        if (page == ph->ph_page)
                   1519:                                continue;
                   1520:
                   1521:                        if (label != NULL)
                   1522:                                printf("%s: ", label);
1.16      briggs   1523:                        printf("pool(%p:%s): page inconsistency: page %p;"
                   1524:                               " item ordinal %d; addr %p (p %p)\n", pp,
1.3       pk       1525:                                pp->pr_wchan, ph->ph_page,
                   1526:                                n, pi, page);
                   1527:                        r++;
                   1528:                        goto out;
                   1529:                }
                   1530:        }
                   1531: out:
1.21      thorpej  1532:        simple_unlock(&pp->pr_slock);
1.3       pk       1533:        return (r);
1.43      thorpej  1534: }
                   1535:
                   1536: /*
                   1537:  * pool_cache_init:
                   1538:  *
                   1539:  *     Initialize a pool cache.
                   1540:  *
                   1541:  *     NOTE: If the pool must be protected from interrupts, we expect
                   1542:  *     to be called at the appropriate interrupt priority level.
                   1543:  */
                   1544: void
                   1545: pool_cache_init(struct pool_cache *pc, struct pool *pp,
                   1546:     int (*ctor)(void *, void *, int),
                   1547:     void (*dtor)(void *, void *),
                   1548:     void *arg)
                   1549: {
                   1550:
                   1551:        TAILQ_INIT(&pc->pc_grouplist);
                   1552:        simple_lock_init(&pc->pc_slock);
                   1553:
                   1554:        pc->pc_allocfrom = NULL;
                   1555:        pc->pc_freeto = NULL;
                   1556:        pc->pc_pool = pp;
                   1557:
                   1558:        pc->pc_ctor = ctor;
                   1559:        pc->pc_dtor = dtor;
                   1560:        pc->pc_arg  = arg;
                   1561:
1.48      thorpej  1562:        pc->pc_hits   = 0;
                   1563:        pc->pc_misses = 0;
                   1564:
                   1565:        pc->pc_ngroups = 0;
                   1566:
                   1567:        pc->pc_nitems = 0;
                   1568:
1.43      thorpej  1569:        simple_lock(&pp->pr_slock);
                   1570:        TAILQ_INSERT_TAIL(&pp->pr_cachelist, pc, pc_poollist);
                   1571:        simple_unlock(&pp->pr_slock);
                   1572: }
                   1573:
                   1574: /*
                   1575:  * pool_cache_destroy:
                   1576:  *
                   1577:  *     Destroy a pool cache.
                   1578:  */
                   1579: void
                   1580: pool_cache_destroy(struct pool_cache *pc)
                   1581: {
                   1582:        struct pool *pp = pc->pc_pool;
                   1583:
                   1584:        /* First, invalidate the entire cache. */
                   1585:        pool_cache_invalidate(pc);
                   1586:
                   1587:        /* ...and remove it from the pool's cache list. */
                   1588:        simple_lock(&pp->pr_slock);
                   1589:        TAILQ_REMOVE(&pp->pr_cachelist, pc, pc_poollist);
                   1590:        simple_unlock(&pp->pr_slock);
                   1591: }
                   1592:
                   1593: static __inline void *
                   1594: pcg_get(struct pool_cache_group *pcg)
                   1595: {
                   1596:        void *object;
                   1597:        u_int idx;
                   1598:
                   1599:        KASSERT(pcg->pcg_avail <= PCG_NOBJECTS);
1.45      thorpej  1600:        KASSERT(pcg->pcg_avail != 0);
1.43      thorpej  1601:        idx = --pcg->pcg_avail;
                   1602:
                   1603:        KASSERT(pcg->pcg_objects[idx] != NULL);
                   1604:        object = pcg->pcg_objects[idx];
                   1605:        pcg->pcg_objects[idx] = NULL;
                   1606:
                   1607:        return (object);
                   1608: }
                   1609:
                   1610: static __inline void
                   1611: pcg_put(struct pool_cache_group *pcg, void *object)
                   1612: {
                   1613:        u_int idx;
                   1614:
                   1615:        KASSERT(pcg->pcg_avail < PCG_NOBJECTS);
                   1616:        idx = pcg->pcg_avail++;
                   1617:
                   1618:        KASSERT(pcg->pcg_objects[idx] == NULL);
                   1619:        pcg->pcg_objects[idx] = object;
                   1620: }
                   1621:
                   1622: /*
                   1623:  * pool_cache_get:
                   1624:  *
                   1625:  *     Get an object from a pool cache.
                   1626:  */
                   1627: void *
                   1628: pool_cache_get(struct pool_cache *pc, int flags)
                   1629: {
                   1630:        struct pool_cache_group *pcg;
                   1631:        void *object;
                   1632:
                   1633:        simple_lock(&pc->pc_slock);
                   1634:
                   1635:        if ((pcg = pc->pc_allocfrom) == NULL) {
                   1636:                for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL;
                   1637:                     pcg = TAILQ_NEXT(pcg, pcg_list)) {
                   1638:                        if (pcg->pcg_avail != 0) {
                   1639:                                pc->pc_allocfrom = pcg;
                   1640:                                goto have_group;
                   1641:                        }
                   1642:                }
                   1643:
                   1644:                /*
                   1645:                 * No groups with any available objects.  Allocate
                   1646:                 * a new object, construct it, and return it to
                   1647:                 * the caller.  We will allocate a group, if necessary,
                   1648:                 * when the object is freed back to the cache.
                   1649:                 */
1.48      thorpej  1650:                pc->pc_misses++;
1.43      thorpej  1651:                simple_unlock(&pc->pc_slock);
                   1652:                object = pool_get(pc->pc_pool, flags);
                   1653:                if (object != NULL && pc->pc_ctor != NULL) {
                   1654:                        if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) {
                   1655:                                pool_put(pc->pc_pool, object);
                   1656:                                return (NULL);
                   1657:                        }
                   1658:                }
                   1659:                return (object);
                   1660:        }
                   1661:
                   1662:  have_group:
1.48      thorpej  1663:        pc->pc_hits++;
                   1664:        pc->pc_nitems--;
1.43      thorpej  1665:        object = pcg_get(pcg);
                   1666:
                   1667:        if (pcg->pcg_avail == 0)
                   1668:                pc->pc_allocfrom = NULL;
1.45      thorpej  1669:
1.43      thorpej  1670:        simple_unlock(&pc->pc_slock);
                   1671:
                   1672:        return (object);
                   1673: }
                   1674:
                   1675: /*
                   1676:  * pool_cache_put:
                   1677:  *
                   1678:  *     Put an object back to the pool cache.
                   1679:  */
                   1680: void
                   1681: pool_cache_put(struct pool_cache *pc, void *object)
                   1682: {
                   1683:        struct pool_cache_group *pcg;
                   1684:
                   1685:        simple_lock(&pc->pc_slock);
                   1686:
                   1687:        if ((pcg = pc->pc_freeto) == NULL) {
                   1688:                for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL;
                   1689:                     pcg = TAILQ_NEXT(pcg, pcg_list)) {
                   1690:                        if (pcg->pcg_avail != PCG_NOBJECTS) {
                   1691:                                pc->pc_freeto = pcg;
                   1692:                                goto have_group;
                   1693:                        }
                   1694:                }
                   1695:
                   1696:                /*
                   1697:                 * No empty groups to free the object to.  Attempt to
1.47      thorpej  1698:                 * allocate one.
1.43      thorpej  1699:                 */
1.47      thorpej  1700:                simple_unlock(&pc->pc_slock);
1.43      thorpej  1701:                pcg = pool_get(&pcgpool, PR_NOWAIT);
                   1702:                if (pcg != NULL) {
                   1703:                        memset(pcg, 0, sizeof(*pcg));
1.47      thorpej  1704:                        simple_lock(&pc->pc_slock);
1.48      thorpej  1705:                        pc->pc_ngroups++;
1.43      thorpej  1706:                        TAILQ_INSERT_TAIL(&pc->pc_grouplist, pcg, pcg_list);
1.47      thorpej  1707:                        if (pc->pc_freeto == NULL)
                   1708:                                pc->pc_freeto = pcg;
1.43      thorpej  1709:                        goto have_group;
                   1710:                }
                   1711:
                   1712:                /*
                   1713:                 * Unable to allocate a cache group; destruct the object
                   1714:                 * and free it back to the pool.
                   1715:                 */
1.51      thorpej  1716:                pool_cache_destruct_object(pc, object);
1.43      thorpej  1717:                return;
                   1718:        }
                   1719:
                   1720:  have_group:
1.48      thorpej  1721:        pc->pc_nitems++;
1.43      thorpej  1722:        pcg_put(pcg, object);
                   1723:
                   1724:        if (pcg->pcg_avail == PCG_NOBJECTS)
                   1725:                pc->pc_freeto = NULL;
                   1726:
                   1727:        simple_unlock(&pc->pc_slock);
1.51      thorpej  1728: }
                   1729:
                   1730: /*
                   1731:  * pool_cache_destruct_object:
                   1732:  *
                   1733:  *     Force destruction of an object and its release back into
                   1734:  *     the pool.
                   1735:  */
                   1736: void
                   1737: pool_cache_destruct_object(struct pool_cache *pc, void *object)
                   1738: {
                   1739:
                   1740:        if (pc->pc_dtor != NULL)
                   1741:                (*pc->pc_dtor)(pc->pc_arg, object);
                   1742:        pool_put(pc->pc_pool, object);
1.43      thorpej  1743: }
                   1744:
                   1745: /*
                   1746:  * pool_cache_do_invalidate:
                   1747:  *
                   1748:  *     This internal function implements pool_cache_invalidate() and
                   1749:  *     pool_cache_reclaim().
                   1750:  */
                   1751: static void
                   1752: pool_cache_do_invalidate(struct pool_cache *pc, int free_groups,
1.56    ! sommerfe 1753:     void (*putit)(struct pool *, void *))
1.43      thorpej  1754: {
                   1755:        struct pool_cache_group *pcg, *npcg;
                   1756:        void *object;
                   1757:
                   1758:        for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL;
                   1759:             pcg = npcg) {
                   1760:                npcg = TAILQ_NEXT(pcg, pcg_list);
                   1761:                while (pcg->pcg_avail != 0) {
1.48      thorpej  1762:                        pc->pc_nitems--;
1.43      thorpej  1763:                        object = pcg_get(pcg);
1.45      thorpej  1764:                        if (pcg->pcg_avail == 0 && pc->pc_allocfrom == pcg)
                   1765:                                pc->pc_allocfrom = NULL;
1.43      thorpej  1766:                        if (pc->pc_dtor != NULL)
                   1767:                                (*pc->pc_dtor)(pc->pc_arg, object);
1.56    ! sommerfe 1768:                        (*putit)(pc->pc_pool, object);
1.43      thorpej  1769:                }
                   1770:                if (free_groups) {
1.48      thorpej  1771:                        pc->pc_ngroups--;
1.43      thorpej  1772:                        TAILQ_REMOVE(&pc->pc_grouplist, pcg, pcg_list);
1.46      thorpej  1773:                        if (pc->pc_freeto == pcg)
                   1774:                                pc->pc_freeto = NULL;
1.43      thorpej  1775:                        pool_put(&pcgpool, pcg);
                   1776:                }
                   1777:        }
                   1778: }
                   1779:
                   1780: /*
                   1781:  * pool_cache_invalidate:
                   1782:  *
                   1783:  *     Invalidate a pool cache (destruct and release all of the
                   1784:  *     cached objects).
                   1785:  */
                   1786: void
                   1787: pool_cache_invalidate(struct pool_cache *pc)
                   1788: {
                   1789:
                   1790:        simple_lock(&pc->pc_slock);
1.56    ! sommerfe 1791:        pool_cache_do_invalidate(pc, 0, pool_put);
1.43      thorpej  1792:        simple_unlock(&pc->pc_slock);
                   1793: }
                   1794:
                   1795: /*
                   1796:  * pool_cache_reclaim:
                   1797:  *
                   1798:  *     Reclaim a pool cache for pool_reclaim().
                   1799:  */
                   1800: static void
                   1801: pool_cache_reclaim(struct pool_cache *pc)
                   1802: {
                   1803:
1.47      thorpej  1804:        simple_lock(&pc->pc_slock);
1.43      thorpej  1805:        pool_cache_do_invalidate(pc, 1, pool_do_put);
                   1806:        simple_unlock(&pc->pc_slock);
1.3       pk       1807: }

CVSweb <webmaster@jp.NetBSD.org>