subr_kmem.c revision 1.50.4.1 1 /* $NetBSD: subr_kmem.c,v 1.50.4.1 2014/05/18 17:46:07 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c)2006 YAMAMOTO Takashi,
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58 /*
59 * allocator of kernel wired memory.
60 */
61
62 /*
63 * This allocator has some debug features enabled with "option DEBUG".
64 *
65 * KMEM_POISON
66 * Try to detect modify-after-free bugs.
67 *
68 * Fill freed (in the sense of kmem_free) memory with a garbage pattern.
69 * Check the pattern on allocation.
70 *
71 * KMEM_REDZONE
72 * Try to detect overrun bugs.
73 *
74 * Allocate some more bytes for each allocation.
75 * The extra bytes are checked by KMEM_POISON on kmem_free.
76 *
77 * KMEM_SIZE
78 * Try to detect alloc/free size mismatch bugs.
79 *
80 * Prefix each allocations with a fixed-sized header and record
81 * the exact user-requested allocation size in it.
82 * When freeing, compare it with kmem_free's "size" argument.
83 *
84 * KMEM_GUARD
85 * See the below "kmguard" section.
86 */
87
88 /*
89 * kmguard
90 *
91 * A kernel with "option DEBUG" has "kmguard" debugging feature compiled in.
92 * See the comment in uvm/uvm_kmguard.c for what kind of bugs it tries to
93 * detect. Even if compiled in, it's disabled by default because it's very
94 * expensive. You can enable it on boot by:
95 *
96 * boot -d
97 * db> w kmem_guard_depth 0t30000
98 * db> c
99 *
100 * The default value of kmem_guard_depth is 0, which means disabled.
101 * It can be changed by KMEM_GUARD_DEPTH kernel config option.
102 */
103
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.50.4.1 2014/05/18 17:46:07 rmind Exp $");
106
107 #include <sys/param.h>
108 #include <sys/callback.h>
109 #include <sys/kmem.h>
110 #include <sys/pool.h>
111 #include <sys/debug.h>
112 #include <sys/lockdebug.h>
113 #include <sys/cpu.h>
114
115 #include <uvm/uvm_extern.h>
116 #include <uvm/uvm_map.h>
117 #include <uvm/uvm_kmguard.h>
118
119 #include <lib/libkern/libkern.h>
120
121 struct kmem_cache_info {
122 size_t kc_size;
123 const char * kc_name;
124 };
125
126 static const struct kmem_cache_info kmem_cache_sizes[] = {
127 { 8, "kmem-8" },
128 { 16, "kmem-16" },
129 { 24, "kmem-24" },
130 { 32, "kmem-32" },
131 { 40, "kmem-40" },
132 { 48, "kmem-48" },
133 { 56, "kmem-56" },
134 { 64, "kmem-64" },
135 { 80, "kmem-80" },
136 { 96, "kmem-96" },
137 { 112, "kmem-112" },
138 { 128, "kmem-128" },
139 { 160, "kmem-160" },
140 { 192, "kmem-192" },
141 { 224, "kmem-224" },
142 { 256, "kmem-256" },
143 { 320, "kmem-320" },
144 { 384, "kmem-384" },
145 { 448, "kmem-448" },
146 { 512, "kmem-512" },
147 { 768, "kmem-768" },
148 { 1024, "kmem-1024" },
149 { 0, NULL }
150 };
151
152 static const struct kmem_cache_info kmem_cache_big_sizes[] = {
153 { 2048, "kmem-2048" },
154 { 4096, "kmem-4096" },
155 { 8192, "kmem-8192" },
156 { 16384, "kmem-16384" },
157 { 0, NULL }
158 };
159
160 /*
161 * KMEM_ALIGN is the smallest guaranteed alignment and also the
162 * smallest allocateable quantum.
163 * Every cache size >= CACHE_LINE_SIZE gets CACHE_LINE_SIZE alignment.
164 */
165 #define KMEM_ALIGN 8
166 #define KMEM_SHIFT 3
167 #define KMEM_MAXSIZE 1024
168 #define KMEM_CACHE_COUNT (KMEM_MAXSIZE >> KMEM_SHIFT)
169
170 static pool_cache_t kmem_cache[KMEM_CACHE_COUNT] __cacheline_aligned;
171 static size_t kmem_cache_maxidx __read_mostly;
172
173 #define KMEM_BIG_ALIGN 2048
174 #define KMEM_BIG_SHIFT 11
175 #define KMEM_BIG_MAXSIZE 16384
176 #define KMEM_CACHE_BIG_COUNT (KMEM_BIG_MAXSIZE >> KMEM_BIG_SHIFT)
177
178 static pool_cache_t kmem_cache_big[KMEM_CACHE_BIG_COUNT] __cacheline_aligned;
179 static size_t kmem_cache_big_maxidx __read_mostly;
180
181
182 #if defined(DEBUG) && defined(_HARDKERNEL)
183 #ifndef KMEM_GUARD_DEPTH
184 #define KMEM_GUARD_DEPTH 0
185 #endif
186 int kmem_guard_depth = KMEM_GUARD_DEPTH;
187 size_t kmem_guard_size;
188 static struct uvm_kmguard kmem_guard;
189 static void *kmem_freecheck;
190 #define KMEM_POISON
191 #define KMEM_REDZONE
192 #define KMEM_SIZE
193 #define KMEM_GUARD
194 #endif /* defined(DEBUG) */
195
196 #if defined(KMEM_POISON)
197 static int kmem_poison_ctor(void *, void *, int);
198 static void kmem_poison_fill(void *, size_t);
199 static void kmem_poison_check(void *, size_t);
200 #else /* defined(KMEM_POISON) */
201 #define kmem_poison_fill(p, sz) /* nothing */
202 #define kmem_poison_check(p, sz) /* nothing */
203 #endif /* defined(KMEM_POISON) */
204
205 #if defined(KMEM_REDZONE)
206 #define REDZONE_SIZE 1
207 #else /* defined(KMEM_REDZONE) */
208 #define REDZONE_SIZE 0
209 #endif /* defined(KMEM_REDZONE) */
210
211 #if defined(KMEM_SIZE)
212 #define SIZE_SIZE (MAX(KMEM_ALIGN, sizeof(size_t)))
213 static void kmem_size_set(void *, size_t);
214 static void kmem_size_check(void *, size_t);
215 #else
216 #define SIZE_SIZE 0
217 #define kmem_size_set(p, sz) /* nothing */
218 #define kmem_size_check(p, sz) /* nothing */
219 #endif
220
221 CTASSERT(KM_SLEEP == PR_WAITOK);
222 CTASSERT(KM_NOSLEEP == PR_NOWAIT);
223
224 /*
225 * kmem_intr_alloc: allocate wired memory.
226 */
227
228 void *
229 kmem_intr_alloc(size_t requested_size, km_flag_t kmflags)
230 {
231 size_t allocsz, index;
232 size_t size;
233 pool_cache_t pc;
234 uint8_t *p;
235
236 KASSERT(requested_size > 0);
237
238 #ifdef KMEM_GUARD
239 if (requested_size <= kmem_guard_size) {
240 return uvm_kmguard_alloc(&kmem_guard, requested_size,
241 (kmflags & KM_SLEEP) != 0);
242 }
243 #endif
244 size = kmem_roundup_size(requested_size);
245 allocsz = size + REDZONE_SIZE + SIZE_SIZE;
246
247 if ((index = ((allocsz -1) >> KMEM_SHIFT))
248 < kmem_cache_maxidx) {
249 pc = kmem_cache[index];
250 } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
251 < kmem_cache_big_maxidx) {
252 pc = kmem_cache_big[index];
253 } else {
254 int ret = uvm_km_kmem_alloc(kmem_va_arena,
255 (vsize_t)round_page(size),
256 ((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP)
257 | VM_INSTANTFIT, (vmem_addr_t *)&p);
258 if (ret) {
259 return NULL;
260 }
261 FREECHECK_OUT(&kmem_freecheck, p);
262 return p;
263 }
264
265 p = pool_cache_get(pc, kmflags);
266
267 if (__predict_true(p != NULL)) {
268 kmem_poison_check(p, size);
269 FREECHECK_OUT(&kmem_freecheck, p);
270 kmem_size_set(p, requested_size);
271
272 return p + SIZE_SIZE;
273 }
274 return p;
275 }
276
277 /*
278 * kmem_intr_zalloc: allocate zeroed wired memory.
279 */
280
281 void *
282 kmem_intr_zalloc(size_t size, km_flag_t kmflags)
283 {
284 void *p;
285
286 p = kmem_intr_alloc(size, kmflags);
287 if (p != NULL) {
288 memset(p, 0, size);
289 }
290 return p;
291 }
292
293 /*
294 * kmem_intr_free: free wired memory allocated by kmem_alloc.
295 */
296
297 void
298 kmem_intr_free(void *p, size_t requested_size)
299 {
300 size_t allocsz, index;
301 size_t size;
302 pool_cache_t pc;
303
304 KASSERT(p != NULL);
305 KASSERT(requested_size > 0);
306
307 #ifdef KMEM_GUARD
308 if (requested_size <= kmem_guard_size) {
309 uvm_kmguard_free(&kmem_guard, requested_size, p);
310 return;
311 }
312 #endif
313 size = kmem_roundup_size(requested_size);
314 allocsz = size + REDZONE_SIZE + SIZE_SIZE;
315
316 if ((index = ((allocsz -1) >> KMEM_SHIFT))
317 < kmem_cache_maxidx) {
318 pc = kmem_cache[index];
319 } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
320 < kmem_cache_big_maxidx) {
321 pc = kmem_cache_big[index];
322 } else {
323 FREECHECK_IN(&kmem_freecheck, p);
324 uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
325 round_page(size));
326 return;
327 }
328
329 p = (uint8_t *)p - SIZE_SIZE;
330 kmem_size_check(p, requested_size);
331 FREECHECK_IN(&kmem_freecheck, p);
332 LOCKDEBUG_MEM_CHECK(p, size);
333 kmem_poison_check((uint8_t *)p + SIZE_SIZE + size,
334 allocsz - (SIZE_SIZE + size));
335 kmem_poison_fill(p, allocsz);
336
337 pool_cache_put(pc, p);
338 }
339
340 /* ---- kmem API */
341
342 /*
343 * kmem_alloc: allocate wired memory.
344 * => must not be called from interrupt context.
345 */
346
347 void *
348 kmem_alloc(size_t size, km_flag_t kmflags)
349 {
350
351 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
352 "kmem(9) should not be used from the interrupt context");
353 return kmem_intr_alloc(size, kmflags);
354 }
355
356 /*
357 * kmem_zalloc: allocate zeroed wired memory.
358 * => must not be called from interrupt context.
359 */
360
361 void *
362 kmem_zalloc(size_t size, km_flag_t kmflags)
363 {
364
365 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
366 "kmem(9) should not be used from the interrupt context");
367 return kmem_intr_zalloc(size, kmflags);
368 }
369
370 /*
371 * kmem_free: free wired memory allocated by kmem_alloc.
372 * => must not be called from interrupt context.
373 */
374
375 void
376 kmem_free(void *p, size_t size)
377 {
378
379 KASSERT(!cpu_intr_p());
380 KASSERT(!cpu_softintr_p());
381 kmem_intr_free(p, size);
382 }
383
384 static size_t
385 kmem_create_caches(const struct kmem_cache_info *array,
386 pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl)
387 {
388 size_t maxidx = 0;
389 size_t table_unit = (1 << shift);
390 size_t size = table_unit;
391 int i;
392
393 for (i = 0; array[i].kc_size != 0 ; i++) {
394 const char *name = array[i].kc_name;
395 size_t cache_size = array[i].kc_size;
396 struct pool_allocator *pa;
397 int flags = PR_NOALIGN;
398 pool_cache_t pc;
399 size_t align;
400
401 if ((cache_size & (CACHE_LINE_SIZE - 1)) == 0)
402 align = CACHE_LINE_SIZE;
403 else if ((cache_size & (PAGE_SIZE - 1)) == 0)
404 align = PAGE_SIZE;
405 else
406 align = KMEM_ALIGN;
407
408 if (cache_size < CACHE_LINE_SIZE)
409 flags |= PR_NOTOUCH;
410
411 /* check if we reached the requested size */
412 if (cache_size > maxsize || cache_size > PAGE_SIZE) {
413 break;
414 }
415 if ((cache_size >> shift) > maxidx) {
416 maxidx = cache_size >> shift;
417 }
418
419 if ((cache_size >> shift) > maxidx) {
420 maxidx = cache_size >> shift;
421 }
422
423 pa = &pool_allocator_kmem;
424 #if defined(KMEM_POISON)
425 pc = pool_cache_init(cache_size, align, 0, flags,
426 name, pa, ipl, kmem_poison_ctor,
427 NULL, (void *)cache_size);
428 #else /* defined(KMEM_POISON) */
429 pc = pool_cache_init(cache_size, align, 0, flags,
430 name, pa, ipl, NULL, NULL, NULL);
431 #endif /* defined(KMEM_POISON) */
432
433 while (size <= cache_size) {
434 alloc_table[(size - 1) >> shift] = pc;
435 size += table_unit;
436 }
437 }
438 return maxidx;
439 }
440
441 void
442 kmem_init(void)
443 {
444
445 #ifdef KMEM_GUARD
446 uvm_kmguard_init(&kmem_guard, &kmem_guard_depth, &kmem_guard_size,
447 kmem_va_arena);
448 #endif
449 kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes,
450 kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM);
451 kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes,
452 kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM);
453 }
454
455 size_t
456 kmem_roundup_size(size_t size)
457 {
458
459 return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
460 }
461
462 /* ---- debug */
463
464 #if defined(KMEM_POISON)
465
466 #if defined(_LP64)
467 #define PRIME 0x9e37fffffffc0000UL
468 #else /* defined(_LP64) */
469 #define PRIME 0x9e3779b1
470 #endif /* defined(_LP64) */
471
472 static inline uint8_t
473 kmem_poison_pattern(const void *p)
474 {
475
476 return (uint8_t)(((uintptr_t)p) * PRIME
477 >> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT);
478 }
479
480 static int
481 kmem_poison_ctor(void *arg, void *obj, int flag)
482 {
483 size_t sz = (size_t)arg;
484
485 kmem_poison_fill(obj, sz);
486
487 return 0;
488 }
489
490 static void
491 kmem_poison_fill(void *p, size_t sz)
492 {
493 uint8_t *cp;
494 const uint8_t *ep;
495
496 cp = p;
497 ep = cp + sz;
498 while (cp < ep) {
499 *cp = kmem_poison_pattern(cp);
500 cp++;
501 }
502 }
503
504 static void
505 kmem_poison_check(void *p, size_t sz)
506 {
507 uint8_t *cp;
508 const uint8_t *ep;
509
510 cp = p;
511 ep = cp + sz;
512 while (cp < ep) {
513 const uint8_t expected = kmem_poison_pattern(cp);
514
515 if (*cp != expected) {
516 panic("%s: %p: 0x%02x != 0x%02x\n",
517 __func__, cp, *cp, expected);
518 }
519 cp++;
520 }
521 }
522
523 #endif /* defined(KMEM_POISON) */
524
525 #if defined(KMEM_SIZE)
526 static void
527 kmem_size_set(void *p, size_t sz)
528 {
529
530 memcpy(p, &sz, sizeof(sz));
531 }
532
533 static void
534 kmem_size_check(void *p, size_t sz)
535 {
536 size_t psz;
537
538 memcpy(&psz, p, sizeof(psz));
539 if (psz != sz) {
540 panic("kmem_free(%p, %zu) != allocated size %zu",
541 (const uint8_t *)p + SIZE_SIZE, sz, psz);
542 }
543 }
544 #endif /* defined(KMEM_SIZE) */
545
546 /*
547 * Used to dynamically allocate string with kmem accordingly to format.
548 */
549 char *
550 kmem_asprintf(const char *fmt, ...)
551 {
552 int size __diagused, len;
553 va_list va;
554 char *str;
555
556 va_start(va, fmt);
557 len = vsnprintf(NULL, 0, fmt, va);
558 va_end(va);
559
560 str = kmem_alloc(len + 1, KM_SLEEP);
561
562 va_start(va, fmt);
563 size = vsnprintf(str, len + 1, fmt, va);
564 va_end(va);
565
566 KASSERT(size == len);
567
568 return str;
569 }
570