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> 856f3b523cSKonstantin Belousov #include <vm/vm_param.h> 86194a979eSMark Johnston #include <vm/vm_domainset.h> 878355f576SJeff Roberson #include <vm/vm_object.h> 888355f576SJeff Roberson #include <vm/vm_page.h> 89a4915c21SAttilio Rao #include <vm/vm_pageout.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> 956f3b523cSKonstantin Belousov #include <vm/vm_dumpset.h> 968355f576SJeff Roberson #include <vm/uma.h> 978355f576SJeff Roberson #include <vm/uma_int.h> 98639c9550SJeff Roberson #include <vm/uma_dbg.h> 998355f576SJeff Roberson 10048c5777eSRobert Watson #include <ddb/ddb.h> 10148c5777eSRobert Watson 1028d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 1038d689e04SGleb Smirnoff #include <vm/memguard.h> 1048d689e04SGleb Smirnoff #endif 1058d689e04SGleb Smirnoff 106a81c400eSJeff Roberson #include <machine/md_var.h> 107a81c400eSJeff Roberson 108d4665eaaSJeff Roberson #ifdef INVARIANTS 109d4665eaaSJeff Roberson #define UMA_ALWAYS_CTORDTOR 1 110d4665eaaSJeff Roberson #else 111d4665eaaSJeff Roberson #define UMA_ALWAYS_CTORDTOR 0 112d4665eaaSJeff Roberson #endif 113d4665eaaSJeff Roberson 1148355f576SJeff Roberson /* 115ab3185d1SJeff Roberson * This is the zone and keg from which all zones are spawned. 1168355f576SJeff Roberson */ 117ab3185d1SJeff Roberson static uma_zone_t kegs; 118ab3185d1SJeff Roberson static uma_zone_t zones; 1198355f576SJeff Roberson 1209b8db4d0SRyan Libby /* 12154007ce8SMark Johnston * On INVARIANTS builds, the slab contains a second bitset of the same size, 12254007ce8SMark Johnston * "dbg_bits", which is laid out immediately after us_free. 12354007ce8SMark Johnston */ 12454007ce8SMark Johnston #ifdef INVARIANTS 12554007ce8SMark Johnston #define SLAB_BITSETS 2 12654007ce8SMark Johnston #else 12754007ce8SMark Johnston #define SLAB_BITSETS 1 12854007ce8SMark Johnston #endif 12954007ce8SMark Johnston 13054007ce8SMark Johnston /* 1319b8db4d0SRyan Libby * These are the two zones from which all offpage uma_slab_ts are allocated. 1329b8db4d0SRyan Libby * 1339b8db4d0SRyan Libby * One zone is for slab headers that can represent a larger number of items, 1349b8db4d0SRyan Libby * making the slabs themselves more efficient, and the other zone is for 1359b8db4d0SRyan Libby * headers that are smaller and represent fewer items, making the headers more 1369b8db4d0SRyan Libby * efficient. 1379b8db4d0SRyan Libby */ 1389b8db4d0SRyan Libby #define SLABZONE_SIZE(setsize) \ 1399b8db4d0SRyan Libby (sizeof(struct uma_hash_slab) + BITSET_SIZE(setsize) * SLAB_BITSETS) 1409b8db4d0SRyan Libby #define SLABZONE0_SETSIZE (PAGE_SIZE / 16) 1419b8db4d0SRyan Libby #define SLABZONE1_SETSIZE SLAB_MAX_SETSIZE 1429b8db4d0SRyan Libby #define SLABZONE0_SIZE SLABZONE_SIZE(SLABZONE0_SETSIZE) 1439b8db4d0SRyan Libby #define SLABZONE1_SIZE SLABZONE_SIZE(SLABZONE1_SETSIZE) 1449b8db4d0SRyan Libby static uma_zone_t slabzones[2]; 1458355f576SJeff Roberson 1468355f576SJeff Roberson /* 1478355f576SJeff Roberson * The initial hash tables come out of this zone so they can be allocated 1488355f576SJeff Roberson * prior to malloc coming up. 1498355f576SJeff Roberson */ 1508355f576SJeff Roberson static uma_zone_t hashzone; 1518355f576SJeff Roberson 1521e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */ 153e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1; 1541e319f6dSRobert Watson 155961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets"); 15620a4e154SJeff Roberson static MALLOC_DEFINE(M_UMA, "UMA", "UMA Misc"); 157961647dfSJeff Roberson 1588355f576SJeff Roberson /* 15986bbae32SJeff Roberson * Are we allowed to allocate buckets? 16086bbae32SJeff Roberson */ 16186bbae32SJeff Roberson static int bucketdisable = 1; 16286bbae32SJeff Roberson 163099a0e58SBosko Milekic /* Linked list of all kegs in the system */ 16413e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs); 1658355f576SJeff Roberson 16603175483SAlexander Motin /* Linked list of all cache-only zones in the system */ 16703175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones = 16803175483SAlexander Motin LIST_HEAD_INITIALIZER(uma_cachezones); 16903175483SAlexander Motin 170111fbcd5SBryan Venteicher /* This RW lock protects the keg list */ 171fe933c1dSMateusz Guzik static struct rwlock_padalign __exclusive_cache_line uma_rwlock; 1728355f576SJeff Roberson 173ac0a6fd0SGleb Smirnoff /* 174a81c400eSJeff Roberson * First available virual address for boot time allocations. 175ac0a6fd0SGleb Smirnoff */ 176a81c400eSJeff Roberson static vm_offset_t bootstart; 177a81c400eSJeff Roberson static vm_offset_t bootmem; 1788355f576SJeff Roberson 17908cfa56eSMark Johnston static struct sx uma_reclaim_lock; 18095c4bf75SKonstantin Belousov 181fbd95859SMark Johnston /* 182fbd95859SMark Johnston * kmem soft limit, initialized by uma_set_limit(). Ensure that early 183fbd95859SMark Johnston * allocations don't trigger a wakeup of the reclaim thread. 184fbd95859SMark Johnston */ 1856d6a03d7SJeff Roberson unsigned long uma_kmem_limit = LONG_MAX; 186fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_limit, CTLFLAG_RD, &uma_kmem_limit, 0, 187fbd95859SMark Johnston "UMA kernel memory soft limit"); 1886d6a03d7SJeff Roberson unsigned long uma_kmem_total; 189fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_total, CTLFLAG_RD, &uma_kmem_total, 0, 190fbd95859SMark Johnston "UMA kernel memory usage"); 1912e47807cSJeff Roberson 1928355f576SJeff Roberson /* Is the VM done starting up? */ 193860bb7a0SMark Johnston static enum { 194860bb7a0SMark Johnston BOOT_COLD, 195a81c400eSJeff Roberson BOOT_KVA, 196dc2b3205SMark Johnston BOOT_PCPU, 197860bb7a0SMark Johnston BOOT_RUNNING, 198860bb7a0SMark Johnston BOOT_SHUTDOWN, 199860bb7a0SMark Johnston } booted = BOOT_COLD; 2008355f576SJeff Roberson 201ef72505eSJeff Roberson /* 2029643769aSJeff Roberson * This is the handle used to schedule events that need to happen 2039643769aSJeff Roberson * outside of the allocation fast path. 2049643769aSJeff Roberson */ 2058355f576SJeff Roberson static struct callout uma_callout; 2069643769aSJeff Roberson #define UMA_TIMEOUT 20 /* Seconds for callout interval. */ 2078355f576SJeff Roberson 2088355f576SJeff Roberson /* 2098355f576SJeff Roberson * This structure is passed as the zone ctor arg so that I don't have to create 2108355f576SJeff Roberson * a special allocation function just for zones. 2118355f576SJeff Roberson */ 2128355f576SJeff Roberson struct uma_zctor_args { 213bb196eb4SMatthew D Fleming const char *name; 214c3bdc05fSAndrew R. Reiter size_t size; 2158355f576SJeff Roberson uma_ctor ctor; 2168355f576SJeff Roberson uma_dtor dtor; 2178355f576SJeff Roberson uma_init uminit; 2188355f576SJeff Roberson uma_fini fini; 2190095a784SJeff Roberson uma_import import; 2200095a784SJeff Roberson uma_release release; 2210095a784SJeff Roberson void *arg; 222099a0e58SBosko Milekic uma_keg_t keg; 223099a0e58SBosko Milekic int align; 22485dcf349SGleb Smirnoff uint32_t flags; 225099a0e58SBosko Milekic }; 226099a0e58SBosko Milekic 227099a0e58SBosko Milekic struct uma_kctor_args { 228099a0e58SBosko Milekic uma_zone_t zone; 229099a0e58SBosko Milekic size_t size; 230099a0e58SBosko Milekic uma_init uminit; 231099a0e58SBosko Milekic uma_fini fini; 2328355f576SJeff Roberson int align; 23385dcf349SGleb Smirnoff uint32_t flags; 2348355f576SJeff Roberson }; 2358355f576SJeff Roberson 236cae33c14SJeff Roberson struct uma_bucket_zone { 237cae33c14SJeff Roberson uma_zone_t ubz_zone; 238eaa17d42SRyan Libby const char *ubz_name; 239fc03d22bSJeff Roberson int ubz_entries; /* Number of items it can hold. */ 240fc03d22bSJeff Roberson int ubz_maxsize; /* Maximum allocation size per-item. */ 241cae33c14SJeff Roberson }; 242cae33c14SJeff Roberson 243f9d27e75SRobert Watson /* 244fc03d22bSJeff Roberson * Compute the actual number of bucket entries to pack them in power 245fc03d22bSJeff Roberson * of two sizes for more efficient space utilization. 246f9d27e75SRobert Watson */ 247fc03d22bSJeff Roberson #define BUCKET_SIZE(n) \ 248fc03d22bSJeff Roberson (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *)) 249fc03d22bSJeff Roberson 2501aa6c758SAlexander Motin #define BUCKET_MAX BUCKET_SIZE(256) 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 441fc03d22bSJeff Roberson static int 442fc03d22bSJeff Roberson bucket_select(int size) 443fc03d22bSJeff Roberson { 444fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 445fc03d22bSJeff Roberson 446fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 447fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 448fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 449fc03d22bSJeff Roberson 450fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 451fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 452fc03d22bSJeff Roberson break; 453fc03d22bSJeff Roberson ubz--; 454fc03d22bSJeff Roberson return (ubz->ubz_entries); 455dc2c7965SRobert Watson } 456dc2c7965SRobert Watson 457cae33c14SJeff Roberson static uma_bucket_t 4586fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags) 459cae33c14SJeff Roberson { 460cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 461cae33c14SJeff Roberson uma_bucket_t bucket; 462cae33c14SJeff Roberson 463cae33c14SJeff Roberson /* 464d4665eaaSJeff Roberson * Don't allocate buckets early in boot. 465cae33c14SJeff Roberson */ 466d4665eaaSJeff Roberson if (__predict_false(booted < BOOT_KVA)) 467cae33c14SJeff Roberson return (NULL); 468a81c400eSJeff Roberson 4696fd34d6fSJeff Roberson /* 4706fd34d6fSJeff Roberson * To limit bucket recursion we store the original zone flags 4716fd34d6fSJeff Roberson * in a cookie passed via zalloc_arg/zfree_arg. This allows the 4726fd34d6fSJeff Roberson * NOVM flag to persist even through deep recursions. We also 4736fd34d6fSJeff Roberson * store ZFLAG_BUCKET once we have recursed attempting to allocate 4746fd34d6fSJeff Roberson * a bucket for a bucket zone so we do not allow infinite bucket 4756fd34d6fSJeff Roberson * recursion. This cookie will even persist to frees of unused 4766fd34d6fSJeff Roberson * buckets via the allocation path or bucket allocations in the 4776fd34d6fSJeff Roberson * free path. 4786fd34d6fSJeff Roberson */ 4796fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 4806fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 481e8a720feSAlexander Motin else { 482e8a720feSAlexander Motin if ((uintptr_t)udata & UMA_ZFLAG_BUCKET) 483e8a720feSAlexander Motin return (NULL); 4846fd34d6fSJeff Roberson udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET); 485e8a720feSAlexander Motin } 486bae55c4aSRyan Libby if (((uintptr_t)udata & UMA_ZONE_VM) != 0) 487af526374SJeff Roberson flags |= M_NOVM; 488f8b6c515SMark Johnston ubz = bucket_zone_lookup(atomic_load_16(&zone->uz_bucket_size)); 48920d3ab87SAlexander Motin if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0) 49020d3ab87SAlexander Motin ubz++; 4916fd34d6fSJeff Roberson bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags); 492cae33c14SJeff Roberson if (bucket) { 493cae33c14SJeff Roberson #ifdef INVARIANTS 494cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 495cae33c14SJeff Roberson #endif 496cae33c14SJeff Roberson bucket->ub_cnt = 0; 497f8b6c515SMark Johnston bucket->ub_entries = min(ubz->ubz_entries, 498f8b6c515SMark Johnston zone->uz_bucket_size_max); 499d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 500d4665eaaSJeff Roberson CTR3(KTR_UMA, "bucket_alloc: zone %s(%p) allocated bucket %p", 501d4665eaaSJeff Roberson zone->uz_name, zone, bucket); 502cae33c14SJeff Roberson } 503cae33c14SJeff Roberson 504cae33c14SJeff Roberson return (bucket); 505cae33c14SJeff Roberson } 506cae33c14SJeff Roberson 507cae33c14SJeff Roberson static void 5086fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata) 509cae33c14SJeff Roberson { 510cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 511cae33c14SJeff Roberson 512c6fd3e23SJeff Roberson if (bucket->ub_cnt != 0) 513c6fd3e23SJeff Roberson bucket_drain(zone, bucket); 514c6fd3e23SJeff Roberson 515fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 516fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 517d4665eaaSJeff Roberson KASSERT(bucket->ub_seq == SMR_SEQ_INVALID, 518d4665eaaSJeff Roberson ("bucket_free: Freeing an SMR bucket.")); 5196fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 5206fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 521dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 5226fd34d6fSJeff Roberson uma_zfree_arg(ubz->ubz_zone, bucket, udata); 523cae33c14SJeff Roberson } 524cae33c14SJeff Roberson 525cae33c14SJeff Roberson static void 526cae33c14SJeff Roberson bucket_zone_drain(void) 527cae33c14SJeff Roberson { 528cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 529cae33c14SJeff Roberson 530cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 53108cfa56eSMark Johnston uma_zone_reclaim(ubz->ubz_zone, UMA_RECLAIM_DRAIN); 532cae33c14SJeff Roberson } 533cae33c14SJeff Roberson 53408cfa56eSMark Johnston /* 535c6fd3e23SJeff Roberson * Acquire the domain lock and record contention. 536c6fd3e23SJeff Roberson */ 537c6fd3e23SJeff Roberson static uma_zone_domain_t 538c6fd3e23SJeff Roberson zone_domain_lock(uma_zone_t zone, int domain) 539c6fd3e23SJeff Roberson { 540c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 541c6fd3e23SJeff Roberson bool lockfail; 542c6fd3e23SJeff Roberson 543c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 544c6fd3e23SJeff Roberson lockfail = false; 545c6fd3e23SJeff Roberson if (ZDOM_OWNED(zdom)) 546c6fd3e23SJeff Roberson lockfail = true; 547c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 548c6fd3e23SJeff Roberson /* This is unsynchronized. The counter does not need to be precise. */ 549c6fd3e23SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 550c6fd3e23SJeff Roberson zone->uz_bucket_size++; 551c6fd3e23SJeff Roberson return (zdom); 552c6fd3e23SJeff Roberson } 553c6fd3e23SJeff Roberson 554c6fd3e23SJeff Roberson /* 555fe835cbfSJeff Roberson * Search for the domain with the least cached items and return it if it 556fe835cbfSJeff Roberson * is out of balance with the preferred domain. 557c6fd3e23SJeff Roberson */ 558c6fd3e23SJeff Roberson static __noinline int 559c6fd3e23SJeff Roberson zone_domain_lowest(uma_zone_t zone, int pref) 560c6fd3e23SJeff Roberson { 561fe835cbfSJeff Roberson long least, nitems, prefitems; 562c6fd3e23SJeff Roberson int domain; 563c6fd3e23SJeff Roberson int i; 564c6fd3e23SJeff Roberson 565fe835cbfSJeff Roberson prefitems = least = LONG_MAX; 566c6fd3e23SJeff Roberson domain = 0; 567c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 568c6fd3e23SJeff Roberson nitems = ZDOM_GET(zone, i)->uzd_nitems; 569c6fd3e23SJeff Roberson if (nitems < least) { 570c6fd3e23SJeff Roberson domain = i; 571c6fd3e23SJeff Roberson least = nitems; 572c6fd3e23SJeff Roberson } 573fe835cbfSJeff Roberson if (domain == pref) 574fe835cbfSJeff Roberson prefitems = nitems; 575fe835cbfSJeff Roberson } 576fe835cbfSJeff Roberson if (prefitems < least * 2) 577fe835cbfSJeff Roberson return (pref); 578c6fd3e23SJeff Roberson 579c6fd3e23SJeff Roberson return (domain); 580c6fd3e23SJeff Roberson } 581c6fd3e23SJeff Roberson 582c6fd3e23SJeff Roberson /* 583c6fd3e23SJeff Roberson * Search for the domain with the most cached items and return it or the 584c6fd3e23SJeff Roberson * preferred domain if it has enough to proceed. 585c6fd3e23SJeff Roberson */ 586c6fd3e23SJeff Roberson static __noinline int 587c6fd3e23SJeff Roberson zone_domain_highest(uma_zone_t zone, int pref) 588c6fd3e23SJeff Roberson { 589c6fd3e23SJeff Roberson long most, nitems; 590c6fd3e23SJeff Roberson int domain; 591c6fd3e23SJeff Roberson int i; 592c6fd3e23SJeff Roberson 593c6fd3e23SJeff Roberson if (ZDOM_GET(zone, pref)->uzd_nitems > BUCKET_MAX) 594c6fd3e23SJeff Roberson return (pref); 595c6fd3e23SJeff Roberson 596c6fd3e23SJeff Roberson most = 0; 597c6fd3e23SJeff Roberson domain = 0; 598c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 599c6fd3e23SJeff Roberson nitems = ZDOM_GET(zone, i)->uzd_nitems; 600c6fd3e23SJeff Roberson if (nitems > most) { 601c6fd3e23SJeff Roberson domain = i; 602c6fd3e23SJeff Roberson most = nitems; 603c6fd3e23SJeff Roberson } 604c6fd3e23SJeff Roberson } 605c6fd3e23SJeff Roberson 606c6fd3e23SJeff Roberson return (domain); 607c6fd3e23SJeff Roberson } 608c6fd3e23SJeff Roberson 609c6fd3e23SJeff Roberson /* 610c6fd3e23SJeff Roberson * Safely subtract cnt from imax. 611c6fd3e23SJeff Roberson */ 612c6fd3e23SJeff Roberson static void 613c6fd3e23SJeff Roberson zone_domain_imax_sub(uma_zone_domain_t zdom, int cnt) 614c6fd3e23SJeff Roberson { 615c6fd3e23SJeff Roberson long new; 616c6fd3e23SJeff Roberson long old; 617c6fd3e23SJeff Roberson 618c6fd3e23SJeff Roberson old = zdom->uzd_imax; 619c6fd3e23SJeff Roberson do { 620c6fd3e23SJeff Roberson if (old <= cnt) 621c6fd3e23SJeff Roberson new = 0; 622c6fd3e23SJeff Roberson else 623c6fd3e23SJeff Roberson new = old - cnt; 624c6fd3e23SJeff Roberson } while (atomic_fcmpset_long(&zdom->uzd_imax, &old, new) == 0); 625c6fd3e23SJeff Roberson } 626c6fd3e23SJeff Roberson 627c6fd3e23SJeff Roberson /* 628c6fd3e23SJeff Roberson * Set the maximum imax value. 629c6fd3e23SJeff Roberson */ 630c6fd3e23SJeff Roberson static void 631c6fd3e23SJeff Roberson zone_domain_imax_set(uma_zone_domain_t zdom, int nitems) 632c6fd3e23SJeff Roberson { 633c6fd3e23SJeff Roberson long old; 634c6fd3e23SJeff Roberson 635c6fd3e23SJeff Roberson old = zdom->uzd_imax; 636c6fd3e23SJeff Roberson do { 637c6fd3e23SJeff Roberson if (old >= nitems) 638c6fd3e23SJeff Roberson break; 639c6fd3e23SJeff Roberson } while (atomic_fcmpset_long(&zdom->uzd_imax, &old, nitems) == 0); 640c6fd3e23SJeff Roberson } 641c6fd3e23SJeff Roberson 642c6fd3e23SJeff Roberson /* 64308cfa56eSMark Johnston * Attempt to satisfy an allocation by retrieving a full bucket from one of the 644d4665eaaSJeff Roberson * zone's caches. If a bucket is found the zone is not locked on return. 64508cfa56eSMark Johnston */ 6460f9b7bf3SMark Johnston static uma_bucket_t 647c6fd3e23SJeff Roberson zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom, bool reclaim) 6480f9b7bf3SMark Johnston { 6490f9b7bf3SMark Johnston uma_bucket_t bucket; 650d4665eaaSJeff Roberson int i; 651d4665eaaSJeff Roberson bool dtor = false; 6520f9b7bf3SMark Johnston 653c6fd3e23SJeff Roberson ZDOM_LOCK_ASSERT(zdom); 6540f9b7bf3SMark Johnston 655dc3915c8SJeff Roberson if ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) == NULL) 656d4665eaaSJeff Roberson return (NULL); 657d4665eaaSJeff Roberson 658543117beSJeff Roberson /* SMR Buckets can not be re-used until readers expire. */ 659d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 660d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 661d4665eaaSJeff Roberson if (!smr_poll(zone->uz_smr, bucket->ub_seq, false)) 662d4665eaaSJeff Roberson return (NULL); 663d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 664543117beSJeff Roberson dtor = (zone->uz_dtor != NULL) || UMA_ALWAYS_CTORDTOR; 665c6fd3e23SJeff Roberson if (STAILQ_NEXT(bucket, ub_link) != NULL) 666c6fd3e23SJeff Roberson zdom->uzd_seq = STAILQ_NEXT(bucket, ub_link)->ub_seq; 667d4665eaaSJeff Roberson } 668dc3915c8SJeff Roberson STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link); 66906d8bdcbSMark Johnston 67006d8bdcbSMark Johnston KASSERT(zdom->uzd_nitems >= bucket->ub_cnt, 67106d8bdcbSMark Johnston ("%s: item count underflow (%ld, %d)", 67206d8bdcbSMark Johnston __func__, zdom->uzd_nitems, bucket->ub_cnt)); 67306d8bdcbSMark Johnston KASSERT(bucket->ub_cnt > 0, 67406d8bdcbSMark Johnston ("%s: empty bucket in bucket cache", __func__)); 6750f9b7bf3SMark Johnston zdom->uzd_nitems -= bucket->ub_cnt; 676c6fd3e23SJeff Roberson 677c6fd3e23SJeff Roberson /* 678c6fd3e23SJeff Roberson * Shift the bounds of the current WSS interval to avoid 679c6fd3e23SJeff Roberson * perturbing the estimate. 680c6fd3e23SJeff Roberson */ 681c6fd3e23SJeff Roberson if (reclaim) { 682c6fd3e23SJeff Roberson zdom->uzd_imin -= lmin(zdom->uzd_imin, bucket->ub_cnt); 683c6fd3e23SJeff Roberson zone_domain_imax_sub(zdom, bucket->ub_cnt); 684c6fd3e23SJeff Roberson } else if (zdom->uzd_imin > zdom->uzd_nitems) 6850f9b7bf3SMark Johnston zdom->uzd_imin = zdom->uzd_nitems; 686c6fd3e23SJeff Roberson 687c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 688d4665eaaSJeff Roberson if (dtor) 689d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 690d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], zone->uz_size, 691d4665eaaSJeff Roberson NULL, SKIP_NONE); 692d4665eaaSJeff Roberson 6930f9b7bf3SMark Johnston return (bucket); 6940f9b7bf3SMark Johnston } 6950f9b7bf3SMark Johnston 69608cfa56eSMark Johnston /* 69708cfa56eSMark Johnston * Insert a full bucket into the specified cache. The "ws" parameter indicates 69808cfa56eSMark Johnston * whether the bucket's contents should be counted as part of the zone's working 699c6fd3e23SJeff Roberson * set. The bucket may be freed if it exceeds the bucket limit. 70008cfa56eSMark Johnston */ 7010f9b7bf3SMark Johnston static void 702c6fd3e23SJeff Roberson zone_put_bucket(uma_zone_t zone, int domain, uma_bucket_t bucket, void *udata, 7030f9b7bf3SMark Johnston const bool ws) 7040f9b7bf3SMark Johnston { 705c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 7060f9b7bf3SMark Johnston 707c6fd3e23SJeff Roberson /* We don't cache empty buckets. This can happen after a reclaim. */ 708c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 709c6fd3e23SJeff Roberson goto out; 710c6fd3e23SJeff Roberson zdom = zone_domain_lock(zone, domain); 711c6fd3e23SJeff Roberson 712c6fd3e23SJeff Roberson /* 713c6fd3e23SJeff Roberson * Conditionally set the maximum number of items. 714c6fd3e23SJeff Roberson */ 7150f9b7bf3SMark Johnston zdom->uzd_nitems += bucket->ub_cnt; 716c6fd3e23SJeff Roberson if (__predict_true(zdom->uzd_nitems < zone->uz_bucket_max)) { 717c6fd3e23SJeff Roberson if (ws) 718c6fd3e23SJeff Roberson zone_domain_imax_set(zdom, zdom->uzd_nitems); 719c6fd3e23SJeff Roberson if (STAILQ_EMPTY(&zdom->uzd_buckets)) 720c6fd3e23SJeff Roberson zdom->uzd_seq = bucket->ub_seq; 7215afdf5c1SMark Johnston 7225afdf5c1SMark Johnston /* 7235afdf5c1SMark Johnston * Try to promote reuse of recently used items. For items 7245afdf5c1SMark Johnston * protected by SMR, try to defer reuse to minimize polling. 7255afdf5c1SMark Johnston */ 7265afdf5c1SMark Johnston if (bucket->ub_seq == SMR_SEQ_INVALID) 7275afdf5c1SMark Johnston STAILQ_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link); 7285afdf5c1SMark Johnston else 729c6fd3e23SJeff Roberson STAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link); 730c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 731c6fd3e23SJeff Roberson return; 732c6fd3e23SJeff Roberson } 733c6fd3e23SJeff Roberson zdom->uzd_nitems -= bucket->ub_cnt; 734c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 735c6fd3e23SJeff Roberson out: 736c6fd3e23SJeff Roberson bucket_free(zone, bucket, udata); 7370f9b7bf3SMark Johnston } 7380f9b7bf3SMark Johnston 739376b1ba3SJeff Roberson /* Pops an item out of a per-cpu cache bucket. */ 740376b1ba3SJeff Roberson static inline void * 741376b1ba3SJeff Roberson cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket) 742376b1ba3SJeff Roberson { 743376b1ba3SJeff Roberson void *item; 744376b1ba3SJeff Roberson 745376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 746376b1ba3SJeff Roberson 747376b1ba3SJeff Roberson bucket->ucb_cnt--; 748376b1ba3SJeff Roberson item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt]; 749376b1ba3SJeff Roberson #ifdef INVARIANTS 750376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL; 751376b1ba3SJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 752376b1ba3SJeff Roberson #endif 753376b1ba3SJeff Roberson cache->uc_allocs++; 754376b1ba3SJeff Roberson 755376b1ba3SJeff Roberson return (item); 756376b1ba3SJeff Roberson } 757376b1ba3SJeff Roberson 758376b1ba3SJeff Roberson /* Pushes an item into a per-cpu cache bucket. */ 759376b1ba3SJeff Roberson static inline void 760376b1ba3SJeff Roberson cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item) 761376b1ba3SJeff Roberson { 762376b1ba3SJeff Roberson 763376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 764376b1ba3SJeff Roberson KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL, 765376b1ba3SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 766376b1ba3SJeff Roberson 767376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item; 768376b1ba3SJeff Roberson bucket->ucb_cnt++; 769376b1ba3SJeff Roberson cache->uc_frees++; 770376b1ba3SJeff Roberson } 771376b1ba3SJeff Roberson 772376b1ba3SJeff Roberson /* 773376b1ba3SJeff Roberson * Unload a UMA bucket from a per-cpu cache. 774376b1ba3SJeff Roberson */ 775376b1ba3SJeff Roberson static inline uma_bucket_t 776376b1ba3SJeff Roberson cache_bucket_unload(uma_cache_bucket_t bucket) 777376b1ba3SJeff Roberson { 778376b1ba3SJeff Roberson uma_bucket_t b; 779376b1ba3SJeff Roberson 780376b1ba3SJeff Roberson b = bucket->ucb_bucket; 781376b1ba3SJeff Roberson if (b != NULL) { 782376b1ba3SJeff Roberson MPASS(b->ub_entries == bucket->ucb_entries); 783376b1ba3SJeff Roberson b->ub_cnt = bucket->ucb_cnt; 784376b1ba3SJeff Roberson bucket->ucb_bucket = NULL; 785376b1ba3SJeff Roberson bucket->ucb_entries = bucket->ucb_cnt = 0; 786376b1ba3SJeff Roberson } 787376b1ba3SJeff Roberson 788376b1ba3SJeff Roberson return (b); 789376b1ba3SJeff Roberson } 790376b1ba3SJeff Roberson 791376b1ba3SJeff Roberson static inline uma_bucket_t 792376b1ba3SJeff Roberson cache_bucket_unload_alloc(uma_cache_t cache) 793376b1ba3SJeff Roberson { 794376b1ba3SJeff Roberson 795376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_allocbucket)); 796376b1ba3SJeff Roberson } 797376b1ba3SJeff Roberson 798376b1ba3SJeff Roberson static inline uma_bucket_t 799376b1ba3SJeff Roberson cache_bucket_unload_free(uma_cache_t cache) 800376b1ba3SJeff Roberson { 801376b1ba3SJeff Roberson 802376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_freebucket)); 803376b1ba3SJeff Roberson } 804376b1ba3SJeff Roberson 805376b1ba3SJeff Roberson static inline uma_bucket_t 806376b1ba3SJeff Roberson cache_bucket_unload_cross(uma_cache_t cache) 807376b1ba3SJeff Roberson { 808376b1ba3SJeff Roberson 809376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_crossbucket)); 810376b1ba3SJeff Roberson } 811376b1ba3SJeff Roberson 812376b1ba3SJeff Roberson /* 813376b1ba3SJeff Roberson * Load a bucket into a per-cpu cache bucket. 814376b1ba3SJeff Roberson */ 815376b1ba3SJeff Roberson static inline void 816376b1ba3SJeff Roberson cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b) 817376b1ba3SJeff Roberson { 818376b1ba3SJeff Roberson 819376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 820376b1ba3SJeff Roberson MPASS(bucket->ucb_bucket == NULL); 821543117beSJeff Roberson MPASS(b->ub_seq == SMR_SEQ_INVALID); 822376b1ba3SJeff Roberson 823376b1ba3SJeff Roberson bucket->ucb_bucket = b; 824376b1ba3SJeff Roberson bucket->ucb_cnt = b->ub_cnt; 825376b1ba3SJeff Roberson bucket->ucb_entries = b->ub_entries; 826376b1ba3SJeff Roberson } 827376b1ba3SJeff Roberson 828376b1ba3SJeff Roberson static inline void 829376b1ba3SJeff Roberson cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b) 830376b1ba3SJeff Roberson { 831376b1ba3SJeff Roberson 832376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_allocbucket, b); 833376b1ba3SJeff Roberson } 834376b1ba3SJeff Roberson 835376b1ba3SJeff Roberson static inline void 836376b1ba3SJeff Roberson cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b) 837376b1ba3SJeff Roberson { 838376b1ba3SJeff Roberson 839376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_freebucket, b); 840376b1ba3SJeff Roberson } 841376b1ba3SJeff Roberson 842dfe13344SJeff Roberson #ifdef NUMA 843376b1ba3SJeff Roberson static inline void 844376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b) 845376b1ba3SJeff Roberson { 846376b1ba3SJeff Roberson 847376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_crossbucket, b); 848376b1ba3SJeff Roberson } 849376b1ba3SJeff Roberson #endif 850376b1ba3SJeff Roberson 851376b1ba3SJeff Roberson /* 852376b1ba3SJeff Roberson * Copy and preserve ucb_spare. 853376b1ba3SJeff Roberson */ 854376b1ba3SJeff Roberson static inline void 855376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 856376b1ba3SJeff Roberson { 857376b1ba3SJeff Roberson 858376b1ba3SJeff Roberson b1->ucb_bucket = b2->ucb_bucket; 859376b1ba3SJeff Roberson b1->ucb_entries = b2->ucb_entries; 860376b1ba3SJeff Roberson b1->ucb_cnt = b2->ucb_cnt; 861376b1ba3SJeff Roberson } 862376b1ba3SJeff Roberson 863376b1ba3SJeff Roberson /* 864376b1ba3SJeff Roberson * Swap two cache buckets. 865376b1ba3SJeff Roberson */ 866376b1ba3SJeff Roberson static inline void 867376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 868376b1ba3SJeff Roberson { 869376b1ba3SJeff Roberson struct uma_cache_bucket b3; 870376b1ba3SJeff Roberson 871376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 872376b1ba3SJeff Roberson 873376b1ba3SJeff Roberson cache_bucket_copy(&b3, b1); 874376b1ba3SJeff Roberson cache_bucket_copy(b1, b2); 875376b1ba3SJeff Roberson cache_bucket_copy(b2, &b3); 876376b1ba3SJeff Roberson } 877376b1ba3SJeff Roberson 878c6fd3e23SJeff Roberson /* 879c6fd3e23SJeff Roberson * Attempt to fetch a bucket from a zone on behalf of the current cpu cache. 880c6fd3e23SJeff Roberson */ 881c6fd3e23SJeff Roberson static uma_bucket_t 882c6fd3e23SJeff Roberson cache_fetch_bucket(uma_zone_t zone, uma_cache_t cache, int domain) 883c6fd3e23SJeff Roberson { 884c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 885c6fd3e23SJeff Roberson uma_bucket_t bucket; 886c6fd3e23SJeff Roberson 887c6fd3e23SJeff Roberson /* 888c6fd3e23SJeff Roberson * Avoid the lock if possible. 889c6fd3e23SJeff Roberson */ 890c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 891c6fd3e23SJeff Roberson if (zdom->uzd_nitems == 0) 892c6fd3e23SJeff Roberson return (NULL); 893c6fd3e23SJeff Roberson 894c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_SMR) != 0 && 895c6fd3e23SJeff Roberson !smr_poll(zone->uz_smr, zdom->uzd_seq, false)) 896c6fd3e23SJeff Roberson return (NULL); 897c6fd3e23SJeff Roberson 898c6fd3e23SJeff Roberson /* 899c6fd3e23SJeff Roberson * Check the zone's cache of buckets. 900c6fd3e23SJeff Roberson */ 901c6fd3e23SJeff Roberson zdom = zone_domain_lock(zone, domain); 90206d8bdcbSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) 903c6fd3e23SJeff Roberson return (bucket); 904c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 905c6fd3e23SJeff Roberson 906c6fd3e23SJeff Roberson return (NULL); 907c6fd3e23SJeff Roberson } 908c6fd3e23SJeff Roberson 9092f891cd5SPawel Jakub Dawidek static void 9102f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 9112f891cd5SPawel Jakub Dawidek { 9122f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 9132f891cd5SPawel Jakub Dawidek 9142f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 9152f891cd5SPawel Jakub Dawidek return; 9162f891cd5SPawel Jakub Dawidek 9172f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 9182f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 9192f891cd5SPawel Jakub Dawidek } 9202f891cd5SPawel Jakub Dawidek 92154503a13SJonathan T. Looney static inline void 92254503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone) 92354503a13SJonathan T. Looney { 924e60b2fcbSGleb Smirnoff 925e60b2fcbSGleb Smirnoff if (zone->uz_maxaction.ta_func != NULL) 926e60b2fcbSGleb Smirnoff taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction); 92754503a13SJonathan T. Looney } 92854503a13SJonathan T. Looney 9298355f576SJeff Roberson /* 9308355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 9319643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 9328355f576SJeff Roberson * 9338355f576SJeff Roberson * Arguments: 9348355f576SJeff Roberson * arg Unused 9358355f576SJeff Roberson * 9368355f576SJeff Roberson * Returns: 9378355f576SJeff Roberson * Nothing 9388355f576SJeff Roberson */ 9398355f576SJeff Roberson static void 9408355f576SJeff Roberson uma_timeout(void *unused) 9418355f576SJeff Roberson { 94286bbae32SJeff Roberson bucket_enable(); 94320a4e154SJeff Roberson zone_foreach(zone_timeout, NULL); 9448355f576SJeff Roberson 9458355f576SJeff Roberson /* Reschedule this event */ 9469643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 9478355f576SJeff Roberson } 9488355f576SJeff Roberson 9498355f576SJeff Roberson /* 9500f9b7bf3SMark Johnston * Update the working set size estimate for the zone's bucket cache. 9510f9b7bf3SMark Johnston * The constants chosen here are somewhat arbitrary. With an update period of 9520f9b7bf3SMark Johnston * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the 9530f9b7bf3SMark Johnston * last 100s. 9540f9b7bf3SMark Johnston */ 9550f9b7bf3SMark Johnston static void 9560f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom) 9570f9b7bf3SMark Johnston { 9580f9b7bf3SMark Johnston long wss; 9590f9b7bf3SMark Johnston 960c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 9610f9b7bf3SMark Johnston MPASS(zdom->uzd_imax >= zdom->uzd_imin); 9620f9b7bf3SMark Johnston wss = zdom->uzd_imax - zdom->uzd_imin; 9630f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems; 96408cfa56eSMark Johnston zdom->uzd_wss = (4 * wss + zdom->uzd_wss) / 5; 965c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 9660f9b7bf3SMark Johnston } 9670f9b7bf3SMark Johnston 9680f9b7bf3SMark Johnston /* 9699643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 9709643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 9718355f576SJeff Roberson * 972e20a199fSJeff Roberson * Returns nothing. 9738355f576SJeff Roberson */ 9748355f576SJeff Roberson static void 97520a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused) 9768355f576SJeff Roberson { 97708034d10SKonstantin Belousov uma_keg_t keg; 9788b987a77SJeff Roberson u_int slabs, pages; 9798355f576SJeff Roberson 98054c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 98108034d10SKonstantin Belousov goto update_wss; 98208034d10SKonstantin Belousov 98308034d10SKonstantin Belousov keg = zone->uz_keg; 9848b987a77SJeff Roberson 9858b987a77SJeff Roberson /* 9868b987a77SJeff Roberson * Hash zones are non-numa by definition so the first domain 9878b987a77SJeff Roberson * is the only one present. 9888b987a77SJeff Roberson */ 9898b987a77SJeff Roberson KEG_LOCK(keg, 0); 9908b987a77SJeff Roberson pages = keg->uk_domain[0].ud_pages; 9918b987a77SJeff Roberson 9928355f576SJeff Roberson /* 993e20a199fSJeff Roberson * Expand the keg hash table. 9948355f576SJeff Roberson * 9958355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 9968355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 9978355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 9988355f576SJeff Roberson */ 9998b987a77SJeff Roberson if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) { 10000aef6126SJeff Roberson struct uma_hash newhash; 10010aef6126SJeff Roberson struct uma_hash oldhash; 10020aef6126SJeff Roberson int ret; 10035300d9ddSJeff Roberson 10040aef6126SJeff Roberson /* 10050aef6126SJeff Roberson * This is so involved because allocating and freeing 1006e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 10070aef6126SJeff Roberson * I have to do everything in stages and check for 10080aef6126SJeff Roberson * races. 10090aef6126SJeff Roberson */ 10108b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 10113b2f2cb8SAlexander Motin ret = hash_alloc(&newhash, 1 << fls(slabs)); 10128b987a77SJeff Roberson KEG_LOCK(keg, 0); 10130aef6126SJeff Roberson if (ret) { 1014099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 1015099a0e58SBosko Milekic oldhash = keg->uk_hash; 1016099a0e58SBosko Milekic keg->uk_hash = newhash; 10170aef6126SJeff Roberson } else 10180aef6126SJeff Roberson oldhash = newhash; 10190aef6126SJeff Roberson 10208b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 10210aef6126SJeff Roberson hash_free(&oldhash); 10228b987a77SJeff Roberson goto update_wss; 10230aef6126SJeff Roberson } 10245300d9ddSJeff Roberson } 10258b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 1026e20a199fSJeff Roberson 102708034d10SKonstantin Belousov update_wss: 1028bb15d1c7SGleb Smirnoff for (int i = 0; i < vm_ndomains; i++) 1029c6fd3e23SJeff Roberson zone_domain_update_wss(ZDOM_GET(zone, i)); 10308355f576SJeff Roberson } 10318355f576SJeff Roberson 10328355f576SJeff Roberson /* 10335300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 10345300d9ddSJeff Roberson * backing store. 10355300d9ddSJeff Roberson * 10365300d9ddSJeff Roberson * Arguments: 10370aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 10385300d9ddSJeff Roberson * 10395300d9ddSJeff Roberson * Returns: 1040763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 10415300d9ddSJeff Roberson */ 104237c84183SPoul-Henning Kamp static int 10433b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 10445300d9ddSJeff Roberson { 104559568a0eSAlexander Motin size_t alloc; 10465300d9ddSJeff Roberson 10473b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 10483b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 10493b2f2cb8SAlexander Motin hash->uh_hashsize = size; 10500aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 10511e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 10525300d9ddSJeff Roberson } else { 10530aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 1054e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 1055ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 10560aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 10575300d9ddSJeff Roberson } 10580aef6126SJeff Roberson if (hash->uh_slab_hash) { 10590aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 10600aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 10610aef6126SJeff Roberson return (1); 10620aef6126SJeff Roberson } 10635300d9ddSJeff Roberson 10640aef6126SJeff Roberson return (0); 10655300d9ddSJeff Roberson } 10665300d9ddSJeff Roberson 10675300d9ddSJeff Roberson /* 106864f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 106964f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 107064f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 10718355f576SJeff Roberson * 10728355f576SJeff Roberson * Arguments: 10730aef6126SJeff Roberson * oldhash The hash you want to expand 10740aef6126SJeff Roberson * newhash The hash structure for the new table 10758355f576SJeff Roberson * 10768355f576SJeff Roberson * Returns: 10778355f576SJeff Roberson * Nothing 10788355f576SJeff Roberson * 10798355f576SJeff Roberson * Discussion: 10808355f576SJeff Roberson */ 10810aef6126SJeff Roberson static int 10820aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 10838355f576SJeff Roberson { 10841e0701e1SJeff Roberson uma_hash_slab_t slab; 10856929b7d1SPedro F. Giffuni u_int hval; 10866929b7d1SPedro F. Giffuni u_int idx; 10878355f576SJeff Roberson 10880aef6126SJeff Roberson if (!newhash->uh_slab_hash) 10890aef6126SJeff Roberson return (0); 10908355f576SJeff Roberson 10910aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 10920aef6126SJeff Roberson return (0); 10938355f576SJeff Roberson 10948355f576SJeff Roberson /* 10958355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 10968355f576SJeff Roberson * full rehash. 10978355f576SJeff Roberson */ 10988355f576SJeff Roberson 10996929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 11001e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 11011e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 11021e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 11031e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 11041e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 11051e0701e1SJeff Roberson slab, uhs_hlink); 11068355f576SJeff Roberson } 11078355f576SJeff Roberson 11080aef6126SJeff Roberson return (1); 11099c2cd7e5SJeff Roberson } 11109c2cd7e5SJeff Roberson 11115300d9ddSJeff Roberson /* 11125300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 11135300d9ddSJeff Roberson * 11145300d9ddSJeff Roberson * Arguments: 11155300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 11165300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 11175300d9ddSJeff Roberson * 11185300d9ddSJeff Roberson * Returns: 11195300d9ddSJeff Roberson * Nothing 11205300d9ddSJeff Roberson */ 11219c2cd7e5SJeff Roberson static void 11220aef6126SJeff Roberson hash_free(struct uma_hash *hash) 11239c2cd7e5SJeff Roberson { 11240aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 11250aef6126SJeff Roberson return; 11260aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 11270095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 11288355f576SJeff Roberson else 1129961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 11308355f576SJeff Roberson } 11318355f576SJeff Roberson 11328355f576SJeff Roberson /* 11338355f576SJeff Roberson * Frees all outstanding items in a bucket 11348355f576SJeff Roberson * 11358355f576SJeff Roberson * Arguments: 11368355f576SJeff Roberson * zone The zone to free to, must be unlocked. 11374bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 11388355f576SJeff Roberson * 11398355f576SJeff Roberson * Returns: 11408355f576SJeff Roberson * Nothing 11418355f576SJeff Roberson */ 11428355f576SJeff Roberson static void 11438355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 11448355f576SJeff Roberson { 11450095a784SJeff Roberson int i; 11468355f576SJeff Roberson 1147c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 11488355f576SJeff Roberson return; 11498355f576SJeff Roberson 1150d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 1151d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 1152d4665eaaSJeff Roberson smr_wait(zone->uz_smr, bucket->ub_seq); 1153543117beSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 1154d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 1155d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], 1156d4665eaaSJeff Roberson zone->uz_size, NULL, SKIP_NONE); 1157d4665eaaSJeff Roberson } 11580095a784SJeff Roberson if (zone->uz_fini) 11590095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 11600095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 11610095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 11624bd61e19SJeff Roberson if (zone->uz_max_items > 0) 11634bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 1164d4665eaaSJeff Roberson #ifdef INVARIANTS 1165d4665eaaSJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt); 1166d4665eaaSJeff Roberson #endif 11670095a784SJeff Roberson bucket->ub_cnt = 0; 11688355f576SJeff Roberson } 11698355f576SJeff Roberson 11708355f576SJeff Roberson /* 11718355f576SJeff Roberson * Drains the per cpu caches for a zone. 11728355f576SJeff Roberson * 1173727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 11745d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 11755d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 11765d1ae027SRobert Watson * 11778355f576SJeff Roberson * Arguments: 11788355f576SJeff Roberson * zone The zone to drain, must be unlocked. 11798355f576SJeff Roberson * 11808355f576SJeff Roberson * Returns: 11818355f576SJeff Roberson * Nothing 11828355f576SJeff Roberson */ 11838355f576SJeff Roberson static void 11849643769aSJeff Roberson cache_drain(uma_zone_t zone) 11858355f576SJeff Roberson { 11868355f576SJeff Roberson uma_cache_t cache; 1187376b1ba3SJeff Roberson uma_bucket_t bucket; 1188543117beSJeff Roberson smr_seq_t seq; 11898355f576SJeff Roberson int cpu; 11908355f576SJeff Roberson 11918355f576SJeff Roberson /* 11925d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 11935d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 11945d1ae027SRobert Watson * of the caches at this point. 11955d1ae027SRobert Watson * 11965d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 11975d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 11988355f576SJeff Roberson */ 1199543117beSJeff Roberson seq = SMR_SEQ_INVALID; 1200543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 1201226dd6dbSJeff Roberson seq = smr_advance(zone->uz_smr); 12023aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 12038355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 1204376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 1205c6fd3e23SJeff Roberson if (bucket != NULL) 1206376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1207376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 1208376b1ba3SJeff Roberson if (bucket != NULL) { 1209543117beSJeff Roberson bucket->ub_seq = seq; 1210376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1211376b1ba3SJeff Roberson } 1212376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 1213376b1ba3SJeff Roberson if (bucket != NULL) { 1214543117beSJeff Roberson bucket->ub_seq = seq; 1215376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1216376b1ba3SJeff Roberson } 1217d56368d7SBosko Milekic } 121808cfa56eSMark Johnston bucket_cache_reclaim(zone, true); 1219aaa8bb16SJeff Roberson } 1220aaa8bb16SJeff Roberson 1221a2de44abSAlexander Motin static void 122220a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 1223a2de44abSAlexander Motin { 1224a2de44abSAlexander Motin 1225a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1226a2de44abSAlexander Motin return; 1227a2de44abSAlexander Motin 122820a4e154SJeff Roberson zone->uz_bucket_size = 122920a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 1230a2de44abSAlexander Motin } 1231a2de44abSAlexander Motin 1232a2de44abSAlexander Motin static void 123320a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 1234a2de44abSAlexander Motin { 1235a2de44abSAlexander Motin uma_cache_t cache; 1236c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 1237ab3185d1SJeff Roberson int domain; 1238a2de44abSAlexander Motin 1239a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1240a2de44abSAlexander Motin return; 1241a2de44abSAlexander Motin 1242c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1243a2de44abSAlexander Motin critical_enter(); 1244a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1245c6fd3e23SJeff Roberson domain = PCPU_GET(domain); 1246376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1247d4665eaaSJeff Roberson 1248d4665eaaSJeff Roberson /* 1249d4665eaaSJeff Roberson * Don't flush SMR zone buckets. This leaves the zone without a 1250d4665eaaSJeff Roberson * bucket and forces every free to synchronize(). 1251d4665eaaSJeff Roberson */ 1252543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0) { 1253376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1254543117beSJeff Roberson b3 = cache_bucket_unload_cross(cache); 1255543117beSJeff Roberson } 1256543117beSJeff Roberson critical_exit(); 1257543117beSJeff Roberson 1258543117beSJeff Roberson if (b1 != NULL) 1259c6fd3e23SJeff Roberson zone_free_bucket(zone, b1, NULL, domain, false); 1260543117beSJeff Roberson if (b2 != NULL) 1261c6fd3e23SJeff Roberson zone_free_bucket(zone, b2, NULL, domain, false); 1262543117beSJeff Roberson if (b3 != NULL) { 1263c6fd3e23SJeff Roberson /* Adjust the domain so it goes to zone_free_cross. */ 1264c6fd3e23SJeff Roberson domain = (domain + 1) % vm_ndomains; 1265c6fd3e23SJeff Roberson zone_free_bucket(zone, b3, NULL, domain, false); 1266c1685086SJeff Roberson } 1267a2de44abSAlexander Motin } 1268a2de44abSAlexander Motin 1269a2de44abSAlexander Motin /* 1270a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1271a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1272a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1273a2de44abSAlexander Motin * to safely access their cache buckets. 1274a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1275a2de44abSAlexander Motin */ 1276a2de44abSAlexander Motin static void 127708cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1278a2de44abSAlexander Motin { 1279a2de44abSAlexander Motin int cpu; 1280a2de44abSAlexander Motin 1281a2de44abSAlexander Motin /* 1282727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1283a2de44abSAlexander Motin */ 1284a2de44abSAlexander Motin if (zone) 128520a4e154SJeff Roberson cache_shrink(zone, NULL); 1286a2de44abSAlexander Motin else 128720a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1288a2de44abSAlexander Motin 1289a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1290a2de44abSAlexander Motin thread_lock(curthread); 1291a2de44abSAlexander Motin sched_bind(curthread, cpu); 1292a2de44abSAlexander Motin thread_unlock(curthread); 1293a2de44abSAlexander Motin 1294a2de44abSAlexander Motin if (zone) 129520a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1296a2de44abSAlexander Motin else 129720a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1298a2de44abSAlexander Motin } 1299a2de44abSAlexander Motin thread_lock(curthread); 1300a2de44abSAlexander Motin sched_unbind(curthread); 1301a2de44abSAlexander Motin thread_unlock(curthread); 1302a2de44abSAlexander Motin } 1303a2de44abSAlexander Motin 1304aaa8bb16SJeff Roberson /* 130508cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 130608cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 130708cfa56eSMark Johnston * estimated working set size. 1308aaa8bb16SJeff Roberson */ 1309aaa8bb16SJeff Roberson static void 131008cfa56eSMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain) 1311aaa8bb16SJeff Roberson { 1312ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1313aaa8bb16SJeff Roberson uma_bucket_t bucket; 1314c6fd3e23SJeff Roberson long target; 1315ab3185d1SJeff Roberson int i; 13168355f576SJeff Roberson 1317c6fd3e23SJeff Roberson /* 1318c6fd3e23SJeff Roberson * Shrink the zone bucket size to ensure that the per-CPU caches 1319c6fd3e23SJeff Roberson * don't grow too large. 1320c6fd3e23SJeff Roberson */ 1321c6fd3e23SJeff Roberson if (zone->uz_bucket_size > zone->uz_bucket_size_min) 1322c6fd3e23SJeff Roberson zone->uz_bucket_size--; 1323c6fd3e23SJeff Roberson 1324ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 132591d947bfSJeff Roberson /* 132691d947bfSJeff Roberson * The cross bucket is partially filled and not part of 132791d947bfSJeff Roberson * the item count. Reclaim it individually here. 132891d947bfSJeff Roberson */ 1329c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 1330226dd6dbSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 || drain) { 133191d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 133291d947bfSJeff Roberson bucket = zdom->uzd_cross; 133391d947bfSJeff Roberson zdom->uzd_cross = NULL; 133491d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 1335c6fd3e23SJeff Roberson if (bucket != NULL) 133691d947bfSJeff Roberson bucket_free(zone, bucket, NULL); 133791d947bfSJeff Roberson } 133891d947bfSJeff Roberson 133991d947bfSJeff Roberson /* 134008cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 134108cfa56eSMark Johnston * this bucket cache is empty. Otherwise, we reclaim items in 134208cfa56eSMark Johnston * excess of the zone's estimated working set size. If the 134308cfa56eSMark Johnston * difference nitems - imin is larger than the WSS estimate, 134408cfa56eSMark Johnston * then the estimate will grow at the end of this interval and 134508cfa56eSMark Johnston * we ignore the historical average. 134608cfa56eSMark Johnston */ 1347c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 134808cfa56eSMark Johnston target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems - 134908cfa56eSMark Johnston zdom->uzd_imin); 135008cfa56eSMark Johnston while (zdom->uzd_nitems > target) { 1351c6fd3e23SJeff Roberson bucket = zone_fetch_bucket(zone, zdom, true); 135208cfa56eSMark Johnston if (bucket == NULL) 135308cfa56eSMark Johnston break; 13546fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 1355c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 13568355f576SJeff Roberson } 1357c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 1358ab3185d1SJeff Roberson } 13598355f576SJeff Roberson } 1360fc03d22bSJeff Roberson 1361fc03d22bSJeff Roberson static void 1362fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1363fc03d22bSJeff Roberson { 1364fc03d22bSJeff Roberson uint8_t *mem; 1365fc03d22bSJeff Roberson int i; 1366fc03d22bSJeff Roberson uint8_t flags; 1367fc03d22bSJeff Roberson 13681431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 13691431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 13701431a748SGleb Smirnoff 13711e0701e1SJeff Roberson mem = slab_data(slab, keg); 1372fc03d22bSJeff Roberson flags = slab->us_flags; 1373fc03d22bSJeff Roberson i = start; 1374fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 1375fc03d22bSJeff Roberson for (i--; i > -1; i--) 1376c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1377c5deaf04SGleb Smirnoff /* 1378c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1379c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1380c5deaf04SGleb Smirnoff * which executed trash_dtor. 1381c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1382c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1383c5deaf04SGleb Smirnoff * invocations. 1384c5deaf04SGleb Smirnoff */ 13851e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1386c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1387c5deaf04SGleb Smirnoff #endif 13881e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1389fc03d22bSJeff Roberson } 139054c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 13919b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab), 13929b8db4d0SRyan Libby NULL, SKIP_NONE); 1393fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 13942e47807cSJeff Roberson uma_total_dec(PAGE_SIZE * keg->uk_ppera); 13958355f576SJeff Roberson } 13968355f576SJeff Roberson 1397f09cbea3SMark Johnston static void 1398f09cbea3SMark Johnston keg_drain_domain(uma_keg_t keg, int domain) 1399f09cbea3SMark Johnston { 1400f09cbea3SMark Johnston struct slabhead freeslabs; 1401f09cbea3SMark Johnston uma_domain_t dom; 1402f09cbea3SMark Johnston uma_slab_t slab, tmp; 1403f09cbea3SMark Johnston uint32_t i, stofree, stokeep, partial; 1404f09cbea3SMark Johnston 1405f09cbea3SMark Johnston dom = &keg->uk_domain[domain]; 1406f09cbea3SMark Johnston LIST_INIT(&freeslabs); 1407f09cbea3SMark Johnston 1408f09cbea3SMark Johnston CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u", 1409575a4437SEd Maste keg->uk_name, keg, domain, dom->ud_free_items); 1410f09cbea3SMark Johnston 1411f09cbea3SMark Johnston KEG_LOCK(keg, domain); 1412f09cbea3SMark Johnston 1413f09cbea3SMark Johnston /* 1414f09cbea3SMark Johnston * Are the free items in partially allocated slabs sufficient to meet 1415f09cbea3SMark Johnston * the reserve? If not, compute the number of fully free slabs that must 1416f09cbea3SMark Johnston * be kept. 1417f09cbea3SMark Johnston */ 1418f09cbea3SMark Johnston partial = dom->ud_free_items - dom->ud_free_slabs * keg->uk_ipers; 1419f09cbea3SMark Johnston if (partial < keg->uk_reserve) { 1420f09cbea3SMark Johnston stokeep = min(dom->ud_free_slabs, 1421f09cbea3SMark Johnston howmany(keg->uk_reserve - partial, keg->uk_ipers)); 1422f09cbea3SMark Johnston } else { 1423f09cbea3SMark Johnston stokeep = 0; 1424f09cbea3SMark Johnston } 1425f09cbea3SMark Johnston stofree = dom->ud_free_slabs - stokeep; 1426f09cbea3SMark Johnston 1427f09cbea3SMark Johnston /* 1428f09cbea3SMark Johnston * Partition the free slabs into two sets: those that must be kept in 1429f09cbea3SMark Johnston * order to maintain the reserve, and those that may be released back to 1430f09cbea3SMark Johnston * the system. Since one set may be much larger than the other, 1431f09cbea3SMark Johnston * populate the smaller of the two sets and swap them if necessary. 1432f09cbea3SMark Johnston */ 1433f09cbea3SMark Johnston for (i = min(stofree, stokeep); i > 0; i--) { 1434f09cbea3SMark Johnston slab = LIST_FIRST(&dom->ud_free_slab); 1435f09cbea3SMark Johnston LIST_REMOVE(slab, us_link); 1436f09cbea3SMark Johnston LIST_INSERT_HEAD(&freeslabs, slab, us_link); 1437f09cbea3SMark Johnston } 1438f09cbea3SMark Johnston if (stofree > stokeep) 1439f09cbea3SMark Johnston LIST_SWAP(&freeslabs, &dom->ud_free_slab, uma_slab, us_link); 1440f09cbea3SMark Johnston 1441f09cbea3SMark Johnston if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) { 1442f09cbea3SMark Johnston LIST_FOREACH(slab, &freeslabs, us_link) 1443f09cbea3SMark Johnston UMA_HASH_REMOVE(&keg->uk_hash, slab); 1444f09cbea3SMark Johnston } 1445f09cbea3SMark Johnston dom->ud_free_items -= stofree * keg->uk_ipers; 1446f09cbea3SMark Johnston dom->ud_free_slabs -= stofree; 1447f09cbea3SMark Johnston dom->ud_pages -= stofree * keg->uk_ppera; 1448f09cbea3SMark Johnston KEG_UNLOCK(keg, domain); 1449f09cbea3SMark Johnston 1450f09cbea3SMark Johnston LIST_FOREACH_SAFE(slab, &freeslabs, us_link, tmp) 1451f09cbea3SMark Johnston keg_free_slab(keg, slab, keg->uk_ipers); 1452f09cbea3SMark Johnston } 1453f09cbea3SMark Johnston 14548355f576SJeff Roberson /* 1455e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 14568355f576SJeff Roberson * the pageout daemon. 14578355f576SJeff Roberson * 1458e20a199fSJeff Roberson * Returns nothing. 14598355f576SJeff Roberson */ 1460e20a199fSJeff Roberson static void 1461e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 14628355f576SJeff Roberson { 1463f09cbea3SMark Johnston int i; 14648355f576SJeff Roberson 1465f09cbea3SMark Johnston if ((keg->uk_flags & UMA_ZONE_NOFREE) != 0) 14668355f576SJeff Roberson return; 1467f09cbea3SMark Johnston for (i = 0; i < vm_ndomains; i++) 1468f09cbea3SMark Johnston keg_drain_domain(keg, i); 14698355f576SJeff Roberson } 14708355f576SJeff Roberson 1471e20a199fSJeff Roberson static void 147208cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain) 1473e20a199fSJeff Roberson { 1474e20a199fSJeff Roberson 14758355f576SJeff Roberson /* 1476e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 1477e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 1478e20a199fSJeff Roberson * is the only call that knows the structure will still be available 1479e20a199fSJeff Roberson * when it wakes up. 1480e20a199fSJeff Roberson */ 1481e20a199fSJeff Roberson ZONE_LOCK(zone); 148208cfa56eSMark Johnston while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) { 1483e20a199fSJeff Roberson if (waitok == M_NOWAIT) 1484e20a199fSJeff Roberson goto out; 1485c6fd3e23SJeff Roberson msleep(zone, &ZDOM_GET(zone, 0)->uzd_lock, PVM, "zonedrain", 1486c6fd3e23SJeff Roberson 1); 1487e20a199fSJeff Roberson } 148808cfa56eSMark Johnston zone->uz_flags |= UMA_ZFLAG_RECLAIMING; 1489e20a199fSJeff Roberson ZONE_UNLOCK(zone); 149091d947bfSJeff Roberson bucket_cache_reclaim(zone, drain); 149108cfa56eSMark Johnston 1492e20a199fSJeff Roberson /* 1493e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 1494111fbcd5SBryan Venteicher * we're running. Normally the uma_rwlock would protect us but we 1495e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 1496e20a199fSJeff Roberson */ 149708034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1498bb15d1c7SGleb Smirnoff keg_drain(zone->uz_keg); 1499e20a199fSJeff Roberson ZONE_LOCK(zone); 150008cfa56eSMark Johnston zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING; 1501e20a199fSJeff Roberson wakeup(zone); 1502e20a199fSJeff Roberson out: 1503e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1504e20a199fSJeff Roberson } 1505e20a199fSJeff Roberson 150608cfa56eSMark Johnston static void 150720a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused) 1508e20a199fSJeff Roberson { 1509e20a199fSJeff Roberson 151008cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, true); 151108cfa56eSMark Johnston } 151208cfa56eSMark Johnston 151308cfa56eSMark Johnston static void 151420a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused) 151508cfa56eSMark Johnston { 151608cfa56eSMark Johnston 151708cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, false); 1518e20a199fSJeff Roberson } 1519e20a199fSJeff Roberson 1520e20a199fSJeff Roberson /* 15218b987a77SJeff Roberson * Allocate a new slab for a keg and inserts it into the partial slab list. 15228b987a77SJeff Roberson * The keg should be unlocked on entry. If the allocation succeeds it will 15238b987a77SJeff Roberson * be locked on return. 15248355f576SJeff Roberson * 15258355f576SJeff Roberson * Arguments: 152686220393SMark Johnston * flags Wait flags for the item initialization routine 152786220393SMark Johnston * aflags Wait flags for the slab allocation 15288355f576SJeff Roberson * 15298355f576SJeff Roberson * Returns: 15308355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 15318355f576SJeff Roberson * caller specified M_NOWAIT. 15328355f576SJeff Roberson */ 15338355f576SJeff Roberson static uma_slab_t 153486220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 153586220393SMark Johnston int aflags) 15368355f576SJeff Roberson { 15378b987a77SJeff Roberson uma_domain_t dom; 1538e20a199fSJeff Roberson uma_alloc allocf; 1539099a0e58SBosko Milekic uma_slab_t slab; 15402e47807cSJeff Roberson unsigned long size; 154185dcf349SGleb Smirnoff uint8_t *mem; 154286220393SMark Johnston uint8_t sflags; 15438355f576SJeff Roberson int i; 15448355f576SJeff Roberson 1545ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1546ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1547a553d4b8SJeff Roberson 15488b987a77SJeff Roberson allocf = keg->uk_allocf; 1549194a979eSMark Johnston slab = NULL; 1550194a979eSMark Johnston mem = NULL; 155154c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 15529b8db4d0SRyan Libby uma_hash_slab_t hslab; 15539b8db4d0SRyan Libby hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL, 15549b8db4d0SRyan Libby domain, aflags); 15559b8db4d0SRyan Libby if (hslab == NULL) 1556727c6918SJeff Roberson goto fail; 15579b8db4d0SRyan Libby slab = &hslab->uhs_slab; 1558a553d4b8SJeff Roberson } 1559a553d4b8SJeff Roberson 15603370c5bfSJeff Roberson /* 15613370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 15623370c5bfSJeff Roberson * first time they are added to a zone. 15633370c5bfSJeff Roberson * 15643370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 15653370c5bfSJeff Roberson */ 15663370c5bfSJeff Roberson 1567099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 156886220393SMark Johnston aflags |= M_ZERO; 15693370c5bfSJeff Roberson else 157086220393SMark Johnston aflags &= ~M_ZERO; 15713370c5bfSJeff Roberson 1572263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 157386220393SMark Johnston aflags |= M_NODUMP; 1574263811f7SKip Macy 1575e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1576194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 157786220393SMark Johnston mem = allocf(zone, size, domain, &sflags, aflags); 1578a553d4b8SJeff Roberson if (mem == NULL) { 157954c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 15809b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), 15819b8db4d0SRyan Libby slab_tohashslab(slab), NULL, SKIP_NONE); 1582727c6918SJeff Roberson goto fail; 1583a553d4b8SJeff Roberson } 15842e47807cSJeff Roberson uma_total_inc(size); 15858355f576SJeff Roberson 15868b987a77SJeff Roberson /* For HASH zones all pages go to the same uma_domain. */ 158754c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 15888b987a77SJeff Roberson domain = 0; 15898b987a77SJeff Roberson 15905c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 159154c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) 1592099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 15931e0701e1SJeff Roberson else 15949b8db4d0SRyan Libby slab_tohashslab(slab)->uhs_data = mem; 15955c0e403bSJeff Roberson 159654c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_VTOSLAB) 1597099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1598584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1599584061b4SJeff Roberson zone, slab); 16008355f576SJeff Roberson 1601099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 160286220393SMark Johnston slab->us_flags = sflags; 1603ab3185d1SJeff Roberson slab->us_domain = domain; 16048b987a77SJeff Roberson 16059b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1606ef72505eSJeff Roberson #ifdef INVARIANTS 1607815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1608ef72505eSJeff Roberson #endif 1609099a0e58SBosko Milekic 1610b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1611099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 16121e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 161386220393SMark Johnston keg->uk_size, flags) != 0) 1614b23f72e9SBrian Feldman break; 1615b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1616fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1617727c6918SJeff Roberson goto fail; 1618b23f72e9SBrian Feldman } 1619b23f72e9SBrian Feldman } 16208b987a77SJeff Roberson KEG_LOCK(keg, domain); 16215c0e403bSJeff Roberson 16221431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 16231431a748SGleb Smirnoff slab, keg->uk_name, keg); 16241431a748SGleb Smirnoff 162554c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 1626099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 16278355f576SJeff Roberson 16288b987a77SJeff Roberson /* 16298b987a77SJeff Roberson * If we got a slab here it's safe to mark it partially used 16308b987a77SJeff Roberson * and return. We assume that the caller is going to remove 16318b987a77SJeff Roberson * at least one item. 16328b987a77SJeff Roberson */ 16338b987a77SJeff Roberson dom = &keg->uk_domain[domain]; 16348b987a77SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 16358b987a77SJeff Roberson dom->ud_pages += keg->uk_ppera; 16364ab3aee8SMark Johnston dom->ud_free_items += keg->uk_ipers; 16378355f576SJeff Roberson 16388355f576SJeff Roberson return (slab); 1639727c6918SJeff Roberson 1640727c6918SJeff Roberson fail: 1641727c6918SJeff Roberson return (NULL); 16428355f576SJeff Roberson } 16438355f576SJeff Roberson 16448355f576SJeff Roberson /* 1645009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1646009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1647009b6fcbSJeff Roberson * the VM is ready. 1648009b6fcbSJeff Roberson */ 1649009b6fcbSJeff Roberson static void * 1650ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1651ab3185d1SJeff Roberson int wait) 1652009b6fcbSJeff Roberson { 1653a81c400eSJeff Roberson vm_paddr_t pa; 1654a81c400eSJeff Roberson vm_page_t m; 1655ac0a6fd0SGleb Smirnoff void *mem; 1656ac0a6fd0SGleb Smirnoff int pages; 1657a81c400eSJeff Roberson int i; 1658099a0e58SBosko Milekic 1659f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1660f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1661a81c400eSJeff Roberson 1662f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1663a81c400eSJeff Roberson m = vm_page_alloc_contig_domain(NULL, 0, domain, 1664a81c400eSJeff Roberson malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages, 1665a81c400eSJeff Roberson (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT); 1666a81c400eSJeff Roberson if (m == NULL) 1667a81c400eSJeff Roberson return (NULL); 1668a81c400eSJeff Roberson 1669a81c400eSJeff Roberson pa = VM_PAGE_TO_PHYS(m); 1670a81c400eSJeff Roberson for (i = 0; i < pages; i++, pa += PAGE_SIZE) { 1671a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1672a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1673a81c400eSJeff Roberson if ((wait & M_NODUMP) == 0) 1674a81c400eSJeff Roberson dump_add_page(pa); 1675a81c400eSJeff Roberson #endif 1676a81c400eSJeff Roberson } 1677a81c400eSJeff Roberson /* Allocate KVA and indirectly advance bootmem. */ 1678a81c400eSJeff Roberson mem = (void *)pmap_map(&bootmem, m->phys_addr, 1679a81c400eSJeff Roberson m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE); 1680a81c400eSJeff Roberson if ((wait & M_ZERO) != 0) 1681a81c400eSJeff Roberson bzero(mem, pages * PAGE_SIZE); 1682f7d35785SGleb Smirnoff 1683f7d35785SGleb Smirnoff return (mem); 1684f7d35785SGleb Smirnoff } 1685f7d35785SGleb Smirnoff 1686a81c400eSJeff Roberson static void 1687a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes) 1688a81c400eSJeff Roberson { 1689a81c400eSJeff Roberson vm_offset_t va; 1690a81c400eSJeff Roberson vm_page_t m; 1691a81c400eSJeff Roberson 1692a81c400eSJeff Roberson va = (vm_offset_t)mem; 1693a81c400eSJeff Roberson m = PHYS_TO_VM_PAGE(pmap_kextract(va)); 1694*663de81fSMark Johnston 1695*663de81fSMark Johnston /* 1696*663de81fSMark Johnston * startup_alloc() returns direct-mapped slabs on some platforms. Avoid 1697*663de81fSMark Johnston * unmapping ranges of the direct map. 1698*663de81fSMark Johnston */ 1699*663de81fSMark Johnston if (va >= bootstart && va + bytes <= bootmem) 1700a81c400eSJeff Roberson pmap_remove(kernel_pmap, va, va + bytes); 1701a81c400eSJeff Roberson for (; bytes != 0; bytes -= PAGE_SIZE, m++) { 1702a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1703a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1704a81c400eSJeff Roberson dump_drop_page(VM_PAGE_TO_PHYS(m)); 1705a81c400eSJeff Roberson #endif 1706a81c400eSJeff Roberson vm_page_unwire_noq(m); 1707a81c400eSJeff Roberson vm_page_free(m); 1708a81c400eSJeff Roberson } 1709a81c400eSJeff Roberson } 1710a81c400eSJeff Roberson 1711f7d35785SGleb Smirnoff /* 17128355f576SJeff Roberson * Allocates a number of pages from the system 17138355f576SJeff Roberson * 17148355f576SJeff Roberson * Arguments: 17158355f576SJeff Roberson * bytes The number of bytes requested 17168355f576SJeff Roberson * wait Shall we wait? 17178355f576SJeff Roberson * 17188355f576SJeff Roberson * Returns: 17198355f576SJeff Roberson * A pointer to the alloced memory or possibly 17208355f576SJeff Roberson * NULL if M_NOWAIT is set. 17218355f576SJeff Roberson */ 17228355f576SJeff Roberson static void * 1723ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1724ab3185d1SJeff Roberson int wait) 17258355f576SJeff Roberson { 17268355f576SJeff Roberson void *p; /* Returned page */ 17278355f576SJeff Roberson 17282e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 17299978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 17308355f576SJeff Roberson 17318355f576SJeff Roberson return (p); 17328355f576SJeff Roberson } 17338355f576SJeff Roberson 1734ab3059a8SMatt Macy static void * 1735ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1736ab3059a8SMatt Macy int wait) 1737ab3059a8SMatt Macy { 1738ab3059a8SMatt Macy struct pglist alloctail; 1739ab3059a8SMatt Macy vm_offset_t addr, zkva; 1740ab3059a8SMatt Macy int cpu, flags; 1741ab3059a8SMatt Macy vm_page_t p, p_next; 1742ab3059a8SMatt Macy #ifdef NUMA 1743ab3059a8SMatt Macy struct pcpu *pc; 1744ab3059a8SMatt Macy #endif 1745ab3059a8SMatt Macy 1746ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1747ab3059a8SMatt Macy 1748013072f0SMark Johnston TAILQ_INIT(&alloctail); 1749ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1750013072f0SMark Johnston malloc2vm_flags(wait); 1751013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1752ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1753ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1754ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1755ab3059a8SMatt Macy } else { 1756ab3059a8SMatt Macy #ifndef NUMA 1757ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1758ab3059a8SMatt Macy #else 1759ab3059a8SMatt Macy pc = pcpu_find(cpu); 176020526802SAndrew Gallatin if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain))) 176120526802SAndrew Gallatin p = NULL; 176220526802SAndrew Gallatin else 176320526802SAndrew Gallatin p = vm_page_alloc_domain(NULL, 0, 176420526802SAndrew Gallatin pc->pc_domain, flags); 1765ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1766ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1767ab3059a8SMatt Macy #endif 1768ab3059a8SMatt Macy } 1769ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1770ab3059a8SMatt Macy goto fail; 1771ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1772ab3059a8SMatt Macy } 1773ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1774ab3059a8SMatt Macy goto fail; 1775ab3059a8SMatt Macy zkva = addr; 1776ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1777ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1778ab3059a8SMatt Macy zkva += PAGE_SIZE; 1779ab3059a8SMatt Macy } 1780ab3059a8SMatt Macy return ((void*)addr); 1781ab3059a8SMatt Macy fail: 1782ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 178388ea538aSMark Johnston vm_page_unwire_noq(p); 1784ab3059a8SMatt Macy vm_page_free(p); 1785ab3059a8SMatt Macy } 1786ab3059a8SMatt Macy return (NULL); 1787ab3059a8SMatt Macy } 1788ab3059a8SMatt Macy 17898355f576SJeff Roberson /* 17908355f576SJeff Roberson * Allocates a number of pages from within an object 17918355f576SJeff Roberson * 17928355f576SJeff Roberson * Arguments: 17938355f576SJeff Roberson * bytes The number of bytes requested 17948355f576SJeff Roberson * wait Shall we wait? 17958355f576SJeff Roberson * 17968355f576SJeff Roberson * Returns: 17978355f576SJeff Roberson * A pointer to the alloced memory or possibly 17988355f576SJeff Roberson * NULL if M_NOWAIT is set. 17998355f576SJeff Roberson */ 18008355f576SJeff Roberson static void * 1801ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1802ab3185d1SJeff Roberson int wait) 18038355f576SJeff Roberson { 1804a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1805a4915c21SAttilio Rao u_long npages; 1806b245ac95SAlan Cox vm_offset_t retkva, zkva; 1807a4915c21SAttilio Rao vm_page_t p, p_next; 1808e20a199fSJeff Roberson uma_keg_t keg; 18098355f576SJeff Roberson 1810a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1811bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1812a4915c21SAttilio Rao 1813a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1814a4915c21SAttilio Rao while (npages > 0) { 1815ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 18168d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1817772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1818772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1819a4915c21SAttilio Rao if (p != NULL) { 1820a4915c21SAttilio Rao /* 1821a4915c21SAttilio Rao * Since the page does not belong to an object, its 1822a4915c21SAttilio Rao * listq is unused. 1823a4915c21SAttilio Rao */ 1824a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1825a4915c21SAttilio Rao npages--; 1826a4915c21SAttilio Rao continue; 1827a4915c21SAttilio Rao } 18288355f576SJeff Roberson /* 1829a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1830a4915c21SAttilio Rao * exit. 18318355f576SJeff Roberson */ 1832a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 183388ea538aSMark Johnston vm_page_unwire_noq(p); 1834b245ac95SAlan Cox vm_page_free(p); 1835b245ac95SAlan Cox } 1836a4915c21SAttilio Rao return (NULL); 1837b245ac95SAlan Cox } 18388355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1839a4915c21SAttilio Rao zkva = keg->uk_kva + 1840a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1841a4915c21SAttilio Rao retkva = zkva; 1842a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1843a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1844a4915c21SAttilio Rao zkva += PAGE_SIZE; 1845a4915c21SAttilio Rao } 18468355f576SJeff Roberson 18478355f576SJeff Roberson return ((void *)retkva); 18488355f576SJeff Roberson } 18498355f576SJeff Roberson 18508355f576SJeff Roberson /* 1851ec0d8280SRyan Libby * Allocate physically contiguous pages. 1852ec0d8280SRyan Libby */ 1853ec0d8280SRyan Libby static void * 1854ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1855ec0d8280SRyan Libby int wait) 1856ec0d8280SRyan Libby { 1857ec0d8280SRyan Libby 1858ec0d8280SRyan Libby *pflag = UMA_SLAB_KERNEL; 1859ec0d8280SRyan Libby return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain), 1860ec0d8280SRyan Libby bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT)); 1861ec0d8280SRyan Libby } 1862ec0d8280SRyan Libby 1863ec0d8280SRyan Libby /* 18648355f576SJeff Roberson * Frees a number of pages to the system 18658355f576SJeff Roberson * 18668355f576SJeff Roberson * Arguments: 18678355f576SJeff Roberson * mem A pointer to the memory to be freed 18688355f576SJeff Roberson * size The size of the memory being freed 18698355f576SJeff Roberson * flags The original p->us_flags field 18708355f576SJeff Roberson * 18718355f576SJeff Roberson * Returns: 18728355f576SJeff Roberson * Nothing 18738355f576SJeff Roberson */ 18748355f576SJeff Roberson static void 1875f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 18768355f576SJeff Roberson { 18773370c5bfSJeff Roberson 1878a81c400eSJeff Roberson if ((flags & UMA_SLAB_BOOT) != 0) { 1879a81c400eSJeff Roberson startup_free(mem, size); 1880a81c400eSJeff Roberson return; 1881a81c400eSJeff Roberson } 1882a81c400eSJeff Roberson 1883ec0d8280SRyan Libby KASSERT((flags & UMA_SLAB_KERNEL) != 0, 1884ec0d8280SRyan Libby ("UMA: page_free used with invalid flags %x", flags)); 18858355f576SJeff Roberson 188649bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 18878355f576SJeff Roberson } 18888355f576SJeff Roberson 18898355f576SJeff Roberson /* 1890ab3059a8SMatt Macy * Frees pcpu zone allocations 1891ab3059a8SMatt Macy * 1892ab3059a8SMatt Macy * Arguments: 1893ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1894ab3059a8SMatt Macy * size The size of the memory being freed 1895ab3059a8SMatt Macy * flags The original p->us_flags field 1896ab3059a8SMatt Macy * 1897ab3059a8SMatt Macy * Returns: 1898ab3059a8SMatt Macy * Nothing 1899ab3059a8SMatt Macy */ 1900ab3059a8SMatt Macy static void 1901ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1902ab3059a8SMatt Macy { 1903ab3059a8SMatt Macy vm_offset_t sva, curva; 1904ab3059a8SMatt Macy vm_paddr_t paddr; 1905ab3059a8SMatt Macy vm_page_t m; 1906ab3059a8SMatt Macy 1907ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 19085ba16cf3SRyan Libby 19095ba16cf3SRyan Libby if ((flags & UMA_SLAB_BOOT) != 0) { 19105ba16cf3SRyan Libby startup_free(mem, size); 19115ba16cf3SRyan Libby return; 19125ba16cf3SRyan Libby } 19135ba16cf3SRyan Libby 1914ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1915ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1916ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1917ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 191888ea538aSMark Johnston vm_page_unwire_noq(m); 1919ab3059a8SMatt Macy vm_page_free(m); 1920ab3059a8SMatt Macy } 1921ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1922ab3059a8SMatt Macy kva_free(sva, size); 1923ab3059a8SMatt Macy } 1924ab3059a8SMatt Macy 1925ab3059a8SMatt Macy /* 19268355f576SJeff Roberson * Zero fill initializer 19278355f576SJeff Roberson * 19288355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 19298355f576SJeff Roberson */ 1930b23f72e9SBrian Feldman static int 1931b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 19328355f576SJeff Roberson { 19338355f576SJeff Roberson bzero(mem, size); 1934b23f72e9SBrian Feldman return (0); 19358355f576SJeff Roberson } 19368355f576SJeff Roberson 1937815db204SRyan Libby #ifdef INVARIANTS 193854007ce8SMark Johnston static struct noslabbits * 1939815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1940815db204SRyan Libby { 1941815db204SRyan Libby 1942815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1943815db204SRyan Libby } 1944815db204SRyan Libby #endif 1945815db204SRyan Libby 19468355f576SJeff Roberson /* 19479b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 19489b78b1f4SJeff Roberson */ 194954007ce8SMark Johnston static size_t 19509b78b1f4SJeff Roberson slab_sizeof(int nitems) 19519b78b1f4SJeff Roberson { 19529b78b1f4SJeff Roberson size_t s; 19539b78b1f4SJeff Roberson 1954815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 19559b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 19569b78b1f4SJeff Roberson } 19579b78b1f4SJeff Roberson 19584a8b575cSRyan Libby #define UMA_FIXPT_SHIFT 31 19594a8b575cSRyan Libby #define UMA_FRAC_FIXPT(n, d) \ 19604a8b575cSRyan Libby ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d))) 19614a8b575cSRyan Libby #define UMA_FIXPT_PCT(f) \ 19624a8b575cSRyan Libby ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT)) 19634a8b575cSRyan Libby #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100) 19644a8b575cSRyan Libby #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE) 19654a8b575cSRyan Libby 19669b78b1f4SJeff Roberson /* 19674a8b575cSRyan Libby * Compute the number of items that will fit in a slab. If hdr is true, the 19684a8b575cSRyan Libby * item count may be limited to provide space in the slab for an inline slab 19694a8b575cSRyan Libby * header. Otherwise, all slab space will be provided for item storage. 19704a8b575cSRyan Libby */ 19714a8b575cSRyan Libby static u_int 19724a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr) 19734a8b575cSRyan Libby { 19744a8b575cSRyan Libby u_int ipers; 19754a8b575cSRyan Libby u_int padpi; 19764a8b575cSRyan Libby 19774a8b575cSRyan Libby /* The padding between items is not needed after the last item. */ 19784a8b575cSRyan Libby padpi = rsize - size; 19794a8b575cSRyan Libby 19804a8b575cSRyan Libby if (hdr) { 19814a8b575cSRyan Libby /* 19824a8b575cSRyan Libby * Start with the maximum item count and remove items until 19834a8b575cSRyan Libby * the slab header first alongside the allocatable memory. 19844a8b575cSRyan Libby */ 19854a8b575cSRyan Libby for (ipers = MIN(SLAB_MAX_SETSIZE, 19864a8b575cSRyan Libby (slabsize + padpi - slab_sizeof(1)) / rsize); 19874a8b575cSRyan Libby ipers > 0 && 19884a8b575cSRyan Libby ipers * rsize - padpi + slab_sizeof(ipers) > slabsize; 19894a8b575cSRyan Libby ipers--) 19904a8b575cSRyan Libby continue; 19914a8b575cSRyan Libby } else { 19924a8b575cSRyan Libby ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE); 19934a8b575cSRyan Libby } 19944a8b575cSRyan Libby 19954a8b575cSRyan Libby return (ipers); 19964a8b575cSRyan Libby } 19974a8b575cSRyan Libby 199827ca37acSRyan Libby struct keg_layout_result { 199927ca37acSRyan Libby u_int format; 200027ca37acSRyan Libby u_int slabsize; 200127ca37acSRyan Libby u_int ipers; 200227ca37acSRyan Libby u_int eff; 200327ca37acSRyan Libby }; 200427ca37acSRyan Libby 200527ca37acSRyan Libby static void 200627ca37acSRyan Libby keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt, 200727ca37acSRyan Libby struct keg_layout_result *kl) 200827ca37acSRyan Libby { 200927ca37acSRyan Libby u_int total; 201027ca37acSRyan Libby 201127ca37acSRyan Libby kl->format = fmt; 201227ca37acSRyan Libby kl->slabsize = slabsize; 201327ca37acSRyan Libby 201427ca37acSRyan Libby /* Handle INTERNAL as inline with an extra page. */ 201527ca37acSRyan Libby if ((fmt & UMA_ZFLAG_INTERNAL) != 0) { 201627ca37acSRyan Libby kl->format &= ~UMA_ZFLAG_INTERNAL; 201727ca37acSRyan Libby kl->slabsize += PAGE_SIZE; 201827ca37acSRyan Libby } 201927ca37acSRyan Libby 202027ca37acSRyan Libby kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize, 202127ca37acSRyan Libby (fmt & UMA_ZFLAG_OFFPAGE) == 0); 202227ca37acSRyan Libby 202327ca37acSRyan Libby /* Account for memory used by an offpage slab header. */ 202427ca37acSRyan Libby total = kl->slabsize; 202527ca37acSRyan Libby if ((fmt & UMA_ZFLAG_OFFPAGE) != 0) 202627ca37acSRyan Libby total += slabzone(kl->ipers)->uz_keg->uk_rsize; 202727ca37acSRyan Libby 202827ca37acSRyan Libby kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total); 202927ca37acSRyan Libby } 203027ca37acSRyan Libby 20319b78b1f4SJeff Roberson /* 20324a8b575cSRyan Libby * Determine the format of a uma keg. This determines where the slab header 20334a8b575cSRyan Libby * will be placed (inline or offpage) and calculates ipers, rsize, and ppera. 20348355f576SJeff Roberson * 20358355f576SJeff Roberson * Arguments 2036e20a199fSJeff Roberson * keg The zone we should initialize 20378355f576SJeff Roberson * 20388355f576SJeff Roberson * Returns 20398355f576SJeff Roberson * Nothing 20408355f576SJeff Roberson */ 20418355f576SJeff Roberson static void 20424a8b575cSRyan Libby keg_layout(uma_keg_t keg) 20438355f576SJeff Roberson { 204427ca37acSRyan Libby struct keg_layout_result kl = {}, kl_tmp; 204527ca37acSRyan Libby u_int fmts[2]; 20464a8b575cSRyan Libby u_int alignsize; 204727ca37acSRyan Libby u_int nfmt; 20484a8b575cSRyan Libby u_int pages; 2049244f4554SBosko Milekic u_int rsize; 2050a55ebb7cSAndriy Gapon u_int slabsize; 205127ca37acSRyan Libby u_int i, j; 20528355f576SJeff Roberson 20534a8b575cSRyan Libby KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 20544a8b575cSRyan Libby (keg->uk_size <= UMA_PCPU_ALLOC_SIZE && 20554a8b575cSRyan Libby (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0), 20564a8b575cSRyan Libby ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b", 20574a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_size, keg->uk_flags, 20584a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 2059bae55c4aSRyan Libby KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 || 20604a8b575cSRyan Libby (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0, 20614a8b575cSRyan Libby ("%s: incompatible flags 0x%b", __func__, keg->uk_flags, 20624a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 2063e28a647dSGleb Smirnoff 20644a8b575cSRyan Libby alignsize = keg->uk_align + 1; 2065ad97af7eSGleb Smirnoff 2066ef72505eSJeff Roberson /* 2067ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 2068ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 2069ef72505eSJeff Roberson * allocation bits for we round it up. 2070ef72505eSJeff Roberson */ 20719b8db4d0SRyan Libby rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT); 20724a8b575cSRyan Libby rsize = roundup2(rsize, alignsize); 2073ad97af7eSGleb Smirnoff 207427ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) { 20759b78b1f4SJeff Roberson /* 20764a8b575cSRyan Libby * We want one item to start on every align boundary in a page. 20774a8b575cSRyan Libby * To do this we will span pages. We will also extend the item 20784a8b575cSRyan Libby * by the size of align if it is an even multiple of align. 20794a8b575cSRyan Libby * Otherwise, it would fall on the same boundary every time. 20809b78b1f4SJeff Roberson */ 20814a8b575cSRyan Libby if ((rsize & alignsize) == 0) 20824a8b575cSRyan Libby rsize += alignsize; 20834a8b575cSRyan Libby slabsize = rsize * (PAGE_SIZE / alignsize); 20844a8b575cSRyan Libby slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE); 20854a8b575cSRyan Libby slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE); 208627ca37acSRyan Libby slabsize = round_page(slabsize); 20874a8b575cSRyan Libby } else { 20884a8b575cSRyan Libby /* 208927ca37acSRyan Libby * Start with a slab size of as many pages as it takes to 209027ca37acSRyan Libby * represent a single item. We will try to fit as many 209127ca37acSRyan Libby * additional items into the slab as possible. 20924a8b575cSRyan Libby */ 209327ca37acSRyan Libby slabsize = round_page(keg->uk_size); 20941ca6ed45SGleb Smirnoff } 2095ad97af7eSGleb Smirnoff 209627ca37acSRyan Libby /* Build a list of all of the available formats for this keg. */ 209727ca37acSRyan Libby nfmt = 0; 209827ca37acSRyan Libby 20994a8b575cSRyan Libby /* Evaluate an inline slab layout. */ 21004a8b575cSRyan Libby if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0) 210127ca37acSRyan Libby fmts[nfmt++] = 0; 21024a8b575cSRyan Libby 21034a8b575cSRyan Libby /* TODO: vm_page-embedded slab. */ 2104244f4554SBosko Milekic 210520e8e865SBosko Milekic /* 2106244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 210720e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 2108bae55c4aSRyan Libby * may end up going to the VM for slabs which we do not want 2109bae55c4aSRyan Libby * to do if we're UMA_ZONE_VM, which clearly forbids it. 2110bae55c4aSRyan Libby * In those cases, evaluate a pseudo-format called INTERNAL 2111bae55c4aSRyan Libby * which has an inline slab header and one extra page to 2112bae55c4aSRyan Libby * guarantee that it fits. 211327ca37acSRyan Libby * 211427ca37acSRyan Libby * Otherwise, see if using an OFFPAGE slab will improve our 211527ca37acSRyan Libby * efficiency. 211620e8e865SBosko Milekic */ 2117bae55c4aSRyan Libby if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0) 211827ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_INTERNAL; 211927ca37acSRyan Libby else 212027ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_OFFPAGE; 2121244f4554SBosko Milekic 2122ef72505eSJeff Roberson /* 212327ca37acSRyan Libby * Choose a slab size and format which satisfy the minimum efficiency. 212427ca37acSRyan Libby * Prefer the smallest slab size that meets the constraints. 2125ef72505eSJeff Roberson * 212627ca37acSRyan Libby * Start with a minimum slab size, to accommodate CACHESPREAD. Then, 212727ca37acSRyan Libby * for small items (up to PAGE_SIZE), the iteration increment is one 212827ca37acSRyan Libby * page; and for large items, the increment is one item. 2129ef72505eSJeff Roberson */ 213027ca37acSRyan Libby i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize); 213127ca37acSRyan Libby KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u", 213227ca37acSRyan Libby keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize, 213327ca37acSRyan Libby rsize, i)); 213427ca37acSRyan Libby for ( ; ; i++) { 213527ca37acSRyan Libby slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) : 213627ca37acSRyan Libby round_page(rsize * (i - 1) + keg->uk_size); 213727ca37acSRyan Libby 213827ca37acSRyan Libby for (j = 0; j < nfmt; j++) { 213927ca37acSRyan Libby /* Only if we have no viable format yet. */ 214027ca37acSRyan Libby if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 && 214127ca37acSRyan Libby kl.ipers > 0) 214227ca37acSRyan Libby continue; 214327ca37acSRyan Libby 214427ca37acSRyan Libby keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp); 214527ca37acSRyan Libby if (kl_tmp.eff <= kl.eff) 214627ca37acSRyan Libby continue; 214727ca37acSRyan Libby 214827ca37acSRyan Libby kl = kl_tmp; 214927ca37acSRyan Libby 215027ca37acSRyan Libby CTR6(KTR_UMA, "keg %s layout: format %#x " 215127ca37acSRyan Libby "(ipers %u * rsize %u) / slabsize %#x = %u%% eff", 215227ca37acSRyan Libby keg->uk_name, kl.format, kl.ipers, rsize, 215327ca37acSRyan Libby kl.slabsize, UMA_FIXPT_PCT(kl.eff)); 215427ca37acSRyan Libby 215527ca37acSRyan Libby /* Stop when we reach the minimum efficiency. */ 215627ca37acSRyan Libby if (kl.eff >= UMA_MIN_EFF) 215727ca37acSRyan Libby break; 21588355f576SJeff Roberson } 2159ad97af7eSGleb Smirnoff 216033e5a1eaSRyan Libby if (kl.eff >= UMA_MIN_EFF || !multipage_slabs || 216127ca37acSRyan Libby slabsize >= SLAB_MAX_SETSIZE * rsize || 216227ca37acSRyan Libby (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0) 216327ca37acSRyan Libby break; 216427ca37acSRyan Libby } 216527ca37acSRyan Libby 216627ca37acSRyan Libby pages = atop(kl.slabsize); 216727ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 216827ca37acSRyan Libby pages *= mp_maxid + 1; 216927ca37acSRyan Libby 217027ca37acSRyan Libby keg->uk_rsize = rsize; 217127ca37acSRyan Libby keg->uk_ipers = kl.ipers; 217227ca37acSRyan Libby keg->uk_ppera = pages; 217327ca37acSRyan Libby keg->uk_flags |= kl.format; 217427ca37acSRyan Libby 21754a8b575cSRyan Libby /* 21764a8b575cSRyan Libby * How do we find the slab header if it is offpage or if not all item 21774a8b575cSRyan Libby * start addresses are in the same page? We could solve the latter 21784a8b575cSRyan Libby * case with vaddr alignment, but we don't. 21794a8b575cSRyan Libby */ 218027ca37acSRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 || 218127ca37acSRyan Libby (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) { 218254c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0) 218327ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_HASH; 218454c5ae80SRyan Libby else 218527ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 218654c5ae80SRyan Libby } 218727ca37acSRyan Libby 2188e63a1c2fSRyan Libby CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u", 218927ca37acSRyan Libby __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers, 219027ca37acSRyan Libby pages); 21914a8b575cSRyan Libby KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 21924a8b575cSRyan Libby ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__, 219327ca37acSRyan Libby keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize, 219427ca37acSRyan Libby keg->uk_ipers, pages)); 2195e20a199fSJeff Roberson } 2196e20a199fSJeff Roberson 21978355f576SJeff Roberson /* 2198099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 2199099a0e58SBosko Milekic * the keg onto the global keg list. 22008355f576SJeff Roberson * 22018355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 2202099a0e58SBosko Milekic * udata Actually uma_kctor_args 2203099a0e58SBosko Milekic */ 2204b23f72e9SBrian Feldman static int 2205b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 2206099a0e58SBosko Milekic { 2207099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 2208099a0e58SBosko Milekic uma_keg_t keg = mem; 2209099a0e58SBosko Milekic uma_zone_t zone; 22108b987a77SJeff Roberson int i; 2211099a0e58SBosko Milekic 2212099a0e58SBosko Milekic bzero(keg, size); 2213099a0e58SBosko Milekic keg->uk_size = arg->size; 2214099a0e58SBosko Milekic keg->uk_init = arg->uminit; 2215099a0e58SBosko Milekic keg->uk_fini = arg->fini; 2216099a0e58SBosko Milekic keg->uk_align = arg->align; 22176fd34d6fSJeff Roberson keg->uk_reserve = 0; 2218099a0e58SBosko Milekic keg->uk_flags = arg->flags; 2219099a0e58SBosko Milekic 2220099a0e58SBosko Milekic /* 2221194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 2222dfe13344SJeff Roberson * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which 2223dfe13344SJeff Roberson * case the iterator is never run. 2224194a979eSMark Johnston */ 2225194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 2226194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 2227194a979eSMark Johnston 2228194a979eSMark Johnston /* 2229c8b0a88bSJeff Roberson * The primary zone is passed to us at keg-creation time. 2230099a0e58SBosko Milekic */ 2231099a0e58SBosko Milekic zone = arg->zone; 2232e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 2233099a0e58SBosko Milekic 2234099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 2235099a0e58SBosko Milekic keg->uk_init = zero_init; 2236099a0e58SBosko Milekic 2237cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 223854c5ae80SRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 2239e20a199fSJeff Roberson 224054c5ae80SRyan Libby #ifndef SMP 2241ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 2242ad97af7eSGleb Smirnoff #endif 2243ad97af7eSGleb Smirnoff 22444a8b575cSRyan Libby keg_layout(keg); 2245099a0e58SBosko Milekic 22468b987a77SJeff Roberson /* 2247c6fd3e23SJeff Roberson * Use a first-touch NUMA policy for kegs that pmap_extract() will 2248c6fd3e23SJeff Roberson * work on. Use round-robin for everything else. 2249dfe13344SJeff Roberson * 2250dfe13344SJeff Roberson * Zones may override the default by specifying either. 22518b987a77SJeff Roberson */ 2252dfe13344SJeff Roberson #ifdef NUMA 2253dfe13344SJeff Roberson if ((keg->uk_flags & 2254c6fd3e23SJeff Roberson (UMA_ZONE_ROUNDROBIN | UMA_ZFLAG_CACHE | UMA_ZONE_NOTPAGE)) == 0) 2255dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_FIRSTTOUCH; 2256dfe13344SJeff Roberson else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2257dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_ROUNDROBIN; 22588b987a77SJeff Roberson #endif 22598b987a77SJeff Roberson 2260099a0e58SBosko Milekic /* 2261099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 2262099a0e58SBosko Milekic * startup cache until the vm is ready. 2263099a0e58SBosko Milekic */ 226477e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2265a81c400eSJeff Roberson if (keg->uk_ppera == 1) 226677e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 2267a81c400eSJeff Roberson else 22688cd02d00SAlan Cox #endif 2269a81c400eSJeff Roberson if (booted < BOOT_KVA) 2270a81c400eSJeff Roberson keg->uk_allocf = startup_alloc; 2271ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 2272ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 2273ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1) 2274ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 227577e19437SGleb Smirnoff else 227677e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 227777e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 227877e19437SGleb Smirnoff if (keg->uk_ppera == 1) 227977e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 228077e19437SGleb Smirnoff else 228177e19437SGleb Smirnoff #endif 2282ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 2283ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 2284ab3059a8SMatt Macy else 228577e19437SGleb Smirnoff keg->uk_freef = page_free; 2286099a0e58SBosko Milekic 2287099a0e58SBosko Milekic /* 22888b987a77SJeff Roberson * Initialize keg's locks. 2289099a0e58SBosko Milekic */ 22908b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 22918b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 2292099a0e58SBosko Milekic 2293099a0e58SBosko Milekic /* 2294099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 22959b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 22969b78b1f4SJeff Roberson * definition. 2297099a0e58SBosko Milekic */ 229854c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) { 22999b78b1f4SJeff Roberson size_t shsize; 23009b78b1f4SJeff Roberson 23019b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 23029b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 2303244f4554SBosko Milekic /* 2304244f4554SBosko Milekic * The only way the following is possible is if with our 2305244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 2306244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 2307244f4554SBosko Milekic * mathematically possible for all cases, so we make 2308244f4554SBosko Milekic * sure here anyway. 2309244f4554SBosko Milekic */ 23109b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 23113d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 23123d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 2313099a0e58SBosko Milekic } 2314099a0e58SBosko Milekic 231554c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 23163b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 2317099a0e58SBosko Milekic 2318e63a1c2fSRyan Libby CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone); 2319099a0e58SBosko Milekic 2320099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 2321099a0e58SBosko Milekic 2322111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2323099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2324111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2325b23f72e9SBrian Feldman return (0); 2326099a0e58SBosko Milekic } 2327099a0e58SBosko Milekic 23282efcc8cbSGleb Smirnoff static void 2329a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused) 2330a81c400eSJeff Roberson { 2331a81c400eSJeff Roberson uma_keg_t keg; 2332a81c400eSJeff Roberson 2333a81c400eSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 2334a81c400eSJeff Roberson return; 2335a81c400eSJeff Roberson KEG_GET(zone, keg); 2336ec0d8280SRyan Libby 2337ec0d8280SRyan Libby if (keg->uk_allocf == startup_alloc) { 2338ec0d8280SRyan Libby /* Switch to the real allocator. */ 2339f96d4157SJeff Roberson if (keg->uk_flags & UMA_ZONE_PCPU) 2340f96d4157SJeff Roberson keg->uk_allocf = pcpu_page_alloc; 2341ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && 2342ec0d8280SRyan Libby keg->uk_ppera > 1) 2343ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 2344ec0d8280SRyan Libby else 2345a81c400eSJeff Roberson keg->uk_allocf = page_alloc; 2346a81c400eSJeff Roberson } 2347ec0d8280SRyan Libby } 2348a81c400eSJeff Roberson 2349a81c400eSJeff Roberson static void 235020a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 23512efcc8cbSGleb Smirnoff { 23522efcc8cbSGleb Smirnoff 23532efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 23542efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 23552efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 2356c6fd3e23SJeff Roberson zone->uz_xdomain = counter_u64_alloc(M_WAITOK); 23572efcc8cbSGleb Smirnoff } 23582efcc8cbSGleb Smirnoff 235920a4e154SJeff Roberson static void 236020a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 236120a4e154SJeff Roberson { 236220a4e154SJeff Roberson uma_zone_domain_t zdom; 23638b987a77SJeff Roberson uma_domain_t dom; 236420a4e154SJeff Roberson uma_keg_t keg; 236520a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 23663b490537SJeff Roberson int domains, i, cnt; 236720a4e154SJeff Roberson static const char *nokeg = "cache zone"; 236820a4e154SJeff Roberson char *c; 236920a4e154SJeff Roberson 237020a4e154SJeff Roberson /* 237120a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 237220a4e154SJeff Roberson * any special characters and handling dups by appending 237320a4e154SJeff Roberson * an index. 237420a4e154SJeff Roberson */ 237520a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 23763b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 23773b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 23783b490537SJeff Roberson cnt /= 10; 23793b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 23803b490537SJeff Roberson M_UMA, M_WAITOK); 238120a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 238220a4e154SJeff Roberson zone->uz_namecnt); 238320a4e154SJeff Roberson } else 238420a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 238520a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 238620a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 238720a4e154SJeff Roberson *c = '_'; 238820a4e154SJeff Roberson 238920a4e154SJeff Roberson /* 239020a4e154SJeff Roberson * Basic parameters at the root. 239120a4e154SJeff Roberson */ 239220a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 23937029da5cSPawel Biernacki OID_AUTO, zone->uz_ctlname, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 239420a4e154SJeff Roberson oid = zone->uz_oid; 239520a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 239620a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 23976d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 23986d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 23996d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 240020a4e154SJeff Roberson "Allocator configuration flags"); 240120a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 240220a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 240320a4e154SJeff Roberson "Desired per-cpu cache size"); 240420a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 240520a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 240620a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 240720a4e154SJeff Roberson 240820a4e154SJeff Roberson /* 240920a4e154SJeff Roberson * keg if present. 241020a4e154SJeff Roberson */ 241154c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 24128b987a77SJeff Roberson domains = vm_ndomains; 24138b987a77SJeff Roberson else 24148b987a77SJeff Roberson domains = 1; 241520a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 24167029da5cSPawel Biernacki "keg", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 241720a4e154SJeff Roberson keg = zone->uz_keg; 24183b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 241920a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 242020a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 242120a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 242220a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 242320a4e154SJeff Roberson "Real object size with alignment"); 242420a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 242520a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 242620a4e154SJeff Roberson "pages per-slab allocation"); 242720a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 242820a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 242920a4e154SJeff Roberson "items available per-slab"); 243020a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 243120a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 243220a4e154SJeff Roberson "item alignment mask"); 2433f09cbea3SMark Johnston SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2434f09cbea3SMark Johnston "reserve", CTLFLAG_RD, &keg->uk_reserve, 0, 2435f09cbea3SMark Johnston "number of reserved items"); 2436f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2437f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2438f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2439f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 24408b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 24417029da5cSPawel Biernacki OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 24428b987a77SJeff Roberson for (i = 0; i < domains; i++) { 24438b987a77SJeff Roberson dom = &keg->uk_domain[i]; 24448b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 24457029da5cSPawel Biernacki OID_AUTO, VM_DOMAIN(i)->vmd_name, 24467029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 24478b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 24488b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 24498b987a77SJeff Roberson "Total pages currently allocated from VM"); 24508b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 24514ab3aee8SMark Johnston "free_items", CTLFLAG_RD, &dom->ud_free_items, 0, 24528b987a77SJeff Roberson "items free in the slab layer"); 24538b987a77SJeff Roberson } 245420a4e154SJeff Roberson } else 245520a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 245620a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 245720a4e154SJeff Roberson 245820a4e154SJeff Roberson /* 245920a4e154SJeff Roberson * Information about zone limits. 246020a4e154SJeff Roberson */ 246120a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 24627029da5cSPawel Biernacki "limit", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 24634bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 24644bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 24654bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 2466e574d407SMark Johnston "Current number of allocated items if limit is set"); 246720a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 246820a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 2469e574d407SMark Johnston "Maximum number of allocated and cached items"); 247020a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 247120a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 247220a4e154SJeff Roberson "Number of threads sleeping at limit"); 247320a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 247420a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 247520a4e154SJeff Roberson "Total zone limit sleeps"); 24764bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2477c6fd3e23SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bucket_max, 0, 2478c6fd3e23SJeff Roberson "Maximum number of items in each domain's bucket cache"); 247920a4e154SJeff Roberson 248020a4e154SJeff Roberson /* 24818b987a77SJeff Roberson * Per-domain zone information. 248220a4e154SJeff Roberson */ 248320a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 24847029da5cSPawel Biernacki OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 248520a4e154SJeff Roberson for (i = 0; i < domains; i++) { 2486c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 248720a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 24887029da5cSPawel Biernacki OID_AUTO, VM_DOMAIN(i)->vmd_name, 24897029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 249020a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 249120a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 249220a4e154SJeff Roberson "number of items in this domain"); 249320a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 249420a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 249520a4e154SJeff Roberson "maximum item count in this period"); 249620a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 249720a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 249820a4e154SJeff Roberson "minimum item count in this period"); 249920a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 250020a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 250120a4e154SJeff Roberson "Working set size"); 250220a4e154SJeff Roberson } 250320a4e154SJeff Roberson 250420a4e154SJeff Roberson /* 250520a4e154SJeff Roberson * General statistics. 250620a4e154SJeff Roberson */ 250720a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 25087029da5cSPawel Biernacki "stats", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 250920a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 251020a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 251120a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 251220a4e154SJeff Roberson "Current number of allocated items"); 251320a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 251420a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 251520a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 251620a4e154SJeff Roberson "Total allocation calls"); 251720a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 251820a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 251920a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 252020a4e154SJeff Roberson "Total free calls"); 252120a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 252220a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 252320a4e154SJeff Roberson "Number of allocation failures"); 2524c6fd3e23SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2525c6fd3e23SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 252620a4e154SJeff Roberson "Free calls from the wrong domain"); 252720a4e154SJeff Roberson } 252820a4e154SJeff Roberson 252920a4e154SJeff Roberson struct uma_zone_count { 253020a4e154SJeff Roberson const char *name; 253120a4e154SJeff Roberson int count; 253220a4e154SJeff Roberson }; 253320a4e154SJeff Roberson 253420a4e154SJeff Roberson static void 253520a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 253620a4e154SJeff Roberson { 253720a4e154SJeff Roberson struct uma_zone_count *cnt; 253820a4e154SJeff Roberson 253920a4e154SJeff Roberson cnt = arg; 25403b490537SJeff Roberson /* 25413b490537SJeff Roberson * Some zones are rapidly created with identical names and 25423b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 25433b490537SJeff Roberson * Use one greater than the maximum observed for this name. 25443b490537SJeff Roberson */ 254520a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 25463b490537SJeff Roberson cnt->count = MAX(cnt->count, 25473b490537SJeff Roberson zone->uz_namecnt + 1); 254820a4e154SJeff Roberson } 254920a4e154SJeff Roberson 2550cc7ce83aSJeff Roberson static void 2551cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2552cc7ce83aSJeff Roberson { 2553cc7ce83aSJeff Roberson int i; 2554cc7ce83aSJeff Roberson 2555cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2556cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2557cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2558cc7ce83aSJeff Roberson } 2559cc7ce83aSJeff Roberson } 2560cc7ce83aSJeff Roberson 2561099a0e58SBosko Milekic /* 2562099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2563099a0e58SBosko Milekic * 2564099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2565099a0e58SBosko Milekic * udata Actually uma_zctor_args 25668355f576SJeff Roberson */ 2567b23f72e9SBrian Feldman static int 2568b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 25698355f576SJeff Roberson { 257020a4e154SJeff Roberson struct uma_zone_count cnt; 25718355f576SJeff Roberson struct uma_zctor_args *arg = udata; 2572c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 25738355f576SJeff Roberson uma_zone_t zone = mem; 2574099a0e58SBosko Milekic uma_zone_t z; 2575099a0e58SBosko Milekic uma_keg_t keg; 257608cfa56eSMark Johnston int i; 25778355f576SJeff Roberson 25788355f576SJeff Roberson bzero(zone, size); 25798355f576SJeff Roberson zone->uz_name = arg->name; 25808355f576SJeff Roberson zone->uz_ctor = arg->ctor; 25818355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2582099a0e58SBosko Milekic zone->uz_init = NULL; 2583099a0e58SBosko Milekic zone->uz_fini = NULL; 2584bf965959SSean Bruno zone->uz_sleeps = 0; 258520a4e154SJeff Roberson zone->uz_bucket_size = 0; 258620a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 258720a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2588d4665eaaSJeff Roberson zone->uz_flags = (arg->flags & UMA_ZONE_SMR); 25892f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2590ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 2591c6fd3e23SJeff Roberson zone->uz_bucket_max = ULONG_MAX; 25922f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2593af526374SJeff Roberson 259420a4e154SJeff Roberson /* Count the number of duplicate names. */ 259520a4e154SJeff Roberson cnt.name = arg->name; 259620a4e154SJeff Roberson cnt.count = 0; 259720a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 259820a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 259991d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 26002efcc8cbSGleb Smirnoff 2601c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 2602c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 2603c6fd3e23SJeff Roberson ZDOM_LOCK_INIT(zone, zdom, (arg->flags & UMA_ZONE_MTXCLASS)); 2604c6fd3e23SJeff Roberson STAILQ_INIT(&zdom->uzd_buckets); 2605c6fd3e23SJeff Roberson } 260608cfa56eSMark Johnston 2607ca293436SRyan Libby #ifdef INVARIANTS 2608ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2609cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 2610ca293436SRyan Libby #endif 2611ca293436SRyan Libby 26120095a784SJeff Roberson /* 26130095a784SJeff Roberson * This is a pure cache zone, no kegs. 26140095a784SJeff Roberson */ 26150095a784SJeff Roberson if (arg->import) { 2616727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2617727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 26186fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2619af526374SJeff Roberson zone->uz_size = arg->size; 26200095a784SJeff Roberson zone->uz_import = arg->import; 26210095a784SJeff Roberson zone->uz_release = arg->release; 26220095a784SJeff Roberson zone->uz_arg = arg->arg; 2623c6fd3e23SJeff Roberson #ifdef NUMA 2624c6fd3e23SJeff Roberson /* 2625c6fd3e23SJeff Roberson * Cache zones are round-robin unless a policy is 2626c6fd3e23SJeff Roberson * specified because they may have incompatible 2627c6fd3e23SJeff Roberson * constraints. 2628c6fd3e23SJeff Roberson */ 2629c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2630c6fd3e23SJeff Roberson zone->uz_flags |= UMA_ZONE_ROUNDROBIN; 2631c6fd3e23SJeff Roberson #endif 2632111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 263303175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2634111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2635af526374SJeff Roberson goto out; 26360095a784SJeff Roberson } 26370095a784SJeff Roberson 26380095a784SJeff Roberson /* 26390095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 26400095a784SJeff Roberson */ 2641b75c4efcSAndrew Turner zone->uz_import = zone_import; 2642b75c4efcSAndrew Turner zone->uz_release = zone_release; 26430095a784SJeff Roberson zone->uz_arg = zone; 2644bb15d1c7SGleb Smirnoff keg = arg->keg; 26450095a784SJeff Roberson 2646099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 264720a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 264820a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2649099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 26508355f576SJeff Roberson zone->uz_init = arg->uminit; 2651e221e841SJeff Roberson zone->uz_fini = arg->fini; 2652e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2653111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2654099a0e58SBosko Milekic ZONE_LOCK(zone); 2655099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2656099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2657099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2658099a0e58SBosko Milekic break; 2659099a0e58SBosko Milekic } 2660099a0e58SBosko Milekic } 2661099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2662111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2663e20a199fSJeff Roberson } else if (keg == NULL) { 2664e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2665e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2666b23f72e9SBrian Feldman return (ENOMEM); 2667099a0e58SBosko Milekic } else { 2668099a0e58SBosko Milekic struct uma_kctor_args karg; 2669b23f72e9SBrian Feldman int error; 2670099a0e58SBosko Milekic 2671099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2672099a0e58SBosko Milekic karg.size = arg->size; 2673099a0e58SBosko Milekic karg.uminit = arg->uminit; 2674099a0e58SBosko Milekic karg.fini = arg->fini; 2675099a0e58SBosko Milekic karg.align = arg->align; 2676d4665eaaSJeff Roberson karg.flags = (arg->flags & ~UMA_ZONE_SMR); 2677099a0e58SBosko Milekic karg.zone = zone; 2678b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2679b23f72e9SBrian Feldman flags); 2680b23f72e9SBrian Feldman if (error) 2681b23f72e9SBrian Feldman return (error); 2682099a0e58SBosko Milekic } 26830095a784SJeff Roberson 268420a4e154SJeff Roberson /* Inherit properties from the keg. */ 2685bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2686e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2687e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2688e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 26898355f576SJeff Roberson 269020a4e154SJeff Roberson out: 2691dc2b3205SMark Johnston if (booted >= BOOT_PCPU) { 269220a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 2693dc2b3205SMark Johnston if (booted >= BOOT_RUNNING) 269420a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 269520a4e154SJeff Roberson } else { 269620a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 269720a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 269820a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2699099a0e58SBosko Milekic } 27008355f576SJeff Roberson 2701d4665eaaSJeff Roberson /* Caller requests a private SMR context. */ 2702d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 2703226dd6dbSJeff Roberson zone->uz_smr = smr_create(zone->uz_name, 0, 0); 2704d4665eaaSJeff Roberson 27057e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 27067e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 27077e28037aSMark Johnston ("Invalid zone flag combination")); 270820a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 270920a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 271020a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 271120a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 271220a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 271320a4e154SJeff Roberson zone->uz_bucket_size = 0; 27147e28037aSMark Johnston else 271520a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 271620a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2717cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2718cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2719cc7ce83aSJeff Roberson zone_update_caches(zone); 2720fc03d22bSJeff Roberson 2721b23f72e9SBrian Feldman return (0); 27228355f576SJeff Roberson } 27238355f576SJeff Roberson 27248355f576SJeff Roberson /* 2725099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2726099a0e58SBosko Milekic * table and removes the keg from the global list. 27279c2cd7e5SJeff Roberson * 27289c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 27299c2cd7e5SJeff Roberson * udata unused 27309c2cd7e5SJeff Roberson */ 2731099a0e58SBosko Milekic static void 2732099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2733099a0e58SBosko Milekic { 2734099a0e58SBosko Milekic uma_keg_t keg; 27358b987a77SJeff Roberson uint32_t free, pages; 27368b987a77SJeff Roberson int i; 27379c2cd7e5SJeff Roberson 2738099a0e58SBosko Milekic keg = (uma_keg_t)arg; 27398b987a77SJeff Roberson free = pages = 0; 27408b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 27414ab3aee8SMark Johnston free += keg->uk_domain[i].ud_free_items; 27428b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 27438b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2744099a0e58SBosko Milekic } 27457e240677SRyan Libby if (pages != 0) 27468b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 27478b987a77SJeff Roberson " Lost %u pages of memory.\n", 27488b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 27497e240677SRyan Libby pages / keg->uk_ppera * keg->uk_ipers - free, pages); 2750099a0e58SBosko Milekic 2751099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2752099a0e58SBosko Milekic } 2753099a0e58SBosko Milekic 2754099a0e58SBosko Milekic /* 2755099a0e58SBosko Milekic * Zone header dtor. 2756099a0e58SBosko Milekic * 2757099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2758099a0e58SBosko Milekic * udata unused 2759099a0e58SBosko Milekic */ 27609c2cd7e5SJeff Roberson static void 27619c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 27629c2cd7e5SJeff Roberson { 27639c2cd7e5SJeff Roberson uma_zone_t zone; 2764099a0e58SBosko Milekic uma_keg_t keg; 2765c6fd3e23SJeff Roberson int i; 27669c2cd7e5SJeff Roberson 27679c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 27689643769aSJeff Roberson 276920a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 277020a4e154SJeff Roberson 2771e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 27729643769aSJeff Roberson cache_drain(zone); 2773099a0e58SBosko Milekic 2774111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2775099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2776111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 27777b516613SJonathan T. Looney if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 27787b516613SJonathan T. Looney keg = zone->uz_keg; 27797b516613SJonathan T. Looney keg->uk_reserve = 0; 27807b516613SJonathan T. Looney } 278108cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2782c6fd3e23SJeff Roberson 2783e20a199fSJeff Roberson /* 2784323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2785e20a199fSJeff Roberson */ 2786323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2787323ad386STycho Nightingale keg = zone->uz_keg; 2788111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2789099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2790111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 27910095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 27929c2cd7e5SJeff Roberson } 27932efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 27942efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 27952efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 2796c6fd3e23SJeff Roberson counter_u64_free(zone->uz_xdomain); 279720a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2798c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) 2799c6fd3e23SJeff Roberson ZDOM_LOCK_FINI(ZDOM_GET(zone, i)); 280091d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 2801099a0e58SBosko Milekic } 2802099a0e58SBosko Milekic 2803a81c400eSJeff Roberson static void 2804a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg) 2805a81c400eSJeff Roberson { 2806a81c400eSJeff Roberson uma_keg_t keg; 2807a81c400eSJeff Roberson uma_zone_t zone; 2808a81c400eSJeff Roberson 2809a81c400eSJeff Roberson LIST_FOREACH(keg, &uma_kegs, uk_link) { 2810a81c400eSJeff Roberson LIST_FOREACH(zone, &keg->uk_zones, uz_link) 2811a81c400eSJeff Roberson zfunc(zone, arg); 2812a81c400eSJeff Roberson } 2813a81c400eSJeff Roberson LIST_FOREACH(zone, &uma_cachezones, uz_link) 2814a81c400eSJeff Roberson zfunc(zone, arg); 2815a81c400eSJeff Roberson } 2816a81c400eSJeff Roberson 28179c2cd7e5SJeff Roberson /* 28188355f576SJeff Roberson * Traverses every zone in the system and calls a callback 28198355f576SJeff Roberson * 28208355f576SJeff Roberson * Arguments: 28218355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 28228355f576SJeff Roberson * as an argument. 28238355f576SJeff Roberson * 28248355f576SJeff Roberson * Returns: 28258355f576SJeff Roberson * Nothing 28268355f576SJeff Roberson */ 28278355f576SJeff Roberson static void 282820a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 28298355f576SJeff Roberson { 28308355f576SJeff Roberson 2831111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2832a81c400eSJeff Roberson zone_foreach_unlocked(zfunc, arg); 2833111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 28348355f576SJeff Roberson } 28358355f576SJeff Roberson 2836f4bef67cSGleb Smirnoff /* 2837a81c400eSJeff Roberson * Initialize the kernel memory allocator. This is done after pages can be 2838a81c400eSJeff Roberson * allocated but before general KVA is available. 2839f4bef67cSGleb Smirnoff */ 2840a81c400eSJeff Roberson void 2841a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail) 2842f4bef67cSGleb Smirnoff { 2843a81c400eSJeff Roberson struct uma_zctor_args args; 2844a81c400eSJeff Roberson size_t ksize, zsize, size; 2845c8b0a88bSJeff Roberson uma_keg_t primarykeg; 2846a81c400eSJeff Roberson uintptr_t m; 284781302f1dSMark Johnston int domain; 2848a81c400eSJeff Roberson uint8_t pflag; 2849a81c400eSJeff Roberson 2850a81c400eSJeff Roberson bootstart = bootmem = virtual_avail; 2851a81c400eSJeff Roberson 2852a81c400eSJeff Roberson rw_init(&uma_rwlock, "UMA lock"); 2853a81c400eSJeff Roberson sx_init(&uma_reclaim_lock, "umareclaim"); 2854f4bef67cSGleb Smirnoff 2855f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2856f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 285779c9f942SJeff Roberson ksize = roundup(ksize, UMA_SUPER_ALIGN); 2858f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2859f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2860f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 286179c9f942SJeff Roberson zsize = roundup(zsize, UMA_SUPER_ALIGN); 2862f4bef67cSGleb Smirnoff 2863a81c400eSJeff Roberson /* Allocate the zone of zones, zone of kegs, and zone of zones keg. */ 2864a81c400eSJeff Roberson size = (zsize * 2) + ksize; 286581302f1dSMark Johnston for (domain = 0; domain < vm_ndomains; domain++) { 286681302f1dSMark Johnston m = (uintptr_t)startup_alloc(NULL, size, domain, &pflag, 286781302f1dSMark Johnston M_NOWAIT | M_ZERO); 286881302f1dSMark Johnston if (m != 0) 286981302f1dSMark Johnston break; 287081302f1dSMark Johnston } 2871ab3185d1SJeff Roberson zones = (uma_zone_t)m; 287279c9f942SJeff Roberson m += zsize; 2873ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 287479c9f942SJeff Roberson m += zsize; 2875c8b0a88bSJeff Roberson primarykeg = (uma_keg_t)m; 2876ab3185d1SJeff Roberson 2877099a0e58SBosko Milekic /* "manually" create the initial zone */ 28780095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2879099a0e58SBosko Milekic args.name = "UMA Kegs"; 2880ab3185d1SJeff Roberson args.size = ksize; 2881099a0e58SBosko Milekic args.ctor = keg_ctor; 2882099a0e58SBosko Milekic args.dtor = keg_dtor; 28838355f576SJeff Roberson args.uminit = zero_init; 28848355f576SJeff Roberson args.fini = NULL; 2885c8b0a88bSJeff Roberson args.keg = primarykeg; 288679c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2887b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2888ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 28898355f576SJeff Roberson 2890099a0e58SBosko Milekic args.name = "UMA Zones"; 2891f4bef67cSGleb Smirnoff args.size = zsize; 2892099a0e58SBosko Milekic args.ctor = zone_ctor; 2893099a0e58SBosko Milekic args.dtor = zone_dtor; 2894099a0e58SBosko Milekic args.uminit = zero_init; 2895099a0e58SBosko Milekic args.fini = NULL; 2896099a0e58SBosko Milekic args.keg = NULL; 289779c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2898099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2899ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2900099a0e58SBosko Milekic 29019b8db4d0SRyan Libby /* Now make zones for slab headers */ 29029b8db4d0SRyan Libby slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE, 29039b8db4d0SRyan Libby NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 29049b8db4d0SRyan Libby slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE, 29051e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 29068355f576SJeff Roberson 29078355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 29088355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 29091e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 29108355f576SJeff Roberson 2911a81c400eSJeff Roberson bucket_init(); 2912d4665eaaSJeff Roberson smr_init(); 29138355f576SJeff Roberson } 29148355f576SJeff Roberson 2915a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2916a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void); 2917f4bef67cSGleb Smirnoff #endif 2918f4bef67cSGleb Smirnoff 2919a81c400eSJeff Roberson /* 2920a81c400eSJeff Roberson * Advertise the availability of normal kva allocations and switch to 2921a81c400eSJeff Roberson * the default back-end allocator. Marks the KVA we consumed on startup 2922a81c400eSJeff Roberson * as used in the map. 2923a81c400eSJeff Roberson */ 29248355f576SJeff Roberson void 292599571dc3SJeff Roberson uma_startup2(void) 29268355f576SJeff Roberson { 2927f4bef67cSGleb Smirnoff 2928530cc6a2SJeff Roberson if (bootstart != bootmem) { 2929a81c400eSJeff Roberson vm_map_lock(kernel_map); 2930a81c400eSJeff Roberson (void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem, 2931a81c400eSJeff Roberson VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 2932a81c400eSJeff Roberson vm_map_unlock(kernel_map); 2933a81c400eSJeff Roberson } 2934a81c400eSJeff Roberson 2935a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2936a81c400eSJeff Roberson /* Set up radix zone to use noobj_alloc. */ 2937a81c400eSJeff Roberson vm_radix_reserve_kva(); 2938f7d35785SGleb Smirnoff #endif 2939a81c400eSJeff Roberson 2940a81c400eSJeff Roberson booted = BOOT_KVA; 2941a81c400eSJeff Roberson zone_foreach_unlocked(zone_kva_available, NULL); 2942f4bef67cSGleb Smirnoff bucket_enable(); 29438355f576SJeff Roberson } 29448355f576SJeff Roberson 2945a81c400eSJeff Roberson /* 2946dc2b3205SMark Johnston * Allocate counters as early as possible so that boot-time allocations are 2947dc2b3205SMark Johnston * accounted more precisely. 2948dc2b3205SMark Johnston */ 2949dc2b3205SMark Johnston static void 2950dc2b3205SMark Johnston uma_startup_pcpu(void *arg __unused) 2951dc2b3205SMark Johnston { 2952dc2b3205SMark Johnston 2953dc2b3205SMark Johnston zone_foreach_unlocked(zone_alloc_counters, NULL); 2954dc2b3205SMark Johnston booted = BOOT_PCPU; 2955dc2b3205SMark Johnston } 2956dc2b3205SMark Johnston SYSINIT(uma_startup_pcpu, SI_SUB_COUNTER, SI_ORDER_ANY, uma_startup_pcpu, NULL); 2957dc2b3205SMark Johnston 2958dc2b3205SMark Johnston /* 2959a81c400eSJeff Roberson * Finish our initialization steps. 2960a81c400eSJeff Roberson */ 29618355f576SJeff Roberson static void 2962dc2b3205SMark Johnston uma_startup3(void *arg __unused) 29638355f576SJeff Roberson { 29641431a748SGleb Smirnoff 2965c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2966c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2967c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2968c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2969c5deaf04SGleb Smirnoff #endif 2970a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_sysctl, NULL); 2971fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 29729643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2973c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 2974860bb7a0SMark Johnston 2975860bb7a0SMark Johnston EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL, 2976860bb7a0SMark Johnston EVENTHANDLER_PRI_FIRST); 2977860bb7a0SMark Johnston } 2978dc2b3205SMark Johnston SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 2979860bb7a0SMark Johnston 2980860bb7a0SMark Johnston static void 2981860bb7a0SMark Johnston uma_shutdown(void) 2982860bb7a0SMark Johnston { 2983860bb7a0SMark Johnston 2984860bb7a0SMark Johnston booted = BOOT_SHUTDOWN; 29858355f576SJeff Roberson } 29868355f576SJeff Roberson 2987e20a199fSJeff Roberson static uma_keg_t 2988099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 298985dcf349SGleb Smirnoff int align, uint32_t flags) 2990099a0e58SBosko Milekic { 2991099a0e58SBosko Milekic struct uma_kctor_args args; 2992099a0e58SBosko Milekic 2993099a0e58SBosko Milekic args.size = size; 2994099a0e58SBosko Milekic args.uminit = uminit; 2995099a0e58SBosko Milekic args.fini = fini; 29961e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2997099a0e58SBosko Milekic args.flags = flags; 2998099a0e58SBosko Milekic args.zone = zone; 2999ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 3000099a0e58SBosko Milekic } 3001099a0e58SBosko Milekic 3002f4bef67cSGleb Smirnoff /* Public functions */ 30038355f576SJeff Roberson /* See uma.h */ 30041e319f6dSRobert Watson void 30051e319f6dSRobert Watson uma_set_align(int align) 30061e319f6dSRobert Watson { 30071e319f6dSRobert Watson 30081e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 30091e319f6dSRobert Watson uma_align_cache = align; 30101e319f6dSRobert Watson } 30111e319f6dSRobert Watson 30121e319f6dSRobert Watson /* See uma.h */ 30138355f576SJeff Roberson uma_zone_t 3014bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 301585dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 30168355f576SJeff Roberson 30178355f576SJeff Roberson { 30188355f576SJeff Roberson struct uma_zctor_args args; 301995c4bf75SKonstantin Belousov uma_zone_t res; 30208355f576SJeff Roberson 3021a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 3022a5a35578SJohn Baldwin align, name)); 3023a5a35578SJohn Baldwin 30248355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 30250095a784SJeff Roberson memset(&args, 0, sizeof(args)); 30268355f576SJeff Roberson args.name = name; 30278355f576SJeff Roberson args.size = size; 30288355f576SJeff Roberson args.ctor = ctor; 30298355f576SJeff Roberson args.dtor = dtor; 30308355f576SJeff Roberson args.uminit = uminit; 30318355f576SJeff Roberson args.fini = fini; 3032afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 3033afc6dc36SJohn-Mark Gurney /* 3034ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 3035ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 3036ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 3037ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 3038ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 3039ca293436SRyan Libby * the ctor and dtor do not need to be empty. 3040afc6dc36SJohn-Mark Gurney */ 304154c5ae80SRyan Libby if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH | 304254c5ae80SRyan Libby UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) { 3043afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 3044afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 3045afc6dc36SJohn-Mark Gurney } 3046afc6dc36SJohn-Mark Gurney #endif 30478355f576SJeff Roberson args.align = align; 30488355f576SJeff Roberson args.flags = flags; 3049099a0e58SBosko Milekic args.keg = NULL; 3050099a0e58SBosko Milekic 305108cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 3052ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 305308cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 3054a81c400eSJeff Roberson 305595c4bf75SKonstantin Belousov return (res); 3056099a0e58SBosko Milekic } 3057099a0e58SBosko Milekic 3058099a0e58SBosko Milekic /* See uma.h */ 3059099a0e58SBosko Milekic uma_zone_t 30600464f16eSMark Johnston uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor, 3061c8b0a88bSJeff Roberson uma_init zinit, uma_fini zfini, uma_zone_t primary) 3062099a0e58SBosko Milekic { 3063099a0e58SBosko Milekic struct uma_zctor_args args; 3064e20a199fSJeff Roberson uma_keg_t keg; 306595c4bf75SKonstantin Belousov uma_zone_t res; 3066099a0e58SBosko Milekic 3067c8b0a88bSJeff Roberson keg = primary->uz_keg; 30680095a784SJeff Roberson memset(&args, 0, sizeof(args)); 3069099a0e58SBosko Milekic args.name = name; 3070e20a199fSJeff Roberson args.size = keg->uk_size; 3071099a0e58SBosko Milekic args.ctor = ctor; 3072099a0e58SBosko Milekic args.dtor = dtor; 3073099a0e58SBosko Milekic args.uminit = zinit; 3074099a0e58SBosko Milekic args.fini = zfini; 3075e20a199fSJeff Roberson args.align = keg->uk_align; 3076e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 3077e20a199fSJeff Roberson args.keg = keg; 30788355f576SJeff Roberson 307908cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 3080ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 308108cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 3082a81c400eSJeff Roberson 308395c4bf75SKonstantin Belousov return (res); 30848355f576SJeff Roberson } 30858355f576SJeff Roberson 30860095a784SJeff Roberson /* See uma.h */ 30870095a784SJeff Roberson uma_zone_t 30880464f16eSMark Johnston uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor, 30890464f16eSMark Johnston uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease, 30900464f16eSMark Johnston void *arg, int flags) 30910095a784SJeff Roberson { 30920095a784SJeff Roberson struct uma_zctor_args args; 30930095a784SJeff Roberson 30940095a784SJeff Roberson memset(&args, 0, sizeof(args)); 30950095a784SJeff Roberson args.name = name; 3096af526374SJeff Roberson args.size = size; 30970095a784SJeff Roberson args.ctor = ctor; 30980095a784SJeff Roberson args.dtor = dtor; 30990095a784SJeff Roberson args.uminit = zinit; 31000095a784SJeff Roberson args.fini = zfini; 31010095a784SJeff Roberson args.import = zimport; 31020095a784SJeff Roberson args.release = zrelease; 31030095a784SJeff Roberson args.arg = arg; 31040095a784SJeff Roberson args.align = 0; 3105bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 31060095a784SJeff Roberson 3107ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 31080095a784SJeff Roberson } 31090095a784SJeff Roberson 31108355f576SJeff Roberson /* See uma.h */ 31119c2cd7e5SJeff Roberson void 31129c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 31139c2cd7e5SJeff Roberson { 3114f4ff923bSRobert Watson 3115860bb7a0SMark Johnston /* 3116860bb7a0SMark Johnston * Large slabs are expensive to reclaim, so don't bother doing 3117860bb7a0SMark Johnston * unnecessary work if we're shutting down. 3118860bb7a0SMark Johnston */ 3119860bb7a0SMark Johnston if (booted == BOOT_SHUTDOWN && 3120860bb7a0SMark Johnston zone->uz_fini == NULL && zone->uz_release == zone_release) 3121860bb7a0SMark Johnston return; 312208cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 31230095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 312408cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 31259c2cd7e5SJeff Roberson } 31269c2cd7e5SJeff Roberson 31278d6fbbb8SJeff Roberson void 31288d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 31298d6fbbb8SJeff Roberson { 31308d6fbbb8SJeff Roberson 313170260874SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 313270260874SJeff Roberson uma_zfree_smr(zone, uma_zalloc_smr(zone, M_WAITOK)); 313370260874SJeff Roberson else if ((zone->uz_flags & UMA_ZONE_PCPU) != 0) 313470260874SJeff Roberson uma_zfree_pcpu(zone, uma_zalloc_pcpu(zone, M_WAITOK)); 313570260874SJeff Roberson else 313670260874SJeff Roberson uma_zfree(zone, uma_zalloc(zone, M_WAITOK)); 31378d6fbbb8SJeff Roberson } 31388d6fbbb8SJeff Roberson 31394e180881SMateusz Guzik void * 31404e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 31414e180881SMateusz Guzik { 31423acb6572SMateusz Guzik void *item, *pcpu_item; 3143b4799947SRuslan Bukin #ifdef SMP 31444e180881SMateusz Guzik int i; 31454e180881SMateusz Guzik 31464e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 3147b4799947SRuslan Bukin #endif 31484e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 31493acb6572SMateusz Guzik if (item == NULL) 31503acb6572SMateusz Guzik return (NULL); 31513acb6572SMateusz Guzik pcpu_item = zpcpu_base_to_offset(item); 31523acb6572SMateusz Guzik if (flags & M_ZERO) { 3153b4799947SRuslan Bukin #ifdef SMP 3154013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 31553acb6572SMateusz Guzik bzero(zpcpu_get_cpu(pcpu_item, i), zone->uz_size); 3156b4799947SRuslan Bukin #else 3157b4799947SRuslan Bukin bzero(item, zone->uz_size); 3158b4799947SRuslan Bukin #endif 31594e180881SMateusz Guzik } 31603acb6572SMateusz Guzik return (pcpu_item); 31614e180881SMateusz Guzik } 31624e180881SMateusz Guzik 31634e180881SMateusz Guzik /* 31644e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 31654e180881SMateusz Guzik */ 31664e180881SMateusz Guzik void 31673acb6572SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *pcpu_item, void *udata) 31684e180881SMateusz Guzik { 31693acb6572SMateusz Guzik void *item; 31704e180881SMateusz Guzik 3171c5b7751fSIan Lepore #ifdef SMP 31724e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 3173c5b7751fSIan Lepore #endif 31743acb6572SMateusz Guzik item = zpcpu_offset_to_base(pcpu_item); 31754e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 31764e180881SMateusz Guzik } 31774e180881SMateusz Guzik 3178d4665eaaSJeff Roberson static inline void * 3179d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags, 3180d4665eaaSJeff Roberson void *item) 3181beb8beefSJeff Roberson { 3182beb8beefSJeff Roberson #ifdef INVARIANTS 3183ca293436SRyan Libby bool skipdbg; 3184beb8beefSJeff Roberson 3185beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 3186ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 3187ca293436SRyan Libby zone->uz_ctor != trash_ctor) 3188cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 3189beb8beefSJeff Roberson #endif 3190d4665eaaSJeff Roberson /* Check flags before loading ctor pointer. */ 3191d4665eaaSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) && 3192d4665eaaSJeff Roberson __predict_false(zone->uz_ctor != NULL) && 3193cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 3194beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3195beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 3196beb8beefSJeff Roberson return (NULL); 3197beb8beefSJeff Roberson } 3198beb8beefSJeff Roberson #ifdef INVARIANTS 3199beb8beefSJeff Roberson if (!skipdbg) 3200beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 3201beb8beefSJeff Roberson #endif 32026d88d784SJeff Roberson if (__predict_false(flags & M_ZERO)) 32036d88d784SJeff Roberson return (memset(item, 0, size)); 3204beb8beefSJeff Roberson 3205beb8beefSJeff Roberson return (item); 3206beb8beefSJeff Roberson } 3207beb8beefSJeff Roberson 3208ca293436SRyan Libby static inline void 3209cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 3210cc7ce83aSJeff Roberson enum zfreeskip skip) 3211ca293436SRyan Libby { 3212ca293436SRyan Libby #ifdef INVARIANTS 3213ca293436SRyan Libby bool skipdbg; 3214ca293436SRyan Libby 3215ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 3216ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 3217ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 3218ca293436SRyan Libby uma_dbg_free(zone, udata, item); 3219ca293436SRyan Libby else 3220ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 3221ca293436SRyan Libby } 3222ca293436SRyan Libby #endif 3223cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 3224ca293436SRyan Libby if (zone->uz_dtor != NULL) 3225cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 3226ca293436SRyan Libby #ifdef INVARIANTS 3227ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 3228ca293436SRyan Libby zone->uz_dtor != trash_dtor) 3229cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 3230ca293436SRyan Libby #endif 3231ca293436SRyan Libby } 3232ca293436SRyan Libby } 3233ca293436SRyan Libby 32341c58c09fSMateusz Guzik #ifdef NUMA 323581302f1dSMark Johnston static int 323681302f1dSMark Johnston item_domain(void *item) 323781302f1dSMark Johnston { 323881302f1dSMark Johnston int domain; 323981302f1dSMark Johnston 3240431fb8abSMark Johnston domain = vm_phys_domain(vtophys(item)); 324181302f1dSMark Johnston KASSERT(domain >= 0 && domain < vm_ndomains, 324281302f1dSMark Johnston ("%s: unknown domain for item %p", __func__, item)); 324381302f1dSMark Johnston return (domain); 324481302f1dSMark Johnston } 32451c58c09fSMateusz Guzik #endif 324681302f1dSMark Johnston 3247d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS) 3248d4665eaaSJeff Roberson #define UMA_ZALLOC_DEBUG 3249d4665eaaSJeff Roberson static int 3250d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags) 3251d4665eaaSJeff Roberson { 3252d4665eaaSJeff Roberson int error; 3253d4665eaaSJeff Roberson 3254d4665eaaSJeff Roberson error = 0; 3255d4665eaaSJeff Roberson #ifdef WITNESS 3256d4665eaaSJeff Roberson if (flags & M_WAITOK) { 3257d4665eaaSJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3258d4665eaaSJeff Roberson "uma_zalloc_debug: zone \"%s\"", zone->uz_name); 3259d4665eaaSJeff Roberson } 3260d4665eaaSJeff Roberson #endif 3261d4665eaaSJeff Roberson 3262d4665eaaSJeff Roberson #ifdef INVARIANTS 3263d4665eaaSJeff Roberson KASSERT((flags & M_EXEC) == 0, 3264d4665eaaSJeff Roberson ("uma_zalloc_debug: called with M_EXEC")); 3265d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3266d4665eaaSJeff Roberson ("uma_zalloc_debug: called within spinlock or critical section")); 3267d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0, 3268d4665eaaSJeff Roberson ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO")); 3269d4665eaaSJeff Roberson #endif 3270d4665eaaSJeff Roberson 3271d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 32729e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) { 3273d4665eaaSJeff Roberson void *item; 3274d4665eaaSJeff Roberson item = memguard_alloc(zone->uz_size, flags); 3275d4665eaaSJeff Roberson if (item != NULL) { 3276d4665eaaSJeff Roberson error = EJUSTRETURN; 3277d4665eaaSJeff Roberson if (zone->uz_init != NULL && 3278d4665eaaSJeff Roberson zone->uz_init(item, zone->uz_size, flags) != 0) { 3279d4665eaaSJeff Roberson *itemp = NULL; 3280d4665eaaSJeff Roberson return (error); 3281d4665eaaSJeff Roberson } 3282d4665eaaSJeff Roberson if (zone->uz_ctor != NULL && 3283d4665eaaSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 3284d4665eaaSJeff Roberson flags) != 0) { 3285d4665eaaSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3286d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3287d4665eaaSJeff Roberson *itemp = NULL; 3288d4665eaaSJeff Roberson return (error); 3289d4665eaaSJeff Roberson } 3290d4665eaaSJeff Roberson *itemp = item; 3291d4665eaaSJeff Roberson return (error); 3292d4665eaaSJeff Roberson } 3293d4665eaaSJeff Roberson /* This is unfortunate but should not be fatal. */ 3294d4665eaaSJeff Roberson } 3295d4665eaaSJeff Roberson #endif 3296d4665eaaSJeff Roberson return (error); 3297d4665eaaSJeff Roberson } 3298d4665eaaSJeff Roberson 3299d4665eaaSJeff Roberson static int 3300d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata) 3301d4665eaaSJeff Roberson { 3302d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3303d4665eaaSJeff Roberson ("uma_zfree_debug: called with spinlock or critical section held")); 3304d4665eaaSJeff Roberson 3305d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 33069e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) { 3307d4665eaaSJeff Roberson if (zone->uz_dtor != NULL) 3308d4665eaaSJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 3309d4665eaaSJeff Roberson if (zone->uz_fini != NULL) 3310d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3311d4665eaaSJeff Roberson memguard_free(item); 3312d4665eaaSJeff Roberson return (EJUSTRETURN); 3313d4665eaaSJeff Roberson } 3314d4665eaaSJeff Roberson #endif 3315d4665eaaSJeff Roberson return (0); 3316d4665eaaSJeff Roberson } 3317d4665eaaSJeff Roberson #endif 3318d4665eaaSJeff Roberson 33196d88d784SJeff Roberson static inline void * 33206d88d784SJeff Roberson cache_alloc_item(uma_zone_t zone, uma_cache_t cache, uma_cache_bucket_t bucket, 33216d88d784SJeff Roberson void *udata, int flags) 3322d4665eaaSJeff Roberson { 33236d88d784SJeff Roberson void *item; 33246d88d784SJeff Roberson int size, uz_flags; 33256d88d784SJeff Roberson 33266d88d784SJeff Roberson item = cache_bucket_pop(cache, bucket); 33276d88d784SJeff Roberson size = cache_uz_size(cache); 33286d88d784SJeff Roberson uz_flags = cache_uz_flags(cache); 33296d88d784SJeff Roberson critical_exit(); 33306d88d784SJeff Roberson return (item_ctor(zone, uz_flags, size, udata, flags, item)); 33316d88d784SJeff Roberson } 33326d88d784SJeff Roberson 33336d88d784SJeff Roberson static __noinline void * 33346d88d784SJeff Roberson cache_alloc_retry(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 33356d88d784SJeff Roberson { 33366d88d784SJeff Roberson uma_cache_bucket_t bucket; 3337d4665eaaSJeff Roberson int domain; 3338d4665eaaSJeff Roberson 33396d88d784SJeff Roberson while (cache_alloc(zone, cache, udata, flags)) { 33406d88d784SJeff Roberson cache = &zone->uz_cpu[curcpu]; 33416d88d784SJeff Roberson bucket = &cache->uc_allocbucket; 33426d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 33436d88d784SJeff Roberson continue; 33446d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, udata, flags)); 33456d88d784SJeff Roberson } 33466d88d784SJeff Roberson critical_exit(); 33476d88d784SJeff Roberson 3348d4665eaaSJeff Roberson /* 3349d4665eaaSJeff Roberson * We can not get a bucket so try to return a single item. 3350d4665eaaSJeff Roberson */ 3351d4665eaaSJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 3352d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3353d4665eaaSJeff Roberson else 3354d4665eaaSJeff Roberson domain = UMA_ANYDOMAIN; 3355d4665eaaSJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3356d4665eaaSJeff Roberson } 3357d4665eaaSJeff Roberson 3358d4665eaaSJeff Roberson /* See uma.h */ 3359d4665eaaSJeff Roberson void * 3360d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags) 3361d4665eaaSJeff Roberson { 3362d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 3363d4665eaaSJeff Roberson uma_cache_t cache; 3364d4665eaaSJeff Roberson 3365d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 33666d88d784SJeff Roberson void *item; 33676d88d784SJeff Roberson 3368d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 3369952c8964SMark Johnston ("uma_zalloc_arg: called with non-SMR zone.")); 3370d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN) 3371d4665eaaSJeff Roberson return (item); 3372d4665eaaSJeff Roberson #endif 3373d4665eaaSJeff Roberson 3374d4665eaaSJeff Roberson critical_enter(); 3375d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3376d4665eaaSJeff Roberson bucket = &cache->uc_allocbucket; 33776d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 33786d88d784SJeff Roberson return (cache_alloc_retry(zone, cache, NULL, flags)); 33796d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, NULL, flags)); 3380d4665eaaSJeff Roberson } 3381d4665eaaSJeff Roberson 33829c2cd7e5SJeff Roberson /* See uma.h */ 33838355f576SJeff Roberson void * 33842cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 33858355f576SJeff Roberson { 3386376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3387ab3185d1SJeff Roberson uma_cache_t cache; 33888355f576SJeff Roberson 3389e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 339019fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 339110cb2424SMark Murray 33928355f576SJeff Roberson /* This is the fast path allocation */ 3393e63a1c2fSRyan Libby CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name, 3394e63a1c2fSRyan Libby zone, flags); 3395a553d4b8SJeff Roberson 3396d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 33976d88d784SJeff Roberson void *item; 33986d88d784SJeff Roberson 3399d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 3400952c8964SMark Johnston ("uma_zalloc_arg: called with SMR zone.")); 3401d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN) 34028d689e04SGleb Smirnoff return (item); 34038d689e04SGleb Smirnoff #endif 3404d4665eaaSJeff Roberson 34055d1ae027SRobert Watson /* 34065d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 34075d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 34085d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 34095d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 34105d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 34115d1ae027SRobert Watson * preemption and migration. We release the critical section in 34125d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 34135d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 34145d1ae027SRobert Watson * must detect and handle migration if it has occurred. 34155d1ae027SRobert Watson */ 34165d1ae027SRobert Watson critical_enter(); 3417cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3418376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 34196d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 34206d88d784SJeff Roberson return (cache_alloc_retry(zone, cache, udata, flags)); 34216d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, udata, flags)); 3422fc03d22bSJeff Roberson } 3423fc03d22bSJeff Roberson 34248355f576SJeff Roberson /* 3425beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 3426beb8beefSJeff Roberson * a critical section. Returns in a critical section. 3427beb8beefSJeff Roberson * 34284bd61e19SJeff Roberson * A false return value indicates an allocation failure. 34294bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 3430beb8beefSJeff Roberson */ 3431beb8beefSJeff Roberson static __noinline bool 3432beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3433beb8beefSJeff Roberson { 3434beb8beefSJeff Roberson uma_bucket_t bucket; 34358c277118SMark Johnston int curdomain, domain; 3436c6fd3e23SJeff Roberson bool new; 3437beb8beefSJeff Roberson 3438beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3439beb8beefSJeff Roberson 3440beb8beefSJeff Roberson /* 3441beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3442beb8beefSJeff Roberson * if we can switch with the free bucket. 3443d4665eaaSJeff Roberson * 3444d4665eaaSJeff Roberson * SMR Zones can't re-use the free bucket until the sequence has 3445d4665eaaSJeff Roberson * expired. 34468355f576SJeff Roberson */ 3447c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_SMR) == 0 && 3448d4665eaaSJeff Roberson cache->uc_freebucket.ucb_cnt != 0) { 3449d4665eaaSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 3450d4665eaaSJeff Roberson &cache->uc_allocbucket); 3451beb8beefSJeff Roberson return (true); 34528355f576SJeff Roberson } 3453fc03d22bSJeff Roberson 3454fc03d22bSJeff Roberson /* 3455fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3456fc03d22bSJeff Roberson */ 3457376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3458fc03d22bSJeff Roberson critical_exit(); 3459c6fd3e23SJeff Roberson 3460c6fd3e23SJeff Roberson if (bucket != NULL) { 3461c6fd3e23SJeff Roberson KASSERT(bucket->ub_cnt == 0, 3462c6fd3e23SJeff Roberson ("cache_alloc: Entered with non-empty alloc bucket.")); 34636fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3464c6fd3e23SJeff Roberson } 3465fc03d22bSJeff Roberson 34665d1ae027SRobert Watson /* 34675d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 3468c6fd3e23SJeff Roberson * we must go back to the zone. This requires the zdom lock, so we 34695d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 34705d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 34715d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 34725d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 34735d1ae027SRobert Watson * the critical section. 34745d1ae027SRobert Watson */ 3475c1685086SJeff Roberson domain = PCPU_GET(domain); 34768c277118SMark Johnston if ((cache_uz_flags(cache) & UMA_ZONE_ROUNDROBIN) != 0 || 34778c277118SMark Johnston VM_DOMAIN_EMPTY(domain)) 3478c6fd3e23SJeff Roberson domain = zone_domain_highest(zone, domain); 3479c6fd3e23SJeff Roberson bucket = cache_fetch_bucket(zone, cache, domain); 3480af32cefdSMark Johnston if (bucket == NULL && zone->uz_bucket_size != 0 && !bucketdisable) { 3481beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 3482c6fd3e23SJeff Roberson new = true; 3483af32cefdSMark Johnston } else { 3484c6fd3e23SJeff Roberson new = false; 3485af32cefdSMark Johnston } 3486c6fd3e23SJeff Roberson 34871431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 34881431a748SGleb Smirnoff zone->uz_name, zone, bucket); 34894bd61e19SJeff Roberson if (bucket == NULL) { 3490fc03d22bSJeff Roberson critical_enter(); 3491beb8beefSJeff Roberson return (false); 34924bd61e19SJeff Roberson } 34930f9b7bf3SMark Johnston 3494fc03d22bSJeff Roberson /* 3495fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3496fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3497fc03d22bSJeff Roberson * the memory directly. 3498fc03d22bSJeff Roberson */ 34994bd61e19SJeff Roberson critical_enter(); 3500cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3501376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 3502c6fd3e23SJeff Roberson ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) == 0 || 35038c277118SMark Johnston (curdomain = PCPU_GET(domain)) == domain || 35048c277118SMark Johnston VM_DOMAIN_EMPTY(curdomain))) { 3505c6fd3e23SJeff Roberson if (new) 3506c6fd3e23SJeff Roberson atomic_add_long(&ZDOM_GET(zone, domain)->uzd_imax, 3507c6fd3e23SJeff Roberson bucket->ub_cnt); 3508376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3509beb8beefSJeff Roberson return (true); 3510c6fd3e23SJeff Roberson } 3511c6fd3e23SJeff Roberson 3512c6fd3e23SJeff Roberson /* 3513c6fd3e23SJeff Roberson * We lost the race, release this bucket and start over. 3514c6fd3e23SJeff Roberson */ 3515c6fd3e23SJeff Roberson critical_exit(); 3516c6fd3e23SJeff Roberson zone_put_bucket(zone, domain, bucket, udata, false); 3517c6fd3e23SJeff Roberson critical_enter(); 3518c6fd3e23SJeff Roberson 3519beb8beefSJeff Roberson return (true); 3520bbee39c6SJeff Roberson } 3521bbee39c6SJeff Roberson 3522ab3185d1SJeff Roberson void * 3523ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3524bbee39c6SJeff Roberson { 352506d8bdcbSMark Johnston #ifdef NUMA 352606d8bdcbSMark Johnston uma_bucket_t bucket; 352706d8bdcbSMark Johnston uma_zone_domain_t zdom; 352806d8bdcbSMark Johnston void *item; 352906d8bdcbSMark Johnston #endif 3530ab3185d1SJeff Roberson 3531ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 353219fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3533ab3185d1SJeff Roberson 3534ab3185d1SJeff Roberson /* This is the fast path allocation */ 3535e63a1c2fSRyan Libby CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d", 3536e63a1c2fSRyan Libby zone->uz_name, zone, domain, flags); 3537ab3185d1SJeff Roberson 3538ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3539ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3540ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3541ab3185d1SJeff Roberson } 3542ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3543ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 354406d8bdcbSMark Johnston KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 354506d8bdcbSMark Johnston ("uma_zalloc_domain: called with SMR zone.")); 354606d8bdcbSMark Johnston #ifdef NUMA 354706d8bdcbSMark Johnston KASSERT((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0, 354806d8bdcbSMark Johnston ("uma_zalloc_domain: called with non-FIRSTTOUCH zone.")); 3549ab3185d1SJeff Roberson 355006d8bdcbSMark Johnston if (vm_ndomains == 1) 355106d8bdcbSMark Johnston return (uma_zalloc_arg(zone, udata, flags)); 355206d8bdcbSMark Johnston 355306d8bdcbSMark Johnston /* 355406d8bdcbSMark Johnston * Try to allocate from the bucket cache before falling back to the keg. 355506d8bdcbSMark Johnston * We could try harder and attempt to allocate from per-CPU caches or 355606d8bdcbSMark Johnston * the per-domain cross-domain buckets, but the complexity is probably 355706d8bdcbSMark Johnston * not worth it. It is more important that frees of previous 355806d8bdcbSMark Johnston * cross-domain allocations do not blow up the cache. 355906d8bdcbSMark Johnston */ 356006d8bdcbSMark Johnston zdom = zone_domain_lock(zone, domain); 356106d8bdcbSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) { 356206d8bdcbSMark Johnston item = bucket->ub_bucket[bucket->ub_cnt - 1]; 356306d8bdcbSMark Johnston #ifdef INVARIANTS 356406d8bdcbSMark Johnston bucket->ub_bucket[bucket->ub_cnt - 1] = NULL; 356506d8bdcbSMark Johnston #endif 356606d8bdcbSMark Johnston bucket->ub_cnt--; 356706d8bdcbSMark Johnston zone_put_bucket(zone, domain, bucket, udata, true); 356806d8bdcbSMark Johnston item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, 356906d8bdcbSMark Johnston flags, item); 357006d8bdcbSMark Johnston if (item != NULL) { 357106d8bdcbSMark Johnston KASSERT(item_domain(item) == domain, 357206d8bdcbSMark Johnston ("%s: bucket cache item %p from wrong domain", 357306d8bdcbSMark Johnston __func__, item)); 357406d8bdcbSMark Johnston counter_u64_add(zone->uz_allocs, 1); 357506d8bdcbSMark Johnston } 357606d8bdcbSMark Johnston return (item); 357706d8bdcbSMark Johnston } 357806d8bdcbSMark Johnston ZDOM_UNLOCK(zdom); 3579ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 358006d8bdcbSMark Johnston #else 358106d8bdcbSMark Johnston return (uma_zalloc_arg(zone, udata, flags)); 358206d8bdcbSMark Johnston #endif 3583ab3185d1SJeff Roberson } 3584ab3185d1SJeff Roberson 3585ab3185d1SJeff Roberson /* 3586ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3587ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3588ab3185d1SJeff Roberson * 3589ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3590ab3185d1SJeff Roberson * only 'domain'. 3591ab3185d1SJeff Roberson */ 3592ab3185d1SJeff Roberson static uma_slab_t 3593194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3594ab3185d1SJeff Roberson { 3595ab3185d1SJeff Roberson uma_domain_t dom; 3596bbee39c6SJeff Roberson uma_slab_t slab; 3597ab3185d1SJeff Roberson int start; 3598ab3185d1SJeff Roberson 3599ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3600ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 36018b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3602ab3185d1SJeff Roberson 3603ab3185d1SJeff Roberson slab = NULL; 3604ab3185d1SJeff Roberson start = domain; 3605ab3185d1SJeff Roberson do { 3606ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 36074ab3aee8SMark Johnston if ((slab = LIST_FIRST(&dom->ud_part_slab)) != NULL) 36084ab3aee8SMark Johnston return (slab); 36094ab3aee8SMark Johnston if ((slab = LIST_FIRST(&dom->ud_free_slab)) != NULL) { 3610ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 36114ab3aee8SMark Johnston dom->ud_free_slabs--; 3612ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3613ab3185d1SJeff Roberson return (slab); 3614ab3185d1SJeff Roberson } 3615ab3185d1SJeff Roberson if (rr) 3616ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3617ab3185d1SJeff Roberson } while (domain != start); 3618ab3185d1SJeff Roberson 3619ab3185d1SJeff Roberson return (NULL); 3620ab3185d1SJeff Roberson } 3621ab3185d1SJeff Roberson 36228b987a77SJeff Roberson /* 36238b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 36248b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 36258b987a77SJeff Roberson */ 3626ab3185d1SJeff Roberson static uma_slab_t 3627194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3628ab3185d1SJeff Roberson { 36298b987a77SJeff Roberson uma_slab_t slab; 3630194a979eSMark Johnston uint32_t reserve; 3631099a0e58SBosko Milekic 36328b987a77SJeff Roberson /* HASH has a single free list. */ 363354c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 36348b987a77SJeff Roberson domain = 0; 3635194a979eSMark Johnston 36368b987a77SJeff Roberson KEG_LOCK(keg, domain); 3637194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 36384ab3aee8SMark Johnston if (keg->uk_domain[domain].ud_free_items <= reserve || 36398b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 36408b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3641194a979eSMark Johnston return (NULL); 36428b987a77SJeff Roberson } 36438b987a77SJeff Roberson return (slab); 3644194a979eSMark Johnston } 3645194a979eSMark Johnston 3646194a979eSMark Johnston static uma_slab_t 3647194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3648194a979eSMark Johnston { 3649194a979eSMark Johnston struct vm_domainset_iter di; 3650194a979eSMark Johnston uma_slab_t slab; 3651194a979eSMark Johnston int aflags, domain; 3652194a979eSMark Johnston bool rr; 3653194a979eSMark Johnston 3654194a979eSMark Johnston restart: 3655bbee39c6SJeff Roberson /* 3656194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3657194a979eSMark Johnston * domain (as happens with first-touch zones). 3658194a979eSMark Johnston * 3659194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3660194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3661194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3662bbee39c6SJeff Roberson */ 3663ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3664ab3185d1SJeff Roberson if (rr) { 3665194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3666194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3667194a979eSMark Johnston &aflags); 3668194a979eSMark Johnston } else { 3669194a979eSMark Johnston aflags = flags; 3670194a979eSMark Johnston domain = rdomain; 3671194a979eSMark Johnston } 3672ab3185d1SJeff Roberson 3673194a979eSMark Johnston for (;;) { 3674194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3675584061b4SJeff Roberson if (slab != NULL) 3676bbee39c6SJeff Roberson return (slab); 3677bbee39c6SJeff Roberson 3678bbee39c6SJeff Roberson /* 3679bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3680bbee39c6SJeff Roberson */ 3681bbee39c6SJeff Roberson if (flags & M_NOVM) 3682bbee39c6SJeff Roberson break; 3683bbee39c6SJeff Roberson 368486220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 36858b987a77SJeff Roberson if (slab != NULL) 3686bbee39c6SJeff Roberson return (slab); 36873639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 36883639ac42SJeff Roberson break; 3689194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3690194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 369189d2fb14SKonstantin Belousov vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0); 3692194a979eSMark Johnston goto restart; 369330c5525bSAndrew Gallatin } 3694194a979eSMark Johnston break; 3695194a979eSMark Johnston } 3696ab3185d1SJeff Roberson } 3697ab3185d1SJeff Roberson 3698bbee39c6SJeff Roberson /* 3699bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3700bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3701bbee39c6SJeff Roberson * fail. 3702bbee39c6SJeff Roberson */ 37038b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 3704bbee39c6SJeff Roberson return (slab); 37058b987a77SJeff Roberson 3706ab3185d1SJeff Roberson return (NULL); 3707ab3185d1SJeff Roberson } 3708bbee39c6SJeff Roberson 3709d56368d7SBosko Milekic static void * 37100095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3711bbee39c6SJeff Roberson { 3712ab3185d1SJeff Roberson uma_domain_t dom; 3713bbee39c6SJeff Roberson void *item; 37149b8db4d0SRyan Libby int freei; 3715bbee39c6SJeff Roberson 37168b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3717099a0e58SBosko Milekic 37188b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 37199b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 37209b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 37211e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3722bbee39c6SJeff Roberson slab->us_freecount--; 37234ab3aee8SMark Johnston dom->ud_free_items--; 3724ef72505eSJeff Roberson 37254ab3aee8SMark Johnston /* 37264ab3aee8SMark Johnston * Move this slab to the full list. It must be on the partial list, so 37274ab3aee8SMark Johnston * we do not need to update the free slab count. In particular, 37284ab3aee8SMark Johnston * keg_fetch_slab() always returns slabs on the partial list. 37294ab3aee8SMark Johnston */ 3730bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3731bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3732ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3733bbee39c6SJeff Roberson } 3734bbee39c6SJeff Roberson 3735bbee39c6SJeff Roberson return (item); 3736bbee39c6SJeff Roberson } 3737bbee39c6SJeff Roberson 3738bbee39c6SJeff Roberson static int 3739b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 37400095a784SJeff Roberson { 37418b987a77SJeff Roberson uma_domain_t dom; 3742b75c4efcSAndrew Turner uma_zone_t zone; 37430095a784SJeff Roberson uma_slab_t slab; 37440095a784SJeff Roberson uma_keg_t keg; 3745a03af342SSean Bruno #ifdef NUMA 3746ab3185d1SJeff Roberson int stripe; 3747a03af342SSean Bruno #endif 37480095a784SJeff Roberson int i; 37490095a784SJeff Roberson 3750b75c4efcSAndrew Turner zone = arg; 37510095a784SJeff Roberson slab = NULL; 3752584061b4SJeff Roberson keg = zone->uz_keg; 3753af526374SJeff Roberson /* Try to keep the buckets totally full */ 37540095a784SJeff Roberson for (i = 0; i < max; ) { 3755584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 37560095a784SJeff Roberson break; 3757a03af342SSean Bruno #ifdef NUMA 3758ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3759a03af342SSean Bruno #endif 37608b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 37611b2dcc8cSMark Johnston do { 37620095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 37631b2dcc8cSMark Johnston if (dom->ud_free_items <= keg->uk_reserve) { 37641b2dcc8cSMark Johnston /* 37651b2dcc8cSMark Johnston * Avoid depleting the reserve after a 37661b2dcc8cSMark Johnston * successful item allocation, even if 37671b2dcc8cSMark Johnston * M_USE_RESERVE is specified. 37681b2dcc8cSMark Johnston */ 37691b2dcc8cSMark Johnston KEG_UNLOCK(keg, slab->us_domain); 37701b2dcc8cSMark Johnston goto out; 37711b2dcc8cSMark Johnston } 3772b6715dabSJeff Roberson #ifdef NUMA 3773ab3185d1SJeff Roberson /* 3774ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3775ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3776ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3777ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3778ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3779ab3185d1SJeff Roberson * time but yields better distribution. 3780ab3185d1SJeff Roberson */ 3781dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 && 3782ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3783ab3185d1SJeff Roberson break; 3784ab3185d1SJeff Roberson #endif 37851b2dcc8cSMark Johnston } while (slab->us_freecount != 0 && i < max); 37868b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 37871b2dcc8cSMark Johnston 3788ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 37890095a784SJeff Roberson flags &= ~M_WAITOK; 37900095a784SJeff Roberson flags |= M_NOWAIT; 37910095a784SJeff Roberson } 37921b2dcc8cSMark Johnston out: 37930095a784SJeff Roberson return i; 37940095a784SJeff Roberson } 37950095a784SJeff Roberson 37964bd61e19SJeff Roberson static int 37974bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 37984bd61e19SJeff Roberson { 37994bd61e19SJeff Roberson uint64_t old, new, total, max; 38004bd61e19SJeff Roberson 38014bd61e19SJeff Roberson /* 38024bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 38034bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 38044bd61e19SJeff Roberson * fairness by keeping fifo order. 38054bd61e19SJeff Roberson * 38064bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 38074bd61e19SJeff Roberson */ 38084bd61e19SJeff Roberson for (;;) { 38094bd61e19SJeff Roberson zone_free_limit(zone, count); 38104bd61e19SJeff Roberson zone_log_warning(zone); 38114bd61e19SJeff Roberson zone_maxaction(zone); 38124bd61e19SJeff Roberson if (flags & M_NOWAIT) 38134bd61e19SJeff Roberson return (0); 38144bd61e19SJeff Roberson 38154bd61e19SJeff Roberson /* 38164bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 38174bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 38184bd61e19SJeff Roberson * is essentially a home rolled semaphore. 38194bd61e19SJeff Roberson */ 38204bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 38214bd61e19SJeff Roberson old = zone->uz_items; 38224bd61e19SJeff Roberson do { 38234bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 38244bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 38254bd61e19SJeff Roberson max = zone->uz_max_items; 38264bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 38274bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 38284bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 38294bd61e19SJeff Roberson else 38304bd61e19SJeff Roberson new = old + MIN(count, max - old); 38314bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 38324bd61e19SJeff Roberson 38334bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 38344bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 38354bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 38364bd61e19SJeff Roberson return (new - old); 38374bd61e19SJeff Roberson } 38384bd61e19SJeff Roberson 38394bd61e19SJeff Roberson /* 38404bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 38414bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 38424bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 38434bd61e19SJeff Roberson * transitions. 38444bd61e19SJeff Roberson */ 38454bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 38464bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 38474bd61e19SJeff Roberson 38484bd61e19SJeff Roberson /* 38494bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 38504bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 38514bd61e19SJeff Roberson */ 38524bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 38534bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 38544bd61e19SJeff Roberson 38554bd61e19SJeff Roberson /* 38564bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 38574bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 38584bd61e19SJeff Roberson * 38594bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 38604bd61e19SJeff Roberson * our count. 38614bd61e19SJeff Roberson */ 38624bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 38634bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 38644bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 38654bd61e19SJeff Roberson /* We're no longer a sleeper. */ 38664bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 38674bd61e19SJeff Roberson 38684bd61e19SJeff Roberson /* 38694bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 38704bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 38714bd61e19SJeff Roberson * against changes via sysctl. 38724bd61e19SJeff Roberson */ 38734bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 38744bd61e19SJeff Roberson max = zone->uz_max_items; 38754bd61e19SJeff Roberson if (total >= max) 38764bd61e19SJeff Roberson continue; 38774bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 38784bd61e19SJeff Roberson if (total + count > max) { 38794bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 38804bd61e19SJeff Roberson count = max - total; 38814bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 38824bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 38834bd61e19SJeff Roberson 38844bd61e19SJeff Roberson return (count); 38854bd61e19SJeff Roberson } 38864bd61e19SJeff Roberson } 38874bd61e19SJeff Roberson 38884bd61e19SJeff Roberson /* 38894bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 38904bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 38914bd61e19SJeff Roberson * one item can be allocated. 38924bd61e19SJeff Roberson */ 38934bd61e19SJeff Roberson static int 38944bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 38954bd61e19SJeff Roberson { 38964bd61e19SJeff Roberson uint64_t old; 38974bd61e19SJeff Roberson uint64_t max; 38984bd61e19SJeff Roberson 38994bd61e19SJeff Roberson max = zone->uz_max_items; 39004bd61e19SJeff Roberson MPASS(max > 0); 39014bd61e19SJeff Roberson 39024bd61e19SJeff Roberson /* 39034bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 39044bd61e19SJeff Roberson * fetchadd. 39054bd61e19SJeff Roberson */ 39064bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 39074bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 39084bd61e19SJeff Roberson return (count); 39094bd61e19SJeff Roberson 39104bd61e19SJeff Roberson /* 39114bd61e19SJeff Roberson * If we had some items and no sleepers just return the 39124bd61e19SJeff Roberson * truncated value. We have to release the excess space 39134bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 39144bd61e19SJeff Roberson * because we were temporarily over the limit. 39154bd61e19SJeff Roberson */ 39164bd61e19SJeff Roberson if (old < max) { 39174bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 39184bd61e19SJeff Roberson return (max - old); 39194bd61e19SJeff Roberson } 39204bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 39214bd61e19SJeff Roberson } 39224bd61e19SJeff Roberson 39234bd61e19SJeff Roberson /* 39244bd61e19SJeff Roberson * Free a number of items back to the limit. 39254bd61e19SJeff Roberson */ 39264bd61e19SJeff Roberson static void 39274bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 39284bd61e19SJeff Roberson { 39294bd61e19SJeff Roberson uint64_t old; 39304bd61e19SJeff Roberson 39314bd61e19SJeff Roberson MPASS(count > 0); 39324bd61e19SJeff Roberson 39334bd61e19SJeff Roberson /* 39344bd61e19SJeff Roberson * In the common case we either have no sleepers or 39354bd61e19SJeff Roberson * are still over the limit and can just return. 39364bd61e19SJeff Roberson */ 39374bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 39384bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 39394bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 39404bd61e19SJeff Roberson return; 39414bd61e19SJeff Roberson 39424bd61e19SJeff Roberson /* 39434bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 39444bd61e19SJeff Roberson * to generate wakeups if necessary. 39454bd61e19SJeff Roberson */ 39464bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 39474bd61e19SJeff Roberson } 39484bd61e19SJeff Roberson 3949fc03d22bSJeff Roberson static uma_bucket_t 3950beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3951bbee39c6SJeff Roberson { 3952bbee39c6SJeff Roberson uma_bucket_t bucket; 3953beb8beefSJeff Roberson int maxbucket, cnt; 3954bbee39c6SJeff Roberson 3955e63a1c2fSRyan Libby CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name, 3956e63a1c2fSRyan Libby zone, domain); 395730c5525bSAndrew Gallatin 3958c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3959c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3960c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 39618c277118SMark Johnston else if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0) 3962c6fd3e23SJeff Roberson domain = UMA_ANYDOMAIN; 3963c1685086SJeff Roberson 39644bd61e19SJeff Roberson if (zone->uz_max_items > 0) 39654bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 39664bd61e19SJeff Roberson M_NOWAIT); 39674bd61e19SJeff Roberson else 396820a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 39694bd61e19SJeff Roberson if (maxbucket == 0) 39704bd61e19SJeff Roberson return (false); 3971beb8beefSJeff Roberson 39726fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 39736fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3974beb8beefSJeff Roberson if (bucket == NULL) { 3975beb8beefSJeff Roberson cnt = 0; 3976beb8beefSJeff Roberson goto out; 3977beb8beefSJeff Roberson } 39780095a784SJeff Roberson 39790095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3980beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 39810095a784SJeff Roberson 39820095a784SJeff Roberson /* 39830095a784SJeff Roberson * Initialize the memory if necessary. 39840095a784SJeff Roberson */ 39850095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 3986099a0e58SBosko Milekic int i; 3987bbee39c6SJeff Roberson 39880095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 3989e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 39900095a784SJeff Roberson flags) != 0) 3991b23f72e9SBrian Feldman break; 3992b23f72e9SBrian Feldman /* 3993b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 3994b23f72e9SBrian Feldman * rest back onto the freelist. 3995b23f72e9SBrian Feldman */ 3996b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 3997af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 39980095a784SJeff Roberson bucket->ub_cnt - i); 3999a5a262c6SBosko Milekic #ifdef INVARIANTS 40000095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 40010095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 4002a5a262c6SBosko Milekic #endif 4003b23f72e9SBrian Feldman bucket->ub_cnt = i; 4004b23f72e9SBrian Feldman } 4005099a0e58SBosko Milekic } 4006099a0e58SBosko Milekic 4007beb8beefSJeff Roberson cnt = bucket->ub_cnt; 4008f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 40096fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 40102efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 4011beb8beefSJeff Roberson bucket = NULL; 4012beb8beefSJeff Roberson } 4013beb8beefSJeff Roberson out: 40144bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 40154bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 4016fc03d22bSJeff Roberson 4017fc03d22bSJeff Roberson return (bucket); 4018fc03d22bSJeff Roberson } 4019fc03d22bSJeff Roberson 40208355f576SJeff Roberson /* 40210095a784SJeff Roberson * Allocates a single item from a zone. 40228355f576SJeff Roberson * 40238355f576SJeff Roberson * Arguments 40248355f576SJeff Roberson * zone The zone to alloc for. 40258355f576SJeff Roberson * udata The data to be passed to the constructor. 4026ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 4027a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 40288355f576SJeff Roberson * 40298355f576SJeff Roberson * Returns 40308355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 4031bbee39c6SJeff Roberson * An item if successful 40328355f576SJeff Roberson */ 40338355f576SJeff Roberson 40348355f576SJeff Roberson static void * 4035ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 40368355f576SJeff Roberson { 40378355f576SJeff Roberson void *item; 40388355f576SJeff Roberson 4039791dda87SAndrew Gallatin if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) { 4040791dda87SAndrew Gallatin counter_u64_add(zone->uz_fails, 1); 4041bb15d1c7SGleb Smirnoff return (NULL); 4042791dda87SAndrew Gallatin } 40438355f576SJeff Roberson 4044c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 4045c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 404630c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 4047c1685086SJeff Roberson 4048ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 4049beb8beefSJeff Roberson goto fail_cnt; 40508355f576SJeff Roberson 4051099a0e58SBosko Milekic /* 4052099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 4053099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 4054099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 4055099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 4056099a0e58SBosko Milekic */ 4057b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 4058e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 4059bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 4060beb8beefSJeff Roberson goto fail_cnt; 4061beb8beefSJeff Roberson } 4062beb8beefSJeff Roberson } 4063d4665eaaSJeff Roberson item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags, 4064d4665eaaSJeff Roberson item); 4065beb8beefSJeff Roberson if (item == NULL) 40660095a784SJeff Roberson goto fail; 40678355f576SJeff Roberson 40682efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 40691431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 40701431a748SGleb Smirnoff zone->uz_name, zone); 40711431a748SGleb Smirnoff 40728355f576SJeff Roberson return (item); 40730095a784SJeff Roberson 4074beb8beefSJeff Roberson fail_cnt: 4075beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 40760095a784SJeff Roberson fail: 40774bd61e19SJeff Roberson if (zone->uz_max_items > 0) 40784bd61e19SJeff Roberson zone_free_limit(zone, 1); 40791431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 40801431a748SGleb Smirnoff zone->uz_name, zone); 40814bd61e19SJeff Roberson 40820095a784SJeff Roberson return (NULL); 40838355f576SJeff Roberson } 40848355f576SJeff Roberson 40858355f576SJeff Roberson /* See uma.h */ 40868355f576SJeff Roberson void 4087d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item) 4088d4665eaaSJeff Roberson { 4089d4665eaaSJeff Roberson uma_cache_t cache; 4090d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 4091c6fd3e23SJeff Roberson int itemdomain, uz_flags; 4092d4665eaaSJeff Roberson 4093d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 4094d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 4095952c8964SMark Johnston ("uma_zfree_smr: called with non-SMR zone.")); 4096d4665eaaSJeff Roberson KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer.")); 4097c6fd3e23SJeff Roberson SMR_ASSERT_NOT_ENTERED(zone->uz_smr); 4098d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN) 4099d4665eaaSJeff Roberson return; 4100d4665eaaSJeff Roberson #endif 4101d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4102d4665eaaSJeff Roberson uz_flags = cache_uz_flags(cache); 4103c6fd3e23SJeff Roberson itemdomain = 0; 4104d4665eaaSJeff Roberson #ifdef NUMA 4105d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 410681302f1dSMark Johnston itemdomain = item_domain(item); 4107d4665eaaSJeff Roberson #endif 4108d4665eaaSJeff Roberson critical_enter(); 4109d4665eaaSJeff Roberson do { 4110d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4111d4665eaaSJeff Roberson /* SMR Zones must free to the free bucket. */ 4112d4665eaaSJeff Roberson bucket = &cache->uc_freebucket; 4113d4665eaaSJeff Roberson #ifdef NUMA 4114d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4115c6fd3e23SJeff Roberson PCPU_GET(domain) != itemdomain) { 4116d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 4117d4665eaaSJeff Roberson } 4118d4665eaaSJeff Roberson #endif 4119d4665eaaSJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 4120d4665eaaSJeff Roberson cache_bucket_push(cache, bucket, item); 4121d4665eaaSJeff Roberson critical_exit(); 4122d4665eaaSJeff Roberson return; 4123d4665eaaSJeff Roberson } 4124d4665eaaSJeff Roberson } while (cache_free(zone, cache, NULL, item, itemdomain)); 4125d4665eaaSJeff Roberson critical_exit(); 4126d4665eaaSJeff Roberson 4127d4665eaaSJeff Roberson /* 4128d4665eaaSJeff Roberson * If nothing else caught this, we'll just do an internal free. 4129d4665eaaSJeff Roberson */ 4130d4665eaaSJeff Roberson zone_free_item(zone, item, NULL, SKIP_NONE); 4131d4665eaaSJeff Roberson } 4132d4665eaaSJeff Roberson 4133d4665eaaSJeff Roberson /* See uma.h */ 4134d4665eaaSJeff Roberson void 41358355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 41368355f576SJeff Roberson { 41378355f576SJeff Roberson uma_cache_t cache; 4138376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 4139c6fd3e23SJeff Roberson int itemdomain, uz_flags; 41408355f576SJeff Roberson 4141e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 414219fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 414310cb2424SMark Murray 4144e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone); 41453659f747SRobert Watson 4146d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 4147d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 4148952c8964SMark Johnston ("uma_zfree_arg: called with SMR zone.")); 4149d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN) 4150d4665eaaSJeff Roberson return; 4151d4665eaaSJeff Roberson #endif 415220ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 415320ed0cb0SMatthew D Fleming if (item == NULL) 415420ed0cb0SMatthew D Fleming return; 4155cc7ce83aSJeff Roberson 4156cc7ce83aSJeff Roberson /* 4157cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 4158cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 4159cc7ce83aSJeff Roberson * will simply read another cpu's line. 4160cc7ce83aSJeff Roberson */ 4161cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4162cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 4163d4665eaaSJeff Roberson if (UMA_ALWAYS_CTORDTOR || 4164d4665eaaSJeff Roberson __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0)) 4165cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 4166ef72505eSJeff Roberson 4167af7f9b97SJeff Roberson /* 4168af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 4169af7f9b97SJeff Roberson * a little longer for the limits to be reset. 4170af7f9b97SJeff Roberson */ 4171cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 41728a6776caSMark Johnston if (atomic_load_32(&zone->uz_sleepers) > 0) 4173fc03d22bSJeff Roberson goto zfree_item; 4174cc7ce83aSJeff Roberson } 4175af7f9b97SJeff Roberson 41765d1ae027SRobert Watson /* 41775d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 41785d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 41795d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 41805d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 41815d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 41825d1ae027SRobert Watson * preemption and migration. We release the critical section in 41835d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 41845d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 41855d1ae027SRobert Watson * detect and handle migration if it has occurred. 41865d1ae027SRobert Watson */ 4187c6fd3e23SJeff Roberson itemdomain = 0; 4188dfe13344SJeff Roberson #ifdef NUMA 4189dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 419081302f1dSMark Johnston itemdomain = item_domain(item); 4191dfe13344SJeff Roberson #endif 41925d1ae027SRobert Watson critical_enter(); 41930a81b439SJeff Roberson do { 4194cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4195a553d4b8SJeff Roberson /* 4196dfe13344SJeff Roberson * Try to free into the allocbucket first to give LIFO 4197dfe13344SJeff Roberson * ordering for cache-hot datastructures. Spill over 4198dfe13344SJeff Roberson * into the freebucket if necessary. Alloc will swap 4199dfe13344SJeff Roberson * them if one runs dry. 4200a553d4b8SJeff Roberson */ 4201dfe13344SJeff Roberson bucket = &cache->uc_allocbucket; 4202d4665eaaSJeff Roberson #ifdef NUMA 4203d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4204c6fd3e23SJeff Roberson PCPU_GET(domain) != itemdomain) { 4205d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 4206d4665eaaSJeff Roberson } else 4207d4665eaaSJeff Roberson #endif 4208fe835cbfSJeff Roberson if (bucket->ucb_cnt == bucket->ucb_entries && 4209fe835cbfSJeff Roberson cache->uc_freebucket.ucb_cnt < 4210fe835cbfSJeff Roberson cache->uc_freebucket.ucb_entries) 4211fe835cbfSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 4212fe835cbfSJeff Roberson &cache->uc_allocbucket); 4213376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 4214376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 42155d1ae027SRobert Watson critical_exit(); 42168355f576SJeff Roberson return; 4217fc03d22bSJeff Roberson } 42180a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 42190a81b439SJeff Roberson critical_exit(); 4220fc03d22bSJeff Roberson 42218355f576SJeff Roberson /* 42220a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 42238355f576SJeff Roberson */ 42240a81b439SJeff Roberson zfree_item: 42250a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 42260a81b439SJeff Roberson } 4227fc03d22bSJeff Roberson 4228dfe13344SJeff Roberson #ifdef NUMA 422991d947bfSJeff Roberson /* 423091d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 423191d947bfSJeff Roberson * them. 423291d947bfSJeff Roberson */ 423391d947bfSJeff Roberson static void 423491d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 423591d947bfSJeff Roberson { 4236991f23efSMark Johnston struct uma_bucketlist emptybuckets, fullbuckets; 423791d947bfSJeff Roberson uma_zone_domain_t zdom; 423891d947bfSJeff Roberson uma_bucket_t b; 4239543117beSJeff Roberson smr_seq_t seq; 424091d947bfSJeff Roberson void *item; 424191d947bfSJeff Roberson int domain; 424291d947bfSJeff Roberson 424391d947bfSJeff Roberson CTR3(KTR_UMA, 424491d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 424591d947bfSJeff Roberson zone->uz_name, zone, bucket); 424691d947bfSJeff Roberson 4247543117beSJeff Roberson /* 4248543117beSJeff Roberson * It is possible for buckets to arrive here out of order so we fetch 4249543117beSJeff Roberson * the current smr seq rather than accepting the bucket's. 4250543117beSJeff Roberson */ 4251543117beSJeff Roberson seq = SMR_SEQ_INVALID; 4252543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 4253226dd6dbSJeff Roberson seq = smr_advance(zone->uz_smr); 4254226dd6dbSJeff Roberson 4255226dd6dbSJeff Roberson /* 4256226dd6dbSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 4257226dd6dbSJeff Roberson * lock on the current crossfree bucket. A full matrix with 4258226dd6dbSJeff Roberson * per-domain locking could be used if necessary. 4259226dd6dbSJeff Roberson */ 4260991f23efSMark Johnston STAILQ_INIT(&emptybuckets); 4261226dd6dbSJeff Roberson STAILQ_INIT(&fullbuckets); 4262226dd6dbSJeff Roberson ZONE_CROSS_LOCK(zone); 4263991f23efSMark Johnston for (; bucket->ub_cnt > 0; bucket->ub_cnt--) { 426491d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 426581302f1dSMark Johnston domain = item_domain(item); 4266c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 426791d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 4268991f23efSMark Johnston if ((b = STAILQ_FIRST(&emptybuckets)) != NULL) { 4269991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4270991f23efSMark Johnston zdom->uzd_cross = b; 4271991f23efSMark Johnston } else { 4272991f23efSMark Johnston /* 4273991f23efSMark Johnston * Avoid allocating a bucket with the cross lock 4274991f23efSMark Johnston * held, since allocation can trigger a 4275991f23efSMark Johnston * cross-domain free and bucket zones may 4276991f23efSMark Johnston * allocate from each other. 4277991f23efSMark Johnston */ 4278991f23efSMark Johnston ZONE_CROSS_UNLOCK(zone); 4279991f23efSMark Johnston b = bucket_alloc(zone, udata, M_NOWAIT); 4280991f23efSMark Johnston if (b == NULL) 4281991f23efSMark Johnston goto out; 4282991f23efSMark Johnston ZONE_CROSS_LOCK(zone); 4283991f23efSMark Johnston if (zdom->uzd_cross != NULL) { 4284991f23efSMark Johnston STAILQ_INSERT_HEAD(&emptybuckets, b, 4285991f23efSMark Johnston ub_link); 4286991f23efSMark Johnston } else { 4287991f23efSMark Johnston zdom->uzd_cross = b; 4288991f23efSMark Johnston } 4289991f23efSMark Johnston } 429091d947bfSJeff Roberson } 4291543117beSJeff Roberson b = zdom->uzd_cross; 4292543117beSJeff Roberson b->ub_bucket[b->ub_cnt++] = item; 4293543117beSJeff Roberson b->ub_seq = seq; 4294543117beSJeff Roberson if (b->ub_cnt == b->ub_entries) { 4295543117beSJeff Roberson STAILQ_INSERT_HEAD(&fullbuckets, b, ub_link); 4296991f23efSMark Johnston if ((b = STAILQ_FIRST(&emptybuckets)) != NULL) 4297991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4298991f23efSMark Johnston zdom->uzd_cross = b; 429991d947bfSJeff Roberson } 430091d947bfSJeff Roberson } 430191d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 4302991f23efSMark Johnston out: 4303c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 4304d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 430591d947bfSJeff Roberson bucket_free(zone, bucket, udata); 4306c6fd3e23SJeff Roberson 4307991f23efSMark Johnston while ((b = STAILQ_FIRST(&emptybuckets)) != NULL) { 4308991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4309991f23efSMark Johnston bucket_free(zone, b, udata); 4310991f23efSMark Johnston } 4311c6fd3e23SJeff Roberson while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) { 4312c6fd3e23SJeff Roberson STAILQ_REMOVE_HEAD(&fullbuckets, ub_link); 431381302f1dSMark Johnston domain = item_domain(b->ub_bucket[0]); 4314c6fd3e23SJeff Roberson zone_put_bucket(zone, domain, b, udata, true); 4315c6fd3e23SJeff Roberson } 431691d947bfSJeff Roberson } 431791d947bfSJeff Roberson #endif 431891d947bfSJeff Roberson 43190a81b439SJeff Roberson static void 43200a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 4321c6fd3e23SJeff Roberson int itemdomain, bool ws) 43220a81b439SJeff Roberson { 43230a81b439SJeff Roberson 4324dfe13344SJeff Roberson #ifdef NUMA 43250a81b439SJeff Roberson /* 43260a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 43270a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 43280a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 432991d947bfSJeff Roberson * correct domains. 43300a81b439SJeff Roberson */ 4331c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4332c6fd3e23SJeff Roberson vm_ndomains > 2 && PCPU_GET(domain) != itemdomain) { 433391d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 43340a81b439SJeff Roberson return; 43350a81b439SJeff Roberson } 43360a81b439SJeff Roberson #endif 433791d947bfSJeff Roberson 43380a81b439SJeff Roberson /* 43390a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 43400a81b439SJeff Roberson */ 43410a81b439SJeff Roberson CTR3(KTR_UMA, 43420a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 43430a81b439SJeff Roberson zone->uz_name, zone, bucket); 43440a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 4345c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0) 4346c6fd3e23SJeff Roberson itemdomain = zone_domain_lowest(zone, itemdomain); 4347c6fd3e23SJeff Roberson zone_put_bucket(zone, itemdomain, bucket, udata, ws); 43488355f576SJeff Roberson } 4349fc03d22bSJeff Roberson 43504d104ba0SAlexander Motin /* 43510a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 43520a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 43530a81b439SJeff Roberson * 43540a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 43550a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 43560a81b439SJeff Roberson * the caller should retry. 43574d104ba0SAlexander Motin */ 43580a81b439SJeff Roberson static __noinline bool 43590a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 43600a81b439SJeff Roberson int itemdomain) 43610a81b439SJeff Roberson { 4362dfe13344SJeff Roberson uma_cache_bucket_t cbucket; 4363d4665eaaSJeff Roberson uma_bucket_t newbucket, bucket; 43640a81b439SJeff Roberson 43650a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 43660a81b439SJeff Roberson 4367d4665eaaSJeff Roberson if (zone->uz_bucket_size == 0) 43680a81b439SJeff Roberson return false; 43690a81b439SJeff Roberson 4370cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4371d4665eaaSJeff Roberson newbucket = NULL; 43720a81b439SJeff Roberson 43730a81b439SJeff Roberson /* 4374dfe13344SJeff Roberson * FIRSTTOUCH domains need to free to the correct zdom. When 4375dfe13344SJeff Roberson * enabled this is the zdom of the item. The bucket is the 4376dfe13344SJeff Roberson * cross bucket if the current domain and itemdomain do not match. 43770a81b439SJeff Roberson */ 4378dfe13344SJeff Roberson cbucket = &cache->uc_freebucket; 4379dfe13344SJeff Roberson #ifdef NUMA 4380c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) { 4381c6fd3e23SJeff Roberson if (PCPU_GET(domain) != itemdomain) { 4382dfe13344SJeff Roberson cbucket = &cache->uc_crossbucket; 4383dfe13344SJeff Roberson if (cbucket->ucb_cnt != 0) 4384c6fd3e23SJeff Roberson counter_u64_add(zone->uz_xdomain, 4385dfe13344SJeff Roberson cbucket->ucb_cnt); 4386dfe13344SJeff Roberson } 4387c6fd3e23SJeff Roberson } 43880a81b439SJeff Roberson #endif 4389dfe13344SJeff Roberson bucket = cache_bucket_unload(cbucket); 4390c6fd3e23SJeff Roberson KASSERT(bucket == NULL || bucket->ub_cnt == bucket->ub_entries, 4391c6fd3e23SJeff Roberson ("cache_free: Entered with non-full free bucket.")); 43920a81b439SJeff Roberson 43930a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 43940a81b439SJeff Roberson critical_exit(); 43950a81b439SJeff Roberson 4396d4665eaaSJeff Roberson /* 4397d4665eaaSJeff Roberson * Don't let SMR zones operate without a free bucket. Force 4398d4665eaaSJeff Roberson * a synchronize and re-use this one. We will only degrade 4399d4665eaaSJeff Roberson * to a synchronize every bucket_size items rather than every 4400d4665eaaSJeff Roberson * item if we fail to allocate a bucket. 4401d4665eaaSJeff Roberson */ 4402d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) { 4403d4665eaaSJeff Roberson if (bucket != NULL) 4404d4665eaaSJeff Roberson bucket->ub_seq = smr_advance(zone->uz_smr); 4405d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4406d4665eaaSJeff Roberson if (newbucket == NULL && bucket != NULL) { 4407d4665eaaSJeff Roberson bucket_drain(zone, bucket); 4408d4665eaaSJeff Roberson newbucket = bucket; 4409d4665eaaSJeff Roberson bucket = NULL; 4410d4665eaaSJeff Roberson } 4411d4665eaaSJeff Roberson } else if (!bucketdisable) 4412d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4413d4665eaaSJeff Roberson 44140a81b439SJeff Roberson if (bucket != NULL) 4415c6fd3e23SJeff Roberson zone_free_bucket(zone, bucket, udata, itemdomain, true); 4416a553d4b8SJeff Roberson 4417fc03d22bSJeff Roberson critical_enter(); 4418d4665eaaSJeff Roberson if ((bucket = newbucket) == NULL) 44190a81b439SJeff Roberson return (false); 4420cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4421dfe13344SJeff Roberson #ifdef NUMA 4422fc03d22bSJeff Roberson /* 44230a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 44240a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 44250a81b439SJeff Roberson * the free bucket. 4426fc03d22bSJeff Roberson */ 4427c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) { 4428c6fd3e23SJeff Roberson if (PCPU_GET(domain) != itemdomain && 4429376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 4430376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 44310a81b439SJeff Roberson return (true); 44320a81b439SJeff Roberson } 44330a81b439SJeff Roberson } 44340a81b439SJeff Roberson #endif 44350a81b439SJeff Roberson /* 44360a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 44370a81b439SJeff Roberson */ 4438376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 4439fc03d22bSJeff Roberson critical_exit(); 44406fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 44410a81b439SJeff Roberson critical_enter(); 44420a81b439SJeff Roberson } else 4443376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 44448355f576SJeff Roberson 44450a81b439SJeff Roberson return (true); 44468355f576SJeff Roberson } 44478355f576SJeff Roberson 44488355f576SJeff Roberson static void 4449bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 44508355f576SJeff Roberson { 4451bb15d1c7SGleb Smirnoff uma_keg_t keg; 4452ab3185d1SJeff Roberson uma_domain_t dom; 44539b8db4d0SRyan Libby int freei; 4454099a0e58SBosko Milekic 4455bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 44568b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 4457ab3185d1SJeff Roberson 44588355f576SJeff Roberson /* Do we need to remove from any lists? */ 44598b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 4460099a0e58SBosko Milekic if (slab->us_freecount + 1 == keg->uk_ipers) { 44618355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4462ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 44634ab3aee8SMark Johnston dom->ud_free_slabs++; 44648355f576SJeff Roberson } else if (slab->us_freecount == 0) { 44658355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4466ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 44678355f576SJeff Roberson } 44688355f576SJeff Roberson 4469ef72505eSJeff Roberson /* Slab management. */ 44701e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 44719b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 44728355f576SJeff Roberson slab->us_freecount++; 44738355f576SJeff Roberson 4474ef72505eSJeff Roberson /* Keg statistics. */ 44754ab3aee8SMark Johnston dom->ud_free_items++; 44760095a784SJeff Roberson } 44770095a784SJeff Roberson 44780095a784SJeff Roberson static void 4479b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 44800095a784SJeff Roberson { 44818b987a77SJeff Roberson struct mtx *lock; 4482b75c4efcSAndrew Turner uma_zone_t zone; 44830095a784SJeff Roberson uma_slab_t slab; 44840095a784SJeff Roberson uma_keg_t keg; 44850095a784SJeff Roberson uint8_t *mem; 44868b987a77SJeff Roberson void *item; 44870095a784SJeff Roberson int i; 44888355f576SJeff Roberson 4489b75c4efcSAndrew Turner zone = arg; 4490bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 44918b987a77SJeff Roberson lock = NULL; 449254c5ae80SRyan Libby if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0)) 44938b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 44940095a784SJeff Roberson for (i = 0; i < cnt; i++) { 44950095a784SJeff Roberson item = bucket[i]; 449654c5ae80SRyan Libby if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) { 44970095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 44988b987a77SJeff Roberson } else { 44998b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 450054c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0) 45018b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 45028b987a77SJeff Roberson else 45038b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 45048b987a77SJeff Roberson } 45058b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 45068b987a77SJeff Roberson if (lock != NULL) 45078b987a77SJeff Roberson mtx_unlock(lock); 45088b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 45098b987a77SJeff Roberson } 4510bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 45110095a784SJeff Roberson } 45128b987a77SJeff Roberson if (lock != NULL) 45138b987a77SJeff Roberson mtx_unlock(lock); 45148355f576SJeff Roberson } 45158355f576SJeff Roberson 45160095a784SJeff Roberson /* 45170095a784SJeff Roberson * Frees a single item to any zone. 45180095a784SJeff Roberson * 45190095a784SJeff Roberson * Arguments: 45200095a784SJeff Roberson * zone The zone to free to 45210095a784SJeff Roberson * item The item we're freeing 45220095a784SJeff Roberson * udata User supplied data for the dtor 45230095a784SJeff Roberson * skip Skip dtors and finis 45240095a784SJeff Roberson */ 45256d88d784SJeff Roberson static __noinline void 45260095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 45270095a784SJeff Roberson { 4528c5deaf04SGleb Smirnoff 4529d4665eaaSJeff Roberson /* 4530d4665eaaSJeff Roberson * If a free is sent directly to an SMR zone we have to 4531d4665eaaSJeff Roberson * synchronize immediately because the item can instantly 4532d4665eaaSJeff Roberson * be reallocated. This should only happen in degenerate 4533d4665eaaSJeff Roberson * cases when no memory is available for per-cpu caches. 4534d4665eaaSJeff Roberson */ 4535d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE) 4536d4665eaaSJeff Roberson smr_synchronize(zone->uz_smr); 4537d4665eaaSJeff Roberson 4538cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 45390095a784SJeff Roberson 45400095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 45410095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 45420095a784SJeff Roberson 45430095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4544bb15d1c7SGleb Smirnoff 4545bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4546bb15d1c7SGleb Smirnoff return; 4547bb15d1c7SGleb Smirnoff 45482efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 45492efcc8cbSGleb Smirnoff 45504bd61e19SJeff Roberson if (zone->uz_max_items > 0) 45514bd61e19SJeff Roberson zone_free_limit(zone, 1); 4552bb45b411SGleb Smirnoff } 45530095a784SJeff Roberson 45548355f576SJeff Roberson /* See uma.h */ 45551c6cae97SLawrence Stewart int 4556736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4557736ee590SJeff Roberson { 4558e574d407SMark Johnston 4559e574d407SMark Johnston /* 4560e574d407SMark Johnston * If the limit is small, we may need to constrain the maximum per-CPU 4561e574d407SMark Johnston * cache size, or disable caching entirely. 4562e574d407SMark Johnston */ 4563e574d407SMark Johnston uma_zone_set_maxcache(zone, nitems); 4564bb15d1c7SGleb Smirnoff 45654bd61e19SJeff Roberson /* 45664bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 45674bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 45684bd61e19SJeff Roberson * way to clear a limit. 45694bd61e19SJeff Roberson */ 4570bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4571bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4572cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4573cc7ce83aSJeff Roberson zone_update_caches(zone); 45744bd61e19SJeff Roberson /* We may need to wake waiters. */ 45754bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4576bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4577bb15d1c7SGleb Smirnoff 4578bb15d1c7SGleb Smirnoff return (nitems); 4579bb15d1c7SGleb Smirnoff } 4580bb15d1c7SGleb Smirnoff 4581bb15d1c7SGleb Smirnoff /* See uma.h */ 4582003cf08bSMark Johnston void 4583bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4584bb15d1c7SGleb Smirnoff { 4585e574d407SMark Johnston int bpcpu, bpdom, bsize, nb; 4586bb15d1c7SGleb Smirnoff 4587bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4588e574d407SMark Johnston 4589e574d407SMark Johnston /* 4590e574d407SMark Johnston * Compute a lower bound on the number of items that may be cached in 4591e574d407SMark Johnston * the zone. Each CPU gets at least two buckets, and for cross-domain 4592e574d407SMark Johnston * frees we use an additional bucket per CPU and per domain. Select the 4593e574d407SMark Johnston * largest bucket size that does not exceed half of the requested limit, 4594e574d407SMark Johnston * with the left over space given to the full bucket cache. 4595e574d407SMark Johnston */ 4596e574d407SMark Johnston bpdom = 0; 4597003cf08bSMark Johnston bpcpu = 2; 4598e574d407SMark Johnston #ifdef NUMA 4599e574d407SMark Johnston if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && vm_ndomains > 1) { 4600003cf08bSMark Johnston bpcpu++; 4601e574d407SMark Johnston bpdom++; 4602003cf08bSMark Johnston } 4603e574d407SMark Johnston #endif 4604e574d407SMark Johnston nb = bpcpu * mp_ncpus + bpdom * vm_ndomains; 4605e574d407SMark Johnston bsize = nitems / nb / 2; 4606e574d407SMark Johnston if (bsize > BUCKET_MAX) 4607e574d407SMark Johnston bsize = BUCKET_MAX; 4608e574d407SMark Johnston else if (bsize == 0 && nitems / nb > 0) 4609e574d407SMark Johnston bsize = 1; 4610e574d407SMark Johnston zone->uz_bucket_size_max = zone->uz_bucket_size = bsize; 461120a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 461220a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4613e574d407SMark Johnston zone->uz_bucket_max = nitems - nb * bsize; 4614bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4615736ee590SJeff Roberson } 4616736ee590SJeff Roberson 4617736ee590SJeff Roberson /* See uma.h */ 4618e49471b0SAndre Oppermann int 4619e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4620e49471b0SAndre Oppermann { 4621e49471b0SAndre Oppermann int nitems; 4622e49471b0SAndre Oppermann 4623727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4624e49471b0SAndre Oppermann 4625e49471b0SAndre Oppermann return (nitems); 4626e49471b0SAndre Oppermann } 4627e49471b0SAndre Oppermann 4628e49471b0SAndre Oppermann /* See uma.h */ 46292f891cd5SPawel Jakub Dawidek void 46302f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 46312f891cd5SPawel Jakub Dawidek { 46322f891cd5SPawel Jakub Dawidek 4633727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 46342f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 46352f891cd5SPawel Jakub Dawidek } 46362f891cd5SPawel Jakub Dawidek 46372f891cd5SPawel Jakub Dawidek /* See uma.h */ 463854503a13SJonathan T. Looney void 463954503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 464054503a13SJonathan T. Looney { 464154503a13SJonathan T. Looney 4642727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4643e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 464454503a13SJonathan T. Looney } 464554503a13SJonathan T. Looney 464654503a13SJonathan T. Looney /* See uma.h */ 4647c4ae7908SLawrence Stewart int 4648c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4649c4ae7908SLawrence Stewart { 4650c4ae7908SLawrence Stewart int64_t nitems; 4651c4ae7908SLawrence Stewart u_int i; 4652c4ae7908SLawrence Stewart 4653bfb6b7a1SJeff Roberson nitems = 0; 4654bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER) 46552efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 46562efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4657727c6918SJeff Roberson CPU_FOREACH(i) 4658727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4659727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4660c4ae7908SLawrence Stewart 4661c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4662c4ae7908SLawrence Stewart } 4663c4ae7908SLawrence Stewart 466420a4e154SJeff Roberson static uint64_t 466520a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 466620a4e154SJeff Roberson { 466720a4e154SJeff Roberson uint64_t nitems; 466820a4e154SJeff Roberson u_int i; 466920a4e154SJeff Roberson 4670bfb6b7a1SJeff Roberson nitems = 0; 4671bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER) 467220a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4673727c6918SJeff Roberson CPU_FOREACH(i) 4674727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 467520a4e154SJeff Roberson 467620a4e154SJeff Roberson return (nitems); 467720a4e154SJeff Roberson } 467820a4e154SJeff Roberson 467920a4e154SJeff Roberson static uint64_t 468020a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 468120a4e154SJeff Roberson { 468220a4e154SJeff Roberson uint64_t nitems; 468320a4e154SJeff Roberson u_int i; 468420a4e154SJeff Roberson 4685bfb6b7a1SJeff Roberson nitems = 0; 4686bfb6b7a1SJeff Roberson if (zone->uz_frees != EARLY_COUNTER) 468720a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4688727c6918SJeff Roberson CPU_FOREACH(i) 4689727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 469020a4e154SJeff Roberson 469120a4e154SJeff Roberson return (nitems); 469220a4e154SJeff Roberson } 469320a4e154SJeff Roberson 469431c251a0SJeff Roberson #ifdef INVARIANTS 469531c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */ 469631c251a0SJeff Roberson static uint64_t 469731c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg) 469831c251a0SJeff Roberson { 469931c251a0SJeff Roberson uma_zone_t z; 470031c251a0SJeff Roberson uint64_t nitems; 470131c251a0SJeff Roberson 470231c251a0SJeff Roberson nitems = 0; 470331c251a0SJeff Roberson LIST_FOREACH(z, &keg->uk_zones, uz_link) 470431c251a0SJeff Roberson nitems += uma_zone_get_allocs(z); 470531c251a0SJeff Roberson 470631c251a0SJeff Roberson return (nitems); 470731c251a0SJeff Roberson } 470831c251a0SJeff Roberson #endif 470931c251a0SJeff Roberson 4710c4ae7908SLawrence Stewart /* See uma.h */ 4711736ee590SJeff Roberson void 4712099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4713099a0e58SBosko Milekic { 4714e20a199fSJeff Roberson uma_keg_t keg; 4715e20a199fSJeff Roberson 4716bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4717727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4718e20a199fSJeff Roberson keg->uk_init = uminit; 4719099a0e58SBosko Milekic } 4720099a0e58SBosko Milekic 4721099a0e58SBosko Milekic /* See uma.h */ 4722099a0e58SBosko Milekic void 4723099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4724099a0e58SBosko Milekic { 4725e20a199fSJeff Roberson uma_keg_t keg; 4726e20a199fSJeff Roberson 4727bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4728727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4729e20a199fSJeff Roberson keg->uk_fini = fini; 4730099a0e58SBosko Milekic } 4731099a0e58SBosko Milekic 4732099a0e58SBosko Milekic /* See uma.h */ 4733099a0e58SBosko Milekic void 4734099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4735099a0e58SBosko Milekic { 4736af526374SJeff Roberson 4737727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4738099a0e58SBosko Milekic zone->uz_init = zinit; 4739099a0e58SBosko Milekic } 4740099a0e58SBosko Milekic 4741099a0e58SBosko Milekic /* See uma.h */ 4742099a0e58SBosko Milekic void 4743099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4744099a0e58SBosko Milekic { 4745af526374SJeff Roberson 4746727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4747099a0e58SBosko Milekic zone->uz_fini = zfini; 4748099a0e58SBosko Milekic } 4749099a0e58SBosko Milekic 4750099a0e58SBosko Milekic /* See uma.h */ 4751099a0e58SBosko Milekic void 47528355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 47538355f576SJeff Roberson { 47540095a784SJeff Roberson uma_keg_t keg; 4755e20a199fSJeff Roberson 4756bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4757727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 47580095a784SJeff Roberson keg->uk_freef = freef; 47598355f576SJeff Roberson } 47608355f576SJeff Roberson 47618355f576SJeff Roberson /* See uma.h */ 47628355f576SJeff Roberson void 47638355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 47648355f576SJeff Roberson { 4765e20a199fSJeff Roberson uma_keg_t keg; 4766e20a199fSJeff Roberson 4767bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4768727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4769e20a199fSJeff Roberson keg->uk_allocf = allocf; 47708355f576SJeff Roberson } 47718355f576SJeff Roberson 47728355f576SJeff Roberson /* See uma.h */ 47736fd34d6fSJeff Roberson void 4774d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr) 4775d4665eaaSJeff Roberson { 4776d4665eaaSJeff Roberson 4777d4665eaaSJeff Roberson ZONE_ASSERT_COLD(zone); 4778d4665eaaSJeff Roberson 47797f746c9fSMateusz Guzik KASSERT(smr != NULL, ("Got NULL smr")); 47807f746c9fSMateusz Guzik KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 47817f746c9fSMateusz Guzik ("zone %p (%s) already uses SMR", zone, zone->uz_name)); 4782d4665eaaSJeff Roberson zone->uz_flags |= UMA_ZONE_SMR; 4783d4665eaaSJeff Roberson zone->uz_smr = smr; 4784d4665eaaSJeff Roberson zone_update_caches(zone); 4785d4665eaaSJeff Roberson } 4786d4665eaaSJeff Roberson 4787d4665eaaSJeff Roberson smr_t 4788d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone) 4789d4665eaaSJeff Roberson { 4790d4665eaaSJeff Roberson 4791d4665eaaSJeff Roberson return (zone->uz_smr); 4792d4665eaaSJeff Roberson } 4793d4665eaaSJeff Roberson 4794d4665eaaSJeff Roberson /* See uma.h */ 4795d4665eaaSJeff Roberson void 47966fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 47976fd34d6fSJeff Roberson { 47986fd34d6fSJeff Roberson uma_keg_t keg; 47996fd34d6fSJeff Roberson 4800bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4801727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 48026fd34d6fSJeff Roberson keg->uk_reserve = items; 48036fd34d6fSJeff Roberson } 48046fd34d6fSJeff Roberson 48056fd34d6fSJeff Roberson /* See uma.h */ 48068355f576SJeff Roberson int 4807a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 48088355f576SJeff Roberson { 4809099a0e58SBosko Milekic uma_keg_t keg; 48108355f576SJeff Roberson vm_offset_t kva; 48119ba30bcbSZbigniew Bodek u_int pages; 48128355f576SJeff Roberson 4813bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4814727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4815727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 48168355f576SJeff Roberson 481779c9f942SJeff Roberson pages = howmany(count, keg->uk_ipers) * keg->uk_ppera; 4818a553d4b8SJeff Roberson 4819a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4820a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4821a4915c21SAttilio Rao #else 4822a4915c21SAttilio Rao if (1) { 4823a4915c21SAttilio Rao #endif 482457223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4825d1f42ac2SAlan Cox if (kva == 0) 48268355f576SJeff Roberson return (0); 4827a4915c21SAttilio Rao } else 4828a4915c21SAttilio Rao kva = 0; 4829bb15d1c7SGleb Smirnoff 4830bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4831099a0e58SBosko Milekic keg->uk_kva = kva; 4832a4915c21SAttilio Rao keg->uk_offset = 0; 4833bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4834a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4835a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4836a4915c21SAttilio Rao #else 4837a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4838a4915c21SAttilio Rao #endif 4839cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4840cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4841cc7ce83aSJeff Roberson zone_update_caches(zone); 4842af526374SJeff Roberson 48438355f576SJeff Roberson return (1); 48448355f576SJeff Roberson } 48458355f576SJeff Roberson 48468355f576SJeff Roberson /* See uma.h */ 48478355f576SJeff Roberson void 48488355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 48498355f576SJeff Roberson { 4850920239efSMark Johnston struct vm_domainset_iter di; 4851ab3185d1SJeff Roberson uma_domain_t dom; 48528355f576SJeff Roberson uma_slab_t slab; 4853099a0e58SBosko Milekic uma_keg_t keg; 485486220393SMark Johnston int aflags, domain, slabs; 48558355f576SJeff Roberson 4856bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 485779c9f942SJeff Roberson slabs = howmany(items, keg->uk_ipers); 4858194a979eSMark Johnston while (slabs-- > 0) { 485986220393SMark Johnston aflags = M_NOWAIT; 486086220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 486186220393SMark Johnston &aflags); 486286220393SMark Johnston for (;;) { 486386220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 486486220393SMark Johnston aflags); 486586220393SMark Johnston if (slab != NULL) { 4866ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 48674ab3aee8SMark Johnston /* 48684ab3aee8SMark Johnston * keg_alloc_slab() always returns a slab on the 48694ab3aee8SMark Johnston * partial list. 48704ab3aee8SMark Johnston */ 48718b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 487286220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 487386220393SMark Johnston us_link); 48744ab3aee8SMark Johnston dom->ud_free_slabs++; 48758b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 4876920239efSMark Johnston break; 48778355f576SJeff Roberson } 48788b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 487989d2fb14SKonstantin Belousov vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0); 488086220393SMark Johnston } 488186220393SMark Johnston } 488286220393SMark Johnston } 48838355f576SJeff Roberson 4884ed581bf6SJeff Roberson /* 4885ed581bf6SJeff Roberson * Returns a snapshot of memory consumption in bytes. 4886ed581bf6SJeff Roberson */ 4887ed581bf6SJeff Roberson size_t 4888ed581bf6SJeff Roberson uma_zone_memory(uma_zone_t zone) 4889ed581bf6SJeff Roberson { 4890ed581bf6SJeff Roberson size_t sz; 4891ed581bf6SJeff Roberson int i; 4892ed581bf6SJeff Roberson 4893ed581bf6SJeff Roberson sz = 0; 4894ed581bf6SJeff Roberson if (zone->uz_flags & UMA_ZFLAG_CACHE) { 4895ed581bf6SJeff Roberson for (i = 0; i < vm_ndomains; i++) 4896c6fd3e23SJeff Roberson sz += ZDOM_GET(zone, i)->uzd_nitems; 4897ed581bf6SJeff Roberson return (sz * zone->uz_size); 4898ed581bf6SJeff Roberson } 4899ed581bf6SJeff Roberson for (i = 0; i < vm_ndomains; i++) 4900ed581bf6SJeff Roberson sz += zone->uz_keg->uk_domain[i].ud_pages; 4901ed581bf6SJeff Roberson 4902ed581bf6SJeff Roberson return (sz * PAGE_SIZE); 4903ed581bf6SJeff Roberson } 4904ed581bf6SJeff Roberson 49058355f576SJeff Roberson /* See uma.h */ 490608cfa56eSMark Johnston void 490708cfa56eSMark Johnston uma_reclaim(int req) 49088355f576SJeff Roberson { 490944ec2b63SKonstantin Belousov 49101431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 491108cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 491286bbae32SJeff Roberson bucket_enable(); 491308cfa56eSMark Johnston 491408cfa56eSMark Johnston switch (req) { 491508cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 491620a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 491708cfa56eSMark Johnston break; 491808cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 491908cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 492020a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 492108cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 492208cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 492320a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4924a2de44abSAlexander Motin } 492508cfa56eSMark Johnston break; 492608cfa56eSMark Johnston default: 492708cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 492808cfa56eSMark Johnston } 49290f9b7bf3SMark Johnston 49308355f576SJeff Roberson /* 49318355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 49328355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 49338355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 49348355f576SJeff Roberson */ 49359b8db4d0SRyan Libby zone_drain(slabzones[0], NULL); 49369b8db4d0SRyan Libby zone_drain(slabzones[1], NULL); 4937cae33c14SJeff Roberson bucket_zone_drain(); 493808cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 49398355f576SJeff Roberson } 49408355f576SJeff Roberson 49412e47807cSJeff Roberson static volatile int uma_reclaim_needed; 494244ec2b63SKonstantin Belousov 494344ec2b63SKonstantin Belousov void 494444ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 494544ec2b63SKonstantin Belousov { 494644ec2b63SKonstantin Belousov 49472e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 49482e47807cSJeff Roberson wakeup(uma_reclaim); 494944ec2b63SKonstantin Belousov } 495044ec2b63SKonstantin Belousov 495144ec2b63SKonstantin Belousov void 495244ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 495344ec2b63SKonstantin Belousov { 495444ec2b63SKonstantin Belousov 495544ec2b63SKonstantin Belousov for (;;) { 495608cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4957200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 495808cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 49592e47807cSJeff Roberson hz); 496008cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 49619b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 496208cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4963200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 49642e47807cSJeff Roberson /* Don't fire more than once per-second. */ 49652e47807cSJeff Roberson pause("umarclslp", hz); 496644ec2b63SKonstantin Belousov } 496744ec2b63SKonstantin Belousov } 496844ec2b63SKonstantin Belousov 4969663b416fSJohn Baldwin /* See uma.h */ 497008cfa56eSMark Johnston void 497108cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 497208cfa56eSMark Johnston { 497308cfa56eSMark Johnston 497408cfa56eSMark Johnston switch (req) { 497508cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 497620a4e154SJeff Roberson zone_trim(zone, NULL); 497708cfa56eSMark Johnston break; 497808cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 497920a4e154SJeff Roberson zone_drain(zone, NULL); 498008cfa56eSMark Johnston break; 498108cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 498208cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 498320a4e154SJeff Roberson zone_drain(zone, NULL); 498408cfa56eSMark Johnston break; 498508cfa56eSMark Johnston default: 498608cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 498708cfa56eSMark Johnston } 498808cfa56eSMark Johnston } 498908cfa56eSMark Johnston 499008cfa56eSMark Johnston /* See uma.h */ 4991663b416fSJohn Baldwin int 4992663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 4993663b416fSJohn Baldwin { 4994663b416fSJohn Baldwin 4995727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 49966c125b8dSMohan Srinivasan } 49976c125b8dSMohan Srinivasan 49982e47807cSJeff Roberson unsigned long 49992e47807cSJeff Roberson uma_limit(void) 50002e47807cSJeff Roberson { 50012e47807cSJeff Roberson 50022e47807cSJeff Roberson return (uma_kmem_limit); 50032e47807cSJeff Roberson } 50042e47807cSJeff Roberson 50052e47807cSJeff Roberson void 50062e47807cSJeff Roberson uma_set_limit(unsigned long limit) 50072e47807cSJeff Roberson { 50082e47807cSJeff Roberson 50092e47807cSJeff Roberson uma_kmem_limit = limit; 50102e47807cSJeff Roberson } 50112e47807cSJeff Roberson 50122e47807cSJeff Roberson unsigned long 50132e47807cSJeff Roberson uma_size(void) 50142e47807cSJeff Roberson { 50152e47807cSJeff Roberson 5016058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 5017ad5b0f5bSJeff Roberson } 5018ad5b0f5bSJeff Roberson 5019ad5b0f5bSJeff Roberson long 5020ad5b0f5bSJeff Roberson uma_avail(void) 5021ad5b0f5bSJeff Roberson { 5022ad5b0f5bSJeff Roberson 5023058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 50242e47807cSJeff Roberson } 50252e47807cSJeff Roberson 5026a0d4b0aeSRobert Watson #ifdef DDB 50278355f576SJeff Roberson /* 50287a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 50297a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 50307a52a97eSRobert Watson * 50317a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 50327a52a97eSRobert Watson * per-CPU cache statistic. 50337a52a97eSRobert Watson * 50347a52a97eSRobert Watson */ 50357a52a97eSRobert Watson static void 50360f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 5037c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 50387a52a97eSRobert Watson { 50397a52a97eSRobert Watson uma_cache_t cache; 5040c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 50417a52a97eSRobert Watson int cachefree, cpu; 50427a52a97eSRobert Watson 5043c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 50447a52a97eSRobert Watson cachefree = 0; 50453aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 50467a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 5047376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 5048376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 5049376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 5050376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 50517a52a97eSRobert Watson allocs += cache->uc_allocs; 50527a52a97eSRobert Watson frees += cache->uc_frees; 50537a52a97eSRobert Watson } 50542efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 50552efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 5056c6fd3e23SJeff Roberson xdomain += counter_u64_fetch(z->uz_xdomain); 5057bf965959SSean Bruno sleeps += z->uz_sleeps; 50587a52a97eSRobert Watson if (cachefreep != NULL) 50597a52a97eSRobert Watson *cachefreep = cachefree; 50607a52a97eSRobert Watson if (allocsp != NULL) 50617a52a97eSRobert Watson *allocsp = allocs; 50627a52a97eSRobert Watson if (freesp != NULL) 50637a52a97eSRobert Watson *freesp = frees; 5064bf965959SSean Bruno if (sleepsp != NULL) 5065bf965959SSean Bruno *sleepsp = sleeps; 5066c1685086SJeff Roberson if (xdomainp != NULL) 5067c1685086SJeff Roberson *xdomainp = xdomain; 50687a52a97eSRobert Watson } 5069a0d4b0aeSRobert Watson #endif /* DDB */ 50707a52a97eSRobert Watson 50717a52a97eSRobert Watson static int 50727a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 50737a52a97eSRobert Watson { 50747a52a97eSRobert Watson uma_keg_t kz; 50757a52a97eSRobert Watson uma_zone_t z; 50767a52a97eSRobert Watson int count; 50777a52a97eSRobert Watson 50787a52a97eSRobert Watson count = 0; 5079111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 50807a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 50817a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 50827a52a97eSRobert Watson count++; 50837a52a97eSRobert Watson } 5084b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 5085b47acb0aSGleb Smirnoff count++; 5086b47acb0aSGleb Smirnoff 5087111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 50887a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 50897a52a97eSRobert Watson } 50907a52a97eSRobert Watson 5091b47acb0aSGleb Smirnoff static void 5092b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 5093b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 5094b47acb0aSGleb Smirnoff { 5095b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 5096b47acb0aSGleb Smirnoff uma_cache_t cache; 5097b47acb0aSGleb Smirnoff int i; 5098b47acb0aSGleb Smirnoff 5099b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 5100c6fd3e23SJeff Roberson zdom = ZDOM_GET(z, i); 5101b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 5102b47acb0aSGleb Smirnoff } 5103b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 5104b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 5105b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 5106c6fd3e23SJeff Roberson uth->uth_xdomain = counter_u64_fetch(z->uz_xdomain); 5107b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 51081de9724eSMark Johnston 5109b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 5110b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 5111b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 5112b47acb0aSGleb Smirnoff continue; 5113b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 5114376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 5115376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 5116376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 5117b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 5118b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 5119b47acb0aSGleb Smirnoff } 5120b47acb0aSGleb Smirnoff } 5121b47acb0aSGleb Smirnoff 51227a52a97eSRobert Watson static int 51237a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 51247a52a97eSRobert Watson { 51257a52a97eSRobert Watson struct uma_stream_header ush; 51267a52a97eSRobert Watson struct uma_type_header uth; 512763b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 51287a52a97eSRobert Watson struct sbuf sbuf; 51297a52a97eSRobert Watson uma_keg_t kz; 51307a52a97eSRobert Watson uma_zone_t z; 51314bd61e19SJeff Roberson uint64_t items; 51328b987a77SJeff Roberson uint32_t kfree, pages; 51334e657159SMatthew D Fleming int count, error, i; 51347a52a97eSRobert Watson 513500f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 513600f0e671SMatthew D Fleming if (error != 0) 513700f0e671SMatthew D Fleming return (error); 51384e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 51391eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 514063b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 51414e657159SMatthew D Fleming 5142404a593eSMatthew D Fleming count = 0; 5143111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 51447a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 51457a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 51467a52a97eSRobert Watson count++; 51477a52a97eSRobert Watson } 51487a52a97eSRobert Watson 5149b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 5150b47acb0aSGleb Smirnoff count++; 5151b47acb0aSGleb Smirnoff 51527a52a97eSRobert Watson /* 51537a52a97eSRobert Watson * Insert stream header. 51547a52a97eSRobert Watson */ 51557a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 51567a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 5157ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 51587a52a97eSRobert Watson ush.ush_count = count; 51594e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 51607a52a97eSRobert Watson 51617a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 51628b987a77SJeff Roberson kfree = pages = 0; 51638b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 51644ab3aee8SMark Johnston kfree += kz->uk_domain[i].ud_free_items; 51658b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 51668b987a77SJeff Roberson } 51677a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 51687a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 5169cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 51707a52a97eSRobert Watson uth.uth_align = kz->uk_align; 51717a52a97eSRobert Watson uth.uth_size = kz->uk_size; 51727a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 51734bd61e19SJeff Roberson if (z->uz_max_items > 0) { 51744bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 51754bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 5176bb15d1c7SGleb Smirnoff kz->uk_ppera; 51774bd61e19SJeff Roberson } else 51788b987a77SJeff Roberson uth.uth_pages = pages; 5179f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 5180bb15d1c7SGleb Smirnoff kz->uk_ppera; 5181bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 51828b987a77SJeff Roberson uth.uth_keg_free = kfree; 5183cbbb4a00SRobert Watson 5184cbbb4a00SRobert Watson /* 5185cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 5186cbbb4a00SRobert Watson * on the keg's zone list. 5187cbbb4a00SRobert Watson */ 5188e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 5189cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 5190cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 5191b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 5192b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 519363b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 519463b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 519563b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 51967a52a97eSRobert Watson } 51977a52a97eSRobert Watson } 5198b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 5199b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 5200b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 5201b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 5202b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 5203b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 5204b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 5205b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 5206b47acb0aSGleb Smirnoff } 5207b47acb0aSGleb Smirnoff 5208111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 52094e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 52104e657159SMatthew D Fleming sbuf_delete(&sbuf); 521163b5d112SKonstantin Belousov free(ups, M_TEMP); 52127a52a97eSRobert Watson return (error); 52137a52a97eSRobert Watson } 521448c5777eSRobert Watson 52150a5a3ccbSGleb Smirnoff int 52160a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 52170a5a3ccbSGleb Smirnoff { 52180a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 521916be9f54SGleb Smirnoff int error, max; 52200a5a3ccbSGleb Smirnoff 522116be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 52220a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 52230a5a3ccbSGleb Smirnoff if (error || !req->newptr) 52240a5a3ccbSGleb Smirnoff return (error); 52250a5a3ccbSGleb Smirnoff 52260a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 52270a5a3ccbSGleb Smirnoff 52280a5a3ccbSGleb Smirnoff return (0); 52290a5a3ccbSGleb Smirnoff } 52300a5a3ccbSGleb Smirnoff 52310a5a3ccbSGleb Smirnoff int 52320a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 52330a5a3ccbSGleb Smirnoff { 523420a4e154SJeff Roberson uma_zone_t zone; 52350a5a3ccbSGleb Smirnoff int cur; 52360a5a3ccbSGleb Smirnoff 523720a4e154SJeff Roberson /* 523820a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 523920a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 524020a4e154SJeff Roberson */ 524120a4e154SJeff Roberson if (arg2 == 0) 524220a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 524320a4e154SJeff Roberson else 524420a4e154SJeff Roberson zone = arg1; 52450a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 52460a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 52470a5a3ccbSGleb Smirnoff } 52480a5a3ccbSGleb Smirnoff 524920a4e154SJeff Roberson static int 525020a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 525120a4e154SJeff Roberson { 525220a4e154SJeff Roberson uma_zone_t zone = arg1; 525320a4e154SJeff Roberson uint64_t cur; 525420a4e154SJeff Roberson 525520a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 525620a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 525720a4e154SJeff Roberson } 525820a4e154SJeff Roberson 525920a4e154SJeff Roberson static int 526020a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 526120a4e154SJeff Roberson { 526220a4e154SJeff Roberson uma_zone_t zone = arg1; 526320a4e154SJeff Roberson uint64_t cur; 526420a4e154SJeff Roberson 526520a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 526620a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 526720a4e154SJeff Roberson } 526820a4e154SJeff Roberson 52696d204a6aSRyan Libby static int 52706d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 52716d204a6aSRyan Libby { 52726d204a6aSRyan Libby struct sbuf sbuf; 52736d204a6aSRyan Libby uma_zone_t zone = arg1; 52746d204a6aSRyan Libby int error; 52756d204a6aSRyan Libby 52766d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 52776d204a6aSRyan Libby if (zone->uz_flags != 0) 52786d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 52796d204a6aSRyan Libby else 52806d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 52816d204a6aSRyan Libby error = sbuf_finish(&sbuf); 52826d204a6aSRyan Libby sbuf_delete(&sbuf); 52836d204a6aSRyan Libby 52846d204a6aSRyan Libby return (error); 52856d204a6aSRyan Libby } 52866d204a6aSRyan Libby 5287f7af5015SRyan Libby static int 5288f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 5289f7af5015SRyan Libby { 5290f7af5015SRyan Libby uma_keg_t keg = arg1; 5291f7af5015SRyan Libby int avail, effpct, total; 5292f7af5015SRyan Libby 5293f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 529454c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 52959b8db4d0SRyan Libby total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize; 5296f7af5015SRyan Libby /* 5297f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 5298f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 5299f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 5300f7af5015SRyan Libby */ 5301f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 5302f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 5303f7af5015SRyan Libby avail *= mp_maxid + 1; 5304f7af5015SRyan Libby effpct = 100 * avail / total; 5305f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 5306f7af5015SRyan Libby } 5307f7af5015SRyan Libby 53084bd61e19SJeff Roberson static int 53094bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 53104bd61e19SJeff Roberson { 53114bd61e19SJeff Roberson uma_zone_t zone = arg1; 53124bd61e19SJeff Roberson uint64_t cur; 53134bd61e19SJeff Roberson 53144bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 53154bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 53164bd61e19SJeff Roberson } 53174bd61e19SJeff Roberson 53189542ea7bSGleb Smirnoff #ifdef INVARIANTS 53199542ea7bSGleb Smirnoff static uma_slab_t 53209542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 53219542ea7bSGleb Smirnoff { 53229542ea7bSGleb Smirnoff uma_slab_t slab; 53239542ea7bSGleb Smirnoff uma_keg_t keg; 53249542ea7bSGleb Smirnoff uint8_t *mem; 53259542ea7bSGleb Smirnoff 53269542ea7bSGleb Smirnoff /* 53279542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 53289542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 53299542ea7bSGleb Smirnoff * essentially holds a reference. 53309542ea7bSGleb Smirnoff */ 5331727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 5332727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5333bb15d1c7SGleb Smirnoff return (NULL); 533454c5ae80SRyan Libby if (zone->uz_flags & UMA_ZFLAG_VTOSLAB) 5335727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 5336bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 533754c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0) 5338727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 53398b987a77SJeff Roberson KEG_LOCK(keg, 0); 53409542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 53418b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 53429542ea7bSGleb Smirnoff 53439542ea7bSGleb Smirnoff return (slab); 53449542ea7bSGleb Smirnoff } 53459542ea7bSGleb Smirnoff 5346c5deaf04SGleb Smirnoff static bool 5347c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 5348c5deaf04SGleb Smirnoff { 5349c5deaf04SGleb Smirnoff 5350727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5351c5deaf04SGleb Smirnoff return (true); 5352c5deaf04SGleb Smirnoff 5353bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 5354c5deaf04SGleb Smirnoff } 5355c5deaf04SGleb Smirnoff 5356c5deaf04SGleb Smirnoff static bool 5357c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 5358c5deaf04SGleb Smirnoff { 5359c5deaf04SGleb Smirnoff uintptr_t idx; 5360c5deaf04SGleb Smirnoff 5361c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 5362c5deaf04SGleb Smirnoff return (true); 5363c5deaf04SGleb Smirnoff 5364c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 5365c5deaf04SGleb Smirnoff return (false); 5366c5deaf04SGleb Smirnoff 5367c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 5368c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 5369c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 5370c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 5371c5deaf04SGleb Smirnoff } 5372c5deaf04SGleb Smirnoff 5373c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 5374c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 5375c5deaf04SGleb Smirnoff return (true); 5376c5deaf04SGleb Smirnoff } 5377c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 5378c5deaf04SGleb Smirnoff 5379c5deaf04SGleb Smirnoff return (false); 5380c5deaf04SGleb Smirnoff } 5381c5deaf04SGleb Smirnoff 53829542ea7bSGleb Smirnoff /* 53839542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 53849542ea7bSGleb Smirnoff * 53859542ea7bSGleb Smirnoff */ 53869542ea7bSGleb Smirnoff static void 53879542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 53889542ea7bSGleb Smirnoff { 53899542ea7bSGleb Smirnoff uma_keg_t keg; 53909542ea7bSGleb Smirnoff int freei; 53919542ea7bSGleb Smirnoff 53929542ea7bSGleb Smirnoff if (slab == NULL) { 53939542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 53949542ea7bSGleb Smirnoff if (slab == NULL) 5395952c8964SMark Johnston panic("uma: item %p did not belong to zone %s", 53969542ea7bSGleb Smirnoff item, zone->uz_name); 53979542ea7bSGleb Smirnoff } 5398584061b4SJeff Roberson keg = zone->uz_keg; 53991e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 54009542ea7bSGleb Smirnoff 5401942951baSRyan Libby if (BIT_TEST_SET_ATOMIC(keg->uk_ipers, freei, 5402942951baSRyan Libby slab_dbg_bits(slab, keg))) 5403952c8964SMark Johnston panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)", 54049542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 54059542ea7bSGleb Smirnoff } 54069542ea7bSGleb Smirnoff 54079542ea7bSGleb Smirnoff /* 54089542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 54099542ea7bSGleb Smirnoff * and duplicate frees. 54109542ea7bSGleb Smirnoff * 54119542ea7bSGleb Smirnoff */ 54129542ea7bSGleb Smirnoff static void 54139542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 54149542ea7bSGleb Smirnoff { 54159542ea7bSGleb Smirnoff uma_keg_t keg; 54169542ea7bSGleb Smirnoff int freei; 54179542ea7bSGleb Smirnoff 54189542ea7bSGleb Smirnoff if (slab == NULL) { 54199542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 54209542ea7bSGleb Smirnoff if (slab == NULL) 5421952c8964SMark Johnston panic("uma: Freed item %p did not belong to zone %s", 54229542ea7bSGleb Smirnoff item, zone->uz_name); 54239542ea7bSGleb Smirnoff } 5424584061b4SJeff Roberson keg = zone->uz_keg; 54251e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 54269542ea7bSGleb Smirnoff 54279542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 5428952c8964SMark Johnston panic("Invalid free of %p from zone %p(%s) slab %p(%d)", 54299542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 54309542ea7bSGleb Smirnoff 54311e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 5432952c8964SMark Johnston panic("Unaligned free of %p from zone %p(%s) slab %p(%d)", 54339542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 54349542ea7bSGleb Smirnoff 5435942951baSRyan Libby if (!BIT_TEST_CLR_ATOMIC(keg->uk_ipers, freei, 5436942951baSRyan Libby slab_dbg_bits(slab, keg))) 5437952c8964SMark Johnston panic("Duplicate free of %p from zone %p(%s) slab %p(%d)", 54389542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 54399542ea7bSGleb Smirnoff } 54409542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 54419542ea7bSGleb Smirnoff 544248c5777eSRobert Watson #ifdef DDB 544346d70077SConrad Meyer static int64_t 544446d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 54450223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 544648c5777eSRobert Watson { 544746d70077SConrad Meyer uint64_t frees; 54480f9b7bf3SMark Johnston int i; 544948c5777eSRobert Watson 545048c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 545146d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 54522efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 545346d70077SConrad Meyer *sleeps = z->uz_sleeps; 545446d70077SConrad Meyer *cachefree = 0; 545546d70077SConrad Meyer *xdomain = 0; 545648c5777eSRobert Watson } else 545746d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 545846d70077SConrad Meyer xdomain); 54598b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 5460c6fd3e23SJeff Roberson *cachefree += ZDOM_GET(z, i)->uzd_nitems; 5461e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 546248c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 54634ab3aee8SMark Johnston *cachefree += kz->uk_domain[i].ud_free_items; 54648b987a77SJeff Roberson } 546546d70077SConrad Meyer *used = *allocs - frees; 546646d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 546746d70077SConrad Meyer } 54680f9b7bf3SMark Johnston 546946d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 547046d70077SConrad Meyer { 547146d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 547246d70077SConrad Meyer uma_keg_t kz; 547346d70077SConrad Meyer uma_zone_t z; 547446d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 547546d70077SConrad Meyer long cachefree; 547646d70077SConrad Meyer /* variables for sorting */ 547746d70077SConrad Meyer uma_keg_t cur_keg; 547846d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 547946d70077SConrad Meyer int64_t cur_size, last_size, size; 548046d70077SConrad Meyer int ties; 548146d70077SConrad Meyer 548246d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 548346d70077SConrad Meyer if (modif[0] == 'i') { 548446d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 548546d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 548646d70077SConrad Meyer } else { 548746d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 548846d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 548946d70077SConrad Meyer } 549046d70077SConrad Meyer 549146d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 549246d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 549346d70077SConrad Meyer 549446d70077SConrad Meyer /* Sort the zones with largest size first. */ 549546d70077SConrad Meyer last_zone = NULL; 549646d70077SConrad Meyer last_size = INT64_MAX; 549746d70077SConrad Meyer for (;;) { 549846d70077SConrad Meyer cur_zone = NULL; 549946d70077SConrad Meyer cur_size = -1; 550046d70077SConrad Meyer ties = 0; 550146d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 550246d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 550346d70077SConrad Meyer /* 550446d70077SConrad Meyer * In the case of size ties, print out zones 550546d70077SConrad Meyer * in the order they are encountered. That is, 550646d70077SConrad Meyer * when we encounter the most recently output 550746d70077SConrad Meyer * zone, we have already printed all preceding 550846d70077SConrad Meyer * ties, and we must print all following ties. 550946d70077SConrad Meyer */ 551046d70077SConrad Meyer if (z == last_zone) { 551146d70077SConrad Meyer ties = 1; 551246d70077SConrad Meyer continue; 551346d70077SConrad Meyer } 551446d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 551546d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 551646d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 551746d70077SConrad Meyer { 551846d70077SConrad Meyer cur_size = size; 551946d70077SConrad Meyer cur_zone = z; 552046d70077SConrad Meyer cur_keg = kz; 552146d70077SConrad Meyer } 552246d70077SConrad Meyer } 552346d70077SConrad Meyer } 552446d70077SConrad Meyer if (cur_zone == NULL) 552546d70077SConrad Meyer break; 552646d70077SConrad Meyer 552746d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 552846d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 552946d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 553046d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 553146d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 553220a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 553320a4e154SJeff Roberson xdomain); 553446d70077SConrad Meyer 5535687c94aaSJohn Baldwin if (db_pager_quit) 5536687c94aaSJohn Baldwin return; 553746d70077SConrad Meyer last_zone = cur_zone; 553846d70077SConrad Meyer last_size = cur_size; 553948c5777eSRobert Watson } 554048c5777eSRobert Watson } 554103175483SAlexander Motin 554203175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 554303175483SAlexander Motin { 554403175483SAlexander Motin uma_zone_t z; 5545ab3185d1SJeff Roberson uint64_t allocs, frees; 55460f9b7bf3SMark Johnston long cachefree; 55470f9b7bf3SMark Johnston int i; 554803175483SAlexander Motin 554903175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 555003175483SAlexander Motin "Requests", "Bucket"); 555103175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 5552c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 55530f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 5554c6fd3e23SJeff Roberson cachefree += ZDOM_GET(z, i)->uzd_nitems; 55550f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 555603175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 555703175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 555820a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 555903175483SAlexander Motin if (db_pager_quit) 556003175483SAlexander Motin return; 556103175483SAlexander Motin } 556203175483SAlexander Motin } 55639542ea7bSGleb Smirnoff #endif /* DDB */ 5564