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> 78*4bd61e19SJeff 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); 272*4bd61e19SJeff Roberson static int zone_alloc_limit(uma_zone_t zone, int count, int flags); 273*4bd61e19SJeff 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); 295*4bd61e19SJeff 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. 899*4bd61e19SJeff 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 910*4bd61e19SJeff 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); 917*4bd61e19SJeff Roberson if (zone->uz_max_items > 0) 918*4bd61e19SJeff 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 * 9255d1ae027SRobert Watson * NOTE: This may only be called while the zone is being turn 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 /* 1044a2de44abSAlexander Motin * Polite bucket sizes shrinking was not enouth, 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; 1225af526374SJeff Roberson msleep(zone, zone->uz_lockptr, 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. 1261194a979eSMark Johnston * If the allocation was successful, the keg lock will be held upon return, 1262194a979eSMark Johnston * otherwise the keg will be left unlocked. 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); 1286bb15d1c7SGleb Smirnoff MPASS(zone->uz_lockptr == &keg->uk_lock); 1287a553d4b8SJeff Roberson 1288e20a199fSJeff Roberson allocf = keg->uk_allocf; 1289e20a199fSJeff Roberson KEG_UNLOCK(keg); 1290a553d4b8SJeff Roberson 1291194a979eSMark Johnston slab = NULL; 1292194a979eSMark Johnston mem = NULL; 1293099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_OFFPAGE) { 129486220393SMark Johnston slab = zone_alloc_item(keg->uk_slabzone, NULL, domain, aflags); 1295fc03d22bSJeff Roberson if (slab == NULL) 1296fc03d22bSJeff Roberson goto out; 1297a553d4b8SJeff Roberson } 1298a553d4b8SJeff Roberson 12993370c5bfSJeff Roberson /* 13003370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 13013370c5bfSJeff Roberson * first time they are added to a zone. 13023370c5bfSJeff Roberson * 13033370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 13043370c5bfSJeff Roberson */ 13053370c5bfSJeff Roberson 1306099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 130786220393SMark Johnston aflags |= M_ZERO; 13083370c5bfSJeff Roberson else 130986220393SMark Johnston aflags &= ~M_ZERO; 13103370c5bfSJeff Roberson 1311263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 131286220393SMark Johnston aflags |= M_NODUMP; 1313263811f7SKip Macy 1314e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1315194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 131686220393SMark Johnston mem = allocf(zone, size, domain, &sflags, aflags); 1317a553d4b8SJeff Roberson if (mem == NULL) { 1318b23f72e9SBrian Feldman if (keg->uk_flags & UMA_ZONE_OFFPAGE) 13190095a784SJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 1320fc03d22bSJeff Roberson slab = NULL; 1321fc03d22bSJeff Roberson goto out; 1322a553d4b8SJeff Roberson } 13232e47807cSJeff Roberson uma_total_inc(size); 13248355f576SJeff Roberson 13255c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 1326099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) 1327099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 13281e0701e1SJeff Roberson else 13291e0701e1SJeff Roberson ((uma_hash_slab_t)slab)->uhs_data = mem; 13305c0e403bSJeff Roberson 1331e20a199fSJeff Roberson if (keg->uk_flags & UMA_ZONE_VTOSLAB) 1332099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1333584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1334584061b4SJeff Roberson zone, slab); 13358355f576SJeff Roberson 1336099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 133786220393SMark Johnston slab->us_flags = sflags; 1338ab3185d1SJeff Roberson slab->us_domain = domain; 13399b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1340ef72505eSJeff Roberson #ifdef INVARIANTS 1341815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1342ef72505eSJeff Roberson #endif 1343099a0e58SBosko Milekic 1344b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1345099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 13461e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 134786220393SMark Johnston keg->uk_size, flags) != 0) 1348b23f72e9SBrian Feldman break; 1349b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1350fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1351fc03d22bSJeff Roberson slab = NULL; 1352fc03d22bSJeff Roberson goto out; 1353b23f72e9SBrian Feldman } 1354b23f72e9SBrian Feldman } 1355e20a199fSJeff Roberson KEG_LOCK(keg); 13565c0e403bSJeff Roberson 13571431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 13581431a748SGleb Smirnoff slab, keg->uk_name, keg); 13591431a748SGleb Smirnoff 1360099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 1361099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 13628355f576SJeff Roberson 1363099a0e58SBosko Milekic keg->uk_pages += keg->uk_ppera; 1364099a0e58SBosko Milekic keg->uk_free += keg->uk_ipers; 13658355f576SJeff Roberson 1366194a979eSMark Johnston out: 13678355f576SJeff Roberson return (slab); 13688355f576SJeff Roberson } 13698355f576SJeff Roberson 13708355f576SJeff Roberson /* 1371009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1372009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1373009b6fcbSJeff Roberson * the VM is ready. 1374009b6fcbSJeff Roberson */ 1375009b6fcbSJeff Roberson static void * 1376ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1377ab3185d1SJeff Roberson int wait) 1378009b6fcbSJeff Roberson { 1379099a0e58SBosko Milekic uma_keg_t keg; 1380ac0a6fd0SGleb Smirnoff void *mem; 1381ac0a6fd0SGleb Smirnoff int pages; 1382099a0e58SBosko Milekic 1383bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1384009b6fcbSJeff Roberson /* 1385f7d35785SGleb Smirnoff * If we are in BOOT_BUCKETS or higher, than switch to real 1386f7d35785SGleb Smirnoff * allocator. Zones with page sized slabs switch at BOOT_PAGEALLOC. 1387009b6fcbSJeff Roberson */ 1388f7d35785SGleb Smirnoff switch (booted) { 1389f7d35785SGleb Smirnoff case BOOT_COLD: 1390f7d35785SGleb Smirnoff case BOOT_STRAPPED: 1391f7d35785SGleb Smirnoff break; 1392f7d35785SGleb Smirnoff case BOOT_PAGEALLOC: 1393f7d35785SGleb Smirnoff if (keg->uk_ppera > 1) 1394f7d35785SGleb Smirnoff break; 1395f7d35785SGleb Smirnoff case BOOT_BUCKETS: 1396f7d35785SGleb Smirnoff case BOOT_RUNNING: 1397009b6fcbSJeff Roberson #ifdef UMA_MD_SMALL_ALLOC 1398f7d35785SGleb Smirnoff keg->uk_allocf = (keg->uk_ppera > 1) ? 1399f7d35785SGleb Smirnoff page_alloc : uma_small_alloc; 1400009b6fcbSJeff Roberson #else 1401099a0e58SBosko Milekic keg->uk_allocf = page_alloc; 1402009b6fcbSJeff Roberson #endif 1403ab3185d1SJeff Roberson return keg->uk_allocf(zone, bytes, domain, pflag, wait); 1404009b6fcbSJeff Roberson } 1405009b6fcbSJeff Roberson 1406009b6fcbSJeff Roberson /* 1407f7d35785SGleb Smirnoff * Check our small startup cache to see if it has pages remaining. 1408f7d35785SGleb Smirnoff */ 1409f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1410f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1411f7d35785SGleb Smirnoff if (pages > boot_pages) 1412f7d35785SGleb Smirnoff panic("UMA zone \"%s\": Increase vm.boot_pages", zone->uz_name); 1413f7d35785SGleb Smirnoff #ifdef DIAGNOSTIC 1414f7d35785SGleb Smirnoff printf("%s from \"%s\", %d boot pages left\n", __func__, zone->uz_name, 1415f7d35785SGleb Smirnoff boot_pages); 1416f7d35785SGleb Smirnoff #endif 1417f7d35785SGleb Smirnoff mem = bootmem; 1418f7d35785SGleb Smirnoff boot_pages -= pages; 1419f7d35785SGleb Smirnoff bootmem += pages * PAGE_SIZE; 1420f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1421f7d35785SGleb Smirnoff 1422f7d35785SGleb Smirnoff return (mem); 1423f7d35785SGleb Smirnoff } 1424f7d35785SGleb Smirnoff 1425f7d35785SGleb Smirnoff /* 14268355f576SJeff Roberson * Allocates a number of pages from the system 14278355f576SJeff Roberson * 14288355f576SJeff Roberson * Arguments: 14298355f576SJeff Roberson * bytes The number of bytes requested 14308355f576SJeff Roberson * wait Shall we wait? 14318355f576SJeff Roberson * 14328355f576SJeff Roberson * Returns: 14338355f576SJeff Roberson * A pointer to the alloced memory or possibly 14348355f576SJeff Roberson * NULL if M_NOWAIT is set. 14358355f576SJeff Roberson */ 14368355f576SJeff Roberson static void * 1437ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1438ab3185d1SJeff Roberson int wait) 14398355f576SJeff Roberson { 14408355f576SJeff Roberson void *p; /* Returned page */ 14418355f576SJeff Roberson 14422e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 14439978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 14448355f576SJeff Roberson 14458355f576SJeff Roberson return (p); 14468355f576SJeff Roberson } 14478355f576SJeff Roberson 1448ab3059a8SMatt Macy static void * 1449ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1450ab3059a8SMatt Macy int wait) 1451ab3059a8SMatt Macy { 1452ab3059a8SMatt Macy struct pglist alloctail; 1453ab3059a8SMatt Macy vm_offset_t addr, zkva; 1454ab3059a8SMatt Macy int cpu, flags; 1455ab3059a8SMatt Macy vm_page_t p, p_next; 1456ab3059a8SMatt Macy #ifdef NUMA 1457ab3059a8SMatt Macy struct pcpu *pc; 1458ab3059a8SMatt Macy #endif 1459ab3059a8SMatt Macy 1460ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1461ab3059a8SMatt Macy 1462013072f0SMark Johnston TAILQ_INIT(&alloctail); 1463ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1464013072f0SMark Johnston malloc2vm_flags(wait); 1465013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1466ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1467ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1468ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1469ab3059a8SMatt Macy } else { 1470ab3059a8SMatt Macy #ifndef NUMA 1471ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1472ab3059a8SMatt Macy #else 1473ab3059a8SMatt Macy pc = pcpu_find(cpu); 1474ab3059a8SMatt Macy p = vm_page_alloc_domain(NULL, 0, pc->pc_domain, flags); 1475ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1476ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1477ab3059a8SMatt Macy #endif 1478ab3059a8SMatt Macy } 1479ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1480ab3059a8SMatt Macy goto fail; 1481ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1482ab3059a8SMatt Macy } 1483ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1484ab3059a8SMatt Macy goto fail; 1485ab3059a8SMatt Macy zkva = addr; 1486ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1487ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1488ab3059a8SMatt Macy zkva += PAGE_SIZE; 1489ab3059a8SMatt Macy } 1490ab3059a8SMatt Macy return ((void*)addr); 1491ab3059a8SMatt Macy fail: 1492ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 149388ea538aSMark Johnston vm_page_unwire_noq(p); 1494ab3059a8SMatt Macy vm_page_free(p); 1495ab3059a8SMatt Macy } 1496ab3059a8SMatt Macy return (NULL); 1497ab3059a8SMatt Macy } 1498ab3059a8SMatt Macy 14998355f576SJeff Roberson /* 15008355f576SJeff Roberson * Allocates a number of pages from within an object 15018355f576SJeff Roberson * 15028355f576SJeff Roberson * Arguments: 15038355f576SJeff Roberson * bytes The number of bytes requested 15048355f576SJeff Roberson * wait Shall we wait? 15058355f576SJeff Roberson * 15068355f576SJeff Roberson * Returns: 15078355f576SJeff Roberson * A pointer to the alloced memory or possibly 15088355f576SJeff Roberson * NULL if M_NOWAIT is set. 15098355f576SJeff Roberson */ 15108355f576SJeff Roberson static void * 1511ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1512ab3185d1SJeff Roberson int wait) 15138355f576SJeff Roberson { 1514a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1515a4915c21SAttilio Rao u_long npages; 1516b245ac95SAlan Cox vm_offset_t retkva, zkva; 1517a4915c21SAttilio Rao vm_page_t p, p_next; 1518e20a199fSJeff Roberson uma_keg_t keg; 15198355f576SJeff Roberson 1520a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1521bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1522a4915c21SAttilio Rao 1523a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1524a4915c21SAttilio Rao while (npages > 0) { 1525ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 15268d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1527772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1528772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1529a4915c21SAttilio Rao if (p != NULL) { 1530a4915c21SAttilio Rao /* 1531a4915c21SAttilio Rao * Since the page does not belong to an object, its 1532a4915c21SAttilio Rao * listq is unused. 1533a4915c21SAttilio Rao */ 1534a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1535a4915c21SAttilio Rao npages--; 1536a4915c21SAttilio Rao continue; 1537a4915c21SAttilio Rao } 15388355f576SJeff Roberson /* 1539a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1540a4915c21SAttilio Rao * exit. 15418355f576SJeff Roberson */ 1542a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 154388ea538aSMark Johnston vm_page_unwire_noq(p); 1544b245ac95SAlan Cox vm_page_free(p); 1545b245ac95SAlan Cox } 1546a4915c21SAttilio Rao return (NULL); 1547b245ac95SAlan Cox } 15488355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1549a4915c21SAttilio Rao zkva = keg->uk_kva + 1550a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1551a4915c21SAttilio Rao retkva = zkva; 1552a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1553a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1554a4915c21SAttilio Rao zkva += PAGE_SIZE; 1555a4915c21SAttilio Rao } 15568355f576SJeff Roberson 15578355f576SJeff Roberson return ((void *)retkva); 15588355f576SJeff Roberson } 15598355f576SJeff Roberson 15608355f576SJeff Roberson /* 15618355f576SJeff Roberson * Frees a number of pages to the system 15628355f576SJeff Roberson * 15638355f576SJeff Roberson * Arguments: 15648355f576SJeff Roberson * mem A pointer to the memory to be freed 15658355f576SJeff Roberson * size The size of the memory being freed 15668355f576SJeff Roberson * flags The original p->us_flags field 15678355f576SJeff Roberson * 15688355f576SJeff Roberson * Returns: 15698355f576SJeff Roberson * Nothing 15708355f576SJeff Roberson */ 15718355f576SJeff Roberson static void 1572f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 15738355f576SJeff Roberson { 15743370c5bfSJeff Roberson 157549bfa624SAlan Cox if ((flags & UMA_SLAB_KERNEL) == 0) 1576b5345ef1SJustin Hibbits panic("UMA: page_free used with invalid flags %x", flags); 15778355f576SJeff Roberson 157849bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 15798355f576SJeff Roberson } 15808355f576SJeff Roberson 15818355f576SJeff Roberson /* 1582ab3059a8SMatt Macy * Frees pcpu zone allocations 1583ab3059a8SMatt Macy * 1584ab3059a8SMatt Macy * Arguments: 1585ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1586ab3059a8SMatt Macy * size The size of the memory being freed 1587ab3059a8SMatt Macy * flags The original p->us_flags field 1588ab3059a8SMatt Macy * 1589ab3059a8SMatt Macy * Returns: 1590ab3059a8SMatt Macy * Nothing 1591ab3059a8SMatt Macy */ 1592ab3059a8SMatt Macy static void 1593ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1594ab3059a8SMatt Macy { 1595ab3059a8SMatt Macy vm_offset_t sva, curva; 1596ab3059a8SMatt Macy vm_paddr_t paddr; 1597ab3059a8SMatt Macy vm_page_t m; 1598ab3059a8SMatt Macy 1599ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 1600ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1601ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1602ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1603ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 160488ea538aSMark Johnston vm_page_unwire_noq(m); 1605ab3059a8SMatt Macy vm_page_free(m); 1606ab3059a8SMatt Macy } 1607ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1608ab3059a8SMatt Macy kva_free(sva, size); 1609ab3059a8SMatt Macy } 1610ab3059a8SMatt Macy 1611ab3059a8SMatt Macy 1612ab3059a8SMatt Macy /* 16138355f576SJeff Roberson * Zero fill initializer 16148355f576SJeff Roberson * 16158355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 16168355f576SJeff Roberson */ 1617b23f72e9SBrian Feldman static int 1618b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 16198355f576SJeff Roberson { 16208355f576SJeff Roberson bzero(mem, size); 1621b23f72e9SBrian Feldman return (0); 16228355f576SJeff Roberson } 16238355f576SJeff Roberson 1624815db204SRyan Libby #ifdef INVARIANTS 1625815db204SRyan Libby struct noslabbits * 1626815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1627815db204SRyan Libby { 1628815db204SRyan Libby 1629815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1630815db204SRyan Libby } 1631815db204SRyan Libby #endif 1632815db204SRyan Libby 16338355f576SJeff Roberson /* 16349b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 16359b78b1f4SJeff Roberson */ 16369b78b1f4SJeff Roberson size_t 16379b78b1f4SJeff Roberson slab_sizeof(int nitems) 16389b78b1f4SJeff Roberson { 16399b78b1f4SJeff Roberson size_t s; 16409b78b1f4SJeff Roberson 1641815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 16429b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 16439b78b1f4SJeff Roberson } 16449b78b1f4SJeff Roberson 16459b78b1f4SJeff Roberson /* 16469b78b1f4SJeff Roberson * Size of memory for embedded slabs (!OFFPAGE). 16479b78b1f4SJeff Roberson */ 16489b78b1f4SJeff Roberson size_t 16499b78b1f4SJeff Roberson slab_space(int nitems) 16509b78b1f4SJeff Roberson { 16519b78b1f4SJeff Roberson return (UMA_SLAB_SIZE - slab_sizeof(nitems)); 16529b78b1f4SJeff Roberson } 16539b78b1f4SJeff Roberson 16549b78b1f4SJeff Roberson /* 16559b78b1f4SJeff Roberson * Compute the number of items that will fit in an embedded (!OFFPAGE) slab 16569b78b1f4SJeff Roberson * with a given size and alignment. 16579b78b1f4SJeff Roberson */ 16589b78b1f4SJeff Roberson int 16599b78b1f4SJeff Roberson slab_ipers(size_t size, int align) 16609b78b1f4SJeff Roberson { 16619b78b1f4SJeff Roberson int rsize; 16629b78b1f4SJeff Roberson int nitems; 16639b78b1f4SJeff Roberson 16649b78b1f4SJeff Roberson /* 16659b78b1f4SJeff Roberson * Compute the ideal number of items that will fit in a page and 16669b78b1f4SJeff Roberson * then compute the actual number based on a bitset nitems wide. 16679b78b1f4SJeff Roberson */ 16689b78b1f4SJeff Roberson rsize = roundup(size, align + 1); 16699b78b1f4SJeff Roberson nitems = UMA_SLAB_SIZE / rsize; 16709b78b1f4SJeff Roberson return (slab_space(nitems) / rsize); 16719b78b1f4SJeff Roberson } 16729b78b1f4SJeff Roberson 16739b78b1f4SJeff Roberson /* 1674e20a199fSJeff Roberson * Finish creating a small uma keg. This calculates ipers, and the keg size. 16758355f576SJeff Roberson * 16768355f576SJeff Roberson * Arguments 1677e20a199fSJeff Roberson * keg The zone we should initialize 16788355f576SJeff Roberson * 16798355f576SJeff Roberson * Returns 16808355f576SJeff Roberson * Nothing 16818355f576SJeff Roberson */ 16828355f576SJeff Roberson static void 1683e20a199fSJeff Roberson keg_small_init(uma_keg_t keg) 16848355f576SJeff Roberson { 1685244f4554SBosko Milekic u_int rsize; 1686244f4554SBosko Milekic u_int memused; 1687244f4554SBosko Milekic u_int wastedspace; 1688244f4554SBosko Milekic u_int shsize; 1689a55ebb7cSAndriy Gapon u_int slabsize; 16908355f576SJeff Roberson 1691ad97af7eSGleb Smirnoff if (keg->uk_flags & UMA_ZONE_PCPU) { 169296c85efbSNathan Whitehorn u_int ncpus = (mp_maxid + 1) ? (mp_maxid + 1) : MAXCPU; 1693e28a647dSGleb Smirnoff 1694ab3059a8SMatt Macy slabsize = UMA_PCPU_ALLOC_SIZE; 1695ab3059a8SMatt Macy keg->uk_ppera = ncpus; 1696ad97af7eSGleb Smirnoff } else { 1697a55ebb7cSAndriy Gapon slabsize = UMA_SLAB_SIZE; 1698ad97af7eSGleb Smirnoff keg->uk_ppera = 1; 1699ad97af7eSGleb Smirnoff } 1700ad97af7eSGleb Smirnoff 1701ef72505eSJeff Roberson /* 1702ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1703ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1704ef72505eSJeff Roberson * allocation bits for we round it up. 1705ef72505eSJeff Roberson */ 1706099a0e58SBosko Milekic rsize = keg->uk_size; 17079b78b1f4SJeff Roberson if (rsize < slabsize / SLAB_MAX_SETSIZE) 17089b78b1f4SJeff Roberson rsize = slabsize / SLAB_MAX_SETSIZE; 1709099a0e58SBosko Milekic if (rsize & keg->uk_align) 17109b78b1f4SJeff Roberson rsize = roundup(rsize, keg->uk_align + 1); 1711099a0e58SBosko Milekic keg->uk_rsize = rsize; 1712ad97af7eSGleb Smirnoff 1713ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 1714ab3059a8SMatt Macy keg->uk_rsize < UMA_PCPU_ALLOC_SIZE, 1715ad97af7eSGleb Smirnoff ("%s: size %u too large", __func__, keg->uk_rsize)); 17168355f576SJeff Roberson 17179b78b1f4SJeff Roberson /* 17189b78b1f4SJeff Roberson * Use a pessimistic bit count for shsize. It may be possible to 17199b78b1f4SJeff Roberson * squeeze one more item in for very particular sizes if we were 17209b78b1f4SJeff Roberson * to loop and reduce the bitsize if there is waste. 17219b78b1f4SJeff Roberson */ 1722ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_OFFPAGE) 17232864dbbfSGleb Smirnoff shsize = 0; 1724ef72505eSJeff Roberson else 17259b78b1f4SJeff Roberson shsize = slab_sizeof(slabsize / rsize); 17268355f576SJeff Roberson 17271ca6ed45SGleb Smirnoff if (rsize <= slabsize - shsize) 1728a55ebb7cSAndriy Gapon keg->uk_ipers = (slabsize - shsize) / rsize; 17291ca6ed45SGleb Smirnoff else { 17301ca6ed45SGleb Smirnoff /* Handle special case when we have 1 item per slab, so 17311ca6ed45SGleb Smirnoff * alignment requirement can be relaxed. */ 17321ca6ed45SGleb Smirnoff KASSERT(keg->uk_size <= slabsize - shsize, 17331ca6ed45SGleb Smirnoff ("%s: size %u greater than slab", __func__, keg->uk_size)); 17341ca6ed45SGleb Smirnoff keg->uk_ipers = 1; 17351ca6ed45SGleb Smirnoff } 17369b78b1f4SJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 1737ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 1738ad97af7eSGleb Smirnoff 1739244f4554SBosko Milekic memused = keg->uk_ipers * rsize + shsize; 1740a55ebb7cSAndriy Gapon wastedspace = slabsize - memused; 1741244f4554SBosko Milekic 174220e8e865SBosko Milekic /* 1743244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 174420e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 17456fd34d6fSJeff Roberson * may end up going to the VM for slabs which we do not 17466fd34d6fSJeff Roberson * want to do if we're UMA_ZFLAG_CACHEONLY as a result 17476fd34d6fSJeff Roberson * of UMA_ZONE_VM, which clearly forbids it. 174820e8e865SBosko Milekic */ 1749099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) || 1750099a0e58SBosko Milekic (keg->uk_flags & UMA_ZFLAG_CACHEONLY)) 17518355f576SJeff Roberson return; 1752244f4554SBosko Milekic 1753ef72505eSJeff Roberson /* 1754ef72505eSJeff Roberson * See if using an OFFPAGE slab will limit our waste. Only do 1755ef72505eSJeff Roberson * this if it permits more items per-slab. 1756ef72505eSJeff Roberson * 1757ef72505eSJeff Roberson * XXX We could try growing slabsize to limit max waste as well. 1758ef72505eSJeff Roberson * Historically this was not done because the VM could not 1759ef72505eSJeff Roberson * efficiently handle contiguous allocations. 1760ef72505eSJeff Roberson */ 1761a55ebb7cSAndriy Gapon if ((wastedspace >= slabsize / UMA_MAX_WASTE) && 1762a55ebb7cSAndriy Gapon (keg->uk_ipers < (slabsize / keg->uk_rsize))) { 1763a55ebb7cSAndriy Gapon keg->uk_ipers = slabsize / keg->uk_rsize; 17649b78b1f4SJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 1765ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 17661431a748SGleb Smirnoff CTR6(KTR_UMA, "UMA decided we need offpage slab headers for " 17671431a748SGleb Smirnoff "keg: %s(%p), calculated wastedspace = %d, " 1768244f4554SBosko Milekic "maximum wasted space allowed = %d, " 1769244f4554SBosko Milekic "calculated ipers = %d, " 17701431a748SGleb Smirnoff "new wasted space = %d\n", keg->uk_name, keg, wastedspace, 1771a55ebb7cSAndriy Gapon slabsize / UMA_MAX_WASTE, keg->uk_ipers, 1772a55ebb7cSAndriy Gapon slabsize - keg->uk_ipers * keg->uk_rsize); 177371353f7aSJeff Roberson /* 177471353f7aSJeff Roberson * If we had access to memory to embed a slab header we 177571353f7aSJeff Roberson * also have a page structure to use vtoslab() instead of 177671353f7aSJeff Roberson * hash to find slabs. If the zone was explicitly created 177771353f7aSJeff Roberson * OFFPAGE we can't necessarily touch the memory. 177871353f7aSJeff Roberson */ 177971353f7aSJeff Roberson if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0) 178071353f7aSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 17818355f576SJeff Roberson } 1782ad97af7eSGleb Smirnoff 1783ad97af7eSGleb Smirnoff if ((keg->uk_flags & UMA_ZONE_OFFPAGE) && 1784ad97af7eSGleb Smirnoff (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1785ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_HASH; 17868355f576SJeff Roberson } 17878355f576SJeff Roberson 17888355f576SJeff Roberson /* 1789e20a199fSJeff Roberson * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do 17908355f576SJeff Roberson * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be 17918355f576SJeff Roberson * more complicated. 17928355f576SJeff Roberson * 17938355f576SJeff Roberson * Arguments 1794e20a199fSJeff Roberson * keg The keg we should initialize 17958355f576SJeff Roberson * 17968355f576SJeff Roberson * Returns 17978355f576SJeff Roberson * Nothing 17988355f576SJeff Roberson */ 17998355f576SJeff Roberson static void 1800e20a199fSJeff Roberson keg_large_init(uma_keg_t keg) 18018355f576SJeff Roberson { 18028355f576SJeff Roberson 1803e20a199fSJeff Roberson KASSERT(keg != NULL, ("Keg is null in keg_large_init")); 1804ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1805ad97af7eSGleb Smirnoff ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__)); 180620e8e865SBosko Milekic 1807ad97af7eSGleb Smirnoff keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE); 1808099a0e58SBosko Milekic keg->uk_ipers = 1; 1809e9a069d8SJohn Baldwin keg->uk_rsize = keg->uk_size; 1810e9a069d8SJohn Baldwin 1811cec48e00SAlexander Motin /* Check whether we have enough space to not do OFFPAGE. */ 18123d5e3df7SGleb Smirnoff if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0 && 18139b78b1f4SJeff Roberson PAGE_SIZE * keg->uk_ppera - keg->uk_rsize < 18149b78b1f4SJeff Roberson slab_sizeof(SLAB_MIN_SETSIZE)) { 18152934eb8aSMark Johnston /* 18162934eb8aSMark Johnston * We can't do OFFPAGE if we're internal, in which case 18172934eb8aSMark Johnston * we need an extra page per allocation to contain the 18182934eb8aSMark Johnston * slab header. 18192934eb8aSMark Johnston */ 18202934eb8aSMark Johnston if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) == 0) 182171353f7aSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 18222934eb8aSMark Johnston else 18232934eb8aSMark Johnston keg->uk_ppera++; 18242934eb8aSMark Johnston } 1825cec48e00SAlexander Motin 1826cec48e00SAlexander Motin if ((keg->uk_flags & UMA_ZONE_OFFPAGE) && 1827cec48e00SAlexander Motin (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1828099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_HASH; 18298355f576SJeff Roberson } 18308355f576SJeff Roberson 1831e20a199fSJeff Roberson static void 1832e20a199fSJeff Roberson keg_cachespread_init(uma_keg_t keg) 1833e20a199fSJeff Roberson { 1834e20a199fSJeff Roberson int alignsize; 1835e20a199fSJeff Roberson int trailer; 1836e20a199fSJeff Roberson int pages; 1837e20a199fSJeff Roberson int rsize; 1838e20a199fSJeff Roberson 1839ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1840ad97af7eSGleb Smirnoff ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__)); 1841ad97af7eSGleb Smirnoff 1842e20a199fSJeff Roberson alignsize = keg->uk_align + 1; 1843e20a199fSJeff Roberson rsize = keg->uk_size; 1844e20a199fSJeff Roberson /* 1845e20a199fSJeff Roberson * We want one item to start on every align boundary in a page. To 1846e20a199fSJeff Roberson * do this we will span pages. We will also extend the item by the 1847e20a199fSJeff Roberson * size of align if it is an even multiple of align. Otherwise, it 1848e20a199fSJeff Roberson * would fall on the same boundary every time. 1849e20a199fSJeff Roberson */ 1850e20a199fSJeff Roberson if (rsize & keg->uk_align) 1851e20a199fSJeff Roberson rsize = (rsize & ~keg->uk_align) + alignsize; 1852e20a199fSJeff Roberson if ((rsize & alignsize) == 0) 1853e20a199fSJeff Roberson rsize += alignsize; 1854e20a199fSJeff Roberson trailer = rsize - keg->uk_size; 1855e20a199fSJeff Roberson pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE; 1856e20a199fSJeff Roberson pages = MIN(pages, (128 * 1024) / PAGE_SIZE); 1857e20a199fSJeff Roberson keg->uk_rsize = rsize; 1858e20a199fSJeff Roberson keg->uk_ppera = pages; 1859e20a199fSJeff Roberson keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize; 1860e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 18619b78b1f4SJeff Roberson KASSERT(keg->uk_ipers <= SLAB_MAX_SETSIZE, 186242321809SGleb Smirnoff ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__, 1863e20a199fSJeff Roberson keg->uk_ipers)); 1864e20a199fSJeff Roberson } 1865e20a199fSJeff Roberson 18668355f576SJeff Roberson /* 1867099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 1868099a0e58SBosko Milekic * the keg onto the global keg list. 18698355f576SJeff Roberson * 18708355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 1871099a0e58SBosko Milekic * udata Actually uma_kctor_args 1872099a0e58SBosko Milekic */ 1873b23f72e9SBrian Feldman static int 1874b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 1875099a0e58SBosko Milekic { 1876099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 1877099a0e58SBosko Milekic uma_keg_t keg = mem; 1878099a0e58SBosko Milekic uma_zone_t zone; 1879099a0e58SBosko Milekic 1880099a0e58SBosko Milekic bzero(keg, size); 1881099a0e58SBosko Milekic keg->uk_size = arg->size; 1882099a0e58SBosko Milekic keg->uk_init = arg->uminit; 1883099a0e58SBosko Milekic keg->uk_fini = arg->fini; 1884099a0e58SBosko Milekic keg->uk_align = arg->align; 1885099a0e58SBosko Milekic keg->uk_free = 0; 18866fd34d6fSJeff Roberson keg->uk_reserve = 0; 1887099a0e58SBosko Milekic keg->uk_pages = 0; 1888099a0e58SBosko Milekic keg->uk_flags = arg->flags; 1889099a0e58SBosko Milekic keg->uk_slabzone = NULL; 1890099a0e58SBosko Milekic 1891099a0e58SBosko Milekic /* 1892194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 1893194a979eSMark Johnston * UMA_ZONE_NUMA set will use first-touch instead, in which case the 1894194a979eSMark Johnston * iterator is never run. 1895194a979eSMark Johnston */ 1896194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 1897194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 1898194a979eSMark Johnston 1899194a979eSMark Johnston /* 1900099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 1901099a0e58SBosko Milekic */ 1902099a0e58SBosko Milekic zone = arg->zone; 1903e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 1904099a0e58SBosko Milekic 1905099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_VM) 1906099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 1907099a0e58SBosko Milekic 1908099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 1909099a0e58SBosko Milekic keg->uk_init = zero_init; 1910099a0e58SBosko Milekic 1911cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 1912e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_VTOSLAB; 1913e20a199fSJeff Roberson 1914ad97af7eSGleb Smirnoff if (arg->flags & UMA_ZONE_PCPU) 1915ad97af7eSGleb Smirnoff #ifdef SMP 1916ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_OFFPAGE; 1917ad97af7eSGleb Smirnoff #else 1918ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 1919ad97af7eSGleb Smirnoff #endif 1920ad97af7eSGleb Smirnoff 1921ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_CACHESPREAD) { 1922e20a199fSJeff Roberson keg_cachespread_init(keg); 1923244f4554SBosko Milekic } else { 19249b78b1f4SJeff Roberson if (keg->uk_size > slab_space(SLAB_MIN_SETSIZE)) 1925e20a199fSJeff Roberson keg_large_init(keg); 1926244f4554SBosko Milekic else 1927e20a199fSJeff Roberson keg_small_init(keg); 1928244f4554SBosko Milekic } 1929099a0e58SBosko Milekic 1930cfcae3f8SGleb Smirnoff if (keg->uk_flags & UMA_ZONE_OFFPAGE) 1931099a0e58SBosko Milekic keg->uk_slabzone = slabzone; 1932099a0e58SBosko Milekic 1933099a0e58SBosko Milekic /* 1934099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 1935099a0e58SBosko Milekic * startup cache until the vm is ready. 1936099a0e58SBosko Milekic */ 1937f4bef67cSGleb Smirnoff if (booted < BOOT_PAGEALLOC) 19388cd02d00SAlan Cox keg->uk_allocf = startup_alloc; 193977e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 194077e19437SGleb Smirnoff else if (keg->uk_ppera == 1) 194177e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 19428cd02d00SAlan Cox #endif 1943ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 1944ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 194577e19437SGleb Smirnoff else 194677e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 194777e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 194877e19437SGleb Smirnoff if (keg->uk_ppera == 1) 194977e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 195077e19437SGleb Smirnoff else 195177e19437SGleb Smirnoff #endif 1952ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 1953ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 1954ab3059a8SMatt Macy else 195577e19437SGleb Smirnoff keg->uk_freef = page_free; 1956099a0e58SBosko Milekic 1957099a0e58SBosko Milekic /* 1958af526374SJeff Roberson * Initialize keg's lock 1959099a0e58SBosko Milekic */ 1960af526374SJeff Roberson KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS)); 1961099a0e58SBosko Milekic 1962099a0e58SBosko Milekic /* 1963099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 19649b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 19659b78b1f4SJeff Roberson * definition. 1966099a0e58SBosko Milekic */ 1967099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) { 19689b78b1f4SJeff Roberson size_t shsize; 19699b78b1f4SJeff Roberson 19709b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 19719b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 1972244f4554SBosko Milekic /* 1973244f4554SBosko Milekic * The only way the following is possible is if with our 1974244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 1975244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 1976244f4554SBosko Milekic * mathematically possible for all cases, so we make 1977244f4554SBosko Milekic * sure here anyway. 1978244f4554SBosko Milekic */ 19799b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 19803d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 19813d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 1982099a0e58SBosko Milekic } 1983099a0e58SBosko Milekic 1984099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 19853b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 1986099a0e58SBosko Milekic 19871431a748SGleb Smirnoff CTR5(KTR_UMA, "keg_ctor %p zone %s(%p) out %d free %d\n", 19881431a748SGleb Smirnoff keg, zone->uz_name, zone, 198957223e99SAndriy Gapon (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free, 199057223e99SAndriy Gapon keg->uk_free); 1991099a0e58SBosko Milekic 1992099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 1993099a0e58SBosko Milekic 1994111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 1995099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 1996111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 1997b23f72e9SBrian Feldman return (0); 1998099a0e58SBosko Milekic } 1999099a0e58SBosko Milekic 20002efcc8cbSGleb Smirnoff static void 200120a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 20022efcc8cbSGleb Smirnoff { 20032efcc8cbSGleb Smirnoff 20042efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 20052efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 20062efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 20072efcc8cbSGleb Smirnoff } 20082efcc8cbSGleb Smirnoff 200920a4e154SJeff Roberson static void 201020a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 201120a4e154SJeff Roberson { 201220a4e154SJeff Roberson uma_zone_domain_t zdom; 201320a4e154SJeff Roberson uma_keg_t keg; 201420a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 20153b490537SJeff Roberson int domains, i, cnt; 201620a4e154SJeff Roberson static const char *nokeg = "cache zone"; 201720a4e154SJeff Roberson char *c; 201820a4e154SJeff Roberson 201920a4e154SJeff Roberson /* 202020a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 202120a4e154SJeff Roberson * any special characters and handling dups by appending 202220a4e154SJeff Roberson * an index. 202320a4e154SJeff Roberson */ 202420a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 20253b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 20263b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 20273b490537SJeff Roberson cnt /= 10; 20283b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 20293b490537SJeff Roberson M_UMA, M_WAITOK); 203020a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 203120a4e154SJeff Roberson zone->uz_namecnt); 203220a4e154SJeff Roberson } else 203320a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 203420a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 203520a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 203620a4e154SJeff Roberson *c = '_'; 203720a4e154SJeff Roberson 203820a4e154SJeff Roberson /* 203920a4e154SJeff Roberson * Basic parameters at the root. 204020a4e154SJeff Roberson */ 204120a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 204220a4e154SJeff Roberson OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, ""); 204320a4e154SJeff Roberson oid = zone->uz_oid; 204420a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 204520a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 20466d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 20476d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 20486d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 204920a4e154SJeff Roberson "Allocator configuration flags"); 205020a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 205120a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 205220a4e154SJeff Roberson "Desired per-cpu cache size"); 205320a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 205420a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 205520a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 205620a4e154SJeff Roberson 205720a4e154SJeff Roberson /* 205820a4e154SJeff Roberson * keg if present. 205920a4e154SJeff Roberson */ 206020a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 206120a4e154SJeff Roberson "keg", CTLFLAG_RD, NULL, ""); 206220a4e154SJeff Roberson keg = zone->uz_keg; 20633b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 206420a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 206520a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 206620a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 206720a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 206820a4e154SJeff Roberson "Real object size with alignment"); 206920a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 207020a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 207120a4e154SJeff Roberson "pages per-slab allocation"); 207220a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 207320a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 207420a4e154SJeff Roberson "items available per-slab"); 207520a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 207620a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 207720a4e154SJeff Roberson "item alignment mask"); 207820a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 207920a4e154SJeff Roberson "pages", CTLFLAG_RD, &keg->uk_pages, 0, 208020a4e154SJeff Roberson "Total pages currently allocated from VM"); 208120a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 208220a4e154SJeff Roberson "free", CTLFLAG_RD, &keg->uk_free, 0, 208320a4e154SJeff Roberson "items free in the slab layer"); 2084f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2085f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2086f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2087f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 208820a4e154SJeff Roberson } else 208920a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 209020a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 209120a4e154SJeff Roberson 209220a4e154SJeff Roberson /* 209320a4e154SJeff Roberson * Information about zone limits. 209420a4e154SJeff Roberson */ 209520a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 209620a4e154SJeff Roberson "limit", CTLFLAG_RD, NULL, ""); 2097*4bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2098*4bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 2099*4bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 2100*4bd61e19SJeff Roberson "current number of allocated items if limit is set"); 210120a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210220a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 210320a4e154SJeff Roberson "Maximum number of cached items"); 210420a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210520a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 210620a4e154SJeff Roberson "Number of threads sleeping at limit"); 210720a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210820a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 210920a4e154SJeff Roberson "Total zone limit sleeps"); 2110*4bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2111*4bd61e19SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bkt_max, 0, 2112*4bd61e19SJeff Roberson "Maximum number of items in the bucket cache"); 2113*4bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2114*4bd61e19SJeff Roberson "bucket_cnt", CTLFLAG_RD, &zone->uz_bkt_count, 0, 2115*4bd61e19SJeff Roberson "Number of items in the bucket cache"); 211620a4e154SJeff Roberson 211720a4e154SJeff Roberson /* 211820a4e154SJeff Roberson * Per-domain information. 211920a4e154SJeff Roberson */ 212020a4e154SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) 212120a4e154SJeff Roberson domains = vm_ndomains; 212220a4e154SJeff Roberson else 212320a4e154SJeff Roberson domains = 1; 212420a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 212520a4e154SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 212620a4e154SJeff Roberson for (i = 0; i < domains; i++) { 212720a4e154SJeff Roberson zdom = &zone->uz_domain[i]; 212820a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 212920a4e154SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, ""); 213020a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213120a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 213220a4e154SJeff Roberson "number of items in this domain"); 213320a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213420a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 213520a4e154SJeff Roberson "maximum item count in this period"); 213620a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213720a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 213820a4e154SJeff Roberson "minimum item count in this period"); 213920a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 214020a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 214120a4e154SJeff Roberson "Working set size"); 214220a4e154SJeff Roberson } 214320a4e154SJeff Roberson 214420a4e154SJeff Roberson /* 214520a4e154SJeff Roberson * General statistics. 214620a4e154SJeff Roberson */ 214720a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 214820a4e154SJeff Roberson "stats", CTLFLAG_RD, NULL, ""); 214920a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 215020a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 215120a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 215220a4e154SJeff Roberson "Current number of allocated items"); 215320a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 215420a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 215520a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 215620a4e154SJeff Roberson "Total allocation calls"); 215720a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 215820a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 215920a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 216020a4e154SJeff Roberson "Total free calls"); 216120a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 216220a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 216320a4e154SJeff Roberson "Number of allocation failures"); 216420a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 216520a4e154SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 0, 216620a4e154SJeff Roberson "Free calls from the wrong domain"); 216720a4e154SJeff Roberson } 216820a4e154SJeff Roberson 216920a4e154SJeff Roberson struct uma_zone_count { 217020a4e154SJeff Roberson const char *name; 217120a4e154SJeff Roberson int count; 217220a4e154SJeff Roberson }; 217320a4e154SJeff Roberson 217420a4e154SJeff Roberson static void 217520a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 217620a4e154SJeff Roberson { 217720a4e154SJeff Roberson struct uma_zone_count *cnt; 217820a4e154SJeff Roberson 217920a4e154SJeff Roberson cnt = arg; 21803b490537SJeff Roberson /* 21813b490537SJeff Roberson * Some zones are rapidly created with identical names and 21823b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 21833b490537SJeff Roberson * Use one greater than the maximum observed for this name. 21843b490537SJeff Roberson */ 218520a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 21863b490537SJeff Roberson cnt->count = MAX(cnt->count, 21873b490537SJeff Roberson zone->uz_namecnt + 1); 218820a4e154SJeff Roberson } 218920a4e154SJeff Roberson 2190cc7ce83aSJeff Roberson static void 2191cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2192cc7ce83aSJeff Roberson { 2193cc7ce83aSJeff Roberson int i; 2194cc7ce83aSJeff Roberson 2195cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2196cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2197cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2198cc7ce83aSJeff Roberson } 2199cc7ce83aSJeff Roberson } 2200cc7ce83aSJeff Roberson 2201099a0e58SBosko Milekic /* 2202099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2203099a0e58SBosko Milekic * 2204099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2205099a0e58SBosko Milekic * udata Actually uma_zctor_args 22068355f576SJeff Roberson */ 2207b23f72e9SBrian Feldman static int 2208b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 22098355f576SJeff Roberson { 221020a4e154SJeff Roberson struct uma_zone_count cnt; 22118355f576SJeff Roberson struct uma_zctor_args *arg = udata; 22128355f576SJeff Roberson uma_zone_t zone = mem; 2213099a0e58SBosko Milekic uma_zone_t z; 2214099a0e58SBosko Milekic uma_keg_t keg; 221508cfa56eSMark Johnston int i; 22168355f576SJeff Roberson 22178355f576SJeff Roberson bzero(zone, size); 22188355f576SJeff Roberson zone->uz_name = arg->name; 22198355f576SJeff Roberson zone->uz_ctor = arg->ctor; 22208355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2221099a0e58SBosko Milekic zone->uz_init = NULL; 2222099a0e58SBosko Milekic zone->uz_fini = NULL; 2223bf965959SSean Bruno zone->uz_sleeps = 0; 2224c1685086SJeff Roberson zone->uz_xdomain = 0; 222520a4e154SJeff Roberson zone->uz_bucket_size = 0; 222620a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 222720a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2228e20a199fSJeff Roberson zone->uz_flags = 0; 22292f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2230ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 2231ab3185d1SJeff Roberson zone->uz_domain = (struct uma_zone_domain *)&zone->uz_cpu[mp_ncpus]; 2232bb15d1c7SGleb Smirnoff zone->uz_bkt_max = ULONG_MAX; 22332f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2234af526374SJeff Roberson 223520a4e154SJeff Roberson /* Count the number of duplicate names. */ 223620a4e154SJeff Roberson cnt.name = arg->name; 223720a4e154SJeff Roberson cnt.count = 0; 223820a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 223920a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 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) { 22536fd34d6fSJeff Roberson if (arg->flags & UMA_ZONE_VM) 22546fd34d6fSJeff Roberson arg->flags |= UMA_ZFLAG_CACHEONLY; 22556fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2256af526374SJeff Roberson zone->uz_size = arg->size; 22570095a784SJeff Roberson zone->uz_import = arg->import; 22580095a784SJeff Roberson zone->uz_release = arg->release; 22590095a784SJeff Roberson zone->uz_arg = arg->arg; 2260af526374SJeff Roberson zone->uz_lockptr = &zone->uz_lock; 2261bb15d1c7SGleb Smirnoff ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 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; 2282af526374SJeff Roberson zone->uz_lockptr = &keg->uk_lock; 2283e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2284111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2285099a0e58SBosko Milekic ZONE_LOCK(zone); 2286099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2287099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2288099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2289099a0e58SBosko Milekic break; 2290099a0e58SBosko Milekic } 2291099a0e58SBosko Milekic } 2292099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2293111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2294e20a199fSJeff Roberson } else if (keg == NULL) { 2295e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2296e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2297b23f72e9SBrian Feldman return (ENOMEM); 2298099a0e58SBosko Milekic } else { 2299099a0e58SBosko Milekic struct uma_kctor_args karg; 2300b23f72e9SBrian Feldman int error; 2301099a0e58SBosko Milekic 2302099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2303099a0e58SBosko Milekic karg.size = arg->size; 2304099a0e58SBosko Milekic karg.uminit = arg->uminit; 2305099a0e58SBosko Milekic karg.fini = arg->fini; 2306099a0e58SBosko Milekic karg.align = arg->align; 2307099a0e58SBosko Milekic karg.flags = arg->flags; 2308099a0e58SBosko Milekic karg.zone = zone; 2309b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2310b23f72e9SBrian Feldman flags); 2311b23f72e9SBrian Feldman if (error) 2312b23f72e9SBrian Feldman return (error); 2313099a0e58SBosko Milekic } 23140095a784SJeff Roberson 231520a4e154SJeff Roberson /* Inherit properties from the keg. */ 2316bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2317e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2318e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2319e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 23208355f576SJeff Roberson 232120a4e154SJeff Roberson out: 232220a4e154SJeff Roberson if (__predict_true(booted == BOOT_RUNNING)) { 232320a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 232420a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 232520a4e154SJeff Roberson } else { 232620a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 232720a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 232820a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2329099a0e58SBosko Milekic } 23308355f576SJeff Roberson 23317e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 23327e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 23337e28037aSMark Johnston ("Invalid zone flag combination")); 233420a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 233520a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 233620a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 233720a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 233820a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) 233920a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN; 234020a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 234120a4e154SJeff Roberson zone->uz_bucket_size = 0; 23427e28037aSMark Johnston else 234320a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 234420a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2345cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2346cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2347cc7ce83aSJeff Roberson zone_update_caches(zone); 2348fc03d22bSJeff Roberson 2349b23f72e9SBrian Feldman return (0); 23508355f576SJeff Roberson } 23518355f576SJeff Roberson 23528355f576SJeff Roberson /* 2353099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2354099a0e58SBosko Milekic * table and removes the keg from the global list. 23559c2cd7e5SJeff Roberson * 23569c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 23579c2cd7e5SJeff Roberson * udata unused 23589c2cd7e5SJeff Roberson */ 2359099a0e58SBosko Milekic static void 2360099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2361099a0e58SBosko Milekic { 2362099a0e58SBosko Milekic uma_keg_t keg; 23639c2cd7e5SJeff Roberson 2364099a0e58SBosko Milekic keg = (uma_keg_t)arg; 2365e20a199fSJeff Roberson KEG_LOCK(keg); 2366099a0e58SBosko Milekic if (keg->uk_free != 0) { 2367a3845534SCraig Rodrigues printf("Freed UMA keg (%s) was not empty (%d items). " 2368099a0e58SBosko Milekic " Lost %d pages of memory.\n", 2369a3845534SCraig Rodrigues keg->uk_name ? keg->uk_name : "", 2370099a0e58SBosko Milekic keg->uk_free, keg->uk_pages); 2371099a0e58SBosko Milekic } 2372e20a199fSJeff Roberson KEG_UNLOCK(keg); 2373099a0e58SBosko Milekic 2374099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2375099a0e58SBosko Milekic 2376e20a199fSJeff Roberson KEG_LOCK_FINI(keg); 2377099a0e58SBosko Milekic } 2378099a0e58SBosko Milekic 2379099a0e58SBosko Milekic /* 2380099a0e58SBosko Milekic * Zone header dtor. 2381099a0e58SBosko Milekic * 2382099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2383099a0e58SBosko Milekic * udata unused 2384099a0e58SBosko Milekic */ 23859c2cd7e5SJeff Roberson static void 23869c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 23879c2cd7e5SJeff Roberson { 23889c2cd7e5SJeff Roberson uma_zone_t zone; 2389099a0e58SBosko Milekic uma_keg_t keg; 23909c2cd7e5SJeff Roberson 23919c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 23929643769aSJeff Roberson 239320a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 239420a4e154SJeff Roberson 2395e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 23969643769aSJeff Roberson cache_drain(zone); 2397099a0e58SBosko Milekic 2398111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2399099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2400111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2401099a0e58SBosko Milekic /* 2402099a0e58SBosko Milekic * XXX there are some races here where 2403099a0e58SBosko Milekic * the zone can be drained but zone lock 2404099a0e58SBosko Milekic * released and then refilled before we 2405099a0e58SBosko Milekic * remove it... we dont care for now 2406099a0e58SBosko Milekic */ 240708cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2408e20a199fSJeff Roberson /* 2409323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2410e20a199fSJeff Roberson */ 2411323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2412323ad386STycho Nightingale keg = zone->uz_keg; 2413111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2414099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2415111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 24160095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 24179c2cd7e5SJeff Roberson } 24182efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 24192efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 24202efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 242120a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2422bb15d1c7SGleb Smirnoff if (zone->uz_lockptr == &zone->uz_lock) 2423af526374SJeff Roberson ZONE_LOCK_FINI(zone); 2424099a0e58SBosko Milekic } 2425099a0e58SBosko Milekic 24269c2cd7e5SJeff Roberson /* 24278355f576SJeff Roberson * Traverses every zone in the system and calls a callback 24288355f576SJeff Roberson * 24298355f576SJeff Roberson * Arguments: 24308355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 24318355f576SJeff Roberson * as an argument. 24328355f576SJeff Roberson * 24338355f576SJeff Roberson * Returns: 24348355f576SJeff Roberson * Nothing 24358355f576SJeff Roberson */ 24368355f576SJeff Roberson static void 243720a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 24388355f576SJeff Roberson { 2439099a0e58SBosko Milekic uma_keg_t keg; 24408355f576SJeff Roberson uma_zone_t zone; 24418355f576SJeff Roberson 24422efcc8cbSGleb Smirnoff /* 24432efcc8cbSGleb Smirnoff * Before BOOT_RUNNING we are guaranteed to be single 24442efcc8cbSGleb Smirnoff * threaded, so locking isn't needed. Startup functions 24452efcc8cbSGleb Smirnoff * are allowed to use M_WAITOK. 24462efcc8cbSGleb Smirnoff */ 24472efcc8cbSGleb Smirnoff if (__predict_true(booted == BOOT_RUNNING)) 2448111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2449099a0e58SBosko Milekic LIST_FOREACH(keg, &uma_kegs, uk_link) { 2450099a0e58SBosko Milekic LIST_FOREACH(zone, &keg->uk_zones, uz_link) 245120a4e154SJeff Roberson zfunc(zone, arg); 2452099a0e58SBosko Milekic } 245308034d10SKonstantin Belousov LIST_FOREACH(zone, &uma_cachezones, uz_link) 245420a4e154SJeff Roberson zfunc(zone, arg); 24552efcc8cbSGleb Smirnoff if (__predict_true(booted == BOOT_RUNNING)) 2456111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 24578355f576SJeff Roberson } 24588355f576SJeff Roberson 2459f4bef67cSGleb Smirnoff /* 2460f4bef67cSGleb Smirnoff * Count how many pages do we need to bootstrap. VM supplies 2461f4bef67cSGleb Smirnoff * its need in early zones in the argument, we add up our zones, 2462325c4cedSMark Johnston * which consist of the UMA Slabs, UMA Hash and 9 Bucket zones. The 2463f4bef67cSGleb Smirnoff * zone of zones and zone of kegs are accounted separately. 2464f4bef67cSGleb Smirnoff */ 2465325c4cedSMark Johnston #define UMA_BOOT_ZONES 11 24665073a083SGleb Smirnoff /* Zone of zones and zone of kegs have arbitrary alignment. */ 24675073a083SGleb Smirnoff #define UMA_BOOT_ALIGN 32 2468f4bef67cSGleb Smirnoff static int zsize, ksize; 2469f4bef67cSGleb Smirnoff int 2470f7d35785SGleb Smirnoff uma_startup_count(int vm_zones) 2471f4bef67cSGleb Smirnoff { 2472f7d35785SGleb Smirnoff int zones, pages; 24739b78b1f4SJeff Roberson size_t space, size; 2474f4bef67cSGleb Smirnoff 2475f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2476f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 2477f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2478f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2479f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 2480f4bef67cSGleb Smirnoff 24815073a083SGleb Smirnoff /* 24825073a083SGleb Smirnoff * Memory for the zone of kegs and its keg, 24835073a083SGleb Smirnoff * and for zone of zones. 24845073a083SGleb Smirnoff */ 2485f4bef67cSGleb Smirnoff pages = howmany(roundup(zsize, CACHE_LINE_SIZE) * 2 + 2486f4bef67cSGleb Smirnoff roundup(ksize, CACHE_LINE_SIZE), PAGE_SIZE); 2487f4bef67cSGleb Smirnoff 2488f7d35785SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2489f7d35785SGleb Smirnoff zones = UMA_BOOT_ZONES; 2490f7d35785SGleb Smirnoff #else 2491f7d35785SGleb Smirnoff zones = UMA_BOOT_ZONES + vm_zones; 2492f7d35785SGleb Smirnoff vm_zones = 0; 2493f7d35785SGleb Smirnoff #endif 24949b78b1f4SJeff Roberson size = slab_sizeof(SLAB_MAX_SETSIZE); 24959b78b1f4SJeff Roberson space = slab_space(SLAB_MAX_SETSIZE); 2496f4bef67cSGleb Smirnoff 24975073a083SGleb Smirnoff /* Memory for the rest of startup zones, UMA and VM, ... */ 24989b78b1f4SJeff Roberson if (zsize > space) { 24990b2e3aeaSGleb Smirnoff /* See keg_large_init(). */ 25000b2e3aeaSGleb Smirnoff u_int ppera; 25010b2e3aeaSGleb Smirnoff 25020b2e3aeaSGleb Smirnoff ppera = howmany(roundup2(zsize, UMA_BOOT_ALIGN), PAGE_SIZE); 25039b78b1f4SJeff Roberson if (PAGE_SIZE * ppera - roundup2(zsize, UMA_BOOT_ALIGN) < size) 25040b2e3aeaSGleb Smirnoff ppera++; 25050b2e3aeaSGleb Smirnoff pages += (zones + vm_zones) * ppera; 25069b78b1f4SJeff Roberson } else if (roundup2(zsize, UMA_BOOT_ALIGN) > space) 25070b2e3aeaSGleb Smirnoff /* See keg_small_init() special case for uk_ppera = 1. */ 250896a10340SGleb Smirnoff pages += zones; 2509f4bef67cSGleb Smirnoff else 25105073a083SGleb Smirnoff pages += howmany(zones, 25119b78b1f4SJeff Roberson space / roundup2(zsize, UMA_BOOT_ALIGN)); 2512f4bef67cSGleb Smirnoff 25135073a083SGleb Smirnoff /* ... and their kegs. Note that zone of zones allocates a keg! */ 25145073a083SGleb Smirnoff pages += howmany(zones + 1, 25159b78b1f4SJeff Roberson space / roundup2(ksize, UMA_BOOT_ALIGN)); 2516f4bef67cSGleb Smirnoff 2517f4bef67cSGleb Smirnoff return (pages); 2518f4bef67cSGleb Smirnoff } 2519f4bef67cSGleb Smirnoff 25208355f576SJeff Roberson void 2521ac0a6fd0SGleb Smirnoff uma_startup(void *mem, int npages) 25228355f576SJeff Roberson { 25238355f576SJeff Roberson struct uma_zctor_args args; 2524ab3185d1SJeff Roberson uma_keg_t masterkeg; 2525ab3185d1SJeff Roberson uintptr_t m; 2526f4bef67cSGleb Smirnoff 2527f4bef67cSGleb Smirnoff #ifdef DIAGNOSTIC 2528f4bef67cSGleb Smirnoff printf("Entering %s with %d boot pages configured\n", __func__, npages); 2529f4bef67cSGleb Smirnoff #endif 25308355f576SJeff Roberson 2531111fbcd5SBryan Venteicher rw_init(&uma_rwlock, "UMA lock"); 2532099a0e58SBosko Milekic 2533ab3185d1SJeff Roberson /* Use bootpages memory for the zone of zones and zone of kegs. */ 2534ab3185d1SJeff Roberson m = (uintptr_t)mem; 2535ab3185d1SJeff Roberson zones = (uma_zone_t)m; 2536ab3185d1SJeff Roberson m += roundup(zsize, CACHE_LINE_SIZE); 2537ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 2538ab3185d1SJeff Roberson m += roundup(zsize, CACHE_LINE_SIZE); 2539ab3185d1SJeff Roberson masterkeg = (uma_keg_t)m; 2540ab3185d1SJeff Roberson m += roundup(ksize, CACHE_LINE_SIZE); 2541ab3185d1SJeff Roberson m = roundup(m, PAGE_SIZE); 2542ab3185d1SJeff Roberson npages -= (m - (uintptr_t)mem) / PAGE_SIZE; 2543ab3185d1SJeff Roberson mem = (void *)m; 2544ab3185d1SJeff Roberson 2545099a0e58SBosko Milekic /* "manually" create the initial zone */ 25460095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2547099a0e58SBosko Milekic args.name = "UMA Kegs"; 2548ab3185d1SJeff Roberson args.size = ksize; 2549099a0e58SBosko Milekic args.ctor = keg_ctor; 2550099a0e58SBosko Milekic args.dtor = keg_dtor; 25518355f576SJeff Roberson args.uminit = zero_init; 25528355f576SJeff Roberson args.fini = NULL; 2553ab3185d1SJeff Roberson args.keg = masterkeg; 25545073a083SGleb Smirnoff args.align = UMA_BOOT_ALIGN - 1; 2555b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2556ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 25578355f576SJeff Roberson 2558ac0a6fd0SGleb Smirnoff bootmem = mem; 2559ac0a6fd0SGleb Smirnoff boot_pages = npages; 25608355f576SJeff Roberson 2561099a0e58SBosko Milekic args.name = "UMA Zones"; 2562f4bef67cSGleb Smirnoff args.size = zsize; 2563099a0e58SBosko Milekic args.ctor = zone_ctor; 2564099a0e58SBosko Milekic args.dtor = zone_dtor; 2565099a0e58SBosko Milekic args.uminit = zero_init; 2566099a0e58SBosko Milekic args.fini = NULL; 2567099a0e58SBosko Milekic args.keg = NULL; 25685073a083SGleb Smirnoff args.align = UMA_BOOT_ALIGN - 1; 2569099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2570ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2571099a0e58SBosko Milekic 25728355f576SJeff Roberson /* Now make a zone for slab headers */ 25731e0701e1SJeff Roberson slabzone = uma_zcreate("UMA Slabs", sizeof(struct uma_hash_slab), 25741e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 25758355f576SJeff Roberson 25768355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 25778355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 25781e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 25798355f576SJeff Roberson 2580f4bef67cSGleb Smirnoff booted = BOOT_STRAPPED; 25818355f576SJeff Roberson } 25828355f576SJeff Roberson 2583f4bef67cSGleb Smirnoff void 2584f4bef67cSGleb Smirnoff uma_startup1(void) 2585f4bef67cSGleb Smirnoff { 2586f4bef67cSGleb Smirnoff 2587f4bef67cSGleb Smirnoff #ifdef DIAGNOSTIC 2588f4bef67cSGleb Smirnoff printf("Entering %s with %d boot pages left\n", __func__, boot_pages); 2589f4bef67cSGleb Smirnoff #endif 2590f4bef67cSGleb Smirnoff booted = BOOT_PAGEALLOC; 2591f4bef67cSGleb Smirnoff } 2592f4bef67cSGleb Smirnoff 25938355f576SJeff Roberson void 259499571dc3SJeff Roberson uma_startup2(void) 25958355f576SJeff Roberson { 2596f4bef67cSGleb Smirnoff 2597f7d35785SGleb Smirnoff #ifdef DIAGNOSTIC 2598f7d35785SGleb Smirnoff printf("Entering %s with %d boot pages left\n", __func__, boot_pages); 2599f7d35785SGleb Smirnoff #endif 260008cfa56eSMark Johnston sx_init(&uma_reclaim_lock, "umareclaim"); 26013182660aSRyan Libby bucket_init(); 26023182660aSRyan Libby booted = BOOT_BUCKETS; 2603f4bef67cSGleb Smirnoff bucket_enable(); 26048355f576SJeff Roberson } 26058355f576SJeff Roberson 26068355f576SJeff Roberson /* 26078355f576SJeff Roberson * Initialize our callout handle 26088355f576SJeff Roberson * 26098355f576SJeff Roberson */ 26108355f576SJeff Roberson static void 26118355f576SJeff Roberson uma_startup3(void) 26128355f576SJeff Roberson { 26131431a748SGleb Smirnoff 2614c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2615c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2616c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2617c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2618c5deaf04SGleb Smirnoff #endif 261920a4e154SJeff Roberson zone_foreach(zone_alloc_counters, NULL); 262020a4e154SJeff Roberson zone_foreach(zone_alloc_sysctl, NULL); 2621fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 26229643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2623c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 26248355f576SJeff Roberson } 26258355f576SJeff Roberson 2626e20a199fSJeff Roberson static uma_keg_t 2627099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 262885dcf349SGleb Smirnoff int align, uint32_t flags) 2629099a0e58SBosko Milekic { 2630099a0e58SBosko Milekic struct uma_kctor_args args; 2631099a0e58SBosko Milekic 2632099a0e58SBosko Milekic args.size = size; 2633099a0e58SBosko Milekic args.uminit = uminit; 2634099a0e58SBosko Milekic args.fini = fini; 26351e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2636099a0e58SBosko Milekic args.flags = flags; 2637099a0e58SBosko Milekic args.zone = zone; 2638ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 2639099a0e58SBosko Milekic } 2640099a0e58SBosko Milekic 2641f4bef67cSGleb Smirnoff /* Public functions */ 26428355f576SJeff Roberson /* See uma.h */ 26431e319f6dSRobert Watson void 26441e319f6dSRobert Watson uma_set_align(int align) 26451e319f6dSRobert Watson { 26461e319f6dSRobert Watson 26471e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 26481e319f6dSRobert Watson uma_align_cache = align; 26491e319f6dSRobert Watson } 26501e319f6dSRobert Watson 26511e319f6dSRobert Watson /* See uma.h */ 26528355f576SJeff Roberson uma_zone_t 2653bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 265485dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 26558355f576SJeff Roberson 26568355f576SJeff Roberson { 26578355f576SJeff Roberson struct uma_zctor_args args; 265895c4bf75SKonstantin Belousov uma_zone_t res; 265995c4bf75SKonstantin Belousov bool locked; 26608355f576SJeff Roberson 2661a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 2662a5a35578SJohn Baldwin align, name)); 2663a5a35578SJohn Baldwin 2664c1685086SJeff Roberson /* Sets all zones to a first-touch domain policy. */ 2665c1685086SJeff Roberson #ifdef UMA_FIRSTTOUCH 2666c1685086SJeff Roberson flags |= UMA_ZONE_NUMA; 2667c1685086SJeff Roberson #endif 2668c1685086SJeff Roberson 26698355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 26700095a784SJeff Roberson memset(&args, 0, sizeof(args)); 26718355f576SJeff Roberson args.name = name; 26728355f576SJeff Roberson args.size = size; 26738355f576SJeff Roberson args.ctor = ctor; 26748355f576SJeff Roberson args.dtor = dtor; 26758355f576SJeff Roberson args.uminit = uminit; 26768355f576SJeff Roberson args.fini = fini; 2677afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 2678afc6dc36SJohn-Mark Gurney /* 2679ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 2680ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 2681ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 2682ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 2683ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 2684ca293436SRyan Libby * the ctor and dtor do not need to be empty. 2685ca293436SRyan Libby * 2686ca293436SRyan Libby * XXX UMA_ZONE_OFFPAGE. 2687afc6dc36SJohn-Mark Gurney */ 268819c591bfSMateusz Guzik if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOFREE))) && 2689ca293436SRyan Libby uminit == NULL && fini == NULL) { 2690afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 2691afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 2692afc6dc36SJohn-Mark Gurney } 2693afc6dc36SJohn-Mark Gurney #endif 26948355f576SJeff Roberson args.align = align; 26958355f576SJeff Roberson args.flags = flags; 2696099a0e58SBosko Milekic args.keg = NULL; 2697099a0e58SBosko Milekic 2698f4bef67cSGleb Smirnoff if (booted < BOOT_BUCKETS) { 269995c4bf75SKonstantin Belousov locked = false; 270095c4bf75SKonstantin Belousov } else { 270108cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 270295c4bf75SKonstantin Belousov locked = true; 270395c4bf75SKonstantin Belousov } 2704ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 270595c4bf75SKonstantin Belousov if (locked) 270608cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 270795c4bf75SKonstantin Belousov return (res); 2708099a0e58SBosko Milekic } 2709099a0e58SBosko Milekic 2710099a0e58SBosko Milekic /* See uma.h */ 2711099a0e58SBosko Milekic uma_zone_t 2712099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 2713099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 2714099a0e58SBosko Milekic { 2715099a0e58SBosko Milekic struct uma_zctor_args args; 2716e20a199fSJeff Roberson uma_keg_t keg; 271795c4bf75SKonstantin Belousov uma_zone_t res; 271895c4bf75SKonstantin Belousov bool locked; 2719099a0e58SBosko Milekic 2720bb15d1c7SGleb Smirnoff keg = master->uz_keg; 27210095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2722099a0e58SBosko Milekic args.name = name; 2723e20a199fSJeff Roberson args.size = keg->uk_size; 2724099a0e58SBosko Milekic args.ctor = ctor; 2725099a0e58SBosko Milekic args.dtor = dtor; 2726099a0e58SBosko Milekic args.uminit = zinit; 2727099a0e58SBosko Milekic args.fini = zfini; 2728e20a199fSJeff Roberson args.align = keg->uk_align; 2729e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 2730e20a199fSJeff Roberson args.keg = keg; 27318355f576SJeff Roberson 2732f4bef67cSGleb Smirnoff if (booted < BOOT_BUCKETS) { 273395c4bf75SKonstantin Belousov locked = false; 273495c4bf75SKonstantin Belousov } else { 273508cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 273695c4bf75SKonstantin Belousov locked = true; 273795c4bf75SKonstantin Belousov } 2738e20a199fSJeff Roberson /* XXX Attaches only one keg of potentially many. */ 2739ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 274095c4bf75SKonstantin Belousov if (locked) 274108cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 274295c4bf75SKonstantin Belousov return (res); 27438355f576SJeff Roberson } 27448355f576SJeff Roberson 27450095a784SJeff Roberson /* See uma.h */ 27460095a784SJeff Roberson uma_zone_t 2747af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 2748af526374SJeff Roberson uma_init zinit, uma_fini zfini, uma_import zimport, 2749af526374SJeff Roberson uma_release zrelease, void *arg, int flags) 27500095a784SJeff Roberson { 27510095a784SJeff Roberson struct uma_zctor_args args; 27520095a784SJeff Roberson 27530095a784SJeff Roberson memset(&args, 0, sizeof(args)); 27540095a784SJeff Roberson args.name = name; 2755af526374SJeff Roberson args.size = size; 27560095a784SJeff Roberson args.ctor = ctor; 27570095a784SJeff Roberson args.dtor = dtor; 27580095a784SJeff Roberson args.uminit = zinit; 27590095a784SJeff Roberson args.fini = zfini; 27600095a784SJeff Roberson args.import = zimport; 27610095a784SJeff Roberson args.release = zrelease; 27620095a784SJeff Roberson args.arg = arg; 27630095a784SJeff Roberson args.align = 0; 2764bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 27650095a784SJeff Roberson 2766ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 27670095a784SJeff Roberson } 27680095a784SJeff Roberson 27698355f576SJeff Roberson /* See uma.h */ 27709c2cd7e5SJeff Roberson void 27719c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 27729c2cd7e5SJeff Roberson { 2773f4ff923bSRobert Watson 277408cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 27750095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 277608cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 27779c2cd7e5SJeff Roberson } 27789c2cd7e5SJeff Roberson 27798d6fbbb8SJeff Roberson void 27808d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 27818d6fbbb8SJeff Roberson { 27828d6fbbb8SJeff Roberson void *item; 27838d6fbbb8SJeff Roberson 27848d6fbbb8SJeff Roberson item = uma_zalloc_arg(zone, NULL, M_WAITOK); 27858d6fbbb8SJeff Roberson uma_zfree(zone, item); 27868d6fbbb8SJeff Roberson } 27878d6fbbb8SJeff Roberson 27884e180881SMateusz Guzik void * 27894e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 27904e180881SMateusz Guzik { 27914e180881SMateusz Guzik void *item; 2792b4799947SRuslan Bukin #ifdef SMP 27934e180881SMateusz Guzik int i; 27944e180881SMateusz Guzik 27954e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2796b4799947SRuslan Bukin #endif 27974e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 27984e180881SMateusz Guzik if (item != NULL && (flags & M_ZERO)) { 2799b4799947SRuslan Bukin #ifdef SMP 2800013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 28014e180881SMateusz Guzik bzero(zpcpu_get_cpu(item, i), zone->uz_size); 2802b4799947SRuslan Bukin #else 2803b4799947SRuslan Bukin bzero(item, zone->uz_size); 2804b4799947SRuslan Bukin #endif 28054e180881SMateusz Guzik } 28064e180881SMateusz Guzik return (item); 28074e180881SMateusz Guzik } 28084e180881SMateusz Guzik 28094e180881SMateusz Guzik /* 28104e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 28114e180881SMateusz Guzik */ 28124e180881SMateusz Guzik void 28134e180881SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata) 28144e180881SMateusz Guzik { 28154e180881SMateusz Guzik 2816c5b7751fSIan Lepore #ifdef SMP 28174e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2818c5b7751fSIan Lepore #endif 28194e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 28204e180881SMateusz Guzik } 28214e180881SMateusz Guzik 2822cc7ce83aSJeff Roberson #ifdef INVARIANTS 2823cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 1 2824cc7ce83aSJeff Roberson #else 2825cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 0 2826cc7ce83aSJeff Roberson #endif 2827cc7ce83aSJeff Roberson 2828beb8beefSJeff Roberson static void * 2829cc7ce83aSJeff Roberson item_ctor(uma_zone_t zone, int size, void *udata, int flags, void *item) 2830beb8beefSJeff Roberson { 2831beb8beefSJeff Roberson #ifdef INVARIANTS 2832ca293436SRyan Libby bool skipdbg; 2833beb8beefSJeff Roberson 2834beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 2835ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2836ca293436SRyan Libby zone->uz_ctor != trash_ctor) 2837cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 2838beb8beefSJeff Roberson #endif 2839ca293436SRyan Libby if (__predict_false(zone->uz_ctor != NULL) && 2840cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 2841beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 2842beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 2843beb8beefSJeff Roberson return (NULL); 2844beb8beefSJeff Roberson } 2845beb8beefSJeff Roberson #ifdef INVARIANTS 2846beb8beefSJeff Roberson if (!skipdbg) 2847beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 2848beb8beefSJeff Roberson #endif 2849beb8beefSJeff Roberson if (flags & M_ZERO) 2850cc7ce83aSJeff Roberson bzero(item, size); 2851beb8beefSJeff Roberson 2852beb8beefSJeff Roberson return (item); 2853beb8beefSJeff Roberson } 2854beb8beefSJeff Roberson 2855ca293436SRyan Libby static inline void 2856cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 2857cc7ce83aSJeff Roberson enum zfreeskip skip) 2858ca293436SRyan Libby { 2859ca293436SRyan Libby #ifdef INVARIANTS 2860ca293436SRyan Libby bool skipdbg; 2861ca293436SRyan Libby 2862ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 2863ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 2864ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 2865ca293436SRyan Libby uma_dbg_free(zone, udata, item); 2866ca293436SRyan Libby else 2867ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 2868ca293436SRyan Libby } 2869ca293436SRyan Libby #endif 2870cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 2871ca293436SRyan Libby if (zone->uz_dtor != NULL) 2872cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 2873ca293436SRyan Libby #ifdef INVARIANTS 2874ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2875ca293436SRyan Libby zone->uz_dtor != trash_dtor) 2876cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 2877ca293436SRyan Libby #endif 2878ca293436SRyan Libby } 2879ca293436SRyan Libby } 2880ca293436SRyan Libby 28819c2cd7e5SJeff Roberson /* See uma.h */ 28828355f576SJeff Roberson void * 28832cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 28848355f576SJeff Roberson { 2885376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 2886ab3185d1SJeff Roberson uma_cache_t cache; 2887ab3185d1SJeff Roberson void *item; 2888cc7ce83aSJeff Roberson int domain, size, uz_flags; 28898355f576SJeff Roberson 2890e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 289119fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 289210cb2424SMark Murray 28938355f576SJeff Roberson /* This is the fast path allocation */ 28941431a748SGleb Smirnoff CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d", 28951431a748SGleb Smirnoff curthread, zone->uz_name, zone, flags); 2896a553d4b8SJeff Roberson 2897cc7ce83aSJeff Roberson #ifdef WITNESS 2898635fd505SRobert Watson if (flags & M_WAITOK) { 2899b23f72e9SBrian Feldman WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2900635fd505SRobert Watson "uma_zalloc_arg: zone \"%s\"", zone->uz_name); 29014c1cc01cSJohn Baldwin } 2902cc7ce83aSJeff Roberson #endif 2903cc7ce83aSJeff Roberson 2904cc7ce83aSJeff Roberson #ifdef INVARIANTS 29050766f278SJonathan T. Looney KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC")); 2906d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 29071067a2baSJonathan T. Looney ("uma_zalloc_arg: called with spinlock or critical section held")); 2908ea99223eSMateusz Guzik if (zone->uz_flags & UMA_ZONE_PCPU) 2909b8af2820SMateusz Guzik KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone " 2910b8af2820SMateusz Guzik "with M_ZERO passed")); 2911cc7ce83aSJeff Roberson #endif 29121067a2baSJonathan T. Looney 29138d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 29148d689e04SGleb Smirnoff if (memguard_cmp_zone(zone)) { 29158d689e04SGleb Smirnoff item = memguard_alloc(zone->uz_size, flags); 29168d689e04SGleb Smirnoff if (item != NULL) { 29178d689e04SGleb Smirnoff if (zone->uz_init != NULL && 29188d689e04SGleb Smirnoff zone->uz_init(item, zone->uz_size, flags) != 0) 29198d689e04SGleb Smirnoff return (NULL); 29208d689e04SGleb Smirnoff if (zone->uz_ctor != NULL && 2921fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 2922fc03d22bSJeff Roberson flags) != 0) { 2923ca293436SRyan Libby counter_u64_add(zone->uz_fails, 1); 29248d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 29258d689e04SGleb Smirnoff return (NULL); 29268d689e04SGleb Smirnoff } 29278d689e04SGleb Smirnoff return (item); 29288d689e04SGleb Smirnoff } 29298d689e04SGleb Smirnoff /* This is unfortunate but should not be fatal. */ 29308d689e04SGleb Smirnoff } 29318d689e04SGleb Smirnoff #endif 29325d1ae027SRobert Watson /* 29335d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 29345d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 29355d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 29365d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 29375d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 29385d1ae027SRobert Watson * preemption and migration. We release the critical section in 29395d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 29405d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 29415d1ae027SRobert Watson * must detect and handle migration if it has occurred. 29425d1ae027SRobert Watson */ 29435d1ae027SRobert Watson critical_enter(); 2944beb8beefSJeff Roberson do { 2945cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 2946376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 2947cc7ce83aSJeff Roberson size = cache_uz_size(cache); 2948cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 2949376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 2950376b1ba3SJeff Roberson item = cache_bucket_pop(cache, bucket); 29515d1ae027SRobert Watson critical_exit(); 2952cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 2953cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 2954cc7ce83aSJeff Roberson return (item_ctor(zone, size, udata, flags, item)); 2955cc7ce83aSJeff Roberson if (flags & M_ZERO) 2956cc7ce83aSJeff Roberson bzero(item, size); 2957cc7ce83aSJeff Roberson return (item); 2958b23f72e9SBrian Feldman } 2959beb8beefSJeff Roberson } while (cache_alloc(zone, cache, udata, flags)); 2960beb8beefSJeff Roberson critical_exit(); 2961beb8beefSJeff Roberson 2962beb8beefSJeff Roberson /* 2963beb8beefSJeff Roberson * We can not get a bucket so try to return a single item. 2964beb8beefSJeff Roberson */ 2965cc7ce83aSJeff Roberson if (uz_flags & UMA_ZONE_NUMA) 2966beb8beefSJeff Roberson domain = PCPU_GET(domain); 2967beb8beefSJeff Roberson else 2968beb8beefSJeff Roberson domain = UMA_ANYDOMAIN; 2969*4bd61e19SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 2970fc03d22bSJeff Roberson } 2971fc03d22bSJeff Roberson 29728355f576SJeff Roberson /* 2973beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 2974beb8beefSJeff Roberson * a critical section. Returns in a critical section. 2975beb8beefSJeff Roberson * 2976*4bd61e19SJeff Roberson * A false return value indicates an allocation failure. 2977*4bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 2978beb8beefSJeff Roberson */ 2979beb8beefSJeff Roberson static __noinline bool 2980beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 2981beb8beefSJeff Roberson { 2982beb8beefSJeff Roberson uma_zone_domain_t zdom; 2983beb8beefSJeff Roberson uma_bucket_t bucket; 2984cc7ce83aSJeff Roberson int domain; 2985beb8beefSJeff Roberson bool lockfail; 2986beb8beefSJeff Roberson 2987beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 2988beb8beefSJeff Roberson 2989beb8beefSJeff Roberson /* 2990beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 2991beb8beefSJeff Roberson * if we can switch with the free bucket. 29928355f576SJeff Roberson */ 2993376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_cnt != 0) { 2994376b1ba3SJeff Roberson cache_bucket_swap(&cache->uc_freebucket, &cache->uc_allocbucket); 2995beb8beefSJeff Roberson return (true); 29968355f576SJeff Roberson } 2997fc03d22bSJeff Roberson 2998fc03d22bSJeff Roberson /* 2999fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3000fc03d22bSJeff Roberson */ 3001376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3002fc03d22bSJeff Roberson critical_exit(); 3003fc03d22bSJeff Roberson if (bucket != NULL) 30046fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3005fc03d22bSJeff Roberson 3006*4bd61e19SJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 3007*4bd61e19SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) { 3008*4bd61e19SJeff Roberson critical_enter(); 3009*4bd61e19SJeff Roberson return (false); 3010*4bd61e19SJeff Roberson } 3011*4bd61e19SJeff Roberson 30125d1ae027SRobert Watson /* 30135d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 30145d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 30155d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 30165d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 30175d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 30185d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 30195d1ae027SRobert Watson * the critical section. 30205d1ae027SRobert Watson */ 3021fc03d22bSJeff Roberson lockfail = 0; 3022fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 3023fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 3024a553d4b8SJeff Roberson ZONE_LOCK(zone); 3025fc03d22bSJeff Roberson lockfail = 1; 3026fc03d22bSJeff Roberson } 3027beb8beefSJeff Roberson 3028fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 3029*4bd61e19SJeff Roberson critical_enter(); 3030*4bd61e19SJeff Roberson cache = &zone->uz_cpu[curcpu]; 3031376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket != NULL) { 3032fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 3033beb8beefSJeff Roberson return (true); 3034a553d4b8SJeff Roberson } 30358355f576SJeff Roberson 3036fc03d22bSJeff Roberson /* 3037fc03d22bSJeff Roberson * Check the zone's cache of buckets. 3038fc03d22bSJeff Roberson */ 3039c1685086SJeff Roberson if (zone->uz_flags & UMA_ZONE_NUMA) { 3040c1685086SJeff Roberson domain = PCPU_GET(domain); 3041ab3185d1SJeff Roberson zdom = &zone->uz_domain[domain]; 3042c1685086SJeff Roberson } else { 3043c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3044c1685086SJeff Roberson zdom = &zone->uz_domain[0]; 3045c1685086SJeff Roberson } 3046c1685086SJeff Roberson 304708cfa56eSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom)) != NULL) { 3048beb8beefSJeff Roberson ZONE_UNLOCK(zone); 3049cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 3050a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 3051376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3052beb8beefSJeff Roberson return (true); 3053a553d4b8SJeff Roberson } 30545d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 30555d1ae027SRobert Watson critical_exit(); 3056bbee39c6SJeff Roberson 3057fc03d22bSJeff Roberson /* 3058fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 3059fc03d22bSJeff Roberson * handle the working set. 3060fc03d22bSJeff Roberson */ 306120a4e154SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 306220a4e154SJeff Roberson zone->uz_bucket_size++; 3063*4bd61e19SJeff Roberson ZONE_UNLOCK(zone); 3064bb15d1c7SGleb Smirnoff 30658355f576SJeff Roberson /* 3066beb8beefSJeff Roberson * Fill a bucket and attempt to use it as the alloc bucket. 3067bbee39c6SJeff Roberson */ 3068beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 30691431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 30701431a748SGleb Smirnoff zone->uz_name, zone, bucket); 3071*4bd61e19SJeff Roberson if (bucket == NULL) { 3072fc03d22bSJeff Roberson critical_enter(); 3073beb8beefSJeff Roberson return (false); 3074*4bd61e19SJeff Roberson } 30750f9b7bf3SMark Johnston 3076fc03d22bSJeff Roberson /* 3077fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3078fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3079fc03d22bSJeff Roberson * the memory directly. 3080fc03d22bSJeff Roberson */ 3081*4bd61e19SJeff Roberson ZONE_LOCK(zone); 3082*4bd61e19SJeff Roberson critical_enter(); 3083cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3084376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 308581c0d72cSGleb Smirnoff ((zone->uz_flags & UMA_ZONE_NUMA) == 0 || 308681c0d72cSGleb Smirnoff domain == PCPU_GET(domain))) { 3087376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 30880f9b7bf3SMark Johnston zdom->uzd_imax += bucket->ub_cnt; 3089bb15d1c7SGleb Smirnoff } else if (zone->uz_bkt_count >= zone->uz_bkt_max) { 309081c0d72cSGleb Smirnoff critical_exit(); 309181c0d72cSGleb Smirnoff ZONE_UNLOCK(zone); 309281c0d72cSGleb Smirnoff bucket_drain(zone, bucket); 309381c0d72cSGleb Smirnoff bucket_free(zone, bucket, udata); 3094beb8beefSJeff Roberson critical_enter(); 3095beb8beefSJeff Roberson return (true); 309681c0d72cSGleb Smirnoff } else 30970f9b7bf3SMark Johnston zone_put_bucket(zone, zdom, bucket, false); 3098bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 3099beb8beefSJeff Roberson return (true); 3100bbee39c6SJeff Roberson } 3101bbee39c6SJeff Roberson 3102ab3185d1SJeff Roberson void * 3103ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3104bbee39c6SJeff Roberson { 3105ab3185d1SJeff Roberson 3106ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 310719fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3108ab3185d1SJeff Roberson 3109ab3185d1SJeff Roberson /* This is the fast path allocation */ 3110ab3185d1SJeff Roberson CTR5(KTR_UMA, 3111ab3185d1SJeff Roberson "uma_zalloc_domain thread %x zone %s(%p) domain %d flags %d", 3112ab3185d1SJeff Roberson curthread, zone->uz_name, zone, domain, flags); 3113ab3185d1SJeff Roberson 3114ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3115ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3116ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3117ab3185d1SJeff Roberson } 3118ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3119ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 3120ab3185d1SJeff Roberson 3121ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3122ab3185d1SJeff Roberson } 3123ab3185d1SJeff Roberson 3124ab3185d1SJeff Roberson /* 3125ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3126ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3127ab3185d1SJeff Roberson * 3128ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3129ab3185d1SJeff Roberson * only 'domain'. 3130ab3185d1SJeff Roberson */ 3131ab3185d1SJeff Roberson static uma_slab_t 3132194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3133ab3185d1SJeff Roberson { 3134ab3185d1SJeff Roberson uma_domain_t dom; 3135bbee39c6SJeff Roberson uma_slab_t slab; 3136ab3185d1SJeff Roberson int start; 3137ab3185d1SJeff Roberson 3138ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3139ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 3140bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 3141ab3185d1SJeff Roberson 3142ab3185d1SJeff Roberson slab = NULL; 3143ab3185d1SJeff Roberson start = domain; 3144ab3185d1SJeff Roberson do { 3145ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 3146ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_part_slab)) 3147ab3185d1SJeff Roberson return (LIST_FIRST(&dom->ud_part_slab)); 3148ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_free_slab)) { 3149ab3185d1SJeff Roberson slab = LIST_FIRST(&dom->ud_free_slab); 3150ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 3151ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3152ab3185d1SJeff Roberson return (slab); 3153ab3185d1SJeff Roberson } 3154ab3185d1SJeff Roberson if (rr) 3155ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3156ab3185d1SJeff Roberson } while (domain != start); 3157ab3185d1SJeff Roberson 3158ab3185d1SJeff Roberson return (NULL); 3159ab3185d1SJeff Roberson } 3160ab3185d1SJeff Roberson 3161ab3185d1SJeff Roberson static uma_slab_t 3162194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3163ab3185d1SJeff Roberson { 3164194a979eSMark Johnston uint32_t reserve; 3165099a0e58SBosko Milekic 3166bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 3167194a979eSMark Johnston 3168194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 3169194a979eSMark Johnston if (keg->uk_free <= reserve) 3170194a979eSMark Johnston return (NULL); 3171194a979eSMark Johnston return (keg_first_slab(keg, domain, rr)); 3172194a979eSMark Johnston } 3173194a979eSMark Johnston 3174194a979eSMark Johnston static uma_slab_t 3175194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3176194a979eSMark Johnston { 3177194a979eSMark Johnston struct vm_domainset_iter di; 3178194a979eSMark Johnston uma_domain_t dom; 3179194a979eSMark Johnston uma_slab_t slab; 3180194a979eSMark Johnston int aflags, domain; 3181194a979eSMark Johnston bool rr; 3182194a979eSMark Johnston 3183194a979eSMark Johnston restart: 3184bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 3185bbee39c6SJeff Roberson 3186bbee39c6SJeff Roberson /* 3187194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3188194a979eSMark Johnston * domain (as happens with first-touch zones). 3189194a979eSMark Johnston * 3190194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3191194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3192194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3193bbee39c6SJeff Roberson */ 3194ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3195ab3185d1SJeff Roberson if (rr) { 3196194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3197194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3198194a979eSMark Johnston &aflags); 3199194a979eSMark Johnston } else { 3200194a979eSMark Johnston aflags = flags; 3201194a979eSMark Johnston domain = rdomain; 3202194a979eSMark Johnston } 3203ab3185d1SJeff Roberson 3204194a979eSMark Johnston for (;;) { 3205194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3206584061b4SJeff Roberson if (slab != NULL) 3207bbee39c6SJeff Roberson return (slab); 3208bbee39c6SJeff Roberson 3209bbee39c6SJeff Roberson /* 3210bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3211bbee39c6SJeff Roberson */ 3212bbee39c6SJeff Roberson if (flags & M_NOVM) 3213bbee39c6SJeff Roberson break; 3214bbee39c6SJeff Roberson 321586220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 3216bbee39c6SJeff Roberson /* 3217bbee39c6SJeff Roberson * If we got a slab here it's safe to mark it partially used 3218bbee39c6SJeff Roberson * and return. We assume that the caller is going to remove 3219bbee39c6SJeff Roberson * at least one item. 3220bbee39c6SJeff Roberson */ 3221bbee39c6SJeff Roberson if (slab) { 3222ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3223ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3224bbee39c6SJeff Roberson return (slab); 3225bbee39c6SJeff Roberson } 3226194a979eSMark Johnston KEG_LOCK(keg); 32273639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 32283639ac42SJeff Roberson break; 3229194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3230194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 3231194a979eSMark Johnston KEG_UNLOCK(keg); 3232194a979eSMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 3233194a979eSMark Johnston KEG_LOCK(keg); 3234194a979eSMark Johnston goto restart; 323530c5525bSAndrew Gallatin } 3236194a979eSMark Johnston break; 3237194a979eSMark Johnston } 3238ab3185d1SJeff Roberson } 3239ab3185d1SJeff Roberson 3240bbee39c6SJeff Roberson /* 3241bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3242bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3243bbee39c6SJeff Roberson * fail. 3244bbee39c6SJeff Roberson */ 3245194a979eSMark Johnston if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) { 3246bbee39c6SJeff Roberson return (slab); 3247bbee39c6SJeff Roberson } 3248ab3185d1SJeff Roberson return (NULL); 3249ab3185d1SJeff Roberson } 3250bbee39c6SJeff Roberson 3251d56368d7SBosko Milekic static void * 32520095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3253bbee39c6SJeff Roberson { 3254ab3185d1SJeff Roberson uma_domain_t dom; 3255bbee39c6SJeff Roberson void *item; 325685dcf349SGleb Smirnoff uint8_t freei; 3257bbee39c6SJeff Roberson 3258bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 3259099a0e58SBosko Milekic 32609b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 32619b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 32621e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3263bbee39c6SJeff Roberson slab->us_freecount--; 3264099a0e58SBosko Milekic keg->uk_free--; 3265ef72505eSJeff Roberson 3266bbee39c6SJeff Roberson /* Move this slab to the full list */ 3267bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3268bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3269ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3270ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3271bbee39c6SJeff Roberson } 3272bbee39c6SJeff Roberson 3273bbee39c6SJeff Roberson return (item); 3274bbee39c6SJeff Roberson } 3275bbee39c6SJeff Roberson 3276bbee39c6SJeff Roberson static int 3277b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 32780095a784SJeff Roberson { 3279b75c4efcSAndrew Turner uma_zone_t zone; 32800095a784SJeff Roberson uma_slab_t slab; 32810095a784SJeff Roberson uma_keg_t keg; 3282a03af342SSean Bruno #ifdef NUMA 3283ab3185d1SJeff Roberson int stripe; 3284a03af342SSean Bruno #endif 32850095a784SJeff Roberson int i; 32860095a784SJeff Roberson 3287b75c4efcSAndrew Turner zone = arg; 32880095a784SJeff Roberson slab = NULL; 3289584061b4SJeff Roberson keg = zone->uz_keg; 3290584061b4SJeff Roberson KEG_LOCK(keg); 3291af526374SJeff Roberson /* Try to keep the buckets totally full */ 32920095a784SJeff Roberson for (i = 0; i < max; ) { 3293584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 32940095a784SJeff Roberson break; 3295a03af342SSean Bruno #ifdef NUMA 3296ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3297a03af342SSean Bruno #endif 32986fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 32990095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 33006fd34d6fSJeff Roberson if (keg->uk_free <= keg->uk_reserve) 33016fd34d6fSJeff Roberson break; 3302b6715dabSJeff Roberson #ifdef NUMA 3303ab3185d1SJeff Roberson /* 3304ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3305ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3306ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3307ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3308ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3309ab3185d1SJeff Roberson * time but yields better distribution. 3310ab3185d1SJeff Roberson */ 3311ab3185d1SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) == 0 && 3312ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3313ab3185d1SJeff Roberson break; 3314ab3185d1SJeff Roberson #endif 33156fd34d6fSJeff Roberson } 3316ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 33170095a784SJeff Roberson flags &= ~M_WAITOK; 33180095a784SJeff Roberson flags |= M_NOWAIT; 33190095a784SJeff Roberson } 33200095a784SJeff Roberson KEG_UNLOCK(keg); 33210095a784SJeff Roberson 33220095a784SJeff Roberson return i; 33230095a784SJeff Roberson } 33240095a784SJeff Roberson 3325*4bd61e19SJeff Roberson static int 3326*4bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 3327*4bd61e19SJeff Roberson { 3328*4bd61e19SJeff Roberson uint64_t old, new, total, max; 3329*4bd61e19SJeff Roberson 3330*4bd61e19SJeff Roberson /* 3331*4bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 3332*4bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 3333*4bd61e19SJeff Roberson * fairness by keeping fifo order. 3334*4bd61e19SJeff Roberson * 3335*4bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 3336*4bd61e19SJeff Roberson */ 3337*4bd61e19SJeff Roberson for (;;) { 3338*4bd61e19SJeff Roberson zone_free_limit(zone, count); 3339*4bd61e19SJeff Roberson zone_log_warning(zone); 3340*4bd61e19SJeff Roberson zone_maxaction(zone); 3341*4bd61e19SJeff Roberson if (flags & M_NOWAIT) 3342*4bd61e19SJeff Roberson return (0); 3343*4bd61e19SJeff Roberson 3344*4bd61e19SJeff Roberson /* 3345*4bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 3346*4bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 3347*4bd61e19SJeff Roberson * is essentially a home rolled semaphore. 3348*4bd61e19SJeff Roberson */ 3349*4bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 3350*4bd61e19SJeff Roberson old = zone->uz_items; 3351*4bd61e19SJeff Roberson do { 3352*4bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 3353*4bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 3354*4bd61e19SJeff Roberson max = zone->uz_max_items; 3355*4bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 3356*4bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 3357*4bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 3358*4bd61e19SJeff Roberson else 3359*4bd61e19SJeff Roberson new = old + MIN(count, max - old); 3360*4bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 3361*4bd61e19SJeff Roberson 3362*4bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 3363*4bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 3364*4bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 3365*4bd61e19SJeff Roberson return (new - old); 3366*4bd61e19SJeff Roberson } 3367*4bd61e19SJeff Roberson 3368*4bd61e19SJeff Roberson /* 3369*4bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 3370*4bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 3371*4bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 3372*4bd61e19SJeff Roberson * transitions. 3373*4bd61e19SJeff Roberson */ 3374*4bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 3375*4bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 3376*4bd61e19SJeff Roberson 3377*4bd61e19SJeff Roberson /* 3378*4bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 3379*4bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 3380*4bd61e19SJeff Roberson */ 3381*4bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 3382*4bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 3383*4bd61e19SJeff Roberson 3384*4bd61e19SJeff Roberson /* 3385*4bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 3386*4bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 3387*4bd61e19SJeff Roberson * 3388*4bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 3389*4bd61e19SJeff Roberson * our count. 3390*4bd61e19SJeff Roberson */ 3391*4bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 3392*4bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 3393*4bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 3394*4bd61e19SJeff Roberson /* We're no longer a sleeper. */ 3395*4bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 3396*4bd61e19SJeff Roberson 3397*4bd61e19SJeff Roberson /* 3398*4bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 3399*4bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 3400*4bd61e19SJeff Roberson * against changes via sysctl. 3401*4bd61e19SJeff Roberson */ 3402*4bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 3403*4bd61e19SJeff Roberson max = zone->uz_max_items; 3404*4bd61e19SJeff Roberson if (total >= max) 3405*4bd61e19SJeff Roberson continue; 3406*4bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 3407*4bd61e19SJeff Roberson if (total + count > max) { 3408*4bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 3409*4bd61e19SJeff Roberson count = max - total; 3410*4bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 3411*4bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 3412*4bd61e19SJeff Roberson 3413*4bd61e19SJeff Roberson return (count); 3414*4bd61e19SJeff Roberson } 3415*4bd61e19SJeff Roberson } 3416*4bd61e19SJeff Roberson 3417*4bd61e19SJeff Roberson /* 3418*4bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 3419*4bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 3420*4bd61e19SJeff Roberson * one item can be allocated. 3421*4bd61e19SJeff Roberson */ 3422*4bd61e19SJeff Roberson static int 3423*4bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 3424*4bd61e19SJeff Roberson { 3425*4bd61e19SJeff Roberson uint64_t old; 3426*4bd61e19SJeff Roberson uint64_t max; 3427*4bd61e19SJeff Roberson 3428*4bd61e19SJeff Roberson max = zone->uz_max_items; 3429*4bd61e19SJeff Roberson MPASS(max > 0); 3430*4bd61e19SJeff Roberson 3431*4bd61e19SJeff Roberson /* 3432*4bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 3433*4bd61e19SJeff Roberson * fetchadd. 3434*4bd61e19SJeff Roberson */ 3435*4bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 3436*4bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 3437*4bd61e19SJeff Roberson return (count); 3438*4bd61e19SJeff Roberson 3439*4bd61e19SJeff Roberson /* 3440*4bd61e19SJeff Roberson * If we had some items and no sleepers just return the 3441*4bd61e19SJeff Roberson * truncated value. We have to release the excess space 3442*4bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 3443*4bd61e19SJeff Roberson * because we were temporarily over the limit. 3444*4bd61e19SJeff Roberson */ 3445*4bd61e19SJeff Roberson if (old < max) { 3446*4bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 3447*4bd61e19SJeff Roberson return (max - old); 3448*4bd61e19SJeff Roberson } 3449*4bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 3450*4bd61e19SJeff Roberson } 3451*4bd61e19SJeff Roberson 3452*4bd61e19SJeff Roberson /* 3453*4bd61e19SJeff Roberson * Free a number of items back to the limit. 3454*4bd61e19SJeff Roberson */ 3455*4bd61e19SJeff Roberson static void 3456*4bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 3457*4bd61e19SJeff Roberson { 3458*4bd61e19SJeff Roberson uint64_t old; 3459*4bd61e19SJeff Roberson 3460*4bd61e19SJeff Roberson MPASS(count > 0); 3461*4bd61e19SJeff Roberson 3462*4bd61e19SJeff Roberson /* 3463*4bd61e19SJeff Roberson * In the common case we either have no sleepers or 3464*4bd61e19SJeff Roberson * are still over the limit and can just return. 3465*4bd61e19SJeff Roberson */ 3466*4bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 3467*4bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 3468*4bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 3469*4bd61e19SJeff Roberson return; 3470*4bd61e19SJeff Roberson 3471*4bd61e19SJeff Roberson /* 3472*4bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 3473*4bd61e19SJeff Roberson * to generate wakeups if necessary. 3474*4bd61e19SJeff Roberson */ 3475*4bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 3476*4bd61e19SJeff Roberson } 3477*4bd61e19SJeff Roberson 3478fc03d22bSJeff Roberson static uma_bucket_t 3479beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3480bbee39c6SJeff Roberson { 3481bbee39c6SJeff Roberson uma_bucket_t bucket; 3482beb8beefSJeff Roberson int maxbucket, cnt; 3483bbee39c6SJeff Roberson 348430c5525bSAndrew Gallatin CTR1(KTR_UMA, "zone_alloc:_bucket domain %d)", domain); 348530c5525bSAndrew Gallatin 3486c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3487c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3488c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3489c1685086SJeff Roberson 3490*4bd61e19SJeff Roberson if (zone->uz_max_items > 0) 3491*4bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 3492*4bd61e19SJeff Roberson M_NOWAIT); 3493*4bd61e19SJeff Roberson else 349420a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 3495*4bd61e19SJeff Roberson if (maxbucket == 0) 3496*4bd61e19SJeff Roberson return (false); 3497beb8beefSJeff Roberson 34986fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 34996fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3500beb8beefSJeff Roberson if (bucket == NULL) { 3501beb8beefSJeff Roberson cnt = 0; 3502beb8beefSJeff Roberson goto out; 3503beb8beefSJeff Roberson } 35040095a784SJeff Roberson 35050095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3506beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 35070095a784SJeff Roberson 35080095a784SJeff Roberson /* 35090095a784SJeff Roberson * Initialize the memory if necessary. 35100095a784SJeff Roberson */ 35110095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 3512099a0e58SBosko Milekic int i; 3513bbee39c6SJeff Roberson 35140095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 3515e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 35160095a784SJeff Roberson flags) != 0) 3517b23f72e9SBrian Feldman break; 3518b23f72e9SBrian Feldman /* 3519b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 3520b23f72e9SBrian Feldman * rest back onto the freelist. 3521b23f72e9SBrian Feldman */ 3522b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 3523af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 35240095a784SJeff Roberson bucket->ub_cnt - i); 3525a5a262c6SBosko Milekic #ifdef INVARIANTS 35260095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 35270095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 3528a5a262c6SBosko Milekic #endif 3529b23f72e9SBrian Feldman bucket->ub_cnt = i; 3530b23f72e9SBrian Feldman } 3531099a0e58SBosko Milekic } 3532099a0e58SBosko Milekic 3533beb8beefSJeff Roberson cnt = bucket->ub_cnt; 3534f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 35356fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 35362efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 3537beb8beefSJeff Roberson bucket = NULL; 3538beb8beefSJeff Roberson } 3539beb8beefSJeff Roberson out: 3540*4bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 3541*4bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 3542fc03d22bSJeff Roberson 3543fc03d22bSJeff Roberson return (bucket); 3544fc03d22bSJeff Roberson } 3545fc03d22bSJeff Roberson 35468355f576SJeff Roberson /* 35470095a784SJeff Roberson * Allocates a single item from a zone. 35488355f576SJeff Roberson * 35498355f576SJeff Roberson * Arguments 35508355f576SJeff Roberson * zone The zone to alloc for. 35518355f576SJeff Roberson * udata The data to be passed to the constructor. 3552ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 3553a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 35548355f576SJeff Roberson * 35558355f576SJeff Roberson * Returns 35568355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 3557bbee39c6SJeff Roberson * An item if successful 35588355f576SJeff Roberson */ 35598355f576SJeff Roberson 35608355f576SJeff Roberson static void * 3561ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 35628355f576SJeff Roberson { 35638355f576SJeff Roberson void *item; 35648355f576SJeff Roberson 3565*4bd61e19SJeff Roberson if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) 3566bb15d1c7SGleb Smirnoff return (NULL); 35678355f576SJeff Roberson 3568c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3569c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 357030c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 3571c1685086SJeff Roberson 3572ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 3573beb8beefSJeff Roberson goto fail_cnt; 35748355f576SJeff Roberson 3575099a0e58SBosko Milekic /* 3576099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 3577099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 3578099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 3579099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 3580099a0e58SBosko Milekic */ 3581b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 3582e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 3583bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 3584beb8beefSJeff Roberson goto fail_cnt; 3585beb8beefSJeff Roberson } 3586beb8beefSJeff Roberson } 3587cc7ce83aSJeff Roberson item = item_ctor(zone, zone->uz_size, udata, flags, item); 3588beb8beefSJeff Roberson if (item == NULL) 35890095a784SJeff Roberson goto fail; 35908355f576SJeff Roberson 35912efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 35921431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 35931431a748SGleb Smirnoff zone->uz_name, zone); 35941431a748SGleb Smirnoff 35958355f576SJeff Roberson return (item); 35960095a784SJeff Roberson 3597beb8beefSJeff Roberson fail_cnt: 3598beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 35990095a784SJeff Roberson fail: 3600*4bd61e19SJeff Roberson if (zone->uz_max_items > 0) 3601*4bd61e19SJeff Roberson zone_free_limit(zone, 1); 36021431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 36031431a748SGleb Smirnoff zone->uz_name, zone); 3604*4bd61e19SJeff Roberson 36050095a784SJeff Roberson return (NULL); 36068355f576SJeff Roberson } 36078355f576SJeff Roberson 36088355f576SJeff Roberson /* See uma.h */ 36098355f576SJeff Roberson void 36108355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 36118355f576SJeff Roberson { 36128355f576SJeff Roberson uma_cache_t cache; 3613376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3614cc7ce83aSJeff Roberson int domain, itemdomain, uz_flags; 36158355f576SJeff Roberson 3616e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 361719fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 361810cb2424SMark Murray 36193659f747SRobert Watson CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread, 36203659f747SRobert Watson zone->uz_name); 36213659f747SRobert Watson 3622d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 36231067a2baSJonathan T. Looney ("uma_zfree_arg: called with spinlock or critical section held")); 36241067a2baSJonathan T. Looney 362520ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 362620ed0cb0SMatthew D Fleming if (item == NULL) 362720ed0cb0SMatthew D Fleming return; 36288d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 36298d689e04SGleb Smirnoff if (is_memguard_addr(item)) { 3630bc9d08e1SMark Johnston if (zone->uz_dtor != NULL) 36318d689e04SGleb Smirnoff zone->uz_dtor(item, zone->uz_size, udata); 3632bc9d08e1SMark Johnston if (zone->uz_fini != NULL) 36338d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 36348d689e04SGleb Smirnoff memguard_free(item); 36358d689e04SGleb Smirnoff return; 36368d689e04SGleb Smirnoff } 36378d689e04SGleb Smirnoff #endif 3638cc7ce83aSJeff Roberson 3639cc7ce83aSJeff Roberson /* 3640cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 3641cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 3642cc7ce83aSJeff Roberson * will simply read another cpu's line. 3643cc7ce83aSJeff Roberson */ 3644cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3645cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3646cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 3647cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 3648cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 3649ef72505eSJeff Roberson 3650af7f9b97SJeff Roberson /* 3651af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 3652af7f9b97SJeff Roberson * a little longer for the limits to be reset. 3653af7f9b97SJeff Roberson */ 3654cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 3655bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0) 3656fc03d22bSJeff Roberson goto zfree_item; 3657cc7ce83aSJeff Roberson } 3658af7f9b97SJeff Roberson 36595d1ae027SRobert Watson /* 36605d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 36615d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 36625d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 36635d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 36645d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 36655d1ae027SRobert Watson * preemption and migration. We release the critical section in 36665d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 36675d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 36685d1ae027SRobert Watson * detect and handle migration if it has occurred. 36695d1ae027SRobert Watson */ 36700a81b439SJeff Roberson domain = itemdomain = 0; 36715d1ae027SRobert Watson critical_enter(); 36720a81b439SJeff Roberson do { 3673cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3674376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 3675c1685086SJeff Roberson #ifdef UMA_XDOMAIN 3676cc7ce83aSJeff Roberson if ((uz_flags & UMA_ZONE_NUMA) != 0) { 36770a81b439SJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 36780a81b439SJeff Roberson domain = PCPU_GET(domain); 36790a81b439SJeff Roberson } 3680cc7ce83aSJeff Roberson if ((uz_flags & UMA_ZONE_NUMA) != 0 && domain != itemdomain) { 3681376b1ba3SJeff Roberson bucket = &cache->uc_crossbucket; 36820a81b439SJeff Roberson } else 3683c1685086SJeff Roberson #endif 36840a81b439SJeff Roberson 3685a553d4b8SJeff Roberson /* 3686fc03d22bSJeff Roberson * Try to free into the allocbucket first to give LIFO ordering 3687fc03d22bSJeff Roberson * for cache-hot datastructures. Spill over into the freebucket 3688fc03d22bSJeff Roberson * if necessary. Alloc will swap them if one runs dry. 3689a553d4b8SJeff Roberson */ 3690376b1ba3SJeff Roberson if (__predict_false(bucket->ucb_cnt >= bucket->ucb_entries)) 3691376b1ba3SJeff Roberson bucket = &cache->uc_freebucket; 3692376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3693376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 36945d1ae027SRobert Watson critical_exit(); 36958355f576SJeff Roberson return; 3696fc03d22bSJeff Roberson } 36970a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 36980a81b439SJeff Roberson critical_exit(); 3699fc03d22bSJeff Roberson 37008355f576SJeff Roberson /* 37010a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 37028355f576SJeff Roberson */ 37030a81b439SJeff Roberson zfree_item: 37040a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 37050a81b439SJeff Roberson } 3706fc03d22bSJeff Roberson 37070a81b439SJeff Roberson static void 37080a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 37090a81b439SJeff Roberson int domain, int itemdomain) 37100a81b439SJeff Roberson { 37110a81b439SJeff Roberson uma_zone_domain_t zdom; 37120a81b439SJeff Roberson 37130a81b439SJeff Roberson #ifdef UMA_XDOMAIN 37140a81b439SJeff Roberson /* 37150a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 37160a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 37170a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 37180a81b439SJeff Roberson * correct domains by freeing the contents to the slab layer. 37190a81b439SJeff Roberson */ 37200a81b439SJeff Roberson if (domain != itemdomain && vm_ndomains > 2) { 37210a81b439SJeff Roberson CTR3(KTR_UMA, 37220a81b439SJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 37230a81b439SJeff Roberson zone->uz_name, zone, bucket); 37240a81b439SJeff Roberson bucket_drain(zone, bucket); 37250a81b439SJeff Roberson bucket_free(zone, bucket, udata); 37260a81b439SJeff Roberson return; 37270a81b439SJeff Roberson } 37280a81b439SJeff Roberson #endif 37290a81b439SJeff Roberson /* 37300a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 37310a81b439SJeff Roberson * 37320a81b439SJeff Roberson * We bump the uz count when the cache size is insufficient to 37330a81b439SJeff Roberson * handle the working set. 37340a81b439SJeff Roberson */ 37354d104ba0SAlexander Motin if (ZONE_TRYLOCK(zone) == 0) { 37364d104ba0SAlexander Motin /* Record contention to size the buckets. */ 37378355f576SJeff Roberson ZONE_LOCK(zone); 373820a4e154SJeff Roberson if (zone->uz_bucket_size < zone->uz_bucket_size_max) 373920a4e154SJeff Roberson zone->uz_bucket_size++; 37404d104ba0SAlexander Motin } 37418355f576SJeff Roberson 37420a81b439SJeff Roberson CTR3(KTR_UMA, 37430a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 37440a81b439SJeff Roberson zone->uz_name, zone, bucket); 37450a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 37460a81b439SJeff Roberson KASSERT(bucket->ub_cnt == bucket->ub_entries, 37470a81b439SJeff Roberson ("uma_zfree: Attempting to insert partial bucket onto the full list.\n")); 37480a81b439SJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 3749c1685086SJeff Roberson ZONE_UNLOCK(zone); 3750c1685086SJeff Roberson bucket_drain(zone, bucket); 3751c1685086SJeff Roberson bucket_free(zone, bucket, udata); 3752c1685086SJeff Roberson } else { 3753c1685086SJeff Roberson zdom = &zone->uz_domain[itemdomain]; 3754c1685086SJeff Roberson zone_put_bucket(zone, zdom, bucket, true); 3755c1685086SJeff Roberson ZONE_UNLOCK(zone); 3756c1685086SJeff Roberson } 37578355f576SJeff Roberson } 3758fc03d22bSJeff Roberson 37594d104ba0SAlexander Motin /* 37600a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 37610a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 37620a81b439SJeff Roberson * 37630a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 37640a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 37650a81b439SJeff Roberson * the caller should retry. 37664d104ba0SAlexander Motin */ 37670a81b439SJeff Roberson static __noinline bool 37680a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 37690a81b439SJeff Roberson int itemdomain) 37700a81b439SJeff Roberson { 37710a81b439SJeff Roberson uma_bucket_t bucket; 3772cc7ce83aSJeff Roberson int domain; 37730a81b439SJeff Roberson 37740a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 37750a81b439SJeff Roberson 377620a4e154SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) 37770a81b439SJeff Roberson return false; 37780a81b439SJeff Roberson 3779cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 37800a81b439SJeff Roberson 37810a81b439SJeff Roberson /* 37820a81b439SJeff Roberson * NUMA domains need to free to the correct zdom. When XDOMAIN 37830a81b439SJeff Roberson * is enabled this is the zdom of the item and the bucket may be 37840a81b439SJeff Roberson * the cross bucket if they do not match. 37850a81b439SJeff Roberson */ 37860a81b439SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) 37870a81b439SJeff Roberson #ifdef UMA_XDOMAIN 37880a81b439SJeff Roberson domain = PCPU_GET(domain); 37890a81b439SJeff Roberson #else 37900a81b439SJeff Roberson itemdomain = domain = PCPU_GET(domain); 37910a81b439SJeff Roberson #endif 37920a81b439SJeff Roberson else 37930a81b439SJeff Roberson itemdomain = domain = 0; 37940a81b439SJeff Roberson #ifdef UMA_XDOMAIN 37950a81b439SJeff Roberson if (domain != itemdomain) { 3796376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 37970a81b439SJeff Roberson if (bucket != NULL) 37980a81b439SJeff Roberson atomic_add_64(&zone->uz_xdomain, bucket->ub_cnt); 37990a81b439SJeff Roberson } else 38000a81b439SJeff Roberson #endif 3801376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 38020a81b439SJeff Roberson 38030a81b439SJeff Roberson 38040a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 38050a81b439SJeff Roberson critical_exit(); 38060a81b439SJeff Roberson 38070a81b439SJeff Roberson if (bucket != NULL) 38080a81b439SJeff Roberson zone_free_bucket(zone, bucket, udata, domain, itemdomain); 3809a553d4b8SJeff Roberson 38106fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT); 38111431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p", 38121431a748SGleb Smirnoff zone->uz_name, zone, bucket); 3813fc03d22bSJeff Roberson critical_enter(); 38140a81b439SJeff Roberson if (bucket == NULL) 38150a81b439SJeff Roberson return (false); 3816cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 38170a81b439SJeff Roberson #ifdef UMA_XDOMAIN 3818fc03d22bSJeff Roberson /* 38190a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 38200a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 38210a81b439SJeff Roberson * the free bucket. 3822fc03d22bSJeff Roberson */ 38230a81b439SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) { 38240a81b439SJeff Roberson domain = PCPU_GET(domain); 3825376b1ba3SJeff Roberson if (domain != itemdomain && 3826376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 3827376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 38280a81b439SJeff Roberson return (true); 38290a81b439SJeff Roberson } 38300a81b439SJeff Roberson } 38310a81b439SJeff Roberson #endif 38320a81b439SJeff Roberson /* 38330a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 38340a81b439SJeff Roberson */ 3835376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 3836fc03d22bSJeff Roberson critical_exit(); 38376fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 38380a81b439SJeff Roberson critical_enter(); 38390a81b439SJeff Roberson } else 3840376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 38418355f576SJeff Roberson 38420a81b439SJeff Roberson return (true); 38438355f576SJeff Roberson } 38448355f576SJeff Roberson 3845ab3185d1SJeff Roberson void 3846ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata) 3847ab3185d1SJeff Roberson { 3848ab3185d1SJeff Roberson 3849ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 385019fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3851ab3185d1SJeff Roberson 3852ab3185d1SJeff Roberson CTR2(KTR_UMA, "uma_zfree_domain thread %x zone %s", curthread, 3853ab3185d1SJeff Roberson zone->uz_name); 3854ab3185d1SJeff Roberson 3855ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3856ab3185d1SJeff Roberson ("uma_zfree_domain: called with spinlock or critical section held")); 3857ab3185d1SJeff Roberson 3858ab3185d1SJeff Roberson /* uma_zfree(..., NULL) does nothing, to match free(9). */ 3859ab3185d1SJeff Roberson if (item == NULL) 3860ab3185d1SJeff Roberson return; 3861ab3185d1SJeff Roberson zone_free_item(zone, item, udata, SKIP_NONE); 3862ab3185d1SJeff Roberson } 3863ab3185d1SJeff Roberson 38648355f576SJeff Roberson static void 3865bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 38668355f576SJeff Roberson { 3867bb15d1c7SGleb Smirnoff uma_keg_t keg; 3868ab3185d1SJeff Roberson uma_domain_t dom; 386985dcf349SGleb Smirnoff uint8_t freei; 3870099a0e58SBosko Milekic 3871bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 3872bb15d1c7SGleb Smirnoff MPASS(zone->uz_lockptr == &keg->uk_lock); 3873bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 38748355f576SJeff Roberson 3875ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3876ab3185d1SJeff Roberson 38778355f576SJeff Roberson /* Do we need to remove from any lists? */ 3878099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 38798355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3880ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 38818355f576SJeff Roberson } else if (slab->us_freecount == 0) { 38828355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3883ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 38848355f576SJeff Roberson } 38858355f576SJeff Roberson 3886ef72505eSJeff Roberson /* Slab management. */ 38871e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 38889b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 38898355f576SJeff Roberson slab->us_freecount++; 38908355f576SJeff Roberson 3891ef72505eSJeff Roberson /* Keg statistics. */ 3892099a0e58SBosko Milekic keg->uk_free++; 38930095a784SJeff Roberson } 38940095a784SJeff Roberson 38950095a784SJeff Roberson static void 3896b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 38970095a784SJeff Roberson { 3898b75c4efcSAndrew Turner uma_zone_t zone; 38990095a784SJeff Roberson void *item; 39000095a784SJeff Roberson uma_slab_t slab; 39010095a784SJeff Roberson uma_keg_t keg; 39020095a784SJeff Roberson uint8_t *mem; 39030095a784SJeff Roberson int i; 39048355f576SJeff Roberson 3905b75c4efcSAndrew Turner zone = arg; 3906bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 3907af526374SJeff Roberson KEG_LOCK(keg); 39080095a784SJeff Roberson for (i = 0; i < cnt; i++) { 39090095a784SJeff Roberson item = bucket[i]; 39100095a784SJeff Roberson if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) { 39110095a784SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 39120095a784SJeff Roberson if (zone->uz_flags & UMA_ZONE_HASH) { 39130095a784SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 39140095a784SJeff Roberson } else { 39150095a784SJeff Roberson mem += keg->uk_pgoff; 39160095a784SJeff Roberson slab = (uma_slab_t)mem; 39170095a784SJeff Roberson } 3918584061b4SJeff Roberson } else 39190095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 3920bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 39210095a784SJeff Roberson } 3922af526374SJeff Roberson KEG_UNLOCK(keg); 39238355f576SJeff Roberson } 39248355f576SJeff Roberson 39250095a784SJeff Roberson /* 39260095a784SJeff Roberson * Frees a single item to any zone. 39270095a784SJeff Roberson * 39280095a784SJeff Roberson * Arguments: 39290095a784SJeff Roberson * zone The zone to free to 39300095a784SJeff Roberson * item The item we're freeing 39310095a784SJeff Roberson * udata User supplied data for the dtor 39320095a784SJeff Roberson * skip Skip dtors and finis 39330095a784SJeff Roberson */ 39340095a784SJeff Roberson static void 39350095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 39360095a784SJeff Roberson { 3937c5deaf04SGleb Smirnoff 3938cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 39390095a784SJeff Roberson 39400095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 39410095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 39420095a784SJeff Roberson 39430095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 3944bb15d1c7SGleb Smirnoff 3945bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 3946bb15d1c7SGleb Smirnoff return; 3947bb15d1c7SGleb Smirnoff 39482efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 39492efcc8cbSGleb Smirnoff 3950*4bd61e19SJeff Roberson if (zone->uz_max_items > 0) 3951*4bd61e19SJeff Roberson zone_free_limit(zone, 1); 3952bb45b411SGleb Smirnoff } 39530095a784SJeff Roberson 39548355f576SJeff Roberson /* See uma.h */ 39551c6cae97SLawrence Stewart int 3956736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 3957736ee590SJeff Roberson { 3958bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 3959003cf08bSMark Johnston int count; 3960bb15d1c7SGleb Smirnoff 3961*4bd61e19SJeff Roberson /* 3962*4bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 3963*4bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 3964*4bd61e19SJeff Roberson * way to clear a limit. 3965*4bd61e19SJeff Roberson */ 3966bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 3967003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 3968003cf08bSMark Johnston count = ubz != NULL ? ubz->ubz_entries : 0; 396920a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = count; 397020a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 397120a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 3972bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 3973cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 3974cc7ce83aSJeff Roberson zone_update_caches(zone); 3975*4bd61e19SJeff Roberson /* We may need to wake waiters. */ 3976*4bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 3977bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 3978bb15d1c7SGleb Smirnoff 3979bb15d1c7SGleb Smirnoff return (nitems); 3980bb15d1c7SGleb Smirnoff } 3981bb15d1c7SGleb Smirnoff 3982bb15d1c7SGleb Smirnoff /* See uma.h */ 3983003cf08bSMark Johnston void 3984bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 3985bb15d1c7SGleb Smirnoff { 3986003cf08bSMark Johnston struct uma_bucket_zone *ubz; 3987003cf08bSMark Johnston int bpcpu; 3988bb15d1c7SGleb Smirnoff 3989bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 3990003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 3991003cf08bSMark Johnston if (ubz != NULL) { 3992003cf08bSMark Johnston bpcpu = 2; 3993003cf08bSMark Johnston #ifdef UMA_XDOMAIN 3994003cf08bSMark Johnston if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) 3995003cf08bSMark Johnston /* Count the cross-domain bucket. */ 3996003cf08bSMark Johnston bpcpu++; 3997003cf08bSMark Johnston #endif 3998003cf08bSMark Johnston nitems -= ubz->ubz_entries * bpcpu * mp_ncpus; 399920a4e154SJeff Roberson zone->uz_bucket_size_max = ubz->ubz_entries; 4000003cf08bSMark Johnston } else { 400120a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 4002003cf08bSMark Johnston } 400320a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 400420a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4005bb15d1c7SGleb Smirnoff zone->uz_bkt_max = nitems; 4006bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4007736ee590SJeff Roberson } 4008736ee590SJeff Roberson 4009736ee590SJeff Roberson /* See uma.h */ 4010e49471b0SAndre Oppermann int 4011e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4012e49471b0SAndre Oppermann { 4013e49471b0SAndre Oppermann int nitems; 4014e49471b0SAndre Oppermann 4015bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4016bb15d1c7SGleb Smirnoff nitems = zone->uz_max_items; 4017bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4018e49471b0SAndre Oppermann 4019e49471b0SAndre Oppermann return (nitems); 4020e49471b0SAndre Oppermann } 4021e49471b0SAndre Oppermann 4022e49471b0SAndre Oppermann /* See uma.h */ 40232f891cd5SPawel Jakub Dawidek void 40242f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 40252f891cd5SPawel Jakub Dawidek { 40262f891cd5SPawel Jakub Dawidek 40272f891cd5SPawel Jakub Dawidek ZONE_LOCK(zone); 40282f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 40292f891cd5SPawel Jakub Dawidek ZONE_UNLOCK(zone); 40302f891cd5SPawel Jakub Dawidek } 40312f891cd5SPawel Jakub Dawidek 40322f891cd5SPawel Jakub Dawidek /* See uma.h */ 403354503a13SJonathan T. Looney void 403454503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 403554503a13SJonathan T. Looney { 403654503a13SJonathan T. Looney 403754503a13SJonathan T. Looney ZONE_LOCK(zone); 4038e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 403954503a13SJonathan T. Looney ZONE_UNLOCK(zone); 404054503a13SJonathan T. Looney } 404154503a13SJonathan T. Looney 404254503a13SJonathan T. Looney /* See uma.h */ 4043c4ae7908SLawrence Stewart int 4044c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4045c4ae7908SLawrence Stewart { 4046c4ae7908SLawrence Stewart int64_t nitems; 4047c4ae7908SLawrence Stewart u_int i; 4048c4ae7908SLawrence Stewart 4049c4ae7908SLawrence Stewart ZONE_LOCK(zone); 40502efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 40512efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 405220a4e154SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_INTERNAL) == 0) { 4053c4ae7908SLawrence Stewart CPU_FOREACH(i) { 4054c4ae7908SLawrence Stewart /* 405520a4e154SJeff Roberson * See the comment in uma_vm_zone_stats() regarding 405620a4e154SJeff Roberson * the safety of accessing the per-cpu caches. With 405720a4e154SJeff Roberson * the zone lock held, it is safe, but can potentially 405820a4e154SJeff Roberson * result in stale data. 4059c4ae7908SLawrence Stewart */ 4060c4ae7908SLawrence Stewart nitems += zone->uz_cpu[i].uc_allocs - 4061c4ae7908SLawrence Stewart zone->uz_cpu[i].uc_frees; 4062c4ae7908SLawrence Stewart } 406320a4e154SJeff Roberson } 4064c4ae7908SLawrence Stewart ZONE_UNLOCK(zone); 4065c4ae7908SLawrence Stewart 4066c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4067c4ae7908SLawrence Stewart } 4068c4ae7908SLawrence Stewart 406920a4e154SJeff Roberson static uint64_t 407020a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 407120a4e154SJeff Roberson { 407220a4e154SJeff Roberson uint64_t nitems; 407320a4e154SJeff Roberson u_int i; 407420a4e154SJeff Roberson 407520a4e154SJeff Roberson ZONE_LOCK(zone); 407620a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 407720a4e154SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_INTERNAL) == 0) { 407820a4e154SJeff Roberson CPU_FOREACH(i) { 407920a4e154SJeff Roberson /* 408020a4e154SJeff Roberson * See the comment in uma_vm_zone_stats() regarding 408120a4e154SJeff Roberson * the safety of accessing the per-cpu caches. With 408220a4e154SJeff Roberson * the zone lock held, it is safe, but can potentially 408320a4e154SJeff Roberson * result in stale data. 408420a4e154SJeff Roberson */ 408520a4e154SJeff Roberson nitems += zone->uz_cpu[i].uc_allocs; 408620a4e154SJeff Roberson } 408720a4e154SJeff Roberson } 408820a4e154SJeff Roberson ZONE_UNLOCK(zone); 408920a4e154SJeff Roberson 409020a4e154SJeff Roberson return (nitems); 409120a4e154SJeff Roberson } 409220a4e154SJeff Roberson 409320a4e154SJeff Roberson static uint64_t 409420a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 409520a4e154SJeff Roberson { 409620a4e154SJeff Roberson uint64_t nitems; 409720a4e154SJeff Roberson u_int i; 409820a4e154SJeff Roberson 409920a4e154SJeff Roberson ZONE_LOCK(zone); 410020a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 410120a4e154SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_INTERNAL) == 0) { 410220a4e154SJeff Roberson CPU_FOREACH(i) { 410320a4e154SJeff Roberson /* 410420a4e154SJeff Roberson * See the comment in uma_vm_zone_stats() regarding 410520a4e154SJeff Roberson * the safety of accessing the per-cpu caches. With 410620a4e154SJeff Roberson * the zone lock held, it is safe, but can potentially 410720a4e154SJeff Roberson * result in stale data. 410820a4e154SJeff Roberson */ 410920a4e154SJeff Roberson nitems += zone->uz_cpu[i].uc_frees; 411020a4e154SJeff Roberson } 411120a4e154SJeff Roberson } 411220a4e154SJeff Roberson ZONE_UNLOCK(zone); 411320a4e154SJeff Roberson 411420a4e154SJeff Roberson return (nitems); 411520a4e154SJeff Roberson } 411620a4e154SJeff Roberson 4117c4ae7908SLawrence Stewart /* See uma.h */ 4118736ee590SJeff Roberson void 4119099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4120099a0e58SBosko Milekic { 4121e20a199fSJeff Roberson uma_keg_t keg; 4122e20a199fSJeff Roberson 4123bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4124af526374SJeff Roberson KEG_LOCK(keg); 4125e20a199fSJeff Roberson KASSERT(keg->uk_pages == 0, 4126099a0e58SBosko Milekic ("uma_zone_set_init on non-empty keg")); 4127e20a199fSJeff Roberson keg->uk_init = uminit; 4128af526374SJeff Roberson KEG_UNLOCK(keg); 4129099a0e58SBosko Milekic } 4130099a0e58SBosko Milekic 4131099a0e58SBosko Milekic /* See uma.h */ 4132099a0e58SBosko Milekic void 4133099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4134099a0e58SBosko Milekic { 4135e20a199fSJeff Roberson uma_keg_t keg; 4136e20a199fSJeff Roberson 4137bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4138af526374SJeff Roberson KEG_LOCK(keg); 4139e20a199fSJeff Roberson KASSERT(keg->uk_pages == 0, 4140099a0e58SBosko Milekic ("uma_zone_set_fini on non-empty keg")); 4141e20a199fSJeff Roberson keg->uk_fini = fini; 4142af526374SJeff Roberson KEG_UNLOCK(keg); 4143099a0e58SBosko Milekic } 4144099a0e58SBosko Milekic 4145099a0e58SBosko Milekic /* See uma.h */ 4146099a0e58SBosko Milekic void 4147099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4148099a0e58SBosko Milekic { 4149af526374SJeff Roberson 4150099a0e58SBosko Milekic ZONE_LOCK(zone); 4151bb15d1c7SGleb Smirnoff KASSERT(zone->uz_keg->uk_pages == 0, 4152099a0e58SBosko Milekic ("uma_zone_set_zinit on non-empty keg")); 4153099a0e58SBosko Milekic zone->uz_init = zinit; 4154099a0e58SBosko Milekic ZONE_UNLOCK(zone); 4155099a0e58SBosko Milekic } 4156099a0e58SBosko Milekic 4157099a0e58SBosko Milekic /* See uma.h */ 4158099a0e58SBosko Milekic void 4159099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4160099a0e58SBosko Milekic { 4161af526374SJeff Roberson 4162099a0e58SBosko Milekic ZONE_LOCK(zone); 4163bb15d1c7SGleb Smirnoff KASSERT(zone->uz_keg->uk_pages == 0, 4164099a0e58SBosko Milekic ("uma_zone_set_zfini on non-empty keg")); 4165099a0e58SBosko Milekic zone->uz_fini = zfini; 4166099a0e58SBosko Milekic ZONE_UNLOCK(zone); 4167099a0e58SBosko Milekic } 4168099a0e58SBosko Milekic 4169099a0e58SBosko Milekic /* See uma.h */ 4170b23f72e9SBrian Feldman /* XXX uk_freef is not actually used with the zone locked */ 4171099a0e58SBosko Milekic void 41728355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 41738355f576SJeff Roberson { 41740095a784SJeff Roberson uma_keg_t keg; 4175e20a199fSJeff Roberson 4176bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 41771d2c0c46SDmitry Chagin KASSERT(keg != NULL, ("uma_zone_set_freef: Invalid zone type")); 4178af526374SJeff Roberson KEG_LOCK(keg); 41790095a784SJeff Roberson keg->uk_freef = freef; 4180af526374SJeff Roberson KEG_UNLOCK(keg); 41818355f576SJeff Roberson } 41828355f576SJeff Roberson 41838355f576SJeff Roberson /* See uma.h */ 4184b23f72e9SBrian Feldman /* XXX uk_allocf is not actually used with the zone locked */ 41858355f576SJeff Roberson void 41868355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 41878355f576SJeff Roberson { 4188e20a199fSJeff Roberson uma_keg_t keg; 4189e20a199fSJeff Roberson 4190bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4191af526374SJeff Roberson KEG_LOCK(keg); 4192e20a199fSJeff Roberson keg->uk_allocf = allocf; 4193af526374SJeff Roberson KEG_UNLOCK(keg); 41948355f576SJeff Roberson } 41958355f576SJeff Roberson 41968355f576SJeff Roberson /* See uma.h */ 41976fd34d6fSJeff Roberson void 41986fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 41996fd34d6fSJeff Roberson { 42006fd34d6fSJeff Roberson uma_keg_t keg; 42016fd34d6fSJeff Roberson 4202bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 42036fd34d6fSJeff Roberson KEG_LOCK(keg); 42046fd34d6fSJeff Roberson keg->uk_reserve = items; 42056fd34d6fSJeff Roberson KEG_UNLOCK(keg); 42066fd34d6fSJeff Roberson } 42076fd34d6fSJeff Roberson 42086fd34d6fSJeff Roberson /* See uma.h */ 42098355f576SJeff Roberson int 4210a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 42118355f576SJeff Roberson { 4212099a0e58SBosko Milekic uma_keg_t keg; 42138355f576SJeff Roberson vm_offset_t kva; 42149ba30bcbSZbigniew Bodek u_int pages; 42158355f576SJeff Roberson 4216bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 42178355f576SJeff Roberson 4218bb15d1c7SGleb Smirnoff pages = count / keg->uk_ipers; 4219099a0e58SBosko Milekic if (pages * keg->uk_ipers < count) 42208355f576SJeff Roberson pages++; 422157223e99SAndriy Gapon pages *= keg->uk_ppera; 4222a553d4b8SJeff Roberson 4223a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4224a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4225a4915c21SAttilio Rao #else 4226a4915c21SAttilio Rao if (1) { 4227a4915c21SAttilio Rao #endif 422857223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4229d1f42ac2SAlan Cox if (kva == 0) 42308355f576SJeff Roberson return (0); 4231a4915c21SAttilio Rao } else 4232a4915c21SAttilio Rao kva = 0; 4233bb15d1c7SGleb Smirnoff 4234bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4235bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4236099a0e58SBosko Milekic keg->uk_kva = kva; 4237a4915c21SAttilio Rao keg->uk_offset = 0; 4238bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4239a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4240a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4241a4915c21SAttilio Rao #else 4242a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4243a4915c21SAttilio Rao #endif 4244cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4245cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4246cc7ce83aSJeff Roberson zone_update_caches(zone); 4247bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4248af526374SJeff Roberson 42498355f576SJeff Roberson return (1); 42508355f576SJeff Roberson } 42518355f576SJeff Roberson 42528355f576SJeff Roberson /* See uma.h */ 42538355f576SJeff Roberson void 42548355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 42558355f576SJeff Roberson { 4256920239efSMark Johnston struct vm_domainset_iter di; 4257ab3185d1SJeff Roberson uma_domain_t dom; 42588355f576SJeff Roberson uma_slab_t slab; 4259099a0e58SBosko Milekic uma_keg_t keg; 426086220393SMark Johnston int aflags, domain, slabs; 42618355f576SJeff Roberson 4262bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4263af526374SJeff Roberson KEG_LOCK(keg); 4264099a0e58SBosko Milekic slabs = items / keg->uk_ipers; 4265099a0e58SBosko Milekic if (slabs * keg->uk_ipers < items) 42668355f576SJeff Roberson slabs++; 4267194a979eSMark Johnston while (slabs-- > 0) { 426886220393SMark Johnston aflags = M_NOWAIT; 426986220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 427086220393SMark Johnston &aflags); 427186220393SMark Johnston for (;;) { 427286220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 427386220393SMark Johnston aflags); 427486220393SMark Johnston if (slab != NULL) { 4275ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 427686220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 427786220393SMark Johnston us_link); 4278920239efSMark Johnston break; 42798355f576SJeff Roberson } 428086220393SMark Johnston KEG_LOCK(keg); 428186220393SMark Johnston if (vm_domainset_iter_policy(&di, &domain) != 0) { 428286220393SMark Johnston KEG_UNLOCK(keg); 428386220393SMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 428486220393SMark Johnston KEG_LOCK(keg); 428586220393SMark Johnston } 428686220393SMark Johnston } 428786220393SMark Johnston } 4288af526374SJeff Roberson KEG_UNLOCK(keg); 42898355f576SJeff Roberson } 42908355f576SJeff Roberson 42918355f576SJeff Roberson /* See uma.h */ 429208cfa56eSMark Johnston void 429308cfa56eSMark Johnston uma_reclaim(int req) 42948355f576SJeff Roberson { 429544ec2b63SKonstantin Belousov 42961431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 429708cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 429886bbae32SJeff Roberson bucket_enable(); 429908cfa56eSMark Johnston 430008cfa56eSMark Johnston switch (req) { 430108cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 430220a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 430308cfa56eSMark Johnston break; 430408cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 430508cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 430620a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 430708cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 430808cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 430920a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4310a2de44abSAlexander Motin } 431108cfa56eSMark Johnston break; 431208cfa56eSMark Johnston default: 431308cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 431408cfa56eSMark Johnston } 43150f9b7bf3SMark Johnston 43168355f576SJeff Roberson /* 43178355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 43188355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 43198355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 43208355f576SJeff Roberson */ 432120a4e154SJeff Roberson zone_drain(slabzone, NULL); 4322cae33c14SJeff Roberson bucket_zone_drain(); 432308cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 43248355f576SJeff Roberson } 43258355f576SJeff Roberson 43262e47807cSJeff Roberson static volatile int uma_reclaim_needed; 432744ec2b63SKonstantin Belousov 432844ec2b63SKonstantin Belousov void 432944ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 433044ec2b63SKonstantin Belousov { 433144ec2b63SKonstantin Belousov 43322e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 43332e47807cSJeff Roberson wakeup(uma_reclaim); 433444ec2b63SKonstantin Belousov } 433544ec2b63SKonstantin Belousov 433644ec2b63SKonstantin Belousov void 433744ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 433844ec2b63SKonstantin Belousov { 433944ec2b63SKonstantin Belousov 434044ec2b63SKonstantin Belousov for (;;) { 434108cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4342200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 434308cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 43442e47807cSJeff Roberson hz); 434508cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 43469b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 434708cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4348200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 43492e47807cSJeff Roberson /* Don't fire more than once per-second. */ 43502e47807cSJeff Roberson pause("umarclslp", hz); 435144ec2b63SKonstantin Belousov } 435244ec2b63SKonstantin Belousov } 435344ec2b63SKonstantin Belousov 4354663b416fSJohn Baldwin /* See uma.h */ 435508cfa56eSMark Johnston void 435608cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 435708cfa56eSMark Johnston { 435808cfa56eSMark Johnston 435908cfa56eSMark Johnston switch (req) { 436008cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 436120a4e154SJeff Roberson zone_trim(zone, NULL); 436208cfa56eSMark Johnston break; 436308cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 436420a4e154SJeff Roberson zone_drain(zone, NULL); 436508cfa56eSMark Johnston break; 436608cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 436708cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 436820a4e154SJeff Roberson zone_drain(zone, NULL); 436908cfa56eSMark Johnston break; 437008cfa56eSMark Johnston default: 437108cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 437208cfa56eSMark Johnston } 437308cfa56eSMark Johnston } 437408cfa56eSMark Johnston 437508cfa56eSMark Johnston /* See uma.h */ 4376663b416fSJohn Baldwin int 4377663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 4378663b416fSJohn Baldwin { 4379663b416fSJohn Baldwin int full; 4380663b416fSJohn Baldwin 4381663b416fSJohn Baldwin ZONE_LOCK(zone); 4382bb15d1c7SGleb Smirnoff full = zone->uz_sleepers > 0; 4383663b416fSJohn Baldwin ZONE_UNLOCK(zone); 4384663b416fSJohn Baldwin return (full); 4385663b416fSJohn Baldwin } 4386663b416fSJohn Baldwin 43876c125b8dSMohan Srinivasan int 43886c125b8dSMohan Srinivasan uma_zone_exhausted_nolock(uma_zone_t zone) 43896c125b8dSMohan Srinivasan { 4390bb15d1c7SGleb Smirnoff return (zone->uz_sleepers > 0); 43916c125b8dSMohan Srinivasan } 43926c125b8dSMohan Srinivasan 43932e47807cSJeff Roberson unsigned long 43942e47807cSJeff Roberson uma_limit(void) 43952e47807cSJeff Roberson { 43962e47807cSJeff Roberson 43972e47807cSJeff Roberson return (uma_kmem_limit); 43982e47807cSJeff Roberson } 43992e47807cSJeff Roberson 44002e47807cSJeff Roberson void 44012e47807cSJeff Roberson uma_set_limit(unsigned long limit) 44022e47807cSJeff Roberson { 44032e47807cSJeff Roberson 44042e47807cSJeff Roberson uma_kmem_limit = limit; 44052e47807cSJeff Roberson } 44062e47807cSJeff Roberson 44072e47807cSJeff Roberson unsigned long 44082e47807cSJeff Roberson uma_size(void) 44092e47807cSJeff Roberson { 44102e47807cSJeff Roberson 4411058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 4412ad5b0f5bSJeff Roberson } 4413ad5b0f5bSJeff Roberson 4414ad5b0f5bSJeff Roberson long 4415ad5b0f5bSJeff Roberson uma_avail(void) 4416ad5b0f5bSJeff Roberson { 4417ad5b0f5bSJeff Roberson 4418058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 44192e47807cSJeff Roberson } 44202e47807cSJeff Roberson 4421a0d4b0aeSRobert Watson #ifdef DDB 44228355f576SJeff Roberson /* 44237a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 44247a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 44257a52a97eSRobert Watson * 44267a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 44277a52a97eSRobert Watson * per-CPU cache statistic. 44287a52a97eSRobert Watson * 44297a52a97eSRobert Watson */ 44307a52a97eSRobert Watson static void 44310f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 4432c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 44337a52a97eSRobert Watson { 44347a52a97eSRobert Watson uma_cache_t cache; 4435c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 44367a52a97eSRobert Watson int cachefree, cpu; 44377a52a97eSRobert Watson 4438c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 44397a52a97eSRobert Watson cachefree = 0; 44403aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 44417a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 4442376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 4443376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 4444376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 4445376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 44467a52a97eSRobert Watson allocs += cache->uc_allocs; 44477a52a97eSRobert Watson frees += cache->uc_frees; 44487a52a97eSRobert Watson } 44492efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 44502efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 4451bf965959SSean Bruno sleeps += z->uz_sleeps; 4452c1685086SJeff Roberson xdomain += z->uz_xdomain; 44537a52a97eSRobert Watson if (cachefreep != NULL) 44547a52a97eSRobert Watson *cachefreep = cachefree; 44557a52a97eSRobert Watson if (allocsp != NULL) 44567a52a97eSRobert Watson *allocsp = allocs; 44577a52a97eSRobert Watson if (freesp != NULL) 44587a52a97eSRobert Watson *freesp = frees; 4459bf965959SSean Bruno if (sleepsp != NULL) 4460bf965959SSean Bruno *sleepsp = sleeps; 4461c1685086SJeff Roberson if (xdomainp != NULL) 4462c1685086SJeff Roberson *xdomainp = xdomain; 44637a52a97eSRobert Watson } 4464a0d4b0aeSRobert Watson #endif /* DDB */ 44657a52a97eSRobert Watson 44667a52a97eSRobert Watson static int 44677a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 44687a52a97eSRobert Watson { 44697a52a97eSRobert Watson uma_keg_t kz; 44707a52a97eSRobert Watson uma_zone_t z; 44717a52a97eSRobert Watson int count; 44727a52a97eSRobert Watson 44737a52a97eSRobert Watson count = 0; 4474111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 44757a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 44767a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 44777a52a97eSRobert Watson count++; 44787a52a97eSRobert Watson } 4479b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4480b47acb0aSGleb Smirnoff count++; 4481b47acb0aSGleb Smirnoff 4482111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 44837a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 44847a52a97eSRobert Watson } 44857a52a97eSRobert Watson 4486b47acb0aSGleb Smirnoff static void 4487b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 4488b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 4489b47acb0aSGleb Smirnoff { 4490b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 4491b47acb0aSGleb Smirnoff uma_cache_t cache; 4492b47acb0aSGleb Smirnoff int i; 4493b47acb0aSGleb Smirnoff 4494b47acb0aSGleb Smirnoff 4495b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 4496b47acb0aSGleb Smirnoff zdom = &z->uz_domain[i]; 4497b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 4498b47acb0aSGleb Smirnoff } 4499b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 4500b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 4501b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 4502b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 4503c1685086SJeff Roberson uth->uth_xdomain = z->uz_xdomain; 45041de9724eSMark Johnston 4505b47acb0aSGleb Smirnoff /* 45061de9724eSMark Johnston * While it is not normally safe to access the cache bucket pointers 45071de9724eSMark Johnston * while not on the CPU that owns the cache, we only allow the pointers 45081de9724eSMark Johnston * to be exchanged without the zone lock held, not invalidated, so 45091de9724eSMark Johnston * accept the possible race associated with bucket exchange during 45101de9724eSMark Johnston * monitoring. Use atomic_load_ptr() to ensure that the bucket pointers 45111de9724eSMark Johnston * are loaded only once. 4512b47acb0aSGleb Smirnoff */ 4513b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 4514b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 4515b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 4516b47acb0aSGleb Smirnoff continue; 4517b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 4518376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 4519376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 4520376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 4521b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 4522b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 4523b47acb0aSGleb Smirnoff } 4524b47acb0aSGleb Smirnoff } 4525b47acb0aSGleb Smirnoff 45267a52a97eSRobert Watson static int 45277a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 45287a52a97eSRobert Watson { 45297a52a97eSRobert Watson struct uma_stream_header ush; 45307a52a97eSRobert Watson struct uma_type_header uth; 453163b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 45327a52a97eSRobert Watson struct sbuf sbuf; 45337a52a97eSRobert Watson uma_keg_t kz; 45347a52a97eSRobert Watson uma_zone_t z; 4535*4bd61e19SJeff Roberson uint64_t items; 45364e657159SMatthew D Fleming int count, error, i; 45377a52a97eSRobert Watson 453800f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 453900f0e671SMatthew D Fleming if (error != 0) 454000f0e671SMatthew D Fleming return (error); 45414e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 45421eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 454363b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 45444e657159SMatthew D Fleming 4545404a593eSMatthew D Fleming count = 0; 4546111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 45477a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 45487a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 45497a52a97eSRobert Watson count++; 45507a52a97eSRobert Watson } 45517a52a97eSRobert Watson 4552b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4553b47acb0aSGleb Smirnoff count++; 4554b47acb0aSGleb Smirnoff 45557a52a97eSRobert Watson /* 45567a52a97eSRobert Watson * Insert stream header. 45577a52a97eSRobert Watson */ 45587a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 45597a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 4560ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 45617a52a97eSRobert Watson ush.ush_count = count; 45624e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 45637a52a97eSRobert Watson 45647a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 45657a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 45667a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 45677a52a97eSRobert Watson ZONE_LOCK(z); 4568cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 45697a52a97eSRobert Watson uth.uth_align = kz->uk_align; 45707a52a97eSRobert Watson uth.uth_size = kz->uk_size; 45717a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 4572*4bd61e19SJeff Roberson if (z->uz_max_items > 0) { 4573*4bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 4574*4bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 4575bb15d1c7SGleb Smirnoff kz->uk_ppera; 4576*4bd61e19SJeff Roberson } else 4577bb45b411SGleb Smirnoff uth.uth_pages = kz->uk_pages; 4578f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 4579bb15d1c7SGleb Smirnoff kz->uk_ppera; 4580bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 4581f8c86a5fSGleb Smirnoff uth.uth_keg_free = z->uz_keg->uk_free; 4582cbbb4a00SRobert Watson 4583cbbb4a00SRobert Watson /* 4584cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 4585cbbb4a00SRobert Watson * on the keg's zone list. 4586cbbb4a00SRobert Watson */ 4587e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 4588cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 4589cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 4590b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 4591b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 45922450bbb8SRobert Watson ZONE_UNLOCK(z); 459363b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 459463b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 459563b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 45967a52a97eSRobert Watson } 45977a52a97eSRobert Watson } 4598b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 4599b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 4600b47acb0aSGleb Smirnoff ZONE_LOCK(z); 4601b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 4602b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 4603b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 4604b47acb0aSGleb Smirnoff ZONE_UNLOCK(z); 4605b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 4606b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 4607b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 4608b47acb0aSGleb Smirnoff } 4609b47acb0aSGleb Smirnoff 4610111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 46114e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 46124e657159SMatthew D Fleming sbuf_delete(&sbuf); 461363b5d112SKonstantin Belousov free(ups, M_TEMP); 46147a52a97eSRobert Watson return (error); 46157a52a97eSRobert Watson } 461648c5777eSRobert Watson 46170a5a3ccbSGleb Smirnoff int 46180a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 46190a5a3ccbSGleb Smirnoff { 46200a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 462116be9f54SGleb Smirnoff int error, max; 46220a5a3ccbSGleb Smirnoff 462316be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 46240a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 46250a5a3ccbSGleb Smirnoff if (error || !req->newptr) 46260a5a3ccbSGleb Smirnoff return (error); 46270a5a3ccbSGleb Smirnoff 46280a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 46290a5a3ccbSGleb Smirnoff 46300a5a3ccbSGleb Smirnoff return (0); 46310a5a3ccbSGleb Smirnoff } 46320a5a3ccbSGleb Smirnoff 46330a5a3ccbSGleb Smirnoff int 46340a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 46350a5a3ccbSGleb Smirnoff { 463620a4e154SJeff Roberson uma_zone_t zone; 46370a5a3ccbSGleb Smirnoff int cur; 46380a5a3ccbSGleb Smirnoff 463920a4e154SJeff Roberson /* 464020a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 464120a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 464220a4e154SJeff Roberson */ 464320a4e154SJeff Roberson if (arg2 == 0) 464420a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 464520a4e154SJeff Roberson else 464620a4e154SJeff Roberson zone = arg1; 46470a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 46480a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 46490a5a3ccbSGleb Smirnoff } 46500a5a3ccbSGleb Smirnoff 465120a4e154SJeff Roberson static int 465220a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 465320a4e154SJeff Roberson { 465420a4e154SJeff Roberson uma_zone_t zone = arg1; 465520a4e154SJeff Roberson uint64_t cur; 465620a4e154SJeff Roberson 465720a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 465820a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 465920a4e154SJeff Roberson } 466020a4e154SJeff Roberson 466120a4e154SJeff Roberson static int 466220a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 466320a4e154SJeff Roberson { 466420a4e154SJeff Roberson uma_zone_t zone = arg1; 466520a4e154SJeff Roberson uint64_t cur; 466620a4e154SJeff Roberson 466720a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 466820a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 466920a4e154SJeff Roberson } 467020a4e154SJeff Roberson 46716d204a6aSRyan Libby static int 46726d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 46736d204a6aSRyan Libby { 46746d204a6aSRyan Libby struct sbuf sbuf; 46756d204a6aSRyan Libby uma_zone_t zone = arg1; 46766d204a6aSRyan Libby int error; 46776d204a6aSRyan Libby 46786d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 46796d204a6aSRyan Libby if (zone->uz_flags != 0) 46806d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 46816d204a6aSRyan Libby else 46826d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 46836d204a6aSRyan Libby error = sbuf_finish(&sbuf); 46846d204a6aSRyan Libby sbuf_delete(&sbuf); 46856d204a6aSRyan Libby 46866d204a6aSRyan Libby return (error); 46876d204a6aSRyan Libby } 46886d204a6aSRyan Libby 4689f7af5015SRyan Libby static int 4690f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 4691f7af5015SRyan Libby { 4692f7af5015SRyan Libby uma_keg_t keg = arg1; 4693f7af5015SRyan Libby int avail, effpct, total; 4694f7af5015SRyan Libby 4695f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 4696f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_OFFPAGE) != 0) 4697f7af5015SRyan Libby total += slab_sizeof(SLAB_MAX_SETSIZE); 4698f7af5015SRyan Libby /* 4699f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 4700f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 4701f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 4702f7af5015SRyan Libby */ 4703f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 4704f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 4705f7af5015SRyan Libby avail *= mp_maxid + 1; 4706f7af5015SRyan Libby effpct = 100 * avail / total; 4707f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 4708f7af5015SRyan Libby } 4709f7af5015SRyan Libby 4710*4bd61e19SJeff Roberson static int 4711*4bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 4712*4bd61e19SJeff Roberson { 4713*4bd61e19SJeff Roberson uma_zone_t zone = arg1; 4714*4bd61e19SJeff Roberson uint64_t cur; 4715*4bd61e19SJeff Roberson 4716*4bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 4717*4bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 4718*4bd61e19SJeff Roberson } 4719*4bd61e19SJeff Roberson 47209542ea7bSGleb Smirnoff #ifdef INVARIANTS 47219542ea7bSGleb Smirnoff static uma_slab_t 47229542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 47239542ea7bSGleb Smirnoff { 47249542ea7bSGleb Smirnoff uma_slab_t slab; 47259542ea7bSGleb Smirnoff uma_keg_t keg; 47269542ea7bSGleb Smirnoff uint8_t *mem; 47279542ea7bSGleb Smirnoff 47289542ea7bSGleb Smirnoff mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 47299542ea7bSGleb Smirnoff if (zone->uz_flags & UMA_ZONE_VTOSLAB) { 47309542ea7bSGleb Smirnoff slab = vtoslab((vm_offset_t)mem); 47319542ea7bSGleb Smirnoff } else { 47329542ea7bSGleb Smirnoff /* 47339542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 47349542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 47359542ea7bSGleb Smirnoff * essentially holds a reference. 47369542ea7bSGleb Smirnoff */ 4737bb15d1c7SGleb Smirnoff if (zone->uz_lockptr == &zone->uz_lock) 4738bb15d1c7SGleb Smirnoff return (NULL); 47399542ea7bSGleb Smirnoff ZONE_LOCK(zone); 4740bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 47419542ea7bSGleb Smirnoff if (keg->uk_flags & UMA_ZONE_HASH) 47429542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 47439542ea7bSGleb Smirnoff else 47449542ea7bSGleb Smirnoff slab = (uma_slab_t)(mem + keg->uk_pgoff); 47459542ea7bSGleb Smirnoff ZONE_UNLOCK(zone); 47469542ea7bSGleb Smirnoff } 47479542ea7bSGleb Smirnoff 47489542ea7bSGleb Smirnoff return (slab); 47499542ea7bSGleb Smirnoff } 47509542ea7bSGleb Smirnoff 4751c5deaf04SGleb Smirnoff static bool 4752c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 4753c5deaf04SGleb Smirnoff { 4754c5deaf04SGleb Smirnoff 4755bb15d1c7SGleb Smirnoff if (zone->uz_lockptr == &zone->uz_lock) 4756c5deaf04SGleb Smirnoff return (true); 4757c5deaf04SGleb Smirnoff 4758bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 4759c5deaf04SGleb Smirnoff } 4760c5deaf04SGleb Smirnoff 4761c5deaf04SGleb Smirnoff static bool 4762c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 4763c5deaf04SGleb Smirnoff { 4764c5deaf04SGleb Smirnoff uintptr_t idx; 4765c5deaf04SGleb Smirnoff 4766c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 4767c5deaf04SGleb Smirnoff return (true); 4768c5deaf04SGleb Smirnoff 4769c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 4770c5deaf04SGleb Smirnoff return (false); 4771c5deaf04SGleb Smirnoff 4772c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 4773c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 4774c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 4775c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 4776c5deaf04SGleb Smirnoff } 4777c5deaf04SGleb Smirnoff 4778c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 4779c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 4780c5deaf04SGleb Smirnoff return (true); 4781c5deaf04SGleb Smirnoff } 4782c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 4783c5deaf04SGleb Smirnoff 4784c5deaf04SGleb Smirnoff return (false); 4785c5deaf04SGleb Smirnoff } 4786c5deaf04SGleb Smirnoff 47879542ea7bSGleb Smirnoff /* 47889542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 47899542ea7bSGleb Smirnoff * 47909542ea7bSGleb Smirnoff */ 47919542ea7bSGleb Smirnoff static void 47929542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 47939542ea7bSGleb Smirnoff { 47949542ea7bSGleb Smirnoff uma_keg_t keg; 47959542ea7bSGleb Smirnoff int freei; 47969542ea7bSGleb Smirnoff 47979542ea7bSGleb Smirnoff if (slab == NULL) { 47989542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 47999542ea7bSGleb Smirnoff if (slab == NULL) 48009542ea7bSGleb Smirnoff panic("uma: item %p did not belong to zone %s\n", 48019542ea7bSGleb Smirnoff item, zone->uz_name); 48029542ea7bSGleb Smirnoff } 4803584061b4SJeff Roberson keg = zone->uz_keg; 48041e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 48059542ea7bSGleb Smirnoff 4806815db204SRyan Libby if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 48079542ea7bSGleb Smirnoff panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n", 48089542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 4809815db204SRyan Libby BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 48109542ea7bSGleb Smirnoff } 48119542ea7bSGleb Smirnoff 48129542ea7bSGleb Smirnoff /* 48139542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 48149542ea7bSGleb Smirnoff * and duplicate frees. 48159542ea7bSGleb Smirnoff * 48169542ea7bSGleb Smirnoff */ 48179542ea7bSGleb Smirnoff static void 48189542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 48199542ea7bSGleb Smirnoff { 48209542ea7bSGleb Smirnoff uma_keg_t keg; 48219542ea7bSGleb Smirnoff int freei; 48229542ea7bSGleb Smirnoff 48239542ea7bSGleb Smirnoff if (slab == NULL) { 48249542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 48259542ea7bSGleb Smirnoff if (slab == NULL) 48269542ea7bSGleb Smirnoff panic("uma: Freed item %p did not belong to zone %s\n", 48279542ea7bSGleb Smirnoff item, zone->uz_name); 48289542ea7bSGleb Smirnoff } 4829584061b4SJeff Roberson keg = zone->uz_keg; 48301e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 48319542ea7bSGleb Smirnoff 48329542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 48339542ea7bSGleb Smirnoff panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n", 48349542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48359542ea7bSGleb Smirnoff 48361e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 48379542ea7bSGleb Smirnoff panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n", 48389542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48399542ea7bSGleb Smirnoff 4840815db204SRyan Libby if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 48419542ea7bSGleb Smirnoff panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n", 48429542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48439542ea7bSGleb Smirnoff 4844815db204SRyan Libby BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 48459542ea7bSGleb Smirnoff } 48469542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 48479542ea7bSGleb Smirnoff 484848c5777eSRobert Watson #ifdef DDB 484946d70077SConrad Meyer static int64_t 485046d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 48510223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 485248c5777eSRobert Watson { 485346d70077SConrad Meyer uint64_t frees; 48540f9b7bf3SMark Johnston int i; 485548c5777eSRobert Watson 485648c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 485746d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 48582efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 485946d70077SConrad Meyer *sleeps = z->uz_sleeps; 486046d70077SConrad Meyer *cachefree = 0; 486146d70077SConrad Meyer *xdomain = 0; 486248c5777eSRobert Watson } else 486346d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 486446d70077SConrad Meyer xdomain); 4865e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 486648c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 486746d70077SConrad Meyer *cachefree += kz->uk_free; 48680f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 486946d70077SConrad Meyer *cachefree += z->uz_domain[i].uzd_nitems; 487046d70077SConrad Meyer *used = *allocs - frees; 487146d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 487246d70077SConrad Meyer } 48730f9b7bf3SMark Johnston 487446d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 487546d70077SConrad Meyer { 487646d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 487746d70077SConrad Meyer uma_keg_t kz; 487846d70077SConrad Meyer uma_zone_t z; 487946d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 488046d70077SConrad Meyer long cachefree; 488146d70077SConrad Meyer /* variables for sorting */ 488246d70077SConrad Meyer uma_keg_t cur_keg; 488346d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 488446d70077SConrad Meyer int64_t cur_size, last_size, size; 488546d70077SConrad Meyer int ties; 488646d70077SConrad Meyer 488746d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 488846d70077SConrad Meyer if (modif[0] == 'i') { 488946d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 489046d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 489146d70077SConrad Meyer } else { 489246d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 489346d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 489446d70077SConrad Meyer } 489546d70077SConrad Meyer 489646d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 489746d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 489846d70077SConrad Meyer 489946d70077SConrad Meyer /* Sort the zones with largest size first. */ 490046d70077SConrad Meyer last_zone = NULL; 490146d70077SConrad Meyer last_size = INT64_MAX; 490246d70077SConrad Meyer for (;;) { 490346d70077SConrad Meyer cur_zone = NULL; 490446d70077SConrad Meyer cur_size = -1; 490546d70077SConrad Meyer ties = 0; 490646d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 490746d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 490846d70077SConrad Meyer /* 490946d70077SConrad Meyer * In the case of size ties, print out zones 491046d70077SConrad Meyer * in the order they are encountered. That is, 491146d70077SConrad Meyer * when we encounter the most recently output 491246d70077SConrad Meyer * zone, we have already printed all preceding 491346d70077SConrad Meyer * ties, and we must print all following ties. 491446d70077SConrad Meyer */ 491546d70077SConrad Meyer if (z == last_zone) { 491646d70077SConrad Meyer ties = 1; 491746d70077SConrad Meyer continue; 491846d70077SConrad Meyer } 491946d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 492046d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 492146d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 492246d70077SConrad Meyer { 492346d70077SConrad Meyer cur_size = size; 492446d70077SConrad Meyer cur_zone = z; 492546d70077SConrad Meyer cur_keg = kz; 492646d70077SConrad Meyer } 492746d70077SConrad Meyer } 492846d70077SConrad Meyer } 492946d70077SConrad Meyer if (cur_zone == NULL) 493046d70077SConrad Meyer break; 493146d70077SConrad Meyer 493246d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 493346d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 493446d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 493546d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 493646d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 493720a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 493820a4e154SJeff Roberson xdomain); 493946d70077SConrad Meyer 4940687c94aaSJohn Baldwin if (db_pager_quit) 4941687c94aaSJohn Baldwin return; 494246d70077SConrad Meyer last_zone = cur_zone; 494346d70077SConrad Meyer last_size = cur_size; 494448c5777eSRobert Watson } 494548c5777eSRobert Watson } 494603175483SAlexander Motin 494703175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 494803175483SAlexander Motin { 494903175483SAlexander Motin uma_zone_t z; 4950ab3185d1SJeff Roberson uint64_t allocs, frees; 49510f9b7bf3SMark Johnston long cachefree; 49520f9b7bf3SMark Johnston int i; 495303175483SAlexander Motin 495403175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 495503175483SAlexander Motin "Requests", "Bucket"); 495603175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 4957c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 49580f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 49590f9b7bf3SMark Johnston cachefree += z->uz_domain[i].uzd_nitems; 49600f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 496103175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 496203175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 496320a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 496403175483SAlexander Motin if (db_pager_quit) 496503175483SAlexander Motin return; 496603175483SAlexander Motin } 496703175483SAlexander Motin } 49689542ea7bSGleb Smirnoff #endif /* DDB */ 4969