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

1.21.2.3! perry       1: /*     $NetBSD: subr_pool.c,v 1.21.2.2 1999/04/07 00:34:55 thorpej Exp $       */
1.1       pk          2:
                      3: /*-
1.20      thorpej     4:  * Copyright (c) 1997, 1999 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:  */
                     39:
                     40: #include <sys/param.h>
                     41: #include <sys/systm.h>
                     42: #include <sys/proc.h>
                     43: #include <sys/errno.h>
                     44: #include <sys/kernel.h>
                     45: #include <sys/malloc.h>
                     46: #include <sys/lock.h>
                     47: #include <sys/pool.h>
1.20      thorpej    48: #include <sys/syslog.h>
1.1       pk         49:
1.3       pk         50: #include <vm/vm.h>
                     51: #include <vm/vm_kern.h>
                     52:
                     53: #include <uvm/uvm.h>
                     54:
1.1       pk         55: /*
                     56:  * Pool resource management utility.
1.3       pk         57:  *
                     58:  * Memory is allocated in pages which are split into pieces according
                     59:  * to the pool item size. Each page is kept on a list headed by `pr_pagelist'
                     60:  * in the pool structure and the individual pool items are on a linked list
                     61:  * headed by `ph_itemlist' in each page header. The memory for building
                     62:  * the page list is either taken from the allocated pages themselves (for
                     63:  * small pool items) or taken from an internal pool of page headers (`phpool').
1.1       pk         64:  */
                     65:
1.3       pk         66: /* List of all pools */
1.5       thorpej    67: TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
1.3       pk         68:
                     69: /* Private pool for page header structures */
                     70: static struct pool phpool;
                     71:
                     72: /* # of seconds to retain page after last use */
                     73: int pool_inactive_time = 10;
                     74:
                     75: /* Next candidate for drainage (see pool_drain()) */
1.21.2.2  thorpej    76: static struct pool     *drainpp;
                     77:
                     78: /* This spin lock protects both pool_head and drainpp. */
                     79: struct simplelock pool_head_slock = SIMPLELOCK_INITIALIZER;
1.3       pk         80:
                     81: struct pool_item_header {
                     82:        /* Page headers */
                     83:        TAILQ_ENTRY(pool_item_header)
                     84:                                ph_pagelist;    /* pool page list */
                     85:        TAILQ_HEAD(,pool_item)  ph_itemlist;    /* chunk list for this page */
                     86:        LIST_ENTRY(pool_item_header)
                     87:                                ph_hashlist;    /* Off-page page headers */
                     88:        int                     ph_nmissing;    /* # of chunks in use */
                     89:        caddr_t                 ph_page;        /* this page's address */
                     90:        struct timeval          ph_time;        /* last referenced */
                     91: };
                     92:
1.1       pk         93: struct pool_item {
1.3       pk         94: #ifdef DIAGNOSTIC
                     95:        int pi_magic;
                     96: #define PI_MAGIC 0xdeadbeef
                     97: #endif
                     98:        /* Other entries use only this list entry */
                     99:        TAILQ_ENTRY(pool_item)  pi_list;
                    100: };
                    101:
                    102:
                    103: #define PR_HASH_INDEX(pp,addr) \
                    104:        (((u_long)(addr) >> (pp)->pr_pageshift) & (PR_HASHTABSIZE - 1))
                    105:
                    106:
                    107:
                    108: static struct pool_item_header
                    109:                *pr_find_pagehead __P((struct pool *, caddr_t));
                    110: static void    pr_rmpage __P((struct pool *, struct pool_item_header *));
1.20      thorpej   111: static int     pool_catchup __P((struct pool *));
1.21      thorpej   112: static void    pool_prime_page __P((struct pool *, caddr_t));
1.3       pk        113: static void    *pool_page_alloc __P((unsigned long, int, int));
                    114: static void    pool_page_free __P((void *, unsigned long, int));
                    115:
1.21      thorpej   116: #if defined(POOL_DIAGNOSTIC) || defined(DEBUG)
                    117: static void pool_print1 __P((struct pool *, const char *));
                    118: #endif
1.3       pk        119:
                    120: #ifdef POOL_DIAGNOSTIC
                    121: /*
                    122:  * Pool log entry. An array of these is allocated in pool_create().
                    123:  */
                    124: struct pool_log {
                    125:        const char      *pl_file;
                    126:        long            pl_line;
                    127:        int             pl_action;
                    128: #define PRLOG_GET      1
                    129: #define PRLOG_PUT      2
                    130:        void            *pl_addr;
1.1       pk        131: };
                    132:
1.3       pk        133: /* Number of entries in pool log buffers */
1.17      thorpej   134: #ifndef POOL_LOGSIZE
                    135: #define        POOL_LOGSIZE    10
                    136: #endif
                    137:
                    138: int pool_logsize = POOL_LOGSIZE;
1.1       pk        139:
1.3       pk        140: static void    pr_log __P((struct pool *, void *, int, const char *, long));
                    141: static void    pr_printlog __P((struct pool *));
                    142:
                    143: static __inline__ void
                    144: pr_log(pp, v, action, file, line)
                    145:        struct pool     *pp;
                    146:        void            *v;
                    147:        int             action;
                    148:        const char      *file;
                    149:        long            line;
                    150: {
                    151:        int n = pp->pr_curlogentry;
                    152:        struct pool_log *pl;
                    153:
1.20      thorpej   154:        if ((pp->pr_roflags & PR_LOGGING) == 0)
1.3       pk        155:                return;
                    156:
                    157:        /*
                    158:         * Fill in the current entry. Wrap around and overwrite
                    159:         * the oldest entry if necessary.
                    160:         */
                    161:        pl = &pp->pr_log[n];
                    162:        pl->pl_file = file;
                    163:        pl->pl_line = line;
                    164:        pl->pl_action = action;
                    165:        pl->pl_addr = v;
                    166:        if (++n >= pp->pr_logsize)
                    167:                n = 0;
                    168:        pp->pr_curlogentry = n;
                    169: }
                    170:
                    171: static void
                    172: pr_printlog(pp)
                    173:        struct pool *pp;
                    174: {
                    175:        int i = pp->pr_logsize;
                    176:        int n = pp->pr_curlogentry;
                    177:
1.20      thorpej   178:        if ((pp->pr_roflags & PR_LOGGING) == 0)
1.3       pk        179:                return;
                    180:
1.21      thorpej   181:        pool_print1(pp, "printlog");
1.3       pk        182:
                    183:        /*
                    184:         * Print all entries in this pool's log.
                    185:         */
                    186:        while (i-- > 0) {
                    187:                struct pool_log *pl = &pp->pr_log[n];
                    188:                if (pl->pl_action != 0) {
                    189:                        printf("log entry %d:\n", i);
                    190:                        printf("\taction = %s, addr = %p\n",
                    191:                                pl->pl_action == PRLOG_GET ? "get" : "put",
                    192:                                pl->pl_addr);
                    193:                        printf("\tfile: %s at line %lu\n",
                    194:                                pl->pl_file, pl->pl_line);
                    195:                }
                    196:                if (++n >= pp->pr_logsize)
                    197:                        n = 0;
                    198:        }
                    199: }
                    200: #else
                    201: #define pr_log(pp, v, action, file, line)
                    202: #define pr_printlog(pp)
                    203: #endif
                    204:
                    205:
                    206: /*
                    207:  * Return the pool page header based on page address.
                    208:  */
                    209: static __inline__ struct pool_item_header *
                    210: pr_find_pagehead(pp, page)
                    211:        struct pool *pp;
                    212:        caddr_t page;
                    213: {
                    214:        struct pool_item_header *ph;
                    215:
1.20      thorpej   216:        if ((pp->pr_roflags & PR_PHINPAGE) != 0)
1.3       pk        217:                return ((struct pool_item_header *)(page + pp->pr_phoffset));
                    218:
                    219:        for (ph = LIST_FIRST(&pp->pr_hashtab[PR_HASH_INDEX(pp, page)]);
                    220:             ph != NULL;
                    221:             ph = LIST_NEXT(ph, ph_hashlist)) {
                    222:                if (ph->ph_page == page)
                    223:                        return (ph);
                    224:        }
                    225:        return (NULL);
                    226: }
                    227:
                    228: /*
                    229:  * Remove a page from the pool.
                    230:  */
                    231: static __inline__ void
                    232: pr_rmpage(pp, ph)
                    233:        struct pool *pp;
                    234:        struct pool_item_header *ph;
                    235: {
                    236:
                    237:        /*
1.7       thorpej   238:         * If the page was idle, decrement the idle page count.
1.3       pk        239:         */
1.6       thorpej   240:        if (ph->ph_nmissing == 0) {
                    241: #ifdef DIAGNOSTIC
                    242:                if (pp->pr_nidle == 0)
                    243:                        panic("pr_rmpage: nidle inconsistent");
1.20      thorpej   244:                if (pp->pr_nitems < pp->pr_itemsperpage)
                    245:                        panic("pr_rmpage: nitems inconsistent");
1.6       thorpej   246: #endif
                    247:                pp->pr_nidle--;
                    248:        }
1.7       thorpej   249:
1.20      thorpej   250:        pp->pr_nitems -= pp->pr_itemsperpage;
                    251:
1.7       thorpej   252:        /*
                    253:         * Unlink a page from the pool and release it.
                    254:         */
                    255:        TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
                    256:        (*pp->pr_free)(ph->ph_page, pp->pr_pagesz, pp->pr_mtype);
                    257:        pp->pr_npages--;
                    258:        pp->pr_npagefree++;
1.6       thorpej   259:
1.21.2.1  chs       260:        if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
                    261:                LIST_REMOVE(ph, ph_hashlist);
                    262:                pool_put(&phpool, ph);
                    263:        }
                    264:
1.3       pk        265:        if (pp->pr_curpage == ph) {
                    266:                /*
                    267:                 * Find a new non-empty page header, if any.
                    268:                 * Start search from the page head, to increase the
                    269:                 * chance for "high water" pages to be freed.
                    270:                 */
                    271:                for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
                    272:                     ph = TAILQ_NEXT(ph, ph_pagelist))
                    273:                        if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
                    274:                                break;
                    275:
                    276:                pp->pr_curpage = ph;
1.21      thorpej   277:        }
1.3       pk        278: }
                    279:
                    280: /*
                    281:  * Allocate and initialize a pool.
                    282:  */
1.1       pk        283: struct pool *
1.3       pk        284: pool_create(size, align, ioff, nitems, wchan, pagesz, alloc, release, mtype)
1.1       pk        285:        size_t  size;
1.3       pk        286:        u_int   align;
                    287:        u_int   ioff;
1.1       pk        288:        int     nitems;
1.21      thorpej   289:        const char *wchan;
1.3       pk        290:        size_t  pagesz;
                    291:        void    *(*alloc) __P((unsigned long, int, int));
                    292:        void    (*release) __P((void *, unsigned long, int));
1.1       pk        293:        int     mtype;
                    294: {
                    295:        struct pool *pp;
1.3       pk        296:        int flags;
1.1       pk        297:
1.3       pk        298:        pp = (struct pool *)malloc(sizeof(*pp), M_POOL, M_NOWAIT);
                    299:        if (pp == NULL)
1.1       pk        300:                return (NULL);
1.3       pk        301:
                    302:        flags = PR_FREEHEADER;
                    303: #ifdef POOL_DIAGNOSTIC
                    304:        if (pool_logsize != 0)
                    305:                flags |= PR_LOGGING;
                    306: #endif
                    307:
                    308:        pool_init(pp, size, align, ioff, flags, wchan, pagesz,
                    309:                  alloc, release, mtype);
                    310:
                    311:        if (nitems != 0) {
                    312:                if (pool_prime(pp, nitems, NULL) != 0) {
                    313:                        pool_destroy(pp);
                    314:                        return (NULL);
                    315:                }
1.21.2.3! perry     316:                int s;
1.1       pk        317:        }
1.21.2.3! perry     318:                s = splhigh();
1.1       pk        319:
1.21.2.3! perry     320:                splx(s);
1.3       pk        321:        return (pp);
                    322: }
                    323:
                    324: /*
                    325:  * Initialize the given pool resource structure.
                    326:  *
                    327:  * We export this routine to allow other kernel parts to declare
                    328:  * static pools that must be initialized before malloc() is available.
                    329:  */
                    330: void
                    331: pool_init(pp, size, align, ioff, flags, wchan, pagesz, alloc, release, mtype)
                    332:        struct pool     *pp;
                    333:        size_t          size;
                    334:        u_int           align;
                    335:        u_int           ioff;
                    336:        int             flags;
1.21      thorpej   337:        const char      *wchan;
1.3       pk        338:        size_t          pagesz;
                    339:        void            *(*alloc) __P((unsigned long, int, int));
                    340:        void            (*release) __P((void *, unsigned long, int));
                    341:        int             mtype;
                    342: {
1.16      briggs    343:        int off, slack, i;
1.3       pk        344:
                    345:        /*
                    346:         * Check arguments and construct default values.
                    347:         */
                    348:        if (!powerof2(pagesz) || pagesz > PAGE_SIZE)
                    349:                panic("pool_init: page size invalid (%lx)\n", (u_long)pagesz);
                    350:
1.4       thorpej   351:        if (alloc == NULL && release == NULL) {
1.3       pk        352:                alloc = pool_page_alloc;
                    353:                release = pool_page_free;
1.4       thorpej   354:                pagesz = PAGE_SIZE;     /* Rounds to PAGE_SIZE anyhow. */
                    355:        } else if ((alloc != NULL && release != NULL) == 0) {
                    356:                /* If you specifiy one, must specify both. */
                    357:                panic("pool_init: must specify alloc and release together");
                    358:        }
                    359:
1.3       pk        360:        if (pagesz == 0)
                    361:                pagesz = PAGE_SIZE;
                    362:
                    363:        if (align == 0)
                    364:                align = ALIGN(1);
1.14      thorpej   365:
                    366:        if (size < sizeof(struct pool_item))
                    367:                size = sizeof(struct pool_item);
1.3       pk        368:
                    369:        /*
                    370:         * Initialize the pool structure.
                    371:         */
                    372:        TAILQ_INIT(&pp->pr_pagelist);
                    373:        pp->pr_curpage = NULL;
                    374:        pp->pr_npages = 0;
                    375:        pp->pr_minitems = 0;
                    376:        pp->pr_minpages = 0;
                    377:        pp->pr_maxpages = UINT_MAX;
1.20      thorpej   378:        pp->pr_roflags = flags;
                    379:        pp->pr_flags = 0;
1.3       pk        380:        pp->pr_size = ALIGN(size);
                    381:        pp->pr_align = align;
                    382:        pp->pr_wchan = wchan;
                    383:        pp->pr_mtype = mtype;
                    384:        pp->pr_alloc = alloc;
                    385:        pp->pr_free = release;
                    386:        pp->pr_pagesz = pagesz;
                    387:        pp->pr_pagemask = ~(pagesz - 1);
                    388:        pp->pr_pageshift = ffs(pagesz) - 1;
1.20      thorpej   389:        pp->pr_nitems = 0;
                    390:        pp->pr_nout = 0;
                    391:        pp->pr_hardlimit = UINT_MAX;
                    392:        pp->pr_hardlimit_warning = NULL;
                    393:        pp->pr_hardlimit_ratecap = 0;
                    394:        memset(&pp->pr_hardlimit_warning_last, 0,
                    395:            sizeof(pp->pr_hardlimit_warning_last));
1.3       pk        396:
                    397:        /*
                    398:         * Decide whether to put the page header off page to avoid
                    399:         * wasting too large a part of the page. Off-page page headers
                    400:         * go on a hash table, so we can match a returned item
                    401:         * with its header based on the page address.
                    402:         * We use 1/16 of the page size as the threshold (XXX: tune)
                    403:         */
                    404:        if (pp->pr_size < pagesz/16) {
                    405:                /* Use the end of the page for the page header */
1.20      thorpej   406:                pp->pr_roflags |= PR_PHINPAGE;
1.3       pk        407:                pp->pr_phoffset = off =
                    408:                        pagesz - ALIGN(sizeof(struct pool_item_header));
1.2       pk        409:        } else {
1.3       pk        410:                /* The page header will be taken from our page header pool */
                    411:                pp->pr_phoffset = 0;
                    412:                off = pagesz;
1.16      briggs    413:                for (i = 0; i < PR_HASHTABSIZE; i++) {
                    414:                        LIST_INIT(&pp->pr_hashtab[i]);
                    415:                }
1.2       pk        416:        }
1.1       pk        417:
1.3       pk        418:        /*
                    419:         * Alignment is to take place at `ioff' within the item. This means
                    420:         * we must reserve up to `align - 1' bytes on the page to allow
                    421:         * appropriate positioning of each item.
                    422:         *
                    423:         * Silently enforce `0 <= ioff < align'.
                    424:         */
                    425:        pp->pr_itemoffset = ioff = ioff % align;
                    426:        pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
                    427:
                    428:        /*
                    429:         * Use the slack between the chunks and the page header
                    430:         * for "cache coloring".
                    431:         */
                    432:        slack = off - pp->pr_itemsperpage * pp->pr_size;
                    433:        pp->pr_maxcolor = (slack / align) * align;
                    434:        pp->pr_curcolor = 0;
                    435:
                    436:        pp->pr_nget = 0;
                    437:        pp->pr_nfail = 0;
                    438:        pp->pr_nput = 0;
                    439:        pp->pr_npagealloc = 0;
                    440:        pp->pr_npagefree = 0;
1.1       pk        441:        pp->pr_hiwat = 0;
1.8       thorpej   442:        pp->pr_nidle = 0;
1.3       pk        443:
                    444: #ifdef POOL_DIAGNOSTIC
                    445:        if ((flags & PR_LOGGING) != 0) {
                    446:                pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
                    447:                                    M_TEMP, M_NOWAIT);
                    448:                if (pp->pr_log == NULL)
1.20      thorpej   449:                        pp->pr_roflags &= ~PR_LOGGING;
1.3       pk        450:                pp->pr_curlogentry = 0;
                    451:                pp->pr_logsize = pool_logsize;
                    452:        }
                    453: #endif
                    454:
1.21      thorpej   455:        simple_lock_init(&pp->pr_slock);
1.1       pk        456:
1.3       pk        457:        /*
                    458:         * Initialize private page header pool if we haven't done so yet.
1.21.2.2  thorpej   459:         * XXX LOCKING.
1.3       pk        460:         */
                    461:        if (phpool.pr_size == 0) {
                    462:                pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
                    463:                          0, "phpool", 0, 0, 0, 0);
1.1       pk        464:        }
                    465:
1.21.2.2  thorpej   466:        /* Insert into the list of all pools. */
                    467:        simple_lock(&pool_head_slock);
                    468:        TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
                    469:        simple_unlock(&pool_head_slock);
1.1       pk        470: }
                    471:
                    472: /*
                    473:  * De-commision a pool resource.
                    474:  */
                    475: void
                    476: pool_destroy(pp)
                    477:        struct pool *pp;
                    478: {
1.3       pk        479:        struct pool_item_header *ph;
                    480:
                    481: #ifdef DIAGNOSTIC
1.20      thorpej   482:        if (pp->pr_nout != 0) {
1.3       pk        483:                pr_printlog(pp);
1.20      thorpej   484:                panic("pool_destroy: pool busy: still out: %u\n",
                    485:                    pp->pr_nout);
1.3       pk        486:        }
                    487: #endif
1.1       pk        488:
1.3       pk        489:        /* Remove all pages */
1.20      thorpej   490:        if ((pp->pr_roflags & PR_STATIC) == 0)
1.3       pk        491:                while ((ph = pp->pr_pagelist.tqh_first) != NULL)
                    492:                        pr_rmpage(pp, ph);
                    493:
                    494:        /* Remove from global pool list */
1.21.2.2  thorpej   495:        simple_lock(&pool_head_slock);
1.3       pk        496:        TAILQ_REMOVE(&pool_head, pp, pr_poollist);
1.21.2.2  thorpej   497:        /* XXX Only clear this if we were drainpp? */
1.3       pk        498:        drainpp = NULL;
1.21.2.2  thorpej   499:        simple_unlock(&pool_head_slock);
1.3       pk        500:
                    501: #ifdef POOL_DIAGNOSTIC
1.20      thorpej   502:        if ((pp->pr_roflags & PR_LOGGING) != 0)
1.3       pk        503:                free(pp->pr_log, M_TEMP);
                    504: #endif
1.2       pk        505:
1.20      thorpej   506:        if (pp->pr_roflags & PR_FREEHEADER)
1.3       pk        507:                free(pp, M_POOL);
1.1       pk        508: }
                    509:
                    510:
                    511: /*
1.3       pk        512:  * Grab an item from the pool; must be called at appropriate spl level
1.1       pk        513:  */
1.3       pk        514: #ifdef POOL_DIAGNOSTIC
                    515: void *
                    516: _pool_get(pp, flags, file, line)
                    517:        struct pool *pp;
                    518:        int flags;
                    519:        const char *file;
                    520:        long line;
                    521: #else
1.1       pk        522: void *
                    523: pool_get(pp, flags)
                    524:        struct pool *pp;
                    525:        int flags;
1.3       pk        526: #endif
1.1       pk        527: {
                    528:        void *v;
                    529:        struct pool_item *pi;
1.3       pk        530:        struct pool_item_header *ph;
1.1       pk        531:
1.2       pk        532: #ifdef DIAGNOSTIC
1.20      thorpej   533:        if ((pp->pr_roflags & PR_STATIC) && (flags & PR_MALLOCOK)) {
1.3       pk        534:                pr_printlog(pp);
1.2       pk        535:                panic("pool_get: static");
1.3       pk        536:        }
1.2       pk        537: #endif
                    538:
1.3       pk        539:        if (curproc == NULL && (flags & PR_WAITOK) != 0)
                    540:                panic("pool_get: must have NOWAIT");
1.1       pk        541:
1.21      thorpej   542:        simple_lock(&pp->pr_slock);
1.20      thorpej   543:
                    544:  startover:
                    545:        /*
                    546:         * Check to see if we've reached the hard limit.  If we have,
                    547:         * and we can wait, then wait until an item has been returned to
                    548:         * the pool.
                    549:         */
                    550: #ifdef DIAGNOSTIC
                    551:        if (pp->pr_nout > pp->pr_hardlimit) {
1.21      thorpej   552:                simple_unlock(&pp->pr_slock);
1.20      thorpej   553:                panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
                    554:        }
                    555: #endif
                    556:        if (pp->pr_nout == pp->pr_hardlimit) {
                    557:                if (flags & PR_WAITOK) {
                    558:                        /*
                    559:                         * XXX: A warning isn't logged in this case.  Should
                    560:                         * it be?
                    561:                         */
                    562:                        pp->pr_flags |= PR_WANTED;
1.21      thorpej   563:                        simple_unlock(&pp->pr_slock);
1.20      thorpej   564:                        tsleep((caddr_t)pp, PSWP, pp->pr_wchan, 0);
1.21      thorpej   565:                        simple_lock(&pp->pr_slock);
1.20      thorpej   566:                        goto startover;
                    567:                }
                    568:                if (pp->pr_hardlimit_warning != NULL) {
                    569:                        /*
                    570:                         * Log a message that the hard limit has been hit.
                    571:                         */
                    572:                        struct timeval curtime, logdiff;
                    573:                        int s = splclock();
                    574:                        curtime = mono_time;
                    575:                        splx(s);
                    576:                        timersub(&curtime, &pp->pr_hardlimit_warning_last,
                    577:                            &logdiff);
                    578:                        if (logdiff.tv_sec >= pp->pr_hardlimit_ratecap) {
                    579:                                pp->pr_hardlimit_warning_last = curtime;
                    580:                                log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
                    581:                        }
                    582:                }
1.21      thorpej   583:
                    584:                if (flags & PR_URGENT)
                    585:                        panic("pool_get: urgent");
                    586:
                    587:                pp->pr_nfail++;
                    588:
                    589:                simple_unlock(&pp->pr_slock);
1.20      thorpej   590:                return (NULL);
                    591:        }
                    592:
1.3       pk        593:        /*
                    594:         * The convention we use is that if `curpage' is not NULL, then
                    595:         * it points at a non-empty bucket. In particular, `curpage'
                    596:         * never points at a page header which has PR_PHINPAGE set and
                    597:         * has no items in its bucket.
                    598:         */
1.20      thorpej   599:        if ((ph = pp->pr_curpage) == NULL) {
1.15      pk        600:                void *v;
                    601:
1.20      thorpej   602: #ifdef DIAGNOSTIC
                    603:                if (pp->pr_nitems != 0) {
1.21      thorpej   604:                        simple_unlock(&pp->pr_slock);
1.20      thorpej   605:                        printf("pool_get: %s: curpage NULL, nitems %u\n",
                    606:                            pp->pr_wchan, pp->pr_nitems);
                    607:                        panic("pool_get: nitems inconsistent\n");
                    608:                }
                    609: #endif
                    610:
1.21      thorpej   611:                /*
                    612:                 * Call the back-end page allocator for more memory.
                    613:                 * Release the pool lock, as the back-end page allocator
                    614:                 * may block.
                    615:                 */
                    616:                simple_unlock(&pp->pr_slock);
                    617:                v = (*pp->pr_alloc)(pp->pr_pagesz, flags, pp->pr_mtype);
                    618:                simple_lock(&pp->pr_slock);
1.15      pk        619:
1.21      thorpej   620:                if (v == NULL) {
                    621:                        /*
                    622:                         * We were unable to allocate a page, but
                    623:                         * we released the lock during allocation,
                    624:                         * so perhaps items were freed back to the
                    625:                         * pool.  Check for this case.
                    626:                         */
                    627:                        if (pp->pr_curpage != NULL)
                    628:                                goto startover;
1.15      pk        629:
1.3       pk        630:                        if (flags & PR_URGENT)
                    631:                                panic("pool_get: urgent");
1.21      thorpej   632:
1.3       pk        633:                        if ((flags & PR_WAITOK) == 0) {
                    634:                                pp->pr_nfail++;
1.21      thorpej   635:                                simple_unlock(&pp->pr_slock);
1.1       pk        636:                                return (NULL);
1.3       pk        637:                        }
                    638:
1.15      pk        639:                        /*
                    640:                         * Wait for items to be returned to this pool.
1.21      thorpej   641:                         *
1.15      pk        642:                         * XXX: we actually want to wait just until
                    643:                         * the page allocator has memory again. Depending
                    644:                         * on this pool's usage, we might get stuck here
                    645:                         * for a long time.
1.20      thorpej   646:                         *
                    647:                         * XXX: maybe we should wake up once a second and
                    648:                         * try again?
1.15      pk        649:                         */
1.1       pk        650:                        pp->pr_flags |= PR_WANTED;
1.21      thorpej   651:                        simple_unlock(&pp->pr_slock);
1.1       pk        652:                        tsleep((caddr_t)pp, PSWP, pp->pr_wchan, 0);
1.21      thorpej   653:                        simple_lock(&pp->pr_slock);
1.20      thorpej   654:                        goto startover;
1.1       pk        655:                }
1.3       pk        656:
1.15      pk        657:                /* We have more memory; add it to the pool */
                    658:                pp->pr_npagealloc++;
                    659:                pool_prime_page(pp, v);
                    660:
1.20      thorpej   661:                /* Start the allocation process over. */
                    662:                goto startover;
1.3       pk        663:        }
                    664:
1.21      thorpej   665:        if ((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL) {
                    666:                simple_unlock(&pp->pr_slock);
1.3       pk        667:                panic("pool_get: %s: page empty", pp->pr_wchan);
1.21      thorpej   668:        }
1.20      thorpej   669: #ifdef DIAGNOSTIC
                    670:        if (pp->pr_nitems == 0) {
1.21      thorpej   671:                simple_unlock(&pp->pr_slock);
1.20      thorpej   672:                printf("pool_get: %s: items on itemlist, nitems %u\n",
                    673:                    pp->pr_wchan, pp->pr_nitems);
                    674:                panic("pool_get: nitems inconsistent\n");
                    675:        }
                    676: #endif
1.3       pk        677:        pr_log(pp, v, PRLOG_GET, file, line);
                    678:
                    679: #ifdef DIAGNOSTIC
                    680:        if (pi->pi_magic != PI_MAGIC) {
                    681:                pr_printlog(pp);
                    682:                panic("pool_get(%s): free list modified: magic=%x; page %p;"
                    683:                       " item addr %p\n",
                    684:                        pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
                    685:        }
                    686: #endif
                    687:
                    688:        /*
                    689:         * Remove from item list.
                    690:         */
                    691:        TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
1.20      thorpej   692:        pp->pr_nitems--;
                    693:        pp->pr_nout++;
1.6       thorpej   694:        if (ph->ph_nmissing == 0) {
                    695: #ifdef DIAGNOSTIC
                    696:                if (pp->pr_nidle == 0)
                    697:                        panic("pool_get: nidle inconsistent");
                    698: #endif
                    699:                pp->pr_nidle--;
                    700:        }
1.3       pk        701:        ph->ph_nmissing++;
                    702:        if (TAILQ_FIRST(&ph->ph_itemlist) == NULL) {
1.21      thorpej   703: #ifdef DIAGNOSTIC
                    704:                if (ph->ph_nmissing != pp->pr_itemsperpage) {
                    705:                        simple_unlock(&pp->pr_slock);
                    706:                        panic("pool_get: %s: nmissing inconsistent",
                    707:                            pp->pr_wchan);
                    708:                }
                    709: #endif
1.3       pk        710:                /*
                    711:                 * Find a new non-empty page header, if any.
                    712:                 * Start search from the page head, to increase
                    713:                 * the chance for "high water" pages to be freed.
                    714:                 *
1.21      thorpej   715:                 * Migrate empty pages to the end of the list.  This
                    716:                 * will speed the update of curpage as pages become
                    717:                 * idle.  Empty pages intermingled with idle pages
                    718:                 * is no big deal.  As soon as a page becomes un-empty,
                    719:                 * it will move back to the head of the list.
1.3       pk        720:                 */
                    721:                TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
1.21      thorpej   722:                TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
                    723:                for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
                    724:                     ph = TAILQ_NEXT(ph, ph_pagelist))
1.3       pk        725:                        if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
                    726:                                break;
                    727:
                    728:                pp->pr_curpage = ph;
1.1       pk        729:        }
1.3       pk        730:
                    731:        pp->pr_nget++;
1.20      thorpej   732:
                    733:        /*
                    734:         * If we have a low water mark and we are now below that low
                    735:         * water mark, add more items to the pool.
                    736:         */
                    737:        if (pp->pr_nitems < pp->pr_minitems && pool_catchup(pp) != 0) {
                    738:                /*
                    739:                 * XXX: Should we log a warning?  Should we set up a timeout
                    740:                 * to try again in a second or so?  The latter could break
                    741:                 * a caller's assumptions about interrupt protection, etc.
                    742:                 */
                    743:        }
                    744:
1.21      thorpej   745:        simple_unlock(&pp->pr_slock);
1.1       pk        746:        return (v);
                    747: }
                    748:
                    749: /*
1.3       pk        750:  * Return resource to the pool; must be called at appropriate spl level
1.1       pk        751:  */
1.3       pk        752: #ifdef POOL_DIAGNOSTIC
                    753: void
                    754: _pool_put(pp, v, file, line)
                    755:        struct pool *pp;
                    756:        void *v;
                    757:        const char *file;
                    758:        long line;
                    759: #else
1.1       pk        760: void
                    761: pool_put(pp, v)
                    762:        struct pool *pp;
                    763:        void *v;
1.3       pk        764: #endif
1.1       pk        765: {
                    766:        struct pool_item *pi = v;
1.3       pk        767:        struct pool_item_header *ph;
                    768:        caddr_t page;
1.21      thorpej   769:        int s;
1.3       pk        770:
                    771:        page = (caddr_t)((u_long)v & pp->pr_pagemask);
1.1       pk        772:
1.21      thorpej   773:        simple_lock(&pp->pr_slock);
1.3       pk        774:
                    775:        pr_log(pp, v, PRLOG_PUT, file, line);
                    776:
                    777:        if ((ph = pr_find_pagehead(pp, page)) == NULL) {
                    778:                pr_printlog(pp);
                    779:                panic("pool_put: %s: page header missing", pp->pr_wchan);
                    780:        }
                    781:
                    782:        /*
                    783:         * Return to item list.
                    784:         */
1.2       pk        785: #ifdef DIAGNOSTIC
1.3       pk        786:        pi->pi_magic = PI_MAGIC;
                    787: #endif
                    788:        TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
                    789:        ph->ph_nmissing--;
                    790:        pp->pr_nput++;
1.20      thorpej   791:        pp->pr_nitems++;
                    792:        pp->pr_nout--;
1.3       pk        793:
                    794:        /* Cancel "pool empty" condition if it exists */
                    795:        if (pp->pr_curpage == NULL)
                    796:                pp->pr_curpage = ph;
                    797:
                    798:        if (pp->pr_flags & PR_WANTED) {
                    799:                pp->pr_flags &= ~PR_WANTED;
1.15      pk        800:                if (ph->ph_nmissing == 0)
                    801:                        pp->pr_nidle++;
1.21      thorpej   802:                simple_unlock(&pp->pr_slock);
1.3       pk        803:                wakeup((caddr_t)pp);
                    804:                return;
                    805:        }
                    806:
                    807:        /*
1.21      thorpej   808:         * If this page is now complete, do one of two things:
                    809:         *
                    810:         *      (1) If we have more pages than the page high water
                    811:         *          mark, free the page back to the system.
                    812:         *
                    813:         *      (2) Move it to the end of the page list, so that
                    814:         *          we minimize our chances of fragmenting the
                    815:         *          pool.  Idle pages migrate to the end (along with
                    816:         *          completely empty pages, so that we find un-empty
                    817:         *          pages more quickly when we update curpage) of the
                    818:         *          list so they can be more easily swept up by
                    819:         *          the pagedaemon when pages are scarce.
1.3       pk        820:         */
                    821:        if (ph->ph_nmissing == 0) {
1.6       thorpej   822:                pp->pr_nidle++;
1.3       pk        823:                if (pp->pr_npages > pp->pr_maxpages) {
                    824:                        pr_rmpage(pp, ph);
                    825:                } else {
                    826:                        TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
                    827:                        TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
                    828:
1.21      thorpej   829:                        /*
                    830:                         * Update the timestamp on the page.  A page must
                    831:                         * be idle for some period of time before it can
                    832:                         * be reclaimed by the pagedaemon.  This minimizes
                    833:                         * ping-pong'ing for memory.
                    834:                         */
                    835:                        s = splclock();
                    836:                        ph->ph_time = mono_time;
                    837:                        splx(s);
                    838:
                    839:                        /*
                    840:                         * Update the current page pointer.  Just look for
                    841:                         * the first page with any free items.
                    842:                         *
                    843:                         * XXX: Maybe we want an option to look for the
                    844:                         * page with the fewest available items, to minimize
                    845:                         * fragmentation?
                    846:                         */
1.3       pk        847:                        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
                    848:                             ph = TAILQ_NEXT(ph, ph_pagelist))
                    849:                                if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
                    850:                                        break;
1.1       pk        851:
1.3       pk        852:                        pp->pr_curpage = ph;
1.1       pk        853:                }
                    854:        }
1.21      thorpej   855:        /*
                    856:         * If the page has just become un-empty, move it to the head of
                    857:         * the list, and make it the current page.  The next allocation
                    858:         * will get the item from this page, instead of further fragmenting
                    859:         * the pool.
                    860:         */
                    861:        else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
                    862:                TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
                    863:                TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
                    864:                pp->pr_curpage = ph;
                    865:        }
                    866:
                    867:        simple_unlock(&pp->pr_slock);
1.3       pk        868:
1.1       pk        869: }
                    870:
                    871: /*
1.3       pk        872:  * Add N items to the pool.
1.1       pk        873:  */
                    874: int
1.2       pk        875: pool_prime(pp, n, storage)
1.1       pk        876:        struct pool *pp;
                    877:        int n;
1.2       pk        878:        caddr_t storage;
1.1       pk        879: {
1.3       pk        880:        caddr_t cp;
                    881:        int newnitems, newpages;
1.2       pk        882:
                    883: #ifdef DIAGNOSTIC
1.20      thorpej   884:        if (storage && !(pp->pr_roflags & PR_STATIC))
1.2       pk        885:                panic("pool_prime: static");
                    886:        /* !storage && static caught below */
                    887: #endif
1.1       pk        888:
1.21      thorpej   889:        simple_lock(&pp->pr_slock);
                    890:
1.3       pk        891:        newnitems = pp->pr_minitems + n;
                    892:        newpages =
1.18      thorpej   893:                roundup(newnitems, pp->pr_itemsperpage) / pp->pr_itemsperpage
1.3       pk        894:                - pp->pr_minpages;
                    895:
                    896:        while (newpages-- > 0) {
1.20      thorpej   897:                if (pp->pr_roflags & PR_STATIC) {
1.3       pk        898:                        cp = storage;
                    899:                        storage += pp->pr_pagesz;
                    900:                } else {
1.21      thorpej   901:                        simple_unlock(&pp->pr_slock);
1.3       pk        902:                        cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
1.21      thorpej   903:                        simple_lock(&pp->pr_slock);
1.3       pk        904:                }
1.2       pk        905:
1.3       pk        906:                if (cp == NULL) {
1.21      thorpej   907:                        simple_unlock(&pp->pr_slock);
1.1       pk        908:                        return (ENOMEM);
                    909:                }
                    910:
1.3       pk        911:                pool_prime_page(pp, cp);
                    912:                pp->pr_minpages++;
1.1       pk        913:        }
1.3       pk        914:
                    915:        pp->pr_minitems = newnitems;
                    916:
                    917:        if (pp->pr_minpages >= pp->pr_maxpages)
                    918:                pp->pr_maxpages = pp->pr_minpages + 1;  /* XXX */
                    919:
1.21      thorpej   920:        simple_unlock(&pp->pr_slock);
1.1       pk        921:        return (0);
                    922: }
1.3       pk        923:
                    924: /*
                    925:  * Add a page worth of items to the pool.
1.21      thorpej   926:  *
                    927:  * Note, we must be called with the pool descriptor LOCKED.
1.3       pk        928:  */
1.21      thorpej   929: static void
1.3       pk        930: pool_prime_page(pp, storage)
                    931:        struct pool *pp;
                    932:        caddr_t storage;
                    933: {
                    934:        struct pool_item *pi;
                    935:        struct pool_item_header *ph;
                    936:        caddr_t cp = storage;
                    937:        unsigned int align = pp->pr_align;
                    938:        unsigned int ioff = pp->pr_itemoffset;
                    939:        int n;
                    940:
1.20      thorpej   941:        if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
1.3       pk        942:                ph = (struct pool_item_header *)(cp + pp->pr_phoffset);
                    943:        } else {
                    944:                ph = pool_get(&phpool, PR_URGENT);
                    945:                LIST_INSERT_HEAD(&pp->pr_hashtab[PR_HASH_INDEX(pp, cp)],
                    946:                                 ph, ph_hashlist);
                    947:        }
                    948:
                    949:        /*
                    950:         * Insert page header.
                    951:         */
                    952:        TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
                    953:        TAILQ_INIT(&ph->ph_itemlist);
                    954:        ph->ph_page = storage;
                    955:        ph->ph_nmissing = 0;
1.21      thorpej   956:        memset(&ph->ph_time, 0, sizeof(ph->ph_time));
1.3       pk        957:
1.6       thorpej   958:        pp->pr_nidle++;
                    959:
1.3       pk        960:        /*
                    961:         * Color this page.
                    962:         */
                    963:        cp = (caddr_t)(cp + pp->pr_curcolor);
                    964:        if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
                    965:                pp->pr_curcolor = 0;
                    966:
                    967:        /*
                    968:         * Adjust storage to apply aligment to `pr_itemoffset' in each item.
                    969:         */
                    970:        if (ioff != 0)
                    971:                cp = (caddr_t)(cp + (align - ioff));
                    972:
                    973:        /*
                    974:         * Insert remaining chunks on the bucket list.
                    975:         */
                    976:        n = pp->pr_itemsperpage;
1.20      thorpej   977:        pp->pr_nitems += n;
1.3       pk        978:
                    979:        while (n--) {
                    980:                pi = (struct pool_item *)cp;
                    981:
                    982:                /* Insert on page list */
                    983:                TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
                    984: #ifdef DIAGNOSTIC
                    985:                pi->pi_magic = PI_MAGIC;
                    986: #endif
                    987:                cp = (caddr_t)(cp + pp->pr_size);
                    988:        }
                    989:
                    990:        /*
                    991:         * If the pool was depleted, point at the new page.
                    992:         */
                    993:        if (pp->pr_curpage == NULL)
                    994:                pp->pr_curpage = ph;
                    995:
                    996:        if (++pp->pr_npages > pp->pr_hiwat)
                    997:                pp->pr_hiwat = pp->pr_npages;
                    998: }
                    999:
1.20      thorpej  1000: /*
                   1001:  * Like pool_prime(), except this is used by pool_get() when nitems
                   1002:  * drops below the low water mark.  This is used to catch up nitmes
                   1003:  * with the low water mark.
                   1004:  *
1.21      thorpej  1005:  * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20      thorpej  1006:  *
                   1007:  * Note 2, this doesn't work with static pools.
                   1008:  *
                   1009:  * Note 3, we must be called with the pool already locked, and we return
                   1010:  * with it locked.
                   1011:  */
                   1012: static int
                   1013: pool_catchup(pp)
                   1014:        struct pool *pp;
                   1015: {
                   1016:        caddr_t cp;
                   1017:        int error = 0;
                   1018:
                   1019:        if (pp->pr_roflags & PR_STATIC) {
                   1020:                /*
                   1021:                 * We dropped below the low water mark, and this is not a
                   1022:                 * good thing.  Log a warning.
1.21      thorpej  1023:                 *
                   1024:                 * XXX: rate-limit this?
1.20      thorpej  1025:                 */
                   1026:                printf("WARNING: static pool `%s' dropped below low water "
                   1027:                    "mark\n", pp->pr_wchan);
                   1028:                return (0);
                   1029:        }
                   1030:
1.21      thorpej  1031:        while (pp->pr_nitems < pp->pr_minitems) {
1.20      thorpej  1032:                /*
1.21      thorpej  1033:                 * Call the page back-end allocator for more memory.
                   1034:                 *
                   1035:                 * XXX: We never wait, so should we bother unlocking
                   1036:                 * the pool descriptor?
1.20      thorpej  1037:                 */
1.21      thorpej  1038:                simple_unlock(&pp->pr_slock);
1.20      thorpej  1039:                cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
1.21      thorpej  1040:                simple_lock(&pp->pr_slock);
1.20      thorpej  1041:                if (cp == NULL) {
                   1042:                        error = ENOMEM;
                   1043:                        break;
                   1044:                }
                   1045:                pool_prime_page(pp, cp);
                   1046:        }
                   1047:
                   1048:        return (error);
                   1049: }
                   1050:
1.3       pk       1051: void
                   1052: pool_setlowat(pp, n)
                   1053:        pool_handle_t   pp;
1.21.2.3! perry    1054:        int s, n;
1.3       pk       1055: {
1.20      thorpej  1056:        int error;
1.15      pk       1057:
1.21      thorpej  1058:        simple_lock(&pp->pr_slock);
1.21.2.3! perry    1059:                s = splhigh();
1.21      thorpej  1060:
1.21.2.3! perry    1061:                splx(s);
1.3       pk       1062:        pp->pr_minitems = n;
1.15      pk       1063:        pp->pr_minpages = (n == 0)
                   1064:                ? 0
1.18      thorpej  1065:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20      thorpej  1066:
                   1067:        /* Make sure we're caught up with the newly-set low water mark. */
1.21      thorpej  1068:        if ((error = pool_catchup(pp)) != 0) {
1.20      thorpej  1069:                /*
                   1070:                 * XXX: Should we log a warning?  Should we set up a timeout
                   1071:                 * to try again in a second or so?  The latter could break
                   1072:                 * a caller's assumptions about interrupt protection, etc.
                   1073:                 */
                   1074:        }
1.21      thorpej  1075:
                   1076:        simple_unlock(&pp->pr_slock);
1.3       pk       1077: }
                   1078:
                   1079: void
                   1080: pool_sethiwat(pp, n)
                   1081:        pool_handle_t   pp;
                   1082:        int n;
                   1083: {
1.15      pk       1084:
1.21      thorpej  1085:        simple_lock(&pp->pr_slock);
                   1086:
1.15      pk       1087:        pp->pr_maxpages = (n == 0)
                   1088:                ? 0
1.18      thorpej  1089:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21      thorpej  1090:
                   1091:        simple_unlock(&pp->pr_slock);
1.3       pk       1092: }
                   1093:
1.20      thorpej  1094: void
                   1095: pool_sethardlimit(pp, n, warnmess, ratecap)
                   1096:        pool_handle_t pp;
                   1097:        int n;
                   1098:        const char *warnmess;
                   1099:        int ratecap;
                   1100: {
                   1101:
1.21      thorpej  1102:        simple_lock(&pp->pr_slock);
1.20      thorpej  1103:
                   1104:        pp->pr_hardlimit = n;
                   1105:        pp->pr_hardlimit_warning = warnmess;
                   1106:        pp->pr_hardlimit_ratecap = ratecap;
                   1107:        memset(&pp->pr_hardlimit_warning_last, 0,
                   1108:            sizeof(pp->pr_hardlimit_warning_last));
                   1109:
                   1110:        /*
1.21      thorpej  1111:         * In-line version of pool_sethiwat(), because we don't want to
                   1112:         * release the lock.
1.20      thorpej  1113:         */
                   1114:        pp->pr_maxpages = (n == 0)
                   1115:                ? 0
                   1116:                : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21      thorpej  1117:
                   1118:        simple_unlock(&pp->pr_slock);
1.20      thorpej  1119: }
1.3       pk       1120:
                   1121: /*
                   1122:  * Default page allocator.
                   1123:  */
                   1124: static void *
                   1125: pool_page_alloc(sz, flags, mtype)
                   1126:        unsigned long sz;
                   1127:        int flags;
                   1128:        int mtype;
                   1129: {
1.11      thorpej  1130:        boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1.3       pk       1131:
1.11      thorpej  1132:        return ((void *)uvm_km_alloc_poolpage(waitok));
1.3       pk       1133: }
                   1134:
                   1135: static void
                   1136: pool_page_free(v, sz, mtype)
                   1137:        void *v;
                   1138:        unsigned long sz;
                   1139:        int mtype;
                   1140: {
                   1141:
1.10      eeh      1142:        uvm_km_free_poolpage((vaddr_t)v);
1.3       pk       1143: }
1.12      thorpej  1144:
                   1145: /*
                   1146:  * Alternate pool page allocator for pools that know they will
                   1147:  * never be accessed in interrupt context.
                   1148:  */
                   1149: void *
                   1150: pool_page_alloc_nointr(sz, flags, mtype)
                   1151:        unsigned long sz;
                   1152:        int flags;
                   1153:        int mtype;
                   1154: {
                   1155:        boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
                   1156:
                   1157:        return ((void *)uvm_km_alloc_poolpage1(kernel_map, uvm.kernel_object,
                   1158:            waitok));
                   1159: }
                   1160:
                   1161: void
                   1162: pool_page_free_nointr(v, sz, mtype)
                   1163:        void *v;
                   1164:        unsigned long sz;
                   1165:        int mtype;
                   1166: {
                   1167:
                   1168:        uvm_km_free_poolpage1(kernel_map, (vaddr_t)v);
                   1169: }
                   1170:
1.3       pk       1171:
                   1172: /*
                   1173:  * Release all complete pages that have not been used recently.
                   1174:  */
                   1175: void
1.21      thorpej  1176: pool_reclaim(pp)
1.3       pk       1177:        pool_handle_t pp;
                   1178: {
                   1179:        struct pool_item_header *ph, *phnext;
1.21      thorpej  1180:        struct timeval curtime;
                   1181:        int s;
1.3       pk       1182:
1.20      thorpej  1183:        if (pp->pr_roflags & PR_STATIC)
1.3       pk       1184:                return;
                   1185:
1.21      thorpej  1186:        if (simple_lock_try(&pp->pr_slock) == 0)
1.3       pk       1187:                return;
                   1188:
1.21      thorpej  1189:        s = splclock();
                   1190:        curtime = mono_time;
                   1191:        splx(s);
                   1192:
1.3       pk       1193:        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL; ph = phnext) {
                   1194:                phnext = TAILQ_NEXT(ph, ph_pagelist);
                   1195:
                   1196:                /* Check our minimum page claim */
                   1197:                if (pp->pr_npages <= pp->pr_minpages)
                   1198:                        break;
                   1199:
                   1200:                if (ph->ph_nmissing == 0) {
                   1201:                        struct timeval diff;
                   1202:                        timersub(&curtime, &ph->ph_time, &diff);
                   1203:                        if (diff.tv_sec < pool_inactive_time)
                   1204:                                continue;
1.21      thorpej  1205:
                   1206:                        /*
                   1207:                         * If freeing this page would put us below
                   1208:                         * the low water mark, stop now.
                   1209:                         */
                   1210:                        if ((pp->pr_nitems - pp->pr_itemsperpage) <
                   1211:                            pp->pr_minitems)
                   1212:                                break;
                   1213:
1.3       pk       1214:                        pr_rmpage(pp, ph);
                   1215:                }
                   1216:        }
                   1217:
1.21      thorpej  1218:        simple_unlock(&pp->pr_slock);
1.3       pk       1219: }
                   1220:
                   1221:
                   1222: /*
                   1223:  * Drain pools, one at a time.
1.21      thorpej  1224:  *
                   1225:  * Note, we must never be called from an interrupt context.
1.3       pk       1226:  */
                   1227: void
                   1228: pool_drain(arg)
                   1229:        void *arg;
                   1230: {
                   1231:        struct pool *pp;
1.21.2.2  thorpej  1232:        int s;
1.3       pk       1233:
1.21.2.2  thorpej  1234:        s = splimp();
                   1235:        simple_lock(&pool_head_slock);
                   1236:
                   1237:        if (drainpp == NULL && (drainpp = TAILQ_FIRST(&pool_head)) == NULL)
                   1238:                goto out;
1.3       pk       1239:
                   1240:        pp = drainpp;
                   1241:        drainpp = TAILQ_NEXT(pp, pr_poollist);
                   1242:
                   1243:        pool_reclaim(pp);
1.21.2.2  thorpej  1244:
                   1245:  out:
                   1246:        simple_unlock(&pool_head_slock);
1.3       pk       1247:        splx(s);
                   1248: }
                   1249:
                   1250:
1.17      thorpej  1251: #if defined(POOL_DIAGNOSTIC) || defined(DEBUG)
1.3       pk       1252: /*
                   1253:  * Diagnostic helpers.
                   1254:  */
                   1255: void
                   1256: pool_print(pp, label)
                   1257:        struct pool *pp;
1.21      thorpej  1258:        const char *label;
                   1259: {
                   1260:        int s;
                   1261:
                   1262:        s = splimp();
                   1263:        simple_lock(&pp->pr_slock);
                   1264:        pool_print1(pp, label);
                   1265:        simple_unlock(&pp->pr_slock);
                   1266:        splx(s);
                   1267: }
                   1268:
                   1269: static void
                   1270: pool_print1(pp, label)
                   1271:        struct pool *pp;
                   1272:        const char *label;
1.3       pk       1273: {
                   1274:
                   1275:        if (label != NULL)
                   1276:                printf("%s: ", label);
                   1277:
                   1278:        printf("pool %s: nalloc %lu nfree %lu npagealloc %lu npagefree %lu\n"
1.6       thorpej  1279:               "         npages %u minitems %u itemsperpage %u itemoffset %u\n"
                   1280:               "         nidle %lu\n",
1.3       pk       1281:                pp->pr_wchan,
                   1282:                pp->pr_nget,
                   1283:                pp->pr_nput,
                   1284:                pp->pr_npagealloc,
                   1285:                pp->pr_npagefree,
                   1286:                pp->pr_npages,
                   1287:                pp->pr_minitems,
                   1288:                pp->pr_itemsperpage,
1.6       thorpej  1289:                pp->pr_itemoffset,
                   1290:                pp->pr_nidle);
1.3       pk       1291: }
                   1292:
                   1293: int
                   1294: pool_chk(pp, label)
                   1295:        struct pool *pp;
                   1296:        char *label;
                   1297: {
                   1298:        struct pool_item_header *ph;
                   1299:        int r = 0;
                   1300:
1.21      thorpej  1301:        simple_lock(&pp->pr_slock);
1.3       pk       1302:
                   1303:        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
                   1304:             ph = TAILQ_NEXT(ph, ph_pagelist)) {
                   1305:
                   1306:                struct pool_item *pi;
                   1307:                int n;
                   1308:                caddr_t page;
                   1309:
                   1310:                page = (caddr_t)((u_long)ph & pp->pr_pagemask);
1.20      thorpej  1311:                if (page != ph->ph_page &&
                   1312:                    (pp->pr_roflags & PR_PHINPAGE) != 0) {
1.3       pk       1313:                        if (label != NULL)
                   1314:                                printf("%s: ", label);
1.16      briggs   1315:                        printf("pool(%p:%s): page inconsistency: page %p;"
                   1316:                               " at page head addr %p (p %p)\n", pp,
1.3       pk       1317:                                pp->pr_wchan, ph->ph_page,
                   1318:                                ph, page);
                   1319:                        r++;
                   1320:                        goto out;
                   1321:                }
                   1322:
                   1323:                for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
                   1324:                     pi != NULL;
                   1325:                     pi = TAILQ_NEXT(pi,pi_list), n++) {
                   1326:
                   1327: #ifdef DIAGNOSTIC
                   1328:                        if (pi->pi_magic != PI_MAGIC) {
                   1329:                                if (label != NULL)
                   1330:                                        printf("%s: ", label);
                   1331:                                printf("pool(%s): free list modified: magic=%x;"
                   1332:                                       " page %p; item ordinal %d;"
                   1333:                                       " addr %p (p %p)\n",
                   1334:                                        pp->pr_wchan, pi->pi_magic, ph->ph_page,
                   1335:                                        n, pi, page);
                   1336:                                panic("pool");
                   1337:                        }
                   1338: #endif
                   1339:                        page = (caddr_t)((u_long)pi & pp->pr_pagemask);
                   1340:                        if (page == ph->ph_page)
                   1341:                                continue;
                   1342:
                   1343:                        if (label != NULL)
                   1344:                                printf("%s: ", label);
1.16      briggs   1345:                        printf("pool(%p:%s): page inconsistency: page %p;"
                   1346:                               " item ordinal %d; addr %p (p %p)\n", pp,
1.3       pk       1347:                                pp->pr_wchan, ph->ph_page,
                   1348:                                n, pi, page);
                   1349:                        r++;
                   1350:                        goto out;
                   1351:                }
                   1352:        }
                   1353: out:
1.21      thorpej  1354:        simple_unlock(&pp->pr_slock);
1.3       pk       1355:        return (r);
                   1356: }
1.17      thorpej  1357: #endif /* POOL_DIAGNOSTIC || DEBUG */

CVSweb <webmaster@jp.NetBSD.org>