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

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

CVSweb <webmaster@jp.NetBSD.org>