1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * Kernel memory allocator, as described in the following two papers: 31 * 32 * Jeff Bonwick, 33 * The Slab Allocator: An Object-Caching Kernel Memory Allocator. 34 * Proceedings of the Summer 1994 Usenix Conference. 35 * Available as /shared/sac/PSARC/1994/028/materials/kmem.pdf. 36 * 37 * Jeff Bonwick and Jonathan Adams, 38 * Magazines and vmem: Extending the Slab Allocator to Many CPUs and 39 * Arbitrary Resources. 40 * Proceedings of the 2001 Usenix Conference. 41 * Available as /shared/sac/PSARC/2000/550/materials/vmem.pdf. 42 */ 43 44 #include <sys/kmem_impl.h> 45 #include <sys/vmem_impl.h> 46 #include <sys/param.h> 47 #include <sys/sysmacros.h> 48 #include <sys/vm.h> 49 #include <sys/proc.h> 50 #include <sys/tuneable.h> 51 #include <sys/systm.h> 52 #include <sys/cmn_err.h> 53 #include <sys/debug.h> 54 #include <sys/mutex.h> 55 #include <sys/bitmap.h> 56 #include <sys/atomic.h> 57 #include <sys/kobj.h> 58 #include <sys/disp.h> 59 #include <vm/seg_kmem.h> 60 #include <sys/log.h> 61 #include <sys/callb.h> 62 #include <sys/taskq.h> 63 #include <sys/modctl.h> 64 #include <sys/reboot.h> 65 #include <sys/id32.h> 66 #include <sys/zone.h> 67 68 extern void streams_msg_init(void); 69 extern int segkp_fromheap; 70 extern void segkp_cache_free(void); 71 72 struct kmem_cache_kstat { 73 kstat_named_t kmc_buf_size; 74 kstat_named_t kmc_align; 75 kstat_named_t kmc_chunk_size; 76 kstat_named_t kmc_slab_size; 77 kstat_named_t kmc_alloc; 78 kstat_named_t kmc_alloc_fail; 79 kstat_named_t kmc_free; 80 kstat_named_t kmc_depot_alloc; 81 kstat_named_t kmc_depot_free; 82 kstat_named_t kmc_depot_contention; 83 kstat_named_t kmc_slab_alloc; 84 kstat_named_t kmc_slab_free; 85 kstat_named_t kmc_buf_constructed; 86 kstat_named_t kmc_buf_avail; 87 kstat_named_t kmc_buf_inuse; 88 kstat_named_t kmc_buf_total; 89 kstat_named_t kmc_buf_max; 90 kstat_named_t kmc_slab_create; 91 kstat_named_t kmc_slab_destroy; 92 kstat_named_t kmc_vmem_source; 93 kstat_named_t kmc_hash_size; 94 kstat_named_t kmc_hash_lookup_depth; 95 kstat_named_t kmc_hash_rescale; 96 kstat_named_t kmc_full_magazines; 97 kstat_named_t kmc_empty_magazines; 98 kstat_named_t kmc_magazine_size; 99 } kmem_cache_kstat = { 100 { "buf_size", KSTAT_DATA_UINT64 }, 101 { "align", KSTAT_DATA_UINT64 }, 102 { "chunk_size", KSTAT_DATA_UINT64 }, 103 { "slab_size", KSTAT_DATA_UINT64 }, 104 { "alloc", KSTAT_DATA_UINT64 }, 105 { "alloc_fail", KSTAT_DATA_UINT64 }, 106 { "free", KSTAT_DATA_UINT64 }, 107 { "depot_alloc", KSTAT_DATA_UINT64 }, 108 { "depot_free", KSTAT_DATA_UINT64 }, 109 { "depot_contention", KSTAT_DATA_UINT64 }, 110 { "slab_alloc", KSTAT_DATA_UINT64 }, 111 { "slab_free", KSTAT_DATA_UINT64 }, 112 { "buf_constructed", KSTAT_DATA_UINT64 }, 113 { "buf_avail", KSTAT_DATA_UINT64 }, 114 { "buf_inuse", KSTAT_DATA_UINT64 }, 115 { "buf_total", KSTAT_DATA_UINT64 }, 116 { "buf_max", KSTAT_DATA_UINT64 }, 117 { "slab_create", KSTAT_DATA_UINT64 }, 118 { "slab_destroy", KSTAT_DATA_UINT64 }, 119 { "vmem_source", KSTAT_DATA_UINT64 }, 120 { "hash_size", KSTAT_DATA_UINT64 }, 121 { "hash_lookup_depth", KSTAT_DATA_UINT64 }, 122 { "hash_rescale", KSTAT_DATA_UINT64 }, 123 { "full_magazines", KSTAT_DATA_UINT64 }, 124 { "empty_magazines", KSTAT_DATA_UINT64 }, 125 { "magazine_size", KSTAT_DATA_UINT64 }, 126 }; 127 128 static kmutex_t kmem_cache_kstat_lock; 129 130 /* 131 * The default set of caches to back kmem_alloc(). 132 * These sizes should be reevaluated periodically. 133 * 134 * We want allocations that are multiples of the coherency granularity 135 * (64 bytes) to be satisfied from a cache which is a multiple of 64 136 * bytes, so that it will be 64-byte aligned. For all multiples of 64, 137 * the next kmem_cache_size greater than or equal to it must be a 138 * multiple of 64. 139 */ 140 static const int kmem_alloc_sizes[] = { 141 1 * 8, 142 2 * 8, 143 3 * 8, 144 4 * 8, 5 * 8, 6 * 8, 7 * 8, 145 4 * 16, 5 * 16, 6 * 16, 7 * 16, 146 4 * 32, 5 * 32, 6 * 32, 7 * 32, 147 4 * 64, 5 * 64, 6 * 64, 7 * 64, 148 4 * 128, 5 * 128, 6 * 128, 7 * 128, 149 P2ALIGN(8192 / 7, 64), 150 P2ALIGN(8192 / 6, 64), 151 P2ALIGN(8192 / 5, 64), 152 P2ALIGN(8192 / 4, 64), 153 P2ALIGN(8192 / 3, 64), 154 P2ALIGN(8192 / 2, 64), 155 P2ALIGN(8192 / 1, 64), 156 4096 * 3, 157 8192 * 2, 158 }; 159 160 #define KMEM_MAXBUF 16384 161 162 static kmem_cache_t *kmem_alloc_table[KMEM_MAXBUF >> KMEM_ALIGN_SHIFT]; 163 164 static kmem_magtype_t kmem_magtype[] = { 165 { 1, 8, 3200, 65536 }, 166 { 3, 16, 256, 32768 }, 167 { 7, 32, 64, 16384 }, 168 { 15, 64, 0, 8192 }, 169 { 31, 64, 0, 4096 }, 170 { 47, 64, 0, 2048 }, 171 { 63, 64, 0, 1024 }, 172 { 95, 64, 0, 512 }, 173 { 143, 64, 0, 0 }, 174 }; 175 176 static uint32_t kmem_reaping; 177 static uint32_t kmem_reaping_idspace; 178 179 /* 180 * kmem tunables 181 */ 182 clock_t kmem_reap_interval; /* cache reaping rate [15 * HZ ticks] */ 183 int kmem_depot_contention = 3; /* max failed tryenters per real interval */ 184 pgcnt_t kmem_reapahead = 0; /* start reaping N pages before pageout */ 185 int kmem_panic = 1; /* whether to panic on error */ 186 int kmem_logging = 1; /* kmem_log_enter() override */ 187 uint32_t kmem_mtbf = 0; /* mean time between failures [default: off] */ 188 size_t kmem_transaction_log_size; /* transaction log size [2% of memory] */ 189 size_t kmem_content_log_size; /* content log size [2% of memory] */ 190 size_t kmem_failure_log_size; /* failure log [4 pages per CPU] */ 191 size_t kmem_slab_log_size; /* slab create log [4 pages per CPU] */ 192 size_t kmem_content_maxsave = 256; /* KMF_CONTENTS max bytes to log */ 193 size_t kmem_lite_minsize = 0; /* minimum buffer size for KMF_LITE */ 194 size_t kmem_lite_maxalign = 1024; /* maximum buffer alignment for KMF_LITE */ 195 int kmem_lite_pcs = 4; /* number of PCs to store in KMF_LITE mode */ 196 size_t kmem_maxverify; /* maximum bytes to inspect in debug routines */ 197 size_t kmem_minfirewall; /* hardware-enforced redzone threshold */ 198 199 #ifdef DEBUG 200 int kmem_flags = KMF_AUDIT | KMF_DEADBEEF | KMF_REDZONE | KMF_CONTENTS; 201 #else 202 int kmem_flags = 0; 203 #endif 204 int kmem_ready; 205 206 static kmem_cache_t *kmem_slab_cache; 207 static kmem_cache_t *kmem_bufctl_cache; 208 static kmem_cache_t *kmem_bufctl_audit_cache; 209 210 static kmutex_t kmem_cache_lock; /* inter-cache linkage only */ 211 kmem_cache_t kmem_null_cache; 212 213 static taskq_t *kmem_taskq; 214 static kmutex_t kmem_flags_lock; 215 static vmem_t *kmem_metadata_arena; 216 static vmem_t *kmem_msb_arena; /* arena for metadata caches */ 217 static vmem_t *kmem_cache_arena; 218 static vmem_t *kmem_hash_arena; 219 static vmem_t *kmem_log_arena; 220 static vmem_t *kmem_oversize_arena; 221 static vmem_t *kmem_va_arena; 222 static vmem_t *kmem_default_arena; 223 static vmem_t *kmem_firewall_va_arena; 224 static vmem_t *kmem_firewall_arena; 225 226 kmem_log_header_t *kmem_transaction_log; 227 kmem_log_header_t *kmem_content_log; 228 kmem_log_header_t *kmem_failure_log; 229 kmem_log_header_t *kmem_slab_log; 230 231 static int kmem_lite_count; /* # of PCs in kmem_buftag_lite_t */ 232 233 #define KMEM_BUFTAG_LITE_ENTER(bt, count, caller) \ 234 if ((count) > 0) { \ 235 pc_t *_s = ((kmem_buftag_lite_t *)(bt))->bt_history; \ 236 pc_t *_e; \ 237 /* memmove() the old entries down one notch */ \ 238 for (_e = &_s[(count) - 1]; _e > _s; _e--) \ 239 *_e = *(_e - 1); \ 240 *_s = (uintptr_t)(caller); \ 241 } 242 243 #define KMERR_MODIFIED 0 /* buffer modified while on freelist */ 244 #define KMERR_REDZONE 1 /* redzone violation (write past end of buf) */ 245 #define KMERR_DUPFREE 2 /* freed a buffer twice */ 246 #define KMERR_BADADDR 3 /* freed a bad (unallocated) address */ 247 #define KMERR_BADBUFTAG 4 /* buftag corrupted */ 248 #define KMERR_BADBUFCTL 5 /* bufctl corrupted */ 249 #define KMERR_BADCACHE 6 /* freed a buffer to the wrong cache */ 250 #define KMERR_BADSIZE 7 /* alloc size != free size */ 251 #define KMERR_BADBASE 8 /* buffer base address wrong */ 252 253 struct { 254 hrtime_t kmp_timestamp; /* timestamp of panic */ 255 int kmp_error; /* type of kmem error */ 256 void *kmp_buffer; /* buffer that induced panic */ 257 void *kmp_realbuf; /* real start address for buffer */ 258 kmem_cache_t *kmp_cache; /* buffer's cache according to client */ 259 kmem_cache_t *kmp_realcache; /* actual cache containing buffer */ 260 kmem_slab_t *kmp_slab; /* slab accoring to kmem_findslab() */ 261 kmem_bufctl_t *kmp_bufctl; /* bufctl */ 262 } kmem_panic_info; 263 264 265 static void 266 copy_pattern(uint64_t pattern, void *buf_arg, size_t size) 267 { 268 uint64_t *bufend = (uint64_t *)((char *)buf_arg + size); 269 uint64_t *buf = buf_arg; 270 271 while (buf < bufend) 272 *buf++ = pattern; 273 } 274 275 static void * 276 verify_pattern(uint64_t pattern, void *buf_arg, size_t size) 277 { 278 uint64_t *bufend = (uint64_t *)((char *)buf_arg + size); 279 uint64_t *buf; 280 281 for (buf = buf_arg; buf < bufend; buf++) 282 if (*buf != pattern) 283 return (buf); 284 return (NULL); 285 } 286 287 static void * 288 verify_and_copy_pattern(uint64_t old, uint64_t new, void *buf_arg, size_t size) 289 { 290 uint64_t *bufend = (uint64_t *)((char *)buf_arg + size); 291 uint64_t *buf; 292 293 for (buf = buf_arg; buf < bufend; buf++) { 294 if (*buf != old) { 295 copy_pattern(old, buf_arg, 296 (char *)buf - (char *)buf_arg); 297 return (buf); 298 } 299 *buf = new; 300 } 301 302 return (NULL); 303 } 304 305 static void 306 kmem_cache_applyall(void (*func)(kmem_cache_t *), taskq_t *tq, int tqflag) 307 { 308 kmem_cache_t *cp; 309 310 mutex_enter(&kmem_cache_lock); 311 for (cp = kmem_null_cache.cache_next; cp != &kmem_null_cache; 312 cp = cp->cache_next) 313 if (tq != NULL) 314 (void) taskq_dispatch(tq, (task_func_t *)func, cp, 315 tqflag); 316 else 317 func(cp); 318 mutex_exit(&kmem_cache_lock); 319 } 320 321 static void 322 kmem_cache_applyall_id(void (*func)(kmem_cache_t *), taskq_t *tq, int tqflag) 323 { 324 kmem_cache_t *cp; 325 326 mutex_enter(&kmem_cache_lock); 327 for (cp = kmem_null_cache.cache_next; cp != &kmem_null_cache; 328 cp = cp->cache_next) { 329 if (!(cp->cache_cflags & KMC_IDENTIFIER)) 330 continue; 331 if (tq != NULL) 332 (void) taskq_dispatch(tq, (task_func_t *)func, cp, 333 tqflag); 334 else 335 func(cp); 336 } 337 mutex_exit(&kmem_cache_lock); 338 } 339 340 /* 341 * Debugging support. Given a buffer address, find its slab. 342 */ 343 static kmem_slab_t * 344 kmem_findslab(kmem_cache_t *cp, void *buf) 345 { 346 kmem_slab_t *sp; 347 348 mutex_enter(&cp->cache_lock); 349 for (sp = cp->cache_nullslab.slab_next; 350 sp != &cp->cache_nullslab; sp = sp->slab_next) { 351 if (KMEM_SLAB_MEMBER(sp, buf)) { 352 mutex_exit(&cp->cache_lock); 353 return (sp); 354 } 355 } 356 mutex_exit(&cp->cache_lock); 357 358 return (NULL); 359 } 360 361 static void 362 kmem_error(int error, kmem_cache_t *cparg, void *bufarg) 363 { 364 kmem_buftag_t *btp = NULL; 365 kmem_bufctl_t *bcp = NULL; 366 kmem_cache_t *cp = cparg; 367 kmem_slab_t *sp; 368 uint64_t *off; 369 void *buf = bufarg; 370 371 kmem_logging = 0; /* stop logging when a bad thing happens */ 372 373 kmem_panic_info.kmp_timestamp = gethrtime(); 374 375 sp = kmem_findslab(cp, buf); 376 if (sp == NULL) { 377 for (cp = kmem_null_cache.cache_prev; cp != &kmem_null_cache; 378 cp = cp->cache_prev) { 379 if ((sp = kmem_findslab(cp, buf)) != NULL) 380 break; 381 } 382 } 383 384 if (sp == NULL) { 385 cp = NULL; 386 error = KMERR_BADADDR; 387 } else { 388 if (cp != cparg) 389 error = KMERR_BADCACHE; 390 else 391 buf = (char *)bufarg - ((uintptr_t)bufarg - 392 (uintptr_t)sp->slab_base) % cp->cache_chunksize; 393 if (buf != bufarg) 394 error = KMERR_BADBASE; 395 if (cp->cache_flags & KMF_BUFTAG) 396 btp = KMEM_BUFTAG(cp, buf); 397 if (cp->cache_flags & KMF_HASH) { 398 mutex_enter(&cp->cache_lock); 399 for (bcp = *KMEM_HASH(cp, buf); bcp; bcp = bcp->bc_next) 400 if (bcp->bc_addr == buf) 401 break; 402 mutex_exit(&cp->cache_lock); 403 if (bcp == NULL && btp != NULL) 404 bcp = btp->bt_bufctl; 405 if (kmem_findslab(cp->cache_bufctl_cache, bcp) == 406 NULL || P2PHASE((uintptr_t)bcp, KMEM_ALIGN) || 407 bcp->bc_addr != buf) { 408 error = KMERR_BADBUFCTL; 409 bcp = NULL; 410 } 411 } 412 } 413 414 kmem_panic_info.kmp_error = error; 415 kmem_panic_info.kmp_buffer = bufarg; 416 kmem_panic_info.kmp_realbuf = buf; 417 kmem_panic_info.kmp_cache = cparg; 418 kmem_panic_info.kmp_realcache = cp; 419 kmem_panic_info.kmp_slab = sp; 420 kmem_panic_info.kmp_bufctl = bcp; 421 422 printf("kernel memory allocator: "); 423 424 switch (error) { 425 426 case KMERR_MODIFIED: 427 printf("buffer modified after being freed\n"); 428 off = verify_pattern(KMEM_FREE_PATTERN, buf, cp->cache_verify); 429 if (off == NULL) /* shouldn't happen */ 430 off = buf; 431 printf("modification occurred at offset 0x%lx " 432 "(0x%llx replaced by 0x%llx)\n", 433 (uintptr_t)off - (uintptr_t)buf, 434 (longlong_t)KMEM_FREE_PATTERN, (longlong_t)*off); 435 break; 436 437 case KMERR_REDZONE: 438 printf("redzone violation: write past end of buffer\n"); 439 break; 440 441 case KMERR_BADADDR: 442 printf("invalid free: buffer not in cache\n"); 443 break; 444 445 case KMERR_DUPFREE: 446 printf("duplicate free: buffer freed twice\n"); 447 break; 448 449 case KMERR_BADBUFTAG: 450 printf("boundary tag corrupted\n"); 451 printf("bcp ^ bxstat = %lx, should be %lx\n", 452 (intptr_t)btp->bt_bufctl ^ btp->bt_bxstat, 453 KMEM_BUFTAG_FREE); 454 break; 455 456 case KMERR_BADBUFCTL: 457 printf("bufctl corrupted\n"); 458 break; 459 460 case KMERR_BADCACHE: 461 printf("buffer freed to wrong cache\n"); 462 printf("buffer was allocated from %s,\n", cp->cache_name); 463 printf("caller attempting free to %s.\n", cparg->cache_name); 464 break; 465 466 case KMERR_BADSIZE: 467 printf("bad free: free size (%u) != alloc size (%u)\n", 468 KMEM_SIZE_DECODE(((uint32_t *)btp)[0]), 469 KMEM_SIZE_DECODE(((uint32_t *)btp)[1])); 470 break; 471 472 case KMERR_BADBASE: 473 printf("bad free: free address (%p) != alloc address (%p)\n", 474 bufarg, buf); 475 break; 476 } 477 478 printf("buffer=%p bufctl=%p cache: %s\n", 479 bufarg, (void *)bcp, cparg->cache_name); 480 481 if (bcp != NULL && (cp->cache_flags & KMF_AUDIT) && 482 error != KMERR_BADBUFCTL) { 483 int d; 484 timestruc_t ts; 485 kmem_bufctl_audit_t *bcap = (kmem_bufctl_audit_t *)bcp; 486 487 hrt2ts(kmem_panic_info.kmp_timestamp - bcap->bc_timestamp, &ts); 488 printf("previous transaction on buffer %p:\n", buf); 489 printf("thread=%p time=T-%ld.%09ld slab=%p cache: %s\n", 490 (void *)bcap->bc_thread, ts.tv_sec, ts.tv_nsec, 491 (void *)sp, cp->cache_name); 492 for (d = 0; d < MIN(bcap->bc_depth, KMEM_STACK_DEPTH); d++) { 493 ulong_t off; 494 char *sym = kobj_getsymname(bcap->bc_stack[d], &off); 495 printf("%s+%lx\n", sym ? sym : "?", off); 496 } 497 } 498 if (kmem_panic > 0) 499 panic("kernel heap corruption detected"); 500 if (kmem_panic == 0) 501 debug_enter(NULL); 502 kmem_logging = 1; /* resume logging */ 503 } 504 505 static kmem_log_header_t * 506 kmem_log_init(size_t logsize) 507 { 508 kmem_log_header_t *lhp; 509 int nchunks = 4 * max_ncpus; 510 size_t lhsize = (size_t)&((kmem_log_header_t *)0)->lh_cpu[max_ncpus]; 511 int i; 512 513 /* 514 * Make sure that lhp->lh_cpu[] is nicely aligned 515 * to prevent false sharing of cache lines. 516 */ 517 lhsize = P2ROUNDUP(lhsize, KMEM_ALIGN); 518 lhp = vmem_xalloc(kmem_log_arena, lhsize, 64, P2NPHASE(lhsize, 64), 0, 519 NULL, NULL, VM_SLEEP); 520 bzero(lhp, lhsize); 521 522 mutex_init(&lhp->lh_lock, NULL, MUTEX_DEFAULT, NULL); 523 lhp->lh_nchunks = nchunks; 524 lhp->lh_chunksize = P2ROUNDUP(logsize / nchunks + 1, PAGESIZE); 525 lhp->lh_base = vmem_alloc(kmem_log_arena, 526 lhp->lh_chunksize * nchunks, VM_SLEEP); 527 lhp->lh_free = vmem_alloc(kmem_log_arena, 528 nchunks * sizeof (int), VM_SLEEP); 529 bzero(lhp->lh_base, lhp->lh_chunksize * nchunks); 530 531 for (i = 0; i < max_ncpus; i++) { 532 kmem_cpu_log_header_t *clhp = &lhp->lh_cpu[i]; 533 mutex_init(&clhp->clh_lock, NULL, MUTEX_DEFAULT, NULL); 534 clhp->clh_chunk = i; 535 } 536 537 for (i = max_ncpus; i < nchunks; i++) 538 lhp->lh_free[i] = i; 539 540 lhp->lh_head = max_ncpus; 541 lhp->lh_tail = 0; 542 543 return (lhp); 544 } 545 546 static void * 547 kmem_log_enter(kmem_log_header_t *lhp, void *data, size_t size) 548 { 549 void *logspace; 550 kmem_cpu_log_header_t *clhp = &lhp->lh_cpu[CPU->cpu_seqid]; 551 552 if (lhp == NULL || kmem_logging == 0 || panicstr) 553 return (NULL); 554 555 mutex_enter(&clhp->clh_lock); 556 clhp->clh_hits++; 557 if (size > clhp->clh_avail) { 558 mutex_enter(&lhp->lh_lock); 559 lhp->lh_hits++; 560 lhp->lh_free[lhp->lh_tail] = clhp->clh_chunk; 561 lhp->lh_tail = (lhp->lh_tail + 1) % lhp->lh_nchunks; 562 clhp->clh_chunk = lhp->lh_free[lhp->lh_head]; 563 lhp->lh_head = (lhp->lh_head + 1) % lhp->lh_nchunks; 564 clhp->clh_current = lhp->lh_base + 565 clhp->clh_chunk * lhp->lh_chunksize; 566 clhp->clh_avail = lhp->lh_chunksize; 567 if (size > lhp->lh_chunksize) 568 size = lhp->lh_chunksize; 569 mutex_exit(&lhp->lh_lock); 570 } 571 logspace = clhp->clh_current; 572 clhp->clh_current += size; 573 clhp->clh_avail -= size; 574 bcopy(data, logspace, size); 575 mutex_exit(&clhp->clh_lock); 576 return (logspace); 577 } 578 579 #define KMEM_AUDIT(lp, cp, bcp) \ 580 { \ 581 kmem_bufctl_audit_t *_bcp = (kmem_bufctl_audit_t *)(bcp); \ 582 _bcp->bc_timestamp = gethrtime(); \ 583 _bcp->bc_thread = curthread; \ 584 _bcp->bc_depth = getpcstack(_bcp->bc_stack, KMEM_STACK_DEPTH); \ 585 _bcp->bc_lastlog = kmem_log_enter((lp), _bcp, sizeof (*_bcp)); \ 586 } 587 588 static void 589 kmem_log_event(kmem_log_header_t *lp, kmem_cache_t *cp, 590 kmem_slab_t *sp, void *addr) 591 { 592 kmem_bufctl_audit_t bca; 593 594 bzero(&bca, sizeof (kmem_bufctl_audit_t)); 595 bca.bc_addr = addr; 596 bca.bc_slab = sp; 597 bca.bc_cache = cp; 598 KMEM_AUDIT(lp, cp, &bca); 599 } 600 601 /* 602 * Create a new slab for cache cp. 603 */ 604 static kmem_slab_t * 605 kmem_slab_create(kmem_cache_t *cp, int kmflag) 606 { 607 size_t slabsize = cp->cache_slabsize; 608 size_t chunksize = cp->cache_chunksize; 609 int cache_flags = cp->cache_flags; 610 size_t color, chunks; 611 char *buf, *slab; 612 kmem_slab_t *sp; 613 kmem_bufctl_t *bcp; 614 vmem_t *vmp = cp->cache_arena; 615 616 color = cp->cache_color + cp->cache_align; 617 if (color > cp->cache_maxcolor) 618 color = cp->cache_mincolor; 619 cp->cache_color = color; 620 621 slab = vmem_alloc(vmp, slabsize, kmflag & KM_VMFLAGS); 622 623 if (slab == NULL) 624 goto vmem_alloc_failure; 625 626 ASSERT(P2PHASE((uintptr_t)slab, vmp->vm_quantum) == 0); 627 628 if (!(cp->cache_cflags & KMC_NOTOUCH)) 629 copy_pattern(KMEM_UNINITIALIZED_PATTERN, slab, slabsize); 630 631 if (cache_flags & KMF_HASH) { 632 if ((sp = kmem_cache_alloc(kmem_slab_cache, kmflag)) == NULL) 633 goto slab_alloc_failure; 634 chunks = (slabsize - color) / chunksize; 635 } else { 636 sp = KMEM_SLAB(cp, slab); 637 chunks = (slabsize - sizeof (kmem_slab_t) - color) / chunksize; 638 } 639 640 sp->slab_cache = cp; 641 sp->slab_head = NULL; 642 sp->slab_refcnt = 0; 643 sp->slab_base = buf = slab + color; 644 sp->slab_chunks = chunks; 645 646 ASSERT(chunks > 0); 647 while (chunks-- != 0) { 648 if (cache_flags & KMF_HASH) { 649 bcp = kmem_cache_alloc(cp->cache_bufctl_cache, kmflag); 650 if (bcp == NULL) 651 goto bufctl_alloc_failure; 652 if (cache_flags & KMF_AUDIT) { 653 kmem_bufctl_audit_t *bcap = 654 (kmem_bufctl_audit_t *)bcp; 655 bzero(bcap, sizeof (kmem_bufctl_audit_t)); 656 bcap->bc_cache = cp; 657 } 658 bcp->bc_addr = buf; 659 bcp->bc_slab = sp; 660 } else { 661 bcp = KMEM_BUFCTL(cp, buf); 662 } 663 if (cache_flags & KMF_BUFTAG) { 664 kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf); 665 btp->bt_redzone = KMEM_REDZONE_PATTERN; 666 btp->bt_bufctl = bcp; 667 btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_FREE; 668 if (cache_flags & KMF_DEADBEEF) { 669 copy_pattern(KMEM_FREE_PATTERN, buf, 670 cp->cache_verify); 671 } 672 } 673 bcp->bc_next = sp->slab_head; 674 sp->slab_head = bcp; 675 buf += chunksize; 676 } 677 678 kmem_log_event(kmem_slab_log, cp, sp, slab); 679 680 return (sp); 681 682 bufctl_alloc_failure: 683 684 while ((bcp = sp->slab_head) != NULL) { 685 sp->slab_head = bcp->bc_next; 686 kmem_cache_free(cp->cache_bufctl_cache, bcp); 687 } 688 kmem_cache_free(kmem_slab_cache, sp); 689 690 slab_alloc_failure: 691 692 vmem_free(vmp, slab, slabsize); 693 694 vmem_alloc_failure: 695 696 kmem_log_event(kmem_failure_log, cp, NULL, NULL); 697 atomic_add_64(&cp->cache_alloc_fail, 1); 698 699 return (NULL); 700 } 701 702 /* 703 * Destroy a slab. 704 */ 705 static void 706 kmem_slab_destroy(kmem_cache_t *cp, kmem_slab_t *sp) 707 { 708 vmem_t *vmp = cp->cache_arena; 709 void *slab = (void *)P2ALIGN((uintptr_t)sp->slab_base, vmp->vm_quantum); 710 711 if (cp->cache_flags & KMF_HASH) { 712 kmem_bufctl_t *bcp; 713 while ((bcp = sp->slab_head) != NULL) { 714 sp->slab_head = bcp->bc_next; 715 kmem_cache_free(cp->cache_bufctl_cache, bcp); 716 } 717 kmem_cache_free(kmem_slab_cache, sp); 718 } 719 vmem_free(vmp, slab, cp->cache_slabsize); 720 } 721 722 /* 723 * Allocate a raw (unconstructed) buffer from cp's slab layer. 724 */ 725 static void * 726 kmem_slab_alloc(kmem_cache_t *cp, int kmflag) 727 { 728 kmem_bufctl_t *bcp, **hash_bucket; 729 kmem_slab_t *sp; 730 void *buf; 731 732 mutex_enter(&cp->cache_lock); 733 cp->cache_slab_alloc++; 734 sp = cp->cache_freelist; 735 ASSERT(sp->slab_cache == cp); 736 if (sp->slab_head == NULL) { 737 /* 738 * The freelist is empty. Create a new slab. 739 */ 740 mutex_exit(&cp->cache_lock); 741 if ((sp = kmem_slab_create(cp, kmflag)) == NULL) 742 return (NULL); 743 mutex_enter(&cp->cache_lock); 744 cp->cache_slab_create++; 745 if ((cp->cache_buftotal += sp->slab_chunks) > cp->cache_bufmax) 746 cp->cache_bufmax = cp->cache_buftotal; 747 sp->slab_next = cp->cache_freelist; 748 sp->slab_prev = cp->cache_freelist->slab_prev; 749 sp->slab_next->slab_prev = sp; 750 sp->slab_prev->slab_next = sp; 751 cp->cache_freelist = sp; 752 } 753 754 sp->slab_refcnt++; 755 ASSERT(sp->slab_refcnt <= sp->slab_chunks); 756 757 /* 758 * If we're taking the last buffer in the slab, 759 * remove the slab from the cache's freelist. 760 */ 761 bcp = sp->slab_head; 762 if ((sp->slab_head = bcp->bc_next) == NULL) { 763 cp->cache_freelist = sp->slab_next; 764 ASSERT(sp->slab_refcnt == sp->slab_chunks); 765 } 766 767 if (cp->cache_flags & KMF_HASH) { 768 /* 769 * Add buffer to allocated-address hash table. 770 */ 771 buf = bcp->bc_addr; 772 hash_bucket = KMEM_HASH(cp, buf); 773 bcp->bc_next = *hash_bucket; 774 *hash_bucket = bcp; 775 if ((cp->cache_flags & (KMF_AUDIT | KMF_BUFTAG)) == KMF_AUDIT) { 776 KMEM_AUDIT(kmem_transaction_log, cp, bcp); 777 } 778 } else { 779 buf = KMEM_BUF(cp, bcp); 780 } 781 782 ASSERT(KMEM_SLAB_MEMBER(sp, buf)); 783 784 mutex_exit(&cp->cache_lock); 785 786 return (buf); 787 } 788 789 /* 790 * Free a raw (unconstructed) buffer to cp's slab layer. 791 */ 792 static void 793 kmem_slab_free(kmem_cache_t *cp, void *buf) 794 { 795 kmem_slab_t *sp; 796 kmem_bufctl_t *bcp, **prev_bcpp; 797 798 ASSERT(buf != NULL); 799 800 mutex_enter(&cp->cache_lock); 801 cp->cache_slab_free++; 802 803 if (cp->cache_flags & KMF_HASH) { 804 /* 805 * Look up buffer in allocated-address hash table. 806 */ 807 prev_bcpp = KMEM_HASH(cp, buf); 808 while ((bcp = *prev_bcpp) != NULL) { 809 if (bcp->bc_addr == buf) { 810 *prev_bcpp = bcp->bc_next; 811 sp = bcp->bc_slab; 812 break; 813 } 814 cp->cache_lookup_depth++; 815 prev_bcpp = &bcp->bc_next; 816 } 817 } else { 818 bcp = KMEM_BUFCTL(cp, buf); 819 sp = KMEM_SLAB(cp, buf); 820 } 821 822 if (bcp == NULL || sp->slab_cache != cp || !KMEM_SLAB_MEMBER(sp, buf)) { 823 mutex_exit(&cp->cache_lock); 824 kmem_error(KMERR_BADADDR, cp, buf); 825 return; 826 } 827 828 if ((cp->cache_flags & (KMF_AUDIT | KMF_BUFTAG)) == KMF_AUDIT) { 829 if (cp->cache_flags & KMF_CONTENTS) 830 ((kmem_bufctl_audit_t *)bcp)->bc_contents = 831 kmem_log_enter(kmem_content_log, buf, 832 cp->cache_contents); 833 KMEM_AUDIT(kmem_transaction_log, cp, bcp); 834 } 835 836 /* 837 * If this slab isn't currently on the freelist, put it there. 838 */ 839 if (sp->slab_head == NULL) { 840 ASSERT(sp->slab_refcnt == sp->slab_chunks); 841 ASSERT(cp->cache_freelist != sp); 842 sp->slab_next->slab_prev = sp->slab_prev; 843 sp->slab_prev->slab_next = sp->slab_next; 844 sp->slab_next = cp->cache_freelist; 845 sp->slab_prev = cp->cache_freelist->slab_prev; 846 sp->slab_next->slab_prev = sp; 847 sp->slab_prev->slab_next = sp; 848 cp->cache_freelist = sp; 849 } 850 851 bcp->bc_next = sp->slab_head; 852 sp->slab_head = bcp; 853 854 ASSERT(sp->slab_refcnt >= 1); 855 if (--sp->slab_refcnt == 0) { 856 /* 857 * There are no outstanding allocations from this slab, 858 * so we can reclaim the memory. 859 */ 860 sp->slab_next->slab_prev = sp->slab_prev; 861 sp->slab_prev->slab_next = sp->slab_next; 862 if (sp == cp->cache_freelist) 863 cp->cache_freelist = sp->slab_next; 864 cp->cache_slab_destroy++; 865 cp->cache_buftotal -= sp->slab_chunks; 866 mutex_exit(&cp->cache_lock); 867 kmem_slab_destroy(cp, sp); 868 return; 869 } 870 mutex_exit(&cp->cache_lock); 871 } 872 873 static int 874 kmem_cache_alloc_debug(kmem_cache_t *cp, void *buf, int kmflag, int construct, 875 caddr_t caller) 876 { 877 kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf); 878 kmem_bufctl_audit_t *bcp = (kmem_bufctl_audit_t *)btp->bt_bufctl; 879 uint32_t mtbf; 880 881 if (btp->bt_bxstat != ((intptr_t)bcp ^ KMEM_BUFTAG_FREE)) { 882 kmem_error(KMERR_BADBUFTAG, cp, buf); 883 return (-1); 884 } 885 886 btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_ALLOC; 887 888 if ((cp->cache_flags & KMF_HASH) && bcp->bc_addr != buf) { 889 kmem_error(KMERR_BADBUFCTL, cp, buf); 890 return (-1); 891 } 892 893 if (cp->cache_flags & KMF_DEADBEEF) { 894 if (!construct && (cp->cache_flags & KMF_LITE)) { 895 if (*(uint64_t *)buf != KMEM_FREE_PATTERN) { 896 kmem_error(KMERR_MODIFIED, cp, buf); 897 return (-1); 898 } 899 if (cp->cache_constructor != NULL) 900 *(uint64_t *)buf = btp->bt_redzone; 901 else 902 *(uint64_t *)buf = KMEM_UNINITIALIZED_PATTERN; 903 } else { 904 construct = 1; 905 if (verify_and_copy_pattern(KMEM_FREE_PATTERN, 906 KMEM_UNINITIALIZED_PATTERN, buf, 907 cp->cache_verify)) { 908 kmem_error(KMERR_MODIFIED, cp, buf); 909 return (-1); 910 } 911 } 912 } 913 btp->bt_redzone = KMEM_REDZONE_PATTERN; 914 915 if ((mtbf = kmem_mtbf | cp->cache_mtbf) != 0 && 916 gethrtime() % mtbf == 0 && 917 (kmflag & (KM_NOSLEEP | KM_PANIC)) == KM_NOSLEEP) { 918 kmem_log_event(kmem_failure_log, cp, NULL, NULL); 919 if (!construct && cp->cache_destructor != NULL) 920 cp->cache_destructor(buf, cp->cache_private); 921 } else { 922 mtbf = 0; 923 } 924 925 if (mtbf || (construct && cp->cache_constructor != NULL && 926 cp->cache_constructor(buf, cp->cache_private, kmflag) != 0)) { 927 atomic_add_64(&cp->cache_alloc_fail, 1); 928 btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_FREE; 929 if (cp->cache_flags & KMF_DEADBEEF) 930 copy_pattern(KMEM_FREE_PATTERN, buf, cp->cache_verify); 931 kmem_slab_free(cp, buf); 932 return (-1); 933 } 934 935 if (cp->cache_flags & KMF_AUDIT) { 936 KMEM_AUDIT(kmem_transaction_log, cp, bcp); 937 } 938 939 if ((cp->cache_flags & KMF_LITE) && 940 !(cp->cache_cflags & KMC_KMEM_ALLOC)) { 941 KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count, caller); 942 } 943 944 return (0); 945 } 946 947 static int 948 kmem_cache_free_debug(kmem_cache_t *cp, void *buf, caddr_t caller) 949 { 950 kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf); 951 kmem_bufctl_audit_t *bcp = (kmem_bufctl_audit_t *)btp->bt_bufctl; 952 kmem_slab_t *sp; 953 954 if (btp->bt_bxstat != ((intptr_t)bcp ^ KMEM_BUFTAG_ALLOC)) { 955 if (btp->bt_bxstat == ((intptr_t)bcp ^ KMEM_BUFTAG_FREE)) { 956 kmem_error(KMERR_DUPFREE, cp, buf); 957 return (-1); 958 } 959 sp = kmem_findslab(cp, buf); 960 if (sp == NULL || sp->slab_cache != cp) 961 kmem_error(KMERR_BADADDR, cp, buf); 962 else 963 kmem_error(KMERR_REDZONE, cp, buf); 964 return (-1); 965 } 966 967 btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_FREE; 968 969 if ((cp->cache_flags & KMF_HASH) && bcp->bc_addr != buf) { 970 kmem_error(KMERR_BADBUFCTL, cp, buf); 971 return (-1); 972 } 973 974 if (btp->bt_redzone != KMEM_REDZONE_PATTERN) { 975 kmem_error(KMERR_REDZONE, cp, buf); 976 return (-1); 977 } 978 979 if (cp->cache_flags & KMF_AUDIT) { 980 if (cp->cache_flags & KMF_CONTENTS) 981 bcp->bc_contents = kmem_log_enter(kmem_content_log, 982 buf, cp->cache_contents); 983 KMEM_AUDIT(kmem_transaction_log, cp, bcp); 984 } 985 986 if ((cp->cache_flags & KMF_LITE) && 987 !(cp->cache_cflags & KMC_KMEM_ALLOC)) { 988 KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count, caller); 989 } 990 991 if (cp->cache_flags & KMF_DEADBEEF) { 992 if (cp->cache_flags & KMF_LITE) 993 btp->bt_redzone = *(uint64_t *)buf; 994 else if (cp->cache_destructor != NULL) 995 cp->cache_destructor(buf, cp->cache_private); 996 997 copy_pattern(KMEM_FREE_PATTERN, buf, cp->cache_verify); 998 } 999 1000 return (0); 1001 } 1002 1003 /* 1004 * Free each object in magazine mp to cp's slab layer, and free mp itself. 1005 */ 1006 static void 1007 kmem_magazine_destroy(kmem_cache_t *cp, kmem_magazine_t *mp, int nrounds) 1008 { 1009 int round; 1010 1011 ASSERT(cp->cache_next == NULL || taskq_member(kmem_taskq, curthread)); 1012 1013 for (round = 0; round < nrounds; round++) { 1014 void *buf = mp->mag_round[round]; 1015 1016 if (cp->cache_flags & KMF_DEADBEEF) { 1017 if (verify_pattern(KMEM_FREE_PATTERN, buf, 1018 cp->cache_verify) != NULL) { 1019 kmem_error(KMERR_MODIFIED, cp, buf); 1020 continue; 1021 } 1022 if ((cp->cache_flags & KMF_LITE) && 1023 cp->cache_destructor != NULL) { 1024 kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf); 1025 *(uint64_t *)buf = btp->bt_redzone; 1026 cp->cache_destructor(buf, cp->cache_private); 1027 *(uint64_t *)buf = KMEM_FREE_PATTERN; 1028 } 1029 } else if (cp->cache_destructor != NULL) { 1030 cp->cache_destructor(buf, cp->cache_private); 1031 } 1032 1033 kmem_slab_free(cp, buf); 1034 } 1035 ASSERT(KMEM_MAGAZINE_VALID(cp, mp)); 1036 kmem_cache_free(cp->cache_magtype->mt_cache, mp); 1037 } 1038 1039 /* 1040 * Allocate a magazine from the depot. 1041 */ 1042 static kmem_magazine_t * 1043 kmem_depot_alloc(kmem_cache_t *cp, kmem_maglist_t *mlp) 1044 { 1045 kmem_magazine_t *mp; 1046 1047 /* 1048 * If we can't get the depot lock without contention, 1049 * update our contention count. We use the depot 1050 * contention rate to determine whether we need to 1051 * increase the magazine size for better scalability. 1052 */ 1053 if (!mutex_tryenter(&cp->cache_depot_lock)) { 1054 mutex_enter(&cp->cache_depot_lock); 1055 cp->cache_depot_contention++; 1056 } 1057 1058 if ((mp = mlp->ml_list) != NULL) { 1059 ASSERT(KMEM_MAGAZINE_VALID(cp, mp)); 1060 mlp->ml_list = mp->mag_next; 1061 if (--mlp->ml_total < mlp->ml_min) 1062 mlp->ml_min = mlp->ml_total; 1063 mlp->ml_alloc++; 1064 } 1065 1066 mutex_exit(&cp->cache_depot_lock); 1067 1068 return (mp); 1069 } 1070 1071 /* 1072 * Free a magazine to the depot. 1073 */ 1074 static void 1075 kmem_depot_free(kmem_cache_t *cp, kmem_maglist_t *mlp, kmem_magazine_t *mp) 1076 { 1077 mutex_enter(&cp->cache_depot_lock); 1078 ASSERT(KMEM_MAGAZINE_VALID(cp, mp)); 1079 mp->mag_next = mlp->ml_list; 1080 mlp->ml_list = mp; 1081 mlp->ml_total++; 1082 mutex_exit(&cp->cache_depot_lock); 1083 } 1084 1085 /* 1086 * Update the working set statistics for cp's depot. 1087 */ 1088 static void 1089 kmem_depot_ws_update(kmem_cache_t *cp) 1090 { 1091 mutex_enter(&cp->cache_depot_lock); 1092 cp->cache_full.ml_reaplimit = cp->cache_full.ml_min; 1093 cp->cache_full.ml_min = cp->cache_full.ml_total; 1094 cp->cache_empty.ml_reaplimit = cp->cache_empty.ml_min; 1095 cp->cache_empty.ml_min = cp->cache_empty.ml_total; 1096 mutex_exit(&cp->cache_depot_lock); 1097 } 1098 1099 /* 1100 * Reap all magazines that have fallen out of the depot's working set. 1101 */ 1102 static void 1103 kmem_depot_ws_reap(kmem_cache_t *cp) 1104 { 1105 long reap; 1106 kmem_magazine_t *mp; 1107 1108 ASSERT(cp->cache_next == NULL || taskq_member(kmem_taskq, curthread)); 1109 1110 reap = MIN(cp->cache_full.ml_reaplimit, cp->cache_full.ml_min); 1111 while (reap-- && (mp = kmem_depot_alloc(cp, &cp->cache_full)) != NULL) 1112 kmem_magazine_destroy(cp, mp, cp->cache_magtype->mt_magsize); 1113 1114 reap = MIN(cp->cache_empty.ml_reaplimit, cp->cache_empty.ml_min); 1115 while (reap-- && (mp = kmem_depot_alloc(cp, &cp->cache_empty)) != NULL) 1116 kmem_magazine_destroy(cp, mp, 0); 1117 } 1118 1119 static void 1120 kmem_cpu_reload(kmem_cpu_cache_t *ccp, kmem_magazine_t *mp, int rounds) 1121 { 1122 ASSERT((ccp->cc_loaded == NULL && ccp->cc_rounds == -1) || 1123 (ccp->cc_loaded && ccp->cc_rounds + rounds == ccp->cc_magsize)); 1124 ASSERT(ccp->cc_magsize > 0); 1125 1126 ccp->cc_ploaded = ccp->cc_loaded; 1127 ccp->cc_prounds = ccp->cc_rounds; 1128 ccp->cc_loaded = mp; 1129 ccp->cc_rounds = rounds; 1130 } 1131 1132 /* 1133 * Allocate a constructed object from cache cp. 1134 */ 1135 void * 1136 kmem_cache_alloc(kmem_cache_t *cp, int kmflag) 1137 { 1138 kmem_cpu_cache_t *ccp = KMEM_CPU_CACHE(cp); 1139 kmem_magazine_t *fmp; 1140 void *buf; 1141 1142 mutex_enter(&ccp->cc_lock); 1143 for (;;) { 1144 /* 1145 * If there's an object available in the current CPU's 1146 * loaded magazine, just take it and return. 1147 */ 1148 if (ccp->cc_rounds > 0) { 1149 buf = ccp->cc_loaded->mag_round[--ccp->cc_rounds]; 1150 ccp->cc_alloc++; 1151 mutex_exit(&ccp->cc_lock); 1152 if ((ccp->cc_flags & KMF_BUFTAG) && 1153 kmem_cache_alloc_debug(cp, buf, kmflag, 0, 1154 caller()) == -1) { 1155 if (kmflag & KM_NOSLEEP) 1156 return (NULL); 1157 mutex_enter(&ccp->cc_lock); 1158 continue; 1159 } 1160 return (buf); 1161 } 1162 1163 /* 1164 * The loaded magazine is empty. If the previously loaded 1165 * magazine was full, exchange them and try again. 1166 */ 1167 if (ccp->cc_prounds > 0) { 1168 kmem_cpu_reload(ccp, ccp->cc_ploaded, ccp->cc_prounds); 1169 continue; 1170 } 1171 1172 /* 1173 * If the magazine layer is disabled, break out now. 1174 */ 1175 if (ccp->cc_magsize == 0) 1176 break; 1177 1178 /* 1179 * Try to get a full magazine from the depot. 1180 */ 1181 fmp = kmem_depot_alloc(cp, &cp->cache_full); 1182 if (fmp != NULL) { 1183 if (ccp->cc_ploaded != NULL) 1184 kmem_depot_free(cp, &cp->cache_empty, 1185 ccp->cc_ploaded); 1186 kmem_cpu_reload(ccp, fmp, ccp->cc_magsize); 1187 continue; 1188 } 1189 1190 /* 1191 * There are no full magazines in the depot, 1192 * so fall through to the slab layer. 1193 */ 1194 break; 1195 } 1196 mutex_exit(&ccp->cc_lock); 1197 1198 /* 1199 * We couldn't allocate a constructed object from the magazine layer, 1200 * so get a raw buffer from the slab layer and apply its constructor. 1201 */ 1202 buf = kmem_slab_alloc(cp, kmflag); 1203 1204 if (buf == NULL) 1205 return (NULL); 1206 1207 if (cp->cache_flags & KMF_BUFTAG) { 1208 /* 1209 * Make kmem_cache_alloc_debug() apply the constructor for us. 1210 */ 1211 if (kmem_cache_alloc_debug(cp, buf, kmflag, 1, 1212 caller()) == -1) { 1213 if (kmflag & KM_NOSLEEP) 1214 return (NULL); 1215 /* 1216 * kmem_cache_alloc_debug() detected corruption 1217 * but didn't panic (kmem_panic <= 0). Try again. 1218 */ 1219 return (kmem_cache_alloc(cp, kmflag)); 1220 } 1221 return (buf); 1222 } 1223 1224 if (cp->cache_constructor != NULL && 1225 cp->cache_constructor(buf, cp->cache_private, kmflag) != 0) { 1226 atomic_add_64(&cp->cache_alloc_fail, 1); 1227 kmem_slab_free(cp, buf); 1228 return (NULL); 1229 } 1230 1231 return (buf); 1232 } 1233 1234 /* 1235 * Free a constructed object to cache cp. 1236 */ 1237 void 1238 kmem_cache_free(kmem_cache_t *cp, void *buf) 1239 { 1240 kmem_cpu_cache_t *ccp = KMEM_CPU_CACHE(cp); 1241 kmem_magazine_t *emp; 1242 kmem_magtype_t *mtp; 1243 1244 if (ccp->cc_flags & KMF_BUFTAG) 1245 if (kmem_cache_free_debug(cp, buf, caller()) == -1) 1246 return; 1247 1248 mutex_enter(&ccp->cc_lock); 1249 for (;;) { 1250 /* 1251 * If there's a slot available in the current CPU's 1252 * loaded magazine, just put the object there and return. 1253 */ 1254 if ((uint_t)ccp->cc_rounds < ccp->cc_magsize) { 1255 ccp->cc_loaded->mag_round[ccp->cc_rounds++] = buf; 1256 ccp->cc_free++; 1257 mutex_exit(&ccp->cc_lock); 1258 return; 1259 } 1260 1261 /* 1262 * The loaded magazine is full. If the previously loaded 1263 * magazine was empty, exchange them and try again. 1264 */ 1265 if (ccp->cc_prounds == 0) { 1266 kmem_cpu_reload(ccp, ccp->cc_ploaded, ccp->cc_prounds); 1267 continue; 1268 } 1269 1270 /* 1271 * If the magazine layer is disabled, break out now. 1272 */ 1273 if (ccp->cc_magsize == 0) 1274 break; 1275 1276 /* 1277 * Try to get an empty magazine from the depot. 1278 */ 1279 emp = kmem_depot_alloc(cp, &cp->cache_empty); 1280 if (emp != NULL) { 1281 if (ccp->cc_ploaded != NULL) 1282 kmem_depot_free(cp, &cp->cache_full, 1283 ccp->cc_ploaded); 1284 kmem_cpu_reload(ccp, emp, 0); 1285 continue; 1286 } 1287 1288 /* 1289 * There are no empty magazines in the depot, 1290 * so try to allocate a new one. We must drop all locks 1291 * across kmem_cache_alloc() because lower layers may 1292 * attempt to allocate from this cache. 1293 */ 1294 mtp = cp->cache_magtype; 1295 mutex_exit(&ccp->cc_lock); 1296 emp = kmem_cache_alloc(mtp->mt_cache, KM_NOSLEEP); 1297 mutex_enter(&ccp->cc_lock); 1298 1299 if (emp != NULL) { 1300 /* 1301 * We successfully allocated an empty magazine. 1302 * However, we had to drop ccp->cc_lock to do it, 1303 * so the cache's magazine size may have changed. 1304 * If so, free the magazine and try again. 1305 */ 1306 if (ccp->cc_magsize != mtp->mt_magsize) { 1307 mutex_exit(&ccp->cc_lock); 1308 kmem_cache_free(mtp->mt_cache, emp); 1309 mutex_enter(&ccp->cc_lock); 1310 continue; 1311 } 1312 1313 /* 1314 * We got a magazine of the right size. Add it to 1315 * the depot and try the whole dance again. 1316 */ 1317 kmem_depot_free(cp, &cp->cache_empty, emp); 1318 continue; 1319 } 1320 1321 /* 1322 * We couldn't allocate an empty magazine, 1323 * so fall through to the slab layer. 1324 */ 1325 break; 1326 } 1327 mutex_exit(&ccp->cc_lock); 1328 1329 /* 1330 * We couldn't free our constructed object to the magazine layer, 1331 * so apply its destructor and free it to the slab layer. 1332 * Note that if KMF_DEADBEEF is in effect and KMF_LITE is not, 1333 * kmem_cache_free_debug() will have already applied the destructor. 1334 */ 1335 if ((cp->cache_flags & (KMF_DEADBEEF | KMF_LITE)) != KMF_DEADBEEF && 1336 cp->cache_destructor != NULL) { 1337 if (cp->cache_flags & KMF_DEADBEEF) { /* KMF_LITE implied */ 1338 kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf); 1339 *(uint64_t *)buf = btp->bt_redzone; 1340 cp->cache_destructor(buf, cp->cache_private); 1341 *(uint64_t *)buf = KMEM_FREE_PATTERN; 1342 } else { 1343 cp->cache_destructor(buf, cp->cache_private); 1344 } 1345 } 1346 1347 kmem_slab_free(cp, buf); 1348 } 1349 1350 void * 1351 kmem_zalloc(size_t size, int kmflag) 1352 { 1353 size_t index = (size - 1) >> KMEM_ALIGN_SHIFT; 1354 void *buf; 1355 1356 if (index < KMEM_MAXBUF >> KMEM_ALIGN_SHIFT) { 1357 kmem_cache_t *cp = kmem_alloc_table[index]; 1358 buf = kmem_cache_alloc(cp, kmflag); 1359 if (buf != NULL) { 1360 if (cp->cache_flags & KMF_BUFTAG) { 1361 kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf); 1362 ((uint8_t *)buf)[size] = KMEM_REDZONE_BYTE; 1363 ((uint32_t *)btp)[1] = KMEM_SIZE_ENCODE(size); 1364 1365 if (cp->cache_flags & KMF_LITE) { 1366 KMEM_BUFTAG_LITE_ENTER(btp, 1367 kmem_lite_count, caller()); 1368 } 1369 } 1370 bzero(buf, size); 1371 } 1372 } else { 1373 buf = kmem_alloc(size, kmflag); 1374 if (buf != NULL) 1375 bzero(buf, size); 1376 } 1377 return (buf); 1378 } 1379 1380 void * 1381 kmem_alloc(size_t size, int kmflag) 1382 { 1383 size_t index = (size - 1) >> KMEM_ALIGN_SHIFT; 1384 void *buf; 1385 1386 if (index < KMEM_MAXBUF >> KMEM_ALIGN_SHIFT) { 1387 kmem_cache_t *cp = kmem_alloc_table[index]; 1388 buf = kmem_cache_alloc(cp, kmflag); 1389 if ((cp->cache_flags & KMF_BUFTAG) && buf != NULL) { 1390 kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf); 1391 ((uint8_t *)buf)[size] = KMEM_REDZONE_BYTE; 1392 ((uint32_t *)btp)[1] = KMEM_SIZE_ENCODE(size); 1393 1394 if (cp->cache_flags & KMF_LITE) { 1395 KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count, 1396 caller()); 1397 } 1398 } 1399 return (buf); 1400 } 1401 if (size == 0) 1402 return (NULL); 1403 buf = vmem_alloc(kmem_oversize_arena, size, kmflag & KM_VMFLAGS); 1404 if (buf == NULL) 1405 kmem_log_event(kmem_failure_log, NULL, NULL, (void *)size); 1406 return (buf); 1407 } 1408 1409 void 1410 kmem_free(void *buf, size_t size) 1411 { 1412 size_t index = (size - 1) >> KMEM_ALIGN_SHIFT; 1413 1414 if (index < KMEM_MAXBUF >> KMEM_ALIGN_SHIFT) { 1415 kmem_cache_t *cp = kmem_alloc_table[index]; 1416 if (cp->cache_flags & KMF_BUFTAG) { 1417 kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf); 1418 uint32_t *ip = (uint32_t *)btp; 1419 if (ip[1] != KMEM_SIZE_ENCODE(size)) { 1420 if (*(uint64_t *)buf == KMEM_FREE_PATTERN) { 1421 kmem_error(KMERR_DUPFREE, cp, buf); 1422 return; 1423 } 1424 if (KMEM_SIZE_VALID(ip[1])) { 1425 ip[0] = KMEM_SIZE_ENCODE(size); 1426 kmem_error(KMERR_BADSIZE, cp, buf); 1427 } else { 1428 kmem_error(KMERR_REDZONE, cp, buf); 1429 } 1430 return; 1431 } 1432 if (((uint8_t *)buf)[size] != KMEM_REDZONE_BYTE) { 1433 kmem_error(KMERR_REDZONE, cp, buf); 1434 return; 1435 } 1436 btp->bt_redzone = KMEM_REDZONE_PATTERN; 1437 if (cp->cache_flags & KMF_LITE) { 1438 KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count, 1439 caller()); 1440 } 1441 } 1442 kmem_cache_free(cp, buf); 1443 } else { 1444 if (buf == NULL && size == 0) 1445 return; 1446 vmem_free(kmem_oversize_arena, buf, size); 1447 } 1448 } 1449 1450 void * 1451 kmem_firewall_va_alloc(vmem_t *vmp, size_t size, int vmflag) 1452 { 1453 size_t realsize = size + vmp->vm_quantum; 1454 void *addr; 1455 1456 /* 1457 * Annoying edge case: if 'size' is just shy of ULONG_MAX, adding 1458 * vm_quantum will cause integer wraparound. Check for this, and 1459 * blow off the firewall page in this case. Note that such a 1460 * giant allocation (the entire kernel address space) can never 1461 * be satisfied, so it will either fail immediately (VM_NOSLEEP) 1462 * or sleep forever (VM_SLEEP). Thus, there is no need for a 1463 * corresponding check in kmem_firewall_va_free(). 1464 */ 1465 if (realsize < size) 1466 realsize = size; 1467 1468 /* 1469 * While boot still owns resource management, make sure that this 1470 * redzone virtual address allocation is properly accounted for in 1471 * OBPs "virtual-memory" "available" lists because we're 1472 * effectively claiming them for a red zone. If we don't do this, 1473 * the available lists become too fragmented and too large for the 1474 * current boot/kernel memory list interface. 1475 */ 1476 addr = vmem_alloc(vmp, realsize, vmflag | VM_NEXTFIT); 1477 1478 if (addr != NULL && kvseg.s_base == NULL && realsize != size) 1479 (void) boot_virt_alloc((char *)addr + size, vmp->vm_quantum); 1480 1481 return (addr); 1482 } 1483 1484 void 1485 kmem_firewall_va_free(vmem_t *vmp, void *addr, size_t size) 1486 { 1487 ASSERT((kvseg.s_base == NULL ? 1488 va_to_pfn((char *)addr + size) : 1489 hat_getpfnum(kas.a_hat, (caddr_t)addr + size)) == PFN_INVALID); 1490 1491 vmem_free(vmp, addr, size + vmp->vm_quantum); 1492 } 1493 1494 /* 1495 * Try to allocate at least `size' bytes of memory without sleeping or 1496 * panicking. Return actual allocated size in `asize'. If allocation failed, 1497 * try final allocation with sleep or panic allowed. 1498 */ 1499 void * 1500 kmem_alloc_tryhard(size_t size, size_t *asize, int kmflag) 1501 { 1502 void *p; 1503 1504 *asize = P2ROUNDUP(size, KMEM_ALIGN); 1505 do { 1506 p = kmem_alloc(*asize, (kmflag | KM_NOSLEEP) & ~KM_PANIC); 1507 if (p != NULL) 1508 return (p); 1509 *asize += KMEM_ALIGN; 1510 } while (*asize <= PAGESIZE); 1511 1512 *asize = P2ROUNDUP(size, KMEM_ALIGN); 1513 return (kmem_alloc(*asize, kmflag)); 1514 } 1515 1516 /* 1517 * Reclaim all unused memory from a cache. 1518 */ 1519 static void 1520 kmem_cache_reap(kmem_cache_t *cp) 1521 { 1522 /* 1523 * Ask the cache's owner to free some memory if possible. 1524 * The idea is to handle things like the inode cache, which 1525 * typically sits on a bunch of memory that it doesn't truly 1526 * *need*. Reclaim policy is entirely up to the owner; this 1527 * callback is just an advisory plea for help. 1528 */ 1529 if (cp->cache_reclaim != NULL) 1530 cp->cache_reclaim(cp->cache_private); 1531 1532 kmem_depot_ws_reap(cp); 1533 } 1534 1535 static void 1536 kmem_reap_timeout(void *flag_arg) 1537 { 1538 uint32_t *flag = (uint32_t *)flag_arg; 1539 1540 ASSERT(flag == &kmem_reaping || flag == &kmem_reaping_idspace); 1541 *flag = 0; 1542 } 1543 1544 static void 1545 kmem_reap_done(void *flag) 1546 { 1547 (void) timeout(kmem_reap_timeout, flag, kmem_reap_interval); 1548 } 1549 1550 static void 1551 kmem_reap_start(void *flag) 1552 { 1553 ASSERT(flag == &kmem_reaping || flag == &kmem_reaping_idspace); 1554 1555 if (flag == &kmem_reaping) { 1556 kmem_cache_applyall(kmem_cache_reap, kmem_taskq, TQ_NOSLEEP); 1557 /* 1558 * if we have segkp under heap, reap segkp cache. 1559 */ 1560 if (segkp_fromheap) 1561 segkp_cache_free(); 1562 } 1563 else 1564 kmem_cache_applyall_id(kmem_cache_reap, kmem_taskq, TQ_NOSLEEP); 1565 1566 /* 1567 * We use taskq_dispatch() to schedule a timeout to clear 1568 * the flag so that kmem_reap() becomes self-throttling: 1569 * we won't reap again until the current reap completes *and* 1570 * at least kmem_reap_interval ticks have elapsed. 1571 */ 1572 if (!taskq_dispatch(kmem_taskq, kmem_reap_done, flag, TQ_NOSLEEP)) 1573 kmem_reap_done(flag); 1574 } 1575 1576 static void 1577 kmem_reap_common(void *flag_arg) 1578 { 1579 uint32_t *flag = (uint32_t *)flag_arg; 1580 1581 if (MUTEX_HELD(&kmem_cache_lock) || kmem_taskq == NULL || 1582 cas32(flag, 0, 1) != 0) 1583 return; 1584 1585 /* 1586 * It may not be kosher to do memory allocation when a reap is called 1587 * is called (for example, if vmem_populate() is in the call chain). 1588 * So we start the reap going with a TQ_NOALLOC dispatch. If the 1589 * dispatch fails, we reset the flag, and the next reap will try again. 1590 */ 1591 if (!taskq_dispatch(kmem_taskq, kmem_reap_start, flag, TQ_NOALLOC)) 1592 *flag = 0; 1593 } 1594 1595 /* 1596 * Reclaim all unused memory from all caches. Called from the VM system 1597 * when memory gets tight. 1598 */ 1599 void 1600 kmem_reap(void) 1601 { 1602 kmem_reap_common(&kmem_reaping); 1603 } 1604 1605 /* 1606 * Reclaim all unused memory from identifier arenas, called when a vmem 1607 * arena not back by memory is exhausted. Since reaping memory-backed caches 1608 * cannot help with identifier exhaustion, we avoid both a large amount of 1609 * work and unwanted side-effects from reclaim callbacks. 1610 */ 1611 void 1612 kmem_reap_idspace(void) 1613 { 1614 kmem_reap_common(&kmem_reaping_idspace); 1615 } 1616 1617 /* 1618 * Purge all magazines from a cache and set its magazine limit to zero. 1619 * All calls are serialized by the kmem_taskq lock, except for the final 1620 * call from kmem_cache_destroy(). 1621 */ 1622 static void 1623 kmem_cache_magazine_purge(kmem_cache_t *cp) 1624 { 1625 kmem_cpu_cache_t *ccp; 1626 kmem_magazine_t *mp, *pmp; 1627 int rounds, prounds, cpu_seqid; 1628 1629 ASSERT(cp->cache_next == NULL || taskq_member(kmem_taskq, curthread)); 1630 ASSERT(MUTEX_NOT_HELD(&cp->cache_lock)); 1631 1632 for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) { 1633 ccp = &cp->cache_cpu[cpu_seqid]; 1634 1635 mutex_enter(&ccp->cc_lock); 1636 mp = ccp->cc_loaded; 1637 pmp = ccp->cc_ploaded; 1638 rounds = ccp->cc_rounds; 1639 prounds = ccp->cc_prounds; 1640 ccp->cc_loaded = NULL; 1641 ccp->cc_ploaded = NULL; 1642 ccp->cc_rounds = -1; 1643 ccp->cc_prounds = -1; 1644 ccp->cc_magsize = 0; 1645 mutex_exit(&ccp->cc_lock); 1646 1647 if (mp) 1648 kmem_magazine_destroy(cp, mp, rounds); 1649 if (pmp) 1650 kmem_magazine_destroy(cp, pmp, prounds); 1651 } 1652 1653 /* 1654 * Updating the working set statistics twice in a row has the 1655 * effect of setting the working set size to zero, so everything 1656 * is eligible for reaping. 1657 */ 1658 kmem_depot_ws_update(cp); 1659 kmem_depot_ws_update(cp); 1660 1661 kmem_depot_ws_reap(cp); 1662 } 1663 1664 /* 1665 * Enable per-cpu magazines on a cache. 1666 */ 1667 static void 1668 kmem_cache_magazine_enable(kmem_cache_t *cp) 1669 { 1670 int cpu_seqid; 1671 1672 if (cp->cache_flags & KMF_NOMAGAZINE) 1673 return; 1674 1675 for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) { 1676 kmem_cpu_cache_t *ccp = &cp->cache_cpu[cpu_seqid]; 1677 mutex_enter(&ccp->cc_lock); 1678 ccp->cc_magsize = cp->cache_magtype->mt_magsize; 1679 mutex_exit(&ccp->cc_lock); 1680 } 1681 1682 } 1683 1684 /* 1685 * Recompute a cache's magazine size. The trade-off is that larger magazines 1686 * provide a higher transfer rate with the depot, while smaller magazines 1687 * reduce memory consumption. Magazine resizing is an expensive operation; 1688 * it should not be done frequently. 1689 * 1690 * Changes to the magazine size are serialized by the kmem_taskq lock. 1691 * 1692 * Note: at present this only grows the magazine size. It might be useful 1693 * to allow shrinkage too. 1694 */ 1695 static void 1696 kmem_cache_magazine_resize(kmem_cache_t *cp) 1697 { 1698 kmem_magtype_t *mtp = cp->cache_magtype; 1699 1700 ASSERT(taskq_member(kmem_taskq, curthread)); 1701 1702 if (cp->cache_chunksize < mtp->mt_maxbuf) { 1703 kmem_cache_magazine_purge(cp); 1704 mutex_enter(&cp->cache_depot_lock); 1705 cp->cache_magtype = ++mtp; 1706 cp->cache_depot_contention_prev = 1707 cp->cache_depot_contention + INT_MAX; 1708 mutex_exit(&cp->cache_depot_lock); 1709 kmem_cache_magazine_enable(cp); 1710 } 1711 } 1712 1713 /* 1714 * Rescale a cache's hash table, so that the table size is roughly the 1715 * cache size. We want the average lookup time to be extremely small. 1716 */ 1717 static void 1718 kmem_hash_rescale(kmem_cache_t *cp) 1719 { 1720 kmem_bufctl_t **old_table, **new_table, *bcp; 1721 size_t old_size, new_size, h; 1722 1723 ASSERT(taskq_member(kmem_taskq, curthread)); 1724 1725 new_size = MAX(KMEM_HASH_INITIAL, 1726 1 << (highbit(3 * cp->cache_buftotal + 4) - 2)); 1727 old_size = cp->cache_hash_mask + 1; 1728 1729 if ((old_size >> 1) <= new_size && new_size <= (old_size << 1)) 1730 return; 1731 1732 new_table = vmem_alloc(kmem_hash_arena, new_size * sizeof (void *), 1733 VM_NOSLEEP); 1734 if (new_table == NULL) 1735 return; 1736 bzero(new_table, new_size * sizeof (void *)); 1737 1738 mutex_enter(&cp->cache_lock); 1739 1740 old_size = cp->cache_hash_mask + 1; 1741 old_table = cp->cache_hash_table; 1742 1743 cp->cache_hash_mask = new_size - 1; 1744 cp->cache_hash_table = new_table; 1745 cp->cache_rescale++; 1746 1747 for (h = 0; h < old_size; h++) { 1748 bcp = old_table[h]; 1749 while (bcp != NULL) { 1750 void *addr = bcp->bc_addr; 1751 kmem_bufctl_t *next_bcp = bcp->bc_next; 1752 kmem_bufctl_t **hash_bucket = KMEM_HASH(cp, addr); 1753 bcp->bc_next = *hash_bucket; 1754 *hash_bucket = bcp; 1755 bcp = next_bcp; 1756 } 1757 } 1758 1759 mutex_exit(&cp->cache_lock); 1760 1761 vmem_free(kmem_hash_arena, old_table, old_size * sizeof (void *)); 1762 } 1763 1764 /* 1765 * Perform periodic maintenance on a cache: hash rescaling, 1766 * depot working-set update, and magazine resizing. 1767 */ 1768 static void 1769 kmem_cache_update(kmem_cache_t *cp) 1770 { 1771 int need_hash_rescale = 0; 1772 int need_magazine_resize = 0; 1773 1774 ASSERT(MUTEX_HELD(&kmem_cache_lock)); 1775 1776 /* 1777 * If the cache has become much larger or smaller than its hash table, 1778 * fire off a request to rescale the hash table. 1779 */ 1780 mutex_enter(&cp->cache_lock); 1781 1782 if ((cp->cache_flags & KMF_HASH) && 1783 (cp->cache_buftotal > (cp->cache_hash_mask << 1) || 1784 (cp->cache_buftotal < (cp->cache_hash_mask >> 1) && 1785 cp->cache_hash_mask > KMEM_HASH_INITIAL))) 1786 need_hash_rescale = 1; 1787 1788 mutex_exit(&cp->cache_lock); 1789 1790 /* 1791 * Update the depot working set statistics. 1792 */ 1793 kmem_depot_ws_update(cp); 1794 1795 /* 1796 * If there's a lot of contention in the depot, 1797 * increase the magazine size. 1798 */ 1799 mutex_enter(&cp->cache_depot_lock); 1800 1801 if (cp->cache_chunksize < cp->cache_magtype->mt_maxbuf && 1802 (int)(cp->cache_depot_contention - 1803 cp->cache_depot_contention_prev) > kmem_depot_contention) 1804 need_magazine_resize = 1; 1805 1806 cp->cache_depot_contention_prev = cp->cache_depot_contention; 1807 1808 mutex_exit(&cp->cache_depot_lock); 1809 1810 if (need_hash_rescale) 1811 (void) taskq_dispatch(kmem_taskq, 1812 (task_func_t *)kmem_hash_rescale, cp, TQ_NOSLEEP); 1813 1814 if (need_magazine_resize) 1815 (void) taskq_dispatch(kmem_taskq, 1816 (task_func_t *)kmem_cache_magazine_resize, cp, TQ_NOSLEEP); 1817 } 1818 1819 static void 1820 kmem_update_timeout(void *dummy) 1821 { 1822 static void kmem_update(void *); 1823 1824 (void) timeout(kmem_update, dummy, kmem_reap_interval); 1825 } 1826 1827 static void 1828 kmem_update(void *dummy) 1829 { 1830 kmem_cache_applyall(kmem_cache_update, NULL, TQ_NOSLEEP); 1831 1832 /* 1833 * We use taskq_dispatch() to reschedule the timeout so that 1834 * kmem_update() becomes self-throttling: it won't schedule 1835 * new tasks until all previous tasks have completed. 1836 */ 1837 if (!taskq_dispatch(kmem_taskq, kmem_update_timeout, dummy, TQ_NOSLEEP)) 1838 kmem_update_timeout(NULL); 1839 } 1840 1841 static int 1842 kmem_cache_kstat_update(kstat_t *ksp, int rw) 1843 { 1844 struct kmem_cache_kstat *kmcp = &kmem_cache_kstat; 1845 kmem_cache_t *cp = ksp->ks_private; 1846 kmem_slab_t *sp; 1847 uint64_t cpu_buf_avail; 1848 uint64_t buf_avail = 0; 1849 int cpu_seqid; 1850 1851 ASSERT(MUTEX_HELD(&kmem_cache_kstat_lock)); 1852 1853 if (rw == KSTAT_WRITE) 1854 return (EACCES); 1855 1856 mutex_enter(&cp->cache_lock); 1857 1858 kmcp->kmc_alloc_fail.value.ui64 = cp->cache_alloc_fail; 1859 kmcp->kmc_alloc.value.ui64 = cp->cache_slab_alloc; 1860 kmcp->kmc_free.value.ui64 = cp->cache_slab_free; 1861 kmcp->kmc_slab_alloc.value.ui64 = cp->cache_slab_alloc; 1862 kmcp->kmc_slab_free.value.ui64 = cp->cache_slab_free; 1863 1864 for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) { 1865 kmem_cpu_cache_t *ccp = &cp->cache_cpu[cpu_seqid]; 1866 1867 mutex_enter(&ccp->cc_lock); 1868 1869 cpu_buf_avail = 0; 1870 if (ccp->cc_rounds > 0) 1871 cpu_buf_avail += ccp->cc_rounds; 1872 if (ccp->cc_prounds > 0) 1873 cpu_buf_avail += ccp->cc_prounds; 1874 1875 kmcp->kmc_alloc.value.ui64 += ccp->cc_alloc; 1876 kmcp->kmc_free.value.ui64 += ccp->cc_free; 1877 buf_avail += cpu_buf_avail; 1878 1879 mutex_exit(&ccp->cc_lock); 1880 } 1881 1882 mutex_enter(&cp->cache_depot_lock); 1883 1884 kmcp->kmc_depot_alloc.value.ui64 = cp->cache_full.ml_alloc; 1885 kmcp->kmc_depot_free.value.ui64 = cp->cache_empty.ml_alloc; 1886 kmcp->kmc_depot_contention.value.ui64 = cp->cache_depot_contention; 1887 kmcp->kmc_full_magazines.value.ui64 = cp->cache_full.ml_total; 1888 kmcp->kmc_empty_magazines.value.ui64 = cp->cache_empty.ml_total; 1889 kmcp->kmc_magazine_size.value.ui64 = 1890 (cp->cache_flags & KMF_NOMAGAZINE) ? 1891 0 : cp->cache_magtype->mt_magsize; 1892 1893 kmcp->kmc_alloc.value.ui64 += cp->cache_full.ml_alloc; 1894 kmcp->kmc_free.value.ui64 += cp->cache_empty.ml_alloc; 1895 buf_avail += cp->cache_full.ml_total * cp->cache_magtype->mt_magsize; 1896 1897 mutex_exit(&cp->cache_depot_lock); 1898 1899 kmcp->kmc_buf_size.value.ui64 = cp->cache_bufsize; 1900 kmcp->kmc_align.value.ui64 = cp->cache_align; 1901 kmcp->kmc_chunk_size.value.ui64 = cp->cache_chunksize; 1902 kmcp->kmc_slab_size.value.ui64 = cp->cache_slabsize; 1903 kmcp->kmc_buf_constructed.value.ui64 = buf_avail; 1904 for (sp = cp->cache_freelist; sp != &cp->cache_nullslab; 1905 sp = sp->slab_next) 1906 buf_avail += sp->slab_chunks - sp->slab_refcnt; 1907 kmcp->kmc_buf_avail.value.ui64 = buf_avail; 1908 kmcp->kmc_buf_inuse.value.ui64 = cp->cache_buftotal - buf_avail; 1909 kmcp->kmc_buf_total.value.ui64 = cp->cache_buftotal; 1910 kmcp->kmc_buf_max.value.ui64 = cp->cache_bufmax; 1911 kmcp->kmc_slab_create.value.ui64 = cp->cache_slab_create; 1912 kmcp->kmc_slab_destroy.value.ui64 = cp->cache_slab_destroy; 1913 kmcp->kmc_hash_size.value.ui64 = (cp->cache_flags & KMF_HASH) ? 1914 cp->cache_hash_mask + 1 : 0; 1915 kmcp->kmc_hash_lookup_depth.value.ui64 = cp->cache_lookup_depth; 1916 kmcp->kmc_hash_rescale.value.ui64 = cp->cache_rescale; 1917 kmcp->kmc_vmem_source.value.ui64 = cp->cache_arena->vm_id; 1918 1919 mutex_exit(&cp->cache_lock); 1920 return (0); 1921 } 1922 1923 /* 1924 * Return a named statistic about a particular cache. 1925 * This shouldn't be called very often, so it's currently designed for 1926 * simplicity (leverages existing kstat support) rather than efficiency. 1927 */ 1928 uint64_t 1929 kmem_cache_stat(kmem_cache_t *cp, char *name) 1930 { 1931 int i; 1932 kstat_t *ksp = cp->cache_kstat; 1933 kstat_named_t *knp = (kstat_named_t *)&kmem_cache_kstat; 1934 uint64_t value = 0; 1935 1936 if (ksp != NULL) { 1937 mutex_enter(&kmem_cache_kstat_lock); 1938 (void) kmem_cache_kstat_update(ksp, KSTAT_READ); 1939 for (i = 0; i < ksp->ks_ndata; i++) { 1940 if (strcmp(knp[i].name, name) == 0) { 1941 value = knp[i].value.ui64; 1942 break; 1943 } 1944 } 1945 mutex_exit(&kmem_cache_kstat_lock); 1946 } 1947 return (value); 1948 } 1949 1950 /* 1951 * Return an estimate of currently available kernel heap memory. 1952 * On 32-bit systems, physical memory may exceed virtual memory, 1953 * we just truncate the result at 1GB. 1954 */ 1955 size_t 1956 kmem_avail(void) 1957 { 1958 spgcnt_t rmem = availrmem - tune.t_minarmem; 1959 spgcnt_t fmem = freemem - minfree; 1960 1961 return ((size_t)ptob(MIN(MAX(MIN(rmem, fmem), 0), 1962 1 << (30 - PAGESHIFT)))); 1963 } 1964 1965 /* 1966 * Return the maximum amount of memory that is (in theory) allocatable 1967 * from the heap. This may be used as an estimate only since there 1968 * is no guarentee this space will still be available when an allocation 1969 * request is made, nor that the space may be allocated in one big request 1970 * due to kernel heap fragmentation. 1971 */ 1972 size_t 1973 kmem_maxavail(void) 1974 { 1975 spgcnt_t pmem = availrmem - tune.t_minarmem; 1976 spgcnt_t vmem = btop(vmem_size(heap_arena, VMEM_FREE)); 1977 1978 return ((size_t)ptob(MAX(MIN(pmem, vmem), 0))); 1979 } 1980 1981 kmem_cache_t * 1982 kmem_cache_create( 1983 char *name, /* descriptive name for this cache */ 1984 size_t bufsize, /* size of the objects it manages */ 1985 size_t align, /* required object alignment */ 1986 int (*constructor)(void *, void *, int), /* object constructor */ 1987 void (*destructor)(void *, void *), /* object destructor */ 1988 void (*reclaim)(void *), /* memory reclaim callback */ 1989 void *private, /* pass-thru arg for constr/destr/reclaim */ 1990 vmem_t *vmp, /* vmem source for slab allocation */ 1991 int cflags) /* cache creation flags */ 1992 { 1993 int cpu_seqid; 1994 size_t chunksize; 1995 kmem_cache_t *cp, *cnext, *cprev; 1996 kmem_magtype_t *mtp; 1997 size_t csize = KMEM_CACHE_SIZE(max_ncpus); 1998 1999 #ifdef DEBUG 2000 /* 2001 * Cache names should conform to the rules for valid C identifiers 2002 */ 2003 if (!strident_valid(name)) { 2004 cmn_err(CE_CONT, 2005 "kmem_cache_create: '%s' is an invalid cache name\n" 2006 "cache names must conform to the rules for " 2007 "C identifiers\n", name); 2008 } 2009 #endif /* DEBUG */ 2010 2011 if (vmp == NULL) 2012 vmp = kmem_default_arena; 2013 2014 /* 2015 * If this kmem cache has an identifier vmem arena as its source, mark 2016 * it such to allow kmem_reap_idspace(). 2017 */ 2018 ASSERT(!(cflags & KMC_IDENTIFIER)); /* consumer should not set this */ 2019 if (vmp->vm_cflags & VMC_IDENTIFIER) 2020 cflags |= KMC_IDENTIFIER; 2021 2022 /* 2023 * Get a kmem_cache structure. We arrange that cp->cache_cpu[] 2024 * is aligned on a KMEM_CPU_CACHE_SIZE boundary to prevent 2025 * false sharing of per-CPU data. 2026 */ 2027 cp = vmem_xalloc(kmem_cache_arena, csize, KMEM_CPU_CACHE_SIZE, 2028 P2NPHASE(csize, KMEM_CPU_CACHE_SIZE), 0, NULL, NULL, VM_SLEEP); 2029 bzero(cp, csize); 2030 2031 if (align == 0) 2032 align = KMEM_ALIGN; 2033 2034 /* 2035 * If we're not at least KMEM_ALIGN aligned, we can't use free 2036 * memory to hold bufctl information (because we can't safely 2037 * perform word loads and stores on it). 2038 */ 2039 if (align < KMEM_ALIGN) 2040 cflags |= KMC_NOTOUCH; 2041 2042 if ((align & (align - 1)) != 0 || align > vmp->vm_quantum) 2043 panic("kmem_cache_create: bad alignment %lu", align); 2044 2045 mutex_enter(&kmem_flags_lock); 2046 if (kmem_flags & KMF_RANDOMIZE) 2047 kmem_flags = (((kmem_flags | ~KMF_RANDOM) + 1) & KMF_RANDOM) | 2048 KMF_RANDOMIZE; 2049 cp->cache_flags = (kmem_flags | cflags) & KMF_DEBUG; 2050 mutex_exit(&kmem_flags_lock); 2051 2052 /* 2053 * Make sure all the various flags are reasonable. 2054 */ 2055 ASSERT(!(cflags & KMC_NOHASH) || !(cflags & KMC_NOTOUCH)); 2056 2057 if (cp->cache_flags & KMF_LITE) { 2058 if (bufsize >= kmem_lite_minsize && 2059 align <= kmem_lite_maxalign && 2060 P2PHASE(bufsize, kmem_lite_maxalign) != 0) { 2061 cp->cache_flags |= KMF_BUFTAG; 2062 cp->cache_flags &= ~(KMF_AUDIT | KMF_FIREWALL); 2063 } else { 2064 cp->cache_flags &= ~KMF_DEBUG; 2065 } 2066 } 2067 2068 if (cp->cache_flags & KMF_DEADBEEF) 2069 cp->cache_flags |= KMF_REDZONE; 2070 2071 if ((cflags & KMC_QCACHE) && (cp->cache_flags & KMF_AUDIT)) 2072 cp->cache_flags |= KMF_NOMAGAZINE; 2073 2074 if (cflags & KMC_NODEBUG) 2075 cp->cache_flags &= ~KMF_DEBUG; 2076 2077 if (cflags & KMC_NOTOUCH) 2078 cp->cache_flags &= ~KMF_TOUCH; 2079 2080 if (cflags & KMC_NOHASH) 2081 cp->cache_flags &= ~(KMF_AUDIT | KMF_FIREWALL); 2082 2083 if (cflags & KMC_NOMAGAZINE) 2084 cp->cache_flags |= KMF_NOMAGAZINE; 2085 2086 if ((cp->cache_flags & KMF_AUDIT) && !(cflags & KMC_NOTOUCH)) 2087 cp->cache_flags |= KMF_REDZONE; 2088 2089 if (!(cp->cache_flags & KMF_AUDIT)) 2090 cp->cache_flags &= ~KMF_CONTENTS; 2091 2092 if ((cp->cache_flags & KMF_BUFTAG) && bufsize >= kmem_minfirewall && 2093 !(cp->cache_flags & KMF_LITE) && !(cflags & KMC_NOHASH)) 2094 cp->cache_flags |= KMF_FIREWALL; 2095 2096 if (vmp != kmem_default_arena || kmem_firewall_arena == NULL) 2097 cp->cache_flags &= ~KMF_FIREWALL; 2098 2099 if (cp->cache_flags & KMF_FIREWALL) { 2100 cp->cache_flags &= ~KMF_BUFTAG; 2101 cp->cache_flags |= KMF_NOMAGAZINE; 2102 ASSERT(vmp == kmem_default_arena); 2103 vmp = kmem_firewall_arena; 2104 } 2105 2106 /* 2107 * Set cache properties. 2108 */ 2109 (void) strncpy(cp->cache_name, name, KMEM_CACHE_NAMELEN); 2110 strident_canon(cp->cache_name, KMEM_CACHE_NAMELEN); 2111 cp->cache_bufsize = bufsize; 2112 cp->cache_align = align; 2113 cp->cache_constructor = constructor; 2114 cp->cache_destructor = destructor; 2115 cp->cache_reclaim = reclaim; 2116 cp->cache_private = private; 2117 cp->cache_arena = vmp; 2118 cp->cache_cflags = cflags; 2119 2120 /* 2121 * Determine the chunk size. 2122 */ 2123 chunksize = bufsize; 2124 2125 if (align >= KMEM_ALIGN) { 2126 chunksize = P2ROUNDUP(chunksize, KMEM_ALIGN); 2127 cp->cache_bufctl = chunksize - KMEM_ALIGN; 2128 } 2129 2130 if (cp->cache_flags & KMF_BUFTAG) { 2131 cp->cache_bufctl = chunksize; 2132 cp->cache_buftag = chunksize; 2133 if (cp->cache_flags & KMF_LITE) 2134 chunksize += KMEM_BUFTAG_LITE_SIZE(kmem_lite_count); 2135 else 2136 chunksize += sizeof (kmem_buftag_t); 2137 } 2138 2139 if (cp->cache_flags & KMF_DEADBEEF) { 2140 cp->cache_verify = MIN(cp->cache_buftag, kmem_maxverify); 2141 if (cp->cache_flags & KMF_LITE) 2142 cp->cache_verify = sizeof (uint64_t); 2143 } 2144 2145 cp->cache_contents = MIN(cp->cache_bufctl, kmem_content_maxsave); 2146 2147 cp->cache_chunksize = chunksize = P2ROUNDUP(chunksize, align); 2148 2149 /* 2150 * Now that we know the chunk size, determine the optimal slab size. 2151 */ 2152 if (vmp == kmem_firewall_arena) { 2153 cp->cache_slabsize = P2ROUNDUP(chunksize, vmp->vm_quantum); 2154 cp->cache_mincolor = cp->cache_slabsize - chunksize; 2155 cp->cache_maxcolor = cp->cache_mincolor; 2156 cp->cache_flags |= KMF_HASH; 2157 ASSERT(!(cp->cache_flags & KMF_BUFTAG)); 2158 } else if ((cflags & KMC_NOHASH) || (!(cflags & KMC_NOTOUCH) && 2159 !(cp->cache_flags & KMF_AUDIT) && 2160 chunksize < vmp->vm_quantum / KMEM_VOID_FRACTION)) { 2161 cp->cache_slabsize = vmp->vm_quantum; 2162 cp->cache_mincolor = 0; 2163 cp->cache_maxcolor = 2164 (cp->cache_slabsize - sizeof (kmem_slab_t)) % chunksize; 2165 ASSERT(chunksize + sizeof (kmem_slab_t) <= cp->cache_slabsize); 2166 ASSERT(!(cp->cache_flags & KMF_AUDIT)); 2167 } else { 2168 size_t chunks, bestfit, waste, slabsize; 2169 size_t minwaste = LONG_MAX; 2170 2171 for (chunks = 1; chunks <= KMEM_VOID_FRACTION; chunks++) { 2172 slabsize = P2ROUNDUP(chunksize * chunks, 2173 vmp->vm_quantum); 2174 chunks = slabsize / chunksize; 2175 waste = (slabsize % chunksize) / chunks; 2176 if (waste < minwaste) { 2177 minwaste = waste; 2178 bestfit = slabsize; 2179 } 2180 } 2181 if (cflags & KMC_QCACHE) 2182 bestfit = VMEM_QCACHE_SLABSIZE(vmp->vm_qcache_max); 2183 cp->cache_slabsize = bestfit; 2184 cp->cache_mincolor = 0; 2185 cp->cache_maxcolor = bestfit % chunksize; 2186 cp->cache_flags |= KMF_HASH; 2187 } 2188 2189 if (cp->cache_flags & KMF_HASH) { 2190 ASSERT(!(cflags & KMC_NOHASH)); 2191 cp->cache_bufctl_cache = (cp->cache_flags & KMF_AUDIT) ? 2192 kmem_bufctl_audit_cache : kmem_bufctl_cache; 2193 } 2194 2195 if (cp->cache_maxcolor >= vmp->vm_quantum) 2196 cp->cache_maxcolor = vmp->vm_quantum - 1; 2197 2198 cp->cache_color = cp->cache_mincolor; 2199 2200 /* 2201 * Initialize the rest of the slab layer. 2202 */ 2203 mutex_init(&cp->cache_lock, NULL, MUTEX_DEFAULT, NULL); 2204 2205 cp->cache_freelist = &cp->cache_nullslab; 2206 cp->cache_nullslab.slab_cache = cp; 2207 cp->cache_nullslab.slab_refcnt = -1; 2208 cp->cache_nullslab.slab_next = &cp->cache_nullslab; 2209 cp->cache_nullslab.slab_prev = &cp->cache_nullslab; 2210 2211 if (cp->cache_flags & KMF_HASH) { 2212 cp->cache_hash_table = vmem_alloc(kmem_hash_arena, 2213 KMEM_HASH_INITIAL * sizeof (void *), VM_SLEEP); 2214 bzero(cp->cache_hash_table, 2215 KMEM_HASH_INITIAL * sizeof (void *)); 2216 cp->cache_hash_mask = KMEM_HASH_INITIAL - 1; 2217 cp->cache_hash_shift = highbit((ulong_t)chunksize) - 1; 2218 } 2219 2220 /* 2221 * Initialize the depot. 2222 */ 2223 mutex_init(&cp->cache_depot_lock, NULL, MUTEX_DEFAULT, NULL); 2224 2225 for (mtp = kmem_magtype; chunksize <= mtp->mt_minbuf; mtp++) 2226 continue; 2227 2228 cp->cache_magtype = mtp; 2229 2230 /* 2231 * Initialize the CPU layer. 2232 */ 2233 for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) { 2234 kmem_cpu_cache_t *ccp = &cp->cache_cpu[cpu_seqid]; 2235 mutex_init(&ccp->cc_lock, NULL, MUTEX_DEFAULT, NULL); 2236 ccp->cc_flags = cp->cache_flags; 2237 ccp->cc_rounds = -1; 2238 ccp->cc_prounds = -1; 2239 } 2240 2241 /* 2242 * Create the cache's kstats. 2243 */ 2244 if ((cp->cache_kstat = kstat_create("unix", 0, cp->cache_name, 2245 "kmem_cache", KSTAT_TYPE_NAMED, 2246 sizeof (kmem_cache_kstat) / sizeof (kstat_named_t), 2247 KSTAT_FLAG_VIRTUAL)) != NULL) { 2248 cp->cache_kstat->ks_data = &kmem_cache_kstat; 2249 cp->cache_kstat->ks_update = kmem_cache_kstat_update; 2250 cp->cache_kstat->ks_private = cp; 2251 cp->cache_kstat->ks_lock = &kmem_cache_kstat_lock; 2252 kstat_install(cp->cache_kstat); 2253 } 2254 2255 /* 2256 * Add the cache to the global list. This makes it visible 2257 * to kmem_update(), so the cache must be ready for business. 2258 */ 2259 mutex_enter(&kmem_cache_lock); 2260 cp->cache_next = cnext = &kmem_null_cache; 2261 cp->cache_prev = cprev = kmem_null_cache.cache_prev; 2262 cnext->cache_prev = cp; 2263 cprev->cache_next = cp; 2264 mutex_exit(&kmem_cache_lock); 2265 2266 if (kmem_ready) 2267 kmem_cache_magazine_enable(cp); 2268 2269 return (cp); 2270 } 2271 2272 void 2273 kmem_cache_destroy(kmem_cache_t *cp) 2274 { 2275 int cpu_seqid; 2276 2277 /* 2278 * Remove the cache from the global cache list so that no one else 2279 * can schedule tasks on its behalf, wait for any pending tasks to 2280 * complete, purge the cache, and then destroy it. 2281 */ 2282 mutex_enter(&kmem_cache_lock); 2283 cp->cache_prev->cache_next = cp->cache_next; 2284 cp->cache_next->cache_prev = cp->cache_prev; 2285 cp->cache_prev = cp->cache_next = NULL; 2286 mutex_exit(&kmem_cache_lock); 2287 2288 if (kmem_taskq != NULL) 2289 taskq_wait(kmem_taskq); 2290 2291 kmem_cache_magazine_purge(cp); 2292 2293 mutex_enter(&cp->cache_lock); 2294 if (cp->cache_buftotal != 0) 2295 cmn_err(CE_WARN, "kmem_cache_destroy: '%s' (%p) not empty", 2296 cp->cache_name, (void *)cp); 2297 cp->cache_reclaim = NULL; 2298 /* 2299 * The cache is now dead. There should be no further activity. 2300 * We enforce this by setting land mines in the constructor and 2301 * destructor routines that induce a kernel text fault if invoked. 2302 */ 2303 cp->cache_constructor = (int (*)(void *, void *, int))1; 2304 cp->cache_destructor = (void (*)(void *, void *))2; 2305 mutex_exit(&cp->cache_lock); 2306 2307 kstat_delete(cp->cache_kstat); 2308 2309 if (cp->cache_hash_table != NULL) 2310 vmem_free(kmem_hash_arena, cp->cache_hash_table, 2311 (cp->cache_hash_mask + 1) * sizeof (void *)); 2312 2313 for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) 2314 mutex_destroy(&cp->cache_cpu[cpu_seqid].cc_lock); 2315 2316 mutex_destroy(&cp->cache_depot_lock); 2317 mutex_destroy(&cp->cache_lock); 2318 2319 vmem_free(kmem_cache_arena, cp, KMEM_CACHE_SIZE(max_ncpus)); 2320 } 2321 2322 /*ARGSUSED*/ 2323 static int 2324 kmem_cpu_setup(cpu_setup_t what, int id, void *arg) 2325 { 2326 ASSERT(MUTEX_HELD(&cpu_lock)); 2327 if (what == CPU_UNCONFIG) { 2328 kmem_cache_applyall(kmem_cache_magazine_purge, 2329 kmem_taskq, TQ_SLEEP); 2330 kmem_cache_applyall(kmem_cache_magazine_enable, 2331 kmem_taskq, TQ_SLEEP); 2332 } 2333 return (0); 2334 } 2335 2336 static void 2337 kmem_cache_init(int pass, int use_large_pages) 2338 { 2339 int i; 2340 size_t size; 2341 kmem_cache_t *cp; 2342 kmem_magtype_t *mtp; 2343 char name[KMEM_CACHE_NAMELEN + 1]; 2344 2345 for (i = 0; i < sizeof (kmem_magtype) / sizeof (*mtp); i++) { 2346 mtp = &kmem_magtype[i]; 2347 (void) sprintf(name, "kmem_magazine_%d", mtp->mt_magsize); 2348 mtp->mt_cache = kmem_cache_create(name, 2349 (mtp->mt_magsize + 1) * sizeof (void *), 2350 mtp->mt_align, NULL, NULL, NULL, NULL, 2351 kmem_msb_arena, KMC_NOHASH); 2352 } 2353 2354 kmem_slab_cache = kmem_cache_create("kmem_slab_cache", 2355 sizeof (kmem_slab_t), 0, NULL, NULL, NULL, NULL, 2356 kmem_msb_arena, KMC_NOHASH); 2357 2358 kmem_bufctl_cache = kmem_cache_create("kmem_bufctl_cache", 2359 sizeof (kmem_bufctl_t), 0, NULL, NULL, NULL, NULL, 2360 kmem_msb_arena, KMC_NOHASH); 2361 2362 kmem_bufctl_audit_cache = kmem_cache_create("kmem_bufctl_audit_cache", 2363 sizeof (kmem_bufctl_audit_t), 0, NULL, NULL, NULL, NULL, 2364 kmem_msb_arena, KMC_NOHASH); 2365 2366 if (pass == 2) { 2367 kmem_va_arena = vmem_create("kmem_va", 2368 NULL, 0, PAGESIZE, 2369 vmem_alloc, vmem_free, heap_arena, 2370 8 * PAGESIZE, VM_SLEEP); 2371 2372 if (use_large_pages) { 2373 kmem_default_arena = vmem_xcreate("kmem_default", 2374 NULL, 0, PAGESIZE, 2375 segkmem_alloc_lp, segkmem_free_lp, kmem_va_arena, 2376 0, VM_SLEEP); 2377 } else { 2378 kmem_default_arena = vmem_create("kmem_default", 2379 NULL, 0, PAGESIZE, 2380 segkmem_alloc, segkmem_free, kmem_va_arena, 2381 0, VM_SLEEP); 2382 } 2383 } else { 2384 /* 2385 * During the first pass, the kmem_alloc_* caches 2386 * are treated as metadata. 2387 */ 2388 kmem_default_arena = kmem_msb_arena; 2389 } 2390 2391 /* 2392 * Set up the default caches to back kmem_alloc() 2393 */ 2394 size = KMEM_ALIGN; 2395 for (i = 0; i < sizeof (kmem_alloc_sizes) / sizeof (int); i++) { 2396 size_t align = KMEM_ALIGN; 2397 size_t cache_size = kmem_alloc_sizes[i]; 2398 /* 2399 * If they allocate a multiple of the coherency granularity, 2400 * they get a coherency-granularity-aligned address. 2401 */ 2402 if (IS_P2ALIGNED(cache_size, 64)) 2403 align = 64; 2404 if (IS_P2ALIGNED(cache_size, PAGESIZE)) 2405 align = PAGESIZE; 2406 (void) sprintf(name, "kmem_alloc_%lu", cache_size); 2407 cp = kmem_cache_create(name, cache_size, align, 2408 NULL, NULL, NULL, NULL, NULL, KMC_KMEM_ALLOC); 2409 while (size <= cache_size) { 2410 kmem_alloc_table[(size - 1) >> KMEM_ALIGN_SHIFT] = cp; 2411 size += KMEM_ALIGN; 2412 } 2413 } 2414 } 2415 2416 void 2417 kmem_init(void) 2418 { 2419 kmem_cache_t *cp; 2420 int old_kmem_flags = kmem_flags; 2421 int use_large_pages = 0; 2422 size_t maxverify, minfirewall; 2423 2424 kstat_init(); 2425 2426 /* 2427 * Small-memory systems (< 24 MB) can't handle kmem_flags overhead. 2428 */ 2429 if (physmem < btop(24 << 20) && !(old_kmem_flags & KMF_STICKY)) 2430 kmem_flags = 0; 2431 2432 /* 2433 * Don't do firewalled allocations if the heap is less than 1TB 2434 * (i.e. on a 32-bit kernel) 2435 * The resulting VM_NEXTFIT allocations would create too much 2436 * fragmentation in a small heap. 2437 */ 2438 #if defined(_LP64) 2439 maxverify = minfirewall = PAGESIZE / 2; 2440 #else 2441 maxverify = minfirewall = ULONG_MAX; 2442 #endif 2443 2444 /* LINTED */ 2445 ASSERT(sizeof (kmem_cpu_cache_t) == KMEM_CPU_CACHE_SIZE); 2446 2447 kmem_null_cache.cache_next = &kmem_null_cache; 2448 kmem_null_cache.cache_prev = &kmem_null_cache; 2449 2450 kmem_metadata_arena = vmem_create("kmem_metadata", NULL, 0, PAGESIZE, 2451 vmem_alloc, vmem_free, heap_arena, 8 * PAGESIZE, 2452 VM_SLEEP | VMC_NO_QCACHE); 2453 2454 kmem_msb_arena = vmem_create("kmem_msb", NULL, 0, 2455 PAGESIZE, segkmem_alloc, segkmem_free, kmem_metadata_arena, 0, 2456 VM_SLEEP); 2457 2458 kmem_cache_arena = vmem_create("kmem_cache", NULL, 0, KMEM_ALIGN, 2459 segkmem_alloc, segkmem_free, kmem_metadata_arena, 0, VM_SLEEP); 2460 2461 kmem_hash_arena = vmem_create("kmem_hash", NULL, 0, KMEM_ALIGN, 2462 segkmem_alloc, segkmem_free, kmem_metadata_arena, 0, VM_SLEEP); 2463 2464 kmem_log_arena = vmem_create("kmem_log", NULL, 0, KMEM_ALIGN, 2465 segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP); 2466 2467 kmem_firewall_va_arena = vmem_create("kmem_firewall_va", 2468 NULL, 0, PAGESIZE, 2469 kmem_firewall_va_alloc, kmem_firewall_va_free, heap_arena, 2470 0, VM_SLEEP); 2471 2472 kmem_firewall_arena = vmem_create("kmem_firewall", NULL, 0, PAGESIZE, 2473 segkmem_alloc, segkmem_free, kmem_firewall_va_arena, 0, VM_SLEEP); 2474 2475 /* temporary oversize arena for mod_read_system_file */ 2476 kmem_oversize_arena = vmem_create("kmem_oversize", NULL, 0, PAGESIZE, 2477 segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP); 2478 2479 kmem_null_cache.cache_next = &kmem_null_cache; 2480 kmem_null_cache.cache_prev = &kmem_null_cache; 2481 2482 kmem_reap_interval = 15 * hz; 2483 2484 /* 2485 * Read /etc/system. This is a chicken-and-egg problem because 2486 * kmem_flags may be set in /etc/system, but mod_read_system_file() 2487 * needs to use the allocator. The simplest solution is to create 2488 * all the standard kmem caches, read /etc/system, destroy all the 2489 * caches we just created, and then create them all again in light 2490 * of the (possibly) new kmem_flags and other kmem tunables. 2491 */ 2492 kmem_cache_init(1, 0); 2493 2494 mod_read_system_file(boothowto & RB_ASKNAME); 2495 2496 while ((cp = kmem_null_cache.cache_prev) != &kmem_null_cache) 2497 kmem_cache_destroy(cp); 2498 2499 vmem_destroy(kmem_oversize_arena); 2500 2501 if (old_kmem_flags & KMF_STICKY) 2502 kmem_flags = old_kmem_flags; 2503 2504 if (!(kmem_flags & KMF_AUDIT)) 2505 vmem_seg_size = offsetof(vmem_seg_t, vs_thread); 2506 2507 if (kmem_maxverify == 0) 2508 kmem_maxverify = maxverify; 2509 2510 if (kmem_minfirewall == 0) 2511 kmem_minfirewall = minfirewall; 2512 2513 /* 2514 * give segkmem a chance to figure out if we are using large pages 2515 * for the kernel heap 2516 */ 2517 use_large_pages = segkmem_lpsetup(); 2518 2519 /* 2520 * To protect against corruption, we keep the actual number of callers 2521 * KMF_LITE records seperate from the tunable. We arbitrarily clamp 2522 * to 16, since the overhead for small buffers quickly gets out of 2523 * hand. 2524 * 2525 * The real limit would depend on the needs of the largest KMC_NOHASH 2526 * cache. 2527 */ 2528 kmem_lite_count = MIN(MAX(0, kmem_lite_pcs), 16); 2529 kmem_lite_pcs = kmem_lite_count; 2530 2531 /* 2532 * Normally, we firewall oversized allocations when possible, but 2533 * if we are using large pages for kernel memory, and we don't have 2534 * any non-LITE debugging flags set, we want to allocate oversized 2535 * buffers from large pages, and so skip the firewalling. 2536 */ 2537 if (use_large_pages && 2538 ((kmem_flags & KMF_LITE) || !(kmem_flags & KMF_DEBUG))) { 2539 kmem_oversize_arena = vmem_xcreate("kmem_oversize", NULL, 0, 2540 PAGESIZE, segkmem_alloc_lp, segkmem_free_lp, heap_arena, 2541 0, VM_SLEEP); 2542 } else { 2543 kmem_oversize_arena = vmem_create("kmem_oversize", 2544 NULL, 0, PAGESIZE, 2545 segkmem_alloc, segkmem_free, kmem_minfirewall < ULONG_MAX? 2546 kmem_firewall_va_arena : heap_arena, 0, VM_SLEEP); 2547 } 2548 2549 kmem_cache_init(2, use_large_pages); 2550 2551 if (kmem_flags & (KMF_AUDIT | KMF_RANDOMIZE)) { 2552 if (kmem_transaction_log_size == 0) 2553 kmem_transaction_log_size = kmem_maxavail() / 50; 2554 kmem_transaction_log = kmem_log_init(kmem_transaction_log_size); 2555 } 2556 2557 if (kmem_flags & (KMF_CONTENTS | KMF_RANDOMIZE)) { 2558 if (kmem_content_log_size == 0) 2559 kmem_content_log_size = kmem_maxavail() / 50; 2560 kmem_content_log = kmem_log_init(kmem_content_log_size); 2561 } 2562 2563 kmem_failure_log = kmem_log_init(kmem_failure_log_size); 2564 2565 kmem_slab_log = kmem_log_init(kmem_slab_log_size); 2566 2567 /* 2568 * Initialize STREAMS message caches so allocb() is available. 2569 * This allows us to initialize the logging framework (cmn_err(9F), 2570 * strlog(9F), etc) so we can start recording messages. 2571 */ 2572 streams_msg_init(); 2573 /* 2574 * Initialize the ZSD framework in Zones so modules loaded henceforth 2575 * can register their callbacks. 2576 */ 2577 zone_zsd_init(); 2578 log_init(); 2579 taskq_init(); 2580 2581 kmem_cache_applyall(kmem_cache_magazine_enable, NULL, TQ_SLEEP); 2582 2583 kmem_ready = 1; 2584 2585 /* 2586 * Initialize the platform-specific aligned/DMA memory allocator. 2587 */ 2588 ka_init(); 2589 2590 /* 2591 * Initialize 32-bit ID cache. 2592 */ 2593 id32_init(); 2594 } 2595 2596 void 2597 kmem_thread_init(void) 2598 { 2599 kmem_taskq = taskq_create_instance("kmem_taskq", 0, 1, minclsyspri, 2600 300, INT_MAX, TASKQ_PREPOPULATE); 2601 } 2602 2603 void 2604 kmem_mp_init(void) 2605 { 2606 mutex_enter(&cpu_lock); 2607 register_cpu_setup_func(kmem_cpu_setup, NULL); 2608 mutex_exit(&cpu_lock); 2609 2610 kmem_update_timeout(NULL); 2611 } 2612