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) 251e84130a0SJeff Roberson #define BUCKET_MIN 2 252fc03d22bSJeff Roberson 253fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = { 254e84130a0SJeff Roberson /* Literal bucket sizes. */ 255e84130a0SJeff Roberson { NULL, "2 Bucket", 2, 4096 }, 256e84130a0SJeff Roberson { NULL, "4 Bucket", 4, 3072 }, 257e84130a0SJeff Roberson { NULL, "8 Bucket", 8, 2048 }, 258e84130a0SJeff Roberson { NULL, "16 Bucket", 16, 1024 }, 259e84130a0SJeff Roberson /* Rounded down power of 2 sizes for efficiency. */ 260fc03d22bSJeff Roberson { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 261fc03d22bSJeff Roberson { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 262fc03d22bSJeff Roberson { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 2631aa6c758SAlexander Motin { NULL, "256 Bucket", BUCKET_SIZE(256), 64 }, 264fc03d22bSJeff Roberson { NULL, NULL, 0} 265fc03d22bSJeff Roberson }; 266cae33c14SJeff Roberson 2672019094aSRobert Watson /* 2682019094aSRobert Watson * Flags and enumerations to be passed to internal functions. 2692019094aSRobert Watson */ 270bb15d1c7SGleb Smirnoff enum zfreeskip { 271bb15d1c7SGleb Smirnoff SKIP_NONE = 0, 272bb15d1c7SGleb Smirnoff SKIP_CNT = 0x00000001, 273bb15d1c7SGleb Smirnoff SKIP_DTOR = 0x00010000, 274bb15d1c7SGleb Smirnoff SKIP_FINI = 0x00020000, 275bb15d1c7SGleb Smirnoff }; 276b23f72e9SBrian Feldman 2778355f576SJeff Roberson /* Prototypes.. */ 2788355f576SJeff Roberson 279a81c400eSJeff Roberson void uma_startup1(vm_offset_t); 280f4bef67cSGleb Smirnoff void uma_startup2(void); 281f4bef67cSGleb Smirnoff 282ab3185d1SJeff Roberson static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 283ab3185d1SJeff Roberson static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 284ab3059a8SMatt Macy static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 285ab3185d1SJeff Roberson static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 286ec0d8280SRyan Libby static void *contig_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 287f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t); 288ab3059a8SMatt Macy static void pcpu_page_free(void *, vm_size_t, uint8_t); 28986220393SMark Johnston static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int); 2909643769aSJeff Roberson static void cache_drain(uma_zone_t); 2918355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t); 29208cfa56eSMark Johnston static void bucket_cache_reclaim(uma_zone_t zone, bool); 293b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int); 294099a0e58SBosko Milekic static void keg_dtor(void *, int, void *); 295b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int); 2969c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *); 297d4665eaaSJeff Roberson static inline void item_dtor(uma_zone_t zone, void *item, int size, 298d4665eaaSJeff Roberson void *udata, enum zfreeskip skip); 299b23f72e9SBrian Feldman static int zero_init(void *, int, int); 300c6fd3e23SJeff Roberson static void zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 301c6fd3e23SJeff Roberson int itemdomain, bool ws); 30220a4e154SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t, void *), void *); 303a81c400eSJeff Roberson static void zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *), void *); 30420a4e154SJeff Roberson static void zone_timeout(uma_zone_t zone, void *); 3053b2f2cb8SAlexander Motin static int hash_alloc(struct uma_hash *, u_int); 3060aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *); 3070aef6126SJeff Roberson static void hash_free(struct uma_hash *hash); 3088355f576SJeff Roberson static void uma_timeout(void *); 309860bb7a0SMark Johnston static void uma_shutdown(void); 310ab3185d1SJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int, int); 3110095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 3124bd61e19SJeff Roberson static int zone_alloc_limit(uma_zone_t zone, int count, int flags); 3134bd61e19SJeff Roberson static void zone_free_limit(uma_zone_t zone, int count); 31486bbae32SJeff Roberson static void bucket_enable(void); 315cae33c14SJeff Roberson static void bucket_init(void); 3166fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int); 3176fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *); 318cae33c14SJeff Roberson static void bucket_zone_drain(void); 319beb8beefSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int); 3200095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 321bb15d1c7SGleb Smirnoff static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item); 322e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 32385dcf349SGleb Smirnoff uma_fini fini, int align, uint32_t flags); 324b75c4efcSAndrew Turner static int zone_import(void *, void **, int, int, int); 325b75c4efcSAndrew Turner static void zone_release(void *, void **, int); 326beb8beefSJeff Roberson static bool cache_alloc(uma_zone_t, uma_cache_t, void *, int); 3270a81b439SJeff Roberson static bool cache_free(uma_zone_t, uma_cache_t, void *, void *, int); 328bbee39c6SJeff Roberson 3297a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 3307a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 33120a4e154SJeff Roberson static int sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS); 33220a4e154SJeff Roberson static int sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS); 3336d204a6aSRyan Libby static int sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS); 334f7af5015SRyan Libby static int sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS); 3354bd61e19SJeff Roberson static int sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS); 3368355f576SJeff Roberson 33731c251a0SJeff Roberson static uint64_t uma_zone_get_allocs(uma_zone_t zone); 33831c251a0SJeff Roberson 3397029da5cSPawel Biernacki static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 34033e5a1eaSRyan Libby "Memory allocation debugging"); 34133e5a1eaSRyan Libby 3429542ea7bSGleb Smirnoff #ifdef INVARIANTS 34331c251a0SJeff Roberson static uint64_t uma_keg_get_allocs(uma_keg_t zone); 344815db204SRyan Libby static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg); 345815db204SRyan Libby 346c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem); 347c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem); 3489542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item); 3499542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item); 350c5deaf04SGleb Smirnoff 351c5deaf04SGleb Smirnoff static u_int dbg_divisor = 1; 352c5deaf04SGleb Smirnoff SYSCTL_UINT(_vm_debug, OID_AUTO, divisor, 353c5deaf04SGleb Smirnoff CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0, 354c5deaf04SGleb Smirnoff "Debug & thrash every this item in memory allocator"); 355c5deaf04SGleb Smirnoff 356c5deaf04SGleb Smirnoff static counter_u64_t uma_dbg_cnt = EARLY_COUNTER; 357c5deaf04SGleb Smirnoff static counter_u64_t uma_skip_cnt = EARLY_COUNTER; 358c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD, 359c5deaf04SGleb Smirnoff &uma_dbg_cnt, "memory items debugged"); 360c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD, 361c5deaf04SGleb Smirnoff &uma_skip_cnt, "memory items skipped, not debugged"); 3629542ea7bSGleb Smirnoff #endif 3639542ea7bSGleb Smirnoff 3647029da5cSPawel Biernacki SYSCTL_NODE(_vm, OID_AUTO, uma, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 3657029da5cSPawel Biernacki "Universal Memory Allocator"); 36635ec24f3SRyan Libby 367a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_INT, 3687a52a97eSRobert Watson 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 3697a52a97eSRobert Watson 370a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_STRUCT, 3717a52a97eSRobert Watson 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 3727a52a97eSRobert Watson 3732f891cd5SPawel Jakub Dawidek static int zone_warnings = 1; 374af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0, 3752f891cd5SPawel Jakub Dawidek "Warn when UMA zones becomes full"); 3762f891cd5SPawel Jakub Dawidek 37733e5a1eaSRyan Libby static int multipage_slabs = 1; 37833e5a1eaSRyan Libby TUNABLE_INT("vm.debug.uma_multipage_slabs", &multipage_slabs); 37933e5a1eaSRyan Libby SYSCTL_INT(_vm_debug, OID_AUTO, uma_multipage_slabs, 38033e5a1eaSRyan Libby CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &multipage_slabs, 0, 38133e5a1eaSRyan Libby "UMA may choose larger slab sizes for better efficiency"); 38233e5a1eaSRyan Libby 38386bbae32SJeff Roberson /* 3849b8db4d0SRyan Libby * Select the slab zone for an offpage slab with the given maximum item count. 3859b8db4d0SRyan Libby */ 3869b8db4d0SRyan Libby static inline uma_zone_t 3879b8db4d0SRyan Libby slabzone(int ipers) 3889b8db4d0SRyan Libby { 3899b8db4d0SRyan Libby 3909b8db4d0SRyan Libby return (slabzones[ipers > SLABZONE0_SETSIZE]); 3919b8db4d0SRyan Libby } 3929b8db4d0SRyan Libby 3939b8db4d0SRyan Libby /* 39486bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 39586bbae32SJeff Roberson */ 39686bbae32SJeff Roberson static void 39786bbae32SJeff Roberson bucket_enable(void) 39886bbae32SJeff Roberson { 3993182660aSRyan Libby 400a81c400eSJeff Roberson KASSERT(booted >= BOOT_KVA, ("Bucket enable before init")); 401251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 40286bbae32SJeff Roberson } 40386bbae32SJeff Roberson 404dc2c7965SRobert Watson /* 405dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 406dc2c7965SRobert Watson * 407dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 408fc03d22bSJeff Roberson * of the header and an array of pointers. 409dc2c7965SRobert Watson */ 410cae33c14SJeff Roberson static void 411cae33c14SJeff Roberson bucket_init(void) 412cae33c14SJeff Roberson { 413cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 414cae33c14SJeff Roberson int size; 415cae33c14SJeff Roberson 416d74e6a1dSAlan Cox for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 417cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 418cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 419cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 420e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 421dfe13344SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | 422dfe13344SJeff Roberson UMA_ZONE_FIRSTTOUCH); 423cae33c14SJeff Roberson } 424cae33c14SJeff Roberson } 425cae33c14SJeff Roberson 426dc2c7965SRobert Watson /* 427dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 428dc2c7965SRobert Watson * to allocate the bucket. 429dc2c7965SRobert Watson */ 430dc2c7965SRobert Watson static struct uma_bucket_zone * 431dc2c7965SRobert Watson bucket_zone_lookup(int entries) 432dc2c7965SRobert Watson { 433fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 434dc2c7965SRobert Watson 435fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 436fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 437fc03d22bSJeff Roberson return (ubz); 438fc03d22bSJeff Roberson ubz--; 439fc03d22bSJeff Roberson return (ubz); 440fc03d22bSJeff Roberson } 441fc03d22bSJeff Roberson 442003cf08bSMark Johnston static struct uma_bucket_zone * 443003cf08bSMark Johnston bucket_zone_max(uma_zone_t zone, int nitems) 444003cf08bSMark Johnston { 445003cf08bSMark Johnston struct uma_bucket_zone *ubz; 446003cf08bSMark Johnston int bpcpu; 447003cf08bSMark Johnston 448003cf08bSMark Johnston bpcpu = 2; 449dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 450003cf08bSMark Johnston /* Count the cross-domain bucket. */ 451003cf08bSMark Johnston bpcpu++; 452003cf08bSMark Johnston 453003cf08bSMark Johnston for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 454003cf08bSMark Johnston if (ubz->ubz_entries * bpcpu * mp_ncpus > nitems) 455003cf08bSMark Johnston break; 456003cf08bSMark Johnston if (ubz == &bucket_zones[0]) 457003cf08bSMark Johnston ubz = NULL; 458003cf08bSMark Johnston else 459003cf08bSMark Johnston ubz--; 460003cf08bSMark Johnston return (ubz); 461003cf08bSMark Johnston } 462003cf08bSMark Johnston 463fc03d22bSJeff Roberson static int 464fc03d22bSJeff Roberson bucket_select(int size) 465fc03d22bSJeff Roberson { 466fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 467fc03d22bSJeff Roberson 468fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 469fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 470fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 471fc03d22bSJeff Roberson 472fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 473fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 474fc03d22bSJeff Roberson break; 475fc03d22bSJeff Roberson ubz--; 476fc03d22bSJeff Roberson return (ubz->ubz_entries); 477dc2c7965SRobert Watson } 478dc2c7965SRobert Watson 479cae33c14SJeff Roberson static uma_bucket_t 4806fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags) 481cae33c14SJeff Roberson { 482cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 483cae33c14SJeff Roberson uma_bucket_t bucket; 484cae33c14SJeff Roberson 485cae33c14SJeff Roberson /* 486d4665eaaSJeff Roberson * Don't allocate buckets early in boot. 487cae33c14SJeff Roberson */ 488d4665eaaSJeff Roberson if (__predict_false(booted < BOOT_KVA)) 489cae33c14SJeff Roberson return (NULL); 490a81c400eSJeff Roberson 4916fd34d6fSJeff Roberson /* 4926fd34d6fSJeff Roberson * To limit bucket recursion we store the original zone flags 4936fd34d6fSJeff Roberson * in a cookie passed via zalloc_arg/zfree_arg. This allows the 4946fd34d6fSJeff Roberson * NOVM flag to persist even through deep recursions. We also 4956fd34d6fSJeff Roberson * store ZFLAG_BUCKET once we have recursed attempting to allocate 4966fd34d6fSJeff Roberson * a bucket for a bucket zone so we do not allow infinite bucket 4976fd34d6fSJeff Roberson * recursion. This cookie will even persist to frees of unused 4986fd34d6fSJeff Roberson * buckets via the allocation path or bucket allocations in the 4996fd34d6fSJeff Roberson * free path. 5006fd34d6fSJeff Roberson */ 5016fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 5026fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 503e8a720feSAlexander Motin else { 504e8a720feSAlexander Motin if ((uintptr_t)udata & UMA_ZFLAG_BUCKET) 505e8a720feSAlexander Motin return (NULL); 5066fd34d6fSJeff Roberson udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET); 507e8a720feSAlexander Motin } 508bae55c4aSRyan Libby if (((uintptr_t)udata & UMA_ZONE_VM) != 0) 509af526374SJeff Roberson flags |= M_NOVM; 51020a4e154SJeff Roberson ubz = bucket_zone_lookup(zone->uz_bucket_size); 51120d3ab87SAlexander Motin if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0) 51220d3ab87SAlexander Motin ubz++; 5136fd34d6fSJeff Roberson bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags); 514cae33c14SJeff Roberson if (bucket) { 515cae33c14SJeff Roberson #ifdef INVARIANTS 516cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 517cae33c14SJeff Roberson #endif 518cae33c14SJeff Roberson bucket->ub_cnt = 0; 519cae33c14SJeff Roberson bucket->ub_entries = ubz->ubz_entries; 520d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 521d4665eaaSJeff Roberson CTR3(KTR_UMA, "bucket_alloc: zone %s(%p) allocated bucket %p", 522d4665eaaSJeff Roberson zone->uz_name, zone, bucket); 523cae33c14SJeff Roberson } 524cae33c14SJeff Roberson 525cae33c14SJeff Roberson return (bucket); 526cae33c14SJeff Roberson } 527cae33c14SJeff Roberson 528cae33c14SJeff Roberson static void 5296fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata) 530cae33c14SJeff Roberson { 531cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 532cae33c14SJeff Roberson 533c6fd3e23SJeff Roberson if (bucket->ub_cnt != 0) 534c6fd3e23SJeff Roberson bucket_drain(zone, bucket); 535c6fd3e23SJeff Roberson 536fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 537fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 538d4665eaaSJeff Roberson KASSERT(bucket->ub_seq == SMR_SEQ_INVALID, 539d4665eaaSJeff Roberson ("bucket_free: Freeing an SMR bucket.")); 5406fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 5416fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 542dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 5436fd34d6fSJeff Roberson uma_zfree_arg(ubz->ubz_zone, bucket, udata); 544cae33c14SJeff Roberson } 545cae33c14SJeff Roberson 546cae33c14SJeff Roberson static void 547cae33c14SJeff Roberson bucket_zone_drain(void) 548cae33c14SJeff Roberson { 549cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 550cae33c14SJeff Roberson 551cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 55208cfa56eSMark Johnston uma_zone_reclaim(ubz->ubz_zone, UMA_RECLAIM_DRAIN); 553cae33c14SJeff Roberson } 554cae33c14SJeff Roberson 55508cfa56eSMark Johnston /* 556c6fd3e23SJeff Roberson * Acquire the domain lock and record contention. 557c6fd3e23SJeff Roberson */ 558c6fd3e23SJeff Roberson static uma_zone_domain_t 559c6fd3e23SJeff Roberson zone_domain_lock(uma_zone_t zone, int domain) 560c6fd3e23SJeff Roberson { 561c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 562c6fd3e23SJeff Roberson bool lockfail; 563c6fd3e23SJeff Roberson 564c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 565c6fd3e23SJeff Roberson lockfail = false; 566c6fd3e23SJeff Roberson if (ZDOM_OWNED(zdom)) 567c6fd3e23SJeff Roberson lockfail = true; 568c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 569c6fd3e23SJeff Roberson /* This is unsynchronized. The counter does not need to be precise. */ 570c6fd3e23SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 571c6fd3e23SJeff Roberson zone->uz_bucket_size++; 572c6fd3e23SJeff Roberson return (zdom); 573c6fd3e23SJeff Roberson } 574c6fd3e23SJeff Roberson 575c6fd3e23SJeff Roberson /* 576fe835cbfSJeff Roberson * Search for the domain with the least cached items and return it if it 577fe835cbfSJeff Roberson * is out of balance with the preferred domain. 578c6fd3e23SJeff Roberson */ 579c6fd3e23SJeff Roberson static __noinline int 580c6fd3e23SJeff Roberson zone_domain_lowest(uma_zone_t zone, int pref) 581c6fd3e23SJeff Roberson { 582fe835cbfSJeff Roberson long least, nitems, prefitems; 583c6fd3e23SJeff Roberson int domain; 584c6fd3e23SJeff Roberson int i; 585c6fd3e23SJeff Roberson 586fe835cbfSJeff Roberson prefitems = least = LONG_MAX; 587c6fd3e23SJeff Roberson domain = 0; 588c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 589c6fd3e23SJeff Roberson nitems = ZDOM_GET(zone, i)->uzd_nitems; 590c6fd3e23SJeff Roberson if (nitems < least) { 591c6fd3e23SJeff Roberson domain = i; 592c6fd3e23SJeff Roberson least = nitems; 593c6fd3e23SJeff Roberson } 594fe835cbfSJeff Roberson if (domain == pref) 595fe835cbfSJeff Roberson prefitems = nitems; 596fe835cbfSJeff Roberson } 597fe835cbfSJeff Roberson if (prefitems < least * 2) 598fe835cbfSJeff Roberson return (pref); 599c6fd3e23SJeff Roberson 600c6fd3e23SJeff Roberson return (domain); 601c6fd3e23SJeff Roberson } 602c6fd3e23SJeff Roberson 603c6fd3e23SJeff Roberson /* 604c6fd3e23SJeff Roberson * Search for the domain with the most cached items and return it or the 605c6fd3e23SJeff Roberson * preferred domain if it has enough to proceed. 606c6fd3e23SJeff Roberson */ 607c6fd3e23SJeff Roberson static __noinline int 608c6fd3e23SJeff Roberson zone_domain_highest(uma_zone_t zone, int pref) 609c6fd3e23SJeff Roberson { 610c6fd3e23SJeff Roberson long most, nitems; 611c6fd3e23SJeff Roberson int domain; 612c6fd3e23SJeff Roberson int i; 613c6fd3e23SJeff Roberson 614c6fd3e23SJeff Roberson if (ZDOM_GET(zone, pref)->uzd_nitems > BUCKET_MAX) 615c6fd3e23SJeff Roberson return (pref); 616c6fd3e23SJeff Roberson 617c6fd3e23SJeff Roberson most = 0; 618c6fd3e23SJeff Roberson domain = 0; 619c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 620c6fd3e23SJeff Roberson nitems = ZDOM_GET(zone, i)->uzd_nitems; 621c6fd3e23SJeff Roberson if (nitems > most) { 622c6fd3e23SJeff Roberson domain = i; 623c6fd3e23SJeff Roberson most = nitems; 624c6fd3e23SJeff Roberson } 625c6fd3e23SJeff Roberson } 626c6fd3e23SJeff Roberson 627c6fd3e23SJeff Roberson return (domain); 628c6fd3e23SJeff Roberson } 629c6fd3e23SJeff Roberson 630c6fd3e23SJeff Roberson /* 631c6fd3e23SJeff Roberson * Safely subtract cnt from imax. 632c6fd3e23SJeff Roberson */ 633c6fd3e23SJeff Roberson static void 634c6fd3e23SJeff Roberson zone_domain_imax_sub(uma_zone_domain_t zdom, int cnt) 635c6fd3e23SJeff Roberson { 636c6fd3e23SJeff Roberson long new; 637c6fd3e23SJeff Roberson long old; 638c6fd3e23SJeff Roberson 639c6fd3e23SJeff Roberson old = zdom->uzd_imax; 640c6fd3e23SJeff Roberson do { 641c6fd3e23SJeff Roberson if (old <= cnt) 642c6fd3e23SJeff Roberson new = 0; 643c6fd3e23SJeff Roberson else 644c6fd3e23SJeff Roberson new = old - cnt; 645c6fd3e23SJeff Roberson } while (atomic_fcmpset_long(&zdom->uzd_imax, &old, new) == 0); 646c6fd3e23SJeff Roberson } 647c6fd3e23SJeff Roberson 648c6fd3e23SJeff Roberson /* 649c6fd3e23SJeff Roberson * Set the maximum imax value. 650c6fd3e23SJeff Roberson */ 651c6fd3e23SJeff Roberson static void 652c6fd3e23SJeff Roberson zone_domain_imax_set(uma_zone_domain_t zdom, int nitems) 653c6fd3e23SJeff Roberson { 654c6fd3e23SJeff Roberson long old; 655c6fd3e23SJeff Roberson 656c6fd3e23SJeff Roberson old = zdom->uzd_imax; 657c6fd3e23SJeff Roberson do { 658c6fd3e23SJeff Roberson if (old >= nitems) 659c6fd3e23SJeff Roberson break; 660c6fd3e23SJeff Roberson } while (atomic_fcmpset_long(&zdom->uzd_imax, &old, nitems) == 0); 661c6fd3e23SJeff Roberson } 662c6fd3e23SJeff Roberson 663c6fd3e23SJeff Roberson /* 66408cfa56eSMark Johnston * Attempt to satisfy an allocation by retrieving a full bucket from one of the 665d4665eaaSJeff Roberson * zone's caches. If a bucket is found the zone is not locked on return. 66608cfa56eSMark Johnston */ 6670f9b7bf3SMark Johnston static uma_bucket_t 668c6fd3e23SJeff Roberson zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom, bool reclaim) 6690f9b7bf3SMark Johnston { 6700f9b7bf3SMark Johnston uma_bucket_t bucket; 671d4665eaaSJeff Roberson int i; 672d4665eaaSJeff Roberson bool dtor = false; 6730f9b7bf3SMark Johnston 674c6fd3e23SJeff Roberson ZDOM_LOCK_ASSERT(zdom); 6750f9b7bf3SMark Johnston 676dc3915c8SJeff Roberson if ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) == NULL) 677d4665eaaSJeff Roberson return (NULL); 678d4665eaaSJeff Roberson 679543117beSJeff Roberson /* SMR Buckets can not be re-used until readers expire. */ 680d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 681d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 682d4665eaaSJeff Roberson if (!smr_poll(zone->uz_smr, bucket->ub_seq, false)) 683d4665eaaSJeff Roberson return (NULL); 684d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 685543117beSJeff Roberson dtor = (zone->uz_dtor != NULL) || UMA_ALWAYS_CTORDTOR; 686c6fd3e23SJeff Roberson if (STAILQ_NEXT(bucket, ub_link) != NULL) 687c6fd3e23SJeff Roberson zdom->uzd_seq = STAILQ_NEXT(bucket, ub_link)->ub_seq; 688d4665eaaSJeff Roberson } 689dc3915c8SJeff Roberson STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link); 69006d8bdcbSMark Johnston 69106d8bdcbSMark Johnston KASSERT(zdom->uzd_nitems >= bucket->ub_cnt, 69206d8bdcbSMark Johnston ("%s: item count underflow (%ld, %d)", 69306d8bdcbSMark Johnston __func__, zdom->uzd_nitems, bucket->ub_cnt)); 69406d8bdcbSMark Johnston KASSERT(bucket->ub_cnt > 0, 69506d8bdcbSMark Johnston ("%s: empty bucket in bucket cache", __func__)); 6960f9b7bf3SMark Johnston zdom->uzd_nitems -= bucket->ub_cnt; 697c6fd3e23SJeff Roberson 698c6fd3e23SJeff Roberson /* 699c6fd3e23SJeff Roberson * Shift the bounds of the current WSS interval to avoid 700c6fd3e23SJeff Roberson * perturbing the estimate. 701c6fd3e23SJeff Roberson */ 702c6fd3e23SJeff Roberson if (reclaim) { 703c6fd3e23SJeff Roberson zdom->uzd_imin -= lmin(zdom->uzd_imin, bucket->ub_cnt); 704c6fd3e23SJeff Roberson zone_domain_imax_sub(zdom, bucket->ub_cnt); 705c6fd3e23SJeff Roberson } else if (zdom->uzd_imin > zdom->uzd_nitems) 7060f9b7bf3SMark Johnston zdom->uzd_imin = zdom->uzd_nitems; 707c6fd3e23SJeff Roberson 708c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 709d4665eaaSJeff Roberson if (dtor) 710d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 711d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], zone->uz_size, 712d4665eaaSJeff Roberson NULL, SKIP_NONE); 713d4665eaaSJeff Roberson 7140f9b7bf3SMark Johnston return (bucket); 7150f9b7bf3SMark Johnston } 7160f9b7bf3SMark Johnston 71708cfa56eSMark Johnston /* 71808cfa56eSMark Johnston * Insert a full bucket into the specified cache. The "ws" parameter indicates 71908cfa56eSMark Johnston * whether the bucket's contents should be counted as part of the zone's working 720c6fd3e23SJeff Roberson * set. The bucket may be freed if it exceeds the bucket limit. 72108cfa56eSMark Johnston */ 7220f9b7bf3SMark Johnston static void 723c6fd3e23SJeff Roberson zone_put_bucket(uma_zone_t zone, int domain, uma_bucket_t bucket, void *udata, 7240f9b7bf3SMark Johnston const bool ws) 7250f9b7bf3SMark Johnston { 726c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 7270f9b7bf3SMark Johnston 728c6fd3e23SJeff Roberson /* We don't cache empty buckets. This can happen after a reclaim. */ 729c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 730c6fd3e23SJeff Roberson goto out; 731c6fd3e23SJeff Roberson zdom = zone_domain_lock(zone, domain); 732c6fd3e23SJeff Roberson 733c6fd3e23SJeff Roberson /* 734c6fd3e23SJeff Roberson * Conditionally set the maximum number of items. 735c6fd3e23SJeff Roberson */ 7360f9b7bf3SMark Johnston zdom->uzd_nitems += bucket->ub_cnt; 737c6fd3e23SJeff Roberson if (__predict_true(zdom->uzd_nitems < zone->uz_bucket_max)) { 738c6fd3e23SJeff Roberson if (ws) 739c6fd3e23SJeff Roberson zone_domain_imax_set(zdom, zdom->uzd_nitems); 740c6fd3e23SJeff Roberson if (STAILQ_EMPTY(&zdom->uzd_buckets)) 741c6fd3e23SJeff Roberson zdom->uzd_seq = bucket->ub_seq; 7425afdf5c1SMark Johnston 7435afdf5c1SMark Johnston /* 7445afdf5c1SMark Johnston * Try to promote reuse of recently used items. For items 7455afdf5c1SMark Johnston * protected by SMR, try to defer reuse to minimize polling. 7465afdf5c1SMark Johnston */ 7475afdf5c1SMark Johnston if (bucket->ub_seq == SMR_SEQ_INVALID) 7485afdf5c1SMark Johnston STAILQ_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link); 7495afdf5c1SMark Johnston else 750c6fd3e23SJeff Roberson STAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link); 751c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 752c6fd3e23SJeff Roberson return; 753c6fd3e23SJeff Roberson } 754c6fd3e23SJeff Roberson zdom->uzd_nitems -= bucket->ub_cnt; 755c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 756c6fd3e23SJeff Roberson out: 757c6fd3e23SJeff Roberson bucket_free(zone, bucket, udata); 7580f9b7bf3SMark Johnston } 7590f9b7bf3SMark Johnston 760376b1ba3SJeff Roberson /* Pops an item out of a per-cpu cache bucket. */ 761376b1ba3SJeff Roberson static inline void * 762376b1ba3SJeff Roberson cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket) 763376b1ba3SJeff Roberson { 764376b1ba3SJeff Roberson void *item; 765376b1ba3SJeff Roberson 766376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 767376b1ba3SJeff Roberson 768376b1ba3SJeff Roberson bucket->ucb_cnt--; 769376b1ba3SJeff Roberson item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt]; 770376b1ba3SJeff Roberson #ifdef INVARIANTS 771376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL; 772376b1ba3SJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 773376b1ba3SJeff Roberson #endif 774376b1ba3SJeff Roberson cache->uc_allocs++; 775376b1ba3SJeff Roberson 776376b1ba3SJeff Roberson return (item); 777376b1ba3SJeff Roberson } 778376b1ba3SJeff Roberson 779376b1ba3SJeff Roberson /* Pushes an item into a per-cpu cache bucket. */ 780376b1ba3SJeff Roberson static inline void 781376b1ba3SJeff Roberson cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item) 782376b1ba3SJeff Roberson { 783376b1ba3SJeff Roberson 784376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 785376b1ba3SJeff Roberson KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL, 786376b1ba3SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 787376b1ba3SJeff Roberson 788376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item; 789376b1ba3SJeff Roberson bucket->ucb_cnt++; 790376b1ba3SJeff Roberson cache->uc_frees++; 791376b1ba3SJeff Roberson } 792376b1ba3SJeff Roberson 793376b1ba3SJeff Roberson /* 794376b1ba3SJeff Roberson * Unload a UMA bucket from a per-cpu cache. 795376b1ba3SJeff Roberson */ 796376b1ba3SJeff Roberson static inline uma_bucket_t 797376b1ba3SJeff Roberson cache_bucket_unload(uma_cache_bucket_t bucket) 798376b1ba3SJeff Roberson { 799376b1ba3SJeff Roberson uma_bucket_t b; 800376b1ba3SJeff Roberson 801376b1ba3SJeff Roberson b = bucket->ucb_bucket; 802376b1ba3SJeff Roberson if (b != NULL) { 803376b1ba3SJeff Roberson MPASS(b->ub_entries == bucket->ucb_entries); 804376b1ba3SJeff Roberson b->ub_cnt = bucket->ucb_cnt; 805376b1ba3SJeff Roberson bucket->ucb_bucket = NULL; 806376b1ba3SJeff Roberson bucket->ucb_entries = bucket->ucb_cnt = 0; 807376b1ba3SJeff Roberson } 808376b1ba3SJeff Roberson 809376b1ba3SJeff Roberson return (b); 810376b1ba3SJeff Roberson } 811376b1ba3SJeff Roberson 812376b1ba3SJeff Roberson static inline uma_bucket_t 813376b1ba3SJeff Roberson cache_bucket_unload_alloc(uma_cache_t cache) 814376b1ba3SJeff Roberson { 815376b1ba3SJeff Roberson 816376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_allocbucket)); 817376b1ba3SJeff Roberson } 818376b1ba3SJeff Roberson 819376b1ba3SJeff Roberson static inline uma_bucket_t 820376b1ba3SJeff Roberson cache_bucket_unload_free(uma_cache_t cache) 821376b1ba3SJeff Roberson { 822376b1ba3SJeff Roberson 823376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_freebucket)); 824376b1ba3SJeff Roberson } 825376b1ba3SJeff Roberson 826376b1ba3SJeff Roberson static inline uma_bucket_t 827376b1ba3SJeff Roberson cache_bucket_unload_cross(uma_cache_t cache) 828376b1ba3SJeff Roberson { 829376b1ba3SJeff Roberson 830376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_crossbucket)); 831376b1ba3SJeff Roberson } 832376b1ba3SJeff Roberson 833376b1ba3SJeff Roberson /* 834376b1ba3SJeff Roberson * Load a bucket into a per-cpu cache bucket. 835376b1ba3SJeff Roberson */ 836376b1ba3SJeff Roberson static inline void 837376b1ba3SJeff Roberson cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b) 838376b1ba3SJeff Roberson { 839376b1ba3SJeff Roberson 840376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 841376b1ba3SJeff Roberson MPASS(bucket->ucb_bucket == NULL); 842543117beSJeff Roberson MPASS(b->ub_seq == SMR_SEQ_INVALID); 843376b1ba3SJeff Roberson 844376b1ba3SJeff Roberson bucket->ucb_bucket = b; 845376b1ba3SJeff Roberson bucket->ucb_cnt = b->ub_cnt; 846376b1ba3SJeff Roberson bucket->ucb_entries = b->ub_entries; 847376b1ba3SJeff Roberson } 848376b1ba3SJeff Roberson 849376b1ba3SJeff Roberson static inline void 850376b1ba3SJeff Roberson cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b) 851376b1ba3SJeff Roberson { 852376b1ba3SJeff Roberson 853376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_allocbucket, b); 854376b1ba3SJeff Roberson } 855376b1ba3SJeff Roberson 856376b1ba3SJeff Roberson static inline void 857376b1ba3SJeff Roberson cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b) 858376b1ba3SJeff Roberson { 859376b1ba3SJeff Roberson 860376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_freebucket, b); 861376b1ba3SJeff Roberson } 862376b1ba3SJeff Roberson 863dfe13344SJeff Roberson #ifdef NUMA 864376b1ba3SJeff Roberson static inline void 865376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b) 866376b1ba3SJeff Roberson { 867376b1ba3SJeff Roberson 868376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_crossbucket, b); 869376b1ba3SJeff Roberson } 870376b1ba3SJeff Roberson #endif 871376b1ba3SJeff Roberson 872376b1ba3SJeff Roberson /* 873376b1ba3SJeff Roberson * Copy and preserve ucb_spare. 874376b1ba3SJeff Roberson */ 875376b1ba3SJeff Roberson static inline void 876376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 877376b1ba3SJeff Roberson { 878376b1ba3SJeff Roberson 879376b1ba3SJeff Roberson b1->ucb_bucket = b2->ucb_bucket; 880376b1ba3SJeff Roberson b1->ucb_entries = b2->ucb_entries; 881376b1ba3SJeff Roberson b1->ucb_cnt = b2->ucb_cnt; 882376b1ba3SJeff Roberson } 883376b1ba3SJeff Roberson 884376b1ba3SJeff Roberson /* 885376b1ba3SJeff Roberson * Swap two cache buckets. 886376b1ba3SJeff Roberson */ 887376b1ba3SJeff Roberson static inline void 888376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 889376b1ba3SJeff Roberson { 890376b1ba3SJeff Roberson struct uma_cache_bucket b3; 891376b1ba3SJeff Roberson 892376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 893376b1ba3SJeff Roberson 894376b1ba3SJeff Roberson cache_bucket_copy(&b3, b1); 895376b1ba3SJeff Roberson cache_bucket_copy(b1, b2); 896376b1ba3SJeff Roberson cache_bucket_copy(b2, &b3); 897376b1ba3SJeff Roberson } 898376b1ba3SJeff Roberson 899c6fd3e23SJeff Roberson /* 900c6fd3e23SJeff Roberson * Attempt to fetch a bucket from a zone on behalf of the current cpu cache. 901c6fd3e23SJeff Roberson */ 902c6fd3e23SJeff Roberson static uma_bucket_t 903c6fd3e23SJeff Roberson cache_fetch_bucket(uma_zone_t zone, uma_cache_t cache, int domain) 904c6fd3e23SJeff Roberson { 905c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 906c6fd3e23SJeff Roberson uma_bucket_t bucket; 907c6fd3e23SJeff Roberson 908c6fd3e23SJeff Roberson /* 909c6fd3e23SJeff Roberson * Avoid the lock if possible. 910c6fd3e23SJeff Roberson */ 911c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 912c6fd3e23SJeff Roberson if (zdom->uzd_nitems == 0) 913c6fd3e23SJeff Roberson return (NULL); 914c6fd3e23SJeff Roberson 915c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_SMR) != 0 && 916c6fd3e23SJeff Roberson !smr_poll(zone->uz_smr, zdom->uzd_seq, false)) 917c6fd3e23SJeff Roberson return (NULL); 918c6fd3e23SJeff Roberson 919c6fd3e23SJeff Roberson /* 920c6fd3e23SJeff Roberson * Check the zone's cache of buckets. 921c6fd3e23SJeff Roberson */ 922c6fd3e23SJeff Roberson zdom = zone_domain_lock(zone, domain); 92306d8bdcbSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) 924c6fd3e23SJeff Roberson return (bucket); 925c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 926c6fd3e23SJeff Roberson 927c6fd3e23SJeff Roberson return (NULL); 928c6fd3e23SJeff Roberson } 929c6fd3e23SJeff Roberson 9302f891cd5SPawel Jakub Dawidek static void 9312f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 9322f891cd5SPawel Jakub Dawidek { 9332f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 9342f891cd5SPawel Jakub Dawidek 9352f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 9362f891cd5SPawel Jakub Dawidek return; 9372f891cd5SPawel Jakub Dawidek 9382f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 9392f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 9402f891cd5SPawel Jakub Dawidek } 9412f891cd5SPawel Jakub Dawidek 94254503a13SJonathan T. Looney static inline void 94354503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone) 94454503a13SJonathan T. Looney { 945e60b2fcbSGleb Smirnoff 946e60b2fcbSGleb Smirnoff if (zone->uz_maxaction.ta_func != NULL) 947e60b2fcbSGleb Smirnoff taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction); 94854503a13SJonathan T. Looney } 94954503a13SJonathan T. Looney 9508355f576SJeff Roberson /* 9518355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 9529643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 9538355f576SJeff Roberson * 9548355f576SJeff Roberson * Arguments: 9558355f576SJeff Roberson * arg Unused 9568355f576SJeff Roberson * 9578355f576SJeff Roberson * Returns: 9588355f576SJeff Roberson * Nothing 9598355f576SJeff Roberson */ 9608355f576SJeff Roberson static void 9618355f576SJeff Roberson uma_timeout(void *unused) 9628355f576SJeff Roberson { 96386bbae32SJeff Roberson bucket_enable(); 96420a4e154SJeff Roberson zone_foreach(zone_timeout, NULL); 9658355f576SJeff Roberson 9668355f576SJeff Roberson /* Reschedule this event */ 9679643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 9688355f576SJeff Roberson } 9698355f576SJeff Roberson 9708355f576SJeff Roberson /* 9710f9b7bf3SMark Johnston * Update the working set size estimate for the zone's bucket cache. 9720f9b7bf3SMark Johnston * The constants chosen here are somewhat arbitrary. With an update period of 9730f9b7bf3SMark Johnston * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the 9740f9b7bf3SMark Johnston * last 100s. 9750f9b7bf3SMark Johnston */ 9760f9b7bf3SMark Johnston static void 9770f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom) 9780f9b7bf3SMark Johnston { 9790f9b7bf3SMark Johnston long wss; 9800f9b7bf3SMark Johnston 981c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 9820f9b7bf3SMark Johnston MPASS(zdom->uzd_imax >= zdom->uzd_imin); 9830f9b7bf3SMark Johnston wss = zdom->uzd_imax - zdom->uzd_imin; 9840f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems; 98508cfa56eSMark Johnston zdom->uzd_wss = (4 * wss + zdom->uzd_wss) / 5; 986c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 9870f9b7bf3SMark Johnston } 9880f9b7bf3SMark Johnston 9890f9b7bf3SMark Johnston /* 9909643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 9919643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 9928355f576SJeff Roberson * 993e20a199fSJeff Roberson * Returns nothing. 9948355f576SJeff Roberson */ 9958355f576SJeff Roberson static void 99620a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused) 9978355f576SJeff Roberson { 99808034d10SKonstantin Belousov uma_keg_t keg; 9998b987a77SJeff Roberson u_int slabs, pages; 10008355f576SJeff Roberson 100154c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 100208034d10SKonstantin Belousov goto update_wss; 100308034d10SKonstantin Belousov 100408034d10SKonstantin Belousov keg = zone->uz_keg; 10058b987a77SJeff Roberson 10068b987a77SJeff Roberson /* 10078b987a77SJeff Roberson * Hash zones are non-numa by definition so the first domain 10088b987a77SJeff Roberson * is the only one present. 10098b987a77SJeff Roberson */ 10108b987a77SJeff Roberson KEG_LOCK(keg, 0); 10118b987a77SJeff Roberson pages = keg->uk_domain[0].ud_pages; 10128b987a77SJeff Roberson 10138355f576SJeff Roberson /* 1014e20a199fSJeff Roberson * Expand the keg hash table. 10158355f576SJeff Roberson * 10168355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 10178355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 10188355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 10198355f576SJeff Roberson */ 10208b987a77SJeff Roberson if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) { 10210aef6126SJeff Roberson struct uma_hash newhash; 10220aef6126SJeff Roberson struct uma_hash oldhash; 10230aef6126SJeff Roberson int ret; 10245300d9ddSJeff Roberson 10250aef6126SJeff Roberson /* 10260aef6126SJeff Roberson * This is so involved because allocating and freeing 1027e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 10280aef6126SJeff Roberson * I have to do everything in stages and check for 10290aef6126SJeff Roberson * races. 10300aef6126SJeff Roberson */ 10318b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 10323b2f2cb8SAlexander Motin ret = hash_alloc(&newhash, 1 << fls(slabs)); 10338b987a77SJeff Roberson KEG_LOCK(keg, 0); 10340aef6126SJeff Roberson if (ret) { 1035099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 1036099a0e58SBosko Milekic oldhash = keg->uk_hash; 1037099a0e58SBosko Milekic keg->uk_hash = newhash; 10380aef6126SJeff Roberson } else 10390aef6126SJeff Roberson oldhash = newhash; 10400aef6126SJeff Roberson 10418b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 10420aef6126SJeff Roberson hash_free(&oldhash); 10438b987a77SJeff Roberson goto update_wss; 10440aef6126SJeff Roberson } 10455300d9ddSJeff Roberson } 10468b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 1047e20a199fSJeff Roberson 104808034d10SKonstantin Belousov update_wss: 1049bb15d1c7SGleb Smirnoff for (int i = 0; i < vm_ndomains; i++) 1050c6fd3e23SJeff Roberson zone_domain_update_wss(ZDOM_GET(zone, i)); 10518355f576SJeff Roberson } 10528355f576SJeff Roberson 10538355f576SJeff Roberson /* 10545300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 10555300d9ddSJeff Roberson * backing store. 10565300d9ddSJeff Roberson * 10575300d9ddSJeff Roberson * Arguments: 10580aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 10595300d9ddSJeff Roberson * 10605300d9ddSJeff Roberson * Returns: 1061763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 10625300d9ddSJeff Roberson */ 106337c84183SPoul-Henning Kamp static int 10643b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 10655300d9ddSJeff Roberson { 106659568a0eSAlexander Motin size_t alloc; 10675300d9ddSJeff Roberson 10683b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 10693b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 10703b2f2cb8SAlexander Motin hash->uh_hashsize = size; 10710aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 10721e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 10735300d9ddSJeff Roberson } else { 10740aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 1075e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 1076ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 10770aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 10785300d9ddSJeff Roberson } 10790aef6126SJeff Roberson if (hash->uh_slab_hash) { 10800aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 10810aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 10820aef6126SJeff Roberson return (1); 10830aef6126SJeff Roberson } 10845300d9ddSJeff Roberson 10850aef6126SJeff Roberson return (0); 10865300d9ddSJeff Roberson } 10875300d9ddSJeff Roberson 10885300d9ddSJeff Roberson /* 108964f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 109064f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 109164f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 10928355f576SJeff Roberson * 10938355f576SJeff Roberson * Arguments: 10940aef6126SJeff Roberson * oldhash The hash you want to expand 10950aef6126SJeff Roberson * newhash The hash structure for the new table 10968355f576SJeff Roberson * 10978355f576SJeff Roberson * Returns: 10988355f576SJeff Roberson * Nothing 10998355f576SJeff Roberson * 11008355f576SJeff Roberson * Discussion: 11018355f576SJeff Roberson */ 11020aef6126SJeff Roberson static int 11030aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 11048355f576SJeff Roberson { 11051e0701e1SJeff Roberson uma_hash_slab_t slab; 11066929b7d1SPedro F. Giffuni u_int hval; 11076929b7d1SPedro F. Giffuni u_int idx; 11088355f576SJeff Roberson 11090aef6126SJeff Roberson if (!newhash->uh_slab_hash) 11100aef6126SJeff Roberson return (0); 11118355f576SJeff Roberson 11120aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 11130aef6126SJeff Roberson return (0); 11148355f576SJeff Roberson 11158355f576SJeff Roberson /* 11168355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 11178355f576SJeff Roberson * full rehash. 11188355f576SJeff Roberson */ 11198355f576SJeff Roberson 11206929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 11211e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 11221e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 11231e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 11241e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 11251e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 11261e0701e1SJeff Roberson slab, uhs_hlink); 11278355f576SJeff Roberson } 11288355f576SJeff Roberson 11290aef6126SJeff Roberson return (1); 11309c2cd7e5SJeff Roberson } 11319c2cd7e5SJeff Roberson 11325300d9ddSJeff Roberson /* 11335300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 11345300d9ddSJeff Roberson * 11355300d9ddSJeff Roberson * Arguments: 11365300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 11375300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 11385300d9ddSJeff Roberson * 11395300d9ddSJeff Roberson * Returns: 11405300d9ddSJeff Roberson * Nothing 11415300d9ddSJeff Roberson */ 11429c2cd7e5SJeff Roberson static void 11430aef6126SJeff Roberson hash_free(struct uma_hash *hash) 11449c2cd7e5SJeff Roberson { 11450aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 11460aef6126SJeff Roberson return; 11470aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 11480095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 11498355f576SJeff Roberson else 1150961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 11518355f576SJeff Roberson } 11528355f576SJeff Roberson 11538355f576SJeff Roberson /* 11548355f576SJeff Roberson * Frees all outstanding items in a bucket 11558355f576SJeff Roberson * 11568355f576SJeff Roberson * Arguments: 11578355f576SJeff Roberson * zone The zone to free to, must be unlocked. 11584bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 11598355f576SJeff Roberson * 11608355f576SJeff Roberson * Returns: 11618355f576SJeff Roberson * Nothing 11628355f576SJeff Roberson */ 11638355f576SJeff Roberson static void 11648355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 11658355f576SJeff Roberson { 11660095a784SJeff Roberson int i; 11678355f576SJeff Roberson 1168c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 11698355f576SJeff Roberson return; 11708355f576SJeff Roberson 1171d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && 1172d4665eaaSJeff Roberson bucket->ub_seq != SMR_SEQ_INVALID) { 1173d4665eaaSJeff Roberson smr_wait(zone->uz_smr, bucket->ub_seq); 1174543117beSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 1175d4665eaaSJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 1176d4665eaaSJeff Roberson item_dtor(zone, bucket->ub_bucket[i], 1177d4665eaaSJeff Roberson zone->uz_size, NULL, SKIP_NONE); 1178d4665eaaSJeff Roberson } 11790095a784SJeff Roberson if (zone->uz_fini) 11800095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 11810095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 11820095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 11834bd61e19SJeff Roberson if (zone->uz_max_items > 0) 11844bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 1185d4665eaaSJeff Roberson #ifdef INVARIANTS 1186d4665eaaSJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt); 1187d4665eaaSJeff Roberson #endif 11880095a784SJeff Roberson bucket->ub_cnt = 0; 11898355f576SJeff Roberson } 11908355f576SJeff Roberson 11918355f576SJeff Roberson /* 11928355f576SJeff Roberson * Drains the per cpu caches for a zone. 11938355f576SJeff Roberson * 1194727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 11955d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 11965d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 11975d1ae027SRobert Watson * 11988355f576SJeff Roberson * Arguments: 11998355f576SJeff Roberson * zone The zone to drain, must be unlocked. 12008355f576SJeff Roberson * 12018355f576SJeff Roberson * Returns: 12028355f576SJeff Roberson * Nothing 12038355f576SJeff Roberson */ 12048355f576SJeff Roberson static void 12059643769aSJeff Roberson cache_drain(uma_zone_t zone) 12068355f576SJeff Roberson { 12078355f576SJeff Roberson uma_cache_t cache; 1208376b1ba3SJeff Roberson uma_bucket_t bucket; 1209543117beSJeff Roberson smr_seq_t seq; 12108355f576SJeff Roberson int cpu; 12118355f576SJeff Roberson 12128355f576SJeff Roberson /* 12135d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 12145d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 12155d1ae027SRobert Watson * of the caches at this point. 12165d1ae027SRobert Watson * 12175d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 12185d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 12198355f576SJeff Roberson */ 1220543117beSJeff Roberson seq = SMR_SEQ_INVALID; 1221543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 1222226dd6dbSJeff Roberson seq = smr_advance(zone->uz_smr); 12233aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 12248355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 1225376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 1226c6fd3e23SJeff Roberson if (bucket != NULL) 1227376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1228376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 1229376b1ba3SJeff Roberson if (bucket != NULL) { 1230543117beSJeff Roberson bucket->ub_seq = seq; 1231376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1232376b1ba3SJeff Roberson } 1233376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 1234376b1ba3SJeff Roberson if (bucket != NULL) { 1235543117beSJeff Roberson bucket->ub_seq = seq; 1236376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 1237376b1ba3SJeff Roberson } 1238d56368d7SBosko Milekic } 123908cfa56eSMark Johnston bucket_cache_reclaim(zone, true); 1240aaa8bb16SJeff Roberson } 1241aaa8bb16SJeff Roberson 1242a2de44abSAlexander Motin static void 124320a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 1244a2de44abSAlexander Motin { 1245a2de44abSAlexander Motin 1246a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1247a2de44abSAlexander Motin return; 1248a2de44abSAlexander Motin 124920a4e154SJeff Roberson zone->uz_bucket_size = 125020a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 1251a2de44abSAlexander Motin } 1252a2de44abSAlexander Motin 1253a2de44abSAlexander Motin static void 125420a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 1255a2de44abSAlexander Motin { 1256a2de44abSAlexander Motin uma_cache_t cache; 1257c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 1258ab3185d1SJeff Roberson int domain; 1259a2de44abSAlexander Motin 1260a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1261a2de44abSAlexander Motin return; 1262a2de44abSAlexander Motin 1263c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1264a2de44abSAlexander Motin critical_enter(); 1265a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1266c6fd3e23SJeff Roberson domain = PCPU_GET(domain); 1267376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1268d4665eaaSJeff Roberson 1269d4665eaaSJeff Roberson /* 1270d4665eaaSJeff Roberson * Don't flush SMR zone buckets. This leaves the zone without a 1271d4665eaaSJeff Roberson * bucket and forces every free to synchronize(). 1272d4665eaaSJeff Roberson */ 1273543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0) { 1274376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1275543117beSJeff Roberson b3 = cache_bucket_unload_cross(cache); 1276543117beSJeff Roberson } 1277543117beSJeff Roberson critical_exit(); 1278543117beSJeff Roberson 1279543117beSJeff Roberson if (b1 != NULL) 1280c6fd3e23SJeff Roberson zone_free_bucket(zone, b1, NULL, domain, false); 1281543117beSJeff Roberson if (b2 != NULL) 1282c6fd3e23SJeff Roberson zone_free_bucket(zone, b2, NULL, domain, false); 1283543117beSJeff Roberson if (b3 != NULL) { 1284c6fd3e23SJeff Roberson /* Adjust the domain so it goes to zone_free_cross. */ 1285c6fd3e23SJeff Roberson domain = (domain + 1) % vm_ndomains; 1286c6fd3e23SJeff Roberson zone_free_bucket(zone, b3, NULL, domain, false); 1287c1685086SJeff Roberson } 1288a2de44abSAlexander Motin } 1289a2de44abSAlexander Motin 1290a2de44abSAlexander Motin /* 1291a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1292a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1293a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1294a2de44abSAlexander Motin * to safely access their cache buckets. 1295a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1296a2de44abSAlexander Motin */ 1297a2de44abSAlexander Motin static void 129808cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1299a2de44abSAlexander Motin { 1300a2de44abSAlexander Motin int cpu; 1301a2de44abSAlexander Motin 1302a2de44abSAlexander Motin /* 1303727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1304a2de44abSAlexander Motin */ 1305a2de44abSAlexander Motin if (zone) 130620a4e154SJeff Roberson cache_shrink(zone, NULL); 1307a2de44abSAlexander Motin else 130820a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1309a2de44abSAlexander Motin 1310a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1311a2de44abSAlexander Motin thread_lock(curthread); 1312a2de44abSAlexander Motin sched_bind(curthread, cpu); 1313a2de44abSAlexander Motin thread_unlock(curthread); 1314a2de44abSAlexander Motin 1315a2de44abSAlexander Motin if (zone) 131620a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1317a2de44abSAlexander Motin else 131820a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1319a2de44abSAlexander Motin } 1320a2de44abSAlexander Motin thread_lock(curthread); 1321a2de44abSAlexander Motin sched_unbind(curthread); 1322a2de44abSAlexander Motin thread_unlock(curthread); 1323a2de44abSAlexander Motin } 1324a2de44abSAlexander Motin 1325aaa8bb16SJeff Roberson /* 132608cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 132708cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 132808cfa56eSMark Johnston * estimated working set size. 1329aaa8bb16SJeff Roberson */ 1330aaa8bb16SJeff Roberson static void 133108cfa56eSMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain) 1332aaa8bb16SJeff Roberson { 1333ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1334aaa8bb16SJeff Roberson uma_bucket_t bucket; 1335c6fd3e23SJeff Roberson long target; 1336ab3185d1SJeff Roberson int i; 13378355f576SJeff Roberson 1338c6fd3e23SJeff Roberson /* 1339c6fd3e23SJeff Roberson * Shrink the zone bucket size to ensure that the per-CPU caches 1340c6fd3e23SJeff Roberson * don't grow too large. 1341c6fd3e23SJeff Roberson */ 1342c6fd3e23SJeff Roberson if (zone->uz_bucket_size > zone->uz_bucket_size_min) 1343c6fd3e23SJeff Roberson zone->uz_bucket_size--; 1344c6fd3e23SJeff Roberson 1345ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 134691d947bfSJeff Roberson /* 134791d947bfSJeff Roberson * The cross bucket is partially filled and not part of 134891d947bfSJeff Roberson * the item count. Reclaim it individually here. 134991d947bfSJeff Roberson */ 1350c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 1351226dd6dbSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 || drain) { 135291d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 135391d947bfSJeff Roberson bucket = zdom->uzd_cross; 135491d947bfSJeff Roberson zdom->uzd_cross = NULL; 135591d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 1356c6fd3e23SJeff Roberson if (bucket != NULL) 135791d947bfSJeff Roberson bucket_free(zone, bucket, NULL); 135891d947bfSJeff Roberson } 135991d947bfSJeff Roberson 136091d947bfSJeff Roberson /* 136108cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 136208cfa56eSMark Johnston * this bucket cache is empty. Otherwise, we reclaim items in 136308cfa56eSMark Johnston * excess of the zone's estimated working set size. If the 136408cfa56eSMark Johnston * difference nitems - imin is larger than the WSS estimate, 136508cfa56eSMark Johnston * then the estimate will grow at the end of this interval and 136608cfa56eSMark Johnston * we ignore the historical average. 136708cfa56eSMark Johnston */ 1368c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 136908cfa56eSMark Johnston target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems - 137008cfa56eSMark Johnston zdom->uzd_imin); 137108cfa56eSMark Johnston while (zdom->uzd_nitems > target) { 1372c6fd3e23SJeff Roberson bucket = zone_fetch_bucket(zone, zdom, true); 137308cfa56eSMark Johnston if (bucket == NULL) 137408cfa56eSMark Johnston break; 13756fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 1376c6fd3e23SJeff Roberson ZDOM_LOCK(zdom); 13778355f576SJeff Roberson } 1378c6fd3e23SJeff Roberson ZDOM_UNLOCK(zdom); 1379ab3185d1SJeff Roberson } 13808355f576SJeff Roberson } 1381fc03d22bSJeff Roberson 1382fc03d22bSJeff Roberson static void 1383fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1384fc03d22bSJeff Roberson { 1385fc03d22bSJeff Roberson uint8_t *mem; 1386fc03d22bSJeff Roberson int i; 1387fc03d22bSJeff Roberson uint8_t flags; 1388fc03d22bSJeff Roberson 13891431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 13901431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 13911431a748SGleb Smirnoff 13921e0701e1SJeff Roberson mem = slab_data(slab, keg); 1393fc03d22bSJeff Roberson flags = slab->us_flags; 1394fc03d22bSJeff Roberson i = start; 1395fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 1396fc03d22bSJeff Roberson for (i--; i > -1; i--) 1397c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1398c5deaf04SGleb Smirnoff /* 1399c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1400c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1401c5deaf04SGleb Smirnoff * which executed trash_dtor. 1402c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1403c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1404c5deaf04SGleb Smirnoff * invocations. 1405c5deaf04SGleb Smirnoff */ 14061e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1407c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1408c5deaf04SGleb Smirnoff #endif 14091e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1410fc03d22bSJeff Roberson } 141154c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 14129b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab), 14139b8db4d0SRyan Libby NULL, SKIP_NONE); 1414fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 14152e47807cSJeff Roberson uma_total_dec(PAGE_SIZE * keg->uk_ppera); 14168355f576SJeff Roberson } 14178355f576SJeff Roberson 1418f09cbea3SMark Johnston static void 1419f09cbea3SMark Johnston keg_drain_domain(uma_keg_t keg, int domain) 1420f09cbea3SMark Johnston { 1421f09cbea3SMark Johnston struct slabhead freeslabs; 1422f09cbea3SMark Johnston uma_domain_t dom; 1423f09cbea3SMark Johnston uma_slab_t slab, tmp; 1424f09cbea3SMark Johnston uint32_t i, stofree, stokeep, partial; 1425f09cbea3SMark Johnston 1426f09cbea3SMark Johnston dom = &keg->uk_domain[domain]; 1427f09cbea3SMark Johnston LIST_INIT(&freeslabs); 1428f09cbea3SMark Johnston 1429f09cbea3SMark Johnston CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u", 1430575a4437SEd Maste keg->uk_name, keg, domain, dom->ud_free_items); 1431f09cbea3SMark Johnston 1432f09cbea3SMark Johnston KEG_LOCK(keg, domain); 1433f09cbea3SMark Johnston 1434f09cbea3SMark Johnston /* 1435f09cbea3SMark Johnston * Are the free items in partially allocated slabs sufficient to meet 1436f09cbea3SMark Johnston * the reserve? If not, compute the number of fully free slabs that must 1437f09cbea3SMark Johnston * be kept. 1438f09cbea3SMark Johnston */ 1439f09cbea3SMark Johnston partial = dom->ud_free_items - dom->ud_free_slabs * keg->uk_ipers; 1440f09cbea3SMark Johnston if (partial < keg->uk_reserve) { 1441f09cbea3SMark Johnston stokeep = min(dom->ud_free_slabs, 1442f09cbea3SMark Johnston howmany(keg->uk_reserve - partial, keg->uk_ipers)); 1443f09cbea3SMark Johnston } else { 1444f09cbea3SMark Johnston stokeep = 0; 1445f09cbea3SMark Johnston } 1446f09cbea3SMark Johnston stofree = dom->ud_free_slabs - stokeep; 1447f09cbea3SMark Johnston 1448f09cbea3SMark Johnston /* 1449f09cbea3SMark Johnston * Partition the free slabs into two sets: those that must be kept in 1450f09cbea3SMark Johnston * order to maintain the reserve, and those that may be released back to 1451f09cbea3SMark Johnston * the system. Since one set may be much larger than the other, 1452f09cbea3SMark Johnston * populate the smaller of the two sets and swap them if necessary. 1453f09cbea3SMark Johnston */ 1454f09cbea3SMark Johnston for (i = min(stofree, stokeep); i > 0; i--) { 1455f09cbea3SMark Johnston slab = LIST_FIRST(&dom->ud_free_slab); 1456f09cbea3SMark Johnston LIST_REMOVE(slab, us_link); 1457f09cbea3SMark Johnston LIST_INSERT_HEAD(&freeslabs, slab, us_link); 1458f09cbea3SMark Johnston } 1459f09cbea3SMark Johnston if (stofree > stokeep) 1460f09cbea3SMark Johnston LIST_SWAP(&freeslabs, &dom->ud_free_slab, uma_slab, us_link); 1461f09cbea3SMark Johnston 1462f09cbea3SMark Johnston if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) { 1463f09cbea3SMark Johnston LIST_FOREACH(slab, &freeslabs, us_link) 1464f09cbea3SMark Johnston UMA_HASH_REMOVE(&keg->uk_hash, slab); 1465f09cbea3SMark Johnston } 1466f09cbea3SMark Johnston dom->ud_free_items -= stofree * keg->uk_ipers; 1467f09cbea3SMark Johnston dom->ud_free_slabs -= stofree; 1468f09cbea3SMark Johnston dom->ud_pages -= stofree * keg->uk_ppera; 1469f09cbea3SMark Johnston KEG_UNLOCK(keg, domain); 1470f09cbea3SMark Johnston 1471f09cbea3SMark Johnston LIST_FOREACH_SAFE(slab, &freeslabs, us_link, tmp) 1472f09cbea3SMark Johnston keg_free_slab(keg, slab, keg->uk_ipers); 1473f09cbea3SMark Johnston } 1474f09cbea3SMark Johnston 14758355f576SJeff Roberson /* 1476e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 14778355f576SJeff Roberson * the pageout daemon. 14788355f576SJeff Roberson * 1479e20a199fSJeff Roberson * Returns nothing. 14808355f576SJeff Roberson */ 1481e20a199fSJeff Roberson static void 1482e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 14838355f576SJeff Roberson { 1484f09cbea3SMark Johnston int i; 14858355f576SJeff Roberson 1486f09cbea3SMark Johnston if ((keg->uk_flags & UMA_ZONE_NOFREE) != 0) 14878355f576SJeff Roberson return; 1488f09cbea3SMark Johnston for (i = 0; i < vm_ndomains; i++) 1489f09cbea3SMark Johnston keg_drain_domain(keg, i); 14908355f576SJeff Roberson } 14918355f576SJeff Roberson 1492e20a199fSJeff Roberson static void 149308cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain) 1494e20a199fSJeff Roberson { 1495e20a199fSJeff Roberson 14968355f576SJeff Roberson /* 1497e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 1498e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 1499e20a199fSJeff Roberson * is the only call that knows the structure will still be available 1500e20a199fSJeff Roberson * when it wakes up. 1501e20a199fSJeff Roberson */ 1502e20a199fSJeff Roberson ZONE_LOCK(zone); 150308cfa56eSMark Johnston while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) { 1504e20a199fSJeff Roberson if (waitok == M_NOWAIT) 1505e20a199fSJeff Roberson goto out; 1506c6fd3e23SJeff Roberson msleep(zone, &ZDOM_GET(zone, 0)->uzd_lock, PVM, "zonedrain", 1507c6fd3e23SJeff Roberson 1); 1508e20a199fSJeff Roberson } 150908cfa56eSMark Johnston zone->uz_flags |= UMA_ZFLAG_RECLAIMING; 1510e20a199fSJeff Roberson ZONE_UNLOCK(zone); 151191d947bfSJeff Roberson bucket_cache_reclaim(zone, drain); 151208cfa56eSMark Johnston 1513e20a199fSJeff Roberson /* 1514e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 1515111fbcd5SBryan Venteicher * we're running. Normally the uma_rwlock would protect us but we 1516e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 1517e20a199fSJeff Roberson */ 151808034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1519bb15d1c7SGleb Smirnoff keg_drain(zone->uz_keg); 1520e20a199fSJeff Roberson ZONE_LOCK(zone); 152108cfa56eSMark Johnston zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING; 1522e20a199fSJeff Roberson wakeup(zone); 1523e20a199fSJeff Roberson out: 1524e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1525e20a199fSJeff Roberson } 1526e20a199fSJeff Roberson 152708cfa56eSMark Johnston static void 152820a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused) 1529e20a199fSJeff Roberson { 1530e20a199fSJeff Roberson 153108cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, true); 153208cfa56eSMark Johnston } 153308cfa56eSMark Johnston 153408cfa56eSMark Johnston static void 153520a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused) 153608cfa56eSMark Johnston { 153708cfa56eSMark Johnston 153808cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, false); 1539e20a199fSJeff Roberson } 1540e20a199fSJeff Roberson 1541e20a199fSJeff Roberson /* 15428b987a77SJeff Roberson * Allocate a new slab for a keg and inserts it into the partial slab list. 15438b987a77SJeff Roberson * The keg should be unlocked on entry. If the allocation succeeds it will 15448b987a77SJeff Roberson * be locked on return. 15458355f576SJeff Roberson * 15468355f576SJeff Roberson * Arguments: 154786220393SMark Johnston * flags Wait flags for the item initialization routine 154886220393SMark Johnston * aflags Wait flags for the slab allocation 15498355f576SJeff Roberson * 15508355f576SJeff Roberson * Returns: 15518355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 15528355f576SJeff Roberson * caller specified M_NOWAIT. 15538355f576SJeff Roberson */ 15548355f576SJeff Roberson static uma_slab_t 155586220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 155686220393SMark Johnston int aflags) 15578355f576SJeff Roberson { 15588b987a77SJeff Roberson uma_domain_t dom; 1559e20a199fSJeff Roberson uma_alloc allocf; 1560099a0e58SBosko Milekic uma_slab_t slab; 15612e47807cSJeff Roberson unsigned long size; 156285dcf349SGleb Smirnoff uint8_t *mem; 156386220393SMark Johnston uint8_t sflags; 15648355f576SJeff Roberson int i; 15658355f576SJeff Roberson 1566ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1567ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1568a553d4b8SJeff Roberson 15698b987a77SJeff Roberson allocf = keg->uk_allocf; 1570194a979eSMark Johnston slab = NULL; 1571194a979eSMark Johnston mem = NULL; 157254c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 15739b8db4d0SRyan Libby uma_hash_slab_t hslab; 15749b8db4d0SRyan Libby hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL, 15759b8db4d0SRyan Libby domain, aflags); 15769b8db4d0SRyan Libby if (hslab == NULL) 1577727c6918SJeff Roberson goto fail; 15789b8db4d0SRyan Libby slab = &hslab->uhs_slab; 1579a553d4b8SJeff Roberson } 1580a553d4b8SJeff Roberson 15813370c5bfSJeff Roberson /* 15823370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 15833370c5bfSJeff Roberson * first time they are added to a zone. 15843370c5bfSJeff Roberson * 15853370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 15863370c5bfSJeff Roberson */ 15873370c5bfSJeff Roberson 1588099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 158986220393SMark Johnston aflags |= M_ZERO; 15903370c5bfSJeff Roberson else 159186220393SMark Johnston aflags &= ~M_ZERO; 15923370c5bfSJeff Roberson 1593263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 159486220393SMark Johnston aflags |= M_NODUMP; 1595263811f7SKip Macy 1596e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1597194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 159886220393SMark Johnston mem = allocf(zone, size, domain, &sflags, aflags); 1599a553d4b8SJeff Roberson if (mem == NULL) { 160054c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 16019b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), 16029b8db4d0SRyan Libby slab_tohashslab(slab), NULL, SKIP_NONE); 1603727c6918SJeff Roberson goto fail; 1604a553d4b8SJeff Roberson } 16052e47807cSJeff Roberson uma_total_inc(size); 16068355f576SJeff Roberson 16078b987a77SJeff Roberson /* For HASH zones all pages go to the same uma_domain. */ 160854c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 16098b987a77SJeff Roberson domain = 0; 16108b987a77SJeff Roberson 16115c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 161254c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) 1613099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 16141e0701e1SJeff Roberson else 16159b8db4d0SRyan Libby slab_tohashslab(slab)->uhs_data = mem; 16165c0e403bSJeff Roberson 161754c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_VTOSLAB) 1618099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1619584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1620584061b4SJeff Roberson zone, slab); 16218355f576SJeff Roberson 1622099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 162386220393SMark Johnston slab->us_flags = sflags; 1624ab3185d1SJeff Roberson slab->us_domain = domain; 16258b987a77SJeff Roberson 16269b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1627ef72505eSJeff Roberson #ifdef INVARIANTS 1628815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1629ef72505eSJeff Roberson #endif 1630099a0e58SBosko Milekic 1631b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1632099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 16331e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 163486220393SMark Johnston keg->uk_size, flags) != 0) 1635b23f72e9SBrian Feldman break; 1636b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1637fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1638727c6918SJeff Roberson goto fail; 1639b23f72e9SBrian Feldman } 1640b23f72e9SBrian Feldman } 16418b987a77SJeff Roberson KEG_LOCK(keg, domain); 16425c0e403bSJeff Roberson 16431431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 16441431a748SGleb Smirnoff slab, keg->uk_name, keg); 16451431a748SGleb Smirnoff 164654c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 1647099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 16488355f576SJeff Roberson 16498b987a77SJeff Roberson /* 16508b987a77SJeff Roberson * If we got a slab here it's safe to mark it partially used 16518b987a77SJeff Roberson * and return. We assume that the caller is going to remove 16528b987a77SJeff Roberson * at least one item. 16538b987a77SJeff Roberson */ 16548b987a77SJeff Roberson dom = &keg->uk_domain[domain]; 16558b987a77SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 16568b987a77SJeff Roberson dom->ud_pages += keg->uk_ppera; 16574ab3aee8SMark Johnston dom->ud_free_items += keg->uk_ipers; 16588355f576SJeff Roberson 16598355f576SJeff Roberson return (slab); 1660727c6918SJeff Roberson 1661727c6918SJeff Roberson fail: 1662727c6918SJeff Roberson return (NULL); 16638355f576SJeff Roberson } 16648355f576SJeff Roberson 16658355f576SJeff Roberson /* 1666009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1667009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1668009b6fcbSJeff Roberson * the VM is ready. 1669009b6fcbSJeff Roberson */ 1670009b6fcbSJeff Roberson static void * 1671ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1672ab3185d1SJeff Roberson int wait) 1673009b6fcbSJeff Roberson { 1674a81c400eSJeff Roberson vm_paddr_t pa; 1675a81c400eSJeff Roberson vm_page_t m; 1676ac0a6fd0SGleb Smirnoff void *mem; 1677ac0a6fd0SGleb Smirnoff int pages; 1678a81c400eSJeff Roberson int i; 1679099a0e58SBosko Milekic 1680f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1681f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1682a81c400eSJeff Roberson 1683f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1684a81c400eSJeff Roberson m = vm_page_alloc_contig_domain(NULL, 0, domain, 1685a81c400eSJeff Roberson malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages, 1686a81c400eSJeff Roberson (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT); 1687a81c400eSJeff Roberson if (m == NULL) 1688a81c400eSJeff Roberson return (NULL); 1689a81c400eSJeff Roberson 1690a81c400eSJeff Roberson pa = VM_PAGE_TO_PHYS(m); 1691a81c400eSJeff Roberson for (i = 0; i < pages; i++, pa += PAGE_SIZE) { 1692a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1693a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1694a81c400eSJeff Roberson if ((wait & M_NODUMP) == 0) 1695a81c400eSJeff Roberson dump_add_page(pa); 1696a81c400eSJeff Roberson #endif 1697a81c400eSJeff Roberson } 1698a81c400eSJeff Roberson /* Allocate KVA and indirectly advance bootmem. */ 1699a81c400eSJeff Roberson mem = (void *)pmap_map(&bootmem, m->phys_addr, 1700a81c400eSJeff Roberson m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE); 1701a81c400eSJeff Roberson if ((wait & M_ZERO) != 0) 1702a81c400eSJeff Roberson bzero(mem, pages * PAGE_SIZE); 1703f7d35785SGleb Smirnoff 1704f7d35785SGleb Smirnoff return (mem); 1705f7d35785SGleb Smirnoff } 1706f7d35785SGleb Smirnoff 1707a81c400eSJeff Roberson static void 1708a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes) 1709a81c400eSJeff Roberson { 1710a81c400eSJeff Roberson vm_offset_t va; 1711a81c400eSJeff Roberson vm_page_t m; 1712a81c400eSJeff Roberson 1713a81c400eSJeff Roberson va = (vm_offset_t)mem; 1714a81c400eSJeff Roberson m = PHYS_TO_VM_PAGE(pmap_kextract(va)); 1715a81c400eSJeff Roberson pmap_remove(kernel_pmap, va, va + bytes); 1716a81c400eSJeff Roberson for (; bytes != 0; bytes -= PAGE_SIZE, m++) { 1717a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1718a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1719a81c400eSJeff Roberson dump_drop_page(VM_PAGE_TO_PHYS(m)); 1720a81c400eSJeff Roberson #endif 1721a81c400eSJeff Roberson vm_page_unwire_noq(m); 1722a81c400eSJeff Roberson vm_page_free(m); 1723a81c400eSJeff Roberson } 1724a81c400eSJeff Roberson } 1725a81c400eSJeff Roberson 1726f7d35785SGleb Smirnoff /* 17278355f576SJeff Roberson * Allocates a number of pages from the system 17288355f576SJeff Roberson * 17298355f576SJeff Roberson * Arguments: 17308355f576SJeff Roberson * bytes The number of bytes requested 17318355f576SJeff Roberson * wait Shall we wait? 17328355f576SJeff Roberson * 17338355f576SJeff Roberson * Returns: 17348355f576SJeff Roberson * A pointer to the alloced memory or possibly 17358355f576SJeff Roberson * NULL if M_NOWAIT is set. 17368355f576SJeff Roberson */ 17378355f576SJeff Roberson static void * 1738ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1739ab3185d1SJeff Roberson int wait) 17408355f576SJeff Roberson { 17418355f576SJeff Roberson void *p; /* Returned page */ 17428355f576SJeff Roberson 17432e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 17449978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 17458355f576SJeff Roberson 17468355f576SJeff Roberson return (p); 17478355f576SJeff Roberson } 17488355f576SJeff Roberson 1749ab3059a8SMatt Macy static void * 1750ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1751ab3059a8SMatt Macy int wait) 1752ab3059a8SMatt Macy { 1753ab3059a8SMatt Macy struct pglist alloctail; 1754ab3059a8SMatt Macy vm_offset_t addr, zkva; 1755ab3059a8SMatt Macy int cpu, flags; 1756ab3059a8SMatt Macy vm_page_t p, p_next; 1757ab3059a8SMatt Macy #ifdef NUMA 1758ab3059a8SMatt Macy struct pcpu *pc; 1759ab3059a8SMatt Macy #endif 1760ab3059a8SMatt Macy 1761ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1762ab3059a8SMatt Macy 1763013072f0SMark Johnston TAILQ_INIT(&alloctail); 1764ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1765013072f0SMark Johnston malloc2vm_flags(wait); 1766013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1767ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1768ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1769ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1770ab3059a8SMatt Macy } else { 1771ab3059a8SMatt Macy #ifndef NUMA 1772ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1773ab3059a8SMatt Macy #else 1774ab3059a8SMatt Macy pc = pcpu_find(cpu); 177520526802SAndrew Gallatin if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain))) 177620526802SAndrew Gallatin p = NULL; 177720526802SAndrew Gallatin else 177820526802SAndrew Gallatin p = vm_page_alloc_domain(NULL, 0, 177920526802SAndrew Gallatin pc->pc_domain, flags); 1780ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1781ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1782ab3059a8SMatt Macy #endif 1783ab3059a8SMatt Macy } 1784ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1785ab3059a8SMatt Macy goto fail; 1786ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1787ab3059a8SMatt Macy } 1788ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1789ab3059a8SMatt Macy goto fail; 1790ab3059a8SMatt Macy zkva = addr; 1791ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1792ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1793ab3059a8SMatt Macy zkva += PAGE_SIZE; 1794ab3059a8SMatt Macy } 1795ab3059a8SMatt Macy return ((void*)addr); 1796ab3059a8SMatt Macy fail: 1797ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 179888ea538aSMark Johnston vm_page_unwire_noq(p); 1799ab3059a8SMatt Macy vm_page_free(p); 1800ab3059a8SMatt Macy } 1801ab3059a8SMatt Macy return (NULL); 1802ab3059a8SMatt Macy } 1803ab3059a8SMatt Macy 18048355f576SJeff Roberson /* 18058355f576SJeff Roberson * Allocates a number of pages from within an object 18068355f576SJeff Roberson * 18078355f576SJeff Roberson * Arguments: 18088355f576SJeff Roberson * bytes The number of bytes requested 18098355f576SJeff Roberson * wait Shall we wait? 18108355f576SJeff Roberson * 18118355f576SJeff Roberson * Returns: 18128355f576SJeff Roberson * A pointer to the alloced memory or possibly 18138355f576SJeff Roberson * NULL if M_NOWAIT is set. 18148355f576SJeff Roberson */ 18158355f576SJeff Roberson static void * 1816ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1817ab3185d1SJeff Roberson int wait) 18188355f576SJeff Roberson { 1819a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1820a4915c21SAttilio Rao u_long npages; 1821b245ac95SAlan Cox vm_offset_t retkva, zkva; 1822a4915c21SAttilio Rao vm_page_t p, p_next; 1823e20a199fSJeff Roberson uma_keg_t keg; 18248355f576SJeff Roberson 1825a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1826bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1827a4915c21SAttilio Rao 1828a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1829a4915c21SAttilio Rao while (npages > 0) { 1830ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 18318d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1832772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1833772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1834a4915c21SAttilio Rao if (p != NULL) { 1835a4915c21SAttilio Rao /* 1836a4915c21SAttilio Rao * Since the page does not belong to an object, its 1837a4915c21SAttilio Rao * listq is unused. 1838a4915c21SAttilio Rao */ 1839a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1840a4915c21SAttilio Rao npages--; 1841a4915c21SAttilio Rao continue; 1842a4915c21SAttilio Rao } 18438355f576SJeff Roberson /* 1844a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1845a4915c21SAttilio Rao * exit. 18468355f576SJeff Roberson */ 1847a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 184888ea538aSMark Johnston vm_page_unwire_noq(p); 1849b245ac95SAlan Cox vm_page_free(p); 1850b245ac95SAlan Cox } 1851a4915c21SAttilio Rao return (NULL); 1852b245ac95SAlan Cox } 18538355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1854a4915c21SAttilio Rao zkva = keg->uk_kva + 1855a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1856a4915c21SAttilio Rao retkva = zkva; 1857a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1858a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1859a4915c21SAttilio Rao zkva += PAGE_SIZE; 1860a4915c21SAttilio Rao } 18618355f576SJeff Roberson 18628355f576SJeff Roberson return ((void *)retkva); 18638355f576SJeff Roberson } 18648355f576SJeff Roberson 18658355f576SJeff Roberson /* 1866ec0d8280SRyan Libby * Allocate physically contiguous pages. 1867ec0d8280SRyan Libby */ 1868ec0d8280SRyan Libby static void * 1869ec0d8280SRyan Libby contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1870ec0d8280SRyan Libby int wait) 1871ec0d8280SRyan Libby { 1872ec0d8280SRyan Libby 1873ec0d8280SRyan Libby *pflag = UMA_SLAB_KERNEL; 1874ec0d8280SRyan Libby return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain), 1875ec0d8280SRyan Libby bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT)); 1876ec0d8280SRyan Libby } 1877ec0d8280SRyan Libby 1878ec0d8280SRyan Libby /* 18798355f576SJeff Roberson * Frees a number of pages to the system 18808355f576SJeff Roberson * 18818355f576SJeff Roberson * Arguments: 18828355f576SJeff Roberson * mem A pointer to the memory to be freed 18838355f576SJeff Roberson * size The size of the memory being freed 18848355f576SJeff Roberson * flags The original p->us_flags field 18858355f576SJeff Roberson * 18868355f576SJeff Roberson * Returns: 18878355f576SJeff Roberson * Nothing 18888355f576SJeff Roberson */ 18898355f576SJeff Roberson static void 1890f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 18918355f576SJeff Roberson { 18923370c5bfSJeff Roberson 1893a81c400eSJeff Roberson if ((flags & UMA_SLAB_BOOT) != 0) { 1894a81c400eSJeff Roberson startup_free(mem, size); 1895a81c400eSJeff Roberson return; 1896a81c400eSJeff Roberson } 1897a81c400eSJeff Roberson 1898ec0d8280SRyan Libby KASSERT((flags & UMA_SLAB_KERNEL) != 0, 1899ec0d8280SRyan Libby ("UMA: page_free used with invalid flags %x", flags)); 19008355f576SJeff Roberson 190149bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 19028355f576SJeff Roberson } 19038355f576SJeff Roberson 19048355f576SJeff Roberson /* 1905ab3059a8SMatt Macy * Frees pcpu zone allocations 1906ab3059a8SMatt Macy * 1907ab3059a8SMatt Macy * Arguments: 1908ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1909ab3059a8SMatt Macy * size The size of the memory being freed 1910ab3059a8SMatt Macy * flags The original p->us_flags field 1911ab3059a8SMatt Macy * 1912ab3059a8SMatt Macy * Returns: 1913ab3059a8SMatt Macy * Nothing 1914ab3059a8SMatt Macy */ 1915ab3059a8SMatt Macy static void 1916ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1917ab3059a8SMatt Macy { 1918ab3059a8SMatt Macy vm_offset_t sva, curva; 1919ab3059a8SMatt Macy vm_paddr_t paddr; 1920ab3059a8SMatt Macy vm_page_t m; 1921ab3059a8SMatt Macy 1922ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 19235ba16cf3SRyan Libby 19245ba16cf3SRyan Libby if ((flags & UMA_SLAB_BOOT) != 0) { 19255ba16cf3SRyan Libby startup_free(mem, size); 19265ba16cf3SRyan Libby return; 19275ba16cf3SRyan Libby } 19285ba16cf3SRyan Libby 1929ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1930ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1931ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1932ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 193388ea538aSMark Johnston vm_page_unwire_noq(m); 1934ab3059a8SMatt Macy vm_page_free(m); 1935ab3059a8SMatt Macy } 1936ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1937ab3059a8SMatt Macy kva_free(sva, size); 1938ab3059a8SMatt Macy } 1939ab3059a8SMatt Macy 1940ab3059a8SMatt Macy /* 19418355f576SJeff Roberson * Zero fill initializer 19428355f576SJeff Roberson * 19438355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 19448355f576SJeff Roberson */ 1945b23f72e9SBrian Feldman static int 1946b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 19478355f576SJeff Roberson { 19488355f576SJeff Roberson bzero(mem, size); 1949b23f72e9SBrian Feldman return (0); 19508355f576SJeff Roberson } 19518355f576SJeff Roberson 1952815db204SRyan Libby #ifdef INVARIANTS 195354007ce8SMark Johnston static struct noslabbits * 1954815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1955815db204SRyan Libby { 1956815db204SRyan Libby 1957815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1958815db204SRyan Libby } 1959815db204SRyan Libby #endif 1960815db204SRyan Libby 19618355f576SJeff Roberson /* 19629b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 19639b78b1f4SJeff Roberson */ 196454007ce8SMark Johnston static size_t 19659b78b1f4SJeff Roberson slab_sizeof(int nitems) 19669b78b1f4SJeff Roberson { 19679b78b1f4SJeff Roberson size_t s; 19689b78b1f4SJeff Roberson 1969815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 19709b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 19719b78b1f4SJeff Roberson } 19729b78b1f4SJeff Roberson 19734a8b575cSRyan Libby #define UMA_FIXPT_SHIFT 31 19744a8b575cSRyan Libby #define UMA_FRAC_FIXPT(n, d) \ 19754a8b575cSRyan Libby ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d))) 19764a8b575cSRyan Libby #define UMA_FIXPT_PCT(f) \ 19774a8b575cSRyan Libby ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT)) 19784a8b575cSRyan Libby #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100) 19794a8b575cSRyan Libby #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE) 19804a8b575cSRyan Libby 19819b78b1f4SJeff Roberson /* 19824a8b575cSRyan Libby * Compute the number of items that will fit in a slab. If hdr is true, the 19834a8b575cSRyan Libby * item count may be limited to provide space in the slab for an inline slab 19844a8b575cSRyan Libby * header. Otherwise, all slab space will be provided for item storage. 19854a8b575cSRyan Libby */ 19864a8b575cSRyan Libby static u_int 19874a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr) 19884a8b575cSRyan Libby { 19894a8b575cSRyan Libby u_int ipers; 19904a8b575cSRyan Libby u_int padpi; 19914a8b575cSRyan Libby 19924a8b575cSRyan Libby /* The padding between items is not needed after the last item. */ 19934a8b575cSRyan Libby padpi = rsize - size; 19944a8b575cSRyan Libby 19954a8b575cSRyan Libby if (hdr) { 19964a8b575cSRyan Libby /* 19974a8b575cSRyan Libby * Start with the maximum item count and remove items until 19984a8b575cSRyan Libby * the slab header first alongside the allocatable memory. 19994a8b575cSRyan Libby */ 20004a8b575cSRyan Libby for (ipers = MIN(SLAB_MAX_SETSIZE, 20014a8b575cSRyan Libby (slabsize + padpi - slab_sizeof(1)) / rsize); 20024a8b575cSRyan Libby ipers > 0 && 20034a8b575cSRyan Libby ipers * rsize - padpi + slab_sizeof(ipers) > slabsize; 20044a8b575cSRyan Libby ipers--) 20054a8b575cSRyan Libby continue; 20064a8b575cSRyan Libby } else { 20074a8b575cSRyan Libby ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE); 20084a8b575cSRyan Libby } 20094a8b575cSRyan Libby 20104a8b575cSRyan Libby return (ipers); 20114a8b575cSRyan Libby } 20124a8b575cSRyan Libby 201327ca37acSRyan Libby struct keg_layout_result { 201427ca37acSRyan Libby u_int format; 201527ca37acSRyan Libby u_int slabsize; 201627ca37acSRyan Libby u_int ipers; 201727ca37acSRyan Libby u_int eff; 201827ca37acSRyan Libby }; 201927ca37acSRyan Libby 202027ca37acSRyan Libby static void 202127ca37acSRyan Libby keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt, 202227ca37acSRyan Libby struct keg_layout_result *kl) 202327ca37acSRyan Libby { 202427ca37acSRyan Libby u_int total; 202527ca37acSRyan Libby 202627ca37acSRyan Libby kl->format = fmt; 202727ca37acSRyan Libby kl->slabsize = slabsize; 202827ca37acSRyan Libby 202927ca37acSRyan Libby /* Handle INTERNAL as inline with an extra page. */ 203027ca37acSRyan Libby if ((fmt & UMA_ZFLAG_INTERNAL) != 0) { 203127ca37acSRyan Libby kl->format &= ~UMA_ZFLAG_INTERNAL; 203227ca37acSRyan Libby kl->slabsize += PAGE_SIZE; 203327ca37acSRyan Libby } 203427ca37acSRyan Libby 203527ca37acSRyan Libby kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize, 203627ca37acSRyan Libby (fmt & UMA_ZFLAG_OFFPAGE) == 0); 203727ca37acSRyan Libby 203827ca37acSRyan Libby /* Account for memory used by an offpage slab header. */ 203927ca37acSRyan Libby total = kl->slabsize; 204027ca37acSRyan Libby if ((fmt & UMA_ZFLAG_OFFPAGE) != 0) 204127ca37acSRyan Libby total += slabzone(kl->ipers)->uz_keg->uk_rsize; 204227ca37acSRyan Libby 204327ca37acSRyan Libby kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total); 204427ca37acSRyan Libby } 204527ca37acSRyan Libby 20469b78b1f4SJeff Roberson /* 20474a8b575cSRyan Libby * Determine the format of a uma keg. This determines where the slab header 20484a8b575cSRyan Libby * will be placed (inline or offpage) and calculates ipers, rsize, and ppera. 20498355f576SJeff Roberson * 20508355f576SJeff Roberson * Arguments 2051e20a199fSJeff Roberson * keg The zone we should initialize 20528355f576SJeff Roberson * 20538355f576SJeff Roberson * Returns 20548355f576SJeff Roberson * Nothing 20558355f576SJeff Roberson */ 20568355f576SJeff Roberson static void 20574a8b575cSRyan Libby keg_layout(uma_keg_t keg) 20588355f576SJeff Roberson { 205927ca37acSRyan Libby struct keg_layout_result kl = {}, kl_tmp; 206027ca37acSRyan Libby u_int fmts[2]; 20614a8b575cSRyan Libby u_int alignsize; 206227ca37acSRyan Libby u_int nfmt; 20634a8b575cSRyan Libby u_int pages; 2064244f4554SBosko Milekic u_int rsize; 2065a55ebb7cSAndriy Gapon u_int slabsize; 206627ca37acSRyan Libby u_int i, j; 20678355f576SJeff Roberson 20684a8b575cSRyan Libby KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 20694a8b575cSRyan Libby (keg->uk_size <= UMA_PCPU_ALLOC_SIZE && 20704a8b575cSRyan Libby (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0), 20714a8b575cSRyan Libby ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b", 20724a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_size, keg->uk_flags, 20734a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 2074bae55c4aSRyan Libby KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 || 20754a8b575cSRyan Libby (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0, 20764a8b575cSRyan Libby ("%s: incompatible flags 0x%b", __func__, keg->uk_flags, 20774a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 2078e28a647dSGleb Smirnoff 20794a8b575cSRyan Libby alignsize = keg->uk_align + 1; 2080ad97af7eSGleb Smirnoff 2081ef72505eSJeff Roberson /* 2082ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 2083ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 2084ef72505eSJeff Roberson * allocation bits for we round it up. 2085ef72505eSJeff Roberson */ 20869b8db4d0SRyan Libby rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT); 20874a8b575cSRyan Libby rsize = roundup2(rsize, alignsize); 2088ad97af7eSGleb Smirnoff 208927ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) { 20909b78b1f4SJeff Roberson /* 20914a8b575cSRyan Libby * We want one item to start on every align boundary in a page. 20924a8b575cSRyan Libby * To do this we will span pages. We will also extend the item 20934a8b575cSRyan Libby * by the size of align if it is an even multiple of align. 20944a8b575cSRyan Libby * Otherwise, it would fall on the same boundary every time. 20959b78b1f4SJeff Roberson */ 20964a8b575cSRyan Libby if ((rsize & alignsize) == 0) 20974a8b575cSRyan Libby rsize += alignsize; 20984a8b575cSRyan Libby slabsize = rsize * (PAGE_SIZE / alignsize); 20994a8b575cSRyan Libby slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE); 21004a8b575cSRyan Libby slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE); 210127ca37acSRyan Libby slabsize = round_page(slabsize); 21024a8b575cSRyan Libby } else { 21034a8b575cSRyan Libby /* 210427ca37acSRyan Libby * Start with a slab size of as many pages as it takes to 210527ca37acSRyan Libby * represent a single item. We will try to fit as many 210627ca37acSRyan Libby * additional items into the slab as possible. 21074a8b575cSRyan Libby */ 210827ca37acSRyan Libby slabsize = round_page(keg->uk_size); 21091ca6ed45SGleb Smirnoff } 2110ad97af7eSGleb Smirnoff 211127ca37acSRyan Libby /* Build a list of all of the available formats for this keg. */ 211227ca37acSRyan Libby nfmt = 0; 211327ca37acSRyan Libby 21144a8b575cSRyan Libby /* Evaluate an inline slab layout. */ 21154a8b575cSRyan Libby if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0) 211627ca37acSRyan Libby fmts[nfmt++] = 0; 21174a8b575cSRyan Libby 21184a8b575cSRyan Libby /* TODO: vm_page-embedded slab. */ 2119244f4554SBosko Milekic 212020e8e865SBosko Milekic /* 2121244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 212220e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 2123bae55c4aSRyan Libby * may end up going to the VM for slabs which we do not want 2124bae55c4aSRyan Libby * to do if we're UMA_ZONE_VM, which clearly forbids it. 2125bae55c4aSRyan Libby * In those cases, evaluate a pseudo-format called INTERNAL 2126bae55c4aSRyan Libby * which has an inline slab header and one extra page to 2127bae55c4aSRyan Libby * guarantee that it fits. 212827ca37acSRyan Libby * 212927ca37acSRyan Libby * Otherwise, see if using an OFFPAGE slab will improve our 213027ca37acSRyan Libby * efficiency. 213120e8e865SBosko Milekic */ 2132bae55c4aSRyan Libby if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0) 213327ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_INTERNAL; 213427ca37acSRyan Libby else 213527ca37acSRyan Libby fmts[nfmt++] = UMA_ZFLAG_OFFPAGE; 2136244f4554SBosko Milekic 2137ef72505eSJeff Roberson /* 213827ca37acSRyan Libby * Choose a slab size and format which satisfy the minimum efficiency. 213927ca37acSRyan Libby * Prefer the smallest slab size that meets the constraints. 2140ef72505eSJeff Roberson * 214127ca37acSRyan Libby * Start with a minimum slab size, to accommodate CACHESPREAD. Then, 214227ca37acSRyan Libby * for small items (up to PAGE_SIZE), the iteration increment is one 214327ca37acSRyan Libby * page; and for large items, the increment is one item. 2144ef72505eSJeff Roberson */ 214527ca37acSRyan Libby i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize); 214627ca37acSRyan Libby KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u", 214727ca37acSRyan Libby keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize, 214827ca37acSRyan Libby rsize, i)); 214927ca37acSRyan Libby for ( ; ; i++) { 215027ca37acSRyan Libby slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) : 215127ca37acSRyan Libby round_page(rsize * (i - 1) + keg->uk_size); 215227ca37acSRyan Libby 215327ca37acSRyan Libby for (j = 0; j < nfmt; j++) { 215427ca37acSRyan Libby /* Only if we have no viable format yet. */ 215527ca37acSRyan Libby if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 && 215627ca37acSRyan Libby kl.ipers > 0) 215727ca37acSRyan Libby continue; 215827ca37acSRyan Libby 215927ca37acSRyan Libby keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp); 216027ca37acSRyan Libby if (kl_tmp.eff <= kl.eff) 216127ca37acSRyan Libby continue; 216227ca37acSRyan Libby 216327ca37acSRyan Libby kl = kl_tmp; 216427ca37acSRyan Libby 216527ca37acSRyan Libby CTR6(KTR_UMA, "keg %s layout: format %#x " 216627ca37acSRyan Libby "(ipers %u * rsize %u) / slabsize %#x = %u%% eff", 216727ca37acSRyan Libby keg->uk_name, kl.format, kl.ipers, rsize, 216827ca37acSRyan Libby kl.slabsize, UMA_FIXPT_PCT(kl.eff)); 216927ca37acSRyan Libby 217027ca37acSRyan Libby /* Stop when we reach the minimum efficiency. */ 217127ca37acSRyan Libby if (kl.eff >= UMA_MIN_EFF) 217227ca37acSRyan Libby break; 21738355f576SJeff Roberson } 2174ad97af7eSGleb Smirnoff 217533e5a1eaSRyan Libby if (kl.eff >= UMA_MIN_EFF || !multipage_slabs || 217627ca37acSRyan Libby slabsize >= SLAB_MAX_SETSIZE * rsize || 217727ca37acSRyan Libby (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0) 217827ca37acSRyan Libby break; 217927ca37acSRyan Libby } 218027ca37acSRyan Libby 218127ca37acSRyan Libby pages = atop(kl.slabsize); 218227ca37acSRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 218327ca37acSRyan Libby pages *= mp_maxid + 1; 218427ca37acSRyan Libby 218527ca37acSRyan Libby keg->uk_rsize = rsize; 218627ca37acSRyan Libby keg->uk_ipers = kl.ipers; 218727ca37acSRyan Libby keg->uk_ppera = pages; 218827ca37acSRyan Libby keg->uk_flags |= kl.format; 218927ca37acSRyan Libby 21904a8b575cSRyan Libby /* 21914a8b575cSRyan Libby * How do we find the slab header if it is offpage or if not all item 21924a8b575cSRyan Libby * start addresses are in the same page? We could solve the latter 21934a8b575cSRyan Libby * case with vaddr alignment, but we don't. 21944a8b575cSRyan Libby */ 219527ca37acSRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 || 219627ca37acSRyan Libby (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) { 219754c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0) 219827ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_HASH; 219954c5ae80SRyan Libby else 220027ca37acSRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 220154c5ae80SRyan Libby } 220227ca37acSRyan Libby 2203e63a1c2fSRyan Libby CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u", 220427ca37acSRyan Libby __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers, 220527ca37acSRyan Libby pages); 22064a8b575cSRyan Libby KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 22074a8b575cSRyan Libby ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__, 220827ca37acSRyan Libby keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize, 220927ca37acSRyan Libby keg->uk_ipers, pages)); 2210e20a199fSJeff Roberson } 2211e20a199fSJeff Roberson 22128355f576SJeff Roberson /* 2213099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 2214099a0e58SBosko Milekic * the keg onto the global keg list. 22158355f576SJeff Roberson * 22168355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 2217099a0e58SBosko Milekic * udata Actually uma_kctor_args 2218099a0e58SBosko Milekic */ 2219b23f72e9SBrian Feldman static int 2220b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 2221099a0e58SBosko Milekic { 2222099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 2223099a0e58SBosko Milekic uma_keg_t keg = mem; 2224099a0e58SBosko Milekic uma_zone_t zone; 22258b987a77SJeff Roberson int i; 2226099a0e58SBosko Milekic 2227099a0e58SBosko Milekic bzero(keg, size); 2228099a0e58SBosko Milekic keg->uk_size = arg->size; 2229099a0e58SBosko Milekic keg->uk_init = arg->uminit; 2230099a0e58SBosko Milekic keg->uk_fini = arg->fini; 2231099a0e58SBosko Milekic keg->uk_align = arg->align; 22326fd34d6fSJeff Roberson keg->uk_reserve = 0; 2233099a0e58SBosko Milekic keg->uk_flags = arg->flags; 2234099a0e58SBosko Milekic 2235099a0e58SBosko Milekic /* 2236194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 2237dfe13344SJeff Roberson * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which 2238dfe13344SJeff Roberson * case the iterator is never run. 2239194a979eSMark Johnston */ 2240194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 2241194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 2242194a979eSMark Johnston 2243194a979eSMark Johnston /* 2244c8b0a88bSJeff Roberson * The primary zone is passed to us at keg-creation time. 2245099a0e58SBosko Milekic */ 2246099a0e58SBosko Milekic zone = arg->zone; 2247e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 2248099a0e58SBosko Milekic 2249099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 2250099a0e58SBosko Milekic keg->uk_init = zero_init; 2251099a0e58SBosko Milekic 2252cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 225354c5ae80SRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 2254e20a199fSJeff Roberson 225554c5ae80SRyan Libby #ifndef SMP 2256ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 2257ad97af7eSGleb Smirnoff #endif 2258ad97af7eSGleb Smirnoff 22594a8b575cSRyan Libby keg_layout(keg); 2260099a0e58SBosko Milekic 22618b987a77SJeff Roberson /* 2262c6fd3e23SJeff Roberson * Use a first-touch NUMA policy for kegs that pmap_extract() will 2263c6fd3e23SJeff Roberson * work on. Use round-robin for everything else. 2264dfe13344SJeff Roberson * 2265dfe13344SJeff Roberson * Zones may override the default by specifying either. 22668b987a77SJeff Roberson */ 2267dfe13344SJeff Roberson #ifdef NUMA 2268dfe13344SJeff Roberson if ((keg->uk_flags & 2269c6fd3e23SJeff Roberson (UMA_ZONE_ROUNDROBIN | UMA_ZFLAG_CACHE | UMA_ZONE_NOTPAGE)) == 0) 2270dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_FIRSTTOUCH; 2271dfe13344SJeff Roberson else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2272dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_ROUNDROBIN; 22738b987a77SJeff Roberson #endif 22748b987a77SJeff Roberson 2275099a0e58SBosko Milekic /* 2276099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 2277099a0e58SBosko Milekic * startup cache until the vm is ready. 2278099a0e58SBosko Milekic */ 227977e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 2280a81c400eSJeff Roberson if (keg->uk_ppera == 1) 228177e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 2282a81c400eSJeff Roberson else 22838cd02d00SAlan Cox #endif 2284a81c400eSJeff Roberson if (booted < BOOT_KVA) 2285a81c400eSJeff Roberson keg->uk_allocf = startup_alloc; 2286ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 2287ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 2288ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1) 2289ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 229077e19437SGleb Smirnoff else 229177e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 229277e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 229377e19437SGleb Smirnoff if (keg->uk_ppera == 1) 229477e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 229577e19437SGleb Smirnoff else 229677e19437SGleb Smirnoff #endif 2297ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 2298ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 2299ab3059a8SMatt Macy else 230077e19437SGleb Smirnoff keg->uk_freef = page_free; 2301099a0e58SBosko Milekic 2302099a0e58SBosko Milekic /* 23038b987a77SJeff Roberson * Initialize keg's locks. 2304099a0e58SBosko Milekic */ 23058b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 23068b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 2307099a0e58SBosko Milekic 2308099a0e58SBosko Milekic /* 2309099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 23109b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 23119b78b1f4SJeff Roberson * definition. 2312099a0e58SBosko Milekic */ 231354c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) { 23149b78b1f4SJeff Roberson size_t shsize; 23159b78b1f4SJeff Roberson 23169b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 23179b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 2318244f4554SBosko Milekic /* 2319244f4554SBosko Milekic * The only way the following is possible is if with our 2320244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 2321244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 2322244f4554SBosko Milekic * mathematically possible for all cases, so we make 2323244f4554SBosko Milekic * sure here anyway. 2324244f4554SBosko Milekic */ 23259b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 23263d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 23273d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 2328099a0e58SBosko Milekic } 2329099a0e58SBosko Milekic 233054c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 23313b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 2332099a0e58SBosko Milekic 2333e63a1c2fSRyan Libby CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone); 2334099a0e58SBosko Milekic 2335099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 2336099a0e58SBosko Milekic 2337111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2338099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2339111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2340b23f72e9SBrian Feldman return (0); 2341099a0e58SBosko Milekic } 2342099a0e58SBosko Milekic 23432efcc8cbSGleb Smirnoff static void 2344a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused) 2345a81c400eSJeff Roberson { 2346a81c400eSJeff Roberson uma_keg_t keg; 2347a81c400eSJeff Roberson 2348a81c400eSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 2349a81c400eSJeff Roberson return; 2350a81c400eSJeff Roberson KEG_GET(zone, keg); 2351ec0d8280SRyan Libby 2352ec0d8280SRyan Libby if (keg->uk_allocf == startup_alloc) { 2353ec0d8280SRyan Libby /* Switch to the real allocator. */ 2354f96d4157SJeff Roberson if (keg->uk_flags & UMA_ZONE_PCPU) 2355f96d4157SJeff Roberson keg->uk_allocf = pcpu_page_alloc; 2356ec0d8280SRyan Libby else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && 2357ec0d8280SRyan Libby keg->uk_ppera > 1) 2358ec0d8280SRyan Libby keg->uk_allocf = contig_alloc; 2359ec0d8280SRyan Libby else 2360a81c400eSJeff Roberson keg->uk_allocf = page_alloc; 2361a81c400eSJeff Roberson } 2362ec0d8280SRyan Libby } 2363a81c400eSJeff Roberson 2364a81c400eSJeff Roberson static void 236520a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 23662efcc8cbSGleb Smirnoff { 23672efcc8cbSGleb Smirnoff 23682efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 23692efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 23702efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 2371c6fd3e23SJeff Roberson zone->uz_xdomain = counter_u64_alloc(M_WAITOK); 23722efcc8cbSGleb Smirnoff } 23732efcc8cbSGleb Smirnoff 237420a4e154SJeff Roberson static void 237520a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 237620a4e154SJeff Roberson { 237720a4e154SJeff Roberson uma_zone_domain_t zdom; 23788b987a77SJeff Roberson uma_domain_t dom; 237920a4e154SJeff Roberson uma_keg_t keg; 238020a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 23813b490537SJeff Roberson int domains, i, cnt; 238220a4e154SJeff Roberson static const char *nokeg = "cache zone"; 238320a4e154SJeff Roberson char *c; 238420a4e154SJeff Roberson 238520a4e154SJeff Roberson /* 238620a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 238720a4e154SJeff Roberson * any special characters and handling dups by appending 238820a4e154SJeff Roberson * an index. 238920a4e154SJeff Roberson */ 239020a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 23913b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 23923b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 23933b490537SJeff Roberson cnt /= 10; 23943b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 23953b490537SJeff Roberson M_UMA, M_WAITOK); 239620a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 239720a4e154SJeff Roberson zone->uz_namecnt); 239820a4e154SJeff Roberson } else 239920a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 240020a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 240120a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 240220a4e154SJeff Roberson *c = '_'; 240320a4e154SJeff Roberson 240420a4e154SJeff Roberson /* 240520a4e154SJeff Roberson * Basic parameters at the root. 240620a4e154SJeff Roberson */ 240720a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 24087029da5cSPawel Biernacki OID_AUTO, zone->uz_ctlname, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 240920a4e154SJeff Roberson oid = zone->uz_oid; 241020a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 241120a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 24126d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 24136d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 24146d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 241520a4e154SJeff Roberson "Allocator configuration flags"); 241620a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 241720a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 241820a4e154SJeff Roberson "Desired per-cpu cache size"); 241920a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 242020a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 242120a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 242220a4e154SJeff Roberson 242320a4e154SJeff Roberson /* 242420a4e154SJeff Roberson * keg if present. 242520a4e154SJeff Roberson */ 242654c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 24278b987a77SJeff Roberson domains = vm_ndomains; 24288b987a77SJeff Roberson else 24298b987a77SJeff Roberson domains = 1; 243020a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 24317029da5cSPawel Biernacki "keg", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 243220a4e154SJeff Roberson keg = zone->uz_keg; 24333b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 243420a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 243520a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 243620a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 243720a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 243820a4e154SJeff Roberson "Real object size with alignment"); 243920a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 244020a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 244120a4e154SJeff Roberson "pages per-slab allocation"); 244220a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 244320a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 244420a4e154SJeff Roberson "items available per-slab"); 244520a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 244620a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 244720a4e154SJeff Roberson "item alignment mask"); 2448f09cbea3SMark Johnston SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2449f09cbea3SMark Johnston "reserve", CTLFLAG_RD, &keg->uk_reserve, 0, 2450f09cbea3SMark Johnston "number of reserved items"); 2451f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2452f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2453f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2454f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 24558b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 24567029da5cSPawel Biernacki OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 24578b987a77SJeff Roberson for (i = 0; i < domains; i++) { 24588b987a77SJeff Roberson dom = &keg->uk_domain[i]; 24598b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 24607029da5cSPawel Biernacki OID_AUTO, VM_DOMAIN(i)->vmd_name, 24617029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 24628b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 24638b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 24648b987a77SJeff Roberson "Total pages currently allocated from VM"); 24658b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 24664ab3aee8SMark Johnston "free_items", CTLFLAG_RD, &dom->ud_free_items, 0, 24678b987a77SJeff Roberson "items free in the slab layer"); 24688b987a77SJeff Roberson } 246920a4e154SJeff Roberson } else 247020a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 247120a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 247220a4e154SJeff Roberson 247320a4e154SJeff Roberson /* 247420a4e154SJeff Roberson * Information about zone limits. 247520a4e154SJeff Roberson */ 247620a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 24777029da5cSPawel Biernacki "limit", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 24784bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 24794bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 24804bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 24814bd61e19SJeff Roberson "current number of allocated items if limit is set"); 248220a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 248320a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 248420a4e154SJeff Roberson "Maximum number of cached items"); 248520a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 248620a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 248720a4e154SJeff Roberson "Number of threads sleeping at limit"); 248820a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 248920a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 249020a4e154SJeff Roberson "Total zone limit sleeps"); 24914bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2492c6fd3e23SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bucket_max, 0, 2493c6fd3e23SJeff Roberson "Maximum number of items in each domain's bucket cache"); 249420a4e154SJeff Roberson 249520a4e154SJeff Roberson /* 24968b987a77SJeff Roberson * Per-domain zone information. 249720a4e154SJeff Roberson */ 249820a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 24997029da5cSPawel Biernacki OID_AUTO, "domain", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 250020a4e154SJeff Roberson for (i = 0; i < domains; i++) { 2501c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 250220a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 25037029da5cSPawel Biernacki OID_AUTO, VM_DOMAIN(i)->vmd_name, 25047029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 250520a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 250620a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 250720a4e154SJeff Roberson "number of items in this domain"); 250820a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 250920a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 251020a4e154SJeff Roberson "maximum item count in this period"); 251120a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 251220a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 251320a4e154SJeff Roberson "minimum item count in this period"); 251420a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 251520a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 251620a4e154SJeff Roberson "Working set size"); 251720a4e154SJeff Roberson } 251820a4e154SJeff Roberson 251920a4e154SJeff Roberson /* 252020a4e154SJeff Roberson * General statistics. 252120a4e154SJeff Roberson */ 252220a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 25237029da5cSPawel Biernacki "stats", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 252420a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 252520a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 252620a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 252720a4e154SJeff Roberson "Current number of allocated items"); 252820a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 252920a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 253020a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 253120a4e154SJeff Roberson "Total allocation calls"); 253220a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 253320a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 253420a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 253520a4e154SJeff Roberson "Total free calls"); 253620a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 253720a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 253820a4e154SJeff Roberson "Number of allocation failures"); 2539c6fd3e23SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2540c6fd3e23SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 254120a4e154SJeff Roberson "Free calls from the wrong domain"); 254220a4e154SJeff Roberson } 254320a4e154SJeff Roberson 254420a4e154SJeff Roberson struct uma_zone_count { 254520a4e154SJeff Roberson const char *name; 254620a4e154SJeff Roberson int count; 254720a4e154SJeff Roberson }; 254820a4e154SJeff Roberson 254920a4e154SJeff Roberson static void 255020a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 255120a4e154SJeff Roberson { 255220a4e154SJeff Roberson struct uma_zone_count *cnt; 255320a4e154SJeff Roberson 255420a4e154SJeff Roberson cnt = arg; 25553b490537SJeff Roberson /* 25563b490537SJeff Roberson * Some zones are rapidly created with identical names and 25573b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 25583b490537SJeff Roberson * Use one greater than the maximum observed for this name. 25593b490537SJeff Roberson */ 256020a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 25613b490537SJeff Roberson cnt->count = MAX(cnt->count, 25623b490537SJeff Roberson zone->uz_namecnt + 1); 256320a4e154SJeff Roberson } 256420a4e154SJeff Roberson 2565cc7ce83aSJeff Roberson static void 2566cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2567cc7ce83aSJeff Roberson { 2568cc7ce83aSJeff Roberson int i; 2569cc7ce83aSJeff Roberson 2570cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2571cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2572cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2573cc7ce83aSJeff Roberson } 2574cc7ce83aSJeff Roberson } 2575cc7ce83aSJeff Roberson 2576099a0e58SBosko Milekic /* 2577099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2578099a0e58SBosko Milekic * 2579099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2580099a0e58SBosko Milekic * udata Actually uma_zctor_args 25818355f576SJeff Roberson */ 2582b23f72e9SBrian Feldman static int 2583b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 25848355f576SJeff Roberson { 258520a4e154SJeff Roberson struct uma_zone_count cnt; 25868355f576SJeff Roberson struct uma_zctor_args *arg = udata; 2587c6fd3e23SJeff Roberson uma_zone_domain_t zdom; 25888355f576SJeff Roberson uma_zone_t zone = mem; 2589099a0e58SBosko Milekic uma_zone_t z; 2590099a0e58SBosko Milekic uma_keg_t keg; 259108cfa56eSMark Johnston int i; 25928355f576SJeff Roberson 25938355f576SJeff Roberson bzero(zone, size); 25948355f576SJeff Roberson zone->uz_name = arg->name; 25958355f576SJeff Roberson zone->uz_ctor = arg->ctor; 25968355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2597099a0e58SBosko Milekic zone->uz_init = NULL; 2598099a0e58SBosko Milekic zone->uz_fini = NULL; 2599bf965959SSean Bruno zone->uz_sleeps = 0; 260020a4e154SJeff Roberson zone->uz_bucket_size = 0; 260120a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 260220a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2603d4665eaaSJeff Roberson zone->uz_flags = (arg->flags & UMA_ZONE_SMR); 26042f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2605ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 2606c6fd3e23SJeff Roberson zone->uz_bucket_max = ULONG_MAX; 26072f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2608af526374SJeff Roberson 260920a4e154SJeff Roberson /* Count the number of duplicate names. */ 261020a4e154SJeff Roberson cnt.name = arg->name; 261120a4e154SJeff Roberson cnt.count = 0; 261220a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 261320a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 261491d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 26152efcc8cbSGleb Smirnoff 2616c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 2617c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, i); 2618c6fd3e23SJeff Roberson ZDOM_LOCK_INIT(zone, zdom, (arg->flags & UMA_ZONE_MTXCLASS)); 2619c6fd3e23SJeff Roberson STAILQ_INIT(&zdom->uzd_buckets); 2620c6fd3e23SJeff Roberson } 262108cfa56eSMark Johnston 2622ca293436SRyan Libby #ifdef INVARIANTS 2623ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2624cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 2625ca293436SRyan Libby #endif 2626ca293436SRyan Libby 26270095a784SJeff Roberson /* 26280095a784SJeff Roberson * This is a pure cache zone, no kegs. 26290095a784SJeff Roberson */ 26300095a784SJeff Roberson if (arg->import) { 2631727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2632727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 26336fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2634af526374SJeff Roberson zone->uz_size = arg->size; 26350095a784SJeff Roberson zone->uz_import = arg->import; 26360095a784SJeff Roberson zone->uz_release = arg->release; 26370095a784SJeff Roberson zone->uz_arg = arg->arg; 2638c6fd3e23SJeff Roberson #ifdef NUMA 2639c6fd3e23SJeff Roberson /* 2640c6fd3e23SJeff Roberson * Cache zones are round-robin unless a policy is 2641c6fd3e23SJeff Roberson * specified because they may have incompatible 2642c6fd3e23SJeff Roberson * constraints. 2643c6fd3e23SJeff Roberson */ 2644c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0) 2645c6fd3e23SJeff Roberson zone->uz_flags |= UMA_ZONE_ROUNDROBIN; 2646c6fd3e23SJeff Roberson #endif 2647111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 264803175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2649111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2650af526374SJeff Roberson goto out; 26510095a784SJeff Roberson } 26520095a784SJeff Roberson 26530095a784SJeff Roberson /* 26540095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 26550095a784SJeff Roberson */ 2656b75c4efcSAndrew Turner zone->uz_import = zone_import; 2657b75c4efcSAndrew Turner zone->uz_release = zone_release; 26580095a784SJeff Roberson zone->uz_arg = zone; 2659bb15d1c7SGleb Smirnoff keg = arg->keg; 26600095a784SJeff Roberson 2661099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 266220a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 266320a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2664099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 26658355f576SJeff Roberson zone->uz_init = arg->uminit; 2666e221e841SJeff Roberson zone->uz_fini = arg->fini; 2667e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2668111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2669099a0e58SBosko Milekic ZONE_LOCK(zone); 2670099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2671099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2672099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2673099a0e58SBosko Milekic break; 2674099a0e58SBosko Milekic } 2675099a0e58SBosko Milekic } 2676099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2677111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2678e20a199fSJeff Roberson } else if (keg == NULL) { 2679e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2680e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2681b23f72e9SBrian Feldman return (ENOMEM); 2682099a0e58SBosko Milekic } else { 2683099a0e58SBosko Milekic struct uma_kctor_args karg; 2684b23f72e9SBrian Feldman int error; 2685099a0e58SBosko Milekic 2686099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2687099a0e58SBosko Milekic karg.size = arg->size; 2688099a0e58SBosko Milekic karg.uminit = arg->uminit; 2689099a0e58SBosko Milekic karg.fini = arg->fini; 2690099a0e58SBosko Milekic karg.align = arg->align; 2691d4665eaaSJeff Roberson karg.flags = (arg->flags & ~UMA_ZONE_SMR); 2692099a0e58SBosko Milekic karg.zone = zone; 2693b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2694b23f72e9SBrian Feldman flags); 2695b23f72e9SBrian Feldman if (error) 2696b23f72e9SBrian Feldman return (error); 2697099a0e58SBosko Milekic } 26980095a784SJeff Roberson 269920a4e154SJeff Roberson /* Inherit properties from the keg. */ 2700bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2701e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2702e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2703e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 27048355f576SJeff Roberson 270520a4e154SJeff Roberson out: 2706dc2b3205SMark Johnston if (booted >= BOOT_PCPU) { 270720a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 2708dc2b3205SMark Johnston if (booted >= BOOT_RUNNING) 270920a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 271020a4e154SJeff Roberson } else { 271120a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 271220a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 271320a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2714099a0e58SBosko Milekic } 27158355f576SJeff Roberson 2716d4665eaaSJeff Roberson /* Caller requests a private SMR context. */ 2717d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 2718226dd6dbSJeff Roberson zone->uz_smr = smr_create(zone->uz_name, 0, 0); 2719d4665eaaSJeff Roberson 27207e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 27217e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 27227e28037aSMark Johnston ("Invalid zone flag combination")); 272320a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 272420a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 272520a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 272620a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 272720a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) 272820a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN; 272920a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 273020a4e154SJeff Roberson zone->uz_bucket_size = 0; 27317e28037aSMark Johnston else 273220a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 273320a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2734cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2735cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2736cc7ce83aSJeff Roberson zone_update_caches(zone); 2737fc03d22bSJeff Roberson 2738b23f72e9SBrian Feldman return (0); 27398355f576SJeff Roberson } 27408355f576SJeff Roberson 27418355f576SJeff Roberson /* 2742099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2743099a0e58SBosko Milekic * table and removes the keg from the global list. 27449c2cd7e5SJeff Roberson * 27459c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 27469c2cd7e5SJeff Roberson * udata unused 27479c2cd7e5SJeff Roberson */ 2748099a0e58SBosko Milekic static void 2749099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2750099a0e58SBosko Milekic { 2751099a0e58SBosko Milekic uma_keg_t keg; 27528b987a77SJeff Roberson uint32_t free, pages; 27538b987a77SJeff Roberson int i; 27549c2cd7e5SJeff Roberson 2755099a0e58SBosko Milekic keg = (uma_keg_t)arg; 27568b987a77SJeff Roberson free = pages = 0; 27578b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 27584ab3aee8SMark Johnston free += keg->uk_domain[i].ud_free_items; 27598b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 27608b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2761099a0e58SBosko Milekic } 27627e240677SRyan Libby if (pages != 0) 27638b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 27648b987a77SJeff Roberson " Lost %u pages of memory.\n", 27658b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 27667e240677SRyan Libby pages / keg->uk_ppera * keg->uk_ipers - free, pages); 2767099a0e58SBosko Milekic 2768099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2769099a0e58SBosko Milekic } 2770099a0e58SBosko Milekic 2771099a0e58SBosko Milekic /* 2772099a0e58SBosko Milekic * Zone header dtor. 2773099a0e58SBosko Milekic * 2774099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2775099a0e58SBosko Milekic * udata unused 2776099a0e58SBosko Milekic */ 27779c2cd7e5SJeff Roberson static void 27789c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 27799c2cd7e5SJeff Roberson { 27809c2cd7e5SJeff Roberson uma_zone_t zone; 2781099a0e58SBosko Milekic uma_keg_t keg; 2782c6fd3e23SJeff Roberson int i; 27839c2cd7e5SJeff Roberson 27849c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 27859643769aSJeff Roberson 278620a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 278720a4e154SJeff Roberson 2788e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 27899643769aSJeff Roberson cache_drain(zone); 2790099a0e58SBosko Milekic 2791111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2792099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2793111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 27947b516613SJonathan T. Looney if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 27957b516613SJonathan T. Looney keg = zone->uz_keg; 27967b516613SJonathan T. Looney keg->uk_reserve = 0; 27977b516613SJonathan T. Looney } 279808cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2799c6fd3e23SJeff Roberson 2800e20a199fSJeff Roberson /* 2801323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2802e20a199fSJeff Roberson */ 2803323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2804323ad386STycho Nightingale keg = zone->uz_keg; 2805111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2806099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2807111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 28080095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 28099c2cd7e5SJeff Roberson } 28102efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 28112efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 28122efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 2813c6fd3e23SJeff Roberson counter_u64_free(zone->uz_xdomain); 281420a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2815c6fd3e23SJeff Roberson for (i = 0; i < vm_ndomains; i++) 2816c6fd3e23SJeff Roberson ZDOM_LOCK_FINI(ZDOM_GET(zone, i)); 281791d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 2818099a0e58SBosko Milekic } 2819099a0e58SBosko Milekic 2820a81c400eSJeff Roberson static void 2821a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg) 2822a81c400eSJeff Roberson { 2823a81c400eSJeff Roberson uma_keg_t keg; 2824a81c400eSJeff Roberson uma_zone_t zone; 2825a81c400eSJeff Roberson 2826a81c400eSJeff Roberson LIST_FOREACH(keg, &uma_kegs, uk_link) { 2827a81c400eSJeff Roberson LIST_FOREACH(zone, &keg->uk_zones, uz_link) 2828a81c400eSJeff Roberson zfunc(zone, arg); 2829a81c400eSJeff Roberson } 2830a81c400eSJeff Roberson LIST_FOREACH(zone, &uma_cachezones, uz_link) 2831a81c400eSJeff Roberson zfunc(zone, arg); 2832a81c400eSJeff Roberson } 2833a81c400eSJeff Roberson 28349c2cd7e5SJeff Roberson /* 28358355f576SJeff Roberson * Traverses every zone in the system and calls a callback 28368355f576SJeff Roberson * 28378355f576SJeff Roberson * Arguments: 28388355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 28398355f576SJeff Roberson * as an argument. 28408355f576SJeff Roberson * 28418355f576SJeff Roberson * Returns: 28428355f576SJeff Roberson * Nothing 28438355f576SJeff Roberson */ 28448355f576SJeff Roberson static void 284520a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 28468355f576SJeff Roberson { 28478355f576SJeff Roberson 2848111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2849a81c400eSJeff Roberson zone_foreach_unlocked(zfunc, arg); 2850111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 28518355f576SJeff Roberson } 28528355f576SJeff Roberson 2853f4bef67cSGleb Smirnoff /* 2854a81c400eSJeff Roberson * Initialize the kernel memory allocator. This is done after pages can be 2855a81c400eSJeff Roberson * allocated but before general KVA is available. 2856f4bef67cSGleb Smirnoff */ 2857a81c400eSJeff Roberson void 2858a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail) 2859f4bef67cSGleb Smirnoff { 2860a81c400eSJeff Roberson struct uma_zctor_args args; 2861a81c400eSJeff Roberson size_t ksize, zsize, size; 2862c8b0a88bSJeff Roberson uma_keg_t primarykeg; 2863a81c400eSJeff Roberson uintptr_t m; 286481302f1dSMark Johnston int domain; 2865a81c400eSJeff Roberson uint8_t pflag; 2866a81c400eSJeff Roberson 2867a81c400eSJeff Roberson bootstart = bootmem = virtual_avail; 2868a81c400eSJeff Roberson 2869a81c400eSJeff Roberson rw_init(&uma_rwlock, "UMA lock"); 2870a81c400eSJeff Roberson sx_init(&uma_reclaim_lock, "umareclaim"); 2871f4bef67cSGleb Smirnoff 2872f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2873f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 287479c9f942SJeff Roberson ksize = roundup(ksize, UMA_SUPER_ALIGN); 2875f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2876f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2877f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 287879c9f942SJeff Roberson zsize = roundup(zsize, UMA_SUPER_ALIGN); 2879f4bef67cSGleb Smirnoff 2880a81c400eSJeff Roberson /* Allocate the zone of zones, zone of kegs, and zone of zones keg. */ 2881a81c400eSJeff Roberson size = (zsize * 2) + ksize; 288281302f1dSMark Johnston for (domain = 0; domain < vm_ndomains; domain++) { 288381302f1dSMark Johnston m = (uintptr_t)startup_alloc(NULL, size, domain, &pflag, 288481302f1dSMark Johnston M_NOWAIT | M_ZERO); 288581302f1dSMark Johnston if (m != 0) 288681302f1dSMark Johnston break; 288781302f1dSMark Johnston } 2888ab3185d1SJeff Roberson zones = (uma_zone_t)m; 288979c9f942SJeff Roberson m += zsize; 2890ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 289179c9f942SJeff Roberson m += zsize; 2892c8b0a88bSJeff Roberson primarykeg = (uma_keg_t)m; 2893ab3185d1SJeff Roberson 2894099a0e58SBosko Milekic /* "manually" create the initial zone */ 28950095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2896099a0e58SBosko Milekic args.name = "UMA Kegs"; 2897ab3185d1SJeff Roberson args.size = ksize; 2898099a0e58SBosko Milekic args.ctor = keg_ctor; 2899099a0e58SBosko Milekic args.dtor = keg_dtor; 29008355f576SJeff Roberson args.uminit = zero_init; 29018355f576SJeff Roberson args.fini = NULL; 2902c8b0a88bSJeff Roberson args.keg = primarykeg; 290379c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2904b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2905ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 29068355f576SJeff Roberson 2907099a0e58SBosko Milekic args.name = "UMA Zones"; 2908f4bef67cSGleb Smirnoff args.size = zsize; 2909099a0e58SBosko Milekic args.ctor = zone_ctor; 2910099a0e58SBosko Milekic args.dtor = zone_dtor; 2911099a0e58SBosko Milekic args.uminit = zero_init; 2912099a0e58SBosko Milekic args.fini = NULL; 2913099a0e58SBosko Milekic args.keg = NULL; 291479c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2915099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2916ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2917099a0e58SBosko Milekic 29189b8db4d0SRyan Libby /* Now make zones for slab headers */ 29199b8db4d0SRyan Libby slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE, 29209b8db4d0SRyan Libby NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 29219b8db4d0SRyan Libby slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE, 29221e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 29238355f576SJeff Roberson 29248355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 29258355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 29261e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 29278355f576SJeff Roberson 2928a81c400eSJeff Roberson bucket_init(); 2929d4665eaaSJeff Roberson smr_init(); 29308355f576SJeff Roberson } 29318355f576SJeff Roberson 2932a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2933a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void); 2934f4bef67cSGleb Smirnoff #endif 2935f4bef67cSGleb Smirnoff 2936a81c400eSJeff Roberson /* 2937a81c400eSJeff Roberson * Advertise the availability of normal kva allocations and switch to 2938a81c400eSJeff Roberson * the default back-end allocator. Marks the KVA we consumed on startup 2939a81c400eSJeff Roberson * as used in the map. 2940a81c400eSJeff Roberson */ 29418355f576SJeff Roberson void 294299571dc3SJeff Roberson uma_startup2(void) 29438355f576SJeff Roberson { 2944f4bef67cSGleb Smirnoff 2945530cc6a2SJeff Roberson if (bootstart != bootmem) { 2946a81c400eSJeff Roberson vm_map_lock(kernel_map); 2947a81c400eSJeff Roberson (void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem, 2948a81c400eSJeff Roberson VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 2949a81c400eSJeff Roberson vm_map_unlock(kernel_map); 2950a81c400eSJeff Roberson } 2951a81c400eSJeff Roberson 2952a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2953a81c400eSJeff Roberson /* Set up radix zone to use noobj_alloc. */ 2954a81c400eSJeff Roberson vm_radix_reserve_kva(); 2955f7d35785SGleb Smirnoff #endif 2956a81c400eSJeff Roberson 2957a81c400eSJeff Roberson booted = BOOT_KVA; 2958a81c400eSJeff Roberson zone_foreach_unlocked(zone_kva_available, NULL); 2959f4bef67cSGleb Smirnoff bucket_enable(); 29608355f576SJeff Roberson } 29618355f576SJeff Roberson 2962a81c400eSJeff Roberson /* 2963dc2b3205SMark Johnston * Allocate counters as early as possible so that boot-time allocations are 2964dc2b3205SMark Johnston * accounted more precisely. 2965dc2b3205SMark Johnston */ 2966dc2b3205SMark Johnston static void 2967dc2b3205SMark Johnston uma_startup_pcpu(void *arg __unused) 2968dc2b3205SMark Johnston { 2969dc2b3205SMark Johnston 2970dc2b3205SMark Johnston zone_foreach_unlocked(zone_alloc_counters, NULL); 2971dc2b3205SMark Johnston booted = BOOT_PCPU; 2972dc2b3205SMark Johnston } 2973dc2b3205SMark Johnston SYSINIT(uma_startup_pcpu, SI_SUB_COUNTER, SI_ORDER_ANY, uma_startup_pcpu, NULL); 2974dc2b3205SMark Johnston 2975dc2b3205SMark Johnston /* 2976a81c400eSJeff Roberson * Finish our initialization steps. 2977a81c400eSJeff Roberson */ 29788355f576SJeff Roberson static void 2979dc2b3205SMark Johnston uma_startup3(void *arg __unused) 29808355f576SJeff Roberson { 29811431a748SGleb Smirnoff 2982c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2983c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2984c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2985c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2986c5deaf04SGleb Smirnoff #endif 2987a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_sysctl, NULL); 2988fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 29899643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2990c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 2991860bb7a0SMark Johnston 2992860bb7a0SMark Johnston EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL, 2993860bb7a0SMark Johnston EVENTHANDLER_PRI_FIRST); 2994860bb7a0SMark Johnston } 2995dc2b3205SMark Johnston SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 2996860bb7a0SMark Johnston 2997860bb7a0SMark Johnston static void 2998860bb7a0SMark Johnston uma_shutdown(void) 2999860bb7a0SMark Johnston { 3000860bb7a0SMark Johnston 3001860bb7a0SMark Johnston booted = BOOT_SHUTDOWN; 30028355f576SJeff Roberson } 30038355f576SJeff Roberson 3004e20a199fSJeff Roberson static uma_keg_t 3005099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 300685dcf349SGleb Smirnoff int align, uint32_t flags) 3007099a0e58SBosko Milekic { 3008099a0e58SBosko Milekic struct uma_kctor_args args; 3009099a0e58SBosko Milekic 3010099a0e58SBosko Milekic args.size = size; 3011099a0e58SBosko Milekic args.uminit = uminit; 3012099a0e58SBosko Milekic args.fini = fini; 30131e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 3014099a0e58SBosko Milekic args.flags = flags; 3015099a0e58SBosko Milekic args.zone = zone; 3016ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 3017099a0e58SBosko Milekic } 3018099a0e58SBosko Milekic 3019f4bef67cSGleb Smirnoff /* Public functions */ 30208355f576SJeff Roberson /* See uma.h */ 30211e319f6dSRobert Watson void 30221e319f6dSRobert Watson uma_set_align(int align) 30231e319f6dSRobert Watson { 30241e319f6dSRobert Watson 30251e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 30261e319f6dSRobert Watson uma_align_cache = align; 30271e319f6dSRobert Watson } 30281e319f6dSRobert Watson 30291e319f6dSRobert Watson /* See uma.h */ 30308355f576SJeff Roberson uma_zone_t 3031bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 303285dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 30338355f576SJeff Roberson 30348355f576SJeff Roberson { 30358355f576SJeff Roberson struct uma_zctor_args args; 303695c4bf75SKonstantin Belousov uma_zone_t res; 30378355f576SJeff Roberson 3038a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 3039a5a35578SJohn Baldwin align, name)); 3040a5a35578SJohn Baldwin 30418355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 30420095a784SJeff Roberson memset(&args, 0, sizeof(args)); 30438355f576SJeff Roberson args.name = name; 30448355f576SJeff Roberson args.size = size; 30458355f576SJeff Roberson args.ctor = ctor; 30468355f576SJeff Roberson args.dtor = dtor; 30478355f576SJeff Roberson args.uminit = uminit; 30488355f576SJeff Roberson args.fini = fini; 3049afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 3050afc6dc36SJohn-Mark Gurney /* 3051ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 3052ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 3053ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 3054ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 3055ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 3056ca293436SRyan Libby * the ctor and dtor do not need to be empty. 3057afc6dc36SJohn-Mark Gurney */ 305854c5ae80SRyan Libby if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH | 305954c5ae80SRyan Libby UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) { 3060afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 3061afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 3062afc6dc36SJohn-Mark Gurney } 3063afc6dc36SJohn-Mark Gurney #endif 30648355f576SJeff Roberson args.align = align; 30658355f576SJeff Roberson args.flags = flags; 3066099a0e58SBosko Milekic args.keg = NULL; 3067099a0e58SBosko Milekic 306808cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 3069ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 307008cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 3071a81c400eSJeff Roberson 307295c4bf75SKonstantin Belousov return (res); 3073099a0e58SBosko Milekic } 3074099a0e58SBosko Milekic 3075099a0e58SBosko Milekic /* See uma.h */ 3076099a0e58SBosko Milekic uma_zone_t 30770464f16eSMark Johnston uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor, 3078c8b0a88bSJeff Roberson uma_init zinit, uma_fini zfini, uma_zone_t primary) 3079099a0e58SBosko Milekic { 3080099a0e58SBosko Milekic struct uma_zctor_args args; 3081e20a199fSJeff Roberson uma_keg_t keg; 308295c4bf75SKonstantin Belousov uma_zone_t res; 3083099a0e58SBosko Milekic 3084c8b0a88bSJeff Roberson keg = primary->uz_keg; 30850095a784SJeff Roberson memset(&args, 0, sizeof(args)); 3086099a0e58SBosko Milekic args.name = name; 3087e20a199fSJeff Roberson args.size = keg->uk_size; 3088099a0e58SBosko Milekic args.ctor = ctor; 3089099a0e58SBosko Milekic args.dtor = dtor; 3090099a0e58SBosko Milekic args.uminit = zinit; 3091099a0e58SBosko Milekic args.fini = zfini; 3092e20a199fSJeff Roberson args.align = keg->uk_align; 3093e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 3094e20a199fSJeff Roberson args.keg = keg; 30958355f576SJeff Roberson 309608cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 3097ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 309808cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 3099a81c400eSJeff Roberson 310095c4bf75SKonstantin Belousov return (res); 31018355f576SJeff Roberson } 31028355f576SJeff Roberson 31030095a784SJeff Roberson /* See uma.h */ 31040095a784SJeff Roberson uma_zone_t 31050464f16eSMark Johnston uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor, 31060464f16eSMark Johnston uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease, 31070464f16eSMark Johnston void *arg, int flags) 31080095a784SJeff Roberson { 31090095a784SJeff Roberson struct uma_zctor_args args; 31100095a784SJeff Roberson 31110095a784SJeff Roberson memset(&args, 0, sizeof(args)); 31120095a784SJeff Roberson args.name = name; 3113af526374SJeff Roberson args.size = size; 31140095a784SJeff Roberson args.ctor = ctor; 31150095a784SJeff Roberson args.dtor = dtor; 31160095a784SJeff Roberson args.uminit = zinit; 31170095a784SJeff Roberson args.fini = zfini; 31180095a784SJeff Roberson args.import = zimport; 31190095a784SJeff Roberson args.release = zrelease; 31200095a784SJeff Roberson args.arg = arg; 31210095a784SJeff Roberson args.align = 0; 3122bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 31230095a784SJeff Roberson 3124ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 31250095a784SJeff Roberson } 31260095a784SJeff Roberson 31278355f576SJeff Roberson /* See uma.h */ 31289c2cd7e5SJeff Roberson void 31299c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 31309c2cd7e5SJeff Roberson { 3131f4ff923bSRobert Watson 3132860bb7a0SMark Johnston /* 3133860bb7a0SMark Johnston * Large slabs are expensive to reclaim, so don't bother doing 3134860bb7a0SMark Johnston * unnecessary work if we're shutting down. 3135860bb7a0SMark Johnston */ 3136860bb7a0SMark Johnston if (booted == BOOT_SHUTDOWN && 3137860bb7a0SMark Johnston zone->uz_fini == NULL && zone->uz_release == zone_release) 3138860bb7a0SMark Johnston return; 313908cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 31400095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 314108cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 31429c2cd7e5SJeff Roberson } 31439c2cd7e5SJeff Roberson 31448d6fbbb8SJeff Roberson void 31458d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 31468d6fbbb8SJeff Roberson { 31478d6fbbb8SJeff Roberson 314870260874SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 314970260874SJeff Roberson uma_zfree_smr(zone, uma_zalloc_smr(zone, M_WAITOK)); 315070260874SJeff Roberson else if ((zone->uz_flags & UMA_ZONE_PCPU) != 0) 315170260874SJeff Roberson uma_zfree_pcpu(zone, uma_zalloc_pcpu(zone, M_WAITOK)); 315270260874SJeff Roberson else 315370260874SJeff Roberson uma_zfree(zone, uma_zalloc(zone, M_WAITOK)); 31548d6fbbb8SJeff Roberson } 31558d6fbbb8SJeff Roberson 31564e180881SMateusz Guzik void * 31574e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 31584e180881SMateusz Guzik { 31593acb6572SMateusz Guzik void *item, *pcpu_item; 3160b4799947SRuslan Bukin #ifdef SMP 31614e180881SMateusz Guzik int i; 31624e180881SMateusz Guzik 31634e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 3164b4799947SRuslan Bukin #endif 31654e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 31663acb6572SMateusz Guzik if (item == NULL) 31673acb6572SMateusz Guzik return (NULL); 31683acb6572SMateusz Guzik pcpu_item = zpcpu_base_to_offset(item); 31693acb6572SMateusz Guzik if (flags & M_ZERO) { 3170b4799947SRuslan Bukin #ifdef SMP 3171013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 31723acb6572SMateusz Guzik bzero(zpcpu_get_cpu(pcpu_item, i), zone->uz_size); 3173b4799947SRuslan Bukin #else 3174b4799947SRuslan Bukin bzero(item, zone->uz_size); 3175b4799947SRuslan Bukin #endif 31764e180881SMateusz Guzik } 31773acb6572SMateusz Guzik return (pcpu_item); 31784e180881SMateusz Guzik } 31794e180881SMateusz Guzik 31804e180881SMateusz Guzik /* 31814e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 31824e180881SMateusz Guzik */ 31834e180881SMateusz Guzik void 31843acb6572SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *pcpu_item, void *udata) 31854e180881SMateusz Guzik { 31863acb6572SMateusz Guzik void *item; 31874e180881SMateusz Guzik 3188c5b7751fSIan Lepore #ifdef SMP 31894e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 3190c5b7751fSIan Lepore #endif 31913acb6572SMateusz Guzik item = zpcpu_offset_to_base(pcpu_item); 31924e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 31934e180881SMateusz Guzik } 31944e180881SMateusz Guzik 3195d4665eaaSJeff Roberson static inline void * 3196d4665eaaSJeff Roberson item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags, 3197d4665eaaSJeff Roberson void *item) 3198beb8beefSJeff Roberson { 3199beb8beefSJeff Roberson #ifdef INVARIANTS 3200ca293436SRyan Libby bool skipdbg; 3201beb8beefSJeff Roberson 3202beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 3203ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 3204ca293436SRyan Libby zone->uz_ctor != trash_ctor) 3205cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 3206beb8beefSJeff Roberson #endif 3207d4665eaaSJeff Roberson /* Check flags before loading ctor pointer. */ 3208d4665eaaSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) && 3209d4665eaaSJeff Roberson __predict_false(zone->uz_ctor != NULL) && 3210cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 3211beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3212beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 3213beb8beefSJeff Roberson return (NULL); 3214beb8beefSJeff Roberson } 3215beb8beefSJeff Roberson #ifdef INVARIANTS 3216beb8beefSJeff Roberson if (!skipdbg) 3217beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 3218beb8beefSJeff Roberson #endif 32196d88d784SJeff Roberson if (__predict_false(flags & M_ZERO)) 32206d88d784SJeff Roberson return (memset(item, 0, size)); 3221beb8beefSJeff Roberson 3222beb8beefSJeff Roberson return (item); 3223beb8beefSJeff Roberson } 3224beb8beefSJeff Roberson 3225ca293436SRyan Libby static inline void 3226cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 3227cc7ce83aSJeff Roberson enum zfreeskip skip) 3228ca293436SRyan Libby { 3229ca293436SRyan Libby #ifdef INVARIANTS 3230ca293436SRyan Libby bool skipdbg; 3231ca293436SRyan Libby 3232ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 3233ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 3234ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 3235ca293436SRyan Libby uma_dbg_free(zone, udata, item); 3236ca293436SRyan Libby else 3237ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 3238ca293436SRyan Libby } 3239ca293436SRyan Libby #endif 3240cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 3241ca293436SRyan Libby if (zone->uz_dtor != NULL) 3242cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 3243ca293436SRyan Libby #ifdef INVARIANTS 3244ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 3245ca293436SRyan Libby zone->uz_dtor != trash_dtor) 3246cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 3247ca293436SRyan Libby #endif 3248ca293436SRyan Libby } 3249ca293436SRyan Libby } 3250ca293436SRyan Libby 32511c58c09fSMateusz Guzik #ifdef NUMA 325281302f1dSMark Johnston static int 325381302f1dSMark Johnston item_domain(void *item) 325481302f1dSMark Johnston { 325581302f1dSMark Johnston int domain; 325681302f1dSMark Johnston 3257431fb8abSMark Johnston domain = vm_phys_domain(vtophys(item)); 325881302f1dSMark Johnston KASSERT(domain >= 0 && domain < vm_ndomains, 325981302f1dSMark Johnston ("%s: unknown domain for item %p", __func__, item)); 326081302f1dSMark Johnston return (domain); 326181302f1dSMark Johnston } 32621c58c09fSMateusz Guzik #endif 326381302f1dSMark Johnston 3264d4665eaaSJeff Roberson #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS) 3265d4665eaaSJeff Roberson #define UMA_ZALLOC_DEBUG 3266d4665eaaSJeff Roberson static int 3267d4665eaaSJeff Roberson uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags) 3268d4665eaaSJeff Roberson { 3269d4665eaaSJeff Roberson int error; 3270d4665eaaSJeff Roberson 3271d4665eaaSJeff Roberson error = 0; 3272d4665eaaSJeff Roberson #ifdef WITNESS 3273d4665eaaSJeff Roberson if (flags & M_WAITOK) { 3274d4665eaaSJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3275d4665eaaSJeff Roberson "uma_zalloc_debug: zone \"%s\"", zone->uz_name); 3276d4665eaaSJeff Roberson } 3277d4665eaaSJeff Roberson #endif 3278d4665eaaSJeff Roberson 3279d4665eaaSJeff Roberson #ifdef INVARIANTS 3280d4665eaaSJeff Roberson KASSERT((flags & M_EXEC) == 0, 3281d4665eaaSJeff Roberson ("uma_zalloc_debug: called with M_EXEC")); 3282d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3283d4665eaaSJeff Roberson ("uma_zalloc_debug: called within spinlock or critical section")); 3284d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0, 3285d4665eaaSJeff Roberson ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO")); 3286d4665eaaSJeff Roberson #endif 3287d4665eaaSJeff Roberson 3288d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 32899e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) { 3290d4665eaaSJeff Roberson void *item; 3291d4665eaaSJeff Roberson item = memguard_alloc(zone->uz_size, flags); 3292d4665eaaSJeff Roberson if (item != NULL) { 3293d4665eaaSJeff Roberson error = EJUSTRETURN; 3294d4665eaaSJeff Roberson if (zone->uz_init != NULL && 3295d4665eaaSJeff Roberson zone->uz_init(item, zone->uz_size, flags) != 0) { 3296d4665eaaSJeff Roberson *itemp = NULL; 3297d4665eaaSJeff Roberson return (error); 3298d4665eaaSJeff Roberson } 3299d4665eaaSJeff Roberson if (zone->uz_ctor != NULL && 3300d4665eaaSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 3301d4665eaaSJeff Roberson flags) != 0) { 3302d4665eaaSJeff Roberson counter_u64_add(zone->uz_fails, 1); 3303d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3304d4665eaaSJeff Roberson *itemp = NULL; 3305d4665eaaSJeff Roberson return (error); 3306d4665eaaSJeff Roberson } 3307d4665eaaSJeff Roberson *itemp = item; 3308d4665eaaSJeff Roberson return (error); 3309d4665eaaSJeff Roberson } 3310d4665eaaSJeff Roberson /* This is unfortunate but should not be fatal. */ 3311d4665eaaSJeff Roberson } 3312d4665eaaSJeff Roberson #endif 3313d4665eaaSJeff Roberson return (error); 3314d4665eaaSJeff Roberson } 3315d4665eaaSJeff Roberson 3316d4665eaaSJeff Roberson static int 3317d4665eaaSJeff Roberson uma_zfree_debug(uma_zone_t zone, void *item, void *udata) 3318d4665eaaSJeff Roberson { 3319d4665eaaSJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3320d4665eaaSJeff Roberson ("uma_zfree_debug: called with spinlock or critical section held")); 3321d4665eaaSJeff Roberson 3322d4665eaaSJeff Roberson #ifdef DEBUG_MEMGUARD 33239e47b341SJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) { 3324d4665eaaSJeff Roberson if (zone->uz_dtor != NULL) 3325d4665eaaSJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 3326d4665eaaSJeff Roberson if (zone->uz_fini != NULL) 3327d4665eaaSJeff Roberson zone->uz_fini(item, zone->uz_size); 3328d4665eaaSJeff Roberson memguard_free(item); 3329d4665eaaSJeff Roberson return (EJUSTRETURN); 3330d4665eaaSJeff Roberson } 3331d4665eaaSJeff Roberson #endif 3332d4665eaaSJeff Roberson return (0); 3333d4665eaaSJeff Roberson } 3334d4665eaaSJeff Roberson #endif 3335d4665eaaSJeff Roberson 33366d88d784SJeff Roberson static inline void * 33376d88d784SJeff Roberson cache_alloc_item(uma_zone_t zone, uma_cache_t cache, uma_cache_bucket_t bucket, 33386d88d784SJeff Roberson void *udata, int flags) 3339d4665eaaSJeff Roberson { 33406d88d784SJeff Roberson void *item; 33416d88d784SJeff Roberson int size, uz_flags; 33426d88d784SJeff Roberson 33436d88d784SJeff Roberson item = cache_bucket_pop(cache, bucket); 33446d88d784SJeff Roberson size = cache_uz_size(cache); 33456d88d784SJeff Roberson uz_flags = cache_uz_flags(cache); 33466d88d784SJeff Roberson critical_exit(); 33476d88d784SJeff Roberson return (item_ctor(zone, uz_flags, size, udata, flags, item)); 33486d88d784SJeff Roberson } 33496d88d784SJeff Roberson 33506d88d784SJeff Roberson static __noinline void * 33516d88d784SJeff Roberson cache_alloc_retry(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 33526d88d784SJeff Roberson { 33536d88d784SJeff Roberson uma_cache_bucket_t bucket; 3354d4665eaaSJeff Roberson int domain; 3355d4665eaaSJeff Roberson 33566d88d784SJeff Roberson while (cache_alloc(zone, cache, udata, flags)) { 33576d88d784SJeff Roberson cache = &zone->uz_cpu[curcpu]; 33586d88d784SJeff Roberson bucket = &cache->uc_allocbucket; 33596d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 33606d88d784SJeff Roberson continue; 33616d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, udata, flags)); 33626d88d784SJeff Roberson } 33636d88d784SJeff Roberson critical_exit(); 33646d88d784SJeff Roberson 3365d4665eaaSJeff Roberson /* 3366d4665eaaSJeff Roberson * We can not get a bucket so try to return a single item. 3367d4665eaaSJeff Roberson */ 3368d4665eaaSJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 3369d4665eaaSJeff Roberson domain = PCPU_GET(domain); 3370d4665eaaSJeff Roberson else 3371d4665eaaSJeff Roberson domain = UMA_ANYDOMAIN; 3372d4665eaaSJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3373d4665eaaSJeff Roberson } 3374d4665eaaSJeff Roberson 3375d4665eaaSJeff Roberson /* See uma.h */ 3376d4665eaaSJeff Roberson void * 3377d4665eaaSJeff Roberson uma_zalloc_smr(uma_zone_t zone, int flags) 3378d4665eaaSJeff Roberson { 3379d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 3380d4665eaaSJeff Roberson uma_cache_t cache; 3381d4665eaaSJeff Roberson 3382d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 33836d88d784SJeff Roberson void *item; 33846d88d784SJeff Roberson 3385d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 3386952c8964SMark Johnston ("uma_zalloc_arg: called with non-SMR zone.")); 3387d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN) 3388d4665eaaSJeff Roberson return (item); 3389d4665eaaSJeff Roberson #endif 3390d4665eaaSJeff Roberson 3391d4665eaaSJeff Roberson critical_enter(); 3392d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3393d4665eaaSJeff Roberson bucket = &cache->uc_allocbucket; 33946d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 33956d88d784SJeff Roberson return (cache_alloc_retry(zone, cache, NULL, flags)); 33966d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, NULL, flags)); 3397d4665eaaSJeff Roberson } 3398d4665eaaSJeff Roberson 33999c2cd7e5SJeff Roberson /* See uma.h */ 34008355f576SJeff Roberson void * 34012cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 34028355f576SJeff Roberson { 3403376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3404ab3185d1SJeff Roberson uma_cache_t cache; 34058355f576SJeff Roberson 3406e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 340719fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 340810cb2424SMark Murray 34098355f576SJeff Roberson /* This is the fast path allocation */ 3410e63a1c2fSRyan Libby CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name, 3411e63a1c2fSRyan Libby zone, flags); 3412a553d4b8SJeff Roberson 3413d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 34146d88d784SJeff Roberson void *item; 34156d88d784SJeff Roberson 3416d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 3417952c8964SMark Johnston ("uma_zalloc_arg: called with SMR zone.")); 3418d4665eaaSJeff Roberson if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN) 34198d689e04SGleb Smirnoff return (item); 34208d689e04SGleb Smirnoff #endif 3421d4665eaaSJeff Roberson 34225d1ae027SRobert Watson /* 34235d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 34245d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 34255d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 34265d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 34275d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 34285d1ae027SRobert Watson * preemption and migration. We release the critical section in 34295d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 34305d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 34315d1ae027SRobert Watson * must detect and handle migration if it has occurred. 34325d1ae027SRobert Watson */ 34335d1ae027SRobert Watson critical_enter(); 3434cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3435376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 34366d88d784SJeff Roberson if (__predict_false(bucket->ucb_cnt == 0)) 34376d88d784SJeff Roberson return (cache_alloc_retry(zone, cache, udata, flags)); 34386d88d784SJeff Roberson return (cache_alloc_item(zone, cache, bucket, udata, flags)); 3439fc03d22bSJeff Roberson } 3440fc03d22bSJeff Roberson 34418355f576SJeff Roberson /* 3442beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 3443beb8beefSJeff Roberson * a critical section. Returns in a critical section. 3444beb8beefSJeff Roberson * 34454bd61e19SJeff Roberson * A false return value indicates an allocation failure. 34464bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 3447beb8beefSJeff Roberson */ 3448beb8beefSJeff Roberson static __noinline bool 3449beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3450beb8beefSJeff Roberson { 3451beb8beefSJeff Roberson uma_bucket_t bucket; 34528c277118SMark Johnston int curdomain, domain; 3453c6fd3e23SJeff Roberson bool new; 3454beb8beefSJeff Roberson 3455beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3456beb8beefSJeff Roberson 3457beb8beefSJeff Roberson /* 3458beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3459beb8beefSJeff Roberson * if we can switch with the free bucket. 3460d4665eaaSJeff Roberson * 3461d4665eaaSJeff Roberson * SMR Zones can't re-use the free bucket until the sequence has 3462d4665eaaSJeff Roberson * expired. 34638355f576SJeff Roberson */ 3464c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_SMR) == 0 && 3465d4665eaaSJeff Roberson cache->uc_freebucket.ucb_cnt != 0) { 3466d4665eaaSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 3467d4665eaaSJeff Roberson &cache->uc_allocbucket); 3468beb8beefSJeff Roberson return (true); 34698355f576SJeff Roberson } 3470fc03d22bSJeff Roberson 3471fc03d22bSJeff Roberson /* 3472fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3473fc03d22bSJeff Roberson */ 3474376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3475fc03d22bSJeff Roberson critical_exit(); 3476c6fd3e23SJeff Roberson 3477c6fd3e23SJeff Roberson if (bucket != NULL) { 3478c6fd3e23SJeff Roberson KASSERT(bucket->ub_cnt == 0, 3479c6fd3e23SJeff Roberson ("cache_alloc: Entered with non-empty alloc bucket.")); 34806fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3481c6fd3e23SJeff Roberson } 3482fc03d22bSJeff Roberson 34835d1ae027SRobert Watson /* 34845d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 3485c6fd3e23SJeff Roberson * we must go back to the zone. This requires the zdom lock, so we 34865d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 34875d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 34885d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 34895d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 34905d1ae027SRobert Watson * the critical section. 34915d1ae027SRobert Watson */ 3492c1685086SJeff Roberson domain = PCPU_GET(domain); 34938c277118SMark Johnston if ((cache_uz_flags(cache) & UMA_ZONE_ROUNDROBIN) != 0 || 34948c277118SMark Johnston VM_DOMAIN_EMPTY(domain)) 3495c6fd3e23SJeff Roberson domain = zone_domain_highest(zone, domain); 3496c6fd3e23SJeff Roberson bucket = cache_fetch_bucket(zone, cache, domain); 3497af32cefdSMark Johnston if (bucket == NULL && zone->uz_bucket_size != 0 && !bucketdisable) { 3498beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 3499c6fd3e23SJeff Roberson new = true; 3500af32cefdSMark Johnston } else { 3501c6fd3e23SJeff Roberson new = false; 3502af32cefdSMark Johnston } 3503c6fd3e23SJeff Roberson 35041431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 35051431a748SGleb Smirnoff zone->uz_name, zone, bucket); 35064bd61e19SJeff Roberson if (bucket == NULL) { 3507fc03d22bSJeff Roberson critical_enter(); 3508beb8beefSJeff Roberson return (false); 35094bd61e19SJeff Roberson } 35100f9b7bf3SMark Johnston 3511fc03d22bSJeff Roberson /* 3512fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3513fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3514fc03d22bSJeff Roberson * the memory directly. 3515fc03d22bSJeff Roberson */ 35164bd61e19SJeff Roberson critical_enter(); 3517cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3518376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 3519c6fd3e23SJeff Roberson ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) == 0 || 35208c277118SMark Johnston (curdomain = PCPU_GET(domain)) == domain || 35218c277118SMark Johnston VM_DOMAIN_EMPTY(curdomain))) { 3522c6fd3e23SJeff Roberson if (new) 3523c6fd3e23SJeff Roberson atomic_add_long(&ZDOM_GET(zone, domain)->uzd_imax, 3524c6fd3e23SJeff Roberson bucket->ub_cnt); 3525376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3526beb8beefSJeff Roberson return (true); 3527c6fd3e23SJeff Roberson } 3528c6fd3e23SJeff Roberson 3529c6fd3e23SJeff Roberson /* 3530c6fd3e23SJeff Roberson * We lost the race, release this bucket and start over. 3531c6fd3e23SJeff Roberson */ 3532c6fd3e23SJeff Roberson critical_exit(); 3533c6fd3e23SJeff Roberson zone_put_bucket(zone, domain, bucket, udata, false); 3534c6fd3e23SJeff Roberson critical_enter(); 3535c6fd3e23SJeff Roberson 3536beb8beefSJeff Roberson return (true); 3537bbee39c6SJeff Roberson } 3538bbee39c6SJeff Roberson 3539ab3185d1SJeff Roberson void * 3540ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3541bbee39c6SJeff Roberson { 354206d8bdcbSMark Johnston #ifdef NUMA 354306d8bdcbSMark Johnston uma_bucket_t bucket; 354406d8bdcbSMark Johnston uma_zone_domain_t zdom; 354506d8bdcbSMark Johnston void *item; 354606d8bdcbSMark Johnston #endif 3547ab3185d1SJeff Roberson 3548ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 354919fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3550ab3185d1SJeff Roberson 3551ab3185d1SJeff Roberson /* This is the fast path allocation */ 3552e63a1c2fSRyan Libby CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d", 3553e63a1c2fSRyan Libby zone->uz_name, zone, domain, flags); 3554ab3185d1SJeff Roberson 3555ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3556ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3557ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3558ab3185d1SJeff Roberson } 3559ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3560ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 356106d8bdcbSMark Johnston KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 356206d8bdcbSMark Johnston ("uma_zalloc_domain: called with SMR zone.")); 356306d8bdcbSMark Johnston #ifdef NUMA 356406d8bdcbSMark Johnston KASSERT((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0, 356506d8bdcbSMark Johnston ("uma_zalloc_domain: called with non-FIRSTTOUCH zone.")); 3566ab3185d1SJeff Roberson 356706d8bdcbSMark Johnston if (vm_ndomains == 1) 356806d8bdcbSMark Johnston return (uma_zalloc_arg(zone, udata, flags)); 356906d8bdcbSMark Johnston 357006d8bdcbSMark Johnston /* 357106d8bdcbSMark Johnston * Try to allocate from the bucket cache before falling back to the keg. 357206d8bdcbSMark Johnston * We could try harder and attempt to allocate from per-CPU caches or 357306d8bdcbSMark Johnston * the per-domain cross-domain buckets, but the complexity is probably 357406d8bdcbSMark Johnston * not worth it. It is more important that frees of previous 357506d8bdcbSMark Johnston * cross-domain allocations do not blow up the cache. 357606d8bdcbSMark Johnston */ 357706d8bdcbSMark Johnston zdom = zone_domain_lock(zone, domain); 357806d8bdcbSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) { 357906d8bdcbSMark Johnston item = bucket->ub_bucket[bucket->ub_cnt - 1]; 358006d8bdcbSMark Johnston #ifdef INVARIANTS 358106d8bdcbSMark Johnston bucket->ub_bucket[bucket->ub_cnt - 1] = NULL; 358206d8bdcbSMark Johnston #endif 358306d8bdcbSMark Johnston bucket->ub_cnt--; 358406d8bdcbSMark Johnston zone_put_bucket(zone, domain, bucket, udata, true); 358506d8bdcbSMark Johnston item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, 358606d8bdcbSMark Johnston flags, item); 358706d8bdcbSMark Johnston if (item != NULL) { 358806d8bdcbSMark Johnston KASSERT(item_domain(item) == domain, 358906d8bdcbSMark Johnston ("%s: bucket cache item %p from wrong domain", 359006d8bdcbSMark Johnston __func__, item)); 359106d8bdcbSMark Johnston counter_u64_add(zone->uz_allocs, 1); 359206d8bdcbSMark Johnston } 359306d8bdcbSMark Johnston return (item); 359406d8bdcbSMark Johnston } 359506d8bdcbSMark Johnston ZDOM_UNLOCK(zdom); 3596ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 359706d8bdcbSMark Johnston #else 359806d8bdcbSMark Johnston return (uma_zalloc_arg(zone, udata, flags)); 359906d8bdcbSMark Johnston #endif 3600ab3185d1SJeff Roberson } 3601ab3185d1SJeff Roberson 3602ab3185d1SJeff Roberson /* 3603ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3604ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3605ab3185d1SJeff Roberson * 3606ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3607ab3185d1SJeff Roberson * only 'domain'. 3608ab3185d1SJeff Roberson */ 3609ab3185d1SJeff Roberson static uma_slab_t 3610194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3611ab3185d1SJeff Roberson { 3612ab3185d1SJeff Roberson uma_domain_t dom; 3613bbee39c6SJeff Roberson uma_slab_t slab; 3614ab3185d1SJeff Roberson int start; 3615ab3185d1SJeff Roberson 3616ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3617ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 36188b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3619ab3185d1SJeff Roberson 3620ab3185d1SJeff Roberson slab = NULL; 3621ab3185d1SJeff Roberson start = domain; 3622ab3185d1SJeff Roberson do { 3623ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 36244ab3aee8SMark Johnston if ((slab = LIST_FIRST(&dom->ud_part_slab)) != NULL) 36254ab3aee8SMark Johnston return (slab); 36264ab3aee8SMark Johnston if ((slab = LIST_FIRST(&dom->ud_free_slab)) != NULL) { 3627ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 36284ab3aee8SMark Johnston dom->ud_free_slabs--; 3629ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3630ab3185d1SJeff Roberson return (slab); 3631ab3185d1SJeff Roberson } 3632ab3185d1SJeff Roberson if (rr) 3633ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3634ab3185d1SJeff Roberson } while (domain != start); 3635ab3185d1SJeff Roberson 3636ab3185d1SJeff Roberson return (NULL); 3637ab3185d1SJeff Roberson } 3638ab3185d1SJeff Roberson 36398b987a77SJeff Roberson /* 36408b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 36418b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 36428b987a77SJeff Roberson */ 3643ab3185d1SJeff Roberson static uma_slab_t 3644194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3645ab3185d1SJeff Roberson { 36468b987a77SJeff Roberson uma_slab_t slab; 3647194a979eSMark Johnston uint32_t reserve; 3648099a0e58SBosko Milekic 36498b987a77SJeff Roberson /* HASH has a single free list. */ 365054c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 36518b987a77SJeff Roberson domain = 0; 3652194a979eSMark Johnston 36538b987a77SJeff Roberson KEG_LOCK(keg, domain); 3654194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 36554ab3aee8SMark Johnston if (keg->uk_domain[domain].ud_free_items <= reserve || 36568b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 36578b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3658194a979eSMark Johnston return (NULL); 36598b987a77SJeff Roberson } 36608b987a77SJeff Roberson return (slab); 3661194a979eSMark Johnston } 3662194a979eSMark Johnston 3663194a979eSMark Johnston static uma_slab_t 3664194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3665194a979eSMark Johnston { 3666194a979eSMark Johnston struct vm_domainset_iter di; 3667194a979eSMark Johnston uma_slab_t slab; 3668194a979eSMark Johnston int aflags, domain; 3669194a979eSMark Johnston bool rr; 3670194a979eSMark Johnston 3671194a979eSMark Johnston restart: 3672bbee39c6SJeff Roberson /* 3673194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3674194a979eSMark Johnston * domain (as happens with first-touch zones). 3675194a979eSMark Johnston * 3676194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3677194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3678194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3679bbee39c6SJeff Roberson */ 3680ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3681ab3185d1SJeff Roberson if (rr) { 3682194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3683194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3684194a979eSMark Johnston &aflags); 3685194a979eSMark Johnston } else { 3686194a979eSMark Johnston aflags = flags; 3687194a979eSMark Johnston domain = rdomain; 3688194a979eSMark Johnston } 3689ab3185d1SJeff Roberson 3690194a979eSMark Johnston for (;;) { 3691194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3692584061b4SJeff Roberson if (slab != NULL) 3693bbee39c6SJeff Roberson return (slab); 3694bbee39c6SJeff Roberson 3695bbee39c6SJeff Roberson /* 3696bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3697bbee39c6SJeff Roberson */ 3698bbee39c6SJeff Roberson if (flags & M_NOVM) 3699bbee39c6SJeff Roberson break; 3700bbee39c6SJeff Roberson 370186220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 37028b987a77SJeff Roberson if (slab != NULL) 3703bbee39c6SJeff Roberson return (slab); 37043639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 37053639ac42SJeff Roberson break; 3706194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3707194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 370889d2fb14SKonstantin Belousov vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0); 3709194a979eSMark Johnston goto restart; 371030c5525bSAndrew Gallatin } 3711194a979eSMark Johnston break; 3712194a979eSMark Johnston } 3713ab3185d1SJeff Roberson } 3714ab3185d1SJeff Roberson 3715bbee39c6SJeff Roberson /* 3716bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3717bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3718bbee39c6SJeff Roberson * fail. 3719bbee39c6SJeff Roberson */ 37208b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 3721bbee39c6SJeff Roberson return (slab); 37228b987a77SJeff Roberson 3723ab3185d1SJeff Roberson return (NULL); 3724ab3185d1SJeff Roberson } 3725bbee39c6SJeff Roberson 3726d56368d7SBosko Milekic static void * 37270095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3728bbee39c6SJeff Roberson { 3729ab3185d1SJeff Roberson uma_domain_t dom; 3730bbee39c6SJeff Roberson void *item; 37319b8db4d0SRyan Libby int freei; 3732bbee39c6SJeff Roberson 37338b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3734099a0e58SBosko Milekic 37358b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 37369b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 37379b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 37381e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3739bbee39c6SJeff Roberson slab->us_freecount--; 37404ab3aee8SMark Johnston dom->ud_free_items--; 3741ef72505eSJeff Roberson 37424ab3aee8SMark Johnston /* 37434ab3aee8SMark Johnston * Move this slab to the full list. It must be on the partial list, so 37444ab3aee8SMark Johnston * we do not need to update the free slab count. In particular, 37454ab3aee8SMark Johnston * keg_fetch_slab() always returns slabs on the partial list. 37464ab3aee8SMark Johnston */ 3747bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3748bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3749ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3750bbee39c6SJeff Roberson } 3751bbee39c6SJeff Roberson 3752bbee39c6SJeff Roberson return (item); 3753bbee39c6SJeff Roberson } 3754bbee39c6SJeff Roberson 3755bbee39c6SJeff Roberson static int 3756b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 37570095a784SJeff Roberson { 37588b987a77SJeff Roberson uma_domain_t dom; 3759b75c4efcSAndrew Turner uma_zone_t zone; 37600095a784SJeff Roberson uma_slab_t slab; 37610095a784SJeff Roberson uma_keg_t keg; 3762a03af342SSean Bruno #ifdef NUMA 3763ab3185d1SJeff Roberson int stripe; 3764a03af342SSean Bruno #endif 37650095a784SJeff Roberson int i; 37660095a784SJeff Roberson 3767b75c4efcSAndrew Turner zone = arg; 37680095a784SJeff Roberson slab = NULL; 3769584061b4SJeff Roberson keg = zone->uz_keg; 3770af526374SJeff Roberson /* Try to keep the buckets totally full */ 37710095a784SJeff Roberson for (i = 0; i < max; ) { 3772584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 37730095a784SJeff Roberson break; 3774a03af342SSean Bruno #ifdef NUMA 3775ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3776a03af342SSean Bruno #endif 37778b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 37781b2dcc8cSMark Johnston do { 37790095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 37801b2dcc8cSMark Johnston if (dom->ud_free_items <= keg->uk_reserve) { 37811b2dcc8cSMark Johnston /* 37821b2dcc8cSMark Johnston * Avoid depleting the reserve after a 37831b2dcc8cSMark Johnston * successful item allocation, even if 37841b2dcc8cSMark Johnston * M_USE_RESERVE is specified. 37851b2dcc8cSMark Johnston */ 37861b2dcc8cSMark Johnston KEG_UNLOCK(keg, slab->us_domain); 37871b2dcc8cSMark Johnston goto out; 37881b2dcc8cSMark Johnston } 3789b6715dabSJeff Roberson #ifdef NUMA 3790ab3185d1SJeff Roberson /* 3791ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3792ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3793ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3794ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3795ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3796ab3185d1SJeff Roberson * time but yields better distribution. 3797ab3185d1SJeff Roberson */ 3798dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 && 3799ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3800ab3185d1SJeff Roberson break; 3801ab3185d1SJeff Roberson #endif 38021b2dcc8cSMark Johnston } while (slab->us_freecount != 0 && i < max); 38038b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 38041b2dcc8cSMark Johnston 3805ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 38060095a784SJeff Roberson flags &= ~M_WAITOK; 38070095a784SJeff Roberson flags |= M_NOWAIT; 38080095a784SJeff Roberson } 38091b2dcc8cSMark Johnston out: 38100095a784SJeff Roberson return i; 38110095a784SJeff Roberson } 38120095a784SJeff Roberson 38134bd61e19SJeff Roberson static int 38144bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 38154bd61e19SJeff Roberson { 38164bd61e19SJeff Roberson uint64_t old, new, total, max; 38174bd61e19SJeff Roberson 38184bd61e19SJeff Roberson /* 38194bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 38204bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 38214bd61e19SJeff Roberson * fairness by keeping fifo order. 38224bd61e19SJeff Roberson * 38234bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 38244bd61e19SJeff Roberson */ 38254bd61e19SJeff Roberson for (;;) { 38264bd61e19SJeff Roberson zone_free_limit(zone, count); 38274bd61e19SJeff Roberson zone_log_warning(zone); 38284bd61e19SJeff Roberson zone_maxaction(zone); 38294bd61e19SJeff Roberson if (flags & M_NOWAIT) 38304bd61e19SJeff Roberson return (0); 38314bd61e19SJeff Roberson 38324bd61e19SJeff Roberson /* 38334bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 38344bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 38354bd61e19SJeff Roberson * is essentially a home rolled semaphore. 38364bd61e19SJeff Roberson */ 38374bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 38384bd61e19SJeff Roberson old = zone->uz_items; 38394bd61e19SJeff Roberson do { 38404bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 38414bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 38424bd61e19SJeff Roberson max = zone->uz_max_items; 38434bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 38444bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 38454bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 38464bd61e19SJeff Roberson else 38474bd61e19SJeff Roberson new = old + MIN(count, max - old); 38484bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 38494bd61e19SJeff Roberson 38504bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 38514bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 38524bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 38534bd61e19SJeff Roberson return (new - old); 38544bd61e19SJeff Roberson } 38554bd61e19SJeff Roberson 38564bd61e19SJeff Roberson /* 38574bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 38584bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 38594bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 38604bd61e19SJeff Roberson * transitions. 38614bd61e19SJeff Roberson */ 38624bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 38634bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 38644bd61e19SJeff Roberson 38654bd61e19SJeff Roberson /* 38664bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 38674bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 38684bd61e19SJeff Roberson */ 38694bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 38704bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 38714bd61e19SJeff Roberson 38724bd61e19SJeff Roberson /* 38734bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 38744bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 38754bd61e19SJeff Roberson * 38764bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 38774bd61e19SJeff Roberson * our count. 38784bd61e19SJeff Roberson */ 38794bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 38804bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 38814bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 38824bd61e19SJeff Roberson /* We're no longer a sleeper. */ 38834bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 38844bd61e19SJeff Roberson 38854bd61e19SJeff Roberson /* 38864bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 38874bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 38884bd61e19SJeff Roberson * against changes via sysctl. 38894bd61e19SJeff Roberson */ 38904bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 38914bd61e19SJeff Roberson max = zone->uz_max_items; 38924bd61e19SJeff Roberson if (total >= max) 38934bd61e19SJeff Roberson continue; 38944bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 38954bd61e19SJeff Roberson if (total + count > max) { 38964bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 38974bd61e19SJeff Roberson count = max - total; 38984bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 38994bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 39004bd61e19SJeff Roberson 39014bd61e19SJeff Roberson return (count); 39024bd61e19SJeff Roberson } 39034bd61e19SJeff Roberson } 39044bd61e19SJeff Roberson 39054bd61e19SJeff Roberson /* 39064bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 39074bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 39084bd61e19SJeff Roberson * one item can be allocated. 39094bd61e19SJeff Roberson */ 39104bd61e19SJeff Roberson static int 39114bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 39124bd61e19SJeff Roberson { 39134bd61e19SJeff Roberson uint64_t old; 39144bd61e19SJeff Roberson uint64_t max; 39154bd61e19SJeff Roberson 39164bd61e19SJeff Roberson max = zone->uz_max_items; 39174bd61e19SJeff Roberson MPASS(max > 0); 39184bd61e19SJeff Roberson 39194bd61e19SJeff Roberson /* 39204bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 39214bd61e19SJeff Roberson * fetchadd. 39224bd61e19SJeff Roberson */ 39234bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 39244bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 39254bd61e19SJeff Roberson return (count); 39264bd61e19SJeff Roberson 39274bd61e19SJeff Roberson /* 39284bd61e19SJeff Roberson * If we had some items and no sleepers just return the 39294bd61e19SJeff Roberson * truncated value. We have to release the excess space 39304bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 39314bd61e19SJeff Roberson * because we were temporarily over the limit. 39324bd61e19SJeff Roberson */ 39334bd61e19SJeff Roberson if (old < max) { 39344bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 39354bd61e19SJeff Roberson return (max - old); 39364bd61e19SJeff Roberson } 39374bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 39384bd61e19SJeff Roberson } 39394bd61e19SJeff Roberson 39404bd61e19SJeff Roberson /* 39414bd61e19SJeff Roberson * Free a number of items back to the limit. 39424bd61e19SJeff Roberson */ 39434bd61e19SJeff Roberson static void 39444bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 39454bd61e19SJeff Roberson { 39464bd61e19SJeff Roberson uint64_t old; 39474bd61e19SJeff Roberson 39484bd61e19SJeff Roberson MPASS(count > 0); 39494bd61e19SJeff Roberson 39504bd61e19SJeff Roberson /* 39514bd61e19SJeff Roberson * In the common case we either have no sleepers or 39524bd61e19SJeff Roberson * are still over the limit and can just return. 39534bd61e19SJeff Roberson */ 39544bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 39554bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 39564bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 39574bd61e19SJeff Roberson return; 39584bd61e19SJeff Roberson 39594bd61e19SJeff Roberson /* 39604bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 39614bd61e19SJeff Roberson * to generate wakeups if necessary. 39624bd61e19SJeff Roberson */ 39634bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 39644bd61e19SJeff Roberson } 39654bd61e19SJeff Roberson 3966fc03d22bSJeff Roberson static uma_bucket_t 3967beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3968bbee39c6SJeff Roberson { 3969bbee39c6SJeff Roberson uma_bucket_t bucket; 3970beb8beefSJeff Roberson int maxbucket, cnt; 3971bbee39c6SJeff Roberson 3972e63a1c2fSRyan Libby CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name, 3973e63a1c2fSRyan Libby zone, domain); 397430c5525bSAndrew Gallatin 3975c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3976c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3977c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 39788c277118SMark Johnston else if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0) 3979c6fd3e23SJeff Roberson domain = UMA_ANYDOMAIN; 3980c1685086SJeff Roberson 39814bd61e19SJeff Roberson if (zone->uz_max_items > 0) 39824bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 39834bd61e19SJeff Roberson M_NOWAIT); 39844bd61e19SJeff Roberson else 398520a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 39864bd61e19SJeff Roberson if (maxbucket == 0) 39874bd61e19SJeff Roberson return (false); 3988beb8beefSJeff Roberson 39896fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 39906fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3991beb8beefSJeff Roberson if (bucket == NULL) { 3992beb8beefSJeff Roberson cnt = 0; 3993beb8beefSJeff Roberson goto out; 3994beb8beefSJeff Roberson } 39950095a784SJeff Roberson 39960095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3997beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 39980095a784SJeff Roberson 39990095a784SJeff Roberson /* 40000095a784SJeff Roberson * Initialize the memory if necessary. 40010095a784SJeff Roberson */ 40020095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 4003099a0e58SBosko Milekic int i; 4004bbee39c6SJeff Roberson 40050095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 4006e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 40070095a784SJeff Roberson flags) != 0) 4008b23f72e9SBrian Feldman break; 4009b23f72e9SBrian Feldman /* 4010b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 4011b23f72e9SBrian Feldman * rest back onto the freelist. 4012b23f72e9SBrian Feldman */ 4013b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 4014af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 40150095a784SJeff Roberson bucket->ub_cnt - i); 4016a5a262c6SBosko Milekic #ifdef INVARIANTS 40170095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 40180095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 4019a5a262c6SBosko Milekic #endif 4020b23f72e9SBrian Feldman bucket->ub_cnt = i; 4021b23f72e9SBrian Feldman } 4022099a0e58SBosko Milekic } 4023099a0e58SBosko Milekic 4024beb8beefSJeff Roberson cnt = bucket->ub_cnt; 4025f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 40266fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 40272efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 4028beb8beefSJeff Roberson bucket = NULL; 4029beb8beefSJeff Roberson } 4030beb8beefSJeff Roberson out: 40314bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 40324bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 4033fc03d22bSJeff Roberson 4034fc03d22bSJeff Roberson return (bucket); 4035fc03d22bSJeff Roberson } 4036fc03d22bSJeff Roberson 40378355f576SJeff Roberson /* 40380095a784SJeff Roberson * Allocates a single item from a zone. 40398355f576SJeff Roberson * 40408355f576SJeff Roberson * Arguments 40418355f576SJeff Roberson * zone The zone to alloc for. 40428355f576SJeff Roberson * udata The data to be passed to the constructor. 4043ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 4044a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 40458355f576SJeff Roberson * 40468355f576SJeff Roberson * Returns 40478355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 4048bbee39c6SJeff Roberson * An item if successful 40498355f576SJeff Roberson */ 40508355f576SJeff Roberson 40518355f576SJeff Roberson static void * 4052ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 40538355f576SJeff Roberson { 40548355f576SJeff Roberson void *item; 40558355f576SJeff Roberson 4056791dda87SAndrew Gallatin if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) { 4057791dda87SAndrew Gallatin counter_u64_add(zone->uz_fails, 1); 4058bb15d1c7SGleb Smirnoff return (NULL); 4059791dda87SAndrew Gallatin } 40608355f576SJeff Roberson 4061c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 4062c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 406330c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 4064c1685086SJeff Roberson 4065ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 4066beb8beefSJeff Roberson goto fail_cnt; 40678355f576SJeff Roberson 4068099a0e58SBosko Milekic /* 4069099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 4070099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 4071099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 4072099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 4073099a0e58SBosko Milekic */ 4074b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 4075e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 4076bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 4077beb8beefSJeff Roberson goto fail_cnt; 4078beb8beefSJeff Roberson } 4079beb8beefSJeff Roberson } 4080d4665eaaSJeff Roberson item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags, 4081d4665eaaSJeff Roberson item); 4082beb8beefSJeff Roberson if (item == NULL) 40830095a784SJeff Roberson goto fail; 40848355f576SJeff Roberson 40852efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 40861431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 40871431a748SGleb Smirnoff zone->uz_name, zone); 40881431a748SGleb Smirnoff 40898355f576SJeff Roberson return (item); 40900095a784SJeff Roberson 4091beb8beefSJeff Roberson fail_cnt: 4092beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 40930095a784SJeff Roberson fail: 40944bd61e19SJeff Roberson if (zone->uz_max_items > 0) 40954bd61e19SJeff Roberson zone_free_limit(zone, 1); 40961431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 40971431a748SGleb Smirnoff zone->uz_name, zone); 40984bd61e19SJeff Roberson 40990095a784SJeff Roberson return (NULL); 41008355f576SJeff Roberson } 41018355f576SJeff Roberson 41028355f576SJeff Roberson /* See uma.h */ 41038355f576SJeff Roberson void 4104d4665eaaSJeff Roberson uma_zfree_smr(uma_zone_t zone, void *item) 4105d4665eaaSJeff Roberson { 4106d4665eaaSJeff Roberson uma_cache_t cache; 4107d4665eaaSJeff Roberson uma_cache_bucket_t bucket; 4108c6fd3e23SJeff Roberson int itemdomain, uz_flags; 4109d4665eaaSJeff Roberson 4110d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 4111d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0, 4112952c8964SMark Johnston ("uma_zfree_smr: called with non-SMR zone.")); 4113d4665eaaSJeff Roberson KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer.")); 4114c6fd3e23SJeff Roberson SMR_ASSERT_NOT_ENTERED(zone->uz_smr); 4115d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN) 4116d4665eaaSJeff Roberson return; 4117d4665eaaSJeff Roberson #endif 4118d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4119d4665eaaSJeff Roberson uz_flags = cache_uz_flags(cache); 4120c6fd3e23SJeff Roberson itemdomain = 0; 4121d4665eaaSJeff Roberson #ifdef NUMA 4122d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 412381302f1dSMark Johnston itemdomain = item_domain(item); 4124d4665eaaSJeff Roberson #endif 4125d4665eaaSJeff Roberson critical_enter(); 4126d4665eaaSJeff Roberson do { 4127d4665eaaSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4128d4665eaaSJeff Roberson /* SMR Zones must free to the free bucket. */ 4129d4665eaaSJeff Roberson bucket = &cache->uc_freebucket; 4130d4665eaaSJeff Roberson #ifdef NUMA 4131d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4132c6fd3e23SJeff Roberson PCPU_GET(domain) != itemdomain) { 4133d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 4134d4665eaaSJeff Roberson } 4135d4665eaaSJeff Roberson #endif 4136d4665eaaSJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 4137d4665eaaSJeff Roberson cache_bucket_push(cache, bucket, item); 4138d4665eaaSJeff Roberson critical_exit(); 4139d4665eaaSJeff Roberson return; 4140d4665eaaSJeff Roberson } 4141d4665eaaSJeff Roberson } while (cache_free(zone, cache, NULL, item, itemdomain)); 4142d4665eaaSJeff Roberson critical_exit(); 4143d4665eaaSJeff Roberson 4144d4665eaaSJeff Roberson /* 4145d4665eaaSJeff Roberson * If nothing else caught this, we'll just do an internal free. 4146d4665eaaSJeff Roberson */ 4147d4665eaaSJeff Roberson zone_free_item(zone, item, NULL, SKIP_NONE); 4148d4665eaaSJeff Roberson } 4149d4665eaaSJeff Roberson 4150d4665eaaSJeff Roberson /* See uma.h */ 4151d4665eaaSJeff Roberson void 41528355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 41538355f576SJeff Roberson { 41548355f576SJeff Roberson uma_cache_t cache; 4155376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 4156c6fd3e23SJeff Roberson int itemdomain, uz_flags; 41578355f576SJeff Roberson 4158e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 415919fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 416010cb2424SMark Murray 4161e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone); 41623659f747SRobert Watson 4163d4665eaaSJeff Roberson #ifdef UMA_ZALLOC_DEBUG 4164d4665eaaSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 4165952c8964SMark Johnston ("uma_zfree_arg: called with SMR zone.")); 4166d4665eaaSJeff Roberson if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN) 4167d4665eaaSJeff Roberson return; 4168d4665eaaSJeff Roberson #endif 416920ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 417020ed0cb0SMatthew D Fleming if (item == NULL) 417120ed0cb0SMatthew D Fleming return; 4172cc7ce83aSJeff Roberson 4173cc7ce83aSJeff Roberson /* 4174cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 4175cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 4176cc7ce83aSJeff Roberson * will simply read another cpu's line. 4177cc7ce83aSJeff Roberson */ 4178cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4179cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 4180d4665eaaSJeff Roberson if (UMA_ALWAYS_CTORDTOR || 4181d4665eaaSJeff Roberson __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0)) 4182cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 4183ef72505eSJeff Roberson 4184af7f9b97SJeff Roberson /* 4185af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 4186af7f9b97SJeff Roberson * a little longer for the limits to be reset. 4187af7f9b97SJeff Roberson */ 4188cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 4189*8a6776caSMark Johnston if (atomic_load_32(&zone->uz_sleepers) > 0) 4190fc03d22bSJeff Roberson goto zfree_item; 4191cc7ce83aSJeff Roberson } 4192af7f9b97SJeff Roberson 41935d1ae027SRobert Watson /* 41945d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 41955d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 41965d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 41975d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 41985d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 41995d1ae027SRobert Watson * preemption and migration. We release the critical section in 42005d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 42015d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 42025d1ae027SRobert Watson * detect and handle migration if it has occurred. 42035d1ae027SRobert Watson */ 4204c6fd3e23SJeff Roberson itemdomain = 0; 4205dfe13344SJeff Roberson #ifdef NUMA 4206dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 420781302f1dSMark Johnston itemdomain = item_domain(item); 4208dfe13344SJeff Roberson #endif 42095d1ae027SRobert Watson critical_enter(); 42100a81b439SJeff Roberson do { 4211cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4212a553d4b8SJeff Roberson /* 4213dfe13344SJeff Roberson * Try to free into the allocbucket first to give LIFO 4214dfe13344SJeff Roberson * ordering for cache-hot datastructures. Spill over 4215dfe13344SJeff Roberson * into the freebucket if necessary. Alloc will swap 4216dfe13344SJeff Roberson * them if one runs dry. 4217a553d4b8SJeff Roberson */ 4218dfe13344SJeff Roberson bucket = &cache->uc_allocbucket; 4219d4665eaaSJeff Roberson #ifdef NUMA 4220d4665eaaSJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4221c6fd3e23SJeff Roberson PCPU_GET(domain) != itemdomain) { 4222d4665eaaSJeff Roberson bucket = &cache->uc_crossbucket; 4223d4665eaaSJeff Roberson } else 4224d4665eaaSJeff Roberson #endif 4225fe835cbfSJeff Roberson if (bucket->ucb_cnt == bucket->ucb_entries && 4226fe835cbfSJeff Roberson cache->uc_freebucket.ucb_cnt < 4227fe835cbfSJeff Roberson cache->uc_freebucket.ucb_entries) 4228fe835cbfSJeff Roberson cache_bucket_swap(&cache->uc_freebucket, 4229fe835cbfSJeff Roberson &cache->uc_allocbucket); 4230376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 4231376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 42325d1ae027SRobert Watson critical_exit(); 42338355f576SJeff Roberson return; 4234fc03d22bSJeff Roberson } 42350a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 42360a81b439SJeff Roberson critical_exit(); 4237fc03d22bSJeff Roberson 42388355f576SJeff Roberson /* 42390a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 42408355f576SJeff Roberson */ 42410a81b439SJeff Roberson zfree_item: 42420a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 42430a81b439SJeff Roberson } 4244fc03d22bSJeff Roberson 4245dfe13344SJeff Roberson #ifdef NUMA 424691d947bfSJeff Roberson /* 424791d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 424891d947bfSJeff Roberson * them. 424991d947bfSJeff Roberson */ 425091d947bfSJeff Roberson static void 425191d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 425291d947bfSJeff Roberson { 4253991f23efSMark Johnston struct uma_bucketlist emptybuckets, fullbuckets; 425491d947bfSJeff Roberson uma_zone_domain_t zdom; 425591d947bfSJeff Roberson uma_bucket_t b; 4256543117beSJeff Roberson smr_seq_t seq; 425791d947bfSJeff Roberson void *item; 425891d947bfSJeff Roberson int domain; 425991d947bfSJeff Roberson 426091d947bfSJeff Roberson CTR3(KTR_UMA, 426191d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 426291d947bfSJeff Roberson zone->uz_name, zone, bucket); 426391d947bfSJeff Roberson 4264543117beSJeff Roberson /* 4265543117beSJeff Roberson * It is possible for buckets to arrive here out of order so we fetch 4266543117beSJeff Roberson * the current smr seq rather than accepting the bucket's. 4267543117beSJeff Roberson */ 4268543117beSJeff Roberson seq = SMR_SEQ_INVALID; 4269543117beSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) 4270226dd6dbSJeff Roberson seq = smr_advance(zone->uz_smr); 4271226dd6dbSJeff Roberson 4272226dd6dbSJeff Roberson /* 4273226dd6dbSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 4274226dd6dbSJeff Roberson * lock on the current crossfree bucket. A full matrix with 4275226dd6dbSJeff Roberson * per-domain locking could be used if necessary. 4276226dd6dbSJeff Roberson */ 4277991f23efSMark Johnston STAILQ_INIT(&emptybuckets); 4278226dd6dbSJeff Roberson STAILQ_INIT(&fullbuckets); 4279226dd6dbSJeff Roberson ZONE_CROSS_LOCK(zone); 4280991f23efSMark Johnston for (; bucket->ub_cnt > 0; bucket->ub_cnt--) { 428191d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 428281302f1dSMark Johnston domain = item_domain(item); 4283c6fd3e23SJeff Roberson zdom = ZDOM_GET(zone, domain); 428491d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 4285991f23efSMark Johnston if ((b = STAILQ_FIRST(&emptybuckets)) != NULL) { 4286991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4287991f23efSMark Johnston zdom->uzd_cross = b; 4288991f23efSMark Johnston } else { 4289991f23efSMark Johnston /* 4290991f23efSMark Johnston * Avoid allocating a bucket with the cross lock 4291991f23efSMark Johnston * held, since allocation can trigger a 4292991f23efSMark Johnston * cross-domain free and bucket zones may 4293991f23efSMark Johnston * allocate from each other. 4294991f23efSMark Johnston */ 4295991f23efSMark Johnston ZONE_CROSS_UNLOCK(zone); 4296991f23efSMark Johnston b = bucket_alloc(zone, udata, M_NOWAIT); 4297991f23efSMark Johnston if (b == NULL) 4298991f23efSMark Johnston goto out; 4299991f23efSMark Johnston ZONE_CROSS_LOCK(zone); 4300991f23efSMark Johnston if (zdom->uzd_cross != NULL) { 4301991f23efSMark Johnston STAILQ_INSERT_HEAD(&emptybuckets, b, 4302991f23efSMark Johnston ub_link); 4303991f23efSMark Johnston } else { 4304991f23efSMark Johnston zdom->uzd_cross = b; 4305991f23efSMark Johnston } 4306991f23efSMark Johnston } 430791d947bfSJeff Roberson } 4308543117beSJeff Roberson b = zdom->uzd_cross; 4309543117beSJeff Roberson b->ub_bucket[b->ub_cnt++] = item; 4310543117beSJeff Roberson b->ub_seq = seq; 4311543117beSJeff Roberson if (b->ub_cnt == b->ub_entries) { 4312543117beSJeff Roberson STAILQ_INSERT_HEAD(&fullbuckets, b, ub_link); 4313991f23efSMark Johnston if ((b = STAILQ_FIRST(&emptybuckets)) != NULL) 4314991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4315991f23efSMark Johnston zdom->uzd_cross = b; 431691d947bfSJeff Roberson } 431791d947bfSJeff Roberson } 431891d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 4319991f23efSMark Johnston out: 4320c6fd3e23SJeff Roberson if (bucket->ub_cnt == 0) 4321d4665eaaSJeff Roberson bucket->ub_seq = SMR_SEQ_INVALID; 432291d947bfSJeff Roberson bucket_free(zone, bucket, udata); 4323c6fd3e23SJeff Roberson 4324991f23efSMark Johnston while ((b = STAILQ_FIRST(&emptybuckets)) != NULL) { 4325991f23efSMark Johnston STAILQ_REMOVE_HEAD(&emptybuckets, ub_link); 4326991f23efSMark Johnston bucket_free(zone, b, udata); 4327991f23efSMark Johnston } 4328c6fd3e23SJeff Roberson while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) { 4329c6fd3e23SJeff Roberson STAILQ_REMOVE_HEAD(&fullbuckets, ub_link); 433081302f1dSMark Johnston domain = item_domain(b->ub_bucket[0]); 4331c6fd3e23SJeff Roberson zone_put_bucket(zone, domain, b, udata, true); 4332c6fd3e23SJeff Roberson } 433391d947bfSJeff Roberson } 433491d947bfSJeff Roberson #endif 433591d947bfSJeff Roberson 43360a81b439SJeff Roberson static void 43370a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 4338c6fd3e23SJeff Roberson int itemdomain, bool ws) 43390a81b439SJeff Roberson { 43400a81b439SJeff Roberson 4341dfe13344SJeff Roberson #ifdef NUMA 43420a81b439SJeff Roberson /* 43430a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 43440a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 43450a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 434691d947bfSJeff Roberson * correct domains. 43470a81b439SJeff Roberson */ 4348c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 4349c6fd3e23SJeff Roberson vm_ndomains > 2 && PCPU_GET(domain) != itemdomain) { 435091d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 43510a81b439SJeff Roberson return; 43520a81b439SJeff Roberson } 43530a81b439SJeff Roberson #endif 435491d947bfSJeff Roberson 43550a81b439SJeff Roberson /* 43560a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 43570a81b439SJeff Roberson */ 43580a81b439SJeff Roberson CTR3(KTR_UMA, 43590a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 43600a81b439SJeff Roberson zone->uz_name, zone, bucket); 43610a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 4362c6fd3e23SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0) 4363c6fd3e23SJeff Roberson itemdomain = zone_domain_lowest(zone, itemdomain); 4364c6fd3e23SJeff Roberson zone_put_bucket(zone, itemdomain, bucket, udata, ws); 43658355f576SJeff Roberson } 4366fc03d22bSJeff Roberson 43674d104ba0SAlexander Motin /* 43680a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 43690a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 43700a81b439SJeff Roberson * 43710a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 43720a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 43730a81b439SJeff Roberson * the caller should retry. 43744d104ba0SAlexander Motin */ 43750a81b439SJeff Roberson static __noinline bool 43760a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 43770a81b439SJeff Roberson int itemdomain) 43780a81b439SJeff Roberson { 4379dfe13344SJeff Roberson uma_cache_bucket_t cbucket; 4380d4665eaaSJeff Roberson uma_bucket_t newbucket, bucket; 43810a81b439SJeff Roberson 43820a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 43830a81b439SJeff Roberson 4384d4665eaaSJeff Roberson if (zone->uz_bucket_size == 0) 43850a81b439SJeff Roberson return false; 43860a81b439SJeff Roberson 4387cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4388d4665eaaSJeff Roberson newbucket = NULL; 43890a81b439SJeff Roberson 43900a81b439SJeff Roberson /* 4391dfe13344SJeff Roberson * FIRSTTOUCH domains need to free to the correct zdom. When 4392dfe13344SJeff Roberson * enabled this is the zdom of the item. The bucket is the 4393dfe13344SJeff Roberson * cross bucket if the current domain and itemdomain do not match. 43940a81b439SJeff Roberson */ 4395dfe13344SJeff Roberson cbucket = &cache->uc_freebucket; 4396dfe13344SJeff Roberson #ifdef NUMA 4397c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) { 4398c6fd3e23SJeff Roberson if (PCPU_GET(domain) != itemdomain) { 4399dfe13344SJeff Roberson cbucket = &cache->uc_crossbucket; 4400dfe13344SJeff Roberson if (cbucket->ucb_cnt != 0) 4401c6fd3e23SJeff Roberson counter_u64_add(zone->uz_xdomain, 4402dfe13344SJeff Roberson cbucket->ucb_cnt); 4403dfe13344SJeff Roberson } 4404c6fd3e23SJeff Roberson } 44050a81b439SJeff Roberson #endif 4406dfe13344SJeff Roberson bucket = cache_bucket_unload(cbucket); 4407c6fd3e23SJeff Roberson KASSERT(bucket == NULL || bucket->ub_cnt == bucket->ub_entries, 4408c6fd3e23SJeff Roberson ("cache_free: Entered with non-full free bucket.")); 44090a81b439SJeff Roberson 44100a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 44110a81b439SJeff Roberson critical_exit(); 44120a81b439SJeff Roberson 4413d4665eaaSJeff Roberson /* 4414d4665eaaSJeff Roberson * Don't let SMR zones operate without a free bucket. Force 4415d4665eaaSJeff Roberson * a synchronize and re-use this one. We will only degrade 4416d4665eaaSJeff Roberson * to a synchronize every bucket_size items rather than every 4417d4665eaaSJeff Roberson * item if we fail to allocate a bucket. 4418d4665eaaSJeff Roberson */ 4419d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0) { 4420d4665eaaSJeff Roberson if (bucket != NULL) 4421d4665eaaSJeff Roberson bucket->ub_seq = smr_advance(zone->uz_smr); 4422d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4423d4665eaaSJeff Roberson if (newbucket == NULL && bucket != NULL) { 4424d4665eaaSJeff Roberson bucket_drain(zone, bucket); 4425d4665eaaSJeff Roberson newbucket = bucket; 4426d4665eaaSJeff Roberson bucket = NULL; 4427d4665eaaSJeff Roberson } 4428d4665eaaSJeff Roberson } else if (!bucketdisable) 4429d4665eaaSJeff Roberson newbucket = bucket_alloc(zone, udata, M_NOWAIT); 4430d4665eaaSJeff Roberson 44310a81b439SJeff Roberson if (bucket != NULL) 4432c6fd3e23SJeff Roberson zone_free_bucket(zone, bucket, udata, itemdomain, true); 4433a553d4b8SJeff Roberson 4434fc03d22bSJeff Roberson critical_enter(); 4435d4665eaaSJeff Roberson if ((bucket = newbucket) == NULL) 44360a81b439SJeff Roberson return (false); 4437cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 4438dfe13344SJeff Roberson #ifdef NUMA 4439fc03d22bSJeff Roberson /* 44400a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 44410a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 44420a81b439SJeff Roberson * the free bucket. 4443fc03d22bSJeff Roberson */ 4444c6fd3e23SJeff Roberson if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) { 4445c6fd3e23SJeff Roberson if (PCPU_GET(domain) != itemdomain && 4446376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 4447376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 44480a81b439SJeff Roberson return (true); 44490a81b439SJeff Roberson } 44500a81b439SJeff Roberson } 44510a81b439SJeff Roberson #endif 44520a81b439SJeff Roberson /* 44530a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 44540a81b439SJeff Roberson */ 4455376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 4456fc03d22bSJeff Roberson critical_exit(); 44576fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 44580a81b439SJeff Roberson critical_enter(); 44590a81b439SJeff Roberson } else 4460376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 44618355f576SJeff Roberson 44620a81b439SJeff Roberson return (true); 44638355f576SJeff Roberson } 44648355f576SJeff Roberson 44658355f576SJeff Roberson static void 4466bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 44678355f576SJeff Roberson { 4468bb15d1c7SGleb Smirnoff uma_keg_t keg; 4469ab3185d1SJeff Roberson uma_domain_t dom; 44709b8db4d0SRyan Libby int freei; 4471099a0e58SBosko Milekic 4472bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 44738b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 4474ab3185d1SJeff Roberson 44758355f576SJeff Roberson /* Do we need to remove from any lists? */ 44768b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 4477099a0e58SBosko Milekic if (slab->us_freecount + 1 == keg->uk_ipers) { 44788355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4479ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 44804ab3aee8SMark Johnston dom->ud_free_slabs++; 44818355f576SJeff Roberson } else if (slab->us_freecount == 0) { 44828355f576SJeff Roberson LIST_REMOVE(slab, us_link); 4483ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 44848355f576SJeff Roberson } 44858355f576SJeff Roberson 4486ef72505eSJeff Roberson /* Slab management. */ 44871e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 44889b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 44898355f576SJeff Roberson slab->us_freecount++; 44908355f576SJeff Roberson 4491ef72505eSJeff Roberson /* Keg statistics. */ 44924ab3aee8SMark Johnston dom->ud_free_items++; 44930095a784SJeff Roberson } 44940095a784SJeff Roberson 44950095a784SJeff Roberson static void 4496b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 44970095a784SJeff Roberson { 44988b987a77SJeff Roberson struct mtx *lock; 4499b75c4efcSAndrew Turner uma_zone_t zone; 45000095a784SJeff Roberson uma_slab_t slab; 45010095a784SJeff Roberson uma_keg_t keg; 45020095a784SJeff Roberson uint8_t *mem; 45038b987a77SJeff Roberson void *item; 45040095a784SJeff Roberson int i; 45058355f576SJeff Roberson 4506b75c4efcSAndrew Turner zone = arg; 4507bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 45088b987a77SJeff Roberson lock = NULL; 450954c5ae80SRyan Libby if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0)) 45108b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 45110095a784SJeff Roberson for (i = 0; i < cnt; i++) { 45120095a784SJeff Roberson item = bucket[i]; 451354c5ae80SRyan Libby if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) { 45140095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 45158b987a77SJeff Roberson } else { 45168b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 451754c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0) 45188b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 45198b987a77SJeff Roberson else 45208b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 45218b987a77SJeff Roberson } 45228b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 45238b987a77SJeff Roberson if (lock != NULL) 45248b987a77SJeff Roberson mtx_unlock(lock); 45258b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 45268b987a77SJeff Roberson } 4527bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 45280095a784SJeff Roberson } 45298b987a77SJeff Roberson if (lock != NULL) 45308b987a77SJeff Roberson mtx_unlock(lock); 45318355f576SJeff Roberson } 45328355f576SJeff Roberson 45330095a784SJeff Roberson /* 45340095a784SJeff Roberson * Frees a single item to any zone. 45350095a784SJeff Roberson * 45360095a784SJeff Roberson * Arguments: 45370095a784SJeff Roberson * zone The zone to free to 45380095a784SJeff Roberson * item The item we're freeing 45390095a784SJeff Roberson * udata User supplied data for the dtor 45400095a784SJeff Roberson * skip Skip dtors and finis 45410095a784SJeff Roberson */ 45426d88d784SJeff Roberson static __noinline void 45430095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 45440095a784SJeff Roberson { 4545c5deaf04SGleb Smirnoff 4546d4665eaaSJeff Roberson /* 4547d4665eaaSJeff Roberson * If a free is sent directly to an SMR zone we have to 4548d4665eaaSJeff Roberson * synchronize immediately because the item can instantly 4549d4665eaaSJeff Roberson * be reallocated. This should only happen in degenerate 4550d4665eaaSJeff Roberson * cases when no memory is available for per-cpu caches. 4551d4665eaaSJeff Roberson */ 4552d4665eaaSJeff Roberson if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE) 4553d4665eaaSJeff Roberson smr_synchronize(zone->uz_smr); 4554d4665eaaSJeff Roberson 4555cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 45560095a784SJeff Roberson 45570095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 45580095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 45590095a784SJeff Roberson 45600095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4561bb15d1c7SGleb Smirnoff 4562bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4563bb15d1c7SGleb Smirnoff return; 4564bb15d1c7SGleb Smirnoff 45652efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 45662efcc8cbSGleb Smirnoff 45674bd61e19SJeff Roberson if (zone->uz_max_items > 0) 45684bd61e19SJeff Roberson zone_free_limit(zone, 1); 4569bb45b411SGleb Smirnoff } 45700095a784SJeff Roberson 45718355f576SJeff Roberson /* See uma.h */ 45721c6cae97SLawrence Stewart int 4573736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4574736ee590SJeff Roberson { 4575bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 4576003cf08bSMark Johnston int count; 4577bb15d1c7SGleb Smirnoff 45784bd61e19SJeff Roberson /* 45794bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 45804bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 45814bd61e19SJeff Roberson * way to clear a limit. 45824bd61e19SJeff Roberson */ 4583bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4584003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4585003cf08bSMark Johnston count = ubz != NULL ? ubz->ubz_entries : 0; 458620a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = count; 458720a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 458820a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4589bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4590cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4591cc7ce83aSJeff Roberson zone_update_caches(zone); 45924bd61e19SJeff Roberson /* We may need to wake waiters. */ 45934bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4594bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4595bb15d1c7SGleb Smirnoff 4596bb15d1c7SGleb Smirnoff return (nitems); 4597bb15d1c7SGleb Smirnoff } 4598bb15d1c7SGleb Smirnoff 4599bb15d1c7SGleb Smirnoff /* See uma.h */ 4600003cf08bSMark Johnston void 4601bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4602bb15d1c7SGleb Smirnoff { 4603003cf08bSMark Johnston struct uma_bucket_zone *ubz; 4604003cf08bSMark Johnston int bpcpu; 4605bb15d1c7SGleb Smirnoff 4606bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4607003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4608003cf08bSMark Johnston if (ubz != NULL) { 4609003cf08bSMark Johnston bpcpu = 2; 4610dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 4611003cf08bSMark Johnston /* Count the cross-domain bucket. */ 4612003cf08bSMark Johnston bpcpu++; 4613003cf08bSMark Johnston nitems -= ubz->ubz_entries * bpcpu * mp_ncpus; 461420a4e154SJeff Roberson zone->uz_bucket_size_max = ubz->ubz_entries; 4615003cf08bSMark Johnston } else { 461620a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 4617003cf08bSMark Johnston } 461820a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 461920a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4620c6fd3e23SJeff Roberson zone->uz_bucket_max = nitems / vm_ndomains; 4621bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4622736ee590SJeff Roberson } 4623736ee590SJeff Roberson 4624736ee590SJeff Roberson /* See uma.h */ 4625e49471b0SAndre Oppermann int 4626e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4627e49471b0SAndre Oppermann { 4628e49471b0SAndre Oppermann int nitems; 4629e49471b0SAndre Oppermann 4630727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4631e49471b0SAndre Oppermann 4632e49471b0SAndre Oppermann return (nitems); 4633e49471b0SAndre Oppermann } 4634e49471b0SAndre Oppermann 4635e49471b0SAndre Oppermann /* See uma.h */ 46362f891cd5SPawel Jakub Dawidek void 46372f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 46382f891cd5SPawel Jakub Dawidek { 46392f891cd5SPawel Jakub Dawidek 4640727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 46412f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 46422f891cd5SPawel Jakub Dawidek } 46432f891cd5SPawel Jakub Dawidek 46442f891cd5SPawel Jakub Dawidek /* See uma.h */ 464554503a13SJonathan T. Looney void 464654503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 464754503a13SJonathan T. Looney { 464854503a13SJonathan T. Looney 4649727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4650e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 465154503a13SJonathan T. Looney } 465254503a13SJonathan T. Looney 465354503a13SJonathan T. Looney /* See uma.h */ 4654c4ae7908SLawrence Stewart int 4655c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4656c4ae7908SLawrence Stewart { 4657c4ae7908SLawrence Stewart int64_t nitems; 4658c4ae7908SLawrence Stewart u_int i; 4659c4ae7908SLawrence Stewart 4660bfb6b7a1SJeff Roberson nitems = 0; 4661bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER) 46622efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 46632efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4664727c6918SJeff Roberson CPU_FOREACH(i) 4665727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4666727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4667c4ae7908SLawrence Stewart 4668c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4669c4ae7908SLawrence Stewart } 4670c4ae7908SLawrence Stewart 467120a4e154SJeff Roberson static uint64_t 467220a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 467320a4e154SJeff Roberson { 467420a4e154SJeff Roberson uint64_t nitems; 467520a4e154SJeff Roberson u_int i; 467620a4e154SJeff Roberson 4677bfb6b7a1SJeff Roberson nitems = 0; 4678bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER) 467920a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4680727c6918SJeff Roberson CPU_FOREACH(i) 4681727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 468220a4e154SJeff Roberson 468320a4e154SJeff Roberson return (nitems); 468420a4e154SJeff Roberson } 468520a4e154SJeff Roberson 468620a4e154SJeff Roberson static uint64_t 468720a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 468820a4e154SJeff Roberson { 468920a4e154SJeff Roberson uint64_t nitems; 469020a4e154SJeff Roberson u_int i; 469120a4e154SJeff Roberson 4692bfb6b7a1SJeff Roberson nitems = 0; 4693bfb6b7a1SJeff Roberson if (zone->uz_frees != EARLY_COUNTER) 469420a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4695727c6918SJeff Roberson CPU_FOREACH(i) 4696727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 469720a4e154SJeff Roberson 469820a4e154SJeff Roberson return (nitems); 469920a4e154SJeff Roberson } 470020a4e154SJeff Roberson 470131c251a0SJeff Roberson #ifdef INVARIANTS 470231c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */ 470331c251a0SJeff Roberson static uint64_t 470431c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg) 470531c251a0SJeff Roberson { 470631c251a0SJeff Roberson uma_zone_t z; 470731c251a0SJeff Roberson uint64_t nitems; 470831c251a0SJeff Roberson 470931c251a0SJeff Roberson nitems = 0; 471031c251a0SJeff Roberson LIST_FOREACH(z, &keg->uk_zones, uz_link) 471131c251a0SJeff Roberson nitems += uma_zone_get_allocs(z); 471231c251a0SJeff Roberson 471331c251a0SJeff Roberson return (nitems); 471431c251a0SJeff Roberson } 471531c251a0SJeff Roberson #endif 471631c251a0SJeff Roberson 4717c4ae7908SLawrence Stewart /* See uma.h */ 4718736ee590SJeff Roberson void 4719099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4720099a0e58SBosko Milekic { 4721e20a199fSJeff Roberson uma_keg_t keg; 4722e20a199fSJeff Roberson 4723bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4724727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4725e20a199fSJeff Roberson keg->uk_init = uminit; 4726099a0e58SBosko Milekic } 4727099a0e58SBosko Milekic 4728099a0e58SBosko Milekic /* See uma.h */ 4729099a0e58SBosko Milekic void 4730099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4731099a0e58SBosko Milekic { 4732e20a199fSJeff Roberson uma_keg_t keg; 4733e20a199fSJeff Roberson 4734bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4735727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4736e20a199fSJeff Roberson keg->uk_fini = fini; 4737099a0e58SBosko Milekic } 4738099a0e58SBosko Milekic 4739099a0e58SBosko Milekic /* See uma.h */ 4740099a0e58SBosko Milekic void 4741099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4742099a0e58SBosko Milekic { 4743af526374SJeff Roberson 4744727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4745099a0e58SBosko Milekic zone->uz_init = zinit; 4746099a0e58SBosko Milekic } 4747099a0e58SBosko Milekic 4748099a0e58SBosko Milekic /* See uma.h */ 4749099a0e58SBosko Milekic void 4750099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4751099a0e58SBosko Milekic { 4752af526374SJeff Roberson 4753727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4754099a0e58SBosko Milekic zone->uz_fini = zfini; 4755099a0e58SBosko Milekic } 4756099a0e58SBosko Milekic 4757099a0e58SBosko Milekic /* See uma.h */ 4758099a0e58SBosko Milekic void 47598355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 47608355f576SJeff Roberson { 47610095a784SJeff Roberson uma_keg_t keg; 4762e20a199fSJeff Roberson 4763bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4764727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 47650095a784SJeff Roberson keg->uk_freef = freef; 47668355f576SJeff Roberson } 47678355f576SJeff Roberson 47688355f576SJeff Roberson /* See uma.h */ 47698355f576SJeff Roberson void 47708355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 47718355f576SJeff Roberson { 4772e20a199fSJeff Roberson uma_keg_t keg; 4773e20a199fSJeff Roberson 4774bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4775727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4776e20a199fSJeff Roberson keg->uk_allocf = allocf; 47778355f576SJeff Roberson } 47788355f576SJeff Roberson 47798355f576SJeff Roberson /* See uma.h */ 47806fd34d6fSJeff Roberson void 4781d4665eaaSJeff Roberson uma_zone_set_smr(uma_zone_t zone, smr_t smr) 4782d4665eaaSJeff Roberson { 4783d4665eaaSJeff Roberson 4784d4665eaaSJeff Roberson ZONE_ASSERT_COLD(zone); 4785d4665eaaSJeff Roberson 47867f746c9fSMateusz Guzik KASSERT(smr != NULL, ("Got NULL smr")); 47877f746c9fSMateusz Guzik KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0, 47887f746c9fSMateusz Guzik ("zone %p (%s) already uses SMR", zone, zone->uz_name)); 4789d4665eaaSJeff Roberson zone->uz_flags |= UMA_ZONE_SMR; 4790d4665eaaSJeff Roberson zone->uz_smr = smr; 4791d4665eaaSJeff Roberson zone_update_caches(zone); 4792d4665eaaSJeff Roberson } 4793d4665eaaSJeff Roberson 4794d4665eaaSJeff Roberson smr_t 4795d4665eaaSJeff Roberson uma_zone_get_smr(uma_zone_t zone) 4796d4665eaaSJeff Roberson { 4797d4665eaaSJeff Roberson 4798d4665eaaSJeff Roberson return (zone->uz_smr); 4799d4665eaaSJeff Roberson } 4800d4665eaaSJeff Roberson 4801d4665eaaSJeff Roberson /* See uma.h */ 4802d4665eaaSJeff Roberson void 48036fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 48046fd34d6fSJeff Roberson { 48056fd34d6fSJeff Roberson uma_keg_t keg; 48066fd34d6fSJeff Roberson 4807bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4808727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 48096fd34d6fSJeff Roberson keg->uk_reserve = items; 48106fd34d6fSJeff Roberson } 48116fd34d6fSJeff Roberson 48126fd34d6fSJeff Roberson /* See uma.h */ 48138355f576SJeff Roberson int 4814a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 48158355f576SJeff Roberson { 4816099a0e58SBosko Milekic uma_keg_t keg; 48178355f576SJeff Roberson vm_offset_t kva; 48189ba30bcbSZbigniew Bodek u_int pages; 48198355f576SJeff Roberson 4820bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4821727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4822727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 48238355f576SJeff Roberson 482479c9f942SJeff Roberson pages = howmany(count, keg->uk_ipers) * keg->uk_ppera; 4825a553d4b8SJeff Roberson 4826a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4827a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4828a4915c21SAttilio Rao #else 4829a4915c21SAttilio Rao if (1) { 4830a4915c21SAttilio Rao #endif 483157223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4832d1f42ac2SAlan Cox if (kva == 0) 48338355f576SJeff Roberson return (0); 4834a4915c21SAttilio Rao } else 4835a4915c21SAttilio Rao kva = 0; 4836bb15d1c7SGleb Smirnoff 4837bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4838099a0e58SBosko Milekic keg->uk_kva = kva; 4839a4915c21SAttilio Rao keg->uk_offset = 0; 4840bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4841a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4842a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4843a4915c21SAttilio Rao #else 4844a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4845a4915c21SAttilio Rao #endif 4846cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4847cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4848cc7ce83aSJeff Roberson zone_update_caches(zone); 4849af526374SJeff Roberson 48508355f576SJeff Roberson return (1); 48518355f576SJeff Roberson } 48528355f576SJeff Roberson 48538355f576SJeff Roberson /* See uma.h */ 48548355f576SJeff Roberson void 48558355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 48568355f576SJeff Roberson { 4857920239efSMark Johnston struct vm_domainset_iter di; 4858ab3185d1SJeff Roberson uma_domain_t dom; 48598355f576SJeff Roberson uma_slab_t slab; 4860099a0e58SBosko Milekic uma_keg_t keg; 486186220393SMark Johnston int aflags, domain, slabs; 48628355f576SJeff Roberson 4863bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 486479c9f942SJeff Roberson slabs = howmany(items, keg->uk_ipers); 4865194a979eSMark Johnston while (slabs-- > 0) { 486686220393SMark Johnston aflags = M_NOWAIT; 486786220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 486886220393SMark Johnston &aflags); 486986220393SMark Johnston for (;;) { 487086220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 487186220393SMark Johnston aflags); 487286220393SMark Johnston if (slab != NULL) { 4873ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 48744ab3aee8SMark Johnston /* 48754ab3aee8SMark Johnston * keg_alloc_slab() always returns a slab on the 48764ab3aee8SMark Johnston * partial list. 48774ab3aee8SMark Johnston */ 48788b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 487986220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 488086220393SMark Johnston us_link); 48814ab3aee8SMark Johnston dom->ud_free_slabs++; 48828b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 4883920239efSMark Johnston break; 48848355f576SJeff Roberson } 48858b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 488689d2fb14SKonstantin Belousov vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0); 488786220393SMark Johnston } 488886220393SMark Johnston } 488986220393SMark Johnston } 48908355f576SJeff Roberson 4891ed581bf6SJeff Roberson /* 4892ed581bf6SJeff Roberson * Returns a snapshot of memory consumption in bytes. 4893ed581bf6SJeff Roberson */ 4894ed581bf6SJeff Roberson size_t 4895ed581bf6SJeff Roberson uma_zone_memory(uma_zone_t zone) 4896ed581bf6SJeff Roberson { 4897ed581bf6SJeff Roberson size_t sz; 4898ed581bf6SJeff Roberson int i; 4899ed581bf6SJeff Roberson 4900ed581bf6SJeff Roberson sz = 0; 4901ed581bf6SJeff Roberson if (zone->uz_flags & UMA_ZFLAG_CACHE) { 4902ed581bf6SJeff Roberson for (i = 0; i < vm_ndomains; i++) 4903c6fd3e23SJeff Roberson sz += ZDOM_GET(zone, i)->uzd_nitems; 4904ed581bf6SJeff Roberson return (sz * zone->uz_size); 4905ed581bf6SJeff Roberson } 4906ed581bf6SJeff Roberson for (i = 0; i < vm_ndomains; i++) 4907ed581bf6SJeff Roberson sz += zone->uz_keg->uk_domain[i].ud_pages; 4908ed581bf6SJeff Roberson 4909ed581bf6SJeff Roberson return (sz * PAGE_SIZE); 4910ed581bf6SJeff Roberson } 4911ed581bf6SJeff Roberson 49128355f576SJeff Roberson /* See uma.h */ 491308cfa56eSMark Johnston void 491408cfa56eSMark Johnston uma_reclaim(int req) 49158355f576SJeff Roberson { 491644ec2b63SKonstantin Belousov 49171431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 491808cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 491986bbae32SJeff Roberson bucket_enable(); 492008cfa56eSMark Johnston 492108cfa56eSMark Johnston switch (req) { 492208cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 492320a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 492408cfa56eSMark Johnston break; 492508cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 492608cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 492720a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 492808cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 492908cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 493020a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4931a2de44abSAlexander Motin } 493208cfa56eSMark Johnston break; 493308cfa56eSMark Johnston default: 493408cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 493508cfa56eSMark Johnston } 49360f9b7bf3SMark Johnston 49378355f576SJeff Roberson /* 49388355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 49398355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 49408355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 49418355f576SJeff Roberson */ 49429b8db4d0SRyan Libby zone_drain(slabzones[0], NULL); 49439b8db4d0SRyan Libby zone_drain(slabzones[1], NULL); 4944cae33c14SJeff Roberson bucket_zone_drain(); 494508cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 49468355f576SJeff Roberson } 49478355f576SJeff Roberson 49482e47807cSJeff Roberson static volatile int uma_reclaim_needed; 494944ec2b63SKonstantin Belousov 495044ec2b63SKonstantin Belousov void 495144ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 495244ec2b63SKonstantin Belousov { 495344ec2b63SKonstantin Belousov 49542e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 49552e47807cSJeff Roberson wakeup(uma_reclaim); 495644ec2b63SKonstantin Belousov } 495744ec2b63SKonstantin Belousov 495844ec2b63SKonstantin Belousov void 495944ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 496044ec2b63SKonstantin Belousov { 496144ec2b63SKonstantin Belousov 496244ec2b63SKonstantin Belousov for (;;) { 496308cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4964200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 496508cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 49662e47807cSJeff Roberson hz); 496708cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 49689b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 496908cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4970200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 49712e47807cSJeff Roberson /* Don't fire more than once per-second. */ 49722e47807cSJeff Roberson pause("umarclslp", hz); 497344ec2b63SKonstantin Belousov } 497444ec2b63SKonstantin Belousov } 497544ec2b63SKonstantin Belousov 4976663b416fSJohn Baldwin /* See uma.h */ 497708cfa56eSMark Johnston void 497808cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 497908cfa56eSMark Johnston { 498008cfa56eSMark Johnston 498108cfa56eSMark Johnston switch (req) { 498208cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 498320a4e154SJeff Roberson zone_trim(zone, NULL); 498408cfa56eSMark Johnston break; 498508cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 498620a4e154SJeff Roberson zone_drain(zone, NULL); 498708cfa56eSMark Johnston break; 498808cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 498908cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 499020a4e154SJeff Roberson zone_drain(zone, NULL); 499108cfa56eSMark Johnston break; 499208cfa56eSMark Johnston default: 499308cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 499408cfa56eSMark Johnston } 499508cfa56eSMark Johnston } 499608cfa56eSMark Johnston 499708cfa56eSMark Johnston /* See uma.h */ 4998663b416fSJohn Baldwin int 4999663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 5000663b416fSJohn Baldwin { 5001663b416fSJohn Baldwin 5002727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 50036c125b8dSMohan Srinivasan } 50046c125b8dSMohan Srinivasan 50052e47807cSJeff Roberson unsigned long 50062e47807cSJeff Roberson uma_limit(void) 50072e47807cSJeff Roberson { 50082e47807cSJeff Roberson 50092e47807cSJeff Roberson return (uma_kmem_limit); 50102e47807cSJeff Roberson } 50112e47807cSJeff Roberson 50122e47807cSJeff Roberson void 50132e47807cSJeff Roberson uma_set_limit(unsigned long limit) 50142e47807cSJeff Roberson { 50152e47807cSJeff Roberson 50162e47807cSJeff Roberson uma_kmem_limit = limit; 50172e47807cSJeff Roberson } 50182e47807cSJeff Roberson 50192e47807cSJeff Roberson unsigned long 50202e47807cSJeff Roberson uma_size(void) 50212e47807cSJeff Roberson { 50222e47807cSJeff Roberson 5023058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 5024ad5b0f5bSJeff Roberson } 5025ad5b0f5bSJeff Roberson 5026ad5b0f5bSJeff Roberson long 5027ad5b0f5bSJeff Roberson uma_avail(void) 5028ad5b0f5bSJeff Roberson { 5029ad5b0f5bSJeff Roberson 5030058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 50312e47807cSJeff Roberson } 50322e47807cSJeff Roberson 5033a0d4b0aeSRobert Watson #ifdef DDB 50348355f576SJeff Roberson /* 50357a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 50367a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 50377a52a97eSRobert Watson * 50387a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 50397a52a97eSRobert Watson * per-CPU cache statistic. 50407a52a97eSRobert Watson * 50417a52a97eSRobert Watson */ 50427a52a97eSRobert Watson static void 50430f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 5044c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 50457a52a97eSRobert Watson { 50467a52a97eSRobert Watson uma_cache_t cache; 5047c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 50487a52a97eSRobert Watson int cachefree, cpu; 50497a52a97eSRobert Watson 5050c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 50517a52a97eSRobert Watson cachefree = 0; 50523aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 50537a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 5054376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 5055376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 5056376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 5057376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 50587a52a97eSRobert Watson allocs += cache->uc_allocs; 50597a52a97eSRobert Watson frees += cache->uc_frees; 50607a52a97eSRobert Watson } 50612efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 50622efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 5063c6fd3e23SJeff Roberson xdomain += counter_u64_fetch(z->uz_xdomain); 5064bf965959SSean Bruno sleeps += z->uz_sleeps; 50657a52a97eSRobert Watson if (cachefreep != NULL) 50667a52a97eSRobert Watson *cachefreep = cachefree; 50677a52a97eSRobert Watson if (allocsp != NULL) 50687a52a97eSRobert Watson *allocsp = allocs; 50697a52a97eSRobert Watson if (freesp != NULL) 50707a52a97eSRobert Watson *freesp = frees; 5071bf965959SSean Bruno if (sleepsp != NULL) 5072bf965959SSean Bruno *sleepsp = sleeps; 5073c1685086SJeff Roberson if (xdomainp != NULL) 5074c1685086SJeff Roberson *xdomainp = xdomain; 50757a52a97eSRobert Watson } 5076a0d4b0aeSRobert Watson #endif /* DDB */ 50777a52a97eSRobert Watson 50787a52a97eSRobert Watson static int 50797a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 50807a52a97eSRobert Watson { 50817a52a97eSRobert Watson uma_keg_t kz; 50827a52a97eSRobert Watson uma_zone_t z; 50837a52a97eSRobert Watson int count; 50847a52a97eSRobert Watson 50857a52a97eSRobert Watson count = 0; 5086111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 50877a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 50887a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 50897a52a97eSRobert Watson count++; 50907a52a97eSRobert Watson } 5091b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 5092b47acb0aSGleb Smirnoff count++; 5093b47acb0aSGleb Smirnoff 5094111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 50957a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 50967a52a97eSRobert Watson } 50977a52a97eSRobert Watson 5098b47acb0aSGleb Smirnoff static void 5099b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 5100b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 5101b47acb0aSGleb Smirnoff { 5102b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 5103b47acb0aSGleb Smirnoff uma_cache_t cache; 5104b47acb0aSGleb Smirnoff int i; 5105b47acb0aSGleb Smirnoff 5106b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 5107c6fd3e23SJeff Roberson zdom = ZDOM_GET(z, i); 5108b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 5109b47acb0aSGleb Smirnoff } 5110b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 5111b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 5112b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 5113c6fd3e23SJeff Roberson uth->uth_xdomain = counter_u64_fetch(z->uz_xdomain); 5114b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 51151de9724eSMark Johnston 5116b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 5117b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 5118b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 5119b47acb0aSGleb Smirnoff continue; 5120b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 5121376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 5122376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 5123376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 5124b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 5125b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 5126b47acb0aSGleb Smirnoff } 5127b47acb0aSGleb Smirnoff } 5128b47acb0aSGleb Smirnoff 51297a52a97eSRobert Watson static int 51307a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 51317a52a97eSRobert Watson { 51327a52a97eSRobert Watson struct uma_stream_header ush; 51337a52a97eSRobert Watson struct uma_type_header uth; 513463b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 51357a52a97eSRobert Watson struct sbuf sbuf; 51367a52a97eSRobert Watson uma_keg_t kz; 51377a52a97eSRobert Watson uma_zone_t z; 51384bd61e19SJeff Roberson uint64_t items; 51398b987a77SJeff Roberson uint32_t kfree, pages; 51404e657159SMatthew D Fleming int count, error, i; 51417a52a97eSRobert Watson 514200f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 514300f0e671SMatthew D Fleming if (error != 0) 514400f0e671SMatthew D Fleming return (error); 51454e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 51461eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 514763b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 51484e657159SMatthew D Fleming 5149404a593eSMatthew D Fleming count = 0; 5150111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 51517a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 51527a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 51537a52a97eSRobert Watson count++; 51547a52a97eSRobert Watson } 51557a52a97eSRobert Watson 5156b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 5157b47acb0aSGleb Smirnoff count++; 5158b47acb0aSGleb Smirnoff 51597a52a97eSRobert Watson /* 51607a52a97eSRobert Watson * Insert stream header. 51617a52a97eSRobert Watson */ 51627a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 51637a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 5164ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 51657a52a97eSRobert Watson ush.ush_count = count; 51664e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 51677a52a97eSRobert Watson 51687a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 51698b987a77SJeff Roberson kfree = pages = 0; 51708b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 51714ab3aee8SMark Johnston kfree += kz->uk_domain[i].ud_free_items; 51728b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 51738b987a77SJeff Roberson } 51747a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 51757a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 5176cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 51777a52a97eSRobert Watson uth.uth_align = kz->uk_align; 51787a52a97eSRobert Watson uth.uth_size = kz->uk_size; 51797a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 51804bd61e19SJeff Roberson if (z->uz_max_items > 0) { 51814bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 51824bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 5183bb15d1c7SGleb Smirnoff kz->uk_ppera; 51844bd61e19SJeff Roberson } else 51858b987a77SJeff Roberson uth.uth_pages = pages; 5186f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 5187bb15d1c7SGleb Smirnoff kz->uk_ppera; 5188bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 51898b987a77SJeff Roberson uth.uth_keg_free = kfree; 5190cbbb4a00SRobert Watson 5191cbbb4a00SRobert Watson /* 5192cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 5193cbbb4a00SRobert Watson * on the keg's zone list. 5194cbbb4a00SRobert Watson */ 5195e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 5196cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 5197cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 5198b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 5199b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 520063b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 520163b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 520263b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 52037a52a97eSRobert Watson } 52047a52a97eSRobert Watson } 5205b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 5206b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 5207b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 5208b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 5209b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 5210b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 5211b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 5212b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 5213b47acb0aSGleb Smirnoff } 5214b47acb0aSGleb Smirnoff 5215111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 52164e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 52174e657159SMatthew D Fleming sbuf_delete(&sbuf); 521863b5d112SKonstantin Belousov free(ups, M_TEMP); 52197a52a97eSRobert Watson return (error); 52207a52a97eSRobert Watson } 522148c5777eSRobert Watson 52220a5a3ccbSGleb Smirnoff int 52230a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 52240a5a3ccbSGleb Smirnoff { 52250a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 522616be9f54SGleb Smirnoff int error, max; 52270a5a3ccbSGleb Smirnoff 522816be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 52290a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 52300a5a3ccbSGleb Smirnoff if (error || !req->newptr) 52310a5a3ccbSGleb Smirnoff return (error); 52320a5a3ccbSGleb Smirnoff 52330a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 52340a5a3ccbSGleb Smirnoff 52350a5a3ccbSGleb Smirnoff return (0); 52360a5a3ccbSGleb Smirnoff } 52370a5a3ccbSGleb Smirnoff 52380a5a3ccbSGleb Smirnoff int 52390a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 52400a5a3ccbSGleb Smirnoff { 524120a4e154SJeff Roberson uma_zone_t zone; 52420a5a3ccbSGleb Smirnoff int cur; 52430a5a3ccbSGleb Smirnoff 524420a4e154SJeff Roberson /* 524520a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 524620a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 524720a4e154SJeff Roberson */ 524820a4e154SJeff Roberson if (arg2 == 0) 524920a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 525020a4e154SJeff Roberson else 525120a4e154SJeff Roberson zone = arg1; 52520a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 52530a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 52540a5a3ccbSGleb Smirnoff } 52550a5a3ccbSGleb Smirnoff 525620a4e154SJeff Roberson static int 525720a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 525820a4e154SJeff Roberson { 525920a4e154SJeff Roberson uma_zone_t zone = arg1; 526020a4e154SJeff Roberson uint64_t cur; 526120a4e154SJeff Roberson 526220a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 526320a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 526420a4e154SJeff Roberson } 526520a4e154SJeff Roberson 526620a4e154SJeff Roberson static int 526720a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 526820a4e154SJeff Roberson { 526920a4e154SJeff Roberson uma_zone_t zone = arg1; 527020a4e154SJeff Roberson uint64_t cur; 527120a4e154SJeff Roberson 527220a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 527320a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 527420a4e154SJeff Roberson } 527520a4e154SJeff Roberson 52766d204a6aSRyan Libby static int 52776d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 52786d204a6aSRyan Libby { 52796d204a6aSRyan Libby struct sbuf sbuf; 52806d204a6aSRyan Libby uma_zone_t zone = arg1; 52816d204a6aSRyan Libby int error; 52826d204a6aSRyan Libby 52836d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 52846d204a6aSRyan Libby if (zone->uz_flags != 0) 52856d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 52866d204a6aSRyan Libby else 52876d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 52886d204a6aSRyan Libby error = sbuf_finish(&sbuf); 52896d204a6aSRyan Libby sbuf_delete(&sbuf); 52906d204a6aSRyan Libby 52916d204a6aSRyan Libby return (error); 52926d204a6aSRyan Libby } 52936d204a6aSRyan Libby 5294f7af5015SRyan Libby static int 5295f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 5296f7af5015SRyan Libby { 5297f7af5015SRyan Libby uma_keg_t keg = arg1; 5298f7af5015SRyan Libby int avail, effpct, total; 5299f7af5015SRyan Libby 5300f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 530154c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 53029b8db4d0SRyan Libby total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize; 5303f7af5015SRyan Libby /* 5304f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 5305f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 5306f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 5307f7af5015SRyan Libby */ 5308f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 5309f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 5310f7af5015SRyan Libby avail *= mp_maxid + 1; 5311f7af5015SRyan Libby effpct = 100 * avail / total; 5312f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 5313f7af5015SRyan Libby } 5314f7af5015SRyan Libby 53154bd61e19SJeff Roberson static int 53164bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 53174bd61e19SJeff Roberson { 53184bd61e19SJeff Roberson uma_zone_t zone = arg1; 53194bd61e19SJeff Roberson uint64_t cur; 53204bd61e19SJeff Roberson 53214bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 53224bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 53234bd61e19SJeff Roberson } 53244bd61e19SJeff Roberson 53259542ea7bSGleb Smirnoff #ifdef INVARIANTS 53269542ea7bSGleb Smirnoff static uma_slab_t 53279542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 53289542ea7bSGleb Smirnoff { 53299542ea7bSGleb Smirnoff uma_slab_t slab; 53309542ea7bSGleb Smirnoff uma_keg_t keg; 53319542ea7bSGleb Smirnoff uint8_t *mem; 53329542ea7bSGleb Smirnoff 53339542ea7bSGleb Smirnoff /* 53349542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 53359542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 53369542ea7bSGleb Smirnoff * essentially holds a reference. 53379542ea7bSGleb Smirnoff */ 5338727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 5339727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5340bb15d1c7SGleb Smirnoff return (NULL); 534154c5ae80SRyan Libby if (zone->uz_flags & UMA_ZFLAG_VTOSLAB) 5342727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 5343bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 534454c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0) 5345727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 53468b987a77SJeff Roberson KEG_LOCK(keg, 0); 53479542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 53488b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 53499542ea7bSGleb Smirnoff 53509542ea7bSGleb Smirnoff return (slab); 53519542ea7bSGleb Smirnoff } 53529542ea7bSGleb Smirnoff 5353c5deaf04SGleb Smirnoff static bool 5354c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 5355c5deaf04SGleb Smirnoff { 5356c5deaf04SGleb Smirnoff 5357727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 5358c5deaf04SGleb Smirnoff return (true); 5359c5deaf04SGleb Smirnoff 5360bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 5361c5deaf04SGleb Smirnoff } 5362c5deaf04SGleb Smirnoff 5363c5deaf04SGleb Smirnoff static bool 5364c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 5365c5deaf04SGleb Smirnoff { 5366c5deaf04SGleb Smirnoff uintptr_t idx; 5367c5deaf04SGleb Smirnoff 5368c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 5369c5deaf04SGleb Smirnoff return (true); 5370c5deaf04SGleb Smirnoff 5371c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 5372c5deaf04SGleb Smirnoff return (false); 5373c5deaf04SGleb Smirnoff 5374c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 5375c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 5376c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 5377c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 5378c5deaf04SGleb Smirnoff } 5379c5deaf04SGleb Smirnoff 5380c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 5381c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 5382c5deaf04SGleb Smirnoff return (true); 5383c5deaf04SGleb Smirnoff } 5384c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 5385c5deaf04SGleb Smirnoff 5386c5deaf04SGleb Smirnoff return (false); 5387c5deaf04SGleb Smirnoff } 5388c5deaf04SGleb Smirnoff 53899542ea7bSGleb Smirnoff /* 53909542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 53919542ea7bSGleb Smirnoff * 53929542ea7bSGleb Smirnoff */ 53939542ea7bSGleb Smirnoff static void 53949542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 53959542ea7bSGleb Smirnoff { 53969542ea7bSGleb Smirnoff uma_keg_t keg; 53979542ea7bSGleb Smirnoff int freei; 53989542ea7bSGleb Smirnoff 53999542ea7bSGleb Smirnoff if (slab == NULL) { 54009542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 54019542ea7bSGleb Smirnoff if (slab == NULL) 5402952c8964SMark Johnston panic("uma: item %p did not belong to zone %s", 54039542ea7bSGleb Smirnoff item, zone->uz_name); 54049542ea7bSGleb Smirnoff } 5405584061b4SJeff Roberson keg = zone->uz_keg; 54061e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 54079542ea7bSGleb Smirnoff 5408815db204SRyan Libby if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 5409952c8964SMark Johnston panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)", 54109542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 5411815db204SRyan Libby BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 54129542ea7bSGleb Smirnoff } 54139542ea7bSGleb Smirnoff 54149542ea7bSGleb Smirnoff /* 54159542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 54169542ea7bSGleb Smirnoff * and duplicate frees. 54179542ea7bSGleb Smirnoff * 54189542ea7bSGleb Smirnoff */ 54199542ea7bSGleb Smirnoff static void 54209542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 54219542ea7bSGleb Smirnoff { 54229542ea7bSGleb Smirnoff uma_keg_t keg; 54239542ea7bSGleb Smirnoff int freei; 54249542ea7bSGleb Smirnoff 54259542ea7bSGleb Smirnoff if (slab == NULL) { 54269542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 54279542ea7bSGleb Smirnoff if (slab == NULL) 5428952c8964SMark Johnston panic("uma: Freed item %p did not belong to zone %s", 54299542ea7bSGleb Smirnoff item, zone->uz_name); 54309542ea7bSGleb Smirnoff } 5431584061b4SJeff Roberson keg = zone->uz_keg; 54321e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 54339542ea7bSGleb Smirnoff 54349542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 5435952c8964SMark Johnston panic("Invalid free of %p from zone %p(%s) slab %p(%d)", 54369542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 54379542ea7bSGleb Smirnoff 54381e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 5439952c8964SMark Johnston panic("Unaligned free of %p from zone %p(%s) slab %p(%d)", 54409542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 54419542ea7bSGleb Smirnoff 5442815db204SRyan Libby if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 5443952c8964SMark Johnston panic("Duplicate free of %p from zone %p(%s) slab %p(%d)", 54449542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 54459542ea7bSGleb Smirnoff 5446815db204SRyan Libby BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 54479542ea7bSGleb Smirnoff } 54489542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 54499542ea7bSGleb Smirnoff 545048c5777eSRobert Watson #ifdef DDB 545146d70077SConrad Meyer static int64_t 545246d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 54530223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 545448c5777eSRobert Watson { 545546d70077SConrad Meyer uint64_t frees; 54560f9b7bf3SMark Johnston int i; 545748c5777eSRobert Watson 545848c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 545946d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 54602efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 546146d70077SConrad Meyer *sleeps = z->uz_sleeps; 546246d70077SConrad Meyer *cachefree = 0; 546346d70077SConrad Meyer *xdomain = 0; 546448c5777eSRobert Watson } else 546546d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 546646d70077SConrad Meyer xdomain); 54678b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 5468c6fd3e23SJeff Roberson *cachefree += ZDOM_GET(z, i)->uzd_nitems; 5469e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 547048c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 54714ab3aee8SMark Johnston *cachefree += kz->uk_domain[i].ud_free_items; 54728b987a77SJeff Roberson } 547346d70077SConrad Meyer *used = *allocs - frees; 547446d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 547546d70077SConrad Meyer } 54760f9b7bf3SMark Johnston 547746d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 547846d70077SConrad Meyer { 547946d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 548046d70077SConrad Meyer uma_keg_t kz; 548146d70077SConrad Meyer uma_zone_t z; 548246d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 548346d70077SConrad Meyer long cachefree; 548446d70077SConrad Meyer /* variables for sorting */ 548546d70077SConrad Meyer uma_keg_t cur_keg; 548646d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 548746d70077SConrad Meyer int64_t cur_size, last_size, size; 548846d70077SConrad Meyer int ties; 548946d70077SConrad Meyer 549046d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 549146d70077SConrad Meyer if (modif[0] == 'i') { 549246d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 549346d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 549446d70077SConrad Meyer } else { 549546d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 549646d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 549746d70077SConrad Meyer } 549846d70077SConrad Meyer 549946d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 550046d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 550146d70077SConrad Meyer 550246d70077SConrad Meyer /* Sort the zones with largest size first. */ 550346d70077SConrad Meyer last_zone = NULL; 550446d70077SConrad Meyer last_size = INT64_MAX; 550546d70077SConrad Meyer for (;;) { 550646d70077SConrad Meyer cur_zone = NULL; 550746d70077SConrad Meyer cur_size = -1; 550846d70077SConrad Meyer ties = 0; 550946d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 551046d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 551146d70077SConrad Meyer /* 551246d70077SConrad Meyer * In the case of size ties, print out zones 551346d70077SConrad Meyer * in the order they are encountered. That is, 551446d70077SConrad Meyer * when we encounter the most recently output 551546d70077SConrad Meyer * zone, we have already printed all preceding 551646d70077SConrad Meyer * ties, and we must print all following ties. 551746d70077SConrad Meyer */ 551846d70077SConrad Meyer if (z == last_zone) { 551946d70077SConrad Meyer ties = 1; 552046d70077SConrad Meyer continue; 552146d70077SConrad Meyer } 552246d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 552346d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 552446d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 552546d70077SConrad Meyer { 552646d70077SConrad Meyer cur_size = size; 552746d70077SConrad Meyer cur_zone = z; 552846d70077SConrad Meyer cur_keg = kz; 552946d70077SConrad Meyer } 553046d70077SConrad Meyer } 553146d70077SConrad Meyer } 553246d70077SConrad Meyer if (cur_zone == NULL) 553346d70077SConrad Meyer break; 553446d70077SConrad Meyer 553546d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 553646d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 553746d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 553846d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 553946d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 554020a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 554120a4e154SJeff Roberson xdomain); 554246d70077SConrad Meyer 5543687c94aaSJohn Baldwin if (db_pager_quit) 5544687c94aaSJohn Baldwin return; 554546d70077SConrad Meyer last_zone = cur_zone; 554646d70077SConrad Meyer last_size = cur_size; 554748c5777eSRobert Watson } 554848c5777eSRobert Watson } 554903175483SAlexander Motin 555003175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 555103175483SAlexander Motin { 555203175483SAlexander Motin uma_zone_t z; 5553ab3185d1SJeff Roberson uint64_t allocs, frees; 55540f9b7bf3SMark Johnston long cachefree; 55550f9b7bf3SMark Johnston int i; 555603175483SAlexander Motin 555703175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 555803175483SAlexander Motin "Requests", "Bucket"); 555903175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 5560c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 55610f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 5562c6fd3e23SJeff Roberson cachefree += ZDOM_GET(z, i)->uzd_nitems; 55630f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 556403175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 556503175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 556620a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 556703175483SAlexander Motin if (db_pager_quit) 556803175483SAlexander Motin return; 556903175483SAlexander Motin } 557003175483SAlexander Motin } 55719542ea7bSGleb Smirnoff #endif /* DDB */ 5572