160727d8bSWarner Losh /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 4584061b4SJeff Roberson * Copyright (c) 2002-2019 Jeffrey Roberson <jeff@FreeBSD.org> 508ecce74SRobert Watson * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org> 6ae4e9636SRobert Watson * Copyright (c) 2004-2006 Robert N. M. Watson 708ecce74SRobert Watson * All rights reserved. 88355f576SJeff Roberson * 98355f576SJeff Roberson * Redistribution and use in source and binary forms, with or without 108355f576SJeff Roberson * modification, are permitted provided that the following conditions 118355f576SJeff Roberson * are met: 128355f576SJeff Roberson * 1. Redistributions of source code must retain the above copyright 138355f576SJeff Roberson * notice unmodified, this list of conditions, and the following 148355f576SJeff Roberson * disclaimer. 158355f576SJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 168355f576SJeff Roberson * notice, this list of conditions and the following disclaimer in the 178355f576SJeff Roberson * documentation and/or other materials provided with the distribution. 188355f576SJeff Roberson * 198355f576SJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 208355f576SJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 218355f576SJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 228355f576SJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 238355f576SJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 248355f576SJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 258355f576SJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 268355f576SJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 278355f576SJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 288355f576SJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 298355f576SJeff Roberson */ 308355f576SJeff Roberson 318355f576SJeff Roberson /* 328355f576SJeff Roberson * uma_core.c Implementation of the Universal Memory allocator 338355f576SJeff Roberson * 348355f576SJeff Roberson * This allocator is intended to replace the multitude of similar object caches 358355f576SJeff Roberson * in the standard FreeBSD kernel. The intent is to be flexible as well as 36763df3ecSPedro F. Giffuni * efficient. A primary design goal is to return unused memory to the rest of 378355f576SJeff Roberson * the system. This will make the system as a whole more flexible due to the 388355f576SJeff Roberson * ability to move memory to subsystems which most need it instead of leaving 398355f576SJeff Roberson * pools of reserved memory unused. 408355f576SJeff Roberson * 418355f576SJeff Roberson * The basic ideas stem from similar slab/zone based allocators whose algorithms 428355f576SJeff Roberson * are well known. 438355f576SJeff Roberson * 448355f576SJeff Roberson */ 458355f576SJeff Roberson 468355f576SJeff Roberson /* 478355f576SJeff Roberson * TODO: 488355f576SJeff Roberson * - Improve memory usage for large allocations 498355f576SJeff Roberson * - Investigate cache size adjustments 508355f576SJeff Roberson */ 518355f576SJeff Roberson 52874651b1SDavid E. O'Brien #include <sys/cdefs.h> 53874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 54874651b1SDavid E. O'Brien 5548c5777eSRobert Watson #include "opt_ddb.h" 568355f576SJeff Roberson #include "opt_param.h" 578d689e04SGleb Smirnoff #include "opt_vm.h" 5848c5777eSRobert Watson 598355f576SJeff Roberson #include <sys/param.h> 608355f576SJeff Roberson #include <sys/systm.h> 61ef72505eSJeff Roberson #include <sys/bitset.h> 62194a979eSMark Johnston #include <sys/domainset.h> 639b43bc27SAndriy Gapon #include <sys/eventhandler.h> 648355f576SJeff Roberson #include <sys/kernel.h> 658355f576SJeff Roberson #include <sys/types.h> 66ad5b0f5bSJeff Roberson #include <sys/limits.h> 678355f576SJeff Roberson #include <sys/queue.h> 688355f576SJeff Roberson #include <sys/malloc.h> 693659f747SRobert Watson #include <sys/ktr.h> 708355f576SJeff Roberson #include <sys/lock.h> 718355f576SJeff Roberson #include <sys/sysctl.h> 728355f576SJeff Roberson #include <sys/mutex.h> 734c1cc01cSJohn Baldwin #include <sys/proc.h> 7410cb2424SMark Murray #include <sys/random.h> 7589f6b863SAttilio Rao #include <sys/rwlock.h> 767a52a97eSRobert Watson #include <sys/sbuf.h> 77a2de44abSAlexander Motin #include <sys/sched.h> 784bd61e19SJeff Roberson #include <sys/sleepqueue.h> 798355f576SJeff Roberson #include <sys/smp.h> 80e60b2fcbSGleb Smirnoff #include <sys/taskqueue.h> 8186bbae32SJeff Roberson #include <sys/vmmeter.h> 8286bbae32SJeff Roberson 838355f576SJeff Roberson #include <vm/vm.h> 84194a979eSMark Johnston #include <vm/vm_domainset.h> 858355f576SJeff Roberson #include <vm/vm_object.h> 868355f576SJeff Roberson #include <vm/vm_page.h> 87a4915c21SAttilio Rao #include <vm/vm_pageout.h> 888355f576SJeff Roberson #include <vm/vm_param.h> 89ab3185d1SJeff Roberson #include <vm/vm_phys.h> 9030c5525bSAndrew Gallatin #include <vm/vm_pagequeue.h> 918355f576SJeff Roberson #include <vm/vm_map.h> 928355f576SJeff Roberson #include <vm/vm_kern.h> 938355f576SJeff Roberson #include <vm/vm_extern.h> 948355f576SJeff Roberson #include <vm/uma.h> 958355f576SJeff Roberson #include <vm/uma_int.h> 96639c9550SJeff Roberson #include <vm/uma_dbg.h> 978355f576SJeff Roberson 9848c5777eSRobert Watson #include <ddb/ddb.h> 9948c5777eSRobert Watson 1008d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 1018d689e04SGleb Smirnoff #include <vm/memguard.h> 1028d689e04SGleb Smirnoff #endif 1038d689e04SGleb Smirnoff 1048355f576SJeff Roberson /* 105ab3185d1SJeff Roberson * This is the zone and keg from which all zones are spawned. 1068355f576SJeff Roberson */ 107ab3185d1SJeff Roberson static uma_zone_t kegs; 108ab3185d1SJeff Roberson static uma_zone_t zones; 1098355f576SJeff Roberson 110ab3185d1SJeff Roberson /* This is the zone from which all offpage uma_slab_ts are allocated. */ 1118355f576SJeff Roberson static uma_zone_t slabzone; 1128355f576SJeff Roberson 1138355f576SJeff Roberson /* 1148355f576SJeff Roberson * The initial hash tables come out of this zone so they can be allocated 1158355f576SJeff Roberson * prior to malloc coming up. 1168355f576SJeff Roberson */ 1178355f576SJeff Roberson static uma_zone_t hashzone; 1188355f576SJeff Roberson 1191e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */ 120e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1; 1211e319f6dSRobert Watson 122961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets"); 12320a4e154SJeff Roberson static MALLOC_DEFINE(M_UMA, "UMA", "UMA Misc"); 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 13303175483SAlexander Motin /* Linked list of all cache-only zones in the system */ 13403175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones = 13503175483SAlexander Motin LIST_HEAD_INITIALIZER(uma_cachezones); 13603175483SAlexander Motin 137111fbcd5SBryan Venteicher /* This RW lock protects the keg list */ 138fe933c1dSMateusz Guzik static struct rwlock_padalign __exclusive_cache_line uma_rwlock; 1398355f576SJeff Roberson 140ac0a6fd0SGleb Smirnoff /* 141ac0a6fd0SGleb Smirnoff * Pointer and counter to pool of pages, that is preallocated at 142f7d35785SGleb Smirnoff * startup to bootstrap UMA. 143ac0a6fd0SGleb Smirnoff */ 144ac0a6fd0SGleb Smirnoff static char *bootmem; 145ac0a6fd0SGleb Smirnoff static int boot_pages; 1468355f576SJeff Roberson 14708cfa56eSMark Johnston static struct sx uma_reclaim_lock; 14895c4bf75SKonstantin Belousov 149fbd95859SMark Johnston /* 150fbd95859SMark Johnston * kmem soft limit, initialized by uma_set_limit(). Ensure that early 151fbd95859SMark Johnston * allocations don't trigger a wakeup of the reclaim thread. 152fbd95859SMark Johnston */ 1536d6a03d7SJeff Roberson unsigned long uma_kmem_limit = LONG_MAX; 154fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_limit, CTLFLAG_RD, &uma_kmem_limit, 0, 155fbd95859SMark Johnston "UMA kernel memory soft limit"); 1566d6a03d7SJeff Roberson unsigned long uma_kmem_total; 157fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_total, CTLFLAG_RD, &uma_kmem_total, 0, 158fbd95859SMark Johnston "UMA kernel memory usage"); 1592e47807cSJeff Roberson 1608355f576SJeff Roberson /* Is the VM done starting up? */ 161f4bef67cSGleb Smirnoff static enum { BOOT_COLD = 0, BOOT_STRAPPED, BOOT_PAGEALLOC, BOOT_BUCKETS, 162f4bef67cSGleb Smirnoff BOOT_RUNNING } booted = BOOT_COLD; 1638355f576SJeff Roberson 164ef72505eSJeff Roberson /* 1659643769aSJeff Roberson * This is the handle used to schedule events that need to happen 1669643769aSJeff Roberson * outside of the allocation fast path. 1679643769aSJeff Roberson */ 1688355f576SJeff Roberson static struct callout uma_callout; 1699643769aSJeff Roberson #define UMA_TIMEOUT 20 /* Seconds for callout interval. */ 1708355f576SJeff Roberson 1718355f576SJeff Roberson /* 1728355f576SJeff Roberson * This structure is passed as the zone ctor arg so that I don't have to create 1738355f576SJeff Roberson * a special allocation function just for zones. 1748355f576SJeff Roberson */ 1758355f576SJeff Roberson struct uma_zctor_args { 176bb196eb4SMatthew D Fleming const char *name; 177c3bdc05fSAndrew R. Reiter size_t size; 1788355f576SJeff Roberson uma_ctor ctor; 1798355f576SJeff Roberson uma_dtor dtor; 1808355f576SJeff Roberson uma_init uminit; 1818355f576SJeff Roberson uma_fini fini; 1820095a784SJeff Roberson uma_import import; 1830095a784SJeff Roberson uma_release release; 1840095a784SJeff Roberson void *arg; 185099a0e58SBosko Milekic uma_keg_t keg; 186099a0e58SBosko Milekic int align; 18785dcf349SGleb Smirnoff uint32_t flags; 188099a0e58SBosko Milekic }; 189099a0e58SBosko Milekic 190099a0e58SBosko Milekic struct uma_kctor_args { 191099a0e58SBosko Milekic uma_zone_t zone; 192099a0e58SBosko Milekic size_t size; 193099a0e58SBosko Milekic uma_init uminit; 194099a0e58SBosko Milekic uma_fini fini; 1958355f576SJeff Roberson int align; 19685dcf349SGleb Smirnoff uint32_t flags; 1978355f576SJeff Roberson }; 1988355f576SJeff Roberson 199cae33c14SJeff Roberson struct uma_bucket_zone { 200cae33c14SJeff Roberson uma_zone_t ubz_zone; 201cae33c14SJeff Roberson char *ubz_name; 202fc03d22bSJeff Roberson int ubz_entries; /* Number of items it can hold. */ 203fc03d22bSJeff Roberson int ubz_maxsize; /* Maximum allocation size per-item. */ 204cae33c14SJeff Roberson }; 205cae33c14SJeff Roberson 206f9d27e75SRobert Watson /* 207fc03d22bSJeff Roberson * Compute the actual number of bucket entries to pack them in power 208fc03d22bSJeff Roberson * of two sizes for more efficient space utilization. 209f9d27e75SRobert Watson */ 210fc03d22bSJeff Roberson #define BUCKET_SIZE(n) \ 211fc03d22bSJeff Roberson (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *)) 212fc03d22bSJeff Roberson 2131aa6c758SAlexander Motin #define BUCKET_MAX BUCKET_SIZE(256) 214eda1b016SJeff Roberson #define BUCKET_MIN BUCKET_SIZE(4) 215fc03d22bSJeff Roberson 216fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = { 2176fd34d6fSJeff Roberson { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 }, 218f3932e90SAlexander Motin { NULL, "6 Bucket", BUCKET_SIZE(6), 3072 }, 2196fd34d6fSJeff Roberson { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 }, 220f3932e90SAlexander Motin { NULL, "12 Bucket", BUCKET_SIZE(12), 1536 }, 2216fd34d6fSJeff Roberson { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 }, 222fc03d22bSJeff Roberson { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 223fc03d22bSJeff Roberson { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 224fc03d22bSJeff Roberson { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 2251aa6c758SAlexander Motin { NULL, "256 Bucket", BUCKET_SIZE(256), 64 }, 226fc03d22bSJeff Roberson { NULL, NULL, 0} 227fc03d22bSJeff Roberson }; 228cae33c14SJeff Roberson 2292019094aSRobert Watson /* 2302019094aSRobert Watson * Flags and enumerations to be passed to internal functions. 2312019094aSRobert Watson */ 232bb15d1c7SGleb Smirnoff enum zfreeskip { 233bb15d1c7SGleb Smirnoff SKIP_NONE = 0, 234bb15d1c7SGleb Smirnoff SKIP_CNT = 0x00000001, 235bb15d1c7SGleb Smirnoff SKIP_DTOR = 0x00010000, 236bb15d1c7SGleb Smirnoff SKIP_FINI = 0x00020000, 237bb15d1c7SGleb Smirnoff }; 238b23f72e9SBrian Feldman 2398355f576SJeff Roberson /* Prototypes.. */ 2408355f576SJeff Roberson 241f4bef67cSGleb Smirnoff int uma_startup_count(int); 242f4bef67cSGleb Smirnoff void uma_startup(void *, int); 243f4bef67cSGleb Smirnoff void uma_startup1(void); 244f4bef67cSGleb Smirnoff void uma_startup2(void); 245f4bef67cSGleb Smirnoff 246ab3185d1SJeff Roberson static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 247ab3185d1SJeff Roberson static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 248ab3059a8SMatt Macy static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 249ab3185d1SJeff Roberson static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 250f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t); 251ab3059a8SMatt Macy static void pcpu_page_free(void *, vm_size_t, uint8_t); 25286220393SMark Johnston static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int); 2539643769aSJeff Roberson static void cache_drain(uma_zone_t); 2548355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t); 25508cfa56eSMark Johnston static void bucket_cache_reclaim(uma_zone_t zone, bool); 256b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int); 257099a0e58SBosko Milekic static void keg_dtor(void *, int, void *); 258b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int); 2599c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *); 260b23f72e9SBrian Feldman static int zero_init(void *, int, int); 261e20a199fSJeff Roberson static void keg_small_init(uma_keg_t keg); 262e20a199fSJeff Roberson static void keg_large_init(uma_keg_t keg); 26320a4e154SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t, void *), void *); 26420a4e154SJeff Roberson static void zone_timeout(uma_zone_t zone, void *); 2653b2f2cb8SAlexander Motin static int hash_alloc(struct uma_hash *, u_int); 2660aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *); 2670aef6126SJeff Roberson static void hash_free(struct uma_hash *hash); 2688355f576SJeff Roberson static void uma_timeout(void *); 2698355f576SJeff Roberson static void uma_startup3(void); 270ab3185d1SJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int, int); 2710095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 2724bd61e19SJeff Roberson static int zone_alloc_limit(uma_zone_t zone, int count, int flags); 2734bd61e19SJeff Roberson static void zone_free_limit(uma_zone_t zone, int count); 27486bbae32SJeff Roberson static void bucket_enable(void); 275cae33c14SJeff Roberson static void bucket_init(void); 2766fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int); 2776fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *); 278cae33c14SJeff Roberson static void bucket_zone_drain(void); 279beb8beefSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int); 2800095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 281bb15d1c7SGleb Smirnoff static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item); 282e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 28385dcf349SGleb Smirnoff uma_fini fini, int align, uint32_t flags); 284b75c4efcSAndrew Turner static int zone_import(void *, void **, int, int, int); 285b75c4efcSAndrew Turner static void zone_release(void *, void **, int); 286beb8beefSJeff Roberson static bool cache_alloc(uma_zone_t, uma_cache_t, void *, int); 2870a81b439SJeff Roberson static bool cache_free(uma_zone_t, uma_cache_t, void *, void *, int); 288bbee39c6SJeff Roberson 2897a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 2907a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 29120a4e154SJeff Roberson static int sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS); 29220a4e154SJeff Roberson static int sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS); 2936d204a6aSRyan Libby static int sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS); 294f7af5015SRyan Libby static int sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS); 2954bd61e19SJeff Roberson static int sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS); 2968355f576SJeff Roberson 2979542ea7bSGleb Smirnoff #ifdef INVARIANTS 298815db204SRyan Libby static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg); 299815db204SRyan Libby 300c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem); 301c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem); 3029542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item); 3039542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item); 304c5deaf04SGleb Smirnoff 305c5deaf04SGleb Smirnoff static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0, 306c5deaf04SGleb Smirnoff "Memory allocation debugging"); 307c5deaf04SGleb Smirnoff 308c5deaf04SGleb Smirnoff static u_int dbg_divisor = 1; 309c5deaf04SGleb Smirnoff SYSCTL_UINT(_vm_debug, OID_AUTO, divisor, 310c5deaf04SGleb Smirnoff CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0, 311c5deaf04SGleb Smirnoff "Debug & thrash every this item in memory allocator"); 312c5deaf04SGleb Smirnoff 313c5deaf04SGleb Smirnoff static counter_u64_t uma_dbg_cnt = EARLY_COUNTER; 314c5deaf04SGleb Smirnoff static counter_u64_t uma_skip_cnt = EARLY_COUNTER; 315c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD, 316c5deaf04SGleb Smirnoff &uma_dbg_cnt, "memory items debugged"); 317c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD, 318c5deaf04SGleb Smirnoff &uma_skip_cnt, "memory items skipped, not debugged"); 3199542ea7bSGleb Smirnoff #endif 3209542ea7bSGleb Smirnoff 3218355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 3228355f576SJeff Roberson 32335ec24f3SRyan Libby SYSCTL_NODE(_vm, OID_AUTO, uma, CTLFLAG_RW, 0, "Universal Memory Allocator"); 32435ec24f3SRyan Libby 3257a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT, 3267a52a97eSRobert Watson 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 3277a52a97eSRobert Watson 3287a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 3297a52a97eSRobert Watson 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 3307a52a97eSRobert Watson 3312f891cd5SPawel Jakub Dawidek static int zone_warnings = 1; 332af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0, 3332f891cd5SPawel Jakub Dawidek "Warn when UMA zones becomes full"); 3342f891cd5SPawel Jakub Dawidek 33586bbae32SJeff Roberson /* 33686bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 33786bbae32SJeff Roberson */ 33886bbae32SJeff Roberson static void 33986bbae32SJeff Roberson bucket_enable(void) 34086bbae32SJeff Roberson { 3413182660aSRyan Libby 3423182660aSRyan Libby KASSERT(booted >= BOOT_BUCKETS, ("Bucket enable before init")); 343251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 34486bbae32SJeff Roberson } 34586bbae32SJeff Roberson 346dc2c7965SRobert Watson /* 347dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 348dc2c7965SRobert Watson * 349dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 350fc03d22bSJeff Roberson * of the header and an array of pointers. 351dc2c7965SRobert Watson */ 352cae33c14SJeff Roberson static void 353cae33c14SJeff Roberson bucket_init(void) 354cae33c14SJeff Roberson { 355cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 356cae33c14SJeff Roberson int size; 357cae33c14SJeff Roberson 358d74e6a1dSAlan Cox for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 359cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 360cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 361cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 362e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 363ab3185d1SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | UMA_ZONE_NUMA); 364cae33c14SJeff Roberson } 365cae33c14SJeff Roberson } 366cae33c14SJeff Roberson 367dc2c7965SRobert Watson /* 368dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 369dc2c7965SRobert Watson * to allocate the bucket. 370dc2c7965SRobert Watson */ 371dc2c7965SRobert Watson static struct uma_bucket_zone * 372dc2c7965SRobert Watson bucket_zone_lookup(int entries) 373dc2c7965SRobert Watson { 374fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 375dc2c7965SRobert Watson 376fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 377fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 378fc03d22bSJeff Roberson return (ubz); 379fc03d22bSJeff Roberson ubz--; 380fc03d22bSJeff Roberson return (ubz); 381fc03d22bSJeff Roberson } 382fc03d22bSJeff Roberson 383003cf08bSMark Johnston static struct uma_bucket_zone * 384003cf08bSMark Johnston bucket_zone_max(uma_zone_t zone, int nitems) 385003cf08bSMark Johnston { 386003cf08bSMark Johnston struct uma_bucket_zone *ubz; 387003cf08bSMark Johnston int bpcpu; 388003cf08bSMark Johnston 389003cf08bSMark Johnston bpcpu = 2; 390003cf08bSMark Johnston #ifdef UMA_XDOMAIN 391003cf08bSMark Johnston if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) 392003cf08bSMark Johnston /* Count the cross-domain bucket. */ 393003cf08bSMark Johnston bpcpu++; 394003cf08bSMark Johnston #endif 395003cf08bSMark Johnston 396003cf08bSMark Johnston for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 397003cf08bSMark Johnston if (ubz->ubz_entries * bpcpu * mp_ncpus > nitems) 398003cf08bSMark Johnston break; 399003cf08bSMark Johnston if (ubz == &bucket_zones[0]) 400003cf08bSMark Johnston ubz = NULL; 401003cf08bSMark Johnston else 402003cf08bSMark Johnston ubz--; 403003cf08bSMark Johnston return (ubz); 404003cf08bSMark Johnston } 405003cf08bSMark Johnston 406fc03d22bSJeff Roberson static int 407fc03d22bSJeff Roberson bucket_select(int size) 408fc03d22bSJeff Roberson { 409fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 410fc03d22bSJeff Roberson 411fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 412fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 413fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 414fc03d22bSJeff Roberson 415fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 416fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 417fc03d22bSJeff Roberson break; 418fc03d22bSJeff Roberson ubz--; 419fc03d22bSJeff Roberson return (ubz->ubz_entries); 420dc2c7965SRobert Watson } 421dc2c7965SRobert Watson 422cae33c14SJeff Roberson static uma_bucket_t 4236fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags) 424cae33c14SJeff Roberson { 425cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 426cae33c14SJeff Roberson uma_bucket_t bucket; 427cae33c14SJeff Roberson 428cae33c14SJeff Roberson /* 429cae33c14SJeff Roberson * This is to stop us from allocating per cpu buckets while we're 4303803b26bSDag-Erling Smørgrav * running out of vm.boot_pages. Otherwise, we would exhaust the 431cae33c14SJeff Roberson * boot pages. This also prevents us from allocating buckets in 432cae33c14SJeff Roberson * low memory situations. 433cae33c14SJeff Roberson */ 434cae33c14SJeff Roberson if (bucketdisable) 435cae33c14SJeff Roberson return (NULL); 4366fd34d6fSJeff Roberson /* 4376fd34d6fSJeff Roberson * To limit bucket recursion we store the original zone flags 4386fd34d6fSJeff Roberson * in a cookie passed via zalloc_arg/zfree_arg. This allows the 4396fd34d6fSJeff Roberson * NOVM flag to persist even through deep recursions. We also 4406fd34d6fSJeff Roberson * store ZFLAG_BUCKET once we have recursed attempting to allocate 4416fd34d6fSJeff Roberson * a bucket for a bucket zone so we do not allow infinite bucket 4426fd34d6fSJeff Roberson * recursion. This cookie will even persist to frees of unused 4436fd34d6fSJeff Roberson * buckets via the allocation path or bucket allocations in the 4446fd34d6fSJeff Roberson * free path. 4456fd34d6fSJeff Roberson */ 4466fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 4476fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 448e8a720feSAlexander Motin else { 449e8a720feSAlexander Motin if ((uintptr_t)udata & UMA_ZFLAG_BUCKET) 450e8a720feSAlexander Motin return (NULL); 4516fd34d6fSJeff Roberson udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET); 452e8a720feSAlexander Motin } 4536fd34d6fSJeff Roberson if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY) 454af526374SJeff Roberson flags |= M_NOVM; 45520a4e154SJeff Roberson ubz = bucket_zone_lookup(zone->uz_bucket_size); 45620d3ab87SAlexander Motin if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0) 45720d3ab87SAlexander Motin ubz++; 4586fd34d6fSJeff Roberson bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags); 459cae33c14SJeff Roberson if (bucket) { 460cae33c14SJeff Roberson #ifdef INVARIANTS 461cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 462cae33c14SJeff Roberson #endif 463cae33c14SJeff Roberson bucket->ub_cnt = 0; 464cae33c14SJeff Roberson bucket->ub_entries = ubz->ubz_entries; 465cae33c14SJeff Roberson } 466cae33c14SJeff Roberson 467cae33c14SJeff Roberson return (bucket); 468cae33c14SJeff Roberson } 469cae33c14SJeff Roberson 470cae33c14SJeff Roberson static void 4716fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata) 472cae33c14SJeff Roberson { 473cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 474cae33c14SJeff Roberson 475fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 476fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 4776fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 4786fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 479dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 4806fd34d6fSJeff Roberson uma_zfree_arg(ubz->ubz_zone, bucket, udata); 481cae33c14SJeff Roberson } 482cae33c14SJeff Roberson 483cae33c14SJeff Roberson static void 484cae33c14SJeff Roberson bucket_zone_drain(void) 485cae33c14SJeff Roberson { 486cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 487cae33c14SJeff Roberson 488cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 48908cfa56eSMark Johnston uma_zone_reclaim(ubz->ubz_zone, UMA_RECLAIM_DRAIN); 490cae33c14SJeff Roberson } 491cae33c14SJeff Roberson 49208cfa56eSMark Johnston /* 49308cfa56eSMark Johnston * Attempt to satisfy an allocation by retrieving a full bucket from one of the 49408cfa56eSMark Johnston * zone's caches. 49508cfa56eSMark Johnston */ 4960f9b7bf3SMark Johnston static uma_bucket_t 49708cfa56eSMark Johnston zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom) 4980f9b7bf3SMark Johnston { 4990f9b7bf3SMark Johnston uma_bucket_t bucket; 5000f9b7bf3SMark Johnston 5010f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 5020f9b7bf3SMark Johnston 50308cfa56eSMark Johnston if ((bucket = TAILQ_FIRST(&zdom->uzd_buckets)) != NULL) { 5040f9b7bf3SMark Johnston MPASS(zdom->uzd_nitems >= bucket->ub_cnt); 50508cfa56eSMark Johnston TAILQ_REMOVE(&zdom->uzd_buckets, bucket, ub_link); 5060f9b7bf3SMark Johnston zdom->uzd_nitems -= bucket->ub_cnt; 50708cfa56eSMark Johnston if (zdom->uzd_imin > zdom->uzd_nitems) 5080f9b7bf3SMark Johnston zdom->uzd_imin = zdom->uzd_nitems; 509bb15d1c7SGleb Smirnoff zone->uz_bkt_count -= bucket->ub_cnt; 5100f9b7bf3SMark Johnston } 5110f9b7bf3SMark Johnston return (bucket); 5120f9b7bf3SMark Johnston } 5130f9b7bf3SMark Johnston 51408cfa56eSMark Johnston /* 51508cfa56eSMark Johnston * Insert a full bucket into the specified cache. The "ws" parameter indicates 51608cfa56eSMark Johnston * whether the bucket's contents should be counted as part of the zone's working 51708cfa56eSMark Johnston * set. 51808cfa56eSMark Johnston */ 5190f9b7bf3SMark Johnston static void 5200f9b7bf3SMark Johnston zone_put_bucket(uma_zone_t zone, uma_zone_domain_t zdom, uma_bucket_t bucket, 5210f9b7bf3SMark Johnston const bool ws) 5220f9b7bf3SMark Johnston { 5230f9b7bf3SMark Johnston 5240f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 52508034d10SKonstantin Belousov KASSERT(!ws || zone->uz_bkt_count < zone->uz_bkt_max, 52608034d10SKonstantin Belousov ("%s: zone %p overflow", __func__, zone)); 5270f9b7bf3SMark Johnston 52808cfa56eSMark Johnston if (ws) 52908cfa56eSMark Johnston TAILQ_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link); 53008cfa56eSMark Johnston else 53108cfa56eSMark Johnston TAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link); 5320f9b7bf3SMark Johnston zdom->uzd_nitems += bucket->ub_cnt; 5330f9b7bf3SMark Johnston if (ws && zdom->uzd_imax < zdom->uzd_nitems) 5340f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_nitems; 535bb15d1c7SGleb Smirnoff zone->uz_bkt_count += bucket->ub_cnt; 5360f9b7bf3SMark Johnston } 5370f9b7bf3SMark Johnston 538376b1ba3SJeff Roberson /* Pops an item out of a per-cpu cache bucket. */ 539376b1ba3SJeff Roberson static inline void * 540376b1ba3SJeff Roberson cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket) 541376b1ba3SJeff Roberson { 542376b1ba3SJeff Roberson void *item; 543376b1ba3SJeff Roberson 544376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 545376b1ba3SJeff Roberson 546376b1ba3SJeff Roberson bucket->ucb_cnt--; 547376b1ba3SJeff Roberson item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt]; 548376b1ba3SJeff Roberson #ifdef INVARIANTS 549376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL; 550376b1ba3SJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 551376b1ba3SJeff Roberson #endif 552376b1ba3SJeff Roberson cache->uc_allocs++; 553376b1ba3SJeff Roberson 554376b1ba3SJeff Roberson return (item); 555376b1ba3SJeff Roberson } 556376b1ba3SJeff Roberson 557376b1ba3SJeff Roberson /* Pushes an item into a per-cpu cache bucket. */ 558376b1ba3SJeff Roberson static inline void 559376b1ba3SJeff Roberson cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item) 560376b1ba3SJeff Roberson { 561376b1ba3SJeff Roberson 562376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 563376b1ba3SJeff Roberson KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL, 564376b1ba3SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 565376b1ba3SJeff Roberson 566376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item; 567376b1ba3SJeff Roberson bucket->ucb_cnt++; 568376b1ba3SJeff Roberson cache->uc_frees++; 569376b1ba3SJeff Roberson } 570376b1ba3SJeff Roberson 571376b1ba3SJeff Roberson /* 572376b1ba3SJeff Roberson * Unload a UMA bucket from a per-cpu cache. 573376b1ba3SJeff Roberson */ 574376b1ba3SJeff Roberson static inline uma_bucket_t 575376b1ba3SJeff Roberson cache_bucket_unload(uma_cache_bucket_t bucket) 576376b1ba3SJeff Roberson { 577376b1ba3SJeff Roberson uma_bucket_t b; 578376b1ba3SJeff Roberson 579376b1ba3SJeff Roberson b = bucket->ucb_bucket; 580376b1ba3SJeff Roberson if (b != NULL) { 581376b1ba3SJeff Roberson MPASS(b->ub_entries == bucket->ucb_entries); 582376b1ba3SJeff Roberson b->ub_cnt = bucket->ucb_cnt; 583376b1ba3SJeff Roberson bucket->ucb_bucket = NULL; 584376b1ba3SJeff Roberson bucket->ucb_entries = bucket->ucb_cnt = 0; 585376b1ba3SJeff Roberson } 586376b1ba3SJeff Roberson 587376b1ba3SJeff Roberson return (b); 588376b1ba3SJeff Roberson } 589376b1ba3SJeff Roberson 590376b1ba3SJeff Roberson static inline uma_bucket_t 591376b1ba3SJeff Roberson cache_bucket_unload_alloc(uma_cache_t cache) 592376b1ba3SJeff Roberson { 593376b1ba3SJeff Roberson 594376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_allocbucket)); 595376b1ba3SJeff Roberson } 596376b1ba3SJeff Roberson 597376b1ba3SJeff Roberson static inline uma_bucket_t 598376b1ba3SJeff Roberson cache_bucket_unload_free(uma_cache_t cache) 599376b1ba3SJeff Roberson { 600376b1ba3SJeff Roberson 601376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_freebucket)); 602376b1ba3SJeff Roberson } 603376b1ba3SJeff Roberson 604376b1ba3SJeff Roberson static inline uma_bucket_t 605376b1ba3SJeff Roberson cache_bucket_unload_cross(uma_cache_t cache) 606376b1ba3SJeff Roberson { 607376b1ba3SJeff Roberson 608376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_crossbucket)); 609376b1ba3SJeff Roberson } 610376b1ba3SJeff Roberson 611376b1ba3SJeff Roberson /* 612376b1ba3SJeff Roberson * Load a bucket into a per-cpu cache bucket. 613376b1ba3SJeff Roberson */ 614376b1ba3SJeff Roberson static inline void 615376b1ba3SJeff Roberson cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b) 616376b1ba3SJeff Roberson { 617376b1ba3SJeff Roberson 618376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 619376b1ba3SJeff Roberson MPASS(bucket->ucb_bucket == NULL); 620376b1ba3SJeff Roberson 621376b1ba3SJeff Roberson bucket->ucb_bucket = b; 622376b1ba3SJeff Roberson bucket->ucb_cnt = b->ub_cnt; 623376b1ba3SJeff Roberson bucket->ucb_entries = b->ub_entries; 624376b1ba3SJeff Roberson } 625376b1ba3SJeff Roberson 626376b1ba3SJeff Roberson static inline void 627376b1ba3SJeff Roberson cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b) 628376b1ba3SJeff Roberson { 629376b1ba3SJeff Roberson 630376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_allocbucket, b); 631376b1ba3SJeff Roberson } 632376b1ba3SJeff Roberson 633376b1ba3SJeff Roberson static inline void 634376b1ba3SJeff Roberson cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b) 635376b1ba3SJeff Roberson { 636376b1ba3SJeff Roberson 637376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_freebucket, b); 638376b1ba3SJeff Roberson } 639376b1ba3SJeff Roberson 640376b1ba3SJeff Roberson #ifdef UMA_XDOMAIN 641376b1ba3SJeff Roberson static inline void 642376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b) 643376b1ba3SJeff Roberson { 644376b1ba3SJeff Roberson 645376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_crossbucket, b); 646376b1ba3SJeff Roberson } 647376b1ba3SJeff Roberson #endif 648376b1ba3SJeff Roberson 649376b1ba3SJeff Roberson /* 650376b1ba3SJeff Roberson * Copy and preserve ucb_spare. 651376b1ba3SJeff Roberson */ 652376b1ba3SJeff Roberson static inline void 653376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 654376b1ba3SJeff Roberson { 655376b1ba3SJeff Roberson 656376b1ba3SJeff Roberson b1->ucb_bucket = b2->ucb_bucket; 657376b1ba3SJeff Roberson b1->ucb_entries = b2->ucb_entries; 658376b1ba3SJeff Roberson b1->ucb_cnt = b2->ucb_cnt; 659376b1ba3SJeff Roberson } 660376b1ba3SJeff Roberson 661376b1ba3SJeff Roberson /* 662376b1ba3SJeff Roberson * Swap two cache buckets. 663376b1ba3SJeff Roberson */ 664376b1ba3SJeff Roberson static inline void 665376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 666376b1ba3SJeff Roberson { 667376b1ba3SJeff Roberson struct uma_cache_bucket b3; 668376b1ba3SJeff Roberson 669376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 670376b1ba3SJeff Roberson 671376b1ba3SJeff Roberson cache_bucket_copy(&b3, b1); 672376b1ba3SJeff Roberson cache_bucket_copy(b1, b2); 673376b1ba3SJeff Roberson cache_bucket_copy(b2, &b3); 674376b1ba3SJeff Roberson } 675376b1ba3SJeff Roberson 6762f891cd5SPawel Jakub Dawidek static void 6772f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 6782f891cd5SPawel Jakub Dawidek { 6792f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 6802f891cd5SPawel Jakub Dawidek 6812f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 6822f891cd5SPawel Jakub Dawidek return; 6832f891cd5SPawel Jakub Dawidek 6842f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 6852f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 6862f891cd5SPawel Jakub Dawidek } 6872f891cd5SPawel Jakub Dawidek 68854503a13SJonathan T. Looney static inline void 68954503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone) 69054503a13SJonathan T. Looney { 691e60b2fcbSGleb Smirnoff 692e60b2fcbSGleb Smirnoff if (zone->uz_maxaction.ta_func != NULL) 693e60b2fcbSGleb Smirnoff taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction); 69454503a13SJonathan T. Looney } 69554503a13SJonathan T. Looney 6968355f576SJeff Roberson /* 6978355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 6989643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 6998355f576SJeff Roberson * 7008355f576SJeff Roberson * Arguments: 7018355f576SJeff Roberson * arg Unused 7028355f576SJeff Roberson * 7038355f576SJeff Roberson * Returns: 7048355f576SJeff Roberson * Nothing 7058355f576SJeff Roberson */ 7068355f576SJeff Roberson static void 7078355f576SJeff Roberson uma_timeout(void *unused) 7088355f576SJeff Roberson { 70986bbae32SJeff Roberson bucket_enable(); 71020a4e154SJeff Roberson zone_foreach(zone_timeout, NULL); 7118355f576SJeff Roberson 7128355f576SJeff Roberson /* Reschedule this event */ 7139643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 7148355f576SJeff Roberson } 7158355f576SJeff Roberson 7168355f576SJeff Roberson /* 7170f9b7bf3SMark Johnston * Update the working set size estimate for the zone's bucket cache. 7180f9b7bf3SMark Johnston * The constants chosen here are somewhat arbitrary. With an update period of 7190f9b7bf3SMark Johnston * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the 7200f9b7bf3SMark Johnston * last 100s. 7210f9b7bf3SMark Johnston */ 7220f9b7bf3SMark Johnston static void 7230f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom) 7240f9b7bf3SMark Johnston { 7250f9b7bf3SMark Johnston long wss; 7260f9b7bf3SMark Johnston 7270f9b7bf3SMark Johnston MPASS(zdom->uzd_imax >= zdom->uzd_imin); 7280f9b7bf3SMark Johnston wss = zdom->uzd_imax - zdom->uzd_imin; 7290f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems; 73008cfa56eSMark Johnston zdom->uzd_wss = (4 * wss + zdom->uzd_wss) / 5; 7310f9b7bf3SMark Johnston } 7320f9b7bf3SMark Johnston 7330f9b7bf3SMark Johnston /* 7349643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 7359643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 7368355f576SJeff Roberson * 737e20a199fSJeff Roberson * Returns nothing. 7388355f576SJeff Roberson */ 7398355f576SJeff Roberson static void 74020a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused) 7418355f576SJeff Roberson { 74208034d10SKonstantin Belousov uma_keg_t keg; 7438b987a77SJeff Roberson u_int slabs, pages; 7448355f576SJeff Roberson 74571353f7aSJeff Roberson if ((zone->uz_flags & UMA_ZONE_HASH) == 0) 74608034d10SKonstantin Belousov goto update_wss; 74708034d10SKonstantin Belousov 74808034d10SKonstantin Belousov keg = zone->uz_keg; 7498b987a77SJeff Roberson 7508b987a77SJeff Roberson /* 7518b987a77SJeff Roberson * Hash zones are non-numa by definition so the first domain 7528b987a77SJeff Roberson * is the only one present. 7538b987a77SJeff Roberson */ 7548b987a77SJeff Roberson KEG_LOCK(keg, 0); 7558b987a77SJeff Roberson pages = keg->uk_domain[0].ud_pages; 7568b987a77SJeff Roberson 7578355f576SJeff Roberson /* 758e20a199fSJeff Roberson * Expand the keg hash table. 7598355f576SJeff Roberson * 7608355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 7618355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 7628355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 7638355f576SJeff Roberson */ 7648b987a77SJeff Roberson if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) { 7650aef6126SJeff Roberson struct uma_hash newhash; 7660aef6126SJeff Roberson struct uma_hash oldhash; 7670aef6126SJeff Roberson int ret; 7685300d9ddSJeff Roberson 7690aef6126SJeff Roberson /* 7700aef6126SJeff Roberson * This is so involved because allocating and freeing 771e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 7720aef6126SJeff Roberson * I have to do everything in stages and check for 7730aef6126SJeff Roberson * races. 7740aef6126SJeff Roberson */ 7758b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 7763b2f2cb8SAlexander Motin ret = hash_alloc(&newhash, 1 << fls(slabs)); 7778b987a77SJeff Roberson KEG_LOCK(keg, 0); 7780aef6126SJeff Roberson if (ret) { 779099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 780099a0e58SBosko Milekic oldhash = keg->uk_hash; 781099a0e58SBosko Milekic keg->uk_hash = newhash; 7820aef6126SJeff Roberson } else 7830aef6126SJeff Roberson oldhash = newhash; 7840aef6126SJeff Roberson 7858b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 7860aef6126SJeff Roberson hash_free(&oldhash); 7878b987a77SJeff Roberson goto update_wss; 7880aef6126SJeff Roberson } 7895300d9ddSJeff Roberson } 7908b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 791e20a199fSJeff Roberson 79208034d10SKonstantin Belousov update_wss: 79308cfa56eSMark Johnston ZONE_LOCK(zone); 794bb15d1c7SGleb Smirnoff for (int i = 0; i < vm_ndomains; i++) 7950f9b7bf3SMark Johnston zone_domain_update_wss(&zone->uz_domain[i]); 79608cfa56eSMark Johnston ZONE_UNLOCK(zone); 7978355f576SJeff Roberson } 7988355f576SJeff Roberson 7998355f576SJeff Roberson /* 8005300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 8015300d9ddSJeff Roberson * backing store. 8025300d9ddSJeff Roberson * 8035300d9ddSJeff Roberson * Arguments: 8040aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 8055300d9ddSJeff Roberson * 8065300d9ddSJeff Roberson * Returns: 807763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 8085300d9ddSJeff Roberson */ 80937c84183SPoul-Henning Kamp static int 8103b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 8115300d9ddSJeff Roberson { 81259568a0eSAlexander Motin size_t alloc; 8135300d9ddSJeff Roberson 8143b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 8153b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 8163b2f2cb8SAlexander Motin hash->uh_hashsize = size; 8170aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 8181e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 8195300d9ddSJeff Roberson } else { 8200aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 821e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 822ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 8230aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 8245300d9ddSJeff Roberson } 8250aef6126SJeff Roberson if (hash->uh_slab_hash) { 8260aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 8270aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 8280aef6126SJeff Roberson return (1); 8290aef6126SJeff Roberson } 8305300d9ddSJeff Roberson 8310aef6126SJeff Roberson return (0); 8325300d9ddSJeff Roberson } 8335300d9ddSJeff Roberson 8345300d9ddSJeff Roberson /* 83564f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 83664f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 83764f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 8388355f576SJeff Roberson * 8398355f576SJeff Roberson * Arguments: 8400aef6126SJeff Roberson * oldhash The hash you want to expand 8410aef6126SJeff Roberson * newhash The hash structure for the new table 8428355f576SJeff Roberson * 8438355f576SJeff Roberson * Returns: 8448355f576SJeff Roberson * Nothing 8458355f576SJeff Roberson * 8468355f576SJeff Roberson * Discussion: 8478355f576SJeff Roberson */ 8480aef6126SJeff Roberson static int 8490aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 8508355f576SJeff Roberson { 8511e0701e1SJeff Roberson uma_hash_slab_t slab; 8526929b7d1SPedro F. Giffuni u_int hval; 8536929b7d1SPedro F. Giffuni u_int idx; 8548355f576SJeff Roberson 8550aef6126SJeff Roberson if (!newhash->uh_slab_hash) 8560aef6126SJeff Roberson return (0); 8578355f576SJeff Roberson 8580aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 8590aef6126SJeff Roberson return (0); 8608355f576SJeff Roberson 8618355f576SJeff Roberson /* 8628355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 8638355f576SJeff Roberson * full rehash. 8648355f576SJeff Roberson */ 8658355f576SJeff Roberson 8666929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 8671e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 8681e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 8691e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 8701e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 8711e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 8721e0701e1SJeff Roberson slab, uhs_hlink); 8738355f576SJeff Roberson } 8748355f576SJeff Roberson 8750aef6126SJeff Roberson return (1); 8769c2cd7e5SJeff Roberson } 8779c2cd7e5SJeff Roberson 8785300d9ddSJeff Roberson /* 8795300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 8805300d9ddSJeff Roberson * 8815300d9ddSJeff Roberson * Arguments: 8825300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 8835300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 8845300d9ddSJeff Roberson * 8855300d9ddSJeff Roberson * Returns: 8865300d9ddSJeff Roberson * Nothing 8875300d9ddSJeff Roberson */ 8889c2cd7e5SJeff Roberson static void 8890aef6126SJeff Roberson hash_free(struct uma_hash *hash) 8909c2cd7e5SJeff Roberson { 8910aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 8920aef6126SJeff Roberson return; 8930aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 8940095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 8958355f576SJeff Roberson else 896961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 8978355f576SJeff Roberson } 8988355f576SJeff Roberson 8998355f576SJeff Roberson /* 9008355f576SJeff Roberson * Frees all outstanding items in a bucket 9018355f576SJeff Roberson * 9028355f576SJeff Roberson * Arguments: 9038355f576SJeff Roberson * zone The zone to free to, must be unlocked. 9044bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 9058355f576SJeff Roberson * 9068355f576SJeff Roberson * Returns: 9078355f576SJeff Roberson * Nothing 9088355f576SJeff Roberson */ 9098355f576SJeff Roberson 9108355f576SJeff Roberson static void 9118355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 9128355f576SJeff Roberson { 9130095a784SJeff Roberson int i; 9148355f576SJeff Roberson 9154bd61e19SJeff Roberson if (bucket == NULL || bucket->ub_cnt == 0) 9168355f576SJeff Roberson return; 9178355f576SJeff Roberson 9180095a784SJeff Roberson if (zone->uz_fini) 9190095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 9200095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 9210095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 9224bd61e19SJeff Roberson if (zone->uz_max_items > 0) 9234bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 9240095a784SJeff Roberson bucket->ub_cnt = 0; 9258355f576SJeff Roberson } 9268355f576SJeff Roberson 9278355f576SJeff Roberson /* 9288355f576SJeff Roberson * Drains the per cpu caches for a zone. 9298355f576SJeff Roberson * 930727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 9315d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 9325d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 9335d1ae027SRobert Watson * 9348355f576SJeff Roberson * Arguments: 9358355f576SJeff Roberson * zone The zone to drain, must be unlocked. 9368355f576SJeff Roberson * 9378355f576SJeff Roberson * Returns: 9388355f576SJeff Roberson * Nothing 9398355f576SJeff Roberson */ 9408355f576SJeff Roberson static void 9419643769aSJeff Roberson cache_drain(uma_zone_t zone) 9428355f576SJeff Roberson { 9438355f576SJeff Roberson uma_cache_t cache; 944376b1ba3SJeff Roberson uma_bucket_t bucket; 9458355f576SJeff Roberson int cpu; 9468355f576SJeff Roberson 9478355f576SJeff Roberson /* 9485d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 9495d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 9505d1ae027SRobert Watson * of the caches at this point. 9515d1ae027SRobert Watson * 9525d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 9535d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 9548355f576SJeff Roberson */ 9553aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 9568355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 957376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 958376b1ba3SJeff Roberson if (bucket != NULL) { 959376b1ba3SJeff Roberson bucket_drain(zone, bucket); 960376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 961376b1ba3SJeff Roberson } 962376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 963376b1ba3SJeff Roberson if (bucket != NULL) { 964376b1ba3SJeff Roberson bucket_drain(zone, bucket); 965376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 966376b1ba3SJeff Roberson } 967376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 968376b1ba3SJeff Roberson if (bucket != NULL) { 969376b1ba3SJeff Roberson bucket_drain(zone, bucket); 970376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 971376b1ba3SJeff Roberson } 972d56368d7SBosko Milekic } 97308cfa56eSMark Johnston bucket_cache_reclaim(zone, true); 974aaa8bb16SJeff Roberson } 975aaa8bb16SJeff Roberson 976a2de44abSAlexander Motin static void 97720a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 978a2de44abSAlexander Motin { 979a2de44abSAlexander Motin 980a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 981a2de44abSAlexander Motin return; 982a2de44abSAlexander Motin 983a2de44abSAlexander Motin ZONE_LOCK(zone); 98420a4e154SJeff Roberson zone->uz_bucket_size = 98520a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 986a2de44abSAlexander Motin ZONE_UNLOCK(zone); 987a2de44abSAlexander Motin } 988a2de44abSAlexander Motin 989a2de44abSAlexander Motin static void 99020a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 991a2de44abSAlexander Motin { 992a2de44abSAlexander Motin uma_cache_t cache; 993c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 994ab3185d1SJeff Roberson int domain; 995a2de44abSAlexander Motin 996a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 997a2de44abSAlexander Motin return; 998a2de44abSAlexander Motin 999c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1000a2de44abSAlexander Motin ZONE_LOCK(zone); 1001a2de44abSAlexander Motin critical_enter(); 1002ab3185d1SJeff Roberson if (zone->uz_flags & UMA_ZONE_NUMA) 1003ab3185d1SJeff Roberson domain = PCPU_GET(domain); 1004ab3185d1SJeff Roberson else 1005ab3185d1SJeff Roberson domain = 0; 1006a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1007376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1008376b1ba3SJeff Roberson if (b1 != NULL && b1->ub_cnt != 0) { 1009376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b1, false); 1010376b1ba3SJeff Roberson b1 = NULL; 1011a2de44abSAlexander Motin } 1012376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1013376b1ba3SJeff Roberson if (b2 != NULL && b2->ub_cnt != 0) { 1014376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b2, false); 1015376b1ba3SJeff Roberson b2 = NULL; 1016a2de44abSAlexander Motin } 1017376b1ba3SJeff Roberson b3 = cache_bucket_unload_cross(cache); 1018a2de44abSAlexander Motin critical_exit(); 1019a2de44abSAlexander Motin ZONE_UNLOCK(zone); 10208a8d9d14SAlexander Motin if (b1) 10218a8d9d14SAlexander Motin bucket_free(zone, b1, NULL); 10228a8d9d14SAlexander Motin if (b2) 10238a8d9d14SAlexander Motin bucket_free(zone, b2, NULL); 1024c1685086SJeff Roberson if (b3) { 1025c1685086SJeff Roberson bucket_drain(zone, b3); 1026c1685086SJeff Roberson bucket_free(zone, b3, NULL); 1027c1685086SJeff Roberson } 1028a2de44abSAlexander Motin } 1029a2de44abSAlexander Motin 1030a2de44abSAlexander Motin /* 1031a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1032a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1033a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1034a2de44abSAlexander Motin * to safely access their cache buckets. 1035a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1036a2de44abSAlexander Motin */ 1037a2de44abSAlexander Motin static void 103808cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1039a2de44abSAlexander Motin { 1040a2de44abSAlexander Motin int cpu; 1041a2de44abSAlexander Motin 1042a2de44abSAlexander Motin /* 1043727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1044a2de44abSAlexander Motin */ 1045a2de44abSAlexander Motin if (zone) 104620a4e154SJeff Roberson cache_shrink(zone, NULL); 1047a2de44abSAlexander Motin else 104820a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1049a2de44abSAlexander Motin 1050a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1051a2de44abSAlexander Motin thread_lock(curthread); 1052a2de44abSAlexander Motin sched_bind(curthread, cpu); 1053a2de44abSAlexander Motin thread_unlock(curthread); 1054a2de44abSAlexander Motin 1055a2de44abSAlexander Motin if (zone) 105620a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1057a2de44abSAlexander Motin else 105820a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1059a2de44abSAlexander Motin } 1060a2de44abSAlexander Motin thread_lock(curthread); 1061a2de44abSAlexander Motin sched_unbind(curthread); 1062a2de44abSAlexander Motin thread_unlock(curthread); 1063a2de44abSAlexander Motin } 1064a2de44abSAlexander Motin 1065aaa8bb16SJeff Roberson /* 106608cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 106708cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 106808cfa56eSMark Johnston * estimated working set size. 1069aaa8bb16SJeff Roberson */ 1070aaa8bb16SJeff Roberson static void 107108cfa56eSMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain) 1072aaa8bb16SJeff Roberson { 1073ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1074aaa8bb16SJeff Roberson uma_bucket_t bucket; 107508cfa56eSMark Johnston long target, tofree; 1076ab3185d1SJeff Roberson int i; 10778355f576SJeff Roberson 1078ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 1079*91d947bfSJeff Roberson /* 1080*91d947bfSJeff Roberson * The cross bucket is partially filled and not part of 1081*91d947bfSJeff Roberson * the item count. Reclaim it individually here. 1082*91d947bfSJeff Roberson */ 1083ab3185d1SJeff Roberson zdom = &zone->uz_domain[i]; 1084*91d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 1085*91d947bfSJeff Roberson bucket = zdom->uzd_cross; 1086*91d947bfSJeff Roberson zdom->uzd_cross = NULL; 1087*91d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 1088*91d947bfSJeff Roberson if (bucket != NULL) { 1089*91d947bfSJeff Roberson bucket_drain(zone, bucket); 1090*91d947bfSJeff Roberson bucket_free(zone, bucket, NULL); 1091*91d947bfSJeff Roberson } 1092*91d947bfSJeff Roberson 1093*91d947bfSJeff Roberson /* 1094*91d947bfSJeff Roberson * Shrink the zone bucket size to ensure that the per-CPU caches 1095*91d947bfSJeff Roberson * don't grow too large. 1096*91d947bfSJeff Roberson */ 1097*91d947bfSJeff Roberson ZONE_LOCK(zone); 1098*91d947bfSJeff Roberson if (i == 0 && zone->uz_bucket_size > zone->uz_bucket_size_min) 1099*91d947bfSJeff Roberson zone->uz_bucket_size--; 110008cfa56eSMark Johnston 110108cfa56eSMark Johnston /* 110208cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 110308cfa56eSMark Johnston * this bucket cache is empty. Otherwise, we reclaim items in 110408cfa56eSMark Johnston * excess of the zone's estimated working set size. If the 110508cfa56eSMark Johnston * difference nitems - imin is larger than the WSS estimate, 110608cfa56eSMark Johnston * then the estimate will grow at the end of this interval and 110708cfa56eSMark Johnston * we ignore the historical average. 110808cfa56eSMark Johnston */ 110908cfa56eSMark Johnston target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems - 111008cfa56eSMark Johnston zdom->uzd_imin); 111108cfa56eSMark Johnston while (zdom->uzd_nitems > target) { 111208cfa56eSMark Johnston bucket = TAILQ_LAST(&zdom->uzd_buckets, uma_bucketlist); 111308cfa56eSMark Johnston if (bucket == NULL) 111408cfa56eSMark Johnston break; 111508cfa56eSMark Johnston tofree = bucket->ub_cnt; 111608cfa56eSMark Johnston TAILQ_REMOVE(&zdom->uzd_buckets, bucket, ub_link); 111708cfa56eSMark Johnston zdom->uzd_nitems -= tofree; 111808cfa56eSMark Johnston 111908cfa56eSMark Johnston /* 112008cfa56eSMark Johnston * Shift the bounds of the current WSS interval to avoid 112108cfa56eSMark Johnston * perturbing the estimate. 112208cfa56eSMark Johnston */ 112308cfa56eSMark Johnston zdom->uzd_imax -= lmin(zdom->uzd_imax, tofree); 112408cfa56eSMark Johnston zdom->uzd_imin -= lmin(zdom->uzd_imin, tofree); 112508cfa56eSMark Johnston 11268355f576SJeff Roberson ZONE_UNLOCK(zone); 11278355f576SJeff Roberson bucket_drain(zone, bucket); 11286fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 11298355f576SJeff Roberson ZONE_LOCK(zone); 11308355f576SJeff Roberson } 1131*91d947bfSJeff Roberson ZONE_UNLOCK(zone); 1132ab3185d1SJeff Roberson } 11338355f576SJeff Roberson } 1134fc03d22bSJeff Roberson 1135fc03d22bSJeff Roberson static void 1136fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1137fc03d22bSJeff Roberson { 1138fc03d22bSJeff Roberson uint8_t *mem; 1139fc03d22bSJeff Roberson int i; 1140fc03d22bSJeff Roberson uint8_t flags; 1141fc03d22bSJeff Roberson 11421431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 11431431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 11441431a748SGleb Smirnoff 11451e0701e1SJeff Roberson mem = slab_data(slab, keg); 1146fc03d22bSJeff Roberson flags = slab->us_flags; 1147fc03d22bSJeff Roberson i = start; 1148fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 1149fc03d22bSJeff Roberson for (i--; i > -1; i--) 1150c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1151c5deaf04SGleb Smirnoff /* 1152c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1153c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1154c5deaf04SGleb Smirnoff * which executed trash_dtor. 1155c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1156c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1157c5deaf04SGleb Smirnoff * invocations. 1158c5deaf04SGleb Smirnoff */ 11591e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1160c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1161c5deaf04SGleb Smirnoff #endif 11621e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1163fc03d22bSJeff Roberson } 1164fc03d22bSJeff Roberson if (keg->uk_flags & UMA_ZONE_OFFPAGE) 1165fc03d22bSJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 1166fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 11672e47807cSJeff Roberson uma_total_dec(PAGE_SIZE * keg->uk_ppera); 11688355f576SJeff Roberson } 11698355f576SJeff Roberson 11708355f576SJeff Roberson /* 1171e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 11728355f576SJeff Roberson * the pageout daemon. 11738355f576SJeff Roberson * 1174e20a199fSJeff Roberson * Returns nothing. 11758355f576SJeff Roberson */ 1176e20a199fSJeff Roberson static void 1177e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 11788355f576SJeff Roberson { 11791e183df2SStefan Farfeleder struct slabhead freeslabs = { 0 }; 1180ab3185d1SJeff Roberson uma_domain_t dom; 1181829be516SMark Johnston uma_slab_t slab, tmp; 11828b987a77SJeff Roberson int i, n; 11838355f576SJeff Roberson 11848355f576SJeff Roberson /* 1185e20a199fSJeff Roberson * We don't want to take pages from statically allocated kegs at this 11868355f576SJeff Roberson * time 11878355f576SJeff Roberson */ 1188099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 11898355f576SJeff Roberson return; 11908355f576SJeff Roberson 1191ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 11928b987a77SJeff Roberson CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u", 11938b987a77SJeff Roberson keg->uk_name, keg, i, dom->ud_free); 11948b987a77SJeff Roberson n = 0; 1195ab3185d1SJeff Roberson dom = &keg->uk_domain[i]; 11968b987a77SJeff Roberson KEG_LOCK(keg, i); 1197ab3185d1SJeff Roberson LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) { 1198829be516SMark Johnston /* We have nowhere to free these to. */ 1199829be516SMark Johnston if (slab->us_flags & UMA_SLAB_BOOT) 12008355f576SJeff Roberson continue; 1201099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 12021e0701e1SJeff Roberson UMA_HASH_REMOVE(&keg->uk_hash, slab); 12038b987a77SJeff Roberson n++; 12048b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 12051e0701e1SJeff Roberson LIST_INSERT_HEAD(&freeslabs, slab, us_link); 1206713deb36SJeff Roberson } 12078b987a77SJeff Roberson dom->ud_pages -= n * keg->uk_ppera; 12088b987a77SJeff Roberson dom->ud_free -= n * keg->uk_ipers; 12098b987a77SJeff Roberson KEG_UNLOCK(keg, i); 1210ab3185d1SJeff Roberson } 1211ab3185d1SJeff Roberson 12121e0701e1SJeff Roberson while ((slab = LIST_FIRST(&freeslabs)) != NULL) { 12131e0701e1SJeff Roberson LIST_REMOVE(slab, us_link); 12141645995bSKirk McKusick keg_free_slab(keg, slab, keg->uk_ipers); 12158355f576SJeff Roberson } 12168355f576SJeff Roberson } 12178355f576SJeff Roberson 1218e20a199fSJeff Roberson static void 121908cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain) 1220e20a199fSJeff Roberson { 1221e20a199fSJeff Roberson 12228355f576SJeff Roberson /* 1223e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 1224e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 1225e20a199fSJeff Roberson * is the only call that knows the structure will still be available 1226e20a199fSJeff Roberson * when it wakes up. 1227e20a199fSJeff Roberson */ 1228e20a199fSJeff Roberson ZONE_LOCK(zone); 122908cfa56eSMark Johnston while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) { 1230e20a199fSJeff Roberson if (waitok == M_NOWAIT) 1231e20a199fSJeff Roberson goto out; 1232727c6918SJeff Roberson msleep(zone, &zone->uz_lock, PVM, "zonedrain", 1); 1233e20a199fSJeff Roberson } 123408cfa56eSMark Johnston zone->uz_flags |= UMA_ZFLAG_RECLAIMING; 1235e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1236*91d947bfSJeff Roberson bucket_cache_reclaim(zone, drain); 123708cfa56eSMark Johnston 1238e20a199fSJeff Roberson /* 1239e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 1240111fbcd5SBryan Venteicher * we're running. Normally the uma_rwlock would protect us but we 1241e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 1242e20a199fSJeff Roberson */ 124308034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1244bb15d1c7SGleb Smirnoff keg_drain(zone->uz_keg); 1245e20a199fSJeff Roberson ZONE_LOCK(zone); 124608cfa56eSMark Johnston zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING; 1247e20a199fSJeff Roberson wakeup(zone); 1248e20a199fSJeff Roberson out: 1249e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1250e20a199fSJeff Roberson } 1251e20a199fSJeff Roberson 125208cfa56eSMark Johnston static void 125320a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused) 1254e20a199fSJeff Roberson { 1255e20a199fSJeff Roberson 125608cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, true); 125708cfa56eSMark Johnston } 125808cfa56eSMark Johnston 125908cfa56eSMark Johnston static void 126020a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused) 126108cfa56eSMark Johnston { 126208cfa56eSMark Johnston 126308cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, false); 1264e20a199fSJeff Roberson } 1265e20a199fSJeff Roberson 1266e20a199fSJeff Roberson /* 12678b987a77SJeff Roberson * Allocate a new slab for a keg and inserts it into the partial slab list. 12688b987a77SJeff Roberson * The keg should be unlocked on entry. If the allocation succeeds it will 12698b987a77SJeff Roberson * be locked on return. 12708355f576SJeff Roberson * 12718355f576SJeff Roberson * Arguments: 127286220393SMark Johnston * flags Wait flags for the item initialization routine 127386220393SMark Johnston * aflags Wait flags for the slab allocation 12748355f576SJeff Roberson * 12758355f576SJeff Roberson * Returns: 12768355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 12778355f576SJeff Roberson * caller specified M_NOWAIT. 12788355f576SJeff Roberson */ 12798355f576SJeff Roberson static uma_slab_t 128086220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 128186220393SMark Johnston int aflags) 12828355f576SJeff Roberson { 12838b987a77SJeff Roberson uma_domain_t dom; 1284e20a199fSJeff Roberson uma_alloc allocf; 1285099a0e58SBosko Milekic uma_slab_t slab; 12862e47807cSJeff Roberson unsigned long size; 128785dcf349SGleb Smirnoff uint8_t *mem; 128886220393SMark Johnston uint8_t sflags; 12898355f576SJeff Roberson int i; 12908355f576SJeff Roberson 1291ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1292ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1293a553d4b8SJeff Roberson 12948b987a77SJeff Roberson allocf = keg->uk_allocf; 1295194a979eSMark Johnston slab = NULL; 1296194a979eSMark Johnston mem = NULL; 1297099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_OFFPAGE) { 129886220393SMark Johnston slab = zone_alloc_item(keg->uk_slabzone, NULL, domain, aflags); 1299fc03d22bSJeff Roberson if (slab == NULL) 1300727c6918SJeff Roberson goto fail; 1301a553d4b8SJeff Roberson } 1302a553d4b8SJeff Roberson 13033370c5bfSJeff Roberson /* 13043370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 13053370c5bfSJeff Roberson * first time they are added to a zone. 13063370c5bfSJeff Roberson * 13073370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 13083370c5bfSJeff Roberson */ 13093370c5bfSJeff Roberson 1310099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 131186220393SMark Johnston aflags |= M_ZERO; 13123370c5bfSJeff Roberson else 131386220393SMark Johnston aflags &= ~M_ZERO; 13143370c5bfSJeff Roberson 1315263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 131686220393SMark Johnston aflags |= M_NODUMP; 1317263811f7SKip Macy 1318e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1319194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 132086220393SMark Johnston mem = allocf(zone, size, domain, &sflags, aflags); 1321a553d4b8SJeff Roberson if (mem == NULL) { 1322b23f72e9SBrian Feldman if (keg->uk_flags & UMA_ZONE_OFFPAGE) 13230095a784SJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 1324727c6918SJeff Roberson goto fail; 1325a553d4b8SJeff Roberson } 13262e47807cSJeff Roberson uma_total_inc(size); 13278355f576SJeff Roberson 13288b987a77SJeff Roberson /* For HASH zones all pages go to the same uma_domain. */ 13298b987a77SJeff Roberson if ((keg->uk_flags & UMA_ZONE_HASH) != 0) 13308b987a77SJeff Roberson domain = 0; 13318b987a77SJeff Roberson 13325c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 1333099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) 1334099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 13351e0701e1SJeff Roberson else 13361e0701e1SJeff Roberson ((uma_hash_slab_t)slab)->uhs_data = mem; 13375c0e403bSJeff Roberson 1338e20a199fSJeff Roberson if (keg->uk_flags & UMA_ZONE_VTOSLAB) 1339099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1340584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1341584061b4SJeff Roberson zone, slab); 13428355f576SJeff Roberson 1343099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 134486220393SMark Johnston slab->us_flags = sflags; 1345ab3185d1SJeff Roberson slab->us_domain = domain; 13468b987a77SJeff Roberson 13479b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1348ef72505eSJeff Roberson #ifdef INVARIANTS 1349815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1350ef72505eSJeff Roberson #endif 1351099a0e58SBosko Milekic 1352b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1353099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 13541e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 135586220393SMark Johnston keg->uk_size, flags) != 0) 1356b23f72e9SBrian Feldman break; 1357b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1358fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1359727c6918SJeff Roberson goto fail; 1360b23f72e9SBrian Feldman } 1361b23f72e9SBrian Feldman } 13628b987a77SJeff Roberson KEG_LOCK(keg, domain); 13635c0e403bSJeff Roberson 13641431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 13651431a748SGleb Smirnoff slab, keg->uk_name, keg); 13661431a748SGleb Smirnoff 1367099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 1368099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 13698355f576SJeff Roberson 13708b987a77SJeff Roberson /* 13718b987a77SJeff Roberson * If we got a slab here it's safe to mark it partially used 13728b987a77SJeff Roberson * and return. We assume that the caller is going to remove 13738b987a77SJeff Roberson * at least one item. 13748b987a77SJeff Roberson */ 13758b987a77SJeff Roberson dom = &keg->uk_domain[domain]; 13768b987a77SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 13778b987a77SJeff Roberson dom->ud_pages += keg->uk_ppera; 13788b987a77SJeff Roberson dom->ud_free += keg->uk_ipers; 13798355f576SJeff Roberson 13808355f576SJeff Roberson return (slab); 1381727c6918SJeff Roberson 1382727c6918SJeff Roberson fail: 1383727c6918SJeff Roberson return (NULL); 13848355f576SJeff Roberson } 13858355f576SJeff Roberson 13868355f576SJeff Roberson /* 1387009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1388009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1389009b6fcbSJeff Roberson * the VM is ready. 1390009b6fcbSJeff Roberson */ 1391009b6fcbSJeff Roberson static void * 1392ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1393ab3185d1SJeff Roberson int wait) 1394009b6fcbSJeff Roberson { 1395099a0e58SBosko Milekic uma_keg_t keg; 1396ac0a6fd0SGleb Smirnoff void *mem; 1397ac0a6fd0SGleb Smirnoff int pages; 1398099a0e58SBosko Milekic 1399bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1400009b6fcbSJeff Roberson /* 1401f7d35785SGleb Smirnoff * If we are in BOOT_BUCKETS or higher, than switch to real 1402f7d35785SGleb Smirnoff * allocator. Zones with page sized slabs switch at BOOT_PAGEALLOC. 1403009b6fcbSJeff Roberson */ 1404f7d35785SGleb Smirnoff switch (booted) { 1405f7d35785SGleb Smirnoff case BOOT_COLD: 1406f7d35785SGleb Smirnoff case BOOT_STRAPPED: 1407f7d35785SGleb Smirnoff break; 1408f7d35785SGleb Smirnoff case BOOT_PAGEALLOC: 1409f7d35785SGleb Smirnoff if (keg->uk_ppera > 1) 1410f7d35785SGleb Smirnoff break; 1411f7d35785SGleb Smirnoff case BOOT_BUCKETS: 1412f7d35785SGleb Smirnoff case BOOT_RUNNING: 1413009b6fcbSJeff Roberson #ifdef UMA_MD_SMALL_ALLOC 1414f7d35785SGleb Smirnoff keg->uk_allocf = (keg->uk_ppera > 1) ? 1415f7d35785SGleb Smirnoff page_alloc : uma_small_alloc; 1416009b6fcbSJeff Roberson #else 1417099a0e58SBosko Milekic keg->uk_allocf = page_alloc; 1418009b6fcbSJeff Roberson #endif 1419ab3185d1SJeff Roberson return keg->uk_allocf(zone, bytes, domain, pflag, wait); 1420009b6fcbSJeff Roberson } 1421009b6fcbSJeff Roberson 1422009b6fcbSJeff Roberson /* 1423f7d35785SGleb Smirnoff * Check our small startup cache to see if it has pages remaining. 1424f7d35785SGleb Smirnoff */ 1425f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1426f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1427f7d35785SGleb Smirnoff if (pages > boot_pages) 1428f7d35785SGleb Smirnoff panic("UMA zone \"%s\": Increase vm.boot_pages", zone->uz_name); 1429f7d35785SGleb Smirnoff #ifdef DIAGNOSTIC 1430f7d35785SGleb Smirnoff printf("%s from \"%s\", %d boot pages left\n", __func__, zone->uz_name, 1431f7d35785SGleb Smirnoff boot_pages); 1432f7d35785SGleb Smirnoff #endif 1433f7d35785SGleb Smirnoff mem = bootmem; 1434f7d35785SGleb Smirnoff boot_pages -= pages; 1435f7d35785SGleb Smirnoff bootmem += pages * PAGE_SIZE; 1436f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1437f7d35785SGleb Smirnoff 1438f7d35785SGleb Smirnoff return (mem); 1439f7d35785SGleb Smirnoff } 1440f7d35785SGleb Smirnoff 1441f7d35785SGleb Smirnoff /* 14428355f576SJeff Roberson * Allocates a number of pages from the system 14438355f576SJeff Roberson * 14448355f576SJeff Roberson * Arguments: 14458355f576SJeff Roberson * bytes The number of bytes requested 14468355f576SJeff Roberson * wait Shall we wait? 14478355f576SJeff Roberson * 14488355f576SJeff Roberson * Returns: 14498355f576SJeff Roberson * A pointer to the alloced memory or possibly 14508355f576SJeff Roberson * NULL if M_NOWAIT is set. 14518355f576SJeff Roberson */ 14528355f576SJeff Roberson static void * 1453ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1454ab3185d1SJeff Roberson int wait) 14558355f576SJeff Roberson { 14568355f576SJeff Roberson void *p; /* Returned page */ 14578355f576SJeff Roberson 14582e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 14599978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 14608355f576SJeff Roberson 14618355f576SJeff Roberson return (p); 14628355f576SJeff Roberson } 14638355f576SJeff Roberson 1464ab3059a8SMatt Macy static void * 1465ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1466ab3059a8SMatt Macy int wait) 1467ab3059a8SMatt Macy { 1468ab3059a8SMatt Macy struct pglist alloctail; 1469ab3059a8SMatt Macy vm_offset_t addr, zkva; 1470ab3059a8SMatt Macy int cpu, flags; 1471ab3059a8SMatt Macy vm_page_t p, p_next; 1472ab3059a8SMatt Macy #ifdef NUMA 1473ab3059a8SMatt Macy struct pcpu *pc; 1474ab3059a8SMatt Macy #endif 1475ab3059a8SMatt Macy 1476ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1477ab3059a8SMatt Macy 1478013072f0SMark Johnston TAILQ_INIT(&alloctail); 1479ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1480013072f0SMark Johnston malloc2vm_flags(wait); 1481013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1482ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1483ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1484ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1485ab3059a8SMatt Macy } else { 1486ab3059a8SMatt Macy #ifndef NUMA 1487ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1488ab3059a8SMatt Macy #else 1489ab3059a8SMatt Macy pc = pcpu_find(cpu); 1490ab3059a8SMatt Macy p = vm_page_alloc_domain(NULL, 0, pc->pc_domain, flags); 1491ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1492ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1493ab3059a8SMatt Macy #endif 1494ab3059a8SMatt Macy } 1495ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1496ab3059a8SMatt Macy goto fail; 1497ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1498ab3059a8SMatt Macy } 1499ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1500ab3059a8SMatt Macy goto fail; 1501ab3059a8SMatt Macy zkva = addr; 1502ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1503ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1504ab3059a8SMatt Macy zkva += PAGE_SIZE; 1505ab3059a8SMatt Macy } 1506ab3059a8SMatt Macy return ((void*)addr); 1507ab3059a8SMatt Macy fail: 1508ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 150988ea538aSMark Johnston vm_page_unwire_noq(p); 1510ab3059a8SMatt Macy vm_page_free(p); 1511ab3059a8SMatt Macy } 1512ab3059a8SMatt Macy return (NULL); 1513ab3059a8SMatt Macy } 1514ab3059a8SMatt Macy 15158355f576SJeff Roberson /* 15168355f576SJeff Roberson * Allocates a number of pages from within an object 15178355f576SJeff Roberson * 15188355f576SJeff Roberson * Arguments: 15198355f576SJeff Roberson * bytes The number of bytes requested 15208355f576SJeff Roberson * wait Shall we wait? 15218355f576SJeff Roberson * 15228355f576SJeff Roberson * Returns: 15238355f576SJeff Roberson * A pointer to the alloced memory or possibly 15248355f576SJeff Roberson * NULL if M_NOWAIT is set. 15258355f576SJeff Roberson */ 15268355f576SJeff Roberson static void * 1527ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1528ab3185d1SJeff Roberson int wait) 15298355f576SJeff Roberson { 1530a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1531a4915c21SAttilio Rao u_long npages; 1532b245ac95SAlan Cox vm_offset_t retkva, zkva; 1533a4915c21SAttilio Rao vm_page_t p, p_next; 1534e20a199fSJeff Roberson uma_keg_t keg; 15358355f576SJeff Roberson 1536a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1537bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1538a4915c21SAttilio Rao 1539a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1540a4915c21SAttilio Rao while (npages > 0) { 1541ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 15428d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1543772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1544772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1545a4915c21SAttilio Rao if (p != NULL) { 1546a4915c21SAttilio Rao /* 1547a4915c21SAttilio Rao * Since the page does not belong to an object, its 1548a4915c21SAttilio Rao * listq is unused. 1549a4915c21SAttilio Rao */ 1550a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1551a4915c21SAttilio Rao npages--; 1552a4915c21SAttilio Rao continue; 1553a4915c21SAttilio Rao } 15548355f576SJeff Roberson /* 1555a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1556a4915c21SAttilio Rao * exit. 15578355f576SJeff Roberson */ 1558a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 155988ea538aSMark Johnston vm_page_unwire_noq(p); 1560b245ac95SAlan Cox vm_page_free(p); 1561b245ac95SAlan Cox } 1562a4915c21SAttilio Rao return (NULL); 1563b245ac95SAlan Cox } 15648355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1565a4915c21SAttilio Rao zkva = keg->uk_kva + 1566a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1567a4915c21SAttilio Rao retkva = zkva; 1568a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1569a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1570a4915c21SAttilio Rao zkva += PAGE_SIZE; 1571a4915c21SAttilio Rao } 15728355f576SJeff Roberson 15738355f576SJeff Roberson return ((void *)retkva); 15748355f576SJeff Roberson } 15758355f576SJeff Roberson 15768355f576SJeff Roberson /* 15778355f576SJeff Roberson * Frees a number of pages to the system 15788355f576SJeff Roberson * 15798355f576SJeff Roberson * Arguments: 15808355f576SJeff Roberson * mem A pointer to the memory to be freed 15818355f576SJeff Roberson * size The size of the memory being freed 15828355f576SJeff Roberson * flags The original p->us_flags field 15838355f576SJeff Roberson * 15848355f576SJeff Roberson * Returns: 15858355f576SJeff Roberson * Nothing 15868355f576SJeff Roberson */ 15878355f576SJeff Roberson static void 1588f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 15898355f576SJeff Roberson { 15903370c5bfSJeff Roberson 159149bfa624SAlan Cox if ((flags & UMA_SLAB_KERNEL) == 0) 1592b5345ef1SJustin Hibbits panic("UMA: page_free used with invalid flags %x", flags); 15938355f576SJeff Roberson 159449bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 15958355f576SJeff Roberson } 15968355f576SJeff Roberson 15978355f576SJeff Roberson /* 1598ab3059a8SMatt Macy * Frees pcpu zone allocations 1599ab3059a8SMatt Macy * 1600ab3059a8SMatt Macy * Arguments: 1601ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1602ab3059a8SMatt Macy * size The size of the memory being freed 1603ab3059a8SMatt Macy * flags The original p->us_flags field 1604ab3059a8SMatt Macy * 1605ab3059a8SMatt Macy * Returns: 1606ab3059a8SMatt Macy * Nothing 1607ab3059a8SMatt Macy */ 1608ab3059a8SMatt Macy static void 1609ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1610ab3059a8SMatt Macy { 1611ab3059a8SMatt Macy vm_offset_t sva, curva; 1612ab3059a8SMatt Macy vm_paddr_t paddr; 1613ab3059a8SMatt Macy vm_page_t m; 1614ab3059a8SMatt Macy 1615ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 1616ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1617ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1618ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1619ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 162088ea538aSMark Johnston vm_page_unwire_noq(m); 1621ab3059a8SMatt Macy vm_page_free(m); 1622ab3059a8SMatt Macy } 1623ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1624ab3059a8SMatt Macy kva_free(sva, size); 1625ab3059a8SMatt Macy } 1626ab3059a8SMatt Macy 1627ab3059a8SMatt Macy 1628ab3059a8SMatt Macy /* 16298355f576SJeff Roberson * Zero fill initializer 16308355f576SJeff Roberson * 16318355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 16328355f576SJeff Roberson */ 1633b23f72e9SBrian Feldman static int 1634b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 16358355f576SJeff Roberson { 16368355f576SJeff Roberson bzero(mem, size); 1637b23f72e9SBrian Feldman return (0); 16388355f576SJeff Roberson } 16398355f576SJeff Roberson 1640815db204SRyan Libby #ifdef INVARIANTS 1641815db204SRyan Libby struct noslabbits * 1642815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1643815db204SRyan Libby { 1644815db204SRyan Libby 1645815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1646815db204SRyan Libby } 1647815db204SRyan Libby #endif 1648815db204SRyan Libby 16498355f576SJeff Roberson /* 16509b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 16519b78b1f4SJeff Roberson */ 16529b78b1f4SJeff Roberson size_t 16539b78b1f4SJeff Roberson slab_sizeof(int nitems) 16549b78b1f4SJeff Roberson { 16559b78b1f4SJeff Roberson size_t s; 16569b78b1f4SJeff Roberson 1657815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 16589b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 16599b78b1f4SJeff Roberson } 16609b78b1f4SJeff Roberson 16619b78b1f4SJeff Roberson /* 16629b78b1f4SJeff Roberson * Size of memory for embedded slabs (!OFFPAGE). 16639b78b1f4SJeff Roberson */ 16649b78b1f4SJeff Roberson size_t 16659b78b1f4SJeff Roberson slab_space(int nitems) 16669b78b1f4SJeff Roberson { 16679b78b1f4SJeff Roberson return (UMA_SLAB_SIZE - slab_sizeof(nitems)); 16689b78b1f4SJeff Roberson } 16699b78b1f4SJeff Roberson 16709b78b1f4SJeff Roberson /* 16719b78b1f4SJeff Roberson * Compute the number of items that will fit in an embedded (!OFFPAGE) slab 16729b78b1f4SJeff Roberson * with a given size and alignment. 16739b78b1f4SJeff Roberson */ 16749b78b1f4SJeff Roberson int 16759b78b1f4SJeff Roberson slab_ipers(size_t size, int align) 16769b78b1f4SJeff Roberson { 16779b78b1f4SJeff Roberson int rsize; 16789b78b1f4SJeff Roberson int nitems; 16799b78b1f4SJeff Roberson 16809b78b1f4SJeff Roberson /* 16819b78b1f4SJeff Roberson * Compute the ideal number of items that will fit in a page and 16829b78b1f4SJeff Roberson * then compute the actual number based on a bitset nitems wide. 16839b78b1f4SJeff Roberson */ 16849b78b1f4SJeff Roberson rsize = roundup(size, align + 1); 16859b78b1f4SJeff Roberson nitems = UMA_SLAB_SIZE / rsize; 16869b78b1f4SJeff Roberson return (slab_space(nitems) / rsize); 16879b78b1f4SJeff Roberson } 16889b78b1f4SJeff Roberson 16899b78b1f4SJeff Roberson /* 1690e20a199fSJeff Roberson * Finish creating a small uma keg. This calculates ipers, and the keg size. 16918355f576SJeff Roberson * 16928355f576SJeff Roberson * Arguments 1693e20a199fSJeff Roberson * keg The zone we should initialize 16948355f576SJeff Roberson * 16958355f576SJeff Roberson * Returns 16968355f576SJeff Roberson * Nothing 16978355f576SJeff Roberson */ 16988355f576SJeff Roberson static void 1699e20a199fSJeff Roberson keg_small_init(uma_keg_t keg) 17008355f576SJeff Roberson { 1701244f4554SBosko Milekic u_int rsize; 1702244f4554SBosko Milekic u_int memused; 1703244f4554SBosko Milekic u_int wastedspace; 1704244f4554SBosko Milekic u_int shsize; 1705a55ebb7cSAndriy Gapon u_int slabsize; 17068355f576SJeff Roberson 1707ad97af7eSGleb Smirnoff if (keg->uk_flags & UMA_ZONE_PCPU) { 170896c85efbSNathan Whitehorn u_int ncpus = (mp_maxid + 1) ? (mp_maxid + 1) : MAXCPU; 1709e28a647dSGleb Smirnoff 1710ab3059a8SMatt Macy slabsize = UMA_PCPU_ALLOC_SIZE; 1711ab3059a8SMatt Macy keg->uk_ppera = ncpus; 1712ad97af7eSGleb Smirnoff } else { 1713a55ebb7cSAndriy Gapon slabsize = UMA_SLAB_SIZE; 1714ad97af7eSGleb Smirnoff keg->uk_ppera = 1; 1715ad97af7eSGleb Smirnoff } 1716ad97af7eSGleb Smirnoff 1717ef72505eSJeff Roberson /* 1718ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1719ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1720ef72505eSJeff Roberson * allocation bits for we round it up. 1721ef72505eSJeff Roberson */ 1722099a0e58SBosko Milekic rsize = keg->uk_size; 17239b78b1f4SJeff Roberson if (rsize < slabsize / SLAB_MAX_SETSIZE) 17249b78b1f4SJeff Roberson rsize = slabsize / SLAB_MAX_SETSIZE; 1725099a0e58SBosko Milekic if (rsize & keg->uk_align) 17269b78b1f4SJeff Roberson rsize = roundup(rsize, keg->uk_align + 1); 1727099a0e58SBosko Milekic keg->uk_rsize = rsize; 1728ad97af7eSGleb Smirnoff 1729ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 1730ab3059a8SMatt Macy keg->uk_rsize < UMA_PCPU_ALLOC_SIZE, 1731ad97af7eSGleb Smirnoff ("%s: size %u too large", __func__, keg->uk_rsize)); 17328355f576SJeff Roberson 17339b78b1f4SJeff Roberson /* 17349b78b1f4SJeff Roberson * Use a pessimistic bit count for shsize. It may be possible to 17359b78b1f4SJeff Roberson * squeeze one more item in for very particular sizes if we were 17369b78b1f4SJeff Roberson * to loop and reduce the bitsize if there is waste. 17379b78b1f4SJeff Roberson */ 1738ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_OFFPAGE) 17392864dbbfSGleb Smirnoff shsize = 0; 1740ef72505eSJeff Roberson else 17419b78b1f4SJeff Roberson shsize = slab_sizeof(slabsize / rsize); 17428355f576SJeff Roberson 17431ca6ed45SGleb Smirnoff if (rsize <= slabsize - shsize) 1744a55ebb7cSAndriy Gapon keg->uk_ipers = (slabsize - shsize) / rsize; 17451ca6ed45SGleb Smirnoff else { 17461ca6ed45SGleb Smirnoff /* Handle special case when we have 1 item per slab, so 17471ca6ed45SGleb Smirnoff * alignment requirement can be relaxed. */ 17481ca6ed45SGleb Smirnoff KASSERT(keg->uk_size <= slabsize - shsize, 17491ca6ed45SGleb Smirnoff ("%s: size %u greater than slab", __func__, keg->uk_size)); 17501ca6ed45SGleb Smirnoff keg->uk_ipers = 1; 17511ca6ed45SGleb Smirnoff } 17529b78b1f4SJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 1753ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 1754ad97af7eSGleb Smirnoff 1755244f4554SBosko Milekic memused = keg->uk_ipers * rsize + shsize; 1756a55ebb7cSAndriy Gapon wastedspace = slabsize - memused; 1757244f4554SBosko Milekic 175820e8e865SBosko Milekic /* 1759244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 176020e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 17616fd34d6fSJeff Roberson * may end up going to the VM for slabs which we do not 17626fd34d6fSJeff Roberson * want to do if we're UMA_ZFLAG_CACHEONLY as a result 17636fd34d6fSJeff Roberson * of UMA_ZONE_VM, which clearly forbids it. 176420e8e865SBosko Milekic */ 1765099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) || 1766099a0e58SBosko Milekic (keg->uk_flags & UMA_ZFLAG_CACHEONLY)) 17678355f576SJeff Roberson return; 1768244f4554SBosko Milekic 1769ef72505eSJeff Roberson /* 1770ef72505eSJeff Roberson * See if using an OFFPAGE slab will limit our waste. Only do 1771ef72505eSJeff Roberson * this if it permits more items per-slab. 1772ef72505eSJeff Roberson * 1773ef72505eSJeff Roberson * XXX We could try growing slabsize to limit max waste as well. 1774ef72505eSJeff Roberson * Historically this was not done because the VM could not 1775ef72505eSJeff Roberson * efficiently handle contiguous allocations. 1776ef72505eSJeff Roberson */ 1777a55ebb7cSAndriy Gapon if ((wastedspace >= slabsize / UMA_MAX_WASTE) && 1778a55ebb7cSAndriy Gapon (keg->uk_ipers < (slabsize / keg->uk_rsize))) { 1779a55ebb7cSAndriy Gapon keg->uk_ipers = slabsize / keg->uk_rsize; 17809b78b1f4SJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 1781ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 17821431a748SGleb Smirnoff CTR6(KTR_UMA, "UMA decided we need offpage slab headers for " 17831431a748SGleb Smirnoff "keg: %s(%p), calculated wastedspace = %d, " 1784244f4554SBosko Milekic "maximum wasted space allowed = %d, " 1785244f4554SBosko Milekic "calculated ipers = %d, " 17861431a748SGleb Smirnoff "new wasted space = %d\n", keg->uk_name, keg, wastedspace, 1787a55ebb7cSAndriy Gapon slabsize / UMA_MAX_WASTE, keg->uk_ipers, 1788a55ebb7cSAndriy Gapon slabsize - keg->uk_ipers * keg->uk_rsize); 178971353f7aSJeff Roberson /* 179071353f7aSJeff Roberson * If we had access to memory to embed a slab header we 179171353f7aSJeff Roberson * also have a page structure to use vtoslab() instead of 179271353f7aSJeff Roberson * hash to find slabs. If the zone was explicitly created 179371353f7aSJeff Roberson * OFFPAGE we can't necessarily touch the memory. 179471353f7aSJeff Roberson */ 179571353f7aSJeff Roberson if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0) 179671353f7aSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 17978355f576SJeff Roberson } 1798ad97af7eSGleb Smirnoff 1799ad97af7eSGleb Smirnoff if ((keg->uk_flags & UMA_ZONE_OFFPAGE) && 1800ad97af7eSGleb Smirnoff (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1801ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_HASH; 18028355f576SJeff Roberson } 18038355f576SJeff Roberson 18048355f576SJeff Roberson /* 1805e20a199fSJeff Roberson * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do 18068355f576SJeff Roberson * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be 18078355f576SJeff Roberson * more complicated. 18088355f576SJeff Roberson * 18098355f576SJeff Roberson * Arguments 1810e20a199fSJeff Roberson * keg The keg we should initialize 18118355f576SJeff Roberson * 18128355f576SJeff Roberson * Returns 18138355f576SJeff Roberson * Nothing 18148355f576SJeff Roberson */ 18158355f576SJeff Roberson static void 1816e20a199fSJeff Roberson keg_large_init(uma_keg_t keg) 18178355f576SJeff Roberson { 18188355f576SJeff Roberson 1819e20a199fSJeff Roberson KASSERT(keg != NULL, ("Keg is null in keg_large_init")); 1820ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1821ad97af7eSGleb Smirnoff ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__)); 182220e8e865SBosko Milekic 1823ad97af7eSGleb Smirnoff keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE); 1824099a0e58SBosko Milekic keg->uk_ipers = 1; 1825e9a069d8SJohn Baldwin keg->uk_rsize = keg->uk_size; 1826e9a069d8SJohn Baldwin 1827cec48e00SAlexander Motin /* Check whether we have enough space to not do OFFPAGE. */ 18283d5e3df7SGleb Smirnoff if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0 && 18299b78b1f4SJeff Roberson PAGE_SIZE * keg->uk_ppera - keg->uk_rsize < 18309b78b1f4SJeff Roberson slab_sizeof(SLAB_MIN_SETSIZE)) { 18312934eb8aSMark Johnston /* 18322934eb8aSMark Johnston * We can't do OFFPAGE if we're internal, in which case 18332934eb8aSMark Johnston * we need an extra page per allocation to contain the 18342934eb8aSMark Johnston * slab header. 18352934eb8aSMark Johnston */ 18362934eb8aSMark Johnston if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) == 0) 183771353f7aSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 18382934eb8aSMark Johnston else 18392934eb8aSMark Johnston keg->uk_ppera++; 18402934eb8aSMark Johnston } 1841cec48e00SAlexander Motin 1842cec48e00SAlexander Motin if ((keg->uk_flags & UMA_ZONE_OFFPAGE) && 1843cec48e00SAlexander Motin (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1844099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_HASH; 18458355f576SJeff Roberson } 18468355f576SJeff Roberson 1847e20a199fSJeff Roberson static void 1848e20a199fSJeff Roberson keg_cachespread_init(uma_keg_t keg) 1849e20a199fSJeff Roberson { 1850e20a199fSJeff Roberson int alignsize; 1851e20a199fSJeff Roberson int trailer; 1852e20a199fSJeff Roberson int pages; 1853e20a199fSJeff Roberson int rsize; 1854e20a199fSJeff Roberson 1855ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1856ad97af7eSGleb Smirnoff ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__)); 1857ad97af7eSGleb Smirnoff 1858e20a199fSJeff Roberson alignsize = keg->uk_align + 1; 1859e20a199fSJeff Roberson rsize = keg->uk_size; 1860e20a199fSJeff Roberson /* 1861e20a199fSJeff Roberson * We want one item to start on every align boundary in a page. To 1862e20a199fSJeff Roberson * do this we will span pages. We will also extend the item by the 1863e20a199fSJeff Roberson * size of align if it is an even multiple of align. Otherwise, it 1864e20a199fSJeff Roberson * would fall on the same boundary every time. 1865e20a199fSJeff Roberson */ 1866e20a199fSJeff Roberson if (rsize & keg->uk_align) 1867e20a199fSJeff Roberson rsize = (rsize & ~keg->uk_align) + alignsize; 1868e20a199fSJeff Roberson if ((rsize & alignsize) == 0) 1869e20a199fSJeff Roberson rsize += alignsize; 1870e20a199fSJeff Roberson trailer = rsize - keg->uk_size; 1871e20a199fSJeff Roberson pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE; 1872e20a199fSJeff Roberson pages = MIN(pages, (128 * 1024) / PAGE_SIZE); 1873e20a199fSJeff Roberson keg->uk_rsize = rsize; 1874e20a199fSJeff Roberson keg->uk_ppera = pages; 1875e20a199fSJeff Roberson keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize; 1876e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 18779b78b1f4SJeff Roberson KASSERT(keg->uk_ipers <= SLAB_MAX_SETSIZE, 187842321809SGleb Smirnoff ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__, 1879e20a199fSJeff Roberson keg->uk_ipers)); 1880e20a199fSJeff Roberson } 1881e20a199fSJeff Roberson 18828355f576SJeff Roberson /* 1883099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 1884099a0e58SBosko Milekic * the keg onto the global keg list. 18858355f576SJeff Roberson * 18868355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 1887099a0e58SBosko Milekic * udata Actually uma_kctor_args 1888099a0e58SBosko Milekic */ 1889b23f72e9SBrian Feldman static int 1890b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 1891099a0e58SBosko Milekic { 1892099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 1893099a0e58SBosko Milekic uma_keg_t keg = mem; 1894099a0e58SBosko Milekic uma_zone_t zone; 18958b987a77SJeff Roberson int i; 1896099a0e58SBosko Milekic 1897099a0e58SBosko Milekic bzero(keg, size); 1898099a0e58SBosko Milekic keg->uk_size = arg->size; 1899099a0e58SBosko Milekic keg->uk_init = arg->uminit; 1900099a0e58SBosko Milekic keg->uk_fini = arg->fini; 1901099a0e58SBosko Milekic keg->uk_align = arg->align; 19026fd34d6fSJeff Roberson keg->uk_reserve = 0; 1903099a0e58SBosko Milekic keg->uk_flags = arg->flags; 1904099a0e58SBosko Milekic keg->uk_slabzone = NULL; 1905099a0e58SBosko Milekic 1906099a0e58SBosko Milekic /* 1907194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 1908194a979eSMark Johnston * UMA_ZONE_NUMA set will use first-touch instead, in which case the 1909194a979eSMark Johnston * iterator is never run. 1910194a979eSMark Johnston */ 1911194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 1912194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 1913194a979eSMark Johnston 1914194a979eSMark Johnston /* 1915099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 1916099a0e58SBosko Milekic */ 1917099a0e58SBosko Milekic zone = arg->zone; 1918e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 1919099a0e58SBosko Milekic 1920099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_VM) 1921099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 1922099a0e58SBosko Milekic 1923099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 1924099a0e58SBosko Milekic keg->uk_init = zero_init; 1925099a0e58SBosko Milekic 1926cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 1927e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_VTOSLAB; 1928e20a199fSJeff Roberson 1929ad97af7eSGleb Smirnoff if (arg->flags & UMA_ZONE_PCPU) 1930ad97af7eSGleb Smirnoff #ifdef SMP 1931ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_OFFPAGE; 1932ad97af7eSGleb Smirnoff #else 1933ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 1934ad97af7eSGleb Smirnoff #endif 1935ad97af7eSGleb Smirnoff 1936ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_CACHESPREAD) { 1937e20a199fSJeff Roberson keg_cachespread_init(keg); 1938244f4554SBosko Milekic } else { 19399b78b1f4SJeff Roberson if (keg->uk_size > slab_space(SLAB_MIN_SETSIZE)) 1940e20a199fSJeff Roberson keg_large_init(keg); 1941244f4554SBosko Milekic else 1942e20a199fSJeff Roberson keg_small_init(keg); 1943244f4554SBosko Milekic } 1944099a0e58SBosko Milekic 19458b987a77SJeff Roberson /* 19468b987a77SJeff Roberson * Sets all kegs with memory that comes from the page array to a 19478b987a77SJeff Roberson * first-touch domain policy. 19488b987a77SJeff Roberson */ 19498b987a77SJeff Roberson #ifdef UMA_FIRSTTOUCH 19508b987a77SJeff Roberson if ((keg->uk_flags & UMA_ZONE_HASH) == 0) 19518b987a77SJeff Roberson keg->uk_flags |= UMA_ZONE_NUMA; 19528b987a77SJeff Roberson #endif 19538b987a77SJeff Roberson 1954cfcae3f8SGleb Smirnoff if (keg->uk_flags & UMA_ZONE_OFFPAGE) 1955099a0e58SBosko Milekic keg->uk_slabzone = slabzone; 1956099a0e58SBosko Milekic 1957099a0e58SBosko Milekic /* 1958099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 1959099a0e58SBosko Milekic * startup cache until the vm is ready. 1960099a0e58SBosko Milekic */ 1961f4bef67cSGleb Smirnoff if (booted < BOOT_PAGEALLOC) 19628cd02d00SAlan Cox keg->uk_allocf = startup_alloc; 196377e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 196477e19437SGleb Smirnoff else if (keg->uk_ppera == 1) 196577e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 19668cd02d00SAlan Cox #endif 1967ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 1968ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 196977e19437SGleb Smirnoff else 197077e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 197177e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 197277e19437SGleb Smirnoff if (keg->uk_ppera == 1) 197377e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 197477e19437SGleb Smirnoff else 197577e19437SGleb Smirnoff #endif 1976ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 1977ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 1978ab3059a8SMatt Macy else 197977e19437SGleb Smirnoff keg->uk_freef = page_free; 1980099a0e58SBosko Milekic 1981099a0e58SBosko Milekic /* 19828b987a77SJeff Roberson * Initialize keg's locks. 1983099a0e58SBosko Milekic */ 19848b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 19858b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 1986099a0e58SBosko Milekic 1987099a0e58SBosko Milekic /* 1988099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 19899b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 19909b78b1f4SJeff Roberson * definition. 1991099a0e58SBosko Milekic */ 1992099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) { 19939b78b1f4SJeff Roberson size_t shsize; 19949b78b1f4SJeff Roberson 19959b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 19969b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 1997244f4554SBosko Milekic /* 1998244f4554SBosko Milekic * The only way the following is possible is if with our 1999244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 2000244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 2001244f4554SBosko Milekic * mathematically possible for all cases, so we make 2002244f4554SBosko Milekic * sure here anyway. 2003244f4554SBosko Milekic */ 20049b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 20053d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 20063d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 2007099a0e58SBosko Milekic } 2008099a0e58SBosko Milekic 2009099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 20103b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 2011099a0e58SBosko Milekic 20128b987a77SJeff Roberson CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)\n", keg, zone->uz_name, zone); 2013099a0e58SBosko Milekic 2014099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 2015099a0e58SBosko Milekic 2016111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2017099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2018111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2019b23f72e9SBrian Feldman return (0); 2020099a0e58SBosko Milekic } 2021099a0e58SBosko Milekic 20222efcc8cbSGleb Smirnoff static void 202320a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 20242efcc8cbSGleb Smirnoff { 20252efcc8cbSGleb Smirnoff 20262efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 20272efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 20282efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 20292efcc8cbSGleb Smirnoff } 20302efcc8cbSGleb Smirnoff 203120a4e154SJeff Roberson static void 203220a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 203320a4e154SJeff Roberson { 203420a4e154SJeff Roberson uma_zone_domain_t zdom; 20358b987a77SJeff Roberson uma_domain_t dom; 203620a4e154SJeff Roberson uma_keg_t keg; 203720a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 20383b490537SJeff Roberson int domains, i, cnt; 203920a4e154SJeff Roberson static const char *nokeg = "cache zone"; 204020a4e154SJeff Roberson char *c; 204120a4e154SJeff Roberson 204220a4e154SJeff Roberson /* 204320a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 204420a4e154SJeff Roberson * any special characters and handling dups by appending 204520a4e154SJeff Roberson * an index. 204620a4e154SJeff Roberson */ 204720a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 20483b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 20493b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 20503b490537SJeff Roberson cnt /= 10; 20513b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 20523b490537SJeff Roberson M_UMA, M_WAITOK); 205320a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 205420a4e154SJeff Roberson zone->uz_namecnt); 205520a4e154SJeff Roberson } else 205620a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 205720a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 205820a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 205920a4e154SJeff Roberson *c = '_'; 206020a4e154SJeff Roberson 206120a4e154SJeff Roberson /* 206220a4e154SJeff Roberson * Basic parameters at the root. 206320a4e154SJeff Roberson */ 206420a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 206520a4e154SJeff Roberson OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, ""); 206620a4e154SJeff Roberson oid = zone->uz_oid; 206720a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 206820a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 20696d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 20706d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 20716d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 207220a4e154SJeff Roberson "Allocator configuration flags"); 207320a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 207420a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 207520a4e154SJeff Roberson "Desired per-cpu cache size"); 207620a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 207720a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 207820a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 207920a4e154SJeff Roberson 208020a4e154SJeff Roberson /* 208120a4e154SJeff Roberson * keg if present. 208220a4e154SJeff Roberson */ 20838b987a77SJeff Roberson if ((zone->uz_flags & UMA_ZONE_HASH) == 0) 20848b987a77SJeff Roberson domains = vm_ndomains; 20858b987a77SJeff Roberson else 20868b987a77SJeff Roberson domains = 1; 208720a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 208820a4e154SJeff Roberson "keg", CTLFLAG_RD, NULL, ""); 208920a4e154SJeff Roberson keg = zone->uz_keg; 20903b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 209120a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 209220a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 209320a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 209420a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 209520a4e154SJeff Roberson "Real object size with alignment"); 209620a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 209720a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 209820a4e154SJeff Roberson "pages per-slab allocation"); 209920a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210020a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 210120a4e154SJeff Roberson "items available per-slab"); 210220a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210320a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 210420a4e154SJeff Roberson "item alignment mask"); 2105f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2106f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2107f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2108f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 21098b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 21108b987a77SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 21118b987a77SJeff Roberson for (i = 0; i < domains; i++) { 21128b987a77SJeff Roberson dom = &keg->uk_domain[i]; 21138b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 21148b987a77SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, 21158b987a77SJeff Roberson NULL, ""); 21168b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21178b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 21188b987a77SJeff Roberson "Total pages currently allocated from VM"); 21198b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21208b987a77SJeff Roberson "free", CTLFLAG_RD, &dom->ud_free, 0, 21218b987a77SJeff Roberson "items free in the slab layer"); 21228b987a77SJeff Roberson } 212320a4e154SJeff Roberson } else 212420a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 212520a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 212620a4e154SJeff Roberson 212720a4e154SJeff Roberson /* 212820a4e154SJeff Roberson * Information about zone limits. 212920a4e154SJeff Roberson */ 213020a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 213120a4e154SJeff Roberson "limit", CTLFLAG_RD, NULL, ""); 21324bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21334bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 21344bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 21354bd61e19SJeff Roberson "current number of allocated items if limit is set"); 213620a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213720a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 213820a4e154SJeff Roberson "Maximum number of cached items"); 213920a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 214020a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 214120a4e154SJeff Roberson "Number of threads sleeping at limit"); 214220a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 214320a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 214420a4e154SJeff Roberson "Total zone limit sleeps"); 21454bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21464bd61e19SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bkt_max, 0, 21474bd61e19SJeff Roberson "Maximum number of items in the bucket cache"); 21484bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21494bd61e19SJeff Roberson "bucket_cnt", CTLFLAG_RD, &zone->uz_bkt_count, 0, 21504bd61e19SJeff Roberson "Number of items in the bucket cache"); 215120a4e154SJeff Roberson 215220a4e154SJeff Roberson /* 21538b987a77SJeff Roberson * Per-domain zone information. 215420a4e154SJeff Roberson */ 215520a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 215620a4e154SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 21578b987a77SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) == 0) 21588b987a77SJeff Roberson domains = 1; 215920a4e154SJeff Roberson for (i = 0; i < domains; i++) { 216020a4e154SJeff Roberson zdom = &zone->uz_domain[i]; 216120a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 216220a4e154SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, ""); 216320a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 216420a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 216520a4e154SJeff Roberson "number of items in this domain"); 216620a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 216720a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 216820a4e154SJeff Roberson "maximum item count in this period"); 216920a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 217020a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 217120a4e154SJeff Roberson "minimum item count in this period"); 217220a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 217320a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 217420a4e154SJeff Roberson "Working set size"); 217520a4e154SJeff Roberson } 217620a4e154SJeff Roberson 217720a4e154SJeff Roberson /* 217820a4e154SJeff Roberson * General statistics. 217920a4e154SJeff Roberson */ 218020a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 218120a4e154SJeff Roberson "stats", CTLFLAG_RD, NULL, ""); 218220a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 218320a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 218420a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 218520a4e154SJeff Roberson "Current number of allocated items"); 218620a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 218720a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 218820a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 218920a4e154SJeff Roberson "Total allocation calls"); 219020a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 219120a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 219220a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 219320a4e154SJeff Roberson "Total free calls"); 219420a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 219520a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 219620a4e154SJeff Roberson "Number of allocation failures"); 219720a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 219820a4e154SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 0, 219920a4e154SJeff Roberson "Free calls from the wrong domain"); 220020a4e154SJeff Roberson } 220120a4e154SJeff Roberson 220220a4e154SJeff Roberson struct uma_zone_count { 220320a4e154SJeff Roberson const char *name; 220420a4e154SJeff Roberson int count; 220520a4e154SJeff Roberson }; 220620a4e154SJeff Roberson 220720a4e154SJeff Roberson static void 220820a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 220920a4e154SJeff Roberson { 221020a4e154SJeff Roberson struct uma_zone_count *cnt; 221120a4e154SJeff Roberson 221220a4e154SJeff Roberson cnt = arg; 22133b490537SJeff Roberson /* 22143b490537SJeff Roberson * Some zones are rapidly created with identical names and 22153b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 22163b490537SJeff Roberson * Use one greater than the maximum observed for this name. 22173b490537SJeff Roberson */ 221820a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 22193b490537SJeff Roberson cnt->count = MAX(cnt->count, 22203b490537SJeff Roberson zone->uz_namecnt + 1); 222120a4e154SJeff Roberson } 222220a4e154SJeff Roberson 2223cc7ce83aSJeff Roberson static void 2224cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2225cc7ce83aSJeff Roberson { 2226cc7ce83aSJeff Roberson int i; 2227cc7ce83aSJeff Roberson 2228cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2229cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2230cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2231cc7ce83aSJeff Roberson } 2232cc7ce83aSJeff Roberson } 2233cc7ce83aSJeff Roberson 2234099a0e58SBosko Milekic /* 2235099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2236099a0e58SBosko Milekic * 2237099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2238099a0e58SBosko Milekic * udata Actually uma_zctor_args 22398355f576SJeff Roberson */ 2240b23f72e9SBrian Feldman static int 2241b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 22428355f576SJeff Roberson { 224320a4e154SJeff Roberson struct uma_zone_count cnt; 22448355f576SJeff Roberson struct uma_zctor_args *arg = udata; 22458355f576SJeff Roberson uma_zone_t zone = mem; 2246099a0e58SBosko Milekic uma_zone_t z; 2247099a0e58SBosko Milekic uma_keg_t keg; 224808cfa56eSMark Johnston int i; 22498355f576SJeff Roberson 22508355f576SJeff Roberson bzero(zone, size); 22518355f576SJeff Roberson zone->uz_name = arg->name; 22528355f576SJeff Roberson zone->uz_ctor = arg->ctor; 22538355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2254099a0e58SBosko Milekic zone->uz_init = NULL; 2255099a0e58SBosko Milekic zone->uz_fini = NULL; 2256bf965959SSean Bruno zone->uz_sleeps = 0; 2257c1685086SJeff Roberson zone->uz_xdomain = 0; 225820a4e154SJeff Roberson zone->uz_bucket_size = 0; 225920a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 226020a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2261e20a199fSJeff Roberson zone->uz_flags = 0; 22622f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2263ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 2264ab3185d1SJeff Roberson zone->uz_domain = (struct uma_zone_domain *)&zone->uz_cpu[mp_ncpus]; 2265bb15d1c7SGleb Smirnoff zone->uz_bkt_max = ULONG_MAX; 22662f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2267af526374SJeff Roberson 226820a4e154SJeff Roberson /* Count the number of duplicate names. */ 226920a4e154SJeff Roberson cnt.name = arg->name; 227020a4e154SJeff Roberson cnt.count = 0; 227120a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 227220a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 2273727c6918SJeff Roberson ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 2274*91d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 22752efcc8cbSGleb Smirnoff 227608cfa56eSMark Johnston for (i = 0; i < vm_ndomains; i++) 227708cfa56eSMark Johnston TAILQ_INIT(&zone->uz_domain[i].uzd_buckets); 227808cfa56eSMark Johnston 2279ca293436SRyan Libby #ifdef INVARIANTS 2280ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2281cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 2282ca293436SRyan Libby #endif 2283ca293436SRyan Libby 22840095a784SJeff Roberson /* 22850095a784SJeff Roberson * This is a pure cache zone, no kegs. 22860095a784SJeff Roberson */ 22870095a784SJeff Roberson if (arg->import) { 2288727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2289727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 22906fd34d6fSJeff Roberson if (arg->flags & UMA_ZONE_VM) 22916fd34d6fSJeff Roberson arg->flags |= UMA_ZFLAG_CACHEONLY; 22926fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2293af526374SJeff Roberson zone->uz_size = arg->size; 22940095a784SJeff Roberson zone->uz_import = arg->import; 22950095a784SJeff Roberson zone->uz_release = arg->release; 22960095a784SJeff Roberson zone->uz_arg = arg->arg; 2297111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 229803175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2299111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2300af526374SJeff Roberson goto out; 23010095a784SJeff Roberson } 23020095a784SJeff Roberson 23030095a784SJeff Roberson /* 23040095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 23050095a784SJeff Roberson */ 2306b75c4efcSAndrew Turner zone->uz_import = zone_import; 2307b75c4efcSAndrew Turner zone->uz_release = zone_release; 23080095a784SJeff Roberson zone->uz_arg = zone; 2309bb15d1c7SGleb Smirnoff keg = arg->keg; 23100095a784SJeff Roberson 2311099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 231220a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 231320a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2314099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 23158355f576SJeff Roberson zone->uz_init = arg->uminit; 2316e221e841SJeff Roberson zone->uz_fini = arg->fini; 2317e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2318111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2319099a0e58SBosko Milekic ZONE_LOCK(zone); 2320099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2321099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2322099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2323099a0e58SBosko Milekic break; 2324099a0e58SBosko Milekic } 2325099a0e58SBosko Milekic } 2326099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2327111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2328e20a199fSJeff Roberson } else if (keg == NULL) { 2329e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2330e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2331b23f72e9SBrian Feldman return (ENOMEM); 2332099a0e58SBosko Milekic } else { 2333099a0e58SBosko Milekic struct uma_kctor_args karg; 2334b23f72e9SBrian Feldman int error; 2335099a0e58SBosko Milekic 2336099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2337099a0e58SBosko Milekic karg.size = arg->size; 2338099a0e58SBosko Milekic karg.uminit = arg->uminit; 2339099a0e58SBosko Milekic karg.fini = arg->fini; 2340099a0e58SBosko Milekic karg.align = arg->align; 2341099a0e58SBosko Milekic karg.flags = arg->flags; 2342099a0e58SBosko Milekic karg.zone = zone; 2343b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2344b23f72e9SBrian Feldman flags); 2345b23f72e9SBrian Feldman if (error) 2346b23f72e9SBrian Feldman return (error); 2347099a0e58SBosko Milekic } 23480095a784SJeff Roberson 234920a4e154SJeff Roberson /* Inherit properties from the keg. */ 2350bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2351e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2352e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2353e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 23548355f576SJeff Roberson 235520a4e154SJeff Roberson out: 235620a4e154SJeff Roberson if (__predict_true(booted == BOOT_RUNNING)) { 235720a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 235820a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 235920a4e154SJeff Roberson } else { 236020a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 236120a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 236220a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2363099a0e58SBosko Milekic } 23648355f576SJeff Roberson 23657e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 23667e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 23677e28037aSMark Johnston ("Invalid zone flag combination")); 236820a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 236920a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 237020a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 237120a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 237220a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) 237320a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN; 237420a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 237520a4e154SJeff Roberson zone->uz_bucket_size = 0; 23767e28037aSMark Johnston else 237720a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 237820a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2379cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2380cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2381cc7ce83aSJeff Roberson zone_update_caches(zone); 2382fc03d22bSJeff Roberson 2383b23f72e9SBrian Feldman return (0); 23848355f576SJeff Roberson } 23858355f576SJeff Roberson 23868355f576SJeff Roberson /* 2387099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2388099a0e58SBosko Milekic * table and removes the keg from the global list. 23899c2cd7e5SJeff Roberson * 23909c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 23919c2cd7e5SJeff Roberson * udata unused 23929c2cd7e5SJeff Roberson */ 2393099a0e58SBosko Milekic static void 2394099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2395099a0e58SBosko Milekic { 2396099a0e58SBosko Milekic uma_keg_t keg; 23978b987a77SJeff Roberson uint32_t free, pages; 23988b987a77SJeff Roberson int i; 23999c2cd7e5SJeff Roberson 2400099a0e58SBosko Milekic keg = (uma_keg_t)arg; 24018b987a77SJeff Roberson free = pages = 0; 24028b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 24038b987a77SJeff Roberson free += keg->uk_domain[i].ud_free; 24048b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 24058b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2406099a0e58SBosko Milekic } 24078b987a77SJeff Roberson if (free != 0) 24088b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 24098b987a77SJeff Roberson " Lost %u pages of memory.\n", 24108b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 24118b987a77SJeff Roberson free, pages); 2412099a0e58SBosko Milekic 2413099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2414099a0e58SBosko Milekic } 2415099a0e58SBosko Milekic 2416099a0e58SBosko Milekic /* 2417099a0e58SBosko Milekic * Zone header dtor. 2418099a0e58SBosko Milekic * 2419099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2420099a0e58SBosko Milekic * udata unused 2421099a0e58SBosko Milekic */ 24229c2cd7e5SJeff Roberson static void 24239c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 24249c2cd7e5SJeff Roberson { 24259c2cd7e5SJeff Roberson uma_zone_t zone; 2426099a0e58SBosko Milekic uma_keg_t keg; 24279c2cd7e5SJeff Roberson 24289c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 24299643769aSJeff Roberson 243020a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 243120a4e154SJeff Roberson 2432e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 24339643769aSJeff Roberson cache_drain(zone); 2434099a0e58SBosko Milekic 2435111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2436099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2437111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2438099a0e58SBosko Milekic /* 2439099a0e58SBosko Milekic * XXX there are some races here where 2440099a0e58SBosko Milekic * the zone can be drained but zone lock 2441099a0e58SBosko Milekic * released and then refilled before we 2442099a0e58SBosko Milekic * remove it... we dont care for now 2443099a0e58SBosko Milekic */ 244408cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2445e20a199fSJeff Roberson /* 2446323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2447e20a199fSJeff Roberson */ 2448323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2449323ad386STycho Nightingale keg = zone->uz_keg; 2450111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2451099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2452111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 24530095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 24549c2cd7e5SJeff Roberson } 24552efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 24562efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 24572efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 245820a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2459af526374SJeff Roberson ZONE_LOCK_FINI(zone); 2460*91d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 2461099a0e58SBosko Milekic } 2462099a0e58SBosko Milekic 24639c2cd7e5SJeff Roberson /* 24648355f576SJeff Roberson * Traverses every zone in the system and calls a callback 24658355f576SJeff Roberson * 24668355f576SJeff Roberson * Arguments: 24678355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 24688355f576SJeff Roberson * as an argument. 24698355f576SJeff Roberson * 24708355f576SJeff Roberson * Returns: 24718355f576SJeff Roberson * Nothing 24728355f576SJeff Roberson */ 24738355f576SJeff Roberson static void 247420a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 24758355f576SJeff Roberson { 2476099a0e58SBosko Milekic uma_keg_t keg; 24778355f576SJeff Roberson uma_zone_t zone; 24788355f576SJeff Roberson 24792efcc8cbSGleb Smirnoff /* 24802efcc8cbSGleb Smirnoff * Before BOOT_RUNNING we are guaranteed to be single 24812efcc8cbSGleb Smirnoff * threaded, so locking isn't needed. Startup functions 24822efcc8cbSGleb Smirnoff * are allowed to use M_WAITOK. 24832efcc8cbSGleb Smirnoff */ 24842efcc8cbSGleb Smirnoff if (__predict_true(booted == BOOT_RUNNING)) 2485111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2486099a0e58SBosko Milekic LIST_FOREACH(keg, &uma_kegs, uk_link) { 2487099a0e58SBosko Milekic LIST_FOREACH(zone, &keg->uk_zones, uz_link) 248820a4e154SJeff Roberson zfunc(zone, arg); 2489099a0e58SBosko Milekic } 249008034d10SKonstantin Belousov LIST_FOREACH(zone, &uma_cachezones, uz_link) 249120a4e154SJeff Roberson zfunc(zone, arg); 24922efcc8cbSGleb Smirnoff if (__predict_true(booted == BOOT_RUNNING)) 2493111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 24948355f576SJeff Roberson } 24958355f576SJeff Roberson 2496f4bef67cSGleb Smirnoff /* 2497f4bef67cSGleb Smirnoff * Count how many pages do we need to bootstrap. VM supplies 2498f4bef67cSGleb Smirnoff * its need in early zones in the argument, we add up our zones, 2499325c4cedSMark Johnston * which consist of the UMA Slabs, UMA Hash and 9 Bucket zones. The 2500f4bef67cSGleb Smirnoff * zone of zones and zone of kegs are accounted separately. 2501f4bef67cSGleb Smirnoff */ 2502325c4cedSMark Johnston #define UMA_BOOT_ZONES 11 25035073a083SGleb Smirnoff /* Zone of zones and zone of kegs have arbitrary alignment. */ 25045073a083SGleb Smirnoff #define UMA_BOOT_ALIGN 32 2505f4bef67cSGleb Smirnoff static int zsize, ksize; 2506f4bef67cSGleb Smirnoff int 2507f7d35785SGleb Smirnoff uma_startup_count(int vm_zones) 2508f4bef67cSGleb Smirnoff { 2509f7d35785SGleb Smirnoff int zones, pages; 25109b78b1f4SJeff Roberson size_t space, size; 2511f4bef67cSGleb Smirnoff 2512f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2513f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 2514f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2515f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2516f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 2517f4bef67cSGleb Smirnoff 25185073a083SGleb Smirnoff /* 25195073a083SGleb Smirnoff * Memory for the zone of kegs and its keg, 25205073a083SGleb Smirnoff * and for zone of zones. 25215073a083SGleb Smirnoff */ 2522f4bef67cSGleb Smirnoff pages = howmany(roundup(zsize, CACHE_LINE_SIZE) * 2 + 2523f4bef67cSGleb Smirnoff roundup(ksize, CACHE_LINE_SIZE), PAGE_SIZE); 2524f4bef67cSGleb Smirnoff 2525f7d35785SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2526f7d35785SGleb Smirnoff zones = UMA_BOOT_ZONES; 2527f7d35785SGleb Smirnoff #else 2528f7d35785SGleb Smirnoff zones = UMA_BOOT_ZONES + vm_zones; 2529f7d35785SGleb Smirnoff vm_zones = 0; 2530f7d35785SGleb Smirnoff #endif 25319b78b1f4SJeff Roberson size = slab_sizeof(SLAB_MAX_SETSIZE); 25329b78b1f4SJeff Roberson space = slab_space(SLAB_MAX_SETSIZE); 2533f4bef67cSGleb Smirnoff 25345073a083SGleb Smirnoff /* Memory for the rest of startup zones, UMA and VM, ... */ 25359b78b1f4SJeff Roberson if (zsize > space) { 25360b2e3aeaSGleb Smirnoff /* See keg_large_init(). */ 25370b2e3aeaSGleb Smirnoff u_int ppera; 25380b2e3aeaSGleb Smirnoff 25390b2e3aeaSGleb Smirnoff ppera = howmany(roundup2(zsize, UMA_BOOT_ALIGN), PAGE_SIZE); 25409b78b1f4SJeff Roberson if (PAGE_SIZE * ppera - roundup2(zsize, UMA_BOOT_ALIGN) < size) 25410b2e3aeaSGleb Smirnoff ppera++; 25420b2e3aeaSGleb Smirnoff pages += (zones + vm_zones) * ppera; 25439b78b1f4SJeff Roberson } else if (roundup2(zsize, UMA_BOOT_ALIGN) > space) 25440b2e3aeaSGleb Smirnoff /* See keg_small_init() special case for uk_ppera = 1. */ 254596a10340SGleb Smirnoff pages += zones; 2546f4bef67cSGleb Smirnoff else 25475073a083SGleb Smirnoff pages += howmany(zones, 25489b78b1f4SJeff Roberson space / roundup2(zsize, UMA_BOOT_ALIGN)); 2549f4bef67cSGleb Smirnoff 25505073a083SGleb Smirnoff /* ... and their kegs. Note that zone of zones allocates a keg! */ 25515073a083SGleb Smirnoff pages += howmany(zones + 1, 25529b78b1f4SJeff Roberson space / roundup2(ksize, UMA_BOOT_ALIGN)); 2553f4bef67cSGleb Smirnoff 2554f4bef67cSGleb Smirnoff return (pages); 2555f4bef67cSGleb Smirnoff } 2556f4bef67cSGleb Smirnoff 25578355f576SJeff Roberson void 2558ac0a6fd0SGleb Smirnoff uma_startup(void *mem, int npages) 25598355f576SJeff Roberson { 25608355f576SJeff Roberson struct uma_zctor_args args; 2561ab3185d1SJeff Roberson uma_keg_t masterkeg; 2562ab3185d1SJeff Roberson uintptr_t m; 2563f4bef67cSGleb Smirnoff 2564f4bef67cSGleb Smirnoff #ifdef DIAGNOSTIC 2565f4bef67cSGleb Smirnoff printf("Entering %s with %d boot pages configured\n", __func__, npages); 2566f4bef67cSGleb Smirnoff #endif 25678355f576SJeff Roberson 2568111fbcd5SBryan Venteicher rw_init(&uma_rwlock, "UMA lock"); 2569099a0e58SBosko Milekic 2570ab3185d1SJeff Roberson /* Use bootpages memory for the zone of zones and zone of kegs. */ 2571ab3185d1SJeff Roberson m = (uintptr_t)mem; 2572ab3185d1SJeff Roberson zones = (uma_zone_t)m; 2573ab3185d1SJeff Roberson m += roundup(zsize, CACHE_LINE_SIZE); 2574ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 2575ab3185d1SJeff Roberson m += roundup(zsize, CACHE_LINE_SIZE); 2576ab3185d1SJeff Roberson masterkeg = (uma_keg_t)m; 2577ab3185d1SJeff Roberson m += roundup(ksize, CACHE_LINE_SIZE); 2578ab3185d1SJeff Roberson m = roundup(m, PAGE_SIZE); 2579ab3185d1SJeff Roberson npages -= (m - (uintptr_t)mem) / PAGE_SIZE; 2580ab3185d1SJeff Roberson mem = (void *)m; 2581ab3185d1SJeff Roberson 2582099a0e58SBosko Milekic /* "manually" create the initial zone */ 25830095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2584099a0e58SBosko Milekic args.name = "UMA Kegs"; 2585ab3185d1SJeff Roberson args.size = ksize; 2586099a0e58SBosko Milekic args.ctor = keg_ctor; 2587099a0e58SBosko Milekic args.dtor = keg_dtor; 25888355f576SJeff Roberson args.uminit = zero_init; 25898355f576SJeff Roberson args.fini = NULL; 2590ab3185d1SJeff Roberson args.keg = masterkeg; 25915073a083SGleb Smirnoff args.align = UMA_BOOT_ALIGN - 1; 2592b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2593ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 25948355f576SJeff Roberson 2595ac0a6fd0SGleb Smirnoff bootmem = mem; 2596ac0a6fd0SGleb Smirnoff boot_pages = npages; 25978355f576SJeff Roberson 2598099a0e58SBosko Milekic args.name = "UMA Zones"; 2599f4bef67cSGleb Smirnoff args.size = zsize; 2600099a0e58SBosko Milekic args.ctor = zone_ctor; 2601099a0e58SBosko Milekic args.dtor = zone_dtor; 2602099a0e58SBosko Milekic args.uminit = zero_init; 2603099a0e58SBosko Milekic args.fini = NULL; 2604099a0e58SBosko Milekic args.keg = NULL; 26055073a083SGleb Smirnoff args.align = UMA_BOOT_ALIGN - 1; 2606099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2607ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2608099a0e58SBosko Milekic 26098355f576SJeff Roberson /* Now make a zone for slab headers */ 26101e0701e1SJeff Roberson slabzone = uma_zcreate("UMA Slabs", sizeof(struct uma_hash_slab), 26111e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 26128355f576SJeff Roberson 26138355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 26148355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 26151e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 26168355f576SJeff Roberson 2617f4bef67cSGleb Smirnoff booted = BOOT_STRAPPED; 26188355f576SJeff Roberson } 26198355f576SJeff Roberson 2620f4bef67cSGleb Smirnoff void 2621f4bef67cSGleb Smirnoff uma_startup1(void) 2622f4bef67cSGleb Smirnoff { 2623f4bef67cSGleb Smirnoff 2624f4bef67cSGleb Smirnoff #ifdef DIAGNOSTIC 2625f4bef67cSGleb Smirnoff printf("Entering %s with %d boot pages left\n", __func__, boot_pages); 2626f4bef67cSGleb Smirnoff #endif 2627f4bef67cSGleb Smirnoff booted = BOOT_PAGEALLOC; 2628f4bef67cSGleb Smirnoff } 2629f4bef67cSGleb Smirnoff 26308355f576SJeff Roberson void 263199571dc3SJeff Roberson uma_startup2(void) 26328355f576SJeff Roberson { 2633f4bef67cSGleb Smirnoff 2634f7d35785SGleb Smirnoff #ifdef DIAGNOSTIC 2635f7d35785SGleb Smirnoff printf("Entering %s with %d boot pages left\n", __func__, boot_pages); 2636f7d35785SGleb Smirnoff #endif 263708cfa56eSMark Johnston sx_init(&uma_reclaim_lock, "umareclaim"); 26383182660aSRyan Libby bucket_init(); 26393182660aSRyan Libby booted = BOOT_BUCKETS; 2640f4bef67cSGleb Smirnoff bucket_enable(); 26418355f576SJeff Roberson } 26428355f576SJeff Roberson 26438355f576SJeff Roberson /* 26448355f576SJeff Roberson * Initialize our callout handle 26458355f576SJeff Roberson * 26468355f576SJeff Roberson */ 26478355f576SJeff Roberson static void 26488355f576SJeff Roberson uma_startup3(void) 26498355f576SJeff Roberson { 26501431a748SGleb Smirnoff 2651c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2652c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2653c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2654c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2655c5deaf04SGleb Smirnoff #endif 265620a4e154SJeff Roberson zone_foreach(zone_alloc_counters, NULL); 265720a4e154SJeff Roberson zone_foreach(zone_alloc_sysctl, NULL); 2658fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 26599643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2660c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 26618355f576SJeff Roberson } 26628355f576SJeff Roberson 2663e20a199fSJeff Roberson static uma_keg_t 2664099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 266585dcf349SGleb Smirnoff int align, uint32_t flags) 2666099a0e58SBosko Milekic { 2667099a0e58SBosko Milekic struct uma_kctor_args args; 2668099a0e58SBosko Milekic 2669099a0e58SBosko Milekic args.size = size; 2670099a0e58SBosko Milekic args.uminit = uminit; 2671099a0e58SBosko Milekic args.fini = fini; 26721e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2673099a0e58SBosko Milekic args.flags = flags; 2674099a0e58SBosko Milekic args.zone = zone; 2675ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 2676099a0e58SBosko Milekic } 2677099a0e58SBosko Milekic 2678f4bef67cSGleb Smirnoff /* Public functions */ 26798355f576SJeff Roberson /* See uma.h */ 26801e319f6dSRobert Watson void 26811e319f6dSRobert Watson uma_set_align(int align) 26821e319f6dSRobert Watson { 26831e319f6dSRobert Watson 26841e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 26851e319f6dSRobert Watson uma_align_cache = align; 26861e319f6dSRobert Watson } 26871e319f6dSRobert Watson 26881e319f6dSRobert Watson /* See uma.h */ 26898355f576SJeff Roberson uma_zone_t 2690bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 269185dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 26928355f576SJeff Roberson 26938355f576SJeff Roberson { 26948355f576SJeff Roberson struct uma_zctor_args args; 269595c4bf75SKonstantin Belousov uma_zone_t res; 269695c4bf75SKonstantin Belousov bool locked; 26978355f576SJeff Roberson 2698a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 2699a5a35578SJohn Baldwin align, name)); 2700a5a35578SJohn Baldwin 27018355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 27020095a784SJeff Roberson memset(&args, 0, sizeof(args)); 27038355f576SJeff Roberson args.name = name; 27048355f576SJeff Roberson args.size = size; 27058355f576SJeff Roberson args.ctor = ctor; 27068355f576SJeff Roberson args.dtor = dtor; 27078355f576SJeff Roberson args.uminit = uminit; 27088355f576SJeff Roberson args.fini = fini; 2709afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 2710afc6dc36SJohn-Mark Gurney /* 2711ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 2712ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 2713ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 2714ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 2715ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 2716ca293436SRyan Libby * the ctor and dtor do not need to be empty. 2717ca293436SRyan Libby * 2718ca293436SRyan Libby * XXX UMA_ZONE_OFFPAGE. 2719afc6dc36SJohn-Mark Gurney */ 272019c591bfSMateusz Guzik if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOFREE))) && 2721ca293436SRyan Libby uminit == NULL && fini == NULL) { 2722afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 2723afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 2724afc6dc36SJohn-Mark Gurney } 2725afc6dc36SJohn-Mark Gurney #endif 27268355f576SJeff Roberson args.align = align; 27278355f576SJeff Roberson args.flags = flags; 2728099a0e58SBosko Milekic args.keg = NULL; 2729099a0e58SBosko Milekic 2730f4bef67cSGleb Smirnoff if (booted < BOOT_BUCKETS) { 273195c4bf75SKonstantin Belousov locked = false; 273295c4bf75SKonstantin Belousov } else { 273308cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 273495c4bf75SKonstantin Belousov locked = true; 273595c4bf75SKonstantin Belousov } 2736ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 273795c4bf75SKonstantin Belousov if (locked) 273808cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 273995c4bf75SKonstantin Belousov return (res); 2740099a0e58SBosko Milekic } 2741099a0e58SBosko Milekic 2742099a0e58SBosko Milekic /* See uma.h */ 2743099a0e58SBosko Milekic uma_zone_t 2744099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 2745099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 2746099a0e58SBosko Milekic { 2747099a0e58SBosko Milekic struct uma_zctor_args args; 2748e20a199fSJeff Roberson uma_keg_t keg; 274995c4bf75SKonstantin Belousov uma_zone_t res; 275095c4bf75SKonstantin Belousov bool locked; 2751099a0e58SBosko Milekic 2752bb15d1c7SGleb Smirnoff keg = master->uz_keg; 27530095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2754099a0e58SBosko Milekic args.name = name; 2755e20a199fSJeff Roberson args.size = keg->uk_size; 2756099a0e58SBosko Milekic args.ctor = ctor; 2757099a0e58SBosko Milekic args.dtor = dtor; 2758099a0e58SBosko Milekic args.uminit = zinit; 2759099a0e58SBosko Milekic args.fini = zfini; 2760e20a199fSJeff Roberson args.align = keg->uk_align; 2761e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 2762e20a199fSJeff Roberson args.keg = keg; 27638355f576SJeff Roberson 2764f4bef67cSGleb Smirnoff if (booted < BOOT_BUCKETS) { 276595c4bf75SKonstantin Belousov locked = false; 276695c4bf75SKonstantin Belousov } else { 276708cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 276895c4bf75SKonstantin Belousov locked = true; 276995c4bf75SKonstantin Belousov } 2770e20a199fSJeff Roberson /* XXX Attaches only one keg of potentially many. */ 2771ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 277295c4bf75SKonstantin Belousov if (locked) 277308cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 277495c4bf75SKonstantin Belousov return (res); 27758355f576SJeff Roberson } 27768355f576SJeff Roberson 27770095a784SJeff Roberson /* See uma.h */ 27780095a784SJeff Roberson uma_zone_t 2779af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 2780af526374SJeff Roberson uma_init zinit, uma_fini zfini, uma_import zimport, 2781af526374SJeff Roberson uma_release zrelease, void *arg, int flags) 27820095a784SJeff Roberson { 27830095a784SJeff Roberson struct uma_zctor_args args; 27840095a784SJeff Roberson 27850095a784SJeff Roberson memset(&args, 0, sizeof(args)); 27860095a784SJeff Roberson args.name = name; 2787af526374SJeff Roberson args.size = size; 27880095a784SJeff Roberson args.ctor = ctor; 27890095a784SJeff Roberson args.dtor = dtor; 27900095a784SJeff Roberson args.uminit = zinit; 27910095a784SJeff Roberson args.fini = zfini; 27920095a784SJeff Roberson args.import = zimport; 27930095a784SJeff Roberson args.release = zrelease; 27940095a784SJeff Roberson args.arg = arg; 27950095a784SJeff Roberson args.align = 0; 2796bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 27970095a784SJeff Roberson 2798ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 27990095a784SJeff Roberson } 28000095a784SJeff Roberson 28018355f576SJeff Roberson /* See uma.h */ 28029c2cd7e5SJeff Roberson void 28039c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 28049c2cd7e5SJeff Roberson { 2805f4ff923bSRobert Watson 280608cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 28070095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 280808cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 28099c2cd7e5SJeff Roberson } 28109c2cd7e5SJeff Roberson 28118d6fbbb8SJeff Roberson void 28128d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 28138d6fbbb8SJeff Roberson { 28148d6fbbb8SJeff Roberson void *item; 28158d6fbbb8SJeff Roberson 28168d6fbbb8SJeff Roberson item = uma_zalloc_arg(zone, NULL, M_WAITOK); 28178d6fbbb8SJeff Roberson uma_zfree(zone, item); 28188d6fbbb8SJeff Roberson } 28198d6fbbb8SJeff Roberson 28204e180881SMateusz Guzik void * 28214e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 28224e180881SMateusz Guzik { 28234e180881SMateusz Guzik void *item; 2824b4799947SRuslan Bukin #ifdef SMP 28254e180881SMateusz Guzik int i; 28264e180881SMateusz Guzik 28274e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2828b4799947SRuslan Bukin #endif 28294e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 28304e180881SMateusz Guzik if (item != NULL && (flags & M_ZERO)) { 2831b4799947SRuslan Bukin #ifdef SMP 2832013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 28334e180881SMateusz Guzik bzero(zpcpu_get_cpu(item, i), zone->uz_size); 2834b4799947SRuslan Bukin #else 2835b4799947SRuslan Bukin bzero(item, zone->uz_size); 2836b4799947SRuslan Bukin #endif 28374e180881SMateusz Guzik } 28384e180881SMateusz Guzik return (item); 28394e180881SMateusz Guzik } 28404e180881SMateusz Guzik 28414e180881SMateusz Guzik /* 28424e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 28434e180881SMateusz Guzik */ 28444e180881SMateusz Guzik void 28454e180881SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata) 28464e180881SMateusz Guzik { 28474e180881SMateusz Guzik 2848c5b7751fSIan Lepore #ifdef SMP 28494e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2850c5b7751fSIan Lepore #endif 28514e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 28524e180881SMateusz Guzik } 28534e180881SMateusz Guzik 2854cc7ce83aSJeff Roberson #ifdef INVARIANTS 2855cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 1 2856cc7ce83aSJeff Roberson #else 2857cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 0 2858cc7ce83aSJeff Roberson #endif 2859cc7ce83aSJeff Roberson 2860beb8beefSJeff Roberson static void * 2861cc7ce83aSJeff Roberson item_ctor(uma_zone_t zone, int size, void *udata, int flags, void *item) 2862beb8beefSJeff Roberson { 2863beb8beefSJeff Roberson #ifdef INVARIANTS 2864ca293436SRyan Libby bool skipdbg; 2865beb8beefSJeff Roberson 2866beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 2867ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2868ca293436SRyan Libby zone->uz_ctor != trash_ctor) 2869cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 2870beb8beefSJeff Roberson #endif 2871ca293436SRyan Libby if (__predict_false(zone->uz_ctor != NULL) && 2872cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 2873beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 2874beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 2875beb8beefSJeff Roberson return (NULL); 2876beb8beefSJeff Roberson } 2877beb8beefSJeff Roberson #ifdef INVARIANTS 2878beb8beefSJeff Roberson if (!skipdbg) 2879beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 2880beb8beefSJeff Roberson #endif 2881beb8beefSJeff Roberson if (flags & M_ZERO) 2882cc7ce83aSJeff Roberson bzero(item, size); 2883beb8beefSJeff Roberson 2884beb8beefSJeff Roberson return (item); 2885beb8beefSJeff Roberson } 2886beb8beefSJeff Roberson 2887ca293436SRyan Libby static inline void 2888cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 2889cc7ce83aSJeff Roberson enum zfreeskip skip) 2890ca293436SRyan Libby { 2891ca293436SRyan Libby #ifdef INVARIANTS 2892ca293436SRyan Libby bool skipdbg; 2893ca293436SRyan Libby 2894ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 2895ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 2896ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 2897ca293436SRyan Libby uma_dbg_free(zone, udata, item); 2898ca293436SRyan Libby else 2899ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 2900ca293436SRyan Libby } 2901ca293436SRyan Libby #endif 2902cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 2903ca293436SRyan Libby if (zone->uz_dtor != NULL) 2904cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 2905ca293436SRyan Libby #ifdef INVARIANTS 2906ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2907ca293436SRyan Libby zone->uz_dtor != trash_dtor) 2908cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 2909ca293436SRyan Libby #endif 2910ca293436SRyan Libby } 2911ca293436SRyan Libby } 2912ca293436SRyan Libby 29139c2cd7e5SJeff Roberson /* See uma.h */ 29148355f576SJeff Roberson void * 29152cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 29168355f576SJeff Roberson { 2917376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 2918ab3185d1SJeff Roberson uma_cache_t cache; 2919ab3185d1SJeff Roberson void *item; 2920cc7ce83aSJeff Roberson int domain, size, uz_flags; 29218355f576SJeff Roberson 2922e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 292319fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 292410cb2424SMark Murray 29258355f576SJeff Roberson /* This is the fast path allocation */ 29261431a748SGleb Smirnoff CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d", 29271431a748SGleb Smirnoff curthread, zone->uz_name, zone, flags); 2928a553d4b8SJeff Roberson 2929cc7ce83aSJeff Roberson #ifdef WITNESS 2930635fd505SRobert Watson if (flags & M_WAITOK) { 2931b23f72e9SBrian Feldman WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2932635fd505SRobert Watson "uma_zalloc_arg: zone \"%s\"", zone->uz_name); 29334c1cc01cSJohn Baldwin } 2934cc7ce83aSJeff Roberson #endif 2935cc7ce83aSJeff Roberson 2936cc7ce83aSJeff Roberson #ifdef INVARIANTS 29370766f278SJonathan T. Looney KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC")); 2938d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 29391067a2baSJonathan T. Looney ("uma_zalloc_arg: called with spinlock or critical section held")); 2940ea99223eSMateusz Guzik if (zone->uz_flags & UMA_ZONE_PCPU) 2941b8af2820SMateusz Guzik KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone " 2942b8af2820SMateusz Guzik "with M_ZERO passed")); 2943cc7ce83aSJeff Roberson #endif 29441067a2baSJonathan T. Looney 29458d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 29468d689e04SGleb Smirnoff if (memguard_cmp_zone(zone)) { 29478d689e04SGleb Smirnoff item = memguard_alloc(zone->uz_size, flags); 29488d689e04SGleb Smirnoff if (item != NULL) { 29498d689e04SGleb Smirnoff if (zone->uz_init != NULL && 29508d689e04SGleb Smirnoff zone->uz_init(item, zone->uz_size, flags) != 0) 29518d689e04SGleb Smirnoff return (NULL); 29528d689e04SGleb Smirnoff if (zone->uz_ctor != NULL && 2953fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 2954fc03d22bSJeff Roberson flags) != 0) { 2955ca293436SRyan Libby counter_u64_add(zone->uz_fails, 1); 29568d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 29578d689e04SGleb Smirnoff return (NULL); 29588d689e04SGleb Smirnoff } 29598d689e04SGleb Smirnoff return (item); 29608d689e04SGleb Smirnoff } 29618d689e04SGleb Smirnoff /* This is unfortunate but should not be fatal. */ 29628d689e04SGleb Smirnoff } 29638d689e04SGleb Smirnoff #endif 29645d1ae027SRobert Watson /* 29655d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 29665d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 29675d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 29685d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 29695d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 29705d1ae027SRobert Watson * preemption and migration. We release the critical section in 29715d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 29725d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 29735d1ae027SRobert Watson * must detect and handle migration if it has occurred. 29745d1ae027SRobert Watson */ 29755d1ae027SRobert Watson critical_enter(); 2976beb8beefSJeff Roberson do { 2977cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 2978376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 2979cc7ce83aSJeff Roberson size = cache_uz_size(cache); 2980cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 2981376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 2982376b1ba3SJeff Roberson item = cache_bucket_pop(cache, bucket); 29835d1ae027SRobert Watson critical_exit(); 2984cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 2985cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 2986cc7ce83aSJeff Roberson return (item_ctor(zone, size, udata, flags, item)); 2987cc7ce83aSJeff Roberson if (flags & M_ZERO) 2988cc7ce83aSJeff Roberson bzero(item, size); 2989cc7ce83aSJeff Roberson return (item); 2990b23f72e9SBrian Feldman } 2991beb8beefSJeff Roberson } while (cache_alloc(zone, cache, udata, flags)); 2992beb8beefSJeff Roberson critical_exit(); 2993beb8beefSJeff Roberson 2994beb8beefSJeff Roberson /* 2995beb8beefSJeff Roberson * We can not get a bucket so try to return a single item. 2996beb8beefSJeff Roberson */ 2997cc7ce83aSJeff Roberson if (uz_flags & UMA_ZONE_NUMA) 2998beb8beefSJeff Roberson domain = PCPU_GET(domain); 2999beb8beefSJeff Roberson else 3000beb8beefSJeff Roberson domain = UMA_ANYDOMAIN; 30014bd61e19SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3002fc03d22bSJeff Roberson } 3003fc03d22bSJeff Roberson 30048355f576SJeff Roberson /* 3005beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 3006beb8beefSJeff Roberson * a critical section. Returns in a critical section. 3007beb8beefSJeff Roberson * 30084bd61e19SJeff Roberson * A false return value indicates an allocation failure. 30094bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 3010beb8beefSJeff Roberson */ 3011beb8beefSJeff Roberson static __noinline bool 3012beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3013beb8beefSJeff Roberson { 3014beb8beefSJeff Roberson uma_zone_domain_t zdom; 3015beb8beefSJeff Roberson uma_bucket_t bucket; 3016cc7ce83aSJeff Roberson int domain; 3017beb8beefSJeff Roberson bool lockfail; 3018beb8beefSJeff Roberson 3019beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3020beb8beefSJeff Roberson 3021beb8beefSJeff Roberson /* 3022beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3023beb8beefSJeff Roberson * if we can switch with the free bucket. 30248355f576SJeff Roberson */ 3025376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_cnt != 0) { 3026376b1ba3SJeff Roberson cache_bucket_swap(&cache->uc_freebucket, &cache->uc_allocbucket); 3027beb8beefSJeff Roberson return (true); 30288355f576SJeff Roberson } 3029fc03d22bSJeff Roberson 3030fc03d22bSJeff Roberson /* 3031fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3032fc03d22bSJeff Roberson */ 3033376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3034fc03d22bSJeff Roberson critical_exit(); 3035fc03d22bSJeff Roberson if (bucket != NULL) 30366fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3037fc03d22bSJeff Roberson 30384bd61e19SJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 30394bd61e19SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) { 30404bd61e19SJeff Roberson critical_enter(); 30414bd61e19SJeff Roberson return (false); 30424bd61e19SJeff Roberson } 30434bd61e19SJeff Roberson 30445d1ae027SRobert Watson /* 30455d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 30465d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 30475d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 30485d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 30495d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 30505d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 30515d1ae027SRobert Watson * the critical section. 30525d1ae027SRobert Watson */ 3053fc03d22bSJeff Roberson lockfail = 0; 3054fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 3055fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 3056a553d4b8SJeff Roberson ZONE_LOCK(zone); 3057fc03d22bSJeff Roberson lockfail = 1; 3058fc03d22bSJeff Roberson } 3059beb8beefSJeff Roberson 3060fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 30614bd61e19SJeff Roberson critical_enter(); 30624bd61e19SJeff Roberson cache = &zone->uz_cpu[curcpu]; 3063376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket != NULL) { 3064fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 3065beb8beefSJeff Roberson return (true); 3066a553d4b8SJeff Roberson } 30678355f576SJeff Roberson 3068fc03d22bSJeff Roberson /* 3069fc03d22bSJeff Roberson * Check the zone's cache of buckets. 3070fc03d22bSJeff Roberson */ 3071c1685086SJeff Roberson if (zone->uz_flags & UMA_ZONE_NUMA) { 3072c1685086SJeff Roberson domain = PCPU_GET(domain); 3073ab3185d1SJeff Roberson zdom = &zone->uz_domain[domain]; 3074c1685086SJeff Roberson } else { 3075c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3076c1685086SJeff Roberson zdom = &zone->uz_domain[0]; 3077c1685086SJeff Roberson } 3078c1685086SJeff Roberson 307908cfa56eSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom)) != NULL) { 3080beb8beefSJeff Roberson ZONE_UNLOCK(zone); 3081cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 3082a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 3083376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3084beb8beefSJeff Roberson return (true); 3085a553d4b8SJeff Roberson } 30865d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 30875d1ae027SRobert Watson critical_exit(); 3088bbee39c6SJeff Roberson 3089fc03d22bSJeff Roberson /* 3090fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 3091fc03d22bSJeff Roberson * handle the working set. 3092fc03d22bSJeff Roberson */ 309320a4e154SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 309420a4e154SJeff Roberson zone->uz_bucket_size++; 30954bd61e19SJeff Roberson ZONE_UNLOCK(zone); 3096bb15d1c7SGleb Smirnoff 30978355f576SJeff Roberson /* 3098beb8beefSJeff Roberson * Fill a bucket and attempt to use it as the alloc bucket. 3099bbee39c6SJeff Roberson */ 3100beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 31011431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 31021431a748SGleb Smirnoff zone->uz_name, zone, bucket); 31034bd61e19SJeff Roberson if (bucket == NULL) { 3104fc03d22bSJeff Roberson critical_enter(); 3105beb8beefSJeff Roberson return (false); 31064bd61e19SJeff Roberson } 31070f9b7bf3SMark Johnston 3108fc03d22bSJeff Roberson /* 3109fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3110fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3111fc03d22bSJeff Roberson * the memory directly. 3112fc03d22bSJeff Roberson */ 31134bd61e19SJeff Roberson ZONE_LOCK(zone); 31144bd61e19SJeff Roberson critical_enter(); 3115cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3116376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 311781c0d72cSGleb Smirnoff ((zone->uz_flags & UMA_ZONE_NUMA) == 0 || 311881c0d72cSGleb Smirnoff domain == PCPU_GET(domain))) { 3119376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 31200f9b7bf3SMark Johnston zdom->uzd_imax += bucket->ub_cnt; 3121bb15d1c7SGleb Smirnoff } else if (zone->uz_bkt_count >= zone->uz_bkt_max) { 312281c0d72cSGleb Smirnoff critical_exit(); 312381c0d72cSGleb Smirnoff ZONE_UNLOCK(zone); 312481c0d72cSGleb Smirnoff bucket_drain(zone, bucket); 312581c0d72cSGleb Smirnoff bucket_free(zone, bucket, udata); 3126beb8beefSJeff Roberson critical_enter(); 3127beb8beefSJeff Roberson return (true); 312881c0d72cSGleb Smirnoff } else 31290f9b7bf3SMark Johnston zone_put_bucket(zone, zdom, bucket, false); 3130bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 3131beb8beefSJeff Roberson return (true); 3132bbee39c6SJeff Roberson } 3133bbee39c6SJeff Roberson 3134ab3185d1SJeff Roberson void * 3135ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3136bbee39c6SJeff Roberson { 3137ab3185d1SJeff Roberson 3138ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 313919fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3140ab3185d1SJeff Roberson 3141ab3185d1SJeff Roberson /* This is the fast path allocation */ 3142ab3185d1SJeff Roberson CTR5(KTR_UMA, 3143ab3185d1SJeff Roberson "uma_zalloc_domain thread %x zone %s(%p) domain %d flags %d", 3144ab3185d1SJeff Roberson curthread, zone->uz_name, zone, domain, flags); 3145ab3185d1SJeff Roberson 3146ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3147ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3148ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3149ab3185d1SJeff Roberson } 3150ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3151ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 3152ab3185d1SJeff Roberson 3153ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3154ab3185d1SJeff Roberson } 3155ab3185d1SJeff Roberson 3156ab3185d1SJeff Roberson /* 3157ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3158ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3159ab3185d1SJeff Roberson * 3160ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3161ab3185d1SJeff Roberson * only 'domain'. 3162ab3185d1SJeff Roberson */ 3163ab3185d1SJeff Roberson static uma_slab_t 3164194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3165ab3185d1SJeff Roberson { 3166ab3185d1SJeff Roberson uma_domain_t dom; 3167bbee39c6SJeff Roberson uma_slab_t slab; 3168ab3185d1SJeff Roberson int start; 3169ab3185d1SJeff Roberson 3170ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3171ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 31728b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3173ab3185d1SJeff Roberson 3174ab3185d1SJeff Roberson slab = NULL; 3175ab3185d1SJeff Roberson start = domain; 3176ab3185d1SJeff Roberson do { 3177ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 3178ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_part_slab)) 3179ab3185d1SJeff Roberson return (LIST_FIRST(&dom->ud_part_slab)); 3180ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_free_slab)) { 3181ab3185d1SJeff Roberson slab = LIST_FIRST(&dom->ud_free_slab); 3182ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 3183ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3184ab3185d1SJeff Roberson return (slab); 3185ab3185d1SJeff Roberson } 3186ab3185d1SJeff Roberson if (rr) 3187ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3188ab3185d1SJeff Roberson } while (domain != start); 3189ab3185d1SJeff Roberson 3190ab3185d1SJeff Roberson return (NULL); 3191ab3185d1SJeff Roberson } 3192ab3185d1SJeff Roberson 31938b987a77SJeff Roberson /* 31948b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 31958b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 31968b987a77SJeff Roberson */ 3197ab3185d1SJeff Roberson static uma_slab_t 3198194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3199ab3185d1SJeff Roberson { 32008b987a77SJeff Roberson uma_slab_t slab; 3201194a979eSMark Johnston uint32_t reserve; 3202099a0e58SBosko Milekic 32038b987a77SJeff Roberson /* HASH has a single free list. */ 32048b987a77SJeff Roberson if ((keg->uk_flags & UMA_ZONE_HASH) != 0) 32058b987a77SJeff Roberson domain = 0; 3206194a979eSMark Johnston 32078b987a77SJeff Roberson KEG_LOCK(keg, domain); 3208194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 32098b987a77SJeff Roberson if (keg->uk_domain[domain].ud_free <= reserve || 32108b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 32118b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3212194a979eSMark Johnston return (NULL); 32138b987a77SJeff Roberson } 32148b987a77SJeff Roberson return (slab); 3215194a979eSMark Johnston } 3216194a979eSMark Johnston 3217194a979eSMark Johnston static uma_slab_t 3218194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3219194a979eSMark Johnston { 3220194a979eSMark Johnston struct vm_domainset_iter di; 3221194a979eSMark Johnston uma_slab_t slab; 3222194a979eSMark Johnston int aflags, domain; 3223194a979eSMark Johnston bool rr; 3224194a979eSMark Johnston 3225194a979eSMark Johnston restart: 3226bbee39c6SJeff Roberson /* 3227194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3228194a979eSMark Johnston * domain (as happens with first-touch zones). 3229194a979eSMark Johnston * 3230194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3231194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3232194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3233bbee39c6SJeff Roberson */ 3234ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3235ab3185d1SJeff Roberson if (rr) { 3236194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3237194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3238194a979eSMark Johnston &aflags); 3239194a979eSMark Johnston } else { 3240194a979eSMark Johnston aflags = flags; 3241194a979eSMark Johnston domain = rdomain; 3242194a979eSMark Johnston } 3243ab3185d1SJeff Roberson 3244194a979eSMark Johnston for (;;) { 3245194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3246584061b4SJeff Roberson if (slab != NULL) 3247bbee39c6SJeff Roberson return (slab); 3248bbee39c6SJeff Roberson 3249bbee39c6SJeff Roberson /* 3250bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3251bbee39c6SJeff Roberson */ 3252bbee39c6SJeff Roberson if (flags & M_NOVM) 3253bbee39c6SJeff Roberson break; 3254bbee39c6SJeff Roberson 325586220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 32568b987a77SJeff Roberson if (slab != NULL) 3257bbee39c6SJeff Roberson return (slab); 32583639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 32593639ac42SJeff Roberson break; 3260194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3261194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 3262194a979eSMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 3263194a979eSMark Johnston goto restart; 326430c5525bSAndrew Gallatin } 3265194a979eSMark Johnston break; 3266194a979eSMark Johnston } 3267ab3185d1SJeff Roberson } 3268ab3185d1SJeff Roberson 3269bbee39c6SJeff Roberson /* 3270bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3271bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3272bbee39c6SJeff Roberson * fail. 3273bbee39c6SJeff Roberson */ 32748b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 3275bbee39c6SJeff Roberson return (slab); 32768b987a77SJeff Roberson 3277ab3185d1SJeff Roberson return (NULL); 3278ab3185d1SJeff Roberson } 3279bbee39c6SJeff Roberson 3280d56368d7SBosko Milekic static void * 32810095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3282bbee39c6SJeff Roberson { 3283ab3185d1SJeff Roberson uma_domain_t dom; 3284bbee39c6SJeff Roberson void *item; 328585dcf349SGleb Smirnoff uint8_t freei; 3286bbee39c6SJeff Roberson 32878b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3288099a0e58SBosko Milekic 32898b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 32909b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 32919b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 32921e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3293bbee39c6SJeff Roberson slab->us_freecount--; 32948b987a77SJeff Roberson dom->ud_free--; 3295ef72505eSJeff Roberson 3296bbee39c6SJeff Roberson /* Move this slab to the full list */ 3297bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3298bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3299ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3300bbee39c6SJeff Roberson } 3301bbee39c6SJeff Roberson 3302bbee39c6SJeff Roberson return (item); 3303bbee39c6SJeff Roberson } 3304bbee39c6SJeff Roberson 3305bbee39c6SJeff Roberson static int 3306b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 33070095a784SJeff Roberson { 33088b987a77SJeff Roberson uma_domain_t dom; 3309b75c4efcSAndrew Turner uma_zone_t zone; 33100095a784SJeff Roberson uma_slab_t slab; 33110095a784SJeff Roberson uma_keg_t keg; 3312a03af342SSean Bruno #ifdef NUMA 3313ab3185d1SJeff Roberson int stripe; 3314a03af342SSean Bruno #endif 33150095a784SJeff Roberson int i; 33160095a784SJeff Roberson 3317b75c4efcSAndrew Turner zone = arg; 33180095a784SJeff Roberson slab = NULL; 3319584061b4SJeff Roberson keg = zone->uz_keg; 3320af526374SJeff Roberson /* Try to keep the buckets totally full */ 33210095a784SJeff Roberson for (i = 0; i < max; ) { 3322584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 33230095a784SJeff Roberson break; 3324a03af342SSean Bruno #ifdef NUMA 3325ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3326a03af342SSean Bruno #endif 33278b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 33286fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 33290095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 33308b987a77SJeff Roberson if (dom->ud_free <= keg->uk_reserve) 33316fd34d6fSJeff Roberson break; 3332b6715dabSJeff Roberson #ifdef NUMA 3333ab3185d1SJeff Roberson /* 3334ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3335ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3336ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3337ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3338ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3339ab3185d1SJeff Roberson * time but yields better distribution. 3340ab3185d1SJeff Roberson */ 3341ab3185d1SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) == 0 && 3342ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3343ab3185d1SJeff Roberson break; 3344ab3185d1SJeff Roberson #endif 33456fd34d6fSJeff Roberson } 33468b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 3347ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 33480095a784SJeff Roberson flags &= ~M_WAITOK; 33490095a784SJeff Roberson flags |= M_NOWAIT; 33500095a784SJeff Roberson } 33510095a784SJeff Roberson 33520095a784SJeff Roberson return i; 33530095a784SJeff Roberson } 33540095a784SJeff Roberson 33554bd61e19SJeff Roberson static int 33564bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 33574bd61e19SJeff Roberson { 33584bd61e19SJeff Roberson uint64_t old, new, total, max; 33594bd61e19SJeff Roberson 33604bd61e19SJeff Roberson /* 33614bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 33624bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 33634bd61e19SJeff Roberson * fairness by keeping fifo order. 33644bd61e19SJeff Roberson * 33654bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 33664bd61e19SJeff Roberson */ 33674bd61e19SJeff Roberson for (;;) { 33684bd61e19SJeff Roberson zone_free_limit(zone, count); 33694bd61e19SJeff Roberson zone_log_warning(zone); 33704bd61e19SJeff Roberson zone_maxaction(zone); 33714bd61e19SJeff Roberson if (flags & M_NOWAIT) 33724bd61e19SJeff Roberson return (0); 33734bd61e19SJeff Roberson 33744bd61e19SJeff Roberson /* 33754bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 33764bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 33774bd61e19SJeff Roberson * is essentially a home rolled semaphore. 33784bd61e19SJeff Roberson */ 33794bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 33804bd61e19SJeff Roberson old = zone->uz_items; 33814bd61e19SJeff Roberson do { 33824bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 33834bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 33844bd61e19SJeff Roberson max = zone->uz_max_items; 33854bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 33864bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 33874bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 33884bd61e19SJeff Roberson else 33894bd61e19SJeff Roberson new = old + MIN(count, max - old); 33904bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 33914bd61e19SJeff Roberson 33924bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 33934bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 33944bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 33954bd61e19SJeff Roberson return (new - old); 33964bd61e19SJeff Roberson } 33974bd61e19SJeff Roberson 33984bd61e19SJeff Roberson /* 33994bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 34004bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 34014bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 34024bd61e19SJeff Roberson * transitions. 34034bd61e19SJeff Roberson */ 34044bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 34054bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 34064bd61e19SJeff Roberson 34074bd61e19SJeff Roberson /* 34084bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 34094bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 34104bd61e19SJeff Roberson */ 34114bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 34124bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 34134bd61e19SJeff Roberson 34144bd61e19SJeff Roberson /* 34154bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 34164bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 34174bd61e19SJeff Roberson * 34184bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 34194bd61e19SJeff Roberson * our count. 34204bd61e19SJeff Roberson */ 34214bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 34224bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 34234bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 34244bd61e19SJeff Roberson /* We're no longer a sleeper. */ 34254bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 34264bd61e19SJeff Roberson 34274bd61e19SJeff Roberson /* 34284bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 34294bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 34304bd61e19SJeff Roberson * against changes via sysctl. 34314bd61e19SJeff Roberson */ 34324bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 34334bd61e19SJeff Roberson max = zone->uz_max_items; 34344bd61e19SJeff Roberson if (total >= max) 34354bd61e19SJeff Roberson continue; 34364bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 34374bd61e19SJeff Roberson if (total + count > max) { 34384bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 34394bd61e19SJeff Roberson count = max - total; 34404bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 34414bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 34424bd61e19SJeff Roberson 34434bd61e19SJeff Roberson return (count); 34444bd61e19SJeff Roberson } 34454bd61e19SJeff Roberson } 34464bd61e19SJeff Roberson 34474bd61e19SJeff Roberson /* 34484bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 34494bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 34504bd61e19SJeff Roberson * one item can be allocated. 34514bd61e19SJeff Roberson */ 34524bd61e19SJeff Roberson static int 34534bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 34544bd61e19SJeff Roberson { 34554bd61e19SJeff Roberson uint64_t old; 34564bd61e19SJeff Roberson uint64_t max; 34574bd61e19SJeff Roberson 34584bd61e19SJeff Roberson max = zone->uz_max_items; 34594bd61e19SJeff Roberson MPASS(max > 0); 34604bd61e19SJeff Roberson 34614bd61e19SJeff Roberson /* 34624bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 34634bd61e19SJeff Roberson * fetchadd. 34644bd61e19SJeff Roberson */ 34654bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 34664bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 34674bd61e19SJeff Roberson return (count); 34684bd61e19SJeff Roberson 34694bd61e19SJeff Roberson /* 34704bd61e19SJeff Roberson * If we had some items and no sleepers just return the 34714bd61e19SJeff Roberson * truncated value. We have to release the excess space 34724bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 34734bd61e19SJeff Roberson * because we were temporarily over the limit. 34744bd61e19SJeff Roberson */ 34754bd61e19SJeff Roberson if (old < max) { 34764bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 34774bd61e19SJeff Roberson return (max - old); 34784bd61e19SJeff Roberson } 34794bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 34804bd61e19SJeff Roberson } 34814bd61e19SJeff Roberson 34824bd61e19SJeff Roberson /* 34834bd61e19SJeff Roberson * Free a number of items back to the limit. 34844bd61e19SJeff Roberson */ 34854bd61e19SJeff Roberson static void 34864bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 34874bd61e19SJeff Roberson { 34884bd61e19SJeff Roberson uint64_t old; 34894bd61e19SJeff Roberson 34904bd61e19SJeff Roberson MPASS(count > 0); 34914bd61e19SJeff Roberson 34924bd61e19SJeff Roberson /* 34934bd61e19SJeff Roberson * In the common case we either have no sleepers or 34944bd61e19SJeff Roberson * are still over the limit and can just return. 34954bd61e19SJeff Roberson */ 34964bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 34974bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 34984bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 34994bd61e19SJeff Roberson return; 35004bd61e19SJeff Roberson 35014bd61e19SJeff Roberson /* 35024bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 35034bd61e19SJeff Roberson * to generate wakeups if necessary. 35044bd61e19SJeff Roberson */ 35054bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 35064bd61e19SJeff Roberson } 35074bd61e19SJeff Roberson 3508fc03d22bSJeff Roberson static uma_bucket_t 3509beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3510bbee39c6SJeff Roberson { 3511bbee39c6SJeff Roberson uma_bucket_t bucket; 3512beb8beefSJeff Roberson int maxbucket, cnt; 3513bbee39c6SJeff Roberson 351430c5525bSAndrew Gallatin CTR1(KTR_UMA, "zone_alloc:_bucket domain %d)", domain); 351530c5525bSAndrew Gallatin 3516c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3517c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3518c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3519c1685086SJeff Roberson 35204bd61e19SJeff Roberson if (zone->uz_max_items > 0) 35214bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 35224bd61e19SJeff Roberson M_NOWAIT); 35234bd61e19SJeff Roberson else 352420a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 35254bd61e19SJeff Roberson if (maxbucket == 0) 35264bd61e19SJeff Roberson return (false); 3527beb8beefSJeff Roberson 35286fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 35296fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3530beb8beefSJeff Roberson if (bucket == NULL) { 3531beb8beefSJeff Roberson cnt = 0; 3532beb8beefSJeff Roberson goto out; 3533beb8beefSJeff Roberson } 35340095a784SJeff Roberson 35350095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3536beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 35370095a784SJeff Roberson 35380095a784SJeff Roberson /* 35390095a784SJeff Roberson * Initialize the memory if necessary. 35400095a784SJeff Roberson */ 35410095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 3542099a0e58SBosko Milekic int i; 3543bbee39c6SJeff Roberson 35440095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 3545e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 35460095a784SJeff Roberson flags) != 0) 3547b23f72e9SBrian Feldman break; 3548b23f72e9SBrian Feldman /* 3549b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 3550b23f72e9SBrian Feldman * rest back onto the freelist. 3551b23f72e9SBrian Feldman */ 3552b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 3553af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 35540095a784SJeff Roberson bucket->ub_cnt - i); 3555a5a262c6SBosko Milekic #ifdef INVARIANTS 35560095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 35570095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 3558a5a262c6SBosko Milekic #endif 3559b23f72e9SBrian Feldman bucket->ub_cnt = i; 3560b23f72e9SBrian Feldman } 3561099a0e58SBosko Milekic } 3562099a0e58SBosko Milekic 3563beb8beefSJeff Roberson cnt = bucket->ub_cnt; 3564f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 35656fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 35662efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 3567beb8beefSJeff Roberson bucket = NULL; 3568beb8beefSJeff Roberson } 3569beb8beefSJeff Roberson out: 35704bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 35714bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 3572fc03d22bSJeff Roberson 3573fc03d22bSJeff Roberson return (bucket); 3574fc03d22bSJeff Roberson } 3575fc03d22bSJeff Roberson 35768355f576SJeff Roberson /* 35770095a784SJeff Roberson * Allocates a single item from a zone. 35788355f576SJeff Roberson * 35798355f576SJeff Roberson * Arguments 35808355f576SJeff Roberson * zone The zone to alloc for. 35818355f576SJeff Roberson * udata The data to be passed to the constructor. 3582ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 3583a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 35848355f576SJeff Roberson * 35858355f576SJeff Roberson * Returns 35868355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 3587bbee39c6SJeff Roberson * An item if successful 35888355f576SJeff Roberson */ 35898355f576SJeff Roberson 35908355f576SJeff Roberson static void * 3591ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 35928355f576SJeff Roberson { 35938355f576SJeff Roberson void *item; 35948355f576SJeff Roberson 35954bd61e19SJeff Roberson if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) 3596bb15d1c7SGleb Smirnoff return (NULL); 35978355f576SJeff Roberson 3598c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3599c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 360030c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 3601c1685086SJeff Roberson 3602ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 3603beb8beefSJeff Roberson goto fail_cnt; 36048355f576SJeff Roberson 3605099a0e58SBosko Milekic /* 3606099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 3607099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 3608099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 3609099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 3610099a0e58SBosko Milekic */ 3611b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 3612e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 3613bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 3614beb8beefSJeff Roberson goto fail_cnt; 3615beb8beefSJeff Roberson } 3616beb8beefSJeff Roberson } 3617cc7ce83aSJeff Roberson item = item_ctor(zone, zone->uz_size, udata, flags, item); 3618beb8beefSJeff Roberson if (item == NULL) 36190095a784SJeff Roberson goto fail; 36208355f576SJeff Roberson 36212efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 36221431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 36231431a748SGleb Smirnoff zone->uz_name, zone); 36241431a748SGleb Smirnoff 36258355f576SJeff Roberson return (item); 36260095a784SJeff Roberson 3627beb8beefSJeff Roberson fail_cnt: 3628beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 36290095a784SJeff Roberson fail: 36304bd61e19SJeff Roberson if (zone->uz_max_items > 0) 36314bd61e19SJeff Roberson zone_free_limit(zone, 1); 36321431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 36331431a748SGleb Smirnoff zone->uz_name, zone); 36344bd61e19SJeff Roberson 36350095a784SJeff Roberson return (NULL); 36368355f576SJeff Roberson } 36378355f576SJeff Roberson 36388355f576SJeff Roberson /* See uma.h */ 36398355f576SJeff Roberson void 36408355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 36418355f576SJeff Roberson { 36428355f576SJeff Roberson uma_cache_t cache; 3643376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3644cc7ce83aSJeff Roberson int domain, itemdomain, uz_flags; 36458355f576SJeff Roberson 3646e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 364719fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 364810cb2424SMark Murray 36493659f747SRobert Watson CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread, 36503659f747SRobert Watson zone->uz_name); 36513659f747SRobert Watson 3652d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 36531067a2baSJonathan T. Looney ("uma_zfree_arg: called with spinlock or critical section held")); 36541067a2baSJonathan T. Looney 365520ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 365620ed0cb0SMatthew D Fleming if (item == NULL) 365720ed0cb0SMatthew D Fleming return; 36588d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 36598d689e04SGleb Smirnoff if (is_memguard_addr(item)) { 3660bc9d08e1SMark Johnston if (zone->uz_dtor != NULL) 36618d689e04SGleb Smirnoff zone->uz_dtor(item, zone->uz_size, udata); 3662bc9d08e1SMark Johnston if (zone->uz_fini != NULL) 36638d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 36648d689e04SGleb Smirnoff memguard_free(item); 36658d689e04SGleb Smirnoff return; 36668d689e04SGleb Smirnoff } 36678d689e04SGleb Smirnoff #endif 3668cc7ce83aSJeff Roberson 3669cc7ce83aSJeff Roberson /* 3670cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 3671cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 3672cc7ce83aSJeff Roberson * will simply read another cpu's line. 3673cc7ce83aSJeff Roberson */ 3674cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3675cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3676cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 3677cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 3678cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 3679ef72505eSJeff Roberson 3680af7f9b97SJeff Roberson /* 3681af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 3682af7f9b97SJeff Roberson * a little longer for the limits to be reset. 3683af7f9b97SJeff Roberson */ 3684cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 3685bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0) 3686fc03d22bSJeff Roberson goto zfree_item; 3687cc7ce83aSJeff Roberson } 3688af7f9b97SJeff Roberson 36895d1ae027SRobert Watson /* 36905d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 36915d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 36925d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 36935d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 36945d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 36955d1ae027SRobert Watson * preemption and migration. We release the critical section in 36965d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 36975d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 36985d1ae027SRobert Watson * detect and handle migration if it has occurred. 36995d1ae027SRobert Watson */ 37000a81b439SJeff Roberson domain = itemdomain = 0; 37015d1ae027SRobert Watson critical_enter(); 37020a81b439SJeff Roberson do { 3703cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3704376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 3705c1685086SJeff Roberson #ifdef UMA_XDOMAIN 3706cc7ce83aSJeff Roberson if ((uz_flags & UMA_ZONE_NUMA) != 0) { 37070a81b439SJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 37080a81b439SJeff Roberson domain = PCPU_GET(domain); 37090a81b439SJeff Roberson } 3710cc7ce83aSJeff Roberson if ((uz_flags & UMA_ZONE_NUMA) != 0 && domain != itemdomain) { 3711376b1ba3SJeff Roberson bucket = &cache->uc_crossbucket; 37120a81b439SJeff Roberson } else 3713c1685086SJeff Roberson #endif 37140a81b439SJeff Roberson 3715a553d4b8SJeff Roberson /* 3716fc03d22bSJeff Roberson * Try to free into the allocbucket first to give LIFO ordering 3717fc03d22bSJeff Roberson * for cache-hot datastructures. Spill over into the freebucket 3718fc03d22bSJeff Roberson * if necessary. Alloc will swap them if one runs dry. 3719a553d4b8SJeff Roberson */ 3720376b1ba3SJeff Roberson if (__predict_false(bucket->ucb_cnt >= bucket->ucb_entries)) 3721376b1ba3SJeff Roberson bucket = &cache->uc_freebucket; 3722376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3723376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 37245d1ae027SRobert Watson critical_exit(); 37258355f576SJeff Roberson return; 3726fc03d22bSJeff Roberson } 37270a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 37280a81b439SJeff Roberson critical_exit(); 3729fc03d22bSJeff Roberson 37308355f576SJeff Roberson /* 37310a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 37328355f576SJeff Roberson */ 37330a81b439SJeff Roberson zfree_item: 37340a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 37350a81b439SJeff Roberson } 3736fc03d22bSJeff Roberson 3737*91d947bfSJeff Roberson #ifdef UMA_XDOMAIN 3738*91d947bfSJeff Roberson /* 3739*91d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 3740*91d947bfSJeff Roberson * them. 3741*91d947bfSJeff Roberson */ 3742*91d947bfSJeff Roberson static void 3743*91d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 3744*91d947bfSJeff Roberson { 3745*91d947bfSJeff Roberson struct uma_bucketlist fullbuckets; 3746*91d947bfSJeff Roberson uma_zone_domain_t zdom; 3747*91d947bfSJeff Roberson uma_bucket_t b; 3748*91d947bfSJeff Roberson void *item; 3749*91d947bfSJeff Roberson int domain; 3750*91d947bfSJeff Roberson 3751*91d947bfSJeff Roberson CTR3(KTR_UMA, 3752*91d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 3753*91d947bfSJeff Roberson zone->uz_name, zone, bucket); 3754*91d947bfSJeff Roberson 3755*91d947bfSJeff Roberson TAILQ_INIT(&fullbuckets); 3756*91d947bfSJeff Roberson 3757*91d947bfSJeff Roberson /* 3758*91d947bfSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 3759*91d947bfSJeff Roberson * lock on the current crossfree bucket. A full matrix with 3760*91d947bfSJeff Roberson * per-domain locking could be used if necessary. 3761*91d947bfSJeff Roberson */ 3762*91d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 3763*91d947bfSJeff Roberson while (bucket->ub_cnt > 0) { 3764*91d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 3765*91d947bfSJeff Roberson domain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 3766*91d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 3767*91d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 3768*91d947bfSJeff Roberson zdom->uzd_cross = bucket_alloc(zone, udata, M_NOWAIT); 3769*91d947bfSJeff Roberson if (zdom->uzd_cross == NULL) 3770*91d947bfSJeff Roberson break; 3771*91d947bfSJeff Roberson } 3772*91d947bfSJeff Roberson zdom->uzd_cross->ub_bucket[zdom->uzd_cross->ub_cnt++] = item; 3773*91d947bfSJeff Roberson if (zdom->uzd_cross->ub_cnt == zdom->uzd_cross->ub_entries) { 3774*91d947bfSJeff Roberson TAILQ_INSERT_HEAD(&fullbuckets, zdom->uzd_cross, 3775*91d947bfSJeff Roberson ub_link); 3776*91d947bfSJeff Roberson zdom->uzd_cross = NULL; 3777*91d947bfSJeff Roberson } 3778*91d947bfSJeff Roberson bucket->ub_cnt--; 3779*91d947bfSJeff Roberson } 3780*91d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 3781*91d947bfSJeff Roberson if (!TAILQ_EMPTY(&fullbuckets)) { 3782*91d947bfSJeff Roberson ZONE_LOCK(zone); 3783*91d947bfSJeff Roberson while ((b = TAILQ_FIRST(&fullbuckets)) != NULL) { 3784*91d947bfSJeff Roberson TAILQ_REMOVE(&fullbuckets, b, ub_link); 3785*91d947bfSJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 3786*91d947bfSJeff Roberson ZONE_UNLOCK(zone); 3787*91d947bfSJeff Roberson bucket_drain(zone, b); 3788*91d947bfSJeff Roberson bucket_free(zone, b, udata); 3789*91d947bfSJeff Roberson ZONE_LOCK(zone); 3790*91d947bfSJeff Roberson } else { 3791*91d947bfSJeff Roberson domain = _vm_phys_domain( 3792*91d947bfSJeff Roberson pmap_kextract( 3793*91d947bfSJeff Roberson (vm_offset_t)b->ub_bucket[0])); 3794*91d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 3795*91d947bfSJeff Roberson zone_put_bucket(zone, zdom, b, true); 3796*91d947bfSJeff Roberson } 3797*91d947bfSJeff Roberson } 3798*91d947bfSJeff Roberson ZONE_UNLOCK(zone); 3799*91d947bfSJeff Roberson } 3800*91d947bfSJeff Roberson if (bucket->ub_cnt != 0) 3801*91d947bfSJeff Roberson bucket_drain(zone, bucket); 3802*91d947bfSJeff Roberson bucket_free(zone, bucket, udata); 3803*91d947bfSJeff Roberson } 3804*91d947bfSJeff Roberson #endif 3805*91d947bfSJeff Roberson 38060a81b439SJeff Roberson static void 38070a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 38080a81b439SJeff Roberson int domain, int itemdomain) 38090a81b439SJeff Roberson { 38100a81b439SJeff Roberson uma_zone_domain_t zdom; 38110a81b439SJeff Roberson 38120a81b439SJeff Roberson #ifdef UMA_XDOMAIN 38130a81b439SJeff Roberson /* 38140a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 38150a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 38160a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 3817*91d947bfSJeff Roberson * correct domains. 38180a81b439SJeff Roberson */ 38190a81b439SJeff Roberson if (domain != itemdomain && vm_ndomains > 2) { 3820*91d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 38210a81b439SJeff Roberson return; 38220a81b439SJeff Roberson } 38230a81b439SJeff Roberson #endif 3824*91d947bfSJeff Roberson 38250a81b439SJeff Roberson /* 38260a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 38270a81b439SJeff Roberson * 38280a81b439SJeff Roberson * We bump the uz count when the cache size is insufficient to 38290a81b439SJeff Roberson * handle the working set. 38300a81b439SJeff Roberson */ 38314d104ba0SAlexander Motin if (ZONE_TRYLOCK(zone) == 0) { 38324d104ba0SAlexander Motin /* Record contention to size the buckets. */ 38338355f576SJeff Roberson ZONE_LOCK(zone); 383420a4e154SJeff Roberson if (zone->uz_bucket_size < zone->uz_bucket_size_max) 383520a4e154SJeff Roberson zone->uz_bucket_size++; 38364d104ba0SAlexander Motin } 38378355f576SJeff Roberson 38380a81b439SJeff Roberson CTR3(KTR_UMA, 38390a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 38400a81b439SJeff Roberson zone->uz_name, zone, bucket); 38410a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 38420a81b439SJeff Roberson KASSERT(bucket->ub_cnt == bucket->ub_entries, 38430a81b439SJeff Roberson ("uma_zfree: Attempting to insert partial bucket onto the full list.\n")); 38440a81b439SJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 3845c1685086SJeff Roberson ZONE_UNLOCK(zone); 3846c1685086SJeff Roberson bucket_drain(zone, bucket); 3847c1685086SJeff Roberson bucket_free(zone, bucket, udata); 3848c1685086SJeff Roberson } else { 3849c1685086SJeff Roberson zdom = &zone->uz_domain[itemdomain]; 3850c1685086SJeff Roberson zone_put_bucket(zone, zdom, bucket, true); 3851c1685086SJeff Roberson ZONE_UNLOCK(zone); 3852c1685086SJeff Roberson } 38538355f576SJeff Roberson } 3854fc03d22bSJeff Roberson 38554d104ba0SAlexander Motin /* 38560a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 38570a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 38580a81b439SJeff Roberson * 38590a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 38600a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 38610a81b439SJeff Roberson * the caller should retry. 38624d104ba0SAlexander Motin */ 38630a81b439SJeff Roberson static __noinline bool 38640a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 38650a81b439SJeff Roberson int itemdomain) 38660a81b439SJeff Roberson { 38670a81b439SJeff Roberson uma_bucket_t bucket; 3868cc7ce83aSJeff Roberson int domain; 38690a81b439SJeff Roberson 38700a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 38710a81b439SJeff Roberson 387220a4e154SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) 38730a81b439SJeff Roberson return false; 38740a81b439SJeff Roberson 3875cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 38760a81b439SJeff Roberson 38770a81b439SJeff Roberson /* 38780a81b439SJeff Roberson * NUMA domains need to free to the correct zdom. When XDOMAIN 38790a81b439SJeff Roberson * is enabled this is the zdom of the item and the bucket may be 38800a81b439SJeff Roberson * the cross bucket if they do not match. 38810a81b439SJeff Roberson */ 38820a81b439SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) 38830a81b439SJeff Roberson #ifdef UMA_XDOMAIN 38840a81b439SJeff Roberson domain = PCPU_GET(domain); 38850a81b439SJeff Roberson #else 38860a81b439SJeff Roberson itemdomain = domain = PCPU_GET(domain); 38870a81b439SJeff Roberson #endif 38880a81b439SJeff Roberson else 38890a81b439SJeff Roberson itemdomain = domain = 0; 38900a81b439SJeff Roberson #ifdef UMA_XDOMAIN 38910a81b439SJeff Roberson if (domain != itemdomain) { 3892376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 38930a81b439SJeff Roberson if (bucket != NULL) 38940a81b439SJeff Roberson atomic_add_64(&zone->uz_xdomain, bucket->ub_cnt); 38950a81b439SJeff Roberson } else 38960a81b439SJeff Roberson #endif 3897376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 38980a81b439SJeff Roberson 38990a81b439SJeff Roberson 39000a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 39010a81b439SJeff Roberson critical_exit(); 39020a81b439SJeff Roberson 39030a81b439SJeff Roberson if (bucket != NULL) 39040a81b439SJeff Roberson zone_free_bucket(zone, bucket, udata, domain, itemdomain); 3905a553d4b8SJeff Roberson 39066fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT); 39071431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p", 39081431a748SGleb Smirnoff zone->uz_name, zone, bucket); 3909fc03d22bSJeff Roberson critical_enter(); 39100a81b439SJeff Roberson if (bucket == NULL) 39110a81b439SJeff Roberson return (false); 3912cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 39130a81b439SJeff Roberson #ifdef UMA_XDOMAIN 3914fc03d22bSJeff Roberson /* 39150a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 39160a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 39170a81b439SJeff Roberson * the free bucket. 3918fc03d22bSJeff Roberson */ 39190a81b439SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) { 39200a81b439SJeff Roberson domain = PCPU_GET(domain); 3921376b1ba3SJeff Roberson if (domain != itemdomain && 3922376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 3923376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 39240a81b439SJeff Roberson return (true); 39250a81b439SJeff Roberson } 39260a81b439SJeff Roberson } 39270a81b439SJeff Roberson #endif 39280a81b439SJeff Roberson /* 39290a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 39300a81b439SJeff Roberson */ 3931376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 3932fc03d22bSJeff Roberson critical_exit(); 39336fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 39340a81b439SJeff Roberson critical_enter(); 39350a81b439SJeff Roberson } else 3936376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 39378355f576SJeff Roberson 39380a81b439SJeff Roberson return (true); 39398355f576SJeff Roberson } 39408355f576SJeff Roberson 3941ab3185d1SJeff Roberson void 3942ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata) 3943ab3185d1SJeff Roberson { 3944ab3185d1SJeff Roberson 3945ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 394619fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3947ab3185d1SJeff Roberson 3948ab3185d1SJeff Roberson CTR2(KTR_UMA, "uma_zfree_domain thread %x zone %s", curthread, 3949ab3185d1SJeff Roberson zone->uz_name); 3950ab3185d1SJeff Roberson 3951ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3952ab3185d1SJeff Roberson ("uma_zfree_domain: called with spinlock or critical section held")); 3953ab3185d1SJeff Roberson 3954ab3185d1SJeff Roberson /* uma_zfree(..., NULL) does nothing, to match free(9). */ 3955ab3185d1SJeff Roberson if (item == NULL) 3956ab3185d1SJeff Roberson return; 3957ab3185d1SJeff Roberson zone_free_item(zone, item, udata, SKIP_NONE); 3958ab3185d1SJeff Roberson } 3959ab3185d1SJeff Roberson 39608355f576SJeff Roberson static void 3961bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 39628355f576SJeff Roberson { 3963bb15d1c7SGleb Smirnoff uma_keg_t keg; 3964ab3185d1SJeff Roberson uma_domain_t dom; 396585dcf349SGleb Smirnoff uint8_t freei; 3966099a0e58SBosko Milekic 3967bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 39688b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3969ab3185d1SJeff Roberson 39708355f576SJeff Roberson /* Do we need to remove from any lists? */ 39718b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3972099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 39738355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3974ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 39758355f576SJeff Roberson } else if (slab->us_freecount == 0) { 39768355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3977ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 39788355f576SJeff Roberson } 39798355f576SJeff Roberson 3980ef72505eSJeff Roberson /* Slab management. */ 39811e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 39829b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 39838355f576SJeff Roberson slab->us_freecount++; 39848355f576SJeff Roberson 3985ef72505eSJeff Roberson /* Keg statistics. */ 39868b987a77SJeff Roberson dom->ud_free++; 39870095a784SJeff Roberson } 39880095a784SJeff Roberson 39890095a784SJeff Roberson static void 3990b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 39910095a784SJeff Roberson { 39928b987a77SJeff Roberson struct mtx *lock; 3993b75c4efcSAndrew Turner uma_zone_t zone; 39940095a784SJeff Roberson uma_slab_t slab; 39950095a784SJeff Roberson uma_keg_t keg; 39960095a784SJeff Roberson uint8_t *mem; 39978b987a77SJeff Roberson void *item; 39980095a784SJeff Roberson int i; 39998355f576SJeff Roberson 4000b75c4efcSAndrew Turner zone = arg; 4001bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 40028b987a77SJeff Roberson lock = NULL; 40038b987a77SJeff Roberson if (__predict_false((zone->uz_flags & UMA_ZONE_HASH) != 0)) 40048b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 40050095a784SJeff Roberson for (i = 0; i < cnt; i++) { 40060095a784SJeff Roberson item = bucket[i]; 40078b987a77SJeff Roberson if (__predict_true((zone->uz_flags & UMA_ZONE_VTOSLAB) != 0)) { 40080095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 40098b987a77SJeff Roberson } else { 40108b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 40118b987a77SJeff Roberson if ((zone->uz_flags & UMA_ZONE_HASH) != 0) 40128b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 40138b987a77SJeff Roberson else 40148b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 40158b987a77SJeff Roberson } 40168b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 40178b987a77SJeff Roberson if (lock != NULL) 40188b987a77SJeff Roberson mtx_unlock(lock); 40198b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 40208b987a77SJeff Roberson } 4021bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 40220095a784SJeff Roberson } 40238b987a77SJeff Roberson if (lock != NULL) 40248b987a77SJeff Roberson mtx_unlock(lock); 40258355f576SJeff Roberson } 40268355f576SJeff Roberson 40270095a784SJeff Roberson /* 40280095a784SJeff Roberson * Frees a single item to any zone. 40290095a784SJeff Roberson * 40300095a784SJeff Roberson * Arguments: 40310095a784SJeff Roberson * zone The zone to free to 40320095a784SJeff Roberson * item The item we're freeing 40330095a784SJeff Roberson * udata User supplied data for the dtor 40340095a784SJeff Roberson * skip Skip dtors and finis 40350095a784SJeff Roberson */ 40360095a784SJeff Roberson static void 40370095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 40380095a784SJeff Roberson { 4039c5deaf04SGleb Smirnoff 4040cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 40410095a784SJeff Roberson 40420095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 40430095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 40440095a784SJeff Roberson 40450095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4046bb15d1c7SGleb Smirnoff 4047bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4048bb15d1c7SGleb Smirnoff return; 4049bb15d1c7SGleb Smirnoff 40502efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 40512efcc8cbSGleb Smirnoff 40524bd61e19SJeff Roberson if (zone->uz_max_items > 0) 40534bd61e19SJeff Roberson zone_free_limit(zone, 1); 4054bb45b411SGleb Smirnoff } 40550095a784SJeff Roberson 40568355f576SJeff Roberson /* See uma.h */ 40571c6cae97SLawrence Stewart int 4058736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4059736ee590SJeff Roberson { 4060bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 4061003cf08bSMark Johnston int count; 4062bb15d1c7SGleb Smirnoff 40634bd61e19SJeff Roberson /* 40644bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 40654bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 40664bd61e19SJeff Roberson * way to clear a limit. 40674bd61e19SJeff Roberson */ 4068bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4069003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4070003cf08bSMark Johnston count = ubz != NULL ? ubz->ubz_entries : 0; 407120a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = count; 407220a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 407320a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4074bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4075cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4076cc7ce83aSJeff Roberson zone_update_caches(zone); 40774bd61e19SJeff Roberson /* We may need to wake waiters. */ 40784bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4079bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4080bb15d1c7SGleb Smirnoff 4081bb15d1c7SGleb Smirnoff return (nitems); 4082bb15d1c7SGleb Smirnoff } 4083bb15d1c7SGleb Smirnoff 4084bb15d1c7SGleb Smirnoff /* See uma.h */ 4085003cf08bSMark Johnston void 4086bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4087bb15d1c7SGleb Smirnoff { 4088003cf08bSMark Johnston struct uma_bucket_zone *ubz; 4089003cf08bSMark Johnston int bpcpu; 4090bb15d1c7SGleb Smirnoff 4091bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4092003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4093003cf08bSMark Johnston if (ubz != NULL) { 4094003cf08bSMark Johnston bpcpu = 2; 4095003cf08bSMark Johnston #ifdef UMA_XDOMAIN 4096003cf08bSMark Johnston if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) 4097003cf08bSMark Johnston /* Count the cross-domain bucket. */ 4098003cf08bSMark Johnston bpcpu++; 4099003cf08bSMark Johnston #endif 4100003cf08bSMark Johnston nitems -= ubz->ubz_entries * bpcpu * mp_ncpus; 410120a4e154SJeff Roberson zone->uz_bucket_size_max = ubz->ubz_entries; 4102003cf08bSMark Johnston } else { 410320a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 4104003cf08bSMark Johnston } 410520a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 410620a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4107bb15d1c7SGleb Smirnoff zone->uz_bkt_max = nitems; 4108bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4109736ee590SJeff Roberson } 4110736ee590SJeff Roberson 4111736ee590SJeff Roberson /* See uma.h */ 4112e49471b0SAndre Oppermann int 4113e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4114e49471b0SAndre Oppermann { 4115e49471b0SAndre Oppermann int nitems; 4116e49471b0SAndre Oppermann 4117727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4118e49471b0SAndre Oppermann 4119e49471b0SAndre Oppermann return (nitems); 4120e49471b0SAndre Oppermann } 4121e49471b0SAndre Oppermann 4122e49471b0SAndre Oppermann /* See uma.h */ 41232f891cd5SPawel Jakub Dawidek void 41242f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 41252f891cd5SPawel Jakub Dawidek { 41262f891cd5SPawel Jakub Dawidek 4127727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 41282f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 41292f891cd5SPawel Jakub Dawidek } 41302f891cd5SPawel Jakub Dawidek 41312f891cd5SPawel Jakub Dawidek /* See uma.h */ 413254503a13SJonathan T. Looney void 413354503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 413454503a13SJonathan T. Looney { 413554503a13SJonathan T. Looney 4136727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4137e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 413854503a13SJonathan T. Looney } 413954503a13SJonathan T. Looney 414054503a13SJonathan T. Looney /* See uma.h */ 4141c4ae7908SLawrence Stewart int 4142c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4143c4ae7908SLawrence Stewart { 4144c4ae7908SLawrence Stewart int64_t nitems; 4145c4ae7908SLawrence Stewart u_int i; 4146c4ae7908SLawrence Stewart 41472efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 41482efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4149727c6918SJeff Roberson CPU_FOREACH(i) 4150727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4151727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4152c4ae7908SLawrence Stewart 4153c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4154c4ae7908SLawrence Stewart } 4155c4ae7908SLawrence Stewart 415620a4e154SJeff Roberson static uint64_t 415720a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 415820a4e154SJeff Roberson { 415920a4e154SJeff Roberson uint64_t nitems; 416020a4e154SJeff Roberson u_int i; 416120a4e154SJeff Roberson 416220a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4163727c6918SJeff Roberson CPU_FOREACH(i) 4164727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 416520a4e154SJeff Roberson 416620a4e154SJeff Roberson return (nitems); 416720a4e154SJeff Roberson } 416820a4e154SJeff Roberson 416920a4e154SJeff Roberson static uint64_t 417020a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 417120a4e154SJeff Roberson { 417220a4e154SJeff Roberson uint64_t nitems; 417320a4e154SJeff Roberson u_int i; 417420a4e154SJeff Roberson 417520a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4176727c6918SJeff Roberson CPU_FOREACH(i) 4177727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 417820a4e154SJeff Roberson 417920a4e154SJeff Roberson return (nitems); 418020a4e154SJeff Roberson } 418120a4e154SJeff Roberson 4182c4ae7908SLawrence Stewart /* See uma.h */ 4183736ee590SJeff Roberson void 4184099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4185099a0e58SBosko Milekic { 4186e20a199fSJeff Roberson uma_keg_t keg; 4187e20a199fSJeff Roberson 4188bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4189727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4190e20a199fSJeff Roberson keg->uk_init = uminit; 4191099a0e58SBosko Milekic } 4192099a0e58SBosko Milekic 4193099a0e58SBosko Milekic /* See uma.h */ 4194099a0e58SBosko Milekic void 4195099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4196099a0e58SBosko Milekic { 4197e20a199fSJeff Roberson uma_keg_t keg; 4198e20a199fSJeff Roberson 4199bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4200727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4201e20a199fSJeff Roberson keg->uk_fini = fini; 4202099a0e58SBosko Milekic } 4203099a0e58SBosko Milekic 4204099a0e58SBosko Milekic /* See uma.h */ 4205099a0e58SBosko Milekic void 4206099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4207099a0e58SBosko Milekic { 4208af526374SJeff Roberson 4209727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4210099a0e58SBosko Milekic zone->uz_init = zinit; 4211099a0e58SBosko Milekic } 4212099a0e58SBosko Milekic 4213099a0e58SBosko Milekic /* See uma.h */ 4214099a0e58SBosko Milekic void 4215099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4216099a0e58SBosko Milekic { 4217af526374SJeff Roberson 4218727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4219099a0e58SBosko Milekic zone->uz_fini = zfini; 4220099a0e58SBosko Milekic } 4221099a0e58SBosko Milekic 4222099a0e58SBosko Milekic /* See uma.h */ 4223099a0e58SBosko Milekic void 42248355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 42258355f576SJeff Roberson { 42260095a784SJeff Roberson uma_keg_t keg; 4227e20a199fSJeff Roberson 4228bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4229727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 42300095a784SJeff Roberson keg->uk_freef = freef; 42318355f576SJeff Roberson } 42328355f576SJeff Roberson 42338355f576SJeff Roberson /* See uma.h */ 42348355f576SJeff Roberson void 42358355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 42368355f576SJeff Roberson { 4237e20a199fSJeff Roberson uma_keg_t keg; 4238e20a199fSJeff Roberson 4239bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4240727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4241e20a199fSJeff Roberson keg->uk_allocf = allocf; 42428355f576SJeff Roberson } 42438355f576SJeff Roberson 42448355f576SJeff Roberson /* See uma.h */ 42456fd34d6fSJeff Roberson void 42466fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 42476fd34d6fSJeff Roberson { 42486fd34d6fSJeff Roberson uma_keg_t keg; 42496fd34d6fSJeff Roberson 4250bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4251727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 42526fd34d6fSJeff Roberson keg->uk_reserve = items; 42536fd34d6fSJeff Roberson } 42546fd34d6fSJeff Roberson 42556fd34d6fSJeff Roberson /* See uma.h */ 42568355f576SJeff Roberson int 4257a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 42588355f576SJeff Roberson { 4259099a0e58SBosko Milekic uma_keg_t keg; 42608355f576SJeff Roberson vm_offset_t kva; 42619ba30bcbSZbigniew Bodek u_int pages; 42628355f576SJeff Roberson 4263bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4264727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4265727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 42668355f576SJeff Roberson 4267bb15d1c7SGleb Smirnoff pages = count / keg->uk_ipers; 4268099a0e58SBosko Milekic if (pages * keg->uk_ipers < count) 42698355f576SJeff Roberson pages++; 427057223e99SAndriy Gapon pages *= keg->uk_ppera; 4271a553d4b8SJeff Roberson 4272a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4273a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4274a4915c21SAttilio Rao #else 4275a4915c21SAttilio Rao if (1) { 4276a4915c21SAttilio Rao #endif 427757223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4278d1f42ac2SAlan Cox if (kva == 0) 42798355f576SJeff Roberson return (0); 4280a4915c21SAttilio Rao } else 4281a4915c21SAttilio Rao kva = 0; 4282bb15d1c7SGleb Smirnoff 4283bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4284bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4285099a0e58SBosko Milekic keg->uk_kva = kva; 4286a4915c21SAttilio Rao keg->uk_offset = 0; 4287bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4288a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4289a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4290a4915c21SAttilio Rao #else 4291a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4292a4915c21SAttilio Rao #endif 4293cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4294cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4295cc7ce83aSJeff Roberson zone_update_caches(zone); 4296bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4297af526374SJeff Roberson 42988355f576SJeff Roberson return (1); 42998355f576SJeff Roberson } 43008355f576SJeff Roberson 43018355f576SJeff Roberson /* See uma.h */ 43028355f576SJeff Roberson void 43038355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 43048355f576SJeff Roberson { 4305920239efSMark Johnston struct vm_domainset_iter di; 4306ab3185d1SJeff Roberson uma_domain_t dom; 43078355f576SJeff Roberson uma_slab_t slab; 4308099a0e58SBosko Milekic uma_keg_t keg; 430986220393SMark Johnston int aflags, domain, slabs; 43108355f576SJeff Roberson 4311bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4312099a0e58SBosko Milekic slabs = items / keg->uk_ipers; 4313099a0e58SBosko Milekic if (slabs * keg->uk_ipers < items) 43148355f576SJeff Roberson slabs++; 4315194a979eSMark Johnston while (slabs-- > 0) { 431686220393SMark Johnston aflags = M_NOWAIT; 431786220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 431886220393SMark Johnston &aflags); 431986220393SMark Johnston for (;;) { 432086220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 432186220393SMark Johnston aflags); 432286220393SMark Johnston if (slab != NULL) { 4323ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 43248b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 432586220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 432686220393SMark Johnston us_link); 43278b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 4328920239efSMark Johnston break; 43298355f576SJeff Roberson } 43308b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 433186220393SMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 433286220393SMark Johnston } 433386220393SMark Johnston } 433486220393SMark Johnston } 43358355f576SJeff Roberson 43368355f576SJeff Roberson /* See uma.h */ 433708cfa56eSMark Johnston void 433808cfa56eSMark Johnston uma_reclaim(int req) 43398355f576SJeff Roberson { 434044ec2b63SKonstantin Belousov 43411431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 434208cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 434386bbae32SJeff Roberson bucket_enable(); 434408cfa56eSMark Johnston 434508cfa56eSMark Johnston switch (req) { 434608cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 434720a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 434808cfa56eSMark Johnston break; 434908cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 435008cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 435120a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 435208cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 435308cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 435420a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4355a2de44abSAlexander Motin } 435608cfa56eSMark Johnston break; 435708cfa56eSMark Johnston default: 435808cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 435908cfa56eSMark Johnston } 43600f9b7bf3SMark Johnston 43618355f576SJeff Roberson /* 43628355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 43638355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 43648355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 43658355f576SJeff Roberson */ 436620a4e154SJeff Roberson zone_drain(slabzone, NULL); 4367cae33c14SJeff Roberson bucket_zone_drain(); 436808cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 43698355f576SJeff Roberson } 43708355f576SJeff Roberson 43712e47807cSJeff Roberson static volatile int uma_reclaim_needed; 437244ec2b63SKonstantin Belousov 437344ec2b63SKonstantin Belousov void 437444ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 437544ec2b63SKonstantin Belousov { 437644ec2b63SKonstantin Belousov 43772e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 43782e47807cSJeff Roberson wakeup(uma_reclaim); 437944ec2b63SKonstantin Belousov } 438044ec2b63SKonstantin Belousov 438144ec2b63SKonstantin Belousov void 438244ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 438344ec2b63SKonstantin Belousov { 438444ec2b63SKonstantin Belousov 438544ec2b63SKonstantin Belousov for (;;) { 438608cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4387200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 438808cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 43892e47807cSJeff Roberson hz); 439008cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 43919b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 439208cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4393200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 43942e47807cSJeff Roberson /* Don't fire more than once per-second. */ 43952e47807cSJeff Roberson pause("umarclslp", hz); 439644ec2b63SKonstantin Belousov } 439744ec2b63SKonstantin Belousov } 439844ec2b63SKonstantin Belousov 4399663b416fSJohn Baldwin /* See uma.h */ 440008cfa56eSMark Johnston void 440108cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 440208cfa56eSMark Johnston { 440308cfa56eSMark Johnston 440408cfa56eSMark Johnston switch (req) { 440508cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 440620a4e154SJeff Roberson zone_trim(zone, NULL); 440708cfa56eSMark Johnston break; 440808cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 440920a4e154SJeff Roberson zone_drain(zone, NULL); 441008cfa56eSMark Johnston break; 441108cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 441208cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 441320a4e154SJeff Roberson zone_drain(zone, NULL); 441408cfa56eSMark Johnston break; 441508cfa56eSMark Johnston default: 441608cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 441708cfa56eSMark Johnston } 441808cfa56eSMark Johnston } 441908cfa56eSMark Johnston 442008cfa56eSMark Johnston /* See uma.h */ 4421663b416fSJohn Baldwin int 4422663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 4423663b416fSJohn Baldwin { 4424663b416fSJohn Baldwin 4425727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 44266c125b8dSMohan Srinivasan } 44276c125b8dSMohan Srinivasan 44282e47807cSJeff Roberson unsigned long 44292e47807cSJeff Roberson uma_limit(void) 44302e47807cSJeff Roberson { 44312e47807cSJeff Roberson 44322e47807cSJeff Roberson return (uma_kmem_limit); 44332e47807cSJeff Roberson } 44342e47807cSJeff Roberson 44352e47807cSJeff Roberson void 44362e47807cSJeff Roberson uma_set_limit(unsigned long limit) 44372e47807cSJeff Roberson { 44382e47807cSJeff Roberson 44392e47807cSJeff Roberson uma_kmem_limit = limit; 44402e47807cSJeff Roberson } 44412e47807cSJeff Roberson 44422e47807cSJeff Roberson unsigned long 44432e47807cSJeff Roberson uma_size(void) 44442e47807cSJeff Roberson { 44452e47807cSJeff Roberson 4446058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 4447ad5b0f5bSJeff Roberson } 4448ad5b0f5bSJeff Roberson 4449ad5b0f5bSJeff Roberson long 4450ad5b0f5bSJeff Roberson uma_avail(void) 4451ad5b0f5bSJeff Roberson { 4452ad5b0f5bSJeff Roberson 4453058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 44542e47807cSJeff Roberson } 44552e47807cSJeff Roberson 4456a0d4b0aeSRobert Watson #ifdef DDB 44578355f576SJeff Roberson /* 44587a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 44597a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 44607a52a97eSRobert Watson * 44617a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 44627a52a97eSRobert Watson * per-CPU cache statistic. 44637a52a97eSRobert Watson * 44647a52a97eSRobert Watson */ 44657a52a97eSRobert Watson static void 44660f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 4467c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 44687a52a97eSRobert Watson { 44697a52a97eSRobert Watson uma_cache_t cache; 4470c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 44717a52a97eSRobert Watson int cachefree, cpu; 44727a52a97eSRobert Watson 4473c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 44747a52a97eSRobert Watson cachefree = 0; 44753aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 44767a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 4477376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 4478376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 4479376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 4480376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 44817a52a97eSRobert Watson allocs += cache->uc_allocs; 44827a52a97eSRobert Watson frees += cache->uc_frees; 44837a52a97eSRobert Watson } 44842efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 44852efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 4486bf965959SSean Bruno sleeps += z->uz_sleeps; 4487c1685086SJeff Roberson xdomain += z->uz_xdomain; 44887a52a97eSRobert Watson if (cachefreep != NULL) 44897a52a97eSRobert Watson *cachefreep = cachefree; 44907a52a97eSRobert Watson if (allocsp != NULL) 44917a52a97eSRobert Watson *allocsp = allocs; 44927a52a97eSRobert Watson if (freesp != NULL) 44937a52a97eSRobert Watson *freesp = frees; 4494bf965959SSean Bruno if (sleepsp != NULL) 4495bf965959SSean Bruno *sleepsp = sleeps; 4496c1685086SJeff Roberson if (xdomainp != NULL) 4497c1685086SJeff Roberson *xdomainp = xdomain; 44987a52a97eSRobert Watson } 4499a0d4b0aeSRobert Watson #endif /* DDB */ 45007a52a97eSRobert Watson 45017a52a97eSRobert Watson static int 45027a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 45037a52a97eSRobert Watson { 45047a52a97eSRobert Watson uma_keg_t kz; 45057a52a97eSRobert Watson uma_zone_t z; 45067a52a97eSRobert Watson int count; 45077a52a97eSRobert Watson 45087a52a97eSRobert Watson count = 0; 4509111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 45107a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 45117a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 45127a52a97eSRobert Watson count++; 45137a52a97eSRobert Watson } 4514b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4515b47acb0aSGleb Smirnoff count++; 4516b47acb0aSGleb Smirnoff 4517111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 45187a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 45197a52a97eSRobert Watson } 45207a52a97eSRobert Watson 4521b47acb0aSGleb Smirnoff static void 4522b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 4523b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 4524b47acb0aSGleb Smirnoff { 4525b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 4526b47acb0aSGleb Smirnoff uma_cache_t cache; 4527b47acb0aSGleb Smirnoff int i; 4528b47acb0aSGleb Smirnoff 4529b47acb0aSGleb Smirnoff 4530b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 4531b47acb0aSGleb Smirnoff zdom = &z->uz_domain[i]; 4532b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 4533b47acb0aSGleb Smirnoff } 4534b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 4535b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 4536b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 4537b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 4538c1685086SJeff Roberson uth->uth_xdomain = z->uz_xdomain; 45391de9724eSMark Johnston 4540b47acb0aSGleb Smirnoff /* 45411de9724eSMark Johnston * While it is not normally safe to access the cache bucket pointers 45421de9724eSMark Johnston * while not on the CPU that owns the cache, we only allow the pointers 45431de9724eSMark Johnston * to be exchanged without the zone lock held, not invalidated, so 45441de9724eSMark Johnston * accept the possible race associated with bucket exchange during 45451de9724eSMark Johnston * monitoring. Use atomic_load_ptr() to ensure that the bucket pointers 45461de9724eSMark Johnston * are loaded only once. 4547b47acb0aSGleb Smirnoff */ 4548b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 4549b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 4550b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 4551b47acb0aSGleb Smirnoff continue; 4552b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 4553376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 4554376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 4555376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 4556b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 4557b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 4558b47acb0aSGleb Smirnoff } 4559b47acb0aSGleb Smirnoff } 4560b47acb0aSGleb Smirnoff 45617a52a97eSRobert Watson static int 45627a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 45637a52a97eSRobert Watson { 45647a52a97eSRobert Watson struct uma_stream_header ush; 45657a52a97eSRobert Watson struct uma_type_header uth; 456663b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 45677a52a97eSRobert Watson struct sbuf sbuf; 45687a52a97eSRobert Watson uma_keg_t kz; 45697a52a97eSRobert Watson uma_zone_t z; 45704bd61e19SJeff Roberson uint64_t items; 45718b987a77SJeff Roberson uint32_t kfree, pages; 45724e657159SMatthew D Fleming int count, error, i; 45737a52a97eSRobert Watson 457400f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 457500f0e671SMatthew D Fleming if (error != 0) 457600f0e671SMatthew D Fleming return (error); 45774e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 45781eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 457963b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 45804e657159SMatthew D Fleming 4581404a593eSMatthew D Fleming count = 0; 4582111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 45837a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 45847a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 45857a52a97eSRobert Watson count++; 45867a52a97eSRobert Watson } 45877a52a97eSRobert Watson 4588b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4589b47acb0aSGleb Smirnoff count++; 4590b47acb0aSGleb Smirnoff 45917a52a97eSRobert Watson /* 45927a52a97eSRobert Watson * Insert stream header. 45937a52a97eSRobert Watson */ 45947a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 45957a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 4596ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 45977a52a97eSRobert Watson ush.ush_count = count; 45984e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 45997a52a97eSRobert Watson 46007a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 46018b987a77SJeff Roberson kfree = pages = 0; 46028b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 46038b987a77SJeff Roberson kfree += kz->uk_domain[i].ud_free; 46048b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 46058b987a77SJeff Roberson } 46067a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 46077a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 46087a52a97eSRobert Watson ZONE_LOCK(z); 4609cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 46107a52a97eSRobert Watson uth.uth_align = kz->uk_align; 46117a52a97eSRobert Watson uth.uth_size = kz->uk_size; 46127a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 46134bd61e19SJeff Roberson if (z->uz_max_items > 0) { 46144bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 46154bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 4616bb15d1c7SGleb Smirnoff kz->uk_ppera; 46174bd61e19SJeff Roberson } else 46188b987a77SJeff Roberson uth.uth_pages = pages; 4619f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 4620bb15d1c7SGleb Smirnoff kz->uk_ppera; 4621bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 46228b987a77SJeff Roberson uth.uth_keg_free = kfree; 4623cbbb4a00SRobert Watson 4624cbbb4a00SRobert Watson /* 4625cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 4626cbbb4a00SRobert Watson * on the keg's zone list. 4627cbbb4a00SRobert Watson */ 4628e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 4629cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 4630cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 4631b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 4632b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 46332450bbb8SRobert Watson ZONE_UNLOCK(z); 463463b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 463563b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 463663b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 46377a52a97eSRobert Watson } 46387a52a97eSRobert Watson } 4639b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 4640b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 4641b47acb0aSGleb Smirnoff ZONE_LOCK(z); 4642b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 4643b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 4644b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 4645b47acb0aSGleb Smirnoff ZONE_UNLOCK(z); 4646b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 4647b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 4648b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 4649b47acb0aSGleb Smirnoff } 4650b47acb0aSGleb Smirnoff 4651111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 46524e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 46534e657159SMatthew D Fleming sbuf_delete(&sbuf); 465463b5d112SKonstantin Belousov free(ups, M_TEMP); 46557a52a97eSRobert Watson return (error); 46567a52a97eSRobert Watson } 465748c5777eSRobert Watson 46580a5a3ccbSGleb Smirnoff int 46590a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 46600a5a3ccbSGleb Smirnoff { 46610a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 466216be9f54SGleb Smirnoff int error, max; 46630a5a3ccbSGleb Smirnoff 466416be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 46650a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 46660a5a3ccbSGleb Smirnoff if (error || !req->newptr) 46670a5a3ccbSGleb Smirnoff return (error); 46680a5a3ccbSGleb Smirnoff 46690a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 46700a5a3ccbSGleb Smirnoff 46710a5a3ccbSGleb Smirnoff return (0); 46720a5a3ccbSGleb Smirnoff } 46730a5a3ccbSGleb Smirnoff 46740a5a3ccbSGleb Smirnoff int 46750a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 46760a5a3ccbSGleb Smirnoff { 467720a4e154SJeff Roberson uma_zone_t zone; 46780a5a3ccbSGleb Smirnoff int cur; 46790a5a3ccbSGleb Smirnoff 468020a4e154SJeff Roberson /* 468120a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 468220a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 468320a4e154SJeff Roberson */ 468420a4e154SJeff Roberson if (arg2 == 0) 468520a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 468620a4e154SJeff Roberson else 468720a4e154SJeff Roberson zone = arg1; 46880a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 46890a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 46900a5a3ccbSGleb Smirnoff } 46910a5a3ccbSGleb Smirnoff 469220a4e154SJeff Roberson static int 469320a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 469420a4e154SJeff Roberson { 469520a4e154SJeff Roberson uma_zone_t zone = arg1; 469620a4e154SJeff Roberson uint64_t cur; 469720a4e154SJeff Roberson 469820a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 469920a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 470020a4e154SJeff Roberson } 470120a4e154SJeff Roberson 470220a4e154SJeff Roberson static int 470320a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 470420a4e154SJeff Roberson { 470520a4e154SJeff Roberson uma_zone_t zone = arg1; 470620a4e154SJeff Roberson uint64_t cur; 470720a4e154SJeff Roberson 470820a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 470920a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 471020a4e154SJeff Roberson } 471120a4e154SJeff Roberson 47126d204a6aSRyan Libby static int 47136d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 47146d204a6aSRyan Libby { 47156d204a6aSRyan Libby struct sbuf sbuf; 47166d204a6aSRyan Libby uma_zone_t zone = arg1; 47176d204a6aSRyan Libby int error; 47186d204a6aSRyan Libby 47196d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 47206d204a6aSRyan Libby if (zone->uz_flags != 0) 47216d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 47226d204a6aSRyan Libby else 47236d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 47246d204a6aSRyan Libby error = sbuf_finish(&sbuf); 47256d204a6aSRyan Libby sbuf_delete(&sbuf); 47266d204a6aSRyan Libby 47276d204a6aSRyan Libby return (error); 47286d204a6aSRyan Libby } 47296d204a6aSRyan Libby 4730f7af5015SRyan Libby static int 4731f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 4732f7af5015SRyan Libby { 4733f7af5015SRyan Libby uma_keg_t keg = arg1; 4734f7af5015SRyan Libby int avail, effpct, total; 4735f7af5015SRyan Libby 4736f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 4737f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_OFFPAGE) != 0) 4738f7af5015SRyan Libby total += slab_sizeof(SLAB_MAX_SETSIZE); 4739f7af5015SRyan Libby /* 4740f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 4741f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 4742f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 4743f7af5015SRyan Libby */ 4744f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 4745f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 4746f7af5015SRyan Libby avail *= mp_maxid + 1; 4747f7af5015SRyan Libby effpct = 100 * avail / total; 4748f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 4749f7af5015SRyan Libby } 4750f7af5015SRyan Libby 47514bd61e19SJeff Roberson static int 47524bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 47534bd61e19SJeff Roberson { 47544bd61e19SJeff Roberson uma_zone_t zone = arg1; 47554bd61e19SJeff Roberson uint64_t cur; 47564bd61e19SJeff Roberson 47574bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 47584bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 47594bd61e19SJeff Roberson } 47604bd61e19SJeff Roberson 47619542ea7bSGleb Smirnoff #ifdef INVARIANTS 47629542ea7bSGleb Smirnoff static uma_slab_t 47639542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 47649542ea7bSGleb Smirnoff { 47659542ea7bSGleb Smirnoff uma_slab_t slab; 47669542ea7bSGleb Smirnoff uma_keg_t keg; 47679542ea7bSGleb Smirnoff uint8_t *mem; 47689542ea7bSGleb Smirnoff 47699542ea7bSGleb Smirnoff /* 47709542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 47719542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 47729542ea7bSGleb Smirnoff * essentially holds a reference. 47739542ea7bSGleb Smirnoff */ 4774727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 4775727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 4776bb15d1c7SGleb Smirnoff return (NULL); 4777727c6918SJeff Roberson if (zone->uz_flags & UMA_ZONE_VTOSLAB) 4778727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 4779bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 4780727c6918SJeff Roberson if ((keg->uk_flags & UMA_ZONE_HASH) == 0) 4781727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 47828b987a77SJeff Roberson KEG_LOCK(keg, 0); 47839542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 47848b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 47859542ea7bSGleb Smirnoff 47869542ea7bSGleb Smirnoff return (slab); 47879542ea7bSGleb Smirnoff } 47889542ea7bSGleb Smirnoff 4789c5deaf04SGleb Smirnoff static bool 4790c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 4791c5deaf04SGleb Smirnoff { 4792c5deaf04SGleb Smirnoff 4793727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 4794c5deaf04SGleb Smirnoff return (true); 4795c5deaf04SGleb Smirnoff 4796bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 4797c5deaf04SGleb Smirnoff } 4798c5deaf04SGleb Smirnoff 4799c5deaf04SGleb Smirnoff static bool 4800c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 4801c5deaf04SGleb Smirnoff { 4802c5deaf04SGleb Smirnoff uintptr_t idx; 4803c5deaf04SGleb Smirnoff 4804c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 4805c5deaf04SGleb Smirnoff return (true); 4806c5deaf04SGleb Smirnoff 4807c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 4808c5deaf04SGleb Smirnoff return (false); 4809c5deaf04SGleb Smirnoff 4810c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 4811c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 4812c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 4813c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 4814c5deaf04SGleb Smirnoff } 4815c5deaf04SGleb Smirnoff 4816c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 4817c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 4818c5deaf04SGleb Smirnoff return (true); 4819c5deaf04SGleb Smirnoff } 4820c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 4821c5deaf04SGleb Smirnoff 4822c5deaf04SGleb Smirnoff return (false); 4823c5deaf04SGleb Smirnoff } 4824c5deaf04SGleb Smirnoff 48259542ea7bSGleb Smirnoff /* 48269542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 48279542ea7bSGleb Smirnoff * 48289542ea7bSGleb Smirnoff */ 48299542ea7bSGleb Smirnoff static void 48309542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 48319542ea7bSGleb Smirnoff { 48329542ea7bSGleb Smirnoff uma_keg_t keg; 48339542ea7bSGleb Smirnoff int freei; 48349542ea7bSGleb Smirnoff 48359542ea7bSGleb Smirnoff if (slab == NULL) { 48369542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 48379542ea7bSGleb Smirnoff if (slab == NULL) 48389542ea7bSGleb Smirnoff panic("uma: item %p did not belong to zone %s\n", 48399542ea7bSGleb Smirnoff item, zone->uz_name); 48409542ea7bSGleb Smirnoff } 4841584061b4SJeff Roberson keg = zone->uz_keg; 48421e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 48439542ea7bSGleb Smirnoff 4844815db204SRyan Libby if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 48459542ea7bSGleb Smirnoff panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n", 48469542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 4847815db204SRyan Libby BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 48489542ea7bSGleb Smirnoff } 48499542ea7bSGleb Smirnoff 48509542ea7bSGleb Smirnoff /* 48519542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 48529542ea7bSGleb Smirnoff * and duplicate frees. 48539542ea7bSGleb Smirnoff * 48549542ea7bSGleb Smirnoff */ 48559542ea7bSGleb Smirnoff static void 48569542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 48579542ea7bSGleb Smirnoff { 48589542ea7bSGleb Smirnoff uma_keg_t keg; 48599542ea7bSGleb Smirnoff int freei; 48609542ea7bSGleb Smirnoff 48619542ea7bSGleb Smirnoff if (slab == NULL) { 48629542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 48639542ea7bSGleb Smirnoff if (slab == NULL) 48649542ea7bSGleb Smirnoff panic("uma: Freed item %p did not belong to zone %s\n", 48659542ea7bSGleb Smirnoff item, zone->uz_name); 48669542ea7bSGleb Smirnoff } 4867584061b4SJeff Roberson keg = zone->uz_keg; 48681e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 48699542ea7bSGleb Smirnoff 48709542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 48719542ea7bSGleb Smirnoff panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n", 48729542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48739542ea7bSGleb Smirnoff 48741e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 48759542ea7bSGleb Smirnoff panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n", 48769542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48779542ea7bSGleb Smirnoff 4878815db204SRyan Libby if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 48799542ea7bSGleb Smirnoff panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n", 48809542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48819542ea7bSGleb Smirnoff 4882815db204SRyan Libby BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 48839542ea7bSGleb Smirnoff } 48849542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 48859542ea7bSGleb Smirnoff 488648c5777eSRobert Watson #ifdef DDB 488746d70077SConrad Meyer static int64_t 488846d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 48890223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 489048c5777eSRobert Watson { 489146d70077SConrad Meyer uint64_t frees; 48920f9b7bf3SMark Johnston int i; 489348c5777eSRobert Watson 489448c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 489546d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 48962efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 489746d70077SConrad Meyer *sleeps = z->uz_sleeps; 489846d70077SConrad Meyer *cachefree = 0; 489946d70077SConrad Meyer *xdomain = 0; 490048c5777eSRobert Watson } else 490146d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 490246d70077SConrad Meyer xdomain); 49038b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 49048b987a77SJeff Roberson *cachefree += z->uz_domain[i].uzd_nitems; 4905e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 490648c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 49078b987a77SJeff Roberson *cachefree += kz->uk_domain[i].ud_free; 49088b987a77SJeff Roberson } 490946d70077SConrad Meyer *used = *allocs - frees; 491046d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 491146d70077SConrad Meyer } 49120f9b7bf3SMark Johnston 491346d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 491446d70077SConrad Meyer { 491546d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 491646d70077SConrad Meyer uma_keg_t kz; 491746d70077SConrad Meyer uma_zone_t z; 491846d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 491946d70077SConrad Meyer long cachefree; 492046d70077SConrad Meyer /* variables for sorting */ 492146d70077SConrad Meyer uma_keg_t cur_keg; 492246d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 492346d70077SConrad Meyer int64_t cur_size, last_size, size; 492446d70077SConrad Meyer int ties; 492546d70077SConrad Meyer 492646d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 492746d70077SConrad Meyer if (modif[0] == 'i') { 492846d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 492946d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 493046d70077SConrad Meyer } else { 493146d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 493246d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 493346d70077SConrad Meyer } 493446d70077SConrad Meyer 493546d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 493646d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 493746d70077SConrad Meyer 493846d70077SConrad Meyer /* Sort the zones with largest size first. */ 493946d70077SConrad Meyer last_zone = NULL; 494046d70077SConrad Meyer last_size = INT64_MAX; 494146d70077SConrad Meyer for (;;) { 494246d70077SConrad Meyer cur_zone = NULL; 494346d70077SConrad Meyer cur_size = -1; 494446d70077SConrad Meyer ties = 0; 494546d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 494646d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 494746d70077SConrad Meyer /* 494846d70077SConrad Meyer * In the case of size ties, print out zones 494946d70077SConrad Meyer * in the order they are encountered. That is, 495046d70077SConrad Meyer * when we encounter the most recently output 495146d70077SConrad Meyer * zone, we have already printed all preceding 495246d70077SConrad Meyer * ties, and we must print all following ties. 495346d70077SConrad Meyer */ 495446d70077SConrad Meyer if (z == last_zone) { 495546d70077SConrad Meyer ties = 1; 495646d70077SConrad Meyer continue; 495746d70077SConrad Meyer } 495846d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 495946d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 496046d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 496146d70077SConrad Meyer { 496246d70077SConrad Meyer cur_size = size; 496346d70077SConrad Meyer cur_zone = z; 496446d70077SConrad Meyer cur_keg = kz; 496546d70077SConrad Meyer } 496646d70077SConrad Meyer } 496746d70077SConrad Meyer } 496846d70077SConrad Meyer if (cur_zone == NULL) 496946d70077SConrad Meyer break; 497046d70077SConrad Meyer 497146d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 497246d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 497346d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 497446d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 497546d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 497620a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 497720a4e154SJeff Roberson xdomain); 497846d70077SConrad Meyer 4979687c94aaSJohn Baldwin if (db_pager_quit) 4980687c94aaSJohn Baldwin return; 498146d70077SConrad Meyer last_zone = cur_zone; 498246d70077SConrad Meyer last_size = cur_size; 498348c5777eSRobert Watson } 498448c5777eSRobert Watson } 498503175483SAlexander Motin 498603175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 498703175483SAlexander Motin { 498803175483SAlexander Motin uma_zone_t z; 4989ab3185d1SJeff Roberson uint64_t allocs, frees; 49900f9b7bf3SMark Johnston long cachefree; 49910f9b7bf3SMark Johnston int i; 499203175483SAlexander Motin 499303175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 499403175483SAlexander Motin "Requests", "Bucket"); 499503175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 4996c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 49970f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 49980f9b7bf3SMark Johnston cachefree += z->uz_domain[i].uzd_nitems; 49990f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 500003175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 500103175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 500220a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 500303175483SAlexander Motin if (db_pager_quit) 500403175483SAlexander Motin return; 500503175483SAlexander Motin } 500603175483SAlexander Motin } 50079542ea7bSGleb Smirnoff #endif /* DDB */ 5008