refcount.c revision 277492
1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5185029Spjd * Common Development and Distribution License (the "License").
6185029Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23248571Smm * Copyright (c) 2012 by Delphix. All rights reserved.
24168404Spjd */
25168404Spjd
26168404Spjd#include <sys/zfs_context.h>
27168404Spjd#include <sys/refcount.h>
28168404Spjd
29219089Spjd#ifdef	ZFS_DEBUG
30168404Spjd
31168404Spjd#ifdef _KERNEL
32168404Spjdint reference_tracking_enable = FALSE; /* runs out of memory too easily */
33277492SwillSYSCTL_DECL(_vfs_zfs);
34277492SwillSYSCTL_INT(_vfs_zfs, OID_AUTO, reference_tracking_enable, CTLFLAG_RDTUN,
35277492Swill    &reference_tracking_enable, 0,
36277492Swill    "Track reference holders to refcount_t objects, used mostly by ZFS");
37168404Spjd#else
38168404Spjdint reference_tracking_enable = TRUE;
39168404Spjd#endif
40248571Smmint reference_history = 3; /* tunable */
41168404Spjd
42168404Spjdstatic kmem_cache_t *reference_cache;
43168404Spjdstatic kmem_cache_t *reference_history_cache;
44168404Spjd
45168404Spjdvoid
46179310Spjdrefcount_sysinit(void)
47168404Spjd{
48168404Spjd	reference_cache = kmem_cache_create("reference_cache",
49168404Spjd	    sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
50168404Spjd
51168404Spjd	reference_history_cache = kmem_cache_create("reference_history_cache",
52168404Spjd	    sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
53168404Spjd}
54168404Spjd
55168404Spjdvoid
56168404Spjdrefcount_fini(void)
57168404Spjd{
58168404Spjd	kmem_cache_destroy(reference_cache);
59168404Spjd	kmem_cache_destroy(reference_history_cache);
60168404Spjd}
61168404Spjd
62168404Spjdvoid
63168404Spjdrefcount_create(refcount_t *rc)
64168404Spjd{
65185029Spjd	mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
66168404Spjd	list_create(&rc->rc_list, sizeof (reference_t),
67168404Spjd	    offsetof(reference_t, ref_link));
68168404Spjd	list_create(&rc->rc_removed, sizeof (reference_t),
69168404Spjd	    offsetof(reference_t, ref_link));
70185029Spjd	rc->rc_count = 0;
71185029Spjd	rc->rc_removed_count = 0;
72248571Smm	rc->rc_tracked = reference_tracking_enable;
73168404Spjd}
74168404Spjd
75168404Spjdvoid
76248571Smmrefcount_create_untracked(refcount_t *rc)
77248571Smm{
78248571Smm	refcount_create(rc);
79248571Smm	rc->rc_tracked = B_FALSE;
80248571Smm}
81248571Smm
82248571Smmvoid
83168404Spjdrefcount_destroy_many(refcount_t *rc, uint64_t number)
84168404Spjd{
85168404Spjd	reference_t *ref;
86168404Spjd
87168404Spjd	ASSERT(rc->rc_count == number);
88168404Spjd	while (ref = list_head(&rc->rc_list)) {
89168404Spjd		list_remove(&rc->rc_list, ref);
90168404Spjd		kmem_cache_free(reference_cache, ref);
91168404Spjd	}
92168404Spjd	list_destroy(&rc->rc_list);
93168404Spjd
94168404Spjd	while (ref = list_head(&rc->rc_removed)) {
95168404Spjd		list_remove(&rc->rc_removed, ref);
96168404Spjd		kmem_cache_free(reference_history_cache, ref->ref_removed);
97168404Spjd		kmem_cache_free(reference_cache, ref);
98168404Spjd	}
99168404Spjd	list_destroy(&rc->rc_removed);
100168404Spjd	mutex_destroy(&rc->rc_mtx);
101168404Spjd}
102168404Spjd
103168404Spjdvoid
104168404Spjdrefcount_destroy(refcount_t *rc)
105168404Spjd{
106168404Spjd	refcount_destroy_many(rc, 0);
107168404Spjd}
108168404Spjd
109168404Spjdint
110168404Spjdrefcount_is_zero(refcount_t *rc)
111168404Spjd{
112168404Spjd	return (rc->rc_count == 0);
113168404Spjd}
114168404Spjd
115168404Spjdint64_t
116168404Spjdrefcount_count(refcount_t *rc)
117168404Spjd{
118168404Spjd	return (rc->rc_count);
119168404Spjd}
120168404Spjd
121168404Spjdint64_t
122168404Spjdrefcount_add_many(refcount_t *rc, uint64_t number, void *holder)
123168404Spjd{
124247187Smm	reference_t *ref = NULL;
125168404Spjd	int64_t count;
126168404Spjd
127248571Smm	if (rc->rc_tracked) {
128168404Spjd		ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
129168404Spjd		ref->ref_holder = holder;
130168404Spjd		ref->ref_number = number;
131168404Spjd	}
132168404Spjd	mutex_enter(&rc->rc_mtx);
133168404Spjd	ASSERT(rc->rc_count >= 0);
134248571Smm	if (rc->rc_tracked)
135168404Spjd		list_insert_head(&rc->rc_list, ref);
136168404Spjd	rc->rc_count += number;
137168404Spjd	count = rc->rc_count;
138168404Spjd	mutex_exit(&rc->rc_mtx);
139168404Spjd
140168404Spjd	return (count);
141168404Spjd}
142168404Spjd
143168404Spjdint64_t
144168404Spjdrefcount_add(refcount_t *rc, void *holder)
145168404Spjd{
146168404Spjd	return (refcount_add_many(rc, 1, holder));
147168404Spjd}
148168404Spjd
149168404Spjdint64_t
150168404Spjdrefcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
151168404Spjd{
152168404Spjd	reference_t *ref;
153168404Spjd	int64_t count;
154168404Spjd
155168404Spjd	mutex_enter(&rc->rc_mtx);
156168404Spjd	ASSERT(rc->rc_count >= number);
157168404Spjd
158248571Smm	if (!rc->rc_tracked) {
159168404Spjd		rc->rc_count -= number;
160168404Spjd		count = rc->rc_count;
161168404Spjd		mutex_exit(&rc->rc_mtx);
162168404Spjd		return (count);
163168404Spjd	}
164168404Spjd
165168404Spjd	for (ref = list_head(&rc->rc_list); ref;
166168404Spjd	    ref = list_next(&rc->rc_list, ref)) {
167168404Spjd		if (ref->ref_holder == holder && ref->ref_number == number) {
168168404Spjd			list_remove(&rc->rc_list, ref);
169168404Spjd			if (reference_history > 0) {
170168404Spjd				ref->ref_removed =
171168404Spjd				    kmem_cache_alloc(reference_history_cache,
172168404Spjd				    KM_SLEEP);
173168404Spjd				list_insert_head(&rc->rc_removed, ref);
174168404Spjd				rc->rc_removed_count++;
175248571Smm				if (rc->rc_removed_count > reference_history) {
176168404Spjd					ref = list_tail(&rc->rc_removed);
177168404Spjd					list_remove(&rc->rc_removed, ref);
178168404Spjd					kmem_cache_free(reference_history_cache,
179168404Spjd					    ref->ref_removed);
180168404Spjd					kmem_cache_free(reference_cache, ref);
181168404Spjd					rc->rc_removed_count--;
182168404Spjd				}
183168404Spjd			} else {
184168404Spjd				kmem_cache_free(reference_cache, ref);
185168404Spjd			}
186168404Spjd			rc->rc_count -= number;
187168404Spjd			count = rc->rc_count;
188168404Spjd			mutex_exit(&rc->rc_mtx);
189168404Spjd			return (count);
190168404Spjd		}
191168404Spjd	}
192168404Spjd	panic("No such hold %p on refcount %llx", holder,
193168404Spjd	    (u_longlong_t)(uintptr_t)rc);
194168404Spjd	return (-1);
195168404Spjd}
196168404Spjd
197168404Spjdint64_t
198168404Spjdrefcount_remove(refcount_t *rc, void *holder)
199168404Spjd{
200168404Spjd	return (refcount_remove_many(rc, 1, holder));
201168404Spjd}
202168404Spjd
203219089Spjdvoid
204219089Spjdrefcount_transfer(refcount_t *dst, refcount_t *src)
205219089Spjd{
206219089Spjd	int64_t count, removed_count;
207219089Spjd	list_t list, removed;
208219089Spjd
209219089Spjd	list_create(&list, sizeof (reference_t),
210219089Spjd	    offsetof(reference_t, ref_link));
211219089Spjd	list_create(&removed, sizeof (reference_t),
212219089Spjd	    offsetof(reference_t, ref_link));
213219089Spjd
214219089Spjd	mutex_enter(&src->rc_mtx);
215219089Spjd	count = src->rc_count;
216219089Spjd	removed_count = src->rc_removed_count;
217219089Spjd	src->rc_count = 0;
218219089Spjd	src->rc_removed_count = 0;
219219089Spjd	list_move_tail(&list, &src->rc_list);
220219089Spjd	list_move_tail(&removed, &src->rc_removed);
221219089Spjd	mutex_exit(&src->rc_mtx);
222219089Spjd
223219089Spjd	mutex_enter(&dst->rc_mtx);
224219089Spjd	dst->rc_count += count;
225219089Spjd	dst->rc_removed_count += removed_count;
226219089Spjd	list_move_tail(&dst->rc_list, &list);
227219089Spjd	list_move_tail(&dst->rc_removed, &removed);
228219089Spjd	mutex_exit(&dst->rc_mtx);
229219089Spjd
230219089Spjd	list_destroy(&list);
231219089Spjd	list_destroy(&removed);
232219089Spjd}
233219089Spjd
234219089Spjd#endif	/* ZFS_DEBUG */
235