Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/kern/subr_pool.c,v rcsdiff: /ftp/cvs/cvsroot/src/sys/kern/subr_pool.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.93.2.1 retrieving revision 1.99.8.1.2.1 diff -u -p -r1.93.2.1 -r1.99.8.1.2.1 --- src/sys/kern/subr_pool.c 2004/06/22 08:58:42 1.93.2.1 +++ src/sys/kern/subr_pool.c 2006/03/10 13:19:48 1.99.8.1.2.1 @@ -1,4 +1,4 @@ -/* $NetBSD: subr_pool.c,v 1.93.2.1 2004/06/22 08:58:42 tron Exp $ */ +/* $NetBSD: subr_pool.c,v 1.99.8.1.2.1 2006/03/10 13:19:48 tron Exp $ */ /*- * Copyright (c) 1997, 1999, 2000 The NetBSD Foundation, Inc. @@ -38,7 +38,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.93.2.1 2004/06/22 08:58:42 tron Exp $"); +__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.99.8.1.2.1 2006/03/10 13:19:48 tron Exp $"); #include "opt_pool.h" #include "opt_poollog.h" @@ -73,13 +73,23 @@ __KERNEL_RCSID(0, "$NetBSD: subr_pool.c, TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head); /* Private pool for page header structures */ -static struct pool phpool; +#define PHPOOL_MAX 8 +static struct pool phpool[PHPOOL_MAX]; +#define PHPOOL_FREELIST_NELEM(idx) (((idx) == 0) ? 0 : (1 << (idx))) #ifdef POOL_SUBPAGE /* Pool of subpages for use by normal pools. */ static struct pool psppool; #endif +static void *pool_page_alloc_meta(struct pool *, int); +static void pool_page_free_meta(struct pool *, void *); + +/* allocator for pool metadata */ +static struct pool_allocator pool_allocator_meta = { + pool_page_alloc_meta, pool_page_free_meta +}; + /* # of seconds to retain page after last use */ int pool_inactive_time = 10; @@ -89,17 +99,39 @@ static struct pool *drainpp; /* This spin lock protects both pool_head and drainpp. */ struct simplelock pool_head_slock = SIMPLELOCK_INITIALIZER; +typedef uint8_t pool_item_freelist_t; + struct pool_item_header { /* Page headers */ LIST_ENTRY(pool_item_header) ph_pagelist; /* pool page list */ - TAILQ_HEAD(,pool_item) ph_itemlist; /* chunk list for this page */ SPLAY_ENTRY(pool_item_header) ph_node; /* Off-page page headers */ - unsigned int ph_nmissing; /* # of chunks in use */ caddr_t ph_page; /* this page's address */ struct timeval ph_time; /* last referenced */ + union { + /* !PR_NOTOUCH */ + struct { + TAILQ_HEAD(, pool_item) + phu_itemlist; /* chunk list for this page */ + } phu_normal; + /* PR_NOTOUCH */ + struct { + uint16_t + phu_off; /* start offset in page */ + pool_item_freelist_t + phu_firstfree; /* first free item */ + /* + * XXX it might be better to use + * a simple bitmap and ffs(3) + */ + } phu_notouch; + } ph_u; + uint16_t ph_nmissing; /* # of chunks in use */ }; +#define ph_itemlist ph_u.phu_normal.phu_itemlist +#define ph_off ph_u.phu_notouch.phu_off +#define ph_firstfree ph_u.phu_notouch.phu_firstfree struct pool_item { #ifdef DIAGNOSTIC @@ -142,7 +174,7 @@ struct pool_item { /* The cache group pool. */ static struct pool pcgpool; -static void pool_cache_reclaim(struct pool_cache *); +static void pool_cache_reclaim(struct pool_cache *, struct pool_pagelist *); static int pool_catchup(struct pool *); static void pool_prime_page(struct pool *, caddr_t, @@ -152,7 +184,7 @@ static void pool_update_curpage(struct p void *pool_allocator_alloc(struct pool *, int); void pool_allocator_free(struct pool *, void *); -static void pool_print_pagelist(struct pool_pagelist *, +static void pool_print_pagelist(struct pool *, struct pool_pagelist *, void (*)(const char *, ...)); static void pool_print1(struct pool *, const char *, void (*)(const char *, ...)); @@ -279,6 +311,50 @@ pr_enter_check(struct pool *pp, void (*p #endif /* POOL_DIAGNOSTIC */ static __inline int +pr_item_notouch_index(const struct pool *pp, const struct pool_item_header *ph, + const void *v) +{ + const char *cp = v; + int idx; + + KASSERT(pp->pr_roflags & PR_NOTOUCH); + idx = (cp - ph->ph_page - ph->ph_off) / pp->pr_size; + KASSERT(idx < pp->pr_itemsperpage); + return idx; +} + +#define PR_FREELIST_ALIGN(p) \ + roundup((uintptr_t)(p), sizeof(pool_item_freelist_t)) +#define PR_FREELIST(ph) ((pool_item_freelist_t *)PR_FREELIST_ALIGN((ph) + 1)) +#define PR_INDEX_USED ((pool_item_freelist_t)-1) +#define PR_INDEX_EOL ((pool_item_freelist_t)-2) + +static __inline void +pr_item_notouch_put(const struct pool *pp, struct pool_item_header *ph, + void *obj) +{ + int idx = pr_item_notouch_index(pp, ph, obj); + pool_item_freelist_t *freelist = PR_FREELIST(ph); + + KASSERT(freelist[idx] == PR_INDEX_USED); + freelist[idx] = ph->ph_firstfree; + ph->ph_firstfree = idx; +} + +static __inline void * +pr_item_notouch_get(const struct pool *pp, struct pool_item_header *ph) +{ + int idx = ph->ph_firstfree; + pool_item_freelist_t *freelist = PR_FREELIST(ph); + + KASSERT(freelist[idx] != PR_INDEX_USED); + ph->ph_firstfree = freelist[idx]; + freelist[idx] = PR_INDEX_USED; + + return ph->ph_page + ph->ph_off + idx * pp->pr_size; +} + +static __inline int phtree_compare(struct pool_item_header *a, struct pool_item_header *b) { if (a->ph_page < b->ph_page) @@ -308,6 +384,23 @@ pr_find_pagehead(struct pool *pp, caddr_ return ph; } +static void +pr_pagelist_free(struct pool *pp, struct pool_pagelist *pq) +{ + struct pool_item_header *ph; + int s; + + while ((ph = LIST_FIRST(pq)) != NULL) { + LIST_REMOVE(ph, ph_pagelist); + pool_allocator_free(pp, ph->ph_page); + if ((pp->pr_roflags & PR_PHINPAGE) == 0) { + s = splvm(); + pool_put(pp->pr_phpool, ph); + splx(s); + } + } +} + /* * Remove a page from the pool. */ @@ -315,9 +408,8 @@ static __inline void pr_rmpage(struct pool *pp, struct pool_item_header *ph, struct pool_pagelist *pq) { - int s; - LOCK_ASSERT(!simple_lock_held(&pp->pr_slock) || pq != NULL); + LOCK_ASSERT(simple_lock_held(&pp->pr_slock)); /* * If the page was idle, decrement the idle page count. @@ -335,21 +427,13 @@ pr_rmpage(struct pool *pp, struct pool_i pp->pr_nitems -= pp->pr_itemsperpage; /* - * Unlink a page from the pool and release it (or queue it for release). + * Unlink the page from the pool and queue it for release. */ LIST_REMOVE(ph, ph_pagelist); if ((pp->pr_roflags & PR_PHINPAGE) == 0) SPLAY_REMOVE(phtree, &pp->pr_phtree, ph); - if (pq) { - LIST_INSERT_HEAD(pq, ph, ph_pagelist); - } else { - pool_allocator_free(pp, ph->ph_page); - if ((pp->pr_roflags & PR_PHINPAGE) == 0) { - s = splvm(); - pool_put(&phpool, ph); - splx(s); - } - } + LIST_INSERT_HEAD(pq, ph, ph_pagelist); + pp->pr_npages--; pp->pr_npagefree++; @@ -357,6 +441,21 @@ pr_rmpage(struct pool *pp, struct pool_i } /* + * Initialize all the pools listed in the "pools" link set. + */ +void +link_pool_init(void) +{ + __link_set_decl(pools, struct link_pool_init); + struct link_pool_init * const *pi; + + __link_set_foreach(pi, pools) + pool_init((*pi)->pp, (*pi)->size, (*pi)->align, + (*pi)->align_offset, (*pi)->flags, (*pi)->wchan, + (*pi)->palloc); +} + +/* * Initialize the given pool resource structure. * * We export this routine to allow other kernel parts to declare @@ -370,6 +469,9 @@ pool_init(struct pool *pp, size_t size, size_t trysize, phsize; int s; + KASSERT((1UL << (CHAR_BIT * sizeof(pool_item_freelist_t))) - 2 >= + PHPOOL_FREELIST_NELEM(PHPOOL_MAX - 1)); + #ifdef POOL_DIAGNOSTIC /* * Always log if POOL_DIAGNOSTIC is defined. @@ -378,35 +480,19 @@ pool_init(struct pool *pp, size_t size, flags |= PR_LOGGING; #endif -#ifdef POOL_SUBPAGE - /* - * XXX We don't provide a real `nointr' back-end - * yet; all sub-pages come from a kmem back-end. - * maybe some day... - */ - if (palloc == NULL) { - extern struct pool_allocator pool_allocator_kmem_subpage; - palloc = &pool_allocator_kmem_subpage; - } - /* - * We'll assume any user-specified back-end allocator - * will deal with sub-pages, or simply don't care. - */ -#else if (palloc == NULL) palloc = &pool_allocator_kmem; +#ifdef POOL_SUBPAGE + if (size > palloc->pa_pagesz) { + if (palloc == &pool_allocator_kmem) + palloc = &pool_allocator_kmem_fullpage; + else if (palloc == &pool_allocator_nointr) + palloc = &pool_allocator_nointr_fullpage; + } #endif /* POOL_SUBPAGE */ if ((palloc->pa_flags & PA_INITIALIZED) == 0) { - if (palloc->pa_pagesz == 0) { -#ifdef POOL_SUBPAGE - if (palloc == &pool_allocator_kmem) - palloc->pa_pagesz = PAGE_SIZE; - else - palloc->pa_pagesz = POOL_SUBPAGE; -#else + if (palloc->pa_pagesz == 0) palloc->pa_pagesz = PAGE_SIZE; -#endif /* POOL_SUBPAGE */ - } TAILQ_INIT(&palloc->pa_list); @@ -475,8 +561,9 @@ pool_init(struct pool *pp, size_t size, /* See the comment below about reserved bytes. */ trysize = palloc->pa_pagesz - ((align - ioff) % align); phsize = ALIGN(sizeof(struct pool_item_header)); - if (pp->pr_size < MIN(palloc->pa_pagesz / 16, phsize << 3) || - trysize / pp->pr_size == (trysize - phsize) / pp->pr_size) { + if ((pp->pr_roflags & PR_NOTOUCH) == 0 && + (pp->pr_size < MIN(palloc->pa_pagesz / 16, phsize << 3) || + trysize / pp->pr_size == (trysize - phsize) / pp->pr_size)) { /* Use the end of the page for the page header */ pp->pr_roflags |= PR_PHINPAGE; pp->pr_phoffset = off = palloc->pa_pagesz - phsize; @@ -494,6 +581,30 @@ pool_init(struct pool *pp, size_t size, */ pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size; KASSERT(pp->pr_itemsperpage != 0); + if ((pp->pr_roflags & PR_NOTOUCH)) { + int idx; + + for (idx = 0; pp->pr_itemsperpage > PHPOOL_FREELIST_NELEM(idx); + idx++) { + /* nothing */ + } + if (idx >= PHPOOL_MAX) { + /* + * if you see this panic, consider to tweak + * PHPOOL_MAX and PHPOOL_FREELIST_NELEM. + */ + panic("%s: too large itemsperpage(%d) for PR_NOTOUCH", + pp->pr_wchan, pp->pr_itemsperpage); + } + pp->pr_phpool = &phpool[idx]; + } else if ((pp->pr_roflags & PR_PHINPAGE) == 0) { + pp->pr_phpool = &phpool[0]; + } +#if defined(DIAGNOSTIC) + else { + pp->pr_phpool = NULL; + } +#endif /* * Use the slack between the chunks and the page header @@ -532,18 +643,30 @@ pool_init(struct pool *pp, size_t size, * haven't done so yet. * XXX LOCKING. */ - if (phpool.pr_size == 0) { + if (phpool[0].pr_size == 0) { + int idx; + for (idx = 0; idx < PHPOOL_MAX; idx++) { + static char phpool_names[PHPOOL_MAX][6+1+6+1]; + int nelem; + size_t sz; + + nelem = PHPOOL_FREELIST_NELEM(idx); + snprintf(phpool_names[idx], sizeof(phpool_names[idx]), + "phpool-%d", nelem); + sz = sizeof(struct pool_item_header); + if (nelem) { + sz = PR_FREELIST_ALIGN(sz) + + nelem * sizeof(pool_item_freelist_t); + } + pool_init(&phpool[idx], sz, 0, 0, 0, + phpool_names[idx], &pool_allocator_meta); + } #ifdef POOL_SUBPAGE - pool_init(&phpool, sizeof(struct pool_item_header), 0, 0, 0, - "phpool", &pool_allocator_kmem); pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0, - PR_RECURSIVE, "psppool", &pool_allocator_kmem); -#else - pool_init(&phpool, sizeof(struct pool_item_header), 0, 0, - 0, "phpool", NULL); + PR_RECURSIVE, "psppool", &pool_allocator_meta); #endif pool_init(&pcgpool, sizeof(struct pool_cache_group), 0, 0, - 0, "pcgpool", NULL); + 0, "pcgpool", &pool_allocator_meta); } /* Insert into the list of all pools. */ @@ -565,20 +688,28 @@ pool_init(struct pool *pp, size_t size, void pool_destroy(struct pool *pp) { + struct pool_pagelist pq; struct pool_item_header *ph; - struct pool_cache *pc; int s; - /* Locking order: pool_allocator -> pool */ + /* Remove from global pool list */ + simple_lock(&pool_head_slock); + TAILQ_REMOVE(&pool_head, pp, pr_poollist); + if (drainpp == pp) + drainpp = NULL; + simple_unlock(&pool_head_slock); + + /* Remove this pool from its allocator's list of pools. */ s = splvm(); simple_lock(&pp->pr_alloc->pa_slock); TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list); simple_unlock(&pp->pr_alloc->pa_slock); splx(s); - /* Destroy all caches for this pool. */ - while ((pc = TAILQ_FIRST(&pp->pr_cachelist)) != NULL) - pool_cache_destroy(pc); + s = splvm(); + simple_lock(&pp->pr_slock); + + KASSERT(TAILQ_EMPTY(&pp->pr_cachelist)); #ifdef DIAGNOSTIC if (pp->pr_nout != 0) { @@ -588,19 +719,18 @@ pool_destroy(struct pool *pp) } #endif - /* Remove all pages */ - while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL) - pr_rmpage(pp, ph, NULL); KASSERT(LIST_EMPTY(&pp->pr_fullpages)); KASSERT(LIST_EMPTY(&pp->pr_partpages)); - /* Remove from global pool list */ - simple_lock(&pool_head_slock); - TAILQ_REMOVE(&pool_head, pp, pr_poollist); - if (drainpp == pp) { - drainpp = NULL; - } - simple_unlock(&pool_head_slock); + /* Remove all pages */ + LIST_INIT(&pq); + while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL) + pr_rmpage(pp, ph, &pq); + + simple_unlock(&pp->pr_slock); + splx(s); + + pr_pagelist_free(pp, &pq); #ifdef POOL_DIAGNOSTIC if ((pp->pr_roflags & PR_LOGGING) != 0) @@ -633,7 +763,7 @@ pool_alloc_item_header(struct pool *pp, ph = (struct pool_item_header *) (storage + pp->pr_phoffset); else { s = splvm(); - ph = pool_get(&phpool, flags); + ph = pool_get(pp->pr_phpool, flags); splx(s); } @@ -655,6 +785,9 @@ pool_get(struct pool *pp, int flags) void *v; #ifdef DIAGNOSTIC + if (__predict_false(pp->pr_itemsperpage == 0)) + panic("pool_get: pool %p: pr_itemsperpage is zero, " + "pool not initialized?", pp); if (__predict_false(curlwp == NULL && doing_shutdown == 0 && (flags & PR_WAITOK) != 0)) panic("pool_get: %s: must have NOWAIT", pp->pr_wchan); @@ -797,38 +930,53 @@ pool_get(struct pool *pp, int flags) /* Start the allocation process over. */ goto startover; } - if (__predict_false((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)) { - pr_leave(pp); - simple_unlock(&pp->pr_slock); - panic("pool_get: %s: page empty", pp->pr_wchan); - } + if (pp->pr_roflags & PR_NOTOUCH) { #ifdef DIAGNOSTIC - if (__predict_false(pp->pr_nitems == 0)) { - pr_leave(pp); - simple_unlock(&pp->pr_slock); - printf("pool_get: %s: items on itemlist, nitems %u\n", - pp->pr_wchan, pp->pr_nitems); - panic("pool_get: nitems inconsistent"); - } + if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) { + pr_leave(pp); + simple_unlock(&pp->pr_slock); + panic("pool_get: %s: page empty", pp->pr_wchan); + } +#endif + v = pr_item_notouch_get(pp, ph); +#ifdef POOL_DIAGNOSTIC + pr_log(pp, v, PRLOG_GET, file, line); +#endif + } else { + v = pi = TAILQ_FIRST(&ph->ph_itemlist); + if (__predict_false(v == NULL)) { + pr_leave(pp); + simple_unlock(&pp->pr_slock); + panic("pool_get: %s: page empty", pp->pr_wchan); + } +#ifdef DIAGNOSTIC + if (__predict_false(pp->pr_nitems == 0)) { + pr_leave(pp); + simple_unlock(&pp->pr_slock); + printf("pool_get: %s: items on itemlist, nitems %u\n", + pp->pr_wchan, pp->pr_nitems); + panic("pool_get: nitems inconsistent"); + } #endif #ifdef POOL_DIAGNOSTIC - pr_log(pp, v, PRLOG_GET, file, line); + pr_log(pp, v, PRLOG_GET, file, line); #endif #ifdef DIAGNOSTIC - if (__predict_false(pi->pi_magic != PI_MAGIC)) { - pr_printlog(pp, pi, printf); - panic("pool_get(%s): free list modified: magic=%x; page %p;" - " item addr %p\n", - pp->pr_wchan, pi->pi_magic, ph->ph_page, pi); - } + if (__predict_false(pi->pi_magic != PI_MAGIC)) { + pr_printlog(pp, pi, printf); + panic("pool_get(%s): free list modified: " + "magic=%x; page %p; item addr %p\n", + pp->pr_wchan, pi->pi_magic, ph->ph_page, pi); + } #endif - /* - * Remove from item list. - */ - TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list); + /* + * Remove from item list. + */ + TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list); + } pp->pr_nitems--; pp->pr_nout++; if (ph->ph_nmissing == 0) { @@ -846,9 +994,10 @@ pool_get(struct pool *pp, int flags) LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist); } ph->ph_nmissing++; - if (TAILQ_EMPTY(&ph->ph_itemlist)) { + if (ph->ph_nmissing == pp->pr_itemsperpage) { #ifdef DIAGNOSTIC - if (__predict_false(ph->ph_nmissing != pp->pr_itemsperpage)) { + if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 && + !TAILQ_EMPTY(&ph->ph_itemlist))) { pr_leave(pp); simple_unlock(&pp->pr_slock); panic("pool_get: %s: nmissing inconsistent", @@ -887,7 +1036,7 @@ pool_get(struct pool *pp, int flags) * Internal version of pool_put(). Pool is already locked/entered. */ static void -pool_do_put(struct pool *pp, void *v) +pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq) { struct pool_item *pi = v; struct pool_item_header *ph; @@ -921,20 +1070,24 @@ pool_do_put(struct pool *pp, void *v) /* * Return to item list. */ + if (pp->pr_roflags & PR_NOTOUCH) { + pr_item_notouch_put(pp, ph, v); + } else { #ifdef DIAGNOSTIC - pi->pi_magic = PI_MAGIC; + pi->pi_magic = PI_MAGIC; #endif #ifdef DEBUG - { - int i, *ip = v; + { + int i, *ip = v; - for (i = 0; i < pp->pr_size / sizeof(int); i++) { - *ip++ = PI_MAGIC; + for (i = 0; i < pp->pr_size / sizeof(int); i++) { + *ip++ = PI_MAGIC; + } } - } #endif - TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list); + TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list); + } KDASSERT(ph->ph_nmissing != 0); ph->ph_nmissing--; pp->pr_nput++; @@ -971,9 +1124,7 @@ pool_do_put(struct pool *pp, void *v) if (pp->pr_npages > pp->pr_minpages && (pp->pr_npages > pp->pr_maxpages || (pp->pr_alloc->pa_flags & PA_WANT) != 0)) { - simple_unlock(&pp->pr_slock); - pr_rmpage(pp, ph, NULL); - simple_lock(&pp->pr_slock); + pr_rmpage(pp, ph, pq); } else { LIST_REMOVE(ph, ph_pagelist); LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist); @@ -1011,16 +1162,22 @@ pool_do_put(struct pool *pp, void *v) void _pool_put(struct pool *pp, void *v, const char *file, long line) { + struct pool_pagelist pq; + + LIST_INIT(&pq); simple_lock(&pp->pr_slock); pr_enter(pp, file, line); pr_log(pp, v, PRLOG_PUT, file, line); - pool_do_put(pp, v); + pool_do_put(pp, v, &pq); pr_leave(pp); simple_unlock(&pp->pr_slock); + + if (! LIST_EMPTY(&pq)) + pr_pagelist_free(pp, &pq); } #undef pool_put #endif /* POOL_DIAGNOSTIC */ @@ -1028,12 +1185,16 @@ _pool_put(struct pool *pp, void *v, cons void pool_put(struct pool *pp, void *v) { + struct pool_pagelist pq; - simple_lock(&pp->pr_slock); - - pool_do_put(pp, v); + LIST_INIT(&pq); + simple_lock(&pp->pr_slock); + pool_do_put(pp, v, &pq); simple_unlock(&pp->pr_slock); + + if (! LIST_EMPTY(&pq)) + pr_pagelist_free(pp, &pq); } #ifdef POOL_DIAGNOSTIC @@ -1136,17 +1297,28 @@ pool_prime_page(struct pool *pp, caddr_t n = pp->pr_itemsperpage; pp->pr_nitems += n; - while (n--) { - pi = (struct pool_item *)cp; + if (pp->pr_roflags & PR_NOTOUCH) { + pool_item_freelist_t *freelist = PR_FREELIST(ph); + int i; + + ph->ph_off = cp - storage; + ph->ph_firstfree = 0; + for (i = 0; i < n - 1; i++) + freelist[i] = i + 1; + freelist[n - 1] = PR_INDEX_EOL; + } else { + while (n--) { + pi = (struct pool_item *)cp; - KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0); + KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0); - /* Insert on page list */ - TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list); + /* Insert on page list */ + TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list); #ifdef DIAGNOSTIC - pi->pi_magic = PI_MAGIC; + pi->pi_magic = PI_MAGIC; #endif - cp = (caddr_t)(cp + pp->pr_size); + cp = (caddr_t)(cp + pp->pr_size); + } } /* @@ -1304,7 +1476,7 @@ pool_reclaim(struct pool *pp) * Reclaim items from the pool's caches. */ TAILQ_FOREACH(pc, &pp->pr_cachelist, pc_poollist) - pool_cache_reclaim(pc); + pool_cache_reclaim(pc, &pq); s = splclock(); curtime = mono_time; @@ -1338,17 +1510,7 @@ pool_reclaim(struct pool *pp) if (LIST_EMPTY(&pq)) return (0); - while ((ph = LIST_FIRST(&pq)) != NULL) { - LIST_REMOVE(ph, ph_pagelist); - pool_allocator_free(pp, ph->ph_page); - if (pp->pr_roflags & PR_PHINPAGE) { - continue; - } - s = splvm(); - pool_put(&phpool, ph); - splx(s); - } - + pr_pagelist_free(pp, &pq); return (1); } @@ -1429,7 +1591,8 @@ pool_printit(struct pool *pp, const char } static void -pool_print_pagelist(struct pool_pagelist *pl, void (*pr)(const char *, ...)) +pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl, + void (*pr)(const char *, ...)) { struct pool_item_header *ph; #ifdef DIAGNOSTIC @@ -1442,10 +1605,12 @@ pool_print_pagelist(struct pool_pagelist (u_long)ph->ph_time.tv_sec, (u_long)ph->ph_time.tv_usec); #ifdef DIAGNOSTIC - TAILQ_FOREACH(pi, &ph->ph_itemlist, pi_list) { - if (pi->pi_magic != PI_MAGIC) { - (*pr)("\t\t\titem %p, magic 0x%x\n", - pi, pi->pi_magic); + if (!(pp->pr_roflags & PR_NOTOUCH)) { + TAILQ_FOREACH(pi, &ph->ph_itemlist, pi_list) { + if (pi->pi_magic != PI_MAGIC) { + (*pr)("\t\t\titem %p, magic 0x%x\n", + pi, pi->pi_magic); + } } } #endif @@ -1489,13 +1654,13 @@ pool_print1(struct pool *pp, const char if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL) (*pr)("\n\tempty page list:\n"); - pool_print_pagelist(&pp->pr_emptypages, pr); + pool_print_pagelist(pp, &pp->pr_emptypages, pr); if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL) (*pr)("\n\tfull page list:\n"); - pool_print_pagelist(&pp->pr_fullpages, pr); + pool_print_pagelist(pp, &pp->pr_fullpages, pr); if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL) (*pr)("\n\tpartial-page list:\n"); - pool_print_pagelist(&pp->pr_partpages, pr); + pool_print_pagelist(pp, &pp->pr_partpages, pr); if (pp->pr_curpage == NULL) (*pr)("\tno current page\n"); @@ -1561,6 +1726,9 @@ pool_chk_page(struct pool *pp, const cha return 1; } + if ((pp->pr_roflags & PR_NOTOUCH) != 0) + return 0; + for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0; pi != NULL; pi = TAILQ_NEXT(pi,pi_list), n++) { @@ -1853,18 +2021,22 @@ pool_cache_destruct_object(struct pool_c } /* - * pool_cache_do_invalidate: + * pool_cache_invalidate: * - * This internal function implements pool_cache_invalidate() and - * pool_cache_reclaim(). + * Invalidate a pool cache (destruct and release all of the + * cached objects). */ -static void -pool_cache_do_invalidate(struct pool_cache *pc, int free_groups, - void (*putit)(struct pool *, void *)) +void +pool_cache_invalidate(struct pool_cache *pc) { + struct pool_pagelist pq; struct pool_cache_group *pcg, *npcg; void *object; - int s; + + LIST_INIT(&pq); + + simple_lock(&pc->pc_slock); + simple_lock(&pc->pc_pool->pr_slock); for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL; pcg = npcg) { @@ -1876,33 +2048,15 @@ pool_cache_do_invalidate(struct pool_cac pc->pc_allocfrom = NULL; if (pc->pc_dtor != NULL) (*pc->pc_dtor)(pc->pc_arg, object); - (*putit)(pc->pc_pool, object); - } - if (free_groups) { - pc->pc_ngroups--; - TAILQ_REMOVE(&pc->pc_grouplist, pcg, pcg_list); - if (pc->pc_freeto == pcg) - pc->pc_freeto = NULL; - s = splvm(); - pool_put(&pcgpool, pcg); - splx(s); + pool_do_put(pc->pc_pool, object, &pq); } } -} - -/* - * pool_cache_invalidate: - * - * Invalidate a pool cache (destruct and release all of the - * cached objects). - */ -void -pool_cache_invalidate(struct pool_cache *pc) -{ - simple_lock(&pc->pc_slock); - pool_cache_do_invalidate(pc, 0, pool_put); + simple_unlock(&pc->pc_pool->pr_slock); simple_unlock(&pc->pc_slock); + + if (! LIST_EMPTY(&pq)) + pr_pagelist_free(pc->pc_pool, &pq); } /* @@ -1911,11 +2065,42 @@ pool_cache_invalidate(struct pool_cache * Reclaim a pool cache for pool_reclaim(). */ static void -pool_cache_reclaim(struct pool_cache *pc) +pool_cache_reclaim(struct pool_cache *pc, struct pool_pagelist *pq) { + struct pool_cache_group *pcg, *npcg; + void *object; + int s; + + /* + * We're locking in the wrong order (normally pool_cache -> pool, + * but the pool is already locked when we get here), so we have + * to use trylock. If we can't lock the pool_cache, it's not really + * a big deal here. + */ + if (simple_lock_try(&pc->pc_slock) == 0) + return; + + for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL; + pcg = npcg) { + npcg = TAILQ_NEXT(pcg, pcg_list); + while (pcg->pcg_avail != 0) { + pc->pc_nitems--; + object = pcg_get(pcg, NULL); + if (pcg->pcg_avail == 0 && pc->pc_allocfrom == pcg) + pc->pc_allocfrom = NULL; + if (pc->pc_dtor != NULL) + (*pc->pc_dtor)(pc->pc_arg, object); + pool_do_put(pc->pc_pool, object, pq); + } + pc->pc_ngroups--; + TAILQ_REMOVE(&pc->pc_grouplist, pcg, pcg_list); + if (pc->pc_freeto == pcg) + pc->pc_freeto = NULL; + s = splvm(); + pool_put(&pcgpool, pcg); + splx(s); + } - simple_lock(&pc->pc_slock); - pool_cache_do_invalidate(pc, 1, pool_do_put); simple_unlock(&pc->pc_slock); } @@ -1935,23 +2120,42 @@ pool_cache_reclaim(struct pool_cache *pc void *pool_page_alloc(struct pool *, int); void pool_page_free(struct pool *, void *); +#ifdef POOL_SUBPAGE +struct pool_allocator pool_allocator_kmem_fullpage = { + pool_page_alloc, pool_page_free, 0, +}; +#else struct pool_allocator pool_allocator_kmem = { pool_page_alloc, pool_page_free, 0, }; +#endif void *pool_page_alloc_nointr(struct pool *, int); void pool_page_free_nointr(struct pool *, void *); +#ifdef POOL_SUBPAGE +struct pool_allocator pool_allocator_nointr_fullpage = { + pool_page_alloc_nointr, pool_page_free_nointr, 0, +}; +#else struct pool_allocator pool_allocator_nointr = { pool_page_alloc_nointr, pool_page_free_nointr, 0, }; +#endif #ifdef POOL_SUBPAGE void *pool_subpage_alloc(struct pool *, int); void pool_subpage_free(struct pool *, void *); -struct pool_allocator pool_allocator_kmem_subpage = { - pool_subpage_alloc, pool_subpage_free, 0, +struct pool_allocator pool_allocator_kmem = { + pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE, +}; + +void *pool_subpage_alloc_nointr(struct pool *, int); +void pool_subpage_free_nointr(struct pool *, void *); + +struct pool_allocator pool_allocator_nointr = { + pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE, }; #endif /* POOL_SUBPAGE */ @@ -2080,14 +2284,29 @@ pool_page_alloc(struct pool *pp, int fla { boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE; - return ((void *) uvm_km_alloc_poolpage(waitok)); + return ((void *) uvm_km_alloc_poolpage_cache(kmem_map, NULL, waitok)); } void pool_page_free(struct pool *pp, void *v) { - uvm_km_free_poolpage((vaddr_t) v); + uvm_km_free_poolpage_cache(kmem_map, (vaddr_t) v); +} + +static void * +pool_page_alloc_meta(struct pool *pp, int flags) +{ + boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE; + + return ((void *) uvm_km_alloc_poolpage1(kmem_map, NULL, waitok)); +} + +static void +pool_page_free_meta(struct pool *pp, void *v) +{ + + uvm_km_free_poolpage1(kmem_map, (vaddr_t) v); } #ifdef POOL_SUBPAGE @@ -2114,25 +2333,25 @@ pool_subpage_free(struct pool *pp, void /* We don't provide a real nointr allocator. Maybe later. */ void * -pool_page_alloc_nointr(struct pool *pp, int flags) +pool_subpage_alloc_nointr(struct pool *pp, int flags) { return (pool_subpage_alloc(pp, flags)); } void -pool_page_free_nointr(struct pool *pp, void *v) +pool_subpage_free_nointr(struct pool *pp, void *v) { pool_subpage_free(pp, v); } -#else +#endif /* POOL_SUBPAGE */ void * pool_page_alloc_nointr(struct pool *pp, int flags) { boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE; - return ((void *) uvm_km_alloc_poolpage1(kernel_map, + return ((void *) uvm_km_alloc_poolpage_cache(kernel_map, uvm.kernel_object, waitok)); } @@ -2140,6 +2359,5 @@ void pool_page_free_nointr(struct pool *pp, void *v) { - uvm_km_free_poolpage1(kernel_map, (vaddr_t) v); + uvm_km_free_poolpage_cache(kernel_map, (vaddr_t) v); } -#endif /* POOL_SUBPAGE */