subr_kmem.c revision 1.66 1 1.66 christos /* $NetBSD: subr_kmem.c,v 1.66 2018/01/09 01:53:55 christos Exp $ */
2 1.1 yamt
3 1.1 yamt /*-
4 1.61 maxv * Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
5 1.23 ad * All rights reserved.
6 1.23 ad *
7 1.23 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.61 maxv * by Andrew Doran and Maxime Villard.
9 1.23 ad *
10 1.23 ad * Redistribution and use in source and binary forms, with or without
11 1.23 ad * modification, are permitted provided that the following conditions
12 1.23 ad * are met:
13 1.23 ad * 1. Redistributions of source code must retain the above copyright
14 1.23 ad * notice, this list of conditions and the following disclaimer.
15 1.23 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.23 ad * notice, this list of conditions and the following disclaimer in the
17 1.23 ad * documentation and/or other materials provided with the distribution.
18 1.23 ad *
19 1.23 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.23 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.23 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.23 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.23 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.23 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.23 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.23 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.23 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.23 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.23 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.23 ad */
31 1.23 ad
32 1.23 ad /*-
33 1.1 yamt * Copyright (c)2006 YAMAMOTO Takashi,
34 1.1 yamt * All rights reserved.
35 1.1 yamt *
36 1.1 yamt * Redistribution and use in source and binary forms, with or without
37 1.1 yamt * modification, are permitted provided that the following conditions
38 1.1 yamt * are met:
39 1.1 yamt * 1. Redistributions of source code must retain the above copyright
40 1.1 yamt * notice, this list of conditions and the following disclaimer.
41 1.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 yamt * notice, this list of conditions and the following disclaimer in the
43 1.1 yamt * documentation and/or other materials provided with the distribution.
44 1.1 yamt *
45 1.1 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 1.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 1.1 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 1.1 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 1.1 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 1.1 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 1.1 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.1 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 1.1 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 1.1 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 1.1 yamt * SUCH DAMAGE.
56 1.1 yamt */
57 1.1 yamt
58 1.1 yamt /*
59 1.55 maxv * Allocator of kernel wired memory. This allocator has some debug features
60 1.55 maxv * enabled with "option DIAGNOSTIC" and "option DEBUG".
61 1.50 yamt */
62 1.50 yamt
63 1.50 yamt /*
64 1.55 maxv * KMEM_SIZE: detect alloc/free size mismatch bugs.
65 1.57 maxv * Prefix each allocations with a fixed-sized, aligned header and record
66 1.57 maxv * the exact user-requested allocation size in it. When freeing, compare
67 1.57 maxv * it with kmem_free's "size" argument.
68 1.60 maxv *
69 1.55 maxv * KMEM_REDZONE: detect overrun bugs.
70 1.57 maxv * Add a 2-byte pattern (allocate one more memory chunk if needed) at the
71 1.57 maxv * end of each allocated buffer. Check this pattern on kmem_free.
72 1.50 yamt *
73 1.60 maxv * These options are enabled on DIAGNOSTIC.
74 1.60 maxv *
75 1.60 maxv * |CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|
76 1.60 maxv * +-----+-----+-----+-----+-----+-----+-----+-----+-----+---+-+--+--+
77 1.60 maxv * |/////| | | | | | | | | |*|**|UU|
78 1.60 maxv * |/HSZ/| | | | | | | | | |*|**|UU|
79 1.60 maxv * |/////| | | | | | | | | |*|**|UU|
80 1.60 maxv * +-----+-----+-----+-----+-----+-----+-----+-----+-----+---+-+--+--+
81 1.60 maxv * |Size | Buffer usable by the caller (requested size) |RedZ|Unused\
82 1.60 maxv */
83 1.60 maxv
84 1.60 maxv /*
85 1.55 maxv * KMEM_POISON: detect modify-after-free bugs.
86 1.50 yamt * Fill freed (in the sense of kmem_free) memory with a garbage pattern.
87 1.50 yamt * Check the pattern on allocation.
88 1.50 yamt *
89 1.50 yamt * KMEM_GUARD
90 1.61 maxv * A kernel with "option DEBUG" has "kmem_guard" debugging feature compiled
91 1.61 maxv * in. See the comment below for what kind of bugs it tries to detect. Even
92 1.61 maxv * if compiled in, it's disabled by default because it's very expensive.
93 1.61 maxv * You can enable it on boot by:
94 1.55 maxv * boot -d
95 1.55 maxv * db> w kmem_guard_depth 0t30000
96 1.55 maxv * db> c
97 1.1 yamt *
98 1.55 maxv * The default value of kmem_guard_depth is 0, which means disabled.
99 1.55 maxv * It can be changed by KMEM_GUARD_DEPTH kernel config option.
100 1.1 yamt */
101 1.1 yamt
102 1.1 yamt #include <sys/cdefs.h>
103 1.66 christos __KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.66 2018/01/09 01:53:55 christos Exp $");
104 1.63 christos
105 1.63 christos #ifdef _KERNEL_OPT
106 1.63 christos #include "opt_kmem.h"
107 1.63 christos #endif
108 1.1 yamt
109 1.1 yamt #include <sys/param.h>
110 1.6 yamt #include <sys/callback.h>
111 1.1 yamt #include <sys/kmem.h>
112 1.39 para #include <sys/pool.h>
113 1.13 ad #include <sys/debug.h>
114 1.17 ad #include <sys/lockdebug.h>
115 1.23 ad #include <sys/cpu.h>
116 1.1 yamt
117 1.6 yamt #include <uvm/uvm_extern.h>
118 1.6 yamt #include <uvm/uvm_map.h>
119 1.6 yamt
120 1.1 yamt #include <lib/libkern/libkern.h>
121 1.1 yamt
122 1.46 para struct kmem_cache_info {
123 1.40 rmind size_t kc_size;
124 1.40 rmind const char * kc_name;
125 1.46 para };
126 1.46 para
127 1.46 para static const struct kmem_cache_info kmem_cache_sizes[] = {
128 1.39 para { 8, "kmem-8" },
129 1.39 para { 16, "kmem-16" },
130 1.39 para { 24, "kmem-24" },
131 1.39 para { 32, "kmem-32" },
132 1.39 para { 40, "kmem-40" },
133 1.39 para { 48, "kmem-48" },
134 1.39 para { 56, "kmem-56" },
135 1.39 para { 64, "kmem-64" },
136 1.39 para { 80, "kmem-80" },
137 1.39 para { 96, "kmem-96" },
138 1.39 para { 112, "kmem-112" },
139 1.39 para { 128, "kmem-128" },
140 1.39 para { 160, "kmem-160" },
141 1.39 para { 192, "kmem-192" },
142 1.39 para { 224, "kmem-224" },
143 1.39 para { 256, "kmem-256" },
144 1.39 para { 320, "kmem-320" },
145 1.39 para { 384, "kmem-384" },
146 1.39 para { 448, "kmem-448" },
147 1.39 para { 512, "kmem-512" },
148 1.39 para { 768, "kmem-768" },
149 1.39 para { 1024, "kmem-1024" },
150 1.46 para { 0, NULL }
151 1.46 para };
152 1.46 para
153 1.46 para static const struct kmem_cache_info kmem_cache_big_sizes[] = {
154 1.39 para { 2048, "kmem-2048" },
155 1.39 para { 4096, "kmem-4096" },
156 1.46 para { 8192, "kmem-8192" },
157 1.46 para { 16384, "kmem-16384" },
158 1.39 para { 0, NULL }
159 1.39 para };
160 1.1 yamt
161 1.39 para /*
162 1.40 rmind * KMEM_ALIGN is the smallest guaranteed alignment and also the
163 1.46 para * smallest allocateable quantum.
164 1.46 para * Every cache size >= CACHE_LINE_SIZE gets CACHE_LINE_SIZE alignment.
165 1.39 para */
166 1.40 rmind #define KMEM_ALIGN 8
167 1.40 rmind #define KMEM_SHIFT 3
168 1.46 para #define KMEM_MAXSIZE 1024
169 1.40 rmind #define KMEM_CACHE_COUNT (KMEM_MAXSIZE >> KMEM_SHIFT)
170 1.1 yamt
171 1.40 rmind static pool_cache_t kmem_cache[KMEM_CACHE_COUNT] __cacheline_aligned;
172 1.40 rmind static size_t kmem_cache_maxidx __read_mostly;
173 1.23 ad
174 1.46 para #define KMEM_BIG_ALIGN 2048
175 1.46 para #define KMEM_BIG_SHIFT 11
176 1.46 para #define KMEM_BIG_MAXSIZE 16384
177 1.46 para #define KMEM_CACHE_BIG_COUNT (KMEM_BIG_MAXSIZE >> KMEM_BIG_SHIFT)
178 1.46 para
179 1.46 para static pool_cache_t kmem_cache_big[KMEM_CACHE_BIG_COUNT] __cacheline_aligned;
180 1.46 para static size_t kmem_cache_big_maxidx __read_mostly;
181 1.46 para
182 1.53 maxv #if defined(DIAGNOSTIC) && defined(_HARDKERNEL)
183 1.57 maxv #define KMEM_SIZE
184 1.60 maxv #define KMEM_REDZONE
185 1.53 maxv #endif /* defined(DIAGNOSTIC) */
186 1.53 maxv
187 1.45 martin #if defined(DEBUG) && defined(_HARDKERNEL)
188 1.61 maxv #define KMEM_SIZE
189 1.19 yamt #define KMEM_POISON
190 1.27 ad #define KMEM_GUARD
191 1.61 maxv static void *kmem_freecheck;
192 1.19 yamt #endif /* defined(DEBUG) */
193 1.19 yamt
194 1.19 yamt #if defined(KMEM_POISON)
195 1.39 para static int kmem_poison_ctor(void *, void *, int);
196 1.4 yamt static void kmem_poison_fill(void *, size_t);
197 1.4 yamt static void kmem_poison_check(void *, size_t);
198 1.19 yamt #else /* defined(KMEM_POISON) */
199 1.40 rmind #define kmem_poison_fill(p, sz) /* nothing */
200 1.40 rmind #define kmem_poison_check(p, sz) /* nothing */
201 1.19 yamt #endif /* defined(KMEM_POISON) */
202 1.19 yamt
203 1.19 yamt #if defined(KMEM_REDZONE)
204 1.54 maxv #define REDZONE_SIZE 2
205 1.57 maxv static void kmem_redzone_fill(void *, size_t);
206 1.57 maxv static void kmem_redzone_check(void *, size_t);
207 1.19 yamt #else /* defined(KMEM_REDZONE) */
208 1.19 yamt #define REDZONE_SIZE 0
209 1.54 maxv #define kmem_redzone_fill(p, sz) /* nothing */
210 1.54 maxv #define kmem_redzone_check(p, sz) /* nothing */
211 1.19 yamt #endif /* defined(KMEM_REDZONE) */
212 1.4 yamt
213 1.23 ad #if defined(KMEM_SIZE)
214 1.57 maxv struct kmem_header {
215 1.57 maxv size_t size;
216 1.57 maxv } __aligned(KMEM_ALIGN);
217 1.57 maxv #define SIZE_SIZE sizeof(struct kmem_header)
218 1.23 ad static void kmem_size_set(void *, size_t);
219 1.39 para static void kmem_size_check(void *, size_t);
220 1.23 ad #else
221 1.23 ad #define SIZE_SIZE 0
222 1.23 ad #define kmem_size_set(p, sz) /* nothing */
223 1.23 ad #define kmem_size_check(p, sz) /* nothing */
224 1.23 ad #endif
225 1.23 ad
226 1.52 maxv #if defined(KMEM_GUARD)
227 1.52 maxv #ifndef KMEM_GUARD_DEPTH
228 1.52 maxv #define KMEM_GUARD_DEPTH 0
229 1.52 maxv #endif
230 1.61 maxv struct kmem_guard {
231 1.61 maxv u_int kg_depth;
232 1.61 maxv intptr_t * kg_fifo;
233 1.61 maxv u_int kg_rotor;
234 1.61 maxv vmem_t * kg_vmem;
235 1.61 maxv };
236 1.61 maxv
237 1.61 maxv static bool kmem_guard_init(struct kmem_guard *, u_int, vmem_t *);
238 1.61 maxv static void *kmem_guard_alloc(struct kmem_guard *, size_t, bool);
239 1.61 maxv static void kmem_guard_free(struct kmem_guard *, size_t, void *);
240 1.61 maxv
241 1.52 maxv int kmem_guard_depth = KMEM_GUARD_DEPTH;
242 1.61 maxv static bool kmem_guard_enabled;
243 1.61 maxv static struct kmem_guard kmem_guard;
244 1.52 maxv #endif /* defined(KMEM_GUARD) */
245 1.52 maxv
246 1.32 skrll CTASSERT(KM_SLEEP == PR_WAITOK);
247 1.32 skrll CTASSERT(KM_NOSLEEP == PR_NOWAIT);
248 1.32 skrll
249 1.46 para /*
250 1.46 para * kmem_intr_alloc: allocate wired memory.
251 1.46 para */
252 1.46 para
253 1.39 para void *
254 1.50 yamt kmem_intr_alloc(size_t requested_size, km_flag_t kmflags)
255 1.1 yamt {
256 1.40 rmind size_t allocsz, index;
257 1.50 yamt size_t size;
258 1.39 para pool_cache_t pc;
259 1.39 para uint8_t *p;
260 1.1 yamt
261 1.50 yamt KASSERT(requested_size > 0);
262 1.1 yamt
263 1.65 riastrad KASSERT((kmflags & KM_SLEEP) || (kmflags & KM_NOSLEEP));
264 1.65 riastrad KASSERT(!(kmflags & KM_SLEEP) || !(kmflags & KM_NOSLEEP));
265 1.65 riastrad
266 1.39 para #ifdef KMEM_GUARD
267 1.61 maxv if (kmem_guard_enabled) {
268 1.61 maxv return kmem_guard_alloc(&kmem_guard, requested_size,
269 1.39 para (kmflags & KM_SLEEP) != 0);
270 1.1 yamt }
271 1.39 para #endif
272 1.50 yamt size = kmem_roundup_size(requested_size);
273 1.54 maxv allocsz = size + SIZE_SIZE;
274 1.54 maxv
275 1.54 maxv #ifdef KMEM_REDZONE
276 1.54 maxv if (size - requested_size < REDZONE_SIZE) {
277 1.57 maxv /* If there isn't enough space in the padding, allocate
278 1.57 maxv * one more memory chunk for the red zone. */
279 1.56 maxv allocsz += kmem_roundup_size(REDZONE_SIZE);
280 1.54 maxv }
281 1.54 maxv #endif
282 1.39 para
283 1.46 para if ((index = ((allocsz -1) >> KMEM_SHIFT))
284 1.46 para < kmem_cache_maxidx) {
285 1.46 para pc = kmem_cache[index];
286 1.46 para } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
287 1.55 maxv < kmem_cache_big_maxidx) {
288 1.46 para pc = kmem_cache_big[index];
289 1.48 uebayasi } else {
290 1.40 rmind int ret = uvm_km_kmem_alloc(kmem_va_arena,
291 1.43 para (vsize_t)round_page(size),
292 1.39 para ((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP)
293 1.39 para | VM_INSTANTFIT, (vmem_addr_t *)&p);
294 1.46 para if (ret) {
295 1.46 para return NULL;
296 1.46 para }
297 1.46 para FREECHECK_OUT(&kmem_freecheck, p);
298 1.46 para return p;
299 1.1 yamt }
300 1.1 yamt
301 1.39 para p = pool_cache_get(pc, kmflags);
302 1.39 para
303 1.39 para if (__predict_true(p != NULL)) {
304 1.58 maxv kmem_poison_check(p, allocsz);
305 1.39 para FREECHECK_OUT(&kmem_freecheck, p);
306 1.50 yamt kmem_size_set(p, requested_size);
307 1.54 maxv kmem_redzone_fill(p, requested_size + SIZE_SIZE);
308 1.47 para
309 1.47 para return p + SIZE_SIZE;
310 1.39 para }
311 1.47 para return p;
312 1.1 yamt }
313 1.1 yamt
314 1.46 para /*
315 1.46 para * kmem_intr_zalloc: allocate zeroed wired memory.
316 1.46 para */
317 1.46 para
318 1.39 para void *
319 1.39 para kmem_intr_zalloc(size_t size, km_flag_t kmflags)
320 1.23 ad {
321 1.39 para void *p;
322 1.23 ad
323 1.39 para p = kmem_intr_alloc(size, kmflags);
324 1.39 para if (p != NULL) {
325 1.39 para memset(p, 0, size);
326 1.39 para }
327 1.39 para return p;
328 1.23 ad }
329 1.23 ad
330 1.46 para /*
331 1.46 para * kmem_intr_free: free wired memory allocated by kmem_alloc.
332 1.46 para */
333 1.46 para
334 1.39 para void
335 1.50 yamt kmem_intr_free(void *p, size_t requested_size)
336 1.23 ad {
337 1.40 rmind size_t allocsz, index;
338 1.50 yamt size_t size;
339 1.39 para pool_cache_t pc;
340 1.23 ad
341 1.39 para KASSERT(p != NULL);
342 1.50 yamt KASSERT(requested_size > 0);
343 1.39 para
344 1.39 para #ifdef KMEM_GUARD
345 1.61 maxv if (kmem_guard_enabled) {
346 1.61 maxv kmem_guard_free(&kmem_guard, requested_size, p);
347 1.39 para return;
348 1.39 para }
349 1.39 para #endif
350 1.54 maxv
351 1.50 yamt size = kmem_roundup_size(requested_size);
352 1.54 maxv allocsz = size + SIZE_SIZE;
353 1.54 maxv
354 1.54 maxv #ifdef KMEM_REDZONE
355 1.54 maxv if (size - requested_size < REDZONE_SIZE) {
356 1.56 maxv allocsz += kmem_roundup_size(REDZONE_SIZE);
357 1.54 maxv }
358 1.54 maxv #endif
359 1.39 para
360 1.46 para if ((index = ((allocsz -1) >> KMEM_SHIFT))
361 1.46 para < kmem_cache_maxidx) {
362 1.46 para pc = kmem_cache[index];
363 1.46 para } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
364 1.55 maxv < kmem_cache_big_maxidx) {
365 1.46 para pc = kmem_cache_big[index];
366 1.46 para } else {
367 1.46 para FREECHECK_IN(&kmem_freecheck, p);
368 1.39 para uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
369 1.43 para round_page(size));
370 1.39 para return;
371 1.39 para }
372 1.39 para
373 1.46 para p = (uint8_t *)p - SIZE_SIZE;
374 1.50 yamt kmem_size_check(p, requested_size);
375 1.54 maxv kmem_redzone_check(p, requested_size + SIZE_SIZE);
376 1.39 para FREECHECK_IN(&kmem_freecheck, p);
377 1.46 para LOCKDEBUG_MEM_CHECK(p, size);
378 1.39 para kmem_poison_fill(p, allocsz);
379 1.39 para
380 1.39 para pool_cache_put(pc, p);
381 1.23 ad }
382 1.23 ad
383 1.1 yamt /* ---- kmem API */
384 1.1 yamt
385 1.1 yamt /*
386 1.1 yamt * kmem_alloc: allocate wired memory.
387 1.1 yamt * => must not be called from interrupt context.
388 1.1 yamt */
389 1.1 yamt
390 1.1 yamt void *
391 1.1 yamt kmem_alloc(size_t size, km_flag_t kmflags)
392 1.1 yamt {
393 1.62 chs void *v;
394 1.62 chs
395 1.40 rmind KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
396 1.40 rmind "kmem(9) should not be used from the interrupt context");
397 1.62 chs v = kmem_intr_alloc(size, kmflags);
398 1.62 chs KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
399 1.62 chs return v;
400 1.1 yamt }
401 1.1 yamt
402 1.1 yamt /*
403 1.39 para * kmem_zalloc: allocate zeroed wired memory.
404 1.2 yamt * => must not be called from interrupt context.
405 1.2 yamt */
406 1.2 yamt
407 1.2 yamt void *
408 1.2 yamt kmem_zalloc(size_t size, km_flag_t kmflags)
409 1.2 yamt {
410 1.62 chs void *v;
411 1.62 chs
412 1.40 rmind KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
413 1.40 rmind "kmem(9) should not be used from the interrupt context");
414 1.62 chs v = kmem_intr_zalloc(size, kmflags);
415 1.62 chs KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
416 1.62 chs return v;
417 1.2 yamt }
418 1.2 yamt
419 1.2 yamt /*
420 1.1 yamt * kmem_free: free wired memory allocated by kmem_alloc.
421 1.1 yamt * => must not be called from interrupt context.
422 1.1 yamt */
423 1.1 yamt
424 1.1 yamt void
425 1.1 yamt kmem_free(void *p, size_t size)
426 1.1 yamt {
427 1.23 ad KASSERT(!cpu_intr_p());
428 1.27 ad KASSERT(!cpu_softintr_p());
429 1.39 para kmem_intr_free(p, size);
430 1.1 yamt }
431 1.1 yamt
432 1.46 para static size_t
433 1.39 para kmem_create_caches(const struct kmem_cache_info *array,
434 1.46 para pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl)
435 1.1 yamt {
436 1.46 para size_t maxidx = 0;
437 1.46 para size_t table_unit = (1 << shift);
438 1.39 para size_t size = table_unit;
439 1.23 ad int i;
440 1.1 yamt
441 1.39 para for (i = 0; array[i].kc_size != 0 ; i++) {
442 1.40 rmind const char *name = array[i].kc_name;
443 1.39 para size_t cache_size = array[i].kc_size;
444 1.46 para struct pool_allocator *pa;
445 1.40 rmind int flags = PR_NOALIGN;
446 1.40 rmind pool_cache_t pc;
447 1.39 para size_t align;
448 1.39 para
449 1.39 para if ((cache_size & (CACHE_LINE_SIZE - 1)) == 0)
450 1.39 para align = CACHE_LINE_SIZE;
451 1.39 para else if ((cache_size & (PAGE_SIZE - 1)) == 0)
452 1.39 para align = PAGE_SIZE;
453 1.39 para else
454 1.39 para align = KMEM_ALIGN;
455 1.39 para
456 1.39 para if (cache_size < CACHE_LINE_SIZE)
457 1.39 para flags |= PR_NOTOUCH;
458 1.27 ad
459 1.39 para /* check if we reached the requested size */
460 1.46 para if (cache_size > maxsize || cache_size > PAGE_SIZE) {
461 1.23 ad break;
462 1.40 rmind }
463 1.46 para if ((cache_size >> shift) > maxidx) {
464 1.46 para maxidx = cache_size >> shift;
465 1.46 para }
466 1.46 para
467 1.46 para if ((cache_size >> shift) > maxidx) {
468 1.46 para maxidx = cache_size >> shift;
469 1.40 rmind }
470 1.1 yamt
471 1.46 para pa = &pool_allocator_kmem;
472 1.39 para #if defined(KMEM_POISON)
473 1.39 para pc = pool_cache_init(cache_size, align, 0, flags,
474 1.49 yamt name, pa, ipl, kmem_poison_ctor,
475 1.39 para NULL, (void *)cache_size);
476 1.39 para #else /* defined(KMEM_POISON) */
477 1.39 para pc = pool_cache_init(cache_size, align, 0, flags,
478 1.46 para name, pa, ipl, NULL, NULL, NULL);
479 1.39 para #endif /* defined(KMEM_POISON) */
480 1.1 yamt
481 1.39 para while (size <= cache_size) {
482 1.46 para alloc_table[(size - 1) >> shift] = pc;
483 1.39 para size += table_unit;
484 1.39 para }
485 1.1 yamt }
486 1.46 para return maxidx;
487 1.1 yamt }
488 1.1 yamt
489 1.39 para void
490 1.39 para kmem_init(void)
491 1.1 yamt {
492 1.39 para #ifdef KMEM_GUARD
493 1.61 maxv kmem_guard_enabled = kmem_guard_init(&kmem_guard, kmem_guard_depth,
494 1.42 rmind kmem_va_arena);
495 1.39 para #endif
496 1.46 para kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes,
497 1.46 para kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM);
498 1.55 maxv kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes,
499 1.46 para kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM);
500 1.1 yamt }
501 1.4 yamt
502 1.39 para size_t
503 1.39 para kmem_roundup_size(size_t size)
504 1.7 yamt {
505 1.61 maxv return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
506 1.61 maxv }
507 1.7 yamt
508 1.61 maxv /*
509 1.61 maxv * Used to dynamically allocate string with kmem accordingly to format.
510 1.61 maxv */
511 1.61 maxv char *
512 1.61 maxv kmem_asprintf(const char *fmt, ...)
513 1.61 maxv {
514 1.61 maxv int size __diagused, len;
515 1.61 maxv va_list va;
516 1.61 maxv char *str;
517 1.61 maxv
518 1.61 maxv va_start(va, fmt);
519 1.61 maxv len = vsnprintf(NULL, 0, fmt, va);
520 1.61 maxv va_end(va);
521 1.61 maxv
522 1.61 maxv str = kmem_alloc(len + 1, KM_SLEEP);
523 1.61 maxv
524 1.61 maxv va_start(va, fmt);
525 1.61 maxv size = vsnprintf(str, len + 1, fmt, va);
526 1.61 maxv va_end(va);
527 1.61 maxv
528 1.61 maxv KASSERT(size == len);
529 1.61 maxv
530 1.61 maxv return str;
531 1.7 yamt }
532 1.7 yamt
533 1.64 christos char *
534 1.64 christos kmem_strdupsize(const char *str, size_t *lenp, km_flag_t flags)
535 1.64 christos {
536 1.64 christos size_t len = strlen(str) + 1;
537 1.64 christos char *ptr = kmem_alloc(len, flags);
538 1.64 christos if (ptr == NULL)
539 1.64 christos return NULL;
540 1.64 christos
541 1.64 christos if (lenp)
542 1.64 christos *lenp = len;
543 1.64 christos memcpy(ptr, str, len);
544 1.64 christos return ptr;
545 1.64 christos }
546 1.64 christos
547 1.66 christos char *
548 1.66 christos kmem_strndup(const char *str, size_t maxlen, km_flag_t flags)
549 1.66 christos {
550 1.66 christos KASSERT(str != NULL);
551 1.66 christos KASSERT(maxlen != 0);
552 1.66 christos
553 1.66 christos size_t len = strnlen(str, maxlen);
554 1.66 christos char *ptr = kmem_alloc(len + 1, flags);
555 1.66 christos if (ptr == NULL)
556 1.66 christos return NULL;
557 1.66 christos
558 1.66 christos memcpy(ptr, str, len);
559 1.66 christos ptr[len] = '\0';
560 1.66 christos
561 1.66 christos return ptr;
562 1.66 christos }
563 1.66 christos
564 1.64 christos void
565 1.64 christos kmem_strfree(char *str)
566 1.64 christos {
567 1.64 christos if (str == NULL)
568 1.64 christos return;
569 1.64 christos
570 1.64 christos kmem_free(str, strlen(str) + 1);
571 1.64 christos }
572 1.64 christos
573 1.54 maxv /* ------------------ DEBUG / DIAGNOSTIC ------------------ */
574 1.4 yamt
575 1.54 maxv #if defined(KMEM_POISON) || defined(KMEM_REDZONE)
576 1.4 yamt #if defined(_LP64)
577 1.39 para #define PRIME 0x9e37fffffffc0000UL
578 1.4 yamt #else /* defined(_LP64) */
579 1.39 para #define PRIME 0x9e3779b1
580 1.4 yamt #endif /* defined(_LP64) */
581 1.4 yamt
582 1.4 yamt static inline uint8_t
583 1.59 maxv kmem_pattern_generate(const void *p)
584 1.4 yamt {
585 1.39 para return (uint8_t)(((uintptr_t)p) * PRIME
586 1.39 para >> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT);
587 1.39 para }
588 1.59 maxv #endif /* defined(KMEM_POISON) || defined(KMEM_REDZONE) */
589 1.39 para
590 1.59 maxv #if defined(KMEM_POISON)
591 1.39 para static int
592 1.39 para kmem_poison_ctor(void *arg, void *obj, int flag)
593 1.39 para {
594 1.39 para size_t sz = (size_t)arg;
595 1.39 para
596 1.39 para kmem_poison_fill(obj, sz);
597 1.39 para
598 1.39 para return 0;
599 1.4 yamt }
600 1.4 yamt
601 1.4 yamt static void
602 1.4 yamt kmem_poison_fill(void *p, size_t sz)
603 1.4 yamt {
604 1.4 yamt uint8_t *cp;
605 1.4 yamt const uint8_t *ep;
606 1.4 yamt
607 1.4 yamt cp = p;
608 1.4 yamt ep = cp + sz;
609 1.4 yamt while (cp < ep) {
610 1.59 maxv *cp = kmem_pattern_generate(cp);
611 1.4 yamt cp++;
612 1.4 yamt }
613 1.4 yamt }
614 1.4 yamt
615 1.4 yamt static void
616 1.4 yamt kmem_poison_check(void *p, size_t sz)
617 1.4 yamt {
618 1.4 yamt uint8_t *cp;
619 1.4 yamt const uint8_t *ep;
620 1.4 yamt
621 1.4 yamt cp = p;
622 1.4 yamt ep = cp + sz;
623 1.4 yamt while (cp < ep) {
624 1.59 maxv const uint8_t expected = kmem_pattern_generate(cp);
625 1.4 yamt
626 1.4 yamt if (*cp != expected) {
627 1.4 yamt panic("%s: %p: 0x%02x != 0x%02x\n",
628 1.39 para __func__, cp, *cp, expected);
629 1.4 yamt }
630 1.4 yamt cp++;
631 1.4 yamt }
632 1.4 yamt }
633 1.19 yamt #endif /* defined(KMEM_POISON) */
634 1.23 ad
635 1.23 ad #if defined(KMEM_SIZE)
636 1.23 ad static void
637 1.23 ad kmem_size_set(void *p, size_t sz)
638 1.23 ad {
639 1.57 maxv struct kmem_header *hd;
640 1.57 maxv hd = (struct kmem_header *)p;
641 1.57 maxv hd->size = sz;
642 1.23 ad }
643 1.23 ad
644 1.23 ad static void
645 1.39 para kmem_size_check(void *p, size_t sz)
646 1.23 ad {
647 1.57 maxv struct kmem_header *hd;
648 1.57 maxv size_t hsz;
649 1.23 ad
650 1.57 maxv hd = (struct kmem_header *)p;
651 1.57 maxv hsz = hd->size;
652 1.57 maxv
653 1.57 maxv if (hsz != sz) {
654 1.23 ad panic("kmem_free(%p, %zu) != allocated size %zu",
655 1.57 maxv (const uint8_t *)p + SIZE_SIZE, sz, hsz);
656 1.23 ad }
657 1.23 ad }
658 1.54 maxv #endif /* defined(KMEM_SIZE) */
659 1.54 maxv
660 1.54 maxv #if defined(KMEM_REDZONE)
661 1.59 maxv #define STATIC_BYTE 0xFE
662 1.59 maxv CTASSERT(REDZONE_SIZE > 1);
663 1.54 maxv static void
664 1.54 maxv kmem_redzone_fill(void *p, size_t sz)
665 1.54 maxv {
666 1.59 maxv uint8_t *cp, pat;
667 1.54 maxv const uint8_t *ep;
668 1.54 maxv
669 1.54 maxv cp = (uint8_t *)p + sz;
670 1.54 maxv ep = cp + REDZONE_SIZE;
671 1.59 maxv
672 1.59 maxv /*
673 1.59 maxv * We really don't want the first byte of the red zone to be '\0';
674 1.59 maxv * an off-by-one in a string may not be properly detected.
675 1.59 maxv */
676 1.59 maxv pat = kmem_pattern_generate(cp);
677 1.59 maxv *cp = (pat == '\0') ? STATIC_BYTE: pat;
678 1.59 maxv cp++;
679 1.59 maxv
680 1.54 maxv while (cp < ep) {
681 1.59 maxv *cp = kmem_pattern_generate(cp);
682 1.54 maxv cp++;
683 1.54 maxv }
684 1.54 maxv }
685 1.54 maxv
686 1.54 maxv static void
687 1.54 maxv kmem_redzone_check(void *p, size_t sz)
688 1.54 maxv {
689 1.59 maxv uint8_t *cp, pat, expected;
690 1.54 maxv const uint8_t *ep;
691 1.54 maxv
692 1.54 maxv cp = (uint8_t *)p + sz;
693 1.57 maxv ep = cp + REDZONE_SIZE;
694 1.59 maxv
695 1.59 maxv pat = kmem_pattern_generate(cp);
696 1.59 maxv expected = (pat == '\0') ? STATIC_BYTE: pat;
697 1.59 maxv if (expected != *cp) {
698 1.59 maxv panic("%s: %p: 0x%02x != 0x%02x\n",
699 1.59 maxv __func__, cp, *cp, expected);
700 1.59 maxv }
701 1.59 maxv cp++;
702 1.59 maxv
703 1.54 maxv while (cp < ep) {
704 1.59 maxv expected = kmem_pattern_generate(cp);
705 1.54 maxv if (*cp != expected) {
706 1.54 maxv panic("%s: %p: 0x%02x != 0x%02x\n",
707 1.54 maxv __func__, cp, *cp, expected);
708 1.54 maxv }
709 1.54 maxv cp++;
710 1.54 maxv }
711 1.54 maxv }
712 1.54 maxv #endif /* defined(KMEM_REDZONE) */
713 1.54 maxv
714 1.33 haad
715 1.61 maxv #if defined(KMEM_GUARD)
716 1.33 haad /*
717 1.61 maxv * The ultimate memory allocator for debugging, baby. It tries to catch:
718 1.61 maxv *
719 1.61 maxv * 1. Overflow, in realtime. A guard page sits immediately after the
720 1.61 maxv * requested area; a read/write overflow therefore triggers a page
721 1.61 maxv * fault.
722 1.61 maxv * 2. Invalid pointer/size passed, at free. A kmem_header structure sits
723 1.61 maxv * just before the requested area, and holds the allocated size. Any
724 1.61 maxv * difference with what is given at free triggers a panic.
725 1.61 maxv * 3. Underflow, at free. If an underflow occurs, the kmem header will be
726 1.61 maxv * modified, and 2. will trigger a panic.
727 1.61 maxv * 4. Use-after-free. When freeing, the memory is unmapped, and depending
728 1.61 maxv * on the value of kmem_guard_depth, the kernel will more or less delay
729 1.61 maxv * the recycling of that memory. Which means that any ulterior read/write
730 1.61 maxv * access to the memory will trigger a page fault, given it hasn't been
731 1.61 maxv * recycled yet.
732 1.61 maxv */
733 1.61 maxv
734 1.61 maxv #include <sys/atomic.h>
735 1.61 maxv #include <uvm/uvm.h>
736 1.61 maxv
737 1.61 maxv static bool
738 1.61 maxv kmem_guard_init(struct kmem_guard *kg, u_int depth, vmem_t *vm)
739 1.61 maxv {
740 1.61 maxv vaddr_t va;
741 1.61 maxv
742 1.61 maxv /* If not enabled, we have nothing to do. */
743 1.61 maxv if (depth == 0) {
744 1.61 maxv return false;
745 1.61 maxv }
746 1.61 maxv depth = roundup(depth, PAGE_SIZE / sizeof(void *));
747 1.61 maxv KASSERT(depth != 0);
748 1.61 maxv
749 1.61 maxv /*
750 1.61 maxv * Allocate fifo.
751 1.61 maxv */
752 1.61 maxv va = uvm_km_alloc(kernel_map, depth * sizeof(void *), PAGE_SIZE,
753 1.61 maxv UVM_KMF_WIRED | UVM_KMF_ZERO);
754 1.61 maxv if (va == 0) {
755 1.61 maxv return false;
756 1.61 maxv }
757 1.61 maxv
758 1.61 maxv /*
759 1.61 maxv * Init object.
760 1.61 maxv */
761 1.61 maxv kg->kg_vmem = vm;
762 1.61 maxv kg->kg_fifo = (void *)va;
763 1.61 maxv kg->kg_depth = depth;
764 1.61 maxv kg->kg_rotor = 0;
765 1.61 maxv
766 1.61 maxv printf("kmem_guard(%p): depth %d\n", kg, depth);
767 1.61 maxv return true;
768 1.61 maxv }
769 1.61 maxv
770 1.61 maxv static void *
771 1.61 maxv kmem_guard_alloc(struct kmem_guard *kg, size_t requested_size, bool waitok)
772 1.61 maxv {
773 1.61 maxv struct vm_page *pg;
774 1.61 maxv vm_flag_t flags;
775 1.61 maxv vmem_addr_t va;
776 1.61 maxv vaddr_t loopva;
777 1.61 maxv vsize_t loopsize;
778 1.61 maxv size_t size;
779 1.61 maxv void **p;
780 1.61 maxv
781 1.61 maxv /*
782 1.61 maxv * Compute the size: take the kmem header into account, and add a guard
783 1.61 maxv * page at the end.
784 1.61 maxv */
785 1.61 maxv size = round_page(requested_size + SIZE_SIZE) + PAGE_SIZE;
786 1.61 maxv
787 1.61 maxv /* Allocate pages of kernel VA, but do not map anything in yet. */
788 1.61 maxv flags = VM_BESTFIT | (waitok ? VM_SLEEP : VM_NOSLEEP);
789 1.61 maxv if (vmem_alloc(kg->kg_vmem, size, flags, &va) != 0) {
790 1.61 maxv return NULL;
791 1.61 maxv }
792 1.61 maxv
793 1.61 maxv loopva = va;
794 1.61 maxv loopsize = size - PAGE_SIZE;
795 1.61 maxv
796 1.61 maxv while (loopsize) {
797 1.61 maxv pg = uvm_pagealloc(NULL, loopva, NULL, 0);
798 1.61 maxv if (__predict_false(pg == NULL)) {
799 1.61 maxv if (waitok) {
800 1.61 maxv uvm_wait("kmem_guard");
801 1.61 maxv continue;
802 1.61 maxv } else {
803 1.61 maxv uvm_km_pgremove_intrsafe(kernel_map, va,
804 1.61 maxv va + size);
805 1.61 maxv vmem_free(kg->kg_vmem, va, size);
806 1.61 maxv return NULL;
807 1.61 maxv }
808 1.61 maxv }
809 1.61 maxv
810 1.61 maxv pg->flags &= ~PG_BUSY; /* new page */
811 1.61 maxv UVM_PAGE_OWN(pg, NULL);
812 1.61 maxv pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
813 1.61 maxv VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE);
814 1.61 maxv
815 1.61 maxv loopva += PAGE_SIZE;
816 1.61 maxv loopsize -= PAGE_SIZE;
817 1.61 maxv }
818 1.61 maxv
819 1.61 maxv pmap_update(pmap_kernel());
820 1.61 maxv
821 1.61 maxv /*
822 1.61 maxv * Offset the returned pointer so that the unmapped guard page sits
823 1.61 maxv * immediately after the returned object.
824 1.61 maxv */
825 1.61 maxv p = (void **)((va + (size - PAGE_SIZE) - requested_size) & ~(uintptr_t)ALIGNBYTES);
826 1.61 maxv kmem_size_set((uint8_t *)p - SIZE_SIZE, requested_size);
827 1.61 maxv return (void *)p;
828 1.61 maxv }
829 1.61 maxv
830 1.61 maxv static void
831 1.61 maxv kmem_guard_free(struct kmem_guard *kg, size_t requested_size, void *p)
832 1.33 haad {
833 1.61 maxv vaddr_t va;
834 1.61 maxv u_int rotor;
835 1.61 maxv size_t size;
836 1.61 maxv uint8_t *ptr;
837 1.48 uebayasi
838 1.61 maxv ptr = (uint8_t *)p - SIZE_SIZE;
839 1.61 maxv kmem_size_check(ptr, requested_size);
840 1.61 maxv va = trunc_page((vaddr_t)ptr);
841 1.61 maxv size = round_page(requested_size + SIZE_SIZE) + PAGE_SIZE;
842 1.33 haad
843 1.61 maxv KASSERT(pmap_extract(pmap_kernel(), va, NULL));
844 1.61 maxv KASSERT(!pmap_extract(pmap_kernel(), va + (size - PAGE_SIZE), NULL));
845 1.33 haad
846 1.61 maxv /*
847 1.61 maxv * Unmap and free the pages. The last one is never allocated.
848 1.61 maxv */
849 1.61 maxv uvm_km_pgremove_intrsafe(kernel_map, va, va + size);
850 1.61 maxv pmap_update(pmap_kernel());
851 1.38 christos
852 1.61 maxv #if 0
853 1.61 maxv /*
854 1.61 maxv * XXX: Here, we need to atomically register the va and its size in the
855 1.61 maxv * fifo.
856 1.61 maxv */
857 1.33 haad
858 1.61 maxv /*
859 1.61 maxv * Put the VA allocation into the list and swap an old one out to free.
860 1.61 maxv * This behaves mostly like a fifo.
861 1.61 maxv */
862 1.61 maxv rotor = atomic_inc_uint_nv(&kg->kg_rotor) % kg->kg_depth;
863 1.61 maxv va = (vaddr_t)atomic_swap_ptr(&kg->kg_fifo[rotor], (void *)va);
864 1.61 maxv if (va != 0) {
865 1.61 maxv vmem_free(kg->kg_vmem, va, size);
866 1.61 maxv }
867 1.61 maxv #else
868 1.61 maxv (void)rotor;
869 1.61 maxv vmem_free(kg->kg_vmem, va, size);
870 1.61 maxv #endif
871 1.33 haad }
872 1.61 maxv
873 1.61 maxv #endif /* defined(KMEM_GUARD) */
874