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