subr_percpu.c revision 1.1 1 1.1 yamt /* $NetBSD: subr_percpu.c,v 1.1 2008/01/14 12:40:03 yamt 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.1 yamt __KERNEL_RCSID(0, "$NetBSD: subr_percpu.c,v 1.1 2008/01/14 12:40:03 yamt Exp $");
35 1.1 yamt
36 1.1 yamt #include <sys/param.h>
37 1.1 yamt #include <sys/cpu.h>
38 1.1 yamt #include <sys/kmem.h>
39 1.1 yamt #include <sys/kernel.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 #include <uvm/uvm_extern.h>
47 1.1 yamt
48 1.1 yamt static krwlock_t percpu_swap_lock;
49 1.1 yamt static kmutex_t percpu_allocation_lock;
50 1.1 yamt static vmem_t *percpu_offset_arena;
51 1.1 yamt static unsigned int percpu_nextoff;
52 1.1 yamt
53 1.1 yamt #define PERCPU_QUANTUM_SIZE (ALIGNBYTES + 1)
54 1.1 yamt #define PERCPU_QCACHE_MAX 0
55 1.1 yamt #define PERCPU_IMPORT_SIZE 2048
56 1.1 yamt
57 1.1 yamt static percpu_cpu_t *
58 1.1 yamt cpu_percpu(struct cpu_info *ci)
59 1.1 yamt {
60 1.1 yamt
61 1.1 yamt return &ci->ci_data.cpu_percpu;
62 1.1 yamt }
63 1.1 yamt
64 1.1 yamt static unsigned int
65 1.1 yamt percpu_offset(percpu_t *pc)
66 1.1 yamt {
67 1.1 yamt
68 1.1 yamt return (uintptr_t)pc;
69 1.1 yamt }
70 1.1 yamt
71 1.1 yamt /*
72 1.1 yamt * percpu_cpu_swap: crosscall handler for percpu_cpu_enlarge
73 1.1 yamt */
74 1.1 yamt
75 1.1 yamt static void
76 1.1 yamt percpu_cpu_swap(void *p1, void *p2)
77 1.1 yamt {
78 1.1 yamt struct cpu_info * const ci = p1;
79 1.1 yamt percpu_cpu_t * const newpcc = p2;
80 1.1 yamt percpu_cpu_t * const pcc = cpu_percpu(ci);
81 1.1 yamt
82 1.1 yamt /*
83 1.1 yamt * swap *pcc and *newpcc unless anyone has beaten us.
84 1.1 yamt */
85 1.1 yamt
86 1.1 yamt rw_enter(&percpu_swap_lock, RW_WRITER);
87 1.1 yamt if (newpcc->pcc_size > pcc->pcc_size) {
88 1.1 yamt percpu_cpu_t tmp;
89 1.1 yamt int s;
90 1.1 yamt
91 1.1 yamt tmp = *pcc;
92 1.1 yamt
93 1.1 yamt /*
94 1.1 yamt * block interrupts so that we don't lose their modifications.
95 1.1 yamt */
96 1.1 yamt
97 1.1 yamt s = splhigh();
98 1.1 yamt
99 1.1 yamt /*
100 1.1 yamt * copy data to new storage.
101 1.1 yamt */
102 1.1 yamt
103 1.1 yamt memcpy(newpcc->pcc_data, pcc->pcc_data, pcc->pcc_size);
104 1.1 yamt
105 1.1 yamt /*
106 1.1 yamt * this assignment needs to be atomic for percpu_getptr_remote.
107 1.1 yamt */
108 1.1 yamt
109 1.1 yamt pcc->pcc_data = newpcc->pcc_data;
110 1.1 yamt
111 1.1 yamt splx(s);
112 1.1 yamt
113 1.1 yamt pcc->pcc_size = newpcc->pcc_size;
114 1.1 yamt *newpcc = tmp;
115 1.1 yamt }
116 1.1 yamt rw_exit(&percpu_swap_lock);
117 1.1 yamt }
118 1.1 yamt
119 1.1 yamt /*
120 1.1 yamt * percpu_cpu_enlarge: ensure that percpu_cpu_t of each cpus have enough space
121 1.1 yamt */
122 1.1 yamt
123 1.1 yamt static void
124 1.1 yamt percpu_cpu_enlarge(size_t size)
125 1.1 yamt {
126 1.1 yamt CPU_INFO_ITERATOR cii;
127 1.1 yamt struct cpu_info *ci;
128 1.1 yamt
129 1.1 yamt for (CPU_INFO_FOREACH(cii, ci)) {
130 1.1 yamt percpu_cpu_t pcc;
131 1.1 yamt
132 1.1 yamt pcc.pcc_data = kmem_alloc(size, KM_SLEEP); /* XXX cacheline */
133 1.1 yamt pcc.pcc_size = size;
134 1.1 yamt if (!mp_online) {
135 1.1 yamt percpu_cpu_swap(ci, &pcc);
136 1.1 yamt } else {
137 1.1 yamt uint64_t where;
138 1.1 yamt
139 1.1 yamt uvm_lwp_hold(curlwp); /* don't swap out pcc */
140 1.1 yamt where = xc_unicast(0, percpu_cpu_swap, ci, &pcc, ci);
141 1.1 yamt xc_wait(where);
142 1.1 yamt uvm_lwp_rele(curlwp);
143 1.1 yamt }
144 1.1 yamt KASSERT(pcc.pcc_size < size);
145 1.1 yamt if (pcc.pcc_data != NULL) {
146 1.1 yamt kmem_free(pcc.pcc_data, pcc.pcc_size);
147 1.1 yamt }
148 1.1 yamt }
149 1.1 yamt }
150 1.1 yamt
151 1.1 yamt /*
152 1.1 yamt * percpu_backend_alloc: vmem import callback for percpu_offset_arena
153 1.1 yamt */
154 1.1 yamt
155 1.1 yamt static vmem_addr_t
156 1.1 yamt percpu_backend_alloc(vmem_t *dummy, vmem_size_t size, vmem_size_t *resultsize,
157 1.1 yamt vm_flag_t vmflags)
158 1.1 yamt {
159 1.1 yamt unsigned int offset;
160 1.1 yamt unsigned int nextoff;
161 1.1 yamt
162 1.1 yamt ASSERT_SLEEPABLE(NULL, __func__);
163 1.1 yamt KASSERT(dummy == NULL);
164 1.1 yamt
165 1.1 yamt if ((vmflags & VM_NOSLEEP) != 0)
166 1.1 yamt return VMEM_ADDR_NULL;
167 1.1 yamt
168 1.1 yamt size = roundup(size, PERCPU_IMPORT_SIZE);
169 1.1 yamt mutex_enter(&percpu_allocation_lock);
170 1.1 yamt offset = percpu_nextoff;
171 1.1 yamt percpu_nextoff = nextoff = percpu_nextoff + size;
172 1.1 yamt mutex_exit(&percpu_allocation_lock);
173 1.1 yamt
174 1.1 yamt percpu_cpu_enlarge(nextoff);
175 1.1 yamt
176 1.1 yamt *resultsize = size;
177 1.1 yamt return (vmem_addr_t)offset;
178 1.1 yamt }
179 1.1 yamt
180 1.1 yamt /*
181 1.1 yamt * percpu_init: subsystem initialization
182 1.1 yamt */
183 1.1 yamt
184 1.1 yamt void
185 1.1 yamt percpu_init(void)
186 1.1 yamt {
187 1.1 yamt
188 1.1 yamt ASSERT_SLEEPABLE(NULL, __func__);
189 1.1 yamt rw_init(&percpu_swap_lock);
190 1.1 yamt mutex_init(&percpu_allocation_lock, MUTEX_DEFAULT, IPL_NONE);
191 1.1 yamt
192 1.1 yamt percpu_offset_arena = vmem_create("percpu", 0, 0, PERCPU_QUANTUM_SIZE,
193 1.1 yamt percpu_backend_alloc, NULL, NULL, PERCPU_QCACHE_MAX, VM_SLEEP,
194 1.1 yamt IPL_NONE);
195 1.1 yamt }
196 1.1 yamt
197 1.1 yamt /*
198 1.1 yamt * percpu_init_cpu: cpu initialization
199 1.1 yamt *
200 1.1 yamt * => should be called before the cpu appears on the list for CPU_INFO_FOREACH.
201 1.1 yamt */
202 1.1 yamt
203 1.1 yamt void
204 1.1 yamt percpu_init_cpu(struct cpu_info *ci)
205 1.1 yamt {
206 1.1 yamt percpu_cpu_t * const pcc = cpu_percpu(ci);
207 1.1 yamt size_t size = percpu_nextoff; /* XXX racy */
208 1.1 yamt
209 1.1 yamt ASSERT_SLEEPABLE(NULL, __func__);
210 1.1 yamt pcc->pcc_size = size;
211 1.1 yamt if (size) {
212 1.1 yamt pcc->pcc_data = kmem_zalloc(pcc->pcc_size, KM_SLEEP);
213 1.1 yamt }
214 1.1 yamt }
215 1.1 yamt
216 1.1 yamt /*
217 1.1 yamt * percpu_alloc: allocate percpu storage
218 1.1 yamt *
219 1.1 yamt * => called in thread context.
220 1.1 yamt * => considered as an expensive and rare operation.
221 1.1 yamt */
222 1.1 yamt
223 1.1 yamt percpu_t *
224 1.1 yamt percpu_alloc(size_t size)
225 1.1 yamt {
226 1.1 yamt unsigned int offset;
227 1.1 yamt percpu_t *pc;
228 1.1 yamt
229 1.1 yamt ASSERT_SLEEPABLE(NULL, __func__);
230 1.1 yamt offset = vmem_alloc(percpu_offset_arena, size, VM_SLEEP | VM_BESTFIT);
231 1.1 yamt pc = (percpu_t *)(uintptr_t)offset;
232 1.1 yamt percpu_zero(pc, size);
233 1.1 yamt return pc;
234 1.1 yamt }
235 1.1 yamt
236 1.1 yamt /*
237 1.1 yamt * percpu_alloc: free percpu storage
238 1.1 yamt *
239 1.1 yamt * => called in thread context.
240 1.1 yamt * => considered as an expensive and rare operation.
241 1.1 yamt */
242 1.1 yamt
243 1.1 yamt void
244 1.1 yamt percpu_free(percpu_t *pc, size_t size)
245 1.1 yamt {
246 1.1 yamt
247 1.1 yamt ASSERT_SLEEPABLE(NULL, __func__);
248 1.1 yamt vmem_free(percpu_offset_arena, (vmem_addr_t)percpu_offset(pc), size);
249 1.1 yamt }
250 1.1 yamt
251 1.1 yamt /*
252 1.1 yamt * percpu_getptr:
253 1.1 yamt *
254 1.1 yamt * => called with preemption disabled
255 1.1 yamt * => safe to be used in either thread or interrupt context
256 1.1 yamt */
257 1.1 yamt
258 1.1 yamt void *
259 1.1 yamt percpu_getptr(percpu_t *pc)
260 1.1 yamt {
261 1.1 yamt
262 1.1 yamt return percpu_getptr_remote(pc, curcpu());
263 1.1 yamt }
264 1.1 yamt
265 1.1 yamt /*
266 1.1 yamt * percpu_traverse_enter, percpu_traverse_exit, percpu_getptr_remote:
267 1.1 yamt * helpers to access remote cpu's percpu data.
268 1.1 yamt *
269 1.1 yamt * => called in thread context.
270 1.1 yamt * => typical usage would be:
271 1.1 yamt *
272 1.1 yamt * sum = 0;
273 1.1 yamt * percpu_traverse_enter();
274 1.1 yamt * for (CPU_INFO_FOREACH(cii, ci)) {
275 1.1 yamt * unsigned int *p = percpu_getptr_remote(pc, ci);
276 1.1 yamt * sum += *p;
277 1.1 yamt * }
278 1.1 yamt * percpu_traverse_exit();
279 1.1 yamt */
280 1.1 yamt
281 1.1 yamt void
282 1.1 yamt percpu_traverse_enter(void)
283 1.1 yamt {
284 1.1 yamt
285 1.1 yamt ASSERT_SLEEPABLE(NULL, __func__);
286 1.1 yamt rw_enter(&percpu_swap_lock, RW_READER);
287 1.1 yamt }
288 1.1 yamt
289 1.1 yamt void
290 1.1 yamt percpu_traverse_exit(void)
291 1.1 yamt {
292 1.1 yamt
293 1.1 yamt rw_exit(&percpu_swap_lock);
294 1.1 yamt }
295 1.1 yamt
296 1.1 yamt void *
297 1.1 yamt percpu_getptr_remote(percpu_t *pc, struct cpu_info *ci)
298 1.1 yamt {
299 1.1 yamt
300 1.1 yamt return &((char *)cpu_percpu(ci)->pcc_data)[percpu_offset(pc)];
301 1.1 yamt }
302 1.1 yamt
303 1.1 yamt static void
304 1.1 yamt percpu_zero_cb(void *vp, void *vp2)
305 1.1 yamt {
306 1.1 yamt size_t sz = (uintptr_t)vp2;
307 1.1 yamt
308 1.1 yamt memset(vp, 0, sz);
309 1.1 yamt }
310 1.1 yamt
311 1.1 yamt /*
312 1.1 yamt * percpu_zero: initialize percpu storage with zero.
313 1.1 yamt */
314 1.1 yamt void
315 1.1 yamt percpu_zero(percpu_t *pc, size_t sz)
316 1.1 yamt {
317 1.1 yamt
318 1.1 yamt percpu_foreach(pc, percpu_zero_cb, (void *)(uintptr_t)sz);
319 1.1 yamt }
320 1.1 yamt
321 1.1 yamt /*
322 1.1 yamt * percpu_foreach: call the specified callback function for each cpus.
323 1.1 yamt *
324 1.1 yamt * => caller should not rely on the cpu iteration order.
325 1.1 yamt */
326 1.1 yamt void
327 1.1 yamt percpu_foreach(percpu_t *pc, percpu_callback_t cb, void *arg)
328 1.1 yamt {
329 1.1 yamt CPU_INFO_ITERATOR cii;
330 1.1 yamt struct cpu_info *ci;
331 1.1 yamt
332 1.1 yamt percpu_traverse_enter();
333 1.1 yamt for (CPU_INFO_FOREACH(cii, ci)) {
334 1.1 yamt void *p = percpu_getptr_remote(pc, ci);
335 1.1 yamt (*cb)(p, arg);
336 1.1 yamt }
337 1.1 yamt percpu_traverse_exit();
338 1.1 yamt }
339