1 /*- 2 * Copyright (c) 2002-2005, 2009, 2013 Jeffrey Roberson <jeff@FreeBSD.org> 3 * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org> 4 * Copyright (c) 2004-2006 Robert N. M. Watson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * uma_core.c Implementation of the Universal Memory allocator 31 * 32 * This allocator is intended to replace the multitude of similar object caches 33 * in the standard FreeBSD kernel. The intent is to be flexible as well as 34 * effecient. A primary design goal is to return unused memory to the rest of 35 * the system. This will make the system as a whole more flexible due to the 36 * ability to move memory to subsystems which most need it instead of leaving 37 * pools of reserved memory unused. 38 * 39 * The basic ideas stem from similar slab/zone based allocators whose algorithms 40 * are well known. 41 * 42 */ 43 44 /* 45 * TODO: 46 * - Improve memory usage for large allocations 47 * - Investigate cache size adjustments 48 */ 49 50 #include <sys/cdefs.h> 51 __FBSDID("$FreeBSD$"); 52 53 /* I should really use ktr.. */ 54 /* 55 #define UMA_DEBUG 1 56 #define UMA_DEBUG_ALLOC 1 57 #define UMA_DEBUG_ALLOC_1 1 58 */ 59 60 #include "opt_ddb.h" 61 #include "opt_param.h" 62 #include "opt_vm.h" 63 64 #include <sys/param.h> 65 #include <sys/systm.h> 66 #include <sys/bitset.h> 67 #include <sys/kernel.h> 68 #include <sys/types.h> 69 #include <sys/queue.h> 70 #include <sys/malloc.h> 71 #include <sys/ktr.h> 72 #include <sys/lock.h> 73 #include <sys/sysctl.h> 74 #include <sys/mutex.h> 75 #include <sys/proc.h> 76 #include <sys/rwlock.h> 77 #include <sys/sbuf.h> 78 #include <sys/smp.h> 79 #include <sys/vmmeter.h> 80 81 #include <vm/vm.h> 82 #include <vm/vm_object.h> 83 #include <vm/vm_page.h> 84 #include <vm/vm_pageout.h> 85 #include <vm/vm_param.h> 86 #include <vm/vm_map.h> 87 #include <vm/vm_kern.h> 88 #include <vm/vm_extern.h> 89 #include <vm/uma.h> 90 #include <vm/uma_int.h> 91 #include <vm/uma_dbg.h> 92 93 #include <ddb/ddb.h> 94 95 #ifdef DEBUG_MEMGUARD 96 #include <vm/memguard.h> 97 #endif 98 99 /* 100 * This is the zone and keg from which all zones are spawned. The idea is that 101 * even the zone & keg heads are allocated from the allocator, so we use the 102 * bss section to bootstrap us. 103 */ 104 static struct uma_keg masterkeg; 105 static struct uma_zone masterzone_k; 106 static struct uma_zone masterzone_z; 107 static uma_zone_t kegs = &masterzone_k; 108 static uma_zone_t zones = &masterzone_z; 109 110 /* This is the zone from which all of uma_slab_t's are allocated. */ 111 static uma_zone_t slabzone; 112 static uma_zone_t slabrefzone; /* With refcounters (for UMA_ZONE_REFCNT) */ 113 114 /* 115 * The initial hash tables come out of this zone so they can be allocated 116 * prior to malloc coming up. 117 */ 118 static uma_zone_t hashzone; 119 120 /* The boot-time adjusted value for cache line alignment. */ 121 int uma_align_cache = 64 - 1; 122 123 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets"); 124 125 /* 126 * Are we allowed to allocate buckets? 127 */ 128 static int bucketdisable = 1; 129 130 /* Linked list of all kegs in the system */ 131 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs); 132 133 /* This mutex protects the keg list */ 134 static struct mtx_padalign uma_mtx; 135 136 /* Linked list of boot time pages */ 137 static LIST_HEAD(,uma_slab) uma_boot_pages = 138 LIST_HEAD_INITIALIZER(uma_boot_pages); 139 140 /* This mutex protects the boot time pages list */ 141 static struct mtx_padalign uma_boot_pages_mtx; 142 143 /* Is the VM done starting up? */ 144 static int booted = 0; 145 #define UMA_STARTUP 1 146 #define UMA_STARTUP2 2 147 148 /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */ 149 static const u_int uma_max_ipers = SLAB_SETSIZE; 150 151 /* 152 * Only mbuf clusters use ref zones. Just provide enough references 153 * to support the one user. New code should not use the ref facility. 154 */ 155 static const u_int uma_max_ipers_ref = PAGE_SIZE / MCLBYTES; 156 157 /* 158 * This is the handle used to schedule events that need to happen 159 * outside of the allocation fast path. 160 */ 161 static struct callout uma_callout; 162 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */ 163 164 /* 165 * This structure is passed as the zone ctor arg so that I don't have to create 166 * a special allocation function just for zones. 167 */ 168 struct uma_zctor_args { 169 const char *name; 170 size_t size; 171 uma_ctor ctor; 172 uma_dtor dtor; 173 uma_init uminit; 174 uma_fini fini; 175 uma_import import; 176 uma_release release; 177 void *arg; 178 uma_keg_t keg; 179 int align; 180 uint32_t flags; 181 }; 182 183 struct uma_kctor_args { 184 uma_zone_t zone; 185 size_t size; 186 uma_init uminit; 187 uma_fini fini; 188 int align; 189 uint32_t flags; 190 }; 191 192 struct uma_bucket_zone { 193 uma_zone_t ubz_zone; 194 char *ubz_name; 195 int ubz_entries; /* Number of items it can hold. */ 196 int ubz_maxsize; /* Maximum allocation size per-item. */ 197 }; 198 199 /* 200 * Compute the actual number of bucket entries to pack them in power 201 * of two sizes for more efficient space utilization. 202 */ 203 #define BUCKET_SIZE(n) \ 204 (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *)) 205 206 #define BUCKET_MAX BUCKET_SIZE(128) 207 208 struct uma_bucket_zone bucket_zones[] = { 209 { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 210 { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 211 { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 212 { NULL, NULL, 0} 213 }; 214 static uma_zone_t largebucket; 215 216 /* 217 * Flags and enumerations to be passed to internal functions. 218 */ 219 enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI }; 220 221 /* Prototypes.. */ 222 223 static void *noobj_alloc(uma_zone_t, int, uint8_t *, int); 224 static void *page_alloc(uma_zone_t, int, uint8_t *, int); 225 static void *startup_alloc(uma_zone_t, int, uint8_t *, int); 226 static void page_free(void *, int, uint8_t); 227 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int); 228 static void cache_drain(uma_zone_t); 229 static void bucket_drain(uma_zone_t, uma_bucket_t); 230 static void bucket_cache_drain(uma_zone_t zone); 231 static int keg_ctor(void *, int, void *, int); 232 static void keg_dtor(void *, int, void *); 233 static int zone_ctor(void *, int, void *, int); 234 static void zone_dtor(void *, int, void *); 235 static int zero_init(void *, int, int); 236 static void keg_small_init(uma_keg_t keg); 237 static void keg_large_init(uma_keg_t keg); 238 static void zone_foreach(void (*zfunc)(uma_zone_t)); 239 static void zone_timeout(uma_zone_t zone); 240 static int hash_alloc(struct uma_hash *); 241 static int hash_expand(struct uma_hash *, struct uma_hash *); 242 static void hash_free(struct uma_hash *hash); 243 static void uma_timeout(void *); 244 static void uma_startup3(void); 245 static void *zone_alloc_item(uma_zone_t, void *, int); 246 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 247 static void bucket_enable(void); 248 static void bucket_init(void); 249 static uma_bucket_t bucket_alloc(uma_zone_t zone, int); 250 static void bucket_free(uma_zone_t zone, uma_bucket_t); 251 static void bucket_zone_drain(void); 252 static uma_bucket_t zone_alloc_bucket(uma_zone_t zone, int flags); 253 static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags); 254 static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags); 255 static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 256 static void slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item); 257 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 258 uma_fini fini, int align, uint32_t flags); 259 static int zone_import(uma_zone_t zone, void **bucket, int max, int flags); 260 static void zone_release(uma_zone_t zone, void **bucket, int cnt); 261 262 void uma_print_zone(uma_zone_t); 263 void uma_print_stats(void); 264 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 265 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 266 267 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 268 269 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT, 270 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 271 272 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 273 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 274 275 static int zone_warnings = 1; 276 TUNABLE_INT("vm.zone_warnings", &zone_warnings); 277 SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RW, &zone_warnings, 0, 278 "Warn when UMA zones becomes full"); 279 280 /* 281 * This routine checks to see whether or not it's safe to enable buckets. 282 */ 283 static void 284 bucket_enable(void) 285 { 286 bucketdisable = vm_page_count_min(); 287 } 288 289 /* 290 * Initialize bucket_zones, the array of zones of buckets of various sizes. 291 * 292 * For each zone, calculate the memory required for each bucket, consisting 293 * of the header and an array of pointers. 294 */ 295 static void 296 bucket_init(void) 297 { 298 struct uma_bucket_zone *ubz; 299 int size; 300 int i; 301 302 for (i = 0, ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 303 size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 304 size += sizeof(void *) * ubz->ubz_entries; 305 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 306 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 307 UMA_ZONE_MAXBUCKET | UMA_ZONE_MTXCLASS); 308 } 309 /* 310 * To avoid recursive bucket allocation loops we disable buckets 311 * on the smallest bucket zone and use it for the largest zone. 312 * The remainder of the zones all use the largest zone. 313 */ 314 ubz--; 315 ubz->ubz_zone->uz_count = bucket_zones[0].ubz_entries; 316 bucket_zones[0].ubz_zone->uz_count = 0; 317 largebucket = ubz->ubz_zone; 318 } 319 320 /* 321 * Given a desired number of entries for a bucket, return the zone from which 322 * to allocate the bucket. 323 */ 324 static struct uma_bucket_zone * 325 bucket_zone_lookup(int entries) 326 { 327 struct uma_bucket_zone *ubz; 328 329 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 330 if (ubz->ubz_entries >= entries) 331 return (ubz); 332 ubz--; 333 return (ubz); 334 } 335 336 static int 337 bucket_select(int size) 338 { 339 struct uma_bucket_zone *ubz; 340 341 ubz = &bucket_zones[0]; 342 if (size > ubz->ubz_maxsize) 343 return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 344 345 for (; ubz->ubz_entries != 0; ubz++) 346 if (ubz->ubz_maxsize < size) 347 break; 348 ubz--; 349 return (ubz->ubz_entries); 350 } 351 352 static uma_bucket_t 353 bucket_alloc(uma_zone_t zone, int flags) 354 { 355 struct uma_bucket_zone *ubz; 356 uma_bucket_t bucket; 357 358 /* 359 * This is to stop us from allocating per cpu buckets while we're 360 * running out of vm.boot_pages. Otherwise, we would exhaust the 361 * boot pages. This also prevents us from allocating buckets in 362 * low memory situations. 363 */ 364 if (bucketdisable) 365 return (NULL); 366 367 if (zone->uz_flags & UMA_ZFLAG_CACHEONLY) 368 flags |= M_NOVM; 369 ubz = bucket_zone_lookup(zone->uz_count); 370 bucket = uma_zalloc(ubz->ubz_zone, flags); 371 if (bucket) { 372 #ifdef INVARIANTS 373 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 374 #endif 375 bucket->ub_cnt = 0; 376 bucket->ub_entries = ubz->ubz_entries; 377 } 378 379 return (bucket); 380 } 381 382 static void 383 bucket_free(uma_zone_t zone, uma_bucket_t bucket) 384 { 385 struct uma_bucket_zone *ubz; 386 387 KASSERT(bucket->ub_cnt == 0, 388 ("bucket_free: Freeing a non free bucket.")); 389 ubz = bucket_zone_lookup(bucket->ub_entries); 390 uma_zfree(ubz->ubz_zone, bucket); 391 } 392 393 static void 394 bucket_zone_drain(void) 395 { 396 struct uma_bucket_zone *ubz; 397 398 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 399 zone_drain(ubz->ubz_zone); 400 } 401 402 static void 403 zone_log_warning(uma_zone_t zone) 404 { 405 static const struct timeval warninterval = { 300, 0 }; 406 407 if (!zone_warnings || zone->uz_warning == NULL) 408 return; 409 410 if (ratecheck(&zone->uz_ratecheck, &warninterval)) 411 printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 412 } 413 414 static void 415 zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t)) 416 { 417 uma_klink_t klink; 418 419 LIST_FOREACH(klink, &zone->uz_kegs, kl_link) 420 kegfn(klink->kl_keg); 421 } 422 423 /* 424 * Routine called by timeout which is used to fire off some time interval 425 * based calculations. (stats, hash size, etc.) 426 * 427 * Arguments: 428 * arg Unused 429 * 430 * Returns: 431 * Nothing 432 */ 433 static void 434 uma_timeout(void *unused) 435 { 436 bucket_enable(); 437 zone_foreach(zone_timeout); 438 439 /* Reschedule this event */ 440 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 441 } 442 443 /* 444 * Routine to perform timeout driven calculations. This expands the 445 * hashes and does per cpu statistics aggregation. 446 * 447 * Returns nothing. 448 */ 449 static void 450 keg_timeout(uma_keg_t keg) 451 { 452 453 KEG_LOCK(keg); 454 /* 455 * Expand the keg hash table. 456 * 457 * This is done if the number of slabs is larger than the hash size. 458 * What I'm trying to do here is completely reduce collisions. This 459 * may be a little aggressive. Should I allow for two collisions max? 460 */ 461 if (keg->uk_flags & UMA_ZONE_HASH && 462 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) { 463 struct uma_hash newhash; 464 struct uma_hash oldhash; 465 int ret; 466 467 /* 468 * This is so involved because allocating and freeing 469 * while the keg lock is held will lead to deadlock. 470 * I have to do everything in stages and check for 471 * races. 472 */ 473 newhash = keg->uk_hash; 474 KEG_UNLOCK(keg); 475 ret = hash_alloc(&newhash); 476 KEG_LOCK(keg); 477 if (ret) { 478 if (hash_expand(&keg->uk_hash, &newhash)) { 479 oldhash = keg->uk_hash; 480 keg->uk_hash = newhash; 481 } else 482 oldhash = newhash; 483 484 KEG_UNLOCK(keg); 485 hash_free(&oldhash); 486 KEG_LOCK(keg); 487 } 488 } 489 KEG_UNLOCK(keg); 490 } 491 492 static void 493 zone_timeout(uma_zone_t zone) 494 { 495 496 zone_foreach_keg(zone, &keg_timeout); 497 } 498 499 /* 500 * Allocate and zero fill the next sized hash table from the appropriate 501 * backing store. 502 * 503 * Arguments: 504 * hash A new hash structure with the old hash size in uh_hashsize 505 * 506 * Returns: 507 * 1 on sucess and 0 on failure. 508 */ 509 static int 510 hash_alloc(struct uma_hash *hash) 511 { 512 int oldsize; 513 int alloc; 514 515 oldsize = hash->uh_hashsize; 516 517 /* We're just going to go to a power of two greater */ 518 if (oldsize) { 519 hash->uh_hashsize = oldsize * 2; 520 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 521 hash->uh_slab_hash = (struct slabhead *)malloc(alloc, 522 M_UMAHASH, M_NOWAIT); 523 } else { 524 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 525 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 526 M_WAITOK); 527 hash->uh_hashsize = UMA_HASH_SIZE_INIT; 528 } 529 if (hash->uh_slab_hash) { 530 bzero(hash->uh_slab_hash, alloc); 531 hash->uh_hashmask = hash->uh_hashsize - 1; 532 return (1); 533 } 534 535 return (0); 536 } 537 538 /* 539 * Expands the hash table for HASH zones. This is done from zone_timeout 540 * to reduce collisions. This must not be done in the regular allocation 541 * path, otherwise, we can recurse on the vm while allocating pages. 542 * 543 * Arguments: 544 * oldhash The hash you want to expand 545 * newhash The hash structure for the new table 546 * 547 * Returns: 548 * Nothing 549 * 550 * Discussion: 551 */ 552 static int 553 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 554 { 555 uma_slab_t slab; 556 int hval; 557 int i; 558 559 if (!newhash->uh_slab_hash) 560 return (0); 561 562 if (oldhash->uh_hashsize >= newhash->uh_hashsize) 563 return (0); 564 565 /* 566 * I need to investigate hash algorithms for resizing without a 567 * full rehash. 568 */ 569 570 for (i = 0; i < oldhash->uh_hashsize; i++) 571 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) { 572 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]); 573 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink); 574 hval = UMA_HASH(newhash, slab->us_data); 575 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 576 slab, us_hlink); 577 } 578 579 return (1); 580 } 581 582 /* 583 * Free the hash bucket to the appropriate backing store. 584 * 585 * Arguments: 586 * slab_hash The hash bucket we're freeing 587 * hashsize The number of entries in that hash bucket 588 * 589 * Returns: 590 * Nothing 591 */ 592 static void 593 hash_free(struct uma_hash *hash) 594 { 595 if (hash->uh_slab_hash == NULL) 596 return; 597 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 598 zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 599 else 600 free(hash->uh_slab_hash, M_UMAHASH); 601 } 602 603 /* 604 * Frees all outstanding items in a bucket 605 * 606 * Arguments: 607 * zone The zone to free to, must be unlocked. 608 * bucket The free/alloc bucket with items, cpu queue must be locked. 609 * 610 * Returns: 611 * Nothing 612 */ 613 614 static void 615 bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 616 { 617 int i; 618 619 if (bucket == NULL) 620 return; 621 622 if (zone->uz_fini) 623 for (i = 0; i < bucket->ub_cnt; i++) 624 zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 625 zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 626 bucket->ub_cnt = 0; 627 } 628 629 /* 630 * Drains the per cpu caches for a zone. 631 * 632 * NOTE: This may only be called while the zone is being turn down, and not 633 * during normal operation. This is necessary in order that we do not have 634 * to migrate CPUs to drain the per-CPU caches. 635 * 636 * Arguments: 637 * zone The zone to drain, must be unlocked. 638 * 639 * Returns: 640 * Nothing 641 */ 642 static void 643 cache_drain(uma_zone_t zone) 644 { 645 uma_cache_t cache; 646 int cpu; 647 648 /* 649 * XXX: It is safe to not lock the per-CPU caches, because we're 650 * tearing down the zone anyway. I.e., there will be no further use 651 * of the caches at this point. 652 * 653 * XXX: It would good to be able to assert that the zone is being 654 * torn down to prevent improper use of cache_drain(). 655 * 656 * XXX: We lock the zone before passing into bucket_cache_drain() as 657 * it is used elsewhere. Should the tear-down path be made special 658 * there in some form? 659 */ 660 CPU_FOREACH(cpu) { 661 cache = &zone->uz_cpu[cpu]; 662 bucket_drain(zone, cache->uc_allocbucket); 663 bucket_drain(zone, cache->uc_freebucket); 664 if (cache->uc_allocbucket != NULL) 665 bucket_free(zone, cache->uc_allocbucket); 666 if (cache->uc_freebucket != NULL) 667 bucket_free(zone, cache->uc_freebucket); 668 cache->uc_allocbucket = cache->uc_freebucket = NULL; 669 } 670 ZONE_LOCK(zone); 671 bucket_cache_drain(zone); 672 ZONE_UNLOCK(zone); 673 } 674 675 /* 676 * Drain the cached buckets from a zone. Expects a locked zone on entry. 677 */ 678 static void 679 bucket_cache_drain(uma_zone_t zone) 680 { 681 uma_bucket_t bucket; 682 683 /* 684 * Drain the bucket queues and free the buckets, we just keep two per 685 * cpu (alloc/free). 686 */ 687 while ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) { 688 LIST_REMOVE(bucket, ub_link); 689 ZONE_UNLOCK(zone); 690 bucket_drain(zone, bucket); 691 bucket_free(zone, bucket); 692 ZONE_LOCK(zone); 693 } 694 } 695 696 static void 697 keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 698 { 699 uint8_t *mem; 700 int i; 701 uint8_t flags; 702 703 mem = slab->us_data; 704 flags = slab->us_flags; 705 i = start; 706 if (keg->uk_fini != NULL) { 707 for (i--; i > -1; i--) 708 keg->uk_fini(slab->us_data + (keg->uk_rsize * i), 709 keg->uk_size); 710 } 711 if (keg->uk_flags & UMA_ZONE_VTOSLAB) { 712 vm_object_t obj; 713 714 if (flags & UMA_SLAB_KMEM) 715 obj = kmem_object; 716 else if (flags & UMA_SLAB_KERNEL) 717 obj = kernel_object; 718 else 719 obj = NULL; 720 for (i = 0; i < keg->uk_ppera; i++) 721 vsetobj((vm_offset_t)mem + (i * PAGE_SIZE), obj); 722 } 723 if (keg->uk_flags & UMA_ZONE_OFFPAGE) 724 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 725 #ifdef UMA_DEBUG 726 printf("%s: Returning %d bytes.\n", keg->uk_name, 727 PAGE_SIZE * keg->uk_ppera); 728 #endif 729 keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 730 } 731 732 /* 733 * Frees pages from a keg back to the system. This is done on demand from 734 * the pageout daemon. 735 * 736 * Returns nothing. 737 */ 738 static void 739 keg_drain(uma_keg_t keg) 740 { 741 struct slabhead freeslabs = { 0 }; 742 uma_slab_t slab; 743 uma_slab_t n; 744 745 /* 746 * We don't want to take pages from statically allocated kegs at this 747 * time 748 */ 749 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 750 return; 751 752 #ifdef UMA_DEBUG 753 printf("%s free items: %u\n", keg->uk_name, keg->uk_free); 754 #endif 755 KEG_LOCK(keg); 756 if (keg->uk_free == 0) 757 goto finished; 758 759 slab = LIST_FIRST(&keg->uk_free_slab); 760 while (slab) { 761 n = LIST_NEXT(slab, us_link); 762 763 /* We have no where to free these to */ 764 if (slab->us_flags & UMA_SLAB_BOOT) { 765 slab = n; 766 continue; 767 } 768 769 LIST_REMOVE(slab, us_link); 770 keg->uk_pages -= keg->uk_ppera; 771 keg->uk_free -= keg->uk_ipers; 772 773 if (keg->uk_flags & UMA_ZONE_HASH) 774 UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data); 775 776 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink); 777 778 slab = n; 779 } 780 finished: 781 KEG_UNLOCK(keg); 782 783 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) { 784 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink); 785 keg_free_slab(keg, slab, 0); 786 } 787 } 788 789 static void 790 zone_drain_wait(uma_zone_t zone, int waitok) 791 { 792 793 /* 794 * Set draining to interlock with zone_dtor() so we can release our 795 * locks as we go. Only dtor() should do a WAITOK call since it 796 * is the only call that knows the structure will still be available 797 * when it wakes up. 798 */ 799 ZONE_LOCK(zone); 800 while (zone->uz_flags & UMA_ZFLAG_DRAINING) { 801 if (waitok == M_NOWAIT) 802 goto out; 803 mtx_unlock(&uma_mtx); 804 msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1); 805 mtx_lock(&uma_mtx); 806 } 807 zone->uz_flags |= UMA_ZFLAG_DRAINING; 808 bucket_cache_drain(zone); 809 ZONE_UNLOCK(zone); 810 /* 811 * The DRAINING flag protects us from being freed while 812 * we're running. Normally the uma_mtx would protect us but we 813 * must be able to release and acquire the right lock for each keg. 814 */ 815 zone_foreach_keg(zone, &keg_drain); 816 ZONE_LOCK(zone); 817 zone->uz_flags &= ~UMA_ZFLAG_DRAINING; 818 wakeup(zone); 819 out: 820 ZONE_UNLOCK(zone); 821 } 822 823 void 824 zone_drain(uma_zone_t zone) 825 { 826 827 zone_drain_wait(zone, M_NOWAIT); 828 } 829 830 /* 831 * Allocate a new slab for a keg. This does not insert the slab onto a list. 832 * 833 * Arguments: 834 * wait Shall we wait? 835 * 836 * Returns: 837 * The slab that was allocated or NULL if there is no memory and the 838 * caller specified M_NOWAIT. 839 */ 840 static uma_slab_t 841 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait) 842 { 843 uma_slabrefcnt_t slabref; 844 uma_alloc allocf; 845 uma_slab_t slab; 846 uint8_t *mem; 847 uint8_t flags; 848 int i; 849 850 mtx_assert(&keg->uk_lock, MA_OWNED); 851 slab = NULL; 852 mem = NULL; 853 854 #ifdef UMA_DEBUG 855 printf("alloc_slab: Allocating a new slab for %s\n", keg->uk_name); 856 #endif 857 allocf = keg->uk_allocf; 858 KEG_UNLOCK(keg); 859 860 if (keg->uk_flags & UMA_ZONE_OFFPAGE) { 861 slab = zone_alloc_item(keg->uk_slabzone, NULL, wait); 862 if (slab == NULL) 863 goto out; 864 } 865 866 /* 867 * This reproduces the old vm_zone behavior of zero filling pages the 868 * first time they are added to a zone. 869 * 870 * Malloced items are zeroed in uma_zalloc. 871 */ 872 873 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 874 wait |= M_ZERO; 875 else 876 wait &= ~M_ZERO; 877 878 if (keg->uk_flags & UMA_ZONE_NODUMP) 879 wait |= M_NODUMP; 880 881 /* zone is passed for legacy reasons. */ 882 mem = allocf(zone, keg->uk_ppera * PAGE_SIZE, &flags, wait); 883 if (mem == NULL) { 884 if (keg->uk_flags & UMA_ZONE_OFFPAGE) 885 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 886 slab = NULL; 887 goto out; 888 } 889 890 /* Point the slab into the allocated memory */ 891 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) 892 slab = (uma_slab_t )(mem + keg->uk_pgoff); 893 894 if (keg->uk_flags & UMA_ZONE_VTOSLAB) 895 for (i = 0; i < keg->uk_ppera; i++) 896 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab); 897 898 slab->us_keg = keg; 899 slab->us_data = mem; 900 slab->us_freecount = keg->uk_ipers; 901 slab->us_flags = flags; 902 BIT_FILL(SLAB_SETSIZE, &slab->us_free); 903 #ifdef INVARIANTS 904 BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree); 905 #endif 906 if (keg->uk_flags & UMA_ZONE_REFCNT) { 907 slabref = (uma_slabrefcnt_t)slab; 908 for (i = 0; i < keg->uk_ipers; i++) 909 slabref->us_refcnt[i] = 0; 910 } 911 912 if (keg->uk_init != NULL) { 913 for (i = 0; i < keg->uk_ipers; i++) 914 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i), 915 keg->uk_size, wait) != 0) 916 break; 917 if (i != keg->uk_ipers) { 918 keg_free_slab(keg, slab, i); 919 slab = NULL; 920 goto out; 921 } 922 } 923 out: 924 KEG_LOCK(keg); 925 926 if (slab != NULL) { 927 if (keg->uk_flags & UMA_ZONE_HASH) 928 UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 929 930 keg->uk_pages += keg->uk_ppera; 931 keg->uk_free += keg->uk_ipers; 932 } 933 934 return (slab); 935 } 936 937 /* 938 * This function is intended to be used early on in place of page_alloc() so 939 * that we may use the boot time page cache to satisfy allocations before 940 * the VM is ready. 941 */ 942 static void * 943 startup_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait) 944 { 945 uma_keg_t keg; 946 uma_slab_t tmps; 947 int pages, check_pages; 948 949 keg = zone_first_keg(zone); 950 pages = howmany(bytes, PAGE_SIZE); 951 check_pages = pages - 1; 952 KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n")); 953 954 /* 955 * Check our small startup cache to see if it has pages remaining. 956 */ 957 mtx_lock(&uma_boot_pages_mtx); 958 959 /* First check if we have enough room. */ 960 tmps = LIST_FIRST(&uma_boot_pages); 961 while (tmps != NULL && check_pages-- > 0) 962 tmps = LIST_NEXT(tmps, us_link); 963 if (tmps != NULL) { 964 /* 965 * It's ok to lose tmps references. The last one will 966 * have tmps->us_data pointing to the start address of 967 * "pages" contiguous pages of memory. 968 */ 969 while (pages-- > 0) { 970 tmps = LIST_FIRST(&uma_boot_pages); 971 LIST_REMOVE(tmps, us_link); 972 } 973 mtx_unlock(&uma_boot_pages_mtx); 974 *pflag = tmps->us_flags; 975 return (tmps->us_data); 976 } 977 mtx_unlock(&uma_boot_pages_mtx); 978 if (booted < UMA_STARTUP2) 979 panic("UMA: Increase vm.boot_pages"); 980 /* 981 * Now that we've booted reset these users to their real allocator. 982 */ 983 #ifdef UMA_MD_SMALL_ALLOC 984 keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc; 985 #else 986 keg->uk_allocf = page_alloc; 987 #endif 988 return keg->uk_allocf(zone, bytes, pflag, wait); 989 } 990 991 /* 992 * Allocates a number of pages from the system 993 * 994 * Arguments: 995 * bytes The number of bytes requested 996 * wait Shall we wait? 997 * 998 * Returns: 999 * A pointer to the alloced memory or possibly 1000 * NULL if M_NOWAIT is set. 1001 */ 1002 static void * 1003 page_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait) 1004 { 1005 void *p; /* Returned page */ 1006 1007 *pflag = UMA_SLAB_KMEM; 1008 p = (void *) kmem_malloc(kmem_map, bytes, wait); 1009 1010 return (p); 1011 } 1012 1013 /* 1014 * Allocates a number of pages from within an object 1015 * 1016 * Arguments: 1017 * bytes The number of bytes requested 1018 * wait Shall we wait? 1019 * 1020 * Returns: 1021 * A pointer to the alloced memory or possibly 1022 * NULL if M_NOWAIT is set. 1023 */ 1024 static void * 1025 noobj_alloc(uma_zone_t zone, int bytes, uint8_t *flags, int wait) 1026 { 1027 TAILQ_HEAD(, vm_page) alloctail; 1028 u_long npages; 1029 vm_offset_t retkva, zkva; 1030 vm_page_t p, p_next; 1031 uma_keg_t keg; 1032 1033 TAILQ_INIT(&alloctail); 1034 keg = zone_first_keg(zone); 1035 1036 npages = howmany(bytes, PAGE_SIZE); 1037 while (npages > 0) { 1038 p = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT | 1039 VM_ALLOC_WIRED | VM_ALLOC_NOOBJ); 1040 if (p != NULL) { 1041 /* 1042 * Since the page does not belong to an object, its 1043 * listq is unused. 1044 */ 1045 TAILQ_INSERT_TAIL(&alloctail, p, listq); 1046 npages--; 1047 continue; 1048 } 1049 if (wait & M_WAITOK) { 1050 VM_WAIT; 1051 continue; 1052 } 1053 1054 /* 1055 * Page allocation failed, free intermediate pages and 1056 * exit. 1057 */ 1058 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 1059 vm_page_unwire(p, 0); 1060 vm_page_free(p); 1061 } 1062 return (NULL); 1063 } 1064 *flags = UMA_SLAB_PRIV; 1065 zkva = keg->uk_kva + 1066 atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1067 retkva = zkva; 1068 TAILQ_FOREACH(p, &alloctail, listq) { 1069 pmap_qenter(zkva, &p, 1); 1070 zkva += PAGE_SIZE; 1071 } 1072 1073 return ((void *)retkva); 1074 } 1075 1076 /* 1077 * Frees a number of pages to the system 1078 * 1079 * Arguments: 1080 * mem A pointer to the memory to be freed 1081 * size The size of the memory being freed 1082 * flags The original p->us_flags field 1083 * 1084 * Returns: 1085 * Nothing 1086 */ 1087 static void 1088 page_free(void *mem, int size, uint8_t flags) 1089 { 1090 vm_map_t map; 1091 1092 if (flags & UMA_SLAB_KMEM) 1093 map = kmem_map; 1094 else if (flags & UMA_SLAB_KERNEL) 1095 map = kernel_map; 1096 else 1097 panic("UMA: page_free used with invalid flags %d", flags); 1098 1099 kmem_free(map, (vm_offset_t)mem, size); 1100 } 1101 1102 /* 1103 * Zero fill initializer 1104 * 1105 * Arguments/Returns follow uma_init specifications 1106 */ 1107 static int 1108 zero_init(void *mem, int size, int flags) 1109 { 1110 bzero(mem, size); 1111 return (0); 1112 } 1113 1114 /* 1115 * Finish creating a small uma keg. This calculates ipers, and the keg size. 1116 * 1117 * Arguments 1118 * keg The zone we should initialize 1119 * 1120 * Returns 1121 * Nothing 1122 */ 1123 static void 1124 keg_small_init(uma_keg_t keg) 1125 { 1126 u_int rsize; 1127 u_int memused; 1128 u_int wastedspace; 1129 u_int shsize; 1130 1131 if (keg->uk_flags & UMA_ZONE_PCPU) { 1132 KASSERT(mp_ncpus > 0, ("%s: ncpus %d\n", __func__, mp_ncpus)); 1133 keg->uk_slabsize = sizeof(struct pcpu); 1134 keg->uk_ppera = howmany(mp_ncpus * sizeof(struct pcpu), 1135 PAGE_SIZE); 1136 } else { 1137 keg->uk_slabsize = UMA_SLAB_SIZE; 1138 keg->uk_ppera = 1; 1139 } 1140 1141 /* 1142 * Calculate the size of each allocation (rsize) according to 1143 * alignment. If the requested size is smaller than we have 1144 * allocation bits for we round it up. 1145 */ 1146 rsize = keg->uk_size; 1147 if (rsize < keg->uk_slabsize / SLAB_SETSIZE) 1148 rsize = keg->uk_slabsize / SLAB_SETSIZE; 1149 if (rsize & keg->uk_align) 1150 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1); 1151 keg->uk_rsize = rsize; 1152 1153 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 1154 keg->uk_rsize < sizeof(struct pcpu), 1155 ("%s: size %u too large", __func__, keg->uk_rsize)); 1156 1157 if (keg->uk_flags & UMA_ZONE_REFCNT) 1158 rsize += sizeof(uint32_t); 1159 1160 if (keg->uk_flags & UMA_ZONE_OFFPAGE) 1161 shsize = 0; 1162 else 1163 shsize = sizeof(struct uma_slab); 1164 1165 keg->uk_ipers = (keg->uk_slabsize - shsize) / rsize; 1166 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE, 1167 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 1168 1169 memused = keg->uk_ipers * rsize + shsize; 1170 wastedspace = keg->uk_slabsize - memused; 1171 1172 /* 1173 * We can't do OFFPAGE if we're internal or if we've been 1174 * asked to not go to the VM for buckets. If we do this we 1175 * may end up going to the VM (kmem_map) for slabs which we 1176 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a 1177 * result of UMA_ZONE_VM, which clearly forbids it. 1178 */ 1179 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) || 1180 (keg->uk_flags & UMA_ZFLAG_CACHEONLY)) 1181 return; 1182 1183 /* 1184 * See if using an OFFPAGE slab will limit our waste. Only do 1185 * this if it permits more items per-slab. 1186 * 1187 * XXX We could try growing slabsize to limit max waste as well. 1188 * Historically this was not done because the VM could not 1189 * efficiently handle contiguous allocations. 1190 */ 1191 if ((wastedspace >= keg->uk_slabsize / UMA_MAX_WASTE) && 1192 (keg->uk_ipers < (keg->uk_slabsize / keg->uk_rsize))) { 1193 keg->uk_ipers = keg->uk_slabsize / keg->uk_rsize; 1194 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE, 1195 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 1196 #ifdef UMA_DEBUG 1197 printf("UMA decided we need offpage slab headers for " 1198 "keg: %s, calculated wastedspace = %d, " 1199 "maximum wasted space allowed = %d, " 1200 "calculated ipers = %d, " 1201 "new wasted space = %d\n", keg->uk_name, wastedspace, 1202 keg->uk_slabsize / UMA_MAX_WASTE, keg->uk_ipers, 1203 keg->uk_slabsize - keg->uk_ipers * keg->uk_rsize); 1204 #endif 1205 keg->uk_flags |= UMA_ZONE_OFFPAGE; 1206 } 1207 1208 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) && 1209 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1210 keg->uk_flags |= UMA_ZONE_HASH; 1211 } 1212 1213 /* 1214 * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do 1215 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be 1216 * more complicated. 1217 * 1218 * Arguments 1219 * keg The keg we should initialize 1220 * 1221 * Returns 1222 * Nothing 1223 */ 1224 static void 1225 keg_large_init(uma_keg_t keg) 1226 { 1227 1228 KASSERT(keg != NULL, ("Keg is null in keg_large_init")); 1229 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0, 1230 ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg")); 1231 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1232 ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__)); 1233 1234 keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE); 1235 keg->uk_slabsize = keg->uk_ppera * PAGE_SIZE; 1236 keg->uk_ipers = 1; 1237 keg->uk_rsize = keg->uk_size; 1238 1239 /* We can't do OFFPAGE if we're internal, bail out here. */ 1240 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) 1241 return; 1242 1243 keg->uk_flags |= UMA_ZONE_OFFPAGE; 1244 if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1245 keg->uk_flags |= UMA_ZONE_HASH; 1246 } 1247 1248 static void 1249 keg_cachespread_init(uma_keg_t keg) 1250 { 1251 int alignsize; 1252 int trailer; 1253 int pages; 1254 int rsize; 1255 1256 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1257 ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__)); 1258 1259 alignsize = keg->uk_align + 1; 1260 rsize = keg->uk_size; 1261 /* 1262 * We want one item to start on every align boundary in a page. To 1263 * do this we will span pages. We will also extend the item by the 1264 * size of align if it is an even multiple of align. Otherwise, it 1265 * would fall on the same boundary every time. 1266 */ 1267 if (rsize & keg->uk_align) 1268 rsize = (rsize & ~keg->uk_align) + alignsize; 1269 if ((rsize & alignsize) == 0) 1270 rsize += alignsize; 1271 trailer = rsize - keg->uk_size; 1272 pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE; 1273 pages = MIN(pages, (128 * 1024) / PAGE_SIZE); 1274 keg->uk_rsize = rsize; 1275 keg->uk_ppera = pages; 1276 keg->uk_slabsize = UMA_SLAB_SIZE; 1277 keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize; 1278 keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 1279 KASSERT(keg->uk_ipers <= uma_max_ipers, 1280 ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__, 1281 keg->uk_ipers)); 1282 } 1283 1284 /* 1285 * Keg header ctor. This initializes all fields, locks, etc. And inserts 1286 * the keg onto the global keg list. 1287 * 1288 * Arguments/Returns follow uma_ctor specifications 1289 * udata Actually uma_kctor_args 1290 */ 1291 static int 1292 keg_ctor(void *mem, int size, void *udata, int flags) 1293 { 1294 struct uma_kctor_args *arg = udata; 1295 uma_keg_t keg = mem; 1296 uma_zone_t zone; 1297 1298 bzero(keg, size); 1299 keg->uk_size = arg->size; 1300 keg->uk_init = arg->uminit; 1301 keg->uk_fini = arg->fini; 1302 keg->uk_align = arg->align; 1303 keg->uk_free = 0; 1304 keg->uk_pages = 0; 1305 keg->uk_flags = arg->flags; 1306 keg->uk_allocf = page_alloc; 1307 keg->uk_freef = page_free; 1308 keg->uk_slabzone = NULL; 1309 1310 /* 1311 * The master zone is passed to us at keg-creation time. 1312 */ 1313 zone = arg->zone; 1314 keg->uk_name = zone->uz_name; 1315 1316 if (arg->flags & UMA_ZONE_VM) 1317 keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 1318 1319 if (arg->flags & UMA_ZONE_ZINIT) 1320 keg->uk_init = zero_init; 1321 1322 if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC) 1323 keg->uk_flags |= UMA_ZONE_VTOSLAB; 1324 1325 if (arg->flags & UMA_ZONE_PCPU) 1326 #ifdef SMP 1327 keg->uk_flags |= UMA_ZONE_OFFPAGE; 1328 #else 1329 keg->uk_flags &= ~UMA_ZONE_PCPU; 1330 #endif 1331 1332 if (keg->uk_flags & UMA_ZONE_CACHESPREAD) { 1333 keg_cachespread_init(keg); 1334 } else if (keg->uk_flags & UMA_ZONE_REFCNT) { 1335 if (keg->uk_size > 1336 (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) - 1337 sizeof(uint32_t))) 1338 keg_large_init(keg); 1339 else 1340 keg_small_init(keg); 1341 } else { 1342 if (keg->uk_size > (UMA_SLAB_SIZE - sizeof(struct uma_slab))) 1343 keg_large_init(keg); 1344 else 1345 keg_small_init(keg); 1346 } 1347 1348 if (keg->uk_flags & UMA_ZONE_OFFPAGE) { 1349 if (keg->uk_flags & UMA_ZONE_REFCNT) { 1350 if (keg->uk_ipers > uma_max_ipers_ref) 1351 panic("Too many ref items per zone: %d > %d\n", 1352 keg->uk_ipers, uma_max_ipers_ref); 1353 keg->uk_slabzone = slabrefzone; 1354 } else 1355 keg->uk_slabzone = slabzone; 1356 } 1357 1358 /* 1359 * If we haven't booted yet we need allocations to go through the 1360 * startup cache until the vm is ready. 1361 */ 1362 if (keg->uk_ppera == 1) { 1363 #ifdef UMA_MD_SMALL_ALLOC 1364 keg->uk_allocf = uma_small_alloc; 1365 keg->uk_freef = uma_small_free; 1366 1367 if (booted < UMA_STARTUP) 1368 keg->uk_allocf = startup_alloc; 1369 #else 1370 if (booted < UMA_STARTUP2) 1371 keg->uk_allocf = startup_alloc; 1372 #endif 1373 } else if (booted < UMA_STARTUP2 && 1374 (keg->uk_flags & UMA_ZFLAG_INTERNAL)) 1375 keg->uk_allocf = startup_alloc; 1376 1377 /* 1378 * Initialize keg's lock 1379 */ 1380 KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS)); 1381 1382 /* 1383 * If we're putting the slab header in the actual page we need to 1384 * figure out where in each page it goes. This calculates a right 1385 * justified offset into the memory on an ALIGN_PTR boundary. 1386 */ 1387 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) { 1388 u_int totsize; 1389 1390 /* Size of the slab struct and free list */ 1391 totsize = sizeof(struct uma_slab); 1392 1393 /* Size of the reference counts. */ 1394 if (keg->uk_flags & UMA_ZONE_REFCNT) 1395 totsize += keg->uk_ipers * sizeof(uint32_t); 1396 1397 if (totsize & UMA_ALIGN_PTR) 1398 totsize = (totsize & ~UMA_ALIGN_PTR) + 1399 (UMA_ALIGN_PTR + 1); 1400 keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize; 1401 1402 /* 1403 * The only way the following is possible is if with our 1404 * UMA_ALIGN_PTR adjustments we are now bigger than 1405 * UMA_SLAB_SIZE. I haven't checked whether this is 1406 * mathematically possible for all cases, so we make 1407 * sure here anyway. 1408 */ 1409 totsize = keg->uk_pgoff + sizeof(struct uma_slab); 1410 if (keg->uk_flags & UMA_ZONE_REFCNT) 1411 totsize += keg->uk_ipers * sizeof(uint32_t); 1412 if (totsize > PAGE_SIZE * keg->uk_ppera) { 1413 printf("zone %s ipers %d rsize %d size %d\n", 1414 zone->uz_name, keg->uk_ipers, keg->uk_rsize, 1415 keg->uk_size); 1416 panic("UMA slab won't fit."); 1417 } 1418 } 1419 1420 if (keg->uk_flags & UMA_ZONE_HASH) 1421 hash_alloc(&keg->uk_hash); 1422 1423 #ifdef UMA_DEBUG 1424 printf("UMA: %s(%p) size %d(%d) flags %#x ipers %d ppera %d out %d free %d\n", 1425 zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags, 1426 keg->uk_ipers, keg->uk_ppera, 1427 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free); 1428 #endif 1429 1430 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 1431 1432 mtx_lock(&uma_mtx); 1433 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 1434 mtx_unlock(&uma_mtx); 1435 return (0); 1436 } 1437 1438 /* 1439 * Zone header ctor. This initializes all fields, locks, etc. 1440 * 1441 * Arguments/Returns follow uma_ctor specifications 1442 * udata Actually uma_zctor_args 1443 */ 1444 static int 1445 zone_ctor(void *mem, int size, void *udata, int flags) 1446 { 1447 struct uma_zctor_args *arg = udata; 1448 uma_zone_t zone = mem; 1449 uma_zone_t z; 1450 uma_keg_t keg; 1451 1452 bzero(zone, size); 1453 zone->uz_name = arg->name; 1454 zone->uz_ctor = arg->ctor; 1455 zone->uz_dtor = arg->dtor; 1456 zone->uz_slab = zone_fetch_slab; 1457 zone->uz_init = NULL; 1458 zone->uz_fini = NULL; 1459 zone->uz_allocs = 0; 1460 zone->uz_frees = 0; 1461 zone->uz_fails = 0; 1462 zone->uz_sleeps = 0; 1463 zone->uz_count = 0; 1464 zone->uz_flags = 0; 1465 zone->uz_warning = NULL; 1466 timevalclear(&zone->uz_ratecheck); 1467 keg = arg->keg; 1468 1469 ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 1470 1471 /* 1472 * This is a pure cache zone, no kegs. 1473 */ 1474 if (arg->import) { 1475 zone->uz_size = arg->size; 1476 zone->uz_import = arg->import; 1477 zone->uz_release = arg->release; 1478 zone->uz_arg = arg->arg; 1479 zone->uz_lockptr = &zone->uz_lock; 1480 goto out; 1481 } 1482 1483 /* 1484 * Use the regular zone/keg/slab allocator. 1485 */ 1486 zone->uz_import = (uma_import)zone_import; 1487 zone->uz_release = (uma_release)zone_release; 1488 zone->uz_arg = zone; 1489 1490 if (arg->flags & UMA_ZONE_SECONDARY) { 1491 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 1492 zone->uz_init = arg->uminit; 1493 zone->uz_fini = arg->fini; 1494 zone->uz_lockptr = &keg->uk_lock; 1495 zone->uz_flags |= UMA_ZONE_SECONDARY; 1496 mtx_lock(&uma_mtx); 1497 ZONE_LOCK(zone); 1498 LIST_FOREACH(z, &keg->uk_zones, uz_link) { 1499 if (LIST_NEXT(z, uz_link) == NULL) { 1500 LIST_INSERT_AFTER(z, zone, uz_link); 1501 break; 1502 } 1503 } 1504 ZONE_UNLOCK(zone); 1505 mtx_unlock(&uma_mtx); 1506 } else if (keg == NULL) { 1507 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 1508 arg->align, arg->flags)) == NULL) 1509 return (ENOMEM); 1510 } else { 1511 struct uma_kctor_args karg; 1512 int error; 1513 1514 /* We should only be here from uma_startup() */ 1515 karg.size = arg->size; 1516 karg.uminit = arg->uminit; 1517 karg.fini = arg->fini; 1518 karg.align = arg->align; 1519 karg.flags = arg->flags; 1520 karg.zone = zone; 1521 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 1522 flags); 1523 if (error) 1524 return (error); 1525 } 1526 1527 /* 1528 * Link in the first keg. 1529 */ 1530 zone->uz_klink.kl_keg = keg; 1531 LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link); 1532 zone->uz_lockptr = &keg->uk_lock; 1533 zone->uz_size = keg->uk_size; 1534 zone->uz_flags |= (keg->uk_flags & 1535 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 1536 1537 /* 1538 * Some internal zones don't have room allocated for the per cpu 1539 * caches. If we're internal, bail out here. 1540 */ 1541 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) { 1542 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 1543 ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 1544 return (0); 1545 } 1546 1547 out: 1548 if ((arg->flags & UMA_ZONE_MAXBUCKET) == 0) 1549 zone->uz_count = bucket_select(zone->uz_size); 1550 else 1551 zone->uz_count = BUCKET_MAX; 1552 1553 return (0); 1554 } 1555 1556 /* 1557 * Keg header dtor. This frees all data, destroys locks, frees the hash 1558 * table and removes the keg from the global list. 1559 * 1560 * Arguments/Returns follow uma_dtor specifications 1561 * udata unused 1562 */ 1563 static void 1564 keg_dtor(void *arg, int size, void *udata) 1565 { 1566 uma_keg_t keg; 1567 1568 keg = (uma_keg_t)arg; 1569 KEG_LOCK(keg); 1570 if (keg->uk_free != 0) { 1571 printf("Freed UMA keg was not empty (%d items). " 1572 " Lost %d pages of memory.\n", 1573 keg->uk_free, keg->uk_pages); 1574 } 1575 KEG_UNLOCK(keg); 1576 1577 hash_free(&keg->uk_hash); 1578 1579 KEG_LOCK_FINI(keg); 1580 } 1581 1582 /* 1583 * Zone header dtor. 1584 * 1585 * Arguments/Returns follow uma_dtor specifications 1586 * udata unused 1587 */ 1588 static void 1589 zone_dtor(void *arg, int size, void *udata) 1590 { 1591 uma_klink_t klink; 1592 uma_zone_t zone; 1593 uma_keg_t keg; 1594 1595 zone = (uma_zone_t)arg; 1596 keg = zone_first_keg(zone); 1597 1598 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 1599 cache_drain(zone); 1600 1601 mtx_lock(&uma_mtx); 1602 LIST_REMOVE(zone, uz_link); 1603 mtx_unlock(&uma_mtx); 1604 /* 1605 * XXX there are some races here where 1606 * the zone can be drained but zone lock 1607 * released and then refilled before we 1608 * remove it... we dont care for now 1609 */ 1610 zone_drain_wait(zone, M_WAITOK); 1611 /* 1612 * Unlink all of our kegs. 1613 */ 1614 while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) { 1615 klink->kl_keg = NULL; 1616 LIST_REMOVE(klink, kl_link); 1617 if (klink == &zone->uz_klink) 1618 continue; 1619 free(klink, M_TEMP); 1620 } 1621 /* 1622 * We only destroy kegs from non secondary zones. 1623 */ 1624 if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0) { 1625 mtx_lock(&uma_mtx); 1626 LIST_REMOVE(keg, uk_link); 1627 mtx_unlock(&uma_mtx); 1628 zone_free_item(kegs, keg, NULL, SKIP_NONE); 1629 } 1630 ZONE_LOCK_FINI(zone); 1631 } 1632 1633 /* 1634 * Traverses every zone in the system and calls a callback 1635 * 1636 * Arguments: 1637 * zfunc A pointer to a function which accepts a zone 1638 * as an argument. 1639 * 1640 * Returns: 1641 * Nothing 1642 */ 1643 static void 1644 zone_foreach(void (*zfunc)(uma_zone_t)) 1645 { 1646 uma_keg_t keg; 1647 uma_zone_t zone; 1648 1649 mtx_lock(&uma_mtx); 1650 LIST_FOREACH(keg, &uma_kegs, uk_link) { 1651 LIST_FOREACH(zone, &keg->uk_zones, uz_link) 1652 zfunc(zone); 1653 } 1654 mtx_unlock(&uma_mtx); 1655 } 1656 1657 /* Public functions */ 1658 /* See uma.h */ 1659 void 1660 uma_startup(void *bootmem, int boot_pages) 1661 { 1662 struct uma_zctor_args args; 1663 uma_slab_t slab; 1664 u_int slabsize; 1665 int i; 1666 1667 #ifdef UMA_DEBUG 1668 printf("Creating uma keg headers zone and keg.\n"); 1669 #endif 1670 mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF); 1671 1672 /* "manually" create the initial zone */ 1673 memset(&args, 0, sizeof(args)); 1674 args.name = "UMA Kegs"; 1675 args.size = sizeof(struct uma_keg); 1676 args.ctor = keg_ctor; 1677 args.dtor = keg_dtor; 1678 args.uminit = zero_init; 1679 args.fini = NULL; 1680 args.keg = &masterkeg; 1681 args.align = 32 - 1; 1682 args.flags = UMA_ZFLAG_INTERNAL; 1683 /* The initial zone has no Per cpu queues so it's smaller */ 1684 zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK); 1685 1686 #ifdef UMA_DEBUG 1687 printf("Filling boot free list.\n"); 1688 #endif 1689 for (i = 0; i < boot_pages; i++) { 1690 slab = (uma_slab_t)((uint8_t *)bootmem + (i * UMA_SLAB_SIZE)); 1691 slab->us_data = (uint8_t *)slab; 1692 slab->us_flags = UMA_SLAB_BOOT; 1693 LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link); 1694 } 1695 mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF); 1696 1697 #ifdef UMA_DEBUG 1698 printf("Creating uma zone headers zone and keg.\n"); 1699 #endif 1700 args.name = "UMA Zones"; 1701 args.size = sizeof(struct uma_zone) + 1702 (sizeof(struct uma_cache) * (mp_maxid + 1)); 1703 args.ctor = zone_ctor; 1704 args.dtor = zone_dtor; 1705 args.uminit = zero_init; 1706 args.fini = NULL; 1707 args.keg = NULL; 1708 args.align = 32 - 1; 1709 args.flags = UMA_ZFLAG_INTERNAL; 1710 /* The initial zone has no Per cpu queues so it's smaller */ 1711 zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK); 1712 1713 #ifdef UMA_DEBUG 1714 printf("Initializing pcpu cache locks.\n"); 1715 #endif 1716 #ifdef UMA_DEBUG 1717 printf("Creating slab and hash zones.\n"); 1718 #endif 1719 1720 /* Now make a zone for slab headers */ 1721 slabzone = uma_zcreate("UMA Slabs", 1722 sizeof(struct uma_slab), 1723 NULL, NULL, NULL, NULL, 1724 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 1725 1726 /* 1727 * We also create a zone for the bigger slabs with reference 1728 * counts in them, to accomodate UMA_ZONE_REFCNT zones. 1729 */ 1730 slabsize = sizeof(struct uma_slab_refcnt); 1731 slabsize += uma_max_ipers_ref * sizeof(uint32_t); 1732 slabrefzone = uma_zcreate("UMA RCntSlabs", 1733 slabsize, 1734 NULL, NULL, NULL, NULL, 1735 UMA_ALIGN_PTR, 1736 UMA_ZFLAG_INTERNAL); 1737 1738 hashzone = uma_zcreate("UMA Hash", 1739 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 1740 NULL, NULL, NULL, NULL, 1741 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 1742 1743 bucket_init(); 1744 1745 booted = UMA_STARTUP; 1746 1747 #ifdef UMA_DEBUG 1748 printf("UMA startup complete.\n"); 1749 #endif 1750 } 1751 1752 /* see uma.h */ 1753 void 1754 uma_startup2(void) 1755 { 1756 booted = UMA_STARTUP2; 1757 bucket_enable(); 1758 #ifdef UMA_DEBUG 1759 printf("UMA startup2 complete.\n"); 1760 #endif 1761 } 1762 1763 /* 1764 * Initialize our callout handle 1765 * 1766 */ 1767 1768 static void 1769 uma_startup3(void) 1770 { 1771 #ifdef UMA_DEBUG 1772 printf("Starting callout.\n"); 1773 #endif 1774 callout_init(&uma_callout, CALLOUT_MPSAFE); 1775 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 1776 #ifdef UMA_DEBUG 1777 printf("UMA startup3 complete.\n"); 1778 #endif 1779 } 1780 1781 static uma_keg_t 1782 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 1783 int align, uint32_t flags) 1784 { 1785 struct uma_kctor_args args; 1786 1787 args.size = size; 1788 args.uminit = uminit; 1789 args.fini = fini; 1790 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 1791 args.flags = flags; 1792 args.zone = zone; 1793 return (zone_alloc_item(kegs, &args, M_WAITOK)); 1794 } 1795 1796 /* See uma.h */ 1797 void 1798 uma_set_align(int align) 1799 { 1800 1801 if (align != UMA_ALIGN_CACHE) 1802 uma_align_cache = align; 1803 } 1804 1805 /* See uma.h */ 1806 uma_zone_t 1807 uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 1808 uma_init uminit, uma_fini fini, int align, uint32_t flags) 1809 1810 { 1811 struct uma_zctor_args args; 1812 1813 /* This stuff is essential for the zone ctor */ 1814 memset(&args, 0, sizeof(args)); 1815 args.name = name; 1816 args.size = size; 1817 args.ctor = ctor; 1818 args.dtor = dtor; 1819 args.uminit = uminit; 1820 args.fini = fini; 1821 args.align = align; 1822 args.flags = flags; 1823 args.keg = NULL; 1824 1825 return (zone_alloc_item(zones, &args, M_WAITOK)); 1826 } 1827 1828 /* See uma.h */ 1829 uma_zone_t 1830 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 1831 uma_init zinit, uma_fini zfini, uma_zone_t master) 1832 { 1833 struct uma_zctor_args args; 1834 uma_keg_t keg; 1835 1836 keg = zone_first_keg(master); 1837 memset(&args, 0, sizeof(args)); 1838 args.name = name; 1839 args.size = keg->uk_size; 1840 args.ctor = ctor; 1841 args.dtor = dtor; 1842 args.uminit = zinit; 1843 args.fini = zfini; 1844 args.align = keg->uk_align; 1845 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 1846 args.keg = keg; 1847 1848 /* XXX Attaches only one keg of potentially many. */ 1849 return (zone_alloc_item(zones, &args, M_WAITOK)); 1850 } 1851 1852 /* See uma.h */ 1853 uma_zone_t 1854 uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 1855 uma_init zinit, uma_fini zfini, uma_import zimport, 1856 uma_release zrelease, void *arg, int flags) 1857 { 1858 struct uma_zctor_args args; 1859 1860 memset(&args, 0, sizeof(args)); 1861 args.name = name; 1862 args.size = size; 1863 args.ctor = ctor; 1864 args.dtor = dtor; 1865 args.uminit = zinit; 1866 args.fini = zfini; 1867 args.import = zimport; 1868 args.release = zrelease; 1869 args.arg = arg; 1870 args.align = 0; 1871 args.flags = flags; 1872 1873 return (zone_alloc_item(zones, &args, M_WAITOK)); 1874 } 1875 1876 static void 1877 zone_lock_pair(uma_zone_t a, uma_zone_t b) 1878 { 1879 if (a < b) { 1880 ZONE_LOCK(a); 1881 mtx_lock_flags(b->uz_lockptr, MTX_DUPOK); 1882 } else { 1883 ZONE_LOCK(b); 1884 mtx_lock_flags(a->uz_lockptr, MTX_DUPOK); 1885 } 1886 } 1887 1888 static void 1889 zone_unlock_pair(uma_zone_t a, uma_zone_t b) 1890 { 1891 1892 ZONE_UNLOCK(a); 1893 ZONE_UNLOCK(b); 1894 } 1895 1896 int 1897 uma_zsecond_add(uma_zone_t zone, uma_zone_t master) 1898 { 1899 uma_klink_t klink; 1900 uma_klink_t kl; 1901 int error; 1902 1903 error = 0; 1904 klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO); 1905 1906 zone_lock_pair(zone, master); 1907 /* 1908 * zone must use vtoslab() to resolve objects and must already be 1909 * a secondary. 1910 */ 1911 if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) 1912 != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) { 1913 error = EINVAL; 1914 goto out; 1915 } 1916 /* 1917 * The new master must also use vtoslab(). 1918 */ 1919 if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) { 1920 error = EINVAL; 1921 goto out; 1922 } 1923 /* 1924 * Both must either be refcnt, or not be refcnt. 1925 */ 1926 if ((zone->uz_flags & UMA_ZONE_REFCNT) != 1927 (master->uz_flags & UMA_ZONE_REFCNT)) { 1928 error = EINVAL; 1929 goto out; 1930 } 1931 /* 1932 * The underlying object must be the same size. rsize 1933 * may be different. 1934 */ 1935 if (master->uz_size != zone->uz_size) { 1936 error = E2BIG; 1937 goto out; 1938 } 1939 /* 1940 * Put it at the end of the list. 1941 */ 1942 klink->kl_keg = zone_first_keg(master); 1943 LIST_FOREACH(kl, &zone->uz_kegs, kl_link) { 1944 if (LIST_NEXT(kl, kl_link) == NULL) { 1945 LIST_INSERT_AFTER(kl, klink, kl_link); 1946 break; 1947 } 1948 } 1949 klink = NULL; 1950 zone->uz_flags |= UMA_ZFLAG_MULTI; 1951 zone->uz_slab = zone_fetch_slab_multi; 1952 1953 out: 1954 zone_unlock_pair(zone, master); 1955 if (klink != NULL) 1956 free(klink, M_TEMP); 1957 1958 return (error); 1959 } 1960 1961 1962 /* See uma.h */ 1963 void 1964 uma_zdestroy(uma_zone_t zone) 1965 { 1966 1967 zone_free_item(zones, zone, NULL, SKIP_NONE); 1968 } 1969 1970 /* See uma.h */ 1971 void * 1972 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 1973 { 1974 void *item; 1975 uma_cache_t cache; 1976 uma_bucket_t bucket; 1977 int lockfail; 1978 int cpu; 1979 1980 /* This is the fast path allocation */ 1981 #ifdef UMA_DEBUG_ALLOC_1 1982 printf("Allocating one item from %s(%p)\n", zone->uz_name, zone); 1983 #endif 1984 CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread, 1985 zone->uz_name, flags); 1986 1987 if (flags & M_WAITOK) { 1988 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1989 "uma_zalloc_arg: zone \"%s\"", zone->uz_name); 1990 } 1991 #ifdef DEBUG_MEMGUARD 1992 if (memguard_cmp_zone(zone)) { 1993 item = memguard_alloc(zone->uz_size, flags); 1994 if (item != NULL) { 1995 /* 1996 * Avoid conflict with the use-after-free 1997 * protecting infrastructure from INVARIANTS. 1998 */ 1999 if (zone->uz_init != NULL && 2000 zone->uz_init != mtrash_init && 2001 zone->uz_init(item, zone->uz_size, flags) != 0) 2002 return (NULL); 2003 if (zone->uz_ctor != NULL && 2004 zone->uz_ctor != mtrash_ctor && 2005 zone->uz_ctor(item, zone->uz_size, udata, 2006 flags) != 0) { 2007 zone->uz_fini(item, zone->uz_size); 2008 return (NULL); 2009 } 2010 return (item); 2011 } 2012 /* This is unfortunate but should not be fatal. */ 2013 } 2014 #endif 2015 /* 2016 * If possible, allocate from the per-CPU cache. There are two 2017 * requirements for safe access to the per-CPU cache: (1) the thread 2018 * accessing the cache must not be preempted or yield during access, 2019 * and (2) the thread must not migrate CPUs without switching which 2020 * cache it accesses. We rely on a critical section to prevent 2021 * preemption and migration. We release the critical section in 2022 * order to acquire the zone mutex if we are unable to allocate from 2023 * the current cache; when we re-acquire the critical section, we 2024 * must detect and handle migration if it has occurred. 2025 */ 2026 critical_enter(); 2027 cpu = curcpu; 2028 cache = &zone->uz_cpu[cpu]; 2029 2030 zalloc_start: 2031 bucket = cache->uc_allocbucket; 2032 if (bucket != NULL && bucket->ub_cnt > 0) { 2033 bucket->ub_cnt--; 2034 item = bucket->ub_bucket[bucket->ub_cnt]; 2035 #ifdef INVARIANTS 2036 bucket->ub_bucket[bucket->ub_cnt] = NULL; 2037 #endif 2038 KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 2039 cache->uc_allocs++; 2040 critical_exit(); 2041 if (zone->uz_ctor != NULL && 2042 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) { 2043 atomic_add_long(&zone->uz_fails, 1); 2044 zone_free_item(zone, item, udata, SKIP_DTOR); 2045 return (NULL); 2046 } 2047 #ifdef INVARIANTS 2048 uma_dbg_alloc(zone, NULL, item); 2049 #endif 2050 if (flags & M_ZERO) 2051 bzero(item, zone->uz_size); 2052 return (item); 2053 } 2054 2055 /* 2056 * We have run out of items in our alloc bucket. 2057 * See if we can switch with our free bucket. 2058 */ 2059 bucket = cache->uc_freebucket; 2060 if (bucket != NULL && bucket->ub_cnt > 0) { 2061 #ifdef UMA_DEBUG_ALLOC 2062 printf("uma_zalloc: Swapping empty with alloc.\n"); 2063 #endif 2064 cache->uc_freebucket = cache->uc_allocbucket; 2065 cache->uc_allocbucket = bucket; 2066 goto zalloc_start; 2067 } 2068 2069 /* 2070 * Discard any empty allocation bucket while we hold no locks. 2071 */ 2072 bucket = cache->uc_allocbucket; 2073 cache->uc_allocbucket = NULL; 2074 critical_exit(); 2075 if (bucket != NULL) 2076 bucket_free(zone, bucket); 2077 2078 /* Short-circuit for zones without buckets and low memory. */ 2079 if (zone->uz_count == 0 || bucketdisable) 2080 goto zalloc_item; 2081 2082 /* 2083 * Attempt to retrieve the item from the per-CPU cache has failed, so 2084 * we must go back to the zone. This requires the zone lock, so we 2085 * must drop the critical section, then re-acquire it when we go back 2086 * to the cache. Since the critical section is released, we may be 2087 * preempted or migrate. As such, make sure not to maintain any 2088 * thread-local state specific to the cache from prior to releasing 2089 * the critical section. 2090 */ 2091 lockfail = 0; 2092 if (ZONE_TRYLOCK(zone) == 0) { 2093 /* Record contention to size the buckets. */ 2094 ZONE_LOCK(zone); 2095 lockfail = 1; 2096 } 2097 critical_enter(); 2098 cpu = curcpu; 2099 cache = &zone->uz_cpu[cpu]; 2100 2101 /* 2102 * Since we have locked the zone we may as well send back our stats. 2103 */ 2104 atomic_add_long(&zone->uz_allocs, cache->uc_allocs); 2105 atomic_add_long(&zone->uz_frees, cache->uc_frees); 2106 cache->uc_allocs = 0; 2107 cache->uc_frees = 0; 2108 2109 /* See if we lost the race to fill the cache. */ 2110 if (cache->uc_allocbucket != NULL) { 2111 ZONE_UNLOCK(zone); 2112 goto zalloc_start; 2113 } 2114 2115 /* 2116 * Check the zone's cache of buckets. 2117 */ 2118 if ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) { 2119 KASSERT(bucket->ub_cnt != 0, 2120 ("uma_zalloc_arg: Returning an empty bucket.")); 2121 2122 LIST_REMOVE(bucket, ub_link); 2123 cache->uc_allocbucket = bucket; 2124 ZONE_UNLOCK(zone); 2125 goto zalloc_start; 2126 } 2127 /* We are no longer associated with this CPU. */ 2128 critical_exit(); 2129 2130 /* 2131 * We bump the uz count when the cache size is insufficient to 2132 * handle the working set. 2133 */ 2134 if (lockfail && zone->uz_count < BUCKET_MAX && zone->uz_count != 0 && 2135 zone != largebucket) 2136 zone->uz_count++; 2137 ZONE_UNLOCK(zone); 2138 2139 /* 2140 * Now lets just fill a bucket and put it on the free list. If that 2141 * works we'll restart the allocation from the begining and it 2142 * will use the just filled bucket. 2143 */ 2144 bucket = zone_alloc_bucket(zone, flags); 2145 if (bucket != NULL) { 2146 ZONE_LOCK(zone); 2147 critical_enter(); 2148 cpu = curcpu; 2149 cache = &zone->uz_cpu[cpu]; 2150 /* 2151 * See if we lost the race or were migrated. Cache the 2152 * initialized bucket to make this less likely or claim 2153 * the memory directly. 2154 */ 2155 if (cache->uc_allocbucket == NULL) 2156 cache->uc_allocbucket = bucket; 2157 else 2158 LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link); 2159 ZONE_UNLOCK(zone); 2160 goto zalloc_start; 2161 } 2162 2163 /* 2164 * We may not be able to get a bucket so return an actual item. 2165 */ 2166 #ifdef UMA_DEBUG 2167 printf("uma_zalloc_arg: Bucketzone returned NULL\n"); 2168 #endif 2169 2170 zalloc_item: 2171 item = zone_alloc_item(zone, udata, flags); 2172 2173 return (item); 2174 } 2175 2176 static uma_slab_t 2177 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags) 2178 { 2179 uma_slab_t slab; 2180 2181 mtx_assert(&keg->uk_lock, MA_OWNED); 2182 slab = NULL; 2183 2184 for (;;) { 2185 /* 2186 * Find a slab with some space. Prefer slabs that are partially 2187 * used over those that are totally full. This helps to reduce 2188 * fragmentation. 2189 */ 2190 if (keg->uk_free != 0) { 2191 if (!LIST_EMPTY(&keg->uk_part_slab)) { 2192 slab = LIST_FIRST(&keg->uk_part_slab); 2193 } else { 2194 slab = LIST_FIRST(&keg->uk_free_slab); 2195 LIST_REMOVE(slab, us_link); 2196 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, 2197 us_link); 2198 } 2199 MPASS(slab->us_keg == keg); 2200 return (slab); 2201 } 2202 2203 /* 2204 * M_NOVM means don't ask at all! 2205 */ 2206 if (flags & M_NOVM) 2207 break; 2208 2209 if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) { 2210 keg->uk_flags |= UMA_ZFLAG_FULL; 2211 /* 2212 * If this is not a multi-zone, set the FULL bit. 2213 * Otherwise slab_multi() takes care of it. 2214 */ 2215 if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) { 2216 zone->uz_flags |= UMA_ZFLAG_FULL; 2217 zone_log_warning(zone); 2218 } 2219 if (flags & M_NOWAIT) 2220 break; 2221 zone->uz_sleeps++; 2222 msleep(keg, &keg->uk_lock, PVM, "keglimit", 0); 2223 continue; 2224 } 2225 slab = keg_alloc_slab(keg, zone, flags); 2226 /* 2227 * If we got a slab here it's safe to mark it partially used 2228 * and return. We assume that the caller is going to remove 2229 * at least one item. 2230 */ 2231 if (slab) { 2232 MPASS(slab->us_keg == keg); 2233 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link); 2234 return (slab); 2235 } 2236 /* 2237 * We might not have been able to get a slab but another cpu 2238 * could have while we were unlocked. Check again before we 2239 * fail. 2240 */ 2241 flags |= M_NOVM; 2242 } 2243 return (slab); 2244 } 2245 2246 static uma_slab_t 2247 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags) 2248 { 2249 uma_slab_t slab; 2250 2251 if (keg == NULL) { 2252 keg = zone_first_keg(zone); 2253 KEG_LOCK(keg); 2254 } 2255 2256 for (;;) { 2257 slab = keg_fetch_slab(keg, zone, flags); 2258 if (slab) 2259 return (slab); 2260 if (flags & (M_NOWAIT | M_NOVM)) 2261 break; 2262 } 2263 KEG_UNLOCK(keg); 2264 return (NULL); 2265 } 2266 2267 /* 2268 * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns 2269 * with the keg locked. On NULL no lock is held. 2270 * 2271 * The last pointer is used to seed the search. It is not required. 2272 */ 2273 static uma_slab_t 2274 zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags) 2275 { 2276 uma_klink_t klink; 2277 uma_slab_t slab; 2278 uma_keg_t keg; 2279 int flags; 2280 int empty; 2281 int full; 2282 2283 /* 2284 * Don't wait on the first pass. This will skip limit tests 2285 * as well. We don't want to block if we can find a provider 2286 * without blocking. 2287 */ 2288 flags = (rflags & ~M_WAITOK) | M_NOWAIT; 2289 /* 2290 * Use the last slab allocated as a hint for where to start 2291 * the search. 2292 */ 2293 if (last != NULL) { 2294 slab = keg_fetch_slab(last, zone, flags); 2295 if (slab) 2296 return (slab); 2297 KEG_UNLOCK(last); 2298 } 2299 /* 2300 * Loop until we have a slab incase of transient failures 2301 * while M_WAITOK is specified. I'm not sure this is 100% 2302 * required but we've done it for so long now. 2303 */ 2304 for (;;) { 2305 empty = 0; 2306 full = 0; 2307 /* 2308 * Search the available kegs for slabs. Be careful to hold the 2309 * correct lock while calling into the keg layer. 2310 */ 2311 LIST_FOREACH(klink, &zone->uz_kegs, kl_link) { 2312 keg = klink->kl_keg; 2313 KEG_LOCK(keg); 2314 if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) { 2315 slab = keg_fetch_slab(keg, zone, flags); 2316 if (slab) 2317 return (slab); 2318 } 2319 if (keg->uk_flags & UMA_ZFLAG_FULL) 2320 full++; 2321 else 2322 empty++; 2323 KEG_UNLOCK(keg); 2324 } 2325 if (rflags & (M_NOWAIT | M_NOVM)) 2326 break; 2327 flags = rflags; 2328 /* 2329 * All kegs are full. XXX We can't atomically check all kegs 2330 * and sleep so just sleep for a short period and retry. 2331 */ 2332 if (full && !empty) { 2333 ZONE_LOCK(zone); 2334 zone->uz_flags |= UMA_ZFLAG_FULL; 2335 zone->uz_sleeps++; 2336 zone_log_warning(zone); 2337 msleep(zone, zone->uz_lockptr, PVM, 2338 "zonelimit", hz/100); 2339 zone->uz_flags &= ~UMA_ZFLAG_FULL; 2340 ZONE_UNLOCK(zone); 2341 continue; 2342 } 2343 } 2344 return (NULL); 2345 } 2346 2347 static void * 2348 slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 2349 { 2350 void *item; 2351 uint8_t freei; 2352 2353 MPASS(keg == slab->us_keg); 2354 mtx_assert(&keg->uk_lock, MA_OWNED); 2355 2356 freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1; 2357 BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free); 2358 item = slab->us_data + (keg->uk_rsize * freei); 2359 slab->us_freecount--; 2360 keg->uk_free--; 2361 2362 /* Move this slab to the full list */ 2363 if (slab->us_freecount == 0) { 2364 LIST_REMOVE(slab, us_link); 2365 LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link); 2366 } 2367 2368 return (item); 2369 } 2370 2371 static int 2372 zone_import(uma_zone_t zone, void **bucket, int max, int flags) 2373 { 2374 uma_slab_t slab; 2375 uma_keg_t keg; 2376 int i; 2377 2378 slab = NULL; 2379 keg = NULL; 2380 /* Try to keep the buckets totally full */ 2381 for (i = 0; i < max; ) { 2382 if ((slab = zone->uz_slab(zone, keg, flags)) == NULL) 2383 break; 2384 keg = slab->us_keg; 2385 while (slab->us_freecount && i < max) 2386 bucket[i++] = slab_alloc_item(keg, slab); 2387 2388 /* Don't block on the next fill */ 2389 flags &= ~M_WAITOK; 2390 flags |= M_NOWAIT; 2391 } 2392 if (slab != NULL) 2393 KEG_UNLOCK(keg); 2394 2395 return i; 2396 } 2397 2398 static uma_bucket_t 2399 zone_alloc_bucket(uma_zone_t zone, int flags) 2400 { 2401 uma_bucket_t bucket; 2402 int max; 2403 2404 bucket = bucket_alloc(zone, M_NOWAIT | (flags & M_NOVM)); 2405 if (bucket == NULL) 2406 goto out; 2407 2408 max = MIN(bucket->ub_entries, zone->uz_count); 2409 bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 2410 max, flags); 2411 2412 /* 2413 * Initialize the memory if necessary. 2414 */ 2415 if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 2416 int i; 2417 2418 for (i = 0; i < bucket->ub_cnt; i++) 2419 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 2420 flags) != 0) 2421 break; 2422 /* 2423 * If we couldn't initialize the whole bucket, put the 2424 * rest back onto the freelist. 2425 */ 2426 if (i != bucket->ub_cnt) { 2427 zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 2428 bucket->ub_cnt - i); 2429 #ifdef INVARIANTS 2430 bzero(&bucket->ub_bucket[i], 2431 sizeof(void *) * (bucket->ub_cnt - i)); 2432 #endif 2433 bucket->ub_cnt = i; 2434 } 2435 } 2436 2437 out: 2438 if (bucket == NULL || bucket->ub_cnt == 0) { 2439 if (bucket != NULL) 2440 bucket_free(zone, bucket); 2441 atomic_add_long(&zone->uz_fails, 1); 2442 return (NULL); 2443 } 2444 2445 return (bucket); 2446 } 2447 2448 /* 2449 * Allocates a single item from a zone. 2450 * 2451 * Arguments 2452 * zone The zone to alloc for. 2453 * udata The data to be passed to the constructor. 2454 * flags M_WAITOK, M_NOWAIT, M_ZERO. 2455 * 2456 * Returns 2457 * NULL if there is no memory and M_NOWAIT is set 2458 * An item if successful 2459 */ 2460 2461 static void * 2462 zone_alloc_item(uma_zone_t zone, void *udata, int flags) 2463 { 2464 void *item; 2465 2466 item = NULL; 2467 2468 #ifdef UMA_DEBUG_ALLOC 2469 printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone); 2470 #endif 2471 if (zone->uz_import(zone->uz_arg, &item, 1, flags) != 1) 2472 goto fail; 2473 atomic_add_long(&zone->uz_allocs, 1); 2474 2475 /* 2476 * We have to call both the zone's init (not the keg's init) 2477 * and the zone's ctor. This is because the item is going from 2478 * a keg slab directly to the user, and the user is expecting it 2479 * to be both zone-init'd as well as zone-ctor'd. 2480 */ 2481 if (zone->uz_init != NULL) { 2482 if (zone->uz_init(item, zone->uz_size, flags) != 0) { 2483 zone_free_item(zone, item, udata, SKIP_FINI); 2484 goto fail; 2485 } 2486 } 2487 if (zone->uz_ctor != NULL) { 2488 if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) { 2489 zone_free_item(zone, item, udata, SKIP_DTOR); 2490 goto fail; 2491 } 2492 } 2493 #ifdef INVARIANTS 2494 uma_dbg_alloc(zone, NULL, item); 2495 #endif 2496 if (flags & M_ZERO) 2497 bzero(item, zone->uz_size); 2498 2499 return (item); 2500 2501 fail: 2502 atomic_add_long(&zone->uz_fails, 1); 2503 return (NULL); 2504 } 2505 2506 /* See uma.h */ 2507 void 2508 uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 2509 { 2510 uma_cache_t cache; 2511 uma_bucket_t bucket; 2512 int cpu; 2513 2514 #ifdef UMA_DEBUG_ALLOC_1 2515 printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone); 2516 #endif 2517 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread, 2518 zone->uz_name); 2519 2520 /* uma_zfree(..., NULL) does nothing, to match free(9). */ 2521 if (item == NULL) 2522 return; 2523 #ifdef DEBUG_MEMGUARD 2524 if (is_memguard_addr(item)) { 2525 if (zone->uz_dtor != NULL && zone->uz_dtor != mtrash_dtor) 2526 zone->uz_dtor(item, zone->uz_size, udata); 2527 if (zone->uz_fini != NULL && zone->uz_fini != mtrash_fini) 2528 zone->uz_fini(item, zone->uz_size); 2529 memguard_free(item); 2530 return; 2531 } 2532 #endif 2533 #ifdef INVARIANTS 2534 if (zone->uz_flags & UMA_ZONE_MALLOC) 2535 uma_dbg_free(zone, udata, item); 2536 else 2537 uma_dbg_free(zone, NULL, item); 2538 #endif 2539 if (zone->uz_dtor != NULL) 2540 zone->uz_dtor(item, zone->uz_size, udata); 2541 2542 /* 2543 * The race here is acceptable. If we miss it we'll just have to wait 2544 * a little longer for the limits to be reset. 2545 */ 2546 if (zone->uz_flags & UMA_ZFLAG_FULL) 2547 goto zfree_item; 2548 2549 /* 2550 * If possible, free to the per-CPU cache. There are two 2551 * requirements for safe access to the per-CPU cache: (1) the thread 2552 * accessing the cache must not be preempted or yield during access, 2553 * and (2) the thread must not migrate CPUs without switching which 2554 * cache it accesses. We rely on a critical section to prevent 2555 * preemption and migration. We release the critical section in 2556 * order to acquire the zone mutex if we are unable to free to the 2557 * current cache; when we re-acquire the critical section, we must 2558 * detect and handle migration if it has occurred. 2559 */ 2560 zfree_restart: 2561 critical_enter(); 2562 cpu = curcpu; 2563 cache = &zone->uz_cpu[cpu]; 2564 2565 zfree_start: 2566 /* 2567 * Try to free into the allocbucket first to give LIFO ordering 2568 * for cache-hot datastructures. Spill over into the freebucket 2569 * if necessary. Alloc will swap them if one runs dry. 2570 */ 2571 bucket = cache->uc_allocbucket; 2572 if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries) 2573 bucket = cache->uc_freebucket; 2574 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) { 2575 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL, 2576 ("uma_zfree: Freeing to non free bucket index.")); 2577 bucket->ub_bucket[bucket->ub_cnt] = item; 2578 bucket->ub_cnt++; 2579 cache->uc_frees++; 2580 critical_exit(); 2581 return; 2582 } 2583 2584 /* 2585 * We must go back the zone, which requires acquiring the zone lock, 2586 * which in turn means we must release and re-acquire the critical 2587 * section. Since the critical section is released, we may be 2588 * preempted or migrate. As such, make sure not to maintain any 2589 * thread-local state specific to the cache from prior to releasing 2590 * the critical section. 2591 */ 2592 critical_exit(); 2593 if (zone->uz_count == 0 || bucketdisable) 2594 goto zfree_item; 2595 2596 ZONE_LOCK(zone); 2597 critical_enter(); 2598 cpu = curcpu; 2599 cache = &zone->uz_cpu[cpu]; 2600 2601 /* 2602 * Since we have locked the zone we may as well send back our stats. 2603 */ 2604 atomic_add_long(&zone->uz_allocs, cache->uc_allocs); 2605 atomic_add_long(&zone->uz_frees, cache->uc_frees); 2606 cache->uc_allocs = 0; 2607 cache->uc_frees = 0; 2608 2609 bucket = cache->uc_freebucket; 2610 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) { 2611 ZONE_UNLOCK(zone); 2612 goto zfree_start; 2613 } 2614 cache->uc_freebucket = NULL; 2615 2616 /* Can we throw this on the zone full list? */ 2617 if (bucket != NULL) { 2618 #ifdef UMA_DEBUG_ALLOC 2619 printf("uma_zfree: Putting old bucket on the free list.\n"); 2620 #endif 2621 /* ub_cnt is pointing to the last free item */ 2622 KASSERT(bucket->ub_cnt != 0, 2623 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n")); 2624 LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link); 2625 } 2626 2627 /* We are no longer associated with this CPU. */ 2628 critical_exit(); 2629 2630 /* And the zone.. */ 2631 ZONE_UNLOCK(zone); 2632 2633 #ifdef UMA_DEBUG_ALLOC 2634 printf("uma_zfree: Allocating new free bucket.\n"); 2635 #endif 2636 bucket = bucket_alloc(zone, M_NOWAIT); 2637 if (bucket) { 2638 critical_enter(); 2639 cpu = curcpu; 2640 cache = &zone->uz_cpu[cpu]; 2641 if (cache->uc_freebucket == NULL) { 2642 cache->uc_freebucket = bucket; 2643 goto zfree_start; 2644 } 2645 /* 2646 * We lost the race, start over. We have to drop our 2647 * critical section to free the bucket. 2648 */ 2649 critical_exit(); 2650 bucket_free(zone, bucket); 2651 goto zfree_restart; 2652 } 2653 2654 /* 2655 * If nothing else caught this, we'll just do an internal free. 2656 */ 2657 zfree_item: 2658 zone_free_item(zone, item, udata, SKIP_DTOR); 2659 2660 return; 2661 } 2662 2663 static void 2664 slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item) 2665 { 2666 uint8_t freei; 2667 2668 mtx_assert(&keg->uk_lock, MA_OWNED); 2669 MPASS(keg == slab->us_keg); 2670 2671 /* Do we need to remove from any lists? */ 2672 if (slab->us_freecount+1 == keg->uk_ipers) { 2673 LIST_REMOVE(slab, us_link); 2674 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link); 2675 } else if (slab->us_freecount == 0) { 2676 LIST_REMOVE(slab, us_link); 2677 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link); 2678 } 2679 2680 /* Slab management. */ 2681 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize; 2682 BIT_SET(SLAB_SETSIZE, freei, &slab->us_free); 2683 slab->us_freecount++; 2684 2685 /* Keg statistics. */ 2686 keg->uk_free++; 2687 } 2688 2689 static void 2690 zone_release(uma_zone_t zone, void **bucket, int cnt) 2691 { 2692 void *item; 2693 uma_slab_t slab; 2694 uma_keg_t keg; 2695 uint8_t *mem; 2696 int clearfull; 2697 int i; 2698 2699 clearfull = 0; 2700 keg = zone_first_keg(zone); 2701 KEG_LOCK(keg); 2702 for (i = 0; i < cnt; i++) { 2703 item = bucket[i]; 2704 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) { 2705 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 2706 if (zone->uz_flags & UMA_ZONE_HASH) { 2707 slab = hash_sfind(&keg->uk_hash, mem); 2708 } else { 2709 mem += keg->uk_pgoff; 2710 slab = (uma_slab_t)mem; 2711 } 2712 } else { 2713 slab = vtoslab((vm_offset_t)item); 2714 if (slab->us_keg != keg) { 2715 KEG_UNLOCK(keg); 2716 keg = slab->us_keg; 2717 KEG_LOCK(keg); 2718 } 2719 } 2720 slab_free_item(keg, slab, item); 2721 if (keg->uk_flags & UMA_ZFLAG_FULL) { 2722 if (keg->uk_pages < keg->uk_maxpages) { 2723 keg->uk_flags &= ~UMA_ZFLAG_FULL; 2724 clearfull = 1; 2725 } 2726 2727 /* 2728 * We can handle one more allocation. Since we're 2729 * clearing ZFLAG_FULL, wake up all procs blocked 2730 * on pages. This should be uncommon, so keeping this 2731 * simple for now (rather than adding count of blocked 2732 * threads etc). 2733 */ 2734 wakeup(keg); 2735 } 2736 } 2737 KEG_UNLOCK(keg); 2738 if (clearfull) { 2739 ZONE_LOCK(zone); 2740 zone->uz_flags &= ~UMA_ZFLAG_FULL; 2741 wakeup(zone); 2742 ZONE_UNLOCK(zone); 2743 } 2744 2745 } 2746 2747 /* 2748 * Frees a single item to any zone. 2749 * 2750 * Arguments: 2751 * zone The zone to free to 2752 * item The item we're freeing 2753 * udata User supplied data for the dtor 2754 * skip Skip dtors and finis 2755 */ 2756 static void 2757 zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 2758 { 2759 2760 #ifdef INVARIANTS 2761 if (skip == SKIP_NONE) { 2762 if (zone->uz_flags & UMA_ZONE_MALLOC) 2763 uma_dbg_free(zone, udata, item); 2764 else 2765 uma_dbg_free(zone, NULL, item); 2766 } 2767 #endif 2768 if (skip < SKIP_DTOR && zone->uz_dtor) 2769 zone->uz_dtor(item, zone->uz_size, udata); 2770 2771 if (skip < SKIP_FINI && zone->uz_fini) 2772 zone->uz_fini(item, zone->uz_size); 2773 2774 atomic_add_long(&zone->uz_frees, 1); 2775 zone->uz_release(zone->uz_arg, &item, 1); 2776 } 2777 2778 /* See uma.h */ 2779 int 2780 uma_zone_set_max(uma_zone_t zone, int nitems) 2781 { 2782 uma_keg_t keg; 2783 2784 keg = zone_first_keg(zone); 2785 if (keg == NULL) 2786 return (0); 2787 KEG_LOCK(keg); 2788 keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera; 2789 if (keg->uk_maxpages * keg->uk_ipers < nitems) 2790 keg->uk_maxpages += keg->uk_ppera; 2791 nitems = keg->uk_maxpages * keg->uk_ipers; 2792 KEG_UNLOCK(keg); 2793 2794 return (nitems); 2795 } 2796 2797 /* See uma.h */ 2798 int 2799 uma_zone_get_max(uma_zone_t zone) 2800 { 2801 int nitems; 2802 uma_keg_t keg; 2803 2804 keg = zone_first_keg(zone); 2805 if (keg == NULL) 2806 return (0); 2807 KEG_LOCK(keg); 2808 nitems = keg->uk_maxpages * keg->uk_ipers; 2809 KEG_UNLOCK(keg); 2810 2811 return (nitems); 2812 } 2813 2814 /* See uma.h */ 2815 void 2816 uma_zone_set_warning(uma_zone_t zone, const char *warning) 2817 { 2818 2819 ZONE_LOCK(zone); 2820 zone->uz_warning = warning; 2821 ZONE_UNLOCK(zone); 2822 } 2823 2824 /* See uma.h */ 2825 int 2826 uma_zone_get_cur(uma_zone_t zone) 2827 { 2828 int64_t nitems; 2829 u_int i; 2830 2831 ZONE_LOCK(zone); 2832 nitems = zone->uz_allocs - zone->uz_frees; 2833 CPU_FOREACH(i) { 2834 /* 2835 * See the comment in sysctl_vm_zone_stats() regarding the 2836 * safety of accessing the per-cpu caches. With the zone lock 2837 * held, it is safe, but can potentially result in stale data. 2838 */ 2839 nitems += zone->uz_cpu[i].uc_allocs - 2840 zone->uz_cpu[i].uc_frees; 2841 } 2842 ZONE_UNLOCK(zone); 2843 2844 return (nitems < 0 ? 0 : nitems); 2845 } 2846 2847 /* See uma.h */ 2848 void 2849 uma_zone_set_init(uma_zone_t zone, uma_init uminit) 2850 { 2851 uma_keg_t keg; 2852 2853 keg = zone_first_keg(zone); 2854 KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type")); 2855 KEG_LOCK(keg); 2856 KASSERT(keg->uk_pages == 0, 2857 ("uma_zone_set_init on non-empty keg")); 2858 keg->uk_init = uminit; 2859 KEG_UNLOCK(keg); 2860 } 2861 2862 /* See uma.h */ 2863 void 2864 uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 2865 { 2866 uma_keg_t keg; 2867 2868 keg = zone_first_keg(zone); 2869 KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type")); 2870 KEG_LOCK(keg); 2871 KASSERT(keg->uk_pages == 0, 2872 ("uma_zone_set_fini on non-empty keg")); 2873 keg->uk_fini = fini; 2874 KEG_UNLOCK(keg); 2875 } 2876 2877 /* See uma.h */ 2878 void 2879 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 2880 { 2881 2882 ZONE_LOCK(zone); 2883 KASSERT(zone_first_keg(zone)->uk_pages == 0, 2884 ("uma_zone_set_zinit on non-empty keg")); 2885 zone->uz_init = zinit; 2886 ZONE_UNLOCK(zone); 2887 } 2888 2889 /* See uma.h */ 2890 void 2891 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 2892 { 2893 2894 ZONE_LOCK(zone); 2895 KASSERT(zone_first_keg(zone)->uk_pages == 0, 2896 ("uma_zone_set_zfini on non-empty keg")); 2897 zone->uz_fini = zfini; 2898 ZONE_UNLOCK(zone); 2899 } 2900 2901 /* See uma.h */ 2902 /* XXX uk_freef is not actually used with the zone locked */ 2903 void 2904 uma_zone_set_freef(uma_zone_t zone, uma_free freef) 2905 { 2906 uma_keg_t keg; 2907 2908 keg = zone_first_keg(zone); 2909 KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type")); 2910 KEG_LOCK(keg); 2911 keg->uk_freef = freef; 2912 KEG_UNLOCK(keg); 2913 } 2914 2915 /* See uma.h */ 2916 /* XXX uk_allocf is not actually used with the zone locked */ 2917 void 2918 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 2919 { 2920 uma_keg_t keg; 2921 2922 keg = zone_first_keg(zone); 2923 KEG_LOCK(keg); 2924 keg->uk_flags |= UMA_ZFLAG_PRIVALLOC; 2925 keg->uk_allocf = allocf; 2926 KEG_UNLOCK(keg); 2927 } 2928 2929 /* See uma.h */ 2930 int 2931 uma_zone_reserve_kva(uma_zone_t zone, int count) 2932 { 2933 uma_keg_t keg; 2934 vm_offset_t kva; 2935 int pages; 2936 2937 keg = zone_first_keg(zone); 2938 if (keg == NULL) 2939 return (0); 2940 pages = count / keg->uk_ipers; 2941 2942 if (pages * keg->uk_ipers < count) 2943 pages++; 2944 2945 #ifdef UMA_MD_SMALL_ALLOC 2946 if (keg->uk_ppera > 1) { 2947 #else 2948 if (1) { 2949 #endif 2950 kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE); 2951 if (kva == 0) 2952 return (0); 2953 } else 2954 kva = 0; 2955 KEG_LOCK(keg); 2956 keg->uk_kva = kva; 2957 keg->uk_offset = 0; 2958 keg->uk_maxpages = pages; 2959 #ifdef UMA_MD_SMALL_ALLOC 2960 keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 2961 #else 2962 keg->uk_allocf = noobj_alloc; 2963 #endif 2964 keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC; 2965 KEG_UNLOCK(keg); 2966 2967 return (1); 2968 } 2969 2970 /* See uma.h */ 2971 void 2972 uma_prealloc(uma_zone_t zone, int items) 2973 { 2974 int slabs; 2975 uma_slab_t slab; 2976 uma_keg_t keg; 2977 2978 keg = zone_first_keg(zone); 2979 if (keg == NULL) 2980 return; 2981 KEG_LOCK(keg); 2982 slabs = items / keg->uk_ipers; 2983 if (slabs * keg->uk_ipers < items) 2984 slabs++; 2985 while (slabs > 0) { 2986 slab = keg_alloc_slab(keg, zone, M_WAITOK); 2987 if (slab == NULL) 2988 break; 2989 MPASS(slab->us_keg == keg); 2990 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link); 2991 slabs--; 2992 } 2993 KEG_UNLOCK(keg); 2994 } 2995 2996 /* See uma.h */ 2997 uint32_t * 2998 uma_find_refcnt(uma_zone_t zone, void *item) 2999 { 3000 uma_slabrefcnt_t slabref; 3001 uma_slab_t slab; 3002 uma_keg_t keg; 3003 uint32_t *refcnt; 3004 int idx; 3005 3006 slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK)); 3007 slabref = (uma_slabrefcnt_t)slab; 3008 keg = slab->us_keg; 3009 KASSERT(keg->uk_flags & UMA_ZONE_REFCNT, 3010 ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT")); 3011 idx = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize; 3012 refcnt = &slabref->us_refcnt[idx]; 3013 return refcnt; 3014 } 3015 3016 /* See uma.h */ 3017 void 3018 uma_reclaim(void) 3019 { 3020 #ifdef UMA_DEBUG 3021 printf("UMA: vm asked us to release pages!\n"); 3022 #endif 3023 bucket_enable(); 3024 zone_foreach(zone_drain); 3025 /* 3026 * Some slabs may have been freed but this zone will be visited early 3027 * we visit again so that we can free pages that are empty once other 3028 * zones are drained. We have to do the same for buckets. 3029 */ 3030 zone_drain(slabzone); 3031 zone_drain(slabrefzone); 3032 bucket_zone_drain(); 3033 } 3034 3035 /* See uma.h */ 3036 int 3037 uma_zone_exhausted(uma_zone_t zone) 3038 { 3039 int full; 3040 3041 ZONE_LOCK(zone); 3042 full = (zone->uz_flags & UMA_ZFLAG_FULL); 3043 ZONE_UNLOCK(zone); 3044 return (full); 3045 } 3046 3047 int 3048 uma_zone_exhausted_nolock(uma_zone_t zone) 3049 { 3050 return (zone->uz_flags & UMA_ZFLAG_FULL); 3051 } 3052 3053 void * 3054 uma_large_malloc(int size, int wait) 3055 { 3056 void *mem; 3057 uma_slab_t slab; 3058 uint8_t flags; 3059 3060 slab = zone_alloc_item(slabzone, NULL, wait); 3061 if (slab == NULL) 3062 return (NULL); 3063 mem = page_alloc(NULL, size, &flags, wait); 3064 if (mem) { 3065 vsetslab((vm_offset_t)mem, slab); 3066 slab->us_data = mem; 3067 slab->us_flags = flags | UMA_SLAB_MALLOC; 3068 slab->us_size = size; 3069 } else { 3070 zone_free_item(slabzone, slab, NULL, SKIP_NONE); 3071 } 3072 3073 return (mem); 3074 } 3075 3076 void 3077 uma_large_free(uma_slab_t slab) 3078 { 3079 vsetobj((vm_offset_t)slab->us_data, kmem_object); 3080 page_free(slab->us_data, slab->us_size, slab->us_flags); 3081 zone_free_item(slabzone, slab, NULL, SKIP_NONE); 3082 } 3083 3084 void 3085 uma_print_stats(void) 3086 { 3087 zone_foreach(uma_print_zone); 3088 } 3089 3090 static void 3091 slab_print(uma_slab_t slab) 3092 { 3093 printf("slab: keg %p, data %p, freecount %d\n", 3094 slab->us_keg, slab->us_data, slab->us_freecount); 3095 } 3096 3097 static void 3098 cache_print(uma_cache_t cache) 3099 { 3100 printf("alloc: %p(%d), free: %p(%d)\n", 3101 cache->uc_allocbucket, 3102 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0, 3103 cache->uc_freebucket, 3104 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0); 3105 } 3106 3107 static void 3108 uma_print_keg(uma_keg_t keg) 3109 { 3110 uma_slab_t slab; 3111 3112 printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d " 3113 "out %d free %d limit %d\n", 3114 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags, 3115 keg->uk_ipers, keg->uk_ppera, 3116 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free, 3117 (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers); 3118 printf("Part slabs:\n"); 3119 LIST_FOREACH(slab, &keg->uk_part_slab, us_link) 3120 slab_print(slab); 3121 printf("Free slabs:\n"); 3122 LIST_FOREACH(slab, &keg->uk_free_slab, us_link) 3123 slab_print(slab); 3124 printf("Full slabs:\n"); 3125 LIST_FOREACH(slab, &keg->uk_full_slab, us_link) 3126 slab_print(slab); 3127 } 3128 3129 void 3130 uma_print_zone(uma_zone_t zone) 3131 { 3132 uma_cache_t cache; 3133 uma_klink_t kl; 3134 int i; 3135 3136 printf("zone: %s(%p) size %d flags %#x\n", 3137 zone->uz_name, zone, zone->uz_size, zone->uz_flags); 3138 LIST_FOREACH(kl, &zone->uz_kegs, kl_link) 3139 uma_print_keg(kl->kl_keg); 3140 CPU_FOREACH(i) { 3141 cache = &zone->uz_cpu[i]; 3142 printf("CPU %d Cache:\n", i); 3143 cache_print(cache); 3144 } 3145 } 3146 3147 #ifdef DDB 3148 /* 3149 * Generate statistics across both the zone and its per-cpu cache's. Return 3150 * desired statistics if the pointer is non-NULL for that statistic. 3151 * 3152 * Note: does not update the zone statistics, as it can't safely clear the 3153 * per-CPU cache statistic. 3154 * 3155 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't 3156 * safe from off-CPU; we should modify the caches to track this information 3157 * directly so that we don't have to. 3158 */ 3159 static void 3160 uma_zone_sumstat(uma_zone_t z, int *cachefreep, uint64_t *allocsp, 3161 uint64_t *freesp, uint64_t *sleepsp) 3162 { 3163 uma_cache_t cache; 3164 uint64_t allocs, frees, sleeps; 3165 int cachefree, cpu; 3166 3167 allocs = frees = sleeps = 0; 3168 cachefree = 0; 3169 CPU_FOREACH(cpu) { 3170 cache = &z->uz_cpu[cpu]; 3171 if (cache->uc_allocbucket != NULL) 3172 cachefree += cache->uc_allocbucket->ub_cnt; 3173 if (cache->uc_freebucket != NULL) 3174 cachefree += cache->uc_freebucket->ub_cnt; 3175 allocs += cache->uc_allocs; 3176 frees += cache->uc_frees; 3177 } 3178 allocs += z->uz_allocs; 3179 frees += z->uz_frees; 3180 sleeps += z->uz_sleeps; 3181 if (cachefreep != NULL) 3182 *cachefreep = cachefree; 3183 if (allocsp != NULL) 3184 *allocsp = allocs; 3185 if (freesp != NULL) 3186 *freesp = frees; 3187 if (sleepsp != NULL) 3188 *sleepsp = sleeps; 3189 } 3190 #endif /* DDB */ 3191 3192 static int 3193 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 3194 { 3195 uma_keg_t kz; 3196 uma_zone_t z; 3197 int count; 3198 3199 count = 0; 3200 mtx_lock(&uma_mtx); 3201 LIST_FOREACH(kz, &uma_kegs, uk_link) { 3202 LIST_FOREACH(z, &kz->uk_zones, uz_link) 3203 count++; 3204 } 3205 mtx_unlock(&uma_mtx); 3206 return (sysctl_handle_int(oidp, &count, 0, req)); 3207 } 3208 3209 static int 3210 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 3211 { 3212 struct uma_stream_header ush; 3213 struct uma_type_header uth; 3214 struct uma_percpu_stat ups; 3215 uma_bucket_t bucket; 3216 struct sbuf sbuf; 3217 uma_cache_t cache; 3218 uma_klink_t kl; 3219 uma_keg_t kz; 3220 uma_zone_t z; 3221 uma_keg_t k; 3222 int count, error, i; 3223 3224 error = sysctl_wire_old_buffer(req, 0); 3225 if (error != 0) 3226 return (error); 3227 sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 3228 3229 count = 0; 3230 mtx_lock(&uma_mtx); 3231 LIST_FOREACH(kz, &uma_kegs, uk_link) { 3232 LIST_FOREACH(z, &kz->uk_zones, uz_link) 3233 count++; 3234 } 3235 3236 /* 3237 * Insert stream header. 3238 */ 3239 bzero(&ush, sizeof(ush)); 3240 ush.ush_version = UMA_STREAM_VERSION; 3241 ush.ush_maxcpus = (mp_maxid + 1); 3242 ush.ush_count = count; 3243 (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 3244 3245 LIST_FOREACH(kz, &uma_kegs, uk_link) { 3246 LIST_FOREACH(z, &kz->uk_zones, uz_link) { 3247 bzero(&uth, sizeof(uth)); 3248 ZONE_LOCK(z); 3249 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 3250 uth.uth_align = kz->uk_align; 3251 uth.uth_size = kz->uk_size; 3252 uth.uth_rsize = kz->uk_rsize; 3253 LIST_FOREACH(kl, &z->uz_kegs, kl_link) { 3254 k = kl->kl_keg; 3255 uth.uth_maxpages += k->uk_maxpages; 3256 uth.uth_pages += k->uk_pages; 3257 uth.uth_keg_free += k->uk_free; 3258 uth.uth_limit = (k->uk_maxpages / k->uk_ppera) 3259 * k->uk_ipers; 3260 } 3261 3262 /* 3263 * A zone is secondary is it is not the first entry 3264 * on the keg's zone list. 3265 */ 3266 if ((z->uz_flags & UMA_ZONE_SECONDARY) && 3267 (LIST_FIRST(&kz->uk_zones) != z)) 3268 uth.uth_zone_flags = UTH_ZONE_SECONDARY; 3269 3270 LIST_FOREACH(bucket, &z->uz_buckets, ub_link) 3271 uth.uth_zone_free += bucket->ub_cnt; 3272 uth.uth_allocs = z->uz_allocs; 3273 uth.uth_frees = z->uz_frees; 3274 uth.uth_fails = z->uz_fails; 3275 uth.uth_sleeps = z->uz_sleeps; 3276 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 3277 /* 3278 * While it is not normally safe to access the cache 3279 * bucket pointers while not on the CPU that owns the 3280 * cache, we only allow the pointers to be exchanged 3281 * without the zone lock held, not invalidated, so 3282 * accept the possible race associated with bucket 3283 * exchange during monitoring. 3284 */ 3285 for (i = 0; i < (mp_maxid + 1); i++) { 3286 bzero(&ups, sizeof(ups)); 3287 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) 3288 goto skip; 3289 if (CPU_ABSENT(i)) 3290 goto skip; 3291 cache = &z->uz_cpu[i]; 3292 if (cache->uc_allocbucket != NULL) 3293 ups.ups_cache_free += 3294 cache->uc_allocbucket->ub_cnt; 3295 if (cache->uc_freebucket != NULL) 3296 ups.ups_cache_free += 3297 cache->uc_freebucket->ub_cnt; 3298 ups.ups_allocs = cache->uc_allocs; 3299 ups.ups_frees = cache->uc_frees; 3300 skip: 3301 (void)sbuf_bcat(&sbuf, &ups, sizeof(ups)); 3302 } 3303 ZONE_UNLOCK(z); 3304 } 3305 } 3306 mtx_unlock(&uma_mtx); 3307 error = sbuf_finish(&sbuf); 3308 sbuf_delete(&sbuf); 3309 return (error); 3310 } 3311 3312 #ifdef DDB 3313 DB_SHOW_COMMAND(uma, db_show_uma) 3314 { 3315 uint64_t allocs, frees, sleeps; 3316 uma_bucket_t bucket; 3317 uma_keg_t kz; 3318 uma_zone_t z; 3319 int cachefree; 3320 3321 db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 3322 "Requests", "Sleeps"); 3323 LIST_FOREACH(kz, &uma_kegs, uk_link) { 3324 LIST_FOREACH(z, &kz->uk_zones, uz_link) { 3325 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 3326 allocs = z->uz_allocs; 3327 frees = z->uz_frees; 3328 sleeps = z->uz_sleeps; 3329 cachefree = 0; 3330 } else 3331 uma_zone_sumstat(z, &cachefree, &allocs, 3332 &frees, &sleeps); 3333 if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 3334 (LIST_FIRST(&kz->uk_zones) != z))) 3335 cachefree += kz->uk_free; 3336 LIST_FOREACH(bucket, &z->uz_buckets, ub_link) 3337 cachefree += bucket->ub_cnt; 3338 db_printf("%18s %8ju %8jd %8d %12ju %8ju\n", z->uz_name, 3339 (uintmax_t)kz->uk_size, 3340 (intmax_t)(allocs - frees), cachefree, 3341 (uintmax_t)allocs, sleeps); 3342 if (db_pager_quit) 3343 return; 3344 } 3345 } 3346 } 3347 #endif 3348