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