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 http://www.opensolaris.org/os/licensing.
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, 2015 by Delphix. All rights reserved.
26 #include <sys/zfs_context.h>
27 #include <sys/refcount.h>
32 int reference_tracking_enable = FALSE; /* runs out of memory too easily */
33 SYSCTL_DECL(_vfs_zfs);
34 TUNABLE_INT("vfs.zfs.reference_tracking_enable", &reference_tracking_enable);
35 SYSCTL_INT(_vfs_zfs, OID_AUTO, reference_tracking_enable, CTLFLAG_RDTUN,
36 &reference_tracking_enable, 0,
37 "Track reference holders to refcount_t objects, used mostly by ZFS");
39 int reference_tracking_enable = TRUE;
41 int reference_history = 3; /* tunable */
43 static kmem_cache_t *reference_cache;
44 static kmem_cache_t *reference_history_cache;
47 refcount_sysinit(void)
49 reference_cache = kmem_cache_create("reference_cache",
50 sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
52 reference_history_cache = kmem_cache_create("reference_history_cache",
53 sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
59 kmem_cache_destroy(reference_cache);
60 kmem_cache_destroy(reference_history_cache);
64 refcount_create(refcount_t *rc)
66 mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
67 list_create(&rc->rc_list, sizeof (reference_t),
68 offsetof(reference_t, ref_link));
69 list_create(&rc->rc_removed, sizeof (reference_t),
70 offsetof(reference_t, ref_link));
72 rc->rc_removed_count = 0;
73 rc->rc_tracked = reference_tracking_enable;
77 refcount_create_tracked(refcount_t *rc)
80 rc->rc_tracked = B_TRUE;
84 refcount_create_untracked(refcount_t *rc)
87 rc->rc_tracked = B_FALSE;
91 refcount_destroy_many(refcount_t *rc, uint64_t number)
95 ASSERT(rc->rc_count == number);
96 while (ref = list_head(&rc->rc_list)) {
97 list_remove(&rc->rc_list, ref);
98 kmem_cache_free(reference_cache, ref);
100 list_destroy(&rc->rc_list);
102 while (ref = list_head(&rc->rc_removed)) {
103 list_remove(&rc->rc_removed, ref);
104 kmem_cache_free(reference_history_cache, ref->ref_removed);
105 kmem_cache_free(reference_cache, ref);
107 list_destroy(&rc->rc_removed);
108 mutex_destroy(&rc->rc_mtx);
112 refcount_destroy(refcount_t *rc)
114 refcount_destroy_many(rc, 0);
118 refcount_is_zero(refcount_t *rc)
120 return (rc->rc_count == 0);
124 refcount_count(refcount_t *rc)
126 return (rc->rc_count);
130 refcount_add_many(refcount_t *rc, uint64_t number, void *holder)
132 reference_t *ref = NULL;
135 if (rc->rc_tracked) {
136 ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
137 ref->ref_holder = holder;
138 ref->ref_number = number;
140 mutex_enter(&rc->rc_mtx);
141 ASSERT(rc->rc_count >= 0);
143 list_insert_head(&rc->rc_list, ref);
144 rc->rc_count += number;
145 count = rc->rc_count;
146 mutex_exit(&rc->rc_mtx);
152 refcount_add(refcount_t *rc, void *holder)
154 return (refcount_add_many(rc, 1, holder));
158 refcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
163 mutex_enter(&rc->rc_mtx);
164 ASSERT(rc->rc_count >= number);
166 if (!rc->rc_tracked) {
167 rc->rc_count -= number;
168 count = rc->rc_count;
169 mutex_exit(&rc->rc_mtx);
173 for (ref = list_head(&rc->rc_list); ref;
174 ref = list_next(&rc->rc_list, ref)) {
175 if (ref->ref_holder == holder && ref->ref_number == number) {
176 list_remove(&rc->rc_list, ref);
177 if (reference_history > 0) {
179 kmem_cache_alloc(reference_history_cache,
181 list_insert_head(&rc->rc_removed, ref);
182 rc->rc_removed_count++;
183 if (rc->rc_removed_count > reference_history) {
184 ref = list_tail(&rc->rc_removed);
185 list_remove(&rc->rc_removed, ref);
186 kmem_cache_free(reference_history_cache,
188 kmem_cache_free(reference_cache, ref);
189 rc->rc_removed_count--;
192 kmem_cache_free(reference_cache, ref);
194 rc->rc_count -= number;
195 count = rc->rc_count;
196 mutex_exit(&rc->rc_mtx);
200 panic("No such hold %p on refcount %llx", holder,
201 (u_longlong_t)(uintptr_t)rc);
206 refcount_remove(refcount_t *rc, void *holder)
208 return (refcount_remove_many(rc, 1, holder));
212 refcount_transfer(refcount_t *dst, refcount_t *src)
214 int64_t count, removed_count;
215 list_t list, removed;
217 list_create(&list, sizeof (reference_t),
218 offsetof(reference_t, ref_link));
219 list_create(&removed, sizeof (reference_t),
220 offsetof(reference_t, ref_link));
222 mutex_enter(&src->rc_mtx);
223 count = src->rc_count;
224 removed_count = src->rc_removed_count;
226 src->rc_removed_count = 0;
227 list_move_tail(&list, &src->rc_list);
228 list_move_tail(&removed, &src->rc_removed);
229 mutex_exit(&src->rc_mtx);
231 mutex_enter(&dst->rc_mtx);
232 dst->rc_count += count;
233 dst->rc_removed_count += removed_count;
234 list_move_tail(&dst->rc_list, &list);
235 list_move_tail(&dst->rc_removed, &removed);
236 mutex_exit(&dst->rc_mtx);
239 list_destroy(&removed);
243 refcount_transfer_ownership(refcount_t *rc, void *current_holder,
247 boolean_t found = B_FALSE;
249 mutex_enter(&rc->rc_mtx);
250 if (!rc->rc_tracked) {
251 mutex_exit(&rc->rc_mtx);
255 for (ref = list_head(&rc->rc_list); ref;
256 ref = list_next(&rc->rc_list, ref)) {
257 if (ref->ref_holder == current_holder) {
258 ref->ref_holder = new_holder;
264 mutex_exit(&rc->rc_mtx);
268 * If tracking is enabled, return true if a reference exists that matches
269 * the "holder" tag. If tracking is disabled, then return true if a reference
273 refcount_held(refcount_t *rc, void *holder)
277 mutex_enter(&rc->rc_mtx);
279 if (!rc->rc_tracked) {
280 mutex_exit(&rc->rc_mtx);
281 return (rc->rc_count > 0);
284 for (ref = list_head(&rc->rc_list); ref;
285 ref = list_next(&rc->rc_list, ref)) {
286 if (ref->ref_holder == holder) {
287 mutex_exit(&rc->rc_mtx);
291 mutex_exit(&rc->rc_mtx);
296 * If tracking is enabled, return true if a reference does not exist that
297 * matches the "holder" tag. If tracking is disabled, always return true
298 * since the reference might not be held.
301 refcount_not_held(refcount_t *rc, void *holder)
305 mutex_enter(&rc->rc_mtx);
307 if (!rc->rc_tracked) {
308 mutex_exit(&rc->rc_mtx);
312 for (ref = list_head(&rc->rc_list); ref;
313 ref = list_next(&rc->rc_list, ref)) {
314 if (ref->ref_holder == holder) {
315 mutex_exit(&rc->rc_mtx);
319 mutex_exit(&rc->rc_mtx);
322 #endif /* ZFS_DEBUG */