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 104a81c400eSJeff Roberson #include <machine/md_var.h> 105a81c400eSJeff 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 /* 156a81c400eSJeff Roberson * First available virual address for boot time allocations. 157ac0a6fd0SGleb Smirnoff */ 158a81c400eSJeff Roberson static vm_offset_t bootstart; 159a81c400eSJeff 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, 177a81c400eSJeff 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 259a81c400eSJeff 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 *); 278a81c400eSJeff 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 371a81c400eSJeff 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 /* 457a81c400eSJeff Roberson * Don't allocate buckets in low memory situations. 458cae33c14SJeff Roberson */ 459cae33c14SJeff Roberson if (bucketdisable) 460cae33c14SJeff Roberson return (NULL); 461a81c400eSJeff 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 { 1423a81c400eSJeff Roberson vm_paddr_t pa; 1424a81c400eSJeff Roberson vm_page_t m; 1425ac0a6fd0SGleb Smirnoff void *mem; 1426ac0a6fd0SGleb Smirnoff int pages; 1427a81c400eSJeff Roberson int i; 1428099a0e58SBosko Milekic 1429f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1430f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1431a81c400eSJeff Roberson 1432f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1433a81c400eSJeff Roberson m = vm_page_alloc_contig_domain(NULL, 0, domain, 1434a81c400eSJeff Roberson malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages, 1435a81c400eSJeff Roberson (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT); 1436a81c400eSJeff Roberson if (m == NULL) 1437a81c400eSJeff Roberson return (NULL); 1438a81c400eSJeff Roberson 1439a81c400eSJeff Roberson pa = VM_PAGE_TO_PHYS(m); 1440a81c400eSJeff Roberson for (i = 0; i < pages; i++, pa += PAGE_SIZE) { 1441a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1442a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1443a81c400eSJeff Roberson if ((wait & M_NODUMP) == 0) 1444a81c400eSJeff Roberson dump_add_page(pa); 1445a81c400eSJeff Roberson #endif 1446a81c400eSJeff Roberson } 1447a81c400eSJeff Roberson /* Allocate KVA and indirectly advance bootmem. */ 1448a81c400eSJeff Roberson mem = (void *)pmap_map(&bootmem, m->phys_addr, 1449a81c400eSJeff Roberson m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE); 1450a81c400eSJeff Roberson if ((wait & M_ZERO) != 0) 1451a81c400eSJeff Roberson bzero(mem, pages * PAGE_SIZE); 1452f7d35785SGleb Smirnoff 1453f7d35785SGleb Smirnoff return (mem); 1454f7d35785SGleb Smirnoff } 1455f7d35785SGleb Smirnoff 1456a81c400eSJeff Roberson static void 1457a81c400eSJeff Roberson startup_free(void *mem, vm_size_t bytes) 1458a81c400eSJeff Roberson { 1459a81c400eSJeff Roberson vm_offset_t va; 1460a81c400eSJeff Roberson vm_page_t m; 1461a81c400eSJeff Roberson 1462a81c400eSJeff Roberson va = (vm_offset_t)mem; 1463a81c400eSJeff Roberson m = PHYS_TO_VM_PAGE(pmap_kextract(va)); 1464a81c400eSJeff Roberson pmap_remove(kernel_pmap, va, va + bytes); 1465a81c400eSJeff Roberson for (; bytes != 0; bytes -= PAGE_SIZE, m++) { 1466a81c400eSJeff Roberson #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \ 1467a81c400eSJeff Roberson defined(__riscv) || defined(__powerpc64__) 1468a81c400eSJeff Roberson dump_drop_page(VM_PAGE_TO_PHYS(m)); 1469a81c400eSJeff Roberson #endif 1470a81c400eSJeff Roberson vm_page_unwire_noq(m); 1471a81c400eSJeff Roberson vm_page_free(m); 1472a81c400eSJeff Roberson } 1473a81c400eSJeff Roberson } 1474a81c400eSJeff 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); 1524*20526802SAndrew Gallatin if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain))) 1525*20526802SAndrew Gallatin p = NULL; 1526*20526802SAndrew Gallatin else 1527*20526802SAndrew Gallatin p = vm_page_alloc_domain(NULL, 0, 1528*20526802SAndrew Gallatin pc->pc_domain, flags); 1529ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1530ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1531ab3059a8SMatt Macy #endif 1532ab3059a8SMatt Macy } 1533ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1534ab3059a8SMatt Macy goto fail; 1535ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1536ab3059a8SMatt Macy } 1537ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1538ab3059a8SMatt Macy goto fail; 1539ab3059a8SMatt Macy zkva = addr; 1540ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1541ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1542ab3059a8SMatt Macy zkva += PAGE_SIZE; 1543ab3059a8SMatt Macy } 1544ab3059a8SMatt Macy return ((void*)addr); 1545ab3059a8SMatt Macy fail: 1546ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 154788ea538aSMark Johnston vm_page_unwire_noq(p); 1548ab3059a8SMatt Macy vm_page_free(p); 1549ab3059a8SMatt Macy } 1550ab3059a8SMatt Macy return (NULL); 1551ab3059a8SMatt Macy } 1552ab3059a8SMatt Macy 15538355f576SJeff Roberson /* 15548355f576SJeff Roberson * Allocates a number of pages from within an object 15558355f576SJeff Roberson * 15568355f576SJeff Roberson * Arguments: 15578355f576SJeff Roberson * bytes The number of bytes requested 15588355f576SJeff Roberson * wait Shall we wait? 15598355f576SJeff Roberson * 15608355f576SJeff Roberson * Returns: 15618355f576SJeff Roberson * A pointer to the alloced memory or possibly 15628355f576SJeff Roberson * NULL if M_NOWAIT is set. 15638355f576SJeff Roberson */ 15648355f576SJeff Roberson static void * 1565ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1566ab3185d1SJeff Roberson int wait) 15678355f576SJeff Roberson { 1568a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1569a4915c21SAttilio Rao u_long npages; 1570b245ac95SAlan Cox vm_offset_t retkva, zkva; 1571a4915c21SAttilio Rao vm_page_t p, p_next; 1572e20a199fSJeff Roberson uma_keg_t keg; 15738355f576SJeff Roberson 1574a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1575bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1576a4915c21SAttilio Rao 1577a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1578a4915c21SAttilio Rao while (npages > 0) { 1579ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 15808d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1581772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1582772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1583a4915c21SAttilio Rao if (p != NULL) { 1584a4915c21SAttilio Rao /* 1585a4915c21SAttilio Rao * Since the page does not belong to an object, its 1586a4915c21SAttilio Rao * listq is unused. 1587a4915c21SAttilio Rao */ 1588a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1589a4915c21SAttilio Rao npages--; 1590a4915c21SAttilio Rao continue; 1591a4915c21SAttilio Rao } 15928355f576SJeff Roberson /* 1593a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1594a4915c21SAttilio Rao * exit. 15958355f576SJeff Roberson */ 1596a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 159788ea538aSMark Johnston vm_page_unwire_noq(p); 1598b245ac95SAlan Cox vm_page_free(p); 1599b245ac95SAlan Cox } 1600a4915c21SAttilio Rao return (NULL); 1601b245ac95SAlan Cox } 16028355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1603a4915c21SAttilio Rao zkva = keg->uk_kva + 1604a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1605a4915c21SAttilio Rao retkva = zkva; 1606a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1607a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1608a4915c21SAttilio Rao zkva += PAGE_SIZE; 1609a4915c21SAttilio Rao } 16108355f576SJeff Roberson 16118355f576SJeff Roberson return ((void *)retkva); 16128355f576SJeff Roberson } 16138355f576SJeff Roberson 16148355f576SJeff Roberson /* 16158355f576SJeff Roberson * Frees a number of pages to the system 16168355f576SJeff Roberson * 16178355f576SJeff Roberson * Arguments: 16188355f576SJeff Roberson * mem A pointer to the memory to be freed 16198355f576SJeff Roberson * size The size of the memory being freed 16208355f576SJeff Roberson * flags The original p->us_flags field 16218355f576SJeff Roberson * 16228355f576SJeff Roberson * Returns: 16238355f576SJeff Roberson * Nothing 16248355f576SJeff Roberson */ 16258355f576SJeff Roberson static void 1626f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 16278355f576SJeff Roberson { 16283370c5bfSJeff Roberson 1629a81c400eSJeff Roberson if ((flags & UMA_SLAB_BOOT) != 0) { 1630a81c400eSJeff Roberson startup_free(mem, size); 1631a81c400eSJeff Roberson return; 1632a81c400eSJeff Roberson } 1633a81c400eSJeff Roberson 163449bfa624SAlan Cox if ((flags & UMA_SLAB_KERNEL) == 0) 1635b5345ef1SJustin Hibbits panic("UMA: page_free used with invalid flags %x", flags); 16368355f576SJeff Roberson 163749bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 16388355f576SJeff Roberson } 16398355f576SJeff Roberson 16408355f576SJeff Roberson /* 1641ab3059a8SMatt Macy * Frees pcpu zone allocations 1642ab3059a8SMatt Macy * 1643ab3059a8SMatt Macy * Arguments: 1644ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1645ab3059a8SMatt Macy * size The size of the memory being freed 1646ab3059a8SMatt Macy * flags The original p->us_flags field 1647ab3059a8SMatt Macy * 1648ab3059a8SMatt Macy * Returns: 1649ab3059a8SMatt Macy * Nothing 1650ab3059a8SMatt Macy */ 1651ab3059a8SMatt Macy static void 1652ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1653ab3059a8SMatt Macy { 1654ab3059a8SMatt Macy vm_offset_t sva, curva; 1655ab3059a8SMatt Macy vm_paddr_t paddr; 1656ab3059a8SMatt Macy vm_page_t m; 1657ab3059a8SMatt Macy 1658ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 1659ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1660ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1661ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1662ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 166388ea538aSMark Johnston vm_page_unwire_noq(m); 1664ab3059a8SMatt Macy vm_page_free(m); 1665ab3059a8SMatt Macy } 1666ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1667ab3059a8SMatt Macy kva_free(sva, size); 1668ab3059a8SMatt Macy } 1669ab3059a8SMatt Macy 1670ab3059a8SMatt Macy 1671ab3059a8SMatt Macy /* 16728355f576SJeff Roberson * Zero fill initializer 16738355f576SJeff Roberson * 16748355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 16758355f576SJeff Roberson */ 1676b23f72e9SBrian Feldman static int 1677b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 16788355f576SJeff Roberson { 16798355f576SJeff Roberson bzero(mem, size); 1680b23f72e9SBrian Feldman return (0); 16818355f576SJeff Roberson } 16828355f576SJeff Roberson 1683815db204SRyan Libby #ifdef INVARIANTS 1684815db204SRyan Libby struct noslabbits * 1685815db204SRyan Libby slab_dbg_bits(uma_slab_t slab, uma_keg_t keg) 1686815db204SRyan Libby { 1687815db204SRyan Libby 1688815db204SRyan Libby return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers))); 1689815db204SRyan Libby } 1690815db204SRyan Libby #endif 1691815db204SRyan Libby 16928355f576SJeff Roberson /* 16939b78b1f4SJeff Roberson * Actual size of embedded struct slab (!OFFPAGE). 16949b78b1f4SJeff Roberson */ 16959b78b1f4SJeff Roberson size_t 16969b78b1f4SJeff Roberson slab_sizeof(int nitems) 16979b78b1f4SJeff Roberson { 16989b78b1f4SJeff Roberson size_t s; 16999b78b1f4SJeff Roberson 1700815db204SRyan Libby s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS; 17019b78b1f4SJeff Roberson return (roundup(s, UMA_ALIGN_PTR + 1)); 17029b78b1f4SJeff Roberson } 17039b78b1f4SJeff Roberson 17049b78b1f4SJeff Roberson /* 17059b78b1f4SJeff Roberson * Size of memory for embedded slabs (!OFFPAGE). 17069b78b1f4SJeff Roberson */ 17079b78b1f4SJeff Roberson size_t 17089b78b1f4SJeff Roberson slab_space(int nitems) 17099b78b1f4SJeff Roberson { 17109b78b1f4SJeff Roberson return (UMA_SLAB_SIZE - slab_sizeof(nitems)); 17119b78b1f4SJeff Roberson } 17129b78b1f4SJeff Roberson 17134a8b575cSRyan Libby #define UMA_FIXPT_SHIFT 31 17144a8b575cSRyan Libby #define UMA_FRAC_FIXPT(n, d) \ 17154a8b575cSRyan Libby ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d))) 17164a8b575cSRyan Libby #define UMA_FIXPT_PCT(f) \ 17174a8b575cSRyan Libby ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT)) 17184a8b575cSRyan Libby #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100) 17194a8b575cSRyan Libby #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE) 17204a8b575cSRyan Libby 17219b78b1f4SJeff Roberson /* 17224a8b575cSRyan Libby * Compute the number of items that will fit in a slab. If hdr is true, the 17234a8b575cSRyan Libby * item count may be limited to provide space in the slab for an inline slab 17244a8b575cSRyan Libby * header. Otherwise, all slab space will be provided for item storage. 17254a8b575cSRyan Libby */ 17264a8b575cSRyan Libby static u_int 17274a8b575cSRyan Libby slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr) 17284a8b575cSRyan Libby { 17294a8b575cSRyan Libby u_int ipers; 17304a8b575cSRyan Libby u_int padpi; 17314a8b575cSRyan Libby 17324a8b575cSRyan Libby /* The padding between items is not needed after the last item. */ 17334a8b575cSRyan Libby padpi = rsize - size; 17344a8b575cSRyan Libby 17354a8b575cSRyan Libby if (hdr) { 17364a8b575cSRyan Libby /* 17374a8b575cSRyan Libby * Start with the maximum item count and remove items until 17384a8b575cSRyan Libby * the slab header first alongside the allocatable memory. 17394a8b575cSRyan Libby */ 17404a8b575cSRyan Libby for (ipers = MIN(SLAB_MAX_SETSIZE, 17414a8b575cSRyan Libby (slabsize + padpi - slab_sizeof(1)) / rsize); 17424a8b575cSRyan Libby ipers > 0 && 17434a8b575cSRyan Libby ipers * rsize - padpi + slab_sizeof(ipers) > slabsize; 17444a8b575cSRyan Libby ipers--) 17454a8b575cSRyan Libby continue; 17464a8b575cSRyan Libby } else { 17474a8b575cSRyan Libby ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE); 17484a8b575cSRyan Libby } 17494a8b575cSRyan Libby 17504a8b575cSRyan Libby return (ipers); 17514a8b575cSRyan Libby } 17524a8b575cSRyan Libby 17534a8b575cSRyan Libby /* 17544a8b575cSRyan Libby * Compute the number of items that will fit in a slab for a startup zone. 17559b78b1f4SJeff Roberson */ 17569b78b1f4SJeff Roberson int 17579b78b1f4SJeff Roberson slab_ipers(size_t size, int align) 17589b78b1f4SJeff Roberson { 17599b78b1f4SJeff Roberson int rsize; 17609b78b1f4SJeff Roberson 17614a8b575cSRyan Libby rsize = roundup(size, align + 1); /* Assume no CACHESPREAD */ 17624a8b575cSRyan Libby return (slab_ipers_hdr(size, rsize, UMA_SLAB_SIZE, true)); 17639b78b1f4SJeff Roberson } 17649b78b1f4SJeff Roberson 17659b78b1f4SJeff Roberson /* 17664a8b575cSRyan Libby * Determine the format of a uma keg. This determines where the slab header 17674a8b575cSRyan Libby * will be placed (inline or offpage) and calculates ipers, rsize, and ppera. 17688355f576SJeff Roberson * 17698355f576SJeff Roberson * Arguments 1770e20a199fSJeff Roberson * keg The zone we should initialize 17718355f576SJeff Roberson * 17728355f576SJeff Roberson * Returns 17738355f576SJeff Roberson * Nothing 17748355f576SJeff Roberson */ 17758355f576SJeff Roberson static void 17764a8b575cSRyan Libby keg_layout(uma_keg_t keg) 17778355f576SJeff Roberson { 17784a8b575cSRyan Libby u_int alignsize; 17794a8b575cSRyan Libby u_int eff; 17804a8b575cSRyan Libby u_int eff_offpage; 17814a8b575cSRyan Libby u_int format; 17824a8b575cSRyan Libby u_int ipers; 17834a8b575cSRyan Libby u_int ipers_offpage; 17844a8b575cSRyan Libby u_int pages; 1785244f4554SBosko Milekic u_int rsize; 1786a55ebb7cSAndriy Gapon u_int slabsize; 17878355f576SJeff Roberson 17884a8b575cSRyan Libby KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 17894a8b575cSRyan Libby (keg->uk_size <= UMA_PCPU_ALLOC_SIZE && 17904a8b575cSRyan Libby (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0), 17914a8b575cSRyan Libby ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b", 17924a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_size, keg->uk_flags, 17934a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 17944a8b575cSRyan Libby KASSERT((keg->uk_flags & 17954a8b575cSRyan Libby (UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY)) == 0 || 17964a8b575cSRyan Libby (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0, 17974a8b575cSRyan Libby ("%s: incompatible flags 0x%b", __func__, keg->uk_flags, 17984a8b575cSRyan Libby PRINT_UMA_ZFLAGS)); 1799e28a647dSGleb Smirnoff 18004a8b575cSRyan Libby alignsize = keg->uk_align + 1; 18014a8b575cSRyan Libby format = 0; 18024a8b575cSRyan Libby ipers = 0; 1803ad97af7eSGleb Smirnoff 1804ef72505eSJeff Roberson /* 1805ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1806ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1807ef72505eSJeff Roberson * allocation bits for we round it up. 1808ef72505eSJeff Roberson */ 18099b8db4d0SRyan Libby rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT); 18104a8b575cSRyan Libby rsize = roundup2(rsize, alignsize); 1811ad97af7eSGleb Smirnoff 18124a8b575cSRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) { 18134a8b575cSRyan Libby slabsize = UMA_PCPU_ALLOC_SIZE; 18144a8b575cSRyan Libby pages = mp_maxid + 1; 18154a8b575cSRyan Libby } else if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) { 18169b78b1f4SJeff Roberson /* 18174a8b575cSRyan Libby * We want one item to start on every align boundary in a page. 18184a8b575cSRyan Libby * To do this we will span pages. We will also extend the item 18194a8b575cSRyan Libby * by the size of align if it is an even multiple of align. 18204a8b575cSRyan Libby * Otherwise, it would fall on the same boundary every time. 18219b78b1f4SJeff Roberson */ 18224a8b575cSRyan Libby if ((rsize & alignsize) == 0) 18234a8b575cSRyan Libby rsize += alignsize; 18244a8b575cSRyan Libby slabsize = rsize * (PAGE_SIZE / alignsize); 18254a8b575cSRyan Libby slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE); 18264a8b575cSRyan Libby slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE); 18274a8b575cSRyan Libby pages = howmany(slabsize, PAGE_SIZE); 18284a8b575cSRyan Libby slabsize = ptoa(pages); 18294a8b575cSRyan Libby } else { 18304a8b575cSRyan Libby /* 18314a8b575cSRyan Libby * Choose a slab size of as many pages as it takes to represent 18324a8b575cSRyan Libby * a single item. We will then try to fit as many additional 18334a8b575cSRyan Libby * items into the slab as possible. At some point, we may want 18344a8b575cSRyan Libby * to increase the slab size for awkward item sizes in order to 18354a8b575cSRyan Libby * increase efficiency. 18364a8b575cSRyan Libby */ 18374a8b575cSRyan Libby pages = howmany(keg->uk_size, PAGE_SIZE); 18384a8b575cSRyan Libby slabsize = ptoa(pages); 18391ca6ed45SGleb Smirnoff } 1840ad97af7eSGleb Smirnoff 18414a8b575cSRyan Libby /* Evaluate an inline slab layout. */ 18424a8b575cSRyan Libby if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0) 18434a8b575cSRyan Libby ipers = slab_ipers_hdr(keg->uk_size, rsize, slabsize, true); 18444a8b575cSRyan Libby 18454a8b575cSRyan Libby /* TODO: vm_page-embedded slab. */ 1846244f4554SBosko Milekic 184720e8e865SBosko Milekic /* 1848244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 184920e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 18506fd34d6fSJeff Roberson * may end up going to the VM for slabs which we do not 18516fd34d6fSJeff Roberson * want to do if we're UMA_ZFLAG_CACHEONLY as a result 18526fd34d6fSJeff Roberson * of UMA_ZONE_VM, which clearly forbids it. 185320e8e865SBosko Milekic */ 18544a8b575cSRyan Libby if ((keg->uk_flags & 18554a8b575cSRyan Libby (UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY)) != 0) { 18564a8b575cSRyan Libby if (ipers == 0) { 18574a8b575cSRyan Libby /* We need an extra page for the slab header. */ 18584a8b575cSRyan Libby pages++; 18594a8b575cSRyan Libby slabsize = ptoa(pages); 18604a8b575cSRyan Libby ipers = slab_ipers_hdr(keg->uk_size, rsize, slabsize, 18614a8b575cSRyan Libby true); 18624a8b575cSRyan Libby } 18634a8b575cSRyan Libby goto out; 186454c5ae80SRyan Libby } 1865244f4554SBosko Milekic 1866ef72505eSJeff Roberson /* 18674a8b575cSRyan Libby * See if using an OFFPAGE slab will improve our efficiency. 18684a8b575cSRyan Libby * Only do this if we are below our efficiency threshold. 1869ef72505eSJeff Roberson * 1870ef72505eSJeff Roberson * XXX We could try growing slabsize to limit max waste as well. 1871ef72505eSJeff Roberson * Historically this was not done because the VM could not 1872ef72505eSJeff Roberson * efficiently handle contiguous allocations. 1873ef72505eSJeff Roberson */ 18744a8b575cSRyan Libby eff = UMA_FRAC_FIXPT(ipers * rsize, slabsize); 18754a8b575cSRyan Libby ipers_offpage = slab_ipers_hdr(keg->uk_size, rsize, slabsize, false); 18764a8b575cSRyan Libby eff_offpage = UMA_FRAC_FIXPT(ipers_offpage * rsize, 18779b8db4d0SRyan Libby slabsize + slabzone(ipers_offpage)->uz_keg->uk_rsize); 18784a8b575cSRyan Libby if (ipers == 0 || (eff < UMA_MIN_EFF && eff < eff_offpage)) { 18794a8b575cSRyan Libby CTR5(KTR_UMA, "UMA decided we need offpage slab headers for " 18804a8b575cSRyan Libby "keg: %s(%p), minimum efficiency allowed = %u%%, " 1881e63a1c2fSRyan Libby "old efficiency = %u%%, offpage efficiency = %u%%", 18824a8b575cSRyan Libby keg->uk_name, keg, UMA_FIXPT_PCT(UMA_MIN_EFF), 18834a8b575cSRyan Libby UMA_FIXPT_PCT(eff), UMA_FIXPT_PCT(eff_offpage)); 18844a8b575cSRyan Libby format = UMA_ZFLAG_OFFPAGE; 18854a8b575cSRyan Libby ipers = ipers_offpage; 18868355f576SJeff Roberson } 1887ad97af7eSGleb Smirnoff 18884a8b575cSRyan Libby out: 18894a8b575cSRyan Libby /* 18904a8b575cSRyan Libby * How do we find the slab header if it is offpage or if not all item 18914a8b575cSRyan Libby * start addresses are in the same page? We could solve the latter 18924a8b575cSRyan Libby * case with vaddr alignment, but we don't. 18934a8b575cSRyan Libby */ 18944a8b575cSRyan Libby if ((format & UMA_ZFLAG_OFFPAGE) != 0 || 18954a8b575cSRyan Libby (ipers - 1) * rsize >= PAGE_SIZE) { 189654c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0) 18974a8b575cSRyan Libby format |= UMA_ZFLAG_HASH; 189854c5ae80SRyan Libby else 18994a8b575cSRyan Libby format |= UMA_ZFLAG_VTOSLAB; 190054c5ae80SRyan Libby } 19014a8b575cSRyan Libby keg->uk_ipers = ipers; 1902e20a199fSJeff Roberson keg->uk_rsize = rsize; 19034a8b575cSRyan Libby keg->uk_flags |= format; 1904e20a199fSJeff Roberson keg->uk_ppera = pages; 1905e63a1c2fSRyan Libby CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u", 19064a8b575cSRyan Libby __func__, keg->uk_name, keg->uk_flags, rsize, ipers, pages); 19074a8b575cSRyan Libby KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE, 19084a8b575cSRyan Libby ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__, 19094a8b575cSRyan Libby keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize, ipers, 19104a8b575cSRyan Libby pages)); 1911e20a199fSJeff Roberson } 1912e20a199fSJeff Roberson 19138355f576SJeff Roberson /* 1914099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 1915099a0e58SBosko Milekic * the keg onto the global keg list. 19168355f576SJeff Roberson * 19178355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 1918099a0e58SBosko Milekic * udata Actually uma_kctor_args 1919099a0e58SBosko Milekic */ 1920b23f72e9SBrian Feldman static int 1921b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 1922099a0e58SBosko Milekic { 1923099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 1924099a0e58SBosko Milekic uma_keg_t keg = mem; 1925099a0e58SBosko Milekic uma_zone_t zone; 19268b987a77SJeff Roberson int i; 1927099a0e58SBosko Milekic 1928099a0e58SBosko Milekic bzero(keg, size); 1929099a0e58SBosko Milekic keg->uk_size = arg->size; 1930099a0e58SBosko Milekic keg->uk_init = arg->uminit; 1931099a0e58SBosko Milekic keg->uk_fini = arg->fini; 1932099a0e58SBosko Milekic keg->uk_align = arg->align; 19336fd34d6fSJeff Roberson keg->uk_reserve = 0; 1934099a0e58SBosko Milekic keg->uk_flags = arg->flags; 1935099a0e58SBosko Milekic 1936099a0e58SBosko Milekic /* 1937194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 1938dfe13344SJeff Roberson * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which 1939dfe13344SJeff Roberson * case the iterator is never run. 1940194a979eSMark Johnston */ 1941194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 1942194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 1943194a979eSMark Johnston 1944194a979eSMark Johnston /* 1945099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 1946099a0e58SBosko Milekic */ 1947099a0e58SBosko Milekic zone = arg->zone; 1948e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 1949099a0e58SBosko Milekic 1950099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_VM) 1951099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 1952099a0e58SBosko Milekic 1953099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 1954099a0e58SBosko Milekic keg->uk_init = zero_init; 1955099a0e58SBosko Milekic 1956cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 195754c5ae80SRyan Libby keg->uk_flags |= UMA_ZFLAG_VTOSLAB; 1958e20a199fSJeff Roberson 195954c5ae80SRyan Libby #ifndef SMP 1960ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 1961ad97af7eSGleb Smirnoff #endif 1962ad97af7eSGleb Smirnoff 19634a8b575cSRyan Libby keg_layout(keg); 1964099a0e58SBosko Milekic 19658b987a77SJeff Roberson /* 1966dfe13344SJeff Roberson * Use a first-touch NUMA policy for all kegs that pmap_extract() 1967dfe13344SJeff Roberson * will work on with the exception of critical VM structures 1968dfe13344SJeff Roberson * necessary for paging. 1969dfe13344SJeff Roberson * 1970dfe13344SJeff Roberson * Zones may override the default by specifying either. 19718b987a77SJeff Roberson */ 1972dfe13344SJeff Roberson #ifdef NUMA 1973dfe13344SJeff Roberson if ((keg->uk_flags & 197454c5ae80SRyan Libby (UMA_ZFLAG_HASH | UMA_ZONE_VM | UMA_ZONE_ROUNDROBIN)) == 0) 1975dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_FIRSTTOUCH; 1976dfe13344SJeff Roberson else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0) 1977dfe13344SJeff Roberson keg->uk_flags |= UMA_ZONE_ROUNDROBIN; 19788b987a77SJeff Roberson #endif 19798b987a77SJeff Roberson 1980099a0e58SBosko Milekic /* 1981099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 1982099a0e58SBosko Milekic * startup cache until the vm is ready. 1983099a0e58SBosko Milekic */ 198477e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 1985a81c400eSJeff Roberson if (keg->uk_ppera == 1) 198677e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 1987a81c400eSJeff Roberson else 19888cd02d00SAlan Cox #endif 1989a81c400eSJeff Roberson if (booted < BOOT_KVA) 1990a81c400eSJeff Roberson keg->uk_allocf = startup_alloc; 1991ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 1992ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 199377e19437SGleb Smirnoff else 199477e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 199577e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 199677e19437SGleb Smirnoff if (keg->uk_ppera == 1) 199777e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 199877e19437SGleb Smirnoff else 199977e19437SGleb Smirnoff #endif 2000ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 2001ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 2002ab3059a8SMatt Macy else 200377e19437SGleb Smirnoff keg->uk_freef = page_free; 2004099a0e58SBosko Milekic 2005099a0e58SBosko Milekic /* 20068b987a77SJeff Roberson * Initialize keg's locks. 2007099a0e58SBosko Milekic */ 20088b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) 20098b987a77SJeff Roberson KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS)); 2010099a0e58SBosko Milekic 2011099a0e58SBosko Milekic /* 2012099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 20139b78b1f4SJeff Roberson * figure out where in each page it goes. See slab_sizeof 20149b78b1f4SJeff Roberson * definition. 2015099a0e58SBosko Milekic */ 201654c5ae80SRyan Libby if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) { 20179b78b1f4SJeff Roberson size_t shsize; 20189b78b1f4SJeff Roberson 20199b78b1f4SJeff Roberson shsize = slab_sizeof(keg->uk_ipers); 20209b78b1f4SJeff Roberson keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize; 2021244f4554SBosko Milekic /* 2022244f4554SBosko Milekic * The only way the following is possible is if with our 2023244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 2024244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 2025244f4554SBosko Milekic * mathematically possible for all cases, so we make 2026244f4554SBosko Milekic * sure here anyway. 2027244f4554SBosko Milekic */ 20289b78b1f4SJeff Roberson KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera, 20293d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 20303d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 2031099a0e58SBosko Milekic } 2032099a0e58SBosko Milekic 203354c5ae80SRyan Libby if (keg->uk_flags & UMA_ZFLAG_HASH) 20343b2f2cb8SAlexander Motin hash_alloc(&keg->uk_hash, 0); 2035099a0e58SBosko Milekic 2036e63a1c2fSRyan Libby CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone); 2037099a0e58SBosko Milekic 2038099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 2039099a0e58SBosko Milekic 2040111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2041099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 2042111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2043b23f72e9SBrian Feldman return (0); 2044099a0e58SBosko Milekic } 2045099a0e58SBosko Milekic 20462efcc8cbSGleb Smirnoff static void 2047a81c400eSJeff Roberson zone_kva_available(uma_zone_t zone, void *unused) 2048a81c400eSJeff Roberson { 2049a81c400eSJeff Roberson uma_keg_t keg; 2050a81c400eSJeff Roberson 2051a81c400eSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 2052a81c400eSJeff Roberson return; 2053a81c400eSJeff Roberson KEG_GET(zone, keg); 2054a81c400eSJeff Roberson if (keg->uk_allocf == startup_alloc) 2055a81c400eSJeff Roberson keg->uk_allocf = page_alloc; 2056a81c400eSJeff Roberson } 2057a81c400eSJeff Roberson 2058a81c400eSJeff Roberson static void 205920a4e154SJeff Roberson zone_alloc_counters(uma_zone_t zone, void *unused) 20602efcc8cbSGleb Smirnoff { 20612efcc8cbSGleb Smirnoff 20622efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 20632efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 20642efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 20652efcc8cbSGleb Smirnoff } 20662efcc8cbSGleb Smirnoff 206720a4e154SJeff Roberson static void 206820a4e154SJeff Roberson zone_alloc_sysctl(uma_zone_t zone, void *unused) 206920a4e154SJeff Roberson { 207020a4e154SJeff Roberson uma_zone_domain_t zdom; 20718b987a77SJeff Roberson uma_domain_t dom; 207220a4e154SJeff Roberson uma_keg_t keg; 207320a4e154SJeff Roberson struct sysctl_oid *oid, *domainoid; 20743b490537SJeff Roberson int domains, i, cnt; 207520a4e154SJeff Roberson static const char *nokeg = "cache zone"; 207620a4e154SJeff Roberson char *c; 207720a4e154SJeff Roberson 207820a4e154SJeff Roberson /* 207920a4e154SJeff Roberson * Make a sysctl safe copy of the zone name by removing 208020a4e154SJeff Roberson * any special characters and handling dups by appending 208120a4e154SJeff Roberson * an index. 208220a4e154SJeff Roberson */ 208320a4e154SJeff Roberson if (zone->uz_namecnt != 0) { 20843b490537SJeff Roberson /* Count the number of decimal digits and '_' separator. */ 20853b490537SJeff Roberson for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++) 20863b490537SJeff Roberson cnt /= 10; 20873b490537SJeff Roberson zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1, 20883b490537SJeff Roberson M_UMA, M_WAITOK); 208920a4e154SJeff Roberson sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name, 209020a4e154SJeff Roberson zone->uz_namecnt); 209120a4e154SJeff Roberson } else 209220a4e154SJeff Roberson zone->uz_ctlname = strdup(zone->uz_name, M_UMA); 209320a4e154SJeff Roberson for (c = zone->uz_ctlname; *c != '\0'; c++) 209420a4e154SJeff Roberson if (strchr("./\\ -", *c) != NULL) 209520a4e154SJeff Roberson *c = '_'; 209620a4e154SJeff Roberson 209720a4e154SJeff Roberson /* 209820a4e154SJeff Roberson * Basic parameters at the root. 209920a4e154SJeff Roberson */ 210020a4e154SJeff Roberson zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma), 210120a4e154SJeff Roberson OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, ""); 210220a4e154SJeff Roberson oid = zone->uz_oid; 210320a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 210420a4e154SJeff Roberson "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size"); 21056d204a6aSRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21066d204a6aSRyan Libby "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, 21076d204a6aSRyan Libby zone, 0, sysctl_handle_uma_zone_flags, "A", 210820a4e154SJeff Roberson "Allocator configuration flags"); 210920a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 211020a4e154SJeff Roberson "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0, 211120a4e154SJeff Roberson "Desired per-cpu cache size"); 211220a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 211320a4e154SJeff Roberson "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0, 211420a4e154SJeff Roberson "Maximum allowed per-cpu cache size"); 211520a4e154SJeff Roberson 211620a4e154SJeff Roberson /* 211720a4e154SJeff Roberson * keg if present. 211820a4e154SJeff Roberson */ 211954c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0) 21208b987a77SJeff Roberson domains = vm_ndomains; 21218b987a77SJeff Roberson else 21228b987a77SJeff Roberson domains = 1; 212320a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 212420a4e154SJeff Roberson "keg", CTLFLAG_RD, NULL, ""); 212520a4e154SJeff Roberson keg = zone->uz_keg; 21263b490537SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) { 212720a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 212820a4e154SJeff Roberson "name", CTLFLAG_RD, keg->uk_name, "Keg name"); 212920a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213020a4e154SJeff Roberson "rsize", CTLFLAG_RD, &keg->uk_rsize, 0, 213120a4e154SJeff Roberson "Real object size with alignment"); 213220a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213320a4e154SJeff Roberson "ppera", CTLFLAG_RD, &keg->uk_ppera, 0, 213420a4e154SJeff Roberson "pages per-slab allocation"); 213520a4e154SJeff Roberson SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213620a4e154SJeff Roberson "ipers", CTLFLAG_RD, &keg->uk_ipers, 0, 213720a4e154SJeff Roberson "items available per-slab"); 213820a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 213920a4e154SJeff Roberson "align", CTLFLAG_RD, &keg->uk_align, 0, 214020a4e154SJeff Roberson "item alignment mask"); 2141f7af5015SRyan Libby SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 2142f7af5015SRyan Libby "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 2143f7af5015SRyan Libby keg, 0, sysctl_handle_uma_slab_efficiency, "I", 2144f7af5015SRyan Libby "Slab utilization (100 - internal fragmentation %)"); 21458b987a77SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid), 21468b987a77SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 21478b987a77SJeff Roberson for (i = 0; i < domains; i++) { 21488b987a77SJeff Roberson dom = &keg->uk_domain[i]; 21498b987a77SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 21508b987a77SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, 21518b987a77SJeff Roberson NULL, ""); 21528b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21538b987a77SJeff Roberson "pages", CTLFLAG_RD, &dom->ud_pages, 0, 21548b987a77SJeff Roberson "Total pages currently allocated from VM"); 21558b987a77SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21568b987a77SJeff Roberson "free", CTLFLAG_RD, &dom->ud_free, 0, 21578b987a77SJeff Roberson "items free in the slab layer"); 21588b987a77SJeff Roberson } 215920a4e154SJeff Roberson } else 216020a4e154SJeff Roberson SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 216120a4e154SJeff Roberson "name", CTLFLAG_RD, nokeg, "Keg name"); 216220a4e154SJeff Roberson 216320a4e154SJeff Roberson /* 216420a4e154SJeff Roberson * Information about zone limits. 216520a4e154SJeff Roberson */ 216620a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 216720a4e154SJeff Roberson "limit", CTLFLAG_RD, NULL, ""); 21684bd61e19SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21694bd61e19SJeff Roberson "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 21704bd61e19SJeff Roberson zone, 0, sysctl_handle_uma_zone_items, "QU", 21714bd61e19SJeff Roberson "current number of allocated items if limit is set"); 217220a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 217320a4e154SJeff Roberson "max_items", CTLFLAG_RD, &zone->uz_max_items, 0, 217420a4e154SJeff Roberson "Maximum number of cached items"); 217520a4e154SJeff Roberson SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 217620a4e154SJeff Roberson "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0, 217720a4e154SJeff Roberson "Number of threads sleeping at limit"); 217820a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 217920a4e154SJeff Roberson "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0, 218020a4e154SJeff Roberson "Total zone limit sleeps"); 21814bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21824bd61e19SJeff Roberson "bucket_max", CTLFLAG_RD, &zone->uz_bkt_max, 0, 21834bd61e19SJeff Roberson "Maximum number of items in the bucket cache"); 21844bd61e19SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 21854bd61e19SJeff Roberson "bucket_cnt", CTLFLAG_RD, &zone->uz_bkt_count, 0, 21864bd61e19SJeff Roberson "Number of items in the bucket cache"); 218720a4e154SJeff Roberson 218820a4e154SJeff Roberson /* 21898b987a77SJeff Roberson * Per-domain zone information. 219020a4e154SJeff Roberson */ 219120a4e154SJeff Roberson domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), 219220a4e154SJeff Roberson OID_AUTO, "domain", CTLFLAG_RD, NULL, ""); 2193dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0) 21948b987a77SJeff Roberson domains = 1; 219520a4e154SJeff Roberson for (i = 0; i < domains; i++) { 219620a4e154SJeff Roberson zdom = &zone->uz_domain[i]; 219720a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid), 219820a4e154SJeff Roberson OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, ""); 219920a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 220020a4e154SJeff Roberson "nitems", CTLFLAG_RD, &zdom->uzd_nitems, 220120a4e154SJeff Roberson "number of items in this domain"); 220220a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 220320a4e154SJeff Roberson "imax", CTLFLAG_RD, &zdom->uzd_imax, 220420a4e154SJeff Roberson "maximum item count in this period"); 220520a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 220620a4e154SJeff Roberson "imin", CTLFLAG_RD, &zdom->uzd_imin, 220720a4e154SJeff Roberson "minimum item count in this period"); 220820a4e154SJeff Roberson SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 220920a4e154SJeff Roberson "wss", CTLFLAG_RD, &zdom->uzd_wss, 221020a4e154SJeff Roberson "Working set size"); 221120a4e154SJeff Roberson } 221220a4e154SJeff Roberson 221320a4e154SJeff Roberson /* 221420a4e154SJeff Roberson * General statistics. 221520a4e154SJeff Roberson */ 221620a4e154SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO, 221720a4e154SJeff Roberson "stats", CTLFLAG_RD, NULL, ""); 221820a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 221920a4e154SJeff Roberson "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE, 222020a4e154SJeff Roberson zone, 1, sysctl_handle_uma_zone_cur, "I", 222120a4e154SJeff Roberson "Current number of allocated items"); 222220a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 222320a4e154SJeff Roberson "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 222420a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_allocs, "QU", 222520a4e154SJeff Roberson "Total allocation calls"); 222620a4e154SJeff Roberson SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 222720a4e154SJeff Roberson "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE, 222820a4e154SJeff Roberson zone, 0, sysctl_handle_uma_zone_frees, "QU", 222920a4e154SJeff Roberson "Total free calls"); 223020a4e154SJeff Roberson SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 223120a4e154SJeff Roberson "fails", CTLFLAG_RD, &zone->uz_fails, 223220a4e154SJeff Roberson "Number of allocation failures"); 223320a4e154SJeff Roberson SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, 223420a4e154SJeff Roberson "xdomain", CTLFLAG_RD, &zone->uz_xdomain, 0, 223520a4e154SJeff Roberson "Free calls from the wrong domain"); 223620a4e154SJeff Roberson } 223720a4e154SJeff Roberson 223820a4e154SJeff Roberson struct uma_zone_count { 223920a4e154SJeff Roberson const char *name; 224020a4e154SJeff Roberson int count; 224120a4e154SJeff Roberson }; 224220a4e154SJeff Roberson 224320a4e154SJeff Roberson static void 224420a4e154SJeff Roberson zone_count(uma_zone_t zone, void *arg) 224520a4e154SJeff Roberson { 224620a4e154SJeff Roberson struct uma_zone_count *cnt; 224720a4e154SJeff Roberson 224820a4e154SJeff Roberson cnt = arg; 22493b490537SJeff Roberson /* 22503b490537SJeff Roberson * Some zones are rapidly created with identical names and 22513b490537SJeff Roberson * destroyed out of order. This can lead to gaps in the count. 22523b490537SJeff Roberson * Use one greater than the maximum observed for this name. 22533b490537SJeff Roberson */ 225420a4e154SJeff Roberson if (strcmp(zone->uz_name, cnt->name) == 0) 22553b490537SJeff Roberson cnt->count = MAX(cnt->count, 22563b490537SJeff Roberson zone->uz_namecnt + 1); 225720a4e154SJeff Roberson } 225820a4e154SJeff Roberson 2259cc7ce83aSJeff Roberson static void 2260cc7ce83aSJeff Roberson zone_update_caches(uma_zone_t zone) 2261cc7ce83aSJeff Roberson { 2262cc7ce83aSJeff Roberson int i; 2263cc7ce83aSJeff Roberson 2264cc7ce83aSJeff Roberson for (i = 0; i <= mp_maxid; i++) { 2265cc7ce83aSJeff Roberson cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size); 2266cc7ce83aSJeff Roberson cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags); 2267cc7ce83aSJeff Roberson } 2268cc7ce83aSJeff Roberson } 2269cc7ce83aSJeff Roberson 2270099a0e58SBosko Milekic /* 2271099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 2272099a0e58SBosko Milekic * 2273099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 2274099a0e58SBosko Milekic * udata Actually uma_zctor_args 22758355f576SJeff Roberson */ 2276b23f72e9SBrian Feldman static int 2277b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 22788355f576SJeff Roberson { 227920a4e154SJeff Roberson struct uma_zone_count cnt; 22808355f576SJeff Roberson struct uma_zctor_args *arg = udata; 22818355f576SJeff Roberson uma_zone_t zone = mem; 2282099a0e58SBosko Milekic uma_zone_t z; 2283099a0e58SBosko Milekic uma_keg_t keg; 228408cfa56eSMark Johnston int i; 22858355f576SJeff Roberson 22868355f576SJeff Roberson bzero(zone, size); 22878355f576SJeff Roberson zone->uz_name = arg->name; 22888355f576SJeff Roberson zone->uz_ctor = arg->ctor; 22898355f576SJeff Roberson zone->uz_dtor = arg->dtor; 2290099a0e58SBosko Milekic zone->uz_init = NULL; 2291099a0e58SBosko Milekic zone->uz_fini = NULL; 2292bf965959SSean Bruno zone->uz_sleeps = 0; 2293c1685086SJeff Roberson zone->uz_xdomain = 0; 229420a4e154SJeff Roberson zone->uz_bucket_size = 0; 229520a4e154SJeff Roberson zone->uz_bucket_size_min = 0; 229620a4e154SJeff Roberson zone->uz_bucket_size_max = BUCKET_MAX; 2297e20a199fSJeff Roberson zone->uz_flags = 0; 22982f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 2299ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 2300ab3185d1SJeff Roberson zone->uz_domain = (struct uma_zone_domain *)&zone->uz_cpu[mp_ncpus]; 2301bb15d1c7SGleb Smirnoff zone->uz_bkt_max = ULONG_MAX; 23022f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 2303af526374SJeff Roberson 230420a4e154SJeff Roberson /* Count the number of duplicate names. */ 230520a4e154SJeff Roberson cnt.name = arg->name; 230620a4e154SJeff Roberson cnt.count = 0; 230720a4e154SJeff Roberson zone_foreach(zone_count, &cnt); 230820a4e154SJeff Roberson zone->uz_namecnt = cnt.count; 2309727c6918SJeff Roberson ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 231091d947bfSJeff Roberson ZONE_CROSS_LOCK_INIT(zone); 23112efcc8cbSGleb Smirnoff 231208cfa56eSMark Johnston for (i = 0; i < vm_ndomains; i++) 231308cfa56eSMark Johnston TAILQ_INIT(&zone->uz_domain[i].uzd_buckets); 231408cfa56eSMark Johnston 2315ca293436SRyan Libby #ifdef INVARIANTS 2316ca293436SRyan Libby if (arg->uminit == trash_init && arg->fini == trash_fini) 2317cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR; 2318ca293436SRyan Libby #endif 2319ca293436SRyan Libby 23200095a784SJeff Roberson /* 23210095a784SJeff Roberson * This is a pure cache zone, no kegs. 23220095a784SJeff Roberson */ 23230095a784SJeff Roberson if (arg->import) { 2324727c6918SJeff Roberson KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0, 2325727c6918SJeff Roberson ("zone_ctor: Import specified for non-cache zone.")); 23266fd34d6fSJeff Roberson if (arg->flags & UMA_ZONE_VM) 23276fd34d6fSJeff Roberson arg->flags |= UMA_ZFLAG_CACHEONLY; 23286fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 2329af526374SJeff Roberson zone->uz_size = arg->size; 23300095a784SJeff Roberson zone->uz_import = arg->import; 23310095a784SJeff Roberson zone->uz_release = arg->release; 23320095a784SJeff Roberson zone->uz_arg = arg->arg; 2333111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 233403175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 2335111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2336af526374SJeff Roberson goto out; 23370095a784SJeff Roberson } 23380095a784SJeff Roberson 23390095a784SJeff Roberson /* 23400095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 23410095a784SJeff Roberson */ 2342b75c4efcSAndrew Turner zone->uz_import = zone_import; 2343b75c4efcSAndrew Turner zone->uz_release = zone_release; 23440095a784SJeff Roberson zone->uz_arg = zone; 2345bb15d1c7SGleb Smirnoff keg = arg->keg; 23460095a784SJeff Roberson 2347099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 234820a4e154SJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 234920a4e154SJeff Roberson ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 2350099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 23518355f576SJeff Roberson zone->uz_init = arg->uminit; 2352e221e841SJeff Roberson zone->uz_fini = arg->fini; 2353e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 2354111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2355099a0e58SBosko Milekic ZONE_LOCK(zone); 2356099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 2357099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 2358099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 2359099a0e58SBosko Milekic break; 2360099a0e58SBosko Milekic } 2361099a0e58SBosko Milekic } 2362099a0e58SBosko Milekic ZONE_UNLOCK(zone); 2363111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2364e20a199fSJeff Roberson } else if (keg == NULL) { 2365e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 2366e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 2367b23f72e9SBrian Feldman return (ENOMEM); 2368099a0e58SBosko Milekic } else { 2369099a0e58SBosko Milekic struct uma_kctor_args karg; 2370b23f72e9SBrian Feldman int error; 2371099a0e58SBosko Milekic 2372099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 2373099a0e58SBosko Milekic karg.size = arg->size; 2374099a0e58SBosko Milekic karg.uminit = arg->uminit; 2375099a0e58SBosko Milekic karg.fini = arg->fini; 2376099a0e58SBosko Milekic karg.align = arg->align; 2377099a0e58SBosko Milekic karg.flags = arg->flags; 2378099a0e58SBosko Milekic karg.zone = zone; 2379b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 2380b23f72e9SBrian Feldman flags); 2381b23f72e9SBrian Feldman if (error) 2382b23f72e9SBrian Feldman return (error); 2383099a0e58SBosko Milekic } 23840095a784SJeff Roberson 238520a4e154SJeff Roberson /* Inherit properties from the keg. */ 2386bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 2387e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 2388e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 2389e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 23908355f576SJeff Roberson 239120a4e154SJeff Roberson out: 2392860bb7a0SMark Johnston if (__predict_true(booted >= BOOT_RUNNING)) { 239320a4e154SJeff Roberson zone_alloc_counters(zone, NULL); 239420a4e154SJeff Roberson zone_alloc_sysctl(zone, NULL); 239520a4e154SJeff Roberson } else { 239620a4e154SJeff Roberson zone->uz_allocs = EARLY_COUNTER; 239720a4e154SJeff Roberson zone->uz_frees = EARLY_COUNTER; 239820a4e154SJeff Roberson zone->uz_fails = EARLY_COUNTER; 2399099a0e58SBosko Milekic } 24008355f576SJeff Roberson 24017e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 24027e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 24037e28037aSMark Johnston ("Invalid zone flag combination")); 240420a4e154SJeff Roberson if (arg->flags & UMA_ZFLAG_INTERNAL) 240520a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 240620a4e154SJeff Roberson if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 240720a4e154SJeff Roberson zone->uz_bucket_size = BUCKET_MAX; 240820a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) 240920a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN; 241020a4e154SJeff Roberson else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 241120a4e154SJeff Roberson zone->uz_bucket_size = 0; 24127e28037aSMark Johnston else 241320a4e154SJeff Roberson zone->uz_bucket_size = bucket_select(zone->uz_size); 241420a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size; 2415cc7ce83aSJeff Roberson if (zone->uz_dtor != NULL || zone->uz_ctor != NULL) 2416cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_CTORDTOR; 2417cc7ce83aSJeff Roberson zone_update_caches(zone); 2418fc03d22bSJeff Roberson 2419b23f72e9SBrian Feldman return (0); 24208355f576SJeff Roberson } 24218355f576SJeff Roberson 24228355f576SJeff Roberson /* 2423099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 2424099a0e58SBosko Milekic * table and removes the keg from the global list. 24259c2cd7e5SJeff Roberson * 24269c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 24279c2cd7e5SJeff Roberson * udata unused 24289c2cd7e5SJeff Roberson */ 2429099a0e58SBosko Milekic static void 2430099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 2431099a0e58SBosko Milekic { 2432099a0e58SBosko Milekic uma_keg_t keg; 24338b987a77SJeff Roberson uint32_t free, pages; 24348b987a77SJeff Roberson int i; 24359c2cd7e5SJeff Roberson 2436099a0e58SBosko Milekic keg = (uma_keg_t)arg; 24378b987a77SJeff Roberson free = pages = 0; 24388b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 24398b987a77SJeff Roberson free += keg->uk_domain[i].ud_free; 24408b987a77SJeff Roberson pages += keg->uk_domain[i].ud_pages; 24418b987a77SJeff Roberson KEG_LOCK_FINI(keg, i); 2442099a0e58SBosko Milekic } 24438b987a77SJeff Roberson if (free != 0) 24448b987a77SJeff Roberson printf("Freed UMA keg (%s) was not empty (%u items). " 24458b987a77SJeff Roberson " Lost %u pages of memory.\n", 24468b987a77SJeff Roberson keg->uk_name ? keg->uk_name : "", 24478b987a77SJeff Roberson free, pages); 2448099a0e58SBosko Milekic 2449099a0e58SBosko Milekic hash_free(&keg->uk_hash); 2450099a0e58SBosko Milekic } 2451099a0e58SBosko Milekic 2452099a0e58SBosko Milekic /* 2453099a0e58SBosko Milekic * Zone header dtor. 2454099a0e58SBosko Milekic * 2455099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 2456099a0e58SBosko Milekic * udata unused 2457099a0e58SBosko Milekic */ 24589c2cd7e5SJeff Roberson static void 24599c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 24609c2cd7e5SJeff Roberson { 24619c2cd7e5SJeff Roberson uma_zone_t zone; 2462099a0e58SBosko Milekic uma_keg_t keg; 24639c2cd7e5SJeff Roberson 24649c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 24659643769aSJeff Roberson 246620a4e154SJeff Roberson sysctl_remove_oid(zone->uz_oid, 1, 1); 246720a4e154SJeff Roberson 2468e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 24699643769aSJeff Roberson cache_drain(zone); 2470099a0e58SBosko Milekic 2471111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2472099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 2473111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 2474099a0e58SBosko Milekic /* 2475099a0e58SBosko Milekic * XXX there are some races here where 2476099a0e58SBosko Milekic * the zone can be drained but zone lock 2477099a0e58SBosko Milekic * released and then refilled before we 2478099a0e58SBosko Milekic * remove it... we dont care for now 2479099a0e58SBosko Milekic */ 248008cfa56eSMark Johnston zone_reclaim(zone, M_WAITOK, true); 2481e20a199fSJeff Roberson /* 2482323ad386STycho Nightingale * We only destroy kegs from non secondary/non cache zones. 2483e20a199fSJeff Roberson */ 2484323ad386STycho Nightingale if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) { 2485323ad386STycho Nightingale keg = zone->uz_keg; 2486111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 2487099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 2488111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 24890095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 24909c2cd7e5SJeff Roberson } 24912efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 24922efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 24932efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 249420a4e154SJeff Roberson free(zone->uz_ctlname, M_UMA); 2495af526374SJeff Roberson ZONE_LOCK_FINI(zone); 249691d947bfSJeff Roberson ZONE_CROSS_LOCK_FINI(zone); 2497099a0e58SBosko Milekic } 2498099a0e58SBosko Milekic 2499a81c400eSJeff Roberson static void 2500a81c400eSJeff Roberson zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg) 2501a81c400eSJeff Roberson { 2502a81c400eSJeff Roberson uma_keg_t keg; 2503a81c400eSJeff Roberson uma_zone_t zone; 2504a81c400eSJeff Roberson 2505a81c400eSJeff Roberson LIST_FOREACH(keg, &uma_kegs, uk_link) { 2506a81c400eSJeff Roberson LIST_FOREACH(zone, &keg->uk_zones, uz_link) 2507a81c400eSJeff Roberson zfunc(zone, arg); 2508a81c400eSJeff Roberson } 2509a81c400eSJeff Roberson LIST_FOREACH(zone, &uma_cachezones, uz_link) 2510a81c400eSJeff Roberson zfunc(zone, arg); 2511a81c400eSJeff Roberson } 2512a81c400eSJeff Roberson 25139c2cd7e5SJeff Roberson /* 25148355f576SJeff Roberson * Traverses every zone in the system and calls a callback 25158355f576SJeff Roberson * 25168355f576SJeff Roberson * Arguments: 25178355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 25188355f576SJeff Roberson * as an argument. 25198355f576SJeff Roberson * 25208355f576SJeff Roberson * Returns: 25218355f576SJeff Roberson * Nothing 25228355f576SJeff Roberson */ 25238355f576SJeff Roberson static void 252420a4e154SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg) 25258355f576SJeff Roberson { 25268355f576SJeff Roberson 2527111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 2528a81c400eSJeff Roberson zone_foreach_unlocked(zfunc, arg); 2529111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 25308355f576SJeff Roberson } 25318355f576SJeff Roberson 2532f4bef67cSGleb Smirnoff /* 2533a81c400eSJeff Roberson * Initialize the kernel memory allocator. This is done after pages can be 2534a81c400eSJeff Roberson * allocated but before general KVA is available. 2535f4bef67cSGleb Smirnoff */ 2536a81c400eSJeff Roberson void 2537a81c400eSJeff Roberson uma_startup1(vm_offset_t virtual_avail) 2538f4bef67cSGleb Smirnoff { 2539a81c400eSJeff Roberson struct uma_zctor_args args; 2540a81c400eSJeff Roberson size_t ksize, zsize, size; 2541a81c400eSJeff Roberson uma_keg_t masterkeg; 2542a81c400eSJeff Roberson uintptr_t m; 2543a81c400eSJeff Roberson uint8_t pflag; 2544a81c400eSJeff Roberson 2545a81c400eSJeff Roberson bootstart = bootmem = virtual_avail; 2546a81c400eSJeff Roberson 2547a81c400eSJeff Roberson rw_init(&uma_rwlock, "UMA lock"); 2548a81c400eSJeff Roberson sx_init(&uma_reclaim_lock, "umareclaim"); 2549f4bef67cSGleb Smirnoff 2550f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 2551f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 255279c9f942SJeff Roberson ksize = roundup(ksize, UMA_SUPER_ALIGN); 2553f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 2554f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 2555f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 255679c9f942SJeff Roberson zsize = roundup(zsize, UMA_SUPER_ALIGN); 2557f4bef67cSGleb Smirnoff 2558a81c400eSJeff Roberson /* Allocate the zone of zones, zone of kegs, and zone of zones keg. */ 2559a81c400eSJeff Roberson size = (zsize * 2) + ksize; 2560a81c400eSJeff Roberson m = (uintptr_t)startup_alloc(NULL, size, 0, &pflag, M_NOWAIT | M_ZERO); 2561ab3185d1SJeff Roberson zones = (uma_zone_t)m; 256279c9f942SJeff Roberson m += zsize; 2563ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 256479c9f942SJeff Roberson m += zsize; 2565ab3185d1SJeff Roberson masterkeg = (uma_keg_t)m; 2566ab3185d1SJeff Roberson 2567099a0e58SBosko Milekic /* "manually" create the initial zone */ 25680095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2569099a0e58SBosko Milekic args.name = "UMA Kegs"; 2570ab3185d1SJeff Roberson args.size = ksize; 2571099a0e58SBosko Milekic args.ctor = keg_ctor; 2572099a0e58SBosko Milekic args.dtor = keg_dtor; 25738355f576SJeff Roberson args.uminit = zero_init; 25748355f576SJeff Roberson args.fini = NULL; 2575ab3185d1SJeff Roberson args.keg = masterkeg; 257679c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2577b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2578ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 25798355f576SJeff Roberson 2580099a0e58SBosko Milekic args.name = "UMA Zones"; 2581f4bef67cSGleb Smirnoff args.size = zsize; 2582099a0e58SBosko Milekic args.ctor = zone_ctor; 2583099a0e58SBosko Milekic args.dtor = zone_dtor; 2584099a0e58SBosko Milekic args.uminit = zero_init; 2585099a0e58SBosko Milekic args.fini = NULL; 2586099a0e58SBosko Milekic args.keg = NULL; 258779c9f942SJeff Roberson args.align = UMA_SUPER_ALIGN - 1; 2588099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2589ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2590099a0e58SBosko Milekic 25919b8db4d0SRyan Libby /* Now make zones for slab headers */ 25929b8db4d0SRyan Libby slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE, 25939b8db4d0SRyan Libby NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 25949b8db4d0SRyan Libby slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE, 25951e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 25968355f576SJeff Roberson 25978355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 25988355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 25991e0701e1SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 26008355f576SJeff Roberson 2601a81c400eSJeff Roberson bucket_init(); 26028355f576SJeff Roberson } 26038355f576SJeff Roberson 2604a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2605a81c400eSJeff Roberson extern void vm_radix_reserve_kva(void); 2606f4bef67cSGleb Smirnoff #endif 2607f4bef67cSGleb Smirnoff 2608a81c400eSJeff Roberson /* 2609a81c400eSJeff Roberson * Advertise the availability of normal kva allocations and switch to 2610a81c400eSJeff Roberson * the default back-end allocator. Marks the KVA we consumed on startup 2611a81c400eSJeff Roberson * as used in the map. 2612a81c400eSJeff Roberson */ 26138355f576SJeff Roberson void 261499571dc3SJeff Roberson uma_startup2(void) 26158355f576SJeff Roberson { 2616f4bef67cSGleb Smirnoff 2617a81c400eSJeff Roberson if (!PMAP_HAS_DMAP) { 2618a81c400eSJeff Roberson vm_map_lock(kernel_map); 2619a81c400eSJeff Roberson (void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem, 2620a81c400eSJeff Roberson VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 2621a81c400eSJeff Roberson vm_map_unlock(kernel_map); 2622a81c400eSJeff Roberson } 2623a81c400eSJeff Roberson 2624a81c400eSJeff Roberson #ifndef UMA_MD_SMALL_ALLOC 2625a81c400eSJeff Roberson /* Set up radix zone to use noobj_alloc. */ 2626a81c400eSJeff Roberson vm_radix_reserve_kva(); 2627f7d35785SGleb Smirnoff #endif 2628a81c400eSJeff Roberson 2629a81c400eSJeff Roberson booted = BOOT_KVA; 2630a81c400eSJeff Roberson zone_foreach_unlocked(zone_kva_available, NULL); 2631f4bef67cSGleb Smirnoff bucket_enable(); 26328355f576SJeff Roberson } 26338355f576SJeff Roberson 2634a81c400eSJeff Roberson /* 2635a81c400eSJeff Roberson * Finish our initialization steps. 2636a81c400eSJeff Roberson */ 26378355f576SJeff Roberson static void 26388355f576SJeff Roberson uma_startup3(void) 26398355f576SJeff Roberson { 26401431a748SGleb Smirnoff 2641c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2642c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2643c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2644c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2645c5deaf04SGleb Smirnoff #endif 2646a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_counters, NULL); 2647a81c400eSJeff Roberson zone_foreach_unlocked(zone_alloc_sysctl, NULL); 2648fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 26499643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2650c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 2651860bb7a0SMark Johnston 2652860bb7a0SMark Johnston EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL, 2653860bb7a0SMark Johnston EVENTHANDLER_PRI_FIRST); 2654860bb7a0SMark Johnston } 2655860bb7a0SMark Johnston 2656860bb7a0SMark Johnston static void 2657860bb7a0SMark Johnston uma_shutdown(void) 2658860bb7a0SMark Johnston { 2659860bb7a0SMark Johnston 2660860bb7a0SMark Johnston booted = BOOT_SHUTDOWN; 26618355f576SJeff Roberson } 26628355f576SJeff Roberson 2663e20a199fSJeff Roberson static uma_keg_t 2664099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 266585dcf349SGleb Smirnoff int align, uint32_t flags) 2666099a0e58SBosko Milekic { 2667099a0e58SBosko Milekic struct uma_kctor_args args; 2668099a0e58SBosko Milekic 2669099a0e58SBosko Milekic args.size = size; 2670099a0e58SBosko Milekic args.uminit = uminit; 2671099a0e58SBosko Milekic args.fini = fini; 26721e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2673099a0e58SBosko Milekic args.flags = flags; 2674099a0e58SBosko Milekic args.zone = zone; 2675ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 2676099a0e58SBosko Milekic } 2677099a0e58SBosko Milekic 2678f4bef67cSGleb Smirnoff /* Public functions */ 26798355f576SJeff Roberson /* See uma.h */ 26801e319f6dSRobert Watson void 26811e319f6dSRobert Watson uma_set_align(int align) 26821e319f6dSRobert Watson { 26831e319f6dSRobert Watson 26841e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 26851e319f6dSRobert Watson uma_align_cache = align; 26861e319f6dSRobert Watson } 26871e319f6dSRobert Watson 26881e319f6dSRobert Watson /* See uma.h */ 26898355f576SJeff Roberson uma_zone_t 2690bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 269185dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 26928355f576SJeff Roberson 26938355f576SJeff Roberson { 26948355f576SJeff Roberson struct uma_zctor_args args; 269595c4bf75SKonstantin Belousov uma_zone_t res; 26968355f576SJeff Roberson 2697a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 2698a5a35578SJohn Baldwin align, name)); 2699a5a35578SJohn Baldwin 27008355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 27010095a784SJeff Roberson memset(&args, 0, sizeof(args)); 27028355f576SJeff Roberson args.name = name; 27038355f576SJeff Roberson args.size = size; 27048355f576SJeff Roberson args.ctor = ctor; 27058355f576SJeff Roberson args.dtor = dtor; 27068355f576SJeff Roberson args.uminit = uminit; 27078355f576SJeff Roberson args.fini = fini; 2708afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 2709afc6dc36SJohn-Mark Gurney /* 2710ca293436SRyan Libby * Inject procedures which check for memory use after free if we are 2711ca293436SRyan Libby * allowed to scramble the memory while it is not allocated. This 2712ca293436SRyan Libby * requires that: UMA is actually able to access the memory, no init 2713ca293436SRyan Libby * or fini procedures, no dependency on the initial value of the 2714ca293436SRyan Libby * memory, and no (legitimate) use of the memory after free. Note, 2715ca293436SRyan Libby * the ctor and dtor do not need to be empty. 2716afc6dc36SJohn-Mark Gurney */ 271754c5ae80SRyan Libby if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH | 271854c5ae80SRyan Libby UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) { 2719afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 2720afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 2721afc6dc36SJohn-Mark Gurney } 2722afc6dc36SJohn-Mark Gurney #endif 27238355f576SJeff Roberson args.align = align; 27248355f576SJeff Roberson args.flags = flags; 2725099a0e58SBosko Milekic args.keg = NULL; 2726099a0e58SBosko Milekic 272708cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 2728ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 272908cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 2730a81c400eSJeff Roberson 273195c4bf75SKonstantin Belousov return (res); 2732099a0e58SBosko Milekic } 2733099a0e58SBosko Milekic 2734099a0e58SBosko Milekic /* See uma.h */ 2735099a0e58SBosko Milekic uma_zone_t 2736099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 2737099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 2738099a0e58SBosko Milekic { 2739099a0e58SBosko Milekic struct uma_zctor_args args; 2740e20a199fSJeff Roberson uma_keg_t keg; 274195c4bf75SKonstantin Belousov uma_zone_t res; 2742099a0e58SBosko Milekic 2743bb15d1c7SGleb Smirnoff keg = master->uz_keg; 27440095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2745099a0e58SBosko Milekic args.name = name; 2746e20a199fSJeff Roberson args.size = keg->uk_size; 2747099a0e58SBosko Milekic args.ctor = ctor; 2748099a0e58SBosko Milekic args.dtor = dtor; 2749099a0e58SBosko Milekic args.uminit = zinit; 2750099a0e58SBosko Milekic args.fini = zfini; 2751e20a199fSJeff Roberson args.align = keg->uk_align; 2752e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 2753e20a199fSJeff Roberson args.keg = keg; 27548355f576SJeff Roberson 275508cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 2756ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 275708cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 2758a81c400eSJeff Roberson 275995c4bf75SKonstantin Belousov return (res); 27608355f576SJeff Roberson } 27618355f576SJeff Roberson 27620095a784SJeff Roberson /* See uma.h */ 27630095a784SJeff Roberson uma_zone_t 2764af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 2765af526374SJeff Roberson uma_init zinit, uma_fini zfini, uma_import zimport, 2766af526374SJeff Roberson uma_release zrelease, void *arg, int flags) 27670095a784SJeff Roberson { 27680095a784SJeff Roberson struct uma_zctor_args args; 27690095a784SJeff Roberson 27700095a784SJeff Roberson memset(&args, 0, sizeof(args)); 27710095a784SJeff Roberson args.name = name; 2772af526374SJeff Roberson args.size = size; 27730095a784SJeff Roberson args.ctor = ctor; 27740095a784SJeff Roberson args.dtor = dtor; 27750095a784SJeff Roberson args.uminit = zinit; 27760095a784SJeff Roberson args.fini = zfini; 27770095a784SJeff Roberson args.import = zimport; 27780095a784SJeff Roberson args.release = zrelease; 27790095a784SJeff Roberson args.arg = arg; 27800095a784SJeff Roberson args.align = 0; 2781bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 27820095a784SJeff Roberson 2783ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 27840095a784SJeff Roberson } 27850095a784SJeff Roberson 27868355f576SJeff Roberson /* See uma.h */ 27879c2cd7e5SJeff Roberson void 27889c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 27899c2cd7e5SJeff Roberson { 2790f4ff923bSRobert Watson 2791860bb7a0SMark Johnston /* 2792860bb7a0SMark Johnston * Large slabs are expensive to reclaim, so don't bother doing 2793860bb7a0SMark Johnston * unnecessary work if we're shutting down. 2794860bb7a0SMark Johnston */ 2795860bb7a0SMark Johnston if (booted == BOOT_SHUTDOWN && 2796860bb7a0SMark Johnston zone->uz_fini == NULL && zone->uz_release == zone_release) 2797860bb7a0SMark Johnston return; 279808cfa56eSMark Johnston sx_slock(&uma_reclaim_lock); 27990095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 280008cfa56eSMark Johnston sx_sunlock(&uma_reclaim_lock); 28019c2cd7e5SJeff Roberson } 28029c2cd7e5SJeff Roberson 28038d6fbbb8SJeff Roberson void 28048d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 28058d6fbbb8SJeff Roberson { 28068d6fbbb8SJeff Roberson void *item; 28078d6fbbb8SJeff Roberson 28088d6fbbb8SJeff Roberson item = uma_zalloc_arg(zone, NULL, M_WAITOK); 28098d6fbbb8SJeff Roberson uma_zfree(zone, item); 28108d6fbbb8SJeff Roberson } 28118d6fbbb8SJeff Roberson 28124e180881SMateusz Guzik void * 28134e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 28144e180881SMateusz Guzik { 28154e180881SMateusz Guzik void *item; 2816b4799947SRuslan Bukin #ifdef SMP 28174e180881SMateusz Guzik int i; 28184e180881SMateusz Guzik 28194e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2820b4799947SRuslan Bukin #endif 28214e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 28224e180881SMateusz Guzik if (item != NULL && (flags & M_ZERO)) { 2823b4799947SRuslan Bukin #ifdef SMP 2824013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 28254e180881SMateusz Guzik bzero(zpcpu_get_cpu(item, i), zone->uz_size); 2826b4799947SRuslan Bukin #else 2827b4799947SRuslan Bukin bzero(item, zone->uz_size); 2828b4799947SRuslan Bukin #endif 28294e180881SMateusz Guzik } 28304e180881SMateusz Guzik return (item); 28314e180881SMateusz Guzik } 28324e180881SMateusz Guzik 28334e180881SMateusz Guzik /* 28344e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 28354e180881SMateusz Guzik */ 28364e180881SMateusz Guzik void 28374e180881SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata) 28384e180881SMateusz Guzik { 28394e180881SMateusz Guzik 2840c5b7751fSIan Lepore #ifdef SMP 28414e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2842c5b7751fSIan Lepore #endif 28434e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 28444e180881SMateusz Guzik } 28454e180881SMateusz Guzik 2846cc7ce83aSJeff Roberson #ifdef INVARIANTS 2847cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 1 2848cc7ce83aSJeff Roberson #else 2849cc7ce83aSJeff Roberson #define UMA_ALWAYS_CTORDTOR 0 2850cc7ce83aSJeff Roberson #endif 2851cc7ce83aSJeff Roberson 2852beb8beefSJeff Roberson static void * 2853cc7ce83aSJeff Roberson item_ctor(uma_zone_t zone, int size, void *udata, int flags, void *item) 2854beb8beefSJeff Roberson { 2855beb8beefSJeff Roberson #ifdef INVARIANTS 2856ca293436SRyan Libby bool skipdbg; 2857beb8beefSJeff Roberson 2858beb8beefSJeff Roberson skipdbg = uma_dbg_zskip(zone, item); 2859ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2860ca293436SRyan Libby zone->uz_ctor != trash_ctor) 2861cc7ce83aSJeff Roberson trash_ctor(item, size, udata, flags); 2862beb8beefSJeff Roberson #endif 2863ca293436SRyan Libby if (__predict_false(zone->uz_ctor != NULL) && 2864cc7ce83aSJeff Roberson zone->uz_ctor(item, size, udata, flags) != 0) { 2865beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 2866beb8beefSJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 2867beb8beefSJeff Roberson return (NULL); 2868beb8beefSJeff Roberson } 2869beb8beefSJeff Roberson #ifdef INVARIANTS 2870beb8beefSJeff Roberson if (!skipdbg) 2871beb8beefSJeff Roberson uma_dbg_alloc(zone, NULL, item); 2872beb8beefSJeff Roberson #endif 2873beb8beefSJeff Roberson if (flags & M_ZERO) 2874cc7ce83aSJeff Roberson bzero(item, size); 2875beb8beefSJeff Roberson 2876beb8beefSJeff Roberson return (item); 2877beb8beefSJeff Roberson } 2878beb8beefSJeff Roberson 2879ca293436SRyan Libby static inline void 2880cc7ce83aSJeff Roberson item_dtor(uma_zone_t zone, void *item, int size, void *udata, 2881cc7ce83aSJeff Roberson enum zfreeskip skip) 2882ca293436SRyan Libby { 2883ca293436SRyan Libby #ifdef INVARIANTS 2884ca293436SRyan Libby bool skipdbg; 2885ca293436SRyan Libby 2886ca293436SRyan Libby skipdbg = uma_dbg_zskip(zone, item); 2887ca293436SRyan Libby if (skip == SKIP_NONE && !skipdbg) { 2888ca293436SRyan Libby if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0) 2889ca293436SRyan Libby uma_dbg_free(zone, udata, item); 2890ca293436SRyan Libby else 2891ca293436SRyan Libby uma_dbg_free(zone, NULL, item); 2892ca293436SRyan Libby } 2893ca293436SRyan Libby #endif 2894cc7ce83aSJeff Roberson if (__predict_true(skip < SKIP_DTOR)) { 2895ca293436SRyan Libby if (zone->uz_dtor != NULL) 2896cc7ce83aSJeff Roberson zone->uz_dtor(item, size, udata); 2897ca293436SRyan Libby #ifdef INVARIANTS 2898ca293436SRyan Libby if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 && 2899ca293436SRyan Libby zone->uz_dtor != trash_dtor) 2900cc7ce83aSJeff Roberson trash_dtor(item, size, udata); 2901ca293436SRyan Libby #endif 2902ca293436SRyan Libby } 2903ca293436SRyan Libby } 2904ca293436SRyan Libby 29059c2cd7e5SJeff Roberson /* See uma.h */ 29068355f576SJeff Roberson void * 29072cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 29088355f576SJeff Roberson { 2909376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 2910ab3185d1SJeff Roberson uma_cache_t cache; 2911ab3185d1SJeff Roberson void *item; 2912cc7ce83aSJeff Roberson int domain, size, uz_flags; 29138355f576SJeff Roberson 2914e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 291519fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 291610cb2424SMark Murray 29178355f576SJeff Roberson /* This is the fast path allocation */ 2918e63a1c2fSRyan Libby CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name, 2919e63a1c2fSRyan Libby zone, flags); 2920a553d4b8SJeff Roberson 2921cc7ce83aSJeff Roberson #ifdef WITNESS 2922635fd505SRobert Watson if (flags & M_WAITOK) { 2923b23f72e9SBrian Feldman WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2924635fd505SRobert Watson "uma_zalloc_arg: zone \"%s\"", zone->uz_name); 29254c1cc01cSJohn Baldwin } 2926cc7ce83aSJeff Roberson #endif 2927cc7ce83aSJeff Roberson 2928cc7ce83aSJeff Roberson #ifdef INVARIANTS 29290766f278SJonathan T. Looney KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC")); 2930d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 29311067a2baSJonathan T. Looney ("uma_zalloc_arg: called with spinlock or critical section held")); 2932ea99223eSMateusz Guzik if (zone->uz_flags & UMA_ZONE_PCPU) 2933b8af2820SMateusz Guzik KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone " 2934b8af2820SMateusz Guzik "with M_ZERO passed")); 2935cc7ce83aSJeff Roberson #endif 29361067a2baSJonathan T. Looney 29378d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 29388d689e04SGleb Smirnoff if (memguard_cmp_zone(zone)) { 29398d689e04SGleb Smirnoff item = memguard_alloc(zone->uz_size, flags); 29408d689e04SGleb Smirnoff if (item != NULL) { 29418d689e04SGleb Smirnoff if (zone->uz_init != NULL && 29428d689e04SGleb Smirnoff zone->uz_init(item, zone->uz_size, flags) != 0) 29438d689e04SGleb Smirnoff return (NULL); 29448d689e04SGleb Smirnoff if (zone->uz_ctor != NULL && 2945fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 2946fc03d22bSJeff Roberson flags) != 0) { 2947ca293436SRyan Libby counter_u64_add(zone->uz_fails, 1); 29488d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 29498d689e04SGleb Smirnoff return (NULL); 29508d689e04SGleb Smirnoff } 29518d689e04SGleb Smirnoff return (item); 29528d689e04SGleb Smirnoff } 29538d689e04SGleb Smirnoff /* This is unfortunate but should not be fatal. */ 29548d689e04SGleb Smirnoff } 29558d689e04SGleb Smirnoff #endif 29565d1ae027SRobert Watson /* 29575d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 29585d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 29595d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 29605d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 29615d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 29625d1ae027SRobert Watson * preemption and migration. We release the critical section in 29635d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 29645d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 29655d1ae027SRobert Watson * must detect and handle migration if it has occurred. 29665d1ae027SRobert Watson */ 29675d1ae027SRobert Watson critical_enter(); 2968beb8beefSJeff Roberson do { 2969cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 2970376b1ba3SJeff Roberson bucket = &cache->uc_allocbucket; 2971cc7ce83aSJeff Roberson size = cache_uz_size(cache); 2972cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 2973376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt != 0)) { 2974376b1ba3SJeff Roberson item = cache_bucket_pop(cache, bucket); 29755d1ae027SRobert Watson critical_exit(); 2976cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 2977cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 2978cc7ce83aSJeff Roberson return (item_ctor(zone, size, udata, flags, item)); 2979cc7ce83aSJeff Roberson if (flags & M_ZERO) 2980cc7ce83aSJeff Roberson bzero(item, size); 2981cc7ce83aSJeff Roberson return (item); 2982b23f72e9SBrian Feldman } 2983beb8beefSJeff Roberson } while (cache_alloc(zone, cache, udata, flags)); 2984beb8beefSJeff Roberson critical_exit(); 2985beb8beefSJeff Roberson 2986beb8beefSJeff Roberson /* 2987beb8beefSJeff Roberson * We can not get a bucket so try to return a single item. 2988beb8beefSJeff Roberson */ 2989dfe13344SJeff Roberson if (uz_flags & UMA_ZONE_FIRSTTOUCH) 2990beb8beefSJeff Roberson domain = PCPU_GET(domain); 2991beb8beefSJeff Roberson else 2992beb8beefSJeff Roberson domain = UMA_ANYDOMAIN; 29934bd61e19SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 2994fc03d22bSJeff Roberson } 2995fc03d22bSJeff Roberson 29968355f576SJeff Roberson /* 2997beb8beefSJeff Roberson * Replenish an alloc bucket and possibly restore an old one. Called in 2998beb8beefSJeff Roberson * a critical section. Returns in a critical section. 2999beb8beefSJeff Roberson * 30004bd61e19SJeff Roberson * A false return value indicates an allocation failure. 30014bd61e19SJeff Roberson * A true return value indicates success and the caller should retry. 3002beb8beefSJeff Roberson */ 3003beb8beefSJeff Roberson static __noinline bool 3004beb8beefSJeff Roberson cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags) 3005beb8beefSJeff Roberson { 3006beb8beefSJeff Roberson uma_zone_domain_t zdom; 3007beb8beefSJeff Roberson uma_bucket_t bucket; 3008cc7ce83aSJeff Roberson int domain; 3009beb8beefSJeff Roberson bool lockfail; 3010beb8beefSJeff Roberson 3011beb8beefSJeff Roberson CRITICAL_ASSERT(curthread); 3012beb8beefSJeff Roberson 3013beb8beefSJeff Roberson /* 3014beb8beefSJeff Roberson * If we have run out of items in our alloc bucket see 3015beb8beefSJeff Roberson * if we can switch with the free bucket. 30168355f576SJeff Roberson */ 3017376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_cnt != 0) { 3018376b1ba3SJeff Roberson cache_bucket_swap(&cache->uc_freebucket, &cache->uc_allocbucket); 3019beb8beefSJeff Roberson return (true); 30208355f576SJeff Roberson } 3021fc03d22bSJeff Roberson 3022fc03d22bSJeff Roberson /* 3023fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 3024fc03d22bSJeff Roberson */ 3025376b1ba3SJeff Roberson bucket = cache_bucket_unload_alloc(cache); 3026fc03d22bSJeff Roberson critical_exit(); 3027fc03d22bSJeff Roberson if (bucket != NULL) 30286fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3029fc03d22bSJeff Roberson 30304bd61e19SJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 30314bd61e19SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) { 30324bd61e19SJeff Roberson critical_enter(); 30334bd61e19SJeff Roberson return (false); 30344bd61e19SJeff Roberson } 30354bd61e19SJeff Roberson 30365d1ae027SRobert Watson /* 30375d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 30385d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 30395d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 30405d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 30415d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 30425d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 30435d1ae027SRobert Watson * the critical section. 30445d1ae027SRobert Watson */ 3045fc03d22bSJeff Roberson lockfail = 0; 3046fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 3047fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 3048a553d4b8SJeff Roberson ZONE_LOCK(zone); 3049fc03d22bSJeff Roberson lockfail = 1; 3050fc03d22bSJeff Roberson } 3051beb8beefSJeff Roberson 3052fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 30534bd61e19SJeff Roberson critical_enter(); 30544bd61e19SJeff Roberson cache = &zone->uz_cpu[curcpu]; 3055376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket != NULL) { 3056fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 3057beb8beefSJeff Roberson return (true); 3058a553d4b8SJeff Roberson } 30598355f576SJeff Roberson 3060fc03d22bSJeff Roberson /* 3061fc03d22bSJeff Roberson * Check the zone's cache of buckets. 3062fc03d22bSJeff Roberson */ 3063dfe13344SJeff Roberson if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH) { 3064c1685086SJeff Roberson domain = PCPU_GET(domain); 3065ab3185d1SJeff Roberson zdom = &zone->uz_domain[domain]; 3066c1685086SJeff Roberson } else { 3067c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3068c1685086SJeff Roberson zdom = &zone->uz_domain[0]; 3069c1685086SJeff Roberson } 3070c1685086SJeff Roberson 307108cfa56eSMark Johnston if ((bucket = zone_fetch_bucket(zone, zdom)) != NULL) { 3072beb8beefSJeff Roberson ZONE_UNLOCK(zone); 3073cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 3074a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 3075376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 3076beb8beefSJeff Roberson return (true); 3077a553d4b8SJeff Roberson } 30785d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 30795d1ae027SRobert Watson critical_exit(); 3080bbee39c6SJeff Roberson 3081fc03d22bSJeff Roberson /* 3082fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 3083fc03d22bSJeff Roberson * handle the working set. 3084fc03d22bSJeff Roberson */ 308520a4e154SJeff Roberson if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max) 308620a4e154SJeff Roberson zone->uz_bucket_size++; 30874bd61e19SJeff Roberson ZONE_UNLOCK(zone); 3088bb15d1c7SGleb Smirnoff 30898355f576SJeff Roberson /* 3090beb8beefSJeff Roberson * Fill a bucket and attempt to use it as the alloc bucket. 3091bbee39c6SJeff Roberson */ 3092beb8beefSJeff Roberson bucket = zone_alloc_bucket(zone, udata, domain, flags); 30931431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 30941431a748SGleb Smirnoff zone->uz_name, zone, bucket); 30954bd61e19SJeff Roberson if (bucket == NULL) { 3096fc03d22bSJeff Roberson critical_enter(); 3097beb8beefSJeff Roberson return (false); 30984bd61e19SJeff Roberson } 30990f9b7bf3SMark Johnston 3100fc03d22bSJeff Roberson /* 3101fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 3102fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 3103fc03d22bSJeff Roberson * the memory directly. 3104fc03d22bSJeff Roberson */ 31054bd61e19SJeff Roberson ZONE_LOCK(zone); 31064bd61e19SJeff Roberson critical_enter(); 3107cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3108376b1ba3SJeff Roberson if (cache->uc_allocbucket.ucb_bucket == NULL && 3109dfe13344SJeff Roberson ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0 || 311081c0d72cSGleb Smirnoff domain == PCPU_GET(domain))) { 3111376b1ba3SJeff Roberson cache_bucket_load_alloc(cache, bucket); 31120f9b7bf3SMark Johnston zdom->uzd_imax += bucket->ub_cnt; 3113bb15d1c7SGleb Smirnoff } else if (zone->uz_bkt_count >= zone->uz_bkt_max) { 311481c0d72cSGleb Smirnoff critical_exit(); 311581c0d72cSGleb Smirnoff ZONE_UNLOCK(zone); 311681c0d72cSGleb Smirnoff bucket_drain(zone, bucket); 311781c0d72cSGleb Smirnoff bucket_free(zone, bucket, udata); 3118beb8beefSJeff Roberson critical_enter(); 3119beb8beefSJeff Roberson return (true); 312081c0d72cSGleb Smirnoff } else 31210f9b7bf3SMark Johnston zone_put_bucket(zone, zdom, bucket, false); 3122bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 3123beb8beefSJeff Roberson return (true); 3124bbee39c6SJeff Roberson } 3125bbee39c6SJeff Roberson 3126ab3185d1SJeff Roberson void * 3127ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 3128bbee39c6SJeff Roberson { 3129ab3185d1SJeff Roberson 3130ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 313119fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3132ab3185d1SJeff Roberson 3133ab3185d1SJeff Roberson /* This is the fast path allocation */ 3134e63a1c2fSRyan Libby CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d", 3135e63a1c2fSRyan Libby zone->uz_name, zone, domain, flags); 3136ab3185d1SJeff Roberson 3137ab3185d1SJeff Roberson if (flags & M_WAITOK) { 3138ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 3139ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 3140ab3185d1SJeff Roberson } 3141ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3142ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 3143ab3185d1SJeff Roberson 3144ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 3145ab3185d1SJeff Roberson } 3146ab3185d1SJeff Roberson 3147ab3185d1SJeff Roberson /* 3148ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 3149ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 3150ab3185d1SJeff Roberson * 3151ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 3152ab3185d1SJeff Roberson * only 'domain'. 3153ab3185d1SJeff Roberson */ 3154ab3185d1SJeff Roberson static uma_slab_t 3155194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 3156ab3185d1SJeff Roberson { 3157ab3185d1SJeff Roberson uma_domain_t dom; 3158bbee39c6SJeff Roberson uma_slab_t slab; 3159ab3185d1SJeff Roberson int start; 3160ab3185d1SJeff Roberson 3161ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3162ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 31638b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, domain); 3164ab3185d1SJeff Roberson 3165ab3185d1SJeff Roberson slab = NULL; 3166ab3185d1SJeff Roberson start = domain; 3167ab3185d1SJeff Roberson do { 3168ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 3169ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_part_slab)) 3170ab3185d1SJeff Roberson return (LIST_FIRST(&dom->ud_part_slab)); 3171ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_free_slab)) { 3172ab3185d1SJeff Roberson slab = LIST_FIRST(&dom->ud_free_slab); 3173ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 3174ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 3175ab3185d1SJeff Roberson return (slab); 3176ab3185d1SJeff Roberson } 3177ab3185d1SJeff Roberson if (rr) 3178ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 3179ab3185d1SJeff Roberson } while (domain != start); 3180ab3185d1SJeff Roberson 3181ab3185d1SJeff Roberson return (NULL); 3182ab3185d1SJeff Roberson } 3183ab3185d1SJeff Roberson 31848b987a77SJeff Roberson /* 31858b987a77SJeff Roberson * Fetch an existing slab from a free or partial list. Returns with the 31868b987a77SJeff Roberson * keg domain lock held if a slab was found or unlocked if not. 31878b987a77SJeff Roberson */ 3188ab3185d1SJeff Roberson static uma_slab_t 3189194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 3190ab3185d1SJeff Roberson { 31918b987a77SJeff Roberson uma_slab_t slab; 3192194a979eSMark Johnston uint32_t reserve; 3193099a0e58SBosko Milekic 31948b987a77SJeff Roberson /* HASH has a single free list. */ 319554c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) 31968b987a77SJeff Roberson domain = 0; 3197194a979eSMark Johnston 31988b987a77SJeff Roberson KEG_LOCK(keg, domain); 3199194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 32008b987a77SJeff Roberson if (keg->uk_domain[domain].ud_free <= reserve || 32018b987a77SJeff Roberson (slab = keg_first_slab(keg, domain, rr)) == NULL) { 32028b987a77SJeff Roberson KEG_UNLOCK(keg, domain); 3203194a979eSMark Johnston return (NULL); 32048b987a77SJeff Roberson } 32058b987a77SJeff Roberson return (slab); 3206194a979eSMark Johnston } 3207194a979eSMark Johnston 3208194a979eSMark Johnston static uma_slab_t 3209194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 3210194a979eSMark Johnston { 3211194a979eSMark Johnston struct vm_domainset_iter di; 3212194a979eSMark Johnston uma_slab_t slab; 3213194a979eSMark Johnston int aflags, domain; 3214194a979eSMark Johnston bool rr; 3215194a979eSMark Johnston 3216194a979eSMark Johnston restart: 3217bbee39c6SJeff Roberson /* 3218194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 3219194a979eSMark Johnston * domain (as happens with first-touch zones). 3220194a979eSMark Johnston * 3221194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 3222194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 3223194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 3224bbee39c6SJeff Roberson */ 3225ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 3226ab3185d1SJeff Roberson if (rr) { 3227194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 3228194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 3229194a979eSMark Johnston &aflags); 3230194a979eSMark Johnston } else { 3231194a979eSMark Johnston aflags = flags; 3232194a979eSMark Johnston domain = rdomain; 3233194a979eSMark Johnston } 3234ab3185d1SJeff Roberson 3235194a979eSMark Johnston for (;;) { 3236194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 3237584061b4SJeff Roberson if (slab != NULL) 3238bbee39c6SJeff Roberson return (slab); 3239bbee39c6SJeff Roberson 3240bbee39c6SJeff Roberson /* 3241bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 3242bbee39c6SJeff Roberson */ 3243bbee39c6SJeff Roberson if (flags & M_NOVM) 3244bbee39c6SJeff Roberson break; 3245bbee39c6SJeff Roberson 324686220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags, aflags); 32478b987a77SJeff Roberson if (slab != NULL) 3248bbee39c6SJeff Roberson return (slab); 32493639ac42SJeff Roberson if (!rr && (flags & M_WAITOK) == 0) 32503639ac42SJeff Roberson break; 3251194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 3252194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 3253194a979eSMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 3254194a979eSMark Johnston goto restart; 325530c5525bSAndrew Gallatin } 3256194a979eSMark Johnston break; 3257194a979eSMark Johnston } 3258ab3185d1SJeff Roberson } 3259ab3185d1SJeff Roberson 3260bbee39c6SJeff Roberson /* 3261bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 3262bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 3263bbee39c6SJeff Roberson * fail. 3264bbee39c6SJeff Roberson */ 32658b987a77SJeff Roberson if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) 3266bbee39c6SJeff Roberson return (slab); 32678b987a77SJeff Roberson 3268ab3185d1SJeff Roberson return (NULL); 3269ab3185d1SJeff Roberson } 3270bbee39c6SJeff Roberson 3271d56368d7SBosko Milekic static void * 32720095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 3273bbee39c6SJeff Roberson { 3274ab3185d1SJeff Roberson uma_domain_t dom; 3275bbee39c6SJeff Roberson void *item; 32769b8db4d0SRyan Libby int freei; 3277bbee39c6SJeff Roberson 32788b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3279099a0e58SBosko Milekic 32808b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 32819b78b1f4SJeff Roberson freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1; 32829b78b1f4SJeff Roberson BIT_CLR(keg->uk_ipers, freei, &slab->us_free); 32831e0701e1SJeff Roberson item = slab_item(slab, keg, freei); 3284bbee39c6SJeff Roberson slab->us_freecount--; 32858b987a77SJeff Roberson dom->ud_free--; 3286ef72505eSJeff Roberson 3287bbee39c6SJeff Roberson /* Move this slab to the full list */ 3288bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 3289bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 3290ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 3291bbee39c6SJeff Roberson } 3292bbee39c6SJeff Roberson 3293bbee39c6SJeff Roberson return (item); 3294bbee39c6SJeff Roberson } 3295bbee39c6SJeff Roberson 3296bbee39c6SJeff Roberson static int 3297b75c4efcSAndrew Turner zone_import(void *arg, void **bucket, int max, int domain, int flags) 32980095a784SJeff Roberson { 32998b987a77SJeff Roberson uma_domain_t dom; 3300b75c4efcSAndrew Turner uma_zone_t zone; 33010095a784SJeff Roberson uma_slab_t slab; 33020095a784SJeff Roberson uma_keg_t keg; 3303a03af342SSean Bruno #ifdef NUMA 3304ab3185d1SJeff Roberson int stripe; 3305a03af342SSean Bruno #endif 33060095a784SJeff Roberson int i; 33070095a784SJeff Roberson 3308b75c4efcSAndrew Turner zone = arg; 33090095a784SJeff Roberson slab = NULL; 3310584061b4SJeff Roberson keg = zone->uz_keg; 3311af526374SJeff Roberson /* Try to keep the buckets totally full */ 33120095a784SJeff Roberson for (i = 0; i < max; ) { 3313584061b4SJeff Roberson if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL) 33140095a784SJeff Roberson break; 3315a03af342SSean Bruno #ifdef NUMA 3316ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 3317a03af342SSean Bruno #endif 33188b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 33196fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 33200095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 33218b987a77SJeff Roberson if (dom->ud_free <= keg->uk_reserve) 33226fd34d6fSJeff Roberson break; 3323b6715dabSJeff Roberson #ifdef NUMA 3324ab3185d1SJeff Roberson /* 3325ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 3326ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 3327ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 3328ab3185d1SJeff Roberson * than stripe within each bucket. The current option 3329ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 3330ab3185d1SJeff Roberson * time but yields better distribution. 3331ab3185d1SJeff Roberson */ 3332dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 && 3333ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 3334ab3185d1SJeff Roberson break; 3335ab3185d1SJeff Roberson #endif 33366fd34d6fSJeff Roberson } 33378b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 3338ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 33390095a784SJeff Roberson flags &= ~M_WAITOK; 33400095a784SJeff Roberson flags |= M_NOWAIT; 33410095a784SJeff Roberson } 33420095a784SJeff Roberson 33430095a784SJeff Roberson return i; 33440095a784SJeff Roberson } 33450095a784SJeff Roberson 33464bd61e19SJeff Roberson static int 33474bd61e19SJeff Roberson zone_alloc_limit_hard(uma_zone_t zone, int count, int flags) 33484bd61e19SJeff Roberson { 33494bd61e19SJeff Roberson uint64_t old, new, total, max; 33504bd61e19SJeff Roberson 33514bd61e19SJeff Roberson /* 33524bd61e19SJeff Roberson * The hard case. We're going to sleep because there were existing 33534bd61e19SJeff Roberson * sleepers or because we ran out of items. This routine enforces 33544bd61e19SJeff Roberson * fairness by keeping fifo order. 33554bd61e19SJeff Roberson * 33564bd61e19SJeff Roberson * First release our ill gotten gains and make some noise. 33574bd61e19SJeff Roberson */ 33584bd61e19SJeff Roberson for (;;) { 33594bd61e19SJeff Roberson zone_free_limit(zone, count); 33604bd61e19SJeff Roberson zone_log_warning(zone); 33614bd61e19SJeff Roberson zone_maxaction(zone); 33624bd61e19SJeff Roberson if (flags & M_NOWAIT) 33634bd61e19SJeff Roberson return (0); 33644bd61e19SJeff Roberson 33654bd61e19SJeff Roberson /* 33664bd61e19SJeff Roberson * We need to allocate an item or set ourself as a sleeper 33674bd61e19SJeff Roberson * while the sleepq lock is held to avoid wakeup races. This 33684bd61e19SJeff Roberson * is essentially a home rolled semaphore. 33694bd61e19SJeff Roberson */ 33704bd61e19SJeff Roberson sleepq_lock(&zone->uz_max_items); 33714bd61e19SJeff Roberson old = zone->uz_items; 33724bd61e19SJeff Roberson do { 33734bd61e19SJeff Roberson MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX); 33744bd61e19SJeff Roberson /* Cache the max since we will evaluate twice. */ 33754bd61e19SJeff Roberson max = zone->uz_max_items; 33764bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(old) != 0 || 33774bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) >= max) 33784bd61e19SJeff Roberson new = old + UZ_ITEMS_SLEEPER; 33794bd61e19SJeff Roberson else 33804bd61e19SJeff Roberson new = old + MIN(count, max - old); 33814bd61e19SJeff Roberson } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0); 33824bd61e19SJeff Roberson 33834bd61e19SJeff Roberson /* We may have successfully allocated under the sleepq lock. */ 33844bd61e19SJeff Roberson if (UZ_ITEMS_SLEEPERS(new) == 0) { 33854bd61e19SJeff Roberson sleepq_release(&zone->uz_max_items); 33864bd61e19SJeff Roberson return (new - old); 33874bd61e19SJeff Roberson } 33884bd61e19SJeff Roberson 33894bd61e19SJeff Roberson /* 33904bd61e19SJeff Roberson * This is in a different cacheline from uz_items so that we 33914bd61e19SJeff Roberson * don't constantly invalidate the fastpath cacheline when we 33924bd61e19SJeff Roberson * adjust item counts. This could be limited to toggling on 33934bd61e19SJeff Roberson * transitions. 33944bd61e19SJeff Roberson */ 33954bd61e19SJeff Roberson atomic_add_32(&zone->uz_sleepers, 1); 33964bd61e19SJeff Roberson atomic_add_64(&zone->uz_sleeps, 1); 33974bd61e19SJeff Roberson 33984bd61e19SJeff Roberson /* 33994bd61e19SJeff Roberson * We have added ourselves as a sleeper. The sleepq lock 34004bd61e19SJeff Roberson * protects us from wakeup races. Sleep now and then retry. 34014bd61e19SJeff Roberson */ 34024bd61e19SJeff Roberson sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0); 34034bd61e19SJeff Roberson sleepq_wait(&zone->uz_max_items, PVM); 34044bd61e19SJeff Roberson 34054bd61e19SJeff Roberson /* 34064bd61e19SJeff Roberson * After wakeup, remove ourselves as a sleeper and try 34074bd61e19SJeff Roberson * again. We no longer have the sleepq lock for protection. 34084bd61e19SJeff Roberson * 34094bd61e19SJeff Roberson * Subract ourselves as a sleeper while attempting to add 34104bd61e19SJeff Roberson * our count. 34114bd61e19SJeff Roberson */ 34124bd61e19SJeff Roberson atomic_subtract_32(&zone->uz_sleepers, 1); 34134bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, 34144bd61e19SJeff Roberson -(UZ_ITEMS_SLEEPER - count)); 34154bd61e19SJeff Roberson /* We're no longer a sleeper. */ 34164bd61e19SJeff Roberson old -= UZ_ITEMS_SLEEPER; 34174bd61e19SJeff Roberson 34184bd61e19SJeff Roberson /* 34194bd61e19SJeff Roberson * If we're still at the limit, restart. Notably do not 34204bd61e19SJeff Roberson * block on other sleepers. Cache the max value to protect 34214bd61e19SJeff Roberson * against changes via sysctl. 34224bd61e19SJeff Roberson */ 34234bd61e19SJeff Roberson total = UZ_ITEMS_COUNT(old); 34244bd61e19SJeff Roberson max = zone->uz_max_items; 34254bd61e19SJeff Roberson if (total >= max) 34264bd61e19SJeff Roberson continue; 34274bd61e19SJeff Roberson /* Truncate if necessary, otherwise wake other sleepers. */ 34284bd61e19SJeff Roberson if (total + count > max) { 34294bd61e19SJeff Roberson zone_free_limit(zone, total + count - max); 34304bd61e19SJeff Roberson count = max - total; 34314bd61e19SJeff Roberson } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0) 34324bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 34334bd61e19SJeff Roberson 34344bd61e19SJeff Roberson return (count); 34354bd61e19SJeff Roberson } 34364bd61e19SJeff Roberson } 34374bd61e19SJeff Roberson 34384bd61e19SJeff Roberson /* 34394bd61e19SJeff Roberson * Allocate 'count' items from our max_items limit. Returns the number 34404bd61e19SJeff Roberson * available. If M_NOWAIT is not specified it will sleep until at least 34414bd61e19SJeff Roberson * one item can be allocated. 34424bd61e19SJeff Roberson */ 34434bd61e19SJeff Roberson static int 34444bd61e19SJeff Roberson zone_alloc_limit(uma_zone_t zone, int count, int flags) 34454bd61e19SJeff Roberson { 34464bd61e19SJeff Roberson uint64_t old; 34474bd61e19SJeff Roberson uint64_t max; 34484bd61e19SJeff Roberson 34494bd61e19SJeff Roberson max = zone->uz_max_items; 34504bd61e19SJeff Roberson MPASS(max > 0); 34514bd61e19SJeff Roberson 34524bd61e19SJeff Roberson /* 34534bd61e19SJeff Roberson * We expect normal allocations to succeed with a simple 34544bd61e19SJeff Roberson * fetchadd. 34554bd61e19SJeff Roberson */ 34564bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, count); 34574bd61e19SJeff Roberson if (__predict_true(old + count <= max)) 34584bd61e19SJeff Roberson return (count); 34594bd61e19SJeff Roberson 34604bd61e19SJeff Roberson /* 34614bd61e19SJeff Roberson * If we had some items and no sleepers just return the 34624bd61e19SJeff Roberson * truncated value. We have to release the excess space 34634bd61e19SJeff Roberson * though because that may wake sleepers who weren't woken 34644bd61e19SJeff Roberson * because we were temporarily over the limit. 34654bd61e19SJeff Roberson */ 34664bd61e19SJeff Roberson if (old < max) { 34674bd61e19SJeff Roberson zone_free_limit(zone, (old + count) - max); 34684bd61e19SJeff Roberson return (max - old); 34694bd61e19SJeff Roberson } 34704bd61e19SJeff Roberson return (zone_alloc_limit_hard(zone, count, flags)); 34714bd61e19SJeff Roberson } 34724bd61e19SJeff Roberson 34734bd61e19SJeff Roberson /* 34744bd61e19SJeff Roberson * Free a number of items back to the limit. 34754bd61e19SJeff Roberson */ 34764bd61e19SJeff Roberson static void 34774bd61e19SJeff Roberson zone_free_limit(uma_zone_t zone, int count) 34784bd61e19SJeff Roberson { 34794bd61e19SJeff Roberson uint64_t old; 34804bd61e19SJeff Roberson 34814bd61e19SJeff Roberson MPASS(count > 0); 34824bd61e19SJeff Roberson 34834bd61e19SJeff Roberson /* 34844bd61e19SJeff Roberson * In the common case we either have no sleepers or 34854bd61e19SJeff Roberson * are still over the limit and can just return. 34864bd61e19SJeff Roberson */ 34874bd61e19SJeff Roberson old = atomic_fetchadd_64(&zone->uz_items, -count); 34884bd61e19SJeff Roberson if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 || 34894bd61e19SJeff Roberson UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items)) 34904bd61e19SJeff Roberson return; 34914bd61e19SJeff Roberson 34924bd61e19SJeff Roberson /* 34934bd61e19SJeff Roberson * Moderate the rate of wakeups. Sleepers will continue 34944bd61e19SJeff Roberson * to generate wakeups if necessary. 34954bd61e19SJeff Roberson */ 34964bd61e19SJeff Roberson wakeup_one(&zone->uz_max_items); 34974bd61e19SJeff Roberson } 34984bd61e19SJeff Roberson 3499fc03d22bSJeff Roberson static uma_bucket_t 3500beb8beefSJeff Roberson zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags) 3501bbee39c6SJeff Roberson { 3502bbee39c6SJeff Roberson uma_bucket_t bucket; 3503beb8beefSJeff Roberson int maxbucket, cnt; 3504bbee39c6SJeff Roberson 3505e63a1c2fSRyan Libby CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name, 3506e63a1c2fSRyan Libby zone, domain); 350730c5525bSAndrew Gallatin 3508c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3509c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 3510c1685086SJeff Roberson domain = UMA_ANYDOMAIN; 3511c1685086SJeff Roberson 35124bd61e19SJeff Roberson if (zone->uz_max_items > 0) 35134bd61e19SJeff Roberson maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size, 35144bd61e19SJeff Roberson M_NOWAIT); 35154bd61e19SJeff Roberson else 351620a4e154SJeff Roberson maxbucket = zone->uz_bucket_size; 35174bd61e19SJeff Roberson if (maxbucket == 0) 35184bd61e19SJeff Roberson return (false); 3519beb8beefSJeff Roberson 35206fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 35216fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 3522beb8beefSJeff Roberson if (bucket == NULL) { 3523beb8beefSJeff Roberson cnt = 0; 3524beb8beefSJeff Roberson goto out; 3525beb8beefSJeff Roberson } 35260095a784SJeff Roberson 35270095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 3528beb8beefSJeff Roberson MIN(maxbucket, bucket->ub_entries), domain, flags); 35290095a784SJeff Roberson 35300095a784SJeff Roberson /* 35310095a784SJeff Roberson * Initialize the memory if necessary. 35320095a784SJeff Roberson */ 35330095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 3534099a0e58SBosko Milekic int i; 3535bbee39c6SJeff Roberson 35360095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 3537e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 35380095a784SJeff Roberson flags) != 0) 3539b23f72e9SBrian Feldman break; 3540b23f72e9SBrian Feldman /* 3541b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 3542b23f72e9SBrian Feldman * rest back onto the freelist. 3543b23f72e9SBrian Feldman */ 3544b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 3545af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 35460095a784SJeff Roberson bucket->ub_cnt - i); 3547a5a262c6SBosko Milekic #ifdef INVARIANTS 35480095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 35490095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 3550a5a262c6SBosko Milekic #endif 3551b23f72e9SBrian Feldman bucket->ub_cnt = i; 3552b23f72e9SBrian Feldman } 3553099a0e58SBosko Milekic } 3554099a0e58SBosko Milekic 3555beb8beefSJeff Roberson cnt = bucket->ub_cnt; 3556f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 35576fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 35582efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 3559beb8beefSJeff Roberson bucket = NULL; 3560beb8beefSJeff Roberson } 3561beb8beefSJeff Roberson out: 35624bd61e19SJeff Roberson if (zone->uz_max_items > 0 && cnt < maxbucket) 35634bd61e19SJeff Roberson zone_free_limit(zone, maxbucket - cnt); 3564fc03d22bSJeff Roberson 3565fc03d22bSJeff Roberson return (bucket); 3566fc03d22bSJeff Roberson } 3567fc03d22bSJeff Roberson 35688355f576SJeff Roberson /* 35690095a784SJeff Roberson * Allocates a single item from a zone. 35708355f576SJeff Roberson * 35718355f576SJeff Roberson * Arguments 35728355f576SJeff Roberson * zone The zone to alloc for. 35738355f576SJeff Roberson * udata The data to be passed to the constructor. 3574ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 3575a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 35768355f576SJeff Roberson * 35778355f576SJeff Roberson * Returns 35788355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 3579bbee39c6SJeff Roberson * An item if successful 35808355f576SJeff Roberson */ 35818355f576SJeff Roberson 35828355f576SJeff Roberson static void * 3583ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 35848355f576SJeff Roberson { 35858355f576SJeff Roberson void *item; 35868355f576SJeff Roberson 35874bd61e19SJeff Roberson if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0) 3588bb15d1c7SGleb Smirnoff return (NULL); 35898355f576SJeff Roberson 3590c1685086SJeff Roberson /* Avoid allocs targeting empty domains. */ 3591c1685086SJeff Roberson if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain)) 359230c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 3593c1685086SJeff Roberson 3594ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 3595beb8beefSJeff Roberson goto fail_cnt; 35968355f576SJeff Roberson 3597099a0e58SBosko Milekic /* 3598099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 3599099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 3600099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 3601099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 3602099a0e58SBosko Milekic */ 3603b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 3604e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 3605bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 3606beb8beefSJeff Roberson goto fail_cnt; 3607beb8beefSJeff Roberson } 3608beb8beefSJeff Roberson } 3609cc7ce83aSJeff Roberson item = item_ctor(zone, zone->uz_size, udata, flags, item); 3610beb8beefSJeff Roberson if (item == NULL) 36110095a784SJeff Roberson goto fail; 36128355f576SJeff Roberson 36132efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 36141431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 36151431a748SGleb Smirnoff zone->uz_name, zone); 36161431a748SGleb Smirnoff 36178355f576SJeff Roberson return (item); 36180095a784SJeff Roberson 3619beb8beefSJeff Roberson fail_cnt: 3620beb8beefSJeff Roberson counter_u64_add(zone->uz_fails, 1); 36210095a784SJeff Roberson fail: 36224bd61e19SJeff Roberson if (zone->uz_max_items > 0) 36234bd61e19SJeff Roberson zone_free_limit(zone, 1); 36241431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 36251431a748SGleb Smirnoff zone->uz_name, zone); 36264bd61e19SJeff Roberson 36270095a784SJeff Roberson return (NULL); 36288355f576SJeff Roberson } 36298355f576SJeff Roberson 36308355f576SJeff Roberson /* See uma.h */ 36318355f576SJeff Roberson void 36328355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 36338355f576SJeff Roberson { 36348355f576SJeff Roberson uma_cache_t cache; 3635376b1ba3SJeff Roberson uma_cache_bucket_t bucket; 3636cc7ce83aSJeff Roberson int domain, itemdomain, uz_flags; 36378355f576SJeff Roberson 3638e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 363919fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 364010cb2424SMark Murray 3641e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone); 36423659f747SRobert Watson 3643d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 36441067a2baSJonathan T. Looney ("uma_zfree_arg: called with spinlock or critical section held")); 36451067a2baSJonathan T. Looney 364620ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 364720ed0cb0SMatthew D Fleming if (item == NULL) 364820ed0cb0SMatthew D Fleming return; 36498d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 36508d689e04SGleb Smirnoff if (is_memguard_addr(item)) { 3651bc9d08e1SMark Johnston if (zone->uz_dtor != NULL) 36528d689e04SGleb Smirnoff zone->uz_dtor(item, zone->uz_size, udata); 3653bc9d08e1SMark Johnston if (zone->uz_fini != NULL) 36548d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 36558d689e04SGleb Smirnoff memguard_free(item); 36568d689e04SGleb Smirnoff return; 36578d689e04SGleb Smirnoff } 36588d689e04SGleb Smirnoff #endif 3659cc7ce83aSJeff Roberson 3660cc7ce83aSJeff Roberson /* 3661cc7ce83aSJeff Roberson * We are accessing the per-cpu cache without a critical section to 3662cc7ce83aSJeff Roberson * fetch size and flags. This is acceptable, if we are preempted we 3663cc7ce83aSJeff Roberson * will simply read another cpu's line. 3664cc7ce83aSJeff Roberson */ 3665cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3666cc7ce83aSJeff Roberson uz_flags = cache_uz_flags(cache); 3667cc7ce83aSJeff Roberson if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0 || 3668cc7ce83aSJeff Roberson UMA_ALWAYS_CTORDTOR)) 3669cc7ce83aSJeff Roberson item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE); 3670ef72505eSJeff Roberson 3671af7f9b97SJeff Roberson /* 3672af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 3673af7f9b97SJeff Roberson * a little longer for the limits to be reset. 3674af7f9b97SJeff Roberson */ 3675cc7ce83aSJeff Roberson if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) { 3676bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0) 3677fc03d22bSJeff Roberson goto zfree_item; 3678cc7ce83aSJeff Roberson } 3679af7f9b97SJeff Roberson 36805d1ae027SRobert Watson /* 36815d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 36825d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 36835d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 36845d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 36855d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 36865d1ae027SRobert Watson * preemption and migration. We release the critical section in 36875d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 36885d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 36895d1ae027SRobert Watson * detect and handle migration if it has occurred. 36905d1ae027SRobert Watson */ 36910a81b439SJeff Roberson domain = itemdomain = 0; 3692dfe13344SJeff Roberson #ifdef NUMA 3693dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 3694dfe13344SJeff Roberson itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 3695dfe13344SJeff Roberson #endif 36965d1ae027SRobert Watson critical_enter(); 36970a81b439SJeff Roberson do { 3698cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3699dfe13344SJeff Roberson #ifdef NUMA 37000a81b439SJeff Roberson domain = PCPU_GET(domain); 3701dfe13344SJeff Roberson if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 && 3702dfe13344SJeff Roberson domain != itemdomain) { 3703376b1ba3SJeff Roberson bucket = &cache->uc_crossbucket; 37040a81b439SJeff Roberson } else 3705c1685086SJeff Roberson #endif 3706dfe13344SJeff Roberson { 3707a553d4b8SJeff Roberson /* 3708dfe13344SJeff Roberson * Try to free into the allocbucket first to give LIFO 3709dfe13344SJeff Roberson * ordering for cache-hot datastructures. Spill over 3710dfe13344SJeff Roberson * into the freebucket if necessary. Alloc will swap 3711dfe13344SJeff Roberson * them if one runs dry. 3712a553d4b8SJeff Roberson */ 3713dfe13344SJeff Roberson bucket = &cache->uc_allocbucket; 3714dfe13344SJeff Roberson if (__predict_false(bucket->ucb_cnt >= 3715dfe13344SJeff Roberson bucket->ucb_entries)) 3716376b1ba3SJeff Roberson bucket = &cache->uc_freebucket; 3717dfe13344SJeff Roberson } 3718376b1ba3SJeff Roberson if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) { 3719376b1ba3SJeff Roberson cache_bucket_push(cache, bucket, item); 37205d1ae027SRobert Watson critical_exit(); 37218355f576SJeff Roberson return; 3722fc03d22bSJeff Roberson } 37230a81b439SJeff Roberson } while (cache_free(zone, cache, udata, item, itemdomain)); 37240a81b439SJeff Roberson critical_exit(); 3725fc03d22bSJeff Roberson 37268355f576SJeff Roberson /* 37270a81b439SJeff Roberson * If nothing else caught this, we'll just do an internal free. 37288355f576SJeff Roberson */ 37290a81b439SJeff Roberson zfree_item: 37300a81b439SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 37310a81b439SJeff Roberson } 3732fc03d22bSJeff Roberson 3733dfe13344SJeff Roberson #ifdef NUMA 373491d947bfSJeff Roberson /* 373591d947bfSJeff Roberson * sort crossdomain free buckets to domain correct buckets and cache 373691d947bfSJeff Roberson * them. 373791d947bfSJeff Roberson */ 373891d947bfSJeff Roberson static void 373991d947bfSJeff Roberson zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata) 374091d947bfSJeff Roberson { 374191d947bfSJeff Roberson struct uma_bucketlist fullbuckets; 374291d947bfSJeff Roberson uma_zone_domain_t zdom; 374391d947bfSJeff Roberson uma_bucket_t b; 374491d947bfSJeff Roberson void *item; 374591d947bfSJeff Roberson int domain; 374691d947bfSJeff Roberson 374791d947bfSJeff Roberson CTR3(KTR_UMA, 374891d947bfSJeff Roberson "uma_zfree: zone %s(%p) draining cross bucket %p", 374991d947bfSJeff Roberson zone->uz_name, zone, bucket); 375091d947bfSJeff Roberson 375191d947bfSJeff Roberson TAILQ_INIT(&fullbuckets); 375291d947bfSJeff Roberson 375391d947bfSJeff Roberson /* 375491d947bfSJeff Roberson * To avoid having ndomain * ndomain buckets for sorting we have a 375591d947bfSJeff Roberson * lock on the current crossfree bucket. A full matrix with 375691d947bfSJeff Roberson * per-domain locking could be used if necessary. 375791d947bfSJeff Roberson */ 375891d947bfSJeff Roberson ZONE_CROSS_LOCK(zone); 375991d947bfSJeff Roberson while (bucket->ub_cnt > 0) { 376091d947bfSJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt - 1]; 376191d947bfSJeff Roberson domain = _vm_phys_domain(pmap_kextract((vm_offset_t)item)); 376291d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 376391d947bfSJeff Roberson if (zdom->uzd_cross == NULL) { 376491d947bfSJeff Roberson zdom->uzd_cross = bucket_alloc(zone, udata, M_NOWAIT); 376591d947bfSJeff Roberson if (zdom->uzd_cross == NULL) 376691d947bfSJeff Roberson break; 376791d947bfSJeff Roberson } 376891d947bfSJeff Roberson zdom->uzd_cross->ub_bucket[zdom->uzd_cross->ub_cnt++] = item; 376991d947bfSJeff Roberson if (zdom->uzd_cross->ub_cnt == zdom->uzd_cross->ub_entries) { 377091d947bfSJeff Roberson TAILQ_INSERT_HEAD(&fullbuckets, zdom->uzd_cross, 377191d947bfSJeff Roberson ub_link); 377291d947bfSJeff Roberson zdom->uzd_cross = NULL; 377391d947bfSJeff Roberson } 377491d947bfSJeff Roberson bucket->ub_cnt--; 377591d947bfSJeff Roberson } 377691d947bfSJeff Roberson ZONE_CROSS_UNLOCK(zone); 377791d947bfSJeff Roberson if (!TAILQ_EMPTY(&fullbuckets)) { 377891d947bfSJeff Roberson ZONE_LOCK(zone); 377991d947bfSJeff Roberson while ((b = TAILQ_FIRST(&fullbuckets)) != NULL) { 378091d947bfSJeff Roberson TAILQ_REMOVE(&fullbuckets, b, ub_link); 378191d947bfSJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 378291d947bfSJeff Roberson ZONE_UNLOCK(zone); 378391d947bfSJeff Roberson bucket_drain(zone, b); 378491d947bfSJeff Roberson bucket_free(zone, b, udata); 378591d947bfSJeff Roberson ZONE_LOCK(zone); 378691d947bfSJeff Roberson } else { 378791d947bfSJeff Roberson domain = _vm_phys_domain( 378891d947bfSJeff Roberson pmap_kextract( 378991d947bfSJeff Roberson (vm_offset_t)b->ub_bucket[0])); 379091d947bfSJeff Roberson zdom = &zone->uz_domain[domain]; 379191d947bfSJeff Roberson zone_put_bucket(zone, zdom, b, true); 379291d947bfSJeff Roberson } 379391d947bfSJeff Roberson } 379491d947bfSJeff Roberson ZONE_UNLOCK(zone); 379591d947bfSJeff Roberson } 379691d947bfSJeff Roberson if (bucket->ub_cnt != 0) 379791d947bfSJeff Roberson bucket_drain(zone, bucket); 379891d947bfSJeff Roberson bucket_free(zone, bucket, udata); 379991d947bfSJeff Roberson } 380091d947bfSJeff Roberson #endif 380191d947bfSJeff Roberson 38020a81b439SJeff Roberson static void 38030a81b439SJeff Roberson zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata, 38040a81b439SJeff Roberson int domain, int itemdomain) 38050a81b439SJeff Roberson { 38060a81b439SJeff Roberson uma_zone_domain_t zdom; 38070a81b439SJeff Roberson 3808dfe13344SJeff Roberson #ifdef NUMA 38090a81b439SJeff Roberson /* 38100a81b439SJeff Roberson * Buckets coming from the wrong domain will be entirely for the 38110a81b439SJeff Roberson * only other domain on two domain systems. In this case we can 38120a81b439SJeff Roberson * simply cache them. Otherwise we need to sort them back to 381391d947bfSJeff Roberson * correct domains. 38140a81b439SJeff Roberson */ 38150a81b439SJeff Roberson if (domain != itemdomain && vm_ndomains > 2) { 381691d947bfSJeff Roberson zone_free_cross(zone, bucket, udata); 38170a81b439SJeff Roberson return; 38180a81b439SJeff Roberson } 38190a81b439SJeff Roberson #endif 382091d947bfSJeff Roberson 38210a81b439SJeff Roberson /* 38220a81b439SJeff Roberson * Attempt to save the bucket in the zone's domain bucket cache. 38230a81b439SJeff Roberson * 38240a81b439SJeff Roberson * We bump the uz count when the cache size is insufficient to 38250a81b439SJeff Roberson * handle the working set. 38260a81b439SJeff Roberson */ 38274d104ba0SAlexander Motin if (ZONE_TRYLOCK(zone) == 0) { 38284d104ba0SAlexander Motin /* Record contention to size the buckets. */ 38298355f576SJeff Roberson ZONE_LOCK(zone); 383020a4e154SJeff Roberson if (zone->uz_bucket_size < zone->uz_bucket_size_max) 383120a4e154SJeff Roberson zone->uz_bucket_size++; 38324d104ba0SAlexander Motin } 38338355f576SJeff Roberson 38340a81b439SJeff Roberson CTR3(KTR_UMA, 38350a81b439SJeff Roberson "uma_zfree: zone %s(%p) putting bucket %p on free list", 38360a81b439SJeff Roberson zone->uz_name, zone, bucket); 38370a81b439SJeff Roberson /* ub_cnt is pointing to the last free item */ 38380a81b439SJeff Roberson KASSERT(bucket->ub_cnt == bucket->ub_entries, 38390a81b439SJeff Roberson ("uma_zfree: Attempting to insert partial bucket onto the full list.\n")); 38400a81b439SJeff Roberson if (zone->uz_bkt_count >= zone->uz_bkt_max) { 3841c1685086SJeff Roberson ZONE_UNLOCK(zone); 3842c1685086SJeff Roberson bucket_drain(zone, bucket); 3843c1685086SJeff Roberson bucket_free(zone, bucket, udata); 3844c1685086SJeff Roberson } else { 3845c1685086SJeff Roberson zdom = &zone->uz_domain[itemdomain]; 3846c1685086SJeff Roberson zone_put_bucket(zone, zdom, bucket, true); 3847c1685086SJeff Roberson ZONE_UNLOCK(zone); 3848c1685086SJeff Roberson } 38498355f576SJeff Roberson } 3850fc03d22bSJeff Roberson 38514d104ba0SAlexander Motin /* 38520a81b439SJeff Roberson * Populate a free or cross bucket for the current cpu cache. Free any 38530a81b439SJeff Roberson * existing full bucket either to the zone cache or back to the slab layer. 38540a81b439SJeff Roberson * 38550a81b439SJeff Roberson * Enters and returns in a critical section. false return indicates that 38560a81b439SJeff Roberson * we can not satisfy this free in the cache layer. true indicates that 38570a81b439SJeff Roberson * the caller should retry. 38584d104ba0SAlexander Motin */ 38590a81b439SJeff Roberson static __noinline bool 38600a81b439SJeff Roberson cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item, 38610a81b439SJeff Roberson int itemdomain) 38620a81b439SJeff Roberson { 3863dfe13344SJeff Roberson uma_cache_bucket_t cbucket; 38640a81b439SJeff Roberson uma_bucket_t bucket; 3865cc7ce83aSJeff Roberson int domain; 38660a81b439SJeff Roberson 38670a81b439SJeff Roberson CRITICAL_ASSERT(curthread); 38680a81b439SJeff Roberson 386920a4e154SJeff Roberson if (zone->uz_bucket_size == 0 || bucketdisable) 38700a81b439SJeff Roberson return false; 38710a81b439SJeff Roberson 3872cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 38730a81b439SJeff Roberson 38740a81b439SJeff Roberson /* 3875dfe13344SJeff Roberson * FIRSTTOUCH domains need to free to the correct zdom. When 3876dfe13344SJeff Roberson * enabled this is the zdom of the item. The bucket is the 3877dfe13344SJeff Roberson * cross bucket if the current domain and itemdomain do not match. 38780a81b439SJeff Roberson */ 3879dfe13344SJeff Roberson cbucket = &cache->uc_freebucket; 3880dfe13344SJeff Roberson #ifdef NUMA 3881dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 38820a81b439SJeff Roberson domain = PCPU_GET(domain); 38830a81b439SJeff Roberson if (domain != itemdomain) { 3884dfe13344SJeff Roberson cbucket = &cache->uc_crossbucket; 3885dfe13344SJeff Roberson if (cbucket->ucb_cnt != 0) 3886dfe13344SJeff Roberson atomic_add_64(&zone->uz_xdomain, 3887dfe13344SJeff Roberson cbucket->ucb_cnt); 3888dfe13344SJeff Roberson } 38890a81b439SJeff Roberson } else 38900a81b439SJeff Roberson #endif 3891dfe13344SJeff Roberson itemdomain = domain = 0; 3892dfe13344SJeff Roberson bucket = cache_bucket_unload(cbucket); 38930a81b439SJeff Roberson 38940a81b439SJeff Roberson /* We are no longer associated with this CPU. */ 38950a81b439SJeff Roberson critical_exit(); 38960a81b439SJeff Roberson 38970a81b439SJeff Roberson if (bucket != NULL) 38980a81b439SJeff Roberson zone_free_bucket(zone, bucket, udata, domain, itemdomain); 3899a553d4b8SJeff Roberson 39006fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT); 39011431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p", 39021431a748SGleb Smirnoff zone->uz_name, zone, bucket); 3903fc03d22bSJeff Roberson critical_enter(); 39040a81b439SJeff Roberson if (bucket == NULL) 39050a81b439SJeff Roberson return (false); 3906cc7ce83aSJeff Roberson cache = &zone->uz_cpu[curcpu]; 3907dfe13344SJeff Roberson #ifdef NUMA 3908fc03d22bSJeff Roberson /* 39090a81b439SJeff Roberson * Check to see if we should be populating the cross bucket. If it 39100a81b439SJeff Roberson * is already populated we will fall through and attempt to populate 39110a81b439SJeff Roberson * the free bucket. 3912fc03d22bSJeff Roberson */ 3913dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) { 39140a81b439SJeff Roberson domain = PCPU_GET(domain); 3915376b1ba3SJeff Roberson if (domain != itemdomain && 3916376b1ba3SJeff Roberson cache->uc_crossbucket.ucb_bucket == NULL) { 3917376b1ba3SJeff Roberson cache_bucket_load_cross(cache, bucket); 39180a81b439SJeff Roberson return (true); 39190a81b439SJeff Roberson } 39200a81b439SJeff Roberson } 39210a81b439SJeff Roberson #endif 39220a81b439SJeff Roberson /* 39230a81b439SJeff Roberson * We may have lost the race to fill the bucket or switched CPUs. 39240a81b439SJeff Roberson */ 3925376b1ba3SJeff Roberson if (cache->uc_freebucket.ucb_bucket != NULL) { 3926fc03d22bSJeff Roberson critical_exit(); 39276fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 39280a81b439SJeff Roberson critical_enter(); 39290a81b439SJeff Roberson } else 3930376b1ba3SJeff Roberson cache_bucket_load_free(cache, bucket); 39318355f576SJeff Roberson 39320a81b439SJeff Roberson return (true); 39338355f576SJeff Roberson } 39348355f576SJeff Roberson 3935ab3185d1SJeff Roberson void 3936ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata) 3937ab3185d1SJeff Roberson { 3938ab3185d1SJeff Roberson 3939ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 394019fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3941ab3185d1SJeff Roberson 3942e63a1c2fSRyan Libby CTR2(KTR_UMA, "uma_zfree_domain zone %s(%p)", zone->uz_name, zone); 3943ab3185d1SJeff Roberson 3944ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3945ab3185d1SJeff Roberson ("uma_zfree_domain: called with spinlock or critical section held")); 3946ab3185d1SJeff Roberson 3947ab3185d1SJeff Roberson /* uma_zfree(..., NULL) does nothing, to match free(9). */ 3948ab3185d1SJeff Roberson if (item == NULL) 3949ab3185d1SJeff Roberson return; 3950ab3185d1SJeff Roberson zone_free_item(zone, item, udata, SKIP_NONE); 3951ab3185d1SJeff Roberson } 3952ab3185d1SJeff Roberson 39538355f576SJeff Roberson static void 3954bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 39558355f576SJeff Roberson { 3956bb15d1c7SGleb Smirnoff uma_keg_t keg; 3957ab3185d1SJeff Roberson uma_domain_t dom; 39589b8db4d0SRyan Libby int freei; 3959099a0e58SBosko Milekic 3960bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 39618b987a77SJeff Roberson KEG_LOCK_ASSERT(keg, slab->us_domain); 3962ab3185d1SJeff Roberson 39638355f576SJeff Roberson /* Do we need to remove from any lists? */ 39648b987a77SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3965099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 39668355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3967ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 39688355f576SJeff Roberson } else if (slab->us_freecount == 0) { 39698355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3970ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 39718355f576SJeff Roberson } 39728355f576SJeff Roberson 3973ef72505eSJeff Roberson /* Slab management. */ 39741e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 39759b78b1f4SJeff Roberson BIT_SET(keg->uk_ipers, freei, &slab->us_free); 39768355f576SJeff Roberson slab->us_freecount++; 39778355f576SJeff Roberson 3978ef72505eSJeff Roberson /* Keg statistics. */ 39798b987a77SJeff Roberson dom->ud_free++; 39800095a784SJeff Roberson } 39810095a784SJeff Roberson 39820095a784SJeff Roberson static void 3983b75c4efcSAndrew Turner zone_release(void *arg, void **bucket, int cnt) 39840095a784SJeff Roberson { 39858b987a77SJeff Roberson struct mtx *lock; 3986b75c4efcSAndrew Turner uma_zone_t zone; 39870095a784SJeff Roberson uma_slab_t slab; 39880095a784SJeff Roberson uma_keg_t keg; 39890095a784SJeff Roberson uint8_t *mem; 39908b987a77SJeff Roberson void *item; 39910095a784SJeff Roberson int i; 39928355f576SJeff Roberson 3993b75c4efcSAndrew Turner zone = arg; 3994bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 39958b987a77SJeff Roberson lock = NULL; 399654c5ae80SRyan Libby if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0)) 39978b987a77SJeff Roberson lock = KEG_LOCK(keg, 0); 39980095a784SJeff Roberson for (i = 0; i < cnt; i++) { 39990095a784SJeff Roberson item = bucket[i]; 400054c5ae80SRyan Libby if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) { 40010095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 40028b987a77SJeff Roberson } else { 40038b987a77SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 400454c5ae80SRyan Libby if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0) 40058b987a77SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 40068b987a77SJeff Roberson else 40078b987a77SJeff Roberson slab = (uma_slab_t)(mem + keg->uk_pgoff); 40088b987a77SJeff Roberson } 40098b987a77SJeff Roberson if (lock != KEG_LOCKPTR(keg, slab->us_domain)) { 40108b987a77SJeff Roberson if (lock != NULL) 40118b987a77SJeff Roberson mtx_unlock(lock); 40128b987a77SJeff Roberson lock = KEG_LOCK(keg, slab->us_domain); 40138b987a77SJeff Roberson } 4014bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 40150095a784SJeff Roberson } 40168b987a77SJeff Roberson if (lock != NULL) 40178b987a77SJeff Roberson mtx_unlock(lock); 40188355f576SJeff Roberson } 40198355f576SJeff Roberson 40200095a784SJeff Roberson /* 40210095a784SJeff Roberson * Frees a single item to any zone. 40220095a784SJeff Roberson * 40230095a784SJeff Roberson * Arguments: 40240095a784SJeff Roberson * zone The zone to free to 40250095a784SJeff Roberson * item The item we're freeing 40260095a784SJeff Roberson * udata User supplied data for the dtor 40270095a784SJeff Roberson * skip Skip dtors and finis 40280095a784SJeff Roberson */ 40290095a784SJeff Roberson static void 40300095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 40310095a784SJeff Roberson { 4032c5deaf04SGleb Smirnoff 4033cc7ce83aSJeff Roberson item_dtor(zone, item, zone->uz_size, udata, skip); 40340095a784SJeff Roberson 40350095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 40360095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 40370095a784SJeff Roberson 40380095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 4039bb15d1c7SGleb Smirnoff 4040bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 4041bb15d1c7SGleb Smirnoff return; 4042bb15d1c7SGleb Smirnoff 40432efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 40442efcc8cbSGleb Smirnoff 40454bd61e19SJeff Roberson if (zone->uz_max_items > 0) 40464bd61e19SJeff Roberson zone_free_limit(zone, 1); 4047bb45b411SGleb Smirnoff } 40480095a784SJeff Roberson 40498355f576SJeff Roberson /* See uma.h */ 40501c6cae97SLawrence Stewart int 4051736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 4052736ee590SJeff Roberson { 4053bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 4054003cf08bSMark Johnston int count; 4055bb15d1c7SGleb Smirnoff 40564bd61e19SJeff Roberson /* 40574bd61e19SJeff Roberson * XXX This can misbehave if the zone has any allocations with 40584bd61e19SJeff Roberson * no limit and a limit is imposed. There is currently no 40594bd61e19SJeff Roberson * way to clear a limit. 40604bd61e19SJeff Roberson */ 4061bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4062003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4063003cf08bSMark Johnston count = ubz != NULL ? ubz->ubz_entries : 0; 406420a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = count; 406520a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 406620a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4067bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 4068cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT; 4069cc7ce83aSJeff Roberson zone_update_caches(zone); 40704bd61e19SJeff Roberson /* We may need to wake waiters. */ 40714bd61e19SJeff Roberson wakeup(&zone->uz_max_items); 4072bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4073bb15d1c7SGleb Smirnoff 4074bb15d1c7SGleb Smirnoff return (nitems); 4075bb15d1c7SGleb Smirnoff } 4076bb15d1c7SGleb Smirnoff 4077bb15d1c7SGleb Smirnoff /* See uma.h */ 4078003cf08bSMark Johnston void 4079bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 4080bb15d1c7SGleb Smirnoff { 4081003cf08bSMark Johnston struct uma_bucket_zone *ubz; 4082003cf08bSMark Johnston int bpcpu; 4083bb15d1c7SGleb Smirnoff 4084bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4085003cf08bSMark Johnston ubz = bucket_zone_max(zone, nitems); 4086003cf08bSMark Johnston if (ubz != NULL) { 4087003cf08bSMark Johnston bpcpu = 2; 4088dfe13344SJeff Roberson if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0) 4089003cf08bSMark Johnston /* Count the cross-domain bucket. */ 4090003cf08bSMark Johnston bpcpu++; 4091003cf08bSMark Johnston nitems -= ubz->ubz_entries * bpcpu * mp_ncpus; 409220a4e154SJeff Roberson zone->uz_bucket_size_max = ubz->ubz_entries; 4093003cf08bSMark Johnston } else { 409420a4e154SJeff Roberson zone->uz_bucket_size_max = zone->uz_bucket_size = 0; 4095003cf08bSMark Johnston } 409620a4e154SJeff Roberson if (zone->uz_bucket_size_min > zone->uz_bucket_size_max) 409720a4e154SJeff Roberson zone->uz_bucket_size_min = zone->uz_bucket_size_max; 4098bb15d1c7SGleb Smirnoff zone->uz_bkt_max = nitems; 4099bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4100736ee590SJeff Roberson } 4101736ee590SJeff Roberson 4102736ee590SJeff Roberson /* See uma.h */ 4103e49471b0SAndre Oppermann int 4104e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 4105e49471b0SAndre Oppermann { 4106e49471b0SAndre Oppermann int nitems; 4107e49471b0SAndre Oppermann 4108727c6918SJeff Roberson nitems = atomic_load_64(&zone->uz_max_items); 4109e49471b0SAndre Oppermann 4110e49471b0SAndre Oppermann return (nitems); 4111e49471b0SAndre Oppermann } 4112e49471b0SAndre Oppermann 4113e49471b0SAndre Oppermann /* See uma.h */ 41142f891cd5SPawel Jakub Dawidek void 41152f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 41162f891cd5SPawel Jakub Dawidek { 41172f891cd5SPawel Jakub Dawidek 4118727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 41192f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 41202f891cd5SPawel Jakub Dawidek } 41212f891cd5SPawel Jakub Dawidek 41222f891cd5SPawel Jakub Dawidek /* See uma.h */ 412354503a13SJonathan T. Looney void 412454503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 412554503a13SJonathan T. Looney { 412654503a13SJonathan T. Looney 4127727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4128e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 412954503a13SJonathan T. Looney } 413054503a13SJonathan T. Looney 413154503a13SJonathan T. Looney /* See uma.h */ 4132c4ae7908SLawrence Stewart int 4133c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 4134c4ae7908SLawrence Stewart { 4135c4ae7908SLawrence Stewart int64_t nitems; 4136c4ae7908SLawrence Stewart u_int i; 4137c4ae7908SLawrence Stewart 4138bfb6b7a1SJeff Roberson nitems = 0; 4139bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER) 41402efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 41412efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 4142727c6918SJeff Roberson CPU_FOREACH(i) 4143727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) - 4144727c6918SJeff Roberson atomic_load_64(&zone->uz_cpu[i].uc_frees); 4145c4ae7908SLawrence Stewart 4146c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 4147c4ae7908SLawrence Stewart } 4148c4ae7908SLawrence Stewart 414920a4e154SJeff Roberson static uint64_t 415020a4e154SJeff Roberson uma_zone_get_allocs(uma_zone_t zone) 415120a4e154SJeff Roberson { 415220a4e154SJeff Roberson uint64_t nitems; 415320a4e154SJeff Roberson u_int i; 415420a4e154SJeff Roberson 4155bfb6b7a1SJeff Roberson nitems = 0; 4156bfb6b7a1SJeff Roberson if (zone->uz_allocs != EARLY_COUNTER) 415720a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_allocs); 4158727c6918SJeff Roberson CPU_FOREACH(i) 4159727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs); 416020a4e154SJeff Roberson 416120a4e154SJeff Roberson return (nitems); 416220a4e154SJeff Roberson } 416320a4e154SJeff Roberson 416420a4e154SJeff Roberson static uint64_t 416520a4e154SJeff Roberson uma_zone_get_frees(uma_zone_t zone) 416620a4e154SJeff Roberson { 416720a4e154SJeff Roberson uint64_t nitems; 416820a4e154SJeff Roberson u_int i; 416920a4e154SJeff Roberson 4170bfb6b7a1SJeff Roberson nitems = 0; 4171bfb6b7a1SJeff Roberson if (zone->uz_frees != EARLY_COUNTER) 417220a4e154SJeff Roberson nitems = counter_u64_fetch(zone->uz_frees); 4173727c6918SJeff Roberson CPU_FOREACH(i) 4174727c6918SJeff Roberson nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees); 417520a4e154SJeff Roberson 417620a4e154SJeff Roberson return (nitems); 417720a4e154SJeff Roberson } 417820a4e154SJeff Roberson 417931c251a0SJeff Roberson #ifdef INVARIANTS 418031c251a0SJeff Roberson /* Used only for KEG_ASSERT_COLD(). */ 418131c251a0SJeff Roberson static uint64_t 418231c251a0SJeff Roberson uma_keg_get_allocs(uma_keg_t keg) 418331c251a0SJeff Roberson { 418431c251a0SJeff Roberson uma_zone_t z; 418531c251a0SJeff Roberson uint64_t nitems; 418631c251a0SJeff Roberson 418731c251a0SJeff Roberson nitems = 0; 418831c251a0SJeff Roberson LIST_FOREACH(z, &keg->uk_zones, uz_link) 418931c251a0SJeff Roberson nitems += uma_zone_get_allocs(z); 419031c251a0SJeff Roberson 419131c251a0SJeff Roberson return (nitems); 419231c251a0SJeff Roberson } 419331c251a0SJeff Roberson #endif 419431c251a0SJeff Roberson 4195c4ae7908SLawrence Stewart /* See uma.h */ 4196736ee590SJeff Roberson void 4197099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 4198099a0e58SBosko Milekic { 4199e20a199fSJeff Roberson uma_keg_t keg; 4200e20a199fSJeff Roberson 4201bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4202727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4203e20a199fSJeff Roberson keg->uk_init = uminit; 4204099a0e58SBosko Milekic } 4205099a0e58SBosko Milekic 4206099a0e58SBosko Milekic /* See uma.h */ 4207099a0e58SBosko Milekic void 4208099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 4209099a0e58SBosko Milekic { 4210e20a199fSJeff Roberson uma_keg_t keg; 4211e20a199fSJeff Roberson 4212bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4213727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4214e20a199fSJeff Roberson keg->uk_fini = fini; 4215099a0e58SBosko Milekic } 4216099a0e58SBosko Milekic 4217099a0e58SBosko Milekic /* See uma.h */ 4218099a0e58SBosko Milekic void 4219099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 4220099a0e58SBosko Milekic { 4221af526374SJeff Roberson 4222727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4223099a0e58SBosko Milekic zone->uz_init = zinit; 4224099a0e58SBosko Milekic } 4225099a0e58SBosko Milekic 4226099a0e58SBosko Milekic /* See uma.h */ 4227099a0e58SBosko Milekic void 4228099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 4229099a0e58SBosko Milekic { 4230af526374SJeff Roberson 4231727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 4232099a0e58SBosko Milekic zone->uz_fini = zfini; 4233099a0e58SBosko Milekic } 4234099a0e58SBosko Milekic 4235099a0e58SBosko Milekic /* See uma.h */ 4236099a0e58SBosko Milekic void 42378355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 42388355f576SJeff Roberson { 42390095a784SJeff Roberson uma_keg_t keg; 4240e20a199fSJeff Roberson 4241bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4242727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 42430095a784SJeff Roberson keg->uk_freef = freef; 42448355f576SJeff Roberson } 42458355f576SJeff Roberson 42468355f576SJeff Roberson /* See uma.h */ 42478355f576SJeff Roberson void 42488355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 42498355f576SJeff Roberson { 4250e20a199fSJeff Roberson uma_keg_t keg; 4251e20a199fSJeff Roberson 4252bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4253727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4254e20a199fSJeff Roberson keg->uk_allocf = allocf; 42558355f576SJeff Roberson } 42568355f576SJeff Roberson 42578355f576SJeff Roberson /* See uma.h */ 42586fd34d6fSJeff Roberson void 42596fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 42606fd34d6fSJeff Roberson { 42616fd34d6fSJeff Roberson uma_keg_t keg; 42626fd34d6fSJeff Roberson 4263bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4264727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 42656fd34d6fSJeff Roberson keg->uk_reserve = items; 42666fd34d6fSJeff Roberson } 42676fd34d6fSJeff Roberson 42686fd34d6fSJeff Roberson /* See uma.h */ 42698355f576SJeff Roberson int 4270a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 42718355f576SJeff Roberson { 4272099a0e58SBosko Milekic uma_keg_t keg; 42738355f576SJeff Roberson vm_offset_t kva; 42749ba30bcbSZbigniew Bodek u_int pages; 42758355f576SJeff Roberson 4276bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 4277727c6918SJeff Roberson KEG_ASSERT_COLD(keg); 4278727c6918SJeff Roberson ZONE_ASSERT_COLD(zone); 42798355f576SJeff Roberson 428079c9f942SJeff Roberson pages = howmany(count, keg->uk_ipers) * keg->uk_ppera; 4281a553d4b8SJeff Roberson 4282a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4283a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 4284a4915c21SAttilio Rao #else 4285a4915c21SAttilio Rao if (1) { 4286a4915c21SAttilio Rao #endif 428757223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 4288d1f42ac2SAlan Cox if (kva == 0) 42898355f576SJeff Roberson return (0); 4290a4915c21SAttilio Rao } else 4291a4915c21SAttilio Rao kva = 0; 4292bb15d1c7SGleb Smirnoff 4293bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 4294bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 4295099a0e58SBosko Milekic keg->uk_kva = kva; 4296a4915c21SAttilio Rao keg->uk_offset = 0; 4297bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 4298a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 4299a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 4300a4915c21SAttilio Rao #else 4301a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 4302a4915c21SAttilio Rao #endif 4303cc7ce83aSJeff Roberson keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4304cc7ce83aSJeff Roberson zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE; 4305cc7ce83aSJeff Roberson zone_update_caches(zone); 4306bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 4307af526374SJeff Roberson 43088355f576SJeff Roberson return (1); 43098355f576SJeff Roberson } 43108355f576SJeff Roberson 43118355f576SJeff Roberson /* See uma.h */ 43128355f576SJeff Roberson void 43138355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 43148355f576SJeff Roberson { 4315920239efSMark Johnston struct vm_domainset_iter di; 4316ab3185d1SJeff Roberson uma_domain_t dom; 43178355f576SJeff Roberson uma_slab_t slab; 4318099a0e58SBosko Milekic uma_keg_t keg; 431986220393SMark Johnston int aflags, domain, slabs; 43208355f576SJeff Roberson 4321bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 432279c9f942SJeff Roberson slabs = howmany(items, keg->uk_ipers); 4323194a979eSMark Johnston while (slabs-- > 0) { 432486220393SMark Johnston aflags = M_NOWAIT; 432586220393SMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 432686220393SMark Johnston &aflags); 432786220393SMark Johnston for (;;) { 432886220393SMark Johnston slab = keg_alloc_slab(keg, zone, domain, M_WAITOK, 432986220393SMark Johnston aflags); 433086220393SMark Johnston if (slab != NULL) { 4331ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 43328b987a77SJeff Roberson LIST_REMOVE(slab, us_link); 433386220393SMark Johnston LIST_INSERT_HEAD(&dom->ud_free_slab, slab, 433486220393SMark Johnston us_link); 43358b987a77SJeff Roberson KEG_UNLOCK(keg, slab->us_domain); 4336920239efSMark Johnston break; 43378355f576SJeff Roberson } 43388b987a77SJeff Roberson if (vm_domainset_iter_policy(&di, &domain) != 0) 433986220393SMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 434086220393SMark Johnston } 434186220393SMark Johnston } 434286220393SMark Johnston } 43438355f576SJeff Roberson 43448355f576SJeff Roberson /* See uma.h */ 434508cfa56eSMark Johnston void 434608cfa56eSMark Johnston uma_reclaim(int req) 43478355f576SJeff Roberson { 434844ec2b63SKonstantin Belousov 43491431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 435008cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 435186bbae32SJeff Roberson bucket_enable(); 435208cfa56eSMark Johnston 435308cfa56eSMark Johnston switch (req) { 435408cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 435520a4e154SJeff Roberson zone_foreach(zone_trim, NULL); 435608cfa56eSMark Johnston break; 435708cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 435808cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 435920a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 436008cfa56eSMark Johnston if (req == UMA_RECLAIM_DRAIN_CPU) { 436108cfa56eSMark Johnston pcpu_cache_drain_safe(NULL); 436220a4e154SJeff Roberson zone_foreach(zone_drain, NULL); 4363a2de44abSAlexander Motin } 436408cfa56eSMark Johnston break; 436508cfa56eSMark Johnston default: 436608cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 436708cfa56eSMark Johnston } 43680f9b7bf3SMark Johnston 43698355f576SJeff Roberson /* 43708355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 43718355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 43728355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 43738355f576SJeff Roberson */ 43749b8db4d0SRyan Libby zone_drain(slabzones[0], NULL); 43759b8db4d0SRyan Libby zone_drain(slabzones[1], NULL); 4376cae33c14SJeff Roberson bucket_zone_drain(); 437708cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 43788355f576SJeff Roberson } 43798355f576SJeff Roberson 43802e47807cSJeff Roberson static volatile int uma_reclaim_needed; 438144ec2b63SKonstantin Belousov 438244ec2b63SKonstantin Belousov void 438344ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 438444ec2b63SKonstantin Belousov { 438544ec2b63SKonstantin Belousov 43862e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 43872e47807cSJeff Roberson wakeup(uma_reclaim); 438844ec2b63SKonstantin Belousov } 438944ec2b63SKonstantin Belousov 439044ec2b63SKonstantin Belousov void 439144ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 439244ec2b63SKonstantin Belousov { 439344ec2b63SKonstantin Belousov 439444ec2b63SKonstantin Belousov for (;;) { 439508cfa56eSMark Johnston sx_xlock(&uma_reclaim_lock); 4396200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 439708cfa56eSMark Johnston sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl", 43982e47807cSJeff Roberson hz); 439908cfa56eSMark Johnston sx_xunlock(&uma_reclaim_lock); 44009b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 440108cfa56eSMark Johnston uma_reclaim(UMA_RECLAIM_DRAIN_CPU); 4402200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 44032e47807cSJeff Roberson /* Don't fire more than once per-second. */ 44042e47807cSJeff Roberson pause("umarclslp", hz); 440544ec2b63SKonstantin Belousov } 440644ec2b63SKonstantin Belousov } 440744ec2b63SKonstantin Belousov 4408663b416fSJohn Baldwin /* See uma.h */ 440908cfa56eSMark Johnston void 441008cfa56eSMark Johnston uma_zone_reclaim(uma_zone_t zone, int req) 441108cfa56eSMark Johnston { 441208cfa56eSMark Johnston 441308cfa56eSMark Johnston switch (req) { 441408cfa56eSMark Johnston case UMA_RECLAIM_TRIM: 441520a4e154SJeff Roberson zone_trim(zone, NULL); 441608cfa56eSMark Johnston break; 441708cfa56eSMark Johnston case UMA_RECLAIM_DRAIN: 441820a4e154SJeff Roberson zone_drain(zone, NULL); 441908cfa56eSMark Johnston break; 442008cfa56eSMark Johnston case UMA_RECLAIM_DRAIN_CPU: 442108cfa56eSMark Johnston pcpu_cache_drain_safe(zone); 442220a4e154SJeff Roberson zone_drain(zone, NULL); 442308cfa56eSMark Johnston break; 442408cfa56eSMark Johnston default: 442508cfa56eSMark Johnston panic("unhandled reclamation request %d", req); 442608cfa56eSMark Johnston } 442708cfa56eSMark Johnston } 442808cfa56eSMark Johnston 442908cfa56eSMark Johnston /* See uma.h */ 4430663b416fSJohn Baldwin int 4431663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 4432663b416fSJohn Baldwin { 4433663b416fSJohn Baldwin 4434727c6918SJeff Roberson return (atomic_load_32(&zone->uz_sleepers) > 0); 44356c125b8dSMohan Srinivasan } 44366c125b8dSMohan Srinivasan 44372e47807cSJeff Roberson unsigned long 44382e47807cSJeff Roberson uma_limit(void) 44392e47807cSJeff Roberson { 44402e47807cSJeff Roberson 44412e47807cSJeff Roberson return (uma_kmem_limit); 44422e47807cSJeff Roberson } 44432e47807cSJeff Roberson 44442e47807cSJeff Roberson void 44452e47807cSJeff Roberson uma_set_limit(unsigned long limit) 44462e47807cSJeff Roberson { 44472e47807cSJeff Roberson 44482e47807cSJeff Roberson uma_kmem_limit = limit; 44492e47807cSJeff Roberson } 44502e47807cSJeff Roberson 44512e47807cSJeff Roberson unsigned long 44522e47807cSJeff Roberson uma_size(void) 44532e47807cSJeff Roberson { 44542e47807cSJeff Roberson 4455058f0f74SMark Johnston return (atomic_load_long(&uma_kmem_total)); 4456ad5b0f5bSJeff Roberson } 4457ad5b0f5bSJeff Roberson 4458ad5b0f5bSJeff Roberson long 4459ad5b0f5bSJeff Roberson uma_avail(void) 4460ad5b0f5bSJeff Roberson { 4461ad5b0f5bSJeff Roberson 4462058f0f74SMark Johnston return (uma_kmem_limit - uma_size()); 44632e47807cSJeff Roberson } 44642e47807cSJeff Roberson 4465a0d4b0aeSRobert Watson #ifdef DDB 44668355f576SJeff Roberson /* 44677a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 44687a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 44697a52a97eSRobert Watson * 44707a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 44717a52a97eSRobert Watson * per-CPU cache statistic. 44727a52a97eSRobert Watson * 44737a52a97eSRobert Watson */ 44747a52a97eSRobert Watson static void 44750f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 4476c1685086SJeff Roberson uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp) 44777a52a97eSRobert Watson { 44787a52a97eSRobert Watson uma_cache_t cache; 4479c1685086SJeff Roberson uint64_t allocs, frees, sleeps, xdomain; 44807a52a97eSRobert Watson int cachefree, cpu; 44817a52a97eSRobert Watson 4482c1685086SJeff Roberson allocs = frees = sleeps = xdomain = 0; 44837a52a97eSRobert Watson cachefree = 0; 44843aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 44857a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 4486376b1ba3SJeff Roberson cachefree += cache->uc_allocbucket.ucb_cnt; 4487376b1ba3SJeff Roberson cachefree += cache->uc_freebucket.ucb_cnt; 4488376b1ba3SJeff Roberson xdomain += cache->uc_crossbucket.ucb_cnt; 4489376b1ba3SJeff Roberson cachefree += cache->uc_crossbucket.ucb_cnt; 44907a52a97eSRobert Watson allocs += cache->uc_allocs; 44917a52a97eSRobert Watson frees += cache->uc_frees; 44927a52a97eSRobert Watson } 44932efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 44942efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 4495bf965959SSean Bruno sleeps += z->uz_sleeps; 4496c1685086SJeff Roberson xdomain += z->uz_xdomain; 44977a52a97eSRobert Watson if (cachefreep != NULL) 44987a52a97eSRobert Watson *cachefreep = cachefree; 44997a52a97eSRobert Watson if (allocsp != NULL) 45007a52a97eSRobert Watson *allocsp = allocs; 45017a52a97eSRobert Watson if (freesp != NULL) 45027a52a97eSRobert Watson *freesp = frees; 4503bf965959SSean Bruno if (sleepsp != NULL) 4504bf965959SSean Bruno *sleepsp = sleeps; 4505c1685086SJeff Roberson if (xdomainp != NULL) 4506c1685086SJeff Roberson *xdomainp = xdomain; 45077a52a97eSRobert Watson } 4508a0d4b0aeSRobert Watson #endif /* DDB */ 45097a52a97eSRobert Watson 45107a52a97eSRobert Watson static int 45117a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 45127a52a97eSRobert Watson { 45137a52a97eSRobert Watson uma_keg_t kz; 45147a52a97eSRobert Watson uma_zone_t z; 45157a52a97eSRobert Watson int count; 45167a52a97eSRobert Watson 45177a52a97eSRobert Watson count = 0; 4518111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 45197a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 45207a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 45217a52a97eSRobert Watson count++; 45227a52a97eSRobert Watson } 4523b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4524b47acb0aSGleb Smirnoff count++; 4525b47acb0aSGleb Smirnoff 4526111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 45277a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 45287a52a97eSRobert Watson } 45297a52a97eSRobert Watson 4530b47acb0aSGleb Smirnoff static void 4531b47acb0aSGleb Smirnoff uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf, 4532b47acb0aSGleb Smirnoff struct uma_percpu_stat *ups, bool internal) 4533b47acb0aSGleb Smirnoff { 4534b47acb0aSGleb Smirnoff uma_zone_domain_t zdom; 4535b47acb0aSGleb Smirnoff uma_cache_t cache; 4536b47acb0aSGleb Smirnoff int i; 4537b47acb0aSGleb Smirnoff 4538b47acb0aSGleb Smirnoff 4539b47acb0aSGleb Smirnoff for (i = 0; i < vm_ndomains; i++) { 4540b47acb0aSGleb Smirnoff zdom = &z->uz_domain[i]; 4541b47acb0aSGleb Smirnoff uth->uth_zone_free += zdom->uzd_nitems; 4542b47acb0aSGleb Smirnoff } 4543b47acb0aSGleb Smirnoff uth->uth_allocs = counter_u64_fetch(z->uz_allocs); 4544b47acb0aSGleb Smirnoff uth->uth_frees = counter_u64_fetch(z->uz_frees); 4545b47acb0aSGleb Smirnoff uth->uth_fails = counter_u64_fetch(z->uz_fails); 4546b47acb0aSGleb Smirnoff uth->uth_sleeps = z->uz_sleeps; 4547c1685086SJeff Roberson uth->uth_xdomain = z->uz_xdomain; 45481de9724eSMark Johnston 4549b47acb0aSGleb Smirnoff /* 45501de9724eSMark Johnston * While it is not normally safe to access the cache bucket pointers 45511de9724eSMark Johnston * while not on the CPU that owns the cache, we only allow the pointers 45521de9724eSMark Johnston * to be exchanged without the zone lock held, not invalidated, so 45531de9724eSMark Johnston * accept the possible race associated with bucket exchange during 45541de9724eSMark Johnston * monitoring. Use atomic_load_ptr() to ensure that the bucket pointers 45551de9724eSMark Johnston * are loaded only once. 4556b47acb0aSGleb Smirnoff */ 4557b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) { 4558b47acb0aSGleb Smirnoff bzero(&ups[i], sizeof(*ups)); 4559b47acb0aSGleb Smirnoff if (internal || CPU_ABSENT(i)) 4560b47acb0aSGleb Smirnoff continue; 4561b47acb0aSGleb Smirnoff cache = &z->uz_cpu[i]; 4562376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt; 4563376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt; 4564376b1ba3SJeff Roberson ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt; 4565b47acb0aSGleb Smirnoff ups[i].ups_allocs = cache->uc_allocs; 4566b47acb0aSGleb Smirnoff ups[i].ups_frees = cache->uc_frees; 4567b47acb0aSGleb Smirnoff } 4568b47acb0aSGleb Smirnoff } 4569b47acb0aSGleb Smirnoff 45707a52a97eSRobert Watson static int 45717a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 45727a52a97eSRobert Watson { 45737a52a97eSRobert Watson struct uma_stream_header ush; 45747a52a97eSRobert Watson struct uma_type_header uth; 457563b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 45767a52a97eSRobert Watson struct sbuf sbuf; 45777a52a97eSRobert Watson uma_keg_t kz; 45787a52a97eSRobert Watson uma_zone_t z; 45794bd61e19SJeff Roberson uint64_t items; 45808b987a77SJeff Roberson uint32_t kfree, pages; 45814e657159SMatthew D Fleming int count, error, i; 45827a52a97eSRobert Watson 458300f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 458400f0e671SMatthew D Fleming if (error != 0) 458500f0e671SMatthew D Fleming return (error); 45864e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 45871eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 458863b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 45894e657159SMatthew D Fleming 4590404a593eSMatthew D Fleming count = 0; 4591111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 45927a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 45937a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 45947a52a97eSRobert Watson count++; 45957a52a97eSRobert Watson } 45967a52a97eSRobert Watson 4597b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) 4598b47acb0aSGleb Smirnoff count++; 4599b47acb0aSGleb Smirnoff 46007a52a97eSRobert Watson /* 46017a52a97eSRobert Watson * Insert stream header. 46027a52a97eSRobert Watson */ 46037a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 46047a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 4605ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 46067a52a97eSRobert Watson ush.ush_count = count; 46074e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 46087a52a97eSRobert Watson 46097a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 46108b987a77SJeff Roberson kfree = pages = 0; 46118b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 46128b987a77SJeff Roberson kfree += kz->uk_domain[i].ud_free; 46138b987a77SJeff Roberson pages += kz->uk_domain[i].ud_pages; 46148b987a77SJeff Roberson } 46157a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 46167a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 46177a52a97eSRobert Watson ZONE_LOCK(z); 4618cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 46197a52a97eSRobert Watson uth.uth_align = kz->uk_align; 46207a52a97eSRobert Watson uth.uth_size = kz->uk_size; 46217a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 46224bd61e19SJeff Roberson if (z->uz_max_items > 0) { 46234bd61e19SJeff Roberson items = UZ_ITEMS_COUNT(z->uz_items); 46244bd61e19SJeff Roberson uth.uth_pages = (items / kz->uk_ipers) * 4625bb15d1c7SGleb Smirnoff kz->uk_ppera; 46264bd61e19SJeff Roberson } else 46278b987a77SJeff Roberson uth.uth_pages = pages; 4628f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 4629bb15d1c7SGleb Smirnoff kz->uk_ppera; 4630bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 46318b987a77SJeff Roberson uth.uth_keg_free = kfree; 4632cbbb4a00SRobert Watson 4633cbbb4a00SRobert Watson /* 4634cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 4635cbbb4a00SRobert Watson * on the keg's zone list. 4636cbbb4a00SRobert Watson */ 4637e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 4638cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 4639cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 4640b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, 4641b47acb0aSGleb Smirnoff kz->uk_flags & UMA_ZFLAG_INTERNAL); 46422450bbb8SRobert Watson ZONE_UNLOCK(z); 464363b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 464463b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 464563b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 46467a52a97eSRobert Watson } 46477a52a97eSRobert Watson } 4648b47acb0aSGleb Smirnoff LIST_FOREACH(z, &uma_cachezones, uz_link) { 4649b47acb0aSGleb Smirnoff bzero(&uth, sizeof(uth)); 4650b47acb0aSGleb Smirnoff ZONE_LOCK(z); 4651b47acb0aSGleb Smirnoff strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 4652b47acb0aSGleb Smirnoff uth.uth_size = z->uz_size; 4653b47acb0aSGleb Smirnoff uma_vm_zone_stats(&uth, z, &sbuf, ups, false); 4654b47acb0aSGleb Smirnoff ZONE_UNLOCK(z); 4655b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 4656b47acb0aSGleb Smirnoff for (i = 0; i < mp_maxid + 1; i++) 4657b47acb0aSGleb Smirnoff (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 4658b47acb0aSGleb Smirnoff } 4659b47acb0aSGleb Smirnoff 4660111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 46614e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 46624e657159SMatthew D Fleming sbuf_delete(&sbuf); 466363b5d112SKonstantin Belousov free(ups, M_TEMP); 46647a52a97eSRobert Watson return (error); 46657a52a97eSRobert Watson } 466648c5777eSRobert Watson 46670a5a3ccbSGleb Smirnoff int 46680a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 46690a5a3ccbSGleb Smirnoff { 46700a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 467116be9f54SGleb Smirnoff int error, max; 46720a5a3ccbSGleb Smirnoff 467316be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 46740a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 46750a5a3ccbSGleb Smirnoff if (error || !req->newptr) 46760a5a3ccbSGleb Smirnoff return (error); 46770a5a3ccbSGleb Smirnoff 46780a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 46790a5a3ccbSGleb Smirnoff 46800a5a3ccbSGleb Smirnoff return (0); 46810a5a3ccbSGleb Smirnoff } 46820a5a3ccbSGleb Smirnoff 46830a5a3ccbSGleb Smirnoff int 46840a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 46850a5a3ccbSGleb Smirnoff { 468620a4e154SJeff Roberson uma_zone_t zone; 46870a5a3ccbSGleb Smirnoff int cur; 46880a5a3ccbSGleb Smirnoff 468920a4e154SJeff Roberson /* 469020a4e154SJeff Roberson * Some callers want to add sysctls for global zones that 469120a4e154SJeff Roberson * may not yet exist so they pass a pointer to a pointer. 469220a4e154SJeff Roberson */ 469320a4e154SJeff Roberson if (arg2 == 0) 469420a4e154SJeff Roberson zone = *(uma_zone_t *)arg1; 469520a4e154SJeff Roberson else 469620a4e154SJeff Roberson zone = arg1; 46970a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 46980a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 46990a5a3ccbSGleb Smirnoff } 47000a5a3ccbSGleb Smirnoff 470120a4e154SJeff Roberson static int 470220a4e154SJeff Roberson sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS) 470320a4e154SJeff Roberson { 470420a4e154SJeff Roberson uma_zone_t zone = arg1; 470520a4e154SJeff Roberson uint64_t cur; 470620a4e154SJeff Roberson 470720a4e154SJeff Roberson cur = uma_zone_get_allocs(zone); 470820a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 470920a4e154SJeff Roberson } 471020a4e154SJeff Roberson 471120a4e154SJeff Roberson static int 471220a4e154SJeff Roberson sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS) 471320a4e154SJeff Roberson { 471420a4e154SJeff Roberson uma_zone_t zone = arg1; 471520a4e154SJeff Roberson uint64_t cur; 471620a4e154SJeff Roberson 471720a4e154SJeff Roberson cur = uma_zone_get_frees(zone); 471820a4e154SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 471920a4e154SJeff Roberson } 472020a4e154SJeff Roberson 47216d204a6aSRyan Libby static int 47226d204a6aSRyan Libby sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS) 47236d204a6aSRyan Libby { 47246d204a6aSRyan Libby struct sbuf sbuf; 47256d204a6aSRyan Libby uma_zone_t zone = arg1; 47266d204a6aSRyan Libby int error; 47276d204a6aSRyan Libby 47286d204a6aSRyan Libby sbuf_new_for_sysctl(&sbuf, NULL, 0, req); 47296d204a6aSRyan Libby if (zone->uz_flags != 0) 47306d204a6aSRyan Libby sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS); 47316d204a6aSRyan Libby else 47326d204a6aSRyan Libby sbuf_printf(&sbuf, "0"); 47336d204a6aSRyan Libby error = sbuf_finish(&sbuf); 47346d204a6aSRyan Libby sbuf_delete(&sbuf); 47356d204a6aSRyan Libby 47366d204a6aSRyan Libby return (error); 47376d204a6aSRyan Libby } 47386d204a6aSRyan Libby 4739f7af5015SRyan Libby static int 4740f7af5015SRyan Libby sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS) 4741f7af5015SRyan Libby { 4742f7af5015SRyan Libby uma_keg_t keg = arg1; 4743f7af5015SRyan Libby int avail, effpct, total; 4744f7af5015SRyan Libby 4745f7af5015SRyan Libby total = keg->uk_ppera * PAGE_SIZE; 474654c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0) 47479b8db4d0SRyan Libby total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize; 4748f7af5015SRyan Libby /* 4749f7af5015SRyan Libby * We consider the client's requested size and alignment here, not the 4750f7af5015SRyan Libby * real size determination uk_rsize, because we also adjust the real 4751f7af5015SRyan Libby * size for internal implementation reasons (max bitset size). 4752f7af5015SRyan Libby */ 4753f7af5015SRyan Libby avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1); 4754f7af5015SRyan Libby if ((keg->uk_flags & UMA_ZONE_PCPU) != 0) 4755f7af5015SRyan Libby avail *= mp_maxid + 1; 4756f7af5015SRyan Libby effpct = 100 * avail / total; 4757f7af5015SRyan Libby return (sysctl_handle_int(oidp, &effpct, 0, req)); 4758f7af5015SRyan Libby } 4759f7af5015SRyan Libby 47604bd61e19SJeff Roberson static int 47614bd61e19SJeff Roberson sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS) 47624bd61e19SJeff Roberson { 47634bd61e19SJeff Roberson uma_zone_t zone = arg1; 47644bd61e19SJeff Roberson uint64_t cur; 47654bd61e19SJeff Roberson 47664bd61e19SJeff Roberson cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items)); 47674bd61e19SJeff Roberson return (sysctl_handle_64(oidp, &cur, 0, req)); 47684bd61e19SJeff Roberson } 47694bd61e19SJeff Roberson 47709542ea7bSGleb Smirnoff #ifdef INVARIANTS 47719542ea7bSGleb Smirnoff static uma_slab_t 47729542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 47739542ea7bSGleb Smirnoff { 47749542ea7bSGleb Smirnoff uma_slab_t slab; 47759542ea7bSGleb Smirnoff uma_keg_t keg; 47769542ea7bSGleb Smirnoff uint8_t *mem; 47779542ea7bSGleb Smirnoff 47789542ea7bSGleb Smirnoff /* 47799542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 47809542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 47819542ea7bSGleb Smirnoff * essentially holds a reference. 47829542ea7bSGleb Smirnoff */ 4783727c6918SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 4784727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 4785bb15d1c7SGleb Smirnoff return (NULL); 478654c5ae80SRyan Libby if (zone->uz_flags & UMA_ZFLAG_VTOSLAB) 4787727c6918SJeff Roberson return (vtoslab((vm_offset_t)mem)); 4788bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 478954c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0) 4790727c6918SJeff Roberson return ((uma_slab_t)(mem + keg->uk_pgoff)); 47918b987a77SJeff Roberson KEG_LOCK(keg, 0); 47929542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 47938b987a77SJeff Roberson KEG_UNLOCK(keg, 0); 47949542ea7bSGleb Smirnoff 47959542ea7bSGleb Smirnoff return (slab); 47969542ea7bSGleb Smirnoff } 47979542ea7bSGleb Smirnoff 4798c5deaf04SGleb Smirnoff static bool 4799c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 4800c5deaf04SGleb Smirnoff { 4801c5deaf04SGleb Smirnoff 4802727c6918SJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0) 4803c5deaf04SGleb Smirnoff return (true); 4804c5deaf04SGleb Smirnoff 4805bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 4806c5deaf04SGleb Smirnoff } 4807c5deaf04SGleb Smirnoff 4808c5deaf04SGleb Smirnoff static bool 4809c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 4810c5deaf04SGleb Smirnoff { 4811c5deaf04SGleb Smirnoff uintptr_t idx; 4812c5deaf04SGleb Smirnoff 4813c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 4814c5deaf04SGleb Smirnoff return (true); 4815c5deaf04SGleb Smirnoff 4816c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 4817c5deaf04SGleb Smirnoff return (false); 4818c5deaf04SGleb Smirnoff 4819c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 4820c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 4821c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 4822c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 4823c5deaf04SGleb Smirnoff } 4824c5deaf04SGleb Smirnoff 4825c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 4826c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 4827c5deaf04SGleb Smirnoff return (true); 4828c5deaf04SGleb Smirnoff } 4829c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 4830c5deaf04SGleb Smirnoff 4831c5deaf04SGleb Smirnoff return (false); 4832c5deaf04SGleb Smirnoff } 4833c5deaf04SGleb Smirnoff 48349542ea7bSGleb Smirnoff /* 48359542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 48369542ea7bSGleb Smirnoff * 48379542ea7bSGleb Smirnoff */ 48389542ea7bSGleb Smirnoff static void 48399542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 48409542ea7bSGleb Smirnoff { 48419542ea7bSGleb Smirnoff uma_keg_t keg; 48429542ea7bSGleb Smirnoff int freei; 48439542ea7bSGleb Smirnoff 48449542ea7bSGleb Smirnoff if (slab == NULL) { 48459542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 48469542ea7bSGleb Smirnoff if (slab == NULL) 48479542ea7bSGleb Smirnoff panic("uma: item %p did not belong to zone %s\n", 48489542ea7bSGleb Smirnoff item, zone->uz_name); 48499542ea7bSGleb Smirnoff } 4850584061b4SJeff Roberson keg = zone->uz_keg; 48511e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 48529542ea7bSGleb Smirnoff 4853815db204SRyan Libby if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 48549542ea7bSGleb Smirnoff panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n", 48559542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 4856815db204SRyan Libby BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 48579542ea7bSGleb Smirnoff } 48589542ea7bSGleb Smirnoff 48599542ea7bSGleb Smirnoff /* 48609542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 48619542ea7bSGleb Smirnoff * and duplicate frees. 48629542ea7bSGleb Smirnoff * 48639542ea7bSGleb Smirnoff */ 48649542ea7bSGleb Smirnoff static void 48659542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 48669542ea7bSGleb Smirnoff { 48679542ea7bSGleb Smirnoff uma_keg_t keg; 48689542ea7bSGleb Smirnoff int freei; 48699542ea7bSGleb Smirnoff 48709542ea7bSGleb Smirnoff if (slab == NULL) { 48719542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 48729542ea7bSGleb Smirnoff if (slab == NULL) 48739542ea7bSGleb Smirnoff panic("uma: Freed item %p did not belong to zone %s\n", 48749542ea7bSGleb Smirnoff item, zone->uz_name); 48759542ea7bSGleb Smirnoff } 4876584061b4SJeff Roberson keg = zone->uz_keg; 48771e0701e1SJeff Roberson freei = slab_item_index(slab, keg, item); 48789542ea7bSGleb Smirnoff 48799542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 48809542ea7bSGleb Smirnoff panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n", 48819542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48829542ea7bSGleb Smirnoff 48831e0701e1SJeff Roberson if (slab_item(slab, keg, freei) != item) 48849542ea7bSGleb Smirnoff panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n", 48859542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48869542ea7bSGleb Smirnoff 4887815db204SRyan Libby if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg))) 48889542ea7bSGleb Smirnoff panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n", 48899542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 48909542ea7bSGleb Smirnoff 4891815db204SRyan Libby BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)); 48929542ea7bSGleb Smirnoff } 48939542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 48949542ea7bSGleb Smirnoff 489548c5777eSRobert Watson #ifdef DDB 489646d70077SConrad Meyer static int64_t 489746d70077SConrad Meyer get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used, 48980223790fSConrad Meyer uint64_t *sleeps, long *cachefree, uint64_t *xdomain) 489948c5777eSRobert Watson { 490046d70077SConrad Meyer uint64_t frees; 49010f9b7bf3SMark Johnston int i; 490248c5777eSRobert Watson 490348c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 490446d70077SConrad Meyer *allocs = counter_u64_fetch(z->uz_allocs); 49052efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 490646d70077SConrad Meyer *sleeps = z->uz_sleeps; 490746d70077SConrad Meyer *cachefree = 0; 490846d70077SConrad Meyer *xdomain = 0; 490948c5777eSRobert Watson } else 491046d70077SConrad Meyer uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps, 491146d70077SConrad Meyer xdomain); 49128b987a77SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 49138b987a77SJeff Roberson *cachefree += z->uz_domain[i].uzd_nitems; 4914e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 491548c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 49168b987a77SJeff Roberson *cachefree += kz->uk_domain[i].ud_free; 49178b987a77SJeff Roberson } 491846d70077SConrad Meyer *used = *allocs - frees; 491946d70077SConrad Meyer return (((int64_t)*used + *cachefree) * kz->uk_size); 492046d70077SConrad Meyer } 49210f9b7bf3SMark Johnston 492246d70077SConrad Meyer DB_SHOW_COMMAND(uma, db_show_uma) 492346d70077SConrad Meyer { 492446d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 492546d70077SConrad Meyer uma_keg_t kz; 492646d70077SConrad Meyer uma_zone_t z; 492746d70077SConrad Meyer uint64_t allocs, used, sleeps, xdomain; 492846d70077SConrad Meyer long cachefree; 492946d70077SConrad Meyer /* variables for sorting */ 493046d70077SConrad Meyer uma_keg_t cur_keg; 493146d70077SConrad Meyer uma_zone_t cur_zone, last_zone; 493246d70077SConrad Meyer int64_t cur_size, last_size, size; 493346d70077SConrad Meyer int ties; 493446d70077SConrad Meyer 493546d70077SConrad Meyer /* /i option produces machine-parseable CSV output */ 493646d70077SConrad Meyer if (modif[0] == 'i') { 493746d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n"; 493846d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n"; 493946d70077SConrad Meyer } else { 494046d70077SConrad Meyer fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n"; 494146d70077SConrad Meyer fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n"; 494246d70077SConrad Meyer } 494346d70077SConrad Meyer 494446d70077SConrad Meyer db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests", 494546d70077SConrad Meyer "Sleeps", "Bucket", "Total Mem", "XFree"); 494646d70077SConrad Meyer 494746d70077SConrad Meyer /* Sort the zones with largest size first. */ 494846d70077SConrad Meyer last_zone = NULL; 494946d70077SConrad Meyer last_size = INT64_MAX; 495046d70077SConrad Meyer for (;;) { 495146d70077SConrad Meyer cur_zone = NULL; 495246d70077SConrad Meyer cur_size = -1; 495346d70077SConrad Meyer ties = 0; 495446d70077SConrad Meyer LIST_FOREACH(kz, &uma_kegs, uk_link) { 495546d70077SConrad Meyer LIST_FOREACH(z, &kz->uk_zones, uz_link) { 495646d70077SConrad Meyer /* 495746d70077SConrad Meyer * In the case of size ties, print out zones 495846d70077SConrad Meyer * in the order they are encountered. That is, 495946d70077SConrad Meyer * when we encounter the most recently output 496046d70077SConrad Meyer * zone, we have already printed all preceding 496146d70077SConrad Meyer * ties, and we must print all following ties. 496246d70077SConrad Meyer */ 496346d70077SConrad Meyer if (z == last_zone) { 496446d70077SConrad Meyer ties = 1; 496546d70077SConrad Meyer continue; 496646d70077SConrad Meyer } 496746d70077SConrad Meyer size = get_uma_stats(kz, z, &allocs, &used, 496846d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 496946d70077SConrad Meyer if (size > cur_size && size < last_size + ties) 497046d70077SConrad Meyer { 497146d70077SConrad Meyer cur_size = size; 497246d70077SConrad Meyer cur_zone = z; 497346d70077SConrad Meyer cur_keg = kz; 497446d70077SConrad Meyer } 497546d70077SConrad Meyer } 497646d70077SConrad Meyer } 497746d70077SConrad Meyer if (cur_zone == NULL) 497846d70077SConrad Meyer break; 497946d70077SConrad Meyer 498046d70077SConrad Meyer size = get_uma_stats(cur_keg, cur_zone, &allocs, &used, 498146d70077SConrad Meyer &sleeps, &cachefree, &xdomain); 498246d70077SConrad Meyer db_printf(fmt_entry, cur_zone->uz_name, 498346d70077SConrad Meyer (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree, 498446d70077SConrad Meyer (uintmax_t)allocs, (uintmax_t)sleeps, 498520a4e154SJeff Roberson (unsigned)cur_zone->uz_bucket_size, (intmax_t)size, 498620a4e154SJeff Roberson xdomain); 498746d70077SConrad Meyer 4988687c94aaSJohn Baldwin if (db_pager_quit) 4989687c94aaSJohn Baldwin return; 499046d70077SConrad Meyer last_zone = cur_zone; 499146d70077SConrad Meyer last_size = cur_size; 499248c5777eSRobert Watson } 499348c5777eSRobert Watson } 499403175483SAlexander Motin 499503175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 499603175483SAlexander Motin { 499703175483SAlexander Motin uma_zone_t z; 4998ab3185d1SJeff Roberson uint64_t allocs, frees; 49990f9b7bf3SMark Johnston long cachefree; 50000f9b7bf3SMark Johnston int i; 500103175483SAlexander Motin 500203175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 500303175483SAlexander Motin "Requests", "Bucket"); 500403175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 5005c1685086SJeff Roberson uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL); 50060f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 50070f9b7bf3SMark Johnston cachefree += z->uz_domain[i].uzd_nitems; 50080f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 500903175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 501003175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 501120a4e154SJeff Roberson (uintmax_t)allocs, z->uz_bucket_size); 501203175483SAlexander Motin if (db_pager_quit) 501303175483SAlexander Motin return; 501403175483SAlexander Motin } 501503175483SAlexander Motin } 50169542ea7bSGleb Smirnoff #endif /* DDB */ 5017