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