160727d8bSWarner Losh /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 4584061b4SJeff Roberson * Copyright (c) 2002-2019 Jeffrey Roberson <jeff@FreeBSD.org> 508ecce74SRobert Watson * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org> 6ae4e9636SRobert Watson * Copyright (c) 2004-2006 Robert N. M. Watson 708ecce74SRobert Watson * All rights reserved. 88355f576SJeff Roberson * 98355f576SJeff Roberson * Redistribution and use in source and binary forms, with or without 108355f576SJeff Roberson * modification, are permitted provided that the following conditions 118355f576SJeff Roberson * are met: 128355f576SJeff Roberson * 1. Redistributions of source code must retain the above copyright 138355f576SJeff Roberson * notice unmodified, this list of conditions, and the following 148355f576SJeff Roberson * disclaimer. 158355f576SJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 168355f576SJeff Roberson * notice, this list of conditions and the following disclaimer in the 178355f576SJeff Roberson * documentation and/or other materials provided with the distribution. 188355f576SJeff Roberson * 198355f576SJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 208355f576SJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 218355f576SJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 228355f576SJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 238355f576SJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 248355f576SJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 258355f576SJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 268355f576SJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 278355f576SJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 288355f576SJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 298355f576SJeff Roberson */ 308355f576SJeff Roberson 318355f576SJeff Roberson /* 328355f576SJeff Roberson * uma_core.c Implementation of the Universal Memory allocator 338355f576SJeff Roberson * 348355f576SJeff Roberson * This allocator is intended to replace the multitude of similar object caches 358355f576SJeff Roberson * in the standard FreeBSD kernel. The intent is to be flexible as well as 36763df3ecSPedro F. Giffuni * efficient. A primary design goal is to return unused memory to the rest of 378355f576SJeff Roberson * the system. This will make the system as a whole more flexible due to the 388355f576SJeff Roberson * ability to move memory to subsystems which most need it instead of leaving 398355f576SJeff Roberson * pools of reserved memory unused. 408355f576SJeff Roberson * 418355f576SJeff Roberson * The basic ideas stem from similar slab/zone based allocators whose algorithms 428355f576SJeff Roberson * are well known. 438355f576SJeff Roberson * 448355f576SJeff Roberson */ 458355f576SJeff Roberson 468355f576SJeff Roberson /* 478355f576SJeff Roberson * TODO: 488355f576SJeff Roberson * - Improve memory usage for large allocations 498355f576SJeff Roberson * - Investigate cache size adjustments 508355f576SJeff Roberson */ 518355f576SJeff Roberson 52874651b1SDavid E. O'Brien #include <sys/cdefs.h> 53874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 54874651b1SDavid E. O'Brien 5548c5777eSRobert Watson #include "opt_ddb.h" 568355f576SJeff Roberson #include "opt_param.h" 578d689e04SGleb Smirnoff #include "opt_vm.h" 5848c5777eSRobert Watson 598355f576SJeff Roberson #include <sys/param.h> 608355f576SJeff Roberson #include <sys/systm.h> 61ef72505eSJeff Roberson #include <sys/bitset.h> 62194a979eSMark Johnston #include <sys/domainset.h> 639b43bc27SAndriy Gapon #include <sys/eventhandler.h> 648355f576SJeff Roberson #include <sys/kernel.h> 658355f576SJeff Roberson #include <sys/types.h> 66ad5b0f5bSJeff Roberson #include <sys/limits.h> 678355f576SJeff Roberson #include <sys/queue.h> 688355f576SJeff Roberson #include <sys/malloc.h> 693659f747SRobert Watson #include <sys/ktr.h> 708355f576SJeff Roberson #include <sys/lock.h> 718355f576SJeff Roberson #include <sys/sysctl.h> 728355f576SJeff Roberson #include <sys/mutex.h> 734c1cc01cSJohn Baldwin #include <sys/proc.h> 7410cb2424SMark Murray #include <sys/random.h> 7589f6b863SAttilio Rao #include <sys/rwlock.h> 767a52a97eSRobert Watson #include <sys/sbuf.h> 77a2de44abSAlexander Motin #include <sys/sched.h> 784bd61e19SJeff Roberson #include <sys/sleepqueue.h> 798355f576SJeff Roberson #include <sys/smp.h> 80e60b2fcbSGleb Smirnoff #include <sys/taskqueue.h> 8186bbae32SJeff Roberson #include <sys/vmmeter.h> 8286bbae32SJeff Roberson 838355f576SJeff Roberson #include <vm/vm.h> 84194a979eSMark Johnston #include <vm/vm_domainset.h> 858355f576SJeff Roberson #include <vm/vm_object.h> 868355f576SJeff Roberson #include <vm/vm_page.h> 87a4915c21SAttilio Rao #include <vm/vm_pageout.h> 888355f576SJeff Roberson #include <vm/vm_param.h> 89ab3185d1SJeff Roberson #include <vm/vm_phys.h> 9030c5525bSAndrew Gallatin #include <vm/vm_pagequeue.h> 918355f576SJeff Roberson #include <vm/vm_map.h> 928355f576SJeff Roberson #include <vm/vm_kern.h> 938355f576SJeff Roberson #include <vm/vm_extern.h> 948355f576SJeff Roberson #include <vm/uma.h> 958355f576SJeff Roberson #include <vm/uma_int.h> 96639c9550SJeff Roberson #include <vm/uma_dbg.h> 978355f576SJeff Roberson 9848c5777eSRobert Watson #include <ddb/ddb.h> 9948c5777eSRobert Watson 1008d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 1018d689e04SGleb Smirnoff #include <vm/memguard.h> 1028d689e04SGleb Smirnoff #endif 1038d689e04SGleb Smirnoff 1048355f576SJeff Roberson /* 105ab3185d1SJeff Roberson * This is the zone and keg from which all zones are spawned. 1068355f576SJeff Roberson */ 107ab3185d1SJeff Roberson static uma_zone_t kegs; 108ab3185d1SJeff Roberson static uma_zone_t zones; 1098355f576SJeff Roberson 110ab3185d1SJeff Roberson /* This is the zone from which all offpage uma_slab_ts are allocated. */ 1118355f576SJeff Roberson static uma_zone_t slabzone; 1128355f576SJeff Roberson 1138355f576SJeff Roberson /* 1148355f576SJeff Roberson * The initial hash tables come out of this zone so they can be allocated 1158355f576SJeff Roberson * prior to malloc coming up. 1168355f576SJeff Roberson */ 1178355f576SJeff Roberson static uma_zone_t hashzone; 1188355f576SJeff Roberson 1191e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */ 120e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1; 1211e319f6dSRobert Watson 122961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets"); 12320a4e154SJeff Roberson static MALLOC_DEFINE(M_UMA, "UMA", "UMA Misc"); 124961647dfSJeff Roberson 1258355f576SJeff Roberson /* 12686bbae32SJeff Roberson * Are we allowed to allocate buckets? 12786bbae32SJeff Roberson */ 12886bbae32SJeff Roberson static int bucketdisable = 1; 12986bbae32SJeff Roberson 130099a0e58SBosko Milekic /* Linked list of all kegs in the system */ 13113e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs); 1328355f576SJeff Roberson 13303175483SAlexander Motin /* Linked list of all cache-only zones in the system */ 13403175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones = 13503175483SAlexander Motin LIST_HEAD_INITIALIZER(uma_cachezones); 13603175483SAlexander Motin 137111fbcd5SBryan Venteicher /* This RW lock protects the keg list */ 138fe933c1dSMateusz Guzik static struct rwlock_padalign __exclusive_cache_line uma_rwlock; 1398355f576SJeff Roberson 140ac0a6fd0SGleb Smirnoff /* 141ac0a6fd0SGleb Smirnoff * Pointer and counter to pool of pages, that is preallocated at 142f7d35785SGleb Smirnoff * startup to bootstrap UMA. 143ac0a6fd0SGleb Smirnoff */ 144ac0a6fd0SGleb Smirnoff static char *bootmem; 145ac0a6fd0SGleb Smirnoff static int boot_pages; 1468355f576SJeff Roberson 14708cfa56eSMark Johnston static struct sx uma_reclaim_lock; 14895c4bf75SKonstantin Belousov 149fbd95859SMark Johnston /* 150fbd95859SMark Johnston * kmem soft limit, initialized by uma_set_limit(). Ensure that early 151fbd95859SMark Johnston * allocations don't trigger a wakeup of the reclaim thread. 152fbd95859SMark Johnston */ 1536d6a03d7SJeff Roberson unsigned long uma_kmem_limit = LONG_MAX; 154fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_limit, CTLFLAG_RD, &uma_kmem_limit, 0, 155fbd95859SMark Johnston "UMA kernel memory soft limit"); 1566d6a03d7SJeff Roberson unsigned long uma_kmem_total; 157fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_total, CTLFLAG_RD, &uma_kmem_total, 0, 158fbd95859SMark Johnston "UMA kernel memory usage"); 1592e47807cSJeff Roberson 1608355f576SJeff Roberson /* Is the VM done starting up? */ 161f4bef67cSGleb Smirnoff static enum { BOOT_COLD = 0, BOOT_STRAPPED, BOOT_PAGEALLOC, BOOT_BUCKETS, 162f4bef67cSGleb Smirnoff BOOT_RUNNING } booted = BOOT_COLD; 1638355f576SJeff Roberson 164ef72505eSJeff Roberson /* 1659643769aSJeff Roberson * This is the handle used to schedule events that need to happen 1669643769aSJeff Roberson * outside of the allocation fast path. 1679643769aSJeff Roberson */ 1688355f576SJeff Roberson static struct callout uma_callout; 1699643769aSJeff Roberson #define UMA_TIMEOUT 20 /* Seconds for callout interval. */ 1708355f576SJeff Roberson 1718355f576SJeff Roberson /* 1728355f576SJeff Roberson * This structure is passed as the zone ctor arg so that I don't have to create 1738355f576SJeff Roberson * a special allocation function just for zones. 1748355f576SJeff Roberson */ 1758355f576SJeff Roberson struct uma_zctor_args { 176bb196eb4SMatthew D Fleming const char *name; 177c3bdc05fSAndrew R. Reiter size_t size; 1788355f576SJeff Roberson uma_ctor ctor; 1798355f576SJeff Roberson uma_dtor dtor; 1808355f576SJeff Roberson uma_init uminit; 1818355f576SJeff Roberson uma_fini fini; 1820095a784SJeff Roberson uma_import import; 1830095a784SJeff Roberson uma_release release; 1840095a784SJeff Roberson void *arg; 185099a0e58SBosko Milekic uma_keg_t keg; 186099a0e58SBosko Milekic int align; 18785dcf349SGleb Smirnoff uint32_t flags; 188099a0e58SBosko Milekic }; 189099a0e58SBosko Milekic 190099a0e58SBosko Milekic struct uma_kctor_args { 191099a0e58SBosko Milekic uma_zone_t zone; 192099a0e58SBosko Milekic size_t size; 193099a0e58SBosko Milekic uma_init uminit; 194099a0e58SBosko Milekic uma_fini fini; 1958355f576SJeff Roberson int align; 19685dcf349SGleb Smirnoff uint32_t flags; 1978355f576SJeff Roberson }; 1988355f576SJeff Roberson 199cae33c14SJeff Roberson struct uma_bucket_zone { 200cae33c14SJeff Roberson uma_zone_t ubz_zone; 201cae33c14SJeff Roberson char *ubz_name; 202fc03d22bSJeff Roberson int ubz_entries; /* Number of items it can hold. */ 203fc03d22bSJeff Roberson int ubz_maxsize; /* Maximum allocation size per-item. */ 204cae33c14SJeff Roberson }; 205cae33c14SJeff Roberson 206f9d27e75SRobert Watson /* 207fc03d22bSJeff Roberson * Compute the actual number of bucket entries to pack them in power 208fc03d22bSJeff Roberson * of two sizes for more efficient space utilization. 209f9d27e75SRobert Watson */ 210fc03d22bSJeff Roberson #define BUCKET_SIZE(n) \ 211fc03d22bSJeff Roberson (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *)) 212fc03d22bSJeff Roberson 2131aa6c758SAlexander Motin #define BUCKET_MAX BUCKET_SIZE(256) 214eda1b016SJeff Roberson #define BUCKET_MIN BUCKET_SIZE(4) 215fc03d22bSJeff Roberson 216fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = { 2176fd34d6fSJeff Roberson { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 }, 218f3932e90SAlexander Motin { NULL, "6 Bucket", BUCKET_SIZE(6), 3072 }, 2196fd34d6fSJeff Roberson { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 }, 220f3932e90SAlexander Motin { NULL, "12 Bucket", BUCKET_SIZE(12), 1536 }, 2216fd34d6fSJeff Roberson { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 }, 222fc03d22bSJeff Roberson { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 223fc03d22bSJeff Roberson { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 224fc03d22bSJeff Roberson { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 2251aa6c758SAlexander Motin { NULL, "256 Bucket", BUCKET_SIZE(256), 64 }, 226fc03d22bSJeff Roberson { NULL, NULL, 0} 227fc03d22bSJeff Roberson }; 228cae33c14SJeff Roberson 2292019094aSRobert Watson /* 2302019094aSRobert Watson * Flags and enumerations to be passed to internal functions. 2312019094aSRobert Watson */ 232bb15d1c7SGleb Smirnoff enum zfreeskip { 233bb15d1c7SGleb Smirnoff SKIP_NONE = 0, 234bb15d1c7SGleb Smirnoff SKIP_CNT = 0x00000001, 235bb15d1c7SGleb Smirnoff SKIP_DTOR = 0x00010000, 236bb15d1c7SGleb Smirnoff SKIP_FINI = 0x00020000, 237bb15d1c7SGleb Smirnoff }; 238b23f72e9SBrian Feldman 2398355f576SJeff Roberson /* Prototypes.. */ 2408355f576SJeff Roberson 241f4bef67cSGleb Smirnoff int uma_startup_count(int); 242f4bef67cSGleb Smirnoff void uma_startup(void *, int); 243f4bef67cSGleb Smirnoff void uma_startup1(void); 244f4bef67cSGleb Smirnoff void uma_startup2(void); 245f4bef67cSGleb Smirnoff 246ab3185d1SJeff Roberson static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 247ab3185d1SJeff Roberson static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 248ab3059a8SMatt Macy static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 249ab3185d1SJeff Roberson static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 250f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t); 251ab3059a8SMatt Macy static void pcpu_page_free(void *, vm_size_t, uint8_t); 25286220393SMark Johnston static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int); 2539643769aSJeff Roberson static void cache_drain(uma_zone_t); 2548355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t); 25508cfa56eSMark Johnston static void bucket_cache_reclaim(uma_zone_t zone, bool); 256b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int); 257099a0e58SBosko Milekic static void keg_dtor(void *, int, void *); 258b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int); 2599c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *); 260b23f72e9SBrian Feldman static int zero_init(void *, int, int); 26120a4e154SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t, void *), void *); 26220a4e154SJeff Roberson static void zone_timeout(uma_zone_t zone, void *); 2633b2f2cb8SAlexander Motin static int hash_alloc(struct uma_hash *, u_int); 2640aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *); 2650aef6126SJeff Roberson static void hash_free(struct uma_hash *hash); 2668355f576SJeff Roberson static void uma_timeout(void *); 2678355f576SJeff Roberson static void uma_startup3(void); 268ab3185d1SJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int, int); 2690095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 2704bd61e19SJeff Roberson static int zone_alloc_limit(uma_zone_t zone, int count, int flags); 2714bd61e19SJeff Roberson static void zone_free_limit(uma_zone_t zone, int count); 27286bbae32SJeff Roberson static void bucket_enable(void); 273cae33c14SJeff Roberson static void bucket_init(void); 2746fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int); 2756fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *); 276cae33c14SJeff Roberson static void bucket_zone_drain(void); 277beb8beefSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int); 2780095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 279bb15d1c7SGleb Smirnoff static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item); 280e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 28185dcf349SGleb Smirnoff uma_fini fini, int align, uint32_t flags); 282b75c4efcSAndrew Turner static int zone_import(void *, void **, int, int, int); 283b75c4efcSAndrew Turner static void zone_release(void *, void **, int); 284beb8beefSJeff Roberson static bool cache_alloc(uma_zone_t, uma_cache_t, void *, int); 2850a81b439SJeff Roberson static bool cache_free(uma_zone_t, uma_cache_t, void *, void *, int); 286bbee39c6SJeff Roberson 2877a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 2887a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 28920a4e154SJeff Roberson static int sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS); 29020a4e154SJeff Roberson static int sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS); 2916d204a6aSRyan Libby static int sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS); 292f7af5015SRyan Libby static int sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS); 2934bd61e19SJeff Roberson static int sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS); 2948355f576SJeff Roberson 29531c251a0SJeff Roberson static uint64_t uma_zone_get_allocs(uma_zone_t zone); 29631c251a0SJeff Roberson 2979542ea7bSGleb Smirnoff #ifdef INVARIANTS 29831c251a0SJeff Roberson static uint64_t uma_keg_get_allocs(uma_keg_t zone); 299815db204SRyan Libby static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg); 300815db204SRyan Libby 301c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem); 302c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem); 3039542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item); 3049542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item); 305c5deaf04SGleb Smirnoff 306c5deaf04SGleb Smirnoff static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0, 307c5deaf04SGleb Smirnoff "Memory allocation debugging"); 308c5deaf04SGleb Smirnoff 309c5deaf04SGleb Smirnoff static u_int dbg_divisor = 1; 310c5deaf04SGleb Smirnoff SYSCTL_UINT(_vm_debug, OID_AUTO, divisor, 311c5deaf04SGleb Smirnoff CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0, 312c5deaf04SGleb Smirnoff "Debug & thrash every this item in memory allocator"); 313c5deaf04SGleb Smirnoff 314c5deaf04SGleb Smirnoff static counter_u64_t uma_dbg_cnt = EARLY_COUNTER; 315c5deaf04SGleb Smirnoff static counter_u64_t uma_skip_cnt = EARLY_COUNTER; 316c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD, 317c5deaf04SGleb Smirnoff &uma_dbg_cnt, "memory items debugged"); 318c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD, 319c5deaf04SGleb Smirnoff &uma_skip_cnt, "memory items skipped, not debugged"); 3209542ea7bSGleb Smirnoff #endif 3219542ea7bSGleb Smirnoff 3228355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 3238355f576SJeff Roberson 32435ec24f3SRyan Libby SYSCTL_NODE(_vm, OID_AUTO, uma, CTLFLAG_RW, 0, "Universal Memory Allocator"); 32535ec24f3SRyan Libby 3267a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT, 3277a52a97eSRobert Watson 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 3287a52a97eSRobert Watson 3297a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 3307a52a97eSRobert Watson 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 3317a52a97eSRobert Watson 3322f891cd5SPawel Jakub Dawidek static int zone_warnings = 1; 333af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0, 3342f891cd5SPawel Jakub Dawidek "Warn when UMA zones becomes full"); 3352f891cd5SPawel Jakub Dawidek 33686bbae32SJeff Roberson /* 33786bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 33886bbae32SJeff Roberson */ 33986bbae32SJeff Roberson static void 34086bbae32SJeff Roberson bucket_enable(void) 34186bbae32SJeff Roberson { 3423182660aSRyan Libby 3433182660aSRyan Libby KASSERT(booted >= BOOT_BUCKETS, ("Bucket enable before init")); 344251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 34586bbae32SJeff Roberson } 34686bbae32SJeff Roberson 347dc2c7965SRobert Watson /* 348dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 349dc2c7965SRobert Watson * 350dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 351fc03d22bSJeff Roberson * of the header and an array of pointers. 352dc2c7965SRobert Watson */ 353cae33c14SJeff Roberson static void 354cae33c14SJeff Roberson bucket_init(void) 355cae33c14SJeff Roberson { 356cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 357cae33c14SJeff Roberson int size; 358cae33c14SJeff Roberson 359d74e6a1dSAlan Cox for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 360cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 361cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 362cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 363e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 364dfe13344SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | 365dfe13344SJeff Roberson UMA_ZONE_FIRSTTOUCH); 366cae33c14SJeff Roberson } 367cae33c14SJeff Roberson } 368cae33c14SJeff Roberson 369dc2c7965SRobert Watson /* 370dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 371dc2c7965SRobert Watson * to allocate the bucket. 372dc2c7965SRobert Watson */ 373dc2c7965SRobert Watson static struct uma_bucket_zone * 374dc2c7965SRobert Watson bucket_zone_lookup(int entries) 375dc2c7965SRobert Watson { 376fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 377dc2c7965SRobert Watson 378fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 379fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 380fc03d22bSJeff Roberson return (ubz); 381fc03d22bSJeff Roberson ubz--; 382fc03d22bSJeff Roberson return (ubz); 383fc03d22bSJeff Roberson } 384fc03d22bSJeff Roberson 385003cf08bSMark Johnston static struct uma_bucket_zone * 386003cf08bSMark Johnston bucket_zone_max(uma_zone_t zone, int nitems) 387003cf08bSMark Johnston { 388003cf08bSMark Johnston struct uma_bucket_zone *ubz; 389003cf08bSMark Johnston int bpcpu; 390003cf08bSMark Johnston 391003cf08bSMark Johnston bpcpu = 2; 392dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 393003cf08bSMark Johnston /* Count the cross-domain bucket. */ 394003cf08bSMark Johnston bpcpu++; 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 640dfe13344SJeff Roberson #ifdef NUMA 641376b1ba3SJeff Roberson static inline void 642376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b) 643376b1ba3SJeff Roberson { 644376b1ba3SJeff Roberson 645376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_crossbucket, b); 646376b1ba3SJeff Roberson } 647376b1ba3SJeff Roberson #endif 648376b1ba3SJeff Roberson 649376b1ba3SJeff Roberson /* 650376b1ba3SJeff Roberson * Copy and preserve ucb_spare. 651376b1ba3SJeff Roberson */ 652376b1ba3SJeff Roberson static inline void 653376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 654376b1ba3SJeff Roberson { 655376b1ba3SJeff Roberson 656376b1ba3SJeff Roberson b1->ucb_bucket = b2->ucb_bucket; 657376b1ba3SJeff Roberson b1->ucb_entries = b2->ucb_entries; 658376b1ba3SJeff Roberson b1->ucb_cnt = b2->ucb_cnt; 659376b1ba3SJeff Roberson } 660376b1ba3SJeff Roberson 661376b1ba3SJeff Roberson /* 662376b1ba3SJeff Roberson * Swap two cache buckets. 663376b1ba3SJeff Roberson */ 664376b1ba3SJeff Roberson static inline void 665376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 666376b1ba3SJeff Roberson { 667376b1ba3SJeff Roberson struct uma_cache_bucket b3; 668376b1ba3SJeff Roberson 669376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 670376b1ba3SJeff Roberson 671376b1ba3SJeff Roberson cache_bucket_copy(&b3, b1); 672376b1ba3SJeff Roberson cache_bucket_copy(b1, b2); 673376b1ba3SJeff Roberson cache_bucket_copy(b2, &b3); 674376b1ba3SJeff Roberson } 675376b1ba3SJeff Roberson 6762f891cd5SPawel Jakub Dawidek static void 6772f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 6782f891cd5SPawel Jakub Dawidek { 6792f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 6802f891cd5SPawel Jakub Dawidek 6812f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 6822f891cd5SPawel Jakub Dawidek return; 6832f891cd5SPawel Jakub Dawidek 6842f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 6852f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 6862f891cd5SPawel Jakub Dawidek } 6872f891cd5SPawel Jakub Dawidek 68854503a13SJonathan T. Looney static inline void 68954503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone) 69054503a13SJonathan T. Looney { 691e60b2fcbSGleb Smirnoff 692e60b2fcbSGleb Smirnoff if (zone->uz_maxaction.ta_func != NULL) 693e60b2fcbSGleb Smirnoff taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction); 69454503a13SJonathan T. Looney } 69554503a13SJonathan T. Looney 6968355f576SJeff Roberson /* 6978355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 6989643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 6998355f576SJeff Roberson * 7008355f576SJeff Roberson * Arguments: 7018355f576SJeff Roberson * arg Unused 7028355f576SJeff Roberson * 7038355f576SJeff Roberson * Returns: 7048355f576SJeff Roberson * Nothing 7058355f576SJeff Roberson */ 7068355f576SJeff Roberson static void 7078355f576SJeff Roberson uma_timeout(void *unused) 7088355f576SJeff Roberson { 70986bbae32SJeff Roberson bucket_enable(); 71020a4e154SJeff Roberson zone_foreach(zone_timeout, NULL); 7118355f576SJeff Roberson 7128355f576SJeff Roberson /* Reschedule this event */ 7139643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 7148355f576SJeff Roberson } 7158355f576SJeff Roberson 7168355f576SJeff Roberson /* 7170f9b7bf3SMark Johnston * Update the working set size estimate for the zone's bucket cache. 7180f9b7bf3SMark Johnston * The constants chosen here are somewhat arbitrary. With an update period of 7190f9b7bf3SMark Johnston * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the 7200f9b7bf3SMark Johnston * last 100s. 7210f9b7bf3SMark Johnston */ 7220f9b7bf3SMark Johnston static void 7230f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom) 7240f9b7bf3SMark Johnston { 7250f9b7bf3SMark Johnston long wss; 7260f9b7bf3SMark Johnston 7270f9b7bf3SMark Johnston MPASS(zdom->uzd_imax >= zdom->uzd_imin); 7280f9b7bf3SMark Johnston wss = zdom->uzd_imax - zdom->uzd_imin; 7290f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems; 73008cfa56eSMark Johnston zdom->uzd_wss = (4 * wss + zdom->uzd_wss) / 5; 7310f9b7bf3SMark Johnston } 7320f9b7bf3SMark Johnston 7330f9b7bf3SMark Johnston /* 7349643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 7359643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 7368355f576SJeff Roberson * 737e20a199fSJeff Roberson * Returns nothing. 7388355f576SJeff Roberson */ 7398355f576SJeff Roberson static void 74020a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused) 7418355f576SJeff Roberson { 74208034d10SKonstantin Belousov uma_keg_t keg; 7438b987a77SJeff Roberson u_int slabs, pages; 7448355f576SJeff Roberson 74554c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 74608034d10SKonstantin Belousov goto update_wss; 74708034d10SKonstantin Belousov 74808034d10SKonstantin Belousov keg = zone->uz_keg; 7498b987a77SJeff Roberson 7508b987a77SJeff Roberson /* 7518b987a77SJeff Roberson * Hash zones are non-numa by definition so the first domain 7528b987a77SJeff Roberson * is the only one present. 7538b987a77SJeff Roberson */ 7548b987a77SJeff Roberson KEG_LOCK(keg, 0); 7558b987a77SJeff Roberson pages = keg->uk_domain[0].ud_pages; 7568b987a77SJeff Roberson 7578355f576SJeff Roberson /* 758e20a199fSJeff Roberson * Expand the keg hash table. 7598355f576SJeff Roberson * 7608355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 7618355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 7628355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 7638355f576SJeff Roberson */ 7648b987a77SJeff Roberson if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) { 7650aef6126SJeff Roberson struct uma_hash newhash; 7660aef6126SJeff Roberson struct uma_hash oldhash; 7670aef6126SJeff Roberson int ret; 7685300d9ddSJeff Roberson 7690aef6126SJeff Roberson /* 7700aef6126SJeff Roberson * This is so involved because allocating and freeing 771e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 7720aef6126SJeff Roberson * I have to do everything in stages and check for 7730aef6126SJeff Roberson * races. 7740aef6126SJeff Roberson */ 7758b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 7763b2f2cb8SAlexander Motin ret = hash_alloc(&newhash, 1 << fls(slabs)); 7778b987a77SJeff Roberson KEG_LOCK(keg, 0); 7780aef6126SJeff Roberson if (ret) { 779099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 780099a0e58SBosko Milekic oldhash = keg->uk_hash; 781099a0e58SBosko Milekic keg->uk_hash = newhash; 7820aef6126SJeff Roberson } else 7830aef6126SJeff Roberson oldhash = newhash; 7840aef6126SJeff Roberson 7858b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 7860aef6126SJeff Roberson hash_free(&oldhash); 7878b987a77SJeff Roberson goto update_wss; 7880aef6126SJeff Roberson } 7895300d9ddSJeff Roberson } 7908b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 791e20a199fSJeff Roberson 79208034d10SKonstantin Belousov update_wss: 79308cfa56eSMark Johnston ZONE_LOCK(zone); 794bb15d1c7SGleb Smirnoff for (int i = 0; i < vm_ndomains; i++) 7950f9b7bf3SMark Johnston zone_domain_update_wss(&zone->uz_domain[i]); 79608cfa56eSMark Johnston ZONE_UNLOCK(zone); 7978355f576SJeff Roberson } 7988355f576SJeff Roberson 7998355f576SJeff Roberson /* 8005300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 8015300d9ddSJeff Roberson * backing store. 8025300d9ddSJeff Roberson * 8035300d9ddSJeff Roberson * Arguments: 8040aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 8055300d9ddSJeff Roberson * 8065300d9ddSJeff Roberson * Returns: 807763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 8085300d9ddSJeff Roberson */ 80937c84183SPoul-Henning Kamp static int 8103b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 8115300d9ddSJeff Roberson { 81259568a0eSAlexander Motin size_t alloc; 8135300d9ddSJeff Roberson 8143b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 8153b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 8163b2f2cb8SAlexander Motin hash->uh_hashsize = size; 8170aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 8181e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 8195300d9ddSJeff Roberson } else { 8200aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 821e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 822ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 8230aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 8245300d9ddSJeff Roberson } 8250aef6126SJeff Roberson if (hash->uh_slab_hash) { 8260aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 8270aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 8280aef6126SJeff Roberson return (1); 8290aef6126SJeff Roberson } 8305300d9ddSJeff Roberson 8310aef6126SJeff Roberson return (0); 8325300d9ddSJeff Roberson } 8335300d9ddSJeff Roberson 8345300d9ddSJeff Roberson /* 83564f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 83664f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 83764f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 8388355f576SJeff Roberson * 8398355f576SJeff Roberson * Arguments: 8400aef6126SJeff Roberson * oldhash The hash you want to expand 8410aef6126SJeff Roberson * newhash The hash structure for the new table 8428355f576SJeff Roberson * 8438355f576SJeff Roberson * Returns: 8448355f576SJeff Roberson * Nothing 8458355f576SJeff Roberson * 8468355f576SJeff Roberson * Discussion: 8478355f576SJeff Roberson */ 8480aef6126SJeff Roberson static int 8490aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 8508355f576SJeff Roberson { 8511e0701e1SJeff Roberson uma_hash_slab_t slab; 8526929b7d1SPedro F. Giffuni u_int hval; 8536929b7d1SPedro F. Giffuni u_int idx; 8548355f576SJeff Roberson 8550aef6126SJeff Roberson if (!newhash->uh_slab_hash) 8560aef6126SJeff Roberson return (0); 8578355f576SJeff Roberson 8580aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 8590aef6126SJeff Roberson return (0); 8608355f576SJeff Roberson 8618355f576SJeff Roberson /* 8628355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 8638355f576SJeff Roberson * full rehash. 8648355f576SJeff Roberson */ 8658355f576SJeff Roberson 8666929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 8671e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 8681e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 8691e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 8701e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 8711e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 8721e0701e1SJeff Roberson slab, uhs_hlink); 8738355f576SJeff Roberson } 8748355f576SJeff Roberson 8750aef6126SJeff Roberson return (1); 8769c2cd7e5SJeff Roberson } 8779c2cd7e5SJeff Roberson 8785300d9ddSJeff Roberson /* 8795300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 8805300d9ddSJeff Roberson * 8815300d9ddSJeff Roberson * Arguments: 8825300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 8835300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 8845300d9ddSJeff Roberson * 8855300d9ddSJeff Roberson * Returns: 8865300d9ddSJeff Roberson * Nothing 8875300d9ddSJeff Roberson */ 8889c2cd7e5SJeff Roberson static void 8890aef6126SJeff Roberson hash_free(struct uma_hash *hash) 8909c2cd7e5SJeff Roberson { 8910aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 8920aef6126SJeff Roberson return; 8930aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 8940095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 8958355f576SJeff Roberson else 896961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 8978355f576SJeff Roberson } 8988355f576SJeff Roberson 8998355f576SJeff Roberson /* 9008355f576SJeff Roberson * Frees all outstanding items in a bucket 9018355f576SJeff Roberson * 9028355f576SJeff Roberson * Arguments: 9038355f576SJeff Roberson * zone The zone to free to, must be unlocked. 9044bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 9058355f576SJeff Roberson * 9068355f576SJeff Roberson * Returns: 9078355f576SJeff Roberson * Nothing 9088355f576SJeff Roberson */ 9098355f576SJeff Roberson 9108355f576SJeff Roberson static void 9118355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 9128355f576SJeff Roberson { 9130095a784SJeff Roberson int i; 9148355f576SJeff Roberson 9154bd61e19SJeff Roberson if (bucket == NULL || bucket->ub_cnt == 0) 9168355f576SJeff Roberson return; 9178355f576SJeff Roberson 9180095a784SJeff Roberson if (zone->uz_fini) 9190095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 9200095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 9210095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 9224bd61e19SJeff Roberson if (zone->uz_max_items > 0) 9234bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 9240095a784SJeff Roberson bucket->ub_cnt = 0; 9258355f576SJeff Roberson } 9268355f576SJeff Roberson 9278355f576SJeff Roberson /* 9288355f576SJeff Roberson * Drains the per cpu caches for a zone. 9298355f576SJeff Roberson * 930727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 9315d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 9325d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 9335d1ae027SRobert Watson * 9348355f576SJeff Roberson * Arguments: 9358355f576SJeff Roberson * zone The zone to drain, must be unlocked. 9368355f576SJeff Roberson * 9378355f576SJeff Roberson * Returns: 9388355f576SJeff Roberson * Nothing 9398355f576SJeff Roberson */ 9408355f576SJeff Roberson static void 9419643769aSJeff Roberson cache_drain(uma_zone_t zone) 9428355f576SJeff Roberson { 9438355f576SJeff Roberson uma_cache_t cache; 944376b1ba3SJeff Roberson uma_bucket_t bucket; 9458355f576SJeff Roberson int cpu; 9468355f576SJeff Roberson 9478355f576SJeff Roberson /* 9485d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 9495d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 9505d1ae027SRobert Watson * of the caches at this point. 9515d1ae027SRobert Watson * 9525d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 9535d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 9548355f576SJeff Roberson */ 9553aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 9568355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 957376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 958376b1ba3SJeff Roberson if (bucket != NULL) { 959376b1ba3SJeff Roberson bucket_drain(zone, bucket); 960376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 961376b1ba3SJeff Roberson } 962376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 963376b1ba3SJeff Roberson if (bucket != NULL) { 964376b1ba3SJeff Roberson bucket_drain(zone, bucket); 965376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 966376b1ba3SJeff Roberson } 967376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 968376b1ba3SJeff Roberson if (bucket != NULL) { 969376b1ba3SJeff Roberson bucket_drain(zone, bucket); 970376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 971376b1ba3SJeff Roberson } 972d56368d7SBosko Milekic } 97308cfa56eSMark Johnston bucket_cache_reclaim(zone, true); 974aaa8bb16SJeff Roberson } 975aaa8bb16SJeff Roberson 976a2de44abSAlexander Motin static void 97720a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 978a2de44abSAlexander Motin { 979a2de44abSAlexander Motin 980a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 981a2de44abSAlexander Motin return; 982a2de44abSAlexander Motin 983a2de44abSAlexander Motin ZONE_LOCK(zone); 98420a4e154SJeff Roberson zone->uz_bucket_size = 98520a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 986a2de44abSAlexander Motin ZONE_UNLOCK(zone); 987a2de44abSAlexander Motin } 988a2de44abSAlexander Motin 989a2de44abSAlexander Motin static void 99020a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 991a2de44abSAlexander Motin { 992a2de44abSAlexander Motin uma_cache_t cache; 993c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 994ab3185d1SJeff Roberson int domain; 995a2de44abSAlexander Motin 996a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 997a2de44abSAlexander Motin return; 998a2de44abSAlexander Motin 999c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1000a2de44abSAlexander Motin ZONE_LOCK(zone); 1001a2de44abSAlexander Motin critical_enter(); 1002dfe13344SJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 1003ab3185d1SJeff Roberson domain = PCPU_GET(domain); 1004ab3185d1SJeff Roberson else 1005ab3185d1SJeff Roberson domain = 0; 1006a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1007376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1008376b1ba3SJeff Roberson if (b1 != NULL && b1->ub_cnt != 0) { 1009376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b1, false); 1010376b1ba3SJeff Roberson b1 = NULL; 1011a2de44abSAlexander Motin } 1012376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1013376b1ba3SJeff Roberson if (b2 != NULL && b2->ub_cnt != 0) { 1014376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b2, false); 1015376b1ba3SJeff Roberson b2 = NULL; 1016a2de44abSAlexander Motin } 1017376b1ba3SJeff Roberson b3 = cache_bucket_unload_cross(cache); 1018a2de44abSAlexander Motin critical_exit(); 1019a2de44abSAlexander Motin ZONE_UNLOCK(zone); 10208a8d9d14SAlexander Motin if (b1) 10218a8d9d14SAlexander Motin bucket_free(zone, b1, NULL); 10228a8d9d14SAlexander Motin if (b2) 10238a8d9d14SAlexander Motin bucket_free(zone, b2, NULL); 1024c1685086SJeff Roberson if (b3) { 1025c1685086SJeff Roberson bucket_drain(zone, b3); 1026c1685086SJeff Roberson bucket_free(zone, b3, NULL); 1027c1685086SJeff Roberson } 1028a2de44abSAlexander Motin } 1029a2de44abSAlexander Motin 1030a2de44abSAlexander Motin /* 1031a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1032a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1033a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1034a2de44abSAlexander Motin * to safely access their cache buckets. 1035a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1036a2de44abSAlexander Motin */ 1037a2de44abSAlexander Motin static void 103808cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1039a2de44abSAlexander Motin { 1040a2de44abSAlexander Motin int cpu; 1041a2de44abSAlexander Motin 1042a2de44abSAlexander Motin /* 1043727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1044a2de44abSAlexander Motin */ 1045a2de44abSAlexander Motin if (zone) 104620a4e154SJeff Roberson cache_shrink(zone, NULL); 1047a2de44abSAlexander Motin else 104820a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1049a2de44abSAlexander Motin 1050a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1051a2de44abSAlexander Motin thread_lock(curthread); 1052a2de44abSAlexander Motin sched_bind(curthread, cpu); 1053a2de44abSAlexander Motin thread_unlock(curthread); 1054a2de44abSAlexander Motin 1055a2de44abSAlexander Motin if (zone) 105620a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1057a2de44abSAlexander Motin else 105820a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1059a2de44abSAlexander Motin } 1060a2de44abSAlexander Motin thread_lock(curthread); 1061a2de44abSAlexander Motin sched_unbind(curthread); 1062a2de44abSAlexander Motin thread_unlock(curthread); 1063a2de44abSAlexander Motin } 1064a2de44abSAlexander Motin 1065aaa8bb16SJeff Roberson /* 106608cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 106708cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 106808cfa56eSMark Johnston * estimated working set size. 1069aaa8bb16SJeff Roberson */ 1070aaa8bb16SJeff Roberson static void 107108cfa56eSMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain) 1072aaa8bb16SJeff Roberson { 1073ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1074aaa8bb16SJeff Roberson uma_bucket_t bucket; 107508cfa56eSMark Johnston long target, tofree; 1076ab3185d1SJeff Roberson int i; 10778355f576SJeff Roberson 1078ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 107991d947bfSJeff Roberson /* 108091d947bfSJeff Roberson * The cross bucket is partially filled and not part of 108191d947bfSJeff Roberson * the item count. Reclaim it individually here. 108291d947bfSJeff Roberson */ 1083ab3185d1SJeff Roberson zdom = &zone->uz_domain[i]; 108491d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 108591d947bfSJeff Roberson bucket = zdom->uzd_cross; 108691d947bfSJeff Roberson zdom->uzd_cross = NULL; 108791d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 108891d947bfSJeff Roberson if (bucket != NULL) { 108991d947bfSJeff Roberson bucket_drain(zone, bucket); 109091d947bfSJeff Roberson bucket_free(zone, bucket, NULL); 109191d947bfSJeff Roberson } 109291d947bfSJeff Roberson 109391d947bfSJeff Roberson /* 109491d947bfSJeff Roberson * Shrink the zone bucket size to ensure that the per-CPU caches 109591d947bfSJeff Roberson * don't grow too large. 109691d947bfSJeff Roberson */ 109791d947bfSJeff Roberson ZONE_LOCK(zone); 109891d947bfSJeff Roberson if (i == 0 && zone->uz_bucket_size > zone->uz_bucket_size_min) 109991d947bfSJeff Roberson zone->uz_bucket_size--; 110008cfa56eSMark Johnston 110108cfa56eSMark Johnston /* 110208cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 110308cfa56eSMark Johnston * this bucket cache is empty. Otherwise, we reclaim items in 110408cfa56eSMark Johnston * excess of the zone's estimated working set size. If the 110508cfa56eSMark Johnston * difference nitems - imin is larger than the WSS estimate, 110608cfa56eSMark Johnston * then the estimate will grow at the end of this interval and 110708cfa56eSMark Johnston * we ignore the historical average. 110808cfa56eSMark Johnston */ 110908cfa56eSMark Johnston target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems - 111008cfa56eSMark Johnston zdom->uzd_imin); 111108cfa56eSMark Johnston while (zdom->uzd_nitems > target) { 111208cfa56eSMark Johnston bucket = TAILQ_LAST(&zdom->uzd_buckets, uma_bucketlist); 111308cfa56eSMark Johnston if (bucket == NULL) 111408cfa56eSMark Johnston break; 111508cfa56eSMark Johnston tofree = bucket->ub_cnt; 111608cfa56eSMark Johnston TAILQ_REMOVE(&zdom->uzd_buckets, bucket, ub_link); 111708cfa56eSMark Johnston zdom->uzd_nitems -= tofree; 111808cfa56eSMark Johnston 111908cfa56eSMark Johnston /* 112008cfa56eSMark Johnston * Shift the bounds of the current WSS interval to avoid 112108cfa56eSMark Johnston * perturbing the estimate. 112208cfa56eSMark Johnston */ 112308cfa56eSMark Johnston zdom->uzd_imax -= lmin(zdom->uzd_imax, tofree); 112408cfa56eSMark Johnston zdom->uzd_imin -= lmin(zdom->uzd_imin, tofree); 112508cfa56eSMark Johnston 11268355f576SJeff Roberson ZONE_UNLOCK(zone); 11278355f576SJeff Roberson bucket_drain(zone, bucket); 11286fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 11298355f576SJeff Roberson ZONE_LOCK(zone); 11308355f576SJeff Roberson } 113191d947bfSJeff Roberson ZONE_UNLOCK(zone); 1132ab3185d1SJeff Roberson } 11338355f576SJeff Roberson } 1134fc03d22bSJeff Roberson 1135fc03d22bSJeff Roberson static void 1136fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1137fc03d22bSJeff Roberson { 1138fc03d22bSJeff Roberson uint8_t *mem; 1139fc03d22bSJeff Roberson int i; 1140fc03d22bSJeff Roberson uint8_t flags; 1141fc03d22bSJeff Roberson 11421431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 11431431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 11441431a748SGleb Smirnoff 11451e0701e1SJeff Roberson mem = slab_data(slab, keg); 1146fc03d22bSJeff Roberson flags = slab->us_flags; 1147fc03d22bSJeff Roberson i = start; 1148fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 1149fc03d22bSJeff Roberson for (i--; i > -1; i--) 1150c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1151c5deaf04SGleb Smirnoff /* 1152c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1153c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1154c5deaf04SGleb Smirnoff * which executed trash_dtor. 1155c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1156c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1157c5deaf04SGleb Smirnoff * invocations. 1158c5deaf04SGleb Smirnoff */ 11591e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1160c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1161c5deaf04SGleb Smirnoff #endif 11621e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1163fc03d22bSJeff Roberson } 116454c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 1165fc03d22bSJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 1166fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 11672e47807cSJeff Roberson uma_total_dec(PAGE_SIZE * keg->uk_ppera); 11688355f576SJeff Roberson } 11698355f576SJeff Roberson 11708355f576SJeff Roberson /* 1171e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 11728355f576SJeff Roberson * the pageout daemon. 11738355f576SJeff Roberson * 1174e20a199fSJeff Roberson * Returns nothing. 11758355f576SJeff Roberson */ 1176e20a199fSJeff Roberson static void 1177e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 11788355f576SJeff Roberson { 11791e183df2SStefan Farfeleder struct slabhead freeslabs = { 0 }; 1180ab3185d1SJeff Roberson uma_domain_t dom; 1181829be516SMark Johnston uma_slab_t slab, tmp; 11828b987a77SJeff Roberson int i, n; 11838355f576SJeff Roberson 11848355f576SJeff Roberson /* 1185e20a199fSJeff Roberson * We don't want to take pages from statically allocated kegs at this 11868355f576SJeff Roberson * time 11878355f576SJeff Roberson */ 1188099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 11898355f576SJeff Roberson return; 11908355f576SJeff Roberson 1191ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 11928b987a77SJeff Roberson CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u", 11938b987a77SJeff Roberson keg->uk_name, keg, i, dom->ud_free); 11948b987a77SJeff Roberson n = 0; 1195ab3185d1SJeff Roberson dom = &keg->uk_domain[i]; 11968b987a77SJeff Roberson KEG_LOCK(keg, i); 1197ab3185d1SJeff Roberson LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) { 1198829be516SMark Johnston /* We have nowhere to free these to. */ 1199829be516SMark Johnston if (slab->us_flags & UMA_SLAB_BOOT) 12008355f576SJeff Roberson continue; 120154c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 12021e0701e1SJeff Roberson UMA_HASH_REMOVE(&keg->uk_hash, slab); 12038b987a77SJeff Roberson n++; 12048b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 12051e0701e1SJeff Roberson LIST_INSERT_HEAD(&freeslabs, slab, us_link); 1206713deb36SJeff Roberson } 12078b987a77SJeff Roberson dom->ud_pages -= n * keg->uk_ppera; 12088b987a77SJeff Roberson dom->ud_free -= n * keg->uk_ipers; 12098b987a77SJeff Roberson KEG_UNLOCK(keg, i); 1210ab3185d1SJeff Roberson } 1211ab3185d1SJeff Roberson 12121e0701e1SJeff Roberson while ((slab = LIST_FIRST(&freeslabs)) != NULL) { 12131e0701e1SJeff Roberson LIST_REMOVE(slab, us_link); 12141645995bSKirk McKusick keg_free_slab(keg, slab, keg->uk_ipers); 12158355f576SJeff Roberson } 12168355f576SJeff Roberson } 12178355f576SJeff Roberson 1218e20a199fSJeff Roberson static void 121908cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain) 1220e20a199fSJeff Roberson { 1221e20a199fSJeff Roberson 12228355f576SJeff Roberson /* 1223e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 1224e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 1225e20a199fSJeff Roberson * is the only call that knows the structure will still be available 1226e20a199fSJeff Roberson * when it wakes up. 1227e20a199fSJeff Roberson */ 1228e20a199fSJeff Roberson ZONE_LOCK(zone); 122908cfa56eSMark Johnston while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) { 1230e20a199fSJeff Roberson if (waitok == M_NOWAIT) 1231e20a199fSJeff Roberson goto out; 1232727c6918SJeff Roberson msleep(zone, &zone->uz_lock, PVM, "zonedrain", 1); 1233e20a199fSJeff Roberson } 123408cfa56eSMark Johnston zone->uz_flags |= UMA_ZFLAG_RECLAIMING; 1235e20a199fSJeff Roberson ZONE_UNLOCK(zone); 123691d947bfSJeff Roberson bucket_cache_reclaim(zone, drain); 123708cfa56eSMark Johnston 1238e20a199fSJeff Roberson /* 1239e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 1240111fbcd5SBryan Venteicher * we're running. Normally the uma_rwlock would protect us but we 1241e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 1242e20a199fSJeff Roberson */ 124308034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1244bb15d1c7SGleb Smirnoff keg_drain(zone->uz_keg); 1245e20a199fSJeff Roberson ZONE_LOCK(zone); 124608cfa56eSMark Johnston zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING; 1247e20a199fSJeff Roberson wakeup(zone); 1248e20a199fSJeff Roberson out: 1249e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1250e20a199fSJeff Roberson } 1251e20a199fSJeff Roberson 125208cfa56eSMark Johnston static void 125320a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused) 1254e20a199fSJeff Roberson { 1255e20a199fSJeff Roberson 125608cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, true); 125708cfa56eSMark Johnston } 125808cfa56eSMark Johnston 125908cfa56eSMark Johnston static void 126020a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused) 126108cfa56eSMark Johnston { 126208cfa56eSMark Johnston 126308cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, false); 1264e20a199fSJeff Roberson } 1265e20a199fSJeff Roberson 1266e20a199fSJeff Roberson /* 12678b987a77SJeff Roberson * Allocate a new slab for a keg and inserts it into the partial slab list. 12688b987a77SJeff Roberson * The keg should be unlocked on entry. If the allocation succeeds it will 12698b987a77SJeff Roberson * be locked on return. 12708355f576SJeff Roberson * 12718355f576SJeff Roberson * Arguments: 127286220393SMark Johnston * flags Wait flags for the item initialization routine 127386220393SMark Johnston * aflags Wait flags for the slab allocation 12748355f576SJeff Roberson * 12758355f576SJeff Roberson * Returns: 12768355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 12778355f576SJeff Roberson * caller specified M_NOWAIT. 12788355f576SJeff Roberson */ 12798355f576SJeff Roberson static uma_slab_t 128086220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 128186220393SMark Johnston int aflags) 12828355f576SJeff Roberson { 12838b987a77SJeff Roberson uma_domain_t dom; 1284e20a199fSJeff Roberson uma_alloc allocf; 1285099a0e58SBosko Milekic uma_slab_t slab; 12862e47807cSJeff Roberson unsigned long size; 128785dcf349SGleb Smirnoff uint8_t *mem; 128886220393SMark Johnston uint8_t sflags; 12898355f576SJeff Roberson int i; 12908355f576SJeff Roberson 1291ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1292ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1293a553d4b8SJeff Roberson 12948b987a77SJeff Roberson allocf = keg->uk_allocf; 1295194a979eSMark Johnston slab = NULL; 1296194a979eSMark Johnston mem = NULL; 129754c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 129886220393SMark Johnston slab = zone_alloc_item(keg->uk_slabzone, NULL, domain, aflags); 1299fc03d22bSJeff Roberson if (slab == NULL) 1300727c6918SJeff Roberson goto fail; 1301a553d4b8SJeff Roberson } 1302a553d4b8SJeff Roberson 13033370c5bfSJeff Roberson /* 13043370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 13053370c5bfSJeff Roberson * first time they are added to a zone. 13063370c5bfSJeff Roberson * 13073370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 13083370c5bfSJeff Roberson */ 13093370c5bfSJeff Roberson 1310099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 131186220393SMark Johnston aflags |= M_ZERO; 13123370c5bfSJeff Roberson else 131386220393SMark Johnston aflags &= ~M_ZERO; 13143370c5bfSJeff Roberson 1315263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 131686220393SMark Johnston aflags |= M_NODUMP; 1317263811f7SKip Macy 1318e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1319194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 132086220393SMark Johnston mem = allocf(zone, size, domain, &sflags, aflags); 1321a553d4b8SJeff Roberson if (mem == NULL) { 132254c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 13230095a784SJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 1324727c6918SJeff Roberson goto fail; 1325a553d4b8SJeff Roberson } 13262e47807cSJeff Roberson uma_total_inc(size); 13278355f576SJeff Roberson 13288b987a77SJeff Roberson /* For HASH zones all pages go to the same uma_domain. */ 132954c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 13308b987a77SJeff Roberson domain = 0; 13318b987a77SJeff Roberson 13325c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 133354c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) 1334099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 13351e0701e1SJeff Roberson else 13361e0701e1SJeff Roberson ((uma_hash_slab_t)slab)->uhs_data = mem; 13375c0e403bSJeff Roberson 133854c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_VTOSLAB) 1339099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1340584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1341584061b4SJeff Roberson zone, slab); 13428355f576SJeff Roberson 1343099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 134486220393SMark Johnston slab->us_flags = sflags; 1345ab3185d1SJeff Roberson slab->us_domain = domain; 13468b987a77SJeff Roberson 13479b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1348ef72505eSJeff Roberson #ifdef INVARIANTS 1349815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1350ef72505eSJeff Roberson #endif 1351099a0e58SBosko Milekic 1352b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1353099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 13541e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 135586220393SMark Johnston keg->uk_size, flags) != 0) 1356b23f72e9SBrian Feldman break; 1357b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1358fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1359727c6918SJeff Roberson goto fail; 1360b23f72e9SBrian Feldman } 1361b23f72e9SBrian Feldman } 13628b987a77SJeff Roberson KEG_LOCK(keg, domain); 13635c0e403bSJeff Roberson 13641431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 13651431a748SGleb Smirnoff slab, keg->uk_name, keg); 13661431a748SGleb Smirnoff 136754c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 1368099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 13698355f576SJeff Roberson 13708b987a77SJeff Roberson /* 13718b987a77SJeff Roberson * If we got a slab here it's safe to mark it partially used 13728b987a77SJeff Roberson * and return. We assume that the caller is going to remove 13738b987a77SJeff Roberson * at least one item. 13748b987a77SJeff Roberson */ 13758b987a77SJeff Roberson dom = &keg->uk_domain[domain]; 13768b987a77SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 13778b987a77SJeff Roberson dom->ud_pages += keg->uk_ppera; 13788b987a77SJeff Roberson dom->ud_free += keg->uk_ipers; 13798355f576SJeff Roberson 13808355f576SJeff Roberson return (slab); 1381727c6918SJeff Roberson 1382727c6918SJeff Roberson fail: 1383727c6918SJeff Roberson return (NULL); 13848355f576SJeff Roberson } 13858355f576SJeff Roberson 13868355f576SJeff Roberson /* 1387009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1388009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1389009b6fcbSJeff Roberson * the VM is ready. 1390009b6fcbSJeff Roberson */ 1391009b6fcbSJeff Roberson static void * 1392ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1393ab3185d1SJeff Roberson int wait) 1394009b6fcbSJeff Roberson { 1395099a0e58SBosko Milekic uma_keg_t keg; 1396ac0a6fd0SGleb Smirnoff void *mem; 1397ac0a6fd0SGleb Smirnoff int pages; 1398099a0e58SBosko Milekic 1399bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1400009b6fcbSJeff Roberson /* 1401f7d35785SGleb Smirnoff * If we are in BOOT_BUCKETS or higher, than switch to real 1402f7d35785SGleb Smirnoff * allocator. Zones with page sized slabs switch at BOOT_PAGEALLOC. 1403009b6fcbSJeff Roberson */ 1404f7d35785SGleb Smirnoff switch (booted) { 1405f7d35785SGleb Smirnoff case BOOT_COLD: 1406f7d35785SGleb Smirnoff case BOOT_STRAPPED: 1407f7d35785SGleb Smirnoff break; 1408f7d35785SGleb Smirnoff case BOOT_PAGEALLOC: 1409f7d35785SGleb Smirnoff if (keg->uk_ppera > 1) 1410f7d35785SGleb Smirnoff break; 1411f7d35785SGleb Smirnoff case BOOT_BUCKETS: 1412f7d35785SGleb Smirnoff case BOOT_RUNNING: 1413009b6fcbSJeff Roberson #ifdef UMA_MD_SMALL_ALLOC 1414f7d35785SGleb Smirnoff keg->uk_allocf = (keg->uk_ppera > 1) ? 1415f7d35785SGleb Smirnoff page_alloc : uma_small_alloc; 1416009b6fcbSJeff Roberson #else 1417099a0e58SBosko Milekic keg->uk_allocf = page_alloc; 1418009b6fcbSJeff Roberson #endif 1419ab3185d1SJeff Roberson return keg->uk_allocf(zone, bytes, domain, pflag, wait); 1420009b6fcbSJeff Roberson } 1421009b6fcbSJeff Roberson 1422009b6fcbSJeff Roberson /* 1423f7d35785SGleb Smirnoff * Check our small startup cache to see if it has pages remaining. 1424f7d35785SGleb Smirnoff */ 1425f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1426f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1427f7d35785SGleb Smirnoff if (pages > boot_pages) 1428f7d35785SGleb Smirnoff panic("UMA zone \"%s\": Increase vm.boot_pages", zone->uz_name); 1429f7d35785SGleb Smirnoff #ifdef DIAGNOSTIC 1430f7d35785SGleb Smirnoff printf("%s from \"%s\", %d boot pages left\n", __func__, zone->uz_name, 1431f7d35785SGleb Smirnoff boot_pages); 1432f7d35785SGleb Smirnoff #endif 1433f7d35785SGleb Smirnoff mem = bootmem; 1434f7d35785SGleb Smirnoff boot_pages -= pages; 1435f7d35785SGleb Smirnoff bootmem += pages * PAGE_SIZE; 1436f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1437f7d35785SGleb Smirnoff 1438f7d35785SGleb Smirnoff return (mem); 1439f7d35785SGleb Smirnoff } 1440f7d35785SGleb Smirnoff 1441f7d35785SGleb Smirnoff /* 14428355f576SJeff Roberson * Allocates a number of pages from the system 14438355f576SJeff Roberson * 14448355f576SJeff Roberson * Arguments: 14458355f576SJeff Roberson * bytes The number of bytes requested 14468355f576SJeff Roberson * wait Shall we wait? 14478355f576SJeff Roberson * 14488355f576SJeff Roberson * Returns: 14498355f576SJeff Roberson * A pointer to the alloced memory or possibly 14508355f576SJeff Roberson * NULL if M_NOWAIT is set. 14518355f576SJeff Roberson */ 14528355f576SJeff Roberson static void * 1453ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1454ab3185d1SJeff Roberson int wait) 14558355f576SJeff Roberson { 14568355f576SJeff Roberson void *p; /* Returned page */ 14578355f576SJeff Roberson 14582e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 14599978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 14608355f576SJeff Roberson 14618355f576SJeff Roberson return (p); 14628355f576SJeff Roberson } 14638355f576SJeff Roberson 1464ab3059a8SMatt Macy static void * 1465ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1466ab3059a8SMatt Macy int wait) 1467ab3059a8SMatt Macy { 1468ab3059a8SMatt Macy struct pglist alloctail; 1469ab3059a8SMatt Macy vm_offset_t addr, zkva; 1470ab3059a8SMatt Macy int cpu, flags; 1471ab3059a8SMatt Macy vm_page_t p, p_next; 1472ab3059a8SMatt Macy #ifdef NUMA 1473ab3059a8SMatt Macy struct pcpu *pc; 1474ab3059a8SMatt Macy #endif 1475ab3059a8SMatt Macy 1476ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1477ab3059a8SMatt Macy 1478013072f0SMark Johnston TAILQ_INIT(&alloctail); 1479ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1480013072f0SMark Johnston malloc2vm_flags(wait); 1481013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1482ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1483ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1484ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1485ab3059a8SMatt Macy } else { 1486ab3059a8SMatt Macy #ifndef NUMA 1487ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1488ab3059a8SMatt Macy #else 1489ab3059a8SMatt Macy pc = pcpu_find(cpu); 1490ab3059a8SMatt Macy p = vm_page_alloc_domain(NULL, 0, pc->pc_domain, flags); 1491ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1492ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1493ab3059a8SMatt Macy #endif 1494ab3059a8SMatt Macy } 1495ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1496ab3059a8SMatt Macy goto fail; 1497ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1498ab3059a8SMatt Macy } 1499ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1500ab3059a8SMatt Macy goto fail; 1501ab3059a8SMatt Macy zkva = addr; 1502ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1503ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1504ab3059a8SMatt Macy zkva += PAGE_SIZE; 1505ab3059a8SMatt Macy } 1506ab3059a8SMatt Macy return ((void*)addr); 1507ab3059a8SMatt Macy fail: 1508ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 150988ea538aSMark Johnston vm_page_unwire_noq(p); 1510ab3059a8SMatt Macy vm_page_free(p); 1511ab3059a8SMatt Macy } 1512ab3059a8SMatt Macy return (NULL); 1513ab3059a8SMatt Macy } 1514ab3059a8SMatt Macy 15158355f576SJeff Roberson /* 15168355f576SJeff Roberson * Allocates a number of pages from within an object 15178355f576SJeff Roberson * 15188355f576SJeff Roberson * Arguments: 15198355f576SJeff Roberson * bytes The number of bytes requested 15208355f576SJeff Roberson * wait Shall we wait? 15218355f576SJeff Roberson * 15228355f576SJeff Roberson * Returns: 15238355f576SJeff Roberson * A pointer to the alloced memory or possibly 15248355f576SJeff Roberson * NULL if M_NOWAIT is set. 15258355f576SJeff Roberson */ 15268355f576SJeff Roberson static void * 1527ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1528ab3185d1SJeff Roberson int wait) 15298355f576SJeff Roberson { 1530a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1531a4915c21SAttilio Rao u_long npages; 1532b245ac95SAlan Cox vm_offset_t retkva, zkva; 1533a4915c21SAttilio Rao vm_page_t p, p_next; 1534e20a199fSJeff Roberson uma_keg_t keg; 15358355f576SJeff Roberson 1536a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1537bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1538a4915c21SAttilio Rao 1539a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1540a4915c21SAttilio Rao while (npages > 0) { 1541ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 15428d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1543772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1544772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1545a4915c21SAttilio Rao if (p != NULL) { 1546a4915c21SAttilio Rao /* 1547a4915c21SAttilio Rao * Since the page does not belong to an object, its 1548a4915c21SAttilio Rao * listq is unused. 1549a4915c21SAttilio Rao */ 1550a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1551a4915c21SAttilio Rao npages--; 1552a4915c21SAttilio Rao continue; 1553a4915c21SAttilio Rao } 15548355f576SJeff Roberson /* 1555a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1556a4915c21SAttilio Rao * exit. 15578355f576SJeff Roberson */ 1558a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 155988ea538aSMark Johnston vm_page_unwire_noq(p); 1560b245ac95SAlan Cox vm_page_free(p); 1561b245ac95SAlan Cox } 1562a4915c21SAttilio Rao return (NULL); 1563b245ac95SAlan Cox } 15648355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1565a4915c21SAttilio Rao zkva = keg->uk_kva + 1566a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1567a4915c21SAttilio Rao retkva = zkva; 1568a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1569a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1570a4915c21SAttilio Rao zkva += PAGE_SIZE; 1571a4915c21SAttilio Rao } 15728355f576SJeff Roberson 15738355f576SJeff Roberson return ((void *)retkva); 15748355f576SJeff Roberson } 15758355f576SJeff Roberson 15768355f576SJeff Roberson /* 15778355f576SJeff Roberson * Frees a number of pages to the system 15788355f576SJeff Roberson * 15798355f576SJeff Roberson * Arguments: 15808355f576SJeff Roberson * mem A pointer to the memory to be freed 15818355f576SJeff Roberson * size The size of the memory being freed 15828355f576SJeff Roberson * flags The original p->us_flags field 15838355f576SJeff Roberson * 15848355f576SJeff Roberson * Returns: 15858355f576SJeff Roberson * Nothing 15868355f576SJeff Roberson */ 15878355f576SJeff Roberson static void 1588f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 15898355f576SJeff Roberson { 15903370c5bfSJeff Roberson 159149bfa624SAlan Cox if ((flags & UMA_SLAB_KERNEL) == 0) 1592b5345ef1SJustin Hibbits panic("UMA: page_free used with invalid flags %x", flags); 15938355f576SJeff Roberson 159449bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 15958355f576SJeff Roberson } 15968355f576SJeff Roberson 15978355f576SJeff Roberson /* 1598ab3059a8SMatt Macy * Frees pcpu zone allocations 1599ab3059a8SMatt Macy * 1600ab3059a8SMatt Macy * Arguments: 1601ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1602ab3059a8SMatt Macy * size The size of the memory being freed 1603ab3059a8SMatt Macy * flags The original p->us_flags field 1604ab3059a8SMatt Macy * 1605ab3059a8SMatt Macy * Returns: 1606ab3059a8SMatt Macy * Nothing 1607ab3059a8SMatt Macy */ 1608ab3059a8SMatt Macy static void 1609ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1610ab3059a8SMatt Macy { 1611ab3059a8SMatt Macy vm_offset_t sva, curva; 1612ab3059a8SMatt Macy vm_paddr_t paddr; 1613ab3059a8SMatt Macy vm_page_t m; 1614ab3059a8SMatt Macy 1615ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 1616ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1617ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1618ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1619ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 162088ea538aSMark Johnston vm_page_unwire_noq(m); 1621ab3059a8SMatt Macy vm_page_free(m); 1622ab3059a8SMatt Macy } 1623ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1624ab3059a8SMatt Macy kva_free(sva, size); 1625ab3059a8SMatt Macy } 1626ab3059a8SMatt Macy 1627ab3059a8SMatt Macy 1628ab3059a8SMatt Macy /* 16298355f576SJeff Roberson * Zero fill initializer 16308355f576SJeff Roberson * 16318355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 16328355f576SJeff Roberson */ 1633b23f72e9SBrian Feldman static int 1634b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 16358355f576SJeff Roberson { 16368355f576SJeff Roberson bzero(mem, size); 1637b23f72e9SBrian Feldman return (0); 16388355f576SJeff Roberson } 16398355f576SJeff Roberson 1640815db204SRyan Libby #ifdef INVARIANTS 1641815db204SRyan Libby struct noslabbits * 1642815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1643815db204SRyan Libby { 1644815db204SRyan Libby 1645815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1646815db204SRyan Libby } 1647815db204SRyan Libby #endif 1648815db204SRyan Libby 16498355f576SJeff Roberson /* 16509b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 16519b78b1f4SJeff Roberson */ 16529b78b1f4SJeff Roberson size_t 16539b78b1f4SJeff Roberson slab_sizeof(int nitems) 16549b78b1f4SJeff Roberson { 16559b78b1f4SJeff Roberson size_t s; 16569b78b1f4SJeff Roberson 1657815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 16589b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 16599b78b1f4SJeff Roberson } 16609b78b1f4SJeff Roberson 16619b78b1f4SJeff Roberson /* 16629b78b1f4SJeff Roberson * Size of memory for embedded slabs (!OFFPAGE). 16639b78b1f4SJeff Roberson */ 16649b78b1f4SJeff Roberson size_t 16659b78b1f4SJeff Roberson slab_space(int nitems) 16669b78b1f4SJeff Roberson { 16679b78b1f4SJeff Roberson return (UMA_SLAB_SIZE - slab_sizeof(nitems)); 16689b78b1f4SJeff Roberson } 16699b78b1f4SJeff Roberson 1670*4a8b575cSRyan Libby #define UMA_FIXPT_SHIFT 31 1671*4a8b575cSRyan Libby #define UMA_FRAC_FIXPT(n, d) \ 1672*4a8b575cSRyan Libby ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d))) 1673*4a8b575cSRyan Libby #define UMA_FIXPT_PCT(f) \ 1674*4a8b575cSRyan Libby ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT)) 1675*4a8b575cSRyan Libby #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100) 1676*4a8b575cSRyan Libby #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE) 1677*4a8b575cSRyan Libby 16789b78b1f4SJeff Roberson /* 1679*4a8b575cSRyan Libby * Compute the number of items that will fit in a slab. If hdr is true, the 1680*4a8b575cSRyan Libby * item count may be limited to provide space in the slab for an inline slab 1681*4a8b575cSRyan Libby * header. Otherwise, all slab space will be provided for item storage. 1682*4a8b575cSRyan Libby */ 1683*4a8b575cSRyan Libby static u_int 1684*4a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr) 1685*4a8b575cSRyan Libby { 1686*4a8b575cSRyan Libby u_int ipers; 1687*4a8b575cSRyan Libby u_int padpi; 1688*4a8b575cSRyan Libby 1689*4a8b575cSRyan Libby /* The padding between items is not needed after the last item. */ 1690*4a8b575cSRyan Libby padpi = rsize - size; 1691*4a8b575cSRyan Libby 1692*4a8b575cSRyan Libby if (hdr) { 1693*4a8b575cSRyan Libby /* 1694*4a8b575cSRyan Libby * Start with the maximum item count and remove items until 1695*4a8b575cSRyan Libby * the slab header first alongside the allocatable memory. 1696*4a8b575cSRyan Libby */ 1697*4a8b575cSRyan Libby for (ipers = MIN(SLAB_MAX_SETSIZE, 1698*4a8b575cSRyan Libby (slabsize + padpi - slab_sizeof(1)) / rsize); 1699*4a8b575cSRyan Libby ipers > 0 && 1700*4a8b575cSRyan Libby ipers * rsize - padpi + slab_sizeof(ipers) > slabsize; 1701*4a8b575cSRyan Libby ipers--) 1702*4a8b575cSRyan Libby continue; 1703*4a8b575cSRyan Libby } else { 1704*4a8b575cSRyan Libby ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE); 1705*4a8b575cSRyan Libby } 1706*4a8b575cSRyan Libby 1707*4a8b575cSRyan Libby return (ipers); 1708*4a8b575cSRyan Libby } 1709*4a8b575cSRyan Libby 1710*4a8b575cSRyan Libby /* 1711*4a8b575cSRyan Libby * Compute the number of items that will fit in a slab for a startup zone. 17129b78b1f4SJeff Roberson */ 17139b78b1f4SJeff Roberson int 17149b78b1f4SJeff Roberson slab_ipers(size_t size, int align) 17159b78b1f4SJeff Roberson { 17169b78b1f4SJeff Roberson int rsize; 17179b78b1f4SJeff Roberson 1718*4a8b575cSRyan Libby rsize = roundup(size, align + 1); /* Assume no CACHESPREAD */ 1719*4a8b575cSRyan Libby return (slab_ipers_hdr(size, rsize, UMA_SLAB_SIZE, true)); 17209b78b1f4SJeff Roberson } 17219b78b1f4SJeff Roberson 17229b78b1f4SJeff Roberson /* 1723*4a8b575cSRyan Libby * Determine the format of a uma keg. This determines where the slab header 1724*4a8b575cSRyan Libby * will be placed (inline or offpage) and calculates ipers, rsize, and ppera. 17258355f576SJeff Roberson * 17268355f576SJeff Roberson * Arguments 1727e20a199fSJeff Roberson * keg The zone we should initialize 17288355f576SJeff Roberson * 17298355f576SJeff Roberson * Returns 17308355f576SJeff Roberson * Nothing 17318355f576SJeff Roberson */ 17328355f576SJeff Roberson static void 1733*4a8b575cSRyan Libby keg_layout(uma_keg_t keg) 17348355f576SJeff Roberson { 1735*4a8b575cSRyan Libby u_int alignsize; 1736*4a8b575cSRyan Libby u_int eff; 1737*4a8b575cSRyan Libby u_int eff_offpage; 1738*4a8b575cSRyan Libby u_int format; 1739*4a8b575cSRyan Libby u_int ipers; 1740*4a8b575cSRyan Libby u_int ipers_offpage; 1741*4a8b575cSRyan Libby u_int pages; 1742244f4554SBosko Milekic u_int rsize; 1743a55ebb7cSAndriy Gapon u_int slabsize; 17448355f576SJeff Roberson 1745*4a8b575cSRyan Libby KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 1746*4a8b575cSRyan Libby (keg->uk_size <= UMA_PCPU_ALLOC_SIZE && 1747*4a8b575cSRyan Libby (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0), 1748*4a8b575cSRyan Libby ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b", 1749*4a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_size, keg->uk_flags, 1750*4a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 1751*4a8b575cSRyan Libby KASSERT((keg->uk_flags & 1752*4a8b575cSRyan Libby (UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY)) == 0 || 1753*4a8b575cSRyan Libby (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0, 1754*4a8b575cSRyan Libby ("%s: incompatible flags 0x%b", __func__, keg->uk_flags, 1755*4a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 1756e28a647dSGleb Smirnoff 1757*4a8b575cSRyan Libby alignsize = keg->uk_align + 1; 1758*4a8b575cSRyan Libby format = 0; 1759*4a8b575cSRyan Libby ipers = 0; 1760ad97af7eSGleb Smirnoff 1761ef72505eSJeff Roberson /* 1762ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1763ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1764ef72505eSJeff Roberson * allocation bits for we round it up. 1765ef72505eSJeff Roberson */ 1766*4a8b575cSRyan Libby rsize = MAX(keg->uk_size, UMA_SLAB_SIZE / SLAB_MAX_SETSIZE); 1767*4a8b575cSRyan Libby rsize = roundup2(rsize, alignsize); 1768ad97af7eSGleb Smirnoff 1769*4a8b575cSRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) { 1770*4a8b575cSRyan Libby slabsize = UMA_PCPU_ALLOC_SIZE; 1771*4a8b575cSRyan Libby pages = mp_maxid + 1; 1772*4a8b575cSRyan Libby } else if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) { 17739b78b1f4SJeff Roberson /* 1774*4a8b575cSRyan Libby * We want one item to start on every align boundary in a page. 1775*4a8b575cSRyan Libby * To do this we will span pages. We will also extend the item 1776*4a8b575cSRyan Libby * by the size of align if it is an even multiple of align. 1777*4a8b575cSRyan Libby * Otherwise, it would fall on the same boundary every time. 17789b78b1f4SJeff Roberson */ 1779*4a8b575cSRyan Libby if ((rsize & alignsize) == 0) 1780*4a8b575cSRyan Libby rsize += alignsize; 1781*4a8b575cSRyan Libby slabsize = rsize * (PAGE_SIZE / alignsize); 1782*4a8b575cSRyan Libby slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE); 1783*4a8b575cSRyan Libby slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE); 1784*4a8b575cSRyan Libby pages = howmany(slabsize, PAGE_SIZE); 1785*4a8b575cSRyan Libby slabsize = ptoa(pages); 1786*4a8b575cSRyan Libby } else { 1787*4a8b575cSRyan Libby /* 1788*4a8b575cSRyan Libby * Choose a slab size of as many pages as it takes to represent 1789*4a8b575cSRyan Libby * a single item. We will then try to fit as many additional 1790*4a8b575cSRyan Libby * items into the slab as possible. At some point, we may want 1791*4a8b575cSRyan Libby * to increase the slab size for awkward item sizes in order to 1792*4a8b575cSRyan Libby * increase efficiency. 1793*4a8b575cSRyan Libby */ 1794*4a8b575cSRyan Libby pages = howmany(keg->uk_size, PAGE_SIZE); 1795*4a8b575cSRyan Libby slabsize = ptoa(pages); 17961ca6ed45SGleb Smirnoff } 1797ad97af7eSGleb Smirnoff 1798*4a8b575cSRyan Libby /* Evaluate an inline slab layout. */ 1799*4a8b575cSRyan Libby if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0) 1800*4a8b575cSRyan Libby ipers = slab_ipers_hdr(keg->uk_size, rsize, slabsize, true); 1801*4a8b575cSRyan Libby 1802*4a8b575cSRyan Libby /* TODO: vm_page-embedded slab. */ 1803244f4554SBosko Milekic 180420e8e865SBosko Milekic /* 1805244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 180620e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 18076fd34d6fSJeff Roberson * may end up going to the VM for slabs which we do not 18086fd34d6fSJeff Roberson * want to do if we're UMA_ZFLAG_CACHEONLY as a result 18096fd34d6fSJeff Roberson * of UMA_ZONE_VM, which clearly forbids it. 181020e8e865SBosko Milekic */ 1811*4a8b575cSRyan Libby if ((keg->uk_flags & 1812*4a8b575cSRyan Libby (UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY)) != 0) { 1813*4a8b575cSRyan Libby if (ipers == 0) { 1814*4a8b575cSRyan Libby /* We need an extra page for the slab header. */ 1815*4a8b575cSRyan Libby pages++; 1816*4a8b575cSRyan Libby slabsize = ptoa(pages); 1817*4a8b575cSRyan Libby ipers = slab_ipers_hdr(keg->uk_size, rsize, slabsize, 1818*4a8b575cSRyan Libby true); 1819*4a8b575cSRyan Libby } 1820*4a8b575cSRyan Libby goto out; 182154c5ae80SRyan Libby } 1822244f4554SBosko Milekic 1823ef72505eSJeff Roberson /* 1824*4a8b575cSRyan Libby * See if using an OFFPAGE slab will improve our efficiency. 1825*4a8b575cSRyan Libby * Only do this if we are below our efficiency threshold. 1826ef72505eSJeff Roberson * 1827ef72505eSJeff Roberson * XXX We could try growing slabsize to limit max waste as well. 1828ef72505eSJeff Roberson * Historically this was not done because the VM could not 1829ef72505eSJeff Roberson * efficiently handle contiguous allocations. 1830ef72505eSJeff Roberson */ 1831*4a8b575cSRyan Libby eff = UMA_FRAC_FIXPT(ipers * rsize, slabsize); 1832*4a8b575cSRyan Libby ipers_offpage = slab_ipers_hdr(keg->uk_size, rsize, slabsize, false); 1833*4a8b575cSRyan Libby eff_offpage = UMA_FRAC_FIXPT(ipers_offpage * rsize, 1834*4a8b575cSRyan Libby slabsize + slab_sizeof(SLAB_MAX_SETSIZE)); 1835*4a8b575cSRyan Libby if (ipers == 0 || (eff < UMA_MIN_EFF && eff < eff_offpage)) { 1836*4a8b575cSRyan Libby CTR5(KTR_UMA, "UMA decided we need offpage slab headers for " 1837*4a8b575cSRyan Libby "keg: %s(%p), minimum efficiency allowed = %u%%, " 1838*4a8b575cSRyan Libby "old efficiency = %u%%, offpage efficiency = %u%%\n", 1839*4a8b575cSRyan Libby keg->uk_name, keg, UMA_FIXPT_PCT(UMA_MIN_EFF), 1840*4a8b575cSRyan Libby UMA_FIXPT_PCT(eff), UMA_FIXPT_PCT(eff_offpage)); 1841*4a8b575cSRyan Libby format = UMA_ZFLAG_OFFPAGE; 1842*4a8b575cSRyan Libby ipers = ipers_offpage; 18438355f576SJeff Roberson } 1844ad97af7eSGleb Smirnoff 1845*4a8b575cSRyan Libby out: 1846*4a8b575cSRyan Libby /* 1847*4a8b575cSRyan Libby * How do we find the slab header if it is offpage or if not all item 1848*4a8b575cSRyan Libby * start addresses are in the same page? We could solve the latter 1849*4a8b575cSRyan Libby * case with vaddr alignment, but we don't. 1850*4a8b575cSRyan Libby */ 1851*4a8b575cSRyan Libby if ((format & UMA_ZFLAG_OFFPAGE) != 0 || 1852*4a8b575cSRyan Libby (ipers - 1) * rsize >= PAGE_SIZE) { 185354c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0) 1854*4a8b575cSRyan Libby format |= UMA_ZFLAG_HASH; 185554c5ae80SRyan Libby else 1856*4a8b575cSRyan Libby format |= UMA_ZFLAG_VTOSLAB; 185754c5ae80SRyan Libby } 1858*4a8b575cSRyan Libby keg->uk_ipers = ipers; 1859e20a199fSJeff Roberson keg->uk_rsize = rsize; 1860*4a8b575cSRyan Libby keg->uk_flags |= format; 1861e20a199fSJeff Roberson keg->uk_ppera = pages; 1862*4a8b575cSRyan Libby CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u\n", 1863*4a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_flags, rsize, ipers, pages); 1864*4a8b575cSRyan Libby KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 1865*4a8b575cSRyan Libby ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__, 1866*4a8b575cSRyan Libby keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize, ipers, 1867*4a8b575cSRyan Libby pages)); 1868e20a199fSJeff Roberson } 1869e20a199fSJeff Roberson 18708355f576SJeff Roberson /* 1871099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 1872099a0e58SBosko Milekic * the keg onto the global keg list. 18738355f576SJeff Roberson * 18748355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 1875099a0e58SBosko Milekic * udata Actually uma_kctor_args 1876099a0e58SBosko Milekic */ 1877b23f72e9SBrian Feldman static int 1878b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 1879099a0e58SBosko Milekic { 1880099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 1881099a0e58SBosko Milekic uma_keg_t keg = mem; 1882099a0e58SBosko Milekic uma_zone_t zone; 18838b987a77SJeff Roberson int i; 1884099a0e58SBosko Milekic 1885099a0e58SBosko Milekic bzero(keg, size); 1886099a0e58SBosko Milekic keg->uk_size = arg->size; 1887099a0e58SBosko Milekic keg->uk_init = arg->uminit; 1888099a0e58SBosko Milekic keg->uk_fini = arg->fini; 1889099a0e58SBosko Milekic keg->uk_align = arg->align; 18906fd34d6fSJeff Roberson keg->uk_reserve = 0; 1891099a0e58SBosko Milekic keg->uk_flags = arg->flags; 1892099a0e58SBosko Milekic keg->uk_slabzone = NULL; 1893099a0e58SBosko Milekic 1894099a0e58SBosko Milekic /* 1895194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 1896dfe13344SJeff Roberson * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which 1897dfe13344SJeff Roberson * case the iterator is never run. 1898194a979eSMark Johnston */ 1899194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 1900194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 1901194a979eSMark Johnston 1902194a979eSMark Johnston /* 1903099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 1904099a0e58SBosko Milekic */ 1905099a0e58SBosko Milekic zone = arg->zone; 1906e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 1907099a0e58SBosko Milekic 1908099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_VM) 1909099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 1910099a0e58SBosko Milekic 1911099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 1912099a0e58SBosko Milekic keg->uk_init = zero_init; 1913099a0e58SBosko Milekic 1914cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 191554c5ae80SRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 1916e20a199fSJeff Roberson 191754c5ae80SRyan Libby #ifndef SMP 1918ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 1919ad97af7eSGleb Smirnoff #endif 1920ad97af7eSGleb Smirnoff 1921*4a8b575cSRyan Libby keg_layout(keg); 1922099a0e58SBosko Milekic 19238b987a77SJeff Roberson /* 1924dfe13344SJeff Roberson * Use a first-touch NUMA policy for all kegs that pmap_extract() 1925dfe13344SJeff Roberson * will work on with the exception of critical VM structures 1926dfe13344SJeff Roberson * necessary for paging. 1927dfe13344SJeff Roberson * 1928dfe13344SJeff Roberson * Zones may override the default by specifying either. 19298b987a77SJeff Roberson */ 1930dfe13344SJeff Roberson #ifdef NUMA 1931dfe13344SJeff Roberson if ((keg->uk_flags & 193254c5ae80SRyan Libby (UMA_ZFLAG_HASH | UMA_ZONE_VM | UMA_ZONE_ROUNDROBIN)) == 0) 1933dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_FIRSTTOUCH; 1934dfe13344SJeff Roberson else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0) 1935dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_ROUNDROBIN; 19368b987a77SJeff Roberson #endif 19378b987a77SJeff Roberson 193854c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 1939099a0e58SBosko Milekic keg->uk_slabzone = slabzone; 1940099a0e58SBosko Milekic 1941099a0e58SBosko Milekic /* 1942099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 1943099a0e58SBosko Milekic * startup cache until the vm is ready. 1944099a0e58SBosko Milekic */ 1945f4bef67cSGleb Smirnoff if (booted < BOOT_PAGEALLOC) 19468cd02d00SAlan Cox keg->uk_allocf = startup_alloc; 194777e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 194877e19437SGleb Smirnoff else if (keg->uk_ppera == 1) 194977e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 19508cd02d00SAlan Cox #endif 1951ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 1952ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 195377e19437SGleb Smirnoff else 195477e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 195577e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 195677e19437SGleb Smirnoff if (keg->uk_ppera == 1) 195777e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 195877e19437SGleb Smirnoff else 195977e19437SGleb Smirnoff #endif 1960ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 1961ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 1962ab3059a8SMatt Macy else 196377e19437SGleb Smirnoff keg->uk_freef = page_free; 1964099a0e58SBosko Milekic 1965099a0e58SBosko Milekic /* 19668b987a77SJeff Roberson * Initialize keg's locks. 1967099a0e58SBosko Milekic */ 19688b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 19698b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 1970099a0e58SBosko Milekic 1971099a0e58SBosko Milekic /* 1972099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 19739b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 19749b78b1f4SJeff Roberson * definition. 1975099a0e58SBosko Milekic */ 197654c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) { 19779b78b1f4SJeff Roberson size_t shsize; 19789b78b1f4SJeff Roberson 19799b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 19809b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 1981244f4554SBosko Milekic /* 1982244f4554SBosko Milekic * The only way the following is possible is if with our 1983244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 1984244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 1985244f4554SBosko Milekic * mathematically possible for all cases, so we make 1986244f4554SBosko Milekic * sure here anyway. 1987244f4554SBosko Milekic */ 19889b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 19893d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 19903d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 1991099a0e58SBosko Milekic } 1992099a0e58SBosko Milekic 199354c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 19943b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 1995099a0e58SBosko Milekic 19968b987a77SJeff Roberson CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)\n", keg, zone->uz_name, zone); 1997099a0e58SBosko Milekic 1998099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 1999099a0e58SBosko Milekic 2000111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2001099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2002111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2003b23f72e9SBrian Feldman return (0); 2004099a0e58SBosko Milekic } 2005099a0e58SBosko Milekic 20062efcc8cbSGleb Smirnoff static void 200720a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 20082efcc8cbSGleb Smirnoff { 20092efcc8cbSGleb Smirnoff 20102efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 20112efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 20122efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 20132efcc8cbSGleb Smirnoff } 20142efcc8cbSGleb Smirnoff 201520a4e154SJeff Roberson static void 201620a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 201720a4e154SJeff Roberson { 201820a4e154SJeff Roberson uma_zone_domain_t zdom; 20198b987a77SJeff Roberson uma_domain_t dom; 202020a4e154SJeff Roberson uma_keg_t keg; 202120a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 20223b490537SJeff Roberson int domains, i, cnt; 202320a4e154SJeff Roberson static const char *nokeg = "cache zone"; 202420a4e154SJeff Roberson char *c; 202520a4e154SJeff Roberson 202620a4e154SJeff Roberson /* 202720a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 202820a4e154SJeff Roberson * any special characters and handling dups by appending 202920a4e154SJeff Roberson * an index. 203020a4e154SJeff Roberson */ 203120a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 20323b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 20333b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 20343b490537SJeff Roberson cnt /= 10; 20353b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 20363b490537SJeff Roberson M_UMA, M_WAITOK); 203720a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 203820a4e154SJeff Roberson zone->uz_namecnt); 203920a4e154SJeff Roberson } else 204020a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 204120a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 204220a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 204320a4e154SJeff Roberson *c = '_'; 204420a4e154SJeff Roberson 204520a4e154SJeff Roberson /* 204620a4e154SJeff Roberson * Basic parameters at the root. 204720a4e154SJeff Roberson */ 204820a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 204920a4e154SJeff Roberson OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, ""); 205020a4e154SJeff Roberson oid = zone->uz_oid; 205120a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 205220a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 20536d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 20546d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 20556d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 205620a4e154SJeff Roberson "Allocator configuration flags"); 205720a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 205820a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 205920a4e154SJeff Roberson "Desired per-cpu cache size"); 206020a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 206120a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 206220a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 206320a4e154SJeff Roberson 206420a4e154SJeff Roberson /* 206520a4e154SJeff Roberson * keg if present. 206620a4e154SJeff Roberson */ 206754c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 20688b987a77SJeff Roberson domains = vm_ndomains; 20698b987a77SJeff Roberson else 20708b987a77SJeff Roberson domains = 1; 207120a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 207220a4e154SJeff Roberson "keg", CTLFLAG_RD, NULL, ""); 207320a4e154SJeff Roberson keg = zone->uz_keg; 20743b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 207520a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 207620a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 207720a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 207820a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 207920a4e154SJeff Roberson "Real object size with alignment"); 208020a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 208120a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 208220a4e154SJeff Roberson "pages per-slab allocation"); 208320a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 208420a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 208520a4e154SJeff Roberson "items available per-slab"); 208620a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 208720a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 208820a4e154SJeff Roberson "item alignment mask"); 2089f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2090f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2091f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2092f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 20938b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 20948b987a77SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 20958b987a77SJeff Roberson for (i = 0; i < domains; i++) { 20968b987a77SJeff Roberson dom = &keg->uk_domain[i]; 20978b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 20988b987a77SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, 20998b987a77SJeff Roberson NULL, ""); 21008b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21018b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 21028b987a77SJeff Roberson "Total pages currently allocated from VM"); 21038b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21048b987a77SJeff Roberson "free", CTLFLAG_RD, &dom->ud_free, 0, 21058b987a77SJeff Roberson "items free in the slab layer"); 21068b987a77SJeff Roberson } 210720a4e154SJeff Roberson } else 210820a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210920a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 211020a4e154SJeff Roberson 211120a4e154SJeff Roberson /* 211220a4e154SJeff Roberson * Information about zone limits. 211320a4e154SJeff Roberson */ 211420a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 211520a4e154SJeff Roberson "limit", CTLFLAG_RD, NULL, ""); 21164bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21174bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 21184bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 21194bd61e19SJeff Roberson "current number of allocated items if limit is set"); 212020a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 212120a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 212220a4e154SJeff Roberson "Maximum number of cached items"); 212320a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 212420a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 212520a4e154SJeff Roberson "Number of threads sleeping at limit"); 212620a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 212720a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 212820a4e154SJeff Roberson "Total zone limit sleeps"); 21294bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21304bd61e19SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bkt_max, 0, 21314bd61e19SJeff Roberson "Maximum number of items in the bucket cache"); 21324bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21334bd61e19SJeff Roberson "bucket_cnt", CTLFLAG_RD, &zone->uz_bkt_count, 0, 21344bd61e19SJeff Roberson "Number of items in the bucket cache"); 213520a4e154SJeff Roberson 213620a4e154SJeff Roberson /* 21378b987a77SJeff Roberson * Per-domain zone information. 213820a4e154SJeff Roberson */ 213920a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 214020a4e154SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 2141dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0) 21428b987a77SJeff Roberson domains = 1; 214320a4e154SJeff Roberson for (i = 0; i < domains; i++) { 214420a4e154SJeff Roberson zdom = &zone->uz_domain[i]; 214520a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 214620a4e154SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, ""); 214720a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 214820a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 214920a4e154SJeff Roberson "number of items in this domain"); 215020a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 215120a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 215220a4e154SJeff Roberson "maximum item count in this period"); 215320a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 215420a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 215520a4e154SJeff Roberson "minimum item count in this period"); 215620a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 215720a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 215820a4e154SJeff Roberson "Working set size"); 215920a4e154SJeff Roberson } 216020a4e154SJeff Roberson 216120a4e154SJeff Roberson /* 216220a4e154SJeff Roberson * General statistics. 216320a4e154SJeff Roberson */ 216420a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 216520a4e154SJeff Roberson "stats", CTLFLAG_RD, NULL, ""); 216620a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 216720a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 216820a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 216920a4e154SJeff Roberson "Current number of allocated items"); 217020a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 217120a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 217220a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 217320a4e154SJeff Roberson "Total allocation calls"); 217420a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 217520a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 217620a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 217720a4e154SJeff Roberson "Total free calls"); 217820a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 217920a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 218020a4e154SJeff Roberson "Number of allocation failures"); 218120a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 218220a4e154SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 0, 218320a4e154SJeff Roberson "Free calls from the wrong domain"); 218420a4e154SJeff Roberson } 218520a4e154SJeff Roberson 218620a4e154SJeff Roberson struct uma_zone_count { 218720a4e154SJeff Roberson const char *name; 218820a4e154SJeff Roberson int count; 218920a4e154SJeff Roberson }; 219020a4e154SJeff Roberson 219120a4e154SJeff Roberson static void 219220a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 219320a4e154SJeff Roberson { 219420a4e154SJeff Roberson struct uma_zone_count *cnt; 219520a4e154SJeff Roberson 219620a4e154SJeff Roberson cnt = arg; 21973b490537SJeff Roberson /* 21983b490537SJeff Roberson * Some zones are rapidly created with identical names and 21993b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 22003b490537SJeff Roberson * Use one greater than the maximum observed for this name. 22013b490537SJeff Roberson */ 220220a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 22033b490537SJeff Roberson cnt->count = MAX(cnt->count, 22043b490537SJeff Roberson zone->uz_namecnt + 1); 220520a4e154SJeff Roberson } 220620a4e154SJeff Roberson 2207cc7ce83aSJeff Roberson static void 2208cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2209cc7ce83aSJeff Roberson { 2210cc7ce83aSJeff Roberson int i; 2211cc7ce83aSJeff Roberson 2212cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2213cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2214cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2215cc7ce83aSJeff Roberson } 2216cc7ce83aSJeff Roberson } 2217cc7ce83aSJeff Roberson 2218099a0e58SBosko Milekic /* 2219099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2220099a0e58SBosko Milekic * 2221099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2222099a0e58SBosko Milekic * udata Actually uma_zctor_args 22238355f576SJeff Roberson */ 2224b23f72e9SBrian Feldman static int 2225b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 22268355f576SJeff Roberson { 222720a4e154SJeff Roberson struct uma_zone_count cnt; 22288355f576SJeff Roberson struct uma_zctor_args *arg = udata; 22298355f576SJeff Roberson uma_zone_t zone = mem; 2230099a0e58SBosko Milekic uma_zone_t z; 2231099a0e58SBosko Milekic uma_keg_t keg; 223208cfa56eSMark Johnston int i; 22338355f576SJeff Roberson 22348355f576SJeff Roberson bzero(zone, size); 22358355f576SJeff Roberson zone->uz_name = arg->name; 22368355f576SJeff Roberson zone->uz_ctor = arg->ctor; 22378355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2238099a0e58SBosko Milekic zone->uz_init = NULL; 2239099a0e58SBosko Milekic zone->uz_fini = NULL; 2240bf965959SSean Bruno zone->uz_sleeps = 0; 2241c1685086SJeff Roberson zone->uz_xdomain = 0; 224220a4e154SJeff Roberson zone->uz_bucket_size = 0; 224320a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 224420a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2245e20a199fSJeff Roberson zone->uz_flags = 0; 22462f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2247ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 2248ab3185d1SJeff Roberson zone->uz_domain = (struct uma_zone_domain *)&zone->uz_cpu[mp_ncpus]; 2249bb15d1c7SGleb Smirnoff zone->uz_bkt_max = ULONG_MAX; 22502f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2251af526374SJeff Roberson 225220a4e154SJeff Roberson /* Count the number of duplicate names. */ 225320a4e154SJeff Roberson cnt.name = arg->name; 225420a4e154SJeff Roberson cnt.count = 0; 225520a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 225620a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 2257727c6918SJeff Roberson ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 225891d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 22592efcc8cbSGleb Smirnoff 226008cfa56eSMark Johnston for (i = 0; i < vm_ndomains; i++) 226108cfa56eSMark Johnston TAILQ_INIT(&zone->uz_domain[i].uzd_buckets); 226208cfa56eSMark Johnston 2263ca293436SRyan Libby #ifdef INVARIANTS 2264ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2265cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 2266ca293436SRyan Libby #endif 2267ca293436SRyan Libby 22680095a784SJeff Roberson /* 22690095a784SJeff Roberson * This is a pure cache zone, no kegs. 22700095a784SJeff Roberson */ 22710095a784SJeff Roberson if (arg->import) { 2272727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2273727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 22746fd34d6fSJeff Roberson if (arg->flags & UMA_ZONE_VM) 22756fd34d6fSJeff Roberson arg->flags |= UMA_ZFLAG_CACHEONLY; 22766fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2277af526374SJeff Roberson zone->uz_size = arg->size; 22780095a784SJeff Roberson zone->uz_import = arg->import; 22790095a784SJeff Roberson zone->uz_release = arg->release; 22800095a784SJeff Roberson zone->uz_arg = arg->arg; 2281111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 228203175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2283111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2284af526374SJeff Roberson goto out; 22850095a784SJeff Roberson } 22860095a784SJeff Roberson 22870095a784SJeff Roberson /* 22880095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 22890095a784SJeff Roberson */ 2290b75c4efcSAndrew Turner zone->uz_import = zone_import; 2291b75c4efcSAndrew Turner zone->uz_release = zone_release; 22920095a784SJeff Roberson zone->uz_arg = zone; 2293bb15d1c7SGleb Smirnoff keg = arg->keg; 22940095a784SJeff Roberson 2295099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 229620a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 229720a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2298099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 22998355f576SJeff Roberson zone->uz_init = arg->uminit; 2300e221e841SJeff Roberson zone->uz_fini = arg->fini; 2301e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2302111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2303099a0e58SBosko Milekic ZONE_LOCK(zone); 2304099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2305099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2306099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2307099a0e58SBosko Milekic break; 2308099a0e58SBosko Milekic } 2309099a0e58SBosko Milekic } 2310099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2311111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2312e20a199fSJeff Roberson } else if (keg == NULL) { 2313e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2314e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2315b23f72e9SBrian Feldman return (ENOMEM); 2316099a0e58SBosko Milekic } else { 2317099a0e58SBosko Milekic struct uma_kctor_args karg; 2318b23f72e9SBrian Feldman int error; 2319099a0e58SBosko Milekic 2320099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2321099a0e58SBosko Milekic karg.size = arg->size; 2322099a0e58SBosko Milekic karg.uminit = arg->uminit; 2323099a0e58SBosko Milekic karg.fini = arg->fini; 2324099a0e58SBosko Milekic karg.align = arg->align; 2325099a0e58SBosko Milekic karg.flags = arg->flags; 2326099a0e58SBosko Milekic karg.zone = zone; 2327b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2328b23f72e9SBrian Feldman flags); 2329b23f72e9SBrian Feldman if (error) 2330b23f72e9SBrian Feldman return (error); 2331099a0e58SBosko Milekic } 23320095a784SJeff Roberson 233320a4e154SJeff Roberson /* Inherit properties from the keg. */ 2334bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2335e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2336e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2337e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 23388355f576SJeff Roberson 233920a4e154SJeff Roberson out: 234020a4e154SJeff Roberson if (__predict_true(booted == BOOT_RUNNING)) { 234120a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 234220a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 234320a4e154SJeff Roberson } else { 234420a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 234520a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 234620a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2347099a0e58SBosko Milekic } 23488355f576SJeff Roberson 23497e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 23507e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 23517e28037aSMark Johnston ("Invalid zone flag combination")); 235220a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 235320a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 235420a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 235520a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 235620a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) 235720a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN; 235820a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 235920a4e154SJeff Roberson zone->uz_bucket_size = 0; 23607e28037aSMark Johnston else 236120a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 236220a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2363cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2364cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2365cc7ce83aSJeff Roberson zone_update_caches(zone); 2366fc03d22bSJeff Roberson 2367b23f72e9SBrian Feldman return (0); 23688355f576SJeff Roberson } 23698355f576SJeff Roberson 23708355f576SJeff Roberson /* 2371099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2372099a0e58SBosko Milekic * table and removes the keg from the global list. 23739c2cd7e5SJeff Roberson * 23749c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 23759c2cd7e5SJeff Roberson * udata unused 23769c2cd7e5SJeff Roberson */ 2377099a0e58SBosko Milekic static void 2378099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2379099a0e58SBosko Milekic { 2380099a0e58SBosko Milekic uma_keg_t keg; 23818b987a77SJeff Roberson uint32_t free, pages; 23828b987a77SJeff Roberson int i; 23839c2cd7e5SJeff Roberson 2384099a0e58SBosko Milekic keg = (uma_keg_t)arg; 23858b987a77SJeff Roberson free = pages = 0; 23868b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 23878b987a77SJeff Roberson free += keg->uk_domain[i].ud_free; 23888b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 23898b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2390099a0e58SBosko Milekic } 23918b987a77SJeff Roberson if (free != 0) 23928b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 23938b987a77SJeff Roberson " Lost %u pages of memory.\n", 23948b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 23958b987a77SJeff Roberson free, pages); 2396099a0e58SBosko Milekic 2397099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2398099a0e58SBosko Milekic } 2399099a0e58SBosko Milekic 2400099a0e58SBosko Milekic /* 2401099a0e58SBosko Milekic * Zone header dtor. 2402099a0e58SBosko Milekic * 2403099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2404099a0e58SBosko Milekic * udata unused 2405099a0e58SBosko Milekic */ 24069c2cd7e5SJeff Roberson static void 24079c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 24089c2cd7e5SJeff Roberson { 24099c2cd7e5SJeff Roberson uma_zone_t zone; 2410099a0e58SBosko Milekic uma_keg_t keg; 24119c2cd7e5SJeff Roberson 24129c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 24139643769aSJeff Roberson 241420a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 241520a4e154SJeff Roberson 2416e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 24179643769aSJeff Roberson cache_drain(zone); 2418099a0e58SBosko Milekic 2419111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2420099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2421111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2422099a0e58SBosko Milekic /* 2423099a0e58SBosko Milekic * XXX there are some races here where 2424099a0e58SBosko Milekic * the zone can be drained but zone lock 2425099a0e58SBosko Milekic * released and then refilled before we 2426099a0e58SBosko Milekic * remove it... we dont care for now 2427099a0e58SBosko Milekic */ 242808cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2429e20a199fSJeff Roberson /* 2430323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2431e20a199fSJeff Roberson */ 2432323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2433323ad386STycho Nightingale keg = zone->uz_keg; 2434111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2435099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2436111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 24370095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 24389c2cd7e5SJeff Roberson } 24392efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 24402efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 24412efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 244220a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2443af526374SJeff Roberson ZONE_LOCK_FINI(zone); 244491d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 2445099a0e58SBosko Milekic } 2446099a0e58SBosko Milekic 24479c2cd7e5SJeff Roberson /* 24488355f576SJeff Roberson * Traverses every zone in the system and calls a callback 24498355f576SJeff Roberson * 24508355f576SJeff Roberson * Arguments: 24518355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 24528355f576SJeff Roberson * as an argument. 24538355f576SJeff Roberson * 24548355f576SJeff Roberson * Returns: 24558355f576SJeff Roberson * Nothing 24568355f576SJeff Roberson */ 24578355f576SJeff Roberson static void 245820a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 24598355f576SJeff Roberson { 2460099a0e58SBosko Milekic uma_keg_t keg; 24618355f576SJeff Roberson uma_zone_t zone; 24628355f576SJeff Roberson 24632efcc8cbSGleb Smirnoff /* 24642efcc8cbSGleb Smirnoff * Before BOOT_RUNNING we are guaranteed to be single 24652efcc8cbSGleb Smirnoff * threaded, so locking isn't needed. Startup functions 24662efcc8cbSGleb Smirnoff * are allowed to use M_WAITOK. 24672efcc8cbSGleb Smirnoff */ 24682efcc8cbSGleb Smirnoff if (__predict_true(booted == BOOT_RUNNING)) 2469111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2470099a0e58SBosko Milekic LIST_FOREACH(keg, &uma_kegs, uk_link) { 2471099a0e58SBosko Milekic LIST_FOREACH(zone, &keg->uk_zones, uz_link) 247220a4e154SJeff Roberson zfunc(zone, arg); 2473099a0e58SBosko Milekic } 247408034d10SKonstantin Belousov LIST_FOREACH(zone, &uma_cachezones, uz_link) 247520a4e154SJeff Roberson zfunc(zone, arg); 24762efcc8cbSGleb Smirnoff if (__predict_true(booted == BOOT_RUNNING)) 2477111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 24788355f576SJeff Roberson } 24798355f576SJeff Roberson 2480f4bef67cSGleb Smirnoff /* 2481f4bef67cSGleb Smirnoff * Count how many pages do we need to bootstrap. VM supplies 2482f4bef67cSGleb Smirnoff * its need in early zones in the argument, we add up our zones, 2483325c4cedSMark Johnston * which consist of the UMA Slabs, UMA Hash and 9 Bucket zones. The 2484f4bef67cSGleb Smirnoff * zone of zones and zone of kegs are accounted separately. 2485f4bef67cSGleb Smirnoff */ 2486325c4cedSMark Johnston #define UMA_BOOT_ZONES 11 2487f4bef67cSGleb Smirnoff static int zsize, ksize; 2488f4bef67cSGleb Smirnoff int 2489f7d35785SGleb Smirnoff uma_startup_count(int vm_zones) 2490f4bef67cSGleb Smirnoff { 2491f7d35785SGleb Smirnoff int zones, pages; 249279c9f942SJeff Roberson u_int zppera, zipers; 249379c9f942SJeff Roberson u_int kppera, kipers; 24949b78b1f4SJeff Roberson size_t space, size; 2495f4bef67cSGleb Smirnoff 2496f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2497f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 249879c9f942SJeff Roberson ksize = roundup(ksize, UMA_SUPER_ALIGN); 2499f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2500f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2501f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 250279c9f942SJeff Roberson zsize = roundup(zsize, UMA_SUPER_ALIGN); 2503f4bef67cSGleb Smirnoff 25045073a083SGleb Smirnoff /* 250579c9f942SJeff Roberson * Memory for the zone of kegs and its keg, and for zone 250679c9f942SJeff Roberson * of zones. Allocated directly in uma_startup(). 25075073a083SGleb Smirnoff */ 250879c9f942SJeff Roberson pages = howmany(zsize * 2 + ksize, PAGE_SIZE); 2509f4bef67cSGleb Smirnoff 2510f7d35785SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2511f7d35785SGleb Smirnoff zones = UMA_BOOT_ZONES; 2512f7d35785SGleb Smirnoff #else 2513f7d35785SGleb Smirnoff zones = UMA_BOOT_ZONES + vm_zones; 2514f7d35785SGleb Smirnoff vm_zones = 0; 2515f7d35785SGleb Smirnoff #endif 25169b78b1f4SJeff Roberson size = slab_sizeof(SLAB_MAX_SETSIZE); 25179b78b1f4SJeff Roberson space = slab_space(SLAB_MAX_SETSIZE); 2518f4bef67cSGleb Smirnoff 25195073a083SGleb Smirnoff /* Memory for the rest of startup zones, UMA and VM, ... */ 25209b78b1f4SJeff Roberson if (zsize > space) { 25210b2e3aeaSGleb Smirnoff /* See keg_large_init(). */ 252279c9f942SJeff Roberson zppera = howmany(zsize + slab_sizeof(1), PAGE_SIZE); 252379c9f942SJeff Roberson zipers = 1; 252479c9f942SJeff Roberson zones += vm_zones; 252579c9f942SJeff Roberson } else { 252679c9f942SJeff Roberson zppera = 1; 252779c9f942SJeff Roberson zipers = space / zsize; 252879c9f942SJeff Roberson } 252979c9f942SJeff Roberson pages += howmany(zones, zipers) * zppera; 2530f4bef67cSGleb Smirnoff 25315073a083SGleb Smirnoff /* ... and their kegs. Note that zone of zones allocates a keg! */ 253279c9f942SJeff Roberson if (ksize > space) { 253379c9f942SJeff Roberson /* See keg_large_init(). */ 253479c9f942SJeff Roberson kppera = howmany(ksize + slab_sizeof(1), PAGE_SIZE); 253579c9f942SJeff Roberson kipers = 1; 253679c9f942SJeff Roberson } else { 253779c9f942SJeff Roberson kppera = 1; 253879c9f942SJeff Roberson kipers = space / ksize; 253979c9f942SJeff Roberson } 254079c9f942SJeff Roberson pages += howmany(zones + 1, kipers) * kppera; 254179c9f942SJeff Roberson 254279c9f942SJeff Roberson /* 254379c9f942SJeff Roberson * Allocate an additional slab for zones and kegs on NUMA 254479c9f942SJeff Roberson * systems. The round-robin allocation policy will populate at 254579c9f942SJeff Roberson * least one slab per-domain. 254679c9f942SJeff Roberson */ 254779c9f942SJeff Roberson pages += (vm_ndomains - 1) * (zppera + kppera); 2548f4bef67cSGleb Smirnoff 2549f4bef67cSGleb Smirnoff return (pages); 2550f4bef67cSGleb Smirnoff } 2551f4bef67cSGleb Smirnoff 25528355f576SJeff Roberson void 2553ac0a6fd0SGleb Smirnoff uma_startup(void *mem, int npages) 25548355f576SJeff Roberson { 25558355f576SJeff Roberson struct uma_zctor_args args; 2556ab3185d1SJeff Roberson uma_keg_t masterkeg; 2557ab3185d1SJeff Roberson uintptr_t m; 2558f4bef67cSGleb Smirnoff 2559f4bef67cSGleb Smirnoff #ifdef DIAGNOSTIC 2560f4bef67cSGleb Smirnoff printf("Entering %s with %d boot pages configured\n", __func__, npages); 2561f4bef67cSGleb Smirnoff #endif 25628355f576SJeff Roberson 2563111fbcd5SBryan Venteicher rw_init(&uma_rwlock, "UMA lock"); 2564099a0e58SBosko Milekic 2565ab3185d1SJeff Roberson /* Use bootpages memory for the zone of zones and zone of kegs. */ 2566ab3185d1SJeff Roberson m = (uintptr_t)mem; 2567ab3185d1SJeff Roberson zones = (uma_zone_t)m; 256879c9f942SJeff Roberson m += zsize; 2569ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 257079c9f942SJeff Roberson m += zsize; 2571ab3185d1SJeff Roberson masterkeg = (uma_keg_t)m; 257279c9f942SJeff Roberson m += ksize; 2573ab3185d1SJeff Roberson m = roundup(m, PAGE_SIZE); 2574ab3185d1SJeff Roberson npages -= (m - (uintptr_t)mem) / PAGE_SIZE; 2575ab3185d1SJeff Roberson mem = (void *)m; 2576ab3185d1SJeff Roberson 2577099a0e58SBosko Milekic /* "manually" create the initial zone */ 25780095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2579099a0e58SBosko Milekic args.name = "UMA Kegs"; 2580ab3185d1SJeff Roberson args.size = ksize; 2581099a0e58SBosko Milekic args.ctor = keg_ctor; 2582099a0e58SBosko Milekic args.dtor = keg_dtor; 25838355f576SJeff Roberson args.uminit = zero_init; 25848355f576SJeff Roberson args.fini = NULL; 2585ab3185d1SJeff Roberson args.keg = masterkeg; 258679c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2587b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2588ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 25898355f576SJeff Roberson 2590ac0a6fd0SGleb Smirnoff bootmem = mem; 2591ac0a6fd0SGleb Smirnoff boot_pages = npages; 25928355f576SJeff Roberson 2593099a0e58SBosko Milekic args.name = "UMA Zones"; 2594f4bef67cSGleb Smirnoff args.size = zsize; 2595099a0e58SBosko Milekic args.ctor = zone_ctor; 2596099a0e58SBosko Milekic args.dtor = zone_dtor; 2597099a0e58SBosko Milekic args.uminit = zero_init; 2598099a0e58SBosko Milekic args.fini = NULL; 2599099a0e58SBosko Milekic args.keg = NULL; 260079c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2601099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2602ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2603099a0e58SBosko Milekic 26048355f576SJeff Roberson /* Now make a zone for slab headers */ 26051e0701e1SJeff Roberson slabzone = uma_zcreate("UMA Slabs", sizeof(struct uma_hash_slab), 26061e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 26078355f576SJeff Roberson 26088355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 26098355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 26101e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 26118355f576SJeff Roberson 2612f4bef67cSGleb Smirnoff booted = BOOT_STRAPPED; 26138355f576SJeff Roberson } 26148355f576SJeff Roberson 2615f4bef67cSGleb Smirnoff void 2616f4bef67cSGleb Smirnoff uma_startup1(void) 2617f4bef67cSGleb Smirnoff { 2618f4bef67cSGleb Smirnoff 2619f4bef67cSGleb Smirnoff #ifdef DIAGNOSTIC 2620f4bef67cSGleb Smirnoff printf("Entering %s with %d boot pages left\n", __func__, boot_pages); 2621f4bef67cSGleb Smirnoff #endif 2622f4bef67cSGleb Smirnoff booted = BOOT_PAGEALLOC; 2623f4bef67cSGleb Smirnoff } 2624f4bef67cSGleb Smirnoff 26258355f576SJeff Roberson void 262699571dc3SJeff Roberson uma_startup2(void) 26278355f576SJeff Roberson { 2628f4bef67cSGleb Smirnoff 2629f7d35785SGleb Smirnoff #ifdef DIAGNOSTIC 2630f7d35785SGleb Smirnoff printf("Entering %s with %d boot pages left\n", __func__, boot_pages); 2631f7d35785SGleb Smirnoff #endif 263208cfa56eSMark Johnston sx_init(&uma_reclaim_lock, "umareclaim"); 26333182660aSRyan Libby bucket_init(); 26343182660aSRyan Libby booted = BOOT_BUCKETS; 2635f4bef67cSGleb Smirnoff bucket_enable(); 26368355f576SJeff Roberson } 26378355f576SJeff Roberson 26388355f576SJeff Roberson /* 26398355f576SJeff Roberson * Initialize our callout handle 26408355f576SJeff Roberson * 26418355f576SJeff Roberson */ 26428355f576SJeff Roberson static void 26438355f576SJeff Roberson uma_startup3(void) 26448355f576SJeff Roberson { 26451431a748SGleb Smirnoff 2646c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2647c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2648c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2649c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2650c5deaf04SGleb Smirnoff #endif 265120a4e154SJeff Roberson zone_foreach(zone_alloc_counters, NULL); 265220a4e154SJeff Roberson zone_foreach(zone_alloc_sysctl, NULL); 2653fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 26549643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2655c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 26568355f576SJeff Roberson } 26578355f576SJeff Roberson 2658e20a199fSJeff Roberson static uma_keg_t 2659099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 266085dcf349SGleb Smirnoff int align, uint32_t flags) 2661099a0e58SBosko Milekic { 2662099a0e58SBosko Milekic struct uma_kctor_args args; 2663099a0e58SBosko Milekic 2664099a0e58SBosko Milekic args.size = size; 2665099a0e58SBosko Milekic args.uminit = uminit; 2666099a0e58SBosko Milekic args.fini = fini; 26671e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2668099a0e58SBosko Milekic args.flags = flags; 2669099a0e58SBosko Milekic args.zone = zone; 2670ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 2671099a0e58SBosko Milekic } 2672099a0e58SBosko Milekic 2673f4bef67cSGleb Smirnoff /* Public functions */ 26748355f576SJeff Roberson /* See uma.h */ 26751e319f6dSRobert Watson void 26761e319f6dSRobert Watson uma_set_align(int align) 26771e319f6dSRobert Watson { 26781e319f6dSRobert Watson 26791e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 26801e319f6dSRobert Watson uma_align_cache = align; 26811e319f6dSRobert Watson } 26821e319f6dSRobert Watson 26831e319f6dSRobert Watson /* See uma.h */ 26848355f576SJeff Roberson uma_zone_t 2685bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 268685dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 26878355f576SJeff Roberson 26888355f576SJeff Roberson { 26898355f576SJeff Roberson struct uma_zctor_args args; 269095c4bf75SKonstantin Belousov uma_zone_t res; 269195c4bf75SKonstantin Belousov bool locked; 26928355f576SJeff Roberson 2693a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 2694a5a35578SJohn Baldwin align, name)); 2695a5a35578SJohn Baldwin 26968355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 26970095a784SJeff Roberson memset(&args, 0, sizeof(args)); 26988355f576SJeff Roberson args.name = name; 26998355f576SJeff Roberson args.size = size; 27008355f576SJeff Roberson args.ctor = ctor; 27018355f576SJeff Roberson args.dtor = dtor; 27028355f576SJeff Roberson args.uminit = uminit; 27038355f576SJeff Roberson args.fini = fini; 2704afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 2705afc6dc36SJohn-Mark Gurney /* 2706ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 2707ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 2708ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 2709ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 2710ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 2711ca293436SRyan Libby * the ctor and dtor do not need to be empty. 2712afc6dc36SJohn-Mark Gurney */ 271354c5ae80SRyan Libby if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH | 271454c5ae80SRyan Libby UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) { 2715afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 2716afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 2717afc6dc36SJohn-Mark Gurney } 2718afc6dc36SJohn-Mark Gurney #endif 27198355f576SJeff Roberson args.align = align; 27208355f576SJeff Roberson args.flags = flags; 2721099a0e58SBosko Milekic args.keg = NULL; 2722099a0e58SBosko Milekic 2723f4bef67cSGleb Smirnoff if (booted < BOOT_BUCKETS) { 272495c4bf75SKonstantin Belousov locked = false; 272595c4bf75SKonstantin Belousov } else { 272608cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 272795c4bf75SKonstantin Belousov locked = true; 272895c4bf75SKonstantin Belousov } 2729ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 273095c4bf75SKonstantin Belousov if (locked) 273108cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 273295c4bf75SKonstantin Belousov return (res); 2733099a0e58SBosko Milekic } 2734099a0e58SBosko Milekic 2735099a0e58SBosko Milekic /* See uma.h */ 2736099a0e58SBosko Milekic uma_zone_t 2737099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 2738099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 2739099a0e58SBosko Milekic { 2740099a0e58SBosko Milekic struct uma_zctor_args args; 2741e20a199fSJeff Roberson uma_keg_t keg; 274295c4bf75SKonstantin Belousov uma_zone_t res; 274395c4bf75SKonstantin Belousov bool locked; 2744099a0e58SBosko Milekic 2745bb15d1c7SGleb Smirnoff keg = master->uz_keg; 27460095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2747099a0e58SBosko Milekic args.name = name; 2748e20a199fSJeff Roberson args.size = keg->uk_size; 2749099a0e58SBosko Milekic args.ctor = ctor; 2750099a0e58SBosko Milekic args.dtor = dtor; 2751099a0e58SBosko Milekic args.uminit = zinit; 2752099a0e58SBosko Milekic args.fini = zfini; 2753e20a199fSJeff Roberson args.align = keg->uk_align; 2754e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 2755e20a199fSJeff Roberson args.keg = keg; 27568355f576SJeff Roberson 2757f4bef67cSGleb Smirnoff if (booted < BOOT_BUCKETS) { 275895c4bf75SKonstantin Belousov locked = false; 275995c4bf75SKonstantin Belousov } else { 276008cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 276195c4bf75SKonstantin Belousov locked = true; 276295c4bf75SKonstantin Belousov } 2763e20a199fSJeff Roberson /* XXX Attaches only one keg of potentially many. */ 2764ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 276595c4bf75SKonstantin Belousov if (locked) 276608cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 276795c4bf75SKonstantin Belousov return (res); 27688355f576SJeff Roberson } 27698355f576SJeff Roberson 27700095a784SJeff Roberson /* See uma.h */ 27710095a784SJeff Roberson uma_zone_t 2772af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 2773af526374SJeff Roberson uma_init zinit, uma_fini zfini, uma_import zimport, 2774af526374SJeff Roberson uma_release zrelease, void *arg, int flags) 27750095a784SJeff Roberson { 27760095a784SJeff Roberson struct uma_zctor_args args; 27770095a784SJeff Roberson 27780095a784SJeff Roberson memset(&args, 0, sizeof(args)); 27790095a784SJeff Roberson args.name = name; 2780af526374SJeff Roberson args.size = size; 27810095a784SJeff Roberson args.ctor = ctor; 27820095a784SJeff Roberson args.dtor = dtor; 27830095a784SJeff Roberson args.uminit = zinit; 27840095a784SJeff Roberson args.fini = zfini; 27850095a784SJeff Roberson args.import = zimport; 27860095a784SJeff Roberson args.release = zrelease; 27870095a784SJeff Roberson args.arg = arg; 27880095a784SJeff Roberson args.align = 0; 2789bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 27900095a784SJeff Roberson 2791ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 27920095a784SJeff Roberson } 27930095a784SJeff Roberson 27948355f576SJeff Roberson /* See uma.h */ 27959c2cd7e5SJeff Roberson void 27969c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 27979c2cd7e5SJeff Roberson { 2798f4ff923bSRobert Watson 279908cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 28000095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 280108cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 28029c2cd7e5SJeff Roberson } 28039c2cd7e5SJeff Roberson 28048d6fbbb8SJeff Roberson void 28058d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 28068d6fbbb8SJeff Roberson { 28078d6fbbb8SJeff Roberson void *item; 28088d6fbbb8SJeff Roberson 28098d6fbbb8SJeff Roberson item = uma_zalloc_arg(zone, NULL, M_WAITOK); 28108d6fbbb8SJeff Roberson uma_zfree(zone, item); 28118d6fbbb8SJeff Roberson } 28128d6fbbb8SJeff Roberson 28134e180881SMateusz Guzik void * 28144e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 28154e180881SMateusz Guzik { 28164e180881SMateusz Guzik void *item; 2817b4799947SRuslan Bukin #ifdef SMP 28184e180881SMateusz Guzik int i; 28194e180881SMateusz Guzik 28204e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2821b4799947SRuslan Bukin #endif 28224e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 28234e180881SMateusz Guzik if (item != NULL && (flags & M_ZERO)) { 2824b4799947SRuslan Bukin #ifdef SMP 2825013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 28264e180881SMateusz Guzik bzero(zpcpu_get_cpu(item, i), zone->uz_size); 2827b4799947SRuslan Bukin #else 2828b4799947SRuslan Bukin bzero(item, zone->uz_size); 2829b4799947SRuslan Bukin #endif 28304e180881SMateusz Guzik } 28314e180881SMateusz Guzik return (item); 28324e180881SMateusz Guzik } 28334e180881SMateusz Guzik 28344e180881SMateusz Guzik /* 28354e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 28364e180881SMateusz Guzik */ 28374e180881SMateusz Guzik void 28384e180881SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata) 28394e180881SMateusz Guzik { 28404e180881SMateusz Guzik 2841c5b7751fSIan Lepore #ifdef SMP 28424e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2843c5b7751fSIan Lepore #endif 28444e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 28454e180881SMateusz Guzik } 28464e180881SMateusz Guzik 2847cc7ce83aSJeff Roberson #ifdef INVARIANTS 2848cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 1 2849cc7ce83aSJeff Roberson #else 2850cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 0 2851cc7ce83aSJeff Roberson #endif 2852cc7ce83aSJeff Roberson 2853beb8beefSJeff Roberson static void * 2854cc7ce83aSJeff Roberson item_ctor(uma_zone_t zone, int size, void *udata, int flags, void *item) 2855beb8beefSJeff Roberson { 2856beb8beefSJeff Roberson #ifdef INVARIANTS 2857ca293436SRyan Libby bool skipdbg; 2858beb8beefSJeff Roberson 2859beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 2860ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2861ca293436SRyan Libby zone->uz_ctor != trash_ctor) 2862cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 2863beb8beefSJeff Roberson #endif 2864ca293436SRyan Libby if (__predict_false(zone->uz_ctor != NULL) && 2865cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 2866beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 2867beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 2868beb8beefSJeff Roberson return (NULL); 2869beb8beefSJeff Roberson } 2870beb8beefSJeff Roberson #ifdef INVARIANTS 2871beb8beefSJeff Roberson if (!skipdbg) 2872beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 2873beb8beefSJeff Roberson #endif 2874beb8beefSJeff Roberson if (flags & M_ZERO) 2875cc7ce83aSJeff Roberson bzero(item, size); 2876beb8beefSJeff Roberson 2877beb8beefSJeff Roberson return (item); 2878beb8beefSJeff Roberson } 2879beb8beefSJeff Roberson 2880ca293436SRyan Libby static inline void 2881cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 2882cc7ce83aSJeff Roberson enum zfreeskip skip) 2883ca293436SRyan Libby { 2884ca293436SRyan Libby #ifdef INVARIANTS 2885ca293436SRyan Libby bool skipdbg; 2886ca293436SRyan Libby 2887ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 2888ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 2889ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 2890ca293436SRyan Libby uma_dbg_free(zone, udata, item); 2891ca293436SRyan Libby else 2892ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 2893ca293436SRyan Libby } 2894ca293436SRyan Libby #endif 2895cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 2896ca293436SRyan Libby if (zone->uz_dtor != NULL) 2897cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 2898ca293436SRyan Libby #ifdef INVARIANTS 2899ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2900ca293436SRyan Libby zone->uz_dtor != trash_dtor) 2901cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 2902ca293436SRyan Libby #endif 2903ca293436SRyan Libby } 2904ca293436SRyan Libby } 2905ca293436SRyan Libby 29069c2cd7e5SJeff Roberson /* See uma.h */ 29078355f576SJeff Roberson void * 29082cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 29098355f576SJeff Roberson { 2910376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 2911ab3185d1SJeff Roberson uma_cache_t cache; 2912ab3185d1SJeff Roberson void *item; 2913cc7ce83aSJeff Roberson int domain, size, uz_flags; 29148355f576SJeff Roberson 2915e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 291619fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 291710cb2424SMark Murray 29188355f576SJeff Roberson /* This is the fast path allocation */ 29191431a748SGleb Smirnoff CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d", 29201431a748SGleb Smirnoff curthread, zone->uz_name, zone, flags); 2921a553d4b8SJeff Roberson 2922cc7ce83aSJeff Roberson #ifdef WITNESS 2923635fd505SRobert Watson if (flags & M_WAITOK) { 2924b23f72e9SBrian Feldman WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2925635fd505SRobert Watson "uma_zalloc_arg: zone \"%s\"", zone->uz_name); 29264c1cc01cSJohn Baldwin } 2927cc7ce83aSJeff Roberson #endif 2928cc7ce83aSJeff Roberson 2929cc7ce83aSJeff Roberson #ifdef INVARIANTS 29300766f278SJonathan T. Looney KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC")); 2931d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 29321067a2baSJonathan T. Looney ("uma_zalloc_arg: called with spinlock or critical section held")); 2933ea99223eSMateusz Guzik if (zone->uz_flags & UMA_ZONE_PCPU) 2934b8af2820SMateusz Guzik KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone " 2935b8af2820SMateusz Guzik "with M_ZERO passed")); 2936cc7ce83aSJeff Roberson #endif 29371067a2baSJonathan T. Looney 29388d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 29398d689e04SGleb Smirnoff if (memguard_cmp_zone(zone)) { 29408d689e04SGleb Smirnoff item = memguard_alloc(zone->uz_size, flags); 29418d689e04SGleb Smirnoff if (item != NULL) { 29428d689e04SGleb Smirnoff if (zone->uz_init != NULL && 29438d689e04SGleb Smirnoff zone->uz_init(item, zone->uz_size, flags) != 0) 29448d689e04SGleb Smirnoff return (NULL); 29458d689e04SGleb Smirnoff if (zone->uz_ctor != NULL && 2946fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 2947fc03d22bSJeff Roberson flags) != 0) { 2948ca293436SRyan Libby counter_u64_add(zone->uz_fails, 1); 29498d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 29508d689e04SGleb Smirnoff return (NULL); 29518d689e04SGleb Smirnoff } 29528d689e04SGleb Smirnoff return (item); 29538d689e04SGleb Smirnoff } 29548d689e04SGleb Smirnoff /* This is unfortunate but should not be fatal. */ 29558d689e04SGleb Smirnoff } 29568d689e04SGleb Smirnoff #endif 29575d1ae027SRobert Watson /* 29585d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 29595d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 29605d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 29615d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 29625d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 29635d1ae027SRobert Watson * preemption and migration. We release the critical section in 29645d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 29655d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 29665d1ae027SRobert Watson * must detect and handle migration if it has occurred. 29675d1ae027SRobert Watson */ 29685d1ae027SRobert Watson critical_enter(); 2969beb8beefSJeff Roberson do { 2970cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 2971376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 2972cc7ce83aSJeff Roberson size = cache_uz_size(cache); 2973cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 2974376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 2975376b1ba3SJeff Roberson item = cache_bucket_pop(cache, bucket); 29765d1ae027SRobert Watson critical_exit(); 2977cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 2978cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 2979cc7ce83aSJeff Roberson return (item_ctor(zone, size, udata, flags, item)); 2980cc7ce83aSJeff Roberson if (flags & M_ZERO) 2981cc7ce83aSJeff Roberson bzero(item, size); 2982cc7ce83aSJeff Roberson return (item); 2983b23f72e9SBrian Feldman } 2984beb8beefSJeff Roberson } while (cache_alloc(zone, cache, udata, flags)); 2985beb8beefSJeff Roberson critical_exit(); 2986beb8beefSJeff Roberson 2987beb8beefSJeff Roberson /* 2988beb8beefSJeff Roberson * We can not get a bucket so try to return a single item. 2989beb8beefSJeff Roberson */ 2990dfe13344SJeff Roberson if (uz_flags & UMA_ZONE_FIRSTTOUCH) 2991beb8beefSJeff Roberson domain = PCPU_GET(domain); 2992beb8beefSJeff Roberson else 2993beb8beefSJeff Roberson domain = UMA_ANYDOMAIN; 29944bd61e19SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 2995fc03d22bSJeff Roberson } 2996fc03d22bSJeff Roberson 29978355f576SJeff Roberson /* 2998beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 2999beb8beefSJeff Roberson * a critical section. Returns in a critical section. 3000beb8beefSJeff Roberson * 30014bd61e19SJeff Roberson * A false return value indicates an allocation failure. 30024bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 3003beb8beefSJeff Roberson */ 3004beb8beefSJeff Roberson static __noinline bool 3005beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3006beb8beefSJeff Roberson { 3007beb8beefSJeff Roberson uma_zone_domain_t zdom; 3008beb8beefSJeff Roberson uma_bucket_t bucket; 3009cc7ce83aSJeff Roberson int domain; 3010beb8beefSJeff Roberson bool lockfail; 3011beb8beefSJeff Roberson 3012beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3013beb8beefSJeff Roberson 3014beb8beefSJeff Roberson /* 3015beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3016beb8beefSJeff Roberson * if we can switch with the free bucket. 30178355f576SJeff Roberson */ 3018376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_cnt != 0) { 3019376b1ba3SJeff Roberson cache_bucket_swap(&cache->uc_freebucket, &cache->uc_allocbucket); 3020beb8beefSJeff Roberson return (true); 30218355f576SJeff Roberson } 3022fc03d22bSJeff Roberson 3023fc03d22bSJeff Roberson /* 3024fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3025fc03d22bSJeff Roberson */ 3026376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3027fc03d22bSJeff Roberson critical_exit(); 3028fc03d22bSJeff Roberson if (bucket != NULL) 30296fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3030fc03d22bSJeff Roberson 30314bd61e19SJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 30324bd61e19SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) { 30334bd61e19SJeff Roberson critical_enter(); 30344bd61e19SJeff Roberson return (false); 30354bd61e19SJeff Roberson } 30364bd61e19SJeff Roberson 30375d1ae027SRobert Watson /* 30385d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 30395d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 30405d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 30415d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 30425d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 30435d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 30445d1ae027SRobert Watson * the critical section. 30455d1ae027SRobert Watson */ 3046fc03d22bSJeff Roberson lockfail = 0; 3047fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 3048fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 3049a553d4b8SJeff Roberson ZONE_LOCK(zone); 3050fc03d22bSJeff Roberson lockfail = 1; 3051fc03d22bSJeff Roberson } 3052beb8beefSJeff Roberson 3053fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 30544bd61e19SJeff Roberson critical_enter(); 30554bd61e19SJeff Roberson cache = &zone->uz_cpu[curcpu]; 3056376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket != NULL) { 3057fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 3058beb8beefSJeff Roberson return (true); 3059a553d4b8SJeff Roberson } 30608355f576SJeff Roberson 3061fc03d22bSJeff Roberson /* 3062fc03d22bSJeff Roberson * Check the zone's cache of buckets. 3063fc03d22bSJeff Roberson */ 3064dfe13344SJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) { 3065c1685086SJeff Roberson domain = PCPU_GET(domain); 3066ab3185d1SJeff Roberson zdom = &zone->uz_domain[domain]; 3067c1685086SJeff Roberson } else { 3068c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3069c1685086SJeff Roberson zdom = &zone->uz_domain[0]; 3070c1685086SJeff Roberson } 3071c1685086SJeff Roberson 307208cfa56eSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom)) != NULL) { 3073beb8beefSJeff Roberson ZONE_UNLOCK(zone); 3074cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 3075a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 3076376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3077beb8beefSJeff Roberson return (true); 3078a553d4b8SJeff Roberson } 30795d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 30805d1ae027SRobert Watson critical_exit(); 3081bbee39c6SJeff Roberson 3082fc03d22bSJeff Roberson /* 3083fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 3084fc03d22bSJeff Roberson * handle the working set. 3085fc03d22bSJeff Roberson */ 308620a4e154SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 308720a4e154SJeff Roberson zone->uz_bucket_size++; 30884bd61e19SJeff Roberson ZONE_UNLOCK(zone); 3089bb15d1c7SGleb Smirnoff 30908355f576SJeff Roberson /* 3091beb8beefSJeff Roberson * Fill a bucket and attempt to use it as the alloc bucket. 3092bbee39c6SJeff Roberson */ 3093beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 30941431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 30951431a748SGleb Smirnoff zone->uz_name, zone, bucket); 30964bd61e19SJeff Roberson if (bucket == NULL) { 3097fc03d22bSJeff Roberson critical_enter(); 3098beb8beefSJeff Roberson return (false); 30994bd61e19SJeff Roberson } 31000f9b7bf3SMark Johnston 3101fc03d22bSJeff Roberson /* 3102fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3103fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3104fc03d22bSJeff Roberson * the memory directly. 3105fc03d22bSJeff Roberson */ 31064bd61e19SJeff Roberson ZONE_LOCK(zone); 31074bd61e19SJeff Roberson critical_enter(); 3108cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3109376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 3110dfe13344SJeff Roberson ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0 || 311181c0d72cSGleb Smirnoff domain == PCPU_GET(domain))) { 3112376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 31130f9b7bf3SMark Johnston zdom->uzd_imax += bucket->ub_cnt; 3114bb15d1c7SGleb Smirnoff } else if (zone->uz_bkt_count >= zone->uz_bkt_max) { 311581c0d72cSGleb Smirnoff critical_exit(); 311681c0d72cSGleb Smirnoff ZONE_UNLOCK(zone); 311781c0d72cSGleb Smirnoff bucket_drain(zone, bucket); 311881c0d72cSGleb Smirnoff bucket_free(zone, bucket, udata); 3119beb8beefSJeff Roberson critical_enter(); 3120beb8beefSJeff Roberson return (true); 312181c0d72cSGleb Smirnoff } else 31220f9b7bf3SMark Johnston zone_put_bucket(zone, zdom, bucket, false); 3123bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 3124beb8beefSJeff Roberson return (true); 3125bbee39c6SJeff Roberson } 3126bbee39c6SJeff Roberson 3127ab3185d1SJeff Roberson void * 3128ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3129bbee39c6SJeff Roberson { 3130ab3185d1SJeff Roberson 3131ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 313219fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3133ab3185d1SJeff Roberson 3134ab3185d1SJeff Roberson /* This is the fast path allocation */ 3135ab3185d1SJeff Roberson CTR5(KTR_UMA, 3136ab3185d1SJeff Roberson "uma_zalloc_domain thread %x zone %s(%p) domain %d flags %d", 3137ab3185d1SJeff Roberson curthread, zone->uz_name, zone, domain, flags); 3138ab3185d1SJeff Roberson 3139ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3140ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3141ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3142ab3185d1SJeff Roberson } 3143ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3144ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 3145ab3185d1SJeff Roberson 3146ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3147ab3185d1SJeff Roberson } 3148ab3185d1SJeff Roberson 3149ab3185d1SJeff Roberson /* 3150ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3151ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3152ab3185d1SJeff Roberson * 3153ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3154ab3185d1SJeff Roberson * only 'domain'. 3155ab3185d1SJeff Roberson */ 3156ab3185d1SJeff Roberson static uma_slab_t 3157194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3158ab3185d1SJeff Roberson { 3159ab3185d1SJeff Roberson uma_domain_t dom; 3160bbee39c6SJeff Roberson uma_slab_t slab; 3161ab3185d1SJeff Roberson int start; 3162ab3185d1SJeff Roberson 3163ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3164ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 31658b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3166ab3185d1SJeff Roberson 3167ab3185d1SJeff Roberson slab = NULL; 3168ab3185d1SJeff Roberson start = domain; 3169ab3185d1SJeff Roberson do { 3170ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 3171ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_part_slab)) 3172ab3185d1SJeff Roberson return (LIST_FIRST(&dom->ud_part_slab)); 3173ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_free_slab)) { 3174ab3185d1SJeff Roberson slab = LIST_FIRST(&dom->ud_free_slab); 3175ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 3176ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3177ab3185d1SJeff Roberson return (slab); 3178ab3185d1SJeff Roberson } 3179ab3185d1SJeff Roberson if (rr) 3180ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3181ab3185d1SJeff Roberson } while (domain != start); 3182ab3185d1SJeff Roberson 3183ab3185d1SJeff Roberson return (NULL); 3184ab3185d1SJeff Roberson } 3185ab3185d1SJeff Roberson 31868b987a77SJeff Roberson /* 31878b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 31888b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 31898b987a77SJeff Roberson */ 3190ab3185d1SJeff Roberson static uma_slab_t 3191194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3192ab3185d1SJeff Roberson { 31938b987a77SJeff Roberson uma_slab_t slab; 3194194a979eSMark Johnston uint32_t reserve; 3195099a0e58SBosko Milekic 31968b987a77SJeff Roberson /* HASH has a single free list. */ 319754c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 31988b987a77SJeff Roberson domain = 0; 3199194a979eSMark Johnston 32008b987a77SJeff Roberson KEG_LOCK(keg, domain); 3201194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 32028b987a77SJeff Roberson if (keg->uk_domain[domain].ud_free <= reserve || 32038b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 32048b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3205194a979eSMark Johnston return (NULL); 32068b987a77SJeff Roberson } 32078b987a77SJeff Roberson return (slab); 3208194a979eSMark Johnston } 3209194a979eSMark Johnston 3210194a979eSMark Johnston static uma_slab_t 3211194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3212194a979eSMark Johnston { 3213194a979eSMark Johnston struct vm_domainset_iter di; 3214194a979eSMark Johnston uma_slab_t slab; 3215194a979eSMark Johnston int aflags, domain; 3216194a979eSMark Johnston bool rr; 3217194a979eSMark Johnston 3218194a979eSMark Johnston restart: 3219bbee39c6SJeff Roberson /* 3220194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3221194a979eSMark Johnston * domain (as happens with first-touch zones). 3222194a979eSMark Johnston * 3223194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3224194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3225194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3226bbee39c6SJeff Roberson */ 3227ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3228ab3185d1SJeff Roberson if (rr) { 3229194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3230194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3231194a979eSMark Johnston &aflags); 3232194a979eSMark Johnston } else { 3233194a979eSMark Johnston aflags = flags; 3234194a979eSMark Johnston domain = rdomain; 3235194a979eSMark Johnston } 3236ab3185d1SJeff Roberson 3237194a979eSMark Johnston for (;;) { 3238194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3239584061b4SJeff Roberson if (slab != NULL) 3240bbee39c6SJeff Roberson return (slab); 3241bbee39c6SJeff Roberson 3242bbee39c6SJeff Roberson /* 3243bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3244bbee39c6SJeff Roberson */ 3245bbee39c6SJeff Roberson if (flags & M_NOVM) 3246bbee39c6SJeff Roberson break; 3247bbee39c6SJeff Roberson 324886220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 32498b987a77SJeff Roberson if (slab != NULL) 3250bbee39c6SJeff Roberson return (slab); 32513639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 32523639ac42SJeff Roberson break; 3253194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3254194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 3255194a979eSMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 3256194a979eSMark Johnston goto restart; 325730c5525bSAndrew Gallatin } 3258194a979eSMark Johnston break; 3259194a979eSMark Johnston } 3260ab3185d1SJeff Roberson } 3261ab3185d1SJeff Roberson 3262bbee39c6SJeff Roberson /* 3263bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3264bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3265bbee39c6SJeff Roberson * fail. 3266bbee39c6SJeff Roberson */ 32678b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 3268bbee39c6SJeff Roberson return (slab); 32698b987a77SJeff Roberson 3270ab3185d1SJeff Roberson return (NULL); 3271ab3185d1SJeff Roberson } 3272bbee39c6SJeff Roberson 3273d56368d7SBosko Milekic static void * 32740095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3275bbee39c6SJeff Roberson { 3276ab3185d1SJeff Roberson uma_domain_t dom; 3277bbee39c6SJeff Roberson void *item; 327885dcf349SGleb Smirnoff uint8_t freei; 3279bbee39c6SJeff Roberson 32808b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3281099a0e58SBosko Milekic 32828b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 32839b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 32849b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 32851e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3286bbee39c6SJeff Roberson slab->us_freecount--; 32878b987a77SJeff Roberson dom->ud_free--; 3288ef72505eSJeff Roberson 3289bbee39c6SJeff Roberson /* Move this slab to the full list */ 3290bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3291bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3292ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3293bbee39c6SJeff Roberson } 3294bbee39c6SJeff Roberson 3295bbee39c6SJeff Roberson return (item); 3296bbee39c6SJeff Roberson } 3297bbee39c6SJeff Roberson 3298bbee39c6SJeff Roberson static int 3299b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 33000095a784SJeff Roberson { 33018b987a77SJeff Roberson uma_domain_t dom; 3302b75c4efcSAndrew Turner uma_zone_t zone; 33030095a784SJeff Roberson uma_slab_t slab; 33040095a784SJeff Roberson uma_keg_t keg; 3305a03af342SSean Bruno #ifdef NUMA 3306ab3185d1SJeff Roberson int stripe; 3307a03af342SSean Bruno #endif 33080095a784SJeff Roberson int i; 33090095a784SJeff Roberson 3310b75c4efcSAndrew Turner zone = arg; 33110095a784SJeff Roberson slab = NULL; 3312584061b4SJeff Roberson keg = zone->uz_keg; 3313af526374SJeff Roberson /* Try to keep the buckets totally full */ 33140095a784SJeff Roberson for (i = 0; i < max; ) { 3315584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 33160095a784SJeff Roberson break; 3317a03af342SSean Bruno #ifdef NUMA 3318ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3319a03af342SSean Bruno #endif 33208b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 33216fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 33220095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 33238b987a77SJeff Roberson if (dom->ud_free <= keg->uk_reserve) 33246fd34d6fSJeff Roberson break; 3325b6715dabSJeff Roberson #ifdef NUMA 3326ab3185d1SJeff Roberson /* 3327ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3328ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3329ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3330ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3331ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3332ab3185d1SJeff Roberson * time but yields better distribution. 3333ab3185d1SJeff Roberson */ 3334dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 && 3335ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3336ab3185d1SJeff Roberson break; 3337ab3185d1SJeff Roberson #endif 33386fd34d6fSJeff Roberson } 33398b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 3340ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 33410095a784SJeff Roberson flags &= ~M_WAITOK; 33420095a784SJeff Roberson flags |= M_NOWAIT; 33430095a784SJeff Roberson } 33440095a784SJeff Roberson 33450095a784SJeff Roberson return i; 33460095a784SJeff Roberson } 33470095a784SJeff Roberson 33484bd61e19SJeff Roberson static int 33494bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 33504bd61e19SJeff Roberson { 33514bd61e19SJeff Roberson uint64_t old, new, total, max; 33524bd61e19SJeff Roberson 33534bd61e19SJeff Roberson /* 33544bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 33554bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 33564bd61e19SJeff Roberson * fairness by keeping fifo order. 33574bd61e19SJeff Roberson * 33584bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 33594bd61e19SJeff Roberson */ 33604bd61e19SJeff Roberson for (;;) { 33614bd61e19SJeff Roberson zone_free_limit(zone, count); 33624bd61e19SJeff Roberson zone_log_warning(zone); 33634bd61e19SJeff Roberson zone_maxaction(zone); 33644bd61e19SJeff Roberson if (flags & M_NOWAIT) 33654bd61e19SJeff Roberson return (0); 33664bd61e19SJeff Roberson 33674bd61e19SJeff Roberson /* 33684bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 33694bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 33704bd61e19SJeff Roberson * is essentially a home rolled semaphore. 33714bd61e19SJeff Roberson */ 33724bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 33734bd61e19SJeff Roberson old = zone->uz_items; 33744bd61e19SJeff Roberson do { 33754bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 33764bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 33774bd61e19SJeff Roberson max = zone->uz_max_items; 33784bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 33794bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 33804bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 33814bd61e19SJeff Roberson else 33824bd61e19SJeff Roberson new = old + MIN(count, max - old); 33834bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 33844bd61e19SJeff Roberson 33854bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 33864bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 33874bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 33884bd61e19SJeff Roberson return (new - old); 33894bd61e19SJeff Roberson } 33904bd61e19SJeff Roberson 33914bd61e19SJeff Roberson /* 33924bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 33934bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 33944bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 33954bd61e19SJeff Roberson * transitions. 33964bd61e19SJeff Roberson */ 33974bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 33984bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 33994bd61e19SJeff Roberson 34004bd61e19SJeff Roberson /* 34014bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 34024bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 34034bd61e19SJeff Roberson */ 34044bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 34054bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 34064bd61e19SJeff Roberson 34074bd61e19SJeff Roberson /* 34084bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 34094bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 34104bd61e19SJeff Roberson * 34114bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 34124bd61e19SJeff Roberson * our count. 34134bd61e19SJeff Roberson */ 34144bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 34154bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 34164bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 34174bd61e19SJeff Roberson /* We're no longer a sleeper. */ 34184bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 34194bd61e19SJeff Roberson 34204bd61e19SJeff Roberson /* 34214bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 34224bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 34234bd61e19SJeff Roberson * against changes via sysctl. 34244bd61e19SJeff Roberson */ 34254bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 34264bd61e19SJeff Roberson max = zone->uz_max_items; 34274bd61e19SJeff Roberson if (total >= max) 34284bd61e19SJeff Roberson continue; 34294bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 34304bd61e19SJeff Roberson if (total + count > max) { 34314bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 34324bd61e19SJeff Roberson count = max - total; 34334bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 34344bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 34354bd61e19SJeff Roberson 34364bd61e19SJeff Roberson return (count); 34374bd61e19SJeff Roberson } 34384bd61e19SJeff Roberson } 34394bd61e19SJeff Roberson 34404bd61e19SJeff Roberson /* 34414bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 34424bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 34434bd61e19SJeff Roberson * one item can be allocated. 34444bd61e19SJeff Roberson */ 34454bd61e19SJeff Roberson static int 34464bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 34474bd61e19SJeff Roberson { 34484bd61e19SJeff Roberson uint64_t old; 34494bd61e19SJeff Roberson uint64_t max; 34504bd61e19SJeff Roberson 34514bd61e19SJeff Roberson max = zone->uz_max_items; 34524bd61e19SJeff Roberson MPASS(max > 0); 34534bd61e19SJeff Roberson 34544bd61e19SJeff Roberson /* 34554bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 34564bd61e19SJeff Roberson * fetchadd. 34574bd61e19SJeff Roberson */ 34584bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 34594bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 34604bd61e19SJeff Roberson return (count); 34614bd61e19SJeff Roberson 34624bd61e19SJeff Roberson /* 34634bd61e19SJeff Roberson * If we had some items and no sleepers just return the 34644bd61e19SJeff Roberson * truncated value. We have to release the excess space 34654bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 34664bd61e19SJeff Roberson * because we were temporarily over the limit. 34674bd61e19SJeff Roberson */ 34684bd61e19SJeff Roberson if (old < max) { 34694bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 34704bd61e19SJeff Roberson return (max - old); 34714bd61e19SJeff Roberson } 34724bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 34734bd61e19SJeff Roberson } 34744bd61e19SJeff Roberson 34754bd61e19SJeff Roberson /* 34764bd61e19SJeff Roberson * Free a number of items back to the limit. 34774bd61e19SJeff Roberson */ 34784bd61e19SJeff Roberson static void 34794bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 34804bd61e19SJeff Roberson { 34814bd61e19SJeff Roberson uint64_t old; 34824bd61e19SJeff Roberson 34834bd61e19SJeff Roberson MPASS(count > 0); 34844bd61e19SJeff Roberson 34854bd61e19SJeff Roberson /* 34864bd61e19SJeff Roberson * In the common case we either have no sleepers or 34874bd61e19SJeff Roberson * are still over the limit and can just return. 34884bd61e19SJeff Roberson */ 34894bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 34904bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 34914bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 34924bd61e19SJeff Roberson return; 34934bd61e19SJeff Roberson 34944bd61e19SJeff Roberson /* 34954bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 34964bd61e19SJeff Roberson * to generate wakeups if necessary. 34974bd61e19SJeff Roberson */ 34984bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 34994bd61e19SJeff Roberson } 35004bd61e19SJeff Roberson 3501fc03d22bSJeff Roberson static uma_bucket_t 3502beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3503bbee39c6SJeff Roberson { 3504bbee39c6SJeff Roberson uma_bucket_t bucket; 3505beb8beefSJeff Roberson int maxbucket, cnt; 3506bbee39c6SJeff Roberson 350730c5525bSAndrew Gallatin CTR1(KTR_UMA, "zone_alloc:_bucket domain %d)", domain); 350830c5525bSAndrew Gallatin 3509c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3510c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3511c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3512c1685086SJeff Roberson 35134bd61e19SJeff Roberson if (zone->uz_max_items > 0) 35144bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 35154bd61e19SJeff Roberson M_NOWAIT); 35164bd61e19SJeff Roberson else 351720a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 35184bd61e19SJeff Roberson if (maxbucket == 0) 35194bd61e19SJeff Roberson return (false); 3520beb8beefSJeff Roberson 35216fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 35226fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3523beb8beefSJeff Roberson if (bucket == NULL) { 3524beb8beefSJeff Roberson cnt = 0; 3525beb8beefSJeff Roberson goto out; 3526beb8beefSJeff Roberson } 35270095a784SJeff Roberson 35280095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3529beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 35300095a784SJeff Roberson 35310095a784SJeff Roberson /* 35320095a784SJeff Roberson * Initialize the memory if necessary. 35330095a784SJeff Roberson */ 35340095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 3535099a0e58SBosko Milekic int i; 3536bbee39c6SJeff Roberson 35370095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 3538e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 35390095a784SJeff Roberson flags) != 0) 3540b23f72e9SBrian Feldman break; 3541b23f72e9SBrian Feldman /* 3542b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 3543b23f72e9SBrian Feldman * rest back onto the freelist. 3544b23f72e9SBrian Feldman */ 3545b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 3546af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 35470095a784SJeff Roberson bucket->ub_cnt - i); 3548a5a262c6SBosko Milekic #ifdef INVARIANTS 35490095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 35500095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 3551a5a262c6SBosko Milekic #endif 3552b23f72e9SBrian Feldman bucket->ub_cnt = i; 3553b23f72e9SBrian Feldman } 3554099a0e58SBosko Milekic } 3555099a0e58SBosko Milekic 3556beb8beefSJeff Roberson cnt = bucket->ub_cnt; 3557f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 35586fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 35592efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 3560beb8beefSJeff Roberson bucket = NULL; 3561beb8beefSJeff Roberson } 3562beb8beefSJeff Roberson out: 35634bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 35644bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 3565fc03d22bSJeff Roberson 3566fc03d22bSJeff Roberson return (bucket); 3567fc03d22bSJeff Roberson } 3568fc03d22bSJeff Roberson 35698355f576SJeff Roberson /* 35700095a784SJeff Roberson * Allocates a single item from a zone. 35718355f576SJeff Roberson * 35728355f576SJeff Roberson * Arguments 35738355f576SJeff Roberson * zone The zone to alloc for. 35748355f576SJeff Roberson * udata The data to be passed to the constructor. 3575ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 3576a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 35778355f576SJeff Roberson * 35788355f576SJeff Roberson * Returns 35798355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 3580bbee39c6SJeff Roberson * An item if successful 35818355f576SJeff Roberson */ 35828355f576SJeff Roberson 35838355f576SJeff Roberson static void * 3584ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 35858355f576SJeff Roberson { 35868355f576SJeff Roberson void *item; 35878355f576SJeff Roberson 35884bd61e19SJeff Roberson if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) 3589bb15d1c7SGleb Smirnoff return (NULL); 35908355f576SJeff Roberson 3591c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3592c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 359330c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 3594c1685086SJeff Roberson 3595ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 3596beb8beefSJeff Roberson goto fail_cnt; 35978355f576SJeff Roberson 3598099a0e58SBosko Milekic /* 3599099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 3600099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 3601099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 3602099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 3603099a0e58SBosko Milekic */ 3604b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 3605e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 3606bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 3607beb8beefSJeff Roberson goto fail_cnt; 3608beb8beefSJeff Roberson } 3609beb8beefSJeff Roberson } 3610cc7ce83aSJeff Roberson item = item_ctor(zone, zone->uz_size, udata, flags, item); 3611beb8beefSJeff Roberson if (item == NULL) 36120095a784SJeff Roberson goto fail; 36138355f576SJeff Roberson 36142efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 36151431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 36161431a748SGleb Smirnoff zone->uz_name, zone); 36171431a748SGleb Smirnoff 36188355f576SJeff Roberson return (item); 36190095a784SJeff Roberson 3620beb8beefSJeff Roberson fail_cnt: 3621beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 36220095a784SJeff Roberson fail: 36234bd61e19SJeff Roberson if (zone->uz_max_items > 0) 36244bd61e19SJeff Roberson zone_free_limit(zone, 1); 36251431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 36261431a748SGleb Smirnoff zone->uz_name, zone); 36274bd61e19SJeff Roberson 36280095a784SJeff Roberson return (NULL); 36298355f576SJeff Roberson } 36308355f576SJeff Roberson 36318355f576SJeff Roberson /* See uma.h */ 36328355f576SJeff Roberson void 36338355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 36348355f576SJeff Roberson { 36358355f576SJeff Roberson uma_cache_t cache; 3636376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3637cc7ce83aSJeff Roberson int domain, itemdomain, uz_flags; 36388355f576SJeff Roberson 3639e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 364019fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 364110cb2424SMark Murray 36423659f747SRobert Watson CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread, 36433659f747SRobert Watson zone->uz_name); 36443659f747SRobert Watson 3645d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 36461067a2baSJonathan T. Looney ("uma_zfree_arg: called with spinlock or critical section held")); 36471067a2baSJonathan T. Looney 364820ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 364920ed0cb0SMatthew D Fleming if (item == NULL) 365020ed0cb0SMatthew D Fleming return; 36518d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 36528d689e04SGleb Smirnoff if (is_memguard_addr(item)) { 3653bc9d08e1SMark Johnston if (zone->uz_dtor != NULL) 36548d689e04SGleb Smirnoff zone->uz_dtor(item, zone->uz_size, udata); 3655bc9d08e1SMark Johnston if (zone->uz_fini != NULL) 36568d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 36578d689e04SGleb Smirnoff memguard_free(item); 36588d689e04SGleb Smirnoff return; 36598d689e04SGleb Smirnoff } 36608d689e04SGleb Smirnoff #endif 3661cc7ce83aSJeff Roberson 3662cc7ce83aSJeff Roberson /* 3663cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 3664cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 3665cc7ce83aSJeff Roberson * will simply read another cpu's line. 3666cc7ce83aSJeff Roberson */ 3667cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3668cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3669cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 3670cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 3671cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 3672ef72505eSJeff Roberson 3673af7f9b97SJeff Roberson /* 3674af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 3675af7f9b97SJeff Roberson * a little longer for the limits to be reset. 3676af7f9b97SJeff Roberson */ 3677cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 3678bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0) 3679fc03d22bSJeff Roberson goto zfree_item; 3680cc7ce83aSJeff Roberson } 3681af7f9b97SJeff Roberson 36825d1ae027SRobert Watson /* 36835d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 36845d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 36855d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 36865d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 36875d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 36885d1ae027SRobert Watson * preemption and migration. We release the critical section in 36895d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 36905d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 36915d1ae027SRobert Watson * detect and handle migration if it has occurred. 36925d1ae027SRobert Watson */ 36930a81b439SJeff Roberson domain = itemdomain = 0; 3694dfe13344SJeff Roberson #ifdef NUMA 3695dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 3696dfe13344SJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 3697dfe13344SJeff Roberson #endif 36985d1ae027SRobert Watson critical_enter(); 36990a81b439SJeff Roberson do { 3700cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3701dfe13344SJeff Roberson #ifdef NUMA 37020a81b439SJeff Roberson domain = PCPU_GET(domain); 3703dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 3704dfe13344SJeff Roberson domain != itemdomain) { 3705376b1ba3SJeff Roberson bucket = &cache->uc_crossbucket; 37060a81b439SJeff Roberson } else 3707c1685086SJeff Roberson #endif 3708dfe13344SJeff Roberson { 3709a553d4b8SJeff Roberson /* 3710dfe13344SJeff Roberson * Try to free into the allocbucket first to give LIFO 3711dfe13344SJeff Roberson * ordering for cache-hot datastructures. Spill over 3712dfe13344SJeff Roberson * into the freebucket if necessary. Alloc will swap 3713dfe13344SJeff Roberson * them if one runs dry. 3714a553d4b8SJeff Roberson */ 3715dfe13344SJeff Roberson bucket = &cache->uc_allocbucket; 3716dfe13344SJeff Roberson if (__predict_false(bucket->ucb_cnt >= 3717dfe13344SJeff Roberson bucket->ucb_entries)) 3718376b1ba3SJeff Roberson bucket = &cache->uc_freebucket; 3719dfe13344SJeff Roberson } 3720376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3721376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 37225d1ae027SRobert Watson critical_exit(); 37238355f576SJeff Roberson return; 3724fc03d22bSJeff Roberson } 37250a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 37260a81b439SJeff Roberson critical_exit(); 3727fc03d22bSJeff Roberson 37288355f576SJeff Roberson /* 37290a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 37308355f576SJeff Roberson */ 37310a81b439SJeff Roberson zfree_item: 37320a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 37330a81b439SJeff Roberson } 3734fc03d22bSJeff Roberson 3735dfe13344SJeff Roberson #ifdef NUMA 373691d947bfSJeff Roberson /* 373791d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 373891d947bfSJeff Roberson * them. 373991d947bfSJeff Roberson */ 374091d947bfSJeff Roberson static void 374191d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 374291d947bfSJeff Roberson { 374391d947bfSJeff Roberson struct uma_bucketlist fullbuckets; 374491d947bfSJeff Roberson uma_zone_domain_t zdom; 374591d947bfSJeff Roberson uma_bucket_t b; 374691d947bfSJeff Roberson void *item; 374791d947bfSJeff Roberson int domain; 374891d947bfSJeff Roberson 374991d947bfSJeff Roberson CTR3(KTR_UMA, 375091d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 375191d947bfSJeff Roberson zone->uz_name, zone, bucket); 375291d947bfSJeff Roberson 375391d947bfSJeff Roberson TAILQ_INIT(&fullbuckets); 375491d947bfSJeff Roberson 375591d947bfSJeff Roberson /* 375691d947bfSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 375791d947bfSJeff Roberson * lock on the current crossfree bucket. A full matrix with 375891d947bfSJeff Roberson * per-domain locking could be used if necessary. 375991d947bfSJeff Roberson */ 376091d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 376191d947bfSJeff Roberson while (bucket->ub_cnt > 0) { 376291d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 376391d947bfSJeff Roberson domain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 376491d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 376591d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 376691d947bfSJeff Roberson zdom->uzd_cross = bucket_alloc(zone, udata, M_NOWAIT); 376791d947bfSJeff Roberson if (zdom->uzd_cross == NULL) 376891d947bfSJeff Roberson break; 376991d947bfSJeff Roberson } 377091d947bfSJeff Roberson zdom->uzd_cross->ub_bucket[zdom->uzd_cross->ub_cnt++] = item; 377191d947bfSJeff Roberson if (zdom->uzd_cross->ub_cnt == zdom->uzd_cross->ub_entries) { 377291d947bfSJeff Roberson TAILQ_INSERT_HEAD(&fullbuckets, zdom->uzd_cross, 377391d947bfSJeff Roberson ub_link); 377491d947bfSJeff Roberson zdom->uzd_cross = NULL; 377591d947bfSJeff Roberson } 377691d947bfSJeff Roberson bucket->ub_cnt--; 377791d947bfSJeff Roberson } 377891d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 377991d947bfSJeff Roberson if (!TAILQ_EMPTY(&fullbuckets)) { 378091d947bfSJeff Roberson ZONE_LOCK(zone); 378191d947bfSJeff Roberson while ((b = TAILQ_FIRST(&fullbuckets)) != NULL) { 378291d947bfSJeff Roberson TAILQ_REMOVE(&fullbuckets, b, ub_link); 378391d947bfSJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 378491d947bfSJeff Roberson ZONE_UNLOCK(zone); 378591d947bfSJeff Roberson bucket_drain(zone, b); 378691d947bfSJeff Roberson bucket_free(zone, b, udata); 378791d947bfSJeff Roberson ZONE_LOCK(zone); 378891d947bfSJeff Roberson } else { 378991d947bfSJeff Roberson domain = _vm_phys_domain( 379091d947bfSJeff Roberson pmap_kextract( 379191d947bfSJeff Roberson (vm_offset_t)b->ub_bucket[0])); 379291d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 379391d947bfSJeff Roberson zone_put_bucket(zone, zdom, b, true); 379491d947bfSJeff Roberson } 379591d947bfSJeff Roberson } 379691d947bfSJeff Roberson ZONE_UNLOCK(zone); 379791d947bfSJeff Roberson } 379891d947bfSJeff Roberson if (bucket->ub_cnt != 0) 379991d947bfSJeff Roberson bucket_drain(zone, bucket); 380091d947bfSJeff Roberson bucket_free(zone, bucket, udata); 380191d947bfSJeff Roberson } 380291d947bfSJeff Roberson #endif 380391d947bfSJeff Roberson 38040a81b439SJeff Roberson static void 38050a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 38060a81b439SJeff Roberson int domain, int itemdomain) 38070a81b439SJeff Roberson { 38080a81b439SJeff Roberson uma_zone_domain_t zdom; 38090a81b439SJeff Roberson 3810dfe13344SJeff Roberson #ifdef NUMA 38110a81b439SJeff Roberson /* 38120a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 38130a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 38140a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 381591d947bfSJeff Roberson * correct domains. 38160a81b439SJeff Roberson */ 38170a81b439SJeff Roberson if (domain != itemdomain && vm_ndomains > 2) { 381891d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 38190a81b439SJeff Roberson return; 38200a81b439SJeff Roberson } 38210a81b439SJeff Roberson #endif 382291d947bfSJeff Roberson 38230a81b439SJeff Roberson /* 38240a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 38250a81b439SJeff Roberson * 38260a81b439SJeff Roberson * We bump the uz count when the cache size is insufficient to 38270a81b439SJeff Roberson * handle the working set. 38280a81b439SJeff Roberson */ 38294d104ba0SAlexander Motin if (ZONE_TRYLOCK(zone) == 0) { 38304d104ba0SAlexander Motin /* Record contention to size the buckets. */ 38318355f576SJeff Roberson ZONE_LOCK(zone); 383220a4e154SJeff Roberson if (zone->uz_bucket_size < zone->uz_bucket_size_max) 383320a4e154SJeff Roberson zone->uz_bucket_size++; 38344d104ba0SAlexander Motin } 38358355f576SJeff Roberson 38360a81b439SJeff Roberson CTR3(KTR_UMA, 38370a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 38380a81b439SJeff Roberson zone->uz_name, zone, bucket); 38390a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 38400a81b439SJeff Roberson KASSERT(bucket->ub_cnt == bucket->ub_entries, 38410a81b439SJeff Roberson ("uma_zfree: Attempting to insert partial bucket onto the full list.\n")); 38420a81b439SJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 3843c1685086SJeff Roberson ZONE_UNLOCK(zone); 3844c1685086SJeff Roberson bucket_drain(zone, bucket); 3845c1685086SJeff Roberson bucket_free(zone, bucket, udata); 3846c1685086SJeff Roberson } else { 3847c1685086SJeff Roberson zdom = &zone->uz_domain[itemdomain]; 3848c1685086SJeff Roberson zone_put_bucket(zone, zdom, bucket, true); 3849c1685086SJeff Roberson ZONE_UNLOCK(zone); 3850c1685086SJeff Roberson } 38518355f576SJeff Roberson } 3852fc03d22bSJeff Roberson 38534d104ba0SAlexander Motin /* 38540a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 38550a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 38560a81b439SJeff Roberson * 38570a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 38580a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 38590a81b439SJeff Roberson * the caller should retry. 38604d104ba0SAlexander Motin */ 38610a81b439SJeff Roberson static __noinline bool 38620a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 38630a81b439SJeff Roberson int itemdomain) 38640a81b439SJeff Roberson { 3865dfe13344SJeff Roberson uma_cache_bucket_t cbucket; 38660a81b439SJeff Roberson uma_bucket_t bucket; 3867cc7ce83aSJeff Roberson int domain; 38680a81b439SJeff Roberson 38690a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 38700a81b439SJeff Roberson 387120a4e154SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) 38720a81b439SJeff Roberson return false; 38730a81b439SJeff Roberson 3874cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 38750a81b439SJeff Roberson 38760a81b439SJeff Roberson /* 3877dfe13344SJeff Roberson * FIRSTTOUCH domains need to free to the correct zdom. When 3878dfe13344SJeff Roberson * enabled this is the zdom of the item. The bucket is the 3879dfe13344SJeff Roberson * cross bucket if the current domain and itemdomain do not match. 38800a81b439SJeff Roberson */ 3881dfe13344SJeff Roberson cbucket = &cache->uc_freebucket; 3882dfe13344SJeff Roberson #ifdef NUMA 3883dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 38840a81b439SJeff Roberson domain = PCPU_GET(domain); 38850a81b439SJeff Roberson if (domain != itemdomain) { 3886dfe13344SJeff Roberson cbucket = &cache->uc_crossbucket; 3887dfe13344SJeff Roberson if (cbucket->ucb_cnt != 0) 3888dfe13344SJeff Roberson atomic_add_64(&zone->uz_xdomain, 3889dfe13344SJeff Roberson cbucket->ucb_cnt); 3890dfe13344SJeff Roberson } 38910a81b439SJeff Roberson } else 38920a81b439SJeff Roberson #endif 3893dfe13344SJeff Roberson itemdomain = domain = 0; 3894dfe13344SJeff Roberson bucket = cache_bucket_unload(cbucket); 38950a81b439SJeff Roberson 38960a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 38970a81b439SJeff Roberson critical_exit(); 38980a81b439SJeff Roberson 38990a81b439SJeff Roberson if (bucket != NULL) 39000a81b439SJeff Roberson zone_free_bucket(zone, bucket, udata, domain, itemdomain); 3901a553d4b8SJeff Roberson 39026fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT); 39031431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p", 39041431a748SGleb Smirnoff zone->uz_name, zone, bucket); 3905fc03d22bSJeff Roberson critical_enter(); 39060a81b439SJeff Roberson if (bucket == NULL) 39070a81b439SJeff Roberson return (false); 3908cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3909dfe13344SJeff Roberson #ifdef NUMA 3910fc03d22bSJeff Roberson /* 39110a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 39120a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 39130a81b439SJeff Roberson * the free bucket. 3914fc03d22bSJeff Roberson */ 3915dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 39160a81b439SJeff Roberson domain = PCPU_GET(domain); 3917376b1ba3SJeff Roberson if (domain != itemdomain && 3918376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 3919376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 39200a81b439SJeff Roberson return (true); 39210a81b439SJeff Roberson } 39220a81b439SJeff Roberson } 39230a81b439SJeff Roberson #endif 39240a81b439SJeff Roberson /* 39250a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 39260a81b439SJeff Roberson */ 3927376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 3928fc03d22bSJeff Roberson critical_exit(); 39296fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 39300a81b439SJeff Roberson critical_enter(); 39310a81b439SJeff Roberson } else 3932376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 39338355f576SJeff Roberson 39340a81b439SJeff Roberson return (true); 39358355f576SJeff Roberson } 39368355f576SJeff Roberson 3937ab3185d1SJeff Roberson void 3938ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata) 3939ab3185d1SJeff Roberson { 3940ab3185d1SJeff Roberson 3941ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 394219fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3943ab3185d1SJeff Roberson 3944ab3185d1SJeff Roberson CTR2(KTR_UMA, "uma_zfree_domain thread %x zone %s", curthread, 3945ab3185d1SJeff Roberson zone->uz_name); 3946ab3185d1SJeff Roberson 3947ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3948ab3185d1SJeff Roberson ("uma_zfree_domain: called with spinlock or critical section held")); 3949ab3185d1SJeff Roberson 3950ab3185d1SJeff Roberson /* uma_zfree(..., NULL) does nothing, to match free(9). */ 3951ab3185d1SJeff Roberson if (item == NULL) 3952ab3185d1SJeff Roberson return; 3953ab3185d1SJeff Roberson zone_free_item(zone, item, udata, SKIP_NONE); 3954ab3185d1SJeff Roberson } 3955ab3185d1SJeff Roberson 39568355f576SJeff Roberson static void 3957bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 39588355f576SJeff Roberson { 3959bb15d1c7SGleb Smirnoff uma_keg_t keg; 3960ab3185d1SJeff Roberson uma_domain_t dom; 396185dcf349SGleb Smirnoff uint8_t freei; 3962099a0e58SBosko Milekic 3963bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 39648b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3965ab3185d1SJeff Roberson 39668355f576SJeff Roberson /* Do we need to remove from any lists? */ 39678b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3968099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 39698355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3970ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 39718355f576SJeff Roberson } else if (slab->us_freecount == 0) { 39728355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3973ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 39748355f576SJeff Roberson } 39758355f576SJeff Roberson 3976ef72505eSJeff Roberson /* Slab management. */ 39771e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 39789b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 39798355f576SJeff Roberson slab->us_freecount++; 39808355f576SJeff Roberson 3981ef72505eSJeff Roberson /* Keg statistics. */ 39828b987a77SJeff Roberson dom->ud_free++; 39830095a784SJeff Roberson } 39840095a784SJeff Roberson 39850095a784SJeff Roberson static void 3986b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 39870095a784SJeff Roberson { 39888b987a77SJeff Roberson struct mtx *lock; 3989b75c4efcSAndrew Turner uma_zone_t zone; 39900095a784SJeff Roberson uma_slab_t slab; 39910095a784SJeff Roberson uma_keg_t keg; 39920095a784SJeff Roberson uint8_t *mem; 39938b987a77SJeff Roberson void *item; 39940095a784SJeff Roberson int i; 39958355f576SJeff Roberson 3996b75c4efcSAndrew Turner zone = arg; 3997bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 39988b987a77SJeff Roberson lock = NULL; 399954c5ae80SRyan Libby if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0)) 40008b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 40010095a784SJeff Roberson for (i = 0; i < cnt; i++) { 40020095a784SJeff Roberson item = bucket[i]; 400354c5ae80SRyan Libby if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) { 40040095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 40058b987a77SJeff Roberson } else { 40068b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 400754c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0) 40088b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 40098b987a77SJeff Roberson else 40108b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 40118b987a77SJeff Roberson } 40128b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 40138b987a77SJeff Roberson if (lock != NULL) 40148b987a77SJeff Roberson mtx_unlock(lock); 40158b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 40168b987a77SJeff Roberson } 4017bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 40180095a784SJeff Roberson } 40198b987a77SJeff Roberson if (lock != NULL) 40208b987a77SJeff Roberson mtx_unlock(lock); 40218355f576SJeff Roberson } 40228355f576SJeff Roberson 40230095a784SJeff Roberson /* 40240095a784SJeff Roberson * Frees a single item to any zone. 40250095a784SJeff Roberson * 40260095a784SJeff Roberson * Arguments: 40270095a784SJeff Roberson * zone The zone to free to 40280095a784SJeff Roberson * item The item we're freeing 40290095a784SJeff Roberson * udata User supplied data for the dtor 40300095a784SJeff Roberson * skip Skip dtors and finis 40310095a784SJeff Roberson */ 40320095a784SJeff Roberson static void 40330095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 40340095a784SJeff Roberson { 4035c5deaf04SGleb Smirnoff 4036cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 40370095a784SJeff Roberson 40380095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 40390095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 40400095a784SJeff Roberson 40410095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4042bb15d1c7SGleb Smirnoff 4043bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4044bb15d1c7SGleb Smirnoff return; 4045bb15d1c7SGleb Smirnoff 40462efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 40472efcc8cbSGleb Smirnoff 40484bd61e19SJeff Roberson if (zone->uz_max_items > 0) 40494bd61e19SJeff Roberson zone_free_limit(zone, 1); 4050bb45b411SGleb Smirnoff } 40510095a784SJeff Roberson 40528355f576SJeff Roberson /* See uma.h */ 40531c6cae97SLawrence Stewart int 4054736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4055736ee590SJeff Roberson { 4056bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 4057003cf08bSMark Johnston int count; 4058bb15d1c7SGleb Smirnoff 40594bd61e19SJeff Roberson /* 40604bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 40614bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 40624bd61e19SJeff Roberson * way to clear a limit. 40634bd61e19SJeff Roberson */ 4064bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4065003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4066003cf08bSMark Johnston count = ubz != NULL ? ubz->ubz_entries : 0; 406720a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = count; 406820a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 406920a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4070bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4071cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4072cc7ce83aSJeff Roberson zone_update_caches(zone); 40734bd61e19SJeff Roberson /* We may need to wake waiters. */ 40744bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4075bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4076bb15d1c7SGleb Smirnoff 4077bb15d1c7SGleb Smirnoff return (nitems); 4078bb15d1c7SGleb Smirnoff } 4079bb15d1c7SGleb Smirnoff 4080bb15d1c7SGleb Smirnoff /* See uma.h */ 4081003cf08bSMark Johnston void 4082bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4083bb15d1c7SGleb Smirnoff { 4084003cf08bSMark Johnston struct uma_bucket_zone *ubz; 4085003cf08bSMark Johnston int bpcpu; 4086bb15d1c7SGleb Smirnoff 4087bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4088003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4089003cf08bSMark Johnston if (ubz != NULL) { 4090003cf08bSMark Johnston bpcpu = 2; 4091dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 4092003cf08bSMark Johnston /* Count the cross-domain bucket. */ 4093003cf08bSMark Johnston bpcpu++; 4094003cf08bSMark Johnston nitems -= ubz->ubz_entries * bpcpu * mp_ncpus; 409520a4e154SJeff Roberson zone->uz_bucket_size_max = ubz->ubz_entries; 4096003cf08bSMark Johnston } else { 409720a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 4098003cf08bSMark Johnston } 409920a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 410020a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4101bb15d1c7SGleb Smirnoff zone->uz_bkt_max = nitems; 4102bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4103736ee590SJeff Roberson } 4104736ee590SJeff Roberson 4105736ee590SJeff Roberson /* See uma.h */ 4106e49471b0SAndre Oppermann int 4107e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4108e49471b0SAndre Oppermann { 4109e49471b0SAndre Oppermann int nitems; 4110e49471b0SAndre Oppermann 4111727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4112e49471b0SAndre Oppermann 4113e49471b0SAndre Oppermann return (nitems); 4114e49471b0SAndre Oppermann } 4115e49471b0SAndre Oppermann 4116e49471b0SAndre Oppermann /* See uma.h */ 41172f891cd5SPawel Jakub Dawidek void 41182f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 41192f891cd5SPawel Jakub Dawidek { 41202f891cd5SPawel Jakub Dawidek 4121727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 41222f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 41232f891cd5SPawel Jakub Dawidek } 41242f891cd5SPawel Jakub Dawidek 41252f891cd5SPawel Jakub Dawidek /* See uma.h */ 412654503a13SJonathan T. Looney void 412754503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 412854503a13SJonathan T. Looney { 412954503a13SJonathan T. Looney 4130727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4131e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 413254503a13SJonathan T. Looney } 413354503a13SJonathan T. Looney 413454503a13SJonathan T. Looney /* See uma.h */ 4135c4ae7908SLawrence Stewart int 4136c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4137c4ae7908SLawrence Stewart { 4138c4ae7908SLawrence Stewart int64_t nitems; 4139c4ae7908SLawrence Stewart u_int i; 4140c4ae7908SLawrence Stewart 4141bfb6b7a1SJeff Roberson nitems = 0; 4142bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER) 41432efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 41442efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4145727c6918SJeff Roberson CPU_FOREACH(i) 4146727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4147727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4148c4ae7908SLawrence Stewart 4149c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4150c4ae7908SLawrence Stewart } 4151c4ae7908SLawrence Stewart 415220a4e154SJeff Roberson static uint64_t 415320a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 415420a4e154SJeff Roberson { 415520a4e154SJeff Roberson uint64_t nitems; 415620a4e154SJeff Roberson u_int i; 415720a4e154SJeff Roberson 4158bfb6b7a1SJeff Roberson nitems = 0; 4159bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER) 416020a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4161727c6918SJeff Roberson CPU_FOREACH(i) 4162727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 416320a4e154SJeff Roberson 416420a4e154SJeff Roberson return (nitems); 416520a4e154SJeff Roberson } 416620a4e154SJeff Roberson 416720a4e154SJeff Roberson static uint64_t 416820a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 416920a4e154SJeff Roberson { 417020a4e154SJeff Roberson uint64_t nitems; 417120a4e154SJeff Roberson u_int i; 417220a4e154SJeff Roberson 4173bfb6b7a1SJeff Roberson nitems = 0; 4174bfb6b7a1SJeff Roberson if (zone->uz_frees != EARLY_COUNTER) 417520a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4176727c6918SJeff Roberson CPU_FOREACH(i) 4177727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 417820a4e154SJeff Roberson 417920a4e154SJeff Roberson return (nitems); 418020a4e154SJeff Roberson } 418120a4e154SJeff Roberson 418231c251a0SJeff Roberson #ifdef INVARIANTS 418331c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */ 418431c251a0SJeff Roberson static uint64_t 418531c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg) 418631c251a0SJeff Roberson { 418731c251a0SJeff Roberson uma_zone_t z; 418831c251a0SJeff Roberson uint64_t nitems; 418931c251a0SJeff Roberson 419031c251a0SJeff Roberson nitems = 0; 419131c251a0SJeff Roberson LIST_FOREACH(z, &keg->uk_zones, uz_link) 419231c251a0SJeff Roberson nitems += uma_zone_get_allocs(z); 419331c251a0SJeff Roberson 419431c251a0SJeff Roberson return (nitems); 419531c251a0SJeff Roberson } 419631c251a0SJeff Roberson #endif 419731c251a0SJeff Roberson 4198c4ae7908SLawrence Stewart /* See uma.h */ 4199736ee590SJeff Roberson void 4200099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4201099a0e58SBosko Milekic { 4202e20a199fSJeff Roberson uma_keg_t keg; 4203e20a199fSJeff Roberson 4204bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4205727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4206e20a199fSJeff Roberson keg->uk_init = uminit; 4207099a0e58SBosko Milekic } 4208099a0e58SBosko Milekic 4209099a0e58SBosko Milekic /* See uma.h */ 4210099a0e58SBosko Milekic void 4211099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4212099a0e58SBosko Milekic { 4213e20a199fSJeff Roberson uma_keg_t keg; 4214e20a199fSJeff Roberson 4215bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4216727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4217e20a199fSJeff Roberson keg->uk_fini = fini; 4218099a0e58SBosko Milekic } 4219099a0e58SBosko Milekic 4220099a0e58SBosko Milekic /* See uma.h */ 4221099a0e58SBosko Milekic void 4222099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4223099a0e58SBosko Milekic { 4224af526374SJeff Roberson 4225727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4226099a0e58SBosko Milekic zone->uz_init = zinit; 4227099a0e58SBosko Milekic } 4228099a0e58SBosko Milekic 4229099a0e58SBosko Milekic /* See uma.h */ 4230099a0e58SBosko Milekic void 4231099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4232099a0e58SBosko Milekic { 4233af526374SJeff Roberson 4234727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4235099a0e58SBosko Milekic zone->uz_fini = zfini; 4236099a0e58SBosko Milekic } 4237099a0e58SBosko Milekic 4238099a0e58SBosko Milekic /* See uma.h */ 4239099a0e58SBosko Milekic void 42408355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 42418355f576SJeff Roberson { 42420095a784SJeff Roberson uma_keg_t keg; 4243e20a199fSJeff Roberson 4244bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4245727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 42460095a784SJeff Roberson keg->uk_freef = freef; 42478355f576SJeff Roberson } 42488355f576SJeff Roberson 42498355f576SJeff Roberson /* See uma.h */ 42508355f576SJeff Roberson void 42518355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 42528355f576SJeff Roberson { 4253e20a199fSJeff Roberson uma_keg_t keg; 4254e20a199fSJeff Roberson 4255bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4256727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4257e20a199fSJeff Roberson keg->uk_allocf = allocf; 42588355f576SJeff Roberson } 42598355f576SJeff Roberson 42608355f576SJeff Roberson /* See uma.h */ 42616fd34d6fSJeff Roberson void 42626fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 42636fd34d6fSJeff Roberson { 42646fd34d6fSJeff Roberson uma_keg_t keg; 42656fd34d6fSJeff Roberson 4266bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4267727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 42686fd34d6fSJeff Roberson keg->uk_reserve = items; 42696fd34d6fSJeff Roberson } 42706fd34d6fSJeff Roberson 42716fd34d6fSJeff Roberson /* See uma.h */ 42728355f576SJeff Roberson int 4273a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 42748355f576SJeff Roberson { 4275099a0e58SBosko Milekic uma_keg_t keg; 42768355f576SJeff Roberson vm_offset_t kva; 42779ba30bcbSZbigniew Bodek u_int pages; 42788355f576SJeff Roberson 4279bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4280727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4281727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 42828355f576SJeff Roberson 428379c9f942SJeff Roberson pages = howmany(count, keg->uk_ipers) * keg->uk_ppera; 4284a553d4b8SJeff Roberson 4285a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4286a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4287a4915c21SAttilio Rao #else 4288a4915c21SAttilio Rao if (1) { 4289a4915c21SAttilio Rao #endif 429057223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4291d1f42ac2SAlan Cox if (kva == 0) 42928355f576SJeff Roberson return (0); 4293a4915c21SAttilio Rao } else 4294a4915c21SAttilio Rao kva = 0; 4295bb15d1c7SGleb Smirnoff 4296bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4297bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4298099a0e58SBosko Milekic keg->uk_kva = kva; 4299a4915c21SAttilio Rao keg->uk_offset = 0; 4300bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4301a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4302a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4303a4915c21SAttilio Rao #else 4304a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4305a4915c21SAttilio Rao #endif 4306cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4307cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4308cc7ce83aSJeff Roberson zone_update_caches(zone); 4309bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4310af526374SJeff Roberson 43118355f576SJeff Roberson return (1); 43128355f576SJeff Roberson } 43138355f576SJeff Roberson 43148355f576SJeff Roberson /* See uma.h */ 43158355f576SJeff Roberson void 43168355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 43178355f576SJeff Roberson { 4318920239efSMark Johnston struct vm_domainset_iter di; 4319ab3185d1SJeff Roberson uma_domain_t dom; 43208355f576SJeff Roberson uma_slab_t slab; 4321099a0e58SBosko Milekic uma_keg_t keg; 432286220393SMark Johnston int aflags, domain, slabs; 43238355f576SJeff Roberson 4324bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 432579c9f942SJeff Roberson slabs = howmany(items, keg->uk_ipers); 4326194a979eSMark Johnston while (slabs-- > 0) { 432786220393SMark Johnston aflags = M_NOWAIT; 432886220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 432986220393SMark Johnston &aflags); 433086220393SMark Johnston for (;;) { 433186220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 433286220393SMark Johnston aflags); 433386220393SMark Johnston if (slab != NULL) { 4334ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 43358b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 433686220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 433786220393SMark Johnston us_link); 43388b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 4339920239efSMark Johnston break; 43408355f576SJeff Roberson } 43418b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 434286220393SMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 434386220393SMark Johnston } 434486220393SMark Johnston } 434586220393SMark Johnston } 43468355f576SJeff Roberson 43478355f576SJeff Roberson /* See uma.h */ 434808cfa56eSMark Johnston void 434908cfa56eSMark Johnston uma_reclaim(int req) 43508355f576SJeff Roberson { 435144ec2b63SKonstantin Belousov 43521431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 435308cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 435486bbae32SJeff Roberson bucket_enable(); 435508cfa56eSMark Johnston 435608cfa56eSMark Johnston switch (req) { 435708cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 435820a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 435908cfa56eSMark Johnston break; 436008cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 436108cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 436220a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 436308cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 436408cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 436520a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4366a2de44abSAlexander Motin } 436708cfa56eSMark Johnston break; 436808cfa56eSMark Johnston default: 436908cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 437008cfa56eSMark Johnston } 43710f9b7bf3SMark Johnston 43728355f576SJeff Roberson /* 43738355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 43748355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 43758355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 43768355f576SJeff Roberson */ 437720a4e154SJeff Roberson zone_drain(slabzone, NULL); 4378cae33c14SJeff Roberson bucket_zone_drain(); 437908cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 43808355f576SJeff Roberson } 43818355f576SJeff Roberson 43822e47807cSJeff Roberson static volatile int uma_reclaim_needed; 438344ec2b63SKonstantin Belousov 438444ec2b63SKonstantin Belousov void 438544ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 438644ec2b63SKonstantin Belousov { 438744ec2b63SKonstantin Belousov 43882e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 43892e47807cSJeff Roberson wakeup(uma_reclaim); 439044ec2b63SKonstantin Belousov } 439144ec2b63SKonstantin Belousov 439244ec2b63SKonstantin Belousov void 439344ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 439444ec2b63SKonstantin Belousov { 439544ec2b63SKonstantin Belousov 439644ec2b63SKonstantin Belousov for (;;) { 439708cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4398200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 439908cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 44002e47807cSJeff Roberson hz); 440108cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 44029b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 440308cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4404200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 44052e47807cSJeff Roberson /* Don't fire more than once per-second. */ 44062e47807cSJeff Roberson pause("umarclslp", hz); 440744ec2b63SKonstantin Belousov } 440844ec2b63SKonstantin Belousov } 440944ec2b63SKonstantin Belousov 4410663b416fSJohn Baldwin /* See uma.h */ 441108cfa56eSMark Johnston void 441208cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 441308cfa56eSMark Johnston { 441408cfa56eSMark Johnston 441508cfa56eSMark Johnston switch (req) { 441608cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 441720a4e154SJeff Roberson zone_trim(zone, NULL); 441808cfa56eSMark Johnston break; 441908cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 442020a4e154SJeff Roberson zone_drain(zone, NULL); 442108cfa56eSMark Johnston break; 442208cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 442308cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 442420a4e154SJeff Roberson zone_drain(zone, NULL); 442508cfa56eSMark Johnston break; 442608cfa56eSMark Johnston default: 442708cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 442808cfa56eSMark Johnston } 442908cfa56eSMark Johnston } 443008cfa56eSMark Johnston 443108cfa56eSMark Johnston /* See uma.h */ 4432663b416fSJohn Baldwin int 4433663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 4434663b416fSJohn Baldwin { 4435663b416fSJohn Baldwin 4436727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 44376c125b8dSMohan Srinivasan } 44386c125b8dSMohan Srinivasan 44392e47807cSJeff Roberson unsigned long 44402e47807cSJeff Roberson uma_limit(void) 44412e47807cSJeff Roberson { 44422e47807cSJeff Roberson 44432e47807cSJeff Roberson return (uma_kmem_limit); 44442e47807cSJeff Roberson } 44452e47807cSJeff Roberson 44462e47807cSJeff Roberson void 44472e47807cSJeff Roberson uma_set_limit(unsigned long limit) 44482e47807cSJeff Roberson { 44492e47807cSJeff Roberson 44502e47807cSJeff Roberson uma_kmem_limit = limit; 44512e47807cSJeff Roberson } 44522e47807cSJeff Roberson 44532e47807cSJeff Roberson unsigned long 44542e47807cSJeff Roberson uma_size(void) 44552e47807cSJeff Roberson { 44562e47807cSJeff Roberson 4457058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 4458ad5b0f5bSJeff Roberson } 4459ad5b0f5bSJeff Roberson 4460ad5b0f5bSJeff Roberson long 4461ad5b0f5bSJeff Roberson uma_avail(void) 4462ad5b0f5bSJeff Roberson { 4463ad5b0f5bSJeff Roberson 4464058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 44652e47807cSJeff Roberson } 44662e47807cSJeff Roberson 4467a0d4b0aeSRobert Watson #ifdef DDB 44688355f576SJeff Roberson /* 44697a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 44707a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 44717a52a97eSRobert Watson * 44727a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 44737a52a97eSRobert Watson * per-CPU cache statistic. 44747a52a97eSRobert Watson * 44757a52a97eSRobert Watson */ 44767a52a97eSRobert Watson static void 44770f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 4478c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 44797a52a97eSRobert Watson { 44807a52a97eSRobert Watson uma_cache_t cache; 4481c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 44827a52a97eSRobert Watson int cachefree, cpu; 44837a52a97eSRobert Watson 4484c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 44857a52a97eSRobert Watson cachefree = 0; 44863aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 44877a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 4488376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 4489376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 4490376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 4491376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 44927a52a97eSRobert Watson allocs += cache->uc_allocs; 44937a52a97eSRobert Watson frees += cache->uc_frees; 44947a52a97eSRobert Watson } 44952efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 44962efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 4497bf965959SSean Bruno sleeps += z->uz_sleeps; 4498c1685086SJeff Roberson xdomain += z->uz_xdomain; 44997a52a97eSRobert Watson if (cachefreep != NULL) 45007a52a97eSRobert Watson *cachefreep = cachefree; 45017a52a97eSRobert Watson if (allocsp != NULL) 45027a52a97eSRobert Watson *allocsp = allocs; 45037a52a97eSRobert Watson if (freesp != NULL) 45047a52a97eSRobert Watson *freesp = frees; 4505bf965959SSean Bruno if (sleepsp != NULL) 4506bf965959SSean Bruno *sleepsp = sleeps; 4507c1685086SJeff Roberson if (xdomainp != NULL) 4508c1685086SJeff Roberson *xdomainp = xdomain; 45097a52a97eSRobert Watson } 4510a0d4b0aeSRobert Watson #endif /* DDB */ 45117a52a97eSRobert Watson 45127a52a97eSRobert Watson static int 45137a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 45147a52a97eSRobert Watson { 45157a52a97eSRobert Watson uma_keg_t kz; 45167a52a97eSRobert Watson uma_zone_t z; 45177a52a97eSRobert Watson int count; 45187a52a97eSRobert Watson 45197a52a97eSRobert Watson count = 0; 4520111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 45217a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 45227a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 45237a52a97eSRobert Watson count++; 45247a52a97eSRobert Watson } 4525b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4526b47acb0aSGleb Smirnoff count++; 4527b47acb0aSGleb Smirnoff 4528111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 45297a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 45307a52a97eSRobert Watson } 45317a52a97eSRobert Watson 4532b47acb0aSGleb Smirnoff static void 4533b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 4534b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 4535b47acb0aSGleb Smirnoff { 4536b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 4537b47acb0aSGleb Smirnoff uma_cache_t cache; 4538b47acb0aSGleb Smirnoff int i; 4539b47acb0aSGleb Smirnoff 4540b47acb0aSGleb Smirnoff 4541b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 4542b47acb0aSGleb Smirnoff zdom = &z->uz_domain[i]; 4543b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 4544b47acb0aSGleb Smirnoff } 4545b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 4546b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 4547b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 4548b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 4549c1685086SJeff Roberson uth->uth_xdomain = z->uz_xdomain; 45501de9724eSMark Johnston 4551b47acb0aSGleb Smirnoff /* 45521de9724eSMark Johnston * While it is not normally safe to access the cache bucket pointers 45531de9724eSMark Johnston * while not on the CPU that owns the cache, we only allow the pointers 45541de9724eSMark Johnston * to be exchanged without the zone lock held, not invalidated, so 45551de9724eSMark Johnston * accept the possible race associated with bucket exchange during 45561de9724eSMark Johnston * monitoring. Use atomic_load_ptr() to ensure that the bucket pointers 45571de9724eSMark Johnston * are loaded only once. 4558b47acb0aSGleb Smirnoff */ 4559b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 4560b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 4561b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 4562b47acb0aSGleb Smirnoff continue; 4563b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 4564376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 4565376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 4566376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 4567b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 4568b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 4569b47acb0aSGleb Smirnoff } 4570b47acb0aSGleb Smirnoff } 4571b47acb0aSGleb Smirnoff 45727a52a97eSRobert Watson static int 45737a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 45747a52a97eSRobert Watson { 45757a52a97eSRobert Watson struct uma_stream_header ush; 45767a52a97eSRobert Watson struct uma_type_header uth; 457763b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 45787a52a97eSRobert Watson struct sbuf sbuf; 45797a52a97eSRobert Watson uma_keg_t kz; 45807a52a97eSRobert Watson uma_zone_t z; 45814bd61e19SJeff Roberson uint64_t items; 45828b987a77SJeff Roberson uint32_t kfree, pages; 45834e657159SMatthew D Fleming int count, error, i; 45847a52a97eSRobert Watson 458500f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 458600f0e671SMatthew D Fleming if (error != 0) 458700f0e671SMatthew D Fleming return (error); 45884e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 45891eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 459063b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 45914e657159SMatthew D Fleming 4592404a593eSMatthew D Fleming count = 0; 4593111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 45947a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 45957a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 45967a52a97eSRobert Watson count++; 45977a52a97eSRobert Watson } 45987a52a97eSRobert Watson 4599b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4600b47acb0aSGleb Smirnoff count++; 4601b47acb0aSGleb Smirnoff 46027a52a97eSRobert Watson /* 46037a52a97eSRobert Watson * Insert stream header. 46047a52a97eSRobert Watson */ 46057a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 46067a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 4607ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 46087a52a97eSRobert Watson ush.ush_count = count; 46094e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 46107a52a97eSRobert Watson 46117a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 46128b987a77SJeff Roberson kfree = pages = 0; 46138b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 46148b987a77SJeff Roberson kfree += kz->uk_domain[i].ud_free; 46158b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 46168b987a77SJeff Roberson } 46177a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 46187a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 46197a52a97eSRobert Watson ZONE_LOCK(z); 4620cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 46217a52a97eSRobert Watson uth.uth_align = kz->uk_align; 46227a52a97eSRobert Watson uth.uth_size = kz->uk_size; 46237a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 46244bd61e19SJeff Roberson if (z->uz_max_items > 0) { 46254bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 46264bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 4627bb15d1c7SGleb Smirnoff kz->uk_ppera; 46284bd61e19SJeff Roberson } else 46298b987a77SJeff Roberson uth.uth_pages = pages; 4630f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 4631bb15d1c7SGleb Smirnoff kz->uk_ppera; 4632bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 46338b987a77SJeff Roberson uth.uth_keg_free = kfree; 4634cbbb4a00SRobert Watson 4635cbbb4a00SRobert Watson /* 4636cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 4637cbbb4a00SRobert Watson * on the keg's zone list. 4638cbbb4a00SRobert Watson */ 4639e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 4640cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 4641cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 4642b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 4643b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 46442450bbb8SRobert Watson ZONE_UNLOCK(z); 464563b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 464663b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 464763b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 46487a52a97eSRobert Watson } 46497a52a97eSRobert Watson } 4650b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 4651b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 4652b47acb0aSGleb Smirnoff ZONE_LOCK(z); 4653b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 4654b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 4655b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 4656b47acb0aSGleb Smirnoff ZONE_UNLOCK(z); 4657b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 4658b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 4659b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 4660b47acb0aSGleb Smirnoff } 4661b47acb0aSGleb Smirnoff 4662111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 46634e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 46644e657159SMatthew D Fleming sbuf_delete(&sbuf); 466563b5d112SKonstantin Belousov free(ups, M_TEMP); 46667a52a97eSRobert Watson return (error); 46677a52a97eSRobert Watson } 466848c5777eSRobert Watson 46690a5a3ccbSGleb Smirnoff int 46700a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 46710a5a3ccbSGleb Smirnoff { 46720a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 467316be9f54SGleb Smirnoff int error, max; 46740a5a3ccbSGleb Smirnoff 467516be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 46760a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 46770a5a3ccbSGleb Smirnoff if (error || !req->newptr) 46780a5a3ccbSGleb Smirnoff return (error); 46790a5a3ccbSGleb Smirnoff 46800a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 46810a5a3ccbSGleb Smirnoff 46820a5a3ccbSGleb Smirnoff return (0); 46830a5a3ccbSGleb Smirnoff } 46840a5a3ccbSGleb Smirnoff 46850a5a3ccbSGleb Smirnoff int 46860a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 46870a5a3ccbSGleb Smirnoff { 468820a4e154SJeff Roberson uma_zone_t zone; 46890a5a3ccbSGleb Smirnoff int cur; 46900a5a3ccbSGleb Smirnoff 469120a4e154SJeff Roberson /* 469220a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 469320a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 469420a4e154SJeff Roberson */ 469520a4e154SJeff Roberson if (arg2 == 0) 469620a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 469720a4e154SJeff Roberson else 469820a4e154SJeff Roberson zone = arg1; 46990a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 47000a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 47010a5a3ccbSGleb Smirnoff } 47020a5a3ccbSGleb Smirnoff 470320a4e154SJeff Roberson static int 470420a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 470520a4e154SJeff Roberson { 470620a4e154SJeff Roberson uma_zone_t zone = arg1; 470720a4e154SJeff Roberson uint64_t cur; 470820a4e154SJeff Roberson 470920a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 471020a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 471120a4e154SJeff Roberson } 471220a4e154SJeff Roberson 471320a4e154SJeff Roberson static int 471420a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 471520a4e154SJeff Roberson { 471620a4e154SJeff Roberson uma_zone_t zone = arg1; 471720a4e154SJeff Roberson uint64_t cur; 471820a4e154SJeff Roberson 471920a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 472020a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 472120a4e154SJeff Roberson } 472220a4e154SJeff Roberson 47236d204a6aSRyan Libby static int 47246d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 47256d204a6aSRyan Libby { 47266d204a6aSRyan Libby struct sbuf sbuf; 47276d204a6aSRyan Libby uma_zone_t zone = arg1; 47286d204a6aSRyan Libby int error; 47296d204a6aSRyan Libby 47306d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 47316d204a6aSRyan Libby if (zone->uz_flags != 0) 47326d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 47336d204a6aSRyan Libby else 47346d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 47356d204a6aSRyan Libby error = sbuf_finish(&sbuf); 47366d204a6aSRyan Libby sbuf_delete(&sbuf); 47376d204a6aSRyan Libby 47386d204a6aSRyan Libby return (error); 47396d204a6aSRyan Libby } 47406d204a6aSRyan Libby 4741f7af5015SRyan Libby static int 4742f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 4743f7af5015SRyan Libby { 4744f7af5015SRyan Libby uma_keg_t keg = arg1; 4745f7af5015SRyan Libby int avail, effpct, total; 4746f7af5015SRyan Libby 4747f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 474854c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 4749f7af5015SRyan Libby total += slab_sizeof(SLAB_MAX_SETSIZE); 4750f7af5015SRyan Libby /* 4751f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 4752f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 4753f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 4754f7af5015SRyan Libby */ 4755f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 4756f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 4757f7af5015SRyan Libby avail *= mp_maxid + 1; 4758f7af5015SRyan Libby effpct = 100 * avail / total; 4759f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 4760f7af5015SRyan Libby } 4761f7af5015SRyan Libby 47624bd61e19SJeff Roberson static int 47634bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 47644bd61e19SJeff Roberson { 47654bd61e19SJeff Roberson uma_zone_t zone = arg1; 47664bd61e19SJeff Roberson uint64_t cur; 47674bd61e19SJeff Roberson 47684bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 47694bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 47704bd61e19SJeff Roberson } 47714bd61e19SJeff Roberson 47729542ea7bSGleb Smirnoff #ifdef INVARIANTS 47739542ea7bSGleb Smirnoff static uma_slab_t 47749542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 47759542ea7bSGleb Smirnoff { 47769542ea7bSGleb Smirnoff uma_slab_t slab; 47779542ea7bSGleb Smirnoff uma_keg_t keg; 47789542ea7bSGleb Smirnoff uint8_t *mem; 47799542ea7bSGleb Smirnoff 47809542ea7bSGleb Smirnoff /* 47819542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 47829542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 47839542ea7bSGleb Smirnoff * essentially holds a reference. 47849542ea7bSGleb Smirnoff */ 4785727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 4786727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 4787bb15d1c7SGleb Smirnoff return (NULL); 478854c5ae80SRyan Libby if (zone->uz_flags & UMA_ZFLAG_VTOSLAB) 4789727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 4790bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 479154c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0) 4792727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 47938b987a77SJeff Roberson KEG_LOCK(keg, 0); 47949542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 47958b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 47969542ea7bSGleb Smirnoff 47979542ea7bSGleb Smirnoff return (slab); 47989542ea7bSGleb Smirnoff } 47999542ea7bSGleb Smirnoff 4800c5deaf04SGleb Smirnoff static bool 4801c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 4802c5deaf04SGleb Smirnoff { 4803c5deaf04SGleb Smirnoff 4804727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 4805c5deaf04SGleb Smirnoff return (true); 4806c5deaf04SGleb Smirnoff 4807bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 4808c5deaf04SGleb Smirnoff } 4809c5deaf04SGleb Smirnoff 4810c5deaf04SGleb Smirnoff static bool 4811c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 4812c5deaf04SGleb Smirnoff { 4813c5deaf04SGleb Smirnoff uintptr_t idx; 4814c5deaf04SGleb Smirnoff 4815c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 4816c5deaf04SGleb Smirnoff return (true); 4817c5deaf04SGleb Smirnoff 4818c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 4819c5deaf04SGleb Smirnoff return (false); 4820c5deaf04SGleb Smirnoff 4821c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 4822c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 4823c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 4824c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 4825c5deaf04SGleb Smirnoff } 4826c5deaf04SGleb Smirnoff 4827c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 4828c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 4829c5deaf04SGleb Smirnoff return (true); 4830c5deaf04SGleb Smirnoff } 4831c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 4832c5deaf04SGleb Smirnoff 4833c5deaf04SGleb Smirnoff return (false); 4834c5deaf04SGleb Smirnoff } 4835c5deaf04SGleb Smirnoff 48369542ea7bSGleb Smirnoff /* 48379542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 48389542ea7bSGleb Smirnoff * 48399542ea7bSGleb Smirnoff */ 48409542ea7bSGleb Smirnoff static void 48419542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 48429542ea7bSGleb Smirnoff { 48439542ea7bSGleb Smirnoff uma_keg_t keg; 48449542ea7bSGleb Smirnoff int freei; 48459542ea7bSGleb Smirnoff 48469542ea7bSGleb Smirnoff if (slab == NULL) { 48479542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 48489542ea7bSGleb Smirnoff if (slab == NULL) 48499542ea7bSGleb Smirnoff panic("uma: item %p did not belong to zone %s\n", 48509542ea7bSGleb Smirnoff item, zone->uz_name); 48519542ea7bSGleb Smirnoff } 4852584061b4SJeff Roberson keg = zone->uz_keg; 48531e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 48549542ea7bSGleb Smirnoff 4855815db204SRyan Libby if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 48569542ea7bSGleb Smirnoff panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n", 48579542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 4858815db204SRyan Libby BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 48599542ea7bSGleb Smirnoff } 48609542ea7bSGleb Smirnoff 48619542ea7bSGleb Smirnoff /* 48629542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 48639542ea7bSGleb Smirnoff * and duplicate frees. 48649542ea7bSGleb Smirnoff * 48659542ea7bSGleb Smirnoff */ 48669542ea7bSGleb Smirnoff static void 48679542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 48689542ea7bSGleb Smirnoff { 48699542ea7bSGleb Smirnoff uma_keg_t keg; 48709542ea7bSGleb Smirnoff int freei; 48719542ea7bSGleb Smirnoff 48729542ea7bSGleb Smirnoff if (slab == NULL) { 48739542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 48749542ea7bSGleb Smirnoff if (slab == NULL) 48759542ea7bSGleb Smirnoff panic("uma: Freed item %p did not belong to zone %s\n", 48769542ea7bSGleb Smirnoff item, zone->uz_name); 48779542ea7bSGleb Smirnoff } 4878584061b4SJeff Roberson keg = zone->uz_keg; 48791e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 48809542ea7bSGleb Smirnoff 48819542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 48829542ea7bSGleb Smirnoff panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n", 48839542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48849542ea7bSGleb Smirnoff 48851e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 48869542ea7bSGleb Smirnoff panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n", 48879542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48889542ea7bSGleb Smirnoff 4889815db204SRyan Libby if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 48909542ea7bSGleb Smirnoff panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n", 48919542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48929542ea7bSGleb Smirnoff 4893815db204SRyan Libby BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 48949542ea7bSGleb Smirnoff } 48959542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 48969542ea7bSGleb Smirnoff 489748c5777eSRobert Watson #ifdef DDB 489846d70077SConrad Meyer static int64_t 489946d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 49000223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 490148c5777eSRobert Watson { 490246d70077SConrad Meyer uint64_t frees; 49030f9b7bf3SMark Johnston int i; 490448c5777eSRobert Watson 490548c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 490646d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 49072efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 490846d70077SConrad Meyer *sleeps = z->uz_sleeps; 490946d70077SConrad Meyer *cachefree = 0; 491046d70077SConrad Meyer *xdomain = 0; 491148c5777eSRobert Watson } else 491246d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 491346d70077SConrad Meyer xdomain); 49148b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 49158b987a77SJeff Roberson *cachefree += z->uz_domain[i].uzd_nitems; 4916e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 491748c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 49188b987a77SJeff Roberson *cachefree += kz->uk_domain[i].ud_free; 49198b987a77SJeff Roberson } 492046d70077SConrad Meyer *used = *allocs - frees; 492146d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 492246d70077SConrad Meyer } 49230f9b7bf3SMark Johnston 492446d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 492546d70077SConrad Meyer { 492646d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 492746d70077SConrad Meyer uma_keg_t kz; 492846d70077SConrad Meyer uma_zone_t z; 492946d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 493046d70077SConrad Meyer long cachefree; 493146d70077SConrad Meyer /* variables for sorting */ 493246d70077SConrad Meyer uma_keg_t cur_keg; 493346d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 493446d70077SConrad Meyer int64_t cur_size, last_size, size; 493546d70077SConrad Meyer int ties; 493646d70077SConrad Meyer 493746d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 493846d70077SConrad Meyer if (modif[0] == 'i') { 493946d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 494046d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 494146d70077SConrad Meyer } else { 494246d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 494346d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 494446d70077SConrad Meyer } 494546d70077SConrad Meyer 494646d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 494746d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 494846d70077SConrad Meyer 494946d70077SConrad Meyer /* Sort the zones with largest size first. */ 495046d70077SConrad Meyer last_zone = NULL; 495146d70077SConrad Meyer last_size = INT64_MAX; 495246d70077SConrad Meyer for (;;) { 495346d70077SConrad Meyer cur_zone = NULL; 495446d70077SConrad Meyer cur_size = -1; 495546d70077SConrad Meyer ties = 0; 495646d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 495746d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 495846d70077SConrad Meyer /* 495946d70077SConrad Meyer * In the case of size ties, print out zones 496046d70077SConrad Meyer * in the order they are encountered. That is, 496146d70077SConrad Meyer * when we encounter the most recently output 496246d70077SConrad Meyer * zone, we have already printed all preceding 496346d70077SConrad Meyer * ties, and we must print all following ties. 496446d70077SConrad Meyer */ 496546d70077SConrad Meyer if (z == last_zone) { 496646d70077SConrad Meyer ties = 1; 496746d70077SConrad Meyer continue; 496846d70077SConrad Meyer } 496946d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 497046d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 497146d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 497246d70077SConrad Meyer { 497346d70077SConrad Meyer cur_size = size; 497446d70077SConrad Meyer cur_zone = z; 497546d70077SConrad Meyer cur_keg = kz; 497646d70077SConrad Meyer } 497746d70077SConrad Meyer } 497846d70077SConrad Meyer } 497946d70077SConrad Meyer if (cur_zone == NULL) 498046d70077SConrad Meyer break; 498146d70077SConrad Meyer 498246d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 498346d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 498446d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 498546d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 498646d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 498720a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 498820a4e154SJeff Roberson xdomain); 498946d70077SConrad Meyer 4990687c94aaSJohn Baldwin if (db_pager_quit) 4991687c94aaSJohn Baldwin return; 499246d70077SConrad Meyer last_zone = cur_zone; 499346d70077SConrad Meyer last_size = cur_size; 499448c5777eSRobert Watson } 499548c5777eSRobert Watson } 499603175483SAlexander Motin 499703175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 499803175483SAlexander Motin { 499903175483SAlexander Motin uma_zone_t z; 5000ab3185d1SJeff Roberson uint64_t allocs, frees; 50010f9b7bf3SMark Johnston long cachefree; 50020f9b7bf3SMark Johnston int i; 500303175483SAlexander Motin 500403175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 500503175483SAlexander Motin "Requests", "Bucket"); 500603175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 5007c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 50080f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 50090f9b7bf3SMark Johnston cachefree += z->uz_domain[i].uzd_nitems; 50100f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 501103175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 501203175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 501320a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 501403175483SAlexander Motin if (db_pager_quit) 501503175483SAlexander Motin return; 501603175483SAlexander Motin } 501703175483SAlexander Motin } 50189542ea7bSGleb Smirnoff #endif /* DDB */ 5019