160727d8bSWarner Losh /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 4584061b4SJeff Roberson * Copyright (c) 2002-2019 Jeffrey Roberson <jeff@FreeBSD.org> 508ecce74SRobert Watson * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org> 6ae4e9636SRobert Watson * Copyright (c) 2004-2006 Robert N. M. Watson 708ecce74SRobert Watson * All rights reserved. 88355f576SJeff Roberson * 98355f576SJeff Roberson * Redistribution and use in source and binary forms, with or without 108355f576SJeff Roberson * modification, are permitted provided that the following conditions 118355f576SJeff Roberson * are met: 128355f576SJeff Roberson * 1. Redistributions of source code must retain the above copyright 138355f576SJeff Roberson * notice unmodified, this list of conditions, and the following 148355f576SJeff Roberson * disclaimer. 158355f576SJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 168355f576SJeff Roberson * notice, this list of conditions and the following disclaimer in the 178355f576SJeff Roberson * documentation and/or other materials provided with the distribution. 188355f576SJeff Roberson * 198355f576SJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 208355f576SJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 218355f576SJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 228355f576SJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 238355f576SJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 248355f576SJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 258355f576SJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 268355f576SJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 278355f576SJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 288355f576SJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 298355f576SJeff Roberson */ 308355f576SJeff Roberson 318355f576SJeff Roberson /* 328355f576SJeff Roberson * uma_core.c Implementation of the Universal Memory allocator 338355f576SJeff Roberson * 348355f576SJeff Roberson * This allocator is intended to replace the multitude of similar object caches 358355f576SJeff Roberson * in the standard FreeBSD kernel. The intent is to be flexible as well as 36763df3ecSPedro F. Giffuni * efficient. A primary design goal is to return unused memory to the rest of 378355f576SJeff Roberson * the system. This will make the system as a whole more flexible due to the 388355f576SJeff Roberson * ability to move memory to subsystems which most need it instead of leaving 398355f576SJeff Roberson * pools of reserved memory unused. 408355f576SJeff Roberson * 418355f576SJeff Roberson * The basic ideas stem from similar slab/zone based allocators whose algorithms 428355f576SJeff Roberson * are well known. 438355f576SJeff Roberson * 448355f576SJeff Roberson */ 458355f576SJeff Roberson 468355f576SJeff Roberson /* 478355f576SJeff Roberson * TODO: 488355f576SJeff Roberson * - Improve memory usage for large allocations 498355f576SJeff Roberson * - Investigate cache size adjustments 508355f576SJeff Roberson */ 518355f576SJeff Roberson 52874651b1SDavid E. O'Brien #include <sys/cdefs.h> 53874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 54874651b1SDavid E. O'Brien 5548c5777eSRobert Watson #include "opt_ddb.h" 568355f576SJeff Roberson #include "opt_param.h" 578d689e04SGleb Smirnoff #include "opt_vm.h" 5848c5777eSRobert Watson 598355f576SJeff Roberson #include <sys/param.h> 608355f576SJeff Roberson #include <sys/systm.h> 61ef72505eSJeff Roberson #include <sys/bitset.h> 62194a979eSMark Johnston #include <sys/domainset.h> 639b43bc27SAndriy Gapon #include <sys/eventhandler.h> 648355f576SJeff Roberson #include <sys/kernel.h> 658355f576SJeff Roberson #include <sys/types.h> 66ad5b0f5bSJeff Roberson #include <sys/limits.h> 678355f576SJeff Roberson #include <sys/queue.h> 688355f576SJeff Roberson #include <sys/malloc.h> 693659f747SRobert Watson #include <sys/ktr.h> 708355f576SJeff Roberson #include <sys/lock.h> 718355f576SJeff Roberson #include <sys/sysctl.h> 728355f576SJeff Roberson #include <sys/mutex.h> 734c1cc01cSJohn Baldwin #include <sys/proc.h> 7410cb2424SMark Murray #include <sys/random.h> 7589f6b863SAttilio Rao #include <sys/rwlock.h> 767a52a97eSRobert Watson #include <sys/sbuf.h> 77a2de44abSAlexander Motin #include <sys/sched.h> 784bd61e19SJeff Roberson #include <sys/sleepqueue.h> 798355f576SJeff Roberson #include <sys/smp.h> 80d4665eaaSJeff Roberson #include <sys/smr.h> 81e60b2fcbSGleb Smirnoff #include <sys/taskqueue.h> 8286bbae32SJeff Roberson #include <sys/vmmeter.h> 8386bbae32SJeff Roberson 848355f576SJeff Roberson #include <vm/vm.h> 85194a979eSMark Johnston #include <vm/vm_domainset.h> 868355f576SJeff Roberson #include <vm/vm_object.h> 878355f576SJeff Roberson #include <vm/vm_page.h> 88a4915c21SAttilio Rao #include <vm/vm_pageout.h> 898355f576SJeff Roberson #include <vm/vm_param.h> 90ab3185d1SJeff Roberson #include <vm/vm_phys.h> 9130c5525bSAndrew Gallatin #include <vm/vm_pagequeue.h> 928355f576SJeff Roberson #include <vm/vm_map.h> 938355f576SJeff Roberson #include <vm/vm_kern.h> 948355f576SJeff Roberson #include <vm/vm_extern.h> 958355f576SJeff Roberson #include <vm/uma.h> 968355f576SJeff Roberson #include <vm/uma_int.h> 97639c9550SJeff Roberson #include <vm/uma_dbg.h> 988355f576SJeff Roberson 9948c5777eSRobert Watson #include <ddb/ddb.h> 10048c5777eSRobert Watson 1018d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 1028d689e04SGleb Smirnoff #include <vm/memguard.h> 1038d689e04SGleb Smirnoff #endif 1048d689e04SGleb Smirnoff 105a81c400eSJeff Roberson #include <machine/md_var.h> 106a81c400eSJeff Roberson 107d4665eaaSJeff Roberson #ifdef INVARIANTS 108d4665eaaSJeff Roberson #define UMA_ALWAYS_CTORDTOR 1 109d4665eaaSJeff Roberson #else 110d4665eaaSJeff Roberson #define UMA_ALWAYS_CTORDTOR 0 111d4665eaaSJeff Roberson #endif 112d4665eaaSJeff Roberson 1138355f576SJeff Roberson /* 114ab3185d1SJeff Roberson * This is the zone and keg from which all zones are spawned. 1158355f576SJeff Roberson */ 116ab3185d1SJeff Roberson static uma_zone_t kegs; 117ab3185d1SJeff Roberson static uma_zone_t zones; 1188355f576SJeff Roberson 1199b8db4d0SRyan Libby /* 12054007ce8SMark Johnston * On INVARIANTS builds, the slab contains a second bitset of the same size, 12154007ce8SMark Johnston * "dbg_bits", which is laid out immediately after us_free. 12254007ce8SMark Johnston */ 12354007ce8SMark Johnston #ifdef INVARIANTS 12454007ce8SMark Johnston #define SLAB_BITSETS 2 12554007ce8SMark Johnston #else 12654007ce8SMark Johnston #define SLAB_BITSETS 1 12754007ce8SMark Johnston #endif 12854007ce8SMark Johnston 12954007ce8SMark Johnston /* 1309b8db4d0SRyan Libby * These are the two zones from which all offpage uma_slab_ts are allocated. 1319b8db4d0SRyan Libby * 1329b8db4d0SRyan Libby * One zone is for slab headers that can represent a larger number of items, 1339b8db4d0SRyan Libby * making the slabs themselves more efficient, and the other zone is for 1349b8db4d0SRyan Libby * headers that are smaller and represent fewer items, making the headers more 1359b8db4d0SRyan Libby * efficient. 1369b8db4d0SRyan Libby */ 1379b8db4d0SRyan Libby #define SLABZONE_SIZE(setsize) \ 1389b8db4d0SRyan Libby (sizeof(struct uma_hash_slab) + BITSET_SIZE(setsize) * SLAB_BITSETS) 1399b8db4d0SRyan Libby #define SLABZONE0_SETSIZE (PAGE_SIZE / 16) 1409b8db4d0SRyan Libby #define SLABZONE1_SETSIZE SLAB_MAX_SETSIZE 1419b8db4d0SRyan Libby #define SLABZONE0_SIZE SLABZONE_SIZE(SLABZONE0_SETSIZE) 1429b8db4d0SRyan Libby #define SLABZONE1_SIZE SLABZONE_SIZE(SLABZONE1_SETSIZE) 1439b8db4d0SRyan Libby static uma_zone_t slabzones[2]; 1448355f576SJeff Roberson 1458355f576SJeff Roberson /* 1468355f576SJeff Roberson * The initial hash tables come out of this zone so they can be allocated 1478355f576SJeff Roberson * prior to malloc coming up. 1488355f576SJeff Roberson */ 1498355f576SJeff Roberson static uma_zone_t hashzone; 1508355f576SJeff Roberson 1511e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */ 152e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1; 1531e319f6dSRobert Watson 154961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets"); 15520a4e154SJeff Roberson static MALLOC_DEFINE(M_UMA, "UMA", "UMA Misc"); 156961647dfSJeff Roberson 1578355f576SJeff Roberson /* 15886bbae32SJeff Roberson * Are we allowed to allocate buckets? 15986bbae32SJeff Roberson */ 16086bbae32SJeff Roberson static int bucketdisable = 1; 16186bbae32SJeff Roberson 162099a0e58SBosko Milekic /* Linked list of all kegs in the system */ 16313e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs); 1648355f576SJeff Roberson 16503175483SAlexander Motin /* Linked list of all cache-only zones in the system */ 16603175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones = 16703175483SAlexander Motin LIST_HEAD_INITIALIZER(uma_cachezones); 16803175483SAlexander Motin 169111fbcd5SBryan Venteicher /* This RW lock protects the keg list */ 170fe933c1dSMateusz Guzik static struct rwlock_padalign __exclusive_cache_line uma_rwlock; 1718355f576SJeff Roberson 172ac0a6fd0SGleb Smirnoff /* 173a81c400eSJeff Roberson * First available virual address for boot time allocations. 174ac0a6fd0SGleb Smirnoff */ 175a81c400eSJeff Roberson static vm_offset_t bootstart; 176a81c400eSJeff Roberson static vm_offset_t bootmem; 1778355f576SJeff Roberson 17808cfa56eSMark Johnston static struct sx uma_reclaim_lock; 17995c4bf75SKonstantin Belousov 180fbd95859SMark Johnston /* 181fbd95859SMark Johnston * kmem soft limit, initialized by uma_set_limit(). Ensure that early 182fbd95859SMark Johnston * allocations don't trigger a wakeup of the reclaim thread. 183fbd95859SMark Johnston */ 1846d6a03d7SJeff Roberson unsigned long uma_kmem_limit = LONG_MAX; 185fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_limit, CTLFLAG_RD, &uma_kmem_limit, 0, 186fbd95859SMark Johnston "UMA kernel memory soft limit"); 1876d6a03d7SJeff Roberson unsigned long uma_kmem_total; 188fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_total, CTLFLAG_RD, &uma_kmem_total, 0, 189fbd95859SMark Johnston "UMA kernel memory usage"); 1902e47807cSJeff Roberson 1918355f576SJeff Roberson /* Is the VM done starting up? */ 192860bb7a0SMark Johnston static enum { 193860bb7a0SMark Johnston BOOT_COLD, 194a81c400eSJeff Roberson BOOT_KVA, 195dc2b3205SMark Johnston BOOT_PCPU, 196860bb7a0SMark Johnston BOOT_RUNNING, 197860bb7a0SMark Johnston BOOT_SHUTDOWN, 198860bb7a0SMark Johnston } booted = BOOT_COLD; 1998355f576SJeff Roberson 200ef72505eSJeff Roberson /* 2019643769aSJeff Roberson * This is the handle used to schedule events that need to happen 2029643769aSJeff Roberson * outside of the allocation fast path. 2039643769aSJeff Roberson */ 2048355f576SJeff Roberson static struct callout uma_callout; 2059643769aSJeff Roberson #define UMA_TIMEOUT 20 /* Seconds for callout interval. */ 2068355f576SJeff Roberson 2078355f576SJeff Roberson /* 2088355f576SJeff Roberson * This structure is passed as the zone ctor arg so that I don't have to create 2098355f576SJeff Roberson * a special allocation function just for zones. 2108355f576SJeff Roberson */ 2118355f576SJeff Roberson struct uma_zctor_args { 212bb196eb4SMatthew D Fleming const char *name; 213c3bdc05fSAndrew R. Reiter size_t size; 2148355f576SJeff Roberson uma_ctor ctor; 2158355f576SJeff Roberson uma_dtor dtor; 2168355f576SJeff Roberson uma_init uminit; 2178355f576SJeff Roberson uma_fini fini; 2180095a784SJeff Roberson uma_import import; 2190095a784SJeff Roberson uma_release release; 2200095a784SJeff Roberson void *arg; 221099a0e58SBosko Milekic uma_keg_t keg; 222099a0e58SBosko Milekic int align; 22385dcf349SGleb Smirnoff uint32_t flags; 224099a0e58SBosko Milekic }; 225099a0e58SBosko Milekic 226099a0e58SBosko Milekic struct uma_kctor_args { 227099a0e58SBosko Milekic uma_zone_t zone; 228099a0e58SBosko Milekic size_t size; 229099a0e58SBosko Milekic uma_init uminit; 230099a0e58SBosko Milekic uma_fini fini; 2318355f576SJeff Roberson int align; 23285dcf349SGleb Smirnoff uint32_t flags; 2338355f576SJeff Roberson }; 2348355f576SJeff Roberson 235cae33c14SJeff Roberson struct uma_bucket_zone { 236cae33c14SJeff Roberson uma_zone_t ubz_zone; 237eaa17d42SRyan Libby const char *ubz_name; 238fc03d22bSJeff Roberson int ubz_entries; /* Number of items it can hold. */ 239fc03d22bSJeff Roberson int ubz_maxsize; /* Maximum allocation size per-item. */ 240cae33c14SJeff Roberson }; 241cae33c14SJeff Roberson 242f9d27e75SRobert Watson /* 243fc03d22bSJeff Roberson * Compute the actual number of bucket entries to pack them in power 244fc03d22bSJeff Roberson * of two sizes for more efficient space utilization. 245f9d27e75SRobert Watson */ 246fc03d22bSJeff Roberson #define BUCKET_SIZE(n) \ 247fc03d22bSJeff Roberson (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *)) 248fc03d22bSJeff Roberson 2491aa6c758SAlexander Motin #define BUCKET_MAX BUCKET_SIZE(256) 250e84130a0SJeff Roberson #define BUCKET_MIN 2 251fc03d22bSJeff Roberson 252fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = { 253e84130a0SJeff Roberson /* Literal bucket sizes. */ 254e84130a0SJeff Roberson { NULL, "2 Bucket", 2, 4096 }, 255e84130a0SJeff Roberson { NULL, "4 Bucket", 4, 3072 }, 256e84130a0SJeff Roberson { NULL, "8 Bucket", 8, 2048 }, 257e84130a0SJeff Roberson { NULL, "16 Bucket", 16, 1024 }, 258e84130a0SJeff Roberson /* Rounded down power of 2 sizes for efficiency. */ 259fc03d22bSJeff Roberson { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 260fc03d22bSJeff Roberson { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 261fc03d22bSJeff Roberson { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 2621aa6c758SAlexander Motin { NULL, "256 Bucket", BUCKET_SIZE(256), 64 }, 263fc03d22bSJeff Roberson { NULL, NULL, 0} 264fc03d22bSJeff Roberson }; 265cae33c14SJeff Roberson 2662019094aSRobert Watson /* 2672019094aSRobert Watson * Flags and enumerations to be passed to internal functions. 2682019094aSRobert Watson */ 269bb15d1c7SGleb Smirnoff enum zfreeskip { 270bb15d1c7SGleb Smirnoff SKIP_NONE = 0, 271bb15d1c7SGleb Smirnoff SKIP_CNT = 0x00000001, 272bb15d1c7SGleb Smirnoff SKIP_DTOR = 0x00010000, 273bb15d1c7SGleb Smirnoff SKIP_FINI = 0x00020000, 274bb15d1c7SGleb Smirnoff }; 275b23f72e9SBrian Feldman 2768355f576SJeff Roberson /* Prototypes.. */ 2778355f576SJeff Roberson 278a81c400eSJeff Roberson void uma_startup1(vm_offset_t); 279f4bef67cSGleb Smirnoff void uma_startup2(void); 280f4bef67cSGleb Smirnoff 281ab3185d1SJeff Roberson static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 282ab3185d1SJeff Roberson static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 283ab3059a8SMatt Macy static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 284ab3185d1SJeff Roberson static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 285ec0d8280SRyan Libby static void *contig_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 286f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t); 287ab3059a8SMatt Macy static void pcpu_page_free(void *, vm_size_t, uint8_t); 28886220393SMark Johnston static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int); 2899643769aSJeff Roberson static void cache_drain(uma_zone_t); 2908355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t); 29108cfa56eSMark Johnston static void bucket_cache_reclaim(uma_zone_t zone, bool); 292b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int); 293099a0e58SBosko Milekic static void keg_dtor(void *, int, void *); 294b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int); 2959c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *); 296d4665eaaSJeff Roberson static inline void item_dtor(uma_zone_t zone, void *item, int size, 297d4665eaaSJeff Roberson void *udata, enum zfreeskip skip); 298b23f72e9SBrian Feldman static int zero_init(void *, int, int); 299c6fd3e23SJeff Roberson static void zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 300c6fd3e23SJeff Roberson int itemdomain, bool ws); 30120a4e154SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t, void *), void *); 302a81c400eSJeff Roberson static void zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *), void *); 30320a4e154SJeff Roberson static void zone_timeout(uma_zone_t zone, void *); 3043b2f2cb8SAlexander Motin static int hash_alloc(struct uma_hash *, u_int); 3050aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *); 3060aef6126SJeff Roberson static void hash_free(struct uma_hash *hash); 3078355f576SJeff Roberson static void uma_timeout(void *); 308860bb7a0SMark Johnston static void uma_shutdown(void); 309ab3185d1SJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int, int); 3100095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 3114bd61e19SJeff Roberson static int zone_alloc_limit(uma_zone_t zone, int count, int flags); 3124bd61e19SJeff Roberson static void zone_free_limit(uma_zone_t zone, int count); 31386bbae32SJeff Roberson static void bucket_enable(void); 314cae33c14SJeff Roberson static void bucket_init(void); 3156fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int); 3166fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *); 317cae33c14SJeff Roberson static void bucket_zone_drain(void); 318beb8beefSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int); 3190095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 320bb15d1c7SGleb Smirnoff static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item); 321e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 32285dcf349SGleb Smirnoff uma_fini fini, int align, uint32_t flags); 323b75c4efcSAndrew Turner static int zone_import(void *, void **, int, int, int); 324b75c4efcSAndrew Turner static void zone_release(void *, void **, int); 325beb8beefSJeff Roberson static bool cache_alloc(uma_zone_t, uma_cache_t, void *, int); 3260a81b439SJeff Roberson static bool cache_free(uma_zone_t, uma_cache_t, void *, void *, int); 327bbee39c6SJeff Roberson 3287a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 3297a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 33020a4e154SJeff Roberson static int sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS); 33120a4e154SJeff Roberson static int sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS); 3326d204a6aSRyan Libby static int sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS); 333f7af5015SRyan Libby static int sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS); 3344bd61e19SJeff Roberson static int sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS); 3358355f576SJeff Roberson 33631c251a0SJeff Roberson static uint64_t uma_zone_get_allocs(uma_zone_t zone); 33731c251a0SJeff Roberson 3387029da5cSPawel Biernacki static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 33933e5a1eaSRyan Libby "Memory allocation debugging"); 34033e5a1eaSRyan Libby 3419542ea7bSGleb Smirnoff #ifdef INVARIANTS 34231c251a0SJeff Roberson static uint64_t uma_keg_get_allocs(uma_keg_t zone); 343815db204SRyan Libby static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg); 344815db204SRyan Libby 345c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem); 346c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem); 3479542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item); 3489542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item); 349c5deaf04SGleb Smirnoff 350c5deaf04SGleb Smirnoff static u_int dbg_divisor = 1; 351c5deaf04SGleb Smirnoff SYSCTL_UINT(_vm_debug, OID_AUTO, divisor, 352c5deaf04SGleb Smirnoff CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0, 353c5deaf04SGleb Smirnoff "Debug & thrash every this item in memory allocator"); 354c5deaf04SGleb Smirnoff 355c5deaf04SGleb Smirnoff static counter_u64_t uma_dbg_cnt = EARLY_COUNTER; 356c5deaf04SGleb Smirnoff static counter_u64_t uma_skip_cnt = EARLY_COUNTER; 357c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD, 358c5deaf04SGleb Smirnoff &uma_dbg_cnt, "memory items debugged"); 359c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD, 360c5deaf04SGleb Smirnoff &uma_skip_cnt, "memory items skipped, not debugged"); 3619542ea7bSGleb Smirnoff #endif 3629542ea7bSGleb Smirnoff 3637029da5cSPawel Biernacki SYSCTL_NODE(_vm, OID_AUTO, uma, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 3647029da5cSPawel Biernacki "Universal Memory Allocator"); 36535ec24f3SRyan Libby 366a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_INT, 3677a52a97eSRobert Watson 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 3687a52a97eSRobert Watson 369a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_STRUCT, 3707a52a97eSRobert Watson 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 3717a52a97eSRobert Watson 3722f891cd5SPawel Jakub Dawidek static int zone_warnings = 1; 373af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0, 3742f891cd5SPawel Jakub Dawidek "Warn when UMA zones becomes full"); 3752f891cd5SPawel Jakub Dawidek 37633e5a1eaSRyan Libby static int multipage_slabs = 1; 37733e5a1eaSRyan Libby TUNABLE_INT("vm.debug.uma_multipage_slabs", &multipage_slabs); 37833e5a1eaSRyan Libby SYSCTL_INT(_vm_debug, OID_AUTO, uma_multipage_slabs, 37933e5a1eaSRyan Libby CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &multipage_slabs, 0, 38033e5a1eaSRyan Libby "UMA may choose larger slab sizes for better efficiency"); 38133e5a1eaSRyan Libby 38286bbae32SJeff Roberson /* 3839b8db4d0SRyan Libby * Select the slab zone for an offpage slab with the given maximum item count. 3849b8db4d0SRyan Libby */ 3859b8db4d0SRyan Libby static inline uma_zone_t 3869b8db4d0SRyan Libby slabzone(int ipers) 3879b8db4d0SRyan Libby { 3889b8db4d0SRyan Libby 3899b8db4d0SRyan Libby return (slabzones[ipers > SLABZONE0_SETSIZE]); 3909b8db4d0SRyan Libby } 3919b8db4d0SRyan Libby 3929b8db4d0SRyan Libby /* 39386bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 39486bbae32SJeff Roberson */ 39586bbae32SJeff Roberson static void 39686bbae32SJeff Roberson bucket_enable(void) 39786bbae32SJeff Roberson { 3983182660aSRyan Libby 399a81c400eSJeff Roberson KASSERT(booted >= BOOT_KVA, ("Bucket enable before init")); 400251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 40186bbae32SJeff Roberson } 40286bbae32SJeff Roberson 403dc2c7965SRobert Watson /* 404dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 405dc2c7965SRobert Watson * 406dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 407fc03d22bSJeff Roberson * of the header and an array of pointers. 408dc2c7965SRobert Watson */ 409cae33c14SJeff Roberson static void 410cae33c14SJeff Roberson bucket_init(void) 411cae33c14SJeff Roberson { 412cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 413cae33c14SJeff Roberson int size; 414cae33c14SJeff Roberson 415d74e6a1dSAlan Cox for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 416cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 417cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 418cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 419e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 420dfe13344SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | 421dfe13344SJeff Roberson UMA_ZONE_FIRSTTOUCH); 422cae33c14SJeff Roberson } 423cae33c14SJeff Roberson } 424cae33c14SJeff Roberson 425dc2c7965SRobert Watson /* 426dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 427dc2c7965SRobert Watson * to allocate the bucket. 428dc2c7965SRobert Watson */ 429dc2c7965SRobert Watson static struct uma_bucket_zone * 430dc2c7965SRobert Watson bucket_zone_lookup(int entries) 431dc2c7965SRobert Watson { 432fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 433dc2c7965SRobert Watson 434fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 435fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 436fc03d22bSJeff Roberson return (ubz); 437fc03d22bSJeff Roberson ubz--; 438fc03d22bSJeff Roberson return (ubz); 439fc03d22bSJeff Roberson } 440fc03d22bSJeff Roberson 441003cf08bSMark Johnston static struct uma_bucket_zone * 442003cf08bSMark Johnston bucket_zone_max(uma_zone_t zone, int nitems) 443003cf08bSMark Johnston { 444003cf08bSMark Johnston struct uma_bucket_zone *ubz; 445003cf08bSMark Johnston int bpcpu; 446003cf08bSMark Johnston 447003cf08bSMark Johnston bpcpu = 2; 448dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 449003cf08bSMark Johnston /* Count the cross-domain bucket. */ 450003cf08bSMark Johnston bpcpu++; 451003cf08bSMark Johnston 452003cf08bSMark Johnston for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 453003cf08bSMark Johnston if (ubz->ubz_entries * bpcpu * mp_ncpus > nitems) 454003cf08bSMark Johnston break; 455003cf08bSMark Johnston if (ubz == &bucket_zones[0]) 456003cf08bSMark Johnston ubz = NULL; 457003cf08bSMark Johnston else 458003cf08bSMark Johnston ubz--; 459003cf08bSMark Johnston return (ubz); 460003cf08bSMark Johnston } 461003cf08bSMark Johnston 462fc03d22bSJeff Roberson static int 463fc03d22bSJeff Roberson bucket_select(int size) 464fc03d22bSJeff Roberson { 465fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 466fc03d22bSJeff Roberson 467fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 468fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 469fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 470fc03d22bSJeff Roberson 471fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 472fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 473fc03d22bSJeff Roberson break; 474fc03d22bSJeff Roberson ubz--; 475fc03d22bSJeff Roberson return (ubz->ubz_entries); 476dc2c7965SRobert Watson } 477dc2c7965SRobert Watson 478cae33c14SJeff Roberson static uma_bucket_t 4796fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags) 480cae33c14SJeff Roberson { 481cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 482cae33c14SJeff Roberson uma_bucket_t bucket; 483cae33c14SJeff Roberson 484cae33c14SJeff Roberson /* 485d4665eaaSJeff Roberson * Don't allocate buckets early in boot. 486cae33c14SJeff Roberson */ 487d4665eaaSJeff Roberson if (__predict_false(booted < BOOT_KVA)) 488cae33c14SJeff Roberson return (NULL); 489a81c400eSJeff Roberson 4906fd34d6fSJeff Roberson /* 4916fd34d6fSJeff Roberson * To limit bucket recursion we store the original zone flags 4926fd34d6fSJeff Roberson * in a cookie passed via zalloc_arg/zfree_arg. This allows the 4936fd34d6fSJeff Roberson * NOVM flag to persist even through deep recursions. We also 4946fd34d6fSJeff Roberson * store ZFLAG_BUCKET once we have recursed attempting to allocate 4956fd34d6fSJeff Roberson * a bucket for a bucket zone so we do not allow infinite bucket 4966fd34d6fSJeff Roberson * recursion. This cookie will even persist to frees of unused 4976fd34d6fSJeff Roberson * buckets via the allocation path or bucket allocations in the 4986fd34d6fSJeff Roberson * free path. 4996fd34d6fSJeff Roberson */ 5006fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 5016fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 502e8a720feSAlexander Motin else { 503e8a720feSAlexander Motin if ((uintptr_t)udata & UMA_ZFLAG_BUCKET) 504e8a720feSAlexander Motin return (NULL); 5056fd34d6fSJeff Roberson udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET); 506e8a720feSAlexander Motin } 507bae55c4aSRyan Libby if (((uintptr_t)udata & UMA_ZONE_VM) != 0) 508af526374SJeff Roberson flags |= M_NOVM; 50920a4e154SJeff Roberson ubz = bucket_zone_lookup(zone->uz_bucket_size); 51020d3ab87SAlexander Motin if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0) 51120d3ab87SAlexander Motin ubz++; 5126fd34d6fSJeff Roberson bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags); 513cae33c14SJeff Roberson if (bucket) { 514cae33c14SJeff Roberson #ifdef INVARIANTS 515cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 516cae33c14SJeff Roberson #endif 517cae33c14SJeff Roberson bucket->ub_cnt = 0; 518cae33c14SJeff Roberson bucket->ub_entries = ubz->ubz_entries; 519d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 520d4665eaaSJeff Roberson CTR3(KTR_UMA, "bucket_alloc: zone %s(%p) allocated bucket %p", 521d4665eaaSJeff Roberson zone->uz_name, zone, bucket); 522cae33c14SJeff Roberson } 523cae33c14SJeff Roberson 524cae33c14SJeff Roberson return (bucket); 525cae33c14SJeff Roberson } 526cae33c14SJeff Roberson 527cae33c14SJeff Roberson static void 5286fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata) 529cae33c14SJeff Roberson { 530cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 531cae33c14SJeff Roberson 532c6fd3e23SJeff Roberson if (bucket->ub_cnt != 0) 533c6fd3e23SJeff Roberson bucket_drain(zone, bucket); 534c6fd3e23SJeff Roberson 535fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 536fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 537d4665eaaSJeff Roberson KASSERT(bucket->ub_seq == SMR_SEQ_INVALID, 538d4665eaaSJeff Roberson ("bucket_free: Freeing an SMR bucket.")); 5396fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 5406fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 541dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 5426fd34d6fSJeff Roberson uma_zfree_arg(ubz->ubz_zone, bucket, udata); 543cae33c14SJeff Roberson } 544cae33c14SJeff Roberson 545cae33c14SJeff Roberson static void 546cae33c14SJeff Roberson bucket_zone_drain(void) 547cae33c14SJeff Roberson { 548cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 549cae33c14SJeff Roberson 550cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 55108cfa56eSMark Johnston uma_zone_reclaim(ubz->ubz_zone, UMA_RECLAIM_DRAIN); 552cae33c14SJeff Roberson } 553cae33c14SJeff Roberson 55408cfa56eSMark Johnston /* 555c6fd3e23SJeff Roberson * Acquire the domain lock and record contention. 556c6fd3e23SJeff Roberson */ 557c6fd3e23SJeff Roberson static uma_zone_domain_t 558c6fd3e23SJeff Roberson zone_domain_lock(uma_zone_t zone, int domain) 559c6fd3e23SJeff Roberson { 560c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 561c6fd3e23SJeff Roberson bool lockfail; 562c6fd3e23SJeff Roberson 563c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 564c6fd3e23SJeff Roberson lockfail = false; 565c6fd3e23SJeff Roberson if (ZDOM_OWNED(zdom)) 566c6fd3e23SJeff Roberson lockfail = true; 567c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 568c6fd3e23SJeff Roberson /* This is unsynchronized. The counter does not need to be precise. */ 569c6fd3e23SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 570c6fd3e23SJeff Roberson zone->uz_bucket_size++; 571c6fd3e23SJeff Roberson return (zdom); 572c6fd3e23SJeff Roberson } 573c6fd3e23SJeff Roberson 574c6fd3e23SJeff Roberson /* 575fe835cbfSJeff Roberson * Search for the domain with the least cached items and return it if it 576fe835cbfSJeff Roberson * is out of balance with the preferred domain. 577c6fd3e23SJeff Roberson */ 578c6fd3e23SJeff Roberson static __noinline int 579c6fd3e23SJeff Roberson zone_domain_lowest(uma_zone_t zone, int pref) 580c6fd3e23SJeff Roberson { 581fe835cbfSJeff Roberson long least, nitems, prefitems; 582c6fd3e23SJeff Roberson int domain; 583c6fd3e23SJeff Roberson int i; 584c6fd3e23SJeff Roberson 585fe835cbfSJeff Roberson prefitems = least = LONG_MAX; 586c6fd3e23SJeff Roberson domain = 0; 587c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 588c6fd3e23SJeff Roberson nitems = ZDOM_GET(zone, i)->uzd_nitems; 589c6fd3e23SJeff Roberson if (nitems < least) { 590c6fd3e23SJeff Roberson domain = i; 591c6fd3e23SJeff Roberson least = nitems; 592c6fd3e23SJeff Roberson } 593fe835cbfSJeff Roberson if (domain == pref) 594fe835cbfSJeff Roberson prefitems = nitems; 595fe835cbfSJeff Roberson } 596fe835cbfSJeff Roberson if (prefitems < least * 2) 597fe835cbfSJeff Roberson return (pref); 598c6fd3e23SJeff Roberson 599c6fd3e23SJeff Roberson return (domain); 600c6fd3e23SJeff Roberson } 601c6fd3e23SJeff Roberson 602c6fd3e23SJeff Roberson /* 603c6fd3e23SJeff Roberson * Search for the domain with the most cached items and return it or the 604c6fd3e23SJeff Roberson * preferred domain if it has enough to proceed. 605c6fd3e23SJeff Roberson */ 606c6fd3e23SJeff Roberson static __noinline int 607c6fd3e23SJeff Roberson zone_domain_highest(uma_zone_t zone, int pref) 608c6fd3e23SJeff Roberson { 609c6fd3e23SJeff Roberson long most, nitems; 610c6fd3e23SJeff Roberson int domain; 611c6fd3e23SJeff Roberson int i; 612c6fd3e23SJeff Roberson 613c6fd3e23SJeff Roberson if (ZDOM_GET(zone, pref)->uzd_nitems > BUCKET_MAX) 614c6fd3e23SJeff Roberson return (pref); 615c6fd3e23SJeff Roberson 616c6fd3e23SJeff Roberson most = 0; 617c6fd3e23SJeff Roberson domain = 0; 618c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 619c6fd3e23SJeff Roberson nitems = ZDOM_GET(zone, i)->uzd_nitems; 620c6fd3e23SJeff Roberson if (nitems > most) { 621c6fd3e23SJeff Roberson domain = i; 622c6fd3e23SJeff Roberson most = nitems; 623c6fd3e23SJeff Roberson } 624c6fd3e23SJeff Roberson } 625c6fd3e23SJeff Roberson 626c6fd3e23SJeff Roberson return (domain); 627c6fd3e23SJeff Roberson } 628c6fd3e23SJeff Roberson 629c6fd3e23SJeff Roberson /* 630c6fd3e23SJeff Roberson * Safely subtract cnt from imax. 631c6fd3e23SJeff Roberson */ 632c6fd3e23SJeff Roberson static void 633c6fd3e23SJeff Roberson zone_domain_imax_sub(uma_zone_domain_t zdom, int cnt) 634c6fd3e23SJeff Roberson { 635c6fd3e23SJeff Roberson long new; 636c6fd3e23SJeff Roberson long old; 637c6fd3e23SJeff Roberson 638c6fd3e23SJeff Roberson old = zdom->uzd_imax; 639c6fd3e23SJeff Roberson do { 640c6fd3e23SJeff Roberson if (old <= cnt) 641c6fd3e23SJeff Roberson new = 0; 642c6fd3e23SJeff Roberson else 643c6fd3e23SJeff Roberson new = old - cnt; 644c6fd3e23SJeff Roberson } while (atomic_fcmpset_long(&zdom->uzd_imax, &old, new) == 0); 645c6fd3e23SJeff Roberson } 646c6fd3e23SJeff Roberson 647c6fd3e23SJeff Roberson /* 648c6fd3e23SJeff Roberson * Set the maximum imax value. 649c6fd3e23SJeff Roberson */ 650c6fd3e23SJeff Roberson static void 651c6fd3e23SJeff Roberson zone_domain_imax_set(uma_zone_domain_t zdom, int nitems) 652c6fd3e23SJeff Roberson { 653c6fd3e23SJeff Roberson long old; 654c6fd3e23SJeff Roberson 655c6fd3e23SJeff Roberson old = zdom->uzd_imax; 656c6fd3e23SJeff Roberson do { 657c6fd3e23SJeff Roberson if (old >= nitems) 658c6fd3e23SJeff Roberson break; 659c6fd3e23SJeff Roberson } while (atomic_fcmpset_long(&zdom->uzd_imax, &old, nitems) == 0); 660c6fd3e23SJeff Roberson } 661c6fd3e23SJeff Roberson 662c6fd3e23SJeff Roberson /* 66308cfa56eSMark Johnston * Attempt to satisfy an allocation by retrieving a full bucket from one of the 664d4665eaaSJeff Roberson * zone's caches. If a bucket is found the zone is not locked on return. 66508cfa56eSMark Johnston */ 6660f9b7bf3SMark Johnston static uma_bucket_t 667c6fd3e23SJeff Roberson zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom, bool reclaim) 6680f9b7bf3SMark Johnston { 6690f9b7bf3SMark Johnston uma_bucket_t bucket; 670d4665eaaSJeff Roberson int i; 671d4665eaaSJeff Roberson bool dtor = false; 6720f9b7bf3SMark Johnston 673c6fd3e23SJeff Roberson ZDOM_LOCK_ASSERT(zdom); 6740f9b7bf3SMark Johnston 675dc3915c8SJeff Roberson if ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) == NULL) 676d4665eaaSJeff Roberson return (NULL); 677d4665eaaSJeff Roberson 678543117beSJeff Roberson /* SMR Buckets can not be re-used until readers expire. */ 679d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 680d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 681d4665eaaSJeff Roberson if (!smr_poll(zone->uz_smr, bucket->ub_seq, false)) 682d4665eaaSJeff Roberson return (NULL); 683d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 684543117beSJeff Roberson dtor = (zone->uz_dtor != NULL) || UMA_ALWAYS_CTORDTOR; 685c6fd3e23SJeff Roberson if (STAILQ_NEXT(bucket, ub_link) != NULL) 686c6fd3e23SJeff Roberson zdom->uzd_seq = STAILQ_NEXT(bucket, ub_link)->ub_seq; 687d4665eaaSJeff Roberson } 6880f9b7bf3SMark Johnston MPASS(zdom->uzd_nitems >= bucket->ub_cnt); 689dc3915c8SJeff Roberson STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link); 6900f9b7bf3SMark Johnston zdom->uzd_nitems -= bucket->ub_cnt; 691c6fd3e23SJeff Roberson 692c6fd3e23SJeff Roberson /* 693c6fd3e23SJeff Roberson * Shift the bounds of the current WSS interval to avoid 694c6fd3e23SJeff Roberson * perturbing the estimate. 695c6fd3e23SJeff Roberson */ 696c6fd3e23SJeff Roberson if (reclaim) { 697c6fd3e23SJeff Roberson zdom->uzd_imin -= lmin(zdom->uzd_imin, bucket->ub_cnt); 698c6fd3e23SJeff Roberson zone_domain_imax_sub(zdom, bucket->ub_cnt); 699c6fd3e23SJeff Roberson } else if (zdom->uzd_imin > zdom->uzd_nitems) 7000f9b7bf3SMark Johnston zdom->uzd_imin = zdom->uzd_nitems; 701c6fd3e23SJeff Roberson 702c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 703d4665eaaSJeff Roberson if (dtor) 704d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 705d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], zone->uz_size, 706d4665eaaSJeff Roberson NULL, SKIP_NONE); 707d4665eaaSJeff Roberson 7080f9b7bf3SMark Johnston return (bucket); 7090f9b7bf3SMark Johnston } 7100f9b7bf3SMark Johnston 71108cfa56eSMark Johnston /* 71208cfa56eSMark Johnston * Insert a full bucket into the specified cache. The "ws" parameter indicates 71308cfa56eSMark Johnston * whether the bucket's contents should be counted as part of the zone's working 714c6fd3e23SJeff Roberson * set. The bucket may be freed if it exceeds the bucket limit. 71508cfa56eSMark Johnston */ 7160f9b7bf3SMark Johnston static void 717c6fd3e23SJeff Roberson zone_put_bucket(uma_zone_t zone, int domain, uma_bucket_t bucket, void *udata, 7180f9b7bf3SMark Johnston const bool ws) 7190f9b7bf3SMark Johnston { 720c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 7210f9b7bf3SMark Johnston 722c6fd3e23SJeff Roberson /* We don't cache empty buckets. This can happen after a reclaim. */ 723c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 724c6fd3e23SJeff Roberson goto out; 725c6fd3e23SJeff Roberson zdom = zone_domain_lock(zone, domain); 726c6fd3e23SJeff Roberson 727c6fd3e23SJeff Roberson /* 728c6fd3e23SJeff Roberson * Conditionally set the maximum number of items. 729c6fd3e23SJeff Roberson */ 7300f9b7bf3SMark Johnston zdom->uzd_nitems += bucket->ub_cnt; 731c6fd3e23SJeff Roberson if (__predict_true(zdom->uzd_nitems < zone->uz_bucket_max)) { 732c6fd3e23SJeff Roberson if (ws) 733c6fd3e23SJeff Roberson zone_domain_imax_set(zdom, zdom->uzd_nitems); 734c6fd3e23SJeff Roberson if (STAILQ_EMPTY(&zdom->uzd_buckets)) 735c6fd3e23SJeff Roberson zdom->uzd_seq = bucket->ub_seq; 736c6fd3e23SJeff Roberson STAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link); 737c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 738c6fd3e23SJeff Roberson return; 739c6fd3e23SJeff Roberson } 740c6fd3e23SJeff Roberson zdom->uzd_nitems -= bucket->ub_cnt; 741c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 742c6fd3e23SJeff Roberson out: 743c6fd3e23SJeff Roberson bucket_free(zone, bucket, udata); 7440f9b7bf3SMark Johnston } 7450f9b7bf3SMark Johnston 746376b1ba3SJeff Roberson /* Pops an item out of a per-cpu cache bucket. */ 747376b1ba3SJeff Roberson static inline void * 748376b1ba3SJeff Roberson cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket) 749376b1ba3SJeff Roberson { 750376b1ba3SJeff Roberson void *item; 751376b1ba3SJeff Roberson 752376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 753376b1ba3SJeff Roberson 754376b1ba3SJeff Roberson bucket->ucb_cnt--; 755376b1ba3SJeff Roberson item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt]; 756376b1ba3SJeff Roberson #ifdef INVARIANTS 757376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL; 758376b1ba3SJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 759376b1ba3SJeff Roberson #endif 760376b1ba3SJeff Roberson cache->uc_allocs++; 761376b1ba3SJeff Roberson 762376b1ba3SJeff Roberson return (item); 763376b1ba3SJeff Roberson } 764376b1ba3SJeff Roberson 765376b1ba3SJeff Roberson /* Pushes an item into a per-cpu cache bucket. */ 766376b1ba3SJeff Roberson static inline void 767376b1ba3SJeff Roberson cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item) 768376b1ba3SJeff Roberson { 769376b1ba3SJeff Roberson 770376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 771376b1ba3SJeff Roberson KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL, 772376b1ba3SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 773376b1ba3SJeff Roberson 774376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item; 775376b1ba3SJeff Roberson bucket->ucb_cnt++; 776376b1ba3SJeff Roberson cache->uc_frees++; 777376b1ba3SJeff Roberson } 778376b1ba3SJeff Roberson 779376b1ba3SJeff Roberson /* 780376b1ba3SJeff Roberson * Unload a UMA bucket from a per-cpu cache. 781376b1ba3SJeff Roberson */ 782376b1ba3SJeff Roberson static inline uma_bucket_t 783376b1ba3SJeff Roberson cache_bucket_unload(uma_cache_bucket_t bucket) 784376b1ba3SJeff Roberson { 785376b1ba3SJeff Roberson uma_bucket_t b; 786376b1ba3SJeff Roberson 787376b1ba3SJeff Roberson b = bucket->ucb_bucket; 788376b1ba3SJeff Roberson if (b != NULL) { 789376b1ba3SJeff Roberson MPASS(b->ub_entries == bucket->ucb_entries); 790376b1ba3SJeff Roberson b->ub_cnt = bucket->ucb_cnt; 791376b1ba3SJeff Roberson bucket->ucb_bucket = NULL; 792376b1ba3SJeff Roberson bucket->ucb_entries = bucket->ucb_cnt = 0; 793376b1ba3SJeff Roberson } 794376b1ba3SJeff Roberson 795376b1ba3SJeff Roberson return (b); 796376b1ba3SJeff Roberson } 797376b1ba3SJeff Roberson 798376b1ba3SJeff Roberson static inline uma_bucket_t 799376b1ba3SJeff Roberson cache_bucket_unload_alloc(uma_cache_t cache) 800376b1ba3SJeff Roberson { 801376b1ba3SJeff Roberson 802376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_allocbucket)); 803376b1ba3SJeff Roberson } 804376b1ba3SJeff Roberson 805376b1ba3SJeff Roberson static inline uma_bucket_t 806376b1ba3SJeff Roberson cache_bucket_unload_free(uma_cache_t cache) 807376b1ba3SJeff Roberson { 808376b1ba3SJeff Roberson 809376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_freebucket)); 810376b1ba3SJeff Roberson } 811376b1ba3SJeff Roberson 812376b1ba3SJeff Roberson static inline uma_bucket_t 813376b1ba3SJeff Roberson cache_bucket_unload_cross(uma_cache_t cache) 814376b1ba3SJeff Roberson { 815376b1ba3SJeff Roberson 816376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_crossbucket)); 817376b1ba3SJeff Roberson } 818376b1ba3SJeff Roberson 819376b1ba3SJeff Roberson /* 820376b1ba3SJeff Roberson * Load a bucket into a per-cpu cache bucket. 821376b1ba3SJeff Roberson */ 822376b1ba3SJeff Roberson static inline void 823376b1ba3SJeff Roberson cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b) 824376b1ba3SJeff Roberson { 825376b1ba3SJeff Roberson 826376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 827376b1ba3SJeff Roberson MPASS(bucket->ucb_bucket == NULL); 828543117beSJeff Roberson MPASS(b->ub_seq == SMR_SEQ_INVALID); 829376b1ba3SJeff Roberson 830376b1ba3SJeff Roberson bucket->ucb_bucket = b; 831376b1ba3SJeff Roberson bucket->ucb_cnt = b->ub_cnt; 832376b1ba3SJeff Roberson bucket->ucb_entries = b->ub_entries; 833376b1ba3SJeff Roberson } 834376b1ba3SJeff Roberson 835376b1ba3SJeff Roberson static inline void 836376b1ba3SJeff Roberson cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b) 837376b1ba3SJeff Roberson { 838376b1ba3SJeff Roberson 839376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_allocbucket, b); 840376b1ba3SJeff Roberson } 841376b1ba3SJeff Roberson 842376b1ba3SJeff Roberson static inline void 843376b1ba3SJeff Roberson cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b) 844376b1ba3SJeff Roberson { 845376b1ba3SJeff Roberson 846376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_freebucket, b); 847376b1ba3SJeff Roberson } 848376b1ba3SJeff Roberson 849dfe13344SJeff Roberson #ifdef NUMA 850376b1ba3SJeff Roberson static inline void 851376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b) 852376b1ba3SJeff Roberson { 853376b1ba3SJeff Roberson 854376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_crossbucket, b); 855376b1ba3SJeff Roberson } 856376b1ba3SJeff Roberson #endif 857376b1ba3SJeff Roberson 858376b1ba3SJeff Roberson /* 859376b1ba3SJeff Roberson * Copy and preserve ucb_spare. 860376b1ba3SJeff Roberson */ 861376b1ba3SJeff Roberson static inline void 862376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 863376b1ba3SJeff Roberson { 864376b1ba3SJeff Roberson 865376b1ba3SJeff Roberson b1->ucb_bucket = b2->ucb_bucket; 866376b1ba3SJeff Roberson b1->ucb_entries = b2->ucb_entries; 867376b1ba3SJeff Roberson b1->ucb_cnt = b2->ucb_cnt; 868376b1ba3SJeff Roberson } 869376b1ba3SJeff Roberson 870376b1ba3SJeff Roberson /* 871376b1ba3SJeff Roberson * Swap two cache buckets. 872376b1ba3SJeff Roberson */ 873376b1ba3SJeff Roberson static inline void 874376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 875376b1ba3SJeff Roberson { 876376b1ba3SJeff Roberson struct uma_cache_bucket b3; 877376b1ba3SJeff Roberson 878376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 879376b1ba3SJeff Roberson 880376b1ba3SJeff Roberson cache_bucket_copy(&b3, b1); 881376b1ba3SJeff Roberson cache_bucket_copy(b1, b2); 882376b1ba3SJeff Roberson cache_bucket_copy(b2, &b3); 883376b1ba3SJeff Roberson } 884376b1ba3SJeff Roberson 885c6fd3e23SJeff Roberson /* 886c6fd3e23SJeff Roberson * Attempt to fetch a bucket from a zone on behalf of the current cpu cache. 887c6fd3e23SJeff Roberson */ 888c6fd3e23SJeff Roberson static uma_bucket_t 889c6fd3e23SJeff Roberson cache_fetch_bucket(uma_zone_t zone, uma_cache_t cache, int domain) 890c6fd3e23SJeff Roberson { 891c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 892c6fd3e23SJeff Roberson uma_bucket_t bucket; 893c6fd3e23SJeff Roberson 894c6fd3e23SJeff Roberson /* 895c6fd3e23SJeff Roberson * Avoid the lock if possible. 896c6fd3e23SJeff Roberson */ 897c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 898c6fd3e23SJeff Roberson if (zdom->uzd_nitems == 0) 899c6fd3e23SJeff Roberson return (NULL); 900c6fd3e23SJeff Roberson 901c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_SMR) != 0 && 902c6fd3e23SJeff Roberson !smr_poll(zone->uz_smr, zdom->uzd_seq, false)) 903c6fd3e23SJeff Roberson return (NULL); 904c6fd3e23SJeff Roberson 905c6fd3e23SJeff Roberson /* 906c6fd3e23SJeff Roberson * Check the zone's cache of buckets. 907c6fd3e23SJeff Roberson */ 908c6fd3e23SJeff Roberson zdom = zone_domain_lock(zone, domain); 909c6fd3e23SJeff Roberson if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) { 910c6fd3e23SJeff Roberson KASSERT(bucket->ub_cnt != 0, 911c6fd3e23SJeff Roberson ("cache_fetch_bucket: Returning an empty bucket.")); 912c6fd3e23SJeff Roberson return (bucket); 913c6fd3e23SJeff Roberson } 914c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 915c6fd3e23SJeff Roberson 916c6fd3e23SJeff Roberson return (NULL); 917c6fd3e23SJeff Roberson } 918c6fd3e23SJeff Roberson 9192f891cd5SPawel Jakub Dawidek static void 9202f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 9212f891cd5SPawel Jakub Dawidek { 9222f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 9232f891cd5SPawel Jakub Dawidek 9242f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 9252f891cd5SPawel Jakub Dawidek return; 9262f891cd5SPawel Jakub Dawidek 9272f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 9282f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 9292f891cd5SPawel Jakub Dawidek } 9302f891cd5SPawel Jakub Dawidek 93154503a13SJonathan T. Looney static inline void 93254503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone) 93354503a13SJonathan T. Looney { 934e60b2fcbSGleb Smirnoff 935e60b2fcbSGleb Smirnoff if (zone->uz_maxaction.ta_func != NULL) 936e60b2fcbSGleb Smirnoff taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction); 93754503a13SJonathan T. Looney } 93854503a13SJonathan T. Looney 9398355f576SJeff Roberson /* 9408355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 9419643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 9428355f576SJeff Roberson * 9438355f576SJeff Roberson * Arguments: 9448355f576SJeff Roberson * arg Unused 9458355f576SJeff Roberson * 9468355f576SJeff Roberson * Returns: 9478355f576SJeff Roberson * Nothing 9488355f576SJeff Roberson */ 9498355f576SJeff Roberson static void 9508355f576SJeff Roberson uma_timeout(void *unused) 9518355f576SJeff Roberson { 95286bbae32SJeff Roberson bucket_enable(); 95320a4e154SJeff Roberson zone_foreach(zone_timeout, NULL); 9548355f576SJeff Roberson 9558355f576SJeff Roberson /* Reschedule this event */ 9569643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 9578355f576SJeff Roberson } 9588355f576SJeff Roberson 9598355f576SJeff Roberson /* 9600f9b7bf3SMark Johnston * Update the working set size estimate for the zone's bucket cache. 9610f9b7bf3SMark Johnston * The constants chosen here are somewhat arbitrary. With an update period of 9620f9b7bf3SMark Johnston * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the 9630f9b7bf3SMark Johnston * last 100s. 9640f9b7bf3SMark Johnston */ 9650f9b7bf3SMark Johnston static void 9660f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom) 9670f9b7bf3SMark Johnston { 9680f9b7bf3SMark Johnston long wss; 9690f9b7bf3SMark Johnston 970c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 9710f9b7bf3SMark Johnston MPASS(zdom->uzd_imax >= zdom->uzd_imin); 9720f9b7bf3SMark Johnston wss = zdom->uzd_imax - zdom->uzd_imin; 9730f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems; 97408cfa56eSMark Johnston zdom->uzd_wss = (4 * wss + zdom->uzd_wss) / 5; 975c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 9760f9b7bf3SMark Johnston } 9770f9b7bf3SMark Johnston 9780f9b7bf3SMark Johnston /* 9799643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 9809643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 9818355f576SJeff Roberson * 982e20a199fSJeff Roberson * Returns nothing. 9838355f576SJeff Roberson */ 9848355f576SJeff Roberson static void 98520a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused) 9868355f576SJeff Roberson { 98708034d10SKonstantin Belousov uma_keg_t keg; 9888b987a77SJeff Roberson u_int slabs, pages; 9898355f576SJeff Roberson 99054c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 99108034d10SKonstantin Belousov goto update_wss; 99208034d10SKonstantin Belousov 99308034d10SKonstantin Belousov keg = zone->uz_keg; 9948b987a77SJeff Roberson 9958b987a77SJeff Roberson /* 9968b987a77SJeff Roberson * Hash zones are non-numa by definition so the first domain 9978b987a77SJeff Roberson * is the only one present. 9988b987a77SJeff Roberson */ 9998b987a77SJeff Roberson KEG_LOCK(keg, 0); 10008b987a77SJeff Roberson pages = keg->uk_domain[0].ud_pages; 10018b987a77SJeff Roberson 10028355f576SJeff Roberson /* 1003e20a199fSJeff Roberson * Expand the keg hash table. 10048355f576SJeff Roberson * 10058355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 10068355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 10078355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 10088355f576SJeff Roberson */ 10098b987a77SJeff Roberson if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) { 10100aef6126SJeff Roberson struct uma_hash newhash; 10110aef6126SJeff Roberson struct uma_hash oldhash; 10120aef6126SJeff Roberson int ret; 10135300d9ddSJeff Roberson 10140aef6126SJeff Roberson /* 10150aef6126SJeff Roberson * This is so involved because allocating and freeing 1016e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 10170aef6126SJeff Roberson * I have to do everything in stages and check for 10180aef6126SJeff Roberson * races. 10190aef6126SJeff Roberson */ 10208b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 10213b2f2cb8SAlexander Motin ret = hash_alloc(&newhash, 1 << fls(slabs)); 10228b987a77SJeff Roberson KEG_LOCK(keg, 0); 10230aef6126SJeff Roberson if (ret) { 1024099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 1025099a0e58SBosko Milekic oldhash = keg->uk_hash; 1026099a0e58SBosko Milekic keg->uk_hash = newhash; 10270aef6126SJeff Roberson } else 10280aef6126SJeff Roberson oldhash = newhash; 10290aef6126SJeff Roberson 10308b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 10310aef6126SJeff Roberson hash_free(&oldhash); 10328b987a77SJeff Roberson goto update_wss; 10330aef6126SJeff Roberson } 10345300d9ddSJeff Roberson } 10358b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 1036e20a199fSJeff Roberson 103708034d10SKonstantin Belousov update_wss: 1038bb15d1c7SGleb Smirnoff for (int i = 0; i < vm_ndomains; i++) 1039c6fd3e23SJeff Roberson zone_domain_update_wss(ZDOM_GET(zone, i)); 10408355f576SJeff Roberson } 10418355f576SJeff Roberson 10428355f576SJeff Roberson /* 10435300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 10445300d9ddSJeff Roberson * backing store. 10455300d9ddSJeff Roberson * 10465300d9ddSJeff Roberson * Arguments: 10470aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 10485300d9ddSJeff Roberson * 10495300d9ddSJeff Roberson * Returns: 1050763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 10515300d9ddSJeff Roberson */ 105237c84183SPoul-Henning Kamp static int 10533b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 10545300d9ddSJeff Roberson { 105559568a0eSAlexander Motin size_t alloc; 10565300d9ddSJeff Roberson 10573b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 10583b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 10593b2f2cb8SAlexander Motin hash->uh_hashsize = size; 10600aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 10611e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 10625300d9ddSJeff Roberson } else { 10630aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 1064e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 1065ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 10660aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 10675300d9ddSJeff Roberson } 10680aef6126SJeff Roberson if (hash->uh_slab_hash) { 10690aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 10700aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 10710aef6126SJeff Roberson return (1); 10720aef6126SJeff Roberson } 10735300d9ddSJeff Roberson 10740aef6126SJeff Roberson return (0); 10755300d9ddSJeff Roberson } 10765300d9ddSJeff Roberson 10775300d9ddSJeff Roberson /* 107864f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 107964f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 108064f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 10818355f576SJeff Roberson * 10828355f576SJeff Roberson * Arguments: 10830aef6126SJeff Roberson * oldhash The hash you want to expand 10840aef6126SJeff Roberson * newhash The hash structure for the new table 10858355f576SJeff Roberson * 10868355f576SJeff Roberson * Returns: 10878355f576SJeff Roberson * Nothing 10888355f576SJeff Roberson * 10898355f576SJeff Roberson * Discussion: 10908355f576SJeff Roberson */ 10910aef6126SJeff Roberson static int 10920aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 10938355f576SJeff Roberson { 10941e0701e1SJeff Roberson uma_hash_slab_t slab; 10956929b7d1SPedro F. Giffuni u_int hval; 10966929b7d1SPedro F. Giffuni u_int idx; 10978355f576SJeff Roberson 10980aef6126SJeff Roberson if (!newhash->uh_slab_hash) 10990aef6126SJeff Roberson return (0); 11008355f576SJeff Roberson 11010aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 11020aef6126SJeff Roberson return (0); 11038355f576SJeff Roberson 11048355f576SJeff Roberson /* 11058355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 11068355f576SJeff Roberson * full rehash. 11078355f576SJeff Roberson */ 11088355f576SJeff Roberson 11096929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 11101e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 11111e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 11121e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 11131e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 11141e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 11151e0701e1SJeff Roberson slab, uhs_hlink); 11168355f576SJeff Roberson } 11178355f576SJeff Roberson 11180aef6126SJeff Roberson return (1); 11199c2cd7e5SJeff Roberson } 11209c2cd7e5SJeff Roberson 11215300d9ddSJeff Roberson /* 11225300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 11235300d9ddSJeff Roberson * 11245300d9ddSJeff Roberson * Arguments: 11255300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 11265300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 11275300d9ddSJeff Roberson * 11285300d9ddSJeff Roberson * Returns: 11295300d9ddSJeff Roberson * Nothing 11305300d9ddSJeff Roberson */ 11319c2cd7e5SJeff Roberson static void 11320aef6126SJeff Roberson hash_free(struct uma_hash *hash) 11339c2cd7e5SJeff Roberson { 11340aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 11350aef6126SJeff Roberson return; 11360aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 11370095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 11388355f576SJeff Roberson else 1139961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 11408355f576SJeff Roberson } 11418355f576SJeff Roberson 11428355f576SJeff Roberson /* 11438355f576SJeff Roberson * Frees all outstanding items in a bucket 11448355f576SJeff Roberson * 11458355f576SJeff Roberson * Arguments: 11468355f576SJeff Roberson * zone The zone to free to, must be unlocked. 11474bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 11488355f576SJeff Roberson * 11498355f576SJeff Roberson * Returns: 11508355f576SJeff Roberson * Nothing 11518355f576SJeff Roberson */ 11528355f576SJeff Roberson static void 11538355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 11548355f576SJeff Roberson { 11550095a784SJeff Roberson int i; 11568355f576SJeff Roberson 1157c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 11588355f576SJeff Roberson return; 11598355f576SJeff Roberson 1160d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 1161d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 1162d4665eaaSJeff Roberson smr_wait(zone->uz_smr, bucket->ub_seq); 1163543117beSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 1164d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 1165d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], 1166d4665eaaSJeff Roberson zone->uz_size, NULL, SKIP_NONE); 1167d4665eaaSJeff Roberson } 11680095a784SJeff Roberson if (zone->uz_fini) 11690095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 11700095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 11710095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 11724bd61e19SJeff Roberson if (zone->uz_max_items > 0) 11734bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 1174d4665eaaSJeff Roberson #ifdef INVARIANTS 1175d4665eaaSJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt); 1176d4665eaaSJeff Roberson #endif 11770095a784SJeff Roberson bucket->ub_cnt = 0; 11788355f576SJeff Roberson } 11798355f576SJeff Roberson 11808355f576SJeff Roberson /* 11818355f576SJeff Roberson * Drains the per cpu caches for a zone. 11828355f576SJeff Roberson * 1183727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 11845d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 11855d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 11865d1ae027SRobert Watson * 11878355f576SJeff Roberson * Arguments: 11888355f576SJeff Roberson * zone The zone to drain, must be unlocked. 11898355f576SJeff Roberson * 11908355f576SJeff Roberson * Returns: 11918355f576SJeff Roberson * Nothing 11928355f576SJeff Roberson */ 11938355f576SJeff Roberson static void 11949643769aSJeff Roberson cache_drain(uma_zone_t zone) 11958355f576SJeff Roberson { 11968355f576SJeff Roberson uma_cache_t cache; 1197376b1ba3SJeff Roberson uma_bucket_t bucket; 1198543117beSJeff Roberson smr_seq_t seq; 11998355f576SJeff Roberson int cpu; 12008355f576SJeff Roberson 12018355f576SJeff Roberson /* 12025d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 12035d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 12045d1ae027SRobert Watson * of the caches at this point. 12055d1ae027SRobert Watson * 12065d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 12075d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 12088355f576SJeff Roberson */ 1209543117beSJeff Roberson seq = SMR_SEQ_INVALID; 1210543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 1211226dd6dbSJeff Roberson seq = smr_advance(zone->uz_smr); 12123aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 12138355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 1214376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 1215c6fd3e23SJeff Roberson if (bucket != NULL) 1216376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1217376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 1218376b1ba3SJeff Roberson if (bucket != NULL) { 1219543117beSJeff Roberson bucket->ub_seq = seq; 1220376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1221376b1ba3SJeff Roberson } 1222376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 1223376b1ba3SJeff Roberson if (bucket != NULL) { 1224543117beSJeff Roberson bucket->ub_seq = seq; 1225376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1226376b1ba3SJeff Roberson } 1227d56368d7SBosko Milekic } 122808cfa56eSMark Johnston bucket_cache_reclaim(zone, true); 1229aaa8bb16SJeff Roberson } 1230aaa8bb16SJeff Roberson 1231a2de44abSAlexander Motin static void 123220a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 1233a2de44abSAlexander Motin { 1234a2de44abSAlexander Motin 1235a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1236a2de44abSAlexander Motin return; 1237a2de44abSAlexander Motin 123820a4e154SJeff Roberson zone->uz_bucket_size = 123920a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 1240a2de44abSAlexander Motin } 1241a2de44abSAlexander Motin 1242a2de44abSAlexander Motin static void 124320a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 1244a2de44abSAlexander Motin { 1245a2de44abSAlexander Motin uma_cache_t cache; 1246c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 1247ab3185d1SJeff Roberson int domain; 1248a2de44abSAlexander Motin 1249a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1250a2de44abSAlexander Motin return; 1251a2de44abSAlexander Motin 1252c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1253a2de44abSAlexander Motin critical_enter(); 1254a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1255c6fd3e23SJeff Roberson domain = PCPU_GET(domain); 1256376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1257d4665eaaSJeff Roberson 1258d4665eaaSJeff Roberson /* 1259d4665eaaSJeff Roberson * Don't flush SMR zone buckets. This leaves the zone without a 1260d4665eaaSJeff Roberson * bucket and forces every free to synchronize(). 1261d4665eaaSJeff Roberson */ 1262543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0) { 1263376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1264543117beSJeff Roberson b3 = cache_bucket_unload_cross(cache); 1265543117beSJeff Roberson } 1266543117beSJeff Roberson critical_exit(); 1267543117beSJeff Roberson 1268543117beSJeff Roberson if (b1 != NULL) 1269c6fd3e23SJeff Roberson zone_free_bucket(zone, b1, NULL, domain, false); 1270543117beSJeff Roberson if (b2 != NULL) 1271c6fd3e23SJeff Roberson zone_free_bucket(zone, b2, NULL, domain, false); 1272543117beSJeff Roberson if (b3 != NULL) { 1273c6fd3e23SJeff Roberson /* Adjust the domain so it goes to zone_free_cross. */ 1274c6fd3e23SJeff Roberson domain = (domain + 1) % vm_ndomains; 1275c6fd3e23SJeff Roberson zone_free_bucket(zone, b3, NULL, domain, false); 1276c1685086SJeff Roberson } 1277a2de44abSAlexander Motin } 1278a2de44abSAlexander Motin 1279a2de44abSAlexander Motin /* 1280a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1281a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1282a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1283a2de44abSAlexander Motin * to safely access their cache buckets. 1284a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1285a2de44abSAlexander Motin */ 1286a2de44abSAlexander Motin static void 128708cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1288a2de44abSAlexander Motin { 1289a2de44abSAlexander Motin int cpu; 1290a2de44abSAlexander Motin 1291a2de44abSAlexander Motin /* 1292727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1293a2de44abSAlexander Motin */ 1294a2de44abSAlexander Motin if (zone) 129520a4e154SJeff Roberson cache_shrink(zone, NULL); 1296a2de44abSAlexander Motin else 129720a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1298a2de44abSAlexander Motin 1299a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1300a2de44abSAlexander Motin thread_lock(curthread); 1301a2de44abSAlexander Motin sched_bind(curthread, cpu); 1302a2de44abSAlexander Motin thread_unlock(curthread); 1303a2de44abSAlexander Motin 1304a2de44abSAlexander Motin if (zone) 130520a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1306a2de44abSAlexander Motin else 130720a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1308a2de44abSAlexander Motin } 1309a2de44abSAlexander Motin thread_lock(curthread); 1310a2de44abSAlexander Motin sched_unbind(curthread); 1311a2de44abSAlexander Motin thread_unlock(curthread); 1312a2de44abSAlexander Motin } 1313a2de44abSAlexander Motin 1314aaa8bb16SJeff Roberson /* 131508cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 131608cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 131708cfa56eSMark Johnston * estimated working set size. 1318aaa8bb16SJeff Roberson */ 1319aaa8bb16SJeff Roberson static void 132008cfa56eSMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain) 1321aaa8bb16SJeff Roberson { 1322ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1323aaa8bb16SJeff Roberson uma_bucket_t bucket; 1324c6fd3e23SJeff Roberson long target; 1325ab3185d1SJeff Roberson int i; 13268355f576SJeff Roberson 1327c6fd3e23SJeff Roberson /* 1328c6fd3e23SJeff Roberson * Shrink the zone bucket size to ensure that the per-CPU caches 1329c6fd3e23SJeff Roberson * don't grow too large. 1330c6fd3e23SJeff Roberson */ 1331c6fd3e23SJeff Roberson if (zone->uz_bucket_size > zone->uz_bucket_size_min) 1332c6fd3e23SJeff Roberson zone->uz_bucket_size--; 1333c6fd3e23SJeff Roberson 1334ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 133591d947bfSJeff Roberson /* 133691d947bfSJeff Roberson * The cross bucket is partially filled and not part of 133791d947bfSJeff Roberson * the item count. Reclaim it individually here. 133891d947bfSJeff Roberson */ 1339c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 1340226dd6dbSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 || drain) { 134191d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 134291d947bfSJeff Roberson bucket = zdom->uzd_cross; 134391d947bfSJeff Roberson zdom->uzd_cross = NULL; 134491d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 1345c6fd3e23SJeff Roberson if (bucket != NULL) 134691d947bfSJeff Roberson bucket_free(zone, bucket, NULL); 134791d947bfSJeff Roberson } 134891d947bfSJeff Roberson 134991d947bfSJeff Roberson /* 135008cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 135108cfa56eSMark Johnston * this bucket cache is empty. Otherwise, we reclaim items in 135208cfa56eSMark Johnston * excess of the zone's estimated working set size. If the 135308cfa56eSMark Johnston * difference nitems - imin is larger than the WSS estimate, 135408cfa56eSMark Johnston * then the estimate will grow at the end of this interval and 135508cfa56eSMark Johnston * we ignore the historical average. 135608cfa56eSMark Johnston */ 1357c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 135808cfa56eSMark Johnston target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems - 135908cfa56eSMark Johnston zdom->uzd_imin); 136008cfa56eSMark Johnston while (zdom->uzd_nitems > target) { 1361c6fd3e23SJeff Roberson bucket = zone_fetch_bucket(zone, zdom, true); 136208cfa56eSMark Johnston if (bucket == NULL) 136308cfa56eSMark Johnston break; 13646fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 1365c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 13668355f576SJeff Roberson } 1367c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 1368ab3185d1SJeff Roberson } 13698355f576SJeff Roberson } 1370fc03d22bSJeff Roberson 1371fc03d22bSJeff Roberson static void 1372fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1373fc03d22bSJeff Roberson { 1374fc03d22bSJeff Roberson uint8_t *mem; 1375fc03d22bSJeff Roberson int i; 1376fc03d22bSJeff Roberson uint8_t flags; 1377fc03d22bSJeff Roberson 13781431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 13791431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 13801431a748SGleb Smirnoff 13811e0701e1SJeff Roberson mem = slab_data(slab, keg); 1382fc03d22bSJeff Roberson flags = slab->us_flags; 1383fc03d22bSJeff Roberson i = start; 1384fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 1385fc03d22bSJeff Roberson for (i--; i > -1; i--) 1386c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1387c5deaf04SGleb Smirnoff /* 1388c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1389c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1390c5deaf04SGleb Smirnoff * which executed trash_dtor. 1391c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1392c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1393c5deaf04SGleb Smirnoff * invocations. 1394c5deaf04SGleb Smirnoff */ 13951e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1396c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1397c5deaf04SGleb Smirnoff #endif 13981e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1399fc03d22bSJeff Roberson } 140054c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 14019b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab), 14029b8db4d0SRyan Libby NULL, SKIP_NONE); 1403fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 14042e47807cSJeff Roberson uma_total_dec(PAGE_SIZE * keg->uk_ppera); 14058355f576SJeff Roberson } 14068355f576SJeff Roberson 14078355f576SJeff Roberson /* 1408e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 14098355f576SJeff Roberson * the pageout daemon. 14108355f576SJeff Roberson * 1411e20a199fSJeff Roberson * Returns nothing. 14128355f576SJeff Roberson */ 1413e20a199fSJeff Roberson static void 1414e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 14158355f576SJeff Roberson { 14164ab3aee8SMark Johnston struct slabhead freeslabs; 1417ab3185d1SJeff Roberson uma_domain_t dom; 1418829be516SMark Johnston uma_slab_t slab, tmp; 14198b987a77SJeff Roberson int i, n; 14208355f576SJeff Roberson 1421099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 14228355f576SJeff Roberson return; 14238355f576SJeff Roberson 1424ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 14258b987a77SJeff Roberson CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u", 14264ab3aee8SMark Johnston keg->uk_name, keg, i, dom->ud_free_items); 1427ab3185d1SJeff Roberson dom = &keg->uk_domain[i]; 14284ab3aee8SMark Johnston LIST_INIT(&freeslabs); 1429ab3185d1SJeff Roberson 14304ab3aee8SMark Johnston KEG_LOCK(keg, i); 14314ab3aee8SMark Johnston if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) { 14324ab3aee8SMark Johnston LIST_FOREACH(slab, &dom->ud_free_slab, us_link) 14334ab3aee8SMark Johnston UMA_HASH_REMOVE(&keg->uk_hash, slab); 14344ab3aee8SMark Johnston } 14354ab3aee8SMark Johnston n = dom->ud_free_slabs; 14364ab3aee8SMark Johnston LIST_SWAP(&freeslabs, &dom->ud_free_slab, uma_slab, us_link); 14374ab3aee8SMark Johnston dom->ud_free_slabs = 0; 14384ab3aee8SMark Johnston dom->ud_free_items -= n * keg->uk_ipers; 14394ab3aee8SMark Johnston dom->ud_pages -= n * keg->uk_ppera; 14404ab3aee8SMark Johnston KEG_UNLOCK(keg, i); 14414ab3aee8SMark Johnston 14424ab3aee8SMark Johnston LIST_FOREACH_SAFE(slab, &freeslabs, us_link, tmp) 14431645995bSKirk McKusick keg_free_slab(keg, slab, keg->uk_ipers); 14448355f576SJeff Roberson } 14458355f576SJeff Roberson } 14468355f576SJeff Roberson 1447e20a199fSJeff Roberson static void 144808cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain) 1449e20a199fSJeff Roberson { 1450e20a199fSJeff Roberson 14518355f576SJeff Roberson /* 1452e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 1453e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 1454e20a199fSJeff Roberson * is the only call that knows the structure will still be available 1455e20a199fSJeff Roberson * when it wakes up. 1456e20a199fSJeff Roberson */ 1457e20a199fSJeff Roberson ZONE_LOCK(zone); 145808cfa56eSMark Johnston while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) { 1459e20a199fSJeff Roberson if (waitok == M_NOWAIT) 1460e20a199fSJeff Roberson goto out; 1461c6fd3e23SJeff Roberson msleep(zone, &ZDOM_GET(zone, 0)->uzd_lock, PVM, "zonedrain", 1462c6fd3e23SJeff Roberson 1); 1463e20a199fSJeff Roberson } 146408cfa56eSMark Johnston zone->uz_flags |= UMA_ZFLAG_RECLAIMING; 1465e20a199fSJeff Roberson ZONE_UNLOCK(zone); 146691d947bfSJeff Roberson bucket_cache_reclaim(zone, drain); 146708cfa56eSMark Johnston 1468e20a199fSJeff Roberson /* 1469e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 1470111fbcd5SBryan Venteicher * we're running. Normally the uma_rwlock would protect us but we 1471e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 1472e20a199fSJeff Roberson */ 147308034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1474bb15d1c7SGleb Smirnoff keg_drain(zone->uz_keg); 1475e20a199fSJeff Roberson ZONE_LOCK(zone); 147608cfa56eSMark Johnston zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING; 1477e20a199fSJeff Roberson wakeup(zone); 1478e20a199fSJeff Roberson out: 1479e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1480e20a199fSJeff Roberson } 1481e20a199fSJeff Roberson 148208cfa56eSMark Johnston static void 148320a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused) 1484e20a199fSJeff Roberson { 1485e20a199fSJeff Roberson 148608cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, true); 148708cfa56eSMark Johnston } 148808cfa56eSMark Johnston 148908cfa56eSMark Johnston static void 149020a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused) 149108cfa56eSMark Johnston { 149208cfa56eSMark Johnston 149308cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, false); 1494e20a199fSJeff Roberson } 1495e20a199fSJeff Roberson 1496e20a199fSJeff Roberson /* 14978b987a77SJeff Roberson * Allocate a new slab for a keg and inserts it into the partial slab list. 14988b987a77SJeff Roberson * The keg should be unlocked on entry. If the allocation succeeds it will 14998b987a77SJeff Roberson * be locked on return. 15008355f576SJeff Roberson * 15018355f576SJeff Roberson * Arguments: 150286220393SMark Johnston * flags Wait flags for the item initialization routine 150386220393SMark Johnston * aflags Wait flags for the slab allocation 15048355f576SJeff Roberson * 15058355f576SJeff Roberson * Returns: 15068355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 15078355f576SJeff Roberson * caller specified M_NOWAIT. 15088355f576SJeff Roberson */ 15098355f576SJeff Roberson static uma_slab_t 151086220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 151186220393SMark Johnston int aflags) 15128355f576SJeff Roberson { 15138b987a77SJeff Roberson uma_domain_t dom; 1514e20a199fSJeff Roberson uma_alloc allocf; 1515099a0e58SBosko Milekic uma_slab_t slab; 15162e47807cSJeff Roberson unsigned long size; 151785dcf349SGleb Smirnoff uint8_t *mem; 151886220393SMark Johnston uint8_t sflags; 15198355f576SJeff Roberson int i; 15208355f576SJeff Roberson 1521ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1522ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1523a553d4b8SJeff Roberson 15248b987a77SJeff Roberson allocf = keg->uk_allocf; 1525194a979eSMark Johnston slab = NULL; 1526194a979eSMark Johnston mem = NULL; 152754c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 15289b8db4d0SRyan Libby uma_hash_slab_t hslab; 15299b8db4d0SRyan Libby hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL, 15309b8db4d0SRyan Libby domain, aflags); 15319b8db4d0SRyan Libby if (hslab == NULL) 1532727c6918SJeff Roberson goto fail; 15339b8db4d0SRyan Libby slab = &hslab->uhs_slab; 1534a553d4b8SJeff Roberson } 1535a553d4b8SJeff Roberson 15363370c5bfSJeff Roberson /* 15373370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 15383370c5bfSJeff Roberson * first time they are added to a zone. 15393370c5bfSJeff Roberson * 15403370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 15413370c5bfSJeff Roberson */ 15423370c5bfSJeff Roberson 1543099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 154486220393SMark Johnston aflags |= M_ZERO; 15453370c5bfSJeff Roberson else 154686220393SMark Johnston aflags &= ~M_ZERO; 15473370c5bfSJeff Roberson 1548263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 154986220393SMark Johnston aflags |= M_NODUMP; 1550263811f7SKip Macy 1551e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1552194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 155386220393SMark Johnston mem = allocf(zone, size, domain, &sflags, aflags); 1554a553d4b8SJeff Roberson if (mem == NULL) { 155554c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 15569b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), 15579b8db4d0SRyan Libby slab_tohashslab(slab), NULL, SKIP_NONE); 1558727c6918SJeff Roberson goto fail; 1559a553d4b8SJeff Roberson } 15602e47807cSJeff Roberson uma_total_inc(size); 15618355f576SJeff Roberson 15628b987a77SJeff Roberson /* For HASH zones all pages go to the same uma_domain. */ 156354c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 15648b987a77SJeff Roberson domain = 0; 15658b987a77SJeff Roberson 15665c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 156754c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) 1568099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 15691e0701e1SJeff Roberson else 15709b8db4d0SRyan Libby slab_tohashslab(slab)->uhs_data = mem; 15715c0e403bSJeff Roberson 157254c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_VTOSLAB) 1573099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1574584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1575584061b4SJeff Roberson zone, slab); 15768355f576SJeff Roberson 1577099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 157886220393SMark Johnston slab->us_flags = sflags; 1579ab3185d1SJeff Roberson slab->us_domain = domain; 15808b987a77SJeff Roberson 15819b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1582ef72505eSJeff Roberson #ifdef INVARIANTS 1583815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1584ef72505eSJeff Roberson #endif 1585099a0e58SBosko Milekic 1586b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1587099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 15881e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 158986220393SMark Johnston keg->uk_size, flags) != 0) 1590b23f72e9SBrian Feldman break; 1591b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1592fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1593727c6918SJeff Roberson goto fail; 1594b23f72e9SBrian Feldman } 1595b23f72e9SBrian Feldman } 15968b987a77SJeff Roberson KEG_LOCK(keg, domain); 15975c0e403bSJeff Roberson 15981431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 15991431a748SGleb Smirnoff slab, keg->uk_name, keg); 16001431a748SGleb Smirnoff 160154c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 1602099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 16038355f576SJeff Roberson 16048b987a77SJeff Roberson /* 16058b987a77SJeff Roberson * If we got a slab here it's safe to mark it partially used 16068b987a77SJeff Roberson * and return. We assume that the caller is going to remove 16078b987a77SJeff Roberson * at least one item. 16088b987a77SJeff Roberson */ 16098b987a77SJeff Roberson dom = &keg->uk_domain[domain]; 16108b987a77SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 16118b987a77SJeff Roberson dom->ud_pages += keg->uk_ppera; 16124ab3aee8SMark Johnston dom->ud_free_items += keg->uk_ipers; 16138355f576SJeff Roberson 16148355f576SJeff Roberson return (slab); 1615727c6918SJeff Roberson 1616727c6918SJeff Roberson fail: 1617727c6918SJeff Roberson return (NULL); 16188355f576SJeff Roberson } 16198355f576SJeff Roberson 16208355f576SJeff Roberson /* 1621009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1622009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1623009b6fcbSJeff Roberson * the VM is ready. 1624009b6fcbSJeff Roberson */ 1625009b6fcbSJeff Roberson static void * 1626ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1627ab3185d1SJeff Roberson int wait) 1628009b6fcbSJeff Roberson { 1629a81c400eSJeff Roberson vm_paddr_t pa; 1630a81c400eSJeff Roberson vm_page_t m; 1631ac0a6fd0SGleb Smirnoff void *mem; 1632ac0a6fd0SGleb Smirnoff int pages; 1633a81c400eSJeff Roberson int i; 1634099a0e58SBosko Milekic 1635f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1636f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1637a81c400eSJeff Roberson 1638f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1639a81c400eSJeff Roberson m = vm_page_alloc_contig_domain(NULL, 0, domain, 1640a81c400eSJeff Roberson malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages, 1641a81c400eSJeff Roberson (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT); 1642a81c400eSJeff Roberson if (m == NULL) 1643a81c400eSJeff Roberson return (NULL); 1644a81c400eSJeff Roberson 1645a81c400eSJeff Roberson pa = VM_PAGE_TO_PHYS(m); 1646a81c400eSJeff Roberson for (i = 0; i < pages; i++, pa += PAGE_SIZE) { 1647a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1648a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1649a81c400eSJeff Roberson if ((wait & M_NODUMP) == 0) 1650a81c400eSJeff Roberson dump_add_page(pa); 1651a81c400eSJeff Roberson #endif 1652a81c400eSJeff Roberson } 1653a81c400eSJeff Roberson /* Allocate KVA and indirectly advance bootmem. */ 1654a81c400eSJeff Roberson mem = (void *)pmap_map(&bootmem, m->phys_addr, 1655a81c400eSJeff Roberson m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE); 1656a81c400eSJeff Roberson if ((wait & M_ZERO) != 0) 1657a81c400eSJeff Roberson bzero(mem, pages * PAGE_SIZE); 1658f7d35785SGleb Smirnoff 1659f7d35785SGleb Smirnoff return (mem); 1660f7d35785SGleb Smirnoff } 1661f7d35785SGleb Smirnoff 1662a81c400eSJeff Roberson static void 1663a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes) 1664a81c400eSJeff Roberson { 1665a81c400eSJeff Roberson vm_offset_t va; 1666a81c400eSJeff Roberson vm_page_t m; 1667a81c400eSJeff Roberson 1668a81c400eSJeff Roberson va = (vm_offset_t)mem; 1669a81c400eSJeff Roberson m = PHYS_TO_VM_PAGE(pmap_kextract(va)); 1670a81c400eSJeff Roberson pmap_remove(kernel_pmap, va, va + bytes); 1671a81c400eSJeff Roberson for (; bytes != 0; bytes -= PAGE_SIZE, m++) { 1672a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1673a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1674a81c400eSJeff Roberson dump_drop_page(VM_PAGE_TO_PHYS(m)); 1675a81c400eSJeff Roberson #endif 1676a81c400eSJeff Roberson vm_page_unwire_noq(m); 1677a81c400eSJeff Roberson vm_page_free(m); 1678a81c400eSJeff Roberson } 1679a81c400eSJeff Roberson } 1680a81c400eSJeff Roberson 1681f7d35785SGleb Smirnoff /* 16828355f576SJeff Roberson * Allocates a number of pages from the system 16838355f576SJeff Roberson * 16848355f576SJeff Roberson * Arguments: 16858355f576SJeff Roberson * bytes The number of bytes requested 16868355f576SJeff Roberson * wait Shall we wait? 16878355f576SJeff Roberson * 16888355f576SJeff Roberson * Returns: 16898355f576SJeff Roberson * A pointer to the alloced memory or possibly 16908355f576SJeff Roberson * NULL if M_NOWAIT is set. 16918355f576SJeff Roberson */ 16928355f576SJeff Roberson static void * 1693ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1694ab3185d1SJeff Roberson int wait) 16958355f576SJeff Roberson { 16968355f576SJeff Roberson void *p; /* Returned page */ 16978355f576SJeff Roberson 16982e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 16999978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 17008355f576SJeff Roberson 17018355f576SJeff Roberson return (p); 17028355f576SJeff Roberson } 17038355f576SJeff Roberson 1704ab3059a8SMatt Macy static void * 1705ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1706ab3059a8SMatt Macy int wait) 1707ab3059a8SMatt Macy { 1708ab3059a8SMatt Macy struct pglist alloctail; 1709ab3059a8SMatt Macy vm_offset_t addr, zkva; 1710ab3059a8SMatt Macy int cpu, flags; 1711ab3059a8SMatt Macy vm_page_t p, p_next; 1712ab3059a8SMatt Macy #ifdef NUMA 1713ab3059a8SMatt Macy struct pcpu *pc; 1714ab3059a8SMatt Macy #endif 1715ab3059a8SMatt Macy 1716ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1717ab3059a8SMatt Macy 1718013072f0SMark Johnston TAILQ_INIT(&alloctail); 1719ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1720013072f0SMark Johnston malloc2vm_flags(wait); 1721013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1722ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1723ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1724ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1725ab3059a8SMatt Macy } else { 1726ab3059a8SMatt Macy #ifndef NUMA 1727ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1728ab3059a8SMatt Macy #else 1729ab3059a8SMatt Macy pc = pcpu_find(cpu); 173020526802SAndrew Gallatin if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain))) 173120526802SAndrew Gallatin p = NULL; 173220526802SAndrew Gallatin else 173320526802SAndrew Gallatin p = vm_page_alloc_domain(NULL, 0, 173420526802SAndrew Gallatin pc->pc_domain, flags); 1735ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1736ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1737ab3059a8SMatt Macy #endif 1738ab3059a8SMatt Macy } 1739ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1740ab3059a8SMatt Macy goto fail; 1741ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1742ab3059a8SMatt Macy } 1743ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1744ab3059a8SMatt Macy goto fail; 1745ab3059a8SMatt Macy zkva = addr; 1746ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1747ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1748ab3059a8SMatt Macy zkva += PAGE_SIZE; 1749ab3059a8SMatt Macy } 1750ab3059a8SMatt Macy return ((void*)addr); 1751ab3059a8SMatt Macy fail: 1752ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 175388ea538aSMark Johnston vm_page_unwire_noq(p); 1754ab3059a8SMatt Macy vm_page_free(p); 1755ab3059a8SMatt Macy } 1756ab3059a8SMatt Macy return (NULL); 1757ab3059a8SMatt Macy } 1758ab3059a8SMatt Macy 17598355f576SJeff Roberson /* 17608355f576SJeff Roberson * Allocates a number of pages from within an object 17618355f576SJeff Roberson * 17628355f576SJeff Roberson * Arguments: 17638355f576SJeff Roberson * bytes The number of bytes requested 17648355f576SJeff Roberson * wait Shall we wait? 17658355f576SJeff Roberson * 17668355f576SJeff Roberson * Returns: 17678355f576SJeff Roberson * A pointer to the alloced memory or possibly 17688355f576SJeff Roberson * NULL if M_NOWAIT is set. 17698355f576SJeff Roberson */ 17708355f576SJeff Roberson static void * 1771ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1772ab3185d1SJeff Roberson int wait) 17738355f576SJeff Roberson { 1774a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1775a4915c21SAttilio Rao u_long npages; 1776b245ac95SAlan Cox vm_offset_t retkva, zkva; 1777a4915c21SAttilio Rao vm_page_t p, p_next; 1778e20a199fSJeff Roberson uma_keg_t keg; 17798355f576SJeff Roberson 1780a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1781bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1782a4915c21SAttilio Rao 1783a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1784a4915c21SAttilio Rao while (npages > 0) { 1785ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 17868d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1787772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1788772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1789a4915c21SAttilio Rao if (p != NULL) { 1790a4915c21SAttilio Rao /* 1791a4915c21SAttilio Rao * Since the page does not belong to an object, its 1792a4915c21SAttilio Rao * listq is unused. 1793a4915c21SAttilio Rao */ 1794a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1795a4915c21SAttilio Rao npages--; 1796a4915c21SAttilio Rao continue; 1797a4915c21SAttilio Rao } 17988355f576SJeff Roberson /* 1799a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1800a4915c21SAttilio Rao * exit. 18018355f576SJeff Roberson */ 1802a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 180388ea538aSMark Johnston vm_page_unwire_noq(p); 1804b245ac95SAlan Cox vm_page_free(p); 1805b245ac95SAlan Cox } 1806a4915c21SAttilio Rao return (NULL); 1807b245ac95SAlan Cox } 18088355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1809a4915c21SAttilio Rao zkva = keg->uk_kva + 1810a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1811a4915c21SAttilio Rao retkva = zkva; 1812a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1813a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1814a4915c21SAttilio Rao zkva += PAGE_SIZE; 1815a4915c21SAttilio Rao } 18168355f576SJeff Roberson 18178355f576SJeff Roberson return ((void *)retkva); 18188355f576SJeff Roberson } 18198355f576SJeff Roberson 18208355f576SJeff Roberson /* 1821ec0d8280SRyan Libby * Allocate physically contiguous pages. 1822ec0d8280SRyan Libby */ 1823ec0d8280SRyan Libby static void * 1824ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1825ec0d8280SRyan Libby int wait) 1826ec0d8280SRyan Libby { 1827ec0d8280SRyan Libby 1828ec0d8280SRyan Libby *pflag = UMA_SLAB_KERNEL; 1829ec0d8280SRyan Libby return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain), 1830ec0d8280SRyan Libby bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT)); 1831ec0d8280SRyan Libby } 1832ec0d8280SRyan Libby 1833ec0d8280SRyan Libby /* 18348355f576SJeff Roberson * Frees a number of pages to the system 18358355f576SJeff Roberson * 18368355f576SJeff Roberson * Arguments: 18378355f576SJeff Roberson * mem A pointer to the memory to be freed 18388355f576SJeff Roberson * size The size of the memory being freed 18398355f576SJeff Roberson * flags The original p->us_flags field 18408355f576SJeff Roberson * 18418355f576SJeff Roberson * Returns: 18428355f576SJeff Roberson * Nothing 18438355f576SJeff Roberson */ 18448355f576SJeff Roberson static void 1845f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 18468355f576SJeff Roberson { 18473370c5bfSJeff Roberson 1848a81c400eSJeff Roberson if ((flags & UMA_SLAB_BOOT) != 0) { 1849a81c400eSJeff Roberson startup_free(mem, size); 1850a81c400eSJeff Roberson return; 1851a81c400eSJeff Roberson } 1852a81c400eSJeff Roberson 1853ec0d8280SRyan Libby KASSERT((flags & UMA_SLAB_KERNEL) != 0, 1854ec0d8280SRyan Libby ("UMA: page_free used with invalid flags %x", flags)); 18558355f576SJeff Roberson 185649bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 18578355f576SJeff Roberson } 18588355f576SJeff Roberson 18598355f576SJeff Roberson /* 1860ab3059a8SMatt Macy * Frees pcpu zone allocations 1861ab3059a8SMatt Macy * 1862ab3059a8SMatt Macy * Arguments: 1863ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1864ab3059a8SMatt Macy * size The size of the memory being freed 1865ab3059a8SMatt Macy * flags The original p->us_flags field 1866ab3059a8SMatt Macy * 1867ab3059a8SMatt Macy * Returns: 1868ab3059a8SMatt Macy * Nothing 1869ab3059a8SMatt Macy */ 1870ab3059a8SMatt Macy static void 1871ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1872ab3059a8SMatt Macy { 1873ab3059a8SMatt Macy vm_offset_t sva, curva; 1874ab3059a8SMatt Macy vm_paddr_t paddr; 1875ab3059a8SMatt Macy vm_page_t m; 1876ab3059a8SMatt Macy 1877ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 18785ba16cf3SRyan Libby 18795ba16cf3SRyan Libby if ((flags & UMA_SLAB_BOOT) != 0) { 18805ba16cf3SRyan Libby startup_free(mem, size); 18815ba16cf3SRyan Libby return; 18825ba16cf3SRyan Libby } 18835ba16cf3SRyan Libby 1884ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1885ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1886ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1887ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 188888ea538aSMark Johnston vm_page_unwire_noq(m); 1889ab3059a8SMatt Macy vm_page_free(m); 1890ab3059a8SMatt Macy } 1891ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1892ab3059a8SMatt Macy kva_free(sva, size); 1893ab3059a8SMatt Macy } 1894ab3059a8SMatt Macy 1895ab3059a8SMatt Macy 1896ab3059a8SMatt Macy /* 18978355f576SJeff Roberson * Zero fill initializer 18988355f576SJeff Roberson * 18998355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 19008355f576SJeff Roberson */ 1901b23f72e9SBrian Feldman static int 1902b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 19038355f576SJeff Roberson { 19048355f576SJeff Roberson bzero(mem, size); 1905b23f72e9SBrian Feldman return (0); 19068355f576SJeff Roberson } 19078355f576SJeff Roberson 1908815db204SRyan Libby #ifdef INVARIANTS 190954007ce8SMark Johnston static struct noslabbits * 1910815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1911815db204SRyan Libby { 1912815db204SRyan Libby 1913815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1914815db204SRyan Libby } 1915815db204SRyan Libby #endif 1916815db204SRyan Libby 19178355f576SJeff Roberson /* 19189b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 19199b78b1f4SJeff Roberson */ 192054007ce8SMark Johnston static size_t 19219b78b1f4SJeff Roberson slab_sizeof(int nitems) 19229b78b1f4SJeff Roberson { 19239b78b1f4SJeff Roberson size_t s; 19249b78b1f4SJeff Roberson 1925815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 19269b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 19279b78b1f4SJeff Roberson } 19289b78b1f4SJeff Roberson 19294a8b575cSRyan Libby #define UMA_FIXPT_SHIFT 31 19304a8b575cSRyan Libby #define UMA_FRAC_FIXPT(n, d) \ 19314a8b575cSRyan Libby ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d))) 19324a8b575cSRyan Libby #define UMA_FIXPT_PCT(f) \ 19334a8b575cSRyan Libby ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT)) 19344a8b575cSRyan Libby #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100) 19354a8b575cSRyan Libby #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE) 19364a8b575cSRyan Libby 19379b78b1f4SJeff Roberson /* 19384a8b575cSRyan Libby * Compute the number of items that will fit in a slab. If hdr is true, the 19394a8b575cSRyan Libby * item count may be limited to provide space in the slab for an inline slab 19404a8b575cSRyan Libby * header. Otherwise, all slab space will be provided for item storage. 19414a8b575cSRyan Libby */ 19424a8b575cSRyan Libby static u_int 19434a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr) 19444a8b575cSRyan Libby { 19454a8b575cSRyan Libby u_int ipers; 19464a8b575cSRyan Libby u_int padpi; 19474a8b575cSRyan Libby 19484a8b575cSRyan Libby /* The padding between items is not needed after the last item. */ 19494a8b575cSRyan Libby padpi = rsize - size; 19504a8b575cSRyan Libby 19514a8b575cSRyan Libby if (hdr) { 19524a8b575cSRyan Libby /* 19534a8b575cSRyan Libby * Start with the maximum item count and remove items until 19544a8b575cSRyan Libby * the slab header first alongside the allocatable memory. 19554a8b575cSRyan Libby */ 19564a8b575cSRyan Libby for (ipers = MIN(SLAB_MAX_SETSIZE, 19574a8b575cSRyan Libby (slabsize + padpi - slab_sizeof(1)) / rsize); 19584a8b575cSRyan Libby ipers > 0 && 19594a8b575cSRyan Libby ipers * rsize - padpi + slab_sizeof(ipers) > slabsize; 19604a8b575cSRyan Libby ipers--) 19614a8b575cSRyan Libby continue; 19624a8b575cSRyan Libby } else { 19634a8b575cSRyan Libby ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE); 19644a8b575cSRyan Libby } 19654a8b575cSRyan Libby 19664a8b575cSRyan Libby return (ipers); 19674a8b575cSRyan Libby } 19684a8b575cSRyan Libby 196927ca37acSRyan Libby struct keg_layout_result { 197027ca37acSRyan Libby u_int format; 197127ca37acSRyan Libby u_int slabsize; 197227ca37acSRyan Libby u_int ipers; 197327ca37acSRyan Libby u_int eff; 197427ca37acSRyan Libby }; 197527ca37acSRyan Libby 197627ca37acSRyan Libby static void 197727ca37acSRyan Libby keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt, 197827ca37acSRyan Libby struct keg_layout_result *kl) 197927ca37acSRyan Libby { 198027ca37acSRyan Libby u_int total; 198127ca37acSRyan Libby 198227ca37acSRyan Libby kl->format = fmt; 198327ca37acSRyan Libby kl->slabsize = slabsize; 198427ca37acSRyan Libby 198527ca37acSRyan Libby /* Handle INTERNAL as inline with an extra page. */ 198627ca37acSRyan Libby if ((fmt & UMA_ZFLAG_INTERNAL) != 0) { 198727ca37acSRyan Libby kl->format &= ~UMA_ZFLAG_INTERNAL; 198827ca37acSRyan Libby kl->slabsize += PAGE_SIZE; 198927ca37acSRyan Libby } 199027ca37acSRyan Libby 199127ca37acSRyan Libby kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize, 199227ca37acSRyan Libby (fmt & UMA_ZFLAG_OFFPAGE) == 0); 199327ca37acSRyan Libby 199427ca37acSRyan Libby /* Account for memory used by an offpage slab header. */ 199527ca37acSRyan Libby total = kl->slabsize; 199627ca37acSRyan Libby if ((fmt & UMA_ZFLAG_OFFPAGE) != 0) 199727ca37acSRyan Libby total += slabzone(kl->ipers)->uz_keg->uk_rsize; 199827ca37acSRyan Libby 199927ca37acSRyan Libby kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total); 200027ca37acSRyan Libby } 200127ca37acSRyan Libby 20029b78b1f4SJeff Roberson /* 20034a8b575cSRyan Libby * Determine the format of a uma keg. This determines where the slab header 20044a8b575cSRyan Libby * will be placed (inline or offpage) and calculates ipers, rsize, and ppera. 20058355f576SJeff Roberson * 20068355f576SJeff Roberson * Arguments 2007e20a199fSJeff Roberson * keg The zone we should initialize 20088355f576SJeff Roberson * 20098355f576SJeff Roberson * Returns 20108355f576SJeff Roberson * Nothing 20118355f576SJeff Roberson */ 20128355f576SJeff Roberson static void 20134a8b575cSRyan Libby keg_layout(uma_keg_t keg) 20148355f576SJeff Roberson { 201527ca37acSRyan Libby struct keg_layout_result kl = {}, kl_tmp; 201627ca37acSRyan Libby u_int fmts[2]; 20174a8b575cSRyan Libby u_int alignsize; 201827ca37acSRyan Libby u_int nfmt; 20194a8b575cSRyan Libby u_int pages; 2020244f4554SBosko Milekic u_int rsize; 2021a55ebb7cSAndriy Gapon u_int slabsize; 202227ca37acSRyan Libby u_int i, j; 20238355f576SJeff Roberson 20244a8b575cSRyan Libby KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 20254a8b575cSRyan Libby (keg->uk_size <= UMA_PCPU_ALLOC_SIZE && 20264a8b575cSRyan Libby (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0), 20274a8b575cSRyan Libby ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b", 20284a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_size, keg->uk_flags, 20294a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 2030bae55c4aSRyan Libby KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 || 20314a8b575cSRyan Libby (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0, 20324a8b575cSRyan Libby ("%s: incompatible flags 0x%b", __func__, keg->uk_flags, 20334a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 2034e28a647dSGleb Smirnoff 20354a8b575cSRyan Libby alignsize = keg->uk_align + 1; 2036ad97af7eSGleb Smirnoff 2037ef72505eSJeff Roberson /* 2038ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 2039ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 2040ef72505eSJeff Roberson * allocation bits for we round it up. 2041ef72505eSJeff Roberson */ 20429b8db4d0SRyan Libby rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT); 20434a8b575cSRyan Libby rsize = roundup2(rsize, alignsize); 2044ad97af7eSGleb Smirnoff 204527ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) { 20469b78b1f4SJeff Roberson /* 20474a8b575cSRyan Libby * We want one item to start on every align boundary in a page. 20484a8b575cSRyan Libby * To do this we will span pages. We will also extend the item 20494a8b575cSRyan Libby * by the size of align if it is an even multiple of align. 20504a8b575cSRyan Libby * Otherwise, it would fall on the same boundary every time. 20519b78b1f4SJeff Roberson */ 20524a8b575cSRyan Libby if ((rsize & alignsize) == 0) 20534a8b575cSRyan Libby rsize += alignsize; 20544a8b575cSRyan Libby slabsize = rsize * (PAGE_SIZE / alignsize); 20554a8b575cSRyan Libby slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE); 20564a8b575cSRyan Libby slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE); 205727ca37acSRyan Libby slabsize = round_page(slabsize); 20584a8b575cSRyan Libby } else { 20594a8b575cSRyan Libby /* 206027ca37acSRyan Libby * Start with a slab size of as many pages as it takes to 206127ca37acSRyan Libby * represent a single item. We will try to fit as many 206227ca37acSRyan Libby * additional items into the slab as possible. 20634a8b575cSRyan Libby */ 206427ca37acSRyan Libby slabsize = round_page(keg->uk_size); 20651ca6ed45SGleb Smirnoff } 2066ad97af7eSGleb Smirnoff 206727ca37acSRyan Libby /* Build a list of all of the available formats for this keg. */ 206827ca37acSRyan Libby nfmt = 0; 206927ca37acSRyan Libby 20704a8b575cSRyan Libby /* Evaluate an inline slab layout. */ 20714a8b575cSRyan Libby if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0) 207227ca37acSRyan Libby fmts[nfmt++] = 0; 20734a8b575cSRyan Libby 20744a8b575cSRyan Libby /* TODO: vm_page-embedded slab. */ 2075244f4554SBosko Milekic 207620e8e865SBosko Milekic /* 2077244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 207820e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 2079bae55c4aSRyan Libby * may end up going to the VM for slabs which we do not want 2080bae55c4aSRyan Libby * to do if we're UMA_ZONE_VM, which clearly forbids it. 2081bae55c4aSRyan Libby * In those cases, evaluate a pseudo-format called INTERNAL 2082bae55c4aSRyan Libby * which has an inline slab header and one extra page to 2083bae55c4aSRyan Libby * guarantee that it fits. 208427ca37acSRyan Libby * 208527ca37acSRyan Libby * Otherwise, see if using an OFFPAGE slab will improve our 208627ca37acSRyan Libby * efficiency. 208720e8e865SBosko Milekic */ 2088bae55c4aSRyan Libby if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0) 208927ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_INTERNAL; 209027ca37acSRyan Libby else 209127ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_OFFPAGE; 2092244f4554SBosko Milekic 2093ef72505eSJeff Roberson /* 209427ca37acSRyan Libby * Choose a slab size and format which satisfy the minimum efficiency. 209527ca37acSRyan Libby * Prefer the smallest slab size that meets the constraints. 2096ef72505eSJeff Roberson * 209727ca37acSRyan Libby * Start with a minimum slab size, to accommodate CACHESPREAD. Then, 209827ca37acSRyan Libby * for small items (up to PAGE_SIZE), the iteration increment is one 209927ca37acSRyan Libby * page; and for large items, the increment is one item. 2100ef72505eSJeff Roberson */ 210127ca37acSRyan Libby i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize); 210227ca37acSRyan Libby KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u", 210327ca37acSRyan Libby keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize, 210427ca37acSRyan Libby rsize, i)); 210527ca37acSRyan Libby for ( ; ; i++) { 210627ca37acSRyan Libby slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) : 210727ca37acSRyan Libby round_page(rsize * (i - 1) + keg->uk_size); 210827ca37acSRyan Libby 210927ca37acSRyan Libby for (j = 0; j < nfmt; j++) { 211027ca37acSRyan Libby /* Only if we have no viable format yet. */ 211127ca37acSRyan Libby if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 && 211227ca37acSRyan Libby kl.ipers > 0) 211327ca37acSRyan Libby continue; 211427ca37acSRyan Libby 211527ca37acSRyan Libby keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp); 211627ca37acSRyan Libby if (kl_tmp.eff <= kl.eff) 211727ca37acSRyan Libby continue; 211827ca37acSRyan Libby 211927ca37acSRyan Libby kl = kl_tmp; 212027ca37acSRyan Libby 212127ca37acSRyan Libby CTR6(KTR_UMA, "keg %s layout: format %#x " 212227ca37acSRyan Libby "(ipers %u * rsize %u) / slabsize %#x = %u%% eff", 212327ca37acSRyan Libby keg->uk_name, kl.format, kl.ipers, rsize, 212427ca37acSRyan Libby kl.slabsize, UMA_FIXPT_PCT(kl.eff)); 212527ca37acSRyan Libby 212627ca37acSRyan Libby /* Stop when we reach the minimum efficiency. */ 212727ca37acSRyan Libby if (kl.eff >= UMA_MIN_EFF) 212827ca37acSRyan Libby break; 21298355f576SJeff Roberson } 2130ad97af7eSGleb Smirnoff 213133e5a1eaSRyan Libby if (kl.eff >= UMA_MIN_EFF || !multipage_slabs || 213227ca37acSRyan Libby slabsize >= SLAB_MAX_SETSIZE * rsize || 213327ca37acSRyan Libby (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0) 213427ca37acSRyan Libby break; 213527ca37acSRyan Libby } 213627ca37acSRyan Libby 213727ca37acSRyan Libby pages = atop(kl.slabsize); 213827ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 213927ca37acSRyan Libby pages *= mp_maxid + 1; 214027ca37acSRyan Libby 214127ca37acSRyan Libby keg->uk_rsize = rsize; 214227ca37acSRyan Libby keg->uk_ipers = kl.ipers; 214327ca37acSRyan Libby keg->uk_ppera = pages; 214427ca37acSRyan Libby keg->uk_flags |= kl.format; 214527ca37acSRyan Libby 21464a8b575cSRyan Libby /* 21474a8b575cSRyan Libby * How do we find the slab header if it is offpage or if not all item 21484a8b575cSRyan Libby * start addresses are in the same page? We could solve the latter 21494a8b575cSRyan Libby * case with vaddr alignment, but we don't. 21504a8b575cSRyan Libby */ 215127ca37acSRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 || 215227ca37acSRyan Libby (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) { 215354c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0) 215427ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_HASH; 215554c5ae80SRyan Libby else 215627ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 215754c5ae80SRyan Libby } 215827ca37acSRyan Libby 2159e63a1c2fSRyan Libby CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u", 216027ca37acSRyan Libby __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers, 216127ca37acSRyan Libby pages); 21624a8b575cSRyan Libby KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 21634a8b575cSRyan Libby ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__, 216427ca37acSRyan Libby keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize, 216527ca37acSRyan Libby keg->uk_ipers, pages)); 2166e20a199fSJeff Roberson } 2167e20a199fSJeff Roberson 21688355f576SJeff Roberson /* 2169099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 2170099a0e58SBosko Milekic * the keg onto the global keg list. 21718355f576SJeff Roberson * 21728355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 2173099a0e58SBosko Milekic * udata Actually uma_kctor_args 2174099a0e58SBosko Milekic */ 2175b23f72e9SBrian Feldman static int 2176b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 2177099a0e58SBosko Milekic { 2178099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 2179099a0e58SBosko Milekic uma_keg_t keg = mem; 2180099a0e58SBosko Milekic uma_zone_t zone; 21818b987a77SJeff Roberson int i; 2182099a0e58SBosko Milekic 2183099a0e58SBosko Milekic bzero(keg, size); 2184099a0e58SBosko Milekic keg->uk_size = arg->size; 2185099a0e58SBosko Milekic keg->uk_init = arg->uminit; 2186099a0e58SBosko Milekic keg->uk_fini = arg->fini; 2187099a0e58SBosko Milekic keg->uk_align = arg->align; 21886fd34d6fSJeff Roberson keg->uk_reserve = 0; 2189099a0e58SBosko Milekic keg->uk_flags = arg->flags; 2190099a0e58SBosko Milekic 2191099a0e58SBosko Milekic /* 2192194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 2193dfe13344SJeff Roberson * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which 2194dfe13344SJeff Roberson * case the iterator is never run. 2195194a979eSMark Johnston */ 2196194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 2197194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 2198194a979eSMark Johnston 2199194a979eSMark Johnston /* 2200c8b0a88bSJeff Roberson * The primary zone is passed to us at keg-creation time. 2201099a0e58SBosko Milekic */ 2202099a0e58SBosko Milekic zone = arg->zone; 2203e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 2204099a0e58SBosko Milekic 2205099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 2206099a0e58SBosko Milekic keg->uk_init = zero_init; 2207099a0e58SBosko Milekic 2208cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 220954c5ae80SRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 2210e20a199fSJeff Roberson 221154c5ae80SRyan Libby #ifndef SMP 2212ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 2213ad97af7eSGleb Smirnoff #endif 2214ad97af7eSGleb Smirnoff 22154a8b575cSRyan Libby keg_layout(keg); 2216099a0e58SBosko Milekic 22178b987a77SJeff Roberson /* 2218c6fd3e23SJeff Roberson * Use a first-touch NUMA policy for kegs that pmap_extract() will 2219c6fd3e23SJeff Roberson * work on. Use round-robin for everything else. 2220dfe13344SJeff Roberson * 2221dfe13344SJeff Roberson * Zones may override the default by specifying either. 22228b987a77SJeff Roberson */ 2223dfe13344SJeff Roberson #ifdef NUMA 2224dfe13344SJeff Roberson if ((keg->uk_flags & 2225c6fd3e23SJeff Roberson (UMA_ZONE_ROUNDROBIN | UMA_ZFLAG_CACHE | UMA_ZONE_NOTPAGE)) == 0) 2226dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_FIRSTTOUCH; 2227dfe13344SJeff Roberson else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2228dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_ROUNDROBIN; 22298b987a77SJeff Roberson #endif 22308b987a77SJeff Roberson 2231099a0e58SBosko Milekic /* 2232099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 2233099a0e58SBosko Milekic * startup cache until the vm is ready. 2234099a0e58SBosko Milekic */ 223577e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2236a81c400eSJeff Roberson if (keg->uk_ppera == 1) 223777e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 2238a81c400eSJeff Roberson else 22398cd02d00SAlan Cox #endif 2240a81c400eSJeff Roberson if (booted < BOOT_KVA) 2241a81c400eSJeff Roberson keg->uk_allocf = startup_alloc; 2242ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 2243ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 2244ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1) 2245ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 224677e19437SGleb Smirnoff else 224777e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 224877e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 224977e19437SGleb Smirnoff if (keg->uk_ppera == 1) 225077e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 225177e19437SGleb Smirnoff else 225277e19437SGleb Smirnoff #endif 2253ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 2254ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 2255ab3059a8SMatt Macy else 225677e19437SGleb Smirnoff keg->uk_freef = page_free; 2257099a0e58SBosko Milekic 2258099a0e58SBosko Milekic /* 22598b987a77SJeff Roberson * Initialize keg's locks. 2260099a0e58SBosko Milekic */ 22618b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 22628b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 2263099a0e58SBosko Milekic 2264099a0e58SBosko Milekic /* 2265099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 22669b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 22679b78b1f4SJeff Roberson * definition. 2268099a0e58SBosko Milekic */ 226954c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) { 22709b78b1f4SJeff Roberson size_t shsize; 22719b78b1f4SJeff Roberson 22729b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 22739b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 2274244f4554SBosko Milekic /* 2275244f4554SBosko Milekic * The only way the following is possible is if with our 2276244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 2277244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 2278244f4554SBosko Milekic * mathematically possible for all cases, so we make 2279244f4554SBosko Milekic * sure here anyway. 2280244f4554SBosko Milekic */ 22819b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 22823d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 22833d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 2284099a0e58SBosko Milekic } 2285099a0e58SBosko Milekic 228654c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 22873b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 2288099a0e58SBosko Milekic 2289e63a1c2fSRyan Libby CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone); 2290099a0e58SBosko Milekic 2291099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 2292099a0e58SBosko Milekic 2293111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2294099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2295111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2296b23f72e9SBrian Feldman return (0); 2297099a0e58SBosko Milekic } 2298099a0e58SBosko Milekic 22992efcc8cbSGleb Smirnoff static void 2300a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused) 2301a81c400eSJeff Roberson { 2302a81c400eSJeff Roberson uma_keg_t keg; 2303a81c400eSJeff Roberson 2304a81c400eSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 2305a81c400eSJeff Roberson return; 2306a81c400eSJeff Roberson KEG_GET(zone, keg); 2307ec0d8280SRyan Libby 2308ec0d8280SRyan Libby if (keg->uk_allocf == startup_alloc) { 2309ec0d8280SRyan Libby /* Switch to the real allocator. */ 2310f96d4157SJeff Roberson if (keg->uk_flags & UMA_ZONE_PCPU) 2311f96d4157SJeff Roberson keg->uk_allocf = pcpu_page_alloc; 2312ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && 2313ec0d8280SRyan Libby keg->uk_ppera > 1) 2314ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 2315ec0d8280SRyan Libby else 2316a81c400eSJeff Roberson keg->uk_allocf = page_alloc; 2317a81c400eSJeff Roberson } 2318ec0d8280SRyan Libby } 2319a81c400eSJeff Roberson 2320a81c400eSJeff Roberson static void 232120a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 23222efcc8cbSGleb Smirnoff { 23232efcc8cbSGleb Smirnoff 23242efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 23252efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 23262efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 2327c6fd3e23SJeff Roberson zone->uz_xdomain = counter_u64_alloc(M_WAITOK); 23282efcc8cbSGleb Smirnoff } 23292efcc8cbSGleb Smirnoff 233020a4e154SJeff Roberson static void 233120a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 233220a4e154SJeff Roberson { 233320a4e154SJeff Roberson uma_zone_domain_t zdom; 23348b987a77SJeff Roberson uma_domain_t dom; 233520a4e154SJeff Roberson uma_keg_t keg; 233620a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 23373b490537SJeff Roberson int domains, i, cnt; 233820a4e154SJeff Roberson static const char *nokeg = "cache zone"; 233920a4e154SJeff Roberson char *c; 234020a4e154SJeff Roberson 234120a4e154SJeff Roberson /* 234220a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 234320a4e154SJeff Roberson * any special characters and handling dups by appending 234420a4e154SJeff Roberson * an index. 234520a4e154SJeff Roberson */ 234620a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 23473b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 23483b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 23493b490537SJeff Roberson cnt /= 10; 23503b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 23513b490537SJeff Roberson M_UMA, M_WAITOK); 235220a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 235320a4e154SJeff Roberson zone->uz_namecnt); 235420a4e154SJeff Roberson } else 235520a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 235620a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 235720a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 235820a4e154SJeff Roberson *c = '_'; 235920a4e154SJeff Roberson 236020a4e154SJeff Roberson /* 236120a4e154SJeff Roberson * Basic parameters at the root. 236220a4e154SJeff Roberson */ 236320a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 23647029da5cSPawel Biernacki OID_AUTO, zone->uz_ctlname, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 236520a4e154SJeff Roberson oid = zone->uz_oid; 236620a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 236720a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 23686d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 23696d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 23706d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 237120a4e154SJeff Roberson "Allocator configuration flags"); 237220a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 237320a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 237420a4e154SJeff Roberson "Desired per-cpu cache size"); 237520a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 237620a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 237720a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 237820a4e154SJeff Roberson 237920a4e154SJeff Roberson /* 238020a4e154SJeff Roberson * keg if present. 238120a4e154SJeff Roberson */ 238254c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 23838b987a77SJeff Roberson domains = vm_ndomains; 23848b987a77SJeff Roberson else 23858b987a77SJeff Roberson domains = 1; 238620a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 23877029da5cSPawel Biernacki "keg", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 238820a4e154SJeff Roberson keg = zone->uz_keg; 23893b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 239020a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 239120a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 239220a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 239320a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 239420a4e154SJeff Roberson "Real object size with alignment"); 239520a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 239620a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 239720a4e154SJeff Roberson "pages per-slab allocation"); 239820a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 239920a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 240020a4e154SJeff Roberson "items available per-slab"); 240120a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 240220a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 240320a4e154SJeff Roberson "item alignment mask"); 2404f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2405f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2406f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2407f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 24088b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 24097029da5cSPawel Biernacki OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 24108b987a77SJeff Roberson for (i = 0; i < domains; i++) { 24118b987a77SJeff Roberson dom = &keg->uk_domain[i]; 24128b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 24137029da5cSPawel Biernacki OID_AUTO, VM_DOMAIN(i)->vmd_name, 24147029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 24158b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 24168b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 24178b987a77SJeff Roberson "Total pages currently allocated from VM"); 24188b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 24194ab3aee8SMark Johnston "free_items", CTLFLAG_RD, &dom->ud_free_items, 0, 24208b987a77SJeff Roberson "items free in the slab layer"); 24218b987a77SJeff Roberson } 242220a4e154SJeff Roberson } else 242320a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 242420a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 242520a4e154SJeff Roberson 242620a4e154SJeff Roberson /* 242720a4e154SJeff Roberson * Information about zone limits. 242820a4e154SJeff Roberson */ 242920a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 24307029da5cSPawel Biernacki "limit", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 24314bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 24324bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 24334bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 24344bd61e19SJeff Roberson "current number of allocated items if limit is set"); 243520a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 243620a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 243720a4e154SJeff Roberson "Maximum number of cached items"); 243820a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 243920a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 244020a4e154SJeff Roberson "Number of threads sleeping at limit"); 244120a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 244220a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 244320a4e154SJeff Roberson "Total zone limit sleeps"); 24444bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2445c6fd3e23SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bucket_max, 0, 2446c6fd3e23SJeff Roberson "Maximum number of items in each domain's bucket cache"); 244720a4e154SJeff Roberson 244820a4e154SJeff Roberson /* 24498b987a77SJeff Roberson * Per-domain zone information. 245020a4e154SJeff Roberson */ 245120a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 24527029da5cSPawel Biernacki OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 245320a4e154SJeff Roberson for (i = 0; i < domains; i++) { 2454c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 245520a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 24567029da5cSPawel Biernacki OID_AUTO, VM_DOMAIN(i)->vmd_name, 24577029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 245820a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 245920a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 246020a4e154SJeff Roberson "number of items in this domain"); 246120a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 246220a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 246320a4e154SJeff Roberson "maximum item count in this period"); 246420a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 246520a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 246620a4e154SJeff Roberson "minimum item count in this period"); 246720a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 246820a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 246920a4e154SJeff Roberson "Working set size"); 247020a4e154SJeff Roberson } 247120a4e154SJeff Roberson 247220a4e154SJeff Roberson /* 247320a4e154SJeff Roberson * General statistics. 247420a4e154SJeff Roberson */ 247520a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 24767029da5cSPawel Biernacki "stats", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 247720a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 247820a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 247920a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 248020a4e154SJeff Roberson "Current number of allocated items"); 248120a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 248220a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 248320a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 248420a4e154SJeff Roberson "Total allocation calls"); 248520a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 248620a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 248720a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 248820a4e154SJeff Roberson "Total free calls"); 248920a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 249020a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 249120a4e154SJeff Roberson "Number of allocation failures"); 2492c6fd3e23SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2493c6fd3e23SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 249420a4e154SJeff Roberson "Free calls from the wrong domain"); 249520a4e154SJeff Roberson } 249620a4e154SJeff Roberson 249720a4e154SJeff Roberson struct uma_zone_count { 249820a4e154SJeff Roberson const char *name; 249920a4e154SJeff Roberson int count; 250020a4e154SJeff Roberson }; 250120a4e154SJeff Roberson 250220a4e154SJeff Roberson static void 250320a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 250420a4e154SJeff Roberson { 250520a4e154SJeff Roberson struct uma_zone_count *cnt; 250620a4e154SJeff Roberson 250720a4e154SJeff Roberson cnt = arg; 25083b490537SJeff Roberson /* 25093b490537SJeff Roberson * Some zones are rapidly created with identical names and 25103b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 25113b490537SJeff Roberson * Use one greater than the maximum observed for this name. 25123b490537SJeff Roberson */ 251320a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 25143b490537SJeff Roberson cnt->count = MAX(cnt->count, 25153b490537SJeff Roberson zone->uz_namecnt + 1); 251620a4e154SJeff Roberson } 251720a4e154SJeff Roberson 2518cc7ce83aSJeff Roberson static void 2519cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2520cc7ce83aSJeff Roberson { 2521cc7ce83aSJeff Roberson int i; 2522cc7ce83aSJeff Roberson 2523cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2524cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2525cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2526cc7ce83aSJeff Roberson } 2527cc7ce83aSJeff Roberson } 2528cc7ce83aSJeff Roberson 2529099a0e58SBosko Milekic /* 2530099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2531099a0e58SBosko Milekic * 2532099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2533099a0e58SBosko Milekic * udata Actually uma_zctor_args 25348355f576SJeff Roberson */ 2535b23f72e9SBrian Feldman static int 2536b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 25378355f576SJeff Roberson { 253820a4e154SJeff Roberson struct uma_zone_count cnt; 25398355f576SJeff Roberson struct uma_zctor_args *arg = udata; 2540c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 25418355f576SJeff Roberson uma_zone_t zone = mem; 2542099a0e58SBosko Milekic uma_zone_t z; 2543099a0e58SBosko Milekic uma_keg_t keg; 254408cfa56eSMark Johnston int i; 25458355f576SJeff Roberson 25468355f576SJeff Roberson bzero(zone, size); 25478355f576SJeff Roberson zone->uz_name = arg->name; 25488355f576SJeff Roberson zone->uz_ctor = arg->ctor; 25498355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2550099a0e58SBosko Milekic zone->uz_init = NULL; 2551099a0e58SBosko Milekic zone->uz_fini = NULL; 2552bf965959SSean Bruno zone->uz_sleeps = 0; 255320a4e154SJeff Roberson zone->uz_bucket_size = 0; 255420a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 255520a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2556d4665eaaSJeff Roberson zone->uz_flags = (arg->flags & UMA_ZONE_SMR); 25572f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2558ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 2559c6fd3e23SJeff Roberson zone->uz_bucket_max = ULONG_MAX; 25602f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2561af526374SJeff Roberson 256220a4e154SJeff Roberson /* Count the number of duplicate names. */ 256320a4e154SJeff Roberson cnt.name = arg->name; 256420a4e154SJeff Roberson cnt.count = 0; 256520a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 256620a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 256791d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 25682efcc8cbSGleb Smirnoff 2569c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 2570c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 2571c6fd3e23SJeff Roberson ZDOM_LOCK_INIT(zone, zdom, (arg->flags & UMA_ZONE_MTXCLASS)); 2572c6fd3e23SJeff Roberson STAILQ_INIT(&zdom->uzd_buckets); 2573c6fd3e23SJeff Roberson } 257408cfa56eSMark Johnston 2575ca293436SRyan Libby #ifdef INVARIANTS 2576ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2577cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 2578ca293436SRyan Libby #endif 2579ca293436SRyan Libby 25800095a784SJeff Roberson /* 25810095a784SJeff Roberson * This is a pure cache zone, no kegs. 25820095a784SJeff Roberson */ 25830095a784SJeff Roberson if (arg->import) { 2584727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2585727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 25866fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2587af526374SJeff Roberson zone->uz_size = arg->size; 25880095a784SJeff Roberson zone->uz_import = arg->import; 25890095a784SJeff Roberson zone->uz_release = arg->release; 25900095a784SJeff Roberson zone->uz_arg = arg->arg; 2591c6fd3e23SJeff Roberson #ifdef NUMA 2592c6fd3e23SJeff Roberson /* 2593c6fd3e23SJeff Roberson * Cache zones are round-robin unless a policy is 2594c6fd3e23SJeff Roberson * specified because they may have incompatible 2595c6fd3e23SJeff Roberson * constraints. 2596c6fd3e23SJeff Roberson */ 2597c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2598c6fd3e23SJeff Roberson zone->uz_flags |= UMA_ZONE_ROUNDROBIN; 2599c6fd3e23SJeff Roberson #endif 2600111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 260103175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2602111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2603af526374SJeff Roberson goto out; 26040095a784SJeff Roberson } 26050095a784SJeff Roberson 26060095a784SJeff Roberson /* 26070095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 26080095a784SJeff Roberson */ 2609b75c4efcSAndrew Turner zone->uz_import = zone_import; 2610b75c4efcSAndrew Turner zone->uz_release = zone_release; 26110095a784SJeff Roberson zone->uz_arg = zone; 2612bb15d1c7SGleb Smirnoff keg = arg->keg; 26130095a784SJeff Roberson 2614099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 261520a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 261620a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2617099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 26188355f576SJeff Roberson zone->uz_init = arg->uminit; 2619e221e841SJeff Roberson zone->uz_fini = arg->fini; 2620e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2621111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2622099a0e58SBosko Milekic ZONE_LOCK(zone); 2623099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2624099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2625099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2626099a0e58SBosko Milekic break; 2627099a0e58SBosko Milekic } 2628099a0e58SBosko Milekic } 2629099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2630111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2631e20a199fSJeff Roberson } else if (keg == NULL) { 2632e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2633e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2634b23f72e9SBrian Feldman return (ENOMEM); 2635099a0e58SBosko Milekic } else { 2636099a0e58SBosko Milekic struct uma_kctor_args karg; 2637b23f72e9SBrian Feldman int error; 2638099a0e58SBosko Milekic 2639099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2640099a0e58SBosko Milekic karg.size = arg->size; 2641099a0e58SBosko Milekic karg.uminit = arg->uminit; 2642099a0e58SBosko Milekic karg.fini = arg->fini; 2643099a0e58SBosko Milekic karg.align = arg->align; 2644d4665eaaSJeff Roberson karg.flags = (arg->flags & ~UMA_ZONE_SMR); 2645099a0e58SBosko Milekic karg.zone = zone; 2646b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2647b23f72e9SBrian Feldman flags); 2648b23f72e9SBrian Feldman if (error) 2649b23f72e9SBrian Feldman return (error); 2650099a0e58SBosko Milekic } 26510095a784SJeff Roberson 265220a4e154SJeff Roberson /* Inherit properties from the keg. */ 2653bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2654e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2655e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2656e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 26578355f576SJeff Roberson 265820a4e154SJeff Roberson out: 2659dc2b3205SMark Johnston if (booted >= BOOT_PCPU) { 266020a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 2661dc2b3205SMark Johnston if (booted >= BOOT_RUNNING) 266220a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 266320a4e154SJeff Roberson } else { 266420a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 266520a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 266620a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2667099a0e58SBosko Milekic } 26688355f576SJeff Roberson 2669d4665eaaSJeff Roberson /* Caller requests a private SMR context. */ 2670d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 2671226dd6dbSJeff Roberson zone->uz_smr = smr_create(zone->uz_name, 0, 0); 2672d4665eaaSJeff Roberson 26737e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 26747e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 26757e28037aSMark Johnston ("Invalid zone flag combination")); 267620a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 267720a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 267820a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 267920a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 268020a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) 268120a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN; 268220a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 268320a4e154SJeff Roberson zone->uz_bucket_size = 0; 26847e28037aSMark Johnston else 268520a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 268620a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2687cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2688cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2689cc7ce83aSJeff Roberson zone_update_caches(zone); 2690fc03d22bSJeff Roberson 2691b23f72e9SBrian Feldman return (0); 26928355f576SJeff Roberson } 26938355f576SJeff Roberson 26948355f576SJeff Roberson /* 2695099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2696099a0e58SBosko Milekic * table and removes the keg from the global list. 26979c2cd7e5SJeff Roberson * 26989c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 26999c2cd7e5SJeff Roberson * udata unused 27009c2cd7e5SJeff Roberson */ 2701099a0e58SBosko Milekic static void 2702099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2703099a0e58SBosko Milekic { 2704099a0e58SBosko Milekic uma_keg_t keg; 27058b987a77SJeff Roberson uint32_t free, pages; 27068b987a77SJeff Roberson int i; 27079c2cd7e5SJeff Roberson 2708099a0e58SBosko Milekic keg = (uma_keg_t)arg; 27098b987a77SJeff Roberson free = pages = 0; 27108b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 27114ab3aee8SMark Johnston free += keg->uk_domain[i].ud_free_items; 27128b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 27138b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2714099a0e58SBosko Milekic } 27157e240677SRyan Libby if (pages != 0) 27168b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 27178b987a77SJeff Roberson " Lost %u pages of memory.\n", 27188b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 27197e240677SRyan Libby pages / keg->uk_ppera * keg->uk_ipers - free, pages); 2720099a0e58SBosko Milekic 2721099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2722099a0e58SBosko Milekic } 2723099a0e58SBosko Milekic 2724099a0e58SBosko Milekic /* 2725099a0e58SBosko Milekic * Zone header dtor. 2726099a0e58SBosko Milekic * 2727099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2728099a0e58SBosko Milekic * udata unused 2729099a0e58SBosko Milekic */ 27309c2cd7e5SJeff Roberson static void 27319c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 27329c2cd7e5SJeff Roberson { 27339c2cd7e5SJeff Roberson uma_zone_t zone; 2734099a0e58SBosko Milekic uma_keg_t keg; 2735c6fd3e23SJeff Roberson int i; 27369c2cd7e5SJeff Roberson 27379c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 27389643769aSJeff Roberson 273920a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 274020a4e154SJeff Roberson 2741e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 27429643769aSJeff Roberson cache_drain(zone); 2743099a0e58SBosko Milekic 2744111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2745099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2746111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 274708cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2748c6fd3e23SJeff Roberson 2749e20a199fSJeff Roberson /* 2750323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2751e20a199fSJeff Roberson */ 2752323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2753323ad386STycho Nightingale keg = zone->uz_keg; 2754111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2755099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2756111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 27570095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 27589c2cd7e5SJeff Roberson } 27592efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 27602efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 27612efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 2762c6fd3e23SJeff Roberson counter_u64_free(zone->uz_xdomain); 276320a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2764c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) 2765c6fd3e23SJeff Roberson ZDOM_LOCK_FINI(ZDOM_GET(zone, i)); 276691d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 2767099a0e58SBosko Milekic } 2768099a0e58SBosko Milekic 2769a81c400eSJeff Roberson static void 2770a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg) 2771a81c400eSJeff Roberson { 2772a81c400eSJeff Roberson uma_keg_t keg; 2773a81c400eSJeff Roberson uma_zone_t zone; 2774a81c400eSJeff Roberson 2775a81c400eSJeff Roberson LIST_FOREACH(keg, &uma_kegs, uk_link) { 2776a81c400eSJeff Roberson LIST_FOREACH(zone, &keg->uk_zones, uz_link) 2777a81c400eSJeff Roberson zfunc(zone, arg); 2778a81c400eSJeff Roberson } 2779a81c400eSJeff Roberson LIST_FOREACH(zone, &uma_cachezones, uz_link) 2780a81c400eSJeff Roberson zfunc(zone, arg); 2781a81c400eSJeff Roberson } 2782a81c400eSJeff Roberson 27839c2cd7e5SJeff Roberson /* 27848355f576SJeff Roberson * Traverses every zone in the system and calls a callback 27858355f576SJeff Roberson * 27868355f576SJeff Roberson * Arguments: 27878355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 27888355f576SJeff Roberson * as an argument. 27898355f576SJeff Roberson * 27908355f576SJeff Roberson * Returns: 27918355f576SJeff Roberson * Nothing 27928355f576SJeff Roberson */ 27938355f576SJeff Roberson static void 279420a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 27958355f576SJeff Roberson { 27968355f576SJeff Roberson 2797111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2798a81c400eSJeff Roberson zone_foreach_unlocked(zfunc, arg); 2799111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 28008355f576SJeff Roberson } 28018355f576SJeff Roberson 2802f4bef67cSGleb Smirnoff /* 2803a81c400eSJeff Roberson * Initialize the kernel memory allocator. This is done after pages can be 2804a81c400eSJeff Roberson * allocated but before general KVA is available. 2805f4bef67cSGleb Smirnoff */ 2806a81c400eSJeff Roberson void 2807a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail) 2808f4bef67cSGleb Smirnoff { 2809a81c400eSJeff Roberson struct uma_zctor_args args; 2810a81c400eSJeff Roberson size_t ksize, zsize, size; 2811c8b0a88bSJeff Roberson uma_keg_t primarykeg; 2812a81c400eSJeff Roberson uintptr_t m; 281381302f1dSMark Johnston int domain; 2814a81c400eSJeff Roberson uint8_t pflag; 2815a81c400eSJeff Roberson 2816a81c400eSJeff Roberson bootstart = bootmem = virtual_avail; 2817a81c400eSJeff Roberson 2818a81c400eSJeff Roberson rw_init(&uma_rwlock, "UMA lock"); 2819a81c400eSJeff Roberson sx_init(&uma_reclaim_lock, "umareclaim"); 2820f4bef67cSGleb Smirnoff 2821f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2822f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 282379c9f942SJeff Roberson ksize = roundup(ksize, UMA_SUPER_ALIGN); 2824f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2825f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2826f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 282779c9f942SJeff Roberson zsize = roundup(zsize, UMA_SUPER_ALIGN); 2828f4bef67cSGleb Smirnoff 2829a81c400eSJeff Roberson /* Allocate the zone of zones, zone of kegs, and zone of zones keg. */ 2830a81c400eSJeff Roberson size = (zsize * 2) + ksize; 283181302f1dSMark Johnston for (domain = 0; domain < vm_ndomains; domain++) { 283281302f1dSMark Johnston m = (uintptr_t)startup_alloc(NULL, size, domain, &pflag, 283381302f1dSMark Johnston M_NOWAIT | M_ZERO); 283481302f1dSMark Johnston if (m != 0) 283581302f1dSMark Johnston break; 283681302f1dSMark Johnston } 2837ab3185d1SJeff Roberson zones = (uma_zone_t)m; 283879c9f942SJeff Roberson m += zsize; 2839ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 284079c9f942SJeff Roberson m += zsize; 2841c8b0a88bSJeff Roberson primarykeg = (uma_keg_t)m; 2842ab3185d1SJeff Roberson 2843099a0e58SBosko Milekic /* "manually" create the initial zone */ 28440095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2845099a0e58SBosko Milekic args.name = "UMA Kegs"; 2846ab3185d1SJeff Roberson args.size = ksize; 2847099a0e58SBosko Milekic args.ctor = keg_ctor; 2848099a0e58SBosko Milekic args.dtor = keg_dtor; 28498355f576SJeff Roberson args.uminit = zero_init; 28508355f576SJeff Roberson args.fini = NULL; 2851c8b0a88bSJeff Roberson args.keg = primarykeg; 285279c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2853b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2854ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 28558355f576SJeff Roberson 2856099a0e58SBosko Milekic args.name = "UMA Zones"; 2857f4bef67cSGleb Smirnoff args.size = zsize; 2858099a0e58SBosko Milekic args.ctor = zone_ctor; 2859099a0e58SBosko Milekic args.dtor = zone_dtor; 2860099a0e58SBosko Milekic args.uminit = zero_init; 2861099a0e58SBosko Milekic args.fini = NULL; 2862099a0e58SBosko Milekic args.keg = NULL; 286379c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2864099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2865ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2866099a0e58SBosko Milekic 28679b8db4d0SRyan Libby /* Now make zones for slab headers */ 28689b8db4d0SRyan Libby slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE, 28699b8db4d0SRyan Libby NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 28709b8db4d0SRyan Libby slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE, 28711e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 28728355f576SJeff Roberson 28738355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 28748355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 28751e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 28768355f576SJeff Roberson 2877a81c400eSJeff Roberson bucket_init(); 2878d4665eaaSJeff Roberson smr_init(); 28798355f576SJeff Roberson } 28808355f576SJeff Roberson 2881a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2882a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void); 2883f4bef67cSGleb Smirnoff #endif 2884f4bef67cSGleb Smirnoff 2885a81c400eSJeff Roberson /* 2886a81c400eSJeff Roberson * Advertise the availability of normal kva allocations and switch to 2887a81c400eSJeff Roberson * the default back-end allocator. Marks the KVA we consumed on startup 2888a81c400eSJeff Roberson * as used in the map. 2889a81c400eSJeff Roberson */ 28908355f576SJeff Roberson void 289199571dc3SJeff Roberson uma_startup2(void) 28928355f576SJeff Roberson { 2893f4bef67cSGleb Smirnoff 2894530cc6a2SJeff Roberson if (bootstart != bootmem) { 2895a81c400eSJeff Roberson vm_map_lock(kernel_map); 2896a81c400eSJeff Roberson (void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem, 2897a81c400eSJeff Roberson VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 2898a81c400eSJeff Roberson vm_map_unlock(kernel_map); 2899a81c400eSJeff Roberson } 2900a81c400eSJeff Roberson 2901a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2902a81c400eSJeff Roberson /* Set up radix zone to use noobj_alloc. */ 2903a81c400eSJeff Roberson vm_radix_reserve_kva(); 2904f7d35785SGleb Smirnoff #endif 2905a81c400eSJeff Roberson 2906a81c400eSJeff Roberson booted = BOOT_KVA; 2907a81c400eSJeff Roberson zone_foreach_unlocked(zone_kva_available, NULL); 2908f4bef67cSGleb Smirnoff bucket_enable(); 29098355f576SJeff Roberson } 29108355f576SJeff Roberson 2911a81c400eSJeff Roberson /* 2912dc2b3205SMark Johnston * Allocate counters as early as possible so that boot-time allocations are 2913dc2b3205SMark Johnston * accounted more precisely. 2914dc2b3205SMark Johnston */ 2915dc2b3205SMark Johnston static void 2916dc2b3205SMark Johnston uma_startup_pcpu(void *arg __unused) 2917dc2b3205SMark Johnston { 2918dc2b3205SMark Johnston 2919dc2b3205SMark Johnston zone_foreach_unlocked(zone_alloc_counters, NULL); 2920dc2b3205SMark Johnston booted = BOOT_PCPU; 2921dc2b3205SMark Johnston } 2922dc2b3205SMark Johnston SYSINIT(uma_startup_pcpu, SI_SUB_COUNTER, SI_ORDER_ANY, uma_startup_pcpu, NULL); 2923dc2b3205SMark Johnston 2924dc2b3205SMark Johnston /* 2925a81c400eSJeff Roberson * Finish our initialization steps. 2926a81c400eSJeff Roberson */ 29278355f576SJeff Roberson static void 2928dc2b3205SMark Johnston uma_startup3(void *arg __unused) 29298355f576SJeff Roberson { 29301431a748SGleb Smirnoff 2931c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2932c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2933c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2934c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2935c5deaf04SGleb Smirnoff #endif 2936a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_sysctl, NULL); 2937fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 29389643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2939c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 2940860bb7a0SMark Johnston 2941860bb7a0SMark Johnston EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL, 2942860bb7a0SMark Johnston EVENTHANDLER_PRI_FIRST); 2943860bb7a0SMark Johnston } 2944dc2b3205SMark Johnston SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 2945860bb7a0SMark Johnston 2946860bb7a0SMark Johnston static void 2947860bb7a0SMark Johnston uma_shutdown(void) 2948860bb7a0SMark Johnston { 2949860bb7a0SMark Johnston 2950860bb7a0SMark Johnston booted = BOOT_SHUTDOWN; 29518355f576SJeff Roberson } 29528355f576SJeff Roberson 2953e20a199fSJeff Roberson static uma_keg_t 2954099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 295585dcf349SGleb Smirnoff int align, uint32_t flags) 2956099a0e58SBosko Milekic { 2957099a0e58SBosko Milekic struct uma_kctor_args args; 2958099a0e58SBosko Milekic 2959099a0e58SBosko Milekic args.size = size; 2960099a0e58SBosko Milekic args.uminit = uminit; 2961099a0e58SBosko Milekic args.fini = fini; 29621e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2963099a0e58SBosko Milekic args.flags = flags; 2964099a0e58SBosko Milekic args.zone = zone; 2965ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 2966099a0e58SBosko Milekic } 2967099a0e58SBosko Milekic 2968f4bef67cSGleb Smirnoff /* Public functions */ 29698355f576SJeff Roberson /* See uma.h */ 29701e319f6dSRobert Watson void 29711e319f6dSRobert Watson uma_set_align(int align) 29721e319f6dSRobert Watson { 29731e319f6dSRobert Watson 29741e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 29751e319f6dSRobert Watson uma_align_cache = align; 29761e319f6dSRobert Watson } 29771e319f6dSRobert Watson 29781e319f6dSRobert Watson /* See uma.h */ 29798355f576SJeff Roberson uma_zone_t 2980bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 298185dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 29828355f576SJeff Roberson 29838355f576SJeff Roberson { 29848355f576SJeff Roberson struct uma_zctor_args args; 298595c4bf75SKonstantin Belousov uma_zone_t res; 29868355f576SJeff Roberson 2987a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 2988a5a35578SJohn Baldwin align, name)); 2989a5a35578SJohn Baldwin 29908355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 29910095a784SJeff Roberson memset(&args, 0, sizeof(args)); 29928355f576SJeff Roberson args.name = name; 29938355f576SJeff Roberson args.size = size; 29948355f576SJeff Roberson args.ctor = ctor; 29958355f576SJeff Roberson args.dtor = dtor; 29968355f576SJeff Roberson args.uminit = uminit; 29978355f576SJeff Roberson args.fini = fini; 2998afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 2999afc6dc36SJohn-Mark Gurney /* 3000ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 3001ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 3002ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 3003ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 3004ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 3005ca293436SRyan Libby * the ctor and dtor do not need to be empty. 3006afc6dc36SJohn-Mark Gurney */ 300754c5ae80SRyan Libby if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH | 300854c5ae80SRyan Libby UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) { 3009afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 3010afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 3011afc6dc36SJohn-Mark Gurney } 3012afc6dc36SJohn-Mark Gurney #endif 30138355f576SJeff Roberson args.align = align; 30148355f576SJeff Roberson args.flags = flags; 3015099a0e58SBosko Milekic args.keg = NULL; 3016099a0e58SBosko Milekic 301708cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 3018ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 301908cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 3020a81c400eSJeff Roberson 302195c4bf75SKonstantin Belousov return (res); 3022099a0e58SBosko Milekic } 3023099a0e58SBosko Milekic 3024099a0e58SBosko Milekic /* See uma.h */ 3025099a0e58SBosko Milekic uma_zone_t 30260464f16eSMark Johnston uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor, 3027c8b0a88bSJeff Roberson uma_init zinit, uma_fini zfini, uma_zone_t primary) 3028099a0e58SBosko Milekic { 3029099a0e58SBosko Milekic struct uma_zctor_args args; 3030e20a199fSJeff Roberson uma_keg_t keg; 303195c4bf75SKonstantin Belousov uma_zone_t res; 3032099a0e58SBosko Milekic 3033c8b0a88bSJeff Roberson keg = primary->uz_keg; 30340095a784SJeff Roberson memset(&args, 0, sizeof(args)); 3035099a0e58SBosko Milekic args.name = name; 3036e20a199fSJeff Roberson args.size = keg->uk_size; 3037099a0e58SBosko Milekic args.ctor = ctor; 3038099a0e58SBosko Milekic args.dtor = dtor; 3039099a0e58SBosko Milekic args.uminit = zinit; 3040099a0e58SBosko Milekic args.fini = zfini; 3041e20a199fSJeff Roberson args.align = keg->uk_align; 3042e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 3043e20a199fSJeff Roberson args.keg = keg; 30448355f576SJeff Roberson 304508cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 3046ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 304708cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 3048a81c400eSJeff Roberson 304995c4bf75SKonstantin Belousov return (res); 30508355f576SJeff Roberson } 30518355f576SJeff Roberson 30520095a784SJeff Roberson /* See uma.h */ 30530095a784SJeff Roberson uma_zone_t 30540464f16eSMark Johnston uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor, 30550464f16eSMark Johnston uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease, 30560464f16eSMark Johnston void *arg, int flags) 30570095a784SJeff Roberson { 30580095a784SJeff Roberson struct uma_zctor_args args; 30590095a784SJeff Roberson 30600095a784SJeff Roberson memset(&args, 0, sizeof(args)); 30610095a784SJeff Roberson args.name = name; 3062af526374SJeff Roberson args.size = size; 30630095a784SJeff Roberson args.ctor = ctor; 30640095a784SJeff Roberson args.dtor = dtor; 30650095a784SJeff Roberson args.uminit = zinit; 30660095a784SJeff Roberson args.fini = zfini; 30670095a784SJeff Roberson args.import = zimport; 30680095a784SJeff Roberson args.release = zrelease; 30690095a784SJeff Roberson args.arg = arg; 30700095a784SJeff Roberson args.align = 0; 3071bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 30720095a784SJeff Roberson 3073ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 30740095a784SJeff Roberson } 30750095a784SJeff Roberson 30768355f576SJeff Roberson /* See uma.h */ 30779c2cd7e5SJeff Roberson void 30789c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 30799c2cd7e5SJeff Roberson { 3080f4ff923bSRobert Watson 3081860bb7a0SMark Johnston /* 3082860bb7a0SMark Johnston * Large slabs are expensive to reclaim, so don't bother doing 3083860bb7a0SMark Johnston * unnecessary work if we're shutting down. 3084860bb7a0SMark Johnston */ 3085860bb7a0SMark Johnston if (booted == BOOT_SHUTDOWN && 3086860bb7a0SMark Johnston zone->uz_fini == NULL && zone->uz_release == zone_release) 3087860bb7a0SMark Johnston return; 308808cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 30890095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 309008cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 30919c2cd7e5SJeff Roberson } 30929c2cd7e5SJeff Roberson 30938d6fbbb8SJeff Roberson void 30948d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 30958d6fbbb8SJeff Roberson { 30968d6fbbb8SJeff Roberson 309770260874SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 309870260874SJeff Roberson uma_zfree_smr(zone, uma_zalloc_smr(zone, M_WAITOK)); 309970260874SJeff Roberson else if ((zone->uz_flags & UMA_ZONE_PCPU) != 0) 310070260874SJeff Roberson uma_zfree_pcpu(zone, uma_zalloc_pcpu(zone, M_WAITOK)); 310170260874SJeff Roberson else 310270260874SJeff Roberson uma_zfree(zone, uma_zalloc(zone, M_WAITOK)); 31038d6fbbb8SJeff Roberson } 31048d6fbbb8SJeff Roberson 31054e180881SMateusz Guzik void * 31064e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 31074e180881SMateusz Guzik { 31083acb6572SMateusz Guzik void *item, *pcpu_item; 3109b4799947SRuslan Bukin #ifdef SMP 31104e180881SMateusz Guzik int i; 31114e180881SMateusz Guzik 31124e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 3113b4799947SRuslan Bukin #endif 31144e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 31153acb6572SMateusz Guzik if (item == NULL) 31163acb6572SMateusz Guzik return (NULL); 31173acb6572SMateusz Guzik pcpu_item = zpcpu_base_to_offset(item); 31183acb6572SMateusz Guzik if (flags & M_ZERO) { 3119b4799947SRuslan Bukin #ifdef SMP 3120013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 31213acb6572SMateusz Guzik bzero(zpcpu_get_cpu(pcpu_item, i), zone->uz_size); 3122b4799947SRuslan Bukin #else 3123b4799947SRuslan Bukin bzero(item, zone->uz_size); 3124b4799947SRuslan Bukin #endif 31254e180881SMateusz Guzik } 31263acb6572SMateusz Guzik return (pcpu_item); 31274e180881SMateusz Guzik } 31284e180881SMateusz Guzik 31294e180881SMateusz Guzik /* 31304e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 31314e180881SMateusz Guzik */ 31324e180881SMateusz Guzik void 31333acb6572SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *pcpu_item, void *udata) 31344e180881SMateusz Guzik { 31353acb6572SMateusz Guzik void *item; 31364e180881SMateusz Guzik 3137c5b7751fSIan Lepore #ifdef SMP 31384e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 3139c5b7751fSIan Lepore #endif 31403acb6572SMateusz Guzik item = zpcpu_offset_to_base(pcpu_item); 31414e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 31424e180881SMateusz Guzik } 31434e180881SMateusz Guzik 3144d4665eaaSJeff Roberson static inline void * 3145d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags, 3146d4665eaaSJeff Roberson void *item) 3147beb8beefSJeff Roberson { 3148beb8beefSJeff Roberson #ifdef INVARIANTS 3149ca293436SRyan Libby bool skipdbg; 3150beb8beefSJeff Roberson 3151beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 3152ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 3153ca293436SRyan Libby zone->uz_ctor != trash_ctor) 3154cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 3155beb8beefSJeff Roberson #endif 3156d4665eaaSJeff Roberson /* Check flags before loading ctor pointer. */ 3157d4665eaaSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) && 3158d4665eaaSJeff Roberson __predict_false(zone->uz_ctor != NULL) && 3159cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 3160beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3161beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 3162beb8beefSJeff Roberson return (NULL); 3163beb8beefSJeff Roberson } 3164beb8beefSJeff Roberson #ifdef INVARIANTS 3165beb8beefSJeff Roberson if (!skipdbg) 3166beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 3167beb8beefSJeff Roberson #endif 31686d88d784SJeff Roberson if (__predict_false(flags & M_ZERO)) 31696d88d784SJeff Roberson return (memset(item, 0, size)); 3170beb8beefSJeff Roberson 3171beb8beefSJeff Roberson return (item); 3172beb8beefSJeff Roberson } 3173beb8beefSJeff Roberson 3174ca293436SRyan Libby static inline void 3175cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 3176cc7ce83aSJeff Roberson enum zfreeskip skip) 3177ca293436SRyan Libby { 3178ca293436SRyan Libby #ifdef INVARIANTS 3179ca293436SRyan Libby bool skipdbg; 3180ca293436SRyan Libby 3181ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 3182ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 3183ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 3184ca293436SRyan Libby uma_dbg_free(zone, udata, item); 3185ca293436SRyan Libby else 3186ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 3187ca293436SRyan Libby } 3188ca293436SRyan Libby #endif 3189cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 3190ca293436SRyan Libby if (zone->uz_dtor != NULL) 3191cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 3192ca293436SRyan Libby #ifdef INVARIANTS 3193ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 3194ca293436SRyan Libby zone->uz_dtor != trash_dtor) 3195cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 3196ca293436SRyan Libby #endif 3197ca293436SRyan Libby } 3198ca293436SRyan Libby } 3199ca293436SRyan Libby 32001c58c09fSMateusz Guzik #ifdef NUMA 320181302f1dSMark Johnston static int 320281302f1dSMark Johnston item_domain(void *item) 320381302f1dSMark Johnston { 320481302f1dSMark Johnston int domain; 320581302f1dSMark Johnston 320681302f1dSMark Johnston domain = _vm_phys_domain(vtophys(item)); 320781302f1dSMark Johnston KASSERT(domain >= 0 && domain < vm_ndomains, 320881302f1dSMark Johnston ("%s: unknown domain for item %p", __func__, item)); 320981302f1dSMark Johnston return (domain); 321081302f1dSMark Johnston } 32111c58c09fSMateusz Guzik #endif 321281302f1dSMark Johnston 3213d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS) 3214d4665eaaSJeff Roberson #define UMA_ZALLOC_DEBUG 3215d4665eaaSJeff Roberson static int 3216d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags) 3217d4665eaaSJeff Roberson { 3218d4665eaaSJeff Roberson int error; 3219d4665eaaSJeff Roberson 3220d4665eaaSJeff Roberson error = 0; 3221d4665eaaSJeff Roberson #ifdef WITNESS 3222d4665eaaSJeff Roberson if (flags & M_WAITOK) { 3223d4665eaaSJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3224d4665eaaSJeff Roberson "uma_zalloc_debug: zone \"%s\"", zone->uz_name); 3225d4665eaaSJeff Roberson } 3226d4665eaaSJeff Roberson #endif 3227d4665eaaSJeff Roberson 3228d4665eaaSJeff Roberson #ifdef INVARIANTS 3229d4665eaaSJeff Roberson KASSERT((flags & M_EXEC) == 0, 3230d4665eaaSJeff Roberson ("uma_zalloc_debug: called with M_EXEC")); 3231d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3232d4665eaaSJeff Roberson ("uma_zalloc_debug: called within spinlock or critical section")); 3233d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0, 3234d4665eaaSJeff Roberson ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO")); 3235d4665eaaSJeff Roberson #endif 3236d4665eaaSJeff Roberson 3237d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 32389e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) { 3239d4665eaaSJeff Roberson void *item; 3240d4665eaaSJeff Roberson item = memguard_alloc(zone->uz_size, flags); 3241d4665eaaSJeff Roberson if (item != NULL) { 3242d4665eaaSJeff Roberson error = EJUSTRETURN; 3243d4665eaaSJeff Roberson if (zone->uz_init != NULL && 3244d4665eaaSJeff Roberson zone->uz_init(item, zone->uz_size, flags) != 0) { 3245d4665eaaSJeff Roberson *itemp = NULL; 3246d4665eaaSJeff Roberson return (error); 3247d4665eaaSJeff Roberson } 3248d4665eaaSJeff Roberson if (zone->uz_ctor != NULL && 3249d4665eaaSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 3250d4665eaaSJeff Roberson flags) != 0) { 3251d4665eaaSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3252d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3253d4665eaaSJeff Roberson *itemp = NULL; 3254d4665eaaSJeff Roberson return (error); 3255d4665eaaSJeff Roberson } 3256d4665eaaSJeff Roberson *itemp = item; 3257d4665eaaSJeff Roberson return (error); 3258d4665eaaSJeff Roberson } 3259d4665eaaSJeff Roberson /* This is unfortunate but should not be fatal. */ 3260d4665eaaSJeff Roberson } 3261d4665eaaSJeff Roberson #endif 3262d4665eaaSJeff Roberson return (error); 3263d4665eaaSJeff Roberson } 3264d4665eaaSJeff Roberson 3265d4665eaaSJeff Roberson static int 3266d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata) 3267d4665eaaSJeff Roberson { 3268d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3269d4665eaaSJeff Roberson ("uma_zfree_debug: called with spinlock or critical section held")); 3270d4665eaaSJeff Roberson 3271d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 32729e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) { 3273d4665eaaSJeff Roberson if (zone->uz_dtor != NULL) 3274d4665eaaSJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 3275d4665eaaSJeff Roberson if (zone->uz_fini != NULL) 3276d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3277d4665eaaSJeff Roberson memguard_free(item); 3278d4665eaaSJeff Roberson return (EJUSTRETURN); 3279d4665eaaSJeff Roberson } 3280d4665eaaSJeff Roberson #endif 3281d4665eaaSJeff Roberson return (0); 3282d4665eaaSJeff Roberson } 3283d4665eaaSJeff Roberson #endif 3284d4665eaaSJeff Roberson 32856d88d784SJeff Roberson static inline void * 32866d88d784SJeff Roberson cache_alloc_item(uma_zone_t zone, uma_cache_t cache, uma_cache_bucket_t bucket, 32876d88d784SJeff Roberson void *udata, int flags) 3288d4665eaaSJeff Roberson { 32896d88d784SJeff Roberson void *item; 32906d88d784SJeff Roberson int size, uz_flags; 32916d88d784SJeff Roberson 32926d88d784SJeff Roberson item = cache_bucket_pop(cache, bucket); 32936d88d784SJeff Roberson size = cache_uz_size(cache); 32946d88d784SJeff Roberson uz_flags = cache_uz_flags(cache); 32956d88d784SJeff Roberson critical_exit(); 32966d88d784SJeff Roberson return (item_ctor(zone, uz_flags, size, udata, flags, item)); 32976d88d784SJeff Roberson } 32986d88d784SJeff Roberson 32996d88d784SJeff Roberson static __noinline void * 33006d88d784SJeff Roberson cache_alloc_retry(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 33016d88d784SJeff Roberson { 33026d88d784SJeff Roberson uma_cache_bucket_t bucket; 3303d4665eaaSJeff Roberson int domain; 3304d4665eaaSJeff Roberson 33056d88d784SJeff Roberson while (cache_alloc(zone, cache, udata, flags)) { 33066d88d784SJeff Roberson cache = &zone->uz_cpu[curcpu]; 33076d88d784SJeff Roberson bucket = &cache->uc_allocbucket; 33086d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 33096d88d784SJeff Roberson continue; 33106d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, udata, flags)); 33116d88d784SJeff Roberson } 33126d88d784SJeff Roberson critical_exit(); 33136d88d784SJeff Roberson 3314d4665eaaSJeff Roberson /* 3315d4665eaaSJeff Roberson * We can not get a bucket so try to return a single item. 3316d4665eaaSJeff Roberson */ 3317d4665eaaSJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 3318d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3319d4665eaaSJeff Roberson else 3320d4665eaaSJeff Roberson domain = UMA_ANYDOMAIN; 3321d4665eaaSJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3322d4665eaaSJeff Roberson } 3323d4665eaaSJeff Roberson 3324d4665eaaSJeff Roberson /* See uma.h */ 3325d4665eaaSJeff Roberson void * 3326d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags) 3327d4665eaaSJeff Roberson { 3328d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 3329d4665eaaSJeff Roberson uma_cache_t cache; 3330d4665eaaSJeff Roberson 3331d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 33326d88d784SJeff Roberson void *item; 33336d88d784SJeff Roberson 3334d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 3335d4665eaaSJeff Roberson ("uma_zalloc_arg: called with non-SMR zone.\n")); 3336d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN) 3337d4665eaaSJeff Roberson return (item); 3338d4665eaaSJeff Roberson #endif 3339d4665eaaSJeff Roberson 3340d4665eaaSJeff Roberson critical_enter(); 3341d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3342d4665eaaSJeff Roberson bucket = &cache->uc_allocbucket; 33436d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 33446d88d784SJeff Roberson return (cache_alloc_retry(zone, cache, NULL, flags)); 33456d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, NULL, flags)); 3346d4665eaaSJeff Roberson } 3347d4665eaaSJeff Roberson 33489c2cd7e5SJeff Roberson /* See uma.h */ 33498355f576SJeff Roberson void * 33502cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 33518355f576SJeff Roberson { 3352376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3353ab3185d1SJeff Roberson uma_cache_t cache; 33548355f576SJeff Roberson 3355e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 335619fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 335710cb2424SMark Murray 33588355f576SJeff Roberson /* This is the fast path allocation */ 3359e63a1c2fSRyan Libby CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name, 3360e63a1c2fSRyan Libby zone, flags); 3361a553d4b8SJeff Roberson 3362d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 33636d88d784SJeff Roberson void *item; 33646d88d784SJeff Roberson 3365d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 3366d4665eaaSJeff Roberson ("uma_zalloc_arg: called with SMR zone.\n")); 3367d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN) 33688d689e04SGleb Smirnoff return (item); 33698d689e04SGleb Smirnoff #endif 3370d4665eaaSJeff Roberson 33715d1ae027SRobert Watson /* 33725d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 33735d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 33745d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 33755d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 33765d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 33775d1ae027SRobert Watson * preemption and migration. We release the critical section in 33785d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 33795d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 33805d1ae027SRobert Watson * must detect and handle migration if it has occurred. 33815d1ae027SRobert Watson */ 33825d1ae027SRobert Watson critical_enter(); 3383cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3384376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 33856d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 33866d88d784SJeff Roberson return (cache_alloc_retry(zone, cache, udata, flags)); 33876d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, udata, flags)); 3388fc03d22bSJeff Roberson } 3389fc03d22bSJeff Roberson 33908355f576SJeff Roberson /* 3391beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 3392beb8beefSJeff Roberson * a critical section. Returns in a critical section. 3393beb8beefSJeff Roberson * 33944bd61e19SJeff Roberson * A false return value indicates an allocation failure. 33954bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 3396beb8beefSJeff Roberson */ 3397beb8beefSJeff Roberson static __noinline bool 3398beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3399beb8beefSJeff Roberson { 3400beb8beefSJeff Roberson uma_bucket_t bucket; 34018c277118SMark Johnston int curdomain, domain; 3402c6fd3e23SJeff Roberson bool new; 3403beb8beefSJeff Roberson 3404beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3405beb8beefSJeff Roberson 3406beb8beefSJeff Roberson /* 3407beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3408beb8beefSJeff Roberson * if we can switch with the free bucket. 3409d4665eaaSJeff Roberson * 3410d4665eaaSJeff Roberson * SMR Zones can't re-use the free bucket until the sequence has 3411d4665eaaSJeff Roberson * expired. 34128355f576SJeff Roberson */ 3413c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_SMR) == 0 && 3414d4665eaaSJeff Roberson cache->uc_freebucket.ucb_cnt != 0) { 3415d4665eaaSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 3416d4665eaaSJeff Roberson &cache->uc_allocbucket); 3417beb8beefSJeff Roberson return (true); 34188355f576SJeff Roberson } 3419fc03d22bSJeff Roberson 3420fc03d22bSJeff Roberson /* 3421fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3422fc03d22bSJeff Roberson */ 3423376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3424fc03d22bSJeff Roberson critical_exit(); 3425c6fd3e23SJeff Roberson 3426c6fd3e23SJeff Roberson if (bucket != NULL) { 3427c6fd3e23SJeff Roberson KASSERT(bucket->ub_cnt == 0, 3428c6fd3e23SJeff Roberson ("cache_alloc: Entered with non-empty alloc bucket.")); 34296fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3430c6fd3e23SJeff Roberson } 3431fc03d22bSJeff Roberson 34325d1ae027SRobert Watson /* 34335d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 3434c6fd3e23SJeff Roberson * we must go back to the zone. This requires the zdom lock, so we 34355d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 34365d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 34375d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 34385d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 34395d1ae027SRobert Watson * the critical section. 34405d1ae027SRobert Watson */ 3441c1685086SJeff Roberson domain = PCPU_GET(domain); 34428c277118SMark Johnston if ((cache_uz_flags(cache) & UMA_ZONE_ROUNDROBIN) != 0 || 34438c277118SMark Johnston VM_DOMAIN_EMPTY(domain)) 3444c6fd3e23SJeff Roberson domain = zone_domain_highest(zone, domain); 3445c6fd3e23SJeff Roberson bucket = cache_fetch_bucket(zone, cache, domain); 3446*af32cefdSMark Johnston if (bucket == NULL && zone->uz_bucket_size != 0 && !bucketdisable) { 3447beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 3448c6fd3e23SJeff Roberson new = true; 3449*af32cefdSMark Johnston } else { 3450c6fd3e23SJeff Roberson new = false; 3451*af32cefdSMark Johnston } 3452c6fd3e23SJeff Roberson 34531431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 34541431a748SGleb Smirnoff zone->uz_name, zone, bucket); 34554bd61e19SJeff Roberson if (bucket == NULL) { 3456fc03d22bSJeff Roberson critical_enter(); 3457beb8beefSJeff Roberson return (false); 34584bd61e19SJeff Roberson } 34590f9b7bf3SMark Johnston 3460fc03d22bSJeff Roberson /* 3461fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3462fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3463fc03d22bSJeff Roberson * the memory directly. 3464fc03d22bSJeff Roberson */ 34654bd61e19SJeff Roberson critical_enter(); 3466cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3467376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 3468c6fd3e23SJeff Roberson ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) == 0 || 34698c277118SMark Johnston (curdomain = PCPU_GET(domain)) == domain || 34708c277118SMark Johnston VM_DOMAIN_EMPTY(curdomain))) { 3471c6fd3e23SJeff Roberson if (new) 3472c6fd3e23SJeff Roberson atomic_add_long(&ZDOM_GET(zone, domain)->uzd_imax, 3473c6fd3e23SJeff Roberson bucket->ub_cnt); 3474376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3475beb8beefSJeff Roberson return (true); 3476c6fd3e23SJeff Roberson } 3477c6fd3e23SJeff Roberson 3478c6fd3e23SJeff Roberson /* 3479c6fd3e23SJeff Roberson * We lost the race, release this bucket and start over. 3480c6fd3e23SJeff Roberson */ 3481c6fd3e23SJeff Roberson critical_exit(); 3482c6fd3e23SJeff Roberson zone_put_bucket(zone, domain, bucket, udata, false); 3483c6fd3e23SJeff Roberson critical_enter(); 3484c6fd3e23SJeff Roberson 3485beb8beefSJeff Roberson return (true); 3486bbee39c6SJeff Roberson } 3487bbee39c6SJeff Roberson 3488ab3185d1SJeff Roberson void * 3489ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3490bbee39c6SJeff Roberson { 3491ab3185d1SJeff Roberson 3492ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 349319fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3494ab3185d1SJeff Roberson 3495ab3185d1SJeff Roberson /* This is the fast path allocation */ 3496e63a1c2fSRyan Libby CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d", 3497e63a1c2fSRyan Libby zone->uz_name, zone, domain, flags); 3498ab3185d1SJeff Roberson 3499ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3500ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3501ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3502ab3185d1SJeff Roberson } 3503ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3504ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 3505ab3185d1SJeff Roberson 3506ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3507ab3185d1SJeff Roberson } 3508ab3185d1SJeff Roberson 3509ab3185d1SJeff Roberson /* 3510ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3511ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3512ab3185d1SJeff Roberson * 3513ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3514ab3185d1SJeff Roberson * only 'domain'. 3515ab3185d1SJeff Roberson */ 3516ab3185d1SJeff Roberson static uma_slab_t 3517194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3518ab3185d1SJeff Roberson { 3519ab3185d1SJeff Roberson uma_domain_t dom; 3520bbee39c6SJeff Roberson uma_slab_t slab; 3521ab3185d1SJeff Roberson int start; 3522ab3185d1SJeff Roberson 3523ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3524ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 35258b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3526ab3185d1SJeff Roberson 3527ab3185d1SJeff Roberson slab = NULL; 3528ab3185d1SJeff Roberson start = domain; 3529ab3185d1SJeff Roberson do { 3530ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 35314ab3aee8SMark Johnston if ((slab = LIST_FIRST(&dom->ud_part_slab)) != NULL) 35324ab3aee8SMark Johnston return (slab); 35334ab3aee8SMark Johnston if ((slab = LIST_FIRST(&dom->ud_free_slab)) != NULL) { 3534ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 35354ab3aee8SMark Johnston dom->ud_free_slabs--; 3536ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3537ab3185d1SJeff Roberson return (slab); 3538ab3185d1SJeff Roberson } 3539ab3185d1SJeff Roberson if (rr) 3540ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3541ab3185d1SJeff Roberson } while (domain != start); 3542ab3185d1SJeff Roberson 3543ab3185d1SJeff Roberson return (NULL); 3544ab3185d1SJeff Roberson } 3545ab3185d1SJeff Roberson 35468b987a77SJeff Roberson /* 35478b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 35488b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 35498b987a77SJeff Roberson */ 3550ab3185d1SJeff Roberson static uma_slab_t 3551194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3552ab3185d1SJeff Roberson { 35538b987a77SJeff Roberson uma_slab_t slab; 3554194a979eSMark Johnston uint32_t reserve; 3555099a0e58SBosko Milekic 35568b987a77SJeff Roberson /* HASH has a single free list. */ 355754c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 35588b987a77SJeff Roberson domain = 0; 3559194a979eSMark Johnston 35608b987a77SJeff Roberson KEG_LOCK(keg, domain); 3561194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 35624ab3aee8SMark Johnston if (keg->uk_domain[domain].ud_free_items <= reserve || 35638b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 35648b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3565194a979eSMark Johnston return (NULL); 35668b987a77SJeff Roberson } 35678b987a77SJeff Roberson return (slab); 3568194a979eSMark Johnston } 3569194a979eSMark Johnston 3570194a979eSMark Johnston static uma_slab_t 3571194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3572194a979eSMark Johnston { 3573194a979eSMark Johnston struct vm_domainset_iter di; 3574194a979eSMark Johnston uma_slab_t slab; 3575194a979eSMark Johnston int aflags, domain; 3576194a979eSMark Johnston bool rr; 3577194a979eSMark Johnston 3578194a979eSMark Johnston restart: 3579bbee39c6SJeff Roberson /* 3580194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3581194a979eSMark Johnston * domain (as happens with first-touch zones). 3582194a979eSMark Johnston * 3583194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3584194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3585194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3586bbee39c6SJeff Roberson */ 3587ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3588ab3185d1SJeff Roberson if (rr) { 3589194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3590194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3591194a979eSMark Johnston &aflags); 3592194a979eSMark Johnston } else { 3593194a979eSMark Johnston aflags = flags; 3594194a979eSMark Johnston domain = rdomain; 3595194a979eSMark Johnston } 3596ab3185d1SJeff Roberson 3597194a979eSMark Johnston for (;;) { 3598194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3599584061b4SJeff Roberson if (slab != NULL) 3600bbee39c6SJeff Roberson return (slab); 3601bbee39c6SJeff Roberson 3602bbee39c6SJeff Roberson /* 3603bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3604bbee39c6SJeff Roberson */ 3605bbee39c6SJeff Roberson if (flags & M_NOVM) 3606bbee39c6SJeff Roberson break; 3607bbee39c6SJeff Roberson 360886220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 36098b987a77SJeff Roberson if (slab != NULL) 3610bbee39c6SJeff Roberson return (slab); 36113639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 36123639ac42SJeff Roberson break; 3613194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3614194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 3615194a979eSMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 3616194a979eSMark Johnston goto restart; 361730c5525bSAndrew Gallatin } 3618194a979eSMark Johnston break; 3619194a979eSMark Johnston } 3620ab3185d1SJeff Roberson } 3621ab3185d1SJeff Roberson 3622bbee39c6SJeff Roberson /* 3623bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3624bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3625bbee39c6SJeff Roberson * fail. 3626bbee39c6SJeff Roberson */ 36278b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 3628bbee39c6SJeff Roberson return (slab); 36298b987a77SJeff Roberson 3630ab3185d1SJeff Roberson return (NULL); 3631ab3185d1SJeff Roberson } 3632bbee39c6SJeff Roberson 3633d56368d7SBosko Milekic static void * 36340095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3635bbee39c6SJeff Roberson { 3636ab3185d1SJeff Roberson uma_domain_t dom; 3637bbee39c6SJeff Roberson void *item; 36389b8db4d0SRyan Libby int freei; 3639bbee39c6SJeff Roberson 36408b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3641099a0e58SBosko Milekic 36428b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 36439b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 36449b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 36451e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3646bbee39c6SJeff Roberson slab->us_freecount--; 36474ab3aee8SMark Johnston dom->ud_free_items--; 3648ef72505eSJeff Roberson 36494ab3aee8SMark Johnston /* 36504ab3aee8SMark Johnston * Move this slab to the full list. It must be on the partial list, so 36514ab3aee8SMark Johnston * we do not need to update the free slab count. In particular, 36524ab3aee8SMark Johnston * keg_fetch_slab() always returns slabs on the partial list. 36534ab3aee8SMark Johnston */ 3654bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3655bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3656ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3657bbee39c6SJeff Roberson } 3658bbee39c6SJeff Roberson 3659bbee39c6SJeff Roberson return (item); 3660bbee39c6SJeff Roberson } 3661bbee39c6SJeff Roberson 3662bbee39c6SJeff Roberson static int 3663b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 36640095a784SJeff Roberson { 36658b987a77SJeff Roberson uma_domain_t dom; 3666b75c4efcSAndrew Turner uma_zone_t zone; 36670095a784SJeff Roberson uma_slab_t slab; 36680095a784SJeff Roberson uma_keg_t keg; 3669a03af342SSean Bruno #ifdef NUMA 3670ab3185d1SJeff Roberson int stripe; 3671a03af342SSean Bruno #endif 36720095a784SJeff Roberson int i; 36730095a784SJeff Roberson 3674b75c4efcSAndrew Turner zone = arg; 36750095a784SJeff Roberson slab = NULL; 3676584061b4SJeff Roberson keg = zone->uz_keg; 3677af526374SJeff Roberson /* Try to keep the buckets totally full */ 36780095a784SJeff Roberson for (i = 0; i < max; ) { 3679584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 36800095a784SJeff Roberson break; 3681a03af342SSean Bruno #ifdef NUMA 3682ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3683a03af342SSean Bruno #endif 36848b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 36856fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 36860095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 36874ab3aee8SMark Johnston if (dom->ud_free_items <= keg->uk_reserve) 36886fd34d6fSJeff Roberson break; 3689b6715dabSJeff Roberson #ifdef NUMA 3690ab3185d1SJeff Roberson /* 3691ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3692ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3693ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3694ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3695ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3696ab3185d1SJeff Roberson * time but yields better distribution. 3697ab3185d1SJeff Roberson */ 3698dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 && 3699ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3700ab3185d1SJeff Roberson break; 3701ab3185d1SJeff Roberson #endif 37026fd34d6fSJeff Roberson } 37038b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 3704ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 37050095a784SJeff Roberson flags &= ~M_WAITOK; 37060095a784SJeff Roberson flags |= M_NOWAIT; 37070095a784SJeff Roberson } 37080095a784SJeff Roberson 37090095a784SJeff Roberson return i; 37100095a784SJeff Roberson } 37110095a784SJeff Roberson 37124bd61e19SJeff Roberson static int 37134bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 37144bd61e19SJeff Roberson { 37154bd61e19SJeff Roberson uint64_t old, new, total, max; 37164bd61e19SJeff Roberson 37174bd61e19SJeff Roberson /* 37184bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 37194bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 37204bd61e19SJeff Roberson * fairness by keeping fifo order. 37214bd61e19SJeff Roberson * 37224bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 37234bd61e19SJeff Roberson */ 37244bd61e19SJeff Roberson for (;;) { 37254bd61e19SJeff Roberson zone_free_limit(zone, count); 37264bd61e19SJeff Roberson zone_log_warning(zone); 37274bd61e19SJeff Roberson zone_maxaction(zone); 37284bd61e19SJeff Roberson if (flags & M_NOWAIT) 37294bd61e19SJeff Roberson return (0); 37304bd61e19SJeff Roberson 37314bd61e19SJeff Roberson /* 37324bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 37334bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 37344bd61e19SJeff Roberson * is essentially a home rolled semaphore. 37354bd61e19SJeff Roberson */ 37364bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 37374bd61e19SJeff Roberson old = zone->uz_items; 37384bd61e19SJeff Roberson do { 37394bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 37404bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 37414bd61e19SJeff Roberson max = zone->uz_max_items; 37424bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 37434bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 37444bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 37454bd61e19SJeff Roberson else 37464bd61e19SJeff Roberson new = old + MIN(count, max - old); 37474bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 37484bd61e19SJeff Roberson 37494bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 37504bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 37514bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 37524bd61e19SJeff Roberson return (new - old); 37534bd61e19SJeff Roberson } 37544bd61e19SJeff Roberson 37554bd61e19SJeff Roberson /* 37564bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 37574bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 37584bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 37594bd61e19SJeff Roberson * transitions. 37604bd61e19SJeff Roberson */ 37614bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 37624bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 37634bd61e19SJeff Roberson 37644bd61e19SJeff Roberson /* 37654bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 37664bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 37674bd61e19SJeff Roberson */ 37684bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 37694bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 37704bd61e19SJeff Roberson 37714bd61e19SJeff Roberson /* 37724bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 37734bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 37744bd61e19SJeff Roberson * 37754bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 37764bd61e19SJeff Roberson * our count. 37774bd61e19SJeff Roberson */ 37784bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 37794bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 37804bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 37814bd61e19SJeff Roberson /* We're no longer a sleeper. */ 37824bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 37834bd61e19SJeff Roberson 37844bd61e19SJeff Roberson /* 37854bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 37864bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 37874bd61e19SJeff Roberson * against changes via sysctl. 37884bd61e19SJeff Roberson */ 37894bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 37904bd61e19SJeff Roberson max = zone->uz_max_items; 37914bd61e19SJeff Roberson if (total >= max) 37924bd61e19SJeff Roberson continue; 37934bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 37944bd61e19SJeff Roberson if (total + count > max) { 37954bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 37964bd61e19SJeff Roberson count = max - total; 37974bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 37984bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 37994bd61e19SJeff Roberson 38004bd61e19SJeff Roberson return (count); 38014bd61e19SJeff Roberson } 38024bd61e19SJeff Roberson } 38034bd61e19SJeff Roberson 38044bd61e19SJeff Roberson /* 38054bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 38064bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 38074bd61e19SJeff Roberson * one item can be allocated. 38084bd61e19SJeff Roberson */ 38094bd61e19SJeff Roberson static int 38104bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 38114bd61e19SJeff Roberson { 38124bd61e19SJeff Roberson uint64_t old; 38134bd61e19SJeff Roberson uint64_t max; 38144bd61e19SJeff Roberson 38154bd61e19SJeff Roberson max = zone->uz_max_items; 38164bd61e19SJeff Roberson MPASS(max > 0); 38174bd61e19SJeff Roberson 38184bd61e19SJeff Roberson /* 38194bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 38204bd61e19SJeff Roberson * fetchadd. 38214bd61e19SJeff Roberson */ 38224bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 38234bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 38244bd61e19SJeff Roberson return (count); 38254bd61e19SJeff Roberson 38264bd61e19SJeff Roberson /* 38274bd61e19SJeff Roberson * If we had some items and no sleepers just return the 38284bd61e19SJeff Roberson * truncated value. We have to release the excess space 38294bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 38304bd61e19SJeff Roberson * because we were temporarily over the limit. 38314bd61e19SJeff Roberson */ 38324bd61e19SJeff Roberson if (old < max) { 38334bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 38344bd61e19SJeff Roberson return (max - old); 38354bd61e19SJeff Roberson } 38364bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 38374bd61e19SJeff Roberson } 38384bd61e19SJeff Roberson 38394bd61e19SJeff Roberson /* 38404bd61e19SJeff Roberson * Free a number of items back to the limit. 38414bd61e19SJeff Roberson */ 38424bd61e19SJeff Roberson static void 38434bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 38444bd61e19SJeff Roberson { 38454bd61e19SJeff Roberson uint64_t old; 38464bd61e19SJeff Roberson 38474bd61e19SJeff Roberson MPASS(count > 0); 38484bd61e19SJeff Roberson 38494bd61e19SJeff Roberson /* 38504bd61e19SJeff Roberson * In the common case we either have no sleepers or 38514bd61e19SJeff Roberson * are still over the limit and can just return. 38524bd61e19SJeff Roberson */ 38534bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 38544bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 38554bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 38564bd61e19SJeff Roberson return; 38574bd61e19SJeff Roberson 38584bd61e19SJeff Roberson /* 38594bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 38604bd61e19SJeff Roberson * to generate wakeups if necessary. 38614bd61e19SJeff Roberson */ 38624bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 38634bd61e19SJeff Roberson } 38644bd61e19SJeff Roberson 3865fc03d22bSJeff Roberson static uma_bucket_t 3866beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3867bbee39c6SJeff Roberson { 3868bbee39c6SJeff Roberson uma_bucket_t bucket; 3869beb8beefSJeff Roberson int maxbucket, cnt; 3870bbee39c6SJeff Roberson 3871e63a1c2fSRyan Libby CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name, 3872e63a1c2fSRyan Libby zone, domain); 387330c5525bSAndrew Gallatin 3874c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3875c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3876c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 38778c277118SMark Johnston else if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0) 3878c6fd3e23SJeff Roberson domain = UMA_ANYDOMAIN; 3879c1685086SJeff Roberson 38804bd61e19SJeff Roberson if (zone->uz_max_items > 0) 38814bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 38824bd61e19SJeff Roberson M_NOWAIT); 38834bd61e19SJeff Roberson else 388420a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 38854bd61e19SJeff Roberson if (maxbucket == 0) 38864bd61e19SJeff Roberson return (false); 3887beb8beefSJeff Roberson 38886fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 38896fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3890beb8beefSJeff Roberson if (bucket == NULL) { 3891beb8beefSJeff Roberson cnt = 0; 3892beb8beefSJeff Roberson goto out; 3893beb8beefSJeff Roberson } 38940095a784SJeff Roberson 38950095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3896beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 38970095a784SJeff Roberson 38980095a784SJeff Roberson /* 38990095a784SJeff Roberson * Initialize the memory if necessary. 39000095a784SJeff Roberson */ 39010095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 3902099a0e58SBosko Milekic int i; 3903bbee39c6SJeff Roberson 39040095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 3905e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 39060095a784SJeff Roberson flags) != 0) 3907b23f72e9SBrian Feldman break; 3908b23f72e9SBrian Feldman /* 3909b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 3910b23f72e9SBrian Feldman * rest back onto the freelist. 3911b23f72e9SBrian Feldman */ 3912b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 3913af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 39140095a784SJeff Roberson bucket->ub_cnt - i); 3915a5a262c6SBosko Milekic #ifdef INVARIANTS 39160095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 39170095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 3918a5a262c6SBosko Milekic #endif 3919b23f72e9SBrian Feldman bucket->ub_cnt = i; 3920b23f72e9SBrian Feldman } 3921099a0e58SBosko Milekic } 3922099a0e58SBosko Milekic 3923beb8beefSJeff Roberson cnt = bucket->ub_cnt; 3924f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 39256fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 39262efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 3927beb8beefSJeff Roberson bucket = NULL; 3928beb8beefSJeff Roberson } 3929beb8beefSJeff Roberson out: 39304bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 39314bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 3932fc03d22bSJeff Roberson 3933fc03d22bSJeff Roberson return (bucket); 3934fc03d22bSJeff Roberson } 3935fc03d22bSJeff Roberson 39368355f576SJeff Roberson /* 39370095a784SJeff Roberson * Allocates a single item from a zone. 39388355f576SJeff Roberson * 39398355f576SJeff Roberson * Arguments 39408355f576SJeff Roberson * zone The zone to alloc for. 39418355f576SJeff Roberson * udata The data to be passed to the constructor. 3942ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 3943a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 39448355f576SJeff Roberson * 39458355f576SJeff Roberson * Returns 39468355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 3947bbee39c6SJeff Roberson * An item if successful 39488355f576SJeff Roberson */ 39498355f576SJeff Roberson 39508355f576SJeff Roberson static void * 3951ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 39528355f576SJeff Roberson { 39538355f576SJeff Roberson void *item; 39548355f576SJeff Roberson 39554bd61e19SJeff Roberson if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) 3956bb15d1c7SGleb Smirnoff return (NULL); 39578355f576SJeff Roberson 3958c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3959c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 396030c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 3961c1685086SJeff Roberson 3962ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 3963beb8beefSJeff Roberson goto fail_cnt; 39648355f576SJeff Roberson 3965099a0e58SBosko Milekic /* 3966099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 3967099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 3968099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 3969099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 3970099a0e58SBosko Milekic */ 3971b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 3972e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 3973bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 3974beb8beefSJeff Roberson goto fail_cnt; 3975beb8beefSJeff Roberson } 3976beb8beefSJeff Roberson } 3977d4665eaaSJeff Roberson item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags, 3978d4665eaaSJeff Roberson item); 3979beb8beefSJeff Roberson if (item == NULL) 39800095a784SJeff Roberson goto fail; 39818355f576SJeff Roberson 39822efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 39831431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 39841431a748SGleb Smirnoff zone->uz_name, zone); 39851431a748SGleb Smirnoff 39868355f576SJeff Roberson return (item); 39870095a784SJeff Roberson 3988beb8beefSJeff Roberson fail_cnt: 3989beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 39900095a784SJeff Roberson fail: 39914bd61e19SJeff Roberson if (zone->uz_max_items > 0) 39924bd61e19SJeff Roberson zone_free_limit(zone, 1); 39931431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 39941431a748SGleb Smirnoff zone->uz_name, zone); 39954bd61e19SJeff Roberson 39960095a784SJeff Roberson return (NULL); 39978355f576SJeff Roberson } 39988355f576SJeff Roberson 39998355f576SJeff Roberson /* See uma.h */ 40008355f576SJeff Roberson void 4001d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item) 4002d4665eaaSJeff Roberson { 4003d4665eaaSJeff Roberson uma_cache_t cache; 4004d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 4005c6fd3e23SJeff Roberson int itemdomain, uz_flags; 4006d4665eaaSJeff Roberson 4007d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 4008d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 4009d4665eaaSJeff Roberson ("uma_zfree_smr: called with non-SMR zone.\n")); 4010d4665eaaSJeff Roberson KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer.")); 4011c6fd3e23SJeff Roberson SMR_ASSERT_NOT_ENTERED(zone->uz_smr); 4012d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN) 4013d4665eaaSJeff Roberson return; 4014d4665eaaSJeff Roberson #endif 4015d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4016d4665eaaSJeff Roberson uz_flags = cache_uz_flags(cache); 4017c6fd3e23SJeff Roberson itemdomain = 0; 4018d4665eaaSJeff Roberson #ifdef NUMA 4019d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 402081302f1dSMark Johnston itemdomain = item_domain(item); 4021d4665eaaSJeff Roberson #endif 4022d4665eaaSJeff Roberson critical_enter(); 4023d4665eaaSJeff Roberson do { 4024d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4025d4665eaaSJeff Roberson /* SMR Zones must free to the free bucket. */ 4026d4665eaaSJeff Roberson bucket = &cache->uc_freebucket; 4027d4665eaaSJeff Roberson #ifdef NUMA 4028d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4029c6fd3e23SJeff Roberson PCPU_GET(domain) != itemdomain) { 4030d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 4031d4665eaaSJeff Roberson } 4032d4665eaaSJeff Roberson #endif 4033d4665eaaSJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 4034d4665eaaSJeff Roberson cache_bucket_push(cache, bucket, item); 4035d4665eaaSJeff Roberson critical_exit(); 4036d4665eaaSJeff Roberson return; 4037d4665eaaSJeff Roberson } 4038d4665eaaSJeff Roberson } while (cache_free(zone, cache, NULL, item, itemdomain)); 4039d4665eaaSJeff Roberson critical_exit(); 4040d4665eaaSJeff Roberson 4041d4665eaaSJeff Roberson /* 4042d4665eaaSJeff Roberson * If nothing else caught this, we'll just do an internal free. 4043d4665eaaSJeff Roberson */ 4044d4665eaaSJeff Roberson zone_free_item(zone, item, NULL, SKIP_NONE); 4045d4665eaaSJeff Roberson } 4046d4665eaaSJeff Roberson 4047d4665eaaSJeff Roberson /* See uma.h */ 4048d4665eaaSJeff Roberson void 40498355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 40508355f576SJeff Roberson { 40518355f576SJeff Roberson uma_cache_t cache; 4052376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 4053c6fd3e23SJeff Roberson int itemdomain, uz_flags; 40548355f576SJeff Roberson 4055e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 405619fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 405710cb2424SMark Murray 4058e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone); 40593659f747SRobert Watson 4060d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 4061d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 4062d4665eaaSJeff Roberson ("uma_zfree_arg: called with SMR zone.\n")); 4063d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN) 4064d4665eaaSJeff Roberson return; 4065d4665eaaSJeff Roberson #endif 406620ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 406720ed0cb0SMatthew D Fleming if (item == NULL) 406820ed0cb0SMatthew D Fleming return; 4069cc7ce83aSJeff Roberson 4070cc7ce83aSJeff Roberson /* 4071cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 4072cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 4073cc7ce83aSJeff Roberson * will simply read another cpu's line. 4074cc7ce83aSJeff Roberson */ 4075cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4076cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 4077d4665eaaSJeff Roberson if (UMA_ALWAYS_CTORDTOR || 4078d4665eaaSJeff Roberson __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0)) 4079cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 4080ef72505eSJeff Roberson 4081af7f9b97SJeff Roberson /* 4082af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 4083af7f9b97SJeff Roberson * a little longer for the limits to be reset. 4084af7f9b97SJeff Roberson */ 4085cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 4086bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0) 4087fc03d22bSJeff Roberson goto zfree_item; 4088cc7ce83aSJeff Roberson } 4089af7f9b97SJeff Roberson 40905d1ae027SRobert Watson /* 40915d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 40925d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 40935d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 40945d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 40955d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 40965d1ae027SRobert Watson * preemption and migration. We release the critical section in 40975d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 40985d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 40995d1ae027SRobert Watson * detect and handle migration if it has occurred. 41005d1ae027SRobert Watson */ 4101c6fd3e23SJeff Roberson itemdomain = 0; 4102dfe13344SJeff Roberson #ifdef NUMA 4103dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 410481302f1dSMark Johnston itemdomain = item_domain(item); 4105dfe13344SJeff Roberson #endif 41065d1ae027SRobert Watson critical_enter(); 41070a81b439SJeff Roberson do { 4108cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4109a553d4b8SJeff Roberson /* 4110dfe13344SJeff Roberson * Try to free into the allocbucket first to give LIFO 4111dfe13344SJeff Roberson * ordering for cache-hot datastructures. Spill over 4112dfe13344SJeff Roberson * into the freebucket if necessary. Alloc will swap 4113dfe13344SJeff Roberson * them if one runs dry. 4114a553d4b8SJeff Roberson */ 4115dfe13344SJeff Roberson bucket = &cache->uc_allocbucket; 4116d4665eaaSJeff Roberson #ifdef NUMA 4117d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4118c6fd3e23SJeff Roberson PCPU_GET(domain) != itemdomain) { 4119d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 4120d4665eaaSJeff Roberson } else 4121d4665eaaSJeff Roberson #endif 4122fe835cbfSJeff Roberson if (bucket->ucb_cnt == bucket->ucb_entries && 4123fe835cbfSJeff Roberson cache->uc_freebucket.ucb_cnt < 4124fe835cbfSJeff Roberson cache->uc_freebucket.ucb_entries) 4125fe835cbfSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 4126fe835cbfSJeff Roberson &cache->uc_allocbucket); 4127376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 4128376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 41295d1ae027SRobert Watson critical_exit(); 41308355f576SJeff Roberson return; 4131fc03d22bSJeff Roberson } 41320a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 41330a81b439SJeff Roberson critical_exit(); 4134fc03d22bSJeff Roberson 41358355f576SJeff Roberson /* 41360a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 41378355f576SJeff Roberson */ 41380a81b439SJeff Roberson zfree_item: 41390a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 41400a81b439SJeff Roberson } 4141fc03d22bSJeff Roberson 4142dfe13344SJeff Roberson #ifdef NUMA 414391d947bfSJeff Roberson /* 414491d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 414591d947bfSJeff Roberson * them. 414691d947bfSJeff Roberson */ 414791d947bfSJeff Roberson static void 414891d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 414991d947bfSJeff Roberson { 415091d947bfSJeff Roberson struct uma_bucketlist fullbuckets; 415191d947bfSJeff Roberson uma_zone_domain_t zdom; 415291d947bfSJeff Roberson uma_bucket_t b; 4153543117beSJeff Roberson smr_seq_t seq; 415491d947bfSJeff Roberson void *item; 415591d947bfSJeff Roberson int domain; 415691d947bfSJeff Roberson 415791d947bfSJeff Roberson CTR3(KTR_UMA, 415891d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 415991d947bfSJeff Roberson zone->uz_name, zone, bucket); 416091d947bfSJeff Roberson 4161543117beSJeff Roberson /* 4162543117beSJeff Roberson * It is possible for buckets to arrive here out of order so we fetch 4163543117beSJeff Roberson * the current smr seq rather than accepting the bucket's. 4164543117beSJeff Roberson */ 4165543117beSJeff Roberson seq = SMR_SEQ_INVALID; 4166543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 4167226dd6dbSJeff Roberson seq = smr_advance(zone->uz_smr); 4168226dd6dbSJeff Roberson 4169226dd6dbSJeff Roberson /* 4170226dd6dbSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 4171226dd6dbSJeff Roberson * lock on the current crossfree bucket. A full matrix with 4172226dd6dbSJeff Roberson * per-domain locking could be used if necessary. 4173226dd6dbSJeff Roberson */ 4174226dd6dbSJeff Roberson STAILQ_INIT(&fullbuckets); 4175226dd6dbSJeff Roberson ZONE_CROSS_LOCK(zone); 417691d947bfSJeff Roberson while (bucket->ub_cnt > 0) { 417791d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 417881302f1dSMark Johnston domain = item_domain(item); 4179c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 418091d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 418191d947bfSJeff Roberson zdom->uzd_cross = bucket_alloc(zone, udata, M_NOWAIT); 418291d947bfSJeff Roberson if (zdom->uzd_cross == NULL) 418391d947bfSJeff Roberson break; 418491d947bfSJeff Roberson } 4185543117beSJeff Roberson b = zdom->uzd_cross; 4186543117beSJeff Roberson b->ub_bucket[b->ub_cnt++] = item; 4187543117beSJeff Roberson b->ub_seq = seq; 4188543117beSJeff Roberson if (b->ub_cnt == b->ub_entries) { 4189543117beSJeff Roberson STAILQ_INSERT_HEAD(&fullbuckets, b, ub_link); 419091d947bfSJeff Roberson zdom->uzd_cross = NULL; 419191d947bfSJeff Roberson } 419291d947bfSJeff Roberson bucket->ub_cnt--; 419391d947bfSJeff Roberson } 419491d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 4195c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 4196d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 419791d947bfSJeff Roberson bucket_free(zone, bucket, udata); 4198c6fd3e23SJeff Roberson 4199c6fd3e23SJeff Roberson while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) { 4200c6fd3e23SJeff Roberson STAILQ_REMOVE_HEAD(&fullbuckets, ub_link); 420181302f1dSMark Johnston domain = item_domain(b->ub_bucket[0]); 4202c6fd3e23SJeff Roberson zone_put_bucket(zone, domain, b, udata, true); 4203c6fd3e23SJeff Roberson } 420491d947bfSJeff Roberson } 420591d947bfSJeff Roberson #endif 420691d947bfSJeff Roberson 42070a81b439SJeff Roberson static void 42080a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 4209c6fd3e23SJeff Roberson int itemdomain, bool ws) 42100a81b439SJeff Roberson { 42110a81b439SJeff Roberson 4212dfe13344SJeff Roberson #ifdef NUMA 42130a81b439SJeff Roberson /* 42140a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 42150a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 42160a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 421791d947bfSJeff Roberson * correct domains. 42180a81b439SJeff Roberson */ 4219c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4220c6fd3e23SJeff Roberson vm_ndomains > 2 && PCPU_GET(domain) != itemdomain) { 422191d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 42220a81b439SJeff Roberson return; 42230a81b439SJeff Roberson } 42240a81b439SJeff Roberson #endif 422591d947bfSJeff Roberson 42260a81b439SJeff Roberson /* 42270a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 42280a81b439SJeff Roberson */ 42290a81b439SJeff Roberson CTR3(KTR_UMA, 42300a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 42310a81b439SJeff Roberson zone->uz_name, zone, bucket); 42320a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 4233c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0) 4234c6fd3e23SJeff Roberson itemdomain = zone_domain_lowest(zone, itemdomain); 4235c6fd3e23SJeff Roberson zone_put_bucket(zone, itemdomain, bucket, udata, ws); 42368355f576SJeff Roberson } 4237fc03d22bSJeff Roberson 42384d104ba0SAlexander Motin /* 42390a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 42400a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 42410a81b439SJeff Roberson * 42420a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 42430a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 42440a81b439SJeff Roberson * the caller should retry. 42454d104ba0SAlexander Motin */ 42460a81b439SJeff Roberson static __noinline bool 42470a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 42480a81b439SJeff Roberson int itemdomain) 42490a81b439SJeff Roberson { 4250dfe13344SJeff Roberson uma_cache_bucket_t cbucket; 4251d4665eaaSJeff Roberson uma_bucket_t newbucket, bucket; 42520a81b439SJeff Roberson 42530a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 42540a81b439SJeff Roberson 4255d4665eaaSJeff Roberson if (zone->uz_bucket_size == 0) 42560a81b439SJeff Roberson return false; 42570a81b439SJeff Roberson 4258cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4259d4665eaaSJeff Roberson newbucket = NULL; 42600a81b439SJeff Roberson 42610a81b439SJeff Roberson /* 4262dfe13344SJeff Roberson * FIRSTTOUCH domains need to free to the correct zdom. When 4263dfe13344SJeff Roberson * enabled this is the zdom of the item. The bucket is the 4264dfe13344SJeff Roberson * cross bucket if the current domain and itemdomain do not match. 42650a81b439SJeff Roberson */ 4266dfe13344SJeff Roberson cbucket = &cache->uc_freebucket; 4267dfe13344SJeff Roberson #ifdef NUMA 4268c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) { 4269c6fd3e23SJeff Roberson if (PCPU_GET(domain) != itemdomain) { 4270dfe13344SJeff Roberson cbucket = &cache->uc_crossbucket; 4271dfe13344SJeff Roberson if (cbucket->ucb_cnt != 0) 4272c6fd3e23SJeff Roberson counter_u64_add(zone->uz_xdomain, 4273dfe13344SJeff Roberson cbucket->ucb_cnt); 4274dfe13344SJeff Roberson } 4275c6fd3e23SJeff Roberson } 42760a81b439SJeff Roberson #endif 4277dfe13344SJeff Roberson bucket = cache_bucket_unload(cbucket); 4278c6fd3e23SJeff Roberson KASSERT(bucket == NULL || bucket->ub_cnt == bucket->ub_entries, 4279c6fd3e23SJeff Roberson ("cache_free: Entered with non-full free bucket.")); 42800a81b439SJeff Roberson 42810a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 42820a81b439SJeff Roberson critical_exit(); 42830a81b439SJeff Roberson 4284d4665eaaSJeff Roberson /* 4285d4665eaaSJeff Roberson * Don't let SMR zones operate without a free bucket. Force 4286d4665eaaSJeff Roberson * a synchronize and re-use this one. We will only degrade 4287d4665eaaSJeff Roberson * to a synchronize every bucket_size items rather than every 4288d4665eaaSJeff Roberson * item if we fail to allocate a bucket. 4289d4665eaaSJeff Roberson */ 4290d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) { 4291d4665eaaSJeff Roberson if (bucket != NULL) 4292d4665eaaSJeff Roberson bucket->ub_seq = smr_advance(zone->uz_smr); 4293d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4294d4665eaaSJeff Roberson if (newbucket == NULL && bucket != NULL) { 4295d4665eaaSJeff Roberson bucket_drain(zone, bucket); 4296d4665eaaSJeff Roberson newbucket = bucket; 4297d4665eaaSJeff Roberson bucket = NULL; 4298d4665eaaSJeff Roberson } 4299d4665eaaSJeff Roberson } else if (!bucketdisable) 4300d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4301d4665eaaSJeff Roberson 43020a81b439SJeff Roberson if (bucket != NULL) 4303c6fd3e23SJeff Roberson zone_free_bucket(zone, bucket, udata, itemdomain, true); 4304a553d4b8SJeff Roberson 4305fc03d22bSJeff Roberson critical_enter(); 4306d4665eaaSJeff Roberson if ((bucket = newbucket) == NULL) 43070a81b439SJeff Roberson return (false); 4308cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4309dfe13344SJeff Roberson #ifdef NUMA 4310fc03d22bSJeff Roberson /* 43110a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 43120a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 43130a81b439SJeff Roberson * the free bucket. 4314fc03d22bSJeff Roberson */ 4315c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) { 4316c6fd3e23SJeff Roberson if (PCPU_GET(domain) != itemdomain && 4317376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 4318376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 43190a81b439SJeff Roberson return (true); 43200a81b439SJeff Roberson } 43210a81b439SJeff Roberson } 43220a81b439SJeff Roberson #endif 43230a81b439SJeff Roberson /* 43240a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 43250a81b439SJeff Roberson */ 4326376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 4327fc03d22bSJeff Roberson critical_exit(); 43286fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 43290a81b439SJeff Roberson critical_enter(); 43300a81b439SJeff Roberson } else 4331376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 43328355f576SJeff Roberson 43330a81b439SJeff Roberson return (true); 43348355f576SJeff Roberson } 43358355f576SJeff Roberson 43368355f576SJeff Roberson static void 4337bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 43388355f576SJeff Roberson { 4339bb15d1c7SGleb Smirnoff uma_keg_t keg; 4340ab3185d1SJeff Roberson uma_domain_t dom; 43419b8db4d0SRyan Libby int freei; 4342099a0e58SBosko Milekic 4343bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 43448b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 4345ab3185d1SJeff Roberson 43468355f576SJeff Roberson /* Do we need to remove from any lists? */ 43478b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 4348099a0e58SBosko Milekic if (slab->us_freecount + 1 == keg->uk_ipers) { 43498355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4350ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 43514ab3aee8SMark Johnston dom->ud_free_slabs++; 43528355f576SJeff Roberson } else if (slab->us_freecount == 0) { 43538355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4354ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 43558355f576SJeff Roberson } 43568355f576SJeff Roberson 4357ef72505eSJeff Roberson /* Slab management. */ 43581e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 43599b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 43608355f576SJeff Roberson slab->us_freecount++; 43618355f576SJeff Roberson 4362ef72505eSJeff Roberson /* Keg statistics. */ 43634ab3aee8SMark Johnston dom->ud_free_items++; 43640095a784SJeff Roberson } 43650095a784SJeff Roberson 43660095a784SJeff Roberson static void 4367b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 43680095a784SJeff Roberson { 43698b987a77SJeff Roberson struct mtx *lock; 4370b75c4efcSAndrew Turner uma_zone_t zone; 43710095a784SJeff Roberson uma_slab_t slab; 43720095a784SJeff Roberson uma_keg_t keg; 43730095a784SJeff Roberson uint8_t *mem; 43748b987a77SJeff Roberson void *item; 43750095a784SJeff Roberson int i; 43768355f576SJeff Roberson 4377b75c4efcSAndrew Turner zone = arg; 4378bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 43798b987a77SJeff Roberson lock = NULL; 438054c5ae80SRyan Libby if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0)) 43818b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 43820095a784SJeff Roberson for (i = 0; i < cnt; i++) { 43830095a784SJeff Roberson item = bucket[i]; 438454c5ae80SRyan Libby if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) { 43850095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 43868b987a77SJeff Roberson } else { 43878b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 438854c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0) 43898b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 43908b987a77SJeff Roberson else 43918b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 43928b987a77SJeff Roberson } 43938b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 43948b987a77SJeff Roberson if (lock != NULL) 43958b987a77SJeff Roberson mtx_unlock(lock); 43968b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 43978b987a77SJeff Roberson } 4398bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 43990095a784SJeff Roberson } 44008b987a77SJeff Roberson if (lock != NULL) 44018b987a77SJeff Roberson mtx_unlock(lock); 44028355f576SJeff Roberson } 44038355f576SJeff Roberson 44040095a784SJeff Roberson /* 44050095a784SJeff Roberson * Frees a single item to any zone. 44060095a784SJeff Roberson * 44070095a784SJeff Roberson * Arguments: 44080095a784SJeff Roberson * zone The zone to free to 44090095a784SJeff Roberson * item The item we're freeing 44100095a784SJeff Roberson * udata User supplied data for the dtor 44110095a784SJeff Roberson * skip Skip dtors and finis 44120095a784SJeff Roberson */ 44136d88d784SJeff Roberson static __noinline void 44140095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 44150095a784SJeff Roberson { 4416c5deaf04SGleb Smirnoff 4417d4665eaaSJeff Roberson /* 4418d4665eaaSJeff Roberson * If a free is sent directly to an SMR zone we have to 4419d4665eaaSJeff Roberson * synchronize immediately because the item can instantly 4420d4665eaaSJeff Roberson * be reallocated. This should only happen in degenerate 4421d4665eaaSJeff Roberson * cases when no memory is available for per-cpu caches. 4422d4665eaaSJeff Roberson */ 4423d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE) 4424d4665eaaSJeff Roberson smr_synchronize(zone->uz_smr); 4425d4665eaaSJeff Roberson 4426cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 44270095a784SJeff Roberson 44280095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 44290095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 44300095a784SJeff Roberson 44310095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4432bb15d1c7SGleb Smirnoff 4433bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4434bb15d1c7SGleb Smirnoff return; 4435bb15d1c7SGleb Smirnoff 44362efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 44372efcc8cbSGleb Smirnoff 44384bd61e19SJeff Roberson if (zone->uz_max_items > 0) 44394bd61e19SJeff Roberson zone_free_limit(zone, 1); 4440bb45b411SGleb Smirnoff } 44410095a784SJeff Roberson 44428355f576SJeff Roberson /* See uma.h */ 44431c6cae97SLawrence Stewart int 4444736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4445736ee590SJeff Roberson { 4446bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 4447003cf08bSMark Johnston int count; 4448bb15d1c7SGleb Smirnoff 44494bd61e19SJeff Roberson /* 44504bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 44514bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 44524bd61e19SJeff Roberson * way to clear a limit. 44534bd61e19SJeff Roberson */ 4454bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4455003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4456003cf08bSMark Johnston count = ubz != NULL ? ubz->ubz_entries : 0; 445720a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = count; 445820a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 445920a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4460bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4461cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4462cc7ce83aSJeff Roberson zone_update_caches(zone); 44634bd61e19SJeff Roberson /* We may need to wake waiters. */ 44644bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4465bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4466bb15d1c7SGleb Smirnoff 4467bb15d1c7SGleb Smirnoff return (nitems); 4468bb15d1c7SGleb Smirnoff } 4469bb15d1c7SGleb Smirnoff 4470bb15d1c7SGleb Smirnoff /* See uma.h */ 4471003cf08bSMark Johnston void 4472bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4473bb15d1c7SGleb Smirnoff { 4474003cf08bSMark Johnston struct uma_bucket_zone *ubz; 4475003cf08bSMark Johnston int bpcpu; 4476bb15d1c7SGleb Smirnoff 4477bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4478003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4479003cf08bSMark Johnston if (ubz != NULL) { 4480003cf08bSMark Johnston bpcpu = 2; 4481dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 4482003cf08bSMark Johnston /* Count the cross-domain bucket. */ 4483003cf08bSMark Johnston bpcpu++; 4484003cf08bSMark Johnston nitems -= ubz->ubz_entries * bpcpu * mp_ncpus; 448520a4e154SJeff Roberson zone->uz_bucket_size_max = ubz->ubz_entries; 4486003cf08bSMark Johnston } else { 448720a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 4488003cf08bSMark Johnston } 448920a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 449020a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4491c6fd3e23SJeff Roberson zone->uz_bucket_max = nitems / vm_ndomains; 4492bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4493736ee590SJeff Roberson } 4494736ee590SJeff Roberson 4495736ee590SJeff Roberson /* See uma.h */ 4496e49471b0SAndre Oppermann int 4497e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4498e49471b0SAndre Oppermann { 4499e49471b0SAndre Oppermann int nitems; 4500e49471b0SAndre Oppermann 4501727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4502e49471b0SAndre Oppermann 4503e49471b0SAndre Oppermann return (nitems); 4504e49471b0SAndre Oppermann } 4505e49471b0SAndre Oppermann 4506e49471b0SAndre Oppermann /* See uma.h */ 45072f891cd5SPawel Jakub Dawidek void 45082f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 45092f891cd5SPawel Jakub Dawidek { 45102f891cd5SPawel Jakub Dawidek 4511727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 45122f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 45132f891cd5SPawel Jakub Dawidek } 45142f891cd5SPawel Jakub Dawidek 45152f891cd5SPawel Jakub Dawidek /* See uma.h */ 451654503a13SJonathan T. Looney void 451754503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 451854503a13SJonathan T. Looney { 451954503a13SJonathan T. Looney 4520727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4521e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 452254503a13SJonathan T. Looney } 452354503a13SJonathan T. Looney 452454503a13SJonathan T. Looney /* See uma.h */ 4525c4ae7908SLawrence Stewart int 4526c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4527c4ae7908SLawrence Stewart { 4528c4ae7908SLawrence Stewart int64_t nitems; 4529c4ae7908SLawrence Stewart u_int i; 4530c4ae7908SLawrence Stewart 4531bfb6b7a1SJeff Roberson nitems = 0; 4532bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER) 45332efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 45342efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4535727c6918SJeff Roberson CPU_FOREACH(i) 4536727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4537727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4538c4ae7908SLawrence Stewart 4539c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4540c4ae7908SLawrence Stewart } 4541c4ae7908SLawrence Stewart 454220a4e154SJeff Roberson static uint64_t 454320a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 454420a4e154SJeff Roberson { 454520a4e154SJeff Roberson uint64_t nitems; 454620a4e154SJeff Roberson u_int i; 454720a4e154SJeff Roberson 4548bfb6b7a1SJeff Roberson nitems = 0; 4549bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER) 455020a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4551727c6918SJeff Roberson CPU_FOREACH(i) 4552727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 455320a4e154SJeff Roberson 455420a4e154SJeff Roberson return (nitems); 455520a4e154SJeff Roberson } 455620a4e154SJeff Roberson 455720a4e154SJeff Roberson static uint64_t 455820a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 455920a4e154SJeff Roberson { 456020a4e154SJeff Roberson uint64_t nitems; 456120a4e154SJeff Roberson u_int i; 456220a4e154SJeff Roberson 4563bfb6b7a1SJeff Roberson nitems = 0; 4564bfb6b7a1SJeff Roberson if (zone->uz_frees != EARLY_COUNTER) 456520a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4566727c6918SJeff Roberson CPU_FOREACH(i) 4567727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 456820a4e154SJeff Roberson 456920a4e154SJeff Roberson return (nitems); 457020a4e154SJeff Roberson } 457120a4e154SJeff Roberson 457231c251a0SJeff Roberson #ifdef INVARIANTS 457331c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */ 457431c251a0SJeff Roberson static uint64_t 457531c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg) 457631c251a0SJeff Roberson { 457731c251a0SJeff Roberson uma_zone_t z; 457831c251a0SJeff Roberson uint64_t nitems; 457931c251a0SJeff Roberson 458031c251a0SJeff Roberson nitems = 0; 458131c251a0SJeff Roberson LIST_FOREACH(z, &keg->uk_zones, uz_link) 458231c251a0SJeff Roberson nitems += uma_zone_get_allocs(z); 458331c251a0SJeff Roberson 458431c251a0SJeff Roberson return (nitems); 458531c251a0SJeff Roberson } 458631c251a0SJeff Roberson #endif 458731c251a0SJeff Roberson 4588c4ae7908SLawrence Stewart /* See uma.h */ 4589736ee590SJeff Roberson void 4590099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4591099a0e58SBosko Milekic { 4592e20a199fSJeff Roberson uma_keg_t keg; 4593e20a199fSJeff Roberson 4594bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4595727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4596e20a199fSJeff Roberson keg->uk_init = uminit; 4597099a0e58SBosko Milekic } 4598099a0e58SBosko Milekic 4599099a0e58SBosko Milekic /* See uma.h */ 4600099a0e58SBosko Milekic void 4601099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4602099a0e58SBosko Milekic { 4603e20a199fSJeff Roberson uma_keg_t keg; 4604e20a199fSJeff Roberson 4605bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4606727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4607e20a199fSJeff Roberson keg->uk_fini = fini; 4608099a0e58SBosko Milekic } 4609099a0e58SBosko Milekic 4610099a0e58SBosko Milekic /* See uma.h */ 4611099a0e58SBosko Milekic void 4612099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4613099a0e58SBosko Milekic { 4614af526374SJeff Roberson 4615727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4616099a0e58SBosko Milekic zone->uz_init = zinit; 4617099a0e58SBosko Milekic } 4618099a0e58SBosko Milekic 4619099a0e58SBosko Milekic /* See uma.h */ 4620099a0e58SBosko Milekic void 4621099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4622099a0e58SBosko Milekic { 4623af526374SJeff Roberson 4624727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4625099a0e58SBosko Milekic zone->uz_fini = zfini; 4626099a0e58SBosko Milekic } 4627099a0e58SBosko Milekic 4628099a0e58SBosko Milekic /* See uma.h */ 4629099a0e58SBosko Milekic void 46308355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 46318355f576SJeff Roberson { 46320095a784SJeff Roberson uma_keg_t keg; 4633e20a199fSJeff Roberson 4634bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4635727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 46360095a784SJeff Roberson keg->uk_freef = freef; 46378355f576SJeff Roberson } 46388355f576SJeff Roberson 46398355f576SJeff Roberson /* See uma.h */ 46408355f576SJeff Roberson void 46418355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 46428355f576SJeff Roberson { 4643e20a199fSJeff Roberson uma_keg_t keg; 4644e20a199fSJeff Roberson 4645bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4646727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4647e20a199fSJeff Roberson keg->uk_allocf = allocf; 46488355f576SJeff Roberson } 46498355f576SJeff Roberson 46508355f576SJeff Roberson /* See uma.h */ 46516fd34d6fSJeff Roberson void 4652d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr) 4653d4665eaaSJeff Roberson { 4654d4665eaaSJeff Roberson 4655d4665eaaSJeff Roberson ZONE_ASSERT_COLD(zone); 4656d4665eaaSJeff Roberson 46577f746c9fSMateusz Guzik KASSERT(smr != NULL, ("Got NULL smr")); 46587f746c9fSMateusz Guzik KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 46597f746c9fSMateusz Guzik ("zone %p (%s) already uses SMR", zone, zone->uz_name)); 4660d4665eaaSJeff Roberson zone->uz_flags |= UMA_ZONE_SMR; 4661d4665eaaSJeff Roberson zone->uz_smr = smr; 4662d4665eaaSJeff Roberson zone_update_caches(zone); 4663d4665eaaSJeff Roberson } 4664d4665eaaSJeff Roberson 4665d4665eaaSJeff Roberson smr_t 4666d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone) 4667d4665eaaSJeff Roberson { 4668d4665eaaSJeff Roberson 4669d4665eaaSJeff Roberson return (zone->uz_smr); 4670d4665eaaSJeff Roberson } 4671d4665eaaSJeff Roberson 4672d4665eaaSJeff Roberson /* See uma.h */ 4673d4665eaaSJeff Roberson void 46746fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 46756fd34d6fSJeff Roberson { 46766fd34d6fSJeff Roberson uma_keg_t keg; 46776fd34d6fSJeff Roberson 4678bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4679727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 46806fd34d6fSJeff Roberson keg->uk_reserve = items; 46816fd34d6fSJeff Roberson } 46826fd34d6fSJeff Roberson 46836fd34d6fSJeff Roberson /* See uma.h */ 46848355f576SJeff Roberson int 4685a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 46868355f576SJeff Roberson { 4687099a0e58SBosko Milekic uma_keg_t keg; 46888355f576SJeff Roberson vm_offset_t kva; 46899ba30bcbSZbigniew Bodek u_int pages; 46908355f576SJeff Roberson 4691bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4692727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4693727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 46948355f576SJeff Roberson 469579c9f942SJeff Roberson pages = howmany(count, keg->uk_ipers) * keg->uk_ppera; 4696a553d4b8SJeff Roberson 4697a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4698a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4699a4915c21SAttilio Rao #else 4700a4915c21SAttilio Rao if (1) { 4701a4915c21SAttilio Rao #endif 470257223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4703d1f42ac2SAlan Cox if (kva == 0) 47048355f576SJeff Roberson return (0); 4705a4915c21SAttilio Rao } else 4706a4915c21SAttilio Rao kva = 0; 4707bb15d1c7SGleb Smirnoff 4708bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4709099a0e58SBosko Milekic keg->uk_kva = kva; 4710a4915c21SAttilio Rao keg->uk_offset = 0; 4711bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4712a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4713a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4714a4915c21SAttilio Rao #else 4715a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4716a4915c21SAttilio Rao #endif 4717cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4718cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4719cc7ce83aSJeff Roberson zone_update_caches(zone); 4720af526374SJeff Roberson 47218355f576SJeff Roberson return (1); 47228355f576SJeff Roberson } 47238355f576SJeff Roberson 47248355f576SJeff Roberson /* See uma.h */ 47258355f576SJeff Roberson void 47268355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 47278355f576SJeff Roberson { 4728920239efSMark Johnston struct vm_domainset_iter di; 4729ab3185d1SJeff Roberson uma_domain_t dom; 47308355f576SJeff Roberson uma_slab_t slab; 4731099a0e58SBosko Milekic uma_keg_t keg; 473286220393SMark Johnston int aflags, domain, slabs; 47338355f576SJeff Roberson 4734bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 473579c9f942SJeff Roberson slabs = howmany(items, keg->uk_ipers); 4736194a979eSMark Johnston while (slabs-- > 0) { 473786220393SMark Johnston aflags = M_NOWAIT; 473886220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 473986220393SMark Johnston &aflags); 474086220393SMark Johnston for (;;) { 474186220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 474286220393SMark Johnston aflags); 474386220393SMark Johnston if (slab != NULL) { 4744ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 47454ab3aee8SMark Johnston /* 47464ab3aee8SMark Johnston * keg_alloc_slab() always returns a slab on the 47474ab3aee8SMark Johnston * partial list. 47484ab3aee8SMark Johnston */ 47498b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 475086220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 475186220393SMark Johnston us_link); 47524ab3aee8SMark Johnston dom->ud_free_slabs++; 47538b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 4754920239efSMark Johnston break; 47558355f576SJeff Roberson } 47568b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 475786220393SMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 475886220393SMark Johnston } 475986220393SMark Johnston } 476086220393SMark Johnston } 47618355f576SJeff Roberson 4762ed581bf6SJeff Roberson /* 4763ed581bf6SJeff Roberson * Returns a snapshot of memory consumption in bytes. 4764ed581bf6SJeff Roberson */ 4765ed581bf6SJeff Roberson size_t 4766ed581bf6SJeff Roberson uma_zone_memory(uma_zone_t zone) 4767ed581bf6SJeff Roberson { 4768ed581bf6SJeff Roberson size_t sz; 4769ed581bf6SJeff Roberson int i; 4770ed581bf6SJeff Roberson 4771ed581bf6SJeff Roberson sz = 0; 4772ed581bf6SJeff Roberson if (zone->uz_flags & UMA_ZFLAG_CACHE) { 4773ed581bf6SJeff Roberson for (i = 0; i < vm_ndomains; i++) 4774c6fd3e23SJeff Roberson sz += ZDOM_GET(zone, i)->uzd_nitems; 4775ed581bf6SJeff Roberson return (sz * zone->uz_size); 4776ed581bf6SJeff Roberson } 4777ed581bf6SJeff Roberson for (i = 0; i < vm_ndomains; i++) 4778ed581bf6SJeff Roberson sz += zone->uz_keg->uk_domain[i].ud_pages; 4779ed581bf6SJeff Roberson 4780ed581bf6SJeff Roberson return (sz * PAGE_SIZE); 4781ed581bf6SJeff Roberson } 4782ed581bf6SJeff Roberson 47838355f576SJeff Roberson /* See uma.h */ 478408cfa56eSMark Johnston void 478508cfa56eSMark Johnston uma_reclaim(int req) 47868355f576SJeff Roberson { 478744ec2b63SKonstantin Belousov 47881431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 478908cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 479086bbae32SJeff Roberson bucket_enable(); 479108cfa56eSMark Johnston 479208cfa56eSMark Johnston switch (req) { 479308cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 479420a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 479508cfa56eSMark Johnston break; 479608cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 479708cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 479820a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 479908cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 480008cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 480120a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4802a2de44abSAlexander Motin } 480308cfa56eSMark Johnston break; 480408cfa56eSMark Johnston default: 480508cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 480608cfa56eSMark Johnston } 48070f9b7bf3SMark Johnston 48088355f576SJeff Roberson /* 48098355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 48108355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 48118355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 48128355f576SJeff Roberson */ 48139b8db4d0SRyan Libby zone_drain(slabzones[0], NULL); 48149b8db4d0SRyan Libby zone_drain(slabzones[1], NULL); 4815cae33c14SJeff Roberson bucket_zone_drain(); 481608cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 48178355f576SJeff Roberson } 48188355f576SJeff Roberson 48192e47807cSJeff Roberson static volatile int uma_reclaim_needed; 482044ec2b63SKonstantin Belousov 482144ec2b63SKonstantin Belousov void 482244ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 482344ec2b63SKonstantin Belousov { 482444ec2b63SKonstantin Belousov 48252e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 48262e47807cSJeff Roberson wakeup(uma_reclaim); 482744ec2b63SKonstantin Belousov } 482844ec2b63SKonstantin Belousov 482944ec2b63SKonstantin Belousov void 483044ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 483144ec2b63SKonstantin Belousov { 483244ec2b63SKonstantin Belousov 483344ec2b63SKonstantin Belousov for (;;) { 483408cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4835200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 483608cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 48372e47807cSJeff Roberson hz); 483808cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 48399b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 484008cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4841200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 48422e47807cSJeff Roberson /* Don't fire more than once per-second. */ 48432e47807cSJeff Roberson pause("umarclslp", hz); 484444ec2b63SKonstantin Belousov } 484544ec2b63SKonstantin Belousov } 484644ec2b63SKonstantin Belousov 4847663b416fSJohn Baldwin /* See uma.h */ 484808cfa56eSMark Johnston void 484908cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 485008cfa56eSMark Johnston { 485108cfa56eSMark Johnston 485208cfa56eSMark Johnston switch (req) { 485308cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 485420a4e154SJeff Roberson zone_trim(zone, NULL); 485508cfa56eSMark Johnston break; 485608cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 485720a4e154SJeff Roberson zone_drain(zone, NULL); 485808cfa56eSMark Johnston break; 485908cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 486008cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 486120a4e154SJeff Roberson zone_drain(zone, NULL); 486208cfa56eSMark Johnston break; 486308cfa56eSMark Johnston default: 486408cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 486508cfa56eSMark Johnston } 486608cfa56eSMark Johnston } 486708cfa56eSMark Johnston 486808cfa56eSMark Johnston /* See uma.h */ 4869663b416fSJohn Baldwin int 4870663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 4871663b416fSJohn Baldwin { 4872663b416fSJohn Baldwin 4873727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 48746c125b8dSMohan Srinivasan } 48756c125b8dSMohan Srinivasan 48762e47807cSJeff Roberson unsigned long 48772e47807cSJeff Roberson uma_limit(void) 48782e47807cSJeff Roberson { 48792e47807cSJeff Roberson 48802e47807cSJeff Roberson return (uma_kmem_limit); 48812e47807cSJeff Roberson } 48822e47807cSJeff Roberson 48832e47807cSJeff Roberson void 48842e47807cSJeff Roberson uma_set_limit(unsigned long limit) 48852e47807cSJeff Roberson { 48862e47807cSJeff Roberson 48872e47807cSJeff Roberson uma_kmem_limit = limit; 48882e47807cSJeff Roberson } 48892e47807cSJeff Roberson 48902e47807cSJeff Roberson unsigned long 48912e47807cSJeff Roberson uma_size(void) 48922e47807cSJeff Roberson { 48932e47807cSJeff Roberson 4894058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 4895ad5b0f5bSJeff Roberson } 4896ad5b0f5bSJeff Roberson 4897ad5b0f5bSJeff Roberson long 4898ad5b0f5bSJeff Roberson uma_avail(void) 4899ad5b0f5bSJeff Roberson { 4900ad5b0f5bSJeff Roberson 4901058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 49022e47807cSJeff Roberson } 49032e47807cSJeff Roberson 4904a0d4b0aeSRobert Watson #ifdef DDB 49058355f576SJeff Roberson /* 49067a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 49077a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 49087a52a97eSRobert Watson * 49097a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 49107a52a97eSRobert Watson * per-CPU cache statistic. 49117a52a97eSRobert Watson * 49127a52a97eSRobert Watson */ 49137a52a97eSRobert Watson static void 49140f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 4915c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 49167a52a97eSRobert Watson { 49177a52a97eSRobert Watson uma_cache_t cache; 4918c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 49197a52a97eSRobert Watson int cachefree, cpu; 49207a52a97eSRobert Watson 4921c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 49227a52a97eSRobert Watson cachefree = 0; 49233aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 49247a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 4925376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 4926376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 4927376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 4928376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 49297a52a97eSRobert Watson allocs += cache->uc_allocs; 49307a52a97eSRobert Watson frees += cache->uc_frees; 49317a52a97eSRobert Watson } 49322efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 49332efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 4934c6fd3e23SJeff Roberson xdomain += counter_u64_fetch(z->uz_xdomain); 4935bf965959SSean Bruno sleeps += z->uz_sleeps; 49367a52a97eSRobert Watson if (cachefreep != NULL) 49377a52a97eSRobert Watson *cachefreep = cachefree; 49387a52a97eSRobert Watson if (allocsp != NULL) 49397a52a97eSRobert Watson *allocsp = allocs; 49407a52a97eSRobert Watson if (freesp != NULL) 49417a52a97eSRobert Watson *freesp = frees; 4942bf965959SSean Bruno if (sleepsp != NULL) 4943bf965959SSean Bruno *sleepsp = sleeps; 4944c1685086SJeff Roberson if (xdomainp != NULL) 4945c1685086SJeff Roberson *xdomainp = xdomain; 49467a52a97eSRobert Watson } 4947a0d4b0aeSRobert Watson #endif /* DDB */ 49487a52a97eSRobert Watson 49497a52a97eSRobert Watson static int 49507a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 49517a52a97eSRobert Watson { 49527a52a97eSRobert Watson uma_keg_t kz; 49537a52a97eSRobert Watson uma_zone_t z; 49547a52a97eSRobert Watson int count; 49557a52a97eSRobert Watson 49567a52a97eSRobert Watson count = 0; 4957111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 49587a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 49597a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 49607a52a97eSRobert Watson count++; 49617a52a97eSRobert Watson } 4962b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4963b47acb0aSGleb Smirnoff count++; 4964b47acb0aSGleb Smirnoff 4965111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 49667a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 49677a52a97eSRobert Watson } 49687a52a97eSRobert Watson 4969b47acb0aSGleb Smirnoff static void 4970b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 4971b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 4972b47acb0aSGleb Smirnoff { 4973b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 4974b47acb0aSGleb Smirnoff uma_cache_t cache; 4975b47acb0aSGleb Smirnoff int i; 4976b47acb0aSGleb Smirnoff 4977b47acb0aSGleb Smirnoff 4978b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 4979c6fd3e23SJeff Roberson zdom = ZDOM_GET(z, i); 4980b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 4981b47acb0aSGleb Smirnoff } 4982b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 4983b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 4984b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 4985c6fd3e23SJeff Roberson uth->uth_xdomain = counter_u64_fetch(z->uz_xdomain); 4986b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 49871de9724eSMark Johnston 4988b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 4989b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 4990b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 4991b47acb0aSGleb Smirnoff continue; 4992b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 4993376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 4994376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 4995376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 4996b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 4997b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 4998b47acb0aSGleb Smirnoff } 4999b47acb0aSGleb Smirnoff } 5000b47acb0aSGleb Smirnoff 50017a52a97eSRobert Watson static int 50027a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 50037a52a97eSRobert Watson { 50047a52a97eSRobert Watson struct uma_stream_header ush; 50057a52a97eSRobert Watson struct uma_type_header uth; 500663b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 50077a52a97eSRobert Watson struct sbuf sbuf; 50087a52a97eSRobert Watson uma_keg_t kz; 50097a52a97eSRobert Watson uma_zone_t z; 50104bd61e19SJeff Roberson uint64_t items; 50118b987a77SJeff Roberson uint32_t kfree, pages; 50124e657159SMatthew D Fleming int count, error, i; 50137a52a97eSRobert Watson 501400f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 501500f0e671SMatthew D Fleming if (error != 0) 501600f0e671SMatthew D Fleming return (error); 50174e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 50181eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 501963b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 50204e657159SMatthew D Fleming 5021404a593eSMatthew D Fleming count = 0; 5022111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 50237a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 50247a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 50257a52a97eSRobert Watson count++; 50267a52a97eSRobert Watson } 50277a52a97eSRobert Watson 5028b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 5029b47acb0aSGleb Smirnoff count++; 5030b47acb0aSGleb Smirnoff 50317a52a97eSRobert Watson /* 50327a52a97eSRobert Watson * Insert stream header. 50337a52a97eSRobert Watson */ 50347a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 50357a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 5036ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 50377a52a97eSRobert Watson ush.ush_count = count; 50384e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 50397a52a97eSRobert Watson 50407a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 50418b987a77SJeff Roberson kfree = pages = 0; 50428b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 50434ab3aee8SMark Johnston kfree += kz->uk_domain[i].ud_free_items; 50448b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 50458b987a77SJeff Roberson } 50467a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 50477a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 5048cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 50497a52a97eSRobert Watson uth.uth_align = kz->uk_align; 50507a52a97eSRobert Watson uth.uth_size = kz->uk_size; 50517a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 50524bd61e19SJeff Roberson if (z->uz_max_items > 0) { 50534bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 50544bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 5055bb15d1c7SGleb Smirnoff kz->uk_ppera; 50564bd61e19SJeff Roberson } else 50578b987a77SJeff Roberson uth.uth_pages = pages; 5058f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 5059bb15d1c7SGleb Smirnoff kz->uk_ppera; 5060bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 50618b987a77SJeff Roberson uth.uth_keg_free = kfree; 5062cbbb4a00SRobert Watson 5063cbbb4a00SRobert Watson /* 5064cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 5065cbbb4a00SRobert Watson * on the keg's zone list. 5066cbbb4a00SRobert Watson */ 5067e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 5068cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 5069cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 5070b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 5071b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 507263b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 507363b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 507463b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 50757a52a97eSRobert Watson } 50767a52a97eSRobert Watson } 5077b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 5078b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 5079b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 5080b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 5081b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 5082b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 5083b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 5084b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 5085b47acb0aSGleb Smirnoff } 5086b47acb0aSGleb Smirnoff 5087111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 50884e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 50894e657159SMatthew D Fleming sbuf_delete(&sbuf); 509063b5d112SKonstantin Belousov free(ups, M_TEMP); 50917a52a97eSRobert Watson return (error); 50927a52a97eSRobert Watson } 509348c5777eSRobert Watson 50940a5a3ccbSGleb Smirnoff int 50950a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 50960a5a3ccbSGleb Smirnoff { 50970a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 509816be9f54SGleb Smirnoff int error, max; 50990a5a3ccbSGleb Smirnoff 510016be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 51010a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 51020a5a3ccbSGleb Smirnoff if (error || !req->newptr) 51030a5a3ccbSGleb Smirnoff return (error); 51040a5a3ccbSGleb Smirnoff 51050a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 51060a5a3ccbSGleb Smirnoff 51070a5a3ccbSGleb Smirnoff return (0); 51080a5a3ccbSGleb Smirnoff } 51090a5a3ccbSGleb Smirnoff 51100a5a3ccbSGleb Smirnoff int 51110a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 51120a5a3ccbSGleb Smirnoff { 511320a4e154SJeff Roberson uma_zone_t zone; 51140a5a3ccbSGleb Smirnoff int cur; 51150a5a3ccbSGleb Smirnoff 511620a4e154SJeff Roberson /* 511720a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 511820a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 511920a4e154SJeff Roberson */ 512020a4e154SJeff Roberson if (arg2 == 0) 512120a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 512220a4e154SJeff Roberson else 512320a4e154SJeff Roberson zone = arg1; 51240a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 51250a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 51260a5a3ccbSGleb Smirnoff } 51270a5a3ccbSGleb Smirnoff 512820a4e154SJeff Roberson static int 512920a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 513020a4e154SJeff Roberson { 513120a4e154SJeff Roberson uma_zone_t zone = arg1; 513220a4e154SJeff Roberson uint64_t cur; 513320a4e154SJeff Roberson 513420a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 513520a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 513620a4e154SJeff Roberson } 513720a4e154SJeff Roberson 513820a4e154SJeff Roberson static int 513920a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 514020a4e154SJeff Roberson { 514120a4e154SJeff Roberson uma_zone_t zone = arg1; 514220a4e154SJeff Roberson uint64_t cur; 514320a4e154SJeff Roberson 514420a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 514520a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 514620a4e154SJeff Roberson } 514720a4e154SJeff Roberson 51486d204a6aSRyan Libby static int 51496d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 51506d204a6aSRyan Libby { 51516d204a6aSRyan Libby struct sbuf sbuf; 51526d204a6aSRyan Libby uma_zone_t zone = arg1; 51536d204a6aSRyan Libby int error; 51546d204a6aSRyan Libby 51556d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 51566d204a6aSRyan Libby if (zone->uz_flags != 0) 51576d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 51586d204a6aSRyan Libby else 51596d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 51606d204a6aSRyan Libby error = sbuf_finish(&sbuf); 51616d204a6aSRyan Libby sbuf_delete(&sbuf); 51626d204a6aSRyan Libby 51636d204a6aSRyan Libby return (error); 51646d204a6aSRyan Libby } 51656d204a6aSRyan Libby 5166f7af5015SRyan Libby static int 5167f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 5168f7af5015SRyan Libby { 5169f7af5015SRyan Libby uma_keg_t keg = arg1; 5170f7af5015SRyan Libby int avail, effpct, total; 5171f7af5015SRyan Libby 5172f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 517354c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 51749b8db4d0SRyan Libby total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize; 5175f7af5015SRyan Libby /* 5176f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 5177f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 5178f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 5179f7af5015SRyan Libby */ 5180f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 5181f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 5182f7af5015SRyan Libby avail *= mp_maxid + 1; 5183f7af5015SRyan Libby effpct = 100 * avail / total; 5184f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 5185f7af5015SRyan Libby } 5186f7af5015SRyan Libby 51874bd61e19SJeff Roberson static int 51884bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 51894bd61e19SJeff Roberson { 51904bd61e19SJeff Roberson uma_zone_t zone = arg1; 51914bd61e19SJeff Roberson uint64_t cur; 51924bd61e19SJeff Roberson 51934bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 51944bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 51954bd61e19SJeff Roberson } 51964bd61e19SJeff Roberson 51979542ea7bSGleb Smirnoff #ifdef INVARIANTS 51989542ea7bSGleb Smirnoff static uma_slab_t 51999542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 52009542ea7bSGleb Smirnoff { 52019542ea7bSGleb Smirnoff uma_slab_t slab; 52029542ea7bSGleb Smirnoff uma_keg_t keg; 52039542ea7bSGleb Smirnoff uint8_t *mem; 52049542ea7bSGleb Smirnoff 52059542ea7bSGleb Smirnoff /* 52069542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 52079542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 52089542ea7bSGleb Smirnoff * essentially holds a reference. 52099542ea7bSGleb Smirnoff */ 5210727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 5211727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5212bb15d1c7SGleb Smirnoff return (NULL); 521354c5ae80SRyan Libby if (zone->uz_flags & UMA_ZFLAG_VTOSLAB) 5214727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 5215bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 521654c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0) 5217727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 52188b987a77SJeff Roberson KEG_LOCK(keg, 0); 52199542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 52208b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 52219542ea7bSGleb Smirnoff 52229542ea7bSGleb Smirnoff return (slab); 52239542ea7bSGleb Smirnoff } 52249542ea7bSGleb Smirnoff 5225c5deaf04SGleb Smirnoff static bool 5226c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 5227c5deaf04SGleb Smirnoff { 5228c5deaf04SGleb Smirnoff 5229727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5230c5deaf04SGleb Smirnoff return (true); 5231c5deaf04SGleb Smirnoff 5232bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 5233c5deaf04SGleb Smirnoff } 5234c5deaf04SGleb Smirnoff 5235c5deaf04SGleb Smirnoff static bool 5236c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 5237c5deaf04SGleb Smirnoff { 5238c5deaf04SGleb Smirnoff uintptr_t idx; 5239c5deaf04SGleb Smirnoff 5240c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 5241c5deaf04SGleb Smirnoff return (true); 5242c5deaf04SGleb Smirnoff 5243c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 5244c5deaf04SGleb Smirnoff return (false); 5245c5deaf04SGleb Smirnoff 5246c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 5247c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 5248c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 5249c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 5250c5deaf04SGleb Smirnoff } 5251c5deaf04SGleb Smirnoff 5252c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 5253c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 5254c5deaf04SGleb Smirnoff return (true); 5255c5deaf04SGleb Smirnoff } 5256c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 5257c5deaf04SGleb Smirnoff 5258c5deaf04SGleb Smirnoff return (false); 5259c5deaf04SGleb Smirnoff } 5260c5deaf04SGleb Smirnoff 52619542ea7bSGleb Smirnoff /* 52629542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 52639542ea7bSGleb Smirnoff * 52649542ea7bSGleb Smirnoff */ 52659542ea7bSGleb Smirnoff static void 52669542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 52679542ea7bSGleb Smirnoff { 52689542ea7bSGleb Smirnoff uma_keg_t keg; 52699542ea7bSGleb Smirnoff int freei; 52709542ea7bSGleb Smirnoff 52719542ea7bSGleb Smirnoff if (slab == NULL) { 52729542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 52739542ea7bSGleb Smirnoff if (slab == NULL) 52749542ea7bSGleb Smirnoff panic("uma: item %p did not belong to zone %s\n", 52759542ea7bSGleb Smirnoff item, zone->uz_name); 52769542ea7bSGleb Smirnoff } 5277584061b4SJeff Roberson keg = zone->uz_keg; 52781e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 52799542ea7bSGleb Smirnoff 5280815db204SRyan Libby if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 52819542ea7bSGleb Smirnoff panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n", 52829542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 5283815db204SRyan Libby BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 52849542ea7bSGleb Smirnoff } 52859542ea7bSGleb Smirnoff 52869542ea7bSGleb Smirnoff /* 52879542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 52889542ea7bSGleb Smirnoff * and duplicate frees. 52899542ea7bSGleb Smirnoff * 52909542ea7bSGleb Smirnoff */ 52919542ea7bSGleb Smirnoff static void 52929542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 52939542ea7bSGleb Smirnoff { 52949542ea7bSGleb Smirnoff uma_keg_t keg; 52959542ea7bSGleb Smirnoff int freei; 52969542ea7bSGleb Smirnoff 52979542ea7bSGleb Smirnoff if (slab == NULL) { 52989542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 52999542ea7bSGleb Smirnoff if (slab == NULL) 53009542ea7bSGleb Smirnoff panic("uma: Freed item %p did not belong to zone %s\n", 53019542ea7bSGleb Smirnoff item, zone->uz_name); 53029542ea7bSGleb Smirnoff } 5303584061b4SJeff Roberson keg = zone->uz_keg; 53041e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 53059542ea7bSGleb Smirnoff 53069542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 53079542ea7bSGleb Smirnoff panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n", 53089542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 53099542ea7bSGleb Smirnoff 53101e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 53119542ea7bSGleb Smirnoff panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n", 53129542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 53139542ea7bSGleb Smirnoff 5314815db204SRyan Libby if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 53159542ea7bSGleb Smirnoff panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n", 53169542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 53179542ea7bSGleb Smirnoff 5318815db204SRyan Libby BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 53199542ea7bSGleb Smirnoff } 53209542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 53219542ea7bSGleb Smirnoff 532248c5777eSRobert Watson #ifdef DDB 532346d70077SConrad Meyer static int64_t 532446d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 53250223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 532648c5777eSRobert Watson { 532746d70077SConrad Meyer uint64_t frees; 53280f9b7bf3SMark Johnston int i; 532948c5777eSRobert Watson 533048c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 533146d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 53322efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 533346d70077SConrad Meyer *sleeps = z->uz_sleeps; 533446d70077SConrad Meyer *cachefree = 0; 533546d70077SConrad Meyer *xdomain = 0; 533648c5777eSRobert Watson } else 533746d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 533846d70077SConrad Meyer xdomain); 53398b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 5340c6fd3e23SJeff Roberson *cachefree += ZDOM_GET(z, i)->uzd_nitems; 5341e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 534248c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 53434ab3aee8SMark Johnston *cachefree += kz->uk_domain[i].ud_free_items; 53448b987a77SJeff Roberson } 534546d70077SConrad Meyer *used = *allocs - frees; 534646d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 534746d70077SConrad Meyer } 53480f9b7bf3SMark Johnston 534946d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 535046d70077SConrad Meyer { 535146d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 535246d70077SConrad Meyer uma_keg_t kz; 535346d70077SConrad Meyer uma_zone_t z; 535446d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 535546d70077SConrad Meyer long cachefree; 535646d70077SConrad Meyer /* variables for sorting */ 535746d70077SConrad Meyer uma_keg_t cur_keg; 535846d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 535946d70077SConrad Meyer int64_t cur_size, last_size, size; 536046d70077SConrad Meyer int ties; 536146d70077SConrad Meyer 536246d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 536346d70077SConrad Meyer if (modif[0] == 'i') { 536446d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 536546d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 536646d70077SConrad Meyer } else { 536746d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 536846d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 536946d70077SConrad Meyer } 537046d70077SConrad Meyer 537146d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 537246d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 537346d70077SConrad Meyer 537446d70077SConrad Meyer /* Sort the zones with largest size first. */ 537546d70077SConrad Meyer last_zone = NULL; 537646d70077SConrad Meyer last_size = INT64_MAX; 537746d70077SConrad Meyer for (;;) { 537846d70077SConrad Meyer cur_zone = NULL; 537946d70077SConrad Meyer cur_size = -1; 538046d70077SConrad Meyer ties = 0; 538146d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 538246d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 538346d70077SConrad Meyer /* 538446d70077SConrad Meyer * In the case of size ties, print out zones 538546d70077SConrad Meyer * in the order they are encountered. That is, 538646d70077SConrad Meyer * when we encounter the most recently output 538746d70077SConrad Meyer * zone, we have already printed all preceding 538846d70077SConrad Meyer * ties, and we must print all following ties. 538946d70077SConrad Meyer */ 539046d70077SConrad Meyer if (z == last_zone) { 539146d70077SConrad Meyer ties = 1; 539246d70077SConrad Meyer continue; 539346d70077SConrad Meyer } 539446d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 539546d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 539646d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 539746d70077SConrad Meyer { 539846d70077SConrad Meyer cur_size = size; 539946d70077SConrad Meyer cur_zone = z; 540046d70077SConrad Meyer cur_keg = kz; 540146d70077SConrad Meyer } 540246d70077SConrad Meyer } 540346d70077SConrad Meyer } 540446d70077SConrad Meyer if (cur_zone == NULL) 540546d70077SConrad Meyer break; 540646d70077SConrad Meyer 540746d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 540846d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 540946d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 541046d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 541146d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 541220a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 541320a4e154SJeff Roberson xdomain); 541446d70077SConrad Meyer 5415687c94aaSJohn Baldwin if (db_pager_quit) 5416687c94aaSJohn Baldwin return; 541746d70077SConrad Meyer last_zone = cur_zone; 541846d70077SConrad Meyer last_size = cur_size; 541948c5777eSRobert Watson } 542048c5777eSRobert Watson } 542103175483SAlexander Motin 542203175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 542303175483SAlexander Motin { 542403175483SAlexander Motin uma_zone_t z; 5425ab3185d1SJeff Roberson uint64_t allocs, frees; 54260f9b7bf3SMark Johnston long cachefree; 54270f9b7bf3SMark Johnston int i; 542803175483SAlexander Motin 542903175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 543003175483SAlexander Motin "Requests", "Bucket"); 543103175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 5432c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 54330f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 5434c6fd3e23SJeff Roberson cachefree += ZDOM_GET(z, i)->uzd_nitems; 54350f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 543603175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 543703175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 543820a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 543903175483SAlexander Motin if (db_pager_quit) 544003175483SAlexander Motin return; 544103175483SAlexander Motin } 544203175483SAlexander Motin } 54439542ea7bSGleb Smirnoff #endif /* DDB */ 5444