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