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