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