1f2860039SMatthew Dillon /*- 273108a16SWarner Losh * Copyright (c) 2001 Matthew Dillon. All Rights Reserved. 3f2860039SMatthew Dillon * 473108a16SWarner Losh * Redistribution and use in source and binary forms, with or without 573108a16SWarner Losh * modification, are permitted provided that the following conditions 673108a16SWarner Losh * are met: 773108a16SWarner Losh * 1. Redistributions of source code must retain the above copyright 873108a16SWarner Losh * notice, this list of conditions and the following disclaimer. 973108a16SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 1073108a16SWarner Losh * notice, this list of conditions and the following disclaimer in the 1173108a16SWarner Losh * documentation and/or other materials provided with the distribution. 1273108a16SWarner Losh * 1373108a16SWarner Losh * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1473108a16SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1573108a16SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1673108a16SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 1773108a16SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1873108a16SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1973108a16SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2073108a16SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2173108a16SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2273108a16SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2373108a16SWarner Losh * SUCH DAMAGE. 2473108a16SWarner Losh */ 2573108a16SWarner Losh 2673108a16SWarner Losh /* Mutex pool routines. These routines are designed to be used as short 27e1e8f51bSRobert Watson * term leaf mutexes (e.g. the last mutex you might acquire other then 28f2860039SMatthew Dillon * calling msleep()). They operate using a shared pool. A mutex is chosen 29f2860039SMatthew Dillon * from the pool based on the supplied pointer (which may or may not be 30f2860039SMatthew Dillon * valid). 31f2860039SMatthew Dillon * 32f2860039SMatthew Dillon * Advantages: 33f2860039SMatthew Dillon * - no structural overhead. Mutexes can be associated with structures 34f2860039SMatthew Dillon * without adding bloat to the structures. 35f2860039SMatthew Dillon * - mutexes can be obtained for invalid pointers, useful when uses 36f2860039SMatthew Dillon * mutexes to interlock destructor ops. 37fca73711SMaxime Henrion * - no initialization/destructor overhead. 38f2860039SMatthew Dillon * - can be used with msleep. 39f2860039SMatthew Dillon * 40f2860039SMatthew Dillon * Disadvantages: 41fca73711SMaxime Henrion * - should generally only be used as leaf mutexes. 42f2860039SMatthew Dillon * - pool/pool dependancy ordering cannot be depended on. 43fca73711SMaxime Henrion * - possible L1 cache mastersip contention between cpus. 44f2860039SMatthew Dillon */ 45f2860039SMatthew Dillon 46677b542eSDavid E. O'Brien #include <sys/cdefs.h> 47677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 48677b542eSDavid E. O'Brien 49f2860039SMatthew Dillon #include <sys/param.h> 50f2860039SMatthew Dillon #include <sys/proc.h> 51f2860039SMatthew Dillon #include <sys/kernel.h> 52f2860039SMatthew Dillon #include <sys/ktr.h> 53f2860039SMatthew Dillon #include <sys/lock.h> 54f2860039SMatthew Dillon #include <sys/malloc.h> 55f2860039SMatthew Dillon #include <sys/mutex.h> 56f2860039SMatthew Dillon #include <sys/systm.h> 57f2860039SMatthew Dillon 58857d9c60SDon Lewis 59c711aea6SPoul-Henning Kamp static MALLOC_DEFINE(M_MTXPOOL, "mtx_pool", "mutex pool"); 60857d9c60SDon Lewis 61857d9c60SDon Lewis /* Pool sizes must be a power of two */ 62857d9c60SDon Lewis #ifndef MTX_POOL_LOCKBUILDER_SIZE 63857d9c60SDon Lewis #define MTX_POOL_LOCKBUILDER_SIZE 128 64f2860039SMatthew Dillon #endif 65857d9c60SDon Lewis #ifndef MTX_POOL_SLEEP_SIZE 66857d9c60SDon Lewis #define MTX_POOL_SLEEP_SIZE 128 67857d9c60SDon Lewis #endif 68f2860039SMatthew Dillon 69857d9c60SDon Lewis struct mtxpool_header { 70857d9c60SDon Lewis int mtxpool_size; 71857d9c60SDon Lewis int mtxpool_mask; 72857d9c60SDon Lewis int mtxpool_shift; 73857d9c60SDon Lewis int mtxpool_next; 74857d9c60SDon Lewis }; 75f2860039SMatthew Dillon 76857d9c60SDon Lewis struct mtx_pool { 77857d9c60SDon Lewis struct mtxpool_header mtx_pool_header; 78857d9c60SDon Lewis struct mtx mtx_pool_ary[1]; 79857d9c60SDon Lewis }; 80f2860039SMatthew Dillon 81857d9c60SDon Lewis static struct mtx_pool_lockbuilder { 82857d9c60SDon Lewis struct mtxpool_header mtx_pool_header; 83857d9c60SDon Lewis struct mtx mtx_pool_ary[MTX_POOL_LOCKBUILDER_SIZE]; 84857d9c60SDon Lewis } lockbuilder_pool; 851b27b1adSPeter Wemm 86857d9c60SDon Lewis #define mtx_pool_size mtx_pool_header.mtxpool_size 87857d9c60SDon Lewis #define mtx_pool_mask mtx_pool_header.mtxpool_mask 88857d9c60SDon Lewis #define mtx_pool_shift mtx_pool_header.mtxpool_shift 89857d9c60SDon Lewis #define mtx_pool_next mtx_pool_header.mtxpool_next 90f2860039SMatthew Dillon 91857d9c60SDon Lewis struct mtx_pool *mtxpool_sleep; 92857d9c60SDon Lewis struct mtx_pool *mtxpool_lockbuilder; 93f2860039SMatthew Dillon 94857d9c60SDon Lewis #if UINTPTR_MAX == UINT64_MAX /* 64 bits */ 95857d9c60SDon Lewis # define POINTER_BITS 64 96857d9c60SDon Lewis # define HASH_MULTIPLIER 11400714819323198485u /* (2^64)*(sqrt(5)-1)/2 */ 97857d9c60SDon Lewis #else /* assume 32 bits */ 98857d9c60SDon Lewis # define POINTER_BITS 32 99857d9c60SDon Lewis # define HASH_MULTIPLIER 2654435769u /* (2^32)*(sqrt(5)-1)/2 */ 100857d9c60SDon Lewis #endif 101f2860039SMatthew Dillon 102f2860039SMatthew Dillon /* 103f2860039SMatthew Dillon * Return the (shared) pool mutex associated with the specified address. 104f2860039SMatthew Dillon * The returned mutex is a leaf level mutex, meaning that if you obtain it 105f2860039SMatthew Dillon * you cannot obtain any other mutexes until you release it. You can 106f2860039SMatthew Dillon * legally msleep() on the mutex. 107f2860039SMatthew Dillon */ 108f2860039SMatthew Dillon struct mtx * 109857d9c60SDon Lewis mtx_pool_find(struct mtx_pool *pool, void *ptr) 110f2860039SMatthew Dillon { 111857d9c60SDon Lewis int p; 112fca73711SMaxime Henrion 113857d9c60SDon Lewis KASSERT(pool != NULL, ("_mtx_pool_find(): null pool")); 114857d9c60SDon Lewis /* 115857d9c60SDon Lewis * Fibonacci hash, see Knuth's 116857d9c60SDon Lewis * _Art of Computer Programming, Volume 3 / Sorting and Searching_ 117857d9c60SDon Lewis */ 118857d9c60SDon Lewis p = ((HASH_MULTIPLIER * (uintptr_t)ptr) >> pool->mtx_pool_shift) & 119857d9c60SDon Lewis pool->mtx_pool_mask; 120857d9c60SDon Lewis return (&pool->mtx_pool_ary[p]); 121857d9c60SDon Lewis } 122857d9c60SDon Lewis 123857d9c60SDon Lewis static void 124857d9c60SDon Lewis mtx_pool_initialize(struct mtx_pool *pool, const char *mtx_name, int pool_size, 125857d9c60SDon Lewis int opts) 126857d9c60SDon Lewis { 127857d9c60SDon Lewis int i, maskbits; 128857d9c60SDon Lewis 129857d9c60SDon Lewis pool->mtx_pool_size = pool_size; 130857d9c60SDon Lewis pool->mtx_pool_mask = pool_size - 1; 131857d9c60SDon Lewis for (i = 1, maskbits = 0; (i & pool_size) == 0; i = i << 1) 132857d9c60SDon Lewis maskbits++; 133857d9c60SDon Lewis pool->mtx_pool_shift = POINTER_BITS - maskbits; 134857d9c60SDon Lewis pool->mtx_pool_next = 0; 135857d9c60SDon Lewis for (i = 0; i < pool_size; ++i) 136857d9c60SDon Lewis mtx_init(&pool->mtx_pool_ary[i], mtx_name, NULL, opts); 137857d9c60SDon Lewis } 138857d9c60SDon Lewis 139857d9c60SDon Lewis struct mtx_pool * 140857d9c60SDon Lewis mtx_pool_create(const char *mtx_name, int pool_size, int opts) 141857d9c60SDon Lewis { 142857d9c60SDon Lewis struct mtx_pool *pool; 143857d9c60SDon Lewis 144857d9c60SDon Lewis if (pool_size <= 0 || !powerof2(pool_size)) { 145857d9c60SDon Lewis printf("WARNING: %s pool size is not a power of 2.\n", 146857d9c60SDon Lewis mtx_name); 147857d9c60SDon Lewis pool_size = 128; 148857d9c60SDon Lewis } 149e11e3f18SDag-Erling Smørgrav pool = malloc(sizeof (struct mtx_pool) + 150e11e3f18SDag-Erling Smørgrav ((pool_size - 1) * sizeof (struct mtx)), 151857d9c60SDon Lewis M_MTXPOOL, M_WAITOK | M_ZERO); 152857d9c60SDon Lewis mtx_pool_initialize(pool, mtx_name, pool_size, opts); 153857d9c60SDon Lewis return pool; 154857d9c60SDon Lewis } 155857d9c60SDon Lewis 156857d9c60SDon Lewis void 157857d9c60SDon Lewis mtx_pool_destroy(struct mtx_pool **poolp) 158857d9c60SDon Lewis { 159857d9c60SDon Lewis int i; 160857d9c60SDon Lewis struct mtx_pool *pool = *poolp; 161857d9c60SDon Lewis 162857d9c60SDon Lewis for (i = pool->mtx_pool_size - 1; i >= 0; --i) 163857d9c60SDon Lewis mtx_destroy(&pool->mtx_pool_ary[i]); 1641ede983cSDag-Erling Smørgrav free(pool, M_MTXPOOL); 165857d9c60SDon Lewis *poolp = NULL; 166857d9c60SDon Lewis } 167857d9c60SDon Lewis 168857d9c60SDon Lewis static void 169857d9c60SDon Lewis mtx_pool_setup_static(void *dummy __unused) 170857d9c60SDon Lewis { 171857d9c60SDon Lewis mtx_pool_initialize((struct mtx_pool *)&lockbuilder_pool, 172857d9c60SDon Lewis "lockbuilder mtxpool", MTX_POOL_LOCKBUILDER_SIZE, 173857d9c60SDon Lewis MTX_DEF | MTX_NOWITNESS | MTX_QUIET); 174857d9c60SDon Lewis mtxpool_lockbuilder = (struct mtx_pool *)&lockbuilder_pool; 175857d9c60SDon Lewis } 176857d9c60SDon Lewis 177857d9c60SDon Lewis static void 178857d9c60SDon Lewis mtx_pool_setup_dynamic(void *dummy __unused) 179857d9c60SDon Lewis { 180857d9c60SDon Lewis mtxpool_sleep = mtx_pool_create("sleep mtxpool", 181857d9c60SDon Lewis MTX_POOL_SLEEP_SIZE, MTX_DEF); 182f2860039SMatthew Dillon } 183f2860039SMatthew Dillon 184f2860039SMatthew Dillon /* 185857d9c60SDon Lewis * Obtain a (shared) mutex from the pool. The returned mutex is a leaf 186857d9c60SDon Lewis * level mutex, meaning that if you obtain it you cannot obtain any other 187857d9c60SDon Lewis * mutexes until you release it. You can legally msleep() on the mutex. 188f2860039SMatthew Dillon */ 189857d9c60SDon Lewis struct mtx * 190857d9c60SDon Lewis mtx_pool_alloc(struct mtx_pool *pool) 191f2860039SMatthew Dillon { 192857d9c60SDon Lewis int i; 193fca73711SMaxime Henrion 194857d9c60SDon Lewis KASSERT(pool != NULL, ("mtx_pool_alloc(): null pool")); 195857d9c60SDon Lewis /* 196857d9c60SDon Lewis * mtx_pool_next is unprotected against multiple accesses, 197857d9c60SDon Lewis * but simultaneous access by two CPUs should not be very 198857d9c60SDon Lewis * harmful. 199857d9c60SDon Lewis */ 200857d9c60SDon Lewis i = pool->mtx_pool_next; 201857d9c60SDon Lewis pool->mtx_pool_next = (i + 1) & pool->mtx_pool_mask; 202857d9c60SDon Lewis return (&pool->mtx_pool_ary[i]); 203f2860039SMatthew Dillon } 204f2860039SMatthew Dillon 205f2860039SMatthew Dillon /* 206857d9c60SDon Lewis * The lockbuilder pool must be initialized early because the lockmgr 207857d9c60SDon Lewis * and sx locks depend on it. The sx locks are used in the kernel 208857d9c60SDon Lewis * memory allocator. The lockmgr subsystem is initialized by 2096ff1481dSDon Lewis * SYSINIT(..., SI_SUB_LOCKMGR, ...). 210857d9c60SDon Lewis * 2111ede983cSDag-Erling Smørgrav * We can't call malloc() to dynamically allocate the sleep pool 212857d9c60SDon Lewis * until after kmeminit() has been called, which is done by 213857d9c60SDon Lewis * SYSINIT(..., SI_SUB_KMEM, ...). 214f2860039SMatthew Dillon */ 215857d9c60SDon Lewis SYSINIT(mtxpooli1, SI_SUB_MTX_POOL_STATIC, SI_ORDER_FIRST, 216857d9c60SDon Lewis mtx_pool_setup_static, NULL); 217857d9c60SDon Lewis SYSINIT(mtxpooli2, SI_SUB_MTX_POOL_DYNAMIC, SI_ORDER_FIRST, 218857d9c60SDon Lewis mtx_pool_setup_dynamic, NULL); 219