1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
14 */
15
16 #include <sys/zfs_context.h>
17 #include <sys/multilist.h>
18 #include <sys/trace_zfs.h>
19
20 /*
21 * This overrides the number of sublists in each multilist_t, which defaults
22 * to the number of CPUs in the system (see multilist_create()).
23 */
24 uint_t zfs_multilist_num_sublists = 0;
25
26 /*
27 * Given the object contained on the list, return a pointer to the
28 * object's multilist_node_t structure it contains.
29 */
30 #ifdef ZFS_DEBUG
31 static multilist_node_t *
multilist_d2l(multilist_t * ml,void * obj)32 multilist_d2l(multilist_t *ml, void *obj)
33 {
34 return ((multilist_node_t *)((char *)obj + ml->ml_offset));
35 }
36 #else
37 #define multilist_d2l(ml, obj) ((void) sizeof (ml), (void) sizeof (obj), NULL)
38 #endif
39
40 /*
41 * Initialize a new mutlilist using the parameters specified.
42 *
43 * - 'size' denotes the size of the structure containing the
44 * multilist_node_t.
45 * - 'offset' denotes the byte offset of the mutlilist_node_t within
46 * the structure that contains it.
47 * - 'num' specifies the number of internal sublists to create.
48 * - 'index_func' is used to determine which sublist to insert into
49 * when the multilist_insert() function is called; as well as which
50 * sublist to remove from when multilist_remove() is called. The
51 * requirements this function must meet, are the following:
52 *
53 * - It must always return the same value when called on the same
54 * object (to ensure the object is removed from the list it was
55 * inserted into).
56 *
57 * - It must return a value in the range [0, number of sublists).
58 * The multilist_get_num_sublists() function may be used to
59 * determine the number of sublists in the multilist.
60 *
61 * Also, in order to reduce internal contention between the sublists
62 * during insertion and removal, this function should choose evenly
63 * between all available sublists when inserting. This isn't a hard
64 * requirement, but a general rule of thumb in order to garner the
65 * best multi-threaded performance out of the data structure.
66 */
67 static void
multilist_create_impl(multilist_t * ml,size_t size,size_t offset,uint_t num,multilist_sublist_index_func_t * index_func)68 multilist_create_impl(multilist_t *ml, size_t size, size_t offset,
69 uint_t num, multilist_sublist_index_func_t *index_func)
70 {
71 ASSERT3U(size, >, 0);
72 ASSERT3U(size, >=, offset + sizeof (multilist_node_t));
73 ASSERT3U(num, >, 0);
74 ASSERT3P(index_func, !=, NULL);
75
76 ml->ml_offset = offset;
77 ml->ml_num_sublists = num;
78 ml->ml_index_func = index_func;
79
80 ml->ml_sublists = vmem_zalloc(sizeof (multilist_sublist_t) *
81 ml->ml_num_sublists, KM_SLEEP);
82
83 ASSERT3P(ml->ml_sublists, !=, NULL);
84
85 for (int i = 0; i < ml->ml_num_sublists; i++) {
86 multilist_sublist_t *mls = &ml->ml_sublists[i];
87 mutex_init(&mls->mls_lock, NULL, MUTEX_NOLOCKDEP, NULL);
88 list_create(&mls->mls_list, size, offset);
89 }
90 }
91
92 /*
93 * Allocate a new multilist, using the default number of sublists (the number
94 * of CPUs, or at least 4, or the tunable zfs_multilist_num_sublists). Note
95 * that the multilists do not expand if more CPUs are hot-added. In that case,
96 * we will have less fanout than boot_ncpus, but we don't want to always
97 * reserve the RAM necessary to create the extra slots for additional CPUs up
98 * front, and dynamically adding them is a complex task.
99 */
100 void
multilist_create(multilist_t * ml,size_t size,size_t offset,multilist_sublist_index_func_t * index_func)101 multilist_create(multilist_t *ml, size_t size, size_t offset,
102 multilist_sublist_index_func_t *index_func)
103 {
104 uint_t num_sublists;
105
106 if (zfs_multilist_num_sublists > 0) {
107 num_sublists = zfs_multilist_num_sublists;
108 } else {
109 num_sublists = MAX(boot_ncpus, 4);
110 }
111
112 multilist_create_impl(ml, size, offset, num_sublists, index_func);
113 }
114
115 /*
116 * Destroy the given multilist object, and free up any memory it holds.
117 */
118 void
multilist_destroy(multilist_t * ml)119 multilist_destroy(multilist_t *ml)
120 {
121 ASSERT(multilist_is_empty(ml));
122
123 for (int i = 0; i < ml->ml_num_sublists; i++) {
124 multilist_sublist_t *mls = &ml->ml_sublists[i];
125
126 ASSERT(list_is_empty(&mls->mls_list));
127
128 list_destroy(&mls->mls_list);
129 mutex_destroy(&mls->mls_lock);
130 }
131
132 ASSERT3P(ml->ml_sublists, !=, NULL);
133 vmem_free(ml->ml_sublists,
134 sizeof (multilist_sublist_t) * ml->ml_num_sublists);
135
136 ml->ml_num_sublists = 0;
137 ml->ml_offset = 0;
138 ml->ml_sublists = NULL;
139 }
140
141 /*
142 * Insert the given object into the multilist.
143 *
144 * This function will insert the object specified into the sublist
145 * determined using the function given at multilist creation time.
146 *
147 * The sublist locks are automatically acquired if not already held, to
148 * ensure consistency when inserting and removing from multiple threads.
149 */
150 void
multilist_insert(multilist_t * ml,void * obj)151 multilist_insert(multilist_t *ml, void *obj)
152 {
153 unsigned int sublist_idx = ml->ml_index_func(ml, obj);
154 multilist_sublist_t *mls;
155 boolean_t need_lock;
156
157 DTRACE_PROBE3(multilist__insert, multilist_t *, ml,
158 unsigned int, sublist_idx, void *, obj);
159
160 ASSERT3U(sublist_idx, <, ml->ml_num_sublists);
161
162 mls = &ml->ml_sublists[sublist_idx];
163
164 /*
165 * Note: Callers may already hold the sublist lock by calling
166 * multilist_sublist_lock(). Here we rely on MUTEX_HELD()
167 * returning TRUE if and only if the current thread holds the
168 * lock. While it's a little ugly to make the lock recursive in
169 * this way, it works and allows the calling code to be much
170 * simpler -- otherwise it would have to pass around a flag
171 * indicating that it already has the lock.
172 */
173 need_lock = !MUTEX_HELD(&mls->mls_lock);
174
175 if (need_lock)
176 mutex_enter(&mls->mls_lock);
177
178 ASSERT(!multilist_link_active(multilist_d2l(ml, obj)));
179
180 multilist_sublist_insert_head(mls, obj);
181
182 if (need_lock)
183 mutex_exit(&mls->mls_lock);
184 }
185
186 /*
187 * Remove the given object from the multilist.
188 *
189 * This function will remove the object specified from the sublist
190 * determined using the function given at multilist creation time.
191 *
192 * The necessary sublist locks are automatically acquired, to ensure
193 * consistency when inserting and removing from multiple threads.
194 */
195 void
multilist_remove(multilist_t * ml,void * obj)196 multilist_remove(multilist_t *ml, void *obj)
197 {
198 unsigned int sublist_idx = ml->ml_index_func(ml, obj);
199 multilist_sublist_t *mls;
200 boolean_t need_lock;
201
202 DTRACE_PROBE3(multilist__remove, multilist_t *, ml,
203 unsigned int, sublist_idx, void *, obj);
204
205 ASSERT3U(sublist_idx, <, ml->ml_num_sublists);
206
207 mls = &ml->ml_sublists[sublist_idx];
208 /* See comment in multilist_insert(). */
209 need_lock = !MUTEX_HELD(&mls->mls_lock);
210
211 if (need_lock)
212 mutex_enter(&mls->mls_lock);
213
214 ASSERT(multilist_link_active(multilist_d2l(ml, obj)));
215
216 multilist_sublist_remove(mls, obj);
217
218 if (need_lock)
219 mutex_exit(&mls->mls_lock);
220 }
221
222 /*
223 * Check to see if this multilist object is empty.
224 *
225 * This will return TRUE if it finds all of the sublists of this
226 * multilist to be empty, and FALSE otherwise. Each sublist lock will be
227 * automatically acquired as necessary.
228 *
229 * If concurrent insertions and removals are occurring, the semantics
230 * of this function become a little fuzzy. Instead of locking all
231 * sublists for the entire call time of the function, each sublist is
232 * only locked as it is individually checked for emptiness. Thus, it's
233 * possible for this function to return TRUE with non-empty sublists at
234 * the time the function returns. This would be due to another thread
235 * inserting into a given sublist, after that specific sublist was check
236 * and deemed empty, but before all sublists have been checked.
237 */
238 int
multilist_is_empty(multilist_t * ml)239 multilist_is_empty(multilist_t *ml)
240 {
241 for (int i = 0; i < ml->ml_num_sublists; i++) {
242 multilist_sublist_t *mls = &ml->ml_sublists[i];
243 /* See comment in multilist_insert(). */
244 boolean_t need_lock = !MUTEX_HELD(&mls->mls_lock);
245
246 if (need_lock)
247 mutex_enter(&mls->mls_lock);
248
249 if (!list_is_empty(&mls->mls_list)) {
250 if (need_lock)
251 mutex_exit(&mls->mls_lock);
252
253 return (FALSE);
254 }
255
256 if (need_lock)
257 mutex_exit(&mls->mls_lock);
258 }
259
260 return (TRUE);
261 }
262
263 /* Return the number of sublists composing this multilist */
264 unsigned int
multilist_get_num_sublists(multilist_t * ml)265 multilist_get_num_sublists(multilist_t *ml)
266 {
267 return (ml->ml_num_sublists);
268 }
269
270 /* Return a randomly selected, valid sublist index for this multilist */
271 unsigned int
multilist_get_random_index(multilist_t * ml)272 multilist_get_random_index(multilist_t *ml)
273 {
274 return (random_in_range(ml->ml_num_sublists));
275 }
276
277 void
multilist_sublist_lock(multilist_sublist_t * mls)278 multilist_sublist_lock(multilist_sublist_t *mls)
279 {
280 mutex_enter(&mls->mls_lock);
281 }
282
283 /* Lock and return the sublist specified at the given index */
284 multilist_sublist_t *
multilist_sublist_lock_idx(multilist_t * ml,unsigned int sublist_idx)285 multilist_sublist_lock_idx(multilist_t *ml, unsigned int sublist_idx)
286 {
287 multilist_sublist_t *mls;
288
289 ASSERT3U(sublist_idx, <, ml->ml_num_sublists);
290 mls = &ml->ml_sublists[sublist_idx];
291 mutex_enter(&mls->mls_lock);
292
293 return (mls);
294 }
295
296 /* Lock and return the sublist that would be used to store the specified obj */
297 multilist_sublist_t *
multilist_sublist_lock_obj(multilist_t * ml,void * obj)298 multilist_sublist_lock_obj(multilist_t *ml, void *obj)
299 {
300 return (multilist_sublist_lock_idx(ml, ml->ml_index_func(ml, obj)));
301 }
302
303 void
multilist_sublist_unlock(multilist_sublist_t * mls)304 multilist_sublist_unlock(multilist_sublist_t *mls)
305 {
306 mutex_exit(&mls->mls_lock);
307 }
308
309 /*
310 * We're allowing any object to be inserted into this specific sublist,
311 * but this can lead to trouble if multilist_remove() is called to
312 * remove this object. Specifically, if calling ml_index_func on this
313 * object returns an index for sublist different than what is passed as
314 * a parameter here, any call to multilist_remove() with this newly
315 * inserted object is undefined! (the call to multilist_remove() will
316 * remove the object from a list that it isn't contained in)
317 */
318 void
multilist_sublist_insert_head(multilist_sublist_t * mls,void * obj)319 multilist_sublist_insert_head(multilist_sublist_t *mls, void *obj)
320 {
321 ASSERT(MUTEX_HELD(&mls->mls_lock));
322 list_insert_head(&mls->mls_list, obj);
323 }
324
325 /* please see comment above multilist_sublist_insert_head */
326 void
multilist_sublist_insert_tail(multilist_sublist_t * mls,void * obj)327 multilist_sublist_insert_tail(multilist_sublist_t *mls, void *obj)
328 {
329 ASSERT(MUTEX_HELD(&mls->mls_lock));
330 list_insert_tail(&mls->mls_list, obj);
331 }
332
333 /* please see comment above multilist_sublist_insert_head */
334 void
multilist_sublist_insert_after(multilist_sublist_t * mls,void * prev,void * obj)335 multilist_sublist_insert_after(multilist_sublist_t *mls, void *prev, void *obj)
336 {
337 ASSERT(MUTEX_HELD(&mls->mls_lock));
338 list_insert_after(&mls->mls_list, prev, obj);
339 }
340
341 /* please see comment above multilist_sublist_insert_head */
342 void
multilist_sublist_insert_before(multilist_sublist_t * mls,void * next,void * obj)343 multilist_sublist_insert_before(multilist_sublist_t *mls, void *next, void *obj)
344 {
345 ASSERT(MUTEX_HELD(&mls->mls_lock));
346 list_insert_before(&mls->mls_list, next, obj);
347 }
348
349 /*
350 * Move the object one element forward in the list.
351 *
352 * This function will move the given object forward in the list (towards
353 * the head) by one object. So, in essence, it will swap its position in
354 * the list with its "prev" pointer. If the given object is already at the
355 * head of the list, it cannot be moved forward any more than it already
356 * is, so no action is taken.
357 *
358 * NOTE: This function **must not** remove any object from the list other
359 * than the object given as the parameter. This is relied upon in
360 * arc_evict_state_impl().
361 */
362 void
multilist_sublist_move_forward(multilist_sublist_t * mls,void * obj)363 multilist_sublist_move_forward(multilist_sublist_t *mls, void *obj)
364 {
365 void *prev = list_prev(&mls->mls_list, obj);
366
367 ASSERT(MUTEX_HELD(&mls->mls_lock));
368 ASSERT(!list_is_empty(&mls->mls_list));
369
370 /* 'obj' must be at the head of the list, nothing to do */
371 if (prev == NULL)
372 return;
373
374 list_remove(&mls->mls_list, obj);
375 list_insert_before(&mls->mls_list, prev, obj);
376 }
377
378 void
multilist_sublist_remove(multilist_sublist_t * mls,void * obj)379 multilist_sublist_remove(multilist_sublist_t *mls, void *obj)
380 {
381 ASSERT(MUTEX_HELD(&mls->mls_lock));
382 list_remove(&mls->mls_list, obj);
383 }
384
385 int
multilist_sublist_is_empty(multilist_sublist_t * mls)386 multilist_sublist_is_empty(multilist_sublist_t *mls)
387 {
388 ASSERT(MUTEX_HELD(&mls->mls_lock));
389 return (list_is_empty(&mls->mls_list));
390 }
391
392 int
multilist_sublist_is_empty_idx(multilist_t * ml,unsigned int sublist_idx)393 multilist_sublist_is_empty_idx(multilist_t *ml, unsigned int sublist_idx)
394 {
395 multilist_sublist_t *mls;
396 int empty;
397
398 ASSERT3U(sublist_idx, <, ml->ml_num_sublists);
399 mls = &ml->ml_sublists[sublist_idx];
400 ASSERT(!MUTEX_HELD(&mls->mls_lock));
401 mutex_enter(&mls->mls_lock);
402 empty = list_is_empty(&mls->mls_list);
403 mutex_exit(&mls->mls_lock);
404 return (empty);
405 }
406
407 void *
multilist_sublist_head(multilist_sublist_t * mls)408 multilist_sublist_head(multilist_sublist_t *mls)
409 {
410 ASSERT(MUTEX_HELD(&mls->mls_lock));
411 return (list_head(&mls->mls_list));
412 }
413
414 void *
multilist_sublist_tail(multilist_sublist_t * mls)415 multilist_sublist_tail(multilist_sublist_t *mls)
416 {
417 ASSERT(MUTEX_HELD(&mls->mls_lock));
418 return (list_tail(&mls->mls_list));
419 }
420
421 void *
multilist_sublist_next(multilist_sublist_t * mls,void * obj)422 multilist_sublist_next(multilist_sublist_t *mls, void *obj)
423 {
424 ASSERT(MUTEX_HELD(&mls->mls_lock));
425 return (list_next(&mls->mls_list, obj));
426 }
427
428 void *
multilist_sublist_prev(multilist_sublist_t * mls,void * obj)429 multilist_sublist_prev(multilist_sublist_t *mls, void *obj)
430 {
431 ASSERT(MUTEX_HELD(&mls->mls_lock));
432 return (list_prev(&mls->mls_list, obj));
433 }
434
435 void
multilist_link_init(multilist_node_t * link)436 multilist_link_init(multilist_node_t *link)
437 {
438 list_link_init(link);
439 }
440
441 int
multilist_link_active(multilist_node_t * link)442 multilist_link_active(multilist_node_t *link)
443 {
444 return (list_link_active(link));
445 }
446
447 ZFS_MODULE_PARAM(zfs, zfs_, multilist_num_sublists, UINT, ZMOD_RW,
448 "Number of sublists used in each multilist");
449