xref: /freebsd/sys/kern/kern_mtxpool.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1f2860039SMatthew Dillon /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro 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 
48f2860039SMatthew Dillon #include <sys/param.h>
49f2860039SMatthew Dillon #include <sys/proc.h>
50f2860039SMatthew Dillon #include <sys/kernel.h>
51f2860039SMatthew Dillon #include <sys/ktr.h>
52f2860039SMatthew Dillon #include <sys/lock.h>
53f2860039SMatthew Dillon #include <sys/malloc.h>
54f2860039SMatthew Dillon #include <sys/mutex.h>
55f2860039SMatthew Dillon #include <sys/systm.h>
56f2860039SMatthew Dillon 
57c711aea6SPoul-Henning Kamp static MALLOC_DEFINE(M_MTXPOOL, "mtx_pool", "mutex pool");
58857d9c60SDon Lewis 
59857d9c60SDon Lewis /* Pool sizes must be a power of two */
60857d9c60SDon Lewis #ifndef MTX_POOL_SLEEP_SIZE
61abeb9f61SAlexander Motin #define MTX_POOL_SLEEP_SIZE		1024
62857d9c60SDon Lewis #endif
63f2860039SMatthew Dillon 
64857d9c60SDon Lewis struct mtxpool_header {
65857d9c60SDon Lewis 	int		mtxpool_size;
66857d9c60SDon Lewis 	int		mtxpool_mask;
67857d9c60SDon Lewis 	int		mtxpool_shift;
68abeb9f61SAlexander Motin 	int		mtxpool_next __aligned(CACHE_LINE_SIZE);
69857d9c60SDon Lewis };
70f2860039SMatthew Dillon 
71857d9c60SDon Lewis struct mtx_pool {
72857d9c60SDon Lewis 	struct mtxpool_header mtx_pool_header;
73857d9c60SDon Lewis 	struct mtx	mtx_pool_ary[1];
74857d9c60SDon Lewis };
75f2860039SMatthew Dillon 
76857d9c60SDon Lewis #define mtx_pool_size	mtx_pool_header.mtxpool_size
77857d9c60SDon Lewis #define mtx_pool_mask	mtx_pool_header.mtxpool_mask
78857d9c60SDon Lewis #define mtx_pool_shift	mtx_pool_header.mtxpool_shift
79857d9c60SDon Lewis #define mtx_pool_next	mtx_pool_header.mtxpool_next
80f2860039SMatthew Dillon 
81d6ae9188SMateusz Guzik struct mtx_pool __read_mostly *mtxpool_sleep;
82f2860039SMatthew Dillon 
83857d9c60SDon Lewis #if UINTPTR_MAX == UINT64_MAX	/* 64 bits */
84857d9c60SDon Lewis # define POINTER_BITS		64
85857d9c60SDon Lewis # define HASH_MULTIPLIER	11400714819323198485u /* (2^64)*(sqrt(5)-1)/2 */
86857d9c60SDon Lewis #else				/* assume 32 bits */
87857d9c60SDon Lewis # define POINTER_BITS		32
88857d9c60SDon Lewis # define HASH_MULTIPLIER	2654435769u	      /* (2^32)*(sqrt(5)-1)/2 */
89857d9c60SDon Lewis #endif
90f2860039SMatthew Dillon 
91f2860039SMatthew Dillon /*
92f2860039SMatthew Dillon  * Return the (shared) pool mutex associated with the specified address.
93f2860039SMatthew Dillon  * The returned mutex is a leaf level mutex, meaning that if you obtain it
94f2860039SMatthew Dillon  * you cannot obtain any other mutexes until you release it.  You can
95f2860039SMatthew Dillon  * legally msleep() on the mutex.
96f2860039SMatthew Dillon  */
97f2860039SMatthew Dillon struct mtx *
mtx_pool_find(struct mtx_pool * pool,void * ptr)98857d9c60SDon Lewis mtx_pool_find(struct mtx_pool *pool, void *ptr)
99f2860039SMatthew Dillon {
100857d9c60SDon Lewis 	int p;
101fca73711SMaxime Henrion 
102857d9c60SDon Lewis 	KASSERT(pool != NULL, ("_mtx_pool_find(): null pool"));
103857d9c60SDon Lewis 	/*
104857d9c60SDon Lewis 	 * Fibonacci hash, see Knuth's
105857d9c60SDon Lewis 	 * _Art of Computer Programming, Volume 3 / Sorting and Searching_
106857d9c60SDon Lewis 	 */
107857d9c60SDon Lewis 	p = ((HASH_MULTIPLIER * (uintptr_t)ptr) >> pool->mtx_pool_shift) &
108857d9c60SDon Lewis 	    pool->mtx_pool_mask;
109857d9c60SDon Lewis 	return (&pool->mtx_pool_ary[p]);
110857d9c60SDon Lewis }
111857d9c60SDon Lewis 
112857d9c60SDon Lewis static void
mtx_pool_initialize(struct mtx_pool * pool,const char * mtx_name,int pool_size,int opts)113857d9c60SDon Lewis mtx_pool_initialize(struct mtx_pool *pool, const char *mtx_name, int pool_size,
114857d9c60SDon Lewis     int opts)
115857d9c60SDon Lewis {
116857d9c60SDon Lewis 	int i, maskbits;
117857d9c60SDon Lewis 
118857d9c60SDon Lewis 	pool->mtx_pool_size = pool_size;
119857d9c60SDon Lewis 	pool->mtx_pool_mask = pool_size - 1;
120857d9c60SDon Lewis 	for (i = 1, maskbits = 0; (i & pool_size) == 0; i = i << 1)
121857d9c60SDon Lewis 		maskbits++;
122857d9c60SDon Lewis 	pool->mtx_pool_shift = POINTER_BITS - maskbits;
123857d9c60SDon Lewis 	pool->mtx_pool_next = 0;
124857d9c60SDon Lewis 	for (i = 0; i < pool_size; ++i)
125857d9c60SDon Lewis 		mtx_init(&pool->mtx_pool_ary[i], mtx_name, NULL, opts);
126857d9c60SDon Lewis }
127857d9c60SDon Lewis 
128857d9c60SDon Lewis struct mtx_pool *
mtx_pool_create(const char * mtx_name,int pool_size,int opts)129857d9c60SDon Lewis mtx_pool_create(const char *mtx_name, int pool_size, int opts)
130857d9c60SDon Lewis {
131857d9c60SDon Lewis 	struct mtx_pool *pool;
132857d9c60SDon Lewis 
133857d9c60SDon Lewis 	if (pool_size <= 0 || !powerof2(pool_size)) {
134857d9c60SDon Lewis 		printf("WARNING: %s pool size is not a power of 2.\n",
135857d9c60SDon Lewis 		    mtx_name);
136857d9c60SDon Lewis 		pool_size = 128;
137857d9c60SDon Lewis 	}
138e11e3f18SDag-Erling Smørgrav 	pool = malloc(sizeof (struct mtx_pool) +
139e11e3f18SDag-Erling Smørgrav 	    ((pool_size - 1) * sizeof (struct mtx)),
140857d9c60SDon Lewis 	    M_MTXPOOL, M_WAITOK | M_ZERO);
141857d9c60SDon Lewis 	mtx_pool_initialize(pool, mtx_name, pool_size, opts);
142857d9c60SDon Lewis 	return pool;
143857d9c60SDon Lewis }
144857d9c60SDon Lewis 
145857d9c60SDon Lewis void
mtx_pool_destroy(struct mtx_pool ** poolp)146857d9c60SDon Lewis mtx_pool_destroy(struct mtx_pool **poolp)
147857d9c60SDon Lewis {
148857d9c60SDon Lewis 	int i;
149857d9c60SDon Lewis 	struct mtx_pool *pool = *poolp;
150857d9c60SDon Lewis 
151857d9c60SDon Lewis 	for (i = pool->mtx_pool_size - 1; i >= 0; --i)
152857d9c60SDon Lewis 		mtx_destroy(&pool->mtx_pool_ary[i]);
1531ede983cSDag-Erling Smørgrav 	free(pool, M_MTXPOOL);
154857d9c60SDon Lewis 	*poolp = NULL;
155857d9c60SDon Lewis }
156857d9c60SDon Lewis 
157857d9c60SDon Lewis static void
mtx_pool_setup_dynamic(void * dummy __unused)158857d9c60SDon Lewis mtx_pool_setup_dynamic(void *dummy __unused)
159857d9c60SDon Lewis {
160857d9c60SDon Lewis 	mtxpool_sleep = mtx_pool_create("sleep mtxpool",
161857d9c60SDon Lewis 	    MTX_POOL_SLEEP_SIZE, MTX_DEF);
162f2860039SMatthew Dillon }
163f2860039SMatthew Dillon 
164f2860039SMatthew Dillon /*
165857d9c60SDon Lewis  * Obtain a (shared) mutex from the pool.  The returned mutex is a leaf
166857d9c60SDon Lewis  * level mutex, meaning that if you obtain it you cannot obtain any other
167857d9c60SDon Lewis  * mutexes until you release it.  You can legally msleep() on the mutex.
168f2860039SMatthew Dillon  */
169857d9c60SDon Lewis struct mtx *
mtx_pool_alloc(struct mtx_pool * pool)170857d9c60SDon Lewis mtx_pool_alloc(struct mtx_pool *pool)
171f2860039SMatthew Dillon {
172857d9c60SDon Lewis 	int i;
173fca73711SMaxime Henrion 
174857d9c60SDon Lewis 	KASSERT(pool != NULL, ("mtx_pool_alloc(): null pool"));
175857d9c60SDon Lewis 	/*
176857d9c60SDon Lewis 	 * mtx_pool_next is unprotected against multiple accesses,
177857d9c60SDon Lewis 	 * but simultaneous access by two CPUs should not be very
178857d9c60SDon Lewis 	 * harmful.
179857d9c60SDon Lewis 	 */
180857d9c60SDon Lewis 	i = pool->mtx_pool_next;
181857d9c60SDon Lewis 	pool->mtx_pool_next = (i + 1) & pool->mtx_pool_mask;
182857d9c60SDon Lewis 	return (&pool->mtx_pool_ary[i]);
183f2860039SMatthew Dillon }
184f2860039SMatthew Dillon 
185857d9c60SDon Lewis SYSINIT(mtxpooli2, SI_SUB_MTX_POOL_DYNAMIC, SI_ORDER_FIRST,
186857d9c60SDon Lewis     mtx_pool_setup_dynamic, NULL);
187