kref.h revision 1.9
1/*	$NetBSD: kref.h,v 1.9 2021/12/19 01:46:01 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/mutex.h>
41
42struct kref {
43	unsigned int kr_count;
44};
45
46static inline void
47kref_init(struct kref *kref)
48{
49	kref->kr_count = 1;
50}
51
52static inline void
53kref_get(struct kref *kref)
54{
55	const unsigned int count __unused =
56	    atomic_inc_uint_nv(&kref->kr_count);
57
58	KASSERTMSG((count > 1), "getting released kref");
59
60#ifndef __HAVE_ATOMIC_AS_MEMBAR
61	membar_enter();
62#endif
63}
64
65static inline bool
66kref_get_unless_zero(struct kref *kref)
67{
68	unsigned count;
69
70	do {
71		count = kref->kr_count;
72		if ((count == 0) || (count == UINT_MAX))
73			return false;
74	} while (atomic_cas_uint(&kref->kr_count, count, (count + 1)) !=
75	    count);
76
77#ifndef __HAVE_ATOMIC_AS_MEMBAR
78	membar_enter();
79#endif
80
81	return true;
82}
83
84static inline int
85kref_sub(struct kref *kref, unsigned int count, void (*release)(struct kref *))
86{
87	unsigned int old, new;
88
89#ifndef __HAVE_ATOMIC_AS_MEMBAR
90	membar_exit();
91#endif
92
93	do {
94		old = kref->kr_count;
95		KASSERTMSG((count <= old), "overreleasing kref: %u - %u",
96		    old, count);
97		new = (old - count);
98	} while (atomic_cas_uint(&kref->kr_count, old, new) != old);
99
100	if (new == 0) {
101		(*release)(kref);
102		return 1;
103	}
104
105	return 0;
106}
107
108static inline int
109kref_put(struct kref *kref, void (*release)(struct kref *))
110{
111
112	return kref_sub(kref, 1, release);
113}
114
115static inline int
116kref_put_mutex(struct kref *kref, void (*release)(struct kref *),
117    struct mutex *interlock)
118{
119	unsigned int old, new;
120
121#ifndef __HAVE_ATOMIC_AS_MEMBAR
122	membar_exit();
123#endif
124
125	do {
126		old = kref->kr_count;
127		KASSERT(old > 0);
128		if (old == 1) {
129			mutex_lock(interlock);
130			if (atomic_add_int_nv(&kref->kr_count, -1) == 0) {
131				(*release)(kref);
132				return 1;
133			}
134			mutex_unlock(interlock);
135			return 0;
136		}
137		new = (old - 1);
138	} while (atomic_cas_uint(&kref->kr_count, old, new) != old);
139
140	return 0;
141}
142
143static inline unsigned
144kref_read(const struct kref *kref)
145{
146	unsigned v;
147
148	v = kref->kr_count;
149
150	return v;
151}
152
153/*
154 * Not native to Linux.  Mostly used for assertions...
155 */
156
157static inline bool
158kref_referenced_p(struct kref *kref)
159{
160
161	return (0 < kref->kr_count);
162}
163
164static inline bool
165kref_exclusive_p(struct kref *kref)
166{
167
168	KASSERT(0 < kref->kr_count);
169	return (kref->kr_count == 1);
170}
171
172#endif  /* _LINUX_KREF_H_ */
173