Home | History | Annotate | Line # | Download | only in kern
subr_kmem.c revision 1.54
      1 /*	$NetBSD: subr_kmem.c,v 1.54 2014/06/24 07:28:23 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  *	Add a 2-byte pattern (allocate some more bytes if needed) at the end
     76  *	of each allocated buffer. Check this pattern 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.54 2014/06/24 07:28:23 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	2
    203 static void kmem_redzone_fill(void *p, size_t sz);
    204 static void kmem_redzone_check(void *p, size_t sz);
    205 #else /* defined(KMEM_REDZONE) */
    206 #define	REDZONE_SIZE	0
    207 #define	kmem_redzone_fill(p, sz)		/* nothing */
    208 #define	kmem_redzone_check(p, sz)	/* nothing */
    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 #if defined(KMEM_GUARD)
    222 #ifndef KMEM_GUARD_DEPTH
    223 #define KMEM_GUARD_DEPTH 0
    224 #endif
    225 int kmem_guard_depth = KMEM_GUARD_DEPTH;
    226 size_t kmem_guard_size;
    227 static struct uvm_kmguard kmem_guard;
    228 static void *kmem_freecheck;
    229 #endif /* defined(KMEM_GUARD) */
    230 
    231 CTASSERT(KM_SLEEP == PR_WAITOK);
    232 CTASSERT(KM_NOSLEEP == PR_NOWAIT);
    233 
    234 /*
    235  * kmem_intr_alloc: allocate wired memory.
    236  */
    237 
    238 void *
    239 kmem_intr_alloc(size_t requested_size, km_flag_t kmflags)
    240 {
    241 	size_t allocsz, index;
    242 	size_t size;
    243 	pool_cache_t pc;
    244 	uint8_t *p;
    245 
    246 	KASSERT(requested_size > 0);
    247 
    248 #ifdef KMEM_GUARD
    249 	if (requested_size <= kmem_guard_size) {
    250 		return uvm_kmguard_alloc(&kmem_guard, requested_size,
    251 		    (kmflags & KM_SLEEP) != 0);
    252 	}
    253 #endif
    254 	size = kmem_roundup_size(requested_size);
    255 	allocsz = size + SIZE_SIZE;
    256 
    257 #ifdef KMEM_REDZONE
    258 	if (size - requested_size < REDZONE_SIZE) {
    259 		/* If there isn't enough space in the page padding,
    260 		 * allocate two more bytes for the red zone. */
    261 		allocsz += REDZONE_SIZE;
    262 	}
    263 #endif
    264 
    265 	if ((index = ((allocsz -1) >> KMEM_SHIFT))
    266 	    < kmem_cache_maxidx) {
    267 		pc = kmem_cache[index];
    268 	} else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
    269             < kmem_cache_big_maxidx) {
    270 		pc = kmem_cache_big[index];
    271 	} else {
    272 		int ret = uvm_km_kmem_alloc(kmem_va_arena,
    273 		    (vsize_t)round_page(size),
    274 		    ((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP)
    275 		     | VM_INSTANTFIT, (vmem_addr_t *)&p);
    276 		if (ret) {
    277 			return NULL;
    278 		}
    279 		FREECHECK_OUT(&kmem_freecheck, p);
    280 		return p;
    281 	}
    282 
    283 	p = pool_cache_get(pc, kmflags);
    284 
    285 	if (__predict_true(p != NULL)) {
    286 		kmem_poison_check(p, size);
    287 		FREECHECK_OUT(&kmem_freecheck, p);
    288 		kmem_size_set(p, requested_size);
    289 		kmem_redzone_fill(p, requested_size + SIZE_SIZE);
    290 
    291 		return p + SIZE_SIZE;
    292 	}
    293 	return p;
    294 }
    295 
    296 /*
    297  * kmem_intr_zalloc: allocate zeroed wired memory.
    298  */
    299 
    300 void *
    301 kmem_intr_zalloc(size_t size, km_flag_t kmflags)
    302 {
    303 	void *p;
    304 
    305 	p = kmem_intr_alloc(size, kmflags);
    306 	if (p != NULL) {
    307 		memset(p, 0, size);
    308 	}
    309 	return p;
    310 }
    311 
    312 /*
    313  * kmem_intr_free: free wired memory allocated by kmem_alloc.
    314  */
    315 
    316 void
    317 kmem_intr_free(void *p, size_t requested_size)
    318 {
    319 	size_t allocsz, index;
    320 	size_t size;
    321 	pool_cache_t pc;
    322 
    323 	KASSERT(p != NULL);
    324 	KASSERT(requested_size > 0);
    325 
    326 #ifdef KMEM_GUARD
    327 	if (requested_size <= kmem_guard_size) {
    328 		uvm_kmguard_free(&kmem_guard, requested_size, p);
    329 		return;
    330 	}
    331 #endif
    332 
    333 	size = kmem_roundup_size(requested_size);
    334 	allocsz = size + SIZE_SIZE;
    335 
    336 #ifdef KMEM_REDZONE
    337 	if (size - requested_size < REDZONE_SIZE) {
    338 		allocsz += REDZONE_SIZE;
    339 	}
    340 #endif
    341 
    342 	if ((index = ((allocsz -1) >> KMEM_SHIFT))
    343 	    < kmem_cache_maxidx) {
    344 		pc = kmem_cache[index];
    345 	} else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
    346             < kmem_cache_big_maxidx) {
    347 		pc = kmem_cache_big[index];
    348 	} else {
    349 		FREECHECK_IN(&kmem_freecheck, p);
    350 		uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
    351 		    round_page(size));
    352 		return;
    353 	}
    354 
    355 	p = (uint8_t *)p - SIZE_SIZE;
    356 	kmem_size_check(p, requested_size);
    357 	kmem_redzone_check(p, requested_size + SIZE_SIZE);
    358 	FREECHECK_IN(&kmem_freecheck, p);
    359 	LOCKDEBUG_MEM_CHECK(p, size);
    360 	kmem_poison_fill(p, allocsz);
    361 
    362 	pool_cache_put(pc, p);
    363 }
    364 
    365 /* ---- kmem API */
    366 
    367 /*
    368  * kmem_alloc: allocate wired memory.
    369  * => must not be called from interrupt context.
    370  */
    371 
    372 void *
    373 kmem_alloc(size_t size, km_flag_t kmflags)
    374 {
    375 
    376 	KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
    377 	    "kmem(9) should not be used from the interrupt context");
    378 	return kmem_intr_alloc(size, kmflags);
    379 }
    380 
    381 /*
    382  * kmem_zalloc: allocate zeroed wired memory.
    383  * => must not be called from interrupt context.
    384  */
    385 
    386 void *
    387 kmem_zalloc(size_t size, km_flag_t kmflags)
    388 {
    389 
    390 	KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
    391 	    "kmem(9) should not be used from the interrupt context");
    392 	return kmem_intr_zalloc(size, kmflags);
    393 }
    394 
    395 /*
    396  * kmem_free: free wired memory allocated by kmem_alloc.
    397  * => must not be called from interrupt context.
    398  */
    399 
    400 void
    401 kmem_free(void *p, size_t size)
    402 {
    403 
    404 	KASSERT(!cpu_intr_p());
    405 	KASSERT(!cpu_softintr_p());
    406 	kmem_intr_free(p, size);
    407 }
    408 
    409 static size_t
    410 kmem_create_caches(const struct kmem_cache_info *array,
    411     pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl)
    412 {
    413 	size_t maxidx = 0;
    414 	size_t table_unit = (1 << shift);
    415 	size_t size = table_unit;
    416 	int i;
    417 
    418 	for (i = 0; array[i].kc_size != 0 ; i++) {
    419 		const char *name = array[i].kc_name;
    420 		size_t cache_size = array[i].kc_size;
    421 		struct pool_allocator *pa;
    422 		int flags = PR_NOALIGN;
    423 		pool_cache_t pc;
    424 		size_t align;
    425 
    426 		if ((cache_size & (CACHE_LINE_SIZE - 1)) == 0)
    427 			align = CACHE_LINE_SIZE;
    428 		else if ((cache_size & (PAGE_SIZE - 1)) == 0)
    429 			align = PAGE_SIZE;
    430 		else
    431 			align = KMEM_ALIGN;
    432 
    433 		if (cache_size < CACHE_LINE_SIZE)
    434 			flags |= PR_NOTOUCH;
    435 
    436 		/* check if we reached the requested size */
    437 		if (cache_size > maxsize || cache_size > PAGE_SIZE) {
    438 			break;
    439 		}
    440 		if ((cache_size >> shift) > maxidx) {
    441 			maxidx = cache_size >> shift;
    442 		}
    443 
    444 		if ((cache_size >> shift) > maxidx) {
    445 			maxidx = cache_size >> shift;
    446 		}
    447 
    448 		pa = &pool_allocator_kmem;
    449 #if defined(KMEM_POISON)
    450 		pc = pool_cache_init(cache_size, align, 0, flags,
    451 		    name, pa, ipl, kmem_poison_ctor,
    452 		    NULL, (void *)cache_size);
    453 #else /* defined(KMEM_POISON) */
    454 		pc = pool_cache_init(cache_size, align, 0, flags,
    455 		    name, pa, ipl, NULL, NULL, NULL);
    456 #endif /* defined(KMEM_POISON) */
    457 
    458 		while (size <= cache_size) {
    459 			alloc_table[(size - 1) >> shift] = pc;
    460 			size += table_unit;
    461 		}
    462 	}
    463 	return maxidx;
    464 }
    465 
    466 void
    467 kmem_init(void)
    468 {
    469 
    470 #ifdef KMEM_GUARD
    471 	uvm_kmguard_init(&kmem_guard, &kmem_guard_depth, &kmem_guard_size,
    472 	    kmem_va_arena);
    473 #endif
    474 	kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes,
    475 	    kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM);
    476        	kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes,
    477 	    kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM);
    478 }
    479 
    480 size_t
    481 kmem_roundup_size(size_t size)
    482 {
    483 
    484 	return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
    485 }
    486 
    487 /* ------------------ DEBUG / DIAGNOSTIC ------------------ */
    488 
    489 #if defined(KMEM_POISON) || defined(KMEM_REDZONE)
    490 #if defined(_LP64)
    491 #define PRIME 0x9e37fffffffc0000UL
    492 #else /* defined(_LP64) */
    493 #define PRIME 0x9e3779b1
    494 #endif /* defined(_LP64) */
    495 #endif /* defined(KMEM_POISON) || defined(KMEM_REDZONE) */
    496 
    497 #if defined(KMEM_POISON)
    498 static inline uint8_t
    499 kmem_poison_pattern(const void *p)
    500 {
    501 	return (uint8_t)(((uintptr_t)p) * PRIME
    502 	   >> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT);
    503 }
    504 
    505 static int
    506 kmem_poison_ctor(void *arg, void *obj, int flag)
    507 {
    508 	size_t sz = (size_t)arg;
    509 
    510 	kmem_poison_fill(obj, sz);
    511 
    512 	return 0;
    513 }
    514 
    515 static void
    516 kmem_poison_fill(void *p, size_t sz)
    517 {
    518 	uint8_t *cp;
    519 	const uint8_t *ep;
    520 
    521 	cp = p;
    522 	ep = cp + sz;
    523 	while (cp < ep) {
    524 		*cp = kmem_poison_pattern(cp);
    525 		cp++;
    526 	}
    527 }
    528 
    529 static void
    530 kmem_poison_check(void *p, size_t sz)
    531 {
    532 	uint8_t *cp;
    533 	const uint8_t *ep;
    534 
    535 	cp = p;
    536 	ep = cp + sz;
    537 	while (cp < ep) {
    538 		const uint8_t expected = kmem_poison_pattern(cp);
    539 
    540 		if (*cp != expected) {
    541 			panic("%s: %p: 0x%02x != 0x%02x\n",
    542 			   __func__, cp, *cp, expected);
    543 		}
    544 		cp++;
    545 	}
    546 }
    547 #endif /* defined(KMEM_POISON) */
    548 
    549 #if defined(KMEM_SIZE)
    550 static void
    551 kmem_size_set(void *p, size_t sz)
    552 {
    553 	memcpy(p, &sz, sizeof(sz));
    554 }
    555 
    556 static void
    557 kmem_size_check(void *p, size_t sz)
    558 {
    559 	size_t psz;
    560 
    561 	memcpy(&psz, p, sizeof(psz));
    562 	if (psz != sz) {
    563 		panic("kmem_free(%p, %zu) != allocated size %zu",
    564 		    (const uint8_t *)p + SIZE_SIZE, sz, psz);
    565 	}
    566 }
    567 #endif /* defined(KMEM_SIZE) */
    568 
    569 #if defined(KMEM_REDZONE)
    570 static inline uint8_t
    571 kmem_redzone_pattern(const void *p)
    572 {
    573 	return (uint8_t)(((uintptr_t)p) * PRIME
    574 	   >> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT);
    575 }
    576 
    577 static void
    578 kmem_redzone_fill(void *p, size_t sz)
    579 {
    580 	uint8_t *cp;
    581 	const uint8_t *ep;
    582 
    583 	cp = (uint8_t *)p + sz;
    584 	ep = cp + REDZONE_SIZE;
    585 	while (cp < ep) {
    586 		*cp = kmem_redzone_pattern(cp);
    587 		cp++;
    588 	}
    589 }
    590 
    591 static void
    592 kmem_redzone_check(void *p, size_t sz)
    593 {
    594 	uint8_t *cp;
    595 	const uint8_t *ep;
    596 
    597 	cp = (uint8_t *)p + sz;
    598 	ep = (uint8_t *)p + sz + REDZONE_SIZE;
    599 	while (cp < ep) {
    600 		const uint8_t expected = kmem_redzone_pattern(cp);
    601 
    602 		if (*cp != expected) {
    603 			panic("%s: %p: 0x%02x != 0x%02x\n",
    604 			   __func__, cp, *cp, expected);
    605 		}
    606 		cp++;
    607 	}
    608 }
    609 #endif /* defined(KMEM_REDZONE) */
    610 
    611 
    612 /*
    613  * Used to dynamically allocate string with kmem accordingly to format.
    614  */
    615 char *
    616 kmem_asprintf(const char *fmt, ...)
    617 {
    618 	int size __diagused, len;
    619 	va_list va;
    620 	char *str;
    621 
    622 	va_start(va, fmt);
    623 	len = vsnprintf(NULL, 0, fmt, va);
    624 	va_end(va);
    625 
    626 	str = kmem_alloc(len + 1, KM_SLEEP);
    627 
    628 	va_start(va, fmt);
    629 	size = vsnprintf(str, len + 1, fmt, va);
    630 	va_end(va);
    631 
    632 	KASSERT(size == len);
    633 
    634 	return str;
    635 }
    636