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

1.58    ! thorpej     1: /*     $NetBSD: subr_pool.c,v 1.57 2001/05/13 17:17:35 sommerfeld 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.58    ! thorpej   595:
        !           596: #ifdef LOCKDEBUG
        !           597:        if (flags & PR_WAITOK)
        !           598:                simple_lock_only_held(NULL, "pool_get(PR_WAITOK)");
1.56      sommerfe  599: #endif
1.58    ! thorpej   600: #endif /* DIAGNOSTIC */
1.1       pk        601:
1.21      thorpej   602:        simple_lock(&pp->pr_slock);
1.25      thorpej   603:        pr_enter(pp, file, line);
1.20      thorpej   604:
                    605:  startover:
                    606:        /*
                    607:         * Check to see if we've reached the hard limit.  If we have,
                    608:         * and we can wait, then wait until an item has been returned to
                    609:         * the pool.
                    610:         */
                    611: #ifdef DIAGNOSTIC
1.34      thorpej   612:        if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
1.25      thorpej   613:                pr_leave(pp);
1.21      thorpej   614:                simple_unlock(&pp->pr_slock);
1.20      thorpej   615:                panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
                    616:        }
                    617: #endif
1.34      thorpej   618:        if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
1.29      sommerfe  619:                if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
1.20      thorpej   620:                        /*
                    621:                         * XXX: A warning isn't logged in this case.  Should
                    622:                         * it be?
                    623:                         */
                    624:                        pp->pr_flags |= PR_WANTED;
1.25      thorpej   625:                        pr_leave(pp);
1.40      sommerfe  626:                        ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
1.25      thorpej   627:                        pr_enter(pp, file, line);
1.20      thorpej   628:                        goto startover;
                    629:                }
1.31      thorpej   630:
                    631:                /*
                    632:                 * Log a message that the hard limit has been hit.
                    633:                 */
                    634:                if (pp->pr_hardlimit_warning != NULL &&
                    635:                    ratecheck(&pp->pr_hardlimit_warning_last,
                    636:                              &pp->pr_hardlimit_ratecap))
                    637:                        log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1.21      thorpej   638:
                    639:                if (flags & PR_URGENT)
                    640:                        panic("pool_get: urgent");
                    641:
                    642:                pp->pr_nfail++;
                    643:
1.25      thorpej   644:                pr_leave(pp);
1.21      thorpej   645:                simple_unlock(&pp->pr_slock);
1.20      thorpej   646:                return (NULL);
                    647:        }
                    648:
1.3       pk        649:        /*
                    650:         * The convention we use is that if `curpage' is not NULL, then
                    651:         * it points at a non-empty bucket. In particular, `curpage'
                    652:         * never points at a page header which has PR_PHINPAGE set and
                    653:         * has no items in its bucket.
                    654:         */
1.20      thorpej   655:        if ((ph = pp->pr_curpage) == NULL) {
                    656: #ifdef DIAGNOSTIC
                    657:                if (pp->pr_nitems != 0) {
1.21      thorpej   658:                        simple_unlock(&pp->pr_slock);
1.20      thorpej   659:                        printf("pool_get: %s: curpage NULL, nitems %u\n",
                    660:                            pp->pr_wchan, pp->pr_nitems);
                    661:                        panic("pool_get: nitems inconsistent\n");
                    662:                }
                    663: #endif
                    664:
1.21      thorpej   665:                /*
                    666:                 * Call the back-end page allocator for more memory.
                    667:                 * Release the pool lock, as the back-end page allocator
                    668:                 * may block.
                    669:                 */
1.25      thorpej   670:                pr_leave(pp);
1.21      thorpej   671:                simple_unlock(&pp->pr_slock);
                    672:                v = (*pp->pr_alloc)(pp->pr_pagesz, flags, pp->pr_mtype);
1.55      thorpej   673:                if (__predict_true(v != NULL))
                    674:                        ph = pool_alloc_item_header(pp, v, flags);
1.21      thorpej   675:                simple_lock(&pp->pr_slock);
1.25      thorpej   676:                pr_enter(pp, file, line);
1.15      pk        677:
1.55      thorpej   678:                if (__predict_false(v == NULL || ph == NULL)) {
                    679:                        if (v != NULL)
                    680:                                (*pp->pr_free)(v, pp->pr_pagesz, pp->pr_mtype);
                    681:
1.21      thorpej   682:                        /*
1.55      thorpej   683:                         * We were unable to allocate a page or item
                    684:                         * header, but we released the lock during
                    685:                         * allocation, so perhaps items were freed
                    686:                         * back to the pool.  Check for this case.
1.21      thorpej   687:                         */
                    688:                        if (pp->pr_curpage != NULL)
                    689:                                goto startover;
1.15      pk        690:
1.3       pk        691:                        if (flags & PR_URGENT)
                    692:                                panic("pool_get: urgent");
1.21      thorpej   693:
1.3       pk        694:                        if ((flags & PR_WAITOK) == 0) {
                    695:                                pp->pr_nfail++;
1.25      thorpej   696:                                pr_leave(pp);
1.21      thorpej   697:                                simple_unlock(&pp->pr_slock);
1.1       pk        698:                                return (NULL);
1.3       pk        699:                        }
                    700:
1.15      pk        701:                        /*
                    702:                         * Wait for items to be returned to this pool.
1.21      thorpej   703:                         *
1.15      pk        704:                         * XXX: we actually want to wait just until
                    705:                         * the page allocator has memory again. Depending
                    706:                         * on this pool's usage, we might get stuck here
                    707:                         * for a long time.
1.20      thorpej   708:                         *
                    709:                         * XXX: maybe we should wake up once a second and
                    710:                         * try again?
1.15      pk        711:                         */
1.1       pk        712:                        pp->pr_flags |= PR_WANTED;
1.25      thorpej   713:                        pr_leave(pp);
1.40      sommerfe  714:                        ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
1.25      thorpej   715:                        pr_enter(pp, file, line);
1.20      thorpej   716:                        goto startover;
1.1       pk        717:                }
1.3       pk        718:
1.15      pk        719:                /* We have more memory; add it to the pool */
1.55      thorpej   720:                pool_prime_page(pp, v, ph);
1.15      pk        721:                pp->pr_npagealloc++;
                    722:
1.20      thorpej   723:                /* Start the allocation process over. */
                    724:                goto startover;
1.3       pk        725:        }
                    726:
1.34      thorpej   727:        if (__predict_false((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)) {
1.25      thorpej   728:                pr_leave(pp);
1.21      thorpej   729:                simple_unlock(&pp->pr_slock);
1.3       pk        730:                panic("pool_get: %s: page empty", pp->pr_wchan);
1.21      thorpej   731:        }
1.20      thorpej   732: #ifdef DIAGNOSTIC
1.34      thorpej   733:        if (__predict_false(pp->pr_nitems == 0)) {
1.25      thorpej   734:                pr_leave(pp);
1.21      thorpej   735:                simple_unlock(&pp->pr_slock);
1.20      thorpej   736:                printf("pool_get: %s: items on itemlist, nitems %u\n",
                    737:                    pp->pr_wchan, pp->pr_nitems);
                    738:                panic("pool_get: nitems inconsistent\n");
                    739:        }
1.56      sommerfe  740:
1.3       pk        741:        pr_log(pp, v, PRLOG_GET, file, line);
                    742:
1.34      thorpej   743:        if (__predict_false(pi->pi_magic != PI_MAGIC)) {
1.25      thorpej   744:                pr_printlog(pp, pi, printf);
1.3       pk        745:                panic("pool_get(%s): free list modified: magic=%x; page %p;"
                    746:                       " item addr %p\n",
                    747:                        pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
                    748:        }
                    749: #endif
                    750:
                    751:        /*
                    752:         * Remove from item list.
                    753:         */
                    754:        TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
1.20      thorpej   755:        pp->pr_nitems--;
                    756:        pp->pr_nout++;
1.6       thorpej   757:        if (ph->ph_nmissing == 0) {
                    758: #ifdef DIAGNOSTIC
1.34      thorpej   759:                if (__predict_false(pp->pr_nidle == 0))
1.6       thorpej   760:                        panic("pool_get: nidle inconsistent");
                    761: #endif
                    762:                pp->pr_nidle--;
                    763:        }
1.3       pk        764:        ph->ph_nmissing++;
                    765:        if (TAILQ_FIRST(&ph->ph_itemlist) == NULL) {
1.21      thorpej   766: #ifdef DIAGNOSTIC
1.34      thorpej   767:                if (__predict_false(ph->ph_nmissing != pp->pr_itemsperpage)) {
1.25      thorpej   768:                        pr_leave(pp);
1.21      thorpej   769:                        simple_unlock(&pp->pr_slock);
                    770:                        panic("pool_get: %s: nmissing inconsistent",
                    771:                            pp->pr_wchan);
                    772:                }
                    773: #endif
1.3       pk        774:                /*
                    775:                 * Find a new non-empty page header, if any.
                    776:                 * Start search from the page head, to increase
                    777:                 * the chance for "high water" pages to be freed.
                    778:                 *
1.21      thorpej   779:                 * Migrate empty pages to the end of the list.  This
                    780:                 * will speed the update of curpage as pages become
                    781:                 * idle.  Empty pages intermingled with idle pages
                    782:                 * is no big deal.  As soon as a page becomes un-empty,
                    783:                 * it will move back to the head of the list.
1.3       pk        784:                 */
                    785:                TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
1.21      thorpej   786:                TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
                    787:                for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
                    788:                     ph = TAILQ_NEXT(ph, ph_pagelist))
1.3       pk        789:                        if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
                    790:                                break;
                    791:
                    792:                pp->pr_curpage = ph;
1.1       pk        793:        }
1.3       pk        794:
                    795:        pp->pr_nget++;
1.20      thorpej   796:
                    797:        /*
                    798:         * If we have a low water mark and we are now below that low
                    799:         * water mark, add more items to the pool.
                    800:         */
1.53      thorpej   801:        if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20      thorpej   802:                /*
                    803:                 * XXX: Should we log a warning?  Should we set up a timeout
                    804:                 * to try again in a second or so?  The latter could break
                    805:                 * a caller's assumptions about interrupt protection, etc.
                    806:                 */
                    807:        }
                    808:
1.25      thorpej   809:        pr_leave(pp);
1.21      thorpej   810:        simple_unlock(&pp->pr_slock);
1.1       pk        811:        return (v);
                    812: }
                    813:
                    814: /*
1.43      thorpej   815:  * Internal version of pool_put().  Pool is already locked/entered.
1.1       pk        816:  */
1.43      thorpej   817: static void
1.56      sommerfe  818: pool_do_put(struct pool *pp, void *v)
1.1       pk        819: {
                    820:        struct pool_item *pi = v;
1.3       pk        821:        struct pool_item_header *ph;
                    822:        caddr_t page;
1.21      thorpej   823:        int s;
1.3       pk        824:
                    825:        page = (caddr_t)((u_long)v & pp->pr_pagemask);
1.1       pk        826:
1.30      thorpej   827: #ifdef DIAGNOSTIC
1.34      thorpej   828:        if (__predict_false(pp->pr_nout == 0)) {
1.30      thorpej   829:                printf("pool %s: putting with none out\n",
                    830:                    pp->pr_wchan);
                    831:                panic("pool_put");
                    832:        }
                    833: #endif
1.3       pk        834:
1.34      thorpej   835:        if (__predict_false((ph = pr_find_pagehead(pp, page)) == NULL)) {
1.25      thorpej   836:                pr_printlog(pp, NULL, printf);
1.3       pk        837:                panic("pool_put: %s: page header missing", pp->pr_wchan);
                    838:        }
1.28      thorpej   839:
                    840: #ifdef LOCKDEBUG
                    841:        /*
                    842:         * Check if we're freeing a locked simple lock.
                    843:         */
                    844:        simple_lock_freecheck((caddr_t)pi, ((caddr_t)pi) + pp->pr_size);
                    845: #endif
1.3       pk        846:
                    847:        /*
                    848:         * Return to item list.
                    849:         */
1.2       pk        850: #ifdef DIAGNOSTIC
1.3       pk        851:        pi->pi_magic = PI_MAGIC;
                    852: #endif
1.32      chs       853: #ifdef DEBUG
                    854:        {
                    855:                int i, *ip = v;
                    856:
                    857:                for (i = 0; i < pp->pr_size / sizeof(int); i++) {
                    858:                        *ip++ = PI_MAGIC;
                    859:                }
                    860:        }
                    861: #endif
                    862:
1.3       pk        863:        TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
                    864:        ph->ph_nmissing--;
                    865:        pp->pr_nput++;
1.20      thorpej   866:        pp->pr_nitems++;
                    867:        pp->pr_nout--;
1.3       pk        868:
                    869:        /* Cancel "pool empty" condition if it exists */
                    870:        if (pp->pr_curpage == NULL)
                    871:                pp->pr_curpage = ph;
                    872:
                    873:        if (pp->pr_flags & PR_WANTED) {
                    874:                pp->pr_flags &= ~PR_WANTED;
1.15      pk        875:                if (ph->ph_nmissing == 0)
                    876:                        pp->pr_nidle++;
1.3       pk        877:                wakeup((caddr_t)pp);
                    878:                return;
                    879:        }
                    880:
                    881:        /*
1.21      thorpej   882:         * If this page is now complete, do one of two things:
                    883:         *
                    884:         *      (1) If we have more pages than the page high water
                    885:         *          mark, free the page back to the system.
                    886:         *
                    887:         *      (2) Move it to the end of the page list, so that
                    888:         *          we minimize our chances of fragmenting the
                    889:         *          pool.  Idle pages migrate to the end (along with
                    890:         *          completely empty pages, so that we find un-empty
                    891:         *          pages more quickly when we update curpage) of the
                    892:         *          list so they can be more easily swept up by
                    893:         *          the pagedaemon when pages are scarce.
1.3       pk        894:         */
                    895:        if (ph->ph_nmissing == 0) {
1.6       thorpej   896:                pp->pr_nidle++;
1.3       pk        897:                if (pp->pr_npages > pp->pr_maxpages) {
                    898:                        pr_rmpage(pp, ph);
                    899:                } else {
                    900:                        TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
                    901:                        TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
                    902:
1.21      thorpej   903:                        /*
                    904:                         * Update the timestamp on the page.  A page must
                    905:                         * be idle for some period of time before it can
                    906:                         * be reclaimed by the pagedaemon.  This minimizes
                    907:                         * ping-pong'ing for memory.
                    908:                         */
                    909:                        s = splclock();
                    910:                        ph->ph_time = mono_time;
                    911:                        splx(s);
                    912:
                    913:                        /*
                    914:                         * Update the current page pointer.  Just look for
                    915:                         * the first page with any free items.
                    916:                         *
                    917:                         * XXX: Maybe we want an option to look for the
                    918:                         * page with the fewest available items, to minimize
                    919:                         * fragmentation?
                    920:                         */
1.3       pk        921:                        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
                    922:                             ph = TAILQ_NEXT(ph, ph_pagelist))
                    923:                                if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
                    924:                                        break;
1.1       pk        925:
1.3       pk        926:                        pp->pr_curpage = ph;
1.1       pk        927:                }
                    928:        }
1.21      thorpej   929:        /*
                    930:         * If the page has just become un-empty, move it to the head of
                    931:         * the list, and make it the current page.  The next allocation
                    932:         * will get the item from this page, instead of further fragmenting
                    933:         * the pool.
                    934:         */
                    935:        else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
                    936:                TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
                    937:                TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
                    938:                pp->pr_curpage = ph;
                    939:        }
1.43      thorpej   940: }
                    941:
                    942: /*
                    943:  * Return resource to the pool; must be called at appropriate spl level
                    944:  */
1.56      sommerfe  945: #ifdef DIAGNOSTIC
1.43      thorpej   946: void
                    947: _pool_put(struct pool *pp, void *v, const char *file, long line)
                    948: {
                    949:
                    950:        simple_lock(&pp->pr_slock);
                    951:        pr_enter(pp, file, line);
                    952:
1.56      sommerfe  953:        pr_log(pp, v, PRLOG_PUT, file, line);
                    954:
                    955:        pool_do_put(pp, v);
1.21      thorpej   956:
1.25      thorpej   957:        pr_leave(pp);
1.21      thorpej   958:        simple_unlock(&pp->pr_slock);
1.1       pk        959: }
1.57      sommerfe  960: #undef pool_put
1.58    ! thorpej   961: #endif /* DIAGNOSTIC */
1.1       pk        962:
1.56      sommerfe  963: void
                    964: pool_put(struct pool *pp, void *v)
                    965: {
                    966:
                    967:        simple_lock(&pp->pr_slock);
                    968:
                    969:        pool_do_put(pp, v);
                    970:
                    971:        simple_unlock(&pp->pr_slock);
                    972: }
1.57      sommerfe  973:
                    974: #ifdef DIAGNOSTIC
                    975: #define                pool_put(h, v)  _pool_put((h), (v), __FILE__, __LINE__)
1.56      sommerfe  976: #endif
                    977:
1.1       pk        978: /*
1.55      thorpej   979:  * Add N items to the pool.
                    980:  */
                    981: int
                    982: pool_prime(struct pool *pp, int n)
                    983: {
                    984:        struct pool_item_header *ph;
                    985:        caddr_t cp;
                    986:        int newpages, error = 0;
                    987:
                    988:        simple_lock(&pp->pr_slock);
                    989:
                    990:        newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
                    991:
                    992:        while (newpages-- > 0) {
                    993:                simple_unlock(&pp->pr_slock);
                    994:                cp = (*pp->pr_alloc)(pp->pr_pagesz, PR_NOWAIT, pp->pr_mtype);
                    995:                if (__predict_true(cp != NULL))
                    996:                        ph = pool_alloc_item_header(pp, cp, PR_NOWAIT);
                    997:                simple_lock(&pp->pr_slock);
                    998:
                    999:                if (__predict_false(cp == NULL || ph == NULL)) {
                   1000:                        error = ENOMEM;
                   1001:                        if (cp != NULL)
                   1002:                                (*pp->pr_free)(cp, pp->pr_pagesz, pp->pr_mtype);
                   1003:                        break;
                   1004:                }
                   1005:
                   1006:                pool_prime_page(pp, cp, ph);
                   1007:                pp->pr_npagealloc++;
                   1008:                pp->pr_minpages++;
                   1009:        }
                   1010:
                   1011:        if (pp->pr_minpages >= pp->pr_maxpages)
                   1012:                pp->pr_maxpages = pp->pr_minpages + 1;  /* XXX */
                   1013:
                   1014:        simple_unlock(&pp->pr_slock);
                   1015:        return (0);
                   1016: }
                   1017:
                   1018: /*
1.3       pk       1019:  * Add a page worth of items to the pool.
1.21      thorpej  1020:  *
                   1021:  * Note, we must be called with the pool descriptor LOCKED.
1.3       pk       1022:  */
1.55      thorpej  1023: static void
                   1024: pool_prime_page(struct pool *pp, caddr_t storage, struct pool_item_header *ph)
1.3       pk       1025: {
                   1026:        struct pool_item *pi;
                   1027:        caddr_t cp = storage;
                   1028:        unsigned int align = pp->pr_align;
                   1029:        unsigned int ioff = pp->pr_itemoffset;
1.55      thorpej  1030:        int n;
1.36      pk       1031:
                   1032:        if (((u_long)cp & (pp->pr_pagesz - 1)) != 0)
                   1033:                panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1.3       pk       1034:
1.55      thorpej  1035:        if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1.3       pk       1036:                LIST_INSERT_HEAD(&pp->pr_hashtab[PR_HASH_INDEX(pp, cp)],
1.55      thorpej  1037:                    ph, ph_hashlist);
1.3       pk       1038:
                   1039:        /*
                   1040:         * Insert page header.
                   1041:         */
                   1042:        TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
                   1043:        TAILQ_INIT(&ph->ph_itemlist);
                   1044:        ph->ph_page = storage;
                   1045:        ph->ph_nmissing = 0;
1.21      thorpej  1046:        memset(&ph->ph_time, 0, sizeof(ph->ph_time));
1.3       pk       1047:
1.6       thorpej  1048:        pp->pr_nidle++;
                   1049:
1.3       pk       1050:        /*
                   1051:         * Color this page.
                   1052:         */
                   1053:        cp = (caddr_t)(cp + pp->pr_curcolor);
                   1054:        if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
                   1055:                pp->pr_curcolor = 0;
                   1056:
                   1057:        /*
                   1058:         * Adjust storage to apply aligment to `pr_itemoffset' in each item.
                   1059:         */
                   1060:        if (ioff != 0)
                   1061:                cp = (caddr_t)(cp + (align - ioff));
                   1062:
                   1063:        /*
                   1064:         * Insert remaining chunks on the bucket list.
                   1065:         */
                   1066:        n = pp->pr_itemsperpage;
1.20      thorpej  1067:        pp->pr_nitems += n;
1.3       pk       1068:
                   1069:        while (n--) {
                   1070:                pi = (struct pool_item *)cp;
                   1071:
                   1072:                /* Insert on page list */
                   1073:                TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
                   1074: #ifdef DIAGNOSTIC
                   1075:                pi->pi_magic = PI_MAGIC;
                   1076: #endif
                   1077:                cp = (caddr_t)(cp + pp->pr_size);
                   1078:        }
                   1079:
                   1080:        /*
                   1081:         * If the pool was depleted, point at the new page.
                   1082:         */
                   1083:        if (pp->pr_curpage == NULL)
                   1084:                pp->pr_curpage = ph;
                   1085:
                   1086:        if (++pp->pr_npages > pp->pr_hiwat)
                   1087:                pp->pr_hiwat = pp->pr_npages;
                   1088: }
                   1089:
1.20      thorpej  1090: /*
1.52      thorpej  1091:  * Used by pool_get() when nitems drops below the low water mark.  This
                   1092:  * is used to catch up nitmes with the low water mark.
1.20      thorpej  1093:  *
1.21      thorpej  1094:  * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20      thorpej  1095:  *
                   1096:  * Note 2, this doesn't work with static pools.
                   1097:  *
                   1098:  * Note 3, we must be called with the pool already locked, and we return
                   1099:  * with it locked.
                   1100:  */
                   1101: static int
1.42      thorpej  1102: pool_catchup(struct pool *pp)
1.20      thorpej  1103: {
1.55      thorpej  1104:        struct pool_item_header *ph;
1.20      thorpej  1105:        caddr_t cp;
                   1106:        int error = 0;
                   1107:
                   1108:        if (pp->pr_roflags & PR_STATIC) {
                   1109:                /*
                   1110:                 * We dropped below the low water mark, and this is not a
                   1111:                 * good thing.  Log a warning.
1.21      thorpej  1112:                 *
                   1113:                 * XXX: rate-limit this?
1.20      thorpej  1114:                 */
                   1115:                printf("WARNING: static pool `%s' dropped below low water "
                   1116:                    "mark\n", pp->pr_wchan);
                   1117:                return (0);
                   1118:        }
                   1119:
1.54      thorpej  1120:        while (POOL_NEEDS_CATCHUP(pp)) {
1.20      thorpej  1121:                /*
1.21      thorpej  1122:                 * Call the page back-end allocator for more memory.
                   1123:                 *
                   1124:                 * XXX: We never wait, so should we bother unlocking
                   1125:                 * the pool descriptor?
1.20      thorpej  1126:                 */
1.21      thorpej  1127:                simple_unlock(&pp->pr_slock);
1.55      thorpej  1128:                cp = (*pp->pr_alloc)(pp->pr_pagesz, PR_NOWAIT, pp->pr_mtype);
                   1129:                if (__predict_true(cp != NULL))
                   1130:                        ph = pool_alloc_item_header(pp, cp, PR_NOWAIT);
1.21      thorpej  1131:                simple_lock(&pp->pr_slock);
1.55      thorpej  1132:                if (__predict_false(cp == NULL || ph == NULL)) {
                   1133:                        if (cp != NULL)
                   1134:                                (*pp->pr_free)(cp, pp->pr_pagesz, pp->pr_mtype);
1.20      thorpej  1135:                        error = ENOMEM;
                   1136:                        break;
                   1137:                }
1.55      thorpej  1138:                pool_prime_page(pp, cp, ph);
1.26      thorpej  1139:                pp->pr_npagealloc++;
1.20      thorpej  1140:        }
                   1141:
                   1142:        return (error);
                   1143: }
                   1144:
1.3       pk       1145: void
1.42      thorpej  1146: pool_setlowat(struct pool *pp, int n)
1.3       pk       1147: {
1.20      thorpej  1148:        int error;
1.15      pk       1149:
1.21      thorpej  1150:        simple_lock(&pp->pr_slock);
                   1151:
1.3       pk       1152:        pp->pr_minitems = n;
1.15      pk       1153:        pp->pr_minpages = (n == 0)
                   1154:                ? 0
1.18      thorpej  1155:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20      thorpej  1156:
                   1157:        /* Make sure we're caught up with the newly-set low water mark. */
1.53      thorpej  1158:        if (POOL_NEEDS_CATCHUP(pp) && (error = pool_catchup(pp) != 0)) {
1.20      thorpej  1159:                /*
                   1160:                 * XXX: Should we log a warning?  Should we set up a timeout
                   1161:                 * to try again in a second or so?  The latter could break
                   1162:                 * a caller's assumptions about interrupt protection, etc.
                   1163:                 */
                   1164:        }
1.21      thorpej  1165:
                   1166:        simple_unlock(&pp->pr_slock);
1.3       pk       1167: }
                   1168:
                   1169: void
1.42      thorpej  1170: pool_sethiwat(struct pool *pp, int n)
1.3       pk       1171: {
1.15      pk       1172:
1.21      thorpej  1173:        simple_lock(&pp->pr_slock);
                   1174:
1.15      pk       1175:        pp->pr_maxpages = (n == 0)
                   1176:                ? 0
1.18      thorpej  1177:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21      thorpej  1178:
                   1179:        simple_unlock(&pp->pr_slock);
1.3       pk       1180: }
                   1181:
1.20      thorpej  1182: void
1.42      thorpej  1183: pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1.20      thorpej  1184: {
                   1185:
1.21      thorpej  1186:        simple_lock(&pp->pr_slock);
1.20      thorpej  1187:
                   1188:        pp->pr_hardlimit = n;
                   1189:        pp->pr_hardlimit_warning = warnmess;
1.31      thorpej  1190:        pp->pr_hardlimit_ratecap.tv_sec = ratecap;
                   1191:        pp->pr_hardlimit_warning_last.tv_sec = 0;
                   1192:        pp->pr_hardlimit_warning_last.tv_usec = 0;
1.20      thorpej  1193:
                   1194:        /*
1.21      thorpej  1195:         * In-line version of pool_sethiwat(), because we don't want to
                   1196:         * release the lock.
1.20      thorpej  1197:         */
                   1198:        pp->pr_maxpages = (n == 0)
                   1199:                ? 0
                   1200:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21      thorpej  1201:
                   1202:        simple_unlock(&pp->pr_slock);
1.20      thorpej  1203: }
1.3       pk       1204:
                   1205: /*
                   1206:  * Default page allocator.
                   1207:  */
                   1208: static void *
1.42      thorpej  1209: pool_page_alloc(unsigned long sz, int flags, int mtype)
1.3       pk       1210: {
1.11      thorpej  1211:        boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1.3       pk       1212:
1.11      thorpej  1213:        return ((void *)uvm_km_alloc_poolpage(waitok));
1.3       pk       1214: }
                   1215:
                   1216: static void
1.42      thorpej  1217: pool_page_free(void *v, unsigned long sz, int mtype)
1.3       pk       1218: {
                   1219:
1.10      eeh      1220:        uvm_km_free_poolpage((vaddr_t)v);
1.3       pk       1221: }
1.12      thorpej  1222:
                   1223: /*
                   1224:  * Alternate pool page allocator for pools that know they will
                   1225:  * never be accessed in interrupt context.
                   1226:  */
                   1227: void *
1.42      thorpej  1228: pool_page_alloc_nointr(unsigned long sz, int flags, int mtype)
1.12      thorpej  1229: {
                   1230:        boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
                   1231:
                   1232:        return ((void *)uvm_km_alloc_poolpage1(kernel_map, uvm.kernel_object,
                   1233:            waitok));
                   1234: }
                   1235:
                   1236: void
1.42      thorpej  1237: pool_page_free_nointr(void *v, unsigned long sz, int mtype)
1.12      thorpej  1238: {
                   1239:
                   1240:        uvm_km_free_poolpage1(kernel_map, (vaddr_t)v);
                   1241: }
                   1242:
1.3       pk       1243:
                   1244: /*
                   1245:  * Release all complete pages that have not been used recently.
                   1246:  */
                   1247: void
1.56      sommerfe 1248: #ifdef DIAGNOSTIC
1.42      thorpej  1249: _pool_reclaim(struct pool *pp, const char *file, long line)
1.56      sommerfe 1250: #else
                   1251: pool_reclaim(struct pool *pp)
                   1252: #endif
1.3       pk       1253: {
                   1254:        struct pool_item_header *ph, *phnext;
1.43      thorpej  1255:        struct pool_cache *pc;
1.21      thorpej  1256:        struct timeval curtime;
                   1257:        int s;
1.3       pk       1258:
1.20      thorpej  1259:        if (pp->pr_roflags & PR_STATIC)
1.3       pk       1260:                return;
                   1261:
1.21      thorpej  1262:        if (simple_lock_try(&pp->pr_slock) == 0)
1.3       pk       1263:                return;
1.25      thorpej  1264:        pr_enter(pp, file, line);
1.3       pk       1265:
1.43      thorpej  1266:        /*
                   1267:         * Reclaim items from the pool's caches.
                   1268:         */
                   1269:        for (pc = TAILQ_FIRST(&pp->pr_cachelist); pc != NULL;
                   1270:             pc = TAILQ_NEXT(pc, pc_poollist))
                   1271:                pool_cache_reclaim(pc);
                   1272:
1.21      thorpej  1273:        s = splclock();
                   1274:        curtime = mono_time;
                   1275:        splx(s);
                   1276:
1.3       pk       1277:        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL; ph = phnext) {
                   1278:                phnext = TAILQ_NEXT(ph, ph_pagelist);
                   1279:
                   1280:                /* Check our minimum page claim */
                   1281:                if (pp->pr_npages <= pp->pr_minpages)
                   1282:                        break;
                   1283:
                   1284:                if (ph->ph_nmissing == 0) {
                   1285:                        struct timeval diff;
                   1286:                        timersub(&curtime, &ph->ph_time, &diff);
                   1287:                        if (diff.tv_sec < pool_inactive_time)
                   1288:                                continue;
1.21      thorpej  1289:
                   1290:                        /*
                   1291:                         * If freeing this page would put us below
                   1292:                         * the low water mark, stop now.
                   1293:                         */
                   1294:                        if ((pp->pr_nitems - pp->pr_itemsperpage) <
                   1295:                            pp->pr_minitems)
                   1296:                                break;
                   1297:
1.3       pk       1298:                        pr_rmpage(pp, ph);
                   1299:                }
                   1300:        }
                   1301:
1.25      thorpej  1302:        pr_leave(pp);
1.21      thorpej  1303:        simple_unlock(&pp->pr_slock);
1.3       pk       1304: }
                   1305:
                   1306:
                   1307: /*
                   1308:  * Drain pools, one at a time.
1.21      thorpej  1309:  *
                   1310:  * Note, we must never be called from an interrupt context.
1.3       pk       1311:  */
                   1312: void
1.42      thorpej  1313: pool_drain(void *arg)
1.3       pk       1314: {
                   1315:        struct pool *pp;
1.23      thorpej  1316:        int s;
1.3       pk       1317:
1.49      thorpej  1318:        s = splvm();
1.23      thorpej  1319:        simple_lock(&pool_head_slock);
                   1320:
                   1321:        if (drainpp == NULL && (drainpp = TAILQ_FIRST(&pool_head)) == NULL)
                   1322:                goto out;
1.3       pk       1323:
                   1324:        pp = drainpp;
                   1325:        drainpp = TAILQ_NEXT(pp, pr_poollist);
                   1326:
                   1327:        pool_reclaim(pp);
1.23      thorpej  1328:
                   1329:  out:
                   1330:        simple_unlock(&pool_head_slock);
1.3       pk       1331:        splx(s);
                   1332: }
                   1333:
                   1334:
                   1335: /*
                   1336:  * Diagnostic helpers.
                   1337:  */
                   1338: void
1.42      thorpej  1339: pool_print(struct pool *pp, const char *modif)
1.21      thorpej  1340: {
                   1341:        int s;
                   1342:
1.49      thorpej  1343:        s = splvm();
1.25      thorpej  1344:        if (simple_lock_try(&pp->pr_slock) == 0) {
                   1345:                printf("pool %s is locked; try again later\n",
                   1346:                    pp->pr_wchan);
                   1347:                splx(s);
                   1348:                return;
                   1349:        }
                   1350:        pool_print1(pp, modif, printf);
1.21      thorpej  1351:        simple_unlock(&pp->pr_slock);
                   1352:        splx(s);
                   1353: }
                   1354:
1.25      thorpej  1355: void
1.42      thorpej  1356: pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.25      thorpej  1357: {
                   1358:        int didlock = 0;
                   1359:
                   1360:        if (pp == NULL) {
                   1361:                (*pr)("Must specify a pool to print.\n");
                   1362:                return;
                   1363:        }
                   1364:
                   1365:        /*
                   1366:         * Called from DDB; interrupts should be blocked, and all
                   1367:         * other processors should be paused.  We can skip locking
                   1368:         * the pool in this case.
                   1369:         *
                   1370:         * We do a simple_lock_try() just to print the lock
                   1371:         * status, however.
                   1372:         */
                   1373:
                   1374:        if (simple_lock_try(&pp->pr_slock) == 0)
                   1375:                (*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
                   1376:        else
                   1377:                didlock = 1;
                   1378:
                   1379:        pool_print1(pp, modif, pr);
                   1380:
                   1381:        if (didlock)
                   1382:                simple_unlock(&pp->pr_slock);
                   1383: }
                   1384:
1.21      thorpej  1385: static void
1.42      thorpej  1386: pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.3       pk       1387: {
1.25      thorpej  1388:        struct pool_item_header *ph;
1.44      thorpej  1389:        struct pool_cache *pc;
                   1390:        struct pool_cache_group *pcg;
1.25      thorpej  1391: #ifdef DIAGNOSTIC
                   1392:        struct pool_item *pi;
                   1393: #endif
1.44      thorpej  1394:        int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1.25      thorpej  1395:        char c;
                   1396:
                   1397:        while ((c = *modif++) != '\0') {
                   1398:                if (c == 'l')
                   1399:                        print_log = 1;
                   1400:                if (c == 'p')
                   1401:                        print_pagelist = 1;
1.44      thorpej  1402:                if (c == 'c')
                   1403:                        print_cache = 1;
1.25      thorpej  1404:                modif++;
                   1405:        }
                   1406:
                   1407:        (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
                   1408:            pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
                   1409:            pp->pr_roflags);
                   1410:        (*pr)("\tpagesz %u, mtype %d\n", pp->pr_pagesz, pp->pr_mtype);
                   1411:        (*pr)("\talloc %p, release %p\n", pp->pr_alloc, pp->pr_free);
                   1412:        (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
                   1413:            pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
                   1414:        (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
                   1415:            pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
                   1416:
                   1417:        (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
                   1418:            pp->pr_nget, pp->pr_nfail, pp->pr_nput);
                   1419:        (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
                   1420:            pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
                   1421:
                   1422:        if (print_pagelist == 0)
                   1423:                goto skip_pagelist;
                   1424:
                   1425:        if ((ph = TAILQ_FIRST(&pp->pr_pagelist)) != NULL)
                   1426:                (*pr)("\n\tpage list:\n");
                   1427:        for (; ph != NULL; ph = TAILQ_NEXT(ph, ph_pagelist)) {
                   1428:                (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
                   1429:                    ph->ph_page, ph->ph_nmissing,
                   1430:                    (u_long)ph->ph_time.tv_sec,
                   1431:                    (u_long)ph->ph_time.tv_usec);
                   1432: #ifdef DIAGNOSTIC
                   1433:                for (pi = TAILQ_FIRST(&ph->ph_itemlist); pi != NULL;
                   1434:                     pi = TAILQ_NEXT(pi, pi_list)) {
                   1435:                        if (pi->pi_magic != PI_MAGIC) {
                   1436:                                (*pr)("\t\t\titem %p, magic 0x%x\n",
                   1437:                                    pi, pi->pi_magic);
                   1438:                        }
                   1439:                }
                   1440: #endif
                   1441:        }
                   1442:        if (pp->pr_curpage == NULL)
                   1443:                (*pr)("\tno current page\n");
                   1444:        else
                   1445:                (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
                   1446:
                   1447:  skip_pagelist:
                   1448:
                   1449:        if (print_log == 0)
                   1450:                goto skip_log;
                   1451:
                   1452:        (*pr)("\n");
                   1453:        if ((pp->pr_roflags & PR_LOGGING) == 0)
                   1454:                (*pr)("\tno log\n");
                   1455:        else
                   1456:                pr_printlog(pp, NULL, pr);
1.3       pk       1457:
1.25      thorpej  1458:  skip_log:
1.44      thorpej  1459:
                   1460:        if (print_cache == 0)
                   1461:                goto skip_cache;
                   1462:
                   1463:        for (pc = TAILQ_FIRST(&pp->pr_cachelist); pc != NULL;
                   1464:             pc = TAILQ_NEXT(pc, pc_poollist)) {
                   1465:                (*pr)("\tcache %p: allocfrom %p freeto %p\n", pc,
                   1466:                    pc->pc_allocfrom, pc->pc_freeto);
1.48      thorpej  1467:                (*pr)("\t    hits %lu misses %lu ngroups %lu nitems %lu\n",
                   1468:                    pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems);
1.44      thorpej  1469:                for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL;
                   1470:                     pcg = TAILQ_NEXT(pcg, pcg_list)) {
                   1471:                        (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail);
                   1472:                        for (i = 0; i < PCG_NOBJECTS; i++)
                   1473:                                (*pr)("\t\t\t%p\n", pcg->pcg_objects[i]);
                   1474:                }
                   1475:        }
                   1476:
                   1477:  skip_cache:
1.3       pk       1478:
1.25      thorpej  1479:        pr_enter_check(pp, pr);
1.3       pk       1480: }
                   1481:
                   1482: int
1.42      thorpej  1483: pool_chk(struct pool *pp, const char *label)
1.3       pk       1484: {
                   1485:        struct pool_item_header *ph;
                   1486:        int r = 0;
                   1487:
1.21      thorpej  1488:        simple_lock(&pp->pr_slock);
1.3       pk       1489:
                   1490:        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
                   1491:             ph = TAILQ_NEXT(ph, ph_pagelist)) {
                   1492:
                   1493:                struct pool_item *pi;
                   1494:                int n;
                   1495:                caddr_t page;
                   1496:
                   1497:                page = (caddr_t)((u_long)ph & pp->pr_pagemask);
1.20      thorpej  1498:                if (page != ph->ph_page &&
                   1499:                    (pp->pr_roflags & PR_PHINPAGE) != 0) {
1.3       pk       1500:                        if (label != NULL)
                   1501:                                printf("%s: ", label);
1.16      briggs   1502:                        printf("pool(%p:%s): page inconsistency: page %p;"
                   1503:                               " at page head addr %p (p %p)\n", pp,
1.3       pk       1504:                                pp->pr_wchan, ph->ph_page,
                   1505:                                ph, page);
                   1506:                        r++;
                   1507:                        goto out;
                   1508:                }
                   1509:
                   1510:                for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
                   1511:                     pi != NULL;
                   1512:                     pi = TAILQ_NEXT(pi,pi_list), n++) {
                   1513:
                   1514: #ifdef DIAGNOSTIC
                   1515:                        if (pi->pi_magic != PI_MAGIC) {
                   1516:                                if (label != NULL)
                   1517:                                        printf("%s: ", label);
                   1518:                                printf("pool(%s): free list modified: magic=%x;"
                   1519:                                       " page %p; item ordinal %d;"
                   1520:                                       " addr %p (p %p)\n",
                   1521:                                        pp->pr_wchan, pi->pi_magic, ph->ph_page,
                   1522:                                        n, pi, page);
                   1523:                                panic("pool");
                   1524:                        }
                   1525: #endif
                   1526:                        page = (caddr_t)((u_long)pi & pp->pr_pagemask);
                   1527:                        if (page == ph->ph_page)
                   1528:                                continue;
                   1529:
                   1530:                        if (label != NULL)
                   1531:                                printf("%s: ", label);
1.16      briggs   1532:                        printf("pool(%p:%s): page inconsistency: page %p;"
                   1533:                               " item ordinal %d; addr %p (p %p)\n", pp,
1.3       pk       1534:                                pp->pr_wchan, ph->ph_page,
                   1535:                                n, pi, page);
                   1536:                        r++;
                   1537:                        goto out;
                   1538:                }
                   1539:        }
                   1540: out:
1.21      thorpej  1541:        simple_unlock(&pp->pr_slock);
1.3       pk       1542:        return (r);
1.43      thorpej  1543: }
                   1544:
                   1545: /*
                   1546:  * pool_cache_init:
                   1547:  *
                   1548:  *     Initialize a pool cache.
                   1549:  *
                   1550:  *     NOTE: If the pool must be protected from interrupts, we expect
                   1551:  *     to be called at the appropriate interrupt priority level.
                   1552:  */
                   1553: void
                   1554: pool_cache_init(struct pool_cache *pc, struct pool *pp,
                   1555:     int (*ctor)(void *, void *, int),
                   1556:     void (*dtor)(void *, void *),
                   1557:     void *arg)
                   1558: {
                   1559:
                   1560:        TAILQ_INIT(&pc->pc_grouplist);
                   1561:        simple_lock_init(&pc->pc_slock);
                   1562:
                   1563:        pc->pc_allocfrom = NULL;
                   1564:        pc->pc_freeto = NULL;
                   1565:        pc->pc_pool = pp;
                   1566:
                   1567:        pc->pc_ctor = ctor;
                   1568:        pc->pc_dtor = dtor;
                   1569:        pc->pc_arg  = arg;
                   1570:
1.48      thorpej  1571:        pc->pc_hits   = 0;
                   1572:        pc->pc_misses = 0;
                   1573:
                   1574:        pc->pc_ngroups = 0;
                   1575:
                   1576:        pc->pc_nitems = 0;
                   1577:
1.43      thorpej  1578:        simple_lock(&pp->pr_slock);
                   1579:        TAILQ_INSERT_TAIL(&pp->pr_cachelist, pc, pc_poollist);
                   1580:        simple_unlock(&pp->pr_slock);
                   1581: }
                   1582:
                   1583: /*
                   1584:  * pool_cache_destroy:
                   1585:  *
                   1586:  *     Destroy a pool cache.
                   1587:  */
                   1588: void
                   1589: pool_cache_destroy(struct pool_cache *pc)
                   1590: {
                   1591:        struct pool *pp = pc->pc_pool;
                   1592:
                   1593:        /* First, invalidate the entire cache. */
                   1594:        pool_cache_invalidate(pc);
                   1595:
                   1596:        /* ...and remove it from the pool's cache list. */
                   1597:        simple_lock(&pp->pr_slock);
                   1598:        TAILQ_REMOVE(&pp->pr_cachelist, pc, pc_poollist);
                   1599:        simple_unlock(&pp->pr_slock);
                   1600: }
                   1601:
                   1602: static __inline void *
                   1603: pcg_get(struct pool_cache_group *pcg)
                   1604: {
                   1605:        void *object;
                   1606:        u_int idx;
                   1607:
                   1608:        KASSERT(pcg->pcg_avail <= PCG_NOBJECTS);
1.45      thorpej  1609:        KASSERT(pcg->pcg_avail != 0);
1.43      thorpej  1610:        idx = --pcg->pcg_avail;
                   1611:
                   1612:        KASSERT(pcg->pcg_objects[idx] != NULL);
                   1613:        object = pcg->pcg_objects[idx];
                   1614:        pcg->pcg_objects[idx] = NULL;
                   1615:
                   1616:        return (object);
                   1617: }
                   1618:
                   1619: static __inline void
                   1620: pcg_put(struct pool_cache_group *pcg, void *object)
                   1621: {
                   1622:        u_int idx;
                   1623:
                   1624:        KASSERT(pcg->pcg_avail < PCG_NOBJECTS);
                   1625:        idx = pcg->pcg_avail++;
                   1626:
                   1627:        KASSERT(pcg->pcg_objects[idx] == NULL);
                   1628:        pcg->pcg_objects[idx] = object;
                   1629: }
                   1630:
                   1631: /*
                   1632:  * pool_cache_get:
                   1633:  *
                   1634:  *     Get an object from a pool cache.
                   1635:  */
                   1636: void *
                   1637: pool_cache_get(struct pool_cache *pc, int flags)
                   1638: {
                   1639:        struct pool_cache_group *pcg;
                   1640:        void *object;
1.58    ! thorpej  1641:
        !          1642: #ifdef LOCKDEBUG
        !          1643:        if (flags & PR_WAITOK)
        !          1644:                simple_lock_only_held(NULL, "pool_cache_get(PR_WAITOK)");
        !          1645: #endif
1.43      thorpej  1646:
                   1647:        simple_lock(&pc->pc_slock);
                   1648:
                   1649:        if ((pcg = pc->pc_allocfrom) == NULL) {
                   1650:                for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL;
                   1651:                     pcg = TAILQ_NEXT(pcg, pcg_list)) {
                   1652:                        if (pcg->pcg_avail != 0) {
                   1653:                                pc->pc_allocfrom = pcg;
                   1654:                                goto have_group;
                   1655:                        }
                   1656:                }
                   1657:
                   1658:                /*
                   1659:                 * No groups with any available objects.  Allocate
                   1660:                 * a new object, construct it, and return it to
                   1661:                 * the caller.  We will allocate a group, if necessary,
                   1662:                 * when the object is freed back to the cache.
                   1663:                 */
1.48      thorpej  1664:                pc->pc_misses++;
1.43      thorpej  1665:                simple_unlock(&pc->pc_slock);
                   1666:                object = pool_get(pc->pc_pool, flags);
                   1667:                if (object != NULL && pc->pc_ctor != NULL) {
                   1668:                        if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) {
                   1669:                                pool_put(pc->pc_pool, object);
                   1670:                                return (NULL);
                   1671:                        }
                   1672:                }
                   1673:                return (object);
                   1674:        }
                   1675:
                   1676:  have_group:
1.48      thorpej  1677:        pc->pc_hits++;
                   1678:        pc->pc_nitems--;
1.43      thorpej  1679:        object = pcg_get(pcg);
                   1680:
                   1681:        if (pcg->pcg_avail == 0)
                   1682:                pc->pc_allocfrom = NULL;
1.45      thorpej  1683:
1.43      thorpej  1684:        simple_unlock(&pc->pc_slock);
                   1685:
                   1686:        return (object);
                   1687: }
                   1688:
                   1689: /*
                   1690:  * pool_cache_put:
                   1691:  *
                   1692:  *     Put an object back to the pool cache.
                   1693:  */
                   1694: void
                   1695: pool_cache_put(struct pool_cache *pc, void *object)
                   1696: {
                   1697:        struct pool_cache_group *pcg;
                   1698:
                   1699:        simple_lock(&pc->pc_slock);
                   1700:
                   1701:        if ((pcg = pc->pc_freeto) == NULL) {
                   1702:                for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL;
                   1703:                     pcg = TAILQ_NEXT(pcg, pcg_list)) {
                   1704:                        if (pcg->pcg_avail != PCG_NOBJECTS) {
                   1705:                                pc->pc_freeto = pcg;
                   1706:                                goto have_group;
                   1707:                        }
                   1708:                }
                   1709:
                   1710:                /*
                   1711:                 * No empty groups to free the object to.  Attempt to
1.47      thorpej  1712:                 * allocate one.
1.43      thorpej  1713:                 */
1.47      thorpej  1714:                simple_unlock(&pc->pc_slock);
1.43      thorpej  1715:                pcg = pool_get(&pcgpool, PR_NOWAIT);
                   1716:                if (pcg != NULL) {
                   1717:                        memset(pcg, 0, sizeof(*pcg));
1.47      thorpej  1718:                        simple_lock(&pc->pc_slock);
1.48      thorpej  1719:                        pc->pc_ngroups++;
1.43      thorpej  1720:                        TAILQ_INSERT_TAIL(&pc->pc_grouplist, pcg, pcg_list);
1.47      thorpej  1721:                        if (pc->pc_freeto == NULL)
                   1722:                                pc->pc_freeto = pcg;
1.43      thorpej  1723:                        goto have_group;
                   1724:                }
                   1725:
                   1726:                /*
                   1727:                 * Unable to allocate a cache group; destruct the object
                   1728:                 * and free it back to the pool.
                   1729:                 */
1.51      thorpej  1730:                pool_cache_destruct_object(pc, object);
1.43      thorpej  1731:                return;
                   1732:        }
                   1733:
                   1734:  have_group:
1.48      thorpej  1735:        pc->pc_nitems++;
1.43      thorpej  1736:        pcg_put(pcg, object);
                   1737:
                   1738:        if (pcg->pcg_avail == PCG_NOBJECTS)
                   1739:                pc->pc_freeto = NULL;
                   1740:
                   1741:        simple_unlock(&pc->pc_slock);
1.51      thorpej  1742: }
                   1743:
                   1744: /*
                   1745:  * pool_cache_destruct_object:
                   1746:  *
                   1747:  *     Force destruction of an object and its release back into
                   1748:  *     the pool.
                   1749:  */
                   1750: void
                   1751: pool_cache_destruct_object(struct pool_cache *pc, void *object)
                   1752: {
                   1753:
                   1754:        if (pc->pc_dtor != NULL)
                   1755:                (*pc->pc_dtor)(pc->pc_arg, object);
                   1756:        pool_put(pc->pc_pool, object);
1.43      thorpej  1757: }
                   1758:
                   1759: /*
                   1760:  * pool_cache_do_invalidate:
                   1761:  *
                   1762:  *     This internal function implements pool_cache_invalidate() and
                   1763:  *     pool_cache_reclaim().
                   1764:  */
                   1765: static void
                   1766: pool_cache_do_invalidate(struct pool_cache *pc, int free_groups,
1.56      sommerfe 1767:     void (*putit)(struct pool *, void *))
1.43      thorpej  1768: {
                   1769:        struct pool_cache_group *pcg, *npcg;
                   1770:        void *object;
                   1771:
                   1772:        for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL;
                   1773:             pcg = npcg) {
                   1774:                npcg = TAILQ_NEXT(pcg, pcg_list);
                   1775:                while (pcg->pcg_avail != 0) {
1.48      thorpej  1776:                        pc->pc_nitems--;
1.43      thorpej  1777:                        object = pcg_get(pcg);
1.45      thorpej  1778:                        if (pcg->pcg_avail == 0 && pc->pc_allocfrom == pcg)
                   1779:                                pc->pc_allocfrom = NULL;
1.43      thorpej  1780:                        if (pc->pc_dtor != NULL)
                   1781:                                (*pc->pc_dtor)(pc->pc_arg, object);
1.56      sommerfe 1782:                        (*putit)(pc->pc_pool, object);
1.43      thorpej  1783:                }
                   1784:                if (free_groups) {
1.48      thorpej  1785:                        pc->pc_ngroups--;
1.43      thorpej  1786:                        TAILQ_REMOVE(&pc->pc_grouplist, pcg, pcg_list);
1.46      thorpej  1787:                        if (pc->pc_freeto == pcg)
                   1788:                                pc->pc_freeto = NULL;
1.43      thorpej  1789:                        pool_put(&pcgpool, pcg);
                   1790:                }
                   1791:        }
                   1792: }
                   1793:
                   1794: /*
                   1795:  * pool_cache_invalidate:
                   1796:  *
                   1797:  *     Invalidate a pool cache (destruct and release all of the
                   1798:  *     cached objects).
                   1799:  */
                   1800: void
                   1801: pool_cache_invalidate(struct pool_cache *pc)
                   1802: {
                   1803:
                   1804:        simple_lock(&pc->pc_slock);
1.56      sommerfe 1805:        pool_cache_do_invalidate(pc, 0, pool_put);
1.43      thorpej  1806:        simple_unlock(&pc->pc_slock);
                   1807: }
                   1808:
                   1809: /*
                   1810:  * pool_cache_reclaim:
                   1811:  *
                   1812:  *     Reclaim a pool cache for pool_reclaim().
                   1813:  */
                   1814: static void
                   1815: pool_cache_reclaim(struct pool_cache *pc)
                   1816: {
                   1817:
1.47      thorpej  1818:        simple_lock(&pc->pc_slock);
1.43      thorpej  1819:        pool_cache_do_invalidate(pc, 1, pool_do_put);
                   1820:        simple_unlock(&pc->pc_slock);
1.3       pk       1821: }

CVSweb <webmaster@jp.NetBSD.org>