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

1.3     ! pk          1: /*     $NetBSD: subr_pool.c,v 1.2 1998/02/19 23:52:14 pk Exp $ */
1.1       pk          2:
                      3: /*-
                      4:  * Copyright (c) 1997 The NetBSD Foundation, Inc.
                      5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Paul Kranenburg.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *        This product includes software developed by the NetBSD
                     21:  *        Foundation, Inc. and its contributors.
                     22:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     23:  *    contributors may be used to endorse or promote products derived
                     24:  *    from this software without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     27:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     28:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     29:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     30:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     31:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     32:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     33:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     34:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     35:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     36:  * POSSIBILITY OF SUCH DAMAGE.
                     37:  */
                     38:
                     39: #include <sys/param.h>
                     40: #include <sys/systm.h>
                     41: #include <sys/proc.h>
                     42: #include <sys/errno.h>
                     43: #include <sys/kernel.h>
                     44: #include <sys/malloc.h>
                     45: #include <sys/lock.h>
                     46: #include <sys/pool.h>
                     47:
1.3     ! pk         48: #include <vm/vm.h>
        !            49: #include <vm/vm_kern.h>
        !            50:
        !            51: #if defined(UVM)
        !            52: #include <uvm/uvm.h>
        !            53: #endif
        !            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').
        !            64:  *
1.1       pk         65:  */
                     66:
1.3     ! pk         67: /* List of all pools */
        !            68: static TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
        !            69:
        !            70: /* Private pool for page header structures */
        !            71: static struct pool phpool;
        !            72:
        !            73: /* # of seconds to retain page after last use */
        !            74: int pool_inactive_time = 10;
        !            75:
        !            76: /* Next candidate for drainage (see pool_drain()) */
        !            77: static struct pool     *drainpp = NULL;
        !            78:
        !            79: struct pool_item_header {
        !            80:        /* Page headers */
        !            81:        TAILQ_ENTRY(pool_item_header)
        !            82:                                ph_pagelist;    /* pool page list */
        !            83:        TAILQ_HEAD(,pool_item)  ph_itemlist;    /* chunk list for this page */
        !            84:        LIST_ENTRY(pool_item_header)
        !            85:                                ph_hashlist;    /* Off-page page headers */
        !            86:        int                     ph_nmissing;    /* # of chunks in use */
        !            87:        caddr_t                 ph_page;        /* this page's address */
        !            88:        struct timeval          ph_time;        /* last referenced */
        !            89: };
        !            90:
1.1       pk         91: struct pool_item {
1.3     ! pk         92: #ifdef DIAGNOSTIC
        !            93:        int pi_magic;
        !            94: #define PI_MAGIC 0xdeadbeef
        !            95: #endif
        !            96:        /* Other entries use only this list entry */
        !            97:        TAILQ_ENTRY(pool_item)  pi_list;
        !            98: };
        !            99:
        !           100:
        !           101: #define PR_HASH_INDEX(pp,addr) \
        !           102:        (((u_long)(addr) >> (pp)->pr_pageshift) & (PR_HASHTABSIZE - 1))
        !           103:
        !           104:
        !           105:
        !           106: static struct pool_item_header
        !           107:                *pr_find_pagehead __P((struct pool *, caddr_t));
        !           108: static void    pr_rmpage __P((struct pool *, struct pool_item_header *));
        !           109: static int     pool_prime_page __P((struct pool *, caddr_t));
        !           110: static void    *pool_page_alloc __P((unsigned long, int, int));
        !           111: static void    pool_page_free __P((void *, unsigned long, int));
        !           112: int pool_chk __P((struct pool *, char *));
        !           113:
        !           114:
        !           115: #ifdef POOL_DIAGNOSTIC
        !           116: /*
        !           117:  * Pool log entry. An array of these is allocated in pool_create().
        !           118:  */
        !           119: struct pool_log {
        !           120:        const char      *pl_file;
        !           121:        long            pl_line;
        !           122:        int             pl_action;
        !           123: #define PRLOG_GET      1
        !           124: #define PRLOG_PUT      2
        !           125:        void            *pl_addr;
1.1       pk        126: };
                    127:
1.3     ! pk        128: /* Number of entries in pool log buffers */
        !           129: int pool_logsize = 10;
1.1       pk        130:
1.3     ! pk        131: static void    pr_log __P((struct pool *, void *, int, const char *, long));
        !           132: static void    pr_printlog __P((struct pool *));
        !           133:
        !           134: static __inline__ void
        !           135: pr_log(pp, v, action, file, line)
        !           136:        struct pool     *pp;
        !           137:        void            *v;
        !           138:        int             action;
        !           139:        const char      *file;
        !           140:        long            line;
        !           141: {
        !           142:        int n = pp->pr_curlogentry;
        !           143:        struct pool_log *pl;
        !           144:
        !           145:        if ((pp->pr_flags & PR_LOGGING) == 0)
        !           146:                return;
        !           147:
        !           148:        /*
        !           149:         * Fill in the current entry. Wrap around and overwrite
        !           150:         * the oldest entry if necessary.
        !           151:         */
        !           152:        pl = &pp->pr_log[n];
        !           153:        pl->pl_file = file;
        !           154:        pl->pl_line = line;
        !           155:        pl->pl_action = action;
        !           156:        pl->pl_addr = v;
        !           157:        if (++n >= pp->pr_logsize)
        !           158:                n = 0;
        !           159:        pp->pr_curlogentry = n;
        !           160: }
        !           161:
        !           162: static void
        !           163: pr_printlog(pp)
        !           164:        struct pool *pp;
        !           165: {
        !           166:        int i = pp->pr_logsize;
        !           167:        int n = pp->pr_curlogentry;
        !           168:
        !           169:        if ((pp->pr_flags & PR_LOGGING) == 0)
        !           170:                return;
        !           171:
        !           172:        pool_print(pp, "printlog");
        !           173:
        !           174:        /*
        !           175:         * Print all entries in this pool's log.
        !           176:         */
        !           177:        while (i-- > 0) {
        !           178:                struct pool_log *pl = &pp->pr_log[n];
        !           179:                if (pl->pl_action != 0) {
        !           180:                        printf("log entry %d:\n", i);
        !           181:                        printf("\taction = %s, addr = %p\n",
        !           182:                                pl->pl_action == PRLOG_GET ? "get" : "put",
        !           183:                                pl->pl_addr);
        !           184:                        printf("\tfile: %s at line %lu\n",
        !           185:                                pl->pl_file, pl->pl_line);
        !           186:                }
        !           187:                if (++n >= pp->pr_logsize)
        !           188:                        n = 0;
        !           189:        }
        !           190: }
        !           191: #else
        !           192: #define pr_log(pp, v, action, file, line)
        !           193: #define pr_printlog(pp)
        !           194: #endif
        !           195:
        !           196:
        !           197: /*
        !           198:  * Return the pool page header based on page address.
        !           199:  */
        !           200: static __inline__ struct pool_item_header *
        !           201: pr_find_pagehead(pp, page)
        !           202:        struct pool *pp;
        !           203:        caddr_t page;
        !           204: {
        !           205:        struct pool_item_header *ph;
        !           206:
        !           207:        if ((pp->pr_flags & PR_PHINPAGE) != 0)
        !           208:                return ((struct pool_item_header *)(page + pp->pr_phoffset));
        !           209:
        !           210:        for (ph = LIST_FIRST(&pp->pr_hashtab[PR_HASH_INDEX(pp, page)]);
        !           211:             ph != NULL;
        !           212:             ph = LIST_NEXT(ph, ph_hashlist)) {
        !           213:                if (ph->ph_page == page)
        !           214:                        return (ph);
        !           215:        }
        !           216:        return (NULL);
        !           217: }
        !           218:
        !           219: /*
        !           220:  * Remove a page from the pool.
        !           221:  */
        !           222: static __inline__ void
        !           223: pr_rmpage(pp, ph)
        !           224:        struct pool *pp;
        !           225:        struct pool_item_header *ph;
        !           226: {
        !           227:
        !           228:        /*
        !           229:         * Unlink a page from the pool and release it.
        !           230:         */
        !           231:        TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
        !           232:        (*pp->pr_free)(ph->ph_page, pp->pr_pagesz, pp->pr_mtype);
        !           233:        pp->pr_npages--;
        !           234:        pp->pr_npagefree++;
        !           235:
        !           236:        if ((pp->pr_flags & PR_PHINPAGE) == 0) {
        !           237:                LIST_REMOVE(ph, ph_hashlist);
        !           238:                pool_put(&phpool, ph);
        !           239:        }
        !           240:
        !           241:        if (pp->pr_curpage == ph) {
        !           242:                /*
        !           243:                 * Find a new non-empty page header, if any.
        !           244:                 * Start search from the page head, to increase the
        !           245:                 * chance for "high water" pages to be freed.
        !           246:                 */
        !           247:                for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
        !           248:                     ph = TAILQ_NEXT(ph, ph_pagelist))
        !           249:                        if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
        !           250:                                break;
        !           251:
        !           252:                pp->pr_curpage = ph;
        !           253:        }
        !           254: }
        !           255:
        !           256: /*
        !           257:  * Allocate and initialize a pool.
        !           258:  */
1.1       pk        259: struct pool *
1.3     ! pk        260: pool_create(size, align, ioff, nitems, wchan, pagesz, alloc, release, mtype)
1.1       pk        261:        size_t  size;
1.3     ! pk        262:        u_int   align;
        !           263:        u_int   ioff;
1.1       pk        264:        int     nitems;
                    265:        char    *wchan;
1.3     ! pk        266:        size_t  pagesz;
        !           267:        void    *(*alloc) __P((unsigned long, int, int));
        !           268:        void    (*release) __P((void *, unsigned long, int));
1.1       pk        269:        int     mtype;
                    270: {
                    271:        struct pool *pp;
1.3     ! pk        272:        int flags;
1.1       pk        273:
1.3     ! pk        274:        pp = (struct pool *)malloc(sizeof(*pp), M_POOL, M_NOWAIT);
        !           275:        if (pp == NULL)
1.1       pk        276:                return (NULL);
1.3     ! pk        277:
        !           278:        flags = PR_FREEHEADER;
        !           279: #ifdef POOL_DIAGNOSTIC
        !           280:        if (pool_logsize != 0)
        !           281:                flags |= PR_LOGGING;
        !           282: #endif
        !           283:
        !           284:        pool_init(pp, size, align, ioff, flags, wchan, pagesz,
        !           285:                  alloc, release, mtype);
        !           286:
        !           287:        if (nitems != 0) {
        !           288:                if (pool_prime(pp, nitems, NULL) != 0) {
        !           289:                        pool_destroy(pp);
        !           290:                        return (NULL);
        !           291:                }
1.1       pk        292:        }
                    293:
1.3     ! pk        294:        return (pp);
        !           295: }
        !           296:
        !           297: /*
        !           298:  * Initialize the given pool resource structure.
        !           299:  *
        !           300:  * We export this routine to allow other kernel parts to declare
        !           301:  * static pools that must be initialized before malloc() is available.
        !           302:  */
        !           303: void
        !           304: pool_init(pp, size, align, ioff, flags, wchan, pagesz, alloc, release, mtype)
        !           305:        struct pool     *pp;
        !           306:        size_t          size;
        !           307:        u_int           align;
        !           308:        u_int           ioff;
        !           309:        int             flags;
        !           310:        char            *wchan;
        !           311:        size_t          pagesz;
        !           312:        void            *(*alloc) __P((unsigned long, int, int));
        !           313:        void            (*release) __P((void *, unsigned long, int));
        !           314:        int             mtype;
        !           315: {
        !           316:        int off, slack;
        !           317:
        !           318:        /*
        !           319:         * Check arguments and construct default values.
        !           320:         */
        !           321:        if (!powerof2(pagesz) || pagesz > PAGE_SIZE)
        !           322:                panic("pool_init: page size invalid (%lx)\n", (u_long)pagesz);
        !           323:
        !           324:        if (alloc == NULL)
        !           325:                alloc = pool_page_alloc;
        !           326:
        !           327:        if (release == NULL)
        !           328:                release = pool_page_free;
        !           329:
        !           330:        if (pagesz == 0)
        !           331:                pagesz = PAGE_SIZE;
        !           332:
        !           333:        if (align == 0)
        !           334:                align = ALIGN(1);
        !           335:
        !           336:        /*
        !           337:         * Initialize the pool structure.
        !           338:         */
        !           339:        TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
        !           340:        TAILQ_INIT(&pp->pr_pagelist);
        !           341:        pp->pr_curpage = NULL;
        !           342:        pp->pr_npages = 0;
        !           343:        pp->pr_minitems = 0;
        !           344:        pp->pr_minpages = 0;
        !           345:        pp->pr_maxpages = UINT_MAX;
        !           346:        pp->pr_flags = flags;
        !           347:        pp->pr_size = ALIGN(size);
        !           348:        pp->pr_align = align;
        !           349:        pp->pr_wchan = wchan;
        !           350:        pp->pr_mtype = mtype;
        !           351:        pp->pr_alloc = alloc;
        !           352:        pp->pr_free = release;
        !           353:        pp->pr_pagesz = pagesz;
        !           354:        pp->pr_pagemask = ~(pagesz - 1);
        !           355:        pp->pr_pageshift = ffs(pagesz) - 1;
        !           356:
        !           357:        /*
        !           358:         * Decide whether to put the page header off page to avoid
        !           359:         * wasting too large a part of the page. Off-page page headers
        !           360:         * go on a hash table, so we can match a returned item
        !           361:         * with its header based on the page address.
        !           362:         * We use 1/16 of the page size as the threshold (XXX: tune)
        !           363:         */
        !           364:        if (pp->pr_size < pagesz/16) {
        !           365:                /* Use the end of the page for the page header */
        !           366:                pp->pr_flags |= PR_PHINPAGE;
        !           367:                pp->pr_phoffset = off =
        !           368:                        pagesz - ALIGN(sizeof(struct pool_item_header));
1.2       pk        369:        } else {
1.3     ! pk        370:                /* The page header will be taken from our page header pool */
        !           371:                pp->pr_phoffset = 0;
        !           372:                off = pagesz;
        !           373:                bzero(pp->pr_hashtab, sizeof(pp->pr_hashtab));
1.2       pk        374:        }
1.1       pk        375:
1.3     ! pk        376:        /*
        !           377:         * Alignment is to take place at `ioff' within the item. This means
        !           378:         * we must reserve up to `align - 1' bytes on the page to allow
        !           379:         * appropriate positioning of each item.
        !           380:         *
        !           381:         * Silently enforce `0 <= ioff < align'.
        !           382:         */
        !           383:        pp->pr_itemoffset = ioff = ioff % align;
        !           384:        pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
        !           385:
        !           386:        /*
        !           387:         * Use the slack between the chunks and the page header
        !           388:         * for "cache coloring".
        !           389:         */
        !           390:        slack = off - pp->pr_itemsperpage * pp->pr_size;
        !           391:        pp->pr_maxcolor = (slack / align) * align;
        !           392:        pp->pr_curcolor = 0;
        !           393:
        !           394:        pp->pr_nget = 0;
        !           395:        pp->pr_nfail = 0;
        !           396:        pp->pr_nput = 0;
        !           397:        pp->pr_npagealloc = 0;
        !           398:        pp->pr_npagefree = 0;
1.1       pk        399:        pp->pr_hiwat = 0;
1.3     ! pk        400:
        !           401: #ifdef POOL_DIAGNOSTIC
        !           402:        if ((flags & PR_LOGGING) != 0) {
        !           403:                pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
        !           404:                                    M_TEMP, M_NOWAIT);
        !           405:                if (pp->pr_log == NULL)
        !           406:                        pp->pr_flags &= ~PR_LOGGING;
        !           407:                pp->pr_curlogentry = 0;
        !           408:                pp->pr_logsize = pool_logsize;
        !           409:        }
        !           410: #endif
        !           411:
1.1       pk        412:        simple_lock_init(&pp->pr_lock);
                    413:
1.3     ! pk        414:        /*
        !           415:         * Initialize private page header pool if we haven't done so yet.
        !           416:         */
        !           417:        if (phpool.pr_size == 0) {
        !           418:                pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
        !           419:                          0, "phpool", 0, 0, 0, 0);
1.1       pk        420:        }
                    421:
1.3     ! pk        422:        return;
1.1       pk        423: }
                    424:
                    425: /*
                    426:  * De-commision a pool resource.
                    427:  */
                    428: void
                    429: pool_destroy(pp)
                    430:        struct pool *pp;
                    431: {
1.3     ! pk        432:        struct pool_item_header *ph;
        !           433:
        !           434: #ifdef DIAGNOSTIC
        !           435:        if (pp->pr_nget - pp->pr_nput != 0) {
        !           436:                pr_printlog(pp);
        !           437:                panic("pool_destroy: pool busy: still out: %lu\n",
        !           438:                      pp->pr_nget - pp->pr_nput);
        !           439:        }
        !           440: #endif
1.1       pk        441:
1.3     ! pk        442:        /* Remove all pages */
        !           443:        if ((pp->pr_flags & PR_STATIC) == 0)
        !           444:                while ((ph = pp->pr_pagelist.tqh_first) != NULL)
        !           445:                        pr_rmpage(pp, ph);
        !           446:
        !           447:        /* Remove from global pool list */
        !           448:        TAILQ_REMOVE(&pool_head, pp, pr_poollist);
        !           449:        drainpp = NULL;
        !           450:
        !           451: #ifdef POOL_DIAGNOSTIC
        !           452:        if ((pp->pr_flags & PR_LOGGING) != 0)
        !           453:                free(pp->pr_log, M_TEMP);
        !           454: #endif
1.2       pk        455:
1.3     ! pk        456:        if (pp->pr_flags & PR_FREEHEADER)
        !           457:                free(pp, M_POOL);
1.1       pk        458: }
                    459:
                    460:
                    461: /*
1.3     ! pk        462:  * Grab an item from the pool; must be called at appropriate spl level
1.1       pk        463:  */
1.3     ! pk        464: #ifdef POOL_DIAGNOSTIC
        !           465: void *
        !           466: _pool_get(pp, flags, file, line)
        !           467:        struct pool *pp;
        !           468:        int flags;
        !           469:        const char *file;
        !           470:        long line;
        !           471: #else
1.1       pk        472: void *
                    473: pool_get(pp, flags)
                    474:        struct pool *pp;
                    475:        int flags;
1.3     ! pk        476: #endif
1.1       pk        477: {
                    478:        void *v;
                    479:        struct pool_item *pi;
1.3     ! pk        480:        struct pool_item_header *ph;
1.1       pk        481:
1.2       pk        482: #ifdef DIAGNOSTIC
1.3     ! pk        483:        if ((pp->pr_flags & PR_STATIC) && (flags & PR_MALLOCOK)) {
        !           484:                pr_printlog(pp);
1.2       pk        485:                panic("pool_get: static");
1.3     ! pk        486:        }
1.2       pk        487: #endif
                    488:
1.1       pk        489:        simple_lock(&pp->pr_lock);
1.3     ! pk        490:        if (curproc == NULL && (flags & PR_WAITOK) != 0)
        !           491:                panic("pool_get: must have NOWAIT");
1.1       pk        492:
1.3     ! pk        493:        /*
        !           494:         * The convention we use is that if `curpage' is not NULL, then
        !           495:         * it points at a non-empty bucket. In particular, `curpage'
        !           496:         * never points at a page header which has PR_PHINPAGE set and
        !           497:         * has no items in its bucket.
        !           498:         */
        !           499: again:
        !           500:        if ((ph = pp->pr_curpage) == NULL) {
        !           501:                void *v = (*pp->pr_alloc)(pp->pr_pagesz, flags, pp->pr_mtype);
1.1       pk        502:                if (v == NULL) {
1.3     ! pk        503:                        if (flags & PR_URGENT)
        !           504:                                panic("pool_get: urgent");
        !           505:                        if ((flags & PR_WAITOK) == 0) {
        !           506:                                pp->pr_nfail++;
        !           507:                                simple_unlock(&pp->pr_lock);
1.1       pk        508:                                return (NULL);
1.3     ! pk        509:                        }
        !           510:
1.1       pk        511:                        pp->pr_flags |= PR_WANTED;
                    512:                        simple_unlock(&pp->pr_lock);
                    513:                        tsleep((caddr_t)pp, PSWP, pp->pr_wchan, 0);
1.3     ! pk        514:                        simple_lock(&pp->pr_lock);
        !           515:                } else {
        !           516:                        pp->pr_npagealloc++;
        !           517:                        pool_prime_page(pp, v);
1.1       pk        518:                }
1.3     ! pk        519:
        !           520:                goto again;
        !           521:        }
        !           522:
        !           523:        if ((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)
        !           524:                panic("pool_get: %s: page empty", pp->pr_wchan);
        !           525:
        !           526:        pr_log(pp, v, PRLOG_GET, file, line);
        !           527:
        !           528: #ifdef DIAGNOSTIC
        !           529:        if (pi->pi_magic != PI_MAGIC) {
        !           530:                pr_printlog(pp);
        !           531:                panic("pool_get(%s): free list modified: magic=%x; page %p;"
        !           532:                       " item addr %p\n",
        !           533:                        pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
        !           534:        }
        !           535: #endif
        !           536:
        !           537:        /*
        !           538:         * Remove from item list.
        !           539:         */
        !           540:        TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
        !           541:        ph->ph_nmissing++;
        !           542:        if (TAILQ_FIRST(&ph->ph_itemlist) == NULL) {
        !           543:                /*
        !           544:                 * Find a new non-empty page header, if any.
        !           545:                 * Start search from the page head, to increase
        !           546:                 * the chance for "high water" pages to be freed.
        !           547:                 *
        !           548:                 * First, move the now empty page to the head of
        !           549:                 * the page list.
        !           550:                 */
        !           551:                TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
        !           552:                TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
        !           553:                while ((ph = TAILQ_NEXT(ph, ph_pagelist)) != NULL)
        !           554:                        if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
        !           555:                                break;
        !           556:
        !           557:                pp->pr_curpage = ph;
1.1       pk        558:        }
1.3     ! pk        559:
        !           560:        pp->pr_nget++;
1.1       pk        561:        simple_unlock(&pp->pr_lock);
                    562:        return (v);
                    563: }
                    564:
                    565: /*
1.3     ! pk        566:  * Return resource to the pool; must be called at appropriate spl level
1.1       pk        567:  */
1.3     ! pk        568: #ifdef POOL_DIAGNOSTIC
        !           569: void
        !           570: _pool_put(pp, v, file, line)
        !           571:        struct pool *pp;
        !           572:        void *v;
        !           573:        const char *file;
        !           574:        long line;
        !           575: #else
1.1       pk        576: void
                    577: pool_put(pp, v)
                    578:        struct pool *pp;
                    579:        void *v;
1.3     ! pk        580: #endif
1.1       pk        581: {
                    582:        struct pool_item *pi = v;
1.3     ! pk        583:        struct pool_item_header *ph;
        !           584:        caddr_t page;
        !           585:
        !           586:        page = (caddr_t)((u_long)v & pp->pr_pagemask);
1.1       pk        587:
                    588:        simple_lock(&pp->pr_lock);
1.3     ! pk        589:
        !           590:        pr_log(pp, v, PRLOG_PUT, file, line);
        !           591:
        !           592:        if ((ph = pr_find_pagehead(pp, page)) == NULL) {
        !           593:                pr_printlog(pp);
        !           594:                panic("pool_put: %s: page header missing", pp->pr_wchan);
        !           595:        }
        !           596:
        !           597:        /*
        !           598:         * Return to item list.
        !           599:         */
1.2       pk        600: #ifdef DIAGNOSTIC
1.3     ! pk        601:        pi->pi_magic = PI_MAGIC;
        !           602: #endif
        !           603:        TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
        !           604:        ph->ph_nmissing--;
        !           605:        pp->pr_nput++;
        !           606:
        !           607:        /* Cancel "pool empty" condition if it exists */
        !           608:        if (pp->pr_curpage == NULL)
        !           609:                pp->pr_curpage = ph;
        !           610:
        !           611:        if (pp->pr_flags & PR_WANTED) {
        !           612:                pp->pr_flags &= ~PR_WANTED;
        !           613:                wakeup((caddr_t)pp);
        !           614:                simple_unlock(&pp->pr_lock);
        !           615:                return;
        !           616:        }
        !           617:
        !           618:        /*
        !           619:         * If this page is now complete, move it to the end of the pagelist.
        !           620:         * If this page has just become un-empty, move it the head.
        !           621:         */
        !           622:        if (ph->ph_nmissing == 0) {
        !           623:                if (pp->pr_npages > pp->pr_maxpages) {
        !           624: #if 0
        !           625:                        timeout(pool_drain, 0, pool_inactive_time*hz);
        !           626: #else
        !           627:                        pr_rmpage(pp, ph);
1.2       pk        628: #endif
1.3     ! pk        629:                } else {
        !           630:                        TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
        !           631:                        TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
        !           632:                        ph->ph_time = time;
        !           633:
        !           634:                        /* XXX - update curpage */
        !           635:                        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
        !           636:                             ph = TAILQ_NEXT(ph, ph_pagelist))
        !           637:                                if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
        !           638:                                        break;
1.1       pk        639:
1.3     ! pk        640:                        pp->pr_curpage = ph;
1.1       pk        641:                }
                    642:        }
1.3     ! pk        643:
1.1       pk        644:        simple_unlock(&pp->pr_lock);
                    645: }
                    646:
                    647: /*
1.3     ! pk        648:  * Add N items to the pool.
1.1       pk        649:  */
                    650: int
1.2       pk        651: pool_prime(pp, n, storage)
1.1       pk        652:        struct pool *pp;
                    653:        int n;
1.2       pk        654:        caddr_t storage;
1.1       pk        655: {
1.3     ! pk        656:        caddr_t cp;
        !           657:        int newnitems, newpages;
1.2       pk        658:
                    659: #ifdef DIAGNOSTIC
                    660:        if (storage && !(pp->pr_flags & PR_STATIC))
                    661:                panic("pool_prime: static");
                    662:        /* !storage && static caught below */
                    663: #endif
1.1       pk        664:
1.3     ! pk        665:        newnitems = pp->pr_minitems + n;
        !           666:        newpages =
        !           667:                roundup(pp->pr_itemsperpage,newnitems) / pp->pr_itemsperpage
        !           668:                - pp->pr_minpages;
        !           669:
1.1       pk        670:        simple_lock(&pp->pr_lock);
1.3     ! pk        671:        while (newpages-- > 0) {
        !           672:
1.2       pk        673:                if (pp->pr_flags & PR_STATIC) {
1.3     ! pk        674:                        cp = storage;
        !           675:                        storage += pp->pr_pagesz;
        !           676:                } else {
        !           677:                        cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
        !           678:                }
1.2       pk        679:
1.3     ! pk        680:                if (cp == NULL) {
1.1       pk        681:                        simple_unlock(&pp->pr_lock);
                    682:                        return (ENOMEM);
                    683:                }
                    684:
1.3     ! pk        685:                pool_prime_page(pp, cp);
        !           686:                pp->pr_minpages++;
1.1       pk        687:        }
1.3     ! pk        688:
        !           689:        pp->pr_minitems = newnitems;
        !           690:
        !           691:        if (pp->pr_minpages >= pp->pr_maxpages)
        !           692:                pp->pr_maxpages = pp->pr_minpages + 1;  /* XXX */
        !           693:
1.1       pk        694:        simple_unlock(&pp->pr_lock);
                    695:        return (0);
                    696: }
1.3     ! pk        697:
        !           698: /*
        !           699:  * Add a page worth of items to the pool.
        !           700:  */
        !           701: int
        !           702: pool_prime_page(pp, storage)
        !           703:        struct pool *pp;
        !           704:        caddr_t storage;
        !           705: {
        !           706:        struct pool_item *pi;
        !           707:        struct pool_item_header *ph;
        !           708:        caddr_t cp = storage;
        !           709:        unsigned int align = pp->pr_align;
        !           710:        unsigned int ioff = pp->pr_itemoffset;
        !           711:        int n;
        !           712:
        !           713:        if ((pp->pr_flags & PR_PHINPAGE) != 0) {
        !           714:                ph = (struct pool_item_header *)(cp + pp->pr_phoffset);
        !           715:        } else {
        !           716:                ph = pool_get(&phpool, PR_URGENT);
        !           717:                LIST_INSERT_HEAD(&pp->pr_hashtab[PR_HASH_INDEX(pp, cp)],
        !           718:                                 ph, ph_hashlist);
        !           719:        }
        !           720:
        !           721:        /*
        !           722:         * Insert page header.
        !           723:         */
        !           724:        TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
        !           725:        TAILQ_INIT(&ph->ph_itemlist);
        !           726:        ph->ph_page = storage;
        !           727:        ph->ph_nmissing = 0;
        !           728:        ph->ph_time.tv_sec = ph->ph_time.tv_usec = 0;
        !           729:
        !           730:        /*
        !           731:         * Color this page.
        !           732:         */
        !           733:        cp = (caddr_t)(cp + pp->pr_curcolor);
        !           734:        if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
        !           735:                pp->pr_curcolor = 0;
        !           736:
        !           737:        /*
        !           738:         * Adjust storage to apply aligment to `pr_itemoffset' in each item.
        !           739:         */
        !           740:        if (ioff != 0)
        !           741:                cp = (caddr_t)(cp + (align - ioff));
        !           742:
        !           743:        /*
        !           744:         * Insert remaining chunks on the bucket list.
        !           745:         */
        !           746:        n = pp->pr_itemsperpage;
        !           747:
        !           748:        while (n--) {
        !           749:                pi = (struct pool_item *)cp;
        !           750:
        !           751:                /* Insert on page list */
        !           752:                TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
        !           753: #ifdef DIAGNOSTIC
        !           754:                pi->pi_magic = PI_MAGIC;
        !           755: #endif
        !           756:                cp = (caddr_t)(cp + pp->pr_size);
        !           757:        }
        !           758:
        !           759:        /*
        !           760:         * If the pool was depleted, point at the new page.
        !           761:         */
        !           762:        if (pp->pr_curpage == NULL)
        !           763:                pp->pr_curpage = ph;
        !           764:
        !           765:        if (++pp->pr_npages > pp->pr_hiwat)
        !           766:                pp->pr_hiwat = pp->pr_npages;
        !           767:
        !           768:        return (0);
        !           769: }
        !           770:
        !           771: void
        !           772: pool_setlowat(pp, n)
        !           773:        pool_handle_t   pp;
        !           774:        int n;
        !           775: {
        !           776:        pp->pr_minitems = n;
        !           777:        if (n == 0) {
        !           778:                pp->pr_minpages = 0;
        !           779:                return;
        !           780:        }
        !           781:        pp->pr_minpages =
        !           782:                roundup(pp->pr_itemsperpage,n) / pp->pr_itemsperpage;
        !           783: }
        !           784:
        !           785: void
        !           786: pool_sethiwat(pp, n)
        !           787:        pool_handle_t   pp;
        !           788:        int n;
        !           789: {
        !           790:        if (n == 0) {
        !           791:                pp->pr_maxpages = 0;
        !           792:                return;
        !           793:        }
        !           794:        pp->pr_maxpages =
        !           795:                roundup(pp->pr_itemsperpage,n) / pp->pr_itemsperpage;
        !           796: }
        !           797:
        !           798:
        !           799: /*
        !           800:  * Default page allocator.
        !           801:  */
        !           802: static void *
        !           803: pool_page_alloc(sz, flags, mtype)
        !           804:        unsigned long sz;
        !           805:        int flags;
        !           806:        int mtype;
        !           807: {
        !           808:        vm_offset_t va;
        !           809:
        !           810: #if defined(UVM)
        !           811:        va = uvm_km_kmemalloc(kernel_map, uvm.kernel_object,
        !           812:                              (vm_size_t)sz, UVM_KMF_NOWAIT);
        !           813: #else
        !           814:        va = kmem_malloc(kmem_map, (vm_size_t)sz, 0);
        !           815: #endif
        !           816:        return ((void *)va);
        !           817: }
        !           818:
        !           819: static void
        !           820: pool_page_free(v, sz, mtype)
        !           821:        void *v;
        !           822:        unsigned long sz;
        !           823:        int mtype;
        !           824: {
        !           825:
        !           826: #if defined(UVM)
        !           827:        uvm_km_free(kernel_map, (vm_offset_t)v, sz);
        !           828: #else
        !           829:        kmem_free(kmem_map, (vm_offset_t)v, sz);
        !           830: #endif
        !           831: }
        !           832:
        !           833: /*
        !           834:  * Release all complete pages that have not been used recently.
        !           835:  */
        !           836: void
        !           837: pool_reclaim (pp)
        !           838:        pool_handle_t pp;
        !           839: {
        !           840:        struct pool_item_header *ph, *phnext;
        !           841:        struct timeval curtime = time;
        !           842:
        !           843:        if (pp->pr_flags & PR_STATIC)
        !           844:                return;
        !           845:
        !           846:        if (simple_lock_try(&pp->pr_lock) == 0)
        !           847:                return;
        !           848:
        !           849:        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL; ph = phnext) {
        !           850:                phnext = TAILQ_NEXT(ph, ph_pagelist);
        !           851:
        !           852:                /* Check our minimum page claim */
        !           853:                if (pp->pr_npages <= pp->pr_minpages)
        !           854:                        break;
        !           855:
        !           856:                if (ph->ph_nmissing == 0) {
        !           857:                        struct timeval diff;
        !           858:                        timersub(&curtime, &ph->ph_time, &diff);
        !           859:                        if (diff.tv_sec < pool_inactive_time)
        !           860:                                continue;
        !           861:                        pr_rmpage(pp, ph);
        !           862:                }
        !           863:        }
        !           864:
        !           865:        simple_unlock(&pp->pr_lock);
        !           866: }
        !           867:
        !           868:
        !           869: /*
        !           870:  * Drain pools, one at a time.
        !           871:  */
        !           872: void
        !           873: pool_drain(arg)
        !           874:        void *arg;
        !           875: {
        !           876:        struct pool *pp;
        !           877:        int s = splimp();
        !           878:
        !           879:        /* XXX:lock pool head */
        !           880:        if (drainpp == NULL && (drainpp = TAILQ_FIRST(&pool_head)) == NULL) {
        !           881:                splx(s);
        !           882:                return;
        !           883:        }
        !           884:
        !           885:        pp = drainpp;
        !           886:        drainpp = TAILQ_NEXT(pp, pr_poollist);
        !           887:        /* XXX:unlock pool head */
        !           888:
        !           889:        pool_reclaim(pp);
        !           890:        splx(s);
        !           891: }
        !           892:
        !           893:
        !           894: #ifdef DEBUG
        !           895: /*
        !           896:  * Diagnostic helpers.
        !           897:  */
        !           898: void
        !           899: pool_print(pp, label)
        !           900:        struct pool *pp;
        !           901:        char *label;
        !           902: {
        !           903:
        !           904:        if (label != NULL)
        !           905:                printf("%s: ", label);
        !           906:
        !           907:        printf("pool %s: nalloc %lu nfree %lu npagealloc %lu npagefree %lu\n"
        !           908:               "         npages %u minitems %u itemsperpage %u itemoffset %u\n",
        !           909:                pp->pr_wchan,
        !           910:                pp->pr_nget,
        !           911:                pp->pr_nput,
        !           912:                pp->pr_npagealloc,
        !           913:                pp->pr_npagefree,
        !           914:                pp->pr_npages,
        !           915:                pp->pr_minitems,
        !           916:                pp->pr_itemsperpage,
        !           917:                pp->pr_itemoffset);
        !           918: }
        !           919:
        !           920: int
        !           921: pool_chk(pp, label)
        !           922:        struct pool *pp;
        !           923:        char *label;
        !           924: {
        !           925:        struct pool_item_header *ph;
        !           926:        int r = 0;
        !           927:
        !           928:        simple_lock(&pp->pr_lock);
        !           929:
        !           930:        for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
        !           931:             ph = TAILQ_NEXT(ph, ph_pagelist)) {
        !           932:
        !           933:                struct pool_item *pi;
        !           934:                int n;
        !           935:                caddr_t page;
        !           936:
        !           937:                page = (caddr_t)((u_long)ph & pp->pr_pagemask);
        !           938:                if (page != ph->ph_page) {
        !           939:                        if (label != NULL)
        !           940:                                printf("%s: ", label);
        !           941:                        printf("pool(%s): page inconsistency: page %p;"
        !           942:                               " at page head addr %p (p %p)\n",
        !           943:                                pp->pr_wchan, ph->ph_page,
        !           944:                                ph, page);
        !           945:                        r++;
        !           946:                        goto out;
        !           947:                }
        !           948:
        !           949:                for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
        !           950:                     pi != NULL;
        !           951:                     pi = TAILQ_NEXT(pi,pi_list), n++) {
        !           952:
        !           953: #ifdef DIAGNOSTIC
        !           954:                        if (pi->pi_magic != PI_MAGIC) {
        !           955:                                if (label != NULL)
        !           956:                                        printf("%s: ", label);
        !           957:                                printf("pool(%s): free list modified: magic=%x;"
        !           958:                                       " page %p; item ordinal %d;"
        !           959:                                       " addr %p (p %p)\n",
        !           960:                                        pp->pr_wchan, pi->pi_magic, ph->ph_page,
        !           961:                                        n, pi, page);
        !           962:                                panic("pool");
        !           963:                        }
        !           964: #endif
        !           965:                        page = (caddr_t)((u_long)pi & pp->pr_pagemask);
        !           966:                        if (page == ph->ph_page)
        !           967:                                continue;
        !           968:
        !           969:                        if (label != NULL)
        !           970:                                printf("%s: ", label);
        !           971:                        printf("pool(%s): page inconsistency: page %p;"
        !           972:                               " item ordinal %d; addr %p (p %p)\n",
        !           973:                                pp->pr_wchan, ph->ph_page,
        !           974:                                n, pi, page);
        !           975:                        r++;
        !           976:                        goto out;
        !           977:                }
        !           978:        }
        !           979: out:
        !           980:        simple_unlock(&pp->pr_lock);
        !           981:        return (r);
        !           982: }
        !           983: #endif

CVSweb <webmaster@jp.NetBSD.org>