subr_kmem.c revision 1.59 1 /* $NetBSD: subr_kmem.c,v 1.59 2014/07/03 08:43:49 maxv 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. This allocator has some debug features
60 * enabled with "option DIAGNOSTIC" and "option DEBUG".
61 */
62
63 /*
64 * KMEM_SIZE: detect alloc/free size mismatch bugs.
65 * Prefix each allocations with a fixed-sized, aligned header and record
66 * the exact user-requested allocation size in it. When freeing, compare
67 * it with kmem_free's "size" argument.
68 */
69
70 /*
71 * KMEM_REDZONE: detect overrun bugs.
72 * Add a 2-byte pattern (allocate one more memory chunk if needed) at the
73 * end of each allocated buffer. Check this pattern on kmem_free.
74 *
75 * KMEM_POISON: detect modify-after-free bugs.
76 * Fill freed (in the sense of kmem_free) memory with a garbage pattern.
77 * Check the pattern on allocation.
78 *
79 * KMEM_GUARD
80 * A kernel with "option DEBUG" has "kmguard" debugging feature compiled
81 * in. See the comment in uvm/uvm_kmguard.c for what kind of bugs it tries
82 * to detect. Even if compiled in, it's disabled by default because it's
83 * very expensive. You can enable it on boot by:
84 * boot -d
85 * db> w kmem_guard_depth 0t30000
86 * db> c
87 *
88 * The default value of kmem_guard_depth is 0, which means disabled.
89 * It can be changed by KMEM_GUARD_DEPTH kernel config option.
90 */
91
92 #include <sys/cdefs.h>
93 __KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.59 2014/07/03 08:43:49 maxv Exp $");
94
95 #include <sys/param.h>
96 #include <sys/callback.h>
97 #include <sys/kmem.h>
98 #include <sys/pool.h>
99 #include <sys/debug.h>
100 #include <sys/lockdebug.h>
101 #include <sys/cpu.h>
102
103 #include <uvm/uvm_extern.h>
104 #include <uvm/uvm_map.h>
105 #include <uvm/uvm_kmguard.h>
106
107 #include <lib/libkern/libkern.h>
108
109 struct kmem_cache_info {
110 size_t kc_size;
111 const char * kc_name;
112 };
113
114 static const struct kmem_cache_info kmem_cache_sizes[] = {
115 { 8, "kmem-8" },
116 { 16, "kmem-16" },
117 { 24, "kmem-24" },
118 { 32, "kmem-32" },
119 { 40, "kmem-40" },
120 { 48, "kmem-48" },
121 { 56, "kmem-56" },
122 { 64, "kmem-64" },
123 { 80, "kmem-80" },
124 { 96, "kmem-96" },
125 { 112, "kmem-112" },
126 { 128, "kmem-128" },
127 { 160, "kmem-160" },
128 { 192, "kmem-192" },
129 { 224, "kmem-224" },
130 { 256, "kmem-256" },
131 { 320, "kmem-320" },
132 { 384, "kmem-384" },
133 { 448, "kmem-448" },
134 { 512, "kmem-512" },
135 { 768, "kmem-768" },
136 { 1024, "kmem-1024" },
137 { 0, NULL }
138 };
139
140 static const struct kmem_cache_info kmem_cache_big_sizes[] = {
141 { 2048, "kmem-2048" },
142 { 4096, "kmem-4096" },
143 { 8192, "kmem-8192" },
144 { 16384, "kmem-16384" },
145 { 0, NULL }
146 };
147
148 /*
149 * KMEM_ALIGN is the smallest guaranteed alignment and also the
150 * smallest allocateable quantum.
151 * Every cache size >= CACHE_LINE_SIZE gets CACHE_LINE_SIZE alignment.
152 */
153 #define KMEM_ALIGN 8
154 #define KMEM_SHIFT 3
155 #define KMEM_MAXSIZE 1024
156 #define KMEM_CACHE_COUNT (KMEM_MAXSIZE >> KMEM_SHIFT)
157
158 static pool_cache_t kmem_cache[KMEM_CACHE_COUNT] __cacheline_aligned;
159 static size_t kmem_cache_maxidx __read_mostly;
160
161 #define KMEM_BIG_ALIGN 2048
162 #define KMEM_BIG_SHIFT 11
163 #define KMEM_BIG_MAXSIZE 16384
164 #define KMEM_CACHE_BIG_COUNT (KMEM_BIG_MAXSIZE >> KMEM_BIG_SHIFT)
165
166 static pool_cache_t kmem_cache_big[KMEM_CACHE_BIG_COUNT] __cacheline_aligned;
167 static size_t kmem_cache_big_maxidx __read_mostly;
168
169 #if defined(DIAGNOSTIC) && defined(_HARDKERNEL)
170 #define KMEM_SIZE
171 #endif /* defined(DIAGNOSTIC) */
172
173 #if defined(DEBUG) && defined(_HARDKERNEL)
174 #define KMEM_POISON
175 #define KMEM_REDZONE
176 #define KMEM_GUARD
177 #endif /* defined(DEBUG) */
178
179 #if defined(KMEM_POISON)
180 static int kmem_poison_ctor(void *, void *, int);
181 static void kmem_poison_fill(void *, size_t);
182 static void kmem_poison_check(void *, size_t);
183 #else /* defined(KMEM_POISON) */
184 #define kmem_poison_fill(p, sz) /* nothing */
185 #define kmem_poison_check(p, sz) /* nothing */
186 #endif /* defined(KMEM_POISON) */
187
188 #if defined(KMEM_REDZONE)
189 #define REDZONE_SIZE 2
190 static void kmem_redzone_fill(void *, size_t);
191 static void kmem_redzone_check(void *, size_t);
192 #else /* defined(KMEM_REDZONE) */
193 #define REDZONE_SIZE 0
194 #define kmem_redzone_fill(p, sz) /* nothing */
195 #define kmem_redzone_check(p, sz) /* nothing */
196 #endif /* defined(KMEM_REDZONE) */
197
198 #if defined(KMEM_SIZE)
199 struct kmem_header {
200 size_t size;
201 } __aligned(KMEM_ALIGN);
202 #define SIZE_SIZE sizeof(struct kmem_header)
203 static void kmem_size_set(void *, size_t);
204 static void kmem_size_check(void *, size_t);
205 #else
206 #define SIZE_SIZE 0
207 #define kmem_size_set(p, sz) /* nothing */
208 #define kmem_size_check(p, sz) /* nothing */
209 #endif
210
211 #if defined(KMEM_GUARD)
212 #ifndef KMEM_GUARD_DEPTH
213 #define KMEM_GUARD_DEPTH 0
214 #endif
215 int kmem_guard_depth = KMEM_GUARD_DEPTH;
216 size_t kmem_guard_size;
217 static struct uvm_kmguard kmem_guard;
218 static void *kmem_freecheck;
219 #endif /* defined(KMEM_GUARD) */
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 + SIZE_SIZE;
246
247 #ifdef KMEM_REDZONE
248 if (size - requested_size < REDZONE_SIZE) {
249 /* If there isn't enough space in the padding, allocate
250 * one more memory chunk for the red zone. */
251 allocsz += kmem_roundup_size(REDZONE_SIZE);
252 }
253 #endif
254
255 if ((index = ((allocsz -1) >> KMEM_SHIFT))
256 < kmem_cache_maxidx) {
257 pc = kmem_cache[index];
258 } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
259 < kmem_cache_big_maxidx) {
260 pc = kmem_cache_big[index];
261 } else {
262 int ret = uvm_km_kmem_alloc(kmem_va_arena,
263 (vsize_t)round_page(size),
264 ((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP)
265 | VM_INSTANTFIT, (vmem_addr_t *)&p);
266 if (ret) {
267 return NULL;
268 }
269 FREECHECK_OUT(&kmem_freecheck, p);
270 return p;
271 }
272
273 p = pool_cache_get(pc, kmflags);
274
275 if (__predict_true(p != NULL)) {
276 kmem_poison_check(p, allocsz);
277 FREECHECK_OUT(&kmem_freecheck, p);
278 kmem_size_set(p, requested_size);
279 kmem_redzone_fill(p, requested_size + SIZE_SIZE);
280
281 return p + SIZE_SIZE;
282 }
283 return p;
284 }
285
286 /*
287 * kmem_intr_zalloc: allocate zeroed wired memory.
288 */
289
290 void *
291 kmem_intr_zalloc(size_t size, km_flag_t kmflags)
292 {
293 void *p;
294
295 p = kmem_intr_alloc(size, kmflags);
296 if (p != NULL) {
297 memset(p, 0, size);
298 }
299 return p;
300 }
301
302 /*
303 * kmem_intr_free: free wired memory allocated by kmem_alloc.
304 */
305
306 void
307 kmem_intr_free(void *p, size_t requested_size)
308 {
309 size_t allocsz, index;
310 size_t size;
311 pool_cache_t pc;
312
313 KASSERT(p != NULL);
314 KASSERT(requested_size > 0);
315
316 #ifdef KMEM_GUARD
317 if (requested_size <= kmem_guard_size) {
318 uvm_kmguard_free(&kmem_guard, requested_size, p);
319 return;
320 }
321 #endif
322
323 size = kmem_roundup_size(requested_size);
324 allocsz = size + SIZE_SIZE;
325
326 #ifdef KMEM_REDZONE
327 if (size - requested_size < REDZONE_SIZE) {
328 allocsz += kmem_roundup_size(REDZONE_SIZE);
329 }
330 #endif
331
332 if ((index = ((allocsz -1) >> KMEM_SHIFT))
333 < kmem_cache_maxidx) {
334 pc = kmem_cache[index];
335 } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
336 < kmem_cache_big_maxidx) {
337 pc = kmem_cache_big[index];
338 } else {
339 FREECHECK_IN(&kmem_freecheck, p);
340 uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
341 round_page(size));
342 return;
343 }
344
345 p = (uint8_t *)p - SIZE_SIZE;
346 kmem_size_check(p, requested_size);
347 kmem_redzone_check(p, requested_size + SIZE_SIZE);
348 FREECHECK_IN(&kmem_freecheck, p);
349 LOCKDEBUG_MEM_CHECK(p, size);
350 kmem_poison_fill(p, allocsz);
351
352 pool_cache_put(pc, p);
353 }
354
355 /* ---- kmem API */
356
357 /*
358 * kmem_alloc: allocate wired memory.
359 * => must not be called from interrupt context.
360 */
361
362 void *
363 kmem_alloc(size_t size, km_flag_t kmflags)
364 {
365
366 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
367 "kmem(9) should not be used from the interrupt context");
368 return kmem_intr_alloc(size, kmflags);
369 }
370
371 /*
372 * kmem_zalloc: allocate zeroed wired memory.
373 * => must not be called from interrupt context.
374 */
375
376 void *
377 kmem_zalloc(size_t size, km_flag_t kmflags)
378 {
379
380 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
381 "kmem(9) should not be used from the interrupt context");
382 return kmem_intr_zalloc(size, kmflags);
383 }
384
385 /*
386 * kmem_free: free wired memory allocated by kmem_alloc.
387 * => must not be called from interrupt context.
388 */
389
390 void
391 kmem_free(void *p, size_t size)
392 {
393
394 KASSERT(!cpu_intr_p());
395 KASSERT(!cpu_softintr_p());
396 kmem_intr_free(p, size);
397 }
398
399 static size_t
400 kmem_create_caches(const struct kmem_cache_info *array,
401 pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl)
402 {
403 size_t maxidx = 0;
404 size_t table_unit = (1 << shift);
405 size_t size = table_unit;
406 int i;
407
408 for (i = 0; array[i].kc_size != 0 ; i++) {
409 const char *name = array[i].kc_name;
410 size_t cache_size = array[i].kc_size;
411 struct pool_allocator *pa;
412 int flags = PR_NOALIGN;
413 pool_cache_t pc;
414 size_t align;
415
416 if ((cache_size & (CACHE_LINE_SIZE - 1)) == 0)
417 align = CACHE_LINE_SIZE;
418 else if ((cache_size & (PAGE_SIZE - 1)) == 0)
419 align = PAGE_SIZE;
420 else
421 align = KMEM_ALIGN;
422
423 if (cache_size < CACHE_LINE_SIZE)
424 flags |= PR_NOTOUCH;
425
426 /* check if we reached the requested size */
427 if (cache_size > maxsize || cache_size > PAGE_SIZE) {
428 break;
429 }
430 if ((cache_size >> shift) > maxidx) {
431 maxidx = cache_size >> shift;
432 }
433
434 if ((cache_size >> shift) > maxidx) {
435 maxidx = cache_size >> shift;
436 }
437
438 pa = &pool_allocator_kmem;
439 #if defined(KMEM_POISON)
440 pc = pool_cache_init(cache_size, align, 0, flags,
441 name, pa, ipl, kmem_poison_ctor,
442 NULL, (void *)cache_size);
443 #else /* defined(KMEM_POISON) */
444 pc = pool_cache_init(cache_size, align, 0, flags,
445 name, pa, ipl, NULL, NULL, NULL);
446 #endif /* defined(KMEM_POISON) */
447
448 while (size <= cache_size) {
449 alloc_table[(size - 1) >> shift] = pc;
450 size += table_unit;
451 }
452 }
453 return maxidx;
454 }
455
456 void
457 kmem_init(void)
458 {
459
460 #ifdef KMEM_GUARD
461 uvm_kmguard_init(&kmem_guard, &kmem_guard_depth, &kmem_guard_size,
462 kmem_va_arena);
463 #endif
464 kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes,
465 kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM);
466 kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes,
467 kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM);
468 }
469
470 size_t
471 kmem_roundup_size(size_t size)
472 {
473
474 return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
475 }
476
477 /* ------------------ DEBUG / DIAGNOSTIC ------------------ */
478
479 #if defined(KMEM_POISON) || defined(KMEM_REDZONE)
480 #if defined(_LP64)
481 #define PRIME 0x9e37fffffffc0000UL
482 #else /* defined(_LP64) */
483 #define PRIME 0x9e3779b1
484 #endif /* defined(_LP64) */
485
486 static inline uint8_t
487 kmem_pattern_generate(const void *p)
488 {
489 return (uint8_t)(((uintptr_t)p) * PRIME
490 >> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT);
491 }
492 #endif /* defined(KMEM_POISON) || defined(KMEM_REDZONE) */
493
494 #if defined(KMEM_POISON)
495 static int
496 kmem_poison_ctor(void *arg, void *obj, int flag)
497 {
498 size_t sz = (size_t)arg;
499
500 kmem_poison_fill(obj, sz);
501
502 return 0;
503 }
504
505 static void
506 kmem_poison_fill(void *p, size_t sz)
507 {
508 uint8_t *cp;
509 const uint8_t *ep;
510
511 cp = p;
512 ep = cp + sz;
513 while (cp < ep) {
514 *cp = kmem_pattern_generate(cp);
515 cp++;
516 }
517 }
518
519 static void
520 kmem_poison_check(void *p, size_t sz)
521 {
522 uint8_t *cp;
523 const uint8_t *ep;
524
525 cp = p;
526 ep = cp + sz;
527 while (cp < ep) {
528 const uint8_t expected = kmem_pattern_generate(cp);
529
530 if (*cp != expected) {
531 panic("%s: %p: 0x%02x != 0x%02x\n",
532 __func__, cp, *cp, expected);
533 }
534 cp++;
535 }
536 }
537 #endif /* defined(KMEM_POISON) */
538
539 #if defined(KMEM_SIZE)
540 static void
541 kmem_size_set(void *p, size_t sz)
542 {
543 struct kmem_header *hd;
544 hd = (struct kmem_header *)p;
545 hd->size = sz;
546 }
547
548 static void
549 kmem_size_check(void *p, size_t sz)
550 {
551 struct kmem_header *hd;
552 size_t hsz;
553
554 hd = (struct kmem_header *)p;
555 hsz = hd->size;
556
557 if (hsz != sz) {
558 panic("kmem_free(%p, %zu) != allocated size %zu",
559 (const uint8_t *)p + SIZE_SIZE, sz, hsz);
560 }
561 }
562 #endif /* defined(KMEM_SIZE) */
563
564 #if defined(KMEM_REDZONE)
565 #define STATIC_BYTE 0xFE
566 CTASSERT(REDZONE_SIZE > 1);
567 static void
568 kmem_redzone_fill(void *p, size_t sz)
569 {
570 uint8_t *cp, pat;
571 const uint8_t *ep;
572
573 cp = (uint8_t *)p + sz;
574 ep = cp + REDZONE_SIZE;
575
576 /*
577 * We really don't want the first byte of the red zone to be '\0';
578 * an off-by-one in a string may not be properly detected.
579 */
580 pat = kmem_pattern_generate(cp);
581 *cp = (pat == '\0') ? STATIC_BYTE: pat;
582 cp++;
583
584 while (cp < ep) {
585 *cp = kmem_pattern_generate(cp);
586 cp++;
587 }
588 }
589
590 static void
591 kmem_redzone_check(void *p, size_t sz)
592 {
593 uint8_t *cp, pat, expected;
594 const uint8_t *ep;
595
596 cp = (uint8_t *)p + sz;
597 ep = cp + REDZONE_SIZE;
598
599 pat = kmem_pattern_generate(cp);
600 expected = (pat == '\0') ? STATIC_BYTE: pat;
601 if (expected != *cp) {
602 panic("%s: %p: 0x%02x != 0x%02x\n",
603 __func__, cp, *cp, expected);
604 }
605 cp++;
606
607 while (cp < ep) {
608 expected = kmem_pattern_generate(cp);
609 if (*cp != expected) {
610 panic("%s: %p: 0x%02x != 0x%02x\n",
611 __func__, cp, *cp, expected);
612 }
613 cp++;
614 }
615 }
616 #endif /* defined(KMEM_REDZONE) */
617
618
619 /*
620 * Used to dynamically allocate string with kmem accordingly to format.
621 */
622 char *
623 kmem_asprintf(const char *fmt, ...)
624 {
625 int size __diagused, len;
626 va_list va;
627 char *str;
628
629 va_start(va, fmt);
630 len = vsnprintf(NULL, 0, fmt, va);
631 va_end(va);
632
633 str = kmem_alloc(len + 1, KM_SLEEP);
634
635 va_start(va, fmt);
636 size = vsnprintf(str, len + 1, fmt, va);
637 va_end(va);
638
639 KASSERT(size == len);
640
641 return str;
642 }
643