1f2860039SMatthew Dillon /*- 2*8a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*8a36da99SPedro F. Giffuni * 473108a16SWarner Losh * Copyright (c) 2001 Matthew Dillon. All Rights Reserved. 5f2860039SMatthew Dillon * 673108a16SWarner Losh * Redistribution and use in source and binary forms, with or without 773108a16SWarner Losh * modification, are permitted provided that the following conditions 873108a16SWarner Losh * are met: 973108a16SWarner Losh * 1. Redistributions of source code must retain the above copyright 1073108a16SWarner Losh * notice, this list of conditions and the following disclaimer. 1173108a16SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 1273108a16SWarner Losh * notice, this list of conditions and the following disclaimer in the 1373108a16SWarner Losh * documentation and/or other materials provided with the distribution. 1473108a16SWarner Losh * 1573108a16SWarner Losh * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1673108a16SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1773108a16SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1873108a16SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 1973108a16SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2073108a16SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2173108a16SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2273108a16SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2373108a16SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2473108a16SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2573108a16SWarner Losh * SUCH DAMAGE. 2673108a16SWarner Losh */ 2773108a16SWarner Losh 2873108a16SWarner Losh /* Mutex pool routines. These routines are designed to be used as short 29e1e8f51bSRobert Watson * term leaf mutexes (e.g. the last mutex you might acquire other then 30f2860039SMatthew Dillon * calling msleep()). They operate using a shared pool. A mutex is chosen 31f2860039SMatthew Dillon * from the pool based on the supplied pointer (which may or may not be 32f2860039SMatthew Dillon * valid). 33f2860039SMatthew Dillon * 34f2860039SMatthew Dillon * Advantages: 35f2860039SMatthew Dillon * - no structural overhead. Mutexes can be associated with structures 36f2860039SMatthew Dillon * without adding bloat to the structures. 37f2860039SMatthew Dillon * - mutexes can be obtained for invalid pointers, useful when uses 38f2860039SMatthew Dillon * mutexes to interlock destructor ops. 39fca73711SMaxime Henrion * - no initialization/destructor overhead. 40f2860039SMatthew Dillon * - can be used with msleep. 41f2860039SMatthew Dillon * 42f2860039SMatthew Dillon * Disadvantages: 43fca73711SMaxime Henrion * - should generally only be used as leaf mutexes. 44e3043798SPedro F. Giffuni * - pool/pool dependency ordering cannot be depended on. 45fca73711SMaxime Henrion * - possible L1 cache mastersip contention between cpus. 46f2860039SMatthew Dillon */ 47f2860039SMatthew Dillon 48677b542eSDavid E. O'Brien #include <sys/cdefs.h> 49677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 50677b542eSDavid E. O'Brien 51f2860039SMatthew Dillon #include <sys/param.h> 52f2860039SMatthew Dillon #include <sys/proc.h> 53f2860039SMatthew Dillon #include <sys/kernel.h> 54f2860039SMatthew Dillon #include <sys/ktr.h> 55f2860039SMatthew Dillon #include <sys/lock.h> 56f2860039SMatthew Dillon #include <sys/malloc.h> 57f2860039SMatthew Dillon #include <sys/mutex.h> 58f2860039SMatthew Dillon #include <sys/systm.h> 59f2860039SMatthew Dillon 60857d9c60SDon Lewis 61c711aea6SPoul-Henning Kamp static MALLOC_DEFINE(M_MTXPOOL, "mtx_pool", "mutex pool"); 62857d9c60SDon Lewis 63857d9c60SDon Lewis /* Pool sizes must be a power of two */ 64857d9c60SDon Lewis #ifndef MTX_POOL_SLEEP_SIZE 65857d9c60SDon Lewis #define MTX_POOL_SLEEP_SIZE 128 66857d9c60SDon Lewis #endif 67f2860039SMatthew Dillon 68857d9c60SDon Lewis struct mtxpool_header { 69857d9c60SDon Lewis int mtxpool_size; 70857d9c60SDon Lewis int mtxpool_mask; 71857d9c60SDon Lewis int mtxpool_shift; 72857d9c60SDon Lewis int mtxpool_next; 73857d9c60SDon Lewis }; 74f2860039SMatthew Dillon 75857d9c60SDon Lewis struct mtx_pool { 76857d9c60SDon Lewis struct mtxpool_header mtx_pool_header; 77857d9c60SDon Lewis struct mtx mtx_pool_ary[1]; 78857d9c60SDon Lewis }; 79f2860039SMatthew Dillon 80857d9c60SDon Lewis #define mtx_pool_size mtx_pool_header.mtxpool_size 81857d9c60SDon Lewis #define mtx_pool_mask mtx_pool_header.mtxpool_mask 82857d9c60SDon Lewis #define mtx_pool_shift mtx_pool_header.mtxpool_shift 83857d9c60SDon Lewis #define mtx_pool_next mtx_pool_header.mtxpool_next 84f2860039SMatthew Dillon 85857d9c60SDon Lewis struct mtx_pool *mtxpool_sleep; 86f2860039SMatthew Dillon 87857d9c60SDon Lewis #if UINTPTR_MAX == UINT64_MAX /* 64 bits */ 88857d9c60SDon Lewis # define POINTER_BITS 64 89857d9c60SDon Lewis # define HASH_MULTIPLIER 11400714819323198485u /* (2^64)*(sqrt(5)-1)/2 */ 90857d9c60SDon Lewis #else /* assume 32 bits */ 91857d9c60SDon Lewis # define POINTER_BITS 32 92857d9c60SDon Lewis # define HASH_MULTIPLIER 2654435769u /* (2^32)*(sqrt(5)-1)/2 */ 93857d9c60SDon Lewis #endif 94f2860039SMatthew Dillon 95f2860039SMatthew Dillon /* 96f2860039SMatthew Dillon * Return the (shared) pool mutex associated with the specified address. 97f2860039SMatthew Dillon * The returned mutex is a leaf level mutex, meaning that if you obtain it 98f2860039SMatthew Dillon * you cannot obtain any other mutexes until you release it. You can 99f2860039SMatthew Dillon * legally msleep() on the mutex. 100f2860039SMatthew Dillon */ 101f2860039SMatthew Dillon struct mtx * 102857d9c60SDon Lewis mtx_pool_find(struct mtx_pool *pool, void *ptr) 103f2860039SMatthew Dillon { 104857d9c60SDon Lewis int p; 105fca73711SMaxime Henrion 106857d9c60SDon Lewis KASSERT(pool != NULL, ("_mtx_pool_find(): null pool")); 107857d9c60SDon Lewis /* 108857d9c60SDon Lewis * Fibonacci hash, see Knuth's 109857d9c60SDon Lewis * _Art of Computer Programming, Volume 3 / Sorting and Searching_ 110857d9c60SDon Lewis */ 111857d9c60SDon Lewis p = ((HASH_MULTIPLIER * (uintptr_t)ptr) >> pool->mtx_pool_shift) & 112857d9c60SDon Lewis pool->mtx_pool_mask; 113857d9c60SDon Lewis return (&pool->mtx_pool_ary[p]); 114857d9c60SDon Lewis } 115857d9c60SDon Lewis 116857d9c60SDon Lewis static void 117857d9c60SDon Lewis mtx_pool_initialize(struct mtx_pool *pool, const char *mtx_name, int pool_size, 118857d9c60SDon Lewis int opts) 119857d9c60SDon Lewis { 120857d9c60SDon Lewis int i, maskbits; 121857d9c60SDon Lewis 122857d9c60SDon Lewis pool->mtx_pool_size = pool_size; 123857d9c60SDon Lewis pool->mtx_pool_mask = pool_size - 1; 124857d9c60SDon Lewis for (i = 1, maskbits = 0; (i & pool_size) == 0; i = i << 1) 125857d9c60SDon Lewis maskbits++; 126857d9c60SDon Lewis pool->mtx_pool_shift = POINTER_BITS - maskbits; 127857d9c60SDon Lewis pool->mtx_pool_next = 0; 128857d9c60SDon Lewis for (i = 0; i < pool_size; ++i) 129857d9c60SDon Lewis mtx_init(&pool->mtx_pool_ary[i], mtx_name, NULL, opts); 130857d9c60SDon Lewis } 131857d9c60SDon Lewis 132857d9c60SDon Lewis struct mtx_pool * 133857d9c60SDon Lewis mtx_pool_create(const char *mtx_name, int pool_size, int opts) 134857d9c60SDon Lewis { 135857d9c60SDon Lewis struct mtx_pool *pool; 136857d9c60SDon Lewis 137857d9c60SDon Lewis if (pool_size <= 0 || !powerof2(pool_size)) { 138857d9c60SDon Lewis printf("WARNING: %s pool size is not a power of 2.\n", 139857d9c60SDon Lewis mtx_name); 140857d9c60SDon Lewis pool_size = 128; 141857d9c60SDon Lewis } 142e11e3f18SDag-Erling Smørgrav pool = malloc(sizeof (struct mtx_pool) + 143e11e3f18SDag-Erling Smørgrav ((pool_size - 1) * sizeof (struct mtx)), 144857d9c60SDon Lewis M_MTXPOOL, M_WAITOK | M_ZERO); 145857d9c60SDon Lewis mtx_pool_initialize(pool, mtx_name, pool_size, opts); 146857d9c60SDon Lewis return pool; 147857d9c60SDon Lewis } 148857d9c60SDon Lewis 149857d9c60SDon Lewis void 150857d9c60SDon Lewis mtx_pool_destroy(struct mtx_pool **poolp) 151857d9c60SDon Lewis { 152857d9c60SDon Lewis int i; 153857d9c60SDon Lewis struct mtx_pool *pool = *poolp; 154857d9c60SDon Lewis 155857d9c60SDon Lewis for (i = pool->mtx_pool_size - 1; i >= 0; --i) 156857d9c60SDon Lewis mtx_destroy(&pool->mtx_pool_ary[i]); 1571ede983cSDag-Erling Smørgrav free(pool, M_MTXPOOL); 158857d9c60SDon Lewis *poolp = NULL; 159857d9c60SDon Lewis } 160857d9c60SDon Lewis 161857d9c60SDon Lewis static void 162857d9c60SDon Lewis mtx_pool_setup_dynamic(void *dummy __unused) 163857d9c60SDon Lewis { 164857d9c60SDon Lewis mtxpool_sleep = mtx_pool_create("sleep mtxpool", 165857d9c60SDon Lewis MTX_POOL_SLEEP_SIZE, MTX_DEF); 166f2860039SMatthew Dillon } 167f2860039SMatthew Dillon 168f2860039SMatthew Dillon /* 169857d9c60SDon Lewis * Obtain a (shared) mutex from the pool. The returned mutex is a leaf 170857d9c60SDon Lewis * level mutex, meaning that if you obtain it you cannot obtain any other 171857d9c60SDon Lewis * mutexes until you release it. You can legally msleep() on the mutex. 172f2860039SMatthew Dillon */ 173857d9c60SDon Lewis struct mtx * 174857d9c60SDon Lewis mtx_pool_alloc(struct mtx_pool *pool) 175f2860039SMatthew Dillon { 176857d9c60SDon Lewis int i; 177fca73711SMaxime Henrion 178857d9c60SDon Lewis KASSERT(pool != NULL, ("mtx_pool_alloc(): null pool")); 179857d9c60SDon Lewis /* 180857d9c60SDon Lewis * mtx_pool_next is unprotected against multiple accesses, 181857d9c60SDon Lewis * but simultaneous access by two CPUs should not be very 182857d9c60SDon Lewis * harmful. 183857d9c60SDon Lewis */ 184857d9c60SDon Lewis i = pool->mtx_pool_next; 185857d9c60SDon Lewis pool->mtx_pool_next = (i + 1) & pool->mtx_pool_mask; 186857d9c60SDon Lewis return (&pool->mtx_pool_ary[i]); 187f2860039SMatthew Dillon } 188f2860039SMatthew Dillon 189857d9c60SDon Lewis SYSINIT(mtxpooli2, SI_SUB_MTX_POOL_DYNAMIC, SI_ORDER_FIRST, 190857d9c60SDon Lewis mtx_pool_setup_dynamic, NULL); 191