160727d8bSWarner Losh /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 4584061b4SJeff Roberson * Copyright (c) 2002-2019 Jeffrey Roberson <jeff@FreeBSD.org> 508ecce74SRobert Watson * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org> 608ecce74SRobert Watson * All rights reserved. 78355f576SJeff Roberson * 88355f576SJeff Roberson * Redistribution and use in source and binary forms, with or without 98355f576SJeff Roberson * modification, are permitted provided that the following conditions 108355f576SJeff Roberson * are met: 118355f576SJeff Roberson * 1. Redistributions of source code must retain the above copyright 128355f576SJeff Roberson * notice unmodified, this list of conditions, and the following 138355f576SJeff Roberson * disclaimer. 148355f576SJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 158355f576SJeff Roberson * notice, this list of conditions and the following disclaimer in the 168355f576SJeff Roberson * documentation and/or other materials provided with the distribution. 178355f576SJeff Roberson * 188355f576SJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 198355f576SJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 208355f576SJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 218355f576SJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 228355f576SJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 238355f576SJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 248355f576SJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 258355f576SJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 268355f576SJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 278355f576SJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 288355f576SJeff Roberson * 298355f576SJeff Roberson * $FreeBSD$ 308355f576SJeff Roberson * 318355f576SJeff Roberson */ 328355f576SJeff Roberson 3339669415SGleb Smirnoff #include <sys/counter.h> 34e04223bfSMark Johnston #include <sys/_bitset.h> 357571e249SMark Johnston #include <sys/_domainset.h> 36b28cc462SGleb Smirnoff #include <sys/_task.h> 37b28cc462SGleb Smirnoff 388355f576SJeff Roberson /* 398355f576SJeff Roberson * This file includes definitions, structures, prototypes, and inlines that 408355f576SJeff Roberson * should not be used outside of the actual implementation of UMA. 418355f576SJeff Roberson */ 428355f576SJeff Roberson 438355f576SJeff Roberson /* 44ab3185d1SJeff Roberson * The brief summary; Zones describe unique allocation types. Zones are 45ab3185d1SJeff Roberson * organized into per-CPU caches which are filled by buckets. Buckets are 46ab3185d1SJeff Roberson * organized according to memory domains. Buckets are filled from kegs which 47ab3185d1SJeff Roberson * are also organized according to memory domains. Kegs describe a unique 48ab3185d1SJeff Roberson * allocation type, backend memory provider, and layout. Kegs are associated 49ab3185d1SJeff Roberson * with one or more zones and zones reference one or more kegs. Kegs provide 50ab3185d1SJeff Roberson * slabs which are virtually contiguous collections of pages. Each slab is 51ab3185d1SJeff Roberson * broken down int one or more items that will satisfy an individual allocation. 52ab3185d1SJeff Roberson * 53ab3185d1SJeff Roberson * Allocation is satisfied in the following order: 54ab3185d1SJeff Roberson * 1) Per-CPU cache 55ab3185d1SJeff Roberson * 2) Per-domain cache of buckets 56ab3185d1SJeff Roberson * 3) Slab from any of N kegs 57ab3185d1SJeff Roberson * 4) Backend page provider 58ab3185d1SJeff Roberson * 59ab3185d1SJeff Roberson * More detail on individual objects is contained below: 608355f576SJeff Roberson * 61099a0e58SBosko Milekic * Kegs contain lists of slabs which are stored in either the full bin, empty 628355f576SJeff Roberson * bin, or partially allocated bin, to reduce fragmentation. They also contain 638355f576SJeff Roberson * the user supplied value for size, which is adjusted for alignment purposes 64099a0e58SBosko Milekic * and rsize is the result of that. The Keg also stores information for 658355f576SJeff Roberson * managing a hash of page addresses that maps pages to uma_slab_t structures 668355f576SJeff Roberson * for pages that don't have embedded uma_slab_t's. 678355f576SJeff Roberson * 68ab3185d1SJeff Roberson * Keg slab lists are organized by memory domain to support NUMA allocation 69ab3185d1SJeff Roberson * policies. By default allocations are spread across domains to reduce the 70ab3185d1SJeff Roberson * potential for hotspots. Special keg creation flags may be specified to 71ab3185d1SJeff Roberson * prefer location allocation. However there is no strict enforcement as frees 72ab3185d1SJeff Roberson * may happen on any CPU and these are returned to the CPU-local cache 73ab3185d1SJeff Roberson * regardless of the originating domain. 74ab3185d1SJeff Roberson * 758355f576SJeff Roberson * The uma_slab_t may be embedded in a UMA_SLAB_SIZE chunk of memory or it may 768355f576SJeff Roberson * be allocated off the page from a special slab zone. The free list within a 77ef72505eSJeff Roberson * slab is managed with a bitmask. For item sizes that would yield more than 78ef72505eSJeff Roberson * 10% memory waste we potentially allocate a separate uma_slab_t if this will 79ef72505eSJeff Roberson * improve the number of items per slab that will fit. 808355f576SJeff Roberson * 818355f576SJeff Roberson * The only really gross cases, with regards to memory waste, are for those 828355f576SJeff Roberson * items that are just over half the page size. You can get nearly 50% waste, 838355f576SJeff Roberson * so you fall back to the memory footprint of the power of two allocator. I 848355f576SJeff Roberson * have looked at memory allocation sizes on many of the machines available to 858355f576SJeff Roberson * me, and there does not seem to be an abundance of allocations at this range 868355f576SJeff Roberson * so at this time it may not make sense to optimize for it. This can, of 878355f576SJeff Roberson * course, be solved with dynamic slab sizes. 888355f576SJeff Roberson * 89099a0e58SBosko Milekic * Kegs may serve multiple Zones but by far most of the time they only serve 90099a0e58SBosko Milekic * one. When a Zone is created, a Keg is allocated and setup for it. While 91099a0e58SBosko Milekic * the backing Keg stores slabs, the Zone caches Buckets of items allocated 92099a0e58SBosko Milekic * from the slabs. Each Zone is equipped with an init/fini and ctor/dtor 93099a0e58SBosko Milekic * pair, as well as with its own set of small per-CPU caches, layered above 94099a0e58SBosko Milekic * the Zone's general Bucket cache. 95099a0e58SBosko Milekic * 966ab3b958SRobert Watson * The PCPU caches are protected by critical sections, and may be accessed 976ab3b958SRobert Watson * safely only from their associated CPU, while the Zones backed by the same 986ab3b958SRobert Watson * Keg all share a common Keg lock (to coalesce contention on the backing 996ab3b958SRobert Watson * slabs). The backing Keg typically only serves one Zone but in the case of 1006ab3b958SRobert Watson * multiple Zones, one of the Zones is considered the Master Zone and all 1016ab3b958SRobert Watson * Zone-related stats from the Keg are done in the Master Zone. For an 1026ab3b958SRobert Watson * example of a Multi-Zone setup, refer to the Mbuf allocation code. 1038355f576SJeff Roberson */ 1048355f576SJeff Roberson 1058355f576SJeff Roberson /* 1068355f576SJeff Roberson * This is the representation for normal (Non OFFPAGE slab) 1078355f576SJeff Roberson * 1088355f576SJeff Roberson * i == item 1098355f576SJeff Roberson * s == slab pointer 1108355f576SJeff Roberson * 1118355f576SJeff Roberson * <---------------- Page (UMA_SLAB_SIZE) ------------------> 1128355f576SJeff Roberson * ___________________________________________________________ 1138355f576SJeff Roberson * | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ___________ | 1148355f576SJeff Roberson * ||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i| |slab header|| 1158355f576SJeff Roberson * ||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_| |___________|| 1168355f576SJeff Roberson * |___________________________________________________________| 1178355f576SJeff Roberson * 1188355f576SJeff Roberson * 1198355f576SJeff Roberson * This is an OFFPAGE slab. These can be larger than UMA_SLAB_SIZE. 1208355f576SJeff Roberson * 1218355f576SJeff Roberson * ___________________________________________________________ 1228355f576SJeff Roberson * | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | 1238355f576SJeff Roberson * ||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i||i| | 1248355f576SJeff Roberson * ||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_||_| | 1258355f576SJeff Roberson * |___________________________________________________________| 1268355f576SJeff Roberson * ___________ ^ 1278355f576SJeff Roberson * |slab header| | 1288355f576SJeff Roberson * |___________|---* 1298355f576SJeff Roberson * 1308355f576SJeff Roberson */ 1318355f576SJeff Roberson 1328355f576SJeff Roberson #ifndef VM_UMA_INT_H 1338355f576SJeff Roberson #define VM_UMA_INT_H 1348355f576SJeff Roberson 1358355f576SJeff Roberson #define UMA_SLAB_SIZE PAGE_SIZE /* How big are our slabs? */ 1368355f576SJeff Roberson #define UMA_SLAB_MASK (PAGE_SIZE - 1) /* Mask to get back to the page */ 1378355f576SJeff Roberson #define UMA_SLAB_SHIFT PAGE_SHIFT /* Number of bits PAGE_MASK */ 1388355f576SJeff Roberson 139ad97af7eSGleb Smirnoff /* Max waste percentage before going to off page slab management */ 140ad97af7eSGleb Smirnoff #define UMA_MAX_WASTE 10 1418355f576SJeff Roberson 1424a8b575cSRyan Libby /* Max size of a CACHESPREAD slab. */ 1434a8b575cSRyan Libby #define UMA_CACHESPREAD_MAX_SIZE (128 * 1024) 1444a8b575cSRyan Libby 14554c5ae80SRyan Libby /* 14654c5ae80SRyan Libby * These flags must not overlap with the UMA_ZONE flags specified in uma.h. 14754c5ae80SRyan Libby */ 14854c5ae80SRyan Libby #define UMA_ZFLAG_OFFPAGE 0x00200000 /* 14954c5ae80SRyan Libby * Force the slab structure 15054c5ae80SRyan Libby * allocation off of the real 15154c5ae80SRyan Libby * memory. 15254c5ae80SRyan Libby */ 15354c5ae80SRyan Libby #define UMA_ZFLAG_HASH 0x00400000 /* 15454c5ae80SRyan Libby * Use a hash table instead of 15554c5ae80SRyan Libby * caching information in the 15654c5ae80SRyan Libby * vm_page. 15754c5ae80SRyan Libby */ 15854c5ae80SRyan Libby #define UMA_ZFLAG_VTOSLAB 0x00800000 /* 15954c5ae80SRyan Libby * Zone uses vtoslab for 16054c5ae80SRyan Libby * lookup. 16154c5ae80SRyan Libby */ 16254c5ae80SRyan Libby #define UMA_ZFLAG_CTORDTOR 0x01000000 /* Zone has ctor/dtor set. */ 16354c5ae80SRyan Libby #define UMA_ZFLAG_LIMIT 0x02000000 /* Zone has limit set. */ 16454c5ae80SRyan Libby #define UMA_ZFLAG_CACHE 0x04000000 /* uma_zcache_create()d it */ 16554c5ae80SRyan Libby #define UMA_ZFLAG_RECLAIMING 0x08000000 /* Running zone_reclaim(). */ 16654c5ae80SRyan Libby #define UMA_ZFLAG_BUCKET 0x10000000 /* Bucket zone. */ 16754c5ae80SRyan Libby #define UMA_ZFLAG_INTERNAL 0x20000000 /* No offpage no PCPU. */ 16854c5ae80SRyan Libby #define UMA_ZFLAG_TRASH 0x40000000 /* Add trash ctor/dtor. */ 16954c5ae80SRyan Libby #define UMA_ZFLAG_CACHEONLY 0x80000000 /* Don't ask VM for buckets. */ 17054c5ae80SRyan Libby 17154c5ae80SRyan Libby #define UMA_ZFLAG_INHERIT \ 17254c5ae80SRyan Libby (UMA_ZFLAG_OFFPAGE | UMA_ZFLAG_HASH | UMA_ZFLAG_VTOSLAB | \ 17354c5ae80SRyan Libby UMA_ZFLAG_BUCKET | UMA_ZFLAG_INTERNAL | UMA_ZFLAG_CACHEONLY) 17454c5ae80SRyan Libby 17554c5ae80SRyan Libby #define PRINT_UMA_ZFLAGS "\20" \ 17654c5ae80SRyan Libby "\40CACHEONLY" \ 17754c5ae80SRyan Libby "\37TRASH" \ 17854c5ae80SRyan Libby "\36INTERNAL" \ 17954c5ae80SRyan Libby "\35BUCKET" \ 18054c5ae80SRyan Libby "\34RECLAIMING" \ 18154c5ae80SRyan Libby "\33CACHE" \ 18254c5ae80SRyan Libby "\32LIMIT" \ 18354c5ae80SRyan Libby "\31CTORDTOR" \ 18454c5ae80SRyan Libby "\30VTOSLAB" \ 18554c5ae80SRyan Libby "\27HASH" \ 18654c5ae80SRyan Libby "\26OFFPAGE" \ 187*d4665eaaSJeff Roberson "\23SMR" \ 18854c5ae80SRyan Libby "\22ROUNDROBIN" \ 18954c5ae80SRyan Libby "\21FIRSTTOUCH" \ 19054c5ae80SRyan Libby "\20PCPU" \ 19154c5ae80SRyan Libby "\17NODUMP" \ 19254c5ae80SRyan Libby "\16CACHESPREAD" \ 19354c5ae80SRyan Libby "\15MINBUCKET" \ 19454c5ae80SRyan Libby "\14MAXBUCKET" \ 19554c5ae80SRyan Libby "\13NOBUCKET" \ 19654c5ae80SRyan Libby "\12SECONDARY" \ 19754c5ae80SRyan Libby "\11NOTPAGE" \ 19854c5ae80SRyan Libby "\10VM" \ 19954c5ae80SRyan Libby "\7MTXCLASS" \ 20054c5ae80SRyan Libby "\6NOFREE" \ 20154c5ae80SRyan Libby "\5MALLOC" \ 20254c5ae80SRyan Libby "\4NOTOUCH" \ 20354c5ae80SRyan Libby "\2ZINIT" 2048355f576SJeff Roberson 2058355f576SJeff Roberson /* 2061e0701e1SJeff Roberson * Hash table for freed address -> slab translation. 2071e0701e1SJeff Roberson * 2081e0701e1SJeff Roberson * Only zones with memory not touchable by the allocator use the 2091e0701e1SJeff Roberson * hash table. Otherwise slabs are found with vtoslab(). 2108355f576SJeff Roberson */ 2111e0701e1SJeff Roberson #define UMA_HASH_SIZE_INIT 32 2128355f576SJeff Roberson 213ef72505eSJeff Roberson #define UMA_HASH(h, s) ((((uintptr_t)s) >> UMA_SLAB_SHIFT) & (h)->uh_hashmask) 2148355f576SJeff Roberson 2158355f576SJeff Roberson #define UMA_HASH_INSERT(h, s, mem) \ 2161e0701e1SJeff Roberson LIST_INSERT_HEAD(&(h)->uh_slab_hash[UMA_HASH((h), \ 2179b8db4d0SRyan Libby (mem))], slab_tohashslab(s), uhs_hlink) 2188355f576SJeff Roberson 2191e0701e1SJeff Roberson #define UMA_HASH_REMOVE(h, s) \ 2209b8db4d0SRyan Libby LIST_REMOVE(slab_tohashslab(s), uhs_hlink) 2218355f576SJeff Roberson 2221e0701e1SJeff Roberson LIST_HEAD(slabhashhead, uma_hash_slab); 2238355f576SJeff Roberson 2248355f576SJeff Roberson struct uma_hash { 2251e0701e1SJeff Roberson struct slabhashhead *uh_slab_hash; /* Hash table for slabs */ 2266929b7d1SPedro F. Giffuni u_int uh_hashsize; /* Current size of the hash table */ 2276929b7d1SPedro F. Giffuni u_int uh_hashmask; /* Mask used during hashing */ 2288355f576SJeff Roberson }; 2298355f576SJeff Roberson 2308355f576SJeff Roberson /* 23179c9f942SJeff Roberson * Align field or structure to cache 'sector' in intel terminology. This 23279c9f942SJeff Roberson * is more efficient with adjacent line prefetch. 2335e4bb93cSKip Macy */ 23412f69195SJustin Hibbits #if defined(__amd64__) || defined(__powerpc64__) 23579c9f942SJeff Roberson #define UMA_SUPER_ALIGN (CACHE_LINE_SIZE * 2) 2361a23373cSKip Macy #else 23779c9f942SJeff Roberson #define UMA_SUPER_ALIGN CACHE_LINE_SIZE 2381a23373cSKip Macy #endif 2395e4bb93cSKip Macy 24079c9f942SJeff Roberson #define UMA_ALIGN __aligned(UMA_SUPER_ALIGN) 24179c9f942SJeff Roberson 2425e4bb93cSKip Macy /* 243376b1ba3SJeff Roberson * The uma_bucket structure is used to queue and manage buckets divorced 244376b1ba3SJeff Roberson * from per-cpu caches. They are loaded into uma_cache_bucket structures 245376b1ba3SJeff Roberson * for use. 2468355f576SJeff Roberson */ 2478355f576SJeff Roberson struct uma_bucket { 24808cfa56eSMark Johnston TAILQ_ENTRY(uma_bucket) ub_link; /* Link into the zone */ 249306abf0fSGleb Smirnoff int16_t ub_cnt; /* Count of items in bucket. */ 250cae33c14SJeff Roberson int16_t ub_entries; /* Max items. */ 251*d4665eaaSJeff Roberson smr_seq_t ub_seq; /* SMR sequence number. */ 252cae33c14SJeff Roberson void *ub_bucket[]; /* actual allocation storage */ 2531a23373cSKip Macy }; 2548355f576SJeff Roberson 2558355f576SJeff Roberson typedef struct uma_bucket * uma_bucket_t; 2568355f576SJeff Roberson 257376b1ba3SJeff Roberson /* 258376b1ba3SJeff Roberson * The uma_cache_bucket structure is statically allocated on each per-cpu 259376b1ba3SJeff Roberson * cache. Its use reduces branches and cache misses in the fast path. 260376b1ba3SJeff Roberson */ 261376b1ba3SJeff Roberson struct uma_cache_bucket { 262376b1ba3SJeff Roberson uma_bucket_t ucb_bucket; 263376b1ba3SJeff Roberson int16_t ucb_cnt; 264376b1ba3SJeff Roberson int16_t ucb_entries; 265376b1ba3SJeff Roberson uint32_t ucb_spare; 266376b1ba3SJeff Roberson }; 267376b1ba3SJeff Roberson 268376b1ba3SJeff Roberson typedef struct uma_cache_bucket * uma_cache_bucket_t; 269376b1ba3SJeff Roberson 270376b1ba3SJeff Roberson /* 271376b1ba3SJeff Roberson * The uma_cache structure is allocated for each cpu for every zone 272376b1ba3SJeff Roberson * type. This optimizes synchronization out of the allocator fast path. 273376b1ba3SJeff Roberson */ 2748355f576SJeff Roberson struct uma_cache { 275376b1ba3SJeff Roberson struct uma_cache_bucket uc_freebucket; /* Bucket we're freeing to */ 276376b1ba3SJeff Roberson struct uma_cache_bucket uc_allocbucket; /* Bucket to allocate from */ 277376b1ba3SJeff Roberson struct uma_cache_bucket uc_crossbucket; /* cross domain bucket */ 27885dcf349SGleb Smirnoff uint64_t uc_allocs; /* Count of allocations */ 27985dcf349SGleb Smirnoff uint64_t uc_frees; /* Count of frees */ 2805e4bb93cSKip Macy } UMA_ALIGN; 2818355f576SJeff Roberson 2828355f576SJeff Roberson typedef struct uma_cache * uma_cache_t; 2838355f576SJeff Roberson 2841e0701e1SJeff Roberson LIST_HEAD(slabhead, uma_slab); 2851e0701e1SJeff Roberson 2868355f576SJeff Roberson /* 287cc7ce83aSJeff Roberson * The cache structure pads perfectly into 64 bytes so we use spare 288cc7ce83aSJeff Roberson * bits from the embedded cache buckets to store information from the zone 289cc7ce83aSJeff Roberson * and keep all fast-path allocations accessing a single per-cpu line. 290cc7ce83aSJeff Roberson */ 291cc7ce83aSJeff Roberson static inline void 292cc7ce83aSJeff Roberson cache_set_uz_flags(uma_cache_t cache, uint32_t flags) 293cc7ce83aSJeff Roberson { 294cc7ce83aSJeff Roberson 295cc7ce83aSJeff Roberson cache->uc_freebucket.ucb_spare = flags; 296cc7ce83aSJeff Roberson } 297cc7ce83aSJeff Roberson 298cc7ce83aSJeff Roberson static inline void 299cc7ce83aSJeff Roberson cache_set_uz_size(uma_cache_t cache, uint32_t size) 300cc7ce83aSJeff Roberson { 301cc7ce83aSJeff Roberson 302cc7ce83aSJeff Roberson cache->uc_allocbucket.ucb_spare = size; 303cc7ce83aSJeff Roberson } 304cc7ce83aSJeff Roberson 305cc7ce83aSJeff Roberson static inline uint32_t 306cc7ce83aSJeff Roberson cache_uz_flags(uma_cache_t cache) 307cc7ce83aSJeff Roberson { 308cc7ce83aSJeff Roberson 309cc7ce83aSJeff Roberson return (cache->uc_freebucket.ucb_spare); 310cc7ce83aSJeff Roberson } 311cc7ce83aSJeff Roberson 312cc7ce83aSJeff Roberson static inline uint32_t 313cc7ce83aSJeff Roberson cache_uz_size(uma_cache_t cache) 314cc7ce83aSJeff Roberson { 315cc7ce83aSJeff Roberson 316cc7ce83aSJeff Roberson return (cache->uc_allocbucket.ucb_spare); 317cc7ce83aSJeff Roberson } 318cc7ce83aSJeff Roberson 319cc7ce83aSJeff Roberson /* 320376b1ba3SJeff Roberson * Per-domain slab lists. Embedded in the kegs. 321ab3185d1SJeff Roberson */ 322ab3185d1SJeff Roberson struct uma_domain { 3238b987a77SJeff Roberson struct mtx_padalign ud_lock; /* Lock for the domain lists. */ 3241e0701e1SJeff Roberson struct slabhead ud_part_slab; /* partially allocated slabs */ 3251e0701e1SJeff Roberson struct slabhead ud_free_slab; /* completely unallocated slabs */ 3261e0701e1SJeff Roberson struct slabhead ud_full_slab; /* fully allocated slabs */ 3278b987a77SJeff Roberson uint32_t ud_pages; /* Total page count */ 3288b987a77SJeff Roberson uint32_t ud_free; /* Count of items free in slabs */ 329727c6918SJeff Roberson } __aligned(CACHE_LINE_SIZE); 330ab3185d1SJeff Roberson 331ab3185d1SJeff Roberson typedef struct uma_domain * uma_domain_t; 332ab3185d1SJeff Roberson 333ab3185d1SJeff Roberson /* 334099a0e58SBosko Milekic * Keg management structure 335099a0e58SBosko Milekic * 336099a0e58SBosko Milekic * TODO: Optimize for cache line size 337099a0e58SBosko Milekic * 338099a0e58SBosko Milekic */ 339099a0e58SBosko Milekic struct uma_keg { 340099a0e58SBosko Milekic struct uma_hash uk_hash; 341099a0e58SBosko Milekic LIST_HEAD(,uma_zone) uk_zones; /* Keg's zones */ 342099a0e58SBosko Milekic 343194a979eSMark Johnston struct domainset_ref uk_dr; /* Domain selection policy. */ 34485dcf349SGleb Smirnoff uint32_t uk_align; /* Alignment mask */ 3456fd34d6fSJeff Roberson uint32_t uk_reserve; /* Number of reserved items. */ 34685dcf349SGleb Smirnoff uint32_t uk_size; /* Requested size of each item */ 34785dcf349SGleb Smirnoff uint32_t uk_rsize; /* Real size of each item */ 348099a0e58SBosko Milekic 349099a0e58SBosko Milekic uma_init uk_init; /* Keg's init routine */ 350099a0e58SBosko Milekic uma_fini uk_fini; /* Keg's fini routine */ 351099a0e58SBosko Milekic uma_alloc uk_allocf; /* Allocation function */ 352099a0e58SBosko Milekic uma_free uk_freef; /* Free routine */ 353099a0e58SBosko Milekic 354a4915c21SAttilio Rao u_long uk_offset; /* Next free offset from base KVA */ 355a4915c21SAttilio Rao vm_offset_t uk_kva; /* Zone base KVA */ 356099a0e58SBosko Milekic 3572d54d4bbSMark Johnston uint32_t uk_pgoff; /* Offset to uma_slab struct */ 35885dcf349SGleb Smirnoff uint16_t uk_ppera; /* pages per allocation from backend */ 35985dcf349SGleb Smirnoff uint16_t uk_ipers; /* Items per slab */ 36085dcf349SGleb Smirnoff uint32_t uk_flags; /* Internal flags */ 361ad97af7eSGleb Smirnoff 362ad97af7eSGleb Smirnoff /* Least used fields go to the last cache line. */ 363ad97af7eSGleb Smirnoff const char *uk_name; /* Name of creating zone. */ 364ad97af7eSGleb Smirnoff LIST_ENTRY(uma_keg) uk_link; /* List of all kegs */ 365ab3185d1SJeff Roberson 366ab3185d1SJeff Roberson /* Must be last, variable sized. */ 367ab3185d1SJeff Roberson struct uma_domain uk_domain[]; /* Keg's slab lists. */ 368099a0e58SBosko Milekic }; 369099a0e58SBosko Milekic typedef struct uma_keg * uma_keg_t; 370099a0e58SBosko Milekic 371815db204SRyan Libby #ifdef _KERNEL 372727c6918SJeff Roberson #define KEG_ASSERT_COLD(k) \ 37331c251a0SJeff Roberson KASSERT(uma_keg_get_allocs((k)) == 0, \ 3748b987a77SJeff Roberson ("keg %s initialization after use.", (k)->uk_name)) 375727c6918SJeff Roberson 376ef72505eSJeff Roberson /* 377ef72505eSJeff Roberson * Free bits per-slab. 378ef72505eSJeff Roberson */ 3799b78b1f4SJeff Roberson #define SLAB_MAX_SETSIZE (PAGE_SIZE / UMA_SMALLEST_UNIT) 3809b78b1f4SJeff Roberson #define SLAB_MIN_SETSIZE _BITSET_BITS 3819b78b1f4SJeff Roberson BITSET_DEFINE(noslabbits, 0); 382099a0e58SBosko Milekic 383ef72505eSJeff Roberson /* 384ef72505eSJeff Roberson * The slab structure manages a single contiguous allocation from backing 385ef72505eSJeff Roberson * store and subdivides it into individually allocatable items. 386ef72505eSJeff Roberson */ 387ef72505eSJeff Roberson struct uma_slab { 3886d6a03d7SJeff Roberson LIST_ENTRY(uma_slab) us_link; /* slabs in zone */ 38985dcf349SGleb Smirnoff uint16_t us_freecount; /* How many are free? */ 39085dcf349SGleb Smirnoff uint8_t us_flags; /* Page flags see uma.h */ 391ab3185d1SJeff Roberson uint8_t us_domain; /* Backing NUMA domain. */ 392815db204SRyan Libby struct noslabbits us_free; /* Free bitmask, flexible. */ 393099a0e58SBosko Milekic }; 394815db204SRyan Libby _Static_assert(sizeof(struct uma_slab) == offsetof(struct uma_slab, us_free), 395815db204SRyan Libby "us_free field must be last"); 396ab3185d1SJeff Roberson #if MAXMEMDOM >= 255 397ab3185d1SJeff Roberson #error "Slab domain type insufficient" 398ab3185d1SJeff Roberson #endif 399ab3185d1SJeff Roberson 400099a0e58SBosko Milekic typedef struct uma_slab * uma_slab_t; 401e20a199fSJeff Roberson 402815db204SRyan Libby /* 403815db204SRyan Libby * On INVARIANTS builds, the slab contains a second bitset of the same size, 404815db204SRyan Libby * "dbg_bits", which is laid out immediately after us_free. 405815db204SRyan Libby */ 406815db204SRyan Libby #ifdef INVARIANTS 407815db204SRyan Libby #define SLAB_BITSETS 2 408815db204SRyan Libby #else 409815db204SRyan Libby #define SLAB_BITSETS 1 410815db204SRyan Libby #endif 411815db204SRyan Libby 4129b78b1f4SJeff Roberson /* These three functions are for embedded (!OFFPAGE) use only. */ 4139b78b1f4SJeff Roberson size_t slab_sizeof(int nitems); 4149b78b1f4SJeff Roberson size_t slab_space(int nitems); 4159b78b1f4SJeff Roberson int slab_ipers(size_t size, int align); 4169b78b1f4SJeff Roberson 4171e0701e1SJeff Roberson /* 4181e0701e1SJeff Roberson * Slab structure with a full sized bitset and hash link for both 4191e0701e1SJeff Roberson * HASH and OFFPAGE zones. 4201e0701e1SJeff Roberson */ 4211e0701e1SJeff Roberson struct uma_hash_slab { 4221e0701e1SJeff Roberson LIST_ENTRY(uma_hash_slab) uhs_hlink; /* Link for hash table */ 4231e0701e1SJeff Roberson uint8_t *uhs_data; /* First item */ 4249b8db4d0SRyan Libby struct uma_slab uhs_slab; /* Must be last. */ 4251e0701e1SJeff Roberson }; 4261e0701e1SJeff Roberson 4271e0701e1SJeff Roberson typedef struct uma_hash_slab * uma_hash_slab_t; 4281e0701e1SJeff Roberson 4299b8db4d0SRyan Libby static inline uma_hash_slab_t 4309b8db4d0SRyan Libby slab_tohashslab(uma_slab_t slab) 4319b8db4d0SRyan Libby { 4329b8db4d0SRyan Libby 4339b8db4d0SRyan Libby return (__containerof(slab, struct uma_hash_slab, uhs_slab)); 4349b8db4d0SRyan Libby } 4359b8db4d0SRyan Libby 4361e0701e1SJeff Roberson static inline void * 4371e0701e1SJeff Roberson slab_data(uma_slab_t slab, uma_keg_t keg) 4381e0701e1SJeff Roberson { 4391e0701e1SJeff Roberson 44054c5ae80SRyan Libby if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) == 0) 4411e0701e1SJeff Roberson return ((void *)((uintptr_t)slab - keg->uk_pgoff)); 4421e0701e1SJeff Roberson else 4439b8db4d0SRyan Libby return (slab_tohashslab(slab)->uhs_data); 4441e0701e1SJeff Roberson } 4451e0701e1SJeff Roberson 4461e0701e1SJeff Roberson static inline void * 4471e0701e1SJeff Roberson slab_item(uma_slab_t slab, uma_keg_t keg, int index) 4481e0701e1SJeff Roberson { 4491e0701e1SJeff Roberson uintptr_t data; 4501e0701e1SJeff Roberson 4511e0701e1SJeff Roberson data = (uintptr_t)slab_data(slab, keg); 4521e0701e1SJeff Roberson return ((void *)(data + keg->uk_rsize * index)); 4531e0701e1SJeff Roberson } 4541e0701e1SJeff Roberson 4551e0701e1SJeff Roberson static inline int 4561e0701e1SJeff Roberson slab_item_index(uma_slab_t slab, uma_keg_t keg, void *item) 4571e0701e1SJeff Roberson { 4581e0701e1SJeff Roberson uintptr_t data; 4591e0701e1SJeff Roberson 4601e0701e1SJeff Roberson data = (uintptr_t)slab_data(slab, keg); 4611e0701e1SJeff Roberson return (((uintptr_t)item - data) / keg->uk_rsize); 4621e0701e1SJeff Roberson } 463815db204SRyan Libby #endif /* _KERNEL */ 4641e0701e1SJeff Roberson 46508cfa56eSMark Johnston TAILQ_HEAD(uma_bucketlist, uma_bucket); 46608cfa56eSMark Johnston 467ab3185d1SJeff Roberson struct uma_zone_domain { 46808cfa56eSMark Johnston struct uma_bucketlist uzd_buckets; /* full buckets */ 46991d947bfSJeff Roberson uma_bucket_t uzd_cross; /* Fills from cross buckets. */ 4700f9b7bf3SMark Johnston long uzd_nitems; /* total item count */ 4710f9b7bf3SMark Johnston long uzd_imax; /* maximum item count this period */ 4720f9b7bf3SMark Johnston long uzd_imin; /* minimum item count this period */ 4730f9b7bf3SMark Johnston long uzd_wss; /* working set size estimate */ 474727c6918SJeff Roberson } __aligned(CACHE_LINE_SIZE); 475ab3185d1SJeff Roberson 476ab3185d1SJeff Roberson typedef struct uma_zone_domain * uma_zone_domain_t; 477ab3185d1SJeff Roberson 478244f4554SBosko Milekic /* 4794bd61e19SJeff Roberson * Zone structure - per memory type. 4808355f576SJeff Roberson */ 4818355f576SJeff Roberson struct uma_zone { 48263b5557bSJeff Roberson /* Offset 0, used in alloc/free fast/medium fast path and const. */ 483727c6918SJeff Roberson uma_keg_t uz_keg; /* This zone's keg if !CACHE */ 484ab3185d1SJeff Roberson struct uma_zone_domain *uz_domain; /* per-domain buckets */ 48563b5557bSJeff Roberson uint32_t uz_flags; /* Flags inherited from kegs */ 48663b5557bSJeff Roberson uint32_t uz_size; /* Size inherited from kegs */ 4878355f576SJeff Roberson uma_ctor uz_ctor; /* Constructor for each allocation */ 4888355f576SJeff Roberson uma_dtor uz_dtor; /* Destructor */ 489*d4665eaaSJeff Roberson smr_t uz_smr; /* Safe memory reclaim context. */ 490bb15d1c7SGleb Smirnoff uint64_t uz_max_items; /* Maximum number of items to alloc */ 4914bd61e19SJeff Roberson uint32_t uz_sleepers; /* Threads sleeping on limit */ 49220a4e154SJeff Roberson uint16_t uz_bucket_size; /* Number of items in full bucket */ 49320a4e154SJeff Roberson uint16_t uz_bucket_size_max; /* Maximum number of bucket items */ 49463b5557bSJeff Roberson 49563b5557bSJeff Roberson /* Offset 64, used in bucket replenish. */ 4960095a784SJeff Roberson uma_import uz_import; /* Import new memory to cache. */ 4970095a784SJeff Roberson uma_release uz_release; /* Release memory from cache. */ 4980095a784SJeff Roberson void *uz_arg; /* Import/release argument. */ 499bb15d1c7SGleb Smirnoff uma_init uz_init; /* Initializer for each item */ 500bb15d1c7SGleb Smirnoff uma_fini uz_fini; /* Finalizer for each item. */ 5014bd61e19SJeff Roberson void *uz_spare1; 502bb15d1c7SGleb Smirnoff uint64_t uz_bkt_count; /* Items in bucket cache */ 503bb15d1c7SGleb Smirnoff uint64_t uz_bkt_max; /* Maximum bucket cache size */ 504099a0e58SBosko Milekic 50563b5557bSJeff Roberson /* Offset 128 Rare. */ 50663b5557bSJeff Roberson /* 50763b5557bSJeff Roberson * The lock is placed here to avoid adjacent line prefetcher 50863b5557bSJeff Roberson * in fast paths and to take up space near infrequently accessed 50963b5557bSJeff Roberson * members to reduce alignment overhead. 51063b5557bSJeff Roberson */ 51163b5557bSJeff Roberson struct mtx uz_lock; /* Lock for the zone */ 512bb15d1c7SGleb Smirnoff LIST_ENTRY(uma_zone) uz_link; /* List of all zones in keg */ 513bb15d1c7SGleb Smirnoff const char *uz_name; /* Text name of the zone */ 51463b5557bSJeff Roberson /* The next two fields are used to print a rate-limited warnings. */ 51563b5557bSJeff Roberson const char *uz_warning; /* Warning to print on failure */ 51663b5557bSJeff Roberson struct timeval uz_ratecheck; /* Warnings rate-limiting */ 51763b5557bSJeff Roberson struct task uz_maxaction; /* Task to run when at limit */ 51820a4e154SJeff Roberson uint16_t uz_bucket_size_min; /* Min number of items in bucket */ 5195e4bb93cSKip Macy 52091d947bfSJeff Roberson struct mtx_padalign uz_cross_lock; /* Cross domain free lock */ 52191d947bfSJeff Roberson 52220a4e154SJeff Roberson /* Offset 256+, stats and misc. */ 5232efcc8cbSGleb Smirnoff counter_u64_t uz_allocs; /* Total number of allocations */ 5242efcc8cbSGleb Smirnoff counter_u64_t uz_frees; /* Total number of frees */ 5252efcc8cbSGleb Smirnoff counter_u64_t uz_fails; /* Total number of alloc failures */ 52685dcf349SGleb Smirnoff uint64_t uz_sleeps; /* Total number of alloc sleeps */ 527c1685086SJeff Roberson uint64_t uz_xdomain; /* Total number of cross-domain frees */ 5284bd61e19SJeff Roberson volatile uint64_t uz_items; /* Total items count & sleepers */ 5294bd61e19SJeff Roberson 53020a4e154SJeff Roberson char *uz_ctlname; /* sysctl safe name string. */ 53120a4e154SJeff Roberson struct sysctl_oid *uz_oid; /* sysctl oid pointer. */ 53220a4e154SJeff Roberson int uz_namecnt; /* duplicate name count. */ 53354503a13SJonathan T. Looney 5348355f576SJeff Roberson /* 5358355f576SJeff Roberson * This HAS to be the last item because we adjust the zone size 5368355f576SJeff Roberson * based on NCPU and then allocate the space for the zones. 5378355f576SJeff Roberson */ 538ab3185d1SJeff Roberson struct uma_cache uz_cpu[]; /* Per cpu caches */ 539ab3185d1SJeff Roberson 540ab3185d1SJeff Roberson /* uz_domain follows here. */ 5418355f576SJeff Roberson }; 5428355f576SJeff Roberson 543b60f5b79SJeff Roberson /* 5444bd61e19SJeff Roberson * Macros for interpreting the uz_items field. 20 bits of sleeper count 5454bd61e19SJeff Roberson * and 44 bit of item count. 5464bd61e19SJeff Roberson */ 5474bd61e19SJeff Roberson #define UZ_ITEMS_SLEEPER_SHIFT 44LL 5484bd61e19SJeff Roberson #define UZ_ITEMS_SLEEPERS_MAX ((1 << (64 - UZ_ITEMS_SLEEPER_SHIFT)) - 1) 5494bd61e19SJeff Roberson #define UZ_ITEMS_COUNT_MASK ((1LL << UZ_ITEMS_SLEEPER_SHIFT) - 1) 5504bd61e19SJeff Roberson #define UZ_ITEMS_COUNT(x) ((x) & UZ_ITEMS_COUNT_MASK) 5514bd61e19SJeff Roberson #define UZ_ITEMS_SLEEPERS(x) ((x) >> UZ_ITEMS_SLEEPER_SHIFT) 5524bd61e19SJeff Roberson #define UZ_ITEMS_SLEEPER (1LL << UZ_ITEMS_SLEEPER_SHIFT) 5534bd61e19SJeff Roberson 554727c6918SJeff Roberson #define ZONE_ASSERT_COLD(z) \ 55531c251a0SJeff Roberson KASSERT(uma_zone_get_allocs((z)) == 0, \ 556727c6918SJeff Roberson ("zone %s initialization after use.", (z)->uz_name)) 557727c6918SJeff Roberson 5585e4bb93cSKip Macy #undef UMA_ALIGN 5595e4bb93cSKip Macy 560af17e9a9SRobert Watson #ifdef _KERNEL 5618355f576SJeff Roberson /* Internal prototypes */ 56285dcf349SGleb Smirnoff static __inline uma_slab_t hash_sfind(struct uma_hash *hash, uint8_t *data); 5638355f576SJeff Roberson 5648355f576SJeff Roberson /* Lock Macros */ 5658355f576SJeff Roberson 5668b987a77SJeff Roberson #define KEG_LOCKPTR(k, d) (struct mtx *)&(k)->uk_domain[(d)].ud_lock 5678b987a77SJeff Roberson #define KEG_LOCK_INIT(k, d, lc) \ 56828bc4419SJeff Roberson do { \ 56928bc4419SJeff Roberson if ((lc)) \ 5708b987a77SJeff Roberson mtx_init(KEG_LOCKPTR(k, d), (k)->uk_name, \ 571e20a199fSJeff Roberson (k)->uk_name, MTX_DEF | MTX_DUPOK); \ 57228bc4419SJeff Roberson else \ 5738b987a77SJeff Roberson mtx_init(KEG_LOCKPTR(k, d), (k)->uk_name, \ 57428bc4419SJeff Roberson "UMA zone", MTX_DEF | MTX_DUPOK); \ 57528bc4419SJeff Roberson } while (0) 57628bc4419SJeff Roberson 5778b987a77SJeff Roberson #define KEG_LOCK_FINI(k, d) mtx_destroy(KEG_LOCKPTR(k, d)) 5788b987a77SJeff Roberson #define KEG_LOCK(k, d) \ 5798b987a77SJeff Roberson ({ mtx_lock(KEG_LOCKPTR(k, d)); KEG_LOCKPTR(k, d); }) 5808b987a77SJeff Roberson #define KEG_UNLOCK(k, d) mtx_unlock(KEG_LOCKPTR(k, d)) 5818b987a77SJeff Roberson #define KEG_LOCK_ASSERT(k, d) mtx_assert(KEG_LOCKPTR(k, d), MA_OWNED) 582bb15d1c7SGleb Smirnoff 583bb15d1c7SGleb Smirnoff #define KEG_GET(zone, keg) do { \ 584bb15d1c7SGleb Smirnoff (keg) = (zone)->uz_keg; \ 585bb15d1c7SGleb Smirnoff KASSERT((void *)(keg) != (void *)&(zone)->uz_lock, \ 586bb15d1c7SGleb Smirnoff ("%s: Invalid zone %p type", __func__, (zone))); \ 587bb15d1c7SGleb Smirnoff } while (0) 588af526374SJeff Roberson 589af526374SJeff Roberson #define ZONE_LOCK_INIT(z, lc) \ 590af526374SJeff Roberson do { \ 591af526374SJeff Roberson if ((lc)) \ 592af526374SJeff Roberson mtx_init(&(z)->uz_lock, (z)->uz_name, \ 593af526374SJeff Roberson (z)->uz_name, MTX_DEF | MTX_DUPOK); \ 594af526374SJeff Roberson else \ 595af526374SJeff Roberson mtx_init(&(z)->uz_lock, (z)->uz_name, \ 596af526374SJeff Roberson "UMA zone", MTX_DEF | MTX_DUPOK); \ 597af526374SJeff Roberson } while (0) 598af526374SJeff Roberson 599727c6918SJeff Roberson #define ZONE_LOCK(z) mtx_lock(&(z)->uz_lock) 600727c6918SJeff Roberson #define ZONE_TRYLOCK(z) mtx_trylock(&(z)->uz_lock) 601727c6918SJeff Roberson #define ZONE_UNLOCK(z) mtx_unlock(&(z)->uz_lock) 602af526374SJeff Roberson #define ZONE_LOCK_FINI(z) mtx_destroy(&(z)->uz_lock) 603727c6918SJeff Roberson #define ZONE_LOCK_ASSERT(z) mtx_assert(&(z)->uz_lock, MA_OWNED) 6048355f576SJeff Roberson 60591d947bfSJeff Roberson #define ZONE_CROSS_LOCK_INIT(z) \ 60691d947bfSJeff Roberson mtx_init(&(z)->uz_cross_lock, "UMA Cross", NULL, MTX_DEF) 60791d947bfSJeff Roberson #define ZONE_CROSS_LOCK(z) mtx_lock(&(z)->uz_cross_lock) 60891d947bfSJeff Roberson #define ZONE_CROSS_UNLOCK(z) mtx_unlock(&(z)->uz_cross_lock) 60991d947bfSJeff Roberson #define ZONE_CROSS_LOCK_FINI(z) mtx_destroy(&(z)->uz_cross_lock) 61091d947bfSJeff Roberson 6118355f576SJeff Roberson /* 6128355f576SJeff Roberson * Find a slab within a hash table. This is used for OFFPAGE zones to lookup 6138355f576SJeff Roberson * the slab structure. 6148355f576SJeff Roberson * 6158355f576SJeff Roberson * Arguments: 6168355f576SJeff Roberson * hash The hash table to search. 6178355f576SJeff Roberson * data The base page of the item. 6188355f576SJeff Roberson * 6198355f576SJeff Roberson * Returns: 6208355f576SJeff Roberson * A pointer to a slab if successful, else NULL. 6218355f576SJeff Roberson */ 6228355f576SJeff Roberson static __inline uma_slab_t 62385dcf349SGleb Smirnoff hash_sfind(struct uma_hash *hash, uint8_t *data) 6248355f576SJeff Roberson { 6251e0701e1SJeff Roberson uma_hash_slab_t slab; 6266929b7d1SPedro F. Giffuni u_int hval; 6278355f576SJeff Roberson 6288355f576SJeff Roberson hval = UMA_HASH(hash, data); 6298355f576SJeff Roberson 6301e0701e1SJeff Roberson LIST_FOREACH(slab, &hash->uh_slab_hash[hval], uhs_hlink) { 6311e0701e1SJeff Roberson if ((uint8_t *)slab->uhs_data == data) 6321e0701e1SJeff Roberson return (&slab->uhs_slab); 6338355f576SJeff Roberson } 6348355f576SJeff Roberson return (NULL); 6358355f576SJeff Roberson } 6368355f576SJeff Roberson 63799571dc3SJeff Roberson static __inline uma_slab_t 63899571dc3SJeff Roberson vtoslab(vm_offset_t va) 63999571dc3SJeff Roberson { 64099571dc3SJeff Roberson vm_page_t p; 64199571dc3SJeff Roberson 64299571dc3SJeff Roberson p = PHYS_TO_VM_PAGE(pmap_kextract(va)); 643584061b4SJeff Roberson return (p->plinks.uma.slab); 64499571dc3SJeff Roberson } 64599571dc3SJeff Roberson 64699571dc3SJeff Roberson static __inline void 647584061b4SJeff Roberson vtozoneslab(vm_offset_t va, uma_zone_t *zone, uma_slab_t *slab) 64899571dc3SJeff Roberson { 64999571dc3SJeff Roberson vm_page_t p; 65099571dc3SJeff Roberson 6516fc96493SOlivier Houchard p = PHYS_TO_VM_PAGE(pmap_kextract(va)); 652584061b4SJeff Roberson *slab = p->plinks.uma.slab; 653584061b4SJeff Roberson *zone = p->plinks.uma.zone; 654584061b4SJeff Roberson } 655584061b4SJeff Roberson 656584061b4SJeff Roberson static __inline void 657584061b4SJeff Roberson vsetzoneslab(vm_offset_t va, uma_zone_t zone, uma_slab_t slab) 658584061b4SJeff Roberson { 659584061b4SJeff Roberson vm_page_t p; 660584061b4SJeff Roberson 661584061b4SJeff Roberson p = PHYS_TO_VM_PAGE(pmap_kextract(va)); 662584061b4SJeff Roberson p->plinks.uma.slab = slab; 663584061b4SJeff Roberson p->plinks.uma.zone = zone; 66499571dc3SJeff Roberson } 66599571dc3SJeff Roberson 6666d6a03d7SJeff Roberson extern unsigned long uma_kmem_limit; 6676d6a03d7SJeff Roberson extern unsigned long uma_kmem_total; 6686d6a03d7SJeff Roberson 6696d6a03d7SJeff Roberson /* Adjust bytes under management by UMA. */ 6706d6a03d7SJeff Roberson static inline void 6716d6a03d7SJeff Roberson uma_total_dec(unsigned long size) 6726d6a03d7SJeff Roberson { 6736d6a03d7SJeff Roberson 6746d6a03d7SJeff Roberson atomic_subtract_long(&uma_kmem_total, size); 6756d6a03d7SJeff Roberson } 6766d6a03d7SJeff Roberson 6776d6a03d7SJeff Roberson static inline void 6786d6a03d7SJeff Roberson uma_total_inc(unsigned long size) 6796d6a03d7SJeff Roberson { 6806d6a03d7SJeff Roberson 6816d6a03d7SJeff Roberson if (atomic_fetchadd_long(&uma_kmem_total, size) > uma_kmem_limit) 6826d6a03d7SJeff Roberson uma_reclaim_wakeup(); 6836d6a03d7SJeff Roberson } 6846d6a03d7SJeff Roberson 68548eea375SJeff Roberson /* 68648eea375SJeff Roberson * The following two functions may be defined by architecture specific code 687763df3ecSPedro F. Giffuni * if they can provide more efficient allocation functions. This is useful 68848eea375SJeff Roberson * for using direct mapped addresses. 68948eea375SJeff Roberson */ 690ab3185d1SJeff Roberson void *uma_small_alloc(uma_zone_t zone, vm_size_t bytes, int domain, 691ab3185d1SJeff Roberson uint8_t *pflag, int wait); 692f2c2231eSRyan Stone void uma_small_free(void *mem, vm_size_t size, uint8_t flags); 6932e47807cSJeff Roberson 6942e47807cSJeff Roberson /* Set a global soft limit on UMA managed memory. */ 6952e47807cSJeff Roberson void uma_set_limit(unsigned long limit); 696af17e9a9SRobert Watson #endif /* _KERNEL */ 69748eea375SJeff Roberson 6988355f576SJeff Roberson #endif /* VM_UMA_INT_H */ 699