Home | History | Annotate | Line # | Download | only in kern
subr_kmem.c revision 1.86
      1 /*	$NetBSD: subr_kmem.c,v 1.86 2022/05/30 21:42:02 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009-2020 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 and Maxime Villard.
      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  *	Append to each allocation a fixed-sized footer and record the exact
     66  *	user-requested allocation size in it.  When freeing, compare it with
     67  *	kmem_free's "size" argument.
     68  *
     69  * This option is enabled on DIAGNOSTIC.
     70  *
     71  *  |CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK| |
     72  *  +-----+-----+-----+-----+-----+-----+-----+-----+-----+-+
     73  *  |     |     |     |     |     |     |     |     |/////|U|
     74  *  |     |     |     |     |     |     |     |     |/HSZ/|U|
     75  *  |     |     |     |     |     |     |     |     |/////|U|
     76  *  +-----+-----+-----+-----+-----+-----+-----+-----+-----+-+
     77  *  | Buffer usable by the caller (requested size)  |Size |Unused
     78  */
     79 
     80 #include <sys/cdefs.h>
     81 __KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.86 2022/05/30 21:42:02 mrg Exp $");
     82 
     83 #ifdef _KERNEL_OPT
     84 #include "opt_kmem.h"
     85 #endif
     86 
     87 #include <sys/param.h>
     88 #include <sys/callback.h>
     89 #include <sys/kmem.h>
     90 #include <sys/pool.h>
     91 #include <sys/debug.h>
     92 #include <sys/lockdebug.h>
     93 #include <sys/cpu.h>
     94 #include <sys/asan.h>
     95 #include <sys/msan.h>
     96 #include <sys/sdt.h>
     97 
     98 #include <uvm/uvm_extern.h>
     99 #include <uvm/uvm_map.h>
    100 
    101 #include <lib/libkern/libkern.h>
    102 
    103 struct kmem_cache_info {
    104 	size_t		kc_size;
    105 	const char *	kc_name;
    106 #ifdef KDTRACE_HOOKS
    107 	const id_t	*kc_alloc_probe_id;
    108 	const id_t	*kc_free_probe_id;
    109 #endif
    110 };
    111 
    112 #define	KMEM_CACHE_SIZES(F)						      \
    113 	F(8, kmem-00008, kmem__00008)					      \
    114 	F(16, kmem-00016, kmem__00016)					      \
    115 	F(24, kmem-00024, kmem__00024)					      \
    116 	F(32, kmem-00032, kmem__00032)					      \
    117 	F(40, kmem-00040, kmem__00040)					      \
    118 	F(48, kmem-00048, kmem__00048)					      \
    119 	F(56, kmem-00056, kmem__00056)					      \
    120 	F(64, kmem-00064, kmem__00064)					      \
    121 	F(80, kmem-00080, kmem__00080)					      \
    122 	F(96, kmem-00096, kmem__00096)					      \
    123 	F(112, kmem-00112, kmem__00112)					      \
    124 	F(128, kmem-00128, kmem__00128)					      \
    125 	F(160, kmem-00160, kmem__00160)					      \
    126 	F(192, kmem-00192, kmem__00192)					      \
    127 	F(224, kmem-00224, kmem__00224)					      \
    128 	F(256, kmem-00256, kmem__00256)					      \
    129 	F(320, kmem-00320, kmem__00320)					      \
    130 	F(384, kmem-00384, kmem__00384)					      \
    131 	F(448, kmem-00448, kmem__00448)					      \
    132 	F(512, kmem-00512, kmem__00512)					      \
    133 	F(768, kmem-00768, kmem__00768)					      \
    134 	F(1024, kmem-01024, kmem__01024)				      \
    135 	/* end of KMEM_CACHE_SIZES */
    136 
    137 #define	KMEM_CACHE_BIG_SIZES(F)						      \
    138 	F(2048, kmem-02048, kmem__02048)				      \
    139 	F(4096, kmem-04096, kmem__04096)				      \
    140 	F(8192, kmem-08192, kmem__08192)				      \
    141 	F(16384, kmem-16384, kmem__16384)				      \
    142 	/* end of KMEM_CACHE_BIG_SIZES */
    143 
    144 /* sdt:kmem:alloc:kmem-* probes */
    145 #define	F(SZ, NAME, PROBENAME)						      \
    146 	SDT_PROBE_DEFINE4(sdt, kmem, alloc, PROBENAME,			      \
    147 	    "void *"/*ptr*/,						      \
    148 	    "size_t"/*requested_size*/,					      \
    149 	    "size_t"/*allocated_size*/,					      \
    150 	    "km_flag_t"/*kmflags*/);
    151 KMEM_CACHE_SIZES(F);
    152 KMEM_CACHE_BIG_SIZES(F);
    153 #undef	F
    154 
    155 /* sdt:kmem:free:kmem-* probes */
    156 #define	F(SZ, NAME, PROBENAME)						      \
    157 	SDT_PROBE_DEFINE3(sdt, kmem, free, PROBENAME,			      \
    158 	    "void *"/*ptr*/,						      \
    159 	    "size_t"/*requested_size*/,					      \
    160 	    "size_t"/*allocated_size*/);
    161 KMEM_CACHE_SIZES(F);
    162 KMEM_CACHE_BIG_SIZES(F);
    163 #undef	F
    164 
    165 /* sdt:kmem:alloc:large, sdt:kmem:free:large probes */
    166 SDT_PROBE_DEFINE4(sdt, kmem, alloc, large,
    167     "void *"/*ptr*/,
    168     "size_t"/*requested_size*/,
    169     "size_t"/*allocated_size*/,
    170     "km_flag_t"/*kmflags*/);
    171 SDT_PROBE_DEFINE3(sdt, kmem, free, large,
    172     "void *"/*ptr*/,
    173     "size_t"/*requested_size*/,
    174     "size_t"/*allocated_size*/);
    175 
    176 #ifdef KDTRACE_HOOKS
    177 #define	F(SZ, NAME, PROBENAME)						      \
    178 	{ SZ, #NAME,							      \
    179 	  &sdt_sdt_kmem_alloc_##PROBENAME->id,				      \
    180 	  &sdt_sdt_kmem_free_##PROBENAME->id },
    181 #else
    182 #define	F(SZ, NAME, PROBENAME)	{ SZ, #NAME },
    183 #endif
    184 
    185 static const struct kmem_cache_info kmem_cache_sizes[] = {
    186 	KMEM_CACHE_SIZES(F)
    187 #ifndef KDTRACE_HOOKS
    188 	{ 0, NULL }
    189 #endif
    190 };
    191 
    192 static const struct kmem_cache_info kmem_cache_big_sizes[] = {
    193 	KMEM_CACHE_BIG_SIZES(F)
    194 #ifndef KDTRACE_HOOKS
    195 	{ 0, NULL }
    196 #endif
    197 };
    198 
    199 #undef	F
    200 
    201 /*
    202  * KMEM_ALIGN is the smallest guaranteed alignment and also the
    203  * smallest allocateable quantum.
    204  * Every cache size >= CACHE_LINE_SIZE gets CACHE_LINE_SIZE alignment.
    205  */
    206 #define	KMEM_ALIGN		8
    207 #define	KMEM_SHIFT		3
    208 #define	KMEM_MAXSIZE		1024
    209 #define	KMEM_CACHE_COUNT	(KMEM_MAXSIZE >> KMEM_SHIFT)
    210 
    211 static pool_cache_t kmem_cache[KMEM_CACHE_COUNT] __cacheline_aligned;
    212 static size_t kmem_cache_maxidx __read_mostly;
    213 
    214 #define	KMEM_BIG_ALIGN		2048
    215 #define	KMEM_BIG_SHIFT		11
    216 #define	KMEM_BIG_MAXSIZE	16384
    217 #define	KMEM_CACHE_BIG_COUNT	(KMEM_BIG_MAXSIZE >> KMEM_BIG_SHIFT)
    218 
    219 static pool_cache_t kmem_cache_big[KMEM_CACHE_BIG_COUNT] __cacheline_aligned;
    220 static size_t kmem_cache_big_maxidx __read_mostly;
    221 
    222 #if defined(DIAGNOSTIC) && defined(_HARDKERNEL)
    223 #define	KMEM_SIZE
    224 #endif
    225 
    226 #if defined(DEBUG) && defined(_HARDKERNEL)
    227 static void *kmem_freecheck;
    228 #endif
    229 
    230 #if defined(KMEM_SIZE)
    231 #define	SIZE_SIZE	sizeof(size_t)
    232 static void kmem_size_set(void *, size_t);
    233 static void kmem_size_check(void *, size_t);
    234 #else
    235 #define	SIZE_SIZE	0
    236 #define	kmem_size_set(p, sz)	/* nothing */
    237 #define	kmem_size_check(p, sz)	/* nothing */
    238 #endif
    239 
    240 #ifndef KDTRACE_HOOKS
    241 
    242 static const id_t **const kmem_cache_alloc_probe_id = NULL;
    243 static const id_t **const kmem_cache_big_alloc_probe_id = NULL;
    244 static const id_t **const kmem_cache_free_probe_id = NULL;
    245 static const id_t **const kmem_cache_big_free_probe_id = NULL;
    246 
    247 #define	KMEM_CACHE_PROBE(ARRAY, INDEX, PTR, REQSIZE, ALLOCSIZE, FLAGS)	      \
    248 	__nothing
    249 
    250 #else
    251 
    252 static const id_t *kmem_cache_alloc_probe_id[KMEM_CACHE_COUNT];
    253 static const id_t *kmem_cache_big_alloc_probe_id[KMEM_CACHE_COUNT];
    254 static const id_t *kmem_cache_free_probe_id[KMEM_CACHE_COUNT];
    255 static const id_t *kmem_cache_big_free_probe_id[KMEM_CACHE_COUNT];
    256 
    257 #define	KMEM_CACHE_PROBE(ARRAY, INDEX, PTR, REQSIZE, ALLOCSIZE, FLAGS) do     \
    258 {									      \
    259 	id_t id;							      \
    260 									      \
    261 	KDASSERT((INDEX) < __arraycount(ARRAY));			      \
    262 	if (__predict_false((id = *(ARRAY)[INDEX]) != 0)) {		      \
    263 		(*sdt_probe_func)(id,					      \
    264 		    (uintptr_t)(PTR),					      \
    265 		    (uintptr_t)(REQSIZE),				      \
    266 		    (uintptr_t)(ALLOCSIZE),				      \
    267 		    (uintptr_t)(FLAGS),					      \
    268 		    (uintptr_t)0);					      \
    269 	}								      \
    270 } while (0)
    271 
    272 #endif	/* KDTRACE_HOOKS */
    273 
    274 #define	KMEM_CACHE_ALLOC_PROBE(I, P, RS, AS, F)				      \
    275 	KMEM_CACHE_PROBE(kmem_cache_alloc_probe_id, I, P, RS, AS, F)
    276 #define	KMEM_CACHE_BIG_ALLOC_PROBE(I, P, RS, AS, F)			      \
    277 	KMEM_CACHE_PROBE(kmem_cache_big_alloc_probe_id, I, P, RS, AS, F)
    278 #define	KMEM_CACHE_FREE_PROBE(I, P, RS, AS)				      \
    279 	KMEM_CACHE_PROBE(kmem_cache_free_probe_id, I, P, RS, AS, 0)
    280 #define	KMEM_CACHE_BIG_FREE_PROBE(I, P, RS, AS)				      \
    281 	KMEM_CACHE_PROBE(kmem_cache_big_free_probe_id, I, P, RS, AS, 0)
    282 
    283 CTASSERT(KM_SLEEP == PR_WAITOK);
    284 CTASSERT(KM_NOSLEEP == PR_NOWAIT);
    285 
    286 /*
    287  * kmem_intr_alloc: allocate wired memory.
    288  */
    289 void *
    290 kmem_intr_alloc(size_t requested_size, km_flag_t kmflags)
    291 {
    292 #ifdef KASAN
    293 	const size_t origsize = requested_size;
    294 #endif
    295 	size_t allocsz, index;
    296 	size_t size;
    297 	pool_cache_t pc;
    298 	uint8_t *p;
    299 
    300 	KASSERT(requested_size > 0);
    301 
    302 	KASSERT((kmflags & KM_SLEEP) || (kmflags & KM_NOSLEEP));
    303 	KASSERT(!(kmflags & KM_SLEEP) || !(kmflags & KM_NOSLEEP));
    304 
    305 	kasan_add_redzone(&requested_size);
    306 	size = kmem_roundup_size(requested_size);
    307 	allocsz = size + SIZE_SIZE;
    308 
    309 	if ((index = ((allocsz - 1) >> KMEM_SHIFT))
    310 	    < kmem_cache_maxidx) {
    311 		pc = kmem_cache[index];
    312 		p = pool_cache_get(pc, kmflags);
    313 		KMEM_CACHE_ALLOC_PROBE(index,
    314 		    p, requested_size, allocsz, kmflags);
    315 	} else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
    316 	    < kmem_cache_big_maxidx) {
    317 		pc = kmem_cache_big[index];
    318 		p = pool_cache_get(pc, kmflags);
    319 		KMEM_CACHE_BIG_ALLOC_PROBE(index,
    320 		    p, requested_size, allocsz, kmflags);
    321 	} else {
    322 		int ret = uvm_km_kmem_alloc(kmem_va_arena,
    323 		    (vsize_t)round_page(size),
    324 		    ((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP)
    325 		     | VM_INSTANTFIT, (vmem_addr_t *)&p);
    326 		SDT_PROBE4(sdt, kmem, alloc, large,
    327 		    ret ? NULL : p, requested_size, round_page(size), kmflags);
    328 		if (ret) {
    329 			return NULL;
    330 		}
    331 		FREECHECK_OUT(&kmem_freecheck, p);
    332 		return p;
    333 	}
    334 
    335 	if (__predict_true(p != NULL)) {
    336 		FREECHECK_OUT(&kmem_freecheck, p);
    337 		kmem_size_set(p, requested_size);
    338 		kasan_mark(p, origsize, size, KASAN_KMEM_REDZONE);
    339 		return p;
    340 	}
    341 	return p;
    342 }
    343 
    344 /*
    345  * kmem_intr_zalloc: allocate zeroed wired memory.
    346  */
    347 void *
    348 kmem_intr_zalloc(size_t size, km_flag_t kmflags)
    349 {
    350 	void *p;
    351 
    352 	p = kmem_intr_alloc(size, kmflags);
    353 	if (p != NULL) {
    354 		memset(p, 0, size);
    355 	}
    356 	return p;
    357 }
    358 
    359 /*
    360  * kmem_intr_free: free wired memory allocated by kmem_alloc.
    361  */
    362 void
    363 kmem_intr_free(void *p, size_t requested_size)
    364 {
    365 	size_t allocsz, index;
    366 	size_t size;
    367 	pool_cache_t pc;
    368 
    369 	KASSERT(p != NULL);
    370 	KASSERTMSG(requested_size > 0, "kmem_intr_free(%p, 0)", p);
    371 
    372 	kasan_add_redzone(&requested_size);
    373 	size = kmem_roundup_size(requested_size);
    374 	allocsz = size + SIZE_SIZE;
    375 
    376 	if ((index = ((allocsz - 1) >> KMEM_SHIFT))
    377 	    < kmem_cache_maxidx) {
    378 		KMEM_CACHE_FREE_PROBE(index, p, requested_size, allocsz);
    379 		pc = kmem_cache[index];
    380 	} else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
    381 	    < kmem_cache_big_maxidx) {
    382 		KMEM_CACHE_BIG_FREE_PROBE(index, p, requested_size, allocsz);
    383 		pc = kmem_cache_big[index];
    384 	} else {
    385 		FREECHECK_IN(&kmem_freecheck, p);
    386 		SDT_PROBE3(sdt, kmem, free, large,
    387 		    p, requested_size, round_page(size));
    388 		uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
    389 		    round_page(size));
    390 		return;
    391 	}
    392 
    393 	kasan_mark(p, size, size, 0);
    394 
    395 	kmem_size_check(p, requested_size);
    396 	FREECHECK_IN(&kmem_freecheck, p);
    397 	LOCKDEBUG_MEM_CHECK(p, size);
    398 
    399 	pool_cache_put(pc, p);
    400 }
    401 
    402 /* -------------------------------- Kmem API -------------------------------- */
    403 
    404 /*
    405  * kmem_alloc: allocate wired memory.
    406  * => must not be called from interrupt context.
    407  */
    408 void *
    409 kmem_alloc(size_t size, km_flag_t kmflags)
    410 {
    411 	void *v;
    412 
    413 	KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
    414 	    "kmem(9) should not be used from the interrupt context");
    415 	v = kmem_intr_alloc(size, kmflags);
    416 	if (__predict_true(v != NULL)) {
    417 		kmsan_mark(v, size, KMSAN_STATE_UNINIT);
    418 		kmsan_orig(v, size, KMSAN_TYPE_KMEM, __RET_ADDR);
    419 	}
    420 	KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
    421 	return v;
    422 }
    423 
    424 /*
    425  * kmem_zalloc: allocate zeroed wired memory.
    426  * => must not be called from interrupt context.
    427  */
    428 void *
    429 kmem_zalloc(size_t size, km_flag_t kmflags)
    430 {
    431 	void *v;
    432 
    433 	KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
    434 	    "kmem(9) should not be used from the interrupt context");
    435 	v = kmem_intr_zalloc(size, kmflags);
    436 	KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
    437 	return v;
    438 }
    439 
    440 /*
    441  * kmem_free: free wired memory allocated by kmem_alloc.
    442  * => must not be called from interrupt context.
    443  */
    444 void
    445 kmem_free(void *p, size_t size)
    446 {
    447 	KASSERT(!cpu_intr_p());
    448 	KASSERT(!cpu_softintr_p());
    449 	kmem_intr_free(p, size);
    450 	kmsan_mark(p, size, KMSAN_STATE_INITED);
    451 }
    452 
    453 static size_t
    454 kmem_create_caches(const struct kmem_cache_info *array,
    455     const id_t *alloc_probe_table[], const id_t *free_probe_table[],
    456     pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl)
    457 {
    458 	size_t maxidx = 0;
    459 	size_t table_unit = (1 << shift);
    460 	size_t size = table_unit;
    461 	int i;
    462 
    463 	for (i = 0; array[i].kc_size != 0 ; i++) {
    464 		const char *name = array[i].kc_name;
    465 		size_t cache_size = array[i].kc_size;
    466 		struct pool_allocator *pa;
    467 		int flags = 0;
    468 		pool_cache_t pc;
    469 		size_t align;
    470 
    471 		/* check if we reached the requested size */
    472 		if (cache_size > maxsize || cache_size > PAGE_SIZE) {
    473 			break;
    474 		}
    475 
    476 		/*
    477 		 * Exclude caches with size not a factor or multiple of the
    478 		 * coherency unit.
    479 		 */
    480 		if (cache_size < COHERENCY_UNIT) {
    481 			if (COHERENCY_UNIT % cache_size > 0) {
    482 			    	continue;
    483 			}
    484 			flags |= PR_NOTOUCH;
    485 			align = KMEM_ALIGN;
    486 		} else if ((cache_size & (PAGE_SIZE - 1)) == 0) {
    487 			align = PAGE_SIZE;
    488 		} else {
    489 			if ((cache_size % COHERENCY_UNIT) > 0) {
    490 				continue;
    491 			}
    492 			align = COHERENCY_UNIT;
    493 		}
    494 
    495 		if ((cache_size >> shift) > maxidx) {
    496 			maxidx = cache_size >> shift;
    497 		}
    498 
    499 		pa = &pool_allocator_kmem;
    500 		pc = pool_cache_init(cache_size, align, 0, flags,
    501 		    name, pa, ipl, NULL, NULL, NULL);
    502 
    503 		while (size <= cache_size) {
    504 			alloc_table[(size - 1) >> shift] = pc;
    505 #ifdef KDTRACE_HOOKS
    506 			if (alloc_probe_table) {
    507 				alloc_probe_table[(size - 1) >> shift] =
    508 				    array[i].kc_alloc_probe_id;
    509 			}
    510 			if (free_probe_table) {
    511 				free_probe_table[(size - 1) >> shift] =
    512 				    array[i].kc_free_probe_id;
    513 			}
    514 #endif
    515 			size += table_unit;
    516 		}
    517 	}
    518 	return maxidx;
    519 }
    520 
    521 void
    522 kmem_init(void)
    523 {
    524 	kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes,
    525 	    kmem_cache_alloc_probe_id, kmem_cache_free_probe_id,
    526 	    kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM);
    527 	kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes,
    528 	    kmem_cache_big_alloc_probe_id, kmem_cache_big_free_probe_id,
    529 	    kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM);
    530 }
    531 
    532 size_t
    533 kmem_roundup_size(size_t size)
    534 {
    535 	return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
    536 }
    537 
    538 /*
    539  * Used to dynamically allocate string with kmem accordingly to format.
    540  */
    541 char *
    542 kmem_asprintf(const char *fmt, ...)
    543 {
    544 	int size __diagused, len;
    545 	va_list va;
    546 	char *str;
    547 
    548 	va_start(va, fmt);
    549 	len = vsnprintf(NULL, 0, fmt, va);
    550 	va_end(va);
    551 
    552 	str = kmem_alloc(len + 1, KM_SLEEP);
    553 
    554 	va_start(va, fmt);
    555 	size = vsnprintf(str, len + 1, fmt, va);
    556 	va_end(va);
    557 
    558 	KASSERT(size == len);
    559 
    560 	return str;
    561 }
    562 
    563 char *
    564 kmem_strdupsize(const char *str, size_t *lenp, km_flag_t flags)
    565 {
    566 	size_t len = strlen(str) + 1;
    567 	char *ptr = kmem_alloc(len, flags);
    568 	if (ptr == NULL)
    569 		return NULL;
    570 
    571 	if (lenp)
    572 		*lenp = len;
    573 	memcpy(ptr, str, len);
    574 	return ptr;
    575 }
    576 
    577 char *
    578 kmem_strndup(const char *str, size_t maxlen, km_flag_t flags)
    579 {
    580 	KASSERT(str != NULL);
    581 	KASSERT(maxlen != 0);
    582 
    583 	size_t len = strnlen(str, maxlen);
    584 	char *ptr = kmem_alloc(len + 1, flags);
    585 	if (ptr == NULL)
    586 		return NULL;
    587 
    588 	memcpy(ptr, str, len);
    589 	ptr[len] = '\0';
    590 
    591 	return ptr;
    592 }
    593 
    594 void
    595 kmem_strfree(char *str)
    596 {
    597 	if (str == NULL)
    598 		return;
    599 
    600 	kmem_free(str, strlen(str) + 1);
    601 }
    602 
    603 /*
    604  * Utility routine to maybe-allocate a temporary buffer if the size
    605  * is larger than we're willing to put on the stack.
    606  */
    607 void *
    608 kmem_tmpbuf_alloc(size_t size, void *stackbuf, size_t stackbufsize,
    609     km_flag_t flags)
    610 {
    611 	if (size <= stackbufsize) {
    612 		return stackbuf;
    613 	}
    614 
    615 	return kmem_alloc(size, flags);
    616 }
    617 
    618 void
    619 kmem_tmpbuf_free(void *buf, size_t size, void *stackbuf)
    620 {
    621 	if (buf != stackbuf) {
    622 		kmem_free(buf, size);
    623 	}
    624 }
    625 
    626 /* --------------------------- DEBUG / DIAGNOSTIC --------------------------- */
    627 
    628 #if defined(KMEM_SIZE)
    629 static void
    630 kmem_size_set(void *p, size_t sz)
    631 {
    632 	memcpy((char *)p + sz, &sz, sizeof(size_t));
    633 }
    634 
    635 static void
    636 kmem_size_check(void *p, size_t sz)
    637 {
    638 	size_t hsz;
    639 
    640 	memcpy(&hsz, (char *)p + sz, sizeof(size_t));
    641 
    642 	if (hsz != sz) {
    643 		panic("kmem_free(%p, %zu) != allocated size %zu; overwrote?",
    644 		    p, sz, hsz);
    645 	}
    646 
    647 	memset((char *)p + sz, 0xff, sizeof(size_t));
    648 }
    649 #endif /* defined(KMEM_SIZE) */
    650