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