xref: /freebsd/sys/kern/kern_mtxpool.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Matthew Dillon.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /* Mutex pool routines.  These routines are designed to be used as short
29  * term leaf mutexes (e.g. the last mutex you might acquire other then
30  * calling msleep()).  They operate using a shared pool.  A mutex is chosen
31  * from the pool based on the supplied pointer (which may or may not be
32  * valid).
33  *
34  * Advantages:
35  *	- no structural overhead.  Mutexes can be associated with structures
36  *	  without adding bloat to the structures.
37  *	- mutexes can be obtained for invalid pointers, useful when uses
38  *	  mutexes to interlock destructor ops.
39  *	- no initialization/destructor overhead.
40  *	- can be used with msleep.
41  *
42  * Disadvantages:
43  *	- should generally only be used as leaf mutexes.
44  *	- pool/pool dependency ordering cannot be depended on.
45  *	- possible L1 cache mastersip contention between cpus.
46  */
47 
48 #include <sys/param.h>
49 #include <sys/proc.h>
50 #include <sys/kernel.h>
51 #include <sys/ktr.h>
52 #include <sys/lock.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
55 #include <sys/systm.h>
56 
57 static MALLOC_DEFINE(M_MTXPOOL, "mtx_pool", "mutex pool");
58 
59 /* Pool sizes must be a power of two */
60 #ifndef MTX_POOL_SLEEP_SIZE
61 #define MTX_POOL_SLEEP_SIZE		1024
62 #endif
63 
64 struct mtxpool_header {
65 	int		mtxpool_size;
66 	int		mtxpool_mask;
67 	int		mtxpool_shift;
68 	int		mtxpool_next __aligned(CACHE_LINE_SIZE);
69 };
70 
71 struct mtx_pool {
72 	struct mtxpool_header mtx_pool_header;
73 	struct mtx	mtx_pool_ary[1];
74 };
75 
76 #define mtx_pool_size	mtx_pool_header.mtxpool_size
77 #define mtx_pool_mask	mtx_pool_header.mtxpool_mask
78 #define mtx_pool_shift	mtx_pool_header.mtxpool_shift
79 #define mtx_pool_next	mtx_pool_header.mtxpool_next
80 
81 struct mtx_pool __read_mostly *mtxpool_sleep;
82 
83 #if UINTPTR_MAX == UINT64_MAX	/* 64 bits */
84 # define POINTER_BITS		64
85 # define HASH_MULTIPLIER	11400714819323198485u /* (2^64)*(sqrt(5)-1)/2 */
86 #else				/* assume 32 bits */
87 # define POINTER_BITS		32
88 # define HASH_MULTIPLIER	2654435769u	      /* (2^32)*(sqrt(5)-1)/2 */
89 #endif
90 
91 /*
92  * Return the (shared) pool mutex associated with the specified address.
93  * The returned mutex is a leaf level mutex, meaning that if you obtain it
94  * you cannot obtain any other mutexes until you release it.  You can
95  * legally msleep() on the mutex.
96  */
97 struct mtx *
98 mtx_pool_find(struct mtx_pool *pool, void *ptr)
99 {
100 	int p;
101 
102 	KASSERT(pool != NULL, ("_mtx_pool_find(): null pool"));
103 	/*
104 	 * Fibonacci hash, see Knuth's
105 	 * _Art of Computer Programming, Volume 3 / Sorting and Searching_
106 	 */
107 	p = ((HASH_MULTIPLIER * (uintptr_t)ptr) >> pool->mtx_pool_shift) &
108 	    pool->mtx_pool_mask;
109 	return (&pool->mtx_pool_ary[p]);
110 }
111 
112 static void
113 mtx_pool_initialize(struct mtx_pool *pool, const char *mtx_name, int pool_size,
114     int opts)
115 {
116 	int i, maskbits;
117 
118 	pool->mtx_pool_size = pool_size;
119 	pool->mtx_pool_mask = pool_size - 1;
120 	for (i = 1, maskbits = 0; (i & pool_size) == 0; i = i << 1)
121 		maskbits++;
122 	pool->mtx_pool_shift = POINTER_BITS - maskbits;
123 	pool->mtx_pool_next = 0;
124 	for (i = 0; i < pool_size; ++i)
125 		mtx_init(&pool->mtx_pool_ary[i], mtx_name, NULL, opts);
126 }
127 
128 struct mtx_pool *
129 mtx_pool_create(const char *mtx_name, int pool_size, int opts)
130 {
131 	struct mtx_pool *pool;
132 
133 	if (pool_size <= 0 || !powerof2(pool_size)) {
134 		printf("WARNING: %s pool size is not a power of 2.\n",
135 		    mtx_name);
136 		pool_size = 128;
137 	}
138 	pool = malloc(sizeof (struct mtx_pool) +
139 	    ((pool_size - 1) * sizeof (struct mtx)),
140 	    M_MTXPOOL, M_WAITOK | M_ZERO);
141 	mtx_pool_initialize(pool, mtx_name, pool_size, opts);
142 	return pool;
143 }
144 
145 void
146 mtx_pool_destroy(struct mtx_pool **poolp)
147 {
148 	int i;
149 	struct mtx_pool *pool = *poolp;
150 
151 	for (i = pool->mtx_pool_size - 1; i >= 0; --i)
152 		mtx_destroy(&pool->mtx_pool_ary[i]);
153 	free(pool, M_MTXPOOL);
154 	*poolp = NULL;
155 }
156 
157 static void
158 mtx_pool_setup_dynamic(void *dummy __unused)
159 {
160 	mtxpool_sleep = mtx_pool_create("sleep mtxpool",
161 	    MTX_POOL_SLEEP_SIZE, MTX_DEF);
162 }
163 
164 /*
165  * Obtain a (shared) mutex from the pool.  The returned mutex is a leaf
166  * level mutex, meaning that if you obtain it you cannot obtain any other
167  * mutexes until you release it.  You can legally msleep() on the mutex.
168  */
169 struct mtx *
170 mtx_pool_alloc(struct mtx_pool *pool)
171 {
172 	int i;
173 
174 	KASSERT(pool != NULL, ("mtx_pool_alloc(): null pool"));
175 	/*
176 	 * mtx_pool_next is unprotected against multiple accesses,
177 	 * but simultaneous access by two CPUs should not be very
178 	 * harmful.
179 	 */
180 	i = pool->mtx_pool_next;
181 	pool->mtx_pool_next = (i + 1) & pool->mtx_pool_mask;
182 	return (&pool->mtx_pool_ary[i]);
183 }
184 
185 SYSINIT(mtxpooli2, SI_SUB_MTX_POOL_DYNAMIC, SI_ORDER_FIRST,
186     mtx_pool_setup_dynamic, NULL);
187