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> 80d4665eaaSJeff Roberson #include <sys/smr.h> 81e60b2fcbSGleb Smirnoff #include <sys/taskqueue.h> 8286bbae32SJeff Roberson #include <sys/vmmeter.h> 8386bbae32SJeff Roberson 848355f576SJeff Roberson #include <vm/vm.h> 85194a979eSMark Johnston #include <vm/vm_domainset.h> 868355f576SJeff Roberson #include <vm/vm_object.h> 878355f576SJeff Roberson #include <vm/vm_page.h> 88a4915c21SAttilio Rao #include <vm/vm_pageout.h> 898355f576SJeff Roberson #include <vm/vm_param.h> 90ab3185d1SJeff Roberson #include <vm/vm_phys.h> 9130c5525bSAndrew Gallatin #include <vm/vm_pagequeue.h> 928355f576SJeff Roberson #include <vm/vm_map.h> 938355f576SJeff Roberson #include <vm/vm_kern.h> 948355f576SJeff Roberson #include <vm/vm_extern.h> 958355f576SJeff Roberson #include <vm/uma.h> 968355f576SJeff Roberson #include <vm/uma_int.h> 97639c9550SJeff Roberson #include <vm/uma_dbg.h> 988355f576SJeff Roberson 9948c5777eSRobert Watson #include <ddb/ddb.h> 10048c5777eSRobert Watson 1018d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 1028d689e04SGleb Smirnoff #include <vm/memguard.h> 1038d689e04SGleb Smirnoff #endif 1048d689e04SGleb Smirnoff 105a81c400eSJeff Roberson #include <machine/md_var.h> 106a81c400eSJeff Roberson 107d4665eaaSJeff Roberson #ifdef INVARIANTS 108d4665eaaSJeff Roberson #define UMA_ALWAYS_CTORDTOR 1 109d4665eaaSJeff Roberson #else 110d4665eaaSJeff Roberson #define UMA_ALWAYS_CTORDTOR 0 111d4665eaaSJeff Roberson #endif 112d4665eaaSJeff Roberson 1138355f576SJeff Roberson /* 114ab3185d1SJeff Roberson * This is the zone and keg from which all zones are spawned. 1158355f576SJeff Roberson */ 116ab3185d1SJeff Roberson static uma_zone_t kegs; 117ab3185d1SJeff Roberson static uma_zone_t zones; 1188355f576SJeff Roberson 1199b8db4d0SRyan Libby /* 1209b8db4d0SRyan Libby * These are the two zones from which all offpage uma_slab_ts are allocated. 1219b8db4d0SRyan Libby * 1229b8db4d0SRyan Libby * One zone is for slab headers that can represent a larger number of items, 1239b8db4d0SRyan Libby * making the slabs themselves more efficient, and the other zone is for 1249b8db4d0SRyan Libby * headers that are smaller and represent fewer items, making the headers more 1259b8db4d0SRyan Libby * efficient. 1269b8db4d0SRyan Libby */ 1279b8db4d0SRyan Libby #define SLABZONE_SIZE(setsize) \ 1289b8db4d0SRyan Libby (sizeof(struct uma_hash_slab) + BITSET_SIZE(setsize) * SLAB_BITSETS) 1299b8db4d0SRyan Libby #define SLABZONE0_SETSIZE (PAGE_SIZE / 16) 1309b8db4d0SRyan Libby #define SLABZONE1_SETSIZE SLAB_MAX_SETSIZE 1319b8db4d0SRyan Libby #define SLABZONE0_SIZE SLABZONE_SIZE(SLABZONE0_SETSIZE) 1329b8db4d0SRyan Libby #define SLABZONE1_SIZE SLABZONE_SIZE(SLABZONE1_SETSIZE) 1339b8db4d0SRyan Libby static uma_zone_t slabzones[2]; 1348355f576SJeff Roberson 1358355f576SJeff Roberson /* 1368355f576SJeff Roberson * The initial hash tables come out of this zone so they can be allocated 1378355f576SJeff Roberson * prior to malloc coming up. 1388355f576SJeff Roberson */ 1398355f576SJeff Roberson static uma_zone_t hashzone; 1408355f576SJeff Roberson 1411e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */ 142e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1; 1431e319f6dSRobert Watson 144961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets"); 14520a4e154SJeff Roberson static MALLOC_DEFINE(M_UMA, "UMA", "UMA Misc"); 146961647dfSJeff Roberson 1478355f576SJeff Roberson /* 14886bbae32SJeff Roberson * Are we allowed to allocate buckets? 14986bbae32SJeff Roberson */ 15086bbae32SJeff Roberson static int bucketdisable = 1; 15186bbae32SJeff Roberson 152099a0e58SBosko Milekic /* Linked list of all kegs in the system */ 15313e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs); 1548355f576SJeff Roberson 15503175483SAlexander Motin /* Linked list of all cache-only zones in the system */ 15603175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones = 15703175483SAlexander Motin LIST_HEAD_INITIALIZER(uma_cachezones); 15803175483SAlexander Motin 159111fbcd5SBryan Venteicher /* This RW lock protects the keg list */ 160fe933c1dSMateusz Guzik static struct rwlock_padalign __exclusive_cache_line uma_rwlock; 1618355f576SJeff Roberson 162ac0a6fd0SGleb Smirnoff /* 163a81c400eSJeff Roberson * First available virual address for boot time allocations. 164ac0a6fd0SGleb Smirnoff */ 165a81c400eSJeff Roberson static vm_offset_t bootstart; 166a81c400eSJeff Roberson static vm_offset_t bootmem; 1678355f576SJeff Roberson 16808cfa56eSMark Johnston static struct sx uma_reclaim_lock; 16995c4bf75SKonstantin Belousov 170fbd95859SMark Johnston /* 171fbd95859SMark Johnston * kmem soft limit, initialized by uma_set_limit(). Ensure that early 172fbd95859SMark Johnston * allocations don't trigger a wakeup of the reclaim thread. 173fbd95859SMark Johnston */ 1746d6a03d7SJeff Roberson unsigned long uma_kmem_limit = LONG_MAX; 175fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_limit, CTLFLAG_RD, &uma_kmem_limit, 0, 176fbd95859SMark Johnston "UMA kernel memory soft limit"); 1776d6a03d7SJeff Roberson unsigned long uma_kmem_total; 178fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_total, CTLFLAG_RD, &uma_kmem_total, 0, 179fbd95859SMark Johnston "UMA kernel memory usage"); 1802e47807cSJeff Roberson 1818355f576SJeff Roberson /* Is the VM done starting up? */ 182860bb7a0SMark Johnston static enum { 183860bb7a0SMark Johnston BOOT_COLD, 184a81c400eSJeff Roberson BOOT_KVA, 185860bb7a0SMark Johnston BOOT_RUNNING, 186860bb7a0SMark Johnston BOOT_SHUTDOWN, 187860bb7a0SMark Johnston } booted = BOOT_COLD; 1888355f576SJeff Roberson 189ef72505eSJeff Roberson /* 1909643769aSJeff Roberson * This is the handle used to schedule events that need to happen 1919643769aSJeff Roberson * outside of the allocation fast path. 1929643769aSJeff Roberson */ 1938355f576SJeff Roberson static struct callout uma_callout; 1949643769aSJeff Roberson #define UMA_TIMEOUT 20 /* Seconds for callout interval. */ 1958355f576SJeff Roberson 1968355f576SJeff Roberson /* 1978355f576SJeff Roberson * This structure is passed as the zone ctor arg so that I don't have to create 1988355f576SJeff Roberson * a special allocation function just for zones. 1998355f576SJeff Roberson */ 2008355f576SJeff Roberson struct uma_zctor_args { 201bb196eb4SMatthew D Fleming const char *name; 202c3bdc05fSAndrew R. Reiter size_t size; 2038355f576SJeff Roberson uma_ctor ctor; 2048355f576SJeff Roberson uma_dtor dtor; 2058355f576SJeff Roberson uma_init uminit; 2068355f576SJeff Roberson uma_fini fini; 2070095a784SJeff Roberson uma_import import; 2080095a784SJeff Roberson uma_release release; 2090095a784SJeff Roberson void *arg; 210099a0e58SBosko Milekic uma_keg_t keg; 211099a0e58SBosko Milekic int align; 21285dcf349SGleb Smirnoff uint32_t flags; 213099a0e58SBosko Milekic }; 214099a0e58SBosko Milekic 215099a0e58SBosko Milekic struct uma_kctor_args { 216099a0e58SBosko Milekic uma_zone_t zone; 217099a0e58SBosko Milekic size_t size; 218099a0e58SBosko Milekic uma_init uminit; 219099a0e58SBosko Milekic uma_fini fini; 2208355f576SJeff Roberson int align; 22185dcf349SGleb Smirnoff uint32_t flags; 2228355f576SJeff Roberson }; 2238355f576SJeff Roberson 224cae33c14SJeff Roberson struct uma_bucket_zone { 225cae33c14SJeff Roberson uma_zone_t ubz_zone; 226cae33c14SJeff Roberson char *ubz_name; 227fc03d22bSJeff Roberson int ubz_entries; /* Number of items it can hold. */ 228fc03d22bSJeff Roberson int ubz_maxsize; /* Maximum allocation size per-item. */ 229cae33c14SJeff Roberson }; 230cae33c14SJeff Roberson 231f9d27e75SRobert Watson /* 232fc03d22bSJeff Roberson * Compute the actual number of bucket entries to pack them in power 233fc03d22bSJeff Roberson * of two sizes for more efficient space utilization. 234f9d27e75SRobert Watson */ 235fc03d22bSJeff Roberson #define BUCKET_SIZE(n) \ 236fc03d22bSJeff Roberson (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *)) 237fc03d22bSJeff Roberson 2381aa6c758SAlexander Motin #define BUCKET_MAX BUCKET_SIZE(256) 239e84130a0SJeff Roberson #define BUCKET_MIN 2 240fc03d22bSJeff Roberson 241fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = { 242e84130a0SJeff Roberson /* Literal bucket sizes. */ 243e84130a0SJeff Roberson { NULL, "2 Bucket", 2, 4096 }, 244e84130a0SJeff Roberson { NULL, "4 Bucket", 4, 3072 }, 245e84130a0SJeff Roberson { NULL, "8 Bucket", 8, 2048 }, 246e84130a0SJeff Roberson { NULL, "16 Bucket", 16, 1024 }, 247e84130a0SJeff Roberson /* Rounded down power of 2 sizes for efficiency. */ 248fc03d22bSJeff Roberson { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 249fc03d22bSJeff Roberson { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 250fc03d22bSJeff Roberson { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 2511aa6c758SAlexander Motin { NULL, "256 Bucket", BUCKET_SIZE(256), 64 }, 252fc03d22bSJeff Roberson { NULL, NULL, 0} 253fc03d22bSJeff Roberson }; 254cae33c14SJeff Roberson 2552019094aSRobert Watson /* 2562019094aSRobert Watson * Flags and enumerations to be passed to internal functions. 2572019094aSRobert Watson */ 258bb15d1c7SGleb Smirnoff enum zfreeskip { 259bb15d1c7SGleb Smirnoff SKIP_NONE = 0, 260bb15d1c7SGleb Smirnoff SKIP_CNT = 0x00000001, 261bb15d1c7SGleb Smirnoff SKIP_DTOR = 0x00010000, 262bb15d1c7SGleb Smirnoff SKIP_FINI = 0x00020000, 263bb15d1c7SGleb Smirnoff }; 264b23f72e9SBrian Feldman 2658355f576SJeff Roberson /* Prototypes.. */ 2668355f576SJeff Roberson 267a81c400eSJeff Roberson void uma_startup1(vm_offset_t); 268f4bef67cSGleb Smirnoff void uma_startup2(void); 269f4bef67cSGleb Smirnoff 270ab3185d1SJeff Roberson static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 271ab3185d1SJeff Roberson static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 272ab3059a8SMatt Macy static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 273ab3185d1SJeff Roberson static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 274*ec0d8280SRyan Libby static void *contig_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 275f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t); 276ab3059a8SMatt Macy static void pcpu_page_free(void *, vm_size_t, uint8_t); 27786220393SMark Johnston static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int); 2789643769aSJeff Roberson static void cache_drain(uma_zone_t); 2798355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t); 28008cfa56eSMark Johnston static void bucket_cache_reclaim(uma_zone_t zone, bool); 281b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int); 282099a0e58SBosko Milekic static void keg_dtor(void *, int, void *); 283b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int); 2849c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *); 285d4665eaaSJeff Roberson static inline void item_dtor(uma_zone_t zone, void *item, int size, 286d4665eaaSJeff Roberson void *udata, enum zfreeskip skip); 287b23f72e9SBrian Feldman static int zero_init(void *, int, int); 28820a4e154SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t, void *), void *); 289a81c400eSJeff Roberson static void zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *), void *); 29020a4e154SJeff Roberson static void zone_timeout(uma_zone_t zone, void *); 2913b2f2cb8SAlexander Motin static int hash_alloc(struct uma_hash *, u_int); 2920aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *); 2930aef6126SJeff Roberson static void hash_free(struct uma_hash *hash); 2948355f576SJeff Roberson static void uma_timeout(void *); 2958355f576SJeff Roberson static void uma_startup3(void); 296860bb7a0SMark Johnston static void uma_shutdown(void); 297ab3185d1SJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int, int); 2980095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 2994bd61e19SJeff Roberson static int zone_alloc_limit(uma_zone_t zone, int count, int flags); 3004bd61e19SJeff Roberson static void zone_free_limit(uma_zone_t zone, int count); 30186bbae32SJeff Roberson static void bucket_enable(void); 302cae33c14SJeff Roberson static void bucket_init(void); 3036fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int); 3046fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *); 305cae33c14SJeff Roberson static void bucket_zone_drain(void); 306beb8beefSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int); 3070095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 308bb15d1c7SGleb Smirnoff static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item); 309e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 31085dcf349SGleb Smirnoff uma_fini fini, int align, uint32_t flags); 311b75c4efcSAndrew Turner static int zone_import(void *, void **, int, int, int); 312b75c4efcSAndrew Turner static void zone_release(void *, void **, int); 313beb8beefSJeff Roberson static bool cache_alloc(uma_zone_t, uma_cache_t, void *, int); 3140a81b439SJeff Roberson static bool cache_free(uma_zone_t, uma_cache_t, void *, void *, int); 315bbee39c6SJeff Roberson 3167a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 3177a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 31820a4e154SJeff Roberson static int sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS); 31920a4e154SJeff Roberson static int sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS); 3206d204a6aSRyan Libby static int sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS); 321f7af5015SRyan Libby static int sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS); 3224bd61e19SJeff Roberson static int sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS); 3238355f576SJeff Roberson 32431c251a0SJeff Roberson static uint64_t uma_zone_get_allocs(uma_zone_t zone); 32531c251a0SJeff Roberson 3269542ea7bSGleb Smirnoff #ifdef INVARIANTS 32731c251a0SJeff Roberson static uint64_t uma_keg_get_allocs(uma_keg_t zone); 328815db204SRyan Libby static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg); 329815db204SRyan Libby 330c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem); 331c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem); 3329542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item); 3339542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item); 334c5deaf04SGleb Smirnoff 335c5deaf04SGleb Smirnoff static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0, 336c5deaf04SGleb Smirnoff "Memory allocation debugging"); 337c5deaf04SGleb Smirnoff 338c5deaf04SGleb Smirnoff static u_int dbg_divisor = 1; 339c5deaf04SGleb Smirnoff SYSCTL_UINT(_vm_debug, OID_AUTO, divisor, 340c5deaf04SGleb Smirnoff CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0, 341c5deaf04SGleb Smirnoff "Debug & thrash every this item in memory allocator"); 342c5deaf04SGleb Smirnoff 343c5deaf04SGleb Smirnoff static counter_u64_t uma_dbg_cnt = EARLY_COUNTER; 344c5deaf04SGleb Smirnoff static counter_u64_t uma_skip_cnt = EARLY_COUNTER; 345c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD, 346c5deaf04SGleb Smirnoff &uma_dbg_cnt, "memory items debugged"); 347c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD, 348c5deaf04SGleb Smirnoff &uma_skip_cnt, "memory items skipped, not debugged"); 3499542ea7bSGleb Smirnoff #endif 3509542ea7bSGleb Smirnoff 3518355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 3528355f576SJeff Roberson 35335ec24f3SRyan Libby SYSCTL_NODE(_vm, OID_AUTO, uma, CTLFLAG_RW, 0, "Universal Memory Allocator"); 35435ec24f3SRyan Libby 355a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_INT, 3567a52a97eSRobert Watson 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 3577a52a97eSRobert Watson 358a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_STRUCT, 3597a52a97eSRobert Watson 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 3607a52a97eSRobert Watson 3612f891cd5SPawel Jakub Dawidek static int zone_warnings = 1; 362af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0, 3632f891cd5SPawel Jakub Dawidek "Warn when UMA zones becomes full"); 3642f891cd5SPawel Jakub Dawidek 36586bbae32SJeff Roberson /* 3669b8db4d0SRyan Libby * Select the slab zone for an offpage slab with the given maximum item count. 3679b8db4d0SRyan Libby */ 3689b8db4d0SRyan Libby static inline uma_zone_t 3699b8db4d0SRyan Libby slabzone(int ipers) 3709b8db4d0SRyan Libby { 3719b8db4d0SRyan Libby 3729b8db4d0SRyan Libby return (slabzones[ipers > SLABZONE0_SETSIZE]); 3739b8db4d0SRyan Libby } 3749b8db4d0SRyan Libby 3759b8db4d0SRyan Libby /* 37686bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 37786bbae32SJeff Roberson */ 37886bbae32SJeff Roberson static void 37986bbae32SJeff Roberson bucket_enable(void) 38086bbae32SJeff Roberson { 3813182660aSRyan Libby 382a81c400eSJeff Roberson KASSERT(booted >= BOOT_KVA, ("Bucket enable before init")); 383251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 38486bbae32SJeff Roberson } 38586bbae32SJeff Roberson 386dc2c7965SRobert Watson /* 387dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 388dc2c7965SRobert Watson * 389dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 390fc03d22bSJeff Roberson * of the header and an array of pointers. 391dc2c7965SRobert Watson */ 392cae33c14SJeff Roberson static void 393cae33c14SJeff Roberson bucket_init(void) 394cae33c14SJeff Roberson { 395cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 396cae33c14SJeff Roberson int size; 397cae33c14SJeff Roberson 398d74e6a1dSAlan Cox for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 399cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 400cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 401cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 402e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 403dfe13344SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | 404dfe13344SJeff Roberson UMA_ZONE_FIRSTTOUCH); 405cae33c14SJeff Roberson } 406cae33c14SJeff Roberson } 407cae33c14SJeff Roberson 408dc2c7965SRobert Watson /* 409dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 410dc2c7965SRobert Watson * to allocate the bucket. 411dc2c7965SRobert Watson */ 412dc2c7965SRobert Watson static struct uma_bucket_zone * 413dc2c7965SRobert Watson bucket_zone_lookup(int entries) 414dc2c7965SRobert Watson { 415fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 416dc2c7965SRobert Watson 417fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 418fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 419fc03d22bSJeff Roberson return (ubz); 420fc03d22bSJeff Roberson ubz--; 421fc03d22bSJeff Roberson return (ubz); 422fc03d22bSJeff Roberson } 423fc03d22bSJeff Roberson 424003cf08bSMark Johnston static struct uma_bucket_zone * 425003cf08bSMark Johnston bucket_zone_max(uma_zone_t zone, int nitems) 426003cf08bSMark Johnston { 427003cf08bSMark Johnston struct uma_bucket_zone *ubz; 428003cf08bSMark Johnston int bpcpu; 429003cf08bSMark Johnston 430003cf08bSMark Johnston bpcpu = 2; 431dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 432003cf08bSMark Johnston /* Count the cross-domain bucket. */ 433003cf08bSMark Johnston bpcpu++; 434003cf08bSMark Johnston 435003cf08bSMark Johnston for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 436003cf08bSMark Johnston if (ubz->ubz_entries * bpcpu * mp_ncpus > nitems) 437003cf08bSMark Johnston break; 438003cf08bSMark Johnston if (ubz == &bucket_zones[0]) 439003cf08bSMark Johnston ubz = NULL; 440003cf08bSMark Johnston else 441003cf08bSMark Johnston ubz--; 442003cf08bSMark Johnston return (ubz); 443003cf08bSMark Johnston } 444003cf08bSMark Johnston 445fc03d22bSJeff Roberson static int 446fc03d22bSJeff Roberson bucket_select(int size) 447fc03d22bSJeff Roberson { 448fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 449fc03d22bSJeff Roberson 450fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 451fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 452fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 453fc03d22bSJeff Roberson 454fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 455fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 456fc03d22bSJeff Roberson break; 457fc03d22bSJeff Roberson ubz--; 458fc03d22bSJeff Roberson return (ubz->ubz_entries); 459dc2c7965SRobert Watson } 460dc2c7965SRobert Watson 461cae33c14SJeff Roberson static uma_bucket_t 4626fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags) 463cae33c14SJeff Roberson { 464cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 465cae33c14SJeff Roberson uma_bucket_t bucket; 466cae33c14SJeff Roberson 467cae33c14SJeff Roberson /* 468d4665eaaSJeff Roberson * Don't allocate buckets early in boot. 469cae33c14SJeff Roberson */ 470d4665eaaSJeff Roberson if (__predict_false(booted < BOOT_KVA)) 471cae33c14SJeff Roberson return (NULL); 472a81c400eSJeff Roberson 4736fd34d6fSJeff Roberson /* 4746fd34d6fSJeff Roberson * To limit bucket recursion we store the original zone flags 4756fd34d6fSJeff Roberson * in a cookie passed via zalloc_arg/zfree_arg. This allows the 4766fd34d6fSJeff Roberson * NOVM flag to persist even through deep recursions. We also 4776fd34d6fSJeff Roberson * store ZFLAG_BUCKET once we have recursed attempting to allocate 4786fd34d6fSJeff Roberson * a bucket for a bucket zone so we do not allow infinite bucket 4796fd34d6fSJeff Roberson * recursion. This cookie will even persist to frees of unused 4806fd34d6fSJeff Roberson * buckets via the allocation path or bucket allocations in the 4816fd34d6fSJeff Roberson * free path. 4826fd34d6fSJeff Roberson */ 4836fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 4846fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 485e8a720feSAlexander Motin else { 486e8a720feSAlexander Motin if ((uintptr_t)udata & UMA_ZFLAG_BUCKET) 487e8a720feSAlexander Motin return (NULL); 4886fd34d6fSJeff Roberson udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET); 489e8a720feSAlexander Motin } 4906fd34d6fSJeff Roberson if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY) 491af526374SJeff Roberson flags |= M_NOVM; 49220a4e154SJeff Roberson ubz = bucket_zone_lookup(zone->uz_bucket_size); 49320d3ab87SAlexander Motin if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0) 49420d3ab87SAlexander Motin ubz++; 4956fd34d6fSJeff Roberson bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags); 496cae33c14SJeff Roberson if (bucket) { 497cae33c14SJeff Roberson #ifdef INVARIANTS 498cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 499cae33c14SJeff Roberson #endif 500cae33c14SJeff Roberson bucket->ub_cnt = 0; 501cae33c14SJeff Roberson bucket->ub_entries = ubz->ubz_entries; 502d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 503d4665eaaSJeff Roberson CTR3(KTR_UMA, "bucket_alloc: zone %s(%p) allocated bucket %p", 504d4665eaaSJeff Roberson zone->uz_name, zone, bucket); 505cae33c14SJeff Roberson } 506cae33c14SJeff Roberson 507cae33c14SJeff Roberson return (bucket); 508cae33c14SJeff Roberson } 509cae33c14SJeff Roberson 510cae33c14SJeff Roberson static void 5116fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata) 512cae33c14SJeff Roberson { 513cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 514cae33c14SJeff Roberson 515fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 516fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 517d4665eaaSJeff Roberson KASSERT(bucket->ub_seq == SMR_SEQ_INVALID, 518d4665eaaSJeff Roberson ("bucket_free: Freeing an SMR bucket.")); 5196fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 5206fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 521dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 5226fd34d6fSJeff Roberson uma_zfree_arg(ubz->ubz_zone, bucket, udata); 523cae33c14SJeff Roberson } 524cae33c14SJeff Roberson 525cae33c14SJeff Roberson static void 526cae33c14SJeff Roberson bucket_zone_drain(void) 527cae33c14SJeff Roberson { 528cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 529cae33c14SJeff Roberson 530cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 53108cfa56eSMark Johnston uma_zone_reclaim(ubz->ubz_zone, UMA_RECLAIM_DRAIN); 532cae33c14SJeff Roberson } 533cae33c14SJeff Roberson 53408cfa56eSMark Johnston /* 53508cfa56eSMark Johnston * Attempt to satisfy an allocation by retrieving a full bucket from one of the 536d4665eaaSJeff Roberson * zone's caches. If a bucket is found the zone is not locked on return. 53708cfa56eSMark Johnston */ 5380f9b7bf3SMark Johnston static uma_bucket_t 53908cfa56eSMark Johnston zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom) 5400f9b7bf3SMark Johnston { 5410f9b7bf3SMark Johnston uma_bucket_t bucket; 542d4665eaaSJeff Roberson int i; 543d4665eaaSJeff Roberson bool dtor = false; 5440f9b7bf3SMark Johnston 5450f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 5460f9b7bf3SMark Johnston 547dc3915c8SJeff Roberson if ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) == NULL) 548d4665eaaSJeff Roberson return (NULL); 549d4665eaaSJeff Roberson 550d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 551d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 552d4665eaaSJeff Roberson if (!smr_poll(zone->uz_smr, bucket->ub_seq, false)) 553d4665eaaSJeff Roberson return (NULL); 554d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 555d4665eaaSJeff Roberson dtor = (zone->uz_dtor != NULL) | UMA_ALWAYS_CTORDTOR; 556d4665eaaSJeff Roberson } 5570f9b7bf3SMark Johnston MPASS(zdom->uzd_nitems >= bucket->ub_cnt); 558dc3915c8SJeff Roberson STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link); 5590f9b7bf3SMark Johnston zdom->uzd_nitems -= bucket->ub_cnt; 56008cfa56eSMark Johnston if (zdom->uzd_imin > zdom->uzd_nitems) 5610f9b7bf3SMark Johnston zdom->uzd_imin = zdom->uzd_nitems; 562bb15d1c7SGleb Smirnoff zone->uz_bkt_count -= bucket->ub_cnt; 563d4665eaaSJeff Roberson ZONE_UNLOCK(zone); 564d4665eaaSJeff Roberson if (dtor) 565d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 566d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], zone->uz_size, 567d4665eaaSJeff Roberson NULL, SKIP_NONE); 568d4665eaaSJeff Roberson 5690f9b7bf3SMark Johnston return (bucket); 5700f9b7bf3SMark Johnston } 5710f9b7bf3SMark Johnston 57208cfa56eSMark Johnston /* 57308cfa56eSMark Johnston * Insert a full bucket into the specified cache. The "ws" parameter indicates 57408cfa56eSMark Johnston * whether the bucket's contents should be counted as part of the zone's working 57508cfa56eSMark Johnston * set. 57608cfa56eSMark Johnston */ 5770f9b7bf3SMark Johnston static void 5780f9b7bf3SMark Johnston zone_put_bucket(uma_zone_t zone, uma_zone_domain_t zdom, uma_bucket_t bucket, 5790f9b7bf3SMark Johnston const bool ws) 5800f9b7bf3SMark Johnston { 5810f9b7bf3SMark Johnston 5820f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 58308034d10SKonstantin Belousov KASSERT(!ws || zone->uz_bkt_count < zone->uz_bkt_max, 58408034d10SKonstantin Belousov ("%s: zone %p overflow", __func__, zone)); 5850f9b7bf3SMark Johnston 586dc3915c8SJeff Roberson STAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link); 5870f9b7bf3SMark Johnston zdom->uzd_nitems += bucket->ub_cnt; 5880f9b7bf3SMark Johnston if (ws && zdom->uzd_imax < zdom->uzd_nitems) 5890f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_nitems; 590bb15d1c7SGleb Smirnoff zone->uz_bkt_count += bucket->ub_cnt; 5910f9b7bf3SMark Johnston } 5920f9b7bf3SMark Johnston 593376b1ba3SJeff Roberson /* Pops an item out of a per-cpu cache bucket. */ 594376b1ba3SJeff Roberson static inline void * 595376b1ba3SJeff Roberson cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket) 596376b1ba3SJeff Roberson { 597376b1ba3SJeff Roberson void *item; 598376b1ba3SJeff Roberson 599376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 600376b1ba3SJeff Roberson 601376b1ba3SJeff Roberson bucket->ucb_cnt--; 602376b1ba3SJeff Roberson item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt]; 603376b1ba3SJeff Roberson #ifdef INVARIANTS 604376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL; 605376b1ba3SJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 606376b1ba3SJeff Roberson #endif 607376b1ba3SJeff Roberson cache->uc_allocs++; 608376b1ba3SJeff Roberson 609376b1ba3SJeff Roberson return (item); 610376b1ba3SJeff Roberson } 611376b1ba3SJeff Roberson 612376b1ba3SJeff Roberson /* Pushes an item into a per-cpu cache bucket. */ 613376b1ba3SJeff Roberson static inline void 614376b1ba3SJeff Roberson cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item) 615376b1ba3SJeff Roberson { 616376b1ba3SJeff Roberson 617376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 618376b1ba3SJeff Roberson KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL, 619376b1ba3SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 620376b1ba3SJeff Roberson 621376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item; 622376b1ba3SJeff Roberson bucket->ucb_cnt++; 623376b1ba3SJeff Roberson cache->uc_frees++; 624376b1ba3SJeff Roberson } 625376b1ba3SJeff Roberson 626376b1ba3SJeff Roberson /* 627376b1ba3SJeff Roberson * Unload a UMA bucket from a per-cpu cache. 628376b1ba3SJeff Roberson */ 629376b1ba3SJeff Roberson static inline uma_bucket_t 630376b1ba3SJeff Roberson cache_bucket_unload(uma_cache_bucket_t bucket) 631376b1ba3SJeff Roberson { 632376b1ba3SJeff Roberson uma_bucket_t b; 633376b1ba3SJeff Roberson 634376b1ba3SJeff Roberson b = bucket->ucb_bucket; 635376b1ba3SJeff Roberson if (b != NULL) { 636376b1ba3SJeff Roberson MPASS(b->ub_entries == bucket->ucb_entries); 637376b1ba3SJeff Roberson b->ub_cnt = bucket->ucb_cnt; 638376b1ba3SJeff Roberson bucket->ucb_bucket = NULL; 639376b1ba3SJeff Roberson bucket->ucb_entries = bucket->ucb_cnt = 0; 640376b1ba3SJeff Roberson } 641376b1ba3SJeff Roberson 642376b1ba3SJeff Roberson return (b); 643376b1ba3SJeff Roberson } 644376b1ba3SJeff Roberson 645376b1ba3SJeff Roberson static inline uma_bucket_t 646376b1ba3SJeff Roberson cache_bucket_unload_alloc(uma_cache_t cache) 647376b1ba3SJeff Roberson { 648376b1ba3SJeff Roberson 649376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_allocbucket)); 650376b1ba3SJeff Roberson } 651376b1ba3SJeff Roberson 652376b1ba3SJeff Roberson static inline uma_bucket_t 653376b1ba3SJeff Roberson cache_bucket_unload_free(uma_cache_t cache) 654376b1ba3SJeff Roberson { 655376b1ba3SJeff Roberson 656376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_freebucket)); 657376b1ba3SJeff Roberson } 658376b1ba3SJeff Roberson 659376b1ba3SJeff Roberson static inline uma_bucket_t 660376b1ba3SJeff Roberson cache_bucket_unload_cross(uma_cache_t cache) 661376b1ba3SJeff Roberson { 662376b1ba3SJeff Roberson 663376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_crossbucket)); 664376b1ba3SJeff Roberson } 665376b1ba3SJeff Roberson 666376b1ba3SJeff Roberson /* 667376b1ba3SJeff Roberson * Load a bucket into a per-cpu cache bucket. 668376b1ba3SJeff Roberson */ 669376b1ba3SJeff Roberson static inline void 670376b1ba3SJeff Roberson cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b) 671376b1ba3SJeff Roberson { 672376b1ba3SJeff Roberson 673376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 674376b1ba3SJeff Roberson MPASS(bucket->ucb_bucket == NULL); 675376b1ba3SJeff Roberson 676376b1ba3SJeff Roberson bucket->ucb_bucket = b; 677376b1ba3SJeff Roberson bucket->ucb_cnt = b->ub_cnt; 678376b1ba3SJeff Roberson bucket->ucb_entries = b->ub_entries; 679376b1ba3SJeff Roberson } 680376b1ba3SJeff Roberson 681376b1ba3SJeff Roberson static inline void 682376b1ba3SJeff Roberson cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b) 683376b1ba3SJeff Roberson { 684376b1ba3SJeff Roberson 685376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_allocbucket, b); 686376b1ba3SJeff Roberson } 687376b1ba3SJeff Roberson 688376b1ba3SJeff Roberson static inline void 689376b1ba3SJeff Roberson cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b) 690376b1ba3SJeff Roberson { 691376b1ba3SJeff Roberson 692376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_freebucket, b); 693376b1ba3SJeff Roberson } 694376b1ba3SJeff Roberson 695dfe13344SJeff Roberson #ifdef NUMA 696376b1ba3SJeff Roberson static inline void 697376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b) 698376b1ba3SJeff Roberson { 699376b1ba3SJeff Roberson 700376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_crossbucket, b); 701376b1ba3SJeff Roberson } 702376b1ba3SJeff Roberson #endif 703376b1ba3SJeff Roberson 704376b1ba3SJeff Roberson /* 705376b1ba3SJeff Roberson * Copy and preserve ucb_spare. 706376b1ba3SJeff Roberson */ 707376b1ba3SJeff Roberson static inline void 708376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 709376b1ba3SJeff Roberson { 710376b1ba3SJeff Roberson 711376b1ba3SJeff Roberson b1->ucb_bucket = b2->ucb_bucket; 712376b1ba3SJeff Roberson b1->ucb_entries = b2->ucb_entries; 713376b1ba3SJeff Roberson b1->ucb_cnt = b2->ucb_cnt; 714376b1ba3SJeff Roberson } 715376b1ba3SJeff Roberson 716376b1ba3SJeff Roberson /* 717376b1ba3SJeff Roberson * Swap two cache buckets. 718376b1ba3SJeff Roberson */ 719376b1ba3SJeff Roberson static inline void 720376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 721376b1ba3SJeff Roberson { 722376b1ba3SJeff Roberson struct uma_cache_bucket b3; 723376b1ba3SJeff Roberson 724376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 725376b1ba3SJeff Roberson 726376b1ba3SJeff Roberson cache_bucket_copy(&b3, b1); 727376b1ba3SJeff Roberson cache_bucket_copy(b1, b2); 728376b1ba3SJeff Roberson cache_bucket_copy(b2, &b3); 729376b1ba3SJeff Roberson } 730376b1ba3SJeff Roberson 7312f891cd5SPawel Jakub Dawidek static void 7322f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 7332f891cd5SPawel Jakub Dawidek { 7342f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 7352f891cd5SPawel Jakub Dawidek 7362f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 7372f891cd5SPawel Jakub Dawidek return; 7382f891cd5SPawel Jakub Dawidek 7392f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 7402f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 7412f891cd5SPawel Jakub Dawidek } 7422f891cd5SPawel Jakub Dawidek 74354503a13SJonathan T. Looney static inline void 74454503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone) 74554503a13SJonathan T. Looney { 746e60b2fcbSGleb Smirnoff 747e60b2fcbSGleb Smirnoff if (zone->uz_maxaction.ta_func != NULL) 748e60b2fcbSGleb Smirnoff taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction); 74954503a13SJonathan T. Looney } 75054503a13SJonathan T. Looney 7518355f576SJeff Roberson /* 7528355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 7539643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 7548355f576SJeff Roberson * 7558355f576SJeff Roberson * Arguments: 7568355f576SJeff Roberson * arg Unused 7578355f576SJeff Roberson * 7588355f576SJeff Roberson * Returns: 7598355f576SJeff Roberson * Nothing 7608355f576SJeff Roberson */ 7618355f576SJeff Roberson static void 7628355f576SJeff Roberson uma_timeout(void *unused) 7638355f576SJeff Roberson { 76486bbae32SJeff Roberson bucket_enable(); 76520a4e154SJeff Roberson zone_foreach(zone_timeout, NULL); 7668355f576SJeff Roberson 7678355f576SJeff Roberson /* Reschedule this event */ 7689643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 7698355f576SJeff Roberson } 7708355f576SJeff Roberson 7718355f576SJeff Roberson /* 7720f9b7bf3SMark Johnston * Update the working set size estimate for the zone's bucket cache. 7730f9b7bf3SMark Johnston * The constants chosen here are somewhat arbitrary. With an update period of 7740f9b7bf3SMark Johnston * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the 7750f9b7bf3SMark Johnston * last 100s. 7760f9b7bf3SMark Johnston */ 7770f9b7bf3SMark Johnston static void 7780f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom) 7790f9b7bf3SMark Johnston { 7800f9b7bf3SMark Johnston long wss; 7810f9b7bf3SMark Johnston 7820f9b7bf3SMark Johnston MPASS(zdom->uzd_imax >= zdom->uzd_imin); 7830f9b7bf3SMark Johnston wss = zdom->uzd_imax - zdom->uzd_imin; 7840f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems; 78508cfa56eSMark Johnston zdom->uzd_wss = (4 * wss + zdom->uzd_wss) / 5; 7860f9b7bf3SMark Johnston } 7870f9b7bf3SMark Johnston 7880f9b7bf3SMark Johnston /* 7899643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 7909643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 7918355f576SJeff Roberson * 792e20a199fSJeff Roberson * Returns nothing. 7938355f576SJeff Roberson */ 7948355f576SJeff Roberson static void 79520a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused) 7968355f576SJeff Roberson { 79708034d10SKonstantin Belousov uma_keg_t keg; 7988b987a77SJeff Roberson u_int slabs, pages; 7998355f576SJeff Roberson 80054c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 80108034d10SKonstantin Belousov goto update_wss; 80208034d10SKonstantin Belousov 80308034d10SKonstantin Belousov keg = zone->uz_keg; 8048b987a77SJeff Roberson 8058b987a77SJeff Roberson /* 8068b987a77SJeff Roberson * Hash zones are non-numa by definition so the first domain 8078b987a77SJeff Roberson * is the only one present. 8088b987a77SJeff Roberson */ 8098b987a77SJeff Roberson KEG_LOCK(keg, 0); 8108b987a77SJeff Roberson pages = keg->uk_domain[0].ud_pages; 8118b987a77SJeff Roberson 8128355f576SJeff Roberson /* 813e20a199fSJeff Roberson * Expand the keg hash table. 8148355f576SJeff Roberson * 8158355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 8168355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 8178355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 8188355f576SJeff Roberson */ 8198b987a77SJeff Roberson if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) { 8200aef6126SJeff Roberson struct uma_hash newhash; 8210aef6126SJeff Roberson struct uma_hash oldhash; 8220aef6126SJeff Roberson int ret; 8235300d9ddSJeff Roberson 8240aef6126SJeff Roberson /* 8250aef6126SJeff Roberson * This is so involved because allocating and freeing 826e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 8270aef6126SJeff Roberson * I have to do everything in stages and check for 8280aef6126SJeff Roberson * races. 8290aef6126SJeff Roberson */ 8308b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 8313b2f2cb8SAlexander Motin ret = hash_alloc(&newhash, 1 << fls(slabs)); 8328b987a77SJeff Roberson KEG_LOCK(keg, 0); 8330aef6126SJeff Roberson if (ret) { 834099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 835099a0e58SBosko Milekic oldhash = keg->uk_hash; 836099a0e58SBosko Milekic keg->uk_hash = newhash; 8370aef6126SJeff Roberson } else 8380aef6126SJeff Roberson oldhash = newhash; 8390aef6126SJeff Roberson 8408b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 8410aef6126SJeff Roberson hash_free(&oldhash); 8428b987a77SJeff Roberson goto update_wss; 8430aef6126SJeff Roberson } 8445300d9ddSJeff Roberson } 8458b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 846e20a199fSJeff Roberson 84708034d10SKonstantin Belousov update_wss: 84808cfa56eSMark Johnston ZONE_LOCK(zone); 849bb15d1c7SGleb Smirnoff for (int i = 0; i < vm_ndomains; i++) 8500f9b7bf3SMark Johnston zone_domain_update_wss(&zone->uz_domain[i]); 85108cfa56eSMark Johnston ZONE_UNLOCK(zone); 8528355f576SJeff Roberson } 8538355f576SJeff Roberson 8548355f576SJeff Roberson /* 8555300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 8565300d9ddSJeff Roberson * backing store. 8575300d9ddSJeff Roberson * 8585300d9ddSJeff Roberson * Arguments: 8590aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 8605300d9ddSJeff Roberson * 8615300d9ddSJeff Roberson * Returns: 862763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 8635300d9ddSJeff Roberson */ 86437c84183SPoul-Henning Kamp static int 8653b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 8665300d9ddSJeff Roberson { 86759568a0eSAlexander Motin size_t alloc; 8685300d9ddSJeff Roberson 8693b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 8703b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 8713b2f2cb8SAlexander Motin hash->uh_hashsize = size; 8720aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 8731e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 8745300d9ddSJeff Roberson } else { 8750aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 876e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 877ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 8780aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 8795300d9ddSJeff Roberson } 8800aef6126SJeff Roberson if (hash->uh_slab_hash) { 8810aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 8820aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 8830aef6126SJeff Roberson return (1); 8840aef6126SJeff Roberson } 8855300d9ddSJeff Roberson 8860aef6126SJeff Roberson return (0); 8875300d9ddSJeff Roberson } 8885300d9ddSJeff Roberson 8895300d9ddSJeff Roberson /* 89064f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 89164f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 89264f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 8938355f576SJeff Roberson * 8948355f576SJeff Roberson * Arguments: 8950aef6126SJeff Roberson * oldhash The hash you want to expand 8960aef6126SJeff Roberson * newhash The hash structure for the new table 8978355f576SJeff Roberson * 8988355f576SJeff Roberson * Returns: 8998355f576SJeff Roberson * Nothing 9008355f576SJeff Roberson * 9018355f576SJeff Roberson * Discussion: 9028355f576SJeff Roberson */ 9030aef6126SJeff Roberson static int 9040aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 9058355f576SJeff Roberson { 9061e0701e1SJeff Roberson uma_hash_slab_t slab; 9076929b7d1SPedro F. Giffuni u_int hval; 9086929b7d1SPedro F. Giffuni u_int idx; 9098355f576SJeff Roberson 9100aef6126SJeff Roberson if (!newhash->uh_slab_hash) 9110aef6126SJeff Roberson return (0); 9128355f576SJeff Roberson 9130aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 9140aef6126SJeff Roberson return (0); 9158355f576SJeff Roberson 9168355f576SJeff Roberson /* 9178355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 9188355f576SJeff Roberson * full rehash. 9198355f576SJeff Roberson */ 9208355f576SJeff Roberson 9216929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 9221e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 9231e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 9241e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 9251e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 9261e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 9271e0701e1SJeff Roberson slab, uhs_hlink); 9288355f576SJeff Roberson } 9298355f576SJeff Roberson 9300aef6126SJeff Roberson return (1); 9319c2cd7e5SJeff Roberson } 9329c2cd7e5SJeff Roberson 9335300d9ddSJeff Roberson /* 9345300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 9355300d9ddSJeff Roberson * 9365300d9ddSJeff Roberson * Arguments: 9375300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 9385300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 9395300d9ddSJeff Roberson * 9405300d9ddSJeff Roberson * Returns: 9415300d9ddSJeff Roberson * Nothing 9425300d9ddSJeff Roberson */ 9439c2cd7e5SJeff Roberson static void 9440aef6126SJeff Roberson hash_free(struct uma_hash *hash) 9459c2cd7e5SJeff Roberson { 9460aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 9470aef6126SJeff Roberson return; 9480aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 9490095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 9508355f576SJeff Roberson else 951961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 9528355f576SJeff Roberson } 9538355f576SJeff Roberson 9548355f576SJeff Roberson /* 9558355f576SJeff Roberson * Frees all outstanding items in a bucket 9568355f576SJeff Roberson * 9578355f576SJeff Roberson * Arguments: 9588355f576SJeff Roberson * zone The zone to free to, must be unlocked. 9594bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 9608355f576SJeff Roberson * 9618355f576SJeff Roberson * Returns: 9628355f576SJeff Roberson * Nothing 9638355f576SJeff Roberson */ 9648355f576SJeff Roberson 9658355f576SJeff Roberson static void 9668355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 9678355f576SJeff Roberson { 9680095a784SJeff Roberson int i; 9698355f576SJeff Roberson 9704bd61e19SJeff Roberson if (bucket == NULL || bucket->ub_cnt == 0) 9718355f576SJeff Roberson return; 9728355f576SJeff Roberson 973d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 974d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 975d4665eaaSJeff Roberson smr_wait(zone->uz_smr, bucket->ub_seq); 976d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 977d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], 978d4665eaaSJeff Roberson zone->uz_size, NULL, SKIP_NONE); 979d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 980d4665eaaSJeff Roberson } 9810095a784SJeff Roberson if (zone->uz_fini) 9820095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 9830095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 9840095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 9854bd61e19SJeff Roberson if (zone->uz_max_items > 0) 9864bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 987d4665eaaSJeff Roberson #ifdef INVARIANTS 988d4665eaaSJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt); 989d4665eaaSJeff Roberson #endif 9900095a784SJeff Roberson bucket->ub_cnt = 0; 9918355f576SJeff Roberson } 9928355f576SJeff Roberson 9938355f576SJeff Roberson /* 9948355f576SJeff Roberson * Drains the per cpu caches for a zone. 9958355f576SJeff Roberson * 996727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 9975d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 9985d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 9995d1ae027SRobert Watson * 10008355f576SJeff Roberson * Arguments: 10018355f576SJeff Roberson * zone The zone to drain, must be unlocked. 10028355f576SJeff Roberson * 10038355f576SJeff Roberson * Returns: 10048355f576SJeff Roberson * Nothing 10058355f576SJeff Roberson */ 10068355f576SJeff Roberson static void 10079643769aSJeff Roberson cache_drain(uma_zone_t zone) 10088355f576SJeff Roberson { 10098355f576SJeff Roberson uma_cache_t cache; 1010376b1ba3SJeff Roberson uma_bucket_t bucket; 10118355f576SJeff Roberson int cpu; 10128355f576SJeff Roberson 10138355f576SJeff Roberson /* 10145d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 10155d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 10165d1ae027SRobert Watson * of the caches at this point. 10175d1ae027SRobert Watson * 10185d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 10195d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 10208355f576SJeff Roberson */ 10213aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 10228355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 1023376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 1024376b1ba3SJeff Roberson if (bucket != NULL) { 1025376b1ba3SJeff Roberson bucket_drain(zone, bucket); 1026376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1027376b1ba3SJeff Roberson } 1028376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 1029376b1ba3SJeff Roberson if (bucket != NULL) { 1030376b1ba3SJeff Roberson bucket_drain(zone, bucket); 1031376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1032376b1ba3SJeff Roberson } 1033376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 1034376b1ba3SJeff Roberson if (bucket != NULL) { 1035376b1ba3SJeff Roberson bucket_drain(zone, bucket); 1036376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1037376b1ba3SJeff Roberson } 1038d56368d7SBosko Milekic } 103908cfa56eSMark Johnston bucket_cache_reclaim(zone, true); 1040aaa8bb16SJeff Roberson } 1041aaa8bb16SJeff Roberson 1042a2de44abSAlexander Motin static void 104320a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 1044a2de44abSAlexander Motin { 1045a2de44abSAlexander Motin 1046a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1047a2de44abSAlexander Motin return; 1048a2de44abSAlexander Motin 1049a2de44abSAlexander Motin ZONE_LOCK(zone); 105020a4e154SJeff Roberson zone->uz_bucket_size = 105120a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 1052a2de44abSAlexander Motin ZONE_UNLOCK(zone); 1053a2de44abSAlexander Motin } 1054a2de44abSAlexander Motin 1055a2de44abSAlexander Motin static void 105620a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 1057a2de44abSAlexander Motin { 1058a2de44abSAlexander Motin uma_cache_t cache; 1059c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 1060ab3185d1SJeff Roberson int domain; 1061a2de44abSAlexander Motin 1062a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1063a2de44abSAlexander Motin return; 1064a2de44abSAlexander Motin 1065c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1066a2de44abSAlexander Motin ZONE_LOCK(zone); 1067a2de44abSAlexander Motin critical_enter(); 1068dfe13344SJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 1069ab3185d1SJeff Roberson domain = PCPU_GET(domain); 1070ab3185d1SJeff Roberson else 1071ab3185d1SJeff Roberson domain = 0; 1072a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1073376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1074376b1ba3SJeff Roberson if (b1 != NULL && b1->ub_cnt != 0) { 1075376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b1, false); 1076376b1ba3SJeff Roberson b1 = NULL; 1077a2de44abSAlexander Motin } 1078d4665eaaSJeff Roberson 1079d4665eaaSJeff Roberson /* 1080d4665eaaSJeff Roberson * Don't flush SMR zone buckets. This leaves the zone without a 1081d4665eaaSJeff Roberson * bucket and forces every free to synchronize(). 1082d4665eaaSJeff Roberson */ 1083d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 1084d4665eaaSJeff Roberson goto out; 1085376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1086376b1ba3SJeff Roberson if (b2 != NULL && b2->ub_cnt != 0) { 1087376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b2, false); 1088376b1ba3SJeff Roberson b2 = NULL; 1089a2de44abSAlexander Motin } 1090376b1ba3SJeff Roberson b3 = cache_bucket_unload_cross(cache); 1091d4665eaaSJeff Roberson 1092d4665eaaSJeff Roberson out: 1093a2de44abSAlexander Motin critical_exit(); 1094a2de44abSAlexander Motin ZONE_UNLOCK(zone); 10958a8d9d14SAlexander Motin if (b1) 10968a8d9d14SAlexander Motin bucket_free(zone, b1, NULL); 10978a8d9d14SAlexander Motin if (b2) 10988a8d9d14SAlexander Motin bucket_free(zone, b2, NULL); 1099c1685086SJeff Roberson if (b3) { 1100c1685086SJeff Roberson bucket_drain(zone, b3); 1101c1685086SJeff Roberson bucket_free(zone, b3, NULL); 1102c1685086SJeff Roberson } 1103a2de44abSAlexander Motin } 1104a2de44abSAlexander Motin 1105a2de44abSAlexander Motin /* 1106a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1107a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1108a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1109a2de44abSAlexander Motin * to safely access their cache buckets. 1110a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1111a2de44abSAlexander Motin */ 1112a2de44abSAlexander Motin static void 111308cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1114a2de44abSAlexander Motin { 1115a2de44abSAlexander Motin int cpu; 1116a2de44abSAlexander Motin 1117a2de44abSAlexander Motin /* 1118727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1119a2de44abSAlexander Motin */ 1120a2de44abSAlexander Motin if (zone) 112120a4e154SJeff Roberson cache_shrink(zone, NULL); 1122a2de44abSAlexander Motin else 112320a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1124a2de44abSAlexander Motin 1125a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1126a2de44abSAlexander Motin thread_lock(curthread); 1127a2de44abSAlexander Motin sched_bind(curthread, cpu); 1128a2de44abSAlexander Motin thread_unlock(curthread); 1129a2de44abSAlexander Motin 1130a2de44abSAlexander Motin if (zone) 113120a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1132a2de44abSAlexander Motin else 113320a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1134a2de44abSAlexander Motin } 1135a2de44abSAlexander Motin thread_lock(curthread); 1136a2de44abSAlexander Motin sched_unbind(curthread); 1137a2de44abSAlexander Motin thread_unlock(curthread); 1138a2de44abSAlexander Motin } 1139a2de44abSAlexander Motin 1140aaa8bb16SJeff Roberson /* 114108cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 114208cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 114308cfa56eSMark Johnston * estimated working set size. 1144aaa8bb16SJeff Roberson */ 1145aaa8bb16SJeff Roberson static void 114608cfa56eSMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain) 1147aaa8bb16SJeff Roberson { 1148ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1149aaa8bb16SJeff Roberson uma_bucket_t bucket; 115008cfa56eSMark Johnston long target, tofree; 1151ab3185d1SJeff Roberson int i; 11528355f576SJeff Roberson 1153ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 115491d947bfSJeff Roberson /* 115591d947bfSJeff Roberson * The cross bucket is partially filled and not part of 115691d947bfSJeff Roberson * the item count. Reclaim it individually here. 115791d947bfSJeff Roberson */ 1158ab3185d1SJeff Roberson zdom = &zone->uz_domain[i]; 115991d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 116091d947bfSJeff Roberson bucket = zdom->uzd_cross; 116191d947bfSJeff Roberson zdom->uzd_cross = NULL; 116291d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 116391d947bfSJeff Roberson if (bucket != NULL) { 116491d947bfSJeff Roberson bucket_drain(zone, bucket); 116591d947bfSJeff Roberson bucket_free(zone, bucket, NULL); 116691d947bfSJeff Roberson } 116791d947bfSJeff Roberson 116891d947bfSJeff Roberson /* 116991d947bfSJeff Roberson * Shrink the zone bucket size to ensure that the per-CPU caches 117091d947bfSJeff Roberson * don't grow too large. 117191d947bfSJeff Roberson */ 117291d947bfSJeff Roberson ZONE_LOCK(zone); 117391d947bfSJeff Roberson if (i == 0 && zone->uz_bucket_size > zone->uz_bucket_size_min) 117491d947bfSJeff Roberson zone->uz_bucket_size--; 117508cfa56eSMark Johnston 117608cfa56eSMark Johnston /* 117708cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 117808cfa56eSMark Johnston * this bucket cache is empty. Otherwise, we reclaim items in 117908cfa56eSMark Johnston * excess of the zone's estimated working set size. If the 118008cfa56eSMark Johnston * difference nitems - imin is larger than the WSS estimate, 118108cfa56eSMark Johnston * then the estimate will grow at the end of this interval and 118208cfa56eSMark Johnston * we ignore the historical average. 118308cfa56eSMark Johnston */ 118408cfa56eSMark Johnston target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems - 118508cfa56eSMark Johnston zdom->uzd_imin); 118608cfa56eSMark Johnston while (zdom->uzd_nitems > target) { 1187dc3915c8SJeff Roberson bucket = STAILQ_FIRST(&zdom->uzd_buckets); 118808cfa56eSMark Johnston if (bucket == NULL) 118908cfa56eSMark Johnston break; 119008cfa56eSMark Johnston tofree = bucket->ub_cnt; 1191dc3915c8SJeff Roberson STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link); 119208cfa56eSMark Johnston zdom->uzd_nitems -= tofree; 119308cfa56eSMark Johnston 119408cfa56eSMark Johnston /* 119508cfa56eSMark Johnston * Shift the bounds of the current WSS interval to avoid 119608cfa56eSMark Johnston * perturbing the estimate. 119708cfa56eSMark Johnston */ 119808cfa56eSMark Johnston zdom->uzd_imax -= lmin(zdom->uzd_imax, tofree); 119908cfa56eSMark Johnston zdom->uzd_imin -= lmin(zdom->uzd_imin, tofree); 120008cfa56eSMark Johnston 12018355f576SJeff Roberson ZONE_UNLOCK(zone); 12028355f576SJeff Roberson bucket_drain(zone, bucket); 12036fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 12048355f576SJeff Roberson ZONE_LOCK(zone); 12058355f576SJeff Roberson } 120691d947bfSJeff Roberson ZONE_UNLOCK(zone); 1207ab3185d1SJeff Roberson } 12088355f576SJeff Roberson } 1209fc03d22bSJeff Roberson 1210fc03d22bSJeff Roberson static void 1211fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1212fc03d22bSJeff Roberson { 1213fc03d22bSJeff Roberson uint8_t *mem; 1214fc03d22bSJeff Roberson int i; 1215fc03d22bSJeff Roberson uint8_t flags; 1216fc03d22bSJeff Roberson 12171431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 12181431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 12191431a748SGleb Smirnoff 12201e0701e1SJeff Roberson mem = slab_data(slab, keg); 1221fc03d22bSJeff Roberson flags = slab->us_flags; 1222fc03d22bSJeff Roberson i = start; 1223fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 1224fc03d22bSJeff Roberson for (i--; i > -1; i--) 1225c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1226c5deaf04SGleb Smirnoff /* 1227c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1228c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1229c5deaf04SGleb Smirnoff * which executed trash_dtor. 1230c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1231c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1232c5deaf04SGleb Smirnoff * invocations. 1233c5deaf04SGleb Smirnoff */ 12341e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1235c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1236c5deaf04SGleb Smirnoff #endif 12371e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1238fc03d22bSJeff Roberson } 123954c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 12409b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab), 12419b8db4d0SRyan Libby NULL, SKIP_NONE); 1242fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 12432e47807cSJeff Roberson uma_total_dec(PAGE_SIZE * keg->uk_ppera); 12448355f576SJeff Roberson } 12458355f576SJeff Roberson 12468355f576SJeff Roberson /* 1247e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 12488355f576SJeff Roberson * the pageout daemon. 12498355f576SJeff Roberson * 1250e20a199fSJeff Roberson * Returns nothing. 12518355f576SJeff Roberson */ 1252e20a199fSJeff Roberson static void 1253e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 12548355f576SJeff Roberson { 12551e183df2SStefan Farfeleder struct slabhead freeslabs = { 0 }; 1256ab3185d1SJeff Roberson uma_domain_t dom; 1257829be516SMark Johnston uma_slab_t slab, tmp; 12588b987a77SJeff Roberson int i, n; 12598355f576SJeff Roberson 12608355f576SJeff Roberson /* 1261e20a199fSJeff Roberson * We don't want to take pages from statically allocated kegs at this 12628355f576SJeff Roberson * time 12638355f576SJeff Roberson */ 1264099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 12658355f576SJeff Roberson return; 12668355f576SJeff Roberson 1267ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 12688b987a77SJeff Roberson CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u", 12698b987a77SJeff Roberson keg->uk_name, keg, i, dom->ud_free); 12708b987a77SJeff Roberson n = 0; 1271ab3185d1SJeff Roberson dom = &keg->uk_domain[i]; 12728b987a77SJeff Roberson KEG_LOCK(keg, i); 1273ab3185d1SJeff Roberson LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) { 127454c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 12751e0701e1SJeff Roberson UMA_HASH_REMOVE(&keg->uk_hash, slab); 12768b987a77SJeff Roberson n++; 12778b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 12781e0701e1SJeff Roberson LIST_INSERT_HEAD(&freeslabs, slab, us_link); 1279713deb36SJeff Roberson } 12808b987a77SJeff Roberson dom->ud_pages -= n * keg->uk_ppera; 12818b987a77SJeff Roberson dom->ud_free -= n * keg->uk_ipers; 12828b987a77SJeff Roberson KEG_UNLOCK(keg, i); 1283ab3185d1SJeff Roberson } 1284ab3185d1SJeff Roberson 12851e0701e1SJeff Roberson while ((slab = LIST_FIRST(&freeslabs)) != NULL) { 12861e0701e1SJeff Roberson LIST_REMOVE(slab, us_link); 12871645995bSKirk McKusick keg_free_slab(keg, slab, keg->uk_ipers); 12888355f576SJeff Roberson } 12898355f576SJeff Roberson } 12908355f576SJeff Roberson 1291e20a199fSJeff Roberson static void 129208cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain) 1293e20a199fSJeff Roberson { 1294e20a199fSJeff Roberson 12958355f576SJeff Roberson /* 1296e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 1297e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 1298e20a199fSJeff Roberson * is the only call that knows the structure will still be available 1299e20a199fSJeff Roberson * when it wakes up. 1300e20a199fSJeff Roberson */ 1301e20a199fSJeff Roberson ZONE_LOCK(zone); 130208cfa56eSMark Johnston while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) { 1303e20a199fSJeff Roberson if (waitok == M_NOWAIT) 1304e20a199fSJeff Roberson goto out; 1305727c6918SJeff Roberson msleep(zone, &zone->uz_lock, PVM, "zonedrain", 1); 1306e20a199fSJeff Roberson } 130708cfa56eSMark Johnston zone->uz_flags |= UMA_ZFLAG_RECLAIMING; 1308e20a199fSJeff Roberson ZONE_UNLOCK(zone); 130991d947bfSJeff Roberson bucket_cache_reclaim(zone, drain); 131008cfa56eSMark Johnston 1311e20a199fSJeff Roberson /* 1312e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 1313111fbcd5SBryan Venteicher * we're running. Normally the uma_rwlock would protect us but we 1314e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 1315e20a199fSJeff Roberson */ 131608034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1317bb15d1c7SGleb Smirnoff keg_drain(zone->uz_keg); 1318e20a199fSJeff Roberson ZONE_LOCK(zone); 131908cfa56eSMark Johnston zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING; 1320e20a199fSJeff Roberson wakeup(zone); 1321e20a199fSJeff Roberson out: 1322e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1323e20a199fSJeff Roberson } 1324e20a199fSJeff Roberson 132508cfa56eSMark Johnston static void 132620a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused) 1327e20a199fSJeff Roberson { 1328e20a199fSJeff Roberson 132908cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, true); 133008cfa56eSMark Johnston } 133108cfa56eSMark Johnston 133208cfa56eSMark Johnston static void 133320a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused) 133408cfa56eSMark Johnston { 133508cfa56eSMark Johnston 133608cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, false); 1337e20a199fSJeff Roberson } 1338e20a199fSJeff Roberson 1339e20a199fSJeff Roberson /* 13408b987a77SJeff Roberson * Allocate a new slab for a keg and inserts it into the partial slab list. 13418b987a77SJeff Roberson * The keg should be unlocked on entry. If the allocation succeeds it will 13428b987a77SJeff Roberson * be locked on return. 13438355f576SJeff Roberson * 13448355f576SJeff Roberson * Arguments: 134586220393SMark Johnston * flags Wait flags for the item initialization routine 134686220393SMark Johnston * aflags Wait flags for the slab allocation 13478355f576SJeff Roberson * 13488355f576SJeff Roberson * Returns: 13498355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 13508355f576SJeff Roberson * caller specified M_NOWAIT. 13518355f576SJeff Roberson */ 13528355f576SJeff Roberson static uma_slab_t 135386220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 135486220393SMark Johnston int aflags) 13558355f576SJeff Roberson { 13568b987a77SJeff Roberson uma_domain_t dom; 1357e20a199fSJeff Roberson uma_alloc allocf; 1358099a0e58SBosko Milekic uma_slab_t slab; 13592e47807cSJeff Roberson unsigned long size; 136085dcf349SGleb Smirnoff uint8_t *mem; 136186220393SMark Johnston uint8_t sflags; 13628355f576SJeff Roberson int i; 13638355f576SJeff Roberson 1364ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1365ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1366a553d4b8SJeff Roberson 13678b987a77SJeff Roberson allocf = keg->uk_allocf; 1368194a979eSMark Johnston slab = NULL; 1369194a979eSMark Johnston mem = NULL; 137054c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 13719b8db4d0SRyan Libby uma_hash_slab_t hslab; 13729b8db4d0SRyan Libby hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL, 13739b8db4d0SRyan Libby domain, aflags); 13749b8db4d0SRyan Libby if (hslab == NULL) 1375727c6918SJeff Roberson goto fail; 13769b8db4d0SRyan Libby slab = &hslab->uhs_slab; 1377a553d4b8SJeff Roberson } 1378a553d4b8SJeff Roberson 13793370c5bfSJeff Roberson /* 13803370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 13813370c5bfSJeff Roberson * first time they are added to a zone. 13823370c5bfSJeff Roberson * 13833370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 13843370c5bfSJeff Roberson */ 13853370c5bfSJeff Roberson 1386099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 138786220393SMark Johnston aflags |= M_ZERO; 13883370c5bfSJeff Roberson else 138986220393SMark Johnston aflags &= ~M_ZERO; 13903370c5bfSJeff Roberson 1391263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 139286220393SMark Johnston aflags |= M_NODUMP; 1393263811f7SKip Macy 1394e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1395194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 139686220393SMark Johnston mem = allocf(zone, size, domain, &sflags, aflags); 1397a553d4b8SJeff Roberson if (mem == NULL) { 139854c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 13999b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), 14009b8db4d0SRyan Libby slab_tohashslab(slab), NULL, SKIP_NONE); 1401727c6918SJeff Roberson goto fail; 1402a553d4b8SJeff Roberson } 14032e47807cSJeff Roberson uma_total_inc(size); 14048355f576SJeff Roberson 14058b987a77SJeff Roberson /* For HASH zones all pages go to the same uma_domain. */ 140654c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 14078b987a77SJeff Roberson domain = 0; 14088b987a77SJeff Roberson 14095c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 141054c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) 1411099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 14121e0701e1SJeff Roberson else 14139b8db4d0SRyan Libby slab_tohashslab(slab)->uhs_data = mem; 14145c0e403bSJeff Roberson 141554c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_VTOSLAB) 1416099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1417584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1418584061b4SJeff Roberson zone, slab); 14198355f576SJeff Roberson 1420099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 142186220393SMark Johnston slab->us_flags = sflags; 1422ab3185d1SJeff Roberson slab->us_domain = domain; 14238b987a77SJeff Roberson 14249b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1425ef72505eSJeff Roberson #ifdef INVARIANTS 1426815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1427ef72505eSJeff Roberson #endif 1428099a0e58SBosko Milekic 1429b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1430099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 14311e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 143286220393SMark Johnston keg->uk_size, flags) != 0) 1433b23f72e9SBrian Feldman break; 1434b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1435fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1436727c6918SJeff Roberson goto fail; 1437b23f72e9SBrian Feldman } 1438b23f72e9SBrian Feldman } 14398b987a77SJeff Roberson KEG_LOCK(keg, domain); 14405c0e403bSJeff Roberson 14411431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 14421431a748SGleb Smirnoff slab, keg->uk_name, keg); 14431431a748SGleb Smirnoff 144454c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 1445099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 14468355f576SJeff Roberson 14478b987a77SJeff Roberson /* 14488b987a77SJeff Roberson * If we got a slab here it's safe to mark it partially used 14498b987a77SJeff Roberson * and return. We assume that the caller is going to remove 14508b987a77SJeff Roberson * at least one item. 14518b987a77SJeff Roberson */ 14528b987a77SJeff Roberson dom = &keg->uk_domain[domain]; 14538b987a77SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 14548b987a77SJeff Roberson dom->ud_pages += keg->uk_ppera; 14558b987a77SJeff Roberson dom->ud_free += keg->uk_ipers; 14568355f576SJeff Roberson 14578355f576SJeff Roberson return (slab); 1458727c6918SJeff Roberson 1459727c6918SJeff Roberson fail: 1460727c6918SJeff Roberson return (NULL); 14618355f576SJeff Roberson } 14628355f576SJeff Roberson 14638355f576SJeff Roberson /* 1464009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1465009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1466009b6fcbSJeff Roberson * the VM is ready. 1467009b6fcbSJeff Roberson */ 1468009b6fcbSJeff Roberson static void * 1469ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1470ab3185d1SJeff Roberson int wait) 1471009b6fcbSJeff Roberson { 1472a81c400eSJeff Roberson vm_paddr_t pa; 1473a81c400eSJeff Roberson vm_page_t m; 1474ac0a6fd0SGleb Smirnoff void *mem; 1475ac0a6fd0SGleb Smirnoff int pages; 1476a81c400eSJeff Roberson int i; 1477099a0e58SBosko Milekic 1478f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1479f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1480a81c400eSJeff Roberson 1481f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1482a81c400eSJeff Roberson m = vm_page_alloc_contig_domain(NULL, 0, domain, 1483a81c400eSJeff Roberson malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages, 1484a81c400eSJeff Roberson (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT); 1485a81c400eSJeff Roberson if (m == NULL) 1486a81c400eSJeff Roberson return (NULL); 1487a81c400eSJeff Roberson 1488a81c400eSJeff Roberson pa = VM_PAGE_TO_PHYS(m); 1489a81c400eSJeff Roberson for (i = 0; i < pages; i++, pa += PAGE_SIZE) { 1490a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1491a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1492a81c400eSJeff Roberson if ((wait & M_NODUMP) == 0) 1493a81c400eSJeff Roberson dump_add_page(pa); 1494a81c400eSJeff Roberson #endif 1495a81c400eSJeff Roberson } 1496a81c400eSJeff Roberson /* Allocate KVA and indirectly advance bootmem. */ 1497a81c400eSJeff Roberson mem = (void *)pmap_map(&bootmem, m->phys_addr, 1498a81c400eSJeff Roberson m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE); 1499a81c400eSJeff Roberson if ((wait & M_ZERO) != 0) 1500a81c400eSJeff Roberson bzero(mem, pages * PAGE_SIZE); 1501f7d35785SGleb Smirnoff 1502f7d35785SGleb Smirnoff return (mem); 1503f7d35785SGleb Smirnoff } 1504f7d35785SGleb Smirnoff 1505a81c400eSJeff Roberson static void 1506a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes) 1507a81c400eSJeff Roberson { 1508a81c400eSJeff Roberson vm_offset_t va; 1509a81c400eSJeff Roberson vm_page_t m; 1510a81c400eSJeff Roberson 1511a81c400eSJeff Roberson va = (vm_offset_t)mem; 1512a81c400eSJeff Roberson m = PHYS_TO_VM_PAGE(pmap_kextract(va)); 1513a81c400eSJeff Roberson pmap_remove(kernel_pmap, va, va + bytes); 1514a81c400eSJeff Roberson for (; bytes != 0; bytes -= PAGE_SIZE, m++) { 1515a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1516a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1517a81c400eSJeff Roberson dump_drop_page(VM_PAGE_TO_PHYS(m)); 1518a81c400eSJeff Roberson #endif 1519a81c400eSJeff Roberson vm_page_unwire_noq(m); 1520a81c400eSJeff Roberson vm_page_free(m); 1521a81c400eSJeff Roberson } 1522a81c400eSJeff Roberson } 1523a81c400eSJeff Roberson 1524f7d35785SGleb Smirnoff /* 15258355f576SJeff Roberson * Allocates a number of pages from the system 15268355f576SJeff Roberson * 15278355f576SJeff Roberson * Arguments: 15288355f576SJeff Roberson * bytes The number of bytes requested 15298355f576SJeff Roberson * wait Shall we wait? 15308355f576SJeff Roberson * 15318355f576SJeff Roberson * Returns: 15328355f576SJeff Roberson * A pointer to the alloced memory or possibly 15338355f576SJeff Roberson * NULL if M_NOWAIT is set. 15348355f576SJeff Roberson */ 15358355f576SJeff Roberson static void * 1536ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1537ab3185d1SJeff Roberson int wait) 15388355f576SJeff Roberson { 15398355f576SJeff Roberson void *p; /* Returned page */ 15408355f576SJeff Roberson 15412e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 15429978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 15438355f576SJeff Roberson 15448355f576SJeff Roberson return (p); 15458355f576SJeff Roberson } 15468355f576SJeff Roberson 1547ab3059a8SMatt Macy static void * 1548ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1549ab3059a8SMatt Macy int wait) 1550ab3059a8SMatt Macy { 1551ab3059a8SMatt Macy struct pglist alloctail; 1552ab3059a8SMatt Macy vm_offset_t addr, zkva; 1553ab3059a8SMatt Macy int cpu, flags; 1554ab3059a8SMatt Macy vm_page_t p, p_next; 1555ab3059a8SMatt Macy #ifdef NUMA 1556ab3059a8SMatt Macy struct pcpu *pc; 1557ab3059a8SMatt Macy #endif 1558ab3059a8SMatt Macy 1559ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1560ab3059a8SMatt Macy 1561013072f0SMark Johnston TAILQ_INIT(&alloctail); 1562ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1563013072f0SMark Johnston malloc2vm_flags(wait); 1564013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1565ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1566ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1567ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1568ab3059a8SMatt Macy } else { 1569ab3059a8SMatt Macy #ifndef NUMA 1570ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1571ab3059a8SMatt Macy #else 1572ab3059a8SMatt Macy pc = pcpu_find(cpu); 157320526802SAndrew Gallatin if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain))) 157420526802SAndrew Gallatin p = NULL; 157520526802SAndrew Gallatin else 157620526802SAndrew Gallatin p = vm_page_alloc_domain(NULL, 0, 157720526802SAndrew Gallatin pc->pc_domain, flags); 1578ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1579ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1580ab3059a8SMatt Macy #endif 1581ab3059a8SMatt Macy } 1582ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1583ab3059a8SMatt Macy goto fail; 1584ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1585ab3059a8SMatt Macy } 1586ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1587ab3059a8SMatt Macy goto fail; 1588ab3059a8SMatt Macy zkva = addr; 1589ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1590ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1591ab3059a8SMatt Macy zkva += PAGE_SIZE; 1592ab3059a8SMatt Macy } 1593ab3059a8SMatt Macy return ((void*)addr); 1594ab3059a8SMatt Macy fail: 1595ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 159688ea538aSMark Johnston vm_page_unwire_noq(p); 1597ab3059a8SMatt Macy vm_page_free(p); 1598ab3059a8SMatt Macy } 1599ab3059a8SMatt Macy return (NULL); 1600ab3059a8SMatt Macy } 1601ab3059a8SMatt Macy 16028355f576SJeff Roberson /* 16038355f576SJeff Roberson * Allocates a number of pages from within an object 16048355f576SJeff Roberson * 16058355f576SJeff Roberson * Arguments: 16068355f576SJeff Roberson * bytes The number of bytes requested 16078355f576SJeff Roberson * wait Shall we wait? 16088355f576SJeff Roberson * 16098355f576SJeff Roberson * Returns: 16108355f576SJeff Roberson * A pointer to the alloced memory or possibly 16118355f576SJeff Roberson * NULL if M_NOWAIT is set. 16128355f576SJeff Roberson */ 16138355f576SJeff Roberson static void * 1614ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1615ab3185d1SJeff Roberson int wait) 16168355f576SJeff Roberson { 1617a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1618a4915c21SAttilio Rao u_long npages; 1619b245ac95SAlan Cox vm_offset_t retkva, zkva; 1620a4915c21SAttilio Rao vm_page_t p, p_next; 1621e20a199fSJeff Roberson uma_keg_t keg; 16228355f576SJeff Roberson 1623a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1624bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1625a4915c21SAttilio Rao 1626a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1627a4915c21SAttilio Rao while (npages > 0) { 1628ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 16298d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1630772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1631772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1632a4915c21SAttilio Rao if (p != NULL) { 1633a4915c21SAttilio Rao /* 1634a4915c21SAttilio Rao * Since the page does not belong to an object, its 1635a4915c21SAttilio Rao * listq is unused. 1636a4915c21SAttilio Rao */ 1637a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1638a4915c21SAttilio Rao npages--; 1639a4915c21SAttilio Rao continue; 1640a4915c21SAttilio Rao } 16418355f576SJeff Roberson /* 1642a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1643a4915c21SAttilio Rao * exit. 16448355f576SJeff Roberson */ 1645a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 164688ea538aSMark Johnston vm_page_unwire_noq(p); 1647b245ac95SAlan Cox vm_page_free(p); 1648b245ac95SAlan Cox } 1649a4915c21SAttilio Rao return (NULL); 1650b245ac95SAlan Cox } 16518355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1652a4915c21SAttilio Rao zkva = keg->uk_kva + 1653a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1654a4915c21SAttilio Rao retkva = zkva; 1655a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1656a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1657a4915c21SAttilio Rao zkva += PAGE_SIZE; 1658a4915c21SAttilio Rao } 16598355f576SJeff Roberson 16608355f576SJeff Roberson return ((void *)retkva); 16618355f576SJeff Roberson } 16628355f576SJeff Roberson 16638355f576SJeff Roberson /* 1664*ec0d8280SRyan Libby * Allocate physically contiguous pages. 1665*ec0d8280SRyan Libby */ 1666*ec0d8280SRyan Libby static void * 1667*ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1668*ec0d8280SRyan Libby int wait) 1669*ec0d8280SRyan Libby { 1670*ec0d8280SRyan Libby 1671*ec0d8280SRyan Libby *pflag = UMA_SLAB_KERNEL; 1672*ec0d8280SRyan Libby return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain), 1673*ec0d8280SRyan Libby bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT)); 1674*ec0d8280SRyan Libby } 1675*ec0d8280SRyan Libby 1676*ec0d8280SRyan Libby /* 16778355f576SJeff Roberson * Frees a number of pages to the system 16788355f576SJeff Roberson * 16798355f576SJeff Roberson * Arguments: 16808355f576SJeff Roberson * mem A pointer to the memory to be freed 16818355f576SJeff Roberson * size The size of the memory being freed 16828355f576SJeff Roberson * flags The original p->us_flags field 16838355f576SJeff Roberson * 16848355f576SJeff Roberson * Returns: 16858355f576SJeff Roberson * Nothing 16868355f576SJeff Roberson */ 16878355f576SJeff Roberson static void 1688f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 16898355f576SJeff Roberson { 16903370c5bfSJeff Roberson 1691a81c400eSJeff Roberson if ((flags & UMA_SLAB_BOOT) != 0) { 1692a81c400eSJeff Roberson startup_free(mem, size); 1693a81c400eSJeff Roberson return; 1694a81c400eSJeff Roberson } 1695a81c400eSJeff Roberson 1696*ec0d8280SRyan Libby KASSERT((flags & UMA_SLAB_KERNEL) != 0, 1697*ec0d8280SRyan Libby ("UMA: page_free used with invalid flags %x", flags)); 16988355f576SJeff Roberson 169949bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 17008355f576SJeff Roberson } 17018355f576SJeff Roberson 17028355f576SJeff Roberson /* 1703ab3059a8SMatt Macy * Frees pcpu zone allocations 1704ab3059a8SMatt Macy * 1705ab3059a8SMatt Macy * Arguments: 1706ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1707ab3059a8SMatt Macy * size The size of the memory being freed 1708ab3059a8SMatt Macy * flags The original p->us_flags field 1709ab3059a8SMatt Macy * 1710ab3059a8SMatt Macy * Returns: 1711ab3059a8SMatt Macy * Nothing 1712ab3059a8SMatt Macy */ 1713ab3059a8SMatt Macy static void 1714ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1715ab3059a8SMatt Macy { 1716ab3059a8SMatt Macy vm_offset_t sva, curva; 1717ab3059a8SMatt Macy vm_paddr_t paddr; 1718ab3059a8SMatt Macy vm_page_t m; 1719ab3059a8SMatt Macy 1720ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 17215ba16cf3SRyan Libby 17225ba16cf3SRyan Libby if ((flags & UMA_SLAB_BOOT) != 0) { 17235ba16cf3SRyan Libby startup_free(mem, size); 17245ba16cf3SRyan Libby return; 17255ba16cf3SRyan Libby } 17265ba16cf3SRyan Libby 1727ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1728ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1729ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1730ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 173188ea538aSMark Johnston vm_page_unwire_noq(m); 1732ab3059a8SMatt Macy vm_page_free(m); 1733ab3059a8SMatt Macy } 1734ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1735ab3059a8SMatt Macy kva_free(sva, size); 1736ab3059a8SMatt Macy } 1737ab3059a8SMatt Macy 1738ab3059a8SMatt Macy 1739ab3059a8SMatt Macy /* 17408355f576SJeff Roberson * Zero fill initializer 17418355f576SJeff Roberson * 17428355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 17438355f576SJeff Roberson */ 1744b23f72e9SBrian Feldman static int 1745b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 17468355f576SJeff Roberson { 17478355f576SJeff Roberson bzero(mem, size); 1748b23f72e9SBrian Feldman return (0); 17498355f576SJeff Roberson } 17508355f576SJeff Roberson 1751815db204SRyan Libby #ifdef INVARIANTS 1752815db204SRyan Libby struct noslabbits * 1753815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1754815db204SRyan Libby { 1755815db204SRyan Libby 1756815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1757815db204SRyan Libby } 1758815db204SRyan Libby #endif 1759815db204SRyan Libby 17608355f576SJeff Roberson /* 17619b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 17629b78b1f4SJeff Roberson */ 17639b78b1f4SJeff Roberson size_t 17649b78b1f4SJeff Roberson slab_sizeof(int nitems) 17659b78b1f4SJeff Roberson { 17669b78b1f4SJeff Roberson size_t s; 17679b78b1f4SJeff Roberson 1768815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 17699b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 17709b78b1f4SJeff Roberson } 17719b78b1f4SJeff Roberson 17729b78b1f4SJeff Roberson /* 17739b78b1f4SJeff Roberson * Size of memory for embedded slabs (!OFFPAGE). 17749b78b1f4SJeff Roberson */ 17759b78b1f4SJeff Roberson size_t 17769b78b1f4SJeff Roberson slab_space(int nitems) 17779b78b1f4SJeff Roberson { 17789b78b1f4SJeff Roberson return (UMA_SLAB_SIZE - slab_sizeof(nitems)); 17799b78b1f4SJeff Roberson } 17809b78b1f4SJeff Roberson 17814a8b575cSRyan Libby #define UMA_FIXPT_SHIFT 31 17824a8b575cSRyan Libby #define UMA_FRAC_FIXPT(n, d) \ 17834a8b575cSRyan Libby ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d))) 17844a8b575cSRyan Libby #define UMA_FIXPT_PCT(f) \ 17854a8b575cSRyan Libby ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT)) 17864a8b575cSRyan Libby #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100) 17874a8b575cSRyan Libby #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE) 17884a8b575cSRyan Libby 17899b78b1f4SJeff Roberson /* 17904a8b575cSRyan Libby * Compute the number of items that will fit in a slab. If hdr is true, the 17914a8b575cSRyan Libby * item count may be limited to provide space in the slab for an inline slab 17924a8b575cSRyan Libby * header. Otherwise, all slab space will be provided for item storage. 17934a8b575cSRyan Libby */ 17944a8b575cSRyan Libby static u_int 17954a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr) 17964a8b575cSRyan Libby { 17974a8b575cSRyan Libby u_int ipers; 17984a8b575cSRyan Libby u_int padpi; 17994a8b575cSRyan Libby 18004a8b575cSRyan Libby /* The padding between items is not needed after the last item. */ 18014a8b575cSRyan Libby padpi = rsize - size; 18024a8b575cSRyan Libby 18034a8b575cSRyan Libby if (hdr) { 18044a8b575cSRyan Libby /* 18054a8b575cSRyan Libby * Start with the maximum item count and remove items until 18064a8b575cSRyan Libby * the slab header first alongside the allocatable memory. 18074a8b575cSRyan Libby */ 18084a8b575cSRyan Libby for (ipers = MIN(SLAB_MAX_SETSIZE, 18094a8b575cSRyan Libby (slabsize + padpi - slab_sizeof(1)) / rsize); 18104a8b575cSRyan Libby ipers > 0 && 18114a8b575cSRyan Libby ipers * rsize - padpi + slab_sizeof(ipers) > slabsize; 18124a8b575cSRyan Libby ipers--) 18134a8b575cSRyan Libby continue; 18144a8b575cSRyan Libby } else { 18154a8b575cSRyan Libby ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE); 18164a8b575cSRyan Libby } 18174a8b575cSRyan Libby 18184a8b575cSRyan Libby return (ipers); 18194a8b575cSRyan Libby } 18204a8b575cSRyan Libby 18214a8b575cSRyan Libby /* 18224a8b575cSRyan Libby * Compute the number of items that will fit in a slab for a startup zone. 18239b78b1f4SJeff Roberson */ 18249b78b1f4SJeff Roberson int 18259b78b1f4SJeff Roberson slab_ipers(size_t size, int align) 18269b78b1f4SJeff Roberson { 18279b78b1f4SJeff Roberson int rsize; 18289b78b1f4SJeff Roberson 18294a8b575cSRyan Libby rsize = roundup(size, align + 1); /* Assume no CACHESPREAD */ 18304a8b575cSRyan Libby return (slab_ipers_hdr(size, rsize, UMA_SLAB_SIZE, true)); 18319b78b1f4SJeff Roberson } 18329b78b1f4SJeff Roberson 18339b78b1f4SJeff Roberson /* 18344a8b575cSRyan Libby * Determine the format of a uma keg. This determines where the slab header 18354a8b575cSRyan Libby * will be placed (inline or offpage) and calculates ipers, rsize, and ppera. 18368355f576SJeff Roberson * 18378355f576SJeff Roberson * Arguments 1838e20a199fSJeff Roberson * keg The zone we should initialize 18398355f576SJeff Roberson * 18408355f576SJeff Roberson * Returns 18418355f576SJeff Roberson * Nothing 18428355f576SJeff Roberson */ 18438355f576SJeff Roberson static void 18444a8b575cSRyan Libby keg_layout(uma_keg_t keg) 18458355f576SJeff Roberson { 18464a8b575cSRyan Libby u_int alignsize; 18474a8b575cSRyan Libby u_int eff; 18484a8b575cSRyan Libby u_int eff_offpage; 18494a8b575cSRyan Libby u_int format; 18504a8b575cSRyan Libby u_int ipers; 18514a8b575cSRyan Libby u_int ipers_offpage; 18524a8b575cSRyan Libby u_int pages; 1853244f4554SBosko Milekic u_int rsize; 1854a55ebb7cSAndriy Gapon u_int slabsize; 18558355f576SJeff Roberson 18564a8b575cSRyan Libby KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 18574a8b575cSRyan Libby (keg->uk_size <= UMA_PCPU_ALLOC_SIZE && 18584a8b575cSRyan Libby (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0), 18594a8b575cSRyan Libby ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b", 18604a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_size, keg->uk_flags, 18614a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 18624a8b575cSRyan Libby KASSERT((keg->uk_flags & 18634a8b575cSRyan Libby (UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY)) == 0 || 18644a8b575cSRyan Libby (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0, 18654a8b575cSRyan Libby ("%s: incompatible flags 0x%b", __func__, keg->uk_flags, 18664a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 1867e28a647dSGleb Smirnoff 18684a8b575cSRyan Libby alignsize = keg->uk_align + 1; 18694a8b575cSRyan Libby format = 0; 18704a8b575cSRyan Libby ipers = 0; 1871ad97af7eSGleb Smirnoff 1872ef72505eSJeff Roberson /* 1873ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1874ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1875ef72505eSJeff Roberson * allocation bits for we round it up. 1876ef72505eSJeff Roberson */ 18779b8db4d0SRyan Libby rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT); 18784a8b575cSRyan Libby rsize = roundup2(rsize, alignsize); 1879ad97af7eSGleb Smirnoff 18804a8b575cSRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) { 18814a8b575cSRyan Libby slabsize = UMA_PCPU_ALLOC_SIZE; 18824a8b575cSRyan Libby pages = mp_maxid + 1; 18834a8b575cSRyan Libby } else if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) { 18849b78b1f4SJeff Roberson /* 18854a8b575cSRyan Libby * We want one item to start on every align boundary in a page. 18864a8b575cSRyan Libby * To do this we will span pages. We will also extend the item 18874a8b575cSRyan Libby * by the size of align if it is an even multiple of align. 18884a8b575cSRyan Libby * Otherwise, it would fall on the same boundary every time. 18899b78b1f4SJeff Roberson */ 18904a8b575cSRyan Libby if ((rsize & alignsize) == 0) 18914a8b575cSRyan Libby rsize += alignsize; 18924a8b575cSRyan Libby slabsize = rsize * (PAGE_SIZE / alignsize); 18934a8b575cSRyan Libby slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE); 18944a8b575cSRyan Libby slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE); 18954a8b575cSRyan Libby pages = howmany(slabsize, PAGE_SIZE); 18964a8b575cSRyan Libby slabsize = ptoa(pages); 18974a8b575cSRyan Libby } else { 18984a8b575cSRyan Libby /* 18994a8b575cSRyan Libby * Choose a slab size of as many pages as it takes to represent 19004a8b575cSRyan Libby * a single item. We will then try to fit as many additional 19014a8b575cSRyan Libby * items into the slab as possible. At some point, we may want 19024a8b575cSRyan Libby * to increase the slab size for awkward item sizes in order to 19034a8b575cSRyan Libby * increase efficiency. 19044a8b575cSRyan Libby */ 19054a8b575cSRyan Libby pages = howmany(keg->uk_size, PAGE_SIZE); 19064a8b575cSRyan Libby slabsize = ptoa(pages); 19071ca6ed45SGleb Smirnoff } 1908ad97af7eSGleb Smirnoff 19094a8b575cSRyan Libby /* Evaluate an inline slab layout. */ 19104a8b575cSRyan Libby if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0) 19114a8b575cSRyan Libby ipers = slab_ipers_hdr(keg->uk_size, rsize, slabsize, true); 19124a8b575cSRyan Libby 19134a8b575cSRyan Libby /* TODO: vm_page-embedded slab. */ 1914244f4554SBosko Milekic 191520e8e865SBosko Milekic /* 1916244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 191720e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 19186fd34d6fSJeff Roberson * may end up going to the VM for slabs which we do not 19196fd34d6fSJeff Roberson * want to do if we're UMA_ZFLAG_CACHEONLY as a result 19206fd34d6fSJeff Roberson * of UMA_ZONE_VM, which clearly forbids it. 192120e8e865SBosko Milekic */ 19224a8b575cSRyan Libby if ((keg->uk_flags & 19234a8b575cSRyan Libby (UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY)) != 0) { 19244a8b575cSRyan Libby if (ipers == 0) { 19254a8b575cSRyan Libby /* We need an extra page for the slab header. */ 19264a8b575cSRyan Libby pages++; 19274a8b575cSRyan Libby slabsize = ptoa(pages); 19284a8b575cSRyan Libby ipers = slab_ipers_hdr(keg->uk_size, rsize, slabsize, 19294a8b575cSRyan Libby true); 19304a8b575cSRyan Libby } 19314a8b575cSRyan Libby goto out; 193254c5ae80SRyan Libby } 1933244f4554SBosko Milekic 1934ef72505eSJeff Roberson /* 19354a8b575cSRyan Libby * See if using an OFFPAGE slab will improve our efficiency. 19364a8b575cSRyan Libby * Only do this if we are below our efficiency threshold. 1937ef72505eSJeff Roberson * 1938ef72505eSJeff Roberson * XXX We could try growing slabsize to limit max waste as well. 1939ef72505eSJeff Roberson * Historically this was not done because the VM could not 1940ef72505eSJeff Roberson * efficiently handle contiguous allocations. 1941ef72505eSJeff Roberson */ 19424a8b575cSRyan Libby eff = UMA_FRAC_FIXPT(ipers * rsize, slabsize); 19434a8b575cSRyan Libby ipers_offpage = slab_ipers_hdr(keg->uk_size, rsize, slabsize, false); 19444a8b575cSRyan Libby eff_offpage = UMA_FRAC_FIXPT(ipers_offpage * rsize, 19459b8db4d0SRyan Libby slabsize + slabzone(ipers_offpage)->uz_keg->uk_rsize); 19464a8b575cSRyan Libby if (ipers == 0 || (eff < UMA_MIN_EFF && eff < eff_offpage)) { 19474a8b575cSRyan Libby CTR5(KTR_UMA, "UMA decided we need offpage slab headers for " 19484a8b575cSRyan Libby "keg: %s(%p), minimum efficiency allowed = %u%%, " 1949e63a1c2fSRyan Libby "old efficiency = %u%%, offpage efficiency = %u%%", 19504a8b575cSRyan Libby keg->uk_name, keg, UMA_FIXPT_PCT(UMA_MIN_EFF), 19514a8b575cSRyan Libby UMA_FIXPT_PCT(eff), UMA_FIXPT_PCT(eff_offpage)); 19524a8b575cSRyan Libby format = UMA_ZFLAG_OFFPAGE; 19534a8b575cSRyan Libby ipers = ipers_offpage; 19548355f576SJeff Roberson } 1955ad97af7eSGleb Smirnoff 19564a8b575cSRyan Libby out: 19574a8b575cSRyan Libby /* 19584a8b575cSRyan Libby * How do we find the slab header if it is offpage or if not all item 19594a8b575cSRyan Libby * start addresses are in the same page? We could solve the latter 19604a8b575cSRyan Libby * case with vaddr alignment, but we don't. 19614a8b575cSRyan Libby */ 19624a8b575cSRyan Libby if ((format & UMA_ZFLAG_OFFPAGE) != 0 || 19634a8b575cSRyan Libby (ipers - 1) * rsize >= PAGE_SIZE) { 196454c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0) 19654a8b575cSRyan Libby format |= UMA_ZFLAG_HASH; 196654c5ae80SRyan Libby else 19674a8b575cSRyan Libby format |= UMA_ZFLAG_VTOSLAB; 196854c5ae80SRyan Libby } 19694a8b575cSRyan Libby keg->uk_ipers = ipers; 1970e20a199fSJeff Roberson keg->uk_rsize = rsize; 19714a8b575cSRyan Libby keg->uk_flags |= format; 1972e20a199fSJeff Roberson keg->uk_ppera = pages; 1973e63a1c2fSRyan Libby CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u", 19744a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_flags, rsize, ipers, pages); 19754a8b575cSRyan Libby KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 19764a8b575cSRyan Libby ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__, 19774a8b575cSRyan Libby keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize, ipers, 19784a8b575cSRyan Libby pages)); 1979e20a199fSJeff Roberson } 1980e20a199fSJeff Roberson 19818355f576SJeff Roberson /* 1982099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 1983099a0e58SBosko Milekic * the keg onto the global keg list. 19848355f576SJeff Roberson * 19858355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 1986099a0e58SBosko Milekic * udata Actually uma_kctor_args 1987099a0e58SBosko Milekic */ 1988b23f72e9SBrian Feldman static int 1989b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 1990099a0e58SBosko Milekic { 1991099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 1992099a0e58SBosko Milekic uma_keg_t keg = mem; 1993099a0e58SBosko Milekic uma_zone_t zone; 19948b987a77SJeff Roberson int i; 1995099a0e58SBosko Milekic 1996099a0e58SBosko Milekic bzero(keg, size); 1997099a0e58SBosko Milekic keg->uk_size = arg->size; 1998099a0e58SBosko Milekic keg->uk_init = arg->uminit; 1999099a0e58SBosko Milekic keg->uk_fini = arg->fini; 2000099a0e58SBosko Milekic keg->uk_align = arg->align; 20016fd34d6fSJeff Roberson keg->uk_reserve = 0; 2002099a0e58SBosko Milekic keg->uk_flags = arg->flags; 2003099a0e58SBosko Milekic 2004099a0e58SBosko Milekic /* 2005194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 2006dfe13344SJeff Roberson * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which 2007dfe13344SJeff Roberson * case the iterator is never run. 2008194a979eSMark Johnston */ 2009194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 2010194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 2011194a979eSMark Johnston 2012194a979eSMark Johnston /* 2013099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 2014099a0e58SBosko Milekic */ 2015099a0e58SBosko Milekic zone = arg->zone; 2016e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 2017099a0e58SBosko Milekic 2018099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_VM) 2019099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 2020099a0e58SBosko Milekic 2021099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 2022099a0e58SBosko Milekic keg->uk_init = zero_init; 2023099a0e58SBosko Milekic 2024cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 202554c5ae80SRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 2026e20a199fSJeff Roberson 202754c5ae80SRyan Libby #ifndef SMP 2028ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 2029ad97af7eSGleb Smirnoff #endif 2030ad97af7eSGleb Smirnoff 20314a8b575cSRyan Libby keg_layout(keg); 2032099a0e58SBosko Milekic 20338b987a77SJeff Roberson /* 2034dfe13344SJeff Roberson * Use a first-touch NUMA policy for all kegs that pmap_extract() 2035dfe13344SJeff Roberson * will work on with the exception of critical VM structures 2036dfe13344SJeff Roberson * necessary for paging. 2037dfe13344SJeff Roberson * 2038dfe13344SJeff Roberson * Zones may override the default by specifying either. 20398b987a77SJeff Roberson */ 2040dfe13344SJeff Roberson #ifdef NUMA 2041dfe13344SJeff Roberson if ((keg->uk_flags & 204254c5ae80SRyan Libby (UMA_ZFLAG_HASH | UMA_ZONE_VM | UMA_ZONE_ROUNDROBIN)) == 0) 2043dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_FIRSTTOUCH; 2044dfe13344SJeff Roberson else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2045dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_ROUNDROBIN; 20468b987a77SJeff Roberson #endif 20478b987a77SJeff Roberson 2048099a0e58SBosko Milekic /* 2049099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 2050099a0e58SBosko Milekic * startup cache until the vm is ready. 2051099a0e58SBosko Milekic */ 205277e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2053a81c400eSJeff Roberson if (keg->uk_ppera == 1) 205477e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 2055a81c400eSJeff Roberson else 20568cd02d00SAlan Cox #endif 2057a81c400eSJeff Roberson if (booted < BOOT_KVA) 2058a81c400eSJeff Roberson keg->uk_allocf = startup_alloc; 2059ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 2060ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 2061*ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1) 2062*ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 206377e19437SGleb Smirnoff else 206477e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 206577e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 206677e19437SGleb Smirnoff if (keg->uk_ppera == 1) 206777e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 206877e19437SGleb Smirnoff else 206977e19437SGleb Smirnoff #endif 2070ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 2071ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 2072ab3059a8SMatt Macy else 207377e19437SGleb Smirnoff keg->uk_freef = page_free; 2074099a0e58SBosko Milekic 2075099a0e58SBosko Milekic /* 20768b987a77SJeff Roberson * Initialize keg's locks. 2077099a0e58SBosko Milekic */ 20788b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 20798b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 2080099a0e58SBosko Milekic 2081099a0e58SBosko Milekic /* 2082099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 20839b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 20849b78b1f4SJeff Roberson * definition. 2085099a0e58SBosko Milekic */ 208654c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) { 20879b78b1f4SJeff Roberson size_t shsize; 20889b78b1f4SJeff Roberson 20899b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 20909b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 2091244f4554SBosko Milekic /* 2092244f4554SBosko Milekic * The only way the following is possible is if with our 2093244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 2094244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 2095244f4554SBosko Milekic * mathematically possible for all cases, so we make 2096244f4554SBosko Milekic * sure here anyway. 2097244f4554SBosko Milekic */ 20989b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 20993d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 21003d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 2101099a0e58SBosko Milekic } 2102099a0e58SBosko Milekic 210354c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 21043b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 2105099a0e58SBosko Milekic 2106e63a1c2fSRyan Libby CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone); 2107099a0e58SBosko Milekic 2108099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 2109099a0e58SBosko Milekic 2110111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2111099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2112111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2113b23f72e9SBrian Feldman return (0); 2114099a0e58SBosko Milekic } 2115099a0e58SBosko Milekic 21162efcc8cbSGleb Smirnoff static void 2117a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused) 2118a81c400eSJeff Roberson { 2119a81c400eSJeff Roberson uma_keg_t keg; 2120a81c400eSJeff Roberson 2121a81c400eSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 2122a81c400eSJeff Roberson return; 2123a81c400eSJeff Roberson KEG_GET(zone, keg); 2124*ec0d8280SRyan Libby 2125*ec0d8280SRyan Libby if (keg->uk_allocf == startup_alloc) { 2126*ec0d8280SRyan Libby /* Switch to the real allocator. */ 2127f96d4157SJeff Roberson if (keg->uk_flags & UMA_ZONE_PCPU) 2128f96d4157SJeff Roberson keg->uk_allocf = pcpu_page_alloc; 2129*ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && 2130*ec0d8280SRyan Libby keg->uk_ppera > 1) 2131*ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 2132*ec0d8280SRyan Libby else 2133a81c400eSJeff Roberson keg->uk_allocf = page_alloc; 2134a81c400eSJeff Roberson } 2135*ec0d8280SRyan Libby } 2136a81c400eSJeff Roberson 2137a81c400eSJeff Roberson static void 213820a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 21392efcc8cbSGleb Smirnoff { 21402efcc8cbSGleb Smirnoff 21412efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 21422efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 21432efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 21442efcc8cbSGleb Smirnoff } 21452efcc8cbSGleb Smirnoff 214620a4e154SJeff Roberson static void 214720a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 214820a4e154SJeff Roberson { 214920a4e154SJeff Roberson uma_zone_domain_t zdom; 21508b987a77SJeff Roberson uma_domain_t dom; 215120a4e154SJeff Roberson uma_keg_t keg; 215220a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 21533b490537SJeff Roberson int domains, i, cnt; 215420a4e154SJeff Roberson static const char *nokeg = "cache zone"; 215520a4e154SJeff Roberson char *c; 215620a4e154SJeff Roberson 215720a4e154SJeff Roberson /* 215820a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 215920a4e154SJeff Roberson * any special characters and handling dups by appending 216020a4e154SJeff Roberson * an index. 216120a4e154SJeff Roberson */ 216220a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 21633b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 21643b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 21653b490537SJeff Roberson cnt /= 10; 21663b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 21673b490537SJeff Roberson M_UMA, M_WAITOK); 216820a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 216920a4e154SJeff Roberson zone->uz_namecnt); 217020a4e154SJeff Roberson } else 217120a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 217220a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 217320a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 217420a4e154SJeff Roberson *c = '_'; 217520a4e154SJeff Roberson 217620a4e154SJeff Roberson /* 217720a4e154SJeff Roberson * Basic parameters at the root. 217820a4e154SJeff Roberson */ 217920a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 218020a4e154SJeff Roberson OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, ""); 218120a4e154SJeff Roberson oid = zone->uz_oid; 218220a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 218320a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 21846d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21856d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 21866d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 218720a4e154SJeff Roberson "Allocator configuration flags"); 218820a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 218920a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 219020a4e154SJeff Roberson "Desired per-cpu cache size"); 219120a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 219220a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 219320a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 219420a4e154SJeff Roberson 219520a4e154SJeff Roberson /* 219620a4e154SJeff Roberson * keg if present. 219720a4e154SJeff Roberson */ 219854c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 21998b987a77SJeff Roberson domains = vm_ndomains; 22008b987a77SJeff Roberson else 22018b987a77SJeff Roberson domains = 1; 220220a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 220320a4e154SJeff Roberson "keg", CTLFLAG_RD, NULL, ""); 220420a4e154SJeff Roberson keg = zone->uz_keg; 22053b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 220620a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 220720a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 220820a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 220920a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 221020a4e154SJeff Roberson "Real object size with alignment"); 221120a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 221220a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 221320a4e154SJeff Roberson "pages per-slab allocation"); 221420a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 221520a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 221620a4e154SJeff Roberson "items available per-slab"); 221720a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 221820a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 221920a4e154SJeff Roberson "item alignment mask"); 2220f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2221f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2222f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2223f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 22248b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 22258b987a77SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 22268b987a77SJeff Roberson for (i = 0; i < domains; i++) { 22278b987a77SJeff Roberson dom = &keg->uk_domain[i]; 22288b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 22298b987a77SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, 22308b987a77SJeff Roberson NULL, ""); 22318b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22328b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 22338b987a77SJeff Roberson "Total pages currently allocated from VM"); 22348b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22358b987a77SJeff Roberson "free", CTLFLAG_RD, &dom->ud_free, 0, 22368b987a77SJeff Roberson "items free in the slab layer"); 22378b987a77SJeff Roberson } 223820a4e154SJeff Roberson } else 223920a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 224020a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 224120a4e154SJeff Roberson 224220a4e154SJeff Roberson /* 224320a4e154SJeff Roberson * Information about zone limits. 224420a4e154SJeff Roberson */ 224520a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 224620a4e154SJeff Roberson "limit", CTLFLAG_RD, NULL, ""); 22474bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22484bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 22494bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 22504bd61e19SJeff Roberson "current number of allocated items if limit is set"); 225120a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 225220a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 225320a4e154SJeff Roberson "Maximum number of cached items"); 225420a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 225520a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 225620a4e154SJeff Roberson "Number of threads sleeping at limit"); 225720a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 225820a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 225920a4e154SJeff Roberson "Total zone limit sleeps"); 22604bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22614bd61e19SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bkt_max, 0, 22624bd61e19SJeff Roberson "Maximum number of items in the bucket cache"); 22634bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22644bd61e19SJeff Roberson "bucket_cnt", CTLFLAG_RD, &zone->uz_bkt_count, 0, 22654bd61e19SJeff Roberson "Number of items in the bucket cache"); 226620a4e154SJeff Roberson 226720a4e154SJeff Roberson /* 22688b987a77SJeff Roberson * Per-domain zone information. 226920a4e154SJeff Roberson */ 227020a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 227120a4e154SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 2272dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0) 22738b987a77SJeff Roberson domains = 1; 227420a4e154SJeff Roberson for (i = 0; i < domains; i++) { 227520a4e154SJeff Roberson zdom = &zone->uz_domain[i]; 227620a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 227720a4e154SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, ""); 227820a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 227920a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 228020a4e154SJeff Roberson "number of items in this domain"); 228120a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 228220a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 228320a4e154SJeff Roberson "maximum item count in this period"); 228420a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 228520a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 228620a4e154SJeff Roberson "minimum item count in this period"); 228720a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 228820a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 228920a4e154SJeff Roberson "Working set size"); 229020a4e154SJeff Roberson } 229120a4e154SJeff Roberson 229220a4e154SJeff Roberson /* 229320a4e154SJeff Roberson * General statistics. 229420a4e154SJeff Roberson */ 229520a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 229620a4e154SJeff Roberson "stats", CTLFLAG_RD, NULL, ""); 229720a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 229820a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 229920a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 230020a4e154SJeff Roberson "Current number of allocated items"); 230120a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 230220a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 230320a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 230420a4e154SJeff Roberson "Total allocation calls"); 230520a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 230620a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 230720a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 230820a4e154SJeff Roberson "Total free calls"); 230920a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 231020a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 231120a4e154SJeff Roberson "Number of allocation failures"); 231220a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 231320a4e154SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 0, 231420a4e154SJeff Roberson "Free calls from the wrong domain"); 231520a4e154SJeff Roberson } 231620a4e154SJeff Roberson 231720a4e154SJeff Roberson struct uma_zone_count { 231820a4e154SJeff Roberson const char *name; 231920a4e154SJeff Roberson int count; 232020a4e154SJeff Roberson }; 232120a4e154SJeff Roberson 232220a4e154SJeff Roberson static void 232320a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 232420a4e154SJeff Roberson { 232520a4e154SJeff Roberson struct uma_zone_count *cnt; 232620a4e154SJeff Roberson 232720a4e154SJeff Roberson cnt = arg; 23283b490537SJeff Roberson /* 23293b490537SJeff Roberson * Some zones are rapidly created with identical names and 23303b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 23313b490537SJeff Roberson * Use one greater than the maximum observed for this name. 23323b490537SJeff Roberson */ 233320a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 23343b490537SJeff Roberson cnt->count = MAX(cnt->count, 23353b490537SJeff Roberson zone->uz_namecnt + 1); 233620a4e154SJeff Roberson } 233720a4e154SJeff Roberson 2338cc7ce83aSJeff Roberson static void 2339cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2340cc7ce83aSJeff Roberson { 2341cc7ce83aSJeff Roberson int i; 2342cc7ce83aSJeff Roberson 2343cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2344cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2345cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2346cc7ce83aSJeff Roberson } 2347cc7ce83aSJeff Roberson } 2348cc7ce83aSJeff Roberson 2349099a0e58SBosko Milekic /* 2350099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2351099a0e58SBosko Milekic * 2352099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2353099a0e58SBosko Milekic * udata Actually uma_zctor_args 23548355f576SJeff Roberson */ 2355b23f72e9SBrian Feldman static int 2356b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 23578355f576SJeff Roberson { 235820a4e154SJeff Roberson struct uma_zone_count cnt; 23598355f576SJeff Roberson struct uma_zctor_args *arg = udata; 23608355f576SJeff Roberson uma_zone_t zone = mem; 2361099a0e58SBosko Milekic uma_zone_t z; 2362099a0e58SBosko Milekic uma_keg_t keg; 236308cfa56eSMark Johnston int i; 23648355f576SJeff Roberson 23658355f576SJeff Roberson bzero(zone, size); 23668355f576SJeff Roberson zone->uz_name = arg->name; 23678355f576SJeff Roberson zone->uz_ctor = arg->ctor; 23688355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2369099a0e58SBosko Milekic zone->uz_init = NULL; 2370099a0e58SBosko Milekic zone->uz_fini = NULL; 2371bf965959SSean Bruno zone->uz_sleeps = 0; 2372c1685086SJeff Roberson zone->uz_xdomain = 0; 237320a4e154SJeff Roberson zone->uz_bucket_size = 0; 237420a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 237520a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2376d4665eaaSJeff Roberson zone->uz_flags = (arg->flags & UMA_ZONE_SMR); 23772f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2378ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 23798d1c459aSRyan Libby zone->uz_domain = 23808d1c459aSRyan Libby (struct uma_zone_domain *)&zone->uz_cpu[mp_maxid + 1]; 2381bb15d1c7SGleb Smirnoff zone->uz_bkt_max = ULONG_MAX; 23822f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2383af526374SJeff Roberson 238420a4e154SJeff Roberson /* Count the number of duplicate names. */ 238520a4e154SJeff Roberson cnt.name = arg->name; 238620a4e154SJeff Roberson cnt.count = 0; 238720a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 238820a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 2389727c6918SJeff Roberson ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 239091d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 23912efcc8cbSGleb Smirnoff 239208cfa56eSMark Johnston for (i = 0; i < vm_ndomains; i++) 2393dc3915c8SJeff Roberson STAILQ_INIT(&zone->uz_domain[i].uzd_buckets); 239408cfa56eSMark Johnston 2395ca293436SRyan Libby #ifdef INVARIANTS 2396ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2397cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 2398ca293436SRyan Libby #endif 2399ca293436SRyan Libby 24000095a784SJeff Roberson /* 24010095a784SJeff Roberson * This is a pure cache zone, no kegs. 24020095a784SJeff Roberson */ 24030095a784SJeff Roberson if (arg->import) { 2404727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2405727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 24066fd34d6fSJeff Roberson if (arg->flags & UMA_ZONE_VM) 24076fd34d6fSJeff Roberson arg->flags |= UMA_ZFLAG_CACHEONLY; 24086fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2409af526374SJeff Roberson zone->uz_size = arg->size; 24100095a784SJeff Roberson zone->uz_import = arg->import; 24110095a784SJeff Roberson zone->uz_release = arg->release; 24120095a784SJeff Roberson zone->uz_arg = arg->arg; 2413111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 241403175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2415111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2416af526374SJeff Roberson goto out; 24170095a784SJeff Roberson } 24180095a784SJeff Roberson 24190095a784SJeff Roberson /* 24200095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 24210095a784SJeff Roberson */ 2422b75c4efcSAndrew Turner zone->uz_import = zone_import; 2423b75c4efcSAndrew Turner zone->uz_release = zone_release; 24240095a784SJeff Roberson zone->uz_arg = zone; 2425bb15d1c7SGleb Smirnoff keg = arg->keg; 24260095a784SJeff Roberson 2427099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 242820a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 242920a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2430099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 24318355f576SJeff Roberson zone->uz_init = arg->uminit; 2432e221e841SJeff Roberson zone->uz_fini = arg->fini; 2433e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2434111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2435099a0e58SBosko Milekic ZONE_LOCK(zone); 2436099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2437099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2438099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2439099a0e58SBosko Milekic break; 2440099a0e58SBosko Milekic } 2441099a0e58SBosko Milekic } 2442099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2443111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2444e20a199fSJeff Roberson } else if (keg == NULL) { 2445e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2446e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2447b23f72e9SBrian Feldman return (ENOMEM); 2448099a0e58SBosko Milekic } else { 2449099a0e58SBosko Milekic struct uma_kctor_args karg; 2450b23f72e9SBrian Feldman int error; 2451099a0e58SBosko Milekic 2452099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2453099a0e58SBosko Milekic karg.size = arg->size; 2454099a0e58SBosko Milekic karg.uminit = arg->uminit; 2455099a0e58SBosko Milekic karg.fini = arg->fini; 2456099a0e58SBosko Milekic karg.align = arg->align; 2457d4665eaaSJeff Roberson karg.flags = (arg->flags & ~UMA_ZONE_SMR); 2458099a0e58SBosko Milekic karg.zone = zone; 2459b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2460b23f72e9SBrian Feldman flags); 2461b23f72e9SBrian Feldman if (error) 2462b23f72e9SBrian Feldman return (error); 2463099a0e58SBosko Milekic } 24640095a784SJeff Roberson 246520a4e154SJeff Roberson /* Inherit properties from the keg. */ 2466bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2467e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2468e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2469e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 24708355f576SJeff Roberson 247120a4e154SJeff Roberson out: 2472860bb7a0SMark Johnston if (__predict_true(booted >= BOOT_RUNNING)) { 247320a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 247420a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 247520a4e154SJeff Roberson } else { 247620a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 247720a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 247820a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2479099a0e58SBosko Milekic } 24808355f576SJeff Roberson 2481d4665eaaSJeff Roberson /* Caller requests a private SMR context. */ 2482d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 2483d4665eaaSJeff Roberson zone->uz_smr = smr_create(zone->uz_name); 2484d4665eaaSJeff Roberson 24857e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 24867e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 24877e28037aSMark Johnston ("Invalid zone flag combination")); 248820a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 248920a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 249020a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 249120a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 249220a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) 249320a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN; 249420a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 249520a4e154SJeff Roberson zone->uz_bucket_size = 0; 24967e28037aSMark Johnston else 249720a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 249820a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2499cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2500cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2501cc7ce83aSJeff Roberson zone_update_caches(zone); 2502fc03d22bSJeff Roberson 2503b23f72e9SBrian Feldman return (0); 25048355f576SJeff Roberson } 25058355f576SJeff Roberson 25068355f576SJeff Roberson /* 2507099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2508099a0e58SBosko Milekic * table and removes the keg from the global list. 25099c2cd7e5SJeff Roberson * 25109c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 25119c2cd7e5SJeff Roberson * udata unused 25129c2cd7e5SJeff Roberson */ 2513099a0e58SBosko Milekic static void 2514099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2515099a0e58SBosko Milekic { 2516099a0e58SBosko Milekic uma_keg_t keg; 25178b987a77SJeff Roberson uint32_t free, pages; 25188b987a77SJeff Roberson int i; 25199c2cd7e5SJeff Roberson 2520099a0e58SBosko Milekic keg = (uma_keg_t)arg; 25218b987a77SJeff Roberson free = pages = 0; 25228b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 25238b987a77SJeff Roberson free += keg->uk_domain[i].ud_free; 25248b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 25258b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2526099a0e58SBosko Milekic } 25277e240677SRyan Libby if (pages != 0) 25288b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 25298b987a77SJeff Roberson " Lost %u pages of memory.\n", 25308b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 25317e240677SRyan Libby pages / keg->uk_ppera * keg->uk_ipers - free, pages); 2532099a0e58SBosko Milekic 2533099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2534099a0e58SBosko Milekic } 2535099a0e58SBosko Milekic 2536099a0e58SBosko Milekic /* 2537099a0e58SBosko Milekic * Zone header dtor. 2538099a0e58SBosko Milekic * 2539099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2540099a0e58SBosko Milekic * udata unused 2541099a0e58SBosko Milekic */ 25429c2cd7e5SJeff Roberson static void 25439c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 25449c2cd7e5SJeff Roberson { 25459c2cd7e5SJeff Roberson uma_zone_t zone; 2546099a0e58SBosko Milekic uma_keg_t keg; 25479c2cd7e5SJeff Roberson 25489c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 25499643769aSJeff Roberson 255020a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 255120a4e154SJeff Roberson 2552e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 25539643769aSJeff Roberson cache_drain(zone); 2554099a0e58SBosko Milekic 2555111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2556099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2557111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2558099a0e58SBosko Milekic /* 2559099a0e58SBosko Milekic * XXX there are some races here where 2560099a0e58SBosko Milekic * the zone can be drained but zone lock 2561099a0e58SBosko Milekic * released and then refilled before we 2562099a0e58SBosko Milekic * remove it... we dont care for now 2563099a0e58SBosko Milekic */ 256408cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2565e20a199fSJeff Roberson /* 2566323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2567e20a199fSJeff Roberson */ 2568323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2569323ad386STycho Nightingale keg = zone->uz_keg; 2570111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2571099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2572111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 25730095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 25749c2cd7e5SJeff Roberson } 25752efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 25762efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 25772efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 257820a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2579af526374SJeff Roberson ZONE_LOCK_FINI(zone); 258091d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 2581099a0e58SBosko Milekic } 2582099a0e58SBosko Milekic 2583a81c400eSJeff Roberson static void 2584a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg) 2585a81c400eSJeff Roberson { 2586a81c400eSJeff Roberson uma_keg_t keg; 2587a81c400eSJeff Roberson uma_zone_t zone; 2588a81c400eSJeff Roberson 2589a81c400eSJeff Roberson LIST_FOREACH(keg, &uma_kegs, uk_link) { 2590a81c400eSJeff Roberson LIST_FOREACH(zone, &keg->uk_zones, uz_link) 2591a81c400eSJeff Roberson zfunc(zone, arg); 2592a81c400eSJeff Roberson } 2593a81c400eSJeff Roberson LIST_FOREACH(zone, &uma_cachezones, uz_link) 2594a81c400eSJeff Roberson zfunc(zone, arg); 2595a81c400eSJeff Roberson } 2596a81c400eSJeff Roberson 25979c2cd7e5SJeff Roberson /* 25988355f576SJeff Roberson * Traverses every zone in the system and calls a callback 25998355f576SJeff Roberson * 26008355f576SJeff Roberson * Arguments: 26018355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 26028355f576SJeff Roberson * as an argument. 26038355f576SJeff Roberson * 26048355f576SJeff Roberson * Returns: 26058355f576SJeff Roberson * Nothing 26068355f576SJeff Roberson */ 26078355f576SJeff Roberson static void 260820a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 26098355f576SJeff Roberson { 26108355f576SJeff Roberson 2611111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2612a81c400eSJeff Roberson zone_foreach_unlocked(zfunc, arg); 2613111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 26148355f576SJeff Roberson } 26158355f576SJeff Roberson 2616f4bef67cSGleb Smirnoff /* 2617a81c400eSJeff Roberson * Initialize the kernel memory allocator. This is done after pages can be 2618a81c400eSJeff Roberson * allocated but before general KVA is available. 2619f4bef67cSGleb Smirnoff */ 2620a81c400eSJeff Roberson void 2621a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail) 2622f4bef67cSGleb Smirnoff { 2623a81c400eSJeff Roberson struct uma_zctor_args args; 2624a81c400eSJeff Roberson size_t ksize, zsize, size; 2625a81c400eSJeff Roberson uma_keg_t masterkeg; 2626a81c400eSJeff Roberson uintptr_t m; 2627a81c400eSJeff Roberson uint8_t pflag; 2628a81c400eSJeff Roberson 2629a81c400eSJeff Roberson bootstart = bootmem = virtual_avail; 2630a81c400eSJeff Roberson 2631a81c400eSJeff Roberson rw_init(&uma_rwlock, "UMA lock"); 2632a81c400eSJeff Roberson sx_init(&uma_reclaim_lock, "umareclaim"); 2633f4bef67cSGleb Smirnoff 2634f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2635f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 263679c9f942SJeff Roberson ksize = roundup(ksize, UMA_SUPER_ALIGN); 2637f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2638f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2639f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 264079c9f942SJeff Roberson zsize = roundup(zsize, UMA_SUPER_ALIGN); 2641f4bef67cSGleb Smirnoff 2642a81c400eSJeff Roberson /* Allocate the zone of zones, zone of kegs, and zone of zones keg. */ 2643a81c400eSJeff Roberson size = (zsize * 2) + ksize; 2644a81c400eSJeff Roberson m = (uintptr_t)startup_alloc(NULL, size, 0, &pflag, M_NOWAIT | M_ZERO); 2645ab3185d1SJeff Roberson zones = (uma_zone_t)m; 264679c9f942SJeff Roberson m += zsize; 2647ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 264879c9f942SJeff Roberson m += zsize; 2649ab3185d1SJeff Roberson masterkeg = (uma_keg_t)m; 2650ab3185d1SJeff Roberson 2651099a0e58SBosko Milekic /* "manually" create the initial zone */ 26520095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2653099a0e58SBosko Milekic args.name = "UMA Kegs"; 2654ab3185d1SJeff Roberson args.size = ksize; 2655099a0e58SBosko Milekic args.ctor = keg_ctor; 2656099a0e58SBosko Milekic args.dtor = keg_dtor; 26578355f576SJeff Roberson args.uminit = zero_init; 26588355f576SJeff Roberson args.fini = NULL; 2659ab3185d1SJeff Roberson args.keg = masterkeg; 266079c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2661b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2662ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 26638355f576SJeff Roberson 2664099a0e58SBosko Milekic args.name = "UMA Zones"; 2665f4bef67cSGleb Smirnoff args.size = zsize; 2666099a0e58SBosko Milekic args.ctor = zone_ctor; 2667099a0e58SBosko Milekic args.dtor = zone_dtor; 2668099a0e58SBosko Milekic args.uminit = zero_init; 2669099a0e58SBosko Milekic args.fini = NULL; 2670099a0e58SBosko Milekic args.keg = NULL; 267179c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2672099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2673ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2674099a0e58SBosko Milekic 26759b8db4d0SRyan Libby /* Now make zones for slab headers */ 26769b8db4d0SRyan Libby slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE, 26779b8db4d0SRyan Libby NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 26789b8db4d0SRyan Libby slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE, 26791e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 26808355f576SJeff Roberson 26818355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 26828355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 26831e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 26848355f576SJeff Roberson 2685a81c400eSJeff Roberson bucket_init(); 2686d4665eaaSJeff Roberson smr_init(); 26878355f576SJeff Roberson } 26888355f576SJeff Roberson 2689a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2690a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void); 2691f4bef67cSGleb Smirnoff #endif 2692f4bef67cSGleb Smirnoff 2693a81c400eSJeff Roberson /* 2694a81c400eSJeff Roberson * Advertise the availability of normal kva allocations and switch to 2695a81c400eSJeff Roberson * the default back-end allocator. Marks the KVA we consumed on startup 2696a81c400eSJeff Roberson * as used in the map. 2697a81c400eSJeff Roberson */ 26988355f576SJeff Roberson void 269999571dc3SJeff Roberson uma_startup2(void) 27008355f576SJeff Roberson { 2701f4bef67cSGleb Smirnoff 2702530cc6a2SJeff Roberson if (bootstart != bootmem) { 2703a81c400eSJeff Roberson vm_map_lock(kernel_map); 2704a81c400eSJeff Roberson (void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem, 2705a81c400eSJeff Roberson VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 2706a81c400eSJeff Roberson vm_map_unlock(kernel_map); 2707a81c400eSJeff Roberson } 2708a81c400eSJeff Roberson 2709a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2710a81c400eSJeff Roberson /* Set up radix zone to use noobj_alloc. */ 2711a81c400eSJeff Roberson vm_radix_reserve_kva(); 2712f7d35785SGleb Smirnoff #endif 2713a81c400eSJeff Roberson 2714a81c400eSJeff Roberson booted = BOOT_KVA; 2715a81c400eSJeff Roberson zone_foreach_unlocked(zone_kva_available, NULL); 2716f4bef67cSGleb Smirnoff bucket_enable(); 27178355f576SJeff Roberson } 27188355f576SJeff Roberson 2719a81c400eSJeff Roberson /* 2720a81c400eSJeff Roberson * Finish our initialization steps. 2721a81c400eSJeff Roberson */ 27228355f576SJeff Roberson static void 27238355f576SJeff Roberson uma_startup3(void) 27248355f576SJeff Roberson { 27251431a748SGleb Smirnoff 2726c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2727c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2728c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2729c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2730c5deaf04SGleb Smirnoff #endif 2731a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_counters, NULL); 2732a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_sysctl, NULL); 2733fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 27349643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2735c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 2736860bb7a0SMark Johnston 2737860bb7a0SMark Johnston EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL, 2738860bb7a0SMark Johnston EVENTHANDLER_PRI_FIRST); 2739860bb7a0SMark Johnston } 2740860bb7a0SMark Johnston 2741860bb7a0SMark Johnston static void 2742860bb7a0SMark Johnston uma_shutdown(void) 2743860bb7a0SMark Johnston { 2744860bb7a0SMark Johnston 2745860bb7a0SMark Johnston booted = BOOT_SHUTDOWN; 27468355f576SJeff Roberson } 27478355f576SJeff Roberson 2748e20a199fSJeff Roberson static uma_keg_t 2749099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 275085dcf349SGleb Smirnoff int align, uint32_t flags) 2751099a0e58SBosko Milekic { 2752099a0e58SBosko Milekic struct uma_kctor_args args; 2753099a0e58SBosko Milekic 2754099a0e58SBosko Milekic args.size = size; 2755099a0e58SBosko Milekic args.uminit = uminit; 2756099a0e58SBosko Milekic args.fini = fini; 27571e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2758099a0e58SBosko Milekic args.flags = flags; 2759099a0e58SBosko Milekic args.zone = zone; 2760ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 2761099a0e58SBosko Milekic } 2762099a0e58SBosko Milekic 2763f4bef67cSGleb Smirnoff /* Public functions */ 27648355f576SJeff Roberson /* See uma.h */ 27651e319f6dSRobert Watson void 27661e319f6dSRobert Watson uma_set_align(int align) 27671e319f6dSRobert Watson { 27681e319f6dSRobert Watson 27691e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 27701e319f6dSRobert Watson uma_align_cache = align; 27711e319f6dSRobert Watson } 27721e319f6dSRobert Watson 27731e319f6dSRobert Watson /* See uma.h */ 27748355f576SJeff Roberson uma_zone_t 2775bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 277685dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 27778355f576SJeff Roberson 27788355f576SJeff Roberson { 27798355f576SJeff Roberson struct uma_zctor_args args; 278095c4bf75SKonstantin Belousov uma_zone_t res; 27818355f576SJeff Roberson 2782a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 2783a5a35578SJohn Baldwin align, name)); 2784a5a35578SJohn Baldwin 27858355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 27860095a784SJeff Roberson memset(&args, 0, sizeof(args)); 27878355f576SJeff Roberson args.name = name; 27888355f576SJeff Roberson args.size = size; 27898355f576SJeff Roberson args.ctor = ctor; 27908355f576SJeff Roberson args.dtor = dtor; 27918355f576SJeff Roberson args.uminit = uminit; 27928355f576SJeff Roberson args.fini = fini; 2793afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 2794afc6dc36SJohn-Mark Gurney /* 2795ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 2796ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 2797ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 2798ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 2799ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 2800ca293436SRyan Libby * the ctor and dtor do not need to be empty. 2801afc6dc36SJohn-Mark Gurney */ 280254c5ae80SRyan Libby if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH | 280354c5ae80SRyan Libby UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) { 2804afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 2805afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 2806afc6dc36SJohn-Mark Gurney } 2807afc6dc36SJohn-Mark Gurney #endif 28088355f576SJeff Roberson args.align = align; 28098355f576SJeff Roberson args.flags = flags; 2810099a0e58SBosko Milekic args.keg = NULL; 2811099a0e58SBosko Milekic 281208cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 2813ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 281408cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 2815a81c400eSJeff Roberson 281695c4bf75SKonstantin Belousov return (res); 2817099a0e58SBosko Milekic } 2818099a0e58SBosko Milekic 2819099a0e58SBosko Milekic /* See uma.h */ 2820099a0e58SBosko Milekic uma_zone_t 2821099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 2822099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 2823099a0e58SBosko Milekic { 2824099a0e58SBosko Milekic struct uma_zctor_args args; 2825e20a199fSJeff Roberson uma_keg_t keg; 282695c4bf75SKonstantin Belousov uma_zone_t res; 2827099a0e58SBosko Milekic 2828bb15d1c7SGleb Smirnoff keg = master->uz_keg; 28290095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2830099a0e58SBosko Milekic args.name = name; 2831e20a199fSJeff Roberson args.size = keg->uk_size; 2832099a0e58SBosko Milekic args.ctor = ctor; 2833099a0e58SBosko Milekic args.dtor = dtor; 2834099a0e58SBosko Milekic args.uminit = zinit; 2835099a0e58SBosko Milekic args.fini = zfini; 2836e20a199fSJeff Roberson args.align = keg->uk_align; 2837e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 2838e20a199fSJeff Roberson args.keg = keg; 28398355f576SJeff Roberson 284008cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 2841ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 284208cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 2843a81c400eSJeff Roberson 284495c4bf75SKonstantin Belousov return (res); 28458355f576SJeff Roberson } 28468355f576SJeff Roberson 28470095a784SJeff Roberson /* See uma.h */ 28480095a784SJeff Roberson uma_zone_t 2849af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 2850af526374SJeff Roberson uma_init zinit, uma_fini zfini, uma_import zimport, 2851af526374SJeff Roberson uma_release zrelease, void *arg, int flags) 28520095a784SJeff Roberson { 28530095a784SJeff Roberson struct uma_zctor_args args; 28540095a784SJeff Roberson 28550095a784SJeff Roberson memset(&args, 0, sizeof(args)); 28560095a784SJeff Roberson args.name = name; 2857af526374SJeff Roberson args.size = size; 28580095a784SJeff Roberson args.ctor = ctor; 28590095a784SJeff Roberson args.dtor = dtor; 28600095a784SJeff Roberson args.uminit = zinit; 28610095a784SJeff Roberson args.fini = zfini; 28620095a784SJeff Roberson args.import = zimport; 28630095a784SJeff Roberson args.release = zrelease; 28640095a784SJeff Roberson args.arg = arg; 28650095a784SJeff Roberson args.align = 0; 2866bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 28670095a784SJeff Roberson 2868ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 28690095a784SJeff Roberson } 28700095a784SJeff Roberson 28718355f576SJeff Roberson /* See uma.h */ 28729c2cd7e5SJeff Roberson void 28739c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 28749c2cd7e5SJeff Roberson { 2875f4ff923bSRobert Watson 2876860bb7a0SMark Johnston /* 2877860bb7a0SMark Johnston * Large slabs are expensive to reclaim, so don't bother doing 2878860bb7a0SMark Johnston * unnecessary work if we're shutting down. 2879860bb7a0SMark Johnston */ 2880860bb7a0SMark Johnston if (booted == BOOT_SHUTDOWN && 2881860bb7a0SMark Johnston zone->uz_fini == NULL && zone->uz_release == zone_release) 2882860bb7a0SMark Johnston return; 288308cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 28840095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 288508cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 28869c2cd7e5SJeff Roberson } 28879c2cd7e5SJeff Roberson 28888d6fbbb8SJeff Roberson void 28898d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 28908d6fbbb8SJeff Roberson { 28918d6fbbb8SJeff Roberson void *item; 28928d6fbbb8SJeff Roberson 28938d6fbbb8SJeff Roberson item = uma_zalloc_arg(zone, NULL, M_WAITOK); 28948d6fbbb8SJeff Roberson uma_zfree(zone, item); 28958d6fbbb8SJeff Roberson } 28968d6fbbb8SJeff Roberson 28974e180881SMateusz Guzik void * 28984e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 28994e180881SMateusz Guzik { 29004e180881SMateusz Guzik void *item; 2901b4799947SRuslan Bukin #ifdef SMP 29024e180881SMateusz Guzik int i; 29034e180881SMateusz Guzik 29044e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2905b4799947SRuslan Bukin #endif 29064e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 29074e180881SMateusz Guzik if (item != NULL && (flags & M_ZERO)) { 2908b4799947SRuslan Bukin #ifdef SMP 2909013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 29104e180881SMateusz Guzik bzero(zpcpu_get_cpu(item, i), zone->uz_size); 2911b4799947SRuslan Bukin #else 2912b4799947SRuslan Bukin bzero(item, zone->uz_size); 2913b4799947SRuslan Bukin #endif 29144e180881SMateusz Guzik } 29154e180881SMateusz Guzik return (item); 29164e180881SMateusz Guzik } 29174e180881SMateusz Guzik 29184e180881SMateusz Guzik /* 29194e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 29204e180881SMateusz Guzik */ 29214e180881SMateusz Guzik void 29224e180881SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata) 29234e180881SMateusz Guzik { 29244e180881SMateusz Guzik 2925c5b7751fSIan Lepore #ifdef SMP 29264e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2927c5b7751fSIan Lepore #endif 29284e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 29294e180881SMateusz Guzik } 29304e180881SMateusz Guzik 2931d4665eaaSJeff Roberson static inline void * 2932d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags, 2933d4665eaaSJeff Roberson void *item) 2934beb8beefSJeff Roberson { 2935beb8beefSJeff Roberson #ifdef INVARIANTS 2936ca293436SRyan Libby bool skipdbg; 2937beb8beefSJeff Roberson 2938beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 2939ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2940ca293436SRyan Libby zone->uz_ctor != trash_ctor) 2941cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 2942beb8beefSJeff Roberson #endif 2943d4665eaaSJeff Roberson /* Check flags before loading ctor pointer. */ 2944d4665eaaSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) && 2945d4665eaaSJeff Roberson __predict_false(zone->uz_ctor != NULL) && 2946cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 2947beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 2948beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 2949beb8beefSJeff Roberson return (NULL); 2950beb8beefSJeff Roberson } 2951beb8beefSJeff Roberson #ifdef INVARIANTS 2952beb8beefSJeff Roberson if (!skipdbg) 2953beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 2954beb8beefSJeff Roberson #endif 2955beb8beefSJeff Roberson if (flags & M_ZERO) 2956cc7ce83aSJeff Roberson bzero(item, size); 2957beb8beefSJeff Roberson 2958beb8beefSJeff Roberson return (item); 2959beb8beefSJeff Roberson } 2960beb8beefSJeff Roberson 2961ca293436SRyan Libby static inline void 2962cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 2963cc7ce83aSJeff Roberson enum zfreeskip skip) 2964ca293436SRyan Libby { 2965ca293436SRyan Libby #ifdef INVARIANTS 2966ca293436SRyan Libby bool skipdbg; 2967ca293436SRyan Libby 2968ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 2969ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 2970ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 2971ca293436SRyan Libby uma_dbg_free(zone, udata, item); 2972ca293436SRyan Libby else 2973ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 2974ca293436SRyan Libby } 2975ca293436SRyan Libby #endif 2976cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 2977ca293436SRyan Libby if (zone->uz_dtor != NULL) 2978cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 2979ca293436SRyan Libby #ifdef INVARIANTS 2980ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2981ca293436SRyan Libby zone->uz_dtor != trash_dtor) 2982cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 2983ca293436SRyan Libby #endif 2984ca293436SRyan Libby } 2985ca293436SRyan Libby } 2986ca293436SRyan Libby 2987d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS) 2988d4665eaaSJeff Roberson #define UMA_ZALLOC_DEBUG 2989d4665eaaSJeff Roberson static int 2990d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags) 2991d4665eaaSJeff Roberson { 2992d4665eaaSJeff Roberson int error; 2993d4665eaaSJeff Roberson 2994d4665eaaSJeff Roberson error = 0; 2995d4665eaaSJeff Roberson #ifdef WITNESS 2996d4665eaaSJeff Roberson if (flags & M_WAITOK) { 2997d4665eaaSJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2998d4665eaaSJeff Roberson "uma_zalloc_debug: zone \"%s\"", zone->uz_name); 2999d4665eaaSJeff Roberson } 3000d4665eaaSJeff Roberson #endif 3001d4665eaaSJeff Roberson 3002d4665eaaSJeff Roberson #ifdef INVARIANTS 3003d4665eaaSJeff Roberson KASSERT((flags & M_EXEC) == 0, 3004d4665eaaSJeff Roberson ("uma_zalloc_debug: called with M_EXEC")); 3005d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3006d4665eaaSJeff Roberson ("uma_zalloc_debug: called within spinlock or critical section")); 3007d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0, 3008d4665eaaSJeff Roberson ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO")); 3009d4665eaaSJeff Roberson #endif 3010d4665eaaSJeff Roberson 3011d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 30129e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) { 3013d4665eaaSJeff Roberson void *item; 3014d4665eaaSJeff Roberson item = memguard_alloc(zone->uz_size, flags); 3015d4665eaaSJeff Roberson if (item != NULL) { 3016d4665eaaSJeff Roberson error = EJUSTRETURN; 3017d4665eaaSJeff Roberson if (zone->uz_init != NULL && 3018d4665eaaSJeff Roberson zone->uz_init(item, zone->uz_size, flags) != 0) { 3019d4665eaaSJeff Roberson *itemp = NULL; 3020d4665eaaSJeff Roberson return (error); 3021d4665eaaSJeff Roberson } 3022d4665eaaSJeff Roberson if (zone->uz_ctor != NULL && 3023d4665eaaSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 3024d4665eaaSJeff Roberson flags) != 0) { 3025d4665eaaSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3026d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3027d4665eaaSJeff Roberson *itemp = NULL; 3028d4665eaaSJeff Roberson return (error); 3029d4665eaaSJeff Roberson } 3030d4665eaaSJeff Roberson *itemp = item; 3031d4665eaaSJeff Roberson return (error); 3032d4665eaaSJeff Roberson } 3033d4665eaaSJeff Roberson /* This is unfortunate but should not be fatal. */ 3034d4665eaaSJeff Roberson } 3035d4665eaaSJeff Roberson #endif 3036d4665eaaSJeff Roberson return (error); 3037d4665eaaSJeff Roberson } 3038d4665eaaSJeff Roberson 3039d4665eaaSJeff Roberson static int 3040d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata) 3041d4665eaaSJeff Roberson { 3042d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3043d4665eaaSJeff Roberson ("uma_zfree_debug: called with spinlock or critical section held")); 3044d4665eaaSJeff Roberson 3045d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 30469e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) { 3047d4665eaaSJeff Roberson if (zone->uz_dtor != NULL) 3048d4665eaaSJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 3049d4665eaaSJeff Roberson if (zone->uz_fini != NULL) 3050d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3051d4665eaaSJeff Roberson memguard_free(item); 3052d4665eaaSJeff Roberson return (EJUSTRETURN); 3053d4665eaaSJeff Roberson } 3054d4665eaaSJeff Roberson #endif 3055d4665eaaSJeff Roberson return (0); 3056d4665eaaSJeff Roberson } 3057d4665eaaSJeff Roberson #endif 3058d4665eaaSJeff Roberson 3059d4665eaaSJeff Roberson static __noinline void * 3060d4665eaaSJeff Roberson uma_zalloc_single(uma_zone_t zone, void *udata, int flags) 3061d4665eaaSJeff Roberson { 3062d4665eaaSJeff Roberson int domain; 3063d4665eaaSJeff Roberson 3064d4665eaaSJeff Roberson /* 3065d4665eaaSJeff Roberson * We can not get a bucket so try to return a single item. 3066d4665eaaSJeff Roberson */ 3067d4665eaaSJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 3068d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3069d4665eaaSJeff Roberson else 3070d4665eaaSJeff Roberson domain = UMA_ANYDOMAIN; 3071d4665eaaSJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3072d4665eaaSJeff Roberson } 3073d4665eaaSJeff Roberson 3074d4665eaaSJeff Roberson /* See uma.h */ 3075d4665eaaSJeff Roberson void * 3076d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags) 3077d4665eaaSJeff Roberson { 3078d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 3079d4665eaaSJeff Roberson uma_cache_t cache; 3080d4665eaaSJeff Roberson void *item; 3081d4665eaaSJeff Roberson int size, uz_flags; 3082d4665eaaSJeff Roberson 3083d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3084d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 3085d4665eaaSJeff Roberson ("uma_zalloc_arg: called with non-SMR zone.\n")); 3086d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN) 3087d4665eaaSJeff Roberson return (item); 3088d4665eaaSJeff Roberson #endif 3089d4665eaaSJeff Roberson 3090d4665eaaSJeff Roberson critical_enter(); 3091d4665eaaSJeff Roberson do { 3092d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3093d4665eaaSJeff Roberson bucket = &cache->uc_allocbucket; 3094d4665eaaSJeff Roberson size = cache_uz_size(cache); 3095d4665eaaSJeff Roberson uz_flags = cache_uz_flags(cache); 3096d4665eaaSJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 3097d4665eaaSJeff Roberson item = cache_bucket_pop(cache, bucket); 3098d4665eaaSJeff Roberson critical_exit(); 3099d4665eaaSJeff Roberson return (item_ctor(zone, uz_flags, size, NULL, flags, 3100d4665eaaSJeff Roberson item)); 3101d4665eaaSJeff Roberson } 3102d4665eaaSJeff Roberson } while (cache_alloc(zone, cache, NULL, flags)); 3103d4665eaaSJeff Roberson critical_exit(); 3104d4665eaaSJeff Roberson 3105d4665eaaSJeff Roberson return (uma_zalloc_single(zone, NULL, flags)); 3106d4665eaaSJeff Roberson } 3107d4665eaaSJeff Roberson 31089c2cd7e5SJeff Roberson /* See uma.h */ 31098355f576SJeff Roberson void * 31102cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 31118355f576SJeff Roberson { 3112376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3113ab3185d1SJeff Roberson uma_cache_t cache; 3114ab3185d1SJeff Roberson void *item; 3115d4665eaaSJeff Roberson int size, uz_flags; 31168355f576SJeff Roberson 3117e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 311819fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 311910cb2424SMark Murray 31208355f576SJeff Roberson /* This is the fast path allocation */ 3121e63a1c2fSRyan Libby CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name, 3122e63a1c2fSRyan Libby zone, flags); 3123a553d4b8SJeff Roberson 3124d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3125d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 3126d4665eaaSJeff Roberson ("uma_zalloc_arg: called with SMR zone.\n")); 3127d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN) 31288d689e04SGleb Smirnoff return (item); 31298d689e04SGleb Smirnoff #endif 3130d4665eaaSJeff Roberson 31315d1ae027SRobert Watson /* 31325d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 31335d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 31345d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 31355d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 31365d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 31375d1ae027SRobert Watson * preemption and migration. We release the critical section in 31385d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 31395d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 31405d1ae027SRobert Watson * must detect and handle migration if it has occurred. 31415d1ae027SRobert Watson */ 31425d1ae027SRobert Watson critical_enter(); 3143beb8beefSJeff Roberson do { 3144cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3145376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 3146cc7ce83aSJeff Roberson size = cache_uz_size(cache); 3147cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3148376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 3149376b1ba3SJeff Roberson item = cache_bucket_pop(cache, bucket); 31505d1ae027SRobert Watson critical_exit(); 3151d4665eaaSJeff Roberson return (item_ctor(zone, uz_flags, size, udata, flags, 3152d4665eaaSJeff Roberson item)); 3153b23f72e9SBrian Feldman } 3154beb8beefSJeff Roberson } while (cache_alloc(zone, cache, udata, flags)); 3155beb8beefSJeff Roberson critical_exit(); 3156beb8beefSJeff Roberson 3157d4665eaaSJeff Roberson return (uma_zalloc_single(zone, udata, flags)); 3158fc03d22bSJeff Roberson } 3159fc03d22bSJeff Roberson 31608355f576SJeff Roberson /* 3161beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 3162beb8beefSJeff Roberson * a critical section. Returns in a critical section. 3163beb8beefSJeff Roberson * 31644bd61e19SJeff Roberson * A false return value indicates an allocation failure. 31654bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 3166beb8beefSJeff Roberson */ 3167beb8beefSJeff Roberson static __noinline bool 3168beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3169beb8beefSJeff Roberson { 3170beb8beefSJeff Roberson uma_zone_domain_t zdom; 3171beb8beefSJeff Roberson uma_bucket_t bucket; 3172cc7ce83aSJeff Roberson int domain; 3173beb8beefSJeff Roberson bool lockfail; 3174beb8beefSJeff Roberson 3175beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3176beb8beefSJeff Roberson 3177beb8beefSJeff Roberson /* 3178beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3179beb8beefSJeff Roberson * if we can switch with the free bucket. 3180d4665eaaSJeff Roberson * 3181d4665eaaSJeff Roberson * SMR Zones can't re-use the free bucket until the sequence has 3182d4665eaaSJeff Roberson * expired. 31838355f576SJeff Roberson */ 3184d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && 3185d4665eaaSJeff Roberson cache->uc_freebucket.ucb_cnt != 0) { 3186d4665eaaSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 3187d4665eaaSJeff Roberson &cache->uc_allocbucket); 3188beb8beefSJeff Roberson return (true); 31898355f576SJeff Roberson } 3190fc03d22bSJeff Roberson 3191fc03d22bSJeff Roberson /* 3192fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3193fc03d22bSJeff Roberson */ 3194376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3195fc03d22bSJeff Roberson critical_exit(); 3196fc03d22bSJeff Roberson if (bucket != NULL) 31976fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3198fc03d22bSJeff Roberson 31994bd61e19SJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 32004bd61e19SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) { 32014bd61e19SJeff Roberson critical_enter(); 32024bd61e19SJeff Roberson return (false); 32034bd61e19SJeff Roberson } 32044bd61e19SJeff Roberson 32055d1ae027SRobert Watson /* 32065d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 32075d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 32085d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 32095d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 32105d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 32115d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 32125d1ae027SRobert Watson * the critical section. 32135d1ae027SRobert Watson */ 3214fc03d22bSJeff Roberson lockfail = 0; 3215fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 3216fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 3217a553d4b8SJeff Roberson ZONE_LOCK(zone); 3218fc03d22bSJeff Roberson lockfail = 1; 3219fc03d22bSJeff Roberson } 3220beb8beefSJeff Roberson 3221fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 32224bd61e19SJeff Roberson critical_enter(); 32234bd61e19SJeff Roberson cache = &zone->uz_cpu[curcpu]; 3224376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket != NULL) { 3225fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 3226beb8beefSJeff Roberson return (true); 3227a553d4b8SJeff Roberson } 32288355f576SJeff Roberson 3229fc03d22bSJeff Roberson /* 3230fc03d22bSJeff Roberson * Check the zone's cache of buckets. 3231fc03d22bSJeff Roberson */ 3232dfe13344SJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) { 3233c1685086SJeff Roberson domain = PCPU_GET(domain); 3234ab3185d1SJeff Roberson zdom = &zone->uz_domain[domain]; 3235c1685086SJeff Roberson } else { 3236c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3237c1685086SJeff Roberson zdom = &zone->uz_domain[0]; 3238c1685086SJeff Roberson } 3239c1685086SJeff Roberson 324008cfa56eSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom)) != NULL) { 3241cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 3242a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 3243376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3244beb8beefSJeff Roberson return (true); 3245a553d4b8SJeff Roberson } 32465d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 32475d1ae027SRobert Watson critical_exit(); 3248bbee39c6SJeff Roberson 3249fc03d22bSJeff Roberson /* 3250fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 3251fc03d22bSJeff Roberson * handle the working set. 3252fc03d22bSJeff Roberson */ 325320a4e154SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 325420a4e154SJeff Roberson zone->uz_bucket_size++; 32554bd61e19SJeff Roberson ZONE_UNLOCK(zone); 3256bb15d1c7SGleb Smirnoff 32578355f576SJeff Roberson /* 3258beb8beefSJeff Roberson * Fill a bucket and attempt to use it as the alloc bucket. 3259bbee39c6SJeff Roberson */ 3260beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 32611431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 32621431a748SGleb Smirnoff zone->uz_name, zone, bucket); 32634bd61e19SJeff Roberson if (bucket == NULL) { 3264fc03d22bSJeff Roberson critical_enter(); 3265beb8beefSJeff Roberson return (false); 32664bd61e19SJeff Roberson } 32670f9b7bf3SMark Johnston 3268fc03d22bSJeff Roberson /* 3269fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3270fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3271fc03d22bSJeff Roberson * the memory directly. 3272fc03d22bSJeff Roberson */ 32734bd61e19SJeff Roberson ZONE_LOCK(zone); 32744bd61e19SJeff Roberson critical_enter(); 3275cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3276376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 3277dfe13344SJeff Roberson ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0 || 327881c0d72cSGleb Smirnoff domain == PCPU_GET(domain))) { 3279376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 32800f9b7bf3SMark Johnston zdom->uzd_imax += bucket->ub_cnt; 3281bb15d1c7SGleb Smirnoff } else if (zone->uz_bkt_count >= zone->uz_bkt_max) { 328281c0d72cSGleb Smirnoff critical_exit(); 328381c0d72cSGleb Smirnoff ZONE_UNLOCK(zone); 328481c0d72cSGleb Smirnoff bucket_drain(zone, bucket); 328581c0d72cSGleb Smirnoff bucket_free(zone, bucket, udata); 3286beb8beefSJeff Roberson critical_enter(); 3287beb8beefSJeff Roberson return (true); 328881c0d72cSGleb Smirnoff } else 32890f9b7bf3SMark Johnston zone_put_bucket(zone, zdom, bucket, false); 3290bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 3291beb8beefSJeff Roberson return (true); 3292bbee39c6SJeff Roberson } 3293bbee39c6SJeff Roberson 3294ab3185d1SJeff Roberson void * 3295ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3296bbee39c6SJeff Roberson { 3297ab3185d1SJeff Roberson 3298ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 329919fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3300ab3185d1SJeff Roberson 3301ab3185d1SJeff Roberson /* This is the fast path allocation */ 3302e63a1c2fSRyan Libby CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d", 3303e63a1c2fSRyan Libby zone->uz_name, zone, domain, flags); 3304ab3185d1SJeff Roberson 3305ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3306ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3307ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3308ab3185d1SJeff Roberson } 3309ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3310ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 3311ab3185d1SJeff Roberson 3312ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3313ab3185d1SJeff Roberson } 3314ab3185d1SJeff Roberson 3315ab3185d1SJeff Roberson /* 3316ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3317ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3318ab3185d1SJeff Roberson * 3319ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3320ab3185d1SJeff Roberson * only 'domain'. 3321ab3185d1SJeff Roberson */ 3322ab3185d1SJeff Roberson static uma_slab_t 3323194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3324ab3185d1SJeff Roberson { 3325ab3185d1SJeff Roberson uma_domain_t dom; 3326bbee39c6SJeff Roberson uma_slab_t slab; 3327ab3185d1SJeff Roberson int start; 3328ab3185d1SJeff Roberson 3329ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3330ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 33318b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3332ab3185d1SJeff Roberson 3333ab3185d1SJeff Roberson slab = NULL; 3334ab3185d1SJeff Roberson start = domain; 3335ab3185d1SJeff Roberson do { 3336ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 3337ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_part_slab)) 3338ab3185d1SJeff Roberson return (LIST_FIRST(&dom->ud_part_slab)); 3339ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_free_slab)) { 3340ab3185d1SJeff Roberson slab = LIST_FIRST(&dom->ud_free_slab); 3341ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 3342ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3343ab3185d1SJeff Roberson return (slab); 3344ab3185d1SJeff Roberson } 3345ab3185d1SJeff Roberson if (rr) 3346ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3347ab3185d1SJeff Roberson } while (domain != start); 3348ab3185d1SJeff Roberson 3349ab3185d1SJeff Roberson return (NULL); 3350ab3185d1SJeff Roberson } 3351ab3185d1SJeff Roberson 33528b987a77SJeff Roberson /* 33538b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 33548b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 33558b987a77SJeff Roberson */ 3356ab3185d1SJeff Roberson static uma_slab_t 3357194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3358ab3185d1SJeff Roberson { 33598b987a77SJeff Roberson uma_slab_t slab; 3360194a979eSMark Johnston uint32_t reserve; 3361099a0e58SBosko Milekic 33628b987a77SJeff Roberson /* HASH has a single free list. */ 336354c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 33648b987a77SJeff Roberson domain = 0; 3365194a979eSMark Johnston 33668b987a77SJeff Roberson KEG_LOCK(keg, domain); 3367194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 33688b987a77SJeff Roberson if (keg->uk_domain[domain].ud_free <= reserve || 33698b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 33708b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3371194a979eSMark Johnston return (NULL); 33728b987a77SJeff Roberson } 33738b987a77SJeff Roberson return (slab); 3374194a979eSMark Johnston } 3375194a979eSMark Johnston 3376194a979eSMark Johnston static uma_slab_t 3377194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3378194a979eSMark Johnston { 3379194a979eSMark Johnston struct vm_domainset_iter di; 3380194a979eSMark Johnston uma_slab_t slab; 3381194a979eSMark Johnston int aflags, domain; 3382194a979eSMark Johnston bool rr; 3383194a979eSMark Johnston 3384194a979eSMark Johnston restart: 3385bbee39c6SJeff Roberson /* 3386194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3387194a979eSMark Johnston * domain (as happens with first-touch zones). 3388194a979eSMark Johnston * 3389194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3390194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3391194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3392bbee39c6SJeff Roberson */ 3393ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3394ab3185d1SJeff Roberson if (rr) { 3395194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3396194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3397194a979eSMark Johnston &aflags); 3398194a979eSMark Johnston } else { 3399194a979eSMark Johnston aflags = flags; 3400194a979eSMark Johnston domain = rdomain; 3401194a979eSMark Johnston } 3402ab3185d1SJeff Roberson 3403194a979eSMark Johnston for (;;) { 3404194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3405584061b4SJeff Roberson if (slab != NULL) 3406bbee39c6SJeff Roberson return (slab); 3407bbee39c6SJeff Roberson 3408bbee39c6SJeff Roberson /* 3409bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3410bbee39c6SJeff Roberson */ 3411bbee39c6SJeff Roberson if (flags & M_NOVM) 3412bbee39c6SJeff Roberson break; 3413bbee39c6SJeff Roberson 341486220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 34158b987a77SJeff Roberson if (slab != NULL) 3416bbee39c6SJeff Roberson return (slab); 34173639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 34183639ac42SJeff Roberson break; 3419194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3420194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 3421194a979eSMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 3422194a979eSMark Johnston goto restart; 342330c5525bSAndrew Gallatin } 3424194a979eSMark Johnston break; 3425194a979eSMark Johnston } 3426ab3185d1SJeff Roberson } 3427ab3185d1SJeff Roberson 3428bbee39c6SJeff Roberson /* 3429bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3430bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3431bbee39c6SJeff Roberson * fail. 3432bbee39c6SJeff Roberson */ 34338b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 3434bbee39c6SJeff Roberson return (slab); 34358b987a77SJeff Roberson 3436ab3185d1SJeff Roberson return (NULL); 3437ab3185d1SJeff Roberson } 3438bbee39c6SJeff Roberson 3439d56368d7SBosko Milekic static void * 34400095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3441bbee39c6SJeff Roberson { 3442ab3185d1SJeff Roberson uma_domain_t dom; 3443bbee39c6SJeff Roberson void *item; 34449b8db4d0SRyan Libby int freei; 3445bbee39c6SJeff Roberson 34468b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3447099a0e58SBosko Milekic 34488b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 34499b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 34509b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 34511e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3452bbee39c6SJeff Roberson slab->us_freecount--; 34538b987a77SJeff Roberson dom->ud_free--; 3454ef72505eSJeff Roberson 3455bbee39c6SJeff Roberson /* Move this slab to the full list */ 3456bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3457bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3458ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3459bbee39c6SJeff Roberson } 3460bbee39c6SJeff Roberson 3461bbee39c6SJeff Roberson return (item); 3462bbee39c6SJeff Roberson } 3463bbee39c6SJeff Roberson 3464bbee39c6SJeff Roberson static int 3465b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 34660095a784SJeff Roberson { 34678b987a77SJeff Roberson uma_domain_t dom; 3468b75c4efcSAndrew Turner uma_zone_t zone; 34690095a784SJeff Roberson uma_slab_t slab; 34700095a784SJeff Roberson uma_keg_t keg; 3471a03af342SSean Bruno #ifdef NUMA 3472ab3185d1SJeff Roberson int stripe; 3473a03af342SSean Bruno #endif 34740095a784SJeff Roberson int i; 34750095a784SJeff Roberson 3476b75c4efcSAndrew Turner zone = arg; 34770095a784SJeff Roberson slab = NULL; 3478584061b4SJeff Roberson keg = zone->uz_keg; 3479af526374SJeff Roberson /* Try to keep the buckets totally full */ 34800095a784SJeff Roberson for (i = 0; i < max; ) { 3481584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 34820095a784SJeff Roberson break; 3483a03af342SSean Bruno #ifdef NUMA 3484ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3485a03af342SSean Bruno #endif 34868b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 34876fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 34880095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 34898b987a77SJeff Roberson if (dom->ud_free <= keg->uk_reserve) 34906fd34d6fSJeff Roberson break; 3491b6715dabSJeff Roberson #ifdef NUMA 3492ab3185d1SJeff Roberson /* 3493ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3494ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3495ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3496ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3497ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3498ab3185d1SJeff Roberson * time but yields better distribution. 3499ab3185d1SJeff Roberson */ 3500dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 && 3501ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3502ab3185d1SJeff Roberson break; 3503ab3185d1SJeff Roberson #endif 35046fd34d6fSJeff Roberson } 35058b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 3506ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 35070095a784SJeff Roberson flags &= ~M_WAITOK; 35080095a784SJeff Roberson flags |= M_NOWAIT; 35090095a784SJeff Roberson } 35100095a784SJeff Roberson 35110095a784SJeff Roberson return i; 35120095a784SJeff Roberson } 35130095a784SJeff Roberson 35144bd61e19SJeff Roberson static int 35154bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 35164bd61e19SJeff Roberson { 35174bd61e19SJeff Roberson uint64_t old, new, total, max; 35184bd61e19SJeff Roberson 35194bd61e19SJeff Roberson /* 35204bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 35214bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 35224bd61e19SJeff Roberson * fairness by keeping fifo order. 35234bd61e19SJeff Roberson * 35244bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 35254bd61e19SJeff Roberson */ 35264bd61e19SJeff Roberson for (;;) { 35274bd61e19SJeff Roberson zone_free_limit(zone, count); 35284bd61e19SJeff Roberson zone_log_warning(zone); 35294bd61e19SJeff Roberson zone_maxaction(zone); 35304bd61e19SJeff Roberson if (flags & M_NOWAIT) 35314bd61e19SJeff Roberson return (0); 35324bd61e19SJeff Roberson 35334bd61e19SJeff Roberson /* 35344bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 35354bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 35364bd61e19SJeff Roberson * is essentially a home rolled semaphore. 35374bd61e19SJeff Roberson */ 35384bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 35394bd61e19SJeff Roberson old = zone->uz_items; 35404bd61e19SJeff Roberson do { 35414bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 35424bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 35434bd61e19SJeff Roberson max = zone->uz_max_items; 35444bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 35454bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 35464bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 35474bd61e19SJeff Roberson else 35484bd61e19SJeff Roberson new = old + MIN(count, max - old); 35494bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 35504bd61e19SJeff Roberson 35514bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 35524bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 35534bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 35544bd61e19SJeff Roberson return (new - old); 35554bd61e19SJeff Roberson } 35564bd61e19SJeff Roberson 35574bd61e19SJeff Roberson /* 35584bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 35594bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 35604bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 35614bd61e19SJeff Roberson * transitions. 35624bd61e19SJeff Roberson */ 35634bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 35644bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 35654bd61e19SJeff Roberson 35664bd61e19SJeff Roberson /* 35674bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 35684bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 35694bd61e19SJeff Roberson */ 35704bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 35714bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 35724bd61e19SJeff Roberson 35734bd61e19SJeff Roberson /* 35744bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 35754bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 35764bd61e19SJeff Roberson * 35774bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 35784bd61e19SJeff Roberson * our count. 35794bd61e19SJeff Roberson */ 35804bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 35814bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 35824bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 35834bd61e19SJeff Roberson /* We're no longer a sleeper. */ 35844bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 35854bd61e19SJeff Roberson 35864bd61e19SJeff Roberson /* 35874bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 35884bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 35894bd61e19SJeff Roberson * against changes via sysctl. 35904bd61e19SJeff Roberson */ 35914bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 35924bd61e19SJeff Roberson max = zone->uz_max_items; 35934bd61e19SJeff Roberson if (total >= max) 35944bd61e19SJeff Roberson continue; 35954bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 35964bd61e19SJeff Roberson if (total + count > max) { 35974bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 35984bd61e19SJeff Roberson count = max - total; 35994bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 36004bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 36014bd61e19SJeff Roberson 36024bd61e19SJeff Roberson return (count); 36034bd61e19SJeff Roberson } 36044bd61e19SJeff Roberson } 36054bd61e19SJeff Roberson 36064bd61e19SJeff Roberson /* 36074bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 36084bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 36094bd61e19SJeff Roberson * one item can be allocated. 36104bd61e19SJeff Roberson */ 36114bd61e19SJeff Roberson static int 36124bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 36134bd61e19SJeff Roberson { 36144bd61e19SJeff Roberson uint64_t old; 36154bd61e19SJeff Roberson uint64_t max; 36164bd61e19SJeff Roberson 36174bd61e19SJeff Roberson max = zone->uz_max_items; 36184bd61e19SJeff Roberson MPASS(max > 0); 36194bd61e19SJeff Roberson 36204bd61e19SJeff Roberson /* 36214bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 36224bd61e19SJeff Roberson * fetchadd. 36234bd61e19SJeff Roberson */ 36244bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 36254bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 36264bd61e19SJeff Roberson return (count); 36274bd61e19SJeff Roberson 36284bd61e19SJeff Roberson /* 36294bd61e19SJeff Roberson * If we had some items and no sleepers just return the 36304bd61e19SJeff Roberson * truncated value. We have to release the excess space 36314bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 36324bd61e19SJeff Roberson * because we were temporarily over the limit. 36334bd61e19SJeff Roberson */ 36344bd61e19SJeff Roberson if (old < max) { 36354bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 36364bd61e19SJeff Roberson return (max - old); 36374bd61e19SJeff Roberson } 36384bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 36394bd61e19SJeff Roberson } 36404bd61e19SJeff Roberson 36414bd61e19SJeff Roberson /* 36424bd61e19SJeff Roberson * Free a number of items back to the limit. 36434bd61e19SJeff Roberson */ 36444bd61e19SJeff Roberson static void 36454bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 36464bd61e19SJeff Roberson { 36474bd61e19SJeff Roberson uint64_t old; 36484bd61e19SJeff Roberson 36494bd61e19SJeff Roberson MPASS(count > 0); 36504bd61e19SJeff Roberson 36514bd61e19SJeff Roberson /* 36524bd61e19SJeff Roberson * In the common case we either have no sleepers or 36534bd61e19SJeff Roberson * are still over the limit and can just return. 36544bd61e19SJeff Roberson */ 36554bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 36564bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 36574bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 36584bd61e19SJeff Roberson return; 36594bd61e19SJeff Roberson 36604bd61e19SJeff Roberson /* 36614bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 36624bd61e19SJeff Roberson * to generate wakeups if necessary. 36634bd61e19SJeff Roberson */ 36644bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 36654bd61e19SJeff Roberson } 36664bd61e19SJeff Roberson 3667fc03d22bSJeff Roberson static uma_bucket_t 3668beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3669bbee39c6SJeff Roberson { 3670bbee39c6SJeff Roberson uma_bucket_t bucket; 3671beb8beefSJeff Roberson int maxbucket, cnt; 3672bbee39c6SJeff Roberson 3673e63a1c2fSRyan Libby CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name, 3674e63a1c2fSRyan Libby zone, domain); 367530c5525bSAndrew Gallatin 3676c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3677c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3678c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3679c1685086SJeff Roberson 36804bd61e19SJeff Roberson if (zone->uz_max_items > 0) 36814bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 36824bd61e19SJeff Roberson M_NOWAIT); 36834bd61e19SJeff Roberson else 368420a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 36854bd61e19SJeff Roberson if (maxbucket == 0) 36864bd61e19SJeff Roberson return (false); 3687beb8beefSJeff Roberson 36886fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 36896fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3690beb8beefSJeff Roberson if (bucket == NULL) { 3691beb8beefSJeff Roberson cnt = 0; 3692beb8beefSJeff Roberson goto out; 3693beb8beefSJeff Roberson } 36940095a784SJeff Roberson 36950095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3696beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 36970095a784SJeff Roberson 36980095a784SJeff Roberson /* 36990095a784SJeff Roberson * Initialize the memory if necessary. 37000095a784SJeff Roberson */ 37010095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 3702099a0e58SBosko Milekic int i; 3703bbee39c6SJeff Roberson 37040095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 3705e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 37060095a784SJeff Roberson flags) != 0) 3707b23f72e9SBrian Feldman break; 3708b23f72e9SBrian Feldman /* 3709b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 3710b23f72e9SBrian Feldman * rest back onto the freelist. 3711b23f72e9SBrian Feldman */ 3712b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 3713af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 37140095a784SJeff Roberson bucket->ub_cnt - i); 3715a5a262c6SBosko Milekic #ifdef INVARIANTS 37160095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 37170095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 3718a5a262c6SBosko Milekic #endif 3719b23f72e9SBrian Feldman bucket->ub_cnt = i; 3720b23f72e9SBrian Feldman } 3721099a0e58SBosko Milekic } 3722099a0e58SBosko Milekic 3723beb8beefSJeff Roberson cnt = bucket->ub_cnt; 3724f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 37256fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 37262efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 3727beb8beefSJeff Roberson bucket = NULL; 3728beb8beefSJeff Roberson } 3729beb8beefSJeff Roberson out: 37304bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 37314bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 3732fc03d22bSJeff Roberson 3733fc03d22bSJeff Roberson return (bucket); 3734fc03d22bSJeff Roberson } 3735fc03d22bSJeff Roberson 37368355f576SJeff Roberson /* 37370095a784SJeff Roberson * Allocates a single item from a zone. 37388355f576SJeff Roberson * 37398355f576SJeff Roberson * Arguments 37408355f576SJeff Roberson * zone The zone to alloc for. 37418355f576SJeff Roberson * udata The data to be passed to the constructor. 3742ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 3743a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 37448355f576SJeff Roberson * 37458355f576SJeff Roberson * Returns 37468355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 3747bbee39c6SJeff Roberson * An item if successful 37488355f576SJeff Roberson */ 37498355f576SJeff Roberson 37508355f576SJeff Roberson static void * 3751ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 37528355f576SJeff Roberson { 37538355f576SJeff Roberson void *item; 37548355f576SJeff Roberson 37554bd61e19SJeff Roberson if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) 3756bb15d1c7SGleb Smirnoff return (NULL); 37578355f576SJeff Roberson 3758c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3759c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 376030c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 3761c1685086SJeff Roberson 3762ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 3763beb8beefSJeff Roberson goto fail_cnt; 37648355f576SJeff Roberson 3765099a0e58SBosko Milekic /* 3766099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 3767099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 3768099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 3769099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 3770099a0e58SBosko Milekic */ 3771b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 3772e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 3773bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 3774beb8beefSJeff Roberson goto fail_cnt; 3775beb8beefSJeff Roberson } 3776beb8beefSJeff Roberson } 3777d4665eaaSJeff Roberson item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags, 3778d4665eaaSJeff Roberson item); 3779beb8beefSJeff Roberson if (item == NULL) 37800095a784SJeff Roberson goto fail; 37818355f576SJeff Roberson 37822efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 37831431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 37841431a748SGleb Smirnoff zone->uz_name, zone); 37851431a748SGleb Smirnoff 37868355f576SJeff Roberson return (item); 37870095a784SJeff Roberson 3788beb8beefSJeff Roberson fail_cnt: 3789beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 37900095a784SJeff Roberson fail: 37914bd61e19SJeff Roberson if (zone->uz_max_items > 0) 37924bd61e19SJeff Roberson zone_free_limit(zone, 1); 37931431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 37941431a748SGleb Smirnoff zone->uz_name, zone); 37954bd61e19SJeff Roberson 37960095a784SJeff Roberson return (NULL); 37978355f576SJeff Roberson } 37988355f576SJeff Roberson 37998355f576SJeff Roberson /* See uma.h */ 38008355f576SJeff Roberson void 3801d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item) 3802d4665eaaSJeff Roberson { 3803d4665eaaSJeff Roberson uma_cache_t cache; 3804d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 3805d4665eaaSJeff Roberson int domain, itemdomain, uz_flags; 3806d4665eaaSJeff Roberson 3807d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3808d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 3809d4665eaaSJeff Roberson ("uma_zfree_smr: called with non-SMR zone.\n")); 3810d4665eaaSJeff Roberson KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer.")); 3811d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN) 3812d4665eaaSJeff Roberson return; 3813d4665eaaSJeff Roberson #endif 3814d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3815d4665eaaSJeff Roberson uz_flags = cache_uz_flags(cache); 3816d4665eaaSJeff Roberson domain = itemdomain = 0; 3817d4665eaaSJeff Roberson #ifdef NUMA 3818d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 3819d4665eaaSJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 3820d4665eaaSJeff Roberson #endif 3821d4665eaaSJeff Roberson critical_enter(); 3822d4665eaaSJeff Roberson do { 3823d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3824d4665eaaSJeff Roberson /* SMR Zones must free to the free bucket. */ 3825d4665eaaSJeff Roberson bucket = &cache->uc_freebucket; 3826d4665eaaSJeff Roberson #ifdef NUMA 3827d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3828d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 3829d4665eaaSJeff Roberson domain != itemdomain) { 3830d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 3831d4665eaaSJeff Roberson } 3832d4665eaaSJeff Roberson #endif 3833d4665eaaSJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3834d4665eaaSJeff Roberson cache_bucket_push(cache, bucket, item); 3835d4665eaaSJeff Roberson critical_exit(); 3836d4665eaaSJeff Roberson return; 3837d4665eaaSJeff Roberson } 3838d4665eaaSJeff Roberson } while (cache_free(zone, cache, NULL, item, itemdomain)); 3839d4665eaaSJeff Roberson critical_exit(); 3840d4665eaaSJeff Roberson 3841d4665eaaSJeff Roberson /* 3842d4665eaaSJeff Roberson * If nothing else caught this, we'll just do an internal free. 3843d4665eaaSJeff Roberson */ 3844d4665eaaSJeff Roberson zone_free_item(zone, item, NULL, SKIP_NONE); 3845d4665eaaSJeff Roberson } 3846d4665eaaSJeff Roberson 3847d4665eaaSJeff Roberson /* See uma.h */ 3848d4665eaaSJeff Roberson void 38498355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 38508355f576SJeff Roberson { 38518355f576SJeff Roberson uma_cache_t cache; 3852376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3853cc7ce83aSJeff Roberson int domain, itemdomain, uz_flags; 38548355f576SJeff Roberson 3855e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 385619fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 385710cb2424SMark Murray 3858e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone); 38593659f747SRobert Watson 3860d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3861d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 3862d4665eaaSJeff Roberson ("uma_zfree_arg: called with SMR zone.\n")); 3863d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN) 3864d4665eaaSJeff Roberson return; 3865d4665eaaSJeff Roberson #endif 386620ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 386720ed0cb0SMatthew D Fleming if (item == NULL) 386820ed0cb0SMatthew D Fleming return; 3869cc7ce83aSJeff Roberson 3870cc7ce83aSJeff Roberson /* 3871cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 3872cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 3873cc7ce83aSJeff Roberson * will simply read another cpu's line. 3874cc7ce83aSJeff Roberson */ 3875cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3876cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3877d4665eaaSJeff Roberson if (UMA_ALWAYS_CTORDTOR || 3878d4665eaaSJeff Roberson __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0)) 3879cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 3880ef72505eSJeff Roberson 3881af7f9b97SJeff Roberson /* 3882af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 3883af7f9b97SJeff Roberson * a little longer for the limits to be reset. 3884af7f9b97SJeff Roberson */ 3885cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 3886bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0) 3887fc03d22bSJeff Roberson goto zfree_item; 3888cc7ce83aSJeff Roberson } 3889af7f9b97SJeff Roberson 38905d1ae027SRobert Watson /* 38915d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 38925d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 38935d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 38945d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 38955d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 38965d1ae027SRobert Watson * preemption and migration. We release the critical section in 38975d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 38985d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 38995d1ae027SRobert Watson * detect and handle migration if it has occurred. 39005d1ae027SRobert Watson */ 39010a81b439SJeff Roberson domain = itemdomain = 0; 3902dfe13344SJeff Roberson #ifdef NUMA 3903dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 3904dfe13344SJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 3905dfe13344SJeff Roberson #endif 39065d1ae027SRobert Watson critical_enter(); 39070a81b439SJeff Roberson do { 3908cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3909a553d4b8SJeff Roberson /* 3910dfe13344SJeff Roberson * Try to free into the allocbucket first to give LIFO 3911dfe13344SJeff Roberson * ordering for cache-hot datastructures. Spill over 3912dfe13344SJeff Roberson * into the freebucket if necessary. Alloc will swap 3913dfe13344SJeff Roberson * them if one runs dry. 3914a553d4b8SJeff Roberson */ 3915dfe13344SJeff Roberson bucket = &cache->uc_allocbucket; 3916d4665eaaSJeff Roberson #ifdef NUMA 3917d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3918d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 3919d4665eaaSJeff Roberson domain != itemdomain) { 3920d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 3921d4665eaaSJeff Roberson } else 3922d4665eaaSJeff Roberson #endif 3923d4665eaaSJeff Roberson if (bucket->ucb_cnt >= bucket->ucb_entries) 3924376b1ba3SJeff Roberson bucket = &cache->uc_freebucket; 3925376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3926376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 39275d1ae027SRobert Watson critical_exit(); 39288355f576SJeff Roberson return; 3929fc03d22bSJeff Roberson } 39300a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 39310a81b439SJeff Roberson critical_exit(); 3932fc03d22bSJeff Roberson 39338355f576SJeff Roberson /* 39340a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 39358355f576SJeff Roberson */ 39360a81b439SJeff Roberson zfree_item: 39370a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 39380a81b439SJeff Roberson } 3939fc03d22bSJeff Roberson 3940dfe13344SJeff Roberson #ifdef NUMA 394191d947bfSJeff Roberson /* 394291d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 394391d947bfSJeff Roberson * them. 394491d947bfSJeff Roberson */ 394591d947bfSJeff Roberson static void 394691d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 394791d947bfSJeff Roberson { 394891d947bfSJeff Roberson struct uma_bucketlist fullbuckets; 394991d947bfSJeff Roberson uma_zone_domain_t zdom; 395091d947bfSJeff Roberson uma_bucket_t b; 395191d947bfSJeff Roberson void *item; 395291d947bfSJeff Roberson int domain; 395391d947bfSJeff Roberson 395491d947bfSJeff Roberson CTR3(KTR_UMA, 395591d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 395691d947bfSJeff Roberson zone->uz_name, zone, bucket); 395791d947bfSJeff Roberson 3958dc3915c8SJeff Roberson STAILQ_INIT(&fullbuckets); 395991d947bfSJeff Roberson 396091d947bfSJeff Roberson /* 396191d947bfSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 396291d947bfSJeff Roberson * lock on the current crossfree bucket. A full matrix with 396391d947bfSJeff Roberson * per-domain locking could be used if necessary. 396491d947bfSJeff Roberson */ 396591d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 396691d947bfSJeff Roberson while (bucket->ub_cnt > 0) { 396791d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 396891d947bfSJeff Roberson domain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 396991d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 397091d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 397191d947bfSJeff Roberson zdom->uzd_cross = bucket_alloc(zone, udata, M_NOWAIT); 397291d947bfSJeff Roberson if (zdom->uzd_cross == NULL) 397391d947bfSJeff Roberson break; 397491d947bfSJeff Roberson } 397591d947bfSJeff Roberson zdom->uzd_cross->ub_bucket[zdom->uzd_cross->ub_cnt++] = item; 397691d947bfSJeff Roberson if (zdom->uzd_cross->ub_cnt == zdom->uzd_cross->ub_entries) { 3977dc3915c8SJeff Roberson STAILQ_INSERT_HEAD(&fullbuckets, zdom->uzd_cross, 397891d947bfSJeff Roberson ub_link); 397991d947bfSJeff Roberson zdom->uzd_cross = NULL; 398091d947bfSJeff Roberson } 398191d947bfSJeff Roberson bucket->ub_cnt--; 398291d947bfSJeff Roberson } 398391d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 3984dc3915c8SJeff Roberson if (!STAILQ_EMPTY(&fullbuckets)) { 398591d947bfSJeff Roberson ZONE_LOCK(zone); 3986dc3915c8SJeff Roberson while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) { 3987d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 3988d4665eaaSJeff Roberson bucket->ub_seq = smr_current(zone->uz_smr); 3989dc3915c8SJeff Roberson STAILQ_REMOVE_HEAD(&fullbuckets, ub_link); 399091d947bfSJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 399191d947bfSJeff Roberson ZONE_UNLOCK(zone); 399291d947bfSJeff Roberson bucket_drain(zone, b); 399391d947bfSJeff Roberson bucket_free(zone, b, udata); 399491d947bfSJeff Roberson ZONE_LOCK(zone); 399591d947bfSJeff Roberson } else { 399691d947bfSJeff Roberson domain = _vm_phys_domain( 399791d947bfSJeff Roberson pmap_kextract( 399891d947bfSJeff Roberson (vm_offset_t)b->ub_bucket[0])); 399991d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 400091d947bfSJeff Roberson zone_put_bucket(zone, zdom, b, true); 400191d947bfSJeff Roberson } 400291d947bfSJeff Roberson } 400391d947bfSJeff Roberson ZONE_UNLOCK(zone); 400491d947bfSJeff Roberson } 400591d947bfSJeff Roberson if (bucket->ub_cnt != 0) 400691d947bfSJeff Roberson bucket_drain(zone, bucket); 4007d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 400891d947bfSJeff Roberson bucket_free(zone, bucket, udata); 400991d947bfSJeff Roberson } 401091d947bfSJeff Roberson #endif 401191d947bfSJeff Roberson 40120a81b439SJeff Roberson static void 40130a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 40140a81b439SJeff Roberson int domain, int itemdomain) 40150a81b439SJeff Roberson { 40160a81b439SJeff Roberson uma_zone_domain_t zdom; 40170a81b439SJeff Roberson 4018dfe13344SJeff Roberson #ifdef NUMA 40190a81b439SJeff Roberson /* 40200a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 40210a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 40220a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 402391d947bfSJeff Roberson * correct domains. 40240a81b439SJeff Roberson */ 40250a81b439SJeff Roberson if (domain != itemdomain && vm_ndomains > 2) { 402691d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 40270a81b439SJeff Roberson return; 40280a81b439SJeff Roberson } 40290a81b439SJeff Roberson #endif 403091d947bfSJeff Roberson 40310a81b439SJeff Roberson /* 40320a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 40330a81b439SJeff Roberson * 40340a81b439SJeff Roberson * We bump the uz count when the cache size is insufficient to 40350a81b439SJeff Roberson * handle the working set. 40360a81b439SJeff Roberson */ 40374d104ba0SAlexander Motin if (ZONE_TRYLOCK(zone) == 0) { 40384d104ba0SAlexander Motin /* Record contention to size the buckets. */ 40398355f576SJeff Roberson ZONE_LOCK(zone); 404020a4e154SJeff Roberson if (zone->uz_bucket_size < zone->uz_bucket_size_max) 404120a4e154SJeff Roberson zone->uz_bucket_size++; 40424d104ba0SAlexander Motin } 40438355f576SJeff Roberson 40440a81b439SJeff Roberson CTR3(KTR_UMA, 40450a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 40460a81b439SJeff Roberson zone->uz_name, zone, bucket); 40470a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 40480a81b439SJeff Roberson KASSERT(bucket->ub_cnt == bucket->ub_entries, 40490a81b439SJeff Roberson ("uma_zfree: Attempting to insert partial bucket onto the full list.\n")); 40500a81b439SJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 4051c1685086SJeff Roberson ZONE_UNLOCK(zone); 4052c1685086SJeff Roberson bucket_drain(zone, bucket); 4053c1685086SJeff Roberson bucket_free(zone, bucket, udata); 4054c1685086SJeff Roberson } else { 4055c1685086SJeff Roberson zdom = &zone->uz_domain[itemdomain]; 4056c1685086SJeff Roberson zone_put_bucket(zone, zdom, bucket, true); 4057c1685086SJeff Roberson ZONE_UNLOCK(zone); 4058c1685086SJeff Roberson } 40598355f576SJeff Roberson } 4060fc03d22bSJeff Roberson 40614d104ba0SAlexander Motin /* 40620a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 40630a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 40640a81b439SJeff Roberson * 40650a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 40660a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 40670a81b439SJeff Roberson * the caller should retry. 40684d104ba0SAlexander Motin */ 40690a81b439SJeff Roberson static __noinline bool 40700a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 40710a81b439SJeff Roberson int itemdomain) 40720a81b439SJeff Roberson { 4073dfe13344SJeff Roberson uma_cache_bucket_t cbucket; 4074d4665eaaSJeff Roberson uma_bucket_t newbucket, bucket; 4075cc7ce83aSJeff Roberson int domain; 40760a81b439SJeff Roberson 40770a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 40780a81b439SJeff Roberson 4079d4665eaaSJeff Roberson if (zone->uz_bucket_size == 0) 40800a81b439SJeff Roberson return false; 40810a81b439SJeff Roberson 4082cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4083d4665eaaSJeff Roberson newbucket = NULL; 40840a81b439SJeff Roberson 40850a81b439SJeff Roberson /* 4086dfe13344SJeff Roberson * FIRSTTOUCH domains need to free to the correct zdom. When 4087dfe13344SJeff Roberson * enabled this is the zdom of the item. The bucket is the 4088dfe13344SJeff Roberson * cross bucket if the current domain and itemdomain do not match. 40890a81b439SJeff Roberson */ 4090dfe13344SJeff Roberson cbucket = &cache->uc_freebucket; 4091dfe13344SJeff Roberson #ifdef NUMA 4092dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 40930a81b439SJeff Roberson domain = PCPU_GET(domain); 40940a81b439SJeff Roberson if (domain != itemdomain) { 4095dfe13344SJeff Roberson cbucket = &cache->uc_crossbucket; 4096dfe13344SJeff Roberson if (cbucket->ucb_cnt != 0) 4097dfe13344SJeff Roberson atomic_add_64(&zone->uz_xdomain, 4098dfe13344SJeff Roberson cbucket->ucb_cnt); 4099dfe13344SJeff Roberson } 41000a81b439SJeff Roberson } else 41010a81b439SJeff Roberson #endif 4102dfe13344SJeff Roberson itemdomain = domain = 0; 4103dfe13344SJeff Roberson bucket = cache_bucket_unload(cbucket); 41040a81b439SJeff Roberson 41050a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 41060a81b439SJeff Roberson critical_exit(); 41070a81b439SJeff Roberson 4108d4665eaaSJeff Roberson /* 4109d4665eaaSJeff Roberson * Don't let SMR zones operate without a free bucket. Force 4110d4665eaaSJeff Roberson * a synchronize and re-use this one. We will only degrade 4111d4665eaaSJeff Roberson * to a synchronize every bucket_size items rather than every 4112d4665eaaSJeff Roberson * item if we fail to allocate a bucket. 4113d4665eaaSJeff Roberson */ 4114d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) { 4115d4665eaaSJeff Roberson if (bucket != NULL) 4116d4665eaaSJeff Roberson bucket->ub_seq = smr_advance(zone->uz_smr); 4117d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4118d4665eaaSJeff Roberson if (newbucket == NULL && bucket != NULL) { 4119d4665eaaSJeff Roberson bucket_drain(zone, bucket); 4120d4665eaaSJeff Roberson newbucket = bucket; 4121d4665eaaSJeff Roberson bucket = NULL; 4122d4665eaaSJeff Roberson } 4123d4665eaaSJeff Roberson } else if (!bucketdisable) 4124d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4125d4665eaaSJeff Roberson 41260a81b439SJeff Roberson if (bucket != NULL) 41270a81b439SJeff Roberson zone_free_bucket(zone, bucket, udata, domain, itemdomain); 4128a553d4b8SJeff Roberson 4129fc03d22bSJeff Roberson critical_enter(); 4130d4665eaaSJeff Roberson if ((bucket = newbucket) == NULL) 41310a81b439SJeff Roberson return (false); 4132cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4133dfe13344SJeff Roberson #ifdef NUMA 4134fc03d22bSJeff Roberson /* 41350a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 41360a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 41370a81b439SJeff Roberson * the free bucket. 4138fc03d22bSJeff Roberson */ 4139dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 41400a81b439SJeff Roberson domain = PCPU_GET(domain); 4141376b1ba3SJeff Roberson if (domain != itemdomain && 4142376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 4143376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 41440a81b439SJeff Roberson return (true); 41450a81b439SJeff Roberson } 41460a81b439SJeff Roberson } 41470a81b439SJeff Roberson #endif 41480a81b439SJeff Roberson /* 41490a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 41500a81b439SJeff Roberson */ 4151376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 4152fc03d22bSJeff Roberson critical_exit(); 41536fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 41540a81b439SJeff Roberson critical_enter(); 41550a81b439SJeff Roberson } else 4156376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 41578355f576SJeff Roberson 41580a81b439SJeff Roberson return (true); 41598355f576SJeff Roberson } 41608355f576SJeff Roberson 4161ab3185d1SJeff Roberson void 4162ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata) 4163ab3185d1SJeff Roberson { 4164ab3185d1SJeff Roberson 4165ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 416619fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 4167ab3185d1SJeff Roberson 4168e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_domain zone %s(%p)", zone->uz_name, zone); 4169ab3185d1SJeff Roberson 4170ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 4171ab3185d1SJeff Roberson ("uma_zfree_domain: called with spinlock or critical section held")); 4172ab3185d1SJeff Roberson 4173ab3185d1SJeff Roberson /* uma_zfree(..., NULL) does nothing, to match free(9). */ 4174ab3185d1SJeff Roberson if (item == NULL) 4175ab3185d1SJeff Roberson return; 4176ab3185d1SJeff Roberson zone_free_item(zone, item, udata, SKIP_NONE); 4177ab3185d1SJeff Roberson } 4178ab3185d1SJeff Roberson 41798355f576SJeff Roberson static void 4180bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 41818355f576SJeff Roberson { 4182bb15d1c7SGleb Smirnoff uma_keg_t keg; 4183ab3185d1SJeff Roberson uma_domain_t dom; 41849b8db4d0SRyan Libby int freei; 4185099a0e58SBosko Milekic 4186bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 41878b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 4188ab3185d1SJeff Roberson 41898355f576SJeff Roberson /* Do we need to remove from any lists? */ 41908b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 4191099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 41928355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4193ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 41948355f576SJeff Roberson } else if (slab->us_freecount == 0) { 41958355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4196ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 41978355f576SJeff Roberson } 41988355f576SJeff Roberson 4199ef72505eSJeff Roberson /* Slab management. */ 42001e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 42019b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 42028355f576SJeff Roberson slab->us_freecount++; 42038355f576SJeff Roberson 4204ef72505eSJeff Roberson /* Keg statistics. */ 42058b987a77SJeff Roberson dom->ud_free++; 42060095a784SJeff Roberson } 42070095a784SJeff Roberson 42080095a784SJeff Roberson static void 4209b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 42100095a784SJeff Roberson { 42118b987a77SJeff Roberson struct mtx *lock; 4212b75c4efcSAndrew Turner uma_zone_t zone; 42130095a784SJeff Roberson uma_slab_t slab; 42140095a784SJeff Roberson uma_keg_t keg; 42150095a784SJeff Roberson uint8_t *mem; 42168b987a77SJeff Roberson void *item; 42170095a784SJeff Roberson int i; 42188355f576SJeff Roberson 4219b75c4efcSAndrew Turner zone = arg; 4220bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 42218b987a77SJeff Roberson lock = NULL; 422254c5ae80SRyan Libby if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0)) 42238b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 42240095a784SJeff Roberson for (i = 0; i < cnt; i++) { 42250095a784SJeff Roberson item = bucket[i]; 422654c5ae80SRyan Libby if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) { 42270095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 42288b987a77SJeff Roberson } else { 42298b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 423054c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0) 42318b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 42328b987a77SJeff Roberson else 42338b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 42348b987a77SJeff Roberson } 42358b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 42368b987a77SJeff Roberson if (lock != NULL) 42378b987a77SJeff Roberson mtx_unlock(lock); 42388b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 42398b987a77SJeff Roberson } 4240bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 42410095a784SJeff Roberson } 42428b987a77SJeff Roberson if (lock != NULL) 42438b987a77SJeff Roberson mtx_unlock(lock); 42448355f576SJeff Roberson } 42458355f576SJeff Roberson 42460095a784SJeff Roberson /* 42470095a784SJeff Roberson * Frees a single item to any zone. 42480095a784SJeff Roberson * 42490095a784SJeff Roberson * Arguments: 42500095a784SJeff Roberson * zone The zone to free to 42510095a784SJeff Roberson * item The item we're freeing 42520095a784SJeff Roberson * udata User supplied data for the dtor 42530095a784SJeff Roberson * skip Skip dtors and finis 42540095a784SJeff Roberson */ 42550095a784SJeff Roberson static void 42560095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 42570095a784SJeff Roberson { 4258c5deaf04SGleb Smirnoff 4259d4665eaaSJeff Roberson /* 4260d4665eaaSJeff Roberson * If a free is sent directly to an SMR zone we have to 4261d4665eaaSJeff Roberson * synchronize immediately because the item can instantly 4262d4665eaaSJeff Roberson * be reallocated. This should only happen in degenerate 4263d4665eaaSJeff Roberson * cases when no memory is available for per-cpu caches. 4264d4665eaaSJeff Roberson */ 4265d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE) 4266d4665eaaSJeff Roberson smr_synchronize(zone->uz_smr); 4267d4665eaaSJeff Roberson 4268cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 42690095a784SJeff Roberson 42700095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 42710095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 42720095a784SJeff Roberson 42730095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4274bb15d1c7SGleb Smirnoff 4275bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4276bb15d1c7SGleb Smirnoff return; 4277bb15d1c7SGleb Smirnoff 42782efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 42792efcc8cbSGleb Smirnoff 42804bd61e19SJeff Roberson if (zone->uz_max_items > 0) 42814bd61e19SJeff Roberson zone_free_limit(zone, 1); 4282bb45b411SGleb Smirnoff } 42830095a784SJeff Roberson 42848355f576SJeff Roberson /* See uma.h */ 42851c6cae97SLawrence Stewart int 4286736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4287736ee590SJeff Roberson { 4288bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 4289003cf08bSMark Johnston int count; 4290bb15d1c7SGleb Smirnoff 42914bd61e19SJeff Roberson /* 42924bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 42934bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 42944bd61e19SJeff Roberson * way to clear a limit. 42954bd61e19SJeff Roberson */ 4296bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4297003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4298003cf08bSMark Johnston count = ubz != NULL ? ubz->ubz_entries : 0; 429920a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = count; 430020a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 430120a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4302bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4303cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4304cc7ce83aSJeff Roberson zone_update_caches(zone); 43054bd61e19SJeff Roberson /* We may need to wake waiters. */ 43064bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4307bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4308bb15d1c7SGleb Smirnoff 4309bb15d1c7SGleb Smirnoff return (nitems); 4310bb15d1c7SGleb Smirnoff } 4311bb15d1c7SGleb Smirnoff 4312bb15d1c7SGleb Smirnoff /* See uma.h */ 4313003cf08bSMark Johnston void 4314bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4315bb15d1c7SGleb Smirnoff { 4316003cf08bSMark Johnston struct uma_bucket_zone *ubz; 4317003cf08bSMark Johnston int bpcpu; 4318bb15d1c7SGleb Smirnoff 4319bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4320003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4321003cf08bSMark Johnston if (ubz != NULL) { 4322003cf08bSMark Johnston bpcpu = 2; 4323dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 4324003cf08bSMark Johnston /* Count the cross-domain bucket. */ 4325003cf08bSMark Johnston bpcpu++; 4326003cf08bSMark Johnston nitems -= ubz->ubz_entries * bpcpu * mp_ncpus; 432720a4e154SJeff Roberson zone->uz_bucket_size_max = ubz->ubz_entries; 4328003cf08bSMark Johnston } else { 432920a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 4330003cf08bSMark Johnston } 433120a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 433220a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4333bb15d1c7SGleb Smirnoff zone->uz_bkt_max = nitems; 4334bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4335736ee590SJeff Roberson } 4336736ee590SJeff Roberson 4337736ee590SJeff Roberson /* See uma.h */ 4338e49471b0SAndre Oppermann int 4339e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4340e49471b0SAndre Oppermann { 4341e49471b0SAndre Oppermann int nitems; 4342e49471b0SAndre Oppermann 4343727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4344e49471b0SAndre Oppermann 4345e49471b0SAndre Oppermann return (nitems); 4346e49471b0SAndre Oppermann } 4347e49471b0SAndre Oppermann 4348e49471b0SAndre Oppermann /* See uma.h */ 43492f891cd5SPawel Jakub Dawidek void 43502f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 43512f891cd5SPawel Jakub Dawidek { 43522f891cd5SPawel Jakub Dawidek 4353727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 43542f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 43552f891cd5SPawel Jakub Dawidek } 43562f891cd5SPawel Jakub Dawidek 43572f891cd5SPawel Jakub Dawidek /* See uma.h */ 435854503a13SJonathan T. Looney void 435954503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 436054503a13SJonathan T. Looney { 436154503a13SJonathan T. Looney 4362727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4363e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 436454503a13SJonathan T. Looney } 436554503a13SJonathan T. Looney 436654503a13SJonathan T. Looney /* See uma.h */ 4367c4ae7908SLawrence Stewart int 4368c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4369c4ae7908SLawrence Stewart { 4370c4ae7908SLawrence Stewart int64_t nitems; 4371c4ae7908SLawrence Stewart u_int i; 4372c4ae7908SLawrence Stewart 4373bfb6b7a1SJeff Roberson nitems = 0; 4374bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER) 43752efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 43762efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4377727c6918SJeff Roberson CPU_FOREACH(i) 4378727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4379727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4380c4ae7908SLawrence Stewart 4381c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4382c4ae7908SLawrence Stewart } 4383c4ae7908SLawrence Stewart 438420a4e154SJeff Roberson static uint64_t 438520a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 438620a4e154SJeff Roberson { 438720a4e154SJeff Roberson uint64_t nitems; 438820a4e154SJeff Roberson u_int i; 438920a4e154SJeff Roberson 4390bfb6b7a1SJeff Roberson nitems = 0; 4391bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER) 439220a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4393727c6918SJeff Roberson CPU_FOREACH(i) 4394727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 439520a4e154SJeff Roberson 439620a4e154SJeff Roberson return (nitems); 439720a4e154SJeff Roberson } 439820a4e154SJeff Roberson 439920a4e154SJeff Roberson static uint64_t 440020a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 440120a4e154SJeff Roberson { 440220a4e154SJeff Roberson uint64_t nitems; 440320a4e154SJeff Roberson u_int i; 440420a4e154SJeff Roberson 4405bfb6b7a1SJeff Roberson nitems = 0; 4406bfb6b7a1SJeff Roberson if (zone->uz_frees != EARLY_COUNTER) 440720a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4408727c6918SJeff Roberson CPU_FOREACH(i) 4409727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 441020a4e154SJeff Roberson 441120a4e154SJeff Roberson return (nitems); 441220a4e154SJeff Roberson } 441320a4e154SJeff Roberson 441431c251a0SJeff Roberson #ifdef INVARIANTS 441531c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */ 441631c251a0SJeff Roberson static uint64_t 441731c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg) 441831c251a0SJeff Roberson { 441931c251a0SJeff Roberson uma_zone_t z; 442031c251a0SJeff Roberson uint64_t nitems; 442131c251a0SJeff Roberson 442231c251a0SJeff Roberson nitems = 0; 442331c251a0SJeff Roberson LIST_FOREACH(z, &keg->uk_zones, uz_link) 442431c251a0SJeff Roberson nitems += uma_zone_get_allocs(z); 442531c251a0SJeff Roberson 442631c251a0SJeff Roberson return (nitems); 442731c251a0SJeff Roberson } 442831c251a0SJeff Roberson #endif 442931c251a0SJeff Roberson 4430c4ae7908SLawrence Stewart /* See uma.h */ 4431736ee590SJeff Roberson void 4432099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4433099a0e58SBosko Milekic { 4434e20a199fSJeff Roberson uma_keg_t keg; 4435e20a199fSJeff Roberson 4436bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4437727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4438e20a199fSJeff Roberson keg->uk_init = uminit; 4439099a0e58SBosko Milekic } 4440099a0e58SBosko Milekic 4441099a0e58SBosko Milekic /* See uma.h */ 4442099a0e58SBosko Milekic void 4443099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4444099a0e58SBosko Milekic { 4445e20a199fSJeff Roberson uma_keg_t keg; 4446e20a199fSJeff Roberson 4447bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4448727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4449e20a199fSJeff Roberson keg->uk_fini = fini; 4450099a0e58SBosko Milekic } 4451099a0e58SBosko Milekic 4452099a0e58SBosko Milekic /* See uma.h */ 4453099a0e58SBosko Milekic void 4454099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4455099a0e58SBosko Milekic { 4456af526374SJeff Roberson 4457727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4458099a0e58SBosko Milekic zone->uz_init = zinit; 4459099a0e58SBosko Milekic } 4460099a0e58SBosko Milekic 4461099a0e58SBosko Milekic /* See uma.h */ 4462099a0e58SBosko Milekic void 4463099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4464099a0e58SBosko Milekic { 4465af526374SJeff Roberson 4466727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4467099a0e58SBosko Milekic zone->uz_fini = zfini; 4468099a0e58SBosko Milekic } 4469099a0e58SBosko Milekic 4470099a0e58SBosko Milekic /* See uma.h */ 4471099a0e58SBosko Milekic void 44728355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 44738355f576SJeff Roberson { 44740095a784SJeff Roberson uma_keg_t keg; 4475e20a199fSJeff Roberson 4476bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4477727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 44780095a784SJeff Roberson keg->uk_freef = freef; 44798355f576SJeff Roberson } 44808355f576SJeff Roberson 44818355f576SJeff Roberson /* See uma.h */ 44828355f576SJeff Roberson void 44838355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 44848355f576SJeff Roberson { 4485e20a199fSJeff Roberson uma_keg_t keg; 4486e20a199fSJeff Roberson 4487bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4488727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4489e20a199fSJeff Roberson keg->uk_allocf = allocf; 44908355f576SJeff Roberson } 44918355f576SJeff Roberson 44928355f576SJeff Roberson /* See uma.h */ 44936fd34d6fSJeff Roberson void 4494d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr) 4495d4665eaaSJeff Roberson { 4496d4665eaaSJeff Roberson 4497d4665eaaSJeff Roberson ZONE_ASSERT_COLD(zone); 4498d4665eaaSJeff Roberson 4499d4665eaaSJeff Roberson zone->uz_flags |= UMA_ZONE_SMR; 4500d4665eaaSJeff Roberson zone->uz_smr = smr; 4501d4665eaaSJeff Roberson zone_update_caches(zone); 4502d4665eaaSJeff Roberson } 4503d4665eaaSJeff Roberson 4504d4665eaaSJeff Roberson smr_t 4505d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone) 4506d4665eaaSJeff Roberson { 4507d4665eaaSJeff Roberson 4508d4665eaaSJeff Roberson return (zone->uz_smr); 4509d4665eaaSJeff Roberson } 4510d4665eaaSJeff Roberson 4511d4665eaaSJeff Roberson /* See uma.h */ 4512d4665eaaSJeff Roberson void 45136fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 45146fd34d6fSJeff Roberson { 45156fd34d6fSJeff Roberson uma_keg_t keg; 45166fd34d6fSJeff Roberson 4517bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4518727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 45196fd34d6fSJeff Roberson keg->uk_reserve = items; 45206fd34d6fSJeff Roberson } 45216fd34d6fSJeff Roberson 45226fd34d6fSJeff Roberson /* See uma.h */ 45238355f576SJeff Roberson int 4524a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 45258355f576SJeff Roberson { 4526099a0e58SBosko Milekic uma_keg_t keg; 45278355f576SJeff Roberson vm_offset_t kva; 45289ba30bcbSZbigniew Bodek u_int pages; 45298355f576SJeff Roberson 4530bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4531727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4532727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 45338355f576SJeff Roberson 453479c9f942SJeff Roberson pages = howmany(count, keg->uk_ipers) * keg->uk_ppera; 4535a553d4b8SJeff Roberson 4536a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4537a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4538a4915c21SAttilio Rao #else 4539a4915c21SAttilio Rao if (1) { 4540a4915c21SAttilio Rao #endif 454157223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4542d1f42ac2SAlan Cox if (kva == 0) 45438355f576SJeff Roberson return (0); 4544a4915c21SAttilio Rao } else 4545a4915c21SAttilio Rao kva = 0; 4546bb15d1c7SGleb Smirnoff 4547bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4548bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4549099a0e58SBosko Milekic keg->uk_kva = kva; 4550a4915c21SAttilio Rao keg->uk_offset = 0; 4551bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4552a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4553a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4554a4915c21SAttilio Rao #else 4555a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4556a4915c21SAttilio Rao #endif 4557cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4558cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4559cc7ce83aSJeff Roberson zone_update_caches(zone); 4560bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4561af526374SJeff Roberson 45628355f576SJeff Roberson return (1); 45638355f576SJeff Roberson } 45648355f576SJeff Roberson 45658355f576SJeff Roberson /* See uma.h */ 45668355f576SJeff Roberson void 45678355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 45688355f576SJeff Roberson { 4569920239efSMark Johnston struct vm_domainset_iter di; 4570ab3185d1SJeff Roberson uma_domain_t dom; 45718355f576SJeff Roberson uma_slab_t slab; 4572099a0e58SBosko Milekic uma_keg_t keg; 457386220393SMark Johnston int aflags, domain, slabs; 45748355f576SJeff Roberson 4575bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 457679c9f942SJeff Roberson slabs = howmany(items, keg->uk_ipers); 4577194a979eSMark Johnston while (slabs-- > 0) { 457886220393SMark Johnston aflags = M_NOWAIT; 457986220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 458086220393SMark Johnston &aflags); 458186220393SMark Johnston for (;;) { 458286220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 458386220393SMark Johnston aflags); 458486220393SMark Johnston if (slab != NULL) { 4585ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 45868b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 458786220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 458886220393SMark Johnston us_link); 45898b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 4590920239efSMark Johnston break; 45918355f576SJeff Roberson } 45928b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 459386220393SMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 459486220393SMark Johnston } 459586220393SMark Johnston } 459686220393SMark Johnston } 45978355f576SJeff Roberson 45988355f576SJeff Roberson /* See uma.h */ 459908cfa56eSMark Johnston void 460008cfa56eSMark Johnston uma_reclaim(int req) 46018355f576SJeff Roberson { 460244ec2b63SKonstantin Belousov 46031431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 460408cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 460586bbae32SJeff Roberson bucket_enable(); 460608cfa56eSMark Johnston 460708cfa56eSMark Johnston switch (req) { 460808cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 460920a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 461008cfa56eSMark Johnston break; 461108cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 461208cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 461320a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 461408cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 461508cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 461620a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4617a2de44abSAlexander Motin } 461808cfa56eSMark Johnston break; 461908cfa56eSMark Johnston default: 462008cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 462108cfa56eSMark Johnston } 46220f9b7bf3SMark Johnston 46238355f576SJeff Roberson /* 46248355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 46258355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 46268355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 46278355f576SJeff Roberson */ 46289b8db4d0SRyan Libby zone_drain(slabzones[0], NULL); 46299b8db4d0SRyan Libby zone_drain(slabzones[1], NULL); 4630cae33c14SJeff Roberson bucket_zone_drain(); 463108cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 46328355f576SJeff Roberson } 46338355f576SJeff Roberson 46342e47807cSJeff Roberson static volatile int uma_reclaim_needed; 463544ec2b63SKonstantin Belousov 463644ec2b63SKonstantin Belousov void 463744ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 463844ec2b63SKonstantin Belousov { 463944ec2b63SKonstantin Belousov 46402e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 46412e47807cSJeff Roberson wakeup(uma_reclaim); 464244ec2b63SKonstantin Belousov } 464344ec2b63SKonstantin Belousov 464444ec2b63SKonstantin Belousov void 464544ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 464644ec2b63SKonstantin Belousov { 464744ec2b63SKonstantin Belousov 464844ec2b63SKonstantin Belousov for (;;) { 464908cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4650200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 465108cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 46522e47807cSJeff Roberson hz); 465308cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 46549b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 465508cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4656200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 46572e47807cSJeff Roberson /* Don't fire more than once per-second. */ 46582e47807cSJeff Roberson pause("umarclslp", hz); 465944ec2b63SKonstantin Belousov } 466044ec2b63SKonstantin Belousov } 466144ec2b63SKonstantin Belousov 4662663b416fSJohn Baldwin /* See uma.h */ 466308cfa56eSMark Johnston void 466408cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 466508cfa56eSMark Johnston { 466608cfa56eSMark Johnston 466708cfa56eSMark Johnston switch (req) { 466808cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 466920a4e154SJeff Roberson zone_trim(zone, NULL); 467008cfa56eSMark Johnston break; 467108cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 467220a4e154SJeff Roberson zone_drain(zone, NULL); 467308cfa56eSMark Johnston break; 467408cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 467508cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 467620a4e154SJeff Roberson zone_drain(zone, NULL); 467708cfa56eSMark Johnston break; 467808cfa56eSMark Johnston default: 467908cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 468008cfa56eSMark Johnston } 468108cfa56eSMark Johnston } 468208cfa56eSMark Johnston 468308cfa56eSMark Johnston /* See uma.h */ 4684663b416fSJohn Baldwin int 4685663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 4686663b416fSJohn Baldwin { 4687663b416fSJohn Baldwin 4688727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 46896c125b8dSMohan Srinivasan } 46906c125b8dSMohan Srinivasan 46912e47807cSJeff Roberson unsigned long 46922e47807cSJeff Roberson uma_limit(void) 46932e47807cSJeff Roberson { 46942e47807cSJeff Roberson 46952e47807cSJeff Roberson return (uma_kmem_limit); 46962e47807cSJeff Roberson } 46972e47807cSJeff Roberson 46982e47807cSJeff Roberson void 46992e47807cSJeff Roberson uma_set_limit(unsigned long limit) 47002e47807cSJeff Roberson { 47012e47807cSJeff Roberson 47022e47807cSJeff Roberson uma_kmem_limit = limit; 47032e47807cSJeff Roberson } 47042e47807cSJeff Roberson 47052e47807cSJeff Roberson unsigned long 47062e47807cSJeff Roberson uma_size(void) 47072e47807cSJeff Roberson { 47082e47807cSJeff Roberson 4709058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 4710ad5b0f5bSJeff Roberson } 4711ad5b0f5bSJeff Roberson 4712ad5b0f5bSJeff Roberson long 4713ad5b0f5bSJeff Roberson uma_avail(void) 4714ad5b0f5bSJeff Roberson { 4715ad5b0f5bSJeff Roberson 4716058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 47172e47807cSJeff Roberson } 47182e47807cSJeff Roberson 4719a0d4b0aeSRobert Watson #ifdef DDB 47208355f576SJeff Roberson /* 47217a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 47227a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 47237a52a97eSRobert Watson * 47247a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 47257a52a97eSRobert Watson * per-CPU cache statistic. 47267a52a97eSRobert Watson * 47277a52a97eSRobert Watson */ 47287a52a97eSRobert Watson static void 47290f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 4730c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 47317a52a97eSRobert Watson { 47327a52a97eSRobert Watson uma_cache_t cache; 4733c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 47347a52a97eSRobert Watson int cachefree, cpu; 47357a52a97eSRobert Watson 4736c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 47377a52a97eSRobert Watson cachefree = 0; 47383aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 47397a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 4740376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 4741376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 4742376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 4743376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 47447a52a97eSRobert Watson allocs += cache->uc_allocs; 47457a52a97eSRobert Watson frees += cache->uc_frees; 47467a52a97eSRobert Watson } 47472efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 47482efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 4749bf965959SSean Bruno sleeps += z->uz_sleeps; 4750c1685086SJeff Roberson xdomain += z->uz_xdomain; 47517a52a97eSRobert Watson if (cachefreep != NULL) 47527a52a97eSRobert Watson *cachefreep = cachefree; 47537a52a97eSRobert Watson if (allocsp != NULL) 47547a52a97eSRobert Watson *allocsp = allocs; 47557a52a97eSRobert Watson if (freesp != NULL) 47567a52a97eSRobert Watson *freesp = frees; 4757bf965959SSean Bruno if (sleepsp != NULL) 4758bf965959SSean Bruno *sleepsp = sleeps; 4759c1685086SJeff Roberson if (xdomainp != NULL) 4760c1685086SJeff Roberson *xdomainp = xdomain; 47617a52a97eSRobert Watson } 4762a0d4b0aeSRobert Watson #endif /* DDB */ 47637a52a97eSRobert Watson 47647a52a97eSRobert Watson static int 47657a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 47667a52a97eSRobert Watson { 47677a52a97eSRobert Watson uma_keg_t kz; 47687a52a97eSRobert Watson uma_zone_t z; 47697a52a97eSRobert Watson int count; 47707a52a97eSRobert Watson 47717a52a97eSRobert Watson count = 0; 4772111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 47737a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 47747a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 47757a52a97eSRobert Watson count++; 47767a52a97eSRobert Watson } 4777b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4778b47acb0aSGleb Smirnoff count++; 4779b47acb0aSGleb Smirnoff 4780111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 47817a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 47827a52a97eSRobert Watson } 47837a52a97eSRobert Watson 4784b47acb0aSGleb Smirnoff static void 4785b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 4786b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 4787b47acb0aSGleb Smirnoff { 4788b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 4789b47acb0aSGleb Smirnoff uma_cache_t cache; 4790b47acb0aSGleb Smirnoff int i; 4791b47acb0aSGleb Smirnoff 4792b47acb0aSGleb Smirnoff 4793b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 4794b47acb0aSGleb Smirnoff zdom = &z->uz_domain[i]; 4795b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 4796b47acb0aSGleb Smirnoff } 4797b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 4798b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 4799b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 4800b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 4801c1685086SJeff Roberson uth->uth_xdomain = z->uz_xdomain; 48021de9724eSMark Johnston 4803b47acb0aSGleb Smirnoff /* 48041de9724eSMark Johnston * While it is not normally safe to access the cache bucket pointers 48051de9724eSMark Johnston * while not on the CPU that owns the cache, we only allow the pointers 48061de9724eSMark Johnston * to be exchanged without the zone lock held, not invalidated, so 48071de9724eSMark Johnston * accept the possible race associated with bucket exchange during 48081de9724eSMark Johnston * monitoring. Use atomic_load_ptr() to ensure that the bucket pointers 48091de9724eSMark Johnston * are loaded only once. 4810b47acb0aSGleb Smirnoff */ 4811b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 4812b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 4813b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 4814b47acb0aSGleb Smirnoff continue; 4815b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 4816376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 4817376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 4818376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 4819b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 4820b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 4821b47acb0aSGleb Smirnoff } 4822b47acb0aSGleb Smirnoff } 4823b47acb0aSGleb Smirnoff 48247a52a97eSRobert Watson static int 48257a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 48267a52a97eSRobert Watson { 48277a52a97eSRobert Watson struct uma_stream_header ush; 48287a52a97eSRobert Watson struct uma_type_header uth; 482963b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 48307a52a97eSRobert Watson struct sbuf sbuf; 48317a52a97eSRobert Watson uma_keg_t kz; 48327a52a97eSRobert Watson uma_zone_t z; 48334bd61e19SJeff Roberson uint64_t items; 48348b987a77SJeff Roberson uint32_t kfree, pages; 48354e657159SMatthew D Fleming int count, error, i; 48367a52a97eSRobert Watson 483700f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 483800f0e671SMatthew D Fleming if (error != 0) 483900f0e671SMatthew D Fleming return (error); 48404e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 48411eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 484263b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 48434e657159SMatthew D Fleming 4844404a593eSMatthew D Fleming count = 0; 4845111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 48467a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 48477a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 48487a52a97eSRobert Watson count++; 48497a52a97eSRobert Watson } 48507a52a97eSRobert Watson 4851b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4852b47acb0aSGleb Smirnoff count++; 4853b47acb0aSGleb Smirnoff 48547a52a97eSRobert Watson /* 48557a52a97eSRobert Watson * Insert stream header. 48567a52a97eSRobert Watson */ 48577a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 48587a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 4859ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 48607a52a97eSRobert Watson ush.ush_count = count; 48614e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 48627a52a97eSRobert Watson 48637a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 48648b987a77SJeff Roberson kfree = pages = 0; 48658b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 48668b987a77SJeff Roberson kfree += kz->uk_domain[i].ud_free; 48678b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 48688b987a77SJeff Roberson } 48697a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 48707a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 48717a52a97eSRobert Watson ZONE_LOCK(z); 4872cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 48737a52a97eSRobert Watson uth.uth_align = kz->uk_align; 48747a52a97eSRobert Watson uth.uth_size = kz->uk_size; 48757a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 48764bd61e19SJeff Roberson if (z->uz_max_items > 0) { 48774bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 48784bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 4879bb15d1c7SGleb Smirnoff kz->uk_ppera; 48804bd61e19SJeff Roberson } else 48818b987a77SJeff Roberson uth.uth_pages = pages; 4882f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 4883bb15d1c7SGleb Smirnoff kz->uk_ppera; 4884bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 48858b987a77SJeff Roberson uth.uth_keg_free = kfree; 4886cbbb4a00SRobert Watson 4887cbbb4a00SRobert Watson /* 4888cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 4889cbbb4a00SRobert Watson * on the keg's zone list. 4890cbbb4a00SRobert Watson */ 4891e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 4892cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 4893cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 4894b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 4895b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 48962450bbb8SRobert Watson ZONE_UNLOCK(z); 489763b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 489863b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 489963b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 49007a52a97eSRobert Watson } 49017a52a97eSRobert Watson } 4902b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 4903b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 4904b47acb0aSGleb Smirnoff ZONE_LOCK(z); 4905b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 4906b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 4907b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 4908b47acb0aSGleb Smirnoff ZONE_UNLOCK(z); 4909b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 4910b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 4911b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 4912b47acb0aSGleb Smirnoff } 4913b47acb0aSGleb Smirnoff 4914111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 49154e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 49164e657159SMatthew D Fleming sbuf_delete(&sbuf); 491763b5d112SKonstantin Belousov free(ups, M_TEMP); 49187a52a97eSRobert Watson return (error); 49197a52a97eSRobert Watson } 492048c5777eSRobert Watson 49210a5a3ccbSGleb Smirnoff int 49220a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 49230a5a3ccbSGleb Smirnoff { 49240a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 492516be9f54SGleb Smirnoff int error, max; 49260a5a3ccbSGleb Smirnoff 492716be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 49280a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 49290a5a3ccbSGleb Smirnoff if (error || !req->newptr) 49300a5a3ccbSGleb Smirnoff return (error); 49310a5a3ccbSGleb Smirnoff 49320a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 49330a5a3ccbSGleb Smirnoff 49340a5a3ccbSGleb Smirnoff return (0); 49350a5a3ccbSGleb Smirnoff } 49360a5a3ccbSGleb Smirnoff 49370a5a3ccbSGleb Smirnoff int 49380a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 49390a5a3ccbSGleb Smirnoff { 494020a4e154SJeff Roberson uma_zone_t zone; 49410a5a3ccbSGleb Smirnoff int cur; 49420a5a3ccbSGleb Smirnoff 494320a4e154SJeff Roberson /* 494420a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 494520a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 494620a4e154SJeff Roberson */ 494720a4e154SJeff Roberson if (arg2 == 0) 494820a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 494920a4e154SJeff Roberson else 495020a4e154SJeff Roberson zone = arg1; 49510a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 49520a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 49530a5a3ccbSGleb Smirnoff } 49540a5a3ccbSGleb Smirnoff 495520a4e154SJeff Roberson static int 495620a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 495720a4e154SJeff Roberson { 495820a4e154SJeff Roberson uma_zone_t zone = arg1; 495920a4e154SJeff Roberson uint64_t cur; 496020a4e154SJeff Roberson 496120a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 496220a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 496320a4e154SJeff Roberson } 496420a4e154SJeff Roberson 496520a4e154SJeff Roberson static int 496620a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 496720a4e154SJeff Roberson { 496820a4e154SJeff Roberson uma_zone_t zone = arg1; 496920a4e154SJeff Roberson uint64_t cur; 497020a4e154SJeff Roberson 497120a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 497220a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 497320a4e154SJeff Roberson } 497420a4e154SJeff Roberson 49756d204a6aSRyan Libby static int 49766d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 49776d204a6aSRyan Libby { 49786d204a6aSRyan Libby struct sbuf sbuf; 49796d204a6aSRyan Libby uma_zone_t zone = arg1; 49806d204a6aSRyan Libby int error; 49816d204a6aSRyan Libby 49826d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 49836d204a6aSRyan Libby if (zone->uz_flags != 0) 49846d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 49856d204a6aSRyan Libby else 49866d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 49876d204a6aSRyan Libby error = sbuf_finish(&sbuf); 49886d204a6aSRyan Libby sbuf_delete(&sbuf); 49896d204a6aSRyan Libby 49906d204a6aSRyan Libby return (error); 49916d204a6aSRyan Libby } 49926d204a6aSRyan Libby 4993f7af5015SRyan Libby static int 4994f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 4995f7af5015SRyan Libby { 4996f7af5015SRyan Libby uma_keg_t keg = arg1; 4997f7af5015SRyan Libby int avail, effpct, total; 4998f7af5015SRyan Libby 4999f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 500054c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 50019b8db4d0SRyan Libby total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize; 5002f7af5015SRyan Libby /* 5003f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 5004f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 5005f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 5006f7af5015SRyan Libby */ 5007f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 5008f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 5009f7af5015SRyan Libby avail *= mp_maxid + 1; 5010f7af5015SRyan Libby effpct = 100 * avail / total; 5011f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 5012f7af5015SRyan Libby } 5013f7af5015SRyan Libby 50144bd61e19SJeff Roberson static int 50154bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 50164bd61e19SJeff Roberson { 50174bd61e19SJeff Roberson uma_zone_t zone = arg1; 50184bd61e19SJeff Roberson uint64_t cur; 50194bd61e19SJeff Roberson 50204bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 50214bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 50224bd61e19SJeff Roberson } 50234bd61e19SJeff Roberson 50249542ea7bSGleb Smirnoff #ifdef INVARIANTS 50259542ea7bSGleb Smirnoff static uma_slab_t 50269542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 50279542ea7bSGleb Smirnoff { 50289542ea7bSGleb Smirnoff uma_slab_t slab; 50299542ea7bSGleb Smirnoff uma_keg_t keg; 50309542ea7bSGleb Smirnoff uint8_t *mem; 50319542ea7bSGleb Smirnoff 50329542ea7bSGleb Smirnoff /* 50339542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 50349542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 50359542ea7bSGleb Smirnoff * essentially holds a reference. 50369542ea7bSGleb Smirnoff */ 5037727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 5038727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5039bb15d1c7SGleb Smirnoff return (NULL); 504054c5ae80SRyan Libby if (zone->uz_flags & UMA_ZFLAG_VTOSLAB) 5041727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 5042bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 504354c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0) 5044727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 50458b987a77SJeff Roberson KEG_LOCK(keg, 0); 50469542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 50478b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 50489542ea7bSGleb Smirnoff 50499542ea7bSGleb Smirnoff return (slab); 50509542ea7bSGleb Smirnoff } 50519542ea7bSGleb Smirnoff 5052c5deaf04SGleb Smirnoff static bool 5053c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 5054c5deaf04SGleb Smirnoff { 5055c5deaf04SGleb Smirnoff 5056727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5057c5deaf04SGleb Smirnoff return (true); 5058c5deaf04SGleb Smirnoff 5059bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 5060c5deaf04SGleb Smirnoff } 5061c5deaf04SGleb Smirnoff 5062c5deaf04SGleb Smirnoff static bool 5063c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 5064c5deaf04SGleb Smirnoff { 5065c5deaf04SGleb Smirnoff uintptr_t idx; 5066c5deaf04SGleb Smirnoff 5067c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 5068c5deaf04SGleb Smirnoff return (true); 5069c5deaf04SGleb Smirnoff 5070c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 5071c5deaf04SGleb Smirnoff return (false); 5072c5deaf04SGleb Smirnoff 5073c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 5074c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 5075c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 5076c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 5077c5deaf04SGleb Smirnoff } 5078c5deaf04SGleb Smirnoff 5079c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 5080c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 5081c5deaf04SGleb Smirnoff return (true); 5082c5deaf04SGleb Smirnoff } 5083c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 5084c5deaf04SGleb Smirnoff 5085c5deaf04SGleb Smirnoff return (false); 5086c5deaf04SGleb Smirnoff } 5087c5deaf04SGleb Smirnoff 50889542ea7bSGleb Smirnoff /* 50899542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 50909542ea7bSGleb Smirnoff * 50919542ea7bSGleb Smirnoff */ 50929542ea7bSGleb Smirnoff static void 50939542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 50949542ea7bSGleb Smirnoff { 50959542ea7bSGleb Smirnoff uma_keg_t keg; 50969542ea7bSGleb Smirnoff int freei; 50979542ea7bSGleb Smirnoff 50989542ea7bSGleb Smirnoff if (slab == NULL) { 50999542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 51009542ea7bSGleb Smirnoff if (slab == NULL) 51019542ea7bSGleb Smirnoff panic("uma: item %p did not belong to zone %s\n", 51029542ea7bSGleb Smirnoff item, zone->uz_name); 51039542ea7bSGleb Smirnoff } 5104584061b4SJeff Roberson keg = zone->uz_keg; 51051e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 51069542ea7bSGleb Smirnoff 5107815db204SRyan Libby if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 51089542ea7bSGleb Smirnoff panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n", 51099542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 5110815db204SRyan Libby BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 51119542ea7bSGleb Smirnoff } 51129542ea7bSGleb Smirnoff 51139542ea7bSGleb Smirnoff /* 51149542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 51159542ea7bSGleb Smirnoff * and duplicate frees. 51169542ea7bSGleb Smirnoff * 51179542ea7bSGleb Smirnoff */ 51189542ea7bSGleb Smirnoff static void 51199542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 51209542ea7bSGleb Smirnoff { 51219542ea7bSGleb Smirnoff uma_keg_t keg; 51229542ea7bSGleb Smirnoff int freei; 51239542ea7bSGleb Smirnoff 51249542ea7bSGleb Smirnoff if (slab == NULL) { 51259542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 51269542ea7bSGleb Smirnoff if (slab == NULL) 51279542ea7bSGleb Smirnoff panic("uma: Freed item %p did not belong to zone %s\n", 51289542ea7bSGleb Smirnoff item, zone->uz_name); 51299542ea7bSGleb Smirnoff } 5130584061b4SJeff Roberson keg = zone->uz_keg; 51311e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 51329542ea7bSGleb Smirnoff 51339542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 51349542ea7bSGleb Smirnoff panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n", 51359542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 51369542ea7bSGleb Smirnoff 51371e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 51389542ea7bSGleb Smirnoff panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n", 51399542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 51409542ea7bSGleb Smirnoff 5141815db204SRyan Libby if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 51429542ea7bSGleb Smirnoff panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n", 51439542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 51449542ea7bSGleb Smirnoff 5145815db204SRyan Libby BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 51469542ea7bSGleb Smirnoff } 51479542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 51489542ea7bSGleb Smirnoff 514948c5777eSRobert Watson #ifdef DDB 515046d70077SConrad Meyer static int64_t 515146d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 51520223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 515348c5777eSRobert Watson { 515446d70077SConrad Meyer uint64_t frees; 51550f9b7bf3SMark Johnston int i; 515648c5777eSRobert Watson 515748c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 515846d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 51592efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 516046d70077SConrad Meyer *sleeps = z->uz_sleeps; 516146d70077SConrad Meyer *cachefree = 0; 516246d70077SConrad Meyer *xdomain = 0; 516348c5777eSRobert Watson } else 516446d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 516546d70077SConrad Meyer xdomain); 51668b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 51678b987a77SJeff Roberson *cachefree += z->uz_domain[i].uzd_nitems; 5168e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 516948c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 51708b987a77SJeff Roberson *cachefree += kz->uk_domain[i].ud_free; 51718b987a77SJeff Roberson } 517246d70077SConrad Meyer *used = *allocs - frees; 517346d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 517446d70077SConrad Meyer } 51750f9b7bf3SMark Johnston 517646d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 517746d70077SConrad Meyer { 517846d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 517946d70077SConrad Meyer uma_keg_t kz; 518046d70077SConrad Meyer uma_zone_t z; 518146d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 518246d70077SConrad Meyer long cachefree; 518346d70077SConrad Meyer /* variables for sorting */ 518446d70077SConrad Meyer uma_keg_t cur_keg; 518546d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 518646d70077SConrad Meyer int64_t cur_size, last_size, size; 518746d70077SConrad Meyer int ties; 518846d70077SConrad Meyer 518946d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 519046d70077SConrad Meyer if (modif[0] == 'i') { 519146d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 519246d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 519346d70077SConrad Meyer } else { 519446d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 519546d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 519646d70077SConrad Meyer } 519746d70077SConrad Meyer 519846d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 519946d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 520046d70077SConrad Meyer 520146d70077SConrad Meyer /* Sort the zones with largest size first. */ 520246d70077SConrad Meyer last_zone = NULL; 520346d70077SConrad Meyer last_size = INT64_MAX; 520446d70077SConrad Meyer for (;;) { 520546d70077SConrad Meyer cur_zone = NULL; 520646d70077SConrad Meyer cur_size = -1; 520746d70077SConrad Meyer ties = 0; 520846d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 520946d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 521046d70077SConrad Meyer /* 521146d70077SConrad Meyer * In the case of size ties, print out zones 521246d70077SConrad Meyer * in the order they are encountered. That is, 521346d70077SConrad Meyer * when we encounter the most recently output 521446d70077SConrad Meyer * zone, we have already printed all preceding 521546d70077SConrad Meyer * ties, and we must print all following ties. 521646d70077SConrad Meyer */ 521746d70077SConrad Meyer if (z == last_zone) { 521846d70077SConrad Meyer ties = 1; 521946d70077SConrad Meyer continue; 522046d70077SConrad Meyer } 522146d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 522246d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 522346d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 522446d70077SConrad Meyer { 522546d70077SConrad Meyer cur_size = size; 522646d70077SConrad Meyer cur_zone = z; 522746d70077SConrad Meyer cur_keg = kz; 522846d70077SConrad Meyer } 522946d70077SConrad Meyer } 523046d70077SConrad Meyer } 523146d70077SConrad Meyer if (cur_zone == NULL) 523246d70077SConrad Meyer break; 523346d70077SConrad Meyer 523446d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 523546d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 523646d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 523746d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 523846d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 523920a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 524020a4e154SJeff Roberson xdomain); 524146d70077SConrad Meyer 5242687c94aaSJohn Baldwin if (db_pager_quit) 5243687c94aaSJohn Baldwin return; 524446d70077SConrad Meyer last_zone = cur_zone; 524546d70077SConrad Meyer last_size = cur_size; 524648c5777eSRobert Watson } 524748c5777eSRobert Watson } 524803175483SAlexander Motin 524903175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 525003175483SAlexander Motin { 525103175483SAlexander Motin uma_zone_t z; 5252ab3185d1SJeff Roberson uint64_t allocs, frees; 52530f9b7bf3SMark Johnston long cachefree; 52540f9b7bf3SMark Johnston int i; 525503175483SAlexander Motin 525603175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 525703175483SAlexander Motin "Requests", "Bucket"); 525803175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 5259c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 52600f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 52610f9b7bf3SMark Johnston cachefree += z->uz_domain[i].uzd_nitems; 52620f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 526303175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 526403175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 526520a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 526603175483SAlexander Motin if (db_pager_quit) 526703175483SAlexander Motin return; 526803175483SAlexander Motin } 526903175483SAlexander Motin } 52709542ea7bSGleb Smirnoff #endif /* DDB */ 5271