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; 1054*a04ce833SMark 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 && 1064*a04ce833SMark Johnston (seq = atomic_load_32(&zdom->uzd_seq)) != SMR_SEQ_INVALID && 1065*a04ce833SMark 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. */ 12262760658bSAlexander Motin for (int i = 0; i < vm_ndomains; i++) { 12272760658bSAlexander Motin if (bucket_cache_reclaim_domain(zone, false, false, i) && 12282760658bSAlexander Motin (zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 12292760658bSAlexander Motin keg_drain(zone->uz_keg, i); 12302760658bSAlexander Motin } 12318355f576SJeff Roberson } 12328355f576SJeff Roberson 12338355f576SJeff Roberson /* 12345300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 12355300d9ddSJeff Roberson * backing store. 12365300d9ddSJeff Roberson * 12375300d9ddSJeff Roberson * Arguments: 12380aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 12395300d9ddSJeff Roberson * 12405300d9ddSJeff Roberson * Returns: 1241763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 12425300d9ddSJeff Roberson */ 124337c84183SPoul-Henning Kamp static int 12443b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 12455300d9ddSJeff Roberson { 124659568a0eSAlexander Motin size_t alloc; 12475300d9ddSJeff Roberson 12483b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 12493b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 12503b2f2cb8SAlexander Motin hash->uh_hashsize = size; 12510aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 12521e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 12535300d9ddSJeff Roberson } else { 12540aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 1255e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 1256ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 12570aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 12585300d9ddSJeff Roberson } 12590aef6126SJeff Roberson if (hash->uh_slab_hash) { 12600aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 12610aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 12620aef6126SJeff Roberson return (1); 12630aef6126SJeff Roberson } 12645300d9ddSJeff Roberson 12650aef6126SJeff Roberson return (0); 12665300d9ddSJeff Roberson } 12675300d9ddSJeff Roberson 12685300d9ddSJeff Roberson /* 126964f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 127064f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 127164f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 12728355f576SJeff Roberson * 12738355f576SJeff Roberson * Arguments: 12740aef6126SJeff Roberson * oldhash The hash you want to expand 12750aef6126SJeff Roberson * newhash The hash structure for the new table 12768355f576SJeff Roberson * 12778355f576SJeff Roberson * Returns: 12788355f576SJeff Roberson * Nothing 12798355f576SJeff Roberson * 12808355f576SJeff Roberson * Discussion: 12818355f576SJeff Roberson */ 12820aef6126SJeff Roberson static int 12830aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 12848355f576SJeff Roberson { 12851e0701e1SJeff Roberson uma_hash_slab_t slab; 12866929b7d1SPedro F. Giffuni u_int hval; 12876929b7d1SPedro F. Giffuni u_int idx; 12888355f576SJeff Roberson 12890aef6126SJeff Roberson if (!newhash->uh_slab_hash) 12900aef6126SJeff Roberson return (0); 12918355f576SJeff Roberson 12920aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 12930aef6126SJeff Roberson return (0); 12948355f576SJeff Roberson 12958355f576SJeff Roberson /* 12968355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 12978355f576SJeff Roberson * full rehash. 12988355f576SJeff Roberson */ 12998355f576SJeff Roberson 13006929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 13011e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 13021e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 13031e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 13041e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 13051e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 13061e0701e1SJeff Roberson slab, uhs_hlink); 13078355f576SJeff Roberson } 13088355f576SJeff Roberson 13090aef6126SJeff Roberson return (1); 13109c2cd7e5SJeff Roberson } 13119c2cd7e5SJeff Roberson 13125300d9ddSJeff Roberson /* 13135300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 13145300d9ddSJeff Roberson * 13155300d9ddSJeff Roberson * Arguments: 13165300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 13175300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 13185300d9ddSJeff Roberson * 13195300d9ddSJeff Roberson * Returns: 13205300d9ddSJeff Roberson * Nothing 13215300d9ddSJeff Roberson */ 13229c2cd7e5SJeff Roberson static void 13230aef6126SJeff Roberson hash_free(struct uma_hash *hash) 13249c2cd7e5SJeff Roberson { 13250aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 13260aef6126SJeff Roberson return; 13270aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 13280095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 13298355f576SJeff Roberson else 1330961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 13318355f576SJeff Roberson } 13328355f576SJeff Roberson 13338355f576SJeff Roberson /* 13348355f576SJeff Roberson * Frees all outstanding items in a bucket 13358355f576SJeff Roberson * 13368355f576SJeff Roberson * Arguments: 13378355f576SJeff Roberson * zone The zone to free to, must be unlocked. 13384bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 13398355f576SJeff Roberson * 13408355f576SJeff Roberson * Returns: 13418355f576SJeff Roberson * Nothing 13428355f576SJeff Roberson */ 13438355f576SJeff Roberson static void 13448355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 13458355f576SJeff Roberson { 13460095a784SJeff Roberson int i; 13478355f576SJeff Roberson 1348c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 13498355f576SJeff Roberson return; 13508355f576SJeff Roberson 1351d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 1352d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 1353d4665eaaSJeff Roberson smr_wait(zone->uz_smr, bucket->ub_seq); 1354543117beSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 1355d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 1356d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], 1357d4665eaaSJeff Roberson zone->uz_size, NULL, SKIP_NONE); 1358d4665eaaSJeff Roberson } 13590095a784SJeff Roberson if (zone->uz_fini) 136009c8cb71SMark Johnston for (i = 0; i < bucket->ub_cnt; i++) { 136109c8cb71SMark Johnston kasan_mark_item_valid(zone, bucket->ub_bucket[i]); 13620095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 136309c8cb71SMark Johnston kasan_mark_item_invalid(zone, bucket->ub_bucket[i]); 136409c8cb71SMark Johnston } 13650095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 13664bd61e19SJeff Roberson if (zone->uz_max_items > 0) 13674bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 1368d4665eaaSJeff Roberson #ifdef INVARIANTS 1369d4665eaaSJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt); 1370d4665eaaSJeff Roberson #endif 13710095a784SJeff Roberson bucket->ub_cnt = 0; 13728355f576SJeff Roberson } 13738355f576SJeff Roberson 13748355f576SJeff Roberson /* 13758355f576SJeff Roberson * Drains the per cpu caches for a zone. 13768355f576SJeff Roberson * 1377727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 13785d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 13795d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 13805d1ae027SRobert Watson * 13818355f576SJeff Roberson * Arguments: 13828355f576SJeff Roberson * zone The zone to drain, must be unlocked. 13838355f576SJeff Roberson * 13848355f576SJeff Roberson * Returns: 13858355f576SJeff Roberson * Nothing 13868355f576SJeff Roberson */ 13878355f576SJeff Roberson static void 13889643769aSJeff Roberson cache_drain(uma_zone_t zone) 13898355f576SJeff Roberson { 13908355f576SJeff Roberson uma_cache_t cache; 1391376b1ba3SJeff Roberson uma_bucket_t bucket; 1392543117beSJeff Roberson smr_seq_t seq; 13938355f576SJeff Roberson int cpu; 13948355f576SJeff Roberson 13958355f576SJeff Roberson /* 13965d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 13975d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 13985d1ae027SRobert Watson * of the caches at this point. 13995d1ae027SRobert Watson * 14005d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 14015d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 14028355f576SJeff Roberson */ 1403543117beSJeff Roberson seq = SMR_SEQ_INVALID; 1404543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 1405226dd6dbSJeff Roberson seq = smr_advance(zone->uz_smr); 14063aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 14078355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 1408376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 1409c6fd3e23SJeff Roberson if (bucket != NULL) 1410376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1411376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 1412376b1ba3SJeff Roberson if (bucket != NULL) { 1413543117beSJeff Roberson bucket->ub_seq = seq; 1414376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1415376b1ba3SJeff Roberson } 1416376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 1417376b1ba3SJeff Roberson if (bucket != NULL) { 1418543117beSJeff Roberson bucket->ub_seq = seq; 1419376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1420376b1ba3SJeff Roberson } 1421d56368d7SBosko Milekic } 1422aabe13f1SMark Johnston bucket_cache_reclaim(zone, true, UMA_ANYDOMAIN); 1423aaa8bb16SJeff Roberson } 1424aaa8bb16SJeff Roberson 1425a2de44abSAlexander Motin static void 142620a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 1427a2de44abSAlexander Motin { 1428a2de44abSAlexander Motin 1429a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1430a2de44abSAlexander Motin return; 1431a2de44abSAlexander Motin 1432aabe13f1SMark Johnston ZONE_LOCK(zone); 143320a4e154SJeff Roberson zone->uz_bucket_size = 143420a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 1435aabe13f1SMark Johnston ZONE_UNLOCK(zone); 1436a2de44abSAlexander Motin } 1437a2de44abSAlexander Motin 1438a2de44abSAlexander Motin static void 143920a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 1440a2de44abSAlexander Motin { 1441a2de44abSAlexander Motin uma_cache_t cache; 1442c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 1443ab3185d1SJeff Roberson int domain; 1444a2de44abSAlexander Motin 1445a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1446a2de44abSAlexander Motin return; 1447a2de44abSAlexander Motin 1448c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1449a2de44abSAlexander Motin critical_enter(); 1450a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1451c6fd3e23SJeff Roberson domain = PCPU_GET(domain); 1452376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1453d4665eaaSJeff Roberson 1454d4665eaaSJeff Roberson /* 1455d4665eaaSJeff Roberson * Don't flush SMR zone buckets. This leaves the zone without a 1456d4665eaaSJeff Roberson * bucket and forces every free to synchronize(). 1457d4665eaaSJeff Roberson */ 1458543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0) { 1459376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1460543117beSJeff Roberson b3 = cache_bucket_unload_cross(cache); 1461543117beSJeff Roberson } 1462543117beSJeff Roberson critical_exit(); 1463543117beSJeff Roberson 1464543117beSJeff Roberson if (b1 != NULL) 1465c6fd3e23SJeff Roberson zone_free_bucket(zone, b1, NULL, domain, false); 1466543117beSJeff Roberson if (b2 != NULL) 1467c6fd3e23SJeff Roberson zone_free_bucket(zone, b2, NULL, domain, false); 1468543117beSJeff Roberson if (b3 != NULL) { 1469c6fd3e23SJeff Roberson /* Adjust the domain so it goes to zone_free_cross. */ 1470c6fd3e23SJeff Roberson domain = (domain + 1) % vm_ndomains; 1471c6fd3e23SJeff Roberson zone_free_bucket(zone, b3, NULL, domain, false); 1472c1685086SJeff Roberson } 1473a2de44abSAlexander Motin } 1474a2de44abSAlexander Motin 1475a2de44abSAlexander Motin /* 1476a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1477a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1478a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1479a2de44abSAlexander Motin * to safely access their cache buckets. 1480a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1481a2de44abSAlexander Motin */ 1482a2de44abSAlexander Motin static void 148308cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1484a2de44abSAlexander Motin { 1485a2de44abSAlexander Motin int cpu; 1486a2de44abSAlexander Motin 1487a2de44abSAlexander Motin /* 1488727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1489a2de44abSAlexander Motin */ 1490a2de44abSAlexander Motin if (zone) 149120a4e154SJeff Roberson cache_shrink(zone, NULL); 1492a2de44abSAlexander Motin else 149320a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1494a2de44abSAlexander Motin 1495a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1496a2de44abSAlexander Motin thread_lock(curthread); 1497a2de44abSAlexander Motin sched_bind(curthread, cpu); 1498a2de44abSAlexander Motin thread_unlock(curthread); 1499a2de44abSAlexander Motin 1500a2de44abSAlexander Motin if (zone) 150120a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1502a2de44abSAlexander Motin else 150320a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1504a2de44abSAlexander Motin } 1505a2de44abSAlexander Motin thread_lock(curthread); 1506a2de44abSAlexander Motin sched_unbind(curthread); 1507a2de44abSAlexander Motin thread_unlock(curthread); 1508a2de44abSAlexander Motin } 1509a2de44abSAlexander Motin 1510aaa8bb16SJeff Roberson /* 151108cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 151208cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 151308cfa56eSMark Johnston * estimated working set size. 1514aaa8bb16SJeff Roberson */ 15152760658bSAlexander Motin static bool 15162760658bSAlexander Motin bucket_cache_reclaim_domain(uma_zone_t zone, bool drain, bool trim, int domain) 1517aaa8bb16SJeff Roberson { 1518ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1519aaa8bb16SJeff Roberson uma_bucket_t bucket; 1520c6fd3e23SJeff Roberson long target; 15212760658bSAlexander Motin bool done = false; 15228355f576SJeff Roberson 1523c6fd3e23SJeff Roberson /* 152491d947bfSJeff Roberson * The cross bucket is partially filled and not part of 152591d947bfSJeff Roberson * the item count. Reclaim it individually here. 152691d947bfSJeff Roberson */ 152754f421f9SMark Johnston zdom = ZDOM_GET(zone, domain); 1528226dd6dbSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 || drain) { 152991d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 153091d947bfSJeff Roberson bucket = zdom->uzd_cross; 153191d947bfSJeff Roberson zdom->uzd_cross = NULL; 153291d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 1533c6fd3e23SJeff Roberson if (bucket != NULL) 153491d947bfSJeff Roberson bucket_free(zone, bucket, NULL); 153591d947bfSJeff Roberson } 153691d947bfSJeff Roberson 153791d947bfSJeff Roberson /* 153808cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 15392760658bSAlexander Motin * this bucket cache is empty. If trim, we reclaim items in 15402760658bSAlexander Motin * excess of the zone's estimated working set size. Multiple 15412760658bSAlexander Motin * consecutive calls will shrink the WSS and so reclaim more. 15422760658bSAlexander Motin * If neither drain nor trim, then voluntarily reclaim 1/4 15432760658bSAlexander Motin * (to reduce first spike) of items not used for a long time. 154408cfa56eSMark Johnston */ 1545c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 15462760658bSAlexander Motin zone_domain_update_wss(zdom); 15472760658bSAlexander Motin if (drain) 15482760658bSAlexander Motin target = 0; 15492760658bSAlexander Motin else if (trim) 15502760658bSAlexander Motin target = zdom->uzd_wss; 15512760658bSAlexander Motin else if (zdom->uzd_timin > 900 / UMA_TIMEOUT) 15522760658bSAlexander Motin target = zdom->uzd_nitems - zdom->uzd_limin / 4; 15532760658bSAlexander Motin else { 15542760658bSAlexander Motin ZDOM_UNLOCK(zdom); 15552760658bSAlexander Motin return (done); 15562760658bSAlexander Motin } 15572760658bSAlexander Motin while ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) != NULL && 15582760658bSAlexander Motin zdom->uzd_nitems >= target + bucket->ub_cnt) { 1559c6fd3e23SJeff Roberson bucket = zone_fetch_bucket(zone, zdom, true); 156008cfa56eSMark Johnston if (bucket == NULL) 156108cfa56eSMark Johnston break; 15626fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 15632760658bSAlexander Motin done = true; 1564c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 15658355f576SJeff Roberson } 1566c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 15672760658bSAlexander Motin return (done); 1568ab3185d1SJeff Roberson } 156954f421f9SMark Johnston 157054f421f9SMark Johnston static void 1571aabe13f1SMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain, int domain) 157254f421f9SMark Johnston { 157354f421f9SMark Johnston int i; 157454f421f9SMark Johnston 157554f421f9SMark Johnston /* 157654f421f9SMark Johnston * Shrink the zone bucket size to ensure that the per-CPU caches 157754f421f9SMark Johnston * don't grow too large. 157854f421f9SMark Johnston */ 157954f421f9SMark Johnston if (zone->uz_bucket_size > zone->uz_bucket_size_min) 158054f421f9SMark Johnston zone->uz_bucket_size--; 158154f421f9SMark Johnston 1582aabe13f1SMark Johnston if (domain != UMA_ANYDOMAIN && 1583aabe13f1SMark Johnston (zone->uz_flags & UMA_ZONE_ROUNDROBIN) == 0) { 15842760658bSAlexander Motin bucket_cache_reclaim_domain(zone, drain, true, domain); 1585aabe13f1SMark Johnston } else { 158654f421f9SMark Johnston for (i = 0; i < vm_ndomains; i++) 15872760658bSAlexander Motin bucket_cache_reclaim_domain(zone, drain, true, i); 15888355f576SJeff Roberson } 1589aabe13f1SMark Johnston } 1590fc03d22bSJeff Roberson 1591fc03d22bSJeff Roberson static void 1592fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1593fc03d22bSJeff Roberson { 1594fc03d22bSJeff Roberson uint8_t *mem; 159509c8cb71SMark Johnston size_t size; 1596fc03d22bSJeff Roberson int i; 1597fc03d22bSJeff Roberson uint8_t flags; 1598fc03d22bSJeff Roberson 15991431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 16001431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 16011431a748SGleb Smirnoff 16021e0701e1SJeff Roberson mem = slab_data(slab, keg); 160309c8cb71SMark Johnston size = PAGE_SIZE * keg->uk_ppera; 160409c8cb71SMark Johnston 160509c8cb71SMark Johnston kasan_mark_slab_valid(keg, mem); 1606fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 160709c8cb71SMark Johnston for (i = start - 1; i > -1; i--) 1608c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1609c5deaf04SGleb Smirnoff /* 1610c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1611c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1612c5deaf04SGleb Smirnoff * which executed trash_dtor. 1613c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1614c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1615c5deaf04SGleb Smirnoff * invocations. 1616c5deaf04SGleb Smirnoff */ 16171e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1618c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1619c5deaf04SGleb Smirnoff #endif 16201e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1621fc03d22bSJeff Roberson } 162209c8cb71SMark Johnston flags = slab->us_flags; 162309c8cb71SMark Johnston if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 16249b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab), 16259b8db4d0SRyan Libby NULL, SKIP_NONE); 162609c8cb71SMark Johnston } 162709c8cb71SMark Johnston keg->uk_freef(mem, size, flags); 162809c8cb71SMark Johnston uma_total_dec(size); 16298355f576SJeff Roberson } 16308355f576SJeff Roberson 1631f09cbea3SMark Johnston static void 1632f09cbea3SMark Johnston keg_drain_domain(uma_keg_t keg, int domain) 1633f09cbea3SMark Johnston { 1634f09cbea3SMark Johnston struct slabhead freeslabs; 1635f09cbea3SMark Johnston uma_domain_t dom; 1636f09cbea3SMark Johnston uma_slab_t slab, tmp; 1637f09cbea3SMark Johnston uint32_t i, stofree, stokeep, partial; 1638f09cbea3SMark Johnston 1639f09cbea3SMark Johnston dom = &keg->uk_domain[domain]; 1640f09cbea3SMark Johnston LIST_INIT(&freeslabs); 1641f09cbea3SMark Johnston 1642f09cbea3SMark Johnston CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u", 1643575a4437SEd Maste keg->uk_name, keg, domain, dom->ud_free_items); 1644f09cbea3SMark Johnston 1645f09cbea3SMark Johnston KEG_LOCK(keg, domain); 1646f09cbea3SMark Johnston 1647f09cbea3SMark Johnston /* 1648f09cbea3SMark Johnston * Are the free items in partially allocated slabs sufficient to meet 1649f09cbea3SMark Johnston * the reserve? If not, compute the number of fully free slabs that must 1650f09cbea3SMark Johnston * be kept. 1651f09cbea3SMark Johnston */ 1652f09cbea3SMark Johnston partial = dom->ud_free_items - dom->ud_free_slabs * keg->uk_ipers; 1653f09cbea3SMark Johnston if (partial < keg->uk_reserve) { 1654f09cbea3SMark Johnston stokeep = min(dom->ud_free_slabs, 1655f09cbea3SMark Johnston howmany(keg->uk_reserve - partial, keg->uk_ipers)); 1656f09cbea3SMark Johnston } else { 1657f09cbea3SMark Johnston stokeep = 0; 1658f09cbea3SMark Johnston } 1659f09cbea3SMark Johnston stofree = dom->ud_free_slabs - stokeep; 1660f09cbea3SMark Johnston 1661f09cbea3SMark Johnston /* 1662f09cbea3SMark Johnston * Partition the free slabs into two sets: those that must be kept in 1663f09cbea3SMark Johnston * order to maintain the reserve, and those that may be released back to 1664f09cbea3SMark Johnston * the system. Since one set may be much larger than the other, 1665f09cbea3SMark Johnston * populate the smaller of the two sets and swap them if necessary. 1666f09cbea3SMark Johnston */ 1667f09cbea3SMark Johnston for (i = min(stofree, stokeep); i > 0; i--) { 1668f09cbea3SMark Johnston slab = LIST_FIRST(&dom->ud_free_slab); 1669f09cbea3SMark Johnston LIST_REMOVE(slab, us_link); 1670f09cbea3SMark Johnston LIST_INSERT_HEAD(&freeslabs, slab, us_link); 1671f09cbea3SMark Johnston } 1672f09cbea3SMark Johnston if (stofree > stokeep) 1673f09cbea3SMark Johnston LIST_SWAP(&freeslabs, &dom->ud_free_slab, uma_slab, us_link); 1674f09cbea3SMark Johnston 1675f09cbea3SMark Johnston if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) { 1676f09cbea3SMark Johnston LIST_FOREACH(slab, &freeslabs, us_link) 1677f09cbea3SMark Johnston UMA_HASH_REMOVE(&keg->uk_hash, slab); 1678f09cbea3SMark Johnston } 1679f09cbea3SMark Johnston dom->ud_free_items -= stofree * keg->uk_ipers; 1680f09cbea3SMark Johnston dom->ud_free_slabs -= stofree; 1681f09cbea3SMark Johnston dom->ud_pages -= stofree * keg->uk_ppera; 1682f09cbea3SMark Johnston KEG_UNLOCK(keg, domain); 1683f09cbea3SMark Johnston 1684f09cbea3SMark Johnston LIST_FOREACH_SAFE(slab, &freeslabs, us_link, tmp) 1685f09cbea3SMark Johnston keg_free_slab(keg, slab, keg->uk_ipers); 1686f09cbea3SMark Johnston } 1687f09cbea3SMark Johnston 16888355f576SJeff Roberson /* 1689e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 16908355f576SJeff Roberson * the pageout daemon. 16918355f576SJeff Roberson * 1692e20a199fSJeff Roberson * Returns nothing. 16938355f576SJeff Roberson */ 1694e20a199fSJeff Roberson static void 1695aabe13f1SMark Johnston keg_drain(uma_keg_t keg, int domain) 16968355f576SJeff Roberson { 1697f09cbea3SMark Johnston int i; 16988355f576SJeff Roberson 1699f09cbea3SMark Johnston if ((keg->uk_flags & UMA_ZONE_NOFREE) != 0) 17008355f576SJeff Roberson return; 1701aabe13f1SMark Johnston if (domain != UMA_ANYDOMAIN) { 1702aabe13f1SMark Johnston keg_drain_domain(keg, domain); 1703aabe13f1SMark Johnston } else { 1704f09cbea3SMark Johnston for (i = 0; i < vm_ndomains; i++) 1705f09cbea3SMark Johnston keg_drain_domain(keg, i); 17068355f576SJeff Roberson } 1707aabe13f1SMark Johnston } 17088355f576SJeff Roberson 1709e20a199fSJeff Roberson static void 1710aabe13f1SMark Johnston zone_reclaim(uma_zone_t zone, int domain, int waitok, bool drain) 1711e20a199fSJeff Roberson { 17128355f576SJeff Roberson /* 1713aabe13f1SMark Johnston * Count active reclaim operations in order to interlock with 1714aabe13f1SMark Johnston * zone_dtor(), which removes the zone from global lists before 1715aabe13f1SMark Johnston * attempting to reclaim items itself. 1716aabe13f1SMark Johnston * 1717aabe13f1SMark Johnston * The zone may be destroyed while sleeping, so only zone_dtor() should 1718aabe13f1SMark Johnston * specify M_WAITOK. 1719e20a199fSJeff Roberson */ 1720e20a199fSJeff Roberson ZONE_LOCK(zone); 1721aabe13f1SMark Johnston if (waitok == M_WAITOK) { 1722aabe13f1SMark Johnston while (zone->uz_reclaimers > 0) 1723aabe13f1SMark Johnston msleep(zone, ZONE_LOCKPTR(zone), PVM, "zonedrain", 1); 1724e20a199fSJeff Roberson } 1725aabe13f1SMark Johnston zone->uz_reclaimers++; 1726e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1727aabe13f1SMark Johnston bucket_cache_reclaim(zone, drain, domain); 172808cfa56eSMark Johnston 172908034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1730aabe13f1SMark Johnston keg_drain(zone->uz_keg, domain); 1731e20a199fSJeff Roberson ZONE_LOCK(zone); 1732aabe13f1SMark Johnston zone->uz_reclaimers--; 1733aabe13f1SMark Johnston if (zone->uz_reclaimers == 0) 1734e20a199fSJeff Roberson wakeup(zone); 1735e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1736e20a199fSJeff Roberson } 1737e20a199fSJeff Roberson 173808cfa56eSMark Johnston static void 1739aabe13f1SMark Johnston zone_drain(uma_zone_t zone, void *arg) 1740e20a199fSJeff Roberson { 1741aabe13f1SMark Johnston int domain; 1742e20a199fSJeff Roberson 1743aabe13f1SMark Johnston domain = (int)(uintptr_t)arg; 1744aabe13f1SMark Johnston zone_reclaim(zone, domain, M_NOWAIT, true); 174508cfa56eSMark Johnston } 174608cfa56eSMark Johnston 174708cfa56eSMark Johnston static void 1748aabe13f1SMark Johnston zone_trim(uma_zone_t zone, void *arg) 174908cfa56eSMark Johnston { 1750aabe13f1SMark Johnston int domain; 175108cfa56eSMark Johnston 1752aabe13f1SMark Johnston domain = (int)(uintptr_t)arg; 1753aabe13f1SMark Johnston zone_reclaim(zone, domain, M_NOWAIT, false); 1754e20a199fSJeff Roberson } 1755e20a199fSJeff Roberson 1756e20a199fSJeff Roberson /* 17578b987a77SJeff Roberson * Allocate a new slab for a keg and inserts it into the partial slab list. 17588b987a77SJeff Roberson * The keg should be unlocked on entry. If the allocation succeeds it will 17598b987a77SJeff Roberson * be locked on return. 17608355f576SJeff Roberson * 17618355f576SJeff Roberson * Arguments: 176286220393SMark Johnston * flags Wait flags for the item initialization routine 176386220393SMark Johnston * aflags Wait flags for the slab allocation 17648355f576SJeff Roberson * 17658355f576SJeff Roberson * Returns: 17668355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 17678355f576SJeff Roberson * caller specified M_NOWAIT. 17688355f576SJeff Roberson */ 17698355f576SJeff Roberson static uma_slab_t 177086220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 177186220393SMark Johnston int aflags) 17728355f576SJeff Roberson { 17738b987a77SJeff Roberson uma_domain_t dom; 1774099a0e58SBosko Milekic uma_slab_t slab; 17752e47807cSJeff Roberson unsigned long size; 177685dcf349SGleb Smirnoff uint8_t *mem; 177786220393SMark Johnston uint8_t sflags; 17788355f576SJeff Roberson int i; 17798355f576SJeff Roberson 1780ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1781ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1782a553d4b8SJeff Roberson 1783194a979eSMark Johnston slab = NULL; 1784194a979eSMark Johnston mem = NULL; 178554c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 17869b8db4d0SRyan Libby uma_hash_slab_t hslab; 17879b8db4d0SRyan Libby hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL, 17889b8db4d0SRyan Libby domain, aflags); 17899b8db4d0SRyan Libby if (hslab == NULL) 1790727c6918SJeff Roberson goto fail; 17919b8db4d0SRyan Libby slab = &hslab->uhs_slab; 1792a553d4b8SJeff Roberson } 1793a553d4b8SJeff Roberson 17943370c5bfSJeff Roberson /* 17953370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 17963370c5bfSJeff Roberson * first time they are added to a zone. 17973370c5bfSJeff Roberson * 17983370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 17993370c5bfSJeff Roberson */ 18003370c5bfSJeff Roberson 1801099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 180286220393SMark Johnston aflags |= M_ZERO; 18033370c5bfSJeff Roberson else 180486220393SMark Johnston aflags &= ~M_ZERO; 18053370c5bfSJeff Roberson 1806263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 180786220393SMark Johnston aflags |= M_NODUMP; 1808263811f7SKip Macy 1809e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1810194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 181109c8cb71SMark Johnston mem = keg->uk_allocf(zone, size, domain, &sflags, aflags); 1812a553d4b8SJeff Roberson if (mem == NULL) { 181354c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 18149b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), 18159b8db4d0SRyan Libby slab_tohashslab(slab), NULL, SKIP_NONE); 1816727c6918SJeff Roberson goto fail; 1817a553d4b8SJeff Roberson } 18182e47807cSJeff Roberson uma_total_inc(size); 18198355f576SJeff Roberson 18208b987a77SJeff Roberson /* For HASH zones all pages go to the same uma_domain. */ 182154c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 18228b987a77SJeff Roberson domain = 0; 18238b987a77SJeff Roberson 18245c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 182554c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) 1826099a0e58SBosko Milekic slab = (uma_slab_t)(mem + keg->uk_pgoff); 18271e0701e1SJeff Roberson else 18289b8db4d0SRyan Libby slab_tohashslab(slab)->uhs_data = mem; 18295c0e403bSJeff Roberson 183054c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_VTOSLAB) 1831099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1832584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1833584061b4SJeff Roberson zone, slab); 18348355f576SJeff Roberson 1835099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 183686220393SMark Johnston slab->us_flags = sflags; 1837ab3185d1SJeff Roberson slab->us_domain = domain; 18388b987a77SJeff Roberson 18399b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1840ef72505eSJeff Roberson #ifdef INVARIANTS 1841815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1842ef72505eSJeff Roberson #endif 1843099a0e58SBosko Milekic 1844b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1845099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 18461e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 184786220393SMark Johnston keg->uk_size, flags) != 0) 1848b23f72e9SBrian Feldman break; 1849b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1850fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1851727c6918SJeff Roberson goto fail; 1852b23f72e9SBrian Feldman } 1853b23f72e9SBrian Feldman } 185409c8cb71SMark Johnston kasan_mark_slab_invalid(keg, mem); 18558b987a77SJeff Roberson KEG_LOCK(keg, domain); 18565c0e403bSJeff Roberson 18571431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 18581431a748SGleb Smirnoff slab, keg->uk_name, keg); 18591431a748SGleb Smirnoff 186054c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 1861099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 18628355f576SJeff Roberson 18638b987a77SJeff Roberson /* 18648b987a77SJeff Roberson * If we got a slab here it's safe to mark it partially used 18658b987a77SJeff Roberson * and return. We assume that the caller is going to remove 18668b987a77SJeff Roberson * at least one item. 18678b987a77SJeff Roberson */ 18688b987a77SJeff Roberson dom = &keg->uk_domain[domain]; 18698b987a77SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 18708b987a77SJeff Roberson dom->ud_pages += keg->uk_ppera; 18714ab3aee8SMark Johnston dom->ud_free_items += keg->uk_ipers; 18728355f576SJeff Roberson 18738355f576SJeff Roberson return (slab); 1874727c6918SJeff Roberson 1875727c6918SJeff Roberson fail: 1876727c6918SJeff Roberson return (NULL); 18778355f576SJeff Roberson } 18788355f576SJeff Roberson 18798355f576SJeff Roberson /* 1880537f92cdSMark Johnston * This function is intended to be used early on in place of page_alloc(). It 1881537f92cdSMark Johnston * performs contiguous physical memory allocations and uses a bump allocator for 1882537f92cdSMark Johnston * KVA, so is usable before the kernel map is initialized. 1883009b6fcbSJeff Roberson */ 1884009b6fcbSJeff Roberson static void * 1885ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1886ab3185d1SJeff Roberson int wait) 1887009b6fcbSJeff Roberson { 1888a81c400eSJeff Roberson vm_paddr_t pa; 1889a81c400eSJeff Roberson vm_page_t m; 189084c39222SMark Johnston int i, pages; 1891099a0e58SBosko Milekic 1892f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1893f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1894a81c400eSJeff Roberson 1895f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 189684c39222SMark Johnston m = vm_page_alloc_noobj_contig_domain(domain, malloc2vm_flags(wait) | 189784c39222SMark Johnston VM_ALLOC_WIRED, pages, (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, 189884c39222SMark Johnston VM_MEMATTR_DEFAULT); 1899a81c400eSJeff Roberson if (m == NULL) 1900a81c400eSJeff Roberson return (NULL); 1901a81c400eSJeff Roberson 1902a81c400eSJeff Roberson pa = VM_PAGE_TO_PHYS(m); 1903a81c400eSJeff Roberson for (i = 0; i < pages; i++, pa += PAGE_SIZE) { 1904c25a30e2SKonstantin Belousov #if defined(__aarch64__) || defined(__amd64__) || \ 1905a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1906a81c400eSJeff Roberson if ((wait & M_NODUMP) == 0) 1907a81c400eSJeff Roberson dump_add_page(pa); 1908a81c400eSJeff Roberson #endif 1909a81c400eSJeff Roberson } 1910f7d35785SGleb Smirnoff 191184c39222SMark Johnston /* Allocate KVA and indirectly advance bootmem. */ 191284c39222SMark Johnston return ((void *)pmap_map(&bootmem, m->phys_addr, 191384c39222SMark Johnston m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE)); 1914f7d35785SGleb Smirnoff } 1915f7d35785SGleb Smirnoff 1916a81c400eSJeff Roberson static void 1917a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes) 1918a81c400eSJeff Roberson { 1919a81c400eSJeff Roberson vm_offset_t va; 1920a81c400eSJeff Roberson vm_page_t m; 1921a81c400eSJeff Roberson 1922a81c400eSJeff Roberson va = (vm_offset_t)mem; 1923a81c400eSJeff Roberson m = PHYS_TO_VM_PAGE(pmap_kextract(va)); 1924663de81fSMark Johnston 1925663de81fSMark Johnston /* 1926663de81fSMark Johnston * startup_alloc() returns direct-mapped slabs on some platforms. Avoid 1927663de81fSMark Johnston * unmapping ranges of the direct map. 1928663de81fSMark Johnston */ 1929663de81fSMark Johnston if (va >= bootstart && va + bytes <= bootmem) 1930a81c400eSJeff Roberson pmap_remove(kernel_pmap, va, va + bytes); 1931a81c400eSJeff Roberson for (; bytes != 0; bytes -= PAGE_SIZE, m++) { 1932c25a30e2SKonstantin Belousov #if defined(__aarch64__) || defined(__amd64__) || \ 1933a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1934a81c400eSJeff Roberson dump_drop_page(VM_PAGE_TO_PHYS(m)); 1935a81c400eSJeff Roberson #endif 1936a81c400eSJeff Roberson vm_page_unwire_noq(m); 1937a81c400eSJeff Roberson vm_page_free(m); 1938a81c400eSJeff Roberson } 1939a81c400eSJeff Roberson } 1940a81c400eSJeff Roberson 1941f7d35785SGleb Smirnoff /* 19428355f576SJeff Roberson * Allocates a number of pages from the system 19438355f576SJeff Roberson * 19448355f576SJeff Roberson * Arguments: 19458355f576SJeff Roberson * bytes The number of bytes requested 19468355f576SJeff Roberson * wait Shall we wait? 19478355f576SJeff Roberson * 19488355f576SJeff Roberson * Returns: 19498355f576SJeff Roberson * A pointer to the alloced memory or possibly 19508355f576SJeff Roberson * NULL if M_NOWAIT is set. 19518355f576SJeff Roberson */ 19528355f576SJeff Roberson static void * 1953ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1954ab3185d1SJeff Roberson int wait) 19558355f576SJeff Roberson { 19568355f576SJeff Roberson void *p; /* Returned page */ 19578355f576SJeff Roberson 19582e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 19599978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 19608355f576SJeff Roberson 19618355f576SJeff Roberson return (p); 19628355f576SJeff Roberson } 19638355f576SJeff Roberson 1964ab3059a8SMatt Macy static void * 1965ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1966ab3059a8SMatt Macy int wait) 1967ab3059a8SMatt Macy { 1968ab3059a8SMatt Macy struct pglist alloctail; 1969ab3059a8SMatt Macy vm_offset_t addr, zkva; 1970ab3059a8SMatt Macy int cpu, flags; 1971ab3059a8SMatt Macy vm_page_t p, p_next; 1972ab3059a8SMatt Macy #ifdef NUMA 1973ab3059a8SMatt Macy struct pcpu *pc; 1974ab3059a8SMatt Macy #endif 1975ab3059a8SMatt Macy 1976ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1977ab3059a8SMatt Macy 1978013072f0SMark Johnston TAILQ_INIT(&alloctail); 1979a4667e09SMark Johnston flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | malloc2vm_flags(wait); 1980013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1981ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1982ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1983a4667e09SMark Johnston p = vm_page_alloc_noobj(flags); 1984ab3059a8SMatt Macy } else { 1985ab3059a8SMatt Macy #ifndef NUMA 1986a4667e09SMark Johnston p = vm_page_alloc_noobj(flags); 1987ab3059a8SMatt Macy #else 1988ab3059a8SMatt Macy pc = pcpu_find(cpu); 198920526802SAndrew Gallatin if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain))) 199020526802SAndrew Gallatin p = NULL; 199120526802SAndrew Gallatin else 1992a4667e09SMark Johnston p = vm_page_alloc_noobj_domain(pc->pc_domain, 1993a4667e09SMark Johnston flags); 1994ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1995a4667e09SMark Johnston p = vm_page_alloc_noobj(flags); 1996ab3059a8SMatt Macy #endif 1997ab3059a8SMatt Macy } 1998ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1999ab3059a8SMatt Macy goto fail; 2000ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 2001ab3059a8SMatt Macy } 2002ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 2003ab3059a8SMatt Macy goto fail; 2004ab3059a8SMatt Macy zkva = addr; 2005ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 2006ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 2007ab3059a8SMatt Macy zkva += PAGE_SIZE; 2008ab3059a8SMatt Macy } 2009ab3059a8SMatt Macy return ((void*)addr); 2010ab3059a8SMatt Macy fail: 2011ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 201288ea538aSMark Johnston vm_page_unwire_noq(p); 2013ab3059a8SMatt Macy vm_page_free(p); 2014ab3059a8SMatt Macy } 2015ab3059a8SMatt Macy return (NULL); 2016ab3059a8SMatt Macy } 2017ab3059a8SMatt Macy 20188355f576SJeff Roberson /* 2019a9d6f1feSMark Johnston * Allocates a number of pages not belonging to a VM object 20208355f576SJeff Roberson * 20218355f576SJeff Roberson * Arguments: 20228355f576SJeff Roberson * bytes The number of bytes requested 20238355f576SJeff Roberson * wait Shall we wait? 20248355f576SJeff Roberson * 20258355f576SJeff Roberson * Returns: 20268355f576SJeff Roberson * A pointer to the alloced memory or possibly 20278355f576SJeff Roberson * NULL if M_NOWAIT is set. 20288355f576SJeff Roberson */ 20298355f576SJeff Roberson static void * 2030ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 2031ab3185d1SJeff Roberson int wait) 20328355f576SJeff Roberson { 2033a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 2034a4915c21SAttilio Rao u_long npages; 2035b245ac95SAlan Cox vm_offset_t retkva, zkva; 2036a4915c21SAttilio Rao vm_page_t p, p_next; 2037e20a199fSJeff Roberson uma_keg_t keg; 2038a4667e09SMark Johnston int req; 20398355f576SJeff Roberson 2040a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 2041bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 2042a4667e09SMark Johnston req = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED; 2043a4667e09SMark Johnston if ((wait & M_WAITOK) != 0) 2044a4667e09SMark Johnston req |= VM_ALLOC_WAITOK; 2045a4915c21SAttilio Rao 2046a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 2047a4915c21SAttilio Rao while (npages > 0) { 2048a4667e09SMark Johnston p = vm_page_alloc_noobj_domain(domain, req); 2049a4915c21SAttilio Rao if (p != NULL) { 2050a4915c21SAttilio Rao /* 2051a4915c21SAttilio Rao * Since the page does not belong to an object, its 2052a4915c21SAttilio Rao * listq is unused. 2053a4915c21SAttilio Rao */ 2054a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 2055a4915c21SAttilio Rao npages--; 2056a4915c21SAttilio Rao continue; 2057a4915c21SAttilio Rao } 20588355f576SJeff Roberson /* 2059a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 2060a4915c21SAttilio Rao * exit. 20618355f576SJeff Roberson */ 2062a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 206388ea538aSMark Johnston vm_page_unwire_noq(p); 2064b245ac95SAlan Cox vm_page_free(p); 2065b245ac95SAlan Cox } 2066a4915c21SAttilio Rao return (NULL); 2067b245ac95SAlan Cox } 20688355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 2069a4915c21SAttilio Rao zkva = keg->uk_kva + 2070a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 2071a4915c21SAttilio Rao retkva = zkva; 2072a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 2073a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 2074a4915c21SAttilio Rao zkva += PAGE_SIZE; 2075a4915c21SAttilio Rao } 20768355f576SJeff Roberson 20778355f576SJeff Roberson return ((void *)retkva); 20788355f576SJeff Roberson } 20798355f576SJeff Roberson 20808355f576SJeff Roberson /* 2081ec0d8280SRyan Libby * Allocate physically contiguous pages. 2082ec0d8280SRyan Libby */ 2083ec0d8280SRyan Libby static void * 2084ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 2085ec0d8280SRyan Libby int wait) 2086ec0d8280SRyan Libby { 2087ec0d8280SRyan Libby 2088ec0d8280SRyan Libby *pflag = UMA_SLAB_KERNEL; 2089ec0d8280SRyan Libby return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain), 2090ec0d8280SRyan Libby bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT)); 2091ec0d8280SRyan Libby } 2092ec0d8280SRyan Libby 2093ec0d8280SRyan Libby /* 20948355f576SJeff Roberson * Frees a number of pages to the system 20958355f576SJeff Roberson * 20968355f576SJeff Roberson * Arguments: 20978355f576SJeff Roberson * mem A pointer to the memory to be freed 20988355f576SJeff Roberson * size The size of the memory being freed 20998355f576SJeff Roberson * flags The original p->us_flags field 21008355f576SJeff Roberson * 21018355f576SJeff Roberson * Returns: 21028355f576SJeff Roberson * Nothing 21038355f576SJeff Roberson */ 21048355f576SJeff Roberson static void 2105f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 21068355f576SJeff Roberson { 21073370c5bfSJeff Roberson 2108a81c400eSJeff Roberson if ((flags & UMA_SLAB_BOOT) != 0) { 2109a81c400eSJeff Roberson startup_free(mem, size); 2110a81c400eSJeff Roberson return; 2111a81c400eSJeff Roberson } 2112a81c400eSJeff Roberson 2113ec0d8280SRyan Libby KASSERT((flags & UMA_SLAB_KERNEL) != 0, 2114ec0d8280SRyan Libby ("UMA: page_free used with invalid flags %x", flags)); 21158355f576SJeff Roberson 211649bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 21178355f576SJeff Roberson } 21188355f576SJeff Roberson 21198355f576SJeff Roberson /* 2120ab3059a8SMatt Macy * Frees pcpu zone allocations 2121ab3059a8SMatt Macy * 2122ab3059a8SMatt Macy * Arguments: 2123ab3059a8SMatt Macy * mem A pointer to the memory to be freed 2124ab3059a8SMatt Macy * size The size of the memory being freed 2125ab3059a8SMatt Macy * flags The original p->us_flags field 2126ab3059a8SMatt Macy * 2127ab3059a8SMatt Macy * Returns: 2128ab3059a8SMatt Macy * Nothing 2129ab3059a8SMatt Macy */ 2130ab3059a8SMatt Macy static void 2131ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 2132ab3059a8SMatt Macy { 2133ab3059a8SMatt Macy vm_offset_t sva, curva; 2134ab3059a8SMatt Macy vm_paddr_t paddr; 2135ab3059a8SMatt Macy vm_page_t m; 2136ab3059a8SMatt Macy 2137ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 21385ba16cf3SRyan Libby 21395ba16cf3SRyan Libby if ((flags & UMA_SLAB_BOOT) != 0) { 21405ba16cf3SRyan Libby startup_free(mem, size); 21415ba16cf3SRyan Libby return; 21425ba16cf3SRyan Libby } 21435ba16cf3SRyan Libby 2144ab3059a8SMatt Macy sva = (vm_offset_t)mem; 2145ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 2146ab3059a8SMatt Macy paddr = pmap_kextract(curva); 2147ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 214888ea538aSMark Johnston vm_page_unwire_noq(m); 2149ab3059a8SMatt Macy vm_page_free(m); 2150ab3059a8SMatt Macy } 2151ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 2152ab3059a8SMatt Macy kva_free(sva, size); 2153ab3059a8SMatt Macy } 2154ab3059a8SMatt Macy 2155ab3059a8SMatt Macy /* 21568355f576SJeff Roberson * Zero fill initializer 21578355f576SJeff Roberson * 21588355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 21598355f576SJeff Roberson */ 2160b23f72e9SBrian Feldman static int 2161b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 21628355f576SJeff Roberson { 21638355f576SJeff Roberson bzero(mem, size); 2164b23f72e9SBrian Feldman return (0); 21658355f576SJeff Roberson } 21668355f576SJeff Roberson 2167815db204SRyan Libby #ifdef INVARIANTS 216854007ce8SMark Johnston static struct noslabbits * 2169815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 2170815db204SRyan Libby { 2171815db204SRyan Libby 2172815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 2173815db204SRyan Libby } 2174815db204SRyan Libby #endif 2175815db204SRyan Libby 21768355f576SJeff Roberson /* 21779b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 21789b78b1f4SJeff Roberson */ 217954007ce8SMark Johnston static size_t 21809b78b1f4SJeff Roberson slab_sizeof(int nitems) 21819b78b1f4SJeff Roberson { 21829b78b1f4SJeff Roberson size_t s; 21839b78b1f4SJeff Roberson 2184815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 21859b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 21869b78b1f4SJeff Roberson } 21879b78b1f4SJeff Roberson 21884a8b575cSRyan Libby #define UMA_FIXPT_SHIFT 31 21894a8b575cSRyan Libby #define UMA_FRAC_FIXPT(n, d) \ 21904a8b575cSRyan Libby ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d))) 21914a8b575cSRyan Libby #define UMA_FIXPT_PCT(f) \ 21924a8b575cSRyan Libby ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT)) 21934a8b575cSRyan Libby #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100) 21944a8b575cSRyan Libby #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE) 21954a8b575cSRyan Libby 21969b78b1f4SJeff Roberson /* 21974a8b575cSRyan Libby * Compute the number of items that will fit in a slab. If hdr is true, the 21984a8b575cSRyan Libby * item count may be limited to provide space in the slab for an inline slab 21994a8b575cSRyan Libby * header. Otherwise, all slab space will be provided for item storage. 22004a8b575cSRyan Libby */ 22014a8b575cSRyan Libby static u_int 22024a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr) 22034a8b575cSRyan Libby { 22044a8b575cSRyan Libby u_int ipers; 22054a8b575cSRyan Libby u_int padpi; 22064a8b575cSRyan Libby 22074a8b575cSRyan Libby /* The padding between items is not needed after the last item. */ 22084a8b575cSRyan Libby padpi = rsize - size; 22094a8b575cSRyan Libby 22104a8b575cSRyan Libby if (hdr) { 22114a8b575cSRyan Libby /* 22124a8b575cSRyan Libby * Start with the maximum item count and remove items until 22134a8b575cSRyan Libby * the slab header first alongside the allocatable memory. 22144a8b575cSRyan Libby */ 22154a8b575cSRyan Libby for (ipers = MIN(SLAB_MAX_SETSIZE, 22164a8b575cSRyan Libby (slabsize + padpi - slab_sizeof(1)) / rsize); 22174a8b575cSRyan Libby ipers > 0 && 22184a8b575cSRyan Libby ipers * rsize - padpi + slab_sizeof(ipers) > slabsize; 22194a8b575cSRyan Libby ipers--) 22204a8b575cSRyan Libby continue; 22214a8b575cSRyan Libby } else { 22224a8b575cSRyan Libby ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE); 22234a8b575cSRyan Libby } 22244a8b575cSRyan Libby 22254a8b575cSRyan Libby return (ipers); 22264a8b575cSRyan Libby } 22274a8b575cSRyan Libby 222827ca37acSRyan Libby struct keg_layout_result { 222927ca37acSRyan Libby u_int format; 223027ca37acSRyan Libby u_int slabsize; 223127ca37acSRyan Libby u_int ipers; 223227ca37acSRyan Libby u_int eff; 223327ca37acSRyan Libby }; 223427ca37acSRyan Libby 223527ca37acSRyan Libby static void 223627ca37acSRyan Libby keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt, 223727ca37acSRyan Libby struct keg_layout_result *kl) 223827ca37acSRyan Libby { 223927ca37acSRyan Libby u_int total; 224027ca37acSRyan Libby 224127ca37acSRyan Libby kl->format = fmt; 224227ca37acSRyan Libby kl->slabsize = slabsize; 224327ca37acSRyan Libby 224427ca37acSRyan Libby /* Handle INTERNAL as inline with an extra page. */ 224527ca37acSRyan Libby if ((fmt & UMA_ZFLAG_INTERNAL) != 0) { 224627ca37acSRyan Libby kl->format &= ~UMA_ZFLAG_INTERNAL; 224727ca37acSRyan Libby kl->slabsize += PAGE_SIZE; 224827ca37acSRyan Libby } 224927ca37acSRyan Libby 225027ca37acSRyan Libby kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize, 225127ca37acSRyan Libby (fmt & UMA_ZFLAG_OFFPAGE) == 0); 225227ca37acSRyan Libby 225327ca37acSRyan Libby /* Account for memory used by an offpage slab header. */ 225427ca37acSRyan Libby total = kl->slabsize; 225527ca37acSRyan Libby if ((fmt & UMA_ZFLAG_OFFPAGE) != 0) 225627ca37acSRyan Libby total += slabzone(kl->ipers)->uz_keg->uk_rsize; 225727ca37acSRyan Libby 225827ca37acSRyan Libby kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total); 225927ca37acSRyan Libby } 226027ca37acSRyan Libby 22619b78b1f4SJeff Roberson /* 22624a8b575cSRyan Libby * Determine the format of a uma keg. This determines where the slab header 22634a8b575cSRyan Libby * will be placed (inline or offpage) and calculates ipers, rsize, and ppera. 22648355f576SJeff Roberson * 22658355f576SJeff Roberson * Arguments 2266e20a199fSJeff Roberson * keg The zone we should initialize 22678355f576SJeff Roberson * 22688355f576SJeff Roberson * Returns 22698355f576SJeff Roberson * Nothing 22708355f576SJeff Roberson */ 22718355f576SJeff Roberson static void 22724a8b575cSRyan Libby keg_layout(uma_keg_t keg) 22738355f576SJeff Roberson { 227427ca37acSRyan Libby struct keg_layout_result kl = {}, kl_tmp; 227527ca37acSRyan Libby u_int fmts[2]; 22764a8b575cSRyan Libby u_int alignsize; 227727ca37acSRyan Libby u_int nfmt; 22784a8b575cSRyan Libby u_int pages; 2279244f4554SBosko Milekic u_int rsize; 2280a55ebb7cSAndriy Gapon u_int slabsize; 228127ca37acSRyan Libby u_int i, j; 22828355f576SJeff Roberson 22834a8b575cSRyan Libby KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 22844a8b575cSRyan Libby (keg->uk_size <= UMA_PCPU_ALLOC_SIZE && 22854a8b575cSRyan Libby (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0), 22864a8b575cSRyan Libby ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b", 22874a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_size, keg->uk_flags, 22884a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 2289bae55c4aSRyan Libby KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 || 22904a8b575cSRyan Libby (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0, 22914a8b575cSRyan Libby ("%s: incompatible flags 0x%b", __func__, keg->uk_flags, 22924a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 2293e28a647dSGleb Smirnoff 22944a8b575cSRyan Libby alignsize = keg->uk_align + 1; 2295b0dfc486SMark Johnston #ifdef KASAN 2296b0dfc486SMark Johnston /* 2297b0dfc486SMark Johnston * ASAN requires that each allocation be aligned to the shadow map 2298b0dfc486SMark Johnston * scale factor. 2299b0dfc486SMark Johnston */ 2300b0dfc486SMark Johnston if (alignsize < KASAN_SHADOW_SCALE) 2301b0dfc486SMark Johnston alignsize = KASAN_SHADOW_SCALE; 2302b0dfc486SMark Johnston #endif 2303ad97af7eSGleb Smirnoff 2304ef72505eSJeff Roberson /* 2305ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 2306ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 2307ef72505eSJeff Roberson * allocation bits for we round it up. 2308ef72505eSJeff Roberson */ 23099b8db4d0SRyan Libby rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT); 23104a8b575cSRyan Libby rsize = roundup2(rsize, alignsize); 2311ad97af7eSGleb Smirnoff 231227ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) { 23139b78b1f4SJeff Roberson /* 23144a8b575cSRyan Libby * We want one item to start on every align boundary in a page. 23154a8b575cSRyan Libby * To do this we will span pages. We will also extend the item 23164a8b575cSRyan Libby * by the size of align if it is an even multiple of align. 23174a8b575cSRyan Libby * Otherwise, it would fall on the same boundary every time. 23189b78b1f4SJeff Roberson */ 23194a8b575cSRyan Libby if ((rsize & alignsize) == 0) 23204a8b575cSRyan Libby rsize += alignsize; 23214a8b575cSRyan Libby slabsize = rsize * (PAGE_SIZE / alignsize); 23224a8b575cSRyan Libby slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE); 23234a8b575cSRyan Libby slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE); 232427ca37acSRyan Libby slabsize = round_page(slabsize); 23254a8b575cSRyan Libby } else { 23264a8b575cSRyan Libby /* 232727ca37acSRyan Libby * Start with a slab size of as many pages as it takes to 232827ca37acSRyan Libby * represent a single item. We will try to fit as many 232927ca37acSRyan Libby * additional items into the slab as possible. 23304a8b575cSRyan Libby */ 233127ca37acSRyan Libby slabsize = round_page(keg->uk_size); 23321ca6ed45SGleb Smirnoff } 2333ad97af7eSGleb Smirnoff 233427ca37acSRyan Libby /* Build a list of all of the available formats for this keg. */ 233527ca37acSRyan Libby nfmt = 0; 233627ca37acSRyan Libby 23374a8b575cSRyan Libby /* Evaluate an inline slab layout. */ 23384a8b575cSRyan Libby if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0) 233927ca37acSRyan Libby fmts[nfmt++] = 0; 23404a8b575cSRyan Libby 23414a8b575cSRyan Libby /* TODO: vm_page-embedded slab. */ 2342244f4554SBosko Milekic 234320e8e865SBosko Milekic /* 2344244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 234520e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 2346bae55c4aSRyan Libby * may end up going to the VM for slabs which we do not want 2347bae55c4aSRyan Libby * to do if we're UMA_ZONE_VM, which clearly forbids it. 2348bae55c4aSRyan Libby * In those cases, evaluate a pseudo-format called INTERNAL 2349bae55c4aSRyan Libby * which has an inline slab header and one extra page to 2350bae55c4aSRyan Libby * guarantee that it fits. 235127ca37acSRyan Libby * 235227ca37acSRyan Libby * Otherwise, see if using an OFFPAGE slab will improve our 235327ca37acSRyan Libby * efficiency. 235420e8e865SBosko Milekic */ 2355bae55c4aSRyan Libby if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0) 235627ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_INTERNAL; 235727ca37acSRyan Libby else 235827ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_OFFPAGE; 2359244f4554SBosko Milekic 2360ef72505eSJeff Roberson /* 236127ca37acSRyan Libby * Choose a slab size and format which satisfy the minimum efficiency. 236227ca37acSRyan Libby * Prefer the smallest slab size that meets the constraints. 2363ef72505eSJeff Roberson * 236427ca37acSRyan Libby * Start with a minimum slab size, to accommodate CACHESPREAD. Then, 236527ca37acSRyan Libby * for small items (up to PAGE_SIZE), the iteration increment is one 236627ca37acSRyan Libby * page; and for large items, the increment is one item. 2367ef72505eSJeff Roberson */ 236827ca37acSRyan Libby i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize); 236927ca37acSRyan Libby KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u", 237027ca37acSRyan Libby keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize, 237127ca37acSRyan Libby rsize, i)); 237227ca37acSRyan Libby for ( ; ; i++) { 237327ca37acSRyan Libby slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) : 237427ca37acSRyan Libby round_page(rsize * (i - 1) + keg->uk_size); 237527ca37acSRyan Libby 237627ca37acSRyan Libby for (j = 0; j < nfmt; j++) { 237727ca37acSRyan Libby /* Only if we have no viable format yet. */ 237827ca37acSRyan Libby if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 && 237927ca37acSRyan Libby kl.ipers > 0) 238027ca37acSRyan Libby continue; 238127ca37acSRyan Libby 238227ca37acSRyan Libby keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp); 238327ca37acSRyan Libby if (kl_tmp.eff <= kl.eff) 238427ca37acSRyan Libby continue; 238527ca37acSRyan Libby 238627ca37acSRyan Libby kl = kl_tmp; 238727ca37acSRyan Libby 238827ca37acSRyan Libby CTR6(KTR_UMA, "keg %s layout: format %#x " 238927ca37acSRyan Libby "(ipers %u * rsize %u) / slabsize %#x = %u%% eff", 239027ca37acSRyan Libby keg->uk_name, kl.format, kl.ipers, rsize, 239127ca37acSRyan Libby kl.slabsize, UMA_FIXPT_PCT(kl.eff)); 239227ca37acSRyan Libby 239327ca37acSRyan Libby /* Stop when we reach the minimum efficiency. */ 239427ca37acSRyan Libby if (kl.eff >= UMA_MIN_EFF) 239527ca37acSRyan Libby break; 23968355f576SJeff Roberson } 2397ad97af7eSGleb Smirnoff 239833e5a1eaSRyan Libby if (kl.eff >= UMA_MIN_EFF || !multipage_slabs || 239927ca37acSRyan Libby slabsize >= SLAB_MAX_SETSIZE * rsize || 240027ca37acSRyan Libby (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0) 240127ca37acSRyan Libby break; 240227ca37acSRyan Libby } 240327ca37acSRyan Libby 240427ca37acSRyan Libby pages = atop(kl.slabsize); 240527ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 240627ca37acSRyan Libby pages *= mp_maxid + 1; 240727ca37acSRyan Libby 240827ca37acSRyan Libby keg->uk_rsize = rsize; 240927ca37acSRyan Libby keg->uk_ipers = kl.ipers; 241027ca37acSRyan Libby keg->uk_ppera = pages; 241127ca37acSRyan Libby keg->uk_flags |= kl.format; 241227ca37acSRyan Libby 24134a8b575cSRyan Libby /* 24144a8b575cSRyan Libby * How do we find the slab header if it is offpage or if not all item 24154a8b575cSRyan Libby * start addresses are in the same page? We could solve the latter 24164a8b575cSRyan Libby * case with vaddr alignment, but we don't. 24174a8b575cSRyan Libby */ 241827ca37acSRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 || 241927ca37acSRyan Libby (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) { 242054c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0) 242127ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_HASH; 242254c5ae80SRyan Libby else 242327ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 242454c5ae80SRyan Libby } 242527ca37acSRyan Libby 2426e63a1c2fSRyan Libby CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u", 242727ca37acSRyan Libby __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers, 242827ca37acSRyan Libby pages); 24294a8b575cSRyan Libby KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 24304a8b575cSRyan Libby ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__, 243127ca37acSRyan Libby keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize, 243227ca37acSRyan Libby keg->uk_ipers, pages)); 2433e20a199fSJeff Roberson } 2434e20a199fSJeff Roberson 24358355f576SJeff Roberson /* 2436099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 2437099a0e58SBosko Milekic * the keg onto the global keg list. 24388355f576SJeff Roberson * 24398355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 2440099a0e58SBosko Milekic * udata Actually uma_kctor_args 2441099a0e58SBosko Milekic */ 2442b23f72e9SBrian Feldman static int 2443b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 2444099a0e58SBosko Milekic { 2445099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 2446099a0e58SBosko Milekic uma_keg_t keg = mem; 2447099a0e58SBosko Milekic uma_zone_t zone; 24488b987a77SJeff Roberson int i; 2449099a0e58SBosko Milekic 2450099a0e58SBosko Milekic bzero(keg, size); 2451099a0e58SBosko Milekic keg->uk_size = arg->size; 2452099a0e58SBosko Milekic keg->uk_init = arg->uminit; 2453099a0e58SBosko Milekic keg->uk_fini = arg->fini; 2454099a0e58SBosko Milekic keg->uk_align = arg->align; 24556fd34d6fSJeff Roberson keg->uk_reserve = 0; 2456099a0e58SBosko Milekic keg->uk_flags = arg->flags; 2457099a0e58SBosko Milekic 2458099a0e58SBosko Milekic /* 2459194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 2460dfe13344SJeff Roberson * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which 2461dfe13344SJeff Roberson * case the iterator is never run. 2462194a979eSMark Johnston */ 2463194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 2464194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 2465194a979eSMark Johnston 2466194a979eSMark Johnston /* 2467c8b0a88bSJeff Roberson * The primary zone is passed to us at keg-creation time. 2468099a0e58SBosko Milekic */ 2469099a0e58SBosko Milekic zone = arg->zone; 2470e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 2471099a0e58SBosko Milekic 2472099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 2473099a0e58SBosko Milekic keg->uk_init = zero_init; 2474099a0e58SBosko Milekic 2475cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 247654c5ae80SRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 2477e20a199fSJeff Roberson 247854c5ae80SRyan Libby #ifndef SMP 2479ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 2480ad97af7eSGleb Smirnoff #endif 2481ad97af7eSGleb Smirnoff 24824a8b575cSRyan Libby keg_layout(keg); 2483099a0e58SBosko Milekic 24848b987a77SJeff Roberson /* 2485c6fd3e23SJeff Roberson * Use a first-touch NUMA policy for kegs that pmap_extract() will 2486c6fd3e23SJeff Roberson * work on. Use round-robin for everything else. 2487dfe13344SJeff Roberson * 2488dfe13344SJeff Roberson * Zones may override the default by specifying either. 24898b987a77SJeff Roberson */ 2490dfe13344SJeff Roberson #ifdef NUMA 2491dfe13344SJeff Roberson if ((keg->uk_flags & 2492c6fd3e23SJeff Roberson (UMA_ZONE_ROUNDROBIN | UMA_ZFLAG_CACHE | UMA_ZONE_NOTPAGE)) == 0) 2493dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_FIRSTTOUCH; 2494dfe13344SJeff Roberson else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2495dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_ROUNDROBIN; 24968b987a77SJeff Roberson #endif 24978b987a77SJeff Roberson 2498099a0e58SBosko Milekic /* 2499099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 2500099a0e58SBosko Milekic * startup cache until the vm is ready. 2501099a0e58SBosko Milekic */ 250277e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2503a81c400eSJeff Roberson if (keg->uk_ppera == 1) 250477e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 2505a81c400eSJeff Roberson else 25068cd02d00SAlan Cox #endif 2507a81c400eSJeff Roberson if (booted < BOOT_KVA) 2508a81c400eSJeff Roberson keg->uk_allocf = startup_alloc; 2509ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 2510ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 2511ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1) 2512ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 251377e19437SGleb Smirnoff else 251477e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 251577e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 251677e19437SGleb Smirnoff if (keg->uk_ppera == 1) 251777e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 251877e19437SGleb Smirnoff else 251977e19437SGleb Smirnoff #endif 2520ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 2521ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 2522ab3059a8SMatt Macy else 252377e19437SGleb Smirnoff keg->uk_freef = page_free; 2524099a0e58SBosko Milekic 2525099a0e58SBosko Milekic /* 25268b987a77SJeff Roberson * Initialize keg's locks. 2527099a0e58SBosko Milekic */ 25288b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 25298b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 2530099a0e58SBosko Milekic 2531099a0e58SBosko Milekic /* 2532099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 25339b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 25349b78b1f4SJeff Roberson * definition. 2535099a0e58SBosko Milekic */ 253654c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) { 25379b78b1f4SJeff Roberson size_t shsize; 25389b78b1f4SJeff Roberson 25399b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 25409b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 2541244f4554SBosko Milekic /* 2542244f4554SBosko Milekic * The only way the following is possible is if with our 2543244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 2544244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 2545244f4554SBosko Milekic * mathematically possible for all cases, so we make 2546244f4554SBosko Milekic * sure here anyway. 2547244f4554SBosko Milekic */ 25489b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 25493d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 25503d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 2551099a0e58SBosko Milekic } 2552099a0e58SBosko Milekic 255354c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 25543b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 2555099a0e58SBosko Milekic 2556e63a1c2fSRyan Libby CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone); 2557099a0e58SBosko Milekic 2558099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 2559099a0e58SBosko Milekic 2560111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2561099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2562111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2563b23f72e9SBrian Feldman return (0); 2564099a0e58SBosko Milekic } 2565099a0e58SBosko Milekic 25662efcc8cbSGleb Smirnoff static void 2567a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused) 2568a81c400eSJeff Roberson { 2569a81c400eSJeff Roberson uma_keg_t keg; 2570a81c400eSJeff Roberson 2571a81c400eSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 2572a81c400eSJeff Roberson return; 2573a81c400eSJeff Roberson KEG_GET(zone, keg); 2574ec0d8280SRyan Libby 2575ec0d8280SRyan Libby if (keg->uk_allocf == startup_alloc) { 2576ec0d8280SRyan Libby /* Switch to the real allocator. */ 2577f96d4157SJeff Roberson if (keg->uk_flags & UMA_ZONE_PCPU) 2578f96d4157SJeff Roberson keg->uk_allocf = pcpu_page_alloc; 2579ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && 2580ec0d8280SRyan Libby keg->uk_ppera > 1) 2581ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 2582ec0d8280SRyan Libby else 2583a81c400eSJeff Roberson keg->uk_allocf = page_alloc; 2584a81c400eSJeff Roberson } 2585ec0d8280SRyan Libby } 2586a81c400eSJeff Roberson 2587a81c400eSJeff Roberson static void 258820a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 25892efcc8cbSGleb Smirnoff { 25902efcc8cbSGleb Smirnoff 25912efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 25922efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 25932efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 2594c6fd3e23SJeff Roberson zone->uz_xdomain = counter_u64_alloc(M_WAITOK); 25952efcc8cbSGleb Smirnoff } 25962efcc8cbSGleb Smirnoff 259720a4e154SJeff Roberson static void 259820a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 259920a4e154SJeff Roberson { 260020a4e154SJeff Roberson uma_zone_domain_t zdom; 26018b987a77SJeff Roberson uma_domain_t dom; 260220a4e154SJeff Roberson uma_keg_t keg; 260320a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 26043b490537SJeff Roberson int domains, i, cnt; 260520a4e154SJeff Roberson static const char *nokeg = "cache zone"; 260620a4e154SJeff Roberson char *c; 260720a4e154SJeff Roberson 260820a4e154SJeff Roberson /* 260920a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 261020a4e154SJeff Roberson * any special characters and handling dups by appending 261120a4e154SJeff Roberson * an index. 261220a4e154SJeff Roberson */ 261320a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 26143b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 26153b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 26163b490537SJeff Roberson cnt /= 10; 26173b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 26183b490537SJeff Roberson M_UMA, M_WAITOK); 261920a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 262020a4e154SJeff Roberson zone->uz_namecnt); 262120a4e154SJeff Roberson } else 262220a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 262320a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 262420a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 262520a4e154SJeff Roberson *c = '_'; 262620a4e154SJeff Roberson 262720a4e154SJeff Roberson /* 262820a4e154SJeff Roberson * Basic parameters at the root. 262920a4e154SJeff Roberson */ 263020a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 26317029da5cSPawel Biernacki OID_AUTO, zone->uz_ctlname, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 263220a4e154SJeff Roberson oid = zone->uz_oid; 263320a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 263420a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 26356d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 26366d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 26376d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 263820a4e154SJeff Roberson "Allocator configuration flags"); 263920a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 264020a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 264120a4e154SJeff Roberson "Desired per-cpu cache size"); 264220a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 264320a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 264420a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 264520a4e154SJeff Roberson 264620a4e154SJeff Roberson /* 264720a4e154SJeff Roberson * keg if present. 264820a4e154SJeff Roberson */ 264954c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 26508b987a77SJeff Roberson domains = vm_ndomains; 26518b987a77SJeff Roberson else 26528b987a77SJeff Roberson domains = 1; 265320a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 26547029da5cSPawel Biernacki "keg", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 265520a4e154SJeff Roberson keg = zone->uz_keg; 26563b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 265720a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 265820a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 265920a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 266020a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 266120a4e154SJeff Roberson "Real object size with alignment"); 266220a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 266320a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 266420a4e154SJeff Roberson "pages per-slab allocation"); 266520a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 266620a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 266720a4e154SJeff Roberson "items available per-slab"); 266820a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 266920a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 267020a4e154SJeff Roberson "item alignment mask"); 2671f09cbea3SMark Johnston SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2672f09cbea3SMark Johnston "reserve", CTLFLAG_RD, &keg->uk_reserve, 0, 2673f09cbea3SMark Johnston "number of reserved items"); 2674f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2675f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2676f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2677f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 26788b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 26797029da5cSPawel Biernacki OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 26808b987a77SJeff Roberson for (i = 0; i < domains; i++) { 26818b987a77SJeff Roberson dom = &keg->uk_domain[i]; 26828b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 26837029da5cSPawel Biernacki OID_AUTO, VM_DOMAIN(i)->vmd_name, 26847029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 26858b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 26868b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 26878b987a77SJeff Roberson "Total pages currently allocated from VM"); 26888b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 26894ab3aee8SMark Johnston "free_items", CTLFLAG_RD, &dom->ud_free_items, 0, 2690d6e77cdaSMark Johnston "Items free in the slab layer"); 2691d6e77cdaSMark Johnston SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2692d6e77cdaSMark Johnston "free_slabs", CTLFLAG_RD, &dom->ud_free_slabs, 0, 2693d6e77cdaSMark Johnston "Unused slabs"); 26948b987a77SJeff Roberson } 269520a4e154SJeff Roberson } else 269620a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 269720a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 269820a4e154SJeff Roberson 269920a4e154SJeff Roberson /* 270020a4e154SJeff Roberson * Information about zone limits. 270120a4e154SJeff Roberson */ 270220a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 27037029da5cSPawel Biernacki "limit", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 27044bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 27054bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 27064bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 2707e574d407SMark Johnston "Current number of allocated items if limit is set"); 270820a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 270920a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 2710e574d407SMark Johnston "Maximum number of allocated and cached items"); 271120a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 271220a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 271320a4e154SJeff Roberson "Number of threads sleeping at limit"); 271420a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 271520a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 271620a4e154SJeff Roberson "Total zone limit sleeps"); 27174bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2718c6fd3e23SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bucket_max, 0, 2719c6fd3e23SJeff Roberson "Maximum number of items in each domain's bucket cache"); 272020a4e154SJeff Roberson 272120a4e154SJeff Roberson /* 27228b987a77SJeff Roberson * Per-domain zone information. 272320a4e154SJeff Roberson */ 272420a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 27257029da5cSPawel Biernacki OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 272620a4e154SJeff Roberson for (i = 0; i < domains; i++) { 2727c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 272820a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 27297029da5cSPawel Biernacki OID_AUTO, VM_DOMAIN(i)->vmd_name, 27307029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 273120a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 273220a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 273320a4e154SJeff Roberson "number of items in this domain"); 273420a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 273520a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 273620a4e154SJeff Roberson "maximum item count in this period"); 273720a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 273820a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 273920a4e154SJeff Roberson "minimum item count in this period"); 274020a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 27412760658bSAlexander Motin "bimin", CTLFLAG_RD, &zdom->uzd_bimin, 27422760658bSAlexander Motin "Minimum item count in this batch"); 27432760658bSAlexander Motin SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 274420a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 274520a4e154SJeff Roberson "Working set size"); 27462760658bSAlexander Motin SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 27472760658bSAlexander Motin "limin", CTLFLAG_RD, &zdom->uzd_limin, 27482760658bSAlexander Motin "Long time minimum item count"); 27492760658bSAlexander Motin SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 27502760658bSAlexander Motin "timin", CTLFLAG_RD, &zdom->uzd_timin, 0, 27512760658bSAlexander Motin "Time since zero long time minimum item count"); 275220a4e154SJeff Roberson } 275320a4e154SJeff Roberson 275420a4e154SJeff Roberson /* 275520a4e154SJeff Roberson * General statistics. 275620a4e154SJeff Roberson */ 275720a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 27587029da5cSPawel Biernacki "stats", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 275920a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 276020a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 276120a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 276220a4e154SJeff Roberson "Current number of allocated items"); 276320a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 276420a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 276520a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 276620a4e154SJeff Roberson "Total allocation calls"); 276720a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 276820a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 276920a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 277020a4e154SJeff Roberson "Total free calls"); 277120a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 277220a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 277320a4e154SJeff Roberson "Number of allocation failures"); 2774c6fd3e23SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2775c6fd3e23SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 277620a4e154SJeff Roberson "Free calls from the wrong domain"); 277720a4e154SJeff Roberson } 277820a4e154SJeff Roberson 277920a4e154SJeff Roberson struct uma_zone_count { 278020a4e154SJeff Roberson const char *name; 278120a4e154SJeff Roberson int count; 278220a4e154SJeff Roberson }; 278320a4e154SJeff Roberson 278420a4e154SJeff Roberson static void 278520a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 278620a4e154SJeff Roberson { 278720a4e154SJeff Roberson struct uma_zone_count *cnt; 278820a4e154SJeff Roberson 278920a4e154SJeff Roberson cnt = arg; 27903b490537SJeff Roberson /* 27913b490537SJeff Roberson * Some zones are rapidly created with identical names and 27923b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 27933b490537SJeff Roberson * Use one greater than the maximum observed for this name. 27943b490537SJeff Roberson */ 279520a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 27963b490537SJeff Roberson cnt->count = MAX(cnt->count, 27973b490537SJeff Roberson zone->uz_namecnt + 1); 279820a4e154SJeff Roberson } 279920a4e154SJeff Roberson 2800cc7ce83aSJeff Roberson static void 2801cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2802cc7ce83aSJeff Roberson { 2803cc7ce83aSJeff Roberson int i; 2804cc7ce83aSJeff Roberson 2805cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2806cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2807cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2808cc7ce83aSJeff Roberson } 2809cc7ce83aSJeff Roberson } 2810cc7ce83aSJeff Roberson 2811099a0e58SBosko Milekic /* 2812099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2813099a0e58SBosko Milekic * 2814099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2815099a0e58SBosko Milekic * udata Actually uma_zctor_args 28168355f576SJeff Roberson */ 2817b23f72e9SBrian Feldman static int 2818b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 28198355f576SJeff Roberson { 282020a4e154SJeff Roberson struct uma_zone_count cnt; 28218355f576SJeff Roberson struct uma_zctor_args *arg = udata; 2822c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 28238355f576SJeff Roberson uma_zone_t zone = mem; 2824099a0e58SBosko Milekic uma_zone_t z; 2825099a0e58SBosko Milekic uma_keg_t keg; 282608cfa56eSMark Johnston int i; 28278355f576SJeff Roberson 28288355f576SJeff Roberson bzero(zone, size); 28298355f576SJeff Roberson zone->uz_name = arg->name; 28308355f576SJeff Roberson zone->uz_ctor = arg->ctor; 28318355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2832099a0e58SBosko Milekic zone->uz_init = NULL; 2833099a0e58SBosko Milekic zone->uz_fini = NULL; 2834bf965959SSean Bruno zone->uz_sleeps = 0; 283520a4e154SJeff Roberson zone->uz_bucket_size = 0; 283620a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 283720a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2838d4665eaaSJeff Roberson zone->uz_flags = (arg->flags & UMA_ZONE_SMR); 28392f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2840ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 2841c6fd3e23SJeff Roberson zone->uz_bucket_max = ULONG_MAX; 28422f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2843af526374SJeff Roberson 284420a4e154SJeff Roberson /* Count the number of duplicate names. */ 284520a4e154SJeff Roberson cnt.name = arg->name; 284620a4e154SJeff Roberson cnt.count = 0; 284720a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 284820a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 284991d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 28502efcc8cbSGleb Smirnoff 2851c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 2852c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 2853c6fd3e23SJeff Roberson ZDOM_LOCK_INIT(zone, zdom, (arg->flags & UMA_ZONE_MTXCLASS)); 2854c6fd3e23SJeff Roberson STAILQ_INIT(&zdom->uzd_buckets); 2855c6fd3e23SJeff Roberson } 285608cfa56eSMark Johnston 285710094910SMark Johnston #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN) 2858ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2859cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 286009c8cb71SMark Johnston #elif defined(KASAN) 286109c8cb71SMark Johnston if ((arg->flags & (UMA_ZONE_NOFREE | UMA_ZFLAG_CACHE)) != 0) 286209c8cb71SMark Johnston arg->flags |= UMA_ZONE_NOKASAN; 2863ca293436SRyan Libby #endif 2864ca293436SRyan Libby 28650095a784SJeff Roberson /* 28660095a784SJeff Roberson * This is a pure cache zone, no kegs. 28670095a784SJeff Roberson */ 28680095a784SJeff Roberson if (arg->import) { 2869727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2870727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 28716fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2872af526374SJeff Roberson zone->uz_size = arg->size; 28730095a784SJeff Roberson zone->uz_import = arg->import; 28740095a784SJeff Roberson zone->uz_release = arg->release; 28750095a784SJeff Roberson zone->uz_arg = arg->arg; 2876c6fd3e23SJeff Roberson #ifdef NUMA 2877c6fd3e23SJeff Roberson /* 2878c6fd3e23SJeff Roberson * Cache zones are round-robin unless a policy is 2879c6fd3e23SJeff Roberson * specified because they may have incompatible 2880c6fd3e23SJeff Roberson * constraints. 2881c6fd3e23SJeff Roberson */ 2882c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2883c6fd3e23SJeff Roberson zone->uz_flags |= UMA_ZONE_ROUNDROBIN; 2884c6fd3e23SJeff Roberson #endif 2885111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 288603175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2887111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2888af526374SJeff Roberson goto out; 28890095a784SJeff Roberson } 28900095a784SJeff Roberson 28910095a784SJeff Roberson /* 28920095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 28930095a784SJeff Roberson */ 2894b75c4efcSAndrew Turner zone->uz_import = zone_import; 2895b75c4efcSAndrew Turner zone->uz_release = zone_release; 28960095a784SJeff Roberson zone->uz_arg = zone; 2897bb15d1c7SGleb Smirnoff keg = arg->keg; 28980095a784SJeff Roberson 2899099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 290020a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 290120a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2902099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 29038355f576SJeff Roberson zone->uz_init = arg->uminit; 2904e221e841SJeff Roberson zone->uz_fini = arg->fini; 2905e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2906111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2907099a0e58SBosko Milekic ZONE_LOCK(zone); 2908099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2909099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2910099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2911099a0e58SBosko Milekic break; 2912099a0e58SBosko Milekic } 2913099a0e58SBosko Milekic } 2914099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2915111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2916e20a199fSJeff Roberson } else if (keg == NULL) { 2917e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2918e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2919b23f72e9SBrian Feldman return (ENOMEM); 2920099a0e58SBosko Milekic } else { 2921099a0e58SBosko Milekic struct uma_kctor_args karg; 2922b23f72e9SBrian Feldman int error; 2923099a0e58SBosko Milekic 2924099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2925099a0e58SBosko Milekic karg.size = arg->size; 2926099a0e58SBosko Milekic karg.uminit = arg->uminit; 2927099a0e58SBosko Milekic karg.fini = arg->fini; 2928099a0e58SBosko Milekic karg.align = arg->align; 2929d4665eaaSJeff Roberson karg.flags = (arg->flags & ~UMA_ZONE_SMR); 2930099a0e58SBosko Milekic karg.zone = zone; 2931b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2932b23f72e9SBrian Feldman flags); 2933b23f72e9SBrian Feldman if (error) 2934b23f72e9SBrian Feldman return (error); 2935099a0e58SBosko Milekic } 29360095a784SJeff Roberson 293720a4e154SJeff Roberson /* Inherit properties from the keg. */ 2938bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2939e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2940e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2941e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 29428355f576SJeff Roberson 294320a4e154SJeff Roberson out: 2944dc2b3205SMark Johnston if (booted >= BOOT_PCPU) { 294520a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 2946dc2b3205SMark Johnston if (booted >= BOOT_RUNNING) 294720a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 294820a4e154SJeff Roberson } else { 294920a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 295020a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 295120a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2952099a0e58SBosko Milekic } 29538355f576SJeff Roberson 2954d4665eaaSJeff Roberson /* Caller requests a private SMR context. */ 2955d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 2956226dd6dbSJeff Roberson zone->uz_smr = smr_create(zone->uz_name, 0, 0); 2957d4665eaaSJeff Roberson 29587e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 29597e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 29607e28037aSMark Johnston ("Invalid zone flag combination")); 296120a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 296220a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 296320a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 296420a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 296520a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 296620a4e154SJeff Roberson zone->uz_bucket_size = 0; 29677e28037aSMark Johnston else 296820a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 296920a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2970cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2971cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2972cc7ce83aSJeff Roberson zone_update_caches(zone); 2973fc03d22bSJeff Roberson 2974b23f72e9SBrian Feldman return (0); 29758355f576SJeff Roberson } 29768355f576SJeff Roberson 29778355f576SJeff Roberson /* 2978099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2979099a0e58SBosko Milekic * table and removes the keg from the global list. 29809c2cd7e5SJeff Roberson * 29819c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 29829c2cd7e5SJeff Roberson * udata unused 29839c2cd7e5SJeff Roberson */ 2984099a0e58SBosko Milekic static void 2985099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2986099a0e58SBosko Milekic { 2987099a0e58SBosko Milekic uma_keg_t keg; 29888b987a77SJeff Roberson uint32_t free, pages; 29898b987a77SJeff Roberson int i; 29909c2cd7e5SJeff Roberson 2991099a0e58SBosko Milekic keg = (uma_keg_t)arg; 29928b987a77SJeff Roberson free = pages = 0; 29938b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 29944ab3aee8SMark Johnston free += keg->uk_domain[i].ud_free_items; 29958b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 29968b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2997099a0e58SBosko Milekic } 29987e240677SRyan Libby if (pages != 0) 29998b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 30008b987a77SJeff Roberson " Lost %u pages of memory.\n", 30018b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 30027e240677SRyan Libby pages / keg->uk_ppera * keg->uk_ipers - free, pages); 3003099a0e58SBosko Milekic 3004099a0e58SBosko Milekic hash_free(&keg->uk_hash); 3005099a0e58SBosko Milekic } 3006099a0e58SBosko Milekic 3007099a0e58SBosko Milekic /* 3008099a0e58SBosko Milekic * Zone header dtor. 3009099a0e58SBosko Milekic * 3010099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 3011099a0e58SBosko Milekic * udata unused 3012099a0e58SBosko Milekic */ 30139c2cd7e5SJeff Roberson static void 30149c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 30159c2cd7e5SJeff Roberson { 30169c2cd7e5SJeff Roberson uma_zone_t zone; 3017099a0e58SBosko Milekic uma_keg_t keg; 3018c6fd3e23SJeff Roberson int i; 30199c2cd7e5SJeff Roberson 30209c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 30219643769aSJeff Roberson 302220a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 302320a4e154SJeff Roberson 3024e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 30259643769aSJeff Roberson cache_drain(zone); 3026099a0e58SBosko Milekic 3027111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 3028099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 3029111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 30307b516613SJonathan T. Looney if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 30317b516613SJonathan T. Looney keg = zone->uz_keg; 30327b516613SJonathan T. Looney keg->uk_reserve = 0; 30337b516613SJonathan T. Looney } 3034aabe13f1SMark Johnston zone_reclaim(zone, UMA_ANYDOMAIN, M_WAITOK, true); 3035c6fd3e23SJeff Roberson 3036e20a199fSJeff Roberson /* 3037323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 3038e20a199fSJeff Roberson */ 3039323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 3040323ad386STycho Nightingale keg = zone->uz_keg; 3041111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 3042099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 3043111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 30440095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 30459c2cd7e5SJeff Roberson } 30462efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 30472efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 30482efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 3049c6fd3e23SJeff Roberson counter_u64_free(zone->uz_xdomain); 305020a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 3051c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) 3052c6fd3e23SJeff Roberson ZDOM_LOCK_FINI(ZDOM_GET(zone, i)); 305391d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 3054099a0e58SBosko Milekic } 3055099a0e58SBosko Milekic 3056a81c400eSJeff Roberson static void 3057a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg) 3058a81c400eSJeff Roberson { 3059a81c400eSJeff Roberson uma_keg_t keg; 3060a81c400eSJeff Roberson uma_zone_t zone; 3061a81c400eSJeff Roberson 3062a81c400eSJeff Roberson LIST_FOREACH(keg, &uma_kegs, uk_link) { 3063a81c400eSJeff Roberson LIST_FOREACH(zone, &keg->uk_zones, uz_link) 3064a81c400eSJeff Roberson zfunc(zone, arg); 3065a81c400eSJeff Roberson } 3066a81c400eSJeff Roberson LIST_FOREACH(zone, &uma_cachezones, uz_link) 3067a81c400eSJeff Roberson zfunc(zone, arg); 3068a81c400eSJeff Roberson } 3069a81c400eSJeff Roberson 30709c2cd7e5SJeff Roberson /* 30718355f576SJeff Roberson * Traverses every zone in the system and calls a callback 30728355f576SJeff Roberson * 30738355f576SJeff Roberson * Arguments: 30748355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 30758355f576SJeff Roberson * as an argument. 30768355f576SJeff Roberson * 30778355f576SJeff Roberson * Returns: 30788355f576SJeff Roberson * Nothing 30798355f576SJeff Roberson */ 30808355f576SJeff Roberson static void 308120a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 30828355f576SJeff Roberson { 30838355f576SJeff Roberson 3084111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 3085a81c400eSJeff Roberson zone_foreach_unlocked(zfunc, arg); 3086111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 30878355f576SJeff Roberson } 30888355f576SJeff Roberson 3089f4bef67cSGleb Smirnoff /* 3090a81c400eSJeff Roberson * Initialize the kernel memory allocator. This is done after pages can be 3091a81c400eSJeff Roberson * allocated but before general KVA is available. 3092f4bef67cSGleb Smirnoff */ 3093a81c400eSJeff Roberson void 3094a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail) 3095f4bef67cSGleb Smirnoff { 3096a81c400eSJeff Roberson struct uma_zctor_args args; 3097a81c400eSJeff Roberson size_t ksize, zsize, size; 3098c8b0a88bSJeff Roberson uma_keg_t primarykeg; 3099a81c400eSJeff Roberson uintptr_t m; 310081302f1dSMark Johnston int domain; 3101a81c400eSJeff Roberson uint8_t pflag; 3102a81c400eSJeff Roberson 3103a81c400eSJeff Roberson bootstart = bootmem = virtual_avail; 3104a81c400eSJeff Roberson 3105a81c400eSJeff Roberson rw_init(&uma_rwlock, "UMA lock"); 3106a81c400eSJeff Roberson sx_init(&uma_reclaim_lock, "umareclaim"); 3107f4bef67cSGleb Smirnoff 3108f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 3109f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 311079c9f942SJeff Roberson ksize = roundup(ksize, UMA_SUPER_ALIGN); 3111f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 3112f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 3113f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 311479c9f942SJeff Roberson zsize = roundup(zsize, UMA_SUPER_ALIGN); 3115f4bef67cSGleb Smirnoff 3116a81c400eSJeff Roberson /* Allocate the zone of zones, zone of kegs, and zone of zones keg. */ 3117a81c400eSJeff Roberson size = (zsize * 2) + ksize; 311881302f1dSMark Johnston for (domain = 0; domain < vm_ndomains; domain++) { 311981302f1dSMark Johnston m = (uintptr_t)startup_alloc(NULL, size, domain, &pflag, 312081302f1dSMark Johnston M_NOWAIT | M_ZERO); 312181302f1dSMark Johnston if (m != 0) 312281302f1dSMark Johnston break; 312381302f1dSMark Johnston } 3124ab3185d1SJeff Roberson zones = (uma_zone_t)m; 312579c9f942SJeff Roberson m += zsize; 3126ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 312779c9f942SJeff Roberson m += zsize; 3128c8b0a88bSJeff Roberson primarykeg = (uma_keg_t)m; 3129ab3185d1SJeff Roberson 3130099a0e58SBosko Milekic /* "manually" create the initial zone */ 31310095a784SJeff Roberson memset(&args, 0, sizeof(args)); 3132099a0e58SBosko Milekic args.name = "UMA Kegs"; 3133ab3185d1SJeff Roberson args.size = ksize; 3134099a0e58SBosko Milekic args.ctor = keg_ctor; 3135099a0e58SBosko Milekic args.dtor = keg_dtor; 31368355f576SJeff Roberson args.uminit = zero_init; 31378355f576SJeff Roberson args.fini = NULL; 3138c8b0a88bSJeff Roberson args.keg = primarykeg; 313979c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 3140b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 3141ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 31428355f576SJeff Roberson 3143099a0e58SBosko Milekic args.name = "UMA Zones"; 3144f4bef67cSGleb Smirnoff args.size = zsize; 3145099a0e58SBosko Milekic args.ctor = zone_ctor; 3146099a0e58SBosko Milekic args.dtor = zone_dtor; 3147099a0e58SBosko Milekic args.uminit = zero_init; 3148099a0e58SBosko Milekic args.fini = NULL; 3149099a0e58SBosko Milekic args.keg = NULL; 315079c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 3151099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 3152ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 3153099a0e58SBosko Milekic 31549b8db4d0SRyan Libby /* Now make zones for slab headers */ 31559b8db4d0SRyan Libby slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE, 31569b8db4d0SRyan Libby NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 31579b8db4d0SRyan Libby slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE, 31581e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 31598355f576SJeff Roberson 31608355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 31618355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 31621e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 31638355f576SJeff Roberson 3164a81c400eSJeff Roberson bucket_init(); 3165d4665eaaSJeff Roberson smr_init(); 31668355f576SJeff Roberson } 31678355f576SJeff Roberson 3168a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 3169a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void); 3170f4bef67cSGleb Smirnoff #endif 3171f4bef67cSGleb Smirnoff 3172a81c400eSJeff Roberson /* 3173a81c400eSJeff Roberson * Advertise the availability of normal kva allocations and switch to 3174a81c400eSJeff Roberson * the default back-end allocator. Marks the KVA we consumed on startup 3175a81c400eSJeff Roberson * as used in the map. 3176a81c400eSJeff Roberson */ 31778355f576SJeff Roberson void 317899571dc3SJeff Roberson uma_startup2(void) 31798355f576SJeff Roberson { 3180f4bef67cSGleb Smirnoff 3181530cc6a2SJeff Roberson if (bootstart != bootmem) { 3182a81c400eSJeff Roberson vm_map_lock(kernel_map); 3183a81c400eSJeff Roberson (void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem, 3184a81c400eSJeff Roberson VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 3185a81c400eSJeff Roberson vm_map_unlock(kernel_map); 3186a81c400eSJeff Roberson } 3187a81c400eSJeff Roberson 3188a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 3189a81c400eSJeff Roberson /* Set up radix zone to use noobj_alloc. */ 3190a81c400eSJeff Roberson vm_radix_reserve_kva(); 3191f7d35785SGleb Smirnoff #endif 3192a81c400eSJeff Roberson 3193a81c400eSJeff Roberson booted = BOOT_KVA; 3194a81c400eSJeff Roberson zone_foreach_unlocked(zone_kva_available, NULL); 3195f4bef67cSGleb Smirnoff bucket_enable(); 31968355f576SJeff Roberson } 31978355f576SJeff Roberson 3198a81c400eSJeff Roberson /* 3199dc2b3205SMark Johnston * Allocate counters as early as possible so that boot-time allocations are 3200dc2b3205SMark Johnston * accounted more precisely. 3201dc2b3205SMark Johnston */ 3202dc2b3205SMark Johnston static void 3203dc2b3205SMark Johnston uma_startup_pcpu(void *arg __unused) 3204dc2b3205SMark Johnston { 3205dc2b3205SMark Johnston 3206dc2b3205SMark Johnston zone_foreach_unlocked(zone_alloc_counters, NULL); 3207dc2b3205SMark Johnston booted = BOOT_PCPU; 3208dc2b3205SMark Johnston } 3209dc2b3205SMark Johnston SYSINIT(uma_startup_pcpu, SI_SUB_COUNTER, SI_ORDER_ANY, uma_startup_pcpu, NULL); 3210dc2b3205SMark Johnston 3211dc2b3205SMark Johnston /* 3212a81c400eSJeff Roberson * Finish our initialization steps. 3213a81c400eSJeff Roberson */ 32148355f576SJeff Roberson static void 3215dc2b3205SMark Johnston uma_startup3(void *arg __unused) 32168355f576SJeff Roberson { 32171431a748SGleb Smirnoff 3218c5deaf04SGleb Smirnoff #ifdef INVARIANTS 3219c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 3220c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 3221c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 3222c5deaf04SGleb Smirnoff #endif 3223a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_sysctl, NULL); 3224fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 32259643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 3226c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 3227860bb7a0SMark Johnston 3228860bb7a0SMark Johnston EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL, 3229860bb7a0SMark Johnston EVENTHANDLER_PRI_FIRST); 3230860bb7a0SMark Johnston } 3231dc2b3205SMark Johnston SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 3232860bb7a0SMark Johnston 3233860bb7a0SMark Johnston static void 3234860bb7a0SMark Johnston uma_shutdown(void) 3235860bb7a0SMark Johnston { 3236860bb7a0SMark Johnston 3237860bb7a0SMark Johnston booted = BOOT_SHUTDOWN; 32388355f576SJeff Roberson } 32398355f576SJeff Roberson 3240e20a199fSJeff Roberson static uma_keg_t 3241099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 324285dcf349SGleb Smirnoff int align, uint32_t flags) 3243099a0e58SBosko Milekic { 3244099a0e58SBosko Milekic struct uma_kctor_args args; 3245099a0e58SBosko Milekic 3246099a0e58SBosko Milekic args.size = size; 3247099a0e58SBosko Milekic args.uminit = uminit; 3248099a0e58SBosko Milekic args.fini = fini; 32491e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 3250099a0e58SBosko Milekic args.flags = flags; 3251099a0e58SBosko Milekic args.zone = zone; 3252ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 3253099a0e58SBosko Milekic } 3254099a0e58SBosko Milekic 3255f4bef67cSGleb Smirnoff /* Public functions */ 32568355f576SJeff Roberson /* See uma.h */ 32571e319f6dSRobert Watson void 32581e319f6dSRobert Watson uma_set_align(int align) 32591e319f6dSRobert Watson { 32601e319f6dSRobert Watson 32611e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 32621e319f6dSRobert Watson uma_align_cache = align; 32631e319f6dSRobert Watson } 32641e319f6dSRobert Watson 32651e319f6dSRobert Watson /* See uma.h */ 32668355f576SJeff Roberson uma_zone_t 3267bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 326885dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 32698355f576SJeff Roberson 32708355f576SJeff Roberson { 32718355f576SJeff Roberson struct uma_zctor_args args; 327295c4bf75SKonstantin Belousov uma_zone_t res; 32738355f576SJeff Roberson 3274a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 3275a5a35578SJohn Baldwin align, name)); 3276a5a35578SJohn Baldwin 32778355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 32780095a784SJeff Roberson memset(&args, 0, sizeof(args)); 32798355f576SJeff Roberson args.name = name; 32808355f576SJeff Roberson args.size = size; 32818355f576SJeff Roberson args.ctor = ctor; 32828355f576SJeff Roberson args.dtor = dtor; 32838355f576SJeff Roberson args.uminit = uminit; 32848355f576SJeff Roberson args.fini = fini; 328510094910SMark Johnston #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN) 3286afc6dc36SJohn-Mark Gurney /* 3287ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 3288ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 3289ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 3290ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 3291ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 3292ca293436SRyan Libby * the ctor and dtor do not need to be empty. 3293afc6dc36SJohn-Mark Gurney */ 329454c5ae80SRyan Libby if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH | 329554c5ae80SRyan Libby UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) { 3296afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 3297afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 3298afc6dc36SJohn-Mark Gurney } 3299afc6dc36SJohn-Mark Gurney #endif 33008355f576SJeff Roberson args.align = align; 33018355f576SJeff Roberson args.flags = flags; 3302099a0e58SBosko Milekic args.keg = NULL; 3303099a0e58SBosko Milekic 3304aabe13f1SMark Johnston sx_xlock(&uma_reclaim_lock); 3305ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 3306aabe13f1SMark Johnston sx_xunlock(&uma_reclaim_lock); 3307a81c400eSJeff Roberson 330895c4bf75SKonstantin Belousov return (res); 3309099a0e58SBosko Milekic } 3310099a0e58SBosko Milekic 3311099a0e58SBosko Milekic /* See uma.h */ 3312099a0e58SBosko Milekic uma_zone_t 33130464f16eSMark Johnston uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor, 3314c8b0a88bSJeff Roberson uma_init zinit, uma_fini zfini, uma_zone_t primary) 3315099a0e58SBosko Milekic { 3316099a0e58SBosko Milekic struct uma_zctor_args args; 3317e20a199fSJeff Roberson uma_keg_t keg; 331895c4bf75SKonstantin Belousov uma_zone_t res; 3319099a0e58SBosko Milekic 3320c8b0a88bSJeff Roberson keg = primary->uz_keg; 33210095a784SJeff Roberson memset(&args, 0, sizeof(args)); 3322099a0e58SBosko Milekic args.name = name; 3323e20a199fSJeff Roberson args.size = keg->uk_size; 3324099a0e58SBosko Milekic args.ctor = ctor; 3325099a0e58SBosko Milekic args.dtor = dtor; 3326099a0e58SBosko Milekic args.uminit = zinit; 3327099a0e58SBosko Milekic args.fini = zfini; 3328e20a199fSJeff Roberson args.align = keg->uk_align; 3329e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 3330e20a199fSJeff Roberson args.keg = keg; 33318355f576SJeff Roberson 3332aabe13f1SMark Johnston sx_xlock(&uma_reclaim_lock); 3333ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 3334aabe13f1SMark Johnston sx_xunlock(&uma_reclaim_lock); 3335a81c400eSJeff Roberson 333695c4bf75SKonstantin Belousov return (res); 33378355f576SJeff Roberson } 33388355f576SJeff Roberson 33390095a784SJeff Roberson /* See uma.h */ 33400095a784SJeff Roberson uma_zone_t 33410464f16eSMark Johnston uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor, 33420464f16eSMark Johnston uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease, 33430464f16eSMark Johnston void *arg, int flags) 33440095a784SJeff Roberson { 33450095a784SJeff Roberson struct uma_zctor_args args; 33460095a784SJeff Roberson 33470095a784SJeff Roberson memset(&args, 0, sizeof(args)); 33480095a784SJeff Roberson args.name = name; 3349af526374SJeff Roberson args.size = size; 33500095a784SJeff Roberson args.ctor = ctor; 33510095a784SJeff Roberson args.dtor = dtor; 33520095a784SJeff Roberson args.uminit = zinit; 33530095a784SJeff Roberson args.fini = zfini; 33540095a784SJeff Roberson args.import = zimport; 33550095a784SJeff Roberson args.release = zrelease; 33560095a784SJeff Roberson args.arg = arg; 33570095a784SJeff Roberson args.align = 0; 3358bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 33590095a784SJeff Roberson 3360ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 33610095a784SJeff Roberson } 33620095a784SJeff Roberson 33638355f576SJeff Roberson /* See uma.h */ 33649c2cd7e5SJeff Roberson void 33659c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 33669c2cd7e5SJeff Roberson { 3367f4ff923bSRobert Watson 3368860bb7a0SMark Johnston /* 3369860bb7a0SMark Johnston * Large slabs are expensive to reclaim, so don't bother doing 3370860bb7a0SMark Johnston * unnecessary work if we're shutting down. 3371860bb7a0SMark Johnston */ 3372860bb7a0SMark Johnston if (booted == BOOT_SHUTDOWN && 3373860bb7a0SMark Johnston zone->uz_fini == NULL && zone->uz_release == zone_release) 3374860bb7a0SMark Johnston return; 3375aabe13f1SMark Johnston sx_xlock(&uma_reclaim_lock); 33760095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 3377aabe13f1SMark Johnston sx_xunlock(&uma_reclaim_lock); 33789c2cd7e5SJeff Roberson } 33799c2cd7e5SJeff Roberson 33808d6fbbb8SJeff Roberson void 33818d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 33828d6fbbb8SJeff Roberson { 33838d6fbbb8SJeff Roberson 338470260874SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 338570260874SJeff Roberson uma_zfree_smr(zone, uma_zalloc_smr(zone, M_WAITOK)); 338670260874SJeff Roberson else if ((zone->uz_flags & UMA_ZONE_PCPU) != 0) 338770260874SJeff Roberson uma_zfree_pcpu(zone, uma_zalloc_pcpu(zone, M_WAITOK)); 338870260874SJeff Roberson else 338970260874SJeff Roberson uma_zfree(zone, uma_zalloc(zone, M_WAITOK)); 33908d6fbbb8SJeff Roberson } 33918d6fbbb8SJeff Roberson 33924e180881SMateusz Guzik void * 33934e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 33944e180881SMateusz Guzik { 33953acb6572SMateusz Guzik void *item, *pcpu_item; 3396b4799947SRuslan Bukin #ifdef SMP 33974e180881SMateusz Guzik int i; 33984e180881SMateusz Guzik 33994e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 3400b4799947SRuslan Bukin #endif 34014e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 34023acb6572SMateusz Guzik if (item == NULL) 34033acb6572SMateusz Guzik return (NULL); 34043acb6572SMateusz Guzik pcpu_item = zpcpu_base_to_offset(item); 34053acb6572SMateusz Guzik if (flags & M_ZERO) { 3406b4799947SRuslan Bukin #ifdef SMP 3407013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 34083acb6572SMateusz Guzik bzero(zpcpu_get_cpu(pcpu_item, i), zone->uz_size); 3409b4799947SRuslan Bukin #else 3410b4799947SRuslan Bukin bzero(item, zone->uz_size); 3411b4799947SRuslan Bukin #endif 34124e180881SMateusz Guzik } 34133acb6572SMateusz Guzik return (pcpu_item); 34144e180881SMateusz Guzik } 34154e180881SMateusz Guzik 34164e180881SMateusz Guzik /* 34174e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 34184e180881SMateusz Guzik */ 34194e180881SMateusz Guzik void 34203acb6572SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *pcpu_item, void *udata) 34214e180881SMateusz Guzik { 34223acb6572SMateusz Guzik void *item; 34234e180881SMateusz Guzik 3424c5b7751fSIan Lepore #ifdef SMP 34254e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 3426c5b7751fSIan Lepore #endif 3427b8f7267dSKristof Provost 3428b8f7267dSKristof Provost /* uma_zfree_pcu_*(..., NULL) does nothing, to match free(9). */ 3429b8f7267dSKristof Provost if (pcpu_item == NULL) 3430b8f7267dSKristof Provost return; 3431b8f7267dSKristof Provost 34323acb6572SMateusz Guzik item = zpcpu_offset_to_base(pcpu_item); 34334e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 34344e180881SMateusz Guzik } 34354e180881SMateusz Guzik 3436d4665eaaSJeff Roberson static inline void * 3437d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags, 3438d4665eaaSJeff Roberson void *item) 3439beb8beefSJeff Roberson { 3440beb8beefSJeff Roberson #ifdef INVARIANTS 3441ca293436SRyan Libby bool skipdbg; 344209c8cb71SMark Johnston #endif 3443beb8beefSJeff Roberson 344409c8cb71SMark Johnston kasan_mark_item_valid(zone, item); 344510094910SMark Johnston kmsan_mark_item_uninitialized(zone, item); 344609c8cb71SMark Johnston 344709c8cb71SMark Johnston #ifdef INVARIANTS 3448beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 344909c8cb71SMark Johnston if (!skipdbg && (uz_flags & UMA_ZFLAG_TRASH) != 0 && 3450ca293436SRyan Libby zone->uz_ctor != trash_ctor) 3451cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 3452beb8beefSJeff Roberson #endif 345309c8cb71SMark Johnston 3454d4665eaaSJeff Roberson /* Check flags before loading ctor pointer. */ 3455d4665eaaSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) && 3456d4665eaaSJeff Roberson __predict_false(zone->uz_ctor != NULL) && 3457cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 3458beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3459beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 3460beb8beefSJeff Roberson return (NULL); 3461beb8beefSJeff Roberson } 3462beb8beefSJeff Roberson #ifdef INVARIANTS 3463beb8beefSJeff Roberson if (!skipdbg) 3464beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 3465beb8beefSJeff Roberson #endif 34666d88d784SJeff Roberson if (__predict_false(flags & M_ZERO)) 34676d88d784SJeff Roberson return (memset(item, 0, size)); 3468beb8beefSJeff Roberson 3469beb8beefSJeff Roberson return (item); 3470beb8beefSJeff Roberson } 3471beb8beefSJeff Roberson 3472ca293436SRyan Libby static inline void 3473cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 3474cc7ce83aSJeff Roberson enum zfreeskip skip) 3475ca293436SRyan Libby { 3476ca293436SRyan Libby #ifdef INVARIANTS 3477ca293436SRyan Libby bool skipdbg; 3478ca293436SRyan Libby 3479ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 3480ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 3481ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 3482ca293436SRyan Libby uma_dbg_free(zone, udata, item); 3483ca293436SRyan Libby else 3484ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 3485ca293436SRyan Libby } 3486ca293436SRyan Libby #endif 3487cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 3488ca293436SRyan Libby if (zone->uz_dtor != NULL) 3489cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 3490ca293436SRyan Libby #ifdef INVARIANTS 3491ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 3492ca293436SRyan Libby zone->uz_dtor != trash_dtor) 3493cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 3494ca293436SRyan Libby #endif 3495ca293436SRyan Libby } 349609c8cb71SMark Johnston kasan_mark_item_invalid(zone, item); 3497ca293436SRyan Libby } 3498ca293436SRyan Libby 34991c58c09fSMateusz Guzik #ifdef NUMA 350081302f1dSMark Johnston static int 350181302f1dSMark Johnston item_domain(void *item) 350281302f1dSMark Johnston { 350381302f1dSMark Johnston int domain; 350481302f1dSMark Johnston 3505431fb8abSMark Johnston domain = vm_phys_domain(vtophys(item)); 350681302f1dSMark Johnston KASSERT(domain >= 0 && domain < vm_ndomains, 350781302f1dSMark Johnston ("%s: unknown domain for item %p", __func__, item)); 350881302f1dSMark Johnston return (domain); 350981302f1dSMark Johnston } 35101c58c09fSMateusz Guzik #endif 351181302f1dSMark Johnston 3512d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS) 3513d4665eaaSJeff Roberson #define UMA_ZALLOC_DEBUG 3514d4665eaaSJeff Roberson static int 3515d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags) 3516d4665eaaSJeff Roberson { 3517d4665eaaSJeff Roberson int error; 3518d4665eaaSJeff Roberson 3519d4665eaaSJeff Roberson error = 0; 3520d4665eaaSJeff Roberson #ifdef WITNESS 3521d4665eaaSJeff Roberson if (flags & M_WAITOK) { 3522d4665eaaSJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3523d4665eaaSJeff Roberson "uma_zalloc_debug: zone \"%s\"", zone->uz_name); 3524d4665eaaSJeff Roberson } 3525d4665eaaSJeff Roberson #endif 3526d4665eaaSJeff Roberson 3527d4665eaaSJeff Roberson #ifdef INVARIANTS 3528d4665eaaSJeff Roberson KASSERT((flags & M_EXEC) == 0, 3529d4665eaaSJeff Roberson ("uma_zalloc_debug: called with M_EXEC")); 3530d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3531d4665eaaSJeff Roberson ("uma_zalloc_debug: called within spinlock or critical section")); 3532d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0, 3533d4665eaaSJeff Roberson ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO")); 3534d4665eaaSJeff Roberson #endif 3535d4665eaaSJeff Roberson 3536d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 35379e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) { 3538d4665eaaSJeff Roberson void *item; 3539d4665eaaSJeff Roberson item = memguard_alloc(zone->uz_size, flags); 3540d4665eaaSJeff Roberson if (item != NULL) { 3541d4665eaaSJeff Roberson error = EJUSTRETURN; 3542d4665eaaSJeff Roberson if (zone->uz_init != NULL && 3543d4665eaaSJeff Roberson zone->uz_init(item, zone->uz_size, flags) != 0) { 3544d4665eaaSJeff Roberson *itemp = NULL; 3545d4665eaaSJeff Roberson return (error); 3546d4665eaaSJeff Roberson } 3547d4665eaaSJeff Roberson if (zone->uz_ctor != NULL && 3548d4665eaaSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 3549d4665eaaSJeff Roberson flags) != 0) { 3550d4665eaaSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3551d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3552d4665eaaSJeff Roberson *itemp = NULL; 3553d4665eaaSJeff Roberson return (error); 3554d4665eaaSJeff Roberson } 3555d4665eaaSJeff Roberson *itemp = item; 3556d4665eaaSJeff Roberson return (error); 3557d4665eaaSJeff Roberson } 3558d4665eaaSJeff Roberson /* This is unfortunate but should not be fatal. */ 3559d4665eaaSJeff Roberson } 3560d4665eaaSJeff Roberson #endif 3561d4665eaaSJeff Roberson return (error); 3562d4665eaaSJeff Roberson } 3563d4665eaaSJeff Roberson 3564d4665eaaSJeff Roberson static int 3565d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata) 3566d4665eaaSJeff Roberson { 3567d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3568d4665eaaSJeff Roberson ("uma_zfree_debug: called with spinlock or critical section held")); 3569d4665eaaSJeff Roberson 3570d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 35719e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) { 3572d4665eaaSJeff Roberson if (zone->uz_dtor != NULL) 3573d4665eaaSJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 3574d4665eaaSJeff Roberson if (zone->uz_fini != NULL) 3575d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3576d4665eaaSJeff Roberson memguard_free(item); 3577d4665eaaSJeff Roberson return (EJUSTRETURN); 3578d4665eaaSJeff Roberson } 3579d4665eaaSJeff Roberson #endif 3580d4665eaaSJeff Roberson return (0); 3581d4665eaaSJeff Roberson } 3582d4665eaaSJeff Roberson #endif 3583d4665eaaSJeff Roberson 35846d88d784SJeff Roberson static inline void * 35856d88d784SJeff Roberson cache_alloc_item(uma_zone_t zone, uma_cache_t cache, uma_cache_bucket_t bucket, 35866d88d784SJeff Roberson void *udata, int flags) 3587d4665eaaSJeff Roberson { 35886d88d784SJeff Roberson void *item; 35896d88d784SJeff Roberson int size, uz_flags; 35906d88d784SJeff Roberson 35916d88d784SJeff Roberson item = cache_bucket_pop(cache, bucket); 35926d88d784SJeff Roberson size = cache_uz_size(cache); 35936d88d784SJeff Roberson uz_flags = cache_uz_flags(cache); 35946d88d784SJeff Roberson critical_exit(); 35956d88d784SJeff Roberson return (item_ctor(zone, uz_flags, size, udata, flags, item)); 35966d88d784SJeff Roberson } 35976d88d784SJeff Roberson 35986d88d784SJeff Roberson static __noinline void * 35996d88d784SJeff Roberson cache_alloc_retry(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 36006d88d784SJeff Roberson { 36016d88d784SJeff Roberson uma_cache_bucket_t bucket; 3602d4665eaaSJeff Roberson int domain; 3603d4665eaaSJeff Roberson 36046d88d784SJeff Roberson while (cache_alloc(zone, cache, udata, flags)) { 36056d88d784SJeff Roberson cache = &zone->uz_cpu[curcpu]; 36066d88d784SJeff Roberson bucket = &cache->uc_allocbucket; 36076d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 36086d88d784SJeff Roberson continue; 36096d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, udata, flags)); 36106d88d784SJeff Roberson } 36116d88d784SJeff Roberson critical_exit(); 36126d88d784SJeff Roberson 3613d4665eaaSJeff Roberson /* 3614d4665eaaSJeff Roberson * We can not get a bucket so try to return a single item. 3615d4665eaaSJeff Roberson */ 3616d4665eaaSJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 3617d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3618d4665eaaSJeff Roberson else 3619d4665eaaSJeff Roberson domain = UMA_ANYDOMAIN; 3620d4665eaaSJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3621d4665eaaSJeff Roberson } 3622d4665eaaSJeff Roberson 3623d4665eaaSJeff Roberson /* See uma.h */ 3624d4665eaaSJeff Roberson void * 3625d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags) 3626d4665eaaSJeff Roberson { 3627d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 3628d4665eaaSJeff Roberson uma_cache_t cache; 3629d4665eaaSJeff Roberson 3630841e0a87SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc_smr zone %s(%p) flags %d", zone->uz_name, 3631841e0a87SGleb Smirnoff zone, flags); 3632841e0a87SGleb Smirnoff 3633d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 36346d88d784SJeff Roberson void *item; 36356d88d784SJeff Roberson 3636d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 3637952c8964SMark Johnston ("uma_zalloc_arg: called with non-SMR zone.")); 3638d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN) 3639d4665eaaSJeff Roberson return (item); 3640d4665eaaSJeff Roberson #endif 3641d4665eaaSJeff Roberson 3642d4665eaaSJeff Roberson critical_enter(); 3643d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3644d4665eaaSJeff Roberson bucket = &cache->uc_allocbucket; 36456d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 36466d88d784SJeff Roberson return (cache_alloc_retry(zone, cache, NULL, flags)); 36476d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, NULL, flags)); 3648d4665eaaSJeff Roberson } 3649d4665eaaSJeff Roberson 36509c2cd7e5SJeff Roberson /* See uma.h */ 36518355f576SJeff Roberson void * 36522cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 36538355f576SJeff Roberson { 3654376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3655ab3185d1SJeff Roberson uma_cache_t cache; 36568355f576SJeff Roberson 3657e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 365819fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 365910cb2424SMark Murray 36608355f576SJeff Roberson /* This is the fast path allocation */ 3661e63a1c2fSRyan Libby CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name, 3662e63a1c2fSRyan Libby zone, flags); 3663a553d4b8SJeff Roberson 3664d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 36656d88d784SJeff Roberson void *item; 36666d88d784SJeff Roberson 3667d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 3668952c8964SMark Johnston ("uma_zalloc_arg: called with SMR zone.")); 3669d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN) 36708d689e04SGleb Smirnoff return (item); 36718d689e04SGleb Smirnoff #endif 3672d4665eaaSJeff Roberson 36735d1ae027SRobert Watson /* 36745d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 36755d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 36765d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 36775d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 36785d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 36795d1ae027SRobert Watson * preemption and migration. We release the critical section in 36805d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 36815d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 36825d1ae027SRobert Watson * must detect and handle migration if it has occurred. 36835d1ae027SRobert Watson */ 36845d1ae027SRobert Watson critical_enter(); 3685cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3686376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 36876d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 36886d88d784SJeff Roberson return (cache_alloc_retry(zone, cache, udata, flags)); 36896d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, udata, flags)); 3690fc03d22bSJeff Roberson } 3691fc03d22bSJeff Roberson 36928355f576SJeff Roberson /* 3693beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 3694beb8beefSJeff Roberson * a critical section. Returns in a critical section. 3695beb8beefSJeff Roberson * 36964bd61e19SJeff Roberson * A false return value indicates an allocation failure. 36974bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 3698beb8beefSJeff Roberson */ 3699beb8beefSJeff Roberson static __noinline bool 3700beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3701beb8beefSJeff Roberson { 3702beb8beefSJeff Roberson uma_bucket_t bucket; 37038c277118SMark Johnston int curdomain, domain; 3704c6fd3e23SJeff Roberson bool new; 3705beb8beefSJeff Roberson 3706beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3707beb8beefSJeff Roberson 3708beb8beefSJeff Roberson /* 3709beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3710beb8beefSJeff Roberson * if we can switch with the free bucket. 3711d4665eaaSJeff Roberson * 3712d4665eaaSJeff Roberson * SMR Zones can't re-use the free bucket until the sequence has 3713d4665eaaSJeff Roberson * expired. 37148355f576SJeff Roberson */ 3715c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_SMR) == 0 && 3716d4665eaaSJeff Roberson cache->uc_freebucket.ucb_cnt != 0) { 3717d4665eaaSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 3718d4665eaaSJeff Roberson &cache->uc_allocbucket); 3719beb8beefSJeff Roberson return (true); 37208355f576SJeff Roberson } 3721fc03d22bSJeff Roberson 3722fc03d22bSJeff Roberson /* 3723fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3724fc03d22bSJeff Roberson */ 3725376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3726fc03d22bSJeff Roberson critical_exit(); 3727c6fd3e23SJeff Roberson 3728c6fd3e23SJeff Roberson if (bucket != NULL) { 3729c6fd3e23SJeff Roberson KASSERT(bucket->ub_cnt == 0, 3730c6fd3e23SJeff Roberson ("cache_alloc: Entered with non-empty alloc bucket.")); 37316fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3732c6fd3e23SJeff Roberson } 3733fc03d22bSJeff Roberson 37345d1ae027SRobert Watson /* 37355d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 3736c6fd3e23SJeff Roberson * we must go back to the zone. This requires the zdom lock, so we 37375d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 37385d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 37395d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 37405d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 37415d1ae027SRobert Watson * the critical section. 37425d1ae027SRobert Watson */ 3743c1685086SJeff Roberson domain = PCPU_GET(domain); 37448c277118SMark Johnston if ((cache_uz_flags(cache) & UMA_ZONE_ROUNDROBIN) != 0 || 37458c277118SMark Johnston VM_DOMAIN_EMPTY(domain)) 3746c6fd3e23SJeff Roberson domain = zone_domain_highest(zone, domain); 3747c6fd3e23SJeff Roberson bucket = cache_fetch_bucket(zone, cache, domain); 3748af32cefdSMark Johnston if (bucket == NULL && zone->uz_bucket_size != 0 && !bucketdisable) { 3749beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 3750c6fd3e23SJeff Roberson new = true; 3751af32cefdSMark Johnston } else { 3752c6fd3e23SJeff Roberson new = false; 3753af32cefdSMark Johnston } 3754c6fd3e23SJeff Roberson 37551431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 37561431a748SGleb Smirnoff zone->uz_name, zone, bucket); 37574bd61e19SJeff Roberson if (bucket == NULL) { 3758fc03d22bSJeff Roberson critical_enter(); 3759beb8beefSJeff Roberson return (false); 37604bd61e19SJeff Roberson } 37610f9b7bf3SMark Johnston 3762fc03d22bSJeff Roberson /* 3763fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3764fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3765fc03d22bSJeff Roberson * the memory directly. 3766fc03d22bSJeff Roberson */ 37674bd61e19SJeff Roberson critical_enter(); 3768cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3769376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 3770c6fd3e23SJeff Roberson ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) == 0 || 37718c277118SMark Johnston (curdomain = PCPU_GET(domain)) == domain || 37728c277118SMark Johnston VM_DOMAIN_EMPTY(curdomain))) { 3773c6fd3e23SJeff Roberson if (new) 3774c6fd3e23SJeff Roberson atomic_add_long(&ZDOM_GET(zone, domain)->uzd_imax, 3775c6fd3e23SJeff Roberson bucket->ub_cnt); 3776376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3777beb8beefSJeff Roberson return (true); 3778c6fd3e23SJeff Roberson } 3779c6fd3e23SJeff Roberson 3780c6fd3e23SJeff Roberson /* 3781c6fd3e23SJeff Roberson * We lost the race, release this bucket and start over. 3782c6fd3e23SJeff Roberson */ 3783c6fd3e23SJeff Roberson critical_exit(); 37842760658bSAlexander Motin zone_put_bucket(zone, domain, bucket, udata, !new); 3785c6fd3e23SJeff Roberson critical_enter(); 3786c6fd3e23SJeff Roberson 3787beb8beefSJeff Roberson return (true); 3788bbee39c6SJeff Roberson } 3789bbee39c6SJeff Roberson 3790ab3185d1SJeff Roberson void * 3791ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3792bbee39c6SJeff Roberson { 379306d8bdcbSMark Johnston #ifdef NUMA 379406d8bdcbSMark Johnston uma_bucket_t bucket; 379506d8bdcbSMark Johnston uma_zone_domain_t zdom; 379606d8bdcbSMark Johnston void *item; 379706d8bdcbSMark Johnston #endif 3798ab3185d1SJeff Roberson 3799ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 380019fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3801ab3185d1SJeff Roberson 3802ab3185d1SJeff Roberson /* This is the fast path allocation */ 3803e63a1c2fSRyan Libby CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d", 3804e63a1c2fSRyan Libby zone->uz_name, zone, domain, flags); 3805ab3185d1SJeff Roberson 3806ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3807ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3808ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3809ab3185d1SJeff Roberson } 3810ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3811ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 381206d8bdcbSMark Johnston KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 381306d8bdcbSMark Johnston ("uma_zalloc_domain: called with SMR zone.")); 381406d8bdcbSMark Johnston #ifdef NUMA 381506d8bdcbSMark Johnston KASSERT((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0, 381606d8bdcbSMark Johnston ("uma_zalloc_domain: called with non-FIRSTTOUCH zone.")); 3817ab3185d1SJeff Roberson 381806d8bdcbSMark Johnston if (vm_ndomains == 1) 381906d8bdcbSMark Johnston return (uma_zalloc_arg(zone, udata, flags)); 382006d8bdcbSMark Johnston 382106d8bdcbSMark Johnston /* 382206d8bdcbSMark Johnston * Try to allocate from the bucket cache before falling back to the keg. 382306d8bdcbSMark Johnston * We could try harder and attempt to allocate from per-CPU caches or 382406d8bdcbSMark Johnston * the per-domain cross-domain buckets, but the complexity is probably 382506d8bdcbSMark Johnston * not worth it. It is more important that frees of previous 382606d8bdcbSMark Johnston * cross-domain allocations do not blow up the cache. 382706d8bdcbSMark Johnston */ 382806d8bdcbSMark Johnston zdom = zone_domain_lock(zone, domain); 382906d8bdcbSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) { 383006d8bdcbSMark Johnston item = bucket->ub_bucket[bucket->ub_cnt - 1]; 383106d8bdcbSMark Johnston #ifdef INVARIANTS 383206d8bdcbSMark Johnston bucket->ub_bucket[bucket->ub_cnt - 1] = NULL; 383306d8bdcbSMark Johnston #endif 383406d8bdcbSMark Johnston bucket->ub_cnt--; 383506d8bdcbSMark Johnston zone_put_bucket(zone, domain, bucket, udata, true); 383606d8bdcbSMark Johnston item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, 383706d8bdcbSMark Johnston flags, item); 383806d8bdcbSMark Johnston if (item != NULL) { 383906d8bdcbSMark Johnston KASSERT(item_domain(item) == domain, 384006d8bdcbSMark Johnston ("%s: bucket cache item %p from wrong domain", 384106d8bdcbSMark Johnston __func__, item)); 384206d8bdcbSMark Johnston counter_u64_add(zone->uz_allocs, 1); 384306d8bdcbSMark Johnston } 384406d8bdcbSMark Johnston return (item); 384506d8bdcbSMark Johnston } 384606d8bdcbSMark Johnston ZDOM_UNLOCK(zdom); 3847ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 384806d8bdcbSMark Johnston #else 384906d8bdcbSMark Johnston return (uma_zalloc_arg(zone, udata, flags)); 385006d8bdcbSMark Johnston #endif 3851ab3185d1SJeff Roberson } 3852ab3185d1SJeff Roberson 3853ab3185d1SJeff Roberson /* 3854ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3855ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3856ab3185d1SJeff Roberson * 3857ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3858ab3185d1SJeff Roberson * only 'domain'. 3859ab3185d1SJeff Roberson */ 3860ab3185d1SJeff Roberson static uma_slab_t 3861194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3862ab3185d1SJeff Roberson { 3863ab3185d1SJeff Roberson uma_domain_t dom; 3864bbee39c6SJeff Roberson uma_slab_t slab; 3865ab3185d1SJeff Roberson int start; 3866ab3185d1SJeff Roberson 3867ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3868ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 38698b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3870ab3185d1SJeff Roberson 3871ab3185d1SJeff Roberson slab = NULL; 3872ab3185d1SJeff Roberson start = domain; 3873ab3185d1SJeff Roberson do { 3874ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 38754ab3aee8SMark Johnston if ((slab = LIST_FIRST(&dom->ud_part_slab)) != NULL) 38764ab3aee8SMark Johnston return (slab); 38774ab3aee8SMark Johnston if ((slab = LIST_FIRST(&dom->ud_free_slab)) != NULL) { 3878ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 38794ab3aee8SMark Johnston dom->ud_free_slabs--; 3880ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3881ab3185d1SJeff Roberson return (slab); 3882ab3185d1SJeff Roberson } 3883ab3185d1SJeff Roberson if (rr) 3884ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3885ab3185d1SJeff Roberson } while (domain != start); 3886ab3185d1SJeff Roberson 3887ab3185d1SJeff Roberson return (NULL); 3888ab3185d1SJeff Roberson } 3889ab3185d1SJeff Roberson 38908b987a77SJeff Roberson /* 38918b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 38928b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 38938b987a77SJeff Roberson */ 3894ab3185d1SJeff Roberson static uma_slab_t 3895194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3896ab3185d1SJeff Roberson { 38978b987a77SJeff Roberson uma_slab_t slab; 3898194a979eSMark Johnston uint32_t reserve; 3899099a0e58SBosko Milekic 39008b987a77SJeff Roberson /* HASH has a single free list. */ 390154c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 39028b987a77SJeff Roberson domain = 0; 3903194a979eSMark Johnston 39048b987a77SJeff Roberson KEG_LOCK(keg, domain); 3905194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 39064ab3aee8SMark Johnston if (keg->uk_domain[domain].ud_free_items <= reserve || 39078b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 39088b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3909194a979eSMark Johnston return (NULL); 39108b987a77SJeff Roberson } 39118b987a77SJeff Roberson return (slab); 3912194a979eSMark Johnston } 3913194a979eSMark Johnston 3914194a979eSMark Johnston static uma_slab_t 3915194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3916194a979eSMark Johnston { 3917194a979eSMark Johnston struct vm_domainset_iter di; 3918194a979eSMark Johnston uma_slab_t slab; 3919194a979eSMark Johnston int aflags, domain; 3920194a979eSMark Johnston bool rr; 3921194a979eSMark Johnston 3922fab343a7SMark Johnston KASSERT((flags & (M_WAITOK | M_NOVM)) != (M_WAITOK | M_NOVM), 3923fab343a7SMark Johnston ("%s: invalid flags %#x", __func__, flags)); 3924fab343a7SMark Johnston 3925194a979eSMark Johnston restart: 3926bbee39c6SJeff Roberson /* 3927194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3928194a979eSMark Johnston * domain (as happens with first-touch zones). 3929194a979eSMark Johnston * 3930194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3931194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3932194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3933bbee39c6SJeff Roberson */ 3934ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3935ab3185d1SJeff Roberson if (rr) { 3936194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3937194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3938194a979eSMark Johnston &aflags); 3939194a979eSMark Johnston } else { 3940194a979eSMark Johnston aflags = flags; 3941194a979eSMark Johnston domain = rdomain; 3942194a979eSMark Johnston } 3943ab3185d1SJeff Roberson 3944194a979eSMark Johnston for (;;) { 3945194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3946584061b4SJeff Roberson if (slab != NULL) 3947bbee39c6SJeff Roberson return (slab); 3948bbee39c6SJeff Roberson 3949bbee39c6SJeff Roberson /* 3950fab343a7SMark Johnston * M_NOVM is used to break the recursion that can otherwise 3951fab343a7SMark Johnston * occur if low-level memory management routines use UMA. 3952bbee39c6SJeff Roberson */ 3953fab343a7SMark Johnston if ((flags & M_NOVM) == 0) { 395486220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 39558b987a77SJeff Roberson if (slab != NULL) 3956bbee39c6SJeff Roberson return (slab); 3957fab343a7SMark Johnston } 3958fab343a7SMark Johnston 3959fab343a7SMark Johnston if (!rr) { 3960fab343a7SMark Johnston if ((flags & M_USE_RESERVE) != 0) { 3961fab343a7SMark Johnston /* 3962fab343a7SMark Johnston * Drain reserves from other domains before 3963fab343a7SMark Johnston * giving up or sleeping. It may be useful to 3964fab343a7SMark Johnston * support per-domain reserves eventually. 3965fab343a7SMark Johnston */ 3966fab343a7SMark Johnston rdomain = UMA_ANYDOMAIN; 3967fab343a7SMark Johnston goto restart; 3968fab343a7SMark Johnston } 3969fab343a7SMark Johnston if ((flags & M_WAITOK) == 0) 39703639ac42SJeff Roberson break; 3971fab343a7SMark Johnston vm_wait_domain(domain); 3972fab343a7SMark Johnston } else if (vm_domainset_iter_policy(&di, &domain) != 0) { 3973194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 397489d2fb14SKonstantin Belousov vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0); 3975194a979eSMark Johnston goto restart; 397630c5525bSAndrew Gallatin } 3977194a979eSMark Johnston break; 3978194a979eSMark Johnston } 3979ab3185d1SJeff Roberson } 3980ab3185d1SJeff Roberson 3981bbee39c6SJeff Roberson /* 3982bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3983bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3984bbee39c6SJeff Roberson * fail. 3985bbee39c6SJeff Roberson */ 39868b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 3987bbee39c6SJeff Roberson return (slab); 39888b987a77SJeff Roberson 3989ab3185d1SJeff Roberson return (NULL); 3990ab3185d1SJeff Roberson } 3991bbee39c6SJeff Roberson 3992d56368d7SBosko Milekic static void * 39930095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3994bbee39c6SJeff Roberson { 3995ab3185d1SJeff Roberson uma_domain_t dom; 3996bbee39c6SJeff Roberson void *item; 39979b8db4d0SRyan Libby int freei; 3998bbee39c6SJeff Roberson 39998b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 4000099a0e58SBosko Milekic 40018b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 40029b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 40039b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 40041e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 4005bbee39c6SJeff Roberson slab->us_freecount--; 40064ab3aee8SMark Johnston dom->ud_free_items--; 4007ef72505eSJeff Roberson 40084ab3aee8SMark Johnston /* 40094ab3aee8SMark Johnston * Move this slab to the full list. It must be on the partial list, so 40104ab3aee8SMark Johnston * we do not need to update the free slab count. In particular, 40114ab3aee8SMark Johnston * keg_fetch_slab() always returns slabs on the partial list. 40124ab3aee8SMark Johnston */ 4013bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 4014bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 4015ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 4016bbee39c6SJeff Roberson } 4017bbee39c6SJeff Roberson 4018bbee39c6SJeff Roberson return (item); 4019bbee39c6SJeff Roberson } 4020bbee39c6SJeff Roberson 4021bbee39c6SJeff Roberson static int 4022b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 40230095a784SJeff Roberson { 40248b987a77SJeff Roberson uma_domain_t dom; 4025b75c4efcSAndrew Turner uma_zone_t zone; 40260095a784SJeff Roberson uma_slab_t slab; 40270095a784SJeff Roberson uma_keg_t keg; 4028a03af342SSean Bruno #ifdef NUMA 4029ab3185d1SJeff Roberson int stripe; 4030a03af342SSean Bruno #endif 40310095a784SJeff Roberson int i; 40320095a784SJeff Roberson 4033b75c4efcSAndrew Turner zone = arg; 40340095a784SJeff Roberson slab = NULL; 4035584061b4SJeff Roberson keg = zone->uz_keg; 4036af526374SJeff Roberson /* Try to keep the buckets totally full */ 40370095a784SJeff Roberson for (i = 0; i < max; ) { 4038584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 40390095a784SJeff Roberson break; 4040a03af342SSean Bruno #ifdef NUMA 4041ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 4042a03af342SSean Bruno #endif 40438b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 40441b2dcc8cSMark Johnston do { 40450095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 40467585c5dbSMark Johnston if (keg->uk_reserve > 0 && 40477585c5dbSMark Johnston dom->ud_free_items <= keg->uk_reserve) { 40481b2dcc8cSMark Johnston /* 40491b2dcc8cSMark Johnston * Avoid depleting the reserve after a 40501b2dcc8cSMark Johnston * successful item allocation, even if 40511b2dcc8cSMark Johnston * M_USE_RESERVE is specified. 40521b2dcc8cSMark Johnston */ 40531b2dcc8cSMark Johnston KEG_UNLOCK(keg, slab->us_domain); 40541b2dcc8cSMark Johnston goto out; 40551b2dcc8cSMark Johnston } 4056b6715dabSJeff Roberson #ifdef NUMA 4057ab3185d1SJeff Roberson /* 4058ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 4059ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 4060ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 4061ab3185d1SJeff Roberson * than stripe within each bucket. The current option 4062ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 4063ab3185d1SJeff Roberson * time but yields better distribution. 4064ab3185d1SJeff Roberson */ 4065dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 && 4066ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 4067ab3185d1SJeff Roberson break; 4068ab3185d1SJeff Roberson #endif 40691b2dcc8cSMark Johnston } while (slab->us_freecount != 0 && i < max); 40708b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 40711b2dcc8cSMark Johnston 4072ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 40730095a784SJeff Roberson flags &= ~M_WAITOK; 40740095a784SJeff Roberson flags |= M_NOWAIT; 40750095a784SJeff Roberson } 40761b2dcc8cSMark Johnston out: 40770095a784SJeff Roberson return i; 40780095a784SJeff Roberson } 40790095a784SJeff Roberson 40804bd61e19SJeff Roberson static int 40814bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 40824bd61e19SJeff Roberson { 40834bd61e19SJeff Roberson uint64_t old, new, total, max; 40844bd61e19SJeff Roberson 40854bd61e19SJeff Roberson /* 40864bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 40874bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 40884bd61e19SJeff Roberson * fairness by keeping fifo order. 40894bd61e19SJeff Roberson * 40904bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 40914bd61e19SJeff Roberson */ 40924bd61e19SJeff Roberson for (;;) { 40934bd61e19SJeff Roberson zone_free_limit(zone, count); 40944bd61e19SJeff Roberson zone_log_warning(zone); 40954bd61e19SJeff Roberson zone_maxaction(zone); 40964bd61e19SJeff Roberson if (flags & M_NOWAIT) 40974bd61e19SJeff Roberson return (0); 40984bd61e19SJeff Roberson 40994bd61e19SJeff Roberson /* 41004bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 41014bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 41024bd61e19SJeff Roberson * is essentially a home rolled semaphore. 41034bd61e19SJeff Roberson */ 41044bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 41054bd61e19SJeff Roberson old = zone->uz_items; 41064bd61e19SJeff Roberson do { 41074bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 41084bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 41094bd61e19SJeff Roberson max = zone->uz_max_items; 41104bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 41114bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 41124bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 41134bd61e19SJeff Roberson else 41144bd61e19SJeff Roberson new = old + MIN(count, max - old); 41154bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 41164bd61e19SJeff Roberson 41174bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 41184bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 41194bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 41204bd61e19SJeff Roberson return (new - old); 41214bd61e19SJeff Roberson } 41224bd61e19SJeff Roberson 41234bd61e19SJeff Roberson /* 41244bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 41254bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 41264bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 41274bd61e19SJeff Roberson * transitions. 41284bd61e19SJeff Roberson */ 41294bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 41304bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 41314bd61e19SJeff Roberson 41324bd61e19SJeff Roberson /* 41334bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 41344bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 41354bd61e19SJeff Roberson */ 41364bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 41374bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 41384bd61e19SJeff Roberson 41394bd61e19SJeff Roberson /* 41404bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 41414bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 41424bd61e19SJeff Roberson * 41434bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 41444bd61e19SJeff Roberson * our count. 41454bd61e19SJeff Roberson */ 41464bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 41474bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 41484bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 41494bd61e19SJeff Roberson /* We're no longer a sleeper. */ 41504bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 41514bd61e19SJeff Roberson 41524bd61e19SJeff Roberson /* 41534bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 41544bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 41554bd61e19SJeff Roberson * against changes via sysctl. 41564bd61e19SJeff Roberson */ 41574bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 41584bd61e19SJeff Roberson max = zone->uz_max_items; 41594bd61e19SJeff Roberson if (total >= max) 41604bd61e19SJeff Roberson continue; 41614bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 41624bd61e19SJeff Roberson if (total + count > max) { 41634bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 41644bd61e19SJeff Roberson count = max - total; 41654bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 41664bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 41674bd61e19SJeff Roberson 41684bd61e19SJeff Roberson return (count); 41694bd61e19SJeff Roberson } 41704bd61e19SJeff Roberson } 41714bd61e19SJeff Roberson 41724bd61e19SJeff Roberson /* 41734bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 41744bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 41754bd61e19SJeff Roberson * one item can be allocated. 41764bd61e19SJeff Roberson */ 41774bd61e19SJeff Roberson static int 41784bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 41794bd61e19SJeff Roberson { 41804bd61e19SJeff Roberson uint64_t old; 41814bd61e19SJeff Roberson uint64_t max; 41824bd61e19SJeff Roberson 41834bd61e19SJeff Roberson max = zone->uz_max_items; 41844bd61e19SJeff Roberson MPASS(max > 0); 41854bd61e19SJeff Roberson 41864bd61e19SJeff Roberson /* 41874bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 41884bd61e19SJeff Roberson * fetchadd. 41894bd61e19SJeff Roberson */ 41904bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 41914bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 41924bd61e19SJeff Roberson return (count); 41934bd61e19SJeff Roberson 41944bd61e19SJeff Roberson /* 41954bd61e19SJeff Roberson * If we had some items and no sleepers just return the 41964bd61e19SJeff Roberson * truncated value. We have to release the excess space 41974bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 41984bd61e19SJeff Roberson * because we were temporarily over the limit. 41994bd61e19SJeff Roberson */ 42004bd61e19SJeff Roberson if (old < max) { 42014bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 42024bd61e19SJeff Roberson return (max - old); 42034bd61e19SJeff Roberson } 42044bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 42054bd61e19SJeff Roberson } 42064bd61e19SJeff Roberson 42074bd61e19SJeff Roberson /* 42084bd61e19SJeff Roberson * Free a number of items back to the limit. 42094bd61e19SJeff Roberson */ 42104bd61e19SJeff Roberson static void 42114bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 42124bd61e19SJeff Roberson { 42134bd61e19SJeff Roberson uint64_t old; 42144bd61e19SJeff Roberson 42154bd61e19SJeff Roberson MPASS(count > 0); 42164bd61e19SJeff Roberson 42174bd61e19SJeff Roberson /* 42184bd61e19SJeff Roberson * In the common case we either have no sleepers or 42194bd61e19SJeff Roberson * are still over the limit and can just return. 42204bd61e19SJeff Roberson */ 42214bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 42224bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 42234bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 42244bd61e19SJeff Roberson return; 42254bd61e19SJeff Roberson 42264bd61e19SJeff Roberson /* 42274bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 42284bd61e19SJeff Roberson * to generate wakeups if necessary. 42294bd61e19SJeff Roberson */ 42304bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 42314bd61e19SJeff Roberson } 42324bd61e19SJeff Roberson 4233fc03d22bSJeff Roberson static uma_bucket_t 4234beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 4235bbee39c6SJeff Roberson { 4236bbee39c6SJeff Roberson uma_bucket_t bucket; 423709c8cb71SMark Johnston int error, maxbucket, cnt; 4238bbee39c6SJeff Roberson 4239e63a1c2fSRyan Libby CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name, 4240e63a1c2fSRyan Libby zone, domain); 424130c5525bSAndrew Gallatin 4242c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 4243c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 4244c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 42458c277118SMark Johnston else if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0) 4246c6fd3e23SJeff Roberson domain = UMA_ANYDOMAIN; 4247c1685086SJeff Roberson 42484bd61e19SJeff Roberson if (zone->uz_max_items > 0) 42494bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 42504bd61e19SJeff Roberson M_NOWAIT); 42514bd61e19SJeff Roberson else 425220a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 42534bd61e19SJeff Roberson if (maxbucket == 0) 42544bd61e19SJeff Roberson return (false); 4255beb8beefSJeff Roberson 42566fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 42576fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 4258beb8beefSJeff Roberson if (bucket == NULL) { 4259beb8beefSJeff Roberson cnt = 0; 4260beb8beefSJeff Roberson goto out; 4261beb8beefSJeff Roberson } 42620095a784SJeff Roberson 42630095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 4264beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 42650095a784SJeff Roberson 42660095a784SJeff Roberson /* 42670095a784SJeff Roberson * Initialize the memory if necessary. 42680095a784SJeff Roberson */ 42690095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 4270099a0e58SBosko Milekic int i; 4271bbee39c6SJeff Roberson 427209c8cb71SMark Johnston for (i = 0; i < bucket->ub_cnt; i++) { 427309c8cb71SMark Johnston kasan_mark_item_valid(zone, bucket->ub_bucket[i]); 427409c8cb71SMark Johnston error = zone->uz_init(bucket->ub_bucket[i], 427509c8cb71SMark Johnston zone->uz_size, flags); 427609c8cb71SMark Johnston kasan_mark_item_invalid(zone, bucket->ub_bucket[i]); 427709c8cb71SMark Johnston if (error != 0) 4278b23f72e9SBrian Feldman break; 427909c8cb71SMark Johnston } 428009c8cb71SMark Johnston 4281b23f72e9SBrian Feldman /* 4282b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 4283b23f72e9SBrian Feldman * rest back onto the freelist. 4284b23f72e9SBrian Feldman */ 4285b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 4286af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 42870095a784SJeff Roberson bucket->ub_cnt - i); 4288a5a262c6SBosko Milekic #ifdef INVARIANTS 42890095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 42900095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 4291a5a262c6SBosko Milekic #endif 4292b23f72e9SBrian Feldman bucket->ub_cnt = i; 4293b23f72e9SBrian Feldman } 4294099a0e58SBosko Milekic } 4295099a0e58SBosko Milekic 4296beb8beefSJeff Roberson cnt = bucket->ub_cnt; 4297f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 42986fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 42992efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 4300beb8beefSJeff Roberson bucket = NULL; 4301beb8beefSJeff Roberson } 4302beb8beefSJeff Roberson out: 43034bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 43044bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 4305fc03d22bSJeff Roberson 4306fc03d22bSJeff Roberson return (bucket); 4307fc03d22bSJeff Roberson } 4308fc03d22bSJeff Roberson 43098355f576SJeff Roberson /* 43100095a784SJeff Roberson * Allocates a single item from a zone. 43118355f576SJeff Roberson * 43128355f576SJeff Roberson * Arguments 43138355f576SJeff Roberson * zone The zone to alloc for. 43148355f576SJeff Roberson * udata The data to be passed to the constructor. 4315ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 4316a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 43178355f576SJeff Roberson * 43188355f576SJeff Roberson * Returns 43198355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 4320bbee39c6SJeff Roberson * An item if successful 43218355f576SJeff Roberson */ 43228355f576SJeff Roberson 43238355f576SJeff Roberson static void * 4324ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 43258355f576SJeff Roberson { 43268355f576SJeff Roberson void *item; 43278355f576SJeff Roberson 4328791dda87SAndrew Gallatin if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) { 4329791dda87SAndrew Gallatin counter_u64_add(zone->uz_fails, 1); 4330bb15d1c7SGleb Smirnoff return (NULL); 4331791dda87SAndrew Gallatin } 43328355f576SJeff Roberson 4333c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 4334c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 433530c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 4336c1685086SJeff Roberson 4337ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 4338beb8beefSJeff Roberson goto fail_cnt; 43398355f576SJeff Roberson 4340099a0e58SBosko Milekic /* 4341099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 4342099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 4343099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 4344099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 4345099a0e58SBosko Milekic */ 4346b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 434709c8cb71SMark Johnston int error; 434809c8cb71SMark Johnston 434909c8cb71SMark Johnston kasan_mark_item_valid(zone, item); 435009c8cb71SMark Johnston error = zone->uz_init(item, zone->uz_size, flags); 435109c8cb71SMark Johnston kasan_mark_item_invalid(zone, item); 435209c8cb71SMark Johnston if (error != 0) { 4353bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 4354beb8beefSJeff Roberson goto fail_cnt; 4355beb8beefSJeff Roberson } 4356beb8beefSJeff Roberson } 4357d4665eaaSJeff Roberson item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags, 4358d4665eaaSJeff Roberson item); 4359beb8beefSJeff Roberson if (item == NULL) 43600095a784SJeff Roberson goto fail; 43618355f576SJeff Roberson 43622efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 43631431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 43641431a748SGleb Smirnoff zone->uz_name, zone); 43651431a748SGleb Smirnoff 43668355f576SJeff Roberson return (item); 43670095a784SJeff Roberson 4368beb8beefSJeff Roberson fail_cnt: 4369beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 43700095a784SJeff Roberson fail: 43714bd61e19SJeff Roberson if (zone->uz_max_items > 0) 43724bd61e19SJeff Roberson zone_free_limit(zone, 1); 43731431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 43741431a748SGleb Smirnoff zone->uz_name, zone); 43754bd61e19SJeff Roberson 43760095a784SJeff Roberson return (NULL); 43778355f576SJeff Roberson } 43788355f576SJeff Roberson 43798355f576SJeff Roberson /* See uma.h */ 43808355f576SJeff Roberson void 4381d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item) 4382d4665eaaSJeff Roberson { 4383d4665eaaSJeff Roberson uma_cache_t cache; 4384d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 4385c6fd3e23SJeff Roberson int itemdomain, uz_flags; 4386d4665eaaSJeff Roberson 4387841e0a87SGleb Smirnoff CTR3(KTR_UMA, "uma_zfree_smr zone %s(%p) item %p", 4388841e0a87SGleb Smirnoff zone->uz_name, zone, item); 4389841e0a87SGleb Smirnoff 4390d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 4391d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 4392952c8964SMark Johnston ("uma_zfree_smr: called with non-SMR zone.")); 4393d4665eaaSJeff Roberson KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer.")); 4394c6fd3e23SJeff Roberson SMR_ASSERT_NOT_ENTERED(zone->uz_smr); 4395d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN) 4396d4665eaaSJeff Roberson return; 4397d4665eaaSJeff Roberson #endif 4398d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4399d4665eaaSJeff Roberson uz_flags = cache_uz_flags(cache); 4400c6fd3e23SJeff Roberson itemdomain = 0; 4401d4665eaaSJeff Roberson #ifdef NUMA 4402d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 440381302f1dSMark Johnston itemdomain = item_domain(item); 4404d4665eaaSJeff Roberson #endif 4405d4665eaaSJeff Roberson critical_enter(); 4406d4665eaaSJeff Roberson do { 4407d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4408d4665eaaSJeff Roberson /* SMR Zones must free to the free bucket. */ 4409d4665eaaSJeff Roberson bucket = &cache->uc_freebucket; 4410d4665eaaSJeff Roberson #ifdef NUMA 4411d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4412c6fd3e23SJeff Roberson PCPU_GET(domain) != itemdomain) { 4413d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 4414d4665eaaSJeff Roberson } 4415d4665eaaSJeff Roberson #endif 4416d4665eaaSJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 4417d4665eaaSJeff Roberson cache_bucket_push(cache, bucket, item); 4418d4665eaaSJeff Roberson critical_exit(); 4419d4665eaaSJeff Roberson return; 4420d4665eaaSJeff Roberson } 44212cb67bd7SGleb Smirnoff } while (cache_free(zone, cache, NULL, itemdomain)); 4422d4665eaaSJeff Roberson critical_exit(); 4423d4665eaaSJeff Roberson 4424d4665eaaSJeff Roberson /* 4425d4665eaaSJeff Roberson * If nothing else caught this, we'll just do an internal free. 4426d4665eaaSJeff Roberson */ 4427d4665eaaSJeff Roberson zone_free_item(zone, item, NULL, SKIP_NONE); 4428d4665eaaSJeff Roberson } 4429d4665eaaSJeff Roberson 4430d4665eaaSJeff Roberson /* See uma.h */ 4431d4665eaaSJeff Roberson void 44328355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 44338355f576SJeff Roberson { 44348355f576SJeff Roberson uma_cache_t cache; 4435376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 4436c6fd3e23SJeff Roberson int itemdomain, uz_flags; 44378355f576SJeff Roberson 4438e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 443919fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 444010cb2424SMark Murray 444128782f73SGleb Smirnoff CTR3(KTR_UMA, "uma_zfree_arg zone %s(%p) item %p", 444228782f73SGleb Smirnoff zone->uz_name, zone, item); 44433659f747SRobert Watson 4444d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 4445d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 4446952c8964SMark Johnston ("uma_zfree_arg: called with SMR zone.")); 4447d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN) 4448d4665eaaSJeff Roberson return; 4449d4665eaaSJeff Roberson #endif 445020ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 445120ed0cb0SMatthew D Fleming if (item == NULL) 445220ed0cb0SMatthew D Fleming return; 4453cc7ce83aSJeff Roberson 4454cc7ce83aSJeff Roberson /* 4455cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 4456cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 4457cc7ce83aSJeff Roberson * will simply read another cpu's line. 4458cc7ce83aSJeff Roberson */ 4459cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4460cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 4461d4665eaaSJeff Roberson if (UMA_ALWAYS_CTORDTOR || 4462d4665eaaSJeff Roberson __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0)) 4463cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 4464ef72505eSJeff Roberson 4465af7f9b97SJeff Roberson /* 4466af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 4467af7f9b97SJeff Roberson * a little longer for the limits to be reset. 4468af7f9b97SJeff Roberson */ 4469cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 44708a6776caSMark Johnston if (atomic_load_32(&zone->uz_sleepers) > 0) 4471fc03d22bSJeff Roberson goto zfree_item; 4472cc7ce83aSJeff Roberson } 4473af7f9b97SJeff Roberson 44745d1ae027SRobert Watson /* 44755d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 44765d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 44775d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 44785d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 44795d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 44805d1ae027SRobert Watson * preemption and migration. We release the critical section in 44815d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 44825d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 44835d1ae027SRobert Watson * detect and handle migration if it has occurred. 44845d1ae027SRobert Watson */ 4485c6fd3e23SJeff Roberson itemdomain = 0; 4486dfe13344SJeff Roberson #ifdef NUMA 4487dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 448881302f1dSMark Johnston itemdomain = item_domain(item); 4489dfe13344SJeff Roberson #endif 44905d1ae027SRobert Watson critical_enter(); 44910a81b439SJeff Roberson do { 4492cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4493a553d4b8SJeff Roberson /* 4494dfe13344SJeff Roberson * Try to free into the allocbucket first to give LIFO 4495dfe13344SJeff Roberson * ordering for cache-hot datastructures. Spill over 4496dfe13344SJeff Roberson * into the freebucket if necessary. Alloc will swap 4497dfe13344SJeff Roberson * them if one runs dry. 4498a553d4b8SJeff Roberson */ 4499dfe13344SJeff Roberson bucket = &cache->uc_allocbucket; 4500d4665eaaSJeff Roberson #ifdef NUMA 4501d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4502c6fd3e23SJeff Roberson PCPU_GET(domain) != itemdomain) { 4503d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 4504d4665eaaSJeff Roberson } else 4505d4665eaaSJeff Roberson #endif 4506fe835cbfSJeff Roberson if (bucket->ucb_cnt == bucket->ucb_entries && 4507fe835cbfSJeff Roberson cache->uc_freebucket.ucb_cnt < 4508fe835cbfSJeff Roberson cache->uc_freebucket.ucb_entries) 4509fe835cbfSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 4510fe835cbfSJeff Roberson &cache->uc_allocbucket); 4511376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 4512376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 45135d1ae027SRobert Watson critical_exit(); 45148355f576SJeff Roberson return; 4515fc03d22bSJeff Roberson } 45162cb67bd7SGleb Smirnoff } while (cache_free(zone, cache, udata, itemdomain)); 45170a81b439SJeff Roberson critical_exit(); 4518fc03d22bSJeff Roberson 45198355f576SJeff Roberson /* 45200a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 45218355f576SJeff Roberson */ 45220a81b439SJeff Roberson zfree_item: 45230a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 45240a81b439SJeff Roberson } 4525fc03d22bSJeff Roberson 4526dfe13344SJeff Roberson #ifdef NUMA 452791d947bfSJeff Roberson /* 452891d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 452991d947bfSJeff Roberson * them. 453091d947bfSJeff Roberson */ 453191d947bfSJeff Roberson static void 453291d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 453391d947bfSJeff Roberson { 4534991f23efSMark Johnston struct uma_bucketlist emptybuckets, fullbuckets; 453591d947bfSJeff Roberson uma_zone_domain_t zdom; 453691d947bfSJeff Roberson uma_bucket_t b; 4537543117beSJeff Roberson smr_seq_t seq; 453891d947bfSJeff Roberson void *item; 453991d947bfSJeff Roberson int domain; 454091d947bfSJeff Roberson 454191d947bfSJeff Roberson CTR3(KTR_UMA, 454291d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 454391d947bfSJeff Roberson zone->uz_name, zone, bucket); 454491d947bfSJeff Roberson 4545543117beSJeff Roberson /* 4546543117beSJeff Roberson * It is possible for buckets to arrive here out of order so we fetch 4547543117beSJeff Roberson * the current smr seq rather than accepting the bucket's. 4548543117beSJeff Roberson */ 4549543117beSJeff Roberson seq = SMR_SEQ_INVALID; 4550543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 4551226dd6dbSJeff Roberson seq = smr_advance(zone->uz_smr); 4552226dd6dbSJeff Roberson 4553226dd6dbSJeff Roberson /* 4554226dd6dbSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 4555226dd6dbSJeff Roberson * lock on the current crossfree bucket. A full matrix with 4556226dd6dbSJeff Roberson * per-domain locking could be used if necessary. 4557226dd6dbSJeff Roberson */ 4558991f23efSMark Johnston STAILQ_INIT(&emptybuckets); 4559226dd6dbSJeff Roberson STAILQ_INIT(&fullbuckets); 4560226dd6dbSJeff Roberson ZONE_CROSS_LOCK(zone); 4561991f23efSMark Johnston for (; bucket->ub_cnt > 0; bucket->ub_cnt--) { 456291d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 456381302f1dSMark Johnston domain = item_domain(item); 4564c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 456591d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 4566991f23efSMark Johnston if ((b = STAILQ_FIRST(&emptybuckets)) != NULL) { 4567991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4568991f23efSMark Johnston zdom->uzd_cross = b; 4569991f23efSMark Johnston } else { 4570991f23efSMark Johnston /* 4571991f23efSMark Johnston * Avoid allocating a bucket with the cross lock 4572991f23efSMark Johnston * held, since allocation can trigger a 4573991f23efSMark Johnston * cross-domain free and bucket zones may 4574991f23efSMark Johnston * allocate from each other. 4575991f23efSMark Johnston */ 4576991f23efSMark Johnston ZONE_CROSS_UNLOCK(zone); 4577991f23efSMark Johnston b = bucket_alloc(zone, udata, M_NOWAIT); 4578991f23efSMark Johnston if (b == NULL) 4579991f23efSMark Johnston goto out; 4580991f23efSMark Johnston ZONE_CROSS_LOCK(zone); 4581991f23efSMark Johnston if (zdom->uzd_cross != NULL) { 4582991f23efSMark Johnston STAILQ_INSERT_HEAD(&emptybuckets, b, 4583991f23efSMark Johnston ub_link); 4584991f23efSMark Johnston } else { 4585991f23efSMark Johnston zdom->uzd_cross = b; 4586991f23efSMark Johnston } 4587991f23efSMark Johnston } 458891d947bfSJeff Roberson } 4589543117beSJeff Roberson b = zdom->uzd_cross; 4590543117beSJeff Roberson b->ub_bucket[b->ub_cnt++] = item; 4591543117beSJeff Roberson b->ub_seq = seq; 4592543117beSJeff Roberson if (b->ub_cnt == b->ub_entries) { 4593543117beSJeff Roberson STAILQ_INSERT_HEAD(&fullbuckets, b, ub_link); 4594991f23efSMark Johnston if ((b = STAILQ_FIRST(&emptybuckets)) != NULL) 4595991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4596991f23efSMark Johnston zdom->uzd_cross = b; 459791d947bfSJeff Roberson } 459891d947bfSJeff Roberson } 459991d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 4600991f23efSMark Johnston out: 4601c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 4602d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 460391d947bfSJeff Roberson bucket_free(zone, bucket, udata); 4604c6fd3e23SJeff Roberson 4605991f23efSMark Johnston while ((b = STAILQ_FIRST(&emptybuckets)) != NULL) { 4606991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4607991f23efSMark Johnston bucket_free(zone, b, udata); 4608991f23efSMark Johnston } 4609c6fd3e23SJeff Roberson while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) { 4610c6fd3e23SJeff Roberson STAILQ_REMOVE_HEAD(&fullbuckets, ub_link); 461181302f1dSMark Johnston domain = item_domain(b->ub_bucket[0]); 4612c6fd3e23SJeff Roberson zone_put_bucket(zone, domain, b, udata, true); 4613c6fd3e23SJeff Roberson } 461491d947bfSJeff Roberson } 461591d947bfSJeff Roberson #endif 461691d947bfSJeff Roberson 46170a81b439SJeff Roberson static void 46180a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 4619c6fd3e23SJeff Roberson int itemdomain, bool ws) 46200a81b439SJeff Roberson { 46210a81b439SJeff Roberson 4622dfe13344SJeff Roberson #ifdef NUMA 46230a81b439SJeff Roberson /* 46240a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 46250a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 46260a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 462791d947bfSJeff Roberson * correct domains. 46280a81b439SJeff Roberson */ 4629c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4630c6fd3e23SJeff Roberson vm_ndomains > 2 && PCPU_GET(domain) != itemdomain) { 463191d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 46320a81b439SJeff Roberson return; 46330a81b439SJeff Roberson } 46340a81b439SJeff Roberson #endif 463591d947bfSJeff Roberson 46360a81b439SJeff Roberson /* 46370a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 46380a81b439SJeff Roberson */ 46390a81b439SJeff Roberson CTR3(KTR_UMA, 46400a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 46410a81b439SJeff Roberson zone->uz_name, zone, bucket); 46420a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 4643c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0) 4644c6fd3e23SJeff Roberson itemdomain = zone_domain_lowest(zone, itemdomain); 4645c6fd3e23SJeff Roberson zone_put_bucket(zone, itemdomain, bucket, udata, ws); 46468355f576SJeff Roberson } 4647fc03d22bSJeff Roberson 46484d104ba0SAlexander Motin /* 46490a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 46500a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 46510a81b439SJeff Roberson * 46520a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 46530a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 46540a81b439SJeff Roberson * the caller should retry. 46554d104ba0SAlexander Motin */ 46560a81b439SJeff Roberson static __noinline bool 46572cb67bd7SGleb Smirnoff cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, int itemdomain) 46580a81b439SJeff Roberson { 4659dfe13344SJeff Roberson uma_cache_bucket_t cbucket; 4660d4665eaaSJeff Roberson uma_bucket_t newbucket, bucket; 46610a81b439SJeff Roberson 46620a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 46630a81b439SJeff Roberson 4664d4665eaaSJeff Roberson if (zone->uz_bucket_size == 0) 46650a81b439SJeff Roberson return false; 46660a81b439SJeff Roberson 4667cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4668d4665eaaSJeff Roberson newbucket = NULL; 46690a81b439SJeff Roberson 46700a81b439SJeff Roberson /* 4671dfe13344SJeff Roberson * FIRSTTOUCH domains need to free to the correct zdom. When 4672dfe13344SJeff Roberson * enabled this is the zdom of the item. The bucket is the 4673dfe13344SJeff Roberson * cross bucket if the current domain and itemdomain do not match. 46740a81b439SJeff Roberson */ 4675dfe13344SJeff Roberson cbucket = &cache->uc_freebucket; 4676dfe13344SJeff Roberson #ifdef NUMA 4677c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) { 4678c6fd3e23SJeff Roberson if (PCPU_GET(domain) != itemdomain) { 4679dfe13344SJeff Roberson cbucket = &cache->uc_crossbucket; 4680dfe13344SJeff Roberson if (cbucket->ucb_cnt != 0) 4681c6fd3e23SJeff Roberson counter_u64_add(zone->uz_xdomain, 4682dfe13344SJeff Roberson cbucket->ucb_cnt); 4683dfe13344SJeff Roberson } 4684c6fd3e23SJeff Roberson } 46850a81b439SJeff Roberson #endif 4686dfe13344SJeff Roberson bucket = cache_bucket_unload(cbucket); 4687c6fd3e23SJeff Roberson KASSERT(bucket == NULL || bucket->ub_cnt == bucket->ub_entries, 4688c6fd3e23SJeff Roberson ("cache_free: Entered with non-full free bucket.")); 46890a81b439SJeff Roberson 46900a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 46910a81b439SJeff Roberson critical_exit(); 46920a81b439SJeff Roberson 4693d4665eaaSJeff Roberson /* 4694d4665eaaSJeff Roberson * Don't let SMR zones operate without a free bucket. Force 4695d4665eaaSJeff Roberson * a synchronize and re-use this one. We will only degrade 4696d4665eaaSJeff Roberson * to a synchronize every bucket_size items rather than every 4697d4665eaaSJeff Roberson * item if we fail to allocate a bucket. 4698d4665eaaSJeff Roberson */ 4699d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) { 4700d4665eaaSJeff Roberson if (bucket != NULL) 4701d4665eaaSJeff Roberson bucket->ub_seq = smr_advance(zone->uz_smr); 4702d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4703d4665eaaSJeff Roberson if (newbucket == NULL && bucket != NULL) { 4704d4665eaaSJeff Roberson bucket_drain(zone, bucket); 4705d4665eaaSJeff Roberson newbucket = bucket; 4706d4665eaaSJeff Roberson bucket = NULL; 4707d4665eaaSJeff Roberson } 4708d4665eaaSJeff Roberson } else if (!bucketdisable) 4709d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4710d4665eaaSJeff Roberson 47110a81b439SJeff Roberson if (bucket != NULL) 4712c6fd3e23SJeff Roberson zone_free_bucket(zone, bucket, udata, itemdomain, true); 4713a553d4b8SJeff Roberson 4714fc03d22bSJeff Roberson critical_enter(); 4715d4665eaaSJeff Roberson if ((bucket = newbucket) == NULL) 47160a81b439SJeff Roberson return (false); 4717cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4718dfe13344SJeff Roberson #ifdef NUMA 4719fc03d22bSJeff Roberson /* 47200a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 47210a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 47220a81b439SJeff Roberson * the free bucket. 4723fc03d22bSJeff Roberson */ 4724c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) { 4725c6fd3e23SJeff Roberson if (PCPU_GET(domain) != itemdomain && 4726376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 4727376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 47280a81b439SJeff Roberson return (true); 47290a81b439SJeff Roberson } 47300a81b439SJeff Roberson } 47310a81b439SJeff Roberson #endif 47320a81b439SJeff Roberson /* 47330a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 47340a81b439SJeff Roberson */ 4735376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 4736fc03d22bSJeff Roberson critical_exit(); 47376fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 47380a81b439SJeff Roberson critical_enter(); 47390a81b439SJeff Roberson } else 4740376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 47418355f576SJeff Roberson 47420a81b439SJeff Roberson return (true); 47438355f576SJeff Roberson } 47448355f576SJeff Roberson 47458355f576SJeff Roberson static void 4746bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 47478355f576SJeff Roberson { 4748bb15d1c7SGleb Smirnoff uma_keg_t keg; 4749ab3185d1SJeff Roberson uma_domain_t dom; 47509b8db4d0SRyan Libby int freei; 4751099a0e58SBosko Milekic 4752bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 47538b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 4754ab3185d1SJeff Roberson 47558355f576SJeff Roberson /* Do we need to remove from any lists? */ 47568b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 4757099a0e58SBosko Milekic if (slab->us_freecount + 1 == keg->uk_ipers) { 47588355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4759ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 47604ab3aee8SMark Johnston dom->ud_free_slabs++; 47618355f576SJeff Roberson } else if (slab->us_freecount == 0) { 47628355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4763ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 47648355f576SJeff Roberson } 47658355f576SJeff Roberson 4766ef72505eSJeff Roberson /* Slab management. */ 47671e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 47689b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 47698355f576SJeff Roberson slab->us_freecount++; 47708355f576SJeff Roberson 4771ef72505eSJeff Roberson /* Keg statistics. */ 47724ab3aee8SMark Johnston dom->ud_free_items++; 47730095a784SJeff Roberson } 47740095a784SJeff Roberson 47750095a784SJeff Roberson static void 4776b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 47770095a784SJeff Roberson { 47788b987a77SJeff Roberson struct mtx *lock; 4779b75c4efcSAndrew Turner uma_zone_t zone; 47800095a784SJeff Roberson uma_slab_t slab; 47810095a784SJeff Roberson uma_keg_t keg; 47820095a784SJeff Roberson uint8_t *mem; 47838b987a77SJeff Roberson void *item; 47840095a784SJeff Roberson int i; 47858355f576SJeff Roberson 4786b75c4efcSAndrew Turner zone = arg; 4787bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 47888b987a77SJeff Roberson lock = NULL; 478954c5ae80SRyan Libby if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0)) 47908b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 47910095a784SJeff Roberson for (i = 0; i < cnt; i++) { 47920095a784SJeff Roberson item = bucket[i]; 479354c5ae80SRyan Libby if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) { 47940095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 47958b987a77SJeff Roberson } else { 47968b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 479754c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0) 47988b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 47998b987a77SJeff Roberson else 48008b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 48018b987a77SJeff Roberson } 48028b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 48038b987a77SJeff Roberson if (lock != NULL) 48048b987a77SJeff Roberson mtx_unlock(lock); 48058b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 48068b987a77SJeff Roberson } 4807bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 48080095a784SJeff Roberson } 48098b987a77SJeff Roberson if (lock != NULL) 48108b987a77SJeff Roberson mtx_unlock(lock); 48118355f576SJeff Roberson } 48128355f576SJeff Roberson 48130095a784SJeff Roberson /* 48140095a784SJeff Roberson * Frees a single item to any zone. 48150095a784SJeff Roberson * 48160095a784SJeff Roberson * Arguments: 48170095a784SJeff Roberson * zone The zone to free to 48180095a784SJeff Roberson * item The item we're freeing 48190095a784SJeff Roberson * udata User supplied data for the dtor 48200095a784SJeff Roberson * skip Skip dtors and finis 48210095a784SJeff Roberson */ 48226d88d784SJeff Roberson static __noinline void 48230095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 48240095a784SJeff Roberson { 4825c5deaf04SGleb Smirnoff 4826d4665eaaSJeff Roberson /* 4827d4665eaaSJeff Roberson * If a free is sent directly to an SMR zone we have to 4828d4665eaaSJeff Roberson * synchronize immediately because the item can instantly 4829d4665eaaSJeff Roberson * be reallocated. This should only happen in degenerate 4830d4665eaaSJeff Roberson * cases when no memory is available for per-cpu caches. 4831d4665eaaSJeff Roberson */ 4832d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE) 4833d4665eaaSJeff Roberson smr_synchronize(zone->uz_smr); 4834d4665eaaSJeff Roberson 4835cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 48360095a784SJeff Roberson 483709c8cb71SMark Johnston if (skip < SKIP_FINI && zone->uz_fini) { 483809c8cb71SMark Johnston kasan_mark_item_valid(zone, item); 48390095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 484009c8cb71SMark Johnston kasan_mark_item_invalid(zone, item); 484109c8cb71SMark Johnston } 48420095a784SJeff Roberson 48430095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4844bb15d1c7SGleb Smirnoff 4845bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4846bb15d1c7SGleb Smirnoff return; 4847bb15d1c7SGleb Smirnoff 48482efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 48492efcc8cbSGleb Smirnoff 48504bd61e19SJeff Roberson if (zone->uz_max_items > 0) 48514bd61e19SJeff Roberson zone_free_limit(zone, 1); 4852bb45b411SGleb Smirnoff } 48530095a784SJeff Roberson 48548355f576SJeff Roberson /* See uma.h */ 48551c6cae97SLawrence Stewart int 4856736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4857736ee590SJeff Roberson { 4858e574d407SMark Johnston 4859e574d407SMark Johnston /* 4860e574d407SMark Johnston * If the limit is small, we may need to constrain the maximum per-CPU 4861e574d407SMark Johnston * cache size, or disable caching entirely. 4862e574d407SMark Johnston */ 4863e574d407SMark Johnston uma_zone_set_maxcache(zone, nitems); 4864bb15d1c7SGleb Smirnoff 48654bd61e19SJeff Roberson /* 48664bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 48674bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 48684bd61e19SJeff Roberson * way to clear a limit. 48694bd61e19SJeff Roberson */ 4870bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4871bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4872cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4873cc7ce83aSJeff Roberson zone_update_caches(zone); 48744bd61e19SJeff Roberson /* We may need to wake waiters. */ 48754bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4876bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4877bb15d1c7SGleb Smirnoff 4878bb15d1c7SGleb Smirnoff return (nitems); 4879bb15d1c7SGleb Smirnoff } 4880bb15d1c7SGleb Smirnoff 4881bb15d1c7SGleb Smirnoff /* See uma.h */ 4882003cf08bSMark Johnston void 4883bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4884bb15d1c7SGleb Smirnoff { 4885e574d407SMark Johnston int bpcpu, bpdom, bsize, nb; 4886bb15d1c7SGleb Smirnoff 4887bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4888e574d407SMark Johnston 4889e574d407SMark Johnston /* 4890e574d407SMark Johnston * Compute a lower bound on the number of items that may be cached in 4891e574d407SMark Johnston * the zone. Each CPU gets at least two buckets, and for cross-domain 4892e574d407SMark Johnston * frees we use an additional bucket per CPU and per domain. Select the 4893e574d407SMark Johnston * largest bucket size that does not exceed half of the requested limit, 4894e574d407SMark Johnston * with the left over space given to the full bucket cache. 4895e574d407SMark Johnston */ 4896e574d407SMark Johnston bpdom = 0; 4897003cf08bSMark Johnston bpcpu = 2; 4898e574d407SMark Johnston #ifdef NUMA 4899e574d407SMark Johnston if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && vm_ndomains > 1) { 4900003cf08bSMark Johnston bpcpu++; 4901e574d407SMark Johnston bpdom++; 4902003cf08bSMark Johnston } 4903e574d407SMark Johnston #endif 4904e574d407SMark Johnston nb = bpcpu * mp_ncpus + bpdom * vm_ndomains; 4905e574d407SMark Johnston bsize = nitems / nb / 2; 4906e574d407SMark Johnston if (bsize > BUCKET_MAX) 4907e574d407SMark Johnston bsize = BUCKET_MAX; 4908e574d407SMark Johnston else if (bsize == 0 && nitems / nb > 0) 4909e574d407SMark Johnston bsize = 1; 4910e574d407SMark Johnston zone->uz_bucket_size_max = zone->uz_bucket_size = bsize; 491120a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 491220a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4913e574d407SMark Johnston zone->uz_bucket_max = nitems - nb * bsize; 4914bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4915736ee590SJeff Roberson } 4916736ee590SJeff Roberson 4917736ee590SJeff Roberson /* See uma.h */ 4918e49471b0SAndre Oppermann int 4919e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4920e49471b0SAndre Oppermann { 4921e49471b0SAndre Oppermann int nitems; 4922e49471b0SAndre Oppermann 4923727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4924e49471b0SAndre Oppermann 4925e49471b0SAndre Oppermann return (nitems); 4926e49471b0SAndre Oppermann } 4927e49471b0SAndre Oppermann 4928e49471b0SAndre Oppermann /* See uma.h */ 49292f891cd5SPawel Jakub Dawidek void 49302f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 49312f891cd5SPawel Jakub Dawidek { 49322f891cd5SPawel Jakub Dawidek 4933727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 49342f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 49352f891cd5SPawel Jakub Dawidek } 49362f891cd5SPawel Jakub Dawidek 49372f891cd5SPawel Jakub Dawidek /* See uma.h */ 493854503a13SJonathan T. Looney void 493954503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 494054503a13SJonathan T. Looney { 494154503a13SJonathan T. Looney 4942727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4943e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 494454503a13SJonathan T. Looney } 494554503a13SJonathan T. Looney 494654503a13SJonathan T. Looney /* See uma.h */ 4947c4ae7908SLawrence Stewart int 4948c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4949c4ae7908SLawrence Stewart { 4950c4ae7908SLawrence Stewart int64_t nitems; 4951c4ae7908SLawrence Stewart u_int i; 4952c4ae7908SLawrence Stewart 4953bfb6b7a1SJeff Roberson nitems = 0; 4954bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER) 49552efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 49562efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4957727c6918SJeff Roberson CPU_FOREACH(i) 4958727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4959727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4960c4ae7908SLawrence Stewart 4961c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4962c4ae7908SLawrence Stewart } 4963c4ae7908SLawrence Stewart 496420a4e154SJeff Roberson static uint64_t 496520a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 496620a4e154SJeff Roberson { 496720a4e154SJeff Roberson uint64_t nitems; 496820a4e154SJeff Roberson u_int i; 496920a4e154SJeff Roberson 4970bfb6b7a1SJeff Roberson nitems = 0; 4971bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER) 497220a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4973727c6918SJeff Roberson CPU_FOREACH(i) 4974727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 497520a4e154SJeff Roberson 497620a4e154SJeff Roberson return (nitems); 497720a4e154SJeff Roberson } 497820a4e154SJeff Roberson 497920a4e154SJeff Roberson static uint64_t 498020a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 498120a4e154SJeff Roberson { 498220a4e154SJeff Roberson uint64_t nitems; 498320a4e154SJeff Roberson u_int i; 498420a4e154SJeff Roberson 4985bfb6b7a1SJeff Roberson nitems = 0; 4986bfb6b7a1SJeff Roberson if (zone->uz_frees != EARLY_COUNTER) 498720a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4988727c6918SJeff Roberson CPU_FOREACH(i) 4989727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 499020a4e154SJeff Roberson 499120a4e154SJeff Roberson return (nitems); 499220a4e154SJeff Roberson } 499320a4e154SJeff Roberson 499431c251a0SJeff Roberson #ifdef INVARIANTS 499531c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */ 499631c251a0SJeff Roberson static uint64_t 499731c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg) 499831c251a0SJeff Roberson { 499931c251a0SJeff Roberson uma_zone_t z; 500031c251a0SJeff Roberson uint64_t nitems; 500131c251a0SJeff Roberson 500231c251a0SJeff Roberson nitems = 0; 500331c251a0SJeff Roberson LIST_FOREACH(z, &keg->uk_zones, uz_link) 500431c251a0SJeff Roberson nitems += uma_zone_get_allocs(z); 500531c251a0SJeff Roberson 500631c251a0SJeff Roberson return (nitems); 500731c251a0SJeff Roberson } 500831c251a0SJeff Roberson #endif 500931c251a0SJeff Roberson 5010c4ae7908SLawrence Stewart /* See uma.h */ 5011736ee590SJeff Roberson void 5012099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 5013099a0e58SBosko Milekic { 5014e20a199fSJeff Roberson uma_keg_t keg; 5015e20a199fSJeff Roberson 5016bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5017727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 5018e20a199fSJeff Roberson keg->uk_init = uminit; 5019099a0e58SBosko Milekic } 5020099a0e58SBosko Milekic 5021099a0e58SBosko Milekic /* See uma.h */ 5022099a0e58SBosko Milekic void 5023099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 5024099a0e58SBosko Milekic { 5025e20a199fSJeff Roberson uma_keg_t keg; 5026e20a199fSJeff Roberson 5027bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5028727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 5029e20a199fSJeff Roberson keg->uk_fini = fini; 5030099a0e58SBosko Milekic } 5031099a0e58SBosko Milekic 5032099a0e58SBosko Milekic /* See uma.h */ 5033099a0e58SBosko Milekic void 5034099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 5035099a0e58SBosko Milekic { 5036af526374SJeff Roberson 5037727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 5038099a0e58SBosko Milekic zone->uz_init = zinit; 5039099a0e58SBosko Milekic } 5040099a0e58SBosko Milekic 5041099a0e58SBosko Milekic /* See uma.h */ 5042099a0e58SBosko Milekic void 5043099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 5044099a0e58SBosko Milekic { 5045af526374SJeff Roberson 5046727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 5047099a0e58SBosko Milekic zone->uz_fini = zfini; 5048099a0e58SBosko Milekic } 5049099a0e58SBosko Milekic 5050099a0e58SBosko Milekic /* See uma.h */ 5051099a0e58SBosko Milekic void 50528355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 50538355f576SJeff Roberson { 50540095a784SJeff Roberson uma_keg_t keg; 5055e20a199fSJeff Roberson 5056bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5057727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 50580095a784SJeff Roberson keg->uk_freef = freef; 50598355f576SJeff Roberson } 50608355f576SJeff Roberson 50618355f576SJeff Roberson /* See uma.h */ 50628355f576SJeff Roberson void 50638355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 50648355f576SJeff Roberson { 5065e20a199fSJeff Roberson uma_keg_t keg; 5066e20a199fSJeff Roberson 5067bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5068727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 5069e20a199fSJeff Roberson keg->uk_allocf = allocf; 50708355f576SJeff Roberson } 50718355f576SJeff Roberson 50728355f576SJeff Roberson /* See uma.h */ 50736fd34d6fSJeff Roberson void 5074d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr) 5075d4665eaaSJeff Roberson { 5076d4665eaaSJeff Roberson 5077d4665eaaSJeff Roberson ZONE_ASSERT_COLD(zone); 5078d4665eaaSJeff Roberson 50797f746c9fSMateusz Guzik KASSERT(smr != NULL, ("Got NULL smr")); 50807f746c9fSMateusz Guzik KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 50817f746c9fSMateusz Guzik ("zone %p (%s) already uses SMR", zone, zone->uz_name)); 5082d4665eaaSJeff Roberson zone->uz_flags |= UMA_ZONE_SMR; 5083d4665eaaSJeff Roberson zone->uz_smr = smr; 5084d4665eaaSJeff Roberson zone_update_caches(zone); 5085d4665eaaSJeff Roberson } 5086d4665eaaSJeff Roberson 5087d4665eaaSJeff Roberson smr_t 5088d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone) 5089d4665eaaSJeff Roberson { 5090d4665eaaSJeff Roberson 5091d4665eaaSJeff Roberson return (zone->uz_smr); 5092d4665eaaSJeff Roberson } 5093d4665eaaSJeff Roberson 5094d4665eaaSJeff Roberson /* See uma.h */ 5095d4665eaaSJeff Roberson void 50966fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 50976fd34d6fSJeff Roberson { 50986fd34d6fSJeff Roberson uma_keg_t keg; 50996fd34d6fSJeff Roberson 5100bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5101727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 51026fd34d6fSJeff Roberson keg->uk_reserve = items; 51036fd34d6fSJeff Roberson } 51046fd34d6fSJeff Roberson 51056fd34d6fSJeff Roberson /* See uma.h */ 51068355f576SJeff Roberson int 5107a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 51088355f576SJeff Roberson { 5109099a0e58SBosko Milekic uma_keg_t keg; 51108355f576SJeff Roberson vm_offset_t kva; 51119ba30bcbSZbigniew Bodek u_int pages; 51128355f576SJeff Roberson 5113bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 5114727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 5115727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 51168355f576SJeff Roberson 511779c9f942SJeff Roberson pages = howmany(count, keg->uk_ipers) * keg->uk_ppera; 5118a553d4b8SJeff Roberson 5119a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 5120a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 5121a4915c21SAttilio Rao #else 5122a4915c21SAttilio Rao if (1) { 5123a4915c21SAttilio Rao #endif 512457223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 5125d1f42ac2SAlan Cox if (kva == 0) 51268355f576SJeff Roberson return (0); 5127a4915c21SAttilio Rao } else 5128a4915c21SAttilio Rao kva = 0; 5129bb15d1c7SGleb Smirnoff 5130bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 5131099a0e58SBosko Milekic keg->uk_kva = kva; 5132a4915c21SAttilio Rao keg->uk_offset = 0; 5133bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 5134a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 5135a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 5136a4915c21SAttilio Rao #else 5137a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 5138a4915c21SAttilio Rao #endif 5139cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 5140cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 5141cc7ce83aSJeff Roberson zone_update_caches(zone); 5142af526374SJeff Roberson 51438355f576SJeff Roberson return (1); 51448355f576SJeff Roberson } 51458355f576SJeff Roberson 51468355f576SJeff Roberson /* See uma.h */ 51478355f576SJeff Roberson void 51488355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 51498355f576SJeff Roberson { 5150920239efSMark Johnston struct vm_domainset_iter di; 5151ab3185d1SJeff Roberson uma_domain_t dom; 51528355f576SJeff Roberson uma_slab_t slab; 5153099a0e58SBosko Milekic uma_keg_t keg; 515486220393SMark Johnston int aflags, domain, slabs; 51558355f576SJeff Roberson 5156bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 515779c9f942SJeff Roberson slabs = howmany(items, keg->uk_ipers); 5158194a979eSMark Johnston while (slabs-- > 0) { 515986220393SMark Johnston aflags = M_NOWAIT; 516086220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 516186220393SMark Johnston &aflags); 516286220393SMark Johnston for (;;) { 516386220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 516486220393SMark Johnston aflags); 516586220393SMark Johnston if (slab != NULL) { 5166ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 51674ab3aee8SMark Johnston /* 51684ab3aee8SMark Johnston * keg_alloc_slab() always returns a slab on the 51694ab3aee8SMark Johnston * partial list. 51704ab3aee8SMark Johnston */ 51718b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 517286220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 517386220393SMark Johnston us_link); 51744ab3aee8SMark Johnston dom->ud_free_slabs++; 51758b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 5176920239efSMark Johnston break; 51778355f576SJeff Roberson } 51788b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 517989d2fb14SKonstantin Belousov vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0); 518086220393SMark Johnston } 518186220393SMark Johnston } 518286220393SMark Johnston } 51838355f576SJeff Roberson 5184ed581bf6SJeff Roberson /* 5185ed581bf6SJeff Roberson * Returns a snapshot of memory consumption in bytes. 5186ed581bf6SJeff Roberson */ 5187ed581bf6SJeff Roberson size_t 5188ed581bf6SJeff Roberson uma_zone_memory(uma_zone_t zone) 5189ed581bf6SJeff Roberson { 5190ed581bf6SJeff Roberson size_t sz; 5191ed581bf6SJeff Roberson int i; 5192ed581bf6SJeff Roberson 5193ed581bf6SJeff Roberson sz = 0; 5194ed581bf6SJeff Roberson if (zone->uz_flags & UMA_ZFLAG_CACHE) { 5195ed581bf6SJeff Roberson for (i = 0; i < vm_ndomains; i++) 5196c6fd3e23SJeff Roberson sz += ZDOM_GET(zone, i)->uzd_nitems; 5197ed581bf6SJeff Roberson return (sz * zone->uz_size); 5198ed581bf6SJeff Roberson } 5199ed581bf6SJeff Roberson for (i = 0; i < vm_ndomains; i++) 5200ed581bf6SJeff Roberson sz += zone->uz_keg->uk_domain[i].ud_pages; 5201ed581bf6SJeff Roberson 5202ed581bf6SJeff Roberson return (sz * PAGE_SIZE); 5203ed581bf6SJeff Roberson } 5204ed581bf6SJeff Roberson 52058355f576SJeff Roberson /* See uma.h */ 520608cfa56eSMark Johnston void 520708cfa56eSMark Johnston uma_reclaim(int req) 52088355f576SJeff Roberson { 5209aabe13f1SMark Johnston uma_reclaim_domain(req, UMA_ANYDOMAIN); 5210aabe13f1SMark Johnston } 521144ec2b63SKonstantin Belousov 5212aabe13f1SMark Johnston void 5213aabe13f1SMark Johnston uma_reclaim_domain(int req, int domain) 5214aabe13f1SMark Johnston { 5215aabe13f1SMark Johnston void *arg; 5216aabe13f1SMark Johnston 521786bbae32SJeff Roberson bucket_enable(); 521808cfa56eSMark Johnston 5219aabe13f1SMark Johnston arg = (void *)(uintptr_t)domain; 5220aabe13f1SMark Johnston sx_slock(&uma_reclaim_lock); 522108cfa56eSMark Johnston switch (req) { 522208cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 5223aabe13f1SMark Johnston zone_foreach(zone_trim, arg); 522408cfa56eSMark Johnston break; 522508cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 5226aabe13f1SMark Johnston zone_foreach(zone_drain, arg); 5227aabe13f1SMark Johnston break; 522808cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 5229aabe13f1SMark Johnston zone_foreach(zone_drain, arg); 523008cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 5231aabe13f1SMark Johnston zone_foreach(zone_drain, arg); 523208cfa56eSMark Johnston break; 523308cfa56eSMark Johnston default: 523408cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 523508cfa56eSMark Johnston } 52360f9b7bf3SMark Johnston 52378355f576SJeff Roberson /* 52388355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 52398355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 52408355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 52418355f576SJeff Roberson */ 5242aabe13f1SMark Johnston zone_drain(slabzones[0], arg); 5243aabe13f1SMark Johnston zone_drain(slabzones[1], arg); 5244aabe13f1SMark Johnston bucket_zone_drain(domain); 5245aabe13f1SMark Johnston sx_sunlock(&uma_reclaim_lock); 52468355f576SJeff Roberson } 52478355f576SJeff Roberson 52482e47807cSJeff Roberson static volatile int uma_reclaim_needed; 524944ec2b63SKonstantin Belousov 525044ec2b63SKonstantin Belousov void 525144ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 525244ec2b63SKonstantin Belousov { 525344ec2b63SKonstantin Belousov 52542e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 52552e47807cSJeff Roberson wakeup(uma_reclaim); 525644ec2b63SKonstantin Belousov } 525744ec2b63SKonstantin Belousov 525844ec2b63SKonstantin Belousov void 525944ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 526044ec2b63SKonstantin Belousov { 526144ec2b63SKonstantin Belousov 526244ec2b63SKonstantin Belousov for (;;) { 526308cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 5264200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 526508cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 52662e47807cSJeff Roberson hz); 526708cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 52689b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 526908cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 5270200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 52712e47807cSJeff Roberson /* Don't fire more than once per-second. */ 52722e47807cSJeff Roberson pause("umarclslp", hz); 527344ec2b63SKonstantin Belousov } 527444ec2b63SKonstantin Belousov } 527544ec2b63SKonstantin Belousov 5276663b416fSJohn Baldwin /* See uma.h */ 527708cfa56eSMark Johnston void 527808cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 527908cfa56eSMark Johnston { 5280aabe13f1SMark Johnston uma_zone_reclaim_domain(zone, req, UMA_ANYDOMAIN); 5281aabe13f1SMark Johnston } 528208cfa56eSMark Johnston 5283aabe13f1SMark Johnston void 5284aabe13f1SMark Johnston uma_zone_reclaim_domain(uma_zone_t zone, int req, int domain) 5285aabe13f1SMark Johnston { 5286aabe13f1SMark Johnston void *arg; 5287aabe13f1SMark Johnston 5288aabe13f1SMark Johnston arg = (void *)(uintptr_t)domain; 528908cfa56eSMark Johnston switch (req) { 529008cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 5291aabe13f1SMark Johnston zone_trim(zone, arg); 529208cfa56eSMark Johnston break; 529308cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 5294aabe13f1SMark Johnston zone_drain(zone, arg); 529508cfa56eSMark Johnston break; 529608cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 529708cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 5298aabe13f1SMark Johnston zone_drain(zone, arg); 529908cfa56eSMark Johnston break; 530008cfa56eSMark Johnston default: 530108cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 530208cfa56eSMark Johnston } 530308cfa56eSMark Johnston } 530408cfa56eSMark Johnston 530508cfa56eSMark Johnston /* See uma.h */ 5306663b416fSJohn Baldwin int 5307663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 5308663b416fSJohn Baldwin { 5309663b416fSJohn Baldwin 5310727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 53116c125b8dSMohan Srinivasan } 53126c125b8dSMohan Srinivasan 53132e47807cSJeff Roberson unsigned long 53142e47807cSJeff Roberson uma_limit(void) 53152e47807cSJeff Roberson { 53162e47807cSJeff Roberson 53172e47807cSJeff Roberson return (uma_kmem_limit); 53182e47807cSJeff Roberson } 53192e47807cSJeff Roberson 53202e47807cSJeff Roberson void 53212e47807cSJeff Roberson uma_set_limit(unsigned long limit) 53222e47807cSJeff Roberson { 53232e47807cSJeff Roberson 53242e47807cSJeff Roberson uma_kmem_limit = limit; 53252e47807cSJeff Roberson } 53262e47807cSJeff Roberson 53272e47807cSJeff Roberson unsigned long 53282e47807cSJeff Roberson uma_size(void) 53292e47807cSJeff Roberson { 53302e47807cSJeff Roberson 5331058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 5332ad5b0f5bSJeff Roberson } 5333ad5b0f5bSJeff Roberson 5334ad5b0f5bSJeff Roberson long 5335ad5b0f5bSJeff Roberson uma_avail(void) 5336ad5b0f5bSJeff Roberson { 5337ad5b0f5bSJeff Roberson 5338058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 53392e47807cSJeff Roberson } 53402e47807cSJeff Roberson 5341a0d4b0aeSRobert Watson #ifdef DDB 53428355f576SJeff Roberson /* 53437a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 53447a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 53457a52a97eSRobert Watson * 53467a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 53477a52a97eSRobert Watson * per-CPU cache statistic. 53487a52a97eSRobert Watson * 53497a52a97eSRobert Watson */ 53507a52a97eSRobert Watson static void 53510f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 5352c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 53537a52a97eSRobert Watson { 53547a52a97eSRobert Watson uma_cache_t cache; 5355c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 53567a52a97eSRobert Watson int cachefree, cpu; 53577a52a97eSRobert Watson 5358c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 53597a52a97eSRobert Watson cachefree = 0; 53603aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 53617a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 5362376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 5363376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 5364376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 5365376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 53667a52a97eSRobert Watson allocs += cache->uc_allocs; 53677a52a97eSRobert Watson frees += cache->uc_frees; 53687a52a97eSRobert Watson } 53692efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 53702efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 5371c6fd3e23SJeff Roberson xdomain += counter_u64_fetch(z->uz_xdomain); 5372bf965959SSean Bruno sleeps += z->uz_sleeps; 53737a52a97eSRobert Watson if (cachefreep != NULL) 53747a52a97eSRobert Watson *cachefreep = cachefree; 53757a52a97eSRobert Watson if (allocsp != NULL) 53767a52a97eSRobert Watson *allocsp = allocs; 53777a52a97eSRobert Watson if (freesp != NULL) 53787a52a97eSRobert Watson *freesp = frees; 5379bf965959SSean Bruno if (sleepsp != NULL) 5380bf965959SSean Bruno *sleepsp = sleeps; 5381c1685086SJeff Roberson if (xdomainp != NULL) 5382c1685086SJeff Roberson *xdomainp = xdomain; 53837a52a97eSRobert Watson } 5384a0d4b0aeSRobert Watson #endif /* DDB */ 53857a52a97eSRobert Watson 53867a52a97eSRobert Watson static int 53877a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 53887a52a97eSRobert Watson { 53897a52a97eSRobert Watson uma_keg_t kz; 53907a52a97eSRobert Watson uma_zone_t z; 53917a52a97eSRobert Watson int count; 53927a52a97eSRobert Watson 53937a52a97eSRobert Watson count = 0; 5394111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 53957a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 53967a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 53977a52a97eSRobert Watson count++; 53987a52a97eSRobert Watson } 5399b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 5400b47acb0aSGleb Smirnoff count++; 5401b47acb0aSGleb Smirnoff 5402111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 54037a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 54047a52a97eSRobert Watson } 54057a52a97eSRobert Watson 5406b47acb0aSGleb Smirnoff static void 5407b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 5408b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 5409b47acb0aSGleb Smirnoff { 5410b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 5411b47acb0aSGleb Smirnoff uma_cache_t cache; 5412b47acb0aSGleb Smirnoff int i; 5413b47acb0aSGleb Smirnoff 5414b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 5415c6fd3e23SJeff Roberson zdom = ZDOM_GET(z, i); 5416b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 5417b47acb0aSGleb Smirnoff } 5418b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 5419b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 5420b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 5421c6fd3e23SJeff Roberson uth->uth_xdomain = counter_u64_fetch(z->uz_xdomain); 5422b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 54231de9724eSMark Johnston 5424b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 5425b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 5426b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 5427b47acb0aSGleb Smirnoff continue; 5428b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 5429376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 5430376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 5431376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 5432b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 5433b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 5434b47acb0aSGleb Smirnoff } 5435b47acb0aSGleb Smirnoff } 5436b47acb0aSGleb Smirnoff 54377a52a97eSRobert Watson static int 54387a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 54397a52a97eSRobert Watson { 54407a52a97eSRobert Watson struct uma_stream_header ush; 54417a52a97eSRobert Watson struct uma_type_header uth; 544263b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 54437a52a97eSRobert Watson struct sbuf sbuf; 54447a52a97eSRobert Watson uma_keg_t kz; 54457a52a97eSRobert Watson uma_zone_t z; 54464bd61e19SJeff Roberson uint64_t items; 54478b987a77SJeff Roberson uint32_t kfree, pages; 54484e657159SMatthew D Fleming int count, error, i; 54497a52a97eSRobert Watson 545000f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 545100f0e671SMatthew D Fleming if (error != 0) 545200f0e671SMatthew D Fleming return (error); 54534e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 54541eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 545563b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 54564e657159SMatthew D Fleming 5457404a593eSMatthew D Fleming count = 0; 5458111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 54597a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 54607a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 54617a52a97eSRobert Watson count++; 54627a52a97eSRobert Watson } 54637a52a97eSRobert Watson 5464b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 5465b47acb0aSGleb Smirnoff count++; 5466b47acb0aSGleb Smirnoff 54677a52a97eSRobert Watson /* 54687a52a97eSRobert Watson * Insert stream header. 54697a52a97eSRobert Watson */ 54707a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 54717a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 5472ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 54737a52a97eSRobert Watson ush.ush_count = count; 54744e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 54757a52a97eSRobert Watson 54767a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 54778b987a77SJeff Roberson kfree = pages = 0; 54788b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 54794ab3aee8SMark Johnston kfree += kz->uk_domain[i].ud_free_items; 54808b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 54818b987a77SJeff Roberson } 54827a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 54837a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 5484cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 54857a52a97eSRobert Watson uth.uth_align = kz->uk_align; 54867a52a97eSRobert Watson uth.uth_size = kz->uk_size; 54877a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 54884bd61e19SJeff Roberson if (z->uz_max_items > 0) { 54894bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 54904bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 5491bb15d1c7SGleb Smirnoff kz->uk_ppera; 54924bd61e19SJeff Roberson } else 54938b987a77SJeff Roberson uth.uth_pages = pages; 5494f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 5495bb15d1c7SGleb Smirnoff kz->uk_ppera; 5496bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 54978b987a77SJeff Roberson uth.uth_keg_free = kfree; 5498cbbb4a00SRobert Watson 5499cbbb4a00SRobert Watson /* 5500cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 5501cbbb4a00SRobert Watson * on the keg's zone list. 5502cbbb4a00SRobert Watson */ 5503e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 5504cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 5505cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 5506b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 5507b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 550863b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 550963b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 551063b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 55117a52a97eSRobert Watson } 55127a52a97eSRobert Watson } 5513b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 5514b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 5515b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 5516b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 5517b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 5518b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 5519b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 5520b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 5521b47acb0aSGleb Smirnoff } 5522b47acb0aSGleb Smirnoff 5523111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 55244e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 55254e657159SMatthew D Fleming sbuf_delete(&sbuf); 552663b5d112SKonstantin Belousov free(ups, M_TEMP); 55277a52a97eSRobert Watson return (error); 55287a52a97eSRobert Watson } 552948c5777eSRobert Watson 55300a5a3ccbSGleb Smirnoff int 55310a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 55320a5a3ccbSGleb Smirnoff { 55330a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 553416be9f54SGleb Smirnoff int error, max; 55350a5a3ccbSGleb Smirnoff 553616be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 55370a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 55380a5a3ccbSGleb Smirnoff if (error || !req->newptr) 55390a5a3ccbSGleb Smirnoff return (error); 55400a5a3ccbSGleb Smirnoff 55410a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 55420a5a3ccbSGleb Smirnoff 55430a5a3ccbSGleb Smirnoff return (0); 55440a5a3ccbSGleb Smirnoff } 55450a5a3ccbSGleb Smirnoff 55460a5a3ccbSGleb Smirnoff int 55470a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 55480a5a3ccbSGleb Smirnoff { 554920a4e154SJeff Roberson uma_zone_t zone; 55500a5a3ccbSGleb Smirnoff int cur; 55510a5a3ccbSGleb Smirnoff 555220a4e154SJeff Roberson /* 555320a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 555420a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 555520a4e154SJeff Roberson */ 555620a4e154SJeff Roberson if (arg2 == 0) 555720a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 555820a4e154SJeff Roberson else 555920a4e154SJeff Roberson zone = arg1; 55600a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 55610a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 55620a5a3ccbSGleb Smirnoff } 55630a5a3ccbSGleb Smirnoff 556420a4e154SJeff Roberson static int 556520a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 556620a4e154SJeff Roberson { 556720a4e154SJeff Roberson uma_zone_t zone = arg1; 556820a4e154SJeff Roberson uint64_t cur; 556920a4e154SJeff Roberson 557020a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 557120a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 557220a4e154SJeff Roberson } 557320a4e154SJeff Roberson 557420a4e154SJeff Roberson static int 557520a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 557620a4e154SJeff Roberson { 557720a4e154SJeff Roberson uma_zone_t zone = arg1; 557820a4e154SJeff Roberson uint64_t cur; 557920a4e154SJeff Roberson 558020a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 558120a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 558220a4e154SJeff Roberson } 558320a4e154SJeff Roberson 55846d204a6aSRyan Libby static int 55856d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 55866d204a6aSRyan Libby { 55876d204a6aSRyan Libby struct sbuf sbuf; 55886d204a6aSRyan Libby uma_zone_t zone = arg1; 55896d204a6aSRyan Libby int error; 55906d204a6aSRyan Libby 55916d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 55926d204a6aSRyan Libby if (zone->uz_flags != 0) 55936d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 55946d204a6aSRyan Libby else 55956d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 55966d204a6aSRyan Libby error = sbuf_finish(&sbuf); 55976d204a6aSRyan Libby sbuf_delete(&sbuf); 55986d204a6aSRyan Libby 55996d204a6aSRyan Libby return (error); 56006d204a6aSRyan Libby } 56016d204a6aSRyan Libby 5602f7af5015SRyan Libby static int 5603f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 5604f7af5015SRyan Libby { 5605f7af5015SRyan Libby uma_keg_t keg = arg1; 5606f7af5015SRyan Libby int avail, effpct, total; 5607f7af5015SRyan Libby 5608f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 560954c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 56109b8db4d0SRyan Libby total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize; 5611f7af5015SRyan Libby /* 5612f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 5613f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 5614f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 5615f7af5015SRyan Libby */ 5616f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 5617f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 5618f7af5015SRyan Libby avail *= mp_maxid + 1; 5619f7af5015SRyan Libby effpct = 100 * avail / total; 5620f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 5621f7af5015SRyan Libby } 5622f7af5015SRyan Libby 56234bd61e19SJeff Roberson static int 56244bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 56254bd61e19SJeff Roberson { 56264bd61e19SJeff Roberson uma_zone_t zone = arg1; 56274bd61e19SJeff Roberson uint64_t cur; 56284bd61e19SJeff Roberson 56294bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 56304bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 56314bd61e19SJeff Roberson } 56324bd61e19SJeff Roberson 56339542ea7bSGleb Smirnoff #ifdef INVARIANTS 56349542ea7bSGleb Smirnoff static uma_slab_t 56359542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 56369542ea7bSGleb Smirnoff { 56379542ea7bSGleb Smirnoff uma_slab_t slab; 56389542ea7bSGleb Smirnoff uma_keg_t keg; 56399542ea7bSGleb Smirnoff uint8_t *mem; 56409542ea7bSGleb Smirnoff 56419542ea7bSGleb Smirnoff /* 56429542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 56439542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 56449542ea7bSGleb Smirnoff * essentially holds a reference. 56459542ea7bSGleb Smirnoff */ 5646727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 5647727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5648bb15d1c7SGleb Smirnoff return (NULL); 564954c5ae80SRyan Libby if (zone->uz_flags & UMA_ZFLAG_VTOSLAB) 5650727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 5651bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 565254c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0) 5653727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 56548b987a77SJeff Roberson KEG_LOCK(keg, 0); 56559542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 56568b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 56579542ea7bSGleb Smirnoff 56589542ea7bSGleb Smirnoff return (slab); 56599542ea7bSGleb Smirnoff } 56609542ea7bSGleb Smirnoff 5661c5deaf04SGleb Smirnoff static bool 5662c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 5663c5deaf04SGleb Smirnoff { 5664c5deaf04SGleb Smirnoff 5665727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5666c5deaf04SGleb Smirnoff return (true); 5667c5deaf04SGleb Smirnoff 5668bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 5669c5deaf04SGleb Smirnoff } 5670c5deaf04SGleb Smirnoff 5671c5deaf04SGleb Smirnoff static bool 5672c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 5673c5deaf04SGleb Smirnoff { 5674c5deaf04SGleb Smirnoff uintptr_t idx; 5675c5deaf04SGleb Smirnoff 5676c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 5677c5deaf04SGleb Smirnoff return (true); 5678c5deaf04SGleb Smirnoff 5679c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 5680c5deaf04SGleb Smirnoff return (false); 5681c5deaf04SGleb Smirnoff 5682c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 5683c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 5684c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 5685c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 5686c5deaf04SGleb Smirnoff } 5687c5deaf04SGleb Smirnoff 5688c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 5689c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 5690c5deaf04SGleb Smirnoff return (true); 5691c5deaf04SGleb Smirnoff } 5692c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 5693c5deaf04SGleb Smirnoff 5694c5deaf04SGleb Smirnoff return (false); 5695c5deaf04SGleb Smirnoff } 5696c5deaf04SGleb Smirnoff 56979542ea7bSGleb Smirnoff /* 56989542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 56999542ea7bSGleb Smirnoff * 57009542ea7bSGleb Smirnoff */ 57019542ea7bSGleb Smirnoff static void 57029542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 57039542ea7bSGleb Smirnoff { 57049542ea7bSGleb Smirnoff uma_keg_t keg; 57059542ea7bSGleb Smirnoff int freei; 57069542ea7bSGleb Smirnoff 57079542ea7bSGleb Smirnoff if (slab == NULL) { 57089542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 57099542ea7bSGleb Smirnoff if (slab == NULL) 5710952c8964SMark Johnston panic("uma: item %p did not belong to zone %s", 57119542ea7bSGleb Smirnoff item, zone->uz_name); 57129542ea7bSGleb Smirnoff } 5713584061b4SJeff Roberson keg = zone->uz_keg; 57141e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 57159542ea7bSGleb Smirnoff 5716942951baSRyan Libby if (BIT_TEST_SET_ATOMIC(keg->uk_ipers, freei, 5717942951baSRyan Libby slab_dbg_bits(slab, keg))) 5718952c8964SMark Johnston panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)", 57199542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 57209542ea7bSGleb Smirnoff } 57219542ea7bSGleb Smirnoff 57229542ea7bSGleb Smirnoff /* 57239542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 57249542ea7bSGleb Smirnoff * and duplicate frees. 57259542ea7bSGleb Smirnoff * 57269542ea7bSGleb Smirnoff */ 57279542ea7bSGleb Smirnoff static void 57289542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 57299542ea7bSGleb Smirnoff { 57309542ea7bSGleb Smirnoff uma_keg_t keg; 57319542ea7bSGleb Smirnoff int freei; 57329542ea7bSGleb Smirnoff 57339542ea7bSGleb Smirnoff if (slab == NULL) { 57349542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 57359542ea7bSGleb Smirnoff if (slab == NULL) 5736952c8964SMark Johnston panic("uma: Freed item %p did not belong to zone %s", 57379542ea7bSGleb Smirnoff item, zone->uz_name); 57389542ea7bSGleb Smirnoff } 5739584061b4SJeff Roberson keg = zone->uz_keg; 57401e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 57419542ea7bSGleb Smirnoff 57429542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 5743952c8964SMark Johnston panic("Invalid free of %p from zone %p(%s) slab %p(%d)", 57449542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 57459542ea7bSGleb Smirnoff 57461e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 5747952c8964SMark Johnston panic("Unaligned free of %p from zone %p(%s) slab %p(%d)", 57489542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 57499542ea7bSGleb Smirnoff 5750942951baSRyan Libby if (!BIT_TEST_CLR_ATOMIC(keg->uk_ipers, freei, 5751942951baSRyan Libby slab_dbg_bits(slab, keg))) 5752952c8964SMark Johnston panic("Duplicate free of %p from zone %p(%s) slab %p(%d)", 57539542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 57549542ea7bSGleb Smirnoff } 57559542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 57569542ea7bSGleb Smirnoff 575748c5777eSRobert Watson #ifdef DDB 575846d70077SConrad Meyer static int64_t 575946d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 57600223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 576148c5777eSRobert Watson { 576246d70077SConrad Meyer uint64_t frees; 57630f9b7bf3SMark Johnston int i; 576448c5777eSRobert Watson 576548c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 576646d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 57672efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 576846d70077SConrad Meyer *sleeps = z->uz_sleeps; 576946d70077SConrad Meyer *cachefree = 0; 577046d70077SConrad Meyer *xdomain = 0; 577148c5777eSRobert Watson } else 577246d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 577346d70077SConrad Meyer xdomain); 57748b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 5775c6fd3e23SJeff Roberson *cachefree += ZDOM_GET(z, i)->uzd_nitems; 5776e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 577748c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 57784ab3aee8SMark Johnston *cachefree += kz->uk_domain[i].ud_free_items; 57798b987a77SJeff Roberson } 578046d70077SConrad Meyer *used = *allocs - frees; 578146d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 578246d70077SConrad Meyer } 57830f9b7bf3SMark Johnston 578446d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 578546d70077SConrad Meyer { 578646d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 578746d70077SConrad Meyer uma_keg_t kz; 578846d70077SConrad Meyer uma_zone_t z; 578946d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 579046d70077SConrad Meyer long cachefree; 579146d70077SConrad Meyer /* variables for sorting */ 579246d70077SConrad Meyer uma_keg_t cur_keg; 579346d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 579446d70077SConrad Meyer int64_t cur_size, last_size, size; 579546d70077SConrad Meyer int ties; 579646d70077SConrad Meyer 579746d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 579846d70077SConrad Meyer if (modif[0] == 'i') { 579946d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 580046d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 580146d70077SConrad Meyer } else { 580246d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 580346d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 580446d70077SConrad Meyer } 580546d70077SConrad Meyer 580646d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 580746d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 580846d70077SConrad Meyer 580946d70077SConrad Meyer /* Sort the zones with largest size first. */ 581046d70077SConrad Meyer last_zone = NULL; 581146d70077SConrad Meyer last_size = INT64_MAX; 581246d70077SConrad Meyer for (;;) { 581346d70077SConrad Meyer cur_zone = NULL; 581446d70077SConrad Meyer cur_size = -1; 581546d70077SConrad Meyer ties = 0; 581646d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 581746d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 581846d70077SConrad Meyer /* 581946d70077SConrad Meyer * In the case of size ties, print out zones 582046d70077SConrad Meyer * in the order they are encountered. That is, 582146d70077SConrad Meyer * when we encounter the most recently output 582246d70077SConrad Meyer * zone, we have already printed all preceding 582346d70077SConrad Meyer * ties, and we must print all following ties. 582446d70077SConrad Meyer */ 582546d70077SConrad Meyer if (z == last_zone) { 582646d70077SConrad Meyer ties = 1; 582746d70077SConrad Meyer continue; 582846d70077SConrad Meyer } 582946d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 583046d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 583146d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 583246d70077SConrad Meyer { 583346d70077SConrad Meyer cur_size = size; 583446d70077SConrad Meyer cur_zone = z; 583546d70077SConrad Meyer cur_keg = kz; 583646d70077SConrad Meyer } 583746d70077SConrad Meyer } 583846d70077SConrad Meyer } 583946d70077SConrad Meyer if (cur_zone == NULL) 584046d70077SConrad Meyer break; 584146d70077SConrad Meyer 584246d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 584346d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 584446d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 584546d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 584646d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 584720a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 584820a4e154SJeff Roberson xdomain); 584946d70077SConrad Meyer 5850687c94aaSJohn Baldwin if (db_pager_quit) 5851687c94aaSJohn Baldwin return; 585246d70077SConrad Meyer last_zone = cur_zone; 585346d70077SConrad Meyer last_size = cur_size; 585448c5777eSRobert Watson } 585548c5777eSRobert Watson } 585603175483SAlexander Motin 585703175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 585803175483SAlexander Motin { 585903175483SAlexander Motin uma_zone_t z; 5860ab3185d1SJeff Roberson uint64_t allocs, frees; 58610f9b7bf3SMark Johnston long cachefree; 58620f9b7bf3SMark Johnston int i; 586303175483SAlexander Motin 586403175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 586503175483SAlexander Motin "Requests", "Bucket"); 586603175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 5867c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 58680f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 5869c6fd3e23SJeff Roberson cachefree += ZDOM_GET(z, i)->uzd_nitems; 58700f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 587103175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 587203175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 587320a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 587403175483SAlexander Motin if (db_pager_quit) 587503175483SAlexander Motin return; 587603175483SAlexander Motin } 587703175483SAlexander Motin } 58789542ea7bSGleb Smirnoff #endif /* DDB */ 5879