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