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> 80*d4665eaaSJeff 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 107*d4665eaaSJeff Roberson #ifdef INVARIANTS 108*d4665eaaSJeff Roberson #define UMA_ALWAYS_CTORDTOR 1 109*d4665eaaSJeff Roberson #else 110*d4665eaaSJeff Roberson #define UMA_ALWAYS_CTORDTOR 0 111*d4665eaaSJeff Roberson #endif 112*d4665eaaSJeff 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) 239eda1b016SJeff Roberson #define BUCKET_MIN BUCKET_SIZE(4) 240fc03d22bSJeff Roberson 241fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = { 2426fd34d6fSJeff Roberson { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 }, 243f3932e90SAlexander Motin { NULL, "6 Bucket", BUCKET_SIZE(6), 3072 }, 2446fd34d6fSJeff Roberson { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 }, 245f3932e90SAlexander Motin { NULL, "12 Bucket", BUCKET_SIZE(12), 1536 }, 2466fd34d6fSJeff Roberson { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 }, 247fc03d22bSJeff Roberson { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 248fc03d22bSJeff Roberson { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 249fc03d22bSJeff Roberson { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 2501aa6c758SAlexander Motin { NULL, "256 Bucket", BUCKET_SIZE(256), 64 }, 251fc03d22bSJeff Roberson { NULL, NULL, 0} 252fc03d22bSJeff Roberson }; 253cae33c14SJeff Roberson 2542019094aSRobert Watson /* 2552019094aSRobert Watson * Flags and enumerations to be passed to internal functions. 2562019094aSRobert Watson */ 257bb15d1c7SGleb Smirnoff enum zfreeskip { 258bb15d1c7SGleb Smirnoff SKIP_NONE = 0, 259bb15d1c7SGleb Smirnoff SKIP_CNT = 0x00000001, 260bb15d1c7SGleb Smirnoff SKIP_DTOR = 0x00010000, 261bb15d1c7SGleb Smirnoff SKIP_FINI = 0x00020000, 262bb15d1c7SGleb Smirnoff }; 263b23f72e9SBrian Feldman 2648355f576SJeff Roberson /* Prototypes.. */ 2658355f576SJeff Roberson 266a81c400eSJeff Roberson void uma_startup1(vm_offset_t); 267f4bef67cSGleb Smirnoff void uma_startup2(void); 268f4bef67cSGleb Smirnoff 269ab3185d1SJeff Roberson static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 270ab3185d1SJeff Roberson static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 271ab3059a8SMatt Macy static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 272ab3185d1SJeff Roberson static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 273f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t); 274ab3059a8SMatt Macy static void pcpu_page_free(void *, vm_size_t, uint8_t); 27586220393SMark Johnston static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int); 2769643769aSJeff Roberson static void cache_drain(uma_zone_t); 2778355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t); 27808cfa56eSMark Johnston static void bucket_cache_reclaim(uma_zone_t zone, bool); 279b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int); 280099a0e58SBosko Milekic static void keg_dtor(void *, int, void *); 281b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int); 2829c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *); 283*d4665eaaSJeff Roberson static inline void item_dtor(uma_zone_t zone, void *item, int size, 284*d4665eaaSJeff Roberson void *udata, enum zfreeskip skip); 285b23f72e9SBrian Feldman static int zero_init(void *, int, int); 28620a4e154SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t, void *), void *); 287a81c400eSJeff Roberson static void zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *), void *); 28820a4e154SJeff Roberson static void zone_timeout(uma_zone_t zone, void *); 2893b2f2cb8SAlexander Motin static int hash_alloc(struct uma_hash *, u_int); 2900aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *); 2910aef6126SJeff Roberson static void hash_free(struct uma_hash *hash); 2928355f576SJeff Roberson static void uma_timeout(void *); 2938355f576SJeff Roberson static void uma_startup3(void); 294860bb7a0SMark Johnston static void uma_shutdown(void); 295ab3185d1SJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int, int); 2960095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 2974bd61e19SJeff Roberson static int zone_alloc_limit(uma_zone_t zone, int count, int flags); 2984bd61e19SJeff Roberson static void zone_free_limit(uma_zone_t zone, int count); 29986bbae32SJeff Roberson static void bucket_enable(void); 300cae33c14SJeff Roberson static void bucket_init(void); 3016fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int); 3026fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *); 303cae33c14SJeff Roberson static void bucket_zone_drain(void); 304beb8beefSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int); 3050095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 306bb15d1c7SGleb Smirnoff static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item); 307e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 30885dcf349SGleb Smirnoff uma_fini fini, int align, uint32_t flags); 309b75c4efcSAndrew Turner static int zone_import(void *, void **, int, int, int); 310b75c4efcSAndrew Turner static void zone_release(void *, void **, int); 311beb8beefSJeff Roberson static bool cache_alloc(uma_zone_t, uma_cache_t, void *, int); 3120a81b439SJeff Roberson static bool cache_free(uma_zone_t, uma_cache_t, void *, void *, int); 313bbee39c6SJeff Roberson 3147a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 3157a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 31620a4e154SJeff Roberson static int sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS); 31720a4e154SJeff Roberson static int sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS); 3186d204a6aSRyan Libby static int sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS); 319f7af5015SRyan Libby static int sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS); 3204bd61e19SJeff Roberson static int sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS); 3218355f576SJeff Roberson 32231c251a0SJeff Roberson static uint64_t uma_zone_get_allocs(uma_zone_t zone); 32331c251a0SJeff Roberson 3249542ea7bSGleb Smirnoff #ifdef INVARIANTS 32531c251a0SJeff Roberson static uint64_t uma_keg_get_allocs(uma_keg_t zone); 326815db204SRyan Libby static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg); 327815db204SRyan Libby 328c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem); 329c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem); 3309542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item); 3319542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item); 332c5deaf04SGleb Smirnoff 333c5deaf04SGleb Smirnoff static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0, 334c5deaf04SGleb Smirnoff "Memory allocation debugging"); 335c5deaf04SGleb Smirnoff 336c5deaf04SGleb Smirnoff static u_int dbg_divisor = 1; 337c5deaf04SGleb Smirnoff SYSCTL_UINT(_vm_debug, OID_AUTO, divisor, 338c5deaf04SGleb Smirnoff CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0, 339c5deaf04SGleb Smirnoff "Debug & thrash every this item in memory allocator"); 340c5deaf04SGleb Smirnoff 341c5deaf04SGleb Smirnoff static counter_u64_t uma_dbg_cnt = EARLY_COUNTER; 342c5deaf04SGleb Smirnoff static counter_u64_t uma_skip_cnt = EARLY_COUNTER; 343c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD, 344c5deaf04SGleb Smirnoff &uma_dbg_cnt, "memory items debugged"); 345c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD, 346c5deaf04SGleb Smirnoff &uma_skip_cnt, "memory items skipped, not debugged"); 3479542ea7bSGleb Smirnoff #endif 3489542ea7bSGleb Smirnoff 3498355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 3508355f576SJeff Roberson 35135ec24f3SRyan Libby SYSCTL_NODE(_vm, OID_AUTO, uma, CTLFLAG_RW, 0, "Universal Memory Allocator"); 35235ec24f3SRyan Libby 353a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_INT, 3547a52a97eSRobert Watson 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 3557a52a97eSRobert Watson 356a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_STRUCT, 3577a52a97eSRobert Watson 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 3587a52a97eSRobert Watson 3592f891cd5SPawel Jakub Dawidek static int zone_warnings = 1; 360af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0, 3612f891cd5SPawel Jakub Dawidek "Warn when UMA zones becomes full"); 3622f891cd5SPawel Jakub Dawidek 36386bbae32SJeff Roberson /* 3649b8db4d0SRyan Libby * Select the slab zone for an offpage slab with the given maximum item count. 3659b8db4d0SRyan Libby */ 3669b8db4d0SRyan Libby static inline uma_zone_t 3679b8db4d0SRyan Libby slabzone(int ipers) 3689b8db4d0SRyan Libby { 3699b8db4d0SRyan Libby 3709b8db4d0SRyan Libby return (slabzones[ipers > SLABZONE0_SETSIZE]); 3719b8db4d0SRyan Libby } 3729b8db4d0SRyan Libby 3739b8db4d0SRyan Libby /* 37486bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 37586bbae32SJeff Roberson */ 37686bbae32SJeff Roberson static void 37786bbae32SJeff Roberson bucket_enable(void) 37886bbae32SJeff Roberson { 3793182660aSRyan Libby 380a81c400eSJeff Roberson KASSERT(booted >= BOOT_KVA, ("Bucket enable before init")); 381251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 38286bbae32SJeff Roberson } 38386bbae32SJeff Roberson 384dc2c7965SRobert Watson /* 385dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 386dc2c7965SRobert Watson * 387dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 388fc03d22bSJeff Roberson * of the header and an array of pointers. 389dc2c7965SRobert Watson */ 390cae33c14SJeff Roberson static void 391cae33c14SJeff Roberson bucket_init(void) 392cae33c14SJeff Roberson { 393cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 394cae33c14SJeff Roberson int size; 395cae33c14SJeff Roberson 396d74e6a1dSAlan Cox for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 397cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 398cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 399cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 400e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 401dfe13344SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | 402dfe13344SJeff Roberson UMA_ZONE_FIRSTTOUCH); 403cae33c14SJeff Roberson } 404cae33c14SJeff Roberson } 405cae33c14SJeff Roberson 406dc2c7965SRobert Watson /* 407dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 408dc2c7965SRobert Watson * to allocate the bucket. 409dc2c7965SRobert Watson */ 410dc2c7965SRobert Watson static struct uma_bucket_zone * 411dc2c7965SRobert Watson bucket_zone_lookup(int entries) 412dc2c7965SRobert Watson { 413fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 414dc2c7965SRobert Watson 415fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 416fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 417fc03d22bSJeff Roberson return (ubz); 418fc03d22bSJeff Roberson ubz--; 419fc03d22bSJeff Roberson return (ubz); 420fc03d22bSJeff Roberson } 421fc03d22bSJeff Roberson 422003cf08bSMark Johnston static struct uma_bucket_zone * 423003cf08bSMark Johnston bucket_zone_max(uma_zone_t zone, int nitems) 424003cf08bSMark Johnston { 425003cf08bSMark Johnston struct uma_bucket_zone *ubz; 426003cf08bSMark Johnston int bpcpu; 427003cf08bSMark Johnston 428003cf08bSMark Johnston bpcpu = 2; 429dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 430003cf08bSMark Johnston /* Count the cross-domain bucket. */ 431003cf08bSMark Johnston bpcpu++; 432003cf08bSMark Johnston 433003cf08bSMark Johnston for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 434003cf08bSMark Johnston if (ubz->ubz_entries * bpcpu * mp_ncpus > nitems) 435003cf08bSMark Johnston break; 436003cf08bSMark Johnston if (ubz == &bucket_zones[0]) 437003cf08bSMark Johnston ubz = NULL; 438003cf08bSMark Johnston else 439003cf08bSMark Johnston ubz--; 440003cf08bSMark Johnston return (ubz); 441003cf08bSMark Johnston } 442003cf08bSMark Johnston 443fc03d22bSJeff Roberson static int 444fc03d22bSJeff Roberson bucket_select(int size) 445fc03d22bSJeff Roberson { 446fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 447fc03d22bSJeff Roberson 448fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 449fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 450fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 451fc03d22bSJeff Roberson 452fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 453fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 454fc03d22bSJeff Roberson break; 455fc03d22bSJeff Roberson ubz--; 456fc03d22bSJeff Roberson return (ubz->ubz_entries); 457dc2c7965SRobert Watson } 458dc2c7965SRobert Watson 459cae33c14SJeff Roberson static uma_bucket_t 4606fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags) 461cae33c14SJeff Roberson { 462cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 463cae33c14SJeff Roberson uma_bucket_t bucket; 464cae33c14SJeff Roberson 465cae33c14SJeff Roberson /* 466*d4665eaaSJeff Roberson * Don't allocate buckets early in boot. 467cae33c14SJeff Roberson */ 468*d4665eaaSJeff Roberson if (__predict_false(booted < BOOT_KVA)) 469cae33c14SJeff Roberson return (NULL); 470a81c400eSJeff Roberson 4716fd34d6fSJeff Roberson /* 4726fd34d6fSJeff Roberson * To limit bucket recursion we store the original zone flags 4736fd34d6fSJeff Roberson * in a cookie passed via zalloc_arg/zfree_arg. This allows the 4746fd34d6fSJeff Roberson * NOVM flag to persist even through deep recursions. We also 4756fd34d6fSJeff Roberson * store ZFLAG_BUCKET once we have recursed attempting to allocate 4766fd34d6fSJeff Roberson * a bucket for a bucket zone so we do not allow infinite bucket 4776fd34d6fSJeff Roberson * recursion. This cookie will even persist to frees of unused 4786fd34d6fSJeff Roberson * buckets via the allocation path or bucket allocations in the 4796fd34d6fSJeff Roberson * free path. 4806fd34d6fSJeff Roberson */ 4816fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 4826fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 483e8a720feSAlexander Motin else { 484e8a720feSAlexander Motin if ((uintptr_t)udata & UMA_ZFLAG_BUCKET) 485e8a720feSAlexander Motin return (NULL); 4866fd34d6fSJeff Roberson udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET); 487e8a720feSAlexander Motin } 4886fd34d6fSJeff Roberson if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY) 489af526374SJeff Roberson flags |= M_NOVM; 49020a4e154SJeff Roberson ubz = bucket_zone_lookup(zone->uz_bucket_size); 49120d3ab87SAlexander Motin if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0) 49220d3ab87SAlexander Motin ubz++; 4936fd34d6fSJeff Roberson bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags); 494cae33c14SJeff Roberson if (bucket) { 495cae33c14SJeff Roberson #ifdef INVARIANTS 496cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 497cae33c14SJeff Roberson #endif 498cae33c14SJeff Roberson bucket->ub_cnt = 0; 499cae33c14SJeff Roberson bucket->ub_entries = ubz->ubz_entries; 500*d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 501*d4665eaaSJeff Roberson CTR3(KTR_UMA, "bucket_alloc: zone %s(%p) allocated bucket %p", 502*d4665eaaSJeff Roberson zone->uz_name, zone, bucket); 503cae33c14SJeff Roberson } 504cae33c14SJeff Roberson 505cae33c14SJeff Roberson return (bucket); 506cae33c14SJeff Roberson } 507cae33c14SJeff Roberson 508cae33c14SJeff Roberson static void 5096fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata) 510cae33c14SJeff Roberson { 511cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 512cae33c14SJeff Roberson 513fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 514fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 515*d4665eaaSJeff Roberson KASSERT(bucket->ub_seq == SMR_SEQ_INVALID, 516*d4665eaaSJeff Roberson ("bucket_free: Freeing an SMR bucket.")); 5176fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 5186fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 519dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 5206fd34d6fSJeff Roberson uma_zfree_arg(ubz->ubz_zone, bucket, udata); 521cae33c14SJeff Roberson } 522cae33c14SJeff Roberson 523cae33c14SJeff Roberson static void 524cae33c14SJeff Roberson bucket_zone_drain(void) 525cae33c14SJeff Roberson { 526cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 527cae33c14SJeff Roberson 528cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 52908cfa56eSMark Johnston uma_zone_reclaim(ubz->ubz_zone, UMA_RECLAIM_DRAIN); 530cae33c14SJeff Roberson } 531cae33c14SJeff Roberson 53208cfa56eSMark Johnston /* 53308cfa56eSMark Johnston * Attempt to satisfy an allocation by retrieving a full bucket from one of the 534*d4665eaaSJeff Roberson * zone's caches. If a bucket is found the zone is not locked on return. 53508cfa56eSMark Johnston */ 5360f9b7bf3SMark Johnston static uma_bucket_t 53708cfa56eSMark Johnston zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom) 5380f9b7bf3SMark Johnston { 5390f9b7bf3SMark Johnston uma_bucket_t bucket; 540*d4665eaaSJeff Roberson int i; 541*d4665eaaSJeff Roberson bool dtor = false; 5420f9b7bf3SMark Johnston 5430f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 5440f9b7bf3SMark Johnston 545*d4665eaaSJeff Roberson if ((bucket = TAILQ_FIRST(&zdom->uzd_buckets)) == NULL) 546*d4665eaaSJeff Roberson return (NULL); 547*d4665eaaSJeff Roberson 548*d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 549*d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 550*d4665eaaSJeff Roberson if (!smr_poll(zone->uz_smr, bucket->ub_seq, false)) 551*d4665eaaSJeff Roberson return (NULL); 552*d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 553*d4665eaaSJeff Roberson dtor = (zone->uz_dtor != NULL) | UMA_ALWAYS_CTORDTOR; 554*d4665eaaSJeff Roberson } 5550f9b7bf3SMark Johnston MPASS(zdom->uzd_nitems >= bucket->ub_cnt); 55608cfa56eSMark Johnston TAILQ_REMOVE(&zdom->uzd_buckets, bucket, ub_link); 5570f9b7bf3SMark Johnston zdom->uzd_nitems -= bucket->ub_cnt; 55808cfa56eSMark Johnston if (zdom->uzd_imin > zdom->uzd_nitems) 5590f9b7bf3SMark Johnston zdom->uzd_imin = zdom->uzd_nitems; 560bb15d1c7SGleb Smirnoff zone->uz_bkt_count -= bucket->ub_cnt; 561*d4665eaaSJeff Roberson ZONE_UNLOCK(zone); 562*d4665eaaSJeff Roberson if (dtor) 563*d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 564*d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], zone->uz_size, 565*d4665eaaSJeff Roberson NULL, SKIP_NONE); 566*d4665eaaSJeff Roberson 5670f9b7bf3SMark Johnston return (bucket); 5680f9b7bf3SMark Johnston } 5690f9b7bf3SMark Johnston 57008cfa56eSMark Johnston /* 57108cfa56eSMark Johnston * Insert a full bucket into the specified cache. The "ws" parameter indicates 57208cfa56eSMark Johnston * whether the bucket's contents should be counted as part of the zone's working 57308cfa56eSMark Johnston * set. 57408cfa56eSMark Johnston */ 5750f9b7bf3SMark Johnston static void 5760f9b7bf3SMark Johnston zone_put_bucket(uma_zone_t zone, uma_zone_domain_t zdom, uma_bucket_t bucket, 5770f9b7bf3SMark Johnston const bool ws) 5780f9b7bf3SMark Johnston { 5790f9b7bf3SMark Johnston 5800f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 58108034d10SKonstantin Belousov KASSERT(!ws || zone->uz_bkt_count < zone->uz_bkt_max, 58208034d10SKonstantin Belousov ("%s: zone %p overflow", __func__, zone)); 5830f9b7bf3SMark Johnston 584*d4665eaaSJeff Roberson if (ws && bucket->ub_seq == SMR_SEQ_INVALID) 58508cfa56eSMark Johnston TAILQ_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link); 58608cfa56eSMark Johnston else 58708cfa56eSMark Johnston TAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link); 5880f9b7bf3SMark Johnston zdom->uzd_nitems += bucket->ub_cnt; 5890f9b7bf3SMark Johnston if (ws && zdom->uzd_imax < zdom->uzd_nitems) 5900f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_nitems; 591bb15d1c7SGleb Smirnoff zone->uz_bkt_count += bucket->ub_cnt; 5920f9b7bf3SMark Johnston } 5930f9b7bf3SMark Johnston 594376b1ba3SJeff Roberson /* Pops an item out of a per-cpu cache bucket. */ 595376b1ba3SJeff Roberson static inline void * 596376b1ba3SJeff Roberson cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket) 597376b1ba3SJeff Roberson { 598376b1ba3SJeff Roberson void *item; 599376b1ba3SJeff Roberson 600376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 601376b1ba3SJeff Roberson 602376b1ba3SJeff Roberson bucket->ucb_cnt--; 603376b1ba3SJeff Roberson item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt]; 604376b1ba3SJeff Roberson #ifdef INVARIANTS 605376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL; 606376b1ba3SJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 607376b1ba3SJeff Roberson #endif 608376b1ba3SJeff Roberson cache->uc_allocs++; 609376b1ba3SJeff Roberson 610376b1ba3SJeff Roberson return (item); 611376b1ba3SJeff Roberson } 612376b1ba3SJeff Roberson 613376b1ba3SJeff Roberson /* Pushes an item into a per-cpu cache bucket. */ 614376b1ba3SJeff Roberson static inline void 615376b1ba3SJeff Roberson cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item) 616376b1ba3SJeff Roberson { 617376b1ba3SJeff Roberson 618376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 619376b1ba3SJeff Roberson KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL, 620376b1ba3SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 621376b1ba3SJeff Roberson 622376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item; 623376b1ba3SJeff Roberson bucket->ucb_cnt++; 624376b1ba3SJeff Roberson cache->uc_frees++; 625376b1ba3SJeff Roberson } 626376b1ba3SJeff Roberson 627376b1ba3SJeff Roberson /* 628376b1ba3SJeff Roberson * Unload a UMA bucket from a per-cpu cache. 629376b1ba3SJeff Roberson */ 630376b1ba3SJeff Roberson static inline uma_bucket_t 631376b1ba3SJeff Roberson cache_bucket_unload(uma_cache_bucket_t bucket) 632376b1ba3SJeff Roberson { 633376b1ba3SJeff Roberson uma_bucket_t b; 634376b1ba3SJeff Roberson 635376b1ba3SJeff Roberson b = bucket->ucb_bucket; 636376b1ba3SJeff Roberson if (b != NULL) { 637376b1ba3SJeff Roberson MPASS(b->ub_entries == bucket->ucb_entries); 638376b1ba3SJeff Roberson b->ub_cnt = bucket->ucb_cnt; 639376b1ba3SJeff Roberson bucket->ucb_bucket = NULL; 640376b1ba3SJeff Roberson bucket->ucb_entries = bucket->ucb_cnt = 0; 641376b1ba3SJeff Roberson } 642376b1ba3SJeff Roberson 643376b1ba3SJeff Roberson return (b); 644376b1ba3SJeff Roberson } 645376b1ba3SJeff Roberson 646376b1ba3SJeff Roberson static inline uma_bucket_t 647376b1ba3SJeff Roberson cache_bucket_unload_alloc(uma_cache_t cache) 648376b1ba3SJeff Roberson { 649376b1ba3SJeff Roberson 650376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_allocbucket)); 651376b1ba3SJeff Roberson } 652376b1ba3SJeff Roberson 653376b1ba3SJeff Roberson static inline uma_bucket_t 654376b1ba3SJeff Roberson cache_bucket_unload_free(uma_cache_t cache) 655376b1ba3SJeff Roberson { 656376b1ba3SJeff Roberson 657376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_freebucket)); 658376b1ba3SJeff Roberson } 659376b1ba3SJeff Roberson 660376b1ba3SJeff Roberson static inline uma_bucket_t 661376b1ba3SJeff Roberson cache_bucket_unload_cross(uma_cache_t cache) 662376b1ba3SJeff Roberson { 663376b1ba3SJeff Roberson 664376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_crossbucket)); 665376b1ba3SJeff Roberson } 666376b1ba3SJeff Roberson 667376b1ba3SJeff Roberson /* 668376b1ba3SJeff Roberson * Load a bucket into a per-cpu cache bucket. 669376b1ba3SJeff Roberson */ 670376b1ba3SJeff Roberson static inline void 671376b1ba3SJeff Roberson cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b) 672376b1ba3SJeff Roberson { 673376b1ba3SJeff Roberson 674376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 675376b1ba3SJeff Roberson MPASS(bucket->ucb_bucket == NULL); 676376b1ba3SJeff Roberson 677376b1ba3SJeff Roberson bucket->ucb_bucket = b; 678376b1ba3SJeff Roberson bucket->ucb_cnt = b->ub_cnt; 679376b1ba3SJeff Roberson bucket->ucb_entries = b->ub_entries; 680376b1ba3SJeff Roberson } 681376b1ba3SJeff Roberson 682376b1ba3SJeff Roberson static inline void 683376b1ba3SJeff Roberson cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b) 684376b1ba3SJeff Roberson { 685376b1ba3SJeff Roberson 686376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_allocbucket, b); 687376b1ba3SJeff Roberson } 688376b1ba3SJeff Roberson 689376b1ba3SJeff Roberson static inline void 690376b1ba3SJeff Roberson cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b) 691376b1ba3SJeff Roberson { 692376b1ba3SJeff Roberson 693376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_freebucket, b); 694376b1ba3SJeff Roberson } 695376b1ba3SJeff Roberson 696dfe13344SJeff Roberson #ifdef NUMA 697376b1ba3SJeff Roberson static inline void 698376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b) 699376b1ba3SJeff Roberson { 700376b1ba3SJeff Roberson 701376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_crossbucket, b); 702376b1ba3SJeff Roberson } 703376b1ba3SJeff Roberson #endif 704376b1ba3SJeff Roberson 705376b1ba3SJeff Roberson /* 706376b1ba3SJeff Roberson * Copy and preserve ucb_spare. 707376b1ba3SJeff Roberson */ 708376b1ba3SJeff Roberson static inline void 709376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 710376b1ba3SJeff Roberson { 711376b1ba3SJeff Roberson 712376b1ba3SJeff Roberson b1->ucb_bucket = b2->ucb_bucket; 713376b1ba3SJeff Roberson b1->ucb_entries = b2->ucb_entries; 714376b1ba3SJeff Roberson b1->ucb_cnt = b2->ucb_cnt; 715376b1ba3SJeff Roberson } 716376b1ba3SJeff Roberson 717376b1ba3SJeff Roberson /* 718376b1ba3SJeff Roberson * Swap two cache buckets. 719376b1ba3SJeff Roberson */ 720376b1ba3SJeff Roberson static inline void 721376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 722376b1ba3SJeff Roberson { 723376b1ba3SJeff Roberson struct uma_cache_bucket b3; 724376b1ba3SJeff Roberson 725376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 726376b1ba3SJeff Roberson 727376b1ba3SJeff Roberson cache_bucket_copy(&b3, b1); 728376b1ba3SJeff Roberson cache_bucket_copy(b1, b2); 729376b1ba3SJeff Roberson cache_bucket_copy(b2, &b3); 730376b1ba3SJeff Roberson } 731376b1ba3SJeff Roberson 7322f891cd5SPawel Jakub Dawidek static void 7332f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 7342f891cd5SPawel Jakub Dawidek { 7352f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 7362f891cd5SPawel Jakub Dawidek 7372f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 7382f891cd5SPawel Jakub Dawidek return; 7392f891cd5SPawel Jakub Dawidek 7402f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 7412f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 7422f891cd5SPawel Jakub Dawidek } 7432f891cd5SPawel Jakub Dawidek 74454503a13SJonathan T. Looney static inline void 74554503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone) 74654503a13SJonathan T. Looney { 747e60b2fcbSGleb Smirnoff 748e60b2fcbSGleb Smirnoff if (zone->uz_maxaction.ta_func != NULL) 749e60b2fcbSGleb Smirnoff taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction); 75054503a13SJonathan T. Looney } 75154503a13SJonathan T. Looney 7528355f576SJeff Roberson /* 7538355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 7549643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 7558355f576SJeff Roberson * 7568355f576SJeff Roberson * Arguments: 7578355f576SJeff Roberson * arg Unused 7588355f576SJeff Roberson * 7598355f576SJeff Roberson * Returns: 7608355f576SJeff Roberson * Nothing 7618355f576SJeff Roberson */ 7628355f576SJeff Roberson static void 7638355f576SJeff Roberson uma_timeout(void *unused) 7648355f576SJeff Roberson { 76586bbae32SJeff Roberson bucket_enable(); 76620a4e154SJeff Roberson zone_foreach(zone_timeout, NULL); 7678355f576SJeff Roberson 7688355f576SJeff Roberson /* Reschedule this event */ 7699643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 7708355f576SJeff Roberson } 7718355f576SJeff Roberson 7728355f576SJeff Roberson /* 7730f9b7bf3SMark Johnston * Update the working set size estimate for the zone's bucket cache. 7740f9b7bf3SMark Johnston * The constants chosen here are somewhat arbitrary. With an update period of 7750f9b7bf3SMark Johnston * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the 7760f9b7bf3SMark Johnston * last 100s. 7770f9b7bf3SMark Johnston */ 7780f9b7bf3SMark Johnston static void 7790f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom) 7800f9b7bf3SMark Johnston { 7810f9b7bf3SMark Johnston long wss; 7820f9b7bf3SMark Johnston 7830f9b7bf3SMark Johnston MPASS(zdom->uzd_imax >= zdom->uzd_imin); 7840f9b7bf3SMark Johnston wss = zdom->uzd_imax - zdom->uzd_imin; 7850f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems; 78608cfa56eSMark Johnston zdom->uzd_wss = (4 * wss + zdom->uzd_wss) / 5; 7870f9b7bf3SMark Johnston } 7880f9b7bf3SMark Johnston 7890f9b7bf3SMark Johnston /* 7909643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 7919643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 7928355f576SJeff Roberson * 793e20a199fSJeff Roberson * Returns nothing. 7948355f576SJeff Roberson */ 7958355f576SJeff Roberson static void 79620a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused) 7978355f576SJeff Roberson { 79808034d10SKonstantin Belousov uma_keg_t keg; 7998b987a77SJeff Roberson u_int slabs, pages; 8008355f576SJeff Roberson 80154c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 80208034d10SKonstantin Belousov goto update_wss; 80308034d10SKonstantin Belousov 80408034d10SKonstantin Belousov keg = zone->uz_keg; 8058b987a77SJeff Roberson 8068b987a77SJeff Roberson /* 8078b987a77SJeff Roberson * Hash zones are non-numa by definition so the first domain 8088b987a77SJeff Roberson * is the only one present. 8098b987a77SJeff Roberson */ 8108b987a77SJeff Roberson KEG_LOCK(keg, 0); 8118b987a77SJeff Roberson pages = keg->uk_domain[0].ud_pages; 8128b987a77SJeff Roberson 8138355f576SJeff Roberson /* 814e20a199fSJeff Roberson * Expand the keg hash table. 8158355f576SJeff Roberson * 8168355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 8178355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 8188355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 8198355f576SJeff Roberson */ 8208b987a77SJeff Roberson if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) { 8210aef6126SJeff Roberson struct uma_hash newhash; 8220aef6126SJeff Roberson struct uma_hash oldhash; 8230aef6126SJeff Roberson int ret; 8245300d9ddSJeff Roberson 8250aef6126SJeff Roberson /* 8260aef6126SJeff Roberson * This is so involved because allocating and freeing 827e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 8280aef6126SJeff Roberson * I have to do everything in stages and check for 8290aef6126SJeff Roberson * races. 8300aef6126SJeff Roberson */ 8318b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 8323b2f2cb8SAlexander Motin ret = hash_alloc(&newhash, 1 << fls(slabs)); 8338b987a77SJeff Roberson KEG_LOCK(keg, 0); 8340aef6126SJeff Roberson if (ret) { 835099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 836099a0e58SBosko Milekic oldhash = keg->uk_hash; 837099a0e58SBosko Milekic keg->uk_hash = newhash; 8380aef6126SJeff Roberson } else 8390aef6126SJeff Roberson oldhash = newhash; 8400aef6126SJeff Roberson 8418b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 8420aef6126SJeff Roberson hash_free(&oldhash); 8438b987a77SJeff Roberson goto update_wss; 8440aef6126SJeff Roberson } 8455300d9ddSJeff Roberson } 8468b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 847e20a199fSJeff Roberson 84808034d10SKonstantin Belousov update_wss: 84908cfa56eSMark Johnston ZONE_LOCK(zone); 850bb15d1c7SGleb Smirnoff for (int i = 0; i < vm_ndomains; i++) 8510f9b7bf3SMark Johnston zone_domain_update_wss(&zone->uz_domain[i]); 85208cfa56eSMark Johnston ZONE_UNLOCK(zone); 8538355f576SJeff Roberson } 8548355f576SJeff Roberson 8558355f576SJeff Roberson /* 8565300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 8575300d9ddSJeff Roberson * backing store. 8585300d9ddSJeff Roberson * 8595300d9ddSJeff Roberson * Arguments: 8600aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 8615300d9ddSJeff Roberson * 8625300d9ddSJeff Roberson * Returns: 863763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 8645300d9ddSJeff Roberson */ 86537c84183SPoul-Henning Kamp static int 8663b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 8675300d9ddSJeff Roberson { 86859568a0eSAlexander Motin size_t alloc; 8695300d9ddSJeff Roberson 8703b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 8713b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 8723b2f2cb8SAlexander Motin hash->uh_hashsize = size; 8730aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 8741e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 8755300d9ddSJeff Roberson } else { 8760aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 877e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 878ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 8790aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 8805300d9ddSJeff Roberson } 8810aef6126SJeff Roberson if (hash->uh_slab_hash) { 8820aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 8830aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 8840aef6126SJeff Roberson return (1); 8850aef6126SJeff Roberson } 8865300d9ddSJeff Roberson 8870aef6126SJeff Roberson return (0); 8885300d9ddSJeff Roberson } 8895300d9ddSJeff Roberson 8905300d9ddSJeff Roberson /* 89164f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 89264f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 89364f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 8948355f576SJeff Roberson * 8958355f576SJeff Roberson * Arguments: 8960aef6126SJeff Roberson * oldhash The hash you want to expand 8970aef6126SJeff Roberson * newhash The hash structure for the new table 8988355f576SJeff Roberson * 8998355f576SJeff Roberson * Returns: 9008355f576SJeff Roberson * Nothing 9018355f576SJeff Roberson * 9028355f576SJeff Roberson * Discussion: 9038355f576SJeff Roberson */ 9040aef6126SJeff Roberson static int 9050aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 9068355f576SJeff Roberson { 9071e0701e1SJeff Roberson uma_hash_slab_t slab; 9086929b7d1SPedro F. Giffuni u_int hval; 9096929b7d1SPedro F. Giffuni u_int idx; 9108355f576SJeff Roberson 9110aef6126SJeff Roberson if (!newhash->uh_slab_hash) 9120aef6126SJeff Roberson return (0); 9138355f576SJeff Roberson 9140aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 9150aef6126SJeff Roberson return (0); 9168355f576SJeff Roberson 9178355f576SJeff Roberson /* 9188355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 9198355f576SJeff Roberson * full rehash. 9208355f576SJeff Roberson */ 9218355f576SJeff Roberson 9226929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 9231e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 9241e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 9251e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 9261e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 9271e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 9281e0701e1SJeff Roberson slab, uhs_hlink); 9298355f576SJeff Roberson } 9308355f576SJeff Roberson 9310aef6126SJeff Roberson return (1); 9329c2cd7e5SJeff Roberson } 9339c2cd7e5SJeff Roberson 9345300d9ddSJeff Roberson /* 9355300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 9365300d9ddSJeff Roberson * 9375300d9ddSJeff Roberson * Arguments: 9385300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 9395300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 9405300d9ddSJeff Roberson * 9415300d9ddSJeff Roberson * Returns: 9425300d9ddSJeff Roberson * Nothing 9435300d9ddSJeff Roberson */ 9449c2cd7e5SJeff Roberson static void 9450aef6126SJeff Roberson hash_free(struct uma_hash *hash) 9469c2cd7e5SJeff Roberson { 9470aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 9480aef6126SJeff Roberson return; 9490aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 9500095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 9518355f576SJeff Roberson else 952961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 9538355f576SJeff Roberson } 9548355f576SJeff Roberson 9558355f576SJeff Roberson /* 9568355f576SJeff Roberson * Frees all outstanding items in a bucket 9578355f576SJeff Roberson * 9588355f576SJeff Roberson * Arguments: 9598355f576SJeff Roberson * zone The zone to free to, must be unlocked. 9604bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 9618355f576SJeff Roberson * 9628355f576SJeff Roberson * Returns: 9638355f576SJeff Roberson * Nothing 9648355f576SJeff Roberson */ 9658355f576SJeff Roberson 9668355f576SJeff Roberson static void 9678355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 9688355f576SJeff Roberson { 9690095a784SJeff Roberson int i; 9708355f576SJeff Roberson 9714bd61e19SJeff Roberson if (bucket == NULL || bucket->ub_cnt == 0) 9728355f576SJeff Roberson return; 9738355f576SJeff Roberson 974*d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 975*d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 976*d4665eaaSJeff Roberson smr_wait(zone->uz_smr, bucket->ub_seq); 977*d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 978*d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], 979*d4665eaaSJeff Roberson zone->uz_size, NULL, SKIP_NONE); 980*d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 981*d4665eaaSJeff Roberson } 9820095a784SJeff Roberson if (zone->uz_fini) 9830095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 9840095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 9850095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 9864bd61e19SJeff Roberson if (zone->uz_max_items > 0) 9874bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 988*d4665eaaSJeff Roberson #ifdef INVARIANTS 989*d4665eaaSJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt); 990*d4665eaaSJeff Roberson #endif 9910095a784SJeff Roberson bucket->ub_cnt = 0; 9928355f576SJeff Roberson } 9938355f576SJeff Roberson 9948355f576SJeff Roberson /* 9958355f576SJeff Roberson * Drains the per cpu caches for a zone. 9968355f576SJeff Roberson * 997727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 9985d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 9995d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 10005d1ae027SRobert Watson * 10018355f576SJeff Roberson * Arguments: 10028355f576SJeff Roberson * zone The zone to drain, must be unlocked. 10038355f576SJeff Roberson * 10048355f576SJeff Roberson * Returns: 10058355f576SJeff Roberson * Nothing 10068355f576SJeff Roberson */ 10078355f576SJeff Roberson static void 10089643769aSJeff Roberson cache_drain(uma_zone_t zone) 10098355f576SJeff Roberson { 10108355f576SJeff Roberson uma_cache_t cache; 1011376b1ba3SJeff Roberson uma_bucket_t bucket; 10128355f576SJeff Roberson int cpu; 10138355f576SJeff Roberson 10148355f576SJeff Roberson /* 10155d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 10165d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 10175d1ae027SRobert Watson * of the caches at this point. 10185d1ae027SRobert Watson * 10195d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 10205d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 10218355f576SJeff Roberson */ 10223aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 10238355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 1024376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 1025376b1ba3SJeff Roberson if (bucket != NULL) { 1026376b1ba3SJeff Roberson bucket_drain(zone, bucket); 1027376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1028376b1ba3SJeff Roberson } 1029376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(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_cross(cache); 1035376b1ba3SJeff Roberson if (bucket != NULL) { 1036376b1ba3SJeff Roberson bucket_drain(zone, bucket); 1037376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1038376b1ba3SJeff Roberson } 1039d56368d7SBosko Milekic } 104008cfa56eSMark Johnston bucket_cache_reclaim(zone, true); 1041aaa8bb16SJeff Roberson } 1042aaa8bb16SJeff Roberson 1043a2de44abSAlexander Motin static void 104420a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 1045a2de44abSAlexander Motin { 1046a2de44abSAlexander Motin 1047a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1048a2de44abSAlexander Motin return; 1049a2de44abSAlexander Motin 1050a2de44abSAlexander Motin ZONE_LOCK(zone); 105120a4e154SJeff Roberson zone->uz_bucket_size = 105220a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 1053a2de44abSAlexander Motin ZONE_UNLOCK(zone); 1054a2de44abSAlexander Motin } 1055a2de44abSAlexander Motin 1056a2de44abSAlexander Motin static void 105720a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 1058a2de44abSAlexander Motin { 1059a2de44abSAlexander Motin uma_cache_t cache; 1060c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 1061ab3185d1SJeff Roberson int domain; 1062a2de44abSAlexander Motin 1063a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1064a2de44abSAlexander Motin return; 1065a2de44abSAlexander Motin 1066c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1067a2de44abSAlexander Motin ZONE_LOCK(zone); 1068a2de44abSAlexander Motin critical_enter(); 1069dfe13344SJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 1070ab3185d1SJeff Roberson domain = PCPU_GET(domain); 1071ab3185d1SJeff Roberson else 1072ab3185d1SJeff Roberson domain = 0; 1073a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1074376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1075376b1ba3SJeff Roberson if (b1 != NULL && b1->ub_cnt != 0) { 1076376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b1, false); 1077376b1ba3SJeff Roberson b1 = NULL; 1078a2de44abSAlexander Motin } 1079*d4665eaaSJeff Roberson 1080*d4665eaaSJeff Roberson /* 1081*d4665eaaSJeff Roberson * Don't flush SMR zone buckets. This leaves the zone without a 1082*d4665eaaSJeff Roberson * bucket and forces every free to synchronize(). 1083*d4665eaaSJeff Roberson */ 1084*d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 1085*d4665eaaSJeff Roberson goto out; 1086376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1087376b1ba3SJeff Roberson if (b2 != NULL && b2->ub_cnt != 0) { 1088376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b2, false); 1089376b1ba3SJeff Roberson b2 = NULL; 1090a2de44abSAlexander Motin } 1091376b1ba3SJeff Roberson b3 = cache_bucket_unload_cross(cache); 1092*d4665eaaSJeff Roberson 1093*d4665eaaSJeff Roberson out: 1094a2de44abSAlexander Motin critical_exit(); 1095a2de44abSAlexander Motin ZONE_UNLOCK(zone); 10968a8d9d14SAlexander Motin if (b1) 10978a8d9d14SAlexander Motin bucket_free(zone, b1, NULL); 10988a8d9d14SAlexander Motin if (b2) 10998a8d9d14SAlexander Motin bucket_free(zone, b2, NULL); 1100c1685086SJeff Roberson if (b3) { 1101c1685086SJeff Roberson bucket_drain(zone, b3); 1102c1685086SJeff Roberson bucket_free(zone, b3, NULL); 1103c1685086SJeff Roberson } 1104a2de44abSAlexander Motin } 1105a2de44abSAlexander Motin 1106a2de44abSAlexander Motin /* 1107a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1108a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1109a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1110a2de44abSAlexander Motin * to safely access their cache buckets. 1111a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1112a2de44abSAlexander Motin */ 1113a2de44abSAlexander Motin static void 111408cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1115a2de44abSAlexander Motin { 1116a2de44abSAlexander Motin int cpu; 1117a2de44abSAlexander Motin 1118a2de44abSAlexander Motin /* 1119727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1120a2de44abSAlexander Motin */ 1121a2de44abSAlexander Motin if (zone) 112220a4e154SJeff Roberson cache_shrink(zone, NULL); 1123a2de44abSAlexander Motin else 112420a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1125a2de44abSAlexander Motin 1126a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1127a2de44abSAlexander Motin thread_lock(curthread); 1128a2de44abSAlexander Motin sched_bind(curthread, cpu); 1129a2de44abSAlexander Motin thread_unlock(curthread); 1130a2de44abSAlexander Motin 1131a2de44abSAlexander Motin if (zone) 113220a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1133a2de44abSAlexander Motin else 113420a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1135a2de44abSAlexander Motin } 1136a2de44abSAlexander Motin thread_lock(curthread); 1137a2de44abSAlexander Motin sched_unbind(curthread); 1138a2de44abSAlexander Motin thread_unlock(curthread); 1139a2de44abSAlexander Motin } 1140a2de44abSAlexander Motin 1141aaa8bb16SJeff Roberson /* 114208cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 114308cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 114408cfa56eSMark Johnston * estimated working set size. 1145aaa8bb16SJeff Roberson */ 1146aaa8bb16SJeff Roberson static void 114708cfa56eSMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain) 1148aaa8bb16SJeff Roberson { 1149ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1150aaa8bb16SJeff Roberson uma_bucket_t bucket; 115108cfa56eSMark Johnston long target, tofree; 1152ab3185d1SJeff Roberson int i; 11538355f576SJeff Roberson 1154ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 115591d947bfSJeff Roberson /* 115691d947bfSJeff Roberson * The cross bucket is partially filled and not part of 115791d947bfSJeff Roberson * the item count. Reclaim it individually here. 115891d947bfSJeff Roberson */ 1159ab3185d1SJeff Roberson zdom = &zone->uz_domain[i]; 116091d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 116191d947bfSJeff Roberson bucket = zdom->uzd_cross; 116291d947bfSJeff Roberson zdom->uzd_cross = NULL; 116391d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 116491d947bfSJeff Roberson if (bucket != NULL) { 116591d947bfSJeff Roberson bucket_drain(zone, bucket); 116691d947bfSJeff Roberson bucket_free(zone, bucket, NULL); 116791d947bfSJeff Roberson } 116891d947bfSJeff Roberson 116991d947bfSJeff Roberson /* 117091d947bfSJeff Roberson * Shrink the zone bucket size to ensure that the per-CPU caches 117191d947bfSJeff Roberson * don't grow too large. 117291d947bfSJeff Roberson */ 117391d947bfSJeff Roberson ZONE_LOCK(zone); 117491d947bfSJeff Roberson if (i == 0 && zone->uz_bucket_size > zone->uz_bucket_size_min) 117591d947bfSJeff Roberson zone->uz_bucket_size--; 117608cfa56eSMark Johnston 117708cfa56eSMark Johnston /* 117808cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 117908cfa56eSMark Johnston * this bucket cache is empty. Otherwise, we reclaim items in 118008cfa56eSMark Johnston * excess of the zone's estimated working set size. If the 118108cfa56eSMark Johnston * difference nitems - imin is larger than the WSS estimate, 118208cfa56eSMark Johnston * then the estimate will grow at the end of this interval and 118308cfa56eSMark Johnston * we ignore the historical average. 118408cfa56eSMark Johnston */ 118508cfa56eSMark Johnston target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems - 118608cfa56eSMark Johnston zdom->uzd_imin); 118708cfa56eSMark Johnston while (zdom->uzd_nitems > target) { 1188*d4665eaaSJeff Roberson bucket = TAILQ_FIRST(&zdom->uzd_buckets); 118908cfa56eSMark Johnston if (bucket == NULL) 119008cfa56eSMark Johnston break; 119108cfa56eSMark Johnston tofree = bucket->ub_cnt; 119208cfa56eSMark Johnston TAILQ_REMOVE(&zdom->uzd_buckets, bucket, ub_link); 119308cfa56eSMark Johnston zdom->uzd_nitems -= tofree; 119408cfa56eSMark Johnston 119508cfa56eSMark Johnston /* 119608cfa56eSMark Johnston * Shift the bounds of the current WSS interval to avoid 119708cfa56eSMark Johnston * perturbing the estimate. 119808cfa56eSMark Johnston */ 119908cfa56eSMark Johnston zdom->uzd_imax -= lmin(zdom->uzd_imax, tofree); 120008cfa56eSMark Johnston zdom->uzd_imin -= lmin(zdom->uzd_imin, tofree); 120108cfa56eSMark Johnston 12028355f576SJeff Roberson ZONE_UNLOCK(zone); 12038355f576SJeff Roberson bucket_drain(zone, bucket); 12046fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 12058355f576SJeff Roberson ZONE_LOCK(zone); 12068355f576SJeff Roberson } 120791d947bfSJeff Roberson ZONE_UNLOCK(zone); 1208ab3185d1SJeff Roberson } 12098355f576SJeff Roberson } 1210fc03d22bSJeff Roberson 1211fc03d22bSJeff Roberson static void 1212fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1213fc03d22bSJeff Roberson { 1214fc03d22bSJeff Roberson uint8_t *mem; 1215fc03d22bSJeff Roberson int i; 1216fc03d22bSJeff Roberson uint8_t flags; 1217fc03d22bSJeff Roberson 12181431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 12191431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 12201431a748SGleb Smirnoff 12211e0701e1SJeff Roberson mem = slab_data(slab, keg); 1222fc03d22bSJeff Roberson flags = slab->us_flags; 1223fc03d22bSJeff Roberson i = start; 1224fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 1225fc03d22bSJeff Roberson for (i--; i > -1; i--) 1226c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1227c5deaf04SGleb Smirnoff /* 1228c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1229c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1230c5deaf04SGleb Smirnoff * which executed trash_dtor. 1231c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1232c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1233c5deaf04SGleb Smirnoff * invocations. 1234c5deaf04SGleb Smirnoff */ 12351e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1236c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1237c5deaf04SGleb Smirnoff #endif 12381e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1239fc03d22bSJeff Roberson } 124054c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 12419b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab), 12429b8db4d0SRyan Libby NULL, SKIP_NONE); 1243fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 12442e47807cSJeff Roberson uma_total_dec(PAGE_SIZE * keg->uk_ppera); 12458355f576SJeff Roberson } 12468355f576SJeff Roberson 12478355f576SJeff Roberson /* 1248e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 12498355f576SJeff Roberson * the pageout daemon. 12508355f576SJeff Roberson * 1251e20a199fSJeff Roberson * Returns nothing. 12528355f576SJeff Roberson */ 1253e20a199fSJeff Roberson static void 1254e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 12558355f576SJeff Roberson { 12561e183df2SStefan Farfeleder struct slabhead freeslabs = { 0 }; 1257ab3185d1SJeff Roberson uma_domain_t dom; 1258829be516SMark Johnston uma_slab_t slab, tmp; 12598b987a77SJeff Roberson int i, n; 12608355f576SJeff Roberson 12618355f576SJeff Roberson /* 1262e20a199fSJeff Roberson * We don't want to take pages from statically allocated kegs at this 12638355f576SJeff Roberson * time 12648355f576SJeff Roberson */ 1265099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 12668355f576SJeff Roberson return; 12678355f576SJeff Roberson 1268ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 12698b987a77SJeff Roberson CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u", 12708b987a77SJeff Roberson keg->uk_name, keg, i, dom->ud_free); 12718b987a77SJeff Roberson n = 0; 1272ab3185d1SJeff Roberson dom = &keg->uk_domain[i]; 12738b987a77SJeff Roberson KEG_LOCK(keg, i); 1274ab3185d1SJeff Roberson LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) { 127554c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 12761e0701e1SJeff Roberson UMA_HASH_REMOVE(&keg->uk_hash, slab); 12778b987a77SJeff Roberson n++; 12788b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 12791e0701e1SJeff Roberson LIST_INSERT_HEAD(&freeslabs, slab, us_link); 1280713deb36SJeff Roberson } 12818b987a77SJeff Roberson dom->ud_pages -= n * keg->uk_ppera; 12828b987a77SJeff Roberson dom->ud_free -= n * keg->uk_ipers; 12838b987a77SJeff Roberson KEG_UNLOCK(keg, i); 1284ab3185d1SJeff Roberson } 1285ab3185d1SJeff Roberson 12861e0701e1SJeff Roberson while ((slab = LIST_FIRST(&freeslabs)) != NULL) { 12871e0701e1SJeff Roberson LIST_REMOVE(slab, us_link); 12881645995bSKirk McKusick keg_free_slab(keg, slab, keg->uk_ipers); 12898355f576SJeff Roberson } 12908355f576SJeff Roberson } 12918355f576SJeff Roberson 1292e20a199fSJeff Roberson static void 129308cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain) 1294e20a199fSJeff Roberson { 1295e20a199fSJeff Roberson 12968355f576SJeff Roberson /* 1297e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 1298e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 1299e20a199fSJeff Roberson * is the only call that knows the structure will still be available 1300e20a199fSJeff Roberson * when it wakes up. 1301e20a199fSJeff Roberson */ 1302e20a199fSJeff Roberson ZONE_LOCK(zone); 130308cfa56eSMark Johnston while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) { 1304e20a199fSJeff Roberson if (waitok == M_NOWAIT) 1305e20a199fSJeff Roberson goto out; 1306727c6918SJeff Roberson msleep(zone, &zone->uz_lock, PVM, "zonedrain", 1); 1307e20a199fSJeff Roberson } 130808cfa56eSMark Johnston zone->uz_flags |= UMA_ZFLAG_RECLAIMING; 1309e20a199fSJeff Roberson ZONE_UNLOCK(zone); 131091d947bfSJeff Roberson bucket_cache_reclaim(zone, drain); 131108cfa56eSMark Johnston 1312e20a199fSJeff Roberson /* 1313e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 1314111fbcd5SBryan Venteicher * we're running. Normally the uma_rwlock would protect us but we 1315e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 1316e20a199fSJeff Roberson */ 131708034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1318bb15d1c7SGleb Smirnoff keg_drain(zone->uz_keg); 1319e20a199fSJeff Roberson ZONE_LOCK(zone); 132008cfa56eSMark Johnston zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING; 1321e20a199fSJeff Roberson wakeup(zone); 1322e20a199fSJeff Roberson out: 1323e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1324e20a199fSJeff Roberson } 1325e20a199fSJeff Roberson 132608cfa56eSMark Johnston static void 132720a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused) 1328e20a199fSJeff Roberson { 1329e20a199fSJeff Roberson 133008cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, true); 133108cfa56eSMark Johnston } 133208cfa56eSMark Johnston 133308cfa56eSMark Johnston static void 133420a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused) 133508cfa56eSMark Johnston { 133608cfa56eSMark Johnston 133708cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, false); 1338e20a199fSJeff Roberson } 1339e20a199fSJeff Roberson 1340e20a199fSJeff Roberson /* 13418b987a77SJeff Roberson * Allocate a new slab for a keg and inserts it into the partial slab list. 13428b987a77SJeff Roberson * The keg should be unlocked on entry. If the allocation succeeds it will 13438b987a77SJeff Roberson * be locked on return. 13448355f576SJeff Roberson * 13458355f576SJeff Roberson * Arguments: 134686220393SMark Johnston * flags Wait flags for the item initialization routine 134786220393SMark Johnston * aflags Wait flags for the slab allocation 13488355f576SJeff Roberson * 13498355f576SJeff Roberson * Returns: 13508355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 13518355f576SJeff Roberson * caller specified M_NOWAIT. 13528355f576SJeff Roberson */ 13538355f576SJeff Roberson static uma_slab_t 135486220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 135586220393SMark Johnston int aflags) 13568355f576SJeff Roberson { 13578b987a77SJeff Roberson uma_domain_t dom; 1358e20a199fSJeff Roberson uma_alloc allocf; 1359099a0e58SBosko Milekic uma_slab_t slab; 13602e47807cSJeff Roberson unsigned long size; 136185dcf349SGleb Smirnoff uint8_t *mem; 136286220393SMark Johnston uint8_t sflags; 13638355f576SJeff Roberson int i; 13648355f576SJeff Roberson 1365ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1366ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1367a553d4b8SJeff Roberson 13688b987a77SJeff Roberson allocf = keg->uk_allocf; 1369194a979eSMark Johnston slab = NULL; 1370194a979eSMark Johnston mem = NULL; 137154c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 13729b8db4d0SRyan Libby uma_hash_slab_t hslab; 13739b8db4d0SRyan Libby hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL, 13749b8db4d0SRyan Libby domain, aflags); 13759b8db4d0SRyan Libby if (hslab == NULL) 1376727c6918SJeff Roberson goto fail; 13779b8db4d0SRyan Libby slab = &hslab->uhs_slab; 1378a553d4b8SJeff Roberson } 1379a553d4b8SJeff Roberson 13803370c5bfSJeff Roberson /* 13813370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 13823370c5bfSJeff Roberson * first time they are added to a zone. 13833370c5bfSJeff Roberson * 13843370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 13853370c5bfSJeff Roberson */ 13863370c5bfSJeff Roberson 1387099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 138886220393SMark Johnston aflags |= M_ZERO; 13893370c5bfSJeff Roberson else 139086220393SMark Johnston aflags &= ~M_ZERO; 13913370c5bfSJeff Roberson 1392263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 139386220393SMark Johnston aflags |= M_NODUMP; 1394263811f7SKip Macy 1395e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1396194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 139786220393SMark Johnston mem = allocf(zone, size, domain, &sflags, aflags); 1398a553d4b8SJeff Roberson if (mem == NULL) { 139954c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 14009b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), 14019b8db4d0SRyan Libby slab_tohashslab(slab), NULL, SKIP_NONE); 1402727c6918SJeff Roberson goto fail; 1403a553d4b8SJeff Roberson } 14042e47807cSJeff Roberson uma_total_inc(size); 14058355f576SJeff Roberson 14068b987a77SJeff Roberson /* For HASH zones all pages go to the same uma_domain. */ 140754c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 14088b987a77SJeff Roberson domain = 0; 14098b987a77SJeff Roberson 14105c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 141154c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) 1412099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 14131e0701e1SJeff Roberson else 14149b8db4d0SRyan Libby slab_tohashslab(slab)->uhs_data = mem; 14155c0e403bSJeff Roberson 141654c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_VTOSLAB) 1417099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1418584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1419584061b4SJeff Roberson zone, slab); 14208355f576SJeff Roberson 1421099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 142286220393SMark Johnston slab->us_flags = sflags; 1423ab3185d1SJeff Roberson slab->us_domain = domain; 14248b987a77SJeff Roberson 14259b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1426ef72505eSJeff Roberson #ifdef INVARIANTS 1427815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1428ef72505eSJeff Roberson #endif 1429099a0e58SBosko Milekic 1430b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1431099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 14321e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 143386220393SMark Johnston keg->uk_size, flags) != 0) 1434b23f72e9SBrian Feldman break; 1435b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1436fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1437727c6918SJeff Roberson goto fail; 1438b23f72e9SBrian Feldman } 1439b23f72e9SBrian Feldman } 14408b987a77SJeff Roberson KEG_LOCK(keg, domain); 14415c0e403bSJeff Roberson 14421431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 14431431a748SGleb Smirnoff slab, keg->uk_name, keg); 14441431a748SGleb Smirnoff 144554c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 1446099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 14478355f576SJeff Roberson 14488b987a77SJeff Roberson /* 14498b987a77SJeff Roberson * If we got a slab here it's safe to mark it partially used 14508b987a77SJeff Roberson * and return. We assume that the caller is going to remove 14518b987a77SJeff Roberson * at least one item. 14528b987a77SJeff Roberson */ 14538b987a77SJeff Roberson dom = &keg->uk_domain[domain]; 14548b987a77SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 14558b987a77SJeff Roberson dom->ud_pages += keg->uk_ppera; 14568b987a77SJeff Roberson dom->ud_free += keg->uk_ipers; 14578355f576SJeff Roberson 14588355f576SJeff Roberson return (slab); 1459727c6918SJeff Roberson 1460727c6918SJeff Roberson fail: 1461727c6918SJeff Roberson return (NULL); 14628355f576SJeff Roberson } 14638355f576SJeff Roberson 14648355f576SJeff Roberson /* 1465009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1466009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1467009b6fcbSJeff Roberson * the VM is ready. 1468009b6fcbSJeff Roberson */ 1469009b6fcbSJeff Roberson static void * 1470ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1471ab3185d1SJeff Roberson int wait) 1472009b6fcbSJeff Roberson { 1473a81c400eSJeff Roberson vm_paddr_t pa; 1474a81c400eSJeff Roberson vm_page_t m; 1475ac0a6fd0SGleb Smirnoff void *mem; 1476ac0a6fd0SGleb Smirnoff int pages; 1477a81c400eSJeff Roberson int i; 1478099a0e58SBosko Milekic 1479f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1480f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1481a81c400eSJeff Roberson 1482f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1483a81c400eSJeff Roberson m = vm_page_alloc_contig_domain(NULL, 0, domain, 1484a81c400eSJeff Roberson malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages, 1485a81c400eSJeff Roberson (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT); 1486a81c400eSJeff Roberson if (m == NULL) 1487a81c400eSJeff Roberson return (NULL); 1488a81c400eSJeff Roberson 1489a81c400eSJeff Roberson pa = VM_PAGE_TO_PHYS(m); 1490a81c400eSJeff Roberson for (i = 0; i < pages; i++, pa += PAGE_SIZE) { 1491a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1492a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1493a81c400eSJeff Roberson if ((wait & M_NODUMP) == 0) 1494a81c400eSJeff Roberson dump_add_page(pa); 1495a81c400eSJeff Roberson #endif 1496a81c400eSJeff Roberson } 1497a81c400eSJeff Roberson /* Allocate KVA and indirectly advance bootmem. */ 1498a81c400eSJeff Roberson mem = (void *)pmap_map(&bootmem, m->phys_addr, 1499a81c400eSJeff Roberson m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE); 1500a81c400eSJeff Roberson if ((wait & M_ZERO) != 0) 1501a81c400eSJeff Roberson bzero(mem, pages * PAGE_SIZE); 1502f7d35785SGleb Smirnoff 1503f7d35785SGleb Smirnoff return (mem); 1504f7d35785SGleb Smirnoff } 1505f7d35785SGleb Smirnoff 1506a81c400eSJeff Roberson static void 1507a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes) 1508a81c400eSJeff Roberson { 1509a81c400eSJeff Roberson vm_offset_t va; 1510a81c400eSJeff Roberson vm_page_t m; 1511a81c400eSJeff Roberson 1512a81c400eSJeff Roberson va = (vm_offset_t)mem; 1513a81c400eSJeff Roberson m = PHYS_TO_VM_PAGE(pmap_kextract(va)); 1514a81c400eSJeff Roberson pmap_remove(kernel_pmap, va, va + bytes); 1515a81c400eSJeff Roberson for (; bytes != 0; bytes -= PAGE_SIZE, m++) { 1516a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1517a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1518a81c400eSJeff Roberson dump_drop_page(VM_PAGE_TO_PHYS(m)); 1519a81c400eSJeff Roberson #endif 1520a81c400eSJeff Roberson vm_page_unwire_noq(m); 1521a81c400eSJeff Roberson vm_page_free(m); 1522a81c400eSJeff Roberson } 1523a81c400eSJeff Roberson } 1524a81c400eSJeff Roberson 1525f7d35785SGleb Smirnoff /* 15268355f576SJeff Roberson * Allocates a number of pages from the system 15278355f576SJeff Roberson * 15288355f576SJeff Roberson * Arguments: 15298355f576SJeff Roberson * bytes The number of bytes requested 15308355f576SJeff Roberson * wait Shall we wait? 15318355f576SJeff Roberson * 15328355f576SJeff Roberson * Returns: 15338355f576SJeff Roberson * A pointer to the alloced memory or possibly 15348355f576SJeff Roberson * NULL if M_NOWAIT is set. 15358355f576SJeff Roberson */ 15368355f576SJeff Roberson static void * 1537ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1538ab3185d1SJeff Roberson int wait) 15398355f576SJeff Roberson { 15408355f576SJeff Roberson void *p; /* Returned page */ 15418355f576SJeff Roberson 15422e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 15439978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 15448355f576SJeff Roberson 15458355f576SJeff Roberson return (p); 15468355f576SJeff Roberson } 15478355f576SJeff Roberson 1548ab3059a8SMatt Macy static void * 1549ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1550ab3059a8SMatt Macy int wait) 1551ab3059a8SMatt Macy { 1552ab3059a8SMatt Macy struct pglist alloctail; 1553ab3059a8SMatt Macy vm_offset_t addr, zkva; 1554ab3059a8SMatt Macy int cpu, flags; 1555ab3059a8SMatt Macy vm_page_t p, p_next; 1556ab3059a8SMatt Macy #ifdef NUMA 1557ab3059a8SMatt Macy struct pcpu *pc; 1558ab3059a8SMatt Macy #endif 1559ab3059a8SMatt Macy 1560ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1561ab3059a8SMatt Macy 1562013072f0SMark Johnston TAILQ_INIT(&alloctail); 1563ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1564013072f0SMark Johnston malloc2vm_flags(wait); 1565013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1566ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1567ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1568ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1569ab3059a8SMatt Macy } else { 1570ab3059a8SMatt Macy #ifndef NUMA 1571ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1572ab3059a8SMatt Macy #else 1573ab3059a8SMatt Macy pc = pcpu_find(cpu); 157420526802SAndrew Gallatin if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain))) 157520526802SAndrew Gallatin p = NULL; 157620526802SAndrew Gallatin else 157720526802SAndrew Gallatin p = vm_page_alloc_domain(NULL, 0, 157820526802SAndrew Gallatin pc->pc_domain, flags); 1579ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1580ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1581ab3059a8SMatt Macy #endif 1582ab3059a8SMatt Macy } 1583ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1584ab3059a8SMatt Macy goto fail; 1585ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1586ab3059a8SMatt Macy } 1587ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1588ab3059a8SMatt Macy goto fail; 1589ab3059a8SMatt Macy zkva = addr; 1590ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1591ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1592ab3059a8SMatt Macy zkva += PAGE_SIZE; 1593ab3059a8SMatt Macy } 1594ab3059a8SMatt Macy return ((void*)addr); 1595ab3059a8SMatt Macy fail: 1596ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 159788ea538aSMark Johnston vm_page_unwire_noq(p); 1598ab3059a8SMatt Macy vm_page_free(p); 1599ab3059a8SMatt Macy } 1600ab3059a8SMatt Macy return (NULL); 1601ab3059a8SMatt Macy } 1602ab3059a8SMatt Macy 16038355f576SJeff Roberson /* 16048355f576SJeff Roberson * Allocates a number of pages from within an object 16058355f576SJeff Roberson * 16068355f576SJeff Roberson * Arguments: 16078355f576SJeff Roberson * bytes The number of bytes requested 16088355f576SJeff Roberson * wait Shall we wait? 16098355f576SJeff Roberson * 16108355f576SJeff Roberson * Returns: 16118355f576SJeff Roberson * A pointer to the alloced memory or possibly 16128355f576SJeff Roberson * NULL if M_NOWAIT is set. 16138355f576SJeff Roberson */ 16148355f576SJeff Roberson static void * 1615ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1616ab3185d1SJeff Roberson int wait) 16178355f576SJeff Roberson { 1618a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1619a4915c21SAttilio Rao u_long npages; 1620b245ac95SAlan Cox vm_offset_t retkva, zkva; 1621a4915c21SAttilio Rao vm_page_t p, p_next; 1622e20a199fSJeff Roberson uma_keg_t keg; 16238355f576SJeff Roberson 1624a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1625bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1626a4915c21SAttilio Rao 1627a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1628a4915c21SAttilio Rao while (npages > 0) { 1629ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 16308d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1631772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1632772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1633a4915c21SAttilio Rao if (p != NULL) { 1634a4915c21SAttilio Rao /* 1635a4915c21SAttilio Rao * Since the page does not belong to an object, its 1636a4915c21SAttilio Rao * listq is unused. 1637a4915c21SAttilio Rao */ 1638a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1639a4915c21SAttilio Rao npages--; 1640a4915c21SAttilio Rao continue; 1641a4915c21SAttilio Rao } 16428355f576SJeff Roberson /* 1643a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1644a4915c21SAttilio Rao * exit. 16458355f576SJeff Roberson */ 1646a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 164788ea538aSMark Johnston vm_page_unwire_noq(p); 1648b245ac95SAlan Cox vm_page_free(p); 1649b245ac95SAlan Cox } 1650a4915c21SAttilio Rao return (NULL); 1651b245ac95SAlan Cox } 16528355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1653a4915c21SAttilio Rao zkva = keg->uk_kva + 1654a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1655a4915c21SAttilio Rao retkva = zkva; 1656a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1657a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1658a4915c21SAttilio Rao zkva += PAGE_SIZE; 1659a4915c21SAttilio Rao } 16608355f576SJeff Roberson 16618355f576SJeff Roberson return ((void *)retkva); 16628355f576SJeff Roberson } 16638355f576SJeff Roberson 16648355f576SJeff Roberson /* 16658355f576SJeff Roberson * Frees a number of pages to the system 16668355f576SJeff Roberson * 16678355f576SJeff Roberson * Arguments: 16688355f576SJeff Roberson * mem A pointer to the memory to be freed 16698355f576SJeff Roberson * size The size of the memory being freed 16708355f576SJeff Roberson * flags The original p->us_flags field 16718355f576SJeff Roberson * 16728355f576SJeff Roberson * Returns: 16738355f576SJeff Roberson * Nothing 16748355f576SJeff Roberson */ 16758355f576SJeff Roberson static void 1676f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 16778355f576SJeff Roberson { 16783370c5bfSJeff Roberson 1679a81c400eSJeff Roberson if ((flags & UMA_SLAB_BOOT) != 0) { 1680a81c400eSJeff Roberson startup_free(mem, size); 1681a81c400eSJeff Roberson return; 1682a81c400eSJeff Roberson } 1683a81c400eSJeff Roberson 168449bfa624SAlan Cox if ((flags & UMA_SLAB_KERNEL) == 0) 1685b5345ef1SJustin Hibbits panic("UMA: page_free used with invalid flags %x", flags); 16868355f576SJeff Roberson 168749bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 16888355f576SJeff Roberson } 16898355f576SJeff Roberson 16908355f576SJeff Roberson /* 1691ab3059a8SMatt Macy * Frees pcpu zone allocations 1692ab3059a8SMatt Macy * 1693ab3059a8SMatt Macy * Arguments: 1694ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1695ab3059a8SMatt Macy * size The size of the memory being freed 1696ab3059a8SMatt Macy * flags The original p->us_flags field 1697ab3059a8SMatt Macy * 1698ab3059a8SMatt Macy * Returns: 1699ab3059a8SMatt Macy * Nothing 1700ab3059a8SMatt Macy */ 1701ab3059a8SMatt Macy static void 1702ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1703ab3059a8SMatt Macy { 1704ab3059a8SMatt Macy vm_offset_t sva, curva; 1705ab3059a8SMatt Macy vm_paddr_t paddr; 1706ab3059a8SMatt Macy vm_page_t m; 1707ab3059a8SMatt Macy 1708ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 1709ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1710ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1711ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1712ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 171388ea538aSMark Johnston vm_page_unwire_noq(m); 1714ab3059a8SMatt Macy vm_page_free(m); 1715ab3059a8SMatt Macy } 1716ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1717ab3059a8SMatt Macy kva_free(sva, size); 1718ab3059a8SMatt Macy } 1719ab3059a8SMatt Macy 1720ab3059a8SMatt Macy 1721ab3059a8SMatt Macy /* 17228355f576SJeff Roberson * Zero fill initializer 17238355f576SJeff Roberson * 17248355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 17258355f576SJeff Roberson */ 1726b23f72e9SBrian Feldman static int 1727b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 17288355f576SJeff Roberson { 17298355f576SJeff Roberson bzero(mem, size); 1730b23f72e9SBrian Feldman return (0); 17318355f576SJeff Roberson } 17328355f576SJeff Roberson 1733815db204SRyan Libby #ifdef INVARIANTS 1734815db204SRyan Libby struct noslabbits * 1735815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1736815db204SRyan Libby { 1737815db204SRyan Libby 1738815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1739815db204SRyan Libby } 1740815db204SRyan Libby #endif 1741815db204SRyan Libby 17428355f576SJeff Roberson /* 17439b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 17449b78b1f4SJeff Roberson */ 17459b78b1f4SJeff Roberson size_t 17469b78b1f4SJeff Roberson slab_sizeof(int nitems) 17479b78b1f4SJeff Roberson { 17489b78b1f4SJeff Roberson size_t s; 17499b78b1f4SJeff Roberson 1750815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 17519b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 17529b78b1f4SJeff Roberson } 17539b78b1f4SJeff Roberson 17549b78b1f4SJeff Roberson /* 17559b78b1f4SJeff Roberson * Size of memory for embedded slabs (!OFFPAGE). 17569b78b1f4SJeff Roberson */ 17579b78b1f4SJeff Roberson size_t 17589b78b1f4SJeff Roberson slab_space(int nitems) 17599b78b1f4SJeff Roberson { 17609b78b1f4SJeff Roberson return (UMA_SLAB_SIZE - slab_sizeof(nitems)); 17619b78b1f4SJeff Roberson } 17629b78b1f4SJeff Roberson 17634a8b575cSRyan Libby #define UMA_FIXPT_SHIFT 31 17644a8b575cSRyan Libby #define UMA_FRAC_FIXPT(n, d) \ 17654a8b575cSRyan Libby ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d))) 17664a8b575cSRyan Libby #define UMA_FIXPT_PCT(f) \ 17674a8b575cSRyan Libby ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT)) 17684a8b575cSRyan Libby #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100) 17694a8b575cSRyan Libby #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE) 17704a8b575cSRyan Libby 17719b78b1f4SJeff Roberson /* 17724a8b575cSRyan Libby * Compute the number of items that will fit in a slab. If hdr is true, the 17734a8b575cSRyan Libby * item count may be limited to provide space in the slab for an inline slab 17744a8b575cSRyan Libby * header. Otherwise, all slab space will be provided for item storage. 17754a8b575cSRyan Libby */ 17764a8b575cSRyan Libby static u_int 17774a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr) 17784a8b575cSRyan Libby { 17794a8b575cSRyan Libby u_int ipers; 17804a8b575cSRyan Libby u_int padpi; 17814a8b575cSRyan Libby 17824a8b575cSRyan Libby /* The padding between items is not needed after the last item. */ 17834a8b575cSRyan Libby padpi = rsize - size; 17844a8b575cSRyan Libby 17854a8b575cSRyan Libby if (hdr) { 17864a8b575cSRyan Libby /* 17874a8b575cSRyan Libby * Start with the maximum item count and remove items until 17884a8b575cSRyan Libby * the slab header first alongside the allocatable memory. 17894a8b575cSRyan Libby */ 17904a8b575cSRyan Libby for (ipers = MIN(SLAB_MAX_SETSIZE, 17914a8b575cSRyan Libby (slabsize + padpi - slab_sizeof(1)) / rsize); 17924a8b575cSRyan Libby ipers > 0 && 17934a8b575cSRyan Libby ipers * rsize - padpi + slab_sizeof(ipers) > slabsize; 17944a8b575cSRyan Libby ipers--) 17954a8b575cSRyan Libby continue; 17964a8b575cSRyan Libby } else { 17974a8b575cSRyan Libby ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE); 17984a8b575cSRyan Libby } 17994a8b575cSRyan Libby 18004a8b575cSRyan Libby return (ipers); 18014a8b575cSRyan Libby } 18024a8b575cSRyan Libby 18034a8b575cSRyan Libby /* 18044a8b575cSRyan Libby * Compute the number of items that will fit in a slab for a startup zone. 18059b78b1f4SJeff Roberson */ 18069b78b1f4SJeff Roberson int 18079b78b1f4SJeff Roberson slab_ipers(size_t size, int align) 18089b78b1f4SJeff Roberson { 18099b78b1f4SJeff Roberson int rsize; 18109b78b1f4SJeff Roberson 18114a8b575cSRyan Libby rsize = roundup(size, align + 1); /* Assume no CACHESPREAD */ 18124a8b575cSRyan Libby return (slab_ipers_hdr(size, rsize, UMA_SLAB_SIZE, true)); 18139b78b1f4SJeff Roberson } 18149b78b1f4SJeff Roberson 18159b78b1f4SJeff Roberson /* 18164a8b575cSRyan Libby * Determine the format of a uma keg. This determines where the slab header 18174a8b575cSRyan Libby * will be placed (inline or offpage) and calculates ipers, rsize, and ppera. 18188355f576SJeff Roberson * 18198355f576SJeff Roberson * Arguments 1820e20a199fSJeff Roberson * keg The zone we should initialize 18218355f576SJeff Roberson * 18228355f576SJeff Roberson * Returns 18238355f576SJeff Roberson * Nothing 18248355f576SJeff Roberson */ 18258355f576SJeff Roberson static void 18264a8b575cSRyan Libby keg_layout(uma_keg_t keg) 18278355f576SJeff Roberson { 18284a8b575cSRyan Libby u_int alignsize; 18294a8b575cSRyan Libby u_int eff; 18304a8b575cSRyan Libby u_int eff_offpage; 18314a8b575cSRyan Libby u_int format; 18324a8b575cSRyan Libby u_int ipers; 18334a8b575cSRyan Libby u_int ipers_offpage; 18344a8b575cSRyan Libby u_int pages; 1835244f4554SBosko Milekic u_int rsize; 1836a55ebb7cSAndriy Gapon u_int slabsize; 18378355f576SJeff Roberson 18384a8b575cSRyan Libby KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 18394a8b575cSRyan Libby (keg->uk_size <= UMA_PCPU_ALLOC_SIZE && 18404a8b575cSRyan Libby (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0), 18414a8b575cSRyan Libby ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b", 18424a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_size, keg->uk_flags, 18434a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 18444a8b575cSRyan Libby KASSERT((keg->uk_flags & 18454a8b575cSRyan Libby (UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY)) == 0 || 18464a8b575cSRyan Libby (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0, 18474a8b575cSRyan Libby ("%s: incompatible flags 0x%b", __func__, keg->uk_flags, 18484a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 1849e28a647dSGleb Smirnoff 18504a8b575cSRyan Libby alignsize = keg->uk_align + 1; 18514a8b575cSRyan Libby format = 0; 18524a8b575cSRyan Libby ipers = 0; 1853ad97af7eSGleb Smirnoff 1854ef72505eSJeff Roberson /* 1855ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1856ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1857ef72505eSJeff Roberson * allocation bits for we round it up. 1858ef72505eSJeff Roberson */ 18599b8db4d0SRyan Libby rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT); 18604a8b575cSRyan Libby rsize = roundup2(rsize, alignsize); 1861ad97af7eSGleb Smirnoff 18624a8b575cSRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) { 18634a8b575cSRyan Libby slabsize = UMA_PCPU_ALLOC_SIZE; 18644a8b575cSRyan Libby pages = mp_maxid + 1; 18654a8b575cSRyan Libby } else if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) { 18669b78b1f4SJeff Roberson /* 18674a8b575cSRyan Libby * We want one item to start on every align boundary in a page. 18684a8b575cSRyan Libby * To do this we will span pages. We will also extend the item 18694a8b575cSRyan Libby * by the size of align if it is an even multiple of align. 18704a8b575cSRyan Libby * Otherwise, it would fall on the same boundary every time. 18719b78b1f4SJeff Roberson */ 18724a8b575cSRyan Libby if ((rsize & alignsize) == 0) 18734a8b575cSRyan Libby rsize += alignsize; 18744a8b575cSRyan Libby slabsize = rsize * (PAGE_SIZE / alignsize); 18754a8b575cSRyan Libby slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE); 18764a8b575cSRyan Libby slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE); 18774a8b575cSRyan Libby pages = howmany(slabsize, PAGE_SIZE); 18784a8b575cSRyan Libby slabsize = ptoa(pages); 18794a8b575cSRyan Libby } else { 18804a8b575cSRyan Libby /* 18814a8b575cSRyan Libby * Choose a slab size of as many pages as it takes to represent 18824a8b575cSRyan Libby * a single item. We will then try to fit as many additional 18834a8b575cSRyan Libby * items into the slab as possible. At some point, we may want 18844a8b575cSRyan Libby * to increase the slab size for awkward item sizes in order to 18854a8b575cSRyan Libby * increase efficiency. 18864a8b575cSRyan Libby */ 18874a8b575cSRyan Libby pages = howmany(keg->uk_size, PAGE_SIZE); 18884a8b575cSRyan Libby slabsize = ptoa(pages); 18891ca6ed45SGleb Smirnoff } 1890ad97af7eSGleb Smirnoff 18914a8b575cSRyan Libby /* Evaluate an inline slab layout. */ 18924a8b575cSRyan Libby if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0) 18934a8b575cSRyan Libby ipers = slab_ipers_hdr(keg->uk_size, rsize, slabsize, true); 18944a8b575cSRyan Libby 18954a8b575cSRyan Libby /* TODO: vm_page-embedded slab. */ 1896244f4554SBosko Milekic 189720e8e865SBosko Milekic /* 1898244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 189920e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 19006fd34d6fSJeff Roberson * may end up going to the VM for slabs which we do not 19016fd34d6fSJeff Roberson * want to do if we're UMA_ZFLAG_CACHEONLY as a result 19026fd34d6fSJeff Roberson * of UMA_ZONE_VM, which clearly forbids it. 190320e8e865SBosko Milekic */ 19044a8b575cSRyan Libby if ((keg->uk_flags & 19054a8b575cSRyan Libby (UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY)) != 0) { 19064a8b575cSRyan Libby if (ipers == 0) { 19074a8b575cSRyan Libby /* We need an extra page for the slab header. */ 19084a8b575cSRyan Libby pages++; 19094a8b575cSRyan Libby slabsize = ptoa(pages); 19104a8b575cSRyan Libby ipers = slab_ipers_hdr(keg->uk_size, rsize, slabsize, 19114a8b575cSRyan Libby true); 19124a8b575cSRyan Libby } 19134a8b575cSRyan Libby goto out; 191454c5ae80SRyan Libby } 1915244f4554SBosko Milekic 1916ef72505eSJeff Roberson /* 19174a8b575cSRyan Libby * See if using an OFFPAGE slab will improve our efficiency. 19184a8b575cSRyan Libby * Only do this if we are below our efficiency threshold. 1919ef72505eSJeff Roberson * 1920ef72505eSJeff Roberson * XXX We could try growing slabsize to limit max waste as well. 1921ef72505eSJeff Roberson * Historically this was not done because the VM could not 1922ef72505eSJeff Roberson * efficiently handle contiguous allocations. 1923ef72505eSJeff Roberson */ 19244a8b575cSRyan Libby eff = UMA_FRAC_FIXPT(ipers * rsize, slabsize); 19254a8b575cSRyan Libby ipers_offpage = slab_ipers_hdr(keg->uk_size, rsize, slabsize, false); 19264a8b575cSRyan Libby eff_offpage = UMA_FRAC_FIXPT(ipers_offpage * rsize, 19279b8db4d0SRyan Libby slabsize + slabzone(ipers_offpage)->uz_keg->uk_rsize); 19284a8b575cSRyan Libby if (ipers == 0 || (eff < UMA_MIN_EFF && eff < eff_offpage)) { 19294a8b575cSRyan Libby CTR5(KTR_UMA, "UMA decided we need offpage slab headers for " 19304a8b575cSRyan Libby "keg: %s(%p), minimum efficiency allowed = %u%%, " 1931e63a1c2fSRyan Libby "old efficiency = %u%%, offpage efficiency = %u%%", 19324a8b575cSRyan Libby keg->uk_name, keg, UMA_FIXPT_PCT(UMA_MIN_EFF), 19334a8b575cSRyan Libby UMA_FIXPT_PCT(eff), UMA_FIXPT_PCT(eff_offpage)); 19344a8b575cSRyan Libby format = UMA_ZFLAG_OFFPAGE; 19354a8b575cSRyan Libby ipers = ipers_offpage; 19368355f576SJeff Roberson } 1937ad97af7eSGleb Smirnoff 19384a8b575cSRyan Libby out: 19394a8b575cSRyan Libby /* 19404a8b575cSRyan Libby * How do we find the slab header if it is offpage or if not all item 19414a8b575cSRyan Libby * start addresses are in the same page? We could solve the latter 19424a8b575cSRyan Libby * case with vaddr alignment, but we don't. 19434a8b575cSRyan Libby */ 19444a8b575cSRyan Libby if ((format & UMA_ZFLAG_OFFPAGE) != 0 || 19454a8b575cSRyan Libby (ipers - 1) * rsize >= PAGE_SIZE) { 194654c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0) 19474a8b575cSRyan Libby format |= UMA_ZFLAG_HASH; 194854c5ae80SRyan Libby else 19494a8b575cSRyan Libby format |= UMA_ZFLAG_VTOSLAB; 195054c5ae80SRyan Libby } 19514a8b575cSRyan Libby keg->uk_ipers = ipers; 1952e20a199fSJeff Roberson keg->uk_rsize = rsize; 19534a8b575cSRyan Libby keg->uk_flags |= format; 1954e20a199fSJeff Roberson keg->uk_ppera = pages; 1955e63a1c2fSRyan Libby CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u", 19564a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_flags, rsize, ipers, pages); 19574a8b575cSRyan Libby KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 19584a8b575cSRyan Libby ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__, 19594a8b575cSRyan Libby keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize, ipers, 19604a8b575cSRyan Libby pages)); 1961e20a199fSJeff Roberson } 1962e20a199fSJeff Roberson 19638355f576SJeff Roberson /* 1964099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 1965099a0e58SBosko Milekic * the keg onto the global keg list. 19668355f576SJeff Roberson * 19678355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 1968099a0e58SBosko Milekic * udata Actually uma_kctor_args 1969099a0e58SBosko Milekic */ 1970b23f72e9SBrian Feldman static int 1971b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 1972099a0e58SBosko Milekic { 1973099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 1974099a0e58SBosko Milekic uma_keg_t keg = mem; 1975099a0e58SBosko Milekic uma_zone_t zone; 19768b987a77SJeff Roberson int i; 1977099a0e58SBosko Milekic 1978099a0e58SBosko Milekic bzero(keg, size); 1979099a0e58SBosko Milekic keg->uk_size = arg->size; 1980099a0e58SBosko Milekic keg->uk_init = arg->uminit; 1981099a0e58SBosko Milekic keg->uk_fini = arg->fini; 1982099a0e58SBosko Milekic keg->uk_align = arg->align; 19836fd34d6fSJeff Roberson keg->uk_reserve = 0; 1984099a0e58SBosko Milekic keg->uk_flags = arg->flags; 1985099a0e58SBosko Milekic 1986099a0e58SBosko Milekic /* 1987194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 1988dfe13344SJeff Roberson * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which 1989dfe13344SJeff Roberson * case the iterator is never run. 1990194a979eSMark Johnston */ 1991194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 1992194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 1993194a979eSMark Johnston 1994194a979eSMark Johnston /* 1995099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 1996099a0e58SBosko Milekic */ 1997099a0e58SBosko Milekic zone = arg->zone; 1998e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 1999099a0e58SBosko Milekic 2000099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_VM) 2001099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 2002099a0e58SBosko Milekic 2003099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 2004099a0e58SBosko Milekic keg->uk_init = zero_init; 2005099a0e58SBosko Milekic 2006cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 200754c5ae80SRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 2008e20a199fSJeff Roberson 200954c5ae80SRyan Libby #ifndef SMP 2010ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 2011ad97af7eSGleb Smirnoff #endif 2012ad97af7eSGleb Smirnoff 20134a8b575cSRyan Libby keg_layout(keg); 2014099a0e58SBosko Milekic 20158b987a77SJeff Roberson /* 2016dfe13344SJeff Roberson * Use a first-touch NUMA policy for all kegs that pmap_extract() 2017dfe13344SJeff Roberson * will work on with the exception of critical VM structures 2018dfe13344SJeff Roberson * necessary for paging. 2019dfe13344SJeff Roberson * 2020dfe13344SJeff Roberson * Zones may override the default by specifying either. 20218b987a77SJeff Roberson */ 2022dfe13344SJeff Roberson #ifdef NUMA 2023dfe13344SJeff Roberson if ((keg->uk_flags & 202454c5ae80SRyan Libby (UMA_ZFLAG_HASH | UMA_ZONE_VM | UMA_ZONE_ROUNDROBIN)) == 0) 2025dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_FIRSTTOUCH; 2026dfe13344SJeff Roberson else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2027dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_ROUNDROBIN; 20288b987a77SJeff Roberson #endif 20298b987a77SJeff Roberson 2030099a0e58SBosko Milekic /* 2031099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 2032099a0e58SBosko Milekic * startup cache until the vm is ready. 2033099a0e58SBosko Milekic */ 203477e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2035a81c400eSJeff Roberson if (keg->uk_ppera == 1) 203677e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 2037a81c400eSJeff Roberson else 20388cd02d00SAlan Cox #endif 2039a81c400eSJeff Roberson if (booted < BOOT_KVA) 2040a81c400eSJeff Roberson keg->uk_allocf = startup_alloc; 2041ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 2042ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 204377e19437SGleb Smirnoff else 204477e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 204577e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 204677e19437SGleb Smirnoff if (keg->uk_ppera == 1) 204777e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 204877e19437SGleb Smirnoff else 204977e19437SGleb Smirnoff #endif 2050ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 2051ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 2052ab3059a8SMatt Macy else 205377e19437SGleb Smirnoff keg->uk_freef = page_free; 2054099a0e58SBosko Milekic 2055099a0e58SBosko Milekic /* 20568b987a77SJeff Roberson * Initialize keg's locks. 2057099a0e58SBosko Milekic */ 20588b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 20598b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 2060099a0e58SBosko Milekic 2061099a0e58SBosko Milekic /* 2062099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 20639b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 20649b78b1f4SJeff Roberson * definition. 2065099a0e58SBosko Milekic */ 206654c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) { 20679b78b1f4SJeff Roberson size_t shsize; 20689b78b1f4SJeff Roberson 20699b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 20709b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 2071244f4554SBosko Milekic /* 2072244f4554SBosko Milekic * The only way the following is possible is if with our 2073244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 2074244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 2075244f4554SBosko Milekic * mathematically possible for all cases, so we make 2076244f4554SBosko Milekic * sure here anyway. 2077244f4554SBosko Milekic */ 20789b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 20793d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 20803d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 2081099a0e58SBosko Milekic } 2082099a0e58SBosko Milekic 208354c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 20843b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 2085099a0e58SBosko Milekic 2086e63a1c2fSRyan Libby CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone); 2087099a0e58SBosko Milekic 2088099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 2089099a0e58SBosko Milekic 2090111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2091099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2092111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2093b23f72e9SBrian Feldman return (0); 2094099a0e58SBosko Milekic } 2095099a0e58SBosko Milekic 20962efcc8cbSGleb Smirnoff static void 2097a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused) 2098a81c400eSJeff Roberson { 2099a81c400eSJeff Roberson uma_keg_t keg; 2100a81c400eSJeff Roberson 2101a81c400eSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 2102a81c400eSJeff Roberson return; 2103a81c400eSJeff Roberson KEG_GET(zone, keg); 2104a81c400eSJeff Roberson if (keg->uk_allocf == startup_alloc) 2105a81c400eSJeff Roberson keg->uk_allocf = page_alloc; 2106a81c400eSJeff Roberson } 2107a81c400eSJeff Roberson 2108a81c400eSJeff Roberson static void 210920a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 21102efcc8cbSGleb Smirnoff { 21112efcc8cbSGleb Smirnoff 21122efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 21132efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 21142efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 21152efcc8cbSGleb Smirnoff } 21162efcc8cbSGleb Smirnoff 211720a4e154SJeff Roberson static void 211820a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 211920a4e154SJeff Roberson { 212020a4e154SJeff Roberson uma_zone_domain_t zdom; 21218b987a77SJeff Roberson uma_domain_t dom; 212220a4e154SJeff Roberson uma_keg_t keg; 212320a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 21243b490537SJeff Roberson int domains, i, cnt; 212520a4e154SJeff Roberson static const char *nokeg = "cache zone"; 212620a4e154SJeff Roberson char *c; 212720a4e154SJeff Roberson 212820a4e154SJeff Roberson /* 212920a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 213020a4e154SJeff Roberson * any special characters and handling dups by appending 213120a4e154SJeff Roberson * an index. 213220a4e154SJeff Roberson */ 213320a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 21343b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 21353b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 21363b490537SJeff Roberson cnt /= 10; 21373b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 21383b490537SJeff Roberson M_UMA, M_WAITOK); 213920a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 214020a4e154SJeff Roberson zone->uz_namecnt); 214120a4e154SJeff Roberson } else 214220a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 214320a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 214420a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 214520a4e154SJeff Roberson *c = '_'; 214620a4e154SJeff Roberson 214720a4e154SJeff Roberson /* 214820a4e154SJeff Roberson * Basic parameters at the root. 214920a4e154SJeff Roberson */ 215020a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 215120a4e154SJeff Roberson OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, ""); 215220a4e154SJeff Roberson oid = zone->uz_oid; 215320a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 215420a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 21556d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21566d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 21576d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 215820a4e154SJeff Roberson "Allocator configuration flags"); 215920a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 216020a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 216120a4e154SJeff Roberson "Desired per-cpu cache size"); 216220a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 216320a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 216420a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 216520a4e154SJeff Roberson 216620a4e154SJeff Roberson /* 216720a4e154SJeff Roberson * keg if present. 216820a4e154SJeff Roberson */ 216954c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 21708b987a77SJeff Roberson domains = vm_ndomains; 21718b987a77SJeff Roberson else 21728b987a77SJeff Roberson domains = 1; 217320a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 217420a4e154SJeff Roberson "keg", CTLFLAG_RD, NULL, ""); 217520a4e154SJeff Roberson keg = zone->uz_keg; 21763b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 217720a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 217820a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 217920a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 218020a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 218120a4e154SJeff Roberson "Real object size with alignment"); 218220a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 218320a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 218420a4e154SJeff Roberson "pages per-slab allocation"); 218520a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 218620a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 218720a4e154SJeff Roberson "items available per-slab"); 218820a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 218920a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 219020a4e154SJeff Roberson "item alignment mask"); 2191f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2192f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2193f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2194f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 21958b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 21968b987a77SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 21978b987a77SJeff Roberson for (i = 0; i < domains; i++) { 21988b987a77SJeff Roberson dom = &keg->uk_domain[i]; 21998b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 22008b987a77SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, 22018b987a77SJeff Roberson NULL, ""); 22028b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22038b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 22048b987a77SJeff Roberson "Total pages currently allocated from VM"); 22058b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22068b987a77SJeff Roberson "free", CTLFLAG_RD, &dom->ud_free, 0, 22078b987a77SJeff Roberson "items free in the slab layer"); 22088b987a77SJeff Roberson } 220920a4e154SJeff Roberson } else 221020a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 221120a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 221220a4e154SJeff Roberson 221320a4e154SJeff Roberson /* 221420a4e154SJeff Roberson * Information about zone limits. 221520a4e154SJeff Roberson */ 221620a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 221720a4e154SJeff Roberson "limit", CTLFLAG_RD, NULL, ""); 22184bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22194bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 22204bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 22214bd61e19SJeff Roberson "current number of allocated items if limit is set"); 222220a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 222320a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 222420a4e154SJeff Roberson "Maximum number of cached items"); 222520a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 222620a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 222720a4e154SJeff Roberson "Number of threads sleeping at limit"); 222820a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 222920a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 223020a4e154SJeff Roberson "Total zone limit sleeps"); 22314bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22324bd61e19SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bkt_max, 0, 22334bd61e19SJeff Roberson "Maximum number of items in the bucket cache"); 22344bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 22354bd61e19SJeff Roberson "bucket_cnt", CTLFLAG_RD, &zone->uz_bkt_count, 0, 22364bd61e19SJeff Roberson "Number of items in the bucket cache"); 223720a4e154SJeff Roberson 223820a4e154SJeff Roberson /* 22398b987a77SJeff Roberson * Per-domain zone information. 224020a4e154SJeff Roberson */ 224120a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 224220a4e154SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 2243dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0) 22448b987a77SJeff Roberson domains = 1; 224520a4e154SJeff Roberson for (i = 0; i < domains; i++) { 224620a4e154SJeff Roberson zdom = &zone->uz_domain[i]; 224720a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 224820a4e154SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, ""); 224920a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 225020a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 225120a4e154SJeff Roberson "number of items in this domain"); 225220a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 225320a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 225420a4e154SJeff Roberson "maximum item count in this period"); 225520a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 225620a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 225720a4e154SJeff Roberson "minimum item count in this period"); 225820a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 225920a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 226020a4e154SJeff Roberson "Working set size"); 226120a4e154SJeff Roberson } 226220a4e154SJeff Roberson 226320a4e154SJeff Roberson /* 226420a4e154SJeff Roberson * General statistics. 226520a4e154SJeff Roberson */ 226620a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 226720a4e154SJeff Roberson "stats", CTLFLAG_RD, NULL, ""); 226820a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 226920a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 227020a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 227120a4e154SJeff Roberson "Current number of allocated items"); 227220a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 227320a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 227420a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 227520a4e154SJeff Roberson "Total allocation calls"); 227620a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 227720a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 227820a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 227920a4e154SJeff Roberson "Total free calls"); 228020a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 228120a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 228220a4e154SJeff Roberson "Number of allocation failures"); 228320a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 228420a4e154SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 0, 228520a4e154SJeff Roberson "Free calls from the wrong domain"); 228620a4e154SJeff Roberson } 228720a4e154SJeff Roberson 228820a4e154SJeff Roberson struct uma_zone_count { 228920a4e154SJeff Roberson const char *name; 229020a4e154SJeff Roberson int count; 229120a4e154SJeff Roberson }; 229220a4e154SJeff Roberson 229320a4e154SJeff Roberson static void 229420a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 229520a4e154SJeff Roberson { 229620a4e154SJeff Roberson struct uma_zone_count *cnt; 229720a4e154SJeff Roberson 229820a4e154SJeff Roberson cnt = arg; 22993b490537SJeff Roberson /* 23003b490537SJeff Roberson * Some zones are rapidly created with identical names and 23013b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 23023b490537SJeff Roberson * Use one greater than the maximum observed for this name. 23033b490537SJeff Roberson */ 230420a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 23053b490537SJeff Roberson cnt->count = MAX(cnt->count, 23063b490537SJeff Roberson zone->uz_namecnt + 1); 230720a4e154SJeff Roberson } 230820a4e154SJeff Roberson 2309cc7ce83aSJeff Roberson static void 2310cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2311cc7ce83aSJeff Roberson { 2312cc7ce83aSJeff Roberson int i; 2313cc7ce83aSJeff Roberson 2314cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2315cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2316cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2317cc7ce83aSJeff Roberson } 2318cc7ce83aSJeff Roberson } 2319cc7ce83aSJeff Roberson 2320099a0e58SBosko Milekic /* 2321099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2322099a0e58SBosko Milekic * 2323099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2324099a0e58SBosko Milekic * udata Actually uma_zctor_args 23258355f576SJeff Roberson */ 2326b23f72e9SBrian Feldman static int 2327b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 23288355f576SJeff Roberson { 232920a4e154SJeff Roberson struct uma_zone_count cnt; 23308355f576SJeff Roberson struct uma_zctor_args *arg = udata; 23318355f576SJeff Roberson uma_zone_t zone = mem; 2332099a0e58SBosko Milekic uma_zone_t z; 2333099a0e58SBosko Milekic uma_keg_t keg; 233408cfa56eSMark Johnston int i; 23358355f576SJeff Roberson 23368355f576SJeff Roberson bzero(zone, size); 23378355f576SJeff Roberson zone->uz_name = arg->name; 23388355f576SJeff Roberson zone->uz_ctor = arg->ctor; 23398355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2340099a0e58SBosko Milekic zone->uz_init = NULL; 2341099a0e58SBosko Milekic zone->uz_fini = NULL; 2342bf965959SSean Bruno zone->uz_sleeps = 0; 2343c1685086SJeff Roberson zone->uz_xdomain = 0; 234420a4e154SJeff Roberson zone->uz_bucket_size = 0; 234520a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 234620a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2347*d4665eaaSJeff Roberson zone->uz_flags = (arg->flags & UMA_ZONE_SMR); 23482f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2349ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 23508d1c459aSRyan Libby zone->uz_domain = 23518d1c459aSRyan Libby (struct uma_zone_domain *)&zone->uz_cpu[mp_maxid + 1]; 2352bb15d1c7SGleb Smirnoff zone->uz_bkt_max = ULONG_MAX; 23532f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2354af526374SJeff Roberson 235520a4e154SJeff Roberson /* Count the number of duplicate names. */ 235620a4e154SJeff Roberson cnt.name = arg->name; 235720a4e154SJeff Roberson cnt.count = 0; 235820a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 235920a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 2360727c6918SJeff Roberson ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 236191d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 23622efcc8cbSGleb Smirnoff 236308cfa56eSMark Johnston for (i = 0; i < vm_ndomains; i++) 236408cfa56eSMark Johnston TAILQ_INIT(&zone->uz_domain[i].uzd_buckets); 236508cfa56eSMark Johnston 2366ca293436SRyan Libby #ifdef INVARIANTS 2367ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2368cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 2369ca293436SRyan Libby #endif 2370ca293436SRyan Libby 23710095a784SJeff Roberson /* 23720095a784SJeff Roberson * This is a pure cache zone, no kegs. 23730095a784SJeff Roberson */ 23740095a784SJeff Roberson if (arg->import) { 2375727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2376727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 23776fd34d6fSJeff Roberson if (arg->flags & UMA_ZONE_VM) 23786fd34d6fSJeff Roberson arg->flags |= UMA_ZFLAG_CACHEONLY; 23796fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2380af526374SJeff Roberson zone->uz_size = arg->size; 23810095a784SJeff Roberson zone->uz_import = arg->import; 23820095a784SJeff Roberson zone->uz_release = arg->release; 23830095a784SJeff Roberson zone->uz_arg = arg->arg; 2384111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 238503175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2386111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2387af526374SJeff Roberson goto out; 23880095a784SJeff Roberson } 23890095a784SJeff Roberson 23900095a784SJeff Roberson /* 23910095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 23920095a784SJeff Roberson */ 2393b75c4efcSAndrew Turner zone->uz_import = zone_import; 2394b75c4efcSAndrew Turner zone->uz_release = zone_release; 23950095a784SJeff Roberson zone->uz_arg = zone; 2396bb15d1c7SGleb Smirnoff keg = arg->keg; 23970095a784SJeff Roberson 2398099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 239920a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 240020a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2401099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 24028355f576SJeff Roberson zone->uz_init = arg->uminit; 2403e221e841SJeff Roberson zone->uz_fini = arg->fini; 2404e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2405111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2406099a0e58SBosko Milekic ZONE_LOCK(zone); 2407099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2408099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2409099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2410099a0e58SBosko Milekic break; 2411099a0e58SBosko Milekic } 2412099a0e58SBosko Milekic } 2413099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2414111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2415e20a199fSJeff Roberson } else if (keg == NULL) { 2416e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2417e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2418b23f72e9SBrian Feldman return (ENOMEM); 2419099a0e58SBosko Milekic } else { 2420099a0e58SBosko Milekic struct uma_kctor_args karg; 2421b23f72e9SBrian Feldman int error; 2422099a0e58SBosko Milekic 2423099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2424099a0e58SBosko Milekic karg.size = arg->size; 2425099a0e58SBosko Milekic karg.uminit = arg->uminit; 2426099a0e58SBosko Milekic karg.fini = arg->fini; 2427099a0e58SBosko Milekic karg.align = arg->align; 2428*d4665eaaSJeff Roberson karg.flags = (arg->flags & ~UMA_ZONE_SMR); 2429099a0e58SBosko Milekic karg.zone = zone; 2430b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2431b23f72e9SBrian Feldman flags); 2432b23f72e9SBrian Feldman if (error) 2433b23f72e9SBrian Feldman return (error); 2434099a0e58SBosko Milekic } 24350095a784SJeff Roberson 243620a4e154SJeff Roberson /* Inherit properties from the keg. */ 2437bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2438e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2439e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2440e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 24418355f576SJeff Roberson 244220a4e154SJeff Roberson out: 2443860bb7a0SMark Johnston if (__predict_true(booted >= BOOT_RUNNING)) { 244420a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 244520a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 244620a4e154SJeff Roberson } else { 244720a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 244820a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 244920a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2450099a0e58SBosko Milekic } 24518355f576SJeff Roberson 2452*d4665eaaSJeff Roberson /* Caller requests a private SMR context. */ 2453*d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 2454*d4665eaaSJeff Roberson zone->uz_smr = smr_create(zone->uz_name); 2455*d4665eaaSJeff Roberson 24567e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 24577e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 24587e28037aSMark Johnston ("Invalid zone flag combination")); 245920a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 246020a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 246120a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 246220a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 246320a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) 246420a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN; 246520a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 246620a4e154SJeff Roberson zone->uz_bucket_size = 0; 24677e28037aSMark Johnston else 246820a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 246920a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2470cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2471cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2472cc7ce83aSJeff Roberson zone_update_caches(zone); 2473fc03d22bSJeff Roberson 2474b23f72e9SBrian Feldman return (0); 24758355f576SJeff Roberson } 24768355f576SJeff Roberson 24778355f576SJeff Roberson /* 2478099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2479099a0e58SBosko Milekic * table and removes the keg from the global list. 24809c2cd7e5SJeff Roberson * 24819c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 24829c2cd7e5SJeff Roberson * udata unused 24839c2cd7e5SJeff Roberson */ 2484099a0e58SBosko Milekic static void 2485099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2486099a0e58SBosko Milekic { 2487099a0e58SBosko Milekic uma_keg_t keg; 24888b987a77SJeff Roberson uint32_t free, pages; 24898b987a77SJeff Roberson int i; 24909c2cd7e5SJeff Roberson 2491099a0e58SBosko Milekic keg = (uma_keg_t)arg; 24928b987a77SJeff Roberson free = pages = 0; 24938b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 24948b987a77SJeff Roberson free += keg->uk_domain[i].ud_free; 24958b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 24968b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2497099a0e58SBosko Milekic } 24987e240677SRyan Libby if (pages != 0) 24998b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 25008b987a77SJeff Roberson " Lost %u pages of memory.\n", 25018b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 25027e240677SRyan Libby pages / keg->uk_ppera * keg->uk_ipers - free, pages); 2503099a0e58SBosko Milekic 2504099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2505099a0e58SBosko Milekic } 2506099a0e58SBosko Milekic 2507099a0e58SBosko Milekic /* 2508099a0e58SBosko Milekic * Zone header dtor. 2509099a0e58SBosko Milekic * 2510099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2511099a0e58SBosko Milekic * udata unused 2512099a0e58SBosko Milekic */ 25139c2cd7e5SJeff Roberson static void 25149c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 25159c2cd7e5SJeff Roberson { 25169c2cd7e5SJeff Roberson uma_zone_t zone; 2517099a0e58SBosko Milekic uma_keg_t keg; 25189c2cd7e5SJeff Roberson 25199c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 25209643769aSJeff Roberson 252120a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 252220a4e154SJeff Roberson 2523e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 25249643769aSJeff Roberson cache_drain(zone); 2525099a0e58SBosko Milekic 2526111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2527099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2528111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2529099a0e58SBosko Milekic /* 2530099a0e58SBosko Milekic * XXX there are some races here where 2531099a0e58SBosko Milekic * the zone can be drained but zone lock 2532099a0e58SBosko Milekic * released and then refilled before we 2533099a0e58SBosko Milekic * remove it... we dont care for now 2534099a0e58SBosko Milekic */ 253508cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2536e20a199fSJeff Roberson /* 2537323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2538e20a199fSJeff Roberson */ 2539323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2540323ad386STycho Nightingale keg = zone->uz_keg; 2541111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2542099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2543111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 25440095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 25459c2cd7e5SJeff Roberson } 25462efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 25472efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 25482efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 254920a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2550af526374SJeff Roberson ZONE_LOCK_FINI(zone); 255191d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 2552099a0e58SBosko Milekic } 2553099a0e58SBosko Milekic 2554a81c400eSJeff Roberson static void 2555a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg) 2556a81c400eSJeff Roberson { 2557a81c400eSJeff Roberson uma_keg_t keg; 2558a81c400eSJeff Roberson uma_zone_t zone; 2559a81c400eSJeff Roberson 2560a81c400eSJeff Roberson LIST_FOREACH(keg, &uma_kegs, uk_link) { 2561a81c400eSJeff Roberson LIST_FOREACH(zone, &keg->uk_zones, uz_link) 2562a81c400eSJeff Roberson zfunc(zone, arg); 2563a81c400eSJeff Roberson } 2564a81c400eSJeff Roberson LIST_FOREACH(zone, &uma_cachezones, uz_link) 2565a81c400eSJeff Roberson zfunc(zone, arg); 2566a81c400eSJeff Roberson } 2567a81c400eSJeff Roberson 25689c2cd7e5SJeff Roberson /* 25698355f576SJeff Roberson * Traverses every zone in the system and calls a callback 25708355f576SJeff Roberson * 25718355f576SJeff Roberson * Arguments: 25728355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 25738355f576SJeff Roberson * as an argument. 25748355f576SJeff Roberson * 25758355f576SJeff Roberson * Returns: 25768355f576SJeff Roberson * Nothing 25778355f576SJeff Roberson */ 25788355f576SJeff Roberson static void 257920a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 25808355f576SJeff Roberson { 25818355f576SJeff Roberson 2582111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2583a81c400eSJeff Roberson zone_foreach_unlocked(zfunc, arg); 2584111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 25858355f576SJeff Roberson } 25868355f576SJeff Roberson 2587f4bef67cSGleb Smirnoff /* 2588a81c400eSJeff Roberson * Initialize the kernel memory allocator. This is done after pages can be 2589a81c400eSJeff Roberson * allocated but before general KVA is available. 2590f4bef67cSGleb Smirnoff */ 2591a81c400eSJeff Roberson void 2592a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail) 2593f4bef67cSGleb Smirnoff { 2594a81c400eSJeff Roberson struct uma_zctor_args args; 2595a81c400eSJeff Roberson size_t ksize, zsize, size; 2596a81c400eSJeff Roberson uma_keg_t masterkeg; 2597a81c400eSJeff Roberson uintptr_t m; 2598a81c400eSJeff Roberson uint8_t pflag; 2599a81c400eSJeff Roberson 2600a81c400eSJeff Roberson bootstart = bootmem = virtual_avail; 2601a81c400eSJeff Roberson 2602a81c400eSJeff Roberson rw_init(&uma_rwlock, "UMA lock"); 2603a81c400eSJeff Roberson sx_init(&uma_reclaim_lock, "umareclaim"); 2604f4bef67cSGleb Smirnoff 2605f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2606f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 260779c9f942SJeff Roberson ksize = roundup(ksize, UMA_SUPER_ALIGN); 2608f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2609f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2610f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 261179c9f942SJeff Roberson zsize = roundup(zsize, UMA_SUPER_ALIGN); 2612f4bef67cSGleb Smirnoff 2613a81c400eSJeff Roberson /* Allocate the zone of zones, zone of kegs, and zone of zones keg. */ 2614a81c400eSJeff Roberson size = (zsize * 2) + ksize; 2615a81c400eSJeff Roberson m = (uintptr_t)startup_alloc(NULL, size, 0, &pflag, M_NOWAIT | M_ZERO); 2616ab3185d1SJeff Roberson zones = (uma_zone_t)m; 261779c9f942SJeff Roberson m += zsize; 2618ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 261979c9f942SJeff Roberson m += zsize; 2620ab3185d1SJeff Roberson masterkeg = (uma_keg_t)m; 2621ab3185d1SJeff Roberson 2622099a0e58SBosko Milekic /* "manually" create the initial zone */ 26230095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2624099a0e58SBosko Milekic args.name = "UMA Kegs"; 2625ab3185d1SJeff Roberson args.size = ksize; 2626099a0e58SBosko Milekic args.ctor = keg_ctor; 2627099a0e58SBosko Milekic args.dtor = keg_dtor; 26288355f576SJeff Roberson args.uminit = zero_init; 26298355f576SJeff Roberson args.fini = NULL; 2630ab3185d1SJeff Roberson args.keg = masterkeg; 263179c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2632b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2633ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 26348355f576SJeff Roberson 2635099a0e58SBosko Milekic args.name = "UMA Zones"; 2636f4bef67cSGleb Smirnoff args.size = zsize; 2637099a0e58SBosko Milekic args.ctor = zone_ctor; 2638099a0e58SBosko Milekic args.dtor = zone_dtor; 2639099a0e58SBosko Milekic args.uminit = zero_init; 2640099a0e58SBosko Milekic args.fini = NULL; 2641099a0e58SBosko Milekic args.keg = NULL; 264279c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2643099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2644ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2645099a0e58SBosko Milekic 26469b8db4d0SRyan Libby /* Now make zones for slab headers */ 26479b8db4d0SRyan Libby slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE, 26489b8db4d0SRyan Libby NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 26499b8db4d0SRyan Libby slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE, 26501e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 26518355f576SJeff Roberson 26528355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 26538355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 26541e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 26558355f576SJeff Roberson 2656a81c400eSJeff Roberson bucket_init(); 2657*d4665eaaSJeff Roberson smr_init(); 26588355f576SJeff Roberson } 26598355f576SJeff Roberson 2660a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2661a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void); 2662f4bef67cSGleb Smirnoff #endif 2663f4bef67cSGleb Smirnoff 2664a81c400eSJeff Roberson /* 2665a81c400eSJeff Roberson * Advertise the availability of normal kva allocations and switch to 2666a81c400eSJeff Roberson * the default back-end allocator. Marks the KVA we consumed on startup 2667a81c400eSJeff Roberson * as used in the map. 2668a81c400eSJeff Roberson */ 26698355f576SJeff Roberson void 267099571dc3SJeff Roberson uma_startup2(void) 26718355f576SJeff Roberson { 2672f4bef67cSGleb Smirnoff 2673530cc6a2SJeff Roberson if (bootstart != bootmem) { 2674a81c400eSJeff Roberson vm_map_lock(kernel_map); 2675a81c400eSJeff Roberson (void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem, 2676a81c400eSJeff Roberson VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 2677a81c400eSJeff Roberson vm_map_unlock(kernel_map); 2678a81c400eSJeff Roberson } 2679a81c400eSJeff Roberson 2680a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2681a81c400eSJeff Roberson /* Set up radix zone to use noobj_alloc. */ 2682a81c400eSJeff Roberson vm_radix_reserve_kva(); 2683f7d35785SGleb Smirnoff #endif 2684a81c400eSJeff Roberson 2685a81c400eSJeff Roberson booted = BOOT_KVA; 2686a81c400eSJeff Roberson zone_foreach_unlocked(zone_kva_available, NULL); 2687f4bef67cSGleb Smirnoff bucket_enable(); 26888355f576SJeff Roberson } 26898355f576SJeff Roberson 2690a81c400eSJeff Roberson /* 2691a81c400eSJeff Roberson * Finish our initialization steps. 2692a81c400eSJeff Roberson */ 26938355f576SJeff Roberson static void 26948355f576SJeff Roberson uma_startup3(void) 26958355f576SJeff Roberson { 26961431a748SGleb Smirnoff 2697c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2698c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2699c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2700c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2701c5deaf04SGleb Smirnoff #endif 2702a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_counters, NULL); 2703a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_sysctl, NULL); 2704fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 27059643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2706c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 2707860bb7a0SMark Johnston 2708860bb7a0SMark Johnston EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL, 2709860bb7a0SMark Johnston EVENTHANDLER_PRI_FIRST); 2710860bb7a0SMark Johnston } 2711860bb7a0SMark Johnston 2712860bb7a0SMark Johnston static void 2713860bb7a0SMark Johnston uma_shutdown(void) 2714860bb7a0SMark Johnston { 2715860bb7a0SMark Johnston 2716860bb7a0SMark Johnston booted = BOOT_SHUTDOWN; 27178355f576SJeff Roberson } 27188355f576SJeff Roberson 2719e20a199fSJeff Roberson static uma_keg_t 2720099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 272185dcf349SGleb Smirnoff int align, uint32_t flags) 2722099a0e58SBosko Milekic { 2723099a0e58SBosko Milekic struct uma_kctor_args args; 2724099a0e58SBosko Milekic 2725099a0e58SBosko Milekic args.size = size; 2726099a0e58SBosko Milekic args.uminit = uminit; 2727099a0e58SBosko Milekic args.fini = fini; 27281e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2729099a0e58SBosko Milekic args.flags = flags; 2730099a0e58SBosko Milekic args.zone = zone; 2731ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 2732099a0e58SBosko Milekic } 2733099a0e58SBosko Milekic 2734f4bef67cSGleb Smirnoff /* Public functions */ 27358355f576SJeff Roberson /* See uma.h */ 27361e319f6dSRobert Watson void 27371e319f6dSRobert Watson uma_set_align(int align) 27381e319f6dSRobert Watson { 27391e319f6dSRobert Watson 27401e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 27411e319f6dSRobert Watson uma_align_cache = align; 27421e319f6dSRobert Watson } 27431e319f6dSRobert Watson 27441e319f6dSRobert Watson /* See uma.h */ 27458355f576SJeff Roberson uma_zone_t 2746bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 274785dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 27488355f576SJeff Roberson 27498355f576SJeff Roberson { 27508355f576SJeff Roberson struct uma_zctor_args args; 275195c4bf75SKonstantin Belousov uma_zone_t res; 27528355f576SJeff Roberson 2753a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 2754a5a35578SJohn Baldwin align, name)); 2755a5a35578SJohn Baldwin 27568355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 27570095a784SJeff Roberson memset(&args, 0, sizeof(args)); 27588355f576SJeff Roberson args.name = name; 27598355f576SJeff Roberson args.size = size; 27608355f576SJeff Roberson args.ctor = ctor; 27618355f576SJeff Roberson args.dtor = dtor; 27628355f576SJeff Roberson args.uminit = uminit; 27638355f576SJeff Roberson args.fini = fini; 2764afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 2765afc6dc36SJohn-Mark Gurney /* 2766ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 2767ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 2768ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 2769ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 2770ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 2771ca293436SRyan Libby * the ctor and dtor do not need to be empty. 2772afc6dc36SJohn-Mark Gurney */ 277354c5ae80SRyan Libby if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH | 277454c5ae80SRyan Libby UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) { 2775afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 2776afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 2777afc6dc36SJohn-Mark Gurney } 2778afc6dc36SJohn-Mark Gurney #endif 27798355f576SJeff Roberson args.align = align; 27808355f576SJeff Roberson args.flags = flags; 2781099a0e58SBosko Milekic args.keg = NULL; 2782099a0e58SBosko Milekic 278308cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 2784ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 278508cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 2786a81c400eSJeff Roberson 278795c4bf75SKonstantin Belousov return (res); 2788099a0e58SBosko Milekic } 2789099a0e58SBosko Milekic 2790099a0e58SBosko Milekic /* See uma.h */ 2791099a0e58SBosko Milekic uma_zone_t 2792099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 2793099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 2794099a0e58SBosko Milekic { 2795099a0e58SBosko Milekic struct uma_zctor_args args; 2796e20a199fSJeff Roberson uma_keg_t keg; 279795c4bf75SKonstantin Belousov uma_zone_t res; 2798099a0e58SBosko Milekic 2799bb15d1c7SGleb Smirnoff keg = master->uz_keg; 28000095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2801099a0e58SBosko Milekic args.name = name; 2802e20a199fSJeff Roberson args.size = keg->uk_size; 2803099a0e58SBosko Milekic args.ctor = ctor; 2804099a0e58SBosko Milekic args.dtor = dtor; 2805099a0e58SBosko Milekic args.uminit = zinit; 2806099a0e58SBosko Milekic args.fini = zfini; 2807e20a199fSJeff Roberson args.align = keg->uk_align; 2808e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 2809e20a199fSJeff Roberson args.keg = keg; 28108355f576SJeff Roberson 281108cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 2812ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 281308cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 2814a81c400eSJeff Roberson 281595c4bf75SKonstantin Belousov return (res); 28168355f576SJeff Roberson } 28178355f576SJeff Roberson 28180095a784SJeff Roberson /* See uma.h */ 28190095a784SJeff Roberson uma_zone_t 2820af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 2821af526374SJeff Roberson uma_init zinit, uma_fini zfini, uma_import zimport, 2822af526374SJeff Roberson uma_release zrelease, void *arg, int flags) 28230095a784SJeff Roberson { 28240095a784SJeff Roberson struct uma_zctor_args args; 28250095a784SJeff Roberson 28260095a784SJeff Roberson memset(&args, 0, sizeof(args)); 28270095a784SJeff Roberson args.name = name; 2828af526374SJeff Roberson args.size = size; 28290095a784SJeff Roberson args.ctor = ctor; 28300095a784SJeff Roberson args.dtor = dtor; 28310095a784SJeff Roberson args.uminit = zinit; 28320095a784SJeff Roberson args.fini = zfini; 28330095a784SJeff Roberson args.import = zimport; 28340095a784SJeff Roberson args.release = zrelease; 28350095a784SJeff Roberson args.arg = arg; 28360095a784SJeff Roberson args.align = 0; 2837bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 28380095a784SJeff Roberson 2839ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 28400095a784SJeff Roberson } 28410095a784SJeff Roberson 28428355f576SJeff Roberson /* See uma.h */ 28439c2cd7e5SJeff Roberson void 28449c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 28459c2cd7e5SJeff Roberson { 2846f4ff923bSRobert Watson 2847860bb7a0SMark Johnston /* 2848860bb7a0SMark Johnston * Large slabs are expensive to reclaim, so don't bother doing 2849860bb7a0SMark Johnston * unnecessary work if we're shutting down. 2850860bb7a0SMark Johnston */ 2851860bb7a0SMark Johnston if (booted == BOOT_SHUTDOWN && 2852860bb7a0SMark Johnston zone->uz_fini == NULL && zone->uz_release == zone_release) 2853860bb7a0SMark Johnston return; 285408cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 28550095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 285608cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 28579c2cd7e5SJeff Roberson } 28589c2cd7e5SJeff Roberson 28598d6fbbb8SJeff Roberson void 28608d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 28618d6fbbb8SJeff Roberson { 28628d6fbbb8SJeff Roberson void *item; 28638d6fbbb8SJeff Roberson 28648d6fbbb8SJeff Roberson item = uma_zalloc_arg(zone, NULL, M_WAITOK); 28658d6fbbb8SJeff Roberson uma_zfree(zone, item); 28668d6fbbb8SJeff Roberson } 28678d6fbbb8SJeff Roberson 28684e180881SMateusz Guzik void * 28694e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 28704e180881SMateusz Guzik { 28714e180881SMateusz Guzik void *item; 2872b4799947SRuslan Bukin #ifdef SMP 28734e180881SMateusz Guzik int i; 28744e180881SMateusz Guzik 28754e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2876b4799947SRuslan Bukin #endif 28774e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 28784e180881SMateusz Guzik if (item != NULL && (flags & M_ZERO)) { 2879b4799947SRuslan Bukin #ifdef SMP 2880013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 28814e180881SMateusz Guzik bzero(zpcpu_get_cpu(item, i), zone->uz_size); 2882b4799947SRuslan Bukin #else 2883b4799947SRuslan Bukin bzero(item, zone->uz_size); 2884b4799947SRuslan Bukin #endif 28854e180881SMateusz Guzik } 28864e180881SMateusz Guzik return (item); 28874e180881SMateusz Guzik } 28884e180881SMateusz Guzik 28894e180881SMateusz Guzik /* 28904e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 28914e180881SMateusz Guzik */ 28924e180881SMateusz Guzik void 28934e180881SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata) 28944e180881SMateusz Guzik { 28954e180881SMateusz Guzik 2896c5b7751fSIan Lepore #ifdef SMP 28974e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2898c5b7751fSIan Lepore #endif 28994e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 29004e180881SMateusz Guzik } 29014e180881SMateusz Guzik 2902*d4665eaaSJeff Roberson static inline void * 2903*d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags, 2904*d4665eaaSJeff Roberson void *item) 2905beb8beefSJeff Roberson { 2906beb8beefSJeff Roberson #ifdef INVARIANTS 2907ca293436SRyan Libby bool skipdbg; 2908beb8beefSJeff Roberson 2909beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 2910ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2911ca293436SRyan Libby zone->uz_ctor != trash_ctor) 2912cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 2913beb8beefSJeff Roberson #endif 2914*d4665eaaSJeff Roberson /* Check flags before loading ctor pointer. */ 2915*d4665eaaSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) && 2916*d4665eaaSJeff Roberson __predict_false(zone->uz_ctor != NULL) && 2917cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 2918beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 2919beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 2920beb8beefSJeff Roberson return (NULL); 2921beb8beefSJeff Roberson } 2922beb8beefSJeff Roberson #ifdef INVARIANTS 2923beb8beefSJeff Roberson if (!skipdbg) 2924beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 2925beb8beefSJeff Roberson #endif 2926beb8beefSJeff Roberson if (flags & M_ZERO) 2927cc7ce83aSJeff Roberson bzero(item, size); 2928beb8beefSJeff Roberson 2929beb8beefSJeff Roberson return (item); 2930beb8beefSJeff Roberson } 2931beb8beefSJeff Roberson 2932ca293436SRyan Libby static inline void 2933cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 2934cc7ce83aSJeff Roberson enum zfreeskip skip) 2935ca293436SRyan Libby { 2936ca293436SRyan Libby #ifdef INVARIANTS 2937ca293436SRyan Libby bool skipdbg; 2938ca293436SRyan Libby 2939ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 2940ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 2941ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 2942ca293436SRyan Libby uma_dbg_free(zone, udata, item); 2943ca293436SRyan Libby else 2944ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 2945ca293436SRyan Libby } 2946ca293436SRyan Libby #endif 2947cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 2948ca293436SRyan Libby if (zone->uz_dtor != NULL) 2949cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 2950ca293436SRyan Libby #ifdef INVARIANTS 2951ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2952ca293436SRyan Libby zone->uz_dtor != trash_dtor) 2953cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 2954ca293436SRyan Libby #endif 2955ca293436SRyan Libby } 2956ca293436SRyan Libby } 2957ca293436SRyan Libby 2958*d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS) 2959*d4665eaaSJeff Roberson #define UMA_ZALLOC_DEBUG 2960*d4665eaaSJeff Roberson static int 2961*d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags) 2962*d4665eaaSJeff Roberson { 2963*d4665eaaSJeff Roberson int error; 2964*d4665eaaSJeff Roberson 2965*d4665eaaSJeff Roberson error = 0; 2966*d4665eaaSJeff Roberson #ifdef WITNESS 2967*d4665eaaSJeff Roberson if (flags & M_WAITOK) { 2968*d4665eaaSJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2969*d4665eaaSJeff Roberson "uma_zalloc_debug: zone \"%s\"", zone->uz_name); 2970*d4665eaaSJeff Roberson } 2971*d4665eaaSJeff Roberson #endif 2972*d4665eaaSJeff Roberson 2973*d4665eaaSJeff Roberson #ifdef INVARIANTS 2974*d4665eaaSJeff Roberson KASSERT((flags & M_EXEC) == 0, 2975*d4665eaaSJeff Roberson ("uma_zalloc_debug: called with M_EXEC")); 2976*d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 2977*d4665eaaSJeff Roberson ("uma_zalloc_debug: called within spinlock or critical section")); 2978*d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0, 2979*d4665eaaSJeff Roberson ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO")); 2980*d4665eaaSJeff Roberson #endif 2981*d4665eaaSJeff Roberson 2982*d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 2983*d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR == 0) && memguard_cmp_zone(zone)) { 2984*d4665eaaSJeff Roberson void *item; 2985*d4665eaaSJeff Roberson item = memguard_alloc(zone->uz_size, flags); 2986*d4665eaaSJeff Roberson if (item != NULL) { 2987*d4665eaaSJeff Roberson error = EJUSTRETURN; 2988*d4665eaaSJeff Roberson if (zone->uz_init != NULL && 2989*d4665eaaSJeff Roberson zone->uz_init(item, zone->uz_size, flags) != 0) { 2990*d4665eaaSJeff Roberson *itemp = NULL; 2991*d4665eaaSJeff Roberson return (error); 2992*d4665eaaSJeff Roberson } 2993*d4665eaaSJeff Roberson if (zone->uz_ctor != NULL && 2994*d4665eaaSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 2995*d4665eaaSJeff Roberson flags) != 0) { 2996*d4665eaaSJeff Roberson counter_u64_add(zone->uz_fails, 1); 2997*d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 2998*d4665eaaSJeff Roberson *itemp = NULL; 2999*d4665eaaSJeff Roberson return (error); 3000*d4665eaaSJeff Roberson } 3001*d4665eaaSJeff Roberson *itemp = item; 3002*d4665eaaSJeff Roberson return (error); 3003*d4665eaaSJeff Roberson } 3004*d4665eaaSJeff Roberson /* This is unfortunate but should not be fatal. */ 3005*d4665eaaSJeff Roberson } 3006*d4665eaaSJeff Roberson #endif 3007*d4665eaaSJeff Roberson return (error); 3008*d4665eaaSJeff Roberson } 3009*d4665eaaSJeff Roberson 3010*d4665eaaSJeff Roberson static int 3011*d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata) 3012*d4665eaaSJeff Roberson { 3013*d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3014*d4665eaaSJeff Roberson ("uma_zfree_debug: called with spinlock or critical section held")); 3015*d4665eaaSJeff Roberson 3016*d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 3017*d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR == 0) && is_memguard_addr(item)) { 3018*d4665eaaSJeff Roberson if (zone->uz_dtor != NULL) 3019*d4665eaaSJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 3020*d4665eaaSJeff Roberson if (zone->uz_fini != NULL) 3021*d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3022*d4665eaaSJeff Roberson memguard_free(item); 3023*d4665eaaSJeff Roberson return (EJUSTRETURN); 3024*d4665eaaSJeff Roberson } 3025*d4665eaaSJeff Roberson #endif 3026*d4665eaaSJeff Roberson return (0); 3027*d4665eaaSJeff Roberson } 3028*d4665eaaSJeff Roberson #endif 3029*d4665eaaSJeff Roberson 3030*d4665eaaSJeff Roberson static __noinline void * 3031*d4665eaaSJeff Roberson uma_zalloc_single(uma_zone_t zone, void *udata, int flags) 3032*d4665eaaSJeff Roberson { 3033*d4665eaaSJeff Roberson int domain; 3034*d4665eaaSJeff Roberson 3035*d4665eaaSJeff Roberson /* 3036*d4665eaaSJeff Roberson * We can not get a bucket so try to return a single item. 3037*d4665eaaSJeff Roberson */ 3038*d4665eaaSJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 3039*d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3040*d4665eaaSJeff Roberson else 3041*d4665eaaSJeff Roberson domain = UMA_ANYDOMAIN; 3042*d4665eaaSJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3043*d4665eaaSJeff Roberson } 3044*d4665eaaSJeff Roberson 3045*d4665eaaSJeff Roberson /* See uma.h */ 3046*d4665eaaSJeff Roberson void * 3047*d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags) 3048*d4665eaaSJeff Roberson { 3049*d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 3050*d4665eaaSJeff Roberson uma_cache_t cache; 3051*d4665eaaSJeff Roberson void *item; 3052*d4665eaaSJeff Roberson int size, uz_flags; 3053*d4665eaaSJeff Roberson 3054*d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3055*d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 3056*d4665eaaSJeff Roberson ("uma_zalloc_arg: called with non-SMR zone.\n")); 3057*d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN) 3058*d4665eaaSJeff Roberson return (item); 3059*d4665eaaSJeff Roberson #endif 3060*d4665eaaSJeff Roberson 3061*d4665eaaSJeff Roberson critical_enter(); 3062*d4665eaaSJeff Roberson do { 3063*d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3064*d4665eaaSJeff Roberson bucket = &cache->uc_allocbucket; 3065*d4665eaaSJeff Roberson size = cache_uz_size(cache); 3066*d4665eaaSJeff Roberson uz_flags = cache_uz_flags(cache); 3067*d4665eaaSJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 3068*d4665eaaSJeff Roberson item = cache_bucket_pop(cache, bucket); 3069*d4665eaaSJeff Roberson critical_exit(); 3070*d4665eaaSJeff Roberson return (item_ctor(zone, uz_flags, size, NULL, flags, 3071*d4665eaaSJeff Roberson item)); 3072*d4665eaaSJeff Roberson } 3073*d4665eaaSJeff Roberson } while (cache_alloc(zone, cache, NULL, flags)); 3074*d4665eaaSJeff Roberson critical_exit(); 3075*d4665eaaSJeff Roberson 3076*d4665eaaSJeff Roberson return (uma_zalloc_single(zone, NULL, flags)); 3077*d4665eaaSJeff Roberson } 3078*d4665eaaSJeff Roberson 30799c2cd7e5SJeff Roberson /* See uma.h */ 30808355f576SJeff Roberson void * 30812cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 30828355f576SJeff Roberson { 3083376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3084ab3185d1SJeff Roberson uma_cache_t cache; 3085ab3185d1SJeff Roberson void *item; 3086*d4665eaaSJeff Roberson int size, uz_flags; 30878355f576SJeff Roberson 3088e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 308919fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 309010cb2424SMark Murray 30918355f576SJeff Roberson /* This is the fast path allocation */ 3092e63a1c2fSRyan Libby CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name, 3093e63a1c2fSRyan Libby zone, flags); 3094a553d4b8SJeff Roberson 3095*d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3096*d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 3097*d4665eaaSJeff Roberson ("uma_zalloc_arg: called with SMR zone.\n")); 3098*d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN) 30998d689e04SGleb Smirnoff return (item); 31008d689e04SGleb Smirnoff #endif 3101*d4665eaaSJeff Roberson 31025d1ae027SRobert Watson /* 31035d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 31045d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 31055d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 31065d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 31075d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 31085d1ae027SRobert Watson * preemption and migration. We release the critical section in 31095d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 31105d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 31115d1ae027SRobert Watson * must detect and handle migration if it has occurred. 31125d1ae027SRobert Watson */ 31135d1ae027SRobert Watson critical_enter(); 3114beb8beefSJeff Roberson do { 3115cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3116376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 3117cc7ce83aSJeff Roberson size = cache_uz_size(cache); 3118cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3119376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 3120376b1ba3SJeff Roberson item = cache_bucket_pop(cache, bucket); 31215d1ae027SRobert Watson critical_exit(); 3122*d4665eaaSJeff Roberson return (item_ctor(zone, uz_flags, size, udata, flags, 3123*d4665eaaSJeff Roberson item)); 3124b23f72e9SBrian Feldman } 3125beb8beefSJeff Roberson } while (cache_alloc(zone, cache, udata, flags)); 3126beb8beefSJeff Roberson critical_exit(); 3127beb8beefSJeff Roberson 3128*d4665eaaSJeff Roberson return (uma_zalloc_single(zone, udata, flags)); 3129fc03d22bSJeff Roberson } 3130fc03d22bSJeff Roberson 31318355f576SJeff Roberson /* 3132beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 3133beb8beefSJeff Roberson * a critical section. Returns in a critical section. 3134beb8beefSJeff Roberson * 31354bd61e19SJeff Roberson * A false return value indicates an allocation failure. 31364bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 3137beb8beefSJeff Roberson */ 3138beb8beefSJeff Roberson static __noinline bool 3139beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3140beb8beefSJeff Roberson { 3141beb8beefSJeff Roberson uma_zone_domain_t zdom; 3142beb8beefSJeff Roberson uma_bucket_t bucket; 3143cc7ce83aSJeff Roberson int domain; 3144beb8beefSJeff Roberson bool lockfail; 3145beb8beefSJeff Roberson 3146beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3147beb8beefSJeff Roberson 3148beb8beefSJeff Roberson /* 3149beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3150beb8beefSJeff Roberson * if we can switch with the free bucket. 3151*d4665eaaSJeff Roberson * 3152*d4665eaaSJeff Roberson * SMR Zones can't re-use the free bucket until the sequence has 3153*d4665eaaSJeff Roberson * expired. 31548355f576SJeff Roberson */ 3155*d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && 3156*d4665eaaSJeff Roberson cache->uc_freebucket.ucb_cnt != 0) { 3157*d4665eaaSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 3158*d4665eaaSJeff Roberson &cache->uc_allocbucket); 3159beb8beefSJeff Roberson return (true); 31608355f576SJeff Roberson } 3161fc03d22bSJeff Roberson 3162fc03d22bSJeff Roberson /* 3163fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3164fc03d22bSJeff Roberson */ 3165376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3166fc03d22bSJeff Roberson critical_exit(); 3167fc03d22bSJeff Roberson if (bucket != NULL) 31686fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3169fc03d22bSJeff Roberson 31704bd61e19SJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 31714bd61e19SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) { 31724bd61e19SJeff Roberson critical_enter(); 31734bd61e19SJeff Roberson return (false); 31744bd61e19SJeff Roberson } 31754bd61e19SJeff Roberson 31765d1ae027SRobert Watson /* 31775d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 31785d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 31795d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 31805d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 31815d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 31825d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 31835d1ae027SRobert Watson * the critical section. 31845d1ae027SRobert Watson */ 3185fc03d22bSJeff Roberson lockfail = 0; 3186fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 3187fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 3188a553d4b8SJeff Roberson ZONE_LOCK(zone); 3189fc03d22bSJeff Roberson lockfail = 1; 3190fc03d22bSJeff Roberson } 3191beb8beefSJeff Roberson 3192fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 31934bd61e19SJeff Roberson critical_enter(); 31944bd61e19SJeff Roberson cache = &zone->uz_cpu[curcpu]; 3195376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket != NULL) { 3196fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 3197beb8beefSJeff Roberson return (true); 3198a553d4b8SJeff Roberson } 31998355f576SJeff Roberson 3200fc03d22bSJeff Roberson /* 3201fc03d22bSJeff Roberson * Check the zone's cache of buckets. 3202fc03d22bSJeff Roberson */ 3203dfe13344SJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) { 3204c1685086SJeff Roberson domain = PCPU_GET(domain); 3205ab3185d1SJeff Roberson zdom = &zone->uz_domain[domain]; 3206c1685086SJeff Roberson } else { 3207c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3208c1685086SJeff Roberson zdom = &zone->uz_domain[0]; 3209c1685086SJeff Roberson } 3210c1685086SJeff Roberson 321108cfa56eSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom)) != NULL) { 3212cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 3213a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 3214376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3215beb8beefSJeff Roberson return (true); 3216a553d4b8SJeff Roberson } 32175d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 32185d1ae027SRobert Watson critical_exit(); 3219bbee39c6SJeff Roberson 3220fc03d22bSJeff Roberson /* 3221fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 3222fc03d22bSJeff Roberson * handle the working set. 3223fc03d22bSJeff Roberson */ 322420a4e154SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 322520a4e154SJeff Roberson zone->uz_bucket_size++; 32264bd61e19SJeff Roberson ZONE_UNLOCK(zone); 3227bb15d1c7SGleb Smirnoff 32288355f576SJeff Roberson /* 3229beb8beefSJeff Roberson * Fill a bucket and attempt to use it as the alloc bucket. 3230bbee39c6SJeff Roberson */ 3231beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 32321431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 32331431a748SGleb Smirnoff zone->uz_name, zone, bucket); 32344bd61e19SJeff Roberson if (bucket == NULL) { 3235fc03d22bSJeff Roberson critical_enter(); 3236beb8beefSJeff Roberson return (false); 32374bd61e19SJeff Roberson } 32380f9b7bf3SMark Johnston 3239fc03d22bSJeff Roberson /* 3240fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3241fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3242fc03d22bSJeff Roberson * the memory directly. 3243fc03d22bSJeff Roberson */ 32444bd61e19SJeff Roberson ZONE_LOCK(zone); 32454bd61e19SJeff Roberson critical_enter(); 3246cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3247376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 3248dfe13344SJeff Roberson ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0 || 324981c0d72cSGleb Smirnoff domain == PCPU_GET(domain))) { 3250376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 32510f9b7bf3SMark Johnston zdom->uzd_imax += bucket->ub_cnt; 3252bb15d1c7SGleb Smirnoff } else if (zone->uz_bkt_count >= zone->uz_bkt_max) { 325381c0d72cSGleb Smirnoff critical_exit(); 325481c0d72cSGleb Smirnoff ZONE_UNLOCK(zone); 325581c0d72cSGleb Smirnoff bucket_drain(zone, bucket); 325681c0d72cSGleb Smirnoff bucket_free(zone, bucket, udata); 3257beb8beefSJeff Roberson critical_enter(); 3258beb8beefSJeff Roberson return (true); 325981c0d72cSGleb Smirnoff } else 32600f9b7bf3SMark Johnston zone_put_bucket(zone, zdom, bucket, false); 3261bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 3262beb8beefSJeff Roberson return (true); 3263bbee39c6SJeff Roberson } 3264bbee39c6SJeff Roberson 3265ab3185d1SJeff Roberson void * 3266ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3267bbee39c6SJeff Roberson { 3268ab3185d1SJeff Roberson 3269ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 327019fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3271ab3185d1SJeff Roberson 3272ab3185d1SJeff Roberson /* This is the fast path allocation */ 3273e63a1c2fSRyan Libby CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d", 3274e63a1c2fSRyan Libby zone->uz_name, zone, domain, flags); 3275ab3185d1SJeff Roberson 3276ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3277ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3278ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3279ab3185d1SJeff Roberson } 3280ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3281ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 3282ab3185d1SJeff Roberson 3283ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3284ab3185d1SJeff Roberson } 3285ab3185d1SJeff Roberson 3286ab3185d1SJeff Roberson /* 3287ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3288ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3289ab3185d1SJeff Roberson * 3290ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3291ab3185d1SJeff Roberson * only 'domain'. 3292ab3185d1SJeff Roberson */ 3293ab3185d1SJeff Roberson static uma_slab_t 3294194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3295ab3185d1SJeff Roberson { 3296ab3185d1SJeff Roberson uma_domain_t dom; 3297bbee39c6SJeff Roberson uma_slab_t slab; 3298ab3185d1SJeff Roberson int start; 3299ab3185d1SJeff Roberson 3300ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3301ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 33028b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3303ab3185d1SJeff Roberson 3304ab3185d1SJeff Roberson slab = NULL; 3305ab3185d1SJeff Roberson start = domain; 3306ab3185d1SJeff Roberson do { 3307ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 3308ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_part_slab)) 3309ab3185d1SJeff Roberson return (LIST_FIRST(&dom->ud_part_slab)); 3310ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_free_slab)) { 3311ab3185d1SJeff Roberson slab = LIST_FIRST(&dom->ud_free_slab); 3312ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 3313ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3314ab3185d1SJeff Roberson return (slab); 3315ab3185d1SJeff Roberson } 3316ab3185d1SJeff Roberson if (rr) 3317ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3318ab3185d1SJeff Roberson } while (domain != start); 3319ab3185d1SJeff Roberson 3320ab3185d1SJeff Roberson return (NULL); 3321ab3185d1SJeff Roberson } 3322ab3185d1SJeff Roberson 33238b987a77SJeff Roberson /* 33248b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 33258b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 33268b987a77SJeff Roberson */ 3327ab3185d1SJeff Roberson static uma_slab_t 3328194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3329ab3185d1SJeff Roberson { 33308b987a77SJeff Roberson uma_slab_t slab; 3331194a979eSMark Johnston uint32_t reserve; 3332099a0e58SBosko Milekic 33338b987a77SJeff Roberson /* HASH has a single free list. */ 333454c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 33358b987a77SJeff Roberson domain = 0; 3336194a979eSMark Johnston 33378b987a77SJeff Roberson KEG_LOCK(keg, domain); 3338194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 33398b987a77SJeff Roberson if (keg->uk_domain[domain].ud_free <= reserve || 33408b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 33418b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3342194a979eSMark Johnston return (NULL); 33438b987a77SJeff Roberson } 33448b987a77SJeff Roberson return (slab); 3345194a979eSMark Johnston } 3346194a979eSMark Johnston 3347194a979eSMark Johnston static uma_slab_t 3348194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3349194a979eSMark Johnston { 3350194a979eSMark Johnston struct vm_domainset_iter di; 3351194a979eSMark Johnston uma_slab_t slab; 3352194a979eSMark Johnston int aflags, domain; 3353194a979eSMark Johnston bool rr; 3354194a979eSMark Johnston 3355194a979eSMark Johnston restart: 3356bbee39c6SJeff Roberson /* 3357194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3358194a979eSMark Johnston * domain (as happens with first-touch zones). 3359194a979eSMark Johnston * 3360194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3361194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3362194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3363bbee39c6SJeff Roberson */ 3364ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3365ab3185d1SJeff Roberson if (rr) { 3366194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3367194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3368194a979eSMark Johnston &aflags); 3369194a979eSMark Johnston } else { 3370194a979eSMark Johnston aflags = flags; 3371194a979eSMark Johnston domain = rdomain; 3372194a979eSMark Johnston } 3373ab3185d1SJeff Roberson 3374194a979eSMark Johnston for (;;) { 3375194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3376584061b4SJeff Roberson if (slab != NULL) 3377bbee39c6SJeff Roberson return (slab); 3378bbee39c6SJeff Roberson 3379bbee39c6SJeff Roberson /* 3380bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3381bbee39c6SJeff Roberson */ 3382bbee39c6SJeff Roberson if (flags & M_NOVM) 3383bbee39c6SJeff Roberson break; 3384bbee39c6SJeff Roberson 338586220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 33868b987a77SJeff Roberson if (slab != NULL) 3387bbee39c6SJeff Roberson return (slab); 33883639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 33893639ac42SJeff Roberson break; 3390194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3391194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 3392194a979eSMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 3393194a979eSMark Johnston goto restart; 339430c5525bSAndrew Gallatin } 3395194a979eSMark Johnston break; 3396194a979eSMark Johnston } 3397ab3185d1SJeff Roberson } 3398ab3185d1SJeff Roberson 3399bbee39c6SJeff Roberson /* 3400bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3401bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3402bbee39c6SJeff Roberson * fail. 3403bbee39c6SJeff Roberson */ 34048b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 3405bbee39c6SJeff Roberson return (slab); 34068b987a77SJeff Roberson 3407ab3185d1SJeff Roberson return (NULL); 3408ab3185d1SJeff Roberson } 3409bbee39c6SJeff Roberson 3410d56368d7SBosko Milekic static void * 34110095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3412bbee39c6SJeff Roberson { 3413ab3185d1SJeff Roberson uma_domain_t dom; 3414bbee39c6SJeff Roberson void *item; 34159b8db4d0SRyan Libby int freei; 3416bbee39c6SJeff Roberson 34178b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3418099a0e58SBosko Milekic 34198b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 34209b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 34219b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 34221e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3423bbee39c6SJeff Roberson slab->us_freecount--; 34248b987a77SJeff Roberson dom->ud_free--; 3425ef72505eSJeff Roberson 3426bbee39c6SJeff Roberson /* Move this slab to the full list */ 3427bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3428bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3429ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3430bbee39c6SJeff Roberson } 3431bbee39c6SJeff Roberson 3432bbee39c6SJeff Roberson return (item); 3433bbee39c6SJeff Roberson } 3434bbee39c6SJeff Roberson 3435bbee39c6SJeff Roberson static int 3436b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 34370095a784SJeff Roberson { 34388b987a77SJeff Roberson uma_domain_t dom; 3439b75c4efcSAndrew Turner uma_zone_t zone; 34400095a784SJeff Roberson uma_slab_t slab; 34410095a784SJeff Roberson uma_keg_t keg; 3442a03af342SSean Bruno #ifdef NUMA 3443ab3185d1SJeff Roberson int stripe; 3444a03af342SSean Bruno #endif 34450095a784SJeff Roberson int i; 34460095a784SJeff Roberson 3447b75c4efcSAndrew Turner zone = arg; 34480095a784SJeff Roberson slab = NULL; 3449584061b4SJeff Roberson keg = zone->uz_keg; 3450af526374SJeff Roberson /* Try to keep the buckets totally full */ 34510095a784SJeff Roberson for (i = 0; i < max; ) { 3452584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 34530095a784SJeff Roberson break; 3454a03af342SSean Bruno #ifdef NUMA 3455ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3456a03af342SSean Bruno #endif 34578b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 34586fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 34590095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 34608b987a77SJeff Roberson if (dom->ud_free <= keg->uk_reserve) 34616fd34d6fSJeff Roberson break; 3462b6715dabSJeff Roberson #ifdef NUMA 3463ab3185d1SJeff Roberson /* 3464ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3465ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3466ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3467ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3468ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3469ab3185d1SJeff Roberson * time but yields better distribution. 3470ab3185d1SJeff Roberson */ 3471dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 && 3472ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3473ab3185d1SJeff Roberson break; 3474ab3185d1SJeff Roberson #endif 34756fd34d6fSJeff Roberson } 34768b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 3477ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 34780095a784SJeff Roberson flags &= ~M_WAITOK; 34790095a784SJeff Roberson flags |= M_NOWAIT; 34800095a784SJeff Roberson } 34810095a784SJeff Roberson 34820095a784SJeff Roberson return i; 34830095a784SJeff Roberson } 34840095a784SJeff Roberson 34854bd61e19SJeff Roberson static int 34864bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 34874bd61e19SJeff Roberson { 34884bd61e19SJeff Roberson uint64_t old, new, total, max; 34894bd61e19SJeff Roberson 34904bd61e19SJeff Roberson /* 34914bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 34924bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 34934bd61e19SJeff Roberson * fairness by keeping fifo order. 34944bd61e19SJeff Roberson * 34954bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 34964bd61e19SJeff Roberson */ 34974bd61e19SJeff Roberson for (;;) { 34984bd61e19SJeff Roberson zone_free_limit(zone, count); 34994bd61e19SJeff Roberson zone_log_warning(zone); 35004bd61e19SJeff Roberson zone_maxaction(zone); 35014bd61e19SJeff Roberson if (flags & M_NOWAIT) 35024bd61e19SJeff Roberson return (0); 35034bd61e19SJeff Roberson 35044bd61e19SJeff Roberson /* 35054bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 35064bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 35074bd61e19SJeff Roberson * is essentially a home rolled semaphore. 35084bd61e19SJeff Roberson */ 35094bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 35104bd61e19SJeff Roberson old = zone->uz_items; 35114bd61e19SJeff Roberson do { 35124bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 35134bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 35144bd61e19SJeff Roberson max = zone->uz_max_items; 35154bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 35164bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 35174bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 35184bd61e19SJeff Roberson else 35194bd61e19SJeff Roberson new = old + MIN(count, max - old); 35204bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 35214bd61e19SJeff Roberson 35224bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 35234bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 35244bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 35254bd61e19SJeff Roberson return (new - old); 35264bd61e19SJeff Roberson } 35274bd61e19SJeff Roberson 35284bd61e19SJeff Roberson /* 35294bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 35304bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 35314bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 35324bd61e19SJeff Roberson * transitions. 35334bd61e19SJeff Roberson */ 35344bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 35354bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 35364bd61e19SJeff Roberson 35374bd61e19SJeff Roberson /* 35384bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 35394bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 35404bd61e19SJeff Roberson */ 35414bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 35424bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 35434bd61e19SJeff Roberson 35444bd61e19SJeff Roberson /* 35454bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 35464bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 35474bd61e19SJeff Roberson * 35484bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 35494bd61e19SJeff Roberson * our count. 35504bd61e19SJeff Roberson */ 35514bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 35524bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 35534bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 35544bd61e19SJeff Roberson /* We're no longer a sleeper. */ 35554bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 35564bd61e19SJeff Roberson 35574bd61e19SJeff Roberson /* 35584bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 35594bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 35604bd61e19SJeff Roberson * against changes via sysctl. 35614bd61e19SJeff Roberson */ 35624bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 35634bd61e19SJeff Roberson max = zone->uz_max_items; 35644bd61e19SJeff Roberson if (total >= max) 35654bd61e19SJeff Roberson continue; 35664bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 35674bd61e19SJeff Roberson if (total + count > max) { 35684bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 35694bd61e19SJeff Roberson count = max - total; 35704bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 35714bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 35724bd61e19SJeff Roberson 35734bd61e19SJeff Roberson return (count); 35744bd61e19SJeff Roberson } 35754bd61e19SJeff Roberson } 35764bd61e19SJeff Roberson 35774bd61e19SJeff Roberson /* 35784bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 35794bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 35804bd61e19SJeff Roberson * one item can be allocated. 35814bd61e19SJeff Roberson */ 35824bd61e19SJeff Roberson static int 35834bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 35844bd61e19SJeff Roberson { 35854bd61e19SJeff Roberson uint64_t old; 35864bd61e19SJeff Roberson uint64_t max; 35874bd61e19SJeff Roberson 35884bd61e19SJeff Roberson max = zone->uz_max_items; 35894bd61e19SJeff Roberson MPASS(max > 0); 35904bd61e19SJeff Roberson 35914bd61e19SJeff Roberson /* 35924bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 35934bd61e19SJeff Roberson * fetchadd. 35944bd61e19SJeff Roberson */ 35954bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 35964bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 35974bd61e19SJeff Roberson return (count); 35984bd61e19SJeff Roberson 35994bd61e19SJeff Roberson /* 36004bd61e19SJeff Roberson * If we had some items and no sleepers just return the 36014bd61e19SJeff Roberson * truncated value. We have to release the excess space 36024bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 36034bd61e19SJeff Roberson * because we were temporarily over the limit. 36044bd61e19SJeff Roberson */ 36054bd61e19SJeff Roberson if (old < max) { 36064bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 36074bd61e19SJeff Roberson return (max - old); 36084bd61e19SJeff Roberson } 36094bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 36104bd61e19SJeff Roberson } 36114bd61e19SJeff Roberson 36124bd61e19SJeff Roberson /* 36134bd61e19SJeff Roberson * Free a number of items back to the limit. 36144bd61e19SJeff Roberson */ 36154bd61e19SJeff Roberson static void 36164bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 36174bd61e19SJeff Roberson { 36184bd61e19SJeff Roberson uint64_t old; 36194bd61e19SJeff Roberson 36204bd61e19SJeff Roberson MPASS(count > 0); 36214bd61e19SJeff Roberson 36224bd61e19SJeff Roberson /* 36234bd61e19SJeff Roberson * In the common case we either have no sleepers or 36244bd61e19SJeff Roberson * are still over the limit and can just return. 36254bd61e19SJeff Roberson */ 36264bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 36274bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 36284bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 36294bd61e19SJeff Roberson return; 36304bd61e19SJeff Roberson 36314bd61e19SJeff Roberson /* 36324bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 36334bd61e19SJeff Roberson * to generate wakeups if necessary. 36344bd61e19SJeff Roberson */ 36354bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 36364bd61e19SJeff Roberson } 36374bd61e19SJeff Roberson 3638fc03d22bSJeff Roberson static uma_bucket_t 3639beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3640bbee39c6SJeff Roberson { 3641bbee39c6SJeff Roberson uma_bucket_t bucket; 3642beb8beefSJeff Roberson int maxbucket, cnt; 3643bbee39c6SJeff Roberson 3644e63a1c2fSRyan Libby CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name, 3645e63a1c2fSRyan Libby zone, domain); 364630c5525bSAndrew Gallatin 3647c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3648c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3649c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3650c1685086SJeff Roberson 36514bd61e19SJeff Roberson if (zone->uz_max_items > 0) 36524bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 36534bd61e19SJeff Roberson M_NOWAIT); 36544bd61e19SJeff Roberson else 365520a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 36564bd61e19SJeff Roberson if (maxbucket == 0) 36574bd61e19SJeff Roberson return (false); 3658beb8beefSJeff Roberson 36596fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 36606fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3661beb8beefSJeff Roberson if (bucket == NULL) { 3662beb8beefSJeff Roberson cnt = 0; 3663beb8beefSJeff Roberson goto out; 3664beb8beefSJeff Roberson } 36650095a784SJeff Roberson 36660095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3667beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 36680095a784SJeff Roberson 36690095a784SJeff Roberson /* 36700095a784SJeff Roberson * Initialize the memory if necessary. 36710095a784SJeff Roberson */ 36720095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 3673099a0e58SBosko Milekic int i; 3674bbee39c6SJeff Roberson 36750095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 3676e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 36770095a784SJeff Roberson flags) != 0) 3678b23f72e9SBrian Feldman break; 3679b23f72e9SBrian Feldman /* 3680b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 3681b23f72e9SBrian Feldman * rest back onto the freelist. 3682b23f72e9SBrian Feldman */ 3683b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 3684af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 36850095a784SJeff Roberson bucket->ub_cnt - i); 3686a5a262c6SBosko Milekic #ifdef INVARIANTS 36870095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 36880095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 3689a5a262c6SBosko Milekic #endif 3690b23f72e9SBrian Feldman bucket->ub_cnt = i; 3691b23f72e9SBrian Feldman } 3692099a0e58SBosko Milekic } 3693099a0e58SBosko Milekic 3694beb8beefSJeff Roberson cnt = bucket->ub_cnt; 3695f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 36966fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 36972efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 3698beb8beefSJeff Roberson bucket = NULL; 3699beb8beefSJeff Roberson } 3700beb8beefSJeff Roberson out: 37014bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 37024bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 3703fc03d22bSJeff Roberson 3704fc03d22bSJeff Roberson return (bucket); 3705fc03d22bSJeff Roberson } 3706fc03d22bSJeff Roberson 37078355f576SJeff Roberson /* 37080095a784SJeff Roberson * Allocates a single item from a zone. 37098355f576SJeff Roberson * 37108355f576SJeff Roberson * Arguments 37118355f576SJeff Roberson * zone The zone to alloc for. 37128355f576SJeff Roberson * udata The data to be passed to the constructor. 3713ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 3714a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 37158355f576SJeff Roberson * 37168355f576SJeff Roberson * Returns 37178355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 3718bbee39c6SJeff Roberson * An item if successful 37198355f576SJeff Roberson */ 37208355f576SJeff Roberson 37218355f576SJeff Roberson static void * 3722ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 37238355f576SJeff Roberson { 37248355f576SJeff Roberson void *item; 37258355f576SJeff Roberson 37264bd61e19SJeff Roberson if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) 3727bb15d1c7SGleb Smirnoff return (NULL); 37288355f576SJeff Roberson 3729c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3730c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 373130c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 3732c1685086SJeff Roberson 3733ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 3734beb8beefSJeff Roberson goto fail_cnt; 37358355f576SJeff Roberson 3736099a0e58SBosko Milekic /* 3737099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 3738099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 3739099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 3740099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 3741099a0e58SBosko Milekic */ 3742b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 3743e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 3744bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 3745beb8beefSJeff Roberson goto fail_cnt; 3746beb8beefSJeff Roberson } 3747beb8beefSJeff Roberson } 3748*d4665eaaSJeff Roberson item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags, 3749*d4665eaaSJeff Roberson item); 3750beb8beefSJeff Roberson if (item == NULL) 37510095a784SJeff Roberson goto fail; 37528355f576SJeff Roberson 37532efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 37541431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 37551431a748SGleb Smirnoff zone->uz_name, zone); 37561431a748SGleb Smirnoff 37578355f576SJeff Roberson return (item); 37580095a784SJeff Roberson 3759beb8beefSJeff Roberson fail_cnt: 3760beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 37610095a784SJeff Roberson fail: 37624bd61e19SJeff Roberson if (zone->uz_max_items > 0) 37634bd61e19SJeff Roberson zone_free_limit(zone, 1); 37641431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 37651431a748SGleb Smirnoff zone->uz_name, zone); 37664bd61e19SJeff Roberson 37670095a784SJeff Roberson return (NULL); 37688355f576SJeff Roberson } 37698355f576SJeff Roberson 37708355f576SJeff Roberson /* See uma.h */ 37718355f576SJeff Roberson void 3772*d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item) 3773*d4665eaaSJeff Roberson { 3774*d4665eaaSJeff Roberson uma_cache_t cache; 3775*d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 3776*d4665eaaSJeff Roberson int domain, itemdomain, uz_flags; 3777*d4665eaaSJeff Roberson 3778*d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3779*d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 3780*d4665eaaSJeff Roberson ("uma_zfree_smr: called with non-SMR zone.\n")); 3781*d4665eaaSJeff Roberson KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer.")); 3782*d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN) 3783*d4665eaaSJeff Roberson return; 3784*d4665eaaSJeff Roberson #endif 3785*d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3786*d4665eaaSJeff Roberson uz_flags = cache_uz_flags(cache); 3787*d4665eaaSJeff Roberson domain = itemdomain = 0; 3788*d4665eaaSJeff Roberson #ifdef NUMA 3789*d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 3790*d4665eaaSJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 3791*d4665eaaSJeff Roberson #endif 3792*d4665eaaSJeff Roberson critical_enter(); 3793*d4665eaaSJeff Roberson do { 3794*d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3795*d4665eaaSJeff Roberson /* SMR Zones must free to the free bucket. */ 3796*d4665eaaSJeff Roberson bucket = &cache->uc_freebucket; 3797*d4665eaaSJeff Roberson #ifdef NUMA 3798*d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3799*d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 3800*d4665eaaSJeff Roberson domain != itemdomain) { 3801*d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 3802*d4665eaaSJeff Roberson } 3803*d4665eaaSJeff Roberson #endif 3804*d4665eaaSJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3805*d4665eaaSJeff Roberson cache_bucket_push(cache, bucket, item); 3806*d4665eaaSJeff Roberson critical_exit(); 3807*d4665eaaSJeff Roberson return; 3808*d4665eaaSJeff Roberson } 3809*d4665eaaSJeff Roberson } while (cache_free(zone, cache, NULL, item, itemdomain)); 3810*d4665eaaSJeff Roberson critical_exit(); 3811*d4665eaaSJeff Roberson 3812*d4665eaaSJeff Roberson /* 3813*d4665eaaSJeff Roberson * If nothing else caught this, we'll just do an internal free. 3814*d4665eaaSJeff Roberson */ 3815*d4665eaaSJeff Roberson zone_free_item(zone, item, NULL, SKIP_NONE); 3816*d4665eaaSJeff Roberson } 3817*d4665eaaSJeff Roberson 3818*d4665eaaSJeff Roberson /* See uma.h */ 3819*d4665eaaSJeff Roberson void 38208355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 38218355f576SJeff Roberson { 38228355f576SJeff Roberson uma_cache_t cache; 3823376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3824cc7ce83aSJeff Roberson int domain, itemdomain, uz_flags; 38258355f576SJeff Roberson 3826e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 382719fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 382810cb2424SMark Murray 3829e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone); 38303659f747SRobert Watson 3831*d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 3832*d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 3833*d4665eaaSJeff Roberson ("uma_zfree_arg: called with SMR zone.\n")); 3834*d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN) 3835*d4665eaaSJeff Roberson return; 3836*d4665eaaSJeff Roberson #endif 383720ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 383820ed0cb0SMatthew D Fleming if (item == NULL) 383920ed0cb0SMatthew D Fleming return; 3840cc7ce83aSJeff Roberson 3841cc7ce83aSJeff Roberson /* 3842cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 3843cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 3844cc7ce83aSJeff Roberson * will simply read another cpu's line. 3845cc7ce83aSJeff Roberson */ 3846cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3847cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3848*d4665eaaSJeff Roberson if (UMA_ALWAYS_CTORDTOR || 3849*d4665eaaSJeff Roberson __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0)) 3850cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 3851ef72505eSJeff Roberson 3852af7f9b97SJeff Roberson /* 3853af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 3854af7f9b97SJeff Roberson * a little longer for the limits to be reset. 3855af7f9b97SJeff Roberson */ 3856cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 3857bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0) 3858fc03d22bSJeff Roberson goto zfree_item; 3859cc7ce83aSJeff Roberson } 3860af7f9b97SJeff Roberson 38615d1ae027SRobert Watson /* 38625d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 38635d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 38645d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 38655d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 38665d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 38675d1ae027SRobert Watson * preemption and migration. We release the critical section in 38685d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 38695d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 38705d1ae027SRobert Watson * detect and handle migration if it has occurred. 38715d1ae027SRobert Watson */ 38720a81b439SJeff Roberson domain = itemdomain = 0; 3873dfe13344SJeff Roberson #ifdef NUMA 3874dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 3875dfe13344SJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 3876dfe13344SJeff Roberson #endif 38775d1ae027SRobert Watson critical_enter(); 38780a81b439SJeff Roberson do { 3879cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3880a553d4b8SJeff Roberson /* 3881dfe13344SJeff Roberson * Try to free into the allocbucket first to give LIFO 3882dfe13344SJeff Roberson * ordering for cache-hot datastructures. Spill over 3883dfe13344SJeff Roberson * into the freebucket if necessary. Alloc will swap 3884dfe13344SJeff Roberson * them if one runs dry. 3885a553d4b8SJeff Roberson */ 3886dfe13344SJeff Roberson bucket = &cache->uc_allocbucket; 3887*d4665eaaSJeff Roberson #ifdef NUMA 3888*d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3889*d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 3890*d4665eaaSJeff Roberson domain != itemdomain) { 3891*d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 3892*d4665eaaSJeff Roberson } else 3893*d4665eaaSJeff Roberson #endif 3894*d4665eaaSJeff Roberson if (bucket->ucb_cnt >= bucket->ucb_entries) 3895376b1ba3SJeff Roberson bucket = &cache->uc_freebucket; 3896376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3897376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 38985d1ae027SRobert Watson critical_exit(); 38998355f576SJeff Roberson return; 3900fc03d22bSJeff Roberson } 39010a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 39020a81b439SJeff Roberson critical_exit(); 3903fc03d22bSJeff Roberson 39048355f576SJeff Roberson /* 39050a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 39068355f576SJeff Roberson */ 39070a81b439SJeff Roberson zfree_item: 39080a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 39090a81b439SJeff Roberson } 3910fc03d22bSJeff Roberson 3911dfe13344SJeff Roberson #ifdef NUMA 391291d947bfSJeff Roberson /* 391391d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 391491d947bfSJeff Roberson * them. 391591d947bfSJeff Roberson */ 391691d947bfSJeff Roberson static void 391791d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 391891d947bfSJeff Roberson { 391991d947bfSJeff Roberson struct uma_bucketlist fullbuckets; 392091d947bfSJeff Roberson uma_zone_domain_t zdom; 392191d947bfSJeff Roberson uma_bucket_t b; 392291d947bfSJeff Roberson void *item; 392391d947bfSJeff Roberson int domain; 392491d947bfSJeff Roberson 392591d947bfSJeff Roberson CTR3(KTR_UMA, 392691d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 392791d947bfSJeff Roberson zone->uz_name, zone, bucket); 392891d947bfSJeff Roberson 392991d947bfSJeff Roberson TAILQ_INIT(&fullbuckets); 393091d947bfSJeff Roberson 393191d947bfSJeff Roberson /* 393291d947bfSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 393391d947bfSJeff Roberson * lock on the current crossfree bucket. A full matrix with 393491d947bfSJeff Roberson * per-domain locking could be used if necessary. 393591d947bfSJeff Roberson */ 393691d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 393791d947bfSJeff Roberson while (bucket->ub_cnt > 0) { 393891d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 393991d947bfSJeff Roberson domain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 394091d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 394191d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 394291d947bfSJeff Roberson zdom->uzd_cross = bucket_alloc(zone, udata, M_NOWAIT); 394391d947bfSJeff Roberson if (zdom->uzd_cross == NULL) 394491d947bfSJeff Roberson break; 394591d947bfSJeff Roberson } 394691d947bfSJeff Roberson zdom->uzd_cross->ub_bucket[zdom->uzd_cross->ub_cnt++] = item; 394791d947bfSJeff Roberson if (zdom->uzd_cross->ub_cnt == zdom->uzd_cross->ub_entries) { 394891d947bfSJeff Roberson TAILQ_INSERT_HEAD(&fullbuckets, zdom->uzd_cross, 394991d947bfSJeff Roberson ub_link); 395091d947bfSJeff Roberson zdom->uzd_cross = NULL; 395191d947bfSJeff Roberson } 395291d947bfSJeff Roberson bucket->ub_cnt--; 395391d947bfSJeff Roberson } 395491d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 395591d947bfSJeff Roberson if (!TAILQ_EMPTY(&fullbuckets)) { 395691d947bfSJeff Roberson ZONE_LOCK(zone); 395791d947bfSJeff Roberson while ((b = TAILQ_FIRST(&fullbuckets)) != NULL) { 3958*d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 3959*d4665eaaSJeff Roberson bucket->ub_seq = smr_current(zone->uz_smr); 396091d947bfSJeff Roberson TAILQ_REMOVE(&fullbuckets, b, ub_link); 396191d947bfSJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 396291d947bfSJeff Roberson ZONE_UNLOCK(zone); 396391d947bfSJeff Roberson bucket_drain(zone, b); 396491d947bfSJeff Roberson bucket_free(zone, b, udata); 396591d947bfSJeff Roberson ZONE_LOCK(zone); 396691d947bfSJeff Roberson } else { 396791d947bfSJeff Roberson domain = _vm_phys_domain( 396891d947bfSJeff Roberson pmap_kextract( 396991d947bfSJeff Roberson (vm_offset_t)b->ub_bucket[0])); 397091d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 397191d947bfSJeff Roberson zone_put_bucket(zone, zdom, b, true); 397291d947bfSJeff Roberson } 397391d947bfSJeff Roberson } 397491d947bfSJeff Roberson ZONE_UNLOCK(zone); 397591d947bfSJeff Roberson } 397691d947bfSJeff Roberson if (bucket->ub_cnt != 0) 397791d947bfSJeff Roberson bucket_drain(zone, bucket); 3978*d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 397991d947bfSJeff Roberson bucket_free(zone, bucket, udata); 398091d947bfSJeff Roberson } 398191d947bfSJeff Roberson #endif 398291d947bfSJeff Roberson 39830a81b439SJeff Roberson static void 39840a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 39850a81b439SJeff Roberson int domain, int itemdomain) 39860a81b439SJeff Roberson { 39870a81b439SJeff Roberson uma_zone_domain_t zdom; 39880a81b439SJeff Roberson 3989dfe13344SJeff Roberson #ifdef NUMA 39900a81b439SJeff Roberson /* 39910a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 39920a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 39930a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 399491d947bfSJeff Roberson * correct domains. 39950a81b439SJeff Roberson */ 39960a81b439SJeff Roberson if (domain != itemdomain && vm_ndomains > 2) { 399791d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 39980a81b439SJeff Roberson return; 39990a81b439SJeff Roberson } 40000a81b439SJeff Roberson #endif 400191d947bfSJeff Roberson 40020a81b439SJeff Roberson /* 40030a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 40040a81b439SJeff Roberson * 40050a81b439SJeff Roberson * We bump the uz count when the cache size is insufficient to 40060a81b439SJeff Roberson * handle the working set. 40070a81b439SJeff Roberson */ 40084d104ba0SAlexander Motin if (ZONE_TRYLOCK(zone) == 0) { 40094d104ba0SAlexander Motin /* Record contention to size the buckets. */ 40108355f576SJeff Roberson ZONE_LOCK(zone); 401120a4e154SJeff Roberson if (zone->uz_bucket_size < zone->uz_bucket_size_max) 401220a4e154SJeff Roberson zone->uz_bucket_size++; 40134d104ba0SAlexander Motin } 40148355f576SJeff Roberson 40150a81b439SJeff Roberson CTR3(KTR_UMA, 40160a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 40170a81b439SJeff Roberson zone->uz_name, zone, bucket); 40180a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 40190a81b439SJeff Roberson KASSERT(bucket->ub_cnt == bucket->ub_entries, 40200a81b439SJeff Roberson ("uma_zfree: Attempting to insert partial bucket onto the full list.\n")); 40210a81b439SJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 4022c1685086SJeff Roberson ZONE_UNLOCK(zone); 4023c1685086SJeff Roberson bucket_drain(zone, bucket); 4024c1685086SJeff Roberson bucket_free(zone, bucket, udata); 4025c1685086SJeff Roberson } else { 4026c1685086SJeff Roberson zdom = &zone->uz_domain[itemdomain]; 4027c1685086SJeff Roberson zone_put_bucket(zone, zdom, bucket, true); 4028c1685086SJeff Roberson ZONE_UNLOCK(zone); 4029c1685086SJeff Roberson } 40308355f576SJeff Roberson } 4031fc03d22bSJeff Roberson 40324d104ba0SAlexander Motin /* 40330a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 40340a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 40350a81b439SJeff Roberson * 40360a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 40370a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 40380a81b439SJeff Roberson * the caller should retry. 40394d104ba0SAlexander Motin */ 40400a81b439SJeff Roberson static __noinline bool 40410a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 40420a81b439SJeff Roberson int itemdomain) 40430a81b439SJeff Roberson { 4044dfe13344SJeff Roberson uma_cache_bucket_t cbucket; 4045*d4665eaaSJeff Roberson uma_bucket_t newbucket, bucket; 4046cc7ce83aSJeff Roberson int domain; 40470a81b439SJeff Roberson 40480a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 40490a81b439SJeff Roberson 4050*d4665eaaSJeff Roberson if (zone->uz_bucket_size == 0) 40510a81b439SJeff Roberson return false; 40520a81b439SJeff Roberson 4053cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4054*d4665eaaSJeff Roberson newbucket = NULL; 40550a81b439SJeff Roberson 40560a81b439SJeff Roberson /* 4057dfe13344SJeff Roberson * FIRSTTOUCH domains need to free to the correct zdom. When 4058dfe13344SJeff Roberson * enabled this is the zdom of the item. The bucket is the 4059dfe13344SJeff Roberson * cross bucket if the current domain and itemdomain do not match. 40600a81b439SJeff Roberson */ 4061dfe13344SJeff Roberson cbucket = &cache->uc_freebucket; 4062dfe13344SJeff Roberson #ifdef NUMA 4063dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 40640a81b439SJeff Roberson domain = PCPU_GET(domain); 40650a81b439SJeff Roberson if (domain != itemdomain) { 4066dfe13344SJeff Roberson cbucket = &cache->uc_crossbucket; 4067dfe13344SJeff Roberson if (cbucket->ucb_cnt != 0) 4068dfe13344SJeff Roberson atomic_add_64(&zone->uz_xdomain, 4069dfe13344SJeff Roberson cbucket->ucb_cnt); 4070dfe13344SJeff Roberson } 40710a81b439SJeff Roberson } else 40720a81b439SJeff Roberson #endif 4073dfe13344SJeff Roberson itemdomain = domain = 0; 4074dfe13344SJeff Roberson bucket = cache_bucket_unload(cbucket); 40750a81b439SJeff Roberson 40760a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 40770a81b439SJeff Roberson critical_exit(); 40780a81b439SJeff Roberson 4079*d4665eaaSJeff Roberson /* 4080*d4665eaaSJeff Roberson * Don't let SMR zones operate without a free bucket. Force 4081*d4665eaaSJeff Roberson * a synchronize and re-use this one. We will only degrade 4082*d4665eaaSJeff Roberson * to a synchronize every bucket_size items rather than every 4083*d4665eaaSJeff Roberson * item if we fail to allocate a bucket. 4084*d4665eaaSJeff Roberson */ 4085*d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) { 4086*d4665eaaSJeff Roberson if (bucket != NULL) 4087*d4665eaaSJeff Roberson bucket->ub_seq = smr_advance(zone->uz_smr); 4088*d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4089*d4665eaaSJeff Roberson if (newbucket == NULL && bucket != NULL) { 4090*d4665eaaSJeff Roberson bucket_drain(zone, bucket); 4091*d4665eaaSJeff Roberson newbucket = bucket; 4092*d4665eaaSJeff Roberson bucket = NULL; 4093*d4665eaaSJeff Roberson } 4094*d4665eaaSJeff Roberson } else if (!bucketdisable) 4095*d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4096*d4665eaaSJeff Roberson 40970a81b439SJeff Roberson if (bucket != NULL) 40980a81b439SJeff Roberson zone_free_bucket(zone, bucket, udata, domain, itemdomain); 4099a553d4b8SJeff Roberson 4100fc03d22bSJeff Roberson critical_enter(); 4101*d4665eaaSJeff Roberson if ((bucket = newbucket) == NULL) 41020a81b439SJeff Roberson return (false); 4103cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4104dfe13344SJeff Roberson #ifdef NUMA 4105fc03d22bSJeff Roberson /* 41060a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 41070a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 41080a81b439SJeff Roberson * the free bucket. 4109fc03d22bSJeff Roberson */ 4110dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 41110a81b439SJeff Roberson domain = PCPU_GET(domain); 4112376b1ba3SJeff Roberson if (domain != itemdomain && 4113376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 4114376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 41150a81b439SJeff Roberson return (true); 41160a81b439SJeff Roberson } 41170a81b439SJeff Roberson } 41180a81b439SJeff Roberson #endif 41190a81b439SJeff Roberson /* 41200a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 41210a81b439SJeff Roberson */ 4122376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 4123fc03d22bSJeff Roberson critical_exit(); 41246fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 41250a81b439SJeff Roberson critical_enter(); 41260a81b439SJeff Roberson } else 4127376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 41288355f576SJeff Roberson 41290a81b439SJeff Roberson return (true); 41308355f576SJeff Roberson } 41318355f576SJeff Roberson 4132ab3185d1SJeff Roberson void 4133ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata) 4134ab3185d1SJeff Roberson { 4135ab3185d1SJeff Roberson 4136ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 413719fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 4138ab3185d1SJeff Roberson 4139e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_domain zone %s(%p)", zone->uz_name, zone); 4140ab3185d1SJeff Roberson 4141ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 4142ab3185d1SJeff Roberson ("uma_zfree_domain: called with spinlock or critical section held")); 4143ab3185d1SJeff Roberson 4144ab3185d1SJeff Roberson /* uma_zfree(..., NULL) does nothing, to match free(9). */ 4145ab3185d1SJeff Roberson if (item == NULL) 4146ab3185d1SJeff Roberson return; 4147ab3185d1SJeff Roberson zone_free_item(zone, item, udata, SKIP_NONE); 4148ab3185d1SJeff Roberson } 4149ab3185d1SJeff Roberson 41508355f576SJeff Roberson static void 4151bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 41528355f576SJeff Roberson { 4153bb15d1c7SGleb Smirnoff uma_keg_t keg; 4154ab3185d1SJeff Roberson uma_domain_t dom; 41559b8db4d0SRyan Libby int freei; 4156099a0e58SBosko Milekic 4157bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 41588b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 4159ab3185d1SJeff Roberson 41608355f576SJeff Roberson /* Do we need to remove from any lists? */ 41618b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 4162099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 41638355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4164ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 41658355f576SJeff Roberson } else if (slab->us_freecount == 0) { 41668355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4167ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 41688355f576SJeff Roberson } 41698355f576SJeff Roberson 4170ef72505eSJeff Roberson /* Slab management. */ 41711e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 41729b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 41738355f576SJeff Roberson slab->us_freecount++; 41748355f576SJeff Roberson 4175ef72505eSJeff Roberson /* Keg statistics. */ 41768b987a77SJeff Roberson dom->ud_free++; 41770095a784SJeff Roberson } 41780095a784SJeff Roberson 41790095a784SJeff Roberson static void 4180b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 41810095a784SJeff Roberson { 41828b987a77SJeff Roberson struct mtx *lock; 4183b75c4efcSAndrew Turner uma_zone_t zone; 41840095a784SJeff Roberson uma_slab_t slab; 41850095a784SJeff Roberson uma_keg_t keg; 41860095a784SJeff Roberson uint8_t *mem; 41878b987a77SJeff Roberson void *item; 41880095a784SJeff Roberson int i; 41898355f576SJeff Roberson 4190b75c4efcSAndrew Turner zone = arg; 4191bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 41928b987a77SJeff Roberson lock = NULL; 419354c5ae80SRyan Libby if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0)) 41948b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 41950095a784SJeff Roberson for (i = 0; i < cnt; i++) { 41960095a784SJeff Roberson item = bucket[i]; 419754c5ae80SRyan Libby if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) { 41980095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 41998b987a77SJeff Roberson } else { 42008b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 420154c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0) 42028b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 42038b987a77SJeff Roberson else 42048b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 42058b987a77SJeff Roberson } 42068b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 42078b987a77SJeff Roberson if (lock != NULL) 42088b987a77SJeff Roberson mtx_unlock(lock); 42098b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 42108b987a77SJeff Roberson } 4211bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 42120095a784SJeff Roberson } 42138b987a77SJeff Roberson if (lock != NULL) 42148b987a77SJeff Roberson mtx_unlock(lock); 42158355f576SJeff Roberson } 42168355f576SJeff Roberson 42170095a784SJeff Roberson /* 42180095a784SJeff Roberson * Frees a single item to any zone. 42190095a784SJeff Roberson * 42200095a784SJeff Roberson * Arguments: 42210095a784SJeff Roberson * zone The zone to free to 42220095a784SJeff Roberson * item The item we're freeing 42230095a784SJeff Roberson * udata User supplied data for the dtor 42240095a784SJeff Roberson * skip Skip dtors and finis 42250095a784SJeff Roberson */ 42260095a784SJeff Roberson static void 42270095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 42280095a784SJeff Roberson { 4229c5deaf04SGleb Smirnoff 4230*d4665eaaSJeff Roberson /* 4231*d4665eaaSJeff Roberson * If a free is sent directly to an SMR zone we have to 4232*d4665eaaSJeff Roberson * synchronize immediately because the item can instantly 4233*d4665eaaSJeff Roberson * be reallocated. This should only happen in degenerate 4234*d4665eaaSJeff Roberson * cases when no memory is available for per-cpu caches. 4235*d4665eaaSJeff Roberson */ 4236*d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE) 4237*d4665eaaSJeff Roberson smr_synchronize(zone->uz_smr); 4238*d4665eaaSJeff Roberson 4239cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 42400095a784SJeff Roberson 42410095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 42420095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 42430095a784SJeff Roberson 42440095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4245bb15d1c7SGleb Smirnoff 4246bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4247bb15d1c7SGleb Smirnoff return; 4248bb15d1c7SGleb Smirnoff 42492efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 42502efcc8cbSGleb Smirnoff 42514bd61e19SJeff Roberson if (zone->uz_max_items > 0) 42524bd61e19SJeff Roberson zone_free_limit(zone, 1); 4253bb45b411SGleb Smirnoff } 42540095a784SJeff Roberson 42558355f576SJeff Roberson /* See uma.h */ 42561c6cae97SLawrence Stewart int 4257736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4258736ee590SJeff Roberson { 4259bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 4260003cf08bSMark Johnston int count; 4261bb15d1c7SGleb Smirnoff 42624bd61e19SJeff Roberson /* 42634bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 42644bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 42654bd61e19SJeff Roberson * way to clear a limit. 42664bd61e19SJeff Roberson */ 4267bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4268003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4269003cf08bSMark Johnston count = ubz != NULL ? ubz->ubz_entries : 0; 427020a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = count; 427120a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 427220a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4273bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4274cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4275cc7ce83aSJeff Roberson zone_update_caches(zone); 42764bd61e19SJeff Roberson /* We may need to wake waiters. */ 42774bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4278bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4279bb15d1c7SGleb Smirnoff 4280bb15d1c7SGleb Smirnoff return (nitems); 4281bb15d1c7SGleb Smirnoff } 4282bb15d1c7SGleb Smirnoff 4283bb15d1c7SGleb Smirnoff /* See uma.h */ 4284003cf08bSMark Johnston void 4285bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4286bb15d1c7SGleb Smirnoff { 4287003cf08bSMark Johnston struct uma_bucket_zone *ubz; 4288003cf08bSMark Johnston int bpcpu; 4289bb15d1c7SGleb Smirnoff 4290bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4291003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4292003cf08bSMark Johnston if (ubz != NULL) { 4293003cf08bSMark Johnston bpcpu = 2; 4294dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 4295003cf08bSMark Johnston /* Count the cross-domain bucket. */ 4296003cf08bSMark Johnston bpcpu++; 4297003cf08bSMark Johnston nitems -= ubz->ubz_entries * bpcpu * mp_ncpus; 429820a4e154SJeff Roberson zone->uz_bucket_size_max = ubz->ubz_entries; 4299003cf08bSMark Johnston } else { 430020a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 4301003cf08bSMark Johnston } 430220a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 430320a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4304bb15d1c7SGleb Smirnoff zone->uz_bkt_max = nitems; 4305bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4306736ee590SJeff Roberson } 4307736ee590SJeff Roberson 4308736ee590SJeff Roberson /* See uma.h */ 4309e49471b0SAndre Oppermann int 4310e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4311e49471b0SAndre Oppermann { 4312e49471b0SAndre Oppermann int nitems; 4313e49471b0SAndre Oppermann 4314727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4315e49471b0SAndre Oppermann 4316e49471b0SAndre Oppermann return (nitems); 4317e49471b0SAndre Oppermann } 4318e49471b0SAndre Oppermann 4319e49471b0SAndre Oppermann /* See uma.h */ 43202f891cd5SPawel Jakub Dawidek void 43212f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 43222f891cd5SPawel Jakub Dawidek { 43232f891cd5SPawel Jakub Dawidek 4324727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 43252f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 43262f891cd5SPawel Jakub Dawidek } 43272f891cd5SPawel Jakub Dawidek 43282f891cd5SPawel Jakub Dawidek /* See uma.h */ 432954503a13SJonathan T. Looney void 433054503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 433154503a13SJonathan T. Looney { 433254503a13SJonathan T. Looney 4333727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4334e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 433554503a13SJonathan T. Looney } 433654503a13SJonathan T. Looney 433754503a13SJonathan T. Looney /* See uma.h */ 4338c4ae7908SLawrence Stewart int 4339c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4340c4ae7908SLawrence Stewart { 4341c4ae7908SLawrence Stewart int64_t nitems; 4342c4ae7908SLawrence Stewart u_int i; 4343c4ae7908SLawrence Stewart 4344bfb6b7a1SJeff Roberson nitems = 0; 4345bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER) 43462efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 43472efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4348727c6918SJeff Roberson CPU_FOREACH(i) 4349727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4350727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4351c4ae7908SLawrence Stewart 4352c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4353c4ae7908SLawrence Stewart } 4354c4ae7908SLawrence Stewart 435520a4e154SJeff Roberson static uint64_t 435620a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 435720a4e154SJeff Roberson { 435820a4e154SJeff Roberson uint64_t nitems; 435920a4e154SJeff Roberson u_int i; 436020a4e154SJeff Roberson 4361bfb6b7a1SJeff Roberson nitems = 0; 4362bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER) 436320a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4364727c6918SJeff Roberson CPU_FOREACH(i) 4365727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 436620a4e154SJeff Roberson 436720a4e154SJeff Roberson return (nitems); 436820a4e154SJeff Roberson } 436920a4e154SJeff Roberson 437020a4e154SJeff Roberson static uint64_t 437120a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 437220a4e154SJeff Roberson { 437320a4e154SJeff Roberson uint64_t nitems; 437420a4e154SJeff Roberson u_int i; 437520a4e154SJeff Roberson 4376bfb6b7a1SJeff Roberson nitems = 0; 4377bfb6b7a1SJeff Roberson if (zone->uz_frees != EARLY_COUNTER) 437820a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4379727c6918SJeff Roberson CPU_FOREACH(i) 4380727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 438120a4e154SJeff Roberson 438220a4e154SJeff Roberson return (nitems); 438320a4e154SJeff Roberson } 438420a4e154SJeff Roberson 438531c251a0SJeff Roberson #ifdef INVARIANTS 438631c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */ 438731c251a0SJeff Roberson static uint64_t 438831c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg) 438931c251a0SJeff Roberson { 439031c251a0SJeff Roberson uma_zone_t z; 439131c251a0SJeff Roberson uint64_t nitems; 439231c251a0SJeff Roberson 439331c251a0SJeff Roberson nitems = 0; 439431c251a0SJeff Roberson LIST_FOREACH(z, &keg->uk_zones, uz_link) 439531c251a0SJeff Roberson nitems += uma_zone_get_allocs(z); 439631c251a0SJeff Roberson 439731c251a0SJeff Roberson return (nitems); 439831c251a0SJeff Roberson } 439931c251a0SJeff Roberson #endif 440031c251a0SJeff Roberson 4401c4ae7908SLawrence Stewart /* See uma.h */ 4402736ee590SJeff Roberson void 4403099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4404099a0e58SBosko Milekic { 4405e20a199fSJeff Roberson uma_keg_t keg; 4406e20a199fSJeff Roberson 4407bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4408727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4409e20a199fSJeff Roberson keg->uk_init = uminit; 4410099a0e58SBosko Milekic } 4411099a0e58SBosko Milekic 4412099a0e58SBosko Milekic /* See uma.h */ 4413099a0e58SBosko Milekic void 4414099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4415099a0e58SBosko Milekic { 4416e20a199fSJeff Roberson uma_keg_t keg; 4417e20a199fSJeff Roberson 4418bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4419727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4420e20a199fSJeff Roberson keg->uk_fini = fini; 4421099a0e58SBosko Milekic } 4422099a0e58SBosko Milekic 4423099a0e58SBosko Milekic /* See uma.h */ 4424099a0e58SBosko Milekic void 4425099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4426099a0e58SBosko Milekic { 4427af526374SJeff Roberson 4428727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4429099a0e58SBosko Milekic zone->uz_init = zinit; 4430099a0e58SBosko Milekic } 4431099a0e58SBosko Milekic 4432099a0e58SBosko Milekic /* See uma.h */ 4433099a0e58SBosko Milekic void 4434099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4435099a0e58SBosko Milekic { 4436af526374SJeff Roberson 4437727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4438099a0e58SBosko Milekic zone->uz_fini = zfini; 4439099a0e58SBosko Milekic } 4440099a0e58SBosko Milekic 4441099a0e58SBosko Milekic /* See uma.h */ 4442099a0e58SBosko Milekic void 44438355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 44448355f576SJeff Roberson { 44450095a784SJeff Roberson uma_keg_t keg; 4446e20a199fSJeff Roberson 4447bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4448727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 44490095a784SJeff Roberson keg->uk_freef = freef; 44508355f576SJeff Roberson } 44518355f576SJeff Roberson 44528355f576SJeff Roberson /* See uma.h */ 44538355f576SJeff Roberson void 44548355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 44558355f576SJeff Roberson { 4456e20a199fSJeff Roberson uma_keg_t keg; 4457e20a199fSJeff Roberson 4458bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4459727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4460e20a199fSJeff Roberson keg->uk_allocf = allocf; 44618355f576SJeff Roberson } 44628355f576SJeff Roberson 44638355f576SJeff Roberson /* See uma.h */ 44646fd34d6fSJeff Roberson void 4465*d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr) 4466*d4665eaaSJeff Roberson { 4467*d4665eaaSJeff Roberson 4468*d4665eaaSJeff Roberson ZONE_ASSERT_COLD(zone); 4469*d4665eaaSJeff Roberson 4470*d4665eaaSJeff Roberson zone->uz_flags |= UMA_ZONE_SMR; 4471*d4665eaaSJeff Roberson zone->uz_smr = smr; 4472*d4665eaaSJeff Roberson zone_update_caches(zone); 4473*d4665eaaSJeff Roberson } 4474*d4665eaaSJeff Roberson 4475*d4665eaaSJeff Roberson smr_t 4476*d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone) 4477*d4665eaaSJeff Roberson { 4478*d4665eaaSJeff Roberson 4479*d4665eaaSJeff Roberson return (zone->uz_smr); 4480*d4665eaaSJeff Roberson } 4481*d4665eaaSJeff Roberson 4482*d4665eaaSJeff Roberson /* See uma.h */ 4483*d4665eaaSJeff Roberson void 44846fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 44856fd34d6fSJeff Roberson { 44866fd34d6fSJeff Roberson uma_keg_t keg; 44876fd34d6fSJeff Roberson 4488bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4489727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 44906fd34d6fSJeff Roberson keg->uk_reserve = items; 44916fd34d6fSJeff Roberson } 44926fd34d6fSJeff Roberson 44936fd34d6fSJeff Roberson /* See uma.h */ 44948355f576SJeff Roberson int 4495a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 44968355f576SJeff Roberson { 4497099a0e58SBosko Milekic uma_keg_t keg; 44988355f576SJeff Roberson vm_offset_t kva; 44999ba30bcbSZbigniew Bodek u_int pages; 45008355f576SJeff Roberson 4501bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4502727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4503727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 45048355f576SJeff Roberson 450579c9f942SJeff Roberson pages = howmany(count, keg->uk_ipers) * keg->uk_ppera; 4506a553d4b8SJeff Roberson 4507a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4508a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4509a4915c21SAttilio Rao #else 4510a4915c21SAttilio Rao if (1) { 4511a4915c21SAttilio Rao #endif 451257223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4513d1f42ac2SAlan Cox if (kva == 0) 45148355f576SJeff Roberson return (0); 4515a4915c21SAttilio Rao } else 4516a4915c21SAttilio Rao kva = 0; 4517bb15d1c7SGleb Smirnoff 4518bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4519bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4520099a0e58SBosko Milekic keg->uk_kva = kva; 4521a4915c21SAttilio Rao keg->uk_offset = 0; 4522bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4523a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4524a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4525a4915c21SAttilio Rao #else 4526a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4527a4915c21SAttilio Rao #endif 4528cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4529cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4530cc7ce83aSJeff Roberson zone_update_caches(zone); 4531bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4532af526374SJeff Roberson 45338355f576SJeff Roberson return (1); 45348355f576SJeff Roberson } 45358355f576SJeff Roberson 45368355f576SJeff Roberson /* See uma.h */ 45378355f576SJeff Roberson void 45388355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 45398355f576SJeff Roberson { 4540920239efSMark Johnston struct vm_domainset_iter di; 4541ab3185d1SJeff Roberson uma_domain_t dom; 45428355f576SJeff Roberson uma_slab_t slab; 4543099a0e58SBosko Milekic uma_keg_t keg; 454486220393SMark Johnston int aflags, domain, slabs; 45458355f576SJeff Roberson 4546bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 454779c9f942SJeff Roberson slabs = howmany(items, keg->uk_ipers); 4548194a979eSMark Johnston while (slabs-- > 0) { 454986220393SMark Johnston aflags = M_NOWAIT; 455086220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 455186220393SMark Johnston &aflags); 455286220393SMark Johnston for (;;) { 455386220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 455486220393SMark Johnston aflags); 455586220393SMark Johnston if (slab != NULL) { 4556ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 45578b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 455886220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 455986220393SMark Johnston us_link); 45608b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 4561920239efSMark Johnston break; 45628355f576SJeff Roberson } 45638b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 456486220393SMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 456586220393SMark Johnston } 456686220393SMark Johnston } 456786220393SMark Johnston } 45688355f576SJeff Roberson 45698355f576SJeff Roberson /* See uma.h */ 457008cfa56eSMark Johnston void 457108cfa56eSMark Johnston uma_reclaim(int req) 45728355f576SJeff Roberson { 457344ec2b63SKonstantin Belousov 45741431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 457508cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 457686bbae32SJeff Roberson bucket_enable(); 457708cfa56eSMark Johnston 457808cfa56eSMark Johnston switch (req) { 457908cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 458020a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 458108cfa56eSMark Johnston break; 458208cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 458308cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 458420a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 458508cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 458608cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 458720a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4588a2de44abSAlexander Motin } 458908cfa56eSMark Johnston break; 459008cfa56eSMark Johnston default: 459108cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 459208cfa56eSMark Johnston } 45930f9b7bf3SMark Johnston 45948355f576SJeff Roberson /* 45958355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 45968355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 45978355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 45988355f576SJeff Roberson */ 45999b8db4d0SRyan Libby zone_drain(slabzones[0], NULL); 46009b8db4d0SRyan Libby zone_drain(slabzones[1], NULL); 4601cae33c14SJeff Roberson bucket_zone_drain(); 460208cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 46038355f576SJeff Roberson } 46048355f576SJeff Roberson 46052e47807cSJeff Roberson static volatile int uma_reclaim_needed; 460644ec2b63SKonstantin Belousov 460744ec2b63SKonstantin Belousov void 460844ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 460944ec2b63SKonstantin Belousov { 461044ec2b63SKonstantin Belousov 46112e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 46122e47807cSJeff Roberson wakeup(uma_reclaim); 461344ec2b63SKonstantin Belousov } 461444ec2b63SKonstantin Belousov 461544ec2b63SKonstantin Belousov void 461644ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 461744ec2b63SKonstantin Belousov { 461844ec2b63SKonstantin Belousov 461944ec2b63SKonstantin Belousov for (;;) { 462008cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4621200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 462208cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 46232e47807cSJeff Roberson hz); 462408cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 46259b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 462608cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4627200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 46282e47807cSJeff Roberson /* Don't fire more than once per-second. */ 46292e47807cSJeff Roberson pause("umarclslp", hz); 463044ec2b63SKonstantin Belousov } 463144ec2b63SKonstantin Belousov } 463244ec2b63SKonstantin Belousov 4633663b416fSJohn Baldwin /* See uma.h */ 463408cfa56eSMark Johnston void 463508cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 463608cfa56eSMark Johnston { 463708cfa56eSMark Johnston 463808cfa56eSMark Johnston switch (req) { 463908cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 464020a4e154SJeff Roberson zone_trim(zone, NULL); 464108cfa56eSMark Johnston break; 464208cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 464320a4e154SJeff Roberson zone_drain(zone, NULL); 464408cfa56eSMark Johnston break; 464508cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 464608cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 464720a4e154SJeff Roberson zone_drain(zone, NULL); 464808cfa56eSMark Johnston break; 464908cfa56eSMark Johnston default: 465008cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 465108cfa56eSMark Johnston } 465208cfa56eSMark Johnston } 465308cfa56eSMark Johnston 465408cfa56eSMark Johnston /* See uma.h */ 4655663b416fSJohn Baldwin int 4656663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 4657663b416fSJohn Baldwin { 4658663b416fSJohn Baldwin 4659727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 46606c125b8dSMohan Srinivasan } 46616c125b8dSMohan Srinivasan 46622e47807cSJeff Roberson unsigned long 46632e47807cSJeff Roberson uma_limit(void) 46642e47807cSJeff Roberson { 46652e47807cSJeff Roberson 46662e47807cSJeff Roberson return (uma_kmem_limit); 46672e47807cSJeff Roberson } 46682e47807cSJeff Roberson 46692e47807cSJeff Roberson void 46702e47807cSJeff Roberson uma_set_limit(unsigned long limit) 46712e47807cSJeff Roberson { 46722e47807cSJeff Roberson 46732e47807cSJeff Roberson uma_kmem_limit = limit; 46742e47807cSJeff Roberson } 46752e47807cSJeff Roberson 46762e47807cSJeff Roberson unsigned long 46772e47807cSJeff Roberson uma_size(void) 46782e47807cSJeff Roberson { 46792e47807cSJeff Roberson 4680058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 4681ad5b0f5bSJeff Roberson } 4682ad5b0f5bSJeff Roberson 4683ad5b0f5bSJeff Roberson long 4684ad5b0f5bSJeff Roberson uma_avail(void) 4685ad5b0f5bSJeff Roberson { 4686ad5b0f5bSJeff Roberson 4687058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 46882e47807cSJeff Roberson } 46892e47807cSJeff Roberson 4690a0d4b0aeSRobert Watson #ifdef DDB 46918355f576SJeff Roberson /* 46927a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 46937a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 46947a52a97eSRobert Watson * 46957a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 46967a52a97eSRobert Watson * per-CPU cache statistic. 46977a52a97eSRobert Watson * 46987a52a97eSRobert Watson */ 46997a52a97eSRobert Watson static void 47000f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 4701c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 47027a52a97eSRobert Watson { 47037a52a97eSRobert Watson uma_cache_t cache; 4704c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 47057a52a97eSRobert Watson int cachefree, cpu; 47067a52a97eSRobert Watson 4707c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 47087a52a97eSRobert Watson cachefree = 0; 47093aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 47107a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 4711376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 4712376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 4713376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 4714376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 47157a52a97eSRobert Watson allocs += cache->uc_allocs; 47167a52a97eSRobert Watson frees += cache->uc_frees; 47177a52a97eSRobert Watson } 47182efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 47192efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 4720bf965959SSean Bruno sleeps += z->uz_sleeps; 4721c1685086SJeff Roberson xdomain += z->uz_xdomain; 47227a52a97eSRobert Watson if (cachefreep != NULL) 47237a52a97eSRobert Watson *cachefreep = cachefree; 47247a52a97eSRobert Watson if (allocsp != NULL) 47257a52a97eSRobert Watson *allocsp = allocs; 47267a52a97eSRobert Watson if (freesp != NULL) 47277a52a97eSRobert Watson *freesp = frees; 4728bf965959SSean Bruno if (sleepsp != NULL) 4729bf965959SSean Bruno *sleepsp = sleeps; 4730c1685086SJeff Roberson if (xdomainp != NULL) 4731c1685086SJeff Roberson *xdomainp = xdomain; 47327a52a97eSRobert Watson } 4733a0d4b0aeSRobert Watson #endif /* DDB */ 47347a52a97eSRobert Watson 47357a52a97eSRobert Watson static int 47367a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 47377a52a97eSRobert Watson { 47387a52a97eSRobert Watson uma_keg_t kz; 47397a52a97eSRobert Watson uma_zone_t z; 47407a52a97eSRobert Watson int count; 47417a52a97eSRobert Watson 47427a52a97eSRobert Watson count = 0; 4743111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 47447a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 47457a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 47467a52a97eSRobert Watson count++; 47477a52a97eSRobert Watson } 4748b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4749b47acb0aSGleb Smirnoff count++; 4750b47acb0aSGleb Smirnoff 4751111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 47527a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 47537a52a97eSRobert Watson } 47547a52a97eSRobert Watson 4755b47acb0aSGleb Smirnoff static void 4756b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 4757b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 4758b47acb0aSGleb Smirnoff { 4759b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 4760b47acb0aSGleb Smirnoff uma_cache_t cache; 4761b47acb0aSGleb Smirnoff int i; 4762b47acb0aSGleb Smirnoff 4763b47acb0aSGleb Smirnoff 4764b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 4765b47acb0aSGleb Smirnoff zdom = &z->uz_domain[i]; 4766b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 4767b47acb0aSGleb Smirnoff } 4768b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 4769b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 4770b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 4771b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 4772c1685086SJeff Roberson uth->uth_xdomain = z->uz_xdomain; 47731de9724eSMark Johnston 4774b47acb0aSGleb Smirnoff /* 47751de9724eSMark Johnston * While it is not normally safe to access the cache bucket pointers 47761de9724eSMark Johnston * while not on the CPU that owns the cache, we only allow the pointers 47771de9724eSMark Johnston * to be exchanged without the zone lock held, not invalidated, so 47781de9724eSMark Johnston * accept the possible race associated with bucket exchange during 47791de9724eSMark Johnston * monitoring. Use atomic_load_ptr() to ensure that the bucket pointers 47801de9724eSMark Johnston * are loaded only once. 4781b47acb0aSGleb Smirnoff */ 4782b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 4783b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 4784b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 4785b47acb0aSGleb Smirnoff continue; 4786b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 4787376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 4788376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 4789376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 4790b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 4791b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 4792b47acb0aSGleb Smirnoff } 4793b47acb0aSGleb Smirnoff } 4794b47acb0aSGleb Smirnoff 47957a52a97eSRobert Watson static int 47967a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 47977a52a97eSRobert Watson { 47987a52a97eSRobert Watson struct uma_stream_header ush; 47997a52a97eSRobert Watson struct uma_type_header uth; 480063b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 48017a52a97eSRobert Watson struct sbuf sbuf; 48027a52a97eSRobert Watson uma_keg_t kz; 48037a52a97eSRobert Watson uma_zone_t z; 48044bd61e19SJeff Roberson uint64_t items; 48058b987a77SJeff Roberson uint32_t kfree, pages; 48064e657159SMatthew D Fleming int count, error, i; 48077a52a97eSRobert Watson 480800f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 480900f0e671SMatthew D Fleming if (error != 0) 481000f0e671SMatthew D Fleming return (error); 48114e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 48121eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 481363b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 48144e657159SMatthew D Fleming 4815404a593eSMatthew D Fleming count = 0; 4816111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 48177a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 48187a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 48197a52a97eSRobert Watson count++; 48207a52a97eSRobert Watson } 48217a52a97eSRobert Watson 4822b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4823b47acb0aSGleb Smirnoff count++; 4824b47acb0aSGleb Smirnoff 48257a52a97eSRobert Watson /* 48267a52a97eSRobert Watson * Insert stream header. 48277a52a97eSRobert Watson */ 48287a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 48297a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 4830ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 48317a52a97eSRobert Watson ush.ush_count = count; 48324e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 48337a52a97eSRobert Watson 48347a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 48358b987a77SJeff Roberson kfree = pages = 0; 48368b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 48378b987a77SJeff Roberson kfree += kz->uk_domain[i].ud_free; 48388b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 48398b987a77SJeff Roberson } 48407a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 48417a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 48427a52a97eSRobert Watson ZONE_LOCK(z); 4843cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 48447a52a97eSRobert Watson uth.uth_align = kz->uk_align; 48457a52a97eSRobert Watson uth.uth_size = kz->uk_size; 48467a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 48474bd61e19SJeff Roberson if (z->uz_max_items > 0) { 48484bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 48494bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 4850bb15d1c7SGleb Smirnoff kz->uk_ppera; 48514bd61e19SJeff Roberson } else 48528b987a77SJeff Roberson uth.uth_pages = pages; 4853f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 4854bb15d1c7SGleb Smirnoff kz->uk_ppera; 4855bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 48568b987a77SJeff Roberson uth.uth_keg_free = kfree; 4857cbbb4a00SRobert Watson 4858cbbb4a00SRobert Watson /* 4859cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 4860cbbb4a00SRobert Watson * on the keg's zone list. 4861cbbb4a00SRobert Watson */ 4862e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 4863cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 4864cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 4865b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 4866b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 48672450bbb8SRobert Watson ZONE_UNLOCK(z); 486863b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 486963b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 487063b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 48717a52a97eSRobert Watson } 48727a52a97eSRobert Watson } 4873b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 4874b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 4875b47acb0aSGleb Smirnoff ZONE_LOCK(z); 4876b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 4877b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 4878b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 4879b47acb0aSGleb Smirnoff ZONE_UNLOCK(z); 4880b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 4881b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 4882b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 4883b47acb0aSGleb Smirnoff } 4884b47acb0aSGleb Smirnoff 4885111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 48864e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 48874e657159SMatthew D Fleming sbuf_delete(&sbuf); 488863b5d112SKonstantin Belousov free(ups, M_TEMP); 48897a52a97eSRobert Watson return (error); 48907a52a97eSRobert Watson } 489148c5777eSRobert Watson 48920a5a3ccbSGleb Smirnoff int 48930a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 48940a5a3ccbSGleb Smirnoff { 48950a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 489616be9f54SGleb Smirnoff int error, max; 48970a5a3ccbSGleb Smirnoff 489816be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 48990a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 49000a5a3ccbSGleb Smirnoff if (error || !req->newptr) 49010a5a3ccbSGleb Smirnoff return (error); 49020a5a3ccbSGleb Smirnoff 49030a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 49040a5a3ccbSGleb Smirnoff 49050a5a3ccbSGleb Smirnoff return (0); 49060a5a3ccbSGleb Smirnoff } 49070a5a3ccbSGleb Smirnoff 49080a5a3ccbSGleb Smirnoff int 49090a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 49100a5a3ccbSGleb Smirnoff { 491120a4e154SJeff Roberson uma_zone_t zone; 49120a5a3ccbSGleb Smirnoff int cur; 49130a5a3ccbSGleb Smirnoff 491420a4e154SJeff Roberson /* 491520a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 491620a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 491720a4e154SJeff Roberson */ 491820a4e154SJeff Roberson if (arg2 == 0) 491920a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 492020a4e154SJeff Roberson else 492120a4e154SJeff Roberson zone = arg1; 49220a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 49230a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 49240a5a3ccbSGleb Smirnoff } 49250a5a3ccbSGleb Smirnoff 492620a4e154SJeff Roberson static int 492720a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 492820a4e154SJeff Roberson { 492920a4e154SJeff Roberson uma_zone_t zone = arg1; 493020a4e154SJeff Roberson uint64_t cur; 493120a4e154SJeff Roberson 493220a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 493320a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 493420a4e154SJeff Roberson } 493520a4e154SJeff Roberson 493620a4e154SJeff Roberson static int 493720a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 493820a4e154SJeff Roberson { 493920a4e154SJeff Roberson uma_zone_t zone = arg1; 494020a4e154SJeff Roberson uint64_t cur; 494120a4e154SJeff Roberson 494220a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 494320a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 494420a4e154SJeff Roberson } 494520a4e154SJeff Roberson 49466d204a6aSRyan Libby static int 49476d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 49486d204a6aSRyan Libby { 49496d204a6aSRyan Libby struct sbuf sbuf; 49506d204a6aSRyan Libby uma_zone_t zone = arg1; 49516d204a6aSRyan Libby int error; 49526d204a6aSRyan Libby 49536d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 49546d204a6aSRyan Libby if (zone->uz_flags != 0) 49556d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 49566d204a6aSRyan Libby else 49576d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 49586d204a6aSRyan Libby error = sbuf_finish(&sbuf); 49596d204a6aSRyan Libby sbuf_delete(&sbuf); 49606d204a6aSRyan Libby 49616d204a6aSRyan Libby return (error); 49626d204a6aSRyan Libby } 49636d204a6aSRyan Libby 4964f7af5015SRyan Libby static int 4965f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 4966f7af5015SRyan Libby { 4967f7af5015SRyan Libby uma_keg_t keg = arg1; 4968f7af5015SRyan Libby int avail, effpct, total; 4969f7af5015SRyan Libby 4970f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 497154c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 49729b8db4d0SRyan Libby total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize; 4973f7af5015SRyan Libby /* 4974f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 4975f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 4976f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 4977f7af5015SRyan Libby */ 4978f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 4979f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 4980f7af5015SRyan Libby avail *= mp_maxid + 1; 4981f7af5015SRyan Libby effpct = 100 * avail / total; 4982f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 4983f7af5015SRyan Libby } 4984f7af5015SRyan Libby 49854bd61e19SJeff Roberson static int 49864bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 49874bd61e19SJeff Roberson { 49884bd61e19SJeff Roberson uma_zone_t zone = arg1; 49894bd61e19SJeff Roberson uint64_t cur; 49904bd61e19SJeff Roberson 49914bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 49924bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 49934bd61e19SJeff Roberson } 49944bd61e19SJeff Roberson 49959542ea7bSGleb Smirnoff #ifdef INVARIANTS 49969542ea7bSGleb Smirnoff static uma_slab_t 49979542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 49989542ea7bSGleb Smirnoff { 49999542ea7bSGleb Smirnoff uma_slab_t slab; 50009542ea7bSGleb Smirnoff uma_keg_t keg; 50019542ea7bSGleb Smirnoff uint8_t *mem; 50029542ea7bSGleb Smirnoff 50039542ea7bSGleb Smirnoff /* 50049542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 50059542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 50069542ea7bSGleb Smirnoff * essentially holds a reference. 50079542ea7bSGleb Smirnoff */ 5008727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 5009727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5010bb15d1c7SGleb Smirnoff return (NULL); 501154c5ae80SRyan Libby if (zone->uz_flags & UMA_ZFLAG_VTOSLAB) 5012727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 5013bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 501454c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0) 5015727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 50168b987a77SJeff Roberson KEG_LOCK(keg, 0); 50179542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 50188b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 50199542ea7bSGleb Smirnoff 50209542ea7bSGleb Smirnoff return (slab); 50219542ea7bSGleb Smirnoff } 50229542ea7bSGleb Smirnoff 5023c5deaf04SGleb Smirnoff static bool 5024c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 5025c5deaf04SGleb Smirnoff { 5026c5deaf04SGleb Smirnoff 5027727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5028c5deaf04SGleb Smirnoff return (true); 5029c5deaf04SGleb Smirnoff 5030bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 5031c5deaf04SGleb Smirnoff } 5032c5deaf04SGleb Smirnoff 5033c5deaf04SGleb Smirnoff static bool 5034c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 5035c5deaf04SGleb Smirnoff { 5036c5deaf04SGleb Smirnoff uintptr_t idx; 5037c5deaf04SGleb Smirnoff 5038c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 5039c5deaf04SGleb Smirnoff return (true); 5040c5deaf04SGleb Smirnoff 5041c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 5042c5deaf04SGleb Smirnoff return (false); 5043c5deaf04SGleb Smirnoff 5044c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 5045c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 5046c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 5047c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 5048c5deaf04SGleb Smirnoff } 5049c5deaf04SGleb Smirnoff 5050c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 5051c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 5052c5deaf04SGleb Smirnoff return (true); 5053c5deaf04SGleb Smirnoff } 5054c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 5055c5deaf04SGleb Smirnoff 5056c5deaf04SGleb Smirnoff return (false); 5057c5deaf04SGleb Smirnoff } 5058c5deaf04SGleb Smirnoff 50599542ea7bSGleb Smirnoff /* 50609542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 50619542ea7bSGleb Smirnoff * 50629542ea7bSGleb Smirnoff */ 50639542ea7bSGleb Smirnoff static void 50649542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 50659542ea7bSGleb Smirnoff { 50669542ea7bSGleb Smirnoff uma_keg_t keg; 50679542ea7bSGleb Smirnoff int freei; 50689542ea7bSGleb Smirnoff 50699542ea7bSGleb Smirnoff if (slab == NULL) { 50709542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 50719542ea7bSGleb Smirnoff if (slab == NULL) 50729542ea7bSGleb Smirnoff panic("uma: item %p did not belong to zone %s\n", 50739542ea7bSGleb Smirnoff item, zone->uz_name); 50749542ea7bSGleb Smirnoff } 5075584061b4SJeff Roberson keg = zone->uz_keg; 50761e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 50779542ea7bSGleb Smirnoff 5078815db204SRyan Libby if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 50799542ea7bSGleb Smirnoff panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n", 50809542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 5081815db204SRyan Libby BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 50829542ea7bSGleb Smirnoff } 50839542ea7bSGleb Smirnoff 50849542ea7bSGleb Smirnoff /* 50859542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 50869542ea7bSGleb Smirnoff * and duplicate frees. 50879542ea7bSGleb Smirnoff * 50889542ea7bSGleb Smirnoff */ 50899542ea7bSGleb Smirnoff static void 50909542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 50919542ea7bSGleb Smirnoff { 50929542ea7bSGleb Smirnoff uma_keg_t keg; 50939542ea7bSGleb Smirnoff int freei; 50949542ea7bSGleb Smirnoff 50959542ea7bSGleb Smirnoff if (slab == NULL) { 50969542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 50979542ea7bSGleb Smirnoff if (slab == NULL) 50989542ea7bSGleb Smirnoff panic("uma: Freed item %p did not belong to zone %s\n", 50999542ea7bSGleb Smirnoff item, zone->uz_name); 51009542ea7bSGleb Smirnoff } 5101584061b4SJeff Roberson keg = zone->uz_keg; 51021e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 51039542ea7bSGleb Smirnoff 51049542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 51059542ea7bSGleb Smirnoff panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n", 51069542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 51079542ea7bSGleb Smirnoff 51081e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 51099542ea7bSGleb Smirnoff panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n", 51109542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 51119542ea7bSGleb Smirnoff 5112815db204SRyan Libby if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 51139542ea7bSGleb Smirnoff panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n", 51149542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 51159542ea7bSGleb Smirnoff 5116815db204SRyan Libby BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 51179542ea7bSGleb Smirnoff } 51189542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 51199542ea7bSGleb Smirnoff 512048c5777eSRobert Watson #ifdef DDB 512146d70077SConrad Meyer static int64_t 512246d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 51230223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 512448c5777eSRobert Watson { 512546d70077SConrad Meyer uint64_t frees; 51260f9b7bf3SMark Johnston int i; 512748c5777eSRobert Watson 512848c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 512946d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 51302efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 513146d70077SConrad Meyer *sleeps = z->uz_sleeps; 513246d70077SConrad Meyer *cachefree = 0; 513346d70077SConrad Meyer *xdomain = 0; 513448c5777eSRobert Watson } else 513546d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 513646d70077SConrad Meyer xdomain); 51378b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 51388b987a77SJeff Roberson *cachefree += z->uz_domain[i].uzd_nitems; 5139e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 514048c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 51418b987a77SJeff Roberson *cachefree += kz->uk_domain[i].ud_free; 51428b987a77SJeff Roberson } 514346d70077SConrad Meyer *used = *allocs - frees; 514446d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 514546d70077SConrad Meyer } 51460f9b7bf3SMark Johnston 514746d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 514846d70077SConrad Meyer { 514946d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 515046d70077SConrad Meyer uma_keg_t kz; 515146d70077SConrad Meyer uma_zone_t z; 515246d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 515346d70077SConrad Meyer long cachefree; 515446d70077SConrad Meyer /* variables for sorting */ 515546d70077SConrad Meyer uma_keg_t cur_keg; 515646d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 515746d70077SConrad Meyer int64_t cur_size, last_size, size; 515846d70077SConrad Meyer int ties; 515946d70077SConrad Meyer 516046d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 516146d70077SConrad Meyer if (modif[0] == 'i') { 516246d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 516346d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 516446d70077SConrad Meyer } else { 516546d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 516646d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 516746d70077SConrad Meyer } 516846d70077SConrad Meyer 516946d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 517046d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 517146d70077SConrad Meyer 517246d70077SConrad Meyer /* Sort the zones with largest size first. */ 517346d70077SConrad Meyer last_zone = NULL; 517446d70077SConrad Meyer last_size = INT64_MAX; 517546d70077SConrad Meyer for (;;) { 517646d70077SConrad Meyer cur_zone = NULL; 517746d70077SConrad Meyer cur_size = -1; 517846d70077SConrad Meyer ties = 0; 517946d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 518046d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 518146d70077SConrad Meyer /* 518246d70077SConrad Meyer * In the case of size ties, print out zones 518346d70077SConrad Meyer * in the order they are encountered. That is, 518446d70077SConrad Meyer * when we encounter the most recently output 518546d70077SConrad Meyer * zone, we have already printed all preceding 518646d70077SConrad Meyer * ties, and we must print all following ties. 518746d70077SConrad Meyer */ 518846d70077SConrad Meyer if (z == last_zone) { 518946d70077SConrad Meyer ties = 1; 519046d70077SConrad Meyer continue; 519146d70077SConrad Meyer } 519246d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 519346d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 519446d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 519546d70077SConrad Meyer { 519646d70077SConrad Meyer cur_size = size; 519746d70077SConrad Meyer cur_zone = z; 519846d70077SConrad Meyer cur_keg = kz; 519946d70077SConrad Meyer } 520046d70077SConrad Meyer } 520146d70077SConrad Meyer } 520246d70077SConrad Meyer if (cur_zone == NULL) 520346d70077SConrad Meyer break; 520446d70077SConrad Meyer 520546d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 520646d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 520746d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 520846d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 520946d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 521020a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 521120a4e154SJeff Roberson xdomain); 521246d70077SConrad Meyer 5213687c94aaSJohn Baldwin if (db_pager_quit) 5214687c94aaSJohn Baldwin return; 521546d70077SConrad Meyer last_zone = cur_zone; 521646d70077SConrad Meyer last_size = cur_size; 521748c5777eSRobert Watson } 521848c5777eSRobert Watson } 521903175483SAlexander Motin 522003175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 522103175483SAlexander Motin { 522203175483SAlexander Motin uma_zone_t z; 5223ab3185d1SJeff Roberson uint64_t allocs, frees; 52240f9b7bf3SMark Johnston long cachefree; 52250f9b7bf3SMark Johnston int i; 522603175483SAlexander Motin 522703175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 522803175483SAlexander Motin "Requests", "Bucket"); 522903175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 5230c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 52310f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 52320f9b7bf3SMark Johnston cachefree += z->uz_domain[i].uzd_nitems; 52330f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 523403175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 523503175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 523620a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 523703175483SAlexander Motin if (db_pager_quit) 523803175483SAlexander Motin return; 523903175483SAlexander Motin } 524003175483SAlexander Motin } 52419542ea7bSGleb Smirnoff #endif /* DDB */ 5242