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