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