xref: /freebsd/sys/contrib/openzfs/module/zfs/refcount.c (revision 2d1d418e1e7bc8325bb052185c17c81a674d0c4e)
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
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.
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 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2021 by Delphix. All rights reserved.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/zfs_refcount.h>
28 
29 #ifdef	ZFS_DEBUG
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  */
35 int reference_tracking_enable = B_FALSE;
36 static uint_t reference_history = 3; /* tunable */
37 
38 static kmem_cache_t *reference_cache;
39 static kmem_cache_t *reference_history_cache;
40 
41 void
42 zfs_refcount_init(void)
43 {
44 	reference_cache = kmem_cache_create("reference_cache",
45 	    sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
46 
47 	reference_history_cache = kmem_cache_create("reference_history_cache",
48 	    sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
49 }
50 
51 void
52 zfs_refcount_fini(void)
53 {
54 	kmem_cache_destroy(reference_cache);
55 	kmem_cache_destroy(reference_history_cache);
56 }
57 
58 void
59 zfs_refcount_create(zfs_refcount_t *rc)
60 {
61 	mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
62 	list_create(&rc->rc_list, sizeof (reference_t),
63 	    offsetof(reference_t, ref_link));
64 	list_create(&rc->rc_removed, sizeof (reference_t),
65 	    offsetof(reference_t, ref_link));
66 	rc->rc_count = 0;
67 	rc->rc_removed_count = 0;
68 	rc->rc_tracked = reference_tracking_enable;
69 }
70 
71 void
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
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
86 zfs_refcount_destroy_many(zfs_refcount_t *rc, uint64_t number)
87 {
88 	reference_t *ref;
89 
90 	ASSERT3U(rc->rc_count, ==, number);
91 	while ((ref = list_remove_head(&rc->rc_list)))
92 		kmem_cache_free(reference_cache, ref);
93 	list_destroy(&rc->rc_list);
94 
95 	while ((ref = list_remove_head(&rc->rc_removed))) {
96 		kmem_cache_free(reference_history_cache, ref->ref_removed);
97 		kmem_cache_free(reference_cache, ref);
98 	}
99 	list_destroy(&rc->rc_removed);
100 	mutex_destroy(&rc->rc_mtx);
101 }
102 
103 void
104 zfs_refcount_destroy(zfs_refcount_t *rc)
105 {
106 	zfs_refcount_destroy_many(rc, 0);
107 }
108 
109 int
110 zfs_refcount_is_zero(zfs_refcount_t *rc)
111 {
112 	return (zfs_refcount_count(rc) == 0);
113 }
114 
115 int64_t
116 zfs_refcount_count(zfs_refcount_t *rc)
117 {
118 	return (atomic_load_64(&rc->rc_count));
119 }
120 
121 int64_t
122 zfs_refcount_add_many(zfs_refcount_t *rc, uint64_t number, const void *holder)
123 {
124 	reference_t *ref = NULL;
125 	int64_t count;
126 
127 	if (!rc->rc_tracked) {
128 		count = atomic_add_64_nv(&(rc)->rc_count, number);
129 		ASSERT3U(count, >=, number);
130 		return (count);
131 	}
132 
133 	ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
134 	ref->ref_holder = holder;
135 	ref->ref_number = number;
136 	mutex_enter(&rc->rc_mtx);
137 	list_insert_head(&rc->rc_list, 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
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
152 zfs_refcount_add_few(zfs_refcount_t *rc, uint64_t number, const void *holder)
153 {
154 	if (!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
161 zfs_refcount_remove_many(zfs_refcount_t *rc, uint64_t number,
162     const void *holder)
163 {
164 	reference_t *ref;
165 	int64_t count;
166 
167 	if (!rc->rc_tracked) {
168 		count = atomic_add_64_nv(&(rc)->rc_count, -number);
169 		ASSERT3S(count, >=, 0);
170 		return (count);
171 	}
172 
173 	mutex_enter(&rc->rc_mtx);
174 	ASSERT3U(rc->rc_count, >=, number);
175 	for (ref = list_head(&rc->rc_list); ref;
176 	    ref = list_next(&rc->rc_list, ref)) {
177 		if (ref->ref_holder == holder && ref->ref_number == number) {
178 			list_remove(&rc->rc_list, ref);
179 			if (reference_history > 0) {
180 				ref->ref_removed =
181 				    kmem_cache_alloc(reference_history_cache,
182 				    KM_SLEEP);
183 				list_insert_head(&rc->rc_removed, ref);
184 				rc->rc_removed_count++;
185 				if (rc->rc_removed_count > reference_history) {
186 					ref = list_tail(&rc->rc_removed);
187 					list_remove(&rc->rc_removed, ref);
188 					kmem_cache_free(reference_history_cache,
189 					    ref->ref_removed);
190 					kmem_cache_free(reference_cache, ref);
191 					rc->rc_removed_count--;
192 				}
193 			} else {
194 				kmem_cache_free(reference_cache, ref);
195 			}
196 			rc->rc_count -= number;
197 			count = rc->rc_count;
198 			mutex_exit(&rc->rc_mtx);
199 			return (count);
200 		}
201 	}
202 	panic("No such hold %p on refcount %llx", holder,
203 	    (u_longlong_t)(uintptr_t)rc);
204 	return (-1);
205 }
206 
207 int64_t
208 zfs_refcount_remove(zfs_refcount_t *rc, const void *holder)
209 {
210 	return (zfs_refcount_remove_many(rc, 1, holder));
211 }
212 
213 void
214 zfs_refcount_remove_few(zfs_refcount_t *rc, uint64_t number, const void *holder)
215 {
216 	if (!rc->rc_tracked)
217 		(void) zfs_refcount_remove_many(rc, number, holder);
218 	else for (; number > 0; number--)
219 		(void) zfs_refcount_remove(rc, holder);
220 }
221 
222 void
223 zfs_refcount_transfer(zfs_refcount_t *dst, zfs_refcount_t *src)
224 {
225 	int64_t count, removed_count;
226 	list_t list, removed;
227 
228 	list_create(&list, sizeof (reference_t),
229 	    offsetof(reference_t, ref_link));
230 	list_create(&removed, sizeof (reference_t),
231 	    offsetof(reference_t, ref_link));
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 	list_move_tail(&list, &src->rc_list);
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 	list_move_tail(&dst->rc_list, &list);
246 	list_move_tail(&dst->rc_removed, &removed);
247 	mutex_exit(&dst->rc_mtx);
248 
249 	list_destroy(&list);
250 	list_destroy(&removed);
251 }
252 
253 void
254 zfs_refcount_transfer_ownership_many(zfs_refcount_t *rc, uint64_t number,
255     const void *current_holder, const void *new_holder)
256 {
257 	reference_t *ref;
258 	boolean_t found = B_FALSE;
259 
260 	if (!rc->rc_tracked)
261 		return;
262 
263 	mutex_enter(&rc->rc_mtx);
264 	for (ref = list_head(&rc->rc_list); ref;
265 	    ref = list_next(&rc->rc_list, ref)) {
266 		if (ref->ref_holder == current_holder &&
267 		    ref->ref_number == number) {
268 			ref->ref_holder = new_holder;
269 			found = B_TRUE;
270 			break;
271 		}
272 	}
273 	ASSERT(found);
274 	mutex_exit(&rc->rc_mtx);
275 }
276 
277 void
278 zfs_refcount_transfer_ownership(zfs_refcount_t *rc, const void *current_holder,
279     const void *new_holder)
280 {
281 	return (zfs_refcount_transfer_ownership_many(rc, 1, current_holder,
282 	    new_holder));
283 }
284 
285 /*
286  * If tracking is enabled, return true if a reference exists that matches
287  * the "holder" tag. If tracking is disabled, then return true if a reference
288  * might be held.
289  */
290 boolean_t
291 zfs_refcount_held(zfs_refcount_t *rc, const void *holder)
292 {
293 	reference_t *ref;
294 
295 	if (!rc->rc_tracked)
296 		return (zfs_refcount_count(rc) > 0);
297 
298 	mutex_enter(&rc->rc_mtx);
299 	for (ref = list_head(&rc->rc_list); ref;
300 	    ref = list_next(&rc->rc_list, ref)) {
301 		if (ref->ref_holder == holder) {
302 			mutex_exit(&rc->rc_mtx);
303 			return (B_TRUE);
304 		}
305 	}
306 	mutex_exit(&rc->rc_mtx);
307 	return (B_FALSE);
308 }
309 
310 /*
311  * If tracking is enabled, return true if a reference does not exist that
312  * matches the "holder" tag. If tracking is disabled, always return true
313  * since the reference might not be held.
314  */
315 boolean_t
316 zfs_refcount_not_held(zfs_refcount_t *rc, const void *holder)
317 {
318 	reference_t *ref;
319 
320 	if (!rc->rc_tracked)
321 		return (B_TRUE);
322 
323 	mutex_enter(&rc->rc_mtx);
324 	for (ref = list_head(&rc->rc_list); ref;
325 	    ref = list_next(&rc->rc_list, ref)) {
326 		if (ref->ref_holder == holder) {
327 			mutex_exit(&rc->rc_mtx);
328 			return (B_FALSE);
329 		}
330 	}
331 	mutex_exit(&rc->rc_mtx);
332 	return (B_TRUE);
333 }
334 
335 EXPORT_SYMBOL(zfs_refcount_create);
336 EXPORT_SYMBOL(zfs_refcount_destroy);
337 EXPORT_SYMBOL(zfs_refcount_is_zero);
338 EXPORT_SYMBOL(zfs_refcount_count);
339 EXPORT_SYMBOL(zfs_refcount_add);
340 EXPORT_SYMBOL(zfs_refcount_remove);
341 EXPORT_SYMBOL(zfs_refcount_held);
342 
343 /* BEGIN CSTYLED */
344 ZFS_MODULE_PARAM(zfs, , reference_tracking_enable, INT, ZMOD_RW,
345 	"Track reference holders to refcount_t objects");
346 
347 ZFS_MODULE_PARAM(zfs, , reference_history, UINT, ZMOD_RW,
348 	"Maximum reference holders being tracked");
349 /* END CSTYLED */
350 #endif	/* ZFS_DEBUG */
351