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? */
166 } booted = BOOT_COLD;
169 * This is the handle used to schedule events that need to happen
170 * outside of the allocation fast path.
172 static struct callout uma_callout;
173 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
176 * This structure is passed as the zone ctor arg so that I don't have to create
177 * a special allocation function just for zones.
179 struct uma_zctor_args {
194 struct uma_kctor_args {
203 struct uma_bucket_zone {
206 int ubz_entries; /* Number of items it can hold. */
207 int ubz_maxsize; /* Maximum allocation size per-item. */
211 * Compute the actual number of bucket entries to pack them in power
212 * of two sizes for more efficient space utilization.
214 #define BUCKET_SIZE(n) \
215 (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *))
217 #define BUCKET_MAX BUCKET_SIZE(256)
219 struct uma_bucket_zone bucket_zones[] = {
220 { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 },
221 { NULL, "6 Bucket", BUCKET_SIZE(6), 3072 },
222 { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 },
223 { NULL, "12 Bucket", BUCKET_SIZE(12), 1536 },
224 { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 },
225 { NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
226 { NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
227 { NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
228 { NULL, "256 Bucket", BUCKET_SIZE(256), 64 },
233 * Flags and enumerations to be passed to internal functions.
235 enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI };
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 uma_shutdown(void);
271 static void *zone_alloc_item(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);
279 static uma_slab_t zone_fetch_slab(uma_zone_t, uma_keg_t, int, int);
280 static uma_slab_t zone_fetch_slab_multi(uma_zone_t, uma_keg_t, int, int);
281 static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
282 static void slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item);
283 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
284 uma_fini fini, int align, uint32_t flags);
285 static int zone_import(uma_zone_t, void **, int, int, int);
286 static void zone_release(uma_zone_t, void **, int);
287 static void uma_zero_item(void *, uma_zone_t);
289 void uma_print_zone(uma_zone_t);
290 void uma_print_stats(void);
291 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
292 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
295 static bool uma_dbg_kskip(uma_keg_t keg, void *mem);
296 static bool uma_dbg_zskip(uma_zone_t zone, void *mem);
297 static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item);
298 static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item);
300 static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0,
301 "Memory allocation debugging");
303 static u_int dbg_divisor = 1;
304 SYSCTL_UINT(_vm_debug, OID_AUTO, divisor,
305 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0,
306 "Debug & thrash every this item in memory allocator");
308 static counter_u64_t uma_dbg_cnt = EARLY_COUNTER;
309 static counter_u64_t uma_skip_cnt = EARLY_COUNTER;
310 SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD,
311 &uma_dbg_cnt, "memory items debugged");
312 SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD,
313 &uma_skip_cnt, "memory items skipped, not debugged");
316 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
318 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
319 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
321 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
322 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
324 static int zone_warnings = 1;
325 SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0,
326 "Warn when UMA zones becomes full");
328 /* Adjust bytes under management by UMA. */
330 uma_total_dec(unsigned long size)
333 atomic_subtract_long(&uma_kmem_total, size);
337 uma_total_inc(unsigned long size)
340 if (atomic_fetchadd_long(&uma_kmem_total, size) > uma_kmem_limit)
341 uma_reclaim_wakeup();
345 * This routine checks to see whether or not it's safe to enable buckets.
350 bucketdisable = vm_page_count_min();
354 * Initialize bucket_zones, the array of zones of buckets of various sizes.
356 * For each zone, calculate the memory required for each bucket, consisting
357 * of the header and an array of pointers.
362 struct uma_bucket_zone *ubz;
365 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
366 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
367 size += sizeof(void *) * ubz->ubz_entries;
368 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
369 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
370 UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | UMA_ZONE_NUMA);
375 * Given a desired number of entries for a bucket, return the zone from which
376 * to allocate the bucket.
378 static struct uma_bucket_zone *
379 bucket_zone_lookup(int entries)
381 struct uma_bucket_zone *ubz;
383 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
384 if (ubz->ubz_entries >= entries)
391 bucket_select(int size)
393 struct uma_bucket_zone *ubz;
395 ubz = &bucket_zones[0];
396 if (size > ubz->ubz_maxsize)
397 return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
399 for (; ubz->ubz_entries != 0; ubz++)
400 if (ubz->ubz_maxsize < size)
403 return (ubz->ubz_entries);
407 bucket_alloc(uma_zone_t zone, void *udata, int flags)
409 struct uma_bucket_zone *ubz;
413 * This is to stop us from allocating per cpu buckets while we're
414 * running out of vm.boot_pages. Otherwise, we would exhaust the
415 * boot pages. This also prevents us from allocating buckets in
416 * low memory situations.
421 * To limit bucket recursion we store the original zone flags
422 * in a cookie passed via zalloc_arg/zfree_arg. This allows the
423 * NOVM flag to persist even through deep recursions. We also
424 * store ZFLAG_BUCKET once we have recursed attempting to allocate
425 * a bucket for a bucket zone so we do not allow infinite bucket
426 * recursion. This cookie will even persist to frees of unused
427 * buckets via the allocation path or bucket allocations in the
430 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
431 udata = (void *)(uintptr_t)zone->uz_flags;
433 if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
435 udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
437 if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY)
439 ubz = bucket_zone_lookup(zone->uz_count);
440 if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0)
442 bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
445 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
448 bucket->ub_entries = ubz->ubz_entries;
455 bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
457 struct uma_bucket_zone *ubz;
459 KASSERT(bucket->ub_cnt == 0,
460 ("bucket_free: Freeing a non free bucket."));
461 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
462 udata = (void *)(uintptr_t)zone->uz_flags;
463 ubz = bucket_zone_lookup(bucket->ub_entries);
464 uma_zfree_arg(ubz->ubz_zone, bucket, udata);
468 bucket_zone_drain(void)
470 struct uma_bucket_zone *ubz;
472 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
473 zone_drain(ubz->ubz_zone);
477 zone_try_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom, const bool ws)
481 ZONE_LOCK_ASSERT(zone);
483 if ((bucket = LIST_FIRST(&zdom->uzd_buckets)) != NULL) {
484 MPASS(zdom->uzd_nitems >= bucket->ub_cnt);
485 LIST_REMOVE(bucket, ub_link);
486 zdom->uzd_nitems -= bucket->ub_cnt;
487 if (ws && zdom->uzd_imin > zdom->uzd_nitems)
488 zdom->uzd_imin = zdom->uzd_nitems;
494 zone_put_bucket(uma_zone_t zone, uma_zone_domain_t zdom, uma_bucket_t bucket,
498 ZONE_LOCK_ASSERT(zone);
500 LIST_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link);
501 zdom->uzd_nitems += bucket->ub_cnt;
502 if (ws && zdom->uzd_imax < zdom->uzd_nitems)
503 zdom->uzd_imax = zdom->uzd_nitems;
507 zone_log_warning(uma_zone_t zone)
509 static const struct timeval warninterval = { 300, 0 };
511 if (!zone_warnings || zone->uz_warning == NULL)
514 if (ratecheck(&zone->uz_ratecheck, &warninterval))
515 printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
519 zone_maxaction(uma_zone_t zone)
522 if (zone->uz_maxaction.ta_func != NULL)
523 taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction);
527 zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
531 LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
532 kegfn(klink->kl_keg);
536 * Routine called by timeout which is used to fire off some time interval
537 * based calculations. (stats, hash size, etc.)
546 uma_timeout(void *unused)
549 zone_foreach(zone_timeout);
551 /* Reschedule this event */
552 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
556 * Update the working set size estimate for the zone's bucket cache.
557 * The constants chosen here are somewhat arbitrary. With an update period of
558 * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the
562 zone_domain_update_wss(uma_zone_domain_t zdom)
566 MPASS(zdom->uzd_imax >= zdom->uzd_imin);
567 wss = zdom->uzd_imax - zdom->uzd_imin;
568 zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems;
569 zdom->uzd_wss = (3 * wss + 2 * zdom->uzd_wss) / 5;
573 * Routine to perform timeout driven calculations. This expands the
574 * hashes and does per cpu statistics aggregation.
579 keg_timeout(uma_keg_t keg)
585 * Expand the keg hash table.
587 * This is done if the number of slabs is larger than the hash size.
588 * What I'm trying to do here is completely reduce collisions. This
589 * may be a little aggressive. Should I allow for two collisions max?
591 if (keg->uk_flags & UMA_ZONE_HASH &&
592 (slabs = keg->uk_pages / keg->uk_ppera) >
593 keg->uk_hash.uh_hashsize) {
594 struct uma_hash newhash;
595 struct uma_hash oldhash;
599 * This is so involved because allocating and freeing
600 * while the keg lock is held will lead to deadlock.
601 * I have to do everything in stages and check for
605 ret = hash_alloc(&newhash, 1 << fls(slabs));
608 if (hash_expand(&keg->uk_hash, &newhash)) {
609 oldhash = keg->uk_hash;
610 keg->uk_hash = newhash;
623 zone_timeout(uma_zone_t zone)
627 zone_foreach_keg(zone, &keg_timeout);
630 for (i = 0; i < vm_ndomains; i++)
631 zone_domain_update_wss(&zone->uz_domain[i]);
636 * Allocate and zero fill the next sized hash table from the appropriate
640 * hash A new hash structure with the old hash size in uh_hashsize
643 * 1 on success and 0 on failure.
646 hash_alloc(struct uma_hash *hash, u_int size)
650 KASSERT(powerof2(size), ("hash size must be power of 2"));
651 if (size > UMA_HASH_SIZE_INIT) {
652 hash->uh_hashsize = size;
653 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
654 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
655 M_UMAHASH, M_NOWAIT);
657 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
658 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
659 UMA_ANYDOMAIN, M_WAITOK);
660 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
662 if (hash->uh_slab_hash) {
663 bzero(hash->uh_slab_hash, alloc);
664 hash->uh_hashmask = hash->uh_hashsize - 1;
672 * Expands the hash table for HASH zones. This is done from zone_timeout
673 * to reduce collisions. This must not be done in the regular allocation
674 * path, otherwise, we can recurse on the vm while allocating pages.
677 * oldhash The hash you want to expand
678 * newhash The hash structure for the new table
686 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
692 if (!newhash->uh_slab_hash)
695 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
699 * I need to investigate hash algorithms for resizing without a
703 for (idx = 0; idx < oldhash->uh_hashsize; idx++)
704 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[idx])) {
705 slab = SLIST_FIRST(&oldhash->uh_slab_hash[idx]);
706 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[idx], us_hlink);
707 hval = UMA_HASH(newhash, slab->us_data);
708 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
716 * Free the hash bucket to the appropriate backing store.
719 * slab_hash The hash bucket we're freeing
720 * hashsize The number of entries in that hash bucket
726 hash_free(struct uma_hash *hash)
728 if (hash->uh_slab_hash == NULL)
730 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
731 zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
733 free(hash->uh_slab_hash, M_UMAHASH);
737 * Frees all outstanding items in a bucket
740 * zone The zone to free to, must be unlocked.
741 * bucket The free/alloc bucket with items, cpu queue must be locked.
748 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
756 for (i = 0; i < bucket->ub_cnt; i++)
757 zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
758 zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
763 * Drains the per cpu caches for a zone.
765 * NOTE: This may only be called while the zone is being turn down, and not
766 * during normal operation. This is necessary in order that we do not have
767 * to migrate CPUs to drain the per-CPU caches.
770 * zone The zone to drain, must be unlocked.
776 cache_drain(uma_zone_t zone)
782 * XXX: It is safe to not lock the per-CPU caches, because we're
783 * tearing down the zone anyway. I.e., there will be no further use
784 * of the caches at this point.
786 * XXX: It would good to be able to assert that the zone is being
787 * torn down to prevent improper use of cache_drain().
789 * XXX: We lock the zone before passing into bucket_cache_drain() as
790 * it is used elsewhere. Should the tear-down path be made special
791 * there in some form?
794 cache = &zone->uz_cpu[cpu];
795 bucket_drain(zone, cache->uc_allocbucket);
796 bucket_drain(zone, cache->uc_freebucket);
797 if (cache->uc_allocbucket != NULL)
798 bucket_free(zone, cache->uc_allocbucket, NULL);
799 if (cache->uc_freebucket != NULL)
800 bucket_free(zone, cache->uc_freebucket, NULL);
801 cache->uc_allocbucket = cache->uc_freebucket = NULL;
804 bucket_cache_drain(zone);
809 cache_shrink(uma_zone_t zone)
812 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
816 zone->uz_count = (zone->uz_count_min + zone->uz_count) / 2;
821 cache_drain_safe_cpu(uma_zone_t zone)
827 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
833 if (zone->uz_flags & UMA_ZONE_NUMA)
834 domain = PCPU_GET(domain);
837 cache = &zone->uz_cpu[curcpu];
838 if (cache->uc_allocbucket) {
839 if (cache->uc_allocbucket->ub_cnt != 0)
840 zone_put_bucket(zone, &zone->uz_domain[domain],
841 cache->uc_allocbucket, false);
843 b1 = cache->uc_allocbucket;
844 cache->uc_allocbucket = NULL;
846 if (cache->uc_freebucket) {
847 if (cache->uc_freebucket->ub_cnt != 0)
848 zone_put_bucket(zone, &zone->uz_domain[domain],
849 cache->uc_freebucket, false);
851 b2 = cache->uc_freebucket;
852 cache->uc_freebucket = NULL;
857 bucket_free(zone, b1, NULL);
859 bucket_free(zone, b2, NULL);
863 * Safely drain per-CPU caches of a zone(s) to alloc bucket.
864 * This is an expensive call because it needs to bind to all CPUs
865 * one by one and enter a critical section on each of them in order
866 * to safely access their cache buckets.
867 * Zone lock must not be held on call this function.
870 cache_drain_safe(uma_zone_t zone)
875 * Polite bucket sizes shrinking was not enouth, shrink aggressively.
880 zone_foreach(cache_shrink);
883 thread_lock(curthread);
884 sched_bind(curthread, cpu);
885 thread_unlock(curthread);
888 cache_drain_safe_cpu(zone);
890 zone_foreach(cache_drain_safe_cpu);
892 thread_lock(curthread);
893 sched_unbind(curthread);
894 thread_unlock(curthread);
898 * Drain the cached buckets from a zone. Expects a locked zone on entry.
901 bucket_cache_drain(uma_zone_t zone)
903 uma_zone_domain_t zdom;
908 * Drain the bucket queues and free the buckets.
910 for (i = 0; i < vm_ndomains; i++) {
911 zdom = &zone->uz_domain[i];
912 while ((bucket = zone_try_fetch_bucket(zone, zdom, false)) !=
915 bucket_drain(zone, bucket);
916 bucket_free(zone, bucket, NULL);
922 * Shrink further bucket sizes. Price of single zone lock collision
923 * is probably lower then price of global cache drain.
925 if (zone->uz_count > zone->uz_count_min)
930 keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
936 CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes",
937 keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera);
940 flags = slab->us_flags;
942 if (keg->uk_fini != NULL) {
943 for (i--; i > -1; i--)
946 * trash_fini implies that dtor was trash_dtor. trash_fini
947 * would check that memory hasn't been modified since free,
948 * which executed trash_dtor.
949 * That's why we need to run uma_dbg_kskip() check here,
950 * albeit we don't make skip check for other init/fini
953 if (!uma_dbg_kskip(keg, slab->us_data + (keg->uk_rsize * i)) ||
954 keg->uk_fini != trash_fini)
956 keg->uk_fini(slab->us_data + (keg->uk_rsize * i),
959 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
960 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
961 keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
962 uma_total_dec(PAGE_SIZE * keg->uk_ppera);
966 * Frees pages from a keg back to the system. This is done on demand from
967 * the pageout daemon.
972 keg_drain(uma_keg_t keg)
974 struct slabhead freeslabs = { 0 };
976 uma_slab_t slab, tmp;
980 * We don't want to take pages from statically allocated kegs at this
983 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
986 CTR3(KTR_UMA, "keg_drain %s(%p) free items: %u",
987 keg->uk_name, keg, keg->uk_free);
989 if (keg->uk_free == 0)
992 for (i = 0; i < vm_ndomains; i++) {
993 dom = &keg->uk_domain[i];
994 LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) {
995 /* We have nowhere to free these to. */
996 if (slab->us_flags & UMA_SLAB_BOOT)
999 LIST_REMOVE(slab, us_link);
1000 keg->uk_pages -= keg->uk_ppera;
1001 keg->uk_free -= keg->uk_ipers;
1003 if (keg->uk_flags & UMA_ZONE_HASH)
1004 UMA_HASH_REMOVE(&keg->uk_hash, slab,
1007 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
1014 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
1015 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
1016 keg_free_slab(keg, slab, keg->uk_ipers);
1021 zone_drain_wait(uma_zone_t zone, int waitok)
1025 * Set draining to interlock with zone_dtor() so we can release our
1026 * locks as we go. Only dtor() should do a WAITOK call since it
1027 * is the only call that knows the structure will still be available
1031 while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
1032 if (waitok == M_NOWAIT)
1034 msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1);
1036 zone->uz_flags |= UMA_ZFLAG_DRAINING;
1037 bucket_cache_drain(zone);
1040 * The DRAINING flag protects us from being freed while
1041 * we're running. Normally the uma_rwlock would protect us but we
1042 * must be able to release and acquire the right lock for each keg.
1044 zone_foreach_keg(zone, &keg_drain);
1046 zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
1053 zone_drain(uma_zone_t zone)
1056 zone_drain_wait(zone, M_NOWAIT);
1060 * Allocate a new slab for a keg. This does not insert the slab onto a list.
1061 * If the allocation was successful, the keg lock will be held upon return,
1062 * otherwise the keg will be left unlocked.
1065 * flags Wait flags for the item initialization routine
1066 * aflags Wait flags for the slab allocation
1069 * The slab that was allocated or NULL if there is no memory and the
1070 * caller specified M_NOWAIT.
1073 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags,
1083 KASSERT(domain >= 0 && domain < vm_ndomains,
1084 ("keg_alloc_slab: domain %d out of range", domain));
1085 mtx_assert(&keg->uk_lock, MA_OWNED);
1087 allocf = keg->uk_allocf;
1092 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1093 slab = zone_alloc_item(keg->uk_slabzone, NULL, domain, aflags);
1099 * This reproduces the old vm_zone behavior of zero filling pages the
1100 * first time they are added to a zone.
1102 * Malloced items are zeroed in uma_zalloc.
1105 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1110 if (keg->uk_flags & UMA_ZONE_NODUMP)
1113 /* zone is passed for legacy reasons. */
1114 size = keg->uk_ppera * PAGE_SIZE;
1115 mem = allocf(zone, size, domain, &sflags, aflags);
1117 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1118 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
1122 uma_total_inc(size);
1124 /* Point the slab into the allocated memory */
1125 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
1126 slab = (uma_slab_t )(mem + keg->uk_pgoff);
1128 if (keg->uk_flags & UMA_ZONE_VTOSLAB)
1129 for (i = 0; i < keg->uk_ppera; i++)
1130 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
1133 slab->us_data = mem;
1134 slab->us_freecount = keg->uk_ipers;
1135 slab->us_flags = sflags;
1136 slab->us_domain = domain;
1137 BIT_FILL(SLAB_SETSIZE, &slab->us_free);
1139 BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree);
1142 if (keg->uk_init != NULL) {
1143 for (i = 0; i < keg->uk_ipers; i++)
1144 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
1145 keg->uk_size, flags) != 0)
1147 if (i != keg->uk_ipers) {
1148 keg_free_slab(keg, slab, i);
1155 CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)",
1156 slab, keg->uk_name, keg);
1158 if (keg->uk_flags & UMA_ZONE_HASH)
1159 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
1161 keg->uk_pages += keg->uk_ppera;
1162 keg->uk_free += keg->uk_ipers;
1169 * This function is intended to be used early on in place of page_alloc() so
1170 * that we may use the boot time page cache to satisfy allocations before
1174 startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1181 keg = zone_first_keg(zone);
1184 * If we are in BOOT_BUCKETS or higher, than switch to real
1185 * allocator. Zones with page sized slabs switch at BOOT_PAGEALLOC.
1191 case BOOT_PAGEALLOC:
1192 if (keg->uk_ppera > 1)
1195 #ifdef UMA_MD_SMALL_ALLOC
1196 keg->uk_allocf = (keg->uk_ppera > 1) ?
1197 page_alloc : uma_small_alloc;
1199 keg->uk_allocf = page_alloc;
1201 return keg->uk_allocf(zone, bytes, domain, pflag, wait);
1205 * Check our small startup cache to see if it has pages remaining.
1207 pages = howmany(bytes, PAGE_SIZE);
1208 KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__));
1209 if (pages > boot_pages)
1210 panic("UMA zone \"%s\": Increase vm.boot_pages", zone->uz_name);
1212 printf("%s from \"%s\", %d boot pages left\n", __func__, zone->uz_name,
1216 boot_pages -= pages;
1217 bootmem += pages * PAGE_SIZE;
1218 *pflag = UMA_SLAB_BOOT;
1224 * Allocates a number of pages from the system
1227 * bytes The number of bytes requested
1228 * wait Shall we wait?
1231 * A pointer to the alloced memory or possibly
1232 * NULL if M_NOWAIT is set.
1235 page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1238 void *p; /* Returned page */
1240 *pflag = UMA_SLAB_KERNEL;
1241 p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait);
1247 pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1250 struct pglist alloctail;
1251 vm_offset_t addr, zkva;
1253 vm_page_t p, p_next;
1258 MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE);
1260 TAILQ_INIT(&alloctail);
1261 flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1262 malloc2vm_flags(wait);
1263 *pflag = UMA_SLAB_KERNEL;
1264 for (cpu = 0; cpu <= mp_maxid; cpu++) {
1265 if (CPU_ABSENT(cpu)) {
1266 p = vm_page_alloc(NULL, 0, flags);
1269 p = vm_page_alloc(NULL, 0, flags);
1271 pc = pcpu_find(cpu);
1272 p = vm_page_alloc_domain(NULL, 0, pc->pc_domain, flags);
1273 if (__predict_false(p == NULL))
1274 p = vm_page_alloc(NULL, 0, flags);
1277 if (__predict_false(p == NULL))
1279 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1281 if ((addr = kva_alloc(bytes)) == 0)
1284 TAILQ_FOREACH(p, &alloctail, listq) {
1285 pmap_qenter(zkva, &p, 1);
1288 return ((void*)addr);
1290 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1291 vm_page_unwire_noq(p);
1298 * Allocates a number of pages from within an object
1301 * bytes The number of bytes requested
1302 * wait Shall we wait?
1305 * A pointer to the alloced memory or possibly
1306 * NULL if M_NOWAIT is set.
1309 noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
1312 TAILQ_HEAD(, vm_page) alloctail;
1314 vm_offset_t retkva, zkva;
1315 vm_page_t p, p_next;
1318 TAILQ_INIT(&alloctail);
1319 keg = zone_first_keg(zone);
1321 npages = howmany(bytes, PAGE_SIZE);
1322 while (npages > 0) {
1323 p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT |
1324 VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1325 ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK :
1329 * Since the page does not belong to an object, its
1332 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1337 * Page allocation failed, free intermediate pages and
1340 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1341 vm_page_unwire_noq(p);
1346 *flags = UMA_SLAB_PRIV;
1347 zkva = keg->uk_kva +
1348 atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1350 TAILQ_FOREACH(p, &alloctail, listq) {
1351 pmap_qenter(zkva, &p, 1);
1355 return ((void *)retkva);
1359 * Frees a number of pages to the system
1362 * mem A pointer to the memory to be freed
1363 * size The size of the memory being freed
1364 * flags The original p->us_flags field
1370 page_free(void *mem, vm_size_t size, uint8_t flags)
1373 if ((flags & UMA_SLAB_KERNEL) == 0)
1374 panic("UMA: page_free used with invalid flags %x", flags);
1376 kmem_free((vm_offset_t)mem, size);
1380 * Frees pcpu zone allocations
1383 * mem A pointer to the memory to be freed
1384 * size The size of the memory being freed
1385 * flags The original p->us_flags field
1391 pcpu_page_free(void *mem, vm_size_t size, uint8_t flags)
1393 vm_offset_t sva, curva;
1397 MPASS(size == (mp_maxid+1)*PAGE_SIZE);
1398 sva = (vm_offset_t)mem;
1399 for (curva = sva; curva < sva + size; curva += PAGE_SIZE) {
1400 paddr = pmap_kextract(curva);
1401 m = PHYS_TO_VM_PAGE(paddr);
1402 vm_page_unwire_noq(m);
1405 pmap_qremove(sva, size >> PAGE_SHIFT);
1406 kva_free(sva, size);
1411 * Zero fill initializer
1413 * Arguments/Returns follow uma_init specifications
1416 zero_init(void *mem, int size, int flags)
1423 * Finish creating a small uma keg. This calculates ipers, and the keg size.
1426 * keg The zone we should initialize
1432 keg_small_init(uma_keg_t keg)
1440 if (keg->uk_flags & UMA_ZONE_PCPU) {
1441 u_int ncpus = (mp_maxid + 1) ? (mp_maxid + 1) : MAXCPU;
1443 slabsize = UMA_PCPU_ALLOC_SIZE;
1444 keg->uk_ppera = ncpus;
1446 slabsize = UMA_SLAB_SIZE;
1451 * Calculate the size of each allocation (rsize) according to
1452 * alignment. If the requested size is smaller than we have
1453 * allocation bits for we round it up.
1455 rsize = keg->uk_size;
1456 if (rsize < slabsize / SLAB_SETSIZE)
1457 rsize = slabsize / SLAB_SETSIZE;
1458 if (rsize & keg->uk_align)
1459 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1460 keg->uk_rsize = rsize;
1462 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
1463 keg->uk_rsize < UMA_PCPU_ALLOC_SIZE,
1464 ("%s: size %u too large", __func__, keg->uk_rsize));
1466 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1469 shsize = sizeof(struct uma_slab);
1471 if (rsize <= slabsize - shsize)
1472 keg->uk_ipers = (slabsize - shsize) / rsize;
1474 /* Handle special case when we have 1 item per slab, so
1475 * alignment requirement can be relaxed. */
1476 KASSERT(keg->uk_size <= slabsize - shsize,
1477 ("%s: size %u greater than slab", __func__, keg->uk_size));
1480 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1481 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1483 memused = keg->uk_ipers * rsize + shsize;
1484 wastedspace = slabsize - memused;
1487 * We can't do OFFPAGE if we're internal or if we've been
1488 * asked to not go to the VM for buckets. If we do this we
1489 * may end up going to the VM for slabs which we do not
1490 * want to do if we're UMA_ZFLAG_CACHEONLY as a result
1491 * of UMA_ZONE_VM, which clearly forbids it.
1493 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1494 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1498 * See if using an OFFPAGE slab will limit our waste. Only do
1499 * this if it permits more items per-slab.
1501 * XXX We could try growing slabsize to limit max waste as well.
1502 * Historically this was not done because the VM could not
1503 * efficiently handle contiguous allocations.
1505 if ((wastedspace >= slabsize / UMA_MAX_WASTE) &&
1506 (keg->uk_ipers < (slabsize / keg->uk_rsize))) {
1507 keg->uk_ipers = slabsize / keg->uk_rsize;
1508 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1509 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1510 CTR6(KTR_UMA, "UMA decided we need offpage slab headers for "
1511 "keg: %s(%p), calculated wastedspace = %d, "
1512 "maximum wasted space allowed = %d, "
1513 "calculated ipers = %d, "
1514 "new wasted space = %d\n", keg->uk_name, keg, wastedspace,
1515 slabsize / UMA_MAX_WASTE, keg->uk_ipers,
1516 slabsize - keg->uk_ipers * keg->uk_rsize);
1517 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1520 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1521 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1522 keg->uk_flags |= UMA_ZONE_HASH;
1526 * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do
1527 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1531 * keg The keg we should initialize
1537 keg_large_init(uma_keg_t keg)
1541 KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1542 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1543 ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1544 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1545 ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__));
1547 keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE);
1549 keg->uk_rsize = keg->uk_size;
1551 /* Check whether we have enough space to not do OFFPAGE. */
1552 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0) {
1553 shsize = sizeof(struct uma_slab);
1554 if (shsize & UMA_ALIGN_PTR)
1555 shsize = (shsize & ~UMA_ALIGN_PTR) +
1556 (UMA_ALIGN_PTR + 1);
1558 if (PAGE_SIZE * keg->uk_ppera - keg->uk_rsize < shsize) {
1560 * We can't do OFFPAGE if we're internal, in which case
1561 * we need an extra page per allocation to contain the
1564 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) == 0)
1565 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1571 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1572 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1573 keg->uk_flags |= UMA_ZONE_HASH;
1577 keg_cachespread_init(uma_keg_t keg)
1584 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1585 ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__));
1587 alignsize = keg->uk_align + 1;
1588 rsize = keg->uk_size;
1590 * We want one item to start on every align boundary in a page. To
1591 * do this we will span pages. We will also extend the item by the
1592 * size of align if it is an even multiple of align. Otherwise, it
1593 * would fall on the same boundary every time.
1595 if (rsize & keg->uk_align)
1596 rsize = (rsize & ~keg->uk_align) + alignsize;
1597 if ((rsize & alignsize) == 0)
1599 trailer = rsize - keg->uk_size;
1600 pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1601 pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1602 keg->uk_rsize = rsize;
1603 keg->uk_ppera = pages;
1604 keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1605 keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1606 KASSERT(keg->uk_ipers <= SLAB_SETSIZE,
1607 ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1612 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1613 * the keg onto the global keg list.
1615 * Arguments/Returns follow uma_ctor specifications
1616 * udata Actually uma_kctor_args
1619 keg_ctor(void *mem, int size, void *udata, int flags)
1621 struct uma_kctor_args *arg = udata;
1622 uma_keg_t keg = mem;
1626 keg->uk_size = arg->size;
1627 keg->uk_init = arg->uminit;
1628 keg->uk_fini = arg->fini;
1629 keg->uk_align = arg->align;
1631 keg->uk_reserve = 0;
1633 keg->uk_flags = arg->flags;
1634 keg->uk_slabzone = NULL;
1637 * We use a global round-robin policy by default. Zones with
1638 * UMA_ZONE_NUMA set will use first-touch instead, in which case the
1639 * iterator is never run.
1641 keg->uk_dr.dr_policy = DOMAINSET_RR();
1642 keg->uk_dr.dr_iter = 0;
1645 * The master zone is passed to us at keg-creation time.
1648 keg->uk_name = zone->uz_name;
1650 if (arg->flags & UMA_ZONE_VM)
1651 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1653 if (arg->flags & UMA_ZONE_ZINIT)
1654 keg->uk_init = zero_init;
1656 if (arg->flags & UMA_ZONE_MALLOC)
1657 keg->uk_flags |= UMA_ZONE_VTOSLAB;
1659 if (arg->flags & UMA_ZONE_PCPU)
1661 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1663 keg->uk_flags &= ~UMA_ZONE_PCPU;
1666 if (keg->uk_flags & UMA_ZONE_CACHESPREAD) {
1667 keg_cachespread_init(keg);
1669 if (keg->uk_size > UMA_SLAB_SPACE)
1670 keg_large_init(keg);
1672 keg_small_init(keg);
1675 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1676 keg->uk_slabzone = slabzone;
1679 * If we haven't booted yet we need allocations to go through the
1680 * startup cache until the vm is ready.
1682 if (booted < BOOT_PAGEALLOC)
1683 keg->uk_allocf = startup_alloc;
1684 #ifdef UMA_MD_SMALL_ALLOC
1685 else if (keg->uk_ppera == 1)
1686 keg->uk_allocf = uma_small_alloc;
1688 else if (keg->uk_flags & UMA_ZONE_PCPU)
1689 keg->uk_allocf = pcpu_page_alloc;
1691 keg->uk_allocf = page_alloc;
1692 #ifdef UMA_MD_SMALL_ALLOC
1693 if (keg->uk_ppera == 1)
1694 keg->uk_freef = uma_small_free;
1697 if (keg->uk_flags & UMA_ZONE_PCPU)
1698 keg->uk_freef = pcpu_page_free;
1700 keg->uk_freef = page_free;
1703 * Initialize keg's lock
1705 KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS));
1708 * If we're putting the slab header in the actual page we need to
1709 * figure out where in each page it goes. This calculates a right
1710 * justified offset into the memory on an ALIGN_PTR boundary.
1712 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1715 /* Size of the slab struct and free list */
1716 totsize = sizeof(struct uma_slab);
1718 if (totsize & UMA_ALIGN_PTR)
1719 totsize = (totsize & ~UMA_ALIGN_PTR) +
1720 (UMA_ALIGN_PTR + 1);
1721 keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize;
1724 * The only way the following is possible is if with our
1725 * UMA_ALIGN_PTR adjustments we are now bigger than
1726 * UMA_SLAB_SIZE. I haven't checked whether this is
1727 * mathematically possible for all cases, so we make
1730 totsize = keg->uk_pgoff + sizeof(struct uma_slab);
1731 if (totsize > PAGE_SIZE * keg->uk_ppera) {
1732 printf("zone %s ipers %d rsize %d size %d\n",
1733 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1735 panic("UMA slab won't fit.");
1739 if (keg->uk_flags & UMA_ZONE_HASH)
1740 hash_alloc(&keg->uk_hash, 0);
1742 CTR5(KTR_UMA, "keg_ctor %p zone %s(%p) out %d free %d\n",
1743 keg, zone->uz_name, zone,
1744 (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
1747 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1749 rw_wlock(&uma_rwlock);
1750 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1751 rw_wunlock(&uma_rwlock);
1756 * Zone header ctor. This initializes all fields, locks, etc.
1758 * Arguments/Returns follow uma_ctor specifications
1759 * udata Actually uma_zctor_args
1762 zone_ctor(void *mem, int size, void *udata, int flags)
1764 struct uma_zctor_args *arg = udata;
1765 uma_zone_t zone = mem;
1770 zone->uz_name = arg->name;
1771 zone->uz_ctor = arg->ctor;
1772 zone->uz_dtor = arg->dtor;
1773 zone->uz_slab = zone_fetch_slab;
1774 zone->uz_init = NULL;
1775 zone->uz_fini = NULL;
1776 zone->uz_allocs = 0;
1779 zone->uz_sleeps = 0;
1781 zone->uz_count_min = 0;
1783 zone->uz_warning = NULL;
1784 /* The domain structures follow the cpu structures. */
1786 (struct uma_zone_domain *)&zone->uz_cpu[mp_maxid + 1];
1787 timevalclear(&zone->uz_ratecheck);
1790 ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
1793 * This is a pure cache zone, no kegs.
1796 if (arg->flags & UMA_ZONE_VM)
1797 arg->flags |= UMA_ZFLAG_CACHEONLY;
1798 zone->uz_flags = arg->flags;
1799 zone->uz_size = arg->size;
1800 zone->uz_import = arg->import;
1801 zone->uz_release = arg->release;
1802 zone->uz_arg = arg->arg;
1803 zone->uz_lockptr = &zone->uz_lock;
1804 rw_wlock(&uma_rwlock);
1805 LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
1806 rw_wunlock(&uma_rwlock);
1811 * Use the regular zone/keg/slab allocator.
1813 zone->uz_import = (uma_import)zone_import;
1814 zone->uz_release = (uma_release)zone_release;
1815 zone->uz_arg = zone;
1817 if (arg->flags & UMA_ZONE_SECONDARY) {
1818 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1819 zone->uz_init = arg->uminit;
1820 zone->uz_fini = arg->fini;
1821 zone->uz_lockptr = &keg->uk_lock;
1822 zone->uz_flags |= UMA_ZONE_SECONDARY;
1823 rw_wlock(&uma_rwlock);
1825 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1826 if (LIST_NEXT(z, uz_link) == NULL) {
1827 LIST_INSERT_AFTER(z, zone, uz_link);
1832 rw_wunlock(&uma_rwlock);
1833 } else if (keg == NULL) {
1834 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1835 arg->align, arg->flags)) == NULL)
1838 struct uma_kctor_args karg;
1841 /* We should only be here from uma_startup() */
1842 karg.size = arg->size;
1843 karg.uminit = arg->uminit;
1844 karg.fini = arg->fini;
1845 karg.align = arg->align;
1846 karg.flags = arg->flags;
1848 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1855 * Link in the first keg.
1857 zone->uz_klink.kl_keg = keg;
1858 LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1859 zone->uz_lockptr = &keg->uk_lock;
1860 zone->uz_size = keg->uk_size;
1861 zone->uz_flags |= (keg->uk_flags &
1862 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1865 * Some internal zones don't have room allocated for the per cpu
1866 * caches. If we're internal, bail out here.
1868 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1869 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1870 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1875 KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) !=
1876 (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET),
1877 ("Invalid zone flag combination"));
1878 if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0)
1879 zone->uz_count = BUCKET_MAX;
1880 else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0)
1883 zone->uz_count = bucket_select(zone->uz_size);
1884 zone->uz_count_min = zone->uz_count;
1890 * Keg header dtor. This frees all data, destroys locks, frees the hash
1891 * table and removes the keg from the global list.
1893 * Arguments/Returns follow uma_dtor specifications
1897 keg_dtor(void *arg, int size, void *udata)
1901 keg = (uma_keg_t)arg;
1903 if (keg->uk_free != 0) {
1904 printf("Freed UMA keg (%s) was not empty (%d items). "
1905 " Lost %d pages of memory.\n",
1906 keg->uk_name ? keg->uk_name : "",
1907 keg->uk_free, keg->uk_pages);
1911 hash_free(&keg->uk_hash);
1919 * Arguments/Returns follow uma_dtor specifications
1923 zone_dtor(void *arg, int size, void *udata)
1929 zone = (uma_zone_t)arg;
1930 keg = zone_first_keg(zone);
1932 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1935 rw_wlock(&uma_rwlock);
1936 LIST_REMOVE(zone, uz_link);
1937 rw_wunlock(&uma_rwlock);
1939 * XXX there are some races here where
1940 * the zone can be drained but zone lock
1941 * released and then refilled before we
1942 * remove it... we dont care for now
1944 zone_drain_wait(zone, M_WAITOK);
1946 * Unlink all of our kegs.
1948 while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1949 klink->kl_keg = NULL;
1950 LIST_REMOVE(klink, kl_link);
1951 if (klink == &zone->uz_klink)
1953 free(klink, M_TEMP);
1956 * We only destroy kegs from non secondary zones.
1958 if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0) {
1959 rw_wlock(&uma_rwlock);
1960 LIST_REMOVE(keg, uk_link);
1961 rw_wunlock(&uma_rwlock);
1962 zone_free_item(kegs, keg, NULL, SKIP_NONE);
1964 ZONE_LOCK_FINI(zone);
1968 * Traverses every zone in the system and calls a callback
1971 * zfunc A pointer to a function which accepts a zone
1978 zone_foreach(void (*zfunc)(uma_zone_t))
1983 rw_rlock(&uma_rwlock);
1984 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1985 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1988 rw_runlock(&uma_rwlock);
1992 * Count how many pages do we need to bootstrap. VM supplies
1993 * its need in early zones in the argument, we add up our zones,
1994 * which consist of: UMA Slabs, UMA Hash and 9 Bucket zones. The
1995 * zone of zones and zone of kegs are accounted separately.
1997 #define UMA_BOOT_ZONES 11
1998 /* Zone of zones and zone of kegs have arbitrary alignment. */
1999 #define UMA_BOOT_ALIGN 32
2000 static int zsize, ksize;
2002 uma_startup_count(int vm_zones)
2006 ksize = sizeof(struct uma_keg) +
2007 (sizeof(struct uma_domain) * vm_ndomains);
2008 zsize = sizeof(struct uma_zone) +
2009 (sizeof(struct uma_cache) * (mp_maxid + 1)) +
2010 (sizeof(struct uma_zone_domain) * vm_ndomains);
2013 * Memory for the zone of kegs and its keg,
2014 * and for zone of zones.
2016 pages = howmany(roundup(zsize, CACHE_LINE_SIZE) * 2 +
2017 roundup(ksize, CACHE_LINE_SIZE), PAGE_SIZE);
2019 #ifdef UMA_MD_SMALL_ALLOC
2020 zones = UMA_BOOT_ZONES;
2022 zones = UMA_BOOT_ZONES + vm_zones;
2026 /* Memory for the rest of startup zones, UMA and VM, ... */
2027 if (zsize > UMA_SLAB_SPACE)
2028 pages += (zones + vm_zones) *
2029 howmany(roundup2(zsize, UMA_BOOT_ALIGN), UMA_SLAB_SIZE);
2030 else if (roundup2(zsize, UMA_BOOT_ALIGN) > UMA_SLAB_SPACE)
2033 pages += howmany(zones,
2034 UMA_SLAB_SPACE / roundup2(zsize, UMA_BOOT_ALIGN));
2036 /* ... and their kegs. Note that zone of zones allocates a keg! */
2037 pages += howmany(zones + 1,
2038 UMA_SLAB_SPACE / roundup2(ksize, UMA_BOOT_ALIGN));
2041 * Most of startup zones are not going to be offpages, that's
2042 * why we use UMA_SLAB_SPACE instead of UMA_SLAB_SIZE in all
2043 * calculations. Some large bucket zones will be offpage, and
2044 * thus will allocate hashes. We take conservative approach
2045 * and assume that all zones may allocate hash. This may give
2046 * us some positive inaccuracy, usually an extra single page.
2048 pages += howmany(zones, UMA_SLAB_SPACE /
2049 (sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT));
2055 uma_startup(void *mem, int npages)
2057 struct uma_zctor_args args;
2058 uma_keg_t masterkeg;
2062 printf("Entering %s with %d boot pages configured\n", __func__, npages);
2065 rw_init(&uma_rwlock, "UMA lock");
2067 /* Use bootpages memory for the zone of zones and zone of kegs. */
2069 zones = (uma_zone_t)m;
2070 m += roundup(zsize, CACHE_LINE_SIZE);
2071 kegs = (uma_zone_t)m;
2072 m += roundup(zsize, CACHE_LINE_SIZE);
2073 masterkeg = (uma_keg_t)m;
2074 m += roundup(ksize, CACHE_LINE_SIZE);
2075 m = roundup(m, PAGE_SIZE);
2076 npages -= (m - (uintptr_t)mem) / PAGE_SIZE;
2079 /* "manually" create the initial zone */
2080 memset(&args, 0, sizeof(args));
2081 args.name = "UMA Kegs";
2083 args.ctor = keg_ctor;
2084 args.dtor = keg_dtor;
2085 args.uminit = zero_init;
2087 args.keg = masterkeg;
2088 args.align = UMA_BOOT_ALIGN - 1;
2089 args.flags = UMA_ZFLAG_INTERNAL;
2090 zone_ctor(kegs, zsize, &args, M_WAITOK);
2093 boot_pages = npages;
2095 args.name = "UMA Zones";
2097 args.ctor = zone_ctor;
2098 args.dtor = zone_dtor;
2099 args.uminit = zero_init;
2102 args.align = UMA_BOOT_ALIGN - 1;
2103 args.flags = UMA_ZFLAG_INTERNAL;
2104 zone_ctor(zones, zsize, &args, M_WAITOK);
2106 /* Now make a zone for slab headers */
2107 slabzone = uma_zcreate("UMA Slabs",
2108 sizeof(struct uma_slab),
2109 NULL, NULL, NULL, NULL,
2110 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2112 hashzone = uma_zcreate("UMA Hash",
2113 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
2114 NULL, NULL, NULL, NULL,
2115 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2119 booted = BOOT_STRAPPED;
2127 printf("Entering %s with %d boot pages left\n", __func__, boot_pages);
2129 booted = BOOT_PAGEALLOC;
2137 printf("Entering %s with %d boot pages left\n", __func__, boot_pages);
2139 booted = BOOT_BUCKETS;
2140 sx_init(&uma_drain_lock, "umadrain");
2149 TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor);
2150 uma_dbg_cnt = counter_u64_alloc(M_WAITOK);
2151 uma_skip_cnt = counter_u64_alloc(M_WAITOK);
2153 callout_init(&uma_callout, 1);
2154 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
2155 booted = BOOT_RUNNING;
2157 EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL,
2158 EVENTHANDLER_PRI_FIRST);
2165 booted = BOOT_SHUTDOWN;
2169 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
2170 int align, uint32_t flags)
2172 struct uma_kctor_args args;
2175 args.uminit = uminit;
2177 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
2180 return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK));
2183 /* Public functions */
2186 uma_set_align(int align)
2189 if (align != UMA_ALIGN_CACHE)
2190 uma_align_cache = align;
2195 uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
2196 uma_init uminit, uma_fini fini, int align, uint32_t flags)
2199 struct uma_zctor_args args;
2203 KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
2206 /* This stuff is essential for the zone ctor */
2207 memset(&args, 0, sizeof(args));
2212 args.uminit = uminit;
2216 * If a zone is being created with an empty constructor and
2217 * destructor, pass UMA constructor/destructor which checks for
2218 * memory use after free.
2220 if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOFREE))) &&
2221 ctor == NULL && dtor == NULL && uminit == NULL && fini == NULL) {
2222 args.ctor = trash_ctor;
2223 args.dtor = trash_dtor;
2224 args.uminit = trash_init;
2225 args.fini = trash_fini;
2232 if (booted < BOOT_BUCKETS) {
2235 sx_slock(&uma_drain_lock);
2238 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
2240 sx_sunlock(&uma_drain_lock);
2246 uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor,
2247 uma_init zinit, uma_fini zfini, uma_zone_t master)
2249 struct uma_zctor_args args;
2254 keg = zone_first_keg(master);
2255 memset(&args, 0, sizeof(args));
2257 args.size = keg->uk_size;
2260 args.uminit = zinit;
2262 args.align = keg->uk_align;
2263 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
2266 if (booted < BOOT_BUCKETS) {
2269 sx_slock(&uma_drain_lock);
2272 /* XXX Attaches only one keg of potentially many. */
2273 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
2275 sx_sunlock(&uma_drain_lock);
2281 uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor,
2282 uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease,
2283 void *arg, int flags)
2285 struct uma_zctor_args args;
2287 memset(&args, 0, sizeof(args));
2292 args.uminit = zinit;
2294 args.import = zimport;
2295 args.release = zrelease;
2300 return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK));
2304 zone_lock_pair(uma_zone_t a, uma_zone_t b)
2308 mtx_lock_flags(b->uz_lockptr, MTX_DUPOK);
2311 mtx_lock_flags(a->uz_lockptr, MTX_DUPOK);
2316 zone_unlock_pair(uma_zone_t a, uma_zone_t b)
2324 uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
2331 klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
2333 zone_lock_pair(zone, master);
2335 * zone must use vtoslab() to resolve objects and must already be
2338 if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
2339 != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
2344 * The new master must also use vtoslab().
2346 if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
2352 * The underlying object must be the same size. rsize
2355 if (master->uz_size != zone->uz_size) {
2360 * Put it at the end of the list.
2362 klink->kl_keg = zone_first_keg(master);
2363 LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
2364 if (LIST_NEXT(kl, kl_link) == NULL) {
2365 LIST_INSERT_AFTER(kl, klink, kl_link);
2370 zone->uz_flags |= UMA_ZFLAG_MULTI;
2371 zone->uz_slab = zone_fetch_slab_multi;
2374 zone_unlock_pair(zone, master);
2376 free(klink, M_TEMP);
2384 uma_zdestroy(uma_zone_t zone)
2388 * Large slabs are expensive to reclaim, so don't bother doing
2389 * unnecessary work if we're shutting down.
2391 if (booted == BOOT_SHUTDOWN &&
2392 zone->uz_fini == NULL &&
2393 zone->uz_release == (uma_release)zone_release)
2395 sx_slock(&uma_drain_lock);
2396 zone_free_item(zones, zone, NULL, SKIP_NONE);
2397 sx_sunlock(&uma_drain_lock);
2401 uma_zwait(uma_zone_t zone)
2405 item = uma_zalloc_arg(zone, NULL, M_WAITOK);
2406 uma_zfree(zone, item);
2410 uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags)
2416 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2418 item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO);
2419 if (item != NULL && (flags & M_ZERO)) {
2421 for (i = 0; i <= mp_maxid; i++)
2422 bzero(zpcpu_get_cpu(item, i), zone->uz_size);
2424 bzero(item, zone->uz_size);
2431 * A stub while both regular and pcpu cases are identical.
2434 uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata)
2438 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2440 uma_zfree_arg(zone, item, udata);
2445 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
2447 uma_zone_domain_t zdom;
2448 uma_bucket_t bucket;
2451 int cpu, domain, lockfail;
2456 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2457 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
2459 /* This is the fast path allocation */
2460 CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d",
2461 curthread, zone->uz_name, zone, flags);
2463 if (flags & M_WAITOK) {
2464 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2465 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
2467 KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC"));
2468 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2469 ("uma_zalloc_arg: called with spinlock or critical section held"));
2470 if (zone->uz_flags & UMA_ZONE_PCPU)
2471 KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone "
2472 "with M_ZERO passed"));
2474 #ifdef DEBUG_MEMGUARD
2475 if (memguard_cmp_zone(zone)) {
2476 item = memguard_alloc(zone->uz_size, flags);
2478 if (zone->uz_init != NULL &&
2479 zone->uz_init(item, zone->uz_size, flags) != 0)
2481 if (zone->uz_ctor != NULL &&
2482 zone->uz_ctor(item, zone->uz_size, udata,
2484 zone->uz_fini(item, zone->uz_size);
2489 /* This is unfortunate but should not be fatal. */
2493 * If possible, allocate from the per-CPU cache. There are two
2494 * requirements for safe access to the per-CPU cache: (1) the thread
2495 * accessing the cache must not be preempted or yield during access,
2496 * and (2) the thread must not migrate CPUs without switching which
2497 * cache it accesses. We rely on a critical section to prevent
2498 * preemption and migration. We release the critical section in
2499 * order to acquire the zone mutex if we are unable to allocate from
2500 * the current cache; when we re-acquire the critical section, we
2501 * must detect and handle migration if it has occurred.
2506 cache = &zone->uz_cpu[cpu];
2509 bucket = cache->uc_allocbucket;
2510 if (bucket != NULL && bucket->ub_cnt > 0) {
2512 item = bucket->ub_bucket[bucket->ub_cnt];
2514 bucket->ub_bucket[bucket->ub_cnt] = NULL;
2516 KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
2520 skipdbg = uma_dbg_zskip(zone, item);
2522 if (zone->uz_ctor != NULL &&
2524 (!skipdbg || zone->uz_ctor != trash_ctor ||
2525 zone->uz_dtor != trash_dtor) &&
2527 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2528 atomic_add_long(&zone->uz_fails, 1);
2529 zone_free_item(zone, item, udata, SKIP_DTOR);
2534 uma_dbg_alloc(zone, NULL, item);
2537 uma_zero_item(item, zone);
2542 * We have run out of items in our alloc bucket.
2543 * See if we can switch with our free bucket.
2545 bucket = cache->uc_freebucket;
2546 if (bucket != NULL && bucket->ub_cnt > 0) {
2548 "uma_zalloc: zone %s(%p) swapping empty with alloc",
2549 zone->uz_name, zone);
2550 cache->uc_freebucket = cache->uc_allocbucket;
2551 cache->uc_allocbucket = bucket;
2556 * Discard any empty allocation bucket while we hold no locks.
2558 bucket = cache->uc_allocbucket;
2559 cache->uc_allocbucket = NULL;
2562 bucket_free(zone, bucket, udata);
2564 if (zone->uz_flags & UMA_ZONE_NUMA) {
2565 domain = PCPU_GET(domain);
2566 if (VM_DOMAIN_EMPTY(domain))
2567 domain = UMA_ANYDOMAIN;
2569 domain = UMA_ANYDOMAIN;
2571 /* Short-circuit for zones without buckets and low memory. */
2572 if (zone->uz_count == 0 || bucketdisable)
2576 * Attempt to retrieve the item from the per-CPU cache has failed, so
2577 * we must go back to the zone. This requires the zone lock, so we
2578 * must drop the critical section, then re-acquire it when we go back
2579 * to the cache. Since the critical section is released, we may be
2580 * preempted or migrate. As such, make sure not to maintain any
2581 * thread-local state specific to the cache from prior to releasing
2582 * the critical section.
2585 if (ZONE_TRYLOCK(zone) == 0) {
2586 /* Record contention to size the buckets. */
2592 cache = &zone->uz_cpu[cpu];
2594 /* See if we lost the race to fill the cache. */
2595 if (cache->uc_allocbucket != NULL) {
2601 * Check the zone's cache of buckets.
2603 if (domain == UMA_ANYDOMAIN)
2604 zdom = &zone->uz_domain[0];
2606 zdom = &zone->uz_domain[domain];
2607 if ((bucket = zone_try_fetch_bucket(zone, zdom, true)) != NULL) {
2608 KASSERT(bucket->ub_cnt != 0,
2609 ("uma_zalloc_arg: Returning an empty bucket."));
2610 cache->uc_allocbucket = bucket;
2614 /* We are no longer associated with this CPU. */
2618 * We bump the uz count when the cache size is insufficient to
2619 * handle the working set.
2621 if (lockfail && zone->uz_count < BUCKET_MAX)
2626 * Now lets just fill a bucket and put it on the free list. If that
2627 * works we'll restart the allocation from the beginning and it
2628 * will use the just filled bucket.
2630 bucket = zone_alloc_bucket(zone, udata, domain, flags);
2631 CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
2632 zone->uz_name, zone, bucket);
2633 if (bucket != NULL) {
2637 cache = &zone->uz_cpu[cpu];
2640 * See if we lost the race or were migrated. Cache the
2641 * initialized bucket to make this less likely or claim
2642 * the memory directly.
2644 if (cache->uc_allocbucket == NULL &&
2645 ((zone->uz_flags & UMA_ZONE_NUMA) == 0 ||
2646 domain == PCPU_GET(domain))) {
2647 cache->uc_allocbucket = bucket;
2648 zdom->uzd_imax += bucket->ub_cnt;
2649 } else if ((zone->uz_flags & UMA_ZONE_NOBUCKETCACHE) != 0) {
2652 bucket_drain(zone, bucket);
2653 bucket_free(zone, bucket, udata);
2654 goto zalloc_restart;
2656 zone_put_bucket(zone, zdom, bucket, false);
2662 * We may not be able to get a bucket so return an actual item.
2665 item = zone_alloc_item(zone, udata, domain, flags);
2671 uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
2674 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2675 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
2677 /* This is the fast path allocation */
2679 "uma_zalloc_domain thread %x zone %s(%p) domain %d flags %d",
2680 curthread, zone->uz_name, zone, domain, flags);
2682 if (flags & M_WAITOK) {
2683 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2684 "uma_zalloc_domain: zone \"%s\"", zone->uz_name);
2686 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2687 ("uma_zalloc_domain: called with spinlock or critical section held"));
2689 return (zone_alloc_item(zone, udata, domain, flags));
2693 * Find a slab with some space. Prefer slabs that are partially used over those
2694 * that are totally full. This helps to reduce fragmentation.
2696 * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check
2700 keg_first_slab(uma_keg_t keg, int domain, bool rr)
2706 KASSERT(domain >= 0 && domain < vm_ndomains,
2707 ("keg_first_slab: domain %d out of range", domain));
2712 dom = &keg->uk_domain[domain];
2713 if (!LIST_EMPTY(&dom->ud_part_slab))
2714 return (LIST_FIRST(&dom->ud_part_slab));
2715 if (!LIST_EMPTY(&dom->ud_free_slab)) {
2716 slab = LIST_FIRST(&dom->ud_free_slab);
2717 LIST_REMOVE(slab, us_link);
2718 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
2722 domain = (domain + 1) % vm_ndomains;
2723 } while (domain != start);
2729 keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags)
2733 mtx_assert(&keg->uk_lock, MA_OWNED);
2735 reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve;
2736 if (keg->uk_free <= reserve)
2738 return (keg_first_slab(keg, domain, rr));
2742 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags)
2744 struct vm_domainset_iter di;
2751 mtx_assert(&keg->uk_lock, MA_OWNED);
2754 * Use the keg's policy if upper layers haven't already specified a
2755 * domain (as happens with first-touch zones).
2757 * To avoid races we run the iterator with the keg lock held, but that
2758 * means that we cannot allow the vm_domainset layer to sleep. Thus,
2759 * clear M_WAITOK and handle low memory conditions locally.
2761 rr = rdomain == UMA_ANYDOMAIN;
2763 aflags = (flags & ~M_WAITOK) | M_NOWAIT;
2764 vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
2772 slab = keg_fetch_free_slab(keg, domain, rr, flags);
2774 MPASS(slab->us_keg == keg);
2779 * M_NOVM means don't ask at all!
2784 if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2785 keg->uk_flags |= UMA_ZFLAG_FULL;
2787 * If this is not a multi-zone, set the FULL bit.
2788 * Otherwise slab_multi() takes care of it.
2790 if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) {
2791 zone->uz_flags |= UMA_ZFLAG_FULL;
2792 zone_log_warning(zone);
2793 zone_maxaction(zone);
2795 if (flags & M_NOWAIT)
2798 msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2801 slab = keg_alloc_slab(keg, zone, domain, flags, aflags);
2803 * If we got a slab here it's safe to mark it partially used
2804 * and return. We assume that the caller is going to remove
2805 * at least one item.
2808 MPASS(slab->us_keg == keg);
2809 dom = &keg->uk_domain[slab->us_domain];
2810 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
2814 if (rr && vm_domainset_iter_policy(&di, &domain) != 0) {
2815 if ((flags & M_WAITOK) != 0) {
2817 vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0);
2826 * We might not have been able to get a slab but another cpu
2827 * could have while we were unlocked. Check again before we
2830 if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) {
2831 MPASS(slab->us_keg == keg);
2838 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int domain, int flags)
2843 keg = zone_first_keg(zone);
2848 slab = keg_fetch_slab(keg, zone, domain, flags);
2851 if (flags & (M_NOWAIT | M_NOVM))
2859 * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns
2860 * with the keg locked. On NULL no lock is held.
2862 * The last pointer is used to seed the search. It is not required.
2865 zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int domain, int rflags)
2875 * Don't wait on the first pass. This will skip limit tests
2876 * as well. We don't want to block if we can find a provider
2879 flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2881 * Use the last slab allocated as a hint for where to start
2885 slab = keg_fetch_slab(last, zone, domain, flags);
2891 * Loop until we have a slab incase of transient failures
2892 * while M_WAITOK is specified. I'm not sure this is 100%
2893 * required but we've done it for so long now.
2899 * Search the available kegs for slabs. Be careful to hold the
2900 * correct lock while calling into the keg layer.
2902 LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2903 keg = klink->kl_keg;
2905 if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2906 slab = keg_fetch_slab(keg, zone, domain, flags);
2910 if (keg->uk_flags & UMA_ZFLAG_FULL)
2916 if (rflags & (M_NOWAIT | M_NOVM))
2920 * All kegs are full. XXX We can't atomically check all kegs
2921 * and sleep so just sleep for a short period and retry.
2923 if (full && !empty) {
2925 zone->uz_flags |= UMA_ZFLAG_FULL;
2927 zone_log_warning(zone);
2928 zone_maxaction(zone);
2929 msleep(zone, zone->uz_lockptr, PVM,
2930 "zonelimit", hz/100);
2931 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2940 slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
2946 MPASS(keg == slab->us_keg);
2947 mtx_assert(&keg->uk_lock, MA_OWNED);
2949 freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2950 BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2951 item = slab->us_data + (keg->uk_rsize * freei);
2952 slab->us_freecount--;
2955 /* Move this slab to the full list */
2956 if (slab->us_freecount == 0) {
2957 LIST_REMOVE(slab, us_link);
2958 dom = &keg->uk_domain[slab->us_domain];
2959 LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link);
2966 zone_import(uma_zone_t zone, void **bucket, int max, int domain, int flags)
2977 /* Try to keep the buckets totally full */
2978 for (i = 0; i < max; ) {
2979 if ((slab = zone->uz_slab(zone, keg, domain, flags)) == NULL)
2983 stripe = howmany(max, vm_ndomains);
2985 while (slab->us_freecount && i < max) {
2986 bucket[i++] = slab_alloc_item(keg, slab);
2987 if (keg->uk_free <= keg->uk_reserve)
2991 * If the zone is striped we pick a new slab for every
2992 * N allocations. Eliminating this conditional will
2993 * instead pick a new domain for each bucket rather
2994 * than stripe within each bucket. The current option
2995 * produces more fragmentation and requires more cpu
2996 * time but yields better distribution.
2998 if ((zone->uz_flags & UMA_ZONE_NUMA) == 0 &&
2999 vm_ndomains > 1 && --stripe == 0)
3003 /* Don't block if we allocated any successfully. */
3014 zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags)
3016 uma_bucket_t bucket;
3019 CTR1(KTR_UMA, "zone_alloc:_bucket domain %d)", domain);
3021 /* Don't wait for buckets, preserve caller's NOVM setting. */
3022 bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
3026 max = MIN(bucket->ub_entries, zone->uz_count);
3027 bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
3028 max, domain, flags);
3031 * Initialize the memory if necessary.
3033 if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
3036 for (i = 0; i < bucket->ub_cnt; i++)
3037 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
3041 * If we couldn't initialize the whole bucket, put the
3042 * rest back onto the freelist.
3044 if (i != bucket->ub_cnt) {
3045 zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
3046 bucket->ub_cnt - i);
3048 bzero(&bucket->ub_bucket[i],
3049 sizeof(void *) * (bucket->ub_cnt - i));
3055 if (bucket->ub_cnt == 0) {
3056 bucket_free(zone, bucket, udata);
3057 atomic_add_long(&zone->uz_fails, 1);
3065 * Allocates a single item from a zone.
3068 * zone The zone to alloc for.
3069 * udata The data to be passed to the constructor.
3070 * domain The domain to allocate from or UMA_ANYDOMAIN.
3071 * flags M_WAITOK, M_NOWAIT, M_ZERO.
3074 * NULL if there is no memory and M_NOWAIT is set
3075 * An item if successful
3079 zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags)
3088 if (domain != UMA_ANYDOMAIN) {
3089 /* avoid allocs targeting empty domains */
3090 if (VM_DOMAIN_EMPTY(domain))
3091 domain = UMA_ANYDOMAIN;
3093 if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1)
3095 atomic_add_long(&zone->uz_allocs, 1);
3098 skipdbg = uma_dbg_zskip(zone, item);
3101 * We have to call both the zone's init (not the keg's init)
3102 * and the zone's ctor. This is because the item is going from
3103 * a keg slab directly to the user, and the user is expecting it
3104 * to be both zone-init'd as well as zone-ctor'd.
3106 if (zone->uz_init != NULL) {
3107 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
3108 zone_free_item(zone, item, udata, SKIP_FINI);
3112 if (zone->uz_ctor != NULL &&
3114 (!skipdbg || zone->uz_ctor != trash_ctor ||
3115 zone->uz_dtor != trash_dtor) &&
3117 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
3118 zone_free_item(zone, item, udata, SKIP_DTOR);
3123 uma_dbg_alloc(zone, NULL, item);
3126 uma_zero_item(item, zone);
3128 CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
3129 zone->uz_name, zone);
3134 CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
3135 zone->uz_name, zone);
3136 atomic_add_long(&zone->uz_fails, 1);
3142 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
3145 uma_bucket_t bucket;
3146 uma_zone_domain_t zdom;
3147 int cpu, domain, lockfail;
3152 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3153 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3155 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
3158 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3159 ("uma_zfree_arg: called with spinlock or critical section held"));
3161 /* uma_zfree(..., NULL) does nothing, to match free(9). */
3164 #ifdef DEBUG_MEMGUARD
3165 if (is_memguard_addr(item)) {
3166 if (zone->uz_dtor != NULL)
3167 zone->uz_dtor(item, zone->uz_size, udata);
3168 if (zone->uz_fini != NULL)
3169 zone->uz_fini(item, zone->uz_size);
3170 memguard_free(item);
3175 skipdbg = uma_dbg_zskip(zone, item);
3176 if (skipdbg == false) {
3177 if (zone->uz_flags & UMA_ZONE_MALLOC)
3178 uma_dbg_free(zone, udata, item);
3180 uma_dbg_free(zone, NULL, item);
3182 if (zone->uz_dtor != NULL && (!skipdbg ||
3183 zone->uz_dtor != trash_dtor || zone->uz_ctor != trash_ctor))
3185 if (zone->uz_dtor != NULL)
3187 zone->uz_dtor(item, zone->uz_size, udata);
3190 * The race here is acceptable. If we miss it we'll just have to wait
3191 * a little longer for the limits to be reset.
3193 if (zone->uz_flags & UMA_ZFLAG_FULL)
3197 * If possible, free to the per-CPU cache. There are two
3198 * requirements for safe access to the per-CPU cache: (1) the thread
3199 * accessing the cache must not be preempted or yield during access,
3200 * and (2) the thread must not migrate CPUs without switching which
3201 * cache it accesses. We rely on a critical section to prevent
3202 * preemption and migration. We release the critical section in
3203 * order to acquire the zone mutex if we are unable to free to the
3204 * current cache; when we re-acquire the critical section, we must
3205 * detect and handle migration if it has occurred.
3210 cache = &zone->uz_cpu[cpu];
3214 * Try to free into the allocbucket first to give LIFO ordering
3215 * for cache-hot datastructures. Spill over into the freebucket
3216 * if necessary. Alloc will swap them if one runs dry.
3218 bucket = cache->uc_allocbucket;
3219 if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries)
3220 bucket = cache->uc_freebucket;
3221 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
3222 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
3223 ("uma_zfree: Freeing to non free bucket index."));
3224 bucket->ub_bucket[bucket->ub_cnt] = item;
3232 * We must go back the zone, which requires acquiring the zone lock,
3233 * which in turn means we must release and re-acquire the critical
3234 * section. Since the critical section is released, we may be
3235 * preempted or migrate. As such, make sure not to maintain any
3236 * thread-local state specific to the cache from prior to releasing
3237 * the critical section.
3240 if (zone->uz_count == 0 || bucketdisable)
3244 if (ZONE_TRYLOCK(zone) == 0) {
3245 /* Record contention to size the buckets. */
3251 cache = &zone->uz_cpu[cpu];
3253 bucket = cache->uc_freebucket;
3254 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
3258 cache->uc_freebucket = NULL;
3259 /* We are no longer associated with this CPU. */
3262 if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) {
3263 domain = PCPU_GET(domain);
3264 if (VM_DOMAIN_EMPTY(domain))
3265 domain = UMA_ANYDOMAIN;
3268 zdom = &zone->uz_domain[0];
3270 /* Can we throw this on the zone full list? */
3271 if (bucket != NULL) {
3273 "uma_zfree: zone %s(%p) putting bucket %p on free list",
3274 zone->uz_name, zone, bucket);
3275 /* ub_cnt is pointing to the last free item */
3276 KASSERT(bucket->ub_cnt != 0,
3277 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
3278 if ((zone->uz_flags & UMA_ZONE_NOBUCKETCACHE) != 0) {
3280 bucket_drain(zone, bucket);
3281 bucket_free(zone, bucket, udata);
3284 zone_put_bucket(zone, zdom, bucket, true);
3288 * We bump the uz count when the cache size is insufficient to
3289 * handle the working set.
3291 if (lockfail && zone->uz_count < BUCKET_MAX)
3295 bucket = bucket_alloc(zone, udata, M_NOWAIT);
3296 CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p",
3297 zone->uz_name, zone, bucket);
3301 cache = &zone->uz_cpu[cpu];
3302 if (cache->uc_freebucket == NULL &&
3303 ((zone->uz_flags & UMA_ZONE_NUMA) == 0 ||
3304 domain == PCPU_GET(domain))) {
3305 cache->uc_freebucket = bucket;
3309 * We lost the race, start over. We have to drop our
3310 * critical section to free the bucket.
3313 bucket_free(zone, bucket, udata);
3318 * If nothing else caught this, we'll just do an internal free.
3321 zone_free_item(zone, item, udata, SKIP_DTOR);
3327 uma_zfree_domain(uma_zone_t zone, void *item, void *udata)
3330 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3331 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3333 CTR2(KTR_UMA, "uma_zfree_domain thread %x zone %s", curthread,
3336 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3337 ("uma_zfree_domain: called with spinlock or critical section held"));
3339 /* uma_zfree(..., NULL) does nothing, to match free(9). */
3342 zone_free_item(zone, item, udata, SKIP_NONE);
3346 slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item)
3351 mtx_assert(&keg->uk_lock, MA_OWNED);
3352 MPASS(keg == slab->us_keg);
3354 dom = &keg->uk_domain[slab->us_domain];
3356 /* Do we need to remove from any lists? */
3357 if (slab->us_freecount+1 == keg->uk_ipers) {
3358 LIST_REMOVE(slab, us_link);
3359 LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
3360 } else if (slab->us_freecount == 0) {
3361 LIST_REMOVE(slab, us_link);
3362 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
3365 /* Slab management. */
3366 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3367 BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
3368 slab->us_freecount++;
3370 /* Keg statistics. */
3375 zone_release(uma_zone_t zone, void **bucket, int cnt)
3385 keg = zone_first_keg(zone);
3387 for (i = 0; i < cnt; i++) {
3389 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
3390 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
3391 if (zone->uz_flags & UMA_ZONE_HASH) {
3392 slab = hash_sfind(&keg->uk_hash, mem);
3394 mem += keg->uk_pgoff;
3395 slab = (uma_slab_t)mem;
3398 slab = vtoslab((vm_offset_t)item);
3399 if (slab->us_keg != keg) {
3405 slab_free_item(keg, slab, item);
3406 if (keg->uk_flags & UMA_ZFLAG_FULL) {
3407 if (keg->uk_pages < keg->uk_maxpages) {
3408 keg->uk_flags &= ~UMA_ZFLAG_FULL;
3413 * We can handle one more allocation. Since we're
3414 * clearing ZFLAG_FULL, wake up all procs blocked
3415 * on pages. This should be uncommon, so keeping this
3416 * simple for now (rather than adding count of blocked
3425 zone->uz_flags &= ~UMA_ZFLAG_FULL;
3433 * Frees a single item to any zone.
3436 * zone The zone to free to
3437 * item The item we're freeing
3438 * udata User supplied data for the dtor
3439 * skip Skip dtors and finis
3442 zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
3447 skipdbg = uma_dbg_zskip(zone, item);
3448 if (skip == SKIP_NONE && !skipdbg) {
3449 if (zone->uz_flags & UMA_ZONE_MALLOC)
3450 uma_dbg_free(zone, udata, item);
3452 uma_dbg_free(zone, NULL, item);
3455 if (skip < SKIP_DTOR && zone->uz_dtor != NULL &&
3456 (!skipdbg || zone->uz_dtor != trash_dtor ||
3457 zone->uz_ctor != trash_ctor))
3459 if (skip < SKIP_DTOR && zone->uz_dtor != NULL)
3461 zone->uz_dtor(item, zone->uz_size, udata);
3463 if (skip < SKIP_FINI && zone->uz_fini)
3464 zone->uz_fini(item, zone->uz_size);
3466 atomic_add_long(&zone->uz_frees, 1);
3467 zone->uz_release(zone->uz_arg, &item, 1);
3472 uma_zone_set_max(uma_zone_t zone, int nitems)
3476 keg = zone_first_keg(zone);
3480 keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
3481 if (keg->uk_maxpages * keg->uk_ipers < nitems)
3482 keg->uk_maxpages += keg->uk_ppera;
3483 nitems = (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers;
3491 uma_zone_get_max(uma_zone_t zone)
3496 keg = zone_first_keg(zone);
3500 nitems = (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers;
3508 uma_zone_set_warning(uma_zone_t zone, const char *warning)
3512 zone->uz_warning = warning;
3518 uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
3522 TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
3528 uma_zone_get_cur(uma_zone_t zone)
3534 nitems = zone->uz_allocs - zone->uz_frees;
3537 * See the comment in sysctl_vm_zone_stats() regarding the
3538 * safety of accessing the per-cpu caches. With the zone lock
3539 * held, it is safe, but can potentially result in stale data.
3541 nitems += zone->uz_cpu[i].uc_allocs -
3542 zone->uz_cpu[i].uc_frees;
3546 return (nitems < 0 ? 0 : nitems);
3551 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
3555 keg = zone_first_keg(zone);
3556 KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
3558 KASSERT(keg->uk_pages == 0,
3559 ("uma_zone_set_init on non-empty keg"));
3560 keg->uk_init = uminit;
3566 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
3570 keg = zone_first_keg(zone);
3571 KASSERT(keg != NULL, ("uma_zone_set_fini: Invalid zone type"));
3573 KASSERT(keg->uk_pages == 0,
3574 ("uma_zone_set_fini on non-empty keg"));
3575 keg->uk_fini = fini;
3581 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
3585 KASSERT(zone_first_keg(zone)->uk_pages == 0,
3586 ("uma_zone_set_zinit on non-empty keg"));
3587 zone->uz_init = zinit;
3593 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
3597 KASSERT(zone_first_keg(zone)->uk_pages == 0,
3598 ("uma_zone_set_zfini on non-empty keg"));
3599 zone->uz_fini = zfini;
3604 /* XXX uk_freef is not actually used with the zone locked */
3606 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
3610 keg = zone_first_keg(zone);
3611 KASSERT(keg != NULL, ("uma_zone_set_freef: Invalid zone type"));
3613 keg->uk_freef = freef;
3618 /* XXX uk_allocf is not actually used with the zone locked */
3620 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
3624 keg = zone_first_keg(zone);
3626 keg->uk_allocf = allocf;
3632 uma_zone_reserve(uma_zone_t zone, int items)
3636 keg = zone_first_keg(zone);
3640 keg->uk_reserve = items;
3648 uma_zone_reserve_kva(uma_zone_t zone, int count)
3654 keg = zone_first_keg(zone);
3657 pages = count / keg->uk_ipers;
3659 if (pages * keg->uk_ipers < count)
3661 pages *= keg->uk_ppera;
3663 #ifdef UMA_MD_SMALL_ALLOC
3664 if (keg->uk_ppera > 1) {
3668 kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
3676 keg->uk_maxpages = pages;
3677 #ifdef UMA_MD_SMALL_ALLOC
3678 keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
3680 keg->uk_allocf = noobj_alloc;
3682 keg->uk_flags |= UMA_ZONE_NOFREE;
3690 uma_prealloc(uma_zone_t zone, int items)
3692 struct vm_domainset_iter di;
3696 int aflags, domain, slabs;
3698 keg = zone_first_keg(zone);
3702 slabs = items / keg->uk_ipers;
3703 if (slabs * keg->uk_ipers < items)
3705 while (slabs-- > 0) {
3707 vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
3710 slab = keg_alloc_slab(keg, zone, domain, M_WAITOK,
3713 MPASS(slab->us_keg == keg);
3714 dom = &keg->uk_domain[slab->us_domain];
3715 LIST_INSERT_HEAD(&dom->ud_free_slab, slab,
3720 if (vm_domainset_iter_policy(&di, &domain) != 0) {
3722 vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask, 0);
3732 uma_reclaim_locked(bool kmem_danger)
3735 CTR0(KTR_UMA, "UMA: vm asked us to release pages!");
3736 sx_assert(&uma_drain_lock, SA_XLOCKED);
3738 zone_foreach(zone_drain);
3739 if (vm_page_count_min() || kmem_danger) {
3740 cache_drain_safe(NULL);
3741 zone_foreach(zone_drain);
3745 * Some slabs may have been freed but this zone will be visited early
3746 * we visit again so that we can free pages that are empty once other
3747 * zones are drained. We have to do the same for buckets.
3749 zone_drain(slabzone);
3750 bucket_zone_drain();
3757 sx_xlock(&uma_drain_lock);
3758 uma_reclaim_locked(false);
3759 sx_xunlock(&uma_drain_lock);
3762 static volatile int uma_reclaim_needed;
3765 uma_reclaim_wakeup(void)
3768 if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
3769 wakeup(uma_reclaim);
3773 uma_reclaim_worker(void *arg __unused)
3777 sx_xlock(&uma_drain_lock);
3778 while (atomic_load_int(&uma_reclaim_needed) == 0)
3779 sx_sleep(uma_reclaim, &uma_drain_lock, PVM, "umarcl",
3781 sx_xunlock(&uma_drain_lock);
3782 EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
3783 sx_xlock(&uma_drain_lock);
3784 uma_reclaim_locked(true);
3785 atomic_store_int(&uma_reclaim_needed, 0);
3786 sx_xunlock(&uma_drain_lock);
3787 /* Don't fire more than once per-second. */
3788 pause("umarclslp", hz);
3794 uma_zone_exhausted(uma_zone_t zone)
3799 full = (zone->uz_flags & UMA_ZFLAG_FULL);
3805 uma_zone_exhausted_nolock(uma_zone_t zone)
3807 return (zone->uz_flags & UMA_ZFLAG_FULL);
3811 uma_large_malloc_domain(vm_size_t size, int domain, int wait)
3813 struct domainset *policy;
3817 if (domain != UMA_ANYDOMAIN) {
3818 /* avoid allocs targeting empty domains */
3819 if (VM_DOMAIN_EMPTY(domain))
3820 domain = UMA_ANYDOMAIN;
3822 slab = zone_alloc_item(slabzone, NULL, domain, wait);
3825 policy = (domain == UMA_ANYDOMAIN) ? DOMAINSET_RR() :
3826 DOMAINSET_FIXED(domain);
3827 addr = kmem_malloc_domainset(policy, size, wait);
3829 vsetslab(addr, slab);
3830 slab->us_data = (void *)addr;
3831 slab->us_flags = UMA_SLAB_KERNEL | UMA_SLAB_MALLOC;
3832 slab->us_size = size;
3833 slab->us_domain = vm_phys_domain(PHYS_TO_VM_PAGE(
3834 pmap_kextract(addr)));
3835 uma_total_inc(size);
3837 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3840 return ((void *)addr);
3844 uma_large_malloc(vm_size_t size, int wait)
3847 return uma_large_malloc_domain(size, UMA_ANYDOMAIN, wait);
3851 uma_large_free(uma_slab_t slab)
3854 KASSERT((slab->us_flags & UMA_SLAB_KERNEL) != 0,
3855 ("uma_large_free: Memory not allocated with uma_large_malloc."));
3856 kmem_free((vm_offset_t)slab->us_data, slab->us_size);
3857 uma_total_dec(slab->us_size);
3858 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3862 uma_zero_item(void *item, uma_zone_t zone)
3865 bzero(item, zone->uz_size);
3872 return (uma_kmem_limit);
3876 uma_set_limit(unsigned long limit)
3879 uma_kmem_limit = limit;
3886 return (atomic_load_long(&uma_kmem_total));
3893 return (uma_kmem_limit - uma_size());
3897 uma_print_stats(void)
3899 zone_foreach(uma_print_zone);
3903 slab_print(uma_slab_t slab)
3905 printf("slab: keg %p, data %p, freecount %d\n",
3906 slab->us_keg, slab->us_data, slab->us_freecount);
3910 cache_print(uma_cache_t cache)
3912 printf("alloc: %p(%d), free: %p(%d)\n",
3913 cache->uc_allocbucket,
3914 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3915 cache->uc_freebucket,
3916 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3920 uma_print_keg(uma_keg_t keg)
3926 printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3927 "out %d free %d limit %d\n",
3928 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3929 keg->uk_ipers, keg->uk_ppera,
3930 (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
3931 keg->uk_free, (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3932 for (i = 0; i < vm_ndomains; i++) {
3933 dom = &keg->uk_domain[i];
3934 printf("Part slabs:\n");
3935 LIST_FOREACH(slab, &dom->ud_part_slab, us_link)
3937 printf("Free slabs:\n");
3938 LIST_FOREACH(slab, &dom->ud_free_slab, us_link)
3940 printf("Full slabs:\n");
3941 LIST_FOREACH(slab, &dom->ud_full_slab, us_link)
3947 uma_print_zone(uma_zone_t zone)
3953 printf("zone: %s(%p) size %d flags %#x\n",
3954 zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3955 LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3956 uma_print_keg(kl->kl_keg);
3958 cache = &zone->uz_cpu[i];
3959 printf("CPU %d Cache:\n", i);
3966 * Generate statistics across both the zone and its per-cpu cache's. Return
3967 * desired statistics if the pointer is non-NULL for that statistic.
3969 * Note: does not update the zone statistics, as it can't safely clear the
3970 * per-CPU cache statistic.
3972 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3973 * safe from off-CPU; we should modify the caches to track this information
3974 * directly so that we don't have to.
3977 uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp,
3978 uint64_t *freesp, uint64_t *sleepsp)
3981 uint64_t allocs, frees, sleeps;
3984 allocs = frees = sleeps = 0;
3987 cache = &z->uz_cpu[cpu];
3988 if (cache->uc_allocbucket != NULL)
3989 cachefree += cache->uc_allocbucket->ub_cnt;
3990 if (cache->uc_freebucket != NULL)
3991 cachefree += cache->uc_freebucket->ub_cnt;
3992 allocs += cache->uc_allocs;
3993 frees += cache->uc_frees;
3995 allocs += z->uz_allocs;
3996 frees += z->uz_frees;
3997 sleeps += z->uz_sleeps;
3998 if (cachefreep != NULL)
3999 *cachefreep = cachefree;
4000 if (allocsp != NULL)
4004 if (sleepsp != NULL)
4010 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
4017 rw_rlock(&uma_rwlock);
4018 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4019 LIST_FOREACH(z, &kz->uk_zones, uz_link)
4022 rw_runlock(&uma_rwlock);
4023 return (sysctl_handle_int(oidp, &count, 0, req));
4027 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
4029 struct uma_stream_header ush;
4030 struct uma_type_header uth;
4031 struct uma_percpu_stat *ups;
4032 uma_zone_domain_t zdom;
4039 int count, error, i;
4041 error = sysctl_wire_old_buffer(req, 0);
4044 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
4045 sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
4046 ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK);
4049 rw_rlock(&uma_rwlock);
4050 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4051 LIST_FOREACH(z, &kz->uk_zones, uz_link)
4056 * Insert stream header.
4058 bzero(&ush, sizeof(ush));
4059 ush.ush_version = UMA_STREAM_VERSION;
4060 ush.ush_maxcpus = (mp_maxid + 1);
4061 ush.ush_count = count;
4062 (void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
4064 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4065 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
4066 bzero(&uth, sizeof(uth));
4068 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
4069 uth.uth_align = kz->uk_align;
4070 uth.uth_size = kz->uk_size;
4071 uth.uth_rsize = kz->uk_rsize;
4072 LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
4074 uth.uth_maxpages += k->uk_maxpages;
4075 uth.uth_pages += k->uk_pages;
4076 uth.uth_keg_free += k->uk_free;
4077 uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
4082 * A zone is secondary is it is not the first entry
4083 * on the keg's zone list.
4085 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
4086 (LIST_FIRST(&kz->uk_zones) != z))
4087 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
4089 for (i = 0; i < vm_ndomains; i++) {
4090 zdom = &z->uz_domain[i];
4091 uth.uth_zone_free += zdom->uzd_nitems;
4093 uth.uth_allocs = z->uz_allocs;
4094 uth.uth_frees = z->uz_frees;
4095 uth.uth_fails = z->uz_fails;
4096 uth.uth_sleeps = z->uz_sleeps;
4098 * While it is not normally safe to access the cache
4099 * bucket pointers while not on the CPU that owns the
4100 * cache, we only allow the pointers to be exchanged
4101 * without the zone lock held, not invalidated, so
4102 * accept the possible race associated with bucket
4103 * exchange during monitoring.
4105 for (i = 0; i < mp_maxid + 1; i++) {
4106 bzero(&ups[i], sizeof(*ups));
4107 if (kz->uk_flags & UMA_ZFLAG_INTERNAL ||
4110 cache = &z->uz_cpu[i];
4111 if (cache->uc_allocbucket != NULL)
4112 ups[i].ups_cache_free +=
4113 cache->uc_allocbucket->ub_cnt;
4114 if (cache->uc_freebucket != NULL)
4115 ups[i].ups_cache_free +=
4116 cache->uc_freebucket->ub_cnt;
4117 ups[i].ups_allocs = cache->uc_allocs;
4118 ups[i].ups_frees = cache->uc_frees;
4121 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
4122 for (i = 0; i < mp_maxid + 1; i++)
4123 (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
4126 rw_runlock(&uma_rwlock);
4127 error = sbuf_finish(&sbuf);
4134 sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
4136 uma_zone_t zone = *(uma_zone_t *)arg1;
4139 max = uma_zone_get_max(zone);
4140 error = sysctl_handle_int(oidp, &max, 0, req);
4141 if (error || !req->newptr)
4144 uma_zone_set_max(zone, max);
4150 sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
4152 uma_zone_t zone = *(uma_zone_t *)arg1;
4155 cur = uma_zone_get_cur(zone);
4156 return (sysctl_handle_int(oidp, &cur, 0, req));
4161 uma_dbg_getslab(uma_zone_t zone, void *item)
4167 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
4168 if (zone->uz_flags & UMA_ZONE_VTOSLAB) {
4169 slab = vtoslab((vm_offset_t)mem);
4172 * It is safe to return the slab here even though the
4173 * zone is unlocked because the item's allocation state
4174 * essentially holds a reference.
4177 keg = LIST_FIRST(&zone->uz_kegs)->kl_keg;
4178 if (keg->uk_flags & UMA_ZONE_HASH)
4179 slab = hash_sfind(&keg->uk_hash, mem);
4181 slab = (uma_slab_t)(mem + keg->uk_pgoff);
4189 uma_dbg_zskip(uma_zone_t zone, void *mem)
4193 if ((keg = zone_first_keg(zone)) == NULL)
4196 return (uma_dbg_kskip(keg, mem));
4200 uma_dbg_kskip(uma_keg_t keg, void *mem)
4204 if (dbg_divisor == 0)
4207 if (dbg_divisor == 1)
4210 idx = (uintptr_t)mem >> PAGE_SHIFT;
4211 if (keg->uk_ipers > 1) {
4212 idx *= keg->uk_ipers;
4213 idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize;
4216 if ((idx / dbg_divisor) * dbg_divisor != idx) {
4217 counter_u64_add(uma_skip_cnt, 1);
4220 counter_u64_add(uma_dbg_cnt, 1);
4226 * Set up the slab's freei data such that uma_dbg_free can function.
4230 uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
4236 slab = uma_dbg_getslab(zone, item);
4238 panic("uma: item %p did not belong to zone %s\n",
4239 item, zone->uz_name);
4242 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
4244 if (BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
4245 panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
4246 item, zone, zone->uz_name, slab, freei);
4247 BIT_SET_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
4253 * Verifies freed addresses. Checks for alignment, valid slab membership
4254 * and duplicate frees.
4258 uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
4264 slab = uma_dbg_getslab(zone, item);
4266 panic("uma: Freed item %p did not belong to zone %s\n",
4267 item, zone->uz_name);
4270 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
4272 if (freei >= keg->uk_ipers)
4273 panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
4274 item, zone, zone->uz_name, slab, freei);
4276 if (((freei * keg->uk_rsize) + slab->us_data) != item)
4277 panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
4278 item, zone, zone->uz_name, slab, freei);
4280 if (!BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
4281 panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
4282 item, zone, zone->uz_name, slab, freei);
4284 BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
4286 #endif /* INVARIANTS */
4289 DB_SHOW_COMMAND(uma, db_show_uma)
4293 uint64_t allocs, frees, sleeps;
4297 db_printf("%18s %8s %8s %8s %12s %8s %8s\n", "Zone", "Size", "Used",
4298 "Free", "Requests", "Sleeps", "Bucket");
4299 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4300 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
4301 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
4302 allocs = z->uz_allocs;
4303 frees = z->uz_frees;
4304 sleeps = z->uz_sleeps;
4307 uma_zone_sumstat(z, &cachefree, &allocs,
4309 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
4310 (LIST_FIRST(&kz->uk_zones) != z)))
4311 cachefree += kz->uk_free;
4312 for (i = 0; i < vm_ndomains; i++)
4313 cachefree += z->uz_domain[i].uzd_nitems;
4315 db_printf("%18s %8ju %8jd %8ld %12ju %8ju %8u\n",
4316 z->uz_name, (uintmax_t)kz->uk_size,
4317 (intmax_t)(allocs - frees), cachefree,
4318 (uintmax_t)allocs, sleeps, z->uz_count);
4325 DB_SHOW_COMMAND(umacache, db_show_umacache)
4328 uint64_t allocs, frees;
4332 db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
4333 "Requests", "Bucket");
4334 LIST_FOREACH(z, &uma_cachezones, uz_link) {
4335 uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL);
4336 for (i = 0; i < vm_ndomains; i++)
4337 cachefree += z->uz_domain[i].uzd_nitems;
4338 db_printf("%18s %8ju %8jd %8ld %12ju %8u\n",
4339 z->uz_name, (uintmax_t)z->uz_size,
4340 (intmax_t)(allocs - frees), cachefree,
4341 (uintmax_t)allocs, z->uz_count);