]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/refcount.h
Support hardware rate limiting (pacing) with TLS offload.
[FreeBSD/FreeBSD.git] / sys / sys / refcount.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 #ifndef __SYS_REFCOUNT_H__
31 #define __SYS_REFCOUNT_H__
32
33 #include <machine/atomic.h>
34
35 #ifdef _KERNEL
36 #include <sys/systm.h>
37 #else
38 #include <stdbool.h>
39 #define KASSERT(exp, msg)       /* */
40 #endif
41
42 #define REFCOUNT_SATURATED(val)         (((val) & (1U << 31)) != 0)
43 #define REFCOUNT_SATURATION_VALUE       (3U << 30)
44
45 /*
46  * Attempt to handle reference count overflow and underflow.  Force the counter
47  * to stay at the saturation value so that a counter overflow cannot trigger
48  * destruction of the containing object and instead leads to a less harmful
49  * memory leak.
50  */
51 static __inline void
52 _refcount_update_saturated(volatile u_int *count)
53 {
54 #ifdef INVARIANTS
55         panic("refcount %p wraparound", count);
56 #else
57         atomic_store_int(count, REFCOUNT_SATURATION_VALUE);
58 #endif
59 }
60
61 static __inline void
62 refcount_init(volatile u_int *count, u_int value)
63 {
64         KASSERT(!REFCOUNT_SATURATED(value),
65             ("invalid initial refcount value %u", value));
66         atomic_store_int(count, value);
67 }
68
69 static __inline u_int
70 refcount_acquire(volatile u_int *count)
71 {
72         u_int old;
73
74         old = atomic_fetchadd_int(count, 1);
75         if (__predict_false(REFCOUNT_SATURATED(old)))
76                 _refcount_update_saturated(count);
77
78         return (old);
79 }
80
81 static __inline u_int
82 refcount_acquiren(volatile u_int *count, u_int n)
83 {
84         u_int old;
85
86         KASSERT(n < REFCOUNT_SATURATION_VALUE / 2,
87             ("refcount_acquiren: n=%u too large", n));
88         old = atomic_fetchadd_int(count, n);
89         if (__predict_false(REFCOUNT_SATURATED(old)))
90                 _refcount_update_saturated(count);
91
92         return (old);
93 }
94
95 static __inline __result_use_check bool
96 refcount_acquire_checked(volatile u_int *count)
97 {
98         u_int old;
99
100         old = atomic_load_int(count);
101         for (;;) {
102                 if (__predict_false(REFCOUNT_SATURATED(old + 1)))
103                         return (false);
104                 if (__predict_true(atomic_fcmpset_int(count, &old,
105                     old + 1) == 1))
106                         return (true);
107         }
108 }
109
110 /*
111  * This functions returns non-zero if the refcount was
112  * incremented. Else zero is returned.
113  */
114 static __inline __result_use_check bool
115 refcount_acquire_if_gt(volatile u_int *count, u_int n)
116 {
117         u_int old;
118
119         old = atomic_load_int(count);
120         for (;;) {
121                 if (old <= n)
122                         return (false);
123                 if (__predict_false(REFCOUNT_SATURATED(old)))
124                         return (true);
125                 if (atomic_fcmpset_int(count, &old, old + 1))
126                         return (true);
127         }
128 }
129
130 static __inline __result_use_check bool
131 refcount_acquire_if_not_zero(volatile u_int *count)
132 {
133
134         return (refcount_acquire_if_gt(count, 0));
135 }
136
137 static __inline bool
138 refcount_releasen(volatile u_int *count, u_int n)
139 {
140         u_int old;
141
142         KASSERT(n < REFCOUNT_SATURATION_VALUE / 2,
143             ("refcount_releasen: n=%u too large", n));
144
145         atomic_thread_fence_rel();
146         old = atomic_fetchadd_int(count, -n);
147         if (__predict_false(old < n || REFCOUNT_SATURATED(old))) {
148                 _refcount_update_saturated(count);
149                 return (false);
150         }
151         if (old > n)
152                 return (false);
153
154         /*
155          * Last reference.  Signal the user to call the destructor.
156          *
157          * Ensure that the destructor sees all updates. This synchronizes with
158          * release fences from all routines which drop the count.
159          */
160         atomic_thread_fence_acq();
161         return (true);
162 }
163
164 static __inline bool
165 refcount_release(volatile u_int *count)
166 {
167
168         return (refcount_releasen(count, 1));
169 }
170
171 static __inline __result_use_check bool
172 refcount_release_if_gt(volatile u_int *count, u_int n)
173 {
174         u_int old;
175
176         KASSERT(n > 0,
177             ("refcount_release_if_gt: Use refcount_release for final ref"));
178         old = atomic_load_int(count);
179         for (;;) {
180                 if (old <= n)
181                         return (false);
182                 if (__predict_false(REFCOUNT_SATURATED(old)))
183                         return (true);
184                 /*
185                  * Paired with acquire fence in refcount_releasen().
186                  */
187                 if (atomic_fcmpset_rel_int(count, &old, old - 1))
188                         return (true);
189         }
190 }
191
192 static __inline __result_use_check bool
193 refcount_release_if_not_last(volatile u_int *count)
194 {
195
196         return (refcount_release_if_gt(count, 1));
197 }
198
199 #endif /* !__SYS_REFCOUNT_H__ */