subr_localcount.c revision 1.2 1 1.2 pgoyette /* $NetBSD: subr_localcount.c,v 1.2 2017/05/19 00:01:33 pgoyette Exp $ */
2 1.2 pgoyette
3 1.2 pgoyette /*-
4 1.2 pgoyette * Copyright (c) 2016 The NetBSD Foundation, Inc.
5 1.2 pgoyette * All rights reserved.
6 1.2 pgoyette *
7 1.2 pgoyette * This code is derived from software contributed to The NetBSD Foundation
8 1.2 pgoyette * by Taylor R. Campbell.
9 1.2 pgoyette *
10 1.2 pgoyette * Redistribution and use in source and binary forms, with or without
11 1.2 pgoyette * modification, are permitted provided that the following conditions
12 1.2 pgoyette * are met:
13 1.2 pgoyette * 1. Redistributions of source code must retain the above copyright
14 1.2 pgoyette * notice, this list of conditions and the following disclaimer.
15 1.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 pgoyette * notice, this list of conditions and the following disclaimer in the
17 1.2 pgoyette * documentation and/or other materials provided with the distribution.
18 1.2 pgoyette *
19 1.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
30 1.2 pgoyette */
31 1.2 pgoyette
32 1.2 pgoyette /*
33 1.2 pgoyette * CPU-local reference counts
34 1.2 pgoyette *
35 1.2 pgoyette * localcount(9) is a reference-counting scheme that involves no
36 1.2 pgoyette * interprocessor synchronization most of the time, at the cost of
37 1.2 pgoyette * eight bytes of memory per CPU per object and at the cost of
38 1.2 pgoyette * expensive interprocessor synchronization to drain references.
39 1.2 pgoyette *
40 1.2 pgoyette * localcount(9) references may be held across sleeps, may be
41 1.2 pgoyette * transferred from CPU to CPU or thread to thread: they behave
42 1.2 pgoyette * semantically like typical reference counts, with different
43 1.2 pgoyette * pragmatic performance characteristics.
44 1.2 pgoyette */
45 1.2 pgoyette
46 1.2 pgoyette #include <sys/cdefs.h>
47 1.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: subr_localcount.c,v 1.2 2017/05/19 00:01:33 pgoyette Exp $");
48 1.2 pgoyette
49 1.2 pgoyette #include <sys/param.h>
50 1.2 pgoyette #include <sys/localcount.h>
51 1.2 pgoyette #include <sys/types.h>
52 1.2 pgoyette #include <sys/condvar.h>
53 1.2 pgoyette #include <sys/errno.h>
54 1.2 pgoyette #include <sys/mutex.h>
55 1.2 pgoyette #include <sys/percpu.h>
56 1.2 pgoyette #include <sys/xcall.h>
57 1.2 pgoyette
58 1.2 pgoyette /*
59 1.2 pgoyette * localcount_init(lc)
60 1.2 pgoyette *
61 1.2 pgoyette * Initialize a localcount object. Returns 0 on success, error
62 1.2 pgoyette * code on failure. May fail to allocate memory for percpu(9).
63 1.2 pgoyette *
64 1.2 pgoyette * The caller must call localcount_drain and then localcount_fini
65 1.2 pgoyette * when done with lc.
66 1.2 pgoyette */
67 1.2 pgoyette static void localcount_xc(void *, void *);
68 1.2 pgoyette
69 1.2 pgoyette int
70 1.2 pgoyette localcount_init(struct localcount *lc)
71 1.2 pgoyette {
72 1.2 pgoyette
73 1.2 pgoyette lc->lc_totalp = NULL;
74 1.2 pgoyette lc->lc_percpu = percpu_alloc(sizeof(int64_t));
75 1.2 pgoyette if (lc->lc_percpu == NULL)
76 1.2 pgoyette return ENOMEM;
77 1.2 pgoyette
78 1.2 pgoyette return 0;
79 1.2 pgoyette }
80 1.2 pgoyette
81 1.2 pgoyette /*
82 1.2 pgoyette * localcount_drain(lc, cv, interlock)
83 1.2 pgoyette *
84 1.2 pgoyette * Wait for all acquired references to lc to drain. Caller must
85 1.2 pgoyette * hold interlock; localcount_drain releases it during cross-calls
86 1.2 pgoyette * and waits on cv. The cv and interlock passed here must be the
87 1.2 pgoyette * same as are passed to localcount_release for this lc.
88 1.2 pgoyette *
89 1.2 pgoyette * Caller must guarantee that no new references can be acquired
90 1.2 pgoyette * with localcount_acquire before calling localcount_drain. For
91 1.2 pgoyette * example, any object that may be found in a list and acquired
92 1.2 pgoyette * must be removed from the list before localcount_drain.
93 1.2 pgoyette *
94 1.2 pgoyette * The localcount object lc may be used only with localcount_fini
95 1.2 pgoyette * after this, unless reinitialized after localcount_fini with
96 1.2 pgoyette * localcount_init.
97 1.2 pgoyette */
98 1.2 pgoyette void
99 1.2 pgoyette localcount_drain(struct localcount *lc, kcondvar_t *cv, kmutex_t *interlock)
100 1.2 pgoyette {
101 1.2 pgoyette int64_t total = 0;
102 1.2 pgoyette
103 1.2 pgoyette KASSERT(mutex_owned(interlock));
104 1.2 pgoyette KASSERT(lc->lc_totalp == NULL);
105 1.2 pgoyette
106 1.2 pgoyette /* Mark it draining. */
107 1.2 pgoyette lc->lc_totalp = &total;
108 1.2 pgoyette
109 1.2 pgoyette /*
110 1.2 pgoyette * Count up all references on all CPUs.
111 1.2 pgoyette *
112 1.2 pgoyette * This serves as a global memory barrier: after xc_wait, all
113 1.2 pgoyette * CPUs will have witnessed the nonnull value of lc->lc_totalp,
114 1.2 pgoyette * so that it is safe to wait on the cv for them.
115 1.2 pgoyette */
116 1.2 pgoyette mutex_exit(interlock);
117 1.2 pgoyette xc_wait(xc_broadcast(0, &localcount_xc, lc, interlock));
118 1.2 pgoyette mutex_enter(interlock);
119 1.2 pgoyette
120 1.2 pgoyette /* Wait for remaining references to drain. */
121 1.2 pgoyette while (total != 0) {
122 1.2 pgoyette /*
123 1.2 pgoyette * At this point, now that we have added up all
124 1.2 pgoyette * references on all CPUs, the total had better be
125 1.2 pgoyette * nonnegative.
126 1.2 pgoyette */
127 1.2 pgoyette KASSERTMSG((0 < total),
128 1.2 pgoyette "negatively referenced localcount: %p, %"PRId64,
129 1.2 pgoyette lc, total);
130 1.2 pgoyette cv_wait(cv, interlock);
131 1.2 pgoyette }
132 1.2 pgoyette
133 1.2 pgoyette /* Paranoia: Cause any further use of lc->lc_totalp to crash. */
134 1.2 pgoyette lc->lc_totalp = (void *)(uintptr_t)1;
135 1.2 pgoyette }
136 1.2 pgoyette
137 1.2 pgoyette /*
138 1.2 pgoyette * localcount_fini(lc)
139 1.2 pgoyette *
140 1.2 pgoyette * Finalize a localcount object, releasing any memory allocated
141 1.2 pgoyette * for it. Caller must have already called localcount_drain.
142 1.2 pgoyette */
143 1.2 pgoyette void
144 1.2 pgoyette localcount_fini(struct localcount *lc)
145 1.2 pgoyette {
146 1.2 pgoyette
147 1.2 pgoyette KASSERT(lc->lc_totalp == (void *)(uintptr_t)1);
148 1.2 pgoyette percpu_free(lc->lc_percpu, sizeof(uint64_t));
149 1.2 pgoyette }
150 1.2 pgoyette
151 1.2 pgoyette /*
152 1.2 pgoyette * localcount_xc(cookie0, cookie1)
153 1.2 pgoyette *
154 1.2 pgoyette * Accumulate and transfer the per-CPU reference counts to a
155 1.2 pgoyette * global total, resetting the per-CPU counter to zero. Once
156 1.2 pgoyette * localcount_drain() has started, we only maintain the total
157 1.2 pgoyette * count in localcount_release().
158 1.2 pgoyette */
159 1.2 pgoyette static void
160 1.2 pgoyette localcount_xc(void *cookie0, void *cookie1)
161 1.2 pgoyette {
162 1.2 pgoyette struct localcount *lc = cookie0;
163 1.2 pgoyette kmutex_t *interlock = cookie1;
164 1.2 pgoyette int64_t *localp;
165 1.2 pgoyette
166 1.2 pgoyette mutex_enter(interlock);
167 1.2 pgoyette localp = percpu_getref(lc->lc_percpu);
168 1.2 pgoyette *lc->lc_totalp += *localp;
169 1.2 pgoyette *localp -= *localp; /* ie, *localp = 0; */
170 1.2 pgoyette percpu_putref(lc->lc_percpu);
171 1.2 pgoyette mutex_exit(interlock);
172 1.2 pgoyette }
173 1.2 pgoyette
174 1.2 pgoyette static void
175 1.2 pgoyette localcount_adjust(struct localcount *lc, int delta)
176 1.2 pgoyette {
177 1.2 pgoyette int64_t *localp;
178 1.2 pgoyette
179 1.2 pgoyette localp = percpu_getref(lc->lc_percpu);
180 1.2 pgoyette *localp += delta;
181 1.2 pgoyette percpu_putref(lc->lc_percpu);
182 1.2 pgoyette }
183 1.2 pgoyette
184 1.2 pgoyette /*
185 1.2 pgoyette * localcount_acquire(lc)
186 1.2 pgoyette *
187 1.2 pgoyette * Acquire a reference to lc.
188 1.2 pgoyette *
189 1.2 pgoyette * The reference may be held across sleeps and may be migrated
190 1.2 pgoyette * from CPU to CPU, or even thread to thread -- it is only
191 1.2 pgoyette * counted, not associated with a particular concrete owner.
192 1.2 pgoyette *
193 1.2 pgoyette * Involves no interprocessor synchronization. May be used in any
194 1.2 pgoyette * context: while a lock is held, within a pserialize(9) read
195 1.2 pgoyette * section, in hard interrupt context (provided other users block
196 1.2 pgoyette * hard interrupts), in soft interrupt context, in thread context,
197 1.2 pgoyette * &c.
198 1.2 pgoyette *
199 1.2 pgoyette * Caller must guarantee that there is no concurrent
200 1.2 pgoyette * localcount_drain. For example, any object that may be found in
201 1.2 pgoyette * a list and acquired must be removed from the list before
202 1.2 pgoyette * localcount_drain.
203 1.2 pgoyette */
204 1.2 pgoyette void
205 1.2 pgoyette localcount_acquire(struct localcount *lc)
206 1.2 pgoyette {
207 1.2 pgoyette
208 1.2 pgoyette KASSERT(lc->lc_totalp == NULL);
209 1.2 pgoyette localcount_adjust(lc, +1);
210 1.2 pgoyette }
211 1.2 pgoyette
212 1.2 pgoyette /*
213 1.2 pgoyette * localcount_release(lc, cv, interlock)
214 1.2 pgoyette *
215 1.2 pgoyette * Release a reference to lc. If there is a concurrent
216 1.2 pgoyette * localcount_drain and this may be the last reference, notify
217 1.2 pgoyette * localcount_drain by acquiring interlock, waking cv, and
218 1.2 pgoyette * releasing interlock. The cv and interlock passed here must be
219 1.2 pgoyette * the same as are passed to localcount_drain for this lc.
220 1.2 pgoyette *
221 1.2 pgoyette * Involves no interprocessor synchronization unless there is a
222 1.2 pgoyette * concurrent localcount_drain in progress.
223 1.2 pgoyette */
224 1.2 pgoyette void
225 1.2 pgoyette localcount_release(struct localcount *lc, kcondvar_t *cv, kmutex_t *interlock)
226 1.2 pgoyette {
227 1.2 pgoyette
228 1.2 pgoyette /*
229 1.2 pgoyette * Block xcall so that if someone begins draining after we see
230 1.2 pgoyette * lc->lc_totalp as null, then they won't start cv_wait until
231 1.2 pgoyette * after they have counted this CPU's contributions.
232 1.2 pgoyette *
233 1.2 pgoyette * Otherwise, localcount_drain may notice an extant reference
234 1.2 pgoyette * from this CPU and cv_wait for it, but having seen
235 1.2 pgoyette * lc->lc_totalp as null, this CPU will not wake
236 1.2 pgoyette * localcount_drain.
237 1.2 pgoyette */
238 1.2 pgoyette kpreempt_disable();
239 1.2 pgoyette
240 1.2 pgoyette KDASSERT(mutex_ownable(interlock));
241 1.2 pgoyette if (__predict_false(lc->lc_totalp != NULL)) {
242 1.2 pgoyette /*
243 1.2 pgoyette * Slow path -- wake localcount_drain in case this is
244 1.2 pgoyette * the last reference.
245 1.2 pgoyette */
246 1.2 pgoyette mutex_enter(interlock);
247 1.2 pgoyette if (--*lc->lc_totalp == 0)
248 1.2 pgoyette cv_broadcast(cv);
249 1.2 pgoyette mutex_exit(interlock);
250 1.2 pgoyette goto out;
251 1.2 pgoyette }
252 1.2 pgoyette
253 1.2 pgoyette localcount_adjust(lc, -1);
254 1.2 pgoyette out: kpreempt_enable();
255 1.2 pgoyette }
256