2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2019 Jeffrey Roberson <jeff@FreeBSD.org>
5 * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
6 * Copyright (c) 2004-2006 Robert N. M. Watson
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * uma_core.c Implementation of the Universal Memory allocator
34 * This allocator is intended to replace the multitude of similar object caches
35 * in the standard FreeBSD kernel. The intent is to be flexible as well as
36 * efficient. A primary design goal is to return unused memory to the rest of
37 * the system. This will make the system as a whole more flexible due to the
38 * ability to move memory to subsystems which most need it instead of leaving
39 * pools of reserved memory unused.
41 * The basic ideas stem from similar slab/zone based allocators whose algorithms
48 * - Improve memory usage for large allocations
49 * - Investigate cache size adjustments
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
56 #include "opt_param.h"
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/bitset.h>
62 #include <sys/domainset.h>
63 #include <sys/eventhandler.h>
64 #include <sys/kernel.h>
65 #include <sys/types.h>
66 #include <sys/limits.h>
67 #include <sys/queue.h>
68 #include <sys/malloc.h>
71 #include <sys/sysctl.h>
72 #include <sys/mutex.h>
74 #include <sys/random.h>
75 #include <sys/rwlock.h>
77 #include <sys/sched.h>
78 #include <sys/sleepqueue.h>
81 #include <sys/taskqueue.h>
82 #include <sys/vmmeter.h>
85 #include <vm/vm_domainset.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pageout.h>
89 #include <vm/vm_param.h>
90 #include <vm/vm_phys.h>
91 #include <vm/vm_pagequeue.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_kern.h>
94 #include <vm/vm_extern.h>
96 #include <vm/uma_int.h>
97 #include <vm/uma_dbg.h>
101 #ifdef DEBUG_MEMGUARD
102 #include <vm/memguard.h>
105 #include <machine/md_var.h>
108 #define UMA_ALWAYS_CTORDTOR 1
110 #define UMA_ALWAYS_CTORDTOR 0
114 * This is the zone and keg from which all zones are spawned.
116 static uma_zone_t kegs;
117 static uma_zone_t zones;
120 * These are the two zones from which all offpage uma_slab_ts are allocated.
122 * One zone is for slab headers that can represent a larger number of items,
123 * making the slabs themselves more efficient, and the other zone is for
124 * headers that are smaller and represent fewer items, making the headers more
127 #define SLABZONE_SIZE(setsize) \
128 (sizeof(struct uma_hash_slab) + BITSET_SIZE(setsize) * SLAB_BITSETS)
129 #define SLABZONE0_SETSIZE (PAGE_SIZE / 16)
130 #define SLABZONE1_SETSIZE SLAB_MAX_SETSIZE
131 #define SLABZONE0_SIZE SLABZONE_SIZE(SLABZONE0_SETSIZE)
132 #define SLABZONE1_SIZE SLABZONE_SIZE(SLABZONE1_SETSIZE)
133 static uma_zone_t slabzones[2];
136 * The initial hash tables come out of this zone so they can be allocated
137 * prior to malloc coming up.
139 static uma_zone_t hashzone;
141 /* The boot-time adjusted value for cache line alignment. */
142 int uma_align_cache = 64 - 1;
144 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
145 static MALLOC_DEFINE(M_UMA, "UMA", "UMA Misc");
148 * Are we allowed to allocate buckets?
150 static int bucketdisable = 1;
152 /* Linked list of all kegs in the system */
153 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
155 /* Linked list of all cache-only zones in the system */
156 static LIST_HEAD(,uma_zone) uma_cachezones =
157 LIST_HEAD_INITIALIZER(uma_cachezones);
159 /* This RW lock protects the keg list */
160 static struct rwlock_padalign __exclusive_cache_line uma_rwlock;
163 * First available virual address for boot time allocations.
165 static vm_offset_t bootstart;
166 static vm_offset_t bootmem;
168 static struct sx uma_reclaim_lock;
171 * kmem soft limit, initialized by uma_set_limit(). Ensure that early
172 * allocations don't trigger a wakeup of the reclaim thread.
174 unsigned long uma_kmem_limit = LONG_MAX;
175 SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_limit, CTLFLAG_RD, &uma_kmem_limit, 0,
176 "UMA kernel memory soft limit");
177 unsigned long uma_kmem_total;
178 SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_total, CTLFLAG_RD, &uma_kmem_total, 0,
179 "UMA kernel memory usage");
181 /* Is the VM done starting up? */
187 } booted = BOOT_COLD;
190 * This is the handle used to schedule events that need to happen
191 * outside of the allocation fast path.
193 static struct callout uma_callout;
194 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
197 * This structure is passed as the zone ctor arg so that I don't have to create
198 * a special allocation function just for zones.
200 struct uma_zctor_args {
215 struct uma_kctor_args {
224 struct uma_bucket_zone {
226 const char *ubz_name;
227 int ubz_entries; /* Number of items it can hold. */
228 int ubz_maxsize; /* Maximum allocation size per-item. */
232 * Compute the actual number of bucket entries to pack them in power
233 * of two sizes for more efficient space utilization.
235 #define BUCKET_SIZE(n) \
236 (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *))
238 #define BUCKET_MAX BUCKET_SIZE(256)
241 struct uma_bucket_zone bucket_zones[] = {
242 /* Literal bucket sizes. */
243 { NULL, "2 Bucket", 2, 4096 },
244 { NULL, "4 Bucket", 4, 3072 },
245 { NULL, "8 Bucket", 8, 2048 },
246 { NULL, "16 Bucket", 16, 1024 },
247 /* Rounded down power of 2 sizes for efficiency. */
248 { NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
249 { NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
250 { NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
251 { NULL, "256 Bucket", BUCKET_SIZE(256), 64 },
256 * Flags and enumerations to be passed to internal functions.
260 SKIP_CNT = 0x00000001,
261 SKIP_DTOR = 0x00010000,
262 SKIP_FINI = 0x00020000,
267 void uma_startup1(vm_offset_t);
268 void uma_startup2(void);
270 static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
271 static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
272 static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
273 static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
274 static void *contig_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
275 static void page_free(void *, vm_size_t, uint8_t);
276 static void pcpu_page_free(void *, vm_size_t, uint8_t);
277 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int);
278 static void cache_drain(uma_zone_t);
279 static void bucket_drain(uma_zone_t, uma_bucket_t);
280 static void bucket_cache_reclaim(uma_zone_t zone, bool);
281 static int keg_ctor(void *, int, void *, int);
282 static void keg_dtor(void *, int, void *);
283 static int zone_ctor(void *, int, void *, int);
284 static void zone_dtor(void *, int, void *);
285 static inline void item_dtor(uma_zone_t zone, void *item, int size,
286 void *udata, enum zfreeskip skip);
287 static int zero_init(void *, int, int);
288 static void zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata,
289 int itemdomain, bool ws);
290 static void zone_foreach(void (*zfunc)(uma_zone_t, void *), void *);
291 static void zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *), void *);
292 static void zone_timeout(uma_zone_t zone, void *);
293 static int hash_alloc(struct uma_hash *, u_int);
294 static int hash_expand(struct uma_hash *, struct uma_hash *);
295 static void hash_free(struct uma_hash *hash);
296 static void uma_timeout(void *);
297 static void uma_startup3(void);
298 static void uma_shutdown(void);
299 static void *zone_alloc_item(uma_zone_t, void *, int, int);
300 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
301 static int zone_alloc_limit(uma_zone_t zone, int count, int flags);
302 static void zone_free_limit(uma_zone_t zone, int count);
303 static void bucket_enable(void);
304 static void bucket_init(void);
305 static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int);
306 static void bucket_free(uma_zone_t zone, uma_bucket_t, void *);
307 static void bucket_zone_drain(void);
308 static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int);
309 static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
310 static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item);
311 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
312 uma_fini fini, int align, uint32_t flags);
313 static int zone_import(void *, void **, int, int, int);
314 static void zone_release(void *, void **, int);
315 static bool cache_alloc(uma_zone_t, uma_cache_t, void *, int);
316 static bool cache_free(uma_zone_t, uma_cache_t, void *, void *, int);
318 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
319 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
320 static int sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS);
321 static int sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS);
322 static int sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS);
323 static int sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS);
324 static int sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS);
326 static uint64_t uma_zone_get_allocs(uma_zone_t zone);
328 static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0,
329 "Memory allocation debugging");
332 static uint64_t uma_keg_get_allocs(uma_keg_t zone);
333 static inline struct noslabbits *slab_dbg_bits(uma_slab_t slab, uma_keg_t keg);
335 static bool uma_dbg_kskip(uma_keg_t keg, void *mem);
336 static bool uma_dbg_zskip(uma_zone_t zone, void *mem);
337 static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item);
338 static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item);
340 static u_int dbg_divisor = 1;
341 SYSCTL_UINT(_vm_debug, OID_AUTO, divisor,
342 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0,
343 "Debug & thrash every this item in memory allocator");
345 static counter_u64_t uma_dbg_cnt = EARLY_COUNTER;
346 static counter_u64_t uma_skip_cnt = EARLY_COUNTER;
347 SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD,
348 &uma_dbg_cnt, "memory items debugged");
349 SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD,
350 &uma_skip_cnt, "memory items skipped, not debugged");
353 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
355 SYSCTL_NODE(_vm, OID_AUTO, uma, CTLFLAG_RW, 0, "Universal Memory Allocator");
357 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_INT,
358 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
360 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLTYPE_STRUCT,
361 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
363 static int zone_warnings = 1;
364 SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0,
365 "Warn when UMA zones becomes full");
367 static int multipage_slabs = 1;
368 TUNABLE_INT("vm.debug.uma_multipage_slabs", &multipage_slabs);
369 SYSCTL_INT(_vm_debug, OID_AUTO, uma_multipage_slabs,
370 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &multipage_slabs, 0,
371 "UMA may choose larger slab sizes for better efficiency");
374 * Select the slab zone for an offpage slab with the given maximum item count.
376 static inline uma_zone_t
380 return (slabzones[ipers > SLABZONE0_SETSIZE]);
384 * This routine checks to see whether or not it's safe to enable buckets.
390 KASSERT(booted >= BOOT_KVA, ("Bucket enable before init"));
391 bucketdisable = vm_page_count_min();
395 * Initialize bucket_zones, the array of zones of buckets of various sizes.
397 * For each zone, calculate the memory required for each bucket, consisting
398 * of the header and an array of pointers.
403 struct uma_bucket_zone *ubz;
406 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
407 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
408 size += sizeof(void *) * ubz->ubz_entries;
409 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
410 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
411 UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET |
412 UMA_ZONE_FIRSTTOUCH);
417 * Given a desired number of entries for a bucket, return the zone from which
418 * to allocate the bucket.
420 static struct uma_bucket_zone *
421 bucket_zone_lookup(int entries)
423 struct uma_bucket_zone *ubz;
425 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
426 if (ubz->ubz_entries >= entries)
432 static struct uma_bucket_zone *
433 bucket_zone_max(uma_zone_t zone, int nitems)
435 struct uma_bucket_zone *ubz;
439 if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
440 /* Count the cross-domain bucket. */
443 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
444 if (ubz->ubz_entries * bpcpu * mp_ncpus > nitems)
446 if (ubz == &bucket_zones[0])
454 bucket_select(int size)
456 struct uma_bucket_zone *ubz;
458 ubz = &bucket_zones[0];
459 if (size > ubz->ubz_maxsize)
460 return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
462 for (; ubz->ubz_entries != 0; ubz++)
463 if (ubz->ubz_maxsize < size)
466 return (ubz->ubz_entries);
470 bucket_alloc(uma_zone_t zone, void *udata, int flags)
472 struct uma_bucket_zone *ubz;
476 * Don't allocate buckets early in boot.
478 if (__predict_false(booted < BOOT_KVA))
482 * To limit bucket recursion we store the original zone flags
483 * in a cookie passed via zalloc_arg/zfree_arg. This allows the
484 * NOVM flag to persist even through deep recursions. We also
485 * store ZFLAG_BUCKET once we have recursed attempting to allocate
486 * a bucket for a bucket zone so we do not allow infinite bucket
487 * recursion. This cookie will even persist to frees of unused
488 * buckets via the allocation path or bucket allocations in the
491 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
492 udata = (void *)(uintptr_t)zone->uz_flags;
494 if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
496 udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
498 if (((uintptr_t)udata & UMA_ZONE_VM) != 0)
500 ubz = bucket_zone_lookup(zone->uz_bucket_size);
501 if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0)
503 bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
506 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
509 bucket->ub_entries = ubz->ubz_entries;
510 bucket->ub_seq = SMR_SEQ_INVALID;
511 CTR3(KTR_UMA, "bucket_alloc: zone %s(%p) allocated bucket %p",
512 zone->uz_name, zone, bucket);
519 bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
521 struct uma_bucket_zone *ubz;
523 if (bucket->ub_cnt != 0)
524 bucket_drain(zone, bucket);
526 KASSERT(bucket->ub_cnt == 0,
527 ("bucket_free: Freeing a non free bucket."));
528 KASSERT(bucket->ub_seq == SMR_SEQ_INVALID,
529 ("bucket_free: Freeing an SMR bucket."));
530 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
531 udata = (void *)(uintptr_t)zone->uz_flags;
532 ubz = bucket_zone_lookup(bucket->ub_entries);
533 uma_zfree_arg(ubz->ubz_zone, bucket, udata);
537 bucket_zone_drain(void)
539 struct uma_bucket_zone *ubz;
541 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
542 uma_zone_reclaim(ubz->ubz_zone, UMA_RECLAIM_DRAIN);
546 * Acquire the domain lock and record contention.
548 static uma_zone_domain_t
549 zone_domain_lock(uma_zone_t zone, int domain)
551 uma_zone_domain_t zdom;
554 zdom = ZDOM_GET(zone, domain);
556 if (ZDOM_OWNED(zdom))
559 /* This is unsynchronized. The counter does not need to be precise. */
560 if (lockfail && zone->uz_bucket_size < zone->uz_bucket_size_max)
561 zone->uz_bucket_size++;
566 * Search for the domain with the least cached items and return it, breaking
567 * ties with a preferred domain by returning it.
569 static __noinline int
570 zone_domain_lowest(uma_zone_t zone, int pref)
578 for (i = 0; i < vm_ndomains; i++) {
579 nitems = ZDOM_GET(zone, i)->uzd_nitems;
580 if (nitems < least) {
583 } else if (nitems == least && (i == pref || domain == pref))
591 * Search for the domain with the most cached items and return it or the
592 * preferred domain if it has enough to proceed.
594 static __noinline int
595 zone_domain_highest(uma_zone_t zone, int pref)
601 if (ZDOM_GET(zone, pref)->uzd_nitems > BUCKET_MAX)
606 for (i = 0; i < vm_ndomains; i++) {
607 nitems = ZDOM_GET(zone, i)->uzd_nitems;
618 * Safely subtract cnt from imax.
621 zone_domain_imax_sub(uma_zone_domain_t zdom, int cnt)
626 old = zdom->uzd_imax;
632 } while (atomic_fcmpset_long(&zdom->uzd_imax, &old, new) == 0);
636 * Set the maximum imax value.
639 zone_domain_imax_set(uma_zone_domain_t zdom, int nitems)
643 old = zdom->uzd_imax;
647 } while (atomic_fcmpset_long(&zdom->uzd_imax, &old, nitems) == 0);
651 * Attempt to satisfy an allocation by retrieving a full bucket from one of the
652 * zone's caches. If a bucket is found the zone is not locked on return.
655 zone_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom, bool reclaim)
661 ZDOM_LOCK_ASSERT(zdom);
663 if ((bucket = STAILQ_FIRST(&zdom->uzd_buckets)) == NULL)
666 /* SMR Buckets can not be re-used until readers expire. */
667 if ((zone->uz_flags & UMA_ZONE_SMR) != 0 &&
668 bucket->ub_seq != SMR_SEQ_INVALID) {
669 if (!smr_poll(zone->uz_smr, bucket->ub_seq, false))
671 bucket->ub_seq = SMR_SEQ_INVALID;
672 dtor = (zone->uz_dtor != NULL) || UMA_ALWAYS_CTORDTOR;
673 if (STAILQ_NEXT(bucket, ub_link) != NULL)
674 zdom->uzd_seq = STAILQ_NEXT(bucket, ub_link)->ub_seq;
676 MPASS(zdom->uzd_nitems >= bucket->ub_cnt);
677 STAILQ_REMOVE_HEAD(&zdom->uzd_buckets, ub_link);
678 zdom->uzd_nitems -= bucket->ub_cnt;
681 * Shift the bounds of the current WSS interval to avoid
682 * perturbing the estimate.
685 zdom->uzd_imin -= lmin(zdom->uzd_imin, bucket->ub_cnt);
686 zone_domain_imax_sub(zdom, bucket->ub_cnt);
687 } else if (zdom->uzd_imin > zdom->uzd_nitems)
688 zdom->uzd_imin = zdom->uzd_nitems;
692 for (i = 0; i < bucket->ub_cnt; i++)
693 item_dtor(zone, bucket->ub_bucket[i], zone->uz_size,
700 * Insert a full bucket into the specified cache. The "ws" parameter indicates
701 * whether the bucket's contents should be counted as part of the zone's working
702 * set. The bucket may be freed if it exceeds the bucket limit.
705 zone_put_bucket(uma_zone_t zone, int domain, uma_bucket_t bucket, void *udata,
708 uma_zone_domain_t zdom;
710 /* We don't cache empty buckets. This can happen after a reclaim. */
711 if (bucket->ub_cnt == 0)
713 zdom = zone_domain_lock(zone, domain);
715 KASSERT(!ws || zdom->uzd_nitems < zone->uz_bucket_max,
716 ("%s: zone %p overflow", __func__, zone));
719 * Conditionally set the maximum number of items.
721 zdom->uzd_nitems += bucket->ub_cnt;
722 if (__predict_true(zdom->uzd_nitems < zone->uz_bucket_max)) {
724 zone_domain_imax_set(zdom, zdom->uzd_nitems);
725 if (STAILQ_EMPTY(&zdom->uzd_buckets))
726 zdom->uzd_seq = bucket->ub_seq;
727 STAILQ_INSERT_TAIL(&zdom->uzd_buckets, bucket, ub_link);
731 zdom->uzd_nitems -= bucket->ub_cnt;
734 bucket_free(zone, bucket, udata);
737 /* Pops an item out of a per-cpu cache bucket. */
739 cache_bucket_pop(uma_cache_t cache, uma_cache_bucket_t bucket)
743 CRITICAL_ASSERT(curthread);
746 item = bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt];
748 bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = NULL;
749 KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
756 /* Pushes an item into a per-cpu cache bucket. */
758 cache_bucket_push(uma_cache_t cache, uma_cache_bucket_t bucket, void *item)
761 CRITICAL_ASSERT(curthread);
762 KASSERT(bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] == NULL,
763 ("uma_zfree: Freeing to non free bucket index."));
765 bucket->ucb_bucket->ub_bucket[bucket->ucb_cnt] = item;
771 * Unload a UMA bucket from a per-cpu cache.
773 static inline uma_bucket_t
774 cache_bucket_unload(uma_cache_bucket_t bucket)
778 b = bucket->ucb_bucket;
780 MPASS(b->ub_entries == bucket->ucb_entries);
781 b->ub_cnt = bucket->ucb_cnt;
782 bucket->ucb_bucket = NULL;
783 bucket->ucb_entries = bucket->ucb_cnt = 0;
789 static inline uma_bucket_t
790 cache_bucket_unload_alloc(uma_cache_t cache)
793 return (cache_bucket_unload(&cache->uc_allocbucket));
796 static inline uma_bucket_t
797 cache_bucket_unload_free(uma_cache_t cache)
800 return (cache_bucket_unload(&cache->uc_freebucket));
803 static inline uma_bucket_t
804 cache_bucket_unload_cross(uma_cache_t cache)
807 return (cache_bucket_unload(&cache->uc_crossbucket));
811 * Load a bucket into a per-cpu cache bucket.
814 cache_bucket_load(uma_cache_bucket_t bucket, uma_bucket_t b)
817 CRITICAL_ASSERT(curthread);
818 MPASS(bucket->ucb_bucket == NULL);
819 MPASS(b->ub_seq == SMR_SEQ_INVALID);
821 bucket->ucb_bucket = b;
822 bucket->ucb_cnt = b->ub_cnt;
823 bucket->ucb_entries = b->ub_entries;
827 cache_bucket_load_alloc(uma_cache_t cache, uma_bucket_t b)
830 cache_bucket_load(&cache->uc_allocbucket, b);
834 cache_bucket_load_free(uma_cache_t cache, uma_bucket_t b)
837 cache_bucket_load(&cache->uc_freebucket, b);
842 cache_bucket_load_cross(uma_cache_t cache, uma_bucket_t b)
845 cache_bucket_load(&cache->uc_crossbucket, b);
850 * Copy and preserve ucb_spare.
853 cache_bucket_copy(uma_cache_bucket_t b1, uma_cache_bucket_t b2)
856 b1->ucb_bucket = b2->ucb_bucket;
857 b1->ucb_entries = b2->ucb_entries;
858 b1->ucb_cnt = b2->ucb_cnt;
862 * Swap two cache buckets.
865 cache_bucket_swap(uma_cache_bucket_t b1, uma_cache_bucket_t b2)
867 struct uma_cache_bucket b3;
869 CRITICAL_ASSERT(curthread);
871 cache_bucket_copy(&b3, b1);
872 cache_bucket_copy(b1, b2);
873 cache_bucket_copy(b2, &b3);
877 * Attempt to fetch a bucket from a zone on behalf of the current cpu cache.
880 cache_fetch_bucket(uma_zone_t zone, uma_cache_t cache, int domain)
882 uma_zone_domain_t zdom;
886 * Avoid the lock if possible.
888 zdom = ZDOM_GET(zone, domain);
889 if (zdom->uzd_nitems == 0)
892 if ((cache_uz_flags(cache) & UMA_ZONE_SMR) != 0 &&
893 !smr_poll(zone->uz_smr, zdom->uzd_seq, false))
897 * Check the zone's cache of buckets.
899 zdom = zone_domain_lock(zone, domain);
900 if ((bucket = zone_fetch_bucket(zone, zdom, false)) != NULL) {
901 KASSERT(bucket->ub_cnt != 0,
902 ("cache_fetch_bucket: Returning an empty bucket."));
911 zone_log_warning(uma_zone_t zone)
913 static const struct timeval warninterval = { 300, 0 };
915 if (!zone_warnings || zone->uz_warning == NULL)
918 if (ratecheck(&zone->uz_ratecheck, &warninterval))
919 printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
923 zone_maxaction(uma_zone_t zone)
926 if (zone->uz_maxaction.ta_func != NULL)
927 taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction);
931 * Routine called by timeout which is used to fire off some time interval
932 * based calculations. (stats, hash size, etc.)
941 uma_timeout(void *unused)
944 zone_foreach(zone_timeout, NULL);
946 /* Reschedule this event */
947 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
951 * Update the working set size estimate for the zone's bucket cache.
952 * The constants chosen here are somewhat arbitrary. With an update period of
953 * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the
957 zone_domain_update_wss(uma_zone_domain_t zdom)
962 MPASS(zdom->uzd_imax >= zdom->uzd_imin);
963 wss = zdom->uzd_imax - zdom->uzd_imin;
964 zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems;
965 zdom->uzd_wss = (4 * wss + zdom->uzd_wss) / 5;
970 * Routine to perform timeout driven calculations. This expands the
971 * hashes and does per cpu statistics aggregation.
976 zone_timeout(uma_zone_t zone, void *unused)
981 if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0)
987 * Hash zones are non-numa by definition so the first domain
988 * is the only one present.
991 pages = keg->uk_domain[0].ud_pages;
994 * Expand the keg hash table.
996 * This is done if the number of slabs is larger than the hash size.
997 * What I'm trying to do here is completely reduce collisions. This
998 * may be a little aggressive. Should I allow for two collisions max?
1000 if ((slabs = pages / keg->uk_ppera) > keg->uk_hash.uh_hashsize) {
1001 struct uma_hash newhash;
1002 struct uma_hash oldhash;
1006 * This is so involved because allocating and freeing
1007 * while the keg lock is held will lead to deadlock.
1008 * I have to do everything in stages and check for
1012 ret = hash_alloc(&newhash, 1 << fls(slabs));
1015 if (hash_expand(&keg->uk_hash, &newhash)) {
1016 oldhash = keg->uk_hash;
1017 keg->uk_hash = newhash;
1022 hash_free(&oldhash);
1029 for (int i = 0; i < vm_ndomains; i++)
1030 zone_domain_update_wss(ZDOM_GET(zone, i));
1034 * Allocate and zero fill the next sized hash table from the appropriate
1038 * hash A new hash structure with the old hash size in uh_hashsize
1041 * 1 on success and 0 on failure.
1044 hash_alloc(struct uma_hash *hash, u_int size)
1048 KASSERT(powerof2(size), ("hash size must be power of 2"));
1049 if (size > UMA_HASH_SIZE_INIT) {
1050 hash->uh_hashsize = size;
1051 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
1052 hash->uh_slab_hash = malloc(alloc, M_UMAHASH, M_NOWAIT);
1054 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
1055 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
1056 UMA_ANYDOMAIN, M_WAITOK);
1057 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
1059 if (hash->uh_slab_hash) {
1060 bzero(hash->uh_slab_hash, alloc);
1061 hash->uh_hashmask = hash->uh_hashsize - 1;
1069 * Expands the hash table for HASH zones. This is done from zone_timeout
1070 * to reduce collisions. This must not be done in the regular allocation
1071 * path, otherwise, we can recurse on the vm while allocating pages.
1074 * oldhash The hash you want to expand
1075 * newhash The hash structure for the new table
1083 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
1085 uma_hash_slab_t slab;
1089 if (!newhash->uh_slab_hash)
1092 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
1096 * I need to investigate hash algorithms for resizing without a
1100 for (idx = 0; idx < oldhash->uh_hashsize; idx++)
1101 while (!LIST_EMPTY(&oldhash->uh_slab_hash[idx])) {
1102 slab = LIST_FIRST(&oldhash->uh_slab_hash[idx]);
1103 LIST_REMOVE(slab, uhs_hlink);
1104 hval = UMA_HASH(newhash, slab->uhs_data);
1105 LIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
1113 * Free the hash bucket to the appropriate backing store.
1116 * slab_hash The hash bucket we're freeing
1117 * hashsize The number of entries in that hash bucket
1123 hash_free(struct uma_hash *hash)
1125 if (hash->uh_slab_hash == NULL)
1127 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
1128 zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
1130 free(hash->uh_slab_hash, M_UMAHASH);
1134 * Frees all outstanding items in a bucket
1137 * zone The zone to free to, must be unlocked.
1138 * bucket The free/alloc bucket with items.
1144 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
1148 if (bucket->ub_cnt == 0)
1151 if ((zone->uz_flags & UMA_ZONE_SMR) != 0 &&
1152 bucket->ub_seq != SMR_SEQ_INVALID) {
1153 smr_wait(zone->uz_smr, bucket->ub_seq);
1154 bucket->ub_seq = SMR_SEQ_INVALID;
1155 for (i = 0; i < bucket->ub_cnt; i++)
1156 item_dtor(zone, bucket->ub_bucket[i],
1157 zone->uz_size, NULL, SKIP_NONE);
1160 for (i = 0; i < bucket->ub_cnt; i++)
1161 zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
1162 zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
1163 if (zone->uz_max_items > 0)
1164 zone_free_limit(zone, bucket->ub_cnt);
1166 bzero(bucket->ub_bucket, sizeof(void *) * bucket->ub_cnt);
1172 * Drains the per cpu caches for a zone.
1174 * NOTE: This may only be called while the zone is being torn down, and not
1175 * during normal operation. This is necessary in order that we do not have
1176 * to migrate CPUs to drain the per-CPU caches.
1179 * zone The zone to drain, must be unlocked.
1185 cache_drain(uma_zone_t zone)
1188 uma_bucket_t bucket;
1193 * XXX: It is safe to not lock the per-CPU caches, because we're
1194 * tearing down the zone anyway. I.e., there will be no further use
1195 * of the caches at this point.
1197 * XXX: It would good to be able to assert that the zone is being
1198 * torn down to prevent improper use of cache_drain().
1200 seq = SMR_SEQ_INVALID;
1201 if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
1202 seq = smr_advance(zone->uz_smr);
1204 cache = &zone->uz_cpu[cpu];
1205 bucket = cache_bucket_unload_alloc(cache);
1207 bucket_free(zone, bucket, NULL);
1208 bucket = cache_bucket_unload_free(cache);
1209 if (bucket != NULL) {
1210 bucket->ub_seq = seq;
1211 bucket_free(zone, bucket, NULL);
1213 bucket = cache_bucket_unload_cross(cache);
1214 if (bucket != NULL) {
1215 bucket->ub_seq = seq;
1216 bucket_free(zone, bucket, NULL);
1219 bucket_cache_reclaim(zone, true);
1223 cache_shrink(uma_zone_t zone, void *unused)
1226 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
1229 zone->uz_bucket_size =
1230 (zone->uz_bucket_size_min + zone->uz_bucket_size) / 2;
1234 cache_drain_safe_cpu(uma_zone_t zone, void *unused)
1237 uma_bucket_t b1, b2, b3;
1240 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
1243 b1 = b2 = b3 = NULL;
1245 cache = &zone->uz_cpu[curcpu];
1246 domain = PCPU_GET(domain);
1247 b1 = cache_bucket_unload_alloc(cache);
1250 * Don't flush SMR zone buckets. This leaves the zone without a
1251 * bucket and forces every free to synchronize().
1253 if ((zone->uz_flags & UMA_ZONE_SMR) == 0) {
1254 b2 = cache_bucket_unload_free(cache);
1255 b3 = cache_bucket_unload_cross(cache);
1260 zone_free_bucket(zone, b1, NULL, domain, false);
1262 zone_free_bucket(zone, b2, NULL, domain, false);
1264 /* Adjust the domain so it goes to zone_free_cross. */
1265 domain = (domain + 1) % vm_ndomains;
1266 zone_free_bucket(zone, b3, NULL, domain, false);
1271 * Safely drain per-CPU caches of a zone(s) to alloc bucket.
1272 * This is an expensive call because it needs to bind to all CPUs
1273 * one by one and enter a critical section on each of them in order
1274 * to safely access their cache buckets.
1275 * Zone lock must not be held on call this function.
1278 pcpu_cache_drain_safe(uma_zone_t zone)
1283 * Polite bucket sizes shrinking was not enough, shrink aggressively.
1286 cache_shrink(zone, NULL);
1288 zone_foreach(cache_shrink, NULL);
1291 thread_lock(curthread);
1292 sched_bind(curthread, cpu);
1293 thread_unlock(curthread);
1296 cache_drain_safe_cpu(zone, NULL);
1298 zone_foreach(cache_drain_safe_cpu, NULL);
1300 thread_lock(curthread);
1301 sched_unbind(curthread);
1302 thread_unlock(curthread);
1306 * Reclaim cached buckets from a zone. All buckets are reclaimed if the caller
1307 * requested a drain, otherwise the per-domain caches are trimmed to either
1308 * estimated working set size.
1311 bucket_cache_reclaim(uma_zone_t zone, bool drain)
1313 uma_zone_domain_t zdom;
1314 uma_bucket_t bucket;
1319 * Shrink the zone bucket size to ensure that the per-CPU caches
1320 * don't grow too large.
1322 if (zone->uz_bucket_size > zone->uz_bucket_size_min)
1323 zone->uz_bucket_size--;
1325 for (i = 0; i < vm_ndomains; i++) {
1327 * The cross bucket is partially filled and not part of
1328 * the item count. Reclaim it individually here.
1330 zdom = ZDOM_GET(zone, i);
1331 if ((zone->uz_flags & UMA_ZONE_SMR) == 0 || drain) {
1332 ZONE_CROSS_LOCK(zone);
1333 bucket = zdom->uzd_cross;
1334 zdom->uzd_cross = NULL;
1335 ZONE_CROSS_UNLOCK(zone);
1337 bucket_free(zone, bucket, NULL);
1341 * If we were asked to drain the zone, we are done only once
1342 * this bucket cache is empty. Otherwise, we reclaim items in
1343 * excess of the zone's estimated working set size. If the
1344 * difference nitems - imin is larger than the WSS estimate,
1345 * then the estimate will grow at the end of this interval and
1346 * we ignore the historical average.
1349 target = drain ? 0 : lmax(zdom->uzd_wss, zdom->uzd_nitems -
1351 while (zdom->uzd_nitems > target) {
1352 bucket = zone_fetch_bucket(zone, zdom, true);
1355 bucket_free(zone, bucket, NULL);
1363 keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
1369 CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes",
1370 keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera);
1372 mem = slab_data(slab, keg);
1373 flags = slab->us_flags;
1375 if (keg->uk_fini != NULL) {
1376 for (i--; i > -1; i--)
1379 * trash_fini implies that dtor was trash_dtor. trash_fini
1380 * would check that memory hasn't been modified since free,
1381 * which executed trash_dtor.
1382 * That's why we need to run uma_dbg_kskip() check here,
1383 * albeit we don't make skip check for other init/fini
1386 if (!uma_dbg_kskip(keg, slab_item(slab, keg, i)) ||
1387 keg->uk_fini != trash_fini)
1389 keg->uk_fini(slab_item(slab, keg, i), keg->uk_size);
1391 if (keg->uk_flags & UMA_ZFLAG_OFFPAGE)
1392 zone_free_item(slabzone(keg->uk_ipers), slab_tohashslab(slab),
1394 keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
1395 uma_total_dec(PAGE_SIZE * keg->uk_ppera);
1399 * Frees pages from a keg back to the system. This is done on demand from
1400 * the pageout daemon.
1405 keg_drain(uma_keg_t keg)
1407 struct slabhead freeslabs;
1409 uma_slab_t slab, tmp;
1412 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
1415 for (i = 0; i < vm_ndomains; i++) {
1416 CTR4(KTR_UMA, "keg_drain %s(%p) domain %d free items: %u",
1417 keg->uk_name, keg, i, dom->ud_free_items);
1418 dom = &keg->uk_domain[i];
1419 LIST_INIT(&freeslabs);
1422 if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0) {
1423 LIST_FOREACH(slab, &dom->ud_free_slab, us_link)
1424 UMA_HASH_REMOVE(&keg->uk_hash, slab);
1426 n = dom->ud_free_slabs;
1427 LIST_SWAP(&freeslabs, &dom->ud_free_slab, uma_slab, us_link);
1428 dom->ud_free_slabs = 0;
1429 dom->ud_free_items -= n * keg->uk_ipers;
1430 dom->ud_pages -= n * keg->uk_ppera;
1433 LIST_FOREACH_SAFE(slab, &freeslabs, us_link, tmp)
1434 keg_free_slab(keg, slab, keg->uk_ipers);
1439 zone_reclaim(uma_zone_t zone, int waitok, bool drain)
1443 * Set draining to interlock with zone_dtor() so we can release our
1444 * locks as we go. Only dtor() should do a WAITOK call since it
1445 * is the only call that knows the structure will still be available
1449 while (zone->uz_flags & UMA_ZFLAG_RECLAIMING) {
1450 if (waitok == M_NOWAIT)
1452 msleep(zone, &ZDOM_GET(zone, 0)->uzd_lock, PVM, "zonedrain",
1455 zone->uz_flags |= UMA_ZFLAG_RECLAIMING;
1457 bucket_cache_reclaim(zone, drain);
1460 * The DRAINING flag protects us from being freed while
1461 * we're running. Normally the uma_rwlock would protect us but we
1462 * must be able to release and acquire the right lock for each keg.
1464 if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0)
1465 keg_drain(zone->uz_keg);
1467 zone->uz_flags &= ~UMA_ZFLAG_RECLAIMING;
1474 zone_drain(uma_zone_t zone, void *unused)
1477 zone_reclaim(zone, M_NOWAIT, true);
1481 zone_trim(uma_zone_t zone, void *unused)
1484 zone_reclaim(zone, M_NOWAIT, false);
1488 * Allocate a new slab for a keg and inserts it into the partial slab list.
1489 * The keg should be unlocked on entry. If the allocation succeeds it will
1490 * be locked on return.
1493 * flags Wait flags for the item initialization routine
1494 * aflags Wait flags for the slab allocation
1497 * The slab that was allocated or NULL if there is no memory and the
1498 * caller specified M_NOWAIT.
1501 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags,
1512 KASSERT(domain >= 0 && domain < vm_ndomains,
1513 ("keg_alloc_slab: domain %d out of range", domain));
1515 allocf = keg->uk_allocf;
1518 if (keg->uk_flags & UMA_ZFLAG_OFFPAGE) {
1519 uma_hash_slab_t hslab;
1520 hslab = zone_alloc_item(slabzone(keg->uk_ipers), NULL,
1524 slab = &hslab->uhs_slab;
1528 * This reproduces the old vm_zone behavior of zero filling pages the
1529 * first time they are added to a zone.
1531 * Malloced items are zeroed in uma_zalloc.
1534 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1539 if (keg->uk_flags & UMA_ZONE_NODUMP)
1542 /* zone is passed for legacy reasons. */
1543 size = keg->uk_ppera * PAGE_SIZE;
1544 mem = allocf(zone, size, domain, &sflags, aflags);
1546 if (keg->uk_flags & UMA_ZFLAG_OFFPAGE)
1547 zone_free_item(slabzone(keg->uk_ipers),
1548 slab_tohashslab(slab), NULL, SKIP_NONE);
1551 uma_total_inc(size);
1553 /* For HASH zones all pages go to the same uma_domain. */
1554 if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0)
1557 /* Point the slab into the allocated memory */
1558 if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE))
1559 slab = (uma_slab_t )(mem + keg->uk_pgoff);
1561 slab_tohashslab(slab)->uhs_data = mem;
1563 if (keg->uk_flags & UMA_ZFLAG_VTOSLAB)
1564 for (i = 0; i < keg->uk_ppera; i++)
1565 vsetzoneslab((vm_offset_t)mem + (i * PAGE_SIZE),
1568 slab->us_freecount = keg->uk_ipers;
1569 slab->us_flags = sflags;
1570 slab->us_domain = domain;
1572 BIT_FILL(keg->uk_ipers, &slab->us_free);
1574 BIT_ZERO(keg->uk_ipers, slab_dbg_bits(slab, keg));
1577 if (keg->uk_init != NULL) {
1578 for (i = 0; i < keg->uk_ipers; i++)
1579 if (keg->uk_init(slab_item(slab, keg, i),
1580 keg->uk_size, flags) != 0)
1582 if (i != keg->uk_ipers) {
1583 keg_free_slab(keg, slab, i);
1587 KEG_LOCK(keg, domain);
1589 CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)",
1590 slab, keg->uk_name, keg);
1592 if (keg->uk_flags & UMA_ZFLAG_HASH)
1593 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
1596 * If we got a slab here it's safe to mark it partially used
1597 * and return. We assume that the caller is going to remove
1598 * at least one item.
1600 dom = &keg->uk_domain[domain];
1601 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
1602 dom->ud_pages += keg->uk_ppera;
1603 dom->ud_free_items += keg->uk_ipers;
1612 * This function is intended to be used early on in place of page_alloc() so
1613 * that we may use the boot time page cache to satisfy allocations before
1617 startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1626 pages = howmany(bytes, PAGE_SIZE);
1627 KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__));
1629 *pflag = UMA_SLAB_BOOT;
1630 m = vm_page_alloc_contig_domain(NULL, 0, domain,
1631 malloc2vm_flags(wait) | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED, pages,
1632 (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT);
1636 pa = VM_PAGE_TO_PHYS(m);
1637 for (i = 0; i < pages; i++, pa += PAGE_SIZE) {
1638 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
1639 defined(__riscv) || defined(__powerpc64__)
1640 if ((wait & M_NODUMP) == 0)
1644 /* Allocate KVA and indirectly advance bootmem. */
1645 mem = (void *)pmap_map(&bootmem, m->phys_addr,
1646 m->phys_addr + (pages * PAGE_SIZE), VM_PROT_READ | VM_PROT_WRITE);
1647 if ((wait & M_ZERO) != 0)
1648 bzero(mem, pages * PAGE_SIZE);
1654 startup_free(void *mem, vm_size_t bytes)
1659 va = (vm_offset_t)mem;
1660 m = PHYS_TO_VM_PAGE(pmap_kextract(va));
1661 pmap_remove(kernel_pmap, va, va + bytes);
1662 for (; bytes != 0; bytes -= PAGE_SIZE, m++) {
1663 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
1664 defined(__riscv) || defined(__powerpc64__)
1665 dump_drop_page(VM_PAGE_TO_PHYS(m));
1667 vm_page_unwire_noq(m);
1673 * Allocates a number of pages from the system
1676 * bytes The number of bytes requested
1677 * wait Shall we wait?
1680 * A pointer to the alloced memory or possibly
1681 * NULL if M_NOWAIT is set.
1684 page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1687 void *p; /* Returned page */
1689 *pflag = UMA_SLAB_KERNEL;
1690 p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait);
1696 pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1699 struct pglist alloctail;
1700 vm_offset_t addr, zkva;
1702 vm_page_t p, p_next;
1707 MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE);
1709 TAILQ_INIT(&alloctail);
1710 flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1711 malloc2vm_flags(wait);
1712 *pflag = UMA_SLAB_KERNEL;
1713 for (cpu = 0; cpu <= mp_maxid; cpu++) {
1714 if (CPU_ABSENT(cpu)) {
1715 p = vm_page_alloc(NULL, 0, flags);
1718 p = vm_page_alloc(NULL, 0, flags);
1720 pc = pcpu_find(cpu);
1721 if (__predict_false(VM_DOMAIN_EMPTY(pc->pc_domain)))
1724 p = vm_page_alloc_domain(NULL, 0,
1725 pc->pc_domain, flags);
1726 if (__predict_false(p == NULL))
1727 p = vm_page_alloc(NULL, 0, flags);
1730 if (__predict_false(p == NULL))
1732 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1734 if ((addr = kva_alloc(bytes)) == 0)
1737 TAILQ_FOREACH(p, &alloctail, listq) {
1738 pmap_qenter(zkva, &p, 1);
1741 return ((void*)addr);
1743 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1744 vm_page_unwire_noq(p);
1751 * Allocates a number of pages from within an object
1754 * bytes The number of bytes requested
1755 * wait Shall we wait?
1758 * A pointer to the alloced memory or possibly
1759 * NULL if M_NOWAIT is set.
1762 noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
1765 TAILQ_HEAD(, vm_page) alloctail;
1767 vm_offset_t retkva, zkva;
1768 vm_page_t p, p_next;
1771 TAILQ_INIT(&alloctail);
1774 npages = howmany(bytes, PAGE_SIZE);
1775 while (npages > 0) {
1776 p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT |
1777 VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1778 ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK :
1782 * Since the page does not belong to an object, its
1785 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1790 * Page allocation failed, free intermediate pages and
1793 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1794 vm_page_unwire_noq(p);
1799 *flags = UMA_SLAB_PRIV;
1800 zkva = keg->uk_kva +
1801 atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1803 TAILQ_FOREACH(p, &alloctail, listq) {
1804 pmap_qenter(zkva, &p, 1);
1808 return ((void *)retkva);
1812 * Allocate physically contiguous pages.
1815 contig_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1819 *pflag = UMA_SLAB_KERNEL;
1820 return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain),
1821 bytes, wait, 0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT));
1825 * Frees a number of pages to the system
1828 * mem A pointer to the memory to be freed
1829 * size The size of the memory being freed
1830 * flags The original p->us_flags field
1836 page_free(void *mem, vm_size_t size, uint8_t flags)
1839 if ((flags & UMA_SLAB_BOOT) != 0) {
1840 startup_free(mem, size);
1844 KASSERT((flags & UMA_SLAB_KERNEL) != 0,
1845 ("UMA: page_free used with invalid flags %x", flags));
1847 kmem_free((vm_offset_t)mem, size);
1851 * Frees pcpu zone allocations
1854 * mem A pointer to the memory to be freed
1855 * size The size of the memory being freed
1856 * flags The original p->us_flags field
1862 pcpu_page_free(void *mem, vm_size_t size, uint8_t flags)
1864 vm_offset_t sva, curva;
1868 MPASS(size == (mp_maxid+1)*PAGE_SIZE);
1870 if ((flags & UMA_SLAB_BOOT) != 0) {
1871 startup_free(mem, size);
1875 sva = (vm_offset_t)mem;
1876 for (curva = sva; curva < sva + size; curva += PAGE_SIZE) {
1877 paddr = pmap_kextract(curva);
1878 m = PHYS_TO_VM_PAGE(paddr);
1879 vm_page_unwire_noq(m);
1882 pmap_qremove(sva, size >> PAGE_SHIFT);
1883 kva_free(sva, size);
1888 * Zero fill initializer
1890 * Arguments/Returns follow uma_init specifications
1893 zero_init(void *mem, int size, int flags)
1901 slab_dbg_bits(uma_slab_t slab, uma_keg_t keg)
1904 return ((void *)((char *)&slab->us_free + BITSET_SIZE(keg->uk_ipers)));
1909 * Actual size of embedded struct slab (!OFFPAGE).
1912 slab_sizeof(int nitems)
1916 s = sizeof(struct uma_slab) + BITSET_SIZE(nitems) * SLAB_BITSETS;
1917 return (roundup(s, UMA_ALIGN_PTR + 1));
1921 * Size of memory for embedded slabs (!OFFPAGE).
1924 slab_space(int nitems)
1926 return (UMA_SLAB_SIZE - slab_sizeof(nitems));
1929 #define UMA_FIXPT_SHIFT 31
1930 #define UMA_FRAC_FIXPT(n, d) \
1931 ((uint32_t)(((uint64_t)(n) << UMA_FIXPT_SHIFT) / (d)))
1932 #define UMA_FIXPT_PCT(f) \
1933 ((u_int)(((uint64_t)100 * (f)) >> UMA_FIXPT_SHIFT))
1934 #define UMA_PCT_FIXPT(pct) UMA_FRAC_FIXPT((pct), 100)
1935 #define UMA_MIN_EFF UMA_PCT_FIXPT(100 - UMA_MAX_WASTE)
1938 * Compute the number of items that will fit in a slab. If hdr is true, the
1939 * item count may be limited to provide space in the slab for an inline slab
1940 * header. Otherwise, all slab space will be provided for item storage.
1943 slab_ipers_hdr(u_int size, u_int rsize, u_int slabsize, bool hdr)
1948 /* The padding between items is not needed after the last item. */
1949 padpi = rsize - size;
1953 * Start with the maximum item count and remove items until
1954 * the slab header first alongside the allocatable memory.
1956 for (ipers = MIN(SLAB_MAX_SETSIZE,
1957 (slabsize + padpi - slab_sizeof(1)) / rsize);
1959 ipers * rsize - padpi + slab_sizeof(ipers) > slabsize;
1963 ipers = MIN((slabsize + padpi) / rsize, SLAB_MAX_SETSIZE);
1970 * Compute the number of items that will fit in a slab for a startup zone.
1973 slab_ipers(size_t size, int align)
1977 rsize = roundup(size, align + 1); /* Assume no CACHESPREAD */
1978 return (slab_ipers_hdr(size, rsize, UMA_SLAB_SIZE, true));
1981 struct keg_layout_result {
1989 keg_layout_one(uma_keg_t keg, u_int rsize, u_int slabsize, u_int fmt,
1990 struct keg_layout_result *kl)
1995 kl->slabsize = slabsize;
1997 /* Handle INTERNAL as inline with an extra page. */
1998 if ((fmt & UMA_ZFLAG_INTERNAL) != 0) {
1999 kl->format &= ~UMA_ZFLAG_INTERNAL;
2000 kl->slabsize += PAGE_SIZE;
2003 kl->ipers = slab_ipers_hdr(keg->uk_size, rsize, kl->slabsize,
2004 (fmt & UMA_ZFLAG_OFFPAGE) == 0);
2006 /* Account for memory used by an offpage slab header. */
2007 total = kl->slabsize;
2008 if ((fmt & UMA_ZFLAG_OFFPAGE) != 0)
2009 total += slabzone(kl->ipers)->uz_keg->uk_rsize;
2011 kl->eff = UMA_FRAC_FIXPT(kl->ipers * rsize, total);
2015 * Determine the format of a uma keg. This determines where the slab header
2016 * will be placed (inline or offpage) and calculates ipers, rsize, and ppera.
2019 * keg The zone we should initialize
2025 keg_layout(uma_keg_t keg)
2027 struct keg_layout_result kl = {}, kl_tmp;
2036 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
2037 (keg->uk_size <= UMA_PCPU_ALLOC_SIZE &&
2038 (keg->uk_flags & UMA_ZONE_CACHESPREAD) == 0),
2039 ("%s: cannot configure for PCPU: keg=%s, size=%u, flags=0x%b",
2040 __func__, keg->uk_name, keg->uk_size, keg->uk_flags,
2042 KASSERT((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) == 0 ||
2043 (keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0,
2044 ("%s: incompatible flags 0x%b", __func__, keg->uk_flags,
2047 alignsize = keg->uk_align + 1;
2050 * Calculate the size of each allocation (rsize) according to
2051 * alignment. If the requested size is smaller than we have
2052 * allocation bits for we round it up.
2054 rsize = MAX(keg->uk_size, UMA_SMALLEST_UNIT);
2055 rsize = roundup2(rsize, alignsize);
2057 if ((keg->uk_flags & UMA_ZONE_CACHESPREAD) != 0) {
2059 * We want one item to start on every align boundary in a page.
2060 * To do this we will span pages. We will also extend the item
2061 * by the size of align if it is an even multiple of align.
2062 * Otherwise, it would fall on the same boundary every time.
2064 if ((rsize & alignsize) == 0)
2066 slabsize = rsize * (PAGE_SIZE / alignsize);
2067 slabsize = MIN(slabsize, rsize * SLAB_MAX_SETSIZE);
2068 slabsize = MIN(slabsize, UMA_CACHESPREAD_MAX_SIZE);
2069 slabsize = round_page(slabsize);
2072 * Start with a slab size of as many pages as it takes to
2073 * represent a single item. We will try to fit as many
2074 * additional items into the slab as possible.
2076 slabsize = round_page(keg->uk_size);
2079 /* Build a list of all of the available formats for this keg. */
2082 /* Evaluate an inline slab layout. */
2083 if ((keg->uk_flags & (UMA_ZONE_NOTOUCH | UMA_ZONE_PCPU)) == 0)
2086 /* TODO: vm_page-embedded slab. */
2089 * We can't do OFFPAGE if we're internal or if we've been
2090 * asked to not go to the VM for buckets. If we do this we
2091 * may end up going to the VM for slabs which we do not want
2092 * to do if we're UMA_ZONE_VM, which clearly forbids it.
2093 * In those cases, evaluate a pseudo-format called INTERNAL
2094 * which has an inline slab header and one extra page to
2095 * guarantee that it fits.
2097 * Otherwise, see if using an OFFPAGE slab will improve our
2100 if ((keg->uk_flags & (UMA_ZFLAG_INTERNAL | UMA_ZONE_VM)) != 0)
2101 fmts[nfmt++] = UMA_ZFLAG_INTERNAL;
2103 fmts[nfmt++] = UMA_ZFLAG_OFFPAGE;
2106 * Choose a slab size and format which satisfy the minimum efficiency.
2107 * Prefer the smallest slab size that meets the constraints.
2109 * Start with a minimum slab size, to accommodate CACHESPREAD. Then,
2110 * for small items (up to PAGE_SIZE), the iteration increment is one
2111 * page; and for large items, the increment is one item.
2113 i = (slabsize + rsize - keg->uk_size) / MAX(PAGE_SIZE, rsize);
2114 KASSERT(i >= 1, ("keg %s(%p) flags=0x%b slabsize=%u, rsize=%u, i=%u",
2115 keg->uk_name, keg, keg->uk_flags, PRINT_UMA_ZFLAGS, slabsize,
2118 slabsize = (rsize <= PAGE_SIZE) ? ptoa(i) :
2119 round_page(rsize * (i - 1) + keg->uk_size);
2121 for (j = 0; j < nfmt; j++) {
2122 /* Only if we have no viable format yet. */
2123 if ((fmts[j] & UMA_ZFLAG_INTERNAL) != 0 &&
2127 keg_layout_one(keg, rsize, slabsize, fmts[j], &kl_tmp);
2128 if (kl_tmp.eff <= kl.eff)
2133 CTR6(KTR_UMA, "keg %s layout: format %#x "
2134 "(ipers %u * rsize %u) / slabsize %#x = %u%% eff",
2135 keg->uk_name, kl.format, kl.ipers, rsize,
2136 kl.slabsize, UMA_FIXPT_PCT(kl.eff));
2138 /* Stop when we reach the minimum efficiency. */
2139 if (kl.eff >= UMA_MIN_EFF)
2143 if (kl.eff >= UMA_MIN_EFF || !multipage_slabs ||
2144 slabsize >= SLAB_MAX_SETSIZE * rsize ||
2145 (keg->uk_flags & (UMA_ZONE_PCPU | UMA_ZONE_CONTIG)) != 0)
2149 pages = atop(kl.slabsize);
2150 if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
2151 pages *= mp_maxid + 1;
2153 keg->uk_rsize = rsize;
2154 keg->uk_ipers = kl.ipers;
2155 keg->uk_ppera = pages;
2156 keg->uk_flags |= kl.format;
2159 * How do we find the slab header if it is offpage or if not all item
2160 * start addresses are in the same page? We could solve the latter
2161 * case with vaddr alignment, but we don't.
2163 if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0 ||
2164 (keg->uk_ipers - 1) * rsize >= PAGE_SIZE) {
2165 if ((keg->uk_flags & UMA_ZONE_NOTPAGE) != 0)
2166 keg->uk_flags |= UMA_ZFLAG_HASH;
2168 keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
2171 CTR6(KTR_UMA, "%s: keg=%s, flags=%#x, rsize=%u, ipers=%u, ppera=%u",
2172 __func__, keg->uk_name, keg->uk_flags, rsize, keg->uk_ipers,
2174 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_MAX_SETSIZE,
2175 ("%s: keg=%s, flags=0x%b, rsize=%u, ipers=%u, ppera=%u", __func__,
2176 keg->uk_name, keg->uk_flags, PRINT_UMA_ZFLAGS, rsize,
2177 keg->uk_ipers, pages));
2181 * Keg header ctor. This initializes all fields, locks, etc. And inserts
2182 * the keg onto the global keg list.
2184 * Arguments/Returns follow uma_ctor specifications
2185 * udata Actually uma_kctor_args
2188 keg_ctor(void *mem, int size, void *udata, int flags)
2190 struct uma_kctor_args *arg = udata;
2191 uma_keg_t keg = mem;
2196 keg->uk_size = arg->size;
2197 keg->uk_init = arg->uminit;
2198 keg->uk_fini = arg->fini;
2199 keg->uk_align = arg->align;
2200 keg->uk_reserve = 0;
2201 keg->uk_flags = arg->flags;
2204 * We use a global round-robin policy by default. Zones with
2205 * UMA_ZONE_FIRSTTOUCH set will use first-touch instead, in which
2206 * case the iterator is never run.
2208 keg->uk_dr.dr_policy = DOMAINSET_RR();
2209 keg->uk_dr.dr_iter = 0;
2212 * The master zone is passed to us at keg-creation time.
2215 keg->uk_name = zone->uz_name;
2217 if (arg->flags & UMA_ZONE_ZINIT)
2218 keg->uk_init = zero_init;
2220 if (arg->flags & UMA_ZONE_MALLOC)
2221 keg->uk_flags |= UMA_ZFLAG_VTOSLAB;
2224 keg->uk_flags &= ~UMA_ZONE_PCPU;
2230 * Use a first-touch NUMA policy for kegs that pmap_extract() will
2231 * work on. Use round-robin for everything else.
2233 * Zones may override the default by specifying either.
2236 if ((keg->uk_flags &
2237 (UMA_ZONE_ROUNDROBIN | UMA_ZFLAG_CACHE | UMA_ZONE_NOTPAGE)) == 0)
2238 keg->uk_flags |= UMA_ZONE_FIRSTTOUCH;
2239 else if ((keg->uk_flags & UMA_ZONE_FIRSTTOUCH) == 0)
2240 keg->uk_flags |= UMA_ZONE_ROUNDROBIN;
2244 * If we haven't booted yet we need allocations to go through the
2245 * startup cache until the vm is ready.
2247 #ifdef UMA_MD_SMALL_ALLOC
2248 if (keg->uk_ppera == 1)
2249 keg->uk_allocf = uma_small_alloc;
2252 if (booted < BOOT_KVA)
2253 keg->uk_allocf = startup_alloc;
2254 else if (keg->uk_flags & UMA_ZONE_PCPU)
2255 keg->uk_allocf = pcpu_page_alloc;
2256 else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 && keg->uk_ppera > 1)
2257 keg->uk_allocf = contig_alloc;
2259 keg->uk_allocf = page_alloc;
2260 #ifdef UMA_MD_SMALL_ALLOC
2261 if (keg->uk_ppera == 1)
2262 keg->uk_freef = uma_small_free;
2265 if (keg->uk_flags & UMA_ZONE_PCPU)
2266 keg->uk_freef = pcpu_page_free;
2268 keg->uk_freef = page_free;
2271 * Initialize keg's locks.
2273 for (i = 0; i < vm_ndomains; i++)
2274 KEG_LOCK_INIT(keg, i, (arg->flags & UMA_ZONE_MTXCLASS));
2277 * If we're putting the slab header in the actual page we need to
2278 * figure out where in each page it goes. See slab_sizeof
2281 if (!(keg->uk_flags & UMA_ZFLAG_OFFPAGE)) {
2284 shsize = slab_sizeof(keg->uk_ipers);
2285 keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - shsize;
2287 * The only way the following is possible is if with our
2288 * UMA_ALIGN_PTR adjustments we are now bigger than
2289 * UMA_SLAB_SIZE. I haven't checked whether this is
2290 * mathematically possible for all cases, so we make
2293 KASSERT(keg->uk_pgoff + shsize <= PAGE_SIZE * keg->uk_ppera,
2294 ("zone %s ipers %d rsize %d size %d slab won't fit",
2295 zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size));
2298 if (keg->uk_flags & UMA_ZFLAG_HASH)
2299 hash_alloc(&keg->uk_hash, 0);
2301 CTR3(KTR_UMA, "keg_ctor %p zone %s(%p)", keg, zone->uz_name, zone);
2303 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
2305 rw_wlock(&uma_rwlock);
2306 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
2307 rw_wunlock(&uma_rwlock);
2312 zone_kva_available(uma_zone_t zone, void *unused)
2316 if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
2320 if (keg->uk_allocf == startup_alloc) {
2321 /* Switch to the real allocator. */
2322 if (keg->uk_flags & UMA_ZONE_PCPU)
2323 keg->uk_allocf = pcpu_page_alloc;
2324 else if ((keg->uk_flags & UMA_ZONE_CONTIG) != 0 &&
2326 keg->uk_allocf = contig_alloc;
2328 keg->uk_allocf = page_alloc;
2333 zone_alloc_counters(uma_zone_t zone, void *unused)
2336 zone->uz_allocs = counter_u64_alloc(M_WAITOK);
2337 zone->uz_frees = counter_u64_alloc(M_WAITOK);
2338 zone->uz_fails = counter_u64_alloc(M_WAITOK);
2339 zone->uz_xdomain = counter_u64_alloc(M_WAITOK);
2343 zone_alloc_sysctl(uma_zone_t zone, void *unused)
2345 uma_zone_domain_t zdom;
2348 struct sysctl_oid *oid, *domainoid;
2349 int domains, i, cnt;
2350 static const char *nokeg = "cache zone";
2354 * Make a sysctl safe copy of the zone name by removing
2355 * any special characters and handling dups by appending
2358 if (zone->uz_namecnt != 0) {
2359 /* Count the number of decimal digits and '_' separator. */
2360 for (i = 1, cnt = zone->uz_namecnt; cnt != 0; i++)
2362 zone->uz_ctlname = malloc(strlen(zone->uz_name) + i + 1,
2364 sprintf(zone->uz_ctlname, "%s_%d", zone->uz_name,
2367 zone->uz_ctlname = strdup(zone->uz_name, M_UMA);
2368 for (c = zone->uz_ctlname; *c != '\0'; c++)
2369 if (strchr("./\\ -", *c) != NULL)
2373 * Basic parameters at the root.
2375 zone->uz_oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_vm_uma),
2376 OID_AUTO, zone->uz_ctlname, CTLFLAG_RD, NULL, "");
2378 SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2379 "size", CTLFLAG_RD, &zone->uz_size, 0, "Allocation size");
2380 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2381 "flags", CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE,
2382 zone, 0, sysctl_handle_uma_zone_flags, "A",
2383 "Allocator configuration flags");
2384 SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2385 "bucket_size", CTLFLAG_RD, &zone->uz_bucket_size, 0,
2386 "Desired per-cpu cache size");
2387 SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2388 "bucket_size_max", CTLFLAG_RD, &zone->uz_bucket_size_max, 0,
2389 "Maximum allowed per-cpu cache size");
2394 if ((zone->uz_flags & UMA_ZFLAG_HASH) == 0)
2395 domains = vm_ndomains;
2398 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
2399 "keg", CTLFLAG_RD, NULL, "");
2401 if ((zone->uz_flags & UMA_ZFLAG_CACHE) == 0) {
2402 SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2403 "name", CTLFLAG_RD, keg->uk_name, "Keg name");
2404 SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2405 "rsize", CTLFLAG_RD, &keg->uk_rsize, 0,
2406 "Real object size with alignment");
2407 SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2408 "ppera", CTLFLAG_RD, &keg->uk_ppera, 0,
2409 "pages per-slab allocation");
2410 SYSCTL_ADD_U16(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2411 "ipers", CTLFLAG_RD, &keg->uk_ipers, 0,
2412 "items available per-slab");
2413 SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2414 "align", CTLFLAG_RD, &keg->uk_align, 0,
2415 "item alignment mask");
2416 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2417 "efficiency", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
2418 keg, 0, sysctl_handle_uma_slab_efficiency, "I",
2419 "Slab utilization (100 - internal fragmentation %)");
2420 domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(oid),
2421 OID_AUTO, "domain", CTLFLAG_RD, NULL, "");
2422 for (i = 0; i < domains; i++) {
2423 dom = &keg->uk_domain[i];
2424 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
2425 OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD,
2427 SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2428 "pages", CTLFLAG_RD, &dom->ud_pages, 0,
2429 "Total pages currently allocated from VM");
2430 SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2431 "free_items", CTLFLAG_RD, &dom->ud_free_items, 0,
2432 "items free in the slab layer");
2435 SYSCTL_ADD_CONST_STRING(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2436 "name", CTLFLAG_RD, nokeg, "Keg name");
2439 * Information about zone limits.
2441 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
2442 "limit", CTLFLAG_RD, NULL, "");
2443 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2444 "items", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
2445 zone, 0, sysctl_handle_uma_zone_items, "QU",
2446 "current number of allocated items if limit is set");
2447 SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2448 "max_items", CTLFLAG_RD, &zone->uz_max_items, 0,
2449 "Maximum number of cached items");
2450 SYSCTL_ADD_U32(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2451 "sleepers", CTLFLAG_RD, &zone->uz_sleepers, 0,
2452 "Number of threads sleeping at limit");
2453 SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2454 "sleeps", CTLFLAG_RD, &zone->uz_sleeps, 0,
2455 "Total zone limit sleeps");
2456 SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2457 "bucket_max", CTLFLAG_RD, &zone->uz_bucket_max, 0,
2458 "Maximum number of items in each domain's bucket cache");
2461 * Per-domain zone information.
2463 domainoid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid),
2464 OID_AUTO, "domain", CTLFLAG_RD, NULL, "");
2465 for (i = 0; i < domains; i++) {
2466 zdom = ZDOM_GET(zone, i);
2467 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(domainoid),
2468 OID_AUTO, VM_DOMAIN(i)->vmd_name, CTLFLAG_RD, NULL, "");
2469 SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2470 "nitems", CTLFLAG_RD, &zdom->uzd_nitems,
2471 "number of items in this domain");
2472 SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2473 "imax", CTLFLAG_RD, &zdom->uzd_imax,
2474 "maximum item count in this period");
2475 SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2476 "imin", CTLFLAG_RD, &zdom->uzd_imin,
2477 "minimum item count in this period");
2478 SYSCTL_ADD_LONG(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2479 "wss", CTLFLAG_RD, &zdom->uzd_wss,
2480 "Working set size");
2484 * General statistics.
2486 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(zone->uz_oid), OID_AUTO,
2487 "stats", CTLFLAG_RD, NULL, "");
2488 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2489 "current", CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
2490 zone, 1, sysctl_handle_uma_zone_cur, "I",
2491 "Current number of allocated items");
2492 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2493 "allocs", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
2494 zone, 0, sysctl_handle_uma_zone_allocs, "QU",
2495 "Total allocation calls");
2496 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2497 "frees", CTLFLAG_RD | CTLTYPE_U64 | CTLFLAG_MPSAFE,
2498 zone, 0, sysctl_handle_uma_zone_frees, "QU",
2499 "Total free calls");
2500 SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2501 "fails", CTLFLAG_RD, &zone->uz_fails,
2502 "Number of allocation failures");
2503 SYSCTL_ADD_COUNTER_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO,
2504 "xdomain", CTLFLAG_RD, &zone->uz_xdomain,
2505 "Free calls from the wrong domain");
2508 struct uma_zone_count {
2514 zone_count(uma_zone_t zone, void *arg)
2516 struct uma_zone_count *cnt;
2520 * Some zones are rapidly created with identical names and
2521 * destroyed out of order. This can lead to gaps in the count.
2522 * Use one greater than the maximum observed for this name.
2524 if (strcmp(zone->uz_name, cnt->name) == 0)
2525 cnt->count = MAX(cnt->count,
2526 zone->uz_namecnt + 1);
2530 zone_update_caches(uma_zone_t zone)
2534 for (i = 0; i <= mp_maxid; i++) {
2535 cache_set_uz_size(&zone->uz_cpu[i], zone->uz_size);
2536 cache_set_uz_flags(&zone->uz_cpu[i], zone->uz_flags);
2541 * Zone header ctor. This initializes all fields, locks, etc.
2543 * Arguments/Returns follow uma_ctor specifications
2544 * udata Actually uma_zctor_args
2547 zone_ctor(void *mem, int size, void *udata, int flags)
2549 struct uma_zone_count cnt;
2550 struct uma_zctor_args *arg = udata;
2551 uma_zone_domain_t zdom;
2552 uma_zone_t zone = mem;
2558 zone->uz_name = arg->name;
2559 zone->uz_ctor = arg->ctor;
2560 zone->uz_dtor = arg->dtor;
2561 zone->uz_init = NULL;
2562 zone->uz_fini = NULL;
2563 zone->uz_sleeps = 0;
2564 zone->uz_bucket_size = 0;
2565 zone->uz_bucket_size_min = 0;
2566 zone->uz_bucket_size_max = BUCKET_MAX;
2567 zone->uz_flags = (arg->flags & UMA_ZONE_SMR);
2568 zone->uz_warning = NULL;
2569 /* The domain structures follow the cpu structures. */
2570 zone->uz_bucket_max = ULONG_MAX;
2571 timevalclear(&zone->uz_ratecheck);
2573 /* Count the number of duplicate names. */
2574 cnt.name = arg->name;
2576 zone_foreach(zone_count, &cnt);
2577 zone->uz_namecnt = cnt.count;
2578 ZONE_CROSS_LOCK_INIT(zone);
2580 for (i = 0; i < vm_ndomains; i++) {
2581 zdom = ZDOM_GET(zone, i);
2582 ZDOM_LOCK_INIT(zone, zdom, (arg->flags & UMA_ZONE_MTXCLASS));
2583 STAILQ_INIT(&zdom->uzd_buckets);
2587 if (arg->uminit == trash_init && arg->fini == trash_fini)
2588 zone->uz_flags |= UMA_ZFLAG_TRASH | UMA_ZFLAG_CTORDTOR;
2592 * This is a pure cache zone, no kegs.
2595 KASSERT((arg->flags & UMA_ZFLAG_CACHE) != 0,
2596 ("zone_ctor: Import specified for non-cache zone."));
2597 zone->uz_flags = arg->flags;
2598 zone->uz_size = arg->size;
2599 zone->uz_import = arg->import;
2600 zone->uz_release = arg->release;
2601 zone->uz_arg = arg->arg;
2604 * Cache zones are round-robin unless a policy is
2605 * specified because they may have incompatible
2608 if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) == 0)
2609 zone->uz_flags |= UMA_ZONE_ROUNDROBIN;
2611 rw_wlock(&uma_rwlock);
2612 LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
2613 rw_wunlock(&uma_rwlock);
2618 * Use the regular zone/keg/slab allocator.
2620 zone->uz_import = zone_import;
2621 zone->uz_release = zone_release;
2622 zone->uz_arg = zone;
2625 if (arg->flags & UMA_ZONE_SECONDARY) {
2626 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
2627 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
2628 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
2629 zone->uz_init = arg->uminit;
2630 zone->uz_fini = arg->fini;
2631 zone->uz_flags |= UMA_ZONE_SECONDARY;
2632 rw_wlock(&uma_rwlock);
2634 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
2635 if (LIST_NEXT(z, uz_link) == NULL) {
2636 LIST_INSERT_AFTER(z, zone, uz_link);
2641 rw_wunlock(&uma_rwlock);
2642 } else if (keg == NULL) {
2643 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
2644 arg->align, arg->flags)) == NULL)
2647 struct uma_kctor_args karg;
2650 /* We should only be here from uma_startup() */
2651 karg.size = arg->size;
2652 karg.uminit = arg->uminit;
2653 karg.fini = arg->fini;
2654 karg.align = arg->align;
2655 karg.flags = (arg->flags & ~UMA_ZONE_SMR);
2657 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
2663 /* Inherit properties from the keg. */
2665 zone->uz_size = keg->uk_size;
2666 zone->uz_flags |= (keg->uk_flags &
2667 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
2670 if (__predict_true(booted >= BOOT_RUNNING)) {
2671 zone_alloc_counters(zone, NULL);
2672 zone_alloc_sysctl(zone, NULL);
2674 zone->uz_allocs = EARLY_COUNTER;
2675 zone->uz_frees = EARLY_COUNTER;
2676 zone->uz_fails = EARLY_COUNTER;
2679 /* Caller requests a private SMR context. */
2680 if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
2681 zone->uz_smr = smr_create(zone->uz_name, 0, 0);
2683 KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) !=
2684 (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET),
2685 ("Invalid zone flag combination"));
2686 if (arg->flags & UMA_ZFLAG_INTERNAL)
2687 zone->uz_bucket_size_max = zone->uz_bucket_size = 0;
2688 if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0)
2689 zone->uz_bucket_size = BUCKET_MAX;
2690 else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0)
2691 zone->uz_bucket_size_max = zone->uz_bucket_size = BUCKET_MIN;
2692 else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0)
2693 zone->uz_bucket_size = 0;
2695 zone->uz_bucket_size = bucket_select(zone->uz_size);
2696 zone->uz_bucket_size_min = zone->uz_bucket_size;
2697 if (zone->uz_dtor != NULL || zone->uz_ctor != NULL)
2698 zone->uz_flags |= UMA_ZFLAG_CTORDTOR;
2699 zone_update_caches(zone);
2705 * Keg header dtor. This frees all data, destroys locks, frees the hash
2706 * table and removes the keg from the global list.
2708 * Arguments/Returns follow uma_dtor specifications
2712 keg_dtor(void *arg, int size, void *udata)
2715 uint32_t free, pages;
2718 keg = (uma_keg_t)arg;
2720 for (i = 0; i < vm_ndomains; i++) {
2721 free += keg->uk_domain[i].ud_free_items;
2722 pages += keg->uk_domain[i].ud_pages;
2723 KEG_LOCK_FINI(keg, i);
2726 printf("Freed UMA keg (%s) was not empty (%u items). "
2727 " Lost %u pages of memory.\n",
2728 keg->uk_name ? keg->uk_name : "",
2729 pages / keg->uk_ppera * keg->uk_ipers - free, pages);
2731 hash_free(&keg->uk_hash);
2737 * Arguments/Returns follow uma_dtor specifications
2741 zone_dtor(void *arg, int size, void *udata)
2747 zone = (uma_zone_t)arg;
2749 sysctl_remove_oid(zone->uz_oid, 1, 1);
2751 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
2754 rw_wlock(&uma_rwlock);
2755 LIST_REMOVE(zone, uz_link);
2756 rw_wunlock(&uma_rwlock);
2757 zone_reclaim(zone, M_WAITOK, true);
2760 * We only destroy kegs from non secondary/non cache zones.
2762 if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) {
2764 rw_wlock(&uma_rwlock);
2765 LIST_REMOVE(keg, uk_link);
2766 rw_wunlock(&uma_rwlock);
2767 zone_free_item(kegs, keg, NULL, SKIP_NONE);
2769 counter_u64_free(zone->uz_allocs);
2770 counter_u64_free(zone->uz_frees);
2771 counter_u64_free(zone->uz_fails);
2772 counter_u64_free(zone->uz_xdomain);
2773 free(zone->uz_ctlname, M_UMA);
2774 for (i = 0; i < vm_ndomains; i++)
2775 ZDOM_LOCK_FINI(ZDOM_GET(zone, i));
2776 ZONE_CROSS_LOCK_FINI(zone);
2780 zone_foreach_unlocked(void (*zfunc)(uma_zone_t, void *arg), void *arg)
2785 LIST_FOREACH(keg, &uma_kegs, uk_link) {
2786 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
2789 LIST_FOREACH(zone, &uma_cachezones, uz_link)
2794 * Traverses every zone in the system and calls a callback
2797 * zfunc A pointer to a function which accepts a zone
2804 zone_foreach(void (*zfunc)(uma_zone_t, void *arg), void *arg)
2807 rw_rlock(&uma_rwlock);
2808 zone_foreach_unlocked(zfunc, arg);
2809 rw_runlock(&uma_rwlock);
2813 * Initialize the kernel memory allocator. This is done after pages can be
2814 * allocated but before general KVA is available.
2817 uma_startup1(vm_offset_t virtual_avail)
2819 struct uma_zctor_args args;
2820 size_t ksize, zsize, size;
2821 uma_keg_t masterkeg;
2825 bootstart = bootmem = virtual_avail;
2827 rw_init(&uma_rwlock, "UMA lock");
2828 sx_init(&uma_reclaim_lock, "umareclaim");
2830 ksize = sizeof(struct uma_keg) +
2831 (sizeof(struct uma_domain) * vm_ndomains);
2832 ksize = roundup(ksize, UMA_SUPER_ALIGN);
2833 zsize = sizeof(struct uma_zone) +
2834 (sizeof(struct uma_cache) * (mp_maxid + 1)) +
2835 (sizeof(struct uma_zone_domain) * vm_ndomains);
2836 zsize = roundup(zsize, UMA_SUPER_ALIGN);
2838 /* Allocate the zone of zones, zone of kegs, and zone of zones keg. */
2839 size = (zsize * 2) + ksize;
2840 m = (uintptr_t)startup_alloc(NULL, size, 0, &pflag, M_NOWAIT | M_ZERO);
2841 zones = (uma_zone_t)m;
2843 kegs = (uma_zone_t)m;
2845 masterkeg = (uma_keg_t)m;
2847 /* "manually" create the initial zone */
2848 memset(&args, 0, sizeof(args));
2849 args.name = "UMA Kegs";
2851 args.ctor = keg_ctor;
2852 args.dtor = keg_dtor;
2853 args.uminit = zero_init;
2855 args.keg = masterkeg;
2856 args.align = UMA_SUPER_ALIGN - 1;
2857 args.flags = UMA_ZFLAG_INTERNAL;
2858 zone_ctor(kegs, zsize, &args, M_WAITOK);
2860 args.name = "UMA Zones";
2862 args.ctor = zone_ctor;
2863 args.dtor = zone_dtor;
2864 args.uminit = zero_init;
2867 args.align = UMA_SUPER_ALIGN - 1;
2868 args.flags = UMA_ZFLAG_INTERNAL;
2869 zone_ctor(zones, zsize, &args, M_WAITOK);
2871 /* Now make zones for slab headers */
2872 slabzones[0] = uma_zcreate("UMA Slabs 0", SLABZONE0_SIZE,
2873 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2874 slabzones[1] = uma_zcreate("UMA Slabs 1", SLABZONE1_SIZE,
2875 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2877 hashzone = uma_zcreate("UMA Hash",
2878 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
2879 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2885 #ifndef UMA_MD_SMALL_ALLOC
2886 extern void vm_radix_reserve_kva(void);
2890 * Advertise the availability of normal kva allocations and switch to
2891 * the default back-end allocator. Marks the KVA we consumed on startup
2892 * as used in the map.
2898 if (bootstart != bootmem) {
2899 vm_map_lock(kernel_map);
2900 (void)vm_map_insert(kernel_map, NULL, 0, bootstart, bootmem,
2901 VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
2902 vm_map_unlock(kernel_map);
2905 #ifndef UMA_MD_SMALL_ALLOC
2906 /* Set up radix zone to use noobj_alloc. */
2907 vm_radix_reserve_kva();
2911 zone_foreach_unlocked(zone_kva_available, NULL);
2916 * Finish our initialization steps.
2923 TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor);
2924 uma_dbg_cnt = counter_u64_alloc(M_WAITOK);
2925 uma_skip_cnt = counter_u64_alloc(M_WAITOK);
2927 zone_foreach_unlocked(zone_alloc_counters, NULL);
2928 zone_foreach_unlocked(zone_alloc_sysctl, NULL);
2929 callout_init(&uma_callout, 1);
2930 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
2931 booted = BOOT_RUNNING;
2933 EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL,
2934 EVENTHANDLER_PRI_FIRST);
2941 booted = BOOT_SHUTDOWN;
2945 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
2946 int align, uint32_t flags)
2948 struct uma_kctor_args args;
2951 args.uminit = uminit;
2953 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
2956 return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK));
2959 /* Public functions */
2962 uma_set_align(int align)
2965 if (align != UMA_ALIGN_CACHE)
2966 uma_align_cache = align;
2971 uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
2972 uma_init uminit, uma_fini fini, int align, uint32_t flags)
2975 struct uma_zctor_args args;
2978 KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
2981 /* This stuff is essential for the zone ctor */
2982 memset(&args, 0, sizeof(args));
2987 args.uminit = uminit;
2991 * Inject procedures which check for memory use after free if we are
2992 * allowed to scramble the memory while it is not allocated. This
2993 * requires that: UMA is actually able to access the memory, no init
2994 * or fini procedures, no dependency on the initial value of the
2995 * memory, and no (legitimate) use of the memory after free. Note,
2996 * the ctor and dtor do not need to be empty.
2998 if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOTOUCH |
2999 UMA_ZONE_NOFREE))) && uminit == NULL && fini == NULL) {
3000 args.uminit = trash_init;
3001 args.fini = trash_fini;
3008 sx_slock(&uma_reclaim_lock);
3009 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
3010 sx_sunlock(&uma_reclaim_lock);
3017 uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor,
3018 uma_init zinit, uma_fini zfini, uma_zone_t master)
3020 struct uma_zctor_args args;
3024 keg = master->uz_keg;
3025 memset(&args, 0, sizeof(args));
3027 args.size = keg->uk_size;
3030 args.uminit = zinit;
3032 args.align = keg->uk_align;
3033 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
3036 sx_slock(&uma_reclaim_lock);
3037 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
3038 sx_sunlock(&uma_reclaim_lock);
3045 uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor,
3046 uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease,
3047 void *arg, int flags)
3049 struct uma_zctor_args args;
3051 memset(&args, 0, sizeof(args));
3056 args.uminit = zinit;
3058 args.import = zimport;
3059 args.release = zrelease;
3062 args.flags = flags | UMA_ZFLAG_CACHE;
3064 return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK));
3069 uma_zdestroy(uma_zone_t zone)
3073 * Large slabs are expensive to reclaim, so don't bother doing
3074 * unnecessary work if we're shutting down.
3076 if (booted == BOOT_SHUTDOWN &&
3077 zone->uz_fini == NULL && zone->uz_release == zone_release)
3079 sx_slock(&uma_reclaim_lock);
3080 zone_free_item(zones, zone, NULL, SKIP_NONE);
3081 sx_sunlock(&uma_reclaim_lock);
3085 uma_zwait(uma_zone_t zone)
3088 if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
3089 uma_zfree_smr(zone, uma_zalloc_smr(zone, M_WAITOK));
3090 else if ((zone->uz_flags & UMA_ZONE_PCPU) != 0)
3091 uma_zfree_pcpu(zone, uma_zalloc_pcpu(zone, M_WAITOK));
3093 uma_zfree(zone, uma_zalloc(zone, M_WAITOK));
3097 uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags)
3099 void *item, *pcpu_item;
3103 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
3105 item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO);
3108 pcpu_item = zpcpu_base_to_offset(item);
3109 if (flags & M_ZERO) {
3111 for (i = 0; i <= mp_maxid; i++)
3112 bzero(zpcpu_get_cpu(pcpu_item, i), zone->uz_size);
3114 bzero(item, zone->uz_size);
3121 * A stub while both regular and pcpu cases are identical.
3124 uma_zfree_pcpu_arg(uma_zone_t zone, void *pcpu_item, void *udata)
3129 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
3131 item = zpcpu_offset_to_base(pcpu_item);
3132 uma_zfree_arg(zone, item, udata);
3135 static inline void *
3136 item_ctor(uma_zone_t zone, int uz_flags, int size, void *udata, int flags,
3142 skipdbg = uma_dbg_zskip(zone, item);
3143 if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3144 zone->uz_ctor != trash_ctor)
3145 trash_ctor(item, size, udata, flags);
3147 /* Check flags before loading ctor pointer. */
3148 if (__predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0) &&
3149 __predict_false(zone->uz_ctor != NULL) &&
3150 zone->uz_ctor(item, size, udata, flags) != 0) {
3151 counter_u64_add(zone->uz_fails, 1);
3152 zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT);
3157 uma_dbg_alloc(zone, NULL, item);
3159 if (__predict_false(flags & M_ZERO))
3160 return (memset(item, 0, size));
3166 item_dtor(uma_zone_t zone, void *item, int size, void *udata,
3167 enum zfreeskip skip)
3172 skipdbg = uma_dbg_zskip(zone, item);
3173 if (skip == SKIP_NONE && !skipdbg) {
3174 if ((zone->uz_flags & UMA_ZONE_MALLOC) != 0)
3175 uma_dbg_free(zone, udata, item);
3177 uma_dbg_free(zone, NULL, item);
3180 if (__predict_true(skip < SKIP_DTOR)) {
3181 if (zone->uz_dtor != NULL)
3182 zone->uz_dtor(item, size, udata);
3184 if (!skipdbg && (zone->uz_flags & UMA_ZFLAG_TRASH) != 0 &&
3185 zone->uz_dtor != trash_dtor)
3186 trash_dtor(item, size, udata);
3191 #if defined(INVARIANTS) || defined(DEBUG_MEMGUARD) || defined(WITNESS)
3192 #define UMA_ZALLOC_DEBUG
3194 uma_zalloc_debug(uma_zone_t zone, void **itemp, void *udata, int flags)
3200 if (flags & M_WAITOK) {
3201 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
3202 "uma_zalloc_debug: zone \"%s\"", zone->uz_name);
3207 KASSERT((flags & M_EXEC) == 0,
3208 ("uma_zalloc_debug: called with M_EXEC"));
3209 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3210 ("uma_zalloc_debug: called within spinlock or critical section"));
3211 KASSERT((zone->uz_flags & UMA_ZONE_PCPU) == 0 || (flags & M_ZERO) == 0,
3212 ("uma_zalloc_debug: allocating from a pcpu zone with M_ZERO"));
3215 #ifdef DEBUG_MEMGUARD
3216 if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && memguard_cmp_zone(zone)) {
3218 item = memguard_alloc(zone->uz_size, flags);
3220 error = EJUSTRETURN;
3221 if (zone->uz_init != NULL &&
3222 zone->uz_init(item, zone->uz_size, flags) != 0) {
3226 if (zone->uz_ctor != NULL &&
3227 zone->uz_ctor(item, zone->uz_size, udata,
3229 counter_u64_add(zone->uz_fails, 1);
3230 zone->uz_fini(item, zone->uz_size);
3237 /* This is unfortunate but should not be fatal. */
3244 uma_zfree_debug(uma_zone_t zone, void *item, void *udata)
3246 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3247 ("uma_zfree_debug: called with spinlock or critical section held"));
3249 #ifdef DEBUG_MEMGUARD
3250 if ((zone->uz_flags & UMA_ZONE_SMR) == 0 && is_memguard_addr(item)) {
3251 if (zone->uz_dtor != NULL)
3252 zone->uz_dtor(item, zone->uz_size, udata);
3253 if (zone->uz_fini != NULL)
3254 zone->uz_fini(item, zone->uz_size);
3255 memguard_free(item);
3256 return (EJUSTRETURN);
3263 static inline void *
3264 cache_alloc_item(uma_zone_t zone, uma_cache_t cache, uma_cache_bucket_t bucket,
3265 void *udata, int flags)
3270 item = cache_bucket_pop(cache, bucket);
3271 size = cache_uz_size(cache);
3272 uz_flags = cache_uz_flags(cache);
3274 return (item_ctor(zone, uz_flags, size, udata, flags, item));
3277 static __noinline void *
3278 cache_alloc_retry(uma_zone_t zone, uma_cache_t cache, void *udata, int flags)
3280 uma_cache_bucket_t bucket;
3283 while (cache_alloc(zone, cache, udata, flags)) {
3284 cache = &zone->uz_cpu[curcpu];
3285 bucket = &cache->uc_allocbucket;
3286 if (__predict_false(bucket->ucb_cnt == 0))
3288 return (cache_alloc_item(zone, cache, bucket, udata, flags));
3293 * We can not get a bucket so try to return a single item.
3295 if (zone->uz_flags & UMA_ZONE_FIRSTTOUCH)
3296 domain = PCPU_GET(domain);
3298 domain = UMA_ANYDOMAIN;
3299 return (zone_alloc_item(zone, udata, domain, flags));
3304 uma_zalloc_smr(uma_zone_t zone, int flags)
3306 uma_cache_bucket_t bucket;
3309 #ifdef UMA_ZALLOC_DEBUG
3312 KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
3313 ("uma_zalloc_arg: called with non-SMR zone.\n"));
3314 if (uma_zalloc_debug(zone, &item, NULL, flags) == EJUSTRETURN)
3319 cache = &zone->uz_cpu[curcpu];
3320 bucket = &cache->uc_allocbucket;
3321 if (__predict_false(bucket->ucb_cnt == 0))
3322 return (cache_alloc_retry(zone, cache, NULL, flags));
3323 return (cache_alloc_item(zone, cache, bucket, NULL, flags));
3328 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
3330 uma_cache_bucket_t bucket;
3333 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3334 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3336 /* This is the fast path allocation */
3337 CTR3(KTR_UMA, "uma_zalloc_arg zone %s(%p) flags %d", zone->uz_name,
3340 #ifdef UMA_ZALLOC_DEBUG
3343 KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
3344 ("uma_zalloc_arg: called with SMR zone.\n"));
3345 if (uma_zalloc_debug(zone, &item, udata, flags) == EJUSTRETURN)
3350 * If possible, allocate from the per-CPU cache. There are two
3351 * requirements for safe access to the per-CPU cache: (1) the thread
3352 * accessing the cache must not be preempted or yield during access,
3353 * and (2) the thread must not migrate CPUs without switching which
3354 * cache it accesses. We rely on a critical section to prevent
3355 * preemption and migration. We release the critical section in
3356 * order to acquire the zone mutex if we are unable to allocate from
3357 * the current cache; when we re-acquire the critical section, we
3358 * must detect and handle migration if it has occurred.
3361 cache = &zone->uz_cpu[curcpu];
3362 bucket = &cache->uc_allocbucket;
3363 if (__predict_false(bucket->ucb_cnt == 0))
3364 return (cache_alloc_retry(zone, cache, udata, flags));
3365 return (cache_alloc_item(zone, cache, bucket, udata, flags));
3369 * Replenish an alloc bucket and possibly restore an old one. Called in
3370 * a critical section. Returns in a critical section.
3372 * A false return value indicates an allocation failure.
3373 * A true return value indicates success and the caller should retry.
3375 static __noinline bool
3376 cache_alloc(uma_zone_t zone, uma_cache_t cache, void *udata, int flags)
3378 uma_bucket_t bucket;
3382 CRITICAL_ASSERT(curthread);
3385 * If we have run out of items in our alloc bucket see
3386 * if we can switch with the free bucket.
3388 * SMR Zones can't re-use the free bucket until the sequence has
3391 if ((cache_uz_flags(cache) & UMA_ZONE_SMR) == 0 &&
3392 cache->uc_freebucket.ucb_cnt != 0) {
3393 cache_bucket_swap(&cache->uc_freebucket,
3394 &cache->uc_allocbucket);
3399 * Discard any empty allocation bucket while we hold no locks.
3401 bucket = cache_bucket_unload_alloc(cache);
3404 if (bucket != NULL) {
3405 KASSERT(bucket->ub_cnt == 0,
3406 ("cache_alloc: Entered with non-empty alloc bucket."));
3407 bucket_free(zone, bucket, udata);
3410 /* Short-circuit for zones without buckets and low memory. */
3411 if (zone->uz_bucket_size == 0 || bucketdisable) {
3417 * Attempt to retrieve the item from the per-CPU cache has failed, so
3418 * we must go back to the zone. This requires the zdom lock, so we
3419 * must drop the critical section, then re-acquire it when we go back
3420 * to the cache. Since the critical section is released, we may be
3421 * preempted or migrate. As such, make sure not to maintain any
3422 * thread-local state specific to the cache from prior to releasing
3423 * the critical section.
3425 domain = PCPU_GET(domain);
3426 if ((cache_uz_flags(cache) & UMA_ZONE_ROUNDROBIN) != 0)
3427 domain = zone_domain_highest(zone, domain);
3428 bucket = cache_fetch_bucket(zone, cache, domain);
3429 if (bucket == NULL) {
3430 bucket = zone_alloc_bucket(zone, udata, domain, flags);
3435 CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
3436 zone->uz_name, zone, bucket);
3437 if (bucket == NULL) {
3443 * See if we lost the race or were migrated. Cache the
3444 * initialized bucket to make this less likely or claim
3445 * the memory directly.
3448 cache = &zone->uz_cpu[curcpu];
3449 if (cache->uc_allocbucket.ucb_bucket == NULL &&
3450 ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) == 0 ||
3451 domain == PCPU_GET(domain))) {
3453 atomic_add_long(&ZDOM_GET(zone, domain)->uzd_imax,
3455 cache_bucket_load_alloc(cache, bucket);
3460 * We lost the race, release this bucket and start over.
3463 zone_put_bucket(zone, domain, bucket, udata, false);
3470 uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
3473 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3474 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3476 /* This is the fast path allocation */
3477 CTR4(KTR_UMA, "uma_zalloc_domain zone %s(%p) domain %d flags %d",
3478 zone->uz_name, zone, domain, flags);
3480 if (flags & M_WAITOK) {
3481 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
3482 "uma_zalloc_domain: zone \"%s\"", zone->uz_name);
3484 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3485 ("uma_zalloc_domain: called with spinlock or critical section held"));
3487 return (zone_alloc_item(zone, udata, domain, flags));
3491 * Find a slab with some space. Prefer slabs that are partially used over those
3492 * that are totally full. This helps to reduce fragmentation.
3494 * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check
3498 keg_first_slab(uma_keg_t keg, int domain, bool rr)
3504 KASSERT(domain >= 0 && domain < vm_ndomains,
3505 ("keg_first_slab: domain %d out of range", domain));
3506 KEG_LOCK_ASSERT(keg, domain);
3511 dom = &keg->uk_domain[domain];
3512 if ((slab = LIST_FIRST(&dom->ud_part_slab)) != NULL)
3514 if ((slab = LIST_FIRST(&dom->ud_free_slab)) != NULL) {
3515 LIST_REMOVE(slab, us_link);
3516 dom->ud_free_slabs--;
3517 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
3521 domain = (domain + 1) % vm_ndomains;
3522 } while (domain != start);
3528 * Fetch an existing slab from a free or partial list. Returns with the
3529 * keg domain lock held if a slab was found or unlocked if not.
3532 keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags)
3537 /* HASH has a single free list. */
3538 if ((keg->uk_flags & UMA_ZFLAG_HASH) != 0)
3541 KEG_LOCK(keg, domain);
3542 reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve;
3543 if (keg->uk_domain[domain].ud_free_items <= reserve ||
3544 (slab = keg_first_slab(keg, domain, rr)) == NULL) {
3545 KEG_UNLOCK(keg, domain);
3552 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags)
3554 struct vm_domainset_iter di;
3561 * Use the keg's policy if upper layers haven't already specified a
3562 * domain (as happens with first-touch zones).
3564 * To avoid races we run the iterator with the keg lock held, but that
3565 * means that we cannot allow the vm_domainset layer to sleep. Thus,
3566 * clear M_WAITOK and handle low memory conditions locally.
3568 rr = rdomain == UMA_ANYDOMAIN;
3570 aflags = (flags & ~M_WAITOK) | M_NOWAIT;
3571 vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
3579 slab = keg_fetch_free_slab(keg, domain, rr, flags);
3584 * M_NOVM means don't ask at all!
3589 slab = keg_alloc_slab(keg, zone, domain, flags, aflags);
3592 if (!rr && (flags & M_WAITOK) == 0)
3594 if (rr && vm_domainset_iter_policy(&di, &domain) != 0) {
3595 if ((flags & M_WAITOK) != 0) {
3596 vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
3604 * We might not have been able to get a slab but another cpu
3605 * could have while we were unlocked. Check again before we
3608 if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL)
3615 slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
3621 KEG_LOCK_ASSERT(keg, slab->us_domain);
3623 dom = &keg->uk_domain[slab->us_domain];
3624 freei = BIT_FFS(keg->uk_ipers, &slab->us_free) - 1;
3625 BIT_CLR(keg->uk_ipers, freei, &slab->us_free);
3626 item = slab_item(slab, keg, freei);
3627 slab->us_freecount--;
3628 dom->ud_free_items--;
3631 * Move this slab to the full list. It must be on the partial list, so
3632 * we do not need to update the free slab count. In particular,
3633 * keg_fetch_slab() always returns slabs on the partial list.
3635 if (slab->us_freecount == 0) {
3636 LIST_REMOVE(slab, us_link);
3637 LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link);
3644 zone_import(void *arg, void **bucket, int max, int domain, int flags)
3658 /* Try to keep the buckets totally full */
3659 for (i = 0; i < max; ) {
3660 if ((slab = keg_fetch_slab(keg, zone, domain, flags)) == NULL)
3663 stripe = howmany(max, vm_ndomains);
3665 dom = &keg->uk_domain[slab->us_domain];
3666 while (slab->us_freecount && i < max) {
3667 bucket[i++] = slab_alloc_item(keg, slab);
3668 if (dom->ud_free_items <= keg->uk_reserve)
3672 * If the zone is striped we pick a new slab for every
3673 * N allocations. Eliminating this conditional will
3674 * instead pick a new domain for each bucket rather
3675 * than stripe within each bucket. The current option
3676 * produces more fragmentation and requires more cpu
3677 * time but yields better distribution.
3679 if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0 &&
3680 vm_ndomains > 1 && --stripe == 0)
3684 KEG_UNLOCK(keg, slab->us_domain);
3685 /* Don't block if we allocated any successfully. */
3694 zone_alloc_limit_hard(uma_zone_t zone, int count, int flags)
3696 uint64_t old, new, total, max;
3699 * The hard case. We're going to sleep because there were existing
3700 * sleepers or because we ran out of items. This routine enforces
3701 * fairness by keeping fifo order.
3703 * First release our ill gotten gains and make some noise.
3706 zone_free_limit(zone, count);
3707 zone_log_warning(zone);
3708 zone_maxaction(zone);
3709 if (flags & M_NOWAIT)
3713 * We need to allocate an item or set ourself as a sleeper
3714 * while the sleepq lock is held to avoid wakeup races. This
3715 * is essentially a home rolled semaphore.
3717 sleepq_lock(&zone->uz_max_items);
3718 old = zone->uz_items;
3720 MPASS(UZ_ITEMS_SLEEPERS(old) < UZ_ITEMS_SLEEPERS_MAX);
3721 /* Cache the max since we will evaluate twice. */
3722 max = zone->uz_max_items;
3723 if (UZ_ITEMS_SLEEPERS(old) != 0 ||
3724 UZ_ITEMS_COUNT(old) >= max)
3725 new = old + UZ_ITEMS_SLEEPER;
3727 new = old + MIN(count, max - old);
3728 } while (atomic_fcmpset_64(&zone->uz_items, &old, new) == 0);
3730 /* We may have successfully allocated under the sleepq lock. */
3731 if (UZ_ITEMS_SLEEPERS(new) == 0) {
3732 sleepq_release(&zone->uz_max_items);
3737 * This is in a different cacheline from uz_items so that we
3738 * don't constantly invalidate the fastpath cacheline when we
3739 * adjust item counts. This could be limited to toggling on
3742 atomic_add_32(&zone->uz_sleepers, 1);
3743 atomic_add_64(&zone->uz_sleeps, 1);
3746 * We have added ourselves as a sleeper. The sleepq lock
3747 * protects us from wakeup races. Sleep now and then retry.
3749 sleepq_add(&zone->uz_max_items, NULL, "zonelimit", 0, 0);
3750 sleepq_wait(&zone->uz_max_items, PVM);
3753 * After wakeup, remove ourselves as a sleeper and try
3754 * again. We no longer have the sleepq lock for protection.
3756 * Subract ourselves as a sleeper while attempting to add
3759 atomic_subtract_32(&zone->uz_sleepers, 1);
3760 old = atomic_fetchadd_64(&zone->uz_items,
3761 -(UZ_ITEMS_SLEEPER - count));
3762 /* We're no longer a sleeper. */
3763 old -= UZ_ITEMS_SLEEPER;
3766 * If we're still at the limit, restart. Notably do not
3767 * block on other sleepers. Cache the max value to protect
3768 * against changes via sysctl.
3770 total = UZ_ITEMS_COUNT(old);
3771 max = zone->uz_max_items;
3774 /* Truncate if necessary, otherwise wake other sleepers. */
3775 if (total + count > max) {
3776 zone_free_limit(zone, total + count - max);
3777 count = max - total;
3778 } else if (total + count < max && UZ_ITEMS_SLEEPERS(old) != 0)
3779 wakeup_one(&zone->uz_max_items);
3786 * Allocate 'count' items from our max_items limit. Returns the number
3787 * available. If M_NOWAIT is not specified it will sleep until at least
3788 * one item can be allocated.
3791 zone_alloc_limit(uma_zone_t zone, int count, int flags)
3796 max = zone->uz_max_items;
3800 * We expect normal allocations to succeed with a simple
3803 old = atomic_fetchadd_64(&zone->uz_items, count);
3804 if (__predict_true(old + count <= max))
3808 * If we had some items and no sleepers just return the
3809 * truncated value. We have to release the excess space
3810 * though because that may wake sleepers who weren't woken
3811 * because we were temporarily over the limit.
3814 zone_free_limit(zone, (old + count) - max);
3817 return (zone_alloc_limit_hard(zone, count, flags));
3821 * Free a number of items back to the limit.
3824 zone_free_limit(uma_zone_t zone, int count)
3831 * In the common case we either have no sleepers or
3832 * are still over the limit and can just return.
3834 old = atomic_fetchadd_64(&zone->uz_items, -count);
3835 if (__predict_true(UZ_ITEMS_SLEEPERS(old) == 0 ||
3836 UZ_ITEMS_COUNT(old) - count >= zone->uz_max_items))
3840 * Moderate the rate of wakeups. Sleepers will continue
3841 * to generate wakeups if necessary.
3843 wakeup_one(&zone->uz_max_items);
3847 zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags)
3849 uma_bucket_t bucket;
3852 CTR3(KTR_UMA, "zone_alloc_bucket zone %s(%p) domain %d", zone->uz_name,
3855 /* Avoid allocs targeting empty domains. */
3856 if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
3857 domain = UMA_ANYDOMAIN;
3858 if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0)
3859 domain = UMA_ANYDOMAIN;
3861 if (zone->uz_max_items > 0)
3862 maxbucket = zone_alloc_limit(zone, zone->uz_bucket_size,
3865 maxbucket = zone->uz_bucket_size;
3869 /* Don't wait for buckets, preserve caller's NOVM setting. */
3870 bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
3871 if (bucket == NULL) {
3876 bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
3877 MIN(maxbucket, bucket->ub_entries), domain, flags);
3880 * Initialize the memory if necessary.
3882 if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
3885 for (i = 0; i < bucket->ub_cnt; i++)
3886 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
3890 * If we couldn't initialize the whole bucket, put the
3891 * rest back onto the freelist.
3893 if (i != bucket->ub_cnt) {
3894 zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
3895 bucket->ub_cnt - i);
3897 bzero(&bucket->ub_bucket[i],
3898 sizeof(void *) * (bucket->ub_cnt - i));
3904 cnt = bucket->ub_cnt;
3905 if (bucket->ub_cnt == 0) {
3906 bucket_free(zone, bucket, udata);
3907 counter_u64_add(zone->uz_fails, 1);
3911 if (zone->uz_max_items > 0 && cnt < maxbucket)
3912 zone_free_limit(zone, maxbucket - cnt);
3918 * Allocates a single item from a zone.
3921 * zone The zone to alloc for.
3922 * udata The data to be passed to the constructor.
3923 * domain The domain to allocate from or UMA_ANYDOMAIN.
3924 * flags M_WAITOK, M_NOWAIT, M_ZERO.
3927 * NULL if there is no memory and M_NOWAIT is set
3928 * An item if successful
3932 zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags)
3936 if (zone->uz_max_items > 0 && zone_alloc_limit(zone, 1, flags) == 0)
3939 /* Avoid allocs targeting empty domains. */
3940 if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
3941 domain = UMA_ANYDOMAIN;
3943 if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1)
3947 * We have to call both the zone's init (not the keg's init)
3948 * and the zone's ctor. This is because the item is going from
3949 * a keg slab directly to the user, and the user is expecting it
3950 * to be both zone-init'd as well as zone-ctor'd.
3952 if (zone->uz_init != NULL) {
3953 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
3954 zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT);
3958 item = item_ctor(zone, zone->uz_flags, zone->uz_size, udata, flags,
3963 counter_u64_add(zone->uz_allocs, 1);
3964 CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
3965 zone->uz_name, zone);
3970 counter_u64_add(zone->uz_fails, 1);
3972 if (zone->uz_max_items > 0)
3973 zone_free_limit(zone, 1);
3974 CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
3975 zone->uz_name, zone);
3982 uma_zfree_smr(uma_zone_t zone, void *item)
3985 uma_cache_bucket_t bucket;
3986 int itemdomain, uz_flags;
3988 #ifdef UMA_ZALLOC_DEBUG
3989 KASSERT((zone->uz_flags & UMA_ZONE_SMR) != 0,
3990 ("uma_zfree_smr: called with non-SMR zone.\n"));
3991 KASSERT(item != NULL, ("uma_zfree_smr: Called with NULL pointer."));
3992 SMR_ASSERT_NOT_ENTERED(zone->uz_smr);
3993 if (uma_zfree_debug(zone, item, NULL) == EJUSTRETURN)
3996 cache = &zone->uz_cpu[curcpu];
3997 uz_flags = cache_uz_flags(cache);
4000 if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
4001 itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item));
4005 cache = &zone->uz_cpu[curcpu];
4006 /* SMR Zones must free to the free bucket. */
4007 bucket = &cache->uc_freebucket;
4009 if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4010 PCPU_GET(domain) != itemdomain) {
4011 bucket = &cache->uc_crossbucket;
4014 if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
4015 cache_bucket_push(cache, bucket, item);
4019 } while (cache_free(zone, cache, NULL, item, itemdomain));
4023 * If nothing else caught this, we'll just do an internal free.
4025 zone_free_item(zone, item, NULL, SKIP_NONE);
4030 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
4033 uma_cache_bucket_t bucket;
4034 int itemdomain, uz_flags;
4036 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
4037 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
4039 CTR2(KTR_UMA, "uma_zfree_arg zone %s(%p)", zone->uz_name, zone);
4041 #ifdef UMA_ZALLOC_DEBUG
4042 KASSERT((zone->uz_flags & UMA_ZONE_SMR) == 0,
4043 ("uma_zfree_arg: called with SMR zone.\n"));
4044 if (uma_zfree_debug(zone, item, udata) == EJUSTRETURN)
4047 /* uma_zfree(..., NULL) does nothing, to match free(9). */
4052 * We are accessing the per-cpu cache without a critical section to
4053 * fetch size and flags. This is acceptable, if we are preempted we
4054 * will simply read another cpu's line.
4056 cache = &zone->uz_cpu[curcpu];
4057 uz_flags = cache_uz_flags(cache);
4058 if (UMA_ALWAYS_CTORDTOR ||
4059 __predict_false((uz_flags & UMA_ZFLAG_CTORDTOR) != 0))
4060 item_dtor(zone, item, cache_uz_size(cache), udata, SKIP_NONE);
4063 * The race here is acceptable. If we miss it we'll just have to wait
4064 * a little longer for the limits to be reset.
4066 if (__predict_false(uz_flags & UMA_ZFLAG_LIMIT)) {
4067 if (zone->uz_sleepers > 0)
4072 * If possible, free to the per-CPU cache. There are two
4073 * requirements for safe access to the per-CPU cache: (1) the thread
4074 * accessing the cache must not be preempted or yield during access,
4075 * and (2) the thread must not migrate CPUs without switching which
4076 * cache it accesses. We rely on a critical section to prevent
4077 * preemption and migration. We release the critical section in
4078 * order to acquire the zone mutex if we are unable to free to the
4079 * current cache; when we re-acquire the critical section, we must
4080 * detect and handle migration if it has occurred.
4084 if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
4085 itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item));
4089 cache = &zone->uz_cpu[curcpu];
4091 * Try to free into the allocbucket first to give LIFO
4092 * ordering for cache-hot datastructures. Spill over
4093 * into the freebucket if necessary. Alloc will swap
4094 * them if one runs dry.
4096 bucket = &cache->uc_allocbucket;
4098 if ((uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4099 PCPU_GET(domain) != itemdomain) {
4100 bucket = &cache->uc_crossbucket;
4103 if (bucket->ucb_cnt >= bucket->ucb_entries)
4104 bucket = &cache->uc_freebucket;
4105 if (__predict_true(bucket->ucb_cnt < bucket->ucb_entries)) {
4106 cache_bucket_push(cache, bucket, item);
4110 } while (cache_free(zone, cache, udata, item, itemdomain));
4114 * If nothing else caught this, we'll just do an internal free.
4117 zone_free_item(zone, item, udata, SKIP_DTOR);
4122 * sort crossdomain free buckets to domain correct buckets and cache
4126 zone_free_cross(uma_zone_t zone, uma_bucket_t bucket, void *udata)
4128 struct uma_bucketlist fullbuckets;
4129 uma_zone_domain_t zdom;
4136 "uma_zfree: zone %s(%p) draining cross bucket %p",
4137 zone->uz_name, zone, bucket);
4140 * It is possible for buckets to arrive here out of order so we fetch
4141 * the current smr seq rather than accepting the bucket's.
4143 seq = SMR_SEQ_INVALID;
4144 if ((zone->uz_flags & UMA_ZONE_SMR) != 0)
4145 seq = smr_advance(zone->uz_smr);
4148 * To avoid having ndomain * ndomain buckets for sorting we have a
4149 * lock on the current crossfree bucket. A full matrix with
4150 * per-domain locking could be used if necessary.
4152 STAILQ_INIT(&fullbuckets);
4153 ZONE_CROSS_LOCK(zone);
4154 while (bucket->ub_cnt > 0) {
4155 item = bucket->ub_bucket[bucket->ub_cnt - 1];
4156 domain = _vm_phys_domain(pmap_kextract((vm_offset_t)item));
4157 zdom = ZDOM_GET(zone, domain);
4158 if (zdom->uzd_cross == NULL) {
4159 zdom->uzd_cross = bucket_alloc(zone, udata, M_NOWAIT);
4160 if (zdom->uzd_cross == NULL)
4163 b = zdom->uzd_cross;
4164 b->ub_bucket[b->ub_cnt++] = item;
4166 if (b->ub_cnt == b->ub_entries) {
4167 STAILQ_INSERT_HEAD(&fullbuckets, b, ub_link);
4168 zdom->uzd_cross = NULL;
4172 ZONE_CROSS_UNLOCK(zone);
4173 if (bucket->ub_cnt == 0)
4174 bucket->ub_seq = SMR_SEQ_INVALID;
4175 bucket_free(zone, bucket, udata);
4177 while ((b = STAILQ_FIRST(&fullbuckets)) != NULL) {
4178 STAILQ_REMOVE_HEAD(&fullbuckets, ub_link);
4179 domain = _vm_phys_domain(pmap_kextract(
4180 (vm_offset_t)b->ub_bucket[0]));
4181 zone_put_bucket(zone, domain, b, udata, true);
4187 zone_free_bucket(uma_zone_t zone, uma_bucket_t bucket, void *udata,
4188 int itemdomain, bool ws)
4193 * Buckets coming from the wrong domain will be entirely for the
4194 * only other domain on two domain systems. In this case we can
4195 * simply cache them. Otherwise we need to sort them back to
4198 if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0 &&
4199 vm_ndomains > 2 && PCPU_GET(domain) != itemdomain) {
4200 zone_free_cross(zone, bucket, udata);
4206 * Attempt to save the bucket in the zone's domain bucket cache.
4209 "uma_zfree: zone %s(%p) putting bucket %p on free list",
4210 zone->uz_name, zone, bucket);
4211 /* ub_cnt is pointing to the last free item */
4212 if ((zone->uz_flags & UMA_ZONE_ROUNDROBIN) != 0)
4213 itemdomain = zone_domain_lowest(zone, itemdomain);
4214 zone_put_bucket(zone, itemdomain, bucket, udata, ws);
4218 * Populate a free or cross bucket for the current cpu cache. Free any
4219 * existing full bucket either to the zone cache or back to the slab layer.
4221 * Enters and returns in a critical section. false return indicates that
4222 * we can not satisfy this free in the cache layer. true indicates that
4223 * the caller should retry.
4225 static __noinline bool
4226 cache_free(uma_zone_t zone, uma_cache_t cache, void *udata, void *item,
4229 uma_cache_bucket_t cbucket;
4230 uma_bucket_t newbucket, bucket;
4232 CRITICAL_ASSERT(curthread);
4234 if (zone->uz_bucket_size == 0)
4237 cache = &zone->uz_cpu[curcpu];
4241 * FIRSTTOUCH domains need to free to the correct zdom. When
4242 * enabled this is the zdom of the item. The bucket is the
4243 * cross bucket if the current domain and itemdomain do not match.
4245 cbucket = &cache->uc_freebucket;
4247 if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) {
4248 if (PCPU_GET(domain) != itemdomain) {
4249 cbucket = &cache->uc_crossbucket;
4250 if (cbucket->ucb_cnt != 0)
4251 counter_u64_add(zone->uz_xdomain,
4256 bucket = cache_bucket_unload(cbucket);
4257 KASSERT(bucket == NULL || bucket->ub_cnt == bucket->ub_entries,
4258 ("cache_free: Entered with non-full free bucket."));
4260 /* We are no longer associated with this CPU. */
4264 * Don't let SMR zones operate without a free bucket. Force
4265 * a synchronize and re-use this one. We will only degrade
4266 * to a synchronize every bucket_size items rather than every
4267 * item if we fail to allocate a bucket.
4269 if ((zone->uz_flags & UMA_ZONE_SMR) != 0) {
4271 bucket->ub_seq = smr_advance(zone->uz_smr);
4272 newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4273 if (newbucket == NULL && bucket != NULL) {
4274 bucket_drain(zone, bucket);
4278 } else if (!bucketdisable)
4279 newbucket = bucket_alloc(zone, udata, M_NOWAIT);
4282 zone_free_bucket(zone, bucket, udata, itemdomain, true);
4285 if ((bucket = newbucket) == NULL)
4287 cache = &zone->uz_cpu[curcpu];
4290 * Check to see if we should be populating the cross bucket. If it
4291 * is already populated we will fall through and attempt to populate
4294 if ((cache_uz_flags(cache) & UMA_ZONE_FIRSTTOUCH) != 0) {
4295 if (PCPU_GET(domain) != itemdomain &&
4296 cache->uc_crossbucket.ucb_bucket == NULL) {
4297 cache_bucket_load_cross(cache, bucket);
4303 * We may have lost the race to fill the bucket or switched CPUs.
4305 if (cache->uc_freebucket.ucb_bucket != NULL) {
4307 bucket_free(zone, bucket, udata);
4310 cache_bucket_load_free(cache, bucket);
4316 uma_zfree_domain(uma_zone_t zone, void *item, void *udata)
4319 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
4320 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
4322 CTR2(KTR_UMA, "uma_zfree_domain zone %s(%p)", zone->uz_name, zone);
4324 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
4325 ("uma_zfree_domain: called with spinlock or critical section held"));
4327 /* uma_zfree(..., NULL) does nothing, to match free(9). */
4330 zone_free_item(zone, item, udata, SKIP_NONE);
4334 slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item)
4341 KEG_LOCK_ASSERT(keg, slab->us_domain);
4343 /* Do we need to remove from any lists? */
4344 dom = &keg->uk_domain[slab->us_domain];
4345 if (slab->us_freecount + 1 == keg->uk_ipers) {
4346 LIST_REMOVE(slab, us_link);
4347 LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
4348 dom->ud_free_slabs++;
4349 } else if (slab->us_freecount == 0) {
4350 LIST_REMOVE(slab, us_link);
4351 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
4354 /* Slab management. */
4355 freei = slab_item_index(slab, keg, item);
4356 BIT_SET(keg->uk_ipers, freei, &slab->us_free);
4357 slab->us_freecount++;
4359 /* Keg statistics. */
4360 dom->ud_free_items++;
4364 zone_release(void *arg, void **bucket, int cnt)
4377 if (__predict_false((zone->uz_flags & UMA_ZFLAG_HASH) != 0))
4378 lock = KEG_LOCK(keg, 0);
4379 for (i = 0; i < cnt; i++) {
4381 if (__predict_true((zone->uz_flags & UMA_ZFLAG_VTOSLAB) != 0)) {
4382 slab = vtoslab((vm_offset_t)item);
4384 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
4385 if ((zone->uz_flags & UMA_ZFLAG_HASH) != 0)
4386 slab = hash_sfind(&keg->uk_hash, mem);
4388 slab = (uma_slab_t)(mem + keg->uk_pgoff);
4390 if (lock != KEG_LOCKPTR(keg, slab->us_domain)) {
4393 lock = KEG_LOCK(keg, slab->us_domain);
4395 slab_free_item(zone, slab, item);
4402 * Frees a single item to any zone.
4405 * zone The zone to free to
4406 * item The item we're freeing
4407 * udata User supplied data for the dtor
4408 * skip Skip dtors and finis
4410 static __noinline void
4411 zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
4415 * If a free is sent directly to an SMR zone we have to
4416 * synchronize immediately because the item can instantly
4417 * be reallocated. This should only happen in degenerate
4418 * cases when no memory is available for per-cpu caches.
4420 if ((zone->uz_flags & UMA_ZONE_SMR) != 0 && skip == SKIP_NONE)
4421 smr_synchronize(zone->uz_smr);
4423 item_dtor(zone, item, zone->uz_size, udata, skip);
4425 if (skip < SKIP_FINI && zone->uz_fini)
4426 zone->uz_fini(item, zone->uz_size);
4428 zone->uz_release(zone->uz_arg, &item, 1);
4430 if (skip & SKIP_CNT)
4433 counter_u64_add(zone->uz_frees, 1);
4435 if (zone->uz_max_items > 0)
4436 zone_free_limit(zone, 1);
4441 uma_zone_set_max(uma_zone_t zone, int nitems)
4443 struct uma_bucket_zone *ubz;
4447 * XXX This can misbehave if the zone has any allocations with
4448 * no limit and a limit is imposed. There is currently no
4449 * way to clear a limit.
4452 ubz = bucket_zone_max(zone, nitems);
4453 count = ubz != NULL ? ubz->ubz_entries : 0;
4454 zone->uz_bucket_size_max = zone->uz_bucket_size = count;
4455 if (zone->uz_bucket_size_min > zone->uz_bucket_size_max)
4456 zone->uz_bucket_size_min = zone->uz_bucket_size_max;
4457 zone->uz_max_items = nitems;
4458 zone->uz_flags |= UMA_ZFLAG_LIMIT;
4459 zone_update_caches(zone);
4460 /* We may need to wake waiters. */
4461 wakeup(&zone->uz_max_items);
4469 uma_zone_set_maxcache(uma_zone_t zone, int nitems)
4471 struct uma_bucket_zone *ubz;
4475 ubz = bucket_zone_max(zone, nitems);
4478 if ((zone->uz_flags & UMA_ZONE_FIRSTTOUCH) != 0)
4479 /* Count the cross-domain bucket. */
4481 nitems -= ubz->ubz_entries * bpcpu * mp_ncpus;
4482 zone->uz_bucket_size_max = ubz->ubz_entries;
4484 zone->uz_bucket_size_max = zone->uz_bucket_size = 0;
4486 if (zone->uz_bucket_size_min > zone->uz_bucket_size_max)
4487 zone->uz_bucket_size_min = zone->uz_bucket_size_max;
4488 zone->uz_bucket_max = nitems / vm_ndomains;
4494 uma_zone_get_max(uma_zone_t zone)
4498 nitems = atomic_load_64(&zone->uz_max_items);
4505 uma_zone_set_warning(uma_zone_t zone, const char *warning)
4508 ZONE_ASSERT_COLD(zone);
4509 zone->uz_warning = warning;
4514 uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
4517 ZONE_ASSERT_COLD(zone);
4518 TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
4523 uma_zone_get_cur(uma_zone_t zone)
4529 if (zone->uz_allocs != EARLY_COUNTER && zone->uz_frees != EARLY_COUNTER)
4530 nitems = counter_u64_fetch(zone->uz_allocs) -
4531 counter_u64_fetch(zone->uz_frees);
4533 nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs) -
4534 atomic_load_64(&zone->uz_cpu[i].uc_frees);
4536 return (nitems < 0 ? 0 : nitems);
4540 uma_zone_get_allocs(uma_zone_t zone)
4546 if (zone->uz_allocs != EARLY_COUNTER)
4547 nitems = counter_u64_fetch(zone->uz_allocs);
4549 nitems += atomic_load_64(&zone->uz_cpu[i].uc_allocs);
4555 uma_zone_get_frees(uma_zone_t zone)
4561 if (zone->uz_frees != EARLY_COUNTER)
4562 nitems = counter_u64_fetch(zone->uz_frees);
4564 nitems += atomic_load_64(&zone->uz_cpu[i].uc_frees);
4570 /* Used only for KEG_ASSERT_COLD(). */
4572 uma_keg_get_allocs(uma_keg_t keg)
4578 LIST_FOREACH(z, &keg->uk_zones, uz_link)
4579 nitems += uma_zone_get_allocs(z);
4587 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
4592 KEG_ASSERT_COLD(keg);
4593 keg->uk_init = uminit;
4598 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
4603 KEG_ASSERT_COLD(keg);
4604 keg->uk_fini = fini;
4609 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
4612 ZONE_ASSERT_COLD(zone);
4613 zone->uz_init = zinit;
4618 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
4621 ZONE_ASSERT_COLD(zone);
4622 zone->uz_fini = zfini;
4627 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
4632 KEG_ASSERT_COLD(keg);
4633 keg->uk_freef = freef;
4638 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
4643 KEG_ASSERT_COLD(keg);
4644 keg->uk_allocf = allocf;
4649 uma_zone_set_smr(uma_zone_t zone, smr_t smr)
4652 ZONE_ASSERT_COLD(zone);
4654 zone->uz_flags |= UMA_ZONE_SMR;
4656 zone_update_caches(zone);
4660 uma_zone_get_smr(uma_zone_t zone)
4663 return (zone->uz_smr);
4668 uma_zone_reserve(uma_zone_t zone, int items)
4673 KEG_ASSERT_COLD(keg);
4674 keg->uk_reserve = items;
4679 uma_zone_reserve_kva(uma_zone_t zone, int count)
4686 KEG_ASSERT_COLD(keg);
4687 ZONE_ASSERT_COLD(zone);
4689 pages = howmany(count, keg->uk_ipers) * keg->uk_ppera;
4691 #ifdef UMA_MD_SMALL_ALLOC
4692 if (keg->uk_ppera > 1) {
4696 kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
4702 MPASS(keg->uk_kva == 0);
4705 zone->uz_max_items = pages * keg->uk_ipers;
4706 #ifdef UMA_MD_SMALL_ALLOC
4707 keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
4709 keg->uk_allocf = noobj_alloc;
4711 keg->uk_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
4712 zone->uz_flags |= UMA_ZFLAG_LIMIT | UMA_ZONE_NOFREE;
4713 zone_update_caches(zone);
4720 uma_prealloc(uma_zone_t zone, int items)
4722 struct vm_domainset_iter di;
4726 int aflags, domain, slabs;
4729 slabs = howmany(items, keg->uk_ipers);
4730 while (slabs-- > 0) {
4732 vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
4735 slab = keg_alloc_slab(keg, zone, domain, M_WAITOK,
4738 dom = &keg->uk_domain[slab->us_domain];
4740 * keg_alloc_slab() always returns a slab on the
4743 LIST_REMOVE(slab, us_link);
4744 LIST_INSERT_HEAD(&dom->ud_free_slab, slab,
4746 dom->ud_free_slabs++;
4747 KEG_UNLOCK(keg, slab->us_domain);
4750 if (vm_domainset_iter_policy(&di, &domain) != 0)
4751 vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
4757 * Returns a snapshot of memory consumption in bytes.
4760 uma_zone_memory(uma_zone_t zone)
4766 if (zone->uz_flags & UMA_ZFLAG_CACHE) {
4767 for (i = 0; i < vm_ndomains; i++)
4768 sz += ZDOM_GET(zone, i)->uzd_nitems;
4769 return (sz * zone->uz_size);
4771 for (i = 0; i < vm_ndomains; i++)
4772 sz += zone->uz_keg->uk_domain[i].ud_pages;
4774 return (sz * PAGE_SIZE);
4779 uma_reclaim(int req)
4782 CTR0(KTR_UMA, "UMA: vm asked us to release pages!");
4783 sx_xlock(&uma_reclaim_lock);
4787 case UMA_RECLAIM_TRIM:
4788 zone_foreach(zone_trim, NULL);
4790 case UMA_RECLAIM_DRAIN:
4791 case UMA_RECLAIM_DRAIN_CPU:
4792 zone_foreach(zone_drain, NULL);
4793 if (req == UMA_RECLAIM_DRAIN_CPU) {
4794 pcpu_cache_drain_safe(NULL);
4795 zone_foreach(zone_drain, NULL);
4799 panic("unhandled reclamation request %d", req);
4803 * Some slabs may have been freed but this zone will be visited early
4804 * we visit again so that we can free pages that are empty once other
4805 * zones are drained. We have to do the same for buckets.
4807 zone_drain(slabzones[0], NULL);
4808 zone_drain(slabzones[1], NULL);
4809 bucket_zone_drain();
4810 sx_xunlock(&uma_reclaim_lock);
4813 static volatile int uma_reclaim_needed;
4816 uma_reclaim_wakeup(void)
4819 if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
4820 wakeup(uma_reclaim);
4824 uma_reclaim_worker(void *arg __unused)
4828 sx_xlock(&uma_reclaim_lock);
4829 while (atomic_load_int(&uma_reclaim_needed) == 0)
4830 sx_sleep(uma_reclaim, &uma_reclaim_lock, PVM, "umarcl",
4832 sx_xunlock(&uma_reclaim_lock);
4833 EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
4834 uma_reclaim(UMA_RECLAIM_DRAIN_CPU);
4835 atomic_store_int(&uma_reclaim_needed, 0);
4836 /* Don't fire more than once per-second. */
4837 pause("umarclslp", hz);
4843 uma_zone_reclaim(uma_zone_t zone, int req)
4847 case UMA_RECLAIM_TRIM:
4848 zone_trim(zone, NULL);
4850 case UMA_RECLAIM_DRAIN:
4851 zone_drain(zone, NULL);
4853 case UMA_RECLAIM_DRAIN_CPU:
4854 pcpu_cache_drain_safe(zone);
4855 zone_drain(zone, NULL);
4858 panic("unhandled reclamation request %d", req);
4864 uma_zone_exhausted(uma_zone_t zone)
4867 return (atomic_load_32(&zone->uz_sleepers) > 0);
4874 return (uma_kmem_limit);
4878 uma_set_limit(unsigned long limit)
4881 uma_kmem_limit = limit;
4888 return (atomic_load_long(&uma_kmem_total));
4895 return (uma_kmem_limit - uma_size());
4900 * Generate statistics across both the zone and its per-cpu cache's. Return
4901 * desired statistics if the pointer is non-NULL for that statistic.
4903 * Note: does not update the zone statistics, as it can't safely clear the
4904 * per-CPU cache statistic.
4908 uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp,
4909 uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp)
4912 uint64_t allocs, frees, sleeps, xdomain;
4915 allocs = frees = sleeps = xdomain = 0;
4918 cache = &z->uz_cpu[cpu];
4919 cachefree += cache->uc_allocbucket.ucb_cnt;
4920 cachefree += cache->uc_freebucket.ucb_cnt;
4921 xdomain += cache->uc_crossbucket.ucb_cnt;
4922 cachefree += cache->uc_crossbucket.ucb_cnt;
4923 allocs += cache->uc_allocs;
4924 frees += cache->uc_frees;
4926 allocs += counter_u64_fetch(z->uz_allocs);
4927 frees += counter_u64_fetch(z->uz_frees);
4928 xdomain += counter_u64_fetch(z->uz_xdomain);
4929 sleeps += z->uz_sleeps;
4930 if (cachefreep != NULL)
4931 *cachefreep = cachefree;
4932 if (allocsp != NULL)
4936 if (sleepsp != NULL)
4938 if (xdomainp != NULL)
4939 *xdomainp = xdomain;
4944 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
4951 rw_rlock(&uma_rwlock);
4952 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4953 LIST_FOREACH(z, &kz->uk_zones, uz_link)
4956 LIST_FOREACH(z, &uma_cachezones, uz_link)
4959 rw_runlock(&uma_rwlock);
4960 return (sysctl_handle_int(oidp, &count, 0, req));
4964 uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf,
4965 struct uma_percpu_stat *ups, bool internal)
4967 uma_zone_domain_t zdom;
4972 for (i = 0; i < vm_ndomains; i++) {
4973 zdom = ZDOM_GET(z, i);
4974 uth->uth_zone_free += zdom->uzd_nitems;
4976 uth->uth_allocs = counter_u64_fetch(z->uz_allocs);
4977 uth->uth_frees = counter_u64_fetch(z->uz_frees);
4978 uth->uth_fails = counter_u64_fetch(z->uz_fails);
4979 uth->uth_xdomain = counter_u64_fetch(z->uz_xdomain);
4980 uth->uth_sleeps = z->uz_sleeps;
4982 for (i = 0; i < mp_maxid + 1; i++) {
4983 bzero(&ups[i], sizeof(*ups));
4984 if (internal || CPU_ABSENT(i))
4986 cache = &z->uz_cpu[i];
4987 ups[i].ups_cache_free += cache->uc_allocbucket.ucb_cnt;
4988 ups[i].ups_cache_free += cache->uc_freebucket.ucb_cnt;
4989 ups[i].ups_cache_free += cache->uc_crossbucket.ucb_cnt;
4990 ups[i].ups_allocs = cache->uc_allocs;
4991 ups[i].ups_frees = cache->uc_frees;
4996 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
4998 struct uma_stream_header ush;
4999 struct uma_type_header uth;
5000 struct uma_percpu_stat *ups;
5005 uint32_t kfree, pages;
5006 int count, error, i;
5008 error = sysctl_wire_old_buffer(req, 0);
5011 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
5012 sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
5013 ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK);
5016 rw_rlock(&uma_rwlock);
5017 LIST_FOREACH(kz, &uma_kegs, uk_link) {
5018 LIST_FOREACH(z, &kz->uk_zones, uz_link)
5022 LIST_FOREACH(z, &uma_cachezones, uz_link)
5026 * Insert stream header.
5028 bzero(&ush, sizeof(ush));
5029 ush.ush_version = UMA_STREAM_VERSION;
5030 ush.ush_maxcpus = (mp_maxid + 1);
5031 ush.ush_count = count;
5032 (void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
5034 LIST_FOREACH(kz, &uma_kegs, uk_link) {
5036 for (i = 0; i < vm_ndomains; i++) {
5037 kfree += kz->uk_domain[i].ud_free_items;
5038 pages += kz->uk_domain[i].ud_pages;
5040 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
5041 bzero(&uth, sizeof(uth));
5042 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
5043 uth.uth_align = kz->uk_align;
5044 uth.uth_size = kz->uk_size;
5045 uth.uth_rsize = kz->uk_rsize;
5046 if (z->uz_max_items > 0) {
5047 items = UZ_ITEMS_COUNT(z->uz_items);
5048 uth.uth_pages = (items / kz->uk_ipers) *
5051 uth.uth_pages = pages;
5052 uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) *
5054 uth.uth_limit = z->uz_max_items;
5055 uth.uth_keg_free = kfree;
5058 * A zone is secondary is it is not the first entry
5059 * on the keg's zone list.
5061 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
5062 (LIST_FIRST(&kz->uk_zones) != z))
5063 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
5064 uma_vm_zone_stats(&uth, z, &sbuf, ups,
5065 kz->uk_flags & UMA_ZFLAG_INTERNAL);
5066 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
5067 for (i = 0; i < mp_maxid + 1; i++)
5068 (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
5071 LIST_FOREACH(z, &uma_cachezones, uz_link) {
5072 bzero(&uth, sizeof(uth));
5073 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
5074 uth.uth_size = z->uz_size;
5075 uma_vm_zone_stats(&uth, z, &sbuf, ups, false);
5076 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
5077 for (i = 0; i < mp_maxid + 1; i++)
5078 (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
5081 rw_runlock(&uma_rwlock);
5082 error = sbuf_finish(&sbuf);
5089 sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
5091 uma_zone_t zone = *(uma_zone_t *)arg1;
5094 max = uma_zone_get_max(zone);
5095 error = sysctl_handle_int(oidp, &max, 0, req);
5096 if (error || !req->newptr)
5099 uma_zone_set_max(zone, max);
5105 sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
5111 * Some callers want to add sysctls for global zones that
5112 * may not yet exist so they pass a pointer to a pointer.
5115 zone = *(uma_zone_t *)arg1;
5118 cur = uma_zone_get_cur(zone);
5119 return (sysctl_handle_int(oidp, &cur, 0, req));
5123 sysctl_handle_uma_zone_allocs(SYSCTL_HANDLER_ARGS)
5125 uma_zone_t zone = arg1;
5128 cur = uma_zone_get_allocs(zone);
5129 return (sysctl_handle_64(oidp, &cur, 0, req));
5133 sysctl_handle_uma_zone_frees(SYSCTL_HANDLER_ARGS)
5135 uma_zone_t zone = arg1;
5138 cur = uma_zone_get_frees(zone);
5139 return (sysctl_handle_64(oidp, &cur, 0, req));
5143 sysctl_handle_uma_zone_flags(SYSCTL_HANDLER_ARGS)
5146 uma_zone_t zone = arg1;
5149 sbuf_new_for_sysctl(&sbuf, NULL, 0, req);
5150 if (zone->uz_flags != 0)
5151 sbuf_printf(&sbuf, "0x%b", zone->uz_flags, PRINT_UMA_ZFLAGS);
5153 sbuf_printf(&sbuf, "0");
5154 error = sbuf_finish(&sbuf);
5161 sysctl_handle_uma_slab_efficiency(SYSCTL_HANDLER_ARGS)
5163 uma_keg_t keg = arg1;
5164 int avail, effpct, total;
5166 total = keg->uk_ppera * PAGE_SIZE;
5167 if ((keg->uk_flags & UMA_ZFLAG_OFFPAGE) != 0)
5168 total += slabzone(keg->uk_ipers)->uz_keg->uk_rsize;
5170 * We consider the client's requested size and alignment here, not the
5171 * real size determination uk_rsize, because we also adjust the real
5172 * size for internal implementation reasons (max bitset size).
5174 avail = keg->uk_ipers * roundup2(keg->uk_size, keg->uk_align + 1);
5175 if ((keg->uk_flags & UMA_ZONE_PCPU) != 0)
5176 avail *= mp_maxid + 1;
5177 effpct = 100 * avail / total;
5178 return (sysctl_handle_int(oidp, &effpct, 0, req));
5182 sysctl_handle_uma_zone_items(SYSCTL_HANDLER_ARGS)
5184 uma_zone_t zone = arg1;
5187 cur = UZ_ITEMS_COUNT(atomic_load_64(&zone->uz_items));
5188 return (sysctl_handle_64(oidp, &cur, 0, req));
5193 uma_dbg_getslab(uma_zone_t zone, void *item)
5200 * It is safe to return the slab here even though the
5201 * zone is unlocked because the item's allocation state
5202 * essentially holds a reference.
5204 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
5205 if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5207 if (zone->uz_flags & UMA_ZFLAG_VTOSLAB)
5208 return (vtoslab((vm_offset_t)mem));
5210 if ((keg->uk_flags & UMA_ZFLAG_HASH) == 0)
5211 return ((uma_slab_t)(mem + keg->uk_pgoff));
5213 slab = hash_sfind(&keg->uk_hash, mem);
5220 uma_dbg_zskip(uma_zone_t zone, void *mem)
5223 if ((zone->uz_flags & UMA_ZFLAG_CACHE) != 0)
5226 return (uma_dbg_kskip(zone->uz_keg, mem));
5230 uma_dbg_kskip(uma_keg_t keg, void *mem)
5234 if (dbg_divisor == 0)
5237 if (dbg_divisor == 1)
5240 idx = (uintptr_t)mem >> PAGE_SHIFT;
5241 if (keg->uk_ipers > 1) {
5242 idx *= keg->uk_ipers;
5243 idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize;
5246 if ((idx / dbg_divisor) * dbg_divisor != idx) {
5247 counter_u64_add(uma_skip_cnt, 1);
5250 counter_u64_add(uma_dbg_cnt, 1);
5256 * Set up the slab's freei data such that uma_dbg_free can function.
5260 uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
5266 slab = uma_dbg_getslab(zone, item);
5268 panic("uma: item %p did not belong to zone %s\n",
5269 item, zone->uz_name);
5272 freei = slab_item_index(slab, keg, item);
5274 if (BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)))
5275 panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
5276 item, zone, zone->uz_name, slab, freei);
5277 BIT_SET_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg));
5281 * Verifies freed addresses. Checks for alignment, valid slab membership
5282 * and duplicate frees.
5286 uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
5292 slab = uma_dbg_getslab(zone, item);
5294 panic("uma: Freed item %p did not belong to zone %s\n",
5295 item, zone->uz_name);
5298 freei = slab_item_index(slab, keg, item);
5300 if (freei >= keg->uk_ipers)
5301 panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
5302 item, zone, zone->uz_name, slab, freei);
5304 if (slab_item(slab, keg, freei) != item)
5305 panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
5306 item, zone, zone->uz_name, slab, freei);
5308 if (!BIT_ISSET(keg->uk_ipers, freei, slab_dbg_bits(slab, keg)))
5309 panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
5310 item, zone, zone->uz_name, slab, freei);
5312 BIT_CLR_ATOMIC(keg->uk_ipers, freei, slab_dbg_bits(slab, keg));
5314 #endif /* INVARIANTS */
5318 get_uma_stats(uma_keg_t kz, uma_zone_t z, uint64_t *allocs, uint64_t *used,
5319 uint64_t *sleeps, long *cachefree, uint64_t *xdomain)
5324 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
5325 *allocs = counter_u64_fetch(z->uz_allocs);
5326 frees = counter_u64_fetch(z->uz_frees);
5327 *sleeps = z->uz_sleeps;
5331 uma_zone_sumstat(z, cachefree, allocs, &frees, sleeps,
5333 for (i = 0; i < vm_ndomains; i++) {
5334 *cachefree += ZDOM_GET(z, i)->uzd_nitems;
5335 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
5336 (LIST_FIRST(&kz->uk_zones) != z)))
5337 *cachefree += kz->uk_domain[i].ud_free_items;
5339 *used = *allocs - frees;
5340 return (((int64_t)*used + *cachefree) * kz->uk_size);
5343 DB_SHOW_COMMAND(uma, db_show_uma)
5345 const char *fmt_hdr, *fmt_entry;
5348 uint64_t allocs, used, sleeps, xdomain;
5350 /* variables for sorting */
5352 uma_zone_t cur_zone, last_zone;
5353 int64_t cur_size, last_size, size;
5356 /* /i option produces machine-parseable CSV output */
5357 if (modif[0] == 'i') {
5358 fmt_hdr = "%s,%s,%s,%s,%s,%s,%s,%s,%s\n";
5359 fmt_entry = "\"%s\",%ju,%jd,%ld,%ju,%ju,%u,%jd,%ju\n";
5361 fmt_hdr = "%18s %6s %7s %7s %11s %7s %7s %10s %8s\n";
5362 fmt_entry = "%18s %6ju %7jd %7ld %11ju %7ju %7u %10jd %8ju\n";
5365 db_printf(fmt_hdr, "Zone", "Size", "Used", "Free", "Requests",
5366 "Sleeps", "Bucket", "Total Mem", "XFree");
5368 /* Sort the zones with largest size first. */
5370 last_size = INT64_MAX;
5375 LIST_FOREACH(kz, &uma_kegs, uk_link) {
5376 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
5378 * In the case of size ties, print out zones
5379 * in the order they are encountered. That is,
5380 * when we encounter the most recently output
5381 * zone, we have already printed all preceding
5382 * ties, and we must print all following ties.
5384 if (z == last_zone) {
5388 size = get_uma_stats(kz, z, &allocs, &used,
5389 &sleeps, &cachefree, &xdomain);
5390 if (size > cur_size && size < last_size + ties)
5398 if (cur_zone == NULL)
5401 size = get_uma_stats(cur_keg, cur_zone, &allocs, &used,
5402 &sleeps, &cachefree, &xdomain);
5403 db_printf(fmt_entry, cur_zone->uz_name,
5404 (uintmax_t)cur_keg->uk_size, (intmax_t)used, cachefree,
5405 (uintmax_t)allocs, (uintmax_t)sleeps,
5406 (unsigned)cur_zone->uz_bucket_size, (intmax_t)size,
5411 last_zone = cur_zone;
5412 last_size = cur_size;
5416 DB_SHOW_COMMAND(umacache, db_show_umacache)
5419 uint64_t allocs, frees;
5423 db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
5424 "Requests", "Bucket");
5425 LIST_FOREACH(z, &uma_cachezones, uz_link) {
5426 uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL);
5427 for (i = 0; i < vm_ndomains; i++)
5428 cachefree += ZDOM_GET(z, i)->uzd_nitems;
5429 db_printf("%18s %8ju %8jd %8ld %12ju %8u\n",
5430 z->uz_name, (uintmax_t)z->uz_size,
5431 (intmax_t)(allocs - frees), cachefree,
5432 (uintmax_t)allocs, z->uz_bucket_size);