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

1.128.2.4! ad          1: /*     $NetBSD: subr_pool.c,v 1.128.2.3 2007/03/21 20:10:22 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.4! ad         41: __KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.128.2.3 2007/03/21 20:10:22 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:
1.3       pk        774:        /*
1.43      thorpej   775:         * Initialize private page header pool and cache magazine pool if we
                    776:         * haven't done so yet.
1.23      thorpej   777:         * XXX LOCKING.
1.3       pk        778:         */
1.97      yamt      779:        if (phpool[0].pr_size == 0) {
                    780:                int idx;
                    781:                for (idx = 0; idx < PHPOOL_MAX; idx++) {
                    782:                        static char phpool_names[PHPOOL_MAX][6+1+6+1];
                    783:                        int nelem;
                    784:                        size_t sz;
                    785:
                    786:                        nelem = PHPOOL_FREELIST_NELEM(idx);
                    787:                        snprintf(phpool_names[idx], sizeof(phpool_names[idx]),
                    788:                            "phpool-%d", nelem);
                    789:                        sz = sizeof(struct pool_item_header);
                    790:                        if (nelem) {
                    791:                                sz = PR_FREELIST_ALIGN(sz)
1.99      yamt      792:                                    + nelem * sizeof(pool_item_freelist_t);
1.97      yamt      793:                        }
                    794:                        pool_init(&phpool[idx], sz, 0, 0, 0,
1.128.2.1  ad        795:                            phpool_names[idx], &pool_allocator_meta, IPL_VM);
1.97      yamt      796:                }
1.62      bjh21     797: #ifdef POOL_SUBPAGE
                    798:                pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0,
1.128.2.1  ad        799:                    PR_RECURSIVE, "psppool", &pool_allocator_meta, IPL_VM);
1.62      bjh21     800: #endif
1.43      thorpej   801:                pool_init(&pcgpool, sizeof(struct pool_cache_group), 0, 0,
1.128.2.1  ad        802:                    0, "pcgpool", &pool_allocator_meta, IPL_VM);
1.1       pk        803:        }
                    804:
1.128.2.2  ad        805:        if (__predict_true(!cold)) {
                    806:                /* Insert into the list of all pools. */
                    807:                mutex_enter(&pool_head_lock);
                    808:                LIST_INSERT_HEAD(&pool_head, pp, pr_poollist);
                    809:                mutex_exit(&pool_head_lock);
                    810:
                    811:                /* Insert this into the list of pools using this allocator. */
                    812:                mutex_enter(&palloc->pa_lock);
                    813:                TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
                    814:                mutex_exit(&palloc->pa_lock);
                    815:        } else {
                    816:                LIST_INSERT_HEAD(&pool_head, pp, pr_poollist);
                    817:                TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
                    818:        }
                    819:
1.117     yamt      820:        pool_reclaim_register(pp);
1.1       pk        821: }
                    822:
                    823: /*
                    824:  * De-commision a pool resource.
                    825:  */
                    826: void
1.42      thorpej   827: pool_destroy(struct pool *pp)
1.1       pk        828: {
1.101     thorpej   829:        struct pool_pagelist pq;
1.3       pk        830:        struct pool_item_header *ph;
1.43      thorpej   831:
1.101     thorpej   832:        /* Remove from global pool list */
1.128.2.2  ad        833:        mutex_enter(&pool_head_lock);
1.102     chs       834:        LIST_REMOVE(pp, pr_poollist);
1.101     thorpej   835:        if (drainpp == pp)
                    836:                drainpp = NULL;
1.128.2.2  ad        837:        mutex_exit(&pool_head_lock);
1.101     thorpej   838:
                    839:        /* Remove this pool from its allocator's list of pools. */
1.117     yamt      840:        pool_reclaim_unregister(pp);
1.128.2.2  ad        841:        mutex_enter(&pp->pr_alloc->pa_lock);
1.66      thorpej   842:        TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list);
1.128.2.2  ad        843:        mutex_exit(&pp->pr_alloc->pa_lock);
1.66      thorpej   844:
1.128.2.2  ad        845:        mutex_enter(&pp->pr_lock);
1.101     thorpej   846:
1.102     chs       847:        KASSERT(LIST_EMPTY(&pp->pr_cachelist));
1.3       pk        848:
                    849: #ifdef DIAGNOSTIC
1.20      thorpej   850:        if (pp->pr_nout != 0) {
1.25      thorpej   851:                pr_printlog(pp, NULL, printf);
1.80      provos    852:                panic("pool_destroy: pool busy: still out: %u",
1.20      thorpej   853:                    pp->pr_nout);
1.3       pk        854:        }
                    855: #endif
1.1       pk        856:
1.101     thorpej   857:        KASSERT(LIST_EMPTY(&pp->pr_fullpages));
                    858:        KASSERT(LIST_EMPTY(&pp->pr_partpages));
                    859:
1.3       pk        860:        /* Remove all pages */
1.101     thorpej   861:        LIST_INIT(&pq);
1.88      chs       862:        while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1.101     thorpej   863:                pr_rmpage(pp, ph, &pq);
                    864:
1.128.2.2  ad        865:        mutex_exit(&pp->pr_lock);
1.3       pk        866:
1.101     thorpej   867:        pr_pagelist_free(pp, &pq);
1.3       pk        868:
1.59      thorpej   869: #ifdef POOL_DIAGNOSTIC
1.20      thorpej   870:        if ((pp->pr_roflags & PR_LOGGING) != 0)
1.3       pk        871:                free(pp->pr_log, M_TEMP);
1.59      thorpej   872: #endif
1.128.2.2  ad        873:
                    874:        cv_destroy(&pp->pr_cv);
                    875:        mutex_destroy(&pp->pr_lock);
1.1       pk        876: }
                    877:
1.68      thorpej   878: void
                    879: pool_set_drain_hook(struct pool *pp, void (*fn)(void *, int), void *arg)
                    880: {
                    881:
                    882:        /* XXX no locking -- must be used just after pool_init() */
                    883: #ifdef DIAGNOSTIC
                    884:        if (pp->pr_drain_hook != NULL)
                    885:                panic("pool_set_drain_hook(%s): already set", pp->pr_wchan);
                    886: #endif
                    887:        pp->pr_drain_hook = fn;
                    888:        pp->pr_drain_hook_arg = arg;
                    889: }
                    890:
1.88      chs       891: static struct pool_item_header *
1.128     christos  892: pool_alloc_item_header(struct pool *pp, void *storage, int flags)
1.55      thorpej   893: {
                    894:        struct pool_item_header *ph;
                    895:
                    896:        if ((pp->pr_roflags & PR_PHINPAGE) != 0)
1.128     christos  897:                ph = (struct pool_item_header *) ((char *)storage + pp->pr_phoffset);
1.128.2.2  ad        898:        else
1.97      yamt      899:                ph = pool_get(pp->pr_phpool, flags);
1.55      thorpej   900:
                    901:        return (ph);
                    902: }
1.1       pk        903:
                    904: /*
1.3       pk        905:  * Grab an item from the pool; must be called at appropriate spl level
1.1       pk        906:  */
1.3       pk        907: void *
1.59      thorpej   908: #ifdef POOL_DIAGNOSTIC
1.42      thorpej   909: _pool_get(struct pool *pp, int flags, const char *file, long line)
1.56      sommerfe  910: #else
                    911: pool_get(struct pool *pp, int flags)
                    912: #endif
1.1       pk        913: {
                    914:        struct pool_item *pi;
1.3       pk        915:        struct pool_item_header *ph;
1.55      thorpej   916:        void *v;
1.1       pk        917:
1.2       pk        918: #ifdef DIAGNOSTIC
1.95      atatat    919:        if (__predict_false(pp->pr_itemsperpage == 0))
                    920:                panic("pool_get: pool %p: pr_itemsperpage is zero, "
                    921:                    "pool not initialized?", pp);
1.84      thorpej   922:        if (__predict_false(curlwp == NULL && doing_shutdown == 0 &&
1.37      sommerfe  923:                            (flags & PR_WAITOK) != 0))
1.77      matt      924:                panic("pool_get: %s: must have NOWAIT", pp->pr_wchan);
1.58      thorpej   925:
1.102     chs       926: #endif /* DIAGNOSTIC */
1.58      thorpej   927: #ifdef LOCKDEBUG
                    928:        if (flags & PR_WAITOK)
1.119     yamt      929:                ASSERT_SLEEPABLE(NULL, "pool_get(PR_WAITOK)");
1.56      sommerfe  930: #endif
1.1       pk        931:
1.128.2.2  ad        932:        mutex_enter(&pp->pr_lock);
1.25      thorpej   933:        pr_enter(pp, file, line);
1.20      thorpej   934:
                    935:  startover:
                    936:        /*
                    937:         * Check to see if we've reached the hard limit.  If we have,
                    938:         * and we can wait, then wait until an item has been returned to
                    939:         * the pool.
                    940:         */
                    941: #ifdef DIAGNOSTIC
1.34      thorpej   942:        if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
1.25      thorpej   943:                pr_leave(pp);
1.128.2.2  ad        944:                mutex_exit(&pp->pr_lock);
1.20      thorpej   945:                panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
                    946:        }
                    947: #endif
1.34      thorpej   948:        if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
1.68      thorpej   949:                if (pp->pr_drain_hook != NULL) {
                    950:                        /*
                    951:                         * Since the drain hook is going to free things
                    952:                         * back to the pool, unlock, call the hook, re-lock,
                    953:                         * and check the hardlimit condition again.
                    954:                         */
                    955:                        pr_leave(pp);
1.128.2.2  ad        956:                        mutex_exit(&pp->pr_lock);
1.68      thorpej   957:                        (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
1.128.2.2  ad        958:                        mutex_enter(&pp->pr_lock);
1.68      thorpej   959:                        pr_enter(pp, file, line);
                    960:                        if (pp->pr_nout < pp->pr_hardlimit)
                    961:                                goto startover;
                    962:                }
                    963:
1.29      sommerfe  964:                if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
1.20      thorpej   965:                        /*
                    966:                         * XXX: A warning isn't logged in this case.  Should
                    967:                         * it be?
                    968:                         */
                    969:                        pp->pr_flags |= PR_WANTED;
1.25      thorpej   970:                        pr_leave(pp);
1.128.2.2  ad        971:                        cv_wait(&pp->pr_cv, &pp->pr_lock);
1.25      thorpej   972:                        pr_enter(pp, file, line);
1.20      thorpej   973:                        goto startover;
                    974:                }
1.31      thorpej   975:
                    976:                /*
                    977:                 * Log a message that the hard limit has been hit.
                    978:                 */
                    979:                if (pp->pr_hardlimit_warning != NULL &&
                    980:                    ratecheck(&pp->pr_hardlimit_warning_last,
                    981:                              &pp->pr_hardlimit_ratecap))
                    982:                        log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1.21      thorpej   983:
                    984:                pp->pr_nfail++;
                    985:
1.25      thorpej   986:                pr_leave(pp);
1.128.2.2  ad        987:                mutex_exit(&pp->pr_lock);
1.20      thorpej   988:                return (NULL);
                    989:        }
                    990:
1.3       pk        991:        /*
                    992:         * The convention we use is that if `curpage' is not NULL, then
                    993:         * it points at a non-empty bucket. In particular, `curpage'
                    994:         * never points at a page header which has PR_PHINPAGE set and
                    995:         * has no items in its bucket.
                    996:         */
1.20      thorpej   997:        if ((ph = pp->pr_curpage) == NULL) {
1.113     yamt      998:                int error;
                    999:
1.20      thorpej  1000: #ifdef DIAGNOSTIC
                   1001:                if (pp->pr_nitems != 0) {
1.128.2.2  ad       1002:                        mutex_exit(&pp->pr_lock);
1.20      thorpej  1003:                        printf("pool_get: %s: curpage NULL, nitems %u\n",
                   1004:                            pp->pr_wchan, pp->pr_nitems);
1.80      provos   1005:                        panic("pool_get: nitems inconsistent");
1.20      thorpej  1006:                }
                   1007: #endif
                   1008:
1.21      thorpej  1009:                /*
                   1010:                 * Call the back-end page allocator for more memory.
                   1011:                 * Release the pool lock, as the back-end page allocator
                   1012:                 * may block.
                   1013:                 */
1.25      thorpej  1014:                pr_leave(pp);
1.113     yamt     1015:                error = pool_grow(pp, flags);
                   1016:                pr_enter(pp, file, line);
                   1017:                if (error != 0) {
1.21      thorpej  1018:                        /*
1.55      thorpej  1019:                         * We were unable to allocate a page or item
                   1020:                         * header, but we released the lock during
                   1021:                         * allocation, so perhaps items were freed
                   1022:                         * back to the pool.  Check for this case.
1.21      thorpej  1023:                         */
                   1024:                        if (pp->pr_curpage != NULL)
                   1025:                                goto startover;
1.15      pk       1026:
1.117     yamt     1027:                        pp->pr_nfail++;
1.25      thorpej  1028:                        pr_leave(pp);
1.128.2.2  ad       1029:                        mutex_exit(&pp->pr_lock);
1.117     yamt     1030:                        return (NULL);
1.1       pk       1031:                }
1.3       pk       1032:
1.20      thorpej  1033:                /* Start the allocation process over. */
                   1034:                goto startover;
1.3       pk       1035:        }
1.97      yamt     1036:        if (pp->pr_roflags & PR_NOTOUCH) {
                   1037: #ifdef DIAGNOSTIC
                   1038:                if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) {
                   1039:                        pr_leave(pp);
1.128.2.2  ad       1040:                        mutex_exit(&pp->pr_lock);
1.97      yamt     1041:                        panic("pool_get: %s: page empty", pp->pr_wchan);
                   1042:                }
                   1043: #endif
                   1044:                v = pr_item_notouch_get(pp, ph);
                   1045: #ifdef POOL_DIAGNOSTIC
                   1046:                pr_log(pp, v, PRLOG_GET, file, line);
                   1047: #endif
                   1048:        } else {
1.102     chs      1049:                v = pi = LIST_FIRST(&ph->ph_itemlist);
1.97      yamt     1050:                if (__predict_false(v == NULL)) {
                   1051:                        pr_leave(pp);
1.128.2.2  ad       1052:                        mutex_exit(&pp->pr_lock);
1.97      yamt     1053:                        panic("pool_get: %s: page empty", pp->pr_wchan);
                   1054:                }
1.20      thorpej  1055: #ifdef DIAGNOSTIC
1.97      yamt     1056:                if (__predict_false(pp->pr_nitems == 0)) {
                   1057:                        pr_leave(pp);
1.128.2.2  ad       1058:                        mutex_exit(&pp->pr_lock);
1.97      yamt     1059:                        printf("pool_get: %s: items on itemlist, nitems %u\n",
                   1060:                            pp->pr_wchan, pp->pr_nitems);
                   1061:                        panic("pool_get: nitems inconsistent");
                   1062:                }
1.65      enami    1063: #endif
1.56      sommerfe 1064:
1.65      enami    1065: #ifdef POOL_DIAGNOSTIC
1.97      yamt     1066:                pr_log(pp, v, PRLOG_GET, file, line);
1.65      enami    1067: #endif
1.3       pk       1068:
1.65      enami    1069: #ifdef DIAGNOSTIC
1.97      yamt     1070:                if (__predict_false(pi->pi_magic != PI_MAGIC)) {
                   1071:                        pr_printlog(pp, pi, printf);
                   1072:                        panic("pool_get(%s): free list modified: "
                   1073:                            "magic=%x; page %p; item addr %p\n",
                   1074:                            pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
                   1075:                }
1.3       pk       1076: #endif
                   1077:
1.97      yamt     1078:                /*
                   1079:                 * Remove from item list.
                   1080:                 */
1.102     chs      1081:                LIST_REMOVE(pi, pi_list);
1.97      yamt     1082:        }
1.20      thorpej  1083:        pp->pr_nitems--;
                   1084:        pp->pr_nout++;
1.6       thorpej  1085:        if (ph->ph_nmissing == 0) {
                   1086: #ifdef DIAGNOSTIC
1.34      thorpej  1087:                if (__predict_false(pp->pr_nidle == 0))
1.6       thorpej  1088:                        panic("pool_get: nidle inconsistent");
                   1089: #endif
                   1090:                pp->pr_nidle--;
1.88      chs      1091:
                   1092:                /*
                   1093:                 * This page was previously empty.  Move it to the list of
                   1094:                 * partially-full pages.  This page is already curpage.
                   1095:                 */
                   1096:                LIST_REMOVE(ph, ph_pagelist);
                   1097:                LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.6       thorpej  1098:        }
1.3       pk       1099:        ph->ph_nmissing++;
1.97      yamt     1100:        if (ph->ph_nmissing == pp->pr_itemsperpage) {
1.21      thorpej  1101: #ifdef DIAGNOSTIC
1.97      yamt     1102:                if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 &&
1.102     chs      1103:                    !LIST_EMPTY(&ph->ph_itemlist))) {
1.25      thorpej  1104:                        pr_leave(pp);
1.128.2.2  ad       1105:                        mutex_exit(&pp->pr_lock);
1.21      thorpej  1106:                        panic("pool_get: %s: nmissing inconsistent",
                   1107:                            pp->pr_wchan);
                   1108:                }
                   1109: #endif
1.3       pk       1110:                /*
1.88      chs      1111:                 * This page is now full.  Move it to the full list
                   1112:                 * and select a new current page.
1.3       pk       1113:                 */
1.88      chs      1114:                LIST_REMOVE(ph, ph_pagelist);
                   1115:                LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
                   1116:                pool_update_curpage(pp);
1.1       pk       1117:        }
1.3       pk       1118:
                   1119:        pp->pr_nget++;
1.111     christos 1120:        pr_leave(pp);
1.20      thorpej  1121:
                   1122:        /*
                   1123:         * If we have a low water mark and we are now below that low
                   1124:         * water mark, add more items to the pool.
                   1125:         */
1.53      thorpej  1126:        if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20      thorpej  1127:                /*
                   1128:                 * XXX: Should we log a warning?  Should we set up a timeout
                   1129:                 * to try again in a second or so?  The latter could break
                   1130:                 * a caller's assumptions about interrupt protection, etc.
                   1131:                 */
                   1132:        }
                   1133:
1.128.2.2  ad       1134:        mutex_exit(&pp->pr_lock);
1.125     ad       1135:        KASSERT((((vaddr_t)v + pp->pr_itemoffset) & (pp->pr_align - 1)) == 0);
                   1136:        FREECHECK_OUT(&pp->pr_freecheck, v);
1.1       pk       1137:        return (v);
                   1138: }
                   1139:
                   1140: /*
1.43      thorpej  1141:  * Internal version of pool_put().  Pool is already locked/entered.
1.1       pk       1142:  */
1.43      thorpej  1143: static void
1.101     thorpej  1144: pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq)
1.1       pk       1145: {
                   1146:        struct pool_item *pi = v;
1.3       pk       1147:        struct pool_item_header *ph;
                   1148:
1.128.2.2  ad       1149:        KASSERT(mutex_owned(&pp->pr_lock));
1.125     ad       1150:        FREECHECK_IN(&pp->pr_freecheck, v);
1.61      chs      1151:
1.30      thorpej  1152: #ifdef DIAGNOSTIC
1.34      thorpej  1153:        if (__predict_false(pp->pr_nout == 0)) {
1.30      thorpej  1154:                printf("pool %s: putting with none out\n",
                   1155:                    pp->pr_wchan);
                   1156:                panic("pool_put");
                   1157:        }
                   1158: #endif
1.3       pk       1159:
1.121     yamt     1160:        if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) {
1.25      thorpej  1161:                pr_printlog(pp, NULL, printf);
1.3       pk       1162:                panic("pool_put: %s: page header missing", pp->pr_wchan);
                   1163:        }
1.28      thorpej  1164:
1.3       pk       1165:        /*
                   1166:         * Return to item list.
                   1167:         */
1.97      yamt     1168:        if (pp->pr_roflags & PR_NOTOUCH) {
                   1169:                pr_item_notouch_put(pp, ph, v);
                   1170:        } else {
1.2       pk       1171: #ifdef DIAGNOSTIC
1.97      yamt     1172:                pi->pi_magic = PI_MAGIC;
1.3       pk       1173: #endif
1.32      chs      1174: #ifdef DEBUG
1.97      yamt     1175:                {
                   1176:                        int i, *ip = v;
1.32      chs      1177:
1.97      yamt     1178:                        for (i = 0; i < pp->pr_size / sizeof(int); i++) {
                   1179:                                *ip++ = PI_MAGIC;
                   1180:                        }
1.32      chs      1181:                }
                   1182: #endif
                   1183:
1.102     chs      1184:                LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.97      yamt     1185:        }
1.79      thorpej  1186:        KDASSERT(ph->ph_nmissing != 0);
1.3       pk       1187:        ph->ph_nmissing--;
                   1188:        pp->pr_nput++;
1.20      thorpej  1189:        pp->pr_nitems++;
                   1190:        pp->pr_nout--;
1.3       pk       1191:
                   1192:        /* Cancel "pool empty" condition if it exists */
                   1193:        if (pp->pr_curpage == NULL)
                   1194:                pp->pr_curpage = ph;
                   1195:
                   1196:        if (pp->pr_flags & PR_WANTED) {
                   1197:                pp->pr_flags &= ~PR_WANTED;
1.15      pk       1198:                if (ph->ph_nmissing == 0)
                   1199:                        pp->pr_nidle++;
1.128.2.4! ad       1200:                cv_broadcast(&pp->pr_cv);
1.3       pk       1201:                return;
                   1202:        }
                   1203:
                   1204:        /*
1.88      chs      1205:         * If this page is now empty, do one of two things:
1.21      thorpej  1206:         *
1.88      chs      1207:         *      (1) If we have more pages than the page high water mark,
1.96      thorpej  1208:         *          free the page back to the system.  ONLY CONSIDER
1.90      thorpej  1209:         *          FREEING BACK A PAGE IF WE HAVE MORE THAN OUR MINIMUM PAGE
                   1210:         *          CLAIM.
1.21      thorpej  1211:         *
1.88      chs      1212:         *      (2) Otherwise, move the page to the empty page list.
                   1213:         *
                   1214:         * Either way, select a new current page (so we use a partially-full
                   1215:         * page if one is available).
1.3       pk       1216:         */
                   1217:        if (ph->ph_nmissing == 0) {
1.6       thorpej  1218:                pp->pr_nidle++;
1.90      thorpej  1219:                if (pp->pr_npages > pp->pr_minpages &&
                   1220:                    (pp->pr_npages > pp->pr_maxpages ||
1.117     yamt     1221:                     pa_starved_p(pp->pr_alloc))) {
1.101     thorpej  1222:                        pr_rmpage(pp, ph, pq);
1.3       pk       1223:                } else {
1.88      chs      1224:                        LIST_REMOVE(ph, ph_pagelist);
                   1225:                        LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.3       pk       1226:
1.21      thorpej  1227:                        /*
                   1228:                         * Update the timestamp on the page.  A page must
                   1229:                         * be idle for some period of time before it can
                   1230:                         * be reclaimed by the pagedaemon.  This minimizes
                   1231:                         * ping-pong'ing for memory.
                   1232:                         */
1.118     kardel   1233:                        getmicrotime(&ph->ph_time);
1.1       pk       1234:                }
1.88      chs      1235:                pool_update_curpage(pp);
1.1       pk       1236:        }
1.88      chs      1237:
1.21      thorpej  1238:        /*
1.88      chs      1239:         * If the page was previously completely full, move it to the
                   1240:         * partially-full list and make it the current page.  The next
                   1241:         * allocation will get the item from this page, instead of
                   1242:         * further fragmenting the pool.
1.21      thorpej  1243:         */
                   1244:        else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
1.88      chs      1245:                LIST_REMOVE(ph, ph_pagelist);
                   1246:                LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1.21      thorpej  1247:                pp->pr_curpage = ph;
                   1248:        }
1.43      thorpej  1249: }
                   1250:
                   1251: /*
                   1252:  * Return resource to the pool; must be called at appropriate spl level
                   1253:  */
1.59      thorpej  1254: #ifdef POOL_DIAGNOSTIC
1.43      thorpej  1255: void
                   1256: _pool_put(struct pool *pp, void *v, const char *file, long line)
                   1257: {
1.101     thorpej  1258:        struct pool_pagelist pq;
                   1259:
                   1260:        LIST_INIT(&pq);
1.43      thorpej  1261:
1.128.2.2  ad       1262:        mutex_enter(&pp->pr_lock);
1.43      thorpej  1263:        pr_enter(pp, file, line);
                   1264:
1.56      sommerfe 1265:        pr_log(pp, v, PRLOG_PUT, file, line);
                   1266:
1.101     thorpej  1267:        pool_do_put(pp, v, &pq);
1.21      thorpej  1268:
1.25      thorpej  1269:        pr_leave(pp);
1.128.2.2  ad       1270:        mutex_exit(&pp->pr_lock);
1.101     thorpej  1271:
1.102     chs      1272:        pr_pagelist_free(pp, &pq);
1.1       pk       1273: }
1.57      sommerfe 1274: #undef pool_put
1.59      thorpej  1275: #endif /* POOL_DIAGNOSTIC */
1.1       pk       1276:
1.56      sommerfe 1277: void
                   1278: pool_put(struct pool *pp, void *v)
                   1279: {
1.101     thorpej  1280:        struct pool_pagelist pq;
                   1281:
                   1282:        LIST_INIT(&pq);
1.56      sommerfe 1283:
1.128.2.2  ad       1284:        mutex_enter(&pp->pr_lock);
1.101     thorpej  1285:        pool_do_put(pp, v, &pq);
1.128.2.2  ad       1286:        mutex_exit(&pp->pr_lock);
1.56      sommerfe 1287:
1.102     chs      1288:        pr_pagelist_free(pp, &pq);
1.56      sommerfe 1289: }
1.57      sommerfe 1290:
1.59      thorpej  1291: #ifdef POOL_DIAGNOSTIC
1.57      sommerfe 1292: #define                pool_put(h, v)  _pool_put((h), (v), __FILE__, __LINE__)
1.56      sommerfe 1293: #endif
1.74      thorpej  1294:
                   1295: /*
1.113     yamt     1296:  * pool_grow: grow a pool by a page.
                   1297:  *
                   1298:  * => called with pool locked.
                   1299:  * => unlock and relock the pool.
                   1300:  * => return with pool locked.
                   1301:  */
                   1302:
                   1303: static int
                   1304: pool_grow(struct pool *pp, int flags)
                   1305: {
                   1306:        struct pool_item_header *ph = NULL;
                   1307:        char *cp;
                   1308:
1.128.2.2  ad       1309:        mutex_exit(&pp->pr_lock);
1.113     yamt     1310:        cp = pool_allocator_alloc(pp, flags);
                   1311:        if (__predict_true(cp != NULL)) {
                   1312:                ph = pool_alloc_item_header(pp, cp, flags);
                   1313:        }
                   1314:        if (__predict_false(cp == NULL || ph == NULL)) {
                   1315:                if (cp != NULL) {
                   1316:                        pool_allocator_free(pp, cp);
                   1317:                }
1.128.2.2  ad       1318:                mutex_enter(&pp->pr_lock);
1.113     yamt     1319:                return ENOMEM;
                   1320:        }
                   1321:
1.128.2.2  ad       1322:        mutex_enter(&pp->pr_lock);
1.113     yamt     1323:        pool_prime_page(pp, cp, ph);
                   1324:        pp->pr_npagealloc++;
                   1325:        return 0;
                   1326: }
                   1327:
                   1328: /*
1.74      thorpej  1329:  * Add N items to the pool.
                   1330:  */
                   1331: int
                   1332: pool_prime(struct pool *pp, int n)
                   1333: {
1.75      simonb   1334:        int newpages;
1.113     yamt     1335:        int error = 0;
1.74      thorpej  1336:
1.128.2.2  ad       1337:        mutex_enter(&pp->pr_lock);
1.74      thorpej  1338:
                   1339:        newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
                   1340:
                   1341:        while (newpages-- > 0) {
1.113     yamt     1342:                error = pool_grow(pp, PR_NOWAIT);
                   1343:                if (error) {
1.74      thorpej  1344:                        break;
                   1345:                }
                   1346:                pp->pr_minpages++;
                   1347:        }
                   1348:
                   1349:        if (pp->pr_minpages >= pp->pr_maxpages)
                   1350:                pp->pr_maxpages = pp->pr_minpages + 1;  /* XXX */
                   1351:
1.128.2.2  ad       1352:        mutex_exit(&pp->pr_lock);
1.113     yamt     1353:        return error;
1.74      thorpej  1354: }
1.55      thorpej  1355:
                   1356: /*
1.3       pk       1357:  * Add a page worth of items to the pool.
1.21      thorpej  1358:  *
                   1359:  * Note, we must be called with the pool descriptor LOCKED.
1.3       pk       1360:  */
1.55      thorpej  1361: static void
1.128     christos 1362: pool_prime_page(struct pool *pp, void *storage, struct pool_item_header *ph)
1.3       pk       1363: {
                   1364:        struct pool_item *pi;
1.128     christos 1365:        void *cp = storage;
1.125     ad       1366:        const unsigned int align = pp->pr_align;
                   1367:        const unsigned int ioff = pp->pr_itemoffset;
1.55      thorpej  1368:        int n;
1.36      pk       1369:
1.128.2.2  ad       1370:        KASSERT(mutex_owned(&pp->pr_lock));
1.91      yamt     1371:
1.66      thorpej  1372: #ifdef DIAGNOSTIC
1.121     yamt     1373:        if ((pp->pr_roflags & PR_NOALIGN) == 0 &&
                   1374:            ((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0)
1.36      pk       1375:                panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1.66      thorpej  1376: #endif
1.3       pk       1377:
                   1378:        /*
                   1379:         * Insert page header.
                   1380:         */
1.88      chs      1381:        LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1.102     chs      1382:        LIST_INIT(&ph->ph_itemlist);
1.3       pk       1383:        ph->ph_page = storage;
                   1384:        ph->ph_nmissing = 0;
1.118     kardel   1385:        getmicrotime(&ph->ph_time);
1.88      chs      1386:        if ((pp->pr_roflags & PR_PHINPAGE) == 0)
                   1387:                SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
1.3       pk       1388:
1.6       thorpej  1389:        pp->pr_nidle++;
                   1390:
1.3       pk       1391:        /*
                   1392:         * Color this page.
                   1393:         */
1.128     christos 1394:        cp = (char *)cp + pp->pr_curcolor;
1.3       pk       1395:        if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
                   1396:                pp->pr_curcolor = 0;
                   1397:
                   1398:        /*
                   1399:         * Adjust storage to apply aligment to `pr_itemoffset' in each item.
                   1400:         */
                   1401:        if (ioff != 0)
1.128     christos 1402:                cp = (char *)cp + align - ioff;
1.3       pk       1403:
1.125     ad       1404:        KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
                   1405:
1.3       pk       1406:        /*
                   1407:         * Insert remaining chunks on the bucket list.
                   1408:         */
                   1409:        n = pp->pr_itemsperpage;
1.20      thorpej  1410:        pp->pr_nitems += n;
1.3       pk       1411:
1.97      yamt     1412:        if (pp->pr_roflags & PR_NOTOUCH) {
1.99      yamt     1413:                pool_item_freelist_t *freelist = PR_FREELIST(ph);
1.97      yamt     1414:                int i;
                   1415:
1.128     christos 1416:                ph->ph_off = (char *)cp - (char *)storage;
1.97      yamt     1417:                ph->ph_firstfree = 0;
                   1418:                for (i = 0; i < n - 1; i++)
                   1419:                        freelist[i] = i + 1;
                   1420:                freelist[n - 1] = PR_INDEX_EOL;
                   1421:        } else {
                   1422:                while (n--) {
                   1423:                        pi = (struct pool_item *)cp;
1.78      thorpej  1424:
1.97      yamt     1425:                        KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
1.3       pk       1426:
1.97      yamt     1427:                        /* Insert on page list */
1.102     chs      1428:                        LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1.3       pk       1429: #ifdef DIAGNOSTIC
1.97      yamt     1430:                        pi->pi_magic = PI_MAGIC;
1.3       pk       1431: #endif
1.128     christos 1432:                        cp = (char *)cp + pp->pr_size;
1.125     ad       1433:
                   1434:                        KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1.97      yamt     1435:                }
1.3       pk       1436:        }
                   1437:
                   1438:        /*
                   1439:         * If the pool was depleted, point at the new page.
                   1440:         */
                   1441:        if (pp->pr_curpage == NULL)
                   1442:                pp->pr_curpage = ph;
                   1443:
                   1444:        if (++pp->pr_npages > pp->pr_hiwat)
                   1445:                pp->pr_hiwat = pp->pr_npages;
                   1446: }
                   1447:
1.20      thorpej  1448: /*
1.52      thorpej  1449:  * Used by pool_get() when nitems drops below the low water mark.  This
1.88      chs      1450:  * is used to catch up pr_nitems with the low water mark.
1.20      thorpej  1451:  *
1.21      thorpej  1452:  * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20      thorpej  1453:  *
1.73      thorpej  1454:  * Note 2, we must be called with the pool already locked, and we return
1.20      thorpej  1455:  * with it locked.
                   1456:  */
                   1457: static int
1.42      thorpej  1458: pool_catchup(struct pool *pp)
1.20      thorpej  1459: {
                   1460:        int error = 0;
                   1461:
1.54      thorpej  1462:        while (POOL_NEEDS_CATCHUP(pp)) {
1.113     yamt     1463:                error = pool_grow(pp, PR_NOWAIT);
                   1464:                if (error) {
1.20      thorpej  1465:                        break;
                   1466:                }
                   1467:        }
1.113     yamt     1468:        return error;
1.20      thorpej  1469: }
                   1470:
1.88      chs      1471: static void
                   1472: pool_update_curpage(struct pool *pp)
                   1473: {
                   1474:
                   1475:        pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
                   1476:        if (pp->pr_curpage == NULL) {
                   1477:                pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
                   1478:        }
                   1479: }
                   1480:
1.3       pk       1481: void
1.42      thorpej  1482: pool_setlowat(struct pool *pp, int n)
1.3       pk       1483: {
1.15      pk       1484:
1.128.2.2  ad       1485:        mutex_enter(&pp->pr_lock);
1.21      thorpej  1486:
1.3       pk       1487:        pp->pr_minitems = n;
1.15      pk       1488:        pp->pr_minpages = (n == 0)
                   1489:                ? 0
1.18      thorpej  1490:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20      thorpej  1491:
                   1492:        /* Make sure we're caught up with the newly-set low water mark. */
1.75      simonb   1493:        if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1.20      thorpej  1494:                /*
                   1495:                 * XXX: Should we log a warning?  Should we set up a timeout
                   1496:                 * to try again in a second or so?  The latter could break
                   1497:                 * a caller's assumptions about interrupt protection, etc.
                   1498:                 */
                   1499:        }
1.21      thorpej  1500:
1.128.2.2  ad       1501:        mutex_exit(&pp->pr_lock);
1.3       pk       1502: }
                   1503:
                   1504: void
1.42      thorpej  1505: pool_sethiwat(struct pool *pp, int n)
1.3       pk       1506: {
1.15      pk       1507:
1.128.2.2  ad       1508:        mutex_enter(&pp->pr_lock);
1.21      thorpej  1509:
1.15      pk       1510:        pp->pr_maxpages = (n == 0)
                   1511:                ? 0
1.18      thorpej  1512:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21      thorpej  1513:
1.128.2.2  ad       1514:        mutex_exit(&pp->pr_lock);
1.3       pk       1515: }
                   1516:
1.20      thorpej  1517: void
1.42      thorpej  1518: pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1.20      thorpej  1519: {
                   1520:
1.128.2.2  ad       1521:        mutex_enter(&pp->pr_lock);
1.20      thorpej  1522:
                   1523:        pp->pr_hardlimit = n;
                   1524:        pp->pr_hardlimit_warning = warnmess;
1.31      thorpej  1525:        pp->pr_hardlimit_ratecap.tv_sec = ratecap;
                   1526:        pp->pr_hardlimit_warning_last.tv_sec = 0;
                   1527:        pp->pr_hardlimit_warning_last.tv_usec = 0;
1.20      thorpej  1528:
                   1529:        /*
1.21      thorpej  1530:         * In-line version of pool_sethiwat(), because we don't want to
                   1531:         * release the lock.
1.20      thorpej  1532:         */
                   1533:        pp->pr_maxpages = (n == 0)
                   1534:                ? 0
                   1535:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21      thorpej  1536:
1.128.2.2  ad       1537:        mutex_exit(&pp->pr_lock);
1.20      thorpej  1538: }
1.3       pk       1539:
                   1540: /*
                   1541:  * Release all complete pages that have not been used recently.
                   1542:  */
1.66      thorpej  1543: int
1.59      thorpej  1544: #ifdef POOL_DIAGNOSTIC
1.42      thorpej  1545: _pool_reclaim(struct pool *pp, const char *file, long line)
1.56      sommerfe 1546: #else
                   1547: pool_reclaim(struct pool *pp)
                   1548: #endif
1.3       pk       1549: {
                   1550:        struct pool_item_header *ph, *phnext;
1.43      thorpej  1551:        struct pool_cache *pc;
1.61      chs      1552:        struct pool_pagelist pq;
1.102     chs      1553:        struct pool_cache_grouplist pcgl;
                   1554:        struct timeval curtime, diff;
1.3       pk       1555:
1.68      thorpej  1556:        if (pp->pr_drain_hook != NULL) {
                   1557:                /*
                   1558:                 * The drain hook must be called with the pool unlocked.
                   1559:                 */
                   1560:                (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT);
                   1561:        }
                   1562:
1.128.2.2  ad       1563:        if (mutex_tryenter(&pp->pr_lock) == 0)
1.66      thorpej  1564:                return (0);
1.25      thorpej  1565:        pr_enter(pp, file, line);
1.68      thorpej  1566:
1.88      chs      1567:        LIST_INIT(&pq);
1.102     chs      1568:        LIST_INIT(&pcgl);
1.3       pk       1569:
1.43      thorpej  1570:        /*
                   1571:         * Reclaim items from the pool's caches.
                   1572:         */
1.102     chs      1573:        LIST_FOREACH(pc, &pp->pr_cachelist, pc_poollist)
                   1574:                pool_cache_reclaim(pc, &pq, &pcgl);
1.43      thorpej  1575:
1.118     kardel   1576:        getmicrotime(&curtime);
1.21      thorpej  1577:
1.88      chs      1578:        for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
                   1579:                phnext = LIST_NEXT(ph, ph_pagelist);
1.3       pk       1580:
                   1581:                /* Check our minimum page claim */
                   1582:                if (pp->pr_npages <= pp->pr_minpages)
                   1583:                        break;
                   1584:
1.88      chs      1585:                KASSERT(ph->ph_nmissing == 0);
                   1586:                timersub(&curtime, &ph->ph_time, &diff);
1.117     yamt     1587:                if (diff.tv_sec < pool_inactive_time
                   1588:                    && !pa_starved_p(pp->pr_alloc))
1.88      chs      1589:                        continue;
1.21      thorpej  1590:
1.88      chs      1591:                /*
                   1592:                 * If freeing this page would put us below
                   1593:                 * the low water mark, stop now.
                   1594:                 */
                   1595:                if ((pp->pr_nitems - pp->pr_itemsperpage) <
                   1596:                    pp->pr_minitems)
                   1597:                        break;
1.21      thorpej  1598:
1.88      chs      1599:                pr_rmpage(pp, ph, &pq);
1.3       pk       1600:        }
                   1601:
1.25      thorpej  1602:        pr_leave(pp);
1.128.2.2  ad       1603:        mutex_exit(&pp->pr_lock);
1.102     chs      1604:        if (LIST_EMPTY(&pq) && LIST_EMPTY(&pcgl))
                   1605:                return 0;
1.66      thorpej  1606:
1.101     thorpej  1607:        pr_pagelist_free(pp, &pq);
1.102     chs      1608:        pcg_grouplist_free(&pcgl);
1.66      thorpej  1609:        return (1);
1.3       pk       1610: }
                   1611:
                   1612: /*
                   1613:  * Drain pools, one at a time.
1.21      thorpej  1614:  *
                   1615:  * Note, we must never be called from an interrupt context.
1.3       pk       1616:  */
                   1617: void
1.124     yamt     1618: pool_drain(void *arg)
1.3       pk       1619: {
                   1620:        struct pool *pp;
1.23      thorpej  1621:        int s;
1.3       pk       1622:
1.61      chs      1623:        pp = NULL;
1.128.2.2  ad       1624:        s = splvm();    /* XXX why? */
                   1625:        mutex_enter(&pool_head_lock);
1.61      chs      1626:        if (drainpp == NULL) {
1.102     chs      1627:                drainpp = LIST_FIRST(&pool_head);
1.61      chs      1628:        }
                   1629:        if (drainpp) {
                   1630:                pp = drainpp;
1.102     chs      1631:                drainpp = LIST_NEXT(pp, pr_poollist);
1.61      chs      1632:        }
1.128.2.2  ad       1633:        mutex_exit(&pool_head_lock);
1.115     christos 1634:        if (pp)
                   1635:                pool_reclaim(pp);
1.61      chs      1636:        splx(s);
1.3       pk       1637: }
                   1638:
                   1639: /*
                   1640:  * Diagnostic helpers.
                   1641:  */
                   1642: void
1.42      thorpej  1643: pool_print(struct pool *pp, const char *modif)
1.21      thorpej  1644: {
                   1645:
1.128.2.2  ad       1646:        if (mutex_tryenter(&pp->pr_lock) == 0) {
1.25      thorpej  1647:                printf("pool %s is locked; try again later\n",
                   1648:                    pp->pr_wchan);
                   1649:                return;
                   1650:        }
                   1651:        pool_print1(pp, modif, printf);
1.128.2.2  ad       1652:        mutex_exit(&pp->pr_lock);
1.21      thorpej  1653: }
                   1654:
1.25      thorpej  1655: void
1.108     yamt     1656: pool_printall(const char *modif, void (*pr)(const char *, ...))
                   1657: {
                   1658:        struct pool *pp;
                   1659:
1.128.2.2  ad       1660:        if (mutex_tryenter(&pool_head_lock) == 0) {
1.108     yamt     1661:                (*pr)("WARNING: pool_head_slock is locked\n");
                   1662:        } else {
1.128.2.2  ad       1663:                mutex_exit(&pool_head_lock);
1.108     yamt     1664:        }
                   1665:
                   1666:        LIST_FOREACH(pp, &pool_head, pr_poollist) {
                   1667:                pool_printit(pp, modif, pr);
                   1668:        }
                   1669: }
                   1670:
                   1671: void
1.42      thorpej  1672: pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.25      thorpej  1673: {
                   1674:
                   1675:        if (pp == NULL) {
                   1676:                (*pr)("Must specify a pool to print.\n");
                   1677:                return;
                   1678:        }
                   1679:
                   1680:        /*
                   1681:         * Called from DDB; interrupts should be blocked, and all
                   1682:         * other processors should be paused.  We can skip locking
                   1683:         * the pool in this case.
                   1684:         *
1.128.2.2  ad       1685:         * We do a mutex_tryenter() just to print the lock
1.25      thorpej  1686:         * status, however.
                   1687:         */
                   1688:
1.128.2.2  ad       1689:        if (mutex_tryenter(&pp->pr_lock) == 0)
1.25      thorpej  1690:                (*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
                   1691:        else
1.128.2.2  ad       1692:                mutex_exit(&pp->pr_lock);
1.25      thorpej  1693:
                   1694:        pool_print1(pp, modif, pr);
                   1695: }
                   1696:
1.21      thorpej  1697: static void
1.124     yamt     1698: pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
1.97      yamt     1699:     void (*pr)(const char *, ...))
1.88      chs      1700: {
                   1701:        struct pool_item_header *ph;
                   1702: #ifdef DIAGNOSTIC
                   1703:        struct pool_item *pi;
                   1704: #endif
                   1705:
                   1706:        LIST_FOREACH(ph, pl, ph_pagelist) {
                   1707:                (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
                   1708:                    ph->ph_page, ph->ph_nmissing,
                   1709:                    (u_long)ph->ph_time.tv_sec,
                   1710:                    (u_long)ph->ph_time.tv_usec);
                   1711: #ifdef DIAGNOSTIC
1.97      yamt     1712:                if (!(pp->pr_roflags & PR_NOTOUCH)) {
1.102     chs      1713:                        LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1.97      yamt     1714:                                if (pi->pi_magic != PI_MAGIC) {
                   1715:                                        (*pr)("\t\t\titem %p, magic 0x%x\n",
                   1716:                                            pi, pi->pi_magic);
                   1717:                                }
1.88      chs      1718:                        }
                   1719:                }
                   1720: #endif
                   1721:        }
                   1722: }
                   1723:
                   1724: static void
1.42      thorpej  1725: pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.3       pk       1726: {
1.25      thorpej  1727:        struct pool_item_header *ph;
1.44      thorpej  1728:        struct pool_cache *pc;
                   1729:        struct pool_cache_group *pcg;
                   1730:        int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1.25      thorpej  1731:        char c;
                   1732:
                   1733:        while ((c = *modif++) != '\0') {
                   1734:                if (c == 'l')
                   1735:                        print_log = 1;
                   1736:                if (c == 'p')
                   1737:                        print_pagelist = 1;
1.44      thorpej  1738:                if (c == 'c')
                   1739:                        print_cache = 1;
1.25      thorpej  1740:        }
                   1741:
                   1742:        (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
                   1743:            pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
                   1744:            pp->pr_roflags);
1.66      thorpej  1745:        (*pr)("\talloc %p\n", pp->pr_alloc);
1.25      thorpej  1746:        (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
                   1747:            pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
                   1748:        (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
                   1749:            pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
                   1750:
                   1751:        (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
                   1752:            pp->pr_nget, pp->pr_nfail, pp->pr_nput);
                   1753:        (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
                   1754:            pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
                   1755:
                   1756:        if (print_pagelist == 0)
                   1757:                goto skip_pagelist;
                   1758:
1.88      chs      1759:        if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
                   1760:                (*pr)("\n\tempty page list:\n");
1.97      yamt     1761:        pool_print_pagelist(pp, &pp->pr_emptypages, pr);
1.88      chs      1762:        if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
                   1763:                (*pr)("\n\tfull page list:\n");
1.97      yamt     1764:        pool_print_pagelist(pp, &pp->pr_fullpages, pr);
1.88      chs      1765:        if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
                   1766:                (*pr)("\n\tpartial-page list:\n");
1.97      yamt     1767:        pool_print_pagelist(pp, &pp->pr_partpages, pr);
1.88      chs      1768:
1.25      thorpej  1769:        if (pp->pr_curpage == NULL)
                   1770:                (*pr)("\tno current page\n");
                   1771:        else
                   1772:                (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
                   1773:
                   1774:  skip_pagelist:
                   1775:        if (print_log == 0)
                   1776:                goto skip_log;
                   1777:
                   1778:        (*pr)("\n");
                   1779:        if ((pp->pr_roflags & PR_LOGGING) == 0)
                   1780:                (*pr)("\tno log\n");
1.122     christos 1781:        else {
1.25      thorpej  1782:                pr_printlog(pp, NULL, pr);
1.122     christos 1783:        }
1.3       pk       1784:
1.25      thorpej  1785:  skip_log:
1.44      thorpej  1786:        if (print_cache == 0)
                   1787:                goto skip_cache;
                   1788:
1.102     chs      1789: #define PR_GROUPLIST(pcg)                                              \
                   1790:        (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail);         \
                   1791:        for (i = 0; i < PCG_NOBJECTS; i++) {                            \
                   1792:                if (pcg->pcg_objects[i].pcgo_pa !=                      \
                   1793:                    POOL_PADDR_INVALID) {                               \
                   1794:                        (*pr)("\t\t\t%p, 0x%llx\n",                     \
                   1795:                            pcg->pcg_objects[i].pcgo_va,                \
                   1796:                            (unsigned long long)                        \
                   1797:                            pcg->pcg_objects[i].pcgo_pa);               \
                   1798:                } else {                                                \
                   1799:                        (*pr)("\t\t\t%p\n",                             \
                   1800:                            pcg->pcg_objects[i].pcgo_va);               \
                   1801:                }                                                       \
                   1802:        }
                   1803:
                   1804:        LIST_FOREACH(pc, &pp->pr_cachelist, pc_poollist) {
1.103     chs      1805:                (*pr)("\tcache %p\n", pc);
1.48      thorpej  1806:                (*pr)("\t    hits %lu misses %lu ngroups %lu nitems %lu\n",
                   1807:                    pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems);
1.102     chs      1808:                (*pr)("\t    full groups:\n");
1.103     chs      1809:                LIST_FOREACH(pcg, &pc->pc_fullgroups, pcg_list) {
1.102     chs      1810:                        PR_GROUPLIST(pcg);
1.103     chs      1811:                }
1.102     chs      1812:                (*pr)("\t    partial groups:\n");
1.103     chs      1813:                LIST_FOREACH(pcg, &pc->pc_partgroups, pcg_list) {
1.102     chs      1814:                        PR_GROUPLIST(pcg);
1.103     chs      1815:                }
1.102     chs      1816:                (*pr)("\t    empty groups:\n");
1.103     chs      1817:                LIST_FOREACH(pcg, &pc->pc_emptygroups, pcg_list) {
1.102     chs      1818:                        PR_GROUPLIST(pcg);
1.103     chs      1819:                }
1.44      thorpej  1820:        }
1.102     chs      1821: #undef PR_GROUPLIST
1.44      thorpej  1822:
                   1823:  skip_cache:
1.88      chs      1824:        pr_enter_check(pp, pr);
                   1825: }
                   1826:
                   1827: static int
                   1828: pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
                   1829: {
                   1830:        struct pool_item *pi;
1.128     christos 1831:        void *page;
1.88      chs      1832:        int n;
                   1833:
1.121     yamt     1834:        if ((pp->pr_roflags & PR_NOALIGN) == 0) {
1.128     christos 1835:                page = (void *)((uintptr_t)ph & pp->pr_alloc->pa_pagemask);
1.121     yamt     1836:                if (page != ph->ph_page &&
                   1837:                    (pp->pr_roflags & PR_PHINPAGE) != 0) {
                   1838:                        if (label != NULL)
                   1839:                                printf("%s: ", label);
                   1840:                        printf("pool(%p:%s): page inconsistency: page %p;"
                   1841:                               " at page head addr %p (p %p)\n", pp,
                   1842:                                pp->pr_wchan, ph->ph_page,
                   1843:                                ph, page);
                   1844:                        return 1;
                   1845:                }
1.88      chs      1846:        }
1.3       pk       1847:
1.97      yamt     1848:        if ((pp->pr_roflags & PR_NOTOUCH) != 0)
                   1849:                return 0;
                   1850:
1.102     chs      1851:        for (pi = LIST_FIRST(&ph->ph_itemlist), n = 0;
1.88      chs      1852:             pi != NULL;
1.102     chs      1853:             pi = LIST_NEXT(pi,pi_list), n++) {
1.88      chs      1854:
                   1855: #ifdef DIAGNOSTIC
                   1856:                if (pi->pi_magic != PI_MAGIC) {
                   1857:                        if (label != NULL)
                   1858:                                printf("%s: ", label);
                   1859:                        printf("pool(%s): free list modified: magic=%x;"
1.121     yamt     1860:                               " page %p; item ordinal %d; addr %p\n",
1.88      chs      1861:                                pp->pr_wchan, pi->pi_magic, ph->ph_page,
1.121     yamt     1862:                                n, pi);
1.88      chs      1863:                        panic("pool");
                   1864:                }
                   1865: #endif
1.121     yamt     1866:                if ((pp->pr_roflags & PR_NOALIGN) != 0) {
                   1867:                        continue;
                   1868:                }
1.128     christos 1869:                page = (void *)((uintptr_t)pi & pp->pr_alloc->pa_pagemask);
1.88      chs      1870:                if (page == ph->ph_page)
                   1871:                        continue;
                   1872:
                   1873:                if (label != NULL)
                   1874:                        printf("%s: ", label);
                   1875:                printf("pool(%p:%s): page inconsistency: page %p;"
                   1876:                       " item ordinal %d; addr %p (p %p)\n", pp,
                   1877:                        pp->pr_wchan, ph->ph_page,
                   1878:                        n, pi, page);
                   1879:                return 1;
                   1880:        }
                   1881:        return 0;
1.3       pk       1882: }
                   1883:
1.88      chs      1884:
1.3       pk       1885: int
1.42      thorpej  1886: pool_chk(struct pool *pp, const char *label)
1.3       pk       1887: {
                   1888:        struct pool_item_header *ph;
                   1889:        int r = 0;
                   1890:
1.128.2.2  ad       1891:        mutex_enter(&pp->pr_lock);
1.88      chs      1892:        LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
                   1893:                r = pool_chk_page(pp, label, ph);
                   1894:                if (r) {
                   1895:                        goto out;
                   1896:                }
                   1897:        }
                   1898:        LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
                   1899:                r = pool_chk_page(pp, label, ph);
                   1900:                if (r) {
1.3       pk       1901:                        goto out;
                   1902:                }
1.88      chs      1903:        }
                   1904:        LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
                   1905:                r = pool_chk_page(pp, label, ph);
                   1906:                if (r) {
1.3       pk       1907:                        goto out;
                   1908:                }
                   1909:        }
1.88      chs      1910:
1.3       pk       1911: out:
1.128.2.2  ad       1912:        mutex_exit(&pp->pr_lock);
1.3       pk       1913:        return (r);
1.43      thorpej  1914: }
                   1915:
                   1916: /*
                   1917:  * pool_cache_init:
                   1918:  *
                   1919:  *     Initialize a pool cache.
                   1920:  *
                   1921:  *     NOTE: If the pool must be protected from interrupts, we expect
                   1922:  *     to be called at the appropriate interrupt priority level.
                   1923:  */
                   1924: void
                   1925: pool_cache_init(struct pool_cache *pc, struct pool *pp,
                   1926:     int (*ctor)(void *, void *, int),
                   1927:     void (*dtor)(void *, void *),
                   1928:     void *arg)
                   1929: {
                   1930:
1.102     chs      1931:        LIST_INIT(&pc->pc_emptygroups);
                   1932:        LIST_INIT(&pc->pc_fullgroups);
                   1933:        LIST_INIT(&pc->pc_partgroups);
1.128.2.2  ad       1934:        mutex_init(&pc->pc_lock, MUTEX_DRIVER, pp->pr_ipl);
1.43      thorpej  1935:
                   1936:        pc->pc_pool = pp;
                   1937:
                   1938:        pc->pc_ctor = ctor;
                   1939:        pc->pc_dtor = dtor;
                   1940:        pc->pc_arg  = arg;
                   1941:
1.48      thorpej  1942:        pc->pc_hits   = 0;
                   1943:        pc->pc_misses = 0;
                   1944:
                   1945:        pc->pc_ngroups = 0;
                   1946:
                   1947:        pc->pc_nitems = 0;
                   1948:
1.128.2.2  ad       1949:        if (__predict_true(!cold)) {
                   1950:                mutex_enter(&pp->pr_lock);
                   1951:                LIST_INSERT_HEAD(&pp->pr_cachelist, pc, pc_poollist);
                   1952:                mutex_exit(&pp->pr_lock);
                   1953:        } else
                   1954:                LIST_INSERT_HEAD(&pp->pr_cachelist, pc, pc_poollist);
1.43      thorpej  1955: }
                   1956:
                   1957: /*
                   1958:  * pool_cache_destroy:
                   1959:  *
                   1960:  *     Destroy a pool cache.
                   1961:  */
                   1962: void
                   1963: pool_cache_destroy(struct pool_cache *pc)
                   1964: {
                   1965:        struct pool *pp = pc->pc_pool;
                   1966:
                   1967:        /* First, invalidate the entire cache. */
                   1968:        pool_cache_invalidate(pc);
                   1969:
                   1970:        /* ...and remove it from the pool's cache list. */
1.128.2.2  ad       1971:        mutex_enter(&pp->pr_lock);
1.102     chs      1972:        LIST_REMOVE(pc, pc_poollist);
1.128.2.2  ad       1973:        mutex_exit(&pp->pr_lock);
                   1974:
                   1975:        mutex_destroy(&pc->pc_lock);
1.43      thorpej  1976: }
                   1977:
1.110     perry    1978: static inline void *
1.87      thorpej  1979: pcg_get(struct pool_cache_group *pcg, paddr_t *pap)
1.43      thorpej  1980: {
                   1981:        void *object;
                   1982:        u_int idx;
                   1983:
                   1984:        KASSERT(pcg->pcg_avail <= PCG_NOBJECTS);
1.45      thorpej  1985:        KASSERT(pcg->pcg_avail != 0);
1.43      thorpej  1986:        idx = --pcg->pcg_avail;
                   1987:
1.87      thorpej  1988:        KASSERT(pcg->pcg_objects[idx].pcgo_va != NULL);
                   1989:        object = pcg->pcg_objects[idx].pcgo_va;
                   1990:        if (pap != NULL)
                   1991:                *pap = pcg->pcg_objects[idx].pcgo_pa;
                   1992:        pcg->pcg_objects[idx].pcgo_va = NULL;
1.43      thorpej  1993:
                   1994:        return (object);
                   1995: }
                   1996:
1.110     perry    1997: static inline void
1.87      thorpej  1998: pcg_put(struct pool_cache_group *pcg, void *object, paddr_t pa)
1.43      thorpej  1999: {
                   2000:        u_int idx;
                   2001:
                   2002:        KASSERT(pcg->pcg_avail < PCG_NOBJECTS);
                   2003:        idx = pcg->pcg_avail++;
                   2004:
1.87      thorpej  2005:        KASSERT(pcg->pcg_objects[idx].pcgo_va == NULL);
                   2006:        pcg->pcg_objects[idx].pcgo_va = object;
                   2007:        pcg->pcg_objects[idx].pcgo_pa = pa;
1.43      thorpej  2008: }
                   2009:
1.102     chs      2010: static void
                   2011: pcg_grouplist_free(struct pool_cache_grouplist *pcgl)
                   2012: {
                   2013:        struct pool_cache_group *pcg;
                   2014:
                   2015:        while ((pcg = LIST_FIRST(pcgl)) != NULL) {
                   2016:                LIST_REMOVE(pcg, pcg_list);
                   2017:                pool_put(&pcgpool, pcg);
                   2018:        }
                   2019: }
                   2020:
1.43      thorpej  2021: /*
1.87      thorpej  2022:  * pool_cache_get{,_paddr}:
1.43      thorpej  2023:  *
1.87      thorpej  2024:  *     Get an object from a pool cache (optionally returning
                   2025:  *     the physical address of the object).
1.43      thorpej  2026:  */
                   2027: void *
1.87      thorpej  2028: pool_cache_get_paddr(struct pool_cache *pc, int flags, paddr_t *pap)
1.43      thorpej  2029: {
                   2030:        struct pool_cache_group *pcg;
                   2031:        void *object;
1.58      thorpej  2032:
                   2033: #ifdef LOCKDEBUG
                   2034:        if (flags & PR_WAITOK)
1.119     yamt     2035:                ASSERT_SLEEPABLE(NULL, "pool_cache_get(PR_WAITOK)");
1.58      thorpej  2036: #endif
1.43      thorpej  2037:
1.128.2.2  ad       2038:        mutex_enter(&pc->pc_lock);
1.43      thorpej  2039:
1.102     chs      2040:        pcg = LIST_FIRST(&pc->pc_partgroups);
                   2041:        if (pcg == NULL) {
                   2042:                pcg = LIST_FIRST(&pc->pc_fullgroups);
                   2043:                if (pcg != NULL) {
                   2044:                        LIST_REMOVE(pcg, pcg_list);
                   2045:                        LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
1.43      thorpej  2046:                }
1.102     chs      2047:        }
                   2048:        if (pcg == NULL) {
1.43      thorpej  2049:
                   2050:                /*
                   2051:                 * No groups with any available objects.  Allocate
                   2052:                 * a new object, construct it, and return it to
                   2053:                 * the caller.  We will allocate a group, if necessary,
                   2054:                 * when the object is freed back to the cache.
                   2055:                 */
1.48      thorpej  2056:                pc->pc_misses++;
1.128.2.2  ad       2057:                mutex_exit(&pc->pc_lock);
1.43      thorpej  2058:                object = pool_get(pc->pc_pool, flags);
                   2059:                if (object != NULL && pc->pc_ctor != NULL) {
                   2060:                        if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) {
                   2061:                                pool_put(pc->pc_pool, object);
                   2062:                                return (NULL);
                   2063:                        }
                   2064:                }
1.125     ad       2065:                KASSERT((((vaddr_t)object + pc->pc_pool->pr_itemoffset) &
                   2066:                    (pc->pc_pool->pr_align - 1)) == 0);
1.87      thorpej  2067:                if (object != NULL && pap != NULL) {
                   2068: #ifdef POOL_VTOPHYS
                   2069:                        *pap = POOL_VTOPHYS(object);
                   2070: #else
                   2071:                        *pap = POOL_PADDR_INVALID;
                   2072: #endif
                   2073:                }
1.125     ad       2074:
                   2075:                FREECHECK_OUT(&pc->pc_freecheck, object);
1.43      thorpej  2076:                return (object);
                   2077:        }
                   2078:
1.48      thorpej  2079:        pc->pc_hits++;
                   2080:        pc->pc_nitems--;
1.87      thorpej  2081:        object = pcg_get(pcg, pap);
1.43      thorpej  2082:
1.102     chs      2083:        if (pcg->pcg_avail == 0) {
                   2084:                LIST_REMOVE(pcg, pcg_list);
                   2085:                LIST_INSERT_HEAD(&pc->pc_emptygroups, pcg, pcg_list);
                   2086:        }
1.128.2.2  ad       2087:        mutex_exit(&pc->pc_lock);
1.43      thorpej  2088:
1.125     ad       2089:        KASSERT((((vaddr_t)object + pc->pc_pool->pr_itemoffset) &
                   2090:            (pc->pc_pool->pr_align - 1)) == 0);
                   2091:        FREECHECK_OUT(&pc->pc_freecheck, object);
1.43      thorpej  2092:        return (object);
                   2093: }
                   2094:
                   2095: /*
1.87      thorpej  2096:  * pool_cache_put{,_paddr}:
1.43      thorpej  2097:  *
1.87      thorpej  2098:  *     Put an object back to the pool cache (optionally caching the
                   2099:  *     physical address of the object).
1.43      thorpej  2100:  */
                   2101: void
1.87      thorpej  2102: pool_cache_put_paddr(struct pool_cache *pc, void *object, paddr_t pa)
1.43      thorpej  2103: {
                   2104:        struct pool_cache_group *pcg;
                   2105:
1.125     ad       2106:        FREECHECK_IN(&pc->pc_freecheck, object);
                   2107:
1.109     christos 2108:        if (__predict_false((pc->pc_pool->pr_flags & PR_WANTED) != 0)) {
                   2109:                goto destruct;
                   2110:        }
                   2111:
1.128.2.2  ad       2112:        mutex_enter(&pc->pc_lock);
1.43      thorpej  2113:
1.102     chs      2114:        pcg = LIST_FIRST(&pc->pc_partgroups);
                   2115:        if (pcg == NULL) {
                   2116:                pcg = LIST_FIRST(&pc->pc_emptygroups);
                   2117:                if (pcg != NULL) {
                   2118:                        LIST_REMOVE(pcg, pcg_list);
                   2119:                        LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
1.43      thorpej  2120:                }
1.102     chs      2121:        }
                   2122:        if (pcg == NULL) {
1.43      thorpej  2123:
                   2124:                /*
                   2125:                 * No empty groups to free the object to.  Attempt to
1.47      thorpej  2126:                 * allocate one.
1.43      thorpej  2127:                 */
1.128.2.2  ad       2128:                mutex_exit(&pc->pc_lock);
1.43      thorpej  2129:                pcg = pool_get(&pcgpool, PR_NOWAIT);
1.102     chs      2130:                if (pcg == NULL) {
1.109     christos 2131: destruct:
1.102     chs      2132:
                   2133:                        /*
                   2134:                         * Unable to allocate a cache group; destruct the object
                   2135:                         * and free it back to the pool.
                   2136:                         */
                   2137:                        pool_cache_destruct_object(pc, object);
                   2138:                        return;
1.43      thorpej  2139:                }
1.102     chs      2140:                memset(pcg, 0, sizeof(*pcg));
1.128.2.2  ad       2141:                mutex_enter(&pc->pc_lock);
1.102     chs      2142:                pc->pc_ngroups++;
                   2143:                LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
1.43      thorpej  2144:        }
                   2145:
1.48      thorpej  2146:        pc->pc_nitems++;
1.87      thorpej  2147:        pcg_put(pcg, object, pa);
1.43      thorpej  2148:
1.102     chs      2149:        if (pcg->pcg_avail == PCG_NOBJECTS) {
                   2150:                LIST_REMOVE(pcg, pcg_list);
                   2151:                LIST_INSERT_HEAD(&pc->pc_fullgroups, pcg, pcg_list);
                   2152:        }
1.128.2.2  ad       2153:        mutex_exit(&pc->pc_lock);
1.51      thorpej  2154: }
                   2155:
                   2156: /*
                   2157:  * pool_cache_destruct_object:
                   2158:  *
                   2159:  *     Force destruction of an object and its release back into
                   2160:  *     the pool.
                   2161:  */
                   2162: void
                   2163: pool_cache_destruct_object(struct pool_cache *pc, void *object)
                   2164: {
                   2165:
                   2166:        if (pc->pc_dtor != NULL)
                   2167:                (*pc->pc_dtor)(pc->pc_arg, object);
                   2168:        pool_put(pc->pc_pool, object);
1.43      thorpej  2169: }
                   2170:
1.102     chs      2171: static void
1.106     christos 2172: pool_do_cache_invalidate_grouplist(struct pool_cache_grouplist *pcgsl,
1.105     christos 2173:     struct pool_cache *pc, struct pool_pagelist *pq,
1.106     christos 2174:     struct pool_cache_grouplist *pcgdl)
1.102     chs      2175: {
1.106     christos 2176:        struct pool_cache_group *pcg, *npcg;
1.102     chs      2177:        void *object;
                   2178:
1.106     christos 2179:        for (pcg = LIST_FIRST(pcgsl); pcg != NULL; pcg = npcg) {
1.102     chs      2180:                npcg = LIST_NEXT(pcg, pcg_list);
                   2181:                while (pcg->pcg_avail != 0) {
                   2182:                        pc->pc_nitems--;
                   2183:                        object = pcg_get(pcg, NULL);
                   2184:                        if (pc->pc_dtor != NULL)
                   2185:                                (*pc->pc_dtor)(pc->pc_arg, object);
                   2186:                        pool_do_put(pc->pc_pool, object, pq);
                   2187:                }
1.103     chs      2188:                pc->pc_ngroups--;
1.102     chs      2189:                LIST_REMOVE(pcg, pcg_list);
1.106     christos 2190:                LIST_INSERT_HEAD(pcgdl, pcg, pcg_list);
1.102     chs      2191:        }
1.105     christos 2192: }
                   2193:
                   2194: static void
                   2195: pool_do_cache_invalidate(struct pool_cache *pc, struct pool_pagelist *pq,
                   2196:     struct pool_cache_grouplist *pcgl)
                   2197: {
                   2198:
1.128.2.2  ad       2199:        KASSERT(mutex_owned(&pc->pc_lock));
                   2200:        KASSERT(mutex_owned(&pc->pc_pool->pr_lock));
1.105     christos 2201:
1.106     christos 2202:        pool_do_cache_invalidate_grouplist(&pc->pc_fullgroups, pc, pq, pcgl);
                   2203:        pool_do_cache_invalidate_grouplist(&pc->pc_partgroups, pc, pq, pcgl);
1.103     chs      2204:
                   2205:        KASSERT(LIST_EMPTY(&pc->pc_partgroups));
                   2206:        KASSERT(LIST_EMPTY(&pc->pc_fullgroups));
                   2207:        KASSERT(pc->pc_nitems == 0);
1.102     chs      2208: }
                   2209:
1.43      thorpej  2210: /*
1.101     thorpej  2211:  * pool_cache_invalidate:
1.43      thorpej  2212:  *
1.101     thorpej  2213:  *     Invalidate a pool cache (destruct and release all of the
                   2214:  *     cached objects).
1.43      thorpej  2215:  */
1.101     thorpej  2216: void
                   2217: pool_cache_invalidate(struct pool_cache *pc)
1.43      thorpej  2218: {
1.101     thorpej  2219:        struct pool_pagelist pq;
1.102     chs      2220:        struct pool_cache_grouplist pcgl;
1.101     thorpej  2221:
                   2222:        LIST_INIT(&pq);
1.102     chs      2223:        LIST_INIT(&pcgl);
1.101     thorpej  2224:
1.128.2.2  ad       2225:        mutex_enter(&pc->pc_lock);
                   2226:        mutex_enter(&pc->pc_pool->pr_lock);
1.43      thorpej  2227:
1.102     chs      2228:        pool_do_cache_invalidate(pc, &pq, &pcgl);
1.43      thorpej  2229:
1.128.2.2  ad       2230:        mutex_exit(&pc->pc_pool->pr_lock);
                   2231:        mutex_exit(&pc->pc_lock);
1.43      thorpej  2232:
1.102     chs      2233:        pr_pagelist_free(pc->pc_pool, &pq);
                   2234:        pcg_grouplist_free(&pcgl);
1.43      thorpej  2235: }
                   2236:
                   2237: /*
                   2238:  * pool_cache_reclaim:
                   2239:  *
                   2240:  *     Reclaim a pool cache for pool_reclaim().
                   2241:  */
                   2242: static void
1.102     chs      2243: pool_cache_reclaim(struct pool_cache *pc, struct pool_pagelist *pq,
                   2244:     struct pool_cache_grouplist *pcgl)
1.43      thorpej  2245: {
1.101     thorpej  2246:
                   2247:        /*
                   2248:         * We're locking in the wrong order (normally pool_cache -> pool,
                   2249:         * but the pool is already locked when we get here), so we have
                   2250:         * to use trylock.  If we can't lock the pool_cache, it's not really
                   2251:         * a big deal here.
                   2252:         */
1.128.2.2  ad       2253:        if (mutex_tryenter(&pc->pc_lock) == 0)
1.101     thorpej  2254:                return;
                   2255:
1.102     chs      2256:        pool_do_cache_invalidate(pc, pq, pcgl);
1.43      thorpej  2257:
1.128.2.2  ad       2258:        mutex_exit(&pc->pc_lock);
1.3       pk       2259: }
1.66      thorpej  2260:
                   2261: /*
                   2262:  * Pool backend allocators.
                   2263:  *
                   2264:  * Each pool has a backend allocator that handles allocation, deallocation,
                   2265:  * and any additional draining that might be needed.
                   2266:  *
                   2267:  * We provide two standard allocators:
                   2268:  *
                   2269:  *     pool_allocator_kmem - the default when no allocator is specified
                   2270:  *
                   2271:  *     pool_allocator_nointr - used for pools that will not be accessed
                   2272:  *     in interrupt context.
                   2273:  */
                   2274: void   *pool_page_alloc(struct pool *, int);
                   2275: void   pool_page_free(struct pool *, void *);
                   2276:
1.112     bjh21    2277: #ifdef POOL_SUBPAGE
                   2278: struct pool_allocator pool_allocator_kmem_fullpage = {
                   2279:        pool_page_alloc, pool_page_free, 0,
1.117     yamt     2280:        .pa_backingmapptr = &kmem_map,
1.112     bjh21    2281: };
                   2282: #else
1.66      thorpej  2283: struct pool_allocator pool_allocator_kmem = {
                   2284:        pool_page_alloc, pool_page_free, 0,
1.117     yamt     2285:        .pa_backingmapptr = &kmem_map,
1.66      thorpej  2286: };
1.112     bjh21    2287: #endif
1.66      thorpej  2288:
                   2289: void   *pool_page_alloc_nointr(struct pool *, int);
                   2290: void   pool_page_free_nointr(struct pool *, void *);
                   2291:
1.112     bjh21    2292: #ifdef POOL_SUBPAGE
                   2293: struct pool_allocator pool_allocator_nointr_fullpage = {
                   2294:        pool_page_alloc_nointr, pool_page_free_nointr, 0,
1.117     yamt     2295:        .pa_backingmapptr = &kernel_map,
1.112     bjh21    2296: };
                   2297: #else
1.66      thorpej  2298: struct pool_allocator pool_allocator_nointr = {
                   2299:        pool_page_alloc_nointr, pool_page_free_nointr, 0,
1.117     yamt     2300:        .pa_backingmapptr = &kernel_map,
1.66      thorpej  2301: };
1.112     bjh21    2302: #endif
1.66      thorpej  2303:
                   2304: #ifdef POOL_SUBPAGE
                   2305: void   *pool_subpage_alloc(struct pool *, int);
                   2306: void   pool_subpage_free(struct pool *, void *);
                   2307:
1.112     bjh21    2308: struct pool_allocator pool_allocator_kmem = {
                   2309:        pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
1.117     yamt     2310:        .pa_backingmapptr = &kmem_map,
1.112     bjh21    2311: };
                   2312:
                   2313: void   *pool_subpage_alloc_nointr(struct pool *, int);
                   2314: void   pool_subpage_free_nointr(struct pool *, void *);
                   2315:
                   2316: struct pool_allocator pool_allocator_nointr = {
                   2317:        pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
1.117     yamt     2318:        .pa_backingmapptr = &kmem_map,
1.66      thorpej  2319: };
                   2320: #endif /* POOL_SUBPAGE */
                   2321:
1.117     yamt     2322: static void *
                   2323: pool_allocator_alloc(struct pool *pp, int flags)
1.66      thorpej  2324: {
1.117     yamt     2325:        struct pool_allocator *pa = pp->pr_alloc;
1.66      thorpej  2326:        void *res;
                   2327:
1.117     yamt     2328:        res = (*pa->pa_alloc)(pp, flags);
                   2329:        if (res == NULL && (flags & PR_WAITOK) == 0) {
1.66      thorpej  2330:                /*
1.117     yamt     2331:                 * We only run the drain hook here if PR_NOWAIT.
                   2332:                 * In other cases, the hook will be run in
                   2333:                 * pool_reclaim().
1.66      thorpej  2334:                 */
1.117     yamt     2335:                if (pp->pr_drain_hook != NULL) {
                   2336:                        (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
                   2337:                        res = (*pa->pa_alloc)(pp, flags);
1.66      thorpej  2338:                }
1.117     yamt     2339:        }
                   2340:        return res;
1.66      thorpej  2341: }
                   2342:
1.117     yamt     2343: static void
1.66      thorpej  2344: pool_allocator_free(struct pool *pp, void *v)
                   2345: {
                   2346:        struct pool_allocator *pa = pp->pr_alloc;
                   2347:
                   2348:        (*pa->pa_free)(pp, v);
                   2349: }
                   2350:
                   2351: void *
1.124     yamt     2352: pool_page_alloc(struct pool *pp, int flags)
1.66      thorpej  2353: {
1.127     thorpej  2354:        bool waitok = (flags & PR_WAITOK) ? true : false;
1.66      thorpej  2355:
1.100     yamt     2356:        return ((void *) uvm_km_alloc_poolpage_cache(kmem_map, waitok));
1.66      thorpej  2357: }
                   2358:
                   2359: void
1.124     yamt     2360: pool_page_free(struct pool *pp, void *v)
1.66      thorpej  2361: {
                   2362:
1.98      yamt     2363:        uvm_km_free_poolpage_cache(kmem_map, (vaddr_t) v);
                   2364: }
                   2365:
                   2366: static void *
1.124     yamt     2367: pool_page_alloc_meta(struct pool *pp, int flags)
1.98      yamt     2368: {
1.127     thorpej  2369:        bool waitok = (flags & PR_WAITOK) ? true : false;
1.98      yamt     2370:
1.100     yamt     2371:        return ((void *) uvm_km_alloc_poolpage(kmem_map, waitok));
1.98      yamt     2372: }
                   2373:
                   2374: static void
1.124     yamt     2375: pool_page_free_meta(struct pool *pp, void *v)
1.98      yamt     2376: {
                   2377:
1.100     yamt     2378:        uvm_km_free_poolpage(kmem_map, (vaddr_t) v);
1.66      thorpej  2379: }
                   2380:
                   2381: #ifdef POOL_SUBPAGE
                   2382: /* Sub-page allocator, for machines with large hardware pages. */
                   2383: void *
                   2384: pool_subpage_alloc(struct pool *pp, int flags)
                   2385: {
1.128.2.2  ad       2386:        return pool_get(&psppool, flags);
1.66      thorpej  2387: }
                   2388:
                   2389: void
                   2390: pool_subpage_free(struct pool *pp, void *v)
                   2391: {
                   2392:        pool_put(&psppool, v);
                   2393: }
                   2394:
                   2395: /* We don't provide a real nointr allocator.  Maybe later. */
                   2396: void *
1.112     bjh21    2397: pool_subpage_alloc_nointr(struct pool *pp, int flags)
1.66      thorpej  2398: {
                   2399:
                   2400:        return (pool_subpage_alloc(pp, flags));
                   2401: }
                   2402:
                   2403: void
1.112     bjh21    2404: pool_subpage_free_nointr(struct pool *pp, void *v)
1.66      thorpej  2405: {
                   2406:
                   2407:        pool_subpage_free(pp, v);
                   2408: }
1.112     bjh21    2409: #endif /* POOL_SUBPAGE */
1.66      thorpej  2410: void *
1.124     yamt     2411: pool_page_alloc_nointr(struct pool *pp, int flags)
1.66      thorpej  2412: {
1.127     thorpej  2413:        bool waitok = (flags & PR_WAITOK) ? true : false;
1.66      thorpej  2414:
1.100     yamt     2415:        return ((void *) uvm_km_alloc_poolpage_cache(kernel_map, waitok));
1.66      thorpej  2416: }
                   2417:
                   2418: void
1.124     yamt     2419: pool_page_free_nointr(struct pool *pp, void *v)
1.66      thorpej  2420: {
                   2421:
1.98      yamt     2422:        uvm_km_free_poolpage_cache(kernel_map, (vaddr_t) v);
1.66      thorpej  2423: }

CVSweb <webmaster@jp.NetBSD.org>