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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2021 by Delphix. All rights reserved.
26 #include <sys/zfs_context.h>
27 #include <sys/zfs_refcount.h>
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.
35 int reference_tracking_enable
= B_FALSE
;
36 static uint_t reference_history
= 3; /* tunable */
38 static kmem_cache_t
*reference_cache
;
41 zfs_refcount_init(void)
43 reference_cache
= kmem_cache_create("reference_cache",
44 sizeof (reference_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
48 zfs_refcount_fini(void)
50 kmem_cache_destroy(reference_cache
);
54 zfs_refcount_compare(const void *x1
, const void *x2
)
56 const reference_t
*r1
= (const reference_t
*)x1
;
57 const reference_t
*r2
= (const reference_t
*)x2
;
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
));
66 zfs_refcount_create(zfs_refcount_t
*rc
)
68 mutex_init(&rc
->rc_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
69 avl_create(&rc
->rc_tree
, zfs_refcount_compare
, sizeof (reference_t
),
70 offsetof(reference_t
, ref_link
.a
));
71 list_create(&rc
->rc_removed
, sizeof (reference_t
),
72 offsetof(reference_t
, ref_link
.l
));
74 rc
->rc_removed_count
= 0;
75 rc
->rc_tracked
= reference_tracking_enable
;
79 zfs_refcount_create_tracked(zfs_refcount_t
*rc
)
81 zfs_refcount_create(rc
);
82 rc
->rc_tracked
= B_TRUE
;
86 zfs_refcount_create_untracked(zfs_refcount_t
*rc
)
88 zfs_refcount_create(rc
);
89 rc
->rc_tracked
= B_FALSE
;
93 zfs_refcount_destroy_many(zfs_refcount_t
*rc
, uint64_t number
)
98 ASSERT3U(rc
->rc_count
, ==, number
);
99 while ((ref
= avl_destroy_nodes(&rc
->rc_tree
, &cookie
)) != NULL
)
100 kmem_cache_free(reference_cache
, ref
);
101 avl_destroy(&rc
->rc_tree
);
103 while ((ref
= list_remove_head(&rc
->rc_removed
)))
104 kmem_cache_free(reference_cache
, ref
);
105 list_destroy(&rc
->rc_removed
);
106 mutex_destroy(&rc
->rc_mtx
);
110 zfs_refcount_destroy(zfs_refcount_t
*rc
)
112 zfs_refcount_destroy_many(rc
, 0);
116 zfs_refcount_is_zero(zfs_refcount_t
*rc
)
118 return (zfs_refcount_count(rc
) == 0);
122 zfs_refcount_count(zfs_refcount_t
*rc
)
124 return (atomic_load_64(&rc
->rc_count
));
128 zfs_refcount_add_many(zfs_refcount_t
*rc
, uint64_t number
, const void *holder
)
133 if (likely(!rc
->rc_tracked
)) {
134 count
= atomic_add_64_nv(&(rc
)->rc_count
, number
);
135 ASSERT3U(count
, >=, number
);
139 ref
= kmem_cache_alloc(reference_cache
, KM_SLEEP
);
140 ref
->ref_holder
= holder
;
141 ref
->ref_number
= number
;
142 ref
->ref_search
= B_FALSE
;
143 mutex_enter(&rc
->rc_mtx
);
144 avl_add(&rc
->rc_tree
, ref
);
145 rc
->rc_count
+= number
;
146 count
= rc
->rc_count
;
147 mutex_exit(&rc
->rc_mtx
);
153 zfs_refcount_add(zfs_refcount_t
*rc
, const void *holder
)
155 return (zfs_refcount_add_many(rc
, 1, holder
));
159 zfs_refcount_add_few(zfs_refcount_t
*rc
, uint64_t number
, const void *holder
)
161 if (likely(!rc
->rc_tracked
))
162 (void) zfs_refcount_add_many(rc
, number
, holder
);
163 else for (; number
> 0; number
--)
164 (void) zfs_refcount_add(rc
, holder
);
168 zfs_refcount_remove_many(zfs_refcount_t
*rc
, uint64_t number
,
174 if (likely(!rc
->rc_tracked
)) {
175 count
= atomic_add_64_nv(&(rc
)->rc_count
, -number
);
176 ASSERT3S(count
, >=, 0);
180 s
.ref_holder
= holder
;
181 s
.ref_number
= number
;
182 s
.ref_search
= B_TRUE
;
183 mutex_enter(&rc
->rc_mtx
);
184 ASSERT3U(rc
->rc_count
, >=, number
);
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
);
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
);
198 rc
->rc_removed_count
++;
201 kmem_cache_free(reference_cache
, ref
);
203 rc
->rc_count
-= number
;
204 count
= rc
->rc_count
;
205 mutex_exit(&rc
->rc_mtx
);
210 zfs_refcount_remove(zfs_refcount_t
*rc
, const void *holder
)
212 return (zfs_refcount_remove_many(rc
, 1, holder
));
216 zfs_refcount_remove_few(zfs_refcount_t
*rc
, uint64_t number
, const void *holder
)
218 if (likely(!rc
->rc_tracked
))
219 (void) zfs_refcount_remove_many(rc
, number
, holder
);
220 else for (; number
> 0; number
--)
221 (void) zfs_refcount_remove(rc
, holder
);
225 zfs_refcount_transfer(zfs_refcount_t
*dst
, zfs_refcount_t
*src
)
232 uint_t removed_count
;
234 avl_create(&tree
, zfs_refcount_compare
, sizeof (reference_t
),
235 offsetof(reference_t
, ref_link
.a
));
236 list_create(&removed
, sizeof (reference_t
),
237 offsetof(reference_t
, ref_link
.l
));
239 mutex_enter(&src
->rc_mtx
);
240 count
= src
->rc_count
;
241 removed_count
= src
->rc_removed_count
;
243 src
->rc_removed_count
= 0;
244 avl_swap(&tree
, &src
->rc_tree
);
245 list_move_tail(&removed
, &src
->rc_removed
);
246 mutex_exit(&src
->rc_mtx
);
248 mutex_enter(&dst
->rc_mtx
);
249 dst
->rc_count
+= count
;
250 dst
->rc_removed_count
+= removed_count
;
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
);
255 list_move_tail(&dst
->rc_removed
, &removed
);
256 mutex_exit(&dst
->rc_mtx
);
259 list_destroy(&removed
);
263 zfs_refcount_transfer_ownership_many(zfs_refcount_t
*rc
, uint64_t number
,
264 const void *current_holder
, const void *new_holder
)
268 if (likely(!rc
->rc_tracked
))
271 s
.ref_holder
= current_holder
;
272 s
.ref_number
= number
;
273 s
.ref_search
= B_TRUE
;
274 mutex_enter(&rc
->rc_mtx
);
275 ref
= avl_find(&rc
->rc_tree
, &s
, NULL
);
277 ref
->ref_holder
= new_holder
;
278 avl_update(&rc
->rc_tree
, ref
);
279 mutex_exit(&rc
->rc_mtx
);
283 zfs_refcount_transfer_ownership(zfs_refcount_t
*rc
, const void *current_holder
,
284 const void *new_holder
)
286 return (zfs_refcount_transfer_ownership_many(rc
, 1, current_holder
,
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
296 zfs_refcount_held(zfs_refcount_t
*rc
, const void *holder
)
302 if (likely(!rc
->rc_tracked
))
303 return (zfs_refcount_count(rc
) > 0);
305 s
.ref_holder
= holder
;
307 s
.ref_search
= B_TRUE
;
308 mutex_enter(&rc
->rc_mtx
);
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
;
313 mutex_exit(&rc
->rc_mtx
);
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.
323 zfs_refcount_not_held(zfs_refcount_t
*rc
, const void *holder
)
329 if (likely(!rc
->rc_tracked
))
332 mutex_enter(&rc
->rc_mtx
);
333 s
.ref_holder
= holder
;
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
;
340 mutex_exit(&rc
->rc_mtx
);
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
);
353 ZFS_MODULE_PARAM(zfs
, , reference_tracking_enable
, INT
, ZMOD_RW
,
354 "Track reference holders to refcount_t objects");
356 ZFS_MODULE_PARAM(zfs
, , reference_history
, UINT
, ZMOD_RW
,
357 "Maximum reference holders being tracked");
359 #endif /* ZFS_DEBUG */