[BACK]Return to subr_pool.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/subr_pool.c, Revision 1.128.2.3

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

CVSweb <webmaster@jp.NetBSD.org>