subr_percpu.c revision 1.23 1 1.23 riastrad /* $NetBSD: subr_percpu.c,v 1.23 2020/02/01 12:53:41 riastradh Exp $ */
2 1.1 yamt
3 1.1 yamt /*-
4 1.1 yamt * Copyright (c)2007,2008 YAMAMOTO Takashi,
5 1.1 yamt * All rights reserved.
6 1.1 yamt *
7 1.1 yamt * Redistribution and use in source and binary forms, with or without
8 1.1 yamt * modification, are permitted provided that the following conditions
9 1.1 yamt * are met:
10 1.1 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1 yamt * notice, this list of conditions and the following disclaimer.
12 1.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1 yamt * documentation and/or other materials provided with the distribution.
15 1.1 yamt *
16 1.1 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 yamt * SUCH DAMAGE.
27 1.1 yamt */
28 1.1 yamt
29 1.1 yamt /*
30 1.1 yamt * per-cpu storage.
31 1.1 yamt */
32 1.1 yamt
33 1.1 yamt #include <sys/cdefs.h>
34 1.23 riastrad __KERNEL_RCSID(0, "$NetBSD: subr_percpu.c,v 1.23 2020/02/01 12:53:41 riastradh Exp $");
35 1.1 yamt
36 1.1 yamt #include <sys/param.h>
37 1.1 yamt #include <sys/cpu.h>
38 1.23 riastrad #include <sys/kernel.h>
39 1.1 yamt #include <sys/kmem.h>
40 1.1 yamt #include <sys/mutex.h>
41 1.1 yamt #include <sys/percpu.h>
42 1.1 yamt #include <sys/rwlock.h>
43 1.1 yamt #include <sys/vmem.h>
44 1.1 yamt #include <sys/xcall.h>
45 1.1 yamt
46 1.1 yamt #define PERCPU_QUANTUM_SIZE (ALIGNBYTES + 1)
47 1.1 yamt #define PERCPU_QCACHE_MAX 0
48 1.1 yamt #define PERCPU_IMPORT_SIZE 2048
49 1.1 yamt
50 1.21 riastrad struct percpu {
51 1.21 riastrad unsigned pc_offset;
52 1.21 riastrad size_t pc_size;
53 1.21 riastrad percpu_callback_t pc_dtor;
54 1.21 riastrad void *pc_cookie;
55 1.21 riastrad };
56 1.8 yamt
57 1.13 rmind static krwlock_t percpu_swap_lock __cacheline_aligned;
58 1.22 riastrad static vmem_t * percpu_offset_arena __read_mostly;
59 1.22 riastrad static struct {
60 1.22 riastrad kmutex_t lock;
61 1.22 riastrad unsigned int nextoff;
62 1.22 riastrad } percpu_allocation __cacheline_aligned;
63 1.9 ad
64 1.1 yamt static percpu_cpu_t *
65 1.1 yamt cpu_percpu(struct cpu_info *ci)
66 1.1 yamt {
67 1.1 yamt
68 1.1 yamt return &ci->ci_data.cpu_percpu;
69 1.1 yamt }
70 1.1 yamt
71 1.1 yamt static unsigned int
72 1.1 yamt percpu_offset(percpu_t *pc)
73 1.1 yamt {
74 1.21 riastrad const unsigned int off = pc->pc_offset;
75 1.1 yamt
76 1.22 riastrad KASSERT(off < percpu_allocation.nextoff);
77 1.8 yamt return off;
78 1.1 yamt }
79 1.1 yamt
80 1.1 yamt /*
81 1.1 yamt * percpu_cpu_swap: crosscall handler for percpu_cpu_enlarge
82 1.1 yamt */
83 1.19 kamil __noubsan
84 1.1 yamt static void
85 1.1 yamt percpu_cpu_swap(void *p1, void *p2)
86 1.1 yamt {
87 1.1 yamt struct cpu_info * const ci = p1;
88 1.1 yamt percpu_cpu_t * const newpcc = p2;
89 1.1 yamt percpu_cpu_t * const pcc = cpu_percpu(ci);
90 1.1 yamt
91 1.12 martin KASSERT(ci == curcpu() || !mp_online);
92 1.11 matt
93 1.1 yamt /*
94 1.1 yamt * swap *pcc and *newpcc unless anyone has beaten us.
95 1.1 yamt */
96 1.1 yamt rw_enter(&percpu_swap_lock, RW_WRITER);
97 1.1 yamt if (newpcc->pcc_size > pcc->pcc_size) {
98 1.1 yamt percpu_cpu_t tmp;
99 1.1 yamt int s;
100 1.1 yamt
101 1.1 yamt tmp = *pcc;
102 1.1 yamt
103 1.1 yamt /*
104 1.1 yamt * block interrupts so that we don't lose their modifications.
105 1.1 yamt */
106 1.1 yamt
107 1.1 yamt s = splhigh();
108 1.1 yamt
109 1.1 yamt /*
110 1.1 yamt * copy data to new storage.
111 1.1 yamt */
112 1.1 yamt
113 1.1 yamt memcpy(newpcc->pcc_data, pcc->pcc_data, pcc->pcc_size);
114 1.1 yamt
115 1.1 yamt /*
116 1.1 yamt * this assignment needs to be atomic for percpu_getptr_remote.
117 1.1 yamt */
118 1.1 yamt
119 1.1 yamt pcc->pcc_data = newpcc->pcc_data;
120 1.1 yamt
121 1.1 yamt splx(s);
122 1.1 yamt
123 1.1 yamt pcc->pcc_size = newpcc->pcc_size;
124 1.1 yamt *newpcc = tmp;
125 1.1 yamt }
126 1.1 yamt rw_exit(&percpu_swap_lock);
127 1.1 yamt }
128 1.1 yamt
129 1.1 yamt /*
130 1.1 yamt * percpu_cpu_enlarge: ensure that percpu_cpu_t of each cpus have enough space
131 1.1 yamt */
132 1.1 yamt
133 1.1 yamt static void
134 1.1 yamt percpu_cpu_enlarge(size_t size)
135 1.1 yamt {
136 1.1 yamt CPU_INFO_ITERATOR cii;
137 1.1 yamt struct cpu_info *ci;
138 1.1 yamt
139 1.1 yamt for (CPU_INFO_FOREACH(cii, ci)) {
140 1.1 yamt percpu_cpu_t pcc;
141 1.1 yamt
142 1.1 yamt pcc.pcc_data = kmem_alloc(size, KM_SLEEP); /* XXX cacheline */
143 1.1 yamt pcc.pcc_size = size;
144 1.1 yamt if (!mp_online) {
145 1.1 yamt percpu_cpu_swap(ci, &pcc);
146 1.1 yamt } else {
147 1.1 yamt uint64_t where;
148 1.1 yamt
149 1.1 yamt where = xc_unicast(0, percpu_cpu_swap, ci, &pcc, ci);
150 1.1 yamt xc_wait(where);
151 1.1 yamt }
152 1.20 riastrad KASSERT(pcc.pcc_size <= size);
153 1.1 yamt if (pcc.pcc_data != NULL) {
154 1.1 yamt kmem_free(pcc.pcc_data, pcc.pcc_size);
155 1.1 yamt }
156 1.1 yamt }
157 1.1 yamt }
158 1.1 yamt
159 1.1 yamt /*
160 1.1 yamt * percpu_backend_alloc: vmem import callback for percpu_offset_arena
161 1.1 yamt */
162 1.1 yamt
163 1.15 dyoung static int
164 1.16 para percpu_backend_alloc(vmem_t *dummy, vmem_size_t size, vmem_size_t *resultsize,
165 1.15 dyoung vm_flag_t vmflags, vmem_addr_t *addrp)
166 1.1 yamt {
167 1.1 yamt unsigned int offset;
168 1.1 yamt unsigned int nextoff;
169 1.1 yamt
170 1.3 yamt ASSERT_SLEEPABLE();
171 1.1 yamt KASSERT(dummy == NULL);
172 1.1 yamt
173 1.1 yamt if ((vmflags & VM_NOSLEEP) != 0)
174 1.15 dyoung return ENOMEM;
175 1.1 yamt
176 1.1 yamt size = roundup(size, PERCPU_IMPORT_SIZE);
177 1.22 riastrad mutex_enter(&percpu_allocation.lock);
178 1.22 riastrad offset = percpu_allocation.nextoff;
179 1.22 riastrad percpu_allocation.nextoff = nextoff = percpu_allocation.nextoff + size;
180 1.22 riastrad mutex_exit(&percpu_allocation.lock);
181 1.1 yamt
182 1.1 yamt percpu_cpu_enlarge(nextoff);
183 1.1 yamt
184 1.1 yamt *resultsize = size;
185 1.15 dyoung *addrp = (vmem_addr_t)offset;
186 1.15 dyoung return 0;
187 1.1 yamt }
188 1.1 yamt
189 1.2 yamt static void
190 1.2 yamt percpu_zero_cb(void *vp, void *vp2, struct cpu_info *ci)
191 1.2 yamt {
192 1.2 yamt size_t sz = (uintptr_t)vp2;
193 1.2 yamt
194 1.2 yamt memset(vp, 0, sz);
195 1.2 yamt }
196 1.2 yamt
197 1.2 yamt /*
198 1.2 yamt * percpu_zero: initialize percpu storage with zero.
199 1.2 yamt */
200 1.2 yamt
201 1.2 yamt static void
202 1.2 yamt percpu_zero(percpu_t *pc, size_t sz)
203 1.2 yamt {
204 1.2 yamt
205 1.2 yamt percpu_foreach(pc, percpu_zero_cb, (void *)(uintptr_t)sz);
206 1.2 yamt }
207 1.2 yamt
208 1.1 yamt /*
209 1.1 yamt * percpu_init: subsystem initialization
210 1.1 yamt */
211 1.1 yamt
212 1.1 yamt void
213 1.1 yamt percpu_init(void)
214 1.1 yamt {
215 1.1 yamt
216 1.3 yamt ASSERT_SLEEPABLE();
217 1.1 yamt rw_init(&percpu_swap_lock);
218 1.22 riastrad mutex_init(&percpu_allocation.lock, MUTEX_DEFAULT, IPL_NONE);
219 1.22 riastrad percpu_allocation.nextoff = PERCPU_QUANTUM_SIZE;
220 1.1 yamt
221 1.16 para percpu_offset_arena = vmem_xcreate("percpu", 0, 0, PERCPU_QUANTUM_SIZE,
222 1.1 yamt percpu_backend_alloc, NULL, NULL, PERCPU_QCACHE_MAX, VM_SLEEP,
223 1.1 yamt IPL_NONE);
224 1.1 yamt }
225 1.1 yamt
226 1.1 yamt /*
227 1.1 yamt * percpu_init_cpu: cpu initialization
228 1.1 yamt *
229 1.1 yamt * => should be called before the cpu appears on the list for CPU_INFO_FOREACH.
230 1.1 yamt */
231 1.1 yamt
232 1.1 yamt void
233 1.1 yamt percpu_init_cpu(struct cpu_info *ci)
234 1.1 yamt {
235 1.1 yamt percpu_cpu_t * const pcc = cpu_percpu(ci);
236 1.22 riastrad size_t size = percpu_allocation.nextoff; /* XXX racy */
237 1.1 yamt
238 1.3 yamt ASSERT_SLEEPABLE();
239 1.1 yamt pcc->pcc_size = size;
240 1.1 yamt if (size) {
241 1.1 yamt pcc->pcc_data = kmem_zalloc(pcc->pcc_size, KM_SLEEP);
242 1.1 yamt }
243 1.1 yamt }
244 1.1 yamt
245 1.1 yamt /*
246 1.1 yamt * percpu_alloc: allocate percpu storage
247 1.1 yamt *
248 1.1 yamt * => called in thread context.
249 1.1 yamt * => considered as an expensive and rare operation.
250 1.2 yamt * => allocated storage is initialized with zeros.
251 1.1 yamt */
252 1.1 yamt
253 1.1 yamt percpu_t *
254 1.1 yamt percpu_alloc(size_t size)
255 1.1 yamt {
256 1.21 riastrad
257 1.21 riastrad return percpu_create(size, NULL, NULL, NULL);
258 1.21 riastrad }
259 1.21 riastrad
260 1.21 riastrad /*
261 1.21 riastrad * percpu_create: allocate percpu storage and associate ctor/dtor with it
262 1.21 riastrad *
263 1.21 riastrad * => called in thread context.
264 1.21 riastrad * => considered as an expensive and rare operation.
265 1.21 riastrad * => allocated storage is initialized by ctor, or zeros if ctor is null
266 1.21 riastrad * => percpu_free will call dtor first, if dtor is nonnull
267 1.21 riastrad * => ctor or dtor may sleep, even on allocation
268 1.21 riastrad */
269 1.21 riastrad
270 1.21 riastrad percpu_t *
271 1.21 riastrad percpu_create(size_t size, percpu_callback_t ctor, percpu_callback_t dtor,
272 1.21 riastrad void *cookie)
273 1.21 riastrad {
274 1.15 dyoung vmem_addr_t offset;
275 1.1 yamt percpu_t *pc;
276 1.1 yamt
277 1.3 yamt ASSERT_SLEEPABLE();
278 1.18 chs (void)vmem_alloc(percpu_offset_arena, size, VM_SLEEP | VM_BESTFIT,
279 1.18 chs &offset);
280 1.21 riastrad
281 1.21 riastrad pc = kmem_alloc(sizeof(*pc), KM_SLEEP);
282 1.21 riastrad pc->pc_offset = offset;
283 1.21 riastrad pc->pc_size = size;
284 1.21 riastrad pc->pc_dtor = dtor;
285 1.21 riastrad pc->pc_cookie = cookie;
286 1.21 riastrad
287 1.21 riastrad if (ctor) {
288 1.21 riastrad CPU_INFO_ITERATOR cii;
289 1.21 riastrad struct cpu_info *ci;
290 1.21 riastrad void *buf;
291 1.21 riastrad
292 1.21 riastrad buf = kmem_alloc(size, KM_SLEEP);
293 1.21 riastrad for (CPU_INFO_FOREACH(cii, ci)) {
294 1.21 riastrad memset(buf, 0, size);
295 1.21 riastrad (*ctor)(buf, cookie, ci);
296 1.21 riastrad percpu_traverse_enter();
297 1.21 riastrad memcpy(percpu_getptr_remote(pc, ci), buf, size);
298 1.21 riastrad percpu_traverse_exit();
299 1.21 riastrad }
300 1.21 riastrad explicit_memset(buf, 0, size);
301 1.21 riastrad kmem_free(buf, size);
302 1.21 riastrad } else {
303 1.21 riastrad percpu_zero(pc, size);
304 1.21 riastrad }
305 1.21 riastrad
306 1.1 yamt return pc;
307 1.1 yamt }
308 1.1 yamt
309 1.1 yamt /*
310 1.5 yamt * percpu_free: free percpu storage
311 1.1 yamt *
312 1.1 yamt * => called in thread context.
313 1.1 yamt * => considered as an expensive and rare operation.
314 1.1 yamt */
315 1.1 yamt
316 1.1 yamt void
317 1.1 yamt percpu_free(percpu_t *pc, size_t size)
318 1.1 yamt {
319 1.1 yamt
320 1.3 yamt ASSERT_SLEEPABLE();
321 1.21 riastrad KASSERT(size == pc->pc_size);
322 1.21 riastrad
323 1.21 riastrad if (pc->pc_dtor) {
324 1.21 riastrad CPU_INFO_ITERATOR cii;
325 1.21 riastrad struct cpu_info *ci;
326 1.21 riastrad void *buf;
327 1.21 riastrad
328 1.21 riastrad buf = kmem_alloc(size, KM_SLEEP);
329 1.21 riastrad for (CPU_INFO_FOREACH(cii, ci)) {
330 1.21 riastrad percpu_traverse_enter();
331 1.21 riastrad memcpy(buf, percpu_getptr_remote(pc, ci), size);
332 1.21 riastrad explicit_memset(percpu_getptr_remote(pc, ci), 0, size);
333 1.21 riastrad percpu_traverse_exit();
334 1.21 riastrad (*pc->pc_dtor)(buf, pc->pc_cookie, ci);
335 1.21 riastrad }
336 1.21 riastrad explicit_memset(buf, 0, size);
337 1.21 riastrad kmem_free(buf, size);
338 1.21 riastrad }
339 1.21 riastrad
340 1.1 yamt vmem_free(percpu_offset_arena, (vmem_addr_t)percpu_offset(pc), size);
341 1.21 riastrad kmem_free(pc, sizeof(*pc));
342 1.1 yamt }
343 1.1 yamt
344 1.1 yamt /*
345 1.4 thorpej * percpu_getref:
346 1.1 yamt *
347 1.1 yamt * => safe to be used in either thread or interrupt context
348 1.4 thorpej * => disables preemption; must be bracketed with a percpu_putref()
349 1.1 yamt */
350 1.1 yamt
351 1.1 yamt void *
352 1.4 thorpej percpu_getref(percpu_t *pc)
353 1.1 yamt {
354 1.1 yamt
355 1.17 uebayasi kpreempt_disable();
356 1.1 yamt return percpu_getptr_remote(pc, curcpu());
357 1.1 yamt }
358 1.1 yamt
359 1.1 yamt /*
360 1.4 thorpej * percpu_putref:
361 1.4 thorpej *
362 1.4 thorpej * => drops the preemption-disabled count after caller is done with per-cpu
363 1.4 thorpej * data
364 1.4 thorpej */
365 1.4 thorpej
366 1.4 thorpej void
367 1.4 thorpej percpu_putref(percpu_t *pc)
368 1.4 thorpej {
369 1.4 thorpej
370 1.17 uebayasi kpreempt_enable();
371 1.4 thorpej }
372 1.4 thorpej
373 1.4 thorpej /*
374 1.1 yamt * percpu_traverse_enter, percpu_traverse_exit, percpu_getptr_remote:
375 1.1 yamt * helpers to access remote cpu's percpu data.
376 1.1 yamt *
377 1.1 yamt * => called in thread context.
378 1.2 yamt * => percpu_traverse_enter can block low-priority xcalls.
379 1.1 yamt * => typical usage would be:
380 1.1 yamt *
381 1.1 yamt * sum = 0;
382 1.1 yamt * percpu_traverse_enter();
383 1.1 yamt * for (CPU_INFO_FOREACH(cii, ci)) {
384 1.1 yamt * unsigned int *p = percpu_getptr_remote(pc, ci);
385 1.1 yamt * sum += *p;
386 1.1 yamt * }
387 1.1 yamt * percpu_traverse_exit();
388 1.1 yamt */
389 1.1 yamt
390 1.1 yamt void
391 1.1 yamt percpu_traverse_enter(void)
392 1.1 yamt {
393 1.1 yamt
394 1.3 yamt ASSERT_SLEEPABLE();
395 1.1 yamt rw_enter(&percpu_swap_lock, RW_READER);
396 1.1 yamt }
397 1.1 yamt
398 1.1 yamt void
399 1.1 yamt percpu_traverse_exit(void)
400 1.1 yamt {
401 1.1 yamt
402 1.1 yamt rw_exit(&percpu_swap_lock);
403 1.1 yamt }
404 1.1 yamt
405 1.1 yamt void *
406 1.1 yamt percpu_getptr_remote(percpu_t *pc, struct cpu_info *ci)
407 1.1 yamt {
408 1.1 yamt
409 1.1 yamt return &((char *)cpu_percpu(ci)->pcc_data)[percpu_offset(pc)];
410 1.1 yamt }
411 1.1 yamt
412 1.1 yamt /*
413 1.1 yamt * percpu_foreach: call the specified callback function for each cpus.
414 1.1 yamt *
415 1.2 yamt * => called in thread context.
416 1.1 yamt * => caller should not rely on the cpu iteration order.
417 1.2 yamt * => the callback function should be minimum because it is executed with
418 1.2 yamt * holding a global lock, which can block low-priority xcalls.
419 1.2 yamt * eg. it's illegal for a callback function to sleep for memory allocation.
420 1.1 yamt */
421 1.1 yamt void
422 1.1 yamt percpu_foreach(percpu_t *pc, percpu_callback_t cb, void *arg)
423 1.1 yamt {
424 1.1 yamt CPU_INFO_ITERATOR cii;
425 1.1 yamt struct cpu_info *ci;
426 1.1 yamt
427 1.1 yamt percpu_traverse_enter();
428 1.1 yamt for (CPU_INFO_FOREACH(cii, ci)) {
429 1.2 yamt (*cb)(percpu_getptr_remote(pc, ci), arg, ci);
430 1.1 yamt }
431 1.1 yamt percpu_traverse_exit();
432 1.1 yamt }
433