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; 7433b2f2cb8SAlexander Motin u_int slabs; 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; 749e20a199fSJeff Roberson KEG_LOCK(keg); 7508355f576SJeff Roberson /* 751e20a199fSJeff Roberson * Expand the keg hash table. 7528355f576SJeff Roberson * 7538355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 7548355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 7558355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 7568355f576SJeff Roberson */ 757099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH && 7583b2f2cb8SAlexander Motin (slabs = keg->uk_pages / keg->uk_ppera) > 7593b2f2cb8SAlexander Motin keg->uk_hash.uh_hashsize) { 7600aef6126SJeff Roberson struct uma_hash newhash; 7610aef6126SJeff Roberson struct uma_hash oldhash; 7620aef6126SJeff Roberson int ret; 7635300d9ddSJeff Roberson 7640aef6126SJeff Roberson /* 7650aef6126SJeff Roberson * This is so involved because allocating and freeing 766e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 7670aef6126SJeff Roberson * I have to do everything in stages and check for 7680aef6126SJeff Roberson * races. 7690aef6126SJeff Roberson */ 770e20a199fSJeff Roberson KEG_UNLOCK(keg); 7713b2f2cb8SAlexander Motin ret = hash_alloc(&newhash, 1 << fls(slabs)); 772e20a199fSJeff Roberson KEG_LOCK(keg); 7730aef6126SJeff Roberson if (ret) { 774099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 775099a0e58SBosko Milekic oldhash = keg->uk_hash; 776099a0e58SBosko Milekic keg->uk_hash = newhash; 7770aef6126SJeff Roberson } else 7780aef6126SJeff Roberson oldhash = newhash; 7790aef6126SJeff Roberson 780e20a199fSJeff Roberson KEG_UNLOCK(keg); 7810aef6126SJeff Roberson hash_free(&oldhash); 782a1dff920SDavide Italiano return; 7830aef6126SJeff Roberson } 7845300d9ddSJeff Roberson } 78508cfa56eSMark Johnston KEG_UNLOCK(keg); 786e20a199fSJeff Roberson 78708034d10SKonstantin Belousov update_wss: 78808cfa56eSMark Johnston ZONE_LOCK(zone); 789bb15d1c7SGleb Smirnoff for (int i = 0; i < vm_ndomains; i++) 7900f9b7bf3SMark Johnston zone_domain_update_wss(&zone->uz_domain[i]); 79108cfa56eSMark Johnston ZONE_UNLOCK(zone); 7928355f576SJeff Roberson } 7938355f576SJeff Roberson 7948355f576SJeff Roberson /* 7955300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 7965300d9ddSJeff Roberson * backing store. 7975300d9ddSJeff Roberson * 7985300d9ddSJeff Roberson * Arguments: 7990aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 8005300d9ddSJeff Roberson * 8015300d9ddSJeff Roberson * Returns: 802763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 8035300d9ddSJeff Roberson */ 80437c84183SPoul-Henning Kamp static int 8053b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 8065300d9ddSJeff Roberson { 80759568a0eSAlexander Motin size_t alloc; 8085300d9ddSJeff Roberson 8093b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 8103b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 8113b2f2cb8SAlexander Motin hash->uh_hashsize = size; 8120aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 8131e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 8145300d9ddSJeff Roberson } else { 8150aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 816e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 817ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 8180aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 8195300d9ddSJeff Roberson } 8200aef6126SJeff Roberson if (hash->uh_slab_hash) { 8210aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 8220aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 8230aef6126SJeff Roberson return (1); 8240aef6126SJeff Roberson } 8255300d9ddSJeff Roberson 8260aef6126SJeff Roberson return (0); 8275300d9ddSJeff Roberson } 8285300d9ddSJeff Roberson 8295300d9ddSJeff Roberson /* 83064f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 83164f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 83264f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 8338355f576SJeff Roberson * 8348355f576SJeff Roberson * Arguments: 8350aef6126SJeff Roberson * oldhash The hash you want to expand 8360aef6126SJeff Roberson * newhash The hash structure for the new table 8378355f576SJeff Roberson * 8388355f576SJeff Roberson * Returns: 8398355f576SJeff Roberson * Nothing 8408355f576SJeff Roberson * 8418355f576SJeff Roberson * Discussion: 8428355f576SJeff Roberson */ 8430aef6126SJeff Roberson static int 8440aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 8458355f576SJeff Roberson { 8461e0701e1SJeff Roberson uma_hash_slab_t slab; 8476929b7d1SPedro F. Giffuni u_int hval; 8486929b7d1SPedro F. Giffuni u_int idx; 8498355f576SJeff Roberson 8500aef6126SJeff Roberson if (!newhash->uh_slab_hash) 8510aef6126SJeff Roberson return (0); 8528355f576SJeff Roberson 8530aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 8540aef6126SJeff Roberson return (0); 8558355f576SJeff Roberson 8568355f576SJeff Roberson /* 8578355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 8588355f576SJeff Roberson * full rehash. 8598355f576SJeff Roberson */ 8608355f576SJeff Roberson 8616929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 8621e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 8631e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 8641e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 8651e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 8661e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 8671e0701e1SJeff Roberson slab, uhs_hlink); 8688355f576SJeff Roberson } 8698355f576SJeff Roberson 8700aef6126SJeff Roberson return (1); 8719c2cd7e5SJeff Roberson } 8729c2cd7e5SJeff Roberson 8735300d9ddSJeff Roberson /* 8745300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 8755300d9ddSJeff Roberson * 8765300d9ddSJeff Roberson * Arguments: 8775300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 8785300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 8795300d9ddSJeff Roberson * 8805300d9ddSJeff Roberson * Returns: 8815300d9ddSJeff Roberson * Nothing 8825300d9ddSJeff Roberson */ 8839c2cd7e5SJeff Roberson static void 8840aef6126SJeff Roberson hash_free(struct uma_hash *hash) 8859c2cd7e5SJeff Roberson { 8860aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 8870aef6126SJeff Roberson return; 8880aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 8890095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 8908355f576SJeff Roberson else 891961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 8928355f576SJeff Roberson } 8938355f576SJeff Roberson 8948355f576SJeff Roberson /* 8958355f576SJeff Roberson * Frees all outstanding items in a bucket 8968355f576SJeff Roberson * 8978355f576SJeff Roberson * Arguments: 8988355f576SJeff Roberson * zone The zone to free to, must be unlocked. 8994bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 9008355f576SJeff Roberson * 9018355f576SJeff Roberson * Returns: 9028355f576SJeff Roberson * Nothing 9038355f576SJeff Roberson */ 9048355f576SJeff Roberson 9058355f576SJeff Roberson static void 9068355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 9078355f576SJeff Roberson { 9080095a784SJeff Roberson int i; 9098355f576SJeff Roberson 9104bd61e19SJeff Roberson if (bucket == NULL || bucket->ub_cnt == 0) 9118355f576SJeff Roberson return; 9128355f576SJeff Roberson 9130095a784SJeff Roberson if (zone->uz_fini) 9140095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 9150095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 9160095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 9174bd61e19SJeff Roberson if (zone->uz_max_items > 0) 9184bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 9190095a784SJeff Roberson bucket->ub_cnt = 0; 9208355f576SJeff Roberson } 9218355f576SJeff Roberson 9228355f576SJeff Roberson /* 9238355f576SJeff Roberson * Drains the per cpu caches for a zone. 9248355f576SJeff Roberson * 925*727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 9265d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 9275d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 9285d1ae027SRobert Watson * 9298355f576SJeff Roberson * Arguments: 9308355f576SJeff Roberson * zone The zone to drain, must be unlocked. 9318355f576SJeff Roberson * 9328355f576SJeff Roberson * Returns: 9338355f576SJeff Roberson * Nothing 9348355f576SJeff Roberson */ 9358355f576SJeff Roberson static void 9369643769aSJeff Roberson cache_drain(uma_zone_t zone) 9378355f576SJeff Roberson { 9388355f576SJeff Roberson uma_cache_t cache; 939376b1ba3SJeff Roberson uma_bucket_t bucket; 9408355f576SJeff Roberson int cpu; 9418355f576SJeff Roberson 9428355f576SJeff Roberson /* 9435d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 9445d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 9455d1ae027SRobert Watson * of the caches at this point. 9465d1ae027SRobert Watson * 9475d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 9485d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 9495d1ae027SRobert Watson * 95008cfa56eSMark Johnston * XXX: We lock the zone before passing into bucket_cache_reclaim() as 9515d1ae027SRobert Watson * it is used elsewhere. Should the tear-down path be made special 9525d1ae027SRobert Watson * there in some form? 9538355f576SJeff Roberson */ 9543aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 9558355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 956376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 957376b1ba3SJeff Roberson if (bucket != NULL) { 958376b1ba3SJeff Roberson bucket_drain(zone, bucket); 959376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 960376b1ba3SJeff Roberson } 961376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 962376b1ba3SJeff Roberson if (bucket != NULL) { 963376b1ba3SJeff Roberson bucket_drain(zone, bucket); 964376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 965376b1ba3SJeff Roberson } 966376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 967376b1ba3SJeff Roberson if (bucket != NULL) { 968376b1ba3SJeff Roberson bucket_drain(zone, bucket); 969376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 970376b1ba3SJeff Roberson } 971d56368d7SBosko Milekic } 972aaa8bb16SJeff Roberson ZONE_LOCK(zone); 97308cfa56eSMark Johnston bucket_cache_reclaim(zone, true); 974aaa8bb16SJeff Roberson ZONE_UNLOCK(zone); 975aaa8bb16SJeff Roberson } 976aaa8bb16SJeff Roberson 977a2de44abSAlexander Motin static void 97820a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 979a2de44abSAlexander Motin { 980a2de44abSAlexander Motin 981a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 982a2de44abSAlexander Motin return; 983a2de44abSAlexander Motin 984a2de44abSAlexander Motin ZONE_LOCK(zone); 98520a4e154SJeff Roberson zone->uz_bucket_size = 98620a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 987a2de44abSAlexander Motin ZONE_UNLOCK(zone); 988a2de44abSAlexander Motin } 989a2de44abSAlexander Motin 990a2de44abSAlexander Motin static void 99120a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 992a2de44abSAlexander Motin { 993a2de44abSAlexander Motin uma_cache_t cache; 994c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 995ab3185d1SJeff Roberson int domain; 996a2de44abSAlexander Motin 997a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 998a2de44abSAlexander Motin return; 999a2de44abSAlexander Motin 1000c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1001a2de44abSAlexander Motin ZONE_LOCK(zone); 1002a2de44abSAlexander Motin critical_enter(); 1003ab3185d1SJeff Roberson if (zone->uz_flags & UMA_ZONE_NUMA) 1004ab3185d1SJeff Roberson domain = PCPU_GET(domain); 1005ab3185d1SJeff Roberson else 1006ab3185d1SJeff Roberson domain = 0; 1007a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1008376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1009376b1ba3SJeff Roberson if (b1 != NULL && b1->ub_cnt != 0) { 1010376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b1, false); 1011376b1ba3SJeff Roberson b1 = NULL; 1012a2de44abSAlexander Motin } 1013376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1014376b1ba3SJeff Roberson if (b2 != NULL && b2->ub_cnt != 0) { 1015376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b2, false); 1016376b1ba3SJeff Roberson b2 = NULL; 1017a2de44abSAlexander Motin } 1018376b1ba3SJeff Roberson b3 = cache_bucket_unload_cross(cache); 1019a2de44abSAlexander Motin critical_exit(); 1020a2de44abSAlexander Motin ZONE_UNLOCK(zone); 10218a8d9d14SAlexander Motin if (b1) 10228a8d9d14SAlexander Motin bucket_free(zone, b1, NULL); 10238a8d9d14SAlexander Motin if (b2) 10248a8d9d14SAlexander Motin bucket_free(zone, b2, NULL); 1025c1685086SJeff Roberson if (b3) { 1026c1685086SJeff Roberson bucket_drain(zone, b3); 1027c1685086SJeff Roberson bucket_free(zone, b3, NULL); 1028c1685086SJeff Roberson } 1029a2de44abSAlexander Motin } 1030a2de44abSAlexander Motin 1031a2de44abSAlexander Motin /* 1032a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1033a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1034a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1035a2de44abSAlexander Motin * to safely access their cache buckets. 1036a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1037a2de44abSAlexander Motin */ 1038a2de44abSAlexander Motin static void 103908cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1040a2de44abSAlexander Motin { 1041a2de44abSAlexander Motin int cpu; 1042a2de44abSAlexander Motin 1043a2de44abSAlexander Motin /* 1044*727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1045a2de44abSAlexander Motin */ 1046a2de44abSAlexander Motin if (zone) 104720a4e154SJeff Roberson cache_shrink(zone, NULL); 1048a2de44abSAlexander Motin else 104920a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1050a2de44abSAlexander Motin 1051a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1052a2de44abSAlexander Motin thread_lock(curthread); 1053a2de44abSAlexander Motin sched_bind(curthread, cpu); 1054a2de44abSAlexander Motin thread_unlock(curthread); 1055a2de44abSAlexander Motin 1056a2de44abSAlexander Motin if (zone) 105720a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1058a2de44abSAlexander Motin else 105920a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1060a2de44abSAlexander Motin } 1061a2de44abSAlexander Motin thread_lock(curthread); 1062a2de44abSAlexander Motin sched_unbind(curthread); 1063a2de44abSAlexander Motin thread_unlock(curthread); 1064a2de44abSAlexander Motin } 1065a2de44abSAlexander Motin 1066aaa8bb16SJeff Roberson /* 106708cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 106808cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 106908cfa56eSMark Johnston * estimated working set size. 1070aaa8bb16SJeff Roberson */ 1071aaa8bb16SJeff Roberson static void 107208cfa56eSMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain) 1073aaa8bb16SJeff Roberson { 1074ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1075aaa8bb16SJeff Roberson uma_bucket_t bucket; 107608cfa56eSMark Johnston long target, tofree; 1077ab3185d1SJeff Roberson int i; 10788355f576SJeff Roberson 1079ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 1080ab3185d1SJeff Roberson zdom = &zone->uz_domain[i]; 108108cfa56eSMark Johnston 108208cfa56eSMark Johnston /* 108308cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 108408cfa56eSMark Johnston * this bucket cache is empty. Otherwise, we reclaim items in 108508cfa56eSMark Johnston * excess of the zone's estimated working set size. If the 108608cfa56eSMark Johnston * difference nitems - imin is larger than the WSS estimate, 108708cfa56eSMark Johnston * then the estimate will grow at the end of this interval and 108808cfa56eSMark Johnston * we ignore the historical average. 108908cfa56eSMark Johnston */ 109008cfa56eSMark Johnston target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems - 109108cfa56eSMark Johnston zdom->uzd_imin); 109208cfa56eSMark Johnston while (zdom->uzd_nitems > target) { 109308cfa56eSMark Johnston bucket = TAILQ_LAST(&zdom->uzd_buckets, uma_bucketlist); 109408cfa56eSMark Johnston if (bucket == NULL) 109508cfa56eSMark Johnston break; 109608cfa56eSMark Johnston tofree = bucket->ub_cnt; 109708cfa56eSMark Johnston TAILQ_REMOVE(&zdom->uzd_buckets, bucket, ub_link); 109808cfa56eSMark Johnston zdom->uzd_nitems -= tofree; 109908cfa56eSMark Johnston 110008cfa56eSMark Johnston /* 110108cfa56eSMark Johnston * Shift the bounds of the current WSS interval to avoid 110208cfa56eSMark Johnston * perturbing the estimate. 110308cfa56eSMark Johnston */ 110408cfa56eSMark Johnston zdom->uzd_imax -= lmin(zdom->uzd_imax, tofree); 110508cfa56eSMark Johnston zdom->uzd_imin -= lmin(zdom->uzd_imin, tofree); 110608cfa56eSMark Johnston 11078355f576SJeff Roberson ZONE_UNLOCK(zone); 11088355f576SJeff Roberson bucket_drain(zone, bucket); 11096fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 11108355f576SJeff Roberson ZONE_LOCK(zone); 11118355f576SJeff Roberson } 1112ab3185d1SJeff Roberson } 1113ace66b56SAlexander Motin 1114ace66b56SAlexander Motin /* 111508cfa56eSMark Johnston * Shrink the zone bucket size to ensure that the per-CPU caches 111608cfa56eSMark Johnston * don't grow too large. 1117ace66b56SAlexander Motin */ 111820a4e154SJeff Roberson if (zone->uz_bucket_size > zone->uz_bucket_size_min) 111920a4e154SJeff Roberson zone->uz_bucket_size--; 11208355f576SJeff Roberson } 1121fc03d22bSJeff Roberson 1122fc03d22bSJeff Roberson static void 1123fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1124fc03d22bSJeff Roberson { 1125fc03d22bSJeff Roberson uint8_t *mem; 1126fc03d22bSJeff Roberson int i; 1127fc03d22bSJeff Roberson uint8_t flags; 1128fc03d22bSJeff Roberson 11291431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 11301431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 11311431a748SGleb Smirnoff 11321e0701e1SJeff Roberson mem = slab_data(slab, keg); 1133fc03d22bSJeff Roberson flags = slab->us_flags; 1134fc03d22bSJeff Roberson i = start; 1135fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 1136fc03d22bSJeff Roberson for (i--; i > -1; i--) 1137c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1138c5deaf04SGleb Smirnoff /* 1139c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1140c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1141c5deaf04SGleb Smirnoff * which executed trash_dtor. 1142c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1143c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1144c5deaf04SGleb Smirnoff * invocations. 1145c5deaf04SGleb Smirnoff */ 11461e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1147c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1148c5deaf04SGleb Smirnoff #endif 11491e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1150fc03d22bSJeff Roberson } 1151fc03d22bSJeff Roberson if (keg->uk_flags & UMA_ZONE_OFFPAGE) 1152fc03d22bSJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 1153fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 11542e47807cSJeff Roberson uma_total_dec(PAGE_SIZE * keg->uk_ppera); 11558355f576SJeff Roberson } 11568355f576SJeff Roberson 11578355f576SJeff Roberson /* 1158e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 11598355f576SJeff Roberson * the pageout daemon. 11608355f576SJeff Roberson * 1161e20a199fSJeff Roberson * Returns nothing. 11628355f576SJeff Roberson */ 1163e20a199fSJeff Roberson static void 1164e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 11658355f576SJeff Roberson { 11661e183df2SStefan Farfeleder struct slabhead freeslabs = { 0 }; 1167ab3185d1SJeff Roberson uma_domain_t dom; 1168829be516SMark Johnston uma_slab_t slab, tmp; 1169ab3185d1SJeff Roberson int i; 11708355f576SJeff Roberson 11718355f576SJeff Roberson /* 1172e20a199fSJeff Roberson * We don't want to take pages from statically allocated kegs at this 11738355f576SJeff Roberson * time 11748355f576SJeff Roberson */ 1175099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 11768355f576SJeff Roberson return; 11778355f576SJeff Roberson 11781431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_drain %s(%p) free items: %u", 11791431a748SGleb Smirnoff keg->uk_name, keg, keg->uk_free); 1180e20a199fSJeff Roberson KEG_LOCK(keg); 1181099a0e58SBosko Milekic if (keg->uk_free == 0) 11828355f576SJeff Roberson goto finished; 11838355f576SJeff Roberson 1184ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 1185ab3185d1SJeff Roberson dom = &keg->uk_domain[i]; 1186ab3185d1SJeff Roberson LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) { 1187829be516SMark Johnston /* We have nowhere to free these to. */ 1188829be516SMark Johnston if (slab->us_flags & UMA_SLAB_BOOT) 11898355f576SJeff Roberson continue; 11908355f576SJeff Roberson 11918355f576SJeff Roberson LIST_REMOVE(slab, us_link); 1192099a0e58SBosko Milekic keg->uk_pages -= keg->uk_ppera; 1193099a0e58SBosko Milekic keg->uk_free -= keg->uk_ipers; 1194713deb36SJeff Roberson 1195099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 11961e0701e1SJeff Roberson UMA_HASH_REMOVE(&keg->uk_hash, slab); 1197713deb36SJeff Roberson 11981e0701e1SJeff Roberson LIST_INSERT_HEAD(&freeslabs, slab, us_link); 1199713deb36SJeff Roberson } 1200ab3185d1SJeff Roberson } 1201ab3185d1SJeff Roberson 1202713deb36SJeff Roberson finished: 1203e20a199fSJeff Roberson KEG_UNLOCK(keg); 1204713deb36SJeff Roberson 12051e0701e1SJeff Roberson while ((slab = LIST_FIRST(&freeslabs)) != NULL) { 12061e0701e1SJeff Roberson LIST_REMOVE(slab, us_link); 12071645995bSKirk McKusick keg_free_slab(keg, slab, keg->uk_ipers); 12088355f576SJeff Roberson } 12098355f576SJeff Roberson } 12108355f576SJeff Roberson 1211e20a199fSJeff Roberson static void 121208cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain) 1213e20a199fSJeff Roberson { 1214e20a199fSJeff Roberson 12158355f576SJeff Roberson /* 1216e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 1217e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 1218e20a199fSJeff Roberson * is the only call that knows the structure will still be available 1219e20a199fSJeff Roberson * when it wakes up. 1220e20a199fSJeff Roberson */ 1221e20a199fSJeff Roberson ZONE_LOCK(zone); 122208cfa56eSMark Johnston while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) { 1223e20a199fSJeff Roberson if (waitok == M_NOWAIT) 1224e20a199fSJeff Roberson goto out; 1225*727c6918SJeff Roberson msleep(zone, &zone->uz_lock, PVM, "zonedrain", 1); 1226e20a199fSJeff Roberson } 122708cfa56eSMark Johnston zone->uz_flags |= UMA_ZFLAG_RECLAIMING; 122808cfa56eSMark Johnston bucket_cache_reclaim(zone, drain); 1229e20a199fSJeff Roberson ZONE_UNLOCK(zone); 123008cfa56eSMark Johnston 1231e20a199fSJeff Roberson /* 1232e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 1233111fbcd5SBryan Venteicher * we're running. Normally the uma_rwlock would protect us but we 1234e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 1235e20a199fSJeff Roberson */ 123608034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1237bb15d1c7SGleb Smirnoff keg_drain(zone->uz_keg); 1238e20a199fSJeff Roberson ZONE_LOCK(zone); 123908cfa56eSMark Johnston zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING; 1240e20a199fSJeff Roberson wakeup(zone); 1241e20a199fSJeff Roberson out: 1242e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1243e20a199fSJeff Roberson } 1244e20a199fSJeff Roberson 124508cfa56eSMark Johnston static void 124620a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused) 1247e20a199fSJeff Roberson { 1248e20a199fSJeff Roberson 124908cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, true); 125008cfa56eSMark Johnston } 125108cfa56eSMark Johnston 125208cfa56eSMark Johnston static void 125320a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused) 125408cfa56eSMark Johnston { 125508cfa56eSMark Johnston 125608cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, false); 1257e20a199fSJeff Roberson } 1258e20a199fSJeff Roberson 1259e20a199fSJeff Roberson /* 1260e20a199fSJeff Roberson * Allocate a new slab for a keg. This does not insert the slab onto a list. 1261*727c6918SJeff Roberson * The keg should be locked on entry and will be dropped and reacquired on 1262*727c6918SJeff Roberson * return. 12638355f576SJeff Roberson * 12648355f576SJeff Roberson * Arguments: 126586220393SMark Johnston * flags Wait flags for the item initialization routine 126686220393SMark Johnston * aflags Wait flags for the slab allocation 12678355f576SJeff Roberson * 12688355f576SJeff Roberson * Returns: 12698355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 12708355f576SJeff Roberson * caller specified M_NOWAIT. 12718355f576SJeff Roberson */ 12728355f576SJeff Roberson static uma_slab_t 127386220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 127486220393SMark Johnston int aflags) 12758355f576SJeff Roberson { 1276e20a199fSJeff Roberson uma_alloc allocf; 1277099a0e58SBosko Milekic uma_slab_t slab; 12782e47807cSJeff Roberson unsigned long size; 127985dcf349SGleb Smirnoff uint8_t *mem; 128086220393SMark Johnston uint8_t sflags; 12818355f576SJeff Roberson int i; 12828355f576SJeff Roberson 1283ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1284ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1285bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 1286e20a199fSJeff Roberson allocf = keg->uk_allocf; 1287e20a199fSJeff Roberson KEG_UNLOCK(keg); 1288a553d4b8SJeff Roberson 1289194a979eSMark Johnston slab = NULL; 1290194a979eSMark Johnston mem = NULL; 1291099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_OFFPAGE) { 129286220393SMark Johnston slab = zone_alloc_item(keg->uk_slabzone, NULL, domain, aflags); 1293fc03d22bSJeff Roberson if (slab == NULL) 1294*727c6918SJeff Roberson goto fail; 1295a553d4b8SJeff Roberson } 1296a553d4b8SJeff Roberson 12973370c5bfSJeff Roberson /* 12983370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 12993370c5bfSJeff Roberson * first time they are added to a zone. 13003370c5bfSJeff Roberson * 13013370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 13023370c5bfSJeff Roberson */ 13033370c5bfSJeff Roberson 1304099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 130586220393SMark Johnston aflags |= M_ZERO; 13063370c5bfSJeff Roberson else 130786220393SMark Johnston aflags &= ~M_ZERO; 13083370c5bfSJeff Roberson 1309263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 131086220393SMark Johnston aflags |= M_NODUMP; 1311263811f7SKip Macy 1312e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1313194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 131486220393SMark Johnston mem = allocf(zone, size, domain, &sflags, aflags); 1315a553d4b8SJeff Roberson if (mem == NULL) { 1316b23f72e9SBrian Feldman if (keg->uk_flags & UMA_ZONE_OFFPAGE) 13170095a784SJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 1318*727c6918SJeff Roberson goto fail; 1319a553d4b8SJeff Roberson } 13202e47807cSJeff Roberson uma_total_inc(size); 13218355f576SJeff Roberson 13225c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 1323099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) 1324099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 13251e0701e1SJeff Roberson else 13261e0701e1SJeff Roberson ((uma_hash_slab_t)slab)->uhs_data = mem; 13275c0e403bSJeff Roberson 1328e20a199fSJeff Roberson if (keg->uk_flags & UMA_ZONE_VTOSLAB) 1329099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1330584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1331584061b4SJeff Roberson zone, slab); 13328355f576SJeff Roberson 1333099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 133486220393SMark Johnston slab->us_flags = sflags; 1335ab3185d1SJeff Roberson slab->us_domain = domain; 13369b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1337ef72505eSJeff Roberson #ifdef INVARIANTS 1338815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1339ef72505eSJeff Roberson #endif 1340099a0e58SBosko Milekic 1341b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1342099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 13431e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 134486220393SMark Johnston keg->uk_size, flags) != 0) 1345b23f72e9SBrian Feldman break; 1346b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1347fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1348*727c6918SJeff Roberson goto fail; 1349b23f72e9SBrian Feldman } 1350b23f72e9SBrian Feldman } 1351e20a199fSJeff Roberson KEG_LOCK(keg); 13525c0e403bSJeff Roberson 13531431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 13541431a748SGleb Smirnoff slab, keg->uk_name, keg); 13551431a748SGleb Smirnoff 1356099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 1357099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 13588355f576SJeff Roberson 1359099a0e58SBosko Milekic keg->uk_pages += keg->uk_ppera; 1360099a0e58SBosko Milekic keg->uk_free += keg->uk_ipers; 13618355f576SJeff Roberson 13628355f576SJeff Roberson return (slab); 1363*727c6918SJeff Roberson 1364*727c6918SJeff Roberson fail: 1365*727c6918SJeff Roberson KEG_LOCK(keg); 1366*727c6918SJeff Roberson return (NULL); 13678355f576SJeff Roberson } 13688355f576SJeff Roberson 13698355f576SJeff Roberson /* 1370009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1371009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1372009b6fcbSJeff Roberson * the VM is ready. 1373009b6fcbSJeff Roberson */ 1374009b6fcbSJeff Roberson static void * 1375ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1376ab3185d1SJeff Roberson int wait) 1377009b6fcbSJeff Roberson { 1378099a0e58SBosko Milekic uma_keg_t keg; 1379ac0a6fd0SGleb Smirnoff void *mem; 1380ac0a6fd0SGleb Smirnoff int pages; 1381099a0e58SBosko Milekic 1382bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1383009b6fcbSJeff Roberson /* 1384f7d35785SGleb Smirnoff * If we are in BOOT_BUCKETS or higher, than switch to real 1385f7d35785SGleb Smirnoff * allocator. Zones with page sized slabs switch at BOOT_PAGEALLOC. 1386009b6fcbSJeff Roberson */ 1387f7d35785SGleb Smirnoff switch (booted) { 1388f7d35785SGleb Smirnoff case BOOT_COLD: 1389f7d35785SGleb Smirnoff case BOOT_STRAPPED: 1390f7d35785SGleb Smirnoff break; 1391f7d35785SGleb Smirnoff case BOOT_PAGEALLOC: 1392f7d35785SGleb Smirnoff if (keg->uk_ppera > 1) 1393f7d35785SGleb Smirnoff break; 1394f7d35785SGleb Smirnoff case BOOT_BUCKETS: 1395f7d35785SGleb Smirnoff case BOOT_RUNNING: 1396009b6fcbSJeff Roberson #ifdef UMA_MD_SMALL_ALLOC 1397f7d35785SGleb Smirnoff keg->uk_allocf = (keg->uk_ppera > 1) ? 1398f7d35785SGleb Smirnoff page_alloc : uma_small_alloc; 1399009b6fcbSJeff Roberson #else 1400099a0e58SBosko Milekic keg->uk_allocf = page_alloc; 1401009b6fcbSJeff Roberson #endif 1402ab3185d1SJeff Roberson return keg->uk_allocf(zone, bytes, domain, pflag, wait); 1403009b6fcbSJeff Roberson } 1404009b6fcbSJeff Roberson 1405009b6fcbSJeff Roberson /* 1406f7d35785SGleb Smirnoff * Check our small startup cache to see if it has pages remaining. 1407f7d35785SGleb Smirnoff */ 1408f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1409f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1410f7d35785SGleb Smirnoff if (pages > boot_pages) 1411f7d35785SGleb Smirnoff panic("UMA zone \"%s\": Increase vm.boot_pages", zone->uz_name); 1412f7d35785SGleb Smirnoff #ifdef DIAGNOSTIC 1413f7d35785SGleb Smirnoff printf("%s from \"%s\", %d boot pages left\n", __func__, zone->uz_name, 1414f7d35785SGleb Smirnoff boot_pages); 1415f7d35785SGleb Smirnoff #endif 1416f7d35785SGleb Smirnoff mem = bootmem; 1417f7d35785SGleb Smirnoff boot_pages -= pages; 1418f7d35785SGleb Smirnoff bootmem += pages * PAGE_SIZE; 1419f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1420f7d35785SGleb Smirnoff 1421f7d35785SGleb Smirnoff return (mem); 1422f7d35785SGleb Smirnoff } 1423f7d35785SGleb Smirnoff 1424f7d35785SGleb Smirnoff /* 14258355f576SJeff Roberson * Allocates a number of pages from the system 14268355f576SJeff Roberson * 14278355f576SJeff Roberson * Arguments: 14288355f576SJeff Roberson * bytes The number of bytes requested 14298355f576SJeff Roberson * wait Shall we wait? 14308355f576SJeff Roberson * 14318355f576SJeff Roberson * Returns: 14328355f576SJeff Roberson * A pointer to the alloced memory or possibly 14338355f576SJeff Roberson * NULL if M_NOWAIT is set. 14348355f576SJeff Roberson */ 14358355f576SJeff Roberson static void * 1436ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1437ab3185d1SJeff Roberson int wait) 14388355f576SJeff Roberson { 14398355f576SJeff Roberson void *p; /* Returned page */ 14408355f576SJeff Roberson 14412e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 14429978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 14438355f576SJeff Roberson 14448355f576SJeff Roberson return (p); 14458355f576SJeff Roberson } 14468355f576SJeff Roberson 1447ab3059a8SMatt Macy static void * 1448ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1449ab3059a8SMatt Macy int wait) 1450ab3059a8SMatt Macy { 1451ab3059a8SMatt Macy struct pglist alloctail; 1452ab3059a8SMatt Macy vm_offset_t addr, zkva; 1453ab3059a8SMatt Macy int cpu, flags; 1454ab3059a8SMatt Macy vm_page_t p, p_next; 1455ab3059a8SMatt Macy #ifdef NUMA 1456ab3059a8SMatt Macy struct pcpu *pc; 1457ab3059a8SMatt Macy #endif 1458ab3059a8SMatt Macy 1459ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1460ab3059a8SMatt Macy 1461013072f0SMark Johnston TAILQ_INIT(&alloctail); 1462ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1463013072f0SMark Johnston malloc2vm_flags(wait); 1464013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1465ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1466ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1467ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1468ab3059a8SMatt Macy } else { 1469ab3059a8SMatt Macy #ifndef NUMA 1470ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1471ab3059a8SMatt Macy #else 1472ab3059a8SMatt Macy pc = pcpu_find(cpu); 1473ab3059a8SMatt Macy p = vm_page_alloc_domain(NULL, 0, pc->pc_domain, flags); 1474ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1475ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1476ab3059a8SMatt Macy #endif 1477ab3059a8SMatt Macy } 1478ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1479ab3059a8SMatt Macy goto fail; 1480ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1481ab3059a8SMatt Macy } 1482ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1483ab3059a8SMatt Macy goto fail; 1484ab3059a8SMatt Macy zkva = addr; 1485ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1486ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1487ab3059a8SMatt Macy zkva += PAGE_SIZE; 1488ab3059a8SMatt Macy } 1489ab3059a8SMatt Macy return ((void*)addr); 1490ab3059a8SMatt Macy fail: 1491ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 149288ea538aSMark Johnston vm_page_unwire_noq(p); 1493ab3059a8SMatt Macy vm_page_free(p); 1494ab3059a8SMatt Macy } 1495ab3059a8SMatt Macy return (NULL); 1496ab3059a8SMatt Macy } 1497ab3059a8SMatt Macy 14988355f576SJeff Roberson /* 14998355f576SJeff Roberson * Allocates a number of pages from within an object 15008355f576SJeff Roberson * 15018355f576SJeff Roberson * Arguments: 15028355f576SJeff Roberson * bytes The number of bytes requested 15038355f576SJeff Roberson * wait Shall we wait? 15048355f576SJeff Roberson * 15058355f576SJeff Roberson * Returns: 15068355f576SJeff Roberson * A pointer to the alloced memory or possibly 15078355f576SJeff Roberson * NULL if M_NOWAIT is set. 15088355f576SJeff Roberson */ 15098355f576SJeff Roberson static void * 1510ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1511ab3185d1SJeff Roberson int wait) 15128355f576SJeff Roberson { 1513a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1514a4915c21SAttilio Rao u_long npages; 1515b245ac95SAlan Cox vm_offset_t retkva, zkva; 1516a4915c21SAttilio Rao vm_page_t p, p_next; 1517e20a199fSJeff Roberson uma_keg_t keg; 15188355f576SJeff Roberson 1519a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1520bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1521a4915c21SAttilio Rao 1522a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1523a4915c21SAttilio Rao while (npages > 0) { 1524ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 15258d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1526772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1527772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1528a4915c21SAttilio Rao if (p != NULL) { 1529a4915c21SAttilio Rao /* 1530a4915c21SAttilio Rao * Since the page does not belong to an object, its 1531a4915c21SAttilio Rao * listq is unused. 1532a4915c21SAttilio Rao */ 1533a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1534a4915c21SAttilio Rao npages--; 1535a4915c21SAttilio Rao continue; 1536a4915c21SAttilio Rao } 15378355f576SJeff Roberson /* 1538a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1539a4915c21SAttilio Rao * exit. 15408355f576SJeff Roberson */ 1541a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 154288ea538aSMark Johnston vm_page_unwire_noq(p); 1543b245ac95SAlan Cox vm_page_free(p); 1544b245ac95SAlan Cox } 1545a4915c21SAttilio Rao return (NULL); 1546b245ac95SAlan Cox } 15478355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1548a4915c21SAttilio Rao zkva = keg->uk_kva + 1549a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1550a4915c21SAttilio Rao retkva = zkva; 1551a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1552a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1553a4915c21SAttilio Rao zkva += PAGE_SIZE; 1554a4915c21SAttilio Rao } 15558355f576SJeff Roberson 15568355f576SJeff Roberson return ((void *)retkva); 15578355f576SJeff Roberson } 15588355f576SJeff Roberson 15598355f576SJeff Roberson /* 15608355f576SJeff Roberson * Frees a number of pages to the system 15618355f576SJeff Roberson * 15628355f576SJeff Roberson * Arguments: 15638355f576SJeff Roberson * mem A pointer to the memory to be freed 15648355f576SJeff Roberson * size The size of the memory being freed 15658355f576SJeff Roberson * flags The original p->us_flags field 15668355f576SJeff Roberson * 15678355f576SJeff Roberson * Returns: 15688355f576SJeff Roberson * Nothing 15698355f576SJeff Roberson */ 15708355f576SJeff Roberson static void 1571f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 15728355f576SJeff Roberson { 15733370c5bfSJeff Roberson 157449bfa624SAlan Cox if ((flags & UMA_SLAB_KERNEL) == 0) 1575b5345ef1SJustin Hibbits panic("UMA: page_free used with invalid flags %x", flags); 15768355f576SJeff Roberson 157749bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 15788355f576SJeff Roberson } 15798355f576SJeff Roberson 15808355f576SJeff Roberson /* 1581ab3059a8SMatt Macy * Frees pcpu zone allocations 1582ab3059a8SMatt Macy * 1583ab3059a8SMatt Macy * Arguments: 1584ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1585ab3059a8SMatt Macy * size The size of the memory being freed 1586ab3059a8SMatt Macy * flags The original p->us_flags field 1587ab3059a8SMatt Macy * 1588ab3059a8SMatt Macy * Returns: 1589ab3059a8SMatt Macy * Nothing 1590ab3059a8SMatt Macy */ 1591ab3059a8SMatt Macy static void 1592ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1593ab3059a8SMatt Macy { 1594ab3059a8SMatt Macy vm_offset_t sva, curva; 1595ab3059a8SMatt Macy vm_paddr_t paddr; 1596ab3059a8SMatt Macy vm_page_t m; 1597ab3059a8SMatt Macy 1598ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 1599ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1600ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1601ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1602ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 160388ea538aSMark Johnston vm_page_unwire_noq(m); 1604ab3059a8SMatt Macy vm_page_free(m); 1605ab3059a8SMatt Macy } 1606ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1607ab3059a8SMatt Macy kva_free(sva, size); 1608ab3059a8SMatt Macy } 1609ab3059a8SMatt Macy 1610ab3059a8SMatt Macy 1611ab3059a8SMatt Macy /* 16128355f576SJeff Roberson * Zero fill initializer 16138355f576SJeff Roberson * 16148355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 16158355f576SJeff Roberson */ 1616b23f72e9SBrian Feldman static int 1617b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 16188355f576SJeff Roberson { 16198355f576SJeff Roberson bzero(mem, size); 1620b23f72e9SBrian Feldman return (0); 16218355f576SJeff Roberson } 16228355f576SJeff Roberson 1623815db204SRyan Libby #ifdef INVARIANTS 1624815db204SRyan Libby struct noslabbits * 1625815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1626815db204SRyan Libby { 1627815db204SRyan Libby 1628815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1629815db204SRyan Libby } 1630815db204SRyan Libby #endif 1631815db204SRyan Libby 16328355f576SJeff Roberson /* 16339b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 16349b78b1f4SJeff Roberson */ 16359b78b1f4SJeff Roberson size_t 16369b78b1f4SJeff Roberson slab_sizeof(int nitems) 16379b78b1f4SJeff Roberson { 16389b78b1f4SJeff Roberson size_t s; 16399b78b1f4SJeff Roberson 1640815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 16419b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 16429b78b1f4SJeff Roberson } 16439b78b1f4SJeff Roberson 16449b78b1f4SJeff Roberson /* 16459b78b1f4SJeff Roberson * Size of memory for embedded slabs (!OFFPAGE). 16469b78b1f4SJeff Roberson */ 16479b78b1f4SJeff Roberson size_t 16489b78b1f4SJeff Roberson slab_space(int nitems) 16499b78b1f4SJeff Roberson { 16509b78b1f4SJeff Roberson return (UMA_SLAB_SIZE - slab_sizeof(nitems)); 16519b78b1f4SJeff Roberson } 16529b78b1f4SJeff Roberson 16539b78b1f4SJeff Roberson /* 16549b78b1f4SJeff Roberson * Compute the number of items that will fit in an embedded (!OFFPAGE) slab 16559b78b1f4SJeff Roberson * with a given size and alignment. 16569b78b1f4SJeff Roberson */ 16579b78b1f4SJeff Roberson int 16589b78b1f4SJeff Roberson slab_ipers(size_t size, int align) 16599b78b1f4SJeff Roberson { 16609b78b1f4SJeff Roberson int rsize; 16619b78b1f4SJeff Roberson int nitems; 16629b78b1f4SJeff Roberson 16639b78b1f4SJeff Roberson /* 16649b78b1f4SJeff Roberson * Compute the ideal number of items that will fit in a page and 16659b78b1f4SJeff Roberson * then compute the actual number based on a bitset nitems wide. 16669b78b1f4SJeff Roberson */ 16679b78b1f4SJeff Roberson rsize = roundup(size, align + 1); 16689b78b1f4SJeff Roberson nitems = UMA_SLAB_SIZE / rsize; 16699b78b1f4SJeff Roberson return (slab_space(nitems) / rsize); 16709b78b1f4SJeff Roberson } 16719b78b1f4SJeff Roberson 16729b78b1f4SJeff Roberson /* 1673e20a199fSJeff Roberson * Finish creating a small uma keg. This calculates ipers, and the keg size. 16748355f576SJeff Roberson * 16758355f576SJeff Roberson * Arguments 1676e20a199fSJeff Roberson * keg The zone we should initialize 16778355f576SJeff Roberson * 16788355f576SJeff Roberson * Returns 16798355f576SJeff Roberson * Nothing 16808355f576SJeff Roberson */ 16818355f576SJeff Roberson static void 1682e20a199fSJeff Roberson keg_small_init(uma_keg_t keg) 16838355f576SJeff Roberson { 1684244f4554SBosko Milekic u_int rsize; 1685244f4554SBosko Milekic u_int memused; 1686244f4554SBosko Milekic u_int wastedspace; 1687244f4554SBosko Milekic u_int shsize; 1688a55ebb7cSAndriy Gapon u_int slabsize; 16898355f576SJeff Roberson 1690ad97af7eSGleb Smirnoff if (keg->uk_flags & UMA_ZONE_PCPU) { 169196c85efbSNathan Whitehorn u_int ncpus = (mp_maxid + 1) ? (mp_maxid + 1) : MAXCPU; 1692e28a647dSGleb Smirnoff 1693ab3059a8SMatt Macy slabsize = UMA_PCPU_ALLOC_SIZE; 1694ab3059a8SMatt Macy keg->uk_ppera = ncpus; 1695ad97af7eSGleb Smirnoff } else { 1696a55ebb7cSAndriy Gapon slabsize = UMA_SLAB_SIZE; 1697ad97af7eSGleb Smirnoff keg->uk_ppera = 1; 1698ad97af7eSGleb Smirnoff } 1699ad97af7eSGleb Smirnoff 1700ef72505eSJeff Roberson /* 1701ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1702ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1703ef72505eSJeff Roberson * allocation bits for we round it up. 1704ef72505eSJeff Roberson */ 1705099a0e58SBosko Milekic rsize = keg->uk_size; 17069b78b1f4SJeff Roberson if (rsize < slabsize / SLAB_MAX_SETSIZE) 17079b78b1f4SJeff Roberson rsize = slabsize / SLAB_MAX_SETSIZE; 1708099a0e58SBosko Milekic if (rsize & keg->uk_align) 17099b78b1f4SJeff Roberson rsize = roundup(rsize, keg->uk_align + 1); 1710099a0e58SBosko Milekic keg->uk_rsize = rsize; 1711ad97af7eSGleb Smirnoff 1712ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 1713ab3059a8SMatt Macy keg->uk_rsize < UMA_PCPU_ALLOC_SIZE, 1714ad97af7eSGleb Smirnoff ("%s: size %u too large", __func__, keg->uk_rsize)); 17158355f576SJeff Roberson 17169b78b1f4SJeff Roberson /* 17179b78b1f4SJeff Roberson * Use a pessimistic bit count for shsize. It may be possible to 17189b78b1f4SJeff Roberson * squeeze one more item in for very particular sizes if we were 17199b78b1f4SJeff Roberson * to loop and reduce the bitsize if there is waste. 17209b78b1f4SJeff Roberson */ 1721ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_OFFPAGE) 17222864dbbfSGleb Smirnoff shsize = 0; 1723ef72505eSJeff Roberson else 17249b78b1f4SJeff Roberson shsize = slab_sizeof(slabsize / rsize); 17258355f576SJeff Roberson 17261ca6ed45SGleb Smirnoff if (rsize <= slabsize - shsize) 1727a55ebb7cSAndriy Gapon keg->uk_ipers = (slabsize - shsize) / rsize; 17281ca6ed45SGleb Smirnoff else { 17291ca6ed45SGleb Smirnoff /* Handle special case when we have 1 item per slab, so 17301ca6ed45SGleb Smirnoff * alignment requirement can be relaxed. */ 17311ca6ed45SGleb Smirnoff KASSERT(keg->uk_size <= slabsize - shsize, 17321ca6ed45SGleb Smirnoff ("%s: size %u greater than slab", __func__, keg->uk_size)); 17331ca6ed45SGleb Smirnoff keg->uk_ipers = 1; 17341ca6ed45SGleb Smirnoff } 17359b78b1f4SJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 1736ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 1737ad97af7eSGleb Smirnoff 1738244f4554SBosko Milekic memused = keg->uk_ipers * rsize + shsize; 1739a55ebb7cSAndriy Gapon wastedspace = slabsize - memused; 1740244f4554SBosko Milekic 174120e8e865SBosko Milekic /* 1742244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 174320e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 17446fd34d6fSJeff Roberson * may end up going to the VM for slabs which we do not 17456fd34d6fSJeff Roberson * want to do if we're UMA_ZFLAG_CACHEONLY as a result 17466fd34d6fSJeff Roberson * of UMA_ZONE_VM, which clearly forbids it. 174720e8e865SBosko Milekic */ 1748099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) || 1749099a0e58SBosko Milekic (keg->uk_flags & UMA_ZFLAG_CACHEONLY)) 17508355f576SJeff Roberson return; 1751244f4554SBosko Milekic 1752ef72505eSJeff Roberson /* 1753ef72505eSJeff Roberson * See if using an OFFPAGE slab will limit our waste. Only do 1754ef72505eSJeff Roberson * this if it permits more items per-slab. 1755ef72505eSJeff Roberson * 1756ef72505eSJeff Roberson * XXX We could try growing slabsize to limit max waste as well. 1757ef72505eSJeff Roberson * Historically this was not done because the VM could not 1758ef72505eSJeff Roberson * efficiently handle contiguous allocations. 1759ef72505eSJeff Roberson */ 1760a55ebb7cSAndriy Gapon if ((wastedspace >= slabsize / UMA_MAX_WASTE) && 1761a55ebb7cSAndriy Gapon (keg->uk_ipers < (slabsize / keg->uk_rsize))) { 1762a55ebb7cSAndriy Gapon keg->uk_ipers = slabsize / keg->uk_rsize; 17639b78b1f4SJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 1764ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 17651431a748SGleb Smirnoff CTR6(KTR_UMA, "UMA decided we need offpage slab headers for " 17661431a748SGleb Smirnoff "keg: %s(%p), calculated wastedspace = %d, " 1767244f4554SBosko Milekic "maximum wasted space allowed = %d, " 1768244f4554SBosko Milekic "calculated ipers = %d, " 17691431a748SGleb Smirnoff "new wasted space = %d\n", keg->uk_name, keg, wastedspace, 1770a55ebb7cSAndriy Gapon slabsize / UMA_MAX_WASTE, keg->uk_ipers, 1771a55ebb7cSAndriy Gapon slabsize - keg->uk_ipers * keg->uk_rsize); 177271353f7aSJeff Roberson /* 177371353f7aSJeff Roberson * If we had access to memory to embed a slab header we 177471353f7aSJeff Roberson * also have a page structure to use vtoslab() instead of 177571353f7aSJeff Roberson * hash to find slabs. If the zone was explicitly created 177671353f7aSJeff Roberson * OFFPAGE we can't necessarily touch the memory. 177771353f7aSJeff Roberson */ 177871353f7aSJeff Roberson if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0) 177971353f7aSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 17808355f576SJeff Roberson } 1781ad97af7eSGleb Smirnoff 1782ad97af7eSGleb Smirnoff if ((keg->uk_flags & UMA_ZONE_OFFPAGE) && 1783ad97af7eSGleb Smirnoff (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1784ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_HASH; 17858355f576SJeff Roberson } 17868355f576SJeff Roberson 17878355f576SJeff Roberson /* 1788e20a199fSJeff Roberson * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do 17898355f576SJeff Roberson * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be 17908355f576SJeff Roberson * more complicated. 17918355f576SJeff Roberson * 17928355f576SJeff Roberson * Arguments 1793e20a199fSJeff Roberson * keg The keg we should initialize 17948355f576SJeff Roberson * 17958355f576SJeff Roberson * Returns 17968355f576SJeff Roberson * Nothing 17978355f576SJeff Roberson */ 17988355f576SJeff Roberson static void 1799e20a199fSJeff Roberson keg_large_init(uma_keg_t keg) 18008355f576SJeff Roberson { 18018355f576SJeff Roberson 1802e20a199fSJeff Roberson KASSERT(keg != NULL, ("Keg is null in keg_large_init")); 1803ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1804ad97af7eSGleb Smirnoff ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__)); 180520e8e865SBosko Milekic 1806ad97af7eSGleb Smirnoff keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE); 1807099a0e58SBosko Milekic keg->uk_ipers = 1; 1808e9a069d8SJohn Baldwin keg->uk_rsize = keg->uk_size; 1809e9a069d8SJohn Baldwin 1810cec48e00SAlexander Motin /* Check whether we have enough space to not do OFFPAGE. */ 18113d5e3df7SGleb Smirnoff if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0 && 18129b78b1f4SJeff Roberson PAGE_SIZE * keg->uk_ppera - keg->uk_rsize < 18139b78b1f4SJeff Roberson slab_sizeof(SLAB_MIN_SETSIZE)) { 18142934eb8aSMark Johnston /* 18152934eb8aSMark Johnston * We can't do OFFPAGE if we're internal, in which case 18162934eb8aSMark Johnston * we need an extra page per allocation to contain the 18172934eb8aSMark Johnston * slab header. 18182934eb8aSMark Johnston */ 18192934eb8aSMark Johnston if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) == 0) 182071353f7aSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 18212934eb8aSMark Johnston else 18222934eb8aSMark Johnston keg->uk_ppera++; 18232934eb8aSMark Johnston } 1824cec48e00SAlexander Motin 1825cec48e00SAlexander Motin if ((keg->uk_flags & UMA_ZONE_OFFPAGE) && 1826cec48e00SAlexander Motin (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1827099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_HASH; 18288355f576SJeff Roberson } 18298355f576SJeff Roberson 1830e20a199fSJeff Roberson static void 1831e20a199fSJeff Roberson keg_cachespread_init(uma_keg_t keg) 1832e20a199fSJeff Roberson { 1833e20a199fSJeff Roberson int alignsize; 1834e20a199fSJeff Roberson int trailer; 1835e20a199fSJeff Roberson int pages; 1836e20a199fSJeff Roberson int rsize; 1837e20a199fSJeff Roberson 1838ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1839ad97af7eSGleb Smirnoff ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__)); 1840ad97af7eSGleb Smirnoff 1841e20a199fSJeff Roberson alignsize = keg->uk_align + 1; 1842e20a199fSJeff Roberson rsize = keg->uk_size; 1843e20a199fSJeff Roberson /* 1844e20a199fSJeff Roberson * We want one item to start on every align boundary in a page. To 1845e20a199fSJeff Roberson * do this we will span pages. We will also extend the item by the 1846e20a199fSJeff Roberson * size of align if it is an even multiple of align. Otherwise, it 1847e20a199fSJeff Roberson * would fall on the same boundary every time. 1848e20a199fSJeff Roberson */ 1849e20a199fSJeff Roberson if (rsize & keg->uk_align) 1850e20a199fSJeff Roberson rsize = (rsize & ~keg->uk_align) + alignsize; 1851e20a199fSJeff Roberson if ((rsize & alignsize) == 0) 1852e20a199fSJeff Roberson rsize += alignsize; 1853e20a199fSJeff Roberson trailer = rsize - keg->uk_size; 1854e20a199fSJeff Roberson pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE; 1855e20a199fSJeff Roberson pages = MIN(pages, (128 * 1024) / PAGE_SIZE); 1856e20a199fSJeff Roberson keg->uk_rsize = rsize; 1857e20a199fSJeff Roberson keg->uk_ppera = pages; 1858e20a199fSJeff Roberson keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize; 1859e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 18609b78b1f4SJeff Roberson KASSERT(keg->uk_ipers <= SLAB_MAX_SETSIZE, 186142321809SGleb Smirnoff ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__, 1862e20a199fSJeff Roberson keg->uk_ipers)); 1863e20a199fSJeff Roberson } 1864e20a199fSJeff Roberson 18658355f576SJeff Roberson /* 1866099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 1867099a0e58SBosko Milekic * the keg onto the global keg list. 18688355f576SJeff Roberson * 18698355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 1870099a0e58SBosko Milekic * udata Actually uma_kctor_args 1871099a0e58SBosko Milekic */ 1872b23f72e9SBrian Feldman static int 1873b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 1874099a0e58SBosko Milekic { 1875099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 1876099a0e58SBosko Milekic uma_keg_t keg = mem; 1877099a0e58SBosko Milekic uma_zone_t zone; 1878099a0e58SBosko Milekic 1879099a0e58SBosko Milekic bzero(keg, size); 1880099a0e58SBosko Milekic keg->uk_size = arg->size; 1881099a0e58SBosko Milekic keg->uk_init = arg->uminit; 1882099a0e58SBosko Milekic keg->uk_fini = arg->fini; 1883099a0e58SBosko Milekic keg->uk_align = arg->align; 1884099a0e58SBosko Milekic keg->uk_free = 0; 18856fd34d6fSJeff Roberson keg->uk_reserve = 0; 1886099a0e58SBosko Milekic keg->uk_pages = 0; 1887099a0e58SBosko Milekic keg->uk_flags = arg->flags; 1888099a0e58SBosko Milekic keg->uk_slabzone = NULL; 1889099a0e58SBosko Milekic 1890099a0e58SBosko Milekic /* 1891194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 1892194a979eSMark Johnston * UMA_ZONE_NUMA set will use first-touch instead, in which case the 1893194a979eSMark Johnston * iterator is never run. 1894194a979eSMark Johnston */ 1895194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 1896194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 1897194a979eSMark Johnston 1898194a979eSMark Johnston /* 1899099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 1900099a0e58SBosko Milekic */ 1901099a0e58SBosko Milekic zone = arg->zone; 1902e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 1903099a0e58SBosko Milekic 1904099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_VM) 1905099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 1906099a0e58SBosko Milekic 1907099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 1908099a0e58SBosko Milekic keg->uk_init = zero_init; 1909099a0e58SBosko Milekic 1910cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 1911e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_VTOSLAB; 1912e20a199fSJeff Roberson 1913ad97af7eSGleb Smirnoff if (arg->flags & UMA_ZONE_PCPU) 1914ad97af7eSGleb Smirnoff #ifdef SMP 1915ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_OFFPAGE; 1916ad97af7eSGleb Smirnoff #else 1917ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 1918ad97af7eSGleb Smirnoff #endif 1919ad97af7eSGleb Smirnoff 1920ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_CACHESPREAD) { 1921e20a199fSJeff Roberson keg_cachespread_init(keg); 1922244f4554SBosko Milekic } else { 19239b78b1f4SJeff Roberson if (keg->uk_size > slab_space(SLAB_MIN_SETSIZE)) 1924e20a199fSJeff Roberson keg_large_init(keg); 1925244f4554SBosko Milekic else 1926e20a199fSJeff Roberson keg_small_init(keg); 1927244f4554SBosko Milekic } 1928099a0e58SBosko Milekic 1929cfcae3f8SGleb Smirnoff if (keg->uk_flags & UMA_ZONE_OFFPAGE) 1930099a0e58SBosko Milekic keg->uk_slabzone = slabzone; 1931099a0e58SBosko Milekic 1932099a0e58SBosko Milekic /* 1933099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 1934099a0e58SBosko Milekic * startup cache until the vm is ready. 1935099a0e58SBosko Milekic */ 1936f4bef67cSGleb Smirnoff if (booted < BOOT_PAGEALLOC) 19378cd02d00SAlan Cox keg->uk_allocf = startup_alloc; 193877e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 193977e19437SGleb Smirnoff else if (keg->uk_ppera == 1) 194077e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 19418cd02d00SAlan Cox #endif 1942ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 1943ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 194477e19437SGleb Smirnoff else 194577e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 194677e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 194777e19437SGleb Smirnoff if (keg->uk_ppera == 1) 194877e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 194977e19437SGleb Smirnoff else 195077e19437SGleb Smirnoff #endif 1951ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 1952ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 1953ab3059a8SMatt Macy else 195477e19437SGleb Smirnoff keg->uk_freef = page_free; 1955099a0e58SBosko Milekic 1956099a0e58SBosko Milekic /* 1957af526374SJeff Roberson * Initialize keg's lock 1958099a0e58SBosko Milekic */ 1959af526374SJeff Roberson KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS)); 1960099a0e58SBosko Milekic 1961099a0e58SBosko Milekic /* 1962099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 19639b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 19649b78b1f4SJeff Roberson * definition. 1965099a0e58SBosko Milekic */ 1966099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) { 19679b78b1f4SJeff Roberson size_t shsize; 19689b78b1f4SJeff Roberson 19699b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 19709b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 1971244f4554SBosko Milekic /* 1972244f4554SBosko Milekic * The only way the following is possible is if with our 1973244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 1974244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 1975244f4554SBosko Milekic * mathematically possible for all cases, so we make 1976244f4554SBosko Milekic * sure here anyway. 1977244f4554SBosko Milekic */ 19789b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 19793d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 19803d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 1981099a0e58SBosko Milekic } 1982099a0e58SBosko Milekic 1983099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 19843b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 1985099a0e58SBosko Milekic 19861431a748SGleb Smirnoff CTR5(KTR_UMA, "keg_ctor %p zone %s(%p) out %d free %d\n", 19871431a748SGleb Smirnoff keg, zone->uz_name, zone, 198857223e99SAndriy Gapon (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free, 198957223e99SAndriy Gapon keg->uk_free); 1990099a0e58SBosko Milekic 1991099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 1992099a0e58SBosko Milekic 1993111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 1994099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 1995111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 1996b23f72e9SBrian Feldman return (0); 1997099a0e58SBosko Milekic } 1998099a0e58SBosko Milekic 19992efcc8cbSGleb Smirnoff static void 200020a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 20012efcc8cbSGleb Smirnoff { 20022efcc8cbSGleb Smirnoff 20032efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 20042efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 20052efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 20062efcc8cbSGleb Smirnoff } 20072efcc8cbSGleb Smirnoff 200820a4e154SJeff Roberson static void 200920a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 201020a4e154SJeff Roberson { 201120a4e154SJeff Roberson uma_zone_domain_t zdom; 201220a4e154SJeff Roberson uma_keg_t keg; 201320a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 20143b490537SJeff Roberson int domains, i, cnt; 201520a4e154SJeff Roberson static const char *nokeg = "cache zone"; 201620a4e154SJeff Roberson char *c; 201720a4e154SJeff Roberson 201820a4e154SJeff Roberson /* 201920a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 202020a4e154SJeff Roberson * any special characters and handling dups by appending 202120a4e154SJeff Roberson * an index. 202220a4e154SJeff Roberson */ 202320a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 20243b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 20253b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 20263b490537SJeff Roberson cnt /= 10; 20273b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 20283b490537SJeff Roberson M_UMA, M_WAITOK); 202920a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 203020a4e154SJeff Roberson zone->uz_namecnt); 203120a4e154SJeff Roberson } else 203220a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 203320a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 203420a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 203520a4e154SJeff Roberson *c = '_'; 203620a4e154SJeff Roberson 203720a4e154SJeff Roberson /* 203820a4e154SJeff Roberson * Basic parameters at the root. 203920a4e154SJeff Roberson */ 204020a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 204120a4e154SJeff Roberson OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, ""); 204220a4e154SJeff Roberson oid = zone->uz_oid; 204320a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 204420a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 20456d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 20466d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 20476d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 204820a4e154SJeff Roberson "Allocator configuration flags"); 204920a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 205020a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 205120a4e154SJeff Roberson "Desired per-cpu cache size"); 205220a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 205320a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 205420a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 205520a4e154SJeff Roberson 205620a4e154SJeff Roberson /* 205720a4e154SJeff Roberson * keg if present. 205820a4e154SJeff Roberson */ 205920a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 206020a4e154SJeff Roberson "keg", CTLFLAG_RD, NULL, ""); 206120a4e154SJeff Roberson keg = zone->uz_keg; 20623b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 206320a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 206420a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 206520a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 206620a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 206720a4e154SJeff Roberson "Real object size with alignment"); 206820a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 206920a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 207020a4e154SJeff Roberson "pages per-slab allocation"); 207120a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 207220a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 207320a4e154SJeff Roberson "items available per-slab"); 207420a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 207520a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 207620a4e154SJeff Roberson "item alignment mask"); 207720a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 207820a4e154SJeff Roberson "pages", CTLFLAG_RD, &keg->uk_pages, 0, 207920a4e154SJeff Roberson "Total pages currently allocated from VM"); 208020a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 208120a4e154SJeff Roberson "free", CTLFLAG_RD, &keg->uk_free, 0, 208220a4e154SJeff Roberson "items free in the slab layer"); 2083f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2084f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2085f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2086f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 208720a4e154SJeff Roberson } else 208820a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 208920a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 209020a4e154SJeff Roberson 209120a4e154SJeff Roberson /* 209220a4e154SJeff Roberson * Information about zone limits. 209320a4e154SJeff Roberson */ 209420a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 209520a4e154SJeff Roberson "limit", CTLFLAG_RD, NULL, ""); 20964bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 20974bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 20984bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 20994bd61e19SJeff Roberson "current number of allocated items if limit is set"); 210020a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210120a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 210220a4e154SJeff Roberson "Maximum number of cached items"); 210320a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210420a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 210520a4e154SJeff Roberson "Number of threads sleeping at limit"); 210620a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210720a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 210820a4e154SJeff Roberson "Total zone limit sleeps"); 21094bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21104bd61e19SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bkt_max, 0, 21114bd61e19SJeff Roberson "Maximum number of items in the bucket cache"); 21124bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21134bd61e19SJeff Roberson "bucket_cnt", CTLFLAG_RD, &zone->uz_bkt_count, 0, 21144bd61e19SJeff Roberson "Number of items in the bucket cache"); 211520a4e154SJeff Roberson 211620a4e154SJeff Roberson /* 211720a4e154SJeff Roberson * Per-domain information. 211820a4e154SJeff Roberson */ 211920a4e154SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) 212020a4e154SJeff Roberson domains = vm_ndomains; 212120a4e154SJeff Roberson else 212220a4e154SJeff Roberson domains = 1; 212320a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 212420a4e154SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 212520a4e154SJeff Roberson for (i = 0; i < domains; i++) { 212620a4e154SJeff Roberson zdom = &zone->uz_domain[i]; 212720a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 212820a4e154SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, ""); 212920a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213020a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 213120a4e154SJeff Roberson "number of items in this domain"); 213220a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213320a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 213420a4e154SJeff Roberson "maximum item count in this period"); 213520a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213620a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 213720a4e154SJeff Roberson "minimum item count in this period"); 213820a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213920a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 214020a4e154SJeff Roberson "Working set size"); 214120a4e154SJeff Roberson } 214220a4e154SJeff Roberson 214320a4e154SJeff Roberson /* 214420a4e154SJeff Roberson * General statistics. 214520a4e154SJeff Roberson */ 214620a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 214720a4e154SJeff Roberson "stats", CTLFLAG_RD, NULL, ""); 214820a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 214920a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 215020a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 215120a4e154SJeff Roberson "Current number of allocated items"); 215220a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 215320a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 215420a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 215520a4e154SJeff Roberson "Total allocation calls"); 215620a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 215720a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 215820a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 215920a4e154SJeff Roberson "Total free calls"); 216020a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 216120a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 216220a4e154SJeff Roberson "Number of allocation failures"); 216320a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 216420a4e154SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 0, 216520a4e154SJeff Roberson "Free calls from the wrong domain"); 216620a4e154SJeff Roberson } 216720a4e154SJeff Roberson 216820a4e154SJeff Roberson struct uma_zone_count { 216920a4e154SJeff Roberson const char *name; 217020a4e154SJeff Roberson int count; 217120a4e154SJeff Roberson }; 217220a4e154SJeff Roberson 217320a4e154SJeff Roberson static void 217420a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 217520a4e154SJeff Roberson { 217620a4e154SJeff Roberson struct uma_zone_count *cnt; 217720a4e154SJeff Roberson 217820a4e154SJeff Roberson cnt = arg; 21793b490537SJeff Roberson /* 21803b490537SJeff Roberson * Some zones are rapidly created with identical names and 21813b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 21823b490537SJeff Roberson * Use one greater than the maximum observed for this name. 21833b490537SJeff Roberson */ 218420a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 21853b490537SJeff Roberson cnt->count = MAX(cnt->count, 21863b490537SJeff Roberson zone->uz_namecnt + 1); 218720a4e154SJeff Roberson } 218820a4e154SJeff Roberson 2189cc7ce83aSJeff Roberson static void 2190cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2191cc7ce83aSJeff Roberson { 2192cc7ce83aSJeff Roberson int i; 2193cc7ce83aSJeff Roberson 2194cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2195cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2196cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2197cc7ce83aSJeff Roberson } 2198cc7ce83aSJeff Roberson } 2199cc7ce83aSJeff Roberson 2200099a0e58SBosko Milekic /* 2201099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2202099a0e58SBosko Milekic * 2203099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2204099a0e58SBosko Milekic * udata Actually uma_zctor_args 22058355f576SJeff Roberson */ 2206b23f72e9SBrian Feldman static int 2207b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 22088355f576SJeff Roberson { 220920a4e154SJeff Roberson struct uma_zone_count cnt; 22108355f576SJeff Roberson struct uma_zctor_args *arg = udata; 22118355f576SJeff Roberson uma_zone_t zone = mem; 2212099a0e58SBosko Milekic uma_zone_t z; 2213099a0e58SBosko Milekic uma_keg_t keg; 221408cfa56eSMark Johnston int i; 22158355f576SJeff Roberson 22168355f576SJeff Roberson bzero(zone, size); 22178355f576SJeff Roberson zone->uz_name = arg->name; 22188355f576SJeff Roberson zone->uz_ctor = arg->ctor; 22198355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2220099a0e58SBosko Milekic zone->uz_init = NULL; 2221099a0e58SBosko Milekic zone->uz_fini = NULL; 2222bf965959SSean Bruno zone->uz_sleeps = 0; 2223c1685086SJeff Roberson zone->uz_xdomain = 0; 222420a4e154SJeff Roberson zone->uz_bucket_size = 0; 222520a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 222620a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2227e20a199fSJeff Roberson zone->uz_flags = 0; 22282f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2229ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 2230ab3185d1SJeff Roberson zone->uz_domain = (struct uma_zone_domain *)&zone->uz_cpu[mp_ncpus]; 2231bb15d1c7SGleb Smirnoff zone->uz_bkt_max = ULONG_MAX; 22322f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2233af526374SJeff Roberson 223420a4e154SJeff Roberson /* Count the number of duplicate names. */ 223520a4e154SJeff Roberson cnt.name = arg->name; 223620a4e154SJeff Roberson cnt.count = 0; 223720a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 223820a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 2239*727c6918SJeff Roberson ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 22402efcc8cbSGleb Smirnoff 224108cfa56eSMark Johnston for (i = 0; i < vm_ndomains; i++) 224208cfa56eSMark Johnston TAILQ_INIT(&zone->uz_domain[i].uzd_buckets); 224308cfa56eSMark Johnston 2244ca293436SRyan Libby #ifdef INVARIANTS 2245ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2246cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 2247ca293436SRyan Libby #endif 2248ca293436SRyan Libby 22490095a784SJeff Roberson /* 22500095a784SJeff Roberson * This is a pure cache zone, no kegs. 22510095a784SJeff Roberson */ 22520095a784SJeff Roberson if (arg->import) { 2253*727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2254*727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 22556fd34d6fSJeff Roberson if (arg->flags & UMA_ZONE_VM) 22566fd34d6fSJeff Roberson arg->flags |= UMA_ZFLAG_CACHEONLY; 22576fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2258af526374SJeff Roberson zone->uz_size = arg->size; 22590095a784SJeff Roberson zone->uz_import = arg->import; 22600095a784SJeff Roberson zone->uz_release = arg->release; 22610095a784SJeff Roberson zone->uz_arg = arg->arg; 2262111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 226303175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2264111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2265af526374SJeff Roberson goto out; 22660095a784SJeff Roberson } 22670095a784SJeff Roberson 22680095a784SJeff Roberson /* 22690095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 22700095a784SJeff Roberson */ 2271b75c4efcSAndrew Turner zone->uz_import = zone_import; 2272b75c4efcSAndrew Turner zone->uz_release = zone_release; 22730095a784SJeff Roberson zone->uz_arg = zone; 2274bb15d1c7SGleb Smirnoff keg = arg->keg; 22750095a784SJeff Roberson 2276099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 227720a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 227820a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2279099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 22808355f576SJeff Roberson zone->uz_init = arg->uminit; 2281e221e841SJeff Roberson zone->uz_fini = arg->fini; 2282e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2283111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2284099a0e58SBosko Milekic ZONE_LOCK(zone); 2285099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2286099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2287099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2288099a0e58SBosko Milekic break; 2289099a0e58SBosko Milekic } 2290099a0e58SBosko Milekic } 2291099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2292111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2293e20a199fSJeff Roberson } else if (keg == NULL) { 2294e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2295e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2296b23f72e9SBrian Feldman return (ENOMEM); 2297099a0e58SBosko Milekic } else { 2298099a0e58SBosko Milekic struct uma_kctor_args karg; 2299b23f72e9SBrian Feldman int error; 2300099a0e58SBosko Milekic 2301099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2302099a0e58SBosko Milekic karg.size = arg->size; 2303099a0e58SBosko Milekic karg.uminit = arg->uminit; 2304099a0e58SBosko Milekic karg.fini = arg->fini; 2305099a0e58SBosko Milekic karg.align = arg->align; 2306099a0e58SBosko Milekic karg.flags = arg->flags; 2307099a0e58SBosko Milekic karg.zone = zone; 2308b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2309b23f72e9SBrian Feldman flags); 2310b23f72e9SBrian Feldman if (error) 2311b23f72e9SBrian Feldman return (error); 2312099a0e58SBosko Milekic } 23130095a784SJeff Roberson 231420a4e154SJeff Roberson /* Inherit properties from the keg. */ 2315bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2316e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2317e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2318e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 23198355f576SJeff Roberson 232020a4e154SJeff Roberson out: 232120a4e154SJeff Roberson if (__predict_true(booted == BOOT_RUNNING)) { 232220a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 232320a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 232420a4e154SJeff Roberson } else { 232520a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 232620a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 232720a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2328099a0e58SBosko Milekic } 23298355f576SJeff Roberson 23307e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 23317e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 23327e28037aSMark Johnston ("Invalid zone flag combination")); 233320a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 233420a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 233520a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 233620a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 233720a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) 233820a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN; 233920a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 234020a4e154SJeff Roberson zone->uz_bucket_size = 0; 23417e28037aSMark Johnston else 234220a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 234320a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2344cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2345cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2346cc7ce83aSJeff Roberson zone_update_caches(zone); 2347fc03d22bSJeff Roberson 2348b23f72e9SBrian Feldman return (0); 23498355f576SJeff Roberson } 23508355f576SJeff Roberson 23518355f576SJeff Roberson /* 2352099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2353099a0e58SBosko Milekic * table and removes the keg from the global list. 23549c2cd7e5SJeff Roberson * 23559c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 23569c2cd7e5SJeff Roberson * udata unused 23579c2cd7e5SJeff Roberson */ 2358099a0e58SBosko Milekic static void 2359099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2360099a0e58SBosko Milekic { 2361099a0e58SBosko Milekic uma_keg_t keg; 23629c2cd7e5SJeff Roberson 2363099a0e58SBosko Milekic keg = (uma_keg_t)arg; 2364e20a199fSJeff Roberson KEG_LOCK(keg); 2365099a0e58SBosko Milekic if (keg->uk_free != 0) { 2366a3845534SCraig Rodrigues printf("Freed UMA keg (%s) was not empty (%d items). " 2367099a0e58SBosko Milekic " Lost %d pages of memory.\n", 2368a3845534SCraig Rodrigues keg->uk_name ? keg->uk_name : "", 2369099a0e58SBosko Milekic keg->uk_free, keg->uk_pages); 2370099a0e58SBosko Milekic } 2371e20a199fSJeff Roberson KEG_UNLOCK(keg); 2372099a0e58SBosko Milekic 2373099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2374099a0e58SBosko Milekic 2375e20a199fSJeff Roberson KEG_LOCK_FINI(keg); 2376099a0e58SBosko Milekic } 2377099a0e58SBosko Milekic 2378099a0e58SBosko Milekic /* 2379099a0e58SBosko Milekic * Zone header dtor. 2380099a0e58SBosko Milekic * 2381099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2382099a0e58SBosko Milekic * udata unused 2383099a0e58SBosko Milekic */ 23849c2cd7e5SJeff Roberson static void 23859c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 23869c2cd7e5SJeff Roberson { 23879c2cd7e5SJeff Roberson uma_zone_t zone; 2388099a0e58SBosko Milekic uma_keg_t keg; 23899c2cd7e5SJeff Roberson 23909c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 23919643769aSJeff Roberson 239220a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 239320a4e154SJeff Roberson 2394e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 23959643769aSJeff Roberson cache_drain(zone); 2396099a0e58SBosko Milekic 2397111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2398099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2399111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2400099a0e58SBosko Milekic /* 2401099a0e58SBosko Milekic * XXX there are some races here where 2402099a0e58SBosko Milekic * the zone can be drained but zone lock 2403099a0e58SBosko Milekic * released and then refilled before we 2404099a0e58SBosko Milekic * remove it... we dont care for now 2405099a0e58SBosko Milekic */ 240608cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2407e20a199fSJeff Roberson /* 2408323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2409e20a199fSJeff Roberson */ 2410323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2411323ad386STycho Nightingale keg = zone->uz_keg; 2412111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2413099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2414111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 24150095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 24169c2cd7e5SJeff Roberson } 24172efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 24182efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 24192efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 242020a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2421af526374SJeff Roberson ZONE_LOCK_FINI(zone); 2422099a0e58SBosko Milekic } 2423099a0e58SBosko Milekic 24249c2cd7e5SJeff Roberson /* 24258355f576SJeff Roberson * Traverses every zone in the system and calls a callback 24268355f576SJeff Roberson * 24278355f576SJeff Roberson * Arguments: 24288355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 24298355f576SJeff Roberson * as an argument. 24308355f576SJeff Roberson * 24318355f576SJeff Roberson * Returns: 24328355f576SJeff Roberson * Nothing 24338355f576SJeff Roberson */ 24348355f576SJeff Roberson static void 243520a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 24368355f576SJeff Roberson { 2437099a0e58SBosko Milekic uma_keg_t keg; 24388355f576SJeff Roberson uma_zone_t zone; 24398355f576SJeff Roberson 24402efcc8cbSGleb Smirnoff /* 24412efcc8cbSGleb Smirnoff * Before BOOT_RUNNING we are guaranteed to be single 24422efcc8cbSGleb Smirnoff * threaded, so locking isn't needed. Startup functions 24432efcc8cbSGleb Smirnoff * are allowed to use M_WAITOK. 24442efcc8cbSGleb Smirnoff */ 24452efcc8cbSGleb Smirnoff if (__predict_true(booted == BOOT_RUNNING)) 2446111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2447099a0e58SBosko Milekic LIST_FOREACH(keg, &uma_kegs, uk_link) { 2448099a0e58SBosko Milekic LIST_FOREACH(zone, &keg->uk_zones, uz_link) 244920a4e154SJeff Roberson zfunc(zone, arg); 2450099a0e58SBosko Milekic } 245108034d10SKonstantin Belousov LIST_FOREACH(zone, &uma_cachezones, uz_link) 245220a4e154SJeff Roberson zfunc(zone, arg); 24532efcc8cbSGleb Smirnoff if (__predict_true(booted == BOOT_RUNNING)) 2454111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 24558355f576SJeff Roberson } 24568355f576SJeff Roberson 2457f4bef67cSGleb Smirnoff /* 2458f4bef67cSGleb Smirnoff * Count how many pages do we need to bootstrap. VM supplies 2459f4bef67cSGleb Smirnoff * its need in early zones in the argument, we add up our zones, 2460325c4cedSMark Johnston * which consist of the UMA Slabs, UMA Hash and 9 Bucket zones. The 2461f4bef67cSGleb Smirnoff * zone of zones and zone of kegs are accounted separately. 2462f4bef67cSGleb Smirnoff */ 2463325c4cedSMark Johnston #define UMA_BOOT_ZONES 11 24645073a083SGleb Smirnoff /* Zone of zones and zone of kegs have arbitrary alignment. */ 24655073a083SGleb Smirnoff #define UMA_BOOT_ALIGN 32 2466f4bef67cSGleb Smirnoff static int zsize, ksize; 2467f4bef67cSGleb Smirnoff int 2468f7d35785SGleb Smirnoff uma_startup_count(int vm_zones) 2469f4bef67cSGleb Smirnoff { 2470f7d35785SGleb Smirnoff int zones, pages; 24719b78b1f4SJeff Roberson size_t space, size; 2472f4bef67cSGleb Smirnoff 2473f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2474f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 2475f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2476f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2477f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 2478f4bef67cSGleb Smirnoff 24795073a083SGleb Smirnoff /* 24805073a083SGleb Smirnoff * Memory for the zone of kegs and its keg, 24815073a083SGleb Smirnoff * and for zone of zones. 24825073a083SGleb Smirnoff */ 2483f4bef67cSGleb Smirnoff pages = howmany(roundup(zsize, CACHE_LINE_SIZE) * 2 + 2484f4bef67cSGleb Smirnoff roundup(ksize, CACHE_LINE_SIZE), PAGE_SIZE); 2485f4bef67cSGleb Smirnoff 2486f7d35785SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2487f7d35785SGleb Smirnoff zones = UMA_BOOT_ZONES; 2488f7d35785SGleb Smirnoff #else 2489f7d35785SGleb Smirnoff zones = UMA_BOOT_ZONES + vm_zones; 2490f7d35785SGleb Smirnoff vm_zones = 0; 2491f7d35785SGleb Smirnoff #endif 24929b78b1f4SJeff Roberson size = slab_sizeof(SLAB_MAX_SETSIZE); 24939b78b1f4SJeff Roberson space = slab_space(SLAB_MAX_SETSIZE); 2494f4bef67cSGleb Smirnoff 24955073a083SGleb Smirnoff /* Memory for the rest of startup zones, UMA and VM, ... */ 24969b78b1f4SJeff Roberson if (zsize > space) { 24970b2e3aeaSGleb Smirnoff /* See keg_large_init(). */ 24980b2e3aeaSGleb Smirnoff u_int ppera; 24990b2e3aeaSGleb Smirnoff 25000b2e3aeaSGleb Smirnoff ppera = howmany(roundup2(zsize, UMA_BOOT_ALIGN), PAGE_SIZE); 25019b78b1f4SJeff Roberson if (PAGE_SIZE * ppera - roundup2(zsize, UMA_BOOT_ALIGN) < size) 25020b2e3aeaSGleb Smirnoff ppera++; 25030b2e3aeaSGleb Smirnoff pages += (zones + vm_zones) * ppera; 25049b78b1f4SJeff Roberson } else if (roundup2(zsize, UMA_BOOT_ALIGN) > space) 25050b2e3aeaSGleb Smirnoff /* See keg_small_init() special case for uk_ppera = 1. */ 250696a10340SGleb Smirnoff pages += zones; 2507f4bef67cSGleb Smirnoff else 25085073a083SGleb Smirnoff pages += howmany(zones, 25099b78b1f4SJeff Roberson space / roundup2(zsize, UMA_BOOT_ALIGN)); 2510f4bef67cSGleb Smirnoff 25115073a083SGleb Smirnoff /* ... and their kegs. Note that zone of zones allocates a keg! */ 25125073a083SGleb Smirnoff pages += howmany(zones + 1, 25139b78b1f4SJeff Roberson space / roundup2(ksize, UMA_BOOT_ALIGN)); 2514f4bef67cSGleb Smirnoff 2515f4bef67cSGleb Smirnoff return (pages); 2516f4bef67cSGleb Smirnoff } 2517f4bef67cSGleb Smirnoff 25188355f576SJeff Roberson void 2519ac0a6fd0SGleb Smirnoff uma_startup(void *mem, int npages) 25208355f576SJeff Roberson { 25218355f576SJeff Roberson struct uma_zctor_args args; 2522ab3185d1SJeff Roberson uma_keg_t masterkeg; 2523ab3185d1SJeff Roberson uintptr_t m; 2524f4bef67cSGleb Smirnoff 2525f4bef67cSGleb Smirnoff #ifdef DIAGNOSTIC 2526f4bef67cSGleb Smirnoff printf("Entering %s with %d boot pages configured\n", __func__, npages); 2527f4bef67cSGleb Smirnoff #endif 25288355f576SJeff Roberson 2529111fbcd5SBryan Venteicher rw_init(&uma_rwlock, "UMA lock"); 2530099a0e58SBosko Milekic 2531ab3185d1SJeff Roberson /* Use bootpages memory for the zone of zones and zone of kegs. */ 2532ab3185d1SJeff Roberson m = (uintptr_t)mem; 2533ab3185d1SJeff Roberson zones = (uma_zone_t)m; 2534ab3185d1SJeff Roberson m += roundup(zsize, CACHE_LINE_SIZE); 2535ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 2536ab3185d1SJeff Roberson m += roundup(zsize, CACHE_LINE_SIZE); 2537ab3185d1SJeff Roberson masterkeg = (uma_keg_t)m; 2538ab3185d1SJeff Roberson m += roundup(ksize, CACHE_LINE_SIZE); 2539ab3185d1SJeff Roberson m = roundup(m, PAGE_SIZE); 2540ab3185d1SJeff Roberson npages -= (m - (uintptr_t)mem) / PAGE_SIZE; 2541ab3185d1SJeff Roberson mem = (void *)m; 2542ab3185d1SJeff Roberson 2543099a0e58SBosko Milekic /* "manually" create the initial zone */ 25440095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2545099a0e58SBosko Milekic args.name = "UMA Kegs"; 2546ab3185d1SJeff Roberson args.size = ksize; 2547099a0e58SBosko Milekic args.ctor = keg_ctor; 2548099a0e58SBosko Milekic args.dtor = keg_dtor; 25498355f576SJeff Roberson args.uminit = zero_init; 25508355f576SJeff Roberson args.fini = NULL; 2551ab3185d1SJeff Roberson args.keg = masterkeg; 25525073a083SGleb Smirnoff args.align = UMA_BOOT_ALIGN - 1; 2553b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2554ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 25558355f576SJeff Roberson 2556ac0a6fd0SGleb Smirnoff bootmem = mem; 2557ac0a6fd0SGleb Smirnoff boot_pages = npages; 25588355f576SJeff Roberson 2559099a0e58SBosko Milekic args.name = "UMA Zones"; 2560f4bef67cSGleb Smirnoff args.size = zsize; 2561099a0e58SBosko Milekic args.ctor = zone_ctor; 2562099a0e58SBosko Milekic args.dtor = zone_dtor; 2563099a0e58SBosko Milekic args.uminit = zero_init; 2564099a0e58SBosko Milekic args.fini = NULL; 2565099a0e58SBosko Milekic args.keg = NULL; 25665073a083SGleb Smirnoff args.align = UMA_BOOT_ALIGN - 1; 2567099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2568ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2569099a0e58SBosko Milekic 25708355f576SJeff Roberson /* Now make a zone for slab headers */ 25711e0701e1SJeff Roberson slabzone = uma_zcreate("UMA Slabs", sizeof(struct uma_hash_slab), 25721e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 25738355f576SJeff Roberson 25748355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 25758355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 25761e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 25778355f576SJeff Roberson 2578f4bef67cSGleb Smirnoff booted = BOOT_STRAPPED; 25798355f576SJeff Roberson } 25808355f576SJeff Roberson 2581f4bef67cSGleb Smirnoff void 2582f4bef67cSGleb Smirnoff uma_startup1(void) 2583f4bef67cSGleb Smirnoff { 2584f4bef67cSGleb Smirnoff 2585f4bef67cSGleb Smirnoff #ifdef DIAGNOSTIC 2586f4bef67cSGleb Smirnoff printf("Entering %s with %d boot pages left\n", __func__, boot_pages); 2587f4bef67cSGleb Smirnoff #endif 2588f4bef67cSGleb Smirnoff booted = BOOT_PAGEALLOC; 2589f4bef67cSGleb Smirnoff } 2590f4bef67cSGleb Smirnoff 25918355f576SJeff Roberson void 259299571dc3SJeff Roberson uma_startup2(void) 25938355f576SJeff Roberson { 2594f4bef67cSGleb Smirnoff 2595f7d35785SGleb Smirnoff #ifdef DIAGNOSTIC 2596f7d35785SGleb Smirnoff printf("Entering %s with %d boot pages left\n", __func__, boot_pages); 2597f7d35785SGleb Smirnoff #endif 259808cfa56eSMark Johnston sx_init(&uma_reclaim_lock, "umareclaim"); 25993182660aSRyan Libby bucket_init(); 26003182660aSRyan Libby booted = BOOT_BUCKETS; 2601f4bef67cSGleb Smirnoff bucket_enable(); 26028355f576SJeff Roberson } 26038355f576SJeff Roberson 26048355f576SJeff Roberson /* 26058355f576SJeff Roberson * Initialize our callout handle 26068355f576SJeff Roberson * 26078355f576SJeff Roberson */ 26088355f576SJeff Roberson static void 26098355f576SJeff Roberson uma_startup3(void) 26108355f576SJeff Roberson { 26111431a748SGleb Smirnoff 2612c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2613c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2614c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2615c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2616c5deaf04SGleb Smirnoff #endif 261720a4e154SJeff Roberson zone_foreach(zone_alloc_counters, NULL); 261820a4e154SJeff Roberson zone_foreach(zone_alloc_sysctl, NULL); 2619fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 26209643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2621c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 26228355f576SJeff Roberson } 26238355f576SJeff Roberson 2624e20a199fSJeff Roberson static uma_keg_t 2625099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 262685dcf349SGleb Smirnoff int align, uint32_t flags) 2627099a0e58SBosko Milekic { 2628099a0e58SBosko Milekic struct uma_kctor_args args; 2629099a0e58SBosko Milekic 2630099a0e58SBosko Milekic args.size = size; 2631099a0e58SBosko Milekic args.uminit = uminit; 2632099a0e58SBosko Milekic args.fini = fini; 26331e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2634099a0e58SBosko Milekic args.flags = flags; 2635099a0e58SBosko Milekic args.zone = zone; 2636ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 2637099a0e58SBosko Milekic } 2638099a0e58SBosko Milekic 2639f4bef67cSGleb Smirnoff /* Public functions */ 26408355f576SJeff Roberson /* See uma.h */ 26411e319f6dSRobert Watson void 26421e319f6dSRobert Watson uma_set_align(int align) 26431e319f6dSRobert Watson { 26441e319f6dSRobert Watson 26451e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 26461e319f6dSRobert Watson uma_align_cache = align; 26471e319f6dSRobert Watson } 26481e319f6dSRobert Watson 26491e319f6dSRobert Watson /* See uma.h */ 26508355f576SJeff Roberson uma_zone_t 2651bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 265285dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 26538355f576SJeff Roberson 26548355f576SJeff Roberson { 26558355f576SJeff Roberson struct uma_zctor_args args; 265695c4bf75SKonstantin Belousov uma_zone_t res; 265795c4bf75SKonstantin Belousov bool locked; 26588355f576SJeff Roberson 2659a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 2660a5a35578SJohn Baldwin align, name)); 2661a5a35578SJohn Baldwin 2662c1685086SJeff Roberson /* Sets all zones to a first-touch domain policy. */ 2663c1685086SJeff Roberson #ifdef UMA_FIRSTTOUCH 2664c1685086SJeff Roberson flags |= UMA_ZONE_NUMA; 2665c1685086SJeff Roberson #endif 2666c1685086SJeff Roberson 26678355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 26680095a784SJeff Roberson memset(&args, 0, sizeof(args)); 26698355f576SJeff Roberson args.name = name; 26708355f576SJeff Roberson args.size = size; 26718355f576SJeff Roberson args.ctor = ctor; 26728355f576SJeff Roberson args.dtor = dtor; 26738355f576SJeff Roberson args.uminit = uminit; 26748355f576SJeff Roberson args.fini = fini; 2675afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 2676afc6dc36SJohn-Mark Gurney /* 2677ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 2678ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 2679ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 2680ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 2681ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 2682ca293436SRyan Libby * the ctor and dtor do not need to be empty. 2683ca293436SRyan Libby * 2684ca293436SRyan Libby * XXX UMA_ZONE_OFFPAGE. 2685afc6dc36SJohn-Mark Gurney */ 268619c591bfSMateusz Guzik if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOFREE))) && 2687ca293436SRyan Libby uminit == NULL && fini == NULL) { 2688afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 2689afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 2690afc6dc36SJohn-Mark Gurney } 2691afc6dc36SJohn-Mark Gurney #endif 26928355f576SJeff Roberson args.align = align; 26938355f576SJeff Roberson args.flags = flags; 2694099a0e58SBosko Milekic args.keg = NULL; 2695099a0e58SBosko Milekic 2696f4bef67cSGleb Smirnoff if (booted < BOOT_BUCKETS) { 269795c4bf75SKonstantin Belousov locked = false; 269895c4bf75SKonstantin Belousov } else { 269908cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 270095c4bf75SKonstantin Belousov locked = true; 270195c4bf75SKonstantin Belousov } 2702ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 270395c4bf75SKonstantin Belousov if (locked) 270408cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 270595c4bf75SKonstantin Belousov return (res); 2706099a0e58SBosko Milekic } 2707099a0e58SBosko Milekic 2708099a0e58SBosko Milekic /* See uma.h */ 2709099a0e58SBosko Milekic uma_zone_t 2710099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 2711099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 2712099a0e58SBosko Milekic { 2713099a0e58SBosko Milekic struct uma_zctor_args args; 2714e20a199fSJeff Roberson uma_keg_t keg; 271595c4bf75SKonstantin Belousov uma_zone_t res; 271695c4bf75SKonstantin Belousov bool locked; 2717099a0e58SBosko Milekic 2718bb15d1c7SGleb Smirnoff keg = master->uz_keg; 27190095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2720099a0e58SBosko Milekic args.name = name; 2721e20a199fSJeff Roberson args.size = keg->uk_size; 2722099a0e58SBosko Milekic args.ctor = ctor; 2723099a0e58SBosko Milekic args.dtor = dtor; 2724099a0e58SBosko Milekic args.uminit = zinit; 2725099a0e58SBosko Milekic args.fini = zfini; 2726e20a199fSJeff Roberson args.align = keg->uk_align; 2727e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 2728e20a199fSJeff Roberson args.keg = keg; 27298355f576SJeff Roberson 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 } 2736e20a199fSJeff Roberson /* XXX Attaches only one keg of potentially many. */ 2737ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 273895c4bf75SKonstantin Belousov if (locked) 273908cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 274095c4bf75SKonstantin Belousov return (res); 27418355f576SJeff Roberson } 27428355f576SJeff Roberson 27430095a784SJeff Roberson /* See uma.h */ 27440095a784SJeff Roberson uma_zone_t 2745af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 2746af526374SJeff Roberson uma_init zinit, uma_fini zfini, uma_import zimport, 2747af526374SJeff Roberson uma_release zrelease, void *arg, int flags) 27480095a784SJeff Roberson { 27490095a784SJeff Roberson struct uma_zctor_args args; 27500095a784SJeff Roberson 27510095a784SJeff Roberson memset(&args, 0, sizeof(args)); 27520095a784SJeff Roberson args.name = name; 2753af526374SJeff Roberson args.size = size; 27540095a784SJeff Roberson args.ctor = ctor; 27550095a784SJeff Roberson args.dtor = dtor; 27560095a784SJeff Roberson args.uminit = zinit; 27570095a784SJeff Roberson args.fini = zfini; 27580095a784SJeff Roberson args.import = zimport; 27590095a784SJeff Roberson args.release = zrelease; 27600095a784SJeff Roberson args.arg = arg; 27610095a784SJeff Roberson args.align = 0; 2762bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 27630095a784SJeff Roberson 2764ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 27650095a784SJeff Roberson } 27660095a784SJeff Roberson 27678355f576SJeff Roberson /* See uma.h */ 27689c2cd7e5SJeff Roberson void 27699c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 27709c2cd7e5SJeff Roberson { 2771f4ff923bSRobert Watson 277208cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 27730095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 277408cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 27759c2cd7e5SJeff Roberson } 27769c2cd7e5SJeff Roberson 27778d6fbbb8SJeff Roberson void 27788d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 27798d6fbbb8SJeff Roberson { 27808d6fbbb8SJeff Roberson void *item; 27818d6fbbb8SJeff Roberson 27828d6fbbb8SJeff Roberson item = uma_zalloc_arg(zone, NULL, M_WAITOK); 27838d6fbbb8SJeff Roberson uma_zfree(zone, item); 27848d6fbbb8SJeff Roberson } 27858d6fbbb8SJeff Roberson 27864e180881SMateusz Guzik void * 27874e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 27884e180881SMateusz Guzik { 27894e180881SMateusz Guzik void *item; 2790b4799947SRuslan Bukin #ifdef SMP 27914e180881SMateusz Guzik int i; 27924e180881SMateusz Guzik 27934e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2794b4799947SRuslan Bukin #endif 27954e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 27964e180881SMateusz Guzik if (item != NULL && (flags & M_ZERO)) { 2797b4799947SRuslan Bukin #ifdef SMP 2798013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 27994e180881SMateusz Guzik bzero(zpcpu_get_cpu(item, i), zone->uz_size); 2800b4799947SRuslan Bukin #else 2801b4799947SRuslan Bukin bzero(item, zone->uz_size); 2802b4799947SRuslan Bukin #endif 28034e180881SMateusz Guzik } 28044e180881SMateusz Guzik return (item); 28054e180881SMateusz Guzik } 28064e180881SMateusz Guzik 28074e180881SMateusz Guzik /* 28084e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 28094e180881SMateusz Guzik */ 28104e180881SMateusz Guzik void 28114e180881SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata) 28124e180881SMateusz Guzik { 28134e180881SMateusz Guzik 2814c5b7751fSIan Lepore #ifdef SMP 28154e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2816c5b7751fSIan Lepore #endif 28174e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 28184e180881SMateusz Guzik } 28194e180881SMateusz Guzik 2820cc7ce83aSJeff Roberson #ifdef INVARIANTS 2821cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 1 2822cc7ce83aSJeff Roberson #else 2823cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 0 2824cc7ce83aSJeff Roberson #endif 2825cc7ce83aSJeff Roberson 2826beb8beefSJeff Roberson static void * 2827cc7ce83aSJeff Roberson item_ctor(uma_zone_t zone, int size, void *udata, int flags, void *item) 2828beb8beefSJeff Roberson { 2829beb8beefSJeff Roberson #ifdef INVARIANTS 2830ca293436SRyan Libby bool skipdbg; 2831beb8beefSJeff Roberson 2832beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 2833ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2834ca293436SRyan Libby zone->uz_ctor != trash_ctor) 2835cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 2836beb8beefSJeff Roberson #endif 2837ca293436SRyan Libby if (__predict_false(zone->uz_ctor != NULL) && 2838cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 2839beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 2840beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 2841beb8beefSJeff Roberson return (NULL); 2842beb8beefSJeff Roberson } 2843beb8beefSJeff Roberson #ifdef INVARIANTS 2844beb8beefSJeff Roberson if (!skipdbg) 2845beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 2846beb8beefSJeff Roberson #endif 2847beb8beefSJeff Roberson if (flags & M_ZERO) 2848cc7ce83aSJeff Roberson bzero(item, size); 2849beb8beefSJeff Roberson 2850beb8beefSJeff Roberson return (item); 2851beb8beefSJeff Roberson } 2852beb8beefSJeff Roberson 2853ca293436SRyan Libby static inline void 2854cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 2855cc7ce83aSJeff Roberson enum zfreeskip skip) 2856ca293436SRyan Libby { 2857ca293436SRyan Libby #ifdef INVARIANTS 2858ca293436SRyan Libby bool skipdbg; 2859ca293436SRyan Libby 2860ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 2861ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 2862ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 2863ca293436SRyan Libby uma_dbg_free(zone, udata, item); 2864ca293436SRyan Libby else 2865ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 2866ca293436SRyan Libby } 2867ca293436SRyan Libby #endif 2868cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 2869ca293436SRyan Libby if (zone->uz_dtor != NULL) 2870cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 2871ca293436SRyan Libby #ifdef INVARIANTS 2872ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2873ca293436SRyan Libby zone->uz_dtor != trash_dtor) 2874cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 2875ca293436SRyan Libby #endif 2876ca293436SRyan Libby } 2877ca293436SRyan Libby } 2878ca293436SRyan Libby 28799c2cd7e5SJeff Roberson /* See uma.h */ 28808355f576SJeff Roberson void * 28812cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 28828355f576SJeff Roberson { 2883376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 2884ab3185d1SJeff Roberson uma_cache_t cache; 2885ab3185d1SJeff Roberson void *item; 2886cc7ce83aSJeff Roberson int domain, size, uz_flags; 28878355f576SJeff Roberson 2888e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 288919fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 289010cb2424SMark Murray 28918355f576SJeff Roberson /* This is the fast path allocation */ 28921431a748SGleb Smirnoff CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d", 28931431a748SGleb Smirnoff curthread, zone->uz_name, zone, flags); 2894a553d4b8SJeff Roberson 2895cc7ce83aSJeff Roberson #ifdef WITNESS 2896635fd505SRobert Watson if (flags & M_WAITOK) { 2897b23f72e9SBrian Feldman WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2898635fd505SRobert Watson "uma_zalloc_arg: zone \"%s\"", zone->uz_name); 28994c1cc01cSJohn Baldwin } 2900cc7ce83aSJeff Roberson #endif 2901cc7ce83aSJeff Roberson 2902cc7ce83aSJeff Roberson #ifdef INVARIANTS 29030766f278SJonathan T. Looney KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC")); 2904d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 29051067a2baSJonathan T. Looney ("uma_zalloc_arg: called with spinlock or critical section held")); 2906ea99223eSMateusz Guzik if (zone->uz_flags & UMA_ZONE_PCPU) 2907b8af2820SMateusz Guzik KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone " 2908b8af2820SMateusz Guzik "with M_ZERO passed")); 2909cc7ce83aSJeff Roberson #endif 29101067a2baSJonathan T. Looney 29118d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 29128d689e04SGleb Smirnoff if (memguard_cmp_zone(zone)) { 29138d689e04SGleb Smirnoff item = memguard_alloc(zone->uz_size, flags); 29148d689e04SGleb Smirnoff if (item != NULL) { 29158d689e04SGleb Smirnoff if (zone->uz_init != NULL && 29168d689e04SGleb Smirnoff zone->uz_init(item, zone->uz_size, flags) != 0) 29178d689e04SGleb Smirnoff return (NULL); 29188d689e04SGleb Smirnoff if (zone->uz_ctor != NULL && 2919fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 2920fc03d22bSJeff Roberson flags) != 0) { 2921ca293436SRyan Libby counter_u64_add(zone->uz_fails, 1); 29228d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 29238d689e04SGleb Smirnoff return (NULL); 29248d689e04SGleb Smirnoff } 29258d689e04SGleb Smirnoff return (item); 29268d689e04SGleb Smirnoff } 29278d689e04SGleb Smirnoff /* This is unfortunate but should not be fatal. */ 29288d689e04SGleb Smirnoff } 29298d689e04SGleb Smirnoff #endif 29305d1ae027SRobert Watson /* 29315d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 29325d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 29335d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 29345d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 29355d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 29365d1ae027SRobert Watson * preemption and migration. We release the critical section in 29375d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 29385d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 29395d1ae027SRobert Watson * must detect and handle migration if it has occurred. 29405d1ae027SRobert Watson */ 29415d1ae027SRobert Watson critical_enter(); 2942beb8beefSJeff Roberson do { 2943cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 2944376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 2945cc7ce83aSJeff Roberson size = cache_uz_size(cache); 2946cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 2947376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 2948376b1ba3SJeff Roberson item = cache_bucket_pop(cache, bucket); 29495d1ae027SRobert Watson critical_exit(); 2950cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 2951cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 2952cc7ce83aSJeff Roberson return (item_ctor(zone, size, udata, flags, item)); 2953cc7ce83aSJeff Roberson if (flags & M_ZERO) 2954cc7ce83aSJeff Roberson bzero(item, size); 2955cc7ce83aSJeff Roberson return (item); 2956b23f72e9SBrian Feldman } 2957beb8beefSJeff Roberson } while (cache_alloc(zone, cache, udata, flags)); 2958beb8beefSJeff Roberson critical_exit(); 2959beb8beefSJeff Roberson 2960beb8beefSJeff Roberson /* 2961beb8beefSJeff Roberson * We can not get a bucket so try to return a single item. 2962beb8beefSJeff Roberson */ 2963cc7ce83aSJeff Roberson if (uz_flags & UMA_ZONE_NUMA) 2964beb8beefSJeff Roberson domain = PCPU_GET(domain); 2965beb8beefSJeff Roberson else 2966beb8beefSJeff Roberson domain = UMA_ANYDOMAIN; 29674bd61e19SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 2968fc03d22bSJeff Roberson } 2969fc03d22bSJeff Roberson 29708355f576SJeff Roberson /* 2971beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 2972beb8beefSJeff Roberson * a critical section. Returns in a critical section. 2973beb8beefSJeff Roberson * 29744bd61e19SJeff Roberson * A false return value indicates an allocation failure. 29754bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 2976beb8beefSJeff Roberson */ 2977beb8beefSJeff Roberson static __noinline bool 2978beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 2979beb8beefSJeff Roberson { 2980beb8beefSJeff Roberson uma_zone_domain_t zdom; 2981beb8beefSJeff Roberson uma_bucket_t bucket; 2982cc7ce83aSJeff Roberson int domain; 2983beb8beefSJeff Roberson bool lockfail; 2984beb8beefSJeff Roberson 2985beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 2986beb8beefSJeff Roberson 2987beb8beefSJeff Roberson /* 2988beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 2989beb8beefSJeff Roberson * if we can switch with the free bucket. 29908355f576SJeff Roberson */ 2991376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_cnt != 0) { 2992376b1ba3SJeff Roberson cache_bucket_swap(&cache->uc_freebucket, &cache->uc_allocbucket); 2993beb8beefSJeff Roberson return (true); 29948355f576SJeff Roberson } 2995fc03d22bSJeff Roberson 2996fc03d22bSJeff Roberson /* 2997fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 2998fc03d22bSJeff Roberson */ 2999376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3000fc03d22bSJeff Roberson critical_exit(); 3001fc03d22bSJeff Roberson if (bucket != NULL) 30026fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3003fc03d22bSJeff Roberson 30044bd61e19SJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 30054bd61e19SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) { 30064bd61e19SJeff Roberson critical_enter(); 30074bd61e19SJeff Roberson return (false); 30084bd61e19SJeff Roberson } 30094bd61e19SJeff Roberson 30105d1ae027SRobert Watson /* 30115d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 30125d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 30135d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 30145d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 30155d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 30165d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 30175d1ae027SRobert Watson * the critical section. 30185d1ae027SRobert Watson */ 3019fc03d22bSJeff Roberson lockfail = 0; 3020fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 3021fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 3022a553d4b8SJeff Roberson ZONE_LOCK(zone); 3023fc03d22bSJeff Roberson lockfail = 1; 3024fc03d22bSJeff Roberson } 3025beb8beefSJeff Roberson 3026fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 30274bd61e19SJeff Roberson critical_enter(); 30284bd61e19SJeff Roberson cache = &zone->uz_cpu[curcpu]; 3029376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket != NULL) { 3030fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 3031beb8beefSJeff Roberson return (true); 3032a553d4b8SJeff Roberson } 30338355f576SJeff Roberson 3034fc03d22bSJeff Roberson /* 3035fc03d22bSJeff Roberson * Check the zone's cache of buckets. 3036fc03d22bSJeff Roberson */ 3037c1685086SJeff Roberson if (zone->uz_flags & UMA_ZONE_NUMA) { 3038c1685086SJeff Roberson domain = PCPU_GET(domain); 3039ab3185d1SJeff Roberson zdom = &zone->uz_domain[domain]; 3040c1685086SJeff Roberson } else { 3041c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3042c1685086SJeff Roberson zdom = &zone->uz_domain[0]; 3043c1685086SJeff Roberson } 3044c1685086SJeff Roberson 304508cfa56eSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom)) != NULL) { 3046beb8beefSJeff Roberson ZONE_UNLOCK(zone); 3047cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 3048a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 3049376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3050beb8beefSJeff Roberson return (true); 3051a553d4b8SJeff Roberson } 30525d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 30535d1ae027SRobert Watson critical_exit(); 3054bbee39c6SJeff Roberson 3055fc03d22bSJeff Roberson /* 3056fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 3057fc03d22bSJeff Roberson * handle the working set. 3058fc03d22bSJeff Roberson */ 305920a4e154SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 306020a4e154SJeff Roberson zone->uz_bucket_size++; 30614bd61e19SJeff Roberson ZONE_UNLOCK(zone); 3062bb15d1c7SGleb Smirnoff 30638355f576SJeff Roberson /* 3064beb8beefSJeff Roberson * Fill a bucket and attempt to use it as the alloc bucket. 3065bbee39c6SJeff Roberson */ 3066beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 30671431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 30681431a748SGleb Smirnoff zone->uz_name, zone, bucket); 30694bd61e19SJeff Roberson if (bucket == NULL) { 3070fc03d22bSJeff Roberson critical_enter(); 3071beb8beefSJeff Roberson return (false); 30724bd61e19SJeff Roberson } 30730f9b7bf3SMark Johnston 3074fc03d22bSJeff Roberson /* 3075fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3076fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3077fc03d22bSJeff Roberson * the memory directly. 3078fc03d22bSJeff Roberson */ 30794bd61e19SJeff Roberson ZONE_LOCK(zone); 30804bd61e19SJeff Roberson critical_enter(); 3081cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3082376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 308381c0d72cSGleb Smirnoff ((zone->uz_flags & UMA_ZONE_NUMA) == 0 || 308481c0d72cSGleb Smirnoff domain == PCPU_GET(domain))) { 3085376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 30860f9b7bf3SMark Johnston zdom->uzd_imax += bucket->ub_cnt; 3087bb15d1c7SGleb Smirnoff } else if (zone->uz_bkt_count >= zone->uz_bkt_max) { 308881c0d72cSGleb Smirnoff critical_exit(); 308981c0d72cSGleb Smirnoff ZONE_UNLOCK(zone); 309081c0d72cSGleb Smirnoff bucket_drain(zone, bucket); 309181c0d72cSGleb Smirnoff bucket_free(zone, bucket, udata); 3092beb8beefSJeff Roberson critical_enter(); 3093beb8beefSJeff Roberson return (true); 309481c0d72cSGleb Smirnoff } else 30950f9b7bf3SMark Johnston zone_put_bucket(zone, zdom, bucket, false); 3096bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 3097beb8beefSJeff Roberson return (true); 3098bbee39c6SJeff Roberson } 3099bbee39c6SJeff Roberson 3100ab3185d1SJeff Roberson void * 3101ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3102bbee39c6SJeff Roberson { 3103ab3185d1SJeff Roberson 3104ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 310519fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3106ab3185d1SJeff Roberson 3107ab3185d1SJeff Roberson /* This is the fast path allocation */ 3108ab3185d1SJeff Roberson CTR5(KTR_UMA, 3109ab3185d1SJeff Roberson "uma_zalloc_domain thread %x zone %s(%p) domain %d flags %d", 3110ab3185d1SJeff Roberson curthread, zone->uz_name, zone, domain, flags); 3111ab3185d1SJeff Roberson 3112ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3113ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3114ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3115ab3185d1SJeff Roberson } 3116ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3117ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 3118ab3185d1SJeff Roberson 3119ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3120ab3185d1SJeff Roberson } 3121ab3185d1SJeff Roberson 3122ab3185d1SJeff Roberson /* 3123ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3124ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3125ab3185d1SJeff Roberson * 3126ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3127ab3185d1SJeff Roberson * only 'domain'. 3128ab3185d1SJeff Roberson */ 3129ab3185d1SJeff Roberson static uma_slab_t 3130194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3131ab3185d1SJeff Roberson { 3132ab3185d1SJeff Roberson uma_domain_t dom; 3133bbee39c6SJeff Roberson uma_slab_t slab; 3134ab3185d1SJeff Roberson int start; 3135ab3185d1SJeff Roberson 3136ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3137ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 3138bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 3139ab3185d1SJeff Roberson 3140ab3185d1SJeff Roberson slab = NULL; 3141ab3185d1SJeff Roberson start = domain; 3142ab3185d1SJeff Roberson do { 3143ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 3144ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_part_slab)) 3145ab3185d1SJeff Roberson return (LIST_FIRST(&dom->ud_part_slab)); 3146ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_free_slab)) { 3147ab3185d1SJeff Roberson slab = LIST_FIRST(&dom->ud_free_slab); 3148ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 3149ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3150ab3185d1SJeff Roberson return (slab); 3151ab3185d1SJeff Roberson } 3152ab3185d1SJeff Roberson if (rr) 3153ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3154ab3185d1SJeff Roberson } while (domain != start); 3155ab3185d1SJeff Roberson 3156ab3185d1SJeff Roberson return (NULL); 3157ab3185d1SJeff Roberson } 3158ab3185d1SJeff Roberson 3159ab3185d1SJeff Roberson static uma_slab_t 3160194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3161ab3185d1SJeff Roberson { 3162194a979eSMark Johnston uint32_t reserve; 3163099a0e58SBosko Milekic 3164bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 3165194a979eSMark Johnston 3166194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 3167194a979eSMark Johnston if (keg->uk_free <= reserve) 3168194a979eSMark Johnston return (NULL); 3169194a979eSMark Johnston return (keg_first_slab(keg, domain, rr)); 3170194a979eSMark Johnston } 3171194a979eSMark Johnston 3172194a979eSMark Johnston static uma_slab_t 3173194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3174194a979eSMark Johnston { 3175194a979eSMark Johnston struct vm_domainset_iter di; 3176194a979eSMark Johnston uma_domain_t dom; 3177194a979eSMark Johnston uma_slab_t slab; 3178194a979eSMark Johnston int aflags, domain; 3179194a979eSMark Johnston bool rr; 3180194a979eSMark Johnston 3181194a979eSMark Johnston restart: 3182bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 3183bbee39c6SJeff Roberson 3184bbee39c6SJeff Roberson /* 3185194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3186194a979eSMark Johnston * domain (as happens with first-touch zones). 3187194a979eSMark Johnston * 3188194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3189194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3190194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3191bbee39c6SJeff Roberson */ 3192ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3193ab3185d1SJeff Roberson if (rr) { 3194194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3195194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3196194a979eSMark Johnston &aflags); 3197194a979eSMark Johnston } else { 3198194a979eSMark Johnston aflags = flags; 3199194a979eSMark Johnston domain = rdomain; 3200194a979eSMark Johnston } 3201ab3185d1SJeff Roberson 3202194a979eSMark Johnston for (;;) { 3203194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3204584061b4SJeff Roberson if (slab != NULL) 3205bbee39c6SJeff Roberson return (slab); 3206bbee39c6SJeff Roberson 3207bbee39c6SJeff Roberson /* 3208bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3209bbee39c6SJeff Roberson */ 3210bbee39c6SJeff Roberson if (flags & M_NOVM) 3211bbee39c6SJeff Roberson break; 3212bbee39c6SJeff Roberson 321386220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 3214bbee39c6SJeff Roberson /* 3215bbee39c6SJeff Roberson * If we got a slab here it's safe to mark it partially used 3216bbee39c6SJeff Roberson * and return. We assume that the caller is going to remove 3217bbee39c6SJeff Roberson * at least one item. 3218bbee39c6SJeff Roberson */ 3219bbee39c6SJeff Roberson if (slab) { 3220ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3221ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3222bbee39c6SJeff Roberson return (slab); 3223bbee39c6SJeff Roberson } 32243639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 32253639ac42SJeff Roberson break; 3226194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3227194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 3228194a979eSMark Johnston KEG_UNLOCK(keg); 3229194a979eSMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 3230194a979eSMark Johnston KEG_LOCK(keg); 3231194a979eSMark Johnston goto restart; 323230c5525bSAndrew Gallatin } 3233194a979eSMark Johnston break; 3234194a979eSMark Johnston } 3235ab3185d1SJeff Roberson } 3236ab3185d1SJeff Roberson 3237bbee39c6SJeff Roberson /* 3238bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3239bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3240bbee39c6SJeff Roberson * fail. 3241bbee39c6SJeff Roberson */ 3242194a979eSMark Johnston if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) { 3243bbee39c6SJeff Roberson return (slab); 3244bbee39c6SJeff Roberson } 3245ab3185d1SJeff Roberson return (NULL); 3246ab3185d1SJeff Roberson } 3247bbee39c6SJeff Roberson 3248d56368d7SBosko Milekic static void * 32490095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3250bbee39c6SJeff Roberson { 3251ab3185d1SJeff Roberson uma_domain_t dom; 3252bbee39c6SJeff Roberson void *item; 325385dcf349SGleb Smirnoff uint8_t freei; 3254bbee39c6SJeff Roberson 3255bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 3256099a0e58SBosko Milekic 32579b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 32589b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 32591e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3260bbee39c6SJeff Roberson slab->us_freecount--; 3261099a0e58SBosko Milekic keg->uk_free--; 3262ef72505eSJeff Roberson 3263bbee39c6SJeff Roberson /* Move this slab to the full list */ 3264bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3265bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3266ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3267ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3268bbee39c6SJeff Roberson } 3269bbee39c6SJeff Roberson 3270bbee39c6SJeff Roberson return (item); 3271bbee39c6SJeff Roberson } 3272bbee39c6SJeff Roberson 3273bbee39c6SJeff Roberson static int 3274b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 32750095a784SJeff Roberson { 3276b75c4efcSAndrew Turner uma_zone_t zone; 32770095a784SJeff Roberson uma_slab_t slab; 32780095a784SJeff Roberson uma_keg_t keg; 3279a03af342SSean Bruno #ifdef NUMA 3280ab3185d1SJeff Roberson int stripe; 3281a03af342SSean Bruno #endif 32820095a784SJeff Roberson int i; 32830095a784SJeff Roberson 3284b75c4efcSAndrew Turner zone = arg; 32850095a784SJeff Roberson slab = NULL; 3286584061b4SJeff Roberson keg = zone->uz_keg; 3287584061b4SJeff Roberson KEG_LOCK(keg); 3288af526374SJeff Roberson /* Try to keep the buckets totally full */ 32890095a784SJeff Roberson for (i = 0; i < max; ) { 3290584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 32910095a784SJeff Roberson break; 3292a03af342SSean Bruno #ifdef NUMA 3293ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3294a03af342SSean Bruno #endif 32956fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 32960095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 32976fd34d6fSJeff Roberson if (keg->uk_free <= keg->uk_reserve) 32986fd34d6fSJeff Roberson break; 3299b6715dabSJeff Roberson #ifdef NUMA 3300ab3185d1SJeff Roberson /* 3301ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3302ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3303ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3304ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3305ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3306ab3185d1SJeff Roberson * time but yields better distribution. 3307ab3185d1SJeff Roberson */ 3308ab3185d1SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) == 0 && 3309ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3310ab3185d1SJeff Roberson break; 3311ab3185d1SJeff Roberson #endif 33126fd34d6fSJeff Roberson } 3313ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 33140095a784SJeff Roberson flags &= ~M_WAITOK; 33150095a784SJeff Roberson flags |= M_NOWAIT; 33160095a784SJeff Roberson } 33170095a784SJeff Roberson KEG_UNLOCK(keg); 33180095a784SJeff Roberson 33190095a784SJeff Roberson return i; 33200095a784SJeff Roberson } 33210095a784SJeff Roberson 33224bd61e19SJeff Roberson static int 33234bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 33244bd61e19SJeff Roberson { 33254bd61e19SJeff Roberson uint64_t old, new, total, max; 33264bd61e19SJeff Roberson 33274bd61e19SJeff Roberson /* 33284bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 33294bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 33304bd61e19SJeff Roberson * fairness by keeping fifo order. 33314bd61e19SJeff Roberson * 33324bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 33334bd61e19SJeff Roberson */ 33344bd61e19SJeff Roberson for (;;) { 33354bd61e19SJeff Roberson zone_free_limit(zone, count); 33364bd61e19SJeff Roberson zone_log_warning(zone); 33374bd61e19SJeff Roberson zone_maxaction(zone); 33384bd61e19SJeff Roberson if (flags & M_NOWAIT) 33394bd61e19SJeff Roberson return (0); 33404bd61e19SJeff Roberson 33414bd61e19SJeff Roberson /* 33424bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 33434bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 33444bd61e19SJeff Roberson * is essentially a home rolled semaphore. 33454bd61e19SJeff Roberson */ 33464bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 33474bd61e19SJeff Roberson old = zone->uz_items; 33484bd61e19SJeff Roberson do { 33494bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 33504bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 33514bd61e19SJeff Roberson max = zone->uz_max_items; 33524bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 33534bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 33544bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 33554bd61e19SJeff Roberson else 33564bd61e19SJeff Roberson new = old + MIN(count, max - old); 33574bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 33584bd61e19SJeff Roberson 33594bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 33604bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 33614bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 33624bd61e19SJeff Roberson return (new - old); 33634bd61e19SJeff Roberson } 33644bd61e19SJeff Roberson 33654bd61e19SJeff Roberson /* 33664bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 33674bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 33684bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 33694bd61e19SJeff Roberson * transitions. 33704bd61e19SJeff Roberson */ 33714bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 33724bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 33734bd61e19SJeff Roberson 33744bd61e19SJeff Roberson /* 33754bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 33764bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 33774bd61e19SJeff Roberson */ 33784bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 33794bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 33804bd61e19SJeff Roberson 33814bd61e19SJeff Roberson /* 33824bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 33834bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 33844bd61e19SJeff Roberson * 33854bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 33864bd61e19SJeff Roberson * our count. 33874bd61e19SJeff Roberson */ 33884bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 33894bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 33904bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 33914bd61e19SJeff Roberson /* We're no longer a sleeper. */ 33924bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 33934bd61e19SJeff Roberson 33944bd61e19SJeff Roberson /* 33954bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 33964bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 33974bd61e19SJeff Roberson * against changes via sysctl. 33984bd61e19SJeff Roberson */ 33994bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 34004bd61e19SJeff Roberson max = zone->uz_max_items; 34014bd61e19SJeff Roberson if (total >= max) 34024bd61e19SJeff Roberson continue; 34034bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 34044bd61e19SJeff Roberson if (total + count > max) { 34054bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 34064bd61e19SJeff Roberson count = max - total; 34074bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 34084bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 34094bd61e19SJeff Roberson 34104bd61e19SJeff Roberson return (count); 34114bd61e19SJeff Roberson } 34124bd61e19SJeff Roberson } 34134bd61e19SJeff Roberson 34144bd61e19SJeff Roberson /* 34154bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 34164bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 34174bd61e19SJeff Roberson * one item can be allocated. 34184bd61e19SJeff Roberson */ 34194bd61e19SJeff Roberson static int 34204bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 34214bd61e19SJeff Roberson { 34224bd61e19SJeff Roberson uint64_t old; 34234bd61e19SJeff Roberson uint64_t max; 34244bd61e19SJeff Roberson 34254bd61e19SJeff Roberson max = zone->uz_max_items; 34264bd61e19SJeff Roberson MPASS(max > 0); 34274bd61e19SJeff Roberson 34284bd61e19SJeff Roberson /* 34294bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 34304bd61e19SJeff Roberson * fetchadd. 34314bd61e19SJeff Roberson */ 34324bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 34334bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 34344bd61e19SJeff Roberson return (count); 34354bd61e19SJeff Roberson 34364bd61e19SJeff Roberson /* 34374bd61e19SJeff Roberson * If we had some items and no sleepers just return the 34384bd61e19SJeff Roberson * truncated value. We have to release the excess space 34394bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 34404bd61e19SJeff Roberson * because we were temporarily over the limit. 34414bd61e19SJeff Roberson */ 34424bd61e19SJeff Roberson if (old < max) { 34434bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 34444bd61e19SJeff Roberson return (max - old); 34454bd61e19SJeff Roberson } 34464bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 34474bd61e19SJeff Roberson } 34484bd61e19SJeff Roberson 34494bd61e19SJeff Roberson /* 34504bd61e19SJeff Roberson * Free a number of items back to the limit. 34514bd61e19SJeff Roberson */ 34524bd61e19SJeff Roberson static void 34534bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 34544bd61e19SJeff Roberson { 34554bd61e19SJeff Roberson uint64_t old; 34564bd61e19SJeff Roberson 34574bd61e19SJeff Roberson MPASS(count > 0); 34584bd61e19SJeff Roberson 34594bd61e19SJeff Roberson /* 34604bd61e19SJeff Roberson * In the common case we either have no sleepers or 34614bd61e19SJeff Roberson * are still over the limit and can just return. 34624bd61e19SJeff Roberson */ 34634bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 34644bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 34654bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 34664bd61e19SJeff Roberson return; 34674bd61e19SJeff Roberson 34684bd61e19SJeff Roberson /* 34694bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 34704bd61e19SJeff Roberson * to generate wakeups if necessary. 34714bd61e19SJeff Roberson */ 34724bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 34734bd61e19SJeff Roberson } 34744bd61e19SJeff Roberson 3475fc03d22bSJeff Roberson static uma_bucket_t 3476beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3477bbee39c6SJeff Roberson { 3478bbee39c6SJeff Roberson uma_bucket_t bucket; 3479beb8beefSJeff Roberson int maxbucket, cnt; 3480bbee39c6SJeff Roberson 348130c5525bSAndrew Gallatin CTR1(KTR_UMA, "zone_alloc:_bucket domain %d)", domain); 348230c5525bSAndrew Gallatin 3483c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3484c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3485c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3486c1685086SJeff Roberson 34874bd61e19SJeff Roberson if (zone->uz_max_items > 0) 34884bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 34894bd61e19SJeff Roberson M_NOWAIT); 34904bd61e19SJeff Roberson else 349120a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 34924bd61e19SJeff Roberson if (maxbucket == 0) 34934bd61e19SJeff Roberson return (false); 3494beb8beefSJeff Roberson 34956fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 34966fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3497beb8beefSJeff Roberson if (bucket == NULL) { 3498beb8beefSJeff Roberson cnt = 0; 3499beb8beefSJeff Roberson goto out; 3500beb8beefSJeff Roberson } 35010095a784SJeff Roberson 35020095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3503beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 35040095a784SJeff Roberson 35050095a784SJeff Roberson /* 35060095a784SJeff Roberson * Initialize the memory if necessary. 35070095a784SJeff Roberson */ 35080095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 3509099a0e58SBosko Milekic int i; 3510bbee39c6SJeff Roberson 35110095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 3512e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 35130095a784SJeff Roberson flags) != 0) 3514b23f72e9SBrian Feldman break; 3515b23f72e9SBrian Feldman /* 3516b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 3517b23f72e9SBrian Feldman * rest back onto the freelist. 3518b23f72e9SBrian Feldman */ 3519b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 3520af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 35210095a784SJeff Roberson bucket->ub_cnt - i); 3522a5a262c6SBosko Milekic #ifdef INVARIANTS 35230095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 35240095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 3525a5a262c6SBosko Milekic #endif 3526b23f72e9SBrian Feldman bucket->ub_cnt = i; 3527b23f72e9SBrian Feldman } 3528099a0e58SBosko Milekic } 3529099a0e58SBosko Milekic 3530beb8beefSJeff Roberson cnt = bucket->ub_cnt; 3531f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 35326fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 35332efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 3534beb8beefSJeff Roberson bucket = NULL; 3535beb8beefSJeff Roberson } 3536beb8beefSJeff Roberson out: 35374bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 35384bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 3539fc03d22bSJeff Roberson 3540fc03d22bSJeff Roberson return (bucket); 3541fc03d22bSJeff Roberson } 3542fc03d22bSJeff Roberson 35438355f576SJeff Roberson /* 35440095a784SJeff Roberson * Allocates a single item from a zone. 35458355f576SJeff Roberson * 35468355f576SJeff Roberson * Arguments 35478355f576SJeff Roberson * zone The zone to alloc for. 35488355f576SJeff Roberson * udata The data to be passed to the constructor. 3549ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 3550a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 35518355f576SJeff Roberson * 35528355f576SJeff Roberson * Returns 35538355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 3554bbee39c6SJeff Roberson * An item if successful 35558355f576SJeff Roberson */ 35568355f576SJeff Roberson 35578355f576SJeff Roberson static void * 3558ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 35598355f576SJeff Roberson { 35608355f576SJeff Roberson void *item; 35618355f576SJeff Roberson 35624bd61e19SJeff Roberson if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) 3563bb15d1c7SGleb Smirnoff return (NULL); 35648355f576SJeff Roberson 3565c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3566c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 356730c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 3568c1685086SJeff Roberson 3569ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 3570beb8beefSJeff Roberson goto fail_cnt; 35718355f576SJeff Roberson 3572099a0e58SBosko Milekic /* 3573099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 3574099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 3575099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 3576099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 3577099a0e58SBosko Milekic */ 3578b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 3579e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 3580bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 3581beb8beefSJeff Roberson goto fail_cnt; 3582beb8beefSJeff Roberson } 3583beb8beefSJeff Roberson } 3584cc7ce83aSJeff Roberson item = item_ctor(zone, zone->uz_size, udata, flags, item); 3585beb8beefSJeff Roberson if (item == NULL) 35860095a784SJeff Roberson goto fail; 35878355f576SJeff Roberson 35882efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 35891431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 35901431a748SGleb Smirnoff zone->uz_name, zone); 35911431a748SGleb Smirnoff 35928355f576SJeff Roberson return (item); 35930095a784SJeff Roberson 3594beb8beefSJeff Roberson fail_cnt: 3595beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 35960095a784SJeff Roberson fail: 35974bd61e19SJeff Roberson if (zone->uz_max_items > 0) 35984bd61e19SJeff Roberson zone_free_limit(zone, 1); 35991431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 36001431a748SGleb Smirnoff zone->uz_name, zone); 36014bd61e19SJeff Roberson 36020095a784SJeff Roberson return (NULL); 36038355f576SJeff Roberson } 36048355f576SJeff Roberson 36058355f576SJeff Roberson /* See uma.h */ 36068355f576SJeff Roberson void 36078355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 36088355f576SJeff Roberson { 36098355f576SJeff Roberson uma_cache_t cache; 3610376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3611cc7ce83aSJeff Roberson int domain, itemdomain, uz_flags; 36128355f576SJeff Roberson 3613e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 361419fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 361510cb2424SMark Murray 36163659f747SRobert Watson CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread, 36173659f747SRobert Watson zone->uz_name); 36183659f747SRobert Watson 3619d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 36201067a2baSJonathan T. Looney ("uma_zfree_arg: called with spinlock or critical section held")); 36211067a2baSJonathan T. Looney 362220ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 362320ed0cb0SMatthew D Fleming if (item == NULL) 362420ed0cb0SMatthew D Fleming return; 36258d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 36268d689e04SGleb Smirnoff if (is_memguard_addr(item)) { 3627bc9d08e1SMark Johnston if (zone->uz_dtor != NULL) 36288d689e04SGleb Smirnoff zone->uz_dtor(item, zone->uz_size, udata); 3629bc9d08e1SMark Johnston if (zone->uz_fini != NULL) 36308d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 36318d689e04SGleb Smirnoff memguard_free(item); 36328d689e04SGleb Smirnoff return; 36338d689e04SGleb Smirnoff } 36348d689e04SGleb Smirnoff #endif 3635cc7ce83aSJeff Roberson 3636cc7ce83aSJeff Roberson /* 3637cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 3638cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 3639cc7ce83aSJeff Roberson * will simply read another cpu's line. 3640cc7ce83aSJeff Roberson */ 3641cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3642cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3643cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 3644cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 3645cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 3646ef72505eSJeff Roberson 3647af7f9b97SJeff Roberson /* 3648af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 3649af7f9b97SJeff Roberson * a little longer for the limits to be reset. 3650af7f9b97SJeff Roberson */ 3651cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 3652bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0) 3653fc03d22bSJeff Roberson goto zfree_item; 3654cc7ce83aSJeff Roberson } 3655af7f9b97SJeff Roberson 36565d1ae027SRobert Watson /* 36575d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 36585d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 36595d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 36605d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 36615d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 36625d1ae027SRobert Watson * preemption and migration. We release the critical section in 36635d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 36645d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 36655d1ae027SRobert Watson * detect and handle migration if it has occurred. 36665d1ae027SRobert Watson */ 36670a81b439SJeff Roberson domain = itemdomain = 0; 36685d1ae027SRobert Watson critical_enter(); 36690a81b439SJeff Roberson do { 3670cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3671376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 3672c1685086SJeff Roberson #ifdef UMA_XDOMAIN 3673cc7ce83aSJeff Roberson if ((uz_flags & UMA_ZONE_NUMA) != 0) { 36740a81b439SJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 36750a81b439SJeff Roberson domain = PCPU_GET(domain); 36760a81b439SJeff Roberson } 3677cc7ce83aSJeff Roberson if ((uz_flags & UMA_ZONE_NUMA) != 0 && domain != itemdomain) { 3678376b1ba3SJeff Roberson bucket = &cache->uc_crossbucket; 36790a81b439SJeff Roberson } else 3680c1685086SJeff Roberson #endif 36810a81b439SJeff Roberson 3682a553d4b8SJeff Roberson /* 3683fc03d22bSJeff Roberson * Try to free into the allocbucket first to give LIFO ordering 3684fc03d22bSJeff Roberson * for cache-hot datastructures. Spill over into the freebucket 3685fc03d22bSJeff Roberson * if necessary. Alloc will swap them if one runs dry. 3686a553d4b8SJeff Roberson */ 3687376b1ba3SJeff Roberson if (__predict_false(bucket->ucb_cnt >= bucket->ucb_entries)) 3688376b1ba3SJeff Roberson bucket = &cache->uc_freebucket; 3689376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3690376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 36915d1ae027SRobert Watson critical_exit(); 36928355f576SJeff Roberson return; 3693fc03d22bSJeff Roberson } 36940a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 36950a81b439SJeff Roberson critical_exit(); 3696fc03d22bSJeff Roberson 36978355f576SJeff Roberson /* 36980a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 36998355f576SJeff Roberson */ 37000a81b439SJeff Roberson zfree_item: 37010a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 37020a81b439SJeff Roberson } 3703fc03d22bSJeff Roberson 37040a81b439SJeff Roberson static void 37050a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 37060a81b439SJeff Roberson int domain, int itemdomain) 37070a81b439SJeff Roberson { 37080a81b439SJeff Roberson uma_zone_domain_t zdom; 37090a81b439SJeff Roberson 37100a81b439SJeff Roberson #ifdef UMA_XDOMAIN 37110a81b439SJeff Roberson /* 37120a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 37130a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 37140a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 37150a81b439SJeff Roberson * correct domains by freeing the contents to the slab layer. 37160a81b439SJeff Roberson */ 37170a81b439SJeff Roberson if (domain != itemdomain && vm_ndomains > 2) { 37180a81b439SJeff Roberson CTR3(KTR_UMA, 37190a81b439SJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 37200a81b439SJeff Roberson zone->uz_name, zone, bucket); 37210a81b439SJeff Roberson bucket_drain(zone, bucket); 37220a81b439SJeff Roberson bucket_free(zone, bucket, udata); 37230a81b439SJeff Roberson return; 37240a81b439SJeff Roberson } 37250a81b439SJeff Roberson #endif 37260a81b439SJeff Roberson /* 37270a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 37280a81b439SJeff Roberson * 37290a81b439SJeff Roberson * We bump the uz count when the cache size is insufficient to 37300a81b439SJeff Roberson * handle the working set. 37310a81b439SJeff Roberson */ 37324d104ba0SAlexander Motin if (ZONE_TRYLOCK(zone) == 0) { 37334d104ba0SAlexander Motin /* Record contention to size the buckets. */ 37348355f576SJeff Roberson ZONE_LOCK(zone); 373520a4e154SJeff Roberson if (zone->uz_bucket_size < zone->uz_bucket_size_max) 373620a4e154SJeff Roberson zone->uz_bucket_size++; 37374d104ba0SAlexander Motin } 37388355f576SJeff Roberson 37390a81b439SJeff Roberson CTR3(KTR_UMA, 37400a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 37410a81b439SJeff Roberson zone->uz_name, zone, bucket); 37420a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 37430a81b439SJeff Roberson KASSERT(bucket->ub_cnt == bucket->ub_entries, 37440a81b439SJeff Roberson ("uma_zfree: Attempting to insert partial bucket onto the full list.\n")); 37450a81b439SJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 3746c1685086SJeff Roberson ZONE_UNLOCK(zone); 3747c1685086SJeff Roberson bucket_drain(zone, bucket); 3748c1685086SJeff Roberson bucket_free(zone, bucket, udata); 3749c1685086SJeff Roberson } else { 3750c1685086SJeff Roberson zdom = &zone->uz_domain[itemdomain]; 3751c1685086SJeff Roberson zone_put_bucket(zone, zdom, bucket, true); 3752c1685086SJeff Roberson ZONE_UNLOCK(zone); 3753c1685086SJeff Roberson } 37548355f576SJeff Roberson } 3755fc03d22bSJeff Roberson 37564d104ba0SAlexander Motin /* 37570a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 37580a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 37590a81b439SJeff Roberson * 37600a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 37610a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 37620a81b439SJeff Roberson * the caller should retry. 37634d104ba0SAlexander Motin */ 37640a81b439SJeff Roberson static __noinline bool 37650a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 37660a81b439SJeff Roberson int itemdomain) 37670a81b439SJeff Roberson { 37680a81b439SJeff Roberson uma_bucket_t bucket; 3769cc7ce83aSJeff Roberson int domain; 37700a81b439SJeff Roberson 37710a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 37720a81b439SJeff Roberson 377320a4e154SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) 37740a81b439SJeff Roberson return false; 37750a81b439SJeff Roberson 3776cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 37770a81b439SJeff Roberson 37780a81b439SJeff Roberson /* 37790a81b439SJeff Roberson * NUMA domains need to free to the correct zdom. When XDOMAIN 37800a81b439SJeff Roberson * is enabled this is the zdom of the item and the bucket may be 37810a81b439SJeff Roberson * the cross bucket if they do not match. 37820a81b439SJeff Roberson */ 37830a81b439SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) 37840a81b439SJeff Roberson #ifdef UMA_XDOMAIN 37850a81b439SJeff Roberson domain = PCPU_GET(domain); 37860a81b439SJeff Roberson #else 37870a81b439SJeff Roberson itemdomain = domain = PCPU_GET(domain); 37880a81b439SJeff Roberson #endif 37890a81b439SJeff Roberson else 37900a81b439SJeff Roberson itemdomain = domain = 0; 37910a81b439SJeff Roberson #ifdef UMA_XDOMAIN 37920a81b439SJeff Roberson if (domain != itemdomain) { 3793376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 37940a81b439SJeff Roberson if (bucket != NULL) 37950a81b439SJeff Roberson atomic_add_64(&zone->uz_xdomain, bucket->ub_cnt); 37960a81b439SJeff Roberson } else 37970a81b439SJeff Roberson #endif 3798376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 37990a81b439SJeff Roberson 38000a81b439SJeff Roberson 38010a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 38020a81b439SJeff Roberson critical_exit(); 38030a81b439SJeff Roberson 38040a81b439SJeff Roberson if (bucket != NULL) 38050a81b439SJeff Roberson zone_free_bucket(zone, bucket, udata, domain, itemdomain); 3806a553d4b8SJeff Roberson 38076fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT); 38081431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p", 38091431a748SGleb Smirnoff zone->uz_name, zone, bucket); 3810fc03d22bSJeff Roberson critical_enter(); 38110a81b439SJeff Roberson if (bucket == NULL) 38120a81b439SJeff Roberson return (false); 3813cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 38140a81b439SJeff Roberson #ifdef UMA_XDOMAIN 3815fc03d22bSJeff Roberson /* 38160a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 38170a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 38180a81b439SJeff Roberson * the free bucket. 3819fc03d22bSJeff Roberson */ 38200a81b439SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) { 38210a81b439SJeff Roberson domain = PCPU_GET(domain); 3822376b1ba3SJeff Roberson if (domain != itemdomain && 3823376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 3824376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 38250a81b439SJeff Roberson return (true); 38260a81b439SJeff Roberson } 38270a81b439SJeff Roberson } 38280a81b439SJeff Roberson #endif 38290a81b439SJeff Roberson /* 38300a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 38310a81b439SJeff Roberson */ 3832376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 3833fc03d22bSJeff Roberson critical_exit(); 38346fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 38350a81b439SJeff Roberson critical_enter(); 38360a81b439SJeff Roberson } else 3837376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 38388355f576SJeff Roberson 38390a81b439SJeff Roberson return (true); 38408355f576SJeff Roberson } 38418355f576SJeff Roberson 3842ab3185d1SJeff Roberson void 3843ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata) 3844ab3185d1SJeff Roberson { 3845ab3185d1SJeff Roberson 3846ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 384719fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3848ab3185d1SJeff Roberson 3849ab3185d1SJeff Roberson CTR2(KTR_UMA, "uma_zfree_domain thread %x zone %s", curthread, 3850ab3185d1SJeff Roberson zone->uz_name); 3851ab3185d1SJeff Roberson 3852ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3853ab3185d1SJeff Roberson ("uma_zfree_domain: called with spinlock or critical section held")); 3854ab3185d1SJeff Roberson 3855ab3185d1SJeff Roberson /* uma_zfree(..., NULL) does nothing, to match free(9). */ 3856ab3185d1SJeff Roberson if (item == NULL) 3857ab3185d1SJeff Roberson return; 3858ab3185d1SJeff Roberson zone_free_item(zone, item, udata, SKIP_NONE); 3859ab3185d1SJeff Roberson } 3860ab3185d1SJeff Roberson 38618355f576SJeff Roberson static void 3862bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 38638355f576SJeff Roberson { 3864bb15d1c7SGleb Smirnoff uma_keg_t keg; 3865ab3185d1SJeff Roberson uma_domain_t dom; 386685dcf349SGleb Smirnoff uint8_t freei; 3867099a0e58SBosko Milekic 3868bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 3869bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 38708355f576SJeff Roberson 3871ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3872ab3185d1SJeff Roberson 38738355f576SJeff Roberson /* Do we need to remove from any lists? */ 3874099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 38758355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3876ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 38778355f576SJeff Roberson } else if (slab->us_freecount == 0) { 38788355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3879ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 38808355f576SJeff Roberson } 38818355f576SJeff Roberson 3882ef72505eSJeff Roberson /* Slab management. */ 38831e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 38849b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 38858355f576SJeff Roberson slab->us_freecount++; 38868355f576SJeff Roberson 3887ef72505eSJeff Roberson /* Keg statistics. */ 3888099a0e58SBosko Milekic keg->uk_free++; 38890095a784SJeff Roberson } 38900095a784SJeff Roberson 38910095a784SJeff Roberson static void 3892b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 38930095a784SJeff Roberson { 3894b75c4efcSAndrew Turner uma_zone_t zone; 38950095a784SJeff Roberson void *item; 38960095a784SJeff Roberson uma_slab_t slab; 38970095a784SJeff Roberson uma_keg_t keg; 38980095a784SJeff Roberson uint8_t *mem; 38990095a784SJeff Roberson int i; 39008355f576SJeff Roberson 3901b75c4efcSAndrew Turner zone = arg; 3902bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 3903af526374SJeff Roberson KEG_LOCK(keg); 39040095a784SJeff Roberson for (i = 0; i < cnt; i++) { 39050095a784SJeff Roberson item = bucket[i]; 39060095a784SJeff Roberson if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) { 39070095a784SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 39080095a784SJeff Roberson if (zone->uz_flags & UMA_ZONE_HASH) { 39090095a784SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 39100095a784SJeff Roberson } else { 39110095a784SJeff Roberson mem += keg->uk_pgoff; 39120095a784SJeff Roberson slab = (uma_slab_t)mem; 39130095a784SJeff Roberson } 3914584061b4SJeff Roberson } else 39150095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 3916bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 39170095a784SJeff Roberson } 3918af526374SJeff Roberson KEG_UNLOCK(keg); 39198355f576SJeff Roberson } 39208355f576SJeff Roberson 39210095a784SJeff Roberson /* 39220095a784SJeff Roberson * Frees a single item to any zone. 39230095a784SJeff Roberson * 39240095a784SJeff Roberson * Arguments: 39250095a784SJeff Roberson * zone The zone to free to 39260095a784SJeff Roberson * item The item we're freeing 39270095a784SJeff Roberson * udata User supplied data for the dtor 39280095a784SJeff Roberson * skip Skip dtors and finis 39290095a784SJeff Roberson */ 39300095a784SJeff Roberson static void 39310095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 39320095a784SJeff Roberson { 3933c5deaf04SGleb Smirnoff 3934cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 39350095a784SJeff Roberson 39360095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 39370095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 39380095a784SJeff Roberson 39390095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 3940bb15d1c7SGleb Smirnoff 3941bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 3942bb15d1c7SGleb Smirnoff return; 3943bb15d1c7SGleb Smirnoff 39442efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 39452efcc8cbSGleb Smirnoff 39464bd61e19SJeff Roberson if (zone->uz_max_items > 0) 39474bd61e19SJeff Roberson zone_free_limit(zone, 1); 3948bb45b411SGleb Smirnoff } 39490095a784SJeff Roberson 39508355f576SJeff Roberson /* See uma.h */ 39511c6cae97SLawrence Stewart int 3952736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 3953736ee590SJeff Roberson { 3954bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 3955003cf08bSMark Johnston int count; 3956bb15d1c7SGleb Smirnoff 39574bd61e19SJeff Roberson /* 39584bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 39594bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 39604bd61e19SJeff Roberson * way to clear a limit. 39614bd61e19SJeff Roberson */ 3962bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 3963003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 3964003cf08bSMark Johnston count = ubz != NULL ? ubz->ubz_entries : 0; 396520a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = count; 396620a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 396720a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 3968bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 3969cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 3970cc7ce83aSJeff Roberson zone_update_caches(zone); 39714bd61e19SJeff Roberson /* We may need to wake waiters. */ 39724bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 3973bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 3974bb15d1c7SGleb Smirnoff 3975bb15d1c7SGleb Smirnoff return (nitems); 3976bb15d1c7SGleb Smirnoff } 3977bb15d1c7SGleb Smirnoff 3978bb15d1c7SGleb Smirnoff /* See uma.h */ 3979003cf08bSMark Johnston void 3980bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 3981bb15d1c7SGleb Smirnoff { 3982003cf08bSMark Johnston struct uma_bucket_zone *ubz; 3983003cf08bSMark Johnston int bpcpu; 3984bb15d1c7SGleb Smirnoff 3985bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 3986003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 3987003cf08bSMark Johnston if (ubz != NULL) { 3988003cf08bSMark Johnston bpcpu = 2; 3989003cf08bSMark Johnston #ifdef UMA_XDOMAIN 3990003cf08bSMark Johnston if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) 3991003cf08bSMark Johnston /* Count the cross-domain bucket. */ 3992003cf08bSMark Johnston bpcpu++; 3993003cf08bSMark Johnston #endif 3994003cf08bSMark Johnston nitems -= ubz->ubz_entries * bpcpu * mp_ncpus; 399520a4e154SJeff Roberson zone->uz_bucket_size_max = ubz->ubz_entries; 3996003cf08bSMark Johnston } else { 399720a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 3998003cf08bSMark Johnston } 399920a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 400020a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4001bb15d1c7SGleb Smirnoff zone->uz_bkt_max = nitems; 4002bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4003736ee590SJeff Roberson } 4004736ee590SJeff Roberson 4005736ee590SJeff Roberson /* See uma.h */ 4006e49471b0SAndre Oppermann int 4007e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4008e49471b0SAndre Oppermann { 4009e49471b0SAndre Oppermann int nitems; 4010e49471b0SAndre Oppermann 4011*727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4012e49471b0SAndre Oppermann 4013e49471b0SAndre Oppermann return (nitems); 4014e49471b0SAndre Oppermann } 4015e49471b0SAndre Oppermann 4016e49471b0SAndre Oppermann /* See uma.h */ 40172f891cd5SPawel Jakub Dawidek void 40182f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 40192f891cd5SPawel Jakub Dawidek { 40202f891cd5SPawel Jakub Dawidek 4021*727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 40222f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 40232f891cd5SPawel Jakub Dawidek } 40242f891cd5SPawel Jakub Dawidek 40252f891cd5SPawel Jakub Dawidek /* See uma.h */ 402654503a13SJonathan T. Looney void 402754503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 402854503a13SJonathan T. Looney { 402954503a13SJonathan T. Looney 4030*727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4031e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 403254503a13SJonathan T. Looney } 403354503a13SJonathan T. Looney 403454503a13SJonathan T. Looney /* See uma.h */ 4035c4ae7908SLawrence Stewart int 4036c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4037c4ae7908SLawrence Stewart { 4038c4ae7908SLawrence Stewart int64_t nitems; 4039c4ae7908SLawrence Stewart u_int i; 4040c4ae7908SLawrence Stewart 40412efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 40422efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4043*727c6918SJeff Roberson CPU_FOREACH(i) 4044*727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4045*727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4046c4ae7908SLawrence Stewart 4047c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4048c4ae7908SLawrence Stewart } 4049c4ae7908SLawrence Stewart 405020a4e154SJeff Roberson static uint64_t 405120a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 405220a4e154SJeff Roberson { 405320a4e154SJeff Roberson uint64_t nitems; 405420a4e154SJeff Roberson u_int i; 405520a4e154SJeff Roberson 405620a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4057*727c6918SJeff Roberson CPU_FOREACH(i) 4058*727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 405920a4e154SJeff Roberson 406020a4e154SJeff Roberson return (nitems); 406120a4e154SJeff Roberson } 406220a4e154SJeff Roberson 406320a4e154SJeff Roberson static uint64_t 406420a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 406520a4e154SJeff Roberson { 406620a4e154SJeff Roberson uint64_t nitems; 406720a4e154SJeff Roberson u_int i; 406820a4e154SJeff Roberson 406920a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4070*727c6918SJeff Roberson CPU_FOREACH(i) 4071*727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 407220a4e154SJeff Roberson 407320a4e154SJeff Roberson return (nitems); 407420a4e154SJeff Roberson } 407520a4e154SJeff Roberson 4076c4ae7908SLawrence Stewart /* See uma.h */ 4077736ee590SJeff Roberson void 4078099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4079099a0e58SBosko Milekic { 4080e20a199fSJeff Roberson uma_keg_t keg; 4081e20a199fSJeff Roberson 4082bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4083*727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4084e20a199fSJeff Roberson keg->uk_init = uminit; 4085099a0e58SBosko Milekic } 4086099a0e58SBosko Milekic 4087099a0e58SBosko Milekic /* See uma.h */ 4088099a0e58SBosko Milekic void 4089099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4090099a0e58SBosko Milekic { 4091e20a199fSJeff Roberson uma_keg_t keg; 4092e20a199fSJeff Roberson 4093bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4094*727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4095e20a199fSJeff Roberson keg->uk_fini = fini; 4096099a0e58SBosko Milekic } 4097099a0e58SBosko Milekic 4098099a0e58SBosko Milekic /* See uma.h */ 4099099a0e58SBosko Milekic void 4100099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4101099a0e58SBosko Milekic { 4102af526374SJeff Roberson 4103*727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4104099a0e58SBosko Milekic zone->uz_init = zinit; 4105099a0e58SBosko Milekic } 4106099a0e58SBosko Milekic 4107099a0e58SBosko Milekic /* See uma.h */ 4108099a0e58SBosko Milekic void 4109099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4110099a0e58SBosko Milekic { 4111af526374SJeff Roberson 4112*727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4113099a0e58SBosko Milekic zone->uz_fini = zfini; 4114099a0e58SBosko Milekic } 4115099a0e58SBosko Milekic 4116099a0e58SBosko Milekic /* See uma.h */ 4117099a0e58SBosko Milekic void 41188355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 41198355f576SJeff Roberson { 41200095a784SJeff Roberson uma_keg_t keg; 4121e20a199fSJeff Roberson 4122bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4123*727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 41240095a784SJeff Roberson keg->uk_freef = freef; 41258355f576SJeff Roberson } 41268355f576SJeff Roberson 41278355f576SJeff Roberson /* See uma.h */ 41288355f576SJeff Roberson void 41298355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 41308355f576SJeff Roberson { 4131e20a199fSJeff Roberson uma_keg_t keg; 4132e20a199fSJeff Roberson 4133bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4134*727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4135e20a199fSJeff Roberson keg->uk_allocf = allocf; 41368355f576SJeff Roberson } 41378355f576SJeff Roberson 41388355f576SJeff Roberson /* See uma.h */ 41396fd34d6fSJeff Roberson void 41406fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 41416fd34d6fSJeff Roberson { 41426fd34d6fSJeff Roberson uma_keg_t keg; 41436fd34d6fSJeff Roberson 4144bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4145*727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 41466fd34d6fSJeff Roberson keg->uk_reserve = items; 41476fd34d6fSJeff Roberson } 41486fd34d6fSJeff Roberson 41496fd34d6fSJeff Roberson /* See uma.h */ 41508355f576SJeff Roberson int 4151a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 41528355f576SJeff Roberson { 4153099a0e58SBosko Milekic uma_keg_t keg; 41548355f576SJeff Roberson vm_offset_t kva; 41559ba30bcbSZbigniew Bodek u_int pages; 41568355f576SJeff Roberson 4157bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4158*727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4159*727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 41608355f576SJeff Roberson 4161bb15d1c7SGleb Smirnoff pages = count / keg->uk_ipers; 4162099a0e58SBosko Milekic if (pages * keg->uk_ipers < count) 41638355f576SJeff Roberson pages++; 416457223e99SAndriy Gapon pages *= keg->uk_ppera; 4165a553d4b8SJeff Roberson 4166a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4167a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4168a4915c21SAttilio Rao #else 4169a4915c21SAttilio Rao if (1) { 4170a4915c21SAttilio Rao #endif 417157223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4172d1f42ac2SAlan Cox if (kva == 0) 41738355f576SJeff Roberson return (0); 4174a4915c21SAttilio Rao } else 4175a4915c21SAttilio Rao kva = 0; 4176bb15d1c7SGleb Smirnoff 4177bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4178bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4179099a0e58SBosko Milekic keg->uk_kva = kva; 4180a4915c21SAttilio Rao keg->uk_offset = 0; 4181bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4182a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4183a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4184a4915c21SAttilio Rao #else 4185a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4186a4915c21SAttilio Rao #endif 4187cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4188cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4189cc7ce83aSJeff Roberson zone_update_caches(zone); 4190bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4191af526374SJeff Roberson 41928355f576SJeff Roberson return (1); 41938355f576SJeff Roberson } 41948355f576SJeff Roberson 41958355f576SJeff Roberson /* See uma.h */ 41968355f576SJeff Roberson void 41978355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 41988355f576SJeff Roberson { 4199920239efSMark Johnston struct vm_domainset_iter di; 4200ab3185d1SJeff Roberson uma_domain_t dom; 42018355f576SJeff Roberson uma_slab_t slab; 4202099a0e58SBosko Milekic uma_keg_t keg; 420386220393SMark Johnston int aflags, domain, slabs; 42048355f576SJeff Roberson 4205bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4206af526374SJeff Roberson KEG_LOCK(keg); 4207099a0e58SBosko Milekic slabs = items / keg->uk_ipers; 4208099a0e58SBosko Milekic if (slabs * keg->uk_ipers < items) 42098355f576SJeff Roberson slabs++; 4210194a979eSMark Johnston while (slabs-- > 0) { 421186220393SMark Johnston aflags = M_NOWAIT; 421286220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 421386220393SMark Johnston &aflags); 421486220393SMark Johnston for (;;) { 421586220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 421686220393SMark Johnston aflags); 421786220393SMark Johnston if (slab != NULL) { 4218ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 421986220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 422086220393SMark Johnston us_link); 4221920239efSMark Johnston break; 42228355f576SJeff Roberson } 422386220393SMark Johnston if (vm_domainset_iter_policy(&di, &domain) != 0) { 422486220393SMark Johnston KEG_UNLOCK(keg); 422586220393SMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 422686220393SMark Johnston KEG_LOCK(keg); 422786220393SMark Johnston } 422886220393SMark Johnston } 422986220393SMark Johnston } 4230af526374SJeff Roberson KEG_UNLOCK(keg); 42318355f576SJeff Roberson } 42328355f576SJeff Roberson 42338355f576SJeff Roberson /* See uma.h */ 423408cfa56eSMark Johnston void 423508cfa56eSMark Johnston uma_reclaim(int req) 42368355f576SJeff Roberson { 423744ec2b63SKonstantin Belousov 42381431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 423908cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 424086bbae32SJeff Roberson bucket_enable(); 424108cfa56eSMark Johnston 424208cfa56eSMark Johnston switch (req) { 424308cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 424420a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 424508cfa56eSMark Johnston break; 424608cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 424708cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 424820a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 424908cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 425008cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 425120a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4252a2de44abSAlexander Motin } 425308cfa56eSMark Johnston break; 425408cfa56eSMark Johnston default: 425508cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 425608cfa56eSMark Johnston } 42570f9b7bf3SMark Johnston 42588355f576SJeff Roberson /* 42598355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 42608355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 42618355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 42628355f576SJeff Roberson */ 426320a4e154SJeff Roberson zone_drain(slabzone, NULL); 4264cae33c14SJeff Roberson bucket_zone_drain(); 426508cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 42668355f576SJeff Roberson } 42678355f576SJeff Roberson 42682e47807cSJeff Roberson static volatile int uma_reclaim_needed; 426944ec2b63SKonstantin Belousov 427044ec2b63SKonstantin Belousov void 427144ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 427244ec2b63SKonstantin Belousov { 427344ec2b63SKonstantin Belousov 42742e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 42752e47807cSJeff Roberson wakeup(uma_reclaim); 427644ec2b63SKonstantin Belousov } 427744ec2b63SKonstantin Belousov 427844ec2b63SKonstantin Belousov void 427944ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 428044ec2b63SKonstantin Belousov { 428144ec2b63SKonstantin Belousov 428244ec2b63SKonstantin Belousov for (;;) { 428308cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4284200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 428508cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 42862e47807cSJeff Roberson hz); 428708cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 42889b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 428908cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4290200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 42912e47807cSJeff Roberson /* Don't fire more than once per-second. */ 42922e47807cSJeff Roberson pause("umarclslp", hz); 429344ec2b63SKonstantin Belousov } 429444ec2b63SKonstantin Belousov } 429544ec2b63SKonstantin Belousov 4296663b416fSJohn Baldwin /* See uma.h */ 429708cfa56eSMark Johnston void 429808cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 429908cfa56eSMark Johnston { 430008cfa56eSMark Johnston 430108cfa56eSMark Johnston switch (req) { 430208cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 430320a4e154SJeff Roberson zone_trim(zone, NULL); 430408cfa56eSMark Johnston break; 430508cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 430620a4e154SJeff Roberson zone_drain(zone, NULL); 430708cfa56eSMark Johnston break; 430808cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 430908cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 431020a4e154SJeff Roberson zone_drain(zone, NULL); 431108cfa56eSMark Johnston break; 431208cfa56eSMark Johnston default: 431308cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 431408cfa56eSMark Johnston } 431508cfa56eSMark Johnston } 431608cfa56eSMark Johnston 431708cfa56eSMark Johnston /* See uma.h */ 4318663b416fSJohn Baldwin int 4319663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 4320663b416fSJohn Baldwin { 4321663b416fSJohn Baldwin 4322*727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 43236c125b8dSMohan Srinivasan } 43246c125b8dSMohan Srinivasan 43252e47807cSJeff Roberson unsigned long 43262e47807cSJeff Roberson uma_limit(void) 43272e47807cSJeff Roberson { 43282e47807cSJeff Roberson 43292e47807cSJeff Roberson return (uma_kmem_limit); 43302e47807cSJeff Roberson } 43312e47807cSJeff Roberson 43322e47807cSJeff Roberson void 43332e47807cSJeff Roberson uma_set_limit(unsigned long limit) 43342e47807cSJeff Roberson { 43352e47807cSJeff Roberson 43362e47807cSJeff Roberson uma_kmem_limit = limit; 43372e47807cSJeff Roberson } 43382e47807cSJeff Roberson 43392e47807cSJeff Roberson unsigned long 43402e47807cSJeff Roberson uma_size(void) 43412e47807cSJeff Roberson { 43422e47807cSJeff Roberson 4343058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 4344ad5b0f5bSJeff Roberson } 4345ad5b0f5bSJeff Roberson 4346ad5b0f5bSJeff Roberson long 4347ad5b0f5bSJeff Roberson uma_avail(void) 4348ad5b0f5bSJeff Roberson { 4349ad5b0f5bSJeff Roberson 4350058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 43512e47807cSJeff Roberson } 43522e47807cSJeff Roberson 4353a0d4b0aeSRobert Watson #ifdef DDB 43548355f576SJeff Roberson /* 43557a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 43567a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 43577a52a97eSRobert Watson * 43587a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 43597a52a97eSRobert Watson * per-CPU cache statistic. 43607a52a97eSRobert Watson * 43617a52a97eSRobert Watson */ 43627a52a97eSRobert Watson static void 43630f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 4364c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 43657a52a97eSRobert Watson { 43667a52a97eSRobert Watson uma_cache_t cache; 4367c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 43687a52a97eSRobert Watson int cachefree, cpu; 43697a52a97eSRobert Watson 4370c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 43717a52a97eSRobert Watson cachefree = 0; 43723aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 43737a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 4374376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 4375376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 4376376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 4377376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 43787a52a97eSRobert Watson allocs += cache->uc_allocs; 43797a52a97eSRobert Watson frees += cache->uc_frees; 43807a52a97eSRobert Watson } 43812efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 43822efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 4383bf965959SSean Bruno sleeps += z->uz_sleeps; 4384c1685086SJeff Roberson xdomain += z->uz_xdomain; 43857a52a97eSRobert Watson if (cachefreep != NULL) 43867a52a97eSRobert Watson *cachefreep = cachefree; 43877a52a97eSRobert Watson if (allocsp != NULL) 43887a52a97eSRobert Watson *allocsp = allocs; 43897a52a97eSRobert Watson if (freesp != NULL) 43907a52a97eSRobert Watson *freesp = frees; 4391bf965959SSean Bruno if (sleepsp != NULL) 4392bf965959SSean Bruno *sleepsp = sleeps; 4393c1685086SJeff Roberson if (xdomainp != NULL) 4394c1685086SJeff Roberson *xdomainp = xdomain; 43957a52a97eSRobert Watson } 4396a0d4b0aeSRobert Watson #endif /* DDB */ 43977a52a97eSRobert Watson 43987a52a97eSRobert Watson static int 43997a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 44007a52a97eSRobert Watson { 44017a52a97eSRobert Watson uma_keg_t kz; 44027a52a97eSRobert Watson uma_zone_t z; 44037a52a97eSRobert Watson int count; 44047a52a97eSRobert Watson 44057a52a97eSRobert Watson count = 0; 4406111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 44077a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 44087a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 44097a52a97eSRobert Watson count++; 44107a52a97eSRobert Watson } 4411b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4412b47acb0aSGleb Smirnoff count++; 4413b47acb0aSGleb Smirnoff 4414111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 44157a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 44167a52a97eSRobert Watson } 44177a52a97eSRobert Watson 4418b47acb0aSGleb Smirnoff static void 4419b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 4420b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 4421b47acb0aSGleb Smirnoff { 4422b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 4423b47acb0aSGleb Smirnoff uma_cache_t cache; 4424b47acb0aSGleb Smirnoff int i; 4425b47acb0aSGleb Smirnoff 4426b47acb0aSGleb Smirnoff 4427b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 4428b47acb0aSGleb Smirnoff zdom = &z->uz_domain[i]; 4429b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 4430b47acb0aSGleb Smirnoff } 4431b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 4432b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 4433b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 4434b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 4435c1685086SJeff Roberson uth->uth_xdomain = z->uz_xdomain; 44361de9724eSMark Johnston 4437b47acb0aSGleb Smirnoff /* 44381de9724eSMark Johnston * While it is not normally safe to access the cache bucket pointers 44391de9724eSMark Johnston * while not on the CPU that owns the cache, we only allow the pointers 44401de9724eSMark Johnston * to be exchanged without the zone lock held, not invalidated, so 44411de9724eSMark Johnston * accept the possible race associated with bucket exchange during 44421de9724eSMark Johnston * monitoring. Use atomic_load_ptr() to ensure that the bucket pointers 44431de9724eSMark Johnston * are loaded only once. 4444b47acb0aSGleb Smirnoff */ 4445b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 4446b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 4447b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 4448b47acb0aSGleb Smirnoff continue; 4449b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 4450376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 4451376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 4452376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 4453b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 4454b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 4455b47acb0aSGleb Smirnoff } 4456b47acb0aSGleb Smirnoff } 4457b47acb0aSGleb Smirnoff 44587a52a97eSRobert Watson static int 44597a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 44607a52a97eSRobert Watson { 44617a52a97eSRobert Watson struct uma_stream_header ush; 44627a52a97eSRobert Watson struct uma_type_header uth; 446363b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 44647a52a97eSRobert Watson struct sbuf sbuf; 44657a52a97eSRobert Watson uma_keg_t kz; 44667a52a97eSRobert Watson uma_zone_t z; 44674bd61e19SJeff Roberson uint64_t items; 44684e657159SMatthew D Fleming int count, error, i; 44697a52a97eSRobert Watson 447000f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 447100f0e671SMatthew D Fleming if (error != 0) 447200f0e671SMatthew D Fleming return (error); 44734e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 44741eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 447563b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 44764e657159SMatthew D Fleming 4477404a593eSMatthew D Fleming count = 0; 4478111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 44797a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 44807a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 44817a52a97eSRobert Watson count++; 44827a52a97eSRobert Watson } 44837a52a97eSRobert Watson 4484b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4485b47acb0aSGleb Smirnoff count++; 4486b47acb0aSGleb Smirnoff 44877a52a97eSRobert Watson /* 44887a52a97eSRobert Watson * Insert stream header. 44897a52a97eSRobert Watson */ 44907a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 44917a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 4492ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 44937a52a97eSRobert Watson ush.ush_count = count; 44944e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 44957a52a97eSRobert Watson 44967a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 44977a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 44987a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 44997a52a97eSRobert Watson ZONE_LOCK(z); 4500cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 45017a52a97eSRobert Watson uth.uth_align = kz->uk_align; 45027a52a97eSRobert Watson uth.uth_size = kz->uk_size; 45037a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 45044bd61e19SJeff Roberson if (z->uz_max_items > 0) { 45054bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 45064bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 4507bb15d1c7SGleb Smirnoff kz->uk_ppera; 45084bd61e19SJeff Roberson } else 4509bb45b411SGleb Smirnoff uth.uth_pages = kz->uk_pages; 4510f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 4511bb15d1c7SGleb Smirnoff kz->uk_ppera; 4512bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 4513f8c86a5fSGleb Smirnoff uth.uth_keg_free = z->uz_keg->uk_free; 4514cbbb4a00SRobert Watson 4515cbbb4a00SRobert Watson /* 4516cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 4517cbbb4a00SRobert Watson * on the keg's zone list. 4518cbbb4a00SRobert Watson */ 4519e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 4520cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 4521cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 4522b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 4523b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 45242450bbb8SRobert Watson ZONE_UNLOCK(z); 452563b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 452663b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 452763b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 45287a52a97eSRobert Watson } 45297a52a97eSRobert Watson } 4530b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 4531b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 4532b47acb0aSGleb Smirnoff ZONE_LOCK(z); 4533b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 4534b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 4535b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 4536b47acb0aSGleb Smirnoff ZONE_UNLOCK(z); 4537b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 4538b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 4539b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 4540b47acb0aSGleb Smirnoff } 4541b47acb0aSGleb Smirnoff 4542111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 45434e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 45444e657159SMatthew D Fleming sbuf_delete(&sbuf); 454563b5d112SKonstantin Belousov free(ups, M_TEMP); 45467a52a97eSRobert Watson return (error); 45477a52a97eSRobert Watson } 454848c5777eSRobert Watson 45490a5a3ccbSGleb Smirnoff int 45500a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 45510a5a3ccbSGleb Smirnoff { 45520a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 455316be9f54SGleb Smirnoff int error, max; 45540a5a3ccbSGleb Smirnoff 455516be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 45560a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 45570a5a3ccbSGleb Smirnoff if (error || !req->newptr) 45580a5a3ccbSGleb Smirnoff return (error); 45590a5a3ccbSGleb Smirnoff 45600a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 45610a5a3ccbSGleb Smirnoff 45620a5a3ccbSGleb Smirnoff return (0); 45630a5a3ccbSGleb Smirnoff } 45640a5a3ccbSGleb Smirnoff 45650a5a3ccbSGleb Smirnoff int 45660a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 45670a5a3ccbSGleb Smirnoff { 456820a4e154SJeff Roberson uma_zone_t zone; 45690a5a3ccbSGleb Smirnoff int cur; 45700a5a3ccbSGleb Smirnoff 457120a4e154SJeff Roberson /* 457220a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 457320a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 457420a4e154SJeff Roberson */ 457520a4e154SJeff Roberson if (arg2 == 0) 457620a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 457720a4e154SJeff Roberson else 457820a4e154SJeff Roberson zone = arg1; 45790a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 45800a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 45810a5a3ccbSGleb Smirnoff } 45820a5a3ccbSGleb Smirnoff 458320a4e154SJeff Roberson static int 458420a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 458520a4e154SJeff Roberson { 458620a4e154SJeff Roberson uma_zone_t zone = arg1; 458720a4e154SJeff Roberson uint64_t cur; 458820a4e154SJeff Roberson 458920a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 459020a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 459120a4e154SJeff Roberson } 459220a4e154SJeff Roberson 459320a4e154SJeff Roberson static int 459420a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 459520a4e154SJeff Roberson { 459620a4e154SJeff Roberson uma_zone_t zone = arg1; 459720a4e154SJeff Roberson uint64_t cur; 459820a4e154SJeff Roberson 459920a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 460020a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 460120a4e154SJeff Roberson } 460220a4e154SJeff Roberson 46036d204a6aSRyan Libby static int 46046d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 46056d204a6aSRyan Libby { 46066d204a6aSRyan Libby struct sbuf sbuf; 46076d204a6aSRyan Libby uma_zone_t zone = arg1; 46086d204a6aSRyan Libby int error; 46096d204a6aSRyan Libby 46106d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 46116d204a6aSRyan Libby if (zone->uz_flags != 0) 46126d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 46136d204a6aSRyan Libby else 46146d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 46156d204a6aSRyan Libby error = sbuf_finish(&sbuf); 46166d204a6aSRyan Libby sbuf_delete(&sbuf); 46176d204a6aSRyan Libby 46186d204a6aSRyan Libby return (error); 46196d204a6aSRyan Libby } 46206d204a6aSRyan Libby 4621f7af5015SRyan Libby static int 4622f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 4623f7af5015SRyan Libby { 4624f7af5015SRyan Libby uma_keg_t keg = arg1; 4625f7af5015SRyan Libby int avail, effpct, total; 4626f7af5015SRyan Libby 4627f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 4628f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_OFFPAGE) != 0) 4629f7af5015SRyan Libby total += slab_sizeof(SLAB_MAX_SETSIZE); 4630f7af5015SRyan Libby /* 4631f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 4632f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 4633f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 4634f7af5015SRyan Libby */ 4635f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 4636f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 4637f7af5015SRyan Libby avail *= mp_maxid + 1; 4638f7af5015SRyan Libby effpct = 100 * avail / total; 4639f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 4640f7af5015SRyan Libby } 4641f7af5015SRyan Libby 46424bd61e19SJeff Roberson static int 46434bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 46444bd61e19SJeff Roberson { 46454bd61e19SJeff Roberson uma_zone_t zone = arg1; 46464bd61e19SJeff Roberson uint64_t cur; 46474bd61e19SJeff Roberson 46484bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 46494bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 46504bd61e19SJeff Roberson } 46514bd61e19SJeff Roberson 46529542ea7bSGleb Smirnoff #ifdef INVARIANTS 46539542ea7bSGleb Smirnoff static uma_slab_t 46549542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 46559542ea7bSGleb Smirnoff { 46569542ea7bSGleb Smirnoff uma_slab_t slab; 46579542ea7bSGleb Smirnoff uma_keg_t keg; 46589542ea7bSGleb Smirnoff uint8_t *mem; 46599542ea7bSGleb Smirnoff 46609542ea7bSGleb Smirnoff /* 46619542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 46629542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 46639542ea7bSGleb Smirnoff * essentially holds a reference. 46649542ea7bSGleb Smirnoff */ 4665*727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 4666*727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 4667bb15d1c7SGleb Smirnoff return (NULL); 4668*727c6918SJeff Roberson if (zone->uz_flags & UMA_ZONE_VTOSLAB) 4669*727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 4670bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 4671*727c6918SJeff Roberson if ((keg->uk_flags & UMA_ZONE_HASH) == 0) 4672*727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 4673*727c6918SJeff Roberson KEG_LOCK(keg); 46749542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 4675*727c6918SJeff Roberson KEG_UNLOCK(keg); 46769542ea7bSGleb Smirnoff 46779542ea7bSGleb Smirnoff return (slab); 46789542ea7bSGleb Smirnoff } 46799542ea7bSGleb Smirnoff 4680c5deaf04SGleb Smirnoff static bool 4681c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 4682c5deaf04SGleb Smirnoff { 4683c5deaf04SGleb Smirnoff 4684*727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 4685c5deaf04SGleb Smirnoff return (true); 4686c5deaf04SGleb Smirnoff 4687bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 4688c5deaf04SGleb Smirnoff } 4689c5deaf04SGleb Smirnoff 4690c5deaf04SGleb Smirnoff static bool 4691c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 4692c5deaf04SGleb Smirnoff { 4693c5deaf04SGleb Smirnoff uintptr_t idx; 4694c5deaf04SGleb Smirnoff 4695c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 4696c5deaf04SGleb Smirnoff return (true); 4697c5deaf04SGleb Smirnoff 4698c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 4699c5deaf04SGleb Smirnoff return (false); 4700c5deaf04SGleb Smirnoff 4701c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 4702c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 4703c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 4704c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 4705c5deaf04SGleb Smirnoff } 4706c5deaf04SGleb Smirnoff 4707c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 4708c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 4709c5deaf04SGleb Smirnoff return (true); 4710c5deaf04SGleb Smirnoff } 4711c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 4712c5deaf04SGleb Smirnoff 4713c5deaf04SGleb Smirnoff return (false); 4714c5deaf04SGleb Smirnoff } 4715c5deaf04SGleb Smirnoff 47169542ea7bSGleb Smirnoff /* 47179542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 47189542ea7bSGleb Smirnoff * 47199542ea7bSGleb Smirnoff */ 47209542ea7bSGleb Smirnoff static void 47219542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 47229542ea7bSGleb Smirnoff { 47239542ea7bSGleb Smirnoff uma_keg_t keg; 47249542ea7bSGleb Smirnoff int freei; 47259542ea7bSGleb Smirnoff 47269542ea7bSGleb Smirnoff if (slab == NULL) { 47279542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 47289542ea7bSGleb Smirnoff if (slab == NULL) 47299542ea7bSGleb Smirnoff panic("uma: item %p did not belong to zone %s\n", 47309542ea7bSGleb Smirnoff item, zone->uz_name); 47319542ea7bSGleb Smirnoff } 4732584061b4SJeff Roberson keg = zone->uz_keg; 47331e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 47349542ea7bSGleb Smirnoff 4735815db204SRyan Libby if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 47369542ea7bSGleb Smirnoff panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n", 47379542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 4738815db204SRyan Libby BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 47399542ea7bSGleb Smirnoff } 47409542ea7bSGleb Smirnoff 47419542ea7bSGleb Smirnoff /* 47429542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 47439542ea7bSGleb Smirnoff * and duplicate frees. 47449542ea7bSGleb Smirnoff * 47459542ea7bSGleb Smirnoff */ 47469542ea7bSGleb Smirnoff static void 47479542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 47489542ea7bSGleb Smirnoff { 47499542ea7bSGleb Smirnoff uma_keg_t keg; 47509542ea7bSGleb Smirnoff int freei; 47519542ea7bSGleb Smirnoff 47529542ea7bSGleb Smirnoff if (slab == NULL) { 47539542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 47549542ea7bSGleb Smirnoff if (slab == NULL) 47559542ea7bSGleb Smirnoff panic("uma: Freed item %p did not belong to zone %s\n", 47569542ea7bSGleb Smirnoff item, zone->uz_name); 47579542ea7bSGleb Smirnoff } 4758584061b4SJeff Roberson keg = zone->uz_keg; 47591e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 47609542ea7bSGleb Smirnoff 47619542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 47629542ea7bSGleb Smirnoff panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n", 47639542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 47649542ea7bSGleb Smirnoff 47651e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 47669542ea7bSGleb Smirnoff panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n", 47679542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 47689542ea7bSGleb Smirnoff 4769815db204SRyan Libby if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 47709542ea7bSGleb Smirnoff panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n", 47719542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 47729542ea7bSGleb Smirnoff 4773815db204SRyan Libby BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 47749542ea7bSGleb Smirnoff } 47759542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 47769542ea7bSGleb Smirnoff 477748c5777eSRobert Watson #ifdef DDB 477846d70077SConrad Meyer static int64_t 477946d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 47800223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 478148c5777eSRobert Watson { 478246d70077SConrad Meyer uint64_t frees; 47830f9b7bf3SMark Johnston int i; 478448c5777eSRobert Watson 478548c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 478646d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 47872efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 478846d70077SConrad Meyer *sleeps = z->uz_sleeps; 478946d70077SConrad Meyer *cachefree = 0; 479046d70077SConrad Meyer *xdomain = 0; 479148c5777eSRobert Watson } else 479246d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 479346d70077SConrad Meyer xdomain); 4794e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 479548c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 479646d70077SConrad Meyer *cachefree += kz->uk_free; 47970f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 479846d70077SConrad Meyer *cachefree += z->uz_domain[i].uzd_nitems; 479946d70077SConrad Meyer *used = *allocs - frees; 480046d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 480146d70077SConrad Meyer } 48020f9b7bf3SMark Johnston 480346d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 480446d70077SConrad Meyer { 480546d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 480646d70077SConrad Meyer uma_keg_t kz; 480746d70077SConrad Meyer uma_zone_t z; 480846d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 480946d70077SConrad Meyer long cachefree; 481046d70077SConrad Meyer /* variables for sorting */ 481146d70077SConrad Meyer uma_keg_t cur_keg; 481246d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 481346d70077SConrad Meyer int64_t cur_size, last_size, size; 481446d70077SConrad Meyer int ties; 481546d70077SConrad Meyer 481646d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 481746d70077SConrad Meyer if (modif[0] == 'i') { 481846d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 481946d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 482046d70077SConrad Meyer } else { 482146d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 482246d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 482346d70077SConrad Meyer } 482446d70077SConrad Meyer 482546d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 482646d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 482746d70077SConrad Meyer 482846d70077SConrad Meyer /* Sort the zones with largest size first. */ 482946d70077SConrad Meyer last_zone = NULL; 483046d70077SConrad Meyer last_size = INT64_MAX; 483146d70077SConrad Meyer for (;;) { 483246d70077SConrad Meyer cur_zone = NULL; 483346d70077SConrad Meyer cur_size = -1; 483446d70077SConrad Meyer ties = 0; 483546d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 483646d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 483746d70077SConrad Meyer /* 483846d70077SConrad Meyer * In the case of size ties, print out zones 483946d70077SConrad Meyer * in the order they are encountered. That is, 484046d70077SConrad Meyer * when we encounter the most recently output 484146d70077SConrad Meyer * zone, we have already printed all preceding 484246d70077SConrad Meyer * ties, and we must print all following ties. 484346d70077SConrad Meyer */ 484446d70077SConrad Meyer if (z == last_zone) { 484546d70077SConrad Meyer ties = 1; 484646d70077SConrad Meyer continue; 484746d70077SConrad Meyer } 484846d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 484946d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 485046d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 485146d70077SConrad Meyer { 485246d70077SConrad Meyer cur_size = size; 485346d70077SConrad Meyer cur_zone = z; 485446d70077SConrad Meyer cur_keg = kz; 485546d70077SConrad Meyer } 485646d70077SConrad Meyer } 485746d70077SConrad Meyer } 485846d70077SConrad Meyer if (cur_zone == NULL) 485946d70077SConrad Meyer break; 486046d70077SConrad Meyer 486146d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 486246d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 486346d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 486446d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 486546d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 486620a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 486720a4e154SJeff Roberson xdomain); 486846d70077SConrad Meyer 4869687c94aaSJohn Baldwin if (db_pager_quit) 4870687c94aaSJohn Baldwin return; 487146d70077SConrad Meyer last_zone = cur_zone; 487246d70077SConrad Meyer last_size = cur_size; 487348c5777eSRobert Watson } 487448c5777eSRobert Watson } 487503175483SAlexander Motin 487603175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 487703175483SAlexander Motin { 487803175483SAlexander Motin uma_zone_t z; 4879ab3185d1SJeff Roberson uint64_t allocs, frees; 48800f9b7bf3SMark Johnston long cachefree; 48810f9b7bf3SMark Johnston int i; 488203175483SAlexander Motin 488303175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 488403175483SAlexander Motin "Requests", "Bucket"); 488503175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 4886c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 48870f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 48880f9b7bf3SMark Johnston cachefree += z->uz_domain[i].uzd_nitems; 48890f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 489003175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 489103175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 489220a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 489303175483SAlexander Motin if (db_pager_quit) 489403175483SAlexander Motin return; 489503175483SAlexander Motin } 489603175483SAlexander Motin } 48979542ea7bSGleb Smirnoff #endif /* DDB */ 4898