1 /* $NetBSD: subr_kmem.c,v 1.80 2020/05/14 17:01:34 maxv 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.80 2020/05/14 17:01:34 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-00008" }, 109 { 16, "kmem-00016" }, 110 { 24, "kmem-00024" }, 111 { 32, "kmem-00032" }, 112 { 40, "kmem-00040" }, 113 { 48, "kmem-00048" }, 114 { 56, "kmem-00056" }, 115 { 64, "kmem-00064" }, 116 { 80, "kmem-00080" }, 117 { 96, "kmem-00096" }, 118 { 112, "kmem-00112" }, 119 { 128, "kmem-00128" }, 120 { 160, "kmem-00160" }, 121 { 192, "kmem-00192" }, 122 { 224, "kmem-00224" }, 123 { 256, "kmem-00256" }, 124 { 320, "kmem-00320" }, 125 { 384, "kmem-00384" }, 126 { 448, "kmem-00448" }, 127 { 512, "kmem-00512" }, 128 { 768, "kmem-00768" }, 129 { 1024, "kmem-01024" }, 130 { 0, NULL } 131 }; 132 133 static const struct kmem_cache_info kmem_cache_big_sizes[] = { 134 { 2048, "kmem-02048" }, 135 { 4096, "kmem-04096" }, 136 { 8192, "kmem-08192" }, 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 #define SIZE_SIZE sizeof(size_t) 172 static void kmem_size_set(void *, size_t); 173 static void kmem_size_check(void *, size_t); 174 #else 175 #define SIZE_SIZE 0 176 #define kmem_size_set(p, sz) /* nothing */ 177 #define kmem_size_check(p, sz) /* nothing */ 178 #endif 179 180 CTASSERT(KM_SLEEP == PR_WAITOK); 181 CTASSERT(KM_NOSLEEP == PR_NOWAIT); 182 183 /* 184 * kmem_intr_alloc: allocate wired memory. 185 */ 186 void * 187 kmem_intr_alloc(size_t requested_size, km_flag_t kmflags) 188 { 189 #ifdef KASAN 190 const size_t origsize = requested_size; 191 #endif 192 size_t allocsz, index; 193 size_t size; 194 pool_cache_t pc; 195 uint8_t *p; 196 197 KASSERT(requested_size > 0); 198 199 KASSERT((kmflags & KM_SLEEP) || (kmflags & KM_NOSLEEP)); 200 KASSERT(!(kmflags & KM_SLEEP) || !(kmflags & KM_NOSLEEP)); 201 202 kasan_add_redzone(&requested_size); 203 size = kmem_roundup_size(requested_size); 204 allocsz = size + SIZE_SIZE; 205 206 if ((index = ((allocsz -1) >> KMEM_SHIFT)) 207 < kmem_cache_maxidx) { 208 pc = kmem_cache[index]; 209 } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT)) 210 < kmem_cache_big_maxidx) { 211 pc = kmem_cache_big[index]; 212 } else { 213 int ret = uvm_km_kmem_alloc(kmem_va_arena, 214 (vsize_t)round_page(size), 215 ((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP) 216 | VM_INSTANTFIT, (vmem_addr_t *)&p); 217 if (ret) { 218 return NULL; 219 } 220 FREECHECK_OUT(&kmem_freecheck, p); 221 return p; 222 } 223 224 p = pool_cache_get(pc, kmflags); 225 226 if (__predict_true(p != NULL)) { 227 FREECHECK_OUT(&kmem_freecheck, p); 228 kmem_size_set(p, requested_size); 229 kasan_mark(p, origsize, size, KASAN_KMEM_REDZONE); 230 return p; 231 } 232 return p; 233 } 234 235 /* 236 * kmem_intr_zalloc: allocate zeroed wired memory. 237 */ 238 void * 239 kmem_intr_zalloc(size_t size, km_flag_t kmflags) 240 { 241 void *p; 242 243 p = kmem_intr_alloc(size, kmflags); 244 if (p != NULL) { 245 memset(p, 0, size); 246 } 247 return p; 248 } 249 250 /* 251 * kmem_intr_free: free wired memory allocated by kmem_alloc. 252 */ 253 void 254 kmem_intr_free(void *p, size_t requested_size) 255 { 256 size_t allocsz, index; 257 size_t size; 258 pool_cache_t pc; 259 260 KASSERT(p != NULL); 261 if (__predict_false(requested_size == 0)) { 262 panic("%s: zero size with pointer %p", __func__, p); 263 } 264 265 kasan_add_redzone(&requested_size); 266 size = kmem_roundup_size(requested_size); 267 allocsz = size + SIZE_SIZE; 268 269 if ((index = ((allocsz -1) >> KMEM_SHIFT)) 270 < kmem_cache_maxidx) { 271 pc = kmem_cache[index]; 272 } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT)) 273 < kmem_cache_big_maxidx) { 274 pc = kmem_cache_big[index]; 275 } else { 276 FREECHECK_IN(&kmem_freecheck, p); 277 uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p, 278 round_page(size)); 279 return; 280 } 281 282 kasan_mark(p, size, size, 0); 283 284 kmem_size_check(p, requested_size); 285 FREECHECK_IN(&kmem_freecheck, p); 286 LOCKDEBUG_MEM_CHECK(p, size); 287 288 pool_cache_put(pc, p); 289 } 290 291 /* -------------------------------- Kmem API -------------------------------- */ 292 293 /* 294 * kmem_alloc: allocate wired memory. 295 * => must not be called from interrupt context. 296 */ 297 void * 298 kmem_alloc(size_t size, km_flag_t kmflags) 299 { 300 void *v; 301 302 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()), 303 "kmem(9) should not be used from the interrupt context"); 304 v = kmem_intr_alloc(size, kmflags); 305 if (__predict_true(v != NULL)) { 306 kmsan_mark(v, size, KMSAN_STATE_UNINIT); 307 kmsan_orig(v, size, KMSAN_TYPE_KMEM, __RET_ADDR); 308 } 309 KASSERT(v || (kmflags & KM_NOSLEEP) != 0); 310 return v; 311 } 312 313 /* 314 * kmem_zalloc: allocate zeroed wired memory. 315 * => must not be called from interrupt context. 316 */ 317 void * 318 kmem_zalloc(size_t size, km_flag_t kmflags) 319 { 320 void *v; 321 322 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()), 323 "kmem(9) should not be used from the interrupt context"); 324 v = kmem_intr_zalloc(size, kmflags); 325 KASSERT(v || (kmflags & KM_NOSLEEP) != 0); 326 return v; 327 } 328 329 /* 330 * kmem_free: free wired memory allocated by kmem_alloc. 331 * => must not be called from interrupt context. 332 */ 333 void 334 kmem_free(void *p, size_t size) 335 { 336 KASSERT(!cpu_intr_p()); 337 KASSERT(!cpu_softintr_p()); 338 kmem_intr_free(p, size); 339 kmsan_mark(p, size, KMSAN_STATE_INITED); 340 } 341 342 static size_t 343 kmem_create_caches(const struct kmem_cache_info *array, 344 pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl) 345 { 346 size_t maxidx = 0; 347 size_t table_unit = (1 << shift); 348 size_t size = table_unit; 349 int i; 350 351 for (i = 0; array[i].kc_size != 0 ; i++) { 352 const char *name = array[i].kc_name; 353 size_t cache_size = array[i].kc_size; 354 struct pool_allocator *pa; 355 int flags = 0; 356 pool_cache_t pc; 357 size_t align; 358 359 /* check if we reached the requested size */ 360 if (cache_size > maxsize || cache_size > PAGE_SIZE) { 361 break; 362 } 363 364 /* 365 * Exclude caches with size not a factor or multiple of the 366 * coherency unit. 367 */ 368 if (cache_size < COHERENCY_UNIT) { 369 if (COHERENCY_UNIT % cache_size > 0) { 370 continue; 371 } 372 flags |= PR_NOTOUCH; 373 align = KMEM_ALIGN; 374 } else if ((cache_size & (PAGE_SIZE - 1)) == 0) { 375 align = PAGE_SIZE; 376 } else { 377 if ((cache_size % COHERENCY_UNIT) > 0) { 378 continue; 379 } 380 align = COHERENCY_UNIT; 381 } 382 383 if ((cache_size >> shift) > maxidx) { 384 maxidx = cache_size >> shift; 385 } 386 387 pa = &pool_allocator_kmem; 388 pc = pool_cache_init(cache_size, align, 0, flags, 389 name, pa, ipl, NULL, NULL, NULL); 390 391 while (size <= cache_size) { 392 alloc_table[(size - 1) >> shift] = pc; 393 size += table_unit; 394 } 395 } 396 return maxidx; 397 } 398 399 void 400 kmem_init(void) 401 { 402 kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes, 403 kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM); 404 kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes, 405 kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM); 406 } 407 408 size_t 409 kmem_roundup_size(size_t size) 410 { 411 return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1); 412 } 413 414 /* 415 * Used to dynamically allocate string with kmem accordingly to format. 416 */ 417 char * 418 kmem_asprintf(const char *fmt, ...) 419 { 420 int size __diagused, len; 421 va_list va; 422 char *str; 423 424 va_start(va, fmt); 425 len = vsnprintf(NULL, 0, fmt, va); 426 va_end(va); 427 428 str = kmem_alloc(len + 1, KM_SLEEP); 429 430 va_start(va, fmt); 431 size = vsnprintf(str, len + 1, fmt, va); 432 va_end(va); 433 434 KASSERT(size == len); 435 436 return str; 437 } 438 439 char * 440 kmem_strdupsize(const char *str, size_t *lenp, km_flag_t flags) 441 { 442 size_t len = strlen(str) + 1; 443 char *ptr = kmem_alloc(len, flags); 444 if (ptr == NULL) 445 return NULL; 446 447 if (lenp) 448 *lenp = len; 449 memcpy(ptr, str, len); 450 return ptr; 451 } 452 453 char * 454 kmem_strndup(const char *str, size_t maxlen, km_flag_t flags) 455 { 456 KASSERT(str != NULL); 457 KASSERT(maxlen != 0); 458 459 size_t len = strnlen(str, maxlen); 460 char *ptr = kmem_alloc(len + 1, flags); 461 if (ptr == NULL) 462 return NULL; 463 464 memcpy(ptr, str, len); 465 ptr[len] = '\0'; 466 467 return ptr; 468 } 469 470 void 471 kmem_strfree(char *str) 472 { 473 if (str == NULL) 474 return; 475 476 kmem_free(str, strlen(str) + 1); 477 } 478 479 /* --------------------------- DEBUG / DIAGNOSTIC --------------------------- */ 480 481 #if defined(KMEM_SIZE) 482 static void 483 kmem_size_set(void *p, size_t sz) 484 { 485 memcpy((size_t *)((uintptr_t)p + sz), &sz, sizeof(size_t)); 486 } 487 488 static void 489 kmem_size_check(void *p, size_t sz) 490 { 491 size_t hsz; 492 493 memcpy(&hsz, (size_t *)((uintptr_t)p + sz), sizeof(size_t)); 494 495 if (hsz != sz) { 496 panic("kmem_free(%p, %zu) != allocated size %zu; overwrote?", 497 p, sz, hsz); 498 } 499 500 memset((size_t *)((uintptr_t)p + sz), 0xff, sizeof(size_t)); 501 } 502 #endif /* defined(KMEM_SIZE) */ 503