Home | History | Annotate | Line # | Download | only in kern
subr_kmem.c revision 1.80.2.1
      1  1.80.2.1   thorpej /*	$NetBSD: subr_kmem.c,v 1.80.2.1 2021/04/03 22:29:00 thorpej Exp $	*/
      2       1.1      yamt 
      3      1.76      maxv /*
      4      1.78        ad  * Copyright (c) 2009-2020 The NetBSD Foundation, Inc.
      5      1.23        ad  * All rights reserved.
      6      1.23        ad  *
      7      1.23        ad  * This code is derived from software contributed to The NetBSD Foundation
      8      1.61      maxv  * by Andrew Doran and Maxime Villard.
      9      1.23        ad  *
     10      1.23        ad  * Redistribution and use in source and binary forms, with or without
     11      1.23        ad  * modification, are permitted provided that the following conditions
     12      1.23        ad  * are met:
     13      1.23        ad  * 1. Redistributions of source code must retain the above copyright
     14      1.23        ad  *    notice, this list of conditions and the following disclaimer.
     15      1.23        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.23        ad  *    notice, this list of conditions and the following disclaimer in the
     17      1.23        ad  *    documentation and/or other materials provided with the distribution.
     18      1.23        ad  *
     19      1.23        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.23        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.23        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.23        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.23        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.23        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.23        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.23        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.23        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.23        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.23        ad  * POSSIBILITY OF SUCH DAMAGE.
     30      1.23        ad  */
     31      1.23        ad 
     32      1.76      maxv /*
     33       1.1      yamt  * Copyright (c)2006 YAMAMOTO Takashi,
     34       1.1      yamt  * All rights reserved.
     35       1.1      yamt  *
     36       1.1      yamt  * Redistribution and use in source and binary forms, with or without
     37       1.1      yamt  * modification, are permitted provided that the following conditions
     38       1.1      yamt  * are met:
     39       1.1      yamt  * 1. Redistributions of source code must retain the above copyright
     40       1.1      yamt  *    notice, this list of conditions and the following disclaimer.
     41       1.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     42       1.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     43       1.1      yamt  *    documentation and/or other materials provided with the distribution.
     44       1.1      yamt  *
     45       1.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     46       1.1      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47       1.1      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48       1.1      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     49       1.1      yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50       1.1      yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51       1.1      yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52       1.1      yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53       1.1      yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54       1.1      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55       1.1      yamt  * SUCH DAMAGE.
     56       1.1      yamt  */
     57       1.1      yamt 
     58       1.1      yamt /*
     59      1.55      maxv  * Allocator of kernel wired memory. This allocator has some debug features
     60      1.55      maxv  * enabled with "option DIAGNOSTIC" and "option DEBUG".
     61      1.50      yamt  */
     62      1.50      yamt 
     63      1.50      yamt /*
     64      1.55      maxv  * KMEM_SIZE: detect alloc/free size mismatch bugs.
     65      1.79        ad  *	Append to each allocation a fixed-sized footer and record the exact
     66      1.79        ad  *	user-requested allocation size in it.  When freeing, compare it with
     67      1.79        ad  *	kmem_free's "size" argument.
     68      1.60      maxv  *
     69      1.76      maxv  * This option is enabled on DIAGNOSTIC.
     70      1.50      yamt  *
     71      1.79        ad  *  |CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK| |
     72      1.79        ad  *  +-----+-----+-----+-----+-----+-----+-----+-----+-----+-+
     73      1.79        ad  *  |     |     |     |     |     |     |     |     |/////|U|
     74      1.79        ad  *  |     |     |     |     |     |     |     |     |/HSZ/|U|
     75      1.79        ad  *  |     |     |     |     |     |     |     |     |/////|U|
     76      1.79        ad  *  +-----+-----+-----+-----+-----+-----+-----+-----+-----+-+
     77      1.79        ad  *  | Buffer usable by the caller (requested size)  |Size |Unused
     78      1.60      maxv  */
     79      1.60      maxv 
     80       1.1      yamt #include <sys/cdefs.h>
     81  1.80.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.80.2.1 2021/04/03 22:29:00 thorpej Exp $");
     82      1.63  christos 
     83      1.63  christos #ifdef _KERNEL_OPT
     84      1.63  christos #include "opt_kmem.h"
     85      1.63  christos #endif
     86       1.1      yamt 
     87       1.1      yamt #include <sys/param.h>
     88       1.6      yamt #include <sys/callback.h>
     89       1.1      yamt #include <sys/kmem.h>
     90      1.39      para #include <sys/pool.h>
     91      1.13        ad #include <sys/debug.h>
     92      1.17        ad #include <sys/lockdebug.h>
     93      1.23        ad #include <sys/cpu.h>
     94      1.69      maxv #include <sys/asan.h>
     95      1.77      maxv #include <sys/msan.h>
     96      1.69      maxv 
     97       1.6      yamt #include <uvm/uvm_extern.h>
     98       1.6      yamt #include <uvm/uvm_map.h>
     99       1.6      yamt 
    100       1.1      yamt #include <lib/libkern/libkern.h>
    101       1.1      yamt 
    102      1.46      para struct kmem_cache_info {
    103      1.40     rmind 	size_t		kc_size;
    104      1.40     rmind 	const char *	kc_name;
    105      1.46      para };
    106      1.46      para 
    107      1.46      para static const struct kmem_cache_info kmem_cache_sizes[] = {
    108      1.78        ad 	{  8, "kmem-00008" },
    109      1.78        ad 	{ 16, "kmem-00016" },
    110      1.78        ad 	{ 24, "kmem-00024" },
    111      1.78        ad 	{ 32, "kmem-00032" },
    112      1.78        ad 	{ 40, "kmem-00040" },
    113      1.78        ad 	{ 48, "kmem-00048" },
    114      1.78        ad 	{ 56, "kmem-00056" },
    115      1.78        ad 	{ 64, "kmem-00064" },
    116      1.78        ad 	{ 80, "kmem-00080" },
    117      1.78        ad 	{ 96, "kmem-00096" },
    118      1.78        ad 	{ 112, "kmem-00112" },
    119      1.78        ad 	{ 128, "kmem-00128" },
    120      1.78        ad 	{ 160, "kmem-00160" },
    121      1.78        ad 	{ 192, "kmem-00192" },
    122      1.78        ad 	{ 224, "kmem-00224" },
    123      1.78        ad 	{ 256, "kmem-00256" },
    124      1.78        ad 	{ 320, "kmem-00320" },
    125      1.78        ad 	{ 384, "kmem-00384" },
    126      1.78        ad 	{ 448, "kmem-00448" },
    127      1.78        ad 	{ 512, "kmem-00512" },
    128      1.78        ad 	{ 768, "kmem-00768" },
    129      1.78        ad 	{ 1024, "kmem-01024" },
    130      1.46      para 	{ 0, NULL }
    131      1.46      para };
    132      1.46      para 
    133      1.46      para static const struct kmem_cache_info kmem_cache_big_sizes[] = {
    134      1.78        ad 	{ 2048, "kmem-02048" },
    135      1.78        ad 	{ 4096, "kmem-04096" },
    136      1.78        ad 	{ 8192, "kmem-08192" },
    137      1.46      para 	{ 16384, "kmem-16384" },
    138      1.39      para 	{ 0, NULL }
    139      1.39      para };
    140       1.1      yamt 
    141      1.39      para /*
    142      1.40     rmind  * KMEM_ALIGN is the smallest guaranteed alignment and also the
    143      1.46      para  * smallest allocateable quantum.
    144      1.46      para  * Every cache size >= CACHE_LINE_SIZE gets CACHE_LINE_SIZE alignment.
    145      1.39      para  */
    146      1.40     rmind #define	KMEM_ALIGN		8
    147      1.40     rmind #define	KMEM_SHIFT		3
    148      1.46      para #define	KMEM_MAXSIZE		1024
    149      1.40     rmind #define	KMEM_CACHE_COUNT	(KMEM_MAXSIZE >> KMEM_SHIFT)
    150       1.1      yamt 
    151      1.40     rmind static pool_cache_t kmem_cache[KMEM_CACHE_COUNT] __cacheline_aligned;
    152      1.40     rmind static size_t kmem_cache_maxidx __read_mostly;
    153      1.23        ad 
    154      1.46      para #define	KMEM_BIG_ALIGN		2048
    155      1.46      para #define	KMEM_BIG_SHIFT		11
    156      1.46      para #define	KMEM_BIG_MAXSIZE	16384
    157      1.46      para #define	KMEM_CACHE_BIG_COUNT	(KMEM_BIG_MAXSIZE >> KMEM_BIG_SHIFT)
    158      1.46      para 
    159      1.46      para static pool_cache_t kmem_cache_big[KMEM_CACHE_BIG_COUNT] __cacheline_aligned;
    160      1.46      para static size_t kmem_cache_big_maxidx __read_mostly;
    161      1.46      para 
    162      1.53      maxv #if defined(DIAGNOSTIC) && defined(_HARDKERNEL)
    163      1.57      maxv #define	KMEM_SIZE
    164      1.67      maxv #endif
    165      1.53      maxv 
    166      1.45    martin #if defined(DEBUG) && defined(_HARDKERNEL)
    167      1.61      maxv static void *kmem_freecheck;
    168      1.67      maxv #endif
    169       1.4      yamt 
    170      1.23        ad #if defined(KMEM_SIZE)
    171      1.79        ad #define	SIZE_SIZE	sizeof(size_t)
    172      1.23        ad static void kmem_size_set(void *, size_t);
    173      1.39      para static void kmem_size_check(void *, size_t);
    174      1.23        ad #else
    175      1.23        ad #define	SIZE_SIZE	0
    176      1.23        ad #define	kmem_size_set(p, sz)	/* nothing */
    177      1.23        ad #define	kmem_size_check(p, sz)	/* nothing */
    178      1.23        ad #endif
    179      1.23        ad 
    180      1.32     skrll CTASSERT(KM_SLEEP == PR_WAITOK);
    181      1.32     skrll CTASSERT(KM_NOSLEEP == PR_NOWAIT);
    182      1.32     skrll 
    183      1.46      para /*
    184      1.46      para  * kmem_intr_alloc: allocate wired memory.
    185      1.46      para  */
    186      1.39      para void *
    187      1.50      yamt kmem_intr_alloc(size_t requested_size, km_flag_t kmflags)
    188       1.1      yamt {
    189      1.71  christos #ifdef KASAN
    190      1.70      maxv 	const size_t origsize = requested_size;
    191      1.71  christos #endif
    192      1.40     rmind 	size_t allocsz, index;
    193      1.50      yamt 	size_t size;
    194      1.39      para 	pool_cache_t pc;
    195      1.39      para 	uint8_t *p;
    196       1.1      yamt 
    197      1.50      yamt 	KASSERT(requested_size > 0);
    198       1.1      yamt 
    199      1.65  riastrad 	KASSERT((kmflags & KM_SLEEP) || (kmflags & KM_NOSLEEP));
    200      1.65  riastrad 	KASSERT(!(kmflags & KM_SLEEP) || !(kmflags & KM_NOSLEEP));
    201      1.65  riastrad 
    202      1.69      maxv 	kasan_add_redzone(&requested_size);
    203      1.50      yamt 	size = kmem_roundup_size(requested_size);
    204      1.54      maxv 	allocsz = size + SIZE_SIZE;
    205      1.54      maxv 
    206      1.46      para 	if ((index = ((allocsz -1) >> KMEM_SHIFT))
    207      1.46      para 	    < kmem_cache_maxidx) {
    208      1.46      para 		pc = kmem_cache[index];
    209      1.46      para 	} else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
    210      1.55      maxv 	    < kmem_cache_big_maxidx) {
    211      1.46      para 		pc = kmem_cache_big[index];
    212      1.48  uebayasi 	} else {
    213      1.40     rmind 		int ret = uvm_km_kmem_alloc(kmem_va_arena,
    214      1.43      para 		    (vsize_t)round_page(size),
    215      1.39      para 		    ((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP)
    216      1.39      para 		     | VM_INSTANTFIT, (vmem_addr_t *)&p);
    217      1.46      para 		if (ret) {
    218      1.46      para 			return NULL;
    219      1.46      para 		}
    220      1.46      para 		FREECHECK_OUT(&kmem_freecheck, p);
    221      1.46      para 		return p;
    222       1.1      yamt 	}
    223       1.1      yamt 
    224      1.39      para 	p = pool_cache_get(pc, kmflags);
    225      1.39      para 
    226      1.39      para 	if (__predict_true(p != NULL)) {
    227      1.39      para 		FREECHECK_OUT(&kmem_freecheck, p);
    228      1.50      yamt 		kmem_size_set(p, requested_size);
    229      1.75      maxv 		kasan_mark(p, origsize, size, KASAN_KMEM_REDZONE);
    230      1.68      maxv 		return p;
    231      1.39      para 	}
    232      1.47      para 	return p;
    233       1.1      yamt }
    234       1.1      yamt 
    235      1.46      para /*
    236      1.46      para  * kmem_intr_zalloc: allocate zeroed wired memory.
    237      1.46      para  */
    238      1.39      para void *
    239      1.39      para kmem_intr_zalloc(size_t size, km_flag_t kmflags)
    240      1.23        ad {
    241      1.39      para 	void *p;
    242      1.23        ad 
    243      1.39      para 	p = kmem_intr_alloc(size, kmflags);
    244      1.39      para 	if (p != NULL) {
    245      1.39      para 		memset(p, 0, size);
    246      1.39      para 	}
    247      1.39      para 	return p;
    248      1.23        ad }
    249      1.23        ad 
    250      1.46      para /*
    251      1.46      para  * kmem_intr_free: free wired memory allocated by kmem_alloc.
    252      1.46      para  */
    253      1.39      para void
    254      1.50      yamt kmem_intr_free(void *p, size_t requested_size)
    255      1.23        ad {
    256      1.40     rmind 	size_t allocsz, index;
    257      1.50      yamt 	size_t size;
    258      1.39      para 	pool_cache_t pc;
    259      1.23        ad 
    260      1.39      para 	KASSERT(p != NULL);
    261      1.80      maxv 	if (__predict_false(requested_size == 0)) {
    262      1.80      maxv 		panic("%s: zero size with pointer %p", __func__, p);
    263      1.80      maxv 	}
    264      1.39      para 
    265      1.69      maxv 	kasan_add_redzone(&requested_size);
    266      1.50      yamt 	size = kmem_roundup_size(requested_size);
    267      1.54      maxv 	allocsz = size + SIZE_SIZE;
    268      1.54      maxv 
    269      1.46      para 	if ((index = ((allocsz -1) >> KMEM_SHIFT))
    270      1.46      para 	    < kmem_cache_maxidx) {
    271      1.46      para 		pc = kmem_cache[index];
    272      1.46      para 	} else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
    273      1.55      maxv 	    < kmem_cache_big_maxidx) {
    274      1.46      para 		pc = kmem_cache_big[index];
    275      1.46      para 	} else {
    276      1.46      para 		FREECHECK_IN(&kmem_freecheck, p);
    277      1.39      para 		uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
    278      1.43      para 		    round_page(size));
    279      1.39      para 		return;
    280      1.39      para 	}
    281      1.39      para 
    282      1.75      maxv 	kasan_mark(p, size, size, 0);
    283      1.70      maxv 
    284      1.50      yamt 	kmem_size_check(p, requested_size);
    285      1.39      para 	FREECHECK_IN(&kmem_freecheck, p);
    286      1.46      para 	LOCKDEBUG_MEM_CHECK(p, size);
    287      1.39      para 
    288      1.39      para 	pool_cache_put(pc, p);
    289      1.23        ad }
    290      1.23        ad 
    291      1.76      maxv /* -------------------------------- Kmem API -------------------------------- */
    292       1.1      yamt 
    293       1.1      yamt /*
    294       1.1      yamt  * kmem_alloc: allocate wired memory.
    295       1.1      yamt  * => must not be called from interrupt context.
    296       1.1      yamt  */
    297       1.1      yamt void *
    298       1.1      yamt kmem_alloc(size_t size, km_flag_t kmflags)
    299       1.1      yamt {
    300      1.62       chs 	void *v;
    301      1.62       chs 
    302      1.40     rmind 	KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
    303      1.40     rmind 	    "kmem(9) should not be used from the interrupt context");
    304      1.62       chs 	v = kmem_intr_alloc(size, kmflags);
    305      1.77      maxv 	if (__predict_true(v != NULL)) {
    306      1.77      maxv 		kmsan_mark(v, size, KMSAN_STATE_UNINIT);
    307      1.77      maxv 		kmsan_orig(v, size, KMSAN_TYPE_KMEM, __RET_ADDR);
    308      1.77      maxv 	}
    309      1.62       chs 	KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
    310      1.62       chs 	return v;
    311       1.1      yamt }
    312       1.1      yamt 
    313       1.1      yamt /*
    314      1.39      para  * kmem_zalloc: allocate zeroed wired memory.
    315       1.2      yamt  * => must not be called from interrupt context.
    316       1.2      yamt  */
    317       1.2      yamt void *
    318       1.2      yamt kmem_zalloc(size_t size, km_flag_t kmflags)
    319       1.2      yamt {
    320      1.62       chs 	void *v;
    321      1.62       chs 
    322      1.40     rmind 	KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
    323      1.40     rmind 	    "kmem(9) should not be used from the interrupt context");
    324      1.62       chs 	v = kmem_intr_zalloc(size, kmflags);
    325      1.62       chs 	KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
    326      1.62       chs 	return v;
    327       1.2      yamt }
    328       1.2      yamt 
    329       1.2      yamt /*
    330       1.1      yamt  * kmem_free: free wired memory allocated by kmem_alloc.
    331       1.1      yamt  * => must not be called from interrupt context.
    332       1.1      yamt  */
    333       1.1      yamt void
    334       1.1      yamt kmem_free(void *p, size_t size)
    335       1.1      yamt {
    336      1.23        ad 	KASSERT(!cpu_intr_p());
    337      1.27        ad 	KASSERT(!cpu_softintr_p());
    338      1.39      para 	kmem_intr_free(p, size);
    339      1.77      maxv 	kmsan_mark(p, size, KMSAN_STATE_INITED);
    340       1.1      yamt }
    341       1.1      yamt 
    342      1.46      para static size_t
    343      1.39      para kmem_create_caches(const struct kmem_cache_info *array,
    344      1.46      para     pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl)
    345       1.1      yamt {
    346      1.46      para 	size_t maxidx = 0;
    347      1.46      para 	size_t table_unit = (1 << shift);
    348      1.39      para 	size_t size = table_unit;
    349      1.23        ad 	int i;
    350       1.1      yamt 
    351      1.39      para 	for (i = 0; array[i].kc_size != 0 ; i++) {
    352      1.40     rmind 		const char *name = array[i].kc_name;
    353      1.39      para 		size_t cache_size = array[i].kc_size;
    354      1.46      para 		struct pool_allocator *pa;
    355      1.74      maxv 		int flags = 0;
    356      1.40     rmind 		pool_cache_t pc;
    357      1.39      para 		size_t align;
    358      1.39      para 
    359      1.39      para 		/* check if we reached the requested size */
    360      1.46      para 		if (cache_size > maxsize || cache_size > PAGE_SIZE) {
    361      1.23        ad 			break;
    362      1.40     rmind 		}
    363      1.78        ad 
    364      1.78        ad 		/*
    365      1.78        ad 		 * Exclude caches with size not a factor or multiple of the
    366      1.78        ad 		 * coherency unit.
    367      1.78        ad 		 */
    368      1.78        ad 		if (cache_size < COHERENCY_UNIT) {
    369      1.78        ad 			if (COHERENCY_UNIT % cache_size > 0) {
    370      1.78        ad 			    	continue;
    371      1.78        ad 			}
    372      1.78        ad 			flags |= PR_NOTOUCH;
    373      1.78        ad 			align = KMEM_ALIGN;
    374      1.78        ad 		} else if ((cache_size & (PAGE_SIZE - 1)) == 0) {
    375      1.78        ad 			align = PAGE_SIZE;
    376      1.78        ad 		} else {
    377      1.78        ad 			if ((cache_size % COHERENCY_UNIT) > 0) {
    378      1.78        ad 				continue;
    379      1.78        ad 			}
    380      1.78        ad 			align = COHERENCY_UNIT;
    381      1.46      para 		}
    382      1.46      para 
    383      1.46      para 		if ((cache_size >> shift) > maxidx) {
    384      1.46      para 			maxidx = cache_size >> shift;
    385      1.40     rmind 		}
    386       1.1      yamt 
    387      1.46      para 		pa = &pool_allocator_kmem;
    388      1.39      para 		pc = pool_cache_init(cache_size, align, 0, flags,
    389      1.46      para 		    name, pa, ipl, NULL, NULL, NULL);
    390       1.1      yamt 
    391      1.39      para 		while (size <= cache_size) {
    392      1.46      para 			alloc_table[(size - 1) >> shift] = pc;
    393      1.39      para 			size += table_unit;
    394      1.39      para 		}
    395       1.1      yamt 	}
    396      1.46      para 	return maxidx;
    397       1.1      yamt }
    398       1.1      yamt 
    399      1.39      para void
    400      1.39      para kmem_init(void)
    401       1.1      yamt {
    402      1.46      para 	kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes,
    403      1.46      para 	    kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM);
    404      1.55      maxv 	kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes,
    405      1.46      para 	    kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM);
    406       1.1      yamt }
    407       1.4      yamt 
    408      1.39      para size_t
    409      1.39      para kmem_roundup_size(size_t size)
    410       1.7      yamt {
    411      1.61      maxv 	return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
    412      1.61      maxv }
    413       1.7      yamt 
    414      1.61      maxv /*
    415      1.61      maxv  * Used to dynamically allocate string with kmem accordingly to format.
    416      1.61      maxv  */
    417      1.61      maxv char *
    418      1.61      maxv kmem_asprintf(const char *fmt, ...)
    419      1.61      maxv {
    420      1.61      maxv 	int size __diagused, len;
    421      1.61      maxv 	va_list va;
    422      1.61      maxv 	char *str;
    423      1.61      maxv 
    424      1.61      maxv 	va_start(va, fmt);
    425      1.61      maxv 	len = vsnprintf(NULL, 0, fmt, va);
    426      1.61      maxv 	va_end(va);
    427      1.61      maxv 
    428      1.61      maxv 	str = kmem_alloc(len + 1, KM_SLEEP);
    429      1.61      maxv 
    430      1.61      maxv 	va_start(va, fmt);
    431      1.61      maxv 	size = vsnprintf(str, len + 1, fmt, va);
    432      1.61      maxv 	va_end(va);
    433      1.61      maxv 
    434      1.61      maxv 	KASSERT(size == len);
    435      1.61      maxv 
    436      1.61      maxv 	return str;
    437       1.7      yamt }
    438       1.7      yamt 
    439      1.64  christos char *
    440      1.64  christos kmem_strdupsize(const char *str, size_t *lenp, km_flag_t flags)
    441      1.64  christos {
    442      1.64  christos 	size_t len = strlen(str) + 1;
    443      1.64  christos 	char *ptr = kmem_alloc(len, flags);
    444      1.64  christos 	if (ptr == NULL)
    445      1.64  christos 		return NULL;
    446      1.64  christos 
    447      1.64  christos 	if (lenp)
    448      1.64  christos 		*lenp = len;
    449      1.64  christos 	memcpy(ptr, str, len);
    450      1.64  christos 	return ptr;
    451      1.64  christos }
    452      1.64  christos 
    453      1.66  christos char *
    454      1.66  christos kmem_strndup(const char *str, size_t maxlen, km_flag_t flags)
    455      1.66  christos {
    456      1.66  christos 	KASSERT(str != NULL);
    457      1.66  christos 	KASSERT(maxlen != 0);
    458      1.66  christos 
    459      1.66  christos 	size_t len = strnlen(str, maxlen);
    460      1.66  christos 	char *ptr = kmem_alloc(len + 1, flags);
    461      1.66  christos 	if (ptr == NULL)
    462      1.66  christos 		return NULL;
    463      1.66  christos 
    464      1.66  christos 	memcpy(ptr, str, len);
    465      1.66  christos 	ptr[len] = '\0';
    466      1.66  christos 
    467      1.66  christos 	return ptr;
    468      1.66  christos }
    469      1.66  christos 
    470      1.64  christos void
    471      1.64  christos kmem_strfree(char *str)
    472      1.64  christos {
    473      1.64  christos 	if (str == NULL)
    474      1.64  christos 		return;
    475      1.64  christos 
    476      1.64  christos 	kmem_free(str, strlen(str) + 1);
    477      1.64  christos }
    478      1.64  christos 
    479  1.80.2.1   thorpej /*
    480  1.80.2.1   thorpej  * Utility routine to maybe-allocate a temporary buffer if the size
    481  1.80.2.1   thorpej  * is larger than we're willing to put on the stack.
    482  1.80.2.1   thorpej  */
    483  1.80.2.1   thorpej void *
    484  1.80.2.1   thorpej kmem_tmpbuf_alloc(size_t size, void *stackbuf, size_t stackbufsize,
    485  1.80.2.1   thorpej     km_flag_t flags)
    486  1.80.2.1   thorpej {
    487  1.80.2.1   thorpej 	if (size <= stackbufsize) {
    488  1.80.2.1   thorpej 		return stackbuf;
    489  1.80.2.1   thorpej 	}
    490  1.80.2.1   thorpej 
    491  1.80.2.1   thorpej 	return kmem_alloc(size, flags);
    492  1.80.2.1   thorpej }
    493  1.80.2.1   thorpej 
    494  1.80.2.1   thorpej void
    495  1.80.2.1   thorpej kmem_tmpbuf_free(void *buf, size_t size, void *stackbuf)
    496  1.80.2.1   thorpej {
    497  1.80.2.1   thorpej 	if (buf != stackbuf) {
    498  1.80.2.1   thorpej 		kmem_free(buf, size);
    499  1.80.2.1   thorpej 	}
    500  1.80.2.1   thorpej }
    501  1.80.2.1   thorpej 
    502      1.76      maxv /* --------------------------- DEBUG / DIAGNOSTIC --------------------------- */
    503       1.4      yamt 
    504      1.23        ad #if defined(KMEM_SIZE)
    505      1.23        ad static void
    506      1.23        ad kmem_size_set(void *p, size_t sz)
    507      1.23        ad {
    508  1.80.2.1   thorpej 	memcpy((char *)p + sz, &sz, sizeof(size_t));
    509      1.23        ad }
    510      1.23        ad 
    511      1.23        ad static void
    512      1.39      para kmem_size_check(void *p, size_t sz)
    513      1.23        ad {
    514      1.57      maxv 	size_t hsz;
    515      1.23        ad 
    516  1.80.2.1   thorpej 	memcpy(&hsz, (char *)p + sz, sizeof(size_t));
    517      1.57      maxv 
    518      1.57      maxv 	if (hsz != sz) {
    519      1.79        ad 		panic("kmem_free(%p, %zu) != allocated size %zu; overwrote?",
    520      1.79        ad 		    p, sz, hsz);
    521      1.23        ad 	}
    522      1.73      maxv 
    523  1.80.2.1   thorpej 	memset((char *)p + sz, 0xff, sizeof(size_t));
    524      1.23        ad }
    525      1.54      maxv #endif /* defined(KMEM_SIZE) */
    526