]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
1d3ba0bf | 9 | * or https://opensource.org/licenses/CDDL-1.0. |
34dc7c2f BB |
10 | * See the License for the specific language governing permissions |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
428870ff | 22 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
dd0b5c85 | 23 | * Copyright (c) 2012, 2021 by Delphix. All rights reserved. |
34dc7c2f BB |
24 | */ |
25 | ||
34dc7c2f | 26 | #include <sys/zfs_context.h> |
27d96d22 | 27 | #include <sys/zfs_refcount.h> |
34dc7c2f | 28 | |
18168da7 | 29 | #ifdef ZFS_DEBUG |
791e480c BB |
30 | /* |
31 | * Reference count tracking is disabled by default. It's memory requirements | |
32 | * are reasonable, however as implemented it consumes a significant amount of | |
33 | * cpu time. Until its performance is improved it should be manually enabled. | |
34 | */ | |
18168da7 | 35 | int reference_tracking_enable = B_FALSE; |
fdc2d303 | 36 | static uint_t reference_history = 3; /* tunable */ |
34dc7c2f BB |
37 | |
38 | static kmem_cache_t *reference_cache; | |
34dc7c2f BB |
39 | |
40 | void | |
424fd7c3 | 41 | zfs_refcount_init(void) |
34dc7c2f BB |
42 | { |
43 | reference_cache = kmem_cache_create("reference_cache", | |
44 | sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0); | |
34dc7c2f BB |
45 | } |
46 | ||
47 | void | |
424fd7c3 | 48 | zfs_refcount_fini(void) |
34dc7c2f BB |
49 | { |
50 | kmem_cache_destroy(reference_cache); | |
d057807e AM |
51 | } |
52 | ||
53 | static int | |
54 | zfs_refcount_compare(const void *x1, const void *x2) | |
55 | { | |
56 | const reference_t *r1 = (const reference_t *)x1; | |
57 | const reference_t *r2 = (const reference_t *)x2; | |
58 | ||
59 | int cmp1 = TREE_CMP(r1->ref_holder, r2->ref_holder); | |
60 | int cmp2 = TREE_CMP(r1->ref_number, r2->ref_number); | |
61 | int cmp = cmp1 ? cmp1 : cmp2; | |
62 | return ((cmp || r1->ref_search) ? cmp : TREE_PCMP(r1, r2)); | |
34dc7c2f BB |
63 | } |
64 | ||
65 | void | |
424fd7c3 | 66 | zfs_refcount_create(zfs_refcount_t *rc) |
34dc7c2f BB |
67 | { |
68 | mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL); | |
d057807e AM |
69 | avl_create(&rc->rc_tree, zfs_refcount_compare, sizeof (reference_t), |
70 | offsetof(reference_t, ref_link.a)); | |
34dc7c2f | 71 | list_create(&rc->rc_removed, sizeof (reference_t), |
d057807e | 72 | offsetof(reference_t, ref_link.l)); |
34dc7c2f BB |
73 | rc->rc_count = 0; |
74 | rc->rc_removed_count = 0; | |
13fe0198 MA |
75 | rc->rc_tracked = reference_tracking_enable; |
76 | } | |
77 | ||
3dfb57a3 | 78 | void |
424fd7c3 | 79 | zfs_refcount_create_tracked(zfs_refcount_t *rc) |
3dfb57a3 | 80 | { |
424fd7c3 | 81 | zfs_refcount_create(rc); |
3dfb57a3 DB |
82 | rc->rc_tracked = B_TRUE; |
83 | } | |
84 | ||
13fe0198 | 85 | void |
424fd7c3 | 86 | zfs_refcount_create_untracked(zfs_refcount_t *rc) |
13fe0198 | 87 | { |
424fd7c3 | 88 | zfs_refcount_create(rc); |
13fe0198 | 89 | rc->rc_tracked = B_FALSE; |
34dc7c2f BB |
90 | } |
91 | ||
92 | void | |
424fd7c3 | 93 | zfs_refcount_destroy_many(zfs_refcount_t *rc, uint64_t number) |
34dc7c2f BB |
94 | { |
95 | reference_t *ref; | |
d057807e | 96 | void *cookie = NULL; |
34dc7c2f | 97 | |
30af21b0 | 98 | ASSERT3U(rc->rc_count, ==, number); |
d057807e | 99 | while ((ref = avl_destroy_nodes(&rc->rc_tree, &cookie)) != NULL) |
34dc7c2f | 100 | kmem_cache_free(reference_cache, ref); |
d057807e | 101 | avl_destroy(&rc->rc_tree); |
34dc7c2f | 102 | |
d057807e | 103 | while ((ref = list_remove_head(&rc->rc_removed))) |
34dc7c2f | 104 | kmem_cache_free(reference_cache, ref); |
34dc7c2f BB |
105 | list_destroy(&rc->rc_removed); |
106 | mutex_destroy(&rc->rc_mtx); | |
107 | } | |
108 | ||
109 | void | |
424fd7c3 | 110 | zfs_refcount_destroy(zfs_refcount_t *rc) |
34dc7c2f | 111 | { |
424fd7c3 | 112 | zfs_refcount_destroy_many(rc, 0); |
34dc7c2f BB |
113 | } |
114 | ||
115 | int | |
424fd7c3 | 116 | zfs_refcount_is_zero(zfs_refcount_t *rc) |
34dc7c2f | 117 | { |
e829a865 | 118 | return (zfs_refcount_count(rc) == 0); |
34dc7c2f BB |
119 | } |
120 | ||
121 | int64_t | |
424fd7c3 | 122 | zfs_refcount_count(zfs_refcount_t *rc) |
34dc7c2f | 123 | { |
e829a865 | 124 | return (atomic_load_64(&rc->rc_count)); |
34dc7c2f BB |
125 | } |
126 | ||
127 | int64_t | |
dc04a8c7 | 128 | zfs_refcount_add_many(zfs_refcount_t *rc, uint64_t number, const void *holder) |
34dc7c2f | 129 | { |
d057807e | 130 | reference_t *ref; |
34dc7c2f BB |
131 | int64_t count; |
132 | ||
d057807e | 133 | if (likely(!rc->rc_tracked)) { |
e829a865 AM |
134 | count = atomic_add_64_nv(&(rc)->rc_count, number); |
135 | ASSERT3U(count, >=, number); | |
136 | return (count); | |
34dc7c2f | 137 | } |
e829a865 AM |
138 | |
139 | ref = kmem_cache_alloc(reference_cache, KM_SLEEP); | |
140 | ref->ref_holder = holder; | |
141 | ref->ref_number = number; | |
d057807e | 142 | ref->ref_search = B_FALSE; |
34dc7c2f | 143 | mutex_enter(&rc->rc_mtx); |
d057807e | 144 | avl_add(&rc->rc_tree, ref); |
34dc7c2f BB |
145 | rc->rc_count += number; |
146 | count = rc->rc_count; | |
147 | mutex_exit(&rc->rc_mtx); | |
148 | ||
149 | return (count); | |
150 | } | |
151 | ||
152 | int64_t | |
dc04a8c7 | 153 | zfs_refcount_add(zfs_refcount_t *rc, const void *holder) |
34dc7c2f | 154 | { |
424fd7c3 | 155 | return (zfs_refcount_add_many(rc, 1, holder)); |
34dc7c2f BB |
156 | } |
157 | ||
5ba4025a AM |
158 | void |
159 | zfs_refcount_add_few(zfs_refcount_t *rc, uint64_t number, const void *holder) | |
160 | { | |
d057807e | 161 | if (likely(!rc->rc_tracked)) |
5ba4025a AM |
162 | (void) zfs_refcount_add_many(rc, number, holder); |
163 | else for (; number > 0; number--) | |
164 | (void) zfs_refcount_add(rc, holder); | |
165 | } | |
166 | ||
34dc7c2f | 167 | int64_t |
dc04a8c7 PD |
168 | zfs_refcount_remove_many(zfs_refcount_t *rc, uint64_t number, |
169 | const void *holder) | |
34dc7c2f | 170 | { |
d057807e | 171 | reference_t *ref, s; |
34dc7c2f BB |
172 | int64_t count; |
173 | ||
d057807e | 174 | if (likely(!rc->rc_tracked)) { |
e829a865 AM |
175 | count = atomic_add_64_nv(&(rc)->rc_count, -number); |
176 | ASSERT3S(count, >=, 0); | |
34dc7c2f BB |
177 | return (count); |
178 | } | |
179 | ||
d057807e AM |
180 | s.ref_holder = holder; |
181 | s.ref_number = number; | |
182 | s.ref_search = B_TRUE; | |
e829a865 AM |
183 | mutex_enter(&rc->rc_mtx); |
184 | ASSERT3U(rc->rc_count, >=, number); | |
d057807e AM |
185 | ref = avl_find(&rc->rc_tree, &s, NULL); |
186 | if (unlikely(ref == NULL)) { | |
187 | panic("No such hold %p on refcount %llx", holder, | |
188 | (u_longlong_t)(uintptr_t)rc); | |
189 | return (-1); | |
190 | } | |
191 | avl_remove(&rc->rc_tree, ref); | |
192 | if (reference_history > 0) { | |
193 | list_insert_head(&rc->rc_removed, ref); | |
194 | if (rc->rc_removed_count >= reference_history) { | |
195 | ref = list_remove_tail(&rc->rc_removed); | |
196 | kmem_cache_free(reference_cache, ref); | |
197 | } else { | |
198 | rc->rc_removed_count++; | |
34dc7c2f | 199 | } |
d057807e AM |
200 | } else { |
201 | kmem_cache_free(reference_cache, ref); | |
34dc7c2f | 202 | } |
d057807e AM |
203 | rc->rc_count -= number; |
204 | count = rc->rc_count; | |
205 | mutex_exit(&rc->rc_mtx); | |
206 | return (count); | |
34dc7c2f BB |
207 | } |
208 | ||
209 | int64_t | |
dc04a8c7 | 210 | zfs_refcount_remove(zfs_refcount_t *rc, const void *holder) |
34dc7c2f | 211 | { |
424fd7c3 | 212 | return (zfs_refcount_remove_many(rc, 1, holder)); |
34dc7c2f BB |
213 | } |
214 | ||
5ba4025a AM |
215 | void |
216 | zfs_refcount_remove_few(zfs_refcount_t *rc, uint64_t number, const void *holder) | |
217 | { | |
d057807e | 218 | if (likely(!rc->rc_tracked)) |
5ba4025a AM |
219 | (void) zfs_refcount_remove_many(rc, number, holder); |
220 | else for (; number > 0; number--) | |
221 | (void) zfs_refcount_remove(rc, holder); | |
222 | } | |
223 | ||
572e2857 | 224 | void |
424fd7c3 | 225 | zfs_refcount_transfer(zfs_refcount_t *dst, zfs_refcount_t *src) |
572e2857 | 226 | { |
d057807e AM |
227 | avl_tree_t tree; |
228 | list_t removed; | |
229 | reference_t *ref; | |
230 | void *cookie = NULL; | |
231 | uint64_t count; | |
232 | uint_t removed_count; | |
572e2857 | 233 | |
d057807e AM |
234 | avl_create(&tree, zfs_refcount_compare, sizeof (reference_t), |
235 | offsetof(reference_t, ref_link.a)); | |
572e2857 | 236 | list_create(&removed, sizeof (reference_t), |
d057807e | 237 | offsetof(reference_t, ref_link.l)); |
572e2857 BB |
238 | |
239 | mutex_enter(&src->rc_mtx); | |
240 | count = src->rc_count; | |
241 | removed_count = src->rc_removed_count; | |
242 | src->rc_count = 0; | |
243 | src->rc_removed_count = 0; | |
d057807e | 244 | avl_swap(&tree, &src->rc_tree); |
572e2857 BB |
245 | list_move_tail(&removed, &src->rc_removed); |
246 | mutex_exit(&src->rc_mtx); | |
247 | ||
248 | mutex_enter(&dst->rc_mtx); | |
249 | dst->rc_count += count; | |
250 | dst->rc_removed_count += removed_count; | |
d057807e AM |
251 | if (avl_is_empty(&dst->rc_tree)) |
252 | avl_swap(&dst->rc_tree, &tree); | |
253 | else while ((ref = avl_destroy_nodes(&tree, &cookie)) != NULL) | |
254 | avl_add(&dst->rc_tree, ref); | |
572e2857 BB |
255 | list_move_tail(&dst->rc_removed, &removed); |
256 | mutex_exit(&dst->rc_mtx); | |
257 | ||
d057807e | 258 | avl_destroy(&tree); |
572e2857 BB |
259 | list_destroy(&removed); |
260 | } | |
261 | ||
d3c2ae1c | 262 | void |
d7e4b30a | 263 | zfs_refcount_transfer_ownership_many(zfs_refcount_t *rc, uint64_t number, |
dc04a8c7 | 264 | const void *current_holder, const void *new_holder) |
d3c2ae1c | 265 | { |
d057807e | 266 | reference_t *ref, s; |
d3c2ae1c | 267 | |
d057807e | 268 | if (likely(!rc->rc_tracked)) |
d3c2ae1c | 269 | return; |
d3c2ae1c | 270 | |
d057807e AM |
271 | s.ref_holder = current_holder; |
272 | s.ref_number = number; | |
273 | s.ref_search = B_TRUE; | |
e829a865 | 274 | mutex_enter(&rc->rc_mtx); |
d057807e AM |
275 | ref = avl_find(&rc->rc_tree, &s, NULL); |
276 | ASSERT(ref); | |
277 | ref->ref_holder = new_holder; | |
278 | avl_update(&rc->rc_tree, ref); | |
d3c2ae1c GW |
279 | mutex_exit(&rc->rc_mtx); |
280 | } | |
3dfb57a3 | 281 | |
d7e4b30a | 282 | void |
dc04a8c7 PD |
283 | zfs_refcount_transfer_ownership(zfs_refcount_t *rc, const void *current_holder, |
284 | const void *new_holder) | |
d7e4b30a BB |
285 | { |
286 | return (zfs_refcount_transfer_ownership_many(rc, 1, current_holder, | |
287 | new_holder)); | |
288 | } | |
289 | ||
3dfb57a3 DB |
290 | /* |
291 | * If tracking is enabled, return true if a reference exists that matches | |
292 | * the "holder" tag. If tracking is disabled, then return true if a reference | |
293 | * might be held. | |
294 | */ | |
295 | boolean_t | |
dc04a8c7 | 296 | zfs_refcount_held(zfs_refcount_t *rc, const void *holder) |
3dfb57a3 | 297 | { |
d057807e AM |
298 | reference_t *ref, s; |
299 | avl_index_t idx; | |
300 | boolean_t res; | |
3dfb57a3 | 301 | |
d057807e | 302 | if (likely(!rc->rc_tracked)) |
e829a865 | 303 | return (zfs_refcount_count(rc) > 0); |
3dfb57a3 | 304 | |
d057807e AM |
305 | s.ref_holder = holder; |
306 | s.ref_number = 0; | |
307 | s.ref_search = B_TRUE; | |
e829a865 | 308 | mutex_enter(&rc->rc_mtx); |
d057807e AM |
309 | ref = avl_find(&rc->rc_tree, &s, &idx); |
310 | if (likely(ref == NULL)) | |
311 | ref = avl_nearest(&rc->rc_tree, idx, AVL_AFTER); | |
312 | res = ref && ref->ref_holder == holder; | |
3dfb57a3 | 313 | mutex_exit(&rc->rc_mtx); |
d057807e | 314 | return (res); |
3dfb57a3 DB |
315 | } |
316 | ||
317 | /* | |
318 | * If tracking is enabled, return true if a reference does not exist that | |
319 | * matches the "holder" tag. If tracking is disabled, always return true | |
320 | * since the reference might not be held. | |
321 | */ | |
322 | boolean_t | |
dc04a8c7 | 323 | zfs_refcount_not_held(zfs_refcount_t *rc, const void *holder) |
3dfb57a3 | 324 | { |
d057807e AM |
325 | reference_t *ref, s; |
326 | avl_index_t idx; | |
327 | boolean_t res; | |
3dfb57a3 | 328 | |
d057807e | 329 | if (likely(!rc->rc_tracked)) |
3dfb57a3 | 330 | return (B_TRUE); |
3dfb57a3 | 331 | |
e829a865 | 332 | mutex_enter(&rc->rc_mtx); |
d057807e AM |
333 | s.ref_holder = holder; |
334 | s.ref_number = 0; | |
335 | s.ref_search = B_TRUE; | |
336 | ref = avl_find(&rc->rc_tree, &s, &idx); | |
337 | if (likely(ref == NULL)) | |
338 | ref = avl_nearest(&rc->rc_tree, idx, AVL_AFTER); | |
339 | res = ref == NULL || ref->ref_holder != holder; | |
3dfb57a3 | 340 | mutex_exit(&rc->rc_mtx); |
d057807e | 341 | return (res); |
3dfb57a3 | 342 | } |
dd0b5c85 | 343 | |
280d0f0c BB |
344 | EXPORT_SYMBOL(zfs_refcount_create); |
345 | EXPORT_SYMBOL(zfs_refcount_destroy); | |
346 | EXPORT_SYMBOL(zfs_refcount_is_zero); | |
347 | EXPORT_SYMBOL(zfs_refcount_count); | |
348 | EXPORT_SYMBOL(zfs_refcount_add); | |
349 | EXPORT_SYMBOL(zfs_refcount_remove); | |
350 | EXPORT_SYMBOL(zfs_refcount_held); | |
351 | ||
dd0b5c85 | 352 | /* BEGIN CSTYLED */ |
18168da7 | 353 | ZFS_MODULE_PARAM(zfs, , reference_tracking_enable, INT, ZMOD_RW, |
dd0b5c85 DB |
354 | "Track reference holders to refcount_t objects"); |
355 | ||
fdc2d303 | 356 | ZFS_MODULE_PARAM(zfs, , reference_history, UINT, ZMOD_RW, |
dd0b5c85 DB |
357 | "Maximum reference holders being tracked"); |
358 | /* END CSTYLED */ | |
572e2857 | 359 | #endif /* ZFS_DEBUG */ |