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