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

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

CVSweb <webmaster@jp.NetBSD.org>