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.197 retrieving revision 1.227 diff -u -p -r1.197 -r1.227 --- src/sys/kern/subr_pool.c 2012/06/05 22:51:47 1.197 +++ src/sys/kern/subr_pool.c 2018/09/10 13:11:05 1.227 @@ -1,13 +1,14 @@ -/* $NetBSD: subr_pool.c,v 1.197 2012/06/05 22:51:47 jym Exp $ */ +/* $NetBSD: subr_pool.c,v 1.227 2018/09/10 13:11:05 maxv Exp $ */ /*- - * Copyright (c) 1997, 1999, 2000, 2002, 2007, 2008, 2010 + * Copyright (c) 1997, 1999, 2000, 2002, 2007, 2008, 2010, 2014, 2015 * The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace - * Simulation Facility, NASA Ames Research Center, and by Andrew Doran. + * Simulation Facility, NASA Ames Research Center; by Andrew Doran, and by + * Maxime Villard. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -32,13 +33,16 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.197 2012/06/05 22:51:47 jym Exp $"); +__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.227 2018/09/10 13:11:05 maxv Exp $"); +#ifdef _KERNEL_OPT #include "opt_ddb.h" #include "opt_lockdebug.h" +#endif #include #include +#include #include #include #include @@ -51,6 +55,7 @@ __KERNEL_RCSID(0, "$NetBSD: subr_pool.c, #include #include #include +#include #include @@ -67,8 +72,8 @@ __KERNEL_RCSID(0, "$NetBSD: subr_pool.c, * an internal pool of page headers (`phpool'). */ -/* List of all pools */ -static TAILQ_HEAD(, pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head); +/* List of all pools. Non static as needed by 'vmstat -m' */ +TAILQ_HEAD(, pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head); /* Private pool for page header structures */ #define PHPOOL_MAX 8 @@ -81,6 +86,25 @@ static struct pool phpool[PHPOOL_MAX]; static struct pool psppool; #endif +#if defined(KASAN) +#define POOL_REDZONE +#endif + +#ifdef POOL_REDZONE +# ifdef KASAN +# define POOL_REDZONE_SIZE 8 +# else +# define POOL_REDZONE_SIZE 2 +# endif +static void pool_redzone_init(struct pool *, size_t); +static void pool_redzone_fill(struct pool *, void *); +static void pool_redzone_check(struct pool *, void *); +#else +# define pool_redzone_init(pp, sz) /* NOTHING */ +# define pool_redzone_fill(pp, ptr) /* NOTHING */ +# define pool_redzone_check(pp, ptr) /* NOTHING */ +#endif + static void *pool_page_alloc_meta(struct pool *, int); static void pool_page_free_meta(struct pool *, void *); @@ -91,6 +115,10 @@ struct pool_allocator pool_allocator_met .pa_pagesz = 0 }; +#define POOL_ALLOCATOR_BIG_BASE 13 +extern struct pool_allocator pool_allocator_big[]; +static int pool_bigidx(size_t); + /* # of seconds to retain page after last use */ int pool_inactive_time = 10; @@ -203,9 +231,9 @@ static void *pool_allocator_alloc(struct static void pool_allocator_free(struct pool *, void *); static void pool_print_pagelist(struct pool *, struct pool_pagelist *, - void (*)(const char *, ...)); + void (*)(const char *, ...) __printflike(1, 2)); static void pool_print1(struct pool *, const char *, - void (*)(const char *, ...)); + void (*)(const char *, ...) __printflike(1, 2)); static int pool_chk_page(struct pool *, const char *, struct pool_item_header *); @@ -229,7 +257,7 @@ pr_item_notouch_put(const struct pool *p { unsigned int idx = pr_item_notouch_index(pp, ph, obj); pool_item_bitmap_t *bitmap = ph->ph_bitmap + (idx / BITMAP_SIZE); - pool_item_bitmap_t mask = 1 << (idx & BITMAP_MASK); + pool_item_bitmap_t mask = 1U << (idx & BITMAP_MASK); KASSERT((*bitmap & mask) == 0); *bitmap |= mask; @@ -252,7 +280,7 @@ pr_item_notouch_get(const struct pool *p bit--; idx = (i * BITMAP_SIZE) + bit; - mask = 1 << bit; + mask = 1U << bit; KASSERT((bitmap[i] & mask) != 0); bitmap[i] &= ~mask; break; @@ -367,12 +395,10 @@ pr_rmpage(struct pool *pp, struct pool_i * If the page was idle, decrement the idle page count. */ if (ph->ph_nmissing == 0) { -#ifdef DIAGNOSTIC - if (pp->pr_nidle == 0) - panic("pr_rmpage: nidle inconsistent"); - if (pp->pr_nitems < pp->pr_itemsperpage) - panic("pr_rmpage: nitems inconsistent"); -#endif + KASSERT(pp->pr_nidle != 0); + KASSERTMSG((pp->pr_nitems >= pp->pr_itemsperpage), + "nitems=%u < itemsperpage=%u", + pp->pr_nitems, pp->pr_itemsperpage); pp->pr_nidle--; } @@ -458,19 +484,23 @@ pool_init(struct pool *pp, size_t size, const char *wchan, struct pool_allocator *palloc, int ipl) { struct pool *pp1; - size_t trysize, phsize; + size_t trysize, phsize, prsize; int off, slack; #ifdef DEBUG + if (__predict_true(!cold)) + mutex_enter(&pool_head_lock); /* * Check that the pool hasn't already been initialised and * added to the list of all pools. */ TAILQ_FOREACH(pp1, &pool_head, pr_poollist) { if (pp == pp1) - panic("pool_init: pool %s already initialised", + panic("%s: [%s] already initialised", __func__, wchan); } + if (__predict_true(!cold)) + mutex_exit(&pool_head_lock); #endif if (palloc == NULL) @@ -501,14 +531,14 @@ pool_init(struct pool *pp, size_t size, if (align == 0) align = ALIGN(1); - if ((flags & PR_NOTOUCH) == 0 && size < sizeof(struct pool_item)) - size = sizeof(struct pool_item); - - size = roundup(size, align); -#ifdef DIAGNOSTIC - if (size > palloc->pa_pagesz) - panic("pool_init: pool item size (%zu) too large", size); -#endif + prsize = size; + if ((flags & PR_NOTOUCH) == 0 && prsize < sizeof(struct pool_item)) + prsize = sizeof(struct pool_item); + + prsize = roundup(prsize, align); + KASSERTMSG((prsize <= palloc->pa_pagesz), + "%s: [%s] pool item size (%zu) larger than page size (%u)", + __func__, wchan, prsize, palloc->pa_pagesz); /* * Initialize the pool structure. @@ -524,7 +554,7 @@ pool_init(struct pool *pp, size_t size, pp->pr_maxpages = UINT_MAX; pp->pr_roflags = flags; pp->pr_flags = 0; - pp->pr_size = size; + pp->pr_size = prsize; pp->pr_align = align; pp->pr_wchan = wchan; pp->pr_alloc = palloc; @@ -539,6 +569,7 @@ pool_init(struct pool *pp, size_t size, pp->pr_drain_hook = NULL; pp->pr_drain_hook_arg = NULL; pp->pr_freecheck = NULL; + pool_redzone_init(pp, size); /* * Decide whether to put the page header off page to avoid @@ -557,9 +588,10 @@ 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_roflags & (PR_NOTOUCH | PR_NOALIGN)) == 0 && + if (pp->pr_roflags & PR_PHINPAGE || + ((pp->pr_roflags & (PR_NOTOUCH | PR_NOALIGN)) == 0 && (pp->pr_size < MIN(palloc->pa_pagesz / 16, phsize << 3) || - trysize / pp->pr_size == (trysize - phsize) / pp->pr_size)) { + 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; @@ -589,7 +621,8 @@ pool_init(struct pool *pp, size_t size, * if you see this panic, consider to tweak * PHPOOL_MAX and PHPOOL_FREELIST_NELEM. */ - panic("%s: too large itemsperpage(%d) for PR_NOTOUCH", + panic("%s: [%s] too large itemsperpage(%d) for " + "PR_NOTOUCH", __func__, pp->pr_wchan, pp->pr_itemsperpage); } pp->pr_phpool = &phpool[idx]; @@ -676,14 +709,8 @@ pool_destroy(struct pool *pp) mutex_enter(&pp->pr_lock); KASSERT(pp->pr_cache == NULL); - -#ifdef DIAGNOSTIC - if (pp->pr_nout != 0) { - panic("pool_destroy: pool busy: still out: %u", - pp->pr_nout); - } -#endif - + KASSERTMSG((pp->pr_nout == 0), + "%s: pool busy: still out: %u", __func__, pp->pr_nout); KASSERT(LIST_EMPTY(&pp->pr_fullpages)); KASSERT(LIST_EMPTY(&pp->pr_partpages)); @@ -704,10 +731,8 @@ pool_set_drain_hook(struct pool *pp, voi { /* XXX no locking -- must be used just after pool_init() */ -#ifdef DIAGNOSTIC - if (pp->pr_drain_hook != NULL) - panic("pool_set_drain_hook(%s): already set", pp->pr_wchan); -#endif + KASSERTMSG((pp->pr_drain_hook == NULL), + "%s: [%s] already set", __func__, pp->pr_wchan); pp->pr_drain_hook = fn; pp->pr_drain_hook_arg = arg; } @@ -718,7 +743,7 @@ pool_alloc_item_header(struct pool *pp, struct pool_item_header *ph; if ((pp->pr_roflags & PR_PHINPAGE) != 0) - ph = (struct pool_item_header *) ((char *)storage + pp->pr_phoffset); + ph = (void *)((char *)storage + pp->pr_phoffset); else ph = pool_get(pp->pr_phpool, flags); @@ -735,15 +760,14 @@ pool_get(struct pool *pp, int flags) struct pool_item_header *ph; void *v; -#ifdef DIAGNOSTIC - if (pp->pr_itemsperpage == 0) - panic("pool_get: pool '%s': pr_itemsperpage is zero, " - "pool not initialized?", pp->pr_wchan); - if ((cpu_intr_p() || cpu_softintr_p()) && pp->pr_ipl == IPL_NONE && - !cold && panicstr == NULL) - panic("pool '%s' is IPL_NONE, but called from " - "interrupt context\n", pp->pr_wchan); -#endif + KASSERT(!(flags & PR_NOWAIT) != !(flags & PR_WAITOK)); + KASSERTMSG((pp->pr_itemsperpage != 0), + "%s: [%s] pr_itemsperpage is zero, " + "pool not initialized?", __func__, pp->pr_wchan); + KASSERTMSG((!(cpu_intr_p() || cpu_softintr_p()) + || pp->pr_ipl != IPL_NONE || cold || panicstr != NULL), + "%s: [%s] is IPL_NONE, but called from interrupt context", + __func__, pp->pr_wchan); if (flags & PR_WAITOK) { ASSERT_SLEEPABLE(); } @@ -755,12 +779,8 @@ pool_get(struct pool *pp, int flags) * and we can wait, then wait until an item has been returned to * the pool. */ -#ifdef DIAGNOSTIC - if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) { - mutex_exit(&pp->pr_lock); - panic("pool_get: %s: crossed hard limit", pp->pr_wchan); - } -#endif + KASSERTMSG((pp->pr_nout <= pp->pr_hardlimit), + "%s: %s: crossed hard limit", __func__, pp->pr_wchan); if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) { if (pp->pr_drain_hook != NULL) { /* @@ -781,7 +801,9 @@ pool_get(struct pool *pp, int flags) * it be? */ pp->pr_flags |= PR_WANTED; - cv_wait(&pp->pr_cv, &pp->pr_lock); + do { + cv_wait(&pp->pr_cv, &pp->pr_lock); + } while (pp->pr_flags & PR_WANTED); goto startover; } @@ -796,6 +818,7 @@ pool_get(struct pool *pp, int flags) pp->pr_nfail++; mutex_exit(&pp->pr_lock); + KASSERT((flags & (PR_NOWAIT|PR_LIMITFAIL)) != 0); return (NULL); } @@ -808,14 +831,9 @@ pool_get(struct pool *pp, int flags) if ((ph = pp->pr_curpage) == NULL) { int error; -#ifdef DIAGNOSTIC - if (pp->pr_nitems != 0) { - mutex_exit(&pp->pr_lock); - printf("pool_get: %s: curpage NULL, nitems %u\n", - pp->pr_wchan, pp->pr_nitems); - panic("pool_get: nitems inconsistent"); - } -#endif + KASSERTMSG((pp->pr_nitems == 0), + "%s: [%s] curpage NULL, inconsistent nitems %u", + __func__, pp->pr_wchan, pp->pr_nitems); /* * Call the back-end page allocator for more memory. @@ -825,6 +843,14 @@ pool_get(struct pool *pp, int flags) error = pool_grow(pp, flags); if (error != 0) { /* + * pool_grow aborts when another thread + * is allocating a new page. Retry if it + * waited for it. + */ + if (error == ERESTART) + goto startover; + + /* * We were unable to allocate a page or item * header, but we released the lock during * allocation, so perhaps items were freed @@ -835,6 +861,7 @@ pool_get(struct pool *pp, int flags) pp->pr_nfail++; mutex_exit(&pp->pr_lock); + KASSERT((flags & (PR_WAITOK|PR_NOWAIT)) == PR_NOWAIT); return (NULL); } @@ -842,35 +869,22 @@ pool_get(struct pool *pp, int flags) goto startover; } if (pp->pr_roflags & PR_NOTOUCH) { -#ifdef DIAGNOSTIC - if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) { - mutex_exit(&pp->pr_lock); - panic("pool_get: %s: page empty", pp->pr_wchan); - } -#endif + KASSERTMSG((ph->ph_nmissing < pp->pr_itemsperpage), + "%s: %s: page empty", __func__, pp->pr_wchan); v = pr_item_notouch_get(pp, ph); } else { v = pi = LIST_FIRST(&ph->ph_itemlist); if (__predict_false(v == NULL)) { mutex_exit(&pp->pr_lock); - panic("pool_get: %s: page empty", pp->pr_wchan); - } -#ifdef DIAGNOSTIC - if (__predict_false(pp->pr_nitems == 0)) { - mutex_exit(&pp->pr_lock); - printf("pool_get: %s: items on itemlist, nitems %u\n", - pp->pr_wchan, pp->pr_nitems); - panic("pool_get: nitems inconsistent"); + panic("%s: [%s] page empty", __func__, pp->pr_wchan); } -#endif - -#ifdef DIAGNOSTIC - if (__predict_false(pi->pi_magic != PI_MAGIC)) { - 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 + KASSERTMSG((pp->pr_nitems > 0), + "%s: [%s] nitems %u inconsistent on itemlist", + __func__, pp->pr_wchan, pp->pr_nitems); + KASSERTMSG((pi->pi_magic == PI_MAGIC), + "%s: [%s] free list modified: " + "magic=%x; page %p; item addr %p", __func__, + pp->pr_wchan, pi->pi_magic, ph->ph_page, pi); /* * Remove from item list. @@ -880,10 +894,7 @@ pool_get(struct pool *pp, int flags) pp->pr_nitems--; pp->pr_nout++; if (ph->ph_nmissing == 0) { -#ifdef DIAGNOSTIC - if (__predict_false(pp->pr_nidle == 0)) - panic("pool_get: nidle inconsistent"); -#endif + KASSERT(pp->pr_nidle > 0); pp->pr_nidle--; /* @@ -895,14 +906,10 @@ pool_get(struct pool *pp, int flags) } ph->ph_nmissing++; if (ph->ph_nmissing == pp->pr_itemsperpage) { -#ifdef DIAGNOSTIC - if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 && - !LIST_EMPTY(&ph->ph_itemlist))) { - mutex_exit(&pp->pr_lock); - panic("pool_get: %s: nmissing inconsistent", - pp->pr_wchan); - } -#endif + KASSERTMSG(((pp->pr_roflags & PR_NOTOUCH) || + LIST_EMPTY(&ph->ph_itemlist)), + "%s: [%s] nmissing (%u) inconsistent", __func__, + pp->pr_wchan, ph->ph_nmissing); /* * This page is now full. Move it to the full list * and select a new current page. @@ -929,6 +936,7 @@ pool_get(struct pool *pp, int flags) mutex_exit(&pp->pr_lock); KASSERT((((vaddr_t)v + pp->pr_itemoffset) & (pp->pr_align - 1)) == 0); FREECHECK_OUT(&pp->pr_freecheck, v); + pool_redzone_fill(pp, v); return (v); } @@ -942,19 +950,15 @@ pool_do_put(struct pool *pp, void *v, st struct pool_item_header *ph; KASSERT(mutex_owned(&pp->pr_lock)); + pool_redzone_check(pp, v); FREECHECK_IN(&pp->pr_freecheck, v); LOCKDEBUG_MEM_CHECK(v, pp->pr_size); -#ifdef DIAGNOSTIC - if (__predict_false(pp->pr_nout == 0)) { - printf("pool %s: putting with none out\n", - pp->pr_wchan); - panic("pool_put"); - } -#endif + KASSERTMSG((pp->pr_nout > 0), + "%s: [%s] putting with none out", __func__, pp->pr_wchan); if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) { - panic("pool_put: %s: page header missing", pp->pr_wchan); + panic("%s: [%s] page header missing", __func__, pp->pr_wchan); } /* @@ -1067,26 +1071,65 @@ pool_put(struct pool *pp, void *v) static int pool_grow(struct pool *pp, int flags) { - struct pool_item_header *ph = NULL; - char *cp; - - mutex_exit(&pp->pr_lock); - cp = pool_allocator_alloc(pp, flags); - if (__predict_true(cp != NULL)) { - ph = pool_alloc_item_header(pp, cp, flags); - } - if (__predict_false(cp == NULL || ph == NULL)) { - if (cp != NULL) { - pool_allocator_free(pp, cp); + /* + * If there's a pool_grow in progress, wait for it to complete + * and try again from the top. + */ + if (pp->pr_flags & PR_GROWING) { + if (flags & PR_WAITOK) { + do { + cv_wait(&pp->pr_cv, &pp->pr_lock); + } while (pp->pr_flags & PR_GROWING); + return ERESTART; + } else { + if (pp->pr_flags & PR_GROWINGNOWAIT) { + /* + * This needs an unlock/relock dance so + * that the other caller has a chance to + * run and actually do the thing. Note + * that this is effectively a busy-wait. + */ + mutex_exit(&pp->pr_lock); + mutex_enter(&pp->pr_lock); + return ERESTART; + } + return EWOULDBLOCK; } - mutex_enter(&pp->pr_lock); - return ENOMEM; } + pp->pr_flags |= PR_GROWING; + if (flags & PR_WAITOK) + mutex_exit(&pp->pr_lock); + else + pp->pr_flags |= PR_GROWINGNOWAIT; - mutex_enter(&pp->pr_lock); + char *cp = pool_allocator_alloc(pp, flags); + if (__predict_false(cp == NULL)) + goto out; + + struct pool_item_header *ph = pool_alloc_item_header(pp, cp, flags); + if (__predict_false(ph == NULL)) { + pool_allocator_free(pp, cp); + goto out; + } + + if (flags & PR_WAITOK) + mutex_enter(&pp->pr_lock); pool_prime_page(pp, cp, ph); pp->pr_npagealloc++; + KASSERT(pp->pr_flags & PR_GROWING); + pp->pr_flags &= ~(PR_GROWING|PR_GROWINGNOWAIT); + /* + * If anyone was waiting for pool_grow, notify them that we + * may have just done it. + */ + cv_broadcast(&pp->pr_cv); return 0; +out: + if (flags & PR_WAITOK) + mutex_enter(&pp->pr_lock); + KASSERT(pp->pr_flags & PR_GROWING); + pp->pr_flags &= ~(PR_GROWING|PR_GROWINGNOWAIT); + return ENOMEM; } /* @@ -1102,12 +1145,15 @@ pool_prime(struct pool *pp, int n) newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; - while (newpages-- > 0) { + while (newpages > 0) { error = pool_grow(pp, PR_NOWAIT); if (error) { + if (error == ERESTART) + continue; break; } pp->pr_minpages++; + newpages--; } if (pp->pr_minpages >= pp->pr_maxpages) @@ -1132,12 +1178,9 @@ pool_prime_page(struct pool *pp, void *s int n; KASSERT(mutex_owned(&pp->pr_lock)); - -#ifdef DIAGNOSTIC - if ((pp->pr_roflags & PR_NOALIGN) == 0 && - ((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0) - panic("pool_prime_page: %s: unaligned page", pp->pr_wchan); -#endif + KASSERTMSG(((pp->pr_roflags & PR_NOALIGN) || + (((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) == 0)), + "%s: [%s] unaligned page: %p", __func__, pp->pr_wchan, cp); /* * Insert page header. @@ -1220,6 +1263,8 @@ pool_catchup(struct pool *pp) while (POOL_NEEDS_CATCHUP(pp)) { error = pool_grow(pp, PR_NOWAIT); if (error) { + if (error == ERESTART) + continue; break; } } @@ -1433,6 +1478,28 @@ pool_drain(struct pool **ppp) } /* + * Calculate the total number of pages consumed by pools. + */ +int +pool_totalpages(void) +{ + struct pool *pp; + uint64_t total = 0; + + mutex_enter(&pool_head_lock); + TAILQ_FOREACH(pp, &pool_head, pr_poollist) { + uint64_t bytes = pp->pr_npages * pp->pr_alloc->pa_pagesz; + + if ((pp->pr_roflags & PR_RECURSIVE) != 0) + bytes -= (pp->pr_nout * pp->pr_size); + total += bytes; + } + mutex_exit(&pool_head_lock); + + return atop(total); +} + +/* * Diagnostic helpers. */ @@ -1463,9 +1530,7 @@ pool_print_pagelist(struct pool *pp, str void (*pr)(const char *, ...)) { struct pool_item_header *ph; -#ifdef DIAGNOSTIC - struct pool_item *pi; -#endif + struct pool_item *pi __diagused; LIST_FOREACH(ph, pl, ph_pagelist) { (*pr)("\t\tpage %p, nmissing %d, time %" PRIu32 "\n", @@ -1727,8 +1792,14 @@ pool_cache_bootstrap(pool_cache_t pc, si struct pool *pp; pp = &pc->pc_pool; - if (palloc == NULL && ipl == IPL_NONE) - palloc = &pool_allocator_nointr; + if (palloc == NULL && ipl == IPL_NONE) { + if (size > PAGE_SIZE) { + int bigidx = pool_bigidx(size); + + palloc = &pool_allocator_big[bigidx]; + } else + palloc = &pool_allocator_nointr; + } pool_init(pp, size, align, align_offset, flags, wchan, palloc, ipl); mutex_init(&pc->pc_lock, MUTEX_DEFAULT, ipl); @@ -2161,8 +2232,10 @@ pool_cache_get_slow(pool_cache_cpu_t *cc object = pool_get(&pc->pc_pool, flags); *objectp = object; - if (__predict_false(object == NULL)) + if (__predict_false(object == NULL)) { + KASSERT((flags & (PR_WAITOK|PR_NOWAIT)) == PR_NOWAIT); return false; + } if (__predict_false((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0)) { pool_put(&pc->pc_pool, object); @@ -2182,6 +2255,7 @@ pool_cache_get_slow(pool_cache_cpu_t *cc } FREECHECK_OUT(&pc->pc_freecheck, object); + pool_redzone_fill(&pc->pc_pool, object); return false; } @@ -2199,10 +2273,11 @@ pool_cache_get_paddr(pool_cache_t pc, in void *object; int s; + KASSERT(!(flags & PR_NOWAIT) != !(flags & PR_WAITOK)); KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()) || (pc->pc_pool.pr_ipl != IPL_NONE || cold || panicstr != NULL), - "pool '%s' is IPL_NONE, but called from interrupt context\n", - pc->pc_pool.pr_wchan); + "%s: [%s] is IPL_NONE, but called from interrupt context", + __func__, pc->pc_pool.pr_wchan); if (flags & PR_WAITOK) { ASSERT_SLEEPABLE(); @@ -2227,6 +2302,7 @@ pool_cache_get_paddr(pool_cache_t pc, in cc->cc_hits++; splx(s); FREECHECK_OUT(&pc->pc_freecheck, object); + pool_redzone_fill(&pc->pc_pool, object); return object; } @@ -2251,12 +2327,18 @@ pool_cache_get_paddr(pool_cache_t pc, in break; } + /* + * We would like to KASSERT(object || (flags & PR_NOWAIT)), but + * pool_cache_get can fail even in the PR_WAITOK case, if the + * constructor fails. + */ return object; } static bool __noinline pool_cache_put_slow(pool_cache_cpu_t *cc, int s, void *object) { + struct lwp *l = curlwp; pcg_t *pcg, *cur; uint64_t ncsw; pool_cache_t pc; @@ -2267,6 +2349,7 @@ pool_cache_put_slow(pool_cache_cpu_t *cc pc = cc->cc_cache; pcg = NULL; cc->cc_misses++; + ncsw = l->l_ncsw; /* * If there are no empty groups in the cache then allocate one @@ -2276,6 +2359,16 @@ pool_cache_put_slow(pool_cache_cpu_t *cc if (__predict_true(!pool_cache_disable)) { pcg = pool_get(pc->pc_pcgpool, PR_NOWAIT); } + /* + * If pool_get() blocked, then our view of + * the per-CPU data is invalid: retry. + */ + if (__predict_false(l->l_ncsw != ncsw)) { + if (pcg != NULL) { + pool_put(pc->pc_pcgpool, pcg); + } + return true; + } if (__predict_true(pcg != NULL)) { pcg->pcg_avail = 0; pcg->pcg_size = pc->pc_pcgsize; @@ -2284,7 +2377,6 @@ pool_cache_put_slow(pool_cache_cpu_t *cc /* Lock the cache. */ if (__predict_false(!mutex_tryenter(&pc->pc_lock))) { - ncsw = curlwp->l_ncsw; mutex_enter(&pc->pc_lock); pc->pc_contended++; @@ -2292,7 +2384,7 @@ pool_cache_put_slow(pool_cache_cpu_t *cc * If we context switched while locking, then our view of * the per-CPU data is invalid: retry. */ - if (__predict_false(curlwp->l_ncsw != ncsw)) { + if (__predict_false(l->l_ncsw != ncsw)) { mutex_exit(&pc->pc_lock); if (pcg != NULL) { pool_put(pc->pc_pcgpool, pcg); @@ -2359,6 +2451,7 @@ pool_cache_put_paddr(pool_cache_t pc, vo int s; KASSERT(object != NULL); + pool_redzone_check(&pc->pc_pool, object); FREECHECK_IN(&pc->pc_freecheck, object); /* Lock out interrupts and disable preemption. */ @@ -2511,6 +2604,61 @@ struct pool_allocator pool_allocator_noi }; #endif /* POOL_SUBPAGE */ +struct pool_allocator pool_allocator_big[] = { + { + .pa_alloc = pool_page_alloc, + .pa_free = pool_page_free, + .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 0), + }, + { + .pa_alloc = pool_page_alloc, + .pa_free = pool_page_free, + .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 1), + }, + { + .pa_alloc = pool_page_alloc, + .pa_free = pool_page_free, + .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 2), + }, + { + .pa_alloc = pool_page_alloc, + .pa_free = pool_page_free, + .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 3), + }, + { + .pa_alloc = pool_page_alloc, + .pa_free = pool_page_free, + .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 4), + }, + { + .pa_alloc = pool_page_alloc, + .pa_free = pool_page_free, + .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 5), + }, + { + .pa_alloc = pool_page_alloc, + .pa_free = pool_page_free, + .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 6), + }, + { + .pa_alloc = pool_page_alloc, + .pa_free = pool_page_free, + .pa_pagesz = 1 << (POOL_ALLOCATOR_BIG_BASE + 7), + } +}; + +static int +pool_bigidx(size_t size) +{ + int i; + + for (i = 0; i < __arraycount(pool_allocator_big); i++) { + if (1 << (i + POOL_ALLOCATOR_BIG_BASE) >= size) + return i; + } + panic("pool item size %zu too large, use a custom allocator", size); +} + static void * pool_allocator_alloc(struct pool *pp, int flags) { @@ -2580,6 +2728,141 @@ pool_page_free_meta(struct pool *pp, voi vmem_free(kmem_meta_arena, (vmem_addr_t)v, pp->pr_alloc->pa_pagesz); } +#ifdef POOL_REDZONE +#if defined(_LP64) +# define PRIME 0x9e37fffffffc0000UL +#else /* defined(_LP64) */ +# define PRIME 0x9e3779b1 +#endif /* defined(_LP64) */ +#define STATIC_BYTE 0xFE +CTASSERT(POOL_REDZONE_SIZE > 1); + +#ifndef KASAN +static inline uint8_t +pool_pattern_generate(const void *p) +{ + return (uint8_t)(((uintptr_t)p) * PRIME + >> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT); +} +#endif + +static void +pool_redzone_init(struct pool *pp, size_t requested_size) +{ + size_t redzsz; + size_t nsz; + +#ifdef KASAN + redzsz = requested_size; + kasan_add_redzone(&redzsz); + redzsz -= requested_size; +#else + redzsz = POOL_REDZONE_SIZE; +#endif + + if (pp->pr_roflags & PR_NOTOUCH) { + pp->pr_reqsize = 0; + pp->pr_redzone = false; + return; + } + + /* + * We may have extended the requested size earlier; check if + * there's naturally space in the padding for a red zone. + */ + if (pp->pr_size - requested_size >= redzsz) { + pp->pr_reqsize = requested_size; + pp->pr_redzone = true; + return; + } + + /* + * No space in the natural padding; check if we can extend a + * bit the size of the pool. + */ + nsz = roundup(pp->pr_size + redzsz, pp->pr_align); + if (nsz <= pp->pr_alloc->pa_pagesz) { + /* Ok, we can */ + pp->pr_size = nsz; + pp->pr_reqsize = requested_size; + pp->pr_redzone = true; + } else { + /* No space for a red zone... snif :'( */ + pp->pr_reqsize = 0; + pp->pr_redzone = false; + printf("pool redzone disabled for '%s'\n", pp->pr_wchan); + } +} + +static void +pool_redzone_fill(struct pool *pp, void *p) +{ + if (!pp->pr_redzone) + return; +#ifdef KASAN + size_t size_with_redzone = pp->pr_reqsize; + kasan_add_redzone(&size_with_redzone); + kasan_alloc(p, pp->pr_reqsize, size_with_redzone); +#else + uint8_t *cp, pat; + const uint8_t *ep; + + cp = (uint8_t *)p + pp->pr_reqsize; + ep = cp + POOL_REDZONE_SIZE; + + /* + * We really don't want the first byte of the red zone to be '\0'; + * an off-by-one in a string may not be properly detected. + */ + pat = pool_pattern_generate(cp); + *cp = (pat == '\0') ? STATIC_BYTE: pat; + cp++; + + while (cp < ep) { + *cp = pool_pattern_generate(cp); + cp++; + } +#endif +} + +static void +pool_redzone_check(struct pool *pp, void *p) +{ + if (!pp->pr_redzone) + return; +#ifdef KASAN + size_t size_with_redzone = pp->pr_reqsize; + kasan_add_redzone(&size_with_redzone); + kasan_free(p, size_with_redzone); +#else + uint8_t *cp, pat, expected; + const uint8_t *ep; + + cp = (uint8_t *)p + pp->pr_reqsize; + ep = cp + POOL_REDZONE_SIZE; + + pat = pool_pattern_generate(cp); + expected = (pat == '\0') ? STATIC_BYTE: pat; + if (__predict_false(expected != *cp)) { + printf("%s: %p: 0x%02x != 0x%02x\n", + __func__, cp, *cp, expected); + } + cp++; + + while (cp < ep) { + expected = pool_pattern_generate(cp); + if (__predict_false(*cp != expected)) { + printf("%s: %p: 0x%02x != 0x%02x\n", + __func__, cp, *cp, expected); + } + cp++; + } +#endif +} + +#endif /* POOL_REDZONE */ + + #ifdef POOL_SUBPAGE /* Sub-page allocator, for machines with large hardware pages. */ void * @@ -2735,3 +3018,100 @@ print: } } #endif /* defined(DDB) */ + +static int +pool_sysctl(SYSCTLFN_ARGS) +{ + struct pool_sysctl data; + struct pool *pp; + struct pool_cache *pc; + pool_cache_cpu_t *cc; + int error; + size_t i, written; + + if (oldp == NULL) { + *oldlenp = 0; + TAILQ_FOREACH(pp, &pool_head, pr_poollist) + *oldlenp += sizeof(data); + return 0; + } + + memset(&data, 0, sizeof(data)); + error = 0; + written = 0; + TAILQ_FOREACH(pp, &pool_head, pr_poollist) { + if (written + sizeof(data) > *oldlenp) + break; + strlcpy(data.pr_wchan, pp->pr_wchan, sizeof(data.pr_wchan)); + data.pr_pagesize = pp->pr_alloc->pa_pagesz; + data.pr_flags = pp->pr_roflags | pp->pr_flags; +#define COPY(field) data.field = pp->field + COPY(pr_size); + + COPY(pr_itemsperpage); + COPY(pr_nitems); + COPY(pr_nout); + COPY(pr_hardlimit); + COPY(pr_npages); + COPY(pr_minpages); + COPY(pr_maxpages); + + COPY(pr_nget); + COPY(pr_nfail); + COPY(pr_nput); + COPY(pr_npagealloc); + COPY(pr_npagefree); + COPY(pr_hiwat); + COPY(pr_nidle); +#undef COPY + + data.pr_cache_nmiss_pcpu = 0; + data.pr_cache_nhit_pcpu = 0; + if (pp->pr_cache) { + pc = pp->pr_cache; + data.pr_cache_meta_size = pc->pc_pcgsize; + data.pr_cache_nfull = pc->pc_nfull; + data.pr_cache_npartial = pc->pc_npart; + data.pr_cache_nempty = pc->pc_nempty; + data.pr_cache_ncontended = pc->pc_contended; + data.pr_cache_nmiss_global = pc->pc_misses; + data.pr_cache_nhit_global = pc->pc_hits; + for (i = 0; i < pc->pc_ncpu; ++i) { + cc = pc->pc_cpus[i]; + if (cc == NULL) + continue; + data.pr_cache_nmiss_pcpu += cc->cc_misses; + data.pr_cache_nhit_pcpu += cc->cc_hits; + } + } else { + data.pr_cache_meta_size = 0; + data.pr_cache_nfull = 0; + data.pr_cache_npartial = 0; + data.pr_cache_nempty = 0; + data.pr_cache_ncontended = 0; + data.pr_cache_nmiss_global = 0; + data.pr_cache_nhit_global = 0; + } + + error = sysctl_copyout(l, &data, oldp, sizeof(data)); + if (error) + break; + written += sizeof(data); + oldp = (char *)oldp + sizeof(data); + } + + *oldlenp = written; + return error; +} + +SYSCTL_SETUP(sysctl_pool_setup, "sysctl kern.pool setup") +{ + const struct sysctlnode *rnode = NULL; + + sysctl_createv(clog, 0, NULL, &rnode, + CTLFLAG_PERMANENT, + CTLTYPE_STRUCT, "pool", + SYSCTL_DESCR("Get pool statistics"), + pool_sysctl, 0, NULL, 0, + CTL_KERN, CTL_CREATE, CTL_EOL); +}