subr_localcount.c revision 1.6 1 /* $NetBSD: subr_localcount.c,v 1.6 2017/06/12 21:08:34 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2016 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 /*
33 * CPU-local reference counts
34 *
35 * localcount(9) is a reference-counting scheme that involves no
36 * interprocessor synchronization most of the time, at the cost of
37 * eight bytes of memory per CPU per object and at the cost of
38 * expensive interprocessor synchronization to drain references.
39 *
40 * localcount(9) references may be held across sleeps, may be
41 * transferred from CPU to CPU or thread to thread: they behave
42 * semantically like typical reference counts, with different
43 * pragmatic performance characteristics.
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: subr_localcount.c,v 1.6 2017/06/12 21:08:34 riastradh Exp $");
48
49 #include <sys/param.h>
50 #include <sys/localcount.h>
51 #include <sys/types.h>
52 #include <sys/condvar.h>
53 #include <sys/errno.h>
54 #include <sys/mutex.h>
55 #include <sys/percpu.h>
56 #include <sys/xcall.h>
57
58 static void localcount_xc(void *, void *);
59
60 /*
61 * localcount_init(lc)
62 *
63 * Initialize a localcount object. Returns 0 on success, error
64 * code on failure. May fail to allocate memory for percpu(9).
65 *
66 * The caller must call localcount_drain and then localcount_fini
67 * when done with lc.
68 */
69 void
70 localcount_init(struct localcount *lc)
71 {
72
73 lc->lc_totalp = NULL;
74 lc->lc_percpu = percpu_alloc(sizeof(int64_t));
75 }
76
77 /*
78 * localcount_drain(lc, cv, interlock)
79 *
80 * Wait for all acquired references to lc to drain. Caller must
81 * hold interlock; localcount_drain releases it during cross-calls
82 * and waits on cv. The cv and interlock passed here must be the
83 * same as are passed to localcount_release for this lc.
84 *
85 * Caller must guarantee that no new references can be acquired
86 * with localcount_acquire before calling localcount_drain. For
87 * example, any object that may be found in a list and acquired
88 * must be removed from the list before localcount_drain.
89 *
90 * The localcount object lc may be used only with localcount_fini
91 * after this, unless reinitialized after localcount_fini with
92 * localcount_init.
93 */
94 void
95 localcount_drain(struct localcount *lc, kcondvar_t *cv, kmutex_t *interlock)
96 {
97 int64_t total = 0;
98
99 KASSERT(mutex_owned(interlock));
100 KASSERT(lc->lc_totalp == NULL);
101
102 /* Mark it draining. */
103 lc->lc_totalp = &total;
104
105 /*
106 * Count up all references on all CPUs.
107 *
108 * This serves as a global memory barrier: after xc_wait, all
109 * CPUs will have witnessed the nonnull value of lc->lc_totalp,
110 * so that it is safe to wait on the cv for them.
111 */
112 mutex_exit(interlock);
113 xc_wait(xc_broadcast(0, &localcount_xc, lc, interlock));
114 mutex_enter(interlock);
115
116 /* Wait for remaining references to drain. */
117 while (total != 0) {
118 /*
119 * At this point, now that we have added up all
120 * references on all CPUs, the total had better be
121 * nonnegative.
122 */
123 KASSERTMSG((0 < total),
124 "negatively referenced localcount: %p, %"PRId64,
125 lc, total);
126 cv_wait(cv, interlock);
127 }
128
129 /* Paranoia: Cause any further use of lc->lc_totalp to crash. */
130 lc->lc_totalp = (void *)(uintptr_t)1;
131 }
132
133 /*
134 * localcount_fini(lc)
135 *
136 * Finalize a localcount object, releasing any memory allocated
137 * for it. The localcount object must already have been drained.
138 */
139 void
140 localcount_fini(struct localcount *lc)
141 {
142
143 KASSERT(lc->lc_totalp == (void *)(uintptr_t)1);
144 percpu_free(lc->lc_percpu, sizeof(uint64_t));
145 }
146
147 /*
148 * localcount_xc(cookie0, cookie1)
149 *
150 * Accumulate and transfer the per-CPU reference counts to a
151 * global total, resetting the per-CPU counter to zero. Once
152 * localcount_drain() has started, we only maintain the total
153 * count in localcount_release().
154 */
155 static void
156 localcount_xc(void *cookie0, void *cookie1)
157 {
158 struct localcount *lc = cookie0;
159 kmutex_t *interlock = cookie1;
160 int64_t *localp;
161
162 mutex_enter(interlock);
163 localp = percpu_getref(lc->lc_percpu);
164 *lc->lc_totalp += *localp;
165 *localp -= *localp; /* ie, *localp = 0; */
166 percpu_putref(lc->lc_percpu);
167 mutex_exit(interlock);
168 }
169
170 /*
171 * localcount_adjust(lc, delta)
172 *
173 * Add delta -- positive or negative -- to the local CPU's count
174 * for lc.
175 */
176 static void
177 localcount_adjust(struct localcount *lc, int delta)
178 {
179 int64_t *localp;
180
181 localp = percpu_getref(lc->lc_percpu);
182 *localp += delta;
183 percpu_putref(lc->lc_percpu);
184 }
185
186 /*
187 * localcount_acquire(lc)
188 *
189 * Acquire a reference to lc.
190 *
191 * The reference may be held across sleeps and may be migrated
192 * from CPU to CPU, or even thread to thread -- it is only
193 * counted, not associated with a particular concrete owner.
194 *
195 * Involves no interprocessor synchronization. May be used in any
196 * context: while a lock is held, within a pserialize(9) read
197 * section, in hard interrupt context (provided other users block
198 * hard interrupts), in soft interrupt context, in thread context,
199 * &c.
200 *
201 * Caller must guarantee that there is no concurrent
202 * localcount_drain. For example, any object that may be found in
203 * a list and acquired must be removed from the list before
204 * localcount_drain.
205 */
206 void
207 localcount_acquire(struct localcount *lc)
208 {
209
210 KASSERT(lc->lc_totalp == NULL);
211 localcount_adjust(lc, +1);
212 }
213
214 /*
215 * localcount_release(lc, cv, interlock)
216 *
217 * Release a reference to lc. If there is a concurrent
218 * localcount_drain and this may be the last reference, notify
219 * localcount_drain by acquiring interlock, waking cv, and
220 * releasing interlock. The cv and interlock passed here must be
221 * the same as are passed to localcount_drain for this lc.
222 *
223 * Involves no interprocessor synchronization unless there is a
224 * concurrent localcount_drain in progress.
225 */
226 void
227 localcount_release(struct localcount *lc, kcondvar_t *cv, kmutex_t *interlock)
228 {
229
230 /*
231 * Block xcall so that if someone begins draining after we see
232 * lc->lc_totalp as null, then they won't start cv_wait until
233 * after they have counted this CPU's contributions.
234 *
235 * Otherwise, localcount_drain may notice an extant reference
236 * from this CPU and cv_wait for it, but having seen
237 * lc->lc_totalp as null, this CPU will not wake
238 * localcount_drain.
239 */
240 kpreempt_disable();
241
242 KDASSERT(mutex_ownable(interlock));
243 if (__predict_false(lc->lc_totalp != NULL)) {
244 /*
245 * Slow path -- wake localcount_drain in case this is
246 * the last reference.
247 */
248 mutex_enter(interlock);
249 if (--*lc->lc_totalp == 0)
250 cv_broadcast(cv);
251 mutex_exit(interlock);
252 goto out;
253 }
254
255 localcount_adjust(lc, -1);
256 out: kpreempt_enable();
257 }
258