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