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); 274ec0d8280SRyan 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 32633e5a1eaSRyan Libby static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0, 32733e5a1eaSRyan Libby "Memory allocation debugging"); 32833e5a1eaSRyan Libby 3299542ea7bSGleb Smirnoff #ifdef INVARIANTS 33031c251a0SJeff Roberson static uint64_t uma_keg_get_allocs(uma_keg_t zone); 331815db204SRyan Libby static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg); 332815db204SRyan Libby 333c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem); 334c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem); 3359542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item); 3369542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item); 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 36533e5a1eaSRyan Libby static int multipage_slabs = 1; 36633e5a1eaSRyan Libby TUNABLE_INT("vm.debug.uma_multipage_slabs", &multipage_slabs); 36733e5a1eaSRyan Libby SYSCTL_INT(_vm_debug, OID_AUTO, uma_multipage_slabs, 36833e5a1eaSRyan Libby CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &multipage_slabs, 0, 36933e5a1eaSRyan Libby "UMA may choose larger slab sizes for better efficiency"); 37033e5a1eaSRyan Libby 37186bbae32SJeff Roberson /* 3729b8db4d0SRyan Libby * Select the slab zone for an offpage slab with the given maximum item count. 3739b8db4d0SRyan Libby */ 3749b8db4d0SRyan Libby static inline uma_zone_t 3759b8db4d0SRyan Libby slabzone(int ipers) 3769b8db4d0SRyan Libby { 3779b8db4d0SRyan Libby 3789b8db4d0SRyan Libby return (slabzones[ipers > SLABZONE0_SETSIZE]); 3799b8db4d0SRyan Libby } 3809b8db4d0SRyan Libby 3819b8db4d0SRyan Libby /* 38286bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 38386bbae32SJeff Roberson */ 38486bbae32SJeff Roberson static void 38586bbae32SJeff Roberson bucket_enable(void) 38686bbae32SJeff Roberson { 3873182660aSRyan Libby 388a81c400eSJeff Roberson KASSERT(booted >= BOOT_KVA, ("Bucket enable before init")); 389251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 39086bbae32SJeff Roberson } 39186bbae32SJeff Roberson 392dc2c7965SRobert Watson /* 393dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 394dc2c7965SRobert Watson * 395dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 396fc03d22bSJeff Roberson * of the header and an array of pointers. 397dc2c7965SRobert Watson */ 398cae33c14SJeff Roberson static void 399cae33c14SJeff Roberson bucket_init(void) 400cae33c14SJeff Roberson { 401cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 402cae33c14SJeff Roberson int size; 403cae33c14SJeff Roberson 404d74e6a1dSAlan Cox for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 405cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 406cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 407cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 408e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 409dfe13344SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | 410dfe13344SJeff Roberson UMA_ZONE_FIRSTTOUCH); 411cae33c14SJeff Roberson } 412cae33c14SJeff Roberson } 413cae33c14SJeff Roberson 414dc2c7965SRobert Watson /* 415dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 416dc2c7965SRobert Watson * to allocate the bucket. 417dc2c7965SRobert Watson */ 418dc2c7965SRobert Watson static struct uma_bucket_zone * 419dc2c7965SRobert Watson bucket_zone_lookup(int entries) 420dc2c7965SRobert Watson { 421fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 422dc2c7965SRobert Watson 423fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 424fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 425fc03d22bSJeff Roberson return (ubz); 426fc03d22bSJeff Roberson ubz--; 427fc03d22bSJeff Roberson return (ubz); 428fc03d22bSJeff Roberson } 429fc03d22bSJeff Roberson 430003cf08bSMark Johnston static struct uma_bucket_zone * 431003cf08bSMark Johnston bucket_zone_max(uma_zone_t zone, int nitems) 432003cf08bSMark Johnston { 433003cf08bSMark Johnston struct uma_bucket_zone *ubz; 434003cf08bSMark Johnston int bpcpu; 435003cf08bSMark Johnston 436003cf08bSMark Johnston bpcpu = 2; 437dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 438003cf08bSMark Johnston /* Count the cross-domain bucket. */ 439003cf08bSMark Johnston bpcpu++; 440003cf08bSMark Johnston 441003cf08bSMark Johnston for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 442003cf08bSMark Johnston if (ubz->ubz_entries * bpcpu * mp_ncpus > nitems) 443003cf08bSMark Johnston break; 444003cf08bSMark Johnston if (ubz == &bucket_zones[0]) 445003cf08bSMark Johnston ubz = NULL; 446003cf08bSMark Johnston else 447003cf08bSMark Johnston ubz--; 448003cf08bSMark Johnston return (ubz); 449003cf08bSMark Johnston } 450003cf08bSMark Johnston 451fc03d22bSJeff Roberson static int 452fc03d22bSJeff Roberson bucket_select(int size) 453fc03d22bSJeff Roberson { 454fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 455fc03d22bSJeff Roberson 456fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 457fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 458fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 459fc03d22bSJeff Roberson 460fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 461fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 462fc03d22bSJeff Roberson break; 463fc03d22bSJeff Roberson ubz--; 464fc03d22bSJeff Roberson return (ubz->ubz_entries); 465dc2c7965SRobert Watson } 466dc2c7965SRobert Watson 467cae33c14SJeff Roberson static uma_bucket_t 4686fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags) 469cae33c14SJeff Roberson { 470cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 471cae33c14SJeff Roberson uma_bucket_t bucket; 472cae33c14SJeff Roberson 473cae33c14SJeff Roberson /* 474d4665eaaSJeff Roberson * Don't allocate buckets early in boot. 475cae33c14SJeff Roberson */ 476d4665eaaSJeff Roberson if (__predict_false(booted < BOOT_KVA)) 477cae33c14SJeff Roberson return (NULL); 478a81c400eSJeff Roberson 4796fd34d6fSJeff Roberson /* 4806fd34d6fSJeff Roberson * To limit bucket recursion we store the original zone flags 4816fd34d6fSJeff Roberson * in a cookie passed via zalloc_arg/zfree_arg. This allows the 4826fd34d6fSJeff Roberson * NOVM flag to persist even through deep recursions. We also 4836fd34d6fSJeff Roberson * store ZFLAG_BUCKET once we have recursed attempting to allocate 4846fd34d6fSJeff Roberson * a bucket for a bucket zone so we do not allow infinite bucket 4856fd34d6fSJeff Roberson * recursion. This cookie will even persist to frees of unused 4866fd34d6fSJeff Roberson * buckets via the allocation path or bucket allocations in the 4876fd34d6fSJeff Roberson * free path. 4886fd34d6fSJeff Roberson */ 4896fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 4906fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 491e8a720feSAlexander Motin else { 492e8a720feSAlexander Motin if ((uintptr_t)udata & UMA_ZFLAG_BUCKET) 493e8a720feSAlexander Motin return (NULL); 4946fd34d6fSJeff Roberson udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET); 495e8a720feSAlexander Motin } 496*bae55c4aSRyan Libby if (((uintptr_t)udata & UMA_ZONE_VM) != 0) 497af526374SJeff Roberson flags |= M_NOVM; 49820a4e154SJeff Roberson ubz = bucket_zone_lookup(zone->uz_bucket_size); 49920d3ab87SAlexander Motin if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0) 50020d3ab87SAlexander Motin ubz++; 5016fd34d6fSJeff Roberson bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags); 502cae33c14SJeff Roberson if (bucket) { 503cae33c14SJeff Roberson #ifdef INVARIANTS 504cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 505cae33c14SJeff Roberson #endif 506cae33c14SJeff Roberson bucket->ub_cnt = 0; 507cae33c14SJeff Roberson bucket->ub_entries = ubz->ubz_entries; 508d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 509d4665eaaSJeff Roberson CTR3(KTR_UMA, "bucket_alloc: zone %s(%p) allocated bucket %p", 510d4665eaaSJeff Roberson zone->uz_name, zone, bucket); 511cae33c14SJeff Roberson } 512cae33c14SJeff Roberson 513cae33c14SJeff Roberson return (bucket); 514cae33c14SJeff Roberson } 515cae33c14SJeff Roberson 516cae33c14SJeff Roberson static void 5176fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata) 518cae33c14SJeff Roberson { 519cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 520cae33c14SJeff Roberson 521fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 522fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 523d4665eaaSJeff Roberson KASSERT(bucket->ub_seq == SMR_SEQ_INVALID, 524d4665eaaSJeff Roberson ("bucket_free: Freeing an SMR bucket.")); 5256fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 5266fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 527dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 5286fd34d6fSJeff Roberson uma_zfree_arg(ubz->ubz_zone, bucket, udata); 529cae33c14SJeff Roberson } 530cae33c14SJeff Roberson 531cae33c14SJeff Roberson static void 532cae33c14SJeff Roberson bucket_zone_drain(void) 533cae33c14SJeff Roberson { 534cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 535cae33c14SJeff Roberson 536cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 53708cfa56eSMark Johnston uma_zone_reclaim(ubz->ubz_zone, UMA_RECLAIM_DRAIN); 538cae33c14SJeff Roberson } 539cae33c14SJeff Roberson 54008cfa56eSMark Johnston /* 54108cfa56eSMark Johnston * Attempt to satisfy an allocation by retrieving a full bucket from one of the 542d4665eaaSJeff Roberson * zone's caches. If a bucket is found the zone is not locked on return. 54308cfa56eSMark Johnston */ 5440f9b7bf3SMark Johnston static uma_bucket_t 54508cfa56eSMark Johnston zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom) 5460f9b7bf3SMark Johnston { 5470f9b7bf3SMark Johnston uma_bucket_t bucket; 548d4665eaaSJeff Roberson int i; 549d4665eaaSJeff Roberson bool dtor = false; 5500f9b7bf3SMark Johnston 5510f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 5520f9b7bf3SMark Johnston 553dc3915c8SJeff Roberson if ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) == NULL) 554d4665eaaSJeff Roberson return (NULL); 555d4665eaaSJeff Roberson 556d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 557d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 558d4665eaaSJeff Roberson if (!smr_poll(zone->uz_smr, bucket->ub_seq, false)) 559d4665eaaSJeff Roberson return (NULL); 560d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 561d4665eaaSJeff Roberson dtor = (zone->uz_dtor != NULL) | UMA_ALWAYS_CTORDTOR; 562d4665eaaSJeff Roberson } 5630f9b7bf3SMark Johnston MPASS(zdom->uzd_nitems >= bucket->ub_cnt); 564dc3915c8SJeff Roberson STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link); 5650f9b7bf3SMark Johnston zdom->uzd_nitems -= bucket->ub_cnt; 56608cfa56eSMark Johnston if (zdom->uzd_imin > zdom->uzd_nitems) 5670f9b7bf3SMark Johnston zdom->uzd_imin = zdom->uzd_nitems; 568bb15d1c7SGleb Smirnoff zone->uz_bkt_count -= bucket->ub_cnt; 569d4665eaaSJeff Roberson ZONE_UNLOCK(zone); 570d4665eaaSJeff Roberson if (dtor) 571d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 572d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], zone->uz_size, 573d4665eaaSJeff Roberson NULL, SKIP_NONE); 574d4665eaaSJeff Roberson 5750f9b7bf3SMark Johnston return (bucket); 5760f9b7bf3SMark Johnston } 5770f9b7bf3SMark Johnston 57808cfa56eSMark Johnston /* 57908cfa56eSMark Johnston * Insert a full bucket into the specified cache. The "ws" parameter indicates 58008cfa56eSMark Johnston * whether the bucket's contents should be counted as part of the zone's working 58108cfa56eSMark Johnston * set. 58208cfa56eSMark Johnston */ 5830f9b7bf3SMark Johnston static void 5840f9b7bf3SMark Johnston zone_put_bucket(uma_zone_t zone, uma_zone_domain_t zdom, uma_bucket_t bucket, 5850f9b7bf3SMark Johnston const bool ws) 5860f9b7bf3SMark Johnston { 5870f9b7bf3SMark Johnston 5880f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 58908034d10SKonstantin Belousov KASSERT(!ws || zone->uz_bkt_count < zone->uz_bkt_max, 59008034d10SKonstantin Belousov ("%s: zone %p overflow", __func__, zone)); 5910f9b7bf3SMark Johnston 592dc3915c8SJeff Roberson STAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link); 5930f9b7bf3SMark Johnston zdom->uzd_nitems += bucket->ub_cnt; 5940f9b7bf3SMark Johnston if (ws && zdom->uzd_imax < zdom->uzd_nitems) 5950f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_nitems; 596bb15d1c7SGleb Smirnoff zone->uz_bkt_count += bucket->ub_cnt; 5970f9b7bf3SMark Johnston } 5980f9b7bf3SMark Johnston 599376b1ba3SJeff Roberson /* Pops an item out of a per-cpu cache bucket. */ 600376b1ba3SJeff Roberson static inline void * 601376b1ba3SJeff Roberson cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket) 602376b1ba3SJeff Roberson { 603376b1ba3SJeff Roberson void *item; 604376b1ba3SJeff Roberson 605376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 606376b1ba3SJeff Roberson 607376b1ba3SJeff Roberson bucket->ucb_cnt--; 608376b1ba3SJeff Roberson item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt]; 609376b1ba3SJeff Roberson #ifdef INVARIANTS 610376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL; 611376b1ba3SJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 612376b1ba3SJeff Roberson #endif 613376b1ba3SJeff Roberson cache->uc_allocs++; 614376b1ba3SJeff Roberson 615376b1ba3SJeff Roberson return (item); 616376b1ba3SJeff Roberson } 617376b1ba3SJeff Roberson 618376b1ba3SJeff Roberson /* Pushes an item into a per-cpu cache bucket. */ 619376b1ba3SJeff Roberson static inline void 620376b1ba3SJeff Roberson cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item) 621376b1ba3SJeff Roberson { 622376b1ba3SJeff Roberson 623376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 624376b1ba3SJeff Roberson KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL, 625376b1ba3SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 626376b1ba3SJeff Roberson 627376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item; 628376b1ba3SJeff Roberson bucket->ucb_cnt++; 629376b1ba3SJeff Roberson cache->uc_frees++; 630376b1ba3SJeff Roberson } 631376b1ba3SJeff Roberson 632376b1ba3SJeff Roberson /* 633376b1ba3SJeff Roberson * Unload a UMA bucket from a per-cpu cache. 634376b1ba3SJeff Roberson */ 635376b1ba3SJeff Roberson static inline uma_bucket_t 636376b1ba3SJeff Roberson cache_bucket_unload(uma_cache_bucket_t bucket) 637376b1ba3SJeff Roberson { 638376b1ba3SJeff Roberson uma_bucket_t b; 639376b1ba3SJeff Roberson 640376b1ba3SJeff Roberson b = bucket->ucb_bucket; 641376b1ba3SJeff Roberson if (b != NULL) { 642376b1ba3SJeff Roberson MPASS(b->ub_entries == bucket->ucb_entries); 643376b1ba3SJeff Roberson b->ub_cnt = bucket->ucb_cnt; 644376b1ba3SJeff Roberson bucket->ucb_bucket = NULL; 645376b1ba3SJeff Roberson bucket->ucb_entries = bucket->ucb_cnt = 0; 646376b1ba3SJeff Roberson } 647376b1ba3SJeff Roberson 648376b1ba3SJeff Roberson return (b); 649376b1ba3SJeff Roberson } 650376b1ba3SJeff Roberson 651376b1ba3SJeff Roberson static inline uma_bucket_t 652376b1ba3SJeff Roberson cache_bucket_unload_alloc(uma_cache_t cache) 653376b1ba3SJeff Roberson { 654376b1ba3SJeff Roberson 655376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_allocbucket)); 656376b1ba3SJeff Roberson } 657376b1ba3SJeff Roberson 658376b1ba3SJeff Roberson static inline uma_bucket_t 659376b1ba3SJeff Roberson cache_bucket_unload_free(uma_cache_t cache) 660376b1ba3SJeff Roberson { 661376b1ba3SJeff Roberson 662376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_freebucket)); 663376b1ba3SJeff Roberson } 664376b1ba3SJeff Roberson 665376b1ba3SJeff Roberson static inline uma_bucket_t 666376b1ba3SJeff Roberson cache_bucket_unload_cross(uma_cache_t cache) 667376b1ba3SJeff Roberson { 668376b1ba3SJeff Roberson 669376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_crossbucket)); 670376b1ba3SJeff Roberson } 671376b1ba3SJeff Roberson 672376b1ba3SJeff Roberson /* 673376b1ba3SJeff Roberson * Load a bucket into a per-cpu cache bucket. 674376b1ba3SJeff Roberson */ 675376b1ba3SJeff Roberson static inline void 676376b1ba3SJeff Roberson cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b) 677376b1ba3SJeff Roberson { 678376b1ba3SJeff Roberson 679376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 680376b1ba3SJeff Roberson MPASS(bucket->ucb_bucket == NULL); 681376b1ba3SJeff Roberson 682376b1ba3SJeff Roberson bucket->ucb_bucket = b; 683376b1ba3SJeff Roberson bucket->ucb_cnt = b->ub_cnt; 684376b1ba3SJeff Roberson bucket->ucb_entries = b->ub_entries; 685376b1ba3SJeff Roberson } 686376b1ba3SJeff Roberson 687376b1ba3SJeff Roberson static inline void 688376b1ba3SJeff Roberson cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b) 689376b1ba3SJeff Roberson { 690376b1ba3SJeff Roberson 691376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_allocbucket, b); 692376b1ba3SJeff Roberson } 693376b1ba3SJeff Roberson 694376b1ba3SJeff Roberson static inline void 695376b1ba3SJeff Roberson cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b) 696376b1ba3SJeff Roberson { 697376b1ba3SJeff Roberson 698376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_freebucket, b); 699376b1ba3SJeff Roberson } 700376b1ba3SJeff Roberson 701dfe13344SJeff Roberson #ifdef NUMA 702376b1ba3SJeff Roberson static inline void 703376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b) 704376b1ba3SJeff Roberson { 705376b1ba3SJeff Roberson 706376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_crossbucket, b); 707376b1ba3SJeff Roberson } 708376b1ba3SJeff Roberson #endif 709376b1ba3SJeff Roberson 710376b1ba3SJeff Roberson /* 711376b1ba3SJeff Roberson * Copy and preserve ucb_spare. 712376b1ba3SJeff Roberson */ 713376b1ba3SJeff Roberson static inline void 714376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 715376b1ba3SJeff Roberson { 716376b1ba3SJeff Roberson 717376b1ba3SJeff Roberson b1->ucb_bucket = b2->ucb_bucket; 718376b1ba3SJeff Roberson b1->ucb_entries = b2->ucb_entries; 719376b1ba3SJeff Roberson b1->ucb_cnt = b2->ucb_cnt; 720376b1ba3SJeff Roberson } 721376b1ba3SJeff Roberson 722376b1ba3SJeff Roberson /* 723376b1ba3SJeff Roberson * Swap two cache buckets. 724376b1ba3SJeff Roberson */ 725376b1ba3SJeff Roberson static inline void 726376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 727376b1ba3SJeff Roberson { 728376b1ba3SJeff Roberson struct uma_cache_bucket b3; 729376b1ba3SJeff Roberson 730376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 731376b1ba3SJeff Roberson 732376b1ba3SJeff Roberson cache_bucket_copy(&b3, b1); 733376b1ba3SJeff Roberson cache_bucket_copy(b1, b2); 734376b1ba3SJeff Roberson cache_bucket_copy(b2, &b3); 735376b1ba3SJeff Roberson } 736376b1ba3SJeff Roberson 7372f891cd5SPawel Jakub Dawidek static void 7382f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 7392f891cd5SPawel Jakub Dawidek { 7402f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 7412f891cd5SPawel Jakub Dawidek 7422f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 7432f891cd5SPawel Jakub Dawidek return; 7442f891cd5SPawel Jakub Dawidek 7452f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 7462f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 7472f891cd5SPawel Jakub Dawidek } 7482f891cd5SPawel Jakub Dawidek 74954503a13SJonathan T. Looney static inline void 75054503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone) 75154503a13SJonathan T. Looney { 752e60b2fcbSGleb Smirnoff 753e60b2fcbSGleb Smirnoff if (zone->uz_maxaction.ta_func != NULL) 754e60b2fcbSGleb Smirnoff taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction); 75554503a13SJonathan T. Looney } 75654503a13SJonathan T. Looney 7578355f576SJeff Roberson /* 7588355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 7599643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 7608355f576SJeff Roberson * 7618355f576SJeff Roberson * Arguments: 7628355f576SJeff Roberson * arg Unused 7638355f576SJeff Roberson * 7648355f576SJeff Roberson * Returns: 7658355f576SJeff Roberson * Nothing 7668355f576SJeff Roberson */ 7678355f576SJeff Roberson static void 7688355f576SJeff Roberson uma_timeout(void *unused) 7698355f576SJeff Roberson { 77086bbae32SJeff Roberson bucket_enable(); 77120a4e154SJeff Roberson zone_foreach(zone_timeout, NULL); 7728355f576SJeff Roberson 7738355f576SJeff Roberson /* Reschedule this event */ 7749643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 7758355f576SJeff Roberson } 7768355f576SJeff Roberson 7778355f576SJeff Roberson /* 7780f9b7bf3SMark Johnston * Update the working set size estimate for the zone's bucket cache. 7790f9b7bf3SMark Johnston * The constants chosen here are somewhat arbitrary. With an update period of 7800f9b7bf3SMark Johnston * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the 7810f9b7bf3SMark Johnston * last 100s. 7820f9b7bf3SMark Johnston */ 7830f9b7bf3SMark Johnston static void 7840f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom) 7850f9b7bf3SMark Johnston { 7860f9b7bf3SMark Johnston long wss; 7870f9b7bf3SMark Johnston 7880f9b7bf3SMark Johnston MPASS(zdom->uzd_imax >= zdom->uzd_imin); 7890f9b7bf3SMark Johnston wss = zdom->uzd_imax - zdom->uzd_imin; 7900f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems; 79108cfa56eSMark Johnston zdom->uzd_wss = (4 * wss + zdom->uzd_wss) / 5; 7920f9b7bf3SMark Johnston } 7930f9b7bf3SMark Johnston 7940f9b7bf3SMark Johnston /* 7959643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 7969643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 7978355f576SJeff Roberson * 798e20a199fSJeff Roberson * Returns nothing. 7998355f576SJeff Roberson */ 8008355f576SJeff Roberson static void 80120a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused) 8028355f576SJeff Roberson { 80308034d10SKonstantin Belousov uma_keg_t keg; 8048b987a77SJeff Roberson u_int slabs, pages; 8058355f576SJeff Roberson 80654c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 80708034d10SKonstantin Belousov goto update_wss; 80808034d10SKonstantin Belousov 80908034d10SKonstantin Belousov keg = zone->uz_keg; 8108b987a77SJeff Roberson 8118b987a77SJeff Roberson /* 8128b987a77SJeff Roberson * Hash zones are non-numa by definition so the first domain 8138b987a77SJeff Roberson * is the only one present. 8148b987a77SJeff Roberson */ 8158b987a77SJeff Roberson KEG_LOCK(keg, 0); 8168b987a77SJeff Roberson pages = keg->uk_domain[0].ud_pages; 8178b987a77SJeff Roberson 8188355f576SJeff Roberson /* 819e20a199fSJeff Roberson * Expand the keg hash table. 8208355f576SJeff Roberson * 8218355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 8228355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 8238355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 8248355f576SJeff Roberson */ 8258b987a77SJeff Roberson if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) { 8260aef6126SJeff Roberson struct uma_hash newhash; 8270aef6126SJeff Roberson struct uma_hash oldhash; 8280aef6126SJeff Roberson int ret; 8295300d9ddSJeff Roberson 8300aef6126SJeff Roberson /* 8310aef6126SJeff Roberson * This is so involved because allocating and freeing 832e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 8330aef6126SJeff Roberson * I have to do everything in stages and check for 8340aef6126SJeff Roberson * races. 8350aef6126SJeff Roberson */ 8368b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 8373b2f2cb8SAlexander Motin ret = hash_alloc(&newhash, 1 << fls(slabs)); 8388b987a77SJeff Roberson KEG_LOCK(keg, 0); 8390aef6126SJeff Roberson if (ret) { 840099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 841099a0e58SBosko Milekic oldhash = keg->uk_hash; 842099a0e58SBosko Milekic keg->uk_hash = newhash; 8430aef6126SJeff Roberson } else 8440aef6126SJeff Roberson oldhash = newhash; 8450aef6126SJeff Roberson 8468b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 8470aef6126SJeff Roberson hash_free(&oldhash); 8488b987a77SJeff Roberson goto update_wss; 8490aef6126SJeff Roberson } 8505300d9ddSJeff Roberson } 8518b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 852e20a199fSJeff Roberson 85308034d10SKonstantin Belousov update_wss: 85408cfa56eSMark Johnston ZONE_LOCK(zone); 855bb15d1c7SGleb Smirnoff for (int i = 0; i < vm_ndomains; i++) 8560f9b7bf3SMark Johnston zone_domain_update_wss(&zone->uz_domain[i]); 85708cfa56eSMark Johnston ZONE_UNLOCK(zone); 8588355f576SJeff Roberson } 8598355f576SJeff Roberson 8608355f576SJeff Roberson /* 8615300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 8625300d9ddSJeff Roberson * backing store. 8635300d9ddSJeff Roberson * 8645300d9ddSJeff Roberson * Arguments: 8650aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 8665300d9ddSJeff Roberson * 8675300d9ddSJeff Roberson * Returns: 868763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 8695300d9ddSJeff Roberson */ 87037c84183SPoul-Henning Kamp static int 8713b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 8725300d9ddSJeff Roberson { 87359568a0eSAlexander Motin size_t alloc; 8745300d9ddSJeff Roberson 8753b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 8763b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 8773b2f2cb8SAlexander Motin hash->uh_hashsize = size; 8780aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 8791e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 8805300d9ddSJeff Roberson } else { 8810aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 882e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 883ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 8840aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 8855300d9ddSJeff Roberson } 8860aef6126SJeff Roberson if (hash->uh_slab_hash) { 8870aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 8880aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 8890aef6126SJeff Roberson return (1); 8900aef6126SJeff Roberson } 8915300d9ddSJeff Roberson 8920aef6126SJeff Roberson return (0); 8935300d9ddSJeff Roberson } 8945300d9ddSJeff Roberson 8955300d9ddSJeff Roberson /* 89664f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 89764f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 89864f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 8998355f576SJeff Roberson * 9008355f576SJeff Roberson * Arguments: 9010aef6126SJeff Roberson * oldhash The hash you want to expand 9020aef6126SJeff Roberson * newhash The hash structure for the new table 9038355f576SJeff Roberson * 9048355f576SJeff Roberson * Returns: 9058355f576SJeff Roberson * Nothing 9068355f576SJeff Roberson * 9078355f576SJeff Roberson * Discussion: 9088355f576SJeff Roberson */ 9090aef6126SJeff Roberson static int 9100aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 9118355f576SJeff Roberson { 9121e0701e1SJeff Roberson uma_hash_slab_t slab; 9136929b7d1SPedro F. Giffuni u_int hval; 9146929b7d1SPedro F. Giffuni u_int idx; 9158355f576SJeff Roberson 9160aef6126SJeff Roberson if (!newhash->uh_slab_hash) 9170aef6126SJeff Roberson return (0); 9188355f576SJeff Roberson 9190aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 9200aef6126SJeff Roberson return (0); 9218355f576SJeff Roberson 9228355f576SJeff Roberson /* 9238355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 9248355f576SJeff Roberson * full rehash. 9258355f576SJeff Roberson */ 9268355f576SJeff Roberson 9276929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 9281e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 9291e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 9301e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 9311e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 9321e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 9331e0701e1SJeff Roberson slab, uhs_hlink); 9348355f576SJeff Roberson } 9358355f576SJeff Roberson 9360aef6126SJeff Roberson return (1); 9379c2cd7e5SJeff Roberson } 9389c2cd7e5SJeff Roberson 9395300d9ddSJeff Roberson /* 9405300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 9415300d9ddSJeff Roberson * 9425300d9ddSJeff Roberson * Arguments: 9435300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 9445300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 9455300d9ddSJeff Roberson * 9465300d9ddSJeff Roberson * Returns: 9475300d9ddSJeff Roberson * Nothing 9485300d9ddSJeff Roberson */ 9499c2cd7e5SJeff Roberson static void 9500aef6126SJeff Roberson hash_free(struct uma_hash *hash) 9519c2cd7e5SJeff Roberson { 9520aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 9530aef6126SJeff Roberson return; 9540aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 9550095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 9568355f576SJeff Roberson else 957961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 9588355f576SJeff Roberson } 9598355f576SJeff Roberson 9608355f576SJeff Roberson /* 9618355f576SJeff Roberson * Frees all outstanding items in a bucket 9628355f576SJeff Roberson * 9638355f576SJeff Roberson * Arguments: 9648355f576SJeff Roberson * zone The zone to free to, must be unlocked. 9654bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 9668355f576SJeff Roberson * 9678355f576SJeff Roberson * Returns: 9688355f576SJeff Roberson * Nothing 9698355f576SJeff Roberson */ 9708355f576SJeff Roberson 9718355f576SJeff Roberson static void 9728355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 9738355f576SJeff Roberson { 9740095a784SJeff Roberson int i; 9758355f576SJeff Roberson 9764bd61e19SJeff Roberson if (bucket == NULL || bucket->ub_cnt == 0) 9778355f576SJeff Roberson return; 9788355f576SJeff Roberson 979d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 980d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 981d4665eaaSJeff Roberson smr_wait(zone->uz_smr, bucket->ub_seq); 982d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 983d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], 984d4665eaaSJeff Roberson zone->uz_size, NULL, SKIP_NONE); 985d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 986d4665eaaSJeff Roberson } 9870095a784SJeff Roberson if (zone->uz_fini) 9880095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 9890095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 9900095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 9914bd61e19SJeff Roberson if (zone->uz_max_items > 0) 9924bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 993d4665eaaSJeff Roberson #ifdef INVARIANTS 994d4665eaaSJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt); 995d4665eaaSJeff Roberson #endif 9960095a784SJeff Roberson bucket->ub_cnt = 0; 9978355f576SJeff Roberson } 9988355f576SJeff Roberson 9998355f576SJeff Roberson /* 10008355f576SJeff Roberson * Drains the per cpu caches for a zone. 10018355f576SJeff Roberson * 1002727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 10035d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 10045d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 10055d1ae027SRobert Watson * 10068355f576SJeff Roberson * Arguments: 10078355f576SJeff Roberson * zone The zone to drain, must be unlocked. 10088355f576SJeff Roberson * 10098355f576SJeff Roberson * Returns: 10108355f576SJeff Roberson * Nothing 10118355f576SJeff Roberson */ 10128355f576SJeff Roberson static void 10139643769aSJeff Roberson cache_drain(uma_zone_t zone) 10148355f576SJeff Roberson { 10158355f576SJeff Roberson uma_cache_t cache; 1016376b1ba3SJeff Roberson uma_bucket_t bucket; 10178355f576SJeff Roberson int cpu; 10188355f576SJeff Roberson 10198355f576SJeff Roberson /* 10205d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 10215d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 10225d1ae027SRobert Watson * of the caches at this point. 10235d1ae027SRobert Watson * 10245d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 10255d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 10268355f576SJeff Roberson */ 10273aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 10288355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 1029376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 1030376b1ba3SJeff Roberson if (bucket != NULL) { 1031376b1ba3SJeff Roberson bucket_drain(zone, bucket); 1032376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1033376b1ba3SJeff Roberson } 1034376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 1035376b1ba3SJeff Roberson if (bucket != NULL) { 1036376b1ba3SJeff Roberson bucket_drain(zone, bucket); 1037376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1038376b1ba3SJeff Roberson } 1039376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 1040376b1ba3SJeff Roberson if (bucket != NULL) { 1041376b1ba3SJeff Roberson bucket_drain(zone, bucket); 1042376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1043376b1ba3SJeff Roberson } 1044d56368d7SBosko Milekic } 104508cfa56eSMark Johnston bucket_cache_reclaim(zone, true); 1046aaa8bb16SJeff Roberson } 1047aaa8bb16SJeff Roberson 1048a2de44abSAlexander Motin static void 104920a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 1050a2de44abSAlexander Motin { 1051a2de44abSAlexander Motin 1052a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1053a2de44abSAlexander Motin return; 1054a2de44abSAlexander Motin 1055a2de44abSAlexander Motin ZONE_LOCK(zone); 105620a4e154SJeff Roberson zone->uz_bucket_size = 105720a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 1058a2de44abSAlexander Motin ZONE_UNLOCK(zone); 1059a2de44abSAlexander Motin } 1060a2de44abSAlexander Motin 1061a2de44abSAlexander Motin static void 106220a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 1063a2de44abSAlexander Motin { 1064a2de44abSAlexander Motin uma_cache_t cache; 1065c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 1066ab3185d1SJeff Roberson int domain; 1067a2de44abSAlexander Motin 1068a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1069a2de44abSAlexander Motin return; 1070a2de44abSAlexander Motin 1071c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1072a2de44abSAlexander Motin ZONE_LOCK(zone); 1073a2de44abSAlexander Motin critical_enter(); 1074dfe13344SJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 1075ab3185d1SJeff Roberson domain = PCPU_GET(domain); 1076ab3185d1SJeff Roberson else 1077ab3185d1SJeff Roberson domain = 0; 1078a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1079376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1080376b1ba3SJeff Roberson if (b1 != NULL && b1->ub_cnt != 0) { 1081376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b1, false); 1082376b1ba3SJeff Roberson b1 = NULL; 1083a2de44abSAlexander Motin } 1084d4665eaaSJeff Roberson 1085d4665eaaSJeff Roberson /* 1086d4665eaaSJeff Roberson * Don't flush SMR zone buckets. This leaves the zone without a 1087d4665eaaSJeff Roberson * bucket and forces every free to synchronize(). 1088d4665eaaSJeff Roberson */ 1089d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 1090d4665eaaSJeff Roberson goto out; 1091376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1092376b1ba3SJeff Roberson if (b2 != NULL && b2->ub_cnt != 0) { 1093376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b2, false); 1094376b1ba3SJeff Roberson b2 = NULL; 1095a2de44abSAlexander Motin } 1096376b1ba3SJeff Roberson b3 = cache_bucket_unload_cross(cache); 1097d4665eaaSJeff Roberson 1098d4665eaaSJeff Roberson out: 1099a2de44abSAlexander Motin critical_exit(); 1100a2de44abSAlexander Motin ZONE_UNLOCK(zone); 11018a8d9d14SAlexander Motin if (b1) 11028a8d9d14SAlexander Motin bucket_free(zone, b1, NULL); 11038a8d9d14SAlexander Motin if (b2) 11048a8d9d14SAlexander Motin bucket_free(zone, b2, NULL); 1105c1685086SJeff Roberson if (b3) { 1106c1685086SJeff Roberson bucket_drain(zone, b3); 1107c1685086SJeff Roberson bucket_free(zone, b3, NULL); 1108c1685086SJeff Roberson } 1109a2de44abSAlexander Motin } 1110a2de44abSAlexander Motin 1111a2de44abSAlexander Motin /* 1112a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1113a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1114a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1115a2de44abSAlexander Motin * to safely access their cache buckets. 1116a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1117a2de44abSAlexander Motin */ 1118a2de44abSAlexander Motin static void 111908cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1120a2de44abSAlexander Motin { 1121a2de44abSAlexander Motin int cpu; 1122a2de44abSAlexander Motin 1123a2de44abSAlexander Motin /* 1124727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1125a2de44abSAlexander Motin */ 1126a2de44abSAlexander Motin if (zone) 112720a4e154SJeff Roberson cache_shrink(zone, NULL); 1128a2de44abSAlexander Motin else 112920a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1130a2de44abSAlexander Motin 1131a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1132a2de44abSAlexander Motin thread_lock(curthread); 1133a2de44abSAlexander Motin sched_bind(curthread, cpu); 1134a2de44abSAlexander Motin thread_unlock(curthread); 1135a2de44abSAlexander Motin 1136a2de44abSAlexander Motin if (zone) 113720a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1138a2de44abSAlexander Motin else 113920a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1140a2de44abSAlexander Motin } 1141a2de44abSAlexander Motin thread_lock(curthread); 1142a2de44abSAlexander Motin sched_unbind(curthread); 1143a2de44abSAlexander Motin thread_unlock(curthread); 1144a2de44abSAlexander Motin } 1145a2de44abSAlexander Motin 1146aaa8bb16SJeff Roberson /* 114708cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 114808cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 114908cfa56eSMark Johnston * estimated working set size. 1150aaa8bb16SJeff Roberson */ 1151aaa8bb16SJeff Roberson static void 115208cfa56eSMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain) 1153aaa8bb16SJeff Roberson { 1154ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1155aaa8bb16SJeff Roberson uma_bucket_t bucket; 115608cfa56eSMark Johnston long target, tofree; 1157ab3185d1SJeff Roberson int i; 11588355f576SJeff Roberson 1159ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 116091d947bfSJeff Roberson /* 116191d947bfSJeff Roberson * The cross bucket is partially filled and not part of 116291d947bfSJeff Roberson * the item count. Reclaim it individually here. 116391d947bfSJeff Roberson */ 1164ab3185d1SJeff Roberson zdom = &zone->uz_domain[i]; 116591d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 116691d947bfSJeff Roberson bucket = zdom->uzd_cross; 116791d947bfSJeff Roberson zdom->uzd_cross = NULL; 116891d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 116991d947bfSJeff Roberson if (bucket != NULL) { 117091d947bfSJeff Roberson bucket_drain(zone, bucket); 117191d947bfSJeff Roberson bucket_free(zone, bucket, NULL); 117291d947bfSJeff Roberson } 117391d947bfSJeff Roberson 117491d947bfSJeff Roberson /* 117591d947bfSJeff Roberson * Shrink the zone bucket size to ensure that the per-CPU caches 117691d947bfSJeff Roberson * don't grow too large. 117791d947bfSJeff Roberson */ 117891d947bfSJeff Roberson ZONE_LOCK(zone); 117991d947bfSJeff Roberson if (i == 0 && zone->uz_bucket_size > zone->uz_bucket_size_min) 118091d947bfSJeff Roberson zone->uz_bucket_size--; 118108cfa56eSMark Johnston 118208cfa56eSMark Johnston /* 118308cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 118408cfa56eSMark Johnston * this bucket cache is empty. Otherwise, we reclaim items in 118508cfa56eSMark Johnston * excess of the zone's estimated working set size. If the 118608cfa56eSMark Johnston * difference nitems - imin is larger than the WSS estimate, 118708cfa56eSMark Johnston * then the estimate will grow at the end of this interval and 118808cfa56eSMark Johnston * we ignore the historical average. 118908cfa56eSMark Johnston */ 119008cfa56eSMark Johnston target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems - 119108cfa56eSMark Johnston zdom->uzd_imin); 119208cfa56eSMark Johnston while (zdom->uzd_nitems > target) { 1193dc3915c8SJeff Roberson bucket = STAILQ_FIRST(&zdom->uzd_buckets); 119408cfa56eSMark Johnston if (bucket == NULL) 119508cfa56eSMark Johnston break; 119608cfa56eSMark Johnston tofree = bucket->ub_cnt; 1197dc3915c8SJeff Roberson STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link); 119808cfa56eSMark Johnston zdom->uzd_nitems -= tofree; 119908cfa56eSMark Johnston 120008cfa56eSMark Johnston /* 120108cfa56eSMark Johnston * Shift the bounds of the current WSS interval to avoid 120208cfa56eSMark Johnston * perturbing the estimate. 120308cfa56eSMark Johnston */ 120408cfa56eSMark Johnston zdom->uzd_imax -= lmin(zdom->uzd_imax, tofree); 120508cfa56eSMark Johnston zdom->uzd_imin -= lmin(zdom->uzd_imin, tofree); 120608cfa56eSMark Johnston 12078355f576SJeff Roberson ZONE_UNLOCK(zone); 12088355f576SJeff Roberson bucket_drain(zone, bucket); 12096fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 12108355f576SJeff Roberson ZONE_LOCK(zone); 12118355f576SJeff Roberson } 121291d947bfSJeff Roberson ZONE_UNLOCK(zone); 1213ab3185d1SJeff Roberson } 12148355f576SJeff Roberson } 1215fc03d22bSJeff Roberson 1216fc03d22bSJeff Roberson static void 1217fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1218fc03d22bSJeff Roberson { 1219fc03d22bSJeff Roberson uint8_t *mem; 1220fc03d22bSJeff Roberson int i; 1221fc03d22bSJeff Roberson uint8_t flags; 1222fc03d22bSJeff Roberson 12231431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 12241431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 12251431a748SGleb Smirnoff 12261e0701e1SJeff Roberson mem = slab_data(slab, keg); 1227fc03d22bSJeff Roberson flags = slab->us_flags; 1228fc03d22bSJeff Roberson i = start; 1229fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 1230fc03d22bSJeff Roberson for (i--; i > -1; i--) 1231c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1232c5deaf04SGleb Smirnoff /* 1233c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1234c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1235c5deaf04SGleb Smirnoff * which executed trash_dtor. 1236c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1237c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1238c5deaf04SGleb Smirnoff * invocations. 1239c5deaf04SGleb Smirnoff */ 12401e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1241c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1242c5deaf04SGleb Smirnoff #endif 12431e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1244fc03d22bSJeff Roberson } 124554c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 12469b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab), 12479b8db4d0SRyan Libby NULL, SKIP_NONE); 1248fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 12492e47807cSJeff Roberson uma_total_dec(PAGE_SIZE * keg->uk_ppera); 12508355f576SJeff Roberson } 12518355f576SJeff Roberson 12528355f576SJeff Roberson /* 1253e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 12548355f576SJeff Roberson * the pageout daemon. 12558355f576SJeff Roberson * 1256e20a199fSJeff Roberson * Returns nothing. 12578355f576SJeff Roberson */ 1258e20a199fSJeff Roberson static void 1259e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 12608355f576SJeff Roberson { 12611e183df2SStefan Farfeleder struct slabhead freeslabs = { 0 }; 1262ab3185d1SJeff Roberson uma_domain_t dom; 1263829be516SMark Johnston uma_slab_t slab, tmp; 12648b987a77SJeff Roberson int i, n; 12658355f576SJeff Roberson 12668355f576SJeff Roberson /* 1267e20a199fSJeff Roberson * We don't want to take pages from statically allocated kegs at this 12688355f576SJeff Roberson * time 12698355f576SJeff Roberson */ 1270099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 12718355f576SJeff Roberson return; 12728355f576SJeff Roberson 1273ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 12748b987a77SJeff Roberson CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u", 12758b987a77SJeff Roberson keg->uk_name, keg, i, dom->ud_free); 12768b987a77SJeff Roberson n = 0; 1277ab3185d1SJeff Roberson dom = &keg->uk_domain[i]; 12788b987a77SJeff Roberson KEG_LOCK(keg, i); 1279ab3185d1SJeff Roberson LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) { 128054c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 12811e0701e1SJeff Roberson UMA_HASH_REMOVE(&keg->uk_hash, slab); 12828b987a77SJeff Roberson n++; 12838b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 12841e0701e1SJeff Roberson LIST_INSERT_HEAD(&freeslabs, slab, us_link); 1285713deb36SJeff Roberson } 12868b987a77SJeff Roberson dom->ud_pages -= n * keg->uk_ppera; 12878b987a77SJeff Roberson dom->ud_free -= n * keg->uk_ipers; 12888b987a77SJeff Roberson KEG_UNLOCK(keg, i); 1289ab3185d1SJeff Roberson } 1290ab3185d1SJeff Roberson 12911e0701e1SJeff Roberson while ((slab = LIST_FIRST(&freeslabs)) != NULL) { 12921e0701e1SJeff Roberson LIST_REMOVE(slab, us_link); 12931645995bSKirk McKusick keg_free_slab(keg, slab, keg->uk_ipers); 12948355f576SJeff Roberson } 12958355f576SJeff Roberson } 12968355f576SJeff Roberson 1297e20a199fSJeff Roberson static void 129808cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain) 1299e20a199fSJeff Roberson { 1300e20a199fSJeff Roberson 13018355f576SJeff Roberson /* 1302e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 1303e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 1304e20a199fSJeff Roberson * is the only call that knows the structure will still be available 1305e20a199fSJeff Roberson * when it wakes up. 1306e20a199fSJeff Roberson */ 1307e20a199fSJeff Roberson ZONE_LOCK(zone); 130808cfa56eSMark Johnston while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) { 1309e20a199fSJeff Roberson if (waitok == M_NOWAIT) 1310e20a199fSJeff Roberson goto out; 1311727c6918SJeff Roberson msleep(zone, &zone->uz_lock, PVM, "zonedrain", 1); 1312e20a199fSJeff Roberson } 131308cfa56eSMark Johnston zone->uz_flags |= UMA_ZFLAG_RECLAIMING; 1314e20a199fSJeff Roberson ZONE_UNLOCK(zone); 131591d947bfSJeff Roberson bucket_cache_reclaim(zone, drain); 131608cfa56eSMark Johnston 1317e20a199fSJeff Roberson /* 1318e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 1319111fbcd5SBryan Venteicher * we're running. Normally the uma_rwlock would protect us but we 1320e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 1321e20a199fSJeff Roberson */ 132208034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1323bb15d1c7SGleb Smirnoff keg_drain(zone->uz_keg); 1324e20a199fSJeff Roberson ZONE_LOCK(zone); 132508cfa56eSMark Johnston zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING; 1326e20a199fSJeff Roberson wakeup(zone); 1327e20a199fSJeff Roberson out: 1328e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1329e20a199fSJeff Roberson } 1330e20a199fSJeff Roberson 133108cfa56eSMark Johnston static void 133220a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused) 1333e20a199fSJeff Roberson { 1334e20a199fSJeff Roberson 133508cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, true); 133608cfa56eSMark Johnston } 133708cfa56eSMark Johnston 133808cfa56eSMark Johnston static void 133920a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused) 134008cfa56eSMark Johnston { 134108cfa56eSMark Johnston 134208cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, false); 1343e20a199fSJeff Roberson } 1344e20a199fSJeff Roberson 1345e20a199fSJeff Roberson /* 13468b987a77SJeff Roberson * Allocate a new slab for a keg and inserts it into the partial slab list. 13478b987a77SJeff Roberson * The keg should be unlocked on entry. If the allocation succeeds it will 13488b987a77SJeff Roberson * be locked on return. 13498355f576SJeff Roberson * 13508355f576SJeff Roberson * Arguments: 135186220393SMark Johnston * flags Wait flags for the item initialization routine 135286220393SMark Johnston * aflags Wait flags for the slab allocation 13538355f576SJeff Roberson * 13548355f576SJeff Roberson * Returns: 13558355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 13568355f576SJeff Roberson * caller specified M_NOWAIT. 13578355f576SJeff Roberson */ 13588355f576SJeff Roberson static uma_slab_t 135986220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 136086220393SMark Johnston int aflags) 13618355f576SJeff Roberson { 13628b987a77SJeff Roberson uma_domain_t dom; 1363e20a199fSJeff Roberson uma_alloc allocf; 1364099a0e58SBosko Milekic uma_slab_t slab; 13652e47807cSJeff Roberson unsigned long size; 136685dcf349SGleb Smirnoff uint8_t *mem; 136786220393SMark Johnston uint8_t sflags; 13688355f576SJeff Roberson int i; 13698355f576SJeff Roberson 1370ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1371ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1372a553d4b8SJeff Roberson 13738b987a77SJeff Roberson allocf = keg->uk_allocf; 1374194a979eSMark Johnston slab = NULL; 1375194a979eSMark Johnston mem = NULL; 137654c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 13779b8db4d0SRyan Libby uma_hash_slab_t hslab; 13789b8db4d0SRyan Libby hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL, 13799b8db4d0SRyan Libby domain, aflags); 13809b8db4d0SRyan Libby if (hslab == NULL) 1381727c6918SJeff Roberson goto fail; 13829b8db4d0SRyan Libby slab = &hslab->uhs_slab; 1383a553d4b8SJeff Roberson } 1384a553d4b8SJeff Roberson 13853370c5bfSJeff Roberson /* 13863370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 13873370c5bfSJeff Roberson * first time they are added to a zone. 13883370c5bfSJeff Roberson * 13893370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 13903370c5bfSJeff Roberson */ 13913370c5bfSJeff Roberson 1392099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 139386220393SMark Johnston aflags |= M_ZERO; 13943370c5bfSJeff Roberson else 139586220393SMark Johnston aflags &= ~M_ZERO; 13963370c5bfSJeff Roberson 1397263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 139886220393SMark Johnston aflags |= M_NODUMP; 1399263811f7SKip Macy 1400e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1401194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 140286220393SMark Johnston mem = allocf(zone, size, domain, &sflags, aflags); 1403a553d4b8SJeff Roberson if (mem == NULL) { 140454c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 14059b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), 14069b8db4d0SRyan Libby slab_tohashslab(slab), NULL, SKIP_NONE); 1407727c6918SJeff Roberson goto fail; 1408a553d4b8SJeff Roberson } 14092e47807cSJeff Roberson uma_total_inc(size); 14108355f576SJeff Roberson 14118b987a77SJeff Roberson /* For HASH zones all pages go to the same uma_domain. */ 141254c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 14138b987a77SJeff Roberson domain = 0; 14148b987a77SJeff Roberson 14155c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 141654c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) 1417099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 14181e0701e1SJeff Roberson else 14199b8db4d0SRyan Libby slab_tohashslab(slab)->uhs_data = mem; 14205c0e403bSJeff Roberson 142154c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_VTOSLAB) 1422099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1423584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1424584061b4SJeff Roberson zone, slab); 14258355f576SJeff Roberson 1426099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 142786220393SMark Johnston slab->us_flags = sflags; 1428ab3185d1SJeff Roberson slab->us_domain = domain; 14298b987a77SJeff Roberson 14309b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1431ef72505eSJeff Roberson #ifdef INVARIANTS 1432815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1433ef72505eSJeff Roberson #endif 1434099a0e58SBosko Milekic 1435b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1436099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 14371e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 143886220393SMark Johnston keg->uk_size, flags) != 0) 1439b23f72e9SBrian Feldman break; 1440b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1441fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1442727c6918SJeff Roberson goto fail; 1443b23f72e9SBrian Feldman } 1444b23f72e9SBrian Feldman } 14458b987a77SJeff Roberson KEG_LOCK(keg, domain); 14465c0e403bSJeff Roberson 14471431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 14481431a748SGleb Smirnoff slab, keg->uk_name, keg); 14491431a748SGleb Smirnoff 145054c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 1451099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 14528355f576SJeff Roberson 14538b987a77SJeff Roberson /* 14548b987a77SJeff Roberson * If we got a slab here it's safe to mark it partially used 14558b987a77SJeff Roberson * and return. We assume that the caller is going to remove 14568b987a77SJeff Roberson * at least one item. 14578b987a77SJeff Roberson */ 14588b987a77SJeff Roberson dom = &keg->uk_domain[domain]; 14598b987a77SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 14608b987a77SJeff Roberson dom->ud_pages += keg->uk_ppera; 14618b987a77SJeff Roberson dom->ud_free += keg->uk_ipers; 14628355f576SJeff Roberson 14638355f576SJeff Roberson return (slab); 1464727c6918SJeff Roberson 1465727c6918SJeff Roberson fail: 1466727c6918SJeff Roberson return (NULL); 14678355f576SJeff Roberson } 14688355f576SJeff Roberson 14698355f576SJeff Roberson /* 1470009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1471009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1472009b6fcbSJeff Roberson * the VM is ready. 1473009b6fcbSJeff Roberson */ 1474009b6fcbSJeff Roberson static void * 1475ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1476ab3185d1SJeff Roberson int wait) 1477009b6fcbSJeff Roberson { 1478a81c400eSJeff Roberson vm_paddr_t pa; 1479a81c400eSJeff Roberson vm_page_t m; 1480ac0a6fd0SGleb Smirnoff void *mem; 1481ac0a6fd0SGleb Smirnoff int pages; 1482a81c400eSJeff Roberson int i; 1483099a0e58SBosko Milekic 1484f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1485f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1486a81c400eSJeff Roberson 1487f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1488a81c400eSJeff Roberson m = vm_page_alloc_contig_domain(NULL, 0, domain, 1489a81c400eSJeff Roberson malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages, 1490a81c400eSJeff Roberson (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT); 1491a81c400eSJeff Roberson if (m == NULL) 1492a81c400eSJeff Roberson return (NULL); 1493a81c400eSJeff Roberson 1494a81c400eSJeff Roberson pa = VM_PAGE_TO_PHYS(m); 1495a81c400eSJeff Roberson for (i = 0; i < pages; i++, pa += PAGE_SIZE) { 1496a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1497a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1498a81c400eSJeff Roberson if ((wait & M_NODUMP) == 0) 1499a81c400eSJeff Roberson dump_add_page(pa); 1500a81c400eSJeff Roberson #endif 1501a81c400eSJeff Roberson } 1502a81c400eSJeff Roberson /* Allocate KVA and indirectly advance bootmem. */ 1503a81c400eSJeff Roberson mem = (void *)pmap_map(&bootmem, m->phys_addr, 1504a81c400eSJeff Roberson m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE); 1505a81c400eSJeff Roberson if ((wait & M_ZERO) != 0) 1506a81c400eSJeff Roberson bzero(mem, pages * PAGE_SIZE); 1507f7d35785SGleb Smirnoff 1508f7d35785SGleb Smirnoff return (mem); 1509f7d35785SGleb Smirnoff } 1510f7d35785SGleb Smirnoff 1511a81c400eSJeff Roberson static void 1512a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes) 1513a81c400eSJeff Roberson { 1514a81c400eSJeff Roberson vm_offset_t va; 1515a81c400eSJeff Roberson vm_page_t m; 1516a81c400eSJeff Roberson 1517a81c400eSJeff Roberson va = (vm_offset_t)mem; 1518a81c400eSJeff Roberson m = PHYS_TO_VM_PAGE(pmap_kextract(va)); 1519a81c400eSJeff Roberson pmap_remove(kernel_pmap, va, va + bytes); 1520a81c400eSJeff Roberson for (; bytes != 0; bytes -= PAGE_SIZE, m++) { 1521a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1522a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1523a81c400eSJeff Roberson dump_drop_page(VM_PAGE_TO_PHYS(m)); 1524a81c400eSJeff Roberson #endif 1525a81c400eSJeff Roberson vm_page_unwire_noq(m); 1526a81c400eSJeff Roberson vm_page_free(m); 1527a81c400eSJeff Roberson } 1528a81c400eSJeff Roberson } 1529a81c400eSJeff Roberson 1530f7d35785SGleb Smirnoff /* 15318355f576SJeff Roberson * Allocates a number of pages from the system 15328355f576SJeff Roberson * 15338355f576SJeff Roberson * Arguments: 15348355f576SJeff Roberson * bytes The number of bytes requested 15358355f576SJeff Roberson * wait Shall we wait? 15368355f576SJeff Roberson * 15378355f576SJeff Roberson * Returns: 15388355f576SJeff Roberson * A pointer to the alloced memory or possibly 15398355f576SJeff Roberson * NULL if M_NOWAIT is set. 15408355f576SJeff Roberson */ 15418355f576SJeff Roberson static void * 1542ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1543ab3185d1SJeff Roberson int wait) 15448355f576SJeff Roberson { 15458355f576SJeff Roberson void *p; /* Returned page */ 15468355f576SJeff Roberson 15472e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 15489978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 15498355f576SJeff Roberson 15508355f576SJeff Roberson return (p); 15518355f576SJeff Roberson } 15528355f576SJeff Roberson 1553ab3059a8SMatt Macy static void * 1554ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1555ab3059a8SMatt Macy int wait) 1556ab3059a8SMatt Macy { 1557ab3059a8SMatt Macy struct pglist alloctail; 1558ab3059a8SMatt Macy vm_offset_t addr, zkva; 1559ab3059a8SMatt Macy int cpu, flags; 1560ab3059a8SMatt Macy vm_page_t p, p_next; 1561ab3059a8SMatt Macy #ifdef NUMA 1562ab3059a8SMatt Macy struct pcpu *pc; 1563ab3059a8SMatt Macy #endif 1564ab3059a8SMatt Macy 1565ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1566ab3059a8SMatt Macy 1567013072f0SMark Johnston TAILQ_INIT(&alloctail); 1568ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1569013072f0SMark Johnston malloc2vm_flags(wait); 1570013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1571ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1572ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1573ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1574ab3059a8SMatt Macy } else { 1575ab3059a8SMatt Macy #ifndef NUMA 1576ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1577ab3059a8SMatt Macy #else 1578ab3059a8SMatt Macy pc = pcpu_find(cpu); 157920526802SAndrew Gallatin if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain))) 158020526802SAndrew Gallatin p = NULL; 158120526802SAndrew Gallatin else 158220526802SAndrew Gallatin p = vm_page_alloc_domain(NULL, 0, 158320526802SAndrew Gallatin pc->pc_domain, flags); 1584ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1585ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1586ab3059a8SMatt Macy #endif 1587ab3059a8SMatt Macy } 1588ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1589ab3059a8SMatt Macy goto fail; 1590ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1591ab3059a8SMatt Macy } 1592ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1593ab3059a8SMatt Macy goto fail; 1594ab3059a8SMatt Macy zkva = addr; 1595ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1596ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1597ab3059a8SMatt Macy zkva += PAGE_SIZE; 1598ab3059a8SMatt Macy } 1599ab3059a8SMatt Macy return ((void*)addr); 1600ab3059a8SMatt Macy fail: 1601ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 160288ea538aSMark Johnston vm_page_unwire_noq(p); 1603ab3059a8SMatt Macy vm_page_free(p); 1604ab3059a8SMatt Macy } 1605ab3059a8SMatt Macy return (NULL); 1606ab3059a8SMatt Macy } 1607ab3059a8SMatt Macy 16088355f576SJeff Roberson /* 16098355f576SJeff Roberson * Allocates a number of pages from within an object 16108355f576SJeff Roberson * 16118355f576SJeff Roberson * Arguments: 16128355f576SJeff Roberson * bytes The number of bytes requested 16138355f576SJeff Roberson * wait Shall we wait? 16148355f576SJeff Roberson * 16158355f576SJeff Roberson * Returns: 16168355f576SJeff Roberson * A pointer to the alloced memory or possibly 16178355f576SJeff Roberson * NULL if M_NOWAIT is set. 16188355f576SJeff Roberson */ 16198355f576SJeff Roberson static void * 1620ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1621ab3185d1SJeff Roberson int wait) 16228355f576SJeff Roberson { 1623a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1624a4915c21SAttilio Rao u_long npages; 1625b245ac95SAlan Cox vm_offset_t retkva, zkva; 1626a4915c21SAttilio Rao vm_page_t p, p_next; 1627e20a199fSJeff Roberson uma_keg_t keg; 16288355f576SJeff Roberson 1629a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1630bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1631a4915c21SAttilio Rao 1632a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1633a4915c21SAttilio Rao while (npages > 0) { 1634ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 16358d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1636772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1637772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1638a4915c21SAttilio Rao if (p != NULL) { 1639a4915c21SAttilio Rao /* 1640a4915c21SAttilio Rao * Since the page does not belong to an object, its 1641a4915c21SAttilio Rao * listq is unused. 1642a4915c21SAttilio Rao */ 1643a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1644a4915c21SAttilio Rao npages--; 1645a4915c21SAttilio Rao continue; 1646a4915c21SAttilio Rao } 16478355f576SJeff Roberson /* 1648a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1649a4915c21SAttilio Rao * exit. 16508355f576SJeff Roberson */ 1651a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 165288ea538aSMark Johnston vm_page_unwire_noq(p); 1653b245ac95SAlan Cox vm_page_free(p); 1654b245ac95SAlan Cox } 1655a4915c21SAttilio Rao return (NULL); 1656b245ac95SAlan Cox } 16578355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1658a4915c21SAttilio Rao zkva = keg->uk_kva + 1659a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1660a4915c21SAttilio Rao retkva = zkva; 1661a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1662a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1663a4915c21SAttilio Rao zkva += PAGE_SIZE; 1664a4915c21SAttilio Rao } 16658355f576SJeff Roberson 16668355f576SJeff Roberson return ((void *)retkva); 16678355f576SJeff Roberson } 16688355f576SJeff Roberson 16698355f576SJeff Roberson /* 1670ec0d8280SRyan Libby * Allocate physically contiguous pages. 1671ec0d8280SRyan Libby */ 1672ec0d8280SRyan Libby static void * 1673ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1674ec0d8280SRyan Libby int wait) 1675ec0d8280SRyan Libby { 1676ec0d8280SRyan Libby 1677ec0d8280SRyan Libby *pflag = UMA_SLAB_KERNEL; 1678ec0d8280SRyan Libby return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain), 1679ec0d8280SRyan Libby bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT)); 1680ec0d8280SRyan Libby } 1681ec0d8280SRyan Libby 1682ec0d8280SRyan Libby /* 16838355f576SJeff Roberson * Frees a number of pages to the system 16848355f576SJeff Roberson * 16858355f576SJeff Roberson * Arguments: 16868355f576SJeff Roberson * mem A pointer to the memory to be freed 16878355f576SJeff Roberson * size The size of the memory being freed 16888355f576SJeff Roberson * flags The original p->us_flags field 16898355f576SJeff Roberson * 16908355f576SJeff Roberson * Returns: 16918355f576SJeff Roberson * Nothing 16928355f576SJeff Roberson */ 16938355f576SJeff Roberson static void 1694f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 16958355f576SJeff Roberson { 16963370c5bfSJeff Roberson 1697a81c400eSJeff Roberson if ((flags & UMA_SLAB_BOOT) != 0) { 1698a81c400eSJeff Roberson startup_free(mem, size); 1699a81c400eSJeff Roberson return; 1700a81c400eSJeff Roberson } 1701a81c400eSJeff Roberson 1702ec0d8280SRyan Libby KASSERT((flags & UMA_SLAB_KERNEL) != 0, 1703ec0d8280SRyan Libby ("UMA: page_free used with invalid flags %x", flags)); 17048355f576SJeff Roberson 170549bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 17068355f576SJeff Roberson } 17078355f576SJeff Roberson 17088355f576SJeff Roberson /* 1709ab3059a8SMatt Macy * Frees pcpu zone allocations 1710ab3059a8SMatt Macy * 1711ab3059a8SMatt Macy * Arguments: 1712ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1713ab3059a8SMatt Macy * size The size of the memory being freed 1714ab3059a8SMatt Macy * flags The original p->us_flags field 1715ab3059a8SMatt Macy * 1716ab3059a8SMatt Macy * Returns: 1717ab3059a8SMatt Macy * Nothing 1718ab3059a8SMatt Macy */ 1719ab3059a8SMatt Macy static void 1720ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1721ab3059a8SMatt Macy { 1722ab3059a8SMatt Macy vm_offset_t sva, curva; 1723ab3059a8SMatt Macy vm_paddr_t paddr; 1724ab3059a8SMatt Macy vm_page_t m; 1725ab3059a8SMatt Macy 1726ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 17275ba16cf3SRyan Libby 17285ba16cf3SRyan Libby if ((flags & UMA_SLAB_BOOT) != 0) { 17295ba16cf3SRyan Libby startup_free(mem, size); 17305ba16cf3SRyan Libby return; 17315ba16cf3SRyan Libby } 17325ba16cf3SRyan Libby 1733ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1734ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1735ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1736ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 173788ea538aSMark Johnston vm_page_unwire_noq(m); 1738ab3059a8SMatt Macy vm_page_free(m); 1739ab3059a8SMatt Macy } 1740ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1741ab3059a8SMatt Macy kva_free(sva, size); 1742ab3059a8SMatt Macy } 1743ab3059a8SMatt Macy 1744ab3059a8SMatt Macy 1745ab3059a8SMatt Macy /* 17468355f576SJeff Roberson * Zero fill initializer 17478355f576SJeff Roberson * 17488355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 17498355f576SJeff Roberson */ 1750b23f72e9SBrian Feldman static int 1751b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 17528355f576SJeff Roberson { 17538355f576SJeff Roberson bzero(mem, size); 1754b23f72e9SBrian Feldman return (0); 17558355f576SJeff Roberson } 17568355f576SJeff Roberson 1757815db204SRyan Libby #ifdef INVARIANTS 1758815db204SRyan Libby struct noslabbits * 1759815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1760815db204SRyan Libby { 1761815db204SRyan Libby 1762815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1763815db204SRyan Libby } 1764815db204SRyan Libby #endif 1765815db204SRyan Libby 17668355f576SJeff Roberson /* 17679b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 17689b78b1f4SJeff Roberson */ 17699b78b1f4SJeff Roberson size_t 17709b78b1f4SJeff Roberson slab_sizeof(int nitems) 17719b78b1f4SJeff Roberson { 17729b78b1f4SJeff Roberson size_t s; 17739b78b1f4SJeff Roberson 1774815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 17759b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 17769b78b1f4SJeff Roberson } 17779b78b1f4SJeff Roberson 17789b78b1f4SJeff Roberson /* 17799b78b1f4SJeff Roberson * Size of memory for embedded slabs (!OFFPAGE). 17809b78b1f4SJeff Roberson */ 17819b78b1f4SJeff Roberson size_t 17829b78b1f4SJeff Roberson slab_space(int nitems) 17839b78b1f4SJeff Roberson { 17849b78b1f4SJeff Roberson return (UMA_SLAB_SIZE - slab_sizeof(nitems)); 17859b78b1f4SJeff Roberson } 17869b78b1f4SJeff Roberson 17874a8b575cSRyan Libby #define UMA_FIXPT_SHIFT 31 17884a8b575cSRyan Libby #define UMA_FRAC_FIXPT(n, d) \ 17894a8b575cSRyan Libby ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d))) 17904a8b575cSRyan Libby #define UMA_FIXPT_PCT(f) \ 17914a8b575cSRyan Libby ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT)) 17924a8b575cSRyan Libby #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100) 17934a8b575cSRyan Libby #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE) 17944a8b575cSRyan Libby 17959b78b1f4SJeff Roberson /* 17964a8b575cSRyan Libby * Compute the number of items that will fit in a slab. If hdr is true, the 17974a8b575cSRyan Libby * item count may be limited to provide space in the slab for an inline slab 17984a8b575cSRyan Libby * header. Otherwise, all slab space will be provided for item storage. 17994a8b575cSRyan Libby */ 18004a8b575cSRyan Libby static u_int 18014a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr) 18024a8b575cSRyan Libby { 18034a8b575cSRyan Libby u_int ipers; 18044a8b575cSRyan Libby u_int padpi; 18054a8b575cSRyan Libby 18064a8b575cSRyan Libby /* The padding between items is not needed after the last item. */ 18074a8b575cSRyan Libby padpi = rsize - size; 18084a8b575cSRyan Libby 18094a8b575cSRyan Libby if (hdr) { 18104a8b575cSRyan Libby /* 18114a8b575cSRyan Libby * Start with the maximum item count and remove items until 18124a8b575cSRyan Libby * the slab header first alongside the allocatable memory. 18134a8b575cSRyan Libby */ 18144a8b575cSRyan Libby for (ipers = MIN(SLAB_MAX_SETSIZE, 18154a8b575cSRyan Libby (slabsize + padpi - slab_sizeof(1)) / rsize); 18164a8b575cSRyan Libby ipers > 0 && 18174a8b575cSRyan Libby ipers * rsize - padpi + slab_sizeof(ipers) > slabsize; 18184a8b575cSRyan Libby ipers--) 18194a8b575cSRyan Libby continue; 18204a8b575cSRyan Libby } else { 18214a8b575cSRyan Libby ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE); 18224a8b575cSRyan Libby } 18234a8b575cSRyan Libby 18244a8b575cSRyan Libby return (ipers); 18254a8b575cSRyan Libby } 18264a8b575cSRyan Libby 18274a8b575cSRyan Libby /* 18284a8b575cSRyan Libby * Compute the number of items that will fit in a slab for a startup zone. 18299b78b1f4SJeff Roberson */ 18309b78b1f4SJeff Roberson int 18319b78b1f4SJeff Roberson slab_ipers(size_t size, int align) 18329b78b1f4SJeff Roberson { 18339b78b1f4SJeff Roberson int rsize; 18349b78b1f4SJeff Roberson 18354a8b575cSRyan Libby rsize = roundup(size, align + 1); /* Assume no CACHESPREAD */ 18364a8b575cSRyan Libby return (slab_ipers_hdr(size, rsize, UMA_SLAB_SIZE, true)); 18379b78b1f4SJeff Roberson } 18389b78b1f4SJeff Roberson 183927ca37acSRyan Libby struct keg_layout_result { 184027ca37acSRyan Libby u_int format; 184127ca37acSRyan Libby u_int slabsize; 184227ca37acSRyan Libby u_int ipers; 184327ca37acSRyan Libby u_int eff; 184427ca37acSRyan Libby }; 184527ca37acSRyan Libby 184627ca37acSRyan Libby static void 184727ca37acSRyan Libby keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt, 184827ca37acSRyan Libby struct keg_layout_result *kl) 184927ca37acSRyan Libby { 185027ca37acSRyan Libby u_int total; 185127ca37acSRyan Libby 185227ca37acSRyan Libby kl->format = fmt; 185327ca37acSRyan Libby kl->slabsize = slabsize; 185427ca37acSRyan Libby 185527ca37acSRyan Libby /* Handle INTERNAL as inline with an extra page. */ 185627ca37acSRyan Libby if ((fmt & UMA_ZFLAG_INTERNAL) != 0) { 185727ca37acSRyan Libby kl->format &= ~UMA_ZFLAG_INTERNAL; 185827ca37acSRyan Libby kl->slabsize += PAGE_SIZE; 185927ca37acSRyan Libby } 186027ca37acSRyan Libby 186127ca37acSRyan Libby kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize, 186227ca37acSRyan Libby (fmt & UMA_ZFLAG_OFFPAGE) == 0); 186327ca37acSRyan Libby 186427ca37acSRyan Libby /* Account for memory used by an offpage slab header. */ 186527ca37acSRyan Libby total = kl->slabsize; 186627ca37acSRyan Libby if ((fmt & UMA_ZFLAG_OFFPAGE) != 0) 186727ca37acSRyan Libby total += slabzone(kl->ipers)->uz_keg->uk_rsize; 186827ca37acSRyan Libby 186927ca37acSRyan Libby kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total); 187027ca37acSRyan Libby } 187127ca37acSRyan Libby 18729b78b1f4SJeff Roberson /* 18734a8b575cSRyan Libby * Determine the format of a uma keg. This determines where the slab header 18744a8b575cSRyan Libby * will be placed (inline or offpage) and calculates ipers, rsize, and ppera. 18758355f576SJeff Roberson * 18768355f576SJeff Roberson * Arguments 1877e20a199fSJeff Roberson * keg The zone we should initialize 18788355f576SJeff Roberson * 18798355f576SJeff Roberson * Returns 18808355f576SJeff Roberson * Nothing 18818355f576SJeff Roberson */ 18828355f576SJeff Roberson static void 18834a8b575cSRyan Libby keg_layout(uma_keg_t keg) 18848355f576SJeff Roberson { 188527ca37acSRyan Libby struct keg_layout_result kl = {}, kl_tmp; 188627ca37acSRyan Libby u_int fmts[2]; 18874a8b575cSRyan Libby u_int alignsize; 188827ca37acSRyan Libby u_int nfmt; 18894a8b575cSRyan Libby u_int pages; 1890244f4554SBosko Milekic u_int rsize; 1891a55ebb7cSAndriy Gapon u_int slabsize; 189227ca37acSRyan Libby u_int i, j; 18938355f576SJeff Roberson 18944a8b575cSRyan Libby KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 18954a8b575cSRyan Libby (keg->uk_size <= UMA_PCPU_ALLOC_SIZE && 18964a8b575cSRyan Libby (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0), 18974a8b575cSRyan Libby ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b", 18984a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_size, keg->uk_flags, 18994a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 1900*bae55c4aSRyan Libby KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 || 19014a8b575cSRyan Libby (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0, 19024a8b575cSRyan Libby ("%s: incompatible flags 0x%b", __func__, keg->uk_flags, 19034a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 1904e28a647dSGleb Smirnoff 19054a8b575cSRyan Libby alignsize = keg->uk_align + 1; 1906ad97af7eSGleb Smirnoff 1907ef72505eSJeff Roberson /* 1908ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1909ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1910ef72505eSJeff Roberson * allocation bits for we round it up. 1911ef72505eSJeff Roberson */ 19129b8db4d0SRyan Libby rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT); 19134a8b575cSRyan Libby rsize = roundup2(rsize, alignsize); 1914ad97af7eSGleb Smirnoff 191527ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) { 19169b78b1f4SJeff Roberson /* 19174a8b575cSRyan Libby * We want one item to start on every align boundary in a page. 19184a8b575cSRyan Libby * To do this we will span pages. We will also extend the item 19194a8b575cSRyan Libby * by the size of align if it is an even multiple of align. 19204a8b575cSRyan Libby * Otherwise, it would fall on the same boundary every time. 19219b78b1f4SJeff Roberson */ 19224a8b575cSRyan Libby if ((rsize & alignsize) == 0) 19234a8b575cSRyan Libby rsize += alignsize; 19244a8b575cSRyan Libby slabsize = rsize * (PAGE_SIZE / alignsize); 19254a8b575cSRyan Libby slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE); 19264a8b575cSRyan Libby slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE); 192727ca37acSRyan Libby slabsize = round_page(slabsize); 19284a8b575cSRyan Libby } else { 19294a8b575cSRyan Libby /* 193027ca37acSRyan Libby * Start with a slab size of as many pages as it takes to 193127ca37acSRyan Libby * represent a single item. We will try to fit as many 193227ca37acSRyan Libby * additional items into the slab as possible. 19334a8b575cSRyan Libby */ 193427ca37acSRyan Libby slabsize = round_page(keg->uk_size); 19351ca6ed45SGleb Smirnoff } 1936ad97af7eSGleb Smirnoff 193727ca37acSRyan Libby /* Build a list of all of the available formats for this keg. */ 193827ca37acSRyan Libby nfmt = 0; 193927ca37acSRyan Libby 19404a8b575cSRyan Libby /* Evaluate an inline slab layout. */ 19414a8b575cSRyan Libby if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0) 194227ca37acSRyan Libby fmts[nfmt++] = 0; 19434a8b575cSRyan Libby 19444a8b575cSRyan Libby /* TODO: vm_page-embedded slab. */ 1945244f4554SBosko Milekic 194620e8e865SBosko Milekic /* 1947244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 194820e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 1949*bae55c4aSRyan Libby * may end up going to the VM for slabs which we do not want 1950*bae55c4aSRyan Libby * to do if we're UMA_ZONE_VM, which clearly forbids it. 1951*bae55c4aSRyan Libby * In those cases, evaluate a pseudo-format called INTERNAL 1952*bae55c4aSRyan Libby * which has an inline slab header and one extra page to 1953*bae55c4aSRyan Libby * guarantee that it fits. 195427ca37acSRyan Libby * 195527ca37acSRyan Libby * Otherwise, see if using an OFFPAGE slab will improve our 195627ca37acSRyan Libby * efficiency. 195720e8e865SBosko Milekic */ 1958*bae55c4aSRyan Libby if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0) 195927ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_INTERNAL; 196027ca37acSRyan Libby else 196127ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_OFFPAGE; 1962244f4554SBosko Milekic 1963ef72505eSJeff Roberson /* 196427ca37acSRyan Libby * Choose a slab size and format which satisfy the minimum efficiency. 196527ca37acSRyan Libby * Prefer the smallest slab size that meets the constraints. 1966ef72505eSJeff Roberson * 196727ca37acSRyan Libby * Start with a minimum slab size, to accommodate CACHESPREAD. Then, 196827ca37acSRyan Libby * for small items (up to PAGE_SIZE), the iteration increment is one 196927ca37acSRyan Libby * page; and for large items, the increment is one item. 1970ef72505eSJeff Roberson */ 197127ca37acSRyan Libby i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize); 197227ca37acSRyan Libby KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u", 197327ca37acSRyan Libby keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize, 197427ca37acSRyan Libby rsize, i)); 197527ca37acSRyan Libby for ( ; ; i++) { 197627ca37acSRyan Libby slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) : 197727ca37acSRyan Libby round_page(rsize * (i - 1) + keg->uk_size); 197827ca37acSRyan Libby 197927ca37acSRyan Libby for (j = 0; j < nfmt; j++) { 198027ca37acSRyan Libby /* Only if we have no viable format yet. */ 198127ca37acSRyan Libby if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 && 198227ca37acSRyan Libby kl.ipers > 0) 198327ca37acSRyan Libby continue; 198427ca37acSRyan Libby 198527ca37acSRyan Libby keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp); 198627ca37acSRyan Libby if (kl_tmp.eff <= kl.eff) 198727ca37acSRyan Libby continue; 198827ca37acSRyan Libby 198927ca37acSRyan Libby kl = kl_tmp; 199027ca37acSRyan Libby 199127ca37acSRyan Libby CTR6(KTR_UMA, "keg %s layout: format %#x " 199227ca37acSRyan Libby "(ipers %u * rsize %u) / slabsize %#x = %u%% eff", 199327ca37acSRyan Libby keg->uk_name, kl.format, kl.ipers, rsize, 199427ca37acSRyan Libby kl.slabsize, UMA_FIXPT_PCT(kl.eff)); 199527ca37acSRyan Libby 199627ca37acSRyan Libby /* Stop when we reach the minimum efficiency. */ 199727ca37acSRyan Libby if (kl.eff >= UMA_MIN_EFF) 199827ca37acSRyan Libby break; 19998355f576SJeff Roberson } 2000ad97af7eSGleb Smirnoff 200133e5a1eaSRyan Libby if (kl.eff >= UMA_MIN_EFF || !multipage_slabs || 200227ca37acSRyan Libby slabsize >= SLAB_MAX_SETSIZE * rsize || 200327ca37acSRyan Libby (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0) 200427ca37acSRyan Libby break; 200527ca37acSRyan Libby } 200627ca37acSRyan Libby 200727ca37acSRyan Libby pages = atop(kl.slabsize); 200827ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 200927ca37acSRyan Libby pages *= mp_maxid + 1; 201027ca37acSRyan Libby 201127ca37acSRyan Libby keg->uk_rsize = rsize; 201227ca37acSRyan Libby keg->uk_ipers = kl.ipers; 201327ca37acSRyan Libby keg->uk_ppera = pages; 201427ca37acSRyan Libby keg->uk_flags |= kl.format; 201527ca37acSRyan Libby 20164a8b575cSRyan Libby /* 20174a8b575cSRyan Libby * How do we find the slab header if it is offpage or if not all item 20184a8b575cSRyan Libby * start addresses are in the same page? We could solve the latter 20194a8b575cSRyan Libby * case with vaddr alignment, but we don't. 20204a8b575cSRyan Libby */ 202127ca37acSRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 || 202227ca37acSRyan Libby (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) { 202354c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0) 202427ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_HASH; 202554c5ae80SRyan Libby else 202627ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 202754c5ae80SRyan Libby } 202827ca37acSRyan Libby 2029e63a1c2fSRyan Libby CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u", 203027ca37acSRyan Libby __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers, 203127ca37acSRyan Libby pages); 20324a8b575cSRyan Libby KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 20334a8b575cSRyan Libby ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__, 203427ca37acSRyan Libby keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize, 203527ca37acSRyan Libby keg->uk_ipers, pages)); 2036e20a199fSJeff Roberson } 2037e20a199fSJeff Roberson 20388355f576SJeff Roberson /* 2039099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 2040099a0e58SBosko Milekic * the keg onto the global keg list. 20418355f576SJeff Roberson * 20428355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 2043099a0e58SBosko Milekic * udata Actually uma_kctor_args 2044099a0e58SBosko Milekic */ 2045b23f72e9SBrian Feldman static int 2046b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 2047099a0e58SBosko Milekic { 2048099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 2049099a0e58SBosko Milekic uma_keg_t keg = mem; 2050099a0e58SBosko Milekic uma_zone_t zone; 20518b987a77SJeff Roberson int i; 2052099a0e58SBosko Milekic 2053099a0e58SBosko Milekic bzero(keg, size); 2054099a0e58SBosko Milekic keg->uk_size = arg->size; 2055099a0e58SBosko Milekic keg->uk_init = arg->uminit; 2056099a0e58SBosko Milekic keg->uk_fini = arg->fini; 2057099a0e58SBosko Milekic keg->uk_align = arg->align; 20586fd34d6fSJeff Roberson keg->uk_reserve = 0; 2059099a0e58SBosko Milekic keg->uk_flags = arg->flags; 2060099a0e58SBosko Milekic 2061099a0e58SBosko Milekic /* 2062194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 2063dfe13344SJeff Roberson * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which 2064dfe13344SJeff Roberson * case the iterator is never run. 2065194a979eSMark Johnston */ 2066194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 2067194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 2068194a979eSMark Johnston 2069194a979eSMark Johnston /* 2070099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 2071099a0e58SBosko Milekic */ 2072099a0e58SBosko Milekic zone = arg->zone; 2073e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 2074099a0e58SBosko Milekic 2075099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 2076099a0e58SBosko Milekic keg->uk_init = zero_init; 2077099a0e58SBosko Milekic 2078cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 207954c5ae80SRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 2080e20a199fSJeff Roberson 208154c5ae80SRyan Libby #ifndef SMP 2082ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 2083ad97af7eSGleb Smirnoff #endif 2084ad97af7eSGleb Smirnoff 20854a8b575cSRyan Libby keg_layout(keg); 2086099a0e58SBosko Milekic 20878b987a77SJeff Roberson /* 2088dfe13344SJeff Roberson * Use a first-touch NUMA policy for all kegs that pmap_extract() 2089dfe13344SJeff Roberson * will work on with the exception of critical VM structures 2090dfe13344SJeff Roberson * necessary for paging. 2091dfe13344SJeff Roberson * 2092dfe13344SJeff Roberson * Zones may override the default by specifying either. 20938b987a77SJeff Roberson */ 2094dfe13344SJeff Roberson #ifdef NUMA 2095dfe13344SJeff Roberson if ((keg->uk_flags & 209654c5ae80SRyan Libby (UMA_ZFLAG_HASH | UMA_ZONE_VM | UMA_ZONE_ROUNDROBIN)) == 0) 2097dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_FIRSTTOUCH; 2098dfe13344SJeff Roberson else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2099dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_ROUNDROBIN; 21008b987a77SJeff Roberson #endif 21018b987a77SJeff Roberson 2102099a0e58SBosko Milekic /* 2103099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 2104099a0e58SBosko Milekic * startup cache until the vm is ready. 2105099a0e58SBosko Milekic */ 210677e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2107a81c400eSJeff Roberson if (keg->uk_ppera == 1) 210877e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 2109a81c400eSJeff Roberson else 21108cd02d00SAlan Cox #endif 2111a81c400eSJeff Roberson if (booted < BOOT_KVA) 2112a81c400eSJeff Roberson keg->uk_allocf = startup_alloc; 2113ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 2114ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 2115ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1) 2116ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 211777e19437SGleb Smirnoff else 211877e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 211977e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 212077e19437SGleb Smirnoff if (keg->uk_ppera == 1) 212177e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 212277e19437SGleb Smirnoff else 212377e19437SGleb Smirnoff #endif 2124ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 2125ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 2126ab3059a8SMatt Macy else 212777e19437SGleb Smirnoff keg->uk_freef = page_free; 2128099a0e58SBosko Milekic 2129099a0e58SBosko Milekic /* 21308b987a77SJeff Roberson * Initialize keg's locks. 2131099a0e58SBosko Milekic */ 21328b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 21338b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 2134099a0e58SBosko Milekic 2135099a0e58SBosko Milekic /* 2136099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 21379b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 21389b78b1f4SJeff Roberson * definition. 2139099a0e58SBosko Milekic */ 214054c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) { 21419b78b1f4SJeff Roberson size_t shsize; 21429b78b1f4SJeff Roberson 21439b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 21449b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 2145244f4554SBosko Milekic /* 2146244f4554SBosko Milekic * The only way the following is possible is if with our 2147244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 2148244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 2149244f4554SBosko Milekic * mathematically possible for all cases, so we make 2150244f4554SBosko Milekic * sure here anyway. 2151244f4554SBosko Milekic */ 21529b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 21533d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 21543d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 2155099a0e58SBosko Milekic } 2156099a0e58SBosko Milekic 215754c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 21583b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 2159099a0e58SBosko Milekic 2160e63a1c2fSRyan Libby CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone); 2161099a0e58SBosko Milekic 2162099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 2163099a0e58SBosko Milekic 2164111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2165099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2166111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2167b23f72e9SBrian Feldman return (0); 2168099a0e58SBosko Milekic } 2169099a0e58SBosko Milekic 21702efcc8cbSGleb Smirnoff static void 2171a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused) 2172a81c400eSJeff Roberson { 2173a81c400eSJeff Roberson uma_keg_t keg; 2174a81c400eSJeff Roberson 2175a81c400eSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 2176a81c400eSJeff Roberson return; 2177a81c400eSJeff Roberson KEG_GET(zone, keg); 2178ec0d8280SRyan Libby 2179ec0d8280SRyan Libby if (keg->uk_allocf == startup_alloc) { 2180ec0d8280SRyan Libby /* Switch to the real allocator. */ 2181f96d4157SJeff Roberson if (keg->uk_flags & UMA_ZONE_PCPU) 2182f96d4157SJeff Roberson keg->uk_allocf = pcpu_page_alloc; 2183ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && 2184ec0d8280SRyan Libby keg->uk_ppera > 1) 2185ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 2186ec0d8280SRyan Libby else 2187a81c400eSJeff Roberson keg->uk_allocf = page_alloc; 2188a81c400eSJeff Roberson } 2189ec0d8280SRyan Libby } 2190a81c400eSJeff Roberson 2191a81c400eSJeff Roberson static void 219220a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 21932efcc8cbSGleb Smirnoff { 21942efcc8cbSGleb Smirnoff 21952efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 21962efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 21972efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 21982efcc8cbSGleb Smirnoff } 21992efcc8cbSGleb Smirnoff 220020a4e154SJeff Roberson static void 220120a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 220220a4e154SJeff Roberson { 220320a4e154SJeff Roberson uma_zone_domain_t zdom; 22048b987a77SJeff Roberson uma_domain_t dom; 220520a4e154SJeff Roberson uma_keg_t keg; 220620a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 22073b490537SJeff Roberson int domains, i, cnt; 220820a4e154SJeff Roberson static const char *nokeg = "cache zone"; 220920a4e154SJeff Roberson char *c; 221020a4e154SJeff Roberson 221120a4e154SJeff Roberson /* 221220a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 221320a4e154SJeff Roberson * any special characters and handling dups by appending 221420a4e154SJeff Roberson * an index. 221520a4e154SJeff Roberson */ 221620a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 22173b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 22183b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 22193b490537SJeff Roberson cnt /= 10; 22203b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 22213b490537SJeff Roberson M_UMA, M_WAITOK); 222220a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 222320a4e154SJeff Roberson zone->uz_namecnt); 222420a4e154SJeff Roberson } else 222520a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 222620a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 222720a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 222820a4e154SJeff Roberson *c = '_'; 222920a4e154SJeff Roberson 223020a4e154SJeff Roberson /* 223120a4e154SJeff Roberson * Basic parameters at the root. 223220a4e154SJeff Roberson */ 223320a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 223420a4e154SJeff Roberson OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, ""); 223520a4e154SJeff Roberson oid = zone->uz_oid; 223620a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 223720a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 22386d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22396d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 22406d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 224120a4e154SJeff Roberson "Allocator configuration flags"); 224220a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 224320a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 224420a4e154SJeff Roberson "Desired per-cpu cache size"); 224520a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 224620a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 224720a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 224820a4e154SJeff Roberson 224920a4e154SJeff Roberson /* 225020a4e154SJeff Roberson * keg if present. 225120a4e154SJeff Roberson */ 225254c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 22538b987a77SJeff Roberson domains = vm_ndomains; 22548b987a77SJeff Roberson else 22558b987a77SJeff Roberson domains = 1; 225620a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 225720a4e154SJeff Roberson "keg", CTLFLAG_RD, NULL, ""); 225820a4e154SJeff Roberson keg = zone->uz_keg; 22593b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 226020a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 226120a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 226220a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 226320a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 226420a4e154SJeff Roberson "Real object size with alignment"); 226520a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 226620a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 226720a4e154SJeff Roberson "pages per-slab allocation"); 226820a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 226920a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 227020a4e154SJeff Roberson "items available per-slab"); 227120a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 227220a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 227320a4e154SJeff Roberson "item alignment mask"); 2274f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2275f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2276f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2277f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 22788b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 22798b987a77SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 22808b987a77SJeff Roberson for (i = 0; i < domains; i++) { 22818b987a77SJeff Roberson dom = &keg->uk_domain[i]; 22828b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 22838b987a77SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, 22848b987a77SJeff Roberson NULL, ""); 22858b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22868b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 22878b987a77SJeff Roberson "Total pages currently allocated from VM"); 22888b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22898b987a77SJeff Roberson "free", CTLFLAG_RD, &dom->ud_free, 0, 22908b987a77SJeff Roberson "items free in the slab layer"); 22918b987a77SJeff Roberson } 229220a4e154SJeff Roberson } else 229320a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 229420a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 229520a4e154SJeff Roberson 229620a4e154SJeff Roberson /* 229720a4e154SJeff Roberson * Information about zone limits. 229820a4e154SJeff Roberson */ 229920a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 230020a4e154SJeff Roberson "limit", CTLFLAG_RD, NULL, ""); 23014bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 23024bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 23034bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 23044bd61e19SJeff Roberson "current number of allocated items if limit is set"); 230520a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 230620a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 230720a4e154SJeff Roberson "Maximum number of cached items"); 230820a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 230920a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 231020a4e154SJeff Roberson "Number of threads sleeping at limit"); 231120a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 231220a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 231320a4e154SJeff Roberson "Total zone limit sleeps"); 23144bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 23154bd61e19SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bkt_max, 0, 23164bd61e19SJeff Roberson "Maximum number of items in the bucket cache"); 23174bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 23184bd61e19SJeff Roberson "bucket_cnt", CTLFLAG_RD, &zone->uz_bkt_count, 0, 23194bd61e19SJeff Roberson "Number of items in the bucket cache"); 232020a4e154SJeff Roberson 232120a4e154SJeff Roberson /* 23228b987a77SJeff Roberson * Per-domain zone information. 232320a4e154SJeff Roberson */ 232420a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 232520a4e154SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 2326dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0) 23278b987a77SJeff Roberson domains = 1; 232820a4e154SJeff Roberson for (i = 0; i < domains; i++) { 232920a4e154SJeff Roberson zdom = &zone->uz_domain[i]; 233020a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 233120a4e154SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, ""); 233220a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 233320a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 233420a4e154SJeff Roberson "number of items in this domain"); 233520a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 233620a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 233720a4e154SJeff Roberson "maximum item count in this period"); 233820a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 233920a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 234020a4e154SJeff Roberson "minimum item count in this period"); 234120a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 234220a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 234320a4e154SJeff Roberson "Working set size"); 234420a4e154SJeff Roberson } 234520a4e154SJeff Roberson 234620a4e154SJeff Roberson /* 234720a4e154SJeff Roberson * General statistics. 234820a4e154SJeff Roberson */ 234920a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 235020a4e154SJeff Roberson "stats", CTLFLAG_RD, NULL, ""); 235120a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 235220a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 235320a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 235420a4e154SJeff Roberson "Current number of allocated items"); 235520a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 235620a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 235720a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 235820a4e154SJeff Roberson "Total allocation calls"); 235920a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 236020a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 236120a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 236220a4e154SJeff Roberson "Total free calls"); 236320a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 236420a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 236520a4e154SJeff Roberson "Number of allocation failures"); 236620a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 236720a4e154SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 0, 236820a4e154SJeff Roberson "Free calls from the wrong domain"); 236920a4e154SJeff Roberson } 237020a4e154SJeff Roberson 237120a4e154SJeff Roberson struct uma_zone_count { 237220a4e154SJeff Roberson const char *name; 237320a4e154SJeff Roberson int count; 237420a4e154SJeff Roberson }; 237520a4e154SJeff Roberson 237620a4e154SJeff Roberson static void 237720a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 237820a4e154SJeff Roberson { 237920a4e154SJeff Roberson struct uma_zone_count *cnt; 238020a4e154SJeff Roberson 238120a4e154SJeff Roberson cnt = arg; 23823b490537SJeff Roberson /* 23833b490537SJeff Roberson * Some zones are rapidly created with identical names and 23843b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 23853b490537SJeff Roberson * Use one greater than the maximum observed for this name. 23863b490537SJeff Roberson */ 238720a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 23883b490537SJeff Roberson cnt->count = MAX(cnt->count, 23893b490537SJeff Roberson zone->uz_namecnt + 1); 239020a4e154SJeff Roberson } 239120a4e154SJeff Roberson 2392cc7ce83aSJeff Roberson static void 2393cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2394cc7ce83aSJeff Roberson { 2395cc7ce83aSJeff Roberson int i; 2396cc7ce83aSJeff Roberson 2397cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2398cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2399cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2400cc7ce83aSJeff Roberson } 2401cc7ce83aSJeff Roberson } 2402cc7ce83aSJeff Roberson 2403099a0e58SBosko Milekic /* 2404099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2405099a0e58SBosko Milekic * 2406099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2407099a0e58SBosko Milekic * udata Actually uma_zctor_args 24088355f576SJeff Roberson */ 2409b23f72e9SBrian Feldman static int 2410b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 24118355f576SJeff Roberson { 241220a4e154SJeff Roberson struct uma_zone_count cnt; 24138355f576SJeff Roberson struct uma_zctor_args *arg = udata; 24148355f576SJeff Roberson uma_zone_t zone = mem; 2415099a0e58SBosko Milekic uma_zone_t z; 2416099a0e58SBosko Milekic uma_keg_t keg; 241708cfa56eSMark Johnston int i; 24188355f576SJeff Roberson 24198355f576SJeff Roberson bzero(zone, size); 24208355f576SJeff Roberson zone->uz_name = arg->name; 24218355f576SJeff Roberson zone->uz_ctor = arg->ctor; 24228355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2423099a0e58SBosko Milekic zone->uz_init = NULL; 2424099a0e58SBosko Milekic zone->uz_fini = NULL; 2425bf965959SSean Bruno zone->uz_sleeps = 0; 2426c1685086SJeff Roberson zone->uz_xdomain = 0; 242720a4e154SJeff Roberson zone->uz_bucket_size = 0; 242820a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 242920a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2430d4665eaaSJeff Roberson zone->uz_flags = (arg->flags & UMA_ZONE_SMR); 24312f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2432ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 24338d1c459aSRyan Libby zone->uz_domain = 24348d1c459aSRyan Libby (struct uma_zone_domain *)&zone->uz_cpu[mp_maxid + 1]; 2435bb15d1c7SGleb Smirnoff zone->uz_bkt_max = ULONG_MAX; 24362f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2437af526374SJeff Roberson 243820a4e154SJeff Roberson /* Count the number of duplicate names. */ 243920a4e154SJeff Roberson cnt.name = arg->name; 244020a4e154SJeff Roberson cnt.count = 0; 244120a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 244220a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 2443727c6918SJeff Roberson ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 244491d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 24452efcc8cbSGleb Smirnoff 244608cfa56eSMark Johnston for (i = 0; i < vm_ndomains; i++) 2447dc3915c8SJeff Roberson STAILQ_INIT(&zone->uz_domain[i].uzd_buckets); 244808cfa56eSMark Johnston 2449ca293436SRyan Libby #ifdef INVARIANTS 2450ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2451cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 2452ca293436SRyan Libby #endif 2453ca293436SRyan Libby 24540095a784SJeff Roberson /* 24550095a784SJeff Roberson * This is a pure cache zone, no kegs. 24560095a784SJeff Roberson */ 24570095a784SJeff Roberson if (arg->import) { 2458727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2459727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 24606fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2461af526374SJeff Roberson zone->uz_size = arg->size; 24620095a784SJeff Roberson zone->uz_import = arg->import; 24630095a784SJeff Roberson zone->uz_release = arg->release; 24640095a784SJeff Roberson zone->uz_arg = arg->arg; 2465111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 246603175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2467111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2468af526374SJeff Roberson goto out; 24690095a784SJeff Roberson } 24700095a784SJeff Roberson 24710095a784SJeff Roberson /* 24720095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 24730095a784SJeff Roberson */ 2474b75c4efcSAndrew Turner zone->uz_import = zone_import; 2475b75c4efcSAndrew Turner zone->uz_release = zone_release; 24760095a784SJeff Roberson zone->uz_arg = zone; 2477bb15d1c7SGleb Smirnoff keg = arg->keg; 24780095a784SJeff Roberson 2479099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 248020a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 248120a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2482099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 24838355f576SJeff Roberson zone->uz_init = arg->uminit; 2484e221e841SJeff Roberson zone->uz_fini = arg->fini; 2485e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2486111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2487099a0e58SBosko Milekic ZONE_LOCK(zone); 2488099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2489099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2490099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2491099a0e58SBosko Milekic break; 2492099a0e58SBosko Milekic } 2493099a0e58SBosko Milekic } 2494099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2495111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2496e20a199fSJeff Roberson } else if (keg == NULL) { 2497e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2498e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2499b23f72e9SBrian Feldman return (ENOMEM); 2500099a0e58SBosko Milekic } else { 2501099a0e58SBosko Milekic struct uma_kctor_args karg; 2502b23f72e9SBrian Feldman int error; 2503099a0e58SBosko Milekic 2504099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2505099a0e58SBosko Milekic karg.size = arg->size; 2506099a0e58SBosko Milekic karg.uminit = arg->uminit; 2507099a0e58SBosko Milekic karg.fini = arg->fini; 2508099a0e58SBosko Milekic karg.align = arg->align; 2509d4665eaaSJeff Roberson karg.flags = (arg->flags & ~UMA_ZONE_SMR); 2510099a0e58SBosko Milekic karg.zone = zone; 2511b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2512b23f72e9SBrian Feldman flags); 2513b23f72e9SBrian Feldman if (error) 2514b23f72e9SBrian Feldman return (error); 2515099a0e58SBosko Milekic } 25160095a784SJeff Roberson 251720a4e154SJeff Roberson /* Inherit properties from the keg. */ 2518bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2519e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2520e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2521e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 25228355f576SJeff Roberson 252320a4e154SJeff Roberson out: 2524860bb7a0SMark Johnston if (__predict_true(booted >= BOOT_RUNNING)) { 252520a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 252620a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 252720a4e154SJeff Roberson } else { 252820a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 252920a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 253020a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2531099a0e58SBosko Milekic } 25328355f576SJeff Roberson 2533d4665eaaSJeff Roberson /* Caller requests a private SMR context. */ 2534d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 2535d4665eaaSJeff Roberson zone->uz_smr = smr_create(zone->uz_name); 2536d4665eaaSJeff Roberson 25377e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 25387e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 25397e28037aSMark Johnston ("Invalid zone flag combination")); 254020a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 254120a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 254220a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 254320a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 254420a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) 254520a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN; 254620a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 254720a4e154SJeff Roberson zone->uz_bucket_size = 0; 25487e28037aSMark Johnston else 254920a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 255020a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2551cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2552cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2553cc7ce83aSJeff Roberson zone_update_caches(zone); 2554fc03d22bSJeff Roberson 2555b23f72e9SBrian Feldman return (0); 25568355f576SJeff Roberson } 25578355f576SJeff Roberson 25588355f576SJeff Roberson /* 2559099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2560099a0e58SBosko Milekic * table and removes the keg from the global list. 25619c2cd7e5SJeff Roberson * 25629c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 25639c2cd7e5SJeff Roberson * udata unused 25649c2cd7e5SJeff Roberson */ 2565099a0e58SBosko Milekic static void 2566099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2567099a0e58SBosko Milekic { 2568099a0e58SBosko Milekic uma_keg_t keg; 25698b987a77SJeff Roberson uint32_t free, pages; 25708b987a77SJeff Roberson int i; 25719c2cd7e5SJeff Roberson 2572099a0e58SBosko Milekic keg = (uma_keg_t)arg; 25738b987a77SJeff Roberson free = pages = 0; 25748b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 25758b987a77SJeff Roberson free += keg->uk_domain[i].ud_free; 25768b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 25778b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2578099a0e58SBosko Milekic } 25797e240677SRyan Libby if (pages != 0) 25808b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 25818b987a77SJeff Roberson " Lost %u pages of memory.\n", 25828b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 25837e240677SRyan Libby pages / keg->uk_ppera * keg->uk_ipers - free, pages); 2584099a0e58SBosko Milekic 2585099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2586099a0e58SBosko Milekic } 2587099a0e58SBosko Milekic 2588099a0e58SBosko Milekic /* 2589099a0e58SBosko Milekic * Zone header dtor. 2590099a0e58SBosko Milekic * 2591099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2592099a0e58SBosko Milekic * udata unused 2593099a0e58SBosko Milekic */ 25949c2cd7e5SJeff Roberson static void 25959c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 25969c2cd7e5SJeff Roberson { 25979c2cd7e5SJeff Roberson uma_zone_t zone; 2598099a0e58SBosko Milekic uma_keg_t keg; 25999c2cd7e5SJeff Roberson 26009c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 26019643769aSJeff Roberson 260220a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 260320a4e154SJeff Roberson 2604e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 26059643769aSJeff Roberson cache_drain(zone); 2606099a0e58SBosko Milekic 2607111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2608099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2609111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2610099a0e58SBosko Milekic /* 2611099a0e58SBosko Milekic * XXX there are some races here where 2612099a0e58SBosko Milekic * the zone can be drained but zone lock 2613099a0e58SBosko Milekic * released and then refilled before we 2614099a0e58SBosko Milekic * remove it... we dont care for now 2615099a0e58SBosko Milekic */ 261608cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2617e20a199fSJeff Roberson /* 2618323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2619e20a199fSJeff Roberson */ 2620323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2621323ad386STycho Nightingale keg = zone->uz_keg; 2622111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2623099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2624111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 26250095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 26269c2cd7e5SJeff Roberson } 26272efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 26282efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 26292efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 263020a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2631af526374SJeff Roberson ZONE_LOCK_FINI(zone); 263291d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 2633099a0e58SBosko Milekic } 2634099a0e58SBosko Milekic 2635a81c400eSJeff Roberson static void 2636a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg) 2637a81c400eSJeff Roberson { 2638a81c400eSJeff Roberson uma_keg_t keg; 2639a81c400eSJeff Roberson uma_zone_t zone; 2640a81c400eSJeff Roberson 2641a81c400eSJeff Roberson LIST_FOREACH(keg, &uma_kegs, uk_link) { 2642a81c400eSJeff Roberson LIST_FOREACH(zone, &keg->uk_zones, uz_link) 2643a81c400eSJeff Roberson zfunc(zone, arg); 2644a81c400eSJeff Roberson } 2645a81c400eSJeff Roberson LIST_FOREACH(zone, &uma_cachezones, uz_link) 2646a81c400eSJeff Roberson zfunc(zone, arg); 2647a81c400eSJeff Roberson } 2648a81c400eSJeff Roberson 26499c2cd7e5SJeff Roberson /* 26508355f576SJeff Roberson * Traverses every zone in the system and calls a callback 26518355f576SJeff Roberson * 26528355f576SJeff Roberson * Arguments: 26538355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 26548355f576SJeff Roberson * as an argument. 26558355f576SJeff Roberson * 26568355f576SJeff Roberson * Returns: 26578355f576SJeff Roberson * Nothing 26588355f576SJeff Roberson */ 26598355f576SJeff Roberson static void 266020a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 26618355f576SJeff Roberson { 26628355f576SJeff Roberson 2663111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2664a81c400eSJeff Roberson zone_foreach_unlocked(zfunc, arg); 2665111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 26668355f576SJeff Roberson } 26678355f576SJeff Roberson 2668f4bef67cSGleb Smirnoff /* 2669a81c400eSJeff Roberson * Initialize the kernel memory allocator. This is done after pages can be 2670a81c400eSJeff Roberson * allocated but before general KVA is available. 2671f4bef67cSGleb Smirnoff */ 2672a81c400eSJeff Roberson void 2673a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail) 2674f4bef67cSGleb Smirnoff { 2675a81c400eSJeff Roberson struct uma_zctor_args args; 2676a81c400eSJeff Roberson size_t ksize, zsize, size; 2677a81c400eSJeff Roberson uma_keg_t masterkeg; 2678a81c400eSJeff Roberson uintptr_t m; 2679a81c400eSJeff Roberson uint8_t pflag; 2680a81c400eSJeff Roberson 2681a81c400eSJeff Roberson bootstart = bootmem = virtual_avail; 2682a81c400eSJeff Roberson 2683a81c400eSJeff Roberson rw_init(&uma_rwlock, "UMA lock"); 2684a81c400eSJeff Roberson sx_init(&uma_reclaim_lock, "umareclaim"); 2685f4bef67cSGleb Smirnoff 2686f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2687f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 268879c9f942SJeff Roberson ksize = roundup(ksize, UMA_SUPER_ALIGN); 2689f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2690f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2691f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 269279c9f942SJeff Roberson zsize = roundup(zsize, UMA_SUPER_ALIGN); 2693f4bef67cSGleb Smirnoff 2694a81c400eSJeff Roberson /* Allocate the zone of zones, zone of kegs, and zone of zones keg. */ 2695a81c400eSJeff Roberson size = (zsize * 2) + ksize; 2696a81c400eSJeff Roberson m = (uintptr_t)startup_alloc(NULL, size, 0, &pflag, M_NOWAIT | M_ZERO); 2697ab3185d1SJeff Roberson zones = (uma_zone_t)m; 269879c9f942SJeff Roberson m += zsize; 2699ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 270079c9f942SJeff Roberson m += zsize; 2701ab3185d1SJeff Roberson masterkeg = (uma_keg_t)m; 2702ab3185d1SJeff Roberson 2703099a0e58SBosko Milekic /* "manually" create the initial zone */ 27040095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2705099a0e58SBosko Milekic args.name = "UMA Kegs"; 2706ab3185d1SJeff Roberson args.size = ksize; 2707099a0e58SBosko Milekic args.ctor = keg_ctor; 2708099a0e58SBosko Milekic args.dtor = keg_dtor; 27098355f576SJeff Roberson args.uminit = zero_init; 27108355f576SJeff Roberson args.fini = NULL; 2711ab3185d1SJeff Roberson args.keg = masterkeg; 271279c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2713b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2714ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 27158355f576SJeff Roberson 2716099a0e58SBosko Milekic args.name = "UMA Zones"; 2717f4bef67cSGleb Smirnoff args.size = zsize; 2718099a0e58SBosko Milekic args.ctor = zone_ctor; 2719099a0e58SBosko Milekic args.dtor = zone_dtor; 2720099a0e58SBosko Milekic args.uminit = zero_init; 2721099a0e58SBosko Milekic args.fini = NULL; 2722099a0e58SBosko Milekic args.keg = NULL; 272379c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2724099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2725ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2726099a0e58SBosko Milekic 27279b8db4d0SRyan Libby /* Now make zones for slab headers */ 27289b8db4d0SRyan Libby slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE, 27299b8db4d0SRyan Libby NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 27309b8db4d0SRyan Libby slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE, 27311e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 27328355f576SJeff Roberson 27338355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 27348355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 27351e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 27368355f576SJeff Roberson 2737a81c400eSJeff Roberson bucket_init(); 2738d4665eaaSJeff Roberson smr_init(); 27398355f576SJeff Roberson } 27408355f576SJeff Roberson 2741a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2742a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void); 2743f4bef67cSGleb Smirnoff #endif 2744f4bef67cSGleb Smirnoff 2745a81c400eSJeff Roberson /* 2746a81c400eSJeff Roberson * Advertise the availability of normal kva allocations and switch to 2747a81c400eSJeff Roberson * the default back-end allocator. Marks the KVA we consumed on startup 2748a81c400eSJeff Roberson * as used in the map. 2749a81c400eSJeff Roberson */ 27508355f576SJeff Roberson void 275199571dc3SJeff Roberson uma_startup2(void) 27528355f576SJeff Roberson { 2753f4bef67cSGleb Smirnoff 2754530cc6a2SJeff Roberson if (bootstart != bootmem) { 2755a81c400eSJeff Roberson vm_map_lock(kernel_map); 2756a81c400eSJeff Roberson (void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem, 2757a81c400eSJeff Roberson VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 2758a81c400eSJeff Roberson vm_map_unlock(kernel_map); 2759a81c400eSJeff Roberson } 2760a81c400eSJeff Roberson 2761a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2762a81c400eSJeff Roberson /* Set up radix zone to use noobj_alloc. */ 2763a81c400eSJeff Roberson vm_radix_reserve_kva(); 2764f7d35785SGleb Smirnoff #endif 2765a81c400eSJeff Roberson 2766a81c400eSJeff Roberson booted = BOOT_KVA; 2767a81c400eSJeff Roberson zone_foreach_unlocked(zone_kva_available, NULL); 2768f4bef67cSGleb Smirnoff bucket_enable(); 27698355f576SJeff Roberson } 27708355f576SJeff Roberson 2771a81c400eSJeff Roberson /* 2772a81c400eSJeff Roberson * Finish our initialization steps. 2773a81c400eSJeff Roberson */ 27748355f576SJeff Roberson static void 27758355f576SJeff Roberson uma_startup3(void) 27768355f576SJeff Roberson { 27771431a748SGleb Smirnoff 2778c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2779c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2780c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2781c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2782c5deaf04SGleb Smirnoff #endif 2783a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_counters, NULL); 2784a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_sysctl, NULL); 2785fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 27869643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2787c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 2788860bb7a0SMark Johnston 2789860bb7a0SMark Johnston EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL, 2790860bb7a0SMark Johnston EVENTHANDLER_PRI_FIRST); 2791860bb7a0SMark Johnston } 2792860bb7a0SMark Johnston 2793860bb7a0SMark Johnston static void 2794860bb7a0SMark Johnston uma_shutdown(void) 2795860bb7a0SMark Johnston { 2796860bb7a0SMark Johnston 2797860bb7a0SMark Johnston booted = BOOT_SHUTDOWN; 27988355f576SJeff Roberson } 27998355f576SJeff Roberson 2800e20a199fSJeff Roberson static uma_keg_t 2801099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 280285dcf349SGleb Smirnoff int align, uint32_t flags) 2803099a0e58SBosko Milekic { 2804099a0e58SBosko Milekic struct uma_kctor_args args; 2805099a0e58SBosko Milekic 2806099a0e58SBosko Milekic args.size = size; 2807099a0e58SBosko Milekic args.uminit = uminit; 2808099a0e58SBosko Milekic args.fini = fini; 28091e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2810099a0e58SBosko Milekic args.flags = flags; 2811099a0e58SBosko Milekic args.zone = zone; 2812ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 2813099a0e58SBosko Milekic } 2814099a0e58SBosko Milekic 2815f4bef67cSGleb Smirnoff /* Public functions */ 28168355f576SJeff Roberson /* See uma.h */ 28171e319f6dSRobert Watson void 28181e319f6dSRobert Watson uma_set_align(int align) 28191e319f6dSRobert Watson { 28201e319f6dSRobert Watson 28211e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 28221e319f6dSRobert Watson uma_align_cache = align; 28231e319f6dSRobert Watson } 28241e319f6dSRobert Watson 28251e319f6dSRobert Watson /* See uma.h */ 28268355f576SJeff Roberson uma_zone_t 2827bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 282885dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 28298355f576SJeff Roberson 28308355f576SJeff Roberson { 28318355f576SJeff Roberson struct uma_zctor_args args; 283295c4bf75SKonstantin Belousov uma_zone_t res; 28338355f576SJeff Roberson 2834a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 2835a5a35578SJohn Baldwin align, name)); 2836a5a35578SJohn Baldwin 28378355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 28380095a784SJeff Roberson memset(&args, 0, sizeof(args)); 28398355f576SJeff Roberson args.name = name; 28408355f576SJeff Roberson args.size = size; 28418355f576SJeff Roberson args.ctor = ctor; 28428355f576SJeff Roberson args.dtor = dtor; 28438355f576SJeff Roberson args.uminit = uminit; 28448355f576SJeff Roberson args.fini = fini; 2845afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 2846afc6dc36SJohn-Mark Gurney /* 2847ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 2848ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 2849ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 2850ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 2851ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 2852ca293436SRyan Libby * the ctor and dtor do not need to be empty. 2853afc6dc36SJohn-Mark Gurney */ 285454c5ae80SRyan Libby if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH | 285554c5ae80SRyan Libby UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) { 2856afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 2857afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 2858afc6dc36SJohn-Mark Gurney } 2859afc6dc36SJohn-Mark Gurney #endif 28608355f576SJeff Roberson args.align = align; 28618355f576SJeff Roberson args.flags = flags; 2862099a0e58SBosko Milekic args.keg = NULL; 2863099a0e58SBosko Milekic 286408cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 2865ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 286608cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 2867a81c400eSJeff Roberson 286895c4bf75SKonstantin Belousov return (res); 2869099a0e58SBosko Milekic } 2870099a0e58SBosko Milekic 2871099a0e58SBosko Milekic /* See uma.h */ 2872099a0e58SBosko Milekic uma_zone_t 2873099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 2874099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 2875099a0e58SBosko Milekic { 2876099a0e58SBosko Milekic struct uma_zctor_args args; 2877e20a199fSJeff Roberson uma_keg_t keg; 287895c4bf75SKonstantin Belousov uma_zone_t res; 2879099a0e58SBosko Milekic 2880bb15d1c7SGleb Smirnoff keg = master->uz_keg; 28810095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2882099a0e58SBosko Milekic args.name = name; 2883e20a199fSJeff Roberson args.size = keg->uk_size; 2884099a0e58SBosko Milekic args.ctor = ctor; 2885099a0e58SBosko Milekic args.dtor = dtor; 2886099a0e58SBosko Milekic args.uminit = zinit; 2887099a0e58SBosko Milekic args.fini = zfini; 2888e20a199fSJeff Roberson args.align = keg->uk_align; 2889e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 2890e20a199fSJeff Roberson args.keg = keg; 28918355f576SJeff Roberson 289208cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 2893ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 289408cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 2895a81c400eSJeff Roberson 289695c4bf75SKonstantin Belousov return (res); 28978355f576SJeff Roberson } 28988355f576SJeff Roberson 28990095a784SJeff Roberson /* See uma.h */ 29000095a784SJeff Roberson uma_zone_t 2901af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 2902af526374SJeff Roberson uma_init zinit, uma_fini zfini, uma_import zimport, 2903af526374SJeff Roberson uma_release zrelease, void *arg, int flags) 29040095a784SJeff Roberson { 29050095a784SJeff Roberson struct uma_zctor_args args; 29060095a784SJeff Roberson 29070095a784SJeff Roberson memset(&args, 0, sizeof(args)); 29080095a784SJeff Roberson args.name = name; 2909af526374SJeff Roberson args.size = size; 29100095a784SJeff Roberson args.ctor = ctor; 29110095a784SJeff Roberson args.dtor = dtor; 29120095a784SJeff Roberson args.uminit = zinit; 29130095a784SJeff Roberson args.fini = zfini; 29140095a784SJeff Roberson args.import = zimport; 29150095a784SJeff Roberson args.release = zrelease; 29160095a784SJeff Roberson args.arg = arg; 29170095a784SJeff Roberson args.align = 0; 2918bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 29190095a784SJeff Roberson 2920ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 29210095a784SJeff Roberson } 29220095a784SJeff Roberson 29238355f576SJeff Roberson /* See uma.h */ 29249c2cd7e5SJeff Roberson void 29259c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 29269c2cd7e5SJeff Roberson { 2927f4ff923bSRobert Watson 2928860bb7a0SMark Johnston /* 2929860bb7a0SMark Johnston * Large slabs are expensive to reclaim, so don't bother doing 2930860bb7a0SMark Johnston * unnecessary work if we're shutting down. 2931860bb7a0SMark Johnston */ 2932860bb7a0SMark Johnston if (booted == BOOT_SHUTDOWN && 2933860bb7a0SMark Johnston zone->uz_fini == NULL && zone->uz_release == zone_release) 2934860bb7a0SMark Johnston return; 293508cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 29360095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 293708cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 29389c2cd7e5SJeff Roberson } 29399c2cd7e5SJeff Roberson 29408d6fbbb8SJeff Roberson void 29418d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 29428d6fbbb8SJeff Roberson { 29438d6fbbb8SJeff Roberson void *item; 29448d6fbbb8SJeff Roberson 29458d6fbbb8SJeff Roberson item = uma_zalloc_arg(zone, NULL, M_WAITOK); 29468d6fbbb8SJeff Roberson uma_zfree(zone, item); 29478d6fbbb8SJeff Roberson } 29488d6fbbb8SJeff Roberson 29494e180881SMateusz Guzik void * 29504e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 29514e180881SMateusz Guzik { 29524e180881SMateusz Guzik void *item; 2953b4799947SRuslan Bukin #ifdef SMP 29544e180881SMateusz Guzik int i; 29554e180881SMateusz Guzik 29564e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2957b4799947SRuslan Bukin #endif 29584e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 29594e180881SMateusz Guzik if (item != NULL && (flags & M_ZERO)) { 2960b4799947SRuslan Bukin #ifdef SMP 2961013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 29624e180881SMateusz Guzik bzero(zpcpu_get_cpu(item, i), zone->uz_size); 2963b4799947SRuslan Bukin #else 2964b4799947SRuslan Bukin bzero(item, zone->uz_size); 2965b4799947SRuslan Bukin #endif 29664e180881SMateusz Guzik } 29674e180881SMateusz Guzik return (item); 29684e180881SMateusz Guzik } 29694e180881SMateusz Guzik 29704e180881SMateusz Guzik /* 29714e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 29724e180881SMateusz Guzik */ 29734e180881SMateusz Guzik void 29744e180881SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata) 29754e180881SMateusz Guzik { 29764e180881SMateusz Guzik 2977c5b7751fSIan Lepore #ifdef SMP 29784e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2979c5b7751fSIan Lepore #endif 29804e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 29814e180881SMateusz Guzik } 29824e180881SMateusz Guzik 2983d4665eaaSJeff Roberson static inline void * 2984d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags, 2985d4665eaaSJeff Roberson void *item) 2986beb8beefSJeff Roberson { 2987beb8beefSJeff Roberson #ifdef INVARIANTS 2988ca293436SRyan Libby bool skipdbg; 2989beb8beefSJeff Roberson 2990beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 2991ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2992ca293436SRyan Libby zone->uz_ctor != trash_ctor) 2993cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 2994beb8beefSJeff Roberson #endif 2995d4665eaaSJeff Roberson /* Check flags before loading ctor pointer. */ 2996d4665eaaSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) && 2997d4665eaaSJeff Roberson __predict_false(zone->uz_ctor != NULL) && 2998cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 2999beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3000beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 3001beb8beefSJeff Roberson return (NULL); 3002beb8beefSJeff Roberson } 3003beb8beefSJeff Roberson #ifdef INVARIANTS 3004beb8beefSJeff Roberson if (!skipdbg) 3005beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 3006beb8beefSJeff Roberson #endif 3007beb8beefSJeff Roberson if (flags & M_ZERO) 3008cc7ce83aSJeff Roberson bzero(item, size); 3009beb8beefSJeff Roberson 3010beb8beefSJeff Roberson return (item); 3011beb8beefSJeff Roberson } 3012beb8beefSJeff Roberson 3013ca293436SRyan Libby static inline void 3014cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 3015cc7ce83aSJeff Roberson enum zfreeskip skip) 3016ca293436SRyan Libby { 3017ca293436SRyan Libby #ifdef INVARIANTS 3018ca293436SRyan Libby bool skipdbg; 3019ca293436SRyan Libby 3020ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 3021ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 3022ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 3023ca293436SRyan Libby uma_dbg_free(zone, udata, item); 3024ca293436SRyan Libby else 3025ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 3026ca293436SRyan Libby } 3027ca293436SRyan Libby #endif 3028cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 3029ca293436SRyan Libby if (zone->uz_dtor != NULL) 3030cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 3031ca293436SRyan Libby #ifdef INVARIANTS 3032ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 3033ca293436SRyan Libby zone->uz_dtor != trash_dtor) 3034cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 3035ca293436SRyan Libby #endif 3036ca293436SRyan Libby } 3037ca293436SRyan Libby } 3038ca293436SRyan Libby 3039d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS) 3040d4665eaaSJeff Roberson #define UMA_ZALLOC_DEBUG 3041d4665eaaSJeff Roberson static int 3042d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags) 3043d4665eaaSJeff Roberson { 3044d4665eaaSJeff Roberson int error; 3045d4665eaaSJeff Roberson 3046d4665eaaSJeff Roberson error = 0; 3047d4665eaaSJeff Roberson #ifdef WITNESS 3048d4665eaaSJeff Roberson if (flags & M_WAITOK) { 3049d4665eaaSJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3050d4665eaaSJeff Roberson "uma_zalloc_debug: zone \"%s\"", zone->uz_name); 3051d4665eaaSJeff Roberson } 3052d4665eaaSJeff Roberson #endif 3053d4665eaaSJeff Roberson 3054d4665eaaSJeff Roberson #ifdef INVARIANTS 3055d4665eaaSJeff Roberson KASSERT((flags & M_EXEC) == 0, 3056d4665eaaSJeff Roberson ("uma_zalloc_debug: called with M_EXEC")); 3057d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3058d4665eaaSJeff Roberson ("uma_zalloc_debug: called within spinlock or critical section")); 3059d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0, 3060d4665eaaSJeff Roberson ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO")); 3061d4665eaaSJeff Roberson #endif 3062d4665eaaSJeff Roberson 3063d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 30649e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) { 3065d4665eaaSJeff Roberson void *item; 3066d4665eaaSJeff Roberson item = memguard_alloc(zone->uz_size, flags); 3067d4665eaaSJeff Roberson if (item != NULL) { 3068d4665eaaSJeff Roberson error = EJUSTRETURN; 3069d4665eaaSJeff Roberson if (zone->uz_init != NULL && 3070d4665eaaSJeff Roberson zone->uz_init(item, zone->uz_size, flags) != 0) { 3071d4665eaaSJeff Roberson *itemp = NULL; 3072d4665eaaSJeff Roberson return (error); 3073d4665eaaSJeff Roberson } 3074d4665eaaSJeff Roberson if (zone->uz_ctor != NULL && 3075d4665eaaSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 3076d4665eaaSJeff Roberson flags) != 0) { 3077d4665eaaSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3078d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3079d4665eaaSJeff Roberson *itemp = NULL; 3080d4665eaaSJeff Roberson return (error); 3081d4665eaaSJeff Roberson } 3082d4665eaaSJeff Roberson *itemp = item; 3083d4665eaaSJeff Roberson return (error); 3084d4665eaaSJeff Roberson } 3085d4665eaaSJeff Roberson /* This is unfortunate but should not be fatal. */ 3086d4665eaaSJeff Roberson } 3087d4665eaaSJeff Roberson #endif 3088d4665eaaSJeff Roberson return (error); 3089d4665eaaSJeff Roberson } 3090d4665eaaSJeff Roberson 3091d4665eaaSJeff Roberson static int 3092d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata) 3093d4665eaaSJeff Roberson { 3094d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3095d4665eaaSJeff Roberson ("uma_zfree_debug: called with spinlock or critical section held")); 3096d4665eaaSJeff Roberson 3097d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 30989e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) { 3099d4665eaaSJeff Roberson if (zone->uz_dtor != NULL) 3100d4665eaaSJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 3101d4665eaaSJeff Roberson if (zone->uz_fini != NULL) 3102d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3103d4665eaaSJeff Roberson memguard_free(item); 3104d4665eaaSJeff Roberson return (EJUSTRETURN); 3105d4665eaaSJeff Roberson } 3106d4665eaaSJeff Roberson #endif 3107d4665eaaSJeff Roberson return (0); 3108d4665eaaSJeff Roberson } 3109d4665eaaSJeff Roberson #endif 3110d4665eaaSJeff Roberson 3111d4665eaaSJeff Roberson static __noinline void * 3112d4665eaaSJeff Roberson uma_zalloc_single(uma_zone_t zone, void *udata, int flags) 3113d4665eaaSJeff Roberson { 3114d4665eaaSJeff Roberson int domain; 3115d4665eaaSJeff Roberson 3116d4665eaaSJeff Roberson /* 3117d4665eaaSJeff Roberson * We can not get a bucket so try to return a single item. 3118d4665eaaSJeff Roberson */ 3119d4665eaaSJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 3120d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3121d4665eaaSJeff Roberson else 3122d4665eaaSJeff Roberson domain = UMA_ANYDOMAIN; 3123d4665eaaSJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3124d4665eaaSJeff Roberson } 3125d4665eaaSJeff Roberson 3126d4665eaaSJeff Roberson /* See uma.h */ 3127d4665eaaSJeff Roberson void * 3128d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags) 3129d4665eaaSJeff Roberson { 3130d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 3131d4665eaaSJeff Roberson uma_cache_t cache; 3132d4665eaaSJeff Roberson void *item; 3133d4665eaaSJeff Roberson int size, uz_flags; 3134d4665eaaSJeff Roberson 3135d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3136d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 3137d4665eaaSJeff Roberson ("uma_zalloc_arg: called with non-SMR zone.\n")); 3138d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN) 3139d4665eaaSJeff Roberson return (item); 3140d4665eaaSJeff Roberson #endif 3141d4665eaaSJeff Roberson 3142d4665eaaSJeff Roberson critical_enter(); 3143d4665eaaSJeff Roberson do { 3144d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3145d4665eaaSJeff Roberson bucket = &cache->uc_allocbucket; 3146d4665eaaSJeff Roberson size = cache_uz_size(cache); 3147d4665eaaSJeff Roberson uz_flags = cache_uz_flags(cache); 3148d4665eaaSJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 3149d4665eaaSJeff Roberson item = cache_bucket_pop(cache, bucket); 3150d4665eaaSJeff Roberson critical_exit(); 3151d4665eaaSJeff Roberson return (item_ctor(zone, uz_flags, size, NULL, flags, 3152d4665eaaSJeff Roberson item)); 3153d4665eaaSJeff Roberson } 3154d4665eaaSJeff Roberson } while (cache_alloc(zone, cache, NULL, flags)); 3155d4665eaaSJeff Roberson critical_exit(); 3156d4665eaaSJeff Roberson 3157d4665eaaSJeff Roberson return (uma_zalloc_single(zone, NULL, flags)); 3158d4665eaaSJeff Roberson } 3159d4665eaaSJeff Roberson 31609c2cd7e5SJeff Roberson /* See uma.h */ 31618355f576SJeff Roberson void * 31622cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 31638355f576SJeff Roberson { 3164376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3165ab3185d1SJeff Roberson uma_cache_t cache; 3166ab3185d1SJeff Roberson void *item; 3167d4665eaaSJeff Roberson int size, uz_flags; 31688355f576SJeff Roberson 3169e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 317019fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 317110cb2424SMark Murray 31728355f576SJeff Roberson /* This is the fast path allocation */ 3173e63a1c2fSRyan Libby CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name, 3174e63a1c2fSRyan Libby zone, flags); 3175a553d4b8SJeff Roberson 3176d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3177d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 3178d4665eaaSJeff Roberson ("uma_zalloc_arg: called with SMR zone.\n")); 3179d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN) 31808d689e04SGleb Smirnoff return (item); 31818d689e04SGleb Smirnoff #endif 3182d4665eaaSJeff Roberson 31835d1ae027SRobert Watson /* 31845d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 31855d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 31865d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 31875d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 31885d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 31895d1ae027SRobert Watson * preemption and migration. We release the critical section in 31905d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 31915d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 31925d1ae027SRobert Watson * must detect and handle migration if it has occurred. 31935d1ae027SRobert Watson */ 31945d1ae027SRobert Watson critical_enter(); 3195beb8beefSJeff Roberson do { 3196cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3197376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 3198cc7ce83aSJeff Roberson size = cache_uz_size(cache); 3199cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3200376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 3201376b1ba3SJeff Roberson item = cache_bucket_pop(cache, bucket); 32025d1ae027SRobert Watson critical_exit(); 3203d4665eaaSJeff Roberson return (item_ctor(zone, uz_flags, size, udata, flags, 3204d4665eaaSJeff Roberson item)); 3205b23f72e9SBrian Feldman } 3206beb8beefSJeff Roberson } while (cache_alloc(zone, cache, udata, flags)); 3207beb8beefSJeff Roberson critical_exit(); 3208beb8beefSJeff Roberson 3209d4665eaaSJeff Roberson return (uma_zalloc_single(zone, udata, flags)); 3210fc03d22bSJeff Roberson } 3211fc03d22bSJeff Roberson 32128355f576SJeff Roberson /* 3213beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 3214beb8beefSJeff Roberson * a critical section. Returns in a critical section. 3215beb8beefSJeff Roberson * 32164bd61e19SJeff Roberson * A false return value indicates an allocation failure. 32174bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 3218beb8beefSJeff Roberson */ 3219beb8beefSJeff Roberson static __noinline bool 3220beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3221beb8beefSJeff Roberson { 3222beb8beefSJeff Roberson uma_zone_domain_t zdom; 3223beb8beefSJeff Roberson uma_bucket_t bucket; 3224cc7ce83aSJeff Roberson int domain; 3225beb8beefSJeff Roberson bool lockfail; 3226beb8beefSJeff Roberson 3227beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3228beb8beefSJeff Roberson 3229beb8beefSJeff Roberson /* 3230beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3231beb8beefSJeff Roberson * if we can switch with the free bucket. 3232d4665eaaSJeff Roberson * 3233d4665eaaSJeff Roberson * SMR Zones can't re-use the free bucket until the sequence has 3234d4665eaaSJeff Roberson * expired. 32358355f576SJeff Roberson */ 3236d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && 3237d4665eaaSJeff Roberson cache->uc_freebucket.ucb_cnt != 0) { 3238d4665eaaSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 3239d4665eaaSJeff Roberson &cache->uc_allocbucket); 3240beb8beefSJeff Roberson return (true); 32418355f576SJeff Roberson } 3242fc03d22bSJeff Roberson 3243fc03d22bSJeff Roberson /* 3244fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3245fc03d22bSJeff Roberson */ 3246376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3247fc03d22bSJeff Roberson critical_exit(); 3248fc03d22bSJeff Roberson if (bucket != NULL) 32496fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3250fc03d22bSJeff Roberson 32514bd61e19SJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 32524bd61e19SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) { 32534bd61e19SJeff Roberson critical_enter(); 32544bd61e19SJeff Roberson return (false); 32554bd61e19SJeff Roberson } 32564bd61e19SJeff Roberson 32575d1ae027SRobert Watson /* 32585d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 32595d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 32605d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 32615d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 32625d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 32635d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 32645d1ae027SRobert Watson * the critical section. 32655d1ae027SRobert Watson */ 3266fc03d22bSJeff Roberson lockfail = 0; 3267fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 3268fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 3269a553d4b8SJeff Roberson ZONE_LOCK(zone); 3270fc03d22bSJeff Roberson lockfail = 1; 3271fc03d22bSJeff Roberson } 3272beb8beefSJeff Roberson 3273fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 32744bd61e19SJeff Roberson critical_enter(); 32754bd61e19SJeff Roberson cache = &zone->uz_cpu[curcpu]; 3276376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket != NULL) { 3277fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 3278beb8beefSJeff Roberson return (true); 3279a553d4b8SJeff Roberson } 32808355f576SJeff Roberson 3281fc03d22bSJeff Roberson /* 3282fc03d22bSJeff Roberson * Check the zone's cache of buckets. 3283fc03d22bSJeff Roberson */ 3284dfe13344SJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) { 3285c1685086SJeff Roberson domain = PCPU_GET(domain); 3286ab3185d1SJeff Roberson zdom = &zone->uz_domain[domain]; 3287c1685086SJeff Roberson } else { 3288c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3289c1685086SJeff Roberson zdom = &zone->uz_domain[0]; 3290c1685086SJeff Roberson } 3291c1685086SJeff Roberson 329208cfa56eSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom)) != NULL) { 3293cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 3294a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 3295376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3296beb8beefSJeff Roberson return (true); 3297a553d4b8SJeff Roberson } 32985d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 32995d1ae027SRobert Watson critical_exit(); 3300bbee39c6SJeff Roberson 3301fc03d22bSJeff Roberson /* 3302fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 3303fc03d22bSJeff Roberson * handle the working set. 3304fc03d22bSJeff Roberson */ 330520a4e154SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 330620a4e154SJeff Roberson zone->uz_bucket_size++; 33074bd61e19SJeff Roberson ZONE_UNLOCK(zone); 3308bb15d1c7SGleb Smirnoff 33098355f576SJeff Roberson /* 3310beb8beefSJeff Roberson * Fill a bucket and attempt to use it as the alloc bucket. 3311bbee39c6SJeff Roberson */ 3312beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 33131431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 33141431a748SGleb Smirnoff zone->uz_name, zone, bucket); 33154bd61e19SJeff Roberson if (bucket == NULL) { 3316fc03d22bSJeff Roberson critical_enter(); 3317beb8beefSJeff Roberson return (false); 33184bd61e19SJeff Roberson } 33190f9b7bf3SMark Johnston 3320fc03d22bSJeff Roberson /* 3321fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3322fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3323fc03d22bSJeff Roberson * the memory directly. 3324fc03d22bSJeff Roberson */ 33254bd61e19SJeff Roberson ZONE_LOCK(zone); 33264bd61e19SJeff Roberson critical_enter(); 3327cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3328376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 3329dfe13344SJeff Roberson ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0 || 333081c0d72cSGleb Smirnoff domain == PCPU_GET(domain))) { 3331376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 33320f9b7bf3SMark Johnston zdom->uzd_imax += bucket->ub_cnt; 3333bb15d1c7SGleb Smirnoff } else if (zone->uz_bkt_count >= zone->uz_bkt_max) { 333481c0d72cSGleb Smirnoff critical_exit(); 333581c0d72cSGleb Smirnoff ZONE_UNLOCK(zone); 333681c0d72cSGleb Smirnoff bucket_drain(zone, bucket); 333781c0d72cSGleb Smirnoff bucket_free(zone, bucket, udata); 3338beb8beefSJeff Roberson critical_enter(); 3339beb8beefSJeff Roberson return (true); 334081c0d72cSGleb Smirnoff } else 33410f9b7bf3SMark Johnston zone_put_bucket(zone, zdom, bucket, false); 3342bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 3343beb8beefSJeff Roberson return (true); 3344bbee39c6SJeff Roberson } 3345bbee39c6SJeff Roberson 3346ab3185d1SJeff Roberson void * 3347ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3348bbee39c6SJeff Roberson { 3349ab3185d1SJeff Roberson 3350ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 335119fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3352ab3185d1SJeff Roberson 3353ab3185d1SJeff Roberson /* This is the fast path allocation */ 3354e63a1c2fSRyan Libby CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d", 3355e63a1c2fSRyan Libby zone->uz_name, zone, domain, flags); 3356ab3185d1SJeff Roberson 3357ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3358ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3359ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3360ab3185d1SJeff Roberson } 3361ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3362ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 3363ab3185d1SJeff Roberson 3364ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3365ab3185d1SJeff Roberson } 3366ab3185d1SJeff Roberson 3367ab3185d1SJeff Roberson /* 3368ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3369ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3370ab3185d1SJeff Roberson * 3371ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3372ab3185d1SJeff Roberson * only 'domain'. 3373ab3185d1SJeff Roberson */ 3374ab3185d1SJeff Roberson static uma_slab_t 3375194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3376ab3185d1SJeff Roberson { 3377ab3185d1SJeff Roberson uma_domain_t dom; 3378bbee39c6SJeff Roberson uma_slab_t slab; 3379ab3185d1SJeff Roberson int start; 3380ab3185d1SJeff Roberson 3381ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3382ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 33838b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3384ab3185d1SJeff Roberson 3385ab3185d1SJeff Roberson slab = NULL; 3386ab3185d1SJeff Roberson start = domain; 3387ab3185d1SJeff Roberson do { 3388ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 3389ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_part_slab)) 3390ab3185d1SJeff Roberson return (LIST_FIRST(&dom->ud_part_slab)); 3391ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_free_slab)) { 3392ab3185d1SJeff Roberson slab = LIST_FIRST(&dom->ud_free_slab); 3393ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 3394ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3395ab3185d1SJeff Roberson return (slab); 3396ab3185d1SJeff Roberson } 3397ab3185d1SJeff Roberson if (rr) 3398ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3399ab3185d1SJeff Roberson } while (domain != start); 3400ab3185d1SJeff Roberson 3401ab3185d1SJeff Roberson return (NULL); 3402ab3185d1SJeff Roberson } 3403ab3185d1SJeff Roberson 34048b987a77SJeff Roberson /* 34058b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 34068b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 34078b987a77SJeff Roberson */ 3408ab3185d1SJeff Roberson static uma_slab_t 3409194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3410ab3185d1SJeff Roberson { 34118b987a77SJeff Roberson uma_slab_t slab; 3412194a979eSMark Johnston uint32_t reserve; 3413099a0e58SBosko Milekic 34148b987a77SJeff Roberson /* HASH has a single free list. */ 341554c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 34168b987a77SJeff Roberson domain = 0; 3417194a979eSMark Johnston 34188b987a77SJeff Roberson KEG_LOCK(keg, domain); 3419194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 34208b987a77SJeff Roberson if (keg->uk_domain[domain].ud_free <= reserve || 34218b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 34228b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3423194a979eSMark Johnston return (NULL); 34248b987a77SJeff Roberson } 34258b987a77SJeff Roberson return (slab); 3426194a979eSMark Johnston } 3427194a979eSMark Johnston 3428194a979eSMark Johnston static uma_slab_t 3429194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3430194a979eSMark Johnston { 3431194a979eSMark Johnston struct vm_domainset_iter di; 3432194a979eSMark Johnston uma_slab_t slab; 3433194a979eSMark Johnston int aflags, domain; 3434194a979eSMark Johnston bool rr; 3435194a979eSMark Johnston 3436194a979eSMark Johnston restart: 3437bbee39c6SJeff Roberson /* 3438194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3439194a979eSMark Johnston * domain (as happens with first-touch zones). 3440194a979eSMark Johnston * 3441194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3442194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3443194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3444bbee39c6SJeff Roberson */ 3445ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3446ab3185d1SJeff Roberson if (rr) { 3447194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3448194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3449194a979eSMark Johnston &aflags); 3450194a979eSMark Johnston } else { 3451194a979eSMark Johnston aflags = flags; 3452194a979eSMark Johnston domain = rdomain; 3453194a979eSMark Johnston } 3454ab3185d1SJeff Roberson 3455194a979eSMark Johnston for (;;) { 3456194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3457584061b4SJeff Roberson if (slab != NULL) 3458bbee39c6SJeff Roberson return (slab); 3459bbee39c6SJeff Roberson 3460bbee39c6SJeff Roberson /* 3461bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3462bbee39c6SJeff Roberson */ 3463bbee39c6SJeff Roberson if (flags & M_NOVM) 3464bbee39c6SJeff Roberson break; 3465bbee39c6SJeff Roberson 346686220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 34678b987a77SJeff Roberson if (slab != NULL) 3468bbee39c6SJeff Roberson return (slab); 34693639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 34703639ac42SJeff Roberson break; 3471194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3472194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 3473194a979eSMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 3474194a979eSMark Johnston goto restart; 347530c5525bSAndrew Gallatin } 3476194a979eSMark Johnston break; 3477194a979eSMark Johnston } 3478ab3185d1SJeff Roberson } 3479ab3185d1SJeff Roberson 3480bbee39c6SJeff Roberson /* 3481bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3482bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3483bbee39c6SJeff Roberson * fail. 3484bbee39c6SJeff Roberson */ 34858b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 3486bbee39c6SJeff Roberson return (slab); 34878b987a77SJeff Roberson 3488ab3185d1SJeff Roberson return (NULL); 3489ab3185d1SJeff Roberson } 3490bbee39c6SJeff Roberson 3491d56368d7SBosko Milekic static void * 34920095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3493bbee39c6SJeff Roberson { 3494ab3185d1SJeff Roberson uma_domain_t dom; 3495bbee39c6SJeff Roberson void *item; 34969b8db4d0SRyan Libby int freei; 3497bbee39c6SJeff Roberson 34988b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3499099a0e58SBosko Milekic 35008b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 35019b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 35029b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 35031e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3504bbee39c6SJeff Roberson slab->us_freecount--; 35058b987a77SJeff Roberson dom->ud_free--; 3506ef72505eSJeff Roberson 3507bbee39c6SJeff Roberson /* Move this slab to the full list */ 3508bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3509bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3510ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3511bbee39c6SJeff Roberson } 3512bbee39c6SJeff Roberson 3513bbee39c6SJeff Roberson return (item); 3514bbee39c6SJeff Roberson } 3515bbee39c6SJeff Roberson 3516bbee39c6SJeff Roberson static int 3517b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 35180095a784SJeff Roberson { 35198b987a77SJeff Roberson uma_domain_t dom; 3520b75c4efcSAndrew Turner uma_zone_t zone; 35210095a784SJeff Roberson uma_slab_t slab; 35220095a784SJeff Roberson uma_keg_t keg; 3523a03af342SSean Bruno #ifdef NUMA 3524ab3185d1SJeff Roberson int stripe; 3525a03af342SSean Bruno #endif 35260095a784SJeff Roberson int i; 35270095a784SJeff Roberson 3528b75c4efcSAndrew Turner zone = arg; 35290095a784SJeff Roberson slab = NULL; 3530584061b4SJeff Roberson keg = zone->uz_keg; 3531af526374SJeff Roberson /* Try to keep the buckets totally full */ 35320095a784SJeff Roberson for (i = 0; i < max; ) { 3533584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 35340095a784SJeff Roberson break; 3535a03af342SSean Bruno #ifdef NUMA 3536ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3537a03af342SSean Bruno #endif 35388b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 35396fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 35400095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 35418b987a77SJeff Roberson if (dom->ud_free <= keg->uk_reserve) 35426fd34d6fSJeff Roberson break; 3543b6715dabSJeff Roberson #ifdef NUMA 3544ab3185d1SJeff Roberson /* 3545ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3546ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3547ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3548ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3549ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3550ab3185d1SJeff Roberson * time but yields better distribution. 3551ab3185d1SJeff Roberson */ 3552dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 && 3553ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3554ab3185d1SJeff Roberson break; 3555ab3185d1SJeff Roberson #endif 35566fd34d6fSJeff Roberson } 35578b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 3558ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 35590095a784SJeff Roberson flags &= ~M_WAITOK; 35600095a784SJeff Roberson flags |= M_NOWAIT; 35610095a784SJeff Roberson } 35620095a784SJeff Roberson 35630095a784SJeff Roberson return i; 35640095a784SJeff Roberson } 35650095a784SJeff Roberson 35664bd61e19SJeff Roberson static int 35674bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 35684bd61e19SJeff Roberson { 35694bd61e19SJeff Roberson uint64_t old, new, total, max; 35704bd61e19SJeff Roberson 35714bd61e19SJeff Roberson /* 35724bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 35734bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 35744bd61e19SJeff Roberson * fairness by keeping fifo order. 35754bd61e19SJeff Roberson * 35764bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 35774bd61e19SJeff Roberson */ 35784bd61e19SJeff Roberson for (;;) { 35794bd61e19SJeff Roberson zone_free_limit(zone, count); 35804bd61e19SJeff Roberson zone_log_warning(zone); 35814bd61e19SJeff Roberson zone_maxaction(zone); 35824bd61e19SJeff Roberson if (flags & M_NOWAIT) 35834bd61e19SJeff Roberson return (0); 35844bd61e19SJeff Roberson 35854bd61e19SJeff Roberson /* 35864bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 35874bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 35884bd61e19SJeff Roberson * is essentially a home rolled semaphore. 35894bd61e19SJeff Roberson */ 35904bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 35914bd61e19SJeff Roberson old = zone->uz_items; 35924bd61e19SJeff Roberson do { 35934bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 35944bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 35954bd61e19SJeff Roberson max = zone->uz_max_items; 35964bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 35974bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 35984bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 35994bd61e19SJeff Roberson else 36004bd61e19SJeff Roberson new = old + MIN(count, max - old); 36014bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 36024bd61e19SJeff Roberson 36034bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 36044bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 36054bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 36064bd61e19SJeff Roberson return (new - old); 36074bd61e19SJeff Roberson } 36084bd61e19SJeff Roberson 36094bd61e19SJeff Roberson /* 36104bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 36114bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 36124bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 36134bd61e19SJeff Roberson * transitions. 36144bd61e19SJeff Roberson */ 36154bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 36164bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 36174bd61e19SJeff Roberson 36184bd61e19SJeff Roberson /* 36194bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 36204bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 36214bd61e19SJeff Roberson */ 36224bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 36234bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 36244bd61e19SJeff Roberson 36254bd61e19SJeff Roberson /* 36264bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 36274bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 36284bd61e19SJeff Roberson * 36294bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 36304bd61e19SJeff Roberson * our count. 36314bd61e19SJeff Roberson */ 36324bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 36334bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 36344bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 36354bd61e19SJeff Roberson /* We're no longer a sleeper. */ 36364bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 36374bd61e19SJeff Roberson 36384bd61e19SJeff Roberson /* 36394bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 36404bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 36414bd61e19SJeff Roberson * against changes via sysctl. 36424bd61e19SJeff Roberson */ 36434bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 36444bd61e19SJeff Roberson max = zone->uz_max_items; 36454bd61e19SJeff Roberson if (total >= max) 36464bd61e19SJeff Roberson continue; 36474bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 36484bd61e19SJeff Roberson if (total + count > max) { 36494bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 36504bd61e19SJeff Roberson count = max - total; 36514bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 36524bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 36534bd61e19SJeff Roberson 36544bd61e19SJeff Roberson return (count); 36554bd61e19SJeff Roberson } 36564bd61e19SJeff Roberson } 36574bd61e19SJeff Roberson 36584bd61e19SJeff Roberson /* 36594bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 36604bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 36614bd61e19SJeff Roberson * one item can be allocated. 36624bd61e19SJeff Roberson */ 36634bd61e19SJeff Roberson static int 36644bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 36654bd61e19SJeff Roberson { 36664bd61e19SJeff Roberson uint64_t old; 36674bd61e19SJeff Roberson uint64_t max; 36684bd61e19SJeff Roberson 36694bd61e19SJeff Roberson max = zone->uz_max_items; 36704bd61e19SJeff Roberson MPASS(max > 0); 36714bd61e19SJeff Roberson 36724bd61e19SJeff Roberson /* 36734bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 36744bd61e19SJeff Roberson * fetchadd. 36754bd61e19SJeff Roberson */ 36764bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 36774bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 36784bd61e19SJeff Roberson return (count); 36794bd61e19SJeff Roberson 36804bd61e19SJeff Roberson /* 36814bd61e19SJeff Roberson * If we had some items and no sleepers just return the 36824bd61e19SJeff Roberson * truncated value. We have to release the excess space 36834bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 36844bd61e19SJeff Roberson * because we were temporarily over the limit. 36854bd61e19SJeff Roberson */ 36864bd61e19SJeff Roberson if (old < max) { 36874bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 36884bd61e19SJeff Roberson return (max - old); 36894bd61e19SJeff Roberson } 36904bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 36914bd61e19SJeff Roberson } 36924bd61e19SJeff Roberson 36934bd61e19SJeff Roberson /* 36944bd61e19SJeff Roberson * Free a number of items back to the limit. 36954bd61e19SJeff Roberson */ 36964bd61e19SJeff Roberson static void 36974bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 36984bd61e19SJeff Roberson { 36994bd61e19SJeff Roberson uint64_t old; 37004bd61e19SJeff Roberson 37014bd61e19SJeff Roberson MPASS(count > 0); 37024bd61e19SJeff Roberson 37034bd61e19SJeff Roberson /* 37044bd61e19SJeff Roberson * In the common case we either have no sleepers or 37054bd61e19SJeff Roberson * are still over the limit and can just return. 37064bd61e19SJeff Roberson */ 37074bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 37084bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 37094bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 37104bd61e19SJeff Roberson return; 37114bd61e19SJeff Roberson 37124bd61e19SJeff Roberson /* 37134bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 37144bd61e19SJeff Roberson * to generate wakeups if necessary. 37154bd61e19SJeff Roberson */ 37164bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 37174bd61e19SJeff Roberson } 37184bd61e19SJeff Roberson 3719fc03d22bSJeff Roberson static uma_bucket_t 3720beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3721bbee39c6SJeff Roberson { 3722bbee39c6SJeff Roberson uma_bucket_t bucket; 3723beb8beefSJeff Roberson int maxbucket, cnt; 3724bbee39c6SJeff Roberson 3725e63a1c2fSRyan Libby CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name, 3726e63a1c2fSRyan Libby zone, domain); 372730c5525bSAndrew Gallatin 3728c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3729c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3730c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3731c1685086SJeff Roberson 37324bd61e19SJeff Roberson if (zone->uz_max_items > 0) 37334bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 37344bd61e19SJeff Roberson M_NOWAIT); 37354bd61e19SJeff Roberson else 373620a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 37374bd61e19SJeff Roberson if (maxbucket == 0) 37384bd61e19SJeff Roberson return (false); 3739beb8beefSJeff Roberson 37406fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 37416fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3742beb8beefSJeff Roberson if (bucket == NULL) { 3743beb8beefSJeff Roberson cnt = 0; 3744beb8beefSJeff Roberson goto out; 3745beb8beefSJeff Roberson } 37460095a784SJeff Roberson 37470095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3748beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 37490095a784SJeff Roberson 37500095a784SJeff Roberson /* 37510095a784SJeff Roberson * Initialize the memory if necessary. 37520095a784SJeff Roberson */ 37530095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 3754099a0e58SBosko Milekic int i; 3755bbee39c6SJeff Roberson 37560095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 3757e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 37580095a784SJeff Roberson flags) != 0) 3759b23f72e9SBrian Feldman break; 3760b23f72e9SBrian Feldman /* 3761b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 3762b23f72e9SBrian Feldman * rest back onto the freelist. 3763b23f72e9SBrian Feldman */ 3764b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 3765af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 37660095a784SJeff Roberson bucket->ub_cnt - i); 3767a5a262c6SBosko Milekic #ifdef INVARIANTS 37680095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 37690095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 3770a5a262c6SBosko Milekic #endif 3771b23f72e9SBrian Feldman bucket->ub_cnt = i; 3772b23f72e9SBrian Feldman } 3773099a0e58SBosko Milekic } 3774099a0e58SBosko Milekic 3775beb8beefSJeff Roberson cnt = bucket->ub_cnt; 3776f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 37776fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 37782efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 3779beb8beefSJeff Roberson bucket = NULL; 3780beb8beefSJeff Roberson } 3781beb8beefSJeff Roberson out: 37824bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 37834bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 3784fc03d22bSJeff Roberson 3785fc03d22bSJeff Roberson return (bucket); 3786fc03d22bSJeff Roberson } 3787fc03d22bSJeff Roberson 37888355f576SJeff Roberson /* 37890095a784SJeff Roberson * Allocates a single item from a zone. 37908355f576SJeff Roberson * 37918355f576SJeff Roberson * Arguments 37928355f576SJeff Roberson * zone The zone to alloc for. 37938355f576SJeff Roberson * udata The data to be passed to the constructor. 3794ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 3795a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 37968355f576SJeff Roberson * 37978355f576SJeff Roberson * Returns 37988355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 3799bbee39c6SJeff Roberson * An item if successful 38008355f576SJeff Roberson */ 38018355f576SJeff Roberson 38028355f576SJeff Roberson static void * 3803ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 38048355f576SJeff Roberson { 38058355f576SJeff Roberson void *item; 38068355f576SJeff Roberson 38074bd61e19SJeff Roberson if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) 3808bb15d1c7SGleb Smirnoff return (NULL); 38098355f576SJeff Roberson 3810c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3811c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 381230c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 3813c1685086SJeff Roberson 3814ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 3815beb8beefSJeff Roberson goto fail_cnt; 38168355f576SJeff Roberson 3817099a0e58SBosko Milekic /* 3818099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 3819099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 3820099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 3821099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 3822099a0e58SBosko Milekic */ 3823b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 3824e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 3825bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 3826beb8beefSJeff Roberson goto fail_cnt; 3827beb8beefSJeff Roberson } 3828beb8beefSJeff Roberson } 3829d4665eaaSJeff Roberson item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags, 3830d4665eaaSJeff Roberson item); 3831beb8beefSJeff Roberson if (item == NULL) 38320095a784SJeff Roberson goto fail; 38338355f576SJeff Roberson 38342efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 38351431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 38361431a748SGleb Smirnoff zone->uz_name, zone); 38371431a748SGleb Smirnoff 38388355f576SJeff Roberson return (item); 38390095a784SJeff Roberson 3840beb8beefSJeff Roberson fail_cnt: 3841beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 38420095a784SJeff Roberson fail: 38434bd61e19SJeff Roberson if (zone->uz_max_items > 0) 38444bd61e19SJeff Roberson zone_free_limit(zone, 1); 38451431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 38461431a748SGleb Smirnoff zone->uz_name, zone); 38474bd61e19SJeff Roberson 38480095a784SJeff Roberson return (NULL); 38498355f576SJeff Roberson } 38508355f576SJeff Roberson 38518355f576SJeff Roberson /* See uma.h */ 38528355f576SJeff Roberson void 3853d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item) 3854d4665eaaSJeff Roberson { 3855d4665eaaSJeff Roberson uma_cache_t cache; 3856d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 3857d4665eaaSJeff Roberson int domain, itemdomain, uz_flags; 3858d4665eaaSJeff Roberson 3859d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3860d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 3861d4665eaaSJeff Roberson ("uma_zfree_smr: called with non-SMR zone.\n")); 3862d4665eaaSJeff Roberson KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer.")); 3863d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN) 3864d4665eaaSJeff Roberson return; 3865d4665eaaSJeff Roberson #endif 3866d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3867d4665eaaSJeff Roberson uz_flags = cache_uz_flags(cache); 3868d4665eaaSJeff Roberson domain = itemdomain = 0; 3869d4665eaaSJeff Roberson #ifdef NUMA 3870d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 3871d4665eaaSJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 3872d4665eaaSJeff Roberson #endif 3873d4665eaaSJeff Roberson critical_enter(); 3874d4665eaaSJeff Roberson do { 3875d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3876d4665eaaSJeff Roberson /* SMR Zones must free to the free bucket. */ 3877d4665eaaSJeff Roberson bucket = &cache->uc_freebucket; 3878d4665eaaSJeff Roberson #ifdef NUMA 3879d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3880d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 3881d4665eaaSJeff Roberson domain != itemdomain) { 3882d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 3883d4665eaaSJeff Roberson } 3884d4665eaaSJeff Roberson #endif 3885d4665eaaSJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3886d4665eaaSJeff Roberson cache_bucket_push(cache, bucket, item); 3887d4665eaaSJeff Roberson critical_exit(); 3888d4665eaaSJeff Roberson return; 3889d4665eaaSJeff Roberson } 3890d4665eaaSJeff Roberson } while (cache_free(zone, cache, NULL, item, itemdomain)); 3891d4665eaaSJeff Roberson critical_exit(); 3892d4665eaaSJeff Roberson 3893d4665eaaSJeff Roberson /* 3894d4665eaaSJeff Roberson * If nothing else caught this, we'll just do an internal free. 3895d4665eaaSJeff Roberson */ 3896d4665eaaSJeff Roberson zone_free_item(zone, item, NULL, SKIP_NONE); 3897d4665eaaSJeff Roberson } 3898d4665eaaSJeff Roberson 3899d4665eaaSJeff Roberson /* See uma.h */ 3900d4665eaaSJeff Roberson void 39018355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 39028355f576SJeff Roberson { 39038355f576SJeff Roberson uma_cache_t cache; 3904376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3905cc7ce83aSJeff Roberson int domain, itemdomain, uz_flags; 39068355f576SJeff Roberson 3907e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 390819fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 390910cb2424SMark Murray 3910e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone); 39113659f747SRobert Watson 3912d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3913d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 3914d4665eaaSJeff Roberson ("uma_zfree_arg: called with SMR zone.\n")); 3915d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN) 3916d4665eaaSJeff Roberson return; 3917d4665eaaSJeff Roberson #endif 391820ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 391920ed0cb0SMatthew D Fleming if (item == NULL) 392020ed0cb0SMatthew D Fleming return; 3921cc7ce83aSJeff Roberson 3922cc7ce83aSJeff Roberson /* 3923cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 3924cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 3925cc7ce83aSJeff Roberson * will simply read another cpu's line. 3926cc7ce83aSJeff Roberson */ 3927cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3928cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3929d4665eaaSJeff Roberson if (UMA_ALWAYS_CTORDTOR || 3930d4665eaaSJeff Roberson __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0)) 3931cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 3932ef72505eSJeff Roberson 3933af7f9b97SJeff Roberson /* 3934af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 3935af7f9b97SJeff Roberson * a little longer for the limits to be reset. 3936af7f9b97SJeff Roberson */ 3937cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 3938bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0) 3939fc03d22bSJeff Roberson goto zfree_item; 3940cc7ce83aSJeff Roberson } 3941af7f9b97SJeff Roberson 39425d1ae027SRobert Watson /* 39435d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 39445d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 39455d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 39465d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 39475d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 39485d1ae027SRobert Watson * preemption and migration. We release the critical section in 39495d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 39505d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 39515d1ae027SRobert Watson * detect and handle migration if it has occurred. 39525d1ae027SRobert Watson */ 39530a81b439SJeff Roberson domain = itemdomain = 0; 3954dfe13344SJeff Roberson #ifdef NUMA 3955dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 3956dfe13344SJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 3957dfe13344SJeff Roberson #endif 39585d1ae027SRobert Watson critical_enter(); 39590a81b439SJeff Roberson do { 3960cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3961a553d4b8SJeff Roberson /* 3962dfe13344SJeff Roberson * Try to free into the allocbucket first to give LIFO 3963dfe13344SJeff Roberson * ordering for cache-hot datastructures. Spill over 3964dfe13344SJeff Roberson * into the freebucket if necessary. Alloc will swap 3965dfe13344SJeff Roberson * them if one runs dry. 3966a553d4b8SJeff Roberson */ 3967dfe13344SJeff Roberson bucket = &cache->uc_allocbucket; 3968d4665eaaSJeff Roberson #ifdef NUMA 3969d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3970d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 3971d4665eaaSJeff Roberson domain != itemdomain) { 3972d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 3973d4665eaaSJeff Roberson } else 3974d4665eaaSJeff Roberson #endif 3975d4665eaaSJeff Roberson if (bucket->ucb_cnt >= bucket->ucb_entries) 3976376b1ba3SJeff Roberson bucket = &cache->uc_freebucket; 3977376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3978376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 39795d1ae027SRobert Watson critical_exit(); 39808355f576SJeff Roberson return; 3981fc03d22bSJeff Roberson } 39820a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 39830a81b439SJeff Roberson critical_exit(); 3984fc03d22bSJeff Roberson 39858355f576SJeff Roberson /* 39860a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 39878355f576SJeff Roberson */ 39880a81b439SJeff Roberson zfree_item: 39890a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 39900a81b439SJeff Roberson } 3991fc03d22bSJeff Roberson 3992dfe13344SJeff Roberson #ifdef NUMA 399391d947bfSJeff Roberson /* 399491d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 399591d947bfSJeff Roberson * them. 399691d947bfSJeff Roberson */ 399791d947bfSJeff Roberson static void 399891d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 399991d947bfSJeff Roberson { 400091d947bfSJeff Roberson struct uma_bucketlist fullbuckets; 400191d947bfSJeff Roberson uma_zone_domain_t zdom; 400291d947bfSJeff Roberson uma_bucket_t b; 400391d947bfSJeff Roberson void *item; 400491d947bfSJeff Roberson int domain; 400591d947bfSJeff Roberson 400691d947bfSJeff Roberson CTR3(KTR_UMA, 400791d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 400891d947bfSJeff Roberson zone->uz_name, zone, bucket); 400991d947bfSJeff Roberson 4010dc3915c8SJeff Roberson STAILQ_INIT(&fullbuckets); 401191d947bfSJeff Roberson 401291d947bfSJeff Roberson /* 401391d947bfSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 401491d947bfSJeff Roberson * lock on the current crossfree bucket. A full matrix with 401591d947bfSJeff Roberson * per-domain locking could be used if necessary. 401691d947bfSJeff Roberson */ 401791d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 401891d947bfSJeff Roberson while (bucket->ub_cnt > 0) { 401991d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 402091d947bfSJeff Roberson domain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 402191d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 402291d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 402391d947bfSJeff Roberson zdom->uzd_cross = bucket_alloc(zone, udata, M_NOWAIT); 402491d947bfSJeff Roberson if (zdom->uzd_cross == NULL) 402591d947bfSJeff Roberson break; 402691d947bfSJeff Roberson } 402791d947bfSJeff Roberson zdom->uzd_cross->ub_bucket[zdom->uzd_cross->ub_cnt++] = item; 402891d947bfSJeff Roberson if (zdom->uzd_cross->ub_cnt == zdom->uzd_cross->ub_entries) { 4029dc3915c8SJeff Roberson STAILQ_INSERT_HEAD(&fullbuckets, zdom->uzd_cross, 403091d947bfSJeff Roberson ub_link); 403191d947bfSJeff Roberson zdom->uzd_cross = NULL; 403291d947bfSJeff Roberson } 403391d947bfSJeff Roberson bucket->ub_cnt--; 403491d947bfSJeff Roberson } 403591d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 4036dc3915c8SJeff Roberson if (!STAILQ_EMPTY(&fullbuckets)) { 403791d947bfSJeff Roberson ZONE_LOCK(zone); 4038dc3915c8SJeff Roberson while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) { 4039d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 4040d4665eaaSJeff Roberson bucket->ub_seq = smr_current(zone->uz_smr); 4041dc3915c8SJeff Roberson STAILQ_REMOVE_HEAD(&fullbuckets, ub_link); 404291d947bfSJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 404391d947bfSJeff Roberson ZONE_UNLOCK(zone); 404491d947bfSJeff Roberson bucket_drain(zone, b); 404591d947bfSJeff Roberson bucket_free(zone, b, udata); 404691d947bfSJeff Roberson ZONE_LOCK(zone); 404791d947bfSJeff Roberson } else { 404891d947bfSJeff Roberson domain = _vm_phys_domain( 404991d947bfSJeff Roberson pmap_kextract( 405091d947bfSJeff Roberson (vm_offset_t)b->ub_bucket[0])); 405191d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 405291d947bfSJeff Roberson zone_put_bucket(zone, zdom, b, true); 405391d947bfSJeff Roberson } 405491d947bfSJeff Roberson } 405591d947bfSJeff Roberson ZONE_UNLOCK(zone); 405691d947bfSJeff Roberson } 405791d947bfSJeff Roberson if (bucket->ub_cnt != 0) 405891d947bfSJeff Roberson bucket_drain(zone, bucket); 4059d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 406091d947bfSJeff Roberson bucket_free(zone, bucket, udata); 406191d947bfSJeff Roberson } 406291d947bfSJeff Roberson #endif 406391d947bfSJeff Roberson 40640a81b439SJeff Roberson static void 40650a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 40660a81b439SJeff Roberson int domain, int itemdomain) 40670a81b439SJeff Roberson { 40680a81b439SJeff Roberson uma_zone_domain_t zdom; 40690a81b439SJeff Roberson 4070dfe13344SJeff Roberson #ifdef NUMA 40710a81b439SJeff Roberson /* 40720a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 40730a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 40740a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 407591d947bfSJeff Roberson * correct domains. 40760a81b439SJeff Roberson */ 40770a81b439SJeff Roberson if (domain != itemdomain && vm_ndomains > 2) { 407891d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 40790a81b439SJeff Roberson return; 40800a81b439SJeff Roberson } 40810a81b439SJeff Roberson #endif 408291d947bfSJeff Roberson 40830a81b439SJeff Roberson /* 40840a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 40850a81b439SJeff Roberson * 40860a81b439SJeff Roberson * We bump the uz count when the cache size is insufficient to 40870a81b439SJeff Roberson * handle the working set. 40880a81b439SJeff Roberson */ 40894d104ba0SAlexander Motin if (ZONE_TRYLOCK(zone) == 0) { 40904d104ba0SAlexander Motin /* Record contention to size the buckets. */ 40918355f576SJeff Roberson ZONE_LOCK(zone); 409220a4e154SJeff Roberson if (zone->uz_bucket_size < zone->uz_bucket_size_max) 409320a4e154SJeff Roberson zone->uz_bucket_size++; 40944d104ba0SAlexander Motin } 40958355f576SJeff Roberson 40960a81b439SJeff Roberson CTR3(KTR_UMA, 40970a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 40980a81b439SJeff Roberson zone->uz_name, zone, bucket); 40990a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 41000a81b439SJeff Roberson KASSERT(bucket->ub_cnt == bucket->ub_entries, 41010a81b439SJeff Roberson ("uma_zfree: Attempting to insert partial bucket onto the full list.\n")); 41020a81b439SJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 4103c1685086SJeff Roberson ZONE_UNLOCK(zone); 4104c1685086SJeff Roberson bucket_drain(zone, bucket); 4105c1685086SJeff Roberson bucket_free(zone, bucket, udata); 4106c1685086SJeff Roberson } else { 4107c1685086SJeff Roberson zdom = &zone->uz_domain[itemdomain]; 4108c1685086SJeff Roberson zone_put_bucket(zone, zdom, bucket, true); 4109c1685086SJeff Roberson ZONE_UNLOCK(zone); 4110c1685086SJeff Roberson } 41118355f576SJeff Roberson } 4112fc03d22bSJeff Roberson 41134d104ba0SAlexander Motin /* 41140a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 41150a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 41160a81b439SJeff Roberson * 41170a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 41180a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 41190a81b439SJeff Roberson * the caller should retry. 41204d104ba0SAlexander Motin */ 41210a81b439SJeff Roberson static __noinline bool 41220a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 41230a81b439SJeff Roberson int itemdomain) 41240a81b439SJeff Roberson { 4125dfe13344SJeff Roberson uma_cache_bucket_t cbucket; 4126d4665eaaSJeff Roberson uma_bucket_t newbucket, bucket; 4127cc7ce83aSJeff Roberson int domain; 41280a81b439SJeff Roberson 41290a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 41300a81b439SJeff Roberson 4131d4665eaaSJeff Roberson if (zone->uz_bucket_size == 0) 41320a81b439SJeff Roberson return false; 41330a81b439SJeff Roberson 4134cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4135d4665eaaSJeff Roberson newbucket = NULL; 41360a81b439SJeff Roberson 41370a81b439SJeff Roberson /* 4138dfe13344SJeff Roberson * FIRSTTOUCH domains need to free to the correct zdom. When 4139dfe13344SJeff Roberson * enabled this is the zdom of the item. The bucket is the 4140dfe13344SJeff Roberson * cross bucket if the current domain and itemdomain do not match. 41410a81b439SJeff Roberson */ 4142dfe13344SJeff Roberson cbucket = &cache->uc_freebucket; 4143dfe13344SJeff Roberson #ifdef NUMA 4144dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 41450a81b439SJeff Roberson domain = PCPU_GET(domain); 41460a81b439SJeff Roberson if (domain != itemdomain) { 4147dfe13344SJeff Roberson cbucket = &cache->uc_crossbucket; 4148dfe13344SJeff Roberson if (cbucket->ucb_cnt != 0) 4149dfe13344SJeff Roberson atomic_add_64(&zone->uz_xdomain, 4150dfe13344SJeff Roberson cbucket->ucb_cnt); 4151dfe13344SJeff Roberson } 41520a81b439SJeff Roberson } else 41530a81b439SJeff Roberson #endif 4154dfe13344SJeff Roberson itemdomain = domain = 0; 4155dfe13344SJeff Roberson bucket = cache_bucket_unload(cbucket); 41560a81b439SJeff Roberson 41570a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 41580a81b439SJeff Roberson critical_exit(); 41590a81b439SJeff Roberson 4160d4665eaaSJeff Roberson /* 4161d4665eaaSJeff Roberson * Don't let SMR zones operate without a free bucket. Force 4162d4665eaaSJeff Roberson * a synchronize and re-use this one. We will only degrade 4163d4665eaaSJeff Roberson * to a synchronize every bucket_size items rather than every 4164d4665eaaSJeff Roberson * item if we fail to allocate a bucket. 4165d4665eaaSJeff Roberson */ 4166d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) { 4167d4665eaaSJeff Roberson if (bucket != NULL) 4168d4665eaaSJeff Roberson bucket->ub_seq = smr_advance(zone->uz_smr); 4169d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4170d4665eaaSJeff Roberson if (newbucket == NULL && bucket != NULL) { 4171d4665eaaSJeff Roberson bucket_drain(zone, bucket); 4172d4665eaaSJeff Roberson newbucket = bucket; 4173d4665eaaSJeff Roberson bucket = NULL; 4174d4665eaaSJeff Roberson } 4175d4665eaaSJeff Roberson } else if (!bucketdisable) 4176d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4177d4665eaaSJeff Roberson 41780a81b439SJeff Roberson if (bucket != NULL) 41790a81b439SJeff Roberson zone_free_bucket(zone, bucket, udata, domain, itemdomain); 4180a553d4b8SJeff Roberson 4181fc03d22bSJeff Roberson critical_enter(); 4182d4665eaaSJeff Roberson if ((bucket = newbucket) == NULL) 41830a81b439SJeff Roberson return (false); 4184cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4185dfe13344SJeff Roberson #ifdef NUMA 4186fc03d22bSJeff Roberson /* 41870a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 41880a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 41890a81b439SJeff Roberson * the free bucket. 4190fc03d22bSJeff Roberson */ 4191dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 41920a81b439SJeff Roberson domain = PCPU_GET(domain); 4193376b1ba3SJeff Roberson if (domain != itemdomain && 4194376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 4195376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 41960a81b439SJeff Roberson return (true); 41970a81b439SJeff Roberson } 41980a81b439SJeff Roberson } 41990a81b439SJeff Roberson #endif 42000a81b439SJeff Roberson /* 42010a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 42020a81b439SJeff Roberson */ 4203376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 4204fc03d22bSJeff Roberson critical_exit(); 42056fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 42060a81b439SJeff Roberson critical_enter(); 42070a81b439SJeff Roberson } else 4208376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 42098355f576SJeff Roberson 42100a81b439SJeff Roberson return (true); 42118355f576SJeff Roberson } 42128355f576SJeff Roberson 4213ab3185d1SJeff Roberson void 4214ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata) 4215ab3185d1SJeff Roberson { 4216ab3185d1SJeff Roberson 4217ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 421819fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 4219ab3185d1SJeff Roberson 4220e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_domain zone %s(%p)", zone->uz_name, zone); 4221ab3185d1SJeff Roberson 4222ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 4223ab3185d1SJeff Roberson ("uma_zfree_domain: called with spinlock or critical section held")); 4224ab3185d1SJeff Roberson 4225ab3185d1SJeff Roberson /* uma_zfree(..., NULL) does nothing, to match free(9). */ 4226ab3185d1SJeff Roberson if (item == NULL) 4227ab3185d1SJeff Roberson return; 4228ab3185d1SJeff Roberson zone_free_item(zone, item, udata, SKIP_NONE); 4229ab3185d1SJeff Roberson } 4230ab3185d1SJeff Roberson 42318355f576SJeff Roberson static void 4232bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 42338355f576SJeff Roberson { 4234bb15d1c7SGleb Smirnoff uma_keg_t keg; 4235ab3185d1SJeff Roberson uma_domain_t dom; 42369b8db4d0SRyan Libby int freei; 4237099a0e58SBosko Milekic 4238bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 42398b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 4240ab3185d1SJeff Roberson 42418355f576SJeff Roberson /* Do we need to remove from any lists? */ 42428b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 4243099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 42448355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4245ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 42468355f576SJeff Roberson } else if (slab->us_freecount == 0) { 42478355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4248ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 42498355f576SJeff Roberson } 42508355f576SJeff Roberson 4251ef72505eSJeff Roberson /* Slab management. */ 42521e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 42539b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 42548355f576SJeff Roberson slab->us_freecount++; 42558355f576SJeff Roberson 4256ef72505eSJeff Roberson /* Keg statistics. */ 42578b987a77SJeff Roberson dom->ud_free++; 42580095a784SJeff Roberson } 42590095a784SJeff Roberson 42600095a784SJeff Roberson static void 4261b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 42620095a784SJeff Roberson { 42638b987a77SJeff Roberson struct mtx *lock; 4264b75c4efcSAndrew Turner uma_zone_t zone; 42650095a784SJeff Roberson uma_slab_t slab; 42660095a784SJeff Roberson uma_keg_t keg; 42670095a784SJeff Roberson uint8_t *mem; 42688b987a77SJeff Roberson void *item; 42690095a784SJeff Roberson int i; 42708355f576SJeff Roberson 4271b75c4efcSAndrew Turner zone = arg; 4272bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 42738b987a77SJeff Roberson lock = NULL; 427454c5ae80SRyan Libby if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0)) 42758b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 42760095a784SJeff Roberson for (i = 0; i < cnt; i++) { 42770095a784SJeff Roberson item = bucket[i]; 427854c5ae80SRyan Libby if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) { 42790095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 42808b987a77SJeff Roberson } else { 42818b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 428254c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0) 42838b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 42848b987a77SJeff Roberson else 42858b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 42868b987a77SJeff Roberson } 42878b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 42888b987a77SJeff Roberson if (lock != NULL) 42898b987a77SJeff Roberson mtx_unlock(lock); 42908b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 42918b987a77SJeff Roberson } 4292bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 42930095a784SJeff Roberson } 42948b987a77SJeff Roberson if (lock != NULL) 42958b987a77SJeff Roberson mtx_unlock(lock); 42968355f576SJeff Roberson } 42978355f576SJeff Roberson 42980095a784SJeff Roberson /* 42990095a784SJeff Roberson * Frees a single item to any zone. 43000095a784SJeff Roberson * 43010095a784SJeff Roberson * Arguments: 43020095a784SJeff Roberson * zone The zone to free to 43030095a784SJeff Roberson * item The item we're freeing 43040095a784SJeff Roberson * udata User supplied data for the dtor 43050095a784SJeff Roberson * skip Skip dtors and finis 43060095a784SJeff Roberson */ 43070095a784SJeff Roberson static void 43080095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 43090095a784SJeff Roberson { 4310c5deaf04SGleb Smirnoff 4311d4665eaaSJeff Roberson /* 4312d4665eaaSJeff Roberson * If a free is sent directly to an SMR zone we have to 4313d4665eaaSJeff Roberson * synchronize immediately because the item can instantly 4314d4665eaaSJeff Roberson * be reallocated. This should only happen in degenerate 4315d4665eaaSJeff Roberson * cases when no memory is available for per-cpu caches. 4316d4665eaaSJeff Roberson */ 4317d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE) 4318d4665eaaSJeff Roberson smr_synchronize(zone->uz_smr); 4319d4665eaaSJeff Roberson 4320cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 43210095a784SJeff Roberson 43220095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 43230095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 43240095a784SJeff Roberson 43250095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4326bb15d1c7SGleb Smirnoff 4327bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4328bb15d1c7SGleb Smirnoff return; 4329bb15d1c7SGleb Smirnoff 43302efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 43312efcc8cbSGleb Smirnoff 43324bd61e19SJeff Roberson if (zone->uz_max_items > 0) 43334bd61e19SJeff Roberson zone_free_limit(zone, 1); 4334bb45b411SGleb Smirnoff } 43350095a784SJeff Roberson 43368355f576SJeff Roberson /* See uma.h */ 43371c6cae97SLawrence Stewart int 4338736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4339736ee590SJeff Roberson { 4340bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 4341003cf08bSMark Johnston int count; 4342bb15d1c7SGleb Smirnoff 43434bd61e19SJeff Roberson /* 43444bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 43454bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 43464bd61e19SJeff Roberson * way to clear a limit. 43474bd61e19SJeff Roberson */ 4348bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4349003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4350003cf08bSMark Johnston count = ubz != NULL ? ubz->ubz_entries : 0; 435120a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = count; 435220a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 435320a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4354bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4355cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4356cc7ce83aSJeff Roberson zone_update_caches(zone); 43574bd61e19SJeff Roberson /* We may need to wake waiters. */ 43584bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4359bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4360bb15d1c7SGleb Smirnoff 4361bb15d1c7SGleb Smirnoff return (nitems); 4362bb15d1c7SGleb Smirnoff } 4363bb15d1c7SGleb Smirnoff 4364bb15d1c7SGleb Smirnoff /* See uma.h */ 4365003cf08bSMark Johnston void 4366bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4367bb15d1c7SGleb Smirnoff { 4368003cf08bSMark Johnston struct uma_bucket_zone *ubz; 4369003cf08bSMark Johnston int bpcpu; 4370bb15d1c7SGleb Smirnoff 4371bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4372003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4373003cf08bSMark Johnston if (ubz != NULL) { 4374003cf08bSMark Johnston bpcpu = 2; 4375dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 4376003cf08bSMark Johnston /* Count the cross-domain bucket. */ 4377003cf08bSMark Johnston bpcpu++; 4378003cf08bSMark Johnston nitems -= ubz->ubz_entries * bpcpu * mp_ncpus; 437920a4e154SJeff Roberson zone->uz_bucket_size_max = ubz->ubz_entries; 4380003cf08bSMark Johnston } else { 438120a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 4382003cf08bSMark Johnston } 438320a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 438420a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4385bb15d1c7SGleb Smirnoff zone->uz_bkt_max = nitems; 4386bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4387736ee590SJeff Roberson } 4388736ee590SJeff Roberson 4389736ee590SJeff Roberson /* See uma.h */ 4390e49471b0SAndre Oppermann int 4391e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4392e49471b0SAndre Oppermann { 4393e49471b0SAndre Oppermann int nitems; 4394e49471b0SAndre Oppermann 4395727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4396e49471b0SAndre Oppermann 4397e49471b0SAndre Oppermann return (nitems); 4398e49471b0SAndre Oppermann } 4399e49471b0SAndre Oppermann 4400e49471b0SAndre Oppermann /* See uma.h */ 44012f891cd5SPawel Jakub Dawidek void 44022f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 44032f891cd5SPawel Jakub Dawidek { 44042f891cd5SPawel Jakub Dawidek 4405727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 44062f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 44072f891cd5SPawel Jakub Dawidek } 44082f891cd5SPawel Jakub Dawidek 44092f891cd5SPawel Jakub Dawidek /* See uma.h */ 441054503a13SJonathan T. Looney void 441154503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 441254503a13SJonathan T. Looney { 441354503a13SJonathan T. Looney 4414727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4415e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 441654503a13SJonathan T. Looney } 441754503a13SJonathan T. Looney 441854503a13SJonathan T. Looney /* See uma.h */ 4419c4ae7908SLawrence Stewart int 4420c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4421c4ae7908SLawrence Stewart { 4422c4ae7908SLawrence Stewart int64_t nitems; 4423c4ae7908SLawrence Stewart u_int i; 4424c4ae7908SLawrence Stewart 4425bfb6b7a1SJeff Roberson nitems = 0; 4426bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER) 44272efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 44282efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4429727c6918SJeff Roberson CPU_FOREACH(i) 4430727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4431727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4432c4ae7908SLawrence Stewart 4433c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4434c4ae7908SLawrence Stewart } 4435c4ae7908SLawrence Stewart 443620a4e154SJeff Roberson static uint64_t 443720a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 443820a4e154SJeff Roberson { 443920a4e154SJeff Roberson uint64_t nitems; 444020a4e154SJeff Roberson u_int i; 444120a4e154SJeff Roberson 4442bfb6b7a1SJeff Roberson nitems = 0; 4443bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER) 444420a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4445727c6918SJeff Roberson CPU_FOREACH(i) 4446727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 444720a4e154SJeff Roberson 444820a4e154SJeff Roberson return (nitems); 444920a4e154SJeff Roberson } 445020a4e154SJeff Roberson 445120a4e154SJeff Roberson static uint64_t 445220a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 445320a4e154SJeff Roberson { 445420a4e154SJeff Roberson uint64_t nitems; 445520a4e154SJeff Roberson u_int i; 445620a4e154SJeff Roberson 4457bfb6b7a1SJeff Roberson nitems = 0; 4458bfb6b7a1SJeff Roberson if (zone->uz_frees != EARLY_COUNTER) 445920a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4460727c6918SJeff Roberson CPU_FOREACH(i) 4461727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 446220a4e154SJeff Roberson 446320a4e154SJeff Roberson return (nitems); 446420a4e154SJeff Roberson } 446520a4e154SJeff Roberson 446631c251a0SJeff Roberson #ifdef INVARIANTS 446731c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */ 446831c251a0SJeff Roberson static uint64_t 446931c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg) 447031c251a0SJeff Roberson { 447131c251a0SJeff Roberson uma_zone_t z; 447231c251a0SJeff Roberson uint64_t nitems; 447331c251a0SJeff Roberson 447431c251a0SJeff Roberson nitems = 0; 447531c251a0SJeff Roberson LIST_FOREACH(z, &keg->uk_zones, uz_link) 447631c251a0SJeff Roberson nitems += uma_zone_get_allocs(z); 447731c251a0SJeff Roberson 447831c251a0SJeff Roberson return (nitems); 447931c251a0SJeff Roberson } 448031c251a0SJeff Roberson #endif 448131c251a0SJeff Roberson 4482c4ae7908SLawrence Stewart /* See uma.h */ 4483736ee590SJeff Roberson void 4484099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4485099a0e58SBosko Milekic { 4486e20a199fSJeff Roberson uma_keg_t keg; 4487e20a199fSJeff Roberson 4488bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4489727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4490e20a199fSJeff Roberson keg->uk_init = uminit; 4491099a0e58SBosko Milekic } 4492099a0e58SBosko Milekic 4493099a0e58SBosko Milekic /* See uma.h */ 4494099a0e58SBosko Milekic void 4495099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4496099a0e58SBosko Milekic { 4497e20a199fSJeff Roberson uma_keg_t keg; 4498e20a199fSJeff Roberson 4499bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4500727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4501e20a199fSJeff Roberson keg->uk_fini = fini; 4502099a0e58SBosko Milekic } 4503099a0e58SBosko Milekic 4504099a0e58SBosko Milekic /* See uma.h */ 4505099a0e58SBosko Milekic void 4506099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4507099a0e58SBosko Milekic { 4508af526374SJeff Roberson 4509727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4510099a0e58SBosko Milekic zone->uz_init = zinit; 4511099a0e58SBosko Milekic } 4512099a0e58SBosko Milekic 4513099a0e58SBosko Milekic /* See uma.h */ 4514099a0e58SBosko Milekic void 4515099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4516099a0e58SBosko Milekic { 4517af526374SJeff Roberson 4518727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4519099a0e58SBosko Milekic zone->uz_fini = zfini; 4520099a0e58SBosko Milekic } 4521099a0e58SBosko Milekic 4522099a0e58SBosko Milekic /* See uma.h */ 4523099a0e58SBosko Milekic void 45248355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 45258355f576SJeff Roberson { 45260095a784SJeff Roberson uma_keg_t keg; 4527e20a199fSJeff Roberson 4528bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4529727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 45300095a784SJeff Roberson keg->uk_freef = freef; 45318355f576SJeff Roberson } 45328355f576SJeff Roberson 45338355f576SJeff Roberson /* See uma.h */ 45348355f576SJeff Roberson void 45358355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 45368355f576SJeff Roberson { 4537e20a199fSJeff Roberson uma_keg_t keg; 4538e20a199fSJeff Roberson 4539bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4540727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4541e20a199fSJeff Roberson keg->uk_allocf = allocf; 45428355f576SJeff Roberson } 45438355f576SJeff Roberson 45448355f576SJeff Roberson /* See uma.h */ 45456fd34d6fSJeff Roberson void 4546d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr) 4547d4665eaaSJeff Roberson { 4548d4665eaaSJeff Roberson 4549d4665eaaSJeff Roberson ZONE_ASSERT_COLD(zone); 4550d4665eaaSJeff Roberson 4551d4665eaaSJeff Roberson zone->uz_flags |= UMA_ZONE_SMR; 4552d4665eaaSJeff Roberson zone->uz_smr = smr; 4553d4665eaaSJeff Roberson zone_update_caches(zone); 4554d4665eaaSJeff Roberson } 4555d4665eaaSJeff Roberson 4556d4665eaaSJeff Roberson smr_t 4557d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone) 4558d4665eaaSJeff Roberson { 4559d4665eaaSJeff Roberson 4560d4665eaaSJeff Roberson return (zone->uz_smr); 4561d4665eaaSJeff Roberson } 4562d4665eaaSJeff Roberson 4563d4665eaaSJeff Roberson /* See uma.h */ 4564d4665eaaSJeff Roberson void 45656fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 45666fd34d6fSJeff Roberson { 45676fd34d6fSJeff Roberson uma_keg_t keg; 45686fd34d6fSJeff Roberson 4569bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4570727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 45716fd34d6fSJeff Roberson keg->uk_reserve = items; 45726fd34d6fSJeff Roberson } 45736fd34d6fSJeff Roberson 45746fd34d6fSJeff Roberson /* See uma.h */ 45758355f576SJeff Roberson int 4576a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 45778355f576SJeff Roberson { 4578099a0e58SBosko Milekic uma_keg_t keg; 45798355f576SJeff Roberson vm_offset_t kva; 45809ba30bcbSZbigniew Bodek u_int pages; 45818355f576SJeff Roberson 4582bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4583727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4584727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 45858355f576SJeff Roberson 458679c9f942SJeff Roberson pages = howmany(count, keg->uk_ipers) * keg->uk_ppera; 4587a553d4b8SJeff Roberson 4588a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4589a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4590a4915c21SAttilio Rao #else 4591a4915c21SAttilio Rao if (1) { 4592a4915c21SAttilio Rao #endif 459357223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4594d1f42ac2SAlan Cox if (kva == 0) 45958355f576SJeff Roberson return (0); 4596a4915c21SAttilio Rao } else 4597a4915c21SAttilio Rao kva = 0; 4598bb15d1c7SGleb Smirnoff 4599bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4600bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4601099a0e58SBosko Milekic keg->uk_kva = kva; 4602a4915c21SAttilio Rao keg->uk_offset = 0; 4603bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4604a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4605a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4606a4915c21SAttilio Rao #else 4607a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4608a4915c21SAttilio Rao #endif 4609cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4610cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4611cc7ce83aSJeff Roberson zone_update_caches(zone); 4612bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4613af526374SJeff Roberson 46148355f576SJeff Roberson return (1); 46158355f576SJeff Roberson } 46168355f576SJeff Roberson 46178355f576SJeff Roberson /* See uma.h */ 46188355f576SJeff Roberson void 46198355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 46208355f576SJeff Roberson { 4621920239efSMark Johnston struct vm_domainset_iter di; 4622ab3185d1SJeff Roberson uma_domain_t dom; 46238355f576SJeff Roberson uma_slab_t slab; 4624099a0e58SBosko Milekic uma_keg_t keg; 462586220393SMark Johnston int aflags, domain, slabs; 46268355f576SJeff Roberson 4627bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 462879c9f942SJeff Roberson slabs = howmany(items, keg->uk_ipers); 4629194a979eSMark Johnston while (slabs-- > 0) { 463086220393SMark Johnston aflags = M_NOWAIT; 463186220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 463286220393SMark Johnston &aflags); 463386220393SMark Johnston for (;;) { 463486220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 463586220393SMark Johnston aflags); 463686220393SMark Johnston if (slab != NULL) { 4637ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 46388b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 463986220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 464086220393SMark Johnston us_link); 46418b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 4642920239efSMark Johnston break; 46438355f576SJeff Roberson } 46448b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 464586220393SMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 464686220393SMark Johnston } 464786220393SMark Johnston } 464886220393SMark Johnston } 46498355f576SJeff Roberson 46508355f576SJeff Roberson /* See uma.h */ 465108cfa56eSMark Johnston void 465208cfa56eSMark Johnston uma_reclaim(int req) 46538355f576SJeff Roberson { 465444ec2b63SKonstantin Belousov 46551431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 465608cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 465786bbae32SJeff Roberson bucket_enable(); 465808cfa56eSMark Johnston 465908cfa56eSMark Johnston switch (req) { 466008cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 466120a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 466208cfa56eSMark Johnston break; 466308cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 466408cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 466520a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 466608cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 466708cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 466820a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4669a2de44abSAlexander Motin } 467008cfa56eSMark Johnston break; 467108cfa56eSMark Johnston default: 467208cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 467308cfa56eSMark Johnston } 46740f9b7bf3SMark Johnston 46758355f576SJeff Roberson /* 46768355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 46778355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 46788355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 46798355f576SJeff Roberson */ 46809b8db4d0SRyan Libby zone_drain(slabzones[0], NULL); 46819b8db4d0SRyan Libby zone_drain(slabzones[1], NULL); 4682cae33c14SJeff Roberson bucket_zone_drain(); 468308cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 46848355f576SJeff Roberson } 46858355f576SJeff Roberson 46862e47807cSJeff Roberson static volatile int uma_reclaim_needed; 468744ec2b63SKonstantin Belousov 468844ec2b63SKonstantin Belousov void 468944ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 469044ec2b63SKonstantin Belousov { 469144ec2b63SKonstantin Belousov 46922e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 46932e47807cSJeff Roberson wakeup(uma_reclaim); 469444ec2b63SKonstantin Belousov } 469544ec2b63SKonstantin Belousov 469644ec2b63SKonstantin Belousov void 469744ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 469844ec2b63SKonstantin Belousov { 469944ec2b63SKonstantin Belousov 470044ec2b63SKonstantin Belousov for (;;) { 470108cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4702200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 470308cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 47042e47807cSJeff Roberson hz); 470508cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 47069b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 470708cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4708200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 47092e47807cSJeff Roberson /* Don't fire more than once per-second. */ 47102e47807cSJeff Roberson pause("umarclslp", hz); 471144ec2b63SKonstantin Belousov } 471244ec2b63SKonstantin Belousov } 471344ec2b63SKonstantin Belousov 4714663b416fSJohn Baldwin /* See uma.h */ 471508cfa56eSMark Johnston void 471608cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 471708cfa56eSMark Johnston { 471808cfa56eSMark Johnston 471908cfa56eSMark Johnston switch (req) { 472008cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 472120a4e154SJeff Roberson zone_trim(zone, NULL); 472208cfa56eSMark Johnston break; 472308cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 472420a4e154SJeff Roberson zone_drain(zone, NULL); 472508cfa56eSMark Johnston break; 472608cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 472708cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 472820a4e154SJeff Roberson zone_drain(zone, NULL); 472908cfa56eSMark Johnston break; 473008cfa56eSMark Johnston default: 473108cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 473208cfa56eSMark Johnston } 473308cfa56eSMark Johnston } 473408cfa56eSMark Johnston 473508cfa56eSMark Johnston /* See uma.h */ 4736663b416fSJohn Baldwin int 4737663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 4738663b416fSJohn Baldwin { 4739663b416fSJohn Baldwin 4740727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 47416c125b8dSMohan Srinivasan } 47426c125b8dSMohan Srinivasan 47432e47807cSJeff Roberson unsigned long 47442e47807cSJeff Roberson uma_limit(void) 47452e47807cSJeff Roberson { 47462e47807cSJeff Roberson 47472e47807cSJeff Roberson return (uma_kmem_limit); 47482e47807cSJeff Roberson } 47492e47807cSJeff Roberson 47502e47807cSJeff Roberson void 47512e47807cSJeff Roberson uma_set_limit(unsigned long limit) 47522e47807cSJeff Roberson { 47532e47807cSJeff Roberson 47542e47807cSJeff Roberson uma_kmem_limit = limit; 47552e47807cSJeff Roberson } 47562e47807cSJeff Roberson 47572e47807cSJeff Roberson unsigned long 47582e47807cSJeff Roberson uma_size(void) 47592e47807cSJeff Roberson { 47602e47807cSJeff Roberson 4761058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 4762ad5b0f5bSJeff Roberson } 4763ad5b0f5bSJeff Roberson 4764ad5b0f5bSJeff Roberson long 4765ad5b0f5bSJeff Roberson uma_avail(void) 4766ad5b0f5bSJeff Roberson { 4767ad5b0f5bSJeff Roberson 4768058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 47692e47807cSJeff Roberson } 47702e47807cSJeff Roberson 4771a0d4b0aeSRobert Watson #ifdef DDB 47728355f576SJeff Roberson /* 47737a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 47747a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 47757a52a97eSRobert Watson * 47767a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 47777a52a97eSRobert Watson * per-CPU cache statistic. 47787a52a97eSRobert Watson * 47797a52a97eSRobert Watson */ 47807a52a97eSRobert Watson static void 47810f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 4782c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 47837a52a97eSRobert Watson { 47847a52a97eSRobert Watson uma_cache_t cache; 4785c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 47867a52a97eSRobert Watson int cachefree, cpu; 47877a52a97eSRobert Watson 4788c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 47897a52a97eSRobert Watson cachefree = 0; 47903aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 47917a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 4792376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 4793376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 4794376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 4795376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 47967a52a97eSRobert Watson allocs += cache->uc_allocs; 47977a52a97eSRobert Watson frees += cache->uc_frees; 47987a52a97eSRobert Watson } 47992efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 48002efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 4801bf965959SSean Bruno sleeps += z->uz_sleeps; 4802c1685086SJeff Roberson xdomain += z->uz_xdomain; 48037a52a97eSRobert Watson if (cachefreep != NULL) 48047a52a97eSRobert Watson *cachefreep = cachefree; 48057a52a97eSRobert Watson if (allocsp != NULL) 48067a52a97eSRobert Watson *allocsp = allocs; 48077a52a97eSRobert Watson if (freesp != NULL) 48087a52a97eSRobert Watson *freesp = frees; 4809bf965959SSean Bruno if (sleepsp != NULL) 4810bf965959SSean Bruno *sleepsp = sleeps; 4811c1685086SJeff Roberson if (xdomainp != NULL) 4812c1685086SJeff Roberson *xdomainp = xdomain; 48137a52a97eSRobert Watson } 4814a0d4b0aeSRobert Watson #endif /* DDB */ 48157a52a97eSRobert Watson 48167a52a97eSRobert Watson static int 48177a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 48187a52a97eSRobert Watson { 48197a52a97eSRobert Watson uma_keg_t kz; 48207a52a97eSRobert Watson uma_zone_t z; 48217a52a97eSRobert Watson int count; 48227a52a97eSRobert Watson 48237a52a97eSRobert Watson count = 0; 4824111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 48257a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 48267a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 48277a52a97eSRobert Watson count++; 48287a52a97eSRobert Watson } 4829b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4830b47acb0aSGleb Smirnoff count++; 4831b47acb0aSGleb Smirnoff 4832111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 48337a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 48347a52a97eSRobert Watson } 48357a52a97eSRobert Watson 4836b47acb0aSGleb Smirnoff static void 4837b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 4838b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 4839b47acb0aSGleb Smirnoff { 4840b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 4841b47acb0aSGleb Smirnoff uma_cache_t cache; 4842b47acb0aSGleb Smirnoff int i; 4843b47acb0aSGleb Smirnoff 4844b47acb0aSGleb Smirnoff 4845b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 4846b47acb0aSGleb Smirnoff zdom = &z->uz_domain[i]; 4847b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 4848b47acb0aSGleb Smirnoff } 4849b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 4850b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 4851b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 4852b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 4853c1685086SJeff Roberson uth->uth_xdomain = z->uz_xdomain; 48541de9724eSMark Johnston 4855b47acb0aSGleb Smirnoff /* 48561de9724eSMark Johnston * While it is not normally safe to access the cache bucket pointers 48571de9724eSMark Johnston * while not on the CPU that owns the cache, we only allow the pointers 48581de9724eSMark Johnston * to be exchanged without the zone lock held, not invalidated, so 48591de9724eSMark Johnston * accept the possible race associated with bucket exchange during 48601de9724eSMark Johnston * monitoring. Use atomic_load_ptr() to ensure that the bucket pointers 48611de9724eSMark Johnston * are loaded only once. 4862b47acb0aSGleb Smirnoff */ 4863b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 4864b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 4865b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 4866b47acb0aSGleb Smirnoff continue; 4867b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 4868376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 4869376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 4870376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 4871b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 4872b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 4873b47acb0aSGleb Smirnoff } 4874b47acb0aSGleb Smirnoff } 4875b47acb0aSGleb Smirnoff 48767a52a97eSRobert Watson static int 48777a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 48787a52a97eSRobert Watson { 48797a52a97eSRobert Watson struct uma_stream_header ush; 48807a52a97eSRobert Watson struct uma_type_header uth; 488163b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 48827a52a97eSRobert Watson struct sbuf sbuf; 48837a52a97eSRobert Watson uma_keg_t kz; 48847a52a97eSRobert Watson uma_zone_t z; 48854bd61e19SJeff Roberson uint64_t items; 48868b987a77SJeff Roberson uint32_t kfree, pages; 48874e657159SMatthew D Fleming int count, error, i; 48887a52a97eSRobert Watson 488900f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 489000f0e671SMatthew D Fleming if (error != 0) 489100f0e671SMatthew D Fleming return (error); 48924e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 48931eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 489463b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 48954e657159SMatthew D Fleming 4896404a593eSMatthew D Fleming count = 0; 4897111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 48987a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 48997a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 49007a52a97eSRobert Watson count++; 49017a52a97eSRobert Watson } 49027a52a97eSRobert Watson 4903b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4904b47acb0aSGleb Smirnoff count++; 4905b47acb0aSGleb Smirnoff 49067a52a97eSRobert Watson /* 49077a52a97eSRobert Watson * Insert stream header. 49087a52a97eSRobert Watson */ 49097a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 49107a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 4911ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 49127a52a97eSRobert Watson ush.ush_count = count; 49134e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 49147a52a97eSRobert Watson 49157a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 49168b987a77SJeff Roberson kfree = pages = 0; 49178b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 49188b987a77SJeff Roberson kfree += kz->uk_domain[i].ud_free; 49198b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 49208b987a77SJeff Roberson } 49217a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 49227a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 49237a52a97eSRobert Watson ZONE_LOCK(z); 4924cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 49257a52a97eSRobert Watson uth.uth_align = kz->uk_align; 49267a52a97eSRobert Watson uth.uth_size = kz->uk_size; 49277a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 49284bd61e19SJeff Roberson if (z->uz_max_items > 0) { 49294bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 49304bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 4931bb15d1c7SGleb Smirnoff kz->uk_ppera; 49324bd61e19SJeff Roberson } else 49338b987a77SJeff Roberson uth.uth_pages = pages; 4934f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 4935bb15d1c7SGleb Smirnoff kz->uk_ppera; 4936bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 49378b987a77SJeff Roberson uth.uth_keg_free = kfree; 4938cbbb4a00SRobert Watson 4939cbbb4a00SRobert Watson /* 4940cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 4941cbbb4a00SRobert Watson * on the keg's zone list. 4942cbbb4a00SRobert Watson */ 4943e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 4944cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 4945cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 4946b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 4947b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 49482450bbb8SRobert Watson ZONE_UNLOCK(z); 494963b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 495063b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 495163b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 49527a52a97eSRobert Watson } 49537a52a97eSRobert Watson } 4954b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 4955b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 4956b47acb0aSGleb Smirnoff ZONE_LOCK(z); 4957b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 4958b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 4959b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 4960b47acb0aSGleb Smirnoff ZONE_UNLOCK(z); 4961b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 4962b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 4963b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 4964b47acb0aSGleb Smirnoff } 4965b47acb0aSGleb Smirnoff 4966111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 49674e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 49684e657159SMatthew D Fleming sbuf_delete(&sbuf); 496963b5d112SKonstantin Belousov free(ups, M_TEMP); 49707a52a97eSRobert Watson return (error); 49717a52a97eSRobert Watson } 497248c5777eSRobert Watson 49730a5a3ccbSGleb Smirnoff int 49740a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 49750a5a3ccbSGleb Smirnoff { 49760a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 497716be9f54SGleb Smirnoff int error, max; 49780a5a3ccbSGleb Smirnoff 497916be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 49800a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 49810a5a3ccbSGleb Smirnoff if (error || !req->newptr) 49820a5a3ccbSGleb Smirnoff return (error); 49830a5a3ccbSGleb Smirnoff 49840a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 49850a5a3ccbSGleb Smirnoff 49860a5a3ccbSGleb Smirnoff return (0); 49870a5a3ccbSGleb Smirnoff } 49880a5a3ccbSGleb Smirnoff 49890a5a3ccbSGleb Smirnoff int 49900a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 49910a5a3ccbSGleb Smirnoff { 499220a4e154SJeff Roberson uma_zone_t zone; 49930a5a3ccbSGleb Smirnoff int cur; 49940a5a3ccbSGleb Smirnoff 499520a4e154SJeff Roberson /* 499620a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 499720a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 499820a4e154SJeff Roberson */ 499920a4e154SJeff Roberson if (arg2 == 0) 500020a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 500120a4e154SJeff Roberson else 500220a4e154SJeff Roberson zone = arg1; 50030a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 50040a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 50050a5a3ccbSGleb Smirnoff } 50060a5a3ccbSGleb Smirnoff 500720a4e154SJeff Roberson static int 500820a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 500920a4e154SJeff Roberson { 501020a4e154SJeff Roberson uma_zone_t zone = arg1; 501120a4e154SJeff Roberson uint64_t cur; 501220a4e154SJeff Roberson 501320a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 501420a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 501520a4e154SJeff Roberson } 501620a4e154SJeff Roberson 501720a4e154SJeff Roberson static int 501820a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 501920a4e154SJeff Roberson { 502020a4e154SJeff Roberson uma_zone_t zone = arg1; 502120a4e154SJeff Roberson uint64_t cur; 502220a4e154SJeff Roberson 502320a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 502420a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 502520a4e154SJeff Roberson } 502620a4e154SJeff Roberson 50276d204a6aSRyan Libby static int 50286d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 50296d204a6aSRyan Libby { 50306d204a6aSRyan Libby struct sbuf sbuf; 50316d204a6aSRyan Libby uma_zone_t zone = arg1; 50326d204a6aSRyan Libby int error; 50336d204a6aSRyan Libby 50346d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 50356d204a6aSRyan Libby if (zone->uz_flags != 0) 50366d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 50376d204a6aSRyan Libby else 50386d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 50396d204a6aSRyan Libby error = sbuf_finish(&sbuf); 50406d204a6aSRyan Libby sbuf_delete(&sbuf); 50416d204a6aSRyan Libby 50426d204a6aSRyan Libby return (error); 50436d204a6aSRyan Libby } 50446d204a6aSRyan Libby 5045f7af5015SRyan Libby static int 5046f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 5047f7af5015SRyan Libby { 5048f7af5015SRyan Libby uma_keg_t keg = arg1; 5049f7af5015SRyan Libby int avail, effpct, total; 5050f7af5015SRyan Libby 5051f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 505254c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 50539b8db4d0SRyan Libby total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize; 5054f7af5015SRyan Libby /* 5055f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 5056f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 5057f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 5058f7af5015SRyan Libby */ 5059f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 5060f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 5061f7af5015SRyan Libby avail *= mp_maxid + 1; 5062f7af5015SRyan Libby effpct = 100 * avail / total; 5063f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 5064f7af5015SRyan Libby } 5065f7af5015SRyan Libby 50664bd61e19SJeff Roberson static int 50674bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 50684bd61e19SJeff Roberson { 50694bd61e19SJeff Roberson uma_zone_t zone = arg1; 50704bd61e19SJeff Roberson uint64_t cur; 50714bd61e19SJeff Roberson 50724bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 50734bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 50744bd61e19SJeff Roberson } 50754bd61e19SJeff Roberson 50769542ea7bSGleb Smirnoff #ifdef INVARIANTS 50779542ea7bSGleb Smirnoff static uma_slab_t 50789542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 50799542ea7bSGleb Smirnoff { 50809542ea7bSGleb Smirnoff uma_slab_t slab; 50819542ea7bSGleb Smirnoff uma_keg_t keg; 50829542ea7bSGleb Smirnoff uint8_t *mem; 50839542ea7bSGleb Smirnoff 50849542ea7bSGleb Smirnoff /* 50859542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 50869542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 50879542ea7bSGleb Smirnoff * essentially holds a reference. 50889542ea7bSGleb Smirnoff */ 5089727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 5090727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5091bb15d1c7SGleb Smirnoff return (NULL); 509254c5ae80SRyan Libby if (zone->uz_flags & UMA_ZFLAG_VTOSLAB) 5093727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 5094bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 509554c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0) 5096727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 50978b987a77SJeff Roberson KEG_LOCK(keg, 0); 50989542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 50998b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 51009542ea7bSGleb Smirnoff 51019542ea7bSGleb Smirnoff return (slab); 51029542ea7bSGleb Smirnoff } 51039542ea7bSGleb Smirnoff 5104c5deaf04SGleb Smirnoff static bool 5105c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 5106c5deaf04SGleb Smirnoff { 5107c5deaf04SGleb Smirnoff 5108727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5109c5deaf04SGleb Smirnoff return (true); 5110c5deaf04SGleb Smirnoff 5111bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 5112c5deaf04SGleb Smirnoff } 5113c5deaf04SGleb Smirnoff 5114c5deaf04SGleb Smirnoff static bool 5115c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 5116c5deaf04SGleb Smirnoff { 5117c5deaf04SGleb Smirnoff uintptr_t idx; 5118c5deaf04SGleb Smirnoff 5119c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 5120c5deaf04SGleb Smirnoff return (true); 5121c5deaf04SGleb Smirnoff 5122c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 5123c5deaf04SGleb Smirnoff return (false); 5124c5deaf04SGleb Smirnoff 5125c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 5126c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 5127c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 5128c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 5129c5deaf04SGleb Smirnoff } 5130c5deaf04SGleb Smirnoff 5131c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 5132c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 5133c5deaf04SGleb Smirnoff return (true); 5134c5deaf04SGleb Smirnoff } 5135c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 5136c5deaf04SGleb Smirnoff 5137c5deaf04SGleb Smirnoff return (false); 5138c5deaf04SGleb Smirnoff } 5139c5deaf04SGleb Smirnoff 51409542ea7bSGleb Smirnoff /* 51419542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 51429542ea7bSGleb Smirnoff * 51439542ea7bSGleb Smirnoff */ 51449542ea7bSGleb Smirnoff static void 51459542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 51469542ea7bSGleb Smirnoff { 51479542ea7bSGleb Smirnoff uma_keg_t keg; 51489542ea7bSGleb Smirnoff int freei; 51499542ea7bSGleb Smirnoff 51509542ea7bSGleb Smirnoff if (slab == NULL) { 51519542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 51529542ea7bSGleb Smirnoff if (slab == NULL) 51539542ea7bSGleb Smirnoff panic("uma: item %p did not belong to zone %s\n", 51549542ea7bSGleb Smirnoff item, zone->uz_name); 51559542ea7bSGleb Smirnoff } 5156584061b4SJeff Roberson keg = zone->uz_keg; 51571e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 51589542ea7bSGleb Smirnoff 5159815db204SRyan Libby if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 51609542ea7bSGleb Smirnoff panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n", 51619542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 5162815db204SRyan Libby BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 51639542ea7bSGleb Smirnoff } 51649542ea7bSGleb Smirnoff 51659542ea7bSGleb Smirnoff /* 51669542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 51679542ea7bSGleb Smirnoff * and duplicate frees. 51689542ea7bSGleb Smirnoff * 51699542ea7bSGleb Smirnoff */ 51709542ea7bSGleb Smirnoff static void 51719542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 51729542ea7bSGleb Smirnoff { 51739542ea7bSGleb Smirnoff uma_keg_t keg; 51749542ea7bSGleb Smirnoff int freei; 51759542ea7bSGleb Smirnoff 51769542ea7bSGleb Smirnoff if (slab == NULL) { 51779542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 51789542ea7bSGleb Smirnoff if (slab == NULL) 51799542ea7bSGleb Smirnoff panic("uma: Freed item %p did not belong to zone %s\n", 51809542ea7bSGleb Smirnoff item, zone->uz_name); 51819542ea7bSGleb Smirnoff } 5182584061b4SJeff Roberson keg = zone->uz_keg; 51831e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 51849542ea7bSGleb Smirnoff 51859542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 51869542ea7bSGleb Smirnoff panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n", 51879542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 51889542ea7bSGleb Smirnoff 51891e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 51909542ea7bSGleb Smirnoff panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n", 51919542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 51929542ea7bSGleb Smirnoff 5193815db204SRyan Libby if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 51949542ea7bSGleb Smirnoff panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n", 51959542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 51969542ea7bSGleb Smirnoff 5197815db204SRyan Libby BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 51989542ea7bSGleb Smirnoff } 51999542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 52009542ea7bSGleb Smirnoff 520148c5777eSRobert Watson #ifdef DDB 520246d70077SConrad Meyer static int64_t 520346d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 52040223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 520548c5777eSRobert Watson { 520646d70077SConrad Meyer uint64_t frees; 52070f9b7bf3SMark Johnston int i; 520848c5777eSRobert Watson 520948c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 521046d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 52112efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 521246d70077SConrad Meyer *sleeps = z->uz_sleeps; 521346d70077SConrad Meyer *cachefree = 0; 521446d70077SConrad Meyer *xdomain = 0; 521548c5777eSRobert Watson } else 521646d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 521746d70077SConrad Meyer xdomain); 52188b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 52198b987a77SJeff Roberson *cachefree += z->uz_domain[i].uzd_nitems; 5220e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 522148c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 52228b987a77SJeff Roberson *cachefree += kz->uk_domain[i].ud_free; 52238b987a77SJeff Roberson } 522446d70077SConrad Meyer *used = *allocs - frees; 522546d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 522646d70077SConrad Meyer } 52270f9b7bf3SMark Johnston 522846d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 522946d70077SConrad Meyer { 523046d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 523146d70077SConrad Meyer uma_keg_t kz; 523246d70077SConrad Meyer uma_zone_t z; 523346d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 523446d70077SConrad Meyer long cachefree; 523546d70077SConrad Meyer /* variables for sorting */ 523646d70077SConrad Meyer uma_keg_t cur_keg; 523746d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 523846d70077SConrad Meyer int64_t cur_size, last_size, size; 523946d70077SConrad Meyer int ties; 524046d70077SConrad Meyer 524146d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 524246d70077SConrad Meyer if (modif[0] == 'i') { 524346d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 524446d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 524546d70077SConrad Meyer } else { 524646d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 524746d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 524846d70077SConrad Meyer } 524946d70077SConrad Meyer 525046d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 525146d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 525246d70077SConrad Meyer 525346d70077SConrad Meyer /* Sort the zones with largest size first. */ 525446d70077SConrad Meyer last_zone = NULL; 525546d70077SConrad Meyer last_size = INT64_MAX; 525646d70077SConrad Meyer for (;;) { 525746d70077SConrad Meyer cur_zone = NULL; 525846d70077SConrad Meyer cur_size = -1; 525946d70077SConrad Meyer ties = 0; 526046d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 526146d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 526246d70077SConrad Meyer /* 526346d70077SConrad Meyer * In the case of size ties, print out zones 526446d70077SConrad Meyer * in the order they are encountered. That is, 526546d70077SConrad Meyer * when we encounter the most recently output 526646d70077SConrad Meyer * zone, we have already printed all preceding 526746d70077SConrad Meyer * ties, and we must print all following ties. 526846d70077SConrad Meyer */ 526946d70077SConrad Meyer if (z == last_zone) { 527046d70077SConrad Meyer ties = 1; 527146d70077SConrad Meyer continue; 527246d70077SConrad Meyer } 527346d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 527446d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 527546d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 527646d70077SConrad Meyer { 527746d70077SConrad Meyer cur_size = size; 527846d70077SConrad Meyer cur_zone = z; 527946d70077SConrad Meyer cur_keg = kz; 528046d70077SConrad Meyer } 528146d70077SConrad Meyer } 528246d70077SConrad Meyer } 528346d70077SConrad Meyer if (cur_zone == NULL) 528446d70077SConrad Meyer break; 528546d70077SConrad Meyer 528646d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 528746d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 528846d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 528946d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 529046d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 529120a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 529220a4e154SJeff Roberson xdomain); 529346d70077SConrad Meyer 5294687c94aaSJohn Baldwin if (db_pager_quit) 5295687c94aaSJohn Baldwin return; 529646d70077SConrad Meyer last_zone = cur_zone; 529746d70077SConrad Meyer last_size = cur_size; 529848c5777eSRobert Watson } 529948c5777eSRobert Watson } 530003175483SAlexander Motin 530103175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 530203175483SAlexander Motin { 530303175483SAlexander Motin uma_zone_t z; 5304ab3185d1SJeff Roberson uint64_t allocs, frees; 53050f9b7bf3SMark Johnston long cachefree; 53060f9b7bf3SMark Johnston int i; 530703175483SAlexander Motin 530803175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 530903175483SAlexander Motin "Requests", "Bucket"); 531003175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 5311c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 53120f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 53130f9b7bf3SMark Johnston cachefree += z->uz_domain[i].uzd_nitems; 53140f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 531503175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 531603175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 531720a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 531803175483SAlexander Motin if (db_pager_quit) 531903175483SAlexander Motin return; 532003175483SAlexander Motin } 532103175483SAlexander Motin } 53229542ea7bSGleb Smirnoff #endif /* DDB */ 5323