160727d8bSWarner Losh /*- 2ef72505eSJeff Roberson * Copyright (c) 2002-2005, 2009, 2013 Jeffrey Roberson <jeff@FreeBSD.org> 308ecce74SRobert Watson * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org> 4ae4e9636SRobert Watson * Copyright (c) 2004-2006 Robert N. M. Watson 508ecce74SRobert Watson * All rights reserved. 68355f576SJeff Roberson * 78355f576SJeff Roberson * Redistribution and use in source and binary forms, with or without 88355f576SJeff Roberson * modification, are permitted provided that the following conditions 98355f576SJeff Roberson * are met: 108355f576SJeff Roberson * 1. Redistributions of source code must retain the above copyright 118355f576SJeff Roberson * notice unmodified, this list of conditions, and the following 128355f576SJeff Roberson * disclaimer. 138355f576SJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 148355f576SJeff Roberson * notice, this list of conditions and the following disclaimer in the 158355f576SJeff Roberson * documentation and/or other materials provided with the distribution. 168355f576SJeff Roberson * 178355f576SJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 188355f576SJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 198355f576SJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 208355f576SJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 218355f576SJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 228355f576SJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 238355f576SJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 248355f576SJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 258355f576SJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 268355f576SJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 278355f576SJeff Roberson */ 288355f576SJeff Roberson 298355f576SJeff Roberson /* 308355f576SJeff Roberson * uma_core.c Implementation of the Universal Memory allocator 318355f576SJeff Roberson * 328355f576SJeff Roberson * This allocator is intended to replace the multitude of similar object caches 338355f576SJeff Roberson * in the standard FreeBSD kernel. The intent is to be flexible as well as 348355f576SJeff Roberson * effecient. A primary design goal is to return unused memory to the rest of 358355f576SJeff Roberson * the system. This will make the system as a whole more flexible due to the 368355f576SJeff Roberson * ability to move memory to subsystems which most need it instead of leaving 378355f576SJeff Roberson * pools of reserved memory unused. 388355f576SJeff Roberson * 398355f576SJeff Roberson * The basic ideas stem from similar slab/zone based allocators whose algorithms 408355f576SJeff Roberson * are well known. 418355f576SJeff Roberson * 428355f576SJeff Roberson */ 438355f576SJeff Roberson 448355f576SJeff Roberson /* 458355f576SJeff Roberson * TODO: 468355f576SJeff Roberson * - Improve memory usage for large allocations 478355f576SJeff Roberson * - Investigate cache size adjustments 488355f576SJeff Roberson */ 498355f576SJeff Roberson 50874651b1SDavid E. O'Brien #include <sys/cdefs.h> 51874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 52874651b1SDavid E. O'Brien 538355f576SJeff Roberson /* I should really use ktr.. */ 548355f576SJeff Roberson /* 558355f576SJeff Roberson #define UMA_DEBUG 1 568355f576SJeff Roberson #define UMA_DEBUG_ALLOC 1 578355f576SJeff Roberson #define UMA_DEBUG_ALLOC_1 1 588355f576SJeff Roberson */ 598355f576SJeff Roberson 6048c5777eSRobert Watson #include "opt_ddb.h" 618355f576SJeff Roberson #include "opt_param.h" 628d689e04SGleb Smirnoff #include "opt_vm.h" 6348c5777eSRobert Watson 648355f576SJeff Roberson #include <sys/param.h> 658355f576SJeff Roberson #include <sys/systm.h> 66ef72505eSJeff Roberson #include <sys/bitset.h> 678355f576SJeff Roberson #include <sys/kernel.h> 688355f576SJeff Roberson #include <sys/types.h> 698355f576SJeff Roberson #include <sys/queue.h> 708355f576SJeff Roberson #include <sys/malloc.h> 713659f747SRobert Watson #include <sys/ktr.h> 728355f576SJeff Roberson #include <sys/lock.h> 738355f576SJeff Roberson #include <sys/sysctl.h> 748355f576SJeff Roberson #include <sys/mutex.h> 754c1cc01cSJohn Baldwin #include <sys/proc.h> 7689f6b863SAttilio Rao #include <sys/rwlock.h> 777a52a97eSRobert Watson #include <sys/sbuf.h> 78*a2de44abSAlexander Motin #include <sys/sched.h> 798355f576SJeff Roberson #include <sys/smp.h> 8086bbae32SJeff Roberson #include <sys/vmmeter.h> 8186bbae32SJeff Roberson 828355f576SJeff Roberson #include <vm/vm.h> 838355f576SJeff Roberson #include <vm/vm_object.h> 848355f576SJeff Roberson #include <vm/vm_page.h> 85a4915c21SAttilio Rao #include <vm/vm_pageout.h> 868355f576SJeff Roberson #include <vm/vm_param.h> 878355f576SJeff Roberson #include <vm/vm_map.h> 888355f576SJeff Roberson #include <vm/vm_kern.h> 898355f576SJeff Roberson #include <vm/vm_extern.h> 908355f576SJeff Roberson #include <vm/uma.h> 918355f576SJeff Roberson #include <vm/uma_int.h> 92639c9550SJeff Roberson #include <vm/uma_dbg.h> 938355f576SJeff Roberson 9448c5777eSRobert Watson #include <ddb/ddb.h> 9548c5777eSRobert Watson 968d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 978d689e04SGleb Smirnoff #include <vm/memguard.h> 988d689e04SGleb Smirnoff #endif 998d689e04SGleb Smirnoff 1008355f576SJeff Roberson /* 101099a0e58SBosko Milekic * This is the zone and keg from which all zones are spawned. The idea is that 102099a0e58SBosko Milekic * even the zone & keg heads are allocated from the allocator, so we use the 103099a0e58SBosko Milekic * bss section to bootstrap us. 1048355f576SJeff Roberson */ 105099a0e58SBosko Milekic static struct uma_keg masterkeg; 106099a0e58SBosko Milekic static struct uma_zone masterzone_k; 107099a0e58SBosko Milekic static struct uma_zone masterzone_z; 108099a0e58SBosko Milekic static uma_zone_t kegs = &masterzone_k; 109099a0e58SBosko Milekic static uma_zone_t zones = &masterzone_z; 1108355f576SJeff Roberson 1118355f576SJeff Roberson /* This is the zone from which all of uma_slab_t's are allocated. */ 1128355f576SJeff Roberson static uma_zone_t slabzone; 113099a0e58SBosko Milekic static uma_zone_t slabrefzone; /* With refcounters (for UMA_ZONE_REFCNT) */ 1148355f576SJeff Roberson 1158355f576SJeff Roberson /* 1168355f576SJeff Roberson * The initial hash tables come out of this zone so they can be allocated 1178355f576SJeff Roberson * prior to malloc coming up. 1188355f576SJeff Roberson */ 1198355f576SJeff Roberson static uma_zone_t hashzone; 1208355f576SJeff Roberson 1211e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */ 122e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1; 1231e319f6dSRobert Watson 124961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets"); 125961647dfSJeff Roberson 1268355f576SJeff Roberson /* 12786bbae32SJeff Roberson * Are we allowed to allocate buckets? 12886bbae32SJeff Roberson */ 12986bbae32SJeff Roberson static int bucketdisable = 1; 13086bbae32SJeff Roberson 131099a0e58SBosko Milekic /* Linked list of all kegs in the system */ 13213e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs); 1338355f576SJeff Roberson 134099a0e58SBosko Milekic /* This mutex protects the keg list */ 1350095a784SJeff Roberson static struct mtx_padalign uma_mtx; 1368355f576SJeff Roberson 1378355f576SJeff Roberson /* Linked list of boot time pages */ 1388355f576SJeff Roberson static LIST_HEAD(,uma_slab) uma_boot_pages = 13913e403fdSAntoine Brodin LIST_HEAD_INITIALIZER(uma_boot_pages); 1408355f576SJeff Roberson 141f353d338SAlan Cox /* This mutex protects the boot time pages list */ 1420095a784SJeff Roberson static struct mtx_padalign uma_boot_pages_mtx; 1438355f576SJeff Roberson 1448355f576SJeff Roberson /* Is the VM done starting up? */ 1458355f576SJeff Roberson static int booted = 0; 146342f1793SAlan Cox #define UMA_STARTUP 1 147342f1793SAlan Cox #define UMA_STARTUP2 2 1488355f576SJeff Roberson 149244f4554SBosko Milekic /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */ 150ef72505eSJeff Roberson static const u_int uma_max_ipers = SLAB_SETSIZE; 151ef72505eSJeff Roberson 152ef72505eSJeff Roberson /* 153ef72505eSJeff Roberson * Only mbuf clusters use ref zones. Just provide enough references 154ef72505eSJeff Roberson * to support the one user. New code should not use the ref facility. 155ef72505eSJeff Roberson */ 156ef72505eSJeff Roberson static const u_int uma_max_ipers_ref = PAGE_SIZE / MCLBYTES; 157244f4554SBosko Milekic 1589643769aSJeff Roberson /* 1599643769aSJeff Roberson * This is the handle used to schedule events that need to happen 1609643769aSJeff Roberson * outside of the allocation fast path. 1619643769aSJeff Roberson */ 1628355f576SJeff Roberson static struct callout uma_callout; 1639643769aSJeff Roberson #define UMA_TIMEOUT 20 /* Seconds for callout interval. */ 1648355f576SJeff Roberson 1658355f576SJeff Roberson /* 1668355f576SJeff Roberson * This structure is passed as the zone ctor arg so that I don't have to create 1678355f576SJeff Roberson * a special allocation function just for zones. 1688355f576SJeff Roberson */ 1698355f576SJeff Roberson struct uma_zctor_args { 170bb196eb4SMatthew D Fleming const char *name; 171c3bdc05fSAndrew R. Reiter size_t size; 1728355f576SJeff Roberson uma_ctor ctor; 1738355f576SJeff Roberson uma_dtor dtor; 1748355f576SJeff Roberson uma_init uminit; 1758355f576SJeff Roberson uma_fini fini; 1760095a784SJeff Roberson uma_import import; 1770095a784SJeff Roberson uma_release release; 1780095a784SJeff Roberson void *arg; 179099a0e58SBosko Milekic uma_keg_t keg; 180099a0e58SBosko Milekic int align; 18185dcf349SGleb Smirnoff uint32_t flags; 182099a0e58SBosko Milekic }; 183099a0e58SBosko Milekic 184099a0e58SBosko Milekic struct uma_kctor_args { 185099a0e58SBosko Milekic uma_zone_t zone; 186099a0e58SBosko Milekic size_t size; 187099a0e58SBosko Milekic uma_init uminit; 188099a0e58SBosko Milekic uma_fini fini; 1898355f576SJeff Roberson int align; 19085dcf349SGleb Smirnoff uint32_t flags; 1918355f576SJeff Roberson }; 1928355f576SJeff Roberson 193cae33c14SJeff Roberson struct uma_bucket_zone { 194cae33c14SJeff Roberson uma_zone_t ubz_zone; 195cae33c14SJeff Roberson char *ubz_name; 196fc03d22bSJeff Roberson int ubz_entries; /* Number of items it can hold. */ 197fc03d22bSJeff Roberson int ubz_maxsize; /* Maximum allocation size per-item. */ 198cae33c14SJeff Roberson }; 199cae33c14SJeff Roberson 200f9d27e75SRobert Watson /* 201fc03d22bSJeff Roberson * Compute the actual number of bucket entries to pack them in power 202fc03d22bSJeff Roberson * of two sizes for more efficient space utilization. 203f9d27e75SRobert Watson */ 204fc03d22bSJeff Roberson #define BUCKET_SIZE(n) \ 205fc03d22bSJeff Roberson (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *)) 206fc03d22bSJeff Roberson 207fc03d22bSJeff Roberson #define BUCKET_MAX BUCKET_SIZE(128) 208fc03d22bSJeff Roberson 209fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = { 2106fd34d6fSJeff Roberson { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 }, 211f3932e90SAlexander Motin { NULL, "6 Bucket", BUCKET_SIZE(6), 3072 }, 2126fd34d6fSJeff Roberson { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 }, 213f3932e90SAlexander Motin { NULL, "12 Bucket", BUCKET_SIZE(12), 1536 }, 2146fd34d6fSJeff Roberson { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 }, 215fc03d22bSJeff Roberson { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 216fc03d22bSJeff Roberson { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 217fc03d22bSJeff Roberson { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 218fc03d22bSJeff Roberson { NULL, NULL, 0} 219fc03d22bSJeff Roberson }; 220cae33c14SJeff Roberson 2212019094aSRobert Watson /* 2222019094aSRobert Watson * Flags and enumerations to be passed to internal functions. 2232019094aSRobert Watson */ 224ef72505eSJeff Roberson enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI }; 225b23f72e9SBrian Feldman 2268355f576SJeff Roberson /* Prototypes.. */ 2278355f576SJeff Roberson 22885dcf349SGleb Smirnoff static void *noobj_alloc(uma_zone_t, int, uint8_t *, int); 22985dcf349SGleb Smirnoff static void *page_alloc(uma_zone_t, int, uint8_t *, int); 23085dcf349SGleb Smirnoff static void *startup_alloc(uma_zone_t, int, uint8_t *, int); 23185dcf349SGleb Smirnoff static void page_free(void *, int, uint8_t); 232e20a199fSJeff Roberson static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int); 2339643769aSJeff Roberson static void cache_drain(uma_zone_t); 2348355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t); 235aaa8bb16SJeff Roberson static void bucket_cache_drain(uma_zone_t zone); 236b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int); 237099a0e58SBosko Milekic static void keg_dtor(void *, int, void *); 238b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int); 2399c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *); 240b23f72e9SBrian Feldman static int zero_init(void *, int, int); 241e20a199fSJeff Roberson static void keg_small_init(uma_keg_t keg); 242e20a199fSJeff Roberson static void keg_large_init(uma_keg_t keg); 2438355f576SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t)); 2448355f576SJeff Roberson static void zone_timeout(uma_zone_t zone); 2450aef6126SJeff Roberson static int hash_alloc(struct uma_hash *); 2460aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *); 2470aef6126SJeff Roberson static void hash_free(struct uma_hash *hash); 2488355f576SJeff Roberson static void uma_timeout(void *); 2498355f576SJeff Roberson static void uma_startup3(void); 250e20a199fSJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int); 2510095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 25286bbae32SJeff Roberson static void bucket_enable(void); 253cae33c14SJeff Roberson static void bucket_init(void); 2546fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int); 2556fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *); 256cae33c14SJeff Roberson static void bucket_zone_drain(void); 2576fd34d6fSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t zone, void *, int flags); 258e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags); 259e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags); 2600095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 2610095a784SJeff Roberson static void slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item); 262e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 26385dcf349SGleb Smirnoff uma_fini fini, int align, uint32_t flags); 2640095a784SJeff Roberson static int zone_import(uma_zone_t zone, void **bucket, int max, int flags); 2650095a784SJeff Roberson static void zone_release(uma_zone_t zone, void **bucket, int cnt); 266bbee39c6SJeff Roberson 2678355f576SJeff Roberson void uma_print_zone(uma_zone_t); 2688355f576SJeff Roberson void uma_print_stats(void); 2697a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 2707a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 2718355f576SJeff Roberson 2728355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 2738355f576SJeff Roberson 2747a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT, 2757a52a97eSRobert Watson 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 2767a52a97eSRobert Watson 2777a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 2787a52a97eSRobert Watson 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 2797a52a97eSRobert Watson 2802f891cd5SPawel Jakub Dawidek static int zone_warnings = 1; 2812f891cd5SPawel Jakub Dawidek TUNABLE_INT("vm.zone_warnings", &zone_warnings); 2822f891cd5SPawel Jakub Dawidek SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RW, &zone_warnings, 0, 2832f891cd5SPawel Jakub Dawidek "Warn when UMA zones becomes full"); 2842f891cd5SPawel Jakub Dawidek 28586bbae32SJeff Roberson /* 28686bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 28786bbae32SJeff Roberson */ 28886bbae32SJeff Roberson static void 28986bbae32SJeff Roberson bucket_enable(void) 29086bbae32SJeff Roberson { 291251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 29286bbae32SJeff Roberson } 29386bbae32SJeff Roberson 294dc2c7965SRobert Watson /* 295dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 296dc2c7965SRobert Watson * 297dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 298fc03d22bSJeff Roberson * of the header and an array of pointers. 299dc2c7965SRobert Watson */ 300cae33c14SJeff Roberson static void 301cae33c14SJeff Roberson bucket_init(void) 302cae33c14SJeff Roberson { 303cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 304cae33c14SJeff Roberson int size; 305fc03d22bSJeff Roberson int i; 306cae33c14SJeff Roberson 307fc03d22bSJeff Roberson for (i = 0, ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 308cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 309cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 310cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 311e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 3126fd34d6fSJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET); 313cae33c14SJeff Roberson } 314cae33c14SJeff Roberson } 315cae33c14SJeff Roberson 316dc2c7965SRobert Watson /* 317dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 318dc2c7965SRobert Watson * to allocate the bucket. 319dc2c7965SRobert Watson */ 320dc2c7965SRobert Watson static struct uma_bucket_zone * 321dc2c7965SRobert Watson bucket_zone_lookup(int entries) 322dc2c7965SRobert Watson { 323fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 324dc2c7965SRobert Watson 325fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 326fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 327fc03d22bSJeff Roberson return (ubz); 328fc03d22bSJeff Roberson ubz--; 329fc03d22bSJeff Roberson return (ubz); 330fc03d22bSJeff Roberson } 331fc03d22bSJeff Roberson 332fc03d22bSJeff Roberson static int 333fc03d22bSJeff Roberson bucket_select(int size) 334fc03d22bSJeff Roberson { 335fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 336fc03d22bSJeff Roberson 337fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 338fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 339fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 340fc03d22bSJeff Roberson 341fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 342fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 343fc03d22bSJeff Roberson break; 344fc03d22bSJeff Roberson ubz--; 345fc03d22bSJeff Roberson return (ubz->ubz_entries); 346dc2c7965SRobert Watson } 347dc2c7965SRobert Watson 348cae33c14SJeff Roberson static uma_bucket_t 3496fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags) 350cae33c14SJeff Roberson { 351cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 352cae33c14SJeff Roberson uma_bucket_t bucket; 353cae33c14SJeff Roberson 354cae33c14SJeff Roberson /* 355cae33c14SJeff Roberson * This is to stop us from allocating per cpu buckets while we're 3563803b26bSDag-Erling Smørgrav * running out of vm.boot_pages. Otherwise, we would exhaust the 357cae33c14SJeff Roberson * boot pages. This also prevents us from allocating buckets in 358cae33c14SJeff Roberson * low memory situations. 359cae33c14SJeff Roberson */ 360cae33c14SJeff Roberson if (bucketdisable) 361cae33c14SJeff Roberson return (NULL); 3626fd34d6fSJeff Roberson /* 3636fd34d6fSJeff Roberson * To limit bucket recursion we store the original zone flags 3646fd34d6fSJeff Roberson * in a cookie passed via zalloc_arg/zfree_arg. This allows the 3656fd34d6fSJeff Roberson * NOVM flag to persist even through deep recursions. We also 3666fd34d6fSJeff Roberson * store ZFLAG_BUCKET once we have recursed attempting to allocate 3676fd34d6fSJeff Roberson * a bucket for a bucket zone so we do not allow infinite bucket 3686fd34d6fSJeff Roberson * recursion. This cookie will even persist to frees of unused 3696fd34d6fSJeff Roberson * buckets via the allocation path or bucket allocations in the 3706fd34d6fSJeff Roberson * free path. 3716fd34d6fSJeff Roberson */ 3726fd34d6fSJeff Roberson if ((uintptr_t)udata & UMA_ZFLAG_BUCKET) 3736fd34d6fSJeff Roberson return (NULL); 3746fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 3756fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 3766fd34d6fSJeff Roberson else 3776fd34d6fSJeff Roberson udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET); 3786fd34d6fSJeff Roberson if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY) 379af526374SJeff Roberson flags |= M_NOVM; 380af526374SJeff Roberson ubz = bucket_zone_lookup(zone->uz_count); 3816fd34d6fSJeff Roberson bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags); 382cae33c14SJeff Roberson if (bucket) { 383cae33c14SJeff Roberson #ifdef INVARIANTS 384cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 385cae33c14SJeff Roberson #endif 386cae33c14SJeff Roberson bucket->ub_cnt = 0; 387cae33c14SJeff Roberson bucket->ub_entries = ubz->ubz_entries; 388cae33c14SJeff Roberson } 389cae33c14SJeff Roberson 390cae33c14SJeff Roberson return (bucket); 391cae33c14SJeff Roberson } 392cae33c14SJeff Roberson 393cae33c14SJeff Roberson static void 3946fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata) 395cae33c14SJeff Roberson { 396cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 397cae33c14SJeff Roberson 398fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 399fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 4006fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 4016fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 402dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 4036fd34d6fSJeff Roberson uma_zfree_arg(ubz->ubz_zone, bucket, udata); 404cae33c14SJeff Roberson } 405cae33c14SJeff Roberson 406cae33c14SJeff Roberson static void 407cae33c14SJeff Roberson bucket_zone_drain(void) 408cae33c14SJeff Roberson { 409cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 410cae33c14SJeff Roberson 411cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 412cae33c14SJeff Roberson zone_drain(ubz->ubz_zone); 413cae33c14SJeff Roberson } 414cae33c14SJeff Roberson 4152f891cd5SPawel Jakub Dawidek static void 4162f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 4172f891cd5SPawel Jakub Dawidek { 4182f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 4192f891cd5SPawel Jakub Dawidek 4202f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 4212f891cd5SPawel Jakub Dawidek return; 4222f891cd5SPawel Jakub Dawidek 4232f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 4242f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 4252f891cd5SPawel Jakub Dawidek } 4262f891cd5SPawel Jakub Dawidek 427e20a199fSJeff Roberson static void 428e20a199fSJeff Roberson zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t)) 429e20a199fSJeff Roberson { 430e20a199fSJeff Roberson uma_klink_t klink; 431e20a199fSJeff Roberson 432e20a199fSJeff Roberson LIST_FOREACH(klink, &zone->uz_kegs, kl_link) 433e20a199fSJeff Roberson kegfn(klink->kl_keg); 434e20a199fSJeff Roberson } 4358355f576SJeff Roberson 4368355f576SJeff Roberson /* 4378355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 4389643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 4398355f576SJeff Roberson * 4408355f576SJeff Roberson * Arguments: 4418355f576SJeff Roberson * arg Unused 4428355f576SJeff Roberson * 4438355f576SJeff Roberson * Returns: 4448355f576SJeff Roberson * Nothing 4458355f576SJeff Roberson */ 4468355f576SJeff Roberson static void 4478355f576SJeff Roberson uma_timeout(void *unused) 4488355f576SJeff Roberson { 44986bbae32SJeff Roberson bucket_enable(); 4508355f576SJeff Roberson zone_foreach(zone_timeout); 4518355f576SJeff Roberson 4528355f576SJeff Roberson /* Reschedule this event */ 4539643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 4548355f576SJeff Roberson } 4558355f576SJeff Roberson 4568355f576SJeff Roberson /* 4579643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 4589643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 4598355f576SJeff Roberson * 460e20a199fSJeff Roberson * Returns nothing. 4618355f576SJeff Roberson */ 4628355f576SJeff Roberson static void 463e20a199fSJeff Roberson keg_timeout(uma_keg_t keg) 4648355f576SJeff Roberson { 4658355f576SJeff Roberson 466e20a199fSJeff Roberson KEG_LOCK(keg); 4678355f576SJeff Roberson /* 468e20a199fSJeff Roberson * Expand the keg hash table. 4698355f576SJeff Roberson * 4708355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 4718355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 4728355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 4738355f576SJeff Roberson */ 474099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH && 475099a0e58SBosko Milekic keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) { 4760aef6126SJeff Roberson struct uma_hash newhash; 4770aef6126SJeff Roberson struct uma_hash oldhash; 4780aef6126SJeff Roberson int ret; 4795300d9ddSJeff Roberson 4800aef6126SJeff Roberson /* 4810aef6126SJeff Roberson * This is so involved because allocating and freeing 482e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 4830aef6126SJeff Roberson * I have to do everything in stages and check for 4840aef6126SJeff Roberson * races. 4850aef6126SJeff Roberson */ 486099a0e58SBosko Milekic newhash = keg->uk_hash; 487e20a199fSJeff Roberson KEG_UNLOCK(keg); 4880aef6126SJeff Roberson ret = hash_alloc(&newhash); 489e20a199fSJeff Roberson KEG_LOCK(keg); 4900aef6126SJeff Roberson if (ret) { 491099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 492099a0e58SBosko Milekic oldhash = keg->uk_hash; 493099a0e58SBosko Milekic keg->uk_hash = newhash; 4940aef6126SJeff Roberson } else 4950aef6126SJeff Roberson oldhash = newhash; 4960aef6126SJeff Roberson 497e20a199fSJeff Roberson KEG_UNLOCK(keg); 4980aef6126SJeff Roberson hash_free(&oldhash); 499a1dff920SDavide Italiano return; 5000aef6126SJeff Roberson } 5015300d9ddSJeff Roberson } 502e20a199fSJeff Roberson KEG_UNLOCK(keg); 503e20a199fSJeff Roberson } 504e20a199fSJeff Roberson 505e20a199fSJeff Roberson static void 506e20a199fSJeff Roberson zone_timeout(uma_zone_t zone) 507e20a199fSJeff Roberson { 508e20a199fSJeff Roberson 509e20a199fSJeff Roberson zone_foreach_keg(zone, &keg_timeout); 5108355f576SJeff Roberson } 5118355f576SJeff Roberson 5128355f576SJeff Roberson /* 5135300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 5145300d9ddSJeff Roberson * backing store. 5155300d9ddSJeff Roberson * 5165300d9ddSJeff Roberson * Arguments: 5170aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 5185300d9ddSJeff Roberson * 5195300d9ddSJeff Roberson * Returns: 5200aef6126SJeff Roberson * 1 on sucess and 0 on failure. 5215300d9ddSJeff Roberson */ 52237c84183SPoul-Henning Kamp static int 5230aef6126SJeff Roberson hash_alloc(struct uma_hash *hash) 5245300d9ddSJeff Roberson { 5250aef6126SJeff Roberson int oldsize; 5265300d9ddSJeff Roberson int alloc; 5275300d9ddSJeff Roberson 5280aef6126SJeff Roberson oldsize = hash->uh_hashsize; 5290aef6126SJeff Roberson 5305300d9ddSJeff Roberson /* We're just going to go to a power of two greater */ 5310aef6126SJeff Roberson if (oldsize) { 5320aef6126SJeff Roberson hash->uh_hashsize = oldsize * 2; 5330aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 5340aef6126SJeff Roberson hash->uh_slab_hash = (struct slabhead *)malloc(alloc, 535961647dfSJeff Roberson M_UMAHASH, M_NOWAIT); 5365300d9ddSJeff Roberson } else { 5370aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 538e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 539a163d034SWarner Losh M_WAITOK); 5400aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 5415300d9ddSJeff Roberson } 5420aef6126SJeff Roberson if (hash->uh_slab_hash) { 5430aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 5440aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 5450aef6126SJeff Roberson return (1); 5460aef6126SJeff Roberson } 5475300d9ddSJeff Roberson 5480aef6126SJeff Roberson return (0); 5495300d9ddSJeff Roberson } 5505300d9ddSJeff Roberson 5515300d9ddSJeff Roberson /* 55264f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 55364f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 55464f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 5558355f576SJeff Roberson * 5568355f576SJeff Roberson * Arguments: 5570aef6126SJeff Roberson * oldhash The hash you want to expand 5580aef6126SJeff Roberson * newhash The hash structure for the new table 5598355f576SJeff Roberson * 5608355f576SJeff Roberson * Returns: 5618355f576SJeff Roberson * Nothing 5628355f576SJeff Roberson * 5638355f576SJeff Roberson * Discussion: 5648355f576SJeff Roberson */ 5650aef6126SJeff Roberson static int 5660aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 5678355f576SJeff Roberson { 5688355f576SJeff Roberson uma_slab_t slab; 5698355f576SJeff Roberson int hval; 5708355f576SJeff Roberson int i; 5718355f576SJeff Roberson 5720aef6126SJeff Roberson if (!newhash->uh_slab_hash) 5730aef6126SJeff Roberson return (0); 5748355f576SJeff Roberson 5750aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 5760aef6126SJeff Roberson return (0); 5778355f576SJeff Roberson 5788355f576SJeff Roberson /* 5798355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 5808355f576SJeff Roberson * full rehash. 5818355f576SJeff Roberson */ 5828355f576SJeff Roberson 5830aef6126SJeff Roberson for (i = 0; i < oldhash->uh_hashsize; i++) 5840aef6126SJeff Roberson while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) { 5850aef6126SJeff Roberson slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]); 5860aef6126SJeff Roberson SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink); 5870aef6126SJeff Roberson hval = UMA_HASH(newhash, slab->us_data); 5880aef6126SJeff Roberson SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 5890aef6126SJeff Roberson slab, us_hlink); 5908355f576SJeff Roberson } 5918355f576SJeff Roberson 5920aef6126SJeff Roberson return (1); 5939c2cd7e5SJeff Roberson } 5949c2cd7e5SJeff Roberson 5955300d9ddSJeff Roberson /* 5965300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 5975300d9ddSJeff Roberson * 5985300d9ddSJeff Roberson * Arguments: 5995300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 6005300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 6015300d9ddSJeff Roberson * 6025300d9ddSJeff Roberson * Returns: 6035300d9ddSJeff Roberson * Nothing 6045300d9ddSJeff Roberson */ 6059c2cd7e5SJeff Roberson static void 6060aef6126SJeff Roberson hash_free(struct uma_hash *hash) 6079c2cd7e5SJeff Roberson { 6080aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 6090aef6126SJeff Roberson return; 6100aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 6110095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 6128355f576SJeff Roberson else 613961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 6148355f576SJeff Roberson } 6158355f576SJeff Roberson 6168355f576SJeff Roberson /* 6178355f576SJeff Roberson * Frees all outstanding items in a bucket 6188355f576SJeff Roberson * 6198355f576SJeff Roberson * Arguments: 6208355f576SJeff Roberson * zone The zone to free to, must be unlocked. 6218355f576SJeff Roberson * bucket The free/alloc bucket with items, cpu queue must be locked. 6228355f576SJeff Roberson * 6238355f576SJeff Roberson * Returns: 6248355f576SJeff Roberson * Nothing 6258355f576SJeff Roberson */ 6268355f576SJeff Roberson 6278355f576SJeff Roberson static void 6288355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 6298355f576SJeff Roberson { 6300095a784SJeff Roberson int i; 6318355f576SJeff Roberson 6328355f576SJeff Roberson if (bucket == NULL) 6338355f576SJeff Roberson return; 6348355f576SJeff Roberson 6350095a784SJeff Roberson if (zone->uz_fini) 6360095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 6370095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 6380095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 6390095a784SJeff Roberson bucket->ub_cnt = 0; 6408355f576SJeff Roberson } 6418355f576SJeff Roberson 6428355f576SJeff Roberson /* 6438355f576SJeff Roberson * Drains the per cpu caches for a zone. 6448355f576SJeff Roberson * 6455d1ae027SRobert Watson * NOTE: This may only be called while the zone is being turn down, and not 6465d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 6475d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 6485d1ae027SRobert Watson * 6498355f576SJeff Roberson * Arguments: 6508355f576SJeff Roberson * zone The zone to drain, must be unlocked. 6518355f576SJeff Roberson * 6528355f576SJeff Roberson * Returns: 6538355f576SJeff Roberson * Nothing 6548355f576SJeff Roberson */ 6558355f576SJeff Roberson static void 6569643769aSJeff Roberson cache_drain(uma_zone_t zone) 6578355f576SJeff Roberson { 6588355f576SJeff Roberson uma_cache_t cache; 6598355f576SJeff Roberson int cpu; 6608355f576SJeff Roberson 6618355f576SJeff Roberson /* 6625d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 6635d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 6645d1ae027SRobert Watson * of the caches at this point. 6655d1ae027SRobert Watson * 6665d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 6675d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 6685d1ae027SRobert Watson * 6695d1ae027SRobert Watson * XXX: We lock the zone before passing into bucket_cache_drain() as 6705d1ae027SRobert Watson * it is used elsewhere. Should the tear-down path be made special 6715d1ae027SRobert Watson * there in some form? 6728355f576SJeff Roberson */ 6733aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 6748355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 6758355f576SJeff Roberson bucket_drain(zone, cache->uc_allocbucket); 6768355f576SJeff Roberson bucket_drain(zone, cache->uc_freebucket); 677174ab450SBosko Milekic if (cache->uc_allocbucket != NULL) 6786fd34d6fSJeff Roberson bucket_free(zone, cache->uc_allocbucket, NULL); 679174ab450SBosko Milekic if (cache->uc_freebucket != NULL) 6806fd34d6fSJeff Roberson bucket_free(zone, cache->uc_freebucket, NULL); 681d56368d7SBosko Milekic cache->uc_allocbucket = cache->uc_freebucket = NULL; 682d56368d7SBosko Milekic } 683aaa8bb16SJeff Roberson ZONE_LOCK(zone); 684aaa8bb16SJeff Roberson bucket_cache_drain(zone); 685aaa8bb16SJeff Roberson ZONE_UNLOCK(zone); 686aaa8bb16SJeff Roberson } 687aaa8bb16SJeff Roberson 688*a2de44abSAlexander Motin static void 689*a2de44abSAlexander Motin cache_shrink(uma_zone_t zone) 690*a2de44abSAlexander Motin { 691*a2de44abSAlexander Motin 692*a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 693*a2de44abSAlexander Motin return; 694*a2de44abSAlexander Motin 695*a2de44abSAlexander Motin ZONE_LOCK(zone); 696*a2de44abSAlexander Motin zone->uz_count = (zone->uz_count_min + zone->uz_count) / 2; 697*a2de44abSAlexander Motin ZONE_UNLOCK(zone); 698*a2de44abSAlexander Motin } 699*a2de44abSAlexander Motin 700*a2de44abSAlexander Motin static void 701*a2de44abSAlexander Motin cache_drain_safe_cpu(uma_zone_t zone) 702*a2de44abSAlexander Motin { 703*a2de44abSAlexander Motin uma_cache_t cache; 704*a2de44abSAlexander Motin 705*a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 706*a2de44abSAlexander Motin return; 707*a2de44abSAlexander Motin 708*a2de44abSAlexander Motin ZONE_LOCK(zone); 709*a2de44abSAlexander Motin critical_enter(); 710*a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 711*a2de44abSAlexander Motin if (cache->uc_allocbucket) { 712*a2de44abSAlexander Motin LIST_INSERT_HEAD(&zone->uz_buckets, cache->uc_allocbucket, 713*a2de44abSAlexander Motin ub_link); 714*a2de44abSAlexander Motin cache->uc_allocbucket = NULL; 715*a2de44abSAlexander Motin } 716*a2de44abSAlexander Motin if (cache->uc_freebucket) { 717*a2de44abSAlexander Motin LIST_INSERT_HEAD(&zone->uz_buckets, cache->uc_freebucket, 718*a2de44abSAlexander Motin ub_link); 719*a2de44abSAlexander Motin cache->uc_freebucket = NULL; 720*a2de44abSAlexander Motin } 721*a2de44abSAlexander Motin critical_exit(); 722*a2de44abSAlexander Motin ZONE_UNLOCK(zone); 723*a2de44abSAlexander Motin } 724*a2de44abSAlexander Motin 725*a2de44abSAlexander Motin /* 726*a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 727*a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 728*a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 729*a2de44abSAlexander Motin * to safely access their cache buckets. 730*a2de44abSAlexander Motin * Zone lock must not be held on call this function. 731*a2de44abSAlexander Motin */ 732*a2de44abSAlexander Motin static void 733*a2de44abSAlexander Motin cache_drain_safe(uma_zone_t zone) 734*a2de44abSAlexander Motin { 735*a2de44abSAlexander Motin int cpu; 736*a2de44abSAlexander Motin 737*a2de44abSAlexander Motin /* 738*a2de44abSAlexander Motin * Polite bucket sizes shrinking was not enouth, shrink aggressively. 739*a2de44abSAlexander Motin */ 740*a2de44abSAlexander Motin if (zone) 741*a2de44abSAlexander Motin cache_shrink(zone); 742*a2de44abSAlexander Motin else 743*a2de44abSAlexander Motin zone_foreach(cache_shrink); 744*a2de44abSAlexander Motin 745*a2de44abSAlexander Motin CPU_FOREACH(cpu) { 746*a2de44abSAlexander Motin thread_lock(curthread); 747*a2de44abSAlexander Motin sched_bind(curthread, cpu); 748*a2de44abSAlexander Motin thread_unlock(curthread); 749*a2de44abSAlexander Motin 750*a2de44abSAlexander Motin if (zone) 751*a2de44abSAlexander Motin cache_drain_safe_cpu(zone); 752*a2de44abSAlexander Motin else 753*a2de44abSAlexander Motin zone_foreach(cache_drain_safe_cpu); 754*a2de44abSAlexander Motin } 755*a2de44abSAlexander Motin thread_lock(curthread); 756*a2de44abSAlexander Motin sched_unbind(curthread); 757*a2de44abSAlexander Motin thread_unlock(curthread); 758*a2de44abSAlexander Motin } 759*a2de44abSAlexander Motin 760aaa8bb16SJeff Roberson /* 761aaa8bb16SJeff Roberson * Drain the cached buckets from a zone. Expects a locked zone on entry. 762aaa8bb16SJeff Roberson */ 763aaa8bb16SJeff Roberson static void 764aaa8bb16SJeff Roberson bucket_cache_drain(uma_zone_t zone) 765aaa8bb16SJeff Roberson { 766aaa8bb16SJeff Roberson uma_bucket_t bucket; 7678355f576SJeff Roberson 7688355f576SJeff Roberson /* 7698355f576SJeff Roberson * Drain the bucket queues and free the buckets, we just keep two per 7708355f576SJeff Roberson * cpu (alloc/free). 7718355f576SJeff Roberson */ 772fc03d22bSJeff Roberson while ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) { 7738355f576SJeff Roberson LIST_REMOVE(bucket, ub_link); 7748355f576SJeff Roberson ZONE_UNLOCK(zone); 7758355f576SJeff Roberson bucket_drain(zone, bucket); 7766fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 7778355f576SJeff Roberson ZONE_LOCK(zone); 7788355f576SJeff Roberson } 779ace66b56SAlexander Motin 780ace66b56SAlexander Motin /* 781ace66b56SAlexander Motin * Shrink further bucket sizes. Price of single zone lock collision 782ace66b56SAlexander Motin * is probably lower then price of global cache drain. 783ace66b56SAlexander Motin */ 784ace66b56SAlexander Motin if (zone->uz_count > zone->uz_count_min) 785ace66b56SAlexander Motin zone->uz_count--; 7868355f576SJeff Roberson } 787fc03d22bSJeff Roberson 788fc03d22bSJeff Roberson static void 789fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 790fc03d22bSJeff Roberson { 791fc03d22bSJeff Roberson uint8_t *mem; 792fc03d22bSJeff Roberson int i; 793fc03d22bSJeff Roberson uint8_t flags; 794fc03d22bSJeff Roberson 795fc03d22bSJeff Roberson mem = slab->us_data; 796fc03d22bSJeff Roberson flags = slab->us_flags; 797fc03d22bSJeff Roberson i = start; 798fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 799fc03d22bSJeff Roberson for (i--; i > -1; i--) 800fc03d22bSJeff Roberson keg->uk_fini(slab->us_data + (keg->uk_rsize * i), 801fc03d22bSJeff Roberson keg->uk_size); 802fc03d22bSJeff Roberson } 803fc03d22bSJeff Roberson if (keg->uk_flags & UMA_ZONE_OFFPAGE) 804fc03d22bSJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 805fc03d22bSJeff Roberson #ifdef UMA_DEBUG 806fc03d22bSJeff Roberson printf("%s: Returning %d bytes.\n", keg->uk_name, 807fc03d22bSJeff Roberson PAGE_SIZE * keg->uk_ppera); 808fc03d22bSJeff Roberson #endif 809fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 8108355f576SJeff Roberson } 8118355f576SJeff Roberson 8128355f576SJeff Roberson /* 813e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 8148355f576SJeff Roberson * the pageout daemon. 8158355f576SJeff Roberson * 816e20a199fSJeff Roberson * Returns nothing. 8178355f576SJeff Roberson */ 818e20a199fSJeff Roberson static void 819e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 8208355f576SJeff Roberson { 8211e183df2SStefan Farfeleder struct slabhead freeslabs = { 0 }; 8228355f576SJeff Roberson uma_slab_t slab; 8238355f576SJeff Roberson uma_slab_t n; 8248355f576SJeff Roberson 8258355f576SJeff Roberson /* 826e20a199fSJeff Roberson * We don't want to take pages from statically allocated kegs at this 8278355f576SJeff Roberson * time 8288355f576SJeff Roberson */ 829099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 8308355f576SJeff Roberson return; 8318355f576SJeff Roberson 8328355f576SJeff Roberson #ifdef UMA_DEBUG 833e20a199fSJeff Roberson printf("%s free items: %u\n", keg->uk_name, keg->uk_free); 8348355f576SJeff Roberson #endif 835e20a199fSJeff Roberson KEG_LOCK(keg); 836099a0e58SBosko Milekic if (keg->uk_free == 0) 8378355f576SJeff Roberson goto finished; 8388355f576SJeff Roberson 839099a0e58SBosko Milekic slab = LIST_FIRST(&keg->uk_free_slab); 8409643769aSJeff Roberson while (slab) { 8418355f576SJeff Roberson n = LIST_NEXT(slab, us_link); 8428355f576SJeff Roberson 8438355f576SJeff Roberson /* We have no where to free these to */ 8448355f576SJeff Roberson if (slab->us_flags & UMA_SLAB_BOOT) { 8458355f576SJeff Roberson slab = n; 8468355f576SJeff Roberson continue; 8478355f576SJeff Roberson } 8488355f576SJeff Roberson 8498355f576SJeff Roberson LIST_REMOVE(slab, us_link); 850099a0e58SBosko Milekic keg->uk_pages -= keg->uk_ppera; 851099a0e58SBosko Milekic keg->uk_free -= keg->uk_ipers; 852713deb36SJeff Roberson 853099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 854099a0e58SBosko Milekic UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data); 855713deb36SJeff Roberson 856713deb36SJeff Roberson SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink); 857713deb36SJeff Roberson 858713deb36SJeff Roberson slab = n; 859713deb36SJeff Roberson } 860713deb36SJeff Roberson finished: 861e20a199fSJeff Roberson KEG_UNLOCK(keg); 862713deb36SJeff Roberson 863713deb36SJeff Roberson while ((slab = SLIST_FIRST(&freeslabs)) != NULL) { 864713deb36SJeff Roberson SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink); 8651645995bSKirk McKusick keg_free_slab(keg, slab, keg->uk_ipers); 8668355f576SJeff Roberson } 8678355f576SJeff Roberson } 8688355f576SJeff Roberson 869e20a199fSJeff Roberson static void 870e20a199fSJeff Roberson zone_drain_wait(uma_zone_t zone, int waitok) 871e20a199fSJeff Roberson { 872e20a199fSJeff Roberson 8738355f576SJeff Roberson /* 874e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 875e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 876e20a199fSJeff Roberson * is the only call that knows the structure will still be available 877e20a199fSJeff Roberson * when it wakes up. 878e20a199fSJeff Roberson */ 879e20a199fSJeff Roberson ZONE_LOCK(zone); 880e20a199fSJeff Roberson while (zone->uz_flags & UMA_ZFLAG_DRAINING) { 881e20a199fSJeff Roberson if (waitok == M_NOWAIT) 882e20a199fSJeff Roberson goto out; 883e20a199fSJeff Roberson mtx_unlock(&uma_mtx); 884af526374SJeff Roberson msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1); 885e20a199fSJeff Roberson mtx_lock(&uma_mtx); 886e20a199fSJeff Roberson } 887e20a199fSJeff Roberson zone->uz_flags |= UMA_ZFLAG_DRAINING; 888e20a199fSJeff Roberson bucket_cache_drain(zone); 889e20a199fSJeff Roberson ZONE_UNLOCK(zone); 890e20a199fSJeff Roberson /* 891e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 892e20a199fSJeff Roberson * we're running. Normally the uma_mtx would protect us but we 893e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 894e20a199fSJeff Roberson */ 895e20a199fSJeff Roberson zone_foreach_keg(zone, &keg_drain); 896e20a199fSJeff Roberson ZONE_LOCK(zone); 897e20a199fSJeff Roberson zone->uz_flags &= ~UMA_ZFLAG_DRAINING; 898e20a199fSJeff Roberson wakeup(zone); 899e20a199fSJeff Roberson out: 900e20a199fSJeff Roberson ZONE_UNLOCK(zone); 901e20a199fSJeff Roberson } 902e20a199fSJeff Roberson 903e20a199fSJeff Roberson void 904e20a199fSJeff Roberson zone_drain(uma_zone_t zone) 905e20a199fSJeff Roberson { 906e20a199fSJeff Roberson 907e20a199fSJeff Roberson zone_drain_wait(zone, M_NOWAIT); 908e20a199fSJeff Roberson } 909e20a199fSJeff Roberson 910e20a199fSJeff Roberson /* 911e20a199fSJeff Roberson * Allocate a new slab for a keg. This does not insert the slab onto a list. 9128355f576SJeff Roberson * 9138355f576SJeff Roberson * Arguments: 9148355f576SJeff Roberson * wait Shall we wait? 9158355f576SJeff Roberson * 9168355f576SJeff Roberson * Returns: 9178355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 9188355f576SJeff Roberson * caller specified M_NOWAIT. 9198355f576SJeff Roberson */ 9208355f576SJeff Roberson static uma_slab_t 921e20a199fSJeff Roberson keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait) 9228355f576SJeff Roberson { 923099a0e58SBosko Milekic uma_slabrefcnt_t slabref; 924e20a199fSJeff Roberson uma_alloc allocf; 925099a0e58SBosko Milekic uma_slab_t slab; 92685dcf349SGleb Smirnoff uint8_t *mem; 92785dcf349SGleb Smirnoff uint8_t flags; 9288355f576SJeff Roberson int i; 9298355f576SJeff Roberson 930e20a199fSJeff Roberson mtx_assert(&keg->uk_lock, MA_OWNED); 931a553d4b8SJeff Roberson slab = NULL; 932fc03d22bSJeff Roberson mem = NULL; 933a553d4b8SJeff Roberson 9348355f576SJeff Roberson #ifdef UMA_DEBUG 9350095a784SJeff Roberson printf("alloc_slab: Allocating a new slab for %s\n", keg->uk_name); 9368355f576SJeff Roberson #endif 937e20a199fSJeff Roberson allocf = keg->uk_allocf; 938e20a199fSJeff Roberson KEG_UNLOCK(keg); 939a553d4b8SJeff Roberson 940099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_OFFPAGE) { 941e20a199fSJeff Roberson slab = zone_alloc_item(keg->uk_slabzone, NULL, wait); 942fc03d22bSJeff Roberson if (slab == NULL) 943fc03d22bSJeff Roberson goto out; 944a553d4b8SJeff Roberson } 945a553d4b8SJeff Roberson 9463370c5bfSJeff Roberson /* 9473370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 9483370c5bfSJeff Roberson * first time they are added to a zone. 9493370c5bfSJeff Roberson * 9503370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 9513370c5bfSJeff Roberson */ 9523370c5bfSJeff Roberson 953099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 9543370c5bfSJeff Roberson wait |= M_ZERO; 9553370c5bfSJeff Roberson else 9563370c5bfSJeff Roberson wait &= ~M_ZERO; 9573370c5bfSJeff Roberson 958263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 959263811f7SKip Macy wait |= M_NODUMP; 960263811f7SKip Macy 961e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 962ad97af7eSGleb Smirnoff mem = allocf(zone, keg->uk_ppera * PAGE_SIZE, &flags, wait); 963a553d4b8SJeff Roberson if (mem == NULL) { 964b23f72e9SBrian Feldman if (keg->uk_flags & UMA_ZONE_OFFPAGE) 9650095a784SJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 966fc03d22bSJeff Roberson slab = NULL; 967fc03d22bSJeff Roberson goto out; 968a553d4b8SJeff Roberson } 9698355f576SJeff Roberson 9705c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 971099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) 972099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 9735c0e403bSJeff Roberson 974e20a199fSJeff Roberson if (keg->uk_flags & UMA_ZONE_VTOSLAB) 975099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 97699571dc3SJeff Roberson vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab); 9778355f576SJeff Roberson 978099a0e58SBosko Milekic slab->us_keg = keg; 9798355f576SJeff Roberson slab->us_data = mem; 980099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 9818355f576SJeff Roberson slab->us_flags = flags; 982ef72505eSJeff Roberson BIT_FILL(SLAB_SETSIZE, &slab->us_free); 983ef72505eSJeff Roberson #ifdef INVARIANTS 984ef72505eSJeff Roberson BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree); 985ef72505eSJeff Roberson #endif 986099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_REFCNT) { 987099a0e58SBosko Milekic slabref = (uma_slabrefcnt_t)slab; 988ab14a3f7SBrian Feldman for (i = 0; i < keg->uk_ipers; i++) 989ef72505eSJeff Roberson slabref->us_refcnt[i] = 0; 990099a0e58SBosko Milekic } 991099a0e58SBosko Milekic 992b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 993099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 994b23f72e9SBrian Feldman if (keg->uk_init(slab->us_data + (keg->uk_rsize * i), 995b23f72e9SBrian Feldman keg->uk_size, wait) != 0) 996b23f72e9SBrian Feldman break; 997b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 998fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 999fc03d22bSJeff Roberson slab = NULL; 1000fc03d22bSJeff Roberson goto out; 1001b23f72e9SBrian Feldman } 1002b23f72e9SBrian Feldman } 1003fc03d22bSJeff Roberson out: 1004e20a199fSJeff Roberson KEG_LOCK(keg); 10055c0e403bSJeff Roberson 1006fc03d22bSJeff Roberson if (slab != NULL) { 1007099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 1008099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 10098355f576SJeff Roberson 1010099a0e58SBosko Milekic keg->uk_pages += keg->uk_ppera; 1011099a0e58SBosko Milekic keg->uk_free += keg->uk_ipers; 1012fc03d22bSJeff Roberson } 10138355f576SJeff Roberson 10148355f576SJeff Roberson return (slab); 10158355f576SJeff Roberson } 10168355f576SJeff Roberson 10178355f576SJeff Roberson /* 1018009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1019009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1020009b6fcbSJeff Roberson * the VM is ready. 1021009b6fcbSJeff Roberson */ 1022009b6fcbSJeff Roberson static void * 102385dcf349SGleb Smirnoff startup_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait) 1024009b6fcbSJeff Roberson { 1025099a0e58SBosko Milekic uma_keg_t keg; 1026f353d338SAlan Cox uma_slab_t tmps; 1027e9a069d8SJohn Baldwin int pages, check_pages; 1028099a0e58SBosko Milekic 1029e20a199fSJeff Roberson keg = zone_first_keg(zone); 1030e9a069d8SJohn Baldwin pages = howmany(bytes, PAGE_SIZE); 1031e9a069d8SJohn Baldwin check_pages = pages - 1; 1032e9a069d8SJohn Baldwin KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n")); 1033099a0e58SBosko Milekic 1034009b6fcbSJeff Roberson /* 1035009b6fcbSJeff Roberson * Check our small startup cache to see if it has pages remaining. 1036009b6fcbSJeff Roberson */ 1037f353d338SAlan Cox mtx_lock(&uma_boot_pages_mtx); 1038e9a069d8SJohn Baldwin 1039e9a069d8SJohn Baldwin /* First check if we have enough room. */ 1040e9a069d8SJohn Baldwin tmps = LIST_FIRST(&uma_boot_pages); 1041e9a069d8SJohn Baldwin while (tmps != NULL && check_pages-- > 0) 1042e9a069d8SJohn Baldwin tmps = LIST_NEXT(tmps, us_link); 1043e9a069d8SJohn Baldwin if (tmps != NULL) { 1044e9a069d8SJohn Baldwin /* 1045e9a069d8SJohn Baldwin * It's ok to lose tmps references. The last one will 1046e9a069d8SJohn Baldwin * have tmps->us_data pointing to the start address of 1047e9a069d8SJohn Baldwin * "pages" contiguous pages of memory. 1048e9a069d8SJohn Baldwin */ 1049e9a069d8SJohn Baldwin while (pages-- > 0) { 1050e9a069d8SJohn Baldwin tmps = LIST_FIRST(&uma_boot_pages); 1051009b6fcbSJeff Roberson LIST_REMOVE(tmps, us_link); 1052e9a069d8SJohn Baldwin } 1053f353d338SAlan Cox mtx_unlock(&uma_boot_pages_mtx); 1054009b6fcbSJeff Roberson *pflag = tmps->us_flags; 1055009b6fcbSJeff Roberson return (tmps->us_data); 1056009b6fcbSJeff Roberson } 1057f353d338SAlan Cox mtx_unlock(&uma_boot_pages_mtx); 1058342f1793SAlan Cox if (booted < UMA_STARTUP2) 10593803b26bSDag-Erling Smørgrav panic("UMA: Increase vm.boot_pages"); 1060009b6fcbSJeff Roberson /* 1061009b6fcbSJeff Roberson * Now that we've booted reset these users to their real allocator. 1062009b6fcbSJeff Roberson */ 1063009b6fcbSJeff Roberson #ifdef UMA_MD_SMALL_ALLOC 1064e9a069d8SJohn Baldwin keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc; 1065009b6fcbSJeff Roberson #else 1066099a0e58SBosko Milekic keg->uk_allocf = page_alloc; 1067009b6fcbSJeff Roberson #endif 1068099a0e58SBosko Milekic return keg->uk_allocf(zone, bytes, pflag, wait); 1069009b6fcbSJeff Roberson } 1070009b6fcbSJeff Roberson 1071009b6fcbSJeff Roberson /* 10728355f576SJeff Roberson * Allocates a number of pages from the system 10738355f576SJeff Roberson * 10748355f576SJeff Roberson * Arguments: 10758355f576SJeff Roberson * bytes The number of bytes requested 10768355f576SJeff Roberson * wait Shall we wait? 10778355f576SJeff Roberson * 10788355f576SJeff Roberson * Returns: 10798355f576SJeff Roberson * A pointer to the alloced memory or possibly 10808355f576SJeff Roberson * NULL if M_NOWAIT is set. 10818355f576SJeff Roberson */ 10828355f576SJeff Roberson static void * 108385dcf349SGleb Smirnoff page_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait) 10848355f576SJeff Roberson { 10858355f576SJeff Roberson void *p; /* Returned page */ 10868355f576SJeff Roberson 10878355f576SJeff Roberson *pflag = UMA_SLAB_KMEM; 10885df87b21SJeff Roberson p = (void *) kmem_malloc(kmem_arena, bytes, wait); 10898355f576SJeff Roberson 10908355f576SJeff Roberson return (p); 10918355f576SJeff Roberson } 10928355f576SJeff Roberson 10938355f576SJeff Roberson /* 10948355f576SJeff Roberson * Allocates a number of pages from within an object 10958355f576SJeff Roberson * 10968355f576SJeff Roberson * Arguments: 10978355f576SJeff Roberson * bytes The number of bytes requested 10988355f576SJeff Roberson * wait Shall we wait? 10998355f576SJeff Roberson * 11008355f576SJeff Roberson * Returns: 11018355f576SJeff Roberson * A pointer to the alloced memory or possibly 11028355f576SJeff Roberson * NULL if M_NOWAIT is set. 11038355f576SJeff Roberson */ 11048355f576SJeff Roberson static void * 110585dcf349SGleb Smirnoff noobj_alloc(uma_zone_t zone, int bytes, uint8_t *flags, int wait) 11068355f576SJeff Roberson { 1107a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1108a4915c21SAttilio Rao u_long npages; 1109b245ac95SAlan Cox vm_offset_t retkva, zkva; 1110a4915c21SAttilio Rao vm_page_t p, p_next; 1111e20a199fSJeff Roberson uma_keg_t keg; 11128355f576SJeff Roberson 1113a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1114e20a199fSJeff Roberson keg = zone_first_keg(zone); 1115a4915c21SAttilio Rao 1116a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1117a4915c21SAttilio Rao while (npages > 0) { 1118a4915c21SAttilio Rao p = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT | 1119a4915c21SAttilio Rao VM_ALLOC_WIRED | VM_ALLOC_NOOBJ); 1120a4915c21SAttilio Rao if (p != NULL) { 1121a4915c21SAttilio Rao /* 1122a4915c21SAttilio Rao * Since the page does not belong to an object, its 1123a4915c21SAttilio Rao * listq is unused. 1124a4915c21SAttilio Rao */ 1125a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1126a4915c21SAttilio Rao npages--; 1127a4915c21SAttilio Rao continue; 1128a4915c21SAttilio Rao } 1129a4915c21SAttilio Rao if (wait & M_WAITOK) { 1130a4915c21SAttilio Rao VM_WAIT; 1131a4915c21SAttilio Rao continue; 1132a4915c21SAttilio Rao } 11338355f576SJeff Roberson 11348355f576SJeff Roberson /* 1135a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1136a4915c21SAttilio Rao * exit. 11378355f576SJeff Roberson */ 1138a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 1139b245ac95SAlan Cox vm_page_unwire(p, 0); 1140b245ac95SAlan Cox vm_page_free(p); 1141b245ac95SAlan Cox } 1142a4915c21SAttilio Rao return (NULL); 1143b245ac95SAlan Cox } 11448355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1145a4915c21SAttilio Rao zkva = keg->uk_kva + 1146a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1147a4915c21SAttilio Rao retkva = zkva; 1148a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1149a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1150a4915c21SAttilio Rao zkva += PAGE_SIZE; 1151a4915c21SAttilio Rao } 11528355f576SJeff Roberson 11538355f576SJeff Roberson return ((void *)retkva); 11548355f576SJeff Roberson } 11558355f576SJeff Roberson 11568355f576SJeff Roberson /* 11578355f576SJeff Roberson * Frees a number of pages to the system 11588355f576SJeff Roberson * 11598355f576SJeff Roberson * Arguments: 11608355f576SJeff Roberson * mem A pointer to the memory to be freed 11618355f576SJeff Roberson * size The size of the memory being freed 11628355f576SJeff Roberson * flags The original p->us_flags field 11638355f576SJeff Roberson * 11648355f576SJeff Roberson * Returns: 11658355f576SJeff Roberson * Nothing 11668355f576SJeff Roberson */ 11678355f576SJeff Roberson static void 116885dcf349SGleb Smirnoff page_free(void *mem, int size, uint8_t flags) 11698355f576SJeff Roberson { 11705df87b21SJeff Roberson struct vmem *vmem; 11713370c5bfSJeff Roberson 11728355f576SJeff Roberson if (flags & UMA_SLAB_KMEM) 11735df87b21SJeff Roberson vmem = kmem_arena; 1174aea6e893SAlan Cox else if (flags & UMA_SLAB_KERNEL) 11755df87b21SJeff Roberson vmem = kernel_arena; 11768355f576SJeff Roberson else 1177aea6e893SAlan Cox panic("UMA: page_free used with invalid flags %d", flags); 11788355f576SJeff Roberson 11795df87b21SJeff Roberson kmem_free(vmem, (vm_offset_t)mem, size); 11808355f576SJeff Roberson } 11818355f576SJeff Roberson 11828355f576SJeff Roberson /* 11838355f576SJeff Roberson * Zero fill initializer 11848355f576SJeff Roberson * 11858355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 11868355f576SJeff Roberson */ 1187b23f72e9SBrian Feldman static int 1188b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 11898355f576SJeff Roberson { 11908355f576SJeff Roberson bzero(mem, size); 1191b23f72e9SBrian Feldman return (0); 11928355f576SJeff Roberson } 11938355f576SJeff Roberson 11948355f576SJeff Roberson /* 1195e20a199fSJeff Roberson * Finish creating a small uma keg. This calculates ipers, and the keg size. 11968355f576SJeff Roberson * 11978355f576SJeff Roberson * Arguments 1198e20a199fSJeff Roberson * keg The zone we should initialize 11998355f576SJeff Roberson * 12008355f576SJeff Roberson * Returns 12018355f576SJeff Roberson * Nothing 12028355f576SJeff Roberson */ 12038355f576SJeff Roberson static void 1204e20a199fSJeff Roberson keg_small_init(uma_keg_t keg) 12058355f576SJeff Roberson { 1206244f4554SBosko Milekic u_int rsize; 1207244f4554SBosko Milekic u_int memused; 1208244f4554SBosko Milekic u_int wastedspace; 1209244f4554SBosko Milekic u_int shsize; 12108355f576SJeff Roberson 1211ad97af7eSGleb Smirnoff if (keg->uk_flags & UMA_ZONE_PCPU) { 1212e28a647dSGleb Smirnoff u_int ncpus = mp_ncpus ? mp_ncpus : MAXCPU; 1213e28a647dSGleb Smirnoff 1214ad97af7eSGleb Smirnoff keg->uk_slabsize = sizeof(struct pcpu); 1215e28a647dSGleb Smirnoff keg->uk_ppera = howmany(ncpus * sizeof(struct pcpu), 1216ad97af7eSGleb Smirnoff PAGE_SIZE); 1217ad97af7eSGleb Smirnoff } else { 1218ad97af7eSGleb Smirnoff keg->uk_slabsize = UMA_SLAB_SIZE; 1219ad97af7eSGleb Smirnoff keg->uk_ppera = 1; 1220ad97af7eSGleb Smirnoff } 1221ad97af7eSGleb Smirnoff 1222ef72505eSJeff Roberson /* 1223ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1224ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1225ef72505eSJeff Roberson * allocation bits for we round it up. 1226ef72505eSJeff Roberson */ 1227099a0e58SBosko Milekic rsize = keg->uk_size; 1228ef72505eSJeff Roberson if (rsize < keg->uk_slabsize / SLAB_SETSIZE) 1229ef72505eSJeff Roberson rsize = keg->uk_slabsize / SLAB_SETSIZE; 1230099a0e58SBosko Milekic if (rsize & keg->uk_align) 1231099a0e58SBosko Milekic rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1); 1232099a0e58SBosko Milekic keg->uk_rsize = rsize; 1233ad97af7eSGleb Smirnoff 1234ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 1235ad97af7eSGleb Smirnoff keg->uk_rsize < sizeof(struct pcpu), 1236ad97af7eSGleb Smirnoff ("%s: size %u too large", __func__, keg->uk_rsize)); 12378355f576SJeff Roberson 1238ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_REFCNT) 1239ef72505eSJeff Roberson rsize += sizeof(uint32_t); 1240ef72505eSJeff Roberson 1241ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_OFFPAGE) 12422864dbbfSGleb Smirnoff shsize = 0; 1243ef72505eSJeff Roberson else 1244244f4554SBosko Milekic shsize = sizeof(struct uma_slab); 12458355f576SJeff Roberson 1246ad97af7eSGleb Smirnoff keg->uk_ipers = (keg->uk_slabsize - shsize) / rsize; 1247ef72505eSJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE, 1248ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 1249ad97af7eSGleb Smirnoff 1250244f4554SBosko Milekic memused = keg->uk_ipers * rsize + shsize; 1251ad97af7eSGleb Smirnoff wastedspace = keg->uk_slabsize - memused; 1252244f4554SBosko Milekic 125320e8e865SBosko Milekic /* 1254244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 125520e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 12566fd34d6fSJeff Roberson * may end up going to the VM for slabs which we do not 12576fd34d6fSJeff Roberson * want to do if we're UMA_ZFLAG_CACHEONLY as a result 12586fd34d6fSJeff Roberson * of UMA_ZONE_VM, which clearly forbids it. 125920e8e865SBosko Milekic */ 1260099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) || 1261099a0e58SBosko Milekic (keg->uk_flags & UMA_ZFLAG_CACHEONLY)) 12628355f576SJeff Roberson return; 1263244f4554SBosko Milekic 1264ef72505eSJeff Roberson /* 1265ef72505eSJeff Roberson * See if using an OFFPAGE slab will limit our waste. Only do 1266ef72505eSJeff Roberson * this if it permits more items per-slab. 1267ef72505eSJeff Roberson * 1268ef72505eSJeff Roberson * XXX We could try growing slabsize to limit max waste as well. 1269ef72505eSJeff Roberson * Historically this was not done because the VM could not 1270ef72505eSJeff Roberson * efficiently handle contiguous allocations. 1271ef72505eSJeff Roberson */ 1272ad97af7eSGleb Smirnoff if ((wastedspace >= keg->uk_slabsize / UMA_MAX_WASTE) && 1273ad97af7eSGleb Smirnoff (keg->uk_ipers < (keg->uk_slabsize / keg->uk_rsize))) { 1274ad97af7eSGleb Smirnoff keg->uk_ipers = keg->uk_slabsize / keg->uk_rsize; 1275ef72505eSJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE, 1276ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 1277244f4554SBosko Milekic #ifdef UMA_DEBUG 1278244f4554SBosko Milekic printf("UMA decided we need offpage slab headers for " 1279e20a199fSJeff Roberson "keg: %s, calculated wastedspace = %d, " 1280244f4554SBosko Milekic "maximum wasted space allowed = %d, " 1281244f4554SBosko Milekic "calculated ipers = %d, " 1282e20a199fSJeff Roberson "new wasted space = %d\n", keg->uk_name, wastedspace, 1283ad97af7eSGleb Smirnoff keg->uk_slabsize / UMA_MAX_WASTE, keg->uk_ipers, 1284ad97af7eSGleb Smirnoff keg->uk_slabsize - keg->uk_ipers * keg->uk_rsize); 1285244f4554SBosko Milekic #endif 1286099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_OFFPAGE; 12878355f576SJeff Roberson } 1288ad97af7eSGleb Smirnoff 1289ad97af7eSGleb Smirnoff if ((keg->uk_flags & UMA_ZONE_OFFPAGE) && 1290ad97af7eSGleb Smirnoff (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1291ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_HASH; 12928355f576SJeff Roberson } 12938355f576SJeff Roberson 12948355f576SJeff Roberson /* 1295e20a199fSJeff Roberson * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do 12968355f576SJeff Roberson * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be 12978355f576SJeff Roberson * more complicated. 12988355f576SJeff Roberson * 12998355f576SJeff Roberson * Arguments 1300e20a199fSJeff Roberson * keg The keg we should initialize 13018355f576SJeff Roberson * 13028355f576SJeff Roberson * Returns 13038355f576SJeff Roberson * Nothing 13048355f576SJeff Roberson */ 13058355f576SJeff Roberson static void 1306e20a199fSJeff Roberson keg_large_init(uma_keg_t keg) 13078355f576SJeff Roberson { 13088355f576SJeff Roberson 1309e20a199fSJeff Roberson KASSERT(keg != NULL, ("Keg is null in keg_large_init")); 1310099a0e58SBosko Milekic KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0, 1311e20a199fSJeff Roberson ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg")); 1312ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1313ad97af7eSGleb Smirnoff ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__)); 131420e8e865SBosko Milekic 1315ad97af7eSGleb Smirnoff keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE); 1316ad97af7eSGleb Smirnoff keg->uk_slabsize = keg->uk_ppera * PAGE_SIZE; 1317099a0e58SBosko Milekic keg->uk_ipers = 1; 1318e9a069d8SJohn Baldwin keg->uk_rsize = keg->uk_size; 1319e9a069d8SJohn Baldwin 1320e9a069d8SJohn Baldwin /* We can't do OFFPAGE if we're internal, bail out here. */ 1321e9a069d8SJohn Baldwin if (keg->uk_flags & UMA_ZFLAG_INTERNAL) 1322e9a069d8SJohn Baldwin return; 13238355f576SJeff Roberson 1324099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_OFFPAGE; 1325e20a199fSJeff Roberson if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1326099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_HASH; 13278355f576SJeff Roberson } 13288355f576SJeff Roberson 1329e20a199fSJeff Roberson static void 1330e20a199fSJeff Roberson keg_cachespread_init(uma_keg_t keg) 1331e20a199fSJeff Roberson { 1332e20a199fSJeff Roberson int alignsize; 1333e20a199fSJeff Roberson int trailer; 1334e20a199fSJeff Roberson int pages; 1335e20a199fSJeff Roberson int rsize; 1336e20a199fSJeff Roberson 1337ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1338ad97af7eSGleb Smirnoff ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__)); 1339ad97af7eSGleb Smirnoff 1340e20a199fSJeff Roberson alignsize = keg->uk_align + 1; 1341e20a199fSJeff Roberson rsize = keg->uk_size; 1342e20a199fSJeff Roberson /* 1343e20a199fSJeff Roberson * We want one item to start on every align boundary in a page. To 1344e20a199fSJeff Roberson * do this we will span pages. We will also extend the item by the 1345e20a199fSJeff Roberson * size of align if it is an even multiple of align. Otherwise, it 1346e20a199fSJeff Roberson * would fall on the same boundary every time. 1347e20a199fSJeff Roberson */ 1348e20a199fSJeff Roberson if (rsize & keg->uk_align) 1349e20a199fSJeff Roberson rsize = (rsize & ~keg->uk_align) + alignsize; 1350e20a199fSJeff Roberson if ((rsize & alignsize) == 0) 1351e20a199fSJeff Roberson rsize += alignsize; 1352e20a199fSJeff Roberson trailer = rsize - keg->uk_size; 1353e20a199fSJeff Roberson pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE; 1354e20a199fSJeff Roberson pages = MIN(pages, (128 * 1024) / PAGE_SIZE); 1355e20a199fSJeff Roberson keg->uk_rsize = rsize; 1356e20a199fSJeff Roberson keg->uk_ppera = pages; 1357ad97af7eSGleb Smirnoff keg->uk_slabsize = UMA_SLAB_SIZE; 1358e20a199fSJeff Roberson keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize; 1359e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 1360e20a199fSJeff Roberson KASSERT(keg->uk_ipers <= uma_max_ipers, 136142321809SGleb Smirnoff ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__, 1362e20a199fSJeff Roberson keg->uk_ipers)); 1363e20a199fSJeff Roberson } 1364e20a199fSJeff Roberson 13658355f576SJeff Roberson /* 1366099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 1367099a0e58SBosko Milekic * the keg onto the global keg list. 13688355f576SJeff Roberson * 13698355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 1370099a0e58SBosko Milekic * udata Actually uma_kctor_args 1371099a0e58SBosko Milekic */ 1372b23f72e9SBrian Feldman static int 1373b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 1374099a0e58SBosko Milekic { 1375099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 1376099a0e58SBosko Milekic uma_keg_t keg = mem; 1377099a0e58SBosko Milekic uma_zone_t zone; 1378099a0e58SBosko Milekic 1379099a0e58SBosko Milekic bzero(keg, size); 1380099a0e58SBosko Milekic keg->uk_size = arg->size; 1381099a0e58SBosko Milekic keg->uk_init = arg->uminit; 1382099a0e58SBosko Milekic keg->uk_fini = arg->fini; 1383099a0e58SBosko Milekic keg->uk_align = arg->align; 1384099a0e58SBosko Milekic keg->uk_free = 0; 13856fd34d6fSJeff Roberson keg->uk_reserve = 0; 1386099a0e58SBosko Milekic keg->uk_pages = 0; 1387099a0e58SBosko Milekic keg->uk_flags = arg->flags; 1388099a0e58SBosko Milekic keg->uk_allocf = page_alloc; 1389099a0e58SBosko Milekic keg->uk_freef = page_free; 1390099a0e58SBosko Milekic keg->uk_slabzone = NULL; 1391099a0e58SBosko Milekic 1392099a0e58SBosko Milekic /* 1393099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 1394099a0e58SBosko Milekic */ 1395099a0e58SBosko Milekic zone = arg->zone; 1396e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 1397099a0e58SBosko Milekic 1398099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_VM) 1399099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 1400099a0e58SBosko Milekic 1401099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 1402099a0e58SBosko Milekic keg->uk_init = zero_init; 1403099a0e58SBosko Milekic 1404e20a199fSJeff Roberson if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC) 1405e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_VTOSLAB; 1406e20a199fSJeff Roberson 1407ad97af7eSGleb Smirnoff if (arg->flags & UMA_ZONE_PCPU) 1408ad97af7eSGleb Smirnoff #ifdef SMP 1409ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_OFFPAGE; 1410ad97af7eSGleb Smirnoff #else 1411ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 1412ad97af7eSGleb Smirnoff #endif 1413ad97af7eSGleb Smirnoff 1414ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_CACHESPREAD) { 1415e20a199fSJeff Roberson keg_cachespread_init(keg); 1416ef72505eSJeff Roberson } else if (keg->uk_flags & UMA_ZONE_REFCNT) { 1417ef72505eSJeff Roberson if (keg->uk_size > 1418ef72505eSJeff Roberson (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) - 1419ef72505eSJeff Roberson sizeof(uint32_t))) 1420e20a199fSJeff Roberson keg_large_init(keg); 1421099a0e58SBosko Milekic else 1422e20a199fSJeff Roberson keg_small_init(keg); 1423244f4554SBosko Milekic } else { 1424ef72505eSJeff Roberson if (keg->uk_size > (UMA_SLAB_SIZE - sizeof(struct uma_slab))) 1425e20a199fSJeff Roberson keg_large_init(keg); 1426244f4554SBosko Milekic else 1427e20a199fSJeff Roberson keg_small_init(keg); 1428244f4554SBosko Milekic } 1429099a0e58SBosko Milekic 1430244f4554SBosko Milekic if (keg->uk_flags & UMA_ZONE_OFFPAGE) { 1431ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_REFCNT) { 1432ef72505eSJeff Roberson if (keg->uk_ipers > uma_max_ipers_ref) 1433ef72505eSJeff Roberson panic("Too many ref items per zone: %d > %d\n", 1434ef72505eSJeff Roberson keg->uk_ipers, uma_max_ipers_ref); 1435099a0e58SBosko Milekic keg->uk_slabzone = slabrefzone; 1436ef72505eSJeff Roberson } else 1437099a0e58SBosko Milekic keg->uk_slabzone = slabzone; 1438244f4554SBosko Milekic } 1439099a0e58SBosko Milekic 1440099a0e58SBosko Milekic /* 1441099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 1442099a0e58SBosko Milekic * startup cache until the vm is ready. 1443099a0e58SBosko Milekic */ 1444099a0e58SBosko Milekic if (keg->uk_ppera == 1) { 1445099a0e58SBosko Milekic #ifdef UMA_MD_SMALL_ALLOC 1446099a0e58SBosko Milekic keg->uk_allocf = uma_small_alloc; 1447099a0e58SBosko Milekic keg->uk_freef = uma_small_free; 14488cd02d00SAlan Cox 1449342f1793SAlan Cox if (booted < UMA_STARTUP) 1450099a0e58SBosko Milekic keg->uk_allocf = startup_alloc; 14518cd02d00SAlan Cox #else 14528cd02d00SAlan Cox if (booted < UMA_STARTUP2) 14538cd02d00SAlan Cox keg->uk_allocf = startup_alloc; 14548cd02d00SAlan Cox #endif 1455342f1793SAlan Cox } else if (booted < UMA_STARTUP2 && 1456342f1793SAlan Cox (keg->uk_flags & UMA_ZFLAG_INTERNAL)) 1457e9a069d8SJohn Baldwin keg->uk_allocf = startup_alloc; 1458099a0e58SBosko Milekic 1459099a0e58SBosko Milekic /* 1460af526374SJeff Roberson * Initialize keg's lock 1461099a0e58SBosko Milekic */ 1462af526374SJeff Roberson KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS)); 1463099a0e58SBosko Milekic 1464099a0e58SBosko Milekic /* 1465099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 1466099a0e58SBosko Milekic * figure out where in each page it goes. This calculates a right 1467099a0e58SBosko Milekic * justified offset into the memory on an ALIGN_PTR boundary. 1468099a0e58SBosko Milekic */ 1469099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) { 1470244f4554SBosko Milekic u_int totsize; 1471099a0e58SBosko Milekic 1472099a0e58SBosko Milekic /* Size of the slab struct and free list */ 1473ef72505eSJeff Roberson totsize = sizeof(struct uma_slab); 1474ef72505eSJeff Roberson 1475ef72505eSJeff Roberson /* Size of the reference counts. */ 1476244f4554SBosko Milekic if (keg->uk_flags & UMA_ZONE_REFCNT) 1477ef72505eSJeff Roberson totsize += keg->uk_ipers * sizeof(uint32_t); 1478244f4554SBosko Milekic 1479099a0e58SBosko Milekic if (totsize & UMA_ALIGN_PTR) 1480099a0e58SBosko Milekic totsize = (totsize & ~UMA_ALIGN_PTR) + 1481099a0e58SBosko Milekic (UMA_ALIGN_PTR + 1); 1482ad97af7eSGleb Smirnoff keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize; 1483244f4554SBosko Milekic 1484244f4554SBosko Milekic /* 1485244f4554SBosko Milekic * The only way the following is possible is if with our 1486244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 1487244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 1488244f4554SBosko Milekic * mathematically possible for all cases, so we make 1489244f4554SBosko Milekic * sure here anyway. 1490244f4554SBosko Milekic */ 1491ef72505eSJeff Roberson totsize = keg->uk_pgoff + sizeof(struct uma_slab); 1492ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_REFCNT) 1493ef72505eSJeff Roberson totsize += keg->uk_ipers * sizeof(uint32_t); 1494ad97af7eSGleb Smirnoff if (totsize > PAGE_SIZE * keg->uk_ppera) { 1495099a0e58SBosko Milekic printf("zone %s ipers %d rsize %d size %d\n", 1496099a0e58SBosko Milekic zone->uz_name, keg->uk_ipers, keg->uk_rsize, 1497099a0e58SBosko Milekic keg->uk_size); 1498aea6e893SAlan Cox panic("UMA slab won't fit."); 1499099a0e58SBosko Milekic } 1500099a0e58SBosko Milekic } 1501099a0e58SBosko Milekic 1502099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 1503099a0e58SBosko Milekic hash_alloc(&keg->uk_hash); 1504099a0e58SBosko Milekic 1505099a0e58SBosko Milekic #ifdef UMA_DEBUG 15060b80c1e4SEitan Adler printf("UMA: %s(%p) size %d(%d) flags %#x ipers %d ppera %d out %d free %d\n", 1507e20a199fSJeff Roberson zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags, 1508e20a199fSJeff Roberson keg->uk_ipers, keg->uk_ppera, 1509e20a199fSJeff Roberson (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free); 1510099a0e58SBosko Milekic #endif 1511099a0e58SBosko Milekic 1512099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 1513099a0e58SBosko Milekic 1514099a0e58SBosko Milekic mtx_lock(&uma_mtx); 1515099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 1516099a0e58SBosko Milekic mtx_unlock(&uma_mtx); 1517b23f72e9SBrian Feldman return (0); 1518099a0e58SBosko Milekic } 1519099a0e58SBosko Milekic 1520099a0e58SBosko Milekic /* 1521099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 1522099a0e58SBosko Milekic * 1523099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 1524099a0e58SBosko Milekic * udata Actually uma_zctor_args 15258355f576SJeff Roberson */ 1526b23f72e9SBrian Feldman static int 1527b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 15288355f576SJeff Roberson { 15298355f576SJeff Roberson struct uma_zctor_args *arg = udata; 15308355f576SJeff Roberson uma_zone_t zone = mem; 1531099a0e58SBosko Milekic uma_zone_t z; 1532099a0e58SBosko Milekic uma_keg_t keg; 15338355f576SJeff Roberson 15348355f576SJeff Roberson bzero(zone, size); 15358355f576SJeff Roberson zone->uz_name = arg->name; 15368355f576SJeff Roberson zone->uz_ctor = arg->ctor; 15378355f576SJeff Roberson zone->uz_dtor = arg->dtor; 1538e20a199fSJeff Roberson zone->uz_slab = zone_fetch_slab; 1539099a0e58SBosko Milekic zone->uz_init = NULL; 1540099a0e58SBosko Milekic zone->uz_fini = NULL; 1541099a0e58SBosko Milekic zone->uz_allocs = 0; 1542773df9abSRobert Watson zone->uz_frees = 0; 15432019094aSRobert Watson zone->uz_fails = 0; 1544bf965959SSean Bruno zone->uz_sleeps = 0; 1545fc03d22bSJeff Roberson zone->uz_count = 0; 1546ace66b56SAlexander Motin zone->uz_count_min = 0; 1547e20a199fSJeff Roberson zone->uz_flags = 0; 15482f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 15492f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 1550e20a199fSJeff Roberson keg = arg->keg; 1551099a0e58SBosko Milekic 1552af526374SJeff Roberson ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 1553af526374SJeff Roberson 15540095a784SJeff Roberson /* 15550095a784SJeff Roberson * This is a pure cache zone, no kegs. 15560095a784SJeff Roberson */ 15570095a784SJeff Roberson if (arg->import) { 15586fd34d6fSJeff Roberson if (arg->flags & UMA_ZONE_VM) 15596fd34d6fSJeff Roberson arg->flags |= UMA_ZFLAG_CACHEONLY; 15606fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 1561af526374SJeff Roberson zone->uz_size = arg->size; 15620095a784SJeff Roberson zone->uz_import = arg->import; 15630095a784SJeff Roberson zone->uz_release = arg->release; 15640095a784SJeff Roberson zone->uz_arg = arg->arg; 1565af526374SJeff Roberson zone->uz_lockptr = &zone->uz_lock; 1566af526374SJeff Roberson goto out; 15670095a784SJeff Roberson } 15680095a784SJeff Roberson 15690095a784SJeff Roberson /* 15700095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 15710095a784SJeff Roberson */ 15720095a784SJeff Roberson zone->uz_import = (uma_import)zone_import; 15730095a784SJeff Roberson zone->uz_release = (uma_release)zone_release; 15740095a784SJeff Roberson zone->uz_arg = zone; 15750095a784SJeff Roberson 1576099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 1577099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 15788355f576SJeff Roberson zone->uz_init = arg->uminit; 1579e221e841SJeff Roberson zone->uz_fini = arg->fini; 1580af526374SJeff Roberson zone->uz_lockptr = &keg->uk_lock; 1581e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 15828355f576SJeff Roberson mtx_lock(&uma_mtx); 1583099a0e58SBosko Milekic ZONE_LOCK(zone); 1584099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 1585099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 1586099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 1587099a0e58SBosko Milekic break; 1588099a0e58SBosko Milekic } 1589099a0e58SBosko Milekic } 1590099a0e58SBosko Milekic ZONE_UNLOCK(zone); 15918355f576SJeff Roberson mtx_unlock(&uma_mtx); 1592e20a199fSJeff Roberson } else if (keg == NULL) { 1593e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 1594e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 1595b23f72e9SBrian Feldman return (ENOMEM); 1596099a0e58SBosko Milekic } else { 1597099a0e58SBosko Milekic struct uma_kctor_args karg; 1598b23f72e9SBrian Feldman int error; 1599099a0e58SBosko Milekic 1600099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 1601099a0e58SBosko Milekic karg.size = arg->size; 1602099a0e58SBosko Milekic karg.uminit = arg->uminit; 1603099a0e58SBosko Milekic karg.fini = arg->fini; 1604099a0e58SBosko Milekic karg.align = arg->align; 1605099a0e58SBosko Milekic karg.flags = arg->flags; 1606099a0e58SBosko Milekic karg.zone = zone; 1607b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 1608b23f72e9SBrian Feldman flags); 1609b23f72e9SBrian Feldman if (error) 1610b23f72e9SBrian Feldman return (error); 1611099a0e58SBosko Milekic } 16120095a784SJeff Roberson 1613e20a199fSJeff Roberson /* 1614e20a199fSJeff Roberson * Link in the first keg. 1615e20a199fSJeff Roberson */ 1616e20a199fSJeff Roberson zone->uz_klink.kl_keg = keg; 1617e20a199fSJeff Roberson LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link); 1618af526374SJeff Roberson zone->uz_lockptr = &keg->uk_lock; 1619e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 1620e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 1621e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 16228355f576SJeff Roberson 16238355f576SJeff Roberson /* 16248355f576SJeff Roberson * Some internal zones don't have room allocated for the per cpu 16258355f576SJeff Roberson * caches. If we're internal, bail out here. 16268355f576SJeff Roberson */ 1627099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZFLAG_INTERNAL) { 1628e20a199fSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 1629099a0e58SBosko Milekic ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 1630b23f72e9SBrian Feldman return (0); 1631099a0e58SBosko Milekic } 16328355f576SJeff Roberson 1633af526374SJeff Roberson out: 1634af526374SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) == 0) 1635af526374SJeff Roberson zone->uz_count = bucket_select(zone->uz_size); 16368355f576SJeff Roberson else 1637cae33c14SJeff Roberson zone->uz_count = BUCKET_MAX; 1638ace66b56SAlexander Motin zone->uz_count_min = zone->uz_count; 1639fc03d22bSJeff Roberson 1640b23f72e9SBrian Feldman return (0); 16418355f576SJeff Roberson } 16428355f576SJeff Roberson 16438355f576SJeff Roberson /* 1644099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 1645099a0e58SBosko Milekic * table and removes the keg from the global list. 16469c2cd7e5SJeff Roberson * 16479c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 16489c2cd7e5SJeff Roberson * udata unused 16499c2cd7e5SJeff Roberson */ 1650099a0e58SBosko Milekic static void 1651099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 1652099a0e58SBosko Milekic { 1653099a0e58SBosko Milekic uma_keg_t keg; 16549c2cd7e5SJeff Roberson 1655099a0e58SBosko Milekic keg = (uma_keg_t)arg; 1656e20a199fSJeff Roberson KEG_LOCK(keg); 1657099a0e58SBosko Milekic if (keg->uk_free != 0) { 1658099a0e58SBosko Milekic printf("Freed UMA keg was not empty (%d items). " 1659099a0e58SBosko Milekic " Lost %d pages of memory.\n", 1660099a0e58SBosko Milekic keg->uk_free, keg->uk_pages); 1661099a0e58SBosko Milekic } 1662e20a199fSJeff Roberson KEG_UNLOCK(keg); 1663099a0e58SBosko Milekic 1664099a0e58SBosko Milekic hash_free(&keg->uk_hash); 1665099a0e58SBosko Milekic 1666e20a199fSJeff Roberson KEG_LOCK_FINI(keg); 1667099a0e58SBosko Milekic } 1668099a0e58SBosko Milekic 1669099a0e58SBosko Milekic /* 1670099a0e58SBosko Milekic * Zone header dtor. 1671099a0e58SBosko Milekic * 1672099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 1673099a0e58SBosko Milekic * udata unused 1674099a0e58SBosko Milekic */ 16759c2cd7e5SJeff Roberson static void 16769c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 16779c2cd7e5SJeff Roberson { 1678e20a199fSJeff Roberson uma_klink_t klink; 16799c2cd7e5SJeff Roberson uma_zone_t zone; 1680099a0e58SBosko Milekic uma_keg_t keg; 16819c2cd7e5SJeff Roberson 16829c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 1683e20a199fSJeff Roberson keg = zone_first_keg(zone); 16849643769aSJeff Roberson 1685e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 16869643769aSJeff Roberson cache_drain(zone); 1687099a0e58SBosko Milekic 168817b9cc49SJeff Roberson mtx_lock(&uma_mtx); 1689099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 1690e20a199fSJeff Roberson mtx_unlock(&uma_mtx); 1691099a0e58SBosko Milekic /* 1692099a0e58SBosko Milekic * XXX there are some races here where 1693099a0e58SBosko Milekic * the zone can be drained but zone lock 1694099a0e58SBosko Milekic * released and then refilled before we 1695099a0e58SBosko Milekic * remove it... we dont care for now 1696099a0e58SBosko Milekic */ 1697e20a199fSJeff Roberson zone_drain_wait(zone, M_WAITOK); 1698e20a199fSJeff Roberson /* 1699e20a199fSJeff Roberson * Unlink all of our kegs. 1700e20a199fSJeff Roberson */ 1701e20a199fSJeff Roberson while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) { 1702e20a199fSJeff Roberson klink->kl_keg = NULL; 1703e20a199fSJeff Roberson LIST_REMOVE(klink, kl_link); 1704e20a199fSJeff Roberson if (klink == &zone->uz_klink) 1705e20a199fSJeff Roberson continue; 1706e20a199fSJeff Roberson free(klink, M_TEMP); 1707e20a199fSJeff Roberson } 1708e20a199fSJeff Roberson /* 1709e20a199fSJeff Roberson * We only destroy kegs from non secondary zones. 1710e20a199fSJeff Roberson */ 17110095a784SJeff Roberson if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0) { 1712e20a199fSJeff Roberson mtx_lock(&uma_mtx); 1713099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 1714099a0e58SBosko Milekic mtx_unlock(&uma_mtx); 17150095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 17169c2cd7e5SJeff Roberson } 1717af526374SJeff Roberson ZONE_LOCK_FINI(zone); 1718099a0e58SBosko Milekic } 1719099a0e58SBosko Milekic 17209c2cd7e5SJeff Roberson /* 17218355f576SJeff Roberson * Traverses every zone in the system and calls a callback 17228355f576SJeff Roberson * 17238355f576SJeff Roberson * Arguments: 17248355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 17258355f576SJeff Roberson * as an argument. 17268355f576SJeff Roberson * 17278355f576SJeff Roberson * Returns: 17288355f576SJeff Roberson * Nothing 17298355f576SJeff Roberson */ 17308355f576SJeff Roberson static void 17318355f576SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t)) 17328355f576SJeff Roberson { 1733099a0e58SBosko Milekic uma_keg_t keg; 17348355f576SJeff Roberson uma_zone_t zone; 17358355f576SJeff Roberson 17368355f576SJeff Roberson mtx_lock(&uma_mtx); 1737099a0e58SBosko Milekic LIST_FOREACH(keg, &uma_kegs, uk_link) { 1738099a0e58SBosko Milekic LIST_FOREACH(zone, &keg->uk_zones, uz_link) 17398355f576SJeff Roberson zfunc(zone); 1740099a0e58SBosko Milekic } 17418355f576SJeff Roberson mtx_unlock(&uma_mtx); 17428355f576SJeff Roberson } 17438355f576SJeff Roberson 17448355f576SJeff Roberson /* Public functions */ 17458355f576SJeff Roberson /* See uma.h */ 17468355f576SJeff Roberson void 17473803b26bSDag-Erling Smørgrav uma_startup(void *bootmem, int boot_pages) 17488355f576SJeff Roberson { 17498355f576SJeff Roberson struct uma_zctor_args args; 17508355f576SJeff Roberson uma_slab_t slab; 1751244f4554SBosko Milekic u_int slabsize; 17528355f576SJeff Roberson int i; 17538355f576SJeff Roberson 17548355f576SJeff Roberson #ifdef UMA_DEBUG 1755099a0e58SBosko Milekic printf("Creating uma keg headers zone and keg.\n"); 17568355f576SJeff Roberson #endif 1757f353d338SAlan Cox mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF); 1758099a0e58SBosko Milekic 1759099a0e58SBosko Milekic /* "manually" create the initial zone */ 17600095a784SJeff Roberson memset(&args, 0, sizeof(args)); 1761099a0e58SBosko Milekic args.name = "UMA Kegs"; 1762099a0e58SBosko Milekic args.size = sizeof(struct uma_keg); 1763099a0e58SBosko Milekic args.ctor = keg_ctor; 1764099a0e58SBosko Milekic args.dtor = keg_dtor; 17658355f576SJeff Roberson args.uminit = zero_init; 17668355f576SJeff Roberson args.fini = NULL; 1767099a0e58SBosko Milekic args.keg = &masterkeg; 17688355f576SJeff Roberson args.align = 32 - 1; 1769b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 17708355f576SJeff Roberson /* The initial zone has no Per cpu queues so it's smaller */ 1771b23f72e9SBrian Feldman zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK); 17728355f576SJeff Roberson 17738355f576SJeff Roberson #ifdef UMA_DEBUG 17748355f576SJeff Roberson printf("Filling boot free list.\n"); 17758355f576SJeff Roberson #endif 17763803b26bSDag-Erling Smørgrav for (i = 0; i < boot_pages; i++) { 177785dcf349SGleb Smirnoff slab = (uma_slab_t)((uint8_t *)bootmem + (i * UMA_SLAB_SIZE)); 177885dcf349SGleb Smirnoff slab->us_data = (uint8_t *)slab; 17798355f576SJeff Roberson slab->us_flags = UMA_SLAB_BOOT; 17808355f576SJeff Roberson LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link); 17818355f576SJeff Roberson } 1782f353d338SAlan Cox mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF); 17838355f576SJeff Roberson 17848355f576SJeff Roberson #ifdef UMA_DEBUG 1785099a0e58SBosko Milekic printf("Creating uma zone headers zone and keg.\n"); 1786099a0e58SBosko Milekic #endif 1787099a0e58SBosko Milekic args.name = "UMA Zones"; 1788099a0e58SBosko Milekic args.size = sizeof(struct uma_zone) + 1789099a0e58SBosko Milekic (sizeof(struct uma_cache) * (mp_maxid + 1)); 1790099a0e58SBosko Milekic args.ctor = zone_ctor; 1791099a0e58SBosko Milekic args.dtor = zone_dtor; 1792099a0e58SBosko Milekic args.uminit = zero_init; 1793099a0e58SBosko Milekic args.fini = NULL; 1794099a0e58SBosko Milekic args.keg = NULL; 1795099a0e58SBosko Milekic args.align = 32 - 1; 1796099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 1797099a0e58SBosko Milekic /* The initial zone has no Per cpu queues so it's smaller */ 1798b23f72e9SBrian Feldman zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK); 1799099a0e58SBosko Milekic 1800099a0e58SBosko Milekic #ifdef UMA_DEBUG 1801099a0e58SBosko Milekic printf("Initializing pcpu cache locks.\n"); 1802099a0e58SBosko Milekic #endif 1803099a0e58SBosko Milekic #ifdef UMA_DEBUG 1804099a0e58SBosko Milekic printf("Creating slab and hash zones.\n"); 18058355f576SJeff Roberson #endif 18068355f576SJeff Roberson 18078355f576SJeff Roberson /* Now make a zone for slab headers */ 18088355f576SJeff Roberson slabzone = uma_zcreate("UMA Slabs", 1809ef72505eSJeff Roberson sizeof(struct uma_slab), 18108355f576SJeff Roberson NULL, NULL, NULL, NULL, 1811b60f5b79SJeff Roberson UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 18128355f576SJeff Roberson 1813099a0e58SBosko Milekic /* 1814099a0e58SBosko Milekic * We also create a zone for the bigger slabs with reference 1815099a0e58SBosko Milekic * counts in them, to accomodate UMA_ZONE_REFCNT zones. 1816099a0e58SBosko Milekic */ 1817ef72505eSJeff Roberson slabsize = sizeof(struct uma_slab_refcnt); 1818ef72505eSJeff Roberson slabsize += uma_max_ipers_ref * sizeof(uint32_t); 1819099a0e58SBosko Milekic slabrefzone = uma_zcreate("UMA RCntSlabs", 1820099a0e58SBosko Milekic slabsize, 1821099a0e58SBosko Milekic NULL, NULL, NULL, NULL, 1822e66468eaSBosko Milekic UMA_ALIGN_PTR, 18237fd87882SBosko Milekic UMA_ZFLAG_INTERNAL); 1824099a0e58SBosko Milekic 18258355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 18268355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 18278355f576SJeff Roberson NULL, NULL, NULL, NULL, 1828b60f5b79SJeff Roberson UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 18298355f576SJeff Roberson 1830cae33c14SJeff Roberson bucket_init(); 18318355f576SJeff Roberson 1832342f1793SAlan Cox booted = UMA_STARTUP; 18338355f576SJeff Roberson 18348355f576SJeff Roberson #ifdef UMA_DEBUG 18358355f576SJeff Roberson printf("UMA startup complete.\n"); 18368355f576SJeff Roberson #endif 18378355f576SJeff Roberson } 18388355f576SJeff Roberson 18398355f576SJeff Roberson /* see uma.h */ 18408355f576SJeff Roberson void 184199571dc3SJeff Roberson uma_startup2(void) 18428355f576SJeff Roberson { 1843342f1793SAlan Cox booted = UMA_STARTUP2; 184486bbae32SJeff Roberson bucket_enable(); 18458355f576SJeff Roberson #ifdef UMA_DEBUG 18468355f576SJeff Roberson printf("UMA startup2 complete.\n"); 18478355f576SJeff Roberson #endif 18488355f576SJeff Roberson } 18498355f576SJeff Roberson 18508355f576SJeff Roberson /* 18518355f576SJeff Roberson * Initialize our callout handle 18528355f576SJeff Roberson * 18538355f576SJeff Roberson */ 18548355f576SJeff Roberson 18558355f576SJeff Roberson static void 18568355f576SJeff Roberson uma_startup3(void) 18578355f576SJeff Roberson { 18588355f576SJeff Roberson #ifdef UMA_DEBUG 18598355f576SJeff Roberson printf("Starting callout.\n"); 18608355f576SJeff Roberson #endif 1861a3c07611SRobert Watson callout_init(&uma_callout, CALLOUT_MPSAFE); 18629643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 18638355f576SJeff Roberson #ifdef UMA_DEBUG 18648355f576SJeff Roberson printf("UMA startup3 complete.\n"); 18658355f576SJeff Roberson #endif 18668355f576SJeff Roberson } 18678355f576SJeff Roberson 1868e20a199fSJeff Roberson static uma_keg_t 1869099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 187085dcf349SGleb Smirnoff int align, uint32_t flags) 1871099a0e58SBosko Milekic { 1872099a0e58SBosko Milekic struct uma_kctor_args args; 1873099a0e58SBosko Milekic 1874099a0e58SBosko Milekic args.size = size; 1875099a0e58SBosko Milekic args.uminit = uminit; 1876099a0e58SBosko Milekic args.fini = fini; 18771e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 1878099a0e58SBosko Milekic args.flags = flags; 1879099a0e58SBosko Milekic args.zone = zone; 1880e20a199fSJeff Roberson return (zone_alloc_item(kegs, &args, M_WAITOK)); 1881099a0e58SBosko Milekic } 1882099a0e58SBosko Milekic 18838355f576SJeff Roberson /* See uma.h */ 18841e319f6dSRobert Watson void 18851e319f6dSRobert Watson uma_set_align(int align) 18861e319f6dSRobert Watson { 18871e319f6dSRobert Watson 18881e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 18891e319f6dSRobert Watson uma_align_cache = align; 18901e319f6dSRobert Watson } 18911e319f6dSRobert Watson 18921e319f6dSRobert Watson /* See uma.h */ 18938355f576SJeff Roberson uma_zone_t 1894bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 189585dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 18968355f576SJeff Roberson 18978355f576SJeff Roberson { 18988355f576SJeff Roberson struct uma_zctor_args args; 18998355f576SJeff Roberson 19008355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 19010095a784SJeff Roberson memset(&args, 0, sizeof(args)); 19028355f576SJeff Roberson args.name = name; 19038355f576SJeff Roberson args.size = size; 19048355f576SJeff Roberson args.ctor = ctor; 19058355f576SJeff Roberson args.dtor = dtor; 19068355f576SJeff Roberson args.uminit = uminit; 19078355f576SJeff Roberson args.fini = fini; 19088355f576SJeff Roberson args.align = align; 19098355f576SJeff Roberson args.flags = flags; 1910099a0e58SBosko Milekic args.keg = NULL; 1911099a0e58SBosko Milekic 1912e20a199fSJeff Roberson return (zone_alloc_item(zones, &args, M_WAITOK)); 1913099a0e58SBosko Milekic } 1914099a0e58SBosko Milekic 1915099a0e58SBosko Milekic /* See uma.h */ 1916099a0e58SBosko Milekic uma_zone_t 1917099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 1918099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 1919099a0e58SBosko Milekic { 1920099a0e58SBosko Milekic struct uma_zctor_args args; 1921e20a199fSJeff Roberson uma_keg_t keg; 1922099a0e58SBosko Milekic 1923e20a199fSJeff Roberson keg = zone_first_keg(master); 19240095a784SJeff Roberson memset(&args, 0, sizeof(args)); 1925099a0e58SBosko Milekic args.name = name; 1926e20a199fSJeff Roberson args.size = keg->uk_size; 1927099a0e58SBosko Milekic args.ctor = ctor; 1928099a0e58SBosko Milekic args.dtor = dtor; 1929099a0e58SBosko Milekic args.uminit = zinit; 1930099a0e58SBosko Milekic args.fini = zfini; 1931e20a199fSJeff Roberson args.align = keg->uk_align; 1932e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 1933e20a199fSJeff Roberson args.keg = keg; 19348355f576SJeff Roberson 1935e20a199fSJeff Roberson /* XXX Attaches only one keg of potentially many. */ 1936e20a199fSJeff Roberson return (zone_alloc_item(zones, &args, M_WAITOK)); 19378355f576SJeff Roberson } 19388355f576SJeff Roberson 19390095a784SJeff Roberson /* See uma.h */ 19400095a784SJeff Roberson uma_zone_t 1941af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 1942af526374SJeff Roberson uma_init zinit, uma_fini zfini, uma_import zimport, 1943af526374SJeff Roberson uma_release zrelease, void *arg, int flags) 19440095a784SJeff Roberson { 19450095a784SJeff Roberson struct uma_zctor_args args; 19460095a784SJeff Roberson 19470095a784SJeff Roberson memset(&args, 0, sizeof(args)); 19480095a784SJeff Roberson args.name = name; 1949af526374SJeff Roberson args.size = size; 19500095a784SJeff Roberson args.ctor = ctor; 19510095a784SJeff Roberson args.dtor = dtor; 19520095a784SJeff Roberson args.uminit = zinit; 19530095a784SJeff Roberson args.fini = zfini; 19540095a784SJeff Roberson args.import = zimport; 19550095a784SJeff Roberson args.release = zrelease; 19560095a784SJeff Roberson args.arg = arg; 19570095a784SJeff Roberson args.align = 0; 19580095a784SJeff Roberson args.flags = flags; 19590095a784SJeff Roberson 19600095a784SJeff Roberson return (zone_alloc_item(zones, &args, M_WAITOK)); 19610095a784SJeff Roberson } 19620095a784SJeff Roberson 1963e20a199fSJeff Roberson static void 1964e20a199fSJeff Roberson zone_lock_pair(uma_zone_t a, uma_zone_t b) 1965e20a199fSJeff Roberson { 1966e20a199fSJeff Roberson if (a < b) { 1967e20a199fSJeff Roberson ZONE_LOCK(a); 1968af526374SJeff Roberson mtx_lock_flags(b->uz_lockptr, MTX_DUPOK); 1969e20a199fSJeff Roberson } else { 1970e20a199fSJeff Roberson ZONE_LOCK(b); 1971af526374SJeff Roberson mtx_lock_flags(a->uz_lockptr, MTX_DUPOK); 1972e20a199fSJeff Roberson } 1973e20a199fSJeff Roberson } 1974e20a199fSJeff Roberson 1975e20a199fSJeff Roberson static void 1976e20a199fSJeff Roberson zone_unlock_pair(uma_zone_t a, uma_zone_t b) 1977e20a199fSJeff Roberson { 1978e20a199fSJeff Roberson 1979e20a199fSJeff Roberson ZONE_UNLOCK(a); 1980e20a199fSJeff Roberson ZONE_UNLOCK(b); 1981e20a199fSJeff Roberson } 1982e20a199fSJeff Roberson 1983e20a199fSJeff Roberson int 1984e20a199fSJeff Roberson uma_zsecond_add(uma_zone_t zone, uma_zone_t master) 1985e20a199fSJeff Roberson { 1986e20a199fSJeff Roberson uma_klink_t klink; 1987e20a199fSJeff Roberson uma_klink_t kl; 1988e20a199fSJeff Roberson int error; 1989e20a199fSJeff Roberson 1990e20a199fSJeff Roberson error = 0; 1991e20a199fSJeff Roberson klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO); 1992e20a199fSJeff Roberson 1993e20a199fSJeff Roberson zone_lock_pair(zone, master); 1994e20a199fSJeff Roberson /* 1995e20a199fSJeff Roberson * zone must use vtoslab() to resolve objects and must already be 1996e20a199fSJeff Roberson * a secondary. 1997e20a199fSJeff Roberson */ 1998e20a199fSJeff Roberson if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) 1999e20a199fSJeff Roberson != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) { 2000e20a199fSJeff Roberson error = EINVAL; 2001e20a199fSJeff Roberson goto out; 2002e20a199fSJeff Roberson } 2003e20a199fSJeff Roberson /* 2004e20a199fSJeff Roberson * The new master must also use vtoslab(). 2005e20a199fSJeff Roberson */ 2006e20a199fSJeff Roberson if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) { 2007e20a199fSJeff Roberson error = EINVAL; 2008e20a199fSJeff Roberson goto out; 2009e20a199fSJeff Roberson } 2010e20a199fSJeff Roberson /* 2011e20a199fSJeff Roberson * Both must either be refcnt, or not be refcnt. 2012e20a199fSJeff Roberson */ 2013e20a199fSJeff Roberson if ((zone->uz_flags & UMA_ZONE_REFCNT) != 2014e20a199fSJeff Roberson (master->uz_flags & UMA_ZONE_REFCNT)) { 2015e20a199fSJeff Roberson error = EINVAL; 2016e20a199fSJeff Roberson goto out; 2017e20a199fSJeff Roberson } 2018e20a199fSJeff Roberson /* 2019e20a199fSJeff Roberson * The underlying object must be the same size. rsize 2020e20a199fSJeff Roberson * may be different. 2021e20a199fSJeff Roberson */ 2022e20a199fSJeff Roberson if (master->uz_size != zone->uz_size) { 2023e20a199fSJeff Roberson error = E2BIG; 2024e20a199fSJeff Roberson goto out; 2025e20a199fSJeff Roberson } 2026e20a199fSJeff Roberson /* 2027e20a199fSJeff Roberson * Put it at the end of the list. 2028e20a199fSJeff Roberson */ 2029e20a199fSJeff Roberson klink->kl_keg = zone_first_keg(master); 2030e20a199fSJeff Roberson LIST_FOREACH(kl, &zone->uz_kegs, kl_link) { 2031e20a199fSJeff Roberson if (LIST_NEXT(kl, kl_link) == NULL) { 2032e20a199fSJeff Roberson LIST_INSERT_AFTER(kl, klink, kl_link); 2033e20a199fSJeff Roberson break; 2034e20a199fSJeff Roberson } 2035e20a199fSJeff Roberson } 2036e20a199fSJeff Roberson klink = NULL; 2037e20a199fSJeff Roberson zone->uz_flags |= UMA_ZFLAG_MULTI; 2038e20a199fSJeff Roberson zone->uz_slab = zone_fetch_slab_multi; 2039e20a199fSJeff Roberson 2040e20a199fSJeff Roberson out: 2041e20a199fSJeff Roberson zone_unlock_pair(zone, master); 2042e20a199fSJeff Roberson if (klink != NULL) 2043e20a199fSJeff Roberson free(klink, M_TEMP); 2044e20a199fSJeff Roberson 2045e20a199fSJeff Roberson return (error); 2046e20a199fSJeff Roberson } 2047e20a199fSJeff Roberson 2048e20a199fSJeff Roberson 20498355f576SJeff Roberson /* See uma.h */ 20509c2cd7e5SJeff Roberson void 20519c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 20529c2cd7e5SJeff Roberson { 2053f4ff923bSRobert Watson 20540095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 20559c2cd7e5SJeff Roberson } 20569c2cd7e5SJeff Roberson 20579c2cd7e5SJeff Roberson /* See uma.h */ 20588355f576SJeff Roberson void * 20592cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 20608355f576SJeff Roberson { 20618355f576SJeff Roberson void *item; 20628355f576SJeff Roberson uma_cache_t cache; 20638355f576SJeff Roberson uma_bucket_t bucket; 2064fc03d22bSJeff Roberson int lockfail; 20658355f576SJeff Roberson int cpu; 20668355f576SJeff Roberson 20678355f576SJeff Roberson /* This is the fast path allocation */ 20688355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1 20698355f576SJeff Roberson printf("Allocating one item from %s(%p)\n", zone->uz_name, zone); 20708355f576SJeff Roberson #endif 20713659f747SRobert Watson CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread, 20723659f747SRobert Watson zone->uz_name, flags); 2073a553d4b8SJeff Roberson 2074635fd505SRobert Watson if (flags & M_WAITOK) { 2075b23f72e9SBrian Feldman WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2076635fd505SRobert Watson "uma_zalloc_arg: zone \"%s\"", zone->uz_name); 20774c1cc01cSJohn Baldwin } 20788d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 20798d689e04SGleb Smirnoff if (memguard_cmp_zone(zone)) { 20808d689e04SGleb Smirnoff item = memguard_alloc(zone->uz_size, flags); 20818d689e04SGleb Smirnoff if (item != NULL) { 20828d689e04SGleb Smirnoff /* 20838d689e04SGleb Smirnoff * Avoid conflict with the use-after-free 20848d689e04SGleb Smirnoff * protecting infrastructure from INVARIANTS. 20858d689e04SGleb Smirnoff */ 20868d689e04SGleb Smirnoff if (zone->uz_init != NULL && 20878d689e04SGleb Smirnoff zone->uz_init != mtrash_init && 20888d689e04SGleb Smirnoff zone->uz_init(item, zone->uz_size, flags) != 0) 20898d689e04SGleb Smirnoff return (NULL); 20908d689e04SGleb Smirnoff if (zone->uz_ctor != NULL && 20918d689e04SGleb Smirnoff zone->uz_ctor != mtrash_ctor && 2092fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 2093fc03d22bSJeff Roberson flags) != 0) { 20948d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 20958d689e04SGleb Smirnoff return (NULL); 20968d689e04SGleb Smirnoff } 20978d689e04SGleb Smirnoff return (item); 20988d689e04SGleb Smirnoff } 20998d689e04SGleb Smirnoff /* This is unfortunate but should not be fatal. */ 21008d689e04SGleb Smirnoff } 21018d689e04SGleb Smirnoff #endif 21025d1ae027SRobert Watson /* 21035d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 21045d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 21055d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 21065d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 21075d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 21085d1ae027SRobert Watson * preemption and migration. We release the critical section in 21095d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 21105d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 21115d1ae027SRobert Watson * must detect and handle migration if it has occurred. 21125d1ae027SRobert Watson */ 21135d1ae027SRobert Watson critical_enter(); 21145d1ae027SRobert Watson cpu = curcpu; 21158355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 21168355f576SJeff Roberson 21178355f576SJeff Roberson zalloc_start: 21188355f576SJeff Roberson bucket = cache->uc_allocbucket; 2119fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt > 0) { 2120cae33c14SJeff Roberson bucket->ub_cnt--; 2121cae33c14SJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt]; 21228355f576SJeff Roberson #ifdef INVARIANTS 2123cae33c14SJeff Roberson bucket->ub_bucket[bucket->ub_cnt] = NULL; 21248355f576SJeff Roberson #endif 2125fc03d22bSJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 21268355f576SJeff Roberson cache->uc_allocs++; 21275d1ae027SRobert Watson critical_exit(); 2128fc03d22bSJeff Roberson if (zone->uz_ctor != NULL && 2129fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) { 21300095a784SJeff Roberson atomic_add_long(&zone->uz_fails, 1); 2131fc03d22bSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 2132b23f72e9SBrian Feldman return (NULL); 2133b23f72e9SBrian Feldman } 2134ef72505eSJeff Roberson #ifdef INVARIANTS 2135ef72505eSJeff Roberson uma_dbg_alloc(zone, NULL, item); 2136ef72505eSJeff Roberson #endif 21372cc35ff9SJeff Roberson if (flags & M_ZERO) 2138e20a199fSJeff Roberson bzero(item, zone->uz_size); 21398355f576SJeff Roberson return (item); 2140fc03d22bSJeff Roberson } 2141fc03d22bSJeff Roberson 21428355f576SJeff Roberson /* 21438355f576SJeff Roberson * We have run out of items in our alloc bucket. 21448355f576SJeff Roberson * See if we can switch with our free bucket. 21458355f576SJeff Roberson */ 2146b983089aSJeff Roberson bucket = cache->uc_freebucket; 2147fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt > 0) { 2148fc03d22bSJeff Roberson #ifdef UMA_DEBUG_ALLOC 2149fc03d22bSJeff Roberson printf("uma_zalloc: Swapping empty with alloc.\n"); 2150fc03d22bSJeff Roberson #endif 21518355f576SJeff Roberson cache->uc_freebucket = cache->uc_allocbucket; 2152b983089aSJeff Roberson cache->uc_allocbucket = bucket; 21538355f576SJeff Roberson goto zalloc_start; 21548355f576SJeff Roberson } 2155fc03d22bSJeff Roberson 2156fc03d22bSJeff Roberson /* 2157fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 2158fc03d22bSJeff Roberson */ 2159fc03d22bSJeff Roberson bucket = cache->uc_allocbucket; 2160fc03d22bSJeff Roberson cache->uc_allocbucket = NULL; 2161fc03d22bSJeff Roberson critical_exit(); 2162fc03d22bSJeff Roberson if (bucket != NULL) 21636fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 2164fc03d22bSJeff Roberson 2165fc03d22bSJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 2166fc03d22bSJeff Roberson if (zone->uz_count == 0 || bucketdisable) 2167fc03d22bSJeff Roberson goto zalloc_item; 2168fc03d22bSJeff Roberson 21695d1ae027SRobert Watson /* 21705d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 21715d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 21725d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 21735d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 21745d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 21755d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 21765d1ae027SRobert Watson * the critical section. 21775d1ae027SRobert Watson */ 2178fc03d22bSJeff Roberson lockfail = 0; 2179fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 2180fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 2181a553d4b8SJeff Roberson ZONE_LOCK(zone); 2182fc03d22bSJeff Roberson lockfail = 1; 2183fc03d22bSJeff Roberson } 21845d1ae027SRobert Watson critical_enter(); 21855d1ae027SRobert Watson cpu = curcpu; 21865d1ae027SRobert Watson cache = &zone->uz_cpu[cpu]; 21875d1ae027SRobert Watson 2188fc03d22bSJeff Roberson /* 2189fc03d22bSJeff Roberson * Since we have locked the zone we may as well send back our stats. 2190fc03d22bSJeff Roberson */ 21910095a784SJeff Roberson atomic_add_long(&zone->uz_allocs, cache->uc_allocs); 21920095a784SJeff Roberson atomic_add_long(&zone->uz_frees, cache->uc_frees); 2193a553d4b8SJeff Roberson cache->uc_allocs = 0; 2194773df9abSRobert Watson cache->uc_frees = 0; 21958355f576SJeff Roberson 2196fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 2197fc03d22bSJeff Roberson if (cache->uc_allocbucket != NULL) { 2198fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 2199fc03d22bSJeff Roberson goto zalloc_start; 2200a553d4b8SJeff Roberson } 22018355f576SJeff Roberson 2202fc03d22bSJeff Roberson /* 2203fc03d22bSJeff Roberson * Check the zone's cache of buckets. 2204fc03d22bSJeff Roberson */ 2205fc03d22bSJeff Roberson if ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) { 2206cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 2207a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 22088355f576SJeff Roberson 2209a553d4b8SJeff Roberson LIST_REMOVE(bucket, ub_link); 2210a553d4b8SJeff Roberson cache->uc_allocbucket = bucket; 2211a553d4b8SJeff Roberson ZONE_UNLOCK(zone); 22128355f576SJeff Roberson goto zalloc_start; 2213a553d4b8SJeff Roberson } 22145d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 22155d1ae027SRobert Watson critical_exit(); 2216bbee39c6SJeff Roberson 2217fc03d22bSJeff Roberson /* 2218fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 2219fc03d22bSJeff Roberson * handle the working set. 2220fc03d22bSJeff Roberson */ 22216fd34d6fSJeff Roberson if (lockfail && zone->uz_count < BUCKET_MAX) 2222a553d4b8SJeff Roberson zone->uz_count++; 2223fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 2224099a0e58SBosko Milekic 22258355f576SJeff Roberson /* 2226a553d4b8SJeff Roberson * Now lets just fill a bucket and put it on the free list. If that 2227fc03d22bSJeff Roberson * works we'll restart the allocation from the begining and it 2228fc03d22bSJeff Roberson * will use the just filled bucket. 2229bbee39c6SJeff Roberson */ 22306fd34d6fSJeff Roberson bucket = zone_alloc_bucket(zone, udata, flags); 2231fc03d22bSJeff Roberson if (bucket != NULL) { 2232fc03d22bSJeff Roberson ZONE_LOCK(zone); 2233fc03d22bSJeff Roberson critical_enter(); 2234fc03d22bSJeff Roberson cpu = curcpu; 2235fc03d22bSJeff Roberson cache = &zone->uz_cpu[cpu]; 2236fc03d22bSJeff Roberson /* 2237fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 2238fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 2239fc03d22bSJeff Roberson * the memory directly. 2240fc03d22bSJeff Roberson */ 2241fc03d22bSJeff Roberson if (cache->uc_allocbucket == NULL) 2242fc03d22bSJeff Roberson cache->uc_allocbucket = bucket; 2243fc03d22bSJeff Roberson else 2244fc03d22bSJeff Roberson LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link); 2245bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 2246fc03d22bSJeff Roberson goto zalloc_start; 2247bbee39c6SJeff Roberson } 2248fc03d22bSJeff Roberson 2249bbee39c6SJeff Roberson /* 2250bbee39c6SJeff Roberson * We may not be able to get a bucket so return an actual item. 2251bbee39c6SJeff Roberson */ 2252bbee39c6SJeff Roberson #ifdef UMA_DEBUG 2253bbee39c6SJeff Roberson printf("uma_zalloc_arg: Bucketzone returned NULL\n"); 2254bbee39c6SJeff Roberson #endif 2255bbee39c6SJeff Roberson 2256fc03d22bSJeff Roberson zalloc_item: 2257e20a199fSJeff Roberson item = zone_alloc_item(zone, udata, flags); 2258fc03d22bSJeff Roberson 2259e20a199fSJeff Roberson return (item); 2260bbee39c6SJeff Roberson } 2261bbee39c6SJeff Roberson 2262bbee39c6SJeff Roberson static uma_slab_t 2263e20a199fSJeff Roberson keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags) 2264bbee39c6SJeff Roberson { 2265bbee39c6SJeff Roberson uma_slab_t slab; 22666fd34d6fSJeff Roberson int reserve; 2267099a0e58SBosko Milekic 2268e20a199fSJeff Roberson mtx_assert(&keg->uk_lock, MA_OWNED); 2269bbee39c6SJeff Roberson slab = NULL; 22706fd34d6fSJeff Roberson reserve = 0; 22716fd34d6fSJeff Roberson if ((flags & M_USE_RESERVE) == 0) 22726fd34d6fSJeff Roberson reserve = keg->uk_reserve; 2273bbee39c6SJeff Roberson 2274bbee39c6SJeff Roberson for (;;) { 2275bbee39c6SJeff Roberson /* 2276bbee39c6SJeff Roberson * Find a slab with some space. Prefer slabs that are partially 2277bbee39c6SJeff Roberson * used over those that are totally full. This helps to reduce 2278bbee39c6SJeff Roberson * fragmentation. 2279bbee39c6SJeff Roberson */ 22806fd34d6fSJeff Roberson if (keg->uk_free > reserve) { 2281099a0e58SBosko Milekic if (!LIST_EMPTY(&keg->uk_part_slab)) { 2282099a0e58SBosko Milekic slab = LIST_FIRST(&keg->uk_part_slab); 2283bbee39c6SJeff Roberson } else { 2284099a0e58SBosko Milekic slab = LIST_FIRST(&keg->uk_free_slab); 2285bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 2286099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_part_slab, slab, 2287bbee39c6SJeff Roberson us_link); 2288bbee39c6SJeff Roberson } 2289e20a199fSJeff Roberson MPASS(slab->us_keg == keg); 2290bbee39c6SJeff Roberson return (slab); 2291bbee39c6SJeff Roberson } 2292bbee39c6SJeff Roberson 2293bbee39c6SJeff Roberson /* 2294bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 2295bbee39c6SJeff Roberson */ 2296bbee39c6SJeff Roberson if (flags & M_NOVM) 2297bbee39c6SJeff Roberson break; 2298bbee39c6SJeff Roberson 2299e20a199fSJeff Roberson if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) { 2300099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_FULL; 2301e20a199fSJeff Roberson /* 2302e20a199fSJeff Roberson * If this is not a multi-zone, set the FULL bit. 2303e20a199fSJeff Roberson * Otherwise slab_multi() takes care of it. 2304e20a199fSJeff Roberson */ 23052f891cd5SPawel Jakub Dawidek if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) { 2306e20a199fSJeff Roberson zone->uz_flags |= UMA_ZFLAG_FULL; 23072f891cd5SPawel Jakub Dawidek zone_log_warning(zone); 23082f891cd5SPawel Jakub Dawidek } 2309ebc85edfSJeff Roberson if (flags & M_NOWAIT) 2310bbee39c6SJeff Roberson break; 2311c288b548SEitan Adler zone->uz_sleeps++; 2312e20a199fSJeff Roberson msleep(keg, &keg->uk_lock, PVM, "keglimit", 0); 2313bbee39c6SJeff Roberson continue; 2314bbee39c6SJeff Roberson } 2315e20a199fSJeff Roberson slab = keg_alloc_slab(keg, zone, flags); 2316bbee39c6SJeff Roberson /* 2317bbee39c6SJeff Roberson * If we got a slab here it's safe to mark it partially used 2318bbee39c6SJeff Roberson * and return. We assume that the caller is going to remove 2319bbee39c6SJeff Roberson * at least one item. 2320bbee39c6SJeff Roberson */ 2321bbee39c6SJeff Roberson if (slab) { 2322e20a199fSJeff Roberson MPASS(slab->us_keg == keg); 2323099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link); 2324bbee39c6SJeff Roberson return (slab); 2325bbee39c6SJeff Roberson } 2326bbee39c6SJeff Roberson /* 2327bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 2328bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 2329bbee39c6SJeff Roberson * fail. 2330bbee39c6SJeff Roberson */ 2331bbee39c6SJeff Roberson flags |= M_NOVM; 2332bbee39c6SJeff Roberson } 2333bbee39c6SJeff Roberson return (slab); 2334bbee39c6SJeff Roberson } 2335bbee39c6SJeff Roberson 2336e20a199fSJeff Roberson static uma_slab_t 2337e20a199fSJeff Roberson zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags) 2338e20a199fSJeff Roberson { 2339e20a199fSJeff Roberson uma_slab_t slab; 2340e20a199fSJeff Roberson 2341af526374SJeff Roberson if (keg == NULL) { 2342e20a199fSJeff Roberson keg = zone_first_keg(zone); 2343af526374SJeff Roberson KEG_LOCK(keg); 2344af526374SJeff Roberson } 2345e20a199fSJeff Roberson 2346e20a199fSJeff Roberson for (;;) { 2347e20a199fSJeff Roberson slab = keg_fetch_slab(keg, zone, flags); 2348e20a199fSJeff Roberson if (slab) 2349e20a199fSJeff Roberson return (slab); 2350e20a199fSJeff Roberson if (flags & (M_NOWAIT | M_NOVM)) 2351e20a199fSJeff Roberson break; 2352e20a199fSJeff Roberson } 2353af526374SJeff Roberson KEG_UNLOCK(keg); 2354e20a199fSJeff Roberson return (NULL); 2355e20a199fSJeff Roberson } 2356e20a199fSJeff Roberson 2357e20a199fSJeff Roberson /* 2358e20a199fSJeff Roberson * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns 2359af526374SJeff Roberson * with the keg locked. On NULL no lock is held. 2360e20a199fSJeff Roberson * 2361e20a199fSJeff Roberson * The last pointer is used to seed the search. It is not required. 2362e20a199fSJeff Roberson */ 2363e20a199fSJeff Roberson static uma_slab_t 2364e20a199fSJeff Roberson zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags) 2365e20a199fSJeff Roberson { 2366e20a199fSJeff Roberson uma_klink_t klink; 2367e20a199fSJeff Roberson uma_slab_t slab; 2368e20a199fSJeff Roberson uma_keg_t keg; 2369e20a199fSJeff Roberson int flags; 2370e20a199fSJeff Roberson int empty; 2371e20a199fSJeff Roberson int full; 2372e20a199fSJeff Roberson 2373e20a199fSJeff Roberson /* 2374e20a199fSJeff Roberson * Don't wait on the first pass. This will skip limit tests 2375e20a199fSJeff Roberson * as well. We don't want to block if we can find a provider 2376e20a199fSJeff Roberson * without blocking. 2377e20a199fSJeff Roberson */ 2378e20a199fSJeff Roberson flags = (rflags & ~M_WAITOK) | M_NOWAIT; 2379e20a199fSJeff Roberson /* 2380e20a199fSJeff Roberson * Use the last slab allocated as a hint for where to start 2381e20a199fSJeff Roberson * the search. 2382e20a199fSJeff Roberson */ 2383af526374SJeff Roberson if (last != NULL) { 2384e20a199fSJeff Roberson slab = keg_fetch_slab(last, zone, flags); 2385e20a199fSJeff Roberson if (slab) 2386e20a199fSJeff Roberson return (slab); 2387af526374SJeff Roberson KEG_UNLOCK(last); 2388e20a199fSJeff Roberson } 2389e20a199fSJeff Roberson /* 2390e20a199fSJeff Roberson * Loop until we have a slab incase of transient failures 2391e20a199fSJeff Roberson * while M_WAITOK is specified. I'm not sure this is 100% 2392e20a199fSJeff Roberson * required but we've done it for so long now. 2393e20a199fSJeff Roberson */ 2394e20a199fSJeff Roberson for (;;) { 2395e20a199fSJeff Roberson empty = 0; 2396e20a199fSJeff Roberson full = 0; 2397e20a199fSJeff Roberson /* 2398e20a199fSJeff Roberson * Search the available kegs for slabs. Be careful to hold the 2399e20a199fSJeff Roberson * correct lock while calling into the keg layer. 2400e20a199fSJeff Roberson */ 2401e20a199fSJeff Roberson LIST_FOREACH(klink, &zone->uz_kegs, kl_link) { 2402e20a199fSJeff Roberson keg = klink->kl_keg; 2403af526374SJeff Roberson KEG_LOCK(keg); 2404e20a199fSJeff Roberson if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) { 2405e20a199fSJeff Roberson slab = keg_fetch_slab(keg, zone, flags); 2406e20a199fSJeff Roberson if (slab) 2407e20a199fSJeff Roberson return (slab); 2408e20a199fSJeff Roberson } 2409e20a199fSJeff Roberson if (keg->uk_flags & UMA_ZFLAG_FULL) 2410e20a199fSJeff Roberson full++; 2411e20a199fSJeff Roberson else 2412e20a199fSJeff Roberson empty++; 2413af526374SJeff Roberson KEG_UNLOCK(keg); 2414e20a199fSJeff Roberson } 2415e20a199fSJeff Roberson if (rflags & (M_NOWAIT | M_NOVM)) 2416e20a199fSJeff Roberson break; 2417e20a199fSJeff Roberson flags = rflags; 2418e20a199fSJeff Roberson /* 2419e20a199fSJeff Roberson * All kegs are full. XXX We can't atomically check all kegs 2420e20a199fSJeff Roberson * and sleep so just sleep for a short period and retry. 2421e20a199fSJeff Roberson */ 2422e20a199fSJeff Roberson if (full && !empty) { 2423af526374SJeff Roberson ZONE_LOCK(zone); 2424e20a199fSJeff Roberson zone->uz_flags |= UMA_ZFLAG_FULL; 2425bf965959SSean Bruno zone->uz_sleeps++; 24262f891cd5SPawel Jakub Dawidek zone_log_warning(zone); 2427af526374SJeff Roberson msleep(zone, zone->uz_lockptr, PVM, 2428af526374SJeff Roberson "zonelimit", hz/100); 2429e20a199fSJeff Roberson zone->uz_flags &= ~UMA_ZFLAG_FULL; 2430af526374SJeff Roberson ZONE_UNLOCK(zone); 2431e20a199fSJeff Roberson continue; 2432e20a199fSJeff Roberson } 2433e20a199fSJeff Roberson } 2434e20a199fSJeff Roberson return (NULL); 2435e20a199fSJeff Roberson } 2436e20a199fSJeff Roberson 2437d56368d7SBosko Milekic static void * 24380095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 2439bbee39c6SJeff Roberson { 2440bbee39c6SJeff Roberson void *item; 244185dcf349SGleb Smirnoff uint8_t freei; 2442bbee39c6SJeff Roberson 24430095a784SJeff Roberson MPASS(keg == slab->us_keg); 2444e20a199fSJeff Roberson mtx_assert(&keg->uk_lock, MA_OWNED); 2445099a0e58SBosko Milekic 2446ef72505eSJeff Roberson freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1; 2447ef72505eSJeff Roberson BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free); 2448099a0e58SBosko Milekic item = slab->us_data + (keg->uk_rsize * freei); 2449bbee39c6SJeff Roberson slab->us_freecount--; 2450099a0e58SBosko Milekic keg->uk_free--; 2451ef72505eSJeff Roberson 2452bbee39c6SJeff Roberson /* Move this slab to the full list */ 2453bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 2454bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 2455099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link); 2456bbee39c6SJeff Roberson } 2457bbee39c6SJeff Roberson 2458bbee39c6SJeff Roberson return (item); 2459bbee39c6SJeff Roberson } 2460bbee39c6SJeff Roberson 2461bbee39c6SJeff Roberson static int 24620095a784SJeff Roberson zone_import(uma_zone_t zone, void **bucket, int max, int flags) 24630095a784SJeff Roberson { 24640095a784SJeff Roberson uma_slab_t slab; 24650095a784SJeff Roberson uma_keg_t keg; 24660095a784SJeff Roberson int i; 24670095a784SJeff Roberson 24680095a784SJeff Roberson slab = NULL; 24690095a784SJeff Roberson keg = NULL; 2470af526374SJeff Roberson /* Try to keep the buckets totally full */ 24710095a784SJeff Roberson for (i = 0; i < max; ) { 24720095a784SJeff Roberson if ((slab = zone->uz_slab(zone, keg, flags)) == NULL) 24730095a784SJeff Roberson break; 24740095a784SJeff Roberson keg = slab->us_keg; 24756fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 24760095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 24776fd34d6fSJeff Roberson if (keg->uk_free <= keg->uk_reserve) 24786fd34d6fSJeff Roberson break; 24796fd34d6fSJeff Roberson } 24806fd34d6fSJeff Roberson /* Don't grab more than one slab at a time. */ 24810095a784SJeff Roberson flags &= ~M_WAITOK; 24820095a784SJeff Roberson flags |= M_NOWAIT; 24830095a784SJeff Roberson } 24840095a784SJeff Roberson if (slab != NULL) 24850095a784SJeff Roberson KEG_UNLOCK(keg); 24860095a784SJeff Roberson 24870095a784SJeff Roberson return i; 24880095a784SJeff Roberson } 24890095a784SJeff Roberson 2490fc03d22bSJeff Roberson static uma_bucket_t 24916fd34d6fSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int flags) 2492bbee39c6SJeff Roberson { 2493bbee39c6SJeff Roberson uma_bucket_t bucket; 24940095a784SJeff Roberson int max; 2495bbee39c6SJeff Roberson 24966fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 24976fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 24980095a784SJeff Roberson if (bucket == NULL) 24990095a784SJeff Roberson goto out; 25000095a784SJeff Roberson 2501af526374SJeff Roberson max = MIN(bucket->ub_entries, zone->uz_count); 25020095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 25030095a784SJeff Roberson max, flags); 25040095a784SJeff Roberson 25050095a784SJeff Roberson /* 25060095a784SJeff Roberson * Initialize the memory if necessary. 25070095a784SJeff Roberson */ 25080095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 2509099a0e58SBosko Milekic int i; 2510bbee39c6SJeff Roberson 25110095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 2512e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 25130095a784SJeff Roberson flags) != 0) 2514b23f72e9SBrian Feldman break; 2515b23f72e9SBrian Feldman /* 2516b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 2517b23f72e9SBrian Feldman * rest back onto the freelist. 2518b23f72e9SBrian Feldman */ 2519b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 2520af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 25210095a784SJeff Roberson bucket->ub_cnt - i); 2522a5a262c6SBosko Milekic #ifdef INVARIANTS 25230095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 25240095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 2525a5a262c6SBosko Milekic #endif 2526b23f72e9SBrian Feldman bucket->ub_cnt = i; 2527b23f72e9SBrian Feldman } 2528099a0e58SBosko Milekic } 2529099a0e58SBosko Milekic 25300095a784SJeff Roberson out: 2531fc03d22bSJeff Roberson if (bucket == NULL || bucket->ub_cnt == 0) { 25320095a784SJeff Roberson if (bucket != NULL) 25336fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 2534fc03d22bSJeff Roberson atomic_add_long(&zone->uz_fails, 1); 2535fc03d22bSJeff Roberson return (NULL); 2536bbee39c6SJeff Roberson } 2537fc03d22bSJeff Roberson 2538fc03d22bSJeff Roberson return (bucket); 2539fc03d22bSJeff Roberson } 2540fc03d22bSJeff Roberson 25418355f576SJeff Roberson /* 25420095a784SJeff Roberson * Allocates a single item from a zone. 25438355f576SJeff Roberson * 25448355f576SJeff Roberson * Arguments 25458355f576SJeff Roberson * zone The zone to alloc for. 25468355f576SJeff Roberson * udata The data to be passed to the constructor. 2547a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 25488355f576SJeff Roberson * 25498355f576SJeff Roberson * Returns 25508355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 2551bbee39c6SJeff Roberson * An item if successful 25528355f576SJeff Roberson */ 25538355f576SJeff Roberson 25548355f576SJeff Roberson static void * 2555e20a199fSJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int flags) 25568355f576SJeff Roberson { 25578355f576SJeff Roberson void *item; 25588355f576SJeff Roberson 25598355f576SJeff Roberson item = NULL; 25608355f576SJeff Roberson 25618355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC 25628355f576SJeff Roberson printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone); 25638355f576SJeff Roberson #endif 25640095a784SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, flags) != 1) 25650095a784SJeff Roberson goto fail; 25660095a784SJeff Roberson atomic_add_long(&zone->uz_allocs, 1); 25678355f576SJeff Roberson 2568099a0e58SBosko Milekic /* 2569099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 2570099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 2571099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 2572099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 2573099a0e58SBosko Milekic */ 2574b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 2575e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 25760095a784SJeff Roberson zone_free_item(zone, item, udata, SKIP_FINI); 25770095a784SJeff Roberson goto fail; 2578b23f72e9SBrian Feldman } 2579b23f72e9SBrian Feldman } 2580b23f72e9SBrian Feldman if (zone->uz_ctor != NULL) { 2581e20a199fSJeff Roberson if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) { 25820095a784SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 25830095a784SJeff Roberson goto fail; 2584b23f72e9SBrian Feldman } 2585b23f72e9SBrian Feldman } 2586ef72505eSJeff Roberson #ifdef INVARIANTS 25870095a784SJeff Roberson uma_dbg_alloc(zone, NULL, item); 2588ef72505eSJeff Roberson #endif 25892cc35ff9SJeff Roberson if (flags & M_ZERO) 2590e20a199fSJeff Roberson bzero(item, zone->uz_size); 25918355f576SJeff Roberson 25928355f576SJeff Roberson return (item); 25930095a784SJeff Roberson 25940095a784SJeff Roberson fail: 25950095a784SJeff Roberson atomic_add_long(&zone->uz_fails, 1); 25960095a784SJeff Roberson return (NULL); 25978355f576SJeff Roberson } 25988355f576SJeff Roberson 25998355f576SJeff Roberson /* See uma.h */ 26008355f576SJeff Roberson void 26018355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 26028355f576SJeff Roberson { 26038355f576SJeff Roberson uma_cache_t cache; 26048355f576SJeff Roberson uma_bucket_t bucket; 26054d104ba0SAlexander Motin int lockfail; 26068355f576SJeff Roberson int cpu; 26078355f576SJeff Roberson 26088355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1 26098355f576SJeff Roberson printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone); 26108355f576SJeff Roberson #endif 26113659f747SRobert Watson CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread, 26123659f747SRobert Watson zone->uz_name); 26133659f747SRobert Watson 261420ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 261520ed0cb0SMatthew D Fleming if (item == NULL) 261620ed0cb0SMatthew D Fleming return; 26178d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 26188d689e04SGleb Smirnoff if (is_memguard_addr(item)) { 26198d689e04SGleb Smirnoff if (zone->uz_dtor != NULL && zone->uz_dtor != mtrash_dtor) 26208d689e04SGleb Smirnoff zone->uz_dtor(item, zone->uz_size, udata); 26218d689e04SGleb Smirnoff if (zone->uz_fini != NULL && zone->uz_fini != mtrash_fini) 26228d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 26238d689e04SGleb Smirnoff memguard_free(item); 26248d689e04SGleb Smirnoff return; 26258d689e04SGleb Smirnoff } 26268d689e04SGleb Smirnoff #endif 26275d1ae027SRobert Watson #ifdef INVARIANTS 2628e20a199fSJeff Roberson if (zone->uz_flags & UMA_ZONE_MALLOC) 26295d1ae027SRobert Watson uma_dbg_free(zone, udata, item); 26305d1ae027SRobert Watson else 26315d1ae027SRobert Watson uma_dbg_free(zone, NULL, item); 26325d1ae027SRobert Watson #endif 2633fc03d22bSJeff Roberson if (zone->uz_dtor != NULL) 2634ef72505eSJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 2635ef72505eSJeff Roberson 2636af7f9b97SJeff Roberson /* 2637af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 2638af7f9b97SJeff Roberson * a little longer for the limits to be reset. 2639af7f9b97SJeff Roberson */ 2640e20a199fSJeff Roberson if (zone->uz_flags & UMA_ZFLAG_FULL) 2641fc03d22bSJeff Roberson goto zfree_item; 2642af7f9b97SJeff Roberson 26435d1ae027SRobert Watson /* 26445d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 26455d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 26465d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 26475d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 26485d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 26495d1ae027SRobert Watson * preemption and migration. We release the critical section in 26505d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 26515d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 26525d1ae027SRobert Watson * detect and handle migration if it has occurred. 26535d1ae027SRobert Watson */ 2654a553d4b8SJeff Roberson zfree_restart: 26555d1ae027SRobert Watson critical_enter(); 26565d1ae027SRobert Watson cpu = curcpu; 26578355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 26588355f576SJeff Roberson 26598355f576SJeff Roberson zfree_start: 2660a553d4b8SJeff Roberson /* 2661fc03d22bSJeff Roberson * Try to free into the allocbucket first to give LIFO ordering 2662fc03d22bSJeff Roberson * for cache-hot datastructures. Spill over into the freebucket 2663fc03d22bSJeff Roberson * if necessary. Alloc will swap them if one runs dry. 2664a553d4b8SJeff Roberson */ 2665fc03d22bSJeff Roberson bucket = cache->uc_allocbucket; 2666fc03d22bSJeff Roberson if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries) 2667fc03d22bSJeff Roberson bucket = cache->uc_freebucket; 2668fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) { 2669cae33c14SJeff Roberson KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL, 26708355f576SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 2671cae33c14SJeff Roberson bucket->ub_bucket[bucket->ub_cnt] = item; 2672cae33c14SJeff Roberson bucket->ub_cnt++; 2673773df9abSRobert Watson cache->uc_frees++; 26745d1ae027SRobert Watson critical_exit(); 26758355f576SJeff Roberson return; 2676fc03d22bSJeff Roberson } 2677fc03d22bSJeff Roberson 26788355f576SJeff Roberson /* 26795d1ae027SRobert Watson * We must go back the zone, which requires acquiring the zone lock, 26805d1ae027SRobert Watson * which in turn means we must release and re-acquire the critical 26815d1ae027SRobert Watson * section. Since the critical section is released, we may be 26825d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 26835d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 26845d1ae027SRobert Watson * the critical section. 26858355f576SJeff Roberson */ 26865d1ae027SRobert Watson critical_exit(); 2687fc03d22bSJeff Roberson if (zone->uz_count == 0 || bucketdisable) 2688fc03d22bSJeff Roberson goto zfree_item; 2689fc03d22bSJeff Roberson 26904d104ba0SAlexander Motin lockfail = 0; 26914d104ba0SAlexander Motin if (ZONE_TRYLOCK(zone) == 0) { 26924d104ba0SAlexander Motin /* Record contention to size the buckets. */ 26938355f576SJeff Roberson ZONE_LOCK(zone); 26944d104ba0SAlexander Motin lockfail = 1; 26954d104ba0SAlexander Motin } 26965d1ae027SRobert Watson critical_enter(); 26975d1ae027SRobert Watson cpu = curcpu; 26985d1ae027SRobert Watson cache = &zone->uz_cpu[cpu]; 26998355f576SJeff Roberson 2700fc03d22bSJeff Roberson /* 2701fc03d22bSJeff Roberson * Since we have locked the zone we may as well send back our stats. 2702fc03d22bSJeff Roberson */ 27030095a784SJeff Roberson atomic_add_long(&zone->uz_allocs, cache->uc_allocs); 27040095a784SJeff Roberson atomic_add_long(&zone->uz_frees, cache->uc_frees); 2705f4ff923bSRobert Watson cache->uc_allocs = 0; 2706f4ff923bSRobert Watson cache->uc_frees = 0; 2707f4ff923bSRobert Watson 27088355f576SJeff Roberson bucket = cache->uc_freebucket; 2709fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) { 2710fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 2711fc03d22bSJeff Roberson goto zfree_start; 2712fc03d22bSJeff Roberson } 27138355f576SJeff Roberson cache->uc_freebucket = NULL; 27148355f576SJeff Roberson 27158355f576SJeff Roberson /* Can we throw this on the zone full list? */ 27168355f576SJeff Roberson if (bucket != NULL) { 27178355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC 27188355f576SJeff Roberson printf("uma_zfree: Putting old bucket on the free list.\n"); 27198355f576SJeff Roberson #endif 2720cae33c14SJeff Roberson /* ub_cnt is pointing to the last free item */ 2721cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 27228355f576SJeff Roberson ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n")); 2723fc03d22bSJeff Roberson LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link); 27248355f576SJeff Roberson } 2725fc03d22bSJeff Roberson 27265d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 27275d1ae027SRobert Watson critical_exit(); 2728a553d4b8SJeff Roberson 27294d104ba0SAlexander Motin /* 27304d104ba0SAlexander Motin * We bump the uz count when the cache size is insufficient to 27314d104ba0SAlexander Motin * handle the working set. 27324d104ba0SAlexander Motin */ 27334d104ba0SAlexander Motin if (lockfail && zone->uz_count < BUCKET_MAX) 27344d104ba0SAlexander Motin zone->uz_count++; 2735a553d4b8SJeff Roberson ZONE_UNLOCK(zone); 2736a553d4b8SJeff Roberson 27378355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC 27388355f576SJeff Roberson printf("uma_zfree: Allocating new free bucket.\n"); 27398355f576SJeff Roberson #endif 27406fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT); 27414741dcbfSJeff Roberson if (bucket) { 2742fc03d22bSJeff Roberson critical_enter(); 2743fc03d22bSJeff Roberson cpu = curcpu; 2744fc03d22bSJeff Roberson cache = &zone->uz_cpu[cpu]; 2745fc03d22bSJeff Roberson if (cache->uc_freebucket == NULL) { 2746fc03d22bSJeff Roberson cache->uc_freebucket = bucket; 2747fc03d22bSJeff Roberson goto zfree_start; 2748fc03d22bSJeff Roberson } 2749fc03d22bSJeff Roberson /* 2750fc03d22bSJeff Roberson * We lost the race, start over. We have to drop our 2751fc03d22bSJeff Roberson * critical section to free the bucket. 2752fc03d22bSJeff Roberson */ 2753fc03d22bSJeff Roberson critical_exit(); 27546fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 2755a553d4b8SJeff Roberson goto zfree_restart; 27568355f576SJeff Roberson } 27578355f576SJeff Roberson 2758a553d4b8SJeff Roberson /* 2759a553d4b8SJeff Roberson * If nothing else caught this, we'll just do an internal free. 2760a553d4b8SJeff Roberson */ 2761fc03d22bSJeff Roberson zfree_item: 27620095a784SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 27638355f576SJeff Roberson 27648355f576SJeff Roberson return; 27658355f576SJeff Roberson } 27668355f576SJeff Roberson 27678355f576SJeff Roberson static void 27680095a784SJeff Roberson slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item) 27698355f576SJeff Roberson { 277085dcf349SGleb Smirnoff uint8_t freei; 2771099a0e58SBosko Milekic 27720095a784SJeff Roberson mtx_assert(&keg->uk_lock, MA_OWNED); 2773e20a199fSJeff Roberson MPASS(keg == slab->us_keg); 27748355f576SJeff Roberson 27758355f576SJeff Roberson /* Do we need to remove from any lists? */ 2776099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 27778355f576SJeff Roberson LIST_REMOVE(slab, us_link); 2778099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link); 27798355f576SJeff Roberson } else if (slab->us_freecount == 0) { 27808355f576SJeff Roberson LIST_REMOVE(slab, us_link); 2781099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link); 27828355f576SJeff Roberson } 27838355f576SJeff Roberson 2784ef72505eSJeff Roberson /* Slab management. */ 2785ef72505eSJeff Roberson freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize; 2786ef72505eSJeff Roberson BIT_SET(SLAB_SETSIZE, freei, &slab->us_free); 27878355f576SJeff Roberson slab->us_freecount++; 27888355f576SJeff Roberson 2789ef72505eSJeff Roberson /* Keg statistics. */ 2790099a0e58SBosko Milekic keg->uk_free++; 27910095a784SJeff Roberson } 27920095a784SJeff Roberson 27930095a784SJeff Roberson static void 27940095a784SJeff Roberson zone_release(uma_zone_t zone, void **bucket, int cnt) 27950095a784SJeff Roberson { 27960095a784SJeff Roberson void *item; 27970095a784SJeff Roberson uma_slab_t slab; 27980095a784SJeff Roberson uma_keg_t keg; 27990095a784SJeff Roberson uint8_t *mem; 28000095a784SJeff Roberson int clearfull; 28010095a784SJeff Roberson int i; 28028355f576SJeff Roberson 2803e20a199fSJeff Roberson clearfull = 0; 28040095a784SJeff Roberson keg = zone_first_keg(zone); 2805af526374SJeff Roberson KEG_LOCK(keg); 28060095a784SJeff Roberson for (i = 0; i < cnt; i++) { 28070095a784SJeff Roberson item = bucket[i]; 28080095a784SJeff Roberson if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) { 28090095a784SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 28100095a784SJeff Roberson if (zone->uz_flags & UMA_ZONE_HASH) { 28110095a784SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 28120095a784SJeff Roberson } else { 28130095a784SJeff Roberson mem += keg->uk_pgoff; 28140095a784SJeff Roberson slab = (uma_slab_t)mem; 28150095a784SJeff Roberson } 28160095a784SJeff Roberson } else { 28170095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 28180095a784SJeff Roberson if (slab->us_keg != keg) { 28190095a784SJeff Roberson KEG_UNLOCK(keg); 28200095a784SJeff Roberson keg = slab->us_keg; 28210095a784SJeff Roberson KEG_LOCK(keg); 28220095a784SJeff Roberson } 28230095a784SJeff Roberson } 28240095a784SJeff Roberson slab_free_item(keg, slab, item); 2825099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZFLAG_FULL) { 2826e20a199fSJeff Roberson if (keg->uk_pages < keg->uk_maxpages) { 2827099a0e58SBosko Milekic keg->uk_flags &= ~UMA_ZFLAG_FULL; 2828e20a199fSJeff Roberson clearfull = 1; 2829e20a199fSJeff Roberson } 2830af7f9b97SJeff Roberson 283177380291SMohan Srinivasan /* 2832ef72505eSJeff Roberson * We can handle one more allocation. Since we're 2833ef72505eSJeff Roberson * clearing ZFLAG_FULL, wake up all procs blocked 2834ef72505eSJeff Roberson * on pages. This should be uncommon, so keeping this 2835ef72505eSJeff Roberson * simple for now (rather than adding count of blocked 283677380291SMohan Srinivasan * threads etc). 283777380291SMohan Srinivasan */ 283877380291SMohan Srinivasan wakeup(keg); 2839af7f9b97SJeff Roberson } 28400095a784SJeff Roberson } 2841af526374SJeff Roberson KEG_UNLOCK(keg); 28420095a784SJeff Roberson if (clearfull) { 2843af526374SJeff Roberson ZONE_LOCK(zone); 2844e20a199fSJeff Roberson zone->uz_flags &= ~UMA_ZFLAG_FULL; 2845e20a199fSJeff Roberson wakeup(zone); 2846605cbd6aSJeff Roberson ZONE_UNLOCK(zone); 2847af526374SJeff Roberson } 2848ef72505eSJeff Roberson 28498355f576SJeff Roberson } 28508355f576SJeff Roberson 28510095a784SJeff Roberson /* 28520095a784SJeff Roberson * Frees a single item to any zone. 28530095a784SJeff Roberson * 28540095a784SJeff Roberson * Arguments: 28550095a784SJeff Roberson * zone The zone to free to 28560095a784SJeff Roberson * item The item we're freeing 28570095a784SJeff Roberson * udata User supplied data for the dtor 28580095a784SJeff Roberson * skip Skip dtors and finis 28590095a784SJeff Roberson */ 28600095a784SJeff Roberson static void 28610095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 28620095a784SJeff Roberson { 28630095a784SJeff Roberson 28640095a784SJeff Roberson #ifdef INVARIANTS 28650095a784SJeff Roberson if (skip == SKIP_NONE) { 28660095a784SJeff Roberson if (zone->uz_flags & UMA_ZONE_MALLOC) 28670095a784SJeff Roberson uma_dbg_free(zone, udata, item); 28680095a784SJeff Roberson else 28690095a784SJeff Roberson uma_dbg_free(zone, NULL, item); 28700095a784SJeff Roberson } 28710095a784SJeff Roberson #endif 28720095a784SJeff Roberson if (skip < SKIP_DTOR && zone->uz_dtor) 28730095a784SJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 28740095a784SJeff Roberson 28750095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 28760095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 28770095a784SJeff Roberson 28780095a784SJeff Roberson atomic_add_long(&zone->uz_frees, 1); 28790095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 28800095a784SJeff Roberson } 28810095a784SJeff Roberson 28828355f576SJeff Roberson /* See uma.h */ 28831c6cae97SLawrence Stewart int 2884736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 2885736ee590SJeff Roberson { 2886099a0e58SBosko Milekic uma_keg_t keg; 2887099a0e58SBosko Milekic 2888e20a199fSJeff Roberson keg = zone_first_keg(zone); 28890095a784SJeff Roberson if (keg == NULL) 28900095a784SJeff Roberson return (0); 2891af526374SJeff Roberson KEG_LOCK(keg); 2892e20a199fSJeff Roberson keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera; 2893099a0e58SBosko Milekic if (keg->uk_maxpages * keg->uk_ipers < nitems) 2894e20a199fSJeff Roberson keg->uk_maxpages += keg->uk_ppera; 28951c6cae97SLawrence Stewart nitems = keg->uk_maxpages * keg->uk_ipers; 2896af526374SJeff Roberson KEG_UNLOCK(keg); 28971c6cae97SLawrence Stewart 28981c6cae97SLawrence Stewart return (nitems); 2899736ee590SJeff Roberson } 2900736ee590SJeff Roberson 2901736ee590SJeff Roberson /* See uma.h */ 2902e49471b0SAndre Oppermann int 2903e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 2904e49471b0SAndre Oppermann { 2905e49471b0SAndre Oppermann int nitems; 2906e49471b0SAndre Oppermann uma_keg_t keg; 2907e49471b0SAndre Oppermann 2908e49471b0SAndre Oppermann keg = zone_first_keg(zone); 29090095a784SJeff Roberson if (keg == NULL) 29100095a784SJeff Roberson return (0); 2911af526374SJeff Roberson KEG_LOCK(keg); 2912e49471b0SAndre Oppermann nitems = keg->uk_maxpages * keg->uk_ipers; 2913af526374SJeff Roberson KEG_UNLOCK(keg); 2914e49471b0SAndre Oppermann 2915e49471b0SAndre Oppermann return (nitems); 2916e49471b0SAndre Oppermann } 2917e49471b0SAndre Oppermann 2918e49471b0SAndre Oppermann /* See uma.h */ 29192f891cd5SPawel Jakub Dawidek void 29202f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 29212f891cd5SPawel Jakub Dawidek { 29222f891cd5SPawel Jakub Dawidek 29232f891cd5SPawel Jakub Dawidek ZONE_LOCK(zone); 29242f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 29252f891cd5SPawel Jakub Dawidek ZONE_UNLOCK(zone); 29262f891cd5SPawel Jakub Dawidek } 29272f891cd5SPawel Jakub Dawidek 29282f891cd5SPawel Jakub Dawidek /* See uma.h */ 2929c4ae7908SLawrence Stewart int 2930c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 2931c4ae7908SLawrence Stewart { 2932c4ae7908SLawrence Stewart int64_t nitems; 2933c4ae7908SLawrence Stewart u_int i; 2934c4ae7908SLawrence Stewart 2935c4ae7908SLawrence Stewart ZONE_LOCK(zone); 2936c4ae7908SLawrence Stewart nitems = zone->uz_allocs - zone->uz_frees; 2937c4ae7908SLawrence Stewart CPU_FOREACH(i) { 2938c4ae7908SLawrence Stewart /* 2939c4ae7908SLawrence Stewart * See the comment in sysctl_vm_zone_stats() regarding the 2940c4ae7908SLawrence Stewart * safety of accessing the per-cpu caches. With the zone lock 2941c4ae7908SLawrence Stewart * held, it is safe, but can potentially result in stale data. 2942c4ae7908SLawrence Stewart */ 2943c4ae7908SLawrence Stewart nitems += zone->uz_cpu[i].uc_allocs - 2944c4ae7908SLawrence Stewart zone->uz_cpu[i].uc_frees; 2945c4ae7908SLawrence Stewart } 2946c4ae7908SLawrence Stewart ZONE_UNLOCK(zone); 2947c4ae7908SLawrence Stewart 2948c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 2949c4ae7908SLawrence Stewart } 2950c4ae7908SLawrence Stewart 2951c4ae7908SLawrence Stewart /* See uma.h */ 2952736ee590SJeff Roberson void 2953099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 2954099a0e58SBosko Milekic { 2955e20a199fSJeff Roberson uma_keg_t keg; 2956e20a199fSJeff Roberson 2957e20a199fSJeff Roberson keg = zone_first_keg(zone); 29580095a784SJeff Roberson KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type")); 2959af526374SJeff Roberson KEG_LOCK(keg); 2960e20a199fSJeff Roberson KASSERT(keg->uk_pages == 0, 2961099a0e58SBosko Milekic ("uma_zone_set_init on non-empty keg")); 2962e20a199fSJeff Roberson keg->uk_init = uminit; 2963af526374SJeff Roberson KEG_UNLOCK(keg); 2964099a0e58SBosko Milekic } 2965099a0e58SBosko Milekic 2966099a0e58SBosko Milekic /* See uma.h */ 2967099a0e58SBosko Milekic void 2968099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 2969099a0e58SBosko Milekic { 2970e20a199fSJeff Roberson uma_keg_t keg; 2971e20a199fSJeff Roberson 2972e20a199fSJeff Roberson keg = zone_first_keg(zone); 29730095a784SJeff Roberson KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type")); 2974af526374SJeff Roberson KEG_LOCK(keg); 2975e20a199fSJeff Roberson KASSERT(keg->uk_pages == 0, 2976099a0e58SBosko Milekic ("uma_zone_set_fini on non-empty keg")); 2977e20a199fSJeff Roberson keg->uk_fini = fini; 2978af526374SJeff Roberson KEG_UNLOCK(keg); 2979099a0e58SBosko Milekic } 2980099a0e58SBosko Milekic 2981099a0e58SBosko Milekic /* See uma.h */ 2982099a0e58SBosko Milekic void 2983099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 2984099a0e58SBosko Milekic { 2985af526374SJeff Roberson 2986099a0e58SBosko Milekic ZONE_LOCK(zone); 2987e20a199fSJeff Roberson KASSERT(zone_first_keg(zone)->uk_pages == 0, 2988099a0e58SBosko Milekic ("uma_zone_set_zinit on non-empty keg")); 2989099a0e58SBosko Milekic zone->uz_init = zinit; 2990099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2991099a0e58SBosko Milekic } 2992099a0e58SBosko Milekic 2993099a0e58SBosko Milekic /* See uma.h */ 2994099a0e58SBosko Milekic void 2995099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 2996099a0e58SBosko Milekic { 2997af526374SJeff Roberson 2998099a0e58SBosko Milekic ZONE_LOCK(zone); 2999e20a199fSJeff Roberson KASSERT(zone_first_keg(zone)->uk_pages == 0, 3000099a0e58SBosko Milekic ("uma_zone_set_zfini on non-empty keg")); 3001099a0e58SBosko Milekic zone->uz_fini = zfini; 3002099a0e58SBosko Milekic ZONE_UNLOCK(zone); 3003099a0e58SBosko Milekic } 3004099a0e58SBosko Milekic 3005099a0e58SBosko Milekic /* See uma.h */ 3006b23f72e9SBrian Feldman /* XXX uk_freef is not actually used with the zone locked */ 3007099a0e58SBosko Milekic void 30088355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 30098355f576SJeff Roberson { 30100095a784SJeff Roberson uma_keg_t keg; 3011e20a199fSJeff Roberson 30120095a784SJeff Roberson keg = zone_first_keg(zone); 30130095a784SJeff Roberson KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type")); 3014af526374SJeff Roberson KEG_LOCK(keg); 30150095a784SJeff Roberson keg->uk_freef = freef; 3016af526374SJeff Roberson KEG_UNLOCK(keg); 30178355f576SJeff Roberson } 30188355f576SJeff Roberson 30198355f576SJeff Roberson /* See uma.h */ 3020b23f72e9SBrian Feldman /* XXX uk_allocf is not actually used with the zone locked */ 30218355f576SJeff Roberson void 30228355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 30238355f576SJeff Roberson { 3024e20a199fSJeff Roberson uma_keg_t keg; 3025e20a199fSJeff Roberson 3026e20a199fSJeff Roberson keg = zone_first_keg(zone); 3027af526374SJeff Roberson KEG_LOCK(keg); 3028e20a199fSJeff Roberson keg->uk_allocf = allocf; 3029af526374SJeff Roberson KEG_UNLOCK(keg); 30308355f576SJeff Roberson } 30318355f576SJeff Roberson 30328355f576SJeff Roberson /* See uma.h */ 30336fd34d6fSJeff Roberson void 30346fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 30356fd34d6fSJeff Roberson { 30366fd34d6fSJeff Roberson uma_keg_t keg; 30376fd34d6fSJeff Roberson 30386fd34d6fSJeff Roberson keg = zone_first_keg(zone); 30396fd34d6fSJeff Roberson if (keg == NULL) 30406fd34d6fSJeff Roberson return; 30416fd34d6fSJeff Roberson KEG_LOCK(keg); 30426fd34d6fSJeff Roberson keg->uk_reserve = items; 30436fd34d6fSJeff Roberson KEG_UNLOCK(keg); 30446fd34d6fSJeff Roberson 30456fd34d6fSJeff Roberson return; 30466fd34d6fSJeff Roberson } 30476fd34d6fSJeff Roberson 30486fd34d6fSJeff Roberson /* See uma.h */ 30498355f576SJeff Roberson int 3050a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 30518355f576SJeff Roberson { 3052099a0e58SBosko Milekic uma_keg_t keg; 30538355f576SJeff Roberson vm_offset_t kva; 3054099a0e58SBosko Milekic int pages; 30558355f576SJeff Roberson 3056e20a199fSJeff Roberson keg = zone_first_keg(zone); 30570095a784SJeff Roberson if (keg == NULL) 30580095a784SJeff Roberson return (0); 3059099a0e58SBosko Milekic pages = count / keg->uk_ipers; 30608355f576SJeff Roberson 3061099a0e58SBosko Milekic if (pages * keg->uk_ipers < count) 30628355f576SJeff Roberson pages++; 3063a553d4b8SJeff Roberson 3064a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 3065a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 3066a4915c21SAttilio Rao #else 3067a4915c21SAttilio Rao if (1) { 3068a4915c21SAttilio Rao #endif 30695df87b21SJeff Roberson kva = kva_alloc(pages * UMA_SLAB_SIZE); 3070d1f42ac2SAlan Cox if (kva == 0) 30718355f576SJeff Roberson return (0); 3072a4915c21SAttilio Rao } else 3073a4915c21SAttilio Rao kva = 0; 3074af526374SJeff Roberson KEG_LOCK(keg); 3075099a0e58SBosko Milekic keg->uk_kva = kva; 3076a4915c21SAttilio Rao keg->uk_offset = 0; 3077099a0e58SBosko Milekic keg->uk_maxpages = pages; 3078a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 3079a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 3080a4915c21SAttilio Rao #else 3081a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 3082a4915c21SAttilio Rao #endif 30836fd34d6fSJeff Roberson keg->uk_flags |= UMA_ZONE_NOFREE; 3084af526374SJeff Roberson KEG_UNLOCK(keg); 3085af526374SJeff Roberson 30868355f576SJeff Roberson return (1); 30878355f576SJeff Roberson } 30888355f576SJeff Roberson 30898355f576SJeff Roberson /* See uma.h */ 30908355f576SJeff Roberson void 30918355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 30928355f576SJeff Roberson { 30938355f576SJeff Roberson int slabs; 30948355f576SJeff Roberson uma_slab_t slab; 3095099a0e58SBosko Milekic uma_keg_t keg; 30968355f576SJeff Roberson 3097e20a199fSJeff Roberson keg = zone_first_keg(zone); 30980095a784SJeff Roberson if (keg == NULL) 30990095a784SJeff Roberson return; 3100af526374SJeff Roberson KEG_LOCK(keg); 3101099a0e58SBosko Milekic slabs = items / keg->uk_ipers; 3102099a0e58SBosko Milekic if (slabs * keg->uk_ipers < items) 31038355f576SJeff Roberson slabs++; 31048355f576SJeff Roberson while (slabs > 0) { 3105e20a199fSJeff Roberson slab = keg_alloc_slab(keg, zone, M_WAITOK); 3106e20a199fSJeff Roberson if (slab == NULL) 3107e20a199fSJeff Roberson break; 3108e20a199fSJeff Roberson MPASS(slab->us_keg == keg); 3109099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link); 31108355f576SJeff Roberson slabs--; 31118355f576SJeff Roberson } 3112af526374SJeff Roberson KEG_UNLOCK(keg); 31138355f576SJeff Roberson } 31148355f576SJeff Roberson 31158355f576SJeff Roberson /* See uma.h */ 311685dcf349SGleb Smirnoff uint32_t * 3117099a0e58SBosko Milekic uma_find_refcnt(uma_zone_t zone, void *item) 3118099a0e58SBosko Milekic { 3119ab14a3f7SBrian Feldman uma_slabrefcnt_t slabref; 3120ef72505eSJeff Roberson uma_slab_t slab; 3121099a0e58SBosko Milekic uma_keg_t keg; 312285dcf349SGleb Smirnoff uint32_t *refcnt; 3123099a0e58SBosko Milekic int idx; 3124099a0e58SBosko Milekic 3125ef72505eSJeff Roberson slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK)); 3126ef72505eSJeff Roberson slabref = (uma_slabrefcnt_t)slab; 3127ef72505eSJeff Roberson keg = slab->us_keg; 3128ef72505eSJeff Roberson KASSERT(keg->uk_flags & UMA_ZONE_REFCNT, 3129099a0e58SBosko Milekic ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT")); 3130ef72505eSJeff Roberson idx = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize; 3131ef72505eSJeff Roberson refcnt = &slabref->us_refcnt[idx]; 3132099a0e58SBosko Milekic return refcnt; 3133099a0e58SBosko Milekic } 3134099a0e58SBosko Milekic 3135099a0e58SBosko Milekic /* See uma.h */ 31368355f576SJeff Roberson void 31378355f576SJeff Roberson uma_reclaim(void) 31388355f576SJeff Roberson { 31398355f576SJeff Roberson #ifdef UMA_DEBUG 31408355f576SJeff Roberson printf("UMA: vm asked us to release pages!\n"); 31418355f576SJeff Roberson #endif 314286bbae32SJeff Roberson bucket_enable(); 31438355f576SJeff Roberson zone_foreach(zone_drain); 3144*a2de44abSAlexander Motin if (vm_page_count_min()) { 3145*a2de44abSAlexander Motin cache_drain_safe(NULL); 3146*a2de44abSAlexander Motin zone_foreach(zone_drain); 3147*a2de44abSAlexander Motin } 31488355f576SJeff Roberson /* 31498355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 31508355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 31518355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 31528355f576SJeff Roberson */ 31539643769aSJeff Roberson zone_drain(slabzone); 3154099a0e58SBosko Milekic zone_drain(slabrefzone); 3155cae33c14SJeff Roberson bucket_zone_drain(); 31568355f576SJeff Roberson } 31578355f576SJeff Roberson 3158663b416fSJohn Baldwin /* See uma.h */ 3159663b416fSJohn Baldwin int 3160663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 3161663b416fSJohn Baldwin { 3162663b416fSJohn Baldwin int full; 3163663b416fSJohn Baldwin 3164663b416fSJohn Baldwin ZONE_LOCK(zone); 3165e20a199fSJeff Roberson full = (zone->uz_flags & UMA_ZFLAG_FULL); 3166663b416fSJohn Baldwin ZONE_UNLOCK(zone); 3167663b416fSJohn Baldwin return (full); 3168663b416fSJohn Baldwin } 3169663b416fSJohn Baldwin 31706c125b8dSMohan Srinivasan int 31716c125b8dSMohan Srinivasan uma_zone_exhausted_nolock(uma_zone_t zone) 31726c125b8dSMohan Srinivasan { 3173e20a199fSJeff Roberson return (zone->uz_flags & UMA_ZFLAG_FULL); 31746c125b8dSMohan Srinivasan } 31756c125b8dSMohan Srinivasan 31768355f576SJeff Roberson void * 31778355f576SJeff Roberson uma_large_malloc(int size, int wait) 31788355f576SJeff Roberson { 31798355f576SJeff Roberson void *mem; 31808355f576SJeff Roberson uma_slab_t slab; 318185dcf349SGleb Smirnoff uint8_t flags; 31828355f576SJeff Roberson 3183e20a199fSJeff Roberson slab = zone_alloc_item(slabzone, NULL, wait); 31848355f576SJeff Roberson if (slab == NULL) 31858355f576SJeff Roberson return (NULL); 31868355f576SJeff Roberson mem = page_alloc(NULL, size, &flags, wait); 31878355f576SJeff Roberson if (mem) { 318899571dc3SJeff Roberson vsetslab((vm_offset_t)mem, slab); 31898355f576SJeff Roberson slab->us_data = mem; 31908355f576SJeff Roberson slab->us_flags = flags | UMA_SLAB_MALLOC; 31918355f576SJeff Roberson slab->us_size = size; 31928355f576SJeff Roberson } else { 31930095a784SJeff Roberson zone_free_item(slabzone, slab, NULL, SKIP_NONE); 31948355f576SJeff Roberson } 31958355f576SJeff Roberson 31968355f576SJeff Roberson return (mem); 31978355f576SJeff Roberson } 31988355f576SJeff Roberson 31998355f576SJeff Roberson void 32008355f576SJeff Roberson uma_large_free(uma_slab_t slab) 32018355f576SJeff Roberson { 3202c325e866SKonstantin Belousov 32038355f576SJeff Roberson page_free(slab->us_data, slab->us_size, slab->us_flags); 32040095a784SJeff Roberson zone_free_item(slabzone, slab, NULL, SKIP_NONE); 32058355f576SJeff Roberson } 32068355f576SJeff Roberson 32078355f576SJeff Roberson void 32088355f576SJeff Roberson uma_print_stats(void) 32098355f576SJeff Roberson { 32108355f576SJeff Roberson zone_foreach(uma_print_zone); 32118355f576SJeff Roberson } 32128355f576SJeff Roberson 3213504d5de3SJeff Roberson static void 3214504d5de3SJeff Roberson slab_print(uma_slab_t slab) 3215504d5de3SJeff Roberson { 3216ef72505eSJeff Roberson printf("slab: keg %p, data %p, freecount %d\n", 3217ef72505eSJeff Roberson slab->us_keg, slab->us_data, slab->us_freecount); 3218504d5de3SJeff Roberson } 3219504d5de3SJeff Roberson 3220504d5de3SJeff Roberson static void 3221504d5de3SJeff Roberson cache_print(uma_cache_t cache) 3222504d5de3SJeff Roberson { 3223504d5de3SJeff Roberson printf("alloc: %p(%d), free: %p(%d)\n", 3224504d5de3SJeff Roberson cache->uc_allocbucket, 3225504d5de3SJeff Roberson cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0, 3226504d5de3SJeff Roberson cache->uc_freebucket, 3227504d5de3SJeff Roberson cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0); 3228504d5de3SJeff Roberson } 3229504d5de3SJeff Roberson 3230e20a199fSJeff Roberson static void 3231e20a199fSJeff Roberson uma_print_keg(uma_keg_t keg) 32328355f576SJeff Roberson { 3233504d5de3SJeff Roberson uma_slab_t slab; 3234504d5de3SJeff Roberson 32350b80c1e4SEitan Adler printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d " 3236e20a199fSJeff Roberson "out %d free %d limit %d\n", 3237e20a199fSJeff Roberson keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags, 3238099a0e58SBosko Milekic keg->uk_ipers, keg->uk_ppera, 3239e20a199fSJeff Roberson (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free, 3240e20a199fSJeff Roberson (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers); 3241504d5de3SJeff Roberson printf("Part slabs:\n"); 3242099a0e58SBosko Milekic LIST_FOREACH(slab, &keg->uk_part_slab, us_link) 3243504d5de3SJeff Roberson slab_print(slab); 3244504d5de3SJeff Roberson printf("Free slabs:\n"); 3245099a0e58SBosko Milekic LIST_FOREACH(slab, &keg->uk_free_slab, us_link) 3246504d5de3SJeff Roberson slab_print(slab); 3247504d5de3SJeff Roberson printf("Full slabs:\n"); 3248099a0e58SBosko Milekic LIST_FOREACH(slab, &keg->uk_full_slab, us_link) 3249504d5de3SJeff Roberson slab_print(slab); 3250e20a199fSJeff Roberson } 3251e20a199fSJeff Roberson 3252e20a199fSJeff Roberson void 3253e20a199fSJeff Roberson uma_print_zone(uma_zone_t zone) 3254e20a199fSJeff Roberson { 3255e20a199fSJeff Roberson uma_cache_t cache; 3256e20a199fSJeff Roberson uma_klink_t kl; 3257e20a199fSJeff Roberson int i; 3258e20a199fSJeff Roberson 32590b80c1e4SEitan Adler printf("zone: %s(%p) size %d flags %#x\n", 3260e20a199fSJeff Roberson zone->uz_name, zone, zone->uz_size, zone->uz_flags); 3261e20a199fSJeff Roberson LIST_FOREACH(kl, &zone->uz_kegs, kl_link) 3262e20a199fSJeff Roberson uma_print_keg(kl->kl_keg); 32633aa6d94eSJohn Baldwin CPU_FOREACH(i) { 3264504d5de3SJeff Roberson cache = &zone->uz_cpu[i]; 3265504d5de3SJeff Roberson printf("CPU %d Cache:\n", i); 3266504d5de3SJeff Roberson cache_print(cache); 3267504d5de3SJeff Roberson } 32688355f576SJeff Roberson } 32698355f576SJeff Roberson 3270a0d4b0aeSRobert Watson #ifdef DDB 32718355f576SJeff Roberson /* 32727a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 32737a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 32747a52a97eSRobert Watson * 32757a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 32767a52a97eSRobert Watson * per-CPU cache statistic. 32777a52a97eSRobert Watson * 32787a52a97eSRobert Watson * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't 32797a52a97eSRobert Watson * safe from off-CPU; we should modify the caches to track this information 32807a52a97eSRobert Watson * directly so that we don't have to. 32817a52a97eSRobert Watson */ 32827a52a97eSRobert Watson static void 328385dcf349SGleb Smirnoff uma_zone_sumstat(uma_zone_t z, int *cachefreep, uint64_t *allocsp, 328485dcf349SGleb Smirnoff uint64_t *freesp, uint64_t *sleepsp) 32857a52a97eSRobert Watson { 32867a52a97eSRobert Watson uma_cache_t cache; 328785dcf349SGleb Smirnoff uint64_t allocs, frees, sleeps; 32887a52a97eSRobert Watson int cachefree, cpu; 32897a52a97eSRobert Watson 3290bf965959SSean Bruno allocs = frees = sleeps = 0; 32917a52a97eSRobert Watson cachefree = 0; 32923aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 32937a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 32947a52a97eSRobert Watson if (cache->uc_allocbucket != NULL) 32957a52a97eSRobert Watson cachefree += cache->uc_allocbucket->ub_cnt; 32967a52a97eSRobert Watson if (cache->uc_freebucket != NULL) 32977a52a97eSRobert Watson cachefree += cache->uc_freebucket->ub_cnt; 32987a52a97eSRobert Watson allocs += cache->uc_allocs; 32997a52a97eSRobert Watson frees += cache->uc_frees; 33007a52a97eSRobert Watson } 33017a52a97eSRobert Watson allocs += z->uz_allocs; 33027a52a97eSRobert Watson frees += z->uz_frees; 3303bf965959SSean Bruno sleeps += z->uz_sleeps; 33047a52a97eSRobert Watson if (cachefreep != NULL) 33057a52a97eSRobert Watson *cachefreep = cachefree; 33067a52a97eSRobert Watson if (allocsp != NULL) 33077a52a97eSRobert Watson *allocsp = allocs; 33087a52a97eSRobert Watson if (freesp != NULL) 33097a52a97eSRobert Watson *freesp = frees; 3310bf965959SSean Bruno if (sleepsp != NULL) 3311bf965959SSean Bruno *sleepsp = sleeps; 33127a52a97eSRobert Watson } 3313a0d4b0aeSRobert Watson #endif /* DDB */ 33147a52a97eSRobert Watson 33157a52a97eSRobert Watson static int 33167a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 33177a52a97eSRobert Watson { 33187a52a97eSRobert Watson uma_keg_t kz; 33197a52a97eSRobert Watson uma_zone_t z; 33207a52a97eSRobert Watson int count; 33217a52a97eSRobert Watson 33227a52a97eSRobert Watson count = 0; 33237a52a97eSRobert Watson mtx_lock(&uma_mtx); 33247a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 33257a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 33267a52a97eSRobert Watson count++; 33277a52a97eSRobert Watson } 33287a52a97eSRobert Watson mtx_unlock(&uma_mtx); 33297a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 33307a52a97eSRobert Watson } 33317a52a97eSRobert Watson 33327a52a97eSRobert Watson static int 33337a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 33347a52a97eSRobert Watson { 33357a52a97eSRobert Watson struct uma_stream_header ush; 33367a52a97eSRobert Watson struct uma_type_header uth; 33377a52a97eSRobert Watson struct uma_percpu_stat ups; 33387a52a97eSRobert Watson uma_bucket_t bucket; 33397a52a97eSRobert Watson struct sbuf sbuf; 33407a52a97eSRobert Watson uma_cache_t cache; 3341e20a199fSJeff Roberson uma_klink_t kl; 33427a52a97eSRobert Watson uma_keg_t kz; 33437a52a97eSRobert Watson uma_zone_t z; 3344e20a199fSJeff Roberson uma_keg_t k; 33454e657159SMatthew D Fleming int count, error, i; 33467a52a97eSRobert Watson 334700f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 334800f0e671SMatthew D Fleming if (error != 0) 334900f0e671SMatthew D Fleming return (error); 33504e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 33514e657159SMatthew D Fleming 3352404a593eSMatthew D Fleming count = 0; 33534e657159SMatthew D Fleming mtx_lock(&uma_mtx); 33547a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 33557a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 33567a52a97eSRobert Watson count++; 33577a52a97eSRobert Watson } 33587a52a97eSRobert Watson 33597a52a97eSRobert Watson /* 33607a52a97eSRobert Watson * Insert stream header. 33617a52a97eSRobert Watson */ 33627a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 33637a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 3364ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 33657a52a97eSRobert Watson ush.ush_count = count; 33664e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 33677a52a97eSRobert Watson 33687a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 33697a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 33707a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 33717a52a97eSRobert Watson ZONE_LOCK(z); 3372cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 33737a52a97eSRobert Watson uth.uth_align = kz->uk_align; 33747a52a97eSRobert Watson uth.uth_size = kz->uk_size; 33757a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 3376e20a199fSJeff Roberson LIST_FOREACH(kl, &z->uz_kegs, kl_link) { 3377e20a199fSJeff Roberson k = kl->kl_keg; 3378e20a199fSJeff Roberson uth.uth_maxpages += k->uk_maxpages; 3379e20a199fSJeff Roberson uth.uth_pages += k->uk_pages; 3380e20a199fSJeff Roberson uth.uth_keg_free += k->uk_free; 3381e20a199fSJeff Roberson uth.uth_limit = (k->uk_maxpages / k->uk_ppera) 3382e20a199fSJeff Roberson * k->uk_ipers; 3383e20a199fSJeff Roberson } 3384cbbb4a00SRobert Watson 3385cbbb4a00SRobert Watson /* 3386cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 3387cbbb4a00SRobert Watson * on the keg's zone list. 3388cbbb4a00SRobert Watson */ 3389e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 3390cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 3391cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 3392cbbb4a00SRobert Watson 3393fc03d22bSJeff Roberson LIST_FOREACH(bucket, &z->uz_buckets, ub_link) 33947a52a97eSRobert Watson uth.uth_zone_free += bucket->ub_cnt; 33957a52a97eSRobert Watson uth.uth_allocs = z->uz_allocs; 33967a52a97eSRobert Watson uth.uth_frees = z->uz_frees; 33972019094aSRobert Watson uth.uth_fails = z->uz_fails; 3398bf965959SSean Bruno uth.uth_sleeps = z->uz_sleeps; 33994e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 34007a52a97eSRobert Watson /* 34012450bbb8SRobert Watson * While it is not normally safe to access the cache 34022450bbb8SRobert Watson * bucket pointers while not on the CPU that owns the 34032450bbb8SRobert Watson * cache, we only allow the pointers to be exchanged 34042450bbb8SRobert Watson * without the zone lock held, not invalidated, so 34052450bbb8SRobert Watson * accept the possible race associated with bucket 34062450bbb8SRobert Watson * exchange during monitoring. 34077a52a97eSRobert Watson */ 3408ab3a57c0SRobert Watson for (i = 0; i < (mp_maxid + 1); i++) { 34097a52a97eSRobert Watson bzero(&ups, sizeof(ups)); 34107a52a97eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) 34117a52a97eSRobert Watson goto skip; 3412082dc776SRobert Watson if (CPU_ABSENT(i)) 3413082dc776SRobert Watson goto skip; 34147a52a97eSRobert Watson cache = &z->uz_cpu[i]; 34157a52a97eSRobert Watson if (cache->uc_allocbucket != NULL) 34167a52a97eSRobert Watson ups.ups_cache_free += 34177a52a97eSRobert Watson cache->uc_allocbucket->ub_cnt; 34187a52a97eSRobert Watson if (cache->uc_freebucket != NULL) 34197a52a97eSRobert Watson ups.ups_cache_free += 34207a52a97eSRobert Watson cache->uc_freebucket->ub_cnt; 34217a52a97eSRobert Watson ups.ups_allocs = cache->uc_allocs; 34227a52a97eSRobert Watson ups.ups_frees = cache->uc_frees; 34237a52a97eSRobert Watson skip: 34244e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ups, sizeof(ups)); 34257a52a97eSRobert Watson } 34262450bbb8SRobert Watson ZONE_UNLOCK(z); 34277a52a97eSRobert Watson } 34287a52a97eSRobert Watson } 34297a52a97eSRobert Watson mtx_unlock(&uma_mtx); 34304e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 34314e657159SMatthew D Fleming sbuf_delete(&sbuf); 34327a52a97eSRobert Watson return (error); 34337a52a97eSRobert Watson } 343448c5777eSRobert Watson 343548c5777eSRobert Watson #ifdef DDB 343648c5777eSRobert Watson DB_SHOW_COMMAND(uma, db_show_uma) 343748c5777eSRobert Watson { 343885dcf349SGleb Smirnoff uint64_t allocs, frees, sleeps; 343948c5777eSRobert Watson uma_bucket_t bucket; 344048c5777eSRobert Watson uma_keg_t kz; 344148c5777eSRobert Watson uma_zone_t z; 344248c5777eSRobert Watson int cachefree; 344348c5777eSRobert Watson 3444bf965959SSean Bruno db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 3445bf965959SSean Bruno "Requests", "Sleeps"); 344648c5777eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 344748c5777eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 344848c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 344948c5777eSRobert Watson allocs = z->uz_allocs; 345048c5777eSRobert Watson frees = z->uz_frees; 3451bf965959SSean Bruno sleeps = z->uz_sleeps; 345248c5777eSRobert Watson cachefree = 0; 345348c5777eSRobert Watson } else 345448c5777eSRobert Watson uma_zone_sumstat(z, &cachefree, &allocs, 3455bf965959SSean Bruno &frees, &sleeps); 3456e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 345748c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 345848c5777eSRobert Watson cachefree += kz->uk_free; 3459fc03d22bSJeff Roberson LIST_FOREACH(bucket, &z->uz_buckets, ub_link) 346048c5777eSRobert Watson cachefree += bucket->ub_cnt; 3461bf965959SSean Bruno db_printf("%18s %8ju %8jd %8d %12ju %8ju\n", z->uz_name, 3462ae4e9636SRobert Watson (uintmax_t)kz->uk_size, 3463ae4e9636SRobert Watson (intmax_t)(allocs - frees), cachefree, 3464bf965959SSean Bruno (uintmax_t)allocs, sleeps); 3465687c94aaSJohn Baldwin if (db_pager_quit) 3466687c94aaSJohn Baldwin return; 346748c5777eSRobert Watson } 346848c5777eSRobert Watson } 346948c5777eSRobert Watson } 347048c5777eSRobert Watson #endif 3471