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. */
1785 zone->uz_domain = (struct uma_zone_domain *)&zone->uz_cpu[mp_ncpus];
1786 timevalclear(&zone->uz_ratecheck);
1789 ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
1792 * This is a pure cache zone, no kegs.
1795 if (arg->flags & UMA_ZONE_VM)
1796 arg->flags |= UMA_ZFLAG_CACHEONLY;
1797 zone->uz_flags = arg->flags;
1798 zone->uz_size = arg->size;
1799 zone->uz_import = arg->import;
1800 zone->uz_release = arg->release;
1801 zone->uz_arg = arg->arg;
1802 zone->uz_lockptr = &zone->uz_lock;
1803 rw_wlock(&uma_rwlock);
1804 LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
1805 rw_wunlock(&uma_rwlock);
1810 * Use the regular zone/keg/slab allocator.
1812 zone->uz_import = (uma_import)zone_import;
1813 zone->uz_release = (uma_release)zone_release;
1814 zone->uz_arg = zone;
1816 if (arg->flags & UMA_ZONE_SECONDARY) {
1817 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1818 zone->uz_init = arg->uminit;
1819 zone->uz_fini = arg->fini;
1820 zone->uz_lockptr = &keg->uk_lock;
1821 zone->uz_flags |= UMA_ZONE_SECONDARY;
1822 rw_wlock(&uma_rwlock);
1824 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1825 if (LIST_NEXT(z, uz_link) == NULL) {
1826 LIST_INSERT_AFTER(z, zone, uz_link);
1831 rw_wunlock(&uma_rwlock);
1832 } else if (keg == NULL) {
1833 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1834 arg->align, arg->flags)) == NULL)
1837 struct uma_kctor_args karg;
1840 /* We should only be here from uma_startup() */
1841 karg.size = arg->size;
1842 karg.uminit = arg->uminit;
1843 karg.fini = arg->fini;
1844 karg.align = arg->align;
1845 karg.flags = arg->flags;
1847 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1854 * Link in the first keg.
1856 zone->uz_klink.kl_keg = keg;
1857 LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1858 zone->uz_lockptr = &keg->uk_lock;
1859 zone->uz_size = keg->uk_size;
1860 zone->uz_flags |= (keg->uk_flags &
1861 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1864 * Some internal zones don't have room allocated for the per cpu
1865 * caches. If we're internal, bail out here.
1867 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1868 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1869 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1874 KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) !=
1875 (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET),
1876 ("Invalid zone flag combination"));
1877 if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0)
1878 zone->uz_count = BUCKET_MAX;
1879 else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0)
1882 zone->uz_count = bucket_select(zone->uz_size);
1883 zone->uz_count_min = zone->uz_count;
1889 * Keg header dtor. This frees all data, destroys locks, frees the hash
1890 * table and removes the keg from the global list.
1892 * Arguments/Returns follow uma_dtor specifications
1896 keg_dtor(void *arg, int size, void *udata)
1900 keg = (uma_keg_t)arg;
1902 if (keg->uk_free != 0) {
1903 printf("Freed UMA keg (%s) was not empty (%d items). "
1904 " Lost %d pages of memory.\n",
1905 keg->uk_name ? keg->uk_name : "",
1906 keg->uk_free, keg->uk_pages);
1910 hash_free(&keg->uk_hash);
1918 * Arguments/Returns follow uma_dtor specifications
1922 zone_dtor(void *arg, int size, void *udata)
1928 zone = (uma_zone_t)arg;
1929 keg = zone_first_keg(zone);
1931 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1934 rw_wlock(&uma_rwlock);
1935 LIST_REMOVE(zone, uz_link);
1936 rw_wunlock(&uma_rwlock);
1938 * XXX there are some races here where
1939 * the zone can be drained but zone lock
1940 * released and then refilled before we
1941 * remove it... we dont care for now
1943 zone_drain_wait(zone, M_WAITOK);
1945 * Unlink all of our kegs.
1947 while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1948 klink->kl_keg = NULL;
1949 LIST_REMOVE(klink, kl_link);
1950 if (klink == &zone->uz_klink)
1952 free(klink, M_TEMP);
1955 * We only destroy kegs from non secondary zones.
1957 if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0) {
1958 rw_wlock(&uma_rwlock);
1959 LIST_REMOVE(keg, uk_link);
1960 rw_wunlock(&uma_rwlock);
1961 zone_free_item(kegs, keg, NULL, SKIP_NONE);
1963 ZONE_LOCK_FINI(zone);
1967 * Traverses every zone in the system and calls a callback
1970 * zfunc A pointer to a function which accepts a zone
1977 zone_foreach(void (*zfunc)(uma_zone_t))
1982 rw_rlock(&uma_rwlock);
1983 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1984 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1987 rw_runlock(&uma_rwlock);
1991 * Count how many pages do we need to bootstrap. VM supplies
1992 * its need in early zones in the argument, we add up our zones,
1993 * which consist of: UMA Slabs, UMA Hash and 9 Bucket zones. The
1994 * zone of zones and zone of kegs are accounted separately.
1996 #define UMA_BOOT_ZONES 11
1997 /* Zone of zones and zone of kegs have arbitrary alignment. */
1998 #define UMA_BOOT_ALIGN 32
1999 static int zsize, ksize;
2001 uma_startup_count(int vm_zones)
2005 ksize = sizeof(struct uma_keg) +
2006 (sizeof(struct uma_domain) * vm_ndomains);
2007 zsize = sizeof(struct uma_zone) +
2008 (sizeof(struct uma_cache) * (mp_maxid + 1)) +
2009 (sizeof(struct uma_zone_domain) * vm_ndomains);
2012 * Memory for the zone of kegs and its keg,
2013 * and for zone of zones.
2015 pages = howmany(roundup(zsize, CACHE_LINE_SIZE) * 2 +
2016 roundup(ksize, CACHE_LINE_SIZE), PAGE_SIZE);
2018 #ifdef UMA_MD_SMALL_ALLOC
2019 zones = UMA_BOOT_ZONES;
2021 zones = UMA_BOOT_ZONES + vm_zones;
2025 /* Memory for the rest of startup zones, UMA and VM, ... */
2026 if (zsize > UMA_SLAB_SPACE)
2027 pages += (zones + vm_zones) *
2028 howmany(roundup2(zsize, UMA_BOOT_ALIGN), UMA_SLAB_SIZE);
2029 else if (roundup2(zsize, UMA_BOOT_ALIGN) > UMA_SLAB_SPACE)
2032 pages += howmany(zones,
2033 UMA_SLAB_SPACE / roundup2(zsize, UMA_BOOT_ALIGN));
2035 /* ... and their kegs. Note that zone of zones allocates a keg! */
2036 pages += howmany(zones + 1,
2037 UMA_SLAB_SPACE / roundup2(ksize, UMA_BOOT_ALIGN));
2040 * Most of startup zones are not going to be offpages, that's
2041 * why we use UMA_SLAB_SPACE instead of UMA_SLAB_SIZE in all
2042 * calculations. Some large bucket zones will be offpage, and
2043 * thus will allocate hashes. We take conservative approach
2044 * and assume that all zones may allocate hash. This may give
2045 * us some positive inaccuracy, usually an extra single page.
2047 pages += howmany(zones, UMA_SLAB_SPACE /
2048 (sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT));
2054 uma_startup(void *mem, int npages)
2056 struct uma_zctor_args args;
2057 uma_keg_t masterkeg;
2061 printf("Entering %s with %d boot pages configured\n", __func__, npages);
2064 rw_init(&uma_rwlock, "UMA lock");
2066 /* Use bootpages memory for the zone of zones and zone of kegs. */
2068 zones = (uma_zone_t)m;
2069 m += roundup(zsize, CACHE_LINE_SIZE);
2070 kegs = (uma_zone_t)m;
2071 m += roundup(zsize, CACHE_LINE_SIZE);
2072 masterkeg = (uma_keg_t)m;
2073 m += roundup(ksize, CACHE_LINE_SIZE);
2074 m = roundup(m, PAGE_SIZE);
2075 npages -= (m - (uintptr_t)mem) / PAGE_SIZE;
2078 /* "manually" create the initial zone */
2079 memset(&args, 0, sizeof(args));
2080 args.name = "UMA Kegs";
2082 args.ctor = keg_ctor;
2083 args.dtor = keg_dtor;
2084 args.uminit = zero_init;
2086 args.keg = masterkeg;
2087 args.align = UMA_BOOT_ALIGN - 1;
2088 args.flags = UMA_ZFLAG_INTERNAL;
2089 zone_ctor(kegs, zsize, &args, M_WAITOK);
2092 boot_pages = npages;
2094 args.name = "UMA Zones";
2096 args.ctor = zone_ctor;
2097 args.dtor = zone_dtor;
2098 args.uminit = zero_init;
2101 args.align = UMA_BOOT_ALIGN - 1;
2102 args.flags = UMA_ZFLAG_INTERNAL;
2103 zone_ctor(zones, zsize, &args, M_WAITOK);
2105 /* Now make a zone for slab headers */
2106 slabzone = uma_zcreate("UMA Slabs",
2107 sizeof(struct uma_slab),
2108 NULL, NULL, NULL, NULL,
2109 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2111 hashzone = uma_zcreate("UMA Hash",
2112 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
2113 NULL, NULL, NULL, NULL,
2114 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2118 booted = BOOT_STRAPPED;
2126 printf("Entering %s with %d boot pages left\n", __func__, boot_pages);
2128 booted = BOOT_PAGEALLOC;
2136 printf("Entering %s with %d boot pages left\n", __func__, boot_pages);
2138 booted = BOOT_BUCKETS;
2139 sx_init(&uma_drain_lock, "umadrain");
2148 TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor);
2149 uma_dbg_cnt = counter_u64_alloc(M_WAITOK);
2150 uma_skip_cnt = counter_u64_alloc(M_WAITOK);
2152 callout_init(&uma_callout, 1);
2153 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
2154 booted = BOOT_RUNNING;
2156 EVENTHANDLER_REGISTER(shutdown_post_sync, uma_shutdown, NULL,
2157 EVENTHANDLER_PRI_FIRST);
2164 booted = BOOT_SHUTDOWN;
2168 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
2169 int align, uint32_t flags)
2171 struct uma_kctor_args args;
2174 args.uminit = uminit;
2176 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
2179 return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK));
2182 /* Public functions */
2185 uma_set_align(int align)
2188 if (align != UMA_ALIGN_CACHE)
2189 uma_align_cache = align;
2194 uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
2195 uma_init uminit, uma_fini fini, int align, uint32_t flags)
2198 struct uma_zctor_args args;
2202 KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
2205 /* This stuff is essential for the zone ctor */
2206 memset(&args, 0, sizeof(args));
2211 args.uminit = uminit;
2215 * If a zone is being created with an empty constructor and
2216 * destructor, pass UMA constructor/destructor which checks for
2217 * memory use after free.
2219 if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOFREE))) &&
2220 ctor == NULL && dtor == NULL && uminit == NULL && fini == NULL) {
2221 args.ctor = trash_ctor;
2222 args.dtor = trash_dtor;
2223 args.uminit = trash_init;
2224 args.fini = trash_fini;
2231 if (booted < BOOT_BUCKETS) {
2234 sx_slock(&uma_drain_lock);
2237 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
2239 sx_sunlock(&uma_drain_lock);
2245 uma_zsecond_create(const char *name, uma_ctor ctor, uma_dtor dtor,
2246 uma_init zinit, uma_fini zfini, uma_zone_t master)
2248 struct uma_zctor_args args;
2253 keg = zone_first_keg(master);
2254 memset(&args, 0, sizeof(args));
2256 args.size = keg->uk_size;
2259 args.uminit = zinit;
2261 args.align = keg->uk_align;
2262 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
2265 if (booted < BOOT_BUCKETS) {
2268 sx_slock(&uma_drain_lock);
2271 /* XXX Attaches only one keg of potentially many. */
2272 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
2274 sx_sunlock(&uma_drain_lock);
2280 uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor,
2281 uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease,
2282 void *arg, int flags)
2284 struct uma_zctor_args args;
2286 memset(&args, 0, sizeof(args));
2291 args.uminit = zinit;
2293 args.import = zimport;
2294 args.release = zrelease;
2299 return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK));
2303 zone_lock_pair(uma_zone_t a, uma_zone_t b)
2307 mtx_lock_flags(b->uz_lockptr, MTX_DUPOK);
2310 mtx_lock_flags(a->uz_lockptr, MTX_DUPOK);
2315 zone_unlock_pair(uma_zone_t a, uma_zone_t b)
2323 uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
2330 klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
2332 zone_lock_pair(zone, master);
2334 * zone must use vtoslab() to resolve objects and must already be
2337 if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
2338 != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
2343 * The new master must also use vtoslab().
2345 if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
2351 * The underlying object must be the same size. rsize
2354 if (master->uz_size != zone->uz_size) {
2359 * Put it at the end of the list.
2361 klink->kl_keg = zone_first_keg(master);
2362 LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
2363 if (LIST_NEXT(kl, kl_link) == NULL) {
2364 LIST_INSERT_AFTER(kl, klink, kl_link);
2369 zone->uz_flags |= UMA_ZFLAG_MULTI;
2370 zone->uz_slab = zone_fetch_slab_multi;
2373 zone_unlock_pair(zone, master);
2375 free(klink, M_TEMP);
2383 uma_zdestroy(uma_zone_t zone)
2387 * Large slabs are expensive to reclaim, so don't bother doing
2388 * unnecessary work if we're shutting down.
2390 if (booted == BOOT_SHUTDOWN &&
2391 zone->uz_fini == NULL &&
2392 zone->uz_release == (uma_release)zone_release)
2394 sx_slock(&uma_drain_lock);
2395 zone_free_item(zones, zone, NULL, SKIP_NONE);
2396 sx_sunlock(&uma_drain_lock);
2400 uma_zwait(uma_zone_t zone)
2404 item = uma_zalloc_arg(zone, NULL, M_WAITOK);
2405 uma_zfree(zone, item);
2409 uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags)
2415 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2417 item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO);
2418 if (item != NULL && (flags & M_ZERO)) {
2420 for (i = 0; i <= mp_maxid; i++)
2421 bzero(zpcpu_get_cpu(item, i), zone->uz_size);
2423 bzero(item, zone->uz_size);
2430 * A stub while both regular and pcpu cases are identical.
2433 uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata)
2437 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2439 uma_zfree_arg(zone, item, udata);
2444 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
2446 uma_zone_domain_t zdom;
2447 uma_bucket_t bucket;
2450 int cpu, domain, lockfail;
2455 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2456 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
2458 /* This is the fast path allocation */
2459 CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d",
2460 curthread, zone->uz_name, zone, flags);
2462 if (flags & M_WAITOK) {
2463 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2464 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
2466 KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC"));
2467 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2468 ("uma_zalloc_arg: called with spinlock or critical section held"));
2469 if (zone->uz_flags & UMA_ZONE_PCPU)
2470 KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone "
2471 "with M_ZERO passed"));
2473 #ifdef DEBUG_MEMGUARD
2474 if (memguard_cmp_zone(zone)) {
2475 item = memguard_alloc(zone->uz_size, flags);
2477 if (zone->uz_init != NULL &&
2478 zone->uz_init(item, zone->uz_size, flags) != 0)
2480 if (zone->uz_ctor != NULL &&
2481 zone->uz_ctor(item, zone->uz_size, udata,
2483 zone->uz_fini(item, zone->uz_size);
2488 /* This is unfortunate but should not be fatal. */
2492 * If possible, allocate from the per-CPU cache. There are two
2493 * requirements for safe access to the per-CPU cache: (1) the thread
2494 * accessing the cache must not be preempted or yield during access,
2495 * and (2) the thread must not migrate CPUs without switching which
2496 * cache it accesses. We rely on a critical section to prevent
2497 * preemption and migration. We release the critical section in
2498 * order to acquire the zone mutex if we are unable to allocate from
2499 * the current cache; when we re-acquire the critical section, we
2500 * must detect and handle migration if it has occurred.
2505 cache = &zone->uz_cpu[cpu];
2508 bucket = cache->uc_allocbucket;
2509 if (bucket != NULL && bucket->ub_cnt > 0) {
2511 item = bucket->ub_bucket[bucket->ub_cnt];
2513 bucket->ub_bucket[bucket->ub_cnt] = NULL;
2515 KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
2519 skipdbg = uma_dbg_zskip(zone, item);
2521 if (zone->uz_ctor != NULL &&
2523 (!skipdbg || zone->uz_ctor != trash_ctor ||
2524 zone->uz_dtor != trash_dtor) &&
2526 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2527 atomic_add_long(&zone->uz_fails, 1);
2528 zone_free_item(zone, item, udata, SKIP_DTOR);
2533 uma_dbg_alloc(zone, NULL, item);
2536 uma_zero_item(item, zone);
2541 * We have run out of items in our alloc bucket.
2542 * See if we can switch with our free bucket.
2544 bucket = cache->uc_freebucket;
2545 if (bucket != NULL && bucket->ub_cnt > 0) {
2547 "uma_zalloc: zone %s(%p) swapping empty with alloc",
2548 zone->uz_name, zone);
2549 cache->uc_freebucket = cache->uc_allocbucket;
2550 cache->uc_allocbucket = bucket;
2555 * Discard any empty allocation bucket while we hold no locks.
2557 bucket = cache->uc_allocbucket;
2558 cache->uc_allocbucket = NULL;
2561 bucket_free(zone, bucket, udata);
2563 if (zone->uz_flags & UMA_ZONE_NUMA) {
2564 domain = PCPU_GET(domain);
2565 if (VM_DOMAIN_EMPTY(domain))
2566 domain = UMA_ANYDOMAIN;
2568 domain = UMA_ANYDOMAIN;
2570 /* Short-circuit for zones without buckets and low memory. */
2571 if (zone->uz_count == 0 || bucketdisable)
2575 * Attempt to retrieve the item from the per-CPU cache has failed, so
2576 * we must go back to the zone. This requires the zone lock, so we
2577 * must drop the critical section, then re-acquire it when we go back
2578 * to the cache. Since the critical section is released, we may be
2579 * preempted or migrate. As such, make sure not to maintain any
2580 * thread-local state specific to the cache from prior to releasing
2581 * the critical section.
2584 if (ZONE_TRYLOCK(zone) == 0) {
2585 /* Record contention to size the buckets. */
2591 cache = &zone->uz_cpu[cpu];
2593 /* See if we lost the race to fill the cache. */
2594 if (cache->uc_allocbucket != NULL) {
2600 * Check the zone's cache of buckets.
2602 if (domain == UMA_ANYDOMAIN)
2603 zdom = &zone->uz_domain[0];
2605 zdom = &zone->uz_domain[domain];
2606 if ((bucket = zone_try_fetch_bucket(zone, zdom, true)) != NULL) {
2607 KASSERT(bucket->ub_cnt != 0,
2608 ("uma_zalloc_arg: Returning an empty bucket."));
2609 cache->uc_allocbucket = bucket;
2613 /* We are no longer associated with this CPU. */
2617 * We bump the uz count when the cache size is insufficient to
2618 * handle the working set.
2620 if (lockfail && zone->uz_count < BUCKET_MAX)
2625 * Now lets just fill a bucket and put it on the free list. If that
2626 * works we'll restart the allocation from the beginning and it
2627 * will use the just filled bucket.
2629 bucket = zone_alloc_bucket(zone, udata, domain, flags);
2630 CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
2631 zone->uz_name, zone, bucket);
2632 if (bucket != NULL) {
2636 cache = &zone->uz_cpu[cpu];
2639 * See if we lost the race or were migrated. Cache the
2640 * initialized bucket to make this less likely or claim
2641 * the memory directly.
2643 if (cache->uc_allocbucket == NULL &&
2644 ((zone->uz_flags & UMA_ZONE_NUMA) == 0 ||
2645 domain == PCPU_GET(domain))) {
2646 cache->uc_allocbucket = bucket;
2647 zdom->uzd_imax += bucket->ub_cnt;
2648 } else if ((zone->uz_flags & UMA_ZONE_NOBUCKETCACHE) != 0) {
2651 bucket_drain(zone, bucket);
2652 bucket_free(zone, bucket, udata);
2653 goto zalloc_restart;
2655 zone_put_bucket(zone, zdom, bucket, false);
2661 * We may not be able to get a bucket so return an actual item.
2664 item = zone_alloc_item(zone, udata, domain, flags);
2670 uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
2673 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2674 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
2676 /* This is the fast path allocation */
2678 "uma_zalloc_domain thread %x zone %s(%p) domain %d flags %d",
2679 curthread, zone->uz_name, zone, domain, flags);
2681 if (flags & M_WAITOK) {
2682 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2683 "uma_zalloc_domain: zone \"%s\"", zone->uz_name);
2685 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2686 ("uma_zalloc_domain: called with spinlock or critical section held"));
2688 return (zone_alloc_item(zone, udata, domain, flags));
2692 * Find a slab with some space. Prefer slabs that are partially used over those
2693 * that are totally full. This helps to reduce fragmentation.
2695 * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check
2699 keg_first_slab(uma_keg_t keg, int domain, bool rr)
2705 KASSERT(domain >= 0 && domain < vm_ndomains,
2706 ("keg_first_slab: domain %d out of range", domain));
2711 dom = &keg->uk_domain[domain];
2712 if (!LIST_EMPTY(&dom->ud_part_slab))
2713 return (LIST_FIRST(&dom->ud_part_slab));
2714 if (!LIST_EMPTY(&dom->ud_free_slab)) {
2715 slab = LIST_FIRST(&dom->ud_free_slab);
2716 LIST_REMOVE(slab, us_link);
2717 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
2721 domain = (domain + 1) % vm_ndomains;
2722 } while (domain != start);
2728 keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags)
2732 mtx_assert(&keg->uk_lock, MA_OWNED);
2734 reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve;
2735 if (keg->uk_free <= reserve)
2737 return (keg_first_slab(keg, domain, rr));
2741 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags)
2743 struct vm_domainset_iter di;
2750 mtx_assert(&keg->uk_lock, MA_OWNED);
2753 * Use the keg's policy if upper layers haven't already specified a
2754 * domain (as happens with first-touch zones).
2756 * To avoid races we run the iterator with the keg lock held, but that
2757 * means that we cannot allow the vm_domainset layer to sleep. Thus,
2758 * clear M_WAITOK and handle low memory conditions locally.
2760 rr = rdomain == UMA_ANYDOMAIN;
2762 aflags = (flags & ~M_WAITOK) | M_NOWAIT;
2763 vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
2771 slab = keg_fetch_free_slab(keg, domain, rr, flags);
2773 MPASS(slab->us_keg == keg);
2778 * M_NOVM means don't ask at all!
2783 if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2784 keg->uk_flags |= UMA_ZFLAG_FULL;
2786 * If this is not a multi-zone, set the FULL bit.
2787 * Otherwise slab_multi() takes care of it.
2789 if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) {
2790 zone->uz_flags |= UMA_ZFLAG_FULL;
2791 zone_log_warning(zone);
2792 zone_maxaction(zone);
2794 if (flags & M_NOWAIT)
2797 msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2800 slab = keg_alloc_slab(keg, zone, domain, flags, aflags);
2802 * If we got a slab here it's safe to mark it partially used
2803 * and return. We assume that the caller is going to remove
2804 * at least one item.
2807 MPASS(slab->us_keg == keg);
2808 dom = &keg->uk_domain[slab->us_domain];
2809 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
2813 if (rr && vm_domainset_iter_policy(&di, &domain) != 0) {
2814 if ((flags & M_WAITOK) != 0) {
2816 vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
2825 * We might not have been able to get a slab but another cpu
2826 * could have while we were unlocked. Check again before we
2829 if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) {
2830 MPASS(slab->us_keg == keg);
2837 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int domain, int flags)
2842 keg = zone_first_keg(zone);
2847 slab = keg_fetch_slab(keg, zone, domain, flags);
2850 if (flags & (M_NOWAIT | M_NOVM))
2858 * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns
2859 * with the keg locked. On NULL no lock is held.
2861 * The last pointer is used to seed the search. It is not required.
2864 zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int domain, int rflags)
2874 * Don't wait on the first pass. This will skip limit tests
2875 * as well. We don't want to block if we can find a provider
2878 flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2880 * Use the last slab allocated as a hint for where to start
2884 slab = keg_fetch_slab(last, zone, domain, flags);
2890 * Loop until we have a slab incase of transient failures
2891 * while M_WAITOK is specified. I'm not sure this is 100%
2892 * required but we've done it for so long now.
2898 * Search the available kegs for slabs. Be careful to hold the
2899 * correct lock while calling into the keg layer.
2901 LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2902 keg = klink->kl_keg;
2904 if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2905 slab = keg_fetch_slab(keg, zone, domain, flags);
2909 if (keg->uk_flags & UMA_ZFLAG_FULL)
2915 if (rflags & (M_NOWAIT | M_NOVM))
2919 * All kegs are full. XXX We can't atomically check all kegs
2920 * and sleep so just sleep for a short period and retry.
2922 if (full && !empty) {
2924 zone->uz_flags |= UMA_ZFLAG_FULL;
2926 zone_log_warning(zone);
2927 zone_maxaction(zone);
2928 msleep(zone, zone->uz_lockptr, PVM,
2929 "zonelimit", hz/100);
2930 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2939 slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
2945 MPASS(keg == slab->us_keg);
2946 mtx_assert(&keg->uk_lock, MA_OWNED);
2948 freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2949 BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2950 item = slab->us_data + (keg->uk_rsize * freei);
2951 slab->us_freecount--;
2954 /* Move this slab to the full list */
2955 if (slab->us_freecount == 0) {
2956 LIST_REMOVE(slab, us_link);
2957 dom = &keg->uk_domain[slab->us_domain];
2958 LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link);
2965 zone_import(uma_zone_t zone, void **bucket, int max, int domain, int flags)
2976 /* Try to keep the buckets totally full */
2977 for (i = 0; i < max; ) {
2978 if ((slab = zone->uz_slab(zone, keg, domain, flags)) == NULL)
2982 stripe = howmany(max, vm_ndomains);
2984 while (slab->us_freecount && i < max) {
2985 bucket[i++] = slab_alloc_item(keg, slab);
2986 if (keg->uk_free <= keg->uk_reserve)
2990 * If the zone is striped we pick a new slab for every
2991 * N allocations. Eliminating this conditional will
2992 * instead pick a new domain for each bucket rather
2993 * than stripe within each bucket. The current option
2994 * produces more fragmentation and requires more cpu
2995 * time but yields better distribution.
2997 if ((zone->uz_flags & UMA_ZONE_NUMA) == 0 &&
2998 vm_ndomains > 1 && --stripe == 0)
3002 /* Don't block if we allocated any successfully. */
3013 zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags)
3015 uma_bucket_t bucket;
3018 CTR1(KTR_UMA, "zone_alloc:_bucket domain %d)", domain);
3020 /* Don't wait for buckets, preserve caller's NOVM setting. */
3021 bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
3025 max = MIN(bucket->ub_entries, zone->uz_count);
3026 bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
3027 max, domain, flags);
3030 * Initialize the memory if necessary.
3032 if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
3035 for (i = 0; i < bucket->ub_cnt; i++)
3036 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
3040 * If we couldn't initialize the whole bucket, put the
3041 * rest back onto the freelist.
3043 if (i != bucket->ub_cnt) {
3044 zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
3045 bucket->ub_cnt - i);
3047 bzero(&bucket->ub_bucket[i],
3048 sizeof(void *) * (bucket->ub_cnt - i));
3054 if (bucket->ub_cnt == 0) {
3055 bucket_free(zone, bucket, udata);
3056 atomic_add_long(&zone->uz_fails, 1);
3064 * Allocates a single item from a zone.
3067 * zone The zone to alloc for.
3068 * udata The data to be passed to the constructor.
3069 * domain The domain to allocate from or UMA_ANYDOMAIN.
3070 * flags M_WAITOK, M_NOWAIT, M_ZERO.
3073 * NULL if there is no memory and M_NOWAIT is set
3074 * An item if successful
3078 zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags)
3087 if (domain != UMA_ANYDOMAIN) {
3088 /* avoid allocs targeting empty domains */
3089 if (VM_DOMAIN_EMPTY(domain))
3090 domain = UMA_ANYDOMAIN;
3092 if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1)
3094 atomic_add_long(&zone->uz_allocs, 1);
3097 skipdbg = uma_dbg_zskip(zone, item);
3100 * We have to call both the zone's init (not the keg's init)
3101 * and the zone's ctor. This is because the item is going from
3102 * a keg slab directly to the user, and the user is expecting it
3103 * to be both zone-init'd as well as zone-ctor'd.
3105 if (zone->uz_init != NULL) {
3106 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
3107 zone_free_item(zone, item, udata, SKIP_FINI);
3111 if (zone->uz_ctor != NULL &&
3113 (!skipdbg || zone->uz_ctor != trash_ctor ||
3114 zone->uz_dtor != trash_dtor) &&
3116 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
3117 zone_free_item(zone, item, udata, SKIP_DTOR);
3122 uma_dbg_alloc(zone, NULL, item);
3125 uma_zero_item(item, zone);
3127 CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
3128 zone->uz_name, zone);
3133 CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
3134 zone->uz_name, zone);
3135 atomic_add_long(&zone->uz_fails, 1);
3141 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
3144 uma_bucket_t bucket;
3145 uma_zone_domain_t zdom;
3146 int cpu, domain, lockfail;
3151 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3152 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3154 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
3157 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3158 ("uma_zfree_arg: called with spinlock or critical section held"));
3160 /* uma_zfree(..., NULL) does nothing, to match free(9). */
3163 #ifdef DEBUG_MEMGUARD
3164 if (is_memguard_addr(item)) {
3165 if (zone->uz_dtor != NULL)
3166 zone->uz_dtor(item, zone->uz_size, udata);
3167 if (zone->uz_fini != NULL)
3168 zone->uz_fini(item, zone->uz_size);
3169 memguard_free(item);
3174 skipdbg = uma_dbg_zskip(zone, item);
3175 if (skipdbg == false) {
3176 if (zone->uz_flags & UMA_ZONE_MALLOC)
3177 uma_dbg_free(zone, udata, item);
3179 uma_dbg_free(zone, NULL, item);
3181 if (zone->uz_dtor != NULL && (!skipdbg ||
3182 zone->uz_dtor != trash_dtor || zone->uz_ctor != trash_ctor))
3184 if (zone->uz_dtor != NULL)
3186 zone->uz_dtor(item, zone->uz_size, udata);
3189 * The race here is acceptable. If we miss it we'll just have to wait
3190 * a little longer for the limits to be reset.
3192 if (zone->uz_flags & UMA_ZFLAG_FULL)
3196 * If possible, free to the per-CPU cache. There are two
3197 * requirements for safe access to the per-CPU cache: (1) the thread
3198 * accessing the cache must not be preempted or yield during access,
3199 * and (2) the thread must not migrate CPUs without switching which
3200 * cache it accesses. We rely on a critical section to prevent
3201 * preemption and migration. We release the critical section in
3202 * order to acquire the zone mutex if we are unable to free to the
3203 * current cache; when we re-acquire the critical section, we must
3204 * detect and handle migration if it has occurred.
3209 cache = &zone->uz_cpu[cpu];
3213 * Try to free into the allocbucket first to give LIFO ordering
3214 * for cache-hot datastructures. Spill over into the freebucket
3215 * if necessary. Alloc will swap them if one runs dry.
3217 bucket = cache->uc_allocbucket;
3218 if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries)
3219 bucket = cache->uc_freebucket;
3220 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
3221 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
3222 ("uma_zfree: Freeing to non free bucket index."));
3223 bucket->ub_bucket[bucket->ub_cnt] = item;
3231 * We must go back the zone, which requires acquiring the zone lock,
3232 * which in turn means we must release and re-acquire the critical
3233 * section. Since the critical section is released, we may be
3234 * preempted or migrate. As such, make sure not to maintain any
3235 * thread-local state specific to the cache from prior to releasing
3236 * the critical section.
3239 if (zone->uz_count == 0 || bucketdisable)
3243 if (ZONE_TRYLOCK(zone) == 0) {
3244 /* Record contention to size the buckets. */
3250 cache = &zone->uz_cpu[cpu];
3252 bucket = cache->uc_freebucket;
3253 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
3257 cache->uc_freebucket = NULL;
3258 /* We are no longer associated with this CPU. */
3261 if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) {
3262 domain = PCPU_GET(domain);
3263 if (VM_DOMAIN_EMPTY(domain))
3264 domain = UMA_ANYDOMAIN;
3267 zdom = &zone->uz_domain[0];
3269 /* Can we throw this on the zone full list? */
3270 if (bucket != NULL) {
3272 "uma_zfree: zone %s(%p) putting bucket %p on free list",
3273 zone->uz_name, zone, bucket);
3274 /* ub_cnt is pointing to the last free item */
3275 KASSERT(bucket->ub_cnt != 0,
3276 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
3277 if ((zone->uz_flags & UMA_ZONE_NOBUCKETCACHE) != 0) {
3279 bucket_drain(zone, bucket);
3280 bucket_free(zone, bucket, udata);
3283 zone_put_bucket(zone, zdom, bucket, true);
3287 * We bump the uz count when the cache size is insufficient to
3288 * handle the working set.
3290 if (lockfail && zone->uz_count < BUCKET_MAX)
3294 bucket = bucket_alloc(zone, udata, M_NOWAIT);
3295 CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p",
3296 zone->uz_name, zone, bucket);
3300 cache = &zone->uz_cpu[cpu];
3301 if (cache->uc_freebucket == NULL &&
3302 ((zone->uz_flags & UMA_ZONE_NUMA) == 0 ||
3303 domain == PCPU_GET(domain))) {
3304 cache->uc_freebucket = bucket;
3308 * We lost the race, start over. We have to drop our
3309 * critical section to free the bucket.
3312 bucket_free(zone, bucket, udata);
3317 * If nothing else caught this, we'll just do an internal free.
3320 zone_free_item(zone, item, udata, SKIP_DTOR);
3326 uma_zfree_domain(uma_zone_t zone, void *item, void *udata)
3329 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3330 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3332 CTR2(KTR_UMA, "uma_zfree_domain thread %x zone %s", curthread,
3335 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3336 ("uma_zfree_domain: called with spinlock or critical section held"));
3338 /* uma_zfree(..., NULL) does nothing, to match free(9). */
3341 zone_free_item(zone, item, udata, SKIP_NONE);
3345 slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item)
3350 mtx_assert(&keg->uk_lock, MA_OWNED);
3351 MPASS(keg == slab->us_keg);
3353 dom = &keg->uk_domain[slab->us_domain];
3355 /* Do we need to remove from any lists? */
3356 if (slab->us_freecount+1 == keg->uk_ipers) {
3357 LIST_REMOVE(slab, us_link);
3358 LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
3359 } else if (slab->us_freecount == 0) {
3360 LIST_REMOVE(slab, us_link);
3361 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
3364 /* Slab management. */
3365 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3366 BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
3367 slab->us_freecount++;
3369 /* Keg statistics. */
3374 zone_release(uma_zone_t zone, void **bucket, int cnt)
3384 keg = zone_first_keg(zone);
3386 for (i = 0; i < cnt; i++) {
3388 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
3389 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
3390 if (zone->uz_flags & UMA_ZONE_HASH) {
3391 slab = hash_sfind(&keg->uk_hash, mem);
3393 mem += keg->uk_pgoff;
3394 slab = (uma_slab_t)mem;
3397 slab = vtoslab((vm_offset_t)item);
3398 if (slab->us_keg != keg) {
3404 slab_free_item(keg, slab, item);
3405 if (keg->uk_flags & UMA_ZFLAG_FULL) {
3406 if (keg->uk_pages < keg->uk_maxpages) {
3407 keg->uk_flags &= ~UMA_ZFLAG_FULL;
3412 * We can handle one more allocation. Since we're
3413 * clearing ZFLAG_FULL, wake up all procs blocked
3414 * on pages. This should be uncommon, so keeping this
3415 * simple for now (rather than adding count of blocked
3424 zone->uz_flags &= ~UMA_ZFLAG_FULL;
3432 * Frees a single item to any zone.
3435 * zone The zone to free to
3436 * item The item we're freeing
3437 * udata User supplied data for the dtor
3438 * skip Skip dtors and finis
3441 zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
3446 skipdbg = uma_dbg_zskip(zone, item);
3447 if (skip == SKIP_NONE && !skipdbg) {
3448 if (zone->uz_flags & UMA_ZONE_MALLOC)
3449 uma_dbg_free(zone, udata, item);
3451 uma_dbg_free(zone, NULL, item);
3454 if (skip < SKIP_DTOR && zone->uz_dtor != NULL &&
3455 (!skipdbg || zone->uz_dtor != trash_dtor ||
3456 zone->uz_ctor != trash_ctor))
3458 if (skip < SKIP_DTOR && zone->uz_dtor != NULL)
3460 zone->uz_dtor(item, zone->uz_size, udata);
3462 if (skip < SKIP_FINI && zone->uz_fini)
3463 zone->uz_fini(item, zone->uz_size);
3465 atomic_add_long(&zone->uz_frees, 1);
3466 zone->uz_release(zone->uz_arg, &item, 1);
3471 uma_zone_set_max(uma_zone_t zone, int nitems)
3475 keg = zone_first_keg(zone);
3479 keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
3480 if (keg->uk_maxpages * keg->uk_ipers < nitems)
3481 keg->uk_maxpages += keg->uk_ppera;
3482 nitems = (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers;
3490 uma_zone_get_max(uma_zone_t zone)
3495 keg = zone_first_keg(zone);
3499 nitems = (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers;
3507 uma_zone_set_warning(uma_zone_t zone, const char *warning)
3511 zone->uz_warning = warning;
3517 uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
3521 TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
3527 uma_zone_get_cur(uma_zone_t zone)
3533 nitems = zone->uz_allocs - zone->uz_frees;
3536 * See the comment in sysctl_vm_zone_stats() regarding the
3537 * safety of accessing the per-cpu caches. With the zone lock
3538 * held, it is safe, but can potentially result in stale data.
3540 nitems += zone->uz_cpu[i].uc_allocs -
3541 zone->uz_cpu[i].uc_frees;
3545 return (nitems < 0 ? 0 : nitems);
3550 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
3554 keg = zone_first_keg(zone);
3555 KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
3557 KASSERT(keg->uk_pages == 0,
3558 ("uma_zone_set_init on non-empty keg"));
3559 keg->uk_init = uminit;
3565 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
3569 keg = zone_first_keg(zone);
3570 KASSERT(keg != NULL, ("uma_zone_set_fini: Invalid zone type"));
3572 KASSERT(keg->uk_pages == 0,
3573 ("uma_zone_set_fini on non-empty keg"));
3574 keg->uk_fini = fini;
3580 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
3584 KASSERT(zone_first_keg(zone)->uk_pages == 0,
3585 ("uma_zone_set_zinit on non-empty keg"));
3586 zone->uz_init = zinit;
3592 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
3596 KASSERT(zone_first_keg(zone)->uk_pages == 0,
3597 ("uma_zone_set_zfini on non-empty keg"));
3598 zone->uz_fini = zfini;
3603 /* XXX uk_freef is not actually used with the zone locked */
3605 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
3609 keg = zone_first_keg(zone);
3610 KASSERT(keg != NULL, ("uma_zone_set_freef: Invalid zone type"));
3612 keg->uk_freef = freef;
3617 /* XXX uk_allocf is not actually used with the zone locked */
3619 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
3623 keg = zone_first_keg(zone);
3625 keg->uk_allocf = allocf;
3631 uma_zone_reserve(uma_zone_t zone, int items)
3635 keg = zone_first_keg(zone);
3639 keg->uk_reserve = items;
3647 uma_zone_reserve_kva(uma_zone_t zone, int count)
3653 keg = zone_first_keg(zone);
3656 pages = count / keg->uk_ipers;
3658 if (pages * keg->uk_ipers < count)
3660 pages *= keg->uk_ppera;
3662 #ifdef UMA_MD_SMALL_ALLOC
3663 if (keg->uk_ppera > 1) {
3667 kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
3675 keg->uk_maxpages = pages;
3676 #ifdef UMA_MD_SMALL_ALLOC
3677 keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
3679 keg->uk_allocf = noobj_alloc;
3681 keg->uk_flags |= UMA_ZONE_NOFREE;
3689 uma_prealloc(uma_zone_t zone, int items)
3691 struct vm_domainset_iter di;
3695 int aflags, domain, slabs;
3697 keg = zone_first_keg(zone);
3701 slabs = items / keg->uk_ipers;
3702 if (slabs * keg->uk_ipers < items)
3704 while (slabs-- > 0) {
3706 vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
3709 slab = keg_alloc_slab(keg, zone, domain, M_WAITOK,
3712 MPASS(slab->us_keg == keg);
3713 dom = &keg->uk_domain[slab->us_domain];
3714 LIST_INSERT_HEAD(&dom->ud_free_slab, slab,
3719 if (vm_domainset_iter_policy(&di, &domain) != 0) {
3721 vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
3731 uma_reclaim_locked(bool kmem_danger)
3734 CTR0(KTR_UMA, "UMA: vm asked us to release pages!");
3735 sx_assert(&uma_drain_lock, SA_XLOCKED);
3737 zone_foreach(zone_drain);
3738 if (vm_page_count_min() || kmem_danger) {
3739 cache_drain_safe(NULL);
3740 zone_foreach(zone_drain);
3744 * Some slabs may have been freed but this zone will be visited early
3745 * we visit again so that we can free pages that are empty once other
3746 * zones are drained. We have to do the same for buckets.
3748 zone_drain(slabzone);
3749 bucket_zone_drain();
3756 sx_xlock(&uma_drain_lock);
3757 uma_reclaim_locked(false);
3758 sx_xunlock(&uma_drain_lock);
3761 static volatile int uma_reclaim_needed;
3764 uma_reclaim_wakeup(void)
3767 if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
3768 wakeup(uma_reclaim);
3772 uma_reclaim_worker(void *arg __unused)
3776 sx_xlock(&uma_drain_lock);
3777 while (atomic_load_int(&uma_reclaim_needed) == 0)
3778 sx_sleep(uma_reclaim, &uma_drain_lock, PVM, "umarcl",
3780 sx_xunlock(&uma_drain_lock);
3781 EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
3782 sx_xlock(&uma_drain_lock);
3783 uma_reclaim_locked(true);
3784 atomic_store_int(&uma_reclaim_needed, 0);
3785 sx_xunlock(&uma_drain_lock);
3786 /* Don't fire more than once per-second. */
3787 pause("umarclslp", hz);
3793 uma_zone_exhausted(uma_zone_t zone)
3798 full = (zone->uz_flags & UMA_ZFLAG_FULL);
3804 uma_zone_exhausted_nolock(uma_zone_t zone)
3806 return (zone->uz_flags & UMA_ZFLAG_FULL);
3810 uma_large_malloc_domain(vm_size_t size, int domain, int wait)
3812 struct domainset *policy;
3816 if (domain != UMA_ANYDOMAIN) {
3817 /* avoid allocs targeting empty domains */
3818 if (VM_DOMAIN_EMPTY(domain))
3819 domain = UMA_ANYDOMAIN;
3821 slab = zone_alloc_item(slabzone, NULL, domain, wait);
3824 policy = (domain == UMA_ANYDOMAIN) ? DOMAINSET_RR() :
3825 DOMAINSET_FIXED(domain);
3826 addr = kmem_malloc_domainset(policy, size, wait);
3828 vsetslab(addr, slab);
3829 slab->us_data = (void *)addr;
3830 slab->us_flags = UMA_SLAB_KERNEL | UMA_SLAB_MALLOC;
3831 slab->us_size = size;
3832 slab->us_domain = vm_phys_domain(PHYS_TO_VM_PAGE(
3833 pmap_kextract(addr)));
3834 uma_total_inc(size);
3836 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3839 return ((void *)addr);
3843 uma_large_malloc(vm_size_t size, int wait)
3846 return uma_large_malloc_domain(size, UMA_ANYDOMAIN, wait);
3850 uma_large_free(uma_slab_t slab)
3853 KASSERT((slab->us_flags & UMA_SLAB_KERNEL) != 0,
3854 ("uma_large_free: Memory not allocated with uma_large_malloc."));
3855 kmem_free((vm_offset_t)slab->us_data, slab->us_size);
3856 uma_total_dec(slab->us_size);
3857 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3861 uma_zero_item(void *item, uma_zone_t zone)
3864 bzero(item, zone->uz_size);
3871 return (uma_kmem_limit);
3875 uma_set_limit(unsigned long limit)
3878 uma_kmem_limit = limit;
3885 return (atomic_load_long(&uma_kmem_total));
3892 return (uma_kmem_limit - uma_size());
3896 uma_print_stats(void)
3898 zone_foreach(uma_print_zone);
3902 slab_print(uma_slab_t slab)
3904 printf("slab: keg %p, data %p, freecount %d\n",
3905 slab->us_keg, slab->us_data, slab->us_freecount);
3909 cache_print(uma_cache_t cache)
3911 printf("alloc: %p(%d), free: %p(%d)\n",
3912 cache->uc_allocbucket,
3913 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3914 cache->uc_freebucket,
3915 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3919 uma_print_keg(uma_keg_t keg)
3925 printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3926 "out %d free %d limit %d\n",
3927 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3928 keg->uk_ipers, keg->uk_ppera,
3929 (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
3930 keg->uk_free, (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3931 for (i = 0; i < vm_ndomains; i++) {
3932 dom = &keg->uk_domain[i];
3933 printf("Part slabs:\n");
3934 LIST_FOREACH(slab, &dom->ud_part_slab, us_link)
3936 printf("Free slabs:\n");
3937 LIST_FOREACH(slab, &dom->ud_free_slab, us_link)
3939 printf("Full slabs:\n");
3940 LIST_FOREACH(slab, &dom->ud_full_slab, us_link)
3946 uma_print_zone(uma_zone_t zone)
3952 printf("zone: %s(%p) size %d flags %#x\n",
3953 zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3954 LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3955 uma_print_keg(kl->kl_keg);
3957 cache = &zone->uz_cpu[i];
3958 printf("CPU %d Cache:\n", i);
3965 * Generate statistics across both the zone and its per-cpu cache's. Return
3966 * desired statistics if the pointer is non-NULL for that statistic.
3968 * Note: does not update the zone statistics, as it can't safely clear the
3969 * per-CPU cache statistic.
3971 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3972 * safe from off-CPU; we should modify the caches to track this information
3973 * directly so that we don't have to.
3976 uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp,
3977 uint64_t *freesp, uint64_t *sleepsp)
3980 uint64_t allocs, frees, sleeps;
3983 allocs = frees = sleeps = 0;
3986 cache = &z->uz_cpu[cpu];
3987 if (cache->uc_allocbucket != NULL)
3988 cachefree += cache->uc_allocbucket->ub_cnt;
3989 if (cache->uc_freebucket != NULL)
3990 cachefree += cache->uc_freebucket->ub_cnt;
3991 allocs += cache->uc_allocs;
3992 frees += cache->uc_frees;
3994 allocs += z->uz_allocs;
3995 frees += z->uz_frees;
3996 sleeps += z->uz_sleeps;
3997 if (cachefreep != NULL)
3998 *cachefreep = cachefree;
3999 if (allocsp != NULL)
4003 if (sleepsp != NULL)
4009 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
4016 rw_rlock(&uma_rwlock);
4017 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4018 LIST_FOREACH(z, &kz->uk_zones, uz_link)
4021 rw_runlock(&uma_rwlock);
4022 return (sysctl_handle_int(oidp, &count, 0, req));
4026 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
4028 struct uma_stream_header ush;
4029 struct uma_type_header uth;
4030 struct uma_percpu_stat *ups;
4031 uma_zone_domain_t zdom;
4038 int count, error, i;
4040 error = sysctl_wire_old_buffer(req, 0);
4043 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
4044 sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
4045 ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK);
4048 rw_rlock(&uma_rwlock);
4049 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4050 LIST_FOREACH(z, &kz->uk_zones, uz_link)
4055 * Insert stream header.
4057 bzero(&ush, sizeof(ush));
4058 ush.ush_version = UMA_STREAM_VERSION;
4059 ush.ush_maxcpus = (mp_maxid + 1);
4060 ush.ush_count = count;
4061 (void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
4063 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4064 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
4065 bzero(&uth, sizeof(uth));
4067 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
4068 uth.uth_align = kz->uk_align;
4069 uth.uth_size = kz->uk_size;
4070 uth.uth_rsize = kz->uk_rsize;
4071 LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
4073 uth.uth_maxpages += k->uk_maxpages;
4074 uth.uth_pages += k->uk_pages;
4075 uth.uth_keg_free += k->uk_free;
4076 uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
4081 * A zone is secondary is it is not the first entry
4082 * on the keg's zone list.
4084 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
4085 (LIST_FIRST(&kz->uk_zones) != z))
4086 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
4088 for (i = 0; i < vm_ndomains; i++) {
4089 zdom = &z->uz_domain[i];
4090 uth.uth_zone_free += zdom->uzd_nitems;
4092 uth.uth_allocs = z->uz_allocs;
4093 uth.uth_frees = z->uz_frees;
4094 uth.uth_fails = z->uz_fails;
4095 uth.uth_sleeps = z->uz_sleeps;
4097 * While it is not normally safe to access the cache
4098 * bucket pointers while not on the CPU that owns the
4099 * cache, we only allow the pointers to be exchanged
4100 * without the zone lock held, not invalidated, so
4101 * accept the possible race associated with bucket
4102 * exchange during monitoring.
4104 for (i = 0; i < mp_maxid + 1; i++) {
4105 bzero(&ups[i], sizeof(*ups));
4106 if (kz->uk_flags & UMA_ZFLAG_INTERNAL ||
4109 cache = &z->uz_cpu[i];
4110 if (cache->uc_allocbucket != NULL)
4111 ups[i].ups_cache_free +=
4112 cache->uc_allocbucket->ub_cnt;
4113 if (cache->uc_freebucket != NULL)
4114 ups[i].ups_cache_free +=
4115 cache->uc_freebucket->ub_cnt;
4116 ups[i].ups_allocs = cache->uc_allocs;
4117 ups[i].ups_frees = cache->uc_frees;
4120 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
4121 for (i = 0; i < mp_maxid + 1; i++)
4122 (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
4125 rw_runlock(&uma_rwlock);
4126 error = sbuf_finish(&sbuf);
4133 sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
4135 uma_zone_t zone = *(uma_zone_t *)arg1;
4138 max = uma_zone_get_max(zone);
4139 error = sysctl_handle_int(oidp, &max, 0, req);
4140 if (error || !req->newptr)
4143 uma_zone_set_max(zone, max);
4149 sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
4151 uma_zone_t zone = *(uma_zone_t *)arg1;
4154 cur = uma_zone_get_cur(zone);
4155 return (sysctl_handle_int(oidp, &cur, 0, req));
4160 uma_dbg_getslab(uma_zone_t zone, void *item)
4166 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
4167 if (zone->uz_flags & UMA_ZONE_VTOSLAB) {
4168 slab = vtoslab((vm_offset_t)mem);
4171 * It is safe to return the slab here even though the
4172 * zone is unlocked because the item's allocation state
4173 * essentially holds a reference.
4176 keg = LIST_FIRST(&zone->uz_kegs)->kl_keg;
4177 if (keg->uk_flags & UMA_ZONE_HASH)
4178 slab = hash_sfind(&keg->uk_hash, mem);
4180 slab = (uma_slab_t)(mem + keg->uk_pgoff);
4188 uma_dbg_zskip(uma_zone_t zone, void *mem)
4192 if ((keg = zone_first_keg(zone)) == NULL)
4195 return (uma_dbg_kskip(keg, mem));
4199 uma_dbg_kskip(uma_keg_t keg, void *mem)
4203 if (dbg_divisor == 0)
4206 if (dbg_divisor == 1)
4209 idx = (uintptr_t)mem >> PAGE_SHIFT;
4210 if (keg->uk_ipers > 1) {
4211 idx *= keg->uk_ipers;
4212 idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize;
4215 if ((idx / dbg_divisor) * dbg_divisor != idx) {
4216 counter_u64_add(uma_skip_cnt, 1);
4219 counter_u64_add(uma_dbg_cnt, 1);
4225 * Set up the slab's freei data such that uma_dbg_free can function.
4229 uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
4235 slab = uma_dbg_getslab(zone, item);
4237 panic("uma: item %p did not belong to zone %s\n",
4238 item, zone->uz_name);
4241 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
4243 if (BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
4244 panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
4245 item, zone, zone->uz_name, slab, freei);
4246 BIT_SET_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
4252 * Verifies freed addresses. Checks for alignment, valid slab membership
4253 * and duplicate frees.
4257 uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
4263 slab = uma_dbg_getslab(zone, item);
4265 panic("uma: Freed item %p did not belong to zone %s\n",
4266 item, zone->uz_name);
4269 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
4271 if (freei >= keg->uk_ipers)
4272 panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
4273 item, zone, zone->uz_name, slab, freei);
4275 if (((freei * keg->uk_rsize) + slab->us_data) != item)
4276 panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
4277 item, zone, zone->uz_name, slab, freei);
4279 if (!BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
4280 panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
4281 item, zone, zone->uz_name, slab, freei);
4283 BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
4285 #endif /* INVARIANTS */
4288 DB_SHOW_COMMAND(uma, db_show_uma)
4292 uint64_t allocs, frees, sleeps;
4296 db_printf("%18s %8s %8s %8s %12s %8s %8s\n", "Zone", "Size", "Used",
4297 "Free", "Requests", "Sleeps", "Bucket");
4298 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4299 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
4300 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
4301 allocs = z->uz_allocs;
4302 frees = z->uz_frees;
4303 sleeps = z->uz_sleeps;
4306 uma_zone_sumstat(z, &cachefree, &allocs,
4308 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
4309 (LIST_FIRST(&kz->uk_zones) != z)))
4310 cachefree += kz->uk_free;
4311 for (i = 0; i < vm_ndomains; i++)
4312 cachefree += z->uz_domain[i].uzd_nitems;
4314 db_printf("%18s %8ju %8jd %8ld %12ju %8ju %8u\n",
4315 z->uz_name, (uintmax_t)kz->uk_size,
4316 (intmax_t)(allocs - frees), cachefree,
4317 (uintmax_t)allocs, sleeps, z->uz_count);
4324 DB_SHOW_COMMAND(umacache, db_show_umacache)
4327 uint64_t allocs, frees;
4331 db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
4332 "Requests", "Bucket");
4333 LIST_FOREACH(z, &uma_cachezones, uz_link) {
4334 uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL);
4335 for (i = 0; i < vm_ndomains; i++)
4336 cachefree += z->uz_domain[i].uzd_nitems;
4337 db_printf("%18s %8ju %8jd %8ld %12ju %8u\n",
4338 z->uz_name, (uintmax_t)z->uz_size,
4339 (intmax_t)(allocs - frees), cachefree,
4340 (uintmax_t)allocs, z->uz_count);