2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2005, 2009, 2013 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>
79 #include <sys/taskqueue.h>
80 #include <sys/vmmeter.h>
83 #include <vm/vm_domainset.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_pageout.h>
87 #include <vm/vm_param.h>
88 #include <vm/vm_phys.h>
89 #include <vm/vm_pagequeue.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_extern.h>
94 #include <vm/uma_int.h>
95 #include <vm/uma_dbg.h>
100 #include <vm/memguard.h>
104 * This is the zone and keg from which all zones are spawned.
106 static uma_zone_t kegs;
107 static uma_zone_t zones;
109 /* This is the zone from which all offpage uma_slab_ts are allocated. */
110 static uma_zone_t slabzone;
113 * The initial hash tables come out of this zone so they can be allocated
114 * prior to malloc coming up.
116 static uma_zone_t hashzone;
118 /* The boot-time adjusted value for cache line alignment. */
119 int uma_align_cache = 64 - 1;
121 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
124 * Are we allowed to allocate buckets?
126 static int bucketdisable = 1;
128 /* Linked list of all kegs in the system */
129 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
131 /* Linked list of all cache-only zones in the system */
132 static LIST_HEAD(,uma_zone) uma_cachezones =
133 LIST_HEAD_INITIALIZER(uma_cachezones);
135 /* This RW lock protects the keg list */
136 static struct rwlock_padalign __exclusive_cache_line uma_rwlock;
139 * Pointer and counter to pool of pages, that is preallocated at
140 * startup to bootstrap UMA.
142 static char *bootmem;
143 static int boot_pages;
145 static struct sx uma_drain_lock;
148 * kmem soft limit, initialized by uma_set_limit(). Ensure that early
149 * allocations don't trigger a wakeup of the reclaim thread.
151 static unsigned long uma_kmem_limit = LONG_MAX;
152 SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_limit, CTLFLAG_RD, &uma_kmem_limit, 0,
153 "UMA kernel memory soft limit");
154 static unsigned long uma_kmem_total;
155 SYSCTL_ULONG(_vm, OID_AUTO, uma_kmem_total, CTLFLAG_RD, &uma_kmem_total, 0,
156 "UMA kernel memory usage");
158 /* Is the VM done starting up? */
159 static enum { BOOT_COLD = 0, BOOT_STRAPPED, BOOT_PAGEALLOC, BOOT_BUCKETS,
160 BOOT_RUNNING } booted = BOOT_COLD;
163 * This is the handle used to schedule events that need to happen
164 * outside of the allocation fast path.
166 static struct callout uma_callout;
167 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
170 * This structure is passed as the zone ctor arg so that I don't have to create
171 * a special allocation function just for zones.
173 struct uma_zctor_args {
188 struct uma_kctor_args {
197 struct uma_bucket_zone {
200 int ubz_entries; /* Number of items it can hold. */
201 int ubz_maxsize; /* Maximum allocation size per-item. */
205 * Compute the actual number of bucket entries to pack them in power
206 * of two sizes for more efficient space utilization.
208 #define BUCKET_SIZE(n) \
209 (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *))
211 #define BUCKET_MAX BUCKET_SIZE(256)
212 #define BUCKET_MIN BUCKET_SIZE(4)
214 struct uma_bucket_zone bucket_zones[] = {
215 { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 },
216 { NULL, "6 Bucket", BUCKET_SIZE(6), 3072 },
217 { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 },
218 { NULL, "12 Bucket", BUCKET_SIZE(12), 1536 },
219 { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 },
220 { NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
221 { NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
222 { NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
223 { NULL, "256 Bucket", BUCKET_SIZE(256), 64 },
228 * Flags and enumerations to be passed to internal functions.
232 SKIP_CNT = 0x00000001,
233 SKIP_DTOR = 0x00010000,
234 SKIP_FINI = 0x00020000,
237 #define UMA_ANYDOMAIN -1 /* Special value for domain search. */
241 int uma_startup_count(int);
242 void uma_startup(void *, int);
243 void uma_startup1(void);
244 void uma_startup2(void);
246 static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
247 static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
248 static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
249 static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
250 static void page_free(void *, vm_size_t, uint8_t);
251 static void pcpu_page_free(void *, vm_size_t, uint8_t);
252 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int);
253 static void cache_drain(uma_zone_t);
254 static void bucket_drain(uma_zone_t, uma_bucket_t);
255 static void bucket_cache_drain(uma_zone_t zone);
256 static int keg_ctor(void *, int, void *, int);
257 static void keg_dtor(void *, int, void *);
258 static int zone_ctor(void *, int, void *, int);
259 static void zone_dtor(void *, int, void *);
260 static int zero_init(void *, int, int);
261 static void keg_small_init(uma_keg_t keg);
262 static void keg_large_init(uma_keg_t keg);
263 static void zone_foreach(void (*zfunc)(uma_zone_t));
264 static void zone_timeout(uma_zone_t zone);
265 static int hash_alloc(struct uma_hash *, u_int);
266 static int hash_expand(struct uma_hash *, struct uma_hash *);
267 static void hash_free(struct uma_hash *hash);
268 static void uma_timeout(void *);
269 static void uma_startup3(void);
270 static void *zone_alloc_item(uma_zone_t, void *, int, int);
271 static void *zone_alloc_item_locked(uma_zone_t, void *, int, int);
272 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
273 static void bucket_enable(void);
274 static void bucket_init(void);
275 static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int);
276 static void bucket_free(uma_zone_t zone, uma_bucket_t, void *);
277 static void bucket_zone_drain(void);
278 static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int, int);
279 static uma_slab_t zone_fetch_slab(uma_zone_t, uma_keg_t, int, int);
280 static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
281 static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item);
282 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
283 uma_fini fini, int align, uint32_t flags);
284 static int zone_import(uma_zone_t, void **, int, int, int);
285 static void zone_release(uma_zone_t, void **, int);
286 static void uma_zero_item(void *, uma_zone_t);
288 void uma_print_zone(uma_zone_t);
289 void uma_print_stats(void);
290 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
291 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
294 static bool uma_dbg_kskip(uma_keg_t keg, void *mem);
295 static bool uma_dbg_zskip(uma_zone_t zone, void *mem);
296 static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item);
297 static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item);
299 static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0,
300 "Memory allocation debugging");
302 static u_int dbg_divisor = 1;
303 SYSCTL_UINT(_vm_debug, OID_AUTO, divisor,
304 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0,
305 "Debug & thrash every this item in memory allocator");
307 static counter_u64_t uma_dbg_cnt = EARLY_COUNTER;
308 static counter_u64_t uma_skip_cnt = EARLY_COUNTER;
309 SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD,
310 &uma_dbg_cnt, "memory items debugged");
311 SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD,
312 &uma_skip_cnt, "memory items skipped, not debugged");
315 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
317 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
318 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
320 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
321 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
323 static int zone_warnings = 1;
324 SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0,
325 "Warn when UMA zones becomes full");
327 /* Adjust bytes under management by UMA. */
329 uma_total_dec(unsigned long size)
332 atomic_subtract_long(&uma_kmem_total, size);
336 uma_total_inc(unsigned long size)
339 if (atomic_fetchadd_long(&uma_kmem_total, size) > uma_kmem_limit)
340 uma_reclaim_wakeup();
344 * This routine checks to see whether or not it's safe to enable buckets.
349 bucketdisable = vm_page_count_min();
353 * Initialize bucket_zones, the array of zones of buckets of various sizes.
355 * For each zone, calculate the memory required for each bucket, consisting
356 * of the header and an array of pointers.
361 struct uma_bucket_zone *ubz;
364 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
365 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
366 size += sizeof(void *) * ubz->ubz_entries;
367 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
368 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
369 UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | UMA_ZONE_NUMA);
374 * Given a desired number of entries for a bucket, return the zone from which
375 * to allocate the bucket.
377 static struct uma_bucket_zone *
378 bucket_zone_lookup(int entries)
380 struct uma_bucket_zone *ubz;
382 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
383 if (ubz->ubz_entries >= entries)
390 bucket_select(int size)
392 struct uma_bucket_zone *ubz;
394 ubz = &bucket_zones[0];
395 if (size > ubz->ubz_maxsize)
396 return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
398 for (; ubz->ubz_entries != 0; ubz++)
399 if (ubz->ubz_maxsize < size)
402 return (ubz->ubz_entries);
406 bucket_alloc(uma_zone_t zone, void *udata, int flags)
408 struct uma_bucket_zone *ubz;
412 * This is to stop us from allocating per cpu buckets while we're
413 * running out of vm.boot_pages. Otherwise, we would exhaust the
414 * boot pages. This also prevents us from allocating buckets in
415 * low memory situations.
420 * To limit bucket recursion we store the original zone flags
421 * in a cookie passed via zalloc_arg/zfree_arg. This allows the
422 * NOVM flag to persist even through deep recursions. We also
423 * store ZFLAG_BUCKET once we have recursed attempting to allocate
424 * a bucket for a bucket zone so we do not allow infinite bucket
425 * recursion. This cookie will even persist to frees of unused
426 * buckets via the allocation path or bucket allocations in the
429 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
430 udata = (void *)(uintptr_t)zone->uz_flags;
432 if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
434 udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
436 if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY)
438 ubz = bucket_zone_lookup(zone->uz_count);
439 if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0)
441 bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
444 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
447 bucket->ub_entries = ubz->ubz_entries;
454 bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
456 struct uma_bucket_zone *ubz;
458 KASSERT(bucket->ub_cnt == 0,
459 ("bucket_free: Freeing a non free bucket."));
460 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
461 udata = (void *)(uintptr_t)zone->uz_flags;
462 ubz = bucket_zone_lookup(bucket->ub_entries);
463 uma_zfree_arg(ubz->ubz_zone, bucket, udata);
467 bucket_zone_drain(void)
469 struct uma_bucket_zone *ubz;
471 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
472 zone_drain(ubz->ubz_zone);
476 zone_try_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom, const bool ws)
480 ZONE_LOCK_ASSERT(zone);
482 if ((bucket = LIST_FIRST(&zdom->uzd_buckets)) != NULL) {
483 MPASS(zdom->uzd_nitems >= bucket->ub_cnt);
484 LIST_REMOVE(bucket, ub_link);
485 zdom->uzd_nitems -= bucket->ub_cnt;
486 if (ws && zdom->uzd_imin > zdom->uzd_nitems)
487 zdom->uzd_imin = zdom->uzd_nitems;
488 zone->uz_bkt_count -= bucket->ub_cnt;
494 zone_put_bucket(uma_zone_t zone, uma_zone_domain_t zdom, uma_bucket_t bucket,
498 ZONE_LOCK_ASSERT(zone);
499 KASSERT(zone->uz_bkt_count < zone->uz_bkt_max, ("%s: zone %p overflow",
502 LIST_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link);
503 zdom->uzd_nitems += bucket->ub_cnt;
504 if (ws && zdom->uzd_imax < zdom->uzd_nitems)
505 zdom->uzd_imax = zdom->uzd_nitems;
506 zone->uz_bkt_count += bucket->ub_cnt;
510 zone_log_warning(uma_zone_t zone)
512 static const struct timeval warninterval = { 300, 0 };
514 if (!zone_warnings || zone->uz_warning == NULL)
517 if (ratecheck(&zone->uz_ratecheck, &warninterval))
518 printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
522 zone_maxaction(uma_zone_t zone)
525 if (zone->uz_maxaction.ta_func != NULL)
526 taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction);
530 * Routine called by timeout which is used to fire off some time interval
531 * based calculations. (stats, hash size, etc.)
540 uma_timeout(void *unused)
543 zone_foreach(zone_timeout);
545 /* Reschedule this event */
546 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
550 * Update the working set size estimate for the zone's bucket cache.
551 * The constants chosen here are somewhat arbitrary. With an update period of
552 * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the
556 zone_domain_update_wss(uma_zone_domain_t zdom)
560 MPASS(zdom->uzd_imax >= zdom->uzd_imin);
561 wss = zdom->uzd_imax - zdom->uzd_imin;
562 zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems;
563 zdom->uzd_wss = (3 * wss + 2 * zdom->uzd_wss) / 5;
567 * Routine to perform timeout driven calculations. This expands the
568 * hashes and does per cpu statistics aggregation.
573 zone_timeout(uma_zone_t zone)
575 uma_keg_t keg = zone->uz_keg;
580 * Expand the keg hash table.
582 * This is done if the number of slabs is larger than the hash size.
583 * What I'm trying to do here is completely reduce collisions. This
584 * may be a little aggressive. Should I allow for two collisions max?
586 if (keg->uk_flags & UMA_ZONE_HASH &&
587 (slabs = keg->uk_pages / keg->uk_ppera) >
588 keg->uk_hash.uh_hashsize) {
589 struct uma_hash newhash;
590 struct uma_hash oldhash;
594 * This is so involved because allocating and freeing
595 * while the keg lock is held will lead to deadlock.
596 * I have to do everything in stages and check for
600 ret = hash_alloc(&newhash, 1 << fls(slabs));
603 if (hash_expand(&keg->uk_hash, &newhash)) {
604 oldhash = keg->uk_hash;
605 keg->uk_hash = newhash;
615 for (int i = 0; i < vm_ndomains; i++)
616 zone_domain_update_wss(&zone->uz_domain[i]);
622 * Allocate and zero fill the next sized hash table from the appropriate
626 * hash A new hash structure with the old hash size in uh_hashsize
629 * 1 on success and 0 on failure.
632 hash_alloc(struct uma_hash *hash, u_int size)
636 KASSERT(powerof2(size), ("hash size must be power of 2"));
637 if (size > UMA_HASH_SIZE_INIT) {
638 hash->uh_hashsize = size;
639 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
640 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
641 M_UMAHASH, M_NOWAIT);
643 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
644 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
645 UMA_ANYDOMAIN, M_WAITOK);
646 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
648 if (hash->uh_slab_hash) {
649 bzero(hash->uh_slab_hash, alloc);
650 hash->uh_hashmask = hash->uh_hashsize - 1;
658 * Expands the hash table for HASH zones. This is done from zone_timeout
659 * to reduce collisions. This must not be done in the regular allocation
660 * path, otherwise, we can recurse on the vm while allocating pages.
663 * oldhash The hash you want to expand
664 * newhash The hash structure for the new table
672 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
678 if (!newhash->uh_slab_hash)
681 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
685 * I need to investigate hash algorithms for resizing without a
689 for (idx = 0; idx < oldhash->uh_hashsize; idx++)
690 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[idx])) {
691 slab = SLIST_FIRST(&oldhash->uh_slab_hash[idx]);
692 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[idx], us_hlink);
693 hval = UMA_HASH(newhash, slab->us_data);
694 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
702 * Free the hash bucket to the appropriate backing store.
705 * slab_hash The hash bucket we're freeing
706 * hashsize The number of entries in that hash bucket
712 hash_free(struct uma_hash *hash)
714 if (hash->uh_slab_hash == NULL)
716 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
717 zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
719 free(hash->uh_slab_hash, M_UMAHASH);
723 * Frees all outstanding items in a bucket
726 * zone The zone to free to, must be unlocked.
727 * bucket The free/alloc bucket with items, cpu queue must be locked.
734 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
742 for (i = 0; i < bucket->ub_cnt; i++)
743 zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
744 zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
745 if (zone->uz_max_items > 0) {
747 zone->uz_items -= bucket->ub_cnt;
748 if (zone->uz_sleepers && zone->uz_items < zone->uz_max_items)
756 * Drains the per cpu caches for a zone.
758 * NOTE: This may only be called while the zone is being turn down, and not
759 * during normal operation. This is necessary in order that we do not have
760 * to migrate CPUs to drain the per-CPU caches.
763 * zone The zone to drain, must be unlocked.
769 cache_drain(uma_zone_t zone)
775 * XXX: It is safe to not lock the per-CPU caches, because we're
776 * tearing down the zone anyway. I.e., there will be no further use
777 * of the caches at this point.
779 * XXX: It would good to be able to assert that the zone is being
780 * torn down to prevent improper use of cache_drain().
782 * XXX: We lock the zone before passing into bucket_cache_drain() as
783 * it is used elsewhere. Should the tear-down path be made special
784 * there in some form?
787 cache = &zone->uz_cpu[cpu];
788 bucket_drain(zone, cache->uc_allocbucket);
789 if (cache->uc_allocbucket != NULL)
790 bucket_free(zone, cache->uc_allocbucket, NULL);
791 cache->uc_allocbucket = NULL;
792 bucket_drain(zone, cache->uc_freebucket);
793 if (cache->uc_freebucket != NULL)
794 bucket_free(zone, cache->uc_freebucket, NULL);
795 cache->uc_freebucket = NULL;
796 bucket_drain(zone, cache->uc_crossbucket);
797 if (cache->uc_crossbucket != NULL)
798 bucket_free(zone, cache->uc_crossbucket, NULL);
799 cache->uc_crossbucket = NULL;
802 bucket_cache_drain(zone);
807 cache_shrink(uma_zone_t zone)
810 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
814 zone->uz_count = (zone->uz_count_min + zone->uz_count) / 2;
819 cache_drain_safe_cpu(uma_zone_t zone)
822 uma_bucket_t b1, b2, b3;
825 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
831 if (zone->uz_flags & UMA_ZONE_NUMA)
832 domain = PCPU_GET(domain);
835 cache = &zone->uz_cpu[curcpu];
836 if (cache->uc_allocbucket) {
837 if (cache->uc_allocbucket->ub_cnt != 0)
838 zone_put_bucket(zone, &zone->uz_domain[domain],
839 cache->uc_allocbucket, false);
841 b1 = cache->uc_allocbucket;
842 cache->uc_allocbucket = NULL;
844 if (cache->uc_freebucket) {
845 if (cache->uc_freebucket->ub_cnt != 0)
846 zone_put_bucket(zone, &zone->uz_domain[domain],
847 cache->uc_freebucket, false);
849 b2 = cache->uc_freebucket;
850 cache->uc_freebucket = NULL;
852 b3 = cache->uc_crossbucket;
853 cache->uc_crossbucket = NULL;
857 bucket_free(zone, b1, NULL);
859 bucket_free(zone, b2, NULL);
861 bucket_drain(zone, b3);
862 bucket_free(zone, b3, NULL);
867 * Safely drain per-CPU caches of a zone(s) to alloc bucket.
868 * This is an expensive call because it needs to bind to all CPUs
869 * one by one and enter a critical section on each of them in order
870 * to safely access their cache buckets.
871 * Zone lock must not be held on call this function.
874 cache_drain_safe(uma_zone_t zone)
879 * Polite bucket sizes shrinking was not enouth, shrink aggressively.
884 zone_foreach(cache_shrink);
887 thread_lock(curthread);
888 sched_bind(curthread, cpu);
889 thread_unlock(curthread);
892 cache_drain_safe_cpu(zone);
894 zone_foreach(cache_drain_safe_cpu);
896 thread_lock(curthread);
897 sched_unbind(curthread);
898 thread_unlock(curthread);
902 * Drain the cached buckets from a zone. Expects a locked zone on entry.
905 bucket_cache_drain(uma_zone_t zone)
907 uma_zone_domain_t zdom;
912 * Drain the bucket queues and free the buckets.
914 for (i = 0; i < vm_ndomains; i++) {
915 zdom = &zone->uz_domain[i];
916 while ((bucket = zone_try_fetch_bucket(zone, zdom, false)) !=
919 bucket_drain(zone, bucket);
920 bucket_free(zone, bucket, NULL);
926 * Shrink further bucket sizes. Price of single zone lock collision
927 * is probably lower then price of global cache drain.
929 if (zone->uz_count > zone->uz_count_min)
934 keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
940 CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes",
941 keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera);
944 flags = slab->us_flags;
946 if (keg->uk_fini != NULL) {
947 for (i--; i > -1; i--)
950 * trash_fini implies that dtor was trash_dtor. trash_fini
951 * would check that memory hasn't been modified since free,
952 * which executed trash_dtor.
953 * That's why we need to run uma_dbg_kskip() check here,
954 * albeit we don't make skip check for other init/fini
957 if (!uma_dbg_kskip(keg, slab->us_data + (keg->uk_rsize * i)) ||
958 keg->uk_fini != trash_fini)
960 keg->uk_fini(slab->us_data + (keg->uk_rsize * i),
963 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
964 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
965 keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
966 uma_total_dec(PAGE_SIZE * keg->uk_ppera);
970 * Frees pages from a keg back to the system. This is done on demand from
971 * the pageout daemon.
976 keg_drain(uma_keg_t keg)
978 struct slabhead freeslabs = { 0 };
980 uma_slab_t slab, tmp;
984 * We don't want to take pages from statically allocated kegs at this
987 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
990 CTR3(KTR_UMA, "keg_drain %s(%p) free items: %u",
991 keg->uk_name, keg, keg->uk_free);
993 if (keg->uk_free == 0)
996 for (i = 0; i < vm_ndomains; i++) {
997 dom = &keg->uk_domain[i];
998 LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) {
999 /* We have nowhere to free these to. */
1000 if (slab->us_flags & UMA_SLAB_BOOT)
1003 LIST_REMOVE(slab, us_link);
1004 keg->uk_pages -= keg->uk_ppera;
1005 keg->uk_free -= keg->uk_ipers;
1007 if (keg->uk_flags & UMA_ZONE_HASH)
1008 UMA_HASH_REMOVE(&keg->uk_hash, slab,
1011 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
1018 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
1019 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
1020 keg_free_slab(keg, slab, keg->uk_ipers);
1025 zone_drain_wait(uma_zone_t zone, int waitok)
1029 * Set draining to interlock with zone_dtor() so we can release our
1030 * locks as we go. Only dtor() should do a WAITOK call since it
1031 * is the only call that knows the structure will still be available
1035 while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
1036 if (waitok == M_NOWAIT)
1038 msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1);
1040 zone->uz_flags |= UMA_ZFLAG_DRAINING;
1041 bucket_cache_drain(zone);
1044 * The DRAINING flag protects us from being freed while
1045 * we're running. Normally the uma_rwlock would protect us but we
1046 * must be able to release and acquire the right lock for each keg.
1048 keg_drain(zone->uz_keg);
1050 zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
1057 zone_drain(uma_zone_t zone)
1060 zone_drain_wait(zone, M_NOWAIT);
1064 * Allocate a new slab for a keg. This does not insert the slab onto a list.
1065 * If the allocation was successful, the keg lock will be held upon return,
1066 * otherwise the keg will be left unlocked.
1069 * flags Wait flags for the item initialization routine
1070 * aflags Wait flags for the slab allocation
1073 * The slab that was allocated or NULL if there is no memory and the
1074 * caller specified M_NOWAIT.
1077 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags,
1087 KASSERT(domain >= 0 && domain < vm_ndomains,
1088 ("keg_alloc_slab: domain %d out of range", domain));
1089 KEG_LOCK_ASSERT(keg);
1090 MPASS(zone->uz_lockptr == &keg->uk_lock);
1092 allocf = keg->uk_allocf;
1097 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1098 slab = zone_alloc_item(keg->uk_slabzone, NULL, domain, aflags);
1104 * This reproduces the old vm_zone behavior of zero filling pages the
1105 * first time they are added to a zone.
1107 * Malloced items are zeroed in uma_zalloc.
1110 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1115 if (keg->uk_flags & UMA_ZONE_NODUMP)
1118 /* zone is passed for legacy reasons. */
1119 size = keg->uk_ppera * PAGE_SIZE;
1120 mem = allocf(zone, size, domain, &sflags, aflags);
1122 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1123 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
1127 uma_total_inc(size);
1129 /* Point the slab into the allocated memory */
1130 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
1131 slab = (uma_slab_t )(mem + keg->uk_pgoff);
1133 if (keg->uk_flags & UMA_ZONE_VTOSLAB)
1134 for (i = 0; i < keg->uk_ppera; i++)
1135 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
1138 slab->us_data = mem;
1139 slab->us_freecount = keg->uk_ipers;
1140 slab->us_flags = sflags;
1141 slab->us_domain = domain;
1142 BIT_FILL(SLAB_SETSIZE, &slab->us_free);
1144 BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree);
1147 if (keg->uk_init != NULL) {
1148 for (i = 0; i < keg->uk_ipers; i++)
1149 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
1150 keg->uk_size, flags) != 0)
1152 if (i != keg->uk_ipers) {
1153 keg_free_slab(keg, slab, i);
1160 CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)",
1161 slab, keg->uk_name, keg);
1163 if (keg->uk_flags & UMA_ZONE_HASH)
1164 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
1166 keg->uk_pages += keg->uk_ppera;
1167 keg->uk_free += keg->uk_ipers;
1174 * This function is intended to be used early on in place of page_alloc() so
1175 * that we may use the boot time page cache to satisfy allocations before
1179 startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1188 * If we are in BOOT_BUCKETS or higher, than switch to real
1189 * allocator. Zones with page sized slabs switch at BOOT_PAGEALLOC.
1195 case BOOT_PAGEALLOC:
1196 if (keg->uk_ppera > 1)
1200 #ifdef UMA_MD_SMALL_ALLOC
1201 keg->uk_allocf = (keg->uk_ppera > 1) ?
1202 page_alloc : uma_small_alloc;
1204 keg->uk_allocf = page_alloc;
1206 return keg->uk_allocf(zone, bytes, domain, pflag, wait);
1210 * Check our small startup cache to see if it has pages remaining.
1212 pages = howmany(bytes, PAGE_SIZE);
1213 KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__));
1214 if (pages > boot_pages)
1215 panic("UMA zone \"%s\": Increase vm.boot_pages", zone->uz_name);
1217 printf("%s from \"%s\", %d boot pages left\n", __func__, zone->uz_name,
1221 boot_pages -= pages;
1222 bootmem += pages * PAGE_SIZE;
1223 *pflag = UMA_SLAB_BOOT;
1229 * Allocates a number of pages from the system
1232 * bytes The number of bytes requested
1233 * wait Shall we wait?
1236 * A pointer to the alloced memory or possibly
1237 * NULL if M_NOWAIT is set.
1240 page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1243 void *p; /* Returned page */
1245 *pflag = UMA_SLAB_KERNEL;
1246 p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait);
1252 pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1255 struct pglist alloctail;
1256 vm_offset_t addr, zkva;
1258 vm_page_t p, p_next;
1263 MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE);
1265 TAILQ_INIT(&alloctail);
1266 flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1267 malloc2vm_flags(wait);
1268 *pflag = UMA_SLAB_KERNEL;
1269 for (cpu = 0; cpu <= mp_maxid; cpu++) {
1270 if (CPU_ABSENT(cpu)) {
1271 p = vm_page_alloc(NULL, 0, flags);
1274 p = vm_page_alloc(NULL, 0, flags);
1276 pc = pcpu_find(cpu);
1277 p = vm_page_alloc_domain(NULL, 0, pc->pc_domain, flags);
1278 if (__predict_false(p == NULL))
1279 p = vm_page_alloc(NULL, 0, flags);
1282 if (__predict_false(p == NULL))
1284 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1286 if ((addr = kva_alloc(bytes)) == 0)
1289 TAILQ_FOREACH(p, &alloctail, listq) {
1290 pmap_qenter(zkva, &p, 1);
1293 return ((void*)addr);
1295 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1296 vm_page_unwire_noq(p);
1303 * Allocates a number of pages from within an object
1306 * bytes The number of bytes requested
1307 * wait Shall we wait?
1310 * A pointer to the alloced memory or possibly
1311 * NULL if M_NOWAIT is set.
1314 noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
1317 TAILQ_HEAD(, vm_page) alloctail;
1319 vm_offset_t retkva, zkva;
1320 vm_page_t p, p_next;
1323 TAILQ_INIT(&alloctail);
1326 npages = howmany(bytes, PAGE_SIZE);
1327 while (npages > 0) {
1328 p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT |
1329 VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1330 ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK :
1334 * Since the page does not belong to an object, its
1337 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1342 * Page allocation failed, free intermediate pages and
1345 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1346 vm_page_unwire_noq(p);
1351 *flags = UMA_SLAB_PRIV;
1352 zkva = keg->uk_kva +
1353 atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1355 TAILQ_FOREACH(p, &alloctail, listq) {
1356 pmap_qenter(zkva, &p, 1);
1360 return ((void *)retkva);
1364 * Frees a number of pages to the system
1367 * mem A pointer to the memory to be freed
1368 * size The size of the memory being freed
1369 * flags The original p->us_flags field
1375 page_free(void *mem, vm_size_t size, uint8_t flags)
1378 if ((flags & UMA_SLAB_KERNEL) == 0)
1379 panic("UMA: page_free used with invalid flags %x", flags);
1381 kmem_free((vm_offset_t)mem, size);
1385 * Frees pcpu zone allocations
1388 * mem A pointer to the memory to be freed
1389 * size The size of the memory being freed
1390 * flags The original p->us_flags field
1396 pcpu_page_free(void *mem, vm_size_t size, uint8_t flags)
1398 vm_offset_t sva, curva;
1402 MPASS(size == (mp_maxid+1)*PAGE_SIZE);
1403 sva = (vm_offset_t)mem;
1404 for (curva = sva; curva < sva + size; curva += PAGE_SIZE) {
1405 paddr = pmap_kextract(curva);
1406 m = PHYS_TO_VM_PAGE(paddr);
1407 vm_page_unwire_noq(m);
1410 pmap_qremove(sva, size >> PAGE_SHIFT);
1411 kva_free(sva, size);
1416 * Zero fill initializer
1418 * Arguments/Returns follow uma_init specifications
1421 zero_init(void *mem, int size, int flags)
1428 * Finish creating a small uma keg. This calculates ipers, and the keg size.
1431 * keg The zone we should initialize
1437 keg_small_init(uma_keg_t keg)
1445 if (keg->uk_flags & UMA_ZONE_PCPU) {
1446 u_int ncpus = (mp_maxid + 1) ? (mp_maxid + 1) : MAXCPU;
1448 slabsize = UMA_PCPU_ALLOC_SIZE;
1449 keg->uk_ppera = ncpus;
1451 slabsize = UMA_SLAB_SIZE;
1456 * Calculate the size of each allocation (rsize) according to
1457 * alignment. If the requested size is smaller than we have
1458 * allocation bits for we round it up.
1460 rsize = keg->uk_size;
1461 if (rsize < slabsize / SLAB_SETSIZE)
1462 rsize = slabsize / SLAB_SETSIZE;
1463 if (rsize & keg->uk_align)
1464 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1465 keg->uk_rsize = rsize;
1467 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
1468 keg->uk_rsize < UMA_PCPU_ALLOC_SIZE,
1469 ("%s: size %u too large", __func__, keg->uk_rsize));
1471 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1474 shsize = SIZEOF_UMA_SLAB;
1476 if (rsize <= slabsize - shsize)
1477 keg->uk_ipers = (slabsize - shsize) / rsize;
1479 /* Handle special case when we have 1 item per slab, so
1480 * alignment requirement can be relaxed. */
1481 KASSERT(keg->uk_size <= slabsize - shsize,
1482 ("%s: size %u greater than slab", __func__, keg->uk_size));
1485 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1486 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1488 memused = keg->uk_ipers * rsize + shsize;
1489 wastedspace = slabsize - memused;
1492 * We can't do OFFPAGE if we're internal or if we've been
1493 * asked to not go to the VM for buckets. If we do this we
1494 * may end up going to the VM for slabs which we do not
1495 * want to do if we're UMA_ZFLAG_CACHEONLY as a result
1496 * of UMA_ZONE_VM, which clearly forbids it.
1498 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1499 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1503 * See if using an OFFPAGE slab will limit our waste. Only do
1504 * this if it permits more items per-slab.
1506 * XXX We could try growing slabsize to limit max waste as well.
1507 * Historically this was not done because the VM could not
1508 * efficiently handle contiguous allocations.
1510 if ((wastedspace >= slabsize / UMA_MAX_WASTE) &&
1511 (keg->uk_ipers < (slabsize / keg->uk_rsize))) {
1512 keg->uk_ipers = slabsize / keg->uk_rsize;
1513 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1514 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1515 CTR6(KTR_UMA, "UMA decided we need offpage slab headers for "
1516 "keg: %s(%p), calculated wastedspace = %d, "
1517 "maximum wasted space allowed = %d, "
1518 "calculated ipers = %d, "
1519 "new wasted space = %d\n", keg->uk_name, keg, wastedspace,
1520 slabsize / UMA_MAX_WASTE, keg->uk_ipers,
1521 slabsize - keg->uk_ipers * keg->uk_rsize);
1522 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1525 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1526 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1527 keg->uk_flags |= UMA_ZONE_HASH;
1531 * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do
1532 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1536 * keg The keg we should initialize
1542 keg_large_init(uma_keg_t keg)
1545 KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1546 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1547 ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__));
1549 keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE);
1551 keg->uk_rsize = keg->uk_size;
1553 /* Check whether we have enough space to not do OFFPAGE. */
1554 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0 &&
1555 PAGE_SIZE * keg->uk_ppera - keg->uk_rsize < SIZEOF_UMA_SLAB) {
1557 * We can't do OFFPAGE if we're internal, in which case
1558 * we need an extra page per allocation to contain the
1561 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) == 0)
1562 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1567 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1568 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1569 keg->uk_flags |= UMA_ZONE_HASH;
1573 keg_cachespread_init(uma_keg_t keg)
1580 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1581 ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__));
1583 alignsize = keg->uk_align + 1;
1584 rsize = keg->uk_size;
1586 * We want one item to start on every align boundary in a page. To
1587 * do this we will span pages. We will also extend the item by the
1588 * size of align if it is an even multiple of align. Otherwise, it
1589 * would fall on the same boundary every time.
1591 if (rsize & keg->uk_align)
1592 rsize = (rsize & ~keg->uk_align) + alignsize;
1593 if ((rsize & alignsize) == 0)
1595 trailer = rsize - keg->uk_size;
1596 pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1597 pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1598 keg->uk_rsize = rsize;
1599 keg->uk_ppera = pages;
1600 keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1601 keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1602 KASSERT(keg->uk_ipers <= SLAB_SETSIZE,
1603 ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1608 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1609 * the keg onto the global keg list.
1611 * Arguments/Returns follow uma_ctor specifications
1612 * udata Actually uma_kctor_args
1615 keg_ctor(void *mem, int size, void *udata, int flags)
1617 struct uma_kctor_args *arg = udata;
1618 uma_keg_t keg = mem;
1622 keg->uk_size = arg->size;
1623 keg->uk_init = arg->uminit;
1624 keg->uk_fini = arg->fini;
1625 keg->uk_align = arg->align;
1627 keg->uk_reserve = 0;
1629 keg->uk_flags = arg->flags;
1630 keg->uk_slabzone = NULL;
1633 * We use a global round-robin policy by default. Zones with
1634 * UMA_ZONE_NUMA set will use first-touch instead, in which case the
1635 * iterator is never run.
1637 keg->uk_dr.dr_policy = DOMAINSET_RR();
1638 keg->uk_dr.dr_iter = 0;
1641 * The master zone is passed to us at keg-creation time.
1644 keg->uk_name = zone->uz_name;
1646 if (arg->flags & UMA_ZONE_VM)
1647 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1649 if (arg->flags & UMA_ZONE_ZINIT)
1650 keg->uk_init = zero_init;
1652 if (arg->flags & UMA_ZONE_MALLOC)
1653 keg->uk_flags |= UMA_ZONE_VTOSLAB;
1655 if (arg->flags & UMA_ZONE_PCPU)
1657 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1659 keg->uk_flags &= ~UMA_ZONE_PCPU;
1662 if (keg->uk_flags & UMA_ZONE_CACHESPREAD) {
1663 keg_cachespread_init(keg);
1665 if (keg->uk_size > UMA_SLAB_SPACE)
1666 keg_large_init(keg);
1668 keg_small_init(keg);
1671 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1672 keg->uk_slabzone = slabzone;
1675 * If we haven't booted yet we need allocations to go through the
1676 * startup cache until the vm is ready.
1678 if (booted < BOOT_PAGEALLOC)
1679 keg->uk_allocf = startup_alloc;
1680 #ifdef UMA_MD_SMALL_ALLOC
1681 else if (keg->uk_ppera == 1)
1682 keg->uk_allocf = uma_small_alloc;
1684 else if (keg->uk_flags & UMA_ZONE_PCPU)
1685 keg->uk_allocf = pcpu_page_alloc;
1687 keg->uk_allocf = page_alloc;
1688 #ifdef UMA_MD_SMALL_ALLOC
1689 if (keg->uk_ppera == 1)
1690 keg->uk_freef = uma_small_free;
1693 if (keg->uk_flags & UMA_ZONE_PCPU)
1694 keg->uk_freef = pcpu_page_free;
1696 keg->uk_freef = page_free;
1699 * Initialize keg's lock
1701 KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS));
1704 * If we're putting the slab header in the actual page we need to
1705 * figure out where in each page it goes. See SIZEOF_UMA_SLAB
1708 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1709 keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - SIZEOF_UMA_SLAB;
1711 * The only way the following is possible is if with our
1712 * UMA_ALIGN_PTR adjustments we are now bigger than
1713 * UMA_SLAB_SIZE. I haven't checked whether this is
1714 * mathematically possible for all cases, so we make
1717 KASSERT(keg->uk_pgoff + sizeof(struct uma_slab) <=
1718 PAGE_SIZE * keg->uk_ppera,
1719 ("zone %s ipers %d rsize %d size %d slab won't fit",
1720 zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size));
1723 if (keg->uk_flags & UMA_ZONE_HASH)
1724 hash_alloc(&keg->uk_hash, 0);
1726 CTR5(KTR_UMA, "keg_ctor %p zone %s(%p) out %d free %d\n",
1727 keg, zone->uz_name, zone,
1728 (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
1731 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1733 rw_wlock(&uma_rwlock);
1734 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1735 rw_wunlock(&uma_rwlock);
1740 zone_alloc_counters(uma_zone_t zone)
1743 zone->uz_allocs = counter_u64_alloc(M_WAITOK);
1744 zone->uz_frees = counter_u64_alloc(M_WAITOK);
1745 zone->uz_fails = counter_u64_alloc(M_WAITOK);
1749 * Zone header ctor. This initializes all fields, locks, etc.
1751 * Arguments/Returns follow uma_ctor specifications
1752 * udata Actually uma_zctor_args
1755 zone_ctor(void *mem, int size, void *udata, int flags)
1757 struct uma_zctor_args *arg = udata;
1758 uma_zone_t zone = mem;
1763 zone->uz_name = arg->name;
1764 zone->uz_ctor = arg->ctor;
1765 zone->uz_dtor = arg->dtor;
1766 zone->uz_init = NULL;
1767 zone->uz_fini = NULL;
1768 zone->uz_sleeps = 0;
1769 zone->uz_xdomain = 0;
1771 zone->uz_count_min = 0;
1772 zone->uz_count_max = BUCKET_MAX;
1774 zone->uz_warning = NULL;
1775 /* The domain structures follow the cpu structures. */
1776 zone->uz_domain = (struct uma_zone_domain *)&zone->uz_cpu[mp_ncpus];
1777 zone->uz_bkt_max = ULONG_MAX;
1778 timevalclear(&zone->uz_ratecheck);
1780 if (__predict_true(booted == BOOT_RUNNING))
1781 zone_alloc_counters(zone);
1783 zone->uz_allocs = EARLY_COUNTER;
1784 zone->uz_frees = EARLY_COUNTER;
1785 zone->uz_fails = EARLY_COUNTER;
1789 * This is a pure cache zone, no kegs.
1792 if (arg->flags & UMA_ZONE_VM)
1793 arg->flags |= UMA_ZFLAG_CACHEONLY;
1794 zone->uz_flags = arg->flags;
1795 zone->uz_size = arg->size;
1796 zone->uz_import = arg->import;
1797 zone->uz_release = arg->release;
1798 zone->uz_arg = arg->arg;
1799 zone->uz_lockptr = &zone->uz_lock;
1800 ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
1801 rw_wlock(&uma_rwlock);
1802 LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
1803 rw_wunlock(&uma_rwlock);
1808 * Use the regular zone/keg/slab allocator.
1810 zone->uz_import = (uma_import)zone_import;
1811 zone->uz_release = (uma_release)zone_release;
1812 zone->uz_arg = zone;
1815 if (arg->flags & UMA_ZONE_SECONDARY) {
1816 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1817 zone->uz_init = arg->uminit;
1818 zone->uz_fini = arg->fini;
1819 zone->uz_lockptr = &keg->uk_lock;
1820 zone->uz_flags |= UMA_ZONE_SECONDARY;
1821 rw_wlock(&uma_rwlock);
1823 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1824 if (LIST_NEXT(z, uz_link) == NULL) {
1825 LIST_INSERT_AFTER(z, zone, uz_link);
1830 rw_wunlock(&uma_rwlock);
1831 } else if (keg == NULL) {
1832 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1833 arg->align, arg->flags)) == NULL)
1836 struct uma_kctor_args karg;
1839 /* We should only be here from uma_startup() */
1840 karg.size = arg->size;
1841 karg.uminit = arg->uminit;
1842 karg.fini = arg->fini;
1843 karg.align = arg->align;
1844 karg.flags = arg->flags;
1846 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1853 zone->uz_size = keg->uk_size;
1854 zone->uz_flags |= (keg->uk_flags &
1855 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1858 * Some internal zones don't have room allocated for the per cpu
1859 * caches. If we're internal, bail out here.
1861 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1862 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1863 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1868 KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) !=
1869 (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET),
1870 ("Invalid zone flag combination"));
1871 if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0) {
1872 zone->uz_count = BUCKET_MAX;
1873 } else if ((arg->flags & UMA_ZONE_MINBUCKET) != 0) {
1874 zone->uz_count = BUCKET_MIN;
1875 zone->uz_count_max = BUCKET_MIN;
1876 } else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0)
1879 zone->uz_count = bucket_select(zone->uz_size);
1880 zone->uz_count_min = zone->uz_count;
1886 * Keg header dtor. This frees all data, destroys locks, frees the hash
1887 * table and removes the keg from the global list.
1889 * Arguments/Returns follow uma_dtor specifications
1893 keg_dtor(void *arg, int size, void *udata)
1897 keg = (uma_keg_t)arg;
1899 if (keg->uk_free != 0) {
1900 printf("Freed UMA keg (%s) was not empty (%d items). "
1901 " Lost %d pages of memory.\n",
1902 keg->uk_name ? keg->uk_name : "",
1903 keg->uk_free, keg->uk_pages);
1907 hash_free(&keg->uk_hash);
1915 * Arguments/Returns follow uma_dtor specifications
1919 zone_dtor(void *arg, int size, void *udata)
1924 zone = (uma_zone_t)arg;
1926 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1929 rw_wlock(&uma_rwlock);
1930 LIST_REMOVE(zone, uz_link);
1931 rw_wunlock(&uma_rwlock);
1933 * XXX there are some races here where
1934 * the zone can be drained but zone lock
1935 * released and then refilled before we
1936 * remove it... we dont care for now
1938 zone_drain_wait(zone, M_WAITOK);
1940 * We only destroy kegs from non secondary/non cache zones.
1942 if ((zone->uz_flags & (UMA_ZONE_SECONDARY | UMA_ZFLAG_CACHE)) == 0) {
1944 rw_wlock(&uma_rwlock);
1945 LIST_REMOVE(keg, uk_link);
1946 rw_wunlock(&uma_rwlock);
1947 zone_free_item(kegs, keg, NULL, SKIP_NONE);
1949 counter_u64_free(zone->uz_allocs);
1950 counter_u64_free(zone->uz_frees);
1951 counter_u64_free(zone->uz_fails);
1952 if (zone->uz_lockptr == &zone->uz_lock)
1953 ZONE_LOCK_FINI(zone);
1957 * Traverses every zone in the system and calls a callback
1960 * zfunc A pointer to a function which accepts a zone
1967 zone_foreach(void (*zfunc)(uma_zone_t))
1973 * Before BOOT_RUNNING we are guaranteed to be single
1974 * threaded, so locking isn't needed. Startup functions
1975 * are allowed to use M_WAITOK.
1977 if (__predict_true(booted == BOOT_RUNNING))
1978 rw_rlock(&uma_rwlock);
1979 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1980 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1983 if (__predict_true(booted == BOOT_RUNNING))
1984 rw_runlock(&uma_rwlock);
1988 * Count how many pages do we need to bootstrap. VM supplies
1989 * its need in early zones in the argument, we add up our zones,
1990 * which consist of: UMA Slabs, UMA Hash and 9 Bucket zones. The
1991 * zone of zones and zone of kegs are accounted separately.
1993 #define UMA_BOOT_ZONES 11
1994 /* Zone of zones and zone of kegs have arbitrary alignment. */
1995 #define UMA_BOOT_ALIGN 32
1996 static int zsize, ksize;
1998 uma_startup_count(int vm_zones)
2002 ksize = sizeof(struct uma_keg) +
2003 (sizeof(struct uma_domain) * vm_ndomains);
2004 zsize = sizeof(struct uma_zone) +
2005 (sizeof(struct uma_cache) * (mp_maxid + 1)) +
2006 (sizeof(struct uma_zone_domain) * vm_ndomains);
2009 * Memory for the zone of kegs and its keg,
2010 * and for zone of zones.
2012 pages = howmany(roundup(zsize, CACHE_LINE_SIZE) * 2 +
2013 roundup(ksize, CACHE_LINE_SIZE), PAGE_SIZE);
2015 #ifdef UMA_MD_SMALL_ALLOC
2016 zones = UMA_BOOT_ZONES;
2018 zones = UMA_BOOT_ZONES + vm_zones;
2022 /* Memory for the rest of startup zones, UMA and VM, ... */
2023 if (zsize > UMA_SLAB_SPACE) {
2024 /* See keg_large_init(). */
2027 ppera = howmany(roundup2(zsize, UMA_BOOT_ALIGN), PAGE_SIZE);
2028 if (PAGE_SIZE * ppera - roundup2(zsize, UMA_BOOT_ALIGN) <
2031 pages += (zones + vm_zones) * ppera;
2032 } else if (roundup2(zsize, UMA_BOOT_ALIGN) > UMA_SLAB_SPACE)
2033 /* See keg_small_init() special case for uk_ppera = 1. */
2036 pages += howmany(zones,
2037 UMA_SLAB_SPACE / roundup2(zsize, UMA_BOOT_ALIGN));
2039 /* ... and their kegs. Note that zone of zones allocates a keg! */
2040 pages += howmany(zones + 1,
2041 UMA_SLAB_SPACE / roundup2(ksize, UMA_BOOT_ALIGN));
2044 * Most of startup zones are not going to be offpages, that's
2045 * why we use UMA_SLAB_SPACE instead of UMA_SLAB_SIZE in all
2046 * calculations. Some large bucket zones will be offpage, and
2047 * thus will allocate hashes. We take conservative approach
2048 * and assume that all zones may allocate hash. This may give
2049 * us some positive inaccuracy, usually an extra single page.
2051 pages += howmany(zones, UMA_SLAB_SPACE /
2052 (sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT));
2058 uma_startup(void *mem, int npages)
2060 struct uma_zctor_args args;
2061 uma_keg_t masterkeg;
2065 printf("Entering %s with %d boot pages configured\n", __func__, npages);
2068 rw_init(&uma_rwlock, "UMA lock");
2070 /* Use bootpages memory for the zone of zones and zone of kegs. */
2072 zones = (uma_zone_t)m;
2073 m += roundup(zsize, CACHE_LINE_SIZE);
2074 kegs = (uma_zone_t)m;
2075 m += roundup(zsize, CACHE_LINE_SIZE);
2076 masterkeg = (uma_keg_t)m;
2077 m += roundup(ksize, CACHE_LINE_SIZE);
2078 m = roundup(m, PAGE_SIZE);
2079 npages -= (m - (uintptr_t)mem) / PAGE_SIZE;
2082 /* "manually" create the initial zone */
2083 memset(&args, 0, sizeof(args));
2084 args.name = "UMA Kegs";
2086 args.ctor = keg_ctor;
2087 args.dtor = keg_dtor;
2088 args.uminit = zero_init;
2090 args.keg = masterkeg;
2091 args.align = UMA_BOOT_ALIGN - 1;
2092 args.flags = UMA_ZFLAG_INTERNAL;
2093 zone_ctor(kegs, zsize, &args, M_WAITOK);
2096 boot_pages = npages;
2098 args.name = "UMA Zones";
2100 args.ctor = zone_ctor;
2101 args.dtor = zone_dtor;
2102 args.uminit = zero_init;
2105 args.align = UMA_BOOT_ALIGN - 1;
2106 args.flags = UMA_ZFLAG_INTERNAL;
2107 zone_ctor(zones, zsize, &args, M_WAITOK);
2109 /* Now make a zone for slab headers */
2110 slabzone = uma_zcreate("UMA Slabs",
2111 sizeof(struct uma_slab),
2112 NULL, NULL, NULL, NULL,
2113 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2115 hashzone = uma_zcreate("UMA Hash",
2116 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
2117 NULL, NULL, NULL, NULL,
2118 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2122 booted = BOOT_STRAPPED;
2130 printf("Entering %s with %d boot pages left\n", __func__, boot_pages);
2132 booted = BOOT_PAGEALLOC;
2140 printf("Entering %s with %d boot pages left\n", __func__, boot_pages);
2142 booted = BOOT_BUCKETS;
2143 sx_init(&uma_drain_lock, "umadrain");
2148 * Initialize our callout handle
2156 TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor);
2157 uma_dbg_cnt = counter_u64_alloc(M_WAITOK);
2158 uma_skip_cnt = counter_u64_alloc(M_WAITOK);
2160 zone_foreach(zone_alloc_counters);
2161 callout_init(&uma_callout, 1);
2162 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
2163 booted = BOOT_RUNNING;
2167 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
2168 int align, uint32_t flags)
2170 struct uma_kctor_args args;
2173 args.uminit = uminit;
2175 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
2178 return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK));
2181 /* Public functions */
2184 uma_set_align(int align)
2187 if (align != UMA_ALIGN_CACHE)
2188 uma_align_cache = align;
2193 uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
2194 uma_init uminit, uma_fini fini, int align, uint32_t flags)
2197 struct uma_zctor_args args;
2201 KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
2204 /* Sets all zones to a first-touch domain policy. */
2205 #ifdef UMA_FIRSTTOUCH
2206 flags |= UMA_ZONE_NUMA;
2209 /* This stuff is essential for the zone ctor */
2210 memset(&args, 0, sizeof(args));
2215 args.uminit = uminit;
2219 * If a zone is being created with an empty constructor and
2220 * destructor, pass UMA constructor/destructor which checks for
2221 * memory use after free.
2223 if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOFREE))) &&
2224 ctor == NULL && dtor == NULL && uminit == NULL && fini == NULL) {
2225 args.ctor = trash_ctor;
2226 args.dtor = trash_dtor;
2227 args.uminit = trash_init;
2228 args.fini = trash_fini;
2235 if (booted < BOOT_BUCKETS) {
2238 sx_slock(&uma_drain_lock);
2241 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
2243 sx_sunlock(&uma_drain_lock);
2249 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
2250 uma_init zinit, uma_fini zfini, uma_zone_t master)
2252 struct uma_zctor_args args;
2257 keg = master->uz_keg;
2258 memset(&args, 0, sizeof(args));
2260 args.size = keg->uk_size;
2263 args.uminit = zinit;
2265 args.align = keg->uk_align;
2266 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
2269 if (booted < BOOT_BUCKETS) {
2272 sx_slock(&uma_drain_lock);
2275 /* XXX Attaches only one keg of potentially many. */
2276 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
2278 sx_sunlock(&uma_drain_lock);
2284 uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
2285 uma_init zinit, uma_fini zfini, uma_import zimport,
2286 uma_release zrelease, void *arg, int flags)
2288 struct uma_zctor_args args;
2290 memset(&args, 0, sizeof(args));
2295 args.uminit = zinit;
2297 args.import = zimport;
2298 args.release = zrelease;
2301 args.flags = flags | UMA_ZFLAG_CACHE;
2303 return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK));
2308 uma_zdestroy(uma_zone_t zone)
2311 sx_slock(&uma_drain_lock);
2312 zone_free_item(zones, zone, NULL, SKIP_NONE);
2313 sx_sunlock(&uma_drain_lock);
2317 uma_zwait(uma_zone_t zone)
2321 item = uma_zalloc_arg(zone, NULL, M_WAITOK);
2322 uma_zfree(zone, item);
2326 uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags)
2332 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2334 item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO);
2335 if (item != NULL && (flags & M_ZERO)) {
2337 for (i = 0; i <= mp_maxid; i++)
2338 bzero(zpcpu_get_cpu(item, i), zone->uz_size);
2340 bzero(item, zone->uz_size);
2347 * A stub while both regular and pcpu cases are identical.
2350 uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata)
2354 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2356 uma_zfree_arg(zone, item, udata);
2361 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
2363 uma_zone_domain_t zdom;
2364 uma_bucket_t bucket;
2367 int cpu, domain, lockfail, maxbucket;
2372 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2373 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
2375 /* This is the fast path allocation */
2376 CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d",
2377 curthread, zone->uz_name, zone, flags);
2379 if (flags & M_WAITOK) {
2380 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2381 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
2383 KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC"));
2384 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2385 ("uma_zalloc_arg: called with spinlock or critical section held"));
2386 if (zone->uz_flags & UMA_ZONE_PCPU)
2387 KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone "
2388 "with M_ZERO passed"));
2390 #ifdef DEBUG_MEMGUARD
2391 if (memguard_cmp_zone(zone)) {
2392 item = memguard_alloc(zone->uz_size, flags);
2394 if (zone->uz_init != NULL &&
2395 zone->uz_init(item, zone->uz_size, flags) != 0)
2397 if (zone->uz_ctor != NULL &&
2398 zone->uz_ctor(item, zone->uz_size, udata,
2400 zone->uz_fini(item, zone->uz_size);
2405 /* This is unfortunate but should not be fatal. */
2409 * If possible, allocate from the per-CPU cache. There are two
2410 * requirements for safe access to the per-CPU cache: (1) the thread
2411 * accessing the cache must not be preempted or yield during access,
2412 * and (2) the thread must not migrate CPUs without switching which
2413 * cache it accesses. We rely on a critical section to prevent
2414 * preemption and migration. We release the critical section in
2415 * order to acquire the zone mutex if we are unable to allocate from
2416 * the current cache; when we re-acquire the critical section, we
2417 * must detect and handle migration if it has occurred.
2422 cache = &zone->uz_cpu[cpu];
2425 bucket = cache->uc_allocbucket;
2426 if (bucket != NULL && bucket->ub_cnt > 0) {
2428 item = bucket->ub_bucket[bucket->ub_cnt];
2430 bucket->ub_bucket[bucket->ub_cnt] = NULL;
2432 KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
2436 skipdbg = uma_dbg_zskip(zone, item);
2438 if (zone->uz_ctor != NULL &&
2440 (!skipdbg || zone->uz_ctor != trash_ctor ||
2441 zone->uz_dtor != trash_dtor) &&
2443 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2444 counter_u64_add(zone->uz_fails, 1);
2445 zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT);
2450 uma_dbg_alloc(zone, NULL, item);
2453 uma_zero_item(item, zone);
2458 * We have run out of items in our alloc bucket.
2459 * See if we can switch with our free bucket.
2461 bucket = cache->uc_freebucket;
2462 if (bucket != NULL && bucket->ub_cnt > 0) {
2464 "uma_zalloc: zone %s(%p) swapping empty with alloc",
2465 zone->uz_name, zone);
2466 cache->uc_freebucket = cache->uc_allocbucket;
2467 cache->uc_allocbucket = bucket;
2472 * Discard any empty allocation bucket while we hold no locks.
2474 bucket = cache->uc_allocbucket;
2475 cache->uc_allocbucket = NULL;
2478 bucket_free(zone, bucket, udata);
2480 /* Short-circuit for zones without buckets and low memory. */
2481 if (zone->uz_count == 0 || bucketdisable) {
2483 if (zone->uz_flags & UMA_ZONE_NUMA)
2484 domain = PCPU_GET(domain);
2486 domain = UMA_ANYDOMAIN;
2491 * Attempt to retrieve the item from the per-CPU cache has failed, so
2492 * we must go back to the zone. This requires the zone lock, so we
2493 * must drop the critical section, then re-acquire it when we go back
2494 * to the cache. Since the critical section is released, we may be
2495 * preempted or migrate. As such, make sure not to maintain any
2496 * thread-local state specific to the cache from prior to releasing
2497 * the critical section.
2500 if (ZONE_TRYLOCK(zone) == 0) {
2501 /* Record contention to size the buckets. */
2507 cache = &zone->uz_cpu[cpu];
2509 /* See if we lost the race to fill the cache. */
2510 if (cache->uc_allocbucket != NULL) {
2516 * Check the zone's cache of buckets.
2518 if (zone->uz_flags & UMA_ZONE_NUMA) {
2519 domain = PCPU_GET(domain);
2520 zdom = &zone->uz_domain[domain];
2522 domain = UMA_ANYDOMAIN;
2523 zdom = &zone->uz_domain[0];
2526 if ((bucket = zone_try_fetch_bucket(zone, zdom, true)) != NULL) {
2527 KASSERT(bucket->ub_cnt != 0,
2528 ("uma_zalloc_arg: Returning an empty bucket."));
2529 cache->uc_allocbucket = bucket;
2533 /* We are no longer associated with this CPU. */
2537 * We bump the uz count when the cache size is insufficient to
2538 * handle the working set.
2540 if (lockfail && zone->uz_count < zone->uz_count_max)
2543 if (zone->uz_max_items > 0) {
2544 if (zone->uz_items >= zone->uz_max_items)
2546 maxbucket = MIN(zone->uz_count,
2547 zone->uz_max_items - zone->uz_items);
2548 zone->uz_items += maxbucket;
2550 maxbucket = zone->uz_count;
2554 * Now lets just fill a bucket and put it on the free list. If that
2555 * works we'll restart the allocation from the beginning and it
2556 * will use the just filled bucket.
2558 bucket = zone_alloc_bucket(zone, udata, domain, flags, maxbucket);
2559 CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
2560 zone->uz_name, zone, bucket);
2562 if (bucket != NULL) {
2563 if (zone->uz_max_items > 0 && bucket->ub_cnt < maxbucket) {
2564 MPASS(zone->uz_items >= maxbucket - bucket->ub_cnt);
2565 zone->uz_items -= maxbucket - bucket->ub_cnt;
2566 if (zone->uz_sleepers > 0 &&
2567 zone->uz_items < zone->uz_max_items)
2572 cache = &zone->uz_cpu[cpu];
2575 * See if we lost the race or were migrated. Cache the
2576 * initialized bucket to make this less likely or claim
2577 * the memory directly.
2579 if (cache->uc_allocbucket == NULL &&
2580 ((zone->uz_flags & UMA_ZONE_NUMA) == 0 ||
2581 domain == PCPU_GET(domain))) {
2582 cache->uc_allocbucket = bucket;
2583 zdom->uzd_imax += bucket->ub_cnt;
2584 } else if (zone->uz_bkt_count >= zone->uz_bkt_max) {
2587 bucket_drain(zone, bucket);
2588 bucket_free(zone, bucket, udata);
2589 goto zalloc_restart;
2591 zone_put_bucket(zone, zdom, bucket, false);
2594 } else if (zone->uz_max_items > 0) {
2595 zone->uz_items -= maxbucket;
2596 if (zone->uz_sleepers > 0 &&
2597 zone->uz_items + 1 < zone->uz_max_items)
2602 * We may not be able to get a bucket so return an actual item.
2605 item = zone_alloc_item_locked(zone, udata, domain, flags);
2611 uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
2614 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2615 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
2617 /* This is the fast path allocation */
2619 "uma_zalloc_domain thread %x zone %s(%p) domain %d flags %d",
2620 curthread, zone->uz_name, zone, domain, flags);
2622 if (flags & M_WAITOK) {
2623 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2624 "uma_zalloc_domain: zone \"%s\"", zone->uz_name);
2626 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2627 ("uma_zalloc_domain: called with spinlock or critical section held"));
2629 return (zone_alloc_item(zone, udata, domain, flags));
2633 * Find a slab with some space. Prefer slabs that are partially used over those
2634 * that are totally full. This helps to reduce fragmentation.
2636 * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check
2640 keg_first_slab(uma_keg_t keg, int domain, bool rr)
2646 KASSERT(domain >= 0 && domain < vm_ndomains,
2647 ("keg_first_slab: domain %d out of range", domain));
2648 KEG_LOCK_ASSERT(keg);
2653 dom = &keg->uk_domain[domain];
2654 if (!LIST_EMPTY(&dom->ud_part_slab))
2655 return (LIST_FIRST(&dom->ud_part_slab));
2656 if (!LIST_EMPTY(&dom->ud_free_slab)) {
2657 slab = LIST_FIRST(&dom->ud_free_slab);
2658 LIST_REMOVE(slab, us_link);
2659 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
2663 domain = (domain + 1) % vm_ndomains;
2664 } while (domain != start);
2670 keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags)
2674 KEG_LOCK_ASSERT(keg);
2676 reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve;
2677 if (keg->uk_free <= reserve)
2679 return (keg_first_slab(keg, domain, rr));
2683 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags)
2685 struct vm_domainset_iter di;
2692 KEG_LOCK_ASSERT(keg);
2695 * Use the keg's policy if upper layers haven't already specified a
2696 * domain (as happens with first-touch zones).
2698 * To avoid races we run the iterator with the keg lock held, but that
2699 * means that we cannot allow the vm_domainset layer to sleep. Thus,
2700 * clear M_WAITOK and handle low memory conditions locally.
2702 rr = rdomain == UMA_ANYDOMAIN;
2704 aflags = (flags & ~M_WAITOK) | M_NOWAIT;
2705 vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
2713 slab = keg_fetch_free_slab(keg, domain, rr, flags);
2715 MPASS(slab->us_keg == keg);
2720 * M_NOVM means don't ask at all!
2725 KASSERT(zone->uz_max_items == 0 ||
2726 zone->uz_items <= zone->uz_max_items,
2727 ("%s: zone %p overflow", __func__, zone));
2729 slab = keg_alloc_slab(keg, zone, domain, flags, aflags);
2731 * If we got a slab here it's safe to mark it partially used
2732 * and return. We assume that the caller is going to remove
2733 * at least one item.
2736 MPASS(slab->us_keg == keg);
2737 dom = &keg->uk_domain[slab->us_domain];
2738 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
2742 if (rr && vm_domainset_iter_policy(&di, &domain) != 0) {
2743 if ((flags & M_WAITOK) != 0) {
2745 vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
2754 * We might not have been able to get a slab but another cpu
2755 * could have while we were unlocked. Check again before we
2758 if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) {
2759 MPASS(slab->us_keg == keg);
2766 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int domain, int flags)
2776 slab = keg_fetch_slab(keg, zone, domain, flags);
2779 if (flags & (M_NOWAIT | M_NOVM))
2787 slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
2793 MPASS(keg == slab->us_keg);
2794 KEG_LOCK_ASSERT(keg);
2796 freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2797 BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2798 item = slab->us_data + (keg->uk_rsize * freei);
2799 slab->us_freecount--;
2802 /* Move this slab to the full list */
2803 if (slab->us_freecount == 0) {
2804 LIST_REMOVE(slab, us_link);
2805 dom = &keg->uk_domain[slab->us_domain];
2806 LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link);
2813 zone_import(uma_zone_t zone, void **bucket, int max, int domain, int flags)
2824 /* Try to keep the buckets totally full */
2825 for (i = 0; i < max; ) {
2826 if ((slab = zone_fetch_slab(zone, keg, domain, flags)) == NULL)
2830 stripe = howmany(max, vm_ndomains);
2832 while (slab->us_freecount && i < max) {
2833 bucket[i++] = slab_alloc_item(keg, slab);
2834 if (keg->uk_free <= keg->uk_reserve)
2838 * If the zone is striped we pick a new slab for every
2839 * N allocations. Eliminating this conditional will
2840 * instead pick a new domain for each bucket rather
2841 * than stripe within each bucket. The current option
2842 * produces more fragmentation and requires more cpu
2843 * time but yields better distribution.
2845 if ((zone->uz_flags & UMA_ZONE_NUMA) == 0 &&
2846 vm_ndomains > 1 && --stripe == 0)
2850 /* Don't block if we allocated any successfully. */
2861 zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags, int max)
2863 uma_bucket_t bucket;
2865 CTR1(KTR_UMA, "zone_alloc:_bucket domain %d)", domain);
2867 /* Avoid allocs targeting empty domains. */
2868 if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
2869 domain = UMA_ANYDOMAIN;
2871 /* Don't wait for buckets, preserve caller's NOVM setting. */
2872 bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
2876 bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
2877 MIN(max, bucket->ub_entries), domain, flags);
2880 * Initialize the memory if necessary.
2882 if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
2885 for (i = 0; i < bucket->ub_cnt; i++)
2886 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
2890 * If we couldn't initialize the whole bucket, put the
2891 * rest back onto the freelist.
2893 if (i != bucket->ub_cnt) {
2894 zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
2895 bucket->ub_cnt - i);
2897 bzero(&bucket->ub_bucket[i],
2898 sizeof(void *) * (bucket->ub_cnt - i));
2904 if (bucket->ub_cnt == 0) {
2905 bucket_free(zone, bucket, udata);
2906 counter_u64_add(zone->uz_fails, 1);
2914 * Allocates a single item from a zone.
2917 * zone The zone to alloc for.
2918 * udata The data to be passed to the constructor.
2919 * domain The domain to allocate from or UMA_ANYDOMAIN.
2920 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2923 * NULL if there is no memory and M_NOWAIT is set
2924 * An item if successful
2928 zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags)
2932 return (zone_alloc_item_locked(zone, udata, domain, flags));
2936 * Returns with zone unlocked.
2939 zone_alloc_item_locked(uma_zone_t zone, void *udata, int domain, int flags)
2946 ZONE_LOCK_ASSERT(zone);
2948 if (zone->uz_max_items > 0) {
2949 if (zone->uz_items >= zone->uz_max_items) {
2950 zone_log_warning(zone);
2951 zone_maxaction(zone);
2952 if (flags & M_NOWAIT) {
2957 zone->uz_sleepers++;
2958 while (zone->uz_items >= zone->uz_max_items)
2959 mtx_sleep(zone, zone->uz_lockptr, PVM,
2961 zone->uz_sleepers--;
2962 if (zone->uz_sleepers > 0 &&
2963 zone->uz_items + 1 < zone->uz_max_items)
2970 /* Avoid allocs targeting empty domains. */
2971 if (domain != UMA_ANYDOMAIN && VM_DOMAIN_EMPTY(domain))
2972 domain = UMA_ANYDOMAIN;
2974 if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1)
2978 skipdbg = uma_dbg_zskip(zone, item);
2981 * We have to call both the zone's init (not the keg's init)
2982 * and the zone's ctor. This is because the item is going from
2983 * a keg slab directly to the user, and the user is expecting it
2984 * to be both zone-init'd as well as zone-ctor'd.
2986 if (zone->uz_init != NULL) {
2987 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
2988 zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT);
2992 if (zone->uz_ctor != NULL &&
2994 (!skipdbg || zone->uz_ctor != trash_ctor ||
2995 zone->uz_dtor != trash_dtor) &&
2997 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2998 zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT);
3003 uma_dbg_alloc(zone, NULL, item);
3006 uma_zero_item(item, zone);
3008 counter_u64_add(zone->uz_allocs, 1);
3009 CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
3010 zone->uz_name, zone);
3015 if (zone->uz_max_items > 0) {
3020 counter_u64_add(zone->uz_fails, 1);
3021 CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
3022 zone->uz_name, zone);
3028 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
3031 uma_bucket_t bucket;
3032 uma_zone_domain_t zdom;
3042 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3043 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3045 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
3048 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3049 ("uma_zfree_arg: called with spinlock or critical section held"));
3051 /* uma_zfree(..., NULL) does nothing, to match free(9). */
3054 #ifdef DEBUG_MEMGUARD
3055 if (is_memguard_addr(item)) {
3056 if (zone->uz_dtor != NULL)
3057 zone->uz_dtor(item, zone->uz_size, udata);
3058 if (zone->uz_fini != NULL)
3059 zone->uz_fini(item, zone->uz_size);
3060 memguard_free(item);
3065 skipdbg = uma_dbg_zskip(zone, item);
3066 if (skipdbg == false) {
3067 if (zone->uz_flags & UMA_ZONE_MALLOC)
3068 uma_dbg_free(zone, udata, item);
3070 uma_dbg_free(zone, NULL, item);
3072 if (zone->uz_dtor != NULL && (!skipdbg ||
3073 zone->uz_dtor != trash_dtor || zone->uz_ctor != trash_ctor))
3075 if (zone->uz_dtor != NULL)
3077 zone->uz_dtor(item, zone->uz_size, udata);
3080 * The race here is acceptable. If we miss it we'll just have to wait
3081 * a little longer for the limits to be reset.
3083 if (zone->uz_sleepers > 0)
3087 if ((zone->uz_flags & UMA_ZONE_NUMA) != 0)
3088 itemdomain = _vm_phys_domain(pmap_kextract((vm_offset_t)item));
3092 * If possible, free to the per-CPU cache. There are two
3093 * requirements for safe access to the per-CPU cache: (1) the thread
3094 * accessing the cache must not be preempted or yield during access,
3095 * and (2) the thread must not migrate CPUs without switching which
3096 * cache it accesses. We rely on a critical section to prevent
3097 * preemption and migration. We release the critical section in
3098 * order to acquire the zone mutex if we are unable to free to the
3099 * current cache; when we re-acquire the critical section, we must
3100 * detect and handle migration if it has occurred.
3105 cache = &zone->uz_cpu[cpu];
3108 domain = PCPU_GET(domain);
3110 if ((zone->uz_flags & UMA_ZONE_NUMA) == 0)
3111 itemdomain = domain;
3114 * Try to free into the allocbucket first to give LIFO ordering
3115 * for cache-hot datastructures. Spill over into the freebucket
3116 * if necessary. Alloc will swap them if one runs dry.
3119 if (domain != itemdomain) {
3120 bucket = cache->uc_crossbucket;
3124 bucket = cache->uc_allocbucket;
3125 if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries)
3126 bucket = cache->uc_freebucket;
3128 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
3129 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
3130 ("uma_zfree: Freeing to non free bucket index."));
3131 bucket->ub_bucket[bucket->ub_cnt] = item;
3139 * We must go back the zone, which requires acquiring the zone lock,
3140 * which in turn means we must release and re-acquire the critical
3141 * section. Since the critical section is released, we may be
3142 * preempted or migrate. As such, make sure not to maintain any
3143 * thread-local state specific to the cache from prior to releasing
3144 * the critical section.
3147 if (zone->uz_count == 0 || bucketdisable)
3151 if (ZONE_TRYLOCK(zone) == 0) {
3152 /* Record contention to size the buckets. */
3158 domain = PCPU_GET(domain);
3159 cache = &zone->uz_cpu[cpu];
3162 if (domain != itemdomain)
3163 bucket = cache->uc_crossbucket;
3166 bucket = cache->uc_freebucket;
3167 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
3172 if (domain != itemdomain)
3173 cache->uc_crossbucket = NULL;
3176 cache->uc_freebucket = NULL;
3177 /* We are no longer associated with this CPU. */
3181 if (domain != itemdomain) {
3182 if (bucket != NULL) {
3183 zone->uz_xdomain += bucket->ub_cnt;
3184 if (vm_ndomains > 2 ||
3185 zone->uz_bkt_count >= zone->uz_bkt_max) {
3187 bucket_drain(zone, bucket);
3188 bucket_free(zone, bucket, udata);
3190 zdom = &zone->uz_domain[itemdomain];
3191 zone_put_bucket(zone, zdom, bucket, true);
3196 bucket = bucket_alloc(zone, udata, M_NOWAIT);
3201 cache = &zone->uz_cpu[cpu];
3202 if (cache->uc_crossbucket == NULL) {
3203 cache->uc_crossbucket = bucket;
3207 bucket_free(zone, bucket, udata);
3212 if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) {
3213 zdom = &zone->uz_domain[domain];
3216 zdom = &zone->uz_domain[0];
3219 /* Can we throw this on the zone full list? */
3220 if (bucket != NULL) {
3222 "uma_zfree: zone %s(%p) putting bucket %p on free list",
3223 zone->uz_name, zone, bucket);
3224 /* ub_cnt is pointing to the last free item */
3225 KASSERT(bucket->ub_cnt == bucket->ub_entries,
3226 ("uma_zfree: Attempting to insert not full bucket onto the full list.\n"));
3227 if (zone->uz_bkt_count >= zone->uz_bkt_max) {
3229 bucket_drain(zone, bucket);
3230 bucket_free(zone, bucket, udata);
3233 zone_put_bucket(zone, zdom, bucket, true);
3237 * We bump the uz count when the cache size is insufficient to
3238 * handle the working set.
3240 if (lockfail && zone->uz_count < zone->uz_count_max)
3244 bucket = bucket_alloc(zone, udata, M_NOWAIT);
3245 CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p",
3246 zone->uz_name, zone, bucket);
3250 cache = &zone->uz_cpu[cpu];
3251 if (cache->uc_freebucket == NULL &&
3252 ((zone->uz_flags & UMA_ZONE_NUMA) == 0 ||
3253 domain == PCPU_GET(domain))) {
3254 cache->uc_freebucket = bucket;
3258 * We lost the race, start over. We have to drop our
3259 * critical section to free the bucket.
3262 bucket_free(zone, bucket, udata);
3267 * If nothing else caught this, we'll just do an internal free.
3270 zone_free_item(zone, item, udata, SKIP_DTOR);
3274 uma_zfree_domain(uma_zone_t zone, void *item, void *udata)
3277 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3278 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3280 CTR2(KTR_UMA, "uma_zfree_domain thread %x zone %s", curthread,
3283 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3284 ("uma_zfree_domain: called with spinlock or critical section held"));
3286 /* uma_zfree(..., NULL) does nothing, to match free(9). */
3289 zone_free_item(zone, item, udata, SKIP_NONE);
3293 slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item)
3300 MPASS(zone->uz_lockptr == &keg->uk_lock);
3301 KEG_LOCK_ASSERT(keg);
3302 MPASS(keg == slab->us_keg);
3304 dom = &keg->uk_domain[slab->us_domain];
3306 /* Do we need to remove from any lists? */
3307 if (slab->us_freecount+1 == keg->uk_ipers) {
3308 LIST_REMOVE(slab, us_link);
3309 LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
3310 } else if (slab->us_freecount == 0) {
3311 LIST_REMOVE(slab, us_link);
3312 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
3315 /* Slab management. */
3316 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3317 BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
3318 slab->us_freecount++;
3320 /* Keg statistics. */
3325 zone_release(uma_zone_t zone, void **bucket, int cnt)
3335 for (i = 0; i < cnt; i++) {
3337 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
3338 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
3339 if (zone->uz_flags & UMA_ZONE_HASH) {
3340 slab = hash_sfind(&keg->uk_hash, mem);
3342 mem += keg->uk_pgoff;
3343 slab = (uma_slab_t)mem;
3346 slab = vtoslab((vm_offset_t)item);
3347 MPASS(slab->us_keg == keg);
3349 slab_free_item(zone, slab, item);
3355 * Frees a single item to any zone.
3358 * zone The zone to free to
3359 * item The item we're freeing
3360 * udata User supplied data for the dtor
3361 * skip Skip dtors and finis
3364 zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
3369 skipdbg = uma_dbg_zskip(zone, item);
3370 if (skip == SKIP_NONE && !skipdbg) {
3371 if (zone->uz_flags & UMA_ZONE_MALLOC)
3372 uma_dbg_free(zone, udata, item);
3374 uma_dbg_free(zone, NULL, item);
3377 if (skip < SKIP_DTOR && zone->uz_dtor != NULL &&
3378 (!skipdbg || zone->uz_dtor != trash_dtor ||
3379 zone->uz_ctor != trash_ctor))
3381 if (skip < SKIP_DTOR && zone->uz_dtor != NULL)
3383 zone->uz_dtor(item, zone->uz_size, udata);
3385 if (skip < SKIP_FINI && zone->uz_fini)
3386 zone->uz_fini(item, zone->uz_size);
3388 zone->uz_release(zone->uz_arg, &item, 1);
3390 if (skip & SKIP_CNT)
3393 counter_u64_add(zone->uz_frees, 1);
3395 if (zone->uz_max_items > 0) {
3398 if (zone->uz_sleepers > 0 &&
3399 zone->uz_items < zone->uz_max_items)
3407 uma_zone_set_max(uma_zone_t zone, int nitems)
3409 struct uma_bucket_zone *ubz;
3412 * If limit is very low we may need to limit how
3413 * much items are allowed in CPU caches.
3415 ubz = &bucket_zones[0];
3416 for (; ubz->ubz_entries != 0; ubz++)
3417 if (ubz->ubz_entries * 2 * mp_ncpus > nitems)
3419 if (ubz == &bucket_zones[0])
3420 nitems = ubz->ubz_entries * 2 * mp_ncpus;
3425 zone->uz_count_max = zone->uz_count = ubz->ubz_entries;
3426 if (zone->uz_count_min > zone->uz_count_max)
3427 zone->uz_count_min = zone->uz_count_max;
3428 zone->uz_max_items = nitems;
3436 uma_zone_set_maxcache(uma_zone_t zone, int nitems)
3440 zone->uz_bkt_max = nitems;
3448 uma_zone_get_max(uma_zone_t zone)
3453 nitems = zone->uz_max_items;
3461 uma_zone_set_warning(uma_zone_t zone, const char *warning)
3465 zone->uz_warning = warning;
3471 uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
3475 TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
3481 uma_zone_get_cur(uma_zone_t zone)
3487 nitems = counter_u64_fetch(zone->uz_allocs) -
3488 counter_u64_fetch(zone->uz_frees);
3491 * See the comment in uma_vm_zone_stats() regarding the
3492 * safety of accessing the per-cpu caches. With the zone lock
3493 * held, it is safe, but can potentially result in stale data.
3495 nitems += zone->uz_cpu[i].uc_allocs -
3496 zone->uz_cpu[i].uc_frees;
3500 return (nitems < 0 ? 0 : nitems);
3505 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
3511 KASSERT(keg->uk_pages == 0,
3512 ("uma_zone_set_init on non-empty keg"));
3513 keg->uk_init = uminit;
3519 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
3525 KASSERT(keg->uk_pages == 0,
3526 ("uma_zone_set_fini on non-empty keg"));
3527 keg->uk_fini = fini;
3533 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
3537 KASSERT(zone->uz_keg->uk_pages == 0,
3538 ("uma_zone_set_zinit on non-empty keg"));
3539 zone->uz_init = zinit;
3545 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
3549 KASSERT(zone->uz_keg->uk_pages == 0,
3550 ("uma_zone_set_zfini on non-empty keg"));
3551 zone->uz_fini = zfini;
3556 /* XXX uk_freef is not actually used with the zone locked */
3558 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
3563 KASSERT(keg != NULL, ("uma_zone_set_freef: Invalid zone type"));
3565 keg->uk_freef = freef;
3570 /* XXX uk_allocf is not actually used with the zone locked */
3572 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
3578 keg->uk_allocf = allocf;
3584 uma_zone_reserve(uma_zone_t zone, int items)
3590 keg->uk_reserve = items;
3596 uma_zone_reserve_kva(uma_zone_t zone, int count)
3604 pages = count / keg->uk_ipers;
3605 if (pages * keg->uk_ipers < count)
3607 pages *= keg->uk_ppera;
3609 #ifdef UMA_MD_SMALL_ALLOC
3610 if (keg->uk_ppera > 1) {
3614 kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
3621 MPASS(keg->uk_kva == 0);
3624 zone->uz_max_items = pages * keg->uk_ipers;
3625 #ifdef UMA_MD_SMALL_ALLOC
3626 keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
3628 keg->uk_allocf = noobj_alloc;
3630 keg->uk_flags |= UMA_ZONE_NOFREE;
3638 uma_prealloc(uma_zone_t zone, int items)
3640 struct vm_domainset_iter di;
3644 int aflags, domain, slabs;
3648 slabs = items / keg->uk_ipers;
3649 if (slabs * keg->uk_ipers < items)
3651 while (slabs-- > 0) {
3653 vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
3656 slab = keg_alloc_slab(keg, zone, domain, M_WAITOK,
3659 MPASS(slab->us_keg == keg);
3660 dom = &keg->uk_domain[slab->us_domain];
3661 LIST_INSERT_HEAD(&dom->ud_free_slab, slab,
3666 if (vm_domainset_iter_policy(&di, &domain) != 0) {
3668 vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
3678 uma_reclaim_locked(bool kmem_danger)
3681 CTR0(KTR_UMA, "UMA: vm asked us to release pages!");
3682 sx_assert(&uma_drain_lock, SA_XLOCKED);
3684 zone_foreach(zone_drain);
3685 if (vm_page_count_min() || kmem_danger) {
3686 cache_drain_safe(NULL);
3687 zone_foreach(zone_drain);
3691 * Some slabs may have been freed but this zone will be visited early
3692 * we visit again so that we can free pages that are empty once other
3693 * zones are drained. We have to do the same for buckets.
3695 zone_drain(slabzone);
3696 bucket_zone_drain();
3703 sx_xlock(&uma_drain_lock);
3704 uma_reclaim_locked(false);
3705 sx_xunlock(&uma_drain_lock);
3708 static volatile int uma_reclaim_needed;
3711 uma_reclaim_wakeup(void)
3714 if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
3715 wakeup(uma_reclaim);
3719 uma_reclaim_worker(void *arg __unused)
3723 sx_xlock(&uma_drain_lock);
3724 while (atomic_load_int(&uma_reclaim_needed) == 0)
3725 sx_sleep(uma_reclaim, &uma_drain_lock, PVM, "umarcl",
3727 sx_xunlock(&uma_drain_lock);
3728 EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
3729 sx_xlock(&uma_drain_lock);
3730 uma_reclaim_locked(true);
3731 atomic_store_int(&uma_reclaim_needed, 0);
3732 sx_xunlock(&uma_drain_lock);
3733 /* Don't fire more than once per-second. */
3734 pause("umarclslp", hz);
3740 uma_zone_exhausted(uma_zone_t zone)
3745 full = zone->uz_sleepers > 0;
3751 uma_zone_exhausted_nolock(uma_zone_t zone)
3753 return (zone->uz_sleepers > 0);
3757 uma_large_malloc_domain(vm_size_t size, int domain, int wait)
3759 struct domainset *policy;
3763 if (domain != UMA_ANYDOMAIN) {
3764 /* avoid allocs targeting empty domains */
3765 if (VM_DOMAIN_EMPTY(domain))
3766 domain = UMA_ANYDOMAIN;
3768 slab = zone_alloc_item(slabzone, NULL, domain, wait);
3771 policy = (domain == UMA_ANYDOMAIN) ? DOMAINSET_RR() :
3772 DOMAINSET_FIXED(domain);
3773 addr = kmem_malloc_domainset(policy, size, wait);
3775 vsetslab(addr, slab);
3776 slab->us_data = (void *)addr;
3777 slab->us_flags = UMA_SLAB_KERNEL | UMA_SLAB_MALLOC;
3778 slab->us_size = size;
3779 slab->us_domain = vm_phys_domain(PHYS_TO_VM_PAGE(
3780 pmap_kextract(addr)));
3781 uma_total_inc(size);
3783 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3786 return ((void *)addr);
3790 uma_large_malloc(vm_size_t size, int wait)
3793 return uma_large_malloc_domain(size, UMA_ANYDOMAIN, wait);
3797 uma_large_free(uma_slab_t slab)
3800 KASSERT((slab->us_flags & UMA_SLAB_KERNEL) != 0,
3801 ("uma_large_free: Memory not allocated with uma_large_malloc."));
3802 kmem_free((vm_offset_t)slab->us_data, slab->us_size);
3803 uma_total_dec(slab->us_size);
3804 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3808 uma_zero_item(void *item, uma_zone_t zone)
3811 bzero(item, zone->uz_size);
3818 return (uma_kmem_limit);
3822 uma_set_limit(unsigned long limit)
3825 uma_kmem_limit = limit;
3832 return (atomic_load_long(&uma_kmem_total));
3839 return (uma_kmem_limit - uma_size());
3843 uma_print_stats(void)
3845 zone_foreach(uma_print_zone);
3849 slab_print(uma_slab_t slab)
3851 printf("slab: keg %p, data %p, freecount %d\n",
3852 slab->us_keg, slab->us_data, slab->us_freecount);
3856 cache_print(uma_cache_t cache)
3858 printf("alloc: %p(%d), free: %p(%d), cross: %p(%d)j\n",
3859 cache->uc_allocbucket,
3860 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3861 cache->uc_freebucket,
3862 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0,
3863 cache->uc_crossbucket,
3864 cache->uc_crossbucket?cache->uc_crossbucket->ub_cnt:0);
3868 uma_print_keg(uma_keg_t keg)
3874 printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3876 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3877 keg->uk_ipers, keg->uk_ppera,
3878 (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
3880 for (i = 0; i < vm_ndomains; i++) {
3881 dom = &keg->uk_domain[i];
3882 printf("Part slabs:\n");
3883 LIST_FOREACH(slab, &dom->ud_part_slab, us_link)
3885 printf("Free slabs:\n");
3886 LIST_FOREACH(slab, &dom->ud_free_slab, us_link)
3888 printf("Full slabs:\n");
3889 LIST_FOREACH(slab, &dom->ud_full_slab, us_link)
3895 uma_print_zone(uma_zone_t zone)
3900 printf("zone: %s(%p) size %d maxitems %ju flags %#x\n",
3901 zone->uz_name, zone, zone->uz_size, (uintmax_t)zone->uz_max_items,
3903 if (zone->uz_lockptr != &zone->uz_lock)
3904 uma_print_keg(zone->uz_keg);
3906 cache = &zone->uz_cpu[i];
3907 printf("CPU %d Cache:\n", i);
3914 * Generate statistics across both the zone and its per-cpu cache's. Return
3915 * desired statistics if the pointer is non-NULL for that statistic.
3917 * Note: does not update the zone statistics, as it can't safely clear the
3918 * per-CPU cache statistic.
3920 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3921 * safe from off-CPU; we should modify the caches to track this information
3922 * directly so that we don't have to.
3925 uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp,
3926 uint64_t *freesp, uint64_t *sleepsp, uint64_t *xdomainp)
3929 uint64_t allocs, frees, sleeps, xdomain;
3932 allocs = frees = sleeps = xdomain = 0;
3935 cache = &z->uz_cpu[cpu];
3936 if (cache->uc_allocbucket != NULL)
3937 cachefree += cache->uc_allocbucket->ub_cnt;
3938 if (cache->uc_freebucket != NULL)
3939 cachefree += cache->uc_freebucket->ub_cnt;
3940 if (cache->uc_crossbucket != NULL) {
3941 xdomain += cache->uc_crossbucket->ub_cnt;
3942 cachefree += cache->uc_crossbucket->ub_cnt;
3944 allocs += cache->uc_allocs;
3945 frees += cache->uc_frees;
3947 allocs += counter_u64_fetch(z->uz_allocs);
3948 frees += counter_u64_fetch(z->uz_frees);
3949 sleeps += z->uz_sleeps;
3950 xdomain += z->uz_xdomain;
3951 if (cachefreep != NULL)
3952 *cachefreep = cachefree;
3953 if (allocsp != NULL)
3957 if (sleepsp != NULL)
3959 if (xdomainp != NULL)
3960 *xdomainp = xdomain;
3965 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
3972 rw_rlock(&uma_rwlock);
3973 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3974 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3977 LIST_FOREACH(z, &uma_cachezones, uz_link)
3980 rw_runlock(&uma_rwlock);
3981 return (sysctl_handle_int(oidp, &count, 0, req));
3985 uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf,
3986 struct uma_percpu_stat *ups, bool internal)
3988 uma_zone_domain_t zdom;
3993 for (i = 0; i < vm_ndomains; i++) {
3994 zdom = &z->uz_domain[i];
3995 uth->uth_zone_free += zdom->uzd_nitems;
3997 uth->uth_allocs = counter_u64_fetch(z->uz_allocs);
3998 uth->uth_frees = counter_u64_fetch(z->uz_frees);
3999 uth->uth_fails = counter_u64_fetch(z->uz_fails);
4000 uth->uth_sleeps = z->uz_sleeps;
4001 uth->uth_xdomain = z->uz_xdomain;
4003 * While it is not normally safe to access the cache
4004 * bucket pointers while not on the CPU that owns the
4005 * cache, we only allow the pointers to be exchanged
4006 * without the zone lock held, not invalidated, so
4007 * accept the possible race associated with bucket
4008 * exchange during monitoring.
4010 for (i = 0; i < mp_maxid + 1; i++) {
4011 bzero(&ups[i], sizeof(*ups));
4012 if (internal || CPU_ABSENT(i))
4014 cache = &z->uz_cpu[i];
4015 if (cache->uc_allocbucket != NULL)
4016 ups[i].ups_cache_free +=
4017 cache->uc_allocbucket->ub_cnt;
4018 if (cache->uc_freebucket != NULL)
4019 ups[i].ups_cache_free +=
4020 cache->uc_freebucket->ub_cnt;
4021 if (cache->uc_crossbucket != NULL)
4022 ups[i].ups_cache_free +=
4023 cache->uc_crossbucket->ub_cnt;
4024 ups[i].ups_allocs = cache->uc_allocs;
4025 ups[i].ups_frees = cache->uc_frees;
4030 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
4032 struct uma_stream_header ush;
4033 struct uma_type_header uth;
4034 struct uma_percpu_stat *ups;
4038 int count, error, i;
4040 error = sysctl_wire_old_buffer(req, 0);
4043 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
4044 sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
4045 ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK);
4048 rw_rlock(&uma_rwlock);
4049 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4050 LIST_FOREACH(z, &kz->uk_zones, uz_link)
4054 LIST_FOREACH(z, &uma_cachezones, uz_link)
4058 * Insert stream header.
4060 bzero(&ush, sizeof(ush));
4061 ush.ush_version = UMA_STREAM_VERSION;
4062 ush.ush_maxcpus = (mp_maxid + 1);
4063 ush.ush_count = count;
4064 (void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
4066 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4067 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
4068 bzero(&uth, sizeof(uth));
4070 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
4071 uth.uth_align = kz->uk_align;
4072 uth.uth_size = kz->uk_size;
4073 uth.uth_rsize = kz->uk_rsize;
4074 if (z->uz_max_items > 0)
4075 uth.uth_pages = (z->uz_items / kz->uk_ipers) *
4078 uth.uth_pages = kz->uk_pages;
4079 uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) *
4081 uth.uth_limit = z->uz_max_items;
4082 uth.uth_keg_free = z->uz_keg->uk_free;
4085 * A zone is secondary is it is not the first entry
4086 * on the keg's zone list.
4088 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
4089 (LIST_FIRST(&kz->uk_zones) != z))
4090 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
4091 uma_vm_zone_stats(&uth, z, &sbuf, ups,
4092 kz->uk_flags & UMA_ZFLAG_INTERNAL);
4094 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
4095 for (i = 0; i < mp_maxid + 1; i++)
4096 (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
4099 LIST_FOREACH(z, &uma_cachezones, uz_link) {
4100 bzero(&uth, sizeof(uth));
4102 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
4103 uth.uth_size = z->uz_size;
4104 uma_vm_zone_stats(&uth, z, &sbuf, ups, false);
4106 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
4107 for (i = 0; i < mp_maxid + 1; i++)
4108 (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
4111 rw_runlock(&uma_rwlock);
4112 error = sbuf_finish(&sbuf);
4119 sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
4121 uma_zone_t zone = *(uma_zone_t *)arg1;
4124 max = uma_zone_get_max(zone);
4125 error = sysctl_handle_int(oidp, &max, 0, req);
4126 if (error || !req->newptr)
4129 uma_zone_set_max(zone, max);
4135 sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
4137 uma_zone_t zone = *(uma_zone_t *)arg1;
4140 cur = uma_zone_get_cur(zone);
4141 return (sysctl_handle_int(oidp, &cur, 0, req));
4146 uma_dbg_getslab(uma_zone_t zone, void *item)
4152 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
4153 if (zone->uz_flags & UMA_ZONE_VTOSLAB) {
4154 slab = vtoslab((vm_offset_t)mem);
4157 * It is safe to return the slab here even though the
4158 * zone is unlocked because the item's allocation state
4159 * essentially holds a reference.
4161 if (zone->uz_lockptr == &zone->uz_lock)
4165 if (keg->uk_flags & UMA_ZONE_HASH)
4166 slab = hash_sfind(&keg->uk_hash, mem);
4168 slab = (uma_slab_t)(mem + keg->uk_pgoff);
4176 uma_dbg_zskip(uma_zone_t zone, void *mem)
4179 if (zone->uz_lockptr == &zone->uz_lock)
4182 return (uma_dbg_kskip(zone->uz_keg, mem));
4186 uma_dbg_kskip(uma_keg_t keg, void *mem)
4190 if (dbg_divisor == 0)
4193 if (dbg_divisor == 1)
4196 idx = (uintptr_t)mem >> PAGE_SHIFT;
4197 if (keg->uk_ipers > 1) {
4198 idx *= keg->uk_ipers;
4199 idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize;
4202 if ((idx / dbg_divisor) * dbg_divisor != idx) {
4203 counter_u64_add(uma_skip_cnt, 1);
4206 counter_u64_add(uma_dbg_cnt, 1);
4212 * Set up the slab's freei data such that uma_dbg_free can function.
4216 uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
4222 slab = uma_dbg_getslab(zone, item);
4224 panic("uma: item %p did not belong to zone %s\n",
4225 item, zone->uz_name);
4228 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
4230 if (BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
4231 panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
4232 item, zone, zone->uz_name, slab, freei);
4233 BIT_SET_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
4239 * Verifies freed addresses. Checks for alignment, valid slab membership
4240 * and duplicate frees.
4244 uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
4250 slab = uma_dbg_getslab(zone, item);
4252 panic("uma: Freed item %p did not belong to zone %s\n",
4253 item, zone->uz_name);
4256 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
4258 if (freei >= keg->uk_ipers)
4259 panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
4260 item, zone, zone->uz_name, slab, freei);
4262 if (((freei * keg->uk_rsize) + slab->us_data) != item)
4263 panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
4264 item, zone, zone->uz_name, slab, freei);
4266 if (!BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
4267 panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
4268 item, zone, zone->uz_name, slab, freei);
4270 BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
4272 #endif /* INVARIANTS */
4275 DB_SHOW_COMMAND(uma, db_show_uma)
4279 uint64_t allocs, frees, sleeps, xdomain;
4283 db_printf("%18s %8s %8s %8s %12s %8s %8s %8s\n", "Zone", "Size", "Used",
4284 "Free", "Requests", "Sleeps", "Bucket", "XFree");
4285 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4286 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
4287 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
4288 allocs = counter_u64_fetch(z->uz_allocs);
4289 frees = counter_u64_fetch(z->uz_frees);
4290 sleeps = z->uz_sleeps;
4293 uma_zone_sumstat(z, &cachefree, &allocs,
4294 &frees, &sleeps, &xdomain);
4295 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
4296 (LIST_FIRST(&kz->uk_zones) != z)))
4297 cachefree += kz->uk_free;
4298 for (i = 0; i < vm_ndomains; i++)
4299 cachefree += z->uz_domain[i].uzd_nitems;
4301 db_printf("%18s %8ju %8jd %8ld %12ju %8ju %8u %8ju\n",
4302 z->uz_name, (uintmax_t)kz->uk_size,
4303 (intmax_t)(allocs - frees), cachefree,
4304 (uintmax_t)allocs, sleeps, z->uz_count, xdomain);
4311 DB_SHOW_COMMAND(umacache, db_show_umacache)
4314 uint64_t allocs, frees;
4318 db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
4319 "Requests", "Bucket");
4320 LIST_FOREACH(z, &uma_cachezones, uz_link) {
4321 uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL, NULL);
4322 for (i = 0; i < vm_ndomains; i++)
4323 cachefree += z->uz_domain[i].uzd_nitems;
4324 db_printf("%18s %8ju %8jd %8ld %12ju %8u\n",
4325 z->uz_name, (uintmax_t)z->uz_size,
4326 (intmax_t)(allocs - frees), cachefree,
4327 (uintmax_t)allocs, z->uz_count);