refcount.c revision 307266
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 http://www.opensolaris.org/os/licensing. 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 by Delphix. All rights reserved. 24 */ 25 26#include <sys/zfs_context.h> 27#include <sys/refcount.h> 28 29#ifdef ZFS_DEBUG 30 31#ifdef _KERNEL 32int reference_tracking_enable = FALSE; /* runs out of memory too easily */ 33SYSCTL_DECL(_vfs_zfs); 34TUNABLE_INT("vfs.zfs.reference_tracking_enable", &reference_tracking_enable); 35SYSCTL_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"); 38#else 39int reference_tracking_enable = TRUE; 40#endif 41int reference_history = 3; /* tunable */ 42 43static kmem_cache_t *reference_cache; 44static kmem_cache_t *reference_history_cache; 45 46void 47refcount_sysinit(void) 48{ 49 reference_cache = kmem_cache_create("reference_cache", 50 sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 51 52 reference_history_cache = kmem_cache_create("reference_history_cache", 53 sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 54} 55 56void 57refcount_fini(void) 58{ 59 kmem_cache_destroy(reference_cache); 60 kmem_cache_destroy(reference_history_cache); 61} 62 63void 64refcount_create(refcount_t *rc) 65{ 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)); 71 rc->rc_count = 0; 72 rc->rc_removed_count = 0; 73 rc->rc_tracked = reference_tracking_enable; 74} 75 76void 77refcount_create_untracked(refcount_t *rc) 78{ 79 refcount_create(rc); 80 rc->rc_tracked = B_FALSE; 81} 82 83void 84refcount_destroy_many(refcount_t *rc, uint64_t number) 85{ 86 reference_t *ref; 87 88 ASSERT(rc->rc_count == number); 89 while (ref = list_head(&rc->rc_list)) { 90 list_remove(&rc->rc_list, ref); 91 kmem_cache_free(reference_cache, ref); 92 } 93 list_destroy(&rc->rc_list); 94 95 while (ref = list_head(&rc->rc_removed)) { 96 list_remove(&rc->rc_removed, ref); 97 kmem_cache_free(reference_history_cache, ref->ref_removed); 98 kmem_cache_free(reference_cache, ref); 99 } 100 list_destroy(&rc->rc_removed); 101 mutex_destroy(&rc->rc_mtx); 102} 103 104void 105refcount_destroy(refcount_t *rc) 106{ 107 refcount_destroy_many(rc, 0); 108} 109 110int 111refcount_is_zero(refcount_t *rc) 112{ 113 return (rc->rc_count == 0); 114} 115 116int64_t 117refcount_count(refcount_t *rc) 118{ 119 return (rc->rc_count); 120} 121 122int64_t 123refcount_add_many(refcount_t *rc, uint64_t number, void *holder) 124{ 125 reference_t *ref = NULL; 126 int64_t count; 127 128 if (rc->rc_tracked) { 129 ref = kmem_cache_alloc(reference_cache, KM_SLEEP); 130 ref->ref_holder = holder; 131 ref->ref_number = number; 132 } 133 mutex_enter(&rc->rc_mtx); 134 ASSERT(rc->rc_count >= 0); 135 if (rc->rc_tracked) 136 list_insert_head(&rc->rc_list, ref); 137 rc->rc_count += number; 138 count = rc->rc_count; 139 mutex_exit(&rc->rc_mtx); 140 141 return (count); 142} 143 144int64_t 145refcount_add(refcount_t *rc, void *holder) 146{ 147 return (refcount_add_many(rc, 1, holder)); 148} 149 150int64_t 151refcount_remove_many(refcount_t *rc, uint64_t number, void *holder) 152{ 153 reference_t *ref; 154 int64_t count; 155 156 mutex_enter(&rc->rc_mtx); 157 ASSERT(rc->rc_count >= number); 158 159 if (!rc->rc_tracked) { 160 rc->rc_count -= number; 161 count = rc->rc_count; 162 mutex_exit(&rc->rc_mtx); 163 return (count); 164 } 165 166 for (ref = list_head(&rc->rc_list); ref; 167 ref = list_next(&rc->rc_list, ref)) { 168 if (ref->ref_holder == holder && ref->ref_number == number) { 169 list_remove(&rc->rc_list, ref); 170 if (reference_history > 0) { 171 ref->ref_removed = 172 kmem_cache_alloc(reference_history_cache, 173 KM_SLEEP); 174 list_insert_head(&rc->rc_removed, ref); 175 rc->rc_removed_count++; 176 if (rc->rc_removed_count > reference_history) { 177 ref = list_tail(&rc->rc_removed); 178 list_remove(&rc->rc_removed, ref); 179 kmem_cache_free(reference_history_cache, 180 ref->ref_removed); 181 kmem_cache_free(reference_cache, ref); 182 rc->rc_removed_count--; 183 } 184 } else { 185 kmem_cache_free(reference_cache, ref); 186 } 187 rc->rc_count -= number; 188 count = rc->rc_count; 189 mutex_exit(&rc->rc_mtx); 190 return (count); 191 } 192 } 193 panic("No such hold %p on refcount %llx", holder, 194 (u_longlong_t)(uintptr_t)rc); 195 return (-1); 196} 197 198int64_t 199refcount_remove(refcount_t *rc, void *holder) 200{ 201 return (refcount_remove_many(rc, 1, holder)); 202} 203 204void 205refcount_transfer(refcount_t *dst, refcount_t *src) 206{ 207 int64_t count, removed_count; 208 list_t list, removed; 209 210 list_create(&list, sizeof (reference_t), 211 offsetof(reference_t, ref_link)); 212 list_create(&removed, sizeof (reference_t), 213 offsetof(reference_t, ref_link)); 214 215 mutex_enter(&src->rc_mtx); 216 count = src->rc_count; 217 removed_count = src->rc_removed_count; 218 src->rc_count = 0; 219 src->rc_removed_count = 0; 220 list_move_tail(&list, &src->rc_list); 221 list_move_tail(&removed, &src->rc_removed); 222 mutex_exit(&src->rc_mtx); 223 224 mutex_enter(&dst->rc_mtx); 225 dst->rc_count += count; 226 dst->rc_removed_count += removed_count; 227 list_move_tail(&dst->rc_list, &list); 228 list_move_tail(&dst->rc_removed, &removed); 229 mutex_exit(&dst->rc_mtx); 230 231 list_destroy(&list); 232 list_destroy(&removed); 233} 234 235void 236refcount_transfer_ownership(refcount_t *rc, void *current_holder, 237 void *new_holder) 238{ 239 reference_t *ref; 240 boolean_t found = B_FALSE; 241 242 mutex_enter(&rc->rc_mtx); 243 if (!rc->rc_tracked) { 244 mutex_exit(&rc->rc_mtx); 245 return; 246 } 247 248 for (ref = list_head(&rc->rc_list); ref; 249 ref = list_next(&rc->rc_list, ref)) { 250 if (ref->ref_holder == current_holder) { 251 ref->ref_holder = new_holder; 252 found = B_TRUE; 253 break; 254 } 255 } 256 ASSERT(found); 257 mutex_exit(&rc->rc_mtx); 258} 259#endif /* ZFS_DEBUG */ 260