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> 6109c8cb71SMark Johnston #include <sys/asan.h> 62ef72505eSJeff Roberson #include <sys/bitset.h> 63194a979eSMark Johnston #include <sys/domainset.h> 649b43bc27SAndriy Gapon #include <sys/eventhandler.h> 658355f576SJeff Roberson #include <sys/kernel.h> 668355f576SJeff Roberson #include <sys/types.h> 67ad5b0f5bSJeff Roberson #include <sys/limits.h> 688355f576SJeff Roberson #include <sys/queue.h> 698355f576SJeff Roberson #include <sys/malloc.h> 703659f747SRobert Watson #include <sys/ktr.h> 718355f576SJeff Roberson #include <sys/lock.h> 7210094910SMark Johnston #include <sys/msan.h> 738355f576SJeff Roberson #include <sys/mutex.h> 744c1cc01cSJohn Baldwin #include <sys/proc.h> 7510cb2424SMark Murray #include <sys/random.h> 7689f6b863SAttilio Rao #include <sys/rwlock.h> 777a52a97eSRobert Watson #include <sys/sbuf.h> 78a2de44abSAlexander Motin #include <sys/sched.h> 794bd61e19SJeff Roberson #include <sys/sleepqueue.h> 808355f576SJeff Roberson #include <sys/smp.h> 81d4665eaaSJeff Roberson #include <sys/smr.h> 8210094910SMark Johnston #include <sys/sysctl.h> 83e60b2fcbSGleb Smirnoff #include <sys/taskqueue.h> 8486bbae32SJeff Roberson #include <sys/vmmeter.h> 8586bbae32SJeff Roberson 868355f576SJeff Roberson #include <vm/vm.h> 876f3b523cSKonstantin Belousov #include <vm/vm_param.h> 88194a979eSMark Johnston #include <vm/vm_domainset.h> 898355f576SJeff Roberson #include <vm/vm_object.h> 908355f576SJeff Roberson #include <vm/vm_page.h> 91a4915c21SAttilio Rao #include <vm/vm_pageout.h> 92ab3185d1SJeff Roberson #include <vm/vm_phys.h> 9330c5525bSAndrew Gallatin #include <vm/vm_pagequeue.h> 948355f576SJeff Roberson #include <vm/vm_map.h> 958355f576SJeff Roberson #include <vm/vm_kern.h> 968355f576SJeff Roberson #include <vm/vm_extern.h> 976f3b523cSKonstantin Belousov #include <vm/vm_dumpset.h> 988355f576SJeff Roberson #include <vm/uma.h> 998355f576SJeff Roberson #include <vm/uma_int.h> 100639c9550SJeff Roberson #include <vm/uma_dbg.h> 1018355f576SJeff Roberson 10248c5777eSRobert Watson #include <ddb/ddb.h> 10348c5777eSRobert Watson 1048d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 1058d689e04SGleb Smirnoff #include <vm/memguard.h> 1068d689e04SGleb Smirnoff #endif 1078d689e04SGleb Smirnoff 108a81c400eSJeff Roberson #include <machine/md_var.h> 109a81c400eSJeff Roberson 110d4665eaaSJeff Roberson #ifdef INVARIANTS 111d4665eaaSJeff Roberson #define UMA_ALWAYS_CTORDTOR 1 112d4665eaaSJeff Roberson #else 113d4665eaaSJeff Roberson #define UMA_ALWAYS_CTORDTOR 0 114d4665eaaSJeff Roberson #endif 115d4665eaaSJeff Roberson 1168355f576SJeff Roberson /* 117ab3185d1SJeff Roberson * This is the zone and keg from which all zones are spawned. 1188355f576SJeff Roberson */ 119ab3185d1SJeff Roberson static uma_zone_t kegs; 120ab3185d1SJeff Roberson static uma_zone_t zones; 1218355f576SJeff Roberson 1229b8db4d0SRyan Libby /* 12354007ce8SMark Johnston * On INVARIANTS builds, the slab contains a second bitset of the same size, 12454007ce8SMark Johnston * "dbg_bits", which is laid out immediately after us_free. 12554007ce8SMark Johnston */ 12654007ce8SMark Johnston #ifdef INVARIANTS 12754007ce8SMark Johnston #define SLAB_BITSETS 2 12854007ce8SMark Johnston #else 12954007ce8SMark Johnston #define SLAB_BITSETS 1 13054007ce8SMark Johnston #endif 13154007ce8SMark Johnston 13254007ce8SMark Johnston /* 1339b8db4d0SRyan Libby * These are the two zones from which all offpage uma_slab_ts are allocated. 1349b8db4d0SRyan Libby * 1359b8db4d0SRyan Libby * One zone is for slab headers that can represent a larger number of items, 1369b8db4d0SRyan Libby * making the slabs themselves more efficient, and the other zone is for 1379b8db4d0SRyan Libby * headers that are smaller and represent fewer items, making the headers more 1389b8db4d0SRyan Libby * efficient. 1399b8db4d0SRyan Libby */ 1409b8db4d0SRyan Libby #define SLABZONE_SIZE(setsize) \ 1419b8db4d0SRyan Libby (sizeof(struct uma_hash_slab) + BITSET_SIZE(setsize) * SLAB_BITSETS) 1429b8db4d0SRyan Libby #define SLABZONE0_SETSIZE (PAGE_SIZE / 16) 1439b8db4d0SRyan Libby #define SLABZONE1_SETSIZE SLAB_MAX_SETSIZE 1449b8db4d0SRyan Libby #define SLABZONE0_SIZE SLABZONE_SIZE(SLABZONE0_SETSIZE) 1459b8db4d0SRyan Libby #define SLABZONE1_SIZE SLABZONE_SIZE(SLABZONE1_SETSIZE) 1469b8db4d0SRyan Libby static uma_zone_t slabzones[2]; 1478355f576SJeff Roberson 1488355f576SJeff Roberson /* 1498355f576SJeff Roberson * The initial hash tables come out of this zone so they can be allocated 1508355f576SJeff Roberson * prior to malloc coming up. 1518355f576SJeff Roberson */ 1528355f576SJeff Roberson static uma_zone_t hashzone; 1538355f576SJeff Roberson 1541e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */ 155e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1; 1561e319f6dSRobert Watson 157961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets"); 15820a4e154SJeff Roberson static MALLOC_DEFINE(M_UMA, "UMA", "UMA Misc"); 159961647dfSJeff Roberson 1608355f576SJeff Roberson /* 16186bbae32SJeff Roberson * Are we allowed to allocate buckets? 16286bbae32SJeff Roberson */ 16386bbae32SJeff Roberson static int bucketdisable = 1; 16486bbae32SJeff Roberson 165099a0e58SBosko Milekic /* Linked list of all kegs in the system */ 16613e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs); 1678355f576SJeff Roberson 16803175483SAlexander Motin /* Linked list of all cache-only zones in the system */ 16903175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones = 17003175483SAlexander Motin LIST_HEAD_INITIALIZER(uma_cachezones); 17103175483SAlexander Motin 172aabe13f1SMark Johnston /* 173aabe13f1SMark Johnston * Mutex for global lists: uma_kegs, uma_cachezones, and the per-keg list of 174aabe13f1SMark Johnston * zones. 175aabe13f1SMark Johnston */ 176fe933c1dSMateusz Guzik static struct rwlock_padalign __exclusive_cache_line uma_rwlock; 1778355f576SJeff Roberson 178aabe13f1SMark Johnston static struct sx uma_reclaim_lock; 179aabe13f1SMark Johnston 180ac0a6fd0SGleb Smirnoff /* 181a81c400eSJeff Roberson * First available virual address for boot time allocations. 182ac0a6fd0SGleb Smirnoff */ 183a81c400eSJeff Roberson static vm_offset_t bootstart; 184a81c400eSJeff Roberson static vm_offset_t bootmem; 1858355f576SJeff Roberson 186fbd95859SMark Johnston /* 187fbd95859SMark Johnston * kmem soft limit, initialized by uma_set_limit(). Ensure that early 188fbd95859SMark Johnston * allocations don't trigger a wakeup of the reclaim thread. 189fbd95859SMark Johnston */ 1906d6a03d7SJeff Roberson unsigned long uma_kmem_limit = LONG_MAX; 191fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_limit, CTLFLAG_RD, &uma_kmem_limit, 0, 192fbd95859SMark Johnston "UMA kernel memory soft limit"); 1936d6a03d7SJeff Roberson unsigned long uma_kmem_total; 194fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_total, CTLFLAG_RD, &uma_kmem_total, 0, 195fbd95859SMark Johnston "UMA kernel memory usage"); 1962e47807cSJeff Roberson 1978355f576SJeff Roberson /* Is the VM done starting up? */ 198860bb7a0SMark Johnston static enum { 199860bb7a0SMark Johnston BOOT_COLD, 200a81c400eSJeff Roberson BOOT_KVA, 201dc2b3205SMark Johnston BOOT_PCPU, 202860bb7a0SMark Johnston BOOT_RUNNING, 203860bb7a0SMark Johnston BOOT_SHUTDOWN, 204860bb7a0SMark Johnston } booted = BOOT_COLD; 2058355f576SJeff Roberson 206ef72505eSJeff Roberson /* 2079643769aSJeff Roberson * This is the handle used to schedule events that need to happen 2089643769aSJeff Roberson * outside of the allocation fast path. 2099643769aSJeff Roberson */ 2108355f576SJeff Roberson static struct callout uma_callout; 2119643769aSJeff Roberson #define UMA_TIMEOUT 20 /* Seconds for callout interval. */ 2128355f576SJeff Roberson 2138355f576SJeff Roberson /* 2148355f576SJeff Roberson * This structure is passed as the zone ctor arg so that I don't have to create 2158355f576SJeff Roberson * a special allocation function just for zones. 2168355f576SJeff Roberson */ 2178355f576SJeff Roberson struct uma_zctor_args { 218bb196eb4SMatthew D Fleming const char *name; 219c3bdc05fSAndrew R. Reiter size_t size; 2208355f576SJeff Roberson uma_ctor ctor; 2218355f576SJeff Roberson uma_dtor dtor; 2228355f576SJeff Roberson uma_init uminit; 2238355f576SJeff Roberson uma_fini fini; 2240095a784SJeff Roberson uma_import import; 2250095a784SJeff Roberson uma_release release; 2260095a784SJeff Roberson void *arg; 227099a0e58SBosko Milekic uma_keg_t keg; 228099a0e58SBosko Milekic int align; 22985dcf349SGleb Smirnoff uint32_t flags; 230099a0e58SBosko Milekic }; 231099a0e58SBosko Milekic 232099a0e58SBosko Milekic struct uma_kctor_args { 233099a0e58SBosko Milekic uma_zone_t zone; 234099a0e58SBosko Milekic size_t size; 235099a0e58SBosko Milekic uma_init uminit; 236099a0e58SBosko Milekic uma_fini fini; 2378355f576SJeff Roberson int align; 23885dcf349SGleb Smirnoff uint32_t flags; 2398355f576SJeff Roberson }; 2408355f576SJeff Roberson 241cae33c14SJeff Roberson struct uma_bucket_zone { 242cae33c14SJeff Roberson uma_zone_t ubz_zone; 243eaa17d42SRyan Libby const char *ubz_name; 244fc03d22bSJeff Roberson int ubz_entries; /* Number of items it can hold. */ 245fc03d22bSJeff Roberson int ubz_maxsize; /* Maximum allocation size per-item. */ 246cae33c14SJeff Roberson }; 247cae33c14SJeff Roberson 248f9d27e75SRobert Watson /* 249fc03d22bSJeff Roberson * Compute the actual number of bucket entries to pack them in power 250fc03d22bSJeff Roberson * of two sizes for more efficient space utilization. 251f9d27e75SRobert Watson */ 252fc03d22bSJeff Roberson #define BUCKET_SIZE(n) \ 253fc03d22bSJeff Roberson (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *)) 254fc03d22bSJeff Roberson 2551aa6c758SAlexander Motin #define BUCKET_MAX BUCKET_SIZE(256) 256fc03d22bSJeff Roberson 257fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = { 258e84130a0SJeff Roberson /* Literal bucket sizes. */ 259e84130a0SJeff Roberson { NULL, "2 Bucket", 2, 4096 }, 260e84130a0SJeff Roberson { NULL, "4 Bucket", 4, 3072 }, 261e84130a0SJeff Roberson { NULL, "8 Bucket", 8, 2048 }, 262e84130a0SJeff Roberson { NULL, "16 Bucket", 16, 1024 }, 263e84130a0SJeff Roberson /* Rounded down power of 2 sizes for efficiency. */ 264fc03d22bSJeff Roberson { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 265fc03d22bSJeff Roberson { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 266fc03d22bSJeff Roberson { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 2671aa6c758SAlexander Motin { NULL, "256 Bucket", BUCKET_SIZE(256), 64 }, 268fc03d22bSJeff Roberson { NULL, NULL, 0} 269fc03d22bSJeff Roberson }; 270cae33c14SJeff Roberson 2712019094aSRobert Watson /* 2722019094aSRobert Watson * Flags and enumerations to be passed to internal functions. 2732019094aSRobert Watson */ 274bb15d1c7SGleb Smirnoff enum zfreeskip { 275bb15d1c7SGleb Smirnoff SKIP_NONE = 0, 276bb15d1c7SGleb Smirnoff SKIP_CNT = 0x00000001, 277bb15d1c7SGleb Smirnoff SKIP_DTOR = 0x00010000, 278bb15d1c7SGleb Smirnoff SKIP_FINI = 0x00020000, 279bb15d1c7SGleb Smirnoff }; 280b23f72e9SBrian Feldman 2818355f576SJeff Roberson /* Prototypes.. */ 2828355f576SJeff Roberson 283a81c400eSJeff Roberson void uma_startup1(vm_offset_t); 284f4bef67cSGleb Smirnoff void uma_startup2(void); 285f4bef67cSGleb Smirnoff 286ab3185d1SJeff Roberson static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 287ab3185d1SJeff Roberson static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 288ab3059a8SMatt Macy static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 289ab3185d1SJeff Roberson static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 290ec0d8280SRyan Libby static void *contig_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 291f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t); 292ab3059a8SMatt Macy static void pcpu_page_free(void *, vm_size_t, uint8_t); 29386220393SMark Johnston static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int); 2949643769aSJeff Roberson static void cache_drain(uma_zone_t); 2958355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t); 296aabe13f1SMark Johnston static void bucket_cache_reclaim(uma_zone_t zone, bool, int); 2972760658bSAlexander Motin static bool bucket_cache_reclaim_domain(uma_zone_t, bool, bool, int); 298b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int); 299099a0e58SBosko Milekic static void keg_dtor(void *, int, void *); 3002760658bSAlexander Motin static void keg_drain(uma_keg_t keg, int domain); 301b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int); 3029c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *); 303d4665eaaSJeff Roberson static inline void item_dtor(uma_zone_t zone, void *item, int size, 304d4665eaaSJeff Roberson void *udata, enum zfreeskip skip); 305b23f72e9SBrian Feldman static int zero_init(void *, int, int); 306c6fd3e23SJeff Roberson static void zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 307c6fd3e23SJeff Roberson int itemdomain, bool ws); 30820a4e154SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t, void *), void *); 309a81c400eSJeff Roberson static void zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *), void *); 31020a4e154SJeff Roberson static void zone_timeout(uma_zone_t zone, void *); 3113b2f2cb8SAlexander Motin static int hash_alloc(struct uma_hash *, u_int); 3120aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *); 3130aef6126SJeff Roberson static void hash_free(struct uma_hash *hash); 3148355f576SJeff Roberson static void uma_timeout(void *); 315860bb7a0SMark Johnston static void uma_shutdown(void); 316ab3185d1SJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int, int); 3170095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 3184bd61e19SJeff Roberson static int zone_alloc_limit(uma_zone_t zone, int count, int flags); 3194bd61e19SJeff Roberson static void zone_free_limit(uma_zone_t zone, int count); 32086bbae32SJeff Roberson static void bucket_enable(void); 321cae33c14SJeff Roberson static void bucket_init(void); 3226fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int); 3236fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *); 324aabe13f1SMark Johnston static void bucket_zone_drain(int domain); 325beb8beefSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int); 3260095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 327bb15d1c7SGleb Smirnoff static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item); 32809c8cb71SMark Johnston static size_t slab_sizeof(int nitems); 329e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 33085dcf349SGleb Smirnoff uma_fini fini, int align, uint32_t flags); 331b75c4efcSAndrew Turner static int zone_import(void *, void **, int, int, int); 332b75c4efcSAndrew Turner static void zone_release(void *, void **, int); 333beb8beefSJeff Roberson static bool cache_alloc(uma_zone_t, uma_cache_t, void *, int); 3342cb67bd7SGleb Smirnoff static bool cache_free(uma_zone_t, uma_cache_t, void *, int); 335bbee39c6SJeff Roberson 3367a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 3377a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 33820a4e154SJeff Roberson static int sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS); 33920a4e154SJeff Roberson static int sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS); 3406d204a6aSRyan Libby static int sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS); 341f7af5015SRyan Libby static int sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS); 3424bd61e19SJeff Roberson static int sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS); 3438355f576SJeff Roberson 34431c251a0SJeff Roberson static uint64_t uma_zone_get_allocs(uma_zone_t zone); 34531c251a0SJeff Roberson 3467029da5cSPawel Biernacki static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 34733e5a1eaSRyan Libby "Memory allocation debugging"); 34833e5a1eaSRyan Libby 3499542ea7bSGleb Smirnoff #ifdef INVARIANTS 35031c251a0SJeff Roberson static uint64_t uma_keg_get_allocs(uma_keg_t zone); 351815db204SRyan Libby static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg); 352815db204SRyan Libby 353c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem); 354c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem); 3559542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item); 3569542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item); 357c5deaf04SGleb Smirnoff 358c5deaf04SGleb Smirnoff static u_int dbg_divisor = 1; 359c5deaf04SGleb Smirnoff SYSCTL_UINT(_vm_debug, OID_AUTO, divisor, 360c5deaf04SGleb Smirnoff CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0, 361c5deaf04SGleb Smirnoff "Debug & thrash every this item in memory allocator"); 362c5deaf04SGleb Smirnoff 363c5deaf04SGleb Smirnoff static counter_u64_t uma_dbg_cnt = EARLY_COUNTER; 364c5deaf04SGleb Smirnoff static counter_u64_t uma_skip_cnt = EARLY_COUNTER; 365c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD, 366c5deaf04SGleb Smirnoff &uma_dbg_cnt, "memory items debugged"); 367c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD, 368c5deaf04SGleb Smirnoff &uma_skip_cnt, "memory items skipped, not debugged"); 3699542ea7bSGleb Smirnoff #endif 3709542ea7bSGleb Smirnoff 3717029da5cSPawel Biernacki SYSCTL_NODE(_vm, OID_AUTO, uma, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 3727029da5cSPawel Biernacki "Universal Memory Allocator"); 37335ec24f3SRyan Libby 374a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_INT, 3757a52a97eSRobert Watson 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 3767a52a97eSRobert Watson 377a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_STRUCT, 3787a52a97eSRobert Watson 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 3797a52a97eSRobert Watson 3802f891cd5SPawel Jakub Dawidek static int zone_warnings = 1; 381af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0, 3822f891cd5SPawel Jakub Dawidek "Warn when UMA zones becomes full"); 3832f891cd5SPawel Jakub Dawidek 38433e5a1eaSRyan Libby static int multipage_slabs = 1; 38533e5a1eaSRyan Libby TUNABLE_INT("vm.debug.uma_multipage_slabs", &multipage_slabs); 38633e5a1eaSRyan Libby SYSCTL_INT(_vm_debug, OID_AUTO, uma_multipage_slabs, 38733e5a1eaSRyan Libby CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &multipage_slabs, 0, 38833e5a1eaSRyan Libby "UMA may choose larger slab sizes for better efficiency"); 38933e5a1eaSRyan Libby 39086bbae32SJeff Roberson /* 3919b8db4d0SRyan Libby * Select the slab zone for an offpage slab with the given maximum item count. 3929b8db4d0SRyan Libby */ 3939b8db4d0SRyan Libby static inline uma_zone_t 3949b8db4d0SRyan Libby slabzone(int ipers) 3959b8db4d0SRyan Libby { 3969b8db4d0SRyan Libby 3979b8db4d0SRyan Libby return (slabzones[ipers > SLABZONE0_SETSIZE]); 3989b8db4d0SRyan Libby } 3999b8db4d0SRyan Libby 4009b8db4d0SRyan Libby /* 40186bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 40286bbae32SJeff Roberson */ 40386bbae32SJeff Roberson static void 40486bbae32SJeff Roberson bucket_enable(void) 40586bbae32SJeff Roberson { 4063182660aSRyan Libby 407a81c400eSJeff Roberson KASSERT(booted >= BOOT_KVA, ("Bucket enable before init")); 408251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 40986bbae32SJeff Roberson } 41086bbae32SJeff Roberson 411dc2c7965SRobert Watson /* 412dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 413dc2c7965SRobert Watson * 414dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 415fc03d22bSJeff Roberson * of the header and an array of pointers. 416dc2c7965SRobert Watson */ 417cae33c14SJeff Roberson static void 418cae33c14SJeff Roberson bucket_init(void) 419cae33c14SJeff Roberson { 420cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 421cae33c14SJeff Roberson int size; 422cae33c14SJeff Roberson 423d74e6a1dSAlan Cox for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 424cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 425cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 426cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 427e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 428dfe13344SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | 429dfe13344SJeff Roberson UMA_ZONE_FIRSTTOUCH); 430cae33c14SJeff Roberson } 431cae33c14SJeff Roberson } 432cae33c14SJeff Roberson 433dc2c7965SRobert Watson /* 434dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 435dc2c7965SRobert Watson * to allocate the bucket. 436dc2c7965SRobert Watson */ 437dc2c7965SRobert Watson static struct uma_bucket_zone * 438dc2c7965SRobert Watson bucket_zone_lookup(int entries) 439dc2c7965SRobert Watson { 440fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 441dc2c7965SRobert Watson 442fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 443fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 444fc03d22bSJeff Roberson return (ubz); 445fc03d22bSJeff Roberson ubz--; 446fc03d22bSJeff Roberson return (ubz); 447fc03d22bSJeff Roberson } 448fc03d22bSJeff Roberson 449fc03d22bSJeff Roberson static int 450fc03d22bSJeff Roberson bucket_select(int size) 451fc03d22bSJeff Roberson { 452fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 453fc03d22bSJeff Roberson 454fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 455fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 456fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 457fc03d22bSJeff Roberson 458fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 459fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 460fc03d22bSJeff Roberson break; 461fc03d22bSJeff Roberson ubz--; 462fc03d22bSJeff Roberson return (ubz->ubz_entries); 463dc2c7965SRobert Watson } 464dc2c7965SRobert Watson 465cae33c14SJeff Roberson static uma_bucket_t 4666fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags) 467cae33c14SJeff Roberson { 468cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 469cae33c14SJeff Roberson uma_bucket_t bucket; 470cae33c14SJeff Roberson 471cae33c14SJeff Roberson /* 472d4665eaaSJeff Roberson * Don't allocate buckets early in boot. 473cae33c14SJeff Roberson */ 474d4665eaaSJeff Roberson if (__predict_false(booted < BOOT_KVA)) 475cae33c14SJeff Roberson return (NULL); 476a81c400eSJeff Roberson 4776fd34d6fSJeff Roberson /* 4786fd34d6fSJeff Roberson * To limit bucket recursion we store the original zone flags 4796fd34d6fSJeff Roberson * in a cookie passed via zalloc_arg/zfree_arg. This allows the 4806fd34d6fSJeff Roberson * NOVM flag to persist even through deep recursions. We also 4816fd34d6fSJeff Roberson * store ZFLAG_BUCKET once we have recursed attempting to allocate 4826fd34d6fSJeff Roberson * a bucket for a bucket zone so we do not allow infinite bucket 4836fd34d6fSJeff Roberson * recursion. This cookie will even persist to frees of unused 4846fd34d6fSJeff Roberson * buckets via the allocation path or bucket allocations in the 4856fd34d6fSJeff Roberson * free path. 4866fd34d6fSJeff Roberson */ 4876fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 4886fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 489e8a720feSAlexander Motin else { 490e8a720feSAlexander Motin if ((uintptr_t)udata & UMA_ZFLAG_BUCKET) 491e8a720feSAlexander Motin return (NULL); 4926fd34d6fSJeff Roberson udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET); 493e8a720feSAlexander Motin } 494bae55c4aSRyan Libby if (((uintptr_t)udata & UMA_ZONE_VM) != 0) 495af526374SJeff Roberson flags |= M_NOVM; 496f8b6c515SMark Johnston ubz = bucket_zone_lookup(atomic_load_16(&zone->uz_bucket_size)); 49720d3ab87SAlexander Motin if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0) 49820d3ab87SAlexander Motin ubz++; 4996fd34d6fSJeff Roberson bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags); 500cae33c14SJeff Roberson if (bucket) { 501cae33c14SJeff Roberson #ifdef INVARIANTS 502cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 503cae33c14SJeff Roberson #endif 504cae33c14SJeff Roberson bucket->ub_cnt = 0; 505f8b6c515SMark Johnston bucket->ub_entries = min(ubz->ubz_entries, 506f8b6c515SMark Johnston zone->uz_bucket_size_max); 507d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 508d4665eaaSJeff Roberson CTR3(KTR_UMA, "bucket_alloc: zone %s(%p) allocated bucket %p", 509d4665eaaSJeff Roberson zone->uz_name, zone, bucket); 510cae33c14SJeff Roberson } 511cae33c14SJeff Roberson 512cae33c14SJeff Roberson return (bucket); 513cae33c14SJeff Roberson } 514cae33c14SJeff Roberson 515cae33c14SJeff Roberson static void 5166fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata) 517cae33c14SJeff Roberson { 518cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 519cae33c14SJeff Roberson 520c6fd3e23SJeff Roberson if (bucket->ub_cnt != 0) 521c6fd3e23SJeff Roberson bucket_drain(zone, bucket); 522c6fd3e23SJeff Roberson 523fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 524fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 525d4665eaaSJeff Roberson KASSERT(bucket->ub_seq == SMR_SEQ_INVALID, 526d4665eaaSJeff Roberson ("bucket_free: Freeing an SMR bucket.")); 5276fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 5286fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 529dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 5306fd34d6fSJeff Roberson uma_zfree_arg(ubz->ubz_zone, bucket, udata); 531cae33c14SJeff Roberson } 532cae33c14SJeff Roberson 533cae33c14SJeff Roberson static void 534aabe13f1SMark Johnston bucket_zone_drain(int domain) 535cae33c14SJeff Roberson { 536cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 537cae33c14SJeff Roberson 538cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 539aabe13f1SMark Johnston uma_zone_reclaim_domain(ubz->ubz_zone, UMA_RECLAIM_DRAIN, 540aabe13f1SMark Johnston domain); 541cae33c14SJeff Roberson } 542cae33c14SJeff Roberson 54309c8cb71SMark Johnston #ifdef KASAN 5449a7c2de3SMark Johnston _Static_assert(UMA_SMALLEST_UNIT % KASAN_SHADOW_SCALE == 0, 5459a7c2de3SMark Johnston "Base UMA allocation size not a multiple of the KASAN scale factor"); 5469a7c2de3SMark Johnston 54709c8cb71SMark Johnston static void 54809c8cb71SMark Johnston kasan_mark_item_valid(uma_zone_t zone, void *item) 54909c8cb71SMark Johnston { 55009c8cb71SMark Johnston void *pcpu_item; 55109c8cb71SMark Johnston size_t sz, rsz; 55209c8cb71SMark Johnston int i; 55309c8cb71SMark Johnston 55409c8cb71SMark Johnston if ((zone->uz_flags & UMA_ZONE_NOKASAN) != 0) 55509c8cb71SMark Johnston return; 55609c8cb71SMark Johnston 55709c8cb71SMark Johnston sz = zone->uz_size; 55809c8cb71SMark Johnston rsz = roundup2(sz, KASAN_SHADOW_SCALE); 55909c8cb71SMark Johnston if ((zone->uz_flags & UMA_ZONE_PCPU) == 0) { 560b0dfc486SMark Johnston kasan_mark(item, sz, rsz, KASAN_GENERIC_REDZONE); 56109c8cb71SMark Johnston } else { 56209c8cb71SMark Johnston pcpu_item = zpcpu_base_to_offset(item); 56309c8cb71SMark Johnston for (i = 0; i <= mp_maxid; i++) 564b0dfc486SMark Johnston kasan_mark(zpcpu_get_cpu(pcpu_item, i), sz, rsz, 565b0dfc486SMark Johnston KASAN_GENERIC_REDZONE); 56609c8cb71SMark Johnston } 56709c8cb71SMark Johnston } 56809c8cb71SMark Johnston 56909c8cb71SMark Johnston static void 57009c8cb71SMark Johnston kasan_mark_item_invalid(uma_zone_t zone, void *item) 57109c8cb71SMark Johnston { 57209c8cb71SMark Johnston void *pcpu_item; 57309c8cb71SMark Johnston size_t sz; 57409c8cb71SMark Johnston int i; 57509c8cb71SMark Johnston 57609c8cb71SMark Johnston if ((zone->uz_flags & UMA_ZONE_NOKASAN) != 0) 57709c8cb71SMark Johnston return; 57809c8cb71SMark Johnston 57909c8cb71SMark Johnston sz = roundup2(zone->uz_size, KASAN_SHADOW_SCALE); 58009c8cb71SMark Johnston if ((zone->uz_flags & UMA_ZONE_PCPU) == 0) { 58109c8cb71SMark Johnston kasan_mark(item, 0, sz, KASAN_UMA_FREED); 58209c8cb71SMark Johnston } else { 58309c8cb71SMark Johnston pcpu_item = zpcpu_base_to_offset(item); 58409c8cb71SMark Johnston for (i = 0; i <= mp_maxid; i++) 585b0dfc486SMark Johnston kasan_mark(zpcpu_get_cpu(pcpu_item, i), 0, sz, 586b0dfc486SMark Johnston KASAN_UMA_FREED); 58709c8cb71SMark Johnston } 58809c8cb71SMark Johnston } 58909c8cb71SMark Johnston 59009c8cb71SMark Johnston static void 59109c8cb71SMark Johnston kasan_mark_slab_valid(uma_keg_t keg, void *mem) 59209c8cb71SMark Johnston { 59309c8cb71SMark Johnston size_t sz; 59409c8cb71SMark Johnston 59509c8cb71SMark Johnston if ((keg->uk_flags & UMA_ZONE_NOKASAN) == 0) { 59609c8cb71SMark Johnston sz = keg->uk_ppera * PAGE_SIZE; 59709c8cb71SMark Johnston kasan_mark(mem, sz, sz, 0); 59809c8cb71SMark Johnston } 59909c8cb71SMark Johnston } 60009c8cb71SMark Johnston 60109c8cb71SMark Johnston static void 60209c8cb71SMark Johnston kasan_mark_slab_invalid(uma_keg_t keg, void *mem) 60309c8cb71SMark Johnston { 60409c8cb71SMark Johnston size_t sz; 60509c8cb71SMark Johnston 60609c8cb71SMark Johnston if ((keg->uk_flags & UMA_ZONE_NOKASAN) == 0) { 60709c8cb71SMark Johnston if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 60809c8cb71SMark Johnston sz = keg->uk_ppera * PAGE_SIZE; 60909c8cb71SMark Johnston else 61009c8cb71SMark Johnston sz = keg->uk_pgoff; 61109c8cb71SMark Johnston kasan_mark(mem, 0, sz, KASAN_UMA_FREED); 61209c8cb71SMark Johnston } 61309c8cb71SMark Johnston } 61409c8cb71SMark Johnston #else /* !KASAN */ 61509c8cb71SMark Johnston static void 61609c8cb71SMark Johnston kasan_mark_item_valid(uma_zone_t zone __unused, void *item __unused) 61709c8cb71SMark Johnston { 61809c8cb71SMark Johnston } 61909c8cb71SMark Johnston 62009c8cb71SMark Johnston static void 62109c8cb71SMark Johnston kasan_mark_item_invalid(uma_zone_t zone __unused, void *item __unused) 62209c8cb71SMark Johnston { 62309c8cb71SMark Johnston } 62409c8cb71SMark Johnston 62509c8cb71SMark Johnston static void 62609c8cb71SMark Johnston kasan_mark_slab_valid(uma_keg_t keg __unused, void *mem __unused) 62709c8cb71SMark Johnston { 62809c8cb71SMark Johnston } 62909c8cb71SMark Johnston 63009c8cb71SMark Johnston static void 63109c8cb71SMark Johnston kasan_mark_slab_invalid(uma_keg_t keg __unused, void *mem __unused) 63209c8cb71SMark Johnston { 63309c8cb71SMark Johnston } 63409c8cb71SMark Johnston #endif /* KASAN */ 63509c8cb71SMark Johnston 63610094910SMark Johnston #ifdef KMSAN 63710094910SMark Johnston static inline void 63810094910SMark Johnston kmsan_mark_item_uninitialized(uma_zone_t zone, void *item) 63910094910SMark Johnston { 64010094910SMark Johnston void *pcpu_item; 64110094910SMark Johnston size_t sz; 64210094910SMark Johnston int i; 64310094910SMark Johnston 64410094910SMark Johnston if ((zone->uz_flags & 64510094910SMark Johnston (UMA_ZFLAG_CACHE | UMA_ZONE_SECONDARY | UMA_ZONE_MALLOC)) != 0) { 64610094910SMark Johnston /* 64710094910SMark Johnston * Cache zones should not be instrumented by default, as UMA 64810094910SMark Johnston * does not have enough information to do so correctly. 64910094910SMark Johnston * Consumers can mark items themselves if it makes sense to do 65010094910SMark Johnston * so. 65110094910SMark Johnston * 65210094910SMark Johnston * Items from secondary zones are initialized by the parent 65310094910SMark Johnston * zone and thus cannot safely be marked by UMA. 65410094910SMark Johnston * 65510094910SMark Johnston * malloc zones are handled directly by malloc(9) and friends, 65610094910SMark Johnston * since they can provide more precise origin tracking. 65710094910SMark Johnston */ 65810094910SMark Johnston return; 65910094910SMark Johnston } 66010094910SMark Johnston if (zone->uz_keg->uk_init != NULL) { 66110094910SMark Johnston /* 66210094910SMark Johnston * By definition, initialized items cannot be marked. The 66310094910SMark Johnston * best we can do is mark items from these zones after they 66410094910SMark Johnston * are freed to the keg. 66510094910SMark Johnston */ 66610094910SMark Johnston return; 66710094910SMark Johnston } 66810094910SMark Johnston 66910094910SMark Johnston sz = zone->uz_size; 67010094910SMark Johnston if ((zone->uz_flags & UMA_ZONE_PCPU) == 0) { 67110094910SMark Johnston kmsan_orig(item, sz, KMSAN_TYPE_UMA, KMSAN_RET_ADDR); 67210094910SMark Johnston kmsan_mark(item, sz, KMSAN_STATE_UNINIT); 67310094910SMark Johnston } else { 67410094910SMark Johnston pcpu_item = zpcpu_base_to_offset(item); 67510094910SMark Johnston for (i = 0; i <= mp_maxid; i++) { 67610094910SMark Johnston kmsan_orig(zpcpu_get_cpu(pcpu_item, i), sz, 67710094910SMark Johnston KMSAN_TYPE_UMA, KMSAN_RET_ADDR); 67810094910SMark Johnston kmsan_mark(zpcpu_get_cpu(pcpu_item, i), sz, 67910094910SMark Johnston KMSAN_STATE_INITED); 68010094910SMark Johnston } 68110094910SMark Johnston } 68210094910SMark Johnston } 68310094910SMark Johnston #else /* !KMSAN */ 68410094910SMark Johnston static inline void 68510094910SMark Johnston kmsan_mark_item_uninitialized(uma_zone_t zone __unused, void *item __unused) 68610094910SMark Johnston { 68710094910SMark Johnston } 68810094910SMark Johnston #endif /* KMSAN */ 68910094910SMark Johnston 69008cfa56eSMark Johnston /* 691c6fd3e23SJeff Roberson * Acquire the domain lock and record contention. 692c6fd3e23SJeff Roberson */ 693c6fd3e23SJeff Roberson static uma_zone_domain_t 694c6fd3e23SJeff Roberson zone_domain_lock(uma_zone_t zone, int domain) 695c6fd3e23SJeff Roberson { 696c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 697c6fd3e23SJeff Roberson bool lockfail; 698c6fd3e23SJeff Roberson 699c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 700c6fd3e23SJeff Roberson lockfail = false; 701c6fd3e23SJeff Roberson if (ZDOM_OWNED(zdom)) 702c6fd3e23SJeff Roberson lockfail = true; 703c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 704c6fd3e23SJeff Roberson /* This is unsynchronized. The counter does not need to be precise. */ 705c6fd3e23SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 706c6fd3e23SJeff Roberson zone->uz_bucket_size++; 707c6fd3e23SJeff Roberson return (zdom); 708c6fd3e23SJeff Roberson } 709c6fd3e23SJeff Roberson 710c6fd3e23SJeff Roberson /* 711fe835cbfSJeff Roberson * Search for the domain with the least cached items and return it if it 712fe835cbfSJeff Roberson * is out of balance with the preferred domain. 713c6fd3e23SJeff Roberson */ 714c6fd3e23SJeff Roberson static __noinline int 715c6fd3e23SJeff Roberson zone_domain_lowest(uma_zone_t zone, int pref) 716c6fd3e23SJeff Roberson { 717fe835cbfSJeff Roberson long least, nitems, prefitems; 718c6fd3e23SJeff Roberson int domain; 719c6fd3e23SJeff Roberson int i; 720c6fd3e23SJeff Roberson 721fe835cbfSJeff Roberson prefitems = least = LONG_MAX; 722c6fd3e23SJeff Roberson domain = 0; 723c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 724c6fd3e23SJeff Roberson nitems = ZDOM_GET(zone, i)->uzd_nitems; 725c6fd3e23SJeff Roberson if (nitems < least) { 726c6fd3e23SJeff Roberson domain = i; 727c6fd3e23SJeff Roberson least = nitems; 728c6fd3e23SJeff Roberson } 729fe835cbfSJeff Roberson if (domain == pref) 730fe835cbfSJeff Roberson prefitems = nitems; 731fe835cbfSJeff Roberson } 732fe835cbfSJeff Roberson if (prefitems < least * 2) 733fe835cbfSJeff Roberson return (pref); 734c6fd3e23SJeff Roberson 735c6fd3e23SJeff Roberson return (domain); 736c6fd3e23SJeff Roberson } 737c6fd3e23SJeff Roberson 738c6fd3e23SJeff Roberson /* 739c6fd3e23SJeff Roberson * Search for the domain with the most cached items and return it or the 740c6fd3e23SJeff Roberson * preferred domain if it has enough to proceed. 741c6fd3e23SJeff Roberson */ 742c6fd3e23SJeff Roberson static __noinline int 743c6fd3e23SJeff Roberson zone_domain_highest(uma_zone_t zone, int pref) 744c6fd3e23SJeff Roberson { 745c6fd3e23SJeff Roberson long most, nitems; 746c6fd3e23SJeff Roberson int domain; 747c6fd3e23SJeff Roberson int i; 748c6fd3e23SJeff Roberson 749c6fd3e23SJeff Roberson if (ZDOM_GET(zone, pref)->uzd_nitems > BUCKET_MAX) 750c6fd3e23SJeff Roberson return (pref); 751c6fd3e23SJeff Roberson 752c6fd3e23SJeff Roberson most = 0; 753c6fd3e23SJeff Roberson domain = 0; 754c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 755c6fd3e23SJeff Roberson nitems = ZDOM_GET(zone, i)->uzd_nitems; 756c6fd3e23SJeff Roberson if (nitems > most) { 757c6fd3e23SJeff Roberson domain = i; 758c6fd3e23SJeff Roberson most = nitems; 759c6fd3e23SJeff Roberson } 760c6fd3e23SJeff Roberson } 761c6fd3e23SJeff Roberson 762c6fd3e23SJeff Roberson return (domain); 763c6fd3e23SJeff Roberson } 764c6fd3e23SJeff Roberson 765c6fd3e23SJeff Roberson /* 766c6fd3e23SJeff Roberson * Set the maximum imax value. 767c6fd3e23SJeff Roberson */ 768c6fd3e23SJeff Roberson static void 769c6fd3e23SJeff Roberson zone_domain_imax_set(uma_zone_domain_t zdom, int nitems) 770c6fd3e23SJeff Roberson { 771c6fd3e23SJeff Roberson long old; 772c6fd3e23SJeff Roberson 773c6fd3e23SJeff Roberson old = zdom->uzd_imax; 774c6fd3e23SJeff Roberson do { 775c6fd3e23SJeff Roberson if (old >= nitems) 7762760658bSAlexander Motin return; 777c6fd3e23SJeff Roberson } while (atomic_fcmpset_long(&zdom->uzd_imax, &old, nitems) == 0); 7782760658bSAlexander Motin 7792760658bSAlexander Motin /* 7802760658bSAlexander Motin * We are at new maximum, so do the last WSS update for the old 7812760658bSAlexander Motin * bimin and prepare to measure next allocation batch. 7822760658bSAlexander Motin */ 7832760658bSAlexander Motin if (zdom->uzd_wss < old - zdom->uzd_bimin) 7842760658bSAlexander Motin zdom->uzd_wss = old - zdom->uzd_bimin; 7852760658bSAlexander Motin zdom->uzd_bimin = nitems; 786c6fd3e23SJeff Roberson } 787c6fd3e23SJeff Roberson 788c6fd3e23SJeff Roberson /* 78908cfa56eSMark Johnston * Attempt to satisfy an allocation by retrieving a full bucket from one of the 790d4665eaaSJeff Roberson * zone's caches. If a bucket is found the zone is not locked on return. 79108cfa56eSMark Johnston */ 7920f9b7bf3SMark Johnston static uma_bucket_t 793c6fd3e23SJeff Roberson zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom, bool reclaim) 7940f9b7bf3SMark Johnston { 7950f9b7bf3SMark Johnston uma_bucket_t bucket; 7962760658bSAlexander Motin long cnt; 797d4665eaaSJeff Roberson int i; 798d4665eaaSJeff Roberson bool dtor = false; 7990f9b7bf3SMark Johnston 800c6fd3e23SJeff Roberson ZDOM_LOCK_ASSERT(zdom); 8010f9b7bf3SMark Johnston 802dc3915c8SJeff Roberson if ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) == NULL) 803d4665eaaSJeff Roberson return (NULL); 804d4665eaaSJeff Roberson 805543117beSJeff Roberson /* SMR Buckets can not be re-used until readers expire. */ 806d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 807d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 808d4665eaaSJeff Roberson if (!smr_poll(zone->uz_smr, bucket->ub_seq, false)) 809d4665eaaSJeff Roberson return (NULL); 810d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 811543117beSJeff Roberson dtor = (zone->uz_dtor != NULL) || UMA_ALWAYS_CTORDTOR; 812c6fd3e23SJeff Roberson if (STAILQ_NEXT(bucket, ub_link) != NULL) 813c6fd3e23SJeff Roberson zdom->uzd_seq = STAILQ_NEXT(bucket, ub_link)->ub_seq; 814d4665eaaSJeff Roberson } 815dc3915c8SJeff Roberson STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link); 81606d8bdcbSMark Johnston 81706d8bdcbSMark Johnston KASSERT(zdom->uzd_nitems >= bucket->ub_cnt, 81806d8bdcbSMark Johnston ("%s: item count underflow (%ld, %d)", 81906d8bdcbSMark Johnston __func__, zdom->uzd_nitems, bucket->ub_cnt)); 82006d8bdcbSMark Johnston KASSERT(bucket->ub_cnt > 0, 82106d8bdcbSMark Johnston ("%s: empty bucket in bucket cache", __func__)); 8220f9b7bf3SMark Johnston zdom->uzd_nitems -= bucket->ub_cnt; 823c6fd3e23SJeff Roberson 8242760658bSAlexander Motin if (reclaim) { 825c6fd3e23SJeff Roberson /* 826c6fd3e23SJeff Roberson * Shift the bounds of the current WSS interval to avoid 8272760658bSAlexander Motin * perturbing the estimates. 828c6fd3e23SJeff Roberson */ 8292760658bSAlexander Motin cnt = lmin(zdom->uzd_bimin, bucket->ub_cnt); 8302760658bSAlexander Motin atomic_subtract_long(&zdom->uzd_imax, cnt); 8312760658bSAlexander Motin zdom->uzd_bimin -= cnt; 832c6fd3e23SJeff Roberson zdom->uzd_imin -= lmin(zdom->uzd_imin, bucket->ub_cnt); 8332760658bSAlexander Motin if (zdom->uzd_limin >= bucket->ub_cnt) { 8342760658bSAlexander Motin zdom->uzd_limin -= bucket->ub_cnt; 8352760658bSAlexander Motin } else { 8362760658bSAlexander Motin zdom->uzd_limin = 0; 8372760658bSAlexander Motin zdom->uzd_timin = 0; 8382760658bSAlexander Motin } 8392760658bSAlexander Motin } else if (zdom->uzd_bimin > zdom->uzd_nitems) { 8402760658bSAlexander Motin zdom->uzd_bimin = zdom->uzd_nitems; 8412760658bSAlexander Motin if (zdom->uzd_imin > zdom->uzd_nitems) 8420f9b7bf3SMark Johnston zdom->uzd_imin = zdom->uzd_nitems; 8432760658bSAlexander Motin } 844c6fd3e23SJeff Roberson 845c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 846d4665eaaSJeff Roberson if (dtor) 847d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 848d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], zone->uz_size, 849d4665eaaSJeff Roberson NULL, SKIP_NONE); 850d4665eaaSJeff Roberson 8510f9b7bf3SMark Johnston return (bucket); 8520f9b7bf3SMark Johnston } 8530f9b7bf3SMark Johnston 85408cfa56eSMark Johnston /* 85508cfa56eSMark Johnston * Insert a full bucket into the specified cache. The "ws" parameter indicates 85608cfa56eSMark Johnston * whether the bucket's contents should be counted as part of the zone's working 857c6fd3e23SJeff Roberson * set. The bucket may be freed if it exceeds the bucket limit. 85808cfa56eSMark Johnston */ 8590f9b7bf3SMark Johnston static void 860c6fd3e23SJeff Roberson zone_put_bucket(uma_zone_t zone, int domain, uma_bucket_t bucket, void *udata, 8610f9b7bf3SMark Johnston const bool ws) 8620f9b7bf3SMark Johnston { 863c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 8640f9b7bf3SMark Johnston 865c6fd3e23SJeff Roberson /* We don't cache empty buckets. This can happen after a reclaim. */ 866c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 867c6fd3e23SJeff Roberson goto out; 868c6fd3e23SJeff Roberson zdom = zone_domain_lock(zone, domain); 869c6fd3e23SJeff Roberson 870c6fd3e23SJeff Roberson /* 871c6fd3e23SJeff Roberson * Conditionally set the maximum number of items. 872c6fd3e23SJeff Roberson */ 8730f9b7bf3SMark Johnston zdom->uzd_nitems += bucket->ub_cnt; 874c6fd3e23SJeff Roberson if (__predict_true(zdom->uzd_nitems < zone->uz_bucket_max)) { 8752760658bSAlexander Motin if (ws) { 876c6fd3e23SJeff Roberson zone_domain_imax_set(zdom, zdom->uzd_nitems); 8772760658bSAlexander Motin } else { 8782760658bSAlexander Motin /* 8792760658bSAlexander Motin * Shift the bounds of the current WSS interval to 8802760658bSAlexander Motin * avoid perturbing the estimates. 8812760658bSAlexander Motin */ 8822760658bSAlexander Motin atomic_add_long(&zdom->uzd_imax, bucket->ub_cnt); 8832760658bSAlexander Motin zdom->uzd_imin += bucket->ub_cnt; 8842760658bSAlexander Motin zdom->uzd_bimin += bucket->ub_cnt; 8852760658bSAlexander Motin zdom->uzd_limin += bucket->ub_cnt; 8862760658bSAlexander Motin } 887c6fd3e23SJeff Roberson if (STAILQ_EMPTY(&zdom->uzd_buckets)) 888c6fd3e23SJeff Roberson zdom->uzd_seq = bucket->ub_seq; 8895afdf5c1SMark Johnston 8905afdf5c1SMark Johnston /* 8915afdf5c1SMark Johnston * Try to promote reuse of recently used items. For items 8925afdf5c1SMark Johnston * protected by SMR, try to defer reuse to minimize polling. 8935afdf5c1SMark Johnston */ 8945afdf5c1SMark Johnston if (bucket->ub_seq == SMR_SEQ_INVALID) 8955afdf5c1SMark Johnston STAILQ_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link); 8965afdf5c1SMark Johnston else 897c6fd3e23SJeff Roberson STAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link); 898c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 899c6fd3e23SJeff Roberson return; 900c6fd3e23SJeff Roberson } 901c6fd3e23SJeff Roberson zdom->uzd_nitems -= bucket->ub_cnt; 902c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 903c6fd3e23SJeff Roberson out: 904c6fd3e23SJeff Roberson bucket_free(zone, bucket, udata); 9050f9b7bf3SMark Johnston } 9060f9b7bf3SMark Johnston 907376b1ba3SJeff Roberson /* Pops an item out of a per-cpu cache bucket. */ 908376b1ba3SJeff Roberson static inline void * 909376b1ba3SJeff Roberson cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket) 910376b1ba3SJeff Roberson { 911376b1ba3SJeff Roberson void *item; 912376b1ba3SJeff Roberson 913376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 914376b1ba3SJeff Roberson 915376b1ba3SJeff Roberson bucket->ucb_cnt--; 916376b1ba3SJeff Roberson item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt]; 917376b1ba3SJeff Roberson #ifdef INVARIANTS 918376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL; 919376b1ba3SJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 920376b1ba3SJeff Roberson #endif 921376b1ba3SJeff Roberson cache->uc_allocs++; 922376b1ba3SJeff Roberson 923376b1ba3SJeff Roberson return (item); 924376b1ba3SJeff Roberson } 925376b1ba3SJeff Roberson 926376b1ba3SJeff Roberson /* Pushes an item into a per-cpu cache bucket. */ 927376b1ba3SJeff Roberson static inline void 928376b1ba3SJeff Roberson cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item) 929376b1ba3SJeff Roberson { 930376b1ba3SJeff Roberson 931376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 932376b1ba3SJeff Roberson KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL, 933376b1ba3SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 934376b1ba3SJeff Roberson 935376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item; 936376b1ba3SJeff Roberson bucket->ucb_cnt++; 937376b1ba3SJeff Roberson cache->uc_frees++; 938376b1ba3SJeff Roberson } 939376b1ba3SJeff Roberson 940376b1ba3SJeff Roberson /* 941376b1ba3SJeff Roberson * Unload a UMA bucket from a per-cpu cache. 942376b1ba3SJeff Roberson */ 943376b1ba3SJeff Roberson static inline uma_bucket_t 944376b1ba3SJeff Roberson cache_bucket_unload(uma_cache_bucket_t bucket) 945376b1ba3SJeff Roberson { 946376b1ba3SJeff Roberson uma_bucket_t b; 947376b1ba3SJeff Roberson 948376b1ba3SJeff Roberson b = bucket->ucb_bucket; 949376b1ba3SJeff Roberson if (b != NULL) { 950376b1ba3SJeff Roberson MPASS(b->ub_entries == bucket->ucb_entries); 951376b1ba3SJeff Roberson b->ub_cnt = bucket->ucb_cnt; 952376b1ba3SJeff Roberson bucket->ucb_bucket = NULL; 953376b1ba3SJeff Roberson bucket->ucb_entries = bucket->ucb_cnt = 0; 954376b1ba3SJeff Roberson } 955376b1ba3SJeff Roberson 956376b1ba3SJeff Roberson return (b); 957376b1ba3SJeff Roberson } 958376b1ba3SJeff Roberson 959376b1ba3SJeff Roberson static inline uma_bucket_t 960376b1ba3SJeff Roberson cache_bucket_unload_alloc(uma_cache_t cache) 961376b1ba3SJeff Roberson { 962376b1ba3SJeff Roberson 963376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_allocbucket)); 964376b1ba3SJeff Roberson } 965376b1ba3SJeff Roberson 966376b1ba3SJeff Roberson static inline uma_bucket_t 967376b1ba3SJeff Roberson cache_bucket_unload_free(uma_cache_t cache) 968376b1ba3SJeff Roberson { 969376b1ba3SJeff Roberson 970376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_freebucket)); 971376b1ba3SJeff Roberson } 972376b1ba3SJeff Roberson 973376b1ba3SJeff Roberson static inline uma_bucket_t 974376b1ba3SJeff Roberson cache_bucket_unload_cross(uma_cache_t cache) 975376b1ba3SJeff Roberson { 976376b1ba3SJeff Roberson 977376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_crossbucket)); 978376b1ba3SJeff Roberson } 979376b1ba3SJeff Roberson 980376b1ba3SJeff Roberson /* 981376b1ba3SJeff Roberson * Load a bucket into a per-cpu cache bucket. 982376b1ba3SJeff Roberson */ 983376b1ba3SJeff Roberson static inline void 984376b1ba3SJeff Roberson cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b) 985376b1ba3SJeff Roberson { 986376b1ba3SJeff Roberson 987376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 988376b1ba3SJeff Roberson MPASS(bucket->ucb_bucket == NULL); 989543117beSJeff Roberson MPASS(b->ub_seq == SMR_SEQ_INVALID); 990376b1ba3SJeff Roberson 991376b1ba3SJeff Roberson bucket->ucb_bucket = b; 992376b1ba3SJeff Roberson bucket->ucb_cnt = b->ub_cnt; 993376b1ba3SJeff Roberson bucket->ucb_entries = b->ub_entries; 994376b1ba3SJeff Roberson } 995376b1ba3SJeff Roberson 996376b1ba3SJeff Roberson static inline void 997376b1ba3SJeff Roberson cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b) 998376b1ba3SJeff Roberson { 999376b1ba3SJeff Roberson 1000376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_allocbucket, b); 1001376b1ba3SJeff Roberson } 1002376b1ba3SJeff Roberson 1003376b1ba3SJeff Roberson static inline void 1004376b1ba3SJeff Roberson cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b) 1005376b1ba3SJeff Roberson { 1006376b1ba3SJeff Roberson 1007376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_freebucket, b); 1008376b1ba3SJeff Roberson } 1009376b1ba3SJeff Roberson 1010dfe13344SJeff Roberson #ifdef NUMA 1011376b1ba3SJeff Roberson static inline void 1012376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b) 1013376b1ba3SJeff Roberson { 1014376b1ba3SJeff Roberson 1015376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_crossbucket, b); 1016376b1ba3SJeff Roberson } 1017376b1ba3SJeff Roberson #endif 1018376b1ba3SJeff Roberson 1019376b1ba3SJeff Roberson /* 1020376b1ba3SJeff Roberson * Copy and preserve ucb_spare. 1021376b1ba3SJeff Roberson */ 1022376b1ba3SJeff Roberson static inline void 1023376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 1024376b1ba3SJeff Roberson { 1025376b1ba3SJeff Roberson 1026376b1ba3SJeff Roberson b1->ucb_bucket = b2->ucb_bucket; 1027376b1ba3SJeff Roberson b1->ucb_entries = b2->ucb_entries; 1028376b1ba3SJeff Roberson b1->ucb_cnt = b2->ucb_cnt; 1029376b1ba3SJeff Roberson } 1030376b1ba3SJeff Roberson 1031376b1ba3SJeff Roberson /* 1032376b1ba3SJeff Roberson * Swap two cache buckets. 1033376b1ba3SJeff Roberson */ 1034376b1ba3SJeff Roberson static inline void 1035376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 1036376b1ba3SJeff Roberson { 1037376b1ba3SJeff Roberson struct uma_cache_bucket b3; 1038376b1ba3SJeff Roberson 1039376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 1040376b1ba3SJeff Roberson 1041376b1ba3SJeff Roberson cache_bucket_copy(&b3, b1); 1042376b1ba3SJeff Roberson cache_bucket_copy(b1, b2); 1043376b1ba3SJeff Roberson cache_bucket_copy(b2, &b3); 1044376b1ba3SJeff Roberson } 1045376b1ba3SJeff Roberson 1046c6fd3e23SJeff Roberson /* 1047c6fd3e23SJeff Roberson * Attempt to fetch a bucket from a zone on behalf of the current cpu cache. 1048c6fd3e23SJeff Roberson */ 1049c6fd3e23SJeff Roberson static uma_bucket_t 1050c6fd3e23SJeff Roberson cache_fetch_bucket(uma_zone_t zone, uma_cache_t cache, int domain) 1051c6fd3e23SJeff Roberson { 1052c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 1053c6fd3e23SJeff Roberson uma_bucket_t bucket; 1054a04ce833SMark Johnston smr_seq_t seq; 1055c6fd3e23SJeff Roberson 1056c6fd3e23SJeff Roberson /* 1057c6fd3e23SJeff Roberson * Avoid the lock if possible. 1058c6fd3e23SJeff Roberson */ 1059c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 1060c6fd3e23SJeff Roberson if (zdom->uzd_nitems == 0) 1061c6fd3e23SJeff Roberson return (NULL); 1062c6fd3e23SJeff Roberson 1063c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_SMR) != 0 && 1064a04ce833SMark Johnston (seq = atomic_load_32(&zdom->uzd_seq)) != SMR_SEQ_INVALID && 1065a04ce833SMark Johnston !smr_poll(zone->uz_smr, seq, false)) 1066c6fd3e23SJeff Roberson return (NULL); 1067c6fd3e23SJeff Roberson 1068c6fd3e23SJeff Roberson /* 1069c6fd3e23SJeff Roberson * Check the zone's cache of buckets. 1070c6fd3e23SJeff Roberson */ 1071c6fd3e23SJeff Roberson zdom = zone_domain_lock(zone, domain); 107206d8bdcbSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) 1073c6fd3e23SJeff Roberson return (bucket); 1074c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 1075c6fd3e23SJeff Roberson 1076c6fd3e23SJeff Roberson return (NULL); 1077c6fd3e23SJeff Roberson } 1078c6fd3e23SJeff Roberson 10792f891cd5SPawel Jakub Dawidek static void 10802f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 10812f891cd5SPawel Jakub Dawidek { 10822f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 10832f891cd5SPawel Jakub Dawidek 10842f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 10852f891cd5SPawel Jakub Dawidek return; 10862f891cd5SPawel Jakub Dawidek 10872f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 10882f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 10892f891cd5SPawel Jakub Dawidek } 10902f891cd5SPawel Jakub Dawidek 109154503a13SJonathan T. Looney static inline void 109254503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone) 109354503a13SJonathan T. Looney { 1094e60b2fcbSGleb Smirnoff 1095e60b2fcbSGleb Smirnoff if (zone->uz_maxaction.ta_func != NULL) 1096e60b2fcbSGleb Smirnoff taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction); 109754503a13SJonathan T. Looney } 109854503a13SJonathan T. Looney 10998355f576SJeff Roberson /* 11008355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 11019643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 11028355f576SJeff Roberson * 11038355f576SJeff Roberson * Arguments: 11048355f576SJeff Roberson * arg Unused 11058355f576SJeff Roberson * 11068355f576SJeff Roberson * Returns: 11078355f576SJeff Roberson * Nothing 11088355f576SJeff Roberson */ 11098355f576SJeff Roberson static void 11108355f576SJeff Roberson uma_timeout(void *unused) 11118355f576SJeff Roberson { 111286bbae32SJeff Roberson bucket_enable(); 111320a4e154SJeff Roberson zone_foreach(zone_timeout, NULL); 11148355f576SJeff Roberson 11158355f576SJeff Roberson /* Reschedule this event */ 11169643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 11178355f576SJeff Roberson } 11188355f576SJeff Roberson 11198355f576SJeff Roberson /* 11202760658bSAlexander Motin * Update the working set size estimates for the zone's bucket cache. 11212760658bSAlexander Motin * The constants chosen here are somewhat arbitrary. 11220f9b7bf3SMark Johnston */ 11230f9b7bf3SMark Johnston static void 11240f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom) 11250f9b7bf3SMark Johnston { 11262760658bSAlexander Motin long m; 11270f9b7bf3SMark Johnston 11282760658bSAlexander Motin ZDOM_LOCK_ASSERT(zdom); 11292760658bSAlexander Motin MPASS(zdom->uzd_imax >= zdom->uzd_nitems); 11302760658bSAlexander Motin MPASS(zdom->uzd_nitems >= zdom->uzd_bimin); 11312760658bSAlexander Motin MPASS(zdom->uzd_bimin >= zdom->uzd_imin); 11322760658bSAlexander Motin 11332760658bSAlexander Motin /* 11342760658bSAlexander Motin * Estimate WSS as modified moving average of biggest allocation 11352760658bSAlexander Motin * batches for each period over few minutes (UMA_TIMEOUT of 20s). 11362760658bSAlexander Motin */ 11372760658bSAlexander Motin zdom->uzd_wss = lmax(zdom->uzd_wss * 3 / 4, 11382760658bSAlexander Motin zdom->uzd_imax - zdom->uzd_bimin); 11392760658bSAlexander Motin 11402760658bSAlexander Motin /* 11412760658bSAlexander Motin * Estimate longtime minimum item count as a combination of recent 11422760658bSAlexander Motin * minimum item count, adjusted by WSS for safety, and the modified 11432760658bSAlexander Motin * moving average over the last several hours (UMA_TIMEOUT of 20s). 11442760658bSAlexander Motin * timin measures time since limin tried to go negative, that means 11452760658bSAlexander Motin * we were dangerously close to or got out of cache. 11462760658bSAlexander Motin */ 11472760658bSAlexander Motin m = zdom->uzd_imin - zdom->uzd_wss; 11482760658bSAlexander Motin if (m >= 0) { 11492760658bSAlexander Motin if (zdom->uzd_limin >= m) 11502760658bSAlexander Motin zdom->uzd_limin = m; 11512760658bSAlexander Motin else 11522760658bSAlexander Motin zdom->uzd_limin = (m + zdom->uzd_limin * 255) / 256; 11532760658bSAlexander Motin zdom->uzd_timin++; 11542760658bSAlexander Motin } else { 11552760658bSAlexander Motin zdom->uzd_limin = 0; 11562760658bSAlexander Motin zdom->uzd_timin = 0; 11572760658bSAlexander Motin } 11582760658bSAlexander Motin 11592760658bSAlexander Motin /* To reduce period edge effects on WSS keep half of the imax. */ 11602760658bSAlexander Motin atomic_subtract_long(&zdom->uzd_imax, 11612760658bSAlexander Motin (zdom->uzd_imax - zdom->uzd_nitems + 1) / 2); 11622760658bSAlexander Motin zdom->uzd_imin = zdom->uzd_bimin = zdom->uzd_nitems; 11630f9b7bf3SMark Johnston } 11640f9b7bf3SMark Johnston 11650f9b7bf3SMark Johnston /* 11669643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 11679643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 11688355f576SJeff Roberson * 1169e20a199fSJeff Roberson * Returns nothing. 11708355f576SJeff Roberson */ 11718355f576SJeff Roberson static void 117220a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused) 11738355f576SJeff Roberson { 117408034d10SKonstantin Belousov uma_keg_t keg; 11758b987a77SJeff Roberson u_int slabs, pages; 11768355f576SJeff Roberson 117754c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 11782760658bSAlexander Motin goto trim; 117908034d10SKonstantin Belousov 118008034d10SKonstantin Belousov keg = zone->uz_keg; 11818b987a77SJeff Roberson 11828b987a77SJeff Roberson /* 11838b987a77SJeff Roberson * Hash zones are non-numa by definition so the first domain 11848b987a77SJeff Roberson * is the only one present. 11858b987a77SJeff Roberson */ 11868b987a77SJeff Roberson KEG_LOCK(keg, 0); 11878b987a77SJeff Roberson pages = keg->uk_domain[0].ud_pages; 11888b987a77SJeff Roberson 11898355f576SJeff Roberson /* 1190e20a199fSJeff Roberson * Expand the keg hash table. 11918355f576SJeff Roberson * 11928355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 11938355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 11948355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 11958355f576SJeff Roberson */ 11968b987a77SJeff Roberson if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) { 11970aef6126SJeff Roberson struct uma_hash newhash; 11980aef6126SJeff Roberson struct uma_hash oldhash; 11990aef6126SJeff Roberson int ret; 12005300d9ddSJeff Roberson 12010aef6126SJeff Roberson /* 12020aef6126SJeff Roberson * This is so involved because allocating and freeing 1203e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 12040aef6126SJeff Roberson * I have to do everything in stages and check for 12050aef6126SJeff Roberson * races. 12060aef6126SJeff Roberson */ 12078b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 12083b2f2cb8SAlexander Motin ret = hash_alloc(&newhash, 1 << fls(slabs)); 12098b987a77SJeff Roberson KEG_LOCK(keg, 0); 12100aef6126SJeff Roberson if (ret) { 1211099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 1212099a0e58SBosko Milekic oldhash = keg->uk_hash; 1213099a0e58SBosko Milekic keg->uk_hash = newhash; 12140aef6126SJeff Roberson } else 12150aef6126SJeff Roberson oldhash = newhash; 12160aef6126SJeff Roberson 12178b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 12180aef6126SJeff Roberson hash_free(&oldhash); 12192760658bSAlexander Motin goto trim; 12200aef6126SJeff Roberson } 12215300d9ddSJeff Roberson } 12228b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 1223e20a199fSJeff Roberson 12242760658bSAlexander Motin trim: 12252760658bSAlexander Motin /* Trim caches not used for a long time. */ 1226389a3fa6SMark Johnston if ((zone->uz_flags & UMA_ZONE_UNMANAGED) == 0) { 12272760658bSAlexander Motin for (int i = 0; i < vm_ndomains; i++) { 12282760658bSAlexander Motin if (bucket_cache_reclaim_domain(zone, false, false, i) && 12292760658bSAlexander Motin (zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 12302760658bSAlexander Motin keg_drain(zone->uz_keg, i); 12312760658bSAlexander Motin } 12328355f576SJeff Roberson } 1233389a3fa6SMark Johnston } 12348355f576SJeff Roberson 12358355f576SJeff Roberson /* 12365300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 12375300d9ddSJeff Roberson * backing store. 12385300d9ddSJeff Roberson * 12395300d9ddSJeff Roberson * Arguments: 12400aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 12415300d9ddSJeff Roberson * 12425300d9ddSJeff Roberson * Returns: 1243763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 12445300d9ddSJeff Roberson */ 124537c84183SPoul-Henning Kamp static int 12463b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 12475300d9ddSJeff Roberson { 124859568a0eSAlexander Motin size_t alloc; 12495300d9ddSJeff Roberson 12503b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 12513b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 12523b2f2cb8SAlexander Motin hash->uh_hashsize = size; 12530aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 12541e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 12555300d9ddSJeff Roberson } else { 12560aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 1257e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 1258ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 12590aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 12605300d9ddSJeff Roberson } 12610aef6126SJeff Roberson if (hash->uh_slab_hash) { 12620aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 12630aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 12640aef6126SJeff Roberson return (1); 12650aef6126SJeff Roberson } 12665300d9ddSJeff Roberson 12670aef6126SJeff Roberson return (0); 12685300d9ddSJeff Roberson } 12695300d9ddSJeff Roberson 12705300d9ddSJeff Roberson /* 127164f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 127264f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 127364f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 12748355f576SJeff Roberson * 12758355f576SJeff Roberson * Arguments: 12760aef6126SJeff Roberson * oldhash The hash you want to expand 12770aef6126SJeff Roberson * newhash The hash structure for the new table 12788355f576SJeff Roberson * 12798355f576SJeff Roberson * Returns: 12808355f576SJeff Roberson * Nothing 12818355f576SJeff Roberson * 12828355f576SJeff Roberson * Discussion: 12838355f576SJeff Roberson */ 12840aef6126SJeff Roberson static int 12850aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 12868355f576SJeff Roberson { 12871e0701e1SJeff Roberson uma_hash_slab_t slab; 12886929b7d1SPedro F. Giffuni u_int hval; 12896929b7d1SPedro F. Giffuni u_int idx; 12908355f576SJeff Roberson 12910aef6126SJeff Roberson if (!newhash->uh_slab_hash) 12920aef6126SJeff Roberson return (0); 12938355f576SJeff Roberson 12940aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 12950aef6126SJeff Roberson return (0); 12968355f576SJeff Roberson 12978355f576SJeff Roberson /* 12988355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 12998355f576SJeff Roberson * full rehash. 13008355f576SJeff Roberson */ 13018355f576SJeff Roberson 13026929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 13031e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 13041e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 13051e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 13061e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 13071e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 13081e0701e1SJeff Roberson slab, uhs_hlink); 13098355f576SJeff Roberson } 13108355f576SJeff Roberson 13110aef6126SJeff Roberson return (1); 13129c2cd7e5SJeff Roberson } 13139c2cd7e5SJeff Roberson 13145300d9ddSJeff Roberson /* 13155300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 13165300d9ddSJeff Roberson * 13175300d9ddSJeff Roberson * Arguments: 13185300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 13195300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 13205300d9ddSJeff Roberson * 13215300d9ddSJeff Roberson * Returns: 13225300d9ddSJeff Roberson * Nothing 13235300d9ddSJeff Roberson */ 13249c2cd7e5SJeff Roberson static void 13250aef6126SJeff Roberson hash_free(struct uma_hash *hash) 13269c2cd7e5SJeff Roberson { 13270aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 13280aef6126SJeff Roberson return; 13290aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 13300095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 13318355f576SJeff Roberson else 1332961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 13338355f576SJeff Roberson } 13348355f576SJeff Roberson 13358355f576SJeff Roberson /* 13368355f576SJeff Roberson * Frees all outstanding items in a bucket 13378355f576SJeff Roberson * 13388355f576SJeff Roberson * Arguments: 13398355f576SJeff Roberson * zone The zone to free to, must be unlocked. 13404bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 13418355f576SJeff Roberson * 13428355f576SJeff Roberson * Returns: 13438355f576SJeff Roberson * Nothing 13448355f576SJeff Roberson */ 13458355f576SJeff Roberson static void 13468355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 13478355f576SJeff Roberson { 13480095a784SJeff Roberson int i; 13498355f576SJeff Roberson 1350c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 13518355f576SJeff Roberson return; 13528355f576SJeff Roberson 1353d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 1354d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 1355d4665eaaSJeff Roberson smr_wait(zone->uz_smr, bucket->ub_seq); 1356543117beSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 1357d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 1358d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], 1359d4665eaaSJeff Roberson zone->uz_size, NULL, SKIP_NONE); 1360d4665eaaSJeff Roberson } 13610095a784SJeff Roberson if (zone->uz_fini) 136209c8cb71SMark Johnston for (i = 0; i < bucket->ub_cnt; i++) { 136309c8cb71SMark Johnston kasan_mark_item_valid(zone, bucket->ub_bucket[i]); 13640095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 136509c8cb71SMark Johnston kasan_mark_item_invalid(zone, bucket->ub_bucket[i]); 136609c8cb71SMark Johnston } 13670095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 13684bd61e19SJeff Roberson if (zone->uz_max_items > 0) 13694bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 1370d4665eaaSJeff Roberson #ifdef INVARIANTS 1371d4665eaaSJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt); 1372d4665eaaSJeff Roberson #endif 13730095a784SJeff Roberson bucket->ub_cnt = 0; 13748355f576SJeff Roberson } 13758355f576SJeff Roberson 13768355f576SJeff Roberson /* 13778355f576SJeff Roberson * Drains the per cpu caches for a zone. 13788355f576SJeff Roberson * 1379727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 13805d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 13815d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 13825d1ae027SRobert Watson * 13838355f576SJeff Roberson * Arguments: 13848355f576SJeff Roberson * zone The zone to drain, must be unlocked. 13858355f576SJeff Roberson * 13868355f576SJeff Roberson * Returns: 13878355f576SJeff Roberson * Nothing 13888355f576SJeff Roberson */ 13898355f576SJeff Roberson static void 13909643769aSJeff Roberson cache_drain(uma_zone_t zone) 13918355f576SJeff Roberson { 13928355f576SJeff Roberson uma_cache_t cache; 1393376b1ba3SJeff Roberson uma_bucket_t bucket; 1394543117beSJeff Roberson smr_seq_t seq; 13958355f576SJeff Roberson int cpu; 13968355f576SJeff Roberson 13978355f576SJeff Roberson /* 13985d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 13995d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 14005d1ae027SRobert Watson * of the caches at this point. 14015d1ae027SRobert Watson * 14025d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 14035d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 14048355f576SJeff Roberson */ 1405543117beSJeff Roberson seq = SMR_SEQ_INVALID; 1406543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 1407226dd6dbSJeff Roberson seq = smr_advance(zone->uz_smr); 14083aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 14098355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 1410376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 1411c6fd3e23SJeff Roberson if (bucket != NULL) 1412376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1413376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 1414376b1ba3SJeff Roberson if (bucket != NULL) { 1415543117beSJeff Roberson bucket->ub_seq = seq; 1416376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1417376b1ba3SJeff Roberson } 1418376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 1419376b1ba3SJeff Roberson if (bucket != NULL) { 1420543117beSJeff Roberson bucket->ub_seq = seq; 1421376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1422376b1ba3SJeff Roberson } 1423d56368d7SBosko Milekic } 1424aabe13f1SMark Johnston bucket_cache_reclaim(zone, true, UMA_ANYDOMAIN); 1425aaa8bb16SJeff Roberson } 1426aaa8bb16SJeff Roberson 1427a2de44abSAlexander Motin static void 142820a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 1429a2de44abSAlexander Motin { 1430a2de44abSAlexander Motin 1431a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1432a2de44abSAlexander Motin return; 1433a2de44abSAlexander Motin 1434aabe13f1SMark Johnston ZONE_LOCK(zone); 143520a4e154SJeff Roberson zone->uz_bucket_size = 143620a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 1437aabe13f1SMark Johnston ZONE_UNLOCK(zone); 1438a2de44abSAlexander Motin } 1439a2de44abSAlexander Motin 1440a2de44abSAlexander Motin static void 144120a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 1442a2de44abSAlexander Motin { 1443a2de44abSAlexander Motin uma_cache_t cache; 1444c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 1445ab3185d1SJeff Roberson int domain; 1446a2de44abSAlexander Motin 1447a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1448a2de44abSAlexander Motin return; 1449a2de44abSAlexander Motin 1450c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1451a2de44abSAlexander Motin critical_enter(); 1452a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1453c6fd3e23SJeff Roberson domain = PCPU_GET(domain); 1454376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1455d4665eaaSJeff Roberson 1456d4665eaaSJeff Roberson /* 1457d4665eaaSJeff Roberson * Don't flush SMR zone buckets. This leaves the zone without a 1458d4665eaaSJeff Roberson * bucket and forces every free to synchronize(). 1459d4665eaaSJeff Roberson */ 1460543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0) { 1461376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1462543117beSJeff Roberson b3 = cache_bucket_unload_cross(cache); 1463543117beSJeff Roberson } 1464543117beSJeff Roberson critical_exit(); 1465543117beSJeff Roberson 1466543117beSJeff Roberson if (b1 != NULL) 1467c6fd3e23SJeff Roberson zone_free_bucket(zone, b1, NULL, domain, false); 1468543117beSJeff Roberson if (b2 != NULL) 1469c6fd3e23SJeff Roberson zone_free_bucket(zone, b2, NULL, domain, false); 1470543117beSJeff Roberson if (b3 != NULL) { 1471c6fd3e23SJeff Roberson /* Adjust the domain so it goes to zone_free_cross. */ 1472c6fd3e23SJeff Roberson domain = (domain + 1) % vm_ndomains; 1473c6fd3e23SJeff Roberson zone_free_bucket(zone, b3, NULL, domain, false); 1474c1685086SJeff Roberson } 1475a2de44abSAlexander Motin } 1476a2de44abSAlexander Motin 1477a2de44abSAlexander Motin /* 1478a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1479a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1480a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1481a2de44abSAlexander Motin * to safely access their cache buckets. 1482a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1483a2de44abSAlexander Motin */ 1484a2de44abSAlexander Motin static void 148508cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1486a2de44abSAlexander Motin { 1487a2de44abSAlexander Motin int cpu; 1488a2de44abSAlexander Motin 1489a2de44abSAlexander Motin /* 1490727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1491a2de44abSAlexander Motin */ 1492a2de44abSAlexander Motin if (zone) 149320a4e154SJeff Roberson cache_shrink(zone, NULL); 1494a2de44abSAlexander Motin else 149520a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1496a2de44abSAlexander Motin 1497a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1498a2de44abSAlexander Motin thread_lock(curthread); 1499a2de44abSAlexander Motin sched_bind(curthread, cpu); 1500a2de44abSAlexander Motin thread_unlock(curthread); 1501a2de44abSAlexander Motin 1502a2de44abSAlexander Motin if (zone) 150320a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1504a2de44abSAlexander Motin else 150520a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1506a2de44abSAlexander Motin } 1507a2de44abSAlexander Motin thread_lock(curthread); 1508a2de44abSAlexander Motin sched_unbind(curthread); 1509a2de44abSAlexander Motin thread_unlock(curthread); 1510a2de44abSAlexander Motin } 1511a2de44abSAlexander Motin 1512aaa8bb16SJeff Roberson /* 151308cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 151408cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 151508cfa56eSMark Johnston * estimated working set size. 1516aaa8bb16SJeff Roberson */ 15172760658bSAlexander Motin static bool 15182760658bSAlexander Motin bucket_cache_reclaim_domain(uma_zone_t zone, bool drain, bool trim, int domain) 1519aaa8bb16SJeff Roberson { 1520ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1521aaa8bb16SJeff Roberson uma_bucket_t bucket; 1522c6fd3e23SJeff Roberson long target; 15232760658bSAlexander Motin bool done = false; 15248355f576SJeff Roberson 1525c6fd3e23SJeff Roberson /* 152691d947bfSJeff Roberson * The cross bucket is partially filled and not part of 152791d947bfSJeff Roberson * the item count. Reclaim it individually here. 152891d947bfSJeff Roberson */ 152954f421f9SMark Johnston zdom = ZDOM_GET(zone, domain); 1530226dd6dbSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 || drain) { 153191d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 153291d947bfSJeff Roberson bucket = zdom->uzd_cross; 153391d947bfSJeff Roberson zdom->uzd_cross = NULL; 153491d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 1535c6fd3e23SJeff Roberson if (bucket != NULL) 153691d947bfSJeff Roberson bucket_free(zone, bucket, NULL); 153791d947bfSJeff Roberson } 153891d947bfSJeff Roberson 153991d947bfSJeff Roberson /* 154008cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 15412760658bSAlexander Motin * this bucket cache is empty. If trim, we reclaim items in 15422760658bSAlexander Motin * excess of the zone's estimated working set size. Multiple 15432760658bSAlexander Motin * consecutive calls will shrink the WSS and so reclaim more. 15442760658bSAlexander Motin * If neither drain nor trim, then voluntarily reclaim 1/4 15452760658bSAlexander Motin * (to reduce first spike) of items not used for a long time. 154608cfa56eSMark Johnston */ 1547c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 15482760658bSAlexander Motin zone_domain_update_wss(zdom); 15492760658bSAlexander Motin if (drain) 15502760658bSAlexander Motin target = 0; 15512760658bSAlexander Motin else if (trim) 15522760658bSAlexander Motin target = zdom->uzd_wss; 15532760658bSAlexander Motin else if (zdom->uzd_timin > 900 / UMA_TIMEOUT) 15542760658bSAlexander Motin target = zdom->uzd_nitems - zdom->uzd_limin / 4; 15552760658bSAlexander Motin else { 15562760658bSAlexander Motin ZDOM_UNLOCK(zdom); 15572760658bSAlexander Motin return (done); 15582760658bSAlexander Motin } 15592760658bSAlexander Motin while ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) != NULL && 15602760658bSAlexander Motin zdom->uzd_nitems >= target + bucket->ub_cnt) { 1561c6fd3e23SJeff Roberson bucket = zone_fetch_bucket(zone, zdom, true); 156208cfa56eSMark Johnston if (bucket == NULL) 156308cfa56eSMark Johnston break; 15646fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 15652760658bSAlexander Motin done = true; 1566c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 15678355f576SJeff Roberson } 1568c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 15692760658bSAlexander Motin return (done); 1570ab3185d1SJeff Roberson } 157154f421f9SMark Johnston 157254f421f9SMark Johnston static void 1573aabe13f1SMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain, int domain) 157454f421f9SMark Johnston { 157554f421f9SMark Johnston int i; 157654f421f9SMark Johnston 157754f421f9SMark Johnston /* 157854f421f9SMark Johnston * Shrink the zone bucket size to ensure that the per-CPU caches 157954f421f9SMark Johnston * don't grow too large. 158054f421f9SMark Johnston */ 158154f421f9SMark Johnston if (zone->uz_bucket_size > zone->uz_bucket_size_min) 158254f421f9SMark Johnston zone->uz_bucket_size--; 158354f421f9SMark Johnston 1584aabe13f1SMark Johnston if (domain != UMA_ANYDOMAIN && 1585aabe13f1SMark Johnston (zone->uz_flags & UMA_ZONE_ROUNDROBIN) == 0) { 15862760658bSAlexander Motin bucket_cache_reclaim_domain(zone, drain, true, domain); 1587aabe13f1SMark Johnston } else { 158854f421f9SMark Johnston for (i = 0; i < vm_ndomains; i++) 15892760658bSAlexander Motin bucket_cache_reclaim_domain(zone, drain, true, i); 15908355f576SJeff Roberson } 1591aabe13f1SMark Johnston } 1592fc03d22bSJeff Roberson 1593fc03d22bSJeff Roberson static void 1594fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1595fc03d22bSJeff Roberson { 1596fc03d22bSJeff Roberson uint8_t *mem; 159709c8cb71SMark Johnston size_t size; 1598fc03d22bSJeff Roberson int i; 1599fc03d22bSJeff Roberson uint8_t flags; 1600fc03d22bSJeff Roberson 16011431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 16021431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 16031431a748SGleb Smirnoff 16041e0701e1SJeff Roberson mem = slab_data(slab, keg); 160509c8cb71SMark Johnston size = PAGE_SIZE * keg->uk_ppera; 160609c8cb71SMark Johnston 160709c8cb71SMark Johnston kasan_mark_slab_valid(keg, mem); 1608fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 160909c8cb71SMark Johnston for (i = start - 1; i > -1; i--) 1610c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1611c5deaf04SGleb Smirnoff /* 1612c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1613c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1614c5deaf04SGleb Smirnoff * which executed trash_dtor. 1615c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1616c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1617c5deaf04SGleb Smirnoff * invocations. 1618c5deaf04SGleb Smirnoff */ 16191e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1620c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1621c5deaf04SGleb Smirnoff #endif 16221e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1623fc03d22bSJeff Roberson } 162409c8cb71SMark Johnston flags = slab->us_flags; 162509c8cb71SMark Johnston if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 16269b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab), 16279b8db4d0SRyan Libby NULL, SKIP_NONE); 162809c8cb71SMark Johnston } 162909c8cb71SMark Johnston keg->uk_freef(mem, size, flags); 163009c8cb71SMark Johnston uma_total_dec(size); 16318355f576SJeff Roberson } 16328355f576SJeff Roberson 1633f09cbea3SMark Johnston static void 1634f09cbea3SMark Johnston keg_drain_domain(uma_keg_t keg, int domain) 1635f09cbea3SMark Johnston { 1636f09cbea3SMark Johnston struct slabhead freeslabs; 1637f09cbea3SMark Johnston uma_domain_t dom; 1638f09cbea3SMark Johnston uma_slab_t slab, tmp; 1639f09cbea3SMark Johnston uint32_t i, stofree, stokeep, partial; 1640f09cbea3SMark Johnston 1641f09cbea3SMark Johnston dom = &keg->uk_domain[domain]; 1642f09cbea3SMark Johnston LIST_INIT(&freeslabs); 1643f09cbea3SMark Johnston 1644f09cbea3SMark Johnston CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u", 1645575a4437SEd Maste keg->uk_name, keg, domain, dom->ud_free_items); 1646f09cbea3SMark Johnston 1647f09cbea3SMark Johnston KEG_LOCK(keg, domain); 1648f09cbea3SMark Johnston 1649f09cbea3SMark Johnston /* 1650f09cbea3SMark Johnston * Are the free items in partially allocated slabs sufficient to meet 1651f09cbea3SMark Johnston * the reserve? If not, compute the number of fully free slabs that must 1652f09cbea3SMark Johnston * be kept. 1653f09cbea3SMark Johnston */ 1654f09cbea3SMark Johnston partial = dom->ud_free_items - dom->ud_free_slabs * keg->uk_ipers; 1655f09cbea3SMark Johnston if (partial < keg->uk_reserve) { 1656f09cbea3SMark Johnston stokeep = min(dom->ud_free_slabs, 1657f09cbea3SMark Johnston howmany(keg->uk_reserve - partial, keg->uk_ipers)); 1658f09cbea3SMark Johnston } else { 1659f09cbea3SMark Johnston stokeep = 0; 1660f09cbea3SMark Johnston } 1661f09cbea3SMark Johnston stofree = dom->ud_free_slabs - stokeep; 1662f09cbea3SMark Johnston 1663f09cbea3SMark Johnston /* 1664f09cbea3SMark Johnston * Partition the free slabs into two sets: those that must be kept in 1665f09cbea3SMark Johnston * order to maintain the reserve, and those that may be released back to 1666f09cbea3SMark Johnston * the system. Since one set may be much larger than the other, 1667f09cbea3SMark Johnston * populate the smaller of the two sets and swap them if necessary. 1668f09cbea3SMark Johnston */ 1669f09cbea3SMark Johnston for (i = min(stofree, stokeep); i > 0; i--) { 1670f09cbea3SMark Johnston slab = LIST_FIRST(&dom->ud_free_slab); 1671f09cbea3SMark Johnston LIST_REMOVE(slab, us_link); 1672f09cbea3SMark Johnston LIST_INSERT_HEAD(&freeslabs, slab, us_link); 1673f09cbea3SMark Johnston } 1674f09cbea3SMark Johnston if (stofree > stokeep) 1675f09cbea3SMark Johnston LIST_SWAP(&freeslabs, &dom->ud_free_slab, uma_slab, us_link); 1676f09cbea3SMark Johnston 1677f09cbea3SMark Johnston if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) { 1678f09cbea3SMark Johnston LIST_FOREACH(slab, &freeslabs, us_link) 1679f09cbea3SMark Johnston UMA_HASH_REMOVE(&keg->uk_hash, slab); 1680f09cbea3SMark Johnston } 1681f09cbea3SMark Johnston dom->ud_free_items -= stofree * keg->uk_ipers; 1682f09cbea3SMark Johnston dom->ud_free_slabs -= stofree; 1683f09cbea3SMark Johnston dom->ud_pages -= stofree * keg->uk_ppera; 1684f09cbea3SMark Johnston KEG_UNLOCK(keg, domain); 1685f09cbea3SMark Johnston 1686f09cbea3SMark Johnston LIST_FOREACH_SAFE(slab, &freeslabs, us_link, tmp) 1687f09cbea3SMark Johnston keg_free_slab(keg, slab, keg->uk_ipers); 1688f09cbea3SMark Johnston } 1689f09cbea3SMark Johnston 16908355f576SJeff Roberson /* 1691e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 16928355f576SJeff Roberson * the pageout daemon. 16938355f576SJeff Roberson * 1694e20a199fSJeff Roberson * Returns nothing. 16958355f576SJeff Roberson */ 1696e20a199fSJeff Roberson static void 1697aabe13f1SMark Johnston keg_drain(uma_keg_t keg, int domain) 16988355f576SJeff Roberson { 1699f09cbea3SMark Johnston int i; 17008355f576SJeff Roberson 1701f09cbea3SMark Johnston if ((keg->uk_flags & UMA_ZONE_NOFREE) != 0) 17028355f576SJeff Roberson return; 1703aabe13f1SMark Johnston if (domain != UMA_ANYDOMAIN) { 1704aabe13f1SMark Johnston keg_drain_domain(keg, domain); 1705aabe13f1SMark Johnston } else { 1706f09cbea3SMark Johnston for (i = 0; i < vm_ndomains; i++) 1707f09cbea3SMark Johnston keg_drain_domain(keg, i); 17088355f576SJeff Roberson } 1709aabe13f1SMark Johnston } 17108355f576SJeff Roberson 1711e20a199fSJeff Roberson static void 1712aabe13f1SMark Johnston zone_reclaim(uma_zone_t zone, int domain, int waitok, bool drain) 1713e20a199fSJeff Roberson { 17148355f576SJeff Roberson /* 1715aabe13f1SMark Johnston * Count active reclaim operations in order to interlock with 1716aabe13f1SMark Johnston * zone_dtor(), which removes the zone from global lists before 1717aabe13f1SMark Johnston * attempting to reclaim items itself. 1718aabe13f1SMark Johnston * 1719aabe13f1SMark Johnston * The zone may be destroyed while sleeping, so only zone_dtor() should 1720aabe13f1SMark Johnston * specify M_WAITOK. 1721e20a199fSJeff Roberson */ 1722e20a199fSJeff Roberson ZONE_LOCK(zone); 1723aabe13f1SMark Johnston if (waitok == M_WAITOK) { 1724aabe13f1SMark Johnston while (zone->uz_reclaimers > 0) 1725aabe13f1SMark Johnston msleep(zone, ZONE_LOCKPTR(zone), PVM, "zonedrain", 1); 1726e20a199fSJeff Roberson } 1727aabe13f1SMark Johnston zone->uz_reclaimers++; 1728e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1729aabe13f1SMark Johnston bucket_cache_reclaim(zone, drain, domain); 173008cfa56eSMark Johnston 173108034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1732aabe13f1SMark Johnston keg_drain(zone->uz_keg, domain); 1733e20a199fSJeff Roberson ZONE_LOCK(zone); 1734aabe13f1SMark Johnston zone->uz_reclaimers--; 1735aabe13f1SMark Johnston if (zone->uz_reclaimers == 0) 1736e20a199fSJeff Roberson wakeup(zone); 1737e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1738e20a199fSJeff Roberson } 1739e20a199fSJeff Roberson 1740e20a199fSJeff Roberson /* 17418b987a77SJeff Roberson * Allocate a new slab for a keg and inserts it into the partial slab list. 17428b987a77SJeff Roberson * The keg should be unlocked on entry. If the allocation succeeds it will 17438b987a77SJeff Roberson * be locked on return. 17448355f576SJeff Roberson * 17458355f576SJeff Roberson * Arguments: 174686220393SMark Johnston * flags Wait flags for the item initialization routine 174786220393SMark Johnston * aflags Wait flags for the slab allocation 17488355f576SJeff Roberson * 17498355f576SJeff Roberson * Returns: 17508355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 17518355f576SJeff Roberson * caller specified M_NOWAIT. 17528355f576SJeff Roberson */ 17538355f576SJeff Roberson static uma_slab_t 175486220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 175586220393SMark Johnston int aflags) 17568355f576SJeff Roberson { 17578b987a77SJeff Roberson uma_domain_t dom; 1758099a0e58SBosko Milekic uma_slab_t slab; 17592e47807cSJeff Roberson unsigned long size; 176085dcf349SGleb Smirnoff uint8_t *mem; 176186220393SMark Johnston uint8_t sflags; 17628355f576SJeff Roberson int i; 17638355f576SJeff Roberson 1764ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1765ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1766a553d4b8SJeff Roberson 1767194a979eSMark Johnston slab = NULL; 1768194a979eSMark Johnston mem = NULL; 176954c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 17709b8db4d0SRyan Libby uma_hash_slab_t hslab; 17719b8db4d0SRyan Libby hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL, 17729b8db4d0SRyan Libby domain, aflags); 17739b8db4d0SRyan Libby if (hslab == NULL) 1774727c6918SJeff Roberson goto fail; 17759b8db4d0SRyan Libby slab = &hslab->uhs_slab; 1776a553d4b8SJeff Roberson } 1777a553d4b8SJeff Roberson 17783370c5bfSJeff Roberson /* 17793370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 17803370c5bfSJeff Roberson * first time they are added to a zone. 17813370c5bfSJeff Roberson * 17823370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 17833370c5bfSJeff Roberson */ 17843370c5bfSJeff Roberson 1785099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 178686220393SMark Johnston aflags |= M_ZERO; 17873370c5bfSJeff Roberson else 178886220393SMark Johnston aflags &= ~M_ZERO; 17893370c5bfSJeff Roberson 1790263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 179186220393SMark Johnston aflags |= M_NODUMP; 1792263811f7SKip Macy 1793e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1794194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 179509c8cb71SMark Johnston mem = keg->uk_allocf(zone, size, domain, &sflags, aflags); 1796a553d4b8SJeff Roberson if (mem == NULL) { 179754c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 17989b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), 17999b8db4d0SRyan Libby slab_tohashslab(slab), NULL, SKIP_NONE); 1800727c6918SJeff Roberson goto fail; 1801a553d4b8SJeff Roberson } 18022e47807cSJeff Roberson uma_total_inc(size); 18038355f576SJeff Roberson 18048b987a77SJeff Roberson /* For HASH zones all pages go to the same uma_domain. */ 180554c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 18068b987a77SJeff Roberson domain = 0; 18078b987a77SJeff Roberson 18085c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 180954c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) 1810099a0e58SBosko Milekic slab = (uma_slab_t)(mem + keg->uk_pgoff); 18111e0701e1SJeff Roberson else 18129b8db4d0SRyan Libby slab_tohashslab(slab)->uhs_data = mem; 18135c0e403bSJeff Roberson 181454c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_VTOSLAB) 1815099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1816584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1817584061b4SJeff Roberson zone, slab); 18188355f576SJeff Roberson 1819099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 182086220393SMark Johnston slab->us_flags = sflags; 1821ab3185d1SJeff Roberson slab->us_domain = domain; 18228b987a77SJeff Roberson 18239b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1824ef72505eSJeff Roberson #ifdef INVARIANTS 1825815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1826ef72505eSJeff Roberson #endif 1827099a0e58SBosko Milekic 1828b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1829099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 18301e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 183186220393SMark Johnston keg->uk_size, flags) != 0) 1832b23f72e9SBrian Feldman break; 1833b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1834fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1835727c6918SJeff Roberson goto fail; 1836b23f72e9SBrian Feldman } 1837b23f72e9SBrian Feldman } 183809c8cb71SMark Johnston kasan_mark_slab_invalid(keg, mem); 18398b987a77SJeff Roberson KEG_LOCK(keg, domain); 18405c0e403bSJeff Roberson 18411431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 18421431a748SGleb Smirnoff slab, keg->uk_name, keg); 18431431a748SGleb Smirnoff 184454c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 1845099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 18468355f576SJeff Roberson 18478b987a77SJeff Roberson /* 18488b987a77SJeff Roberson * If we got a slab here it's safe to mark it partially used 18498b987a77SJeff Roberson * and return. We assume that the caller is going to remove 18508b987a77SJeff Roberson * at least one item. 18518b987a77SJeff Roberson */ 18528b987a77SJeff Roberson dom = &keg->uk_domain[domain]; 18538b987a77SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 18548b987a77SJeff Roberson dom->ud_pages += keg->uk_ppera; 18554ab3aee8SMark Johnston dom->ud_free_items += keg->uk_ipers; 18568355f576SJeff Roberson 18578355f576SJeff Roberson return (slab); 1858727c6918SJeff Roberson 1859727c6918SJeff Roberson fail: 1860727c6918SJeff Roberson return (NULL); 18618355f576SJeff Roberson } 18628355f576SJeff Roberson 18638355f576SJeff Roberson /* 1864537f92cdSMark Johnston * This function is intended to be used early on in place of page_alloc(). It 1865537f92cdSMark Johnston * performs contiguous physical memory allocations and uses a bump allocator for 1866537f92cdSMark Johnston * KVA, so is usable before the kernel map is initialized. 1867009b6fcbSJeff Roberson */ 1868009b6fcbSJeff Roberson static void * 1869ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1870ab3185d1SJeff Roberson int wait) 1871009b6fcbSJeff Roberson { 1872a81c400eSJeff Roberson vm_paddr_t pa; 1873a81c400eSJeff Roberson vm_page_t m; 187484c39222SMark Johnston int i, pages; 1875099a0e58SBosko Milekic 1876f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1877f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1878a81c400eSJeff Roberson 1879f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 188084c39222SMark Johnston m = vm_page_alloc_noobj_contig_domain(domain, malloc2vm_flags(wait) | 188184c39222SMark Johnston VM_ALLOC_WIRED, pages, (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, 188284c39222SMark Johnston VM_MEMATTR_DEFAULT); 1883a81c400eSJeff Roberson if (m == NULL) 1884a81c400eSJeff Roberson return (NULL); 1885a81c400eSJeff Roberson 1886a81c400eSJeff Roberson pa = VM_PAGE_TO_PHYS(m); 1887a81c400eSJeff Roberson for (i = 0; i < pages; i++, pa += PAGE_SIZE) { 1888c25a30e2SKonstantin Belousov #if defined(__aarch64__) || defined(__amd64__) || \ 1889a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1890a81c400eSJeff Roberson if ((wait & M_NODUMP) == 0) 1891a81c400eSJeff Roberson dump_add_page(pa); 1892a81c400eSJeff Roberson #endif 1893a81c400eSJeff Roberson } 1894f7d35785SGleb Smirnoff 189584c39222SMark Johnston /* Allocate KVA and indirectly advance bootmem. */ 189684c39222SMark Johnston return ((void *)pmap_map(&bootmem, m->phys_addr, 189784c39222SMark Johnston m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE)); 1898f7d35785SGleb Smirnoff } 1899f7d35785SGleb Smirnoff 1900a81c400eSJeff Roberson static void 1901a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes) 1902a81c400eSJeff Roberson { 1903a81c400eSJeff Roberson vm_offset_t va; 1904a81c400eSJeff Roberson vm_page_t m; 1905a81c400eSJeff Roberson 1906a81c400eSJeff Roberson va = (vm_offset_t)mem; 1907a81c400eSJeff Roberson m = PHYS_TO_VM_PAGE(pmap_kextract(va)); 1908663de81fSMark Johnston 1909663de81fSMark Johnston /* 1910663de81fSMark Johnston * startup_alloc() returns direct-mapped slabs on some platforms. Avoid 1911663de81fSMark Johnston * unmapping ranges of the direct map. 1912663de81fSMark Johnston */ 1913663de81fSMark Johnston if (va >= bootstart && va + bytes <= bootmem) 1914a81c400eSJeff Roberson pmap_remove(kernel_pmap, va, va + bytes); 1915a81c400eSJeff Roberson for (; bytes != 0; bytes -= PAGE_SIZE, m++) { 1916c25a30e2SKonstantin Belousov #if defined(__aarch64__) || defined(__amd64__) || \ 1917a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1918a81c400eSJeff Roberson dump_drop_page(VM_PAGE_TO_PHYS(m)); 1919a81c400eSJeff Roberson #endif 1920a81c400eSJeff Roberson vm_page_unwire_noq(m); 1921a81c400eSJeff Roberson vm_page_free(m); 1922a81c400eSJeff Roberson } 1923a81c400eSJeff Roberson } 1924a81c400eSJeff Roberson 1925f7d35785SGleb Smirnoff /* 19268355f576SJeff Roberson * Allocates a number of pages from the system 19278355f576SJeff Roberson * 19288355f576SJeff Roberson * Arguments: 19298355f576SJeff Roberson * bytes The number of bytes requested 19308355f576SJeff Roberson * wait Shall we wait? 19318355f576SJeff Roberson * 19328355f576SJeff Roberson * Returns: 19338355f576SJeff Roberson * A pointer to the alloced memory or possibly 19348355f576SJeff Roberson * NULL if M_NOWAIT is set. 19358355f576SJeff Roberson */ 19368355f576SJeff Roberson static void * 1937ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1938ab3185d1SJeff Roberson int wait) 19398355f576SJeff Roberson { 19408355f576SJeff Roberson void *p; /* Returned page */ 19418355f576SJeff Roberson 19422e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 19439978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 19448355f576SJeff Roberson 19458355f576SJeff Roberson return (p); 19468355f576SJeff Roberson } 19478355f576SJeff Roberson 1948ab3059a8SMatt Macy static void * 1949ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1950ab3059a8SMatt Macy int wait) 1951ab3059a8SMatt Macy { 1952ab3059a8SMatt Macy struct pglist alloctail; 1953ab3059a8SMatt Macy vm_offset_t addr, zkva; 1954ab3059a8SMatt Macy int cpu, flags; 1955ab3059a8SMatt Macy vm_page_t p, p_next; 1956ab3059a8SMatt Macy #ifdef NUMA 1957ab3059a8SMatt Macy struct pcpu *pc; 1958ab3059a8SMatt Macy #endif 1959ab3059a8SMatt Macy 1960ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1961ab3059a8SMatt Macy 1962013072f0SMark Johnston TAILQ_INIT(&alloctail); 1963a4667e09SMark Johnston flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | malloc2vm_flags(wait); 1964013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1965ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1966ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1967a4667e09SMark Johnston p = vm_page_alloc_noobj(flags); 1968ab3059a8SMatt Macy } else { 1969ab3059a8SMatt Macy #ifndef NUMA 1970a4667e09SMark Johnston p = vm_page_alloc_noobj(flags); 1971ab3059a8SMatt Macy #else 1972ab3059a8SMatt Macy pc = pcpu_find(cpu); 197320526802SAndrew Gallatin if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain))) 197420526802SAndrew Gallatin p = NULL; 197520526802SAndrew Gallatin else 1976a4667e09SMark Johnston p = vm_page_alloc_noobj_domain(pc->pc_domain, 1977a4667e09SMark Johnston flags); 1978ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1979a4667e09SMark Johnston p = vm_page_alloc_noobj(flags); 1980ab3059a8SMatt Macy #endif 1981ab3059a8SMatt Macy } 1982ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1983ab3059a8SMatt Macy goto fail; 1984ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1985ab3059a8SMatt Macy } 1986ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1987ab3059a8SMatt Macy goto fail; 1988ab3059a8SMatt Macy zkva = addr; 1989ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1990ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1991ab3059a8SMatt Macy zkva += PAGE_SIZE; 1992ab3059a8SMatt Macy } 1993ab3059a8SMatt Macy return ((void*)addr); 1994ab3059a8SMatt Macy fail: 1995ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 199688ea538aSMark Johnston vm_page_unwire_noq(p); 1997ab3059a8SMatt Macy vm_page_free(p); 1998ab3059a8SMatt Macy } 1999ab3059a8SMatt Macy return (NULL); 2000ab3059a8SMatt Macy } 2001ab3059a8SMatt Macy 20028355f576SJeff Roberson /* 2003a9d6f1feSMark Johnston * Allocates a number of pages not belonging to a VM object 20048355f576SJeff Roberson * 20058355f576SJeff Roberson * Arguments: 20068355f576SJeff Roberson * bytes The number of bytes requested 20078355f576SJeff Roberson * wait Shall we wait? 20088355f576SJeff Roberson * 20098355f576SJeff Roberson * Returns: 20108355f576SJeff Roberson * A pointer to the alloced memory or possibly 20118355f576SJeff Roberson * NULL if M_NOWAIT is set. 20128355f576SJeff Roberson */ 20138355f576SJeff Roberson static void * 2014ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 2015ab3185d1SJeff Roberson int wait) 20168355f576SJeff Roberson { 2017a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 2018a4915c21SAttilio Rao u_long npages; 2019b245ac95SAlan Cox vm_offset_t retkva, zkva; 2020a4915c21SAttilio Rao vm_page_t p, p_next; 2021e20a199fSJeff Roberson uma_keg_t keg; 2022a4667e09SMark Johnston int req; 20238355f576SJeff Roberson 2024a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 2025bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 2026a4667e09SMark Johnston req = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED; 2027a4667e09SMark Johnston if ((wait & M_WAITOK) != 0) 2028a4667e09SMark Johnston req |= VM_ALLOC_WAITOK; 2029a4915c21SAttilio Rao 2030a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 2031a4915c21SAttilio Rao while (npages > 0) { 2032a4667e09SMark Johnston p = vm_page_alloc_noobj_domain(domain, req); 2033a4915c21SAttilio Rao if (p != NULL) { 2034a4915c21SAttilio Rao /* 2035a4915c21SAttilio Rao * Since the page does not belong to an object, its 2036a4915c21SAttilio Rao * listq is unused. 2037a4915c21SAttilio Rao */ 2038a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 2039a4915c21SAttilio Rao npages--; 2040a4915c21SAttilio Rao continue; 2041a4915c21SAttilio Rao } 20428355f576SJeff Roberson /* 2043a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 2044a4915c21SAttilio Rao * exit. 20458355f576SJeff Roberson */ 2046a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 204788ea538aSMark Johnston vm_page_unwire_noq(p); 2048b245ac95SAlan Cox vm_page_free(p); 2049b245ac95SAlan Cox } 2050a4915c21SAttilio Rao return (NULL); 2051b245ac95SAlan Cox } 20528355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 2053a4915c21SAttilio Rao zkva = keg->uk_kva + 2054a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 2055a4915c21SAttilio Rao retkva = zkva; 2056a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 2057a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 2058a4915c21SAttilio Rao zkva += PAGE_SIZE; 2059a4915c21SAttilio Rao } 20608355f576SJeff Roberson 20618355f576SJeff Roberson return ((void *)retkva); 20628355f576SJeff Roberson } 20638355f576SJeff Roberson 20648355f576SJeff Roberson /* 2065ec0d8280SRyan Libby * Allocate physically contiguous pages. 2066ec0d8280SRyan Libby */ 2067ec0d8280SRyan Libby static void * 2068ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 2069ec0d8280SRyan Libby int wait) 2070ec0d8280SRyan Libby { 2071ec0d8280SRyan Libby 2072ec0d8280SRyan Libby *pflag = UMA_SLAB_KERNEL; 2073ec0d8280SRyan Libby return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain), 2074ec0d8280SRyan Libby bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT)); 2075ec0d8280SRyan Libby } 2076ec0d8280SRyan Libby 2077ec0d8280SRyan Libby /* 20788355f576SJeff Roberson * Frees a number of pages to the system 20798355f576SJeff Roberson * 20808355f576SJeff Roberson * Arguments: 20818355f576SJeff Roberson * mem A pointer to the memory to be freed 20828355f576SJeff Roberson * size The size of the memory being freed 20838355f576SJeff Roberson * flags The original p->us_flags field 20848355f576SJeff Roberson * 20858355f576SJeff Roberson * Returns: 20868355f576SJeff Roberson * Nothing 20878355f576SJeff Roberson */ 20888355f576SJeff Roberson static void 2089f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 20908355f576SJeff Roberson { 20913370c5bfSJeff Roberson 2092a81c400eSJeff Roberson if ((flags & UMA_SLAB_BOOT) != 0) { 2093a81c400eSJeff Roberson startup_free(mem, size); 2094a81c400eSJeff Roberson return; 2095a81c400eSJeff Roberson } 2096a81c400eSJeff Roberson 2097ec0d8280SRyan Libby KASSERT((flags & UMA_SLAB_KERNEL) != 0, 2098ec0d8280SRyan Libby ("UMA: page_free used with invalid flags %x", flags)); 20998355f576SJeff Roberson 210049bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 21018355f576SJeff Roberson } 21028355f576SJeff Roberson 21038355f576SJeff Roberson /* 2104ab3059a8SMatt Macy * Frees pcpu zone allocations 2105ab3059a8SMatt Macy * 2106ab3059a8SMatt Macy * Arguments: 2107ab3059a8SMatt Macy * mem A pointer to the memory to be freed 2108ab3059a8SMatt Macy * size The size of the memory being freed 2109ab3059a8SMatt Macy * flags The original p->us_flags field 2110ab3059a8SMatt Macy * 2111ab3059a8SMatt Macy * Returns: 2112ab3059a8SMatt Macy * Nothing 2113ab3059a8SMatt Macy */ 2114ab3059a8SMatt Macy static void 2115ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 2116ab3059a8SMatt Macy { 2117ab3059a8SMatt Macy vm_offset_t sva, curva; 2118ab3059a8SMatt Macy vm_paddr_t paddr; 2119ab3059a8SMatt Macy vm_page_t m; 2120ab3059a8SMatt Macy 2121ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 21225ba16cf3SRyan Libby 21235ba16cf3SRyan Libby if ((flags & UMA_SLAB_BOOT) != 0) { 21245ba16cf3SRyan Libby startup_free(mem, size); 21255ba16cf3SRyan Libby return; 21265ba16cf3SRyan Libby } 21275ba16cf3SRyan Libby 2128ab3059a8SMatt Macy sva = (vm_offset_t)mem; 2129ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 2130ab3059a8SMatt Macy paddr = pmap_kextract(curva); 2131ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 213288ea538aSMark Johnston vm_page_unwire_noq(m); 2133ab3059a8SMatt Macy vm_page_free(m); 2134ab3059a8SMatt Macy } 2135ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 2136ab3059a8SMatt Macy kva_free(sva, size); 2137ab3059a8SMatt Macy } 2138ab3059a8SMatt Macy 2139ab3059a8SMatt Macy /* 21408355f576SJeff Roberson * Zero fill initializer 21418355f576SJeff Roberson * 21428355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 21438355f576SJeff Roberson */ 2144b23f72e9SBrian Feldman static int 2145b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 21468355f576SJeff Roberson { 21478355f576SJeff Roberson bzero(mem, size); 2148b23f72e9SBrian Feldman return (0); 21498355f576SJeff Roberson } 21508355f576SJeff Roberson 2151815db204SRyan Libby #ifdef INVARIANTS 215254007ce8SMark Johnston static struct noslabbits * 2153815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 2154815db204SRyan Libby { 2155815db204SRyan Libby 2156815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 2157815db204SRyan Libby } 2158815db204SRyan Libby #endif 2159815db204SRyan Libby 21608355f576SJeff Roberson /* 21619b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 21629b78b1f4SJeff Roberson */ 216354007ce8SMark Johnston static size_t 21649b78b1f4SJeff Roberson slab_sizeof(int nitems) 21659b78b1f4SJeff Roberson { 21669b78b1f4SJeff Roberson size_t s; 21679b78b1f4SJeff Roberson 2168815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 21699b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 21709b78b1f4SJeff Roberson } 21719b78b1f4SJeff Roberson 21724a8b575cSRyan Libby #define UMA_FIXPT_SHIFT 31 21734a8b575cSRyan Libby #define UMA_FRAC_FIXPT(n, d) \ 21744a8b575cSRyan Libby ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d))) 21754a8b575cSRyan Libby #define UMA_FIXPT_PCT(f) \ 21764a8b575cSRyan Libby ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT)) 21774a8b575cSRyan Libby #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100) 21784a8b575cSRyan Libby #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE) 21794a8b575cSRyan Libby 21809b78b1f4SJeff Roberson /* 21814a8b575cSRyan Libby * Compute the number of items that will fit in a slab. If hdr is true, the 21824a8b575cSRyan Libby * item count may be limited to provide space in the slab for an inline slab 21834a8b575cSRyan Libby * header. Otherwise, all slab space will be provided for item storage. 21844a8b575cSRyan Libby */ 21854a8b575cSRyan Libby static u_int 21864a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr) 21874a8b575cSRyan Libby { 21884a8b575cSRyan Libby u_int ipers; 21894a8b575cSRyan Libby u_int padpi; 21904a8b575cSRyan Libby 21914a8b575cSRyan Libby /* The padding between items is not needed after the last item. */ 21924a8b575cSRyan Libby padpi = rsize - size; 21934a8b575cSRyan Libby 21944a8b575cSRyan Libby if (hdr) { 21954a8b575cSRyan Libby /* 21964a8b575cSRyan Libby * Start with the maximum item count and remove items until 21974a8b575cSRyan Libby * the slab header first alongside the allocatable memory. 21984a8b575cSRyan Libby */ 21994a8b575cSRyan Libby for (ipers = MIN(SLAB_MAX_SETSIZE, 22004a8b575cSRyan Libby (slabsize + padpi - slab_sizeof(1)) / rsize); 22014a8b575cSRyan Libby ipers > 0 && 22024a8b575cSRyan Libby ipers * rsize - padpi + slab_sizeof(ipers) > slabsize; 22034a8b575cSRyan Libby ipers--) 22044a8b575cSRyan Libby continue; 22054a8b575cSRyan Libby } else { 22064a8b575cSRyan Libby ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE); 22074a8b575cSRyan Libby } 22084a8b575cSRyan Libby 22094a8b575cSRyan Libby return (ipers); 22104a8b575cSRyan Libby } 22114a8b575cSRyan Libby 221227ca37acSRyan Libby struct keg_layout_result { 221327ca37acSRyan Libby u_int format; 221427ca37acSRyan Libby u_int slabsize; 221527ca37acSRyan Libby u_int ipers; 221627ca37acSRyan Libby u_int eff; 221727ca37acSRyan Libby }; 221827ca37acSRyan Libby 221927ca37acSRyan Libby static void 222027ca37acSRyan Libby keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt, 222127ca37acSRyan Libby struct keg_layout_result *kl) 222227ca37acSRyan Libby { 222327ca37acSRyan Libby u_int total; 222427ca37acSRyan Libby 222527ca37acSRyan Libby kl->format = fmt; 222627ca37acSRyan Libby kl->slabsize = slabsize; 222727ca37acSRyan Libby 222827ca37acSRyan Libby /* Handle INTERNAL as inline with an extra page. */ 222927ca37acSRyan Libby if ((fmt & UMA_ZFLAG_INTERNAL) != 0) { 223027ca37acSRyan Libby kl->format &= ~UMA_ZFLAG_INTERNAL; 223127ca37acSRyan Libby kl->slabsize += PAGE_SIZE; 223227ca37acSRyan Libby } 223327ca37acSRyan Libby 223427ca37acSRyan Libby kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize, 223527ca37acSRyan Libby (fmt & UMA_ZFLAG_OFFPAGE) == 0); 223627ca37acSRyan Libby 223727ca37acSRyan Libby /* Account for memory used by an offpage slab header. */ 223827ca37acSRyan Libby total = kl->slabsize; 223927ca37acSRyan Libby if ((fmt & UMA_ZFLAG_OFFPAGE) != 0) 224027ca37acSRyan Libby total += slabzone(kl->ipers)->uz_keg->uk_rsize; 224127ca37acSRyan Libby 224227ca37acSRyan Libby kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total); 224327ca37acSRyan Libby } 224427ca37acSRyan Libby 22459b78b1f4SJeff Roberson /* 22464a8b575cSRyan Libby * Determine the format of a uma keg. This determines where the slab header 22474a8b575cSRyan Libby * will be placed (inline or offpage) and calculates ipers, rsize, and ppera. 22488355f576SJeff Roberson * 22498355f576SJeff Roberson * Arguments 2250e20a199fSJeff Roberson * keg The zone we should initialize 22518355f576SJeff Roberson * 22528355f576SJeff Roberson * Returns 22538355f576SJeff Roberson * Nothing 22548355f576SJeff Roberson */ 22558355f576SJeff Roberson static void 22564a8b575cSRyan Libby keg_layout(uma_keg_t keg) 22578355f576SJeff Roberson { 225827ca37acSRyan Libby struct keg_layout_result kl = {}, kl_tmp; 225927ca37acSRyan Libby u_int fmts[2]; 22604a8b575cSRyan Libby u_int alignsize; 226127ca37acSRyan Libby u_int nfmt; 22624a8b575cSRyan Libby u_int pages; 2263244f4554SBosko Milekic u_int rsize; 2264a55ebb7cSAndriy Gapon u_int slabsize; 226527ca37acSRyan Libby u_int i, j; 22668355f576SJeff Roberson 22674a8b575cSRyan Libby KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 22684a8b575cSRyan Libby (keg->uk_size <= UMA_PCPU_ALLOC_SIZE && 22694a8b575cSRyan Libby (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0), 22704a8b575cSRyan Libby ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b", 22714a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_size, keg->uk_flags, 22724a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 2273bae55c4aSRyan Libby KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 || 22744a8b575cSRyan Libby (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0, 22754a8b575cSRyan Libby ("%s: incompatible flags 0x%b", __func__, keg->uk_flags, 22764a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 2277e28a647dSGleb Smirnoff 22784a8b575cSRyan Libby alignsize = keg->uk_align + 1; 2279b0dfc486SMark Johnston #ifdef KASAN 2280b0dfc486SMark Johnston /* 2281b0dfc486SMark Johnston * ASAN requires that each allocation be aligned to the shadow map 2282b0dfc486SMark Johnston * scale factor. 2283b0dfc486SMark Johnston */ 2284b0dfc486SMark Johnston if (alignsize < KASAN_SHADOW_SCALE) 2285b0dfc486SMark Johnston alignsize = KASAN_SHADOW_SCALE; 2286b0dfc486SMark Johnston #endif 2287ad97af7eSGleb Smirnoff 2288ef72505eSJeff Roberson /* 2289ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 2290ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 2291ef72505eSJeff Roberson * allocation bits for we round it up. 2292ef72505eSJeff Roberson */ 22939b8db4d0SRyan Libby rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT); 22944a8b575cSRyan Libby rsize = roundup2(rsize, alignsize); 2295ad97af7eSGleb Smirnoff 229627ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) { 22979b78b1f4SJeff Roberson /* 22984a8b575cSRyan Libby * We want one item to start on every align boundary in a page. 22994a8b575cSRyan Libby * To do this we will span pages. We will also extend the item 23004a8b575cSRyan Libby * by the size of align if it is an even multiple of align. 23014a8b575cSRyan Libby * Otherwise, it would fall on the same boundary every time. 23029b78b1f4SJeff Roberson */ 23034a8b575cSRyan Libby if ((rsize & alignsize) == 0) 23044a8b575cSRyan Libby rsize += alignsize; 23054a8b575cSRyan Libby slabsize = rsize * (PAGE_SIZE / alignsize); 23064a8b575cSRyan Libby slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE); 23074a8b575cSRyan Libby slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE); 230827ca37acSRyan Libby slabsize = round_page(slabsize); 23094a8b575cSRyan Libby } else { 23104a8b575cSRyan Libby /* 231127ca37acSRyan Libby * Start with a slab size of as many pages as it takes to 231227ca37acSRyan Libby * represent a single item. We will try to fit as many 231327ca37acSRyan Libby * additional items into the slab as possible. 23144a8b575cSRyan Libby */ 231527ca37acSRyan Libby slabsize = round_page(keg->uk_size); 23161ca6ed45SGleb Smirnoff } 2317ad97af7eSGleb Smirnoff 231827ca37acSRyan Libby /* Build a list of all of the available formats for this keg. */ 231927ca37acSRyan Libby nfmt = 0; 232027ca37acSRyan Libby 23214a8b575cSRyan Libby /* Evaluate an inline slab layout. */ 23224a8b575cSRyan Libby if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0) 232327ca37acSRyan Libby fmts[nfmt++] = 0; 23244a8b575cSRyan Libby 23254a8b575cSRyan Libby /* TODO: vm_page-embedded slab. */ 2326244f4554SBosko Milekic 232720e8e865SBosko Milekic /* 2328244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 232920e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 2330bae55c4aSRyan Libby * may end up going to the VM for slabs which we do not want 2331bae55c4aSRyan Libby * to do if we're UMA_ZONE_VM, which clearly forbids it. 2332bae55c4aSRyan Libby * In those cases, evaluate a pseudo-format called INTERNAL 2333bae55c4aSRyan Libby * which has an inline slab header and one extra page to 2334bae55c4aSRyan Libby * guarantee that it fits. 233527ca37acSRyan Libby * 233627ca37acSRyan Libby * Otherwise, see if using an OFFPAGE slab will improve our 233727ca37acSRyan Libby * efficiency. 233820e8e865SBosko Milekic */ 2339bae55c4aSRyan Libby if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0) 234027ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_INTERNAL; 234127ca37acSRyan Libby else 234227ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_OFFPAGE; 2343244f4554SBosko Milekic 2344ef72505eSJeff Roberson /* 234527ca37acSRyan Libby * Choose a slab size and format which satisfy the minimum efficiency. 234627ca37acSRyan Libby * Prefer the smallest slab size that meets the constraints. 2347ef72505eSJeff Roberson * 234827ca37acSRyan Libby * Start with a minimum slab size, to accommodate CACHESPREAD. Then, 234927ca37acSRyan Libby * for small items (up to PAGE_SIZE), the iteration increment is one 235027ca37acSRyan Libby * page; and for large items, the increment is one item. 2351ef72505eSJeff Roberson */ 235227ca37acSRyan Libby i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize); 235327ca37acSRyan Libby KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u", 235427ca37acSRyan Libby keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize, 235527ca37acSRyan Libby rsize, i)); 235627ca37acSRyan Libby for ( ; ; i++) { 235727ca37acSRyan Libby slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) : 235827ca37acSRyan Libby round_page(rsize * (i - 1) + keg->uk_size); 235927ca37acSRyan Libby 236027ca37acSRyan Libby for (j = 0; j < nfmt; j++) { 236127ca37acSRyan Libby /* Only if we have no viable format yet. */ 236227ca37acSRyan Libby if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 && 236327ca37acSRyan Libby kl.ipers > 0) 236427ca37acSRyan Libby continue; 236527ca37acSRyan Libby 236627ca37acSRyan Libby keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp); 236727ca37acSRyan Libby if (kl_tmp.eff <= kl.eff) 236827ca37acSRyan Libby continue; 236927ca37acSRyan Libby 237027ca37acSRyan Libby kl = kl_tmp; 237127ca37acSRyan Libby 237227ca37acSRyan Libby CTR6(KTR_UMA, "keg %s layout: format %#x " 237327ca37acSRyan Libby "(ipers %u * rsize %u) / slabsize %#x = %u%% eff", 237427ca37acSRyan Libby keg->uk_name, kl.format, kl.ipers, rsize, 237527ca37acSRyan Libby kl.slabsize, UMA_FIXPT_PCT(kl.eff)); 237627ca37acSRyan Libby 237727ca37acSRyan Libby /* Stop when we reach the minimum efficiency. */ 237827ca37acSRyan Libby if (kl.eff >= UMA_MIN_EFF) 237927ca37acSRyan Libby break; 23808355f576SJeff Roberson } 2381ad97af7eSGleb Smirnoff 238233e5a1eaSRyan Libby if (kl.eff >= UMA_MIN_EFF || !multipage_slabs || 238327ca37acSRyan Libby slabsize >= SLAB_MAX_SETSIZE * rsize || 238427ca37acSRyan Libby (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0) 238527ca37acSRyan Libby break; 238627ca37acSRyan Libby } 238727ca37acSRyan Libby 238827ca37acSRyan Libby pages = atop(kl.slabsize); 238927ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 239027ca37acSRyan Libby pages *= mp_maxid + 1; 239127ca37acSRyan Libby 239227ca37acSRyan Libby keg->uk_rsize = rsize; 239327ca37acSRyan Libby keg->uk_ipers = kl.ipers; 239427ca37acSRyan Libby keg->uk_ppera = pages; 239527ca37acSRyan Libby keg->uk_flags |= kl.format; 239627ca37acSRyan Libby 23974a8b575cSRyan Libby /* 23984a8b575cSRyan Libby * How do we find the slab header if it is offpage or if not all item 23994a8b575cSRyan Libby * start addresses are in the same page? We could solve the latter 24004a8b575cSRyan Libby * case with vaddr alignment, but we don't. 24014a8b575cSRyan Libby */ 240227ca37acSRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 || 240327ca37acSRyan Libby (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) { 240454c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0) 240527ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_HASH; 240654c5ae80SRyan Libby else 240727ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 240854c5ae80SRyan Libby } 240927ca37acSRyan Libby 2410e63a1c2fSRyan Libby CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u", 241127ca37acSRyan Libby __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers, 241227ca37acSRyan Libby pages); 24134a8b575cSRyan Libby KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 24144a8b575cSRyan Libby ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__, 241527ca37acSRyan Libby keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize, 241627ca37acSRyan Libby keg->uk_ipers, pages)); 2417e20a199fSJeff Roberson } 2418e20a199fSJeff Roberson 24198355f576SJeff Roberson /* 2420099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 2421099a0e58SBosko Milekic * the keg onto the global keg list. 24228355f576SJeff Roberson * 24238355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 2424099a0e58SBosko Milekic * udata Actually uma_kctor_args 2425099a0e58SBosko Milekic */ 2426b23f72e9SBrian Feldman static int 2427b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 2428099a0e58SBosko Milekic { 2429099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 2430099a0e58SBosko Milekic uma_keg_t keg = mem; 2431099a0e58SBosko Milekic uma_zone_t zone; 24328b987a77SJeff Roberson int i; 2433099a0e58SBosko Milekic 2434099a0e58SBosko Milekic bzero(keg, size); 2435099a0e58SBosko Milekic keg->uk_size = arg->size; 2436099a0e58SBosko Milekic keg->uk_init = arg->uminit; 2437099a0e58SBosko Milekic keg->uk_fini = arg->fini; 2438099a0e58SBosko Milekic keg->uk_align = arg->align; 24396fd34d6fSJeff Roberson keg->uk_reserve = 0; 2440099a0e58SBosko Milekic keg->uk_flags = arg->flags; 2441099a0e58SBosko Milekic 2442099a0e58SBosko Milekic /* 2443194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 2444dfe13344SJeff Roberson * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which 2445dfe13344SJeff Roberson * case the iterator is never run. 2446194a979eSMark Johnston */ 2447194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 2448194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 2449194a979eSMark Johnston 2450194a979eSMark Johnston /* 2451c8b0a88bSJeff Roberson * The primary zone is passed to us at keg-creation time. 2452099a0e58SBosko Milekic */ 2453099a0e58SBosko Milekic zone = arg->zone; 2454e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 2455099a0e58SBosko Milekic 2456099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 2457099a0e58SBosko Milekic keg->uk_init = zero_init; 2458099a0e58SBosko Milekic 2459cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 246054c5ae80SRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 2461e20a199fSJeff Roberson 246254c5ae80SRyan Libby #ifndef SMP 2463ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 2464ad97af7eSGleb Smirnoff #endif 2465ad97af7eSGleb Smirnoff 24664a8b575cSRyan Libby keg_layout(keg); 2467099a0e58SBosko Milekic 24688b987a77SJeff Roberson /* 2469c6fd3e23SJeff Roberson * Use a first-touch NUMA policy for kegs that pmap_extract() will 2470c6fd3e23SJeff Roberson * work on. Use round-robin for everything else. 2471dfe13344SJeff Roberson * 2472dfe13344SJeff Roberson * Zones may override the default by specifying either. 24738b987a77SJeff Roberson */ 2474dfe13344SJeff Roberson #ifdef NUMA 2475dfe13344SJeff Roberson if ((keg->uk_flags & 2476c6fd3e23SJeff Roberson (UMA_ZONE_ROUNDROBIN | UMA_ZFLAG_CACHE | UMA_ZONE_NOTPAGE)) == 0) 2477dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_FIRSTTOUCH; 2478dfe13344SJeff Roberson else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2479dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_ROUNDROBIN; 24808b987a77SJeff Roberson #endif 24818b987a77SJeff Roberson 2482099a0e58SBosko Milekic /* 2483099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 2484099a0e58SBosko Milekic * startup cache until the vm is ready. 2485099a0e58SBosko Milekic */ 248677e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2487a81c400eSJeff Roberson if (keg->uk_ppera == 1) 248877e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 2489a81c400eSJeff Roberson else 24908cd02d00SAlan Cox #endif 2491a81c400eSJeff Roberson if (booted < BOOT_KVA) 2492a81c400eSJeff Roberson keg->uk_allocf = startup_alloc; 2493ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 2494ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 2495ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1) 2496ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 249777e19437SGleb Smirnoff else 249877e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 249977e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 250077e19437SGleb Smirnoff if (keg->uk_ppera == 1) 250177e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 250277e19437SGleb Smirnoff else 250377e19437SGleb Smirnoff #endif 2504ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 2505ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 2506ab3059a8SMatt Macy else 250777e19437SGleb Smirnoff keg->uk_freef = page_free; 2508099a0e58SBosko Milekic 2509099a0e58SBosko Milekic /* 25108b987a77SJeff Roberson * Initialize keg's locks. 2511099a0e58SBosko Milekic */ 25128b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 25138b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 2514099a0e58SBosko Milekic 2515099a0e58SBosko Milekic /* 2516099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 25179b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 25189b78b1f4SJeff Roberson * definition. 2519099a0e58SBosko Milekic */ 252054c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) { 25219b78b1f4SJeff Roberson size_t shsize; 25229b78b1f4SJeff Roberson 25239b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 25249b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 2525244f4554SBosko Milekic /* 2526244f4554SBosko Milekic * The only way the following is possible is if with our 2527244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 2528244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 2529244f4554SBosko Milekic * mathematically possible for all cases, so we make 2530244f4554SBosko Milekic * sure here anyway. 2531244f4554SBosko Milekic */ 25329b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 25333d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 25343d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 2535099a0e58SBosko Milekic } 2536099a0e58SBosko Milekic 253754c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 25383b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 2539099a0e58SBosko Milekic 2540e63a1c2fSRyan Libby CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone); 2541099a0e58SBosko Milekic 2542099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 2543099a0e58SBosko Milekic 2544111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2545099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2546111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2547b23f72e9SBrian Feldman return (0); 2548099a0e58SBosko Milekic } 2549099a0e58SBosko Milekic 25502efcc8cbSGleb Smirnoff static void 2551a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused) 2552a81c400eSJeff Roberson { 2553a81c400eSJeff Roberson uma_keg_t keg; 2554a81c400eSJeff Roberson 2555a81c400eSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 2556a81c400eSJeff Roberson return; 2557a81c400eSJeff Roberson KEG_GET(zone, keg); 2558ec0d8280SRyan Libby 2559ec0d8280SRyan Libby if (keg->uk_allocf == startup_alloc) { 2560ec0d8280SRyan Libby /* Switch to the real allocator. */ 2561f96d4157SJeff Roberson if (keg->uk_flags & UMA_ZONE_PCPU) 2562f96d4157SJeff Roberson keg->uk_allocf = pcpu_page_alloc; 2563ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && 2564ec0d8280SRyan Libby keg->uk_ppera > 1) 2565ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 2566ec0d8280SRyan Libby else 2567a81c400eSJeff Roberson keg->uk_allocf = page_alloc; 2568a81c400eSJeff Roberson } 2569ec0d8280SRyan Libby } 2570a81c400eSJeff Roberson 2571a81c400eSJeff Roberson static void 257220a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 25732efcc8cbSGleb Smirnoff { 25742efcc8cbSGleb Smirnoff 25752efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 25762efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 25772efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 2578c6fd3e23SJeff Roberson zone->uz_xdomain = counter_u64_alloc(M_WAITOK); 25792efcc8cbSGleb Smirnoff } 25802efcc8cbSGleb Smirnoff 258120a4e154SJeff Roberson static void 258220a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 258320a4e154SJeff Roberson { 258420a4e154SJeff Roberson uma_zone_domain_t zdom; 25858b987a77SJeff Roberson uma_domain_t dom; 258620a4e154SJeff Roberson uma_keg_t keg; 258720a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 25883b490537SJeff Roberson int domains, i, cnt; 258920a4e154SJeff Roberson static const char *nokeg = "cache zone"; 259020a4e154SJeff Roberson char *c; 259120a4e154SJeff Roberson 259220a4e154SJeff Roberson /* 259320a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 259420a4e154SJeff Roberson * any special characters and handling dups by appending 259520a4e154SJeff Roberson * an index. 259620a4e154SJeff Roberson */ 259720a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 25983b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 25993b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 26003b490537SJeff Roberson cnt /= 10; 26013b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 26023b490537SJeff Roberson M_UMA, M_WAITOK); 260320a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 260420a4e154SJeff Roberson zone->uz_namecnt); 260520a4e154SJeff Roberson } else 260620a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 260720a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 260820a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 260920a4e154SJeff Roberson *c = '_'; 261020a4e154SJeff Roberson 261120a4e154SJeff Roberson /* 261220a4e154SJeff Roberson * Basic parameters at the root. 261320a4e154SJeff Roberson */ 261420a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 26157029da5cSPawel Biernacki OID_AUTO, zone->uz_ctlname, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 261620a4e154SJeff Roberson oid = zone->uz_oid; 261720a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 261820a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 26196d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 26206d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 26216d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 262220a4e154SJeff Roberson "Allocator configuration flags"); 262320a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 262420a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 262520a4e154SJeff Roberson "Desired per-cpu cache size"); 262620a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 262720a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 262820a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 262920a4e154SJeff Roberson 263020a4e154SJeff Roberson /* 263120a4e154SJeff Roberson * keg if present. 263220a4e154SJeff Roberson */ 263354c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 26348b987a77SJeff Roberson domains = vm_ndomains; 26358b987a77SJeff Roberson else 26368b987a77SJeff Roberson domains = 1; 263720a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 26387029da5cSPawel Biernacki "keg", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 263920a4e154SJeff Roberson keg = zone->uz_keg; 26403b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 264120a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 264220a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 264320a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 264420a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 264520a4e154SJeff Roberson "Real object size with alignment"); 264620a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 264720a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 264820a4e154SJeff Roberson "pages per-slab allocation"); 264920a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 265020a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 265120a4e154SJeff Roberson "items available per-slab"); 265220a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 265320a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 265420a4e154SJeff Roberson "item alignment mask"); 2655f09cbea3SMark Johnston SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2656f09cbea3SMark Johnston "reserve", CTLFLAG_RD, &keg->uk_reserve, 0, 2657f09cbea3SMark Johnston "number of reserved items"); 2658f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2659f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2660f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2661f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 26628b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 26637029da5cSPawel Biernacki OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 26648b987a77SJeff Roberson for (i = 0; i < domains; i++) { 26658b987a77SJeff Roberson dom = &keg->uk_domain[i]; 26668b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 26677029da5cSPawel Biernacki OID_AUTO, VM_DOMAIN(i)->vmd_name, 26687029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 26698b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 26708b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 26718b987a77SJeff Roberson "Total pages currently allocated from VM"); 26728b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 26734ab3aee8SMark Johnston "free_items", CTLFLAG_RD, &dom->ud_free_items, 0, 2674d6e77cdaSMark Johnston "Items free in the slab layer"); 2675d6e77cdaSMark Johnston SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2676d6e77cdaSMark Johnston "free_slabs", CTLFLAG_RD, &dom->ud_free_slabs, 0, 2677d6e77cdaSMark Johnston "Unused slabs"); 26788b987a77SJeff Roberson } 267920a4e154SJeff Roberson } else 268020a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 268120a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 268220a4e154SJeff Roberson 268320a4e154SJeff Roberson /* 268420a4e154SJeff Roberson * Information about zone limits. 268520a4e154SJeff Roberson */ 268620a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 26877029da5cSPawel Biernacki "limit", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 26884bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 26894bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 26904bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 2691e574d407SMark Johnston "Current number of allocated items if limit is set"); 269220a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 269320a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 2694e574d407SMark Johnston "Maximum number of allocated and cached items"); 269520a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 269620a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 269720a4e154SJeff Roberson "Number of threads sleeping at limit"); 269820a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 269920a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 270020a4e154SJeff Roberson "Total zone limit sleeps"); 27014bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2702c6fd3e23SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bucket_max, 0, 2703c6fd3e23SJeff Roberson "Maximum number of items in each domain's bucket cache"); 270420a4e154SJeff Roberson 270520a4e154SJeff Roberson /* 27068b987a77SJeff Roberson * Per-domain zone information. 270720a4e154SJeff Roberson */ 270820a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 27097029da5cSPawel Biernacki OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 271020a4e154SJeff Roberson for (i = 0; i < domains; i++) { 2711c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 271220a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 27137029da5cSPawel Biernacki OID_AUTO, VM_DOMAIN(i)->vmd_name, 27147029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 271520a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 271620a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 271720a4e154SJeff Roberson "number of items in this domain"); 271820a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 271920a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 272020a4e154SJeff Roberson "maximum item count in this period"); 272120a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 272220a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 272320a4e154SJeff Roberson "minimum item count in this period"); 272420a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 27252760658bSAlexander Motin "bimin", CTLFLAG_RD, &zdom->uzd_bimin, 27262760658bSAlexander Motin "Minimum item count in this batch"); 27272760658bSAlexander Motin SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 272820a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 272920a4e154SJeff Roberson "Working set size"); 27302760658bSAlexander Motin SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 27312760658bSAlexander Motin "limin", CTLFLAG_RD, &zdom->uzd_limin, 27322760658bSAlexander Motin "Long time minimum item count"); 27332760658bSAlexander Motin SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 27342760658bSAlexander Motin "timin", CTLFLAG_RD, &zdom->uzd_timin, 0, 27352760658bSAlexander Motin "Time since zero long time minimum item count"); 273620a4e154SJeff Roberson } 273720a4e154SJeff Roberson 273820a4e154SJeff Roberson /* 273920a4e154SJeff Roberson * General statistics. 274020a4e154SJeff Roberson */ 274120a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 27427029da5cSPawel Biernacki "stats", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 274320a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 274420a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 274520a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 274620a4e154SJeff Roberson "Current number of allocated items"); 274720a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 274820a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 274920a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 275020a4e154SJeff Roberson "Total allocation calls"); 275120a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 275220a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 275320a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 275420a4e154SJeff Roberson "Total free calls"); 275520a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 275620a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 275720a4e154SJeff Roberson "Number of allocation failures"); 2758c6fd3e23SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2759c6fd3e23SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 276020a4e154SJeff Roberson "Free calls from the wrong domain"); 276120a4e154SJeff Roberson } 276220a4e154SJeff Roberson 276320a4e154SJeff Roberson struct uma_zone_count { 276420a4e154SJeff Roberson const char *name; 276520a4e154SJeff Roberson int count; 276620a4e154SJeff Roberson }; 276720a4e154SJeff Roberson 276820a4e154SJeff Roberson static void 276920a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 277020a4e154SJeff Roberson { 277120a4e154SJeff Roberson struct uma_zone_count *cnt; 277220a4e154SJeff Roberson 277320a4e154SJeff Roberson cnt = arg; 27743b490537SJeff Roberson /* 27753b490537SJeff Roberson * Some zones are rapidly created with identical names and 27763b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 27773b490537SJeff Roberson * Use one greater than the maximum observed for this name. 27783b490537SJeff Roberson */ 277920a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 27803b490537SJeff Roberson cnt->count = MAX(cnt->count, 27813b490537SJeff Roberson zone->uz_namecnt + 1); 278220a4e154SJeff Roberson } 278320a4e154SJeff Roberson 2784cc7ce83aSJeff Roberson static void 2785cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2786cc7ce83aSJeff Roberson { 2787cc7ce83aSJeff Roberson int i; 2788cc7ce83aSJeff Roberson 2789cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2790cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2791cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2792cc7ce83aSJeff Roberson } 2793cc7ce83aSJeff Roberson } 2794cc7ce83aSJeff Roberson 2795099a0e58SBosko Milekic /* 2796099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2797099a0e58SBosko Milekic * 2798099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2799099a0e58SBosko Milekic * udata Actually uma_zctor_args 28008355f576SJeff Roberson */ 2801b23f72e9SBrian Feldman static int 2802b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 28038355f576SJeff Roberson { 280420a4e154SJeff Roberson struct uma_zone_count cnt; 28058355f576SJeff Roberson struct uma_zctor_args *arg = udata; 2806c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 28078355f576SJeff Roberson uma_zone_t zone = mem; 2808099a0e58SBosko Milekic uma_zone_t z; 2809099a0e58SBosko Milekic uma_keg_t keg; 281008cfa56eSMark Johnston int i; 28118355f576SJeff Roberson 28128355f576SJeff Roberson bzero(zone, size); 28138355f576SJeff Roberson zone->uz_name = arg->name; 28148355f576SJeff Roberson zone->uz_ctor = arg->ctor; 28158355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2816099a0e58SBosko Milekic zone->uz_init = NULL; 2817099a0e58SBosko Milekic zone->uz_fini = NULL; 2818bf965959SSean Bruno zone->uz_sleeps = 0; 281920a4e154SJeff Roberson zone->uz_bucket_size = 0; 282020a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 282120a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2822d4665eaaSJeff Roberson zone->uz_flags = (arg->flags & UMA_ZONE_SMR); 28232f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2824ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 2825c6fd3e23SJeff Roberson zone->uz_bucket_max = ULONG_MAX; 28262f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2827af526374SJeff Roberson 282820a4e154SJeff Roberson /* Count the number of duplicate names. */ 282920a4e154SJeff Roberson cnt.name = arg->name; 283020a4e154SJeff Roberson cnt.count = 0; 283120a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 283220a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 283391d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 28342efcc8cbSGleb Smirnoff 2835c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 2836c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 2837c6fd3e23SJeff Roberson ZDOM_LOCK_INIT(zone, zdom, (arg->flags & UMA_ZONE_MTXCLASS)); 2838c6fd3e23SJeff Roberson STAILQ_INIT(&zdom->uzd_buckets); 2839c6fd3e23SJeff Roberson } 284008cfa56eSMark Johnston 284110094910SMark Johnston #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN) 2842ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2843cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 284409c8cb71SMark Johnston #elif defined(KASAN) 284509c8cb71SMark Johnston if ((arg->flags & (UMA_ZONE_NOFREE | UMA_ZFLAG_CACHE)) != 0) 284609c8cb71SMark Johnston arg->flags |= UMA_ZONE_NOKASAN; 2847ca293436SRyan Libby #endif 2848ca293436SRyan Libby 28490095a784SJeff Roberson /* 28500095a784SJeff Roberson * This is a pure cache zone, no kegs. 28510095a784SJeff Roberson */ 28520095a784SJeff Roberson if (arg->import) { 2853727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2854727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 28556fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2856af526374SJeff Roberson zone->uz_size = arg->size; 28570095a784SJeff Roberson zone->uz_import = arg->import; 28580095a784SJeff Roberson zone->uz_release = arg->release; 28590095a784SJeff Roberson zone->uz_arg = arg->arg; 2860c6fd3e23SJeff Roberson #ifdef NUMA 2861c6fd3e23SJeff Roberson /* 2862c6fd3e23SJeff Roberson * Cache zones are round-robin unless a policy is 2863c6fd3e23SJeff Roberson * specified because they may have incompatible 2864c6fd3e23SJeff Roberson * constraints. 2865c6fd3e23SJeff Roberson */ 2866c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2867c6fd3e23SJeff Roberson zone->uz_flags |= UMA_ZONE_ROUNDROBIN; 2868c6fd3e23SJeff Roberson #endif 2869111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 287003175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2871111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2872af526374SJeff Roberson goto out; 28730095a784SJeff Roberson } 28740095a784SJeff Roberson 28750095a784SJeff Roberson /* 28760095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 28770095a784SJeff Roberson */ 2878b75c4efcSAndrew Turner zone->uz_import = zone_import; 2879b75c4efcSAndrew Turner zone->uz_release = zone_release; 28800095a784SJeff Roberson zone->uz_arg = zone; 2881bb15d1c7SGleb Smirnoff keg = arg->keg; 28820095a784SJeff Roberson 2883099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 288420a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 288520a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2886099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 28878355f576SJeff Roberson zone->uz_init = arg->uminit; 2888e221e841SJeff Roberson zone->uz_fini = arg->fini; 2889e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2890111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2891099a0e58SBosko Milekic ZONE_LOCK(zone); 2892099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2893099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2894099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2895099a0e58SBosko Milekic break; 2896099a0e58SBosko Milekic } 2897099a0e58SBosko Milekic } 2898099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2899111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2900e20a199fSJeff Roberson } else if (keg == NULL) { 2901e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2902e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2903b23f72e9SBrian Feldman return (ENOMEM); 2904099a0e58SBosko Milekic } else { 2905099a0e58SBosko Milekic struct uma_kctor_args karg; 2906b23f72e9SBrian Feldman int error; 2907099a0e58SBosko Milekic 2908099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2909099a0e58SBosko Milekic karg.size = arg->size; 2910099a0e58SBosko Milekic karg.uminit = arg->uminit; 2911099a0e58SBosko Milekic karg.fini = arg->fini; 2912099a0e58SBosko Milekic karg.align = arg->align; 2913d4665eaaSJeff Roberson karg.flags = (arg->flags & ~UMA_ZONE_SMR); 2914099a0e58SBosko Milekic karg.zone = zone; 2915b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2916b23f72e9SBrian Feldman flags); 2917b23f72e9SBrian Feldman if (error) 2918b23f72e9SBrian Feldman return (error); 2919099a0e58SBosko Milekic } 29200095a784SJeff Roberson 292120a4e154SJeff Roberson /* Inherit properties from the keg. */ 2922bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2923e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2924e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2925e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 29268355f576SJeff Roberson 292720a4e154SJeff Roberson out: 2928dc2b3205SMark Johnston if (booted >= BOOT_PCPU) { 292920a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 2930dc2b3205SMark Johnston if (booted >= BOOT_RUNNING) 293120a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 293220a4e154SJeff Roberson } else { 293320a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 293420a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 293520a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2936099a0e58SBosko Milekic } 29378355f576SJeff Roberson 2938d4665eaaSJeff Roberson /* Caller requests a private SMR context. */ 2939d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 2940226dd6dbSJeff Roberson zone->uz_smr = smr_create(zone->uz_name, 0, 0); 2941d4665eaaSJeff Roberson 29427e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 29437e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 29447e28037aSMark Johnston ("Invalid zone flag combination")); 294520a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 294620a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 294720a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 294820a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 294920a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 295020a4e154SJeff Roberson zone->uz_bucket_size = 0; 29517e28037aSMark Johnston else 295220a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 295320a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2954cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2955cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2956cc7ce83aSJeff Roberson zone_update_caches(zone); 2957fc03d22bSJeff Roberson 2958b23f72e9SBrian Feldman return (0); 29598355f576SJeff Roberson } 29608355f576SJeff Roberson 29618355f576SJeff Roberson /* 2962099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2963099a0e58SBosko Milekic * table and removes the keg from the global list. 29649c2cd7e5SJeff Roberson * 29659c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 29669c2cd7e5SJeff Roberson * udata unused 29679c2cd7e5SJeff Roberson */ 2968099a0e58SBosko Milekic static void 2969099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2970099a0e58SBosko Milekic { 2971099a0e58SBosko Milekic uma_keg_t keg; 29728b987a77SJeff Roberson uint32_t free, pages; 29738b987a77SJeff Roberson int i; 29749c2cd7e5SJeff Roberson 2975099a0e58SBosko Milekic keg = (uma_keg_t)arg; 29768b987a77SJeff Roberson free = pages = 0; 29778b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 29784ab3aee8SMark Johnston free += keg->uk_domain[i].ud_free_items; 29798b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 29808b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2981099a0e58SBosko Milekic } 29827e240677SRyan Libby if (pages != 0) 29838b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 29848b987a77SJeff Roberson " Lost %u pages of memory.\n", 29858b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 29867e240677SRyan Libby pages / keg->uk_ppera * keg->uk_ipers - free, pages); 2987099a0e58SBosko Milekic 2988099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2989099a0e58SBosko Milekic } 2990099a0e58SBosko Milekic 2991099a0e58SBosko Milekic /* 2992099a0e58SBosko Milekic * Zone header dtor. 2993099a0e58SBosko Milekic * 2994099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2995099a0e58SBosko Milekic * udata unused 2996099a0e58SBosko Milekic */ 29979c2cd7e5SJeff Roberson static void 29989c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 29999c2cd7e5SJeff Roberson { 30009c2cd7e5SJeff Roberson uma_zone_t zone; 3001099a0e58SBosko Milekic uma_keg_t keg; 3002c6fd3e23SJeff Roberson int i; 30039c2cd7e5SJeff Roberson 30049c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 30059643769aSJeff Roberson 300620a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 300720a4e154SJeff Roberson 3008e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 30099643769aSJeff Roberson cache_drain(zone); 3010099a0e58SBosko Milekic 3011111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 3012099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 3013111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 30147b516613SJonathan T. Looney if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 30157b516613SJonathan T. Looney keg = zone->uz_keg; 30167b516613SJonathan T. Looney keg->uk_reserve = 0; 30177b516613SJonathan T. Looney } 3018aabe13f1SMark Johnston zone_reclaim(zone, UMA_ANYDOMAIN, M_WAITOK, true); 3019c6fd3e23SJeff Roberson 3020e20a199fSJeff Roberson /* 3021323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 3022e20a199fSJeff Roberson */ 3023323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 3024323ad386STycho Nightingale keg = zone->uz_keg; 3025111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 3026099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 3027111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 30280095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 30299c2cd7e5SJeff Roberson } 30302efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 30312efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 30322efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 3033c6fd3e23SJeff Roberson counter_u64_free(zone->uz_xdomain); 303420a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 3035c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) 3036c6fd3e23SJeff Roberson ZDOM_LOCK_FINI(ZDOM_GET(zone, i)); 303791d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 3038099a0e58SBosko Milekic } 3039099a0e58SBosko Milekic 3040a81c400eSJeff Roberson static void 3041a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg) 3042a81c400eSJeff Roberson { 3043a81c400eSJeff Roberson uma_keg_t keg; 3044a81c400eSJeff Roberson uma_zone_t zone; 3045a81c400eSJeff Roberson 3046a81c400eSJeff Roberson LIST_FOREACH(keg, &uma_kegs, uk_link) { 3047a81c400eSJeff Roberson LIST_FOREACH(zone, &keg->uk_zones, uz_link) 3048a81c400eSJeff Roberson zfunc(zone, arg); 3049a81c400eSJeff Roberson } 3050a81c400eSJeff Roberson LIST_FOREACH(zone, &uma_cachezones, uz_link) 3051a81c400eSJeff Roberson zfunc(zone, arg); 3052a81c400eSJeff Roberson } 3053a81c400eSJeff Roberson 30549c2cd7e5SJeff Roberson /* 30558355f576SJeff Roberson * Traverses every zone in the system and calls a callback 30568355f576SJeff Roberson * 30578355f576SJeff Roberson * Arguments: 30588355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 30598355f576SJeff Roberson * as an argument. 30608355f576SJeff Roberson * 30618355f576SJeff Roberson * Returns: 30628355f576SJeff Roberson * Nothing 30638355f576SJeff Roberson */ 30648355f576SJeff Roberson static void 306520a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 30668355f576SJeff Roberson { 30678355f576SJeff Roberson 3068111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 3069a81c400eSJeff Roberson zone_foreach_unlocked(zfunc, arg); 3070111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 30718355f576SJeff Roberson } 30728355f576SJeff Roberson 3073f4bef67cSGleb Smirnoff /* 3074a81c400eSJeff Roberson * Initialize the kernel memory allocator. This is done after pages can be 3075a81c400eSJeff Roberson * allocated but before general KVA is available. 3076f4bef67cSGleb Smirnoff */ 3077a81c400eSJeff Roberson void 3078a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail) 3079f4bef67cSGleb Smirnoff { 3080a81c400eSJeff Roberson struct uma_zctor_args args; 3081a81c400eSJeff Roberson size_t ksize, zsize, size; 3082c8b0a88bSJeff Roberson uma_keg_t primarykeg; 3083a81c400eSJeff Roberson uintptr_t m; 308481302f1dSMark Johnston int domain; 3085a81c400eSJeff Roberson uint8_t pflag; 3086a81c400eSJeff Roberson 3087a81c400eSJeff Roberson bootstart = bootmem = virtual_avail; 3088a81c400eSJeff Roberson 3089a81c400eSJeff Roberson rw_init(&uma_rwlock, "UMA lock"); 3090a81c400eSJeff Roberson sx_init(&uma_reclaim_lock, "umareclaim"); 3091f4bef67cSGleb Smirnoff 3092f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 3093f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 309479c9f942SJeff Roberson ksize = roundup(ksize, UMA_SUPER_ALIGN); 3095f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 3096f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 3097f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 309879c9f942SJeff Roberson zsize = roundup(zsize, UMA_SUPER_ALIGN); 3099f4bef67cSGleb Smirnoff 3100a81c400eSJeff Roberson /* Allocate the zone of zones, zone of kegs, and zone of zones keg. */ 3101a81c400eSJeff Roberson size = (zsize * 2) + ksize; 310281302f1dSMark Johnston for (domain = 0; domain < vm_ndomains; domain++) { 310381302f1dSMark Johnston m = (uintptr_t)startup_alloc(NULL, size, domain, &pflag, 310481302f1dSMark Johnston M_NOWAIT | M_ZERO); 310581302f1dSMark Johnston if (m != 0) 310681302f1dSMark Johnston break; 310781302f1dSMark Johnston } 3108ab3185d1SJeff Roberson zones = (uma_zone_t)m; 310979c9f942SJeff Roberson m += zsize; 3110ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 311179c9f942SJeff Roberson m += zsize; 3112c8b0a88bSJeff Roberson primarykeg = (uma_keg_t)m; 3113ab3185d1SJeff Roberson 3114099a0e58SBosko Milekic /* "manually" create the initial zone */ 31150095a784SJeff Roberson memset(&args, 0, sizeof(args)); 3116099a0e58SBosko Milekic args.name = "UMA Kegs"; 3117ab3185d1SJeff Roberson args.size = ksize; 3118099a0e58SBosko Milekic args.ctor = keg_ctor; 3119099a0e58SBosko Milekic args.dtor = keg_dtor; 31208355f576SJeff Roberson args.uminit = zero_init; 31218355f576SJeff Roberson args.fini = NULL; 3122c8b0a88bSJeff Roberson args.keg = primarykeg; 312379c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 3124b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 3125ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 31268355f576SJeff Roberson 3127099a0e58SBosko Milekic args.name = "UMA Zones"; 3128f4bef67cSGleb Smirnoff args.size = zsize; 3129099a0e58SBosko Milekic args.ctor = zone_ctor; 3130099a0e58SBosko Milekic args.dtor = zone_dtor; 3131099a0e58SBosko Milekic args.uminit = zero_init; 3132099a0e58SBosko Milekic args.fini = NULL; 3133099a0e58SBosko Milekic args.keg = NULL; 313479c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 3135099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 3136ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 3137099a0e58SBosko Milekic 31389b8db4d0SRyan Libby /* Now make zones for slab headers */ 31399b8db4d0SRyan Libby slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE, 31409b8db4d0SRyan Libby NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 31419b8db4d0SRyan Libby slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE, 31421e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 31438355f576SJeff Roberson 31448355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 31458355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 31461e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 31478355f576SJeff Roberson 3148a81c400eSJeff Roberson bucket_init(); 3149d4665eaaSJeff Roberson smr_init(); 31508355f576SJeff Roberson } 31518355f576SJeff Roberson 3152a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 3153a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void); 3154f4bef67cSGleb Smirnoff #endif 3155f4bef67cSGleb Smirnoff 3156a81c400eSJeff Roberson /* 3157a81c400eSJeff Roberson * Advertise the availability of normal kva allocations and switch to 3158a81c400eSJeff Roberson * the default back-end allocator. Marks the KVA we consumed on startup 3159a81c400eSJeff Roberson * as used in the map. 3160a81c400eSJeff Roberson */ 31618355f576SJeff Roberson void 316299571dc3SJeff Roberson uma_startup2(void) 31638355f576SJeff Roberson { 3164f4bef67cSGleb Smirnoff 3165530cc6a2SJeff Roberson if (bootstart != bootmem) { 3166a81c400eSJeff Roberson vm_map_lock(kernel_map); 3167a81c400eSJeff Roberson (void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem, 3168a81c400eSJeff Roberson VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 3169a81c400eSJeff Roberson vm_map_unlock(kernel_map); 3170a81c400eSJeff Roberson } 3171a81c400eSJeff Roberson 3172a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 3173a81c400eSJeff Roberson /* Set up radix zone to use noobj_alloc. */ 3174a81c400eSJeff Roberson vm_radix_reserve_kva(); 3175f7d35785SGleb Smirnoff #endif 3176a81c400eSJeff Roberson 3177a81c400eSJeff Roberson booted = BOOT_KVA; 3178a81c400eSJeff Roberson zone_foreach_unlocked(zone_kva_available, NULL); 3179f4bef67cSGleb Smirnoff bucket_enable(); 31808355f576SJeff Roberson } 31818355f576SJeff Roberson 3182a81c400eSJeff Roberson /* 3183dc2b3205SMark Johnston * Allocate counters as early as possible so that boot-time allocations are 3184dc2b3205SMark Johnston * accounted more precisely. 3185dc2b3205SMark Johnston */ 3186dc2b3205SMark Johnston static void 3187dc2b3205SMark Johnston uma_startup_pcpu(void *arg __unused) 3188dc2b3205SMark Johnston { 3189dc2b3205SMark Johnston 3190dc2b3205SMark Johnston zone_foreach_unlocked(zone_alloc_counters, NULL); 3191dc2b3205SMark Johnston booted = BOOT_PCPU; 3192dc2b3205SMark Johnston } 3193dc2b3205SMark Johnston SYSINIT(uma_startup_pcpu, SI_SUB_COUNTER, SI_ORDER_ANY, uma_startup_pcpu, NULL); 3194dc2b3205SMark Johnston 3195dc2b3205SMark Johnston /* 3196a81c400eSJeff Roberson * Finish our initialization steps. 3197a81c400eSJeff Roberson */ 31988355f576SJeff Roberson static void 3199dc2b3205SMark Johnston uma_startup3(void *arg __unused) 32008355f576SJeff Roberson { 32011431a748SGleb Smirnoff 3202c5deaf04SGleb Smirnoff #ifdef INVARIANTS 3203c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 3204c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 3205c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 3206c5deaf04SGleb Smirnoff #endif 3207a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_sysctl, NULL); 3208fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 32099643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 3210c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 3211860bb7a0SMark Johnston 3212860bb7a0SMark Johnston EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL, 3213860bb7a0SMark Johnston EVENTHANDLER_PRI_FIRST); 3214860bb7a0SMark Johnston } 3215dc2b3205SMark Johnston SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 3216860bb7a0SMark Johnston 3217860bb7a0SMark Johnston static void 3218860bb7a0SMark Johnston uma_shutdown(void) 3219860bb7a0SMark Johnston { 3220860bb7a0SMark Johnston 3221860bb7a0SMark Johnston booted = BOOT_SHUTDOWN; 32228355f576SJeff Roberson } 32238355f576SJeff Roberson 3224e20a199fSJeff Roberson static uma_keg_t 3225099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 322685dcf349SGleb Smirnoff int align, uint32_t flags) 3227099a0e58SBosko Milekic { 3228099a0e58SBosko Milekic struct uma_kctor_args args; 3229099a0e58SBosko Milekic 3230099a0e58SBosko Milekic args.size = size; 3231099a0e58SBosko Milekic args.uminit = uminit; 3232099a0e58SBosko Milekic args.fini = fini; 32331e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 3234099a0e58SBosko Milekic args.flags = flags; 3235099a0e58SBosko Milekic args.zone = zone; 3236ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 3237099a0e58SBosko Milekic } 3238099a0e58SBosko Milekic 3239f4bef67cSGleb Smirnoff /* Public functions */ 32408355f576SJeff Roberson /* See uma.h */ 32411e319f6dSRobert Watson void 32421e319f6dSRobert Watson uma_set_align(int align) 32431e319f6dSRobert Watson { 32441e319f6dSRobert Watson 32451e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 32461e319f6dSRobert Watson uma_align_cache = align; 32471e319f6dSRobert Watson } 32481e319f6dSRobert Watson 32491e319f6dSRobert Watson /* See uma.h */ 32508355f576SJeff Roberson uma_zone_t 3251bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 325285dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 32538355f576SJeff Roberson 32548355f576SJeff Roberson { 32558355f576SJeff Roberson struct uma_zctor_args args; 325695c4bf75SKonstantin Belousov uma_zone_t res; 32578355f576SJeff Roberson 3258a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 3259a5a35578SJohn Baldwin align, name)); 3260a5a35578SJohn Baldwin 32618355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 32620095a784SJeff Roberson memset(&args, 0, sizeof(args)); 32638355f576SJeff Roberson args.name = name; 32648355f576SJeff Roberson args.size = size; 32658355f576SJeff Roberson args.ctor = ctor; 32668355f576SJeff Roberson args.dtor = dtor; 32678355f576SJeff Roberson args.uminit = uminit; 32688355f576SJeff Roberson args.fini = fini; 326910094910SMark Johnston #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN) 3270afc6dc36SJohn-Mark Gurney /* 3271ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 3272ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 3273ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 3274ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 3275ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 3276ca293436SRyan Libby * the ctor and dtor do not need to be empty. 3277afc6dc36SJohn-Mark Gurney */ 327854c5ae80SRyan Libby if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH | 327954c5ae80SRyan Libby UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) { 3280afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 3281afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 3282afc6dc36SJohn-Mark Gurney } 3283afc6dc36SJohn-Mark Gurney #endif 32848355f576SJeff Roberson args.align = align; 32858355f576SJeff Roberson args.flags = flags; 3286099a0e58SBosko Milekic args.keg = NULL; 3287099a0e58SBosko Milekic 3288aabe13f1SMark Johnston sx_xlock(&uma_reclaim_lock); 3289ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 3290aabe13f1SMark Johnston sx_xunlock(&uma_reclaim_lock); 3291a81c400eSJeff Roberson 329295c4bf75SKonstantin Belousov return (res); 3293099a0e58SBosko Milekic } 3294099a0e58SBosko Milekic 3295099a0e58SBosko Milekic /* See uma.h */ 3296099a0e58SBosko Milekic uma_zone_t 32970464f16eSMark Johnston uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor, 3298c8b0a88bSJeff Roberson uma_init zinit, uma_fini zfini, uma_zone_t primary) 3299099a0e58SBosko Milekic { 3300099a0e58SBosko Milekic struct uma_zctor_args args; 3301e20a199fSJeff Roberson uma_keg_t keg; 330295c4bf75SKonstantin Belousov uma_zone_t res; 3303099a0e58SBosko Milekic 3304c8b0a88bSJeff Roberson keg = primary->uz_keg; 33050095a784SJeff Roberson memset(&args, 0, sizeof(args)); 3306099a0e58SBosko Milekic args.name = name; 3307e20a199fSJeff Roberson args.size = keg->uk_size; 3308099a0e58SBosko Milekic args.ctor = ctor; 3309099a0e58SBosko Milekic args.dtor = dtor; 3310099a0e58SBosko Milekic args.uminit = zinit; 3311099a0e58SBosko Milekic args.fini = zfini; 3312e20a199fSJeff Roberson args.align = keg->uk_align; 3313e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 3314e20a199fSJeff Roberson args.keg = keg; 33158355f576SJeff Roberson 3316aabe13f1SMark Johnston sx_xlock(&uma_reclaim_lock); 3317ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 3318aabe13f1SMark Johnston sx_xunlock(&uma_reclaim_lock); 3319a81c400eSJeff Roberson 332095c4bf75SKonstantin Belousov return (res); 33218355f576SJeff Roberson } 33228355f576SJeff Roberson 33230095a784SJeff Roberson /* See uma.h */ 33240095a784SJeff Roberson uma_zone_t 33250464f16eSMark Johnston uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor, 33260464f16eSMark Johnston uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease, 33270464f16eSMark Johnston void *arg, int flags) 33280095a784SJeff Roberson { 33290095a784SJeff Roberson struct uma_zctor_args args; 33300095a784SJeff Roberson 33310095a784SJeff Roberson memset(&args, 0, sizeof(args)); 33320095a784SJeff Roberson args.name = name; 3333af526374SJeff Roberson args.size = size; 33340095a784SJeff Roberson args.ctor = ctor; 33350095a784SJeff Roberson args.dtor = dtor; 33360095a784SJeff Roberson args.uminit = zinit; 33370095a784SJeff Roberson args.fini = zfini; 33380095a784SJeff Roberson args.import = zimport; 33390095a784SJeff Roberson args.release = zrelease; 33400095a784SJeff Roberson args.arg = arg; 33410095a784SJeff Roberson args.align = 0; 3342bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 33430095a784SJeff Roberson 3344ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 33450095a784SJeff Roberson } 33460095a784SJeff Roberson 33478355f576SJeff Roberson /* See uma.h */ 33489c2cd7e5SJeff Roberson void 33499c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 33509c2cd7e5SJeff Roberson { 3351f4ff923bSRobert Watson 3352860bb7a0SMark Johnston /* 3353860bb7a0SMark Johnston * Large slabs are expensive to reclaim, so don't bother doing 3354860bb7a0SMark Johnston * unnecessary work if we're shutting down. 3355860bb7a0SMark Johnston */ 3356860bb7a0SMark Johnston if (booted == BOOT_SHUTDOWN && 3357860bb7a0SMark Johnston zone->uz_fini == NULL && zone->uz_release == zone_release) 3358860bb7a0SMark Johnston return; 3359aabe13f1SMark Johnston sx_xlock(&uma_reclaim_lock); 33600095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 3361aabe13f1SMark Johnston sx_xunlock(&uma_reclaim_lock); 33629c2cd7e5SJeff Roberson } 33639c2cd7e5SJeff Roberson 33648d6fbbb8SJeff Roberson void 33658d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 33668d6fbbb8SJeff Roberson { 33678d6fbbb8SJeff Roberson 336870260874SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 336970260874SJeff Roberson uma_zfree_smr(zone, uma_zalloc_smr(zone, M_WAITOK)); 337070260874SJeff Roberson else if ((zone->uz_flags & UMA_ZONE_PCPU) != 0) 337170260874SJeff Roberson uma_zfree_pcpu(zone, uma_zalloc_pcpu(zone, M_WAITOK)); 337270260874SJeff Roberson else 337370260874SJeff Roberson uma_zfree(zone, uma_zalloc(zone, M_WAITOK)); 33748d6fbbb8SJeff Roberson } 33758d6fbbb8SJeff Roberson 33764e180881SMateusz Guzik void * 33774e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 33784e180881SMateusz Guzik { 33793acb6572SMateusz Guzik void *item, *pcpu_item; 3380b4799947SRuslan Bukin #ifdef SMP 33814e180881SMateusz Guzik int i; 33824e180881SMateusz Guzik 33834e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 3384b4799947SRuslan Bukin #endif 33854e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 33863acb6572SMateusz Guzik if (item == NULL) 33873acb6572SMateusz Guzik return (NULL); 33883acb6572SMateusz Guzik pcpu_item = zpcpu_base_to_offset(item); 33893acb6572SMateusz Guzik if (flags & M_ZERO) { 3390b4799947SRuslan Bukin #ifdef SMP 3391013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 33923acb6572SMateusz Guzik bzero(zpcpu_get_cpu(pcpu_item, i), zone->uz_size); 3393b4799947SRuslan Bukin #else 3394b4799947SRuslan Bukin bzero(item, zone->uz_size); 3395b4799947SRuslan Bukin #endif 33964e180881SMateusz Guzik } 33973acb6572SMateusz Guzik return (pcpu_item); 33984e180881SMateusz Guzik } 33994e180881SMateusz Guzik 34004e180881SMateusz Guzik /* 34014e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 34024e180881SMateusz Guzik */ 34034e180881SMateusz Guzik void 34043acb6572SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *pcpu_item, void *udata) 34054e180881SMateusz Guzik { 34063acb6572SMateusz Guzik void *item; 34074e180881SMateusz Guzik 3408c5b7751fSIan Lepore #ifdef SMP 34094e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 3410c5b7751fSIan Lepore #endif 3411b8f7267dSKristof Provost 3412b8f7267dSKristof Provost /* uma_zfree_pcu_*(..., NULL) does nothing, to match free(9). */ 3413b8f7267dSKristof Provost if (pcpu_item == NULL) 3414b8f7267dSKristof Provost return; 3415b8f7267dSKristof Provost 34163acb6572SMateusz Guzik item = zpcpu_offset_to_base(pcpu_item); 34174e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 34184e180881SMateusz Guzik } 34194e180881SMateusz Guzik 3420d4665eaaSJeff Roberson static inline void * 3421d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags, 3422d4665eaaSJeff Roberson void *item) 3423beb8beefSJeff Roberson { 3424beb8beefSJeff Roberson #ifdef INVARIANTS 3425ca293436SRyan Libby bool skipdbg; 342609c8cb71SMark Johnston #endif 3427beb8beefSJeff Roberson 342809c8cb71SMark Johnston kasan_mark_item_valid(zone, item); 342910094910SMark Johnston kmsan_mark_item_uninitialized(zone, item); 343009c8cb71SMark Johnston 343109c8cb71SMark Johnston #ifdef INVARIANTS 3432beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 343309c8cb71SMark Johnston if (!skipdbg && (uz_flags & UMA_ZFLAG_TRASH) != 0 && 3434ca293436SRyan Libby zone->uz_ctor != trash_ctor) 3435cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 3436beb8beefSJeff Roberson #endif 343709c8cb71SMark Johnston 3438d4665eaaSJeff Roberson /* Check flags before loading ctor pointer. */ 3439d4665eaaSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) && 3440d4665eaaSJeff Roberson __predict_false(zone->uz_ctor != NULL) && 3441cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 3442beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3443beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 3444beb8beefSJeff Roberson return (NULL); 3445beb8beefSJeff Roberson } 3446beb8beefSJeff Roberson #ifdef INVARIANTS 3447beb8beefSJeff Roberson if (!skipdbg) 3448beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 3449beb8beefSJeff Roberson #endif 34506d88d784SJeff Roberson if (__predict_false(flags & M_ZERO)) 34516d88d784SJeff Roberson return (memset(item, 0, size)); 3452beb8beefSJeff Roberson 3453beb8beefSJeff Roberson return (item); 3454beb8beefSJeff Roberson } 3455beb8beefSJeff Roberson 3456ca293436SRyan Libby static inline void 3457cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 3458cc7ce83aSJeff Roberson enum zfreeskip skip) 3459ca293436SRyan Libby { 3460ca293436SRyan Libby #ifdef INVARIANTS 3461ca293436SRyan Libby bool skipdbg; 3462ca293436SRyan Libby 3463ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 3464ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 3465ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 3466ca293436SRyan Libby uma_dbg_free(zone, udata, item); 3467ca293436SRyan Libby else 3468ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 3469ca293436SRyan Libby } 3470ca293436SRyan Libby #endif 3471cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 3472ca293436SRyan Libby if (zone->uz_dtor != NULL) 3473cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 3474ca293436SRyan Libby #ifdef INVARIANTS 3475ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 3476ca293436SRyan Libby zone->uz_dtor != trash_dtor) 3477cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 3478ca293436SRyan Libby #endif 3479ca293436SRyan Libby } 348009c8cb71SMark Johnston kasan_mark_item_invalid(zone, item); 3481ca293436SRyan Libby } 3482ca293436SRyan Libby 34831c58c09fSMateusz Guzik #ifdef NUMA 348481302f1dSMark Johnston static int 348581302f1dSMark Johnston item_domain(void *item) 348681302f1dSMark Johnston { 348781302f1dSMark Johnston int domain; 348881302f1dSMark Johnston 3489431fb8abSMark Johnston domain = vm_phys_domain(vtophys(item)); 349081302f1dSMark Johnston KASSERT(domain >= 0 && domain < vm_ndomains, 349181302f1dSMark Johnston ("%s: unknown domain for item %p", __func__, item)); 349281302f1dSMark Johnston return (domain); 349381302f1dSMark Johnston } 34941c58c09fSMateusz Guzik #endif 349581302f1dSMark Johnston 3496d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS) 3497*a8cbb835SEric van Gyzen #if defined(INVARIANTS) && (defined(DDB) || defined(STACK)) 3498*a8cbb835SEric van Gyzen #include <sys/stack.h> 3499*a8cbb835SEric van Gyzen #endif 3500d4665eaaSJeff Roberson #define UMA_ZALLOC_DEBUG 3501d4665eaaSJeff Roberson static int 3502d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags) 3503d4665eaaSJeff Roberson { 3504d4665eaaSJeff Roberson int error; 3505d4665eaaSJeff Roberson 3506d4665eaaSJeff Roberson error = 0; 3507d4665eaaSJeff Roberson #ifdef WITNESS 3508d4665eaaSJeff Roberson if (flags & M_WAITOK) { 3509d4665eaaSJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3510d4665eaaSJeff Roberson "uma_zalloc_debug: zone \"%s\"", zone->uz_name); 3511d4665eaaSJeff Roberson } 3512d4665eaaSJeff Roberson #endif 3513d4665eaaSJeff Roberson 3514d4665eaaSJeff Roberson #ifdef INVARIANTS 3515d4665eaaSJeff Roberson KASSERT((flags & M_EXEC) == 0, 3516d4665eaaSJeff Roberson ("uma_zalloc_debug: called with M_EXEC")); 3517d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3518d4665eaaSJeff Roberson ("uma_zalloc_debug: called within spinlock or critical section")); 3519d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0, 3520d4665eaaSJeff Roberson ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO")); 3521*a8cbb835SEric van Gyzen 3522*a8cbb835SEric van Gyzen _Static_assert(M_NOWAIT != 0 && M_WAITOK != 0, 3523*a8cbb835SEric van Gyzen "M_NOWAIT and M_WAITOK must be non-zero for this assertion:"); 3524*a8cbb835SEric van Gyzen #if 0 3525*a8cbb835SEric van Gyzen /* 3526*a8cbb835SEric van Gyzen * Give the #elif clause time to find problems, then remove it 3527*a8cbb835SEric van Gyzen * and enable this. (Remove <sys/stack.h> above, too.) 3528*a8cbb835SEric van Gyzen */ 3529*a8cbb835SEric van Gyzen KASSERT((flags & (M_NOWAIT|M_WAITOK)) == M_NOWAIT || 3530*a8cbb835SEric van Gyzen (flags & (M_NOWAIT|M_WAITOK)) == M_WAITOK, 3531*a8cbb835SEric van Gyzen ("uma_zalloc_debug: must pass one of M_NOWAIT or M_WAITOK")); 3532*a8cbb835SEric van Gyzen #elif defined(DDB) || defined(STACK) 3533*a8cbb835SEric van Gyzen if (__predict_false((flags & (M_NOWAIT|M_WAITOK)) != M_NOWAIT && 3534*a8cbb835SEric van Gyzen (flags & (M_NOWAIT|M_WAITOK)) != M_WAITOK)) { 3535*a8cbb835SEric van Gyzen static int stack_count; 3536*a8cbb835SEric van Gyzen struct stack st; 3537*a8cbb835SEric van Gyzen 3538*a8cbb835SEric van Gyzen if (stack_count < 10) { 3539*a8cbb835SEric van Gyzen ++stack_count; 3540*a8cbb835SEric van Gyzen printf("uma_zalloc* called with bad WAIT flags:\n"); 3541*a8cbb835SEric van Gyzen stack_save(&st); 3542*a8cbb835SEric van Gyzen stack_print(&st); 3543*a8cbb835SEric van Gyzen } 3544*a8cbb835SEric van Gyzen } 3545*a8cbb835SEric van Gyzen #endif 3546d4665eaaSJeff Roberson #endif 3547d4665eaaSJeff Roberson 3548d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 35499e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) { 3550d4665eaaSJeff Roberson void *item; 3551d4665eaaSJeff Roberson item = memguard_alloc(zone->uz_size, flags); 3552d4665eaaSJeff Roberson if (item != NULL) { 3553d4665eaaSJeff Roberson error = EJUSTRETURN; 3554d4665eaaSJeff Roberson if (zone->uz_init != NULL && 3555d4665eaaSJeff Roberson zone->uz_init(item, zone->uz_size, flags) != 0) { 3556d4665eaaSJeff Roberson *itemp = NULL; 3557d4665eaaSJeff Roberson return (error); 3558d4665eaaSJeff Roberson } 3559d4665eaaSJeff Roberson if (zone->uz_ctor != NULL && 3560d4665eaaSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 3561d4665eaaSJeff Roberson flags) != 0) { 3562d4665eaaSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3563389a3fa6SMark Johnston if (zone->uz_fini != NULL) 3564d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3565d4665eaaSJeff Roberson *itemp = NULL; 3566d4665eaaSJeff Roberson return (error); 3567d4665eaaSJeff Roberson } 3568d4665eaaSJeff Roberson *itemp = item; 3569d4665eaaSJeff Roberson return (error); 3570d4665eaaSJeff Roberson } 3571d4665eaaSJeff Roberson /* This is unfortunate but should not be fatal. */ 3572d4665eaaSJeff Roberson } 3573d4665eaaSJeff Roberson #endif 3574d4665eaaSJeff Roberson return (error); 3575d4665eaaSJeff Roberson } 3576d4665eaaSJeff Roberson 3577d4665eaaSJeff Roberson static int 3578d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata) 3579d4665eaaSJeff Roberson { 3580d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3581d4665eaaSJeff Roberson ("uma_zfree_debug: called with spinlock or critical section held")); 3582d4665eaaSJeff Roberson 3583d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 35849e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) { 3585d4665eaaSJeff Roberson if (zone->uz_dtor != NULL) 3586d4665eaaSJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 3587d4665eaaSJeff Roberson if (zone->uz_fini != NULL) 3588d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3589d4665eaaSJeff Roberson memguard_free(item); 3590d4665eaaSJeff Roberson return (EJUSTRETURN); 3591d4665eaaSJeff Roberson } 3592d4665eaaSJeff Roberson #endif 3593d4665eaaSJeff Roberson return (0); 3594d4665eaaSJeff Roberson } 3595d4665eaaSJeff Roberson #endif 3596d4665eaaSJeff Roberson 35976d88d784SJeff Roberson static inline void * 35986d88d784SJeff Roberson cache_alloc_item(uma_zone_t zone, uma_cache_t cache, uma_cache_bucket_t bucket, 35996d88d784SJeff Roberson void *udata, int flags) 3600d4665eaaSJeff Roberson { 36016d88d784SJeff Roberson void *item; 36026d88d784SJeff Roberson int size, uz_flags; 36036d88d784SJeff Roberson 36046d88d784SJeff Roberson item = cache_bucket_pop(cache, bucket); 36056d88d784SJeff Roberson size = cache_uz_size(cache); 36066d88d784SJeff Roberson uz_flags = cache_uz_flags(cache); 36076d88d784SJeff Roberson critical_exit(); 36086d88d784SJeff Roberson return (item_ctor(zone, uz_flags, size, udata, flags, item)); 36096d88d784SJeff Roberson } 36106d88d784SJeff Roberson 36116d88d784SJeff Roberson static __noinline void * 36126d88d784SJeff Roberson cache_alloc_retry(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 36136d88d784SJeff Roberson { 36146d88d784SJeff Roberson uma_cache_bucket_t bucket; 3615d4665eaaSJeff Roberson int domain; 3616d4665eaaSJeff Roberson 36176d88d784SJeff Roberson while (cache_alloc(zone, cache, udata, flags)) { 36186d88d784SJeff Roberson cache = &zone->uz_cpu[curcpu]; 36196d88d784SJeff Roberson bucket = &cache->uc_allocbucket; 36206d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 36216d88d784SJeff Roberson continue; 36226d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, udata, flags)); 36236d88d784SJeff Roberson } 36246d88d784SJeff Roberson critical_exit(); 36256d88d784SJeff Roberson 3626d4665eaaSJeff Roberson /* 3627d4665eaaSJeff Roberson * We can not get a bucket so try to return a single item. 3628d4665eaaSJeff Roberson */ 3629d4665eaaSJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 3630d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3631d4665eaaSJeff Roberson else 3632d4665eaaSJeff Roberson domain = UMA_ANYDOMAIN; 3633d4665eaaSJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3634d4665eaaSJeff Roberson } 3635d4665eaaSJeff Roberson 3636d4665eaaSJeff Roberson /* See uma.h */ 3637d4665eaaSJeff Roberson void * 3638d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags) 3639d4665eaaSJeff Roberson { 3640d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 3641d4665eaaSJeff Roberson uma_cache_t cache; 3642d4665eaaSJeff Roberson 3643841e0a87SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc_smr zone %s(%p) flags %d", zone->uz_name, 3644841e0a87SGleb Smirnoff zone, flags); 3645841e0a87SGleb Smirnoff 3646d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 36476d88d784SJeff Roberson void *item; 36486d88d784SJeff Roberson 3649d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 3650952c8964SMark Johnston ("uma_zalloc_arg: called with non-SMR zone.")); 3651d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN) 3652d4665eaaSJeff Roberson return (item); 3653d4665eaaSJeff Roberson #endif 3654d4665eaaSJeff Roberson 3655d4665eaaSJeff Roberson critical_enter(); 3656d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3657d4665eaaSJeff Roberson bucket = &cache->uc_allocbucket; 36586d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 36596d88d784SJeff Roberson return (cache_alloc_retry(zone, cache, NULL, flags)); 36606d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, NULL, flags)); 3661d4665eaaSJeff Roberson } 3662d4665eaaSJeff Roberson 36639c2cd7e5SJeff Roberson /* See uma.h */ 36648355f576SJeff Roberson void * 36652cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 36668355f576SJeff Roberson { 3667376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3668ab3185d1SJeff Roberson uma_cache_t cache; 36698355f576SJeff Roberson 3670e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 367119fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 367210cb2424SMark Murray 36738355f576SJeff Roberson /* This is the fast path allocation */ 3674e63a1c2fSRyan Libby CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name, 3675e63a1c2fSRyan Libby zone, flags); 3676a553d4b8SJeff Roberson 3677d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 36786d88d784SJeff Roberson void *item; 36796d88d784SJeff Roberson 3680d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 3681952c8964SMark Johnston ("uma_zalloc_arg: called with SMR zone.")); 3682d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN) 36838d689e04SGleb Smirnoff return (item); 36848d689e04SGleb Smirnoff #endif 3685d4665eaaSJeff Roberson 36865d1ae027SRobert Watson /* 36875d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 36885d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 36895d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 36905d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 36915d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 36925d1ae027SRobert Watson * preemption and migration. We release the critical section in 36935d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 36945d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 36955d1ae027SRobert Watson * must detect and handle migration if it has occurred. 36965d1ae027SRobert Watson */ 36975d1ae027SRobert Watson critical_enter(); 3698cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3699376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 37006d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 37016d88d784SJeff Roberson return (cache_alloc_retry(zone, cache, udata, flags)); 37026d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, udata, flags)); 3703fc03d22bSJeff Roberson } 3704fc03d22bSJeff Roberson 37058355f576SJeff Roberson /* 3706beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 3707beb8beefSJeff Roberson * a critical section. Returns in a critical section. 3708beb8beefSJeff Roberson * 37094bd61e19SJeff Roberson * A false return value indicates an allocation failure. 37104bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 3711beb8beefSJeff Roberson */ 3712beb8beefSJeff Roberson static __noinline bool 3713beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3714beb8beefSJeff Roberson { 3715beb8beefSJeff Roberson uma_bucket_t bucket; 37168c277118SMark Johnston int curdomain, domain; 3717c6fd3e23SJeff Roberson bool new; 3718beb8beefSJeff Roberson 3719beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3720beb8beefSJeff Roberson 3721beb8beefSJeff Roberson /* 3722beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3723beb8beefSJeff Roberson * if we can switch with the free bucket. 3724d4665eaaSJeff Roberson * 3725d4665eaaSJeff Roberson * SMR Zones can't re-use the free bucket until the sequence has 3726d4665eaaSJeff Roberson * expired. 37278355f576SJeff Roberson */ 3728c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_SMR) == 0 && 3729d4665eaaSJeff Roberson cache->uc_freebucket.ucb_cnt != 0) { 3730d4665eaaSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 3731d4665eaaSJeff Roberson &cache->uc_allocbucket); 3732beb8beefSJeff Roberson return (true); 37338355f576SJeff Roberson } 3734fc03d22bSJeff Roberson 3735fc03d22bSJeff Roberson /* 3736fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3737fc03d22bSJeff Roberson */ 3738376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3739fc03d22bSJeff Roberson critical_exit(); 3740c6fd3e23SJeff Roberson 3741c6fd3e23SJeff Roberson if (bucket != NULL) { 3742c6fd3e23SJeff Roberson KASSERT(bucket->ub_cnt == 0, 3743c6fd3e23SJeff Roberson ("cache_alloc: Entered with non-empty alloc bucket.")); 37446fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3745c6fd3e23SJeff Roberson } 3746fc03d22bSJeff Roberson 37475d1ae027SRobert Watson /* 37485d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 3749c6fd3e23SJeff Roberson * we must go back to the zone. This requires the zdom lock, so we 37505d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 37515d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 37525d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 37535d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 37545d1ae027SRobert Watson * the critical section. 37555d1ae027SRobert Watson */ 3756c1685086SJeff Roberson domain = PCPU_GET(domain); 37578c277118SMark Johnston if ((cache_uz_flags(cache) & UMA_ZONE_ROUNDROBIN) != 0 || 37588c277118SMark Johnston VM_DOMAIN_EMPTY(domain)) 3759c6fd3e23SJeff Roberson domain = zone_domain_highest(zone, domain); 3760c6fd3e23SJeff Roberson bucket = cache_fetch_bucket(zone, cache, domain); 3761af32cefdSMark Johnston if (bucket == NULL && zone->uz_bucket_size != 0 && !bucketdisable) { 3762beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 3763c6fd3e23SJeff Roberson new = true; 3764af32cefdSMark Johnston } else { 3765c6fd3e23SJeff Roberson new = false; 3766af32cefdSMark Johnston } 3767c6fd3e23SJeff Roberson 37681431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 37691431a748SGleb Smirnoff zone->uz_name, zone, bucket); 37704bd61e19SJeff Roberson if (bucket == NULL) { 3771fc03d22bSJeff Roberson critical_enter(); 3772beb8beefSJeff Roberson return (false); 37734bd61e19SJeff Roberson } 37740f9b7bf3SMark Johnston 3775fc03d22bSJeff Roberson /* 3776fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3777fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3778fc03d22bSJeff Roberson * the memory directly. 3779fc03d22bSJeff Roberson */ 37804bd61e19SJeff Roberson critical_enter(); 3781cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3782376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 3783c6fd3e23SJeff Roberson ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) == 0 || 37848c277118SMark Johnston (curdomain = PCPU_GET(domain)) == domain || 37858c277118SMark Johnston VM_DOMAIN_EMPTY(curdomain))) { 3786c6fd3e23SJeff Roberson if (new) 3787c6fd3e23SJeff Roberson atomic_add_long(&ZDOM_GET(zone, domain)->uzd_imax, 3788c6fd3e23SJeff Roberson bucket->ub_cnt); 3789376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3790beb8beefSJeff Roberson return (true); 3791c6fd3e23SJeff Roberson } 3792c6fd3e23SJeff Roberson 3793c6fd3e23SJeff Roberson /* 3794c6fd3e23SJeff Roberson * We lost the race, release this bucket and start over. 3795c6fd3e23SJeff Roberson */ 3796c6fd3e23SJeff Roberson critical_exit(); 37972760658bSAlexander Motin zone_put_bucket(zone, domain, bucket, udata, !new); 3798c6fd3e23SJeff Roberson critical_enter(); 3799c6fd3e23SJeff Roberson 3800beb8beefSJeff Roberson return (true); 3801bbee39c6SJeff Roberson } 3802bbee39c6SJeff Roberson 3803ab3185d1SJeff Roberson void * 3804ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3805bbee39c6SJeff Roberson { 380606d8bdcbSMark Johnston #ifdef NUMA 380706d8bdcbSMark Johnston uma_bucket_t bucket; 380806d8bdcbSMark Johnston uma_zone_domain_t zdom; 380906d8bdcbSMark Johnston void *item; 381006d8bdcbSMark Johnston #endif 3811ab3185d1SJeff Roberson 3812ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 381319fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3814ab3185d1SJeff Roberson 3815ab3185d1SJeff Roberson /* This is the fast path allocation */ 3816e63a1c2fSRyan Libby CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d", 3817e63a1c2fSRyan Libby zone->uz_name, zone, domain, flags); 3818ab3185d1SJeff Roberson 3819ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3820ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3821ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3822ab3185d1SJeff Roberson } 3823ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3824ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 382506d8bdcbSMark Johnston KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 382606d8bdcbSMark Johnston ("uma_zalloc_domain: called with SMR zone.")); 382706d8bdcbSMark Johnston #ifdef NUMA 382806d8bdcbSMark Johnston KASSERT((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0, 382906d8bdcbSMark Johnston ("uma_zalloc_domain: called with non-FIRSTTOUCH zone.")); 3830ab3185d1SJeff Roberson 383106d8bdcbSMark Johnston if (vm_ndomains == 1) 383206d8bdcbSMark Johnston return (uma_zalloc_arg(zone, udata, flags)); 383306d8bdcbSMark Johnston 383406d8bdcbSMark Johnston /* 383506d8bdcbSMark Johnston * Try to allocate from the bucket cache before falling back to the keg. 383606d8bdcbSMark Johnston * We could try harder and attempt to allocate from per-CPU caches or 383706d8bdcbSMark Johnston * the per-domain cross-domain buckets, but the complexity is probably 383806d8bdcbSMark Johnston * not worth it. It is more important that frees of previous 383906d8bdcbSMark Johnston * cross-domain allocations do not blow up the cache. 384006d8bdcbSMark Johnston */ 384106d8bdcbSMark Johnston zdom = zone_domain_lock(zone, domain); 384206d8bdcbSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) { 384306d8bdcbSMark Johnston item = bucket->ub_bucket[bucket->ub_cnt - 1]; 384406d8bdcbSMark Johnston #ifdef INVARIANTS 384506d8bdcbSMark Johnston bucket->ub_bucket[bucket->ub_cnt - 1] = NULL; 384606d8bdcbSMark Johnston #endif 384706d8bdcbSMark Johnston bucket->ub_cnt--; 384806d8bdcbSMark Johnston zone_put_bucket(zone, domain, bucket, udata, true); 384906d8bdcbSMark Johnston item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, 385006d8bdcbSMark Johnston flags, item); 385106d8bdcbSMark Johnston if (item != NULL) { 385206d8bdcbSMark Johnston KASSERT(item_domain(item) == domain, 385306d8bdcbSMark Johnston ("%s: bucket cache item %p from wrong domain", 385406d8bdcbSMark Johnston __func__, item)); 385506d8bdcbSMark Johnston counter_u64_add(zone->uz_allocs, 1); 385606d8bdcbSMark Johnston } 385706d8bdcbSMark Johnston return (item); 385806d8bdcbSMark Johnston } 385906d8bdcbSMark Johnston ZDOM_UNLOCK(zdom); 3860ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 386106d8bdcbSMark Johnston #else 386206d8bdcbSMark Johnston return (uma_zalloc_arg(zone, udata, flags)); 386306d8bdcbSMark Johnston #endif 3864ab3185d1SJeff Roberson } 3865ab3185d1SJeff Roberson 3866ab3185d1SJeff Roberson /* 3867ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3868ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3869ab3185d1SJeff Roberson * 3870ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3871ab3185d1SJeff Roberson * only 'domain'. 3872ab3185d1SJeff Roberson */ 3873ab3185d1SJeff Roberson static uma_slab_t 3874194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3875ab3185d1SJeff Roberson { 3876ab3185d1SJeff Roberson uma_domain_t dom; 3877bbee39c6SJeff Roberson uma_slab_t slab; 3878ab3185d1SJeff Roberson int start; 3879ab3185d1SJeff Roberson 3880ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3881ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 38828b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3883ab3185d1SJeff Roberson 3884ab3185d1SJeff Roberson slab = NULL; 3885ab3185d1SJeff Roberson start = domain; 3886ab3185d1SJeff Roberson do { 3887ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 38884ab3aee8SMark Johnston if ((slab = LIST_FIRST(&dom->ud_part_slab)) != NULL) 38894ab3aee8SMark Johnston return (slab); 38904ab3aee8SMark Johnston if ((slab = LIST_FIRST(&dom->ud_free_slab)) != NULL) { 3891ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 38924ab3aee8SMark Johnston dom->ud_free_slabs--; 3893ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3894ab3185d1SJeff Roberson return (slab); 3895ab3185d1SJeff Roberson } 3896ab3185d1SJeff Roberson if (rr) 3897ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3898ab3185d1SJeff Roberson } while (domain != start); 3899ab3185d1SJeff Roberson 3900ab3185d1SJeff Roberson return (NULL); 3901ab3185d1SJeff Roberson } 3902ab3185d1SJeff Roberson 39038b987a77SJeff Roberson /* 39048b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 39058b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 39068b987a77SJeff Roberson */ 3907ab3185d1SJeff Roberson static uma_slab_t 3908194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3909ab3185d1SJeff Roberson { 39108b987a77SJeff Roberson uma_slab_t slab; 3911194a979eSMark Johnston uint32_t reserve; 3912099a0e58SBosko Milekic 39138b987a77SJeff Roberson /* HASH has a single free list. */ 391454c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 39158b987a77SJeff Roberson domain = 0; 3916194a979eSMark Johnston 39178b987a77SJeff Roberson KEG_LOCK(keg, domain); 3918194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 39194ab3aee8SMark Johnston if (keg->uk_domain[domain].ud_free_items <= reserve || 39208b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 39218b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3922194a979eSMark Johnston return (NULL); 39238b987a77SJeff Roberson } 39248b987a77SJeff Roberson return (slab); 3925194a979eSMark Johnston } 3926194a979eSMark Johnston 3927194a979eSMark Johnston static uma_slab_t 3928194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3929194a979eSMark Johnston { 3930194a979eSMark Johnston struct vm_domainset_iter di; 3931194a979eSMark Johnston uma_slab_t slab; 3932194a979eSMark Johnston int aflags, domain; 3933194a979eSMark Johnston bool rr; 3934194a979eSMark Johnston 3935fab343a7SMark Johnston KASSERT((flags & (M_WAITOK | M_NOVM)) != (M_WAITOK | M_NOVM), 3936fab343a7SMark Johnston ("%s: invalid flags %#x", __func__, flags)); 3937fab343a7SMark Johnston 3938194a979eSMark Johnston restart: 3939bbee39c6SJeff Roberson /* 3940194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3941194a979eSMark Johnston * domain (as happens with first-touch zones). 3942194a979eSMark Johnston * 3943194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3944194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3945194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3946bbee39c6SJeff Roberson */ 3947ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3948ab3185d1SJeff Roberson if (rr) { 3949194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3950194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3951194a979eSMark Johnston &aflags); 3952194a979eSMark Johnston } else { 3953194a979eSMark Johnston aflags = flags; 3954194a979eSMark Johnston domain = rdomain; 3955194a979eSMark Johnston } 3956ab3185d1SJeff Roberson 3957194a979eSMark Johnston for (;;) { 3958194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3959584061b4SJeff Roberson if (slab != NULL) 3960bbee39c6SJeff Roberson return (slab); 3961bbee39c6SJeff Roberson 3962bbee39c6SJeff Roberson /* 3963fab343a7SMark Johnston * M_NOVM is used to break the recursion that can otherwise 3964fab343a7SMark Johnston * occur if low-level memory management routines use UMA. 3965bbee39c6SJeff Roberson */ 3966fab343a7SMark Johnston if ((flags & M_NOVM) == 0) { 396786220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 39688b987a77SJeff Roberson if (slab != NULL) 3969bbee39c6SJeff Roberson return (slab); 3970fab343a7SMark Johnston } 3971fab343a7SMark Johnston 3972fab343a7SMark Johnston if (!rr) { 3973fab343a7SMark Johnston if ((flags & M_USE_RESERVE) != 0) { 3974fab343a7SMark Johnston /* 3975fab343a7SMark Johnston * Drain reserves from other domains before 3976fab343a7SMark Johnston * giving up or sleeping. It may be useful to 3977fab343a7SMark Johnston * support per-domain reserves eventually. 3978fab343a7SMark Johnston */ 3979fab343a7SMark Johnston rdomain = UMA_ANYDOMAIN; 3980fab343a7SMark Johnston goto restart; 3981fab343a7SMark Johnston } 3982fab343a7SMark Johnston if ((flags & M_WAITOK) == 0) 39833639ac42SJeff Roberson break; 3984fab343a7SMark Johnston vm_wait_domain(domain); 3985fab343a7SMark Johnston } else if (vm_domainset_iter_policy(&di, &domain) != 0) { 3986194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 398789d2fb14SKonstantin Belousov vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0); 3988194a979eSMark Johnston goto restart; 398930c5525bSAndrew Gallatin } 3990194a979eSMark Johnston break; 3991194a979eSMark Johnston } 3992ab3185d1SJeff Roberson } 3993ab3185d1SJeff Roberson 3994bbee39c6SJeff Roberson /* 3995bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3996bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3997bbee39c6SJeff Roberson * fail. 3998bbee39c6SJeff Roberson */ 39998b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 4000bbee39c6SJeff Roberson return (slab); 40018b987a77SJeff Roberson 4002ab3185d1SJeff Roberson return (NULL); 4003ab3185d1SJeff Roberson } 4004bbee39c6SJeff Roberson 4005d56368d7SBosko Milekic static void * 40060095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 4007bbee39c6SJeff Roberson { 4008ab3185d1SJeff Roberson uma_domain_t dom; 4009bbee39c6SJeff Roberson void *item; 40109b8db4d0SRyan Libby int freei; 4011bbee39c6SJeff Roberson 40128b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 4013099a0e58SBosko Milekic 40148b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 40159b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 40169b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 40171e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 4018bbee39c6SJeff Roberson slab->us_freecount--; 40194ab3aee8SMark Johnston dom->ud_free_items--; 4020ef72505eSJeff Roberson 40214ab3aee8SMark Johnston /* 40224ab3aee8SMark Johnston * Move this slab to the full list. It must be on the partial list, so 40234ab3aee8SMark Johnston * we do not need to update the free slab count. In particular, 40244ab3aee8SMark Johnston * keg_fetch_slab() always returns slabs on the partial list. 40254ab3aee8SMark Johnston */ 4026bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 4027bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 4028ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 4029bbee39c6SJeff Roberson } 4030bbee39c6SJeff Roberson 4031bbee39c6SJeff Roberson return (item); 4032bbee39c6SJeff Roberson } 4033bbee39c6SJeff Roberson 4034bbee39c6SJeff Roberson static int 4035b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 40360095a784SJeff Roberson { 40378b987a77SJeff Roberson uma_domain_t dom; 4038b75c4efcSAndrew Turner uma_zone_t zone; 40390095a784SJeff Roberson uma_slab_t slab; 40400095a784SJeff Roberson uma_keg_t keg; 4041a03af342SSean Bruno #ifdef NUMA 4042ab3185d1SJeff Roberson int stripe; 4043a03af342SSean Bruno #endif 40440095a784SJeff Roberson int i; 40450095a784SJeff Roberson 4046b75c4efcSAndrew Turner zone = arg; 40470095a784SJeff Roberson slab = NULL; 4048584061b4SJeff Roberson keg = zone->uz_keg; 4049af526374SJeff Roberson /* Try to keep the buckets totally full */ 40500095a784SJeff Roberson for (i = 0; i < max; ) { 4051584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 40520095a784SJeff Roberson break; 4053a03af342SSean Bruno #ifdef NUMA 4054ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 4055a03af342SSean Bruno #endif 40568b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 40571b2dcc8cSMark Johnston do { 40580095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 40597585c5dbSMark Johnston if (keg->uk_reserve > 0 && 40607585c5dbSMark Johnston dom->ud_free_items <= keg->uk_reserve) { 40611b2dcc8cSMark Johnston /* 40621b2dcc8cSMark Johnston * Avoid depleting the reserve after a 40631b2dcc8cSMark Johnston * successful item allocation, even if 40641b2dcc8cSMark Johnston * M_USE_RESERVE is specified. 40651b2dcc8cSMark Johnston */ 40661b2dcc8cSMark Johnston KEG_UNLOCK(keg, slab->us_domain); 40671b2dcc8cSMark Johnston goto out; 40681b2dcc8cSMark Johnston } 4069b6715dabSJeff Roberson #ifdef NUMA 4070ab3185d1SJeff Roberson /* 4071ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 4072ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 4073ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 4074ab3185d1SJeff Roberson * than stripe within each bucket. The current option 4075ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 4076ab3185d1SJeff Roberson * time but yields better distribution. 4077ab3185d1SJeff Roberson */ 4078dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 && 4079ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 4080ab3185d1SJeff Roberson break; 4081ab3185d1SJeff Roberson #endif 40821b2dcc8cSMark Johnston } while (slab->us_freecount != 0 && i < max); 40838b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 40841b2dcc8cSMark Johnston 4085ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 40860095a784SJeff Roberson flags &= ~M_WAITOK; 40870095a784SJeff Roberson flags |= M_NOWAIT; 40880095a784SJeff Roberson } 40891b2dcc8cSMark Johnston out: 40900095a784SJeff Roberson return i; 40910095a784SJeff Roberson } 40920095a784SJeff Roberson 40934bd61e19SJeff Roberson static int 40944bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 40954bd61e19SJeff Roberson { 40964bd61e19SJeff Roberson uint64_t old, new, total, max; 40974bd61e19SJeff Roberson 40984bd61e19SJeff Roberson /* 40994bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 41004bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 41014bd61e19SJeff Roberson * fairness by keeping fifo order. 41024bd61e19SJeff Roberson * 41034bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 41044bd61e19SJeff Roberson */ 41054bd61e19SJeff Roberson for (;;) { 41064bd61e19SJeff Roberson zone_free_limit(zone, count); 41074bd61e19SJeff Roberson zone_log_warning(zone); 41084bd61e19SJeff Roberson zone_maxaction(zone); 41094bd61e19SJeff Roberson if (flags & M_NOWAIT) 41104bd61e19SJeff Roberson return (0); 41114bd61e19SJeff Roberson 41124bd61e19SJeff Roberson /* 41134bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 41144bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 41154bd61e19SJeff Roberson * is essentially a home rolled semaphore. 41164bd61e19SJeff Roberson */ 41174bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 41184bd61e19SJeff Roberson old = zone->uz_items; 41194bd61e19SJeff Roberson do { 41204bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 41214bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 41224bd61e19SJeff Roberson max = zone->uz_max_items; 41234bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 41244bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 41254bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 41264bd61e19SJeff Roberson else 41274bd61e19SJeff Roberson new = old + MIN(count, max - old); 41284bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 41294bd61e19SJeff Roberson 41304bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 41314bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 41324bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 41334bd61e19SJeff Roberson return (new - old); 41344bd61e19SJeff Roberson } 41354bd61e19SJeff Roberson 41364bd61e19SJeff Roberson /* 41374bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 41384bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 41394bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 41404bd61e19SJeff Roberson * transitions. 41414bd61e19SJeff Roberson */ 41424bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 41434bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 41444bd61e19SJeff Roberson 41454bd61e19SJeff Roberson /* 41464bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 41474bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 41484bd61e19SJeff Roberson */ 41494bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 41504bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 41514bd61e19SJeff Roberson 41524bd61e19SJeff Roberson /* 41534bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 41544bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 41554bd61e19SJeff Roberson * 41564bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 41574bd61e19SJeff Roberson * our count. 41584bd61e19SJeff Roberson */ 41594bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 41604bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 41614bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 41624bd61e19SJeff Roberson /* We're no longer a sleeper. */ 41634bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 41644bd61e19SJeff Roberson 41654bd61e19SJeff Roberson /* 41664bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 41674bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 41684bd61e19SJeff Roberson * against changes via sysctl. 41694bd61e19SJeff Roberson */ 41704bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 41714bd61e19SJeff Roberson max = zone->uz_max_items; 41724bd61e19SJeff Roberson if (total >= max) 41734bd61e19SJeff Roberson continue; 41744bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 41754bd61e19SJeff Roberson if (total + count > max) { 41764bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 41774bd61e19SJeff Roberson count = max - total; 41784bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 41794bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 41804bd61e19SJeff Roberson 41814bd61e19SJeff Roberson return (count); 41824bd61e19SJeff Roberson } 41834bd61e19SJeff Roberson } 41844bd61e19SJeff Roberson 41854bd61e19SJeff Roberson /* 41864bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 41874bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 41884bd61e19SJeff Roberson * one item can be allocated. 41894bd61e19SJeff Roberson */ 41904bd61e19SJeff Roberson static int 41914bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 41924bd61e19SJeff Roberson { 41934bd61e19SJeff Roberson uint64_t old; 41944bd61e19SJeff Roberson uint64_t max; 41954bd61e19SJeff Roberson 41964bd61e19SJeff Roberson max = zone->uz_max_items; 41974bd61e19SJeff Roberson MPASS(max > 0); 41984bd61e19SJeff Roberson 41994bd61e19SJeff Roberson /* 42004bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 42014bd61e19SJeff Roberson * fetchadd. 42024bd61e19SJeff Roberson */ 42034bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 42044bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 42054bd61e19SJeff Roberson return (count); 42064bd61e19SJeff Roberson 42074bd61e19SJeff Roberson /* 42084bd61e19SJeff Roberson * If we had some items and no sleepers just return the 42094bd61e19SJeff Roberson * truncated value. We have to release the excess space 42104bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 42114bd61e19SJeff Roberson * because we were temporarily over the limit. 42124bd61e19SJeff Roberson */ 42134bd61e19SJeff Roberson if (old < max) { 42144bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 42154bd61e19SJeff Roberson return (max - old); 42164bd61e19SJeff Roberson } 42174bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 42184bd61e19SJeff Roberson } 42194bd61e19SJeff Roberson 42204bd61e19SJeff Roberson /* 42214bd61e19SJeff Roberson * Free a number of items back to the limit. 42224bd61e19SJeff Roberson */ 42234bd61e19SJeff Roberson static void 42244bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 42254bd61e19SJeff Roberson { 42264bd61e19SJeff Roberson uint64_t old; 42274bd61e19SJeff Roberson 42284bd61e19SJeff Roberson MPASS(count > 0); 42294bd61e19SJeff Roberson 42304bd61e19SJeff Roberson /* 42314bd61e19SJeff Roberson * In the common case we either have no sleepers or 42324bd61e19SJeff Roberson * are still over the limit and can just return. 42334bd61e19SJeff Roberson */ 42344bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 42354bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 42364bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 42374bd61e19SJeff Roberson return; 42384bd61e19SJeff Roberson 42394bd61e19SJeff Roberson /* 42404bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 42414bd61e19SJeff Roberson * to generate wakeups if necessary. 42424bd61e19SJeff Roberson */ 42434bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 42444bd61e19SJeff Roberson } 42454bd61e19SJeff Roberson 4246fc03d22bSJeff Roberson static uma_bucket_t 4247beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 4248bbee39c6SJeff Roberson { 4249bbee39c6SJeff Roberson uma_bucket_t bucket; 425009c8cb71SMark Johnston int error, maxbucket, cnt; 4251bbee39c6SJeff Roberson 4252e63a1c2fSRyan Libby CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name, 4253e63a1c2fSRyan Libby zone, domain); 425430c5525bSAndrew Gallatin 4255c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 4256c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 4257c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 42588c277118SMark Johnston else if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0) 4259c6fd3e23SJeff Roberson domain = UMA_ANYDOMAIN; 4260c1685086SJeff Roberson 42614bd61e19SJeff Roberson if (zone->uz_max_items > 0) 42624bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 42634bd61e19SJeff Roberson M_NOWAIT); 42644bd61e19SJeff Roberson else 426520a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 42664bd61e19SJeff Roberson if (maxbucket == 0) 42674bd61e19SJeff Roberson return (false); 4268beb8beefSJeff Roberson 42696fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 42706fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 4271beb8beefSJeff Roberson if (bucket == NULL) { 4272beb8beefSJeff Roberson cnt = 0; 4273beb8beefSJeff Roberson goto out; 4274beb8beefSJeff Roberson } 42750095a784SJeff Roberson 42760095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 4277beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 42780095a784SJeff Roberson 42790095a784SJeff Roberson /* 42800095a784SJeff Roberson * Initialize the memory if necessary. 42810095a784SJeff Roberson */ 42820095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 4283099a0e58SBosko Milekic int i; 4284bbee39c6SJeff Roberson 428509c8cb71SMark Johnston for (i = 0; i < bucket->ub_cnt; i++) { 428609c8cb71SMark Johnston kasan_mark_item_valid(zone, bucket->ub_bucket[i]); 428709c8cb71SMark Johnston error = zone->uz_init(bucket->ub_bucket[i], 428809c8cb71SMark Johnston zone->uz_size, flags); 428909c8cb71SMark Johnston kasan_mark_item_invalid(zone, bucket->ub_bucket[i]); 429009c8cb71SMark Johnston if (error != 0) 4291b23f72e9SBrian Feldman break; 429209c8cb71SMark Johnston } 429309c8cb71SMark Johnston 4294b23f72e9SBrian Feldman /* 4295b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 4296b23f72e9SBrian Feldman * rest back onto the freelist. 4297b23f72e9SBrian Feldman */ 4298b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 4299af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 43000095a784SJeff Roberson bucket->ub_cnt - i); 4301a5a262c6SBosko Milekic #ifdef INVARIANTS 43020095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 43030095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 4304a5a262c6SBosko Milekic #endif 4305b23f72e9SBrian Feldman bucket->ub_cnt = i; 4306b23f72e9SBrian Feldman } 4307099a0e58SBosko Milekic } 4308099a0e58SBosko Milekic 4309beb8beefSJeff Roberson cnt = bucket->ub_cnt; 4310f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 43116fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 43122efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 4313beb8beefSJeff Roberson bucket = NULL; 4314beb8beefSJeff Roberson } 4315beb8beefSJeff Roberson out: 43164bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 43174bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 4318fc03d22bSJeff Roberson 4319fc03d22bSJeff Roberson return (bucket); 4320fc03d22bSJeff Roberson } 4321fc03d22bSJeff Roberson 43228355f576SJeff Roberson /* 43230095a784SJeff Roberson * Allocates a single item from a zone. 43248355f576SJeff Roberson * 43258355f576SJeff Roberson * Arguments 43268355f576SJeff Roberson * zone The zone to alloc for. 43278355f576SJeff Roberson * udata The data to be passed to the constructor. 4328ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 4329a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 43308355f576SJeff Roberson * 43318355f576SJeff Roberson * Returns 43328355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 4333bbee39c6SJeff Roberson * An item if successful 43348355f576SJeff Roberson */ 43358355f576SJeff Roberson 43368355f576SJeff Roberson static void * 4337ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 43388355f576SJeff Roberson { 43398355f576SJeff Roberson void *item; 43408355f576SJeff Roberson 4341791dda87SAndrew Gallatin if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) { 4342791dda87SAndrew Gallatin counter_u64_add(zone->uz_fails, 1); 4343bb15d1c7SGleb Smirnoff return (NULL); 4344791dda87SAndrew Gallatin } 43458355f576SJeff Roberson 4346c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 4347c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 434830c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 4349c1685086SJeff Roberson 4350ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 4351beb8beefSJeff Roberson goto fail_cnt; 43528355f576SJeff Roberson 4353099a0e58SBosko Milekic /* 4354099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 4355099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 4356099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 4357099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 4358099a0e58SBosko Milekic */ 4359b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 436009c8cb71SMark Johnston int error; 436109c8cb71SMark Johnston 436209c8cb71SMark Johnston kasan_mark_item_valid(zone, item); 436309c8cb71SMark Johnston error = zone->uz_init(item, zone->uz_size, flags); 436409c8cb71SMark Johnston kasan_mark_item_invalid(zone, item); 436509c8cb71SMark Johnston if (error != 0) { 4366bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 4367beb8beefSJeff Roberson goto fail_cnt; 4368beb8beefSJeff Roberson } 4369beb8beefSJeff Roberson } 4370d4665eaaSJeff Roberson item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags, 4371d4665eaaSJeff Roberson item); 4372beb8beefSJeff Roberson if (item == NULL) 43730095a784SJeff Roberson goto fail; 43748355f576SJeff Roberson 43752efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 43761431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 43771431a748SGleb Smirnoff zone->uz_name, zone); 43781431a748SGleb Smirnoff 43798355f576SJeff Roberson return (item); 43800095a784SJeff Roberson 4381beb8beefSJeff Roberson fail_cnt: 4382beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 43830095a784SJeff Roberson fail: 43844bd61e19SJeff Roberson if (zone->uz_max_items > 0) 43854bd61e19SJeff Roberson zone_free_limit(zone, 1); 43861431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 43871431a748SGleb Smirnoff zone->uz_name, zone); 43884bd61e19SJeff Roberson 43890095a784SJeff Roberson return (NULL); 43908355f576SJeff Roberson } 43918355f576SJeff Roberson 43928355f576SJeff Roberson /* See uma.h */ 43938355f576SJeff Roberson void 4394d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item) 4395d4665eaaSJeff Roberson { 4396d4665eaaSJeff Roberson uma_cache_t cache; 4397d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 4398c6fd3e23SJeff Roberson int itemdomain, uz_flags; 4399d4665eaaSJeff Roberson 4400841e0a87SGleb Smirnoff CTR3(KTR_UMA, "uma_zfree_smr zone %s(%p) item %p", 4401841e0a87SGleb Smirnoff zone->uz_name, zone, item); 4402841e0a87SGleb Smirnoff 4403d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 4404d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 4405952c8964SMark Johnston ("uma_zfree_smr: called with non-SMR zone.")); 4406d4665eaaSJeff Roberson KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer.")); 4407c6fd3e23SJeff Roberson SMR_ASSERT_NOT_ENTERED(zone->uz_smr); 4408d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN) 4409d4665eaaSJeff Roberson return; 4410d4665eaaSJeff Roberson #endif 4411d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4412d4665eaaSJeff Roberson uz_flags = cache_uz_flags(cache); 4413c6fd3e23SJeff Roberson itemdomain = 0; 4414d4665eaaSJeff Roberson #ifdef NUMA 4415d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 441681302f1dSMark Johnston itemdomain = item_domain(item); 4417d4665eaaSJeff Roberson #endif 4418d4665eaaSJeff Roberson critical_enter(); 4419d4665eaaSJeff Roberson do { 4420d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4421d4665eaaSJeff Roberson /* SMR Zones must free to the free bucket. */ 4422d4665eaaSJeff Roberson bucket = &cache->uc_freebucket; 4423d4665eaaSJeff Roberson #ifdef NUMA 4424d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4425c6fd3e23SJeff Roberson PCPU_GET(domain) != itemdomain) { 4426d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 4427d4665eaaSJeff Roberson } 4428d4665eaaSJeff Roberson #endif 4429d4665eaaSJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 4430d4665eaaSJeff Roberson cache_bucket_push(cache, bucket, item); 4431d4665eaaSJeff Roberson critical_exit(); 4432d4665eaaSJeff Roberson return; 4433d4665eaaSJeff Roberson } 44342cb67bd7SGleb Smirnoff } while (cache_free(zone, cache, NULL, itemdomain)); 4435d4665eaaSJeff Roberson critical_exit(); 4436d4665eaaSJeff Roberson 4437d4665eaaSJeff Roberson /* 4438d4665eaaSJeff Roberson * If nothing else caught this, we'll just do an internal free. 4439d4665eaaSJeff Roberson */ 4440d4665eaaSJeff Roberson zone_free_item(zone, item, NULL, SKIP_NONE); 4441d4665eaaSJeff Roberson } 4442d4665eaaSJeff Roberson 4443d4665eaaSJeff Roberson /* See uma.h */ 4444d4665eaaSJeff Roberson void 44458355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 44468355f576SJeff Roberson { 44478355f576SJeff Roberson uma_cache_t cache; 4448376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 4449c6fd3e23SJeff Roberson int itemdomain, uz_flags; 44508355f576SJeff Roberson 4451e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 445219fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 445310cb2424SMark Murray 445428782f73SGleb Smirnoff CTR3(KTR_UMA, "uma_zfree_arg zone %s(%p) item %p", 445528782f73SGleb Smirnoff zone->uz_name, zone, item); 44563659f747SRobert Watson 4457d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 4458d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 4459952c8964SMark Johnston ("uma_zfree_arg: called with SMR zone.")); 4460d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN) 4461d4665eaaSJeff Roberson return; 4462d4665eaaSJeff Roberson #endif 446320ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 446420ed0cb0SMatthew D Fleming if (item == NULL) 446520ed0cb0SMatthew D Fleming return; 4466cc7ce83aSJeff Roberson 4467cc7ce83aSJeff Roberson /* 4468cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 4469cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 4470cc7ce83aSJeff Roberson * will simply read another cpu's line. 4471cc7ce83aSJeff Roberson */ 4472cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4473cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 4474d4665eaaSJeff Roberson if (UMA_ALWAYS_CTORDTOR || 4475d4665eaaSJeff Roberson __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0)) 4476cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 4477ef72505eSJeff Roberson 4478af7f9b97SJeff Roberson /* 4479af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 4480af7f9b97SJeff Roberson * a little longer for the limits to be reset. 4481af7f9b97SJeff Roberson */ 4482cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 44838a6776caSMark Johnston if (atomic_load_32(&zone->uz_sleepers) > 0) 4484fc03d22bSJeff Roberson goto zfree_item; 4485cc7ce83aSJeff Roberson } 4486af7f9b97SJeff Roberson 44875d1ae027SRobert Watson /* 44885d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 44895d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 44905d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 44915d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 44925d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 44935d1ae027SRobert Watson * preemption and migration. We release the critical section in 44945d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 44955d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 44965d1ae027SRobert Watson * detect and handle migration if it has occurred. 44975d1ae027SRobert Watson */ 4498c6fd3e23SJeff Roberson itemdomain = 0; 4499dfe13344SJeff Roberson #ifdef NUMA 4500dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 450181302f1dSMark Johnston itemdomain = item_domain(item); 4502dfe13344SJeff Roberson #endif 45035d1ae027SRobert Watson critical_enter(); 45040a81b439SJeff Roberson do { 4505cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4506a553d4b8SJeff Roberson /* 4507dfe13344SJeff Roberson * Try to free into the allocbucket first to give LIFO 4508dfe13344SJeff Roberson * ordering for cache-hot datastructures. Spill over 4509dfe13344SJeff Roberson * into the freebucket if necessary. Alloc will swap 4510dfe13344SJeff Roberson * them if one runs dry. 4511a553d4b8SJeff Roberson */ 4512dfe13344SJeff Roberson bucket = &cache->uc_allocbucket; 4513d4665eaaSJeff Roberson #ifdef NUMA 4514d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4515c6fd3e23SJeff Roberson PCPU_GET(domain) != itemdomain) { 4516d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 4517d4665eaaSJeff Roberson } else 4518d4665eaaSJeff Roberson #endif 4519fe835cbfSJeff Roberson if (bucket->ucb_cnt == bucket->ucb_entries && 4520fe835cbfSJeff Roberson cache->uc_freebucket.ucb_cnt < 4521fe835cbfSJeff Roberson cache->uc_freebucket.ucb_entries) 4522fe835cbfSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 4523fe835cbfSJeff Roberson &cache->uc_allocbucket); 4524376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 4525376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 45265d1ae027SRobert Watson critical_exit(); 45278355f576SJeff Roberson return; 4528fc03d22bSJeff Roberson } 45292cb67bd7SGleb Smirnoff } while (cache_free(zone, cache, udata, itemdomain)); 45300a81b439SJeff Roberson critical_exit(); 4531fc03d22bSJeff Roberson 45328355f576SJeff Roberson /* 45330a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 45348355f576SJeff Roberson */ 45350a81b439SJeff Roberson zfree_item: 45360a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 45370a81b439SJeff Roberson } 4538fc03d22bSJeff Roberson 4539dfe13344SJeff Roberson #ifdef NUMA 454091d947bfSJeff Roberson /* 454191d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 454291d947bfSJeff Roberson * them. 454391d947bfSJeff Roberson */ 454491d947bfSJeff Roberson static void 454591d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 454691d947bfSJeff Roberson { 4547991f23efSMark Johnston struct uma_bucketlist emptybuckets, fullbuckets; 454891d947bfSJeff Roberson uma_zone_domain_t zdom; 454991d947bfSJeff Roberson uma_bucket_t b; 4550543117beSJeff Roberson smr_seq_t seq; 455191d947bfSJeff Roberson void *item; 455291d947bfSJeff Roberson int domain; 455391d947bfSJeff Roberson 455491d947bfSJeff Roberson CTR3(KTR_UMA, 455591d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 455691d947bfSJeff Roberson zone->uz_name, zone, bucket); 455791d947bfSJeff Roberson 4558543117beSJeff Roberson /* 4559543117beSJeff Roberson * It is possible for buckets to arrive here out of order so we fetch 4560543117beSJeff Roberson * the current smr seq rather than accepting the bucket's. 4561543117beSJeff Roberson */ 4562543117beSJeff Roberson seq = SMR_SEQ_INVALID; 4563543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 4564226dd6dbSJeff Roberson seq = smr_advance(zone->uz_smr); 4565226dd6dbSJeff Roberson 4566226dd6dbSJeff Roberson /* 4567226dd6dbSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 4568226dd6dbSJeff Roberson * lock on the current crossfree bucket. A full matrix with 4569226dd6dbSJeff Roberson * per-domain locking could be used if necessary. 4570226dd6dbSJeff Roberson */ 4571991f23efSMark Johnston STAILQ_INIT(&emptybuckets); 4572226dd6dbSJeff Roberson STAILQ_INIT(&fullbuckets); 4573226dd6dbSJeff Roberson ZONE_CROSS_LOCK(zone); 4574991f23efSMark Johnston for (; bucket->ub_cnt > 0; bucket->ub_cnt--) { 457591d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 457681302f1dSMark Johnston domain = item_domain(item); 4577c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 457891d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 4579991f23efSMark Johnston if ((b = STAILQ_FIRST(&emptybuckets)) != NULL) { 4580991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4581991f23efSMark Johnston zdom->uzd_cross = b; 4582991f23efSMark Johnston } else { 4583991f23efSMark Johnston /* 4584991f23efSMark Johnston * Avoid allocating a bucket with the cross lock 4585991f23efSMark Johnston * held, since allocation can trigger a 4586991f23efSMark Johnston * cross-domain free and bucket zones may 4587991f23efSMark Johnston * allocate from each other. 4588991f23efSMark Johnston */ 4589991f23efSMark Johnston ZONE_CROSS_UNLOCK(zone); 4590991f23efSMark Johnston b = bucket_alloc(zone, udata, M_NOWAIT); 4591991f23efSMark Johnston if (b == NULL) 4592991f23efSMark Johnston goto out; 4593991f23efSMark Johnston ZONE_CROSS_LOCK(zone); 4594991f23efSMark Johnston if (zdom->uzd_cross != NULL) { 4595991f23efSMark Johnston STAILQ_INSERT_HEAD(&emptybuckets, b, 4596991f23efSMark Johnston ub_link); 4597991f23efSMark Johnston } else { 4598991f23efSMark Johnston zdom->uzd_cross = b; 4599991f23efSMark Johnston } 4600991f23efSMark Johnston } 460191d947bfSJeff Roberson } 4602543117beSJeff Roberson b = zdom->uzd_cross; 4603543117beSJeff Roberson b->ub_bucket[b->ub_cnt++] = item; 4604543117beSJeff Roberson b->ub_seq = seq; 4605543117beSJeff Roberson if (b->ub_cnt == b->ub_entries) { 4606543117beSJeff Roberson STAILQ_INSERT_HEAD(&fullbuckets, b, ub_link); 4607991f23efSMark Johnston if ((b = STAILQ_FIRST(&emptybuckets)) != NULL) 4608991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4609991f23efSMark Johnston zdom->uzd_cross = b; 461091d947bfSJeff Roberson } 461191d947bfSJeff Roberson } 461291d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 4613991f23efSMark Johnston out: 4614c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 4615d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 461691d947bfSJeff Roberson bucket_free(zone, bucket, udata); 4617c6fd3e23SJeff Roberson 4618991f23efSMark Johnston while ((b = STAILQ_FIRST(&emptybuckets)) != NULL) { 4619991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4620991f23efSMark Johnston bucket_free(zone, b, udata); 4621991f23efSMark Johnston } 4622c6fd3e23SJeff Roberson while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) { 4623c6fd3e23SJeff Roberson STAILQ_REMOVE_HEAD(&fullbuckets, ub_link); 462481302f1dSMark Johnston domain = item_domain(b->ub_bucket[0]); 4625c6fd3e23SJeff Roberson zone_put_bucket(zone, domain, b, udata, true); 4626c6fd3e23SJeff Roberson } 462791d947bfSJeff Roberson } 462891d947bfSJeff Roberson #endif 462991d947bfSJeff Roberson 46300a81b439SJeff Roberson static void 46310a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 4632c6fd3e23SJeff Roberson int itemdomain, bool ws) 46330a81b439SJeff Roberson { 46340a81b439SJeff Roberson 4635dfe13344SJeff Roberson #ifdef NUMA 46360a81b439SJeff Roberson /* 46370a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 46380a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 46390a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 464091d947bfSJeff Roberson * correct domains. 46410a81b439SJeff Roberson */ 4642c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4643c6fd3e23SJeff Roberson vm_ndomains > 2 && PCPU_GET(domain) != itemdomain) { 464491d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 46450a81b439SJeff Roberson return; 46460a81b439SJeff Roberson } 46470a81b439SJeff Roberson #endif 464891d947bfSJeff Roberson 46490a81b439SJeff Roberson /* 46500a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 46510a81b439SJeff Roberson */ 46520a81b439SJeff Roberson CTR3(KTR_UMA, 46530a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 46540a81b439SJeff Roberson zone->uz_name, zone, bucket); 46550a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 4656c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0) 4657c6fd3e23SJeff Roberson itemdomain = zone_domain_lowest(zone, itemdomain); 4658c6fd3e23SJeff Roberson zone_put_bucket(zone, itemdomain, bucket, udata, ws); 46598355f576SJeff Roberson } 4660fc03d22bSJeff Roberson 46614d104ba0SAlexander Motin /* 46620a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 46630a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 46640a81b439SJeff Roberson * 46650a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 46660a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 46670a81b439SJeff Roberson * the caller should retry. 46684d104ba0SAlexander Motin */ 46690a81b439SJeff Roberson static __noinline bool 46702cb67bd7SGleb Smirnoff cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, int itemdomain) 46710a81b439SJeff Roberson { 4672dfe13344SJeff Roberson uma_cache_bucket_t cbucket; 4673d4665eaaSJeff Roberson uma_bucket_t newbucket, bucket; 46740a81b439SJeff Roberson 46750a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 46760a81b439SJeff Roberson 4677d4665eaaSJeff Roberson if (zone->uz_bucket_size == 0) 46780a81b439SJeff Roberson return false; 46790a81b439SJeff Roberson 4680cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4681d4665eaaSJeff Roberson newbucket = NULL; 46820a81b439SJeff Roberson 46830a81b439SJeff Roberson /* 4684dfe13344SJeff Roberson * FIRSTTOUCH domains need to free to the correct zdom. When 4685dfe13344SJeff Roberson * enabled this is the zdom of the item. The bucket is the 4686dfe13344SJeff Roberson * cross bucket if the current domain and itemdomain do not match. 46870a81b439SJeff Roberson */ 4688dfe13344SJeff Roberson cbucket = &cache->uc_freebucket; 4689dfe13344SJeff Roberson #ifdef NUMA 4690c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) { 4691c6fd3e23SJeff Roberson if (PCPU_GET(domain) != itemdomain) { 4692dfe13344SJeff Roberson cbucket = &cache->uc_crossbucket; 4693dfe13344SJeff Roberson if (cbucket->ucb_cnt != 0) 4694c6fd3e23SJeff Roberson counter_u64_add(zone->uz_xdomain, 4695dfe13344SJeff Roberson cbucket->ucb_cnt); 4696dfe13344SJeff Roberson } 4697c6fd3e23SJeff Roberson } 46980a81b439SJeff Roberson #endif 4699dfe13344SJeff Roberson bucket = cache_bucket_unload(cbucket); 4700c6fd3e23SJeff Roberson KASSERT(bucket == NULL || bucket->ub_cnt == bucket->ub_entries, 4701c6fd3e23SJeff Roberson ("cache_free: Entered with non-full free bucket.")); 47020a81b439SJeff Roberson 47030a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 47040a81b439SJeff Roberson critical_exit(); 47050a81b439SJeff Roberson 4706d4665eaaSJeff Roberson /* 4707d4665eaaSJeff Roberson * Don't let SMR zones operate without a free bucket. Force 4708d4665eaaSJeff Roberson * a synchronize and re-use this one. We will only degrade 4709d4665eaaSJeff Roberson * to a synchronize every bucket_size items rather than every 4710d4665eaaSJeff Roberson * item if we fail to allocate a bucket. 4711d4665eaaSJeff Roberson */ 4712d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) { 4713d4665eaaSJeff Roberson if (bucket != NULL) 4714d4665eaaSJeff Roberson bucket->ub_seq = smr_advance(zone->uz_smr); 4715d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4716d4665eaaSJeff Roberson if (newbucket == NULL && bucket != NULL) { 4717d4665eaaSJeff Roberson bucket_drain(zone, bucket); 4718d4665eaaSJeff Roberson newbucket = bucket; 4719d4665eaaSJeff Roberson bucket = NULL; 4720d4665eaaSJeff Roberson } 4721d4665eaaSJeff Roberson } else if (!bucketdisable) 4722d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4723d4665eaaSJeff Roberson 47240a81b439SJeff Roberson if (bucket != NULL) 4725c6fd3e23SJeff Roberson zone_free_bucket(zone, bucket, udata, itemdomain, true); 4726a553d4b8SJeff Roberson 4727fc03d22bSJeff Roberson critical_enter(); 4728d4665eaaSJeff Roberson if ((bucket = newbucket) == NULL) 47290a81b439SJeff Roberson return (false); 4730cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4731dfe13344SJeff Roberson #ifdef NUMA 4732fc03d22bSJeff Roberson /* 47330a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 47340a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 47350a81b439SJeff Roberson * the free bucket. 4736fc03d22bSJeff Roberson */ 4737c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) { 4738c6fd3e23SJeff Roberson if (PCPU_GET(domain) != itemdomain && 4739376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 4740376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 47410a81b439SJeff Roberson return (true); 47420a81b439SJeff Roberson } 47430a81b439SJeff Roberson } 47440a81b439SJeff Roberson #endif 47450a81b439SJeff Roberson /* 47460a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 47470a81b439SJeff Roberson */ 4748376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 4749fc03d22bSJeff Roberson critical_exit(); 47506fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 47510a81b439SJeff Roberson critical_enter(); 47520a81b439SJeff Roberson } else 4753376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 47548355f576SJeff Roberson 47550a81b439SJeff Roberson return (true); 47568355f576SJeff Roberson } 47578355f576SJeff Roberson 47588355f576SJeff Roberson static void 4759bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 47608355f576SJeff Roberson { 4761bb15d1c7SGleb Smirnoff uma_keg_t keg; 4762ab3185d1SJeff Roberson uma_domain_t dom; 47639b8db4d0SRyan Libby int freei; 4764099a0e58SBosko Milekic 4765bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 47668b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 4767ab3185d1SJeff Roberson 47688355f576SJeff Roberson /* Do we need to remove from any lists? */ 47698b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 4770099a0e58SBosko Milekic if (slab->us_freecount + 1 == keg->uk_ipers) { 47718355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4772ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 47734ab3aee8SMark Johnston dom->ud_free_slabs++; 47748355f576SJeff Roberson } else if (slab->us_freecount == 0) { 47758355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4776ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 47778355f576SJeff Roberson } 47788355f576SJeff Roberson 4779ef72505eSJeff Roberson /* Slab management. */ 47801e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 47819b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 47828355f576SJeff Roberson slab->us_freecount++; 47838355f576SJeff Roberson 4784ef72505eSJeff Roberson /* Keg statistics. */ 47854ab3aee8SMark Johnston dom->ud_free_items++; 47860095a784SJeff Roberson } 47870095a784SJeff Roberson 47880095a784SJeff Roberson static void 4789b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 47900095a784SJeff Roberson { 47918b987a77SJeff Roberson struct mtx *lock; 4792b75c4efcSAndrew Turner uma_zone_t zone; 47930095a784SJeff Roberson uma_slab_t slab; 47940095a784SJeff Roberson uma_keg_t keg; 47950095a784SJeff Roberson uint8_t *mem; 47968b987a77SJeff Roberson void *item; 47970095a784SJeff Roberson int i; 47988355f576SJeff Roberson 4799b75c4efcSAndrew Turner zone = arg; 4800bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 48018b987a77SJeff Roberson lock = NULL; 480254c5ae80SRyan Libby if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0)) 48038b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 48040095a784SJeff Roberson for (i = 0; i < cnt; i++) { 48050095a784SJeff Roberson item = bucket[i]; 480654c5ae80SRyan Libby if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) { 48070095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 48088b987a77SJeff Roberson } else { 48098b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 481054c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0) 48118b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 48128b987a77SJeff Roberson else 48138b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 48148b987a77SJeff Roberson } 48158b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 48168b987a77SJeff Roberson if (lock != NULL) 48178b987a77SJeff Roberson mtx_unlock(lock); 48188b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 48198b987a77SJeff Roberson } 4820bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 48210095a784SJeff Roberson } 48228b987a77SJeff Roberson if (lock != NULL) 48238b987a77SJeff Roberson mtx_unlock(lock); 48248355f576SJeff Roberson } 48258355f576SJeff Roberson 48260095a784SJeff Roberson /* 48270095a784SJeff Roberson * Frees a single item to any zone. 48280095a784SJeff Roberson * 48290095a784SJeff Roberson * Arguments: 48300095a784SJeff Roberson * zone The zone to free to 48310095a784SJeff Roberson * item The item we're freeing 48320095a784SJeff Roberson * udata User supplied data for the dtor 48330095a784SJeff Roberson * skip Skip dtors and finis 48340095a784SJeff Roberson */ 48356d88d784SJeff Roberson static __noinline void 48360095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 48370095a784SJeff Roberson { 4838c5deaf04SGleb Smirnoff 4839d4665eaaSJeff Roberson /* 4840d4665eaaSJeff Roberson * If a free is sent directly to an SMR zone we have to 4841d4665eaaSJeff Roberson * synchronize immediately because the item can instantly 4842d4665eaaSJeff Roberson * be reallocated. This should only happen in degenerate 4843d4665eaaSJeff Roberson * cases when no memory is available for per-cpu caches. 4844d4665eaaSJeff Roberson */ 4845d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE) 4846d4665eaaSJeff Roberson smr_synchronize(zone->uz_smr); 4847d4665eaaSJeff Roberson 4848cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 48490095a784SJeff Roberson 485009c8cb71SMark Johnston if (skip < SKIP_FINI && zone->uz_fini) { 485109c8cb71SMark Johnston kasan_mark_item_valid(zone, item); 48520095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 485309c8cb71SMark Johnston kasan_mark_item_invalid(zone, item); 485409c8cb71SMark Johnston } 48550095a784SJeff Roberson 48560095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4857bb15d1c7SGleb Smirnoff 4858bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4859bb15d1c7SGleb Smirnoff return; 4860bb15d1c7SGleb Smirnoff 48612efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 48622efcc8cbSGleb Smirnoff 48634bd61e19SJeff Roberson if (zone->uz_max_items > 0) 48644bd61e19SJeff Roberson zone_free_limit(zone, 1); 4865bb45b411SGleb Smirnoff } 48660095a784SJeff Roberson 48678355f576SJeff Roberson /* See uma.h */ 48681c6cae97SLawrence Stewart int 4869736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4870736ee590SJeff Roberson { 4871e574d407SMark Johnston 4872e574d407SMark Johnston /* 4873e574d407SMark Johnston * If the limit is small, we may need to constrain the maximum per-CPU 4874e574d407SMark Johnston * cache size, or disable caching entirely. 4875e574d407SMark Johnston */ 4876e574d407SMark Johnston uma_zone_set_maxcache(zone, nitems); 4877bb15d1c7SGleb Smirnoff 48784bd61e19SJeff Roberson /* 48794bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 48804bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 48814bd61e19SJeff Roberson * way to clear a limit. 48824bd61e19SJeff Roberson */ 4883bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4884bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4885cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4886cc7ce83aSJeff Roberson zone_update_caches(zone); 48874bd61e19SJeff Roberson /* We may need to wake waiters. */ 48884bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4889bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4890bb15d1c7SGleb Smirnoff 4891bb15d1c7SGleb Smirnoff return (nitems); 4892bb15d1c7SGleb Smirnoff } 4893bb15d1c7SGleb Smirnoff 4894bb15d1c7SGleb Smirnoff /* See uma.h */ 4895003cf08bSMark Johnston void 4896bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4897bb15d1c7SGleb Smirnoff { 4898e574d407SMark Johnston int bpcpu, bpdom, bsize, nb; 4899bb15d1c7SGleb Smirnoff 4900bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4901e574d407SMark Johnston 4902e574d407SMark Johnston /* 4903e574d407SMark Johnston * Compute a lower bound on the number of items that may be cached in 4904e574d407SMark Johnston * the zone. Each CPU gets at least two buckets, and for cross-domain 4905e574d407SMark Johnston * frees we use an additional bucket per CPU and per domain. Select the 4906e574d407SMark Johnston * largest bucket size that does not exceed half of the requested limit, 4907e574d407SMark Johnston * with the left over space given to the full bucket cache. 4908e574d407SMark Johnston */ 4909e574d407SMark Johnston bpdom = 0; 4910003cf08bSMark Johnston bpcpu = 2; 4911e574d407SMark Johnston #ifdef NUMA 4912e574d407SMark Johnston if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && vm_ndomains > 1) { 4913003cf08bSMark Johnston bpcpu++; 4914e574d407SMark Johnston bpdom++; 4915003cf08bSMark Johnston } 4916e574d407SMark Johnston #endif 4917e574d407SMark Johnston nb = bpcpu * mp_ncpus + bpdom * vm_ndomains; 4918e574d407SMark Johnston bsize = nitems / nb / 2; 4919e574d407SMark Johnston if (bsize > BUCKET_MAX) 4920e574d407SMark Johnston bsize = BUCKET_MAX; 4921e574d407SMark Johnston else if (bsize == 0 && nitems / nb > 0) 4922e574d407SMark Johnston bsize = 1; 4923e574d407SMark Johnston zone->uz_bucket_size_max = zone->uz_bucket_size = bsize; 492420a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 492520a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4926e574d407SMark Johnston zone->uz_bucket_max = nitems - nb * bsize; 4927bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4928736ee590SJeff Roberson } 4929736ee590SJeff Roberson 4930736ee590SJeff Roberson /* See uma.h */ 4931e49471b0SAndre Oppermann int 4932e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4933e49471b0SAndre Oppermann { 4934e49471b0SAndre Oppermann int nitems; 4935e49471b0SAndre Oppermann 4936727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4937e49471b0SAndre Oppermann 4938e49471b0SAndre Oppermann return (nitems); 4939e49471b0SAndre Oppermann } 4940e49471b0SAndre Oppermann 4941e49471b0SAndre Oppermann /* See uma.h */ 49422f891cd5SPawel Jakub Dawidek void 49432f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 49442f891cd5SPawel Jakub Dawidek { 49452f891cd5SPawel Jakub Dawidek 4946727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 49472f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 49482f891cd5SPawel Jakub Dawidek } 49492f891cd5SPawel Jakub Dawidek 49502f891cd5SPawel Jakub Dawidek /* See uma.h */ 495154503a13SJonathan T. Looney void 495254503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 495354503a13SJonathan T. Looney { 495454503a13SJonathan T. Looney 4955727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4956e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 495754503a13SJonathan T. Looney } 495854503a13SJonathan T. Looney 495954503a13SJonathan T. Looney /* See uma.h */ 4960c4ae7908SLawrence Stewart int 4961c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4962c4ae7908SLawrence Stewart { 4963c4ae7908SLawrence Stewart int64_t nitems; 4964c4ae7908SLawrence Stewart u_int i; 4965c4ae7908SLawrence Stewart 4966bfb6b7a1SJeff Roberson nitems = 0; 4967bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER) 49682efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 49692efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4970727c6918SJeff Roberson CPU_FOREACH(i) 4971727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4972727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4973c4ae7908SLawrence Stewart 4974c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4975c4ae7908SLawrence Stewart } 4976c4ae7908SLawrence Stewart 497720a4e154SJeff Roberson static uint64_t 497820a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 497920a4e154SJeff Roberson { 498020a4e154SJeff Roberson uint64_t nitems; 498120a4e154SJeff Roberson u_int i; 498220a4e154SJeff Roberson 4983bfb6b7a1SJeff Roberson nitems = 0; 4984bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER) 498520a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4986727c6918SJeff Roberson CPU_FOREACH(i) 4987727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 498820a4e154SJeff Roberson 498920a4e154SJeff Roberson return (nitems); 499020a4e154SJeff Roberson } 499120a4e154SJeff Roberson 499220a4e154SJeff Roberson static uint64_t 499320a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 499420a4e154SJeff Roberson { 499520a4e154SJeff Roberson uint64_t nitems; 499620a4e154SJeff Roberson u_int i; 499720a4e154SJeff Roberson 4998bfb6b7a1SJeff Roberson nitems = 0; 4999bfb6b7a1SJeff Roberson if (zone->uz_frees != EARLY_COUNTER) 500020a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 5001727c6918SJeff Roberson CPU_FOREACH(i) 5002727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 500320a4e154SJeff Roberson 500420a4e154SJeff Roberson return (nitems); 500520a4e154SJeff Roberson } 500620a4e154SJeff Roberson 500731c251a0SJeff Roberson #ifdef INVARIANTS 500831c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */ 500931c251a0SJeff Roberson static uint64_t 501031c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg) 501131c251a0SJeff Roberson { 501231c251a0SJeff Roberson uma_zone_t z; 501331c251a0SJeff Roberson uint64_t nitems; 501431c251a0SJeff Roberson 501531c251a0SJeff Roberson nitems = 0; 501631c251a0SJeff Roberson LIST_FOREACH(z, &keg->uk_zones, uz_link) 501731c251a0SJeff Roberson nitems += uma_zone_get_allocs(z); 501831c251a0SJeff Roberson 501931c251a0SJeff Roberson return (nitems); 502031c251a0SJeff Roberson } 502131c251a0SJeff Roberson #endif 502231c251a0SJeff Roberson 5023c4ae7908SLawrence Stewart /* See uma.h */ 5024736ee590SJeff Roberson void 5025099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 5026099a0e58SBosko Milekic { 5027e20a199fSJeff Roberson uma_keg_t keg; 5028e20a199fSJeff Roberson 5029bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5030727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 5031e20a199fSJeff Roberson keg->uk_init = uminit; 5032099a0e58SBosko Milekic } 5033099a0e58SBosko Milekic 5034099a0e58SBosko Milekic /* See uma.h */ 5035099a0e58SBosko Milekic void 5036099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 5037099a0e58SBosko Milekic { 5038e20a199fSJeff Roberson uma_keg_t keg; 5039e20a199fSJeff Roberson 5040bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5041727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 5042e20a199fSJeff Roberson keg->uk_fini = fini; 5043099a0e58SBosko Milekic } 5044099a0e58SBosko Milekic 5045099a0e58SBosko Milekic /* See uma.h */ 5046099a0e58SBosko Milekic void 5047099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 5048099a0e58SBosko Milekic { 5049af526374SJeff Roberson 5050727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 5051099a0e58SBosko Milekic zone->uz_init = zinit; 5052099a0e58SBosko Milekic } 5053099a0e58SBosko Milekic 5054099a0e58SBosko Milekic /* See uma.h */ 5055099a0e58SBosko Milekic void 5056099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 5057099a0e58SBosko Milekic { 5058af526374SJeff Roberson 5059727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 5060099a0e58SBosko Milekic zone->uz_fini = zfini; 5061099a0e58SBosko Milekic } 5062099a0e58SBosko Milekic 5063099a0e58SBosko Milekic /* See uma.h */ 5064099a0e58SBosko Milekic void 50658355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 50668355f576SJeff Roberson { 50670095a784SJeff Roberson uma_keg_t keg; 5068e20a199fSJeff Roberson 5069bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5070727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 50710095a784SJeff Roberson keg->uk_freef = freef; 50728355f576SJeff Roberson } 50738355f576SJeff Roberson 50748355f576SJeff Roberson /* See uma.h */ 50758355f576SJeff Roberson void 50768355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 50778355f576SJeff Roberson { 5078e20a199fSJeff Roberson uma_keg_t keg; 5079e20a199fSJeff Roberson 5080bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5081727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 5082e20a199fSJeff Roberson keg->uk_allocf = allocf; 50838355f576SJeff Roberson } 50848355f576SJeff Roberson 50858355f576SJeff Roberson /* See uma.h */ 50866fd34d6fSJeff Roberson void 5087d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr) 5088d4665eaaSJeff Roberson { 5089d4665eaaSJeff Roberson 5090d4665eaaSJeff Roberson ZONE_ASSERT_COLD(zone); 5091d4665eaaSJeff Roberson 50927f746c9fSMateusz Guzik KASSERT(smr != NULL, ("Got NULL smr")); 50937f746c9fSMateusz Guzik KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 50947f746c9fSMateusz Guzik ("zone %p (%s) already uses SMR", zone, zone->uz_name)); 5095d4665eaaSJeff Roberson zone->uz_flags |= UMA_ZONE_SMR; 5096d4665eaaSJeff Roberson zone->uz_smr = smr; 5097d4665eaaSJeff Roberson zone_update_caches(zone); 5098d4665eaaSJeff Roberson } 5099d4665eaaSJeff Roberson 5100d4665eaaSJeff Roberson smr_t 5101d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone) 5102d4665eaaSJeff Roberson { 5103d4665eaaSJeff Roberson 5104d4665eaaSJeff Roberson return (zone->uz_smr); 5105d4665eaaSJeff Roberson } 5106d4665eaaSJeff Roberson 5107d4665eaaSJeff Roberson /* See uma.h */ 5108d4665eaaSJeff Roberson void 51096fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 51106fd34d6fSJeff Roberson { 51116fd34d6fSJeff Roberson uma_keg_t keg; 51126fd34d6fSJeff Roberson 5113bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5114727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 51156fd34d6fSJeff Roberson keg->uk_reserve = items; 51166fd34d6fSJeff Roberson } 51176fd34d6fSJeff Roberson 51186fd34d6fSJeff Roberson /* See uma.h */ 51198355f576SJeff Roberson int 5120a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 51218355f576SJeff Roberson { 5122099a0e58SBosko Milekic uma_keg_t keg; 51238355f576SJeff Roberson vm_offset_t kva; 51249ba30bcbSZbigniew Bodek u_int pages; 51258355f576SJeff Roberson 5126bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5127727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 5128727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 51298355f576SJeff Roberson 513079c9f942SJeff Roberson pages = howmany(count, keg->uk_ipers) * keg->uk_ppera; 5131a553d4b8SJeff Roberson 5132a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 5133a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 5134a4915c21SAttilio Rao #else 5135a4915c21SAttilio Rao if (1) { 5136a4915c21SAttilio Rao #endif 513757223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 5138d1f42ac2SAlan Cox if (kva == 0) 51398355f576SJeff Roberson return (0); 5140a4915c21SAttilio Rao } else 5141a4915c21SAttilio Rao kva = 0; 5142bb15d1c7SGleb Smirnoff 5143bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 5144099a0e58SBosko Milekic keg->uk_kva = kva; 5145a4915c21SAttilio Rao keg->uk_offset = 0; 5146bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 5147a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 5148a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 5149a4915c21SAttilio Rao #else 5150a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 5151a4915c21SAttilio Rao #endif 5152cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 5153cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 5154cc7ce83aSJeff Roberson zone_update_caches(zone); 5155af526374SJeff Roberson 51568355f576SJeff Roberson return (1); 51578355f576SJeff Roberson } 51588355f576SJeff Roberson 51598355f576SJeff Roberson /* See uma.h */ 51608355f576SJeff Roberson void 51618355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 51628355f576SJeff Roberson { 5163920239efSMark Johnston struct vm_domainset_iter di; 5164ab3185d1SJeff Roberson uma_domain_t dom; 51658355f576SJeff Roberson uma_slab_t slab; 5166099a0e58SBosko Milekic uma_keg_t keg; 516786220393SMark Johnston int aflags, domain, slabs; 51688355f576SJeff Roberson 5169bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 517079c9f942SJeff Roberson slabs = howmany(items, keg->uk_ipers); 5171194a979eSMark Johnston while (slabs-- > 0) { 517286220393SMark Johnston aflags = M_NOWAIT; 517386220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 517486220393SMark Johnston &aflags); 517586220393SMark Johnston for (;;) { 517686220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 517786220393SMark Johnston aflags); 517886220393SMark Johnston if (slab != NULL) { 5179ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 51804ab3aee8SMark Johnston /* 51814ab3aee8SMark Johnston * keg_alloc_slab() always returns a slab on the 51824ab3aee8SMark Johnston * partial list. 51834ab3aee8SMark Johnston */ 51848b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 518586220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 518686220393SMark Johnston us_link); 51874ab3aee8SMark Johnston dom->ud_free_slabs++; 51888b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 5189920239efSMark Johnston break; 51908355f576SJeff Roberson } 51918b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 519289d2fb14SKonstantin Belousov vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0); 519386220393SMark Johnston } 519486220393SMark Johnston } 519586220393SMark Johnston } 51968355f576SJeff Roberson 5197ed581bf6SJeff Roberson /* 5198ed581bf6SJeff Roberson * Returns a snapshot of memory consumption in bytes. 5199ed581bf6SJeff Roberson */ 5200ed581bf6SJeff Roberson size_t 5201ed581bf6SJeff Roberson uma_zone_memory(uma_zone_t zone) 5202ed581bf6SJeff Roberson { 5203ed581bf6SJeff Roberson size_t sz; 5204ed581bf6SJeff Roberson int i; 5205ed581bf6SJeff Roberson 5206ed581bf6SJeff Roberson sz = 0; 5207ed581bf6SJeff Roberson if (zone->uz_flags & UMA_ZFLAG_CACHE) { 5208ed581bf6SJeff Roberson for (i = 0; i < vm_ndomains; i++) 5209c6fd3e23SJeff Roberson sz += ZDOM_GET(zone, i)->uzd_nitems; 5210ed581bf6SJeff Roberson return (sz * zone->uz_size); 5211ed581bf6SJeff Roberson } 5212ed581bf6SJeff Roberson for (i = 0; i < vm_ndomains; i++) 5213ed581bf6SJeff Roberson sz += zone->uz_keg->uk_domain[i].ud_pages; 5214ed581bf6SJeff Roberson 5215ed581bf6SJeff Roberson return (sz * PAGE_SIZE); 5216ed581bf6SJeff Roberson } 5217ed581bf6SJeff Roberson 5218389a3fa6SMark Johnston struct uma_reclaim_args { 5219389a3fa6SMark Johnston int domain; 5220389a3fa6SMark Johnston int req; 5221389a3fa6SMark Johnston }; 5222389a3fa6SMark Johnston 5223389a3fa6SMark Johnston static void 5224389a3fa6SMark Johnston uma_reclaim_domain_cb(uma_zone_t zone, void *arg) 5225389a3fa6SMark Johnston { 5226389a3fa6SMark Johnston struct uma_reclaim_args *args; 5227389a3fa6SMark Johnston 5228389a3fa6SMark Johnston args = arg; 5229389a3fa6SMark Johnston if ((zone->uz_flags & UMA_ZONE_UNMANAGED) == 0) 5230389a3fa6SMark Johnston uma_zone_reclaim_domain(zone, args->req, args->domain); 5231389a3fa6SMark Johnston } 5232389a3fa6SMark Johnston 52338355f576SJeff Roberson /* See uma.h */ 523408cfa56eSMark Johnston void 523508cfa56eSMark Johnston uma_reclaim(int req) 52368355f576SJeff Roberson { 5237aabe13f1SMark Johnston uma_reclaim_domain(req, UMA_ANYDOMAIN); 5238aabe13f1SMark Johnston } 523944ec2b63SKonstantin Belousov 5240aabe13f1SMark Johnston void 5241aabe13f1SMark Johnston uma_reclaim_domain(int req, int domain) 5242aabe13f1SMark Johnston { 5243389a3fa6SMark Johnston struct uma_reclaim_args args; 5244aabe13f1SMark Johnston 524586bbae32SJeff Roberson bucket_enable(); 524608cfa56eSMark Johnston 5247389a3fa6SMark Johnston args.domain = domain; 5248389a3fa6SMark Johnston args.req = req; 5249389a3fa6SMark Johnston 5250aabe13f1SMark Johnston sx_slock(&uma_reclaim_lock); 525108cfa56eSMark Johnston switch (req) { 525208cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 525308cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 5254389a3fa6SMark Johnston zone_foreach(uma_reclaim_domain_cb, &args); 5255aabe13f1SMark Johnston break; 525608cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 5257389a3fa6SMark Johnston zone_foreach(uma_reclaim_domain_cb, &args); 525808cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 5259389a3fa6SMark Johnston zone_foreach(uma_reclaim_domain_cb, &args); 526008cfa56eSMark Johnston break; 526108cfa56eSMark Johnston default: 526208cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 526308cfa56eSMark Johnston } 52640f9b7bf3SMark Johnston 52658355f576SJeff Roberson /* 52668355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 52678355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 52688355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 52698355f576SJeff Roberson */ 5270389a3fa6SMark Johnston uma_zone_reclaim_domain(slabzones[0], UMA_RECLAIM_DRAIN, domain); 5271389a3fa6SMark Johnston uma_zone_reclaim_domain(slabzones[1], UMA_RECLAIM_DRAIN, domain); 5272aabe13f1SMark Johnston bucket_zone_drain(domain); 5273aabe13f1SMark Johnston sx_sunlock(&uma_reclaim_lock); 52748355f576SJeff Roberson } 52758355f576SJeff Roberson 52762e47807cSJeff Roberson static volatile int uma_reclaim_needed; 527744ec2b63SKonstantin Belousov 527844ec2b63SKonstantin Belousov void 527944ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 528044ec2b63SKonstantin Belousov { 528144ec2b63SKonstantin Belousov 52822e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 52832e47807cSJeff Roberson wakeup(uma_reclaim); 528444ec2b63SKonstantin Belousov } 528544ec2b63SKonstantin Belousov 528644ec2b63SKonstantin Belousov void 528744ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 528844ec2b63SKonstantin Belousov { 528944ec2b63SKonstantin Belousov 529044ec2b63SKonstantin Belousov for (;;) { 529108cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 5292200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 529308cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 52942e47807cSJeff Roberson hz); 529508cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 52969b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 529708cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 5298200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 52992e47807cSJeff Roberson /* Don't fire more than once per-second. */ 53002e47807cSJeff Roberson pause("umarclslp", hz); 530144ec2b63SKonstantin Belousov } 530244ec2b63SKonstantin Belousov } 530344ec2b63SKonstantin Belousov 5304663b416fSJohn Baldwin /* See uma.h */ 530508cfa56eSMark Johnston void 530608cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 530708cfa56eSMark Johnston { 5308aabe13f1SMark Johnston uma_zone_reclaim_domain(zone, req, UMA_ANYDOMAIN); 5309aabe13f1SMark Johnston } 531008cfa56eSMark Johnston 5311aabe13f1SMark Johnston void 5312aabe13f1SMark Johnston uma_zone_reclaim_domain(uma_zone_t zone, int req, int domain) 5313aabe13f1SMark Johnston { 531408cfa56eSMark Johnston switch (req) { 531508cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 5316389a3fa6SMark Johnston zone_reclaim(zone, domain, M_NOWAIT, false); 531708cfa56eSMark Johnston break; 531808cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 5319389a3fa6SMark Johnston zone_reclaim(zone, domain, M_NOWAIT, true); 532008cfa56eSMark Johnston break; 532108cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 532208cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 5323389a3fa6SMark Johnston zone_reclaim(zone, domain, M_NOWAIT, true); 532408cfa56eSMark Johnston break; 532508cfa56eSMark Johnston default: 532608cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 532708cfa56eSMark Johnston } 532808cfa56eSMark Johnston } 532908cfa56eSMark Johnston 533008cfa56eSMark Johnston /* See uma.h */ 5331663b416fSJohn Baldwin int 5332663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 5333663b416fSJohn Baldwin { 5334663b416fSJohn Baldwin 5335727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 53366c125b8dSMohan Srinivasan } 53376c125b8dSMohan Srinivasan 53382e47807cSJeff Roberson unsigned long 53392e47807cSJeff Roberson uma_limit(void) 53402e47807cSJeff Roberson { 53412e47807cSJeff Roberson 53422e47807cSJeff Roberson return (uma_kmem_limit); 53432e47807cSJeff Roberson } 53442e47807cSJeff Roberson 53452e47807cSJeff Roberson void 53462e47807cSJeff Roberson uma_set_limit(unsigned long limit) 53472e47807cSJeff Roberson { 53482e47807cSJeff Roberson 53492e47807cSJeff Roberson uma_kmem_limit = limit; 53502e47807cSJeff Roberson } 53512e47807cSJeff Roberson 53522e47807cSJeff Roberson unsigned long 53532e47807cSJeff Roberson uma_size(void) 53542e47807cSJeff Roberson { 53552e47807cSJeff Roberson 5356058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 5357ad5b0f5bSJeff Roberson } 5358ad5b0f5bSJeff Roberson 5359ad5b0f5bSJeff Roberson long 5360ad5b0f5bSJeff Roberson uma_avail(void) 5361ad5b0f5bSJeff Roberson { 5362ad5b0f5bSJeff Roberson 5363058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 53642e47807cSJeff Roberson } 53652e47807cSJeff Roberson 5366a0d4b0aeSRobert Watson #ifdef DDB 53678355f576SJeff Roberson /* 53687a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 53697a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 53707a52a97eSRobert Watson * 53717a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 53727a52a97eSRobert Watson * per-CPU cache statistic. 53737a52a97eSRobert Watson * 53747a52a97eSRobert Watson */ 53757a52a97eSRobert Watson static void 53760f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 5377c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 53787a52a97eSRobert Watson { 53797a52a97eSRobert Watson uma_cache_t cache; 5380c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 53817a52a97eSRobert Watson int cachefree, cpu; 53827a52a97eSRobert Watson 5383c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 53847a52a97eSRobert Watson cachefree = 0; 53853aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 53867a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 5387376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 5388376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 5389376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 5390376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 53917a52a97eSRobert Watson allocs += cache->uc_allocs; 53927a52a97eSRobert Watson frees += cache->uc_frees; 53937a52a97eSRobert Watson } 53942efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 53952efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 5396c6fd3e23SJeff Roberson xdomain += counter_u64_fetch(z->uz_xdomain); 5397bf965959SSean Bruno sleeps += z->uz_sleeps; 53987a52a97eSRobert Watson if (cachefreep != NULL) 53997a52a97eSRobert Watson *cachefreep = cachefree; 54007a52a97eSRobert Watson if (allocsp != NULL) 54017a52a97eSRobert Watson *allocsp = allocs; 54027a52a97eSRobert Watson if (freesp != NULL) 54037a52a97eSRobert Watson *freesp = frees; 5404bf965959SSean Bruno if (sleepsp != NULL) 5405bf965959SSean Bruno *sleepsp = sleeps; 5406c1685086SJeff Roberson if (xdomainp != NULL) 5407c1685086SJeff Roberson *xdomainp = xdomain; 54087a52a97eSRobert Watson } 5409a0d4b0aeSRobert Watson #endif /* DDB */ 54107a52a97eSRobert Watson 54117a52a97eSRobert Watson static int 54127a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 54137a52a97eSRobert Watson { 54147a52a97eSRobert Watson uma_keg_t kz; 54157a52a97eSRobert Watson uma_zone_t z; 54167a52a97eSRobert Watson int count; 54177a52a97eSRobert Watson 54187a52a97eSRobert Watson count = 0; 5419111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 54207a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 54217a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 54227a52a97eSRobert Watson count++; 54237a52a97eSRobert Watson } 5424b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 5425b47acb0aSGleb Smirnoff count++; 5426b47acb0aSGleb Smirnoff 5427111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 54287a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 54297a52a97eSRobert Watson } 54307a52a97eSRobert Watson 5431b47acb0aSGleb Smirnoff static void 5432b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 5433b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 5434b47acb0aSGleb Smirnoff { 5435b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 5436b47acb0aSGleb Smirnoff uma_cache_t cache; 5437b47acb0aSGleb Smirnoff int i; 5438b47acb0aSGleb Smirnoff 5439b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 5440c6fd3e23SJeff Roberson zdom = ZDOM_GET(z, i); 5441b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 5442b47acb0aSGleb Smirnoff } 5443b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 5444b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 5445b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 5446c6fd3e23SJeff Roberson uth->uth_xdomain = counter_u64_fetch(z->uz_xdomain); 5447b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 54481de9724eSMark Johnston 5449b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 5450b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 5451b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 5452b47acb0aSGleb Smirnoff continue; 5453b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 5454376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 5455376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 5456376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 5457b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 5458b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 5459b47acb0aSGleb Smirnoff } 5460b47acb0aSGleb Smirnoff } 5461b47acb0aSGleb Smirnoff 54627a52a97eSRobert Watson static int 54637a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 54647a52a97eSRobert Watson { 54657a52a97eSRobert Watson struct uma_stream_header ush; 54667a52a97eSRobert Watson struct uma_type_header uth; 546763b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 54687a52a97eSRobert Watson struct sbuf sbuf; 54697a52a97eSRobert Watson uma_keg_t kz; 54707a52a97eSRobert Watson uma_zone_t z; 54714bd61e19SJeff Roberson uint64_t items; 54728b987a77SJeff Roberson uint32_t kfree, pages; 54734e657159SMatthew D Fleming int count, error, i; 54747a52a97eSRobert Watson 547500f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 547600f0e671SMatthew D Fleming if (error != 0) 547700f0e671SMatthew D Fleming return (error); 54784e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 54791eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 548063b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 54814e657159SMatthew D Fleming 5482404a593eSMatthew D Fleming count = 0; 5483111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 54847a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 54857a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 54867a52a97eSRobert Watson count++; 54877a52a97eSRobert Watson } 54887a52a97eSRobert Watson 5489b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 5490b47acb0aSGleb Smirnoff count++; 5491b47acb0aSGleb Smirnoff 54927a52a97eSRobert Watson /* 54937a52a97eSRobert Watson * Insert stream header. 54947a52a97eSRobert Watson */ 54957a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 54967a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 5497ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 54987a52a97eSRobert Watson ush.ush_count = count; 54994e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 55007a52a97eSRobert Watson 55017a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 55028b987a77SJeff Roberson kfree = pages = 0; 55038b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 55044ab3aee8SMark Johnston kfree += kz->uk_domain[i].ud_free_items; 55058b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 55068b987a77SJeff Roberson } 55077a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 55087a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 5509cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 55107a52a97eSRobert Watson uth.uth_align = kz->uk_align; 55117a52a97eSRobert Watson uth.uth_size = kz->uk_size; 55127a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 55134bd61e19SJeff Roberson if (z->uz_max_items > 0) { 55144bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 55154bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 5516bb15d1c7SGleb Smirnoff kz->uk_ppera; 55174bd61e19SJeff Roberson } else 55188b987a77SJeff Roberson uth.uth_pages = pages; 5519f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 5520bb15d1c7SGleb Smirnoff kz->uk_ppera; 5521bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 55228b987a77SJeff Roberson uth.uth_keg_free = kfree; 5523cbbb4a00SRobert Watson 5524cbbb4a00SRobert Watson /* 5525cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 5526cbbb4a00SRobert Watson * on the keg's zone list. 5527cbbb4a00SRobert Watson */ 5528e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 5529cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 5530cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 5531b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 5532b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 553363b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 553463b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 553563b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 55367a52a97eSRobert Watson } 55377a52a97eSRobert Watson } 5538b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 5539b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 5540b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 5541b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 5542b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 5543b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 5544b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 5545b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 5546b47acb0aSGleb Smirnoff } 5547b47acb0aSGleb Smirnoff 5548111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 55494e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 55504e657159SMatthew D Fleming sbuf_delete(&sbuf); 555163b5d112SKonstantin Belousov free(ups, M_TEMP); 55527a52a97eSRobert Watson return (error); 55537a52a97eSRobert Watson } 555448c5777eSRobert Watson 55550a5a3ccbSGleb Smirnoff int 55560a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 55570a5a3ccbSGleb Smirnoff { 55580a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 555916be9f54SGleb Smirnoff int error, max; 55600a5a3ccbSGleb Smirnoff 556116be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 55620a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 55630a5a3ccbSGleb Smirnoff if (error || !req->newptr) 55640a5a3ccbSGleb Smirnoff return (error); 55650a5a3ccbSGleb Smirnoff 55660a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 55670a5a3ccbSGleb Smirnoff 55680a5a3ccbSGleb Smirnoff return (0); 55690a5a3ccbSGleb Smirnoff } 55700a5a3ccbSGleb Smirnoff 55710a5a3ccbSGleb Smirnoff int 55720a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 55730a5a3ccbSGleb Smirnoff { 557420a4e154SJeff Roberson uma_zone_t zone; 55750a5a3ccbSGleb Smirnoff int cur; 55760a5a3ccbSGleb Smirnoff 557720a4e154SJeff Roberson /* 557820a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 557920a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 558020a4e154SJeff Roberson */ 558120a4e154SJeff Roberson if (arg2 == 0) 558220a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 558320a4e154SJeff Roberson else 558420a4e154SJeff Roberson zone = arg1; 55850a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 55860a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 55870a5a3ccbSGleb Smirnoff } 55880a5a3ccbSGleb Smirnoff 558920a4e154SJeff Roberson static int 559020a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 559120a4e154SJeff Roberson { 559220a4e154SJeff Roberson uma_zone_t zone = arg1; 559320a4e154SJeff Roberson uint64_t cur; 559420a4e154SJeff Roberson 559520a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 559620a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 559720a4e154SJeff Roberson } 559820a4e154SJeff Roberson 559920a4e154SJeff Roberson static int 560020a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 560120a4e154SJeff Roberson { 560220a4e154SJeff Roberson uma_zone_t zone = arg1; 560320a4e154SJeff Roberson uint64_t cur; 560420a4e154SJeff Roberson 560520a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 560620a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 560720a4e154SJeff Roberson } 560820a4e154SJeff Roberson 56096d204a6aSRyan Libby static int 56106d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 56116d204a6aSRyan Libby { 56126d204a6aSRyan Libby struct sbuf sbuf; 56136d204a6aSRyan Libby uma_zone_t zone = arg1; 56146d204a6aSRyan Libby int error; 56156d204a6aSRyan Libby 56166d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 56176d204a6aSRyan Libby if (zone->uz_flags != 0) 56186d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 56196d204a6aSRyan Libby else 56206d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 56216d204a6aSRyan Libby error = sbuf_finish(&sbuf); 56226d204a6aSRyan Libby sbuf_delete(&sbuf); 56236d204a6aSRyan Libby 56246d204a6aSRyan Libby return (error); 56256d204a6aSRyan Libby } 56266d204a6aSRyan Libby 5627f7af5015SRyan Libby static int 5628f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 5629f7af5015SRyan Libby { 5630f7af5015SRyan Libby uma_keg_t keg = arg1; 5631f7af5015SRyan Libby int avail, effpct, total; 5632f7af5015SRyan Libby 5633f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 563454c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 56359b8db4d0SRyan Libby total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize; 5636f7af5015SRyan Libby /* 5637f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 5638f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 5639f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 5640f7af5015SRyan Libby */ 5641f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 5642f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 5643f7af5015SRyan Libby avail *= mp_maxid + 1; 5644f7af5015SRyan Libby effpct = 100 * avail / total; 5645f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 5646f7af5015SRyan Libby } 5647f7af5015SRyan Libby 56484bd61e19SJeff Roberson static int 56494bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 56504bd61e19SJeff Roberson { 56514bd61e19SJeff Roberson uma_zone_t zone = arg1; 56524bd61e19SJeff Roberson uint64_t cur; 56534bd61e19SJeff Roberson 56544bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 56554bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 56564bd61e19SJeff Roberson } 56574bd61e19SJeff Roberson 56589542ea7bSGleb Smirnoff #ifdef INVARIANTS 56599542ea7bSGleb Smirnoff static uma_slab_t 56609542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 56619542ea7bSGleb Smirnoff { 56629542ea7bSGleb Smirnoff uma_slab_t slab; 56639542ea7bSGleb Smirnoff uma_keg_t keg; 56649542ea7bSGleb Smirnoff uint8_t *mem; 56659542ea7bSGleb Smirnoff 56669542ea7bSGleb Smirnoff /* 56679542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 56689542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 56699542ea7bSGleb Smirnoff * essentially holds a reference. 56709542ea7bSGleb Smirnoff */ 5671727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 5672727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5673bb15d1c7SGleb Smirnoff return (NULL); 567454c5ae80SRyan Libby if (zone->uz_flags & UMA_ZFLAG_VTOSLAB) 5675727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 5676bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 567754c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0) 5678727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 56798b987a77SJeff Roberson KEG_LOCK(keg, 0); 56809542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 56818b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 56829542ea7bSGleb Smirnoff 56839542ea7bSGleb Smirnoff return (slab); 56849542ea7bSGleb Smirnoff } 56859542ea7bSGleb Smirnoff 5686c5deaf04SGleb Smirnoff static bool 5687c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 5688c5deaf04SGleb Smirnoff { 5689c5deaf04SGleb Smirnoff 5690727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5691c5deaf04SGleb Smirnoff return (true); 5692c5deaf04SGleb Smirnoff 5693bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 5694c5deaf04SGleb Smirnoff } 5695c5deaf04SGleb Smirnoff 5696c5deaf04SGleb Smirnoff static bool 5697c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 5698c5deaf04SGleb Smirnoff { 5699c5deaf04SGleb Smirnoff uintptr_t idx; 5700c5deaf04SGleb Smirnoff 5701c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 5702c5deaf04SGleb Smirnoff return (true); 5703c5deaf04SGleb Smirnoff 5704c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 5705c5deaf04SGleb Smirnoff return (false); 5706c5deaf04SGleb Smirnoff 5707c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 5708c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 5709c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 5710c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 5711c5deaf04SGleb Smirnoff } 5712c5deaf04SGleb Smirnoff 5713c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 5714c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 5715c5deaf04SGleb Smirnoff return (true); 5716c5deaf04SGleb Smirnoff } 5717c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 5718c5deaf04SGleb Smirnoff 5719c5deaf04SGleb Smirnoff return (false); 5720c5deaf04SGleb Smirnoff } 5721c5deaf04SGleb Smirnoff 57229542ea7bSGleb Smirnoff /* 57239542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 57249542ea7bSGleb Smirnoff * 57259542ea7bSGleb Smirnoff */ 57269542ea7bSGleb Smirnoff static void 57279542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 57289542ea7bSGleb Smirnoff { 57299542ea7bSGleb Smirnoff uma_keg_t keg; 57309542ea7bSGleb Smirnoff int freei; 57319542ea7bSGleb Smirnoff 57329542ea7bSGleb Smirnoff if (slab == NULL) { 57339542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 57349542ea7bSGleb Smirnoff if (slab == NULL) 5735952c8964SMark Johnston panic("uma: item %p did not belong to zone %s", 57369542ea7bSGleb Smirnoff item, zone->uz_name); 57379542ea7bSGleb Smirnoff } 5738584061b4SJeff Roberson keg = zone->uz_keg; 57391e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 57409542ea7bSGleb Smirnoff 5741942951baSRyan Libby if (BIT_TEST_SET_ATOMIC(keg->uk_ipers, freei, 5742942951baSRyan Libby slab_dbg_bits(slab, keg))) 5743952c8964SMark Johnston panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)", 57449542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 57459542ea7bSGleb Smirnoff } 57469542ea7bSGleb Smirnoff 57479542ea7bSGleb Smirnoff /* 57489542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 57499542ea7bSGleb Smirnoff * and duplicate frees. 57509542ea7bSGleb Smirnoff * 57519542ea7bSGleb Smirnoff */ 57529542ea7bSGleb Smirnoff static void 57539542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 57549542ea7bSGleb Smirnoff { 57559542ea7bSGleb Smirnoff uma_keg_t keg; 57569542ea7bSGleb Smirnoff int freei; 57579542ea7bSGleb Smirnoff 57589542ea7bSGleb Smirnoff if (slab == NULL) { 57599542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 57609542ea7bSGleb Smirnoff if (slab == NULL) 5761952c8964SMark Johnston panic("uma: Freed item %p did not belong to zone %s", 57629542ea7bSGleb Smirnoff item, zone->uz_name); 57639542ea7bSGleb Smirnoff } 5764584061b4SJeff Roberson keg = zone->uz_keg; 57651e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 57669542ea7bSGleb Smirnoff 57679542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 5768952c8964SMark Johnston panic("Invalid free of %p from zone %p(%s) slab %p(%d)", 57699542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 57709542ea7bSGleb Smirnoff 57711e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 5772952c8964SMark Johnston panic("Unaligned free of %p from zone %p(%s) slab %p(%d)", 57739542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 57749542ea7bSGleb Smirnoff 5775942951baSRyan Libby if (!BIT_TEST_CLR_ATOMIC(keg->uk_ipers, freei, 5776942951baSRyan Libby slab_dbg_bits(slab, keg))) 5777952c8964SMark Johnston panic("Duplicate free of %p from zone %p(%s) slab %p(%d)", 57789542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 57799542ea7bSGleb Smirnoff } 57809542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 57819542ea7bSGleb Smirnoff 578248c5777eSRobert Watson #ifdef DDB 578346d70077SConrad Meyer static int64_t 578446d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 57850223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 578648c5777eSRobert Watson { 578746d70077SConrad Meyer uint64_t frees; 57880f9b7bf3SMark Johnston int i; 578948c5777eSRobert Watson 579048c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 579146d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 57922efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 579346d70077SConrad Meyer *sleeps = z->uz_sleeps; 579446d70077SConrad Meyer *cachefree = 0; 579546d70077SConrad Meyer *xdomain = 0; 579648c5777eSRobert Watson } else 579746d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 579846d70077SConrad Meyer xdomain); 57998b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 5800c6fd3e23SJeff Roberson *cachefree += ZDOM_GET(z, i)->uzd_nitems; 5801e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 580248c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 58034ab3aee8SMark Johnston *cachefree += kz->uk_domain[i].ud_free_items; 58048b987a77SJeff Roberson } 580546d70077SConrad Meyer *used = *allocs - frees; 580646d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 580746d70077SConrad Meyer } 58080f9b7bf3SMark Johnston 580946d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 581046d70077SConrad Meyer { 581146d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 581246d70077SConrad Meyer uma_keg_t kz; 581346d70077SConrad Meyer uma_zone_t z; 581446d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 581546d70077SConrad Meyer long cachefree; 581646d70077SConrad Meyer /* variables for sorting */ 581746d70077SConrad Meyer uma_keg_t cur_keg; 581846d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 581946d70077SConrad Meyer int64_t cur_size, last_size, size; 582046d70077SConrad Meyer int ties; 582146d70077SConrad Meyer 582246d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 582346d70077SConrad Meyer if (modif[0] == 'i') { 582446d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 582546d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 582646d70077SConrad Meyer } else { 582746d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 582846d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 582946d70077SConrad Meyer } 583046d70077SConrad Meyer 583146d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 583246d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 583346d70077SConrad Meyer 583446d70077SConrad Meyer /* Sort the zones with largest size first. */ 583546d70077SConrad Meyer last_zone = NULL; 583646d70077SConrad Meyer last_size = INT64_MAX; 583746d70077SConrad Meyer for (;;) { 583846d70077SConrad Meyer cur_zone = NULL; 583946d70077SConrad Meyer cur_size = -1; 584046d70077SConrad Meyer ties = 0; 584146d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 584246d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 584346d70077SConrad Meyer /* 584446d70077SConrad Meyer * In the case of size ties, print out zones 584546d70077SConrad Meyer * in the order they are encountered. That is, 584646d70077SConrad Meyer * when we encounter the most recently output 584746d70077SConrad Meyer * zone, we have already printed all preceding 584846d70077SConrad Meyer * ties, and we must print all following ties. 584946d70077SConrad Meyer */ 585046d70077SConrad Meyer if (z == last_zone) { 585146d70077SConrad Meyer ties = 1; 585246d70077SConrad Meyer continue; 585346d70077SConrad Meyer } 585446d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 585546d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 585646d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 585746d70077SConrad Meyer { 585846d70077SConrad Meyer cur_size = size; 585946d70077SConrad Meyer cur_zone = z; 586046d70077SConrad Meyer cur_keg = kz; 586146d70077SConrad Meyer } 586246d70077SConrad Meyer } 586346d70077SConrad Meyer } 586446d70077SConrad Meyer if (cur_zone == NULL) 586546d70077SConrad Meyer break; 586646d70077SConrad Meyer 586746d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 586846d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 586946d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 587046d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 587146d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 587220a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 587320a4e154SJeff Roberson xdomain); 587446d70077SConrad Meyer 5875687c94aaSJohn Baldwin if (db_pager_quit) 5876687c94aaSJohn Baldwin return; 587746d70077SConrad Meyer last_zone = cur_zone; 587846d70077SConrad Meyer last_size = cur_size; 587948c5777eSRobert Watson } 588048c5777eSRobert Watson } 588103175483SAlexander Motin 588203175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 588303175483SAlexander Motin { 588403175483SAlexander Motin uma_zone_t z; 5885ab3185d1SJeff Roberson uint64_t allocs, frees; 58860f9b7bf3SMark Johnston long cachefree; 58870f9b7bf3SMark Johnston int i; 588803175483SAlexander Motin 588903175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 589003175483SAlexander Motin "Requests", "Bucket"); 589103175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 5892c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 58930f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 5894c6fd3e23SJeff Roberson cachefree += ZDOM_GET(z, i)->uzd_nitems; 58950f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 589603175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 589703175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 589820a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 589903175483SAlexander Motin if (db_pager_quit) 590003175483SAlexander Motin return; 590103175483SAlexander Motin } 590203175483SAlexander Motin } 59039542ea7bSGleb Smirnoff #endif /* DDB */ 5904