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> 788355f576SJeff Roberson #include <sys/smp.h> 7986bbae32SJeff Roberson #include <sys/vmmeter.h> 8086bbae32SJeff Roberson 818355f576SJeff Roberson #include <vm/vm.h> 828355f576SJeff Roberson #include <vm/vm_object.h> 838355f576SJeff Roberson #include <vm/vm_page.h> 84a4915c21SAttilio Rao #include <vm/vm_pageout.h> 858355f576SJeff Roberson #include <vm/vm_param.h> 868355f576SJeff Roberson #include <vm/vm_map.h> 878355f576SJeff Roberson #include <vm/vm_kern.h> 888355f576SJeff Roberson #include <vm/vm_extern.h> 898355f576SJeff Roberson #include <vm/uma.h> 908355f576SJeff Roberson #include <vm/uma_int.h> 91639c9550SJeff Roberson #include <vm/uma_dbg.h> 928355f576SJeff Roberson 9348c5777eSRobert Watson #include <ddb/ddb.h> 9448c5777eSRobert Watson 958d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 968d689e04SGleb Smirnoff #include <vm/memguard.h> 978d689e04SGleb Smirnoff #endif 988d689e04SGleb Smirnoff 998355f576SJeff Roberson /* 100099a0e58SBosko Milekic * This is the zone and keg from which all zones are spawned. The idea is that 101099a0e58SBosko Milekic * even the zone & keg heads are allocated from the allocator, so we use the 102099a0e58SBosko Milekic * bss section to bootstrap us. 1038355f576SJeff Roberson */ 104099a0e58SBosko Milekic static struct uma_keg masterkeg; 105099a0e58SBosko Milekic static struct uma_zone masterzone_k; 106099a0e58SBosko Milekic static struct uma_zone masterzone_z; 107099a0e58SBosko Milekic static uma_zone_t kegs = &masterzone_k; 108099a0e58SBosko Milekic static uma_zone_t zones = &masterzone_z; 1098355f576SJeff Roberson 1108355f576SJeff Roberson /* This is the zone from which all of uma_slab_t's are allocated. */ 1118355f576SJeff Roberson static uma_zone_t slabzone; 112099a0e58SBosko Milekic static uma_zone_t slabrefzone; /* With refcounters (for UMA_ZONE_REFCNT) */ 1138355f576SJeff Roberson 1148355f576SJeff Roberson /* 1158355f576SJeff Roberson * The initial hash tables come out of this zone so they can be allocated 1168355f576SJeff Roberson * prior to malloc coming up. 1178355f576SJeff Roberson */ 1188355f576SJeff Roberson static uma_zone_t hashzone; 1198355f576SJeff Roberson 1201e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */ 121e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1; 1221e319f6dSRobert Watson 123961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets"); 124961647dfSJeff Roberson 1258355f576SJeff Roberson /* 12686bbae32SJeff Roberson * Are we allowed to allocate buckets? 12786bbae32SJeff Roberson */ 12886bbae32SJeff Roberson static int bucketdisable = 1; 12986bbae32SJeff Roberson 130099a0e58SBosko Milekic /* Linked list of all kegs in the system */ 13113e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs); 1328355f576SJeff Roberson 133099a0e58SBosko Milekic /* This mutex protects the keg list */ 1340095a784SJeff Roberson static struct mtx_padalign uma_mtx; 1358355f576SJeff Roberson 1368355f576SJeff Roberson /* Linked list of boot time pages */ 1378355f576SJeff Roberson static LIST_HEAD(,uma_slab) uma_boot_pages = 13813e403fdSAntoine Brodin LIST_HEAD_INITIALIZER(uma_boot_pages); 1398355f576SJeff Roberson 140f353d338SAlan Cox /* This mutex protects the boot time pages list */ 1410095a784SJeff Roberson static struct mtx_padalign uma_boot_pages_mtx; 1428355f576SJeff Roberson 1438355f576SJeff Roberson /* Is the VM done starting up? */ 1448355f576SJeff Roberson static int booted = 0; 145342f1793SAlan Cox #define UMA_STARTUP 1 146342f1793SAlan Cox #define UMA_STARTUP2 2 1478355f576SJeff Roberson 148244f4554SBosko Milekic /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */ 149ef72505eSJeff Roberson static const u_int uma_max_ipers = SLAB_SETSIZE; 150ef72505eSJeff Roberson 151ef72505eSJeff Roberson /* 152ef72505eSJeff Roberson * Only mbuf clusters use ref zones. Just provide enough references 153ef72505eSJeff Roberson * to support the one user. New code should not use the ref facility. 154ef72505eSJeff Roberson */ 155ef72505eSJeff Roberson static const u_int uma_max_ipers_ref = PAGE_SIZE / MCLBYTES; 156244f4554SBosko Milekic 1579643769aSJeff Roberson /* 1589643769aSJeff Roberson * This is the handle used to schedule events that need to happen 1599643769aSJeff Roberson * outside of the allocation fast path. 1609643769aSJeff Roberson */ 1618355f576SJeff Roberson static struct callout uma_callout; 1629643769aSJeff Roberson #define UMA_TIMEOUT 20 /* Seconds for callout interval. */ 1638355f576SJeff Roberson 1648355f576SJeff Roberson /* 1658355f576SJeff Roberson * This structure is passed as the zone ctor arg so that I don't have to create 1668355f576SJeff Roberson * a special allocation function just for zones. 1678355f576SJeff Roberson */ 1688355f576SJeff Roberson struct uma_zctor_args { 169bb196eb4SMatthew D Fleming const char *name; 170c3bdc05fSAndrew R. Reiter size_t size; 1718355f576SJeff Roberson uma_ctor ctor; 1728355f576SJeff Roberson uma_dtor dtor; 1738355f576SJeff Roberson uma_init uminit; 1748355f576SJeff Roberson uma_fini fini; 1750095a784SJeff Roberson uma_import import; 1760095a784SJeff Roberson uma_release release; 1770095a784SJeff Roberson void *arg; 178099a0e58SBosko Milekic uma_keg_t keg; 179099a0e58SBosko Milekic int align; 18085dcf349SGleb Smirnoff uint32_t flags; 181099a0e58SBosko Milekic }; 182099a0e58SBosko Milekic 183099a0e58SBosko Milekic struct uma_kctor_args { 184099a0e58SBosko Milekic uma_zone_t zone; 185099a0e58SBosko Milekic size_t size; 186099a0e58SBosko Milekic uma_init uminit; 187099a0e58SBosko Milekic uma_fini fini; 1888355f576SJeff Roberson int align; 18985dcf349SGleb Smirnoff uint32_t flags; 1908355f576SJeff Roberson }; 1918355f576SJeff Roberson 192cae33c14SJeff Roberson struct uma_bucket_zone { 193cae33c14SJeff Roberson uma_zone_t ubz_zone; 194cae33c14SJeff Roberson char *ubz_name; 195*fc03d22bSJeff Roberson int ubz_entries; /* Number of items it can hold. */ 196*fc03d22bSJeff Roberson int ubz_maxsize; /* Maximum allocation size per-item. */ 197cae33c14SJeff Roberson }; 198cae33c14SJeff Roberson 199f9d27e75SRobert Watson /* 200*fc03d22bSJeff Roberson * Compute the actual number of bucket entries to pack them in power 201*fc03d22bSJeff Roberson * of two sizes for more efficient space utilization. 202f9d27e75SRobert Watson */ 203*fc03d22bSJeff Roberson #define BUCKET_SIZE(n) \ 204*fc03d22bSJeff Roberson (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *)) 205*fc03d22bSJeff Roberson 206*fc03d22bSJeff Roberson #define BUCKET_MAX BUCKET_SIZE(128) 207*fc03d22bSJeff Roberson 208*fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = { 209*fc03d22bSJeff Roberson { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 210*fc03d22bSJeff Roberson { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 211*fc03d22bSJeff Roberson { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 212*fc03d22bSJeff Roberson { NULL, NULL, 0} 213*fc03d22bSJeff Roberson }; 214*fc03d22bSJeff Roberson static uma_zone_t largebucket; 215cae33c14SJeff Roberson 2162019094aSRobert Watson /* 2172019094aSRobert Watson * Flags and enumerations to be passed to internal functions. 2182019094aSRobert Watson */ 219ef72505eSJeff Roberson enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI }; 220b23f72e9SBrian Feldman 2218355f576SJeff Roberson /* Prototypes.. */ 2228355f576SJeff Roberson 22385dcf349SGleb Smirnoff static void *noobj_alloc(uma_zone_t, int, uint8_t *, int); 22485dcf349SGleb Smirnoff static void *page_alloc(uma_zone_t, int, uint8_t *, int); 22585dcf349SGleb Smirnoff static void *startup_alloc(uma_zone_t, int, uint8_t *, int); 22685dcf349SGleb Smirnoff static void page_free(void *, int, uint8_t); 227e20a199fSJeff Roberson static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int); 2289643769aSJeff Roberson static void cache_drain(uma_zone_t); 2298355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t); 230aaa8bb16SJeff Roberson static void bucket_cache_drain(uma_zone_t zone); 231b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int); 232099a0e58SBosko Milekic static void keg_dtor(void *, int, void *); 233b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int); 2349c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *); 235b23f72e9SBrian Feldman static int zero_init(void *, int, int); 236e20a199fSJeff Roberson static void keg_small_init(uma_keg_t keg); 237e20a199fSJeff Roberson static void keg_large_init(uma_keg_t keg); 2388355f576SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t)); 2398355f576SJeff Roberson static void zone_timeout(uma_zone_t zone); 2400aef6126SJeff Roberson static int hash_alloc(struct uma_hash *); 2410aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *); 2420aef6126SJeff Roberson static void hash_free(struct uma_hash *hash); 2438355f576SJeff Roberson static void uma_timeout(void *); 2448355f576SJeff Roberson static void uma_startup3(void); 245e20a199fSJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int); 2460095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 24786bbae32SJeff Roberson static void bucket_enable(void); 248cae33c14SJeff Roberson static void bucket_init(void); 249cae33c14SJeff Roberson static uma_bucket_t bucket_alloc(int, int); 250cae33c14SJeff Roberson static void bucket_free(uma_bucket_t); 251cae33c14SJeff Roberson static void bucket_zone_drain(void); 252*fc03d22bSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t zone, int flags); 253e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags); 254e20a199fSJeff Roberson static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags); 2550095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 2560095a784SJeff Roberson static void slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item); 257e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 25885dcf349SGleb Smirnoff uma_fini fini, int align, uint32_t flags); 259e20a199fSJeff Roberson static inline void zone_relock(uma_zone_t zone, uma_keg_t keg); 260e20a199fSJeff Roberson static inline void keg_relock(uma_keg_t keg, uma_zone_t zone); 2610095a784SJeff Roberson static int zone_import(uma_zone_t zone, void **bucket, int max, int flags); 2620095a784SJeff Roberson static void zone_release(uma_zone_t zone, void **bucket, int cnt); 263bbee39c6SJeff Roberson 2648355f576SJeff Roberson void uma_print_zone(uma_zone_t); 2658355f576SJeff Roberson void uma_print_stats(void); 2667a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 2677a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 2688355f576SJeff Roberson 2698355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 2708355f576SJeff Roberson 2717a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT, 2727a52a97eSRobert Watson 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 2737a52a97eSRobert Watson 2747a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 2757a52a97eSRobert Watson 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 2767a52a97eSRobert Watson 2772f891cd5SPawel Jakub Dawidek static int zone_warnings = 1; 2782f891cd5SPawel Jakub Dawidek TUNABLE_INT("vm.zone_warnings", &zone_warnings); 2792f891cd5SPawel Jakub Dawidek SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RW, &zone_warnings, 0, 2802f891cd5SPawel Jakub Dawidek "Warn when UMA zones becomes full"); 2812f891cd5SPawel Jakub Dawidek 28286bbae32SJeff Roberson /* 28386bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 28486bbae32SJeff Roberson */ 28586bbae32SJeff Roberson static void 28686bbae32SJeff Roberson bucket_enable(void) 28786bbae32SJeff Roberson { 288251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 28986bbae32SJeff Roberson } 29086bbae32SJeff Roberson 291dc2c7965SRobert Watson /* 292dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 293dc2c7965SRobert Watson * 294dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 295*fc03d22bSJeff Roberson * of the header and an array of pointers. 296dc2c7965SRobert Watson */ 297cae33c14SJeff Roberson static void 298cae33c14SJeff Roberson bucket_init(void) 299cae33c14SJeff Roberson { 300cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 301cae33c14SJeff Roberson int size; 302*fc03d22bSJeff Roberson int i; 303cae33c14SJeff Roberson 304*fc03d22bSJeff Roberson for (i = 0, ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 305cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 306cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 307cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 308e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 309*fc03d22bSJeff Roberson UMA_ZONE_MAXBUCKET | UMA_ZONE_MTXCLASS); 310cae33c14SJeff Roberson } 311*fc03d22bSJeff Roberson /* 312*fc03d22bSJeff Roberson * To avoid recursive bucket allocation loops we disable buckets 313*fc03d22bSJeff Roberson * on the smallest bucket zone and use it for the largest zone. 314*fc03d22bSJeff Roberson * The remainder of the zones all use the largest zone. 315*fc03d22bSJeff Roberson */ 316*fc03d22bSJeff Roberson ubz--; 317*fc03d22bSJeff Roberson ubz->ubz_zone->uz_count = bucket_zones[0].ubz_entries; 318*fc03d22bSJeff Roberson bucket_zones[0].ubz_zone->uz_count = 0; 319*fc03d22bSJeff Roberson largebucket = ubz->ubz_zone; 320cae33c14SJeff Roberson } 321cae33c14SJeff Roberson 322dc2c7965SRobert Watson /* 323dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 324dc2c7965SRobert Watson * to allocate the bucket. 325dc2c7965SRobert Watson */ 326dc2c7965SRobert Watson static struct uma_bucket_zone * 327dc2c7965SRobert Watson bucket_zone_lookup(int entries) 328dc2c7965SRobert Watson { 329*fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 330dc2c7965SRobert Watson 331*fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 332*fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 333*fc03d22bSJeff Roberson return (ubz); 334*fc03d22bSJeff Roberson ubz--; 335*fc03d22bSJeff Roberson return (ubz); 336*fc03d22bSJeff Roberson } 337*fc03d22bSJeff Roberson 338*fc03d22bSJeff Roberson static int 339*fc03d22bSJeff Roberson bucket_select(int size) 340*fc03d22bSJeff Roberson { 341*fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 342*fc03d22bSJeff Roberson 343*fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 344*fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 345*fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 346*fc03d22bSJeff Roberson 347*fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 348*fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 349*fc03d22bSJeff Roberson break; 350*fc03d22bSJeff Roberson ubz--; 351*fc03d22bSJeff Roberson return (ubz->ubz_entries); 352dc2c7965SRobert Watson } 353dc2c7965SRobert Watson 354cae33c14SJeff Roberson static uma_bucket_t 355cae33c14SJeff Roberson bucket_alloc(int entries, int bflags) 356cae33c14SJeff Roberson { 357cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 358cae33c14SJeff Roberson uma_bucket_t bucket; 359cae33c14SJeff Roberson 360cae33c14SJeff Roberson /* 361cae33c14SJeff Roberson * This is to stop us from allocating per cpu buckets while we're 3623803b26bSDag-Erling Smørgrav * running out of vm.boot_pages. Otherwise, we would exhaust the 363cae33c14SJeff Roberson * boot pages. This also prevents us from allocating buckets in 364cae33c14SJeff Roberson * low memory situations. 365cae33c14SJeff Roberson */ 366cae33c14SJeff Roberson if (bucketdisable) 367cae33c14SJeff Roberson return (NULL); 368dc2c7965SRobert Watson 369dc2c7965SRobert Watson ubz = bucket_zone_lookup(entries); 370*fc03d22bSJeff Roberson bucket = uma_zalloc(ubz->ubz_zone, bflags); 371cae33c14SJeff Roberson if (bucket) { 372cae33c14SJeff Roberson #ifdef INVARIANTS 373cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 374cae33c14SJeff Roberson #endif 375cae33c14SJeff Roberson bucket->ub_cnt = 0; 376cae33c14SJeff Roberson bucket->ub_entries = ubz->ubz_entries; 377cae33c14SJeff Roberson } 378cae33c14SJeff Roberson 379cae33c14SJeff Roberson return (bucket); 380cae33c14SJeff Roberson } 381cae33c14SJeff Roberson 382cae33c14SJeff Roberson static void 383cae33c14SJeff Roberson bucket_free(uma_bucket_t bucket) 384cae33c14SJeff Roberson { 385cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 386cae33c14SJeff Roberson 387*fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 388*fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 389dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 390*fc03d22bSJeff Roberson uma_zfree(ubz->ubz_zone, bucket); 391cae33c14SJeff Roberson } 392cae33c14SJeff Roberson 393cae33c14SJeff Roberson static void 394cae33c14SJeff Roberson bucket_zone_drain(void) 395cae33c14SJeff Roberson { 396cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 397cae33c14SJeff Roberson 398cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 399cae33c14SJeff Roberson zone_drain(ubz->ubz_zone); 400cae33c14SJeff Roberson } 401cae33c14SJeff Roberson 4022f891cd5SPawel Jakub Dawidek static void 4032f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 4042f891cd5SPawel Jakub Dawidek { 4052f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 4062f891cd5SPawel Jakub Dawidek 4072f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 4082f891cd5SPawel Jakub Dawidek return; 4092f891cd5SPawel Jakub Dawidek 4102f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 4112f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 4122f891cd5SPawel Jakub Dawidek } 4132f891cd5SPawel Jakub Dawidek 414e20a199fSJeff Roberson static void 415e20a199fSJeff Roberson zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t)) 416e20a199fSJeff Roberson { 417e20a199fSJeff Roberson uma_klink_t klink; 418e20a199fSJeff Roberson 419e20a199fSJeff Roberson LIST_FOREACH(klink, &zone->uz_kegs, kl_link) 420e20a199fSJeff Roberson kegfn(klink->kl_keg); 421e20a199fSJeff Roberson } 4228355f576SJeff Roberson 4238355f576SJeff Roberson /* 4248355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 4259643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 4268355f576SJeff Roberson * 4278355f576SJeff Roberson * Arguments: 4288355f576SJeff Roberson * arg Unused 4298355f576SJeff Roberson * 4308355f576SJeff Roberson * Returns: 4318355f576SJeff Roberson * Nothing 4328355f576SJeff Roberson */ 4338355f576SJeff Roberson static void 4348355f576SJeff Roberson uma_timeout(void *unused) 4358355f576SJeff Roberson { 43686bbae32SJeff Roberson bucket_enable(); 4378355f576SJeff Roberson zone_foreach(zone_timeout); 4388355f576SJeff Roberson 4398355f576SJeff Roberson /* Reschedule this event */ 4409643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 4418355f576SJeff Roberson } 4428355f576SJeff Roberson 4438355f576SJeff Roberson /* 4449643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 4459643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 4468355f576SJeff Roberson * 447e20a199fSJeff Roberson * Returns nothing. 4488355f576SJeff Roberson */ 4498355f576SJeff Roberson static void 450e20a199fSJeff Roberson keg_timeout(uma_keg_t keg) 4518355f576SJeff Roberson { 4528355f576SJeff Roberson 453e20a199fSJeff Roberson KEG_LOCK(keg); 4548355f576SJeff Roberson /* 455e20a199fSJeff Roberson * Expand the keg hash table. 4568355f576SJeff Roberson * 4578355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 4588355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 4598355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 4608355f576SJeff Roberson */ 461099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH && 462099a0e58SBosko Milekic keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) { 4630aef6126SJeff Roberson struct uma_hash newhash; 4640aef6126SJeff Roberson struct uma_hash oldhash; 4650aef6126SJeff Roberson int ret; 4665300d9ddSJeff Roberson 4670aef6126SJeff Roberson /* 4680aef6126SJeff Roberson * This is so involved because allocating and freeing 469e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 4700aef6126SJeff Roberson * I have to do everything in stages and check for 4710aef6126SJeff Roberson * races. 4720aef6126SJeff Roberson */ 473099a0e58SBosko Milekic newhash = keg->uk_hash; 474e20a199fSJeff Roberson KEG_UNLOCK(keg); 4750aef6126SJeff Roberson ret = hash_alloc(&newhash); 476e20a199fSJeff Roberson KEG_LOCK(keg); 4770aef6126SJeff Roberson if (ret) { 478099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 479099a0e58SBosko Milekic oldhash = keg->uk_hash; 480099a0e58SBosko Milekic keg->uk_hash = newhash; 4810aef6126SJeff Roberson } else 4820aef6126SJeff Roberson oldhash = newhash; 4830aef6126SJeff Roberson 484e20a199fSJeff Roberson KEG_UNLOCK(keg); 4850aef6126SJeff Roberson hash_free(&oldhash); 486e20a199fSJeff Roberson KEG_LOCK(keg); 4870aef6126SJeff Roberson } 4885300d9ddSJeff Roberson } 489e20a199fSJeff Roberson KEG_UNLOCK(keg); 490e20a199fSJeff Roberson } 491e20a199fSJeff Roberson 492e20a199fSJeff Roberson static void 493e20a199fSJeff Roberson zone_timeout(uma_zone_t zone) 494e20a199fSJeff Roberson { 495e20a199fSJeff Roberson 496e20a199fSJeff Roberson zone_foreach_keg(zone, &keg_timeout); 4978355f576SJeff Roberson } 4988355f576SJeff Roberson 4998355f576SJeff Roberson /* 5005300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 5015300d9ddSJeff Roberson * backing store. 5025300d9ddSJeff Roberson * 5035300d9ddSJeff Roberson * Arguments: 5040aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 5055300d9ddSJeff Roberson * 5065300d9ddSJeff Roberson * Returns: 5070aef6126SJeff Roberson * 1 on sucess and 0 on failure. 5085300d9ddSJeff Roberson */ 50937c84183SPoul-Henning Kamp static int 5100aef6126SJeff Roberson hash_alloc(struct uma_hash *hash) 5115300d9ddSJeff Roberson { 5120aef6126SJeff Roberson int oldsize; 5135300d9ddSJeff Roberson int alloc; 5145300d9ddSJeff Roberson 5150aef6126SJeff Roberson oldsize = hash->uh_hashsize; 5160aef6126SJeff Roberson 5175300d9ddSJeff Roberson /* We're just going to go to a power of two greater */ 5180aef6126SJeff Roberson if (oldsize) { 5190aef6126SJeff Roberson hash->uh_hashsize = oldsize * 2; 5200aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 5210aef6126SJeff Roberson hash->uh_slab_hash = (struct slabhead *)malloc(alloc, 522961647dfSJeff Roberson M_UMAHASH, M_NOWAIT); 5235300d9ddSJeff Roberson } else { 5240aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 525e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 526a163d034SWarner Losh M_WAITOK); 5270aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 5285300d9ddSJeff Roberson } 5290aef6126SJeff Roberson if (hash->uh_slab_hash) { 5300aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 5310aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 5320aef6126SJeff Roberson return (1); 5330aef6126SJeff Roberson } 5345300d9ddSJeff Roberson 5350aef6126SJeff Roberson return (0); 5365300d9ddSJeff Roberson } 5375300d9ddSJeff Roberson 5385300d9ddSJeff Roberson /* 53964f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 54064f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 54164f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 5428355f576SJeff Roberson * 5438355f576SJeff Roberson * Arguments: 5440aef6126SJeff Roberson * oldhash The hash you want to expand 5450aef6126SJeff Roberson * newhash The hash structure for the new table 5468355f576SJeff Roberson * 5478355f576SJeff Roberson * Returns: 5488355f576SJeff Roberson * Nothing 5498355f576SJeff Roberson * 5508355f576SJeff Roberson * Discussion: 5518355f576SJeff Roberson */ 5520aef6126SJeff Roberson static int 5530aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 5548355f576SJeff Roberson { 5558355f576SJeff Roberson uma_slab_t slab; 5568355f576SJeff Roberson int hval; 5578355f576SJeff Roberson int i; 5588355f576SJeff Roberson 5590aef6126SJeff Roberson if (!newhash->uh_slab_hash) 5600aef6126SJeff Roberson return (0); 5618355f576SJeff Roberson 5620aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 5630aef6126SJeff Roberson return (0); 5648355f576SJeff Roberson 5658355f576SJeff Roberson /* 5668355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 5678355f576SJeff Roberson * full rehash. 5688355f576SJeff Roberson */ 5698355f576SJeff Roberson 5700aef6126SJeff Roberson for (i = 0; i < oldhash->uh_hashsize; i++) 5710aef6126SJeff Roberson while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) { 5720aef6126SJeff Roberson slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]); 5730aef6126SJeff Roberson SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink); 5740aef6126SJeff Roberson hval = UMA_HASH(newhash, slab->us_data); 5750aef6126SJeff Roberson SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 5760aef6126SJeff Roberson slab, us_hlink); 5778355f576SJeff Roberson } 5788355f576SJeff Roberson 5790aef6126SJeff Roberson return (1); 5809c2cd7e5SJeff Roberson } 5819c2cd7e5SJeff Roberson 5825300d9ddSJeff Roberson /* 5835300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 5845300d9ddSJeff Roberson * 5855300d9ddSJeff Roberson * Arguments: 5865300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 5875300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 5885300d9ddSJeff Roberson * 5895300d9ddSJeff Roberson * Returns: 5905300d9ddSJeff Roberson * Nothing 5915300d9ddSJeff Roberson */ 5929c2cd7e5SJeff Roberson static void 5930aef6126SJeff Roberson hash_free(struct uma_hash *hash) 5949c2cd7e5SJeff Roberson { 5950aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 5960aef6126SJeff Roberson return; 5970aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 5980095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 5998355f576SJeff Roberson else 600961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 6018355f576SJeff Roberson } 6028355f576SJeff Roberson 6038355f576SJeff Roberson /* 6048355f576SJeff Roberson * Frees all outstanding items in a bucket 6058355f576SJeff Roberson * 6068355f576SJeff Roberson * Arguments: 6078355f576SJeff Roberson * zone The zone to free to, must be unlocked. 6088355f576SJeff Roberson * bucket The free/alloc bucket with items, cpu queue must be locked. 6098355f576SJeff Roberson * 6108355f576SJeff Roberson * Returns: 6118355f576SJeff Roberson * Nothing 6128355f576SJeff Roberson */ 6138355f576SJeff Roberson 6148355f576SJeff Roberson static void 6158355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 6168355f576SJeff Roberson { 6170095a784SJeff Roberson int i; 6188355f576SJeff Roberson 6198355f576SJeff Roberson if (bucket == NULL) 6208355f576SJeff Roberson return; 6218355f576SJeff Roberson 6220095a784SJeff Roberson if (zone->uz_fini) 6230095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 6240095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 6250095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 6260095a784SJeff Roberson bucket->ub_cnt = 0; 6278355f576SJeff Roberson } 6288355f576SJeff Roberson 6298355f576SJeff Roberson /* 6308355f576SJeff Roberson * Drains the per cpu caches for a zone. 6318355f576SJeff Roberson * 6325d1ae027SRobert Watson * NOTE: This may only be called while the zone is being turn down, and not 6335d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 6345d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 6355d1ae027SRobert Watson * 6368355f576SJeff Roberson * Arguments: 6378355f576SJeff Roberson * zone The zone to drain, must be unlocked. 6388355f576SJeff Roberson * 6398355f576SJeff Roberson * Returns: 6408355f576SJeff Roberson * Nothing 6418355f576SJeff Roberson */ 6428355f576SJeff Roberson static void 6439643769aSJeff Roberson cache_drain(uma_zone_t zone) 6448355f576SJeff Roberson { 6458355f576SJeff Roberson uma_cache_t cache; 6468355f576SJeff Roberson int cpu; 6478355f576SJeff Roberson 6488355f576SJeff Roberson /* 6495d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 6505d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 6515d1ae027SRobert Watson * of the caches at this point. 6525d1ae027SRobert Watson * 6535d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 6545d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 6555d1ae027SRobert Watson * 6565d1ae027SRobert Watson * XXX: We lock the zone before passing into bucket_cache_drain() as 6575d1ae027SRobert Watson * it is used elsewhere. Should the tear-down path be made special 6585d1ae027SRobert Watson * there in some form? 6598355f576SJeff Roberson */ 6603aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 6618355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 6628355f576SJeff Roberson bucket_drain(zone, cache->uc_allocbucket); 6638355f576SJeff Roberson bucket_drain(zone, cache->uc_freebucket); 664174ab450SBosko Milekic if (cache->uc_allocbucket != NULL) 665cae33c14SJeff Roberson bucket_free(cache->uc_allocbucket); 666174ab450SBosko Milekic if (cache->uc_freebucket != NULL) 667cae33c14SJeff Roberson bucket_free(cache->uc_freebucket); 668d56368d7SBosko Milekic cache->uc_allocbucket = cache->uc_freebucket = NULL; 669d56368d7SBosko Milekic } 670aaa8bb16SJeff Roberson ZONE_LOCK(zone); 671aaa8bb16SJeff Roberson bucket_cache_drain(zone); 672aaa8bb16SJeff Roberson ZONE_UNLOCK(zone); 673aaa8bb16SJeff Roberson } 674aaa8bb16SJeff Roberson 675aaa8bb16SJeff Roberson /* 676aaa8bb16SJeff Roberson * Drain the cached buckets from a zone. Expects a locked zone on entry. 677aaa8bb16SJeff Roberson */ 678aaa8bb16SJeff Roberson static void 679aaa8bb16SJeff Roberson bucket_cache_drain(uma_zone_t zone) 680aaa8bb16SJeff Roberson { 681aaa8bb16SJeff Roberson uma_bucket_t bucket; 6828355f576SJeff Roberson 6838355f576SJeff Roberson /* 6848355f576SJeff Roberson * Drain the bucket queues and free the buckets, we just keep two per 6858355f576SJeff Roberson * cpu (alloc/free). 6868355f576SJeff Roberson */ 687*fc03d22bSJeff Roberson while ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) { 6888355f576SJeff Roberson LIST_REMOVE(bucket, ub_link); 6898355f576SJeff Roberson ZONE_UNLOCK(zone); 6908355f576SJeff Roberson bucket_drain(zone, bucket); 691cae33c14SJeff Roberson bucket_free(bucket); 6928355f576SJeff Roberson ZONE_LOCK(zone); 6938355f576SJeff Roberson } 6948355f576SJeff Roberson } 695*fc03d22bSJeff Roberson 696*fc03d22bSJeff Roberson static void 697*fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 698*fc03d22bSJeff Roberson { 699*fc03d22bSJeff Roberson uint8_t *mem; 700*fc03d22bSJeff Roberson int i; 701*fc03d22bSJeff Roberson uint8_t flags; 702*fc03d22bSJeff Roberson 703*fc03d22bSJeff Roberson mem = slab->us_data; 704*fc03d22bSJeff Roberson flags = slab->us_flags; 705*fc03d22bSJeff Roberson i = start; 706*fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 707*fc03d22bSJeff Roberson for (i--; i > -1; i--) 708*fc03d22bSJeff Roberson keg->uk_fini(slab->us_data + (keg->uk_rsize * i), 709*fc03d22bSJeff Roberson keg->uk_size); 710*fc03d22bSJeff Roberson } 711*fc03d22bSJeff Roberson if (keg->uk_flags & UMA_ZONE_VTOSLAB) { 712*fc03d22bSJeff Roberson vm_object_t obj; 713*fc03d22bSJeff Roberson 714*fc03d22bSJeff Roberson if (flags & UMA_SLAB_KMEM) 715*fc03d22bSJeff Roberson obj = kmem_object; 716*fc03d22bSJeff Roberson else if (flags & UMA_SLAB_KERNEL) 717*fc03d22bSJeff Roberson obj = kernel_object; 718*fc03d22bSJeff Roberson else 719*fc03d22bSJeff Roberson obj = NULL; 720*fc03d22bSJeff Roberson for (i = 0; i < keg->uk_ppera; i++) 721*fc03d22bSJeff Roberson vsetobj((vm_offset_t)mem + (i * PAGE_SIZE), obj); 722*fc03d22bSJeff Roberson } 723*fc03d22bSJeff Roberson if (keg->uk_flags & UMA_ZONE_OFFPAGE) 724*fc03d22bSJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 725*fc03d22bSJeff Roberson #ifdef UMA_DEBUG 726*fc03d22bSJeff Roberson printf("%s: Returning %d bytes.\n", keg->uk_name, 727*fc03d22bSJeff Roberson PAGE_SIZE * keg->uk_ppera); 728*fc03d22bSJeff Roberson #endif 729*fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 7308355f576SJeff Roberson } 7318355f576SJeff Roberson 7328355f576SJeff Roberson /* 733e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 7348355f576SJeff Roberson * the pageout daemon. 7358355f576SJeff Roberson * 736e20a199fSJeff Roberson * Returns nothing. 7378355f576SJeff Roberson */ 738e20a199fSJeff Roberson static void 739e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 7408355f576SJeff Roberson { 7411e183df2SStefan Farfeleder struct slabhead freeslabs = { 0 }; 7428355f576SJeff Roberson uma_slab_t slab; 7438355f576SJeff Roberson uma_slab_t n; 7448355f576SJeff Roberson 7458355f576SJeff Roberson /* 746e20a199fSJeff Roberson * We don't want to take pages from statically allocated kegs at this 7478355f576SJeff Roberson * time 7488355f576SJeff Roberson */ 749099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 7508355f576SJeff Roberson return; 7518355f576SJeff Roberson 7528355f576SJeff Roberson #ifdef UMA_DEBUG 753e20a199fSJeff Roberson printf("%s free items: %u\n", keg->uk_name, keg->uk_free); 7548355f576SJeff Roberson #endif 755e20a199fSJeff Roberson KEG_LOCK(keg); 756099a0e58SBosko Milekic if (keg->uk_free == 0) 7578355f576SJeff Roberson goto finished; 7588355f576SJeff Roberson 759099a0e58SBosko Milekic slab = LIST_FIRST(&keg->uk_free_slab); 7609643769aSJeff Roberson while (slab) { 7618355f576SJeff Roberson n = LIST_NEXT(slab, us_link); 7628355f576SJeff Roberson 7638355f576SJeff Roberson /* We have no where to free these to */ 7648355f576SJeff Roberson if (slab->us_flags & UMA_SLAB_BOOT) { 7658355f576SJeff Roberson slab = n; 7668355f576SJeff Roberson continue; 7678355f576SJeff Roberson } 7688355f576SJeff Roberson 7698355f576SJeff Roberson LIST_REMOVE(slab, us_link); 770099a0e58SBosko Milekic keg->uk_pages -= keg->uk_ppera; 771099a0e58SBosko Milekic keg->uk_free -= keg->uk_ipers; 772713deb36SJeff Roberson 773099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 774099a0e58SBosko Milekic UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data); 775713deb36SJeff Roberson 776713deb36SJeff Roberson SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink); 777713deb36SJeff Roberson 778713deb36SJeff Roberson slab = n; 779713deb36SJeff Roberson } 780713deb36SJeff Roberson finished: 781e20a199fSJeff Roberson KEG_UNLOCK(keg); 782713deb36SJeff Roberson 783713deb36SJeff Roberson while ((slab = SLIST_FIRST(&freeslabs)) != NULL) { 784713deb36SJeff Roberson SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink); 785*fc03d22bSJeff Roberson keg_free_slab(keg, slab, 0); 7868355f576SJeff Roberson } 7878355f576SJeff Roberson } 7888355f576SJeff Roberson 789e20a199fSJeff Roberson static void 790e20a199fSJeff Roberson zone_drain_wait(uma_zone_t zone, int waitok) 791e20a199fSJeff Roberson { 792e20a199fSJeff Roberson 7938355f576SJeff Roberson /* 794e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 795e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 796e20a199fSJeff Roberson * is the only call that knows the structure will still be available 797e20a199fSJeff Roberson * when it wakes up. 798e20a199fSJeff Roberson */ 799e20a199fSJeff Roberson ZONE_LOCK(zone); 800e20a199fSJeff Roberson while (zone->uz_flags & UMA_ZFLAG_DRAINING) { 801e20a199fSJeff Roberson if (waitok == M_NOWAIT) 802e20a199fSJeff Roberson goto out; 803e20a199fSJeff Roberson mtx_unlock(&uma_mtx); 804e20a199fSJeff Roberson msleep(zone, zone->uz_lock, PVM, "zonedrain", 1); 805e20a199fSJeff Roberson mtx_lock(&uma_mtx); 806e20a199fSJeff Roberson } 807e20a199fSJeff Roberson zone->uz_flags |= UMA_ZFLAG_DRAINING; 808e20a199fSJeff Roberson bucket_cache_drain(zone); 809e20a199fSJeff Roberson ZONE_UNLOCK(zone); 810e20a199fSJeff Roberson /* 811e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 812e20a199fSJeff Roberson * we're running. Normally the uma_mtx would protect us but we 813e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 814e20a199fSJeff Roberson */ 815e20a199fSJeff Roberson zone_foreach_keg(zone, &keg_drain); 816e20a199fSJeff Roberson ZONE_LOCK(zone); 817e20a199fSJeff Roberson zone->uz_flags &= ~UMA_ZFLAG_DRAINING; 818e20a199fSJeff Roberson wakeup(zone); 819e20a199fSJeff Roberson out: 820e20a199fSJeff Roberson ZONE_UNLOCK(zone); 821e20a199fSJeff Roberson } 822e20a199fSJeff Roberson 823e20a199fSJeff Roberson void 824e20a199fSJeff Roberson zone_drain(uma_zone_t zone) 825e20a199fSJeff Roberson { 826e20a199fSJeff Roberson 827e20a199fSJeff Roberson zone_drain_wait(zone, M_NOWAIT); 828e20a199fSJeff Roberson } 829e20a199fSJeff Roberson 830e20a199fSJeff Roberson /* 831e20a199fSJeff Roberson * Allocate a new slab for a keg. This does not insert the slab onto a list. 8328355f576SJeff Roberson * 8338355f576SJeff Roberson * Arguments: 8348355f576SJeff Roberson * wait Shall we wait? 8358355f576SJeff Roberson * 8368355f576SJeff Roberson * Returns: 8378355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 8388355f576SJeff Roberson * caller specified M_NOWAIT. 8398355f576SJeff Roberson */ 8408355f576SJeff Roberson static uma_slab_t 841e20a199fSJeff Roberson keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait) 8428355f576SJeff Roberson { 843099a0e58SBosko Milekic uma_slabrefcnt_t slabref; 844e20a199fSJeff Roberson uma_alloc allocf; 845099a0e58SBosko Milekic uma_slab_t slab; 84685dcf349SGleb Smirnoff uint8_t *mem; 84785dcf349SGleb Smirnoff uint8_t flags; 8488355f576SJeff Roberson int i; 8498355f576SJeff Roberson 850e20a199fSJeff Roberson mtx_assert(&keg->uk_lock, MA_OWNED); 851a553d4b8SJeff Roberson slab = NULL; 852*fc03d22bSJeff Roberson mem = NULL; 853a553d4b8SJeff Roberson 8548355f576SJeff Roberson #ifdef UMA_DEBUG 8550095a784SJeff Roberson printf("alloc_slab: Allocating a new slab for %s\n", keg->uk_name); 8568355f576SJeff Roberson #endif 857e20a199fSJeff Roberson allocf = keg->uk_allocf; 858e20a199fSJeff Roberson KEG_UNLOCK(keg); 859a553d4b8SJeff Roberson 860099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_OFFPAGE) { 861e20a199fSJeff Roberson slab = zone_alloc_item(keg->uk_slabzone, NULL, wait); 862*fc03d22bSJeff Roberson if (slab == NULL) 863*fc03d22bSJeff Roberson goto out; 864a553d4b8SJeff Roberson } 865a553d4b8SJeff Roberson 8663370c5bfSJeff Roberson /* 8673370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 8683370c5bfSJeff Roberson * first time they are added to a zone. 8693370c5bfSJeff Roberson * 8703370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 8713370c5bfSJeff Roberson */ 8723370c5bfSJeff Roberson 873099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 8743370c5bfSJeff Roberson wait |= M_ZERO; 8753370c5bfSJeff Roberson else 8763370c5bfSJeff Roberson wait &= ~M_ZERO; 8773370c5bfSJeff Roberson 878263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 879263811f7SKip Macy wait |= M_NODUMP; 880263811f7SKip Macy 881e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 882ad97af7eSGleb Smirnoff mem = allocf(zone, keg->uk_ppera * PAGE_SIZE, &flags, wait); 883a553d4b8SJeff Roberson if (mem == NULL) { 884b23f72e9SBrian Feldman if (keg->uk_flags & UMA_ZONE_OFFPAGE) 8850095a784SJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 886*fc03d22bSJeff Roberson slab = NULL; 887*fc03d22bSJeff Roberson goto out; 888a553d4b8SJeff Roberson } 8898355f576SJeff Roberson 8905c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 891099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) 892099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 8935c0e403bSJeff Roberson 894e20a199fSJeff Roberson if (keg->uk_flags & UMA_ZONE_VTOSLAB) 895099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 89699571dc3SJeff Roberson vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab); 8978355f576SJeff Roberson 898099a0e58SBosko Milekic slab->us_keg = keg; 8998355f576SJeff Roberson slab->us_data = mem; 900099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 9018355f576SJeff Roberson slab->us_flags = flags; 902ef72505eSJeff Roberson BIT_FILL(SLAB_SETSIZE, &slab->us_free); 903ef72505eSJeff Roberson #ifdef INVARIANTS 904ef72505eSJeff Roberson BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree); 905ef72505eSJeff Roberson #endif 906099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_REFCNT) { 907099a0e58SBosko Milekic slabref = (uma_slabrefcnt_t)slab; 908ab14a3f7SBrian Feldman for (i = 0; i < keg->uk_ipers; i++) 909ef72505eSJeff Roberson slabref->us_refcnt[i] = 0; 910099a0e58SBosko Milekic } 911099a0e58SBosko Milekic 912b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 913099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 914b23f72e9SBrian Feldman if (keg->uk_init(slab->us_data + (keg->uk_rsize * i), 915b23f72e9SBrian Feldman keg->uk_size, wait) != 0) 916b23f72e9SBrian Feldman break; 917b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 918*fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 919*fc03d22bSJeff Roberson slab = NULL; 920*fc03d22bSJeff Roberson goto out; 921b23f72e9SBrian Feldman } 922b23f72e9SBrian Feldman } 923*fc03d22bSJeff Roberson out: 924e20a199fSJeff Roberson KEG_LOCK(keg); 9255c0e403bSJeff Roberson 926*fc03d22bSJeff Roberson if (slab != NULL) { 927099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 928099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 9298355f576SJeff Roberson 930099a0e58SBosko Milekic keg->uk_pages += keg->uk_ppera; 931099a0e58SBosko Milekic keg->uk_free += keg->uk_ipers; 932*fc03d22bSJeff Roberson } 9338355f576SJeff Roberson 9348355f576SJeff Roberson return (slab); 9358355f576SJeff Roberson } 9368355f576SJeff Roberson 9378355f576SJeff Roberson /* 938009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 939009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 940009b6fcbSJeff Roberson * the VM is ready. 941009b6fcbSJeff Roberson */ 942009b6fcbSJeff Roberson static void * 94385dcf349SGleb Smirnoff startup_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait) 944009b6fcbSJeff Roberson { 945099a0e58SBosko Milekic uma_keg_t keg; 946f353d338SAlan Cox uma_slab_t tmps; 947e9a069d8SJohn Baldwin int pages, check_pages; 948099a0e58SBosko Milekic 949e20a199fSJeff Roberson keg = zone_first_keg(zone); 950e9a069d8SJohn Baldwin pages = howmany(bytes, PAGE_SIZE); 951e9a069d8SJohn Baldwin check_pages = pages - 1; 952e9a069d8SJohn Baldwin KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n")); 953099a0e58SBosko Milekic 954009b6fcbSJeff Roberson /* 955009b6fcbSJeff Roberson * Check our small startup cache to see if it has pages remaining. 956009b6fcbSJeff Roberson */ 957f353d338SAlan Cox mtx_lock(&uma_boot_pages_mtx); 958e9a069d8SJohn Baldwin 959e9a069d8SJohn Baldwin /* First check if we have enough room. */ 960e9a069d8SJohn Baldwin tmps = LIST_FIRST(&uma_boot_pages); 961e9a069d8SJohn Baldwin while (tmps != NULL && check_pages-- > 0) 962e9a069d8SJohn Baldwin tmps = LIST_NEXT(tmps, us_link); 963e9a069d8SJohn Baldwin if (tmps != NULL) { 964e9a069d8SJohn Baldwin /* 965e9a069d8SJohn Baldwin * It's ok to lose tmps references. The last one will 966e9a069d8SJohn Baldwin * have tmps->us_data pointing to the start address of 967e9a069d8SJohn Baldwin * "pages" contiguous pages of memory. 968e9a069d8SJohn Baldwin */ 969e9a069d8SJohn Baldwin while (pages-- > 0) { 970e9a069d8SJohn Baldwin tmps = LIST_FIRST(&uma_boot_pages); 971009b6fcbSJeff Roberson LIST_REMOVE(tmps, us_link); 972e9a069d8SJohn Baldwin } 973f353d338SAlan Cox mtx_unlock(&uma_boot_pages_mtx); 974009b6fcbSJeff Roberson *pflag = tmps->us_flags; 975009b6fcbSJeff Roberson return (tmps->us_data); 976009b6fcbSJeff Roberson } 977f353d338SAlan Cox mtx_unlock(&uma_boot_pages_mtx); 978342f1793SAlan Cox if (booted < UMA_STARTUP2) 9793803b26bSDag-Erling Smørgrav panic("UMA: Increase vm.boot_pages"); 980009b6fcbSJeff Roberson /* 981009b6fcbSJeff Roberson * Now that we've booted reset these users to their real allocator. 982009b6fcbSJeff Roberson */ 983009b6fcbSJeff Roberson #ifdef UMA_MD_SMALL_ALLOC 984e9a069d8SJohn Baldwin keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc; 985009b6fcbSJeff Roberson #else 986099a0e58SBosko Milekic keg->uk_allocf = page_alloc; 987009b6fcbSJeff Roberson #endif 988099a0e58SBosko Milekic return keg->uk_allocf(zone, bytes, pflag, wait); 989009b6fcbSJeff Roberson } 990009b6fcbSJeff Roberson 991009b6fcbSJeff Roberson /* 9928355f576SJeff Roberson * Allocates a number of pages from the system 9938355f576SJeff Roberson * 9948355f576SJeff Roberson * Arguments: 9958355f576SJeff Roberson * bytes The number of bytes requested 9968355f576SJeff Roberson * wait Shall we wait? 9978355f576SJeff Roberson * 9988355f576SJeff Roberson * Returns: 9998355f576SJeff Roberson * A pointer to the alloced memory or possibly 10008355f576SJeff Roberson * NULL if M_NOWAIT is set. 10018355f576SJeff Roberson */ 10028355f576SJeff Roberson static void * 100385dcf349SGleb Smirnoff page_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait) 10048355f576SJeff Roberson { 10058355f576SJeff Roberson void *p; /* Returned page */ 10068355f576SJeff Roberson 10078355f576SJeff Roberson *pflag = UMA_SLAB_KMEM; 10088355f576SJeff Roberson p = (void *) kmem_malloc(kmem_map, bytes, wait); 10098355f576SJeff Roberson 10108355f576SJeff Roberson return (p); 10118355f576SJeff Roberson } 10128355f576SJeff Roberson 10138355f576SJeff Roberson /* 10148355f576SJeff Roberson * Allocates a number of pages from within an object 10158355f576SJeff Roberson * 10168355f576SJeff Roberson * Arguments: 10178355f576SJeff Roberson * bytes The number of bytes requested 10188355f576SJeff Roberson * wait Shall we wait? 10198355f576SJeff Roberson * 10208355f576SJeff Roberson * Returns: 10218355f576SJeff Roberson * A pointer to the alloced memory or possibly 10228355f576SJeff Roberson * NULL if M_NOWAIT is set. 10238355f576SJeff Roberson */ 10248355f576SJeff Roberson static void * 102585dcf349SGleb Smirnoff noobj_alloc(uma_zone_t zone, int bytes, uint8_t *flags, int wait) 10268355f576SJeff Roberson { 1027a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1028a4915c21SAttilio Rao u_long npages; 1029b245ac95SAlan Cox vm_offset_t retkva, zkva; 1030a4915c21SAttilio Rao vm_page_t p, p_next; 1031e20a199fSJeff Roberson uma_keg_t keg; 10328355f576SJeff Roberson 1033a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1034e20a199fSJeff Roberson keg = zone_first_keg(zone); 1035a4915c21SAttilio Rao 1036a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1037a4915c21SAttilio Rao while (npages > 0) { 1038a4915c21SAttilio Rao p = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT | 1039a4915c21SAttilio Rao VM_ALLOC_WIRED | VM_ALLOC_NOOBJ); 1040a4915c21SAttilio Rao if (p != NULL) { 1041a4915c21SAttilio Rao /* 1042a4915c21SAttilio Rao * Since the page does not belong to an object, its 1043a4915c21SAttilio Rao * listq is unused. 1044a4915c21SAttilio Rao */ 1045a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1046a4915c21SAttilio Rao npages--; 1047a4915c21SAttilio Rao continue; 1048a4915c21SAttilio Rao } 1049a4915c21SAttilio Rao if (wait & M_WAITOK) { 1050a4915c21SAttilio Rao VM_WAIT; 1051a4915c21SAttilio Rao continue; 1052a4915c21SAttilio Rao } 10538355f576SJeff Roberson 10548355f576SJeff Roberson /* 1055a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1056a4915c21SAttilio Rao * exit. 10578355f576SJeff Roberson */ 1058a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 1059b245ac95SAlan Cox vm_page_unwire(p, 0); 1060b245ac95SAlan Cox vm_page_free(p); 1061b245ac95SAlan Cox } 1062a4915c21SAttilio Rao return (NULL); 1063b245ac95SAlan Cox } 10648355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1065a4915c21SAttilio Rao zkva = keg->uk_kva + 1066a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1067a4915c21SAttilio Rao retkva = zkva; 1068a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1069a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1070a4915c21SAttilio Rao zkva += PAGE_SIZE; 1071a4915c21SAttilio Rao } 10728355f576SJeff Roberson 10738355f576SJeff Roberson return ((void *)retkva); 10748355f576SJeff Roberson } 10758355f576SJeff Roberson 10768355f576SJeff Roberson /* 10778355f576SJeff Roberson * Frees a number of pages to the system 10788355f576SJeff Roberson * 10798355f576SJeff Roberson * Arguments: 10808355f576SJeff Roberson * mem A pointer to the memory to be freed 10818355f576SJeff Roberson * size The size of the memory being freed 10828355f576SJeff Roberson * flags The original p->us_flags field 10838355f576SJeff Roberson * 10848355f576SJeff Roberson * Returns: 10858355f576SJeff Roberson * Nothing 10868355f576SJeff Roberson */ 10878355f576SJeff Roberson static void 108885dcf349SGleb Smirnoff page_free(void *mem, int size, uint8_t flags) 10898355f576SJeff Roberson { 10908355f576SJeff Roberson vm_map_t map; 10913370c5bfSJeff Roberson 10928355f576SJeff Roberson if (flags & UMA_SLAB_KMEM) 10938355f576SJeff Roberson map = kmem_map; 1094aea6e893SAlan Cox else if (flags & UMA_SLAB_KERNEL) 1095aea6e893SAlan Cox map = kernel_map; 10968355f576SJeff Roberson else 1097aea6e893SAlan Cox panic("UMA: page_free used with invalid flags %d", flags); 10988355f576SJeff Roberson 10998355f576SJeff Roberson kmem_free(map, (vm_offset_t)mem, size); 11008355f576SJeff Roberson } 11018355f576SJeff Roberson 11028355f576SJeff Roberson /* 11038355f576SJeff Roberson * Zero fill initializer 11048355f576SJeff Roberson * 11058355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 11068355f576SJeff Roberson */ 1107b23f72e9SBrian Feldman static int 1108b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 11098355f576SJeff Roberson { 11108355f576SJeff Roberson bzero(mem, size); 1111b23f72e9SBrian Feldman return (0); 11128355f576SJeff Roberson } 11138355f576SJeff Roberson 11148355f576SJeff Roberson /* 1115e20a199fSJeff Roberson * Finish creating a small uma keg. This calculates ipers, and the keg size. 11168355f576SJeff Roberson * 11178355f576SJeff Roberson * Arguments 1118e20a199fSJeff Roberson * keg The zone we should initialize 11198355f576SJeff Roberson * 11208355f576SJeff Roberson * Returns 11218355f576SJeff Roberson * Nothing 11228355f576SJeff Roberson */ 11238355f576SJeff Roberson static void 1124e20a199fSJeff Roberson keg_small_init(uma_keg_t keg) 11258355f576SJeff Roberson { 1126244f4554SBosko Milekic u_int rsize; 1127244f4554SBosko Milekic u_int memused; 1128244f4554SBosko Milekic u_int wastedspace; 1129244f4554SBosko Milekic u_int shsize; 11308355f576SJeff Roberson 1131ad97af7eSGleb Smirnoff if (keg->uk_flags & UMA_ZONE_PCPU) { 113208a3102cSGleb Smirnoff KASSERT(mp_ncpus > 0, ("%s: ncpus %d\n", __func__, mp_ncpus)); 1133ad97af7eSGleb Smirnoff keg->uk_slabsize = sizeof(struct pcpu); 1134ad97af7eSGleb Smirnoff keg->uk_ppera = howmany(mp_ncpus * sizeof(struct pcpu), 1135ad97af7eSGleb Smirnoff PAGE_SIZE); 1136ad97af7eSGleb Smirnoff } else { 1137ad97af7eSGleb Smirnoff keg->uk_slabsize = UMA_SLAB_SIZE; 1138ad97af7eSGleb Smirnoff keg->uk_ppera = 1; 1139ad97af7eSGleb Smirnoff } 1140ad97af7eSGleb Smirnoff 1141ef72505eSJeff Roberson /* 1142ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1143ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1144ef72505eSJeff Roberson * allocation bits for we round it up. 1145ef72505eSJeff Roberson */ 1146099a0e58SBosko Milekic rsize = keg->uk_size; 1147ef72505eSJeff Roberson if (rsize < keg->uk_slabsize / SLAB_SETSIZE) 1148ef72505eSJeff Roberson rsize = keg->uk_slabsize / SLAB_SETSIZE; 1149099a0e58SBosko Milekic if (rsize & keg->uk_align) 1150099a0e58SBosko Milekic rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1); 1151099a0e58SBosko Milekic keg->uk_rsize = rsize; 1152ad97af7eSGleb Smirnoff 1153ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 1154ad97af7eSGleb Smirnoff keg->uk_rsize < sizeof(struct pcpu), 1155ad97af7eSGleb Smirnoff ("%s: size %u too large", __func__, keg->uk_rsize)); 11568355f576SJeff Roberson 1157ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_REFCNT) 1158ef72505eSJeff Roberson rsize += sizeof(uint32_t); 1159ef72505eSJeff Roberson 1160ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_OFFPAGE) 11612864dbbfSGleb Smirnoff shsize = 0; 1162ef72505eSJeff Roberson else 1163244f4554SBosko Milekic shsize = sizeof(struct uma_slab); 11648355f576SJeff Roberson 1165ad97af7eSGleb Smirnoff keg->uk_ipers = (keg->uk_slabsize - shsize) / rsize; 1166ef72505eSJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE, 1167ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 1168ad97af7eSGleb Smirnoff 1169244f4554SBosko Milekic memused = keg->uk_ipers * rsize + shsize; 1170ad97af7eSGleb Smirnoff wastedspace = keg->uk_slabsize - memused; 1171244f4554SBosko Milekic 117220e8e865SBosko Milekic /* 1173244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 117420e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 117520e8e865SBosko Milekic * may end up going to the VM (kmem_map) for slabs which we 117620e8e865SBosko Milekic * do not want to do if we're UMA_ZFLAG_CACHEONLY as a 117720e8e865SBosko Milekic * result of UMA_ZONE_VM, which clearly forbids it. 117820e8e865SBosko Milekic */ 1179099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) || 1180099a0e58SBosko Milekic (keg->uk_flags & UMA_ZFLAG_CACHEONLY)) 11818355f576SJeff Roberson return; 1182244f4554SBosko Milekic 1183ef72505eSJeff Roberson /* 1184ef72505eSJeff Roberson * See if using an OFFPAGE slab will limit our waste. Only do 1185ef72505eSJeff Roberson * this if it permits more items per-slab. 1186ef72505eSJeff Roberson * 1187ef72505eSJeff Roberson * XXX We could try growing slabsize to limit max waste as well. 1188ef72505eSJeff Roberson * Historically this was not done because the VM could not 1189ef72505eSJeff Roberson * efficiently handle contiguous allocations. 1190ef72505eSJeff Roberson */ 1191ad97af7eSGleb Smirnoff if ((wastedspace >= keg->uk_slabsize / UMA_MAX_WASTE) && 1192ad97af7eSGleb Smirnoff (keg->uk_ipers < (keg->uk_slabsize / keg->uk_rsize))) { 1193ad97af7eSGleb Smirnoff keg->uk_ipers = keg->uk_slabsize / keg->uk_rsize; 1194ef72505eSJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE, 1195ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 1196244f4554SBosko Milekic #ifdef UMA_DEBUG 1197244f4554SBosko Milekic printf("UMA decided we need offpage slab headers for " 1198e20a199fSJeff Roberson "keg: %s, calculated wastedspace = %d, " 1199244f4554SBosko Milekic "maximum wasted space allowed = %d, " 1200244f4554SBosko Milekic "calculated ipers = %d, " 1201e20a199fSJeff Roberson "new wasted space = %d\n", keg->uk_name, wastedspace, 1202ad97af7eSGleb Smirnoff keg->uk_slabsize / UMA_MAX_WASTE, keg->uk_ipers, 1203ad97af7eSGleb Smirnoff keg->uk_slabsize - keg->uk_ipers * keg->uk_rsize); 1204244f4554SBosko Milekic #endif 1205099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_OFFPAGE; 12068355f576SJeff Roberson } 1207ad97af7eSGleb Smirnoff 1208ad97af7eSGleb Smirnoff if ((keg->uk_flags & UMA_ZONE_OFFPAGE) && 1209ad97af7eSGleb Smirnoff (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1210ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_HASH; 12118355f576SJeff Roberson } 12128355f576SJeff Roberson 12138355f576SJeff Roberson /* 1214e20a199fSJeff Roberson * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do 12158355f576SJeff Roberson * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be 12168355f576SJeff Roberson * more complicated. 12178355f576SJeff Roberson * 12188355f576SJeff Roberson * Arguments 1219e20a199fSJeff Roberson * keg The keg we should initialize 12208355f576SJeff Roberson * 12218355f576SJeff Roberson * Returns 12228355f576SJeff Roberson * Nothing 12238355f576SJeff Roberson */ 12248355f576SJeff Roberson static void 1225e20a199fSJeff Roberson keg_large_init(uma_keg_t keg) 12268355f576SJeff Roberson { 12278355f576SJeff Roberson 1228e20a199fSJeff Roberson KASSERT(keg != NULL, ("Keg is null in keg_large_init")); 1229099a0e58SBosko Milekic KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0, 1230e20a199fSJeff Roberson ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg")); 1231ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1232ad97af7eSGleb Smirnoff ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__)); 123320e8e865SBosko Milekic 1234ad97af7eSGleb Smirnoff keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE); 1235ad97af7eSGleb Smirnoff keg->uk_slabsize = keg->uk_ppera * PAGE_SIZE; 1236099a0e58SBosko Milekic keg->uk_ipers = 1; 1237e9a069d8SJohn Baldwin keg->uk_rsize = keg->uk_size; 1238e9a069d8SJohn Baldwin 1239e9a069d8SJohn Baldwin /* We can't do OFFPAGE if we're internal, bail out here. */ 1240e9a069d8SJohn Baldwin if (keg->uk_flags & UMA_ZFLAG_INTERNAL) 1241e9a069d8SJohn Baldwin return; 12428355f576SJeff Roberson 1243099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_OFFPAGE; 1244e20a199fSJeff Roberson if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1245099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_HASH; 12468355f576SJeff Roberson } 12478355f576SJeff Roberson 1248e20a199fSJeff Roberson static void 1249e20a199fSJeff Roberson keg_cachespread_init(uma_keg_t keg) 1250e20a199fSJeff Roberson { 1251e20a199fSJeff Roberson int alignsize; 1252e20a199fSJeff Roberson int trailer; 1253e20a199fSJeff Roberson int pages; 1254e20a199fSJeff Roberson int rsize; 1255e20a199fSJeff Roberson 1256ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1257ad97af7eSGleb Smirnoff ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__)); 1258ad97af7eSGleb Smirnoff 1259e20a199fSJeff Roberson alignsize = keg->uk_align + 1; 1260e20a199fSJeff Roberson rsize = keg->uk_size; 1261e20a199fSJeff Roberson /* 1262e20a199fSJeff Roberson * We want one item to start on every align boundary in a page. To 1263e20a199fSJeff Roberson * do this we will span pages. We will also extend the item by the 1264e20a199fSJeff Roberson * size of align if it is an even multiple of align. Otherwise, it 1265e20a199fSJeff Roberson * would fall on the same boundary every time. 1266e20a199fSJeff Roberson */ 1267e20a199fSJeff Roberson if (rsize & keg->uk_align) 1268e20a199fSJeff Roberson rsize = (rsize & ~keg->uk_align) + alignsize; 1269e20a199fSJeff Roberson if ((rsize & alignsize) == 0) 1270e20a199fSJeff Roberson rsize += alignsize; 1271e20a199fSJeff Roberson trailer = rsize - keg->uk_size; 1272e20a199fSJeff Roberson pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE; 1273e20a199fSJeff Roberson pages = MIN(pages, (128 * 1024) / PAGE_SIZE); 1274e20a199fSJeff Roberson keg->uk_rsize = rsize; 1275e20a199fSJeff Roberson keg->uk_ppera = pages; 1276ad97af7eSGleb Smirnoff keg->uk_slabsize = UMA_SLAB_SIZE; 1277e20a199fSJeff Roberson keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize; 1278e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 1279e20a199fSJeff Roberson KASSERT(keg->uk_ipers <= uma_max_ipers, 128042321809SGleb Smirnoff ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__, 1281e20a199fSJeff Roberson keg->uk_ipers)); 1282e20a199fSJeff Roberson } 1283e20a199fSJeff Roberson 12848355f576SJeff Roberson /* 1285099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 1286099a0e58SBosko Milekic * the keg onto the global keg list. 12878355f576SJeff Roberson * 12888355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 1289099a0e58SBosko Milekic * udata Actually uma_kctor_args 1290099a0e58SBosko Milekic */ 1291b23f72e9SBrian Feldman static int 1292b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 1293099a0e58SBosko Milekic { 1294099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 1295099a0e58SBosko Milekic uma_keg_t keg = mem; 1296099a0e58SBosko Milekic uma_zone_t zone; 1297099a0e58SBosko Milekic 1298099a0e58SBosko Milekic bzero(keg, size); 1299099a0e58SBosko Milekic keg->uk_size = arg->size; 1300099a0e58SBosko Milekic keg->uk_init = arg->uminit; 1301099a0e58SBosko Milekic keg->uk_fini = arg->fini; 1302099a0e58SBosko Milekic keg->uk_align = arg->align; 1303099a0e58SBosko Milekic keg->uk_free = 0; 1304099a0e58SBosko Milekic keg->uk_pages = 0; 1305099a0e58SBosko Milekic keg->uk_flags = arg->flags; 1306099a0e58SBosko Milekic keg->uk_allocf = page_alloc; 1307099a0e58SBosko Milekic keg->uk_freef = page_free; 1308099a0e58SBosko Milekic keg->uk_slabzone = NULL; 1309099a0e58SBosko Milekic 1310099a0e58SBosko Milekic /* 1311099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 1312099a0e58SBosko Milekic */ 1313099a0e58SBosko Milekic zone = arg->zone; 1314e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 1315099a0e58SBosko Milekic 1316099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_VM) 1317099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 1318099a0e58SBosko Milekic 1319099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 1320099a0e58SBosko Milekic keg->uk_init = zero_init; 1321099a0e58SBosko Milekic 1322e20a199fSJeff Roberson if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC) 1323e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_VTOSLAB; 1324e20a199fSJeff Roberson 1325ad97af7eSGleb Smirnoff if (arg->flags & UMA_ZONE_PCPU) 1326ad97af7eSGleb Smirnoff #ifdef SMP 1327ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_OFFPAGE; 1328ad97af7eSGleb Smirnoff #else 1329ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 1330ad97af7eSGleb Smirnoff #endif 1331ad97af7eSGleb Smirnoff 1332ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_CACHESPREAD) { 1333e20a199fSJeff Roberson keg_cachespread_init(keg); 1334ef72505eSJeff Roberson } else if (keg->uk_flags & UMA_ZONE_REFCNT) { 1335ef72505eSJeff Roberson if (keg->uk_size > 1336ef72505eSJeff Roberson (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) - 1337ef72505eSJeff Roberson sizeof(uint32_t))) 1338e20a199fSJeff Roberson keg_large_init(keg); 1339099a0e58SBosko Milekic else 1340e20a199fSJeff Roberson keg_small_init(keg); 1341244f4554SBosko Milekic } else { 1342ef72505eSJeff Roberson if (keg->uk_size > (UMA_SLAB_SIZE - sizeof(struct uma_slab))) 1343e20a199fSJeff Roberson keg_large_init(keg); 1344244f4554SBosko Milekic else 1345e20a199fSJeff Roberson keg_small_init(keg); 1346244f4554SBosko Milekic } 1347099a0e58SBosko Milekic 1348244f4554SBosko Milekic if (keg->uk_flags & UMA_ZONE_OFFPAGE) { 1349ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_REFCNT) { 1350ef72505eSJeff Roberson if (keg->uk_ipers > uma_max_ipers_ref) 1351ef72505eSJeff Roberson panic("Too many ref items per zone: %d > %d\n", 1352ef72505eSJeff Roberson keg->uk_ipers, uma_max_ipers_ref); 1353099a0e58SBosko Milekic keg->uk_slabzone = slabrefzone; 1354ef72505eSJeff Roberson } else 1355099a0e58SBosko Milekic keg->uk_slabzone = slabzone; 1356244f4554SBosko Milekic } 1357099a0e58SBosko Milekic 1358099a0e58SBosko Milekic /* 1359099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 1360099a0e58SBosko Milekic * startup cache until the vm is ready. 1361099a0e58SBosko Milekic */ 1362099a0e58SBosko Milekic if (keg->uk_ppera == 1) { 1363099a0e58SBosko Milekic #ifdef UMA_MD_SMALL_ALLOC 1364099a0e58SBosko Milekic keg->uk_allocf = uma_small_alloc; 1365099a0e58SBosko Milekic keg->uk_freef = uma_small_free; 13668cd02d00SAlan Cox 1367342f1793SAlan Cox if (booted < UMA_STARTUP) 1368099a0e58SBosko Milekic keg->uk_allocf = startup_alloc; 13698cd02d00SAlan Cox #else 13708cd02d00SAlan Cox if (booted < UMA_STARTUP2) 13718cd02d00SAlan Cox keg->uk_allocf = startup_alloc; 13728cd02d00SAlan Cox #endif 1373342f1793SAlan Cox } else if (booted < UMA_STARTUP2 && 1374342f1793SAlan Cox (keg->uk_flags & UMA_ZFLAG_INTERNAL)) 1375e9a069d8SJohn Baldwin keg->uk_allocf = startup_alloc; 1376099a0e58SBosko Milekic 1377099a0e58SBosko Milekic /* 1378e20a199fSJeff Roberson * Initialize keg's lock (shared among zones). 1379099a0e58SBosko Milekic */ 1380099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_MTXCLASS) 1381e20a199fSJeff Roberson KEG_LOCK_INIT(keg, 1); 1382099a0e58SBosko Milekic else 1383e20a199fSJeff Roberson KEG_LOCK_INIT(keg, 0); 1384099a0e58SBosko Milekic 1385099a0e58SBosko Milekic /* 1386099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 1387099a0e58SBosko Milekic * figure out where in each page it goes. This calculates a right 1388099a0e58SBosko Milekic * justified offset into the memory on an ALIGN_PTR boundary. 1389099a0e58SBosko Milekic */ 1390099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) { 1391244f4554SBosko Milekic u_int totsize; 1392099a0e58SBosko Milekic 1393099a0e58SBosko Milekic /* Size of the slab struct and free list */ 1394ef72505eSJeff Roberson totsize = sizeof(struct uma_slab); 1395ef72505eSJeff Roberson 1396ef72505eSJeff Roberson /* Size of the reference counts. */ 1397244f4554SBosko Milekic if (keg->uk_flags & UMA_ZONE_REFCNT) 1398ef72505eSJeff Roberson totsize += keg->uk_ipers * sizeof(uint32_t); 1399244f4554SBosko Milekic 1400099a0e58SBosko Milekic if (totsize & UMA_ALIGN_PTR) 1401099a0e58SBosko Milekic totsize = (totsize & ~UMA_ALIGN_PTR) + 1402099a0e58SBosko Milekic (UMA_ALIGN_PTR + 1); 1403ad97af7eSGleb Smirnoff keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize; 1404244f4554SBosko Milekic 1405244f4554SBosko Milekic /* 1406244f4554SBosko Milekic * The only way the following is possible is if with our 1407244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 1408244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 1409244f4554SBosko Milekic * mathematically possible for all cases, so we make 1410244f4554SBosko Milekic * sure here anyway. 1411244f4554SBosko Milekic */ 1412ef72505eSJeff Roberson totsize = keg->uk_pgoff + sizeof(struct uma_slab); 1413ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_REFCNT) 1414ef72505eSJeff Roberson totsize += keg->uk_ipers * sizeof(uint32_t); 1415ad97af7eSGleb Smirnoff if (totsize > PAGE_SIZE * keg->uk_ppera) { 1416099a0e58SBosko Milekic printf("zone %s ipers %d rsize %d size %d\n", 1417099a0e58SBosko Milekic zone->uz_name, keg->uk_ipers, keg->uk_rsize, 1418099a0e58SBosko Milekic keg->uk_size); 1419aea6e893SAlan Cox panic("UMA slab won't fit."); 1420099a0e58SBosko Milekic } 1421099a0e58SBosko Milekic } 1422099a0e58SBosko Milekic 1423099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 1424099a0e58SBosko Milekic hash_alloc(&keg->uk_hash); 1425099a0e58SBosko Milekic 1426099a0e58SBosko Milekic #ifdef UMA_DEBUG 14270b80c1e4SEitan Adler printf("UMA: %s(%p) size %d(%d) flags %#x ipers %d ppera %d out %d free %d\n", 1428e20a199fSJeff Roberson zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags, 1429e20a199fSJeff Roberson keg->uk_ipers, keg->uk_ppera, 1430e20a199fSJeff Roberson (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free); 1431099a0e58SBosko Milekic #endif 1432099a0e58SBosko Milekic 1433099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 1434099a0e58SBosko Milekic 1435099a0e58SBosko Milekic mtx_lock(&uma_mtx); 1436099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 1437099a0e58SBosko Milekic mtx_unlock(&uma_mtx); 1438b23f72e9SBrian Feldman return (0); 1439099a0e58SBosko Milekic } 1440099a0e58SBosko Milekic 1441099a0e58SBosko Milekic /* 1442099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 1443099a0e58SBosko Milekic * 1444099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 1445099a0e58SBosko Milekic * udata Actually uma_zctor_args 14468355f576SJeff Roberson */ 1447b23f72e9SBrian Feldman static int 1448b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 14498355f576SJeff Roberson { 14508355f576SJeff Roberson struct uma_zctor_args *arg = udata; 14518355f576SJeff Roberson uma_zone_t zone = mem; 1452099a0e58SBosko Milekic uma_zone_t z; 1453099a0e58SBosko Milekic uma_keg_t keg; 14548355f576SJeff Roberson 14558355f576SJeff Roberson bzero(zone, size); 14568355f576SJeff Roberson zone->uz_name = arg->name; 14578355f576SJeff Roberson zone->uz_ctor = arg->ctor; 14588355f576SJeff Roberson zone->uz_dtor = arg->dtor; 1459e20a199fSJeff Roberson zone->uz_slab = zone_fetch_slab; 1460099a0e58SBosko Milekic zone->uz_init = NULL; 1461099a0e58SBosko Milekic zone->uz_fini = NULL; 1462099a0e58SBosko Milekic zone->uz_allocs = 0; 1463773df9abSRobert Watson zone->uz_frees = 0; 14642019094aSRobert Watson zone->uz_fails = 0; 1465bf965959SSean Bruno zone->uz_sleeps = 0; 1466*fc03d22bSJeff Roberson zone->uz_count = 0; 1467e20a199fSJeff Roberson zone->uz_flags = 0; 14682f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 14692f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 1470e20a199fSJeff Roberson keg = arg->keg; 1471099a0e58SBosko Milekic 14720095a784SJeff Roberson /* 14730095a784SJeff Roberson * This is a pure cache zone, no kegs. 14740095a784SJeff Roberson */ 14750095a784SJeff Roberson if (arg->import) { 14760095a784SJeff Roberson zone->uz_import = arg->import; 14770095a784SJeff Roberson zone->uz_release = arg->release; 14780095a784SJeff Roberson zone->uz_arg = arg->arg; 14790095a784SJeff Roberson zone->uz_count = BUCKET_MAX; 14800095a784SJeff Roberson return (0); 14810095a784SJeff Roberson } 14820095a784SJeff Roberson 14830095a784SJeff Roberson /* 14840095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 14850095a784SJeff Roberson */ 14860095a784SJeff Roberson zone->uz_import = (uma_import)zone_import; 14870095a784SJeff Roberson zone->uz_release = (uma_release)zone_release; 14880095a784SJeff Roberson zone->uz_arg = zone; 14890095a784SJeff Roberson 1490099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 1491099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 14928355f576SJeff Roberson zone->uz_init = arg->uminit; 1493e221e841SJeff Roberson zone->uz_fini = arg->fini; 1494099a0e58SBosko Milekic zone->uz_lock = &keg->uk_lock; 1495e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 14968355f576SJeff Roberson mtx_lock(&uma_mtx); 1497099a0e58SBosko Milekic ZONE_LOCK(zone); 1498099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 1499099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 1500099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 1501099a0e58SBosko Milekic break; 1502099a0e58SBosko Milekic } 1503099a0e58SBosko Milekic } 1504099a0e58SBosko Milekic ZONE_UNLOCK(zone); 15058355f576SJeff Roberson mtx_unlock(&uma_mtx); 1506e20a199fSJeff Roberson } else if (keg == NULL) { 1507e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 1508e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 1509b23f72e9SBrian Feldman return (ENOMEM); 1510099a0e58SBosko Milekic } else { 1511099a0e58SBosko Milekic struct uma_kctor_args karg; 1512b23f72e9SBrian Feldman int error; 1513099a0e58SBosko Milekic 1514099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 1515099a0e58SBosko Milekic karg.size = arg->size; 1516099a0e58SBosko Milekic karg.uminit = arg->uminit; 1517099a0e58SBosko Milekic karg.fini = arg->fini; 1518099a0e58SBosko Milekic karg.align = arg->align; 1519099a0e58SBosko Milekic karg.flags = arg->flags; 1520099a0e58SBosko Milekic karg.zone = zone; 1521b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 1522b23f72e9SBrian Feldman flags); 1523b23f72e9SBrian Feldman if (error) 1524b23f72e9SBrian Feldman return (error); 1525099a0e58SBosko Milekic } 15260095a784SJeff Roberson 1527e20a199fSJeff Roberson /* 1528e20a199fSJeff Roberson * Link in the first keg. 1529e20a199fSJeff Roberson */ 1530e20a199fSJeff Roberson zone->uz_klink.kl_keg = keg; 1531e20a199fSJeff Roberson LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link); 1532099a0e58SBosko Milekic zone->uz_lock = &keg->uk_lock; 1533e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 1534e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 1535e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 15368355f576SJeff Roberson 15378355f576SJeff Roberson /* 15388355f576SJeff Roberson * Some internal zones don't have room allocated for the per cpu 15398355f576SJeff Roberson * caches. If we're internal, bail out here. 15408355f576SJeff Roberson */ 1541099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZFLAG_INTERNAL) { 1542e20a199fSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 1543099a0e58SBosko Milekic ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 1544b23f72e9SBrian Feldman return (0); 1545099a0e58SBosko Milekic } 15468355f576SJeff Roberson 1547*fc03d22bSJeff Roberson if ((keg->uk_flags & UMA_ZONE_MAXBUCKET) == 0) 1548*fc03d22bSJeff Roberson zone->uz_count = bucket_select(keg->uk_rsize); 15498355f576SJeff Roberson else 1550cae33c14SJeff Roberson zone->uz_count = BUCKET_MAX; 1551*fc03d22bSJeff Roberson 1552b23f72e9SBrian Feldman return (0); 15538355f576SJeff Roberson } 15548355f576SJeff Roberson 15558355f576SJeff Roberson /* 1556099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 1557099a0e58SBosko Milekic * table and removes the keg from the global list. 15589c2cd7e5SJeff Roberson * 15599c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 15609c2cd7e5SJeff Roberson * udata unused 15619c2cd7e5SJeff Roberson */ 1562099a0e58SBosko Milekic static void 1563099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 1564099a0e58SBosko Milekic { 1565099a0e58SBosko Milekic uma_keg_t keg; 15669c2cd7e5SJeff Roberson 1567099a0e58SBosko Milekic keg = (uma_keg_t)arg; 1568e20a199fSJeff Roberson KEG_LOCK(keg); 1569099a0e58SBosko Milekic if (keg->uk_free != 0) { 1570099a0e58SBosko Milekic printf("Freed UMA keg was not empty (%d items). " 1571099a0e58SBosko Milekic " Lost %d pages of memory.\n", 1572099a0e58SBosko Milekic keg->uk_free, keg->uk_pages); 1573099a0e58SBosko Milekic } 1574e20a199fSJeff Roberson KEG_UNLOCK(keg); 1575099a0e58SBosko Milekic 1576099a0e58SBosko Milekic hash_free(&keg->uk_hash); 1577099a0e58SBosko Milekic 1578e20a199fSJeff Roberson KEG_LOCK_FINI(keg); 1579099a0e58SBosko Milekic } 1580099a0e58SBosko Milekic 1581099a0e58SBosko Milekic /* 1582099a0e58SBosko Milekic * Zone header dtor. 1583099a0e58SBosko Milekic * 1584099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 1585099a0e58SBosko Milekic * udata unused 1586099a0e58SBosko Milekic */ 15879c2cd7e5SJeff Roberson static void 15889c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 15899c2cd7e5SJeff Roberson { 1590e20a199fSJeff Roberson uma_klink_t klink; 15919c2cd7e5SJeff Roberson uma_zone_t zone; 1592099a0e58SBosko Milekic uma_keg_t keg; 15939c2cd7e5SJeff Roberson 15949c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 1595e20a199fSJeff Roberson keg = zone_first_keg(zone); 15969643769aSJeff Roberson 1597e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 15989643769aSJeff Roberson cache_drain(zone); 1599099a0e58SBosko Milekic 160017b9cc49SJeff Roberson mtx_lock(&uma_mtx); 1601099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 1602e20a199fSJeff Roberson mtx_unlock(&uma_mtx); 1603099a0e58SBosko Milekic /* 1604099a0e58SBosko Milekic * XXX there are some races here where 1605099a0e58SBosko Milekic * the zone can be drained but zone lock 1606099a0e58SBosko Milekic * released and then refilled before we 1607099a0e58SBosko Milekic * remove it... we dont care for now 1608099a0e58SBosko Milekic */ 1609e20a199fSJeff Roberson zone_drain_wait(zone, M_WAITOK); 1610e20a199fSJeff Roberson /* 1611e20a199fSJeff Roberson * Unlink all of our kegs. 1612e20a199fSJeff Roberson */ 1613e20a199fSJeff Roberson while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) { 1614e20a199fSJeff Roberson klink->kl_keg = NULL; 1615e20a199fSJeff Roberson LIST_REMOVE(klink, kl_link); 1616e20a199fSJeff Roberson if (klink == &zone->uz_klink) 1617e20a199fSJeff Roberson continue; 1618e20a199fSJeff Roberson free(klink, M_TEMP); 1619e20a199fSJeff Roberson } 1620e20a199fSJeff Roberson /* 1621e20a199fSJeff Roberson * We only destroy kegs from non secondary zones. 1622e20a199fSJeff Roberson */ 16230095a784SJeff Roberson if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0) { 1624e20a199fSJeff Roberson mtx_lock(&uma_mtx); 1625099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 1626099a0e58SBosko Milekic mtx_unlock(&uma_mtx); 16270095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 16289c2cd7e5SJeff Roberson } 1629099a0e58SBosko Milekic } 1630099a0e58SBosko Milekic 16319c2cd7e5SJeff Roberson /* 16328355f576SJeff Roberson * Traverses every zone in the system and calls a callback 16338355f576SJeff Roberson * 16348355f576SJeff Roberson * Arguments: 16358355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 16368355f576SJeff Roberson * as an argument. 16378355f576SJeff Roberson * 16388355f576SJeff Roberson * Returns: 16398355f576SJeff Roberson * Nothing 16408355f576SJeff Roberson */ 16418355f576SJeff Roberson static void 16428355f576SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t)) 16438355f576SJeff Roberson { 1644099a0e58SBosko Milekic uma_keg_t keg; 16458355f576SJeff Roberson uma_zone_t zone; 16468355f576SJeff Roberson 16478355f576SJeff Roberson mtx_lock(&uma_mtx); 1648099a0e58SBosko Milekic LIST_FOREACH(keg, &uma_kegs, uk_link) { 1649099a0e58SBosko Milekic LIST_FOREACH(zone, &keg->uk_zones, uz_link) 16508355f576SJeff Roberson zfunc(zone); 1651099a0e58SBosko Milekic } 16528355f576SJeff Roberson mtx_unlock(&uma_mtx); 16538355f576SJeff Roberson } 16548355f576SJeff Roberson 16558355f576SJeff Roberson /* Public functions */ 16568355f576SJeff Roberson /* See uma.h */ 16578355f576SJeff Roberson void 16583803b26bSDag-Erling Smørgrav uma_startup(void *bootmem, int boot_pages) 16598355f576SJeff Roberson { 16608355f576SJeff Roberson struct uma_zctor_args args; 16618355f576SJeff Roberson uma_slab_t slab; 1662244f4554SBosko Milekic u_int slabsize; 16638355f576SJeff Roberson int i; 16648355f576SJeff Roberson 16658355f576SJeff Roberson #ifdef UMA_DEBUG 1666099a0e58SBosko Milekic printf("Creating uma keg headers zone and keg.\n"); 16678355f576SJeff Roberson #endif 1668f353d338SAlan Cox mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF); 1669099a0e58SBosko Milekic 1670099a0e58SBosko Milekic /* "manually" create the initial zone */ 16710095a784SJeff Roberson memset(&args, 0, sizeof(args)); 1672099a0e58SBosko Milekic args.name = "UMA Kegs"; 1673099a0e58SBosko Milekic args.size = sizeof(struct uma_keg); 1674099a0e58SBosko Milekic args.ctor = keg_ctor; 1675099a0e58SBosko Milekic args.dtor = keg_dtor; 16768355f576SJeff Roberson args.uminit = zero_init; 16778355f576SJeff Roberson args.fini = NULL; 1678099a0e58SBosko Milekic args.keg = &masterkeg; 16798355f576SJeff Roberson args.align = 32 - 1; 1680b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 16818355f576SJeff Roberson /* The initial zone has no Per cpu queues so it's smaller */ 1682b23f72e9SBrian Feldman zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK); 16838355f576SJeff Roberson 16848355f576SJeff Roberson #ifdef UMA_DEBUG 16858355f576SJeff Roberson printf("Filling boot free list.\n"); 16868355f576SJeff Roberson #endif 16873803b26bSDag-Erling Smørgrav for (i = 0; i < boot_pages; i++) { 168885dcf349SGleb Smirnoff slab = (uma_slab_t)((uint8_t *)bootmem + (i * UMA_SLAB_SIZE)); 168985dcf349SGleb Smirnoff slab->us_data = (uint8_t *)slab; 16908355f576SJeff Roberson slab->us_flags = UMA_SLAB_BOOT; 16918355f576SJeff Roberson LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link); 16928355f576SJeff Roberson } 1693f353d338SAlan Cox mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF); 16948355f576SJeff Roberson 16958355f576SJeff Roberson #ifdef UMA_DEBUG 1696099a0e58SBosko Milekic printf("Creating uma zone headers zone and keg.\n"); 1697099a0e58SBosko Milekic #endif 1698099a0e58SBosko Milekic args.name = "UMA Zones"; 1699099a0e58SBosko Milekic args.size = sizeof(struct uma_zone) + 1700099a0e58SBosko Milekic (sizeof(struct uma_cache) * (mp_maxid + 1)); 1701099a0e58SBosko Milekic args.ctor = zone_ctor; 1702099a0e58SBosko Milekic args.dtor = zone_dtor; 1703099a0e58SBosko Milekic args.uminit = zero_init; 1704099a0e58SBosko Milekic args.fini = NULL; 1705099a0e58SBosko Milekic args.keg = NULL; 1706099a0e58SBosko Milekic args.align = 32 - 1; 1707099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 1708099a0e58SBosko Milekic /* The initial zone has no Per cpu queues so it's smaller */ 1709b23f72e9SBrian Feldman zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK); 1710099a0e58SBosko Milekic 1711099a0e58SBosko Milekic #ifdef UMA_DEBUG 1712099a0e58SBosko Milekic printf("Initializing pcpu cache locks.\n"); 1713099a0e58SBosko Milekic #endif 1714099a0e58SBosko Milekic #ifdef UMA_DEBUG 1715099a0e58SBosko Milekic printf("Creating slab and hash zones.\n"); 17168355f576SJeff Roberson #endif 17178355f576SJeff Roberson 17188355f576SJeff Roberson /* Now make a zone for slab headers */ 17198355f576SJeff Roberson slabzone = uma_zcreate("UMA Slabs", 1720ef72505eSJeff Roberson sizeof(struct uma_slab), 17218355f576SJeff Roberson NULL, NULL, NULL, NULL, 1722b60f5b79SJeff Roberson UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 17238355f576SJeff Roberson 1724099a0e58SBosko Milekic /* 1725099a0e58SBosko Milekic * We also create a zone for the bigger slabs with reference 1726099a0e58SBosko Milekic * counts in them, to accomodate UMA_ZONE_REFCNT zones. 1727099a0e58SBosko Milekic */ 1728ef72505eSJeff Roberson slabsize = sizeof(struct uma_slab_refcnt); 1729ef72505eSJeff Roberson slabsize += uma_max_ipers_ref * sizeof(uint32_t); 1730099a0e58SBosko Milekic slabrefzone = uma_zcreate("UMA RCntSlabs", 1731099a0e58SBosko Milekic slabsize, 1732099a0e58SBosko Milekic NULL, NULL, NULL, NULL, 1733e66468eaSBosko Milekic UMA_ALIGN_PTR, 17347fd87882SBosko Milekic UMA_ZFLAG_INTERNAL); 1735099a0e58SBosko Milekic 17368355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 17378355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 17388355f576SJeff Roberson NULL, NULL, NULL, NULL, 1739b60f5b79SJeff Roberson UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 17408355f576SJeff Roberson 1741cae33c14SJeff Roberson bucket_init(); 17428355f576SJeff Roberson 1743342f1793SAlan Cox booted = UMA_STARTUP; 17448355f576SJeff Roberson 17458355f576SJeff Roberson #ifdef UMA_DEBUG 17468355f576SJeff Roberson printf("UMA startup complete.\n"); 17478355f576SJeff Roberson #endif 17488355f576SJeff Roberson } 17498355f576SJeff Roberson 17508355f576SJeff Roberson /* see uma.h */ 17518355f576SJeff Roberson void 175299571dc3SJeff Roberson uma_startup2(void) 17538355f576SJeff Roberson { 1754342f1793SAlan Cox booted = UMA_STARTUP2; 175586bbae32SJeff Roberson bucket_enable(); 17568355f576SJeff Roberson #ifdef UMA_DEBUG 17578355f576SJeff Roberson printf("UMA startup2 complete.\n"); 17588355f576SJeff Roberson #endif 17598355f576SJeff Roberson } 17608355f576SJeff Roberson 17618355f576SJeff Roberson /* 17628355f576SJeff Roberson * Initialize our callout handle 17638355f576SJeff Roberson * 17648355f576SJeff Roberson */ 17658355f576SJeff Roberson 17668355f576SJeff Roberson static void 17678355f576SJeff Roberson uma_startup3(void) 17688355f576SJeff Roberson { 17698355f576SJeff Roberson #ifdef UMA_DEBUG 17708355f576SJeff Roberson printf("Starting callout.\n"); 17718355f576SJeff Roberson #endif 1772a3c07611SRobert Watson callout_init(&uma_callout, CALLOUT_MPSAFE); 17739643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 17748355f576SJeff Roberson #ifdef UMA_DEBUG 17758355f576SJeff Roberson printf("UMA startup3 complete.\n"); 17768355f576SJeff Roberson #endif 17778355f576SJeff Roberson } 17788355f576SJeff Roberson 1779e20a199fSJeff Roberson static uma_keg_t 1780099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 178185dcf349SGleb Smirnoff int align, uint32_t flags) 1782099a0e58SBosko Milekic { 1783099a0e58SBosko Milekic struct uma_kctor_args args; 1784099a0e58SBosko Milekic 1785099a0e58SBosko Milekic args.size = size; 1786099a0e58SBosko Milekic args.uminit = uminit; 1787099a0e58SBosko Milekic args.fini = fini; 17881e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 1789099a0e58SBosko Milekic args.flags = flags; 1790099a0e58SBosko Milekic args.zone = zone; 1791e20a199fSJeff Roberson return (zone_alloc_item(kegs, &args, M_WAITOK)); 1792099a0e58SBosko Milekic } 1793099a0e58SBosko Milekic 17948355f576SJeff Roberson /* See uma.h */ 17951e319f6dSRobert Watson void 17961e319f6dSRobert Watson uma_set_align(int align) 17971e319f6dSRobert Watson { 17981e319f6dSRobert Watson 17991e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 18001e319f6dSRobert Watson uma_align_cache = align; 18011e319f6dSRobert Watson } 18021e319f6dSRobert Watson 18031e319f6dSRobert Watson /* See uma.h */ 18048355f576SJeff Roberson uma_zone_t 1805bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 180685dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 18078355f576SJeff Roberson 18088355f576SJeff Roberson { 18098355f576SJeff Roberson struct uma_zctor_args args; 18108355f576SJeff Roberson 18118355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 18120095a784SJeff Roberson memset(&args, 0, sizeof(args)); 18138355f576SJeff Roberson args.name = name; 18148355f576SJeff Roberson args.size = size; 18158355f576SJeff Roberson args.ctor = ctor; 18168355f576SJeff Roberson args.dtor = dtor; 18178355f576SJeff Roberson args.uminit = uminit; 18188355f576SJeff Roberson args.fini = fini; 18198355f576SJeff Roberson args.align = align; 18208355f576SJeff Roberson args.flags = flags; 1821099a0e58SBosko Milekic args.keg = NULL; 1822099a0e58SBosko Milekic 1823e20a199fSJeff Roberson return (zone_alloc_item(zones, &args, M_WAITOK)); 1824099a0e58SBosko Milekic } 1825099a0e58SBosko Milekic 1826099a0e58SBosko Milekic /* See uma.h */ 1827099a0e58SBosko Milekic uma_zone_t 1828099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 1829099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 1830099a0e58SBosko Milekic { 1831099a0e58SBosko Milekic struct uma_zctor_args args; 1832e20a199fSJeff Roberson uma_keg_t keg; 1833099a0e58SBosko Milekic 1834e20a199fSJeff Roberson keg = zone_first_keg(master); 18350095a784SJeff Roberson memset(&args, 0, sizeof(args)); 1836099a0e58SBosko Milekic args.name = name; 1837e20a199fSJeff Roberson args.size = keg->uk_size; 1838099a0e58SBosko Milekic args.ctor = ctor; 1839099a0e58SBosko Milekic args.dtor = dtor; 1840099a0e58SBosko Milekic args.uminit = zinit; 1841099a0e58SBosko Milekic args.fini = zfini; 1842e20a199fSJeff Roberson args.align = keg->uk_align; 1843e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 1844e20a199fSJeff Roberson args.keg = keg; 18458355f576SJeff Roberson 1846e20a199fSJeff Roberson /* XXX Attaches only one keg of potentially many. */ 1847e20a199fSJeff Roberson return (zone_alloc_item(zones, &args, M_WAITOK)); 18488355f576SJeff Roberson } 18498355f576SJeff Roberson 18500095a784SJeff Roberson /* See uma.h */ 18510095a784SJeff Roberson uma_zone_t 18520095a784SJeff Roberson uma_zcache_create(char *name, uma_ctor ctor, uma_dtor dtor, uma_init zinit, 18530095a784SJeff Roberson uma_fini zfini, uma_import zimport, uma_release zrelease, 18540095a784SJeff Roberson void *arg, int flags) 18550095a784SJeff Roberson { 18560095a784SJeff Roberson struct uma_zctor_args args; 18570095a784SJeff Roberson 18580095a784SJeff Roberson memset(&args, 0, sizeof(args)); 18590095a784SJeff Roberson args.name = name; 18600095a784SJeff Roberson args.size = 0; 18610095a784SJeff Roberson args.ctor = ctor; 18620095a784SJeff Roberson args.dtor = dtor; 18630095a784SJeff Roberson args.uminit = zinit; 18640095a784SJeff Roberson args.fini = zfini; 18650095a784SJeff Roberson args.import = zimport; 18660095a784SJeff Roberson args.release = zrelease; 18670095a784SJeff Roberson args.arg = arg; 18680095a784SJeff Roberson args.align = 0; 18690095a784SJeff Roberson args.flags = flags; 18700095a784SJeff Roberson 18710095a784SJeff Roberson return (zone_alloc_item(zones, &args, M_WAITOK)); 18720095a784SJeff Roberson } 18730095a784SJeff Roberson 1874e20a199fSJeff Roberson static void 1875e20a199fSJeff Roberson zone_lock_pair(uma_zone_t a, uma_zone_t b) 1876e20a199fSJeff Roberson { 1877e20a199fSJeff Roberson if (a < b) { 1878e20a199fSJeff Roberson ZONE_LOCK(a); 1879e20a199fSJeff Roberson mtx_lock_flags(b->uz_lock, MTX_DUPOK); 1880e20a199fSJeff Roberson } else { 1881e20a199fSJeff Roberson ZONE_LOCK(b); 1882e20a199fSJeff Roberson mtx_lock_flags(a->uz_lock, MTX_DUPOK); 1883e20a199fSJeff Roberson } 1884e20a199fSJeff Roberson } 1885e20a199fSJeff Roberson 1886e20a199fSJeff Roberson static void 1887e20a199fSJeff Roberson zone_unlock_pair(uma_zone_t a, uma_zone_t b) 1888e20a199fSJeff Roberson { 1889e20a199fSJeff Roberson 1890e20a199fSJeff Roberson ZONE_UNLOCK(a); 1891e20a199fSJeff Roberson ZONE_UNLOCK(b); 1892e20a199fSJeff Roberson } 1893e20a199fSJeff Roberson 1894e20a199fSJeff Roberson int 1895e20a199fSJeff Roberson uma_zsecond_add(uma_zone_t zone, uma_zone_t master) 1896e20a199fSJeff Roberson { 1897e20a199fSJeff Roberson uma_klink_t klink; 1898e20a199fSJeff Roberson uma_klink_t kl; 1899e20a199fSJeff Roberson int error; 1900e20a199fSJeff Roberson 1901e20a199fSJeff Roberson error = 0; 1902e20a199fSJeff Roberson klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO); 1903e20a199fSJeff Roberson 1904e20a199fSJeff Roberson zone_lock_pair(zone, master); 1905e20a199fSJeff Roberson /* 1906e20a199fSJeff Roberson * zone must use vtoslab() to resolve objects and must already be 1907e20a199fSJeff Roberson * a secondary. 1908e20a199fSJeff Roberson */ 1909e20a199fSJeff Roberson if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) 1910e20a199fSJeff Roberson != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) { 1911e20a199fSJeff Roberson error = EINVAL; 1912e20a199fSJeff Roberson goto out; 1913e20a199fSJeff Roberson } 1914e20a199fSJeff Roberson /* 1915e20a199fSJeff Roberson * The new master must also use vtoslab(). 1916e20a199fSJeff Roberson */ 1917e20a199fSJeff Roberson if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) { 1918e20a199fSJeff Roberson error = EINVAL; 1919e20a199fSJeff Roberson goto out; 1920e20a199fSJeff Roberson } 1921e20a199fSJeff Roberson /* 1922e20a199fSJeff Roberson * Both must either be refcnt, or not be refcnt. 1923e20a199fSJeff Roberson */ 1924e20a199fSJeff Roberson if ((zone->uz_flags & UMA_ZONE_REFCNT) != 1925e20a199fSJeff Roberson (master->uz_flags & UMA_ZONE_REFCNT)) { 1926e20a199fSJeff Roberson error = EINVAL; 1927e20a199fSJeff Roberson goto out; 1928e20a199fSJeff Roberson } 1929e20a199fSJeff Roberson /* 1930e20a199fSJeff Roberson * The underlying object must be the same size. rsize 1931e20a199fSJeff Roberson * may be different. 1932e20a199fSJeff Roberson */ 1933e20a199fSJeff Roberson if (master->uz_size != zone->uz_size) { 1934e20a199fSJeff Roberson error = E2BIG; 1935e20a199fSJeff Roberson goto out; 1936e20a199fSJeff Roberson } 1937e20a199fSJeff Roberson /* 1938e20a199fSJeff Roberson * Put it at the end of the list. 1939e20a199fSJeff Roberson */ 1940e20a199fSJeff Roberson klink->kl_keg = zone_first_keg(master); 1941e20a199fSJeff Roberson LIST_FOREACH(kl, &zone->uz_kegs, kl_link) { 1942e20a199fSJeff Roberson if (LIST_NEXT(kl, kl_link) == NULL) { 1943e20a199fSJeff Roberson LIST_INSERT_AFTER(kl, klink, kl_link); 1944e20a199fSJeff Roberson break; 1945e20a199fSJeff Roberson } 1946e20a199fSJeff Roberson } 1947e20a199fSJeff Roberson klink = NULL; 1948e20a199fSJeff Roberson zone->uz_flags |= UMA_ZFLAG_MULTI; 1949e20a199fSJeff Roberson zone->uz_slab = zone_fetch_slab_multi; 1950e20a199fSJeff Roberson 1951e20a199fSJeff Roberson out: 1952e20a199fSJeff Roberson zone_unlock_pair(zone, master); 1953e20a199fSJeff Roberson if (klink != NULL) 1954e20a199fSJeff Roberson free(klink, M_TEMP); 1955e20a199fSJeff Roberson 1956e20a199fSJeff Roberson return (error); 1957e20a199fSJeff Roberson } 1958e20a199fSJeff Roberson 1959e20a199fSJeff Roberson 19608355f576SJeff Roberson /* See uma.h */ 19619c2cd7e5SJeff Roberson void 19629c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 19639c2cd7e5SJeff Roberson { 1964f4ff923bSRobert Watson 19650095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 19669c2cd7e5SJeff Roberson } 19679c2cd7e5SJeff Roberson 19689c2cd7e5SJeff Roberson /* See uma.h */ 19698355f576SJeff Roberson void * 19702cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 19718355f576SJeff Roberson { 19728355f576SJeff Roberson void *item; 19738355f576SJeff Roberson uma_cache_t cache; 19748355f576SJeff Roberson uma_bucket_t bucket; 1975*fc03d22bSJeff Roberson int lockfail; 19768355f576SJeff Roberson int cpu; 19778355f576SJeff Roberson 19788355f576SJeff Roberson /* This is the fast path allocation */ 19798355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1 19808355f576SJeff Roberson printf("Allocating one item from %s(%p)\n", zone->uz_name, zone); 19818355f576SJeff Roberson #endif 19823659f747SRobert Watson CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread, 19833659f747SRobert Watson zone->uz_name, flags); 1984a553d4b8SJeff Roberson 1985635fd505SRobert Watson if (flags & M_WAITOK) { 1986b23f72e9SBrian Feldman WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1987635fd505SRobert Watson "uma_zalloc_arg: zone \"%s\"", zone->uz_name); 19884c1cc01cSJohn Baldwin } 19898d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 19908d689e04SGleb Smirnoff if (memguard_cmp_zone(zone)) { 19918d689e04SGleb Smirnoff item = memguard_alloc(zone->uz_size, flags); 19928d689e04SGleb Smirnoff if (item != NULL) { 19938d689e04SGleb Smirnoff /* 19948d689e04SGleb Smirnoff * Avoid conflict with the use-after-free 19958d689e04SGleb Smirnoff * protecting infrastructure from INVARIANTS. 19968d689e04SGleb Smirnoff */ 19978d689e04SGleb Smirnoff if (zone->uz_init != NULL && 19988d689e04SGleb Smirnoff zone->uz_init != mtrash_init && 19998d689e04SGleb Smirnoff zone->uz_init(item, zone->uz_size, flags) != 0) 20008d689e04SGleb Smirnoff return (NULL); 20018d689e04SGleb Smirnoff if (zone->uz_ctor != NULL && 20028d689e04SGleb Smirnoff zone->uz_ctor != mtrash_ctor && 2003*fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 2004*fc03d22bSJeff Roberson flags) != 0) { 20058d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 20068d689e04SGleb Smirnoff return (NULL); 20078d689e04SGleb Smirnoff } 20088d689e04SGleb Smirnoff return (item); 20098d689e04SGleb Smirnoff } 20108d689e04SGleb Smirnoff /* This is unfortunate but should not be fatal. */ 20118d689e04SGleb Smirnoff } 20128d689e04SGleb Smirnoff #endif 20135d1ae027SRobert Watson /* 20145d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 20155d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 20165d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 20175d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 20185d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 20195d1ae027SRobert Watson * preemption and migration. We release the critical section in 20205d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 20215d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 20225d1ae027SRobert Watson * must detect and handle migration if it has occurred. 20235d1ae027SRobert Watson */ 20245d1ae027SRobert Watson critical_enter(); 20255d1ae027SRobert Watson cpu = curcpu; 20268355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 20278355f576SJeff Roberson 20288355f576SJeff Roberson zalloc_start: 20298355f576SJeff Roberson bucket = cache->uc_allocbucket; 2030*fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt > 0) { 2031cae33c14SJeff Roberson bucket->ub_cnt--; 2032cae33c14SJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt]; 20338355f576SJeff Roberson #ifdef INVARIANTS 2034cae33c14SJeff Roberson bucket->ub_bucket[bucket->ub_cnt] = NULL; 20358355f576SJeff Roberson #endif 2036*fc03d22bSJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 20378355f576SJeff Roberson cache->uc_allocs++; 20385d1ae027SRobert Watson critical_exit(); 2039*fc03d22bSJeff Roberson if (zone->uz_ctor != NULL && 2040*fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) { 20410095a784SJeff Roberson atomic_add_long(&zone->uz_fails, 1); 2042*fc03d22bSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 2043b23f72e9SBrian Feldman return (NULL); 2044b23f72e9SBrian Feldman } 2045ef72505eSJeff Roberson #ifdef INVARIANTS 2046ef72505eSJeff Roberson uma_dbg_alloc(zone, NULL, item); 2047ef72505eSJeff Roberson #endif 20482cc35ff9SJeff Roberson if (flags & M_ZERO) 2049e20a199fSJeff Roberson bzero(item, zone->uz_size); 20508355f576SJeff Roberson return (item); 2051*fc03d22bSJeff Roberson } 2052*fc03d22bSJeff Roberson 20538355f576SJeff Roberson /* 20548355f576SJeff Roberson * We have run out of items in our alloc bucket. 20558355f576SJeff Roberson * See if we can switch with our free bucket. 20568355f576SJeff Roberson */ 2057b983089aSJeff Roberson bucket = cache->uc_freebucket; 2058*fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt > 0) { 2059*fc03d22bSJeff Roberson #ifdef UMA_DEBUG_ALLOC 2060*fc03d22bSJeff Roberson printf("uma_zalloc: Swapping empty with alloc.\n"); 2061*fc03d22bSJeff Roberson #endif 20628355f576SJeff Roberson cache->uc_freebucket = cache->uc_allocbucket; 2063b983089aSJeff Roberson cache->uc_allocbucket = bucket; 20648355f576SJeff Roberson goto zalloc_start; 20658355f576SJeff Roberson } 2066*fc03d22bSJeff Roberson 2067*fc03d22bSJeff Roberson /* 2068*fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 2069*fc03d22bSJeff Roberson */ 2070*fc03d22bSJeff Roberson bucket = cache->uc_allocbucket; 2071*fc03d22bSJeff Roberson cache->uc_allocbucket = NULL; 2072*fc03d22bSJeff Roberson critical_exit(); 2073*fc03d22bSJeff Roberson if (bucket != NULL) 2074*fc03d22bSJeff Roberson bucket_free(bucket); 2075*fc03d22bSJeff Roberson 2076*fc03d22bSJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 2077*fc03d22bSJeff Roberson if (zone->uz_count == 0 || bucketdisable) 2078*fc03d22bSJeff Roberson goto zalloc_item; 2079*fc03d22bSJeff Roberson 20805d1ae027SRobert Watson /* 20815d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 20825d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 20835d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 20845d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 20855d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 20865d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 20875d1ae027SRobert Watson * the critical section. 20885d1ae027SRobert Watson */ 2089*fc03d22bSJeff Roberson lockfail = 0; 2090*fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 2091*fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 2092a553d4b8SJeff Roberson ZONE_LOCK(zone); 2093*fc03d22bSJeff Roberson lockfail = 1; 2094*fc03d22bSJeff Roberson } 20955d1ae027SRobert Watson critical_enter(); 20965d1ae027SRobert Watson cpu = curcpu; 20975d1ae027SRobert Watson cache = &zone->uz_cpu[cpu]; 20985d1ae027SRobert Watson 2099*fc03d22bSJeff Roberson /* 2100*fc03d22bSJeff Roberson * Since we have locked the zone we may as well send back our stats. 2101*fc03d22bSJeff Roberson */ 21020095a784SJeff Roberson atomic_add_long(&zone->uz_allocs, cache->uc_allocs); 21030095a784SJeff Roberson atomic_add_long(&zone->uz_frees, cache->uc_frees); 2104a553d4b8SJeff Roberson cache->uc_allocs = 0; 2105773df9abSRobert Watson cache->uc_frees = 0; 21068355f576SJeff Roberson 2107*fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 2108*fc03d22bSJeff Roberson if (cache->uc_allocbucket != NULL) { 2109*fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 2110*fc03d22bSJeff Roberson goto zalloc_start; 2111a553d4b8SJeff Roberson } 21128355f576SJeff Roberson 2113*fc03d22bSJeff Roberson /* 2114*fc03d22bSJeff Roberson * Check the zone's cache of buckets. 2115*fc03d22bSJeff Roberson */ 2116*fc03d22bSJeff Roberson if ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) { 2117cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 2118a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 21198355f576SJeff Roberson 2120a553d4b8SJeff Roberson LIST_REMOVE(bucket, ub_link); 2121a553d4b8SJeff Roberson cache->uc_allocbucket = bucket; 2122a553d4b8SJeff Roberson ZONE_UNLOCK(zone); 21238355f576SJeff Roberson goto zalloc_start; 2124a553d4b8SJeff Roberson } 21255d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 21265d1ae027SRobert Watson critical_exit(); 2127bbee39c6SJeff Roberson 2128*fc03d22bSJeff Roberson /* 2129*fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 2130*fc03d22bSJeff Roberson * handle the working set. 2131*fc03d22bSJeff Roberson */ 2132*fc03d22bSJeff Roberson if (lockfail && zone->uz_count < BUCKET_MAX && zone->uz_count != 0 && 2133*fc03d22bSJeff Roberson zone != largebucket) 2134a553d4b8SJeff Roberson zone->uz_count++; 2135*fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 2136099a0e58SBosko Milekic 21378355f576SJeff Roberson /* 2138a553d4b8SJeff Roberson * Now lets just fill a bucket and put it on the free list. If that 2139*fc03d22bSJeff Roberson * works we'll restart the allocation from the begining and it 2140*fc03d22bSJeff Roberson * will use the just filled bucket. 2141bbee39c6SJeff Roberson */ 2142*fc03d22bSJeff Roberson bucket = zone_alloc_bucket(zone, flags); 2143*fc03d22bSJeff Roberson if (bucket != NULL) { 2144*fc03d22bSJeff Roberson ZONE_LOCK(zone); 2145*fc03d22bSJeff Roberson critical_enter(); 2146*fc03d22bSJeff Roberson cpu = curcpu; 2147*fc03d22bSJeff Roberson cache = &zone->uz_cpu[cpu]; 2148*fc03d22bSJeff Roberson /* 2149*fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 2150*fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 2151*fc03d22bSJeff Roberson * the memory directly. 2152*fc03d22bSJeff Roberson */ 2153*fc03d22bSJeff Roberson if (cache->uc_allocbucket == NULL) 2154*fc03d22bSJeff Roberson cache->uc_allocbucket = bucket; 2155*fc03d22bSJeff Roberson else 2156*fc03d22bSJeff Roberson LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link); 2157bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 2158*fc03d22bSJeff Roberson goto zalloc_start; 2159bbee39c6SJeff Roberson } 2160*fc03d22bSJeff Roberson 2161bbee39c6SJeff Roberson /* 2162bbee39c6SJeff Roberson * We may not be able to get a bucket so return an actual item. 2163bbee39c6SJeff Roberson */ 2164bbee39c6SJeff Roberson #ifdef UMA_DEBUG 2165bbee39c6SJeff Roberson printf("uma_zalloc_arg: Bucketzone returned NULL\n"); 2166bbee39c6SJeff Roberson #endif 2167bbee39c6SJeff Roberson 2168*fc03d22bSJeff Roberson zalloc_item: 2169e20a199fSJeff Roberson item = zone_alloc_item(zone, udata, flags); 2170*fc03d22bSJeff Roberson 2171e20a199fSJeff Roberson return (item); 2172bbee39c6SJeff Roberson } 2173bbee39c6SJeff Roberson 2174bbee39c6SJeff Roberson static uma_slab_t 2175e20a199fSJeff Roberson keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags) 2176bbee39c6SJeff Roberson { 2177bbee39c6SJeff Roberson uma_slab_t slab; 2178099a0e58SBosko Milekic 2179e20a199fSJeff Roberson mtx_assert(&keg->uk_lock, MA_OWNED); 2180bbee39c6SJeff Roberson slab = NULL; 2181bbee39c6SJeff Roberson 2182bbee39c6SJeff Roberson for (;;) { 2183bbee39c6SJeff Roberson /* 2184bbee39c6SJeff Roberson * Find a slab with some space. Prefer slabs that are partially 2185bbee39c6SJeff Roberson * used over those that are totally full. This helps to reduce 2186bbee39c6SJeff Roberson * fragmentation. 2187bbee39c6SJeff Roberson */ 2188099a0e58SBosko Milekic if (keg->uk_free != 0) { 2189099a0e58SBosko Milekic if (!LIST_EMPTY(&keg->uk_part_slab)) { 2190099a0e58SBosko Milekic slab = LIST_FIRST(&keg->uk_part_slab); 2191bbee39c6SJeff Roberson } else { 2192099a0e58SBosko Milekic slab = LIST_FIRST(&keg->uk_free_slab); 2193bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 2194099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_part_slab, slab, 2195bbee39c6SJeff Roberson us_link); 2196bbee39c6SJeff Roberson } 2197e20a199fSJeff Roberson MPASS(slab->us_keg == keg); 2198bbee39c6SJeff Roberson return (slab); 2199bbee39c6SJeff Roberson } 2200bbee39c6SJeff Roberson 2201bbee39c6SJeff Roberson /* 2202bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 2203bbee39c6SJeff Roberson */ 2204bbee39c6SJeff Roberson if (flags & M_NOVM) 2205bbee39c6SJeff Roberson break; 2206bbee39c6SJeff Roberson 2207e20a199fSJeff Roberson if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) { 2208099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_FULL; 2209e20a199fSJeff Roberson /* 2210e20a199fSJeff Roberson * If this is not a multi-zone, set the FULL bit. 2211e20a199fSJeff Roberson * Otherwise slab_multi() takes care of it. 2212e20a199fSJeff Roberson */ 22132f891cd5SPawel Jakub Dawidek if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) { 2214e20a199fSJeff Roberson zone->uz_flags |= UMA_ZFLAG_FULL; 22152f891cd5SPawel Jakub Dawidek zone_log_warning(zone); 22162f891cd5SPawel Jakub Dawidek } 2217ebc85edfSJeff Roberson if (flags & M_NOWAIT) 2218bbee39c6SJeff Roberson break; 2219c288b548SEitan Adler zone->uz_sleeps++; 2220e20a199fSJeff Roberson msleep(keg, &keg->uk_lock, PVM, "keglimit", 0); 2221bbee39c6SJeff Roberson continue; 2222bbee39c6SJeff Roberson } 2223e20a199fSJeff Roberson slab = keg_alloc_slab(keg, zone, flags); 2224bbee39c6SJeff Roberson /* 2225bbee39c6SJeff Roberson * If we got a slab here it's safe to mark it partially used 2226bbee39c6SJeff Roberson * and return. We assume that the caller is going to remove 2227bbee39c6SJeff Roberson * at least one item. 2228bbee39c6SJeff Roberson */ 2229bbee39c6SJeff Roberson if (slab) { 2230e20a199fSJeff Roberson MPASS(slab->us_keg == keg); 2231099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link); 2232bbee39c6SJeff Roberson return (slab); 2233bbee39c6SJeff Roberson } 2234bbee39c6SJeff Roberson /* 2235bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 2236bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 2237bbee39c6SJeff Roberson * fail. 2238bbee39c6SJeff Roberson */ 2239bbee39c6SJeff Roberson flags |= M_NOVM; 2240bbee39c6SJeff Roberson } 2241bbee39c6SJeff Roberson return (slab); 2242bbee39c6SJeff Roberson } 2243bbee39c6SJeff Roberson 2244e20a199fSJeff Roberson static inline void 2245e20a199fSJeff Roberson zone_relock(uma_zone_t zone, uma_keg_t keg) 2246e20a199fSJeff Roberson { 2247e20a199fSJeff Roberson if (zone->uz_lock != &keg->uk_lock) { 2248e20a199fSJeff Roberson KEG_UNLOCK(keg); 2249e20a199fSJeff Roberson ZONE_LOCK(zone); 2250e20a199fSJeff Roberson } 2251e20a199fSJeff Roberson } 2252e20a199fSJeff Roberson 2253e20a199fSJeff Roberson static inline void 2254e20a199fSJeff Roberson keg_relock(uma_keg_t keg, uma_zone_t zone) 2255e20a199fSJeff Roberson { 2256e20a199fSJeff Roberson if (zone->uz_lock != &keg->uk_lock) { 2257e20a199fSJeff Roberson ZONE_UNLOCK(zone); 2258e20a199fSJeff Roberson KEG_LOCK(keg); 2259e20a199fSJeff Roberson } 2260e20a199fSJeff Roberson } 2261e20a199fSJeff Roberson 2262e20a199fSJeff Roberson static uma_slab_t 2263e20a199fSJeff Roberson zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags) 2264e20a199fSJeff Roberson { 2265e20a199fSJeff Roberson uma_slab_t slab; 2266e20a199fSJeff Roberson 2267e20a199fSJeff Roberson if (keg == NULL) 2268e20a199fSJeff Roberson keg = zone_first_keg(zone); 2269e20a199fSJeff Roberson 2270e20a199fSJeff Roberson for (;;) { 2271e20a199fSJeff Roberson slab = keg_fetch_slab(keg, zone, flags); 2272e20a199fSJeff Roberson if (slab) 2273e20a199fSJeff Roberson return (slab); 2274e20a199fSJeff Roberson if (flags & (M_NOWAIT | M_NOVM)) 2275e20a199fSJeff Roberson break; 2276e20a199fSJeff Roberson } 2277e20a199fSJeff Roberson return (NULL); 2278e20a199fSJeff Roberson } 2279e20a199fSJeff Roberson 2280e20a199fSJeff Roberson /* 2281e20a199fSJeff Roberson * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns 2282e20a199fSJeff Roberson * with the keg locked. Caller must call zone_relock() afterwards if the 2283e20a199fSJeff Roberson * zone lock is required. On NULL the zone lock is held. 2284e20a199fSJeff Roberson * 2285e20a199fSJeff Roberson * The last pointer is used to seed the search. It is not required. 2286e20a199fSJeff Roberson */ 2287e20a199fSJeff Roberson static uma_slab_t 2288e20a199fSJeff Roberson zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags) 2289e20a199fSJeff Roberson { 2290e20a199fSJeff Roberson uma_klink_t klink; 2291e20a199fSJeff Roberson uma_slab_t slab; 2292e20a199fSJeff Roberson uma_keg_t keg; 2293e20a199fSJeff Roberson int flags; 2294e20a199fSJeff Roberson int empty; 2295e20a199fSJeff Roberson int full; 2296e20a199fSJeff Roberson 2297e20a199fSJeff Roberson /* 2298e20a199fSJeff Roberson * Don't wait on the first pass. This will skip limit tests 2299e20a199fSJeff Roberson * as well. We don't want to block if we can find a provider 2300e20a199fSJeff Roberson * without blocking. 2301e20a199fSJeff Roberson */ 2302e20a199fSJeff Roberson flags = (rflags & ~M_WAITOK) | M_NOWAIT; 2303e20a199fSJeff Roberson /* 2304e20a199fSJeff Roberson * Use the last slab allocated as a hint for where to start 2305e20a199fSJeff Roberson * the search. 2306e20a199fSJeff Roberson */ 2307e20a199fSJeff Roberson if (last) { 2308e20a199fSJeff Roberson slab = keg_fetch_slab(last, zone, flags); 2309e20a199fSJeff Roberson if (slab) 2310e20a199fSJeff Roberson return (slab); 2311e20a199fSJeff Roberson zone_relock(zone, last); 2312e20a199fSJeff Roberson last = NULL; 2313e20a199fSJeff Roberson } 2314e20a199fSJeff Roberson /* 2315e20a199fSJeff Roberson * Loop until we have a slab incase of transient failures 2316e20a199fSJeff Roberson * while M_WAITOK is specified. I'm not sure this is 100% 2317e20a199fSJeff Roberson * required but we've done it for so long now. 2318e20a199fSJeff Roberson */ 2319e20a199fSJeff Roberson for (;;) { 2320e20a199fSJeff Roberson empty = 0; 2321e20a199fSJeff Roberson full = 0; 2322e20a199fSJeff Roberson /* 2323e20a199fSJeff Roberson * Search the available kegs for slabs. Be careful to hold the 2324e20a199fSJeff Roberson * correct lock while calling into the keg layer. 2325e20a199fSJeff Roberson */ 2326e20a199fSJeff Roberson LIST_FOREACH(klink, &zone->uz_kegs, kl_link) { 2327e20a199fSJeff Roberson keg = klink->kl_keg; 2328e20a199fSJeff Roberson keg_relock(keg, zone); 2329e20a199fSJeff Roberson if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) { 2330e20a199fSJeff Roberson slab = keg_fetch_slab(keg, zone, flags); 2331e20a199fSJeff Roberson if (slab) 2332e20a199fSJeff Roberson return (slab); 2333e20a199fSJeff Roberson } 2334e20a199fSJeff Roberson if (keg->uk_flags & UMA_ZFLAG_FULL) 2335e20a199fSJeff Roberson full++; 2336e20a199fSJeff Roberson else 2337e20a199fSJeff Roberson empty++; 2338e20a199fSJeff Roberson zone_relock(zone, keg); 2339e20a199fSJeff Roberson } 2340e20a199fSJeff Roberson if (rflags & (M_NOWAIT | M_NOVM)) 2341e20a199fSJeff Roberson break; 2342e20a199fSJeff Roberson flags = rflags; 2343e20a199fSJeff Roberson /* 2344e20a199fSJeff Roberson * All kegs are full. XXX We can't atomically check all kegs 2345e20a199fSJeff Roberson * and sleep so just sleep for a short period and retry. 2346e20a199fSJeff Roberson */ 2347e20a199fSJeff Roberson if (full && !empty) { 2348e20a199fSJeff Roberson zone->uz_flags |= UMA_ZFLAG_FULL; 2349bf965959SSean Bruno zone->uz_sleeps++; 23502f891cd5SPawel Jakub Dawidek zone_log_warning(zone); 2351e20a199fSJeff Roberson msleep(zone, zone->uz_lock, PVM, "zonelimit", hz/100); 2352e20a199fSJeff Roberson zone->uz_flags &= ~UMA_ZFLAG_FULL; 2353e20a199fSJeff Roberson continue; 2354e20a199fSJeff Roberson } 2355e20a199fSJeff Roberson } 2356e20a199fSJeff Roberson return (NULL); 2357e20a199fSJeff Roberson } 2358e20a199fSJeff Roberson 2359d56368d7SBosko Milekic static void * 23600095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 2361bbee39c6SJeff Roberson { 2362bbee39c6SJeff Roberson void *item; 236385dcf349SGleb Smirnoff uint8_t freei; 2364bbee39c6SJeff Roberson 23650095a784SJeff Roberson MPASS(keg == slab->us_keg); 2366e20a199fSJeff Roberson mtx_assert(&keg->uk_lock, MA_OWNED); 2367099a0e58SBosko Milekic 2368ef72505eSJeff Roberson freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1; 2369ef72505eSJeff Roberson BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free); 2370099a0e58SBosko Milekic item = slab->us_data + (keg->uk_rsize * freei); 2371bbee39c6SJeff Roberson slab->us_freecount--; 2372099a0e58SBosko Milekic keg->uk_free--; 2373ef72505eSJeff Roberson 2374bbee39c6SJeff Roberson /* Move this slab to the full list */ 2375bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 2376bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 2377099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link); 2378bbee39c6SJeff Roberson } 2379bbee39c6SJeff Roberson 2380bbee39c6SJeff Roberson return (item); 2381bbee39c6SJeff Roberson } 2382bbee39c6SJeff Roberson 2383bbee39c6SJeff Roberson static int 23840095a784SJeff Roberson zone_import(uma_zone_t zone, void **bucket, int max, int flags) 23850095a784SJeff Roberson { 23860095a784SJeff Roberson uma_slab_t slab; 23870095a784SJeff Roberson uma_keg_t keg; 23880095a784SJeff Roberson int i; 23890095a784SJeff Roberson 23900095a784SJeff Roberson ZONE_LOCK(zone); 23910095a784SJeff Roberson /* Try to keep the buckets totally full */ 23920095a784SJeff Roberson slab = NULL; 23930095a784SJeff Roberson keg = NULL; 23940095a784SJeff Roberson for (i = 0; i < max; ) { 23950095a784SJeff Roberson if ((slab = zone->uz_slab(zone, keg, flags)) == NULL) 23960095a784SJeff Roberson break; 23970095a784SJeff Roberson keg = slab->us_keg; 23980095a784SJeff Roberson while (slab->us_freecount && i < max) 23990095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 24000095a784SJeff Roberson 24010095a784SJeff Roberson /* Don't block on the next fill */ 24020095a784SJeff Roberson flags &= ~M_WAITOK; 24030095a784SJeff Roberson flags |= M_NOWAIT; 24040095a784SJeff Roberson } 24050095a784SJeff Roberson if (slab != NULL) 24060095a784SJeff Roberson KEG_UNLOCK(keg); 24070095a784SJeff Roberson else 24080095a784SJeff Roberson ZONE_UNLOCK(zone); 24090095a784SJeff Roberson 24100095a784SJeff Roberson return i; 24110095a784SJeff Roberson } 24120095a784SJeff Roberson 2413*fc03d22bSJeff Roberson static uma_bucket_t 2414e20a199fSJeff Roberson zone_alloc_bucket(uma_zone_t zone, int flags) 2415bbee39c6SJeff Roberson { 2416bbee39c6SJeff Roberson uma_bucket_t bucket; 241718aa2de5SJeff Roberson int bflags; 24180095a784SJeff Roberson int max; 2419bbee39c6SJeff Roberson 24200095a784SJeff Roberson max = zone->uz_count; 2421*fc03d22bSJeff Roberson bflags = M_NOWAIT; 24220095a784SJeff Roberson if (zone->uz_flags & UMA_ZFLAG_CACHEONLY) 24230095a784SJeff Roberson bflags |= M_NOVM; 24240095a784SJeff Roberson bucket = bucket_alloc(zone->uz_count, bflags); 24250095a784SJeff Roberson if (bucket == NULL) 24260095a784SJeff Roberson goto out; 24270095a784SJeff Roberson 24280095a784SJeff Roberson max = MIN(bucket->ub_entries, max); 24290095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 24300095a784SJeff Roberson max, flags); 24310095a784SJeff Roberson 24320095a784SJeff Roberson /* 24330095a784SJeff Roberson * Initialize the memory if necessary. 24340095a784SJeff Roberson */ 24350095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 2436099a0e58SBosko Milekic int i; 2437bbee39c6SJeff Roberson 24380095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 2439e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 24400095a784SJeff Roberson flags) != 0) 2441b23f72e9SBrian Feldman break; 2442b23f72e9SBrian Feldman /* 2443b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 2444b23f72e9SBrian Feldman * rest back onto the freelist. 2445b23f72e9SBrian Feldman */ 2446b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 24470095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket[i], 24480095a784SJeff Roberson bucket->ub_cnt - i); 2449a5a262c6SBosko Milekic #ifdef INVARIANTS 24500095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 24510095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 2452a5a262c6SBosko Milekic #endif 2453b23f72e9SBrian Feldman bucket->ub_cnt = i; 2454b23f72e9SBrian Feldman } 2455099a0e58SBosko Milekic } 2456099a0e58SBosko Milekic 24570095a784SJeff Roberson out: 2458*fc03d22bSJeff Roberson if (bucket == NULL || bucket->ub_cnt == 0) { 24590095a784SJeff Roberson if (bucket != NULL) 2460cae33c14SJeff Roberson bucket_free(bucket); 2461*fc03d22bSJeff Roberson atomic_add_long(&zone->uz_fails, 1); 2462*fc03d22bSJeff Roberson return (NULL); 2463bbee39c6SJeff Roberson } 2464*fc03d22bSJeff Roberson 2465*fc03d22bSJeff Roberson return (bucket); 2466*fc03d22bSJeff Roberson } 2467*fc03d22bSJeff Roberson 24688355f576SJeff Roberson /* 24690095a784SJeff Roberson * Allocates a single item from a zone. 24708355f576SJeff Roberson * 24718355f576SJeff Roberson * Arguments 24728355f576SJeff Roberson * zone The zone to alloc for. 24738355f576SJeff Roberson * udata The data to be passed to the constructor. 2474a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 24758355f576SJeff Roberson * 24768355f576SJeff Roberson * Returns 24778355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 2478bbee39c6SJeff Roberson * An item if successful 24798355f576SJeff Roberson */ 24808355f576SJeff Roberson 24818355f576SJeff Roberson static void * 2482e20a199fSJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int flags) 24838355f576SJeff Roberson { 24848355f576SJeff Roberson void *item; 24858355f576SJeff Roberson 24868355f576SJeff Roberson item = NULL; 24878355f576SJeff Roberson 24888355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC 24898355f576SJeff Roberson printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone); 24908355f576SJeff Roberson #endif 24910095a784SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, flags) != 1) 24920095a784SJeff Roberson goto fail; 24930095a784SJeff Roberson atomic_add_long(&zone->uz_allocs, 1); 24948355f576SJeff Roberson 2495099a0e58SBosko Milekic /* 2496099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 2497099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 2498099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 2499099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 2500099a0e58SBosko Milekic */ 2501b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 2502e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 25030095a784SJeff Roberson zone_free_item(zone, item, udata, SKIP_FINI); 25040095a784SJeff Roberson goto fail; 2505b23f72e9SBrian Feldman } 2506b23f72e9SBrian Feldman } 2507b23f72e9SBrian Feldman if (zone->uz_ctor != NULL) { 2508e20a199fSJeff Roberson if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) { 25090095a784SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 25100095a784SJeff Roberson goto fail; 2511b23f72e9SBrian Feldman } 2512b23f72e9SBrian Feldman } 2513ef72505eSJeff Roberson #ifdef INVARIANTS 25140095a784SJeff Roberson uma_dbg_alloc(zone, NULL, item); 2515ef72505eSJeff Roberson #endif 25162cc35ff9SJeff Roberson if (flags & M_ZERO) 2517e20a199fSJeff Roberson bzero(item, zone->uz_size); 25188355f576SJeff Roberson 25198355f576SJeff Roberson return (item); 25200095a784SJeff Roberson 25210095a784SJeff Roberson fail: 25220095a784SJeff Roberson atomic_add_long(&zone->uz_fails, 1); 25230095a784SJeff Roberson return (NULL); 25248355f576SJeff Roberson } 25258355f576SJeff Roberson 25268355f576SJeff Roberson /* See uma.h */ 25278355f576SJeff Roberson void 25288355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 25298355f576SJeff Roberson { 25308355f576SJeff Roberson uma_cache_t cache; 25318355f576SJeff Roberson uma_bucket_t bucket; 25324741dcbfSJeff Roberson int bflags; 25338355f576SJeff Roberson int cpu; 25348355f576SJeff Roberson 25358355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC_1 25368355f576SJeff Roberson printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone); 25378355f576SJeff Roberson #endif 25383659f747SRobert Watson CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread, 25393659f747SRobert Watson zone->uz_name); 25403659f747SRobert Watson 254120ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 254220ed0cb0SMatthew D Fleming if (item == NULL) 254320ed0cb0SMatthew D Fleming return; 25448d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 25458d689e04SGleb Smirnoff if (is_memguard_addr(item)) { 25468d689e04SGleb Smirnoff if (zone->uz_dtor != NULL && zone->uz_dtor != mtrash_dtor) 25478d689e04SGleb Smirnoff zone->uz_dtor(item, zone->uz_size, udata); 25488d689e04SGleb Smirnoff if (zone->uz_fini != NULL && zone->uz_fini != mtrash_fini) 25498d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 25508d689e04SGleb Smirnoff memguard_free(item); 25518d689e04SGleb Smirnoff return; 25528d689e04SGleb Smirnoff } 25538d689e04SGleb Smirnoff #endif 25545d1ae027SRobert Watson #ifdef INVARIANTS 2555e20a199fSJeff Roberson if (zone->uz_flags & UMA_ZONE_MALLOC) 25565d1ae027SRobert Watson uma_dbg_free(zone, udata, item); 25575d1ae027SRobert Watson else 25585d1ae027SRobert Watson uma_dbg_free(zone, NULL, item); 25595d1ae027SRobert Watson #endif 2560*fc03d22bSJeff Roberson if (zone->uz_dtor != NULL) 2561ef72505eSJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 2562ef72505eSJeff Roberson 2563af7f9b97SJeff Roberson /* 2564af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 2565af7f9b97SJeff Roberson * a little longer for the limits to be reset. 2566af7f9b97SJeff Roberson */ 2567e20a199fSJeff Roberson if (zone->uz_flags & UMA_ZFLAG_FULL) 2568*fc03d22bSJeff Roberson goto zfree_item; 2569af7f9b97SJeff Roberson 25705d1ae027SRobert Watson /* 25715d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 25725d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 25735d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 25745d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 25755d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 25765d1ae027SRobert Watson * preemption and migration. We release the critical section in 25775d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 25785d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 25795d1ae027SRobert Watson * detect and handle migration if it has occurred. 25805d1ae027SRobert Watson */ 2581a553d4b8SJeff Roberson zfree_restart: 25825d1ae027SRobert Watson critical_enter(); 25835d1ae027SRobert Watson cpu = curcpu; 25848355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 25858355f576SJeff Roberson 25868355f576SJeff Roberson zfree_start: 2587a553d4b8SJeff Roberson /* 2588*fc03d22bSJeff Roberson * Try to free into the allocbucket first to give LIFO ordering 2589*fc03d22bSJeff Roberson * for cache-hot datastructures. Spill over into the freebucket 2590*fc03d22bSJeff Roberson * if necessary. Alloc will swap them if one runs dry. 2591a553d4b8SJeff Roberson */ 2592*fc03d22bSJeff Roberson bucket = cache->uc_allocbucket; 2593*fc03d22bSJeff Roberson if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries) 2594*fc03d22bSJeff Roberson bucket = cache->uc_freebucket; 2595*fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) { 2596cae33c14SJeff Roberson KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL, 25978355f576SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 2598cae33c14SJeff Roberson bucket->ub_bucket[bucket->ub_cnt] = item; 2599cae33c14SJeff Roberson bucket->ub_cnt++; 2600773df9abSRobert Watson cache->uc_frees++; 26015d1ae027SRobert Watson critical_exit(); 26028355f576SJeff Roberson return; 2603*fc03d22bSJeff Roberson } 2604*fc03d22bSJeff Roberson 26058355f576SJeff Roberson /* 26065d1ae027SRobert Watson * We must go back the zone, which requires acquiring the zone lock, 26075d1ae027SRobert Watson * which in turn means we must release and re-acquire the critical 26085d1ae027SRobert Watson * section. Since the critical section is released, we may be 26095d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 26105d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 26115d1ae027SRobert Watson * the critical section. 26128355f576SJeff Roberson */ 26135d1ae027SRobert Watson critical_exit(); 2614*fc03d22bSJeff Roberson if (zone->uz_count == 0 || bucketdisable) 2615*fc03d22bSJeff Roberson goto zfree_item; 2616*fc03d22bSJeff Roberson 26178355f576SJeff Roberson ZONE_LOCK(zone); 26185d1ae027SRobert Watson critical_enter(); 26195d1ae027SRobert Watson cpu = curcpu; 26205d1ae027SRobert Watson cache = &zone->uz_cpu[cpu]; 26218355f576SJeff Roberson 2622*fc03d22bSJeff Roberson /* 2623*fc03d22bSJeff Roberson * Since we have locked the zone we may as well send back our stats. 2624*fc03d22bSJeff Roberson */ 26250095a784SJeff Roberson atomic_add_long(&zone->uz_allocs, cache->uc_allocs); 26260095a784SJeff Roberson atomic_add_long(&zone->uz_frees, cache->uc_frees); 2627f4ff923bSRobert Watson cache->uc_allocs = 0; 2628f4ff923bSRobert Watson cache->uc_frees = 0; 2629f4ff923bSRobert Watson 26308355f576SJeff Roberson bucket = cache->uc_freebucket; 2631*fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) { 2632*fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 2633*fc03d22bSJeff Roberson goto zfree_start; 2634*fc03d22bSJeff Roberson } 26358355f576SJeff Roberson cache->uc_freebucket = NULL; 26368355f576SJeff Roberson 26378355f576SJeff Roberson /* Can we throw this on the zone full list? */ 26388355f576SJeff Roberson if (bucket != NULL) { 26398355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC 26408355f576SJeff Roberson printf("uma_zfree: Putting old bucket on the free list.\n"); 26418355f576SJeff Roberson #endif 2642cae33c14SJeff Roberson /* ub_cnt is pointing to the last free item */ 2643cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 26448355f576SJeff Roberson ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n")); 2645*fc03d22bSJeff Roberson LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link); 26468355f576SJeff Roberson } 2647*fc03d22bSJeff Roberson 26485d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 26495d1ae027SRobert Watson critical_exit(); 2650a553d4b8SJeff Roberson 2651a553d4b8SJeff Roberson /* And the zone.. */ 2652a553d4b8SJeff Roberson ZONE_UNLOCK(zone); 2653a553d4b8SJeff Roberson 26548355f576SJeff Roberson #ifdef UMA_DEBUG_ALLOC 26558355f576SJeff Roberson printf("uma_zfree: Allocating new free bucket.\n"); 26568355f576SJeff Roberson #endif 26574741dcbfSJeff Roberson bflags = M_NOWAIT; 2658e20a199fSJeff Roberson if (zone->uz_flags & UMA_ZFLAG_CACHEONLY) 26594741dcbfSJeff Roberson bflags |= M_NOVM; 2660cae33c14SJeff Roberson bucket = bucket_alloc(zone->uz_count, bflags); 26614741dcbfSJeff Roberson if (bucket) { 2662*fc03d22bSJeff Roberson critical_enter(); 2663*fc03d22bSJeff Roberson cpu = curcpu; 2664*fc03d22bSJeff Roberson cache = &zone->uz_cpu[cpu]; 2665*fc03d22bSJeff Roberson if (cache->uc_freebucket == NULL) { 2666*fc03d22bSJeff Roberson cache->uc_freebucket = bucket; 2667*fc03d22bSJeff Roberson goto zfree_start; 2668*fc03d22bSJeff Roberson } 2669*fc03d22bSJeff Roberson /* 2670*fc03d22bSJeff Roberson * We lost the race, start over. We have to drop our 2671*fc03d22bSJeff Roberson * critical section to free the bucket. 2672*fc03d22bSJeff Roberson */ 2673*fc03d22bSJeff Roberson critical_exit(); 2674*fc03d22bSJeff Roberson bucket_free(bucket); 2675a553d4b8SJeff Roberson goto zfree_restart; 26768355f576SJeff Roberson } 26778355f576SJeff Roberson 2678a553d4b8SJeff Roberson /* 2679a553d4b8SJeff Roberson * If nothing else caught this, we'll just do an internal free. 2680a553d4b8SJeff Roberson */ 2681*fc03d22bSJeff Roberson zfree_item: 26820095a784SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 26838355f576SJeff Roberson 26848355f576SJeff Roberson return; 26858355f576SJeff Roberson } 26868355f576SJeff Roberson 26878355f576SJeff Roberson static void 26880095a784SJeff Roberson slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item) 26898355f576SJeff Roberson { 269085dcf349SGleb Smirnoff uint8_t freei; 2691099a0e58SBosko Milekic 26920095a784SJeff Roberson mtx_assert(&keg->uk_lock, MA_OWNED); 2693e20a199fSJeff Roberson MPASS(keg == slab->us_keg); 26948355f576SJeff Roberson 26958355f576SJeff Roberson /* Do we need to remove from any lists? */ 2696099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 26978355f576SJeff Roberson LIST_REMOVE(slab, us_link); 2698099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link); 26998355f576SJeff Roberson } else if (slab->us_freecount == 0) { 27008355f576SJeff Roberson LIST_REMOVE(slab, us_link); 2701099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link); 27028355f576SJeff Roberson } 27038355f576SJeff Roberson 2704ef72505eSJeff Roberson /* Slab management. */ 2705ef72505eSJeff Roberson freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize; 2706ef72505eSJeff Roberson BIT_SET(SLAB_SETSIZE, freei, &slab->us_free); 27078355f576SJeff Roberson slab->us_freecount++; 27088355f576SJeff Roberson 2709ef72505eSJeff Roberson /* Keg statistics. */ 2710099a0e58SBosko Milekic keg->uk_free++; 27110095a784SJeff Roberson } 27120095a784SJeff Roberson 27130095a784SJeff Roberson static void 27140095a784SJeff Roberson zone_release(uma_zone_t zone, void **bucket, int cnt) 27150095a784SJeff Roberson { 27160095a784SJeff Roberson void *item; 27170095a784SJeff Roberson uma_slab_t slab; 27180095a784SJeff Roberson uma_keg_t keg; 27190095a784SJeff Roberson uint8_t *mem; 27200095a784SJeff Roberson int clearfull; 27210095a784SJeff Roberson int i; 27228355f576SJeff Roberson 2723e20a199fSJeff Roberson clearfull = 0; 27240095a784SJeff Roberson ZONE_LOCK(zone); 27250095a784SJeff Roberson keg = zone_first_keg(zone); 27260095a784SJeff Roberson for (i = 0; i < cnt; i++) { 27270095a784SJeff Roberson item = bucket[i]; 27280095a784SJeff Roberson if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) { 27290095a784SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 27300095a784SJeff Roberson if (zone->uz_flags & UMA_ZONE_HASH) { 27310095a784SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 27320095a784SJeff Roberson } else { 27330095a784SJeff Roberson mem += keg->uk_pgoff; 27340095a784SJeff Roberson slab = (uma_slab_t)mem; 27350095a784SJeff Roberson } 27360095a784SJeff Roberson } else { 27370095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 27380095a784SJeff Roberson if (slab->us_keg != keg) { 27390095a784SJeff Roberson KEG_UNLOCK(keg); 27400095a784SJeff Roberson keg = slab->us_keg; 27410095a784SJeff Roberson KEG_LOCK(keg); 27420095a784SJeff Roberson } 27430095a784SJeff Roberson } 27440095a784SJeff Roberson slab_free_item(keg, slab, item); 2745099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZFLAG_FULL) { 2746e20a199fSJeff Roberson if (keg->uk_pages < keg->uk_maxpages) { 2747099a0e58SBosko Milekic keg->uk_flags &= ~UMA_ZFLAG_FULL; 2748e20a199fSJeff Roberson clearfull = 1; 2749e20a199fSJeff Roberson } 2750af7f9b97SJeff Roberson 275177380291SMohan Srinivasan /* 2752ef72505eSJeff Roberson * We can handle one more allocation. Since we're 2753ef72505eSJeff Roberson * clearing ZFLAG_FULL, wake up all procs blocked 2754ef72505eSJeff Roberson * on pages. This should be uncommon, so keeping this 2755ef72505eSJeff Roberson * simple for now (rather than adding count of blocked 275677380291SMohan Srinivasan * threads etc). 275777380291SMohan Srinivasan */ 275877380291SMohan Srinivasan wakeup(keg); 2759af7f9b97SJeff Roberson } 27600095a784SJeff Roberson } 2761e20a199fSJeff Roberson zone_relock(zone, keg); 27620095a784SJeff Roberson if (clearfull) { 2763e20a199fSJeff Roberson zone->uz_flags &= ~UMA_ZFLAG_FULL; 2764e20a199fSJeff Roberson wakeup(zone); 27650095a784SJeff Roberson } 2766605cbd6aSJeff Roberson ZONE_UNLOCK(zone); 2767ef72505eSJeff Roberson 27688355f576SJeff Roberson } 27698355f576SJeff Roberson 27700095a784SJeff Roberson /* 27710095a784SJeff Roberson * Frees a single item to any zone. 27720095a784SJeff Roberson * 27730095a784SJeff Roberson * Arguments: 27740095a784SJeff Roberson * zone The zone to free to 27750095a784SJeff Roberson * item The item we're freeing 27760095a784SJeff Roberson * udata User supplied data for the dtor 27770095a784SJeff Roberson * skip Skip dtors and finis 27780095a784SJeff Roberson */ 27790095a784SJeff Roberson static void 27800095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 27810095a784SJeff Roberson { 27820095a784SJeff Roberson 27830095a784SJeff Roberson #ifdef INVARIANTS 27840095a784SJeff Roberson if (skip == SKIP_NONE) { 27850095a784SJeff Roberson if (zone->uz_flags & UMA_ZONE_MALLOC) 27860095a784SJeff Roberson uma_dbg_free(zone, udata, item); 27870095a784SJeff Roberson else 27880095a784SJeff Roberson uma_dbg_free(zone, NULL, item); 27890095a784SJeff Roberson } 27900095a784SJeff Roberson #endif 27910095a784SJeff Roberson if (skip < SKIP_DTOR && zone->uz_dtor) 27920095a784SJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 27930095a784SJeff Roberson 27940095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 27950095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 27960095a784SJeff Roberson 27970095a784SJeff Roberson atomic_add_long(&zone->uz_frees, 1); 27980095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 27990095a784SJeff Roberson } 28000095a784SJeff Roberson 28018355f576SJeff Roberson /* See uma.h */ 28021c6cae97SLawrence Stewart int 2803736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 2804736ee590SJeff Roberson { 2805099a0e58SBosko Milekic uma_keg_t keg; 2806099a0e58SBosko Milekic 2807e20a199fSJeff Roberson keg = zone_first_keg(zone); 28080095a784SJeff Roberson if (keg == NULL) 28090095a784SJeff Roberson return (0); 28100095a784SJeff Roberson ZONE_LOCK(zone); 2811e20a199fSJeff Roberson keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera; 2812099a0e58SBosko Milekic if (keg->uk_maxpages * keg->uk_ipers < nitems) 2813e20a199fSJeff Roberson keg->uk_maxpages += keg->uk_ppera; 28141c6cae97SLawrence Stewart nitems = keg->uk_maxpages * keg->uk_ipers; 2815736ee590SJeff Roberson ZONE_UNLOCK(zone); 28161c6cae97SLawrence Stewart 28171c6cae97SLawrence Stewart return (nitems); 2818736ee590SJeff Roberson } 2819736ee590SJeff Roberson 2820736ee590SJeff Roberson /* See uma.h */ 2821e49471b0SAndre Oppermann int 2822e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 2823e49471b0SAndre Oppermann { 2824e49471b0SAndre Oppermann int nitems; 2825e49471b0SAndre Oppermann uma_keg_t keg; 2826e49471b0SAndre Oppermann 2827e49471b0SAndre Oppermann keg = zone_first_keg(zone); 28280095a784SJeff Roberson if (keg == NULL) 28290095a784SJeff Roberson return (0); 28300095a784SJeff Roberson ZONE_LOCK(zone); 2831e49471b0SAndre Oppermann nitems = keg->uk_maxpages * keg->uk_ipers; 2832e49471b0SAndre Oppermann ZONE_UNLOCK(zone); 2833e49471b0SAndre Oppermann 2834e49471b0SAndre Oppermann return (nitems); 2835e49471b0SAndre Oppermann } 2836e49471b0SAndre Oppermann 2837e49471b0SAndre Oppermann /* See uma.h */ 28382f891cd5SPawel Jakub Dawidek void 28392f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 28402f891cd5SPawel Jakub Dawidek { 28412f891cd5SPawel Jakub Dawidek 28422f891cd5SPawel Jakub Dawidek ZONE_LOCK(zone); 28432f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 28442f891cd5SPawel Jakub Dawidek ZONE_UNLOCK(zone); 28452f891cd5SPawel Jakub Dawidek } 28462f891cd5SPawel Jakub Dawidek 28472f891cd5SPawel Jakub Dawidek /* See uma.h */ 2848c4ae7908SLawrence Stewart int 2849c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 2850c4ae7908SLawrence Stewart { 2851c4ae7908SLawrence Stewart int64_t nitems; 2852c4ae7908SLawrence Stewart u_int i; 2853c4ae7908SLawrence Stewart 2854c4ae7908SLawrence Stewart ZONE_LOCK(zone); 2855c4ae7908SLawrence Stewart nitems = zone->uz_allocs - zone->uz_frees; 2856c4ae7908SLawrence Stewart CPU_FOREACH(i) { 2857c4ae7908SLawrence Stewart /* 2858c4ae7908SLawrence Stewart * See the comment in sysctl_vm_zone_stats() regarding the 2859c4ae7908SLawrence Stewart * safety of accessing the per-cpu caches. With the zone lock 2860c4ae7908SLawrence Stewart * held, it is safe, but can potentially result in stale data. 2861c4ae7908SLawrence Stewart */ 2862c4ae7908SLawrence Stewart nitems += zone->uz_cpu[i].uc_allocs - 2863c4ae7908SLawrence Stewart zone->uz_cpu[i].uc_frees; 2864c4ae7908SLawrence Stewart } 2865c4ae7908SLawrence Stewart ZONE_UNLOCK(zone); 2866c4ae7908SLawrence Stewart 2867c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 2868c4ae7908SLawrence Stewart } 2869c4ae7908SLawrence Stewart 2870c4ae7908SLawrence Stewart /* See uma.h */ 2871736ee590SJeff Roberson void 2872099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 2873099a0e58SBosko Milekic { 2874e20a199fSJeff Roberson uma_keg_t keg; 2875e20a199fSJeff Roberson 2876099a0e58SBosko Milekic ZONE_LOCK(zone); 2877e20a199fSJeff Roberson keg = zone_first_keg(zone); 28780095a784SJeff Roberson KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type")); 2879e20a199fSJeff Roberson KASSERT(keg->uk_pages == 0, 2880099a0e58SBosko Milekic ("uma_zone_set_init on non-empty keg")); 2881e20a199fSJeff Roberson keg->uk_init = uminit; 2882099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2883099a0e58SBosko Milekic } 2884099a0e58SBosko Milekic 2885099a0e58SBosko Milekic /* See uma.h */ 2886099a0e58SBosko Milekic void 2887099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 2888099a0e58SBosko Milekic { 2889e20a199fSJeff Roberson uma_keg_t keg; 2890e20a199fSJeff Roberson 2891099a0e58SBosko Milekic ZONE_LOCK(zone); 2892e20a199fSJeff Roberson keg = zone_first_keg(zone); 28930095a784SJeff Roberson KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type")); 2894e20a199fSJeff Roberson KASSERT(keg->uk_pages == 0, 2895099a0e58SBosko Milekic ("uma_zone_set_fini on non-empty keg")); 2896e20a199fSJeff Roberson keg->uk_fini = fini; 2897099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2898099a0e58SBosko Milekic } 2899099a0e58SBosko Milekic 2900099a0e58SBosko Milekic /* See uma.h */ 2901099a0e58SBosko Milekic void 2902099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 2903099a0e58SBosko Milekic { 2904099a0e58SBosko Milekic ZONE_LOCK(zone); 2905e20a199fSJeff Roberson KASSERT(zone_first_keg(zone)->uk_pages == 0, 2906099a0e58SBosko Milekic ("uma_zone_set_zinit on non-empty keg")); 2907099a0e58SBosko Milekic zone->uz_init = zinit; 2908099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2909099a0e58SBosko Milekic } 2910099a0e58SBosko Milekic 2911099a0e58SBosko Milekic /* See uma.h */ 2912099a0e58SBosko Milekic void 2913099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 2914099a0e58SBosko Milekic { 2915099a0e58SBosko Milekic ZONE_LOCK(zone); 2916e20a199fSJeff Roberson KASSERT(zone_first_keg(zone)->uk_pages == 0, 2917099a0e58SBosko Milekic ("uma_zone_set_zfini on non-empty keg")); 2918099a0e58SBosko Milekic zone->uz_fini = zfini; 2919099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2920099a0e58SBosko Milekic } 2921099a0e58SBosko Milekic 2922099a0e58SBosko Milekic /* See uma.h */ 2923b23f72e9SBrian Feldman /* XXX uk_freef is not actually used with the zone locked */ 2924099a0e58SBosko Milekic void 29258355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 29268355f576SJeff Roberson { 29270095a784SJeff Roberson uma_keg_t keg; 2928e20a199fSJeff Roberson 29298355f576SJeff Roberson ZONE_LOCK(zone); 29300095a784SJeff Roberson keg = zone_first_keg(zone); 29310095a784SJeff Roberson KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type")); 29320095a784SJeff Roberson keg->uk_freef = freef; 29338355f576SJeff Roberson ZONE_UNLOCK(zone); 29348355f576SJeff Roberson } 29358355f576SJeff Roberson 29368355f576SJeff Roberson /* See uma.h */ 2937b23f72e9SBrian Feldman /* XXX uk_allocf is not actually used with the zone locked */ 29388355f576SJeff Roberson void 29398355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 29408355f576SJeff Roberson { 2941e20a199fSJeff Roberson uma_keg_t keg; 2942e20a199fSJeff Roberson 29438355f576SJeff Roberson ZONE_LOCK(zone); 2944e20a199fSJeff Roberson keg = zone_first_keg(zone); 2945e20a199fSJeff Roberson keg->uk_flags |= UMA_ZFLAG_PRIVALLOC; 2946e20a199fSJeff Roberson keg->uk_allocf = allocf; 29478355f576SJeff Roberson ZONE_UNLOCK(zone); 29488355f576SJeff Roberson } 29498355f576SJeff Roberson 29508355f576SJeff Roberson /* See uma.h */ 29518355f576SJeff Roberson int 2952a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 29538355f576SJeff Roberson { 2954099a0e58SBosko Milekic uma_keg_t keg; 29558355f576SJeff Roberson vm_offset_t kva; 2956099a0e58SBosko Milekic int pages; 29578355f576SJeff Roberson 2958e20a199fSJeff Roberson keg = zone_first_keg(zone); 29590095a784SJeff Roberson if (keg == NULL) 29600095a784SJeff Roberson return (0); 2961099a0e58SBosko Milekic pages = count / keg->uk_ipers; 29628355f576SJeff Roberson 2963099a0e58SBosko Milekic if (pages * keg->uk_ipers < count) 29648355f576SJeff Roberson pages++; 2965a553d4b8SJeff Roberson 2966a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 2967a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 2968a4915c21SAttilio Rao #else 2969a4915c21SAttilio Rao if (1) { 2970a4915c21SAttilio Rao #endif 29715285558aSAlan Cox kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE); 2972d1f42ac2SAlan Cox if (kva == 0) 29738355f576SJeff Roberson return (0); 2974a4915c21SAttilio Rao } else 2975a4915c21SAttilio Rao kva = 0; 2976a553d4b8SJeff Roberson ZONE_LOCK(zone); 2977099a0e58SBosko Milekic keg->uk_kva = kva; 2978a4915c21SAttilio Rao keg->uk_offset = 0; 2979099a0e58SBosko Milekic keg->uk_maxpages = pages; 2980a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 2981a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 2982a4915c21SAttilio Rao #else 2983a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 2984a4915c21SAttilio Rao #endif 2985099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC; 29868355f576SJeff Roberson ZONE_UNLOCK(zone); 29878355f576SJeff Roberson return (1); 29888355f576SJeff Roberson } 29898355f576SJeff Roberson 29908355f576SJeff Roberson /* See uma.h */ 29918355f576SJeff Roberson void 29928355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 29938355f576SJeff Roberson { 29948355f576SJeff Roberson int slabs; 29958355f576SJeff Roberson uma_slab_t slab; 2996099a0e58SBosko Milekic uma_keg_t keg; 29978355f576SJeff Roberson 2998e20a199fSJeff Roberson keg = zone_first_keg(zone); 29990095a784SJeff Roberson if (keg == NULL) 30000095a784SJeff Roberson return; 30018355f576SJeff Roberson ZONE_LOCK(zone); 3002099a0e58SBosko Milekic slabs = items / keg->uk_ipers; 3003099a0e58SBosko Milekic if (slabs * keg->uk_ipers < items) 30048355f576SJeff Roberson slabs++; 30058355f576SJeff Roberson while (slabs > 0) { 3006e20a199fSJeff Roberson slab = keg_alloc_slab(keg, zone, M_WAITOK); 3007e20a199fSJeff Roberson if (slab == NULL) 3008e20a199fSJeff Roberson break; 3009e20a199fSJeff Roberson MPASS(slab->us_keg == keg); 3010099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link); 30118355f576SJeff Roberson slabs--; 30128355f576SJeff Roberson } 30138355f576SJeff Roberson ZONE_UNLOCK(zone); 30148355f576SJeff Roberson } 30158355f576SJeff Roberson 30168355f576SJeff Roberson /* See uma.h */ 301785dcf349SGleb Smirnoff uint32_t * 3018099a0e58SBosko Milekic uma_find_refcnt(uma_zone_t zone, void *item) 3019099a0e58SBosko Milekic { 3020ab14a3f7SBrian Feldman uma_slabrefcnt_t slabref; 3021ef72505eSJeff Roberson uma_slab_t slab; 3022099a0e58SBosko Milekic uma_keg_t keg; 302385dcf349SGleb Smirnoff uint32_t *refcnt; 3024099a0e58SBosko Milekic int idx; 3025099a0e58SBosko Milekic 3026ef72505eSJeff Roberson slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK)); 3027ef72505eSJeff Roberson slabref = (uma_slabrefcnt_t)slab; 3028ef72505eSJeff Roberson keg = slab->us_keg; 3029ef72505eSJeff Roberson KASSERT(keg->uk_flags & UMA_ZONE_REFCNT, 3030099a0e58SBosko Milekic ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT")); 3031ef72505eSJeff Roberson idx = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize; 3032ef72505eSJeff Roberson refcnt = &slabref->us_refcnt[idx]; 3033099a0e58SBosko Milekic return refcnt; 3034099a0e58SBosko Milekic } 3035099a0e58SBosko Milekic 3036099a0e58SBosko Milekic /* See uma.h */ 30378355f576SJeff Roberson void 30388355f576SJeff Roberson uma_reclaim(void) 30398355f576SJeff Roberson { 30408355f576SJeff Roberson #ifdef UMA_DEBUG 30418355f576SJeff Roberson printf("UMA: vm asked us to release pages!\n"); 30428355f576SJeff Roberson #endif 304386bbae32SJeff Roberson bucket_enable(); 30448355f576SJeff Roberson zone_foreach(zone_drain); 30458355f576SJeff Roberson /* 30468355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 30478355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 30488355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 30498355f576SJeff Roberson */ 30509643769aSJeff Roberson zone_drain(slabzone); 3051099a0e58SBosko Milekic zone_drain(slabrefzone); 3052cae33c14SJeff Roberson bucket_zone_drain(); 30538355f576SJeff Roberson } 30548355f576SJeff Roberson 3055663b416fSJohn Baldwin /* See uma.h */ 3056663b416fSJohn Baldwin int 3057663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 3058663b416fSJohn Baldwin { 3059663b416fSJohn Baldwin int full; 3060663b416fSJohn Baldwin 3061663b416fSJohn Baldwin ZONE_LOCK(zone); 3062e20a199fSJeff Roberson full = (zone->uz_flags & UMA_ZFLAG_FULL); 3063663b416fSJohn Baldwin ZONE_UNLOCK(zone); 3064663b416fSJohn Baldwin return (full); 3065663b416fSJohn Baldwin } 3066663b416fSJohn Baldwin 30676c125b8dSMohan Srinivasan int 30686c125b8dSMohan Srinivasan uma_zone_exhausted_nolock(uma_zone_t zone) 30696c125b8dSMohan Srinivasan { 3070e20a199fSJeff Roberson return (zone->uz_flags & UMA_ZFLAG_FULL); 30716c125b8dSMohan Srinivasan } 30726c125b8dSMohan Srinivasan 30738355f576SJeff Roberson void * 30748355f576SJeff Roberson uma_large_malloc(int size, int wait) 30758355f576SJeff Roberson { 30768355f576SJeff Roberson void *mem; 30778355f576SJeff Roberson uma_slab_t slab; 307885dcf349SGleb Smirnoff uint8_t flags; 30798355f576SJeff Roberson 3080e20a199fSJeff Roberson slab = zone_alloc_item(slabzone, NULL, wait); 30818355f576SJeff Roberson if (slab == NULL) 30828355f576SJeff Roberson return (NULL); 30838355f576SJeff Roberson mem = page_alloc(NULL, size, &flags, wait); 30848355f576SJeff Roberson if (mem) { 308599571dc3SJeff Roberson vsetslab((vm_offset_t)mem, slab); 30868355f576SJeff Roberson slab->us_data = mem; 30878355f576SJeff Roberson slab->us_flags = flags | UMA_SLAB_MALLOC; 30888355f576SJeff Roberson slab->us_size = size; 30898355f576SJeff Roberson } else { 30900095a784SJeff Roberson zone_free_item(slabzone, slab, NULL, SKIP_NONE); 30918355f576SJeff Roberson } 30928355f576SJeff Roberson 30938355f576SJeff Roberson return (mem); 30948355f576SJeff Roberson } 30958355f576SJeff Roberson 30968355f576SJeff Roberson void 30978355f576SJeff Roberson uma_large_free(uma_slab_t slab) 30988355f576SJeff Roberson { 309999571dc3SJeff Roberson vsetobj((vm_offset_t)slab->us_data, kmem_object); 31008355f576SJeff Roberson page_free(slab->us_data, slab->us_size, slab->us_flags); 31010095a784SJeff Roberson zone_free_item(slabzone, slab, NULL, SKIP_NONE); 31028355f576SJeff Roberson } 31038355f576SJeff Roberson 31048355f576SJeff Roberson void 31058355f576SJeff Roberson uma_print_stats(void) 31068355f576SJeff Roberson { 31078355f576SJeff Roberson zone_foreach(uma_print_zone); 31088355f576SJeff Roberson } 31098355f576SJeff Roberson 3110504d5de3SJeff Roberson static void 3111504d5de3SJeff Roberson slab_print(uma_slab_t slab) 3112504d5de3SJeff Roberson { 3113ef72505eSJeff Roberson printf("slab: keg %p, data %p, freecount %d\n", 3114ef72505eSJeff Roberson slab->us_keg, slab->us_data, slab->us_freecount); 3115504d5de3SJeff Roberson } 3116504d5de3SJeff Roberson 3117504d5de3SJeff Roberson static void 3118504d5de3SJeff Roberson cache_print(uma_cache_t cache) 3119504d5de3SJeff Roberson { 3120504d5de3SJeff Roberson printf("alloc: %p(%d), free: %p(%d)\n", 3121504d5de3SJeff Roberson cache->uc_allocbucket, 3122504d5de3SJeff Roberson cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0, 3123504d5de3SJeff Roberson cache->uc_freebucket, 3124504d5de3SJeff Roberson cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0); 3125504d5de3SJeff Roberson } 3126504d5de3SJeff Roberson 3127e20a199fSJeff Roberson static void 3128e20a199fSJeff Roberson uma_print_keg(uma_keg_t keg) 31298355f576SJeff Roberson { 3130504d5de3SJeff Roberson uma_slab_t slab; 3131504d5de3SJeff Roberson 31320b80c1e4SEitan Adler printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d " 3133e20a199fSJeff Roberson "out %d free %d limit %d\n", 3134e20a199fSJeff Roberson keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags, 3135099a0e58SBosko Milekic keg->uk_ipers, keg->uk_ppera, 3136e20a199fSJeff Roberson (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free, 3137e20a199fSJeff Roberson (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers); 3138504d5de3SJeff Roberson printf("Part slabs:\n"); 3139099a0e58SBosko Milekic LIST_FOREACH(slab, &keg->uk_part_slab, us_link) 3140504d5de3SJeff Roberson slab_print(slab); 3141504d5de3SJeff Roberson printf("Free slabs:\n"); 3142099a0e58SBosko Milekic LIST_FOREACH(slab, &keg->uk_free_slab, us_link) 3143504d5de3SJeff Roberson slab_print(slab); 3144504d5de3SJeff Roberson printf("Full slabs:\n"); 3145099a0e58SBosko Milekic LIST_FOREACH(slab, &keg->uk_full_slab, us_link) 3146504d5de3SJeff Roberson slab_print(slab); 3147e20a199fSJeff Roberson } 3148e20a199fSJeff Roberson 3149e20a199fSJeff Roberson void 3150e20a199fSJeff Roberson uma_print_zone(uma_zone_t zone) 3151e20a199fSJeff Roberson { 3152e20a199fSJeff Roberson uma_cache_t cache; 3153e20a199fSJeff Roberson uma_klink_t kl; 3154e20a199fSJeff Roberson int i; 3155e20a199fSJeff Roberson 31560b80c1e4SEitan Adler printf("zone: %s(%p) size %d flags %#x\n", 3157e20a199fSJeff Roberson zone->uz_name, zone, zone->uz_size, zone->uz_flags); 3158e20a199fSJeff Roberson LIST_FOREACH(kl, &zone->uz_kegs, kl_link) 3159e20a199fSJeff Roberson uma_print_keg(kl->kl_keg); 31603aa6d94eSJohn Baldwin CPU_FOREACH(i) { 3161504d5de3SJeff Roberson cache = &zone->uz_cpu[i]; 3162504d5de3SJeff Roberson printf("CPU %d Cache:\n", i); 3163504d5de3SJeff Roberson cache_print(cache); 3164504d5de3SJeff Roberson } 31658355f576SJeff Roberson } 31668355f576SJeff Roberson 3167a0d4b0aeSRobert Watson #ifdef DDB 31688355f576SJeff Roberson /* 31697a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 31707a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 31717a52a97eSRobert Watson * 31727a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 31737a52a97eSRobert Watson * per-CPU cache statistic. 31747a52a97eSRobert Watson * 31757a52a97eSRobert Watson * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't 31767a52a97eSRobert Watson * safe from off-CPU; we should modify the caches to track this information 31777a52a97eSRobert Watson * directly so that we don't have to. 31787a52a97eSRobert Watson */ 31797a52a97eSRobert Watson static void 318085dcf349SGleb Smirnoff uma_zone_sumstat(uma_zone_t z, int *cachefreep, uint64_t *allocsp, 318185dcf349SGleb Smirnoff uint64_t *freesp, uint64_t *sleepsp) 31827a52a97eSRobert Watson { 31837a52a97eSRobert Watson uma_cache_t cache; 318485dcf349SGleb Smirnoff uint64_t allocs, frees, sleeps; 31857a52a97eSRobert Watson int cachefree, cpu; 31867a52a97eSRobert Watson 3187bf965959SSean Bruno allocs = frees = sleeps = 0; 31887a52a97eSRobert Watson cachefree = 0; 31893aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 31907a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 31917a52a97eSRobert Watson if (cache->uc_allocbucket != NULL) 31927a52a97eSRobert Watson cachefree += cache->uc_allocbucket->ub_cnt; 31937a52a97eSRobert Watson if (cache->uc_freebucket != NULL) 31947a52a97eSRobert Watson cachefree += cache->uc_freebucket->ub_cnt; 31957a52a97eSRobert Watson allocs += cache->uc_allocs; 31967a52a97eSRobert Watson frees += cache->uc_frees; 31977a52a97eSRobert Watson } 31987a52a97eSRobert Watson allocs += z->uz_allocs; 31997a52a97eSRobert Watson frees += z->uz_frees; 3200bf965959SSean Bruno sleeps += z->uz_sleeps; 32017a52a97eSRobert Watson if (cachefreep != NULL) 32027a52a97eSRobert Watson *cachefreep = cachefree; 32037a52a97eSRobert Watson if (allocsp != NULL) 32047a52a97eSRobert Watson *allocsp = allocs; 32057a52a97eSRobert Watson if (freesp != NULL) 32067a52a97eSRobert Watson *freesp = frees; 3207bf965959SSean Bruno if (sleepsp != NULL) 3208bf965959SSean Bruno *sleepsp = sleeps; 32097a52a97eSRobert Watson } 3210a0d4b0aeSRobert Watson #endif /* DDB */ 32117a52a97eSRobert Watson 32127a52a97eSRobert Watson static int 32137a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 32147a52a97eSRobert Watson { 32157a52a97eSRobert Watson uma_keg_t kz; 32167a52a97eSRobert Watson uma_zone_t z; 32177a52a97eSRobert Watson int count; 32187a52a97eSRobert Watson 32197a52a97eSRobert Watson count = 0; 32207a52a97eSRobert Watson mtx_lock(&uma_mtx); 32217a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 32227a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 32237a52a97eSRobert Watson count++; 32247a52a97eSRobert Watson } 32257a52a97eSRobert Watson mtx_unlock(&uma_mtx); 32267a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 32277a52a97eSRobert Watson } 32287a52a97eSRobert Watson 32297a52a97eSRobert Watson static int 32307a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 32317a52a97eSRobert Watson { 32327a52a97eSRobert Watson struct uma_stream_header ush; 32337a52a97eSRobert Watson struct uma_type_header uth; 32347a52a97eSRobert Watson struct uma_percpu_stat ups; 32357a52a97eSRobert Watson uma_bucket_t bucket; 32367a52a97eSRobert Watson struct sbuf sbuf; 32377a52a97eSRobert Watson uma_cache_t cache; 3238e20a199fSJeff Roberson uma_klink_t kl; 32397a52a97eSRobert Watson uma_keg_t kz; 32407a52a97eSRobert Watson uma_zone_t z; 3241e20a199fSJeff Roberson uma_keg_t k; 32424e657159SMatthew D Fleming int count, error, i; 32437a52a97eSRobert Watson 324400f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 324500f0e671SMatthew D Fleming if (error != 0) 324600f0e671SMatthew D Fleming return (error); 32474e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 32484e657159SMatthew D Fleming 3249404a593eSMatthew D Fleming count = 0; 32504e657159SMatthew D Fleming mtx_lock(&uma_mtx); 32517a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 32527a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 32537a52a97eSRobert Watson count++; 32547a52a97eSRobert Watson } 32557a52a97eSRobert Watson 32567a52a97eSRobert Watson /* 32577a52a97eSRobert Watson * Insert stream header. 32587a52a97eSRobert Watson */ 32597a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 32607a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 3261ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 32627a52a97eSRobert Watson ush.ush_count = count; 32634e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 32647a52a97eSRobert Watson 32657a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 32667a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 32677a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 32687a52a97eSRobert Watson ZONE_LOCK(z); 3269cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 32707a52a97eSRobert Watson uth.uth_align = kz->uk_align; 32717a52a97eSRobert Watson uth.uth_size = kz->uk_size; 32727a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 3273e20a199fSJeff Roberson LIST_FOREACH(kl, &z->uz_kegs, kl_link) { 3274e20a199fSJeff Roberson k = kl->kl_keg; 3275e20a199fSJeff Roberson uth.uth_maxpages += k->uk_maxpages; 3276e20a199fSJeff Roberson uth.uth_pages += k->uk_pages; 3277e20a199fSJeff Roberson uth.uth_keg_free += k->uk_free; 3278e20a199fSJeff Roberson uth.uth_limit = (k->uk_maxpages / k->uk_ppera) 3279e20a199fSJeff Roberson * k->uk_ipers; 3280e20a199fSJeff Roberson } 3281cbbb4a00SRobert Watson 3282cbbb4a00SRobert Watson /* 3283cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 3284cbbb4a00SRobert Watson * on the keg's zone list. 3285cbbb4a00SRobert Watson */ 3286e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 3287cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 3288cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 3289cbbb4a00SRobert Watson 3290*fc03d22bSJeff Roberson LIST_FOREACH(bucket, &z->uz_buckets, ub_link) 32917a52a97eSRobert Watson uth.uth_zone_free += bucket->ub_cnt; 32927a52a97eSRobert Watson uth.uth_allocs = z->uz_allocs; 32937a52a97eSRobert Watson uth.uth_frees = z->uz_frees; 32942019094aSRobert Watson uth.uth_fails = z->uz_fails; 3295bf965959SSean Bruno uth.uth_sleeps = z->uz_sleeps; 32964e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 32977a52a97eSRobert Watson /* 32982450bbb8SRobert Watson * While it is not normally safe to access the cache 32992450bbb8SRobert Watson * bucket pointers while not on the CPU that owns the 33002450bbb8SRobert Watson * cache, we only allow the pointers to be exchanged 33012450bbb8SRobert Watson * without the zone lock held, not invalidated, so 33022450bbb8SRobert Watson * accept the possible race associated with bucket 33032450bbb8SRobert Watson * exchange during monitoring. 33047a52a97eSRobert Watson */ 3305ab3a57c0SRobert Watson for (i = 0; i < (mp_maxid + 1); i++) { 33067a52a97eSRobert Watson bzero(&ups, sizeof(ups)); 33077a52a97eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) 33087a52a97eSRobert Watson goto skip; 3309082dc776SRobert Watson if (CPU_ABSENT(i)) 3310082dc776SRobert Watson goto skip; 33117a52a97eSRobert Watson cache = &z->uz_cpu[i]; 33127a52a97eSRobert Watson if (cache->uc_allocbucket != NULL) 33137a52a97eSRobert Watson ups.ups_cache_free += 33147a52a97eSRobert Watson cache->uc_allocbucket->ub_cnt; 33157a52a97eSRobert Watson if (cache->uc_freebucket != NULL) 33167a52a97eSRobert Watson ups.ups_cache_free += 33177a52a97eSRobert Watson cache->uc_freebucket->ub_cnt; 33187a52a97eSRobert Watson ups.ups_allocs = cache->uc_allocs; 33197a52a97eSRobert Watson ups.ups_frees = cache->uc_frees; 33207a52a97eSRobert Watson skip: 33214e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ups, sizeof(ups)); 33227a52a97eSRobert Watson } 33232450bbb8SRobert Watson ZONE_UNLOCK(z); 33247a52a97eSRobert Watson } 33257a52a97eSRobert Watson } 33267a52a97eSRobert Watson mtx_unlock(&uma_mtx); 33274e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 33284e657159SMatthew D Fleming sbuf_delete(&sbuf); 33297a52a97eSRobert Watson return (error); 33307a52a97eSRobert Watson } 333148c5777eSRobert Watson 333248c5777eSRobert Watson #ifdef DDB 333348c5777eSRobert Watson DB_SHOW_COMMAND(uma, db_show_uma) 333448c5777eSRobert Watson { 333585dcf349SGleb Smirnoff uint64_t allocs, frees, sleeps; 333648c5777eSRobert Watson uma_bucket_t bucket; 333748c5777eSRobert Watson uma_keg_t kz; 333848c5777eSRobert Watson uma_zone_t z; 333948c5777eSRobert Watson int cachefree; 334048c5777eSRobert Watson 3341bf965959SSean Bruno db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 3342bf965959SSean Bruno "Requests", "Sleeps"); 334348c5777eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 334448c5777eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 334548c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 334648c5777eSRobert Watson allocs = z->uz_allocs; 334748c5777eSRobert Watson frees = z->uz_frees; 3348bf965959SSean Bruno sleeps = z->uz_sleeps; 334948c5777eSRobert Watson cachefree = 0; 335048c5777eSRobert Watson } else 335148c5777eSRobert Watson uma_zone_sumstat(z, &cachefree, &allocs, 3352bf965959SSean Bruno &frees, &sleeps); 3353e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 335448c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 335548c5777eSRobert Watson cachefree += kz->uk_free; 3356*fc03d22bSJeff Roberson LIST_FOREACH(bucket, &z->uz_buckets, ub_link) 335748c5777eSRobert Watson cachefree += bucket->ub_cnt; 3358bf965959SSean Bruno db_printf("%18s %8ju %8jd %8d %12ju %8ju\n", z->uz_name, 3359ae4e9636SRobert Watson (uintmax_t)kz->uk_size, 3360ae4e9636SRobert Watson (intmax_t)(allocs - frees), cachefree, 3361bf965959SSean Bruno (uintmax_t)allocs, sleeps); 3362687c94aaSJohn Baldwin if (db_pager_quit) 3363687c94aaSJohn Baldwin return; 336448c5777eSRobert Watson } 336548c5777eSRobert Watson } 336648c5777eSRobert Watson } 336748c5777eSRobert Watson #endif 3368