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/eventhandler.h>
63 #include <sys/kernel.h>
64 #include <sys/types.h>
65 #include <sys/limits.h>
66 #include <sys/queue.h>
67 #include <sys/malloc.h>
70 #include <sys/sysctl.h>
71 #include <sys/mutex.h>
73 #include <sys/random.h>
74 #include <sys/rwlock.h>
76 #include <sys/sched.h>
78 #include <sys/taskqueue.h>
79 #include <sys/vmmeter.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_pageout.h>
85 #include <vm/vm_param.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_kern.h>
88 #include <vm/vm_extern.h>
90 #include <vm/uma_int.h>
91 #include <vm/uma_dbg.h>
96 #include <vm/memguard.h>
100 * This is the zone and keg from which all zones are spawned. The idea is that
101 * even the zone & keg heads are allocated from the allocator, so we use the
102 * bss section to bootstrap us.
104 static struct uma_keg masterkeg;
105 static struct uma_zone masterzone_k;
106 static struct uma_zone masterzone_z;
107 static uma_zone_t kegs = &masterzone_k;
108 static uma_zone_t zones = &masterzone_z;
110 /* This is the zone from which all of uma_slab_t's are allocated. */
111 static uma_zone_t slabzone;
114 * The initial hash tables come out of this zone so they can be allocated
115 * prior to malloc coming up.
117 static uma_zone_t hashzone;
119 /* The boot-time adjusted value for cache line alignment. */
120 int uma_align_cache = 64 - 1;
122 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
125 * Are we allowed to allocate buckets?
127 static int bucketdisable = 1;
129 /* Linked list of all kegs in the system */
130 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
132 /* Linked list of all cache-only zones in the system */
133 static LIST_HEAD(,uma_zone) uma_cachezones =
134 LIST_HEAD_INITIALIZER(uma_cachezones);
136 /* This RW lock protects the keg list */
137 static struct rwlock_padalign __exclusive_cache_line uma_rwlock;
140 * Pointer and counter to pool of pages, that is preallocated at
141 * startup to bootstrap UMA. Early zones continue to use the pool
142 * until it is depleted, so allocations may happen after boot, thus
143 * we need a mutex to protect it.
145 static char *bootmem;
146 static int boot_pages;
147 static struct mtx uma_boot_pages_mtx;
149 static struct sx uma_drain_lock;
151 /* kmem soft limit. */
152 static unsigned long uma_kmem_limit = LONG_MAX;
153 static volatile unsigned long uma_kmem_total;
155 /* Is the VM done starting up? */
156 static int booted = 0;
157 #define UMA_STARTUP 1
158 #define UMA_STARTUP2 2
161 * This is the handle used to schedule events that need to happen
162 * outside of the allocation fast path.
164 static struct callout uma_callout;
165 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
168 * This structure is passed as the zone ctor arg so that I don't have to create
169 * a special allocation function just for zones.
171 struct uma_zctor_args {
186 struct uma_kctor_args {
195 struct uma_bucket_zone {
198 int ubz_entries; /* Number of items it can hold. */
199 int ubz_maxsize; /* Maximum allocation size per-item. */
203 * Compute the actual number of bucket entries to pack them in power
204 * of two sizes for more efficient space utilization.
206 #define BUCKET_SIZE(n) \
207 (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *))
209 #define BUCKET_MAX BUCKET_SIZE(256)
211 struct uma_bucket_zone bucket_zones[] = {
212 { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 },
213 { NULL, "6 Bucket", BUCKET_SIZE(6), 3072 },
214 { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 },
215 { NULL, "12 Bucket", BUCKET_SIZE(12), 1536 },
216 { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 },
217 { NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
218 { NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
219 { NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
220 { NULL, "256 Bucket", BUCKET_SIZE(256), 64 },
225 * Flags and enumerations to be passed to internal functions.
227 enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI };
231 static void *noobj_alloc(uma_zone_t, vm_size_t, uint8_t *, int);
232 static void *page_alloc(uma_zone_t, vm_size_t, uint8_t *, int);
233 static void *startup_alloc(uma_zone_t, vm_size_t, uint8_t *, int);
234 static void page_free(void *, vm_size_t, uint8_t);
235 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int);
236 static void cache_drain(uma_zone_t);
237 static void bucket_drain(uma_zone_t, uma_bucket_t);
238 static void bucket_cache_drain(uma_zone_t zone);
239 static int keg_ctor(void *, int, void *, int);
240 static void keg_dtor(void *, int, void *);
241 static int zone_ctor(void *, int, void *, int);
242 static void zone_dtor(void *, int, void *);
243 static int zero_init(void *, int, int);
244 static void keg_small_init(uma_keg_t keg);
245 static void keg_large_init(uma_keg_t keg);
246 static void zone_foreach(void (*zfunc)(uma_zone_t));
247 static void zone_timeout(uma_zone_t zone);
248 static int hash_alloc(struct uma_hash *);
249 static int hash_expand(struct uma_hash *, struct uma_hash *);
250 static void hash_free(struct uma_hash *hash);
251 static void uma_timeout(void *);
252 static void uma_startup3(void);
253 static void *zone_alloc_item(uma_zone_t, void *, int);
254 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
255 static void bucket_enable(void);
256 static void bucket_init(void);
257 static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int);
258 static void bucket_free(uma_zone_t zone, uma_bucket_t, void *);
259 static void bucket_zone_drain(void);
260 static uma_bucket_t zone_alloc_bucket(uma_zone_t zone, void *, int flags);
261 static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags);
262 static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags);
263 static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
264 static void slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item);
265 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
266 uma_fini fini, int align, uint32_t flags);
267 static int zone_import(uma_zone_t zone, void **bucket, int max, int flags);
268 static void zone_release(uma_zone_t zone, void **bucket, int cnt);
269 static void uma_zero_item(void *item, uma_zone_t zone);
271 void uma_print_zone(uma_zone_t);
272 void uma_print_stats(void);
273 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
274 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
277 static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item);
278 static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item);
281 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
283 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
284 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
286 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
287 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
289 static int zone_warnings = 1;
290 SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0,
291 "Warn when UMA zones becomes full");
293 /* Adjust bytes under management by UMA. */
295 uma_total_dec(unsigned long size)
298 atomic_subtract_long(&uma_kmem_total, size);
302 uma_total_inc(unsigned long size)
305 if (atomic_fetchadd_long(&uma_kmem_total, size) > uma_kmem_limit)
306 uma_reclaim_wakeup();
310 * This routine checks to see whether or not it's safe to enable buckets.
315 bucketdisable = vm_page_count_min();
319 * Initialize bucket_zones, the array of zones of buckets of various sizes.
321 * For each zone, calculate the memory required for each bucket, consisting
322 * of the header and an array of pointers.
327 struct uma_bucket_zone *ubz;
330 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
331 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
332 size += sizeof(void *) * ubz->ubz_entries;
333 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
334 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
335 UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET);
340 * Given a desired number of entries for a bucket, return the zone from which
341 * to allocate the bucket.
343 static struct uma_bucket_zone *
344 bucket_zone_lookup(int entries)
346 struct uma_bucket_zone *ubz;
348 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
349 if (ubz->ubz_entries >= entries)
356 bucket_select(int size)
358 struct uma_bucket_zone *ubz;
360 ubz = &bucket_zones[0];
361 if (size > ubz->ubz_maxsize)
362 return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
364 for (; ubz->ubz_entries != 0; ubz++)
365 if (ubz->ubz_maxsize < size)
368 return (ubz->ubz_entries);
372 bucket_alloc(uma_zone_t zone, void *udata, int flags)
374 struct uma_bucket_zone *ubz;
378 * This is to stop us from allocating per cpu buckets while we're
379 * running out of vm.boot_pages. Otherwise, we would exhaust the
380 * boot pages. This also prevents us from allocating buckets in
381 * low memory situations.
386 * To limit bucket recursion we store the original zone flags
387 * in a cookie passed via zalloc_arg/zfree_arg. This allows the
388 * NOVM flag to persist even through deep recursions. We also
389 * store ZFLAG_BUCKET once we have recursed attempting to allocate
390 * a bucket for a bucket zone so we do not allow infinite bucket
391 * recursion. This cookie will even persist to frees of unused
392 * buckets via the allocation path or bucket allocations in the
395 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
396 udata = (void *)(uintptr_t)zone->uz_flags;
398 if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
400 udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
402 if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY)
404 ubz = bucket_zone_lookup(zone->uz_count);
405 if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0)
407 bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
410 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
413 bucket->ub_entries = ubz->ubz_entries;
420 bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
422 struct uma_bucket_zone *ubz;
424 KASSERT(bucket->ub_cnt == 0,
425 ("bucket_free: Freeing a non free bucket."));
426 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
427 udata = (void *)(uintptr_t)zone->uz_flags;
428 ubz = bucket_zone_lookup(bucket->ub_entries);
429 uma_zfree_arg(ubz->ubz_zone, bucket, udata);
433 bucket_zone_drain(void)
435 struct uma_bucket_zone *ubz;
437 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
438 zone_drain(ubz->ubz_zone);
442 zone_log_warning(uma_zone_t zone)
444 static const struct timeval warninterval = { 300, 0 };
446 if (!zone_warnings || zone->uz_warning == NULL)
449 if (ratecheck(&zone->uz_ratecheck, &warninterval))
450 printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
454 zone_maxaction(uma_zone_t zone)
457 if (zone->uz_maxaction.ta_func != NULL)
458 taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction);
462 zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
466 LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
467 kegfn(klink->kl_keg);
471 * Routine called by timeout which is used to fire off some time interval
472 * based calculations. (stats, hash size, etc.)
481 uma_timeout(void *unused)
484 zone_foreach(zone_timeout);
486 /* Reschedule this event */
487 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
491 * Routine to perform timeout driven calculations. This expands the
492 * hashes and does per cpu statistics aggregation.
497 keg_timeout(uma_keg_t keg)
502 * Expand the keg hash table.
504 * This is done if the number of slabs is larger than the hash size.
505 * What I'm trying to do here is completely reduce collisions. This
506 * may be a little aggressive. Should I allow for two collisions max?
508 if (keg->uk_flags & UMA_ZONE_HASH &&
509 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
510 struct uma_hash newhash;
511 struct uma_hash oldhash;
515 * This is so involved because allocating and freeing
516 * while the keg lock is held will lead to deadlock.
517 * I have to do everything in stages and check for
520 newhash = keg->uk_hash;
522 ret = hash_alloc(&newhash);
525 if (hash_expand(&keg->uk_hash, &newhash)) {
526 oldhash = keg->uk_hash;
527 keg->uk_hash = newhash;
540 zone_timeout(uma_zone_t zone)
543 zone_foreach_keg(zone, &keg_timeout);
547 * Allocate and zero fill the next sized hash table from the appropriate
551 * hash A new hash structure with the old hash size in uh_hashsize
554 * 1 on success and 0 on failure.
557 hash_alloc(struct uma_hash *hash)
562 oldsize = hash->uh_hashsize;
564 /* We're just going to go to a power of two greater */
566 hash->uh_hashsize = oldsize * 2;
567 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
568 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
569 M_UMAHASH, M_NOWAIT);
571 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
572 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
574 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
576 if (hash->uh_slab_hash) {
577 bzero(hash->uh_slab_hash, alloc);
578 hash->uh_hashmask = hash->uh_hashsize - 1;
586 * Expands the hash table for HASH zones. This is done from zone_timeout
587 * to reduce collisions. This must not be done in the regular allocation
588 * path, otherwise, we can recurse on the vm while allocating pages.
591 * oldhash The hash you want to expand
592 * newhash The hash structure for the new table
600 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
606 if (!newhash->uh_slab_hash)
609 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
613 * I need to investigate hash algorithms for resizing without a
617 for (i = 0; i < oldhash->uh_hashsize; i++)
618 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
619 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
620 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
621 hval = UMA_HASH(newhash, slab->us_data);
622 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
630 * Free the hash bucket to the appropriate backing store.
633 * slab_hash The hash bucket we're freeing
634 * hashsize The number of entries in that hash bucket
640 hash_free(struct uma_hash *hash)
642 if (hash->uh_slab_hash == NULL)
644 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
645 zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
647 free(hash->uh_slab_hash, M_UMAHASH);
651 * Frees all outstanding items in a bucket
654 * zone The zone to free to, must be unlocked.
655 * bucket The free/alloc bucket with items, cpu queue must be locked.
662 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
670 for (i = 0; i < bucket->ub_cnt; i++)
671 zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
672 zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
677 * Drains the per cpu caches for a zone.
679 * NOTE: This may only be called while the zone is being turn down, and not
680 * during normal operation. This is necessary in order that we do not have
681 * to migrate CPUs to drain the per-CPU caches.
684 * zone The zone to drain, must be unlocked.
690 cache_drain(uma_zone_t zone)
696 * XXX: It is safe to not lock the per-CPU caches, because we're
697 * tearing down the zone anyway. I.e., there will be no further use
698 * of the caches at this point.
700 * XXX: It would good to be able to assert that the zone is being
701 * torn down to prevent improper use of cache_drain().
703 * XXX: We lock the zone before passing into bucket_cache_drain() as
704 * it is used elsewhere. Should the tear-down path be made special
705 * there in some form?
708 cache = &zone->uz_cpu[cpu];
709 bucket_drain(zone, cache->uc_allocbucket);
710 bucket_drain(zone, cache->uc_freebucket);
711 if (cache->uc_allocbucket != NULL)
712 bucket_free(zone, cache->uc_allocbucket, NULL);
713 if (cache->uc_freebucket != NULL)
714 bucket_free(zone, cache->uc_freebucket, NULL);
715 cache->uc_allocbucket = cache->uc_freebucket = NULL;
718 bucket_cache_drain(zone);
723 cache_shrink(uma_zone_t zone)
726 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
730 zone->uz_count = (zone->uz_count_min + zone->uz_count) / 2;
735 cache_drain_safe_cpu(uma_zone_t zone)
740 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
746 cache = &zone->uz_cpu[curcpu];
747 if (cache->uc_allocbucket) {
748 if (cache->uc_allocbucket->ub_cnt != 0)
749 LIST_INSERT_HEAD(&zone->uz_buckets,
750 cache->uc_allocbucket, ub_link);
752 b1 = cache->uc_allocbucket;
753 cache->uc_allocbucket = NULL;
755 if (cache->uc_freebucket) {
756 if (cache->uc_freebucket->ub_cnt != 0)
757 LIST_INSERT_HEAD(&zone->uz_buckets,
758 cache->uc_freebucket, ub_link);
760 b2 = cache->uc_freebucket;
761 cache->uc_freebucket = NULL;
766 bucket_free(zone, b1, NULL);
768 bucket_free(zone, b2, NULL);
772 * Safely drain per-CPU caches of a zone(s) to alloc bucket.
773 * This is an expensive call because it needs to bind to all CPUs
774 * one by one and enter a critical section on each of them in order
775 * to safely access their cache buckets.
776 * Zone lock must not be held on call this function.
779 cache_drain_safe(uma_zone_t zone)
784 * Polite bucket sizes shrinking was not enouth, shrink aggressively.
789 zone_foreach(cache_shrink);
792 thread_lock(curthread);
793 sched_bind(curthread, cpu);
794 thread_unlock(curthread);
797 cache_drain_safe_cpu(zone);
799 zone_foreach(cache_drain_safe_cpu);
801 thread_lock(curthread);
802 sched_unbind(curthread);
803 thread_unlock(curthread);
807 * Drain the cached buckets from a zone. Expects a locked zone on entry.
810 bucket_cache_drain(uma_zone_t zone)
815 * Drain the bucket queues and free the buckets, we just keep two per
818 while ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
819 LIST_REMOVE(bucket, ub_link);
821 bucket_drain(zone, bucket);
822 bucket_free(zone, bucket, NULL);
827 * Shrink further bucket sizes. Price of single zone lock collision
828 * is probably lower then price of global cache drain.
830 if (zone->uz_count > zone->uz_count_min)
835 keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
841 CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes",
842 keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera);
845 flags = slab->us_flags;
847 if (keg->uk_fini != NULL) {
848 for (i--; i > -1; i--)
849 keg->uk_fini(slab->us_data + (keg->uk_rsize * i),
852 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
853 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
854 keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
855 uma_total_dec(PAGE_SIZE * keg->uk_ppera);
859 * Frees pages from a keg back to the system. This is done on demand from
860 * the pageout daemon.
865 keg_drain(uma_keg_t keg)
867 struct slabhead freeslabs = { 0 };
868 uma_slab_t slab, tmp;
871 * We don't want to take pages from statically allocated kegs at this
874 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
877 CTR3(KTR_UMA, "keg_drain %s(%p) free items: %u",
878 keg->uk_name, keg, keg->uk_free);
880 if (keg->uk_free == 0)
883 LIST_FOREACH_SAFE(slab, &keg->uk_free_slab, us_link, tmp) {
884 /* We have nowhere to free these to. */
885 if (slab->us_flags & UMA_SLAB_BOOT)
888 LIST_REMOVE(slab, us_link);
889 keg->uk_pages -= keg->uk_ppera;
890 keg->uk_free -= keg->uk_ipers;
892 if (keg->uk_flags & UMA_ZONE_HASH)
893 UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
895 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
900 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
901 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
902 keg_free_slab(keg, slab, keg->uk_ipers);
907 zone_drain_wait(uma_zone_t zone, int waitok)
911 * Set draining to interlock with zone_dtor() so we can release our
912 * locks as we go. Only dtor() should do a WAITOK call since it
913 * is the only call that knows the structure will still be available
917 while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
918 if (waitok == M_NOWAIT)
920 msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1);
922 zone->uz_flags |= UMA_ZFLAG_DRAINING;
923 bucket_cache_drain(zone);
926 * The DRAINING flag protects us from being freed while
927 * we're running. Normally the uma_rwlock would protect us but we
928 * must be able to release and acquire the right lock for each keg.
930 zone_foreach_keg(zone, &keg_drain);
932 zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
939 zone_drain(uma_zone_t zone)
942 zone_drain_wait(zone, M_NOWAIT);
946 * Allocate a new slab for a keg. This does not insert the slab onto a list.
949 * wait Shall we wait?
952 * The slab that was allocated or NULL if there is no memory and the
953 * caller specified M_NOWAIT.
956 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait)
965 mtx_assert(&keg->uk_lock, MA_OWNED);
969 allocf = keg->uk_allocf;
971 size = keg->uk_ppera * PAGE_SIZE;
973 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
974 slab = zone_alloc_item(keg->uk_slabzone, NULL, wait);
980 * This reproduces the old vm_zone behavior of zero filling pages the
981 * first time they are added to a zone.
983 * Malloced items are zeroed in uma_zalloc.
986 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
991 if (keg->uk_flags & UMA_ZONE_NODUMP)
994 /* zone is passed for legacy reasons. */
995 mem = allocf(zone, size, &flags, wait);
997 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
998 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
1002 uma_total_inc(size);
1004 /* Point the slab into the allocated memory */
1005 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
1006 slab = (uma_slab_t )(mem + keg->uk_pgoff);
1008 if (keg->uk_flags & UMA_ZONE_VTOSLAB)
1009 for (i = 0; i < keg->uk_ppera; i++)
1010 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
1013 slab->us_data = mem;
1014 slab->us_freecount = keg->uk_ipers;
1015 slab->us_flags = flags;
1016 BIT_FILL(SLAB_SETSIZE, &slab->us_free);
1018 BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree);
1021 if (keg->uk_init != NULL) {
1022 for (i = 0; i < keg->uk_ipers; i++)
1023 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
1024 keg->uk_size, wait) != 0)
1026 if (i != keg->uk_ipers) {
1027 keg_free_slab(keg, slab, i);
1035 CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)",
1036 slab, keg->uk_name, keg);
1039 if (keg->uk_flags & UMA_ZONE_HASH)
1040 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
1042 keg->uk_pages += keg->uk_ppera;
1043 keg->uk_free += keg->uk_ipers;
1050 * This function is intended to be used early on in place of page_alloc() so
1051 * that we may use the boot time page cache to satisfy allocations before
1055 startup_alloc(uma_zone_t zone, vm_size_t bytes, uint8_t *pflag, int wait)
1061 keg = zone_first_keg(zone);
1062 pages = howmany(bytes, PAGE_SIZE);
1063 KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n"));
1066 * Check our small startup cache to see if it has pages remaining.
1068 mtx_lock(&uma_boot_pages_mtx);
1069 if (pages <= boot_pages) {
1071 boot_pages -= pages;
1072 bootmem += pages * PAGE_SIZE;
1073 mtx_unlock(&uma_boot_pages_mtx);
1074 *pflag = UMA_SLAB_BOOT;
1077 mtx_unlock(&uma_boot_pages_mtx);
1078 if (booted < UMA_STARTUP2)
1079 panic("UMA: Increase vm.boot_pages");
1081 * Now that we've booted reset these users to their real allocator.
1083 #ifdef UMA_MD_SMALL_ALLOC
1084 keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc;
1086 keg->uk_allocf = page_alloc;
1088 return keg->uk_allocf(zone, bytes, pflag, wait);
1092 * Allocates a number of pages from the system
1095 * bytes The number of bytes requested
1096 * wait Shall we wait?
1099 * A pointer to the alloced memory or possibly
1100 * NULL if M_NOWAIT is set.
1103 page_alloc(uma_zone_t zone, vm_size_t bytes, uint8_t *pflag, int wait)
1105 void *p; /* Returned page */
1107 *pflag = UMA_SLAB_KERNEL;
1108 p = (void *) kmem_malloc(kernel_arena, bytes, wait);
1114 * Allocates a number of pages from within an object
1117 * bytes The number of bytes requested
1118 * wait Shall we wait?
1121 * A pointer to the alloced memory or possibly
1122 * NULL if M_NOWAIT is set.
1125 noobj_alloc(uma_zone_t zone, vm_size_t bytes, uint8_t *flags, int wait)
1127 TAILQ_HEAD(, vm_page) alloctail;
1129 vm_offset_t retkva, zkva;
1130 vm_page_t p, p_next;
1133 TAILQ_INIT(&alloctail);
1134 keg = zone_first_keg(zone);
1136 npages = howmany(bytes, PAGE_SIZE);
1137 while (npages > 0) {
1138 p = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT |
1139 VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1140 ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK :
1144 * Since the page does not belong to an object, its
1147 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1152 * Page allocation failed, free intermediate pages and
1155 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1156 vm_page_unwire(p, PQ_NONE);
1161 *flags = UMA_SLAB_PRIV;
1162 zkva = keg->uk_kva +
1163 atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1165 TAILQ_FOREACH(p, &alloctail, listq) {
1166 pmap_qenter(zkva, &p, 1);
1170 return ((void *)retkva);
1174 * Frees a number of pages to the system
1177 * mem A pointer to the memory to be freed
1178 * size The size of the memory being freed
1179 * flags The original p->us_flags field
1185 page_free(void *mem, vm_size_t size, uint8_t flags)
1189 if (flags & UMA_SLAB_KERNEL)
1190 vmem = kernel_arena;
1192 panic("UMA: page_free used with invalid flags %x", flags);
1194 kmem_free(vmem, (vm_offset_t)mem, size);
1198 * Zero fill initializer
1200 * Arguments/Returns follow uma_init specifications
1203 zero_init(void *mem, int size, int flags)
1210 * Finish creating a small uma keg. This calculates ipers, and the keg size.
1213 * keg The zone we should initialize
1219 keg_small_init(uma_keg_t keg)
1227 if (keg->uk_flags & UMA_ZONE_PCPU) {
1228 u_int ncpus = (mp_maxid + 1) ? (mp_maxid + 1) : MAXCPU;
1230 slabsize = sizeof(struct pcpu);
1231 keg->uk_ppera = howmany(ncpus * sizeof(struct pcpu),
1234 slabsize = UMA_SLAB_SIZE;
1239 * Calculate the size of each allocation (rsize) according to
1240 * alignment. If the requested size is smaller than we have
1241 * allocation bits for we round it up.
1243 rsize = keg->uk_size;
1244 if (rsize < slabsize / SLAB_SETSIZE)
1245 rsize = slabsize / SLAB_SETSIZE;
1246 if (rsize & keg->uk_align)
1247 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1248 keg->uk_rsize = rsize;
1250 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
1251 keg->uk_rsize < sizeof(struct pcpu),
1252 ("%s: size %u too large", __func__, keg->uk_rsize));
1254 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1257 shsize = sizeof(struct uma_slab);
1259 keg->uk_ipers = (slabsize - shsize) / rsize;
1260 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1261 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1263 memused = keg->uk_ipers * rsize + shsize;
1264 wastedspace = slabsize - memused;
1267 * We can't do OFFPAGE if we're internal or if we've been
1268 * asked to not go to the VM for buckets. If we do this we
1269 * may end up going to the VM for slabs which we do not
1270 * want to do if we're UMA_ZFLAG_CACHEONLY as a result
1271 * of UMA_ZONE_VM, which clearly forbids it.
1273 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1274 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1278 * See if using an OFFPAGE slab will limit our waste. Only do
1279 * this if it permits more items per-slab.
1281 * XXX We could try growing slabsize to limit max waste as well.
1282 * Historically this was not done because the VM could not
1283 * efficiently handle contiguous allocations.
1285 if ((wastedspace >= slabsize / UMA_MAX_WASTE) &&
1286 (keg->uk_ipers < (slabsize / keg->uk_rsize))) {
1287 keg->uk_ipers = slabsize / keg->uk_rsize;
1288 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1289 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1290 CTR6(KTR_UMA, "UMA decided we need offpage slab headers for "
1291 "keg: %s(%p), calculated wastedspace = %d, "
1292 "maximum wasted space allowed = %d, "
1293 "calculated ipers = %d, "
1294 "new wasted space = %d\n", keg->uk_name, keg, wastedspace,
1295 slabsize / UMA_MAX_WASTE, keg->uk_ipers,
1296 slabsize - keg->uk_ipers * keg->uk_rsize);
1297 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1300 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1301 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1302 keg->uk_flags |= UMA_ZONE_HASH;
1306 * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do
1307 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1311 * keg The keg we should initialize
1317 keg_large_init(uma_keg_t keg)
1321 KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1322 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1323 ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1324 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1325 ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__));
1327 keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE);
1329 keg->uk_rsize = keg->uk_size;
1331 /* Check whether we have enough space to not do OFFPAGE. */
1332 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0) {
1333 shsize = sizeof(struct uma_slab);
1334 if (shsize & UMA_ALIGN_PTR)
1335 shsize = (shsize & ~UMA_ALIGN_PTR) +
1336 (UMA_ALIGN_PTR + 1);
1338 if (PAGE_SIZE * keg->uk_ppera - keg->uk_rsize < shsize) {
1340 * We can't do OFFPAGE if we're internal, in which case
1341 * we need an extra page per allocation to contain the
1344 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) == 0)
1345 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1351 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1352 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1353 keg->uk_flags |= UMA_ZONE_HASH;
1357 keg_cachespread_init(uma_keg_t keg)
1364 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1365 ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__));
1367 alignsize = keg->uk_align + 1;
1368 rsize = keg->uk_size;
1370 * We want one item to start on every align boundary in a page. To
1371 * do this we will span pages. We will also extend the item by the
1372 * size of align if it is an even multiple of align. Otherwise, it
1373 * would fall on the same boundary every time.
1375 if (rsize & keg->uk_align)
1376 rsize = (rsize & ~keg->uk_align) + alignsize;
1377 if ((rsize & alignsize) == 0)
1379 trailer = rsize - keg->uk_size;
1380 pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1381 pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1382 keg->uk_rsize = rsize;
1383 keg->uk_ppera = pages;
1384 keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1385 keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1386 KASSERT(keg->uk_ipers <= SLAB_SETSIZE,
1387 ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1392 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1393 * the keg onto the global keg list.
1395 * Arguments/Returns follow uma_ctor specifications
1396 * udata Actually uma_kctor_args
1399 keg_ctor(void *mem, int size, void *udata, int flags)
1401 struct uma_kctor_args *arg = udata;
1402 uma_keg_t keg = mem;
1406 keg->uk_size = arg->size;
1407 keg->uk_init = arg->uminit;
1408 keg->uk_fini = arg->fini;
1409 keg->uk_align = arg->align;
1411 keg->uk_reserve = 0;
1413 keg->uk_flags = arg->flags;
1414 keg->uk_slabzone = NULL;
1417 * The master zone is passed to us at keg-creation time.
1420 keg->uk_name = zone->uz_name;
1422 if (arg->flags & UMA_ZONE_VM)
1423 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1425 if (arg->flags & UMA_ZONE_ZINIT)
1426 keg->uk_init = zero_init;
1428 if (arg->flags & UMA_ZONE_MALLOC)
1429 keg->uk_flags |= UMA_ZONE_VTOSLAB;
1431 if (arg->flags & UMA_ZONE_PCPU)
1433 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1435 keg->uk_flags &= ~UMA_ZONE_PCPU;
1438 if (keg->uk_flags & UMA_ZONE_CACHESPREAD) {
1439 keg_cachespread_init(keg);
1441 if (keg->uk_size > (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1442 keg_large_init(keg);
1444 keg_small_init(keg);
1447 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1448 keg->uk_slabzone = slabzone;
1451 * If we haven't booted yet we need allocations to go through the
1452 * startup cache until the vm is ready.
1454 if (booted < UMA_STARTUP2)
1455 keg->uk_allocf = startup_alloc;
1456 #ifdef UMA_MD_SMALL_ALLOC
1457 else if (keg->uk_ppera == 1)
1458 keg->uk_allocf = uma_small_alloc;
1461 keg->uk_allocf = page_alloc;
1462 #ifdef UMA_MD_SMALL_ALLOC
1463 if (keg->uk_ppera == 1)
1464 keg->uk_freef = uma_small_free;
1467 keg->uk_freef = page_free;
1470 * Initialize keg's lock
1472 KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS));
1475 * If we're putting the slab header in the actual page we need to
1476 * figure out where in each page it goes. This calculates a right
1477 * justified offset into the memory on an ALIGN_PTR boundary.
1479 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1482 /* Size of the slab struct and free list */
1483 totsize = sizeof(struct uma_slab);
1485 if (totsize & UMA_ALIGN_PTR)
1486 totsize = (totsize & ~UMA_ALIGN_PTR) +
1487 (UMA_ALIGN_PTR + 1);
1488 keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize;
1491 * The only way the following is possible is if with our
1492 * UMA_ALIGN_PTR adjustments we are now bigger than
1493 * UMA_SLAB_SIZE. I haven't checked whether this is
1494 * mathematically possible for all cases, so we make
1497 totsize = keg->uk_pgoff + sizeof(struct uma_slab);
1498 if (totsize > PAGE_SIZE * keg->uk_ppera) {
1499 printf("zone %s ipers %d rsize %d size %d\n",
1500 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1502 panic("UMA slab won't fit.");
1506 if (keg->uk_flags & UMA_ZONE_HASH)
1507 hash_alloc(&keg->uk_hash);
1509 CTR5(KTR_UMA, "keg_ctor %p zone %s(%p) out %d free %d\n",
1510 keg, zone->uz_name, zone,
1511 (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
1514 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1516 rw_wlock(&uma_rwlock);
1517 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1518 rw_wunlock(&uma_rwlock);
1523 * Zone header ctor. This initializes all fields, locks, etc.
1525 * Arguments/Returns follow uma_ctor specifications
1526 * udata Actually uma_zctor_args
1529 zone_ctor(void *mem, int size, void *udata, int flags)
1531 struct uma_zctor_args *arg = udata;
1532 uma_zone_t zone = mem;
1537 zone->uz_name = arg->name;
1538 zone->uz_ctor = arg->ctor;
1539 zone->uz_dtor = arg->dtor;
1540 zone->uz_slab = zone_fetch_slab;
1541 zone->uz_init = NULL;
1542 zone->uz_fini = NULL;
1543 zone->uz_allocs = 0;
1546 zone->uz_sleeps = 0;
1548 zone->uz_count_min = 0;
1550 zone->uz_warning = NULL;
1551 timevalclear(&zone->uz_ratecheck);
1554 ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
1557 * This is a pure cache zone, no kegs.
1560 if (arg->flags & UMA_ZONE_VM)
1561 arg->flags |= UMA_ZFLAG_CACHEONLY;
1562 zone->uz_flags = arg->flags;
1563 zone->uz_size = arg->size;
1564 zone->uz_import = arg->import;
1565 zone->uz_release = arg->release;
1566 zone->uz_arg = arg->arg;
1567 zone->uz_lockptr = &zone->uz_lock;
1568 rw_wlock(&uma_rwlock);
1569 LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
1570 rw_wunlock(&uma_rwlock);
1575 * Use the regular zone/keg/slab allocator.
1577 zone->uz_import = (uma_import)zone_import;
1578 zone->uz_release = (uma_release)zone_release;
1579 zone->uz_arg = zone;
1581 if (arg->flags & UMA_ZONE_SECONDARY) {
1582 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1583 zone->uz_init = arg->uminit;
1584 zone->uz_fini = arg->fini;
1585 zone->uz_lockptr = &keg->uk_lock;
1586 zone->uz_flags |= UMA_ZONE_SECONDARY;
1587 rw_wlock(&uma_rwlock);
1589 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1590 if (LIST_NEXT(z, uz_link) == NULL) {
1591 LIST_INSERT_AFTER(z, zone, uz_link);
1596 rw_wunlock(&uma_rwlock);
1597 } else if (keg == NULL) {
1598 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1599 arg->align, arg->flags)) == NULL)
1602 struct uma_kctor_args karg;
1605 /* We should only be here from uma_startup() */
1606 karg.size = arg->size;
1607 karg.uminit = arg->uminit;
1608 karg.fini = arg->fini;
1609 karg.align = arg->align;
1610 karg.flags = arg->flags;
1612 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1619 * Link in the first keg.
1621 zone->uz_klink.kl_keg = keg;
1622 LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1623 zone->uz_lockptr = &keg->uk_lock;
1624 zone->uz_size = keg->uk_size;
1625 zone->uz_flags |= (keg->uk_flags &
1626 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1629 * Some internal zones don't have room allocated for the per cpu
1630 * caches. If we're internal, bail out here.
1632 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1633 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1634 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1639 if ((arg->flags & UMA_ZONE_MAXBUCKET) == 0)
1640 zone->uz_count = bucket_select(zone->uz_size);
1642 zone->uz_count = BUCKET_MAX;
1643 zone->uz_count_min = zone->uz_count;
1649 * Keg header dtor. This frees all data, destroys locks, frees the hash
1650 * table and removes the keg from the global list.
1652 * Arguments/Returns follow uma_dtor specifications
1656 keg_dtor(void *arg, int size, void *udata)
1660 keg = (uma_keg_t)arg;
1662 if (keg->uk_free != 0) {
1663 printf("Freed UMA keg (%s) was not empty (%d items). "
1664 " Lost %d pages of memory.\n",
1665 keg->uk_name ? keg->uk_name : "",
1666 keg->uk_free, keg->uk_pages);
1670 hash_free(&keg->uk_hash);
1678 * Arguments/Returns follow uma_dtor specifications
1682 zone_dtor(void *arg, int size, void *udata)
1688 zone = (uma_zone_t)arg;
1689 keg = zone_first_keg(zone);
1691 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1694 rw_wlock(&uma_rwlock);
1695 LIST_REMOVE(zone, uz_link);
1696 rw_wunlock(&uma_rwlock);
1698 * XXX there are some races here where
1699 * the zone can be drained but zone lock
1700 * released and then refilled before we
1701 * remove it... we dont care for now
1703 zone_drain_wait(zone, M_WAITOK);
1705 * Unlink all of our kegs.
1707 while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1708 klink->kl_keg = NULL;
1709 LIST_REMOVE(klink, kl_link);
1710 if (klink == &zone->uz_klink)
1712 free(klink, M_TEMP);
1715 * We only destroy kegs from non secondary zones.
1717 if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0) {
1718 rw_wlock(&uma_rwlock);
1719 LIST_REMOVE(keg, uk_link);
1720 rw_wunlock(&uma_rwlock);
1721 zone_free_item(kegs, keg, NULL, SKIP_NONE);
1723 ZONE_LOCK_FINI(zone);
1727 * Traverses every zone in the system and calls a callback
1730 * zfunc A pointer to a function which accepts a zone
1737 zone_foreach(void (*zfunc)(uma_zone_t))
1742 rw_rlock(&uma_rwlock);
1743 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1744 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1747 rw_runlock(&uma_rwlock);
1750 /* Public functions */
1753 uma_startup(void *mem, int npages)
1755 struct uma_zctor_args args;
1757 rw_init(&uma_rwlock, "UMA lock");
1759 /* "manually" create the initial zone */
1760 memset(&args, 0, sizeof(args));
1761 args.name = "UMA Kegs";
1762 args.size = sizeof(struct uma_keg);
1763 args.ctor = keg_ctor;
1764 args.dtor = keg_dtor;
1765 args.uminit = zero_init;
1767 args.keg = &masterkeg;
1768 args.align = 32 - 1;
1769 args.flags = UMA_ZFLAG_INTERNAL;
1770 /* The initial zone has no Per cpu queues so it's smaller */
1771 zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
1773 mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
1775 boot_pages = npages;
1777 args.name = "UMA Zones";
1778 args.size = sizeof(struct uma_zone) +
1779 (sizeof(struct uma_cache) * (mp_maxid + 1));
1780 args.ctor = zone_ctor;
1781 args.dtor = zone_dtor;
1782 args.uminit = zero_init;
1785 args.align = 32 - 1;
1786 args.flags = UMA_ZFLAG_INTERNAL;
1787 /* The initial zone has no Per cpu queues so it's smaller */
1788 zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1790 /* Now make a zone for slab headers */
1791 slabzone = uma_zcreate("UMA Slabs",
1792 sizeof(struct uma_slab),
1793 NULL, NULL, NULL, NULL,
1794 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1796 hashzone = uma_zcreate("UMA Hash",
1797 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1798 NULL, NULL, NULL, NULL,
1799 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1803 booted = UMA_STARTUP;
1810 booted = UMA_STARTUP2;
1812 sx_init(&uma_drain_lock, "umadrain");
1816 * Initialize our callout handle
1824 callout_init(&uma_callout, 1);
1825 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1829 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
1830 int align, uint32_t flags)
1832 struct uma_kctor_args args;
1835 args.uminit = uminit;
1837 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1840 return (zone_alloc_item(kegs, &args, M_WAITOK));
1845 uma_set_align(int align)
1848 if (align != UMA_ALIGN_CACHE)
1849 uma_align_cache = align;
1854 uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1855 uma_init uminit, uma_fini fini, int align, uint32_t flags)
1858 struct uma_zctor_args args;
1862 KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
1865 /* This stuff is essential for the zone ctor */
1866 memset(&args, 0, sizeof(args));
1871 args.uminit = uminit;
1875 * If a zone is being created with an empty constructor and
1876 * destructor, pass UMA constructor/destructor which checks for
1877 * memory use after free.
1879 if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOFREE))) &&
1880 ctor == NULL && dtor == NULL && uminit == NULL && fini == NULL) {
1881 args.ctor = trash_ctor;
1882 args.dtor = trash_dtor;
1883 args.uminit = trash_init;
1884 args.fini = trash_fini;
1891 if (booted < UMA_STARTUP2) {
1894 sx_slock(&uma_drain_lock);
1897 res = zone_alloc_item(zones, &args, M_WAITOK);
1899 sx_sunlock(&uma_drain_lock);
1905 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1906 uma_init zinit, uma_fini zfini, uma_zone_t master)
1908 struct uma_zctor_args args;
1913 keg = zone_first_keg(master);
1914 memset(&args, 0, sizeof(args));
1916 args.size = keg->uk_size;
1919 args.uminit = zinit;
1921 args.align = keg->uk_align;
1922 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
1925 if (booted < UMA_STARTUP2) {
1928 sx_slock(&uma_drain_lock);
1931 /* XXX Attaches only one keg of potentially many. */
1932 res = zone_alloc_item(zones, &args, M_WAITOK);
1934 sx_sunlock(&uma_drain_lock);
1940 uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
1941 uma_init zinit, uma_fini zfini, uma_import zimport,
1942 uma_release zrelease, void *arg, int flags)
1944 struct uma_zctor_args args;
1946 memset(&args, 0, sizeof(args));
1951 args.uminit = zinit;
1953 args.import = zimport;
1954 args.release = zrelease;
1959 return (zone_alloc_item(zones, &args, M_WAITOK));
1963 zone_lock_pair(uma_zone_t a, uma_zone_t b)
1967 mtx_lock_flags(b->uz_lockptr, MTX_DUPOK);
1970 mtx_lock_flags(a->uz_lockptr, MTX_DUPOK);
1975 zone_unlock_pair(uma_zone_t a, uma_zone_t b)
1983 uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
1990 klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
1992 zone_lock_pair(zone, master);
1994 * zone must use vtoslab() to resolve objects and must already be
1997 if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
1998 != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
2003 * The new master must also use vtoslab().
2005 if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
2011 * The underlying object must be the same size. rsize
2014 if (master->uz_size != zone->uz_size) {
2019 * Put it at the end of the list.
2021 klink->kl_keg = zone_first_keg(master);
2022 LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
2023 if (LIST_NEXT(kl, kl_link) == NULL) {
2024 LIST_INSERT_AFTER(kl, klink, kl_link);
2029 zone->uz_flags |= UMA_ZFLAG_MULTI;
2030 zone->uz_slab = zone_fetch_slab_multi;
2033 zone_unlock_pair(zone, master);
2035 free(klink, M_TEMP);
2043 uma_zdestroy(uma_zone_t zone)
2046 sx_slock(&uma_drain_lock);
2047 zone_free_item(zones, zone, NULL, SKIP_NONE);
2048 sx_sunlock(&uma_drain_lock);
2052 uma_zwait(uma_zone_t zone)
2056 item = uma_zalloc_arg(zone, NULL, M_WAITOK);
2057 uma_zfree(zone, item);
2062 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
2066 uma_bucket_t bucket;
2070 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2071 random_harvest_fast_uma(&zone, sizeof(zone), 1, RANDOM_UMA);
2073 /* This is the fast path allocation */
2074 CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d",
2075 curthread, zone->uz_name, zone, flags);
2077 if (flags & M_WAITOK) {
2078 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2079 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
2081 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2082 ("uma_zalloc_arg: called with spinlock or critical section held"));
2084 #ifdef DEBUG_MEMGUARD
2085 if (memguard_cmp_zone(zone)) {
2086 item = memguard_alloc(zone->uz_size, flags);
2088 if (zone->uz_init != NULL &&
2089 zone->uz_init(item, zone->uz_size, flags) != 0)
2091 if (zone->uz_ctor != NULL &&
2092 zone->uz_ctor(item, zone->uz_size, udata,
2094 zone->uz_fini(item, zone->uz_size);
2099 /* This is unfortunate but should not be fatal. */
2103 * If possible, allocate from the per-CPU cache. There are two
2104 * requirements for safe access to the per-CPU cache: (1) the thread
2105 * accessing the cache must not be preempted or yield during access,
2106 * and (2) the thread must not migrate CPUs without switching which
2107 * cache it accesses. We rely on a critical section to prevent
2108 * preemption and migration. We release the critical section in
2109 * order to acquire the zone mutex if we are unable to allocate from
2110 * the current cache; when we re-acquire the critical section, we
2111 * must detect and handle migration if it has occurred.
2115 cache = &zone->uz_cpu[cpu];
2118 bucket = cache->uc_allocbucket;
2119 if (bucket != NULL && bucket->ub_cnt > 0) {
2121 item = bucket->ub_bucket[bucket->ub_cnt];
2123 bucket->ub_bucket[bucket->ub_cnt] = NULL;
2125 KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
2128 if (zone->uz_ctor != NULL &&
2129 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2130 atomic_add_long(&zone->uz_fails, 1);
2131 zone_free_item(zone, item, udata, SKIP_DTOR);
2135 uma_dbg_alloc(zone, NULL, item);
2138 uma_zero_item(item, zone);
2143 * We have run out of items in our alloc bucket.
2144 * See if we can switch with our free bucket.
2146 bucket = cache->uc_freebucket;
2147 if (bucket != NULL && bucket->ub_cnt > 0) {
2149 "uma_zalloc: zone %s(%p) swapping empty with alloc",
2150 zone->uz_name, zone);
2151 cache->uc_freebucket = cache->uc_allocbucket;
2152 cache->uc_allocbucket = bucket;
2157 * Discard any empty allocation bucket while we hold no locks.
2159 bucket = cache->uc_allocbucket;
2160 cache->uc_allocbucket = NULL;
2163 bucket_free(zone, bucket, udata);
2165 /* Short-circuit for zones without buckets and low memory. */
2166 if (zone->uz_count == 0 || bucketdisable)
2170 * Attempt to retrieve the item from the per-CPU cache has failed, so
2171 * we must go back to the zone. This requires the zone lock, so we
2172 * must drop the critical section, then re-acquire it when we go back
2173 * to the cache. Since the critical section is released, we may be
2174 * preempted or migrate. As such, make sure not to maintain any
2175 * thread-local state specific to the cache from prior to releasing
2176 * the critical section.
2179 if (ZONE_TRYLOCK(zone) == 0) {
2180 /* Record contention to size the buckets. */
2186 cache = &zone->uz_cpu[cpu];
2189 * Since we have locked the zone we may as well send back our stats.
2191 atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
2192 atomic_add_long(&zone->uz_frees, cache->uc_frees);
2193 cache->uc_allocs = 0;
2194 cache->uc_frees = 0;
2196 /* See if we lost the race to fill the cache. */
2197 if (cache->uc_allocbucket != NULL) {
2203 * Check the zone's cache of buckets.
2205 if ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
2206 KASSERT(bucket->ub_cnt != 0,
2207 ("uma_zalloc_arg: Returning an empty bucket."));
2209 LIST_REMOVE(bucket, ub_link);
2210 cache->uc_allocbucket = bucket;
2214 /* We are no longer associated with this CPU. */
2218 * We bump the uz count when the cache size is insufficient to
2219 * handle the working set.
2221 if (lockfail && zone->uz_count < BUCKET_MAX)
2226 * Now lets just fill a bucket and put it on the free list. If that
2227 * works we'll restart the allocation from the beginning and it
2228 * will use the just filled bucket.
2230 bucket = zone_alloc_bucket(zone, udata, flags);
2231 CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
2232 zone->uz_name, zone, bucket);
2233 if (bucket != NULL) {
2237 cache = &zone->uz_cpu[cpu];
2239 * See if we lost the race or were migrated. Cache the
2240 * initialized bucket to make this less likely or claim
2241 * the memory directly.
2243 if (cache->uc_allocbucket == NULL)
2244 cache->uc_allocbucket = bucket;
2246 LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
2252 * We may not be able to get a bucket so return an actual item.
2255 item = zone_alloc_item(zone, udata, flags);
2261 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags)
2266 mtx_assert(&keg->uk_lock, MA_OWNED);
2269 if ((flags & M_USE_RESERVE) == 0)
2270 reserve = keg->uk_reserve;
2274 * Find a slab with some space. Prefer slabs that are partially
2275 * used over those that are totally full. This helps to reduce
2278 if (keg->uk_free > reserve) {
2279 if (!LIST_EMPTY(&keg->uk_part_slab)) {
2280 slab = LIST_FIRST(&keg->uk_part_slab);
2282 slab = LIST_FIRST(&keg->uk_free_slab);
2283 LIST_REMOVE(slab, us_link);
2284 LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2287 MPASS(slab->us_keg == keg);
2292 * M_NOVM means don't ask at all!
2297 if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2298 keg->uk_flags |= UMA_ZFLAG_FULL;
2300 * If this is not a multi-zone, set the FULL bit.
2301 * Otherwise slab_multi() takes care of it.
2303 if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) {
2304 zone->uz_flags |= UMA_ZFLAG_FULL;
2305 zone_log_warning(zone);
2306 zone_maxaction(zone);
2308 if (flags & M_NOWAIT)
2311 msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2314 slab = keg_alloc_slab(keg, zone, flags);
2316 * If we got a slab here it's safe to mark it partially used
2317 * and return. We assume that the caller is going to remove
2318 * at least one item.
2321 MPASS(slab->us_keg == keg);
2322 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2326 * We might not have been able to get a slab but another cpu
2327 * could have while we were unlocked. Check again before we
2336 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags)
2341 keg = zone_first_keg(zone);
2346 slab = keg_fetch_slab(keg, zone, flags);
2349 if (flags & (M_NOWAIT | M_NOVM))
2357 * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns
2358 * with the keg locked. On NULL no lock is held.
2360 * The last pointer is used to seed the search. It is not required.
2363 zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags)
2373 * Don't wait on the first pass. This will skip limit tests
2374 * as well. We don't want to block if we can find a provider
2377 flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2379 * Use the last slab allocated as a hint for where to start
2383 slab = keg_fetch_slab(last, zone, flags);
2389 * Loop until we have a slab incase of transient failures
2390 * while M_WAITOK is specified. I'm not sure this is 100%
2391 * required but we've done it for so long now.
2397 * Search the available kegs for slabs. Be careful to hold the
2398 * correct lock while calling into the keg layer.
2400 LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2401 keg = klink->kl_keg;
2403 if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2404 slab = keg_fetch_slab(keg, zone, flags);
2408 if (keg->uk_flags & UMA_ZFLAG_FULL)
2414 if (rflags & (M_NOWAIT | M_NOVM))
2418 * All kegs are full. XXX We can't atomically check all kegs
2419 * and sleep so just sleep for a short period and retry.
2421 if (full && !empty) {
2423 zone->uz_flags |= UMA_ZFLAG_FULL;
2425 zone_log_warning(zone);
2426 zone_maxaction(zone);
2427 msleep(zone, zone->uz_lockptr, PVM,
2428 "zonelimit", hz/100);
2429 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2438 slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
2443 MPASS(keg == slab->us_keg);
2444 mtx_assert(&keg->uk_lock, MA_OWNED);
2446 freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2447 BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2448 item = slab->us_data + (keg->uk_rsize * freei);
2449 slab->us_freecount--;
2452 /* Move this slab to the full list */
2453 if (slab->us_freecount == 0) {
2454 LIST_REMOVE(slab, us_link);
2455 LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2462 zone_import(uma_zone_t zone, void **bucket, int max, int flags)
2470 /* Try to keep the buckets totally full */
2471 for (i = 0; i < max; ) {
2472 if ((slab = zone->uz_slab(zone, keg, flags)) == NULL)
2475 while (slab->us_freecount && i < max) {
2476 bucket[i++] = slab_alloc_item(keg, slab);
2477 if (keg->uk_free <= keg->uk_reserve)
2480 /* Don't grab more than one slab at a time. */
2491 zone_alloc_bucket(uma_zone_t zone, void *udata, int flags)
2493 uma_bucket_t bucket;
2496 /* Don't wait for buckets, preserve caller's NOVM setting. */
2497 bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
2501 max = MIN(bucket->ub_entries, zone->uz_count);
2502 bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
2506 * Initialize the memory if necessary.
2508 if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
2511 for (i = 0; i < bucket->ub_cnt; i++)
2512 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
2516 * If we couldn't initialize the whole bucket, put the
2517 * rest back onto the freelist.
2519 if (i != bucket->ub_cnt) {
2520 zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
2521 bucket->ub_cnt - i);
2523 bzero(&bucket->ub_bucket[i],
2524 sizeof(void *) * (bucket->ub_cnt - i));
2530 if (bucket->ub_cnt == 0) {
2531 bucket_free(zone, bucket, udata);
2532 atomic_add_long(&zone->uz_fails, 1);
2540 * Allocates a single item from a zone.
2543 * zone The zone to alloc for.
2544 * udata The data to be passed to the constructor.
2545 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2548 * NULL if there is no memory and M_NOWAIT is set
2549 * An item if successful
2553 zone_alloc_item(uma_zone_t zone, void *udata, int flags)
2559 if (zone->uz_import(zone->uz_arg, &item, 1, flags) != 1)
2561 atomic_add_long(&zone->uz_allocs, 1);
2564 * We have to call both the zone's init (not the keg's init)
2565 * and the zone's ctor. This is because the item is going from
2566 * a keg slab directly to the user, and the user is expecting it
2567 * to be both zone-init'd as well as zone-ctor'd.
2569 if (zone->uz_init != NULL) {
2570 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
2571 zone_free_item(zone, item, udata, SKIP_FINI);
2575 if (zone->uz_ctor != NULL) {
2576 if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2577 zone_free_item(zone, item, udata, SKIP_DTOR);
2582 uma_dbg_alloc(zone, NULL, item);
2585 uma_zero_item(item, zone);
2587 CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
2588 zone->uz_name, zone);
2593 CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
2594 zone->uz_name, zone);
2595 atomic_add_long(&zone->uz_fails, 1);
2601 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
2604 uma_bucket_t bucket;
2608 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2609 random_harvest_fast_uma(&zone, sizeof(zone), 1, RANDOM_UMA);
2611 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
2614 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2615 ("uma_zfree_arg: called with spinlock or critical section held"));
2617 /* uma_zfree(..., NULL) does nothing, to match free(9). */
2620 #ifdef DEBUG_MEMGUARD
2621 if (is_memguard_addr(item)) {
2622 if (zone->uz_dtor != NULL)
2623 zone->uz_dtor(item, zone->uz_size, udata);
2624 if (zone->uz_fini != NULL)
2625 zone->uz_fini(item, zone->uz_size);
2626 memguard_free(item);
2631 if (zone->uz_flags & UMA_ZONE_MALLOC)
2632 uma_dbg_free(zone, udata, item);
2634 uma_dbg_free(zone, NULL, item);
2636 if (zone->uz_dtor != NULL)
2637 zone->uz_dtor(item, zone->uz_size, udata);
2640 * The race here is acceptable. If we miss it we'll just have to wait
2641 * a little longer for the limits to be reset.
2643 if (zone->uz_flags & UMA_ZFLAG_FULL)
2647 * If possible, free to the per-CPU cache. There are two
2648 * requirements for safe access to the per-CPU cache: (1) the thread
2649 * accessing the cache must not be preempted or yield during access,
2650 * and (2) the thread must not migrate CPUs without switching which
2651 * cache it accesses. We rely on a critical section to prevent
2652 * preemption and migration. We release the critical section in
2653 * order to acquire the zone mutex if we are unable to free to the
2654 * current cache; when we re-acquire the critical section, we must
2655 * detect and handle migration if it has occurred.
2660 cache = &zone->uz_cpu[cpu];
2664 * Try to free into the allocbucket first to give LIFO ordering
2665 * for cache-hot datastructures. Spill over into the freebucket
2666 * if necessary. Alloc will swap them if one runs dry.
2668 bucket = cache->uc_allocbucket;
2669 if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries)
2670 bucket = cache->uc_freebucket;
2671 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2672 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
2673 ("uma_zfree: Freeing to non free bucket index."));
2674 bucket->ub_bucket[bucket->ub_cnt] = item;
2682 * We must go back the zone, which requires acquiring the zone lock,
2683 * which in turn means we must release and re-acquire the critical
2684 * section. Since the critical section is released, we may be
2685 * preempted or migrate. As such, make sure not to maintain any
2686 * thread-local state specific to the cache from prior to releasing
2687 * the critical section.
2690 if (zone->uz_count == 0 || bucketdisable)
2694 if (ZONE_TRYLOCK(zone) == 0) {
2695 /* Record contention to size the buckets. */
2701 cache = &zone->uz_cpu[cpu];
2704 * Since we have locked the zone we may as well send back our stats.
2706 atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
2707 atomic_add_long(&zone->uz_frees, cache->uc_frees);
2708 cache->uc_allocs = 0;
2709 cache->uc_frees = 0;
2711 bucket = cache->uc_freebucket;
2712 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2716 cache->uc_freebucket = NULL;
2717 /* We are no longer associated with this CPU. */
2720 /* Can we throw this on the zone full list? */
2721 if (bucket != NULL) {
2723 "uma_zfree: zone %s(%p) putting bucket %p on free list",
2724 zone->uz_name, zone, bucket);
2725 /* ub_cnt is pointing to the last free item */
2726 KASSERT(bucket->ub_cnt != 0,
2727 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2728 LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
2732 * We bump the uz count when the cache size is insufficient to
2733 * handle the working set.
2735 if (lockfail && zone->uz_count < BUCKET_MAX)
2739 bucket = bucket_alloc(zone, udata, M_NOWAIT);
2740 CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p",
2741 zone->uz_name, zone, bucket);
2745 cache = &zone->uz_cpu[cpu];
2746 if (cache->uc_freebucket == NULL) {
2747 cache->uc_freebucket = bucket;
2751 * We lost the race, start over. We have to drop our
2752 * critical section to free the bucket.
2755 bucket_free(zone, bucket, udata);
2760 * If nothing else caught this, we'll just do an internal free.
2763 zone_free_item(zone, item, udata, SKIP_DTOR);
2769 slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item)
2773 mtx_assert(&keg->uk_lock, MA_OWNED);
2774 MPASS(keg == slab->us_keg);
2776 /* Do we need to remove from any lists? */
2777 if (slab->us_freecount+1 == keg->uk_ipers) {
2778 LIST_REMOVE(slab, us_link);
2779 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2780 } else if (slab->us_freecount == 0) {
2781 LIST_REMOVE(slab, us_link);
2782 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2785 /* Slab management. */
2786 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
2787 BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
2788 slab->us_freecount++;
2790 /* Keg statistics. */
2795 zone_release(uma_zone_t zone, void **bucket, int cnt)
2805 keg = zone_first_keg(zone);
2807 for (i = 0; i < cnt; i++) {
2809 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
2810 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
2811 if (zone->uz_flags & UMA_ZONE_HASH) {
2812 slab = hash_sfind(&keg->uk_hash, mem);
2814 mem += keg->uk_pgoff;
2815 slab = (uma_slab_t)mem;
2818 slab = vtoslab((vm_offset_t)item);
2819 if (slab->us_keg != keg) {
2825 slab_free_item(keg, slab, item);
2826 if (keg->uk_flags & UMA_ZFLAG_FULL) {
2827 if (keg->uk_pages < keg->uk_maxpages) {
2828 keg->uk_flags &= ~UMA_ZFLAG_FULL;
2833 * We can handle one more allocation. Since we're
2834 * clearing ZFLAG_FULL, wake up all procs blocked
2835 * on pages. This should be uncommon, so keeping this
2836 * simple for now (rather than adding count of blocked
2845 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2853 * Frees a single item to any zone.
2856 * zone The zone to free to
2857 * item The item we're freeing
2858 * udata User supplied data for the dtor
2859 * skip Skip dtors and finis
2862 zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
2866 if (skip == SKIP_NONE) {
2867 if (zone->uz_flags & UMA_ZONE_MALLOC)
2868 uma_dbg_free(zone, udata, item);
2870 uma_dbg_free(zone, NULL, item);
2873 if (skip < SKIP_DTOR && zone->uz_dtor)
2874 zone->uz_dtor(item, zone->uz_size, udata);
2876 if (skip < SKIP_FINI && zone->uz_fini)
2877 zone->uz_fini(item, zone->uz_size);
2879 atomic_add_long(&zone->uz_frees, 1);
2880 zone->uz_release(zone->uz_arg, &item, 1);
2885 uma_zone_set_max(uma_zone_t zone, int nitems)
2889 keg = zone_first_keg(zone);
2893 keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
2894 if (keg->uk_maxpages * keg->uk_ipers < nitems)
2895 keg->uk_maxpages += keg->uk_ppera;
2896 nitems = (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers;
2904 uma_zone_get_max(uma_zone_t zone)
2909 keg = zone_first_keg(zone);
2913 nitems = (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers;
2921 uma_zone_set_warning(uma_zone_t zone, const char *warning)
2925 zone->uz_warning = warning;
2931 uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
2935 TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
2941 uma_zone_get_cur(uma_zone_t zone)
2947 nitems = zone->uz_allocs - zone->uz_frees;
2950 * See the comment in sysctl_vm_zone_stats() regarding the
2951 * safety of accessing the per-cpu caches. With the zone lock
2952 * held, it is safe, but can potentially result in stale data.
2954 nitems += zone->uz_cpu[i].uc_allocs -
2955 zone->uz_cpu[i].uc_frees;
2959 return (nitems < 0 ? 0 : nitems);
2964 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2968 keg = zone_first_keg(zone);
2969 KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
2971 KASSERT(keg->uk_pages == 0,
2972 ("uma_zone_set_init on non-empty keg"));
2973 keg->uk_init = uminit;
2979 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2983 keg = zone_first_keg(zone);
2984 KASSERT(keg != NULL, ("uma_zone_set_fini: Invalid zone type"));
2986 KASSERT(keg->uk_pages == 0,
2987 ("uma_zone_set_fini on non-empty keg"));
2988 keg->uk_fini = fini;
2994 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2998 KASSERT(zone_first_keg(zone)->uk_pages == 0,
2999 ("uma_zone_set_zinit on non-empty keg"));
3000 zone->uz_init = zinit;
3006 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
3010 KASSERT(zone_first_keg(zone)->uk_pages == 0,
3011 ("uma_zone_set_zfini on non-empty keg"));
3012 zone->uz_fini = zfini;
3017 /* XXX uk_freef is not actually used with the zone locked */
3019 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
3023 keg = zone_first_keg(zone);
3024 KASSERT(keg != NULL, ("uma_zone_set_freef: Invalid zone type"));
3026 keg->uk_freef = freef;
3031 /* XXX uk_allocf is not actually used with the zone locked */
3033 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
3037 keg = zone_first_keg(zone);
3039 keg->uk_allocf = allocf;
3045 uma_zone_reserve(uma_zone_t zone, int items)
3049 keg = zone_first_keg(zone);
3053 keg->uk_reserve = items;
3061 uma_zone_reserve_kva(uma_zone_t zone, int count)
3067 keg = zone_first_keg(zone);
3070 pages = count / keg->uk_ipers;
3072 if (pages * keg->uk_ipers < count)
3074 pages *= keg->uk_ppera;
3076 #ifdef UMA_MD_SMALL_ALLOC
3077 if (keg->uk_ppera > 1) {
3081 kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
3089 keg->uk_maxpages = pages;
3090 #ifdef UMA_MD_SMALL_ALLOC
3091 keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
3093 keg->uk_allocf = noobj_alloc;
3095 keg->uk_flags |= UMA_ZONE_NOFREE;
3103 uma_prealloc(uma_zone_t zone, int items)
3109 keg = zone_first_keg(zone);
3113 slabs = items / keg->uk_ipers;
3114 if (slabs * keg->uk_ipers < items)
3117 slab = keg_alloc_slab(keg, zone, M_WAITOK);
3120 MPASS(slab->us_keg == keg);
3121 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
3129 uma_reclaim_locked(bool kmem_danger)
3132 CTR0(KTR_UMA, "UMA: vm asked us to release pages!");
3133 sx_assert(&uma_drain_lock, SA_XLOCKED);
3135 zone_foreach(zone_drain);
3136 if (vm_page_count_min() || kmem_danger) {
3137 cache_drain_safe(NULL);
3138 zone_foreach(zone_drain);
3141 * Some slabs may have been freed but this zone will be visited early
3142 * we visit again so that we can free pages that are empty once other
3143 * zones are drained. We have to do the same for buckets.
3145 zone_drain(slabzone);
3146 bucket_zone_drain();
3153 sx_xlock(&uma_drain_lock);
3154 uma_reclaim_locked(false);
3155 sx_xunlock(&uma_drain_lock);
3158 static volatile int uma_reclaim_needed;
3161 uma_reclaim_wakeup(void)
3164 if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
3165 wakeup(uma_reclaim);
3169 uma_reclaim_worker(void *arg __unused)
3173 sx_xlock(&uma_drain_lock);
3174 while (atomic_load_int(&uma_reclaim_needed) == 0)
3175 sx_sleep(uma_reclaim, &uma_drain_lock, PVM, "umarcl",
3177 sx_xunlock(&uma_drain_lock);
3178 EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
3179 sx_xlock(&uma_drain_lock);
3180 uma_reclaim_locked(true);
3181 atomic_store_int(&uma_reclaim_needed, 0);
3182 sx_xunlock(&uma_drain_lock);
3183 /* Don't fire more than once per-second. */
3184 pause("umarclslp", hz);
3190 uma_zone_exhausted(uma_zone_t zone)
3195 full = (zone->uz_flags & UMA_ZFLAG_FULL);
3201 uma_zone_exhausted_nolock(uma_zone_t zone)
3203 return (zone->uz_flags & UMA_ZFLAG_FULL);
3207 uma_large_malloc(vm_size_t size, int wait)
3213 slab = zone_alloc_item(slabzone, NULL, wait);
3216 mem = page_alloc(NULL, size, &flags, wait);
3218 vsetslab((vm_offset_t)mem, slab);
3219 slab->us_data = mem;
3220 slab->us_flags = flags | UMA_SLAB_MALLOC;
3221 slab->us_size = size;
3222 uma_total_inc(size);
3224 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3231 uma_large_free(uma_slab_t slab)
3234 page_free(slab->us_data, slab->us_size, slab->us_flags);
3235 uma_total_dec(slab->us_size);
3236 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3240 uma_zero_item(void *item, uma_zone_t zone)
3244 if (zone->uz_flags & UMA_ZONE_PCPU) {
3246 bzero(zpcpu_get_cpu(item, i), zone->uz_size);
3248 bzero(item, zone->uz_size);
3255 return (uma_kmem_limit);
3259 uma_set_limit(unsigned long limit)
3262 uma_kmem_limit = limit;
3269 return (uma_kmem_total);
3276 return (uma_kmem_limit - uma_kmem_total);
3280 uma_print_stats(void)
3282 zone_foreach(uma_print_zone);
3286 slab_print(uma_slab_t slab)
3288 printf("slab: keg %p, data %p, freecount %d\n",
3289 slab->us_keg, slab->us_data, slab->us_freecount);
3293 cache_print(uma_cache_t cache)
3295 printf("alloc: %p(%d), free: %p(%d)\n",
3296 cache->uc_allocbucket,
3297 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3298 cache->uc_freebucket,
3299 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3303 uma_print_keg(uma_keg_t keg)
3307 printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3308 "out %d free %d limit %d\n",
3309 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3310 keg->uk_ipers, keg->uk_ppera,
3311 (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
3312 keg->uk_free, (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3313 printf("Part slabs:\n");
3314 LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
3316 printf("Free slabs:\n");
3317 LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
3319 printf("Full slabs:\n");
3320 LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
3325 uma_print_zone(uma_zone_t zone)
3331 printf("zone: %s(%p) size %d flags %#x\n",
3332 zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3333 LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3334 uma_print_keg(kl->kl_keg);
3336 cache = &zone->uz_cpu[i];
3337 printf("CPU %d Cache:\n", i);
3344 * Generate statistics across both the zone and its per-cpu cache's. Return
3345 * desired statistics if the pointer is non-NULL for that statistic.
3347 * Note: does not update the zone statistics, as it can't safely clear the
3348 * per-CPU cache statistic.
3350 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3351 * safe from off-CPU; we should modify the caches to track this information
3352 * directly so that we don't have to.
3355 uma_zone_sumstat(uma_zone_t z, int *cachefreep, uint64_t *allocsp,
3356 uint64_t *freesp, uint64_t *sleepsp)
3359 uint64_t allocs, frees, sleeps;
3362 allocs = frees = sleeps = 0;
3365 cache = &z->uz_cpu[cpu];
3366 if (cache->uc_allocbucket != NULL)
3367 cachefree += cache->uc_allocbucket->ub_cnt;
3368 if (cache->uc_freebucket != NULL)
3369 cachefree += cache->uc_freebucket->ub_cnt;
3370 allocs += cache->uc_allocs;
3371 frees += cache->uc_frees;
3373 allocs += z->uz_allocs;
3374 frees += z->uz_frees;
3375 sleeps += z->uz_sleeps;
3376 if (cachefreep != NULL)
3377 *cachefreep = cachefree;
3378 if (allocsp != NULL)
3382 if (sleepsp != NULL)
3388 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
3395 rw_rlock(&uma_rwlock);
3396 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3397 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3400 rw_runlock(&uma_rwlock);
3401 return (sysctl_handle_int(oidp, &count, 0, req));
3405 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
3407 struct uma_stream_header ush;
3408 struct uma_type_header uth;
3409 struct uma_percpu_stat ups;
3410 uma_bucket_t bucket;
3417 int count, error, i;
3419 error = sysctl_wire_old_buffer(req, 0);
3422 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
3423 sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
3426 rw_rlock(&uma_rwlock);
3427 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3428 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3433 * Insert stream header.
3435 bzero(&ush, sizeof(ush));
3436 ush.ush_version = UMA_STREAM_VERSION;
3437 ush.ush_maxcpus = (mp_maxid + 1);
3438 ush.ush_count = count;
3439 (void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
3441 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3442 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3443 bzero(&uth, sizeof(uth));
3445 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
3446 uth.uth_align = kz->uk_align;
3447 uth.uth_size = kz->uk_size;
3448 uth.uth_rsize = kz->uk_rsize;
3449 LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3451 uth.uth_maxpages += k->uk_maxpages;
3452 uth.uth_pages += k->uk_pages;
3453 uth.uth_keg_free += k->uk_free;
3454 uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3459 * A zone is secondary is it is not the first entry
3460 * on the keg's zone list.
3462 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3463 (LIST_FIRST(&kz->uk_zones) != z))
3464 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3466 LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
3467 uth.uth_zone_free += bucket->ub_cnt;
3468 uth.uth_allocs = z->uz_allocs;
3469 uth.uth_frees = z->uz_frees;
3470 uth.uth_fails = z->uz_fails;
3471 uth.uth_sleeps = z->uz_sleeps;
3472 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
3474 * While it is not normally safe to access the cache
3475 * bucket pointers while not on the CPU that owns the
3476 * cache, we only allow the pointers to be exchanged
3477 * without the zone lock held, not invalidated, so
3478 * accept the possible race associated with bucket
3479 * exchange during monitoring.
3481 for (i = 0; i < (mp_maxid + 1); i++) {
3482 bzero(&ups, sizeof(ups));
3483 if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
3487 cache = &z->uz_cpu[i];
3488 if (cache->uc_allocbucket != NULL)
3489 ups.ups_cache_free +=
3490 cache->uc_allocbucket->ub_cnt;
3491 if (cache->uc_freebucket != NULL)
3492 ups.ups_cache_free +=
3493 cache->uc_freebucket->ub_cnt;
3494 ups.ups_allocs = cache->uc_allocs;
3495 ups.ups_frees = cache->uc_frees;
3497 (void)sbuf_bcat(&sbuf, &ups, sizeof(ups));
3502 rw_runlock(&uma_rwlock);
3503 error = sbuf_finish(&sbuf);
3509 sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
3511 uma_zone_t zone = *(uma_zone_t *)arg1;
3514 max = uma_zone_get_max(zone);
3515 error = sysctl_handle_int(oidp, &max, 0, req);
3516 if (error || !req->newptr)
3519 uma_zone_set_max(zone, max);
3525 sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
3527 uma_zone_t zone = *(uma_zone_t *)arg1;
3530 cur = uma_zone_get_cur(zone);
3531 return (sysctl_handle_int(oidp, &cur, 0, req));
3536 uma_dbg_getslab(uma_zone_t zone, void *item)
3542 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
3543 if (zone->uz_flags & UMA_ZONE_VTOSLAB) {
3544 slab = vtoslab((vm_offset_t)mem);
3547 * It is safe to return the slab here even though the
3548 * zone is unlocked because the item's allocation state
3549 * essentially holds a reference.
3552 keg = LIST_FIRST(&zone->uz_kegs)->kl_keg;
3553 if (keg->uk_flags & UMA_ZONE_HASH)
3554 slab = hash_sfind(&keg->uk_hash, mem);
3556 slab = (uma_slab_t)(mem + keg->uk_pgoff);
3564 * Set up the slab's freei data such that uma_dbg_free can function.
3568 uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
3573 if (zone_first_keg(zone) == NULL)
3576 slab = uma_dbg_getslab(zone, item);
3578 panic("uma: item %p did not belong to zone %s\n",
3579 item, zone->uz_name);
3582 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3584 if (BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
3585 panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
3586 item, zone, zone->uz_name, slab, freei);
3587 BIT_SET_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
3593 * Verifies freed addresses. Checks for alignment, valid slab membership
3594 * and duplicate frees.
3598 uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
3603 if (zone_first_keg(zone) == NULL)
3606 slab = uma_dbg_getslab(zone, item);
3608 panic("uma: Freed item %p did not belong to zone %s\n",
3609 item, zone->uz_name);
3612 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3614 if (freei >= keg->uk_ipers)
3615 panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
3616 item, zone, zone->uz_name, slab, freei);
3618 if (((freei * keg->uk_rsize) + slab->us_data) != item)
3619 panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
3620 item, zone, zone->uz_name, slab, freei);
3622 if (!BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
3623 panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
3624 item, zone, zone->uz_name, slab, freei);
3626 BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
3628 #endif /* INVARIANTS */
3631 DB_SHOW_COMMAND(uma, db_show_uma)
3633 uint64_t allocs, frees, sleeps;
3634 uma_bucket_t bucket;
3639 db_printf("%18s %8s %8s %8s %12s %8s %8s\n", "Zone", "Size", "Used",
3640 "Free", "Requests", "Sleeps", "Bucket");
3641 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3642 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3643 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
3644 allocs = z->uz_allocs;
3645 frees = z->uz_frees;
3646 sleeps = z->uz_sleeps;
3649 uma_zone_sumstat(z, &cachefree, &allocs,
3651 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
3652 (LIST_FIRST(&kz->uk_zones) != z)))
3653 cachefree += kz->uk_free;
3654 LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
3655 cachefree += bucket->ub_cnt;
3656 db_printf("%18s %8ju %8jd %8d %12ju %8ju %8u\n",
3657 z->uz_name, (uintmax_t)kz->uk_size,
3658 (intmax_t)(allocs - frees), cachefree,
3659 (uintmax_t)allocs, sleeps, z->uz_count);
3666 DB_SHOW_COMMAND(umacache, db_show_umacache)
3668 uint64_t allocs, frees;
3669 uma_bucket_t bucket;
3673 db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
3674 "Requests", "Bucket");
3675 LIST_FOREACH(z, &uma_cachezones, uz_link) {
3676 uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL);
3677 LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
3678 cachefree += bucket->ub_cnt;
3679 db_printf("%18s %8ju %8jd %8d %12ju %8u\n",
3680 z->uz_name, (uintmax_t)z->uz_size,
3681 (intmax_t)(allocs - frees), cachefree,
3682 (uintmax_t)allocs, z->uz_count);