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> 80e60b2fcbSGleb Smirnoff #include <sys/taskqueue.h> 8186bbae32SJeff Roberson #include <sys/vmmeter.h> 8286bbae32SJeff Roberson 838355f576SJeff Roberson #include <vm/vm.h> 84194a979eSMark Johnston #include <vm/vm_domainset.h> 858355f576SJeff Roberson #include <vm/vm_object.h> 868355f576SJeff Roberson #include <vm/vm_page.h> 87a4915c21SAttilio Rao #include <vm/vm_pageout.h> 888355f576SJeff Roberson #include <vm/vm_param.h> 89ab3185d1SJeff Roberson #include <vm/vm_phys.h> 9030c5525bSAndrew Gallatin #include <vm/vm_pagequeue.h> 918355f576SJeff Roberson #include <vm/vm_map.h> 928355f576SJeff Roberson #include <vm/vm_kern.h> 938355f576SJeff Roberson #include <vm/vm_extern.h> 948355f576SJeff Roberson #include <vm/uma.h> 958355f576SJeff Roberson #include <vm/uma_int.h> 96639c9550SJeff Roberson #include <vm/uma_dbg.h> 978355f576SJeff Roberson 9848c5777eSRobert Watson #include <ddb/ddb.h> 9948c5777eSRobert Watson 1008d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 1018d689e04SGleb Smirnoff #include <vm/memguard.h> 1028d689e04SGleb Smirnoff #endif 1038d689e04SGleb Smirnoff 104*a81c400eSJeff Roberson #include <machine/md_var.h> 105*a81c400eSJeff Roberson 1068355f576SJeff Roberson /* 107ab3185d1SJeff Roberson * This is the zone and keg from which all zones are spawned. 1088355f576SJeff Roberson */ 109ab3185d1SJeff Roberson static uma_zone_t kegs; 110ab3185d1SJeff Roberson static uma_zone_t zones; 1118355f576SJeff Roberson 1129b8db4d0SRyan Libby /* 1139b8db4d0SRyan Libby * These are the two zones from which all offpage uma_slab_ts are allocated. 1149b8db4d0SRyan Libby * 1159b8db4d0SRyan Libby * One zone is for slab headers that can represent a larger number of items, 1169b8db4d0SRyan Libby * making the slabs themselves more efficient, and the other zone is for 1179b8db4d0SRyan Libby * headers that are smaller and represent fewer items, making the headers more 1189b8db4d0SRyan Libby * efficient. 1199b8db4d0SRyan Libby */ 1209b8db4d0SRyan Libby #define SLABZONE_SIZE(setsize) \ 1219b8db4d0SRyan Libby (sizeof(struct uma_hash_slab) + BITSET_SIZE(setsize) * SLAB_BITSETS) 1229b8db4d0SRyan Libby #define SLABZONE0_SETSIZE (PAGE_SIZE / 16) 1239b8db4d0SRyan Libby #define SLABZONE1_SETSIZE SLAB_MAX_SETSIZE 1249b8db4d0SRyan Libby #define SLABZONE0_SIZE SLABZONE_SIZE(SLABZONE0_SETSIZE) 1259b8db4d0SRyan Libby #define SLABZONE1_SIZE SLABZONE_SIZE(SLABZONE1_SETSIZE) 1269b8db4d0SRyan Libby static uma_zone_t slabzones[2]; 1278355f576SJeff Roberson 1288355f576SJeff Roberson /* 1298355f576SJeff Roberson * The initial hash tables come out of this zone so they can be allocated 1308355f576SJeff Roberson * prior to malloc coming up. 1318355f576SJeff Roberson */ 1328355f576SJeff Roberson static uma_zone_t hashzone; 1338355f576SJeff Roberson 1341e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */ 135e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1; 1361e319f6dSRobert Watson 137961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets"); 13820a4e154SJeff Roberson static MALLOC_DEFINE(M_UMA, "UMA", "UMA Misc"); 139961647dfSJeff Roberson 1408355f576SJeff Roberson /* 14186bbae32SJeff Roberson * Are we allowed to allocate buckets? 14286bbae32SJeff Roberson */ 14386bbae32SJeff Roberson static int bucketdisable = 1; 14486bbae32SJeff Roberson 145099a0e58SBosko Milekic /* Linked list of all kegs in the system */ 14613e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs); 1478355f576SJeff Roberson 14803175483SAlexander Motin /* Linked list of all cache-only zones in the system */ 14903175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones = 15003175483SAlexander Motin LIST_HEAD_INITIALIZER(uma_cachezones); 15103175483SAlexander Motin 152111fbcd5SBryan Venteicher /* This RW lock protects the keg list */ 153fe933c1dSMateusz Guzik static struct rwlock_padalign __exclusive_cache_line uma_rwlock; 1548355f576SJeff Roberson 155ac0a6fd0SGleb Smirnoff /* 156*a81c400eSJeff Roberson * First available virual address for boot time allocations. 157ac0a6fd0SGleb Smirnoff */ 158*a81c400eSJeff Roberson static vm_offset_t bootstart; 159*a81c400eSJeff Roberson static vm_offset_t bootmem; 1608355f576SJeff Roberson 16108cfa56eSMark Johnston static struct sx uma_reclaim_lock; 16295c4bf75SKonstantin Belousov 163fbd95859SMark Johnston /* 164fbd95859SMark Johnston * kmem soft limit, initialized by uma_set_limit(). Ensure that early 165fbd95859SMark Johnston * allocations don't trigger a wakeup of the reclaim thread. 166fbd95859SMark Johnston */ 1676d6a03d7SJeff Roberson unsigned long uma_kmem_limit = LONG_MAX; 168fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_limit, CTLFLAG_RD, &uma_kmem_limit, 0, 169fbd95859SMark Johnston "UMA kernel memory soft limit"); 1706d6a03d7SJeff Roberson unsigned long uma_kmem_total; 171fbd95859SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_total, CTLFLAG_RD, &uma_kmem_total, 0, 172fbd95859SMark Johnston "UMA kernel memory usage"); 1732e47807cSJeff Roberson 1748355f576SJeff Roberson /* Is the VM done starting up? */ 175860bb7a0SMark Johnston static enum { 176860bb7a0SMark Johnston BOOT_COLD, 177*a81c400eSJeff Roberson BOOT_KVA, 178860bb7a0SMark Johnston BOOT_RUNNING, 179860bb7a0SMark Johnston BOOT_SHUTDOWN, 180860bb7a0SMark Johnston } booted = BOOT_COLD; 1818355f576SJeff Roberson 182ef72505eSJeff Roberson /* 1839643769aSJeff Roberson * This is the handle used to schedule events that need to happen 1849643769aSJeff Roberson * outside of the allocation fast path. 1859643769aSJeff Roberson */ 1868355f576SJeff Roberson static struct callout uma_callout; 1879643769aSJeff Roberson #define UMA_TIMEOUT 20 /* Seconds for callout interval. */ 1888355f576SJeff Roberson 1898355f576SJeff Roberson /* 1908355f576SJeff Roberson * This structure is passed as the zone ctor arg so that I don't have to create 1918355f576SJeff Roberson * a special allocation function just for zones. 1928355f576SJeff Roberson */ 1938355f576SJeff Roberson struct uma_zctor_args { 194bb196eb4SMatthew D Fleming const char *name; 195c3bdc05fSAndrew R. Reiter size_t size; 1968355f576SJeff Roberson uma_ctor ctor; 1978355f576SJeff Roberson uma_dtor dtor; 1988355f576SJeff Roberson uma_init uminit; 1998355f576SJeff Roberson uma_fini fini; 2000095a784SJeff Roberson uma_import import; 2010095a784SJeff Roberson uma_release release; 2020095a784SJeff Roberson void *arg; 203099a0e58SBosko Milekic uma_keg_t keg; 204099a0e58SBosko Milekic int align; 20585dcf349SGleb Smirnoff uint32_t flags; 206099a0e58SBosko Milekic }; 207099a0e58SBosko Milekic 208099a0e58SBosko Milekic struct uma_kctor_args { 209099a0e58SBosko Milekic uma_zone_t zone; 210099a0e58SBosko Milekic size_t size; 211099a0e58SBosko Milekic uma_init uminit; 212099a0e58SBosko Milekic uma_fini fini; 2138355f576SJeff Roberson int align; 21485dcf349SGleb Smirnoff uint32_t flags; 2158355f576SJeff Roberson }; 2168355f576SJeff Roberson 217cae33c14SJeff Roberson struct uma_bucket_zone { 218cae33c14SJeff Roberson uma_zone_t ubz_zone; 219cae33c14SJeff Roberson char *ubz_name; 220fc03d22bSJeff Roberson int ubz_entries; /* Number of items it can hold. */ 221fc03d22bSJeff Roberson int ubz_maxsize; /* Maximum allocation size per-item. */ 222cae33c14SJeff Roberson }; 223cae33c14SJeff Roberson 224f9d27e75SRobert Watson /* 225fc03d22bSJeff Roberson * Compute the actual number of bucket entries to pack them in power 226fc03d22bSJeff Roberson * of two sizes for more efficient space utilization. 227f9d27e75SRobert Watson */ 228fc03d22bSJeff Roberson #define BUCKET_SIZE(n) \ 229fc03d22bSJeff Roberson (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *)) 230fc03d22bSJeff Roberson 2311aa6c758SAlexander Motin #define BUCKET_MAX BUCKET_SIZE(256) 232eda1b016SJeff Roberson #define BUCKET_MIN BUCKET_SIZE(4) 233fc03d22bSJeff Roberson 234fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = { 2356fd34d6fSJeff Roberson { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 }, 236f3932e90SAlexander Motin { NULL, "6 Bucket", BUCKET_SIZE(6), 3072 }, 2376fd34d6fSJeff Roberson { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 }, 238f3932e90SAlexander Motin { NULL, "12 Bucket", BUCKET_SIZE(12), 1536 }, 2396fd34d6fSJeff Roberson { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 }, 240fc03d22bSJeff Roberson { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 241fc03d22bSJeff Roberson { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 242fc03d22bSJeff Roberson { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 2431aa6c758SAlexander Motin { NULL, "256 Bucket", BUCKET_SIZE(256), 64 }, 244fc03d22bSJeff Roberson { NULL, NULL, 0} 245fc03d22bSJeff Roberson }; 246cae33c14SJeff Roberson 2472019094aSRobert Watson /* 2482019094aSRobert Watson * Flags and enumerations to be passed to internal functions. 2492019094aSRobert Watson */ 250bb15d1c7SGleb Smirnoff enum zfreeskip { 251bb15d1c7SGleb Smirnoff SKIP_NONE = 0, 252bb15d1c7SGleb Smirnoff SKIP_CNT = 0x00000001, 253bb15d1c7SGleb Smirnoff SKIP_DTOR = 0x00010000, 254bb15d1c7SGleb Smirnoff SKIP_FINI = 0x00020000, 255bb15d1c7SGleb Smirnoff }; 256b23f72e9SBrian Feldman 2578355f576SJeff Roberson /* Prototypes.. */ 2588355f576SJeff Roberson 259*a81c400eSJeff Roberson void uma_startup1(vm_offset_t); 260f4bef67cSGleb Smirnoff void uma_startup2(void); 261f4bef67cSGleb Smirnoff 262ab3185d1SJeff Roberson static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 263ab3185d1SJeff Roberson static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 264ab3059a8SMatt Macy static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 265ab3185d1SJeff Roberson static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 266f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t); 267ab3059a8SMatt Macy static void pcpu_page_free(void *, vm_size_t, uint8_t); 26886220393SMark Johnston static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int); 2699643769aSJeff Roberson static void cache_drain(uma_zone_t); 2708355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t); 27108cfa56eSMark Johnston static void bucket_cache_reclaim(uma_zone_t zone, bool); 272b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int); 273099a0e58SBosko Milekic static void keg_dtor(void *, int, void *); 274b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int); 2759c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *); 276b23f72e9SBrian Feldman static int zero_init(void *, int, int); 27720a4e154SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t, void *), void *); 278*a81c400eSJeff Roberson static void zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *), void *); 27920a4e154SJeff Roberson static void zone_timeout(uma_zone_t zone, void *); 2803b2f2cb8SAlexander Motin static int hash_alloc(struct uma_hash *, u_int); 2810aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *); 2820aef6126SJeff Roberson static void hash_free(struct uma_hash *hash); 2838355f576SJeff Roberson static void uma_timeout(void *); 2848355f576SJeff Roberson static void uma_startup3(void); 285860bb7a0SMark Johnston static void uma_shutdown(void); 286ab3185d1SJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int, int); 2870095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 2884bd61e19SJeff Roberson static int zone_alloc_limit(uma_zone_t zone, int count, int flags); 2894bd61e19SJeff Roberson static void zone_free_limit(uma_zone_t zone, int count); 29086bbae32SJeff Roberson static void bucket_enable(void); 291cae33c14SJeff Roberson static void bucket_init(void); 2926fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int); 2936fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *); 294cae33c14SJeff Roberson static void bucket_zone_drain(void); 295beb8beefSJeff Roberson static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int); 2960095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 297bb15d1c7SGleb Smirnoff static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item); 298e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 29985dcf349SGleb Smirnoff uma_fini fini, int align, uint32_t flags); 300b75c4efcSAndrew Turner static int zone_import(void *, void **, int, int, int); 301b75c4efcSAndrew Turner static void zone_release(void *, void **, int); 302beb8beefSJeff Roberson static bool cache_alloc(uma_zone_t, uma_cache_t, void *, int); 3030a81b439SJeff Roberson static bool cache_free(uma_zone_t, uma_cache_t, void *, void *, int); 304bbee39c6SJeff Roberson 3057a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 3067a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 30720a4e154SJeff Roberson static int sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS); 30820a4e154SJeff Roberson static int sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS); 3096d204a6aSRyan Libby static int sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS); 310f7af5015SRyan Libby static int sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS); 3114bd61e19SJeff Roberson static int sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS); 3128355f576SJeff Roberson 31331c251a0SJeff Roberson static uint64_t uma_zone_get_allocs(uma_zone_t zone); 31431c251a0SJeff Roberson 3159542ea7bSGleb Smirnoff #ifdef INVARIANTS 31631c251a0SJeff Roberson static uint64_t uma_keg_get_allocs(uma_keg_t zone); 317815db204SRyan Libby static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg); 318815db204SRyan Libby 319c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem); 320c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem); 3219542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item); 3229542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item); 323c5deaf04SGleb Smirnoff 324c5deaf04SGleb Smirnoff static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0, 325c5deaf04SGleb Smirnoff "Memory allocation debugging"); 326c5deaf04SGleb Smirnoff 327c5deaf04SGleb Smirnoff static u_int dbg_divisor = 1; 328c5deaf04SGleb Smirnoff SYSCTL_UINT(_vm_debug, OID_AUTO, divisor, 329c5deaf04SGleb Smirnoff CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0, 330c5deaf04SGleb Smirnoff "Debug & thrash every this item in memory allocator"); 331c5deaf04SGleb Smirnoff 332c5deaf04SGleb Smirnoff static counter_u64_t uma_dbg_cnt = EARLY_COUNTER; 333c5deaf04SGleb Smirnoff static counter_u64_t uma_skip_cnt = EARLY_COUNTER; 334c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD, 335c5deaf04SGleb Smirnoff &uma_dbg_cnt, "memory items debugged"); 336c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD, 337c5deaf04SGleb Smirnoff &uma_skip_cnt, "memory items skipped, not debugged"); 3389542ea7bSGleb Smirnoff #endif 3399542ea7bSGleb Smirnoff 3408355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 3418355f576SJeff Roberson 34235ec24f3SRyan Libby SYSCTL_NODE(_vm, OID_AUTO, uma, CTLFLAG_RW, 0, "Universal Memory Allocator"); 34335ec24f3SRyan Libby 344a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_INT, 3457a52a97eSRobert Watson 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 3467a52a97eSRobert Watson 347a314aba8SMateusz Guzik SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_STRUCT, 3487a52a97eSRobert Watson 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 3497a52a97eSRobert Watson 3502f891cd5SPawel Jakub Dawidek static int zone_warnings = 1; 351af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0, 3522f891cd5SPawel Jakub Dawidek "Warn when UMA zones becomes full"); 3532f891cd5SPawel Jakub Dawidek 35486bbae32SJeff Roberson /* 3559b8db4d0SRyan Libby * Select the slab zone for an offpage slab with the given maximum item count. 3569b8db4d0SRyan Libby */ 3579b8db4d0SRyan Libby static inline uma_zone_t 3589b8db4d0SRyan Libby slabzone(int ipers) 3599b8db4d0SRyan Libby { 3609b8db4d0SRyan Libby 3619b8db4d0SRyan Libby return (slabzones[ipers > SLABZONE0_SETSIZE]); 3629b8db4d0SRyan Libby } 3639b8db4d0SRyan Libby 3649b8db4d0SRyan Libby /* 36586bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 36686bbae32SJeff Roberson */ 36786bbae32SJeff Roberson static void 36886bbae32SJeff Roberson bucket_enable(void) 36986bbae32SJeff Roberson { 3703182660aSRyan Libby 371*a81c400eSJeff Roberson KASSERT(booted >= BOOT_KVA, ("Bucket enable before init")); 372251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 37386bbae32SJeff Roberson } 37486bbae32SJeff Roberson 375dc2c7965SRobert Watson /* 376dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 377dc2c7965SRobert Watson * 378dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 379fc03d22bSJeff Roberson * of the header and an array of pointers. 380dc2c7965SRobert Watson */ 381cae33c14SJeff Roberson static void 382cae33c14SJeff Roberson bucket_init(void) 383cae33c14SJeff Roberson { 384cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 385cae33c14SJeff Roberson int size; 386cae33c14SJeff Roberson 387d74e6a1dSAlan Cox for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 388cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 389cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 390cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 391e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 392dfe13344SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | 393dfe13344SJeff Roberson UMA_ZONE_FIRSTTOUCH); 394cae33c14SJeff Roberson } 395cae33c14SJeff Roberson } 396cae33c14SJeff Roberson 397dc2c7965SRobert Watson /* 398dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 399dc2c7965SRobert Watson * to allocate the bucket. 400dc2c7965SRobert Watson */ 401dc2c7965SRobert Watson static struct uma_bucket_zone * 402dc2c7965SRobert Watson bucket_zone_lookup(int entries) 403dc2c7965SRobert Watson { 404fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 405dc2c7965SRobert Watson 406fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 407fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 408fc03d22bSJeff Roberson return (ubz); 409fc03d22bSJeff Roberson ubz--; 410fc03d22bSJeff Roberson return (ubz); 411fc03d22bSJeff Roberson } 412fc03d22bSJeff Roberson 413003cf08bSMark Johnston static struct uma_bucket_zone * 414003cf08bSMark Johnston bucket_zone_max(uma_zone_t zone, int nitems) 415003cf08bSMark Johnston { 416003cf08bSMark Johnston struct uma_bucket_zone *ubz; 417003cf08bSMark Johnston int bpcpu; 418003cf08bSMark Johnston 419003cf08bSMark Johnston bpcpu = 2; 420dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 421003cf08bSMark Johnston /* Count the cross-domain bucket. */ 422003cf08bSMark Johnston bpcpu++; 423003cf08bSMark Johnston 424003cf08bSMark Johnston for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 425003cf08bSMark Johnston if (ubz->ubz_entries * bpcpu * mp_ncpus > nitems) 426003cf08bSMark Johnston break; 427003cf08bSMark Johnston if (ubz == &bucket_zones[0]) 428003cf08bSMark Johnston ubz = NULL; 429003cf08bSMark Johnston else 430003cf08bSMark Johnston ubz--; 431003cf08bSMark Johnston return (ubz); 432003cf08bSMark Johnston } 433003cf08bSMark Johnston 434fc03d22bSJeff Roberson static int 435fc03d22bSJeff Roberson bucket_select(int size) 436fc03d22bSJeff Roberson { 437fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 438fc03d22bSJeff Roberson 439fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 440fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 441fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 442fc03d22bSJeff Roberson 443fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 444fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 445fc03d22bSJeff Roberson break; 446fc03d22bSJeff Roberson ubz--; 447fc03d22bSJeff Roberson return (ubz->ubz_entries); 448dc2c7965SRobert Watson } 449dc2c7965SRobert Watson 450cae33c14SJeff Roberson static uma_bucket_t 4516fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags) 452cae33c14SJeff Roberson { 453cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 454cae33c14SJeff Roberson uma_bucket_t bucket; 455cae33c14SJeff Roberson 456cae33c14SJeff Roberson /* 457*a81c400eSJeff Roberson * Don't allocate buckets in low memory situations. 458cae33c14SJeff Roberson */ 459cae33c14SJeff Roberson if (bucketdisable) 460cae33c14SJeff Roberson return (NULL); 461*a81c400eSJeff Roberson 4626fd34d6fSJeff Roberson /* 4636fd34d6fSJeff Roberson * To limit bucket recursion we store the original zone flags 4646fd34d6fSJeff Roberson * in a cookie passed via zalloc_arg/zfree_arg. This allows the 4656fd34d6fSJeff Roberson * NOVM flag to persist even through deep recursions. We also 4666fd34d6fSJeff Roberson * store ZFLAG_BUCKET once we have recursed attempting to allocate 4676fd34d6fSJeff Roberson * a bucket for a bucket zone so we do not allow infinite bucket 4686fd34d6fSJeff Roberson * recursion. This cookie will even persist to frees of unused 4696fd34d6fSJeff Roberson * buckets via the allocation path or bucket allocations in the 4706fd34d6fSJeff Roberson * free path. 4716fd34d6fSJeff Roberson */ 4726fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 4736fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 474e8a720feSAlexander Motin else { 475e8a720feSAlexander Motin if ((uintptr_t)udata & UMA_ZFLAG_BUCKET) 476e8a720feSAlexander Motin return (NULL); 4776fd34d6fSJeff Roberson udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET); 478e8a720feSAlexander Motin } 4796fd34d6fSJeff Roberson if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY) 480af526374SJeff Roberson flags |= M_NOVM; 48120a4e154SJeff Roberson ubz = bucket_zone_lookup(zone->uz_bucket_size); 48220d3ab87SAlexander Motin if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0) 48320d3ab87SAlexander Motin ubz++; 4846fd34d6fSJeff Roberson bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags); 485cae33c14SJeff Roberson if (bucket) { 486cae33c14SJeff Roberson #ifdef INVARIANTS 487cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 488cae33c14SJeff Roberson #endif 489cae33c14SJeff Roberson bucket->ub_cnt = 0; 490cae33c14SJeff Roberson bucket->ub_entries = ubz->ubz_entries; 491cae33c14SJeff Roberson } 492cae33c14SJeff Roberson 493cae33c14SJeff Roberson return (bucket); 494cae33c14SJeff Roberson } 495cae33c14SJeff Roberson 496cae33c14SJeff Roberson static void 4976fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata) 498cae33c14SJeff Roberson { 499cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 500cae33c14SJeff Roberson 501fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 502fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 5036fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 5046fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 505dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 5066fd34d6fSJeff Roberson uma_zfree_arg(ubz->ubz_zone, bucket, udata); 507cae33c14SJeff Roberson } 508cae33c14SJeff Roberson 509cae33c14SJeff Roberson static void 510cae33c14SJeff Roberson bucket_zone_drain(void) 511cae33c14SJeff Roberson { 512cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 513cae33c14SJeff Roberson 514cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 51508cfa56eSMark Johnston uma_zone_reclaim(ubz->ubz_zone, UMA_RECLAIM_DRAIN); 516cae33c14SJeff Roberson } 517cae33c14SJeff Roberson 51808cfa56eSMark Johnston /* 51908cfa56eSMark Johnston * Attempt to satisfy an allocation by retrieving a full bucket from one of the 52008cfa56eSMark Johnston * zone's caches. 52108cfa56eSMark Johnston */ 5220f9b7bf3SMark Johnston static uma_bucket_t 52308cfa56eSMark Johnston zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom) 5240f9b7bf3SMark Johnston { 5250f9b7bf3SMark Johnston uma_bucket_t bucket; 5260f9b7bf3SMark Johnston 5270f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 5280f9b7bf3SMark Johnston 52908cfa56eSMark Johnston if ((bucket = TAILQ_FIRST(&zdom->uzd_buckets)) != NULL) { 5300f9b7bf3SMark Johnston MPASS(zdom->uzd_nitems >= bucket->ub_cnt); 53108cfa56eSMark Johnston TAILQ_REMOVE(&zdom->uzd_buckets, bucket, ub_link); 5320f9b7bf3SMark Johnston zdom->uzd_nitems -= bucket->ub_cnt; 53308cfa56eSMark Johnston if (zdom->uzd_imin > zdom->uzd_nitems) 5340f9b7bf3SMark Johnston zdom->uzd_imin = zdom->uzd_nitems; 535bb15d1c7SGleb Smirnoff zone->uz_bkt_count -= bucket->ub_cnt; 5360f9b7bf3SMark Johnston } 5370f9b7bf3SMark Johnston return (bucket); 5380f9b7bf3SMark Johnston } 5390f9b7bf3SMark Johnston 54008cfa56eSMark Johnston /* 54108cfa56eSMark Johnston * Insert a full bucket into the specified cache. The "ws" parameter indicates 54208cfa56eSMark Johnston * whether the bucket's contents should be counted as part of the zone's working 54308cfa56eSMark Johnston * set. 54408cfa56eSMark Johnston */ 5450f9b7bf3SMark Johnston static void 5460f9b7bf3SMark Johnston zone_put_bucket(uma_zone_t zone, uma_zone_domain_t zdom, uma_bucket_t bucket, 5470f9b7bf3SMark Johnston const bool ws) 5480f9b7bf3SMark Johnston { 5490f9b7bf3SMark Johnston 5500f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 55108034d10SKonstantin Belousov KASSERT(!ws || zone->uz_bkt_count < zone->uz_bkt_max, 55208034d10SKonstantin Belousov ("%s: zone %p overflow", __func__, zone)); 5530f9b7bf3SMark Johnston 55408cfa56eSMark Johnston if (ws) 55508cfa56eSMark Johnston TAILQ_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link); 55608cfa56eSMark Johnston else 55708cfa56eSMark Johnston TAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link); 5580f9b7bf3SMark Johnston zdom->uzd_nitems += bucket->ub_cnt; 5590f9b7bf3SMark Johnston if (ws && zdom->uzd_imax < zdom->uzd_nitems) 5600f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_nitems; 561bb15d1c7SGleb Smirnoff zone->uz_bkt_count += bucket->ub_cnt; 5620f9b7bf3SMark Johnston } 5630f9b7bf3SMark Johnston 564376b1ba3SJeff Roberson /* Pops an item out of a per-cpu cache bucket. */ 565376b1ba3SJeff Roberson static inline void * 566376b1ba3SJeff Roberson cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket) 567376b1ba3SJeff Roberson { 568376b1ba3SJeff Roberson void *item; 569376b1ba3SJeff Roberson 570376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 571376b1ba3SJeff Roberson 572376b1ba3SJeff Roberson bucket->ucb_cnt--; 573376b1ba3SJeff Roberson item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt]; 574376b1ba3SJeff Roberson #ifdef INVARIANTS 575376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL; 576376b1ba3SJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 577376b1ba3SJeff Roberson #endif 578376b1ba3SJeff Roberson cache->uc_allocs++; 579376b1ba3SJeff Roberson 580376b1ba3SJeff Roberson return (item); 581376b1ba3SJeff Roberson } 582376b1ba3SJeff Roberson 583376b1ba3SJeff Roberson /* Pushes an item into a per-cpu cache bucket. */ 584376b1ba3SJeff Roberson static inline void 585376b1ba3SJeff Roberson cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item) 586376b1ba3SJeff Roberson { 587376b1ba3SJeff Roberson 588376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 589376b1ba3SJeff Roberson KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL, 590376b1ba3SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 591376b1ba3SJeff Roberson 592376b1ba3SJeff Roberson bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item; 593376b1ba3SJeff Roberson bucket->ucb_cnt++; 594376b1ba3SJeff Roberson cache->uc_frees++; 595376b1ba3SJeff Roberson } 596376b1ba3SJeff Roberson 597376b1ba3SJeff Roberson /* 598376b1ba3SJeff Roberson * Unload a UMA bucket from a per-cpu cache. 599376b1ba3SJeff Roberson */ 600376b1ba3SJeff Roberson static inline uma_bucket_t 601376b1ba3SJeff Roberson cache_bucket_unload(uma_cache_bucket_t bucket) 602376b1ba3SJeff Roberson { 603376b1ba3SJeff Roberson uma_bucket_t b; 604376b1ba3SJeff Roberson 605376b1ba3SJeff Roberson b = bucket->ucb_bucket; 606376b1ba3SJeff Roberson if (b != NULL) { 607376b1ba3SJeff Roberson MPASS(b->ub_entries == bucket->ucb_entries); 608376b1ba3SJeff Roberson b->ub_cnt = bucket->ucb_cnt; 609376b1ba3SJeff Roberson bucket->ucb_bucket = NULL; 610376b1ba3SJeff Roberson bucket->ucb_entries = bucket->ucb_cnt = 0; 611376b1ba3SJeff Roberson } 612376b1ba3SJeff Roberson 613376b1ba3SJeff Roberson return (b); 614376b1ba3SJeff Roberson } 615376b1ba3SJeff Roberson 616376b1ba3SJeff Roberson static inline uma_bucket_t 617376b1ba3SJeff Roberson cache_bucket_unload_alloc(uma_cache_t cache) 618376b1ba3SJeff Roberson { 619376b1ba3SJeff Roberson 620376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_allocbucket)); 621376b1ba3SJeff Roberson } 622376b1ba3SJeff Roberson 623376b1ba3SJeff Roberson static inline uma_bucket_t 624376b1ba3SJeff Roberson cache_bucket_unload_free(uma_cache_t cache) 625376b1ba3SJeff Roberson { 626376b1ba3SJeff Roberson 627376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_freebucket)); 628376b1ba3SJeff Roberson } 629376b1ba3SJeff Roberson 630376b1ba3SJeff Roberson static inline uma_bucket_t 631376b1ba3SJeff Roberson cache_bucket_unload_cross(uma_cache_t cache) 632376b1ba3SJeff Roberson { 633376b1ba3SJeff Roberson 634376b1ba3SJeff Roberson return (cache_bucket_unload(&cache->uc_crossbucket)); 635376b1ba3SJeff Roberson } 636376b1ba3SJeff Roberson 637376b1ba3SJeff Roberson /* 638376b1ba3SJeff Roberson * Load a bucket into a per-cpu cache bucket. 639376b1ba3SJeff Roberson */ 640376b1ba3SJeff Roberson static inline void 641376b1ba3SJeff Roberson cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b) 642376b1ba3SJeff Roberson { 643376b1ba3SJeff Roberson 644376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 645376b1ba3SJeff Roberson MPASS(bucket->ucb_bucket == NULL); 646376b1ba3SJeff Roberson 647376b1ba3SJeff Roberson bucket->ucb_bucket = b; 648376b1ba3SJeff Roberson bucket->ucb_cnt = b->ub_cnt; 649376b1ba3SJeff Roberson bucket->ucb_entries = b->ub_entries; 650376b1ba3SJeff Roberson } 651376b1ba3SJeff Roberson 652376b1ba3SJeff Roberson static inline void 653376b1ba3SJeff Roberson cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b) 654376b1ba3SJeff Roberson { 655376b1ba3SJeff Roberson 656376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_allocbucket, b); 657376b1ba3SJeff Roberson } 658376b1ba3SJeff Roberson 659376b1ba3SJeff Roberson static inline void 660376b1ba3SJeff Roberson cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b) 661376b1ba3SJeff Roberson { 662376b1ba3SJeff Roberson 663376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_freebucket, b); 664376b1ba3SJeff Roberson } 665376b1ba3SJeff Roberson 666dfe13344SJeff Roberson #ifdef NUMA 667376b1ba3SJeff Roberson static inline void 668376b1ba3SJeff Roberson cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b) 669376b1ba3SJeff Roberson { 670376b1ba3SJeff Roberson 671376b1ba3SJeff Roberson cache_bucket_load(&cache->uc_crossbucket, b); 672376b1ba3SJeff Roberson } 673376b1ba3SJeff Roberson #endif 674376b1ba3SJeff Roberson 675376b1ba3SJeff Roberson /* 676376b1ba3SJeff Roberson * Copy and preserve ucb_spare. 677376b1ba3SJeff Roberson */ 678376b1ba3SJeff Roberson static inline void 679376b1ba3SJeff Roberson cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 680376b1ba3SJeff Roberson { 681376b1ba3SJeff Roberson 682376b1ba3SJeff Roberson b1->ucb_bucket = b2->ucb_bucket; 683376b1ba3SJeff Roberson b1->ucb_entries = b2->ucb_entries; 684376b1ba3SJeff Roberson b1->ucb_cnt = b2->ucb_cnt; 685376b1ba3SJeff Roberson } 686376b1ba3SJeff Roberson 687376b1ba3SJeff Roberson /* 688376b1ba3SJeff Roberson * Swap two cache buckets. 689376b1ba3SJeff Roberson */ 690376b1ba3SJeff Roberson static inline void 691376b1ba3SJeff Roberson cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2) 692376b1ba3SJeff Roberson { 693376b1ba3SJeff Roberson struct uma_cache_bucket b3; 694376b1ba3SJeff Roberson 695376b1ba3SJeff Roberson CRITICAL_ASSERT(curthread); 696376b1ba3SJeff Roberson 697376b1ba3SJeff Roberson cache_bucket_copy(&b3, b1); 698376b1ba3SJeff Roberson cache_bucket_copy(b1, b2); 699376b1ba3SJeff Roberson cache_bucket_copy(b2, &b3); 700376b1ba3SJeff Roberson } 701376b1ba3SJeff Roberson 7022f891cd5SPawel Jakub Dawidek static void 7032f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 7042f891cd5SPawel Jakub Dawidek { 7052f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 7062f891cd5SPawel Jakub Dawidek 7072f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 7082f891cd5SPawel Jakub Dawidek return; 7092f891cd5SPawel Jakub Dawidek 7102f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 7112f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 7122f891cd5SPawel Jakub Dawidek } 7132f891cd5SPawel Jakub Dawidek 71454503a13SJonathan T. Looney static inline void 71554503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone) 71654503a13SJonathan T. Looney { 717e60b2fcbSGleb Smirnoff 718e60b2fcbSGleb Smirnoff if (zone->uz_maxaction.ta_func != NULL) 719e60b2fcbSGleb Smirnoff taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction); 72054503a13SJonathan T. Looney } 72154503a13SJonathan T. Looney 7228355f576SJeff Roberson /* 7238355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 7249643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 7258355f576SJeff Roberson * 7268355f576SJeff Roberson * Arguments: 7278355f576SJeff Roberson * arg Unused 7288355f576SJeff Roberson * 7298355f576SJeff Roberson * Returns: 7308355f576SJeff Roberson * Nothing 7318355f576SJeff Roberson */ 7328355f576SJeff Roberson static void 7338355f576SJeff Roberson uma_timeout(void *unused) 7348355f576SJeff Roberson { 73586bbae32SJeff Roberson bucket_enable(); 73620a4e154SJeff Roberson zone_foreach(zone_timeout, NULL); 7378355f576SJeff Roberson 7388355f576SJeff Roberson /* Reschedule this event */ 7399643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 7408355f576SJeff Roberson } 7418355f576SJeff Roberson 7428355f576SJeff Roberson /* 7430f9b7bf3SMark Johnston * Update the working set size estimate for the zone's bucket cache. 7440f9b7bf3SMark Johnston * The constants chosen here are somewhat arbitrary. With an update period of 7450f9b7bf3SMark Johnston * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the 7460f9b7bf3SMark Johnston * last 100s. 7470f9b7bf3SMark Johnston */ 7480f9b7bf3SMark Johnston static void 7490f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom) 7500f9b7bf3SMark Johnston { 7510f9b7bf3SMark Johnston long wss; 7520f9b7bf3SMark Johnston 7530f9b7bf3SMark Johnston MPASS(zdom->uzd_imax >= zdom->uzd_imin); 7540f9b7bf3SMark Johnston wss = zdom->uzd_imax - zdom->uzd_imin; 7550f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems; 75608cfa56eSMark Johnston zdom->uzd_wss = (4 * wss + zdom->uzd_wss) / 5; 7570f9b7bf3SMark Johnston } 7580f9b7bf3SMark Johnston 7590f9b7bf3SMark Johnston /* 7609643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 7619643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 7628355f576SJeff Roberson * 763e20a199fSJeff Roberson * Returns nothing. 7648355f576SJeff Roberson */ 7658355f576SJeff Roberson static void 76620a4e154SJeff Roberson zone_timeout(uma_zone_t zone, void *unused) 7678355f576SJeff Roberson { 76808034d10SKonstantin Belousov uma_keg_t keg; 7698b987a77SJeff Roberson u_int slabs, pages; 7708355f576SJeff Roberson 77154c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 77208034d10SKonstantin Belousov goto update_wss; 77308034d10SKonstantin Belousov 77408034d10SKonstantin Belousov keg = zone->uz_keg; 7758b987a77SJeff Roberson 7768b987a77SJeff Roberson /* 7778b987a77SJeff Roberson * Hash zones are non-numa by definition so the first domain 7788b987a77SJeff Roberson * is the only one present. 7798b987a77SJeff Roberson */ 7808b987a77SJeff Roberson KEG_LOCK(keg, 0); 7818b987a77SJeff Roberson pages = keg->uk_domain[0].ud_pages; 7828b987a77SJeff Roberson 7838355f576SJeff Roberson /* 784e20a199fSJeff Roberson * Expand the keg hash table. 7858355f576SJeff Roberson * 7868355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 7878355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 7888355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 7898355f576SJeff Roberson */ 7908b987a77SJeff Roberson if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) { 7910aef6126SJeff Roberson struct uma_hash newhash; 7920aef6126SJeff Roberson struct uma_hash oldhash; 7930aef6126SJeff Roberson int ret; 7945300d9ddSJeff Roberson 7950aef6126SJeff Roberson /* 7960aef6126SJeff Roberson * This is so involved because allocating and freeing 797e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 7980aef6126SJeff Roberson * I have to do everything in stages and check for 7990aef6126SJeff Roberson * races. 8000aef6126SJeff Roberson */ 8018b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 8023b2f2cb8SAlexander Motin ret = hash_alloc(&newhash, 1 << fls(slabs)); 8038b987a77SJeff Roberson KEG_LOCK(keg, 0); 8040aef6126SJeff Roberson if (ret) { 805099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 806099a0e58SBosko Milekic oldhash = keg->uk_hash; 807099a0e58SBosko Milekic keg->uk_hash = newhash; 8080aef6126SJeff Roberson } else 8090aef6126SJeff Roberson oldhash = newhash; 8100aef6126SJeff Roberson 8118b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 8120aef6126SJeff Roberson hash_free(&oldhash); 8138b987a77SJeff Roberson goto update_wss; 8140aef6126SJeff Roberson } 8155300d9ddSJeff Roberson } 8168b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 817e20a199fSJeff Roberson 81808034d10SKonstantin Belousov update_wss: 81908cfa56eSMark Johnston ZONE_LOCK(zone); 820bb15d1c7SGleb Smirnoff for (int i = 0; i < vm_ndomains; i++) 8210f9b7bf3SMark Johnston zone_domain_update_wss(&zone->uz_domain[i]); 82208cfa56eSMark Johnston ZONE_UNLOCK(zone); 8238355f576SJeff Roberson } 8248355f576SJeff Roberson 8258355f576SJeff Roberson /* 8265300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 8275300d9ddSJeff Roberson * backing store. 8285300d9ddSJeff Roberson * 8295300d9ddSJeff Roberson * Arguments: 8300aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 8315300d9ddSJeff Roberson * 8325300d9ddSJeff Roberson * Returns: 833763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 8345300d9ddSJeff Roberson */ 83537c84183SPoul-Henning Kamp static int 8363b2f2cb8SAlexander Motin hash_alloc(struct uma_hash *hash, u_int size) 8375300d9ddSJeff Roberson { 83859568a0eSAlexander Motin size_t alloc; 8395300d9ddSJeff Roberson 8403b2f2cb8SAlexander Motin KASSERT(powerof2(size), ("hash size must be power of 2")); 8413b2f2cb8SAlexander Motin if (size > UMA_HASH_SIZE_INIT) { 8423b2f2cb8SAlexander Motin hash->uh_hashsize = size; 8430aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 8441e0701e1SJeff Roberson hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT); 8455300d9ddSJeff Roberson } else { 8460aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 847e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 848ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 8490aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 8505300d9ddSJeff Roberson } 8510aef6126SJeff Roberson if (hash->uh_slab_hash) { 8520aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 8530aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 8540aef6126SJeff Roberson return (1); 8550aef6126SJeff Roberson } 8565300d9ddSJeff Roberson 8570aef6126SJeff Roberson return (0); 8585300d9ddSJeff Roberson } 8595300d9ddSJeff Roberson 8605300d9ddSJeff Roberson /* 86164f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 86264f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 86364f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 8648355f576SJeff Roberson * 8658355f576SJeff Roberson * Arguments: 8660aef6126SJeff Roberson * oldhash The hash you want to expand 8670aef6126SJeff Roberson * newhash The hash structure for the new table 8688355f576SJeff Roberson * 8698355f576SJeff Roberson * Returns: 8708355f576SJeff Roberson * Nothing 8718355f576SJeff Roberson * 8728355f576SJeff Roberson * Discussion: 8738355f576SJeff Roberson */ 8740aef6126SJeff Roberson static int 8750aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 8768355f576SJeff Roberson { 8771e0701e1SJeff Roberson uma_hash_slab_t slab; 8786929b7d1SPedro F. Giffuni u_int hval; 8796929b7d1SPedro F. Giffuni u_int idx; 8808355f576SJeff Roberson 8810aef6126SJeff Roberson if (!newhash->uh_slab_hash) 8820aef6126SJeff Roberson return (0); 8838355f576SJeff Roberson 8840aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 8850aef6126SJeff Roberson return (0); 8868355f576SJeff Roberson 8878355f576SJeff Roberson /* 8888355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 8898355f576SJeff Roberson * full rehash. 8908355f576SJeff Roberson */ 8918355f576SJeff Roberson 8926929b7d1SPedro F. Giffuni for (idx = 0; idx < oldhash->uh_hashsize; idx++) 8931e0701e1SJeff Roberson while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) { 8941e0701e1SJeff Roberson slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]); 8951e0701e1SJeff Roberson LIST_REMOVE(slab, uhs_hlink); 8961e0701e1SJeff Roberson hval = UMA_HASH(newhash, slab->uhs_data); 8971e0701e1SJeff Roberson LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 8981e0701e1SJeff Roberson slab, uhs_hlink); 8998355f576SJeff Roberson } 9008355f576SJeff Roberson 9010aef6126SJeff Roberson return (1); 9029c2cd7e5SJeff Roberson } 9039c2cd7e5SJeff Roberson 9045300d9ddSJeff Roberson /* 9055300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 9065300d9ddSJeff Roberson * 9075300d9ddSJeff Roberson * Arguments: 9085300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 9095300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 9105300d9ddSJeff Roberson * 9115300d9ddSJeff Roberson * Returns: 9125300d9ddSJeff Roberson * Nothing 9135300d9ddSJeff Roberson */ 9149c2cd7e5SJeff Roberson static void 9150aef6126SJeff Roberson hash_free(struct uma_hash *hash) 9169c2cd7e5SJeff Roberson { 9170aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 9180aef6126SJeff Roberson return; 9190aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 9200095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 9218355f576SJeff Roberson else 922961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 9238355f576SJeff Roberson } 9248355f576SJeff Roberson 9258355f576SJeff Roberson /* 9268355f576SJeff Roberson * Frees all outstanding items in a bucket 9278355f576SJeff Roberson * 9288355f576SJeff Roberson * Arguments: 9298355f576SJeff Roberson * zone The zone to free to, must be unlocked. 9304bd61e19SJeff Roberson * bucket The free/alloc bucket with items. 9318355f576SJeff Roberson * 9328355f576SJeff Roberson * Returns: 9338355f576SJeff Roberson * Nothing 9348355f576SJeff Roberson */ 9358355f576SJeff Roberson 9368355f576SJeff Roberson static void 9378355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 9388355f576SJeff Roberson { 9390095a784SJeff Roberson int i; 9408355f576SJeff Roberson 9414bd61e19SJeff Roberson if (bucket == NULL || bucket->ub_cnt == 0) 9428355f576SJeff Roberson return; 9438355f576SJeff Roberson 9440095a784SJeff Roberson if (zone->uz_fini) 9450095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 9460095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 9470095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 9484bd61e19SJeff Roberson if (zone->uz_max_items > 0) 9494bd61e19SJeff Roberson zone_free_limit(zone, bucket->ub_cnt); 9500095a784SJeff Roberson bucket->ub_cnt = 0; 9518355f576SJeff Roberson } 9528355f576SJeff Roberson 9538355f576SJeff Roberson /* 9548355f576SJeff Roberson * Drains the per cpu caches for a zone. 9558355f576SJeff Roberson * 956727c6918SJeff Roberson * NOTE: This may only be called while the zone is being torn down, and not 9575d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 9585d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 9595d1ae027SRobert Watson * 9608355f576SJeff Roberson * Arguments: 9618355f576SJeff Roberson * zone The zone to drain, must be unlocked. 9628355f576SJeff Roberson * 9638355f576SJeff Roberson * Returns: 9648355f576SJeff Roberson * Nothing 9658355f576SJeff Roberson */ 9668355f576SJeff Roberson static void 9679643769aSJeff Roberson cache_drain(uma_zone_t zone) 9688355f576SJeff Roberson { 9698355f576SJeff Roberson uma_cache_t cache; 970376b1ba3SJeff Roberson uma_bucket_t bucket; 9718355f576SJeff Roberson int cpu; 9728355f576SJeff Roberson 9738355f576SJeff Roberson /* 9745d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 9755d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 9765d1ae027SRobert Watson * of the caches at this point. 9775d1ae027SRobert Watson * 9785d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 9795d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 9808355f576SJeff Roberson */ 9813aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 9828355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 983376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 984376b1ba3SJeff Roberson if (bucket != NULL) { 985376b1ba3SJeff Roberson bucket_drain(zone, bucket); 986376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 987376b1ba3SJeff Roberson } 988376b1ba3SJeff Roberson bucket = cache_bucket_unload_free(cache); 989376b1ba3SJeff Roberson if (bucket != NULL) { 990376b1ba3SJeff Roberson bucket_drain(zone, bucket); 991376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 992376b1ba3SJeff Roberson } 993376b1ba3SJeff Roberson bucket = cache_bucket_unload_cross(cache); 994376b1ba3SJeff Roberson if (bucket != NULL) { 995376b1ba3SJeff Roberson bucket_drain(zone, bucket); 996376b1ba3SJeff Roberson bucket_free(zone, bucket, NULL); 997376b1ba3SJeff Roberson } 998d56368d7SBosko Milekic } 99908cfa56eSMark Johnston bucket_cache_reclaim(zone, true); 1000aaa8bb16SJeff Roberson } 1001aaa8bb16SJeff Roberson 1002a2de44abSAlexander Motin static void 100320a4e154SJeff Roberson cache_shrink(uma_zone_t zone, void *unused) 1004a2de44abSAlexander Motin { 1005a2de44abSAlexander Motin 1006a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1007a2de44abSAlexander Motin return; 1008a2de44abSAlexander Motin 1009a2de44abSAlexander Motin ZONE_LOCK(zone); 101020a4e154SJeff Roberson zone->uz_bucket_size = 101120a4e154SJeff Roberson (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2; 1012a2de44abSAlexander Motin ZONE_UNLOCK(zone); 1013a2de44abSAlexander Motin } 1014a2de44abSAlexander Motin 1015a2de44abSAlexander Motin static void 101620a4e154SJeff Roberson cache_drain_safe_cpu(uma_zone_t zone, void *unused) 1017a2de44abSAlexander Motin { 1018a2de44abSAlexander Motin uma_cache_t cache; 1019c1685086SJeff Roberson uma_bucket_t b1, b2, b3; 1020ab3185d1SJeff Roberson int domain; 1021a2de44abSAlexander Motin 1022a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 1023a2de44abSAlexander Motin return; 1024a2de44abSAlexander Motin 1025c1685086SJeff Roberson b1 = b2 = b3 = NULL; 1026a2de44abSAlexander Motin ZONE_LOCK(zone); 1027a2de44abSAlexander Motin critical_enter(); 1028dfe13344SJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) 1029ab3185d1SJeff Roberson domain = PCPU_GET(domain); 1030ab3185d1SJeff Roberson else 1031ab3185d1SJeff Roberson domain = 0; 1032a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 1033376b1ba3SJeff Roberson b1 = cache_bucket_unload_alloc(cache); 1034376b1ba3SJeff Roberson if (b1 != NULL && b1->ub_cnt != 0) { 1035376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b1, false); 1036376b1ba3SJeff Roberson b1 = NULL; 1037a2de44abSAlexander Motin } 1038376b1ba3SJeff Roberson b2 = cache_bucket_unload_free(cache); 1039376b1ba3SJeff Roberson if (b2 != NULL && b2->ub_cnt != 0) { 1040376b1ba3SJeff Roberson zone_put_bucket(zone, &zone->uz_domain[domain], b2, false); 1041376b1ba3SJeff Roberson b2 = NULL; 1042a2de44abSAlexander Motin } 1043376b1ba3SJeff Roberson b3 = cache_bucket_unload_cross(cache); 1044a2de44abSAlexander Motin critical_exit(); 1045a2de44abSAlexander Motin ZONE_UNLOCK(zone); 10468a8d9d14SAlexander Motin if (b1) 10478a8d9d14SAlexander Motin bucket_free(zone, b1, NULL); 10488a8d9d14SAlexander Motin if (b2) 10498a8d9d14SAlexander Motin bucket_free(zone, b2, NULL); 1050c1685086SJeff Roberson if (b3) { 1051c1685086SJeff Roberson bucket_drain(zone, b3); 1052c1685086SJeff Roberson bucket_free(zone, b3, NULL); 1053c1685086SJeff Roberson } 1054a2de44abSAlexander Motin } 1055a2de44abSAlexander Motin 1056a2de44abSAlexander Motin /* 1057a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 1058a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 1059a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 1060a2de44abSAlexander Motin * to safely access their cache buckets. 1061a2de44abSAlexander Motin * Zone lock must not be held on call this function. 1062a2de44abSAlexander Motin */ 1063a2de44abSAlexander Motin static void 106408cfa56eSMark Johnston pcpu_cache_drain_safe(uma_zone_t zone) 1065a2de44abSAlexander Motin { 1066a2de44abSAlexander Motin int cpu; 1067a2de44abSAlexander Motin 1068a2de44abSAlexander Motin /* 1069727c6918SJeff Roberson * Polite bucket sizes shrinking was not enough, shrink aggressively. 1070a2de44abSAlexander Motin */ 1071a2de44abSAlexander Motin if (zone) 107220a4e154SJeff Roberson cache_shrink(zone, NULL); 1073a2de44abSAlexander Motin else 107420a4e154SJeff Roberson zone_foreach(cache_shrink, NULL); 1075a2de44abSAlexander Motin 1076a2de44abSAlexander Motin CPU_FOREACH(cpu) { 1077a2de44abSAlexander Motin thread_lock(curthread); 1078a2de44abSAlexander Motin sched_bind(curthread, cpu); 1079a2de44abSAlexander Motin thread_unlock(curthread); 1080a2de44abSAlexander Motin 1081a2de44abSAlexander Motin if (zone) 108220a4e154SJeff Roberson cache_drain_safe_cpu(zone, NULL); 1083a2de44abSAlexander Motin else 108420a4e154SJeff Roberson zone_foreach(cache_drain_safe_cpu, NULL); 1085a2de44abSAlexander Motin } 1086a2de44abSAlexander Motin thread_lock(curthread); 1087a2de44abSAlexander Motin sched_unbind(curthread); 1088a2de44abSAlexander Motin thread_unlock(curthread); 1089a2de44abSAlexander Motin } 1090a2de44abSAlexander Motin 1091aaa8bb16SJeff Roberson /* 109208cfa56eSMark Johnston * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller 109308cfa56eSMark Johnston * requested a drain, otherwise the per-domain caches are trimmed to either 109408cfa56eSMark Johnston * estimated working set size. 1095aaa8bb16SJeff Roberson */ 1096aaa8bb16SJeff Roberson static void 109708cfa56eSMark Johnston bucket_cache_reclaim(uma_zone_t zone, bool drain) 1098aaa8bb16SJeff Roberson { 1099ab3185d1SJeff Roberson uma_zone_domain_t zdom; 1100aaa8bb16SJeff Roberson uma_bucket_t bucket; 110108cfa56eSMark Johnston long target, tofree; 1102ab3185d1SJeff Roberson int i; 11038355f576SJeff Roberson 1104ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 110591d947bfSJeff Roberson /* 110691d947bfSJeff Roberson * The cross bucket is partially filled and not part of 110791d947bfSJeff Roberson * the item count. Reclaim it individually here. 110891d947bfSJeff Roberson */ 1109ab3185d1SJeff Roberson zdom = &zone->uz_domain[i]; 111091d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 111191d947bfSJeff Roberson bucket = zdom->uzd_cross; 111291d947bfSJeff Roberson zdom->uzd_cross = NULL; 111391d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 111491d947bfSJeff Roberson if (bucket != NULL) { 111591d947bfSJeff Roberson bucket_drain(zone, bucket); 111691d947bfSJeff Roberson bucket_free(zone, bucket, NULL); 111791d947bfSJeff Roberson } 111891d947bfSJeff Roberson 111991d947bfSJeff Roberson /* 112091d947bfSJeff Roberson * Shrink the zone bucket size to ensure that the per-CPU caches 112191d947bfSJeff Roberson * don't grow too large. 112291d947bfSJeff Roberson */ 112391d947bfSJeff Roberson ZONE_LOCK(zone); 112491d947bfSJeff Roberson if (i == 0 && zone->uz_bucket_size > zone->uz_bucket_size_min) 112591d947bfSJeff Roberson zone->uz_bucket_size--; 112608cfa56eSMark Johnston 112708cfa56eSMark Johnston /* 112808cfa56eSMark Johnston * If we were asked to drain the zone, we are done only once 112908cfa56eSMark Johnston * this bucket cache is empty. Otherwise, we reclaim items in 113008cfa56eSMark Johnston * excess of the zone's estimated working set size. If the 113108cfa56eSMark Johnston * difference nitems - imin is larger than the WSS estimate, 113208cfa56eSMark Johnston * then the estimate will grow at the end of this interval and 113308cfa56eSMark Johnston * we ignore the historical average. 113408cfa56eSMark Johnston */ 113508cfa56eSMark Johnston target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems - 113608cfa56eSMark Johnston zdom->uzd_imin); 113708cfa56eSMark Johnston while (zdom->uzd_nitems > target) { 113808cfa56eSMark Johnston bucket = TAILQ_LAST(&zdom->uzd_buckets, uma_bucketlist); 113908cfa56eSMark Johnston if (bucket == NULL) 114008cfa56eSMark Johnston break; 114108cfa56eSMark Johnston tofree = bucket->ub_cnt; 114208cfa56eSMark Johnston TAILQ_REMOVE(&zdom->uzd_buckets, bucket, ub_link); 114308cfa56eSMark Johnston zdom->uzd_nitems -= tofree; 114408cfa56eSMark Johnston 114508cfa56eSMark Johnston /* 114608cfa56eSMark Johnston * Shift the bounds of the current WSS interval to avoid 114708cfa56eSMark Johnston * perturbing the estimate. 114808cfa56eSMark Johnston */ 114908cfa56eSMark Johnston zdom->uzd_imax -= lmin(zdom->uzd_imax, tofree); 115008cfa56eSMark Johnston zdom->uzd_imin -= lmin(zdom->uzd_imin, tofree); 115108cfa56eSMark Johnston 11528355f576SJeff Roberson ZONE_UNLOCK(zone); 11538355f576SJeff Roberson bucket_drain(zone, bucket); 11546fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 11558355f576SJeff Roberson ZONE_LOCK(zone); 11568355f576SJeff Roberson } 115791d947bfSJeff Roberson ZONE_UNLOCK(zone); 1158ab3185d1SJeff Roberson } 11598355f576SJeff Roberson } 1160fc03d22bSJeff Roberson 1161fc03d22bSJeff Roberson static void 1162fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 1163fc03d22bSJeff Roberson { 1164fc03d22bSJeff Roberson uint8_t *mem; 1165fc03d22bSJeff Roberson int i; 1166fc03d22bSJeff Roberson uint8_t flags; 1167fc03d22bSJeff Roberson 11681431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 11691431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 11701431a748SGleb Smirnoff 11711e0701e1SJeff Roberson mem = slab_data(slab, keg); 1172fc03d22bSJeff Roberson flags = slab->us_flags; 1173fc03d22bSJeff Roberson i = start; 1174fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 1175fc03d22bSJeff Roberson for (i--; i > -1; i--) 1176c5deaf04SGleb Smirnoff #ifdef INVARIANTS 1177c5deaf04SGleb Smirnoff /* 1178c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 1179c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 1180c5deaf04SGleb Smirnoff * which executed trash_dtor. 1181c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 1182c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 1183c5deaf04SGleb Smirnoff * invocations. 1184c5deaf04SGleb Smirnoff */ 11851e0701e1SJeff Roberson if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) || 1186c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 1187c5deaf04SGleb Smirnoff #endif 11881e0701e1SJeff Roberson keg->uk_fini(slab_item(slab, keg, i), keg->uk_size); 1189fc03d22bSJeff Roberson } 119054c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 11919b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab), 11929b8db4d0SRyan Libby NULL, SKIP_NONE); 1193fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 11942e47807cSJeff Roberson uma_total_dec(PAGE_SIZE * keg->uk_ppera); 11958355f576SJeff Roberson } 11968355f576SJeff Roberson 11978355f576SJeff Roberson /* 1198e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 11998355f576SJeff Roberson * the pageout daemon. 12008355f576SJeff Roberson * 1201e20a199fSJeff Roberson * Returns nothing. 12028355f576SJeff Roberson */ 1203e20a199fSJeff Roberson static void 1204e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 12058355f576SJeff Roberson { 12061e183df2SStefan Farfeleder struct slabhead freeslabs = { 0 }; 1207ab3185d1SJeff Roberson uma_domain_t dom; 1208829be516SMark Johnston uma_slab_t slab, tmp; 12098b987a77SJeff Roberson int i, n; 12108355f576SJeff Roberson 12118355f576SJeff Roberson /* 1212e20a199fSJeff Roberson * We don't want to take pages from statically allocated kegs at this 12138355f576SJeff Roberson * time 12148355f576SJeff Roberson */ 1215099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 12168355f576SJeff Roberson return; 12178355f576SJeff Roberson 1218ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 12198b987a77SJeff Roberson CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u", 12208b987a77SJeff Roberson keg->uk_name, keg, i, dom->ud_free); 12218b987a77SJeff Roberson n = 0; 1222ab3185d1SJeff Roberson dom = &keg->uk_domain[i]; 12238b987a77SJeff Roberson KEG_LOCK(keg, i); 1224ab3185d1SJeff Roberson LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) { 122554c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 12261e0701e1SJeff Roberson UMA_HASH_REMOVE(&keg->uk_hash, slab); 12278b987a77SJeff Roberson n++; 12288b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 12291e0701e1SJeff Roberson LIST_INSERT_HEAD(&freeslabs, slab, us_link); 1230713deb36SJeff Roberson } 12318b987a77SJeff Roberson dom->ud_pages -= n * keg->uk_ppera; 12328b987a77SJeff Roberson dom->ud_free -= n * keg->uk_ipers; 12338b987a77SJeff Roberson KEG_UNLOCK(keg, i); 1234ab3185d1SJeff Roberson } 1235ab3185d1SJeff Roberson 12361e0701e1SJeff Roberson while ((slab = LIST_FIRST(&freeslabs)) != NULL) { 12371e0701e1SJeff Roberson LIST_REMOVE(slab, us_link); 12381645995bSKirk McKusick keg_free_slab(keg, slab, keg->uk_ipers); 12398355f576SJeff Roberson } 12408355f576SJeff Roberson } 12418355f576SJeff Roberson 1242e20a199fSJeff Roberson static void 124308cfa56eSMark Johnston zone_reclaim(uma_zone_t zone, int waitok, bool drain) 1244e20a199fSJeff Roberson { 1245e20a199fSJeff Roberson 12468355f576SJeff Roberson /* 1247e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 1248e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 1249e20a199fSJeff Roberson * is the only call that knows the structure will still be available 1250e20a199fSJeff Roberson * when it wakes up. 1251e20a199fSJeff Roberson */ 1252e20a199fSJeff Roberson ZONE_LOCK(zone); 125308cfa56eSMark Johnston while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) { 1254e20a199fSJeff Roberson if (waitok == M_NOWAIT) 1255e20a199fSJeff Roberson goto out; 1256727c6918SJeff Roberson msleep(zone, &zone->uz_lock, PVM, "zonedrain", 1); 1257e20a199fSJeff Roberson } 125808cfa56eSMark Johnston zone->uz_flags |= UMA_ZFLAG_RECLAIMING; 1259e20a199fSJeff Roberson ZONE_UNLOCK(zone); 126091d947bfSJeff Roberson bucket_cache_reclaim(zone, drain); 126108cfa56eSMark Johnston 1262e20a199fSJeff Roberson /* 1263e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 1264111fbcd5SBryan Venteicher * we're running. Normally the uma_rwlock would protect us but we 1265e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 1266e20a199fSJeff Roberson */ 126708034d10SKonstantin Belousov if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) 1268bb15d1c7SGleb Smirnoff keg_drain(zone->uz_keg); 1269e20a199fSJeff Roberson ZONE_LOCK(zone); 127008cfa56eSMark Johnston zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING; 1271e20a199fSJeff Roberson wakeup(zone); 1272e20a199fSJeff Roberson out: 1273e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1274e20a199fSJeff Roberson } 1275e20a199fSJeff Roberson 127608cfa56eSMark Johnston static void 127720a4e154SJeff Roberson zone_drain(uma_zone_t zone, void *unused) 1278e20a199fSJeff Roberson { 1279e20a199fSJeff Roberson 128008cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, true); 128108cfa56eSMark Johnston } 128208cfa56eSMark Johnston 128308cfa56eSMark Johnston static void 128420a4e154SJeff Roberson zone_trim(uma_zone_t zone, void *unused) 128508cfa56eSMark Johnston { 128608cfa56eSMark Johnston 128708cfa56eSMark Johnston zone_reclaim(zone, M_NOWAIT, false); 1288e20a199fSJeff Roberson } 1289e20a199fSJeff Roberson 1290e20a199fSJeff Roberson /* 12918b987a77SJeff Roberson * Allocate a new slab for a keg and inserts it into the partial slab list. 12928b987a77SJeff Roberson * The keg should be unlocked on entry. If the allocation succeeds it will 12938b987a77SJeff Roberson * be locked on return. 12948355f576SJeff Roberson * 12958355f576SJeff Roberson * Arguments: 129686220393SMark Johnston * flags Wait flags for the item initialization routine 129786220393SMark Johnston * aflags Wait flags for the slab allocation 12988355f576SJeff Roberson * 12998355f576SJeff Roberson * Returns: 13008355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 13018355f576SJeff Roberson * caller specified M_NOWAIT. 13028355f576SJeff Roberson */ 13038355f576SJeff Roberson static uma_slab_t 130486220393SMark Johnston keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags, 130586220393SMark Johnston int aflags) 13068355f576SJeff Roberson { 13078b987a77SJeff Roberson uma_domain_t dom; 1308e20a199fSJeff Roberson uma_alloc allocf; 1309099a0e58SBosko Milekic uma_slab_t slab; 13102e47807cSJeff Roberson unsigned long size; 131185dcf349SGleb Smirnoff uint8_t *mem; 131286220393SMark Johnston uint8_t sflags; 13138355f576SJeff Roberson int i; 13148355f576SJeff Roberson 1315ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1316ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1317a553d4b8SJeff Roberson 13188b987a77SJeff Roberson allocf = keg->uk_allocf; 1319194a979eSMark Johnston slab = NULL; 1320194a979eSMark Johnston mem = NULL; 132154c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) { 13229b8db4d0SRyan Libby uma_hash_slab_t hslab; 13239b8db4d0SRyan Libby hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL, 13249b8db4d0SRyan Libby domain, aflags); 13259b8db4d0SRyan Libby if (hslab == NULL) 1326727c6918SJeff Roberson goto fail; 13279b8db4d0SRyan Libby slab = &hslab->uhs_slab; 1328a553d4b8SJeff Roberson } 1329a553d4b8SJeff Roberson 13303370c5bfSJeff Roberson /* 13313370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 13323370c5bfSJeff Roberson * first time they are added to a zone. 13333370c5bfSJeff Roberson * 13343370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 13353370c5bfSJeff Roberson */ 13363370c5bfSJeff Roberson 1337099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 133886220393SMark Johnston aflags |= M_ZERO; 13393370c5bfSJeff Roberson else 134086220393SMark Johnston aflags &= ~M_ZERO; 13413370c5bfSJeff Roberson 1342263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 134386220393SMark Johnston aflags |= M_NODUMP; 1344263811f7SKip Macy 1345e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1346194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 134786220393SMark Johnston mem = allocf(zone, size, domain, &sflags, aflags); 1348a553d4b8SJeff Roberson if (mem == NULL) { 134954c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) 13509b8db4d0SRyan Libby zone_free_item(slabzone(keg->uk_ipers), 13519b8db4d0SRyan Libby slab_tohashslab(slab), NULL, SKIP_NONE); 1352727c6918SJeff Roberson goto fail; 1353a553d4b8SJeff Roberson } 13542e47807cSJeff Roberson uma_total_inc(size); 13558355f576SJeff Roberson 13568b987a77SJeff Roberson /* For HASH zones all pages go to the same uma_domain. */ 135754c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 13588b987a77SJeff Roberson domain = 0; 13598b987a77SJeff Roberson 13605c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 136154c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) 1362099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 13631e0701e1SJeff Roberson else 13649b8db4d0SRyan Libby slab_tohashslab(slab)->uhs_data = mem; 13655c0e403bSJeff Roberson 136654c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_VTOSLAB) 1367099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 1368584061b4SJeff Roberson vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE), 1369584061b4SJeff Roberson zone, slab); 13708355f576SJeff Roberson 1371099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 137286220393SMark Johnston slab->us_flags = sflags; 1373ab3185d1SJeff Roberson slab->us_domain = domain; 13748b987a77SJeff Roberson 13759b78b1f4SJeff Roberson BIT_FILL(keg->uk_ipers, &slab->us_free); 1376ef72505eSJeff Roberson #ifdef INVARIANTS 1377815db204SRyan Libby BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg)); 1378ef72505eSJeff Roberson #endif 1379099a0e58SBosko Milekic 1380b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1381099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 13821e0701e1SJeff Roberson if (keg->uk_init(slab_item(slab, keg, i), 138386220393SMark Johnston keg->uk_size, flags) != 0) 1384b23f72e9SBrian Feldman break; 1385b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1386fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1387727c6918SJeff Roberson goto fail; 1388b23f72e9SBrian Feldman } 1389b23f72e9SBrian Feldman } 13908b987a77SJeff Roberson KEG_LOCK(keg, domain); 13915c0e403bSJeff Roberson 13921431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 13931431a748SGleb Smirnoff slab, keg->uk_name, keg); 13941431a748SGleb Smirnoff 139554c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 1396099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 13978355f576SJeff Roberson 13988b987a77SJeff Roberson /* 13998b987a77SJeff Roberson * If we got a slab here it's safe to mark it partially used 14008b987a77SJeff Roberson * and return. We assume that the caller is going to remove 14018b987a77SJeff Roberson * at least one item. 14028b987a77SJeff Roberson */ 14038b987a77SJeff Roberson dom = &keg->uk_domain[domain]; 14048b987a77SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 14058b987a77SJeff Roberson dom->ud_pages += keg->uk_ppera; 14068b987a77SJeff Roberson dom->ud_free += keg->uk_ipers; 14078355f576SJeff Roberson 14088355f576SJeff Roberson return (slab); 1409727c6918SJeff Roberson 1410727c6918SJeff Roberson fail: 1411727c6918SJeff Roberson return (NULL); 14128355f576SJeff Roberson } 14138355f576SJeff Roberson 14148355f576SJeff Roberson /* 1415009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1416009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1417009b6fcbSJeff Roberson * the VM is ready. 1418009b6fcbSJeff Roberson */ 1419009b6fcbSJeff Roberson static void * 1420ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1421ab3185d1SJeff Roberson int wait) 1422009b6fcbSJeff Roberson { 1423*a81c400eSJeff Roberson vm_paddr_t pa; 1424*a81c400eSJeff Roberson vm_page_t m; 1425ac0a6fd0SGleb Smirnoff void *mem; 1426ac0a6fd0SGleb Smirnoff int pages; 1427*a81c400eSJeff Roberson int i; 1428099a0e58SBosko Milekic 1429f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1430f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1431*a81c400eSJeff Roberson 1432f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1433*a81c400eSJeff Roberson m = vm_page_alloc_contig_domain(NULL, 0, domain, 1434*a81c400eSJeff Roberson malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages, 1435*a81c400eSJeff Roberson (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT); 1436*a81c400eSJeff Roberson if (m == NULL) 1437*a81c400eSJeff Roberson return (NULL); 1438*a81c400eSJeff Roberson 1439*a81c400eSJeff Roberson pa = VM_PAGE_TO_PHYS(m); 1440*a81c400eSJeff Roberson for (i = 0; i < pages; i++, pa += PAGE_SIZE) { 1441*a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1442*a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1443*a81c400eSJeff Roberson if ((wait & M_NODUMP) == 0) 1444*a81c400eSJeff Roberson dump_add_page(pa); 1445*a81c400eSJeff Roberson #endif 1446*a81c400eSJeff Roberson } 1447*a81c400eSJeff Roberson /* Allocate KVA and indirectly advance bootmem. */ 1448*a81c400eSJeff Roberson mem = (void *)pmap_map(&bootmem, m->phys_addr, 1449*a81c400eSJeff Roberson m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE); 1450*a81c400eSJeff Roberson if ((wait & M_ZERO) != 0) 1451*a81c400eSJeff Roberson bzero(mem, pages * PAGE_SIZE); 1452f7d35785SGleb Smirnoff 1453f7d35785SGleb Smirnoff return (mem); 1454f7d35785SGleb Smirnoff } 1455f7d35785SGleb Smirnoff 1456*a81c400eSJeff Roberson static void 1457*a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes) 1458*a81c400eSJeff Roberson { 1459*a81c400eSJeff Roberson vm_offset_t va; 1460*a81c400eSJeff Roberson vm_page_t m; 1461*a81c400eSJeff Roberson 1462*a81c400eSJeff Roberson va = (vm_offset_t)mem; 1463*a81c400eSJeff Roberson m = PHYS_TO_VM_PAGE(pmap_kextract(va)); 1464*a81c400eSJeff Roberson pmap_remove(kernel_pmap, va, va + bytes); 1465*a81c400eSJeff Roberson for (; bytes != 0; bytes -= PAGE_SIZE, m++) { 1466*a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1467*a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1468*a81c400eSJeff Roberson dump_drop_page(VM_PAGE_TO_PHYS(m)); 1469*a81c400eSJeff Roberson #endif 1470*a81c400eSJeff Roberson vm_page_unwire_noq(m); 1471*a81c400eSJeff Roberson vm_page_free(m); 1472*a81c400eSJeff Roberson } 1473*a81c400eSJeff Roberson } 1474*a81c400eSJeff Roberson 1475f7d35785SGleb Smirnoff /* 14768355f576SJeff Roberson * Allocates a number of pages from the system 14778355f576SJeff Roberson * 14788355f576SJeff Roberson * Arguments: 14798355f576SJeff Roberson * bytes The number of bytes requested 14808355f576SJeff Roberson * wait Shall we wait? 14818355f576SJeff Roberson * 14828355f576SJeff Roberson * Returns: 14838355f576SJeff Roberson * A pointer to the alloced memory or possibly 14848355f576SJeff Roberson * NULL if M_NOWAIT is set. 14858355f576SJeff Roberson */ 14868355f576SJeff Roberson static void * 1487ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1488ab3185d1SJeff Roberson int wait) 14898355f576SJeff Roberson { 14908355f576SJeff Roberson void *p; /* Returned page */ 14918355f576SJeff Roberson 14922e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 14939978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 14948355f576SJeff Roberson 14958355f576SJeff Roberson return (p); 14968355f576SJeff Roberson } 14978355f576SJeff Roberson 1498ab3059a8SMatt Macy static void * 1499ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1500ab3059a8SMatt Macy int wait) 1501ab3059a8SMatt Macy { 1502ab3059a8SMatt Macy struct pglist alloctail; 1503ab3059a8SMatt Macy vm_offset_t addr, zkva; 1504ab3059a8SMatt Macy int cpu, flags; 1505ab3059a8SMatt Macy vm_page_t p, p_next; 1506ab3059a8SMatt Macy #ifdef NUMA 1507ab3059a8SMatt Macy struct pcpu *pc; 1508ab3059a8SMatt Macy #endif 1509ab3059a8SMatt Macy 1510ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1511ab3059a8SMatt Macy 1512013072f0SMark Johnston TAILQ_INIT(&alloctail); 1513ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1514013072f0SMark Johnston malloc2vm_flags(wait); 1515013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1516ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1517ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1518ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1519ab3059a8SMatt Macy } else { 1520ab3059a8SMatt Macy #ifndef NUMA 1521ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1522ab3059a8SMatt Macy #else 1523ab3059a8SMatt Macy pc = pcpu_find(cpu); 1524ab3059a8SMatt Macy p = vm_page_alloc_domain(NULL, 0, pc->pc_domain, flags); 1525ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1526ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1527ab3059a8SMatt Macy #endif 1528ab3059a8SMatt Macy } 1529ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1530ab3059a8SMatt Macy goto fail; 1531ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1532ab3059a8SMatt Macy } 1533ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1534ab3059a8SMatt Macy goto fail; 1535ab3059a8SMatt Macy zkva = addr; 1536ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1537ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1538ab3059a8SMatt Macy zkva += PAGE_SIZE; 1539ab3059a8SMatt Macy } 1540ab3059a8SMatt Macy return ((void*)addr); 1541ab3059a8SMatt Macy fail: 1542ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 154388ea538aSMark Johnston vm_page_unwire_noq(p); 1544ab3059a8SMatt Macy vm_page_free(p); 1545ab3059a8SMatt Macy } 1546ab3059a8SMatt Macy return (NULL); 1547ab3059a8SMatt Macy } 1548ab3059a8SMatt Macy 15498355f576SJeff Roberson /* 15508355f576SJeff Roberson * Allocates a number of pages from within an object 15518355f576SJeff Roberson * 15528355f576SJeff Roberson * Arguments: 15538355f576SJeff Roberson * bytes The number of bytes requested 15548355f576SJeff Roberson * wait Shall we wait? 15558355f576SJeff Roberson * 15568355f576SJeff Roberson * Returns: 15578355f576SJeff Roberson * A pointer to the alloced memory or possibly 15588355f576SJeff Roberson * NULL if M_NOWAIT is set. 15598355f576SJeff Roberson */ 15608355f576SJeff Roberson static void * 1561ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1562ab3185d1SJeff Roberson int wait) 15638355f576SJeff Roberson { 1564a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1565a4915c21SAttilio Rao u_long npages; 1566b245ac95SAlan Cox vm_offset_t retkva, zkva; 1567a4915c21SAttilio Rao vm_page_t p, p_next; 1568e20a199fSJeff Roberson uma_keg_t keg; 15698355f576SJeff Roberson 1570a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1571bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1572a4915c21SAttilio Rao 1573a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1574a4915c21SAttilio Rao while (npages > 0) { 1575ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 15768d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1577772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1578772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1579a4915c21SAttilio Rao if (p != NULL) { 1580a4915c21SAttilio Rao /* 1581a4915c21SAttilio Rao * Since the page does not belong to an object, its 1582a4915c21SAttilio Rao * listq is unused. 1583a4915c21SAttilio Rao */ 1584a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1585a4915c21SAttilio Rao npages--; 1586a4915c21SAttilio Rao continue; 1587a4915c21SAttilio Rao } 15888355f576SJeff Roberson /* 1589a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1590a4915c21SAttilio Rao * exit. 15918355f576SJeff Roberson */ 1592a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 159388ea538aSMark Johnston vm_page_unwire_noq(p); 1594b245ac95SAlan Cox vm_page_free(p); 1595b245ac95SAlan Cox } 1596a4915c21SAttilio Rao return (NULL); 1597b245ac95SAlan Cox } 15988355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1599a4915c21SAttilio Rao zkva = keg->uk_kva + 1600a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1601a4915c21SAttilio Rao retkva = zkva; 1602a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1603a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1604a4915c21SAttilio Rao zkva += PAGE_SIZE; 1605a4915c21SAttilio Rao } 16068355f576SJeff Roberson 16078355f576SJeff Roberson return ((void *)retkva); 16088355f576SJeff Roberson } 16098355f576SJeff Roberson 16108355f576SJeff Roberson /* 16118355f576SJeff Roberson * Frees a number of pages to the system 16128355f576SJeff Roberson * 16138355f576SJeff Roberson * Arguments: 16148355f576SJeff Roberson * mem A pointer to the memory to be freed 16158355f576SJeff Roberson * size The size of the memory being freed 16168355f576SJeff Roberson * flags The original p->us_flags field 16178355f576SJeff Roberson * 16188355f576SJeff Roberson * Returns: 16198355f576SJeff Roberson * Nothing 16208355f576SJeff Roberson */ 16218355f576SJeff Roberson static void 1622f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 16238355f576SJeff Roberson { 16243370c5bfSJeff Roberson 1625*a81c400eSJeff Roberson if ((flags & UMA_SLAB_BOOT) != 0) { 1626*a81c400eSJeff Roberson startup_free(mem, size); 1627*a81c400eSJeff Roberson return; 1628*a81c400eSJeff Roberson } 1629*a81c400eSJeff Roberson 163049bfa624SAlan Cox if ((flags & UMA_SLAB_KERNEL) == 0) 1631b5345ef1SJustin Hibbits panic("UMA: page_free used with invalid flags %x", flags); 16328355f576SJeff Roberson 163349bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 16348355f576SJeff Roberson } 16358355f576SJeff Roberson 16368355f576SJeff Roberson /* 1637ab3059a8SMatt Macy * Frees pcpu zone allocations 1638ab3059a8SMatt Macy * 1639ab3059a8SMatt Macy * Arguments: 1640ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1641ab3059a8SMatt Macy * size The size of the memory being freed 1642ab3059a8SMatt Macy * flags The original p->us_flags field 1643ab3059a8SMatt Macy * 1644ab3059a8SMatt Macy * Returns: 1645ab3059a8SMatt Macy * Nothing 1646ab3059a8SMatt Macy */ 1647ab3059a8SMatt Macy static void 1648ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1649ab3059a8SMatt Macy { 1650ab3059a8SMatt Macy vm_offset_t sva, curva; 1651ab3059a8SMatt Macy vm_paddr_t paddr; 1652ab3059a8SMatt Macy vm_page_t m; 1653ab3059a8SMatt Macy 1654ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 1655ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1656ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1657ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1658ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 165988ea538aSMark Johnston vm_page_unwire_noq(m); 1660ab3059a8SMatt Macy vm_page_free(m); 1661ab3059a8SMatt Macy } 1662ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1663ab3059a8SMatt Macy kva_free(sva, size); 1664ab3059a8SMatt Macy } 1665ab3059a8SMatt Macy 1666ab3059a8SMatt Macy 1667ab3059a8SMatt Macy /* 16688355f576SJeff Roberson * Zero fill initializer 16698355f576SJeff Roberson * 16708355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 16718355f576SJeff Roberson */ 1672b23f72e9SBrian Feldman static int 1673b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 16748355f576SJeff Roberson { 16758355f576SJeff Roberson bzero(mem, size); 1676b23f72e9SBrian Feldman return (0); 16778355f576SJeff Roberson } 16788355f576SJeff Roberson 1679815db204SRyan Libby #ifdef INVARIANTS 1680815db204SRyan Libby struct noslabbits * 1681815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1682815db204SRyan Libby { 1683815db204SRyan Libby 1684815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1685815db204SRyan Libby } 1686815db204SRyan Libby #endif 1687815db204SRyan Libby 16888355f576SJeff Roberson /* 16899b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 16909b78b1f4SJeff Roberson */ 16919b78b1f4SJeff Roberson size_t 16929b78b1f4SJeff Roberson slab_sizeof(int nitems) 16939b78b1f4SJeff Roberson { 16949b78b1f4SJeff Roberson size_t s; 16959b78b1f4SJeff Roberson 1696815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 16979b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 16989b78b1f4SJeff Roberson } 16999b78b1f4SJeff Roberson 17009b78b1f4SJeff Roberson /* 17019b78b1f4SJeff Roberson * Size of memory for embedded slabs (!OFFPAGE). 17029b78b1f4SJeff Roberson */ 17039b78b1f4SJeff Roberson size_t 17049b78b1f4SJeff Roberson slab_space(int nitems) 17059b78b1f4SJeff Roberson { 17069b78b1f4SJeff Roberson return (UMA_SLAB_SIZE - slab_sizeof(nitems)); 17079b78b1f4SJeff Roberson } 17089b78b1f4SJeff Roberson 17094a8b575cSRyan Libby #define UMA_FIXPT_SHIFT 31 17104a8b575cSRyan Libby #define UMA_FRAC_FIXPT(n, d) \ 17114a8b575cSRyan Libby ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d))) 17124a8b575cSRyan Libby #define UMA_FIXPT_PCT(f) \ 17134a8b575cSRyan Libby ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT)) 17144a8b575cSRyan Libby #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100) 17154a8b575cSRyan Libby #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE) 17164a8b575cSRyan Libby 17179b78b1f4SJeff Roberson /* 17184a8b575cSRyan Libby * Compute the number of items that will fit in a slab. If hdr is true, the 17194a8b575cSRyan Libby * item count may be limited to provide space in the slab for an inline slab 17204a8b575cSRyan Libby * header. Otherwise, all slab space will be provided for item storage. 17214a8b575cSRyan Libby */ 17224a8b575cSRyan Libby static u_int 17234a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr) 17244a8b575cSRyan Libby { 17254a8b575cSRyan Libby u_int ipers; 17264a8b575cSRyan Libby u_int padpi; 17274a8b575cSRyan Libby 17284a8b575cSRyan Libby /* The padding between items is not needed after the last item. */ 17294a8b575cSRyan Libby padpi = rsize - size; 17304a8b575cSRyan Libby 17314a8b575cSRyan Libby if (hdr) { 17324a8b575cSRyan Libby /* 17334a8b575cSRyan Libby * Start with the maximum item count and remove items until 17344a8b575cSRyan Libby * the slab header first alongside the allocatable memory. 17354a8b575cSRyan Libby */ 17364a8b575cSRyan Libby for (ipers = MIN(SLAB_MAX_SETSIZE, 17374a8b575cSRyan Libby (slabsize + padpi - slab_sizeof(1)) / rsize); 17384a8b575cSRyan Libby ipers > 0 && 17394a8b575cSRyan Libby ipers * rsize - padpi + slab_sizeof(ipers) > slabsize; 17404a8b575cSRyan Libby ipers--) 17414a8b575cSRyan Libby continue; 17424a8b575cSRyan Libby } else { 17434a8b575cSRyan Libby ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE); 17444a8b575cSRyan Libby } 17454a8b575cSRyan Libby 17464a8b575cSRyan Libby return (ipers); 17474a8b575cSRyan Libby } 17484a8b575cSRyan Libby 17494a8b575cSRyan Libby /* 17504a8b575cSRyan Libby * Compute the number of items that will fit in a slab for a startup zone. 17519b78b1f4SJeff Roberson */ 17529b78b1f4SJeff Roberson int 17539b78b1f4SJeff Roberson slab_ipers(size_t size, int align) 17549b78b1f4SJeff Roberson { 17559b78b1f4SJeff Roberson int rsize; 17569b78b1f4SJeff Roberson 17574a8b575cSRyan Libby rsize = roundup(size, align + 1); /* Assume no CACHESPREAD */ 17584a8b575cSRyan Libby return (slab_ipers_hdr(size, rsize, UMA_SLAB_SIZE, true)); 17599b78b1f4SJeff Roberson } 17609b78b1f4SJeff Roberson 17619b78b1f4SJeff Roberson /* 17624a8b575cSRyan Libby * Determine the format of a uma keg. This determines where the slab header 17634a8b575cSRyan Libby * will be placed (inline or offpage) and calculates ipers, rsize, and ppera. 17648355f576SJeff Roberson * 17658355f576SJeff Roberson * Arguments 1766e20a199fSJeff Roberson * keg The zone we should initialize 17678355f576SJeff Roberson * 17688355f576SJeff Roberson * Returns 17698355f576SJeff Roberson * Nothing 17708355f576SJeff Roberson */ 17718355f576SJeff Roberson static void 17724a8b575cSRyan Libby keg_layout(uma_keg_t keg) 17738355f576SJeff Roberson { 17744a8b575cSRyan Libby u_int alignsize; 17754a8b575cSRyan Libby u_int eff; 17764a8b575cSRyan Libby u_int eff_offpage; 17774a8b575cSRyan Libby u_int format; 17784a8b575cSRyan Libby u_int ipers; 17794a8b575cSRyan Libby u_int ipers_offpage; 17804a8b575cSRyan Libby u_int pages; 1781244f4554SBosko Milekic u_int rsize; 1782a55ebb7cSAndriy Gapon u_int slabsize; 17838355f576SJeff Roberson 17844a8b575cSRyan Libby KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 17854a8b575cSRyan Libby (keg->uk_size <= UMA_PCPU_ALLOC_SIZE && 17864a8b575cSRyan Libby (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0), 17874a8b575cSRyan Libby ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b", 17884a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_size, keg->uk_flags, 17894a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 17904a8b575cSRyan Libby KASSERT((keg->uk_flags & 17914a8b575cSRyan Libby (UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY)) == 0 || 17924a8b575cSRyan Libby (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0, 17934a8b575cSRyan Libby ("%s: incompatible flags 0x%b", __func__, keg->uk_flags, 17944a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 1795e28a647dSGleb Smirnoff 17964a8b575cSRyan Libby alignsize = keg->uk_align + 1; 17974a8b575cSRyan Libby format = 0; 17984a8b575cSRyan Libby ipers = 0; 1799ad97af7eSGleb Smirnoff 1800ef72505eSJeff Roberson /* 1801ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1802ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1803ef72505eSJeff Roberson * allocation bits for we round it up. 1804ef72505eSJeff Roberson */ 18059b8db4d0SRyan Libby rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT); 18064a8b575cSRyan Libby rsize = roundup2(rsize, alignsize); 1807ad97af7eSGleb Smirnoff 18084a8b575cSRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) { 18094a8b575cSRyan Libby slabsize = UMA_PCPU_ALLOC_SIZE; 18104a8b575cSRyan Libby pages = mp_maxid + 1; 18114a8b575cSRyan Libby } else if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) { 18129b78b1f4SJeff Roberson /* 18134a8b575cSRyan Libby * We want one item to start on every align boundary in a page. 18144a8b575cSRyan Libby * To do this we will span pages. We will also extend the item 18154a8b575cSRyan Libby * by the size of align if it is an even multiple of align. 18164a8b575cSRyan Libby * Otherwise, it would fall on the same boundary every time. 18179b78b1f4SJeff Roberson */ 18184a8b575cSRyan Libby if ((rsize & alignsize) == 0) 18194a8b575cSRyan Libby rsize += alignsize; 18204a8b575cSRyan Libby slabsize = rsize * (PAGE_SIZE / alignsize); 18214a8b575cSRyan Libby slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE); 18224a8b575cSRyan Libby slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE); 18234a8b575cSRyan Libby pages = howmany(slabsize, PAGE_SIZE); 18244a8b575cSRyan Libby slabsize = ptoa(pages); 18254a8b575cSRyan Libby } else { 18264a8b575cSRyan Libby /* 18274a8b575cSRyan Libby * Choose a slab size of as many pages as it takes to represent 18284a8b575cSRyan Libby * a single item. We will then try to fit as many additional 18294a8b575cSRyan Libby * items into the slab as possible. At some point, we may want 18304a8b575cSRyan Libby * to increase the slab size for awkward item sizes in order to 18314a8b575cSRyan Libby * increase efficiency. 18324a8b575cSRyan Libby */ 18334a8b575cSRyan Libby pages = howmany(keg->uk_size, PAGE_SIZE); 18344a8b575cSRyan Libby slabsize = ptoa(pages); 18351ca6ed45SGleb Smirnoff } 1836ad97af7eSGleb Smirnoff 18374a8b575cSRyan Libby /* Evaluate an inline slab layout. */ 18384a8b575cSRyan Libby if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0) 18394a8b575cSRyan Libby ipers = slab_ipers_hdr(keg->uk_size, rsize, slabsize, true); 18404a8b575cSRyan Libby 18414a8b575cSRyan Libby /* TODO: vm_page-embedded slab. */ 1842244f4554SBosko Milekic 184320e8e865SBosko Milekic /* 1844244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 184520e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 18466fd34d6fSJeff Roberson * may end up going to the VM for slabs which we do not 18476fd34d6fSJeff Roberson * want to do if we're UMA_ZFLAG_CACHEONLY as a result 18486fd34d6fSJeff Roberson * of UMA_ZONE_VM, which clearly forbids it. 184920e8e865SBosko Milekic */ 18504a8b575cSRyan Libby if ((keg->uk_flags & 18514a8b575cSRyan Libby (UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY)) != 0) { 18524a8b575cSRyan Libby if (ipers == 0) { 18534a8b575cSRyan Libby /* We need an extra page for the slab header. */ 18544a8b575cSRyan Libby pages++; 18554a8b575cSRyan Libby slabsize = ptoa(pages); 18564a8b575cSRyan Libby ipers = slab_ipers_hdr(keg->uk_size, rsize, slabsize, 18574a8b575cSRyan Libby true); 18584a8b575cSRyan Libby } 18594a8b575cSRyan Libby goto out; 186054c5ae80SRyan Libby } 1861244f4554SBosko Milekic 1862ef72505eSJeff Roberson /* 18634a8b575cSRyan Libby * See if using an OFFPAGE slab will improve our efficiency. 18644a8b575cSRyan Libby * Only do this if we are below our efficiency threshold. 1865ef72505eSJeff Roberson * 1866ef72505eSJeff Roberson * XXX We could try growing slabsize to limit max waste as well. 1867ef72505eSJeff Roberson * Historically this was not done because the VM could not 1868ef72505eSJeff Roberson * efficiently handle contiguous allocations. 1869ef72505eSJeff Roberson */ 18704a8b575cSRyan Libby eff = UMA_FRAC_FIXPT(ipers * rsize, slabsize); 18714a8b575cSRyan Libby ipers_offpage = slab_ipers_hdr(keg->uk_size, rsize, slabsize, false); 18724a8b575cSRyan Libby eff_offpage = UMA_FRAC_FIXPT(ipers_offpage * rsize, 18739b8db4d0SRyan Libby slabsize + slabzone(ipers_offpage)->uz_keg->uk_rsize); 18744a8b575cSRyan Libby if (ipers == 0 || (eff < UMA_MIN_EFF && eff < eff_offpage)) { 18754a8b575cSRyan Libby CTR5(KTR_UMA, "UMA decided we need offpage slab headers for " 18764a8b575cSRyan Libby "keg: %s(%p), minimum efficiency allowed = %u%%, " 1877e63a1c2fSRyan Libby "old efficiency = %u%%, offpage efficiency = %u%%", 18784a8b575cSRyan Libby keg->uk_name, keg, UMA_FIXPT_PCT(UMA_MIN_EFF), 18794a8b575cSRyan Libby UMA_FIXPT_PCT(eff), UMA_FIXPT_PCT(eff_offpage)); 18804a8b575cSRyan Libby format = UMA_ZFLAG_OFFPAGE; 18814a8b575cSRyan Libby ipers = ipers_offpage; 18828355f576SJeff Roberson } 1883ad97af7eSGleb Smirnoff 18844a8b575cSRyan Libby out: 18854a8b575cSRyan Libby /* 18864a8b575cSRyan Libby * How do we find the slab header if it is offpage or if not all item 18874a8b575cSRyan Libby * start addresses are in the same page? We could solve the latter 18884a8b575cSRyan Libby * case with vaddr alignment, but we don't. 18894a8b575cSRyan Libby */ 18904a8b575cSRyan Libby if ((format & UMA_ZFLAG_OFFPAGE) != 0 || 18914a8b575cSRyan Libby (ipers - 1) * rsize >= PAGE_SIZE) { 189254c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0) 18934a8b575cSRyan Libby format |= UMA_ZFLAG_HASH; 189454c5ae80SRyan Libby else 18954a8b575cSRyan Libby format |= UMA_ZFLAG_VTOSLAB; 189654c5ae80SRyan Libby } 18974a8b575cSRyan Libby keg->uk_ipers = ipers; 1898e20a199fSJeff Roberson keg->uk_rsize = rsize; 18994a8b575cSRyan Libby keg->uk_flags |= format; 1900e20a199fSJeff Roberson keg->uk_ppera = pages; 1901e63a1c2fSRyan Libby CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u", 19024a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_flags, rsize, ipers, pages); 19034a8b575cSRyan Libby KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 19044a8b575cSRyan Libby ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__, 19054a8b575cSRyan Libby keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize, ipers, 19064a8b575cSRyan Libby pages)); 1907e20a199fSJeff Roberson } 1908e20a199fSJeff Roberson 19098355f576SJeff Roberson /* 1910099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 1911099a0e58SBosko Milekic * the keg onto the global keg list. 19128355f576SJeff Roberson * 19138355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 1914099a0e58SBosko Milekic * udata Actually uma_kctor_args 1915099a0e58SBosko Milekic */ 1916b23f72e9SBrian Feldman static int 1917b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 1918099a0e58SBosko Milekic { 1919099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 1920099a0e58SBosko Milekic uma_keg_t keg = mem; 1921099a0e58SBosko Milekic uma_zone_t zone; 19228b987a77SJeff Roberson int i; 1923099a0e58SBosko Milekic 1924099a0e58SBosko Milekic bzero(keg, size); 1925099a0e58SBosko Milekic keg->uk_size = arg->size; 1926099a0e58SBosko Milekic keg->uk_init = arg->uminit; 1927099a0e58SBosko Milekic keg->uk_fini = arg->fini; 1928099a0e58SBosko Milekic keg->uk_align = arg->align; 19296fd34d6fSJeff Roberson keg->uk_reserve = 0; 1930099a0e58SBosko Milekic keg->uk_flags = arg->flags; 1931099a0e58SBosko Milekic 1932099a0e58SBosko Milekic /* 1933194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 1934dfe13344SJeff Roberson * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which 1935dfe13344SJeff Roberson * case the iterator is never run. 1936194a979eSMark Johnston */ 1937194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 1938194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 1939194a979eSMark Johnston 1940194a979eSMark Johnston /* 1941099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 1942099a0e58SBosko Milekic */ 1943099a0e58SBosko Milekic zone = arg->zone; 1944e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 1945099a0e58SBosko Milekic 1946099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_VM) 1947099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 1948099a0e58SBosko Milekic 1949099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 1950099a0e58SBosko Milekic keg->uk_init = zero_init; 1951099a0e58SBosko Milekic 1952cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 195354c5ae80SRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 1954e20a199fSJeff Roberson 195554c5ae80SRyan Libby #ifndef SMP 1956ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 1957ad97af7eSGleb Smirnoff #endif 1958ad97af7eSGleb Smirnoff 19594a8b575cSRyan Libby keg_layout(keg); 1960099a0e58SBosko Milekic 19618b987a77SJeff Roberson /* 1962dfe13344SJeff Roberson * Use a first-touch NUMA policy for all kegs that pmap_extract() 1963dfe13344SJeff Roberson * will work on with the exception of critical VM structures 1964dfe13344SJeff Roberson * necessary for paging. 1965dfe13344SJeff Roberson * 1966dfe13344SJeff Roberson * Zones may override the default by specifying either. 19678b987a77SJeff Roberson */ 1968dfe13344SJeff Roberson #ifdef NUMA 1969dfe13344SJeff Roberson if ((keg->uk_flags & 197054c5ae80SRyan Libby (UMA_ZFLAG_HASH | UMA_ZONE_VM | UMA_ZONE_ROUNDROBIN)) == 0) 1971dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_FIRSTTOUCH; 1972dfe13344SJeff Roberson else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0) 1973dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_ROUNDROBIN; 19748b987a77SJeff Roberson #endif 19758b987a77SJeff Roberson 1976099a0e58SBosko Milekic /* 1977099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 1978099a0e58SBosko Milekic * startup cache until the vm is ready. 1979099a0e58SBosko Milekic */ 198077e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 1981*a81c400eSJeff Roberson if (keg->uk_ppera == 1) 198277e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 1983*a81c400eSJeff Roberson else 19848cd02d00SAlan Cox #endif 1985*a81c400eSJeff Roberson if (booted < BOOT_KVA) 1986*a81c400eSJeff Roberson keg->uk_allocf = startup_alloc; 1987ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 1988ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 198977e19437SGleb Smirnoff else 199077e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 199177e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 199277e19437SGleb Smirnoff if (keg->uk_ppera == 1) 199377e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 199477e19437SGleb Smirnoff else 199577e19437SGleb Smirnoff #endif 1996ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 1997ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 1998ab3059a8SMatt Macy else 199977e19437SGleb Smirnoff keg->uk_freef = page_free; 2000099a0e58SBosko Milekic 2001099a0e58SBosko Milekic /* 20028b987a77SJeff Roberson * Initialize keg's locks. 2003099a0e58SBosko Milekic */ 20048b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 20058b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 2006099a0e58SBosko Milekic 2007099a0e58SBosko Milekic /* 2008099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 20099b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 20109b78b1f4SJeff Roberson * definition. 2011099a0e58SBosko Milekic */ 201254c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) { 20139b78b1f4SJeff Roberson size_t shsize; 20149b78b1f4SJeff Roberson 20159b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 20169b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 2017244f4554SBosko Milekic /* 2018244f4554SBosko Milekic * The only way the following is possible is if with our 2019244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 2020244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 2021244f4554SBosko Milekic * mathematically possible for all cases, so we make 2022244f4554SBosko Milekic * sure here anyway. 2023244f4554SBosko Milekic */ 20249b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 20253d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 20263d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 2027099a0e58SBosko Milekic } 2028099a0e58SBosko Milekic 202954c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 20303b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 2031099a0e58SBosko Milekic 2032e63a1c2fSRyan Libby CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone); 2033099a0e58SBosko Milekic 2034099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 2035099a0e58SBosko Milekic 2036111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2037099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2038111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2039b23f72e9SBrian Feldman return (0); 2040099a0e58SBosko Milekic } 2041099a0e58SBosko Milekic 20422efcc8cbSGleb Smirnoff static void 2043*a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused) 2044*a81c400eSJeff Roberson { 2045*a81c400eSJeff Roberson uma_keg_t keg; 2046*a81c400eSJeff Roberson 2047*a81c400eSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 2048*a81c400eSJeff Roberson return; 2049*a81c400eSJeff Roberson KEG_GET(zone, keg); 2050*a81c400eSJeff Roberson if (keg->uk_allocf == startup_alloc) 2051*a81c400eSJeff Roberson keg->uk_allocf = page_alloc; 2052*a81c400eSJeff Roberson } 2053*a81c400eSJeff Roberson 2054*a81c400eSJeff Roberson static void 205520a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 20562efcc8cbSGleb Smirnoff { 20572efcc8cbSGleb Smirnoff 20582efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 20592efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 20602efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 20612efcc8cbSGleb Smirnoff } 20622efcc8cbSGleb Smirnoff 206320a4e154SJeff Roberson static void 206420a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 206520a4e154SJeff Roberson { 206620a4e154SJeff Roberson uma_zone_domain_t zdom; 20678b987a77SJeff Roberson uma_domain_t dom; 206820a4e154SJeff Roberson uma_keg_t keg; 206920a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 20703b490537SJeff Roberson int domains, i, cnt; 207120a4e154SJeff Roberson static const char *nokeg = "cache zone"; 207220a4e154SJeff Roberson char *c; 207320a4e154SJeff Roberson 207420a4e154SJeff Roberson /* 207520a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 207620a4e154SJeff Roberson * any special characters and handling dups by appending 207720a4e154SJeff Roberson * an index. 207820a4e154SJeff Roberson */ 207920a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 20803b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 20813b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 20823b490537SJeff Roberson cnt /= 10; 20833b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 20843b490537SJeff Roberson M_UMA, M_WAITOK); 208520a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 208620a4e154SJeff Roberson zone->uz_namecnt); 208720a4e154SJeff Roberson } else 208820a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 208920a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 209020a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 209120a4e154SJeff Roberson *c = '_'; 209220a4e154SJeff Roberson 209320a4e154SJeff Roberson /* 209420a4e154SJeff Roberson * Basic parameters at the root. 209520a4e154SJeff Roberson */ 209620a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 209720a4e154SJeff Roberson OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, ""); 209820a4e154SJeff Roberson oid = zone->uz_oid; 209920a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210020a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 21016d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21026d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 21036d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 210420a4e154SJeff Roberson "Allocator configuration flags"); 210520a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210620a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 210720a4e154SJeff Roberson "Desired per-cpu cache size"); 210820a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210920a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 211020a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 211120a4e154SJeff Roberson 211220a4e154SJeff Roberson /* 211320a4e154SJeff Roberson * keg if present. 211420a4e154SJeff Roberson */ 211554c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 21168b987a77SJeff Roberson domains = vm_ndomains; 21178b987a77SJeff Roberson else 21188b987a77SJeff Roberson domains = 1; 211920a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 212020a4e154SJeff Roberson "keg", CTLFLAG_RD, NULL, ""); 212120a4e154SJeff Roberson keg = zone->uz_keg; 21223b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 212320a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 212420a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 212520a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 212620a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 212720a4e154SJeff Roberson "Real object size with alignment"); 212820a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 212920a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 213020a4e154SJeff Roberson "pages per-slab allocation"); 213120a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213220a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 213320a4e154SJeff Roberson "items available per-slab"); 213420a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213520a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 213620a4e154SJeff Roberson "item alignment mask"); 2137f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2138f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2139f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2140f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 21418b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 21428b987a77SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 21438b987a77SJeff Roberson for (i = 0; i < domains; i++) { 21448b987a77SJeff Roberson dom = &keg->uk_domain[i]; 21458b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 21468b987a77SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, 21478b987a77SJeff Roberson NULL, ""); 21488b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21498b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 21508b987a77SJeff Roberson "Total pages currently allocated from VM"); 21518b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21528b987a77SJeff Roberson "free", CTLFLAG_RD, &dom->ud_free, 0, 21538b987a77SJeff Roberson "items free in the slab layer"); 21548b987a77SJeff Roberson } 215520a4e154SJeff Roberson } else 215620a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 215720a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 215820a4e154SJeff Roberson 215920a4e154SJeff Roberson /* 216020a4e154SJeff Roberson * Information about zone limits. 216120a4e154SJeff Roberson */ 216220a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 216320a4e154SJeff Roberson "limit", CTLFLAG_RD, NULL, ""); 21644bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21654bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 21664bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 21674bd61e19SJeff Roberson "current number of allocated items if limit is set"); 216820a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 216920a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 217020a4e154SJeff Roberson "Maximum number of cached items"); 217120a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 217220a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 217320a4e154SJeff Roberson "Number of threads sleeping at limit"); 217420a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 217520a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 217620a4e154SJeff Roberson "Total zone limit sleeps"); 21774bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21784bd61e19SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bkt_max, 0, 21794bd61e19SJeff Roberson "Maximum number of items in the bucket cache"); 21804bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21814bd61e19SJeff Roberson "bucket_cnt", CTLFLAG_RD, &zone->uz_bkt_count, 0, 21824bd61e19SJeff Roberson "Number of items in the bucket cache"); 218320a4e154SJeff Roberson 218420a4e154SJeff Roberson /* 21858b987a77SJeff Roberson * Per-domain zone information. 218620a4e154SJeff Roberson */ 218720a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 218820a4e154SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 2189dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0) 21908b987a77SJeff Roberson domains = 1; 219120a4e154SJeff Roberson for (i = 0; i < domains; i++) { 219220a4e154SJeff Roberson zdom = &zone->uz_domain[i]; 219320a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 219420a4e154SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, ""); 219520a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 219620a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 219720a4e154SJeff Roberson "number of items in this domain"); 219820a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 219920a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 220020a4e154SJeff Roberson "maximum item count in this period"); 220120a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 220220a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 220320a4e154SJeff Roberson "minimum item count in this period"); 220420a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 220520a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 220620a4e154SJeff Roberson "Working set size"); 220720a4e154SJeff Roberson } 220820a4e154SJeff Roberson 220920a4e154SJeff Roberson /* 221020a4e154SJeff Roberson * General statistics. 221120a4e154SJeff Roberson */ 221220a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 221320a4e154SJeff Roberson "stats", CTLFLAG_RD, NULL, ""); 221420a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 221520a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 221620a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 221720a4e154SJeff Roberson "Current number of allocated items"); 221820a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 221920a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 222020a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 222120a4e154SJeff Roberson "Total allocation calls"); 222220a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 222320a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 222420a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 222520a4e154SJeff Roberson "Total free calls"); 222620a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 222720a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 222820a4e154SJeff Roberson "Number of allocation failures"); 222920a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 223020a4e154SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 0, 223120a4e154SJeff Roberson "Free calls from the wrong domain"); 223220a4e154SJeff Roberson } 223320a4e154SJeff Roberson 223420a4e154SJeff Roberson struct uma_zone_count { 223520a4e154SJeff Roberson const char *name; 223620a4e154SJeff Roberson int count; 223720a4e154SJeff Roberson }; 223820a4e154SJeff Roberson 223920a4e154SJeff Roberson static void 224020a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 224120a4e154SJeff Roberson { 224220a4e154SJeff Roberson struct uma_zone_count *cnt; 224320a4e154SJeff Roberson 224420a4e154SJeff Roberson cnt = arg; 22453b490537SJeff Roberson /* 22463b490537SJeff Roberson * Some zones are rapidly created with identical names and 22473b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 22483b490537SJeff Roberson * Use one greater than the maximum observed for this name. 22493b490537SJeff Roberson */ 225020a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 22513b490537SJeff Roberson cnt->count = MAX(cnt->count, 22523b490537SJeff Roberson zone->uz_namecnt + 1); 225320a4e154SJeff Roberson } 225420a4e154SJeff Roberson 2255cc7ce83aSJeff Roberson static void 2256cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2257cc7ce83aSJeff Roberson { 2258cc7ce83aSJeff Roberson int i; 2259cc7ce83aSJeff Roberson 2260cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2261cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2262cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2263cc7ce83aSJeff Roberson } 2264cc7ce83aSJeff Roberson } 2265cc7ce83aSJeff Roberson 2266099a0e58SBosko Milekic /* 2267099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2268099a0e58SBosko Milekic * 2269099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2270099a0e58SBosko Milekic * udata Actually uma_zctor_args 22718355f576SJeff Roberson */ 2272b23f72e9SBrian Feldman static int 2273b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 22748355f576SJeff Roberson { 227520a4e154SJeff Roberson struct uma_zone_count cnt; 22768355f576SJeff Roberson struct uma_zctor_args *arg = udata; 22778355f576SJeff Roberson uma_zone_t zone = mem; 2278099a0e58SBosko Milekic uma_zone_t z; 2279099a0e58SBosko Milekic uma_keg_t keg; 228008cfa56eSMark Johnston int i; 22818355f576SJeff Roberson 22828355f576SJeff Roberson bzero(zone, size); 22838355f576SJeff Roberson zone->uz_name = arg->name; 22848355f576SJeff Roberson zone->uz_ctor = arg->ctor; 22858355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2286099a0e58SBosko Milekic zone->uz_init = NULL; 2287099a0e58SBosko Milekic zone->uz_fini = NULL; 2288bf965959SSean Bruno zone->uz_sleeps = 0; 2289c1685086SJeff Roberson zone->uz_xdomain = 0; 229020a4e154SJeff Roberson zone->uz_bucket_size = 0; 229120a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 229220a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2293e20a199fSJeff Roberson zone->uz_flags = 0; 22942f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2295ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 2296ab3185d1SJeff Roberson zone->uz_domain = (struct uma_zone_domain *)&zone->uz_cpu[mp_ncpus]; 2297bb15d1c7SGleb Smirnoff zone->uz_bkt_max = ULONG_MAX; 22982f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2299af526374SJeff Roberson 230020a4e154SJeff Roberson /* Count the number of duplicate names. */ 230120a4e154SJeff Roberson cnt.name = arg->name; 230220a4e154SJeff Roberson cnt.count = 0; 230320a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 230420a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 2305727c6918SJeff Roberson ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 230691d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 23072efcc8cbSGleb Smirnoff 230808cfa56eSMark Johnston for (i = 0; i < vm_ndomains; i++) 230908cfa56eSMark Johnston TAILQ_INIT(&zone->uz_domain[i].uzd_buckets); 231008cfa56eSMark Johnston 2311ca293436SRyan Libby #ifdef INVARIANTS 2312ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2313cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 2314ca293436SRyan Libby #endif 2315ca293436SRyan Libby 23160095a784SJeff Roberson /* 23170095a784SJeff Roberson * This is a pure cache zone, no kegs. 23180095a784SJeff Roberson */ 23190095a784SJeff Roberson if (arg->import) { 2320727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2321727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 23226fd34d6fSJeff Roberson if (arg->flags & UMA_ZONE_VM) 23236fd34d6fSJeff Roberson arg->flags |= UMA_ZFLAG_CACHEONLY; 23246fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2325af526374SJeff Roberson zone->uz_size = arg->size; 23260095a784SJeff Roberson zone->uz_import = arg->import; 23270095a784SJeff Roberson zone->uz_release = arg->release; 23280095a784SJeff Roberson zone->uz_arg = arg->arg; 2329111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 233003175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2331111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2332af526374SJeff Roberson goto out; 23330095a784SJeff Roberson } 23340095a784SJeff Roberson 23350095a784SJeff Roberson /* 23360095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 23370095a784SJeff Roberson */ 2338b75c4efcSAndrew Turner zone->uz_import = zone_import; 2339b75c4efcSAndrew Turner zone->uz_release = zone_release; 23400095a784SJeff Roberson zone->uz_arg = zone; 2341bb15d1c7SGleb Smirnoff keg = arg->keg; 23420095a784SJeff Roberson 2343099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 234420a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 234520a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2346099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 23478355f576SJeff Roberson zone->uz_init = arg->uminit; 2348e221e841SJeff Roberson zone->uz_fini = arg->fini; 2349e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2350111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2351099a0e58SBosko Milekic ZONE_LOCK(zone); 2352099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2353099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2354099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2355099a0e58SBosko Milekic break; 2356099a0e58SBosko Milekic } 2357099a0e58SBosko Milekic } 2358099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2359111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2360e20a199fSJeff Roberson } else if (keg == NULL) { 2361e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2362e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2363b23f72e9SBrian Feldman return (ENOMEM); 2364099a0e58SBosko Milekic } else { 2365099a0e58SBosko Milekic struct uma_kctor_args karg; 2366b23f72e9SBrian Feldman int error; 2367099a0e58SBosko Milekic 2368099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2369099a0e58SBosko Milekic karg.size = arg->size; 2370099a0e58SBosko Milekic karg.uminit = arg->uminit; 2371099a0e58SBosko Milekic karg.fini = arg->fini; 2372099a0e58SBosko Milekic karg.align = arg->align; 2373099a0e58SBosko Milekic karg.flags = arg->flags; 2374099a0e58SBosko Milekic karg.zone = zone; 2375b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2376b23f72e9SBrian Feldman flags); 2377b23f72e9SBrian Feldman if (error) 2378b23f72e9SBrian Feldman return (error); 2379099a0e58SBosko Milekic } 23800095a784SJeff Roberson 238120a4e154SJeff Roberson /* Inherit properties from the keg. */ 2382bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2383e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2384e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2385e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 23868355f576SJeff Roberson 238720a4e154SJeff Roberson out: 2388860bb7a0SMark Johnston if (__predict_true(booted >= BOOT_RUNNING)) { 238920a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 239020a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 239120a4e154SJeff Roberson } else { 239220a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 239320a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 239420a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2395099a0e58SBosko Milekic } 23968355f576SJeff Roberson 23977e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 23987e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 23997e28037aSMark Johnston ("Invalid zone flag combination")); 240020a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 240120a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 240220a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 240320a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 240420a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) 240520a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN; 240620a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 240720a4e154SJeff Roberson zone->uz_bucket_size = 0; 24087e28037aSMark Johnston else 240920a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 241020a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2411cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2412cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2413cc7ce83aSJeff Roberson zone_update_caches(zone); 2414fc03d22bSJeff Roberson 2415b23f72e9SBrian Feldman return (0); 24168355f576SJeff Roberson } 24178355f576SJeff Roberson 24188355f576SJeff Roberson /* 2419099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2420099a0e58SBosko Milekic * table and removes the keg from the global list. 24219c2cd7e5SJeff Roberson * 24229c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 24239c2cd7e5SJeff Roberson * udata unused 24249c2cd7e5SJeff Roberson */ 2425099a0e58SBosko Milekic static void 2426099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2427099a0e58SBosko Milekic { 2428099a0e58SBosko Milekic uma_keg_t keg; 24298b987a77SJeff Roberson uint32_t free, pages; 24308b987a77SJeff Roberson int i; 24319c2cd7e5SJeff Roberson 2432099a0e58SBosko Milekic keg = (uma_keg_t)arg; 24338b987a77SJeff Roberson free = pages = 0; 24348b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 24358b987a77SJeff Roberson free += keg->uk_domain[i].ud_free; 24368b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 24378b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2438099a0e58SBosko Milekic } 24398b987a77SJeff Roberson if (free != 0) 24408b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 24418b987a77SJeff Roberson " Lost %u pages of memory.\n", 24428b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 24438b987a77SJeff Roberson free, pages); 2444099a0e58SBosko Milekic 2445099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2446099a0e58SBosko Milekic } 2447099a0e58SBosko Milekic 2448099a0e58SBosko Milekic /* 2449099a0e58SBosko Milekic * Zone header dtor. 2450099a0e58SBosko Milekic * 2451099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2452099a0e58SBosko Milekic * udata unused 2453099a0e58SBosko Milekic */ 24549c2cd7e5SJeff Roberson static void 24559c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 24569c2cd7e5SJeff Roberson { 24579c2cd7e5SJeff Roberson uma_zone_t zone; 2458099a0e58SBosko Milekic uma_keg_t keg; 24599c2cd7e5SJeff Roberson 24609c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 24619643769aSJeff Roberson 246220a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 246320a4e154SJeff Roberson 2464e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 24659643769aSJeff Roberson cache_drain(zone); 2466099a0e58SBosko Milekic 2467111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2468099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2469111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2470099a0e58SBosko Milekic /* 2471099a0e58SBosko Milekic * XXX there are some races here where 2472099a0e58SBosko Milekic * the zone can be drained but zone lock 2473099a0e58SBosko Milekic * released and then refilled before we 2474099a0e58SBosko Milekic * remove it... we dont care for now 2475099a0e58SBosko Milekic */ 247608cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2477e20a199fSJeff Roberson /* 2478323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2479e20a199fSJeff Roberson */ 2480323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2481323ad386STycho Nightingale keg = zone->uz_keg; 2482111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2483099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2484111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 24850095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 24869c2cd7e5SJeff Roberson } 24872efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 24882efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 24892efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 249020a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2491af526374SJeff Roberson ZONE_LOCK_FINI(zone); 249291d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 2493099a0e58SBosko Milekic } 2494099a0e58SBosko Milekic 2495*a81c400eSJeff Roberson static void 2496*a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg) 2497*a81c400eSJeff Roberson { 2498*a81c400eSJeff Roberson uma_keg_t keg; 2499*a81c400eSJeff Roberson uma_zone_t zone; 2500*a81c400eSJeff Roberson 2501*a81c400eSJeff Roberson LIST_FOREACH(keg, &uma_kegs, uk_link) { 2502*a81c400eSJeff Roberson LIST_FOREACH(zone, &keg->uk_zones, uz_link) 2503*a81c400eSJeff Roberson zfunc(zone, arg); 2504*a81c400eSJeff Roberson } 2505*a81c400eSJeff Roberson LIST_FOREACH(zone, &uma_cachezones, uz_link) 2506*a81c400eSJeff Roberson zfunc(zone, arg); 2507*a81c400eSJeff Roberson } 2508*a81c400eSJeff Roberson 25099c2cd7e5SJeff Roberson /* 25108355f576SJeff Roberson * Traverses every zone in the system and calls a callback 25118355f576SJeff Roberson * 25128355f576SJeff Roberson * Arguments: 25138355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 25148355f576SJeff Roberson * as an argument. 25158355f576SJeff Roberson * 25168355f576SJeff Roberson * Returns: 25178355f576SJeff Roberson * Nothing 25188355f576SJeff Roberson */ 25198355f576SJeff Roberson static void 252020a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 25218355f576SJeff Roberson { 25228355f576SJeff Roberson 2523111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2524*a81c400eSJeff Roberson zone_foreach_unlocked(zfunc, arg); 2525111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 25268355f576SJeff Roberson } 25278355f576SJeff Roberson 2528f4bef67cSGleb Smirnoff /* 2529*a81c400eSJeff Roberson * Initialize the kernel memory allocator. This is done after pages can be 2530*a81c400eSJeff Roberson * allocated but before general KVA is available. 2531f4bef67cSGleb Smirnoff */ 2532*a81c400eSJeff Roberson void 2533*a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail) 2534f4bef67cSGleb Smirnoff { 2535*a81c400eSJeff Roberson struct uma_zctor_args args; 2536*a81c400eSJeff Roberson size_t ksize, zsize, size; 2537*a81c400eSJeff Roberson uma_keg_t masterkeg; 2538*a81c400eSJeff Roberson uintptr_t m; 2539*a81c400eSJeff Roberson uint8_t pflag; 2540*a81c400eSJeff Roberson 2541*a81c400eSJeff Roberson bootstart = bootmem = virtual_avail; 2542*a81c400eSJeff Roberson 2543*a81c400eSJeff Roberson rw_init(&uma_rwlock, "UMA lock"); 2544*a81c400eSJeff Roberson sx_init(&uma_reclaim_lock, "umareclaim"); 2545f4bef67cSGleb Smirnoff 2546f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2547f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 254879c9f942SJeff Roberson ksize = roundup(ksize, UMA_SUPER_ALIGN); 2549f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2550f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2551f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 255279c9f942SJeff Roberson zsize = roundup(zsize, UMA_SUPER_ALIGN); 2553f4bef67cSGleb Smirnoff 2554*a81c400eSJeff Roberson /* Allocate the zone of zones, zone of kegs, and zone of zones keg. */ 2555*a81c400eSJeff Roberson size = (zsize * 2) + ksize; 2556*a81c400eSJeff Roberson m = (uintptr_t)startup_alloc(NULL, size, 0, &pflag, M_NOWAIT | M_ZERO); 2557ab3185d1SJeff Roberson zones = (uma_zone_t)m; 255879c9f942SJeff Roberson m += zsize; 2559ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 256079c9f942SJeff Roberson m += zsize; 2561ab3185d1SJeff Roberson masterkeg = (uma_keg_t)m; 2562ab3185d1SJeff Roberson 2563099a0e58SBosko Milekic /* "manually" create the initial zone */ 25640095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2565099a0e58SBosko Milekic args.name = "UMA Kegs"; 2566ab3185d1SJeff Roberson args.size = ksize; 2567099a0e58SBosko Milekic args.ctor = keg_ctor; 2568099a0e58SBosko Milekic args.dtor = keg_dtor; 25698355f576SJeff Roberson args.uminit = zero_init; 25708355f576SJeff Roberson args.fini = NULL; 2571ab3185d1SJeff Roberson args.keg = masterkeg; 257279c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2573b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2574ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 25758355f576SJeff Roberson 2576099a0e58SBosko Milekic args.name = "UMA Zones"; 2577f4bef67cSGleb Smirnoff args.size = zsize; 2578099a0e58SBosko Milekic args.ctor = zone_ctor; 2579099a0e58SBosko Milekic args.dtor = zone_dtor; 2580099a0e58SBosko Milekic args.uminit = zero_init; 2581099a0e58SBosko Milekic args.fini = NULL; 2582099a0e58SBosko Milekic args.keg = NULL; 258379c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2584099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2585ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2586099a0e58SBosko Milekic 25879b8db4d0SRyan Libby /* Now make zones for slab headers */ 25889b8db4d0SRyan Libby slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE, 25899b8db4d0SRyan Libby NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 25909b8db4d0SRyan Libby slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE, 25911e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 25928355f576SJeff Roberson 25938355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 25948355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 25951e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 25968355f576SJeff Roberson 2597*a81c400eSJeff Roberson bucket_init(); 25988355f576SJeff Roberson } 25998355f576SJeff Roberson 2600*a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2601*a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void); 2602f4bef67cSGleb Smirnoff #endif 2603f4bef67cSGleb Smirnoff 2604*a81c400eSJeff Roberson /* 2605*a81c400eSJeff Roberson * Advertise the availability of normal kva allocations and switch to 2606*a81c400eSJeff Roberson * the default back-end allocator. Marks the KVA we consumed on startup 2607*a81c400eSJeff Roberson * as used in the map. 2608*a81c400eSJeff Roberson */ 26098355f576SJeff Roberson void 261099571dc3SJeff Roberson uma_startup2(void) 26118355f576SJeff Roberson { 2612f4bef67cSGleb Smirnoff 2613*a81c400eSJeff Roberson if (!PMAP_HAS_DMAP) { 2614*a81c400eSJeff Roberson vm_map_lock(kernel_map); 2615*a81c400eSJeff Roberson (void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem, 2616*a81c400eSJeff Roberson VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 2617*a81c400eSJeff Roberson vm_map_unlock(kernel_map); 2618*a81c400eSJeff Roberson } 2619*a81c400eSJeff Roberson 2620*a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2621*a81c400eSJeff Roberson /* Set up radix zone to use noobj_alloc. */ 2622*a81c400eSJeff Roberson vm_radix_reserve_kva(); 2623f7d35785SGleb Smirnoff #endif 2624*a81c400eSJeff Roberson 2625*a81c400eSJeff Roberson booted = BOOT_KVA; 2626*a81c400eSJeff Roberson zone_foreach_unlocked(zone_kva_available, NULL); 2627f4bef67cSGleb Smirnoff bucket_enable(); 26288355f576SJeff Roberson } 26298355f576SJeff Roberson 2630*a81c400eSJeff Roberson /* 2631*a81c400eSJeff Roberson * Finish our initialization steps. 2632*a81c400eSJeff Roberson */ 26338355f576SJeff Roberson static void 26348355f576SJeff Roberson uma_startup3(void) 26358355f576SJeff Roberson { 26361431a748SGleb Smirnoff 2637c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2638c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2639c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2640c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2641c5deaf04SGleb Smirnoff #endif 2642*a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_counters, NULL); 2643*a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_sysctl, NULL); 2644fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 26459643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2646c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 2647860bb7a0SMark Johnston 2648860bb7a0SMark Johnston EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL, 2649860bb7a0SMark Johnston EVENTHANDLER_PRI_FIRST); 2650860bb7a0SMark Johnston } 2651860bb7a0SMark Johnston 2652860bb7a0SMark Johnston static void 2653860bb7a0SMark Johnston uma_shutdown(void) 2654860bb7a0SMark Johnston { 2655860bb7a0SMark Johnston 2656860bb7a0SMark Johnston booted = BOOT_SHUTDOWN; 26578355f576SJeff Roberson } 26588355f576SJeff Roberson 2659e20a199fSJeff Roberson static uma_keg_t 2660099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 266185dcf349SGleb Smirnoff int align, uint32_t flags) 2662099a0e58SBosko Milekic { 2663099a0e58SBosko Milekic struct uma_kctor_args args; 2664099a0e58SBosko Milekic 2665099a0e58SBosko Milekic args.size = size; 2666099a0e58SBosko Milekic args.uminit = uminit; 2667099a0e58SBosko Milekic args.fini = fini; 26681e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2669099a0e58SBosko Milekic args.flags = flags; 2670099a0e58SBosko Milekic args.zone = zone; 2671ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 2672099a0e58SBosko Milekic } 2673099a0e58SBosko Milekic 2674f4bef67cSGleb Smirnoff /* Public functions */ 26758355f576SJeff Roberson /* See uma.h */ 26761e319f6dSRobert Watson void 26771e319f6dSRobert Watson uma_set_align(int align) 26781e319f6dSRobert Watson { 26791e319f6dSRobert Watson 26801e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 26811e319f6dSRobert Watson uma_align_cache = align; 26821e319f6dSRobert Watson } 26831e319f6dSRobert Watson 26841e319f6dSRobert Watson /* See uma.h */ 26858355f576SJeff Roberson uma_zone_t 2686bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 268785dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 26888355f576SJeff Roberson 26898355f576SJeff Roberson { 26908355f576SJeff Roberson struct uma_zctor_args args; 269195c4bf75SKonstantin Belousov uma_zone_t res; 26928355f576SJeff Roberson 2693a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 2694a5a35578SJohn Baldwin align, name)); 2695a5a35578SJohn Baldwin 26968355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 26970095a784SJeff Roberson memset(&args, 0, sizeof(args)); 26988355f576SJeff Roberson args.name = name; 26998355f576SJeff Roberson args.size = size; 27008355f576SJeff Roberson args.ctor = ctor; 27018355f576SJeff Roberson args.dtor = dtor; 27028355f576SJeff Roberson args.uminit = uminit; 27038355f576SJeff Roberson args.fini = fini; 2704afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 2705afc6dc36SJohn-Mark Gurney /* 2706ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 2707ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 2708ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 2709ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 2710ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 2711ca293436SRyan Libby * the ctor and dtor do not need to be empty. 2712afc6dc36SJohn-Mark Gurney */ 271354c5ae80SRyan Libby if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH | 271454c5ae80SRyan Libby UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) { 2715afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 2716afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 2717afc6dc36SJohn-Mark Gurney } 2718afc6dc36SJohn-Mark Gurney #endif 27198355f576SJeff Roberson args.align = align; 27208355f576SJeff Roberson args.flags = flags; 2721099a0e58SBosko Milekic args.keg = NULL; 2722099a0e58SBosko Milekic 272308cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 2724ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 272508cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 2726*a81c400eSJeff Roberson 272795c4bf75SKonstantin Belousov return (res); 2728099a0e58SBosko Milekic } 2729099a0e58SBosko Milekic 2730099a0e58SBosko Milekic /* See uma.h */ 2731099a0e58SBosko Milekic uma_zone_t 2732099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 2733099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 2734099a0e58SBosko Milekic { 2735099a0e58SBosko Milekic struct uma_zctor_args args; 2736e20a199fSJeff Roberson uma_keg_t keg; 273795c4bf75SKonstantin Belousov uma_zone_t res; 2738099a0e58SBosko Milekic 2739bb15d1c7SGleb Smirnoff keg = master->uz_keg; 27400095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2741099a0e58SBosko Milekic args.name = name; 2742e20a199fSJeff Roberson args.size = keg->uk_size; 2743099a0e58SBosko Milekic args.ctor = ctor; 2744099a0e58SBosko Milekic args.dtor = dtor; 2745099a0e58SBosko Milekic args.uminit = zinit; 2746099a0e58SBosko Milekic args.fini = zfini; 2747e20a199fSJeff Roberson args.align = keg->uk_align; 2748e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 2749e20a199fSJeff Roberson args.keg = keg; 27508355f576SJeff Roberson 275108cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 2752ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 275308cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 2754*a81c400eSJeff Roberson 275595c4bf75SKonstantin Belousov return (res); 27568355f576SJeff Roberson } 27578355f576SJeff Roberson 27580095a784SJeff Roberson /* See uma.h */ 27590095a784SJeff Roberson uma_zone_t 2760af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 2761af526374SJeff Roberson uma_init zinit, uma_fini zfini, uma_import zimport, 2762af526374SJeff Roberson uma_release zrelease, void *arg, int flags) 27630095a784SJeff Roberson { 27640095a784SJeff Roberson struct uma_zctor_args args; 27650095a784SJeff Roberson 27660095a784SJeff Roberson memset(&args, 0, sizeof(args)); 27670095a784SJeff Roberson args.name = name; 2768af526374SJeff Roberson args.size = size; 27690095a784SJeff Roberson args.ctor = ctor; 27700095a784SJeff Roberson args.dtor = dtor; 27710095a784SJeff Roberson args.uminit = zinit; 27720095a784SJeff Roberson args.fini = zfini; 27730095a784SJeff Roberson args.import = zimport; 27740095a784SJeff Roberson args.release = zrelease; 27750095a784SJeff Roberson args.arg = arg; 27760095a784SJeff Roberson args.align = 0; 2777bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 27780095a784SJeff Roberson 2779ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 27800095a784SJeff Roberson } 27810095a784SJeff Roberson 27828355f576SJeff Roberson /* See uma.h */ 27839c2cd7e5SJeff Roberson void 27849c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 27859c2cd7e5SJeff Roberson { 2786f4ff923bSRobert Watson 2787860bb7a0SMark Johnston /* 2788860bb7a0SMark Johnston * Large slabs are expensive to reclaim, so don't bother doing 2789860bb7a0SMark Johnston * unnecessary work if we're shutting down. 2790860bb7a0SMark Johnston */ 2791860bb7a0SMark Johnston if (booted == BOOT_SHUTDOWN && 2792860bb7a0SMark Johnston zone->uz_fini == NULL && zone->uz_release == zone_release) 2793860bb7a0SMark Johnston return; 279408cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 27950095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 279608cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 27979c2cd7e5SJeff Roberson } 27989c2cd7e5SJeff Roberson 27998d6fbbb8SJeff Roberson void 28008d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 28018d6fbbb8SJeff Roberson { 28028d6fbbb8SJeff Roberson void *item; 28038d6fbbb8SJeff Roberson 28048d6fbbb8SJeff Roberson item = uma_zalloc_arg(zone, NULL, M_WAITOK); 28058d6fbbb8SJeff Roberson uma_zfree(zone, item); 28068d6fbbb8SJeff Roberson } 28078d6fbbb8SJeff Roberson 28084e180881SMateusz Guzik void * 28094e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 28104e180881SMateusz Guzik { 28114e180881SMateusz Guzik void *item; 2812b4799947SRuslan Bukin #ifdef SMP 28134e180881SMateusz Guzik int i; 28144e180881SMateusz Guzik 28154e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2816b4799947SRuslan Bukin #endif 28174e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 28184e180881SMateusz Guzik if (item != NULL && (flags & M_ZERO)) { 2819b4799947SRuslan Bukin #ifdef SMP 2820013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 28214e180881SMateusz Guzik bzero(zpcpu_get_cpu(item, i), zone->uz_size); 2822b4799947SRuslan Bukin #else 2823b4799947SRuslan Bukin bzero(item, zone->uz_size); 2824b4799947SRuslan Bukin #endif 28254e180881SMateusz Guzik } 28264e180881SMateusz Guzik return (item); 28274e180881SMateusz Guzik } 28284e180881SMateusz Guzik 28294e180881SMateusz Guzik /* 28304e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 28314e180881SMateusz Guzik */ 28324e180881SMateusz Guzik void 28334e180881SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata) 28344e180881SMateusz Guzik { 28354e180881SMateusz Guzik 2836c5b7751fSIan Lepore #ifdef SMP 28374e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2838c5b7751fSIan Lepore #endif 28394e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 28404e180881SMateusz Guzik } 28414e180881SMateusz Guzik 2842cc7ce83aSJeff Roberson #ifdef INVARIANTS 2843cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 1 2844cc7ce83aSJeff Roberson #else 2845cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 0 2846cc7ce83aSJeff Roberson #endif 2847cc7ce83aSJeff Roberson 2848beb8beefSJeff Roberson static void * 2849cc7ce83aSJeff Roberson item_ctor(uma_zone_t zone, int size, void *udata, int flags, void *item) 2850beb8beefSJeff Roberson { 2851beb8beefSJeff Roberson #ifdef INVARIANTS 2852ca293436SRyan Libby bool skipdbg; 2853beb8beefSJeff Roberson 2854beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 2855ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2856ca293436SRyan Libby zone->uz_ctor != trash_ctor) 2857cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 2858beb8beefSJeff Roberson #endif 2859ca293436SRyan Libby if (__predict_false(zone->uz_ctor != NULL) && 2860cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 2861beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 2862beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 2863beb8beefSJeff Roberson return (NULL); 2864beb8beefSJeff Roberson } 2865beb8beefSJeff Roberson #ifdef INVARIANTS 2866beb8beefSJeff Roberson if (!skipdbg) 2867beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 2868beb8beefSJeff Roberson #endif 2869beb8beefSJeff Roberson if (flags & M_ZERO) 2870cc7ce83aSJeff Roberson bzero(item, size); 2871beb8beefSJeff Roberson 2872beb8beefSJeff Roberson return (item); 2873beb8beefSJeff Roberson } 2874beb8beefSJeff Roberson 2875ca293436SRyan Libby static inline void 2876cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 2877cc7ce83aSJeff Roberson enum zfreeskip skip) 2878ca293436SRyan Libby { 2879ca293436SRyan Libby #ifdef INVARIANTS 2880ca293436SRyan Libby bool skipdbg; 2881ca293436SRyan Libby 2882ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 2883ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 2884ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 2885ca293436SRyan Libby uma_dbg_free(zone, udata, item); 2886ca293436SRyan Libby else 2887ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 2888ca293436SRyan Libby } 2889ca293436SRyan Libby #endif 2890cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 2891ca293436SRyan Libby if (zone->uz_dtor != NULL) 2892cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 2893ca293436SRyan Libby #ifdef INVARIANTS 2894ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2895ca293436SRyan Libby zone->uz_dtor != trash_dtor) 2896cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 2897ca293436SRyan Libby #endif 2898ca293436SRyan Libby } 2899ca293436SRyan Libby } 2900ca293436SRyan Libby 29019c2cd7e5SJeff Roberson /* See uma.h */ 29028355f576SJeff Roberson void * 29032cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 29048355f576SJeff Roberson { 2905376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 2906ab3185d1SJeff Roberson uma_cache_t cache; 2907ab3185d1SJeff Roberson void *item; 2908cc7ce83aSJeff Roberson int domain, size, uz_flags; 29098355f576SJeff Roberson 2910e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 291119fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 291210cb2424SMark Murray 29138355f576SJeff Roberson /* This is the fast path allocation */ 2914e63a1c2fSRyan Libby CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name, 2915e63a1c2fSRyan Libby zone, flags); 2916a553d4b8SJeff Roberson 2917cc7ce83aSJeff Roberson #ifdef WITNESS 2918635fd505SRobert Watson if (flags & M_WAITOK) { 2919b23f72e9SBrian Feldman WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2920635fd505SRobert Watson "uma_zalloc_arg: zone \"%s\"", zone->uz_name); 29214c1cc01cSJohn Baldwin } 2922cc7ce83aSJeff Roberson #endif 2923cc7ce83aSJeff Roberson 2924cc7ce83aSJeff Roberson #ifdef INVARIANTS 29250766f278SJonathan T. Looney KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC")); 2926d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 29271067a2baSJonathan T. Looney ("uma_zalloc_arg: called with spinlock or critical section held")); 2928ea99223eSMateusz Guzik if (zone->uz_flags & UMA_ZONE_PCPU) 2929b8af2820SMateusz Guzik KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone " 2930b8af2820SMateusz Guzik "with M_ZERO passed")); 2931cc7ce83aSJeff Roberson #endif 29321067a2baSJonathan T. Looney 29338d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 29348d689e04SGleb Smirnoff if (memguard_cmp_zone(zone)) { 29358d689e04SGleb Smirnoff item = memguard_alloc(zone->uz_size, flags); 29368d689e04SGleb Smirnoff if (item != NULL) { 29378d689e04SGleb Smirnoff if (zone->uz_init != NULL && 29388d689e04SGleb Smirnoff zone->uz_init(item, zone->uz_size, flags) != 0) 29398d689e04SGleb Smirnoff return (NULL); 29408d689e04SGleb Smirnoff if (zone->uz_ctor != NULL && 2941fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 2942fc03d22bSJeff Roberson flags) != 0) { 2943ca293436SRyan Libby counter_u64_add(zone->uz_fails, 1); 29448d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 29458d689e04SGleb Smirnoff return (NULL); 29468d689e04SGleb Smirnoff } 29478d689e04SGleb Smirnoff return (item); 29488d689e04SGleb Smirnoff } 29498d689e04SGleb Smirnoff /* This is unfortunate but should not be fatal. */ 29508d689e04SGleb Smirnoff } 29518d689e04SGleb Smirnoff #endif 29525d1ae027SRobert Watson /* 29535d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 29545d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 29555d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 29565d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 29575d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 29585d1ae027SRobert Watson * preemption and migration. We release the critical section in 29595d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 29605d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 29615d1ae027SRobert Watson * must detect and handle migration if it has occurred. 29625d1ae027SRobert Watson */ 29635d1ae027SRobert Watson critical_enter(); 2964beb8beefSJeff Roberson do { 2965cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 2966376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 2967cc7ce83aSJeff Roberson size = cache_uz_size(cache); 2968cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 2969376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 2970376b1ba3SJeff Roberson item = cache_bucket_pop(cache, bucket); 29715d1ae027SRobert Watson critical_exit(); 2972cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 2973cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 2974cc7ce83aSJeff Roberson return (item_ctor(zone, size, udata, flags, item)); 2975cc7ce83aSJeff Roberson if (flags & M_ZERO) 2976cc7ce83aSJeff Roberson bzero(item, size); 2977cc7ce83aSJeff Roberson return (item); 2978b23f72e9SBrian Feldman } 2979beb8beefSJeff Roberson } while (cache_alloc(zone, cache, udata, flags)); 2980beb8beefSJeff Roberson critical_exit(); 2981beb8beefSJeff Roberson 2982beb8beefSJeff Roberson /* 2983beb8beefSJeff Roberson * We can not get a bucket so try to return a single item. 2984beb8beefSJeff Roberson */ 2985dfe13344SJeff Roberson if (uz_flags & UMA_ZONE_FIRSTTOUCH) 2986beb8beefSJeff Roberson domain = PCPU_GET(domain); 2987beb8beefSJeff Roberson else 2988beb8beefSJeff Roberson domain = UMA_ANYDOMAIN; 29894bd61e19SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 2990fc03d22bSJeff Roberson } 2991fc03d22bSJeff Roberson 29928355f576SJeff Roberson /* 2993beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 2994beb8beefSJeff Roberson * a critical section. Returns in a critical section. 2995beb8beefSJeff Roberson * 29964bd61e19SJeff Roberson * A false return value indicates an allocation failure. 29974bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 2998beb8beefSJeff Roberson */ 2999beb8beefSJeff Roberson static __noinline bool 3000beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3001beb8beefSJeff Roberson { 3002beb8beefSJeff Roberson uma_zone_domain_t zdom; 3003beb8beefSJeff Roberson uma_bucket_t bucket; 3004cc7ce83aSJeff Roberson int domain; 3005beb8beefSJeff Roberson bool lockfail; 3006beb8beefSJeff Roberson 3007beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3008beb8beefSJeff Roberson 3009beb8beefSJeff Roberson /* 3010beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3011beb8beefSJeff Roberson * if we can switch with the free bucket. 30128355f576SJeff Roberson */ 3013376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_cnt != 0) { 3014376b1ba3SJeff Roberson cache_bucket_swap(&cache->uc_freebucket, &cache->uc_allocbucket); 3015beb8beefSJeff Roberson return (true); 30168355f576SJeff Roberson } 3017fc03d22bSJeff Roberson 3018fc03d22bSJeff Roberson /* 3019fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3020fc03d22bSJeff Roberson */ 3021376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3022fc03d22bSJeff Roberson critical_exit(); 3023fc03d22bSJeff Roberson if (bucket != NULL) 30246fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3025fc03d22bSJeff Roberson 30264bd61e19SJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 30274bd61e19SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) { 30284bd61e19SJeff Roberson critical_enter(); 30294bd61e19SJeff Roberson return (false); 30304bd61e19SJeff Roberson } 30314bd61e19SJeff Roberson 30325d1ae027SRobert Watson /* 30335d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 30345d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 30355d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 30365d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 30375d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 30385d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 30395d1ae027SRobert Watson * the critical section. 30405d1ae027SRobert Watson */ 3041fc03d22bSJeff Roberson lockfail = 0; 3042fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 3043fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 3044a553d4b8SJeff Roberson ZONE_LOCK(zone); 3045fc03d22bSJeff Roberson lockfail = 1; 3046fc03d22bSJeff Roberson } 3047beb8beefSJeff Roberson 3048fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 30494bd61e19SJeff Roberson critical_enter(); 30504bd61e19SJeff Roberson cache = &zone->uz_cpu[curcpu]; 3051376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket != NULL) { 3052fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 3053beb8beefSJeff Roberson return (true); 3054a553d4b8SJeff Roberson } 30558355f576SJeff Roberson 3056fc03d22bSJeff Roberson /* 3057fc03d22bSJeff Roberson * Check the zone's cache of buckets. 3058fc03d22bSJeff Roberson */ 3059dfe13344SJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) { 3060c1685086SJeff Roberson domain = PCPU_GET(domain); 3061ab3185d1SJeff Roberson zdom = &zone->uz_domain[domain]; 3062c1685086SJeff Roberson } else { 3063c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3064c1685086SJeff Roberson zdom = &zone->uz_domain[0]; 3065c1685086SJeff Roberson } 3066c1685086SJeff Roberson 306708cfa56eSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom)) != NULL) { 3068beb8beefSJeff Roberson ZONE_UNLOCK(zone); 3069cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 3070a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 3071376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3072beb8beefSJeff Roberson return (true); 3073a553d4b8SJeff Roberson } 30745d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 30755d1ae027SRobert Watson critical_exit(); 3076bbee39c6SJeff Roberson 3077fc03d22bSJeff Roberson /* 3078fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 3079fc03d22bSJeff Roberson * handle the working set. 3080fc03d22bSJeff Roberson */ 308120a4e154SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 308220a4e154SJeff Roberson zone->uz_bucket_size++; 30834bd61e19SJeff Roberson ZONE_UNLOCK(zone); 3084bb15d1c7SGleb Smirnoff 30858355f576SJeff Roberson /* 3086beb8beefSJeff Roberson * Fill a bucket and attempt to use it as the alloc bucket. 3087bbee39c6SJeff Roberson */ 3088beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 30891431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 30901431a748SGleb Smirnoff zone->uz_name, zone, bucket); 30914bd61e19SJeff Roberson if (bucket == NULL) { 3092fc03d22bSJeff Roberson critical_enter(); 3093beb8beefSJeff Roberson return (false); 30944bd61e19SJeff Roberson } 30950f9b7bf3SMark Johnston 3096fc03d22bSJeff Roberson /* 3097fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3098fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3099fc03d22bSJeff Roberson * the memory directly. 3100fc03d22bSJeff Roberson */ 31014bd61e19SJeff Roberson ZONE_LOCK(zone); 31024bd61e19SJeff Roberson critical_enter(); 3103cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3104376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 3105dfe13344SJeff Roberson ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0 || 310681c0d72cSGleb Smirnoff domain == PCPU_GET(domain))) { 3107376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 31080f9b7bf3SMark Johnston zdom->uzd_imax += bucket->ub_cnt; 3109bb15d1c7SGleb Smirnoff } else if (zone->uz_bkt_count >= zone->uz_bkt_max) { 311081c0d72cSGleb Smirnoff critical_exit(); 311181c0d72cSGleb Smirnoff ZONE_UNLOCK(zone); 311281c0d72cSGleb Smirnoff bucket_drain(zone, bucket); 311381c0d72cSGleb Smirnoff bucket_free(zone, bucket, udata); 3114beb8beefSJeff Roberson critical_enter(); 3115beb8beefSJeff Roberson return (true); 311681c0d72cSGleb Smirnoff } else 31170f9b7bf3SMark Johnston zone_put_bucket(zone, zdom, bucket, false); 3118bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 3119beb8beefSJeff Roberson return (true); 3120bbee39c6SJeff Roberson } 3121bbee39c6SJeff Roberson 3122ab3185d1SJeff Roberson void * 3123ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3124bbee39c6SJeff Roberson { 3125ab3185d1SJeff Roberson 3126ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 312719fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3128ab3185d1SJeff Roberson 3129ab3185d1SJeff Roberson /* This is the fast path allocation */ 3130e63a1c2fSRyan Libby CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d", 3131e63a1c2fSRyan Libby zone->uz_name, zone, domain, flags); 3132ab3185d1SJeff Roberson 3133ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3134ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3135ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3136ab3185d1SJeff Roberson } 3137ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3138ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 3139ab3185d1SJeff Roberson 3140ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3141ab3185d1SJeff Roberson } 3142ab3185d1SJeff Roberson 3143ab3185d1SJeff Roberson /* 3144ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3145ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3146ab3185d1SJeff Roberson * 3147ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3148ab3185d1SJeff Roberson * only 'domain'. 3149ab3185d1SJeff Roberson */ 3150ab3185d1SJeff Roberson static uma_slab_t 3151194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3152ab3185d1SJeff Roberson { 3153ab3185d1SJeff Roberson uma_domain_t dom; 3154bbee39c6SJeff Roberson uma_slab_t slab; 3155ab3185d1SJeff Roberson int start; 3156ab3185d1SJeff Roberson 3157ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3158ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 31598b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3160ab3185d1SJeff Roberson 3161ab3185d1SJeff Roberson slab = NULL; 3162ab3185d1SJeff Roberson start = domain; 3163ab3185d1SJeff Roberson do { 3164ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 3165ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_part_slab)) 3166ab3185d1SJeff Roberson return (LIST_FIRST(&dom->ud_part_slab)); 3167ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_free_slab)) { 3168ab3185d1SJeff Roberson slab = LIST_FIRST(&dom->ud_free_slab); 3169ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 3170ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3171ab3185d1SJeff Roberson return (slab); 3172ab3185d1SJeff Roberson } 3173ab3185d1SJeff Roberson if (rr) 3174ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3175ab3185d1SJeff Roberson } while (domain != start); 3176ab3185d1SJeff Roberson 3177ab3185d1SJeff Roberson return (NULL); 3178ab3185d1SJeff Roberson } 3179ab3185d1SJeff Roberson 31808b987a77SJeff Roberson /* 31818b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 31828b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 31838b987a77SJeff Roberson */ 3184ab3185d1SJeff Roberson static uma_slab_t 3185194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3186ab3185d1SJeff Roberson { 31878b987a77SJeff Roberson uma_slab_t slab; 3188194a979eSMark Johnston uint32_t reserve; 3189099a0e58SBosko Milekic 31908b987a77SJeff Roberson /* HASH has a single free list. */ 319154c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 31928b987a77SJeff Roberson domain = 0; 3193194a979eSMark Johnston 31948b987a77SJeff Roberson KEG_LOCK(keg, domain); 3195194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 31968b987a77SJeff Roberson if (keg->uk_domain[domain].ud_free <= reserve || 31978b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 31988b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3199194a979eSMark Johnston return (NULL); 32008b987a77SJeff Roberson } 32018b987a77SJeff Roberson return (slab); 3202194a979eSMark Johnston } 3203194a979eSMark Johnston 3204194a979eSMark Johnston static uma_slab_t 3205194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3206194a979eSMark Johnston { 3207194a979eSMark Johnston struct vm_domainset_iter di; 3208194a979eSMark Johnston uma_slab_t slab; 3209194a979eSMark Johnston int aflags, domain; 3210194a979eSMark Johnston bool rr; 3211194a979eSMark Johnston 3212194a979eSMark Johnston restart: 3213bbee39c6SJeff Roberson /* 3214194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3215194a979eSMark Johnston * domain (as happens with first-touch zones). 3216194a979eSMark Johnston * 3217194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3218194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3219194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3220bbee39c6SJeff Roberson */ 3221ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3222ab3185d1SJeff Roberson if (rr) { 3223194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3224194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3225194a979eSMark Johnston &aflags); 3226194a979eSMark Johnston } else { 3227194a979eSMark Johnston aflags = flags; 3228194a979eSMark Johnston domain = rdomain; 3229194a979eSMark Johnston } 3230ab3185d1SJeff Roberson 3231194a979eSMark Johnston for (;;) { 3232194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3233584061b4SJeff Roberson if (slab != NULL) 3234bbee39c6SJeff Roberson return (slab); 3235bbee39c6SJeff Roberson 3236bbee39c6SJeff Roberson /* 3237bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3238bbee39c6SJeff Roberson */ 3239bbee39c6SJeff Roberson if (flags & M_NOVM) 3240bbee39c6SJeff Roberson break; 3241bbee39c6SJeff Roberson 324286220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 32438b987a77SJeff Roberson if (slab != NULL) 3244bbee39c6SJeff Roberson return (slab); 32453639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 32463639ac42SJeff Roberson break; 3247194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3248194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 3249194a979eSMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 3250194a979eSMark Johnston goto restart; 325130c5525bSAndrew Gallatin } 3252194a979eSMark Johnston break; 3253194a979eSMark Johnston } 3254ab3185d1SJeff Roberson } 3255ab3185d1SJeff Roberson 3256bbee39c6SJeff Roberson /* 3257bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3258bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3259bbee39c6SJeff Roberson * fail. 3260bbee39c6SJeff Roberson */ 32618b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 3262bbee39c6SJeff Roberson return (slab); 32638b987a77SJeff Roberson 3264ab3185d1SJeff Roberson return (NULL); 3265ab3185d1SJeff Roberson } 3266bbee39c6SJeff Roberson 3267d56368d7SBosko Milekic static void * 32680095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3269bbee39c6SJeff Roberson { 3270ab3185d1SJeff Roberson uma_domain_t dom; 3271bbee39c6SJeff Roberson void *item; 32729b8db4d0SRyan Libby int freei; 3273bbee39c6SJeff Roberson 32748b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3275099a0e58SBosko Milekic 32768b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 32779b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 32789b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 32791e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3280bbee39c6SJeff Roberson slab->us_freecount--; 32818b987a77SJeff Roberson dom->ud_free--; 3282ef72505eSJeff Roberson 3283bbee39c6SJeff Roberson /* Move this slab to the full list */ 3284bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3285bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3286ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3287bbee39c6SJeff Roberson } 3288bbee39c6SJeff Roberson 3289bbee39c6SJeff Roberson return (item); 3290bbee39c6SJeff Roberson } 3291bbee39c6SJeff Roberson 3292bbee39c6SJeff Roberson static int 3293b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 32940095a784SJeff Roberson { 32958b987a77SJeff Roberson uma_domain_t dom; 3296b75c4efcSAndrew Turner uma_zone_t zone; 32970095a784SJeff Roberson uma_slab_t slab; 32980095a784SJeff Roberson uma_keg_t keg; 3299a03af342SSean Bruno #ifdef NUMA 3300ab3185d1SJeff Roberson int stripe; 3301a03af342SSean Bruno #endif 33020095a784SJeff Roberson int i; 33030095a784SJeff Roberson 3304b75c4efcSAndrew Turner zone = arg; 33050095a784SJeff Roberson slab = NULL; 3306584061b4SJeff Roberson keg = zone->uz_keg; 3307af526374SJeff Roberson /* Try to keep the buckets totally full */ 33080095a784SJeff Roberson for (i = 0; i < max; ) { 3309584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 33100095a784SJeff Roberson break; 3311a03af342SSean Bruno #ifdef NUMA 3312ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3313a03af342SSean Bruno #endif 33148b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 33156fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 33160095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 33178b987a77SJeff Roberson if (dom->ud_free <= keg->uk_reserve) 33186fd34d6fSJeff Roberson break; 3319b6715dabSJeff Roberson #ifdef NUMA 3320ab3185d1SJeff Roberson /* 3321ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3322ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3323ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3324ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3325ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3326ab3185d1SJeff Roberson * time but yields better distribution. 3327ab3185d1SJeff Roberson */ 3328dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 && 3329ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3330ab3185d1SJeff Roberson break; 3331ab3185d1SJeff Roberson #endif 33326fd34d6fSJeff Roberson } 33338b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 3334ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 33350095a784SJeff Roberson flags &= ~M_WAITOK; 33360095a784SJeff Roberson flags |= M_NOWAIT; 33370095a784SJeff Roberson } 33380095a784SJeff Roberson 33390095a784SJeff Roberson return i; 33400095a784SJeff Roberson } 33410095a784SJeff Roberson 33424bd61e19SJeff Roberson static int 33434bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 33444bd61e19SJeff Roberson { 33454bd61e19SJeff Roberson uint64_t old, new, total, max; 33464bd61e19SJeff Roberson 33474bd61e19SJeff Roberson /* 33484bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 33494bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 33504bd61e19SJeff Roberson * fairness by keeping fifo order. 33514bd61e19SJeff Roberson * 33524bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 33534bd61e19SJeff Roberson */ 33544bd61e19SJeff Roberson for (;;) { 33554bd61e19SJeff Roberson zone_free_limit(zone, count); 33564bd61e19SJeff Roberson zone_log_warning(zone); 33574bd61e19SJeff Roberson zone_maxaction(zone); 33584bd61e19SJeff Roberson if (flags & M_NOWAIT) 33594bd61e19SJeff Roberson return (0); 33604bd61e19SJeff Roberson 33614bd61e19SJeff Roberson /* 33624bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 33634bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 33644bd61e19SJeff Roberson * is essentially a home rolled semaphore. 33654bd61e19SJeff Roberson */ 33664bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 33674bd61e19SJeff Roberson old = zone->uz_items; 33684bd61e19SJeff Roberson do { 33694bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 33704bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 33714bd61e19SJeff Roberson max = zone->uz_max_items; 33724bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 33734bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 33744bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 33754bd61e19SJeff Roberson else 33764bd61e19SJeff Roberson new = old + MIN(count, max - old); 33774bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 33784bd61e19SJeff Roberson 33794bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 33804bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 33814bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 33824bd61e19SJeff Roberson return (new - old); 33834bd61e19SJeff Roberson } 33844bd61e19SJeff Roberson 33854bd61e19SJeff Roberson /* 33864bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 33874bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 33884bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 33894bd61e19SJeff Roberson * transitions. 33904bd61e19SJeff Roberson */ 33914bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 33924bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 33934bd61e19SJeff Roberson 33944bd61e19SJeff Roberson /* 33954bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 33964bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 33974bd61e19SJeff Roberson */ 33984bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 33994bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 34004bd61e19SJeff Roberson 34014bd61e19SJeff Roberson /* 34024bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 34034bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 34044bd61e19SJeff Roberson * 34054bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 34064bd61e19SJeff Roberson * our count. 34074bd61e19SJeff Roberson */ 34084bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 34094bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 34104bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 34114bd61e19SJeff Roberson /* We're no longer a sleeper. */ 34124bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 34134bd61e19SJeff Roberson 34144bd61e19SJeff Roberson /* 34154bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 34164bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 34174bd61e19SJeff Roberson * against changes via sysctl. 34184bd61e19SJeff Roberson */ 34194bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 34204bd61e19SJeff Roberson max = zone->uz_max_items; 34214bd61e19SJeff Roberson if (total >= max) 34224bd61e19SJeff Roberson continue; 34234bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 34244bd61e19SJeff Roberson if (total + count > max) { 34254bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 34264bd61e19SJeff Roberson count = max - total; 34274bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 34284bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 34294bd61e19SJeff Roberson 34304bd61e19SJeff Roberson return (count); 34314bd61e19SJeff Roberson } 34324bd61e19SJeff Roberson } 34334bd61e19SJeff Roberson 34344bd61e19SJeff Roberson /* 34354bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 34364bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 34374bd61e19SJeff Roberson * one item can be allocated. 34384bd61e19SJeff Roberson */ 34394bd61e19SJeff Roberson static int 34404bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 34414bd61e19SJeff Roberson { 34424bd61e19SJeff Roberson uint64_t old; 34434bd61e19SJeff Roberson uint64_t max; 34444bd61e19SJeff Roberson 34454bd61e19SJeff Roberson max = zone->uz_max_items; 34464bd61e19SJeff Roberson MPASS(max > 0); 34474bd61e19SJeff Roberson 34484bd61e19SJeff Roberson /* 34494bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 34504bd61e19SJeff Roberson * fetchadd. 34514bd61e19SJeff Roberson */ 34524bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 34534bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 34544bd61e19SJeff Roberson return (count); 34554bd61e19SJeff Roberson 34564bd61e19SJeff Roberson /* 34574bd61e19SJeff Roberson * If we had some items and no sleepers just return the 34584bd61e19SJeff Roberson * truncated value. We have to release the excess space 34594bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 34604bd61e19SJeff Roberson * because we were temporarily over the limit. 34614bd61e19SJeff Roberson */ 34624bd61e19SJeff Roberson if (old < max) { 34634bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 34644bd61e19SJeff Roberson return (max - old); 34654bd61e19SJeff Roberson } 34664bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 34674bd61e19SJeff Roberson } 34684bd61e19SJeff Roberson 34694bd61e19SJeff Roberson /* 34704bd61e19SJeff Roberson * Free a number of items back to the limit. 34714bd61e19SJeff Roberson */ 34724bd61e19SJeff Roberson static void 34734bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 34744bd61e19SJeff Roberson { 34754bd61e19SJeff Roberson uint64_t old; 34764bd61e19SJeff Roberson 34774bd61e19SJeff Roberson MPASS(count > 0); 34784bd61e19SJeff Roberson 34794bd61e19SJeff Roberson /* 34804bd61e19SJeff Roberson * In the common case we either have no sleepers or 34814bd61e19SJeff Roberson * are still over the limit and can just return. 34824bd61e19SJeff Roberson */ 34834bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 34844bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 34854bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 34864bd61e19SJeff Roberson return; 34874bd61e19SJeff Roberson 34884bd61e19SJeff Roberson /* 34894bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 34904bd61e19SJeff Roberson * to generate wakeups if necessary. 34914bd61e19SJeff Roberson */ 34924bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 34934bd61e19SJeff Roberson } 34944bd61e19SJeff Roberson 3495fc03d22bSJeff Roberson static uma_bucket_t 3496beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3497bbee39c6SJeff Roberson { 3498bbee39c6SJeff Roberson uma_bucket_t bucket; 3499beb8beefSJeff Roberson int maxbucket, cnt; 3500bbee39c6SJeff Roberson 3501e63a1c2fSRyan Libby CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name, 3502e63a1c2fSRyan Libby zone, domain); 350330c5525bSAndrew Gallatin 3504c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3505c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3506c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3507c1685086SJeff Roberson 35084bd61e19SJeff Roberson if (zone->uz_max_items > 0) 35094bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 35104bd61e19SJeff Roberson M_NOWAIT); 35114bd61e19SJeff Roberson else 351220a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 35134bd61e19SJeff Roberson if (maxbucket == 0) 35144bd61e19SJeff Roberson return (false); 3515beb8beefSJeff Roberson 35166fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 35176fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3518beb8beefSJeff Roberson if (bucket == NULL) { 3519beb8beefSJeff Roberson cnt = 0; 3520beb8beefSJeff Roberson goto out; 3521beb8beefSJeff Roberson } 35220095a784SJeff Roberson 35230095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3524beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 35250095a784SJeff Roberson 35260095a784SJeff Roberson /* 35270095a784SJeff Roberson * Initialize the memory if necessary. 35280095a784SJeff Roberson */ 35290095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 3530099a0e58SBosko Milekic int i; 3531bbee39c6SJeff Roberson 35320095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 3533e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 35340095a784SJeff Roberson flags) != 0) 3535b23f72e9SBrian Feldman break; 3536b23f72e9SBrian Feldman /* 3537b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 3538b23f72e9SBrian Feldman * rest back onto the freelist. 3539b23f72e9SBrian Feldman */ 3540b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 3541af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 35420095a784SJeff Roberson bucket->ub_cnt - i); 3543a5a262c6SBosko Milekic #ifdef INVARIANTS 35440095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 35450095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 3546a5a262c6SBosko Milekic #endif 3547b23f72e9SBrian Feldman bucket->ub_cnt = i; 3548b23f72e9SBrian Feldman } 3549099a0e58SBosko Milekic } 3550099a0e58SBosko Milekic 3551beb8beefSJeff Roberson cnt = bucket->ub_cnt; 3552f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 35536fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 35542efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 3555beb8beefSJeff Roberson bucket = NULL; 3556beb8beefSJeff Roberson } 3557beb8beefSJeff Roberson out: 35584bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 35594bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 3560fc03d22bSJeff Roberson 3561fc03d22bSJeff Roberson return (bucket); 3562fc03d22bSJeff Roberson } 3563fc03d22bSJeff Roberson 35648355f576SJeff Roberson /* 35650095a784SJeff Roberson * Allocates a single item from a zone. 35668355f576SJeff Roberson * 35678355f576SJeff Roberson * Arguments 35688355f576SJeff Roberson * zone The zone to alloc for. 35698355f576SJeff Roberson * udata The data to be passed to the constructor. 3570ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 3571a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 35728355f576SJeff Roberson * 35738355f576SJeff Roberson * Returns 35748355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 3575bbee39c6SJeff Roberson * An item if successful 35768355f576SJeff Roberson */ 35778355f576SJeff Roberson 35788355f576SJeff Roberson static void * 3579ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 35808355f576SJeff Roberson { 35818355f576SJeff Roberson void *item; 35828355f576SJeff Roberson 35834bd61e19SJeff Roberson if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) 3584bb15d1c7SGleb Smirnoff return (NULL); 35858355f576SJeff Roberson 3586c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3587c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 358830c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 3589c1685086SJeff Roberson 3590ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 3591beb8beefSJeff Roberson goto fail_cnt; 35928355f576SJeff Roberson 3593099a0e58SBosko Milekic /* 3594099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 3595099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 3596099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 3597099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 3598099a0e58SBosko Milekic */ 3599b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 3600e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 3601bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 3602beb8beefSJeff Roberson goto fail_cnt; 3603beb8beefSJeff Roberson } 3604beb8beefSJeff Roberson } 3605cc7ce83aSJeff Roberson item = item_ctor(zone, zone->uz_size, udata, flags, item); 3606beb8beefSJeff Roberson if (item == NULL) 36070095a784SJeff Roberson goto fail; 36088355f576SJeff Roberson 36092efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 36101431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 36111431a748SGleb Smirnoff zone->uz_name, zone); 36121431a748SGleb Smirnoff 36138355f576SJeff Roberson return (item); 36140095a784SJeff Roberson 3615beb8beefSJeff Roberson fail_cnt: 3616beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 36170095a784SJeff Roberson fail: 36184bd61e19SJeff Roberson if (zone->uz_max_items > 0) 36194bd61e19SJeff Roberson zone_free_limit(zone, 1); 36201431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 36211431a748SGleb Smirnoff zone->uz_name, zone); 36224bd61e19SJeff Roberson 36230095a784SJeff Roberson return (NULL); 36248355f576SJeff Roberson } 36258355f576SJeff Roberson 36268355f576SJeff Roberson /* See uma.h */ 36278355f576SJeff Roberson void 36288355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 36298355f576SJeff Roberson { 36308355f576SJeff Roberson uma_cache_t cache; 3631376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3632cc7ce83aSJeff Roberson int domain, itemdomain, uz_flags; 36338355f576SJeff Roberson 3634e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 363519fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 363610cb2424SMark Murray 3637e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone); 36383659f747SRobert Watson 3639d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 36401067a2baSJonathan T. Looney ("uma_zfree_arg: called with spinlock or critical section held")); 36411067a2baSJonathan T. Looney 364220ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 364320ed0cb0SMatthew D Fleming if (item == NULL) 364420ed0cb0SMatthew D Fleming return; 36458d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 36468d689e04SGleb Smirnoff if (is_memguard_addr(item)) { 3647bc9d08e1SMark Johnston if (zone->uz_dtor != NULL) 36488d689e04SGleb Smirnoff zone->uz_dtor(item, zone->uz_size, udata); 3649bc9d08e1SMark Johnston if (zone->uz_fini != NULL) 36508d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 36518d689e04SGleb Smirnoff memguard_free(item); 36528d689e04SGleb Smirnoff return; 36538d689e04SGleb Smirnoff } 36548d689e04SGleb Smirnoff #endif 3655cc7ce83aSJeff Roberson 3656cc7ce83aSJeff Roberson /* 3657cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 3658cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 3659cc7ce83aSJeff Roberson * will simply read another cpu's line. 3660cc7ce83aSJeff Roberson */ 3661cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3662cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3663cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 3664cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 3665cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 3666ef72505eSJeff Roberson 3667af7f9b97SJeff Roberson /* 3668af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 3669af7f9b97SJeff Roberson * a little longer for the limits to be reset. 3670af7f9b97SJeff Roberson */ 3671cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 3672bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0) 3673fc03d22bSJeff Roberson goto zfree_item; 3674cc7ce83aSJeff Roberson } 3675af7f9b97SJeff Roberson 36765d1ae027SRobert Watson /* 36775d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 36785d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 36795d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 36805d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 36815d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 36825d1ae027SRobert Watson * preemption and migration. We release the critical section in 36835d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 36845d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 36855d1ae027SRobert Watson * detect and handle migration if it has occurred. 36865d1ae027SRobert Watson */ 36870a81b439SJeff Roberson domain = itemdomain = 0; 3688dfe13344SJeff Roberson #ifdef NUMA 3689dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 3690dfe13344SJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 3691dfe13344SJeff Roberson #endif 36925d1ae027SRobert Watson critical_enter(); 36930a81b439SJeff Roberson do { 3694cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3695dfe13344SJeff Roberson #ifdef NUMA 36960a81b439SJeff Roberson domain = PCPU_GET(domain); 3697dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 3698dfe13344SJeff Roberson domain != itemdomain) { 3699376b1ba3SJeff Roberson bucket = &cache->uc_crossbucket; 37000a81b439SJeff Roberson } else 3701c1685086SJeff Roberson #endif 3702dfe13344SJeff Roberson { 3703a553d4b8SJeff Roberson /* 3704dfe13344SJeff Roberson * Try to free into the allocbucket first to give LIFO 3705dfe13344SJeff Roberson * ordering for cache-hot datastructures. Spill over 3706dfe13344SJeff Roberson * into the freebucket if necessary. Alloc will swap 3707dfe13344SJeff Roberson * them if one runs dry. 3708a553d4b8SJeff Roberson */ 3709dfe13344SJeff Roberson bucket = &cache->uc_allocbucket; 3710dfe13344SJeff Roberson if (__predict_false(bucket->ucb_cnt >= 3711dfe13344SJeff Roberson bucket->ucb_entries)) 3712376b1ba3SJeff Roberson bucket = &cache->uc_freebucket; 3713dfe13344SJeff Roberson } 3714376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3715376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 37165d1ae027SRobert Watson critical_exit(); 37178355f576SJeff Roberson return; 3718fc03d22bSJeff Roberson } 37190a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 37200a81b439SJeff Roberson critical_exit(); 3721fc03d22bSJeff Roberson 37228355f576SJeff Roberson /* 37230a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 37248355f576SJeff Roberson */ 37250a81b439SJeff Roberson zfree_item: 37260a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 37270a81b439SJeff Roberson } 3728fc03d22bSJeff Roberson 3729dfe13344SJeff Roberson #ifdef NUMA 373091d947bfSJeff Roberson /* 373191d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 373291d947bfSJeff Roberson * them. 373391d947bfSJeff Roberson */ 373491d947bfSJeff Roberson static void 373591d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 373691d947bfSJeff Roberson { 373791d947bfSJeff Roberson struct uma_bucketlist fullbuckets; 373891d947bfSJeff Roberson uma_zone_domain_t zdom; 373991d947bfSJeff Roberson uma_bucket_t b; 374091d947bfSJeff Roberson void *item; 374191d947bfSJeff Roberson int domain; 374291d947bfSJeff Roberson 374391d947bfSJeff Roberson CTR3(KTR_UMA, 374491d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 374591d947bfSJeff Roberson zone->uz_name, zone, bucket); 374691d947bfSJeff Roberson 374791d947bfSJeff Roberson TAILQ_INIT(&fullbuckets); 374891d947bfSJeff Roberson 374991d947bfSJeff Roberson /* 375091d947bfSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 375191d947bfSJeff Roberson * lock on the current crossfree bucket. A full matrix with 375291d947bfSJeff Roberson * per-domain locking could be used if necessary. 375391d947bfSJeff Roberson */ 375491d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 375591d947bfSJeff Roberson while (bucket->ub_cnt > 0) { 375691d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 375791d947bfSJeff Roberson domain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 375891d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 375991d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 376091d947bfSJeff Roberson zdom->uzd_cross = bucket_alloc(zone, udata, M_NOWAIT); 376191d947bfSJeff Roberson if (zdom->uzd_cross == NULL) 376291d947bfSJeff Roberson break; 376391d947bfSJeff Roberson } 376491d947bfSJeff Roberson zdom->uzd_cross->ub_bucket[zdom->uzd_cross->ub_cnt++] = item; 376591d947bfSJeff Roberson if (zdom->uzd_cross->ub_cnt == zdom->uzd_cross->ub_entries) { 376691d947bfSJeff Roberson TAILQ_INSERT_HEAD(&fullbuckets, zdom->uzd_cross, 376791d947bfSJeff Roberson ub_link); 376891d947bfSJeff Roberson zdom->uzd_cross = NULL; 376991d947bfSJeff Roberson } 377091d947bfSJeff Roberson bucket->ub_cnt--; 377191d947bfSJeff Roberson } 377291d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 377391d947bfSJeff Roberson if (!TAILQ_EMPTY(&fullbuckets)) { 377491d947bfSJeff Roberson ZONE_LOCK(zone); 377591d947bfSJeff Roberson while ((b = TAILQ_FIRST(&fullbuckets)) != NULL) { 377691d947bfSJeff Roberson TAILQ_REMOVE(&fullbuckets, b, ub_link); 377791d947bfSJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 377891d947bfSJeff Roberson ZONE_UNLOCK(zone); 377991d947bfSJeff Roberson bucket_drain(zone, b); 378091d947bfSJeff Roberson bucket_free(zone, b, udata); 378191d947bfSJeff Roberson ZONE_LOCK(zone); 378291d947bfSJeff Roberson } else { 378391d947bfSJeff Roberson domain = _vm_phys_domain( 378491d947bfSJeff Roberson pmap_kextract( 378591d947bfSJeff Roberson (vm_offset_t)b->ub_bucket[0])); 378691d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 378791d947bfSJeff Roberson zone_put_bucket(zone, zdom, b, true); 378891d947bfSJeff Roberson } 378991d947bfSJeff Roberson } 379091d947bfSJeff Roberson ZONE_UNLOCK(zone); 379191d947bfSJeff Roberson } 379291d947bfSJeff Roberson if (bucket->ub_cnt != 0) 379391d947bfSJeff Roberson bucket_drain(zone, bucket); 379491d947bfSJeff Roberson bucket_free(zone, bucket, udata); 379591d947bfSJeff Roberson } 379691d947bfSJeff Roberson #endif 379791d947bfSJeff Roberson 37980a81b439SJeff Roberson static void 37990a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 38000a81b439SJeff Roberson int domain, int itemdomain) 38010a81b439SJeff Roberson { 38020a81b439SJeff Roberson uma_zone_domain_t zdom; 38030a81b439SJeff Roberson 3804dfe13344SJeff Roberson #ifdef NUMA 38050a81b439SJeff Roberson /* 38060a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 38070a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 38080a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 380991d947bfSJeff Roberson * correct domains. 38100a81b439SJeff Roberson */ 38110a81b439SJeff Roberson if (domain != itemdomain && vm_ndomains > 2) { 381291d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 38130a81b439SJeff Roberson return; 38140a81b439SJeff Roberson } 38150a81b439SJeff Roberson #endif 381691d947bfSJeff Roberson 38170a81b439SJeff Roberson /* 38180a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 38190a81b439SJeff Roberson * 38200a81b439SJeff Roberson * We bump the uz count when the cache size is insufficient to 38210a81b439SJeff Roberson * handle the working set. 38220a81b439SJeff Roberson */ 38234d104ba0SAlexander Motin if (ZONE_TRYLOCK(zone) == 0) { 38244d104ba0SAlexander Motin /* Record contention to size the buckets. */ 38258355f576SJeff Roberson ZONE_LOCK(zone); 382620a4e154SJeff Roberson if (zone->uz_bucket_size < zone->uz_bucket_size_max) 382720a4e154SJeff Roberson zone->uz_bucket_size++; 38284d104ba0SAlexander Motin } 38298355f576SJeff Roberson 38300a81b439SJeff Roberson CTR3(KTR_UMA, 38310a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 38320a81b439SJeff Roberson zone->uz_name, zone, bucket); 38330a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 38340a81b439SJeff Roberson KASSERT(bucket->ub_cnt == bucket->ub_entries, 38350a81b439SJeff Roberson ("uma_zfree: Attempting to insert partial bucket onto the full list.\n")); 38360a81b439SJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 3837c1685086SJeff Roberson ZONE_UNLOCK(zone); 3838c1685086SJeff Roberson bucket_drain(zone, bucket); 3839c1685086SJeff Roberson bucket_free(zone, bucket, udata); 3840c1685086SJeff Roberson } else { 3841c1685086SJeff Roberson zdom = &zone->uz_domain[itemdomain]; 3842c1685086SJeff Roberson zone_put_bucket(zone, zdom, bucket, true); 3843c1685086SJeff Roberson ZONE_UNLOCK(zone); 3844c1685086SJeff Roberson } 38458355f576SJeff Roberson } 3846fc03d22bSJeff Roberson 38474d104ba0SAlexander Motin /* 38480a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 38490a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 38500a81b439SJeff Roberson * 38510a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 38520a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 38530a81b439SJeff Roberson * the caller should retry. 38544d104ba0SAlexander Motin */ 38550a81b439SJeff Roberson static __noinline bool 38560a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 38570a81b439SJeff Roberson int itemdomain) 38580a81b439SJeff Roberson { 3859dfe13344SJeff Roberson uma_cache_bucket_t cbucket; 38600a81b439SJeff Roberson uma_bucket_t bucket; 3861cc7ce83aSJeff Roberson int domain; 38620a81b439SJeff Roberson 38630a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 38640a81b439SJeff Roberson 386520a4e154SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) 38660a81b439SJeff Roberson return false; 38670a81b439SJeff Roberson 3868cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 38690a81b439SJeff Roberson 38700a81b439SJeff Roberson /* 3871dfe13344SJeff Roberson * FIRSTTOUCH domains need to free to the correct zdom. When 3872dfe13344SJeff Roberson * enabled this is the zdom of the item. The bucket is the 3873dfe13344SJeff Roberson * cross bucket if the current domain and itemdomain do not match. 38740a81b439SJeff Roberson */ 3875dfe13344SJeff Roberson cbucket = &cache->uc_freebucket; 3876dfe13344SJeff Roberson #ifdef NUMA 3877dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 38780a81b439SJeff Roberson domain = PCPU_GET(domain); 38790a81b439SJeff Roberson if (domain != itemdomain) { 3880dfe13344SJeff Roberson cbucket = &cache->uc_crossbucket; 3881dfe13344SJeff Roberson if (cbucket->ucb_cnt != 0) 3882dfe13344SJeff Roberson atomic_add_64(&zone->uz_xdomain, 3883dfe13344SJeff Roberson cbucket->ucb_cnt); 3884dfe13344SJeff Roberson } 38850a81b439SJeff Roberson } else 38860a81b439SJeff Roberson #endif 3887dfe13344SJeff Roberson itemdomain = domain = 0; 3888dfe13344SJeff Roberson bucket = cache_bucket_unload(cbucket); 38890a81b439SJeff Roberson 38900a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 38910a81b439SJeff Roberson critical_exit(); 38920a81b439SJeff Roberson 38930a81b439SJeff Roberson if (bucket != NULL) 38940a81b439SJeff Roberson zone_free_bucket(zone, bucket, udata, domain, itemdomain); 3895a553d4b8SJeff Roberson 38966fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT); 38971431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p", 38981431a748SGleb Smirnoff zone->uz_name, zone, bucket); 3899fc03d22bSJeff Roberson critical_enter(); 39000a81b439SJeff Roberson if (bucket == NULL) 39010a81b439SJeff Roberson return (false); 3902cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3903dfe13344SJeff Roberson #ifdef NUMA 3904fc03d22bSJeff Roberson /* 39050a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 39060a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 39070a81b439SJeff Roberson * the free bucket. 3908fc03d22bSJeff Roberson */ 3909dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 39100a81b439SJeff Roberson domain = PCPU_GET(domain); 3911376b1ba3SJeff Roberson if (domain != itemdomain && 3912376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 3913376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 39140a81b439SJeff Roberson return (true); 39150a81b439SJeff Roberson } 39160a81b439SJeff Roberson } 39170a81b439SJeff Roberson #endif 39180a81b439SJeff Roberson /* 39190a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 39200a81b439SJeff Roberson */ 3921376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 3922fc03d22bSJeff Roberson critical_exit(); 39236fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 39240a81b439SJeff Roberson critical_enter(); 39250a81b439SJeff Roberson } else 3926376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 39278355f576SJeff Roberson 39280a81b439SJeff Roberson return (true); 39298355f576SJeff Roberson } 39308355f576SJeff Roberson 3931ab3185d1SJeff Roberson void 3932ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata) 3933ab3185d1SJeff Roberson { 3934ab3185d1SJeff Roberson 3935ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 393619fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3937ab3185d1SJeff Roberson 3938e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_domain zone %s(%p)", zone->uz_name, zone); 3939ab3185d1SJeff Roberson 3940ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3941ab3185d1SJeff Roberson ("uma_zfree_domain: called with spinlock or critical section held")); 3942ab3185d1SJeff Roberson 3943ab3185d1SJeff Roberson /* uma_zfree(..., NULL) does nothing, to match free(9). */ 3944ab3185d1SJeff Roberson if (item == NULL) 3945ab3185d1SJeff Roberson return; 3946ab3185d1SJeff Roberson zone_free_item(zone, item, udata, SKIP_NONE); 3947ab3185d1SJeff Roberson } 3948ab3185d1SJeff Roberson 39498355f576SJeff Roberson static void 3950bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 39518355f576SJeff Roberson { 3952bb15d1c7SGleb Smirnoff uma_keg_t keg; 3953ab3185d1SJeff Roberson uma_domain_t dom; 39549b8db4d0SRyan Libby int freei; 3955099a0e58SBosko Milekic 3956bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 39578b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3958ab3185d1SJeff Roberson 39598355f576SJeff Roberson /* Do we need to remove from any lists? */ 39608b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3961099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 39628355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3963ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 39648355f576SJeff Roberson } else if (slab->us_freecount == 0) { 39658355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3966ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 39678355f576SJeff Roberson } 39688355f576SJeff Roberson 3969ef72505eSJeff Roberson /* Slab management. */ 39701e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 39719b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 39728355f576SJeff Roberson slab->us_freecount++; 39738355f576SJeff Roberson 3974ef72505eSJeff Roberson /* Keg statistics. */ 39758b987a77SJeff Roberson dom->ud_free++; 39760095a784SJeff Roberson } 39770095a784SJeff Roberson 39780095a784SJeff Roberson static void 3979b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 39800095a784SJeff Roberson { 39818b987a77SJeff Roberson struct mtx *lock; 3982b75c4efcSAndrew Turner uma_zone_t zone; 39830095a784SJeff Roberson uma_slab_t slab; 39840095a784SJeff Roberson uma_keg_t keg; 39850095a784SJeff Roberson uint8_t *mem; 39868b987a77SJeff Roberson void *item; 39870095a784SJeff Roberson int i; 39888355f576SJeff Roberson 3989b75c4efcSAndrew Turner zone = arg; 3990bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 39918b987a77SJeff Roberson lock = NULL; 399254c5ae80SRyan Libby if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0)) 39938b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 39940095a784SJeff Roberson for (i = 0; i < cnt; i++) { 39950095a784SJeff Roberson item = bucket[i]; 399654c5ae80SRyan Libby if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) { 39970095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 39988b987a77SJeff Roberson } else { 39998b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 400054c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0) 40018b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 40028b987a77SJeff Roberson else 40038b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 40048b987a77SJeff Roberson } 40058b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 40068b987a77SJeff Roberson if (lock != NULL) 40078b987a77SJeff Roberson mtx_unlock(lock); 40088b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 40098b987a77SJeff Roberson } 4010bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 40110095a784SJeff Roberson } 40128b987a77SJeff Roberson if (lock != NULL) 40138b987a77SJeff Roberson mtx_unlock(lock); 40148355f576SJeff Roberson } 40158355f576SJeff Roberson 40160095a784SJeff Roberson /* 40170095a784SJeff Roberson * Frees a single item to any zone. 40180095a784SJeff Roberson * 40190095a784SJeff Roberson * Arguments: 40200095a784SJeff Roberson * zone The zone to free to 40210095a784SJeff Roberson * item The item we're freeing 40220095a784SJeff Roberson * udata User supplied data for the dtor 40230095a784SJeff Roberson * skip Skip dtors and finis 40240095a784SJeff Roberson */ 40250095a784SJeff Roberson static void 40260095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 40270095a784SJeff Roberson { 4028c5deaf04SGleb Smirnoff 4029cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 40300095a784SJeff Roberson 40310095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 40320095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 40330095a784SJeff Roberson 40340095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4035bb15d1c7SGleb Smirnoff 4036bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4037bb15d1c7SGleb Smirnoff return; 4038bb15d1c7SGleb Smirnoff 40392efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 40402efcc8cbSGleb Smirnoff 40414bd61e19SJeff Roberson if (zone->uz_max_items > 0) 40424bd61e19SJeff Roberson zone_free_limit(zone, 1); 4043bb45b411SGleb Smirnoff } 40440095a784SJeff Roberson 40458355f576SJeff Roberson /* See uma.h */ 40461c6cae97SLawrence Stewart int 4047736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4048736ee590SJeff Roberson { 4049bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 4050003cf08bSMark Johnston int count; 4051bb15d1c7SGleb Smirnoff 40524bd61e19SJeff Roberson /* 40534bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 40544bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 40554bd61e19SJeff Roberson * way to clear a limit. 40564bd61e19SJeff Roberson */ 4057bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4058003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4059003cf08bSMark Johnston count = ubz != NULL ? ubz->ubz_entries : 0; 406020a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = count; 406120a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 406220a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4063bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4064cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4065cc7ce83aSJeff Roberson zone_update_caches(zone); 40664bd61e19SJeff Roberson /* We may need to wake waiters. */ 40674bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4068bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4069bb15d1c7SGleb Smirnoff 4070bb15d1c7SGleb Smirnoff return (nitems); 4071bb15d1c7SGleb Smirnoff } 4072bb15d1c7SGleb Smirnoff 4073bb15d1c7SGleb Smirnoff /* See uma.h */ 4074003cf08bSMark Johnston void 4075bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4076bb15d1c7SGleb Smirnoff { 4077003cf08bSMark Johnston struct uma_bucket_zone *ubz; 4078003cf08bSMark Johnston int bpcpu; 4079bb15d1c7SGleb Smirnoff 4080bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4081003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4082003cf08bSMark Johnston if (ubz != NULL) { 4083003cf08bSMark Johnston bpcpu = 2; 4084dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 4085003cf08bSMark Johnston /* Count the cross-domain bucket. */ 4086003cf08bSMark Johnston bpcpu++; 4087003cf08bSMark Johnston nitems -= ubz->ubz_entries * bpcpu * mp_ncpus; 408820a4e154SJeff Roberson zone->uz_bucket_size_max = ubz->ubz_entries; 4089003cf08bSMark Johnston } else { 409020a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 4091003cf08bSMark Johnston } 409220a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 409320a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4094bb15d1c7SGleb Smirnoff zone->uz_bkt_max = nitems; 4095bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4096736ee590SJeff Roberson } 4097736ee590SJeff Roberson 4098736ee590SJeff Roberson /* See uma.h */ 4099e49471b0SAndre Oppermann int 4100e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4101e49471b0SAndre Oppermann { 4102e49471b0SAndre Oppermann int nitems; 4103e49471b0SAndre Oppermann 4104727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4105e49471b0SAndre Oppermann 4106e49471b0SAndre Oppermann return (nitems); 4107e49471b0SAndre Oppermann } 4108e49471b0SAndre Oppermann 4109e49471b0SAndre Oppermann /* See uma.h */ 41102f891cd5SPawel Jakub Dawidek void 41112f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 41122f891cd5SPawel Jakub Dawidek { 41132f891cd5SPawel Jakub Dawidek 4114727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 41152f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 41162f891cd5SPawel Jakub Dawidek } 41172f891cd5SPawel Jakub Dawidek 41182f891cd5SPawel Jakub Dawidek /* See uma.h */ 411954503a13SJonathan T. Looney void 412054503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 412154503a13SJonathan T. Looney { 412254503a13SJonathan T. Looney 4123727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4124e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 412554503a13SJonathan T. Looney } 412654503a13SJonathan T. Looney 412754503a13SJonathan T. Looney /* See uma.h */ 4128c4ae7908SLawrence Stewart int 4129c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4130c4ae7908SLawrence Stewart { 4131c4ae7908SLawrence Stewart int64_t nitems; 4132c4ae7908SLawrence Stewart u_int i; 4133c4ae7908SLawrence Stewart 4134bfb6b7a1SJeff Roberson nitems = 0; 4135bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER) 41362efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 41372efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4138727c6918SJeff Roberson CPU_FOREACH(i) 4139727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4140727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4141c4ae7908SLawrence Stewart 4142c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4143c4ae7908SLawrence Stewart } 4144c4ae7908SLawrence Stewart 414520a4e154SJeff Roberson static uint64_t 414620a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 414720a4e154SJeff Roberson { 414820a4e154SJeff Roberson uint64_t nitems; 414920a4e154SJeff Roberson u_int i; 415020a4e154SJeff Roberson 4151bfb6b7a1SJeff Roberson nitems = 0; 4152bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER) 415320a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4154727c6918SJeff Roberson CPU_FOREACH(i) 4155727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 415620a4e154SJeff Roberson 415720a4e154SJeff Roberson return (nitems); 415820a4e154SJeff Roberson } 415920a4e154SJeff Roberson 416020a4e154SJeff Roberson static uint64_t 416120a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 416220a4e154SJeff Roberson { 416320a4e154SJeff Roberson uint64_t nitems; 416420a4e154SJeff Roberson u_int i; 416520a4e154SJeff Roberson 4166bfb6b7a1SJeff Roberson nitems = 0; 4167bfb6b7a1SJeff Roberson if (zone->uz_frees != EARLY_COUNTER) 416820a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4169727c6918SJeff Roberson CPU_FOREACH(i) 4170727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 417120a4e154SJeff Roberson 417220a4e154SJeff Roberson return (nitems); 417320a4e154SJeff Roberson } 417420a4e154SJeff Roberson 417531c251a0SJeff Roberson #ifdef INVARIANTS 417631c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */ 417731c251a0SJeff Roberson static uint64_t 417831c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg) 417931c251a0SJeff Roberson { 418031c251a0SJeff Roberson uma_zone_t z; 418131c251a0SJeff Roberson uint64_t nitems; 418231c251a0SJeff Roberson 418331c251a0SJeff Roberson nitems = 0; 418431c251a0SJeff Roberson LIST_FOREACH(z, &keg->uk_zones, uz_link) 418531c251a0SJeff Roberson nitems += uma_zone_get_allocs(z); 418631c251a0SJeff Roberson 418731c251a0SJeff Roberson return (nitems); 418831c251a0SJeff Roberson } 418931c251a0SJeff Roberson #endif 419031c251a0SJeff Roberson 4191c4ae7908SLawrence Stewart /* See uma.h */ 4192736ee590SJeff Roberson void 4193099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4194099a0e58SBosko Milekic { 4195e20a199fSJeff Roberson uma_keg_t keg; 4196e20a199fSJeff Roberson 4197bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4198727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4199e20a199fSJeff Roberson keg->uk_init = uminit; 4200099a0e58SBosko Milekic } 4201099a0e58SBosko Milekic 4202099a0e58SBosko Milekic /* See uma.h */ 4203099a0e58SBosko Milekic void 4204099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4205099a0e58SBosko Milekic { 4206e20a199fSJeff Roberson uma_keg_t keg; 4207e20a199fSJeff Roberson 4208bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4209727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4210e20a199fSJeff Roberson keg->uk_fini = fini; 4211099a0e58SBosko Milekic } 4212099a0e58SBosko Milekic 4213099a0e58SBosko Milekic /* See uma.h */ 4214099a0e58SBosko Milekic void 4215099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4216099a0e58SBosko Milekic { 4217af526374SJeff Roberson 4218727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4219099a0e58SBosko Milekic zone->uz_init = zinit; 4220099a0e58SBosko Milekic } 4221099a0e58SBosko Milekic 4222099a0e58SBosko Milekic /* See uma.h */ 4223099a0e58SBosko Milekic void 4224099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4225099a0e58SBosko Milekic { 4226af526374SJeff Roberson 4227727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4228099a0e58SBosko Milekic zone->uz_fini = zfini; 4229099a0e58SBosko Milekic } 4230099a0e58SBosko Milekic 4231099a0e58SBosko Milekic /* See uma.h */ 4232099a0e58SBosko Milekic void 42338355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 42348355f576SJeff Roberson { 42350095a784SJeff Roberson uma_keg_t keg; 4236e20a199fSJeff Roberson 4237bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4238727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 42390095a784SJeff Roberson keg->uk_freef = freef; 42408355f576SJeff Roberson } 42418355f576SJeff Roberson 42428355f576SJeff Roberson /* See uma.h */ 42438355f576SJeff Roberson void 42448355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 42458355f576SJeff Roberson { 4246e20a199fSJeff Roberson uma_keg_t keg; 4247e20a199fSJeff Roberson 4248bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4249727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4250e20a199fSJeff Roberson keg->uk_allocf = allocf; 42518355f576SJeff Roberson } 42528355f576SJeff Roberson 42538355f576SJeff Roberson /* See uma.h */ 42546fd34d6fSJeff Roberson void 42556fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 42566fd34d6fSJeff Roberson { 42576fd34d6fSJeff Roberson uma_keg_t keg; 42586fd34d6fSJeff Roberson 4259bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4260727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 42616fd34d6fSJeff Roberson keg->uk_reserve = items; 42626fd34d6fSJeff Roberson } 42636fd34d6fSJeff Roberson 42646fd34d6fSJeff Roberson /* See uma.h */ 42658355f576SJeff Roberson int 4266a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 42678355f576SJeff Roberson { 4268099a0e58SBosko Milekic uma_keg_t keg; 42698355f576SJeff Roberson vm_offset_t kva; 42709ba30bcbSZbigniew Bodek u_int pages; 42718355f576SJeff Roberson 4272bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4273727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4274727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 42758355f576SJeff Roberson 427679c9f942SJeff Roberson pages = howmany(count, keg->uk_ipers) * keg->uk_ppera; 4277a553d4b8SJeff Roberson 4278a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4279a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4280a4915c21SAttilio Rao #else 4281a4915c21SAttilio Rao if (1) { 4282a4915c21SAttilio Rao #endif 428357223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4284d1f42ac2SAlan Cox if (kva == 0) 42858355f576SJeff Roberson return (0); 4286a4915c21SAttilio Rao } else 4287a4915c21SAttilio Rao kva = 0; 4288bb15d1c7SGleb Smirnoff 4289bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4290bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4291099a0e58SBosko Milekic keg->uk_kva = kva; 4292a4915c21SAttilio Rao keg->uk_offset = 0; 4293bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4294a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4295a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4296a4915c21SAttilio Rao #else 4297a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4298a4915c21SAttilio Rao #endif 4299cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4300cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4301cc7ce83aSJeff Roberson zone_update_caches(zone); 4302bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4303af526374SJeff Roberson 43048355f576SJeff Roberson return (1); 43058355f576SJeff Roberson } 43068355f576SJeff Roberson 43078355f576SJeff Roberson /* See uma.h */ 43088355f576SJeff Roberson void 43098355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 43108355f576SJeff Roberson { 4311920239efSMark Johnston struct vm_domainset_iter di; 4312ab3185d1SJeff Roberson uma_domain_t dom; 43138355f576SJeff Roberson uma_slab_t slab; 4314099a0e58SBosko Milekic uma_keg_t keg; 431586220393SMark Johnston int aflags, domain, slabs; 43168355f576SJeff Roberson 4317bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 431879c9f942SJeff Roberson slabs = howmany(items, keg->uk_ipers); 4319194a979eSMark Johnston while (slabs-- > 0) { 432086220393SMark Johnston aflags = M_NOWAIT; 432186220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 432286220393SMark Johnston &aflags); 432386220393SMark Johnston for (;;) { 432486220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 432586220393SMark Johnston aflags); 432686220393SMark Johnston if (slab != NULL) { 4327ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 43288b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 432986220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 433086220393SMark Johnston us_link); 43318b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 4332920239efSMark Johnston break; 43338355f576SJeff Roberson } 43348b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 433586220393SMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 433686220393SMark Johnston } 433786220393SMark Johnston } 433886220393SMark Johnston } 43398355f576SJeff Roberson 43408355f576SJeff Roberson /* See uma.h */ 434108cfa56eSMark Johnston void 434208cfa56eSMark Johnston uma_reclaim(int req) 43438355f576SJeff Roberson { 434444ec2b63SKonstantin Belousov 43451431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 434608cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 434786bbae32SJeff Roberson bucket_enable(); 434808cfa56eSMark Johnston 434908cfa56eSMark Johnston switch (req) { 435008cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 435120a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 435208cfa56eSMark Johnston break; 435308cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 435408cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 435520a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 435608cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 435708cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 435820a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4359a2de44abSAlexander Motin } 436008cfa56eSMark Johnston break; 436108cfa56eSMark Johnston default: 436208cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 436308cfa56eSMark Johnston } 43640f9b7bf3SMark Johnston 43658355f576SJeff Roberson /* 43668355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 43678355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 43688355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 43698355f576SJeff Roberson */ 43709b8db4d0SRyan Libby zone_drain(slabzones[0], NULL); 43719b8db4d0SRyan Libby zone_drain(slabzones[1], NULL); 4372cae33c14SJeff Roberson bucket_zone_drain(); 437308cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 43748355f576SJeff Roberson } 43758355f576SJeff Roberson 43762e47807cSJeff Roberson static volatile int uma_reclaim_needed; 437744ec2b63SKonstantin Belousov 437844ec2b63SKonstantin Belousov void 437944ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 438044ec2b63SKonstantin Belousov { 438144ec2b63SKonstantin Belousov 43822e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 43832e47807cSJeff Roberson wakeup(uma_reclaim); 438444ec2b63SKonstantin Belousov } 438544ec2b63SKonstantin Belousov 438644ec2b63SKonstantin Belousov void 438744ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 438844ec2b63SKonstantin Belousov { 438944ec2b63SKonstantin Belousov 439044ec2b63SKonstantin Belousov for (;;) { 439108cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4392200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 439308cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 43942e47807cSJeff Roberson hz); 439508cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 43969b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 439708cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4398200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 43992e47807cSJeff Roberson /* Don't fire more than once per-second. */ 44002e47807cSJeff Roberson pause("umarclslp", hz); 440144ec2b63SKonstantin Belousov } 440244ec2b63SKonstantin Belousov } 440344ec2b63SKonstantin Belousov 4404663b416fSJohn Baldwin /* See uma.h */ 440508cfa56eSMark Johnston void 440608cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 440708cfa56eSMark Johnston { 440808cfa56eSMark Johnston 440908cfa56eSMark Johnston switch (req) { 441008cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 441120a4e154SJeff Roberson zone_trim(zone, NULL); 441208cfa56eSMark Johnston break; 441308cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 441420a4e154SJeff Roberson zone_drain(zone, NULL); 441508cfa56eSMark Johnston break; 441608cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 441708cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 441820a4e154SJeff Roberson zone_drain(zone, NULL); 441908cfa56eSMark Johnston break; 442008cfa56eSMark Johnston default: 442108cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 442208cfa56eSMark Johnston } 442308cfa56eSMark Johnston } 442408cfa56eSMark Johnston 442508cfa56eSMark Johnston /* See uma.h */ 4426663b416fSJohn Baldwin int 4427663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 4428663b416fSJohn Baldwin { 4429663b416fSJohn Baldwin 4430727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 44316c125b8dSMohan Srinivasan } 44326c125b8dSMohan Srinivasan 44332e47807cSJeff Roberson unsigned long 44342e47807cSJeff Roberson uma_limit(void) 44352e47807cSJeff Roberson { 44362e47807cSJeff Roberson 44372e47807cSJeff Roberson return (uma_kmem_limit); 44382e47807cSJeff Roberson } 44392e47807cSJeff Roberson 44402e47807cSJeff Roberson void 44412e47807cSJeff Roberson uma_set_limit(unsigned long limit) 44422e47807cSJeff Roberson { 44432e47807cSJeff Roberson 44442e47807cSJeff Roberson uma_kmem_limit = limit; 44452e47807cSJeff Roberson } 44462e47807cSJeff Roberson 44472e47807cSJeff Roberson unsigned long 44482e47807cSJeff Roberson uma_size(void) 44492e47807cSJeff Roberson { 44502e47807cSJeff Roberson 4451058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 4452ad5b0f5bSJeff Roberson } 4453ad5b0f5bSJeff Roberson 4454ad5b0f5bSJeff Roberson long 4455ad5b0f5bSJeff Roberson uma_avail(void) 4456ad5b0f5bSJeff Roberson { 4457ad5b0f5bSJeff Roberson 4458058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 44592e47807cSJeff Roberson } 44602e47807cSJeff Roberson 4461a0d4b0aeSRobert Watson #ifdef DDB 44628355f576SJeff Roberson /* 44637a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 44647a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 44657a52a97eSRobert Watson * 44667a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 44677a52a97eSRobert Watson * per-CPU cache statistic. 44687a52a97eSRobert Watson * 44697a52a97eSRobert Watson */ 44707a52a97eSRobert Watson static void 44710f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 4472c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 44737a52a97eSRobert Watson { 44747a52a97eSRobert Watson uma_cache_t cache; 4475c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 44767a52a97eSRobert Watson int cachefree, cpu; 44777a52a97eSRobert Watson 4478c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 44797a52a97eSRobert Watson cachefree = 0; 44803aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 44817a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 4482376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 4483376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 4484376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 4485376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 44867a52a97eSRobert Watson allocs += cache->uc_allocs; 44877a52a97eSRobert Watson frees += cache->uc_frees; 44887a52a97eSRobert Watson } 44892efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 44902efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 4491bf965959SSean Bruno sleeps += z->uz_sleeps; 4492c1685086SJeff Roberson xdomain += z->uz_xdomain; 44937a52a97eSRobert Watson if (cachefreep != NULL) 44947a52a97eSRobert Watson *cachefreep = cachefree; 44957a52a97eSRobert Watson if (allocsp != NULL) 44967a52a97eSRobert Watson *allocsp = allocs; 44977a52a97eSRobert Watson if (freesp != NULL) 44987a52a97eSRobert Watson *freesp = frees; 4499bf965959SSean Bruno if (sleepsp != NULL) 4500bf965959SSean Bruno *sleepsp = sleeps; 4501c1685086SJeff Roberson if (xdomainp != NULL) 4502c1685086SJeff Roberson *xdomainp = xdomain; 45037a52a97eSRobert Watson } 4504a0d4b0aeSRobert Watson #endif /* DDB */ 45057a52a97eSRobert Watson 45067a52a97eSRobert Watson static int 45077a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 45087a52a97eSRobert Watson { 45097a52a97eSRobert Watson uma_keg_t kz; 45107a52a97eSRobert Watson uma_zone_t z; 45117a52a97eSRobert Watson int count; 45127a52a97eSRobert Watson 45137a52a97eSRobert Watson count = 0; 4514111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 45157a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 45167a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 45177a52a97eSRobert Watson count++; 45187a52a97eSRobert Watson } 4519b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4520b47acb0aSGleb Smirnoff count++; 4521b47acb0aSGleb Smirnoff 4522111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 45237a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 45247a52a97eSRobert Watson } 45257a52a97eSRobert Watson 4526b47acb0aSGleb Smirnoff static void 4527b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 4528b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 4529b47acb0aSGleb Smirnoff { 4530b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 4531b47acb0aSGleb Smirnoff uma_cache_t cache; 4532b47acb0aSGleb Smirnoff int i; 4533b47acb0aSGleb Smirnoff 4534b47acb0aSGleb Smirnoff 4535b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 4536b47acb0aSGleb Smirnoff zdom = &z->uz_domain[i]; 4537b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 4538b47acb0aSGleb Smirnoff } 4539b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 4540b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 4541b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 4542b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 4543c1685086SJeff Roberson uth->uth_xdomain = z->uz_xdomain; 45441de9724eSMark Johnston 4545b47acb0aSGleb Smirnoff /* 45461de9724eSMark Johnston * While it is not normally safe to access the cache bucket pointers 45471de9724eSMark Johnston * while not on the CPU that owns the cache, we only allow the pointers 45481de9724eSMark Johnston * to be exchanged without the zone lock held, not invalidated, so 45491de9724eSMark Johnston * accept the possible race associated with bucket exchange during 45501de9724eSMark Johnston * monitoring. Use atomic_load_ptr() to ensure that the bucket pointers 45511de9724eSMark Johnston * are loaded only once. 4552b47acb0aSGleb Smirnoff */ 4553b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 4554b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 4555b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 4556b47acb0aSGleb Smirnoff continue; 4557b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 4558376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 4559376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 4560376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 4561b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 4562b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 4563b47acb0aSGleb Smirnoff } 4564b47acb0aSGleb Smirnoff } 4565b47acb0aSGleb Smirnoff 45667a52a97eSRobert Watson static int 45677a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 45687a52a97eSRobert Watson { 45697a52a97eSRobert Watson struct uma_stream_header ush; 45707a52a97eSRobert Watson struct uma_type_header uth; 457163b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 45727a52a97eSRobert Watson struct sbuf sbuf; 45737a52a97eSRobert Watson uma_keg_t kz; 45747a52a97eSRobert Watson uma_zone_t z; 45754bd61e19SJeff Roberson uint64_t items; 45768b987a77SJeff Roberson uint32_t kfree, pages; 45774e657159SMatthew D Fleming int count, error, i; 45787a52a97eSRobert Watson 457900f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 458000f0e671SMatthew D Fleming if (error != 0) 458100f0e671SMatthew D Fleming return (error); 45824e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 45831eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 458463b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 45854e657159SMatthew D Fleming 4586404a593eSMatthew D Fleming count = 0; 4587111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 45887a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 45897a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 45907a52a97eSRobert Watson count++; 45917a52a97eSRobert Watson } 45927a52a97eSRobert Watson 4593b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4594b47acb0aSGleb Smirnoff count++; 4595b47acb0aSGleb Smirnoff 45967a52a97eSRobert Watson /* 45977a52a97eSRobert Watson * Insert stream header. 45987a52a97eSRobert Watson */ 45997a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 46007a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 4601ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 46027a52a97eSRobert Watson ush.ush_count = count; 46034e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 46047a52a97eSRobert Watson 46057a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 46068b987a77SJeff Roberson kfree = pages = 0; 46078b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 46088b987a77SJeff Roberson kfree += kz->uk_domain[i].ud_free; 46098b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 46108b987a77SJeff Roberson } 46117a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 46127a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 46137a52a97eSRobert Watson ZONE_LOCK(z); 4614cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 46157a52a97eSRobert Watson uth.uth_align = kz->uk_align; 46167a52a97eSRobert Watson uth.uth_size = kz->uk_size; 46177a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 46184bd61e19SJeff Roberson if (z->uz_max_items > 0) { 46194bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 46204bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 4621bb15d1c7SGleb Smirnoff kz->uk_ppera; 46224bd61e19SJeff Roberson } else 46238b987a77SJeff Roberson uth.uth_pages = pages; 4624f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 4625bb15d1c7SGleb Smirnoff kz->uk_ppera; 4626bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 46278b987a77SJeff Roberson uth.uth_keg_free = kfree; 4628cbbb4a00SRobert Watson 4629cbbb4a00SRobert Watson /* 4630cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 4631cbbb4a00SRobert Watson * on the keg's zone list. 4632cbbb4a00SRobert Watson */ 4633e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 4634cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 4635cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 4636b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 4637b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 46382450bbb8SRobert Watson ZONE_UNLOCK(z); 463963b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 464063b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 464163b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 46427a52a97eSRobert Watson } 46437a52a97eSRobert Watson } 4644b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 4645b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 4646b47acb0aSGleb Smirnoff ZONE_LOCK(z); 4647b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 4648b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 4649b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 4650b47acb0aSGleb Smirnoff ZONE_UNLOCK(z); 4651b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 4652b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 4653b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 4654b47acb0aSGleb Smirnoff } 4655b47acb0aSGleb Smirnoff 4656111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 46574e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 46584e657159SMatthew D Fleming sbuf_delete(&sbuf); 465963b5d112SKonstantin Belousov free(ups, M_TEMP); 46607a52a97eSRobert Watson return (error); 46617a52a97eSRobert Watson } 466248c5777eSRobert Watson 46630a5a3ccbSGleb Smirnoff int 46640a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 46650a5a3ccbSGleb Smirnoff { 46660a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 466716be9f54SGleb Smirnoff int error, max; 46680a5a3ccbSGleb Smirnoff 466916be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 46700a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 46710a5a3ccbSGleb Smirnoff if (error || !req->newptr) 46720a5a3ccbSGleb Smirnoff return (error); 46730a5a3ccbSGleb Smirnoff 46740a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 46750a5a3ccbSGleb Smirnoff 46760a5a3ccbSGleb Smirnoff return (0); 46770a5a3ccbSGleb Smirnoff } 46780a5a3ccbSGleb Smirnoff 46790a5a3ccbSGleb Smirnoff int 46800a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 46810a5a3ccbSGleb Smirnoff { 468220a4e154SJeff Roberson uma_zone_t zone; 46830a5a3ccbSGleb Smirnoff int cur; 46840a5a3ccbSGleb Smirnoff 468520a4e154SJeff Roberson /* 468620a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 468720a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 468820a4e154SJeff Roberson */ 468920a4e154SJeff Roberson if (arg2 == 0) 469020a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 469120a4e154SJeff Roberson else 469220a4e154SJeff Roberson zone = arg1; 46930a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 46940a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 46950a5a3ccbSGleb Smirnoff } 46960a5a3ccbSGleb Smirnoff 469720a4e154SJeff Roberson static int 469820a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 469920a4e154SJeff Roberson { 470020a4e154SJeff Roberson uma_zone_t zone = arg1; 470120a4e154SJeff Roberson uint64_t cur; 470220a4e154SJeff Roberson 470320a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 470420a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 470520a4e154SJeff Roberson } 470620a4e154SJeff Roberson 470720a4e154SJeff Roberson static int 470820a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 470920a4e154SJeff Roberson { 471020a4e154SJeff Roberson uma_zone_t zone = arg1; 471120a4e154SJeff Roberson uint64_t cur; 471220a4e154SJeff Roberson 471320a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 471420a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 471520a4e154SJeff Roberson } 471620a4e154SJeff Roberson 47176d204a6aSRyan Libby static int 47186d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 47196d204a6aSRyan Libby { 47206d204a6aSRyan Libby struct sbuf sbuf; 47216d204a6aSRyan Libby uma_zone_t zone = arg1; 47226d204a6aSRyan Libby int error; 47236d204a6aSRyan Libby 47246d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 47256d204a6aSRyan Libby if (zone->uz_flags != 0) 47266d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 47276d204a6aSRyan Libby else 47286d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 47296d204a6aSRyan Libby error = sbuf_finish(&sbuf); 47306d204a6aSRyan Libby sbuf_delete(&sbuf); 47316d204a6aSRyan Libby 47326d204a6aSRyan Libby return (error); 47336d204a6aSRyan Libby } 47346d204a6aSRyan Libby 4735f7af5015SRyan Libby static int 4736f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 4737f7af5015SRyan Libby { 4738f7af5015SRyan Libby uma_keg_t keg = arg1; 4739f7af5015SRyan Libby int avail, effpct, total; 4740f7af5015SRyan Libby 4741f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 474254c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 47439b8db4d0SRyan Libby total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize; 4744f7af5015SRyan Libby /* 4745f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 4746f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 4747f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 4748f7af5015SRyan Libby */ 4749f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 4750f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 4751f7af5015SRyan Libby avail *= mp_maxid + 1; 4752f7af5015SRyan Libby effpct = 100 * avail / total; 4753f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 4754f7af5015SRyan Libby } 4755f7af5015SRyan Libby 47564bd61e19SJeff Roberson static int 47574bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 47584bd61e19SJeff Roberson { 47594bd61e19SJeff Roberson uma_zone_t zone = arg1; 47604bd61e19SJeff Roberson uint64_t cur; 47614bd61e19SJeff Roberson 47624bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 47634bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 47644bd61e19SJeff Roberson } 47654bd61e19SJeff Roberson 47669542ea7bSGleb Smirnoff #ifdef INVARIANTS 47679542ea7bSGleb Smirnoff static uma_slab_t 47689542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 47699542ea7bSGleb Smirnoff { 47709542ea7bSGleb Smirnoff uma_slab_t slab; 47719542ea7bSGleb Smirnoff uma_keg_t keg; 47729542ea7bSGleb Smirnoff uint8_t *mem; 47739542ea7bSGleb Smirnoff 47749542ea7bSGleb Smirnoff /* 47759542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 47769542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 47779542ea7bSGleb Smirnoff * essentially holds a reference. 47789542ea7bSGleb Smirnoff */ 4779727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 4780727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 4781bb15d1c7SGleb Smirnoff return (NULL); 478254c5ae80SRyan Libby if (zone->uz_flags & UMA_ZFLAG_VTOSLAB) 4783727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 4784bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 478554c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0) 4786727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 47878b987a77SJeff Roberson KEG_LOCK(keg, 0); 47889542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 47898b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 47909542ea7bSGleb Smirnoff 47919542ea7bSGleb Smirnoff return (slab); 47929542ea7bSGleb Smirnoff } 47939542ea7bSGleb Smirnoff 4794c5deaf04SGleb Smirnoff static bool 4795c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 4796c5deaf04SGleb Smirnoff { 4797c5deaf04SGleb Smirnoff 4798727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 4799c5deaf04SGleb Smirnoff return (true); 4800c5deaf04SGleb Smirnoff 4801bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 4802c5deaf04SGleb Smirnoff } 4803c5deaf04SGleb Smirnoff 4804c5deaf04SGleb Smirnoff static bool 4805c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 4806c5deaf04SGleb Smirnoff { 4807c5deaf04SGleb Smirnoff uintptr_t idx; 4808c5deaf04SGleb Smirnoff 4809c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 4810c5deaf04SGleb Smirnoff return (true); 4811c5deaf04SGleb Smirnoff 4812c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 4813c5deaf04SGleb Smirnoff return (false); 4814c5deaf04SGleb Smirnoff 4815c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 4816c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 4817c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 4818c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 4819c5deaf04SGleb Smirnoff } 4820c5deaf04SGleb Smirnoff 4821c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 4822c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 4823c5deaf04SGleb Smirnoff return (true); 4824c5deaf04SGleb Smirnoff } 4825c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 4826c5deaf04SGleb Smirnoff 4827c5deaf04SGleb Smirnoff return (false); 4828c5deaf04SGleb Smirnoff } 4829c5deaf04SGleb Smirnoff 48309542ea7bSGleb Smirnoff /* 48319542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 48329542ea7bSGleb Smirnoff * 48339542ea7bSGleb Smirnoff */ 48349542ea7bSGleb Smirnoff static void 48359542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 48369542ea7bSGleb Smirnoff { 48379542ea7bSGleb Smirnoff uma_keg_t keg; 48389542ea7bSGleb Smirnoff int freei; 48399542ea7bSGleb Smirnoff 48409542ea7bSGleb Smirnoff if (slab == NULL) { 48419542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 48429542ea7bSGleb Smirnoff if (slab == NULL) 48439542ea7bSGleb Smirnoff panic("uma: item %p did not belong to zone %s\n", 48449542ea7bSGleb Smirnoff item, zone->uz_name); 48459542ea7bSGleb Smirnoff } 4846584061b4SJeff Roberson keg = zone->uz_keg; 48471e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 48489542ea7bSGleb Smirnoff 4849815db204SRyan Libby if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 48509542ea7bSGleb Smirnoff panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n", 48519542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 4852815db204SRyan Libby BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 48539542ea7bSGleb Smirnoff } 48549542ea7bSGleb Smirnoff 48559542ea7bSGleb Smirnoff /* 48569542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 48579542ea7bSGleb Smirnoff * and duplicate frees. 48589542ea7bSGleb Smirnoff * 48599542ea7bSGleb Smirnoff */ 48609542ea7bSGleb Smirnoff static void 48619542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 48629542ea7bSGleb Smirnoff { 48639542ea7bSGleb Smirnoff uma_keg_t keg; 48649542ea7bSGleb Smirnoff int freei; 48659542ea7bSGleb Smirnoff 48669542ea7bSGleb Smirnoff if (slab == NULL) { 48679542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 48689542ea7bSGleb Smirnoff if (slab == NULL) 48699542ea7bSGleb Smirnoff panic("uma: Freed item %p did not belong to zone %s\n", 48709542ea7bSGleb Smirnoff item, zone->uz_name); 48719542ea7bSGleb Smirnoff } 4872584061b4SJeff Roberson keg = zone->uz_keg; 48731e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 48749542ea7bSGleb Smirnoff 48759542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 48769542ea7bSGleb Smirnoff panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n", 48779542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48789542ea7bSGleb Smirnoff 48791e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 48809542ea7bSGleb Smirnoff panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n", 48819542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48829542ea7bSGleb Smirnoff 4883815db204SRyan Libby if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 48849542ea7bSGleb Smirnoff panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n", 48859542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48869542ea7bSGleb Smirnoff 4887815db204SRyan Libby BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 48889542ea7bSGleb Smirnoff } 48899542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 48909542ea7bSGleb Smirnoff 489148c5777eSRobert Watson #ifdef DDB 489246d70077SConrad Meyer static int64_t 489346d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 48940223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 489548c5777eSRobert Watson { 489646d70077SConrad Meyer uint64_t frees; 48970f9b7bf3SMark Johnston int i; 489848c5777eSRobert Watson 489948c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 490046d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 49012efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 490246d70077SConrad Meyer *sleeps = z->uz_sleeps; 490346d70077SConrad Meyer *cachefree = 0; 490446d70077SConrad Meyer *xdomain = 0; 490548c5777eSRobert Watson } else 490646d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 490746d70077SConrad Meyer xdomain); 49088b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 49098b987a77SJeff Roberson *cachefree += z->uz_domain[i].uzd_nitems; 4910e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 491148c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 49128b987a77SJeff Roberson *cachefree += kz->uk_domain[i].ud_free; 49138b987a77SJeff Roberson } 491446d70077SConrad Meyer *used = *allocs - frees; 491546d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 491646d70077SConrad Meyer } 49170f9b7bf3SMark Johnston 491846d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 491946d70077SConrad Meyer { 492046d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 492146d70077SConrad Meyer uma_keg_t kz; 492246d70077SConrad Meyer uma_zone_t z; 492346d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 492446d70077SConrad Meyer long cachefree; 492546d70077SConrad Meyer /* variables for sorting */ 492646d70077SConrad Meyer uma_keg_t cur_keg; 492746d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 492846d70077SConrad Meyer int64_t cur_size, last_size, size; 492946d70077SConrad Meyer int ties; 493046d70077SConrad Meyer 493146d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 493246d70077SConrad Meyer if (modif[0] == 'i') { 493346d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 493446d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 493546d70077SConrad Meyer } else { 493646d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 493746d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 493846d70077SConrad Meyer } 493946d70077SConrad Meyer 494046d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 494146d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 494246d70077SConrad Meyer 494346d70077SConrad Meyer /* Sort the zones with largest size first. */ 494446d70077SConrad Meyer last_zone = NULL; 494546d70077SConrad Meyer last_size = INT64_MAX; 494646d70077SConrad Meyer for (;;) { 494746d70077SConrad Meyer cur_zone = NULL; 494846d70077SConrad Meyer cur_size = -1; 494946d70077SConrad Meyer ties = 0; 495046d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 495146d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 495246d70077SConrad Meyer /* 495346d70077SConrad Meyer * In the case of size ties, print out zones 495446d70077SConrad Meyer * in the order they are encountered. That is, 495546d70077SConrad Meyer * when we encounter the most recently output 495646d70077SConrad Meyer * zone, we have already printed all preceding 495746d70077SConrad Meyer * ties, and we must print all following ties. 495846d70077SConrad Meyer */ 495946d70077SConrad Meyer if (z == last_zone) { 496046d70077SConrad Meyer ties = 1; 496146d70077SConrad Meyer continue; 496246d70077SConrad Meyer } 496346d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 496446d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 496546d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 496646d70077SConrad Meyer { 496746d70077SConrad Meyer cur_size = size; 496846d70077SConrad Meyer cur_zone = z; 496946d70077SConrad Meyer cur_keg = kz; 497046d70077SConrad Meyer } 497146d70077SConrad Meyer } 497246d70077SConrad Meyer } 497346d70077SConrad Meyer if (cur_zone == NULL) 497446d70077SConrad Meyer break; 497546d70077SConrad Meyer 497646d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 497746d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 497846d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 497946d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 498046d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 498120a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 498220a4e154SJeff Roberson xdomain); 498346d70077SConrad Meyer 4984687c94aaSJohn Baldwin if (db_pager_quit) 4985687c94aaSJohn Baldwin return; 498646d70077SConrad Meyer last_zone = cur_zone; 498746d70077SConrad Meyer last_size = cur_size; 498848c5777eSRobert Watson } 498948c5777eSRobert Watson } 499003175483SAlexander Motin 499103175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 499203175483SAlexander Motin { 499303175483SAlexander Motin uma_zone_t z; 4994ab3185d1SJeff Roberson uint64_t allocs, frees; 49950f9b7bf3SMark Johnston long cachefree; 49960f9b7bf3SMark Johnston int i; 499703175483SAlexander Motin 499803175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 499903175483SAlexander Motin "Requests", "Bucket"); 500003175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 5001c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 50020f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 50030f9b7bf3SMark Johnston cachefree += z->uz_domain[i].uzd_nitems; 50040f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 500503175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 500603175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 500720a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 500803175483SAlexander Motin if (db_pager_quit) 500903175483SAlexander Motin return; 501003175483SAlexander Motin } 501103175483SAlexander Motin } 50129542ea7bSGleb Smirnoff #endif /* DDB */ 5013