kref.h revision 1.11
1/*	$NetBSD: kref.h,v 1.11 2021/12/19 11:39:00 riastradh Exp $	*/
2
3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _LINUX_KREF_H_
33#define _LINUX_KREF_H_
34
35#include <sys/types.h>
36#include <sys/atomic.h>
37#include <sys/systm.h>
38
39#include <linux/atomic.h>
40#include <linux/refcount.h>
41#include <linux/mutex.h>
42#include <linux/spinlock.h>
43
44struct kref {
45	unsigned int kr_count;
46};
47
48static inline void
49kref_init(struct kref *kref)
50{
51	kref->kr_count = 1;
52}
53
54static inline void
55kref_get(struct kref *kref)
56{
57	const unsigned int count __unused =
58	    atomic_inc_uint_nv(&kref->kr_count);
59
60	KASSERTMSG((count > 1), "getting released kref");
61
62#ifndef __HAVE_ATOMIC_AS_MEMBAR
63	membar_enter();
64#endif
65}
66
67static inline bool
68kref_get_unless_zero(struct kref *kref)
69{
70	unsigned count;
71
72	do {
73		count = kref->kr_count;
74		if ((count == 0) || (count == UINT_MAX))
75			return false;
76	} while (atomic_cas_uint(&kref->kr_count, count, (count + 1)) !=
77	    count);
78
79#ifndef __HAVE_ATOMIC_AS_MEMBAR
80	membar_enter();
81#endif
82
83	return true;
84}
85
86static inline int
87kref_sub(struct kref *kref, unsigned int count, void (*release)(struct kref *))
88{
89	unsigned int old, new;
90
91#ifndef __HAVE_ATOMIC_AS_MEMBAR
92	membar_exit();
93#endif
94
95	do {
96		old = kref->kr_count;
97		KASSERTMSG((count <= old), "overreleasing kref: %u - %u",
98		    old, count);
99		new = (old - count);
100	} while (atomic_cas_uint(&kref->kr_count, old, new) != old);
101
102	if (new == 0) {
103		(*release)(kref);
104		return 1;
105	}
106
107	return 0;
108}
109
110static inline int
111kref_put_lock(struct kref *kref, void (*release)(struct kref *), spinlock_t *interlock)
112{
113	unsigned int old, new;
114
115#ifndef __HAVE_ATOMIC_AS_MEMBAR
116	membar_exit();
117#endif
118
119	do {
120		old = kref->kr_count;
121		KASSERT(old > 0);
122		if (old == 1) {
123			spin_lock(interlock);
124			if (atomic_add_int_nv(&kref->kr_count, -1) == 0) {
125				(*release)(kref);
126				return 1;
127			}
128			spin_unlock(interlock);
129			return 0;
130		}
131		new = (old - 1);
132	} while (atomic_cas_uint(&kref->kr_count, old, new) != old);
133
134	return 0;
135}
136
137static inline int
138kref_put(struct kref *kref, void (*release)(struct kref *))
139{
140
141	return kref_sub(kref, 1, release);
142}
143
144static inline int
145kref_put_mutex(struct kref *kref, void (*release)(struct kref *),
146    struct mutex *interlock)
147{
148	unsigned int old, new;
149
150#ifndef __HAVE_ATOMIC_AS_MEMBAR
151	membar_exit();
152#endif
153
154	do {
155		old = kref->kr_count;
156		KASSERT(old > 0);
157		if (old == 1) {
158			mutex_lock(interlock);
159			if (atomic_add_int_nv(&kref->kr_count, -1) == 0) {
160				(*release)(kref);
161				return 1;
162			}
163			mutex_unlock(interlock);
164			return 0;
165		}
166		new = (old - 1);
167	} while (atomic_cas_uint(&kref->kr_count, old, new) != old);
168
169	return 0;
170}
171
172static inline unsigned
173kref_read(const struct kref *kref)
174{
175	unsigned v;
176
177	v = kref->kr_count;
178
179	return v;
180}
181
182/*
183 * Not native to Linux.  Mostly used for assertions...
184 */
185
186static inline bool
187kref_referenced_p(struct kref *kref)
188{
189
190	return (0 < kref->kr_count);
191}
192
193static inline bool
194kref_exclusive_p(struct kref *kref)
195{
196
197	KASSERT(0 < kref->kr_count);
198	return (kref->kr_count == 1);
199}
200
201#endif  /* _LINUX_KREF_H_ */
202