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

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

CVSweb <webmaster@jp.NetBSD.org>