160727d8bSWarner Losh /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 4ef72505eSJeff Roberson * Copyright (c) 2002-2005, 2009, 2013 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> 788355f576SJeff Roberson #include <sys/smp.h> 79e60b2fcbSGleb Smirnoff #include <sys/taskqueue.h> 8086bbae32SJeff Roberson #include <sys/vmmeter.h> 8186bbae32SJeff Roberson 828355f576SJeff Roberson #include <vm/vm.h> 83194a979eSMark Johnston #include <vm/vm_domainset.h> 848355f576SJeff Roberson #include <vm/vm_object.h> 858355f576SJeff Roberson #include <vm/vm_page.h> 86a4915c21SAttilio Rao #include <vm/vm_pageout.h> 878355f576SJeff Roberson #include <vm/vm_param.h> 88ab3185d1SJeff Roberson #include <vm/vm_phys.h> 8930c5525bSAndrew Gallatin #include <vm/vm_pagequeue.h> 908355f576SJeff Roberson #include <vm/vm_map.h> 918355f576SJeff Roberson #include <vm/vm_kern.h> 928355f576SJeff Roberson #include <vm/vm_extern.h> 938355f576SJeff Roberson #include <vm/uma.h> 948355f576SJeff Roberson #include <vm/uma_int.h> 95639c9550SJeff Roberson #include <vm/uma_dbg.h> 968355f576SJeff Roberson 9748c5777eSRobert Watson #include <ddb/ddb.h> 9848c5777eSRobert Watson 998d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 1008d689e04SGleb Smirnoff #include <vm/memguard.h> 1018d689e04SGleb Smirnoff #endif 1028d689e04SGleb Smirnoff 1038355f576SJeff Roberson /* 104ab3185d1SJeff Roberson * This is the zone and keg from which all zones are spawned. 1058355f576SJeff Roberson */ 106ab3185d1SJeff Roberson static uma_zone_t kegs; 107ab3185d1SJeff Roberson static uma_zone_t zones; 1088355f576SJeff Roberson 109ab3185d1SJeff Roberson /* This is the zone from which all offpage uma_slab_ts are allocated. */ 1108355f576SJeff Roberson static uma_zone_t slabzone; 1118355f576SJeff Roberson 1128355f576SJeff Roberson /* 1138355f576SJeff Roberson * The initial hash tables come out of this zone so they can be allocated 1148355f576SJeff Roberson * prior to malloc coming up. 1158355f576SJeff Roberson */ 1168355f576SJeff Roberson static uma_zone_t hashzone; 1178355f576SJeff Roberson 1181e319f6dSRobert Watson /* The boot-time adjusted value for cache line alignment. */ 119e4cd31ddSJeff Roberson int uma_align_cache = 64 - 1; 1201e319f6dSRobert Watson 121961647dfSJeff Roberson static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets"); 122961647dfSJeff Roberson 1238355f576SJeff Roberson /* 12486bbae32SJeff Roberson * Are we allowed to allocate buckets? 12586bbae32SJeff Roberson */ 12686bbae32SJeff Roberson static int bucketdisable = 1; 12786bbae32SJeff Roberson 128099a0e58SBosko Milekic /* Linked list of all kegs in the system */ 12913e403fdSAntoine Brodin static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs); 1308355f576SJeff Roberson 13103175483SAlexander Motin /* Linked list of all cache-only zones in the system */ 13203175483SAlexander Motin static LIST_HEAD(,uma_zone) uma_cachezones = 13303175483SAlexander Motin LIST_HEAD_INITIALIZER(uma_cachezones); 13403175483SAlexander Motin 135111fbcd5SBryan Venteicher /* This RW lock protects the keg list */ 136fe933c1dSMateusz Guzik static struct rwlock_padalign __exclusive_cache_line uma_rwlock; 1378355f576SJeff Roberson 138ac0a6fd0SGleb Smirnoff /* 139ac0a6fd0SGleb Smirnoff * Pointer and counter to pool of pages, that is preallocated at 140f7d35785SGleb Smirnoff * startup to bootstrap UMA. 141ac0a6fd0SGleb Smirnoff */ 142ac0a6fd0SGleb Smirnoff static char *bootmem; 143ac0a6fd0SGleb Smirnoff static int boot_pages; 1448355f576SJeff Roberson 14595c4bf75SKonstantin Belousov static struct sx uma_drain_lock; 14695c4bf75SKonstantin Belousov 1472e47807cSJeff Roberson /* kmem soft limit. */ 148ad5b0f5bSJeff Roberson static unsigned long uma_kmem_limit = LONG_MAX; 1492e47807cSJeff Roberson static volatile unsigned long uma_kmem_total; 1502e47807cSJeff Roberson 1518355f576SJeff Roberson /* Is the VM done starting up? */ 152f4bef67cSGleb Smirnoff static enum { BOOT_COLD = 0, BOOT_STRAPPED, BOOT_PAGEALLOC, BOOT_BUCKETS, 153f4bef67cSGleb Smirnoff BOOT_RUNNING } booted = BOOT_COLD; 1548355f576SJeff Roberson 155ef72505eSJeff Roberson /* 1569643769aSJeff Roberson * This is the handle used to schedule events that need to happen 1579643769aSJeff Roberson * outside of the allocation fast path. 1589643769aSJeff Roberson */ 1598355f576SJeff Roberson static struct callout uma_callout; 1609643769aSJeff Roberson #define UMA_TIMEOUT 20 /* Seconds for callout interval. */ 1618355f576SJeff Roberson 1628355f576SJeff Roberson /* 1638355f576SJeff Roberson * This structure is passed as the zone ctor arg so that I don't have to create 1648355f576SJeff Roberson * a special allocation function just for zones. 1658355f576SJeff Roberson */ 1668355f576SJeff Roberson struct uma_zctor_args { 167bb196eb4SMatthew D Fleming const char *name; 168c3bdc05fSAndrew R. Reiter size_t size; 1698355f576SJeff Roberson uma_ctor ctor; 1708355f576SJeff Roberson uma_dtor dtor; 1718355f576SJeff Roberson uma_init uminit; 1728355f576SJeff Roberson uma_fini fini; 1730095a784SJeff Roberson uma_import import; 1740095a784SJeff Roberson uma_release release; 1750095a784SJeff Roberson void *arg; 176099a0e58SBosko Milekic uma_keg_t keg; 177099a0e58SBosko Milekic int align; 17885dcf349SGleb Smirnoff uint32_t flags; 179099a0e58SBosko Milekic }; 180099a0e58SBosko Milekic 181099a0e58SBosko Milekic struct uma_kctor_args { 182099a0e58SBosko Milekic uma_zone_t zone; 183099a0e58SBosko Milekic size_t size; 184099a0e58SBosko Milekic uma_init uminit; 185099a0e58SBosko Milekic uma_fini fini; 1868355f576SJeff Roberson int align; 18785dcf349SGleb Smirnoff uint32_t flags; 1888355f576SJeff Roberson }; 1898355f576SJeff Roberson 190cae33c14SJeff Roberson struct uma_bucket_zone { 191cae33c14SJeff Roberson uma_zone_t ubz_zone; 192cae33c14SJeff Roberson char *ubz_name; 193fc03d22bSJeff Roberson int ubz_entries; /* Number of items it can hold. */ 194fc03d22bSJeff Roberson int ubz_maxsize; /* Maximum allocation size per-item. */ 195cae33c14SJeff Roberson }; 196cae33c14SJeff Roberson 197f9d27e75SRobert Watson /* 198fc03d22bSJeff Roberson * Compute the actual number of bucket entries to pack them in power 199fc03d22bSJeff Roberson * of two sizes for more efficient space utilization. 200f9d27e75SRobert Watson */ 201fc03d22bSJeff Roberson #define BUCKET_SIZE(n) \ 202fc03d22bSJeff Roberson (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *)) 203fc03d22bSJeff Roberson 2041aa6c758SAlexander Motin #define BUCKET_MAX BUCKET_SIZE(256) 205fc03d22bSJeff Roberson 206fc03d22bSJeff Roberson struct uma_bucket_zone bucket_zones[] = { 2076fd34d6fSJeff Roberson { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 }, 208f3932e90SAlexander Motin { NULL, "6 Bucket", BUCKET_SIZE(6), 3072 }, 2096fd34d6fSJeff Roberson { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 }, 210f3932e90SAlexander Motin { NULL, "12 Bucket", BUCKET_SIZE(12), 1536 }, 2116fd34d6fSJeff Roberson { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 }, 212fc03d22bSJeff Roberson { NULL, "32 Bucket", BUCKET_SIZE(32), 512 }, 213fc03d22bSJeff Roberson { NULL, "64 Bucket", BUCKET_SIZE(64), 256 }, 214fc03d22bSJeff Roberson { NULL, "128 Bucket", BUCKET_SIZE(128), 128 }, 2151aa6c758SAlexander Motin { NULL, "256 Bucket", BUCKET_SIZE(256), 64 }, 216fc03d22bSJeff Roberson { NULL, NULL, 0} 217fc03d22bSJeff Roberson }; 218cae33c14SJeff Roberson 2192019094aSRobert Watson /* 2202019094aSRobert Watson * Flags and enumerations to be passed to internal functions. 2212019094aSRobert Watson */ 222bb15d1c7SGleb Smirnoff enum zfreeskip { 223bb15d1c7SGleb Smirnoff SKIP_NONE = 0, 224bb15d1c7SGleb Smirnoff SKIP_CNT = 0x00000001, 225bb15d1c7SGleb Smirnoff SKIP_DTOR = 0x00010000, 226bb15d1c7SGleb Smirnoff SKIP_FINI = 0x00020000, 227bb15d1c7SGleb Smirnoff }; 228b23f72e9SBrian Feldman 229ab3185d1SJeff Roberson #define UMA_ANYDOMAIN -1 /* Special value for domain search. */ 230ab3185d1SJeff Roberson 2318355f576SJeff Roberson /* Prototypes.. */ 2328355f576SJeff Roberson 233f4bef67cSGleb Smirnoff int uma_startup_count(int); 234f4bef67cSGleb Smirnoff void uma_startup(void *, int); 235f4bef67cSGleb Smirnoff void uma_startup1(void); 236f4bef67cSGleb Smirnoff void uma_startup2(void); 237f4bef67cSGleb Smirnoff 238ab3185d1SJeff Roberson static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 239ab3185d1SJeff Roberson static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 240ab3059a8SMatt Macy static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 241ab3185d1SJeff Roberson static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int); 242f2c2231eSRyan Stone static void page_free(void *, vm_size_t, uint8_t); 243ab3059a8SMatt Macy static void pcpu_page_free(void *, vm_size_t, uint8_t); 244ab3185d1SJeff Roberson static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int); 2459643769aSJeff Roberson static void cache_drain(uma_zone_t); 2468355f576SJeff Roberson static void bucket_drain(uma_zone_t, uma_bucket_t); 247aaa8bb16SJeff Roberson static void bucket_cache_drain(uma_zone_t zone); 248b23f72e9SBrian Feldman static int keg_ctor(void *, int, void *, int); 249099a0e58SBosko Milekic static void keg_dtor(void *, int, void *); 250b23f72e9SBrian Feldman static int zone_ctor(void *, int, void *, int); 2519c2cd7e5SJeff Roberson static void zone_dtor(void *, int, void *); 252b23f72e9SBrian Feldman static int zero_init(void *, int, int); 253e20a199fSJeff Roberson static void keg_small_init(uma_keg_t keg); 254e20a199fSJeff Roberson static void keg_large_init(uma_keg_t keg); 2558355f576SJeff Roberson static void zone_foreach(void (*zfunc)(uma_zone_t)); 2568355f576SJeff Roberson static void zone_timeout(uma_zone_t zone); 2570aef6126SJeff Roberson static int hash_alloc(struct uma_hash *); 2580aef6126SJeff Roberson static int hash_expand(struct uma_hash *, struct uma_hash *); 2590aef6126SJeff Roberson static void hash_free(struct uma_hash *hash); 2608355f576SJeff Roberson static void uma_timeout(void *); 2618355f576SJeff Roberson static void uma_startup3(void); 262ab3185d1SJeff Roberson static void *zone_alloc_item(uma_zone_t, void *, int, int); 263bb15d1c7SGleb Smirnoff static void *zone_alloc_item_locked(uma_zone_t, void *, int, int); 2640095a784SJeff Roberson static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip); 26586bbae32SJeff Roberson static void bucket_enable(void); 266cae33c14SJeff Roberson static void bucket_init(void); 2676fd34d6fSJeff Roberson static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int); 2686fd34d6fSJeff Roberson static void bucket_free(uma_zone_t zone, uma_bucket_t, void *); 269cae33c14SJeff Roberson static void bucket_zone_drain(void); 270bb15d1c7SGleb Smirnoff static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int, int); 271ab3185d1SJeff Roberson static uma_slab_t zone_fetch_slab(uma_zone_t, uma_keg_t, int, int); 2720095a784SJeff Roberson static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab); 273bb15d1c7SGleb Smirnoff static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item); 274e20a199fSJeff Roberson static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, 27585dcf349SGleb Smirnoff uma_fini fini, int align, uint32_t flags); 276ab3185d1SJeff Roberson static int zone_import(uma_zone_t, void **, int, int, int); 277ab3185d1SJeff Roberson static void zone_release(uma_zone_t, void **, int); 278ab3185d1SJeff Roberson static void uma_zero_item(void *, uma_zone_t); 279bbee39c6SJeff Roberson 2808355f576SJeff Roberson void uma_print_zone(uma_zone_t); 2818355f576SJeff Roberson void uma_print_stats(void); 2827a52a97eSRobert Watson static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS); 2837a52a97eSRobert Watson static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS); 2848355f576SJeff Roberson 2859542ea7bSGleb Smirnoff #ifdef INVARIANTS 286c5deaf04SGleb Smirnoff static bool uma_dbg_kskip(uma_keg_t keg, void *mem); 287c5deaf04SGleb Smirnoff static bool uma_dbg_zskip(uma_zone_t zone, void *mem); 2889542ea7bSGleb Smirnoff static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item); 2899542ea7bSGleb Smirnoff static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item); 290c5deaf04SGleb Smirnoff 291c5deaf04SGleb Smirnoff static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0, 292c5deaf04SGleb Smirnoff "Memory allocation debugging"); 293c5deaf04SGleb Smirnoff 294c5deaf04SGleb Smirnoff static u_int dbg_divisor = 1; 295c5deaf04SGleb Smirnoff SYSCTL_UINT(_vm_debug, OID_AUTO, divisor, 296c5deaf04SGleb Smirnoff CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0, 297c5deaf04SGleb Smirnoff "Debug & thrash every this item in memory allocator"); 298c5deaf04SGleb Smirnoff 299c5deaf04SGleb Smirnoff static counter_u64_t uma_dbg_cnt = EARLY_COUNTER; 300c5deaf04SGleb Smirnoff static counter_u64_t uma_skip_cnt = EARLY_COUNTER; 301c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD, 302c5deaf04SGleb Smirnoff &uma_dbg_cnt, "memory items debugged"); 303c5deaf04SGleb Smirnoff SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD, 304c5deaf04SGleb Smirnoff &uma_skip_cnt, "memory items skipped, not debugged"); 3059542ea7bSGleb Smirnoff #endif 3069542ea7bSGleb Smirnoff 3078355f576SJeff Roberson SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL); 3088355f576SJeff Roberson 3097a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT, 3107a52a97eSRobert Watson 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones"); 3117a52a97eSRobert Watson 3127a52a97eSRobert Watson SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 3137a52a97eSRobert Watson 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats"); 3147a52a97eSRobert Watson 3152f891cd5SPawel Jakub Dawidek static int zone_warnings = 1; 316af3b2549SHans Petter Selasky SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0, 3172f891cd5SPawel Jakub Dawidek "Warn when UMA zones becomes full"); 3182f891cd5SPawel Jakub Dawidek 3192e47807cSJeff Roberson /* Adjust bytes under management by UMA. */ 3202e47807cSJeff Roberson static inline void 3212e47807cSJeff Roberson uma_total_dec(unsigned long size) 3222e47807cSJeff Roberson { 3232e47807cSJeff Roberson 3242e47807cSJeff Roberson atomic_subtract_long(&uma_kmem_total, size); 3252e47807cSJeff Roberson } 3262e47807cSJeff Roberson 3272e47807cSJeff Roberson static inline void 3282e47807cSJeff Roberson uma_total_inc(unsigned long size) 3292e47807cSJeff Roberson { 3302e47807cSJeff Roberson 3312e47807cSJeff Roberson if (atomic_fetchadd_long(&uma_kmem_total, size) > uma_kmem_limit) 3322e47807cSJeff Roberson uma_reclaim_wakeup(); 3332e47807cSJeff Roberson } 3342e47807cSJeff Roberson 33586bbae32SJeff Roberson /* 33686bbae32SJeff Roberson * This routine checks to see whether or not it's safe to enable buckets. 33786bbae32SJeff Roberson */ 33886bbae32SJeff Roberson static void 33986bbae32SJeff Roberson bucket_enable(void) 34086bbae32SJeff Roberson { 341251386b4SMaksim Yevmenkin bucketdisable = vm_page_count_min(); 34286bbae32SJeff Roberson } 34386bbae32SJeff Roberson 344dc2c7965SRobert Watson /* 345dc2c7965SRobert Watson * Initialize bucket_zones, the array of zones of buckets of various sizes. 346dc2c7965SRobert Watson * 347dc2c7965SRobert Watson * For each zone, calculate the memory required for each bucket, consisting 348fc03d22bSJeff Roberson * of the header and an array of pointers. 349dc2c7965SRobert Watson */ 350cae33c14SJeff Roberson static void 351cae33c14SJeff Roberson bucket_init(void) 352cae33c14SJeff Roberson { 353cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 354cae33c14SJeff Roberson int size; 355cae33c14SJeff Roberson 356d74e6a1dSAlan Cox for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) { 357cae33c14SJeff Roberson size = roundup(sizeof(struct uma_bucket), sizeof(void *)); 358cae33c14SJeff Roberson size += sizeof(void *) * ubz->ubz_entries; 359cae33c14SJeff Roberson ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size, 360e20a199fSJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 361ab3185d1SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | UMA_ZONE_NUMA); 362cae33c14SJeff Roberson } 363cae33c14SJeff Roberson } 364cae33c14SJeff Roberson 365dc2c7965SRobert Watson /* 366dc2c7965SRobert Watson * Given a desired number of entries for a bucket, return the zone from which 367dc2c7965SRobert Watson * to allocate the bucket. 368dc2c7965SRobert Watson */ 369dc2c7965SRobert Watson static struct uma_bucket_zone * 370dc2c7965SRobert Watson bucket_zone_lookup(int entries) 371dc2c7965SRobert Watson { 372fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 373dc2c7965SRobert Watson 374fc03d22bSJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 375fc03d22bSJeff Roberson if (ubz->ubz_entries >= entries) 376fc03d22bSJeff Roberson return (ubz); 377fc03d22bSJeff Roberson ubz--; 378fc03d22bSJeff Roberson return (ubz); 379fc03d22bSJeff Roberson } 380fc03d22bSJeff Roberson 381fc03d22bSJeff Roberson static int 382fc03d22bSJeff Roberson bucket_select(int size) 383fc03d22bSJeff Roberson { 384fc03d22bSJeff Roberson struct uma_bucket_zone *ubz; 385fc03d22bSJeff Roberson 386fc03d22bSJeff Roberson ubz = &bucket_zones[0]; 387fc03d22bSJeff Roberson if (size > ubz->ubz_maxsize) 388fc03d22bSJeff Roberson return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1); 389fc03d22bSJeff Roberson 390fc03d22bSJeff Roberson for (; ubz->ubz_entries != 0; ubz++) 391fc03d22bSJeff Roberson if (ubz->ubz_maxsize < size) 392fc03d22bSJeff Roberson break; 393fc03d22bSJeff Roberson ubz--; 394fc03d22bSJeff Roberson return (ubz->ubz_entries); 395dc2c7965SRobert Watson } 396dc2c7965SRobert Watson 397cae33c14SJeff Roberson static uma_bucket_t 3986fd34d6fSJeff Roberson bucket_alloc(uma_zone_t zone, void *udata, int flags) 399cae33c14SJeff Roberson { 400cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 401cae33c14SJeff Roberson uma_bucket_t bucket; 402cae33c14SJeff Roberson 403cae33c14SJeff Roberson /* 404cae33c14SJeff Roberson * This is to stop us from allocating per cpu buckets while we're 4053803b26bSDag-Erling Smørgrav * running out of vm.boot_pages. Otherwise, we would exhaust the 406cae33c14SJeff Roberson * boot pages. This also prevents us from allocating buckets in 407cae33c14SJeff Roberson * low memory situations. 408cae33c14SJeff Roberson */ 409cae33c14SJeff Roberson if (bucketdisable) 410cae33c14SJeff Roberson return (NULL); 4116fd34d6fSJeff Roberson /* 4126fd34d6fSJeff Roberson * To limit bucket recursion we store the original zone flags 4136fd34d6fSJeff Roberson * in a cookie passed via zalloc_arg/zfree_arg. This allows the 4146fd34d6fSJeff Roberson * NOVM flag to persist even through deep recursions. We also 4156fd34d6fSJeff Roberson * store ZFLAG_BUCKET once we have recursed attempting to allocate 4166fd34d6fSJeff Roberson * a bucket for a bucket zone so we do not allow infinite bucket 4176fd34d6fSJeff Roberson * recursion. This cookie will even persist to frees of unused 4186fd34d6fSJeff Roberson * buckets via the allocation path or bucket allocations in the 4196fd34d6fSJeff Roberson * free path. 4206fd34d6fSJeff Roberson */ 4216fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 4226fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 423e8a720feSAlexander Motin else { 424e8a720feSAlexander Motin if ((uintptr_t)udata & UMA_ZFLAG_BUCKET) 425e8a720feSAlexander Motin return (NULL); 4266fd34d6fSJeff Roberson udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET); 427e8a720feSAlexander Motin } 4286fd34d6fSJeff Roberson if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY) 429af526374SJeff Roberson flags |= M_NOVM; 430af526374SJeff Roberson ubz = bucket_zone_lookup(zone->uz_count); 43120d3ab87SAlexander Motin if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0) 43220d3ab87SAlexander Motin ubz++; 4336fd34d6fSJeff Roberson bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags); 434cae33c14SJeff Roberson if (bucket) { 435cae33c14SJeff Roberson #ifdef INVARIANTS 436cae33c14SJeff Roberson bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries); 437cae33c14SJeff Roberson #endif 438cae33c14SJeff Roberson bucket->ub_cnt = 0; 439cae33c14SJeff Roberson bucket->ub_entries = ubz->ubz_entries; 440cae33c14SJeff Roberson } 441cae33c14SJeff Roberson 442cae33c14SJeff Roberson return (bucket); 443cae33c14SJeff Roberson } 444cae33c14SJeff Roberson 445cae33c14SJeff Roberson static void 4466fd34d6fSJeff Roberson bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata) 447cae33c14SJeff Roberson { 448cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 449cae33c14SJeff Roberson 450fc03d22bSJeff Roberson KASSERT(bucket->ub_cnt == 0, 451fc03d22bSJeff Roberson ("bucket_free: Freeing a non free bucket.")); 4526fd34d6fSJeff Roberson if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0) 4536fd34d6fSJeff Roberson udata = (void *)(uintptr_t)zone->uz_flags; 454dc2c7965SRobert Watson ubz = bucket_zone_lookup(bucket->ub_entries); 4556fd34d6fSJeff Roberson uma_zfree_arg(ubz->ubz_zone, bucket, udata); 456cae33c14SJeff Roberson } 457cae33c14SJeff Roberson 458cae33c14SJeff Roberson static void 459cae33c14SJeff Roberson bucket_zone_drain(void) 460cae33c14SJeff Roberson { 461cae33c14SJeff Roberson struct uma_bucket_zone *ubz; 462cae33c14SJeff Roberson 463cae33c14SJeff Roberson for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) 464cae33c14SJeff Roberson zone_drain(ubz->ubz_zone); 465cae33c14SJeff Roberson } 466cae33c14SJeff Roberson 4670f9b7bf3SMark Johnston static uma_bucket_t 4680f9b7bf3SMark Johnston zone_try_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom, const bool ws) 4690f9b7bf3SMark Johnston { 4700f9b7bf3SMark Johnston uma_bucket_t bucket; 4710f9b7bf3SMark Johnston 4720f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 4730f9b7bf3SMark Johnston 4740f9b7bf3SMark Johnston if ((bucket = LIST_FIRST(&zdom->uzd_buckets)) != NULL) { 4750f9b7bf3SMark Johnston MPASS(zdom->uzd_nitems >= bucket->ub_cnt); 4760f9b7bf3SMark Johnston LIST_REMOVE(bucket, ub_link); 4770f9b7bf3SMark Johnston zdom->uzd_nitems -= bucket->ub_cnt; 4780f9b7bf3SMark Johnston if (ws && zdom->uzd_imin > zdom->uzd_nitems) 4790f9b7bf3SMark Johnston zdom->uzd_imin = zdom->uzd_nitems; 480bb15d1c7SGleb Smirnoff zone->uz_bkt_count -= bucket->ub_cnt; 4810f9b7bf3SMark Johnston } 4820f9b7bf3SMark Johnston return (bucket); 4830f9b7bf3SMark Johnston } 4840f9b7bf3SMark Johnston 4850f9b7bf3SMark Johnston static void 4860f9b7bf3SMark Johnston zone_put_bucket(uma_zone_t zone, uma_zone_domain_t zdom, uma_bucket_t bucket, 4870f9b7bf3SMark Johnston const bool ws) 4880f9b7bf3SMark Johnston { 4890f9b7bf3SMark Johnston 4900f9b7bf3SMark Johnston ZONE_LOCK_ASSERT(zone); 491bb15d1c7SGleb Smirnoff KASSERT(zone->uz_bkt_count < zone->uz_bkt_max, ("%s: zone %p overflow", 492bb15d1c7SGleb Smirnoff __func__, zone)); 4930f9b7bf3SMark Johnston 4940f9b7bf3SMark Johnston LIST_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link); 4950f9b7bf3SMark Johnston zdom->uzd_nitems += bucket->ub_cnt; 4960f9b7bf3SMark Johnston if (ws && zdom->uzd_imax < zdom->uzd_nitems) 4970f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_nitems; 498bb15d1c7SGleb Smirnoff zone->uz_bkt_count += bucket->ub_cnt; 4990f9b7bf3SMark Johnston } 5000f9b7bf3SMark Johnston 5012f891cd5SPawel Jakub Dawidek static void 5022f891cd5SPawel Jakub Dawidek zone_log_warning(uma_zone_t zone) 5032f891cd5SPawel Jakub Dawidek { 5042f891cd5SPawel Jakub Dawidek static const struct timeval warninterval = { 300, 0 }; 5052f891cd5SPawel Jakub Dawidek 5062f891cd5SPawel Jakub Dawidek if (!zone_warnings || zone->uz_warning == NULL) 5072f891cd5SPawel Jakub Dawidek return; 5082f891cd5SPawel Jakub Dawidek 5092f891cd5SPawel Jakub Dawidek if (ratecheck(&zone->uz_ratecheck, &warninterval)) 5102f891cd5SPawel Jakub Dawidek printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning); 5112f891cd5SPawel Jakub Dawidek } 5122f891cd5SPawel Jakub Dawidek 51354503a13SJonathan T. Looney static inline void 51454503a13SJonathan T. Looney zone_maxaction(uma_zone_t zone) 51554503a13SJonathan T. Looney { 516e60b2fcbSGleb Smirnoff 517e60b2fcbSGleb Smirnoff if (zone->uz_maxaction.ta_func != NULL) 518e60b2fcbSGleb Smirnoff taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction); 51954503a13SJonathan T. Looney } 52054503a13SJonathan T. Looney 5218355f576SJeff Roberson /* 5228355f576SJeff Roberson * Routine called by timeout which is used to fire off some time interval 5239643769aSJeff Roberson * based calculations. (stats, hash size, etc.) 5248355f576SJeff Roberson * 5258355f576SJeff Roberson * Arguments: 5268355f576SJeff Roberson * arg Unused 5278355f576SJeff Roberson * 5288355f576SJeff Roberson * Returns: 5298355f576SJeff Roberson * Nothing 5308355f576SJeff Roberson */ 5318355f576SJeff Roberson static void 5328355f576SJeff Roberson uma_timeout(void *unused) 5338355f576SJeff Roberson { 53486bbae32SJeff Roberson bucket_enable(); 5358355f576SJeff Roberson zone_foreach(zone_timeout); 5368355f576SJeff Roberson 5378355f576SJeff Roberson /* Reschedule this event */ 5389643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 5398355f576SJeff Roberson } 5408355f576SJeff Roberson 5418355f576SJeff Roberson /* 5420f9b7bf3SMark Johnston * Update the working set size estimate for the zone's bucket cache. 5430f9b7bf3SMark Johnston * The constants chosen here are somewhat arbitrary. With an update period of 5440f9b7bf3SMark Johnston * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the 5450f9b7bf3SMark Johnston * last 100s. 5460f9b7bf3SMark Johnston */ 5470f9b7bf3SMark Johnston static void 5480f9b7bf3SMark Johnston zone_domain_update_wss(uma_zone_domain_t zdom) 5490f9b7bf3SMark Johnston { 5500f9b7bf3SMark Johnston long wss; 5510f9b7bf3SMark Johnston 5520f9b7bf3SMark Johnston MPASS(zdom->uzd_imax >= zdom->uzd_imin); 5530f9b7bf3SMark Johnston wss = zdom->uzd_imax - zdom->uzd_imin; 5540f9b7bf3SMark Johnston zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems; 5550f9b7bf3SMark Johnston zdom->uzd_wss = (3 * wss + 2 * zdom->uzd_wss) / 5; 5560f9b7bf3SMark Johnston } 5570f9b7bf3SMark Johnston 5580f9b7bf3SMark Johnston /* 5599643769aSJeff Roberson * Routine to perform timeout driven calculations. This expands the 5609643769aSJeff Roberson * hashes and does per cpu statistics aggregation. 5618355f576SJeff Roberson * 562e20a199fSJeff Roberson * Returns nothing. 5638355f576SJeff Roberson */ 5648355f576SJeff Roberson static void 565bb15d1c7SGleb Smirnoff zone_timeout(uma_zone_t zone) 5668355f576SJeff Roberson { 567bb15d1c7SGleb Smirnoff uma_keg_t keg = zone->uz_keg; 5688355f576SJeff Roberson 569e20a199fSJeff Roberson KEG_LOCK(keg); 5708355f576SJeff Roberson /* 571e20a199fSJeff Roberson * Expand the keg hash table. 5728355f576SJeff Roberson * 5738355f576SJeff Roberson * This is done if the number of slabs is larger than the hash size. 5748355f576SJeff Roberson * What I'm trying to do here is completely reduce collisions. This 5758355f576SJeff Roberson * may be a little aggressive. Should I allow for two collisions max? 5768355f576SJeff Roberson */ 577099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH && 578099a0e58SBosko Milekic keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) { 5790aef6126SJeff Roberson struct uma_hash newhash; 5800aef6126SJeff Roberson struct uma_hash oldhash; 5810aef6126SJeff Roberson int ret; 5825300d9ddSJeff Roberson 5830aef6126SJeff Roberson /* 5840aef6126SJeff Roberson * This is so involved because allocating and freeing 585e20a199fSJeff Roberson * while the keg lock is held will lead to deadlock. 5860aef6126SJeff Roberson * I have to do everything in stages and check for 5870aef6126SJeff Roberson * races. 5880aef6126SJeff Roberson */ 589099a0e58SBosko Milekic newhash = keg->uk_hash; 590e20a199fSJeff Roberson KEG_UNLOCK(keg); 5910aef6126SJeff Roberson ret = hash_alloc(&newhash); 592e20a199fSJeff Roberson KEG_LOCK(keg); 5930aef6126SJeff Roberson if (ret) { 594099a0e58SBosko Milekic if (hash_expand(&keg->uk_hash, &newhash)) { 595099a0e58SBosko Milekic oldhash = keg->uk_hash; 596099a0e58SBosko Milekic keg->uk_hash = newhash; 5970aef6126SJeff Roberson } else 5980aef6126SJeff Roberson oldhash = newhash; 5990aef6126SJeff Roberson 600e20a199fSJeff Roberson KEG_UNLOCK(keg); 6010aef6126SJeff Roberson hash_free(&oldhash); 602a1dff920SDavide Italiano return; 6030aef6126SJeff Roberson } 6045300d9ddSJeff Roberson } 605e20a199fSJeff Roberson 606bb15d1c7SGleb Smirnoff for (int i = 0; i < vm_ndomains; i++) 6070f9b7bf3SMark Johnston zone_domain_update_wss(&zone->uz_domain[i]); 608bb15d1c7SGleb Smirnoff 609bb15d1c7SGleb Smirnoff KEG_UNLOCK(keg); 6108355f576SJeff Roberson } 6118355f576SJeff Roberson 6128355f576SJeff Roberson /* 6135300d9ddSJeff Roberson * Allocate and zero fill the next sized hash table from the appropriate 6145300d9ddSJeff Roberson * backing store. 6155300d9ddSJeff Roberson * 6165300d9ddSJeff Roberson * Arguments: 6170aef6126SJeff Roberson * hash A new hash structure with the old hash size in uh_hashsize 6185300d9ddSJeff Roberson * 6195300d9ddSJeff Roberson * Returns: 620763df3ecSPedro F. Giffuni * 1 on success and 0 on failure. 6215300d9ddSJeff Roberson */ 62237c84183SPoul-Henning Kamp static int 6230aef6126SJeff Roberson hash_alloc(struct uma_hash *hash) 6245300d9ddSJeff Roberson { 6250aef6126SJeff Roberson int oldsize; 6265300d9ddSJeff Roberson int alloc; 6275300d9ddSJeff Roberson 6280aef6126SJeff Roberson oldsize = hash->uh_hashsize; 6290aef6126SJeff Roberson 6305300d9ddSJeff Roberson /* We're just going to go to a power of two greater */ 6310aef6126SJeff Roberson if (oldsize) { 6320aef6126SJeff Roberson hash->uh_hashsize = oldsize * 2; 6330aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize; 6340aef6126SJeff Roberson hash->uh_slab_hash = (struct slabhead *)malloc(alloc, 635961647dfSJeff Roberson M_UMAHASH, M_NOWAIT); 6365300d9ddSJeff Roberson } else { 6370aef6126SJeff Roberson alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT; 638e20a199fSJeff Roberson hash->uh_slab_hash = zone_alloc_item(hashzone, NULL, 639ab3185d1SJeff Roberson UMA_ANYDOMAIN, M_WAITOK); 6400aef6126SJeff Roberson hash->uh_hashsize = UMA_HASH_SIZE_INIT; 6415300d9ddSJeff Roberson } 6420aef6126SJeff Roberson if (hash->uh_slab_hash) { 6430aef6126SJeff Roberson bzero(hash->uh_slab_hash, alloc); 6440aef6126SJeff Roberson hash->uh_hashmask = hash->uh_hashsize - 1; 6450aef6126SJeff Roberson return (1); 6460aef6126SJeff Roberson } 6475300d9ddSJeff Roberson 6480aef6126SJeff Roberson return (0); 6495300d9ddSJeff Roberson } 6505300d9ddSJeff Roberson 6515300d9ddSJeff Roberson /* 65264f051e9SJeff Roberson * Expands the hash table for HASH zones. This is done from zone_timeout 65364f051e9SJeff Roberson * to reduce collisions. This must not be done in the regular allocation 65464f051e9SJeff Roberson * path, otherwise, we can recurse on the vm while allocating pages. 6558355f576SJeff Roberson * 6568355f576SJeff Roberson * Arguments: 6570aef6126SJeff Roberson * oldhash The hash you want to expand 6580aef6126SJeff Roberson * newhash The hash structure for the new table 6598355f576SJeff Roberson * 6608355f576SJeff Roberson * Returns: 6618355f576SJeff Roberson * Nothing 6628355f576SJeff Roberson * 6638355f576SJeff Roberson * Discussion: 6648355f576SJeff Roberson */ 6650aef6126SJeff Roberson static int 6660aef6126SJeff Roberson hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash) 6678355f576SJeff Roberson { 6688355f576SJeff Roberson uma_slab_t slab; 6698355f576SJeff Roberson int hval; 6708355f576SJeff Roberson int i; 6718355f576SJeff Roberson 6720aef6126SJeff Roberson if (!newhash->uh_slab_hash) 6730aef6126SJeff Roberson return (0); 6748355f576SJeff Roberson 6750aef6126SJeff Roberson if (oldhash->uh_hashsize >= newhash->uh_hashsize) 6760aef6126SJeff Roberson return (0); 6778355f576SJeff Roberson 6788355f576SJeff Roberson /* 6798355f576SJeff Roberson * I need to investigate hash algorithms for resizing without a 6808355f576SJeff Roberson * full rehash. 6818355f576SJeff Roberson */ 6828355f576SJeff Roberson 6830aef6126SJeff Roberson for (i = 0; i < oldhash->uh_hashsize; i++) 6840aef6126SJeff Roberson while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) { 6850aef6126SJeff Roberson slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]); 6860aef6126SJeff Roberson SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink); 6870aef6126SJeff Roberson hval = UMA_HASH(newhash, slab->us_data); 6880aef6126SJeff Roberson SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval], 6890aef6126SJeff Roberson slab, us_hlink); 6908355f576SJeff Roberson } 6918355f576SJeff Roberson 6920aef6126SJeff Roberson return (1); 6939c2cd7e5SJeff Roberson } 6949c2cd7e5SJeff Roberson 6955300d9ddSJeff Roberson /* 6965300d9ddSJeff Roberson * Free the hash bucket to the appropriate backing store. 6975300d9ddSJeff Roberson * 6985300d9ddSJeff Roberson * Arguments: 6995300d9ddSJeff Roberson * slab_hash The hash bucket we're freeing 7005300d9ddSJeff Roberson * hashsize The number of entries in that hash bucket 7015300d9ddSJeff Roberson * 7025300d9ddSJeff Roberson * Returns: 7035300d9ddSJeff Roberson * Nothing 7045300d9ddSJeff Roberson */ 7059c2cd7e5SJeff Roberson static void 7060aef6126SJeff Roberson hash_free(struct uma_hash *hash) 7079c2cd7e5SJeff Roberson { 7080aef6126SJeff Roberson if (hash->uh_slab_hash == NULL) 7090aef6126SJeff Roberson return; 7100aef6126SJeff Roberson if (hash->uh_hashsize == UMA_HASH_SIZE_INIT) 7110095a784SJeff Roberson zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE); 7128355f576SJeff Roberson else 713961647dfSJeff Roberson free(hash->uh_slab_hash, M_UMAHASH); 7148355f576SJeff Roberson } 7158355f576SJeff Roberson 7168355f576SJeff Roberson /* 7178355f576SJeff Roberson * Frees all outstanding items in a bucket 7188355f576SJeff Roberson * 7198355f576SJeff Roberson * Arguments: 7208355f576SJeff Roberson * zone The zone to free to, must be unlocked. 7218355f576SJeff Roberson * bucket The free/alloc bucket with items, cpu queue must be locked. 7228355f576SJeff Roberson * 7238355f576SJeff Roberson * Returns: 7248355f576SJeff Roberson * Nothing 7258355f576SJeff Roberson */ 7268355f576SJeff Roberson 7278355f576SJeff Roberson static void 7288355f576SJeff Roberson bucket_drain(uma_zone_t zone, uma_bucket_t bucket) 7298355f576SJeff Roberson { 7300095a784SJeff Roberson int i; 7318355f576SJeff Roberson 7328355f576SJeff Roberson if (bucket == NULL) 7338355f576SJeff Roberson return; 7348355f576SJeff Roberson 7350095a784SJeff Roberson if (zone->uz_fini) 7360095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 7370095a784SJeff Roberson zone->uz_fini(bucket->ub_bucket[i], zone->uz_size); 7380095a784SJeff Roberson zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt); 739bb45b411SGleb Smirnoff if (zone->uz_max_items > 0) { 740bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 741bb15d1c7SGleb Smirnoff zone->uz_items -= bucket->ub_cnt; 742bb15d1c7SGleb Smirnoff if (zone->uz_sleepers && zone->uz_items < zone->uz_max_items) 743bb15d1c7SGleb Smirnoff wakeup_one(zone); 744bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 745bb45b411SGleb Smirnoff } 7460095a784SJeff Roberson bucket->ub_cnt = 0; 7478355f576SJeff Roberson } 7488355f576SJeff Roberson 7498355f576SJeff Roberson /* 7508355f576SJeff Roberson * Drains the per cpu caches for a zone. 7518355f576SJeff Roberson * 7525d1ae027SRobert Watson * NOTE: This may only be called while the zone is being turn down, and not 7535d1ae027SRobert Watson * during normal operation. This is necessary in order that we do not have 7545d1ae027SRobert Watson * to migrate CPUs to drain the per-CPU caches. 7555d1ae027SRobert Watson * 7568355f576SJeff Roberson * Arguments: 7578355f576SJeff Roberson * zone The zone to drain, must be unlocked. 7588355f576SJeff Roberson * 7598355f576SJeff Roberson * Returns: 7608355f576SJeff Roberson * Nothing 7618355f576SJeff Roberson */ 7628355f576SJeff Roberson static void 7639643769aSJeff Roberson cache_drain(uma_zone_t zone) 7648355f576SJeff Roberson { 7658355f576SJeff Roberson uma_cache_t cache; 7668355f576SJeff Roberson int cpu; 7678355f576SJeff Roberson 7688355f576SJeff Roberson /* 7695d1ae027SRobert Watson * XXX: It is safe to not lock the per-CPU caches, because we're 7705d1ae027SRobert Watson * tearing down the zone anyway. I.e., there will be no further use 7715d1ae027SRobert Watson * of the caches at this point. 7725d1ae027SRobert Watson * 7735d1ae027SRobert Watson * XXX: It would good to be able to assert that the zone is being 7745d1ae027SRobert Watson * torn down to prevent improper use of cache_drain(). 7755d1ae027SRobert Watson * 7765d1ae027SRobert Watson * XXX: We lock the zone before passing into bucket_cache_drain() as 7775d1ae027SRobert Watson * it is used elsewhere. Should the tear-down path be made special 7785d1ae027SRobert Watson * there in some form? 7798355f576SJeff Roberson */ 7803aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 7818355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 7828355f576SJeff Roberson bucket_drain(zone, cache->uc_allocbucket); 7838355f576SJeff Roberson bucket_drain(zone, cache->uc_freebucket); 784174ab450SBosko Milekic if (cache->uc_allocbucket != NULL) 7856fd34d6fSJeff Roberson bucket_free(zone, cache->uc_allocbucket, NULL); 786174ab450SBosko Milekic if (cache->uc_freebucket != NULL) 7876fd34d6fSJeff Roberson bucket_free(zone, cache->uc_freebucket, NULL); 788d56368d7SBosko Milekic cache->uc_allocbucket = cache->uc_freebucket = NULL; 789d56368d7SBosko Milekic } 790aaa8bb16SJeff Roberson ZONE_LOCK(zone); 791aaa8bb16SJeff Roberson bucket_cache_drain(zone); 792aaa8bb16SJeff Roberson ZONE_UNLOCK(zone); 793aaa8bb16SJeff Roberson } 794aaa8bb16SJeff Roberson 795a2de44abSAlexander Motin static void 796a2de44abSAlexander Motin cache_shrink(uma_zone_t zone) 797a2de44abSAlexander Motin { 798a2de44abSAlexander Motin 799a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 800a2de44abSAlexander Motin return; 801a2de44abSAlexander Motin 802a2de44abSAlexander Motin ZONE_LOCK(zone); 803a2de44abSAlexander Motin zone->uz_count = (zone->uz_count_min + zone->uz_count) / 2; 804a2de44abSAlexander Motin ZONE_UNLOCK(zone); 805a2de44abSAlexander Motin } 806a2de44abSAlexander Motin 807a2de44abSAlexander Motin static void 808a2de44abSAlexander Motin cache_drain_safe_cpu(uma_zone_t zone) 809a2de44abSAlexander Motin { 810a2de44abSAlexander Motin uma_cache_t cache; 8118a8d9d14SAlexander Motin uma_bucket_t b1, b2; 812ab3185d1SJeff Roberson int domain; 813a2de44abSAlexander Motin 814a2de44abSAlexander Motin if (zone->uz_flags & UMA_ZFLAG_INTERNAL) 815a2de44abSAlexander Motin return; 816a2de44abSAlexander Motin 8178a8d9d14SAlexander Motin b1 = b2 = NULL; 818a2de44abSAlexander Motin ZONE_LOCK(zone); 819a2de44abSAlexander Motin critical_enter(); 820ab3185d1SJeff Roberson if (zone->uz_flags & UMA_ZONE_NUMA) 821ab3185d1SJeff Roberson domain = PCPU_GET(domain); 822ab3185d1SJeff Roberson else 823ab3185d1SJeff Roberson domain = 0; 824a2de44abSAlexander Motin cache = &zone->uz_cpu[curcpu]; 825a2de44abSAlexander Motin if (cache->uc_allocbucket) { 8268a8d9d14SAlexander Motin if (cache->uc_allocbucket->ub_cnt != 0) 8270f9b7bf3SMark Johnston zone_put_bucket(zone, &zone->uz_domain[domain], 8280f9b7bf3SMark Johnston cache->uc_allocbucket, false); 8298a8d9d14SAlexander Motin else 8308a8d9d14SAlexander Motin b1 = cache->uc_allocbucket; 831a2de44abSAlexander Motin cache->uc_allocbucket = NULL; 832a2de44abSAlexander Motin } 833a2de44abSAlexander Motin if (cache->uc_freebucket) { 8348a8d9d14SAlexander Motin if (cache->uc_freebucket->ub_cnt != 0) 8350f9b7bf3SMark Johnston zone_put_bucket(zone, &zone->uz_domain[domain], 8360f9b7bf3SMark Johnston cache->uc_freebucket, false); 8378a8d9d14SAlexander Motin else 8388a8d9d14SAlexander Motin b2 = cache->uc_freebucket; 839a2de44abSAlexander Motin cache->uc_freebucket = NULL; 840a2de44abSAlexander Motin } 841a2de44abSAlexander Motin critical_exit(); 842a2de44abSAlexander Motin ZONE_UNLOCK(zone); 8438a8d9d14SAlexander Motin if (b1) 8448a8d9d14SAlexander Motin bucket_free(zone, b1, NULL); 8458a8d9d14SAlexander Motin if (b2) 8468a8d9d14SAlexander Motin bucket_free(zone, b2, NULL); 847a2de44abSAlexander Motin } 848a2de44abSAlexander Motin 849a2de44abSAlexander Motin /* 850a2de44abSAlexander Motin * Safely drain per-CPU caches of a zone(s) to alloc bucket. 851a2de44abSAlexander Motin * This is an expensive call because it needs to bind to all CPUs 852a2de44abSAlexander Motin * one by one and enter a critical section on each of them in order 853a2de44abSAlexander Motin * to safely access their cache buckets. 854a2de44abSAlexander Motin * Zone lock must not be held on call this function. 855a2de44abSAlexander Motin */ 856a2de44abSAlexander Motin static void 857a2de44abSAlexander Motin cache_drain_safe(uma_zone_t zone) 858a2de44abSAlexander Motin { 859a2de44abSAlexander Motin int cpu; 860a2de44abSAlexander Motin 861a2de44abSAlexander Motin /* 862a2de44abSAlexander Motin * Polite bucket sizes shrinking was not enouth, shrink aggressively. 863a2de44abSAlexander Motin */ 864a2de44abSAlexander Motin if (zone) 865a2de44abSAlexander Motin cache_shrink(zone); 866a2de44abSAlexander Motin else 867a2de44abSAlexander Motin zone_foreach(cache_shrink); 868a2de44abSAlexander Motin 869a2de44abSAlexander Motin CPU_FOREACH(cpu) { 870a2de44abSAlexander Motin thread_lock(curthread); 871a2de44abSAlexander Motin sched_bind(curthread, cpu); 872a2de44abSAlexander Motin thread_unlock(curthread); 873a2de44abSAlexander Motin 874a2de44abSAlexander Motin if (zone) 875a2de44abSAlexander Motin cache_drain_safe_cpu(zone); 876a2de44abSAlexander Motin else 877a2de44abSAlexander Motin zone_foreach(cache_drain_safe_cpu); 878a2de44abSAlexander Motin } 879a2de44abSAlexander Motin thread_lock(curthread); 880a2de44abSAlexander Motin sched_unbind(curthread); 881a2de44abSAlexander Motin thread_unlock(curthread); 882a2de44abSAlexander Motin } 883a2de44abSAlexander Motin 884aaa8bb16SJeff Roberson /* 885aaa8bb16SJeff Roberson * Drain the cached buckets from a zone. Expects a locked zone on entry. 886aaa8bb16SJeff Roberson */ 887aaa8bb16SJeff Roberson static void 888aaa8bb16SJeff Roberson bucket_cache_drain(uma_zone_t zone) 889aaa8bb16SJeff Roberson { 890ab3185d1SJeff Roberson uma_zone_domain_t zdom; 891aaa8bb16SJeff Roberson uma_bucket_t bucket; 892ab3185d1SJeff Roberson int i; 8938355f576SJeff Roberson 8948355f576SJeff Roberson /* 895ab3185d1SJeff Roberson * Drain the bucket queues and free the buckets. 8968355f576SJeff Roberson */ 897ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 898ab3185d1SJeff Roberson zdom = &zone->uz_domain[i]; 8990f9b7bf3SMark Johnston while ((bucket = zone_try_fetch_bucket(zone, zdom, false)) != 9000f9b7bf3SMark Johnston NULL) { 9018355f576SJeff Roberson ZONE_UNLOCK(zone); 9028355f576SJeff Roberson bucket_drain(zone, bucket); 9036fd34d6fSJeff Roberson bucket_free(zone, bucket, NULL); 9048355f576SJeff Roberson ZONE_LOCK(zone); 9058355f576SJeff Roberson } 906ab3185d1SJeff Roberson } 907ace66b56SAlexander Motin 908ace66b56SAlexander Motin /* 909ace66b56SAlexander Motin * Shrink further bucket sizes. Price of single zone lock collision 910ace66b56SAlexander Motin * is probably lower then price of global cache drain. 911ace66b56SAlexander Motin */ 912ace66b56SAlexander Motin if (zone->uz_count > zone->uz_count_min) 913ace66b56SAlexander Motin zone->uz_count--; 9148355f576SJeff Roberson } 915fc03d22bSJeff Roberson 916fc03d22bSJeff Roberson static void 917fc03d22bSJeff Roberson keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start) 918fc03d22bSJeff Roberson { 919fc03d22bSJeff Roberson uint8_t *mem; 920fc03d22bSJeff Roberson int i; 921fc03d22bSJeff Roberson uint8_t flags; 922fc03d22bSJeff Roberson 9231431a748SGleb Smirnoff CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes", 9241431a748SGleb Smirnoff keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera); 9251431a748SGleb Smirnoff 926fc03d22bSJeff Roberson mem = slab->us_data; 927fc03d22bSJeff Roberson flags = slab->us_flags; 928fc03d22bSJeff Roberson i = start; 929fc03d22bSJeff Roberson if (keg->uk_fini != NULL) { 930fc03d22bSJeff Roberson for (i--; i > -1; i--) 931c5deaf04SGleb Smirnoff #ifdef INVARIANTS 932c5deaf04SGleb Smirnoff /* 933c5deaf04SGleb Smirnoff * trash_fini implies that dtor was trash_dtor. trash_fini 934c5deaf04SGleb Smirnoff * would check that memory hasn't been modified since free, 935c5deaf04SGleb Smirnoff * which executed trash_dtor. 936c5deaf04SGleb Smirnoff * That's why we need to run uma_dbg_kskip() check here, 937c5deaf04SGleb Smirnoff * albeit we don't make skip check for other init/fini 938c5deaf04SGleb Smirnoff * invocations. 939c5deaf04SGleb Smirnoff */ 940c5deaf04SGleb Smirnoff if (!uma_dbg_kskip(keg, slab->us_data + (keg->uk_rsize * i)) || 941c5deaf04SGleb Smirnoff keg->uk_fini != trash_fini) 942c5deaf04SGleb Smirnoff #endif 943fc03d22bSJeff Roberson keg->uk_fini(slab->us_data + (keg->uk_rsize * i), 944fc03d22bSJeff Roberson keg->uk_size); 945fc03d22bSJeff Roberson } 946fc03d22bSJeff Roberson if (keg->uk_flags & UMA_ZONE_OFFPAGE) 947fc03d22bSJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 948fc03d22bSJeff Roberson keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags); 9492e47807cSJeff Roberson uma_total_dec(PAGE_SIZE * keg->uk_ppera); 9508355f576SJeff Roberson } 9518355f576SJeff Roberson 9528355f576SJeff Roberson /* 953e20a199fSJeff Roberson * Frees pages from a keg back to the system. This is done on demand from 9548355f576SJeff Roberson * the pageout daemon. 9558355f576SJeff Roberson * 956e20a199fSJeff Roberson * Returns nothing. 9578355f576SJeff Roberson */ 958e20a199fSJeff Roberson static void 959e20a199fSJeff Roberson keg_drain(uma_keg_t keg) 9608355f576SJeff Roberson { 9611e183df2SStefan Farfeleder struct slabhead freeslabs = { 0 }; 962ab3185d1SJeff Roberson uma_domain_t dom; 963829be516SMark Johnston uma_slab_t slab, tmp; 964ab3185d1SJeff Roberson int i; 9658355f576SJeff Roberson 9668355f576SJeff Roberson /* 967e20a199fSJeff Roberson * We don't want to take pages from statically allocated kegs at this 9688355f576SJeff Roberson * time 9698355f576SJeff Roberson */ 970099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL) 9718355f576SJeff Roberson return; 9728355f576SJeff Roberson 9731431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_drain %s(%p) free items: %u", 9741431a748SGleb Smirnoff keg->uk_name, keg, keg->uk_free); 975e20a199fSJeff Roberson KEG_LOCK(keg); 976099a0e58SBosko Milekic if (keg->uk_free == 0) 9778355f576SJeff Roberson goto finished; 9788355f576SJeff Roberson 979ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 980ab3185d1SJeff Roberson dom = &keg->uk_domain[i]; 981ab3185d1SJeff Roberson LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) { 982829be516SMark Johnston /* We have nowhere to free these to. */ 983829be516SMark Johnston if (slab->us_flags & UMA_SLAB_BOOT) 9848355f576SJeff Roberson continue; 9858355f576SJeff Roberson 9868355f576SJeff Roberson LIST_REMOVE(slab, us_link); 987099a0e58SBosko Milekic keg->uk_pages -= keg->uk_ppera; 988099a0e58SBosko Milekic keg->uk_free -= keg->uk_ipers; 989713deb36SJeff Roberson 990099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 991ab3185d1SJeff Roberson UMA_HASH_REMOVE(&keg->uk_hash, slab, 992ab3185d1SJeff Roberson slab->us_data); 993713deb36SJeff Roberson 994713deb36SJeff Roberson SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink); 995713deb36SJeff Roberson } 996ab3185d1SJeff Roberson } 997ab3185d1SJeff Roberson 998713deb36SJeff Roberson finished: 999e20a199fSJeff Roberson KEG_UNLOCK(keg); 1000713deb36SJeff Roberson 1001713deb36SJeff Roberson while ((slab = SLIST_FIRST(&freeslabs)) != NULL) { 1002713deb36SJeff Roberson SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink); 10031645995bSKirk McKusick keg_free_slab(keg, slab, keg->uk_ipers); 10048355f576SJeff Roberson } 10058355f576SJeff Roberson } 10068355f576SJeff Roberson 1007e20a199fSJeff Roberson static void 1008e20a199fSJeff Roberson zone_drain_wait(uma_zone_t zone, int waitok) 1009e20a199fSJeff Roberson { 1010e20a199fSJeff Roberson 10118355f576SJeff Roberson /* 1012e20a199fSJeff Roberson * Set draining to interlock with zone_dtor() so we can release our 1013e20a199fSJeff Roberson * locks as we go. Only dtor() should do a WAITOK call since it 1014e20a199fSJeff Roberson * is the only call that knows the structure will still be available 1015e20a199fSJeff Roberson * when it wakes up. 1016e20a199fSJeff Roberson */ 1017e20a199fSJeff Roberson ZONE_LOCK(zone); 1018e20a199fSJeff Roberson while (zone->uz_flags & UMA_ZFLAG_DRAINING) { 1019e20a199fSJeff Roberson if (waitok == M_NOWAIT) 1020e20a199fSJeff Roberson goto out; 1021af526374SJeff Roberson msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1); 1022e20a199fSJeff Roberson } 1023e20a199fSJeff Roberson zone->uz_flags |= UMA_ZFLAG_DRAINING; 1024e20a199fSJeff Roberson bucket_cache_drain(zone); 1025e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1026e20a199fSJeff Roberson /* 1027e20a199fSJeff Roberson * The DRAINING flag protects us from being freed while 1028111fbcd5SBryan Venteicher * we're running. Normally the uma_rwlock would protect us but we 1029e20a199fSJeff Roberson * must be able to release and acquire the right lock for each keg. 1030e20a199fSJeff Roberson */ 1031bb15d1c7SGleb Smirnoff keg_drain(zone->uz_keg); 1032e20a199fSJeff Roberson ZONE_LOCK(zone); 1033e20a199fSJeff Roberson zone->uz_flags &= ~UMA_ZFLAG_DRAINING; 1034e20a199fSJeff Roberson wakeup(zone); 1035e20a199fSJeff Roberson out: 1036e20a199fSJeff Roberson ZONE_UNLOCK(zone); 1037e20a199fSJeff Roberson } 1038e20a199fSJeff Roberson 1039e20a199fSJeff Roberson void 1040e20a199fSJeff Roberson zone_drain(uma_zone_t zone) 1041e20a199fSJeff Roberson { 1042e20a199fSJeff Roberson 1043e20a199fSJeff Roberson zone_drain_wait(zone, M_NOWAIT); 1044e20a199fSJeff Roberson } 1045e20a199fSJeff Roberson 1046e20a199fSJeff Roberson /* 1047e20a199fSJeff Roberson * Allocate a new slab for a keg. This does not insert the slab onto a list. 1048194a979eSMark Johnston * If the allocation was successful, the keg lock will be held upon return, 1049194a979eSMark Johnston * otherwise the keg will be left unlocked. 10508355f576SJeff Roberson * 10518355f576SJeff Roberson * Arguments: 10528355f576SJeff Roberson * wait Shall we wait? 10538355f576SJeff Roberson * 10548355f576SJeff Roberson * Returns: 10558355f576SJeff Roberson * The slab that was allocated or NULL if there is no memory and the 10568355f576SJeff Roberson * caller specified M_NOWAIT. 10578355f576SJeff Roberson */ 10588355f576SJeff Roberson static uma_slab_t 1059ab3185d1SJeff Roberson keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int wait) 10608355f576SJeff Roberson { 1061e20a199fSJeff Roberson uma_alloc allocf; 1062099a0e58SBosko Milekic uma_slab_t slab; 10632e47807cSJeff Roberson unsigned long size; 106485dcf349SGleb Smirnoff uint8_t *mem; 106585dcf349SGleb Smirnoff uint8_t flags; 10668355f576SJeff Roberson int i; 10678355f576SJeff Roberson 1068ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 1069ab3185d1SJeff Roberson ("keg_alloc_slab: domain %d out of range", domain)); 1070bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 1071bb15d1c7SGleb Smirnoff MPASS(zone->uz_lockptr == &keg->uk_lock); 1072a553d4b8SJeff Roberson 1073e20a199fSJeff Roberson allocf = keg->uk_allocf; 1074e20a199fSJeff Roberson KEG_UNLOCK(keg); 1075a553d4b8SJeff Roberson 1076194a979eSMark Johnston slab = NULL; 1077194a979eSMark Johnston mem = NULL; 1078099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_OFFPAGE) { 1079ab3185d1SJeff Roberson slab = zone_alloc_item(keg->uk_slabzone, NULL, domain, wait); 1080fc03d22bSJeff Roberson if (slab == NULL) 1081fc03d22bSJeff Roberson goto out; 1082a553d4b8SJeff Roberson } 1083a553d4b8SJeff Roberson 10843370c5bfSJeff Roberson /* 10853370c5bfSJeff Roberson * This reproduces the old vm_zone behavior of zero filling pages the 10863370c5bfSJeff Roberson * first time they are added to a zone. 10873370c5bfSJeff Roberson * 10883370c5bfSJeff Roberson * Malloced items are zeroed in uma_zalloc. 10893370c5bfSJeff Roberson */ 10903370c5bfSJeff Roberson 1091099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0) 10923370c5bfSJeff Roberson wait |= M_ZERO; 10933370c5bfSJeff Roberson else 10943370c5bfSJeff Roberson wait &= ~M_ZERO; 10953370c5bfSJeff Roberson 1096263811f7SKip Macy if (keg->uk_flags & UMA_ZONE_NODUMP) 1097263811f7SKip Macy wait |= M_NODUMP; 1098263811f7SKip Macy 1099e20a199fSJeff Roberson /* zone is passed for legacy reasons. */ 1100194a979eSMark Johnston size = keg->uk_ppera * PAGE_SIZE; 1101ab3185d1SJeff Roberson mem = allocf(zone, size, domain, &flags, wait); 1102a553d4b8SJeff Roberson if (mem == NULL) { 1103b23f72e9SBrian Feldman if (keg->uk_flags & UMA_ZONE_OFFPAGE) 11040095a784SJeff Roberson zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE); 1105fc03d22bSJeff Roberson slab = NULL; 1106fc03d22bSJeff Roberson goto out; 1107a553d4b8SJeff Roberson } 11082e47807cSJeff Roberson uma_total_inc(size); 11098355f576SJeff Roberson 11105c0e403bSJeff Roberson /* Point the slab into the allocated memory */ 1111099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) 1112099a0e58SBosko Milekic slab = (uma_slab_t )(mem + keg->uk_pgoff); 11135c0e403bSJeff Roberson 1114e20a199fSJeff Roberson if (keg->uk_flags & UMA_ZONE_VTOSLAB) 1115099a0e58SBosko Milekic for (i = 0; i < keg->uk_ppera; i++) 111699571dc3SJeff Roberson vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab); 11178355f576SJeff Roberson 1118099a0e58SBosko Milekic slab->us_keg = keg; 11198355f576SJeff Roberson slab->us_data = mem; 1120099a0e58SBosko Milekic slab->us_freecount = keg->uk_ipers; 11218355f576SJeff Roberson slab->us_flags = flags; 1122ab3185d1SJeff Roberson slab->us_domain = domain; 1123ef72505eSJeff Roberson BIT_FILL(SLAB_SETSIZE, &slab->us_free); 1124ef72505eSJeff Roberson #ifdef INVARIANTS 1125ef72505eSJeff Roberson BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree); 1126ef72505eSJeff Roberson #endif 1127099a0e58SBosko Milekic 1128b23f72e9SBrian Feldman if (keg->uk_init != NULL) { 1129099a0e58SBosko Milekic for (i = 0; i < keg->uk_ipers; i++) 1130b23f72e9SBrian Feldman if (keg->uk_init(slab->us_data + (keg->uk_rsize * i), 1131b23f72e9SBrian Feldman keg->uk_size, wait) != 0) 1132b23f72e9SBrian Feldman break; 1133b23f72e9SBrian Feldman if (i != keg->uk_ipers) { 1134fc03d22bSJeff Roberson keg_free_slab(keg, slab, i); 1135fc03d22bSJeff Roberson slab = NULL; 1136fc03d22bSJeff Roberson goto out; 1137b23f72e9SBrian Feldman } 1138b23f72e9SBrian Feldman } 1139e20a199fSJeff Roberson KEG_LOCK(keg); 11405c0e403bSJeff Roberson 11411431a748SGleb Smirnoff CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)", 11421431a748SGleb Smirnoff slab, keg->uk_name, keg); 11431431a748SGleb Smirnoff 1144099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 1145099a0e58SBosko Milekic UMA_HASH_INSERT(&keg->uk_hash, slab, mem); 11468355f576SJeff Roberson 1147099a0e58SBosko Milekic keg->uk_pages += keg->uk_ppera; 1148099a0e58SBosko Milekic keg->uk_free += keg->uk_ipers; 11498355f576SJeff Roberson 1150194a979eSMark Johnston out: 11518355f576SJeff Roberson return (slab); 11528355f576SJeff Roberson } 11538355f576SJeff Roberson 11548355f576SJeff Roberson /* 1155009b6fcbSJeff Roberson * This function is intended to be used early on in place of page_alloc() so 1156009b6fcbSJeff Roberson * that we may use the boot time page cache to satisfy allocations before 1157009b6fcbSJeff Roberson * the VM is ready. 1158009b6fcbSJeff Roberson */ 1159009b6fcbSJeff Roberson static void * 1160ab3185d1SJeff Roberson startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1161ab3185d1SJeff Roberson int wait) 1162009b6fcbSJeff Roberson { 1163099a0e58SBosko Milekic uma_keg_t keg; 1164ac0a6fd0SGleb Smirnoff void *mem; 1165ac0a6fd0SGleb Smirnoff int pages; 1166099a0e58SBosko Milekic 1167bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1168009b6fcbSJeff Roberson /* 1169f7d35785SGleb Smirnoff * If we are in BOOT_BUCKETS or higher, than switch to real 1170f7d35785SGleb Smirnoff * allocator. Zones with page sized slabs switch at BOOT_PAGEALLOC. 1171009b6fcbSJeff Roberson */ 1172f7d35785SGleb Smirnoff switch (booted) { 1173f7d35785SGleb Smirnoff case BOOT_COLD: 1174f7d35785SGleb Smirnoff case BOOT_STRAPPED: 1175f7d35785SGleb Smirnoff break; 1176f7d35785SGleb Smirnoff case BOOT_PAGEALLOC: 1177f7d35785SGleb Smirnoff if (keg->uk_ppera > 1) 1178f7d35785SGleb Smirnoff break; 1179f7d35785SGleb Smirnoff case BOOT_BUCKETS: 1180f7d35785SGleb Smirnoff case BOOT_RUNNING: 1181009b6fcbSJeff Roberson #ifdef UMA_MD_SMALL_ALLOC 1182f7d35785SGleb Smirnoff keg->uk_allocf = (keg->uk_ppera > 1) ? 1183f7d35785SGleb Smirnoff page_alloc : uma_small_alloc; 1184009b6fcbSJeff Roberson #else 1185099a0e58SBosko Milekic keg->uk_allocf = page_alloc; 1186009b6fcbSJeff Roberson #endif 1187ab3185d1SJeff Roberson return keg->uk_allocf(zone, bytes, domain, pflag, wait); 1188009b6fcbSJeff Roberson } 1189009b6fcbSJeff Roberson 1190009b6fcbSJeff Roberson /* 1191f7d35785SGleb Smirnoff * Check our small startup cache to see if it has pages remaining. 1192f7d35785SGleb Smirnoff */ 1193f7d35785SGleb Smirnoff pages = howmany(bytes, PAGE_SIZE); 1194f7d35785SGleb Smirnoff KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__)); 1195f7d35785SGleb Smirnoff if (pages > boot_pages) 1196f7d35785SGleb Smirnoff panic("UMA zone \"%s\": Increase vm.boot_pages", zone->uz_name); 1197f7d35785SGleb Smirnoff #ifdef DIAGNOSTIC 1198f7d35785SGleb Smirnoff printf("%s from \"%s\", %d boot pages left\n", __func__, zone->uz_name, 1199f7d35785SGleb Smirnoff boot_pages); 1200f7d35785SGleb Smirnoff #endif 1201f7d35785SGleb Smirnoff mem = bootmem; 1202f7d35785SGleb Smirnoff boot_pages -= pages; 1203f7d35785SGleb Smirnoff bootmem += pages * PAGE_SIZE; 1204f7d35785SGleb Smirnoff *pflag = UMA_SLAB_BOOT; 1205f7d35785SGleb Smirnoff 1206f7d35785SGleb Smirnoff return (mem); 1207f7d35785SGleb Smirnoff } 1208f7d35785SGleb Smirnoff 1209f7d35785SGleb Smirnoff /* 12108355f576SJeff Roberson * Allocates a number of pages from the system 12118355f576SJeff Roberson * 12128355f576SJeff Roberson * Arguments: 12138355f576SJeff Roberson * bytes The number of bytes requested 12148355f576SJeff Roberson * wait Shall we wait? 12158355f576SJeff Roberson * 12168355f576SJeff Roberson * Returns: 12178355f576SJeff Roberson * A pointer to the alloced memory or possibly 12188355f576SJeff Roberson * NULL if M_NOWAIT is set. 12198355f576SJeff Roberson */ 12208355f576SJeff Roberson static void * 1221ab3185d1SJeff Roberson page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1222ab3185d1SJeff Roberson int wait) 12238355f576SJeff Roberson { 12248355f576SJeff Roberson void *p; /* Returned page */ 12258355f576SJeff Roberson 12262e47807cSJeff Roberson *pflag = UMA_SLAB_KERNEL; 12279978bd99SMark Johnston p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait); 12288355f576SJeff Roberson 12298355f576SJeff Roberson return (p); 12308355f576SJeff Roberson } 12318355f576SJeff Roberson 1232ab3059a8SMatt Macy static void * 1233ab3059a8SMatt Macy pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 1234ab3059a8SMatt Macy int wait) 1235ab3059a8SMatt Macy { 1236ab3059a8SMatt Macy struct pglist alloctail; 1237ab3059a8SMatt Macy vm_offset_t addr, zkva; 1238ab3059a8SMatt Macy int cpu, flags; 1239ab3059a8SMatt Macy vm_page_t p, p_next; 1240ab3059a8SMatt Macy #ifdef NUMA 1241ab3059a8SMatt Macy struct pcpu *pc; 1242ab3059a8SMatt Macy #endif 1243ab3059a8SMatt Macy 1244ab3059a8SMatt Macy MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE); 1245ab3059a8SMatt Macy 1246013072f0SMark Johnston TAILQ_INIT(&alloctail); 1247ab3059a8SMatt Macy flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1248013072f0SMark Johnston malloc2vm_flags(wait); 1249013072f0SMark Johnston *pflag = UMA_SLAB_KERNEL; 1250ab3059a8SMatt Macy for (cpu = 0; cpu <= mp_maxid; cpu++) { 1251ab3059a8SMatt Macy if (CPU_ABSENT(cpu)) { 1252ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1253ab3059a8SMatt Macy } else { 1254ab3059a8SMatt Macy #ifndef NUMA 1255ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1256ab3059a8SMatt Macy #else 1257ab3059a8SMatt Macy pc = pcpu_find(cpu); 1258ab3059a8SMatt Macy p = vm_page_alloc_domain(NULL, 0, pc->pc_domain, flags); 1259ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1260ab3059a8SMatt Macy p = vm_page_alloc(NULL, 0, flags); 1261ab3059a8SMatt Macy #endif 1262ab3059a8SMatt Macy } 1263ab3059a8SMatt Macy if (__predict_false(p == NULL)) 1264ab3059a8SMatt Macy goto fail; 1265ab3059a8SMatt Macy TAILQ_INSERT_TAIL(&alloctail, p, listq); 1266ab3059a8SMatt Macy } 1267ab3059a8SMatt Macy if ((addr = kva_alloc(bytes)) == 0) 1268ab3059a8SMatt Macy goto fail; 1269ab3059a8SMatt Macy zkva = addr; 1270ab3059a8SMatt Macy TAILQ_FOREACH(p, &alloctail, listq) { 1271ab3059a8SMatt Macy pmap_qenter(zkva, &p, 1); 1272ab3059a8SMatt Macy zkva += PAGE_SIZE; 1273ab3059a8SMatt Macy } 1274ab3059a8SMatt Macy return ((void*)addr); 1275ab3059a8SMatt Macy fail: 1276ab3059a8SMatt Macy TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 1277ab3059a8SMatt Macy vm_page_unwire(p, PQ_NONE); 1278ab3059a8SMatt Macy vm_page_free(p); 1279ab3059a8SMatt Macy } 1280ab3059a8SMatt Macy return (NULL); 1281ab3059a8SMatt Macy } 1282ab3059a8SMatt Macy 12838355f576SJeff Roberson /* 12848355f576SJeff Roberson * Allocates a number of pages from within an object 12858355f576SJeff Roberson * 12868355f576SJeff Roberson * Arguments: 12878355f576SJeff Roberson * bytes The number of bytes requested 12888355f576SJeff Roberson * wait Shall we wait? 12898355f576SJeff Roberson * 12908355f576SJeff Roberson * Returns: 12918355f576SJeff Roberson * A pointer to the alloced memory or possibly 12928355f576SJeff Roberson * NULL if M_NOWAIT is set. 12938355f576SJeff Roberson */ 12948355f576SJeff Roberson static void * 1295ab3185d1SJeff Roberson noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags, 1296ab3185d1SJeff Roberson int wait) 12978355f576SJeff Roberson { 1298a4915c21SAttilio Rao TAILQ_HEAD(, vm_page) alloctail; 1299a4915c21SAttilio Rao u_long npages; 1300b245ac95SAlan Cox vm_offset_t retkva, zkva; 1301a4915c21SAttilio Rao vm_page_t p, p_next; 1302e20a199fSJeff Roberson uma_keg_t keg; 13038355f576SJeff Roberson 1304a4915c21SAttilio Rao TAILQ_INIT(&alloctail); 1305bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 1306a4915c21SAttilio Rao 1307a4915c21SAttilio Rao npages = howmany(bytes, PAGE_SIZE); 1308a4915c21SAttilio Rao while (npages > 0) { 1309ab3185d1SJeff Roberson p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT | 13108d6fbbb8SJeff Roberson VM_ALLOC_WIRED | VM_ALLOC_NOOBJ | 1311772c8b67SKonstantin Belousov ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK : 1312772c8b67SKonstantin Belousov VM_ALLOC_NOWAIT)); 1313a4915c21SAttilio Rao if (p != NULL) { 1314a4915c21SAttilio Rao /* 1315a4915c21SAttilio Rao * Since the page does not belong to an object, its 1316a4915c21SAttilio Rao * listq is unused. 1317a4915c21SAttilio Rao */ 1318a4915c21SAttilio Rao TAILQ_INSERT_TAIL(&alloctail, p, listq); 1319a4915c21SAttilio Rao npages--; 1320a4915c21SAttilio Rao continue; 1321a4915c21SAttilio Rao } 13228355f576SJeff Roberson /* 1323a4915c21SAttilio Rao * Page allocation failed, free intermediate pages and 1324a4915c21SAttilio Rao * exit. 13258355f576SJeff Roberson */ 1326a4915c21SAttilio Rao TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) { 1327087a6132SAlan Cox vm_page_unwire(p, PQ_NONE); 1328b245ac95SAlan Cox vm_page_free(p); 1329b245ac95SAlan Cox } 1330a4915c21SAttilio Rao return (NULL); 1331b245ac95SAlan Cox } 13328355f576SJeff Roberson *flags = UMA_SLAB_PRIV; 1333a4915c21SAttilio Rao zkva = keg->uk_kva + 1334a4915c21SAttilio Rao atomic_fetchadd_long(&keg->uk_offset, round_page(bytes)); 1335a4915c21SAttilio Rao retkva = zkva; 1336a4915c21SAttilio Rao TAILQ_FOREACH(p, &alloctail, listq) { 1337a4915c21SAttilio Rao pmap_qenter(zkva, &p, 1); 1338a4915c21SAttilio Rao zkva += PAGE_SIZE; 1339a4915c21SAttilio Rao } 13408355f576SJeff Roberson 13418355f576SJeff Roberson return ((void *)retkva); 13428355f576SJeff Roberson } 13438355f576SJeff Roberson 13448355f576SJeff Roberson /* 13458355f576SJeff Roberson * Frees a number of pages to the system 13468355f576SJeff Roberson * 13478355f576SJeff Roberson * Arguments: 13488355f576SJeff Roberson * mem A pointer to the memory to be freed 13498355f576SJeff Roberson * size The size of the memory being freed 13508355f576SJeff Roberson * flags The original p->us_flags field 13518355f576SJeff Roberson * 13528355f576SJeff Roberson * Returns: 13538355f576SJeff Roberson * Nothing 13548355f576SJeff Roberson */ 13558355f576SJeff Roberson static void 1356f2c2231eSRyan Stone page_free(void *mem, vm_size_t size, uint8_t flags) 13578355f576SJeff Roberson { 13583370c5bfSJeff Roberson 135949bfa624SAlan Cox if ((flags & UMA_SLAB_KERNEL) == 0) 1360b5345ef1SJustin Hibbits panic("UMA: page_free used with invalid flags %x", flags); 13618355f576SJeff Roberson 136249bfa624SAlan Cox kmem_free((vm_offset_t)mem, size); 13638355f576SJeff Roberson } 13648355f576SJeff Roberson 13658355f576SJeff Roberson /* 1366ab3059a8SMatt Macy * Frees pcpu zone allocations 1367ab3059a8SMatt Macy * 1368ab3059a8SMatt Macy * Arguments: 1369ab3059a8SMatt Macy * mem A pointer to the memory to be freed 1370ab3059a8SMatt Macy * size The size of the memory being freed 1371ab3059a8SMatt Macy * flags The original p->us_flags field 1372ab3059a8SMatt Macy * 1373ab3059a8SMatt Macy * Returns: 1374ab3059a8SMatt Macy * Nothing 1375ab3059a8SMatt Macy */ 1376ab3059a8SMatt Macy static void 1377ab3059a8SMatt Macy pcpu_page_free(void *mem, vm_size_t size, uint8_t flags) 1378ab3059a8SMatt Macy { 1379ab3059a8SMatt Macy vm_offset_t sva, curva; 1380ab3059a8SMatt Macy vm_paddr_t paddr; 1381ab3059a8SMatt Macy vm_page_t m; 1382ab3059a8SMatt Macy 1383ab3059a8SMatt Macy MPASS(size == (mp_maxid+1)*PAGE_SIZE); 1384ab3059a8SMatt Macy sva = (vm_offset_t)mem; 1385ab3059a8SMatt Macy for (curva = sva; curva < sva + size; curva += PAGE_SIZE) { 1386ab3059a8SMatt Macy paddr = pmap_kextract(curva); 1387ab3059a8SMatt Macy m = PHYS_TO_VM_PAGE(paddr); 1388ab3059a8SMatt Macy vm_page_unwire(m, PQ_NONE); 1389ab3059a8SMatt Macy vm_page_free(m); 1390ab3059a8SMatt Macy } 1391ab3059a8SMatt Macy pmap_qremove(sva, size >> PAGE_SHIFT); 1392ab3059a8SMatt Macy kva_free(sva, size); 1393ab3059a8SMatt Macy } 1394ab3059a8SMatt Macy 1395ab3059a8SMatt Macy 1396ab3059a8SMatt Macy /* 13978355f576SJeff Roberson * Zero fill initializer 13988355f576SJeff Roberson * 13998355f576SJeff Roberson * Arguments/Returns follow uma_init specifications 14008355f576SJeff Roberson */ 1401b23f72e9SBrian Feldman static int 1402b23f72e9SBrian Feldman zero_init(void *mem, int size, int flags) 14038355f576SJeff Roberson { 14048355f576SJeff Roberson bzero(mem, size); 1405b23f72e9SBrian Feldman return (0); 14068355f576SJeff Roberson } 14078355f576SJeff Roberson 14088355f576SJeff Roberson /* 1409e20a199fSJeff Roberson * Finish creating a small uma keg. This calculates ipers, and the keg size. 14108355f576SJeff Roberson * 14118355f576SJeff Roberson * Arguments 1412e20a199fSJeff Roberson * keg The zone we should initialize 14138355f576SJeff Roberson * 14148355f576SJeff Roberson * Returns 14158355f576SJeff Roberson * Nothing 14168355f576SJeff Roberson */ 14178355f576SJeff Roberson static void 1418e20a199fSJeff Roberson keg_small_init(uma_keg_t keg) 14198355f576SJeff Roberson { 1420244f4554SBosko Milekic u_int rsize; 1421244f4554SBosko Milekic u_int memused; 1422244f4554SBosko Milekic u_int wastedspace; 1423244f4554SBosko Milekic u_int shsize; 1424a55ebb7cSAndriy Gapon u_int slabsize; 14258355f576SJeff Roberson 1426ad97af7eSGleb Smirnoff if (keg->uk_flags & UMA_ZONE_PCPU) { 142796c85efbSNathan Whitehorn u_int ncpus = (mp_maxid + 1) ? (mp_maxid + 1) : MAXCPU; 1428e28a647dSGleb Smirnoff 1429ab3059a8SMatt Macy slabsize = UMA_PCPU_ALLOC_SIZE; 1430ab3059a8SMatt Macy keg->uk_ppera = ncpus; 1431ad97af7eSGleb Smirnoff } else { 1432a55ebb7cSAndriy Gapon slabsize = UMA_SLAB_SIZE; 1433ad97af7eSGleb Smirnoff keg->uk_ppera = 1; 1434ad97af7eSGleb Smirnoff } 1435ad97af7eSGleb Smirnoff 1436ef72505eSJeff Roberson /* 1437ef72505eSJeff Roberson * Calculate the size of each allocation (rsize) according to 1438ef72505eSJeff Roberson * alignment. If the requested size is smaller than we have 1439ef72505eSJeff Roberson * allocation bits for we round it up. 1440ef72505eSJeff Roberson */ 1441099a0e58SBosko Milekic rsize = keg->uk_size; 1442a55ebb7cSAndriy Gapon if (rsize < slabsize / SLAB_SETSIZE) 1443a55ebb7cSAndriy Gapon rsize = slabsize / SLAB_SETSIZE; 1444099a0e58SBosko Milekic if (rsize & keg->uk_align) 1445099a0e58SBosko Milekic rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1); 1446099a0e58SBosko Milekic keg->uk_rsize = rsize; 1447ad97af7eSGleb Smirnoff 1448ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 || 1449ab3059a8SMatt Macy keg->uk_rsize < UMA_PCPU_ALLOC_SIZE, 1450ad97af7eSGleb Smirnoff ("%s: size %u too large", __func__, keg->uk_rsize)); 14518355f576SJeff Roberson 1452ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_OFFPAGE) 14532864dbbfSGleb Smirnoff shsize = 0; 1454ef72505eSJeff Roberson else 14553d5e3df7SGleb Smirnoff shsize = SIZEOF_UMA_SLAB; 14568355f576SJeff Roberson 14571ca6ed45SGleb Smirnoff if (rsize <= slabsize - shsize) 1458a55ebb7cSAndriy Gapon keg->uk_ipers = (slabsize - shsize) / rsize; 14591ca6ed45SGleb Smirnoff else { 14601ca6ed45SGleb Smirnoff /* Handle special case when we have 1 item per slab, so 14611ca6ed45SGleb Smirnoff * alignment requirement can be relaxed. */ 14621ca6ed45SGleb Smirnoff KASSERT(keg->uk_size <= slabsize - shsize, 14631ca6ed45SGleb Smirnoff ("%s: size %u greater than slab", __func__, keg->uk_size)); 14641ca6ed45SGleb Smirnoff keg->uk_ipers = 1; 14651ca6ed45SGleb Smirnoff } 1466ef72505eSJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE, 1467ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 1468ad97af7eSGleb Smirnoff 1469244f4554SBosko Milekic memused = keg->uk_ipers * rsize + shsize; 1470a55ebb7cSAndriy Gapon wastedspace = slabsize - memused; 1471244f4554SBosko Milekic 147220e8e865SBosko Milekic /* 1473244f4554SBosko Milekic * We can't do OFFPAGE if we're internal or if we've been 147420e8e865SBosko Milekic * asked to not go to the VM for buckets. If we do this we 14756fd34d6fSJeff Roberson * may end up going to the VM for slabs which we do not 14766fd34d6fSJeff Roberson * want to do if we're UMA_ZFLAG_CACHEONLY as a result 14776fd34d6fSJeff Roberson * of UMA_ZONE_VM, which clearly forbids it. 147820e8e865SBosko Milekic */ 1479099a0e58SBosko Milekic if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) || 1480099a0e58SBosko Milekic (keg->uk_flags & UMA_ZFLAG_CACHEONLY)) 14818355f576SJeff Roberson return; 1482244f4554SBosko Milekic 1483ef72505eSJeff Roberson /* 1484ef72505eSJeff Roberson * See if using an OFFPAGE slab will limit our waste. Only do 1485ef72505eSJeff Roberson * this if it permits more items per-slab. 1486ef72505eSJeff Roberson * 1487ef72505eSJeff Roberson * XXX We could try growing slabsize to limit max waste as well. 1488ef72505eSJeff Roberson * Historically this was not done because the VM could not 1489ef72505eSJeff Roberson * efficiently handle contiguous allocations. 1490ef72505eSJeff Roberson */ 1491a55ebb7cSAndriy Gapon if ((wastedspace >= slabsize / UMA_MAX_WASTE) && 1492a55ebb7cSAndriy Gapon (keg->uk_ipers < (slabsize / keg->uk_rsize))) { 1493a55ebb7cSAndriy Gapon keg->uk_ipers = slabsize / keg->uk_rsize; 1494ef72505eSJeff Roberson KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE, 1495ad97af7eSGleb Smirnoff ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers)); 14961431a748SGleb Smirnoff CTR6(KTR_UMA, "UMA decided we need offpage slab headers for " 14971431a748SGleb Smirnoff "keg: %s(%p), calculated wastedspace = %d, " 1498244f4554SBosko Milekic "maximum wasted space allowed = %d, " 1499244f4554SBosko Milekic "calculated ipers = %d, " 15001431a748SGleb Smirnoff "new wasted space = %d\n", keg->uk_name, keg, wastedspace, 1501a55ebb7cSAndriy Gapon slabsize / UMA_MAX_WASTE, keg->uk_ipers, 1502a55ebb7cSAndriy Gapon slabsize - keg->uk_ipers * keg->uk_rsize); 1503099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_OFFPAGE; 15048355f576SJeff Roberson } 1505ad97af7eSGleb Smirnoff 1506ad97af7eSGleb Smirnoff if ((keg->uk_flags & UMA_ZONE_OFFPAGE) && 1507ad97af7eSGleb Smirnoff (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1508ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_HASH; 15098355f576SJeff Roberson } 15108355f576SJeff Roberson 15118355f576SJeff Roberson /* 1512e20a199fSJeff Roberson * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do 15138355f576SJeff Roberson * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be 15148355f576SJeff Roberson * more complicated. 15158355f576SJeff Roberson * 15168355f576SJeff Roberson * Arguments 1517e20a199fSJeff Roberson * keg The keg we should initialize 15188355f576SJeff Roberson * 15198355f576SJeff Roberson * Returns 15208355f576SJeff Roberson * Nothing 15218355f576SJeff Roberson */ 15228355f576SJeff Roberson static void 1523e20a199fSJeff Roberson keg_large_init(uma_keg_t keg) 15248355f576SJeff Roberson { 15258355f576SJeff Roberson 1526e20a199fSJeff Roberson KASSERT(keg != NULL, ("Keg is null in keg_large_init")); 1527ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1528ad97af7eSGleb Smirnoff ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__)); 152920e8e865SBosko Milekic 1530ad97af7eSGleb Smirnoff keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE); 1531099a0e58SBosko Milekic keg->uk_ipers = 1; 1532e9a069d8SJohn Baldwin keg->uk_rsize = keg->uk_size; 1533e9a069d8SJohn Baldwin 1534cec48e00SAlexander Motin /* Check whether we have enough space to not do OFFPAGE. */ 15353d5e3df7SGleb Smirnoff if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0 && 15363d5e3df7SGleb Smirnoff PAGE_SIZE * keg->uk_ppera - keg->uk_rsize < SIZEOF_UMA_SLAB) { 15372934eb8aSMark Johnston /* 15382934eb8aSMark Johnston * We can't do OFFPAGE if we're internal, in which case 15392934eb8aSMark Johnston * we need an extra page per allocation to contain the 15402934eb8aSMark Johnston * slab header. 15412934eb8aSMark Johnston */ 15422934eb8aSMark Johnston if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) == 0) 1543099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_OFFPAGE; 15442934eb8aSMark Johnston else 15452934eb8aSMark Johnston keg->uk_ppera++; 15462934eb8aSMark Johnston } 1547cec48e00SAlexander Motin 1548cec48e00SAlexander Motin if ((keg->uk_flags & UMA_ZONE_OFFPAGE) && 1549cec48e00SAlexander Motin (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0) 1550099a0e58SBosko Milekic keg->uk_flags |= UMA_ZONE_HASH; 15518355f576SJeff Roberson } 15528355f576SJeff Roberson 1553e20a199fSJeff Roberson static void 1554e20a199fSJeff Roberson keg_cachespread_init(uma_keg_t keg) 1555e20a199fSJeff Roberson { 1556e20a199fSJeff Roberson int alignsize; 1557e20a199fSJeff Roberson int trailer; 1558e20a199fSJeff Roberson int pages; 1559e20a199fSJeff Roberson int rsize; 1560e20a199fSJeff Roberson 1561ad97af7eSGleb Smirnoff KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0, 1562ad97af7eSGleb Smirnoff ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__)); 1563ad97af7eSGleb Smirnoff 1564e20a199fSJeff Roberson alignsize = keg->uk_align + 1; 1565e20a199fSJeff Roberson rsize = keg->uk_size; 1566e20a199fSJeff Roberson /* 1567e20a199fSJeff Roberson * We want one item to start on every align boundary in a page. To 1568e20a199fSJeff Roberson * do this we will span pages. We will also extend the item by the 1569e20a199fSJeff Roberson * size of align if it is an even multiple of align. Otherwise, it 1570e20a199fSJeff Roberson * would fall on the same boundary every time. 1571e20a199fSJeff Roberson */ 1572e20a199fSJeff Roberson if (rsize & keg->uk_align) 1573e20a199fSJeff Roberson rsize = (rsize & ~keg->uk_align) + alignsize; 1574e20a199fSJeff Roberson if ((rsize & alignsize) == 0) 1575e20a199fSJeff Roberson rsize += alignsize; 1576e20a199fSJeff Roberson trailer = rsize - keg->uk_size; 1577e20a199fSJeff Roberson pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE; 1578e20a199fSJeff Roberson pages = MIN(pages, (128 * 1024) / PAGE_SIZE); 1579e20a199fSJeff Roberson keg->uk_rsize = rsize; 1580e20a199fSJeff Roberson keg->uk_ppera = pages; 1581e20a199fSJeff Roberson keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize; 1582e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB; 15832367b4ddSDimitry Andric KASSERT(keg->uk_ipers <= SLAB_SETSIZE, 158442321809SGleb Smirnoff ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__, 1585e20a199fSJeff Roberson keg->uk_ipers)); 1586e20a199fSJeff Roberson } 1587e20a199fSJeff Roberson 15888355f576SJeff Roberson /* 1589099a0e58SBosko Milekic * Keg header ctor. This initializes all fields, locks, etc. And inserts 1590099a0e58SBosko Milekic * the keg onto the global keg list. 15918355f576SJeff Roberson * 15928355f576SJeff Roberson * Arguments/Returns follow uma_ctor specifications 1593099a0e58SBosko Milekic * udata Actually uma_kctor_args 1594099a0e58SBosko Milekic */ 1595b23f72e9SBrian Feldman static int 1596b23f72e9SBrian Feldman keg_ctor(void *mem, int size, void *udata, int flags) 1597099a0e58SBosko Milekic { 1598099a0e58SBosko Milekic struct uma_kctor_args *arg = udata; 1599099a0e58SBosko Milekic uma_keg_t keg = mem; 1600099a0e58SBosko Milekic uma_zone_t zone; 1601099a0e58SBosko Milekic 1602099a0e58SBosko Milekic bzero(keg, size); 1603099a0e58SBosko Milekic keg->uk_size = arg->size; 1604099a0e58SBosko Milekic keg->uk_init = arg->uminit; 1605099a0e58SBosko Milekic keg->uk_fini = arg->fini; 1606099a0e58SBosko Milekic keg->uk_align = arg->align; 1607099a0e58SBosko Milekic keg->uk_free = 0; 16086fd34d6fSJeff Roberson keg->uk_reserve = 0; 1609099a0e58SBosko Milekic keg->uk_pages = 0; 1610099a0e58SBosko Milekic keg->uk_flags = arg->flags; 1611099a0e58SBosko Milekic keg->uk_slabzone = NULL; 1612099a0e58SBosko Milekic 1613099a0e58SBosko Milekic /* 1614194a979eSMark Johnston * We use a global round-robin policy by default. Zones with 1615194a979eSMark Johnston * UMA_ZONE_NUMA set will use first-touch instead, in which case the 1616194a979eSMark Johnston * iterator is never run. 1617194a979eSMark Johnston */ 1618194a979eSMark Johnston keg->uk_dr.dr_policy = DOMAINSET_RR(); 1619194a979eSMark Johnston keg->uk_dr.dr_iter = 0; 1620194a979eSMark Johnston 1621194a979eSMark Johnston /* 1622099a0e58SBosko Milekic * The master zone is passed to us at keg-creation time. 1623099a0e58SBosko Milekic */ 1624099a0e58SBosko Milekic zone = arg->zone; 1625e20a199fSJeff Roberson keg->uk_name = zone->uz_name; 1626099a0e58SBosko Milekic 1627099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_VM) 1628099a0e58SBosko Milekic keg->uk_flags |= UMA_ZFLAG_CACHEONLY; 1629099a0e58SBosko Milekic 1630099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_ZINIT) 1631099a0e58SBosko Milekic keg->uk_init = zero_init; 1632099a0e58SBosko Milekic 1633cfcae3f8SGleb Smirnoff if (arg->flags & UMA_ZONE_MALLOC) 1634e20a199fSJeff Roberson keg->uk_flags |= UMA_ZONE_VTOSLAB; 1635e20a199fSJeff Roberson 1636ad97af7eSGleb Smirnoff if (arg->flags & UMA_ZONE_PCPU) 1637ad97af7eSGleb Smirnoff #ifdef SMP 1638ad97af7eSGleb Smirnoff keg->uk_flags |= UMA_ZONE_OFFPAGE; 1639ad97af7eSGleb Smirnoff #else 1640ad97af7eSGleb Smirnoff keg->uk_flags &= ~UMA_ZONE_PCPU; 1641ad97af7eSGleb Smirnoff #endif 1642ad97af7eSGleb Smirnoff 1643ef72505eSJeff Roberson if (keg->uk_flags & UMA_ZONE_CACHESPREAD) { 1644e20a199fSJeff Roberson keg_cachespread_init(keg); 1645244f4554SBosko Milekic } else { 1646b92b26adSGleb Smirnoff if (keg->uk_size > UMA_SLAB_SPACE) 1647e20a199fSJeff Roberson keg_large_init(keg); 1648244f4554SBosko Milekic else 1649e20a199fSJeff Roberson keg_small_init(keg); 1650244f4554SBosko Milekic } 1651099a0e58SBosko Milekic 1652cfcae3f8SGleb Smirnoff if (keg->uk_flags & UMA_ZONE_OFFPAGE) 1653099a0e58SBosko Milekic keg->uk_slabzone = slabzone; 1654099a0e58SBosko Milekic 1655099a0e58SBosko Milekic /* 1656099a0e58SBosko Milekic * If we haven't booted yet we need allocations to go through the 1657099a0e58SBosko Milekic * startup cache until the vm is ready. 1658099a0e58SBosko Milekic */ 1659f4bef67cSGleb Smirnoff if (booted < BOOT_PAGEALLOC) 16608cd02d00SAlan Cox keg->uk_allocf = startup_alloc; 166177e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 166277e19437SGleb Smirnoff else if (keg->uk_ppera == 1) 166377e19437SGleb Smirnoff keg->uk_allocf = uma_small_alloc; 16648cd02d00SAlan Cox #endif 1665ab3059a8SMatt Macy else if (keg->uk_flags & UMA_ZONE_PCPU) 1666ab3059a8SMatt Macy keg->uk_allocf = pcpu_page_alloc; 166777e19437SGleb Smirnoff else 166877e19437SGleb Smirnoff keg->uk_allocf = page_alloc; 166977e19437SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 167077e19437SGleb Smirnoff if (keg->uk_ppera == 1) 167177e19437SGleb Smirnoff keg->uk_freef = uma_small_free; 167277e19437SGleb Smirnoff else 167377e19437SGleb Smirnoff #endif 1674ab3059a8SMatt Macy if (keg->uk_flags & UMA_ZONE_PCPU) 1675ab3059a8SMatt Macy keg->uk_freef = pcpu_page_free; 1676ab3059a8SMatt Macy else 167777e19437SGleb Smirnoff keg->uk_freef = page_free; 1678099a0e58SBosko Milekic 1679099a0e58SBosko Milekic /* 1680af526374SJeff Roberson * Initialize keg's lock 1681099a0e58SBosko Milekic */ 1682af526374SJeff Roberson KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS)); 1683099a0e58SBosko Milekic 1684099a0e58SBosko Milekic /* 1685099a0e58SBosko Milekic * If we're putting the slab header in the actual page we need to 16863d5e3df7SGleb Smirnoff * figure out where in each page it goes. See SIZEOF_UMA_SLAB 16873d5e3df7SGleb Smirnoff * macro definition. 1688099a0e58SBosko Milekic */ 1689099a0e58SBosko Milekic if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) { 16903d5e3df7SGleb Smirnoff keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - SIZEOF_UMA_SLAB; 1691244f4554SBosko Milekic /* 1692244f4554SBosko Milekic * The only way the following is possible is if with our 1693244f4554SBosko Milekic * UMA_ALIGN_PTR adjustments we are now bigger than 1694244f4554SBosko Milekic * UMA_SLAB_SIZE. I haven't checked whether this is 1695244f4554SBosko Milekic * mathematically possible for all cases, so we make 1696244f4554SBosko Milekic * sure here anyway. 1697244f4554SBosko Milekic */ 16983d5e3df7SGleb Smirnoff KASSERT(keg->uk_pgoff + sizeof(struct uma_slab) <= 16993d5e3df7SGleb Smirnoff PAGE_SIZE * keg->uk_ppera, 17003d5e3df7SGleb Smirnoff ("zone %s ipers %d rsize %d size %d slab won't fit", 17013d5e3df7SGleb Smirnoff zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size)); 1702099a0e58SBosko Milekic } 1703099a0e58SBosko Milekic 1704099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZONE_HASH) 1705099a0e58SBosko Milekic hash_alloc(&keg->uk_hash); 1706099a0e58SBosko Milekic 17071431a748SGleb Smirnoff CTR5(KTR_UMA, "keg_ctor %p zone %s(%p) out %d free %d\n", 17081431a748SGleb Smirnoff keg, zone->uz_name, zone, 170957223e99SAndriy Gapon (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free, 171057223e99SAndriy Gapon keg->uk_free); 1711099a0e58SBosko Milekic 1712099a0e58SBosko Milekic LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link); 1713099a0e58SBosko Milekic 1714111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 1715099a0e58SBosko Milekic LIST_INSERT_HEAD(&uma_kegs, keg, uk_link); 1716111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 1717b23f72e9SBrian Feldman return (0); 1718099a0e58SBosko Milekic } 1719099a0e58SBosko Milekic 17202efcc8cbSGleb Smirnoff static void 17212efcc8cbSGleb Smirnoff zone_alloc_counters(uma_zone_t zone) 17222efcc8cbSGleb Smirnoff { 17232efcc8cbSGleb Smirnoff 17242efcc8cbSGleb Smirnoff zone->uz_allocs = counter_u64_alloc(M_WAITOK); 17252efcc8cbSGleb Smirnoff zone->uz_frees = counter_u64_alloc(M_WAITOK); 17262efcc8cbSGleb Smirnoff zone->uz_fails = counter_u64_alloc(M_WAITOK); 17272efcc8cbSGleb Smirnoff } 17282efcc8cbSGleb Smirnoff 1729099a0e58SBosko Milekic /* 1730099a0e58SBosko Milekic * Zone header ctor. This initializes all fields, locks, etc. 1731099a0e58SBosko Milekic * 1732099a0e58SBosko Milekic * Arguments/Returns follow uma_ctor specifications 1733099a0e58SBosko Milekic * udata Actually uma_zctor_args 17348355f576SJeff Roberson */ 1735b23f72e9SBrian Feldman static int 1736b23f72e9SBrian Feldman zone_ctor(void *mem, int size, void *udata, int flags) 17378355f576SJeff Roberson { 17388355f576SJeff Roberson struct uma_zctor_args *arg = udata; 17398355f576SJeff Roberson uma_zone_t zone = mem; 1740099a0e58SBosko Milekic uma_zone_t z; 1741099a0e58SBosko Milekic uma_keg_t keg; 17428355f576SJeff Roberson 17438355f576SJeff Roberson bzero(zone, size); 17448355f576SJeff Roberson zone->uz_name = arg->name; 17458355f576SJeff Roberson zone->uz_ctor = arg->ctor; 17468355f576SJeff Roberson zone->uz_dtor = arg->dtor; 1747e20a199fSJeff Roberson zone->uz_slab = zone_fetch_slab; 1748099a0e58SBosko Milekic zone->uz_init = NULL; 1749099a0e58SBosko Milekic zone->uz_fini = NULL; 1750bf965959SSean Bruno zone->uz_sleeps = 0; 1751fc03d22bSJeff Roberson zone->uz_count = 0; 1752ace66b56SAlexander Motin zone->uz_count_min = 0; 1753bb15d1c7SGleb Smirnoff zone->uz_count_max = BUCKET_MAX; 1754e20a199fSJeff Roberson zone->uz_flags = 0; 17552f891cd5SPawel Jakub Dawidek zone->uz_warning = NULL; 1756ab3185d1SJeff Roberson /* The domain structures follow the cpu structures. */ 1757ab3185d1SJeff Roberson zone->uz_domain = (struct uma_zone_domain *)&zone->uz_cpu[mp_ncpus]; 1758bb15d1c7SGleb Smirnoff zone->uz_bkt_max = ULONG_MAX; 17592f891cd5SPawel Jakub Dawidek timevalclear(&zone->uz_ratecheck); 1760af526374SJeff Roberson 17612efcc8cbSGleb Smirnoff if (__predict_true(booted == BOOT_RUNNING)) 17622efcc8cbSGleb Smirnoff zone_alloc_counters(zone); 17632efcc8cbSGleb Smirnoff else { 17642efcc8cbSGleb Smirnoff zone->uz_allocs = EARLY_COUNTER; 17652efcc8cbSGleb Smirnoff zone->uz_frees = EARLY_COUNTER; 17662efcc8cbSGleb Smirnoff zone->uz_fails = EARLY_COUNTER; 17672efcc8cbSGleb Smirnoff } 17682efcc8cbSGleb Smirnoff 17690095a784SJeff Roberson /* 17700095a784SJeff Roberson * This is a pure cache zone, no kegs. 17710095a784SJeff Roberson */ 17720095a784SJeff Roberson if (arg->import) { 17736fd34d6fSJeff Roberson if (arg->flags & UMA_ZONE_VM) 17746fd34d6fSJeff Roberson arg->flags |= UMA_ZFLAG_CACHEONLY; 17756fd34d6fSJeff Roberson zone->uz_flags = arg->flags; 1776af526374SJeff Roberson zone->uz_size = arg->size; 17770095a784SJeff Roberson zone->uz_import = arg->import; 17780095a784SJeff Roberson zone->uz_release = arg->release; 17790095a784SJeff Roberson zone->uz_arg = arg->arg; 1780af526374SJeff Roberson zone->uz_lockptr = &zone->uz_lock; 1781bb15d1c7SGleb Smirnoff ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS)); 1782111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 178303175483SAlexander Motin LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link); 1784111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 1785af526374SJeff Roberson goto out; 17860095a784SJeff Roberson } 17870095a784SJeff Roberson 17880095a784SJeff Roberson /* 17890095a784SJeff Roberson * Use the regular zone/keg/slab allocator. 17900095a784SJeff Roberson */ 17910095a784SJeff Roberson zone->uz_import = (uma_import)zone_import; 17920095a784SJeff Roberson zone->uz_release = (uma_release)zone_release; 17930095a784SJeff Roberson zone->uz_arg = zone; 1794bb15d1c7SGleb Smirnoff keg = arg->keg; 17950095a784SJeff Roberson 1796099a0e58SBosko Milekic if (arg->flags & UMA_ZONE_SECONDARY) { 1797099a0e58SBosko Milekic KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg")); 17988355f576SJeff Roberson zone->uz_init = arg->uminit; 1799e221e841SJeff Roberson zone->uz_fini = arg->fini; 1800af526374SJeff Roberson zone->uz_lockptr = &keg->uk_lock; 1801e20a199fSJeff Roberson zone->uz_flags |= UMA_ZONE_SECONDARY; 1802111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 1803099a0e58SBosko Milekic ZONE_LOCK(zone); 1804099a0e58SBosko Milekic LIST_FOREACH(z, &keg->uk_zones, uz_link) { 1805099a0e58SBosko Milekic if (LIST_NEXT(z, uz_link) == NULL) { 1806099a0e58SBosko Milekic LIST_INSERT_AFTER(z, zone, uz_link); 1807099a0e58SBosko Milekic break; 1808099a0e58SBosko Milekic } 1809099a0e58SBosko Milekic } 1810099a0e58SBosko Milekic ZONE_UNLOCK(zone); 1811111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 1812e20a199fSJeff Roberson } else if (keg == NULL) { 1813e20a199fSJeff Roberson if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini, 1814e20a199fSJeff Roberson arg->align, arg->flags)) == NULL) 1815b23f72e9SBrian Feldman return (ENOMEM); 1816099a0e58SBosko Milekic } else { 1817099a0e58SBosko Milekic struct uma_kctor_args karg; 1818b23f72e9SBrian Feldman int error; 1819099a0e58SBosko Milekic 1820099a0e58SBosko Milekic /* We should only be here from uma_startup() */ 1821099a0e58SBosko Milekic karg.size = arg->size; 1822099a0e58SBosko Milekic karg.uminit = arg->uminit; 1823099a0e58SBosko Milekic karg.fini = arg->fini; 1824099a0e58SBosko Milekic karg.align = arg->align; 1825099a0e58SBosko Milekic karg.flags = arg->flags; 1826099a0e58SBosko Milekic karg.zone = zone; 1827b23f72e9SBrian Feldman error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg, 1828b23f72e9SBrian Feldman flags); 1829b23f72e9SBrian Feldman if (error) 1830b23f72e9SBrian Feldman return (error); 1831099a0e58SBosko Milekic } 18320095a784SJeff Roberson 1833bb15d1c7SGleb Smirnoff zone->uz_keg = keg; 1834e20a199fSJeff Roberson zone->uz_size = keg->uk_size; 1835e20a199fSJeff Roberson zone->uz_flags |= (keg->uk_flags & 1836e20a199fSJeff Roberson (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT)); 18378355f576SJeff Roberson 18388355f576SJeff Roberson /* 18398355f576SJeff Roberson * Some internal zones don't have room allocated for the per cpu 18408355f576SJeff Roberson * caches. If we're internal, bail out here. 18418355f576SJeff Roberson */ 1842099a0e58SBosko Milekic if (keg->uk_flags & UMA_ZFLAG_INTERNAL) { 1843e20a199fSJeff Roberson KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0, 1844099a0e58SBosko Milekic ("Secondary zone requested UMA_ZFLAG_INTERNAL")); 1845b23f72e9SBrian Feldman return (0); 1846099a0e58SBosko Milekic } 18478355f576SJeff Roberson 1848af526374SJeff Roberson out: 18497e28037aSMark Johnston KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) != 18507e28037aSMark Johnston (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET), 18517e28037aSMark Johnston ("Invalid zone flag combination")); 18527e28037aSMark Johnston if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) 1853cae33c14SJeff Roberson zone->uz_count = BUCKET_MAX; 18547e28037aSMark Johnston else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0) 18557e28037aSMark Johnston zone->uz_count = 0; 18567e28037aSMark Johnston else 18577e28037aSMark Johnston zone->uz_count = bucket_select(zone->uz_size); 1858ace66b56SAlexander Motin zone->uz_count_min = zone->uz_count; 1859fc03d22bSJeff Roberson 1860b23f72e9SBrian Feldman return (0); 18618355f576SJeff Roberson } 18628355f576SJeff Roberson 18638355f576SJeff Roberson /* 1864099a0e58SBosko Milekic * Keg header dtor. This frees all data, destroys locks, frees the hash 1865099a0e58SBosko Milekic * table and removes the keg from the global list. 18669c2cd7e5SJeff Roberson * 18679c2cd7e5SJeff Roberson * Arguments/Returns follow uma_dtor specifications 18689c2cd7e5SJeff Roberson * udata unused 18699c2cd7e5SJeff Roberson */ 1870099a0e58SBosko Milekic static void 1871099a0e58SBosko Milekic keg_dtor(void *arg, int size, void *udata) 1872099a0e58SBosko Milekic { 1873099a0e58SBosko Milekic uma_keg_t keg; 18749c2cd7e5SJeff Roberson 1875099a0e58SBosko Milekic keg = (uma_keg_t)arg; 1876e20a199fSJeff Roberson KEG_LOCK(keg); 1877099a0e58SBosko Milekic if (keg->uk_free != 0) { 1878a3845534SCraig Rodrigues printf("Freed UMA keg (%s) was not empty (%d items). " 1879099a0e58SBosko Milekic " Lost %d pages of memory.\n", 1880a3845534SCraig Rodrigues keg->uk_name ? keg->uk_name : "", 1881099a0e58SBosko Milekic keg->uk_free, keg->uk_pages); 1882099a0e58SBosko Milekic } 1883e20a199fSJeff Roberson KEG_UNLOCK(keg); 1884099a0e58SBosko Milekic 1885099a0e58SBosko Milekic hash_free(&keg->uk_hash); 1886099a0e58SBosko Milekic 1887e20a199fSJeff Roberson KEG_LOCK_FINI(keg); 1888099a0e58SBosko Milekic } 1889099a0e58SBosko Milekic 1890099a0e58SBosko Milekic /* 1891099a0e58SBosko Milekic * Zone header dtor. 1892099a0e58SBosko Milekic * 1893099a0e58SBosko Milekic * Arguments/Returns follow uma_dtor specifications 1894099a0e58SBosko Milekic * udata unused 1895099a0e58SBosko Milekic */ 18969c2cd7e5SJeff Roberson static void 18979c2cd7e5SJeff Roberson zone_dtor(void *arg, int size, void *udata) 18989c2cd7e5SJeff Roberson { 18999c2cd7e5SJeff Roberson uma_zone_t zone; 1900099a0e58SBosko Milekic uma_keg_t keg; 19019c2cd7e5SJeff Roberson 19029c2cd7e5SJeff Roberson zone = (uma_zone_t)arg; 19039643769aSJeff Roberson 1904e20a199fSJeff Roberson if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL)) 19059643769aSJeff Roberson cache_drain(zone); 1906099a0e58SBosko Milekic 1907111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 1908099a0e58SBosko Milekic LIST_REMOVE(zone, uz_link); 1909111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 1910099a0e58SBosko Milekic /* 1911099a0e58SBosko Milekic * XXX there are some races here where 1912099a0e58SBosko Milekic * the zone can be drained but zone lock 1913099a0e58SBosko Milekic * released and then refilled before we 1914099a0e58SBosko Milekic * remove it... we dont care for now 1915099a0e58SBosko Milekic */ 1916e20a199fSJeff Roberson zone_drain_wait(zone, M_WAITOK); 1917e20a199fSJeff Roberson /* 1918e20a199fSJeff Roberson * We only destroy kegs from non secondary zones. 1919e20a199fSJeff Roberson */ 1920bb15d1c7SGleb Smirnoff if ((keg = zone->uz_keg) != NULL && 1921bb15d1c7SGleb Smirnoff (zone->uz_flags & UMA_ZONE_SECONDARY) == 0) { 1922111fbcd5SBryan Venteicher rw_wlock(&uma_rwlock); 1923099a0e58SBosko Milekic LIST_REMOVE(keg, uk_link); 1924111fbcd5SBryan Venteicher rw_wunlock(&uma_rwlock); 19250095a784SJeff Roberson zone_free_item(kegs, keg, NULL, SKIP_NONE); 19269c2cd7e5SJeff Roberson } 19272efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_allocs); 19282efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_frees); 19292efcc8cbSGleb Smirnoff counter_u64_free(zone->uz_fails); 1930bb15d1c7SGleb Smirnoff if (zone->uz_lockptr == &zone->uz_lock) 1931af526374SJeff Roberson ZONE_LOCK_FINI(zone); 1932099a0e58SBosko Milekic } 1933099a0e58SBosko Milekic 19349c2cd7e5SJeff Roberson /* 19358355f576SJeff Roberson * Traverses every zone in the system and calls a callback 19368355f576SJeff Roberson * 19378355f576SJeff Roberson * Arguments: 19388355f576SJeff Roberson * zfunc A pointer to a function which accepts a zone 19398355f576SJeff Roberson * as an argument. 19408355f576SJeff Roberson * 19418355f576SJeff Roberson * Returns: 19428355f576SJeff Roberson * Nothing 19438355f576SJeff Roberson */ 19448355f576SJeff Roberson static void 19458355f576SJeff Roberson zone_foreach(void (*zfunc)(uma_zone_t)) 19468355f576SJeff Roberson { 1947099a0e58SBosko Milekic uma_keg_t keg; 19488355f576SJeff Roberson uma_zone_t zone; 19498355f576SJeff Roberson 19502efcc8cbSGleb Smirnoff /* 19512efcc8cbSGleb Smirnoff * Before BOOT_RUNNING we are guaranteed to be single 19522efcc8cbSGleb Smirnoff * threaded, so locking isn't needed. Startup functions 19532efcc8cbSGleb Smirnoff * are allowed to use M_WAITOK. 19542efcc8cbSGleb Smirnoff */ 19552efcc8cbSGleb Smirnoff if (__predict_true(booted == BOOT_RUNNING)) 1956111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 1957099a0e58SBosko Milekic LIST_FOREACH(keg, &uma_kegs, uk_link) { 1958099a0e58SBosko Milekic LIST_FOREACH(zone, &keg->uk_zones, uz_link) 19598355f576SJeff Roberson zfunc(zone); 1960099a0e58SBosko Milekic } 19612efcc8cbSGleb Smirnoff if (__predict_true(booted == BOOT_RUNNING)) 1962111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 19638355f576SJeff Roberson } 19648355f576SJeff Roberson 1965f4bef67cSGleb Smirnoff /* 1966f4bef67cSGleb Smirnoff * Count how many pages do we need to bootstrap. VM supplies 1967f4bef67cSGleb Smirnoff * its need in early zones in the argument, we add up our zones, 1968f4bef67cSGleb Smirnoff * which consist of: UMA Slabs, UMA Hash and 9 Bucket zones. The 1969f4bef67cSGleb Smirnoff * zone of zones and zone of kegs are accounted separately. 1970f4bef67cSGleb Smirnoff */ 1971f4bef67cSGleb Smirnoff #define UMA_BOOT_ZONES 11 19725073a083SGleb Smirnoff /* Zone of zones and zone of kegs have arbitrary alignment. */ 19735073a083SGleb Smirnoff #define UMA_BOOT_ALIGN 32 1974f4bef67cSGleb Smirnoff static int zsize, ksize; 1975f4bef67cSGleb Smirnoff int 1976f7d35785SGleb Smirnoff uma_startup_count(int vm_zones) 1977f4bef67cSGleb Smirnoff { 1978f7d35785SGleb Smirnoff int zones, pages; 1979f4bef67cSGleb Smirnoff 1980f4bef67cSGleb Smirnoff ksize = sizeof(struct uma_keg) + 1981f4bef67cSGleb Smirnoff (sizeof(struct uma_domain) * vm_ndomains); 1982f4bef67cSGleb Smirnoff zsize = sizeof(struct uma_zone) + 1983f4bef67cSGleb Smirnoff (sizeof(struct uma_cache) * (mp_maxid + 1)) + 1984f4bef67cSGleb Smirnoff (sizeof(struct uma_zone_domain) * vm_ndomains); 1985f4bef67cSGleb Smirnoff 19865073a083SGleb Smirnoff /* 19875073a083SGleb Smirnoff * Memory for the zone of kegs and its keg, 19885073a083SGleb Smirnoff * and for zone of zones. 19895073a083SGleb Smirnoff */ 1990f4bef67cSGleb Smirnoff pages = howmany(roundup(zsize, CACHE_LINE_SIZE) * 2 + 1991f4bef67cSGleb Smirnoff roundup(ksize, CACHE_LINE_SIZE), PAGE_SIZE); 1992f4bef67cSGleb Smirnoff 1993f7d35785SGleb Smirnoff #ifdef UMA_MD_SMALL_ALLOC 1994f7d35785SGleb Smirnoff zones = UMA_BOOT_ZONES; 1995f7d35785SGleb Smirnoff #else 1996f7d35785SGleb Smirnoff zones = UMA_BOOT_ZONES + vm_zones; 1997f7d35785SGleb Smirnoff vm_zones = 0; 1998f7d35785SGleb Smirnoff #endif 1999f4bef67cSGleb Smirnoff 20005073a083SGleb Smirnoff /* Memory for the rest of startup zones, UMA and VM, ... */ 20010b2e3aeaSGleb Smirnoff if (zsize > UMA_SLAB_SPACE) { 20020b2e3aeaSGleb Smirnoff /* See keg_large_init(). */ 20030b2e3aeaSGleb Smirnoff u_int ppera; 20040b2e3aeaSGleb Smirnoff 20050b2e3aeaSGleb Smirnoff ppera = howmany(roundup2(zsize, UMA_BOOT_ALIGN), PAGE_SIZE); 20060b2e3aeaSGleb Smirnoff if (PAGE_SIZE * ppera - roundup2(zsize, UMA_BOOT_ALIGN) < 20070b2e3aeaSGleb Smirnoff SIZEOF_UMA_SLAB) 20080b2e3aeaSGleb Smirnoff ppera++; 20090b2e3aeaSGleb Smirnoff pages += (zones + vm_zones) * ppera; 20100b2e3aeaSGleb Smirnoff } else if (roundup2(zsize, UMA_BOOT_ALIGN) > UMA_SLAB_SPACE) 20110b2e3aeaSGleb Smirnoff /* See keg_small_init() special case for uk_ppera = 1. */ 201296a10340SGleb Smirnoff pages += zones; 2013f4bef67cSGleb Smirnoff else 20145073a083SGleb Smirnoff pages += howmany(zones, 20155073a083SGleb Smirnoff UMA_SLAB_SPACE / roundup2(zsize, UMA_BOOT_ALIGN)); 2016f4bef67cSGleb Smirnoff 20175073a083SGleb Smirnoff /* ... and their kegs. Note that zone of zones allocates a keg! */ 20185073a083SGleb Smirnoff pages += howmany(zones + 1, 20195073a083SGleb Smirnoff UMA_SLAB_SPACE / roundup2(ksize, UMA_BOOT_ALIGN)); 2020f4bef67cSGleb Smirnoff 2021f4bef67cSGleb Smirnoff /* 20225073a083SGleb Smirnoff * Most of startup zones are not going to be offpages, that's 20235073a083SGleb Smirnoff * why we use UMA_SLAB_SPACE instead of UMA_SLAB_SIZE in all 20245073a083SGleb Smirnoff * calculations. Some large bucket zones will be offpage, and 20255073a083SGleb Smirnoff * thus will allocate hashes. We take conservative approach 20265073a083SGleb Smirnoff * and assume that all zones may allocate hash. This may give 20275073a083SGleb Smirnoff * us some positive inaccuracy, usually an extra single page. 2028f4bef67cSGleb Smirnoff */ 20295073a083SGleb Smirnoff pages += howmany(zones, UMA_SLAB_SPACE / 2030d2be4a1eSGleb Smirnoff (sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT)); 2031f4bef67cSGleb Smirnoff 2032f4bef67cSGleb Smirnoff return (pages); 2033f4bef67cSGleb Smirnoff } 2034f4bef67cSGleb Smirnoff 20358355f576SJeff Roberson void 2036ac0a6fd0SGleb Smirnoff uma_startup(void *mem, int npages) 20378355f576SJeff Roberson { 20388355f576SJeff Roberson struct uma_zctor_args args; 2039ab3185d1SJeff Roberson uma_keg_t masterkeg; 2040ab3185d1SJeff Roberson uintptr_t m; 2041f4bef67cSGleb Smirnoff 2042f4bef67cSGleb Smirnoff #ifdef DIAGNOSTIC 2043f4bef67cSGleb Smirnoff printf("Entering %s with %d boot pages configured\n", __func__, npages); 2044f4bef67cSGleb Smirnoff #endif 20458355f576SJeff Roberson 2046111fbcd5SBryan Venteicher rw_init(&uma_rwlock, "UMA lock"); 2047099a0e58SBosko Milekic 2048ab3185d1SJeff Roberson /* Use bootpages memory for the zone of zones and zone of kegs. */ 2049ab3185d1SJeff Roberson m = (uintptr_t)mem; 2050ab3185d1SJeff Roberson zones = (uma_zone_t)m; 2051ab3185d1SJeff Roberson m += roundup(zsize, CACHE_LINE_SIZE); 2052ab3185d1SJeff Roberson kegs = (uma_zone_t)m; 2053ab3185d1SJeff Roberson m += roundup(zsize, CACHE_LINE_SIZE); 2054ab3185d1SJeff Roberson masterkeg = (uma_keg_t)m; 2055ab3185d1SJeff Roberson m += roundup(ksize, CACHE_LINE_SIZE); 2056ab3185d1SJeff Roberson m = roundup(m, PAGE_SIZE); 2057ab3185d1SJeff Roberson npages -= (m - (uintptr_t)mem) / PAGE_SIZE; 2058ab3185d1SJeff Roberson mem = (void *)m; 2059ab3185d1SJeff Roberson 2060099a0e58SBosko Milekic /* "manually" create the initial zone */ 20610095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2062099a0e58SBosko Milekic args.name = "UMA Kegs"; 2063ab3185d1SJeff Roberson args.size = ksize; 2064099a0e58SBosko Milekic args.ctor = keg_ctor; 2065099a0e58SBosko Milekic args.dtor = keg_dtor; 20668355f576SJeff Roberson args.uminit = zero_init; 20678355f576SJeff Roberson args.fini = NULL; 2068ab3185d1SJeff Roberson args.keg = masterkeg; 20695073a083SGleb Smirnoff args.align = UMA_BOOT_ALIGN - 1; 2070b60f5b79SJeff Roberson args.flags = UMA_ZFLAG_INTERNAL; 2071ab3185d1SJeff Roberson zone_ctor(kegs, zsize, &args, M_WAITOK); 20728355f576SJeff Roberson 2073ac0a6fd0SGleb Smirnoff bootmem = mem; 2074ac0a6fd0SGleb Smirnoff boot_pages = npages; 20758355f576SJeff Roberson 2076099a0e58SBosko Milekic args.name = "UMA Zones"; 2077f4bef67cSGleb Smirnoff args.size = zsize; 2078099a0e58SBosko Milekic args.ctor = zone_ctor; 2079099a0e58SBosko Milekic args.dtor = zone_dtor; 2080099a0e58SBosko Milekic args.uminit = zero_init; 2081099a0e58SBosko Milekic args.fini = NULL; 2082099a0e58SBosko Milekic args.keg = NULL; 20835073a083SGleb Smirnoff args.align = UMA_BOOT_ALIGN - 1; 2084099a0e58SBosko Milekic args.flags = UMA_ZFLAG_INTERNAL; 2085ab3185d1SJeff Roberson zone_ctor(zones, zsize, &args, M_WAITOK); 2086099a0e58SBosko Milekic 20878355f576SJeff Roberson /* Now make a zone for slab headers */ 20888355f576SJeff Roberson slabzone = uma_zcreate("UMA Slabs", 2089ef72505eSJeff Roberson sizeof(struct uma_slab), 20908355f576SJeff Roberson NULL, NULL, NULL, NULL, 2091b60f5b79SJeff Roberson UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 20928355f576SJeff Roberson 20938355f576SJeff Roberson hashzone = uma_zcreate("UMA Hash", 20948355f576SJeff Roberson sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT, 20958355f576SJeff Roberson NULL, NULL, NULL, NULL, 2096b60f5b79SJeff Roberson UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL); 20978355f576SJeff Roberson 2098cae33c14SJeff Roberson bucket_init(); 20998355f576SJeff Roberson 2100f4bef67cSGleb Smirnoff booted = BOOT_STRAPPED; 21018355f576SJeff Roberson } 21028355f576SJeff Roberson 2103f4bef67cSGleb Smirnoff void 2104f4bef67cSGleb Smirnoff uma_startup1(void) 2105f4bef67cSGleb Smirnoff { 2106f4bef67cSGleb Smirnoff 2107f4bef67cSGleb Smirnoff #ifdef DIAGNOSTIC 2108f4bef67cSGleb Smirnoff printf("Entering %s with %d boot pages left\n", __func__, boot_pages); 2109f4bef67cSGleb Smirnoff #endif 2110f4bef67cSGleb Smirnoff booted = BOOT_PAGEALLOC; 2111f4bef67cSGleb Smirnoff } 2112f4bef67cSGleb Smirnoff 21138355f576SJeff Roberson void 211499571dc3SJeff Roberson uma_startup2(void) 21158355f576SJeff Roberson { 2116f4bef67cSGleb Smirnoff 2117f7d35785SGleb Smirnoff #ifdef DIAGNOSTIC 2118f7d35785SGleb Smirnoff printf("Entering %s with %d boot pages left\n", __func__, boot_pages); 2119f7d35785SGleb Smirnoff #endif 2120f4bef67cSGleb Smirnoff booted = BOOT_BUCKETS; 212195c4bf75SKonstantin Belousov sx_init(&uma_drain_lock, "umadrain"); 2122f4bef67cSGleb Smirnoff bucket_enable(); 21238355f576SJeff Roberson } 21248355f576SJeff Roberson 21258355f576SJeff Roberson /* 21268355f576SJeff Roberson * Initialize our callout handle 21278355f576SJeff Roberson * 21288355f576SJeff Roberson */ 21298355f576SJeff Roberson static void 21308355f576SJeff Roberson uma_startup3(void) 21318355f576SJeff Roberson { 21321431a748SGleb Smirnoff 2133c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2134c5deaf04SGleb Smirnoff TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor); 2135c5deaf04SGleb Smirnoff uma_dbg_cnt = counter_u64_alloc(M_WAITOK); 2136c5deaf04SGleb Smirnoff uma_skip_cnt = counter_u64_alloc(M_WAITOK); 2137c5deaf04SGleb Smirnoff #endif 21382efcc8cbSGleb Smirnoff zone_foreach(zone_alloc_counters); 2139fd90e2edSJung-uk Kim callout_init(&uma_callout, 1); 21409643769aSJeff Roberson callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL); 2141c5deaf04SGleb Smirnoff booted = BOOT_RUNNING; 21428355f576SJeff Roberson } 21438355f576SJeff Roberson 2144e20a199fSJeff Roberson static uma_keg_t 2145099a0e58SBosko Milekic uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini, 214685dcf349SGleb Smirnoff int align, uint32_t flags) 2147099a0e58SBosko Milekic { 2148099a0e58SBosko Milekic struct uma_kctor_args args; 2149099a0e58SBosko Milekic 2150099a0e58SBosko Milekic args.size = size; 2151099a0e58SBosko Milekic args.uminit = uminit; 2152099a0e58SBosko Milekic args.fini = fini; 21531e319f6dSRobert Watson args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align; 2154099a0e58SBosko Milekic args.flags = flags; 2155099a0e58SBosko Milekic args.zone = zone; 2156ab3185d1SJeff Roberson return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK)); 2157099a0e58SBosko Milekic } 2158099a0e58SBosko Milekic 2159f4bef67cSGleb Smirnoff /* Public functions */ 21608355f576SJeff Roberson /* See uma.h */ 21611e319f6dSRobert Watson void 21621e319f6dSRobert Watson uma_set_align(int align) 21631e319f6dSRobert Watson { 21641e319f6dSRobert Watson 21651e319f6dSRobert Watson if (align != UMA_ALIGN_CACHE) 21661e319f6dSRobert Watson uma_align_cache = align; 21671e319f6dSRobert Watson } 21681e319f6dSRobert Watson 21691e319f6dSRobert Watson /* See uma.h */ 21708355f576SJeff Roberson uma_zone_t 2171bb196eb4SMatthew D Fleming uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, 217285dcf349SGleb Smirnoff uma_init uminit, uma_fini fini, int align, uint32_t flags) 21738355f576SJeff Roberson 21748355f576SJeff Roberson { 21758355f576SJeff Roberson struct uma_zctor_args args; 217695c4bf75SKonstantin Belousov uma_zone_t res; 217795c4bf75SKonstantin Belousov bool locked; 21788355f576SJeff Roberson 2179a5a35578SJohn Baldwin KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"", 2180a5a35578SJohn Baldwin align, name)); 2181a5a35578SJohn Baldwin 21828355f576SJeff Roberson /* This stuff is essential for the zone ctor */ 21830095a784SJeff Roberson memset(&args, 0, sizeof(args)); 21848355f576SJeff Roberson args.name = name; 21858355f576SJeff Roberson args.size = size; 21868355f576SJeff Roberson args.ctor = ctor; 21878355f576SJeff Roberson args.dtor = dtor; 21888355f576SJeff Roberson args.uminit = uminit; 21898355f576SJeff Roberson args.fini = fini; 2190afc6dc36SJohn-Mark Gurney #ifdef INVARIANTS 2191afc6dc36SJohn-Mark Gurney /* 2192afc6dc36SJohn-Mark Gurney * If a zone is being created with an empty constructor and 2193afc6dc36SJohn-Mark Gurney * destructor, pass UMA constructor/destructor which checks for 2194afc6dc36SJohn-Mark Gurney * memory use after free. 2195afc6dc36SJohn-Mark Gurney */ 219619c591bfSMateusz Guzik if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOFREE))) && 219719c591bfSMateusz Guzik ctor == NULL && dtor == NULL && uminit == NULL && fini == NULL) { 2198afc6dc36SJohn-Mark Gurney args.ctor = trash_ctor; 2199afc6dc36SJohn-Mark Gurney args.dtor = trash_dtor; 2200afc6dc36SJohn-Mark Gurney args.uminit = trash_init; 2201afc6dc36SJohn-Mark Gurney args.fini = trash_fini; 2202afc6dc36SJohn-Mark Gurney } 2203afc6dc36SJohn-Mark Gurney #endif 22048355f576SJeff Roberson args.align = align; 22058355f576SJeff Roberson args.flags = flags; 2206099a0e58SBosko Milekic args.keg = NULL; 2207099a0e58SBosko Milekic 2208f4bef67cSGleb Smirnoff if (booted < BOOT_BUCKETS) { 220995c4bf75SKonstantin Belousov locked = false; 221095c4bf75SKonstantin Belousov } else { 221195c4bf75SKonstantin Belousov sx_slock(&uma_drain_lock); 221295c4bf75SKonstantin Belousov locked = true; 221395c4bf75SKonstantin Belousov } 2214ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 221595c4bf75SKonstantin Belousov if (locked) 221695c4bf75SKonstantin Belousov sx_sunlock(&uma_drain_lock); 221795c4bf75SKonstantin Belousov return (res); 2218099a0e58SBosko Milekic } 2219099a0e58SBosko Milekic 2220099a0e58SBosko Milekic /* See uma.h */ 2221099a0e58SBosko Milekic uma_zone_t 2222099a0e58SBosko Milekic uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor, 2223099a0e58SBosko Milekic uma_init zinit, uma_fini zfini, uma_zone_t master) 2224099a0e58SBosko Milekic { 2225099a0e58SBosko Milekic struct uma_zctor_args args; 2226e20a199fSJeff Roberson uma_keg_t keg; 222795c4bf75SKonstantin Belousov uma_zone_t res; 222895c4bf75SKonstantin Belousov bool locked; 2229099a0e58SBosko Milekic 2230bb15d1c7SGleb Smirnoff keg = master->uz_keg; 22310095a784SJeff Roberson memset(&args, 0, sizeof(args)); 2232099a0e58SBosko Milekic args.name = name; 2233e20a199fSJeff Roberson args.size = keg->uk_size; 2234099a0e58SBosko Milekic args.ctor = ctor; 2235099a0e58SBosko Milekic args.dtor = dtor; 2236099a0e58SBosko Milekic args.uminit = zinit; 2237099a0e58SBosko Milekic args.fini = zfini; 2238e20a199fSJeff Roberson args.align = keg->uk_align; 2239e20a199fSJeff Roberson args.flags = keg->uk_flags | UMA_ZONE_SECONDARY; 2240e20a199fSJeff Roberson args.keg = keg; 22418355f576SJeff Roberson 2242f4bef67cSGleb Smirnoff if (booted < BOOT_BUCKETS) { 224395c4bf75SKonstantin Belousov locked = false; 224495c4bf75SKonstantin Belousov } else { 224595c4bf75SKonstantin Belousov sx_slock(&uma_drain_lock); 224695c4bf75SKonstantin Belousov locked = true; 224795c4bf75SKonstantin Belousov } 2248e20a199fSJeff Roberson /* XXX Attaches only one keg of potentially many. */ 2249ab3185d1SJeff Roberson res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK); 225095c4bf75SKonstantin Belousov if (locked) 225195c4bf75SKonstantin Belousov sx_sunlock(&uma_drain_lock); 225295c4bf75SKonstantin Belousov return (res); 22538355f576SJeff Roberson } 22548355f576SJeff Roberson 22550095a784SJeff Roberson /* See uma.h */ 22560095a784SJeff Roberson uma_zone_t 2257af526374SJeff Roberson uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor, 2258af526374SJeff Roberson uma_init zinit, uma_fini zfini, uma_import zimport, 2259af526374SJeff Roberson uma_release zrelease, void *arg, int flags) 22600095a784SJeff Roberson { 22610095a784SJeff Roberson struct uma_zctor_args args; 22620095a784SJeff Roberson 22630095a784SJeff Roberson memset(&args, 0, sizeof(args)); 22640095a784SJeff Roberson args.name = name; 2265af526374SJeff Roberson args.size = size; 22660095a784SJeff Roberson args.ctor = ctor; 22670095a784SJeff Roberson args.dtor = dtor; 22680095a784SJeff Roberson args.uminit = zinit; 22690095a784SJeff Roberson args.fini = zfini; 22700095a784SJeff Roberson args.import = zimport; 22710095a784SJeff Roberson args.release = zrelease; 22720095a784SJeff Roberson args.arg = arg; 22730095a784SJeff Roberson args.align = 0; 2274bb15d1c7SGleb Smirnoff args.flags = flags | UMA_ZFLAG_CACHE; 22750095a784SJeff Roberson 2276ab3185d1SJeff Roberson return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK)); 22770095a784SJeff Roberson } 22780095a784SJeff Roberson 22798355f576SJeff Roberson /* See uma.h */ 22809c2cd7e5SJeff Roberson void 22819c2cd7e5SJeff Roberson uma_zdestroy(uma_zone_t zone) 22829c2cd7e5SJeff Roberson { 2283f4ff923bSRobert Watson 228495c4bf75SKonstantin Belousov sx_slock(&uma_drain_lock); 22850095a784SJeff Roberson zone_free_item(zones, zone, NULL, SKIP_NONE); 228695c4bf75SKonstantin Belousov sx_sunlock(&uma_drain_lock); 22879c2cd7e5SJeff Roberson } 22889c2cd7e5SJeff Roberson 22898d6fbbb8SJeff Roberson void 22908d6fbbb8SJeff Roberson uma_zwait(uma_zone_t zone) 22918d6fbbb8SJeff Roberson { 22928d6fbbb8SJeff Roberson void *item; 22938d6fbbb8SJeff Roberson 22948d6fbbb8SJeff Roberson item = uma_zalloc_arg(zone, NULL, M_WAITOK); 22958d6fbbb8SJeff Roberson uma_zfree(zone, item); 22968d6fbbb8SJeff Roberson } 22978d6fbbb8SJeff Roberson 22984e180881SMateusz Guzik void * 22994e180881SMateusz Guzik uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags) 23004e180881SMateusz Guzik { 23014e180881SMateusz Guzik void *item; 2302b4799947SRuslan Bukin #ifdef SMP 23034e180881SMateusz Guzik int i; 23044e180881SMateusz Guzik 23054e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2306b4799947SRuslan Bukin #endif 23074e180881SMateusz Guzik item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO); 23084e180881SMateusz Guzik if (item != NULL && (flags & M_ZERO)) { 2309b4799947SRuslan Bukin #ifdef SMP 2310013072f0SMark Johnston for (i = 0; i <= mp_maxid; i++) 23114e180881SMateusz Guzik bzero(zpcpu_get_cpu(item, i), zone->uz_size); 2312b4799947SRuslan Bukin #else 2313b4799947SRuslan Bukin bzero(item, zone->uz_size); 2314b4799947SRuslan Bukin #endif 23154e180881SMateusz Guzik } 23164e180881SMateusz Guzik return (item); 23174e180881SMateusz Guzik } 23184e180881SMateusz Guzik 23194e180881SMateusz Guzik /* 23204e180881SMateusz Guzik * A stub while both regular and pcpu cases are identical. 23214e180881SMateusz Guzik */ 23224e180881SMateusz Guzik void 23234e180881SMateusz Guzik uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata) 23244e180881SMateusz Guzik { 23254e180881SMateusz Guzik 2326c5b7751fSIan Lepore #ifdef SMP 23274e180881SMateusz Guzik MPASS(zone->uz_flags & UMA_ZONE_PCPU); 2328c5b7751fSIan Lepore #endif 23294e180881SMateusz Guzik uma_zfree_arg(zone, item, udata); 23304e180881SMateusz Guzik } 23314e180881SMateusz Guzik 23329c2cd7e5SJeff Roberson /* See uma.h */ 23338355f576SJeff Roberson void * 23342cc35ff9SJeff Roberson uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) 23358355f576SJeff Roberson { 2336ab3185d1SJeff Roberson uma_zone_domain_t zdom; 23378355f576SJeff Roberson uma_bucket_t bucket; 2338ab3185d1SJeff Roberson uma_cache_t cache; 2339ab3185d1SJeff Roberson void *item; 2340bb15d1c7SGleb Smirnoff int cpu, domain, lockfail, maxbucket; 2341c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2342c5deaf04SGleb Smirnoff bool skipdbg; 2343c5deaf04SGleb Smirnoff #endif 23448355f576SJeff Roberson 2345e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 234619fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 234710cb2424SMark Murray 23488355f576SJeff Roberson /* This is the fast path allocation */ 23491431a748SGleb Smirnoff CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d", 23501431a748SGleb Smirnoff curthread, zone->uz_name, zone, flags); 2351a553d4b8SJeff Roberson 2352635fd505SRobert Watson if (flags & M_WAITOK) { 2353b23f72e9SBrian Feldman WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2354635fd505SRobert Watson "uma_zalloc_arg: zone \"%s\"", zone->uz_name); 23554c1cc01cSJohn Baldwin } 23560766f278SJonathan T. Looney KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC")); 2357d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 23581067a2baSJonathan T. Looney ("uma_zalloc_arg: called with spinlock or critical section held")); 2359ea99223eSMateusz Guzik if (zone->uz_flags & UMA_ZONE_PCPU) 2360b8af2820SMateusz Guzik KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone " 2361b8af2820SMateusz Guzik "with M_ZERO passed")); 23621067a2baSJonathan T. Looney 23638d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 23648d689e04SGleb Smirnoff if (memguard_cmp_zone(zone)) { 23658d689e04SGleb Smirnoff item = memguard_alloc(zone->uz_size, flags); 23668d689e04SGleb Smirnoff if (item != NULL) { 23678d689e04SGleb Smirnoff if (zone->uz_init != NULL && 23688d689e04SGleb Smirnoff zone->uz_init(item, zone->uz_size, flags) != 0) 23698d689e04SGleb Smirnoff return (NULL); 23708d689e04SGleb Smirnoff if (zone->uz_ctor != NULL && 2371fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, 2372fc03d22bSJeff Roberson flags) != 0) { 23738d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 23748d689e04SGleb Smirnoff return (NULL); 23758d689e04SGleb Smirnoff } 23768d689e04SGleb Smirnoff return (item); 23778d689e04SGleb Smirnoff } 23788d689e04SGleb Smirnoff /* This is unfortunate but should not be fatal. */ 23798d689e04SGleb Smirnoff } 23808d689e04SGleb Smirnoff #endif 23815d1ae027SRobert Watson /* 23825d1ae027SRobert Watson * If possible, allocate from the per-CPU cache. There are two 23835d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 23845d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 23855d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 23865d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 23875d1ae027SRobert Watson * preemption and migration. We release the critical section in 23885d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to allocate from 23895d1ae027SRobert Watson * the current cache; when we re-acquire the critical section, we 23905d1ae027SRobert Watson * must detect and handle migration if it has occurred. 23915d1ae027SRobert Watson */ 239281c0d72cSGleb Smirnoff zalloc_restart: 23935d1ae027SRobert Watson critical_enter(); 23945d1ae027SRobert Watson cpu = curcpu; 23958355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 23968355f576SJeff Roberson 23978355f576SJeff Roberson zalloc_start: 23988355f576SJeff Roberson bucket = cache->uc_allocbucket; 2399fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt > 0) { 2400cae33c14SJeff Roberson bucket->ub_cnt--; 2401cae33c14SJeff Roberson item = bucket->ub_bucket[bucket->ub_cnt]; 24028355f576SJeff Roberson #ifdef INVARIANTS 2403cae33c14SJeff Roberson bucket->ub_bucket[bucket->ub_cnt] = NULL; 24048355f576SJeff Roberson #endif 2405fc03d22bSJeff Roberson KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled.")); 24068355f576SJeff Roberson cache->uc_allocs++; 24075d1ae027SRobert Watson critical_exit(); 2408c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2409c5deaf04SGleb Smirnoff skipdbg = uma_dbg_zskip(zone, item); 2410c5deaf04SGleb Smirnoff #endif 2411fc03d22bSJeff Roberson if (zone->uz_ctor != NULL && 2412c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2413c5deaf04SGleb Smirnoff (!skipdbg || zone->uz_ctor != trash_ctor || 2414c5deaf04SGleb Smirnoff zone->uz_dtor != trash_dtor) && 2415c5deaf04SGleb Smirnoff #endif 2416fc03d22bSJeff Roberson zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) { 24172efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 2418bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 2419b23f72e9SBrian Feldman return (NULL); 2420b23f72e9SBrian Feldman } 2421ef72505eSJeff Roberson #ifdef INVARIANTS 2422c5deaf04SGleb Smirnoff if (!skipdbg) 2423ef72505eSJeff Roberson uma_dbg_alloc(zone, NULL, item); 2424ef72505eSJeff Roberson #endif 24252cc35ff9SJeff Roberson if (flags & M_ZERO) 242648343a2fSGleb Smirnoff uma_zero_item(item, zone); 24278355f576SJeff Roberson return (item); 2428fc03d22bSJeff Roberson } 2429fc03d22bSJeff Roberson 24308355f576SJeff Roberson /* 24318355f576SJeff Roberson * We have run out of items in our alloc bucket. 24328355f576SJeff Roberson * See if we can switch with our free bucket. 24338355f576SJeff Roberson */ 2434b983089aSJeff Roberson bucket = cache->uc_freebucket; 2435fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt > 0) { 24361431a748SGleb Smirnoff CTR2(KTR_UMA, 24371431a748SGleb Smirnoff "uma_zalloc: zone %s(%p) swapping empty with alloc", 24381431a748SGleb Smirnoff zone->uz_name, zone); 24398355f576SJeff Roberson cache->uc_freebucket = cache->uc_allocbucket; 2440b983089aSJeff Roberson cache->uc_allocbucket = bucket; 24418355f576SJeff Roberson goto zalloc_start; 24428355f576SJeff Roberson } 2443fc03d22bSJeff Roberson 2444fc03d22bSJeff Roberson /* 2445fc03d22bSJeff Roberson * Discard any empty allocation bucket while we hold no locks. 2446fc03d22bSJeff Roberson */ 2447fc03d22bSJeff Roberson bucket = cache->uc_allocbucket; 2448fc03d22bSJeff Roberson cache->uc_allocbucket = NULL; 2449fc03d22bSJeff Roberson critical_exit(); 2450fc03d22bSJeff Roberson if (bucket != NULL) 24516fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 2452fc03d22bSJeff Roberson 245330c5525bSAndrew Gallatin if (zone->uz_flags & UMA_ZONE_NUMA) { 2454ab3185d1SJeff Roberson domain = PCPU_GET(domain); 245530c5525bSAndrew Gallatin if (VM_DOMAIN_EMPTY(domain)) 245630c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 245730c5525bSAndrew Gallatin } else 2458ab3185d1SJeff Roberson domain = UMA_ANYDOMAIN; 2459ab3185d1SJeff Roberson 2460fc03d22bSJeff Roberson /* Short-circuit for zones without buckets and low memory. */ 2461bb15d1c7SGleb Smirnoff if (zone->uz_count == 0 || bucketdisable) { 2462bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 2463fc03d22bSJeff Roberson goto zalloc_item; 2464bb15d1c7SGleb Smirnoff } 2465fc03d22bSJeff Roberson 24665d1ae027SRobert Watson /* 24675d1ae027SRobert Watson * Attempt to retrieve the item from the per-CPU cache has failed, so 24685d1ae027SRobert Watson * we must go back to the zone. This requires the zone lock, so we 24695d1ae027SRobert Watson * must drop the critical section, then re-acquire it when we go back 24705d1ae027SRobert Watson * to the cache. Since the critical section is released, we may be 24715d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 24725d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 24735d1ae027SRobert Watson * the critical section. 24745d1ae027SRobert Watson */ 2475fc03d22bSJeff Roberson lockfail = 0; 2476fc03d22bSJeff Roberson if (ZONE_TRYLOCK(zone) == 0) { 2477fc03d22bSJeff Roberson /* Record contention to size the buckets. */ 2478a553d4b8SJeff Roberson ZONE_LOCK(zone); 2479fc03d22bSJeff Roberson lockfail = 1; 2480fc03d22bSJeff Roberson } 24815d1ae027SRobert Watson critical_enter(); 24825d1ae027SRobert Watson cpu = curcpu; 24835d1ae027SRobert Watson cache = &zone->uz_cpu[cpu]; 24845d1ae027SRobert Watson 2485fc03d22bSJeff Roberson /* See if we lost the race to fill the cache. */ 2486fc03d22bSJeff Roberson if (cache->uc_allocbucket != NULL) { 2487fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 2488fc03d22bSJeff Roberson goto zalloc_start; 2489a553d4b8SJeff Roberson } 24908355f576SJeff Roberson 2491fc03d22bSJeff Roberson /* 2492fc03d22bSJeff Roberson * Check the zone's cache of buckets. 2493fc03d22bSJeff Roberson */ 2494ab3185d1SJeff Roberson if (domain == UMA_ANYDOMAIN) 2495ab3185d1SJeff Roberson zdom = &zone->uz_domain[0]; 2496ab3185d1SJeff Roberson else 2497ab3185d1SJeff Roberson zdom = &zone->uz_domain[domain]; 24980f9b7bf3SMark Johnston if ((bucket = zone_try_fetch_bucket(zone, zdom, true)) != NULL) { 2499cae33c14SJeff Roberson KASSERT(bucket->ub_cnt != 0, 2500a553d4b8SJeff Roberson ("uma_zalloc_arg: Returning an empty bucket.")); 2501a553d4b8SJeff Roberson cache->uc_allocbucket = bucket; 2502a553d4b8SJeff Roberson ZONE_UNLOCK(zone); 25038355f576SJeff Roberson goto zalloc_start; 2504a553d4b8SJeff Roberson } 25055d1ae027SRobert Watson /* We are no longer associated with this CPU. */ 25065d1ae027SRobert Watson critical_exit(); 2507bbee39c6SJeff Roberson 2508fc03d22bSJeff Roberson /* 2509fc03d22bSJeff Roberson * We bump the uz count when the cache size is insufficient to 2510fc03d22bSJeff Roberson * handle the working set. 2511fc03d22bSJeff Roberson */ 2512bb15d1c7SGleb Smirnoff if (lockfail && zone->uz_count < zone->uz_count_max) 2513a553d4b8SJeff Roberson zone->uz_count++; 2514bb15d1c7SGleb Smirnoff 2515bb15d1c7SGleb Smirnoff if (zone->uz_max_items > 0) { 2516bb15d1c7SGleb Smirnoff if (zone->uz_items >= zone->uz_max_items) 2517bb15d1c7SGleb Smirnoff goto zalloc_item; 2518bb15d1c7SGleb Smirnoff maxbucket = MIN(zone->uz_count, 2519bb15d1c7SGleb Smirnoff zone->uz_max_items - zone->uz_items); 2520bb45b411SGleb Smirnoff zone->uz_items += maxbucket; 2521bb15d1c7SGleb Smirnoff } else 2522bb15d1c7SGleb Smirnoff maxbucket = zone->uz_count; 2523fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 2524099a0e58SBosko Milekic 25258355f576SJeff Roberson /* 2526a553d4b8SJeff Roberson * Now lets just fill a bucket and put it on the free list. If that 2527763df3ecSPedro F. Giffuni * works we'll restart the allocation from the beginning and it 2528fc03d22bSJeff Roberson * will use the just filled bucket. 2529bbee39c6SJeff Roberson */ 2530bb15d1c7SGleb Smirnoff bucket = zone_alloc_bucket(zone, udata, domain, flags, maxbucket); 25311431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p", 25321431a748SGleb Smirnoff zone->uz_name, zone, bucket); 2533fc03d22bSJeff Roberson ZONE_LOCK(zone); 2534bb15d1c7SGleb Smirnoff if (bucket != NULL) { 2535bb45b411SGleb Smirnoff if (zone->uz_max_items > 0 && bucket->ub_cnt < maxbucket) { 2536bb45b411SGleb Smirnoff MPASS(zone->uz_items >= maxbucket - bucket->ub_cnt); 2537bb15d1c7SGleb Smirnoff zone->uz_items -= maxbucket - bucket->ub_cnt; 2538bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0 && 2539bb15d1c7SGleb Smirnoff zone->uz_items < zone->uz_max_items) 2540bb15d1c7SGleb Smirnoff wakeup_one(zone); 2541bb15d1c7SGleb Smirnoff } 2542fc03d22bSJeff Roberson critical_enter(); 2543fc03d22bSJeff Roberson cpu = curcpu; 2544fc03d22bSJeff Roberson cache = &zone->uz_cpu[cpu]; 25450f9b7bf3SMark Johnston 2546fc03d22bSJeff Roberson /* 2547fc03d22bSJeff Roberson * See if we lost the race or were migrated. Cache the 2548fc03d22bSJeff Roberson * initialized bucket to make this less likely or claim 2549fc03d22bSJeff Roberson * the memory directly. 2550fc03d22bSJeff Roberson */ 255181c0d72cSGleb Smirnoff if (cache->uc_allocbucket == NULL && 255281c0d72cSGleb Smirnoff ((zone->uz_flags & UMA_ZONE_NUMA) == 0 || 255381c0d72cSGleb Smirnoff domain == PCPU_GET(domain))) { 2554ab3185d1SJeff Roberson cache->uc_allocbucket = bucket; 25550f9b7bf3SMark Johnston zdom->uzd_imax += bucket->ub_cnt; 2556bb15d1c7SGleb Smirnoff } else if (zone->uz_bkt_count >= zone->uz_bkt_max) { 255781c0d72cSGleb Smirnoff critical_exit(); 255881c0d72cSGleb Smirnoff ZONE_UNLOCK(zone); 255981c0d72cSGleb Smirnoff bucket_drain(zone, bucket); 256081c0d72cSGleb Smirnoff bucket_free(zone, bucket, udata); 256181c0d72cSGleb Smirnoff goto zalloc_restart; 256281c0d72cSGleb Smirnoff } else 25630f9b7bf3SMark Johnston zone_put_bucket(zone, zdom, bucket, false); 2564bbee39c6SJeff Roberson ZONE_UNLOCK(zone); 2565fc03d22bSJeff Roberson goto zalloc_start; 2566bb45b411SGleb Smirnoff } else if (zone->uz_max_items > 0) { 2567bb15d1c7SGleb Smirnoff zone->uz_items -= maxbucket; 2568bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0 && 2569bb15d1c7SGleb Smirnoff zone->uz_items + 1 < zone->uz_max_items) 2570bb15d1c7SGleb Smirnoff wakeup_one(zone); 2571bbee39c6SJeff Roberson } 2572fc03d22bSJeff Roberson 2573bbee39c6SJeff Roberson /* 2574bbee39c6SJeff Roberson * We may not be able to get a bucket so return an actual item. 2575bbee39c6SJeff Roberson */ 2576fc03d22bSJeff Roberson zalloc_item: 2577bb15d1c7SGleb Smirnoff item = zone_alloc_item_locked(zone, udata, domain, flags); 2578fc03d22bSJeff Roberson 2579e20a199fSJeff Roberson return (item); 2580bbee39c6SJeff Roberson } 2581bbee39c6SJeff Roberson 2582ab3185d1SJeff Roberson void * 2583ab3185d1SJeff Roberson uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags) 2584bbee39c6SJeff Roberson { 2585ab3185d1SJeff Roberson 2586ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 258719fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 2588ab3185d1SJeff Roberson 2589ab3185d1SJeff Roberson /* This is the fast path allocation */ 2590ab3185d1SJeff Roberson CTR5(KTR_UMA, 2591ab3185d1SJeff Roberson "uma_zalloc_domain thread %x zone %s(%p) domain %d flags %d", 2592ab3185d1SJeff Roberson curthread, zone->uz_name, zone, domain, flags); 2593ab3185d1SJeff Roberson 2594ab3185d1SJeff Roberson if (flags & M_WAITOK) { 2595ab3185d1SJeff Roberson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2596ab3185d1SJeff Roberson "uma_zalloc_domain: zone \"%s\"", zone->uz_name); 2597ab3185d1SJeff Roberson } 2598ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 2599ab3185d1SJeff Roberson ("uma_zalloc_domain: called with spinlock or critical section held")); 2600ab3185d1SJeff Roberson 2601ab3185d1SJeff Roberson return (zone_alloc_item(zone, udata, domain, flags)); 2602ab3185d1SJeff Roberson } 2603ab3185d1SJeff Roberson 2604ab3185d1SJeff Roberson /* 2605ab3185d1SJeff Roberson * Find a slab with some space. Prefer slabs that are partially used over those 2606ab3185d1SJeff Roberson * that are totally full. This helps to reduce fragmentation. 2607ab3185d1SJeff Roberson * 2608ab3185d1SJeff Roberson * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check 2609ab3185d1SJeff Roberson * only 'domain'. 2610ab3185d1SJeff Roberson */ 2611ab3185d1SJeff Roberson static uma_slab_t 2612194a979eSMark Johnston keg_first_slab(uma_keg_t keg, int domain, bool rr) 2613ab3185d1SJeff Roberson { 2614ab3185d1SJeff Roberson uma_domain_t dom; 2615bbee39c6SJeff Roberson uma_slab_t slab; 2616ab3185d1SJeff Roberson int start; 2617ab3185d1SJeff Roberson 2618ab3185d1SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 2619ab3185d1SJeff Roberson ("keg_first_slab: domain %d out of range", domain)); 2620bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 2621ab3185d1SJeff Roberson 2622ab3185d1SJeff Roberson slab = NULL; 2623ab3185d1SJeff Roberson start = domain; 2624ab3185d1SJeff Roberson do { 2625ab3185d1SJeff Roberson dom = &keg->uk_domain[domain]; 2626ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_part_slab)) 2627ab3185d1SJeff Roberson return (LIST_FIRST(&dom->ud_part_slab)); 2628ab3185d1SJeff Roberson if (!LIST_EMPTY(&dom->ud_free_slab)) { 2629ab3185d1SJeff Roberson slab = LIST_FIRST(&dom->ud_free_slab); 2630ab3185d1SJeff Roberson LIST_REMOVE(slab, us_link); 2631ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 2632ab3185d1SJeff Roberson return (slab); 2633ab3185d1SJeff Roberson } 2634ab3185d1SJeff Roberson if (rr) 2635ab3185d1SJeff Roberson domain = (domain + 1) % vm_ndomains; 2636ab3185d1SJeff Roberson } while (domain != start); 2637ab3185d1SJeff Roberson 2638ab3185d1SJeff Roberson return (NULL); 2639ab3185d1SJeff Roberson } 2640ab3185d1SJeff Roberson 2641ab3185d1SJeff Roberson static uma_slab_t 2642194a979eSMark Johnston keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags) 2643ab3185d1SJeff Roberson { 2644194a979eSMark Johnston uint32_t reserve; 2645099a0e58SBosko Milekic 2646bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 2647194a979eSMark Johnston 2648194a979eSMark Johnston reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve; 2649194a979eSMark Johnston if (keg->uk_free <= reserve) 2650194a979eSMark Johnston return (NULL); 2651194a979eSMark Johnston return (keg_first_slab(keg, domain, rr)); 2652194a979eSMark Johnston } 2653194a979eSMark Johnston 2654194a979eSMark Johnston static uma_slab_t 2655194a979eSMark Johnston keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags) 2656194a979eSMark Johnston { 2657194a979eSMark Johnston struct vm_domainset_iter di; 2658194a979eSMark Johnston uma_domain_t dom; 2659194a979eSMark Johnston uma_slab_t slab; 2660194a979eSMark Johnston int aflags, domain; 2661194a979eSMark Johnston bool rr; 2662194a979eSMark Johnston 2663194a979eSMark Johnston restart: 2664bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 2665bbee39c6SJeff Roberson 2666bbee39c6SJeff Roberson /* 2667194a979eSMark Johnston * Use the keg's policy if upper layers haven't already specified a 2668194a979eSMark Johnston * domain (as happens with first-touch zones). 2669194a979eSMark Johnston * 2670194a979eSMark Johnston * To avoid races we run the iterator with the keg lock held, but that 2671194a979eSMark Johnston * means that we cannot allow the vm_domainset layer to sleep. Thus, 2672194a979eSMark Johnston * clear M_WAITOK and handle low memory conditions locally. 2673bbee39c6SJeff Roberson */ 2674ab3185d1SJeff Roberson rr = rdomain == UMA_ANYDOMAIN; 2675ab3185d1SJeff Roberson if (rr) { 2676194a979eSMark Johnston aflags = (flags & ~M_WAITOK) | M_NOWAIT; 2677194a979eSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, 2678194a979eSMark Johnston &aflags); 2679194a979eSMark Johnston } else { 2680194a979eSMark Johnston aflags = flags; 2681194a979eSMark Johnston domain = rdomain; 2682194a979eSMark Johnston } 2683ab3185d1SJeff Roberson 2684194a979eSMark Johnston for (;;) { 2685194a979eSMark Johnston slab = keg_fetch_free_slab(keg, domain, rr, flags); 2686194a979eSMark Johnston if (slab != NULL) { 2687e20a199fSJeff Roberson MPASS(slab->us_keg == keg); 2688bbee39c6SJeff Roberson return (slab); 2689bbee39c6SJeff Roberson } 2690bbee39c6SJeff Roberson 2691bbee39c6SJeff Roberson /* 2692bbee39c6SJeff Roberson * M_NOVM means don't ask at all! 2693bbee39c6SJeff Roberson */ 2694bbee39c6SJeff Roberson if (flags & M_NOVM) 2695bbee39c6SJeff Roberson break; 2696bbee39c6SJeff Roberson 2697bb15d1c7SGleb Smirnoff KASSERT(zone->uz_max_items == 0 || 2698bb15d1c7SGleb Smirnoff zone->uz_items <= zone->uz_max_items, 2699bb15d1c7SGleb Smirnoff ("%s: zone %p overflow", __func__, zone)); 2700bb15d1c7SGleb Smirnoff 2701194a979eSMark Johnston slab = keg_alloc_slab(keg, zone, domain, aflags); 2702bbee39c6SJeff Roberson /* 2703bbee39c6SJeff Roberson * If we got a slab here it's safe to mark it partially used 2704bbee39c6SJeff Roberson * and return. We assume that the caller is going to remove 2705bbee39c6SJeff Roberson * at least one item. 2706bbee39c6SJeff Roberson */ 2707bbee39c6SJeff Roberson if (slab) { 2708e20a199fSJeff Roberson MPASS(slab->us_keg == keg); 2709ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 2710ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 2711bbee39c6SJeff Roberson return (slab); 2712bbee39c6SJeff Roberson } 2713194a979eSMark Johnston KEG_LOCK(keg); 2714194a979eSMark Johnston if (rr && vm_domainset_iter_policy(&di, &domain) != 0) { 2715194a979eSMark Johnston if ((flags & M_WAITOK) != 0) { 2716194a979eSMark Johnston KEG_UNLOCK(keg); 2717194a979eSMark Johnston vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask); 2718194a979eSMark Johnston KEG_LOCK(keg); 2719194a979eSMark Johnston goto restart; 272030c5525bSAndrew Gallatin } 2721194a979eSMark Johnston break; 2722194a979eSMark Johnston } 2723ab3185d1SJeff Roberson } 2724ab3185d1SJeff Roberson 2725bbee39c6SJeff Roberson /* 2726bbee39c6SJeff Roberson * We might not have been able to get a slab but another cpu 2727bbee39c6SJeff Roberson * could have while we were unlocked. Check again before we 2728bbee39c6SJeff Roberson * fail. 2729bbee39c6SJeff Roberson */ 2730194a979eSMark Johnston if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) { 2731ab3185d1SJeff Roberson MPASS(slab->us_keg == keg); 2732bbee39c6SJeff Roberson return (slab); 2733bbee39c6SJeff Roberson } 2734ab3185d1SJeff Roberson return (NULL); 2735ab3185d1SJeff Roberson } 2736bbee39c6SJeff Roberson 2737e20a199fSJeff Roberson static uma_slab_t 2738ab3185d1SJeff Roberson zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int domain, int flags) 2739e20a199fSJeff Roberson { 2740e20a199fSJeff Roberson uma_slab_t slab; 2741e20a199fSJeff Roberson 2742af526374SJeff Roberson if (keg == NULL) { 2743bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 2744af526374SJeff Roberson KEG_LOCK(keg); 2745af526374SJeff Roberson } 2746e20a199fSJeff Roberson 2747e20a199fSJeff Roberson for (;;) { 2748ab3185d1SJeff Roberson slab = keg_fetch_slab(keg, zone, domain, flags); 2749e20a199fSJeff Roberson if (slab) 2750e20a199fSJeff Roberson return (slab); 2751e20a199fSJeff Roberson if (flags & (M_NOWAIT | M_NOVM)) 2752e20a199fSJeff Roberson break; 2753e20a199fSJeff Roberson } 2754af526374SJeff Roberson KEG_UNLOCK(keg); 2755e20a199fSJeff Roberson return (NULL); 2756e20a199fSJeff Roberson } 2757e20a199fSJeff Roberson 2758d56368d7SBosko Milekic static void * 27590095a784SJeff Roberson slab_alloc_item(uma_keg_t keg, uma_slab_t slab) 2760bbee39c6SJeff Roberson { 2761ab3185d1SJeff Roberson uma_domain_t dom; 2762bbee39c6SJeff Roberson void *item; 276385dcf349SGleb Smirnoff uint8_t freei; 2764bbee39c6SJeff Roberson 27650095a784SJeff Roberson MPASS(keg == slab->us_keg); 2766bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 2767099a0e58SBosko Milekic 2768ef72505eSJeff Roberson freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1; 2769ef72505eSJeff Roberson BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free); 2770099a0e58SBosko Milekic item = slab->us_data + (keg->uk_rsize * freei); 2771bbee39c6SJeff Roberson slab->us_freecount--; 2772099a0e58SBosko Milekic keg->uk_free--; 2773ef72505eSJeff Roberson 2774bbee39c6SJeff Roberson /* Move this slab to the full list */ 2775bbee39c6SJeff Roberson if (slab->us_freecount == 0) { 2776bbee39c6SJeff Roberson LIST_REMOVE(slab, us_link); 2777ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 2778ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link); 2779bbee39c6SJeff Roberson } 2780bbee39c6SJeff Roberson 2781bbee39c6SJeff Roberson return (item); 2782bbee39c6SJeff Roberson } 2783bbee39c6SJeff Roberson 2784bbee39c6SJeff Roberson static int 2785ab3185d1SJeff Roberson zone_import(uma_zone_t zone, void **bucket, int max, int domain, int flags) 27860095a784SJeff Roberson { 27870095a784SJeff Roberson uma_slab_t slab; 27880095a784SJeff Roberson uma_keg_t keg; 2789a03af342SSean Bruno #ifdef NUMA 2790ab3185d1SJeff Roberson int stripe; 2791a03af342SSean Bruno #endif 27920095a784SJeff Roberson int i; 27930095a784SJeff Roberson 27940095a784SJeff Roberson slab = NULL; 27950095a784SJeff Roberson keg = NULL; 2796af526374SJeff Roberson /* Try to keep the buckets totally full */ 27970095a784SJeff Roberson for (i = 0; i < max; ) { 2798ab3185d1SJeff Roberson if ((slab = zone->uz_slab(zone, keg, domain, flags)) == NULL) 27990095a784SJeff Roberson break; 28000095a784SJeff Roberson keg = slab->us_keg; 2801a03af342SSean Bruno #ifdef NUMA 2802ab3185d1SJeff Roberson stripe = howmany(max, vm_ndomains); 2803a03af342SSean Bruno #endif 28046fd34d6fSJeff Roberson while (slab->us_freecount && i < max) { 28050095a784SJeff Roberson bucket[i++] = slab_alloc_item(keg, slab); 28066fd34d6fSJeff Roberson if (keg->uk_free <= keg->uk_reserve) 28076fd34d6fSJeff Roberson break; 2808b6715dabSJeff Roberson #ifdef NUMA 2809ab3185d1SJeff Roberson /* 2810ab3185d1SJeff Roberson * If the zone is striped we pick a new slab for every 2811ab3185d1SJeff Roberson * N allocations. Eliminating this conditional will 2812ab3185d1SJeff Roberson * instead pick a new domain for each bucket rather 2813ab3185d1SJeff Roberson * than stripe within each bucket. The current option 2814ab3185d1SJeff Roberson * produces more fragmentation and requires more cpu 2815ab3185d1SJeff Roberson * time but yields better distribution. 2816ab3185d1SJeff Roberson */ 2817ab3185d1SJeff Roberson if ((zone->uz_flags & UMA_ZONE_NUMA) == 0 && 2818ab3185d1SJeff Roberson vm_ndomains > 1 && --stripe == 0) 2819ab3185d1SJeff Roberson break; 2820ab3185d1SJeff Roberson #endif 28216fd34d6fSJeff Roberson } 2822ab3185d1SJeff Roberson /* Don't block if we allocated any successfully. */ 28230095a784SJeff Roberson flags &= ~M_WAITOK; 28240095a784SJeff Roberson flags |= M_NOWAIT; 28250095a784SJeff Roberson } 28260095a784SJeff Roberson if (slab != NULL) 28270095a784SJeff Roberson KEG_UNLOCK(keg); 28280095a784SJeff Roberson 28290095a784SJeff Roberson return i; 28300095a784SJeff Roberson } 28310095a784SJeff Roberson 2832fc03d22bSJeff Roberson static uma_bucket_t 2833bb15d1c7SGleb Smirnoff zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags, int max) 2834bbee39c6SJeff Roberson { 2835bbee39c6SJeff Roberson uma_bucket_t bucket; 2836bbee39c6SJeff Roberson 283730c5525bSAndrew Gallatin CTR1(KTR_UMA, "zone_alloc:_bucket domain %d)", domain); 283830c5525bSAndrew Gallatin 28396fd34d6fSJeff Roberson /* Don't wait for buckets, preserve caller's NOVM setting. */ 28406fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM)); 28410095a784SJeff Roberson if (bucket == NULL) 2842f7104ccdSAlexander Motin return (NULL); 28430095a784SJeff Roberson 28440095a784SJeff Roberson bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket, 2845ab3185d1SJeff Roberson max, domain, flags); 28460095a784SJeff Roberson 28470095a784SJeff Roberson /* 28480095a784SJeff Roberson * Initialize the memory if necessary. 28490095a784SJeff Roberson */ 28500095a784SJeff Roberson if (bucket->ub_cnt != 0 && zone->uz_init != NULL) { 2851099a0e58SBosko Milekic int i; 2852bbee39c6SJeff Roberson 28530095a784SJeff Roberson for (i = 0; i < bucket->ub_cnt; i++) 2854e20a199fSJeff Roberson if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size, 28550095a784SJeff Roberson flags) != 0) 2856b23f72e9SBrian Feldman break; 2857b23f72e9SBrian Feldman /* 2858b23f72e9SBrian Feldman * If we couldn't initialize the whole bucket, put the 2859b23f72e9SBrian Feldman * rest back onto the freelist. 2860b23f72e9SBrian Feldman */ 2861b23f72e9SBrian Feldman if (i != bucket->ub_cnt) { 2862af526374SJeff Roberson zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i], 28630095a784SJeff Roberson bucket->ub_cnt - i); 2864a5a262c6SBosko Milekic #ifdef INVARIANTS 28650095a784SJeff Roberson bzero(&bucket->ub_bucket[i], 28660095a784SJeff Roberson sizeof(void *) * (bucket->ub_cnt - i)); 2867a5a262c6SBosko Milekic #endif 2868b23f72e9SBrian Feldman bucket->ub_cnt = i; 2869b23f72e9SBrian Feldman } 2870099a0e58SBosko Milekic } 2871099a0e58SBosko Milekic 2872f7104ccdSAlexander Motin if (bucket->ub_cnt == 0) { 28736fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 28742efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 2875fc03d22bSJeff Roberson return (NULL); 2876bbee39c6SJeff Roberson } 2877fc03d22bSJeff Roberson 2878fc03d22bSJeff Roberson return (bucket); 2879fc03d22bSJeff Roberson } 2880fc03d22bSJeff Roberson 28818355f576SJeff Roberson /* 28820095a784SJeff Roberson * Allocates a single item from a zone. 28838355f576SJeff Roberson * 28848355f576SJeff Roberson * Arguments 28858355f576SJeff Roberson * zone The zone to alloc for. 28868355f576SJeff Roberson * udata The data to be passed to the constructor. 2887ab3185d1SJeff Roberson * domain The domain to allocate from or UMA_ANYDOMAIN. 2888a163d034SWarner Losh * flags M_WAITOK, M_NOWAIT, M_ZERO. 28898355f576SJeff Roberson * 28908355f576SJeff Roberson * Returns 28918355f576SJeff Roberson * NULL if there is no memory and M_NOWAIT is set 2892bbee39c6SJeff Roberson * An item if successful 28938355f576SJeff Roberson */ 28948355f576SJeff Roberson 28958355f576SJeff Roberson static void * 2896ab3185d1SJeff Roberson zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags) 28978355f576SJeff Roberson { 2898bb15d1c7SGleb Smirnoff 2899bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 2900bb15d1c7SGleb Smirnoff return (zone_alloc_item_locked(zone, udata, domain, flags)); 2901bb15d1c7SGleb Smirnoff } 2902bb15d1c7SGleb Smirnoff 2903bb15d1c7SGleb Smirnoff /* 2904bb15d1c7SGleb Smirnoff * Returns with zone unlocked. 2905bb15d1c7SGleb Smirnoff */ 2906bb15d1c7SGleb Smirnoff static void * 2907bb15d1c7SGleb Smirnoff zone_alloc_item_locked(uma_zone_t zone, void *udata, int domain, int flags) 2908bb15d1c7SGleb Smirnoff { 29098355f576SJeff Roberson void *item; 2910c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2911c5deaf04SGleb Smirnoff bool skipdbg; 2912c5deaf04SGleb Smirnoff #endif 29138355f576SJeff Roberson 2914bb15d1c7SGleb Smirnoff ZONE_LOCK_ASSERT(zone); 2915bb15d1c7SGleb Smirnoff 2916bb45b411SGleb Smirnoff if (zone->uz_max_items > 0) { 2917bb45b411SGleb Smirnoff if (zone->uz_items >= zone->uz_max_items) { 2918bb15d1c7SGleb Smirnoff zone_log_warning(zone); 2919bb15d1c7SGleb Smirnoff zone_maxaction(zone); 2920bb15d1c7SGleb Smirnoff if (flags & M_NOWAIT) { 2921bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 2922bb15d1c7SGleb Smirnoff return (NULL); 2923bb15d1c7SGleb Smirnoff } 2924bb15d1c7SGleb Smirnoff zone->uz_sleeps++; 2925bb15d1c7SGleb Smirnoff zone->uz_sleepers++; 2926bb15d1c7SGleb Smirnoff while (zone->uz_items >= zone->uz_max_items) 2927bb15d1c7SGleb Smirnoff mtx_sleep(zone, zone->uz_lockptr, PVM, "zonelimit", 0); 2928bb15d1c7SGleb Smirnoff zone->uz_sleepers--; 2929bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0 && 2930bb15d1c7SGleb Smirnoff zone->uz_items + 1 < zone->uz_max_items) 2931bb15d1c7SGleb Smirnoff wakeup_one(zone); 2932bb15d1c7SGleb Smirnoff } 2933bb15d1c7SGleb Smirnoff zone->uz_items++; 2934bb45b411SGleb Smirnoff } 2935bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 29368355f576SJeff Roberson 293730c5525bSAndrew Gallatin if (domain != UMA_ANYDOMAIN) { 293830c5525bSAndrew Gallatin /* avoid allocs targeting empty domains */ 293930c5525bSAndrew Gallatin if (VM_DOMAIN_EMPTY(domain)) 294030c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 294130c5525bSAndrew Gallatin } 2942ab3185d1SJeff Roberson if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1) 29430095a784SJeff Roberson goto fail; 29448355f576SJeff Roberson 2945c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2946c5deaf04SGleb Smirnoff skipdbg = uma_dbg_zskip(zone, item); 2947c5deaf04SGleb Smirnoff #endif 2948099a0e58SBosko Milekic /* 2949099a0e58SBosko Milekic * We have to call both the zone's init (not the keg's init) 2950099a0e58SBosko Milekic * and the zone's ctor. This is because the item is going from 2951099a0e58SBosko Milekic * a keg slab directly to the user, and the user is expecting it 2952099a0e58SBosko Milekic * to be both zone-init'd as well as zone-ctor'd. 2953099a0e58SBosko Milekic */ 2954b23f72e9SBrian Feldman if (zone->uz_init != NULL) { 2955e20a199fSJeff Roberson if (zone->uz_init(item, zone->uz_size, flags) != 0) { 2956bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT); 29570095a784SJeff Roberson goto fail; 2958b23f72e9SBrian Feldman } 2959b23f72e9SBrian Feldman } 2960c5deaf04SGleb Smirnoff if (zone->uz_ctor != NULL && 2961c5deaf04SGleb Smirnoff #ifdef INVARIANTS 2962c5deaf04SGleb Smirnoff (!skipdbg || zone->uz_ctor != trash_ctor || 2963c5deaf04SGleb Smirnoff zone->uz_dtor != trash_dtor) && 2964c5deaf04SGleb Smirnoff #endif 2965c5deaf04SGleb Smirnoff zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) { 2966bb15d1c7SGleb Smirnoff zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT); 29670095a784SJeff Roberson goto fail; 2968b23f72e9SBrian Feldman } 2969ef72505eSJeff Roberson #ifdef INVARIANTS 2970c5deaf04SGleb Smirnoff if (!skipdbg) 29710095a784SJeff Roberson uma_dbg_alloc(zone, NULL, item); 2972ef72505eSJeff Roberson #endif 29732cc35ff9SJeff Roberson if (flags & M_ZERO) 297448343a2fSGleb Smirnoff uma_zero_item(item, zone); 29758355f576SJeff Roberson 29762efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_allocs, 1); 29771431a748SGleb Smirnoff CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item, 29781431a748SGleb Smirnoff zone->uz_name, zone); 29791431a748SGleb Smirnoff 29808355f576SJeff Roberson return (item); 29810095a784SJeff Roberson 29820095a784SJeff Roberson fail: 2983bb45b411SGleb Smirnoff if (zone->uz_max_items > 0) { 2984bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 2985bb15d1c7SGleb Smirnoff zone->uz_items--; 2986bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 2987bb45b411SGleb Smirnoff } 29882efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_fails, 1); 29891431a748SGleb Smirnoff CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)", 29901431a748SGleb Smirnoff zone->uz_name, zone); 29910095a784SJeff Roberson return (NULL); 29928355f576SJeff Roberson } 29938355f576SJeff Roberson 29948355f576SJeff Roberson /* See uma.h */ 29958355f576SJeff Roberson void 29968355f576SJeff Roberson uma_zfree_arg(uma_zone_t zone, void *item, void *udata) 29978355f576SJeff Roberson { 29988355f576SJeff Roberson uma_cache_t cache; 29998355f576SJeff Roberson uma_bucket_t bucket; 3000ab3185d1SJeff Roberson uma_zone_domain_t zdom; 3001bb15d1c7SGleb Smirnoff int cpu, domain; 3002bb15d1c7SGleb Smirnoff bool lockfail; 3003c5deaf04SGleb Smirnoff #ifdef INVARIANTS 3004c5deaf04SGleb Smirnoff bool skipdbg; 3005c5deaf04SGleb Smirnoff #endif 30068355f576SJeff Roberson 3007e866d8f0SMark Murray /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 300819fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 300910cb2424SMark Murray 30103659f747SRobert Watson CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread, 30113659f747SRobert Watson zone->uz_name); 30123659f747SRobert Watson 3013d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 30141067a2baSJonathan T. Looney ("uma_zfree_arg: called with spinlock or critical section held")); 30151067a2baSJonathan T. Looney 301620ed0cb0SMatthew D Fleming /* uma_zfree(..., NULL) does nothing, to match free(9). */ 301720ed0cb0SMatthew D Fleming if (item == NULL) 301820ed0cb0SMatthew D Fleming return; 30198d689e04SGleb Smirnoff #ifdef DEBUG_MEMGUARD 30208d689e04SGleb Smirnoff if (is_memguard_addr(item)) { 3021bc9d08e1SMark Johnston if (zone->uz_dtor != NULL) 30228d689e04SGleb Smirnoff zone->uz_dtor(item, zone->uz_size, udata); 3023bc9d08e1SMark Johnston if (zone->uz_fini != NULL) 30248d689e04SGleb Smirnoff zone->uz_fini(item, zone->uz_size); 30258d689e04SGleb Smirnoff memguard_free(item); 30268d689e04SGleb Smirnoff return; 30278d689e04SGleb Smirnoff } 30288d689e04SGleb Smirnoff #endif 30295d1ae027SRobert Watson #ifdef INVARIANTS 3030c5deaf04SGleb Smirnoff skipdbg = uma_dbg_zskip(zone, item); 3031c5deaf04SGleb Smirnoff if (skipdbg == false) { 3032e20a199fSJeff Roberson if (zone->uz_flags & UMA_ZONE_MALLOC) 30335d1ae027SRobert Watson uma_dbg_free(zone, udata, item); 30345d1ae027SRobert Watson else 30355d1ae027SRobert Watson uma_dbg_free(zone, NULL, item); 3036c5deaf04SGleb Smirnoff } 3037c5deaf04SGleb Smirnoff if (zone->uz_dtor != NULL && (!skipdbg || 3038c5deaf04SGleb Smirnoff zone->uz_dtor != trash_dtor || zone->uz_ctor != trash_ctor)) 3039c5deaf04SGleb Smirnoff #else 3040fc03d22bSJeff Roberson if (zone->uz_dtor != NULL) 3041c5deaf04SGleb Smirnoff #endif 3042ef72505eSJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 3043ef72505eSJeff Roberson 3044af7f9b97SJeff Roberson /* 3045af7f9b97SJeff Roberson * The race here is acceptable. If we miss it we'll just have to wait 3046af7f9b97SJeff Roberson * a little longer for the limits to be reset. 3047af7f9b97SJeff Roberson */ 3048bb15d1c7SGleb Smirnoff if (zone->uz_sleepers > 0) 3049fc03d22bSJeff Roberson goto zfree_item; 3050af7f9b97SJeff Roberson 30515d1ae027SRobert Watson /* 30525d1ae027SRobert Watson * If possible, free to the per-CPU cache. There are two 30535d1ae027SRobert Watson * requirements for safe access to the per-CPU cache: (1) the thread 30545d1ae027SRobert Watson * accessing the cache must not be preempted or yield during access, 30555d1ae027SRobert Watson * and (2) the thread must not migrate CPUs without switching which 30565d1ae027SRobert Watson * cache it accesses. We rely on a critical section to prevent 30575d1ae027SRobert Watson * preemption and migration. We release the critical section in 30585d1ae027SRobert Watson * order to acquire the zone mutex if we are unable to free to the 30595d1ae027SRobert Watson * current cache; when we re-acquire the critical section, we must 30605d1ae027SRobert Watson * detect and handle migration if it has occurred. 30615d1ae027SRobert Watson */ 3062a553d4b8SJeff Roberson zfree_restart: 30635d1ae027SRobert Watson critical_enter(); 30645d1ae027SRobert Watson cpu = curcpu; 30658355f576SJeff Roberson cache = &zone->uz_cpu[cpu]; 30668355f576SJeff Roberson 30678355f576SJeff Roberson zfree_start: 3068a553d4b8SJeff Roberson /* 3069fc03d22bSJeff Roberson * Try to free into the allocbucket first to give LIFO ordering 3070fc03d22bSJeff Roberson * for cache-hot datastructures. Spill over into the freebucket 3071fc03d22bSJeff Roberson * if necessary. Alloc will swap them if one runs dry. 3072a553d4b8SJeff Roberson */ 3073fc03d22bSJeff Roberson bucket = cache->uc_allocbucket; 3074fc03d22bSJeff Roberson if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries) 3075fc03d22bSJeff Roberson bucket = cache->uc_freebucket; 3076fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) { 3077cae33c14SJeff Roberson KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL, 30788355f576SJeff Roberson ("uma_zfree: Freeing to non free bucket index.")); 3079cae33c14SJeff Roberson bucket->ub_bucket[bucket->ub_cnt] = item; 3080cae33c14SJeff Roberson bucket->ub_cnt++; 3081773df9abSRobert Watson cache->uc_frees++; 30825d1ae027SRobert Watson critical_exit(); 30838355f576SJeff Roberson return; 3084fc03d22bSJeff Roberson } 3085fc03d22bSJeff Roberson 30868355f576SJeff Roberson /* 30875d1ae027SRobert Watson * We must go back the zone, which requires acquiring the zone lock, 30885d1ae027SRobert Watson * which in turn means we must release and re-acquire the critical 30895d1ae027SRobert Watson * section. Since the critical section is released, we may be 30905d1ae027SRobert Watson * preempted or migrate. As such, make sure not to maintain any 30915d1ae027SRobert Watson * thread-local state specific to the cache from prior to releasing 30925d1ae027SRobert Watson * the critical section. 30938355f576SJeff Roberson */ 30945d1ae027SRobert Watson critical_exit(); 3095fc03d22bSJeff Roberson if (zone->uz_count == 0 || bucketdisable) 3096fc03d22bSJeff Roberson goto zfree_item; 3097fc03d22bSJeff Roberson 3098bb15d1c7SGleb Smirnoff lockfail = false; 30994d104ba0SAlexander Motin if (ZONE_TRYLOCK(zone) == 0) { 31004d104ba0SAlexander Motin /* Record contention to size the buckets. */ 31018355f576SJeff Roberson ZONE_LOCK(zone); 3102bb15d1c7SGleb Smirnoff lockfail = true; 31034d104ba0SAlexander Motin } 31045d1ae027SRobert Watson critical_enter(); 31055d1ae027SRobert Watson cpu = curcpu; 31065d1ae027SRobert Watson cache = &zone->uz_cpu[cpu]; 31078355f576SJeff Roberson 31088355f576SJeff Roberson bucket = cache->uc_freebucket; 3109fc03d22bSJeff Roberson if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) { 3110fc03d22bSJeff Roberson ZONE_UNLOCK(zone); 3111fc03d22bSJeff Roberson goto zfree_start; 3112fc03d22bSJeff Roberson } 31138355f576SJeff Roberson cache->uc_freebucket = NULL; 3114afa5d703SMark Johnston /* We are no longer associated with this CPU. */ 3115afa5d703SMark Johnston critical_exit(); 31168355f576SJeff Roberson 311730c5525bSAndrew Gallatin if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) { 3118ab3185d1SJeff Roberson domain = PCPU_GET(domain); 311930c5525bSAndrew Gallatin if (VM_DOMAIN_EMPTY(domain)) 312030c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 312130c5525bSAndrew Gallatin } else 3122ab3185d1SJeff Roberson domain = 0; 3123ab3185d1SJeff Roberson zdom = &zone->uz_domain[0]; 3124ab3185d1SJeff Roberson 31258355f576SJeff Roberson /* Can we throw this on the zone full list? */ 31268355f576SJeff Roberson if (bucket != NULL) { 31271431a748SGleb Smirnoff CTR3(KTR_UMA, 31281431a748SGleb Smirnoff "uma_zfree: zone %s(%p) putting bucket %p on free list", 31291431a748SGleb Smirnoff zone->uz_name, zone, bucket); 3130cae33c14SJeff Roberson /* ub_cnt is pointing to the last free item */ 3131bb15d1c7SGleb Smirnoff KASSERT(bucket->ub_cnt == bucket->ub_entries, 3132bb15d1c7SGleb Smirnoff ("uma_zfree: Attempting to insert not full bucket onto the full list.\n")); 3133bb15d1c7SGleb Smirnoff if (zone->uz_bkt_count >= zone->uz_bkt_max) { 3134e8bb2dc7SJeff Roberson ZONE_UNLOCK(zone); 3135e8bb2dc7SJeff Roberson bucket_drain(zone, bucket); 3136e8bb2dc7SJeff Roberson bucket_free(zone, bucket, udata); 3137e8bb2dc7SJeff Roberson goto zfree_restart; 3138e8bb2dc7SJeff Roberson } else 31390f9b7bf3SMark Johnston zone_put_bucket(zone, zdom, bucket, true); 31408355f576SJeff Roberson } 3141fc03d22bSJeff Roberson 31424d104ba0SAlexander Motin /* 31434d104ba0SAlexander Motin * We bump the uz count when the cache size is insufficient to 31444d104ba0SAlexander Motin * handle the working set. 31454d104ba0SAlexander Motin */ 3146bb15d1c7SGleb Smirnoff if (lockfail && zone->uz_count < zone->uz_count_max) 31474d104ba0SAlexander Motin zone->uz_count++; 3148a553d4b8SJeff Roberson ZONE_UNLOCK(zone); 3149a553d4b8SJeff Roberson 31506fd34d6fSJeff Roberson bucket = bucket_alloc(zone, udata, M_NOWAIT); 31511431a748SGleb Smirnoff CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p", 31521431a748SGleb Smirnoff zone->uz_name, zone, bucket); 31534741dcbfSJeff Roberson if (bucket) { 3154fc03d22bSJeff Roberson critical_enter(); 3155fc03d22bSJeff Roberson cpu = curcpu; 3156fc03d22bSJeff Roberson cache = &zone->uz_cpu[cpu]; 3157ab3185d1SJeff Roberson if (cache->uc_freebucket == NULL && 3158ab3185d1SJeff Roberson ((zone->uz_flags & UMA_ZONE_NUMA) == 0 || 3159ab3185d1SJeff Roberson domain == PCPU_GET(domain))) { 3160fc03d22bSJeff Roberson cache->uc_freebucket = bucket; 3161fc03d22bSJeff Roberson goto zfree_start; 3162fc03d22bSJeff Roberson } 3163fc03d22bSJeff Roberson /* 3164fc03d22bSJeff Roberson * We lost the race, start over. We have to drop our 3165fc03d22bSJeff Roberson * critical section to free the bucket. 3166fc03d22bSJeff Roberson */ 3167fc03d22bSJeff Roberson critical_exit(); 31686fd34d6fSJeff Roberson bucket_free(zone, bucket, udata); 3169a553d4b8SJeff Roberson goto zfree_restart; 31708355f576SJeff Roberson } 31718355f576SJeff Roberson 3172a553d4b8SJeff Roberson /* 3173a553d4b8SJeff Roberson * If nothing else caught this, we'll just do an internal free. 3174a553d4b8SJeff Roberson */ 3175fc03d22bSJeff Roberson zfree_item: 31760095a784SJeff Roberson zone_free_item(zone, item, udata, SKIP_DTOR); 31778355f576SJeff Roberson } 31788355f576SJeff Roberson 3179ab3185d1SJeff Roberson void 3180ab3185d1SJeff Roberson uma_zfree_domain(uma_zone_t zone, void *item, void *udata) 3181ab3185d1SJeff Roberson { 3182ab3185d1SJeff Roberson 3183ab3185d1SJeff Roberson /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ 318419fa89e9SMark Murray random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); 3185ab3185d1SJeff Roberson 3186ab3185d1SJeff Roberson CTR2(KTR_UMA, "uma_zfree_domain thread %x zone %s", curthread, 3187ab3185d1SJeff Roberson zone->uz_name); 3188ab3185d1SJeff Roberson 3189ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 3190ab3185d1SJeff Roberson ("uma_zfree_domain: called with spinlock or critical section held")); 3191ab3185d1SJeff Roberson 3192ab3185d1SJeff Roberson /* uma_zfree(..., NULL) does nothing, to match free(9). */ 3193ab3185d1SJeff Roberson if (item == NULL) 3194ab3185d1SJeff Roberson return; 3195ab3185d1SJeff Roberson zone_free_item(zone, item, udata, SKIP_NONE); 3196ab3185d1SJeff Roberson } 3197ab3185d1SJeff Roberson 31988355f576SJeff Roberson static void 3199bb15d1c7SGleb Smirnoff slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item) 32008355f576SJeff Roberson { 3201bb15d1c7SGleb Smirnoff uma_keg_t keg; 3202ab3185d1SJeff Roberson uma_domain_t dom; 320385dcf349SGleb Smirnoff uint8_t freei; 3204099a0e58SBosko Milekic 3205bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 3206bb15d1c7SGleb Smirnoff MPASS(zone->uz_lockptr == &keg->uk_lock); 3207bb15d1c7SGleb Smirnoff KEG_LOCK_ASSERT(keg); 3208e20a199fSJeff Roberson MPASS(keg == slab->us_keg); 32098355f576SJeff Roberson 3210ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3211ab3185d1SJeff Roberson 32128355f576SJeff Roberson /* Do we need to remove from any lists? */ 3213099a0e58SBosko Milekic if (slab->us_freecount+1 == keg->uk_ipers) { 32148355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3215ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 32168355f576SJeff Roberson } else if (slab->us_freecount == 0) { 32178355f576SJeff Roberson LIST_REMOVE(slab, us_link); 3218ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link); 32198355f576SJeff Roberson } 32208355f576SJeff Roberson 3221ef72505eSJeff Roberson /* Slab management. */ 3222ef72505eSJeff Roberson freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize; 3223ef72505eSJeff Roberson BIT_SET(SLAB_SETSIZE, freei, &slab->us_free); 32248355f576SJeff Roberson slab->us_freecount++; 32258355f576SJeff Roberson 3226ef72505eSJeff Roberson /* Keg statistics. */ 3227099a0e58SBosko Milekic keg->uk_free++; 32280095a784SJeff Roberson } 32290095a784SJeff Roberson 32300095a784SJeff Roberson static void 32310095a784SJeff Roberson zone_release(uma_zone_t zone, void **bucket, int cnt) 32320095a784SJeff Roberson { 32330095a784SJeff Roberson void *item; 32340095a784SJeff Roberson uma_slab_t slab; 32350095a784SJeff Roberson uma_keg_t keg; 32360095a784SJeff Roberson uint8_t *mem; 32370095a784SJeff Roberson int i; 32388355f576SJeff Roberson 3239bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 3240af526374SJeff Roberson KEG_LOCK(keg); 32410095a784SJeff Roberson for (i = 0; i < cnt; i++) { 32420095a784SJeff Roberson item = bucket[i]; 32430095a784SJeff Roberson if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) { 32440095a784SJeff Roberson mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 32450095a784SJeff Roberson if (zone->uz_flags & UMA_ZONE_HASH) { 32460095a784SJeff Roberson slab = hash_sfind(&keg->uk_hash, mem); 32470095a784SJeff Roberson } else { 32480095a784SJeff Roberson mem += keg->uk_pgoff; 32490095a784SJeff Roberson slab = (uma_slab_t)mem; 32500095a784SJeff Roberson } 32510095a784SJeff Roberson } else { 32520095a784SJeff Roberson slab = vtoslab((vm_offset_t)item); 3253bb15d1c7SGleb Smirnoff MPASS(slab->us_keg == keg); 32540095a784SJeff Roberson } 3255bb15d1c7SGleb Smirnoff slab_free_item(zone, slab, item); 32560095a784SJeff Roberson } 3257af526374SJeff Roberson KEG_UNLOCK(keg); 32588355f576SJeff Roberson } 32598355f576SJeff Roberson 32600095a784SJeff Roberson /* 32610095a784SJeff Roberson * Frees a single item to any zone. 32620095a784SJeff Roberson * 32630095a784SJeff Roberson * Arguments: 32640095a784SJeff Roberson * zone The zone to free to 32650095a784SJeff Roberson * item The item we're freeing 32660095a784SJeff Roberson * udata User supplied data for the dtor 32670095a784SJeff Roberson * skip Skip dtors and finis 32680095a784SJeff Roberson */ 32690095a784SJeff Roberson static void 32700095a784SJeff Roberson zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip) 32710095a784SJeff Roberson { 32720095a784SJeff Roberson #ifdef INVARIANTS 3273c5deaf04SGleb Smirnoff bool skipdbg; 3274c5deaf04SGleb Smirnoff 3275c5deaf04SGleb Smirnoff skipdbg = uma_dbg_zskip(zone, item); 3276c5deaf04SGleb Smirnoff if (skip == SKIP_NONE && !skipdbg) { 32770095a784SJeff Roberson if (zone->uz_flags & UMA_ZONE_MALLOC) 32780095a784SJeff Roberson uma_dbg_free(zone, udata, item); 32790095a784SJeff Roberson else 32800095a784SJeff Roberson uma_dbg_free(zone, NULL, item); 32810095a784SJeff Roberson } 3282c5deaf04SGleb Smirnoff 3283c5deaf04SGleb Smirnoff if (skip < SKIP_DTOR && zone->uz_dtor != NULL && 3284c5deaf04SGleb Smirnoff (!skipdbg || zone->uz_dtor != trash_dtor || 3285c5deaf04SGleb Smirnoff zone->uz_ctor != trash_ctor)) 3286c5deaf04SGleb Smirnoff #else 3287c5deaf04SGleb Smirnoff if (skip < SKIP_DTOR && zone->uz_dtor != NULL) 32880095a784SJeff Roberson #endif 32890095a784SJeff Roberson zone->uz_dtor(item, zone->uz_size, udata); 32900095a784SJeff Roberson 32910095a784SJeff Roberson if (skip < SKIP_FINI && zone->uz_fini) 32920095a784SJeff Roberson zone->uz_fini(item, zone->uz_size); 32930095a784SJeff Roberson 32940095a784SJeff Roberson zone->uz_release(zone->uz_arg, &item, 1); 3295bb15d1c7SGleb Smirnoff 3296bb15d1c7SGleb Smirnoff if (skip & SKIP_CNT) 3297bb15d1c7SGleb Smirnoff return; 3298bb15d1c7SGleb Smirnoff 32992efcc8cbSGleb Smirnoff counter_u64_add(zone->uz_frees, 1); 33002efcc8cbSGleb Smirnoff 3301bb45b411SGleb Smirnoff if (zone->uz_max_items > 0) { 3302bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 3303bb15d1c7SGleb Smirnoff zone->uz_items--; 3304bb45b411SGleb Smirnoff if (zone->uz_sleepers > 0 && 3305bb45b411SGleb Smirnoff zone->uz_items < zone->uz_max_items) 3306bb15d1c7SGleb Smirnoff wakeup_one(zone); 3307bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 33080095a784SJeff Roberson } 3309bb45b411SGleb Smirnoff } 33100095a784SJeff Roberson 33118355f576SJeff Roberson /* See uma.h */ 33121c6cae97SLawrence Stewart int 3313736ee590SJeff Roberson uma_zone_set_max(uma_zone_t zone, int nitems) 3314736ee590SJeff Roberson { 3315bb15d1c7SGleb Smirnoff struct uma_bucket_zone *ubz; 3316099a0e58SBosko Milekic 3317bb15d1c7SGleb Smirnoff /* 3318bb15d1c7SGleb Smirnoff * If limit is very low we may need to limit how 3319bb15d1c7SGleb Smirnoff * much items are allowed in CPU caches. 3320bb15d1c7SGleb Smirnoff */ 3321bb15d1c7SGleb Smirnoff ubz = &bucket_zones[0]; 3322bb15d1c7SGleb Smirnoff for (; ubz->ubz_entries != 0; ubz++) 3323bb15d1c7SGleb Smirnoff if (ubz->ubz_entries * 2 * mp_ncpus > nitems) 3324bb15d1c7SGleb Smirnoff break; 3325bb15d1c7SGleb Smirnoff if (ubz == &bucket_zones[0]) 3326bb15d1c7SGleb Smirnoff nitems = ubz->ubz_entries * 2 * mp_ncpus; 3327bb15d1c7SGleb Smirnoff else 3328bb15d1c7SGleb Smirnoff ubz--; 3329bb15d1c7SGleb Smirnoff 3330bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 3331bb15d1c7SGleb Smirnoff zone->uz_count_max = zone->uz_count = ubz->ubz_entries; 3332bb15d1c7SGleb Smirnoff if (zone->uz_count_min > zone->uz_count_max) 3333bb15d1c7SGleb Smirnoff zone->uz_count_min = zone->uz_count_max; 3334bb15d1c7SGleb Smirnoff zone->uz_max_items = nitems; 3335bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 3336bb15d1c7SGleb Smirnoff 3337bb15d1c7SGleb Smirnoff return (nitems); 3338bb15d1c7SGleb Smirnoff } 3339bb15d1c7SGleb Smirnoff 3340bb15d1c7SGleb Smirnoff /* See uma.h */ 3341bb15d1c7SGleb Smirnoff int 3342bb15d1c7SGleb Smirnoff uma_zone_set_maxcache(uma_zone_t zone, int nitems) 3343bb15d1c7SGleb Smirnoff { 3344bb15d1c7SGleb Smirnoff 3345bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 3346bb15d1c7SGleb Smirnoff zone->uz_bkt_max = nitems; 3347bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 33481c6cae97SLawrence Stewart 33491c6cae97SLawrence Stewart return (nitems); 3350736ee590SJeff Roberson } 3351736ee590SJeff Roberson 3352736ee590SJeff Roberson /* See uma.h */ 3353e49471b0SAndre Oppermann int 3354e49471b0SAndre Oppermann uma_zone_get_max(uma_zone_t zone) 3355e49471b0SAndre Oppermann { 3356e49471b0SAndre Oppermann int nitems; 3357e49471b0SAndre Oppermann 3358bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 3359bb15d1c7SGleb Smirnoff nitems = zone->uz_max_items; 3360bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 3361e49471b0SAndre Oppermann 3362e49471b0SAndre Oppermann return (nitems); 3363e49471b0SAndre Oppermann } 3364e49471b0SAndre Oppermann 3365e49471b0SAndre Oppermann /* See uma.h */ 33662f891cd5SPawel Jakub Dawidek void 33672f891cd5SPawel Jakub Dawidek uma_zone_set_warning(uma_zone_t zone, const char *warning) 33682f891cd5SPawel Jakub Dawidek { 33692f891cd5SPawel Jakub Dawidek 33702f891cd5SPawel Jakub Dawidek ZONE_LOCK(zone); 33712f891cd5SPawel Jakub Dawidek zone->uz_warning = warning; 33722f891cd5SPawel Jakub Dawidek ZONE_UNLOCK(zone); 33732f891cd5SPawel Jakub Dawidek } 33742f891cd5SPawel Jakub Dawidek 33752f891cd5SPawel Jakub Dawidek /* See uma.h */ 337654503a13SJonathan T. Looney void 337754503a13SJonathan T. Looney uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction) 337854503a13SJonathan T. Looney { 337954503a13SJonathan T. Looney 338054503a13SJonathan T. Looney ZONE_LOCK(zone); 3381e60b2fcbSGleb Smirnoff TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone); 338254503a13SJonathan T. Looney ZONE_UNLOCK(zone); 338354503a13SJonathan T. Looney } 338454503a13SJonathan T. Looney 338554503a13SJonathan T. Looney /* See uma.h */ 3386c4ae7908SLawrence Stewart int 3387c4ae7908SLawrence Stewart uma_zone_get_cur(uma_zone_t zone) 3388c4ae7908SLawrence Stewart { 3389c4ae7908SLawrence Stewart int64_t nitems; 3390c4ae7908SLawrence Stewart u_int i; 3391c4ae7908SLawrence Stewart 3392c4ae7908SLawrence Stewart ZONE_LOCK(zone); 33932efcc8cbSGleb Smirnoff nitems = counter_u64_fetch(zone->uz_allocs) - 33942efcc8cbSGleb Smirnoff counter_u64_fetch(zone->uz_frees); 3395c4ae7908SLawrence Stewart CPU_FOREACH(i) { 3396c4ae7908SLawrence Stewart /* 3397c4ae7908SLawrence Stewart * See the comment in sysctl_vm_zone_stats() regarding the 3398c4ae7908SLawrence Stewart * safety of accessing the per-cpu caches. With the zone lock 3399c4ae7908SLawrence Stewart * held, it is safe, but can potentially result in stale data. 3400c4ae7908SLawrence Stewart */ 3401c4ae7908SLawrence Stewart nitems += zone->uz_cpu[i].uc_allocs - 3402c4ae7908SLawrence Stewart zone->uz_cpu[i].uc_frees; 3403c4ae7908SLawrence Stewart } 3404c4ae7908SLawrence Stewart ZONE_UNLOCK(zone); 3405c4ae7908SLawrence Stewart 3406c4ae7908SLawrence Stewart return (nitems < 0 ? 0 : nitems); 3407c4ae7908SLawrence Stewart } 3408c4ae7908SLawrence Stewart 3409c4ae7908SLawrence Stewart /* See uma.h */ 3410736ee590SJeff Roberson void 3411099a0e58SBosko Milekic uma_zone_set_init(uma_zone_t zone, uma_init uminit) 3412099a0e58SBosko Milekic { 3413e20a199fSJeff Roberson uma_keg_t keg; 3414e20a199fSJeff Roberson 3415bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 3416af526374SJeff Roberson KEG_LOCK(keg); 3417e20a199fSJeff Roberson KASSERT(keg->uk_pages == 0, 3418099a0e58SBosko Milekic ("uma_zone_set_init on non-empty keg")); 3419e20a199fSJeff Roberson keg->uk_init = uminit; 3420af526374SJeff Roberson KEG_UNLOCK(keg); 3421099a0e58SBosko Milekic } 3422099a0e58SBosko Milekic 3423099a0e58SBosko Milekic /* See uma.h */ 3424099a0e58SBosko Milekic void 3425099a0e58SBosko Milekic uma_zone_set_fini(uma_zone_t zone, uma_fini fini) 3426099a0e58SBosko Milekic { 3427e20a199fSJeff Roberson uma_keg_t keg; 3428e20a199fSJeff Roberson 3429bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 3430af526374SJeff Roberson KEG_LOCK(keg); 3431e20a199fSJeff Roberson KASSERT(keg->uk_pages == 0, 3432099a0e58SBosko Milekic ("uma_zone_set_fini on non-empty keg")); 3433e20a199fSJeff Roberson keg->uk_fini = fini; 3434af526374SJeff Roberson KEG_UNLOCK(keg); 3435099a0e58SBosko Milekic } 3436099a0e58SBosko Milekic 3437099a0e58SBosko Milekic /* See uma.h */ 3438099a0e58SBosko Milekic void 3439099a0e58SBosko Milekic uma_zone_set_zinit(uma_zone_t zone, uma_init zinit) 3440099a0e58SBosko Milekic { 3441af526374SJeff Roberson 3442099a0e58SBosko Milekic ZONE_LOCK(zone); 3443bb15d1c7SGleb Smirnoff KASSERT(zone->uz_keg->uk_pages == 0, 3444099a0e58SBosko Milekic ("uma_zone_set_zinit on non-empty keg")); 3445099a0e58SBosko Milekic zone->uz_init = zinit; 3446099a0e58SBosko Milekic ZONE_UNLOCK(zone); 3447099a0e58SBosko Milekic } 3448099a0e58SBosko Milekic 3449099a0e58SBosko Milekic /* See uma.h */ 3450099a0e58SBosko Milekic void 3451099a0e58SBosko Milekic uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini) 3452099a0e58SBosko Milekic { 3453af526374SJeff Roberson 3454099a0e58SBosko Milekic ZONE_LOCK(zone); 3455bb15d1c7SGleb Smirnoff KASSERT(zone->uz_keg->uk_pages == 0, 3456099a0e58SBosko Milekic ("uma_zone_set_zfini on non-empty keg")); 3457099a0e58SBosko Milekic zone->uz_fini = zfini; 3458099a0e58SBosko Milekic ZONE_UNLOCK(zone); 3459099a0e58SBosko Milekic } 3460099a0e58SBosko Milekic 3461099a0e58SBosko Milekic /* See uma.h */ 3462b23f72e9SBrian Feldman /* XXX uk_freef is not actually used with the zone locked */ 3463099a0e58SBosko Milekic void 34648355f576SJeff Roberson uma_zone_set_freef(uma_zone_t zone, uma_free freef) 34658355f576SJeff Roberson { 34660095a784SJeff Roberson uma_keg_t keg; 3467e20a199fSJeff Roberson 3468bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 34691d2c0c46SDmitry Chagin KASSERT(keg != NULL, ("uma_zone_set_freef: Invalid zone type")); 3470af526374SJeff Roberson KEG_LOCK(keg); 34710095a784SJeff Roberson keg->uk_freef = freef; 3472af526374SJeff Roberson KEG_UNLOCK(keg); 34738355f576SJeff Roberson } 34748355f576SJeff Roberson 34758355f576SJeff Roberson /* See uma.h */ 3476b23f72e9SBrian Feldman /* XXX uk_allocf is not actually used with the zone locked */ 34778355f576SJeff Roberson void 34788355f576SJeff Roberson uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf) 34798355f576SJeff Roberson { 3480e20a199fSJeff Roberson uma_keg_t keg; 3481e20a199fSJeff Roberson 3482bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 3483af526374SJeff Roberson KEG_LOCK(keg); 3484e20a199fSJeff Roberson keg->uk_allocf = allocf; 3485af526374SJeff Roberson KEG_UNLOCK(keg); 34868355f576SJeff Roberson } 34878355f576SJeff Roberson 34888355f576SJeff Roberson /* See uma.h */ 34896fd34d6fSJeff Roberson void 34906fd34d6fSJeff Roberson uma_zone_reserve(uma_zone_t zone, int items) 34916fd34d6fSJeff Roberson { 34926fd34d6fSJeff Roberson uma_keg_t keg; 34936fd34d6fSJeff Roberson 3494bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 34956fd34d6fSJeff Roberson KEG_LOCK(keg); 34966fd34d6fSJeff Roberson keg->uk_reserve = items; 34976fd34d6fSJeff Roberson KEG_UNLOCK(keg); 34986fd34d6fSJeff Roberson } 34996fd34d6fSJeff Roberson 35006fd34d6fSJeff Roberson /* See uma.h */ 35018355f576SJeff Roberson int 3502a4915c21SAttilio Rao uma_zone_reserve_kva(uma_zone_t zone, int count) 35038355f576SJeff Roberson { 3504099a0e58SBosko Milekic uma_keg_t keg; 35058355f576SJeff Roberson vm_offset_t kva; 35069ba30bcbSZbigniew Bodek u_int pages; 35078355f576SJeff Roberson 3508bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 35098355f576SJeff Roberson 3510bb15d1c7SGleb Smirnoff pages = count / keg->uk_ipers; 3511099a0e58SBosko Milekic if (pages * keg->uk_ipers < count) 35128355f576SJeff Roberson pages++; 351357223e99SAndriy Gapon pages *= keg->uk_ppera; 3514a553d4b8SJeff Roberson 3515a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 3516a4915c21SAttilio Rao if (keg->uk_ppera > 1) { 3517a4915c21SAttilio Rao #else 3518a4915c21SAttilio Rao if (1) { 3519a4915c21SAttilio Rao #endif 352057223e99SAndriy Gapon kva = kva_alloc((vm_size_t)pages * PAGE_SIZE); 3521d1f42ac2SAlan Cox if (kva == 0) 35228355f576SJeff Roberson return (0); 3523a4915c21SAttilio Rao } else 3524a4915c21SAttilio Rao kva = 0; 3525bb15d1c7SGleb Smirnoff 3526bb15d1c7SGleb Smirnoff ZONE_LOCK(zone); 3527bb15d1c7SGleb Smirnoff MPASS(keg->uk_kva == 0); 3528099a0e58SBosko Milekic keg->uk_kva = kva; 3529a4915c21SAttilio Rao keg->uk_offset = 0; 3530bb15d1c7SGleb Smirnoff zone->uz_max_items = pages * keg->uk_ipers; 3531a4915c21SAttilio Rao #ifdef UMA_MD_SMALL_ALLOC 3532a4915c21SAttilio Rao keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc; 3533a4915c21SAttilio Rao #else 3534a4915c21SAttilio Rao keg->uk_allocf = noobj_alloc; 3535a4915c21SAttilio Rao #endif 35366fd34d6fSJeff Roberson keg->uk_flags |= UMA_ZONE_NOFREE; 3537bb15d1c7SGleb Smirnoff ZONE_UNLOCK(zone); 3538af526374SJeff Roberson 35398355f576SJeff Roberson return (1); 35408355f576SJeff Roberson } 35418355f576SJeff Roberson 35428355f576SJeff Roberson /* See uma.h */ 35438355f576SJeff Roberson void 35448355f576SJeff Roberson uma_prealloc(uma_zone_t zone, int items) 35458355f576SJeff Roberson { 3546920239efSMark Johnston struct vm_domainset_iter di; 3547ab3185d1SJeff Roberson uma_domain_t dom; 35488355f576SJeff Roberson uma_slab_t slab; 3549099a0e58SBosko Milekic uma_keg_t keg; 3550920239efSMark Johnston int domain, flags, slabs; 35518355f576SJeff Roberson 3552bb15d1c7SGleb Smirnoff KEG_GET(zone, keg); 3553af526374SJeff Roberson KEG_LOCK(keg); 3554099a0e58SBosko Milekic slabs = items / keg->uk_ipers; 3555099a0e58SBosko Milekic if (slabs * keg->uk_ipers < items) 35568355f576SJeff Roberson slabs++; 3557920239efSMark Johnston flags = M_WAITOK; 3558920239efSMark Johnston vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain, &flags); 3559194a979eSMark Johnston while (slabs-- > 0) { 3560920239efSMark Johnston slab = keg_alloc_slab(keg, zone, domain, flags); 3561e20a199fSJeff Roberson if (slab == NULL) 3562194a979eSMark Johnston return; 3563e20a199fSJeff Roberson MPASS(slab->us_keg == keg); 3564ab3185d1SJeff Roberson dom = &keg->uk_domain[slab->us_domain]; 3565ab3185d1SJeff Roberson LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link); 3566920239efSMark Johnston if (vm_domainset_iter_policy(&di, &domain) != 0) 3567920239efSMark Johnston break; 35688355f576SJeff Roberson } 3569af526374SJeff Roberson KEG_UNLOCK(keg); 35708355f576SJeff Roberson } 35718355f576SJeff Roberson 35728355f576SJeff Roberson /* See uma.h */ 357344ec2b63SKonstantin Belousov static void 357444ec2b63SKonstantin Belousov uma_reclaim_locked(bool kmem_danger) 35758355f576SJeff Roberson { 357644ec2b63SKonstantin Belousov 35771431a748SGleb Smirnoff CTR0(KTR_UMA, "UMA: vm asked us to release pages!"); 357844ec2b63SKonstantin Belousov sx_assert(&uma_drain_lock, SA_XLOCKED); 357986bbae32SJeff Roberson bucket_enable(); 35808355f576SJeff Roberson zone_foreach(zone_drain); 358144ec2b63SKonstantin Belousov if (vm_page_count_min() || kmem_danger) { 3582a2de44abSAlexander Motin cache_drain_safe(NULL); 3583a2de44abSAlexander Motin zone_foreach(zone_drain); 3584a2de44abSAlexander Motin } 35850f9b7bf3SMark Johnston 35868355f576SJeff Roberson /* 35878355f576SJeff Roberson * Some slabs may have been freed but this zone will be visited early 35888355f576SJeff Roberson * we visit again so that we can free pages that are empty once other 35898355f576SJeff Roberson * zones are drained. We have to do the same for buckets. 35908355f576SJeff Roberson */ 35919643769aSJeff Roberson zone_drain(slabzone); 3592cae33c14SJeff Roberson bucket_zone_drain(); 359344ec2b63SKonstantin Belousov } 359444ec2b63SKonstantin Belousov 359544ec2b63SKonstantin Belousov void 359644ec2b63SKonstantin Belousov uma_reclaim(void) 359744ec2b63SKonstantin Belousov { 359844ec2b63SKonstantin Belousov 359944ec2b63SKonstantin Belousov sx_xlock(&uma_drain_lock); 360044ec2b63SKonstantin Belousov uma_reclaim_locked(false); 360195c4bf75SKonstantin Belousov sx_xunlock(&uma_drain_lock); 36028355f576SJeff Roberson } 36038355f576SJeff Roberson 36042e47807cSJeff Roberson static volatile int uma_reclaim_needed; 360544ec2b63SKonstantin Belousov 360644ec2b63SKonstantin Belousov void 360744ec2b63SKonstantin Belousov uma_reclaim_wakeup(void) 360844ec2b63SKonstantin Belousov { 360944ec2b63SKonstantin Belousov 36102e47807cSJeff Roberson if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0) 36112e47807cSJeff Roberson wakeup(uma_reclaim); 361244ec2b63SKonstantin Belousov } 361344ec2b63SKonstantin Belousov 361444ec2b63SKonstantin Belousov void 361544ec2b63SKonstantin Belousov uma_reclaim_worker(void *arg __unused) 361644ec2b63SKonstantin Belousov { 361744ec2b63SKonstantin Belousov 361844ec2b63SKonstantin Belousov for (;;) { 36192e47807cSJeff Roberson sx_xlock(&uma_drain_lock); 3620200f8117SKonstantin Belousov while (atomic_load_int(&uma_reclaim_needed) == 0) 36212e47807cSJeff Roberson sx_sleep(uma_reclaim, &uma_drain_lock, PVM, "umarcl", 36222e47807cSJeff Roberson hz); 36239b43bc27SAndriy Gapon sx_xunlock(&uma_drain_lock); 36249b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM); 36259b43bc27SAndriy Gapon sx_xlock(&uma_drain_lock); 362644ec2b63SKonstantin Belousov uma_reclaim_locked(true); 3627200f8117SKonstantin Belousov atomic_store_int(&uma_reclaim_needed, 0); 36282e47807cSJeff Roberson sx_xunlock(&uma_drain_lock); 36292e47807cSJeff Roberson /* Don't fire more than once per-second. */ 36302e47807cSJeff Roberson pause("umarclslp", hz); 363144ec2b63SKonstantin Belousov } 363244ec2b63SKonstantin Belousov } 363344ec2b63SKonstantin Belousov 3634663b416fSJohn Baldwin /* See uma.h */ 3635663b416fSJohn Baldwin int 3636663b416fSJohn Baldwin uma_zone_exhausted(uma_zone_t zone) 3637663b416fSJohn Baldwin { 3638663b416fSJohn Baldwin int full; 3639663b416fSJohn Baldwin 3640663b416fSJohn Baldwin ZONE_LOCK(zone); 3641bb15d1c7SGleb Smirnoff full = zone->uz_sleepers > 0; 3642663b416fSJohn Baldwin ZONE_UNLOCK(zone); 3643663b416fSJohn Baldwin return (full); 3644663b416fSJohn Baldwin } 3645663b416fSJohn Baldwin 36466c125b8dSMohan Srinivasan int 36476c125b8dSMohan Srinivasan uma_zone_exhausted_nolock(uma_zone_t zone) 36486c125b8dSMohan Srinivasan { 3649bb15d1c7SGleb Smirnoff return (zone->uz_sleepers > 0); 36506c125b8dSMohan Srinivasan } 36516c125b8dSMohan Srinivasan 36528355f576SJeff Roberson void * 3653ab3185d1SJeff Roberson uma_large_malloc_domain(vm_size_t size, int domain, int wait) 36548355f576SJeff Roberson { 36559978bd99SMark Johnston struct domainset *policy; 3656ab3185d1SJeff Roberson vm_offset_t addr; 36578355f576SJeff Roberson uma_slab_t slab; 36588355f576SJeff Roberson 365930c5525bSAndrew Gallatin if (domain != UMA_ANYDOMAIN) { 366030c5525bSAndrew Gallatin /* avoid allocs targeting empty domains */ 366130c5525bSAndrew Gallatin if (VM_DOMAIN_EMPTY(domain)) 366230c5525bSAndrew Gallatin domain = UMA_ANYDOMAIN; 366330c5525bSAndrew Gallatin } 3664ab3185d1SJeff Roberson slab = zone_alloc_item(slabzone, NULL, domain, wait); 36658355f576SJeff Roberson if (slab == NULL) 36668355f576SJeff Roberson return (NULL); 36679978bd99SMark Johnston policy = (domain == UMA_ANYDOMAIN) ? DOMAINSET_RR() : 36689978bd99SMark Johnston DOMAINSET_FIXED(domain); 36699978bd99SMark Johnston addr = kmem_malloc_domainset(policy, size, wait); 3670ab3185d1SJeff Roberson if (addr != 0) { 3671ab3185d1SJeff Roberson vsetslab(addr, slab); 3672ab3185d1SJeff Roberson slab->us_data = (void *)addr; 3673ab3185d1SJeff Roberson slab->us_flags = UMA_SLAB_KERNEL | UMA_SLAB_MALLOC; 36748355f576SJeff Roberson slab->us_size = size; 3675e2068d0bSJeff Roberson slab->us_domain = vm_phys_domain(PHYS_TO_VM_PAGE( 3676ab3185d1SJeff Roberson pmap_kextract(addr))); 36772e47807cSJeff Roberson uma_total_inc(size); 36788355f576SJeff Roberson } else { 36790095a784SJeff Roberson zone_free_item(slabzone, slab, NULL, SKIP_NONE); 36808355f576SJeff Roberson } 36818355f576SJeff Roberson 3682ab3185d1SJeff Roberson return ((void *)addr); 3683ab3185d1SJeff Roberson } 3684ab3185d1SJeff Roberson 3685ab3185d1SJeff Roberson void * 3686ab3185d1SJeff Roberson uma_large_malloc(vm_size_t size, int wait) 3687ab3185d1SJeff Roberson { 3688ab3185d1SJeff Roberson 3689ab3185d1SJeff Roberson return uma_large_malloc_domain(size, UMA_ANYDOMAIN, wait); 36908355f576SJeff Roberson } 36918355f576SJeff Roberson 36928355f576SJeff Roberson void 36938355f576SJeff Roberson uma_large_free(uma_slab_t slab) 36948355f576SJeff Roberson { 3695c325e866SKonstantin Belousov 3696ab3185d1SJeff Roberson KASSERT((slab->us_flags & UMA_SLAB_KERNEL) != 0, 3697ab3185d1SJeff Roberson ("uma_large_free: Memory not allocated with uma_large_malloc.")); 369849bfa624SAlan Cox kmem_free((vm_offset_t)slab->us_data, slab->us_size); 36992e47807cSJeff Roberson uma_total_dec(slab->us_size); 37000095a784SJeff Roberson zone_free_item(slabzone, slab, NULL, SKIP_NONE); 37018355f576SJeff Roberson } 37028355f576SJeff Roberson 370348343a2fSGleb Smirnoff static void 370448343a2fSGleb Smirnoff uma_zero_item(void *item, uma_zone_t zone) 370548343a2fSGleb Smirnoff { 370648343a2fSGleb Smirnoff 370748343a2fSGleb Smirnoff bzero(item, zone->uz_size); 370848343a2fSGleb Smirnoff } 370948343a2fSGleb Smirnoff 37102e47807cSJeff Roberson unsigned long 37112e47807cSJeff Roberson uma_limit(void) 37122e47807cSJeff Roberson { 37132e47807cSJeff Roberson 37142e47807cSJeff Roberson return (uma_kmem_limit); 37152e47807cSJeff Roberson } 37162e47807cSJeff Roberson 37172e47807cSJeff Roberson void 37182e47807cSJeff Roberson uma_set_limit(unsigned long limit) 37192e47807cSJeff Roberson { 37202e47807cSJeff Roberson 37212e47807cSJeff Roberson uma_kmem_limit = limit; 37222e47807cSJeff Roberson } 37232e47807cSJeff Roberson 37242e47807cSJeff Roberson unsigned long 37252e47807cSJeff Roberson uma_size(void) 37262e47807cSJeff Roberson { 37272e47807cSJeff Roberson 3728ad5b0f5bSJeff Roberson return (uma_kmem_total); 3729ad5b0f5bSJeff Roberson } 3730ad5b0f5bSJeff Roberson 3731ad5b0f5bSJeff Roberson long 3732ad5b0f5bSJeff Roberson uma_avail(void) 3733ad5b0f5bSJeff Roberson { 3734ad5b0f5bSJeff Roberson 3735ad5b0f5bSJeff Roberson return (uma_kmem_limit - uma_kmem_total); 37362e47807cSJeff Roberson } 37372e47807cSJeff Roberson 37388355f576SJeff Roberson void 37398355f576SJeff Roberson uma_print_stats(void) 37408355f576SJeff Roberson { 37418355f576SJeff Roberson zone_foreach(uma_print_zone); 37428355f576SJeff Roberson } 37438355f576SJeff Roberson 3744504d5de3SJeff Roberson static void 3745504d5de3SJeff Roberson slab_print(uma_slab_t slab) 3746504d5de3SJeff Roberson { 3747ef72505eSJeff Roberson printf("slab: keg %p, data %p, freecount %d\n", 3748ef72505eSJeff Roberson slab->us_keg, slab->us_data, slab->us_freecount); 3749504d5de3SJeff Roberson } 3750504d5de3SJeff Roberson 3751504d5de3SJeff Roberson static void 3752504d5de3SJeff Roberson cache_print(uma_cache_t cache) 3753504d5de3SJeff Roberson { 3754504d5de3SJeff Roberson printf("alloc: %p(%d), free: %p(%d)\n", 3755504d5de3SJeff Roberson cache->uc_allocbucket, 3756504d5de3SJeff Roberson cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0, 3757504d5de3SJeff Roberson cache->uc_freebucket, 3758504d5de3SJeff Roberson cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0); 3759504d5de3SJeff Roberson } 3760504d5de3SJeff Roberson 3761e20a199fSJeff Roberson static void 3762e20a199fSJeff Roberson uma_print_keg(uma_keg_t keg) 37638355f576SJeff Roberson { 3764ab3185d1SJeff Roberson uma_domain_t dom; 3765504d5de3SJeff Roberson uma_slab_t slab; 3766ab3185d1SJeff Roberson int i; 3767504d5de3SJeff Roberson 37680b80c1e4SEitan Adler printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d " 3769bb15d1c7SGleb Smirnoff "out %d free %d\n", 3770e20a199fSJeff Roberson keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags, 3771099a0e58SBosko Milekic keg->uk_ipers, keg->uk_ppera, 377257223e99SAndriy Gapon (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free, 3773bb15d1c7SGleb Smirnoff keg->uk_free); 3774ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 3775ab3185d1SJeff Roberson dom = &keg->uk_domain[i]; 3776504d5de3SJeff Roberson printf("Part slabs:\n"); 3777ab3185d1SJeff Roberson LIST_FOREACH(slab, &dom->ud_part_slab, us_link) 3778504d5de3SJeff Roberson slab_print(slab); 3779504d5de3SJeff Roberson printf("Free slabs:\n"); 3780ab3185d1SJeff Roberson LIST_FOREACH(slab, &dom->ud_free_slab, us_link) 3781504d5de3SJeff Roberson slab_print(slab); 3782504d5de3SJeff Roberson printf("Full slabs:\n"); 3783ab3185d1SJeff Roberson LIST_FOREACH(slab, &dom->ud_full_slab, us_link) 3784504d5de3SJeff Roberson slab_print(slab); 3785e20a199fSJeff Roberson } 3786ab3185d1SJeff Roberson } 3787e20a199fSJeff Roberson 3788e20a199fSJeff Roberson void 3789e20a199fSJeff Roberson uma_print_zone(uma_zone_t zone) 3790e20a199fSJeff Roberson { 3791e20a199fSJeff Roberson uma_cache_t cache; 3792e20a199fSJeff Roberson int i; 3793e20a199fSJeff Roberson 37945a8eee2bSGleb Smirnoff printf("zone: %s(%p) size %d maxitems %ju flags %#x\n", 37955a8eee2bSGleb Smirnoff zone->uz_name, zone, zone->uz_size, (uintmax_t)zone->uz_max_items, 3796bb15d1c7SGleb Smirnoff zone->uz_flags); 3797bb15d1c7SGleb Smirnoff if (zone->uz_lockptr != &zone->uz_lock) 3798bb15d1c7SGleb Smirnoff uma_print_keg(zone->uz_keg); 37993aa6d94eSJohn Baldwin CPU_FOREACH(i) { 3800504d5de3SJeff Roberson cache = &zone->uz_cpu[i]; 3801504d5de3SJeff Roberson printf("CPU %d Cache:\n", i); 3802504d5de3SJeff Roberson cache_print(cache); 3803504d5de3SJeff Roberson } 38048355f576SJeff Roberson } 38058355f576SJeff Roberson 3806a0d4b0aeSRobert Watson #ifdef DDB 38078355f576SJeff Roberson /* 38087a52a97eSRobert Watson * Generate statistics across both the zone and its per-cpu cache's. Return 38097a52a97eSRobert Watson * desired statistics if the pointer is non-NULL for that statistic. 38107a52a97eSRobert Watson * 38117a52a97eSRobert Watson * Note: does not update the zone statistics, as it can't safely clear the 38127a52a97eSRobert Watson * per-CPU cache statistic. 38137a52a97eSRobert Watson * 38147a52a97eSRobert Watson * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't 38157a52a97eSRobert Watson * safe from off-CPU; we should modify the caches to track this information 38167a52a97eSRobert Watson * directly so that we don't have to. 38177a52a97eSRobert Watson */ 38187a52a97eSRobert Watson static void 38190f9b7bf3SMark Johnston uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp, 382085dcf349SGleb Smirnoff uint64_t *freesp, uint64_t *sleepsp) 38217a52a97eSRobert Watson { 38227a52a97eSRobert Watson uma_cache_t cache; 382385dcf349SGleb Smirnoff uint64_t allocs, frees, sleeps; 38247a52a97eSRobert Watson int cachefree, cpu; 38257a52a97eSRobert Watson 3826bf965959SSean Bruno allocs = frees = sleeps = 0; 38277a52a97eSRobert Watson cachefree = 0; 38283aa6d94eSJohn Baldwin CPU_FOREACH(cpu) { 38297a52a97eSRobert Watson cache = &z->uz_cpu[cpu]; 38307a52a97eSRobert Watson if (cache->uc_allocbucket != NULL) 38317a52a97eSRobert Watson cachefree += cache->uc_allocbucket->ub_cnt; 38327a52a97eSRobert Watson if (cache->uc_freebucket != NULL) 38337a52a97eSRobert Watson cachefree += cache->uc_freebucket->ub_cnt; 38347a52a97eSRobert Watson allocs += cache->uc_allocs; 38357a52a97eSRobert Watson frees += cache->uc_frees; 38367a52a97eSRobert Watson } 38372efcc8cbSGleb Smirnoff allocs += counter_u64_fetch(z->uz_allocs); 38382efcc8cbSGleb Smirnoff frees += counter_u64_fetch(z->uz_frees); 3839bf965959SSean Bruno sleeps += z->uz_sleeps; 38407a52a97eSRobert Watson if (cachefreep != NULL) 38417a52a97eSRobert Watson *cachefreep = cachefree; 38427a52a97eSRobert Watson if (allocsp != NULL) 38437a52a97eSRobert Watson *allocsp = allocs; 38447a52a97eSRobert Watson if (freesp != NULL) 38457a52a97eSRobert Watson *freesp = frees; 3846bf965959SSean Bruno if (sleepsp != NULL) 3847bf965959SSean Bruno *sleepsp = sleeps; 38487a52a97eSRobert Watson } 3849a0d4b0aeSRobert Watson #endif /* DDB */ 38507a52a97eSRobert Watson 38517a52a97eSRobert Watson static int 38527a52a97eSRobert Watson sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS) 38537a52a97eSRobert Watson { 38547a52a97eSRobert Watson uma_keg_t kz; 38557a52a97eSRobert Watson uma_zone_t z; 38567a52a97eSRobert Watson int count; 38577a52a97eSRobert Watson 38587a52a97eSRobert Watson count = 0; 3859111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 38607a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 38617a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 38627a52a97eSRobert Watson count++; 38637a52a97eSRobert Watson } 3864111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 38657a52a97eSRobert Watson return (sysctl_handle_int(oidp, &count, 0, req)); 38667a52a97eSRobert Watson } 38677a52a97eSRobert Watson 38687a52a97eSRobert Watson static int 38697a52a97eSRobert Watson sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS) 38707a52a97eSRobert Watson { 38717a52a97eSRobert Watson struct uma_stream_header ush; 38727a52a97eSRobert Watson struct uma_type_header uth; 387363b5d112SKonstantin Belousov struct uma_percpu_stat *ups; 3874ab3185d1SJeff Roberson uma_zone_domain_t zdom; 38757a52a97eSRobert Watson struct sbuf sbuf; 38767a52a97eSRobert Watson uma_cache_t cache; 38777a52a97eSRobert Watson uma_keg_t kz; 38787a52a97eSRobert Watson uma_zone_t z; 38794e657159SMatthew D Fleming int count, error, i; 38807a52a97eSRobert Watson 388100f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 388200f0e671SMatthew D Fleming if (error != 0) 388300f0e671SMatthew D Fleming return (error); 38844e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 38851eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 388663b5d112SKonstantin Belousov ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK); 38874e657159SMatthew D Fleming 3888404a593eSMatthew D Fleming count = 0; 3889111fbcd5SBryan Venteicher rw_rlock(&uma_rwlock); 38907a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 38917a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) 38927a52a97eSRobert Watson count++; 38937a52a97eSRobert Watson } 38947a52a97eSRobert Watson 38957a52a97eSRobert Watson /* 38967a52a97eSRobert Watson * Insert stream header. 38977a52a97eSRobert Watson */ 38987a52a97eSRobert Watson bzero(&ush, sizeof(ush)); 38997a52a97eSRobert Watson ush.ush_version = UMA_STREAM_VERSION; 3900ab3a57c0SRobert Watson ush.ush_maxcpus = (mp_maxid + 1); 39017a52a97eSRobert Watson ush.ush_count = count; 39024e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &ush, sizeof(ush)); 39037a52a97eSRobert Watson 39047a52a97eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 39057a52a97eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 39067a52a97eSRobert Watson bzero(&uth, sizeof(uth)); 39077a52a97eSRobert Watson ZONE_LOCK(z); 3908cbbb4a00SRobert Watson strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME); 39097a52a97eSRobert Watson uth.uth_align = kz->uk_align; 39107a52a97eSRobert Watson uth.uth_size = kz->uk_size; 39117a52a97eSRobert Watson uth.uth_rsize = kz->uk_rsize; 3912bb45b411SGleb Smirnoff if (z->uz_max_items > 0) 3913bb45b411SGleb Smirnoff uth.uth_pages = (z->uz_items / kz->uk_ipers) * 3914bb15d1c7SGleb Smirnoff kz->uk_ppera; 3915bb45b411SGleb Smirnoff else 3916bb45b411SGleb Smirnoff uth.uth_pages = kz->uk_pages; 3917*f8c86a5fSGleb Smirnoff uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) * 3918bb15d1c7SGleb Smirnoff kz->uk_ppera; 3919bb15d1c7SGleb Smirnoff uth.uth_limit = z->uz_max_items; 3920*f8c86a5fSGleb Smirnoff uth.uth_keg_free = z->uz_keg->uk_free; 3921cbbb4a00SRobert Watson 3922cbbb4a00SRobert Watson /* 3923cbbb4a00SRobert Watson * A zone is secondary is it is not the first entry 3924cbbb4a00SRobert Watson * on the keg's zone list. 3925cbbb4a00SRobert Watson */ 3926e20a199fSJeff Roberson if ((z->uz_flags & UMA_ZONE_SECONDARY) && 3927cbbb4a00SRobert Watson (LIST_FIRST(&kz->uk_zones) != z)) 3928cbbb4a00SRobert Watson uth.uth_zone_flags = UTH_ZONE_SECONDARY; 3929cbbb4a00SRobert Watson 3930ab3185d1SJeff Roberson for (i = 0; i < vm_ndomains; i++) { 3931ab3185d1SJeff Roberson zdom = &z->uz_domain[i]; 39320f9b7bf3SMark Johnston uth.uth_zone_free += zdom->uzd_nitems; 3933ab3185d1SJeff Roberson } 39342efcc8cbSGleb Smirnoff uth.uth_allocs = counter_u64_fetch(z->uz_allocs); 39352efcc8cbSGleb Smirnoff uth.uth_frees = counter_u64_fetch(z->uz_frees); 39362efcc8cbSGleb Smirnoff uth.uth_fails = counter_u64_fetch(z->uz_fails); 3937bf965959SSean Bruno uth.uth_sleeps = z->uz_sleeps; 39387a52a97eSRobert Watson /* 39392450bbb8SRobert Watson * While it is not normally safe to access the cache 39402450bbb8SRobert Watson * bucket pointers while not on the CPU that owns the 39412450bbb8SRobert Watson * cache, we only allow the pointers to be exchanged 39422450bbb8SRobert Watson * without the zone lock held, not invalidated, so 39432450bbb8SRobert Watson * accept the possible race associated with bucket 39442450bbb8SRobert Watson * exchange during monitoring. 39457a52a97eSRobert Watson */ 394663b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) { 394763b5d112SKonstantin Belousov bzero(&ups[i], sizeof(*ups)); 394863b5d112SKonstantin Belousov if (kz->uk_flags & UMA_ZFLAG_INTERNAL || 394963b5d112SKonstantin Belousov CPU_ABSENT(i)) 395063b5d112SKonstantin Belousov continue; 39517a52a97eSRobert Watson cache = &z->uz_cpu[i]; 39527a52a97eSRobert Watson if (cache->uc_allocbucket != NULL) 395363b5d112SKonstantin Belousov ups[i].ups_cache_free += 39547a52a97eSRobert Watson cache->uc_allocbucket->ub_cnt; 39557a52a97eSRobert Watson if (cache->uc_freebucket != NULL) 395663b5d112SKonstantin Belousov ups[i].ups_cache_free += 39577a52a97eSRobert Watson cache->uc_freebucket->ub_cnt; 395863b5d112SKonstantin Belousov ups[i].ups_allocs = cache->uc_allocs; 395963b5d112SKonstantin Belousov ups[i].ups_frees = cache->uc_frees; 39607a52a97eSRobert Watson } 39612450bbb8SRobert Watson ZONE_UNLOCK(z); 396263b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &uth, sizeof(uth)); 396363b5d112SKonstantin Belousov for (i = 0; i < mp_maxid + 1; i++) 396463b5d112SKonstantin Belousov (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i])); 39657a52a97eSRobert Watson } 39667a52a97eSRobert Watson } 3967111fbcd5SBryan Venteicher rw_runlock(&uma_rwlock); 39684e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 39694e657159SMatthew D Fleming sbuf_delete(&sbuf); 397063b5d112SKonstantin Belousov free(ups, M_TEMP); 39717a52a97eSRobert Watson return (error); 39727a52a97eSRobert Watson } 397348c5777eSRobert Watson 39740a5a3ccbSGleb Smirnoff int 39750a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS) 39760a5a3ccbSGleb Smirnoff { 39770a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 397816be9f54SGleb Smirnoff int error, max; 39790a5a3ccbSGleb Smirnoff 398016be9f54SGleb Smirnoff max = uma_zone_get_max(zone); 39810a5a3ccbSGleb Smirnoff error = sysctl_handle_int(oidp, &max, 0, req); 39820a5a3ccbSGleb Smirnoff if (error || !req->newptr) 39830a5a3ccbSGleb Smirnoff return (error); 39840a5a3ccbSGleb Smirnoff 39850a5a3ccbSGleb Smirnoff uma_zone_set_max(zone, max); 39860a5a3ccbSGleb Smirnoff 39870a5a3ccbSGleb Smirnoff return (0); 39880a5a3ccbSGleb Smirnoff } 39890a5a3ccbSGleb Smirnoff 39900a5a3ccbSGleb Smirnoff int 39910a5a3ccbSGleb Smirnoff sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS) 39920a5a3ccbSGleb Smirnoff { 39930a5a3ccbSGleb Smirnoff uma_zone_t zone = *(uma_zone_t *)arg1; 39940a5a3ccbSGleb Smirnoff int cur; 39950a5a3ccbSGleb Smirnoff 39960a5a3ccbSGleb Smirnoff cur = uma_zone_get_cur(zone); 39970a5a3ccbSGleb Smirnoff return (sysctl_handle_int(oidp, &cur, 0, req)); 39980a5a3ccbSGleb Smirnoff } 39990a5a3ccbSGleb Smirnoff 40009542ea7bSGleb Smirnoff #ifdef INVARIANTS 40019542ea7bSGleb Smirnoff static uma_slab_t 40029542ea7bSGleb Smirnoff uma_dbg_getslab(uma_zone_t zone, void *item) 40039542ea7bSGleb Smirnoff { 40049542ea7bSGleb Smirnoff uma_slab_t slab; 40059542ea7bSGleb Smirnoff uma_keg_t keg; 40069542ea7bSGleb Smirnoff uint8_t *mem; 40079542ea7bSGleb Smirnoff 40089542ea7bSGleb Smirnoff mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK)); 40099542ea7bSGleb Smirnoff if (zone->uz_flags & UMA_ZONE_VTOSLAB) { 40109542ea7bSGleb Smirnoff slab = vtoslab((vm_offset_t)mem); 40119542ea7bSGleb Smirnoff } else { 40129542ea7bSGleb Smirnoff /* 40139542ea7bSGleb Smirnoff * It is safe to return the slab here even though the 40149542ea7bSGleb Smirnoff * zone is unlocked because the item's allocation state 40159542ea7bSGleb Smirnoff * essentially holds a reference. 40169542ea7bSGleb Smirnoff */ 4017bb15d1c7SGleb Smirnoff if (zone->uz_lockptr == &zone->uz_lock) 4018bb15d1c7SGleb Smirnoff return (NULL); 40199542ea7bSGleb Smirnoff ZONE_LOCK(zone); 4020bb15d1c7SGleb Smirnoff keg = zone->uz_keg; 40219542ea7bSGleb Smirnoff if (keg->uk_flags & UMA_ZONE_HASH) 40229542ea7bSGleb Smirnoff slab = hash_sfind(&keg->uk_hash, mem); 40239542ea7bSGleb Smirnoff else 40249542ea7bSGleb Smirnoff slab = (uma_slab_t)(mem + keg->uk_pgoff); 40259542ea7bSGleb Smirnoff ZONE_UNLOCK(zone); 40269542ea7bSGleb Smirnoff } 40279542ea7bSGleb Smirnoff 40289542ea7bSGleb Smirnoff return (slab); 40299542ea7bSGleb Smirnoff } 40309542ea7bSGleb Smirnoff 4031c5deaf04SGleb Smirnoff static bool 4032c5deaf04SGleb Smirnoff uma_dbg_zskip(uma_zone_t zone, void *mem) 4033c5deaf04SGleb Smirnoff { 4034c5deaf04SGleb Smirnoff 4035bb15d1c7SGleb Smirnoff if (zone->uz_lockptr == &zone->uz_lock) 4036c5deaf04SGleb Smirnoff return (true); 4037c5deaf04SGleb Smirnoff 4038bb15d1c7SGleb Smirnoff return (uma_dbg_kskip(zone->uz_keg, mem)); 4039c5deaf04SGleb Smirnoff } 4040c5deaf04SGleb Smirnoff 4041c5deaf04SGleb Smirnoff static bool 4042c5deaf04SGleb Smirnoff uma_dbg_kskip(uma_keg_t keg, void *mem) 4043c5deaf04SGleb Smirnoff { 4044c5deaf04SGleb Smirnoff uintptr_t idx; 4045c5deaf04SGleb Smirnoff 4046c5deaf04SGleb Smirnoff if (dbg_divisor == 0) 4047c5deaf04SGleb Smirnoff return (true); 4048c5deaf04SGleb Smirnoff 4049c5deaf04SGleb Smirnoff if (dbg_divisor == 1) 4050c5deaf04SGleb Smirnoff return (false); 4051c5deaf04SGleb Smirnoff 4052c5deaf04SGleb Smirnoff idx = (uintptr_t)mem >> PAGE_SHIFT; 4053c5deaf04SGleb Smirnoff if (keg->uk_ipers > 1) { 4054c5deaf04SGleb Smirnoff idx *= keg->uk_ipers; 4055c5deaf04SGleb Smirnoff idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize; 4056c5deaf04SGleb Smirnoff } 4057c5deaf04SGleb Smirnoff 4058c5deaf04SGleb Smirnoff if ((idx / dbg_divisor) * dbg_divisor != idx) { 4059c5deaf04SGleb Smirnoff counter_u64_add(uma_skip_cnt, 1); 4060c5deaf04SGleb Smirnoff return (true); 4061c5deaf04SGleb Smirnoff } 4062c5deaf04SGleb Smirnoff counter_u64_add(uma_dbg_cnt, 1); 4063c5deaf04SGleb Smirnoff 4064c5deaf04SGleb Smirnoff return (false); 4065c5deaf04SGleb Smirnoff } 4066c5deaf04SGleb Smirnoff 40679542ea7bSGleb Smirnoff /* 40689542ea7bSGleb Smirnoff * Set up the slab's freei data such that uma_dbg_free can function. 40699542ea7bSGleb Smirnoff * 40709542ea7bSGleb Smirnoff */ 40719542ea7bSGleb Smirnoff static void 40729542ea7bSGleb Smirnoff uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 40739542ea7bSGleb Smirnoff { 40749542ea7bSGleb Smirnoff uma_keg_t keg; 40759542ea7bSGleb Smirnoff int freei; 40769542ea7bSGleb Smirnoff 40779542ea7bSGleb Smirnoff if (slab == NULL) { 40789542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 40799542ea7bSGleb Smirnoff if (slab == NULL) 40809542ea7bSGleb Smirnoff panic("uma: item %p did not belong to zone %s\n", 40819542ea7bSGleb Smirnoff item, zone->uz_name); 40829542ea7bSGleb Smirnoff } 40839542ea7bSGleb Smirnoff keg = slab->us_keg; 40849542ea7bSGleb Smirnoff freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize; 40859542ea7bSGleb Smirnoff 40869542ea7bSGleb Smirnoff if (BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree)) 40879542ea7bSGleb Smirnoff panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n", 40889542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 40899542ea7bSGleb Smirnoff BIT_SET_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree); 40909542ea7bSGleb Smirnoff 40919542ea7bSGleb Smirnoff return; 40929542ea7bSGleb Smirnoff } 40939542ea7bSGleb Smirnoff 40949542ea7bSGleb Smirnoff /* 40959542ea7bSGleb Smirnoff * Verifies freed addresses. Checks for alignment, valid slab membership 40969542ea7bSGleb Smirnoff * and duplicate frees. 40979542ea7bSGleb Smirnoff * 40989542ea7bSGleb Smirnoff */ 40999542ea7bSGleb Smirnoff static void 41009542ea7bSGleb Smirnoff uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 41019542ea7bSGleb Smirnoff { 41029542ea7bSGleb Smirnoff uma_keg_t keg; 41039542ea7bSGleb Smirnoff int freei; 41049542ea7bSGleb Smirnoff 41059542ea7bSGleb Smirnoff if (slab == NULL) { 41069542ea7bSGleb Smirnoff slab = uma_dbg_getslab(zone, item); 41079542ea7bSGleb Smirnoff if (slab == NULL) 41089542ea7bSGleb Smirnoff panic("uma: Freed item %p did not belong to zone %s\n", 41099542ea7bSGleb Smirnoff item, zone->uz_name); 41109542ea7bSGleb Smirnoff } 41119542ea7bSGleb Smirnoff keg = slab->us_keg; 41129542ea7bSGleb Smirnoff freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize; 41139542ea7bSGleb Smirnoff 41149542ea7bSGleb Smirnoff if (freei >= keg->uk_ipers) 41159542ea7bSGleb Smirnoff panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n", 41169542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 41179542ea7bSGleb Smirnoff 41189542ea7bSGleb Smirnoff if (((freei * keg->uk_rsize) + slab->us_data) != item) 41199542ea7bSGleb Smirnoff panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n", 41209542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 41219542ea7bSGleb Smirnoff 41229542ea7bSGleb Smirnoff if (!BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree)) 41239542ea7bSGleb Smirnoff panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n", 41249542ea7bSGleb Smirnoff item, zone, zone->uz_name, slab, freei); 41259542ea7bSGleb Smirnoff 41269542ea7bSGleb Smirnoff BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree); 41279542ea7bSGleb Smirnoff } 41289542ea7bSGleb Smirnoff #endif /* INVARIANTS */ 41299542ea7bSGleb Smirnoff 413048c5777eSRobert Watson #ifdef DDB 413148c5777eSRobert Watson DB_SHOW_COMMAND(uma, db_show_uma) 413248c5777eSRobert Watson { 413348c5777eSRobert Watson uma_keg_t kz; 413448c5777eSRobert Watson uma_zone_t z; 4135ab3185d1SJeff Roberson uint64_t allocs, frees, sleeps; 41360f9b7bf3SMark Johnston long cachefree; 41370f9b7bf3SMark Johnston int i; 413848c5777eSRobert Watson 413903175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s %8s\n", "Zone", "Size", "Used", 414003175483SAlexander Motin "Free", "Requests", "Sleeps", "Bucket"); 414148c5777eSRobert Watson LIST_FOREACH(kz, &uma_kegs, uk_link) { 414248c5777eSRobert Watson LIST_FOREACH(z, &kz->uk_zones, uz_link) { 414348c5777eSRobert Watson if (kz->uk_flags & UMA_ZFLAG_INTERNAL) { 41442efcc8cbSGleb Smirnoff allocs = counter_u64_fetch(z->uz_allocs); 41452efcc8cbSGleb Smirnoff frees = counter_u64_fetch(z->uz_frees); 4146bf965959SSean Bruno sleeps = z->uz_sleeps; 414748c5777eSRobert Watson cachefree = 0; 414848c5777eSRobert Watson } else 414948c5777eSRobert Watson uma_zone_sumstat(z, &cachefree, &allocs, 4150bf965959SSean Bruno &frees, &sleeps); 4151e20a199fSJeff Roberson if (!((z->uz_flags & UMA_ZONE_SECONDARY) && 415248c5777eSRobert Watson (LIST_FIRST(&kz->uk_zones) != z))) 415348c5777eSRobert Watson cachefree += kz->uk_free; 41540f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 41550f9b7bf3SMark Johnston cachefree += z->uz_domain[i].uzd_nitems; 41560f9b7bf3SMark Johnston 41570f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8ju %8u\n", 415803175483SAlexander Motin z->uz_name, (uintmax_t)kz->uk_size, 4159ae4e9636SRobert Watson (intmax_t)(allocs - frees), cachefree, 416003175483SAlexander Motin (uintmax_t)allocs, sleeps, z->uz_count); 4161687c94aaSJohn Baldwin if (db_pager_quit) 4162687c94aaSJohn Baldwin return; 416348c5777eSRobert Watson } 416448c5777eSRobert Watson } 416548c5777eSRobert Watson } 416603175483SAlexander Motin 416703175483SAlexander Motin DB_SHOW_COMMAND(umacache, db_show_umacache) 416803175483SAlexander Motin { 416903175483SAlexander Motin uma_zone_t z; 4170ab3185d1SJeff Roberson uint64_t allocs, frees; 41710f9b7bf3SMark Johnston long cachefree; 41720f9b7bf3SMark Johnston int i; 417303175483SAlexander Motin 417403175483SAlexander Motin db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free", 417503175483SAlexander Motin "Requests", "Bucket"); 417603175483SAlexander Motin LIST_FOREACH(z, &uma_cachezones, uz_link) { 417703175483SAlexander Motin uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL); 41780f9b7bf3SMark Johnston for (i = 0; i < vm_ndomains; i++) 41790f9b7bf3SMark Johnston cachefree += z->uz_domain[i].uzd_nitems; 41800f9b7bf3SMark Johnston db_printf("%18s %8ju %8jd %8ld %12ju %8u\n", 418103175483SAlexander Motin z->uz_name, (uintmax_t)z->uz_size, 418203175483SAlexander Motin (intmax_t)(allocs - frees), cachefree, 418303175483SAlexander Motin (uintmax_t)allocs, z->uz_count); 418403175483SAlexander Motin if (db_pager_quit) 418503175483SAlexander Motin return; 418603175483SAlexander Motin } 418703175483SAlexander Motin } 41889542ea7bSGleb Smirnoff #endif /* DDB */ 4189