1 /* $NetBSD: kref.h,v 1.12 2021/12/19 11:45: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/refcount.h> 41 #include <linux/mutex.h> 42 #include <linux/spinlock.h> 43 44 struct kref { 45 unsigned int kr_count; 46 }; 47 48 static inline void 49 kref_init(struct kref *kref) 50 { 51 atomic_store_relaxed(&kref->kr_count, 1); 52 } 53 54 static inline void 55 kref_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 67 static inline bool 68 kref_get_unless_zero(struct kref *kref) 69 { 70 unsigned count; 71 72 do { 73 count = atomic_load_relaxed(&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 86 static inline int 87 kref_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 = atomic_load_relaxed(&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 110 static inline int 111 kref_put_lock(struct kref *kref, void (*release)(struct kref *), 112 spinlock_t *interlock) 113 { 114 unsigned int old, new; 115 116 #ifndef __HAVE_ATOMIC_AS_MEMBAR 117 membar_exit(); 118 #endif 119 120 do { 121 old = atomic_load_relaxed(&kref->kr_count); 122 KASSERT(old > 0); 123 if (old == 1) { 124 spin_lock(interlock); 125 if (atomic_add_int_nv(&kref->kr_count, -1) == 0) { 126 (*release)(kref); 127 return 1; 128 } 129 spin_unlock(interlock); 130 return 0; 131 } 132 new = (old - 1); 133 } while (atomic_cas_uint(&kref->kr_count, old, new) != old); 134 135 return 0; 136 } 137 138 static inline int 139 kref_put(struct kref *kref, void (*release)(struct kref *)) 140 { 141 142 return kref_sub(kref, 1, release); 143 } 144 145 static inline int 146 kref_put_mutex(struct kref *kref, void (*release)(struct kref *), 147 struct mutex *interlock) 148 { 149 unsigned int old, new; 150 151 #ifndef __HAVE_ATOMIC_AS_MEMBAR 152 membar_exit(); 153 #endif 154 155 do { 156 old = atomic_load_relaxed(&kref->kr_count); 157 KASSERT(old > 0); 158 if (old == 1) { 159 mutex_lock(interlock); 160 if (atomic_add_int_nv(&kref->kr_count, -1) == 0) { 161 (*release)(kref); 162 return 1; 163 } 164 mutex_unlock(interlock); 165 return 0; 166 } 167 new = (old - 1); 168 } while (atomic_cas_uint(&kref->kr_count, old, new) != old); 169 170 return 0; 171 } 172 173 static inline unsigned 174 kref_read(const struct kref *kref) 175 { 176 177 return atomic_load_relaxed(&kref->kr_count); 178 } 179 180 /* 181 * Not native to Linux. Mostly used for assertions... 182 */ 183 184 static inline bool 185 kref_referenced_p(struct kref *kref) 186 { 187 188 return (0 < kref->kr_count); 189 } 190 191 static inline bool 192 kref_exclusive_p(struct kref *kref) 193 { 194 195 KASSERT(0 < kref->kr_count); 196 return (kref->kr_count == 1); 197 } 198 199 #endif /* _LINUX_KREF_H_ */ 200